2 * lxc: linux Container library
4 * (C) Copyright IBM Corp. 2007, 2008
7 * Daniel Lezcano <daniel.lezcano at free.fr>
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
27 #include <arpa/inet.h>
31 #include <linux/netlink.h>
32 #include <linux/rtnetlink.h>
33 #include <linux/sockios.h>
34 #include <net/ethernet.h>
36 #include <net/if_arp.h>
37 #include <netinet/in.h>
41 #include <sys/inotify.h>
42 #include <sys/ioctl.h>
43 #include <sys/param.h>
44 #include <sys/socket.h>
46 #include <sys/types.h>
50 #include "../include/netns_ifaddrs.h"
54 #include "file_utils.h"
57 #include "memory_utils.h"
60 #include "raw_syscalls.h"
61 #include "syscall_wrappers.h"
65 #include "include/strlcpy.h"
68 lxc_log_define(network
, lxc
);
70 typedef int (*instantiate_cb
)(struct lxc_handler
*, struct lxc_netdev
*);
71 static const char loop_device
[] = "lo";
73 static int lxc_ip_route_dest(__u16 nlmsg_type
, int family
, int ifindex
, void *dest
, unsigned int netmask
)
76 struct nl_handler nlh
;
78 struct nlmsg
*answer
= NULL
, *nlmsg
= NULL
;
80 addrlen
= family
== AF_INET
? sizeof(struct in_addr
)
81 : sizeof(struct in6_addr
);
83 err
= netlink_open(&nlh
, NETLINK_ROUTE
);
88 nlmsg
= nlmsg_alloc(NLMSG_GOOD_SIZE
);
92 answer
= nlmsg_alloc_reserve(NLMSG_GOOD_SIZE
);
96 nlmsg
->nlmsghdr
->nlmsg_flags
=
97 NLM_F_ACK
| NLM_F_REQUEST
| NLM_F_CREATE
| NLM_F_EXCL
;
98 nlmsg
->nlmsghdr
->nlmsg_type
= nlmsg_type
;
100 rt
= nlmsg_reserve(nlmsg
, sizeof(struct rtmsg
));
103 rt
->rtm_family
= family
;
104 rt
->rtm_table
= RT_TABLE_MAIN
;
105 rt
->rtm_scope
= RT_SCOPE_LINK
;
106 rt
->rtm_protocol
= RTPROT_BOOT
;
107 rt
->rtm_type
= RTN_UNICAST
;
108 rt
->rtm_dst_len
= netmask
;
111 if (nla_put_buffer(nlmsg
, RTA_DST
, dest
, addrlen
))
113 if (nla_put_u32(nlmsg
, RTA_OIF
, ifindex
))
115 err
= netlink_transaction(&nlh
, nlmsg
, answer
);
123 static int lxc_ipv4_dest_add(int ifindex
, struct in_addr
*dest
, unsigned int netmask
)
125 return lxc_ip_route_dest(RTM_NEWROUTE
, AF_INET
, ifindex
, dest
, netmask
);
128 static int lxc_ipv6_dest_add(int ifindex
, struct in6_addr
*dest
, unsigned int netmask
)
130 return lxc_ip_route_dest(RTM_NEWROUTE
, AF_INET6
, ifindex
, dest
, netmask
);
133 static int lxc_ipv4_dest_del(int ifindex
, struct in_addr
*dest
, unsigned int netmask
)
135 return lxc_ip_route_dest(RTM_DELROUTE
, AF_INET
, ifindex
, dest
, netmask
);
138 static int lxc_ipv6_dest_del(int ifindex
, struct in6_addr
*dest
, unsigned int netmask
)
140 return lxc_ip_route_dest(RTM_DELROUTE
, AF_INET6
, ifindex
, dest
, netmask
);
143 static int lxc_setup_ipv4_routes(struct lxc_list
*ip
, int ifindex
)
145 struct lxc_list
*iterator
;
148 lxc_list_for_each(iterator
, ip
) {
149 struct lxc_inetdev
*inetdev
= iterator
->elem
;
151 err
= lxc_ipv4_dest_add(ifindex
, &inetdev
->addr
, inetdev
->prefix
);
153 SYSERROR("Failed to setup ipv4 route for network device "
154 "with ifindex %d", ifindex
);
155 return minus_one_set_errno(-err
);
162 static int lxc_setup_ipv6_routes(struct lxc_list
*ip
, int ifindex
)
164 struct lxc_list
*iterator
;
167 lxc_list_for_each(iterator
, ip
) {
168 struct lxc_inet6dev
*inet6dev
= iterator
->elem
;
170 err
= lxc_ipv6_dest_add(ifindex
, &inet6dev
->addr
, inet6dev
->prefix
);
172 SYSERROR("Failed to setup ipv6 route for network device "
173 "with ifindex %d", ifindex
);
174 return minus_one_set_errno(-err
);
181 static int instantiate_veth(struct lxc_handler
*handler
, struct lxc_netdev
*netdev
)
183 int bridge_index
, err
;
185 char veth1buf
[IFNAMSIZ
], veth2buf
[IFNAMSIZ
];
186 unsigned int mtu
= 0;
188 if (netdev
->priv
.veth_attr
.pair
[0] != '\0') {
189 veth1
= netdev
->priv
.veth_attr
.pair
;
190 if (handler
->conf
->reboot
)
191 lxc_netdev_delete_by_name(veth1
);
193 err
= snprintf(veth1buf
, sizeof(veth1buf
), "vethXXXXXX");
194 if (err
< 0 || (size_t)err
>= sizeof(veth1buf
))
197 veth1
= lxc_mkifname(veth1buf
);
201 /* store away for deconf */
202 memcpy(netdev
->priv
.veth_attr
.veth1
, veth1
, IFNAMSIZ
);
205 err
= snprintf(veth2buf
, sizeof(veth2buf
), "vethXXXXXX");
206 if (err
< 0 || (size_t)err
>= sizeof(veth2buf
))
209 veth2
= lxc_mkifname(veth2buf
);
213 err
= lxc_veth_create(veth1
, veth2
);
216 SYSERROR("Failed to create veth pair \"%s\" and \"%s\"", veth1
, veth2
);
220 /* changing the high byte of the mac address to 0xfe, the bridge interface
221 * will always keep the host's mac address and not take the mac address
223 err
= setup_private_host_hw_addr(veth1
);
226 SYSERROR("Failed to change mac address of host interface \"%s\"", veth1
);
230 /* Retrieve ifindex of the host's veth device. */
231 netdev
->priv
.veth_attr
.ifindex
= if_nametoindex(veth1
);
232 if (!netdev
->priv
.veth_attr
.ifindex
) {
233 ERROR("Failed to retrieve ifindex for \"%s\"", veth1
);
237 /* Note that we're retrieving the container's ifindex in the host's
238 * network namespace because we need it to move the device from the
239 * host's network namespace to the container's network namespace later
242 netdev
->ifindex
= if_nametoindex(veth2
);
243 if (!netdev
->ifindex
) {
244 ERROR("Failed to retrieve ifindex for \"%s\"", veth2
);
249 if (lxc_safe_uint(netdev
->mtu
, &mtu
) < 0)
250 WARN("Failed to parse mtu");
252 INFO("Retrieved mtu %d", mtu
);
253 } else if (netdev
->link
[0] != '\0') {
254 bridge_index
= if_nametoindex(netdev
->link
);
256 mtu
= netdev_get_mtu(bridge_index
);
257 INFO("Retrieved mtu %d from %s", mtu
, netdev
->link
);
259 mtu
= netdev_get_mtu(netdev
->ifindex
);
260 INFO("Retrieved mtu %d from %s", mtu
, veth2
);
265 err
= lxc_netdev_set_mtu(veth1
, mtu
);
267 err
= lxc_netdev_set_mtu(veth2
, mtu
);
271 SYSERROR("Failed to set mtu \"%d\" for veth pair \"%s\" "
272 "and \"%s\"", mtu
, veth1
, veth2
);
277 if (netdev
->link
[0] != '\0') {
278 err
= lxc_bridge_attach(netdev
->link
, veth1
);
281 SYSERROR("Failed to attach \"%s\" to bridge \"%s\"",
282 veth1
, netdev
->link
);
285 INFO("Attached \"%s\" to bridge \"%s\"", veth1
, netdev
->link
);
288 err
= lxc_netdev_up(veth1
);
291 SYSERROR("Failed to set \"%s\" up", veth1
);
295 /* setup ipv4 routes on the host interface */
296 if (lxc_setup_ipv4_routes(&netdev
->priv
.veth_attr
.ipv4_routes
, netdev
->priv
.veth_attr
.ifindex
)) {
297 ERROR("Failed to setup ipv4 routes for network device \"%s\"", veth1
);
301 /* setup ipv6 routes on the host interface */
302 if (lxc_setup_ipv6_routes(&netdev
->priv
.veth_attr
.ipv6_routes
, netdev
->priv
.veth_attr
.ifindex
)) {
303 ERROR("Failed to setup ipv6 routes for network device \"%s\"", veth1
);
307 if (netdev
->upscript
) {
315 err
= run_script_argv(handler
->name
,
316 handler
->conf
->hooks_version
, "net",
317 netdev
->upscript
, "up", argv
);
322 DEBUG("Instantiated veth \"%s/%s\", index is \"%d\"", veth1
, veth2
,
328 if (netdev
->ifindex
!= 0)
329 lxc_netdev_delete_by_name(veth1
);
333 static int instantiate_macvlan(struct lxc_handler
*handler
, struct lxc_netdev
*netdev
)
337 unsigned int mtu
= 0;
339 if (netdev
->link
[0] == '\0') {
340 ERROR("No link for macvlan network device specified");
344 err
= snprintf(peer
, sizeof(peer
), "mcXXXXXX");
345 if (err
< 0 || (size_t)err
>= sizeof(peer
))
348 if (!lxc_mkifname(peer
))
351 err
= lxc_macvlan_create(netdev
->link
, peer
,
352 netdev
->priv
.macvlan_attr
.mode
);
355 SYSERROR("Failed to create macvlan interface \"%s\" on \"%s\"",
360 netdev
->ifindex
= if_nametoindex(peer
);
361 if (!netdev
->ifindex
) {
362 ERROR("Failed to retrieve ifindex for \"%s\"", peer
);
367 err
= lxc_safe_uint(netdev
->mtu
, &mtu
);
370 SYSERROR("Failed to parse mtu \"%s\" for interface \"%s\"", netdev
->mtu
, peer
);
374 err
= lxc_netdev_set_mtu(peer
, mtu
);
377 SYSERROR("Failed to set mtu \"%s\" for interface \"%s\"", netdev
->mtu
, peer
);
382 if (netdev
->upscript
) {
389 err
= run_script_argv(handler
->name
,
390 handler
->conf
->hooks_version
, "net",
391 netdev
->upscript
, "up", argv
);
396 DEBUG("Instantiated macvlan \"%s\" with ifindex is %d and mode %d",
397 peer
, netdev
->ifindex
, netdev
->priv
.macvlan_attr
.mode
);
402 lxc_netdev_delete_by_name(peer
);
406 static int lxc_ipvlan_create(const char *master
, const char *name
, int mode
, int isolation
)
409 struct ifinfomsg
*ifi
;
410 struct nl_handler nlh
;
411 struct rtattr
*nest
, *nest2
;
412 struct nlmsg
*answer
= NULL
, *nlmsg
= NULL
;
414 len
= strlen(master
);
415 if (len
== 1 || len
>= IFNAMSIZ
)
416 return minus_one_set_errno(EINVAL
);
419 if (len
== 1 || len
>= IFNAMSIZ
)
420 return minus_one_set_errno(EINVAL
);
422 index
= if_nametoindex(master
);
424 return minus_one_set_errno(EINVAL
);
426 err
= netlink_open(&nlh
, NETLINK_ROUTE
);
428 return minus_one_set_errno(-err
);
431 nlmsg
= nlmsg_alloc(NLMSG_GOOD_SIZE
);
435 answer
= nlmsg_alloc_reserve(NLMSG_GOOD_SIZE
);
439 nlmsg
->nlmsghdr
->nlmsg_flags
= NLM_F_REQUEST
| NLM_F_CREATE
| NLM_F_EXCL
| NLM_F_ACK
;
440 nlmsg
->nlmsghdr
->nlmsg_type
= RTM_NEWLINK
;
442 ifi
= nlmsg_reserve(nlmsg
, sizeof(struct ifinfomsg
));
446 ifi
->ifi_family
= AF_UNSPEC
;
449 nest
= nla_begin_nested(nlmsg
, IFLA_LINKINFO
);
453 if (nla_put_string(nlmsg
, IFLA_INFO_KIND
, "ipvlan"))
457 nest2
= nla_begin_nested(nlmsg
, IFLA_INFO_DATA
);
461 if (nla_put_u32(nlmsg
, IFLA_IPVLAN_MODE
, mode
))
464 /* if_link.h does not define the isolation flag value for bridge mode so we define it as 0
465 * and only send mode if mode >0 as default mode is bridge anyway according to ipvlan docs.
468 if (nla_put_u16(nlmsg
, IFLA_IPVLAN_ISOLATION
, isolation
))
472 nla_end_nested(nlmsg
, nest2
);
475 nla_end_nested(nlmsg
, nest
);
477 if (nla_put_u32(nlmsg
, IFLA_LINK
, index
))
480 if (nla_put_string(nlmsg
, IFLA_IFNAME
, name
))
483 err
= netlink_transaction(&nlh
, nlmsg
, answer
);
489 return minus_one_set_errno(-err
);
493 static int instantiate_ipvlan(struct lxc_handler
*handler
, struct lxc_netdev
*netdev
)
495 char peerbuf
[IFNAMSIZ
], *peer
;
497 unsigned int mtu
= 0;
499 if (netdev
->link
[0] == '\0') {
500 ERROR("No link for ipvlan network device specified");
504 err
= snprintf(peerbuf
, sizeof(peerbuf
), "ipXXXXXX");
505 if (err
< 0 || (size_t)err
>= sizeof(peerbuf
))
508 peer
= lxc_mkifname(peerbuf
);
512 err
= lxc_ipvlan_create(netdev
->link
, peer
, netdev
->priv
.ipvlan_attr
.mode
, netdev
->priv
.ipvlan_attr
.isolation
);
514 SYSERROR("Failed to create ipvlan interface \"%s\" on \"%s\"", peer
, netdev
->link
);
518 netdev
->ifindex
= if_nametoindex(peer
);
519 if (!netdev
->ifindex
) {
520 ERROR("Failed to retrieve ifindex for \"%s\"", peer
);
525 err
= lxc_safe_uint(netdev
->mtu
, &mtu
);
528 SYSERROR("Failed to parse mtu \"%s\" for interface \"%s\"", netdev
->mtu
, peer
);
532 err
= lxc_netdev_set_mtu(peer
, mtu
);
535 SYSERROR("Failed to set mtu \"%s\" for interface \"%s\"", netdev
->mtu
, peer
);
540 if (netdev
->upscript
) {
547 err
= run_script_argv(handler
->name
,
548 handler
->conf
->hooks_version
, "net",
549 netdev
->upscript
, "up", argv
);
554 DEBUG("Instantiated ipvlan \"%s\" with ifindex is %d and mode %d",
555 peer
, netdev
->ifindex
, netdev
->priv
.macvlan_attr
.mode
);
560 lxc_netdev_delete_by_name(peer
);
564 static int instantiate_vlan(struct lxc_handler
*handler
, struct lxc_netdev
*netdev
)
568 static uint16_t vlan_cntr
= 0;
569 unsigned int mtu
= 0;
571 if (netdev
->link
[0] == '\0') {
572 ERROR("No link for vlan network device specified");
576 err
= snprintf(peer
, sizeof(peer
), "vlan%d-%d", netdev
->priv
.vlan_attr
.vid
, vlan_cntr
++);
577 if (err
< 0 || (size_t)err
>= sizeof(peer
))
580 err
= lxc_vlan_create(netdev
->link
, peer
, netdev
->priv
.vlan_attr
.vid
);
583 SYSERROR("Failed to create vlan interface \"%s\" on \"%s\"",
588 netdev
->ifindex
= if_nametoindex(peer
);
589 if (!netdev
->ifindex
) {
590 ERROR("Failed to retrieve ifindex for \"%s\"", peer
);
595 err
= lxc_safe_uint(netdev
->mtu
, &mtu
);
598 SYSERROR("Failed to parse mtu \"%s\" for interface \"%s\"", netdev
->mtu
, peer
);
602 err
= lxc_netdev_set_mtu(peer
, mtu
);
605 SYSERROR("Failed to set mtu \"%s\" for interface \"%s\"", netdev
->mtu
, peer
);
610 if (netdev
->upscript
) {
617 err
= run_script_argv(handler
->name
,
618 handler
->conf
->hooks_version
, "net",
619 netdev
->upscript
, "up", argv
);
625 DEBUG("Instantiated vlan \"%s\" with ifindex is \"%d\"",
626 peer
, netdev
->ifindex
);
631 lxc_netdev_delete_by_name(peer
);
635 static int instantiate_phys(struct lxc_handler
*handler
, struct lxc_netdev
*netdev
)
637 int err
, mtu_orig
= 0;
638 unsigned int mtu
= 0;
640 if (netdev
->link
[0] == '\0') {
641 ERROR("No link for physical interface specified");
645 /* Note that we're retrieving the container's ifindex in the host's
646 * network namespace because we need it to move the device from the
647 * host's network namespace to the container's network namespace later
649 * Note that netdev->link will contain the name of the physical network
650 * device in the host's namespace.
652 netdev
->ifindex
= if_nametoindex(netdev
->link
);
653 if (!netdev
->ifindex
) {
654 ERROR("Failed to retrieve ifindex for \"%s\"", netdev
->link
);
658 /* Store the ifindex of the host's network device in the host's
661 netdev
->priv
.phys_attr
.ifindex
= netdev
->ifindex
;
663 /* Get original device MTU setting and store for restoration after container shutdown. */
664 mtu_orig
= netdev_get_mtu(netdev
->ifindex
);
666 SYSERROR("Failed to get original mtu for interface \"%s\"", netdev
->link
);
667 return minus_one_set_errno(-mtu_orig
);
670 netdev
->priv
.phys_attr
.mtu
= mtu_orig
;
673 err
= lxc_safe_uint(netdev
->mtu
, &mtu
);
676 SYSERROR("Failed to parse mtu \"%s\" for interface \"%s\"", netdev
->mtu
, netdev
->link
);
680 err
= lxc_netdev_set_mtu(netdev
->link
, mtu
);
683 SYSERROR("Failed to set mtu \"%s\" for interface \"%s\"", netdev
->mtu
, netdev
->link
);
688 if (netdev
->upscript
) {
695 err
= run_script_argv(handler
->name
,
696 handler
->conf
->hooks_version
, "net",
697 netdev
->upscript
, "up", argv
);
703 DEBUG("Instantiated phys \"%s\" with ifindex is \"%d\"", netdev
->link
, netdev
->ifindex
);
708 static int instantiate_empty(struct lxc_handler
*handler
, struct lxc_netdev
*netdev
)
717 if (!netdev
->upscript
)
720 ret
= run_script_argv(handler
->name
, handler
->conf
->hooks_version
,
721 "net", netdev
->upscript
, "up", argv
);
728 static int instantiate_none(struct lxc_handler
*handler
, struct lxc_netdev
*netdev
)
734 static instantiate_cb netdev_conf
[LXC_NET_MAXCONFTYPE
+ 1] = {
735 [LXC_NET_VETH
] = instantiate_veth
,
736 [LXC_NET_MACVLAN
] = instantiate_macvlan
,
737 [LXC_NET_IPVLAN
] = instantiate_ipvlan
,
738 [LXC_NET_VLAN
] = instantiate_vlan
,
739 [LXC_NET_PHYS
] = instantiate_phys
,
740 [LXC_NET_EMPTY
] = instantiate_empty
,
741 [LXC_NET_NONE
] = instantiate_none
,
744 static int shutdown_veth(struct lxc_handler
*handler
, struct lxc_netdev
*netdev
)
754 if (!netdev
->downscript
)
757 if (netdev
->priv
.veth_attr
.pair
[0] != '\0')
758 argv
[2] = netdev
->priv
.veth_attr
.pair
;
760 argv
[2] = netdev
->priv
.veth_attr
.veth1
;
762 ret
= run_script_argv(handler
->name
,
763 handler
->conf
->hooks_version
, "net",
764 netdev
->downscript
, "down", argv
);
771 static int shutdown_macvlan(struct lxc_handler
*handler
, struct lxc_netdev
*netdev
)
780 if (!netdev
->downscript
)
783 ret
= run_script_argv(handler
->name
, handler
->conf
->hooks_version
,
784 "net", netdev
->downscript
, "down", argv
);
791 static int shutdown_ipvlan(struct lxc_handler
*handler
, struct lxc_netdev
*netdev
)
800 if (!netdev
->downscript
)
803 ret
= run_script_argv(handler
->name
, handler
->conf
->hooks_version
,
804 "net", netdev
->downscript
, "down", argv
);
811 static int shutdown_vlan(struct lxc_handler
*handler
, struct lxc_netdev
*netdev
)
820 if (!netdev
->downscript
)
823 ret
= run_script_argv(handler
->name
, handler
->conf
->hooks_version
,
824 "net", netdev
->downscript
, "down", argv
);
831 static int shutdown_phys(struct lxc_handler
*handler
, struct lxc_netdev
*netdev
)
840 if (!netdev
->downscript
)
843 ret
= run_script_argv(handler
->name
, handler
->conf
->hooks_version
,
844 "net", netdev
->downscript
, "down", argv
);
851 static int shutdown_empty(struct lxc_handler
*handler
, struct lxc_netdev
*netdev
)
859 if (!netdev
->downscript
)
862 ret
= run_script_argv(handler
->name
, handler
->conf
->hooks_version
,
863 "net", netdev
->downscript
, "down", argv
);
870 static int shutdown_none(struct lxc_handler
*handler
, struct lxc_netdev
*netdev
)
875 static instantiate_cb netdev_deconf
[LXC_NET_MAXCONFTYPE
+ 1] = {
876 [LXC_NET_VETH
] = shutdown_veth
,
877 [LXC_NET_MACVLAN
] = shutdown_macvlan
,
878 [LXC_NET_IPVLAN
] = shutdown_ipvlan
,
879 [LXC_NET_VLAN
] = shutdown_vlan
,
880 [LXC_NET_PHYS
] = shutdown_phys
,
881 [LXC_NET_EMPTY
] = shutdown_empty
,
882 [LXC_NET_NONE
] = shutdown_none
,
885 static int lxc_netdev_move_by_index_fd(int ifindex
, int fd
, const char *ifname
)
888 struct nl_handler nlh
;
889 struct ifinfomsg
*ifi
;
890 struct nlmsg
*nlmsg
= NULL
;
892 err
= netlink_open(&nlh
, NETLINK_ROUTE
);
897 nlmsg
= nlmsg_alloc(NLMSG_GOOD_SIZE
);
901 nlmsg
->nlmsghdr
->nlmsg_flags
= NLM_F_REQUEST
| NLM_F_ACK
;
902 nlmsg
->nlmsghdr
->nlmsg_type
= RTM_NEWLINK
;
904 ifi
= nlmsg_reserve(nlmsg
, sizeof(struct ifinfomsg
));
907 ifi
->ifi_family
= AF_UNSPEC
;
908 ifi
->ifi_index
= ifindex
;
910 if (nla_put_u32(nlmsg
, IFLA_NET_NS_FD
, fd
))
913 if (ifname
!= NULL
) {
914 if (nla_put_string(nlmsg
, IFLA_IFNAME
, ifname
))
918 err
= netlink_transaction(&nlh
, nlmsg
, nlmsg
);
925 int lxc_netdev_move_by_index(int ifindex
, pid_t pid
, const char *ifname
)
928 struct nl_handler nlh
;
929 struct ifinfomsg
*ifi
;
930 struct nlmsg
*nlmsg
= NULL
;
932 err
= netlink_open(&nlh
, NETLINK_ROUTE
);
937 nlmsg
= nlmsg_alloc(NLMSG_GOOD_SIZE
);
941 nlmsg
->nlmsghdr
->nlmsg_flags
= NLM_F_REQUEST
| NLM_F_ACK
;
942 nlmsg
->nlmsghdr
->nlmsg_type
= RTM_NEWLINK
;
944 ifi
= nlmsg_reserve(nlmsg
, sizeof(struct ifinfomsg
));
947 ifi
->ifi_family
= AF_UNSPEC
;
948 ifi
->ifi_index
= ifindex
;
950 if (nla_put_u32(nlmsg
, IFLA_NET_NS_PID
, pid
))
953 if (ifname
!= NULL
) {
954 if (nla_put_string(nlmsg
, IFLA_IFNAME
, ifname
))
958 err
= netlink_transaction(&nlh
, nlmsg
, nlmsg
);
965 /* If we are asked to move a wireless interface, then we must actually move its
966 * phyN device. Detect that condition and return the physname here. The physname
967 * will be passed to lxc_netdev_move_wlan() which will free it when done.
969 #define PHYSNAME "/sys/class/net/%s/phy80211/name"
970 static char *is_wlan(const char *ifname
)
972 __do_free
char *path
= NULL
;
977 char *physname
= NULL
;
979 len
= strlen(ifname
) + strlen(PHYSNAME
) - 1;
980 path
= must_realloc(NULL
, len
+ 1);
981 ret
= snprintf(path
, len
, PHYSNAME
, ifname
);
982 if (ret
< 0 || (size_t)ret
>= len
)
985 f
= fopen(path
, "r");
989 /* Feh - sb.st_size is always 4096. */
990 fseek(f
, 0, SEEK_END
);
992 fseek(f
, 0, SEEK_SET
);
998 physname
= malloc(physlen
+ 1);
1004 memset(physname
, 0, physlen
+ 1);
1005 ret
= fread(physname
, 1, physlen
, f
);
1010 for (i
= 0; i
< physlen
; i
++) {
1011 if (physname
[i
] == '\n')
1014 if (physname
[i
] == '\0')
1025 static int lxc_netdev_rename_by_name_in_netns(pid_t pid
, const char *old
,
1035 return wait_for_pid(fpid
);
1037 if (!switch_to_ns(pid
, "net"))
1040 _exit(lxc_netdev_rename_by_name(old
, new));
1043 static int lxc_netdev_move_wlan(char *physname
, const char *ifname
, pid_t pid
,
1044 const char *newname
)
1050 /* Move phyN into the container. TODO - do this using netlink.
1051 * However, IIUC this involves a bit more complicated work to talk to
1052 * the 80211 module, so for now just call out to iw.
1054 cmd
= on_path("iw", NULL
);
1065 sprintf(pidstr
, "%d", pid
);
1066 execlp("iw", "iw", "phy", physname
, "set", "netns", pidstr
,
1068 _exit(EXIT_FAILURE
);
1071 if (wait_for_pid(fpid
))
1076 err
= lxc_netdev_rename_by_name_in_netns(pid
, ifname
, newname
);
1083 int lxc_netdev_move_by_name(const char *ifname
, pid_t pid
, const char* newname
)
1091 index
= if_nametoindex(ifname
);
1095 physname
= is_wlan(ifname
);
1097 return lxc_netdev_move_wlan(physname
, ifname
, pid
, newname
);
1099 return lxc_netdev_move_by_index(index
, pid
, newname
);
1102 int lxc_netdev_delete_by_index(int ifindex
)
1105 struct ifinfomsg
*ifi
;
1106 struct nl_handler nlh
;
1107 struct nlmsg
*answer
= NULL
, *nlmsg
= NULL
;
1109 err
= netlink_open(&nlh
, NETLINK_ROUTE
);
1114 nlmsg
= nlmsg_alloc(NLMSG_GOOD_SIZE
);
1118 answer
= nlmsg_alloc_reserve(NLMSG_GOOD_SIZE
);
1122 nlmsg
->nlmsghdr
->nlmsg_flags
= NLM_F_ACK
| NLM_F_REQUEST
;
1123 nlmsg
->nlmsghdr
->nlmsg_type
= RTM_DELLINK
;
1125 ifi
= nlmsg_reserve(nlmsg
, sizeof(struct ifinfomsg
));
1128 ifi
->ifi_family
= AF_UNSPEC
;
1129 ifi
->ifi_index
= ifindex
;
1131 err
= netlink_transaction(&nlh
, nlmsg
, answer
);
1133 netlink_close(&nlh
);
1139 int lxc_netdev_delete_by_name(const char *name
)
1143 index
= if_nametoindex(name
);
1147 return lxc_netdev_delete_by_index(index
);
1150 int lxc_netdev_rename_by_index(int ifindex
, const char *newname
)
1153 struct ifinfomsg
*ifi
;
1154 struct nl_handler nlh
;
1155 struct nlmsg
*answer
= NULL
, *nlmsg
= NULL
;
1157 err
= netlink_open(&nlh
, NETLINK_ROUTE
);
1161 len
= strlen(newname
);
1162 if (len
== 1 || len
>= IFNAMSIZ
) {
1168 nlmsg
= nlmsg_alloc(NLMSG_GOOD_SIZE
);
1172 answer
= nlmsg_alloc_reserve(NLMSG_GOOD_SIZE
);
1176 nlmsg
->nlmsghdr
->nlmsg_flags
= NLM_F_ACK
| NLM_F_REQUEST
;
1177 nlmsg
->nlmsghdr
->nlmsg_type
= RTM_NEWLINK
;
1179 ifi
= nlmsg_reserve(nlmsg
, sizeof(struct ifinfomsg
));
1182 ifi
->ifi_family
= AF_UNSPEC
;
1183 ifi
->ifi_index
= ifindex
;
1185 if (nla_put_string(nlmsg
, IFLA_IFNAME
, newname
))
1188 err
= netlink_transaction(&nlh
, nlmsg
, answer
);
1190 netlink_close(&nlh
);
1196 int lxc_netdev_rename_by_name(const char *oldname
, const char *newname
)
1200 len
= strlen(oldname
);
1201 if (len
== 1 || len
>= IFNAMSIZ
)
1204 index
= if_nametoindex(oldname
);
1208 return lxc_netdev_rename_by_index(index
, newname
);
1211 int netdev_set_flag(const char *name
, int flag
)
1213 int err
, index
, len
;
1214 struct ifinfomsg
*ifi
;
1215 struct nl_handler nlh
;
1216 struct nlmsg
*answer
= NULL
, *nlmsg
= NULL
;
1218 err
= netlink_open(&nlh
, NETLINK_ROUTE
);
1224 if (len
== 1 || len
>= IFNAMSIZ
)
1228 nlmsg
= nlmsg_alloc(NLMSG_GOOD_SIZE
);
1232 answer
= nlmsg_alloc_reserve(NLMSG_GOOD_SIZE
);
1237 index
= if_nametoindex(name
);
1241 nlmsg
->nlmsghdr
->nlmsg_flags
= NLM_F_REQUEST
| NLM_F_ACK
;
1242 nlmsg
->nlmsghdr
->nlmsg_type
= RTM_NEWLINK
;
1244 ifi
= nlmsg_reserve(nlmsg
, sizeof(struct ifinfomsg
));
1249 ifi
->ifi_family
= AF_UNSPEC
;
1250 ifi
->ifi_index
= index
;
1251 ifi
->ifi_change
|= IFF_UP
;
1252 ifi
->ifi_flags
|= flag
;
1254 err
= netlink_transaction(&nlh
, nlmsg
, answer
);
1256 netlink_close(&nlh
);
1262 int netdev_get_flag(const char *name
, int *flag
)
1264 int err
, index
, len
;
1265 struct ifinfomsg
*ifi
;
1266 struct nl_handler nlh
;
1267 struct nlmsg
*answer
= NULL
, *nlmsg
= NULL
;
1272 err
= netlink_open(&nlh
, NETLINK_ROUTE
);
1278 if (len
== 1 || len
>= IFNAMSIZ
)
1282 nlmsg
= nlmsg_alloc(NLMSG_GOOD_SIZE
);
1286 answer
= nlmsg_alloc_reserve(NLMSG_GOOD_SIZE
);
1291 index
= if_nametoindex(name
);
1295 nlmsg
->nlmsghdr
->nlmsg_flags
= NLM_F_REQUEST
;
1296 nlmsg
->nlmsghdr
->nlmsg_type
= RTM_GETLINK
;
1298 ifi
= nlmsg_reserve(nlmsg
, sizeof(struct ifinfomsg
));
1303 ifi
->ifi_family
= AF_UNSPEC
;
1304 ifi
->ifi_index
= index
;
1306 err
= netlink_transaction(&nlh
, nlmsg
, answer
);
1310 ifi
= NLMSG_DATA(answer
->nlmsghdr
);
1312 *flag
= ifi
->ifi_flags
;
1314 netlink_close(&nlh
);
1321 * \brief Check a interface is up or not.
1323 * \param name: name for the interface.
1326 * 0 means interface is down.
1327 * 1 means interface is up.
1328 * Others means error happened, and ret-value is the error number.
1330 int lxc_netdev_isup(const char *name
)
1334 err
= netdev_get_flag(name
, &flag
);
1344 int netdev_get_mtu(int ifindex
)
1346 int answer_len
, err
, res
;
1347 struct nl_handler nlh
;
1348 struct ifinfomsg
*ifi
;
1349 struct nlmsghdr
*msg
;
1350 int readmore
= 0, recv_len
= 0;
1351 struct nlmsg
*answer
= NULL
, *nlmsg
= NULL
;
1353 err
= netlink_open(&nlh
, NETLINK_ROUTE
);
1358 nlmsg
= nlmsg_alloc(NLMSG_GOOD_SIZE
);
1362 answer
= nlmsg_alloc_reserve(NLMSG_GOOD_SIZE
);
1366 /* Save the answer buffer length, since it will be overwritten
1367 * on the first receive (and we might need to receive more than
1370 answer_len
= answer
->nlmsghdr
->nlmsg_len
;
1372 nlmsg
->nlmsghdr
->nlmsg_flags
= NLM_F_REQUEST
| NLM_F_DUMP
;
1373 nlmsg
->nlmsghdr
->nlmsg_type
= RTM_GETLINK
;
1375 ifi
= nlmsg_reserve(nlmsg
, sizeof(struct ifinfomsg
));
1378 ifi
->ifi_family
= AF_UNSPEC
;
1380 /* Send the request for addresses, which returns all addresses
1381 * on all interfaces. */
1382 err
= netlink_send(&nlh
, nlmsg
);
1386 #pragma GCC diagnostic push
1387 #pragma GCC diagnostic ignored "-Wcast-align"
1390 /* Restore the answer buffer length, it might have been
1391 * overwritten by a previous receive.
1393 answer
->nlmsghdr
->nlmsg_len
= answer_len
;
1395 /* Get the (next) batch of reply messages */
1396 err
= netlink_rcv(&nlh
, answer
);
1402 /* Satisfy the typing for the netlink macros */
1403 msg
= answer
->nlmsghdr
;
1405 while (NLMSG_OK(msg
, recv_len
)) {
1407 /* Stop reading if we see an error message */
1408 if (msg
->nlmsg_type
== NLMSG_ERROR
) {
1409 struct nlmsgerr
*errmsg
=
1410 (struct nlmsgerr
*)NLMSG_DATA(msg
);
1411 err
= errmsg
->error
;
1415 /* Stop reading if we see a NLMSG_DONE message */
1416 if (msg
->nlmsg_type
== NLMSG_DONE
) {
1421 ifi
= NLMSG_DATA(msg
);
1422 if (ifi
->ifi_index
== ifindex
) {
1423 struct rtattr
*rta
= IFLA_RTA(ifi
);
1425 msg
->nlmsg_len
- NLMSG_LENGTH(sizeof(*ifi
));
1427 while (RTA_OK(rta
, attr_len
)) {
1428 /* Found a local address for the
1429 * requested interface, return it.
1431 if (rta
->rta_type
== IFLA_MTU
) {
1432 memcpy(&res
, RTA_DATA(rta
),
1437 rta
= RTA_NEXT(rta
, attr_len
);
1441 /* Keep reading more data from the socket if the last
1442 * message had the NLF_F_MULTI flag set.
1444 readmore
= (msg
->nlmsg_flags
& NLM_F_MULTI
);
1446 /* Look at the next message received in this buffer. */
1447 msg
= NLMSG_NEXT(msg
, recv_len
);
1451 #pragma GCC diagnostic pop
1453 /* If we end up here, we didn't find any result, so signal an error. */
1457 netlink_close(&nlh
);
1463 int lxc_netdev_set_mtu(const char *name
, int mtu
)
1465 int err
, index
, len
;
1466 struct ifinfomsg
*ifi
;
1467 struct nl_handler nlh
;
1468 struct nlmsg
*answer
= NULL
, *nlmsg
= NULL
;
1470 err
= netlink_open(&nlh
, NETLINK_ROUTE
);
1476 if (len
== 1 || len
>= IFNAMSIZ
)
1480 nlmsg
= nlmsg_alloc(NLMSG_GOOD_SIZE
);
1484 answer
= nlmsg_alloc_reserve(NLMSG_GOOD_SIZE
);
1489 index
= if_nametoindex(name
);
1493 nlmsg
->nlmsghdr
->nlmsg_flags
= NLM_F_REQUEST
| NLM_F_ACK
;
1494 nlmsg
->nlmsghdr
->nlmsg_type
= RTM_NEWLINK
;
1496 ifi
= nlmsg_reserve(nlmsg
, sizeof(struct ifinfomsg
));
1501 ifi
->ifi_family
= AF_UNSPEC
;
1502 ifi
->ifi_index
= index
;
1504 if (nla_put_u32(nlmsg
, IFLA_MTU
, mtu
))
1507 err
= netlink_transaction(&nlh
, nlmsg
, answer
);
1509 netlink_close(&nlh
);
1515 int lxc_netdev_up(const char *name
)
1517 return netdev_set_flag(name
, IFF_UP
);
1520 int lxc_netdev_down(const char *name
)
1522 return netdev_set_flag(name
, 0);
1525 int lxc_veth_create(const char *name1
, const char *name2
)
1528 struct ifinfomsg
*ifi
;
1529 struct nl_handler nlh
;
1530 struct rtattr
*nest1
, *nest2
, *nest3
;
1531 struct nlmsg
*answer
= NULL
, *nlmsg
= NULL
;
1533 err
= netlink_open(&nlh
, NETLINK_ROUTE
);
1538 len
= strlen(name1
);
1539 if (len
== 1 || len
>= IFNAMSIZ
)
1542 len
= strlen(name2
);
1543 if (len
== 1 || len
>= IFNAMSIZ
)
1547 nlmsg
= nlmsg_alloc(NLMSG_GOOD_SIZE
);
1551 answer
= nlmsg_alloc_reserve(NLMSG_GOOD_SIZE
);
1555 nlmsg
->nlmsghdr
->nlmsg_flags
=
1556 NLM_F_REQUEST
| NLM_F_CREATE
| NLM_F_EXCL
| NLM_F_ACK
;
1557 nlmsg
->nlmsghdr
->nlmsg_type
= RTM_NEWLINK
;
1559 ifi
= nlmsg_reserve(nlmsg
, sizeof(struct ifinfomsg
));
1562 ifi
->ifi_family
= AF_UNSPEC
;
1565 nest1
= nla_begin_nested(nlmsg
, IFLA_LINKINFO
);
1569 if (nla_put_string(nlmsg
, IFLA_INFO_KIND
, "veth"))
1572 nest2
= nla_begin_nested(nlmsg
, IFLA_INFO_DATA
);
1576 nest3
= nla_begin_nested(nlmsg
, VETH_INFO_PEER
);
1580 ifi
= nlmsg_reserve(nlmsg
, sizeof(struct ifinfomsg
));
1586 if (nla_put_string(nlmsg
, IFLA_IFNAME
, name2
))
1589 nla_end_nested(nlmsg
, nest3
);
1590 nla_end_nested(nlmsg
, nest2
);
1591 nla_end_nested(nlmsg
, nest1
);
1593 if (nla_put_string(nlmsg
, IFLA_IFNAME
, name1
))
1596 err
= netlink_transaction(&nlh
, nlmsg
, answer
);
1598 netlink_close(&nlh
);
1604 /* TODO: merge with lxc_macvlan_create */
1605 int lxc_vlan_create(const char *master
, const char *name
, unsigned short vlanid
)
1607 int err
, len
, lindex
;
1608 struct ifinfomsg
*ifi
;
1609 struct nl_handler nlh
;
1610 struct rtattr
*nest
, *nest2
;
1611 struct nlmsg
*answer
= NULL
, *nlmsg
= NULL
;
1613 err
= netlink_open(&nlh
, NETLINK_ROUTE
);
1618 len
= strlen(master
);
1619 if (len
== 1 || len
>= IFNAMSIZ
)
1623 if (len
== 1 || len
>= IFNAMSIZ
)
1627 nlmsg
= nlmsg_alloc(NLMSG_GOOD_SIZE
);
1631 answer
= nlmsg_alloc_reserve(NLMSG_GOOD_SIZE
);
1636 lindex
= if_nametoindex(master
);
1640 nlmsg
->nlmsghdr
->nlmsg_flags
=
1641 NLM_F_REQUEST
| NLM_F_CREATE
| NLM_F_EXCL
| NLM_F_ACK
;
1642 nlmsg
->nlmsghdr
->nlmsg_type
= RTM_NEWLINK
;
1644 ifi
= nlmsg_reserve(nlmsg
, sizeof(struct ifinfomsg
));
1649 ifi
->ifi_family
= AF_UNSPEC
;
1651 nest
= nla_begin_nested(nlmsg
, IFLA_LINKINFO
);
1655 if (nla_put_string(nlmsg
, IFLA_INFO_KIND
, "vlan"))
1658 nest2
= nla_begin_nested(nlmsg
, IFLA_INFO_DATA
);
1662 if (nla_put_u16(nlmsg
, IFLA_VLAN_ID
, vlanid
))
1665 nla_end_nested(nlmsg
, nest2
);
1666 nla_end_nested(nlmsg
, nest
);
1668 if (nla_put_u32(nlmsg
, IFLA_LINK
, lindex
))
1671 if (nla_put_string(nlmsg
, IFLA_IFNAME
, name
))
1674 err
= netlink_transaction(&nlh
, nlmsg
, answer
);
1680 netlink_close(&nlh
);
1684 int lxc_macvlan_create(const char *master
, const char *name
, int mode
)
1686 int err
, index
, len
;
1687 struct ifinfomsg
*ifi
;
1688 struct nl_handler nlh
;
1689 struct rtattr
*nest
, *nest2
;
1690 struct nlmsg
*answer
= NULL
, *nlmsg
= NULL
;
1692 err
= netlink_open(&nlh
, NETLINK_ROUTE
);
1697 len
= strlen(master
);
1698 if (len
== 1 || len
>= IFNAMSIZ
)
1702 if (len
== 1 || len
>= IFNAMSIZ
)
1706 nlmsg
= nlmsg_alloc(NLMSG_GOOD_SIZE
);
1710 answer
= nlmsg_alloc_reserve(NLMSG_GOOD_SIZE
);
1715 index
= if_nametoindex(master
);
1719 nlmsg
->nlmsghdr
->nlmsg_flags
=
1720 NLM_F_REQUEST
| NLM_F_CREATE
| NLM_F_EXCL
| NLM_F_ACK
;
1721 nlmsg
->nlmsghdr
->nlmsg_type
= RTM_NEWLINK
;
1723 ifi
= nlmsg_reserve(nlmsg
, sizeof(struct ifinfomsg
));
1728 ifi
->ifi_family
= AF_UNSPEC
;
1730 nest
= nla_begin_nested(nlmsg
, IFLA_LINKINFO
);
1734 if (nla_put_string(nlmsg
, IFLA_INFO_KIND
, "macvlan"))
1738 nest2
= nla_begin_nested(nlmsg
, IFLA_INFO_DATA
);
1742 if (nla_put_u32(nlmsg
, IFLA_MACVLAN_MODE
, mode
))
1745 nla_end_nested(nlmsg
, nest2
);
1748 nla_end_nested(nlmsg
, nest
);
1750 if (nla_put_u32(nlmsg
, IFLA_LINK
, index
))
1753 if (nla_put_string(nlmsg
, IFLA_IFNAME
, name
))
1756 err
= netlink_transaction(&nlh
, nlmsg
, answer
);
1758 netlink_close(&nlh
);
1764 static int proc_sys_net_write(const char *path
, const char *value
)
1769 fd
= open(path
, O_WRONLY
);
1773 if (lxc_write_nointr(fd
, value
, strlen(value
)) < 0)
1780 static int lxc_is_ip_forwarding_enabled(const char *ifname
, int family
)
1783 char path
[PATH_MAX
];
1786 if (family
!= AF_INET
&& family
!= AF_INET6
)
1787 return minus_one_set_errno(EINVAL
);
1789 ret
= snprintf(path
, PATH_MAX
, "/proc/sys/net/%s/conf/%s/%s",
1790 family
== AF_INET
? "ipv4" : "ipv6", ifname
,
1792 if (ret
< 0 || (size_t)ret
>= PATH_MAX
)
1793 return minus_one_set_errno(E2BIG
);
1795 return lxc_read_file_expect(path
, buf
, 1, "1");
1798 static int neigh_proxy_set(const char *ifname
, int family
, int flag
)
1801 char path
[PATH_MAX
];
1803 if (family
!= AF_INET
&& family
!= AF_INET6
)
1806 ret
= snprintf(path
, PATH_MAX
, "/proc/sys/net/%s/conf/%s/%s",
1807 family
== AF_INET
? "ipv4" : "ipv6", ifname
,
1808 family
== AF_INET
? "proxy_arp" : "proxy_ndp");
1809 if (ret
< 0 || (size_t)ret
>= PATH_MAX
)
1812 return proc_sys_net_write(path
, flag
? "1" : "0");
1815 static int lxc_is_ip_neigh_proxy_enabled(const char *ifname
, int family
)
1818 char path
[PATH_MAX
];
1821 if (family
!= AF_INET
&& family
!= AF_INET6
)
1822 return minus_one_set_errno(EINVAL
);
1824 ret
= snprintf(path
, PATH_MAX
, "/proc/sys/net/%s/conf/%s/%s",
1825 family
== AF_INET
? "ipv4" : "ipv6", ifname
,
1826 family
== AF_INET
? "proxy_arp" : "proxy_ndp");
1827 if (ret
< 0 || (size_t)ret
>= PATH_MAX
)
1828 return minus_one_set_errno(E2BIG
);
1830 return lxc_read_file_expect(path
, buf
, 1, "1");
1833 int lxc_neigh_proxy_on(const char *name
, int family
)
1835 return neigh_proxy_set(name
, family
, 1);
1838 int lxc_neigh_proxy_off(const char *name
, int family
)
1840 return neigh_proxy_set(name
, family
, 0);
1843 int lxc_convert_mac(char *macaddr
, struct sockaddr
*sockaddr
)
1848 unsigned char *data
;
1850 sockaddr
->sa_family
= ARPHRD_ETHER
;
1851 data
= (unsigned char *)sockaddr
->sa_data
;
1853 while ((*macaddr
!= '\0') && (i
< ETH_ALEN
)) {
1857 else if (c
>= 'a' && c
<= 'f')
1859 else if (c
>= 'A' && c
<= 'F')
1868 else if (c
>= 'a' && c
<= 'f')
1869 val
|= c
- 'a' + 10;
1870 else if (c
>= 'A' && c
<= 'F')
1871 val
|= c
- 'A' + 10;
1872 else if (c
== ':' || c
== 0)
1878 *data
++ = (unsigned char)(val
& 0377);
1881 if (*macaddr
== ':')
1888 static int ip_addr_add(int family
, int ifindex
, void *addr
, void *bcast
,
1889 void *acast
, int prefix
)
1892 struct ifaddrmsg
*ifa
;
1893 struct nl_handler nlh
;
1894 struct nlmsg
*answer
= NULL
, *nlmsg
= NULL
;
1896 addrlen
= family
== AF_INET
? sizeof(struct in_addr
)
1897 : sizeof(struct in6_addr
);
1899 err
= netlink_open(&nlh
, NETLINK_ROUTE
);
1904 nlmsg
= nlmsg_alloc(NLMSG_GOOD_SIZE
);
1908 answer
= nlmsg_alloc_reserve(NLMSG_GOOD_SIZE
);
1912 nlmsg
->nlmsghdr
->nlmsg_flags
=
1913 NLM_F_ACK
| NLM_F_REQUEST
| NLM_F_CREATE
| NLM_F_EXCL
;
1914 nlmsg
->nlmsghdr
->nlmsg_type
= RTM_NEWADDR
;
1916 ifa
= nlmsg_reserve(nlmsg
, sizeof(struct ifaddrmsg
));
1919 ifa
->ifa_prefixlen
= prefix
;
1920 ifa
->ifa_index
= ifindex
;
1921 ifa
->ifa_family
= family
;
1925 if (nla_put_buffer(nlmsg
, IFA_LOCAL
, addr
, addrlen
))
1928 if (nla_put_buffer(nlmsg
, IFA_ADDRESS
, addr
, addrlen
))
1931 if (nla_put_buffer(nlmsg
, IFA_BROADCAST
, bcast
, addrlen
))
1934 /* TODO: multicast, anycast with ipv6 */
1935 err
= -EPROTONOSUPPORT
;
1936 if (family
== AF_INET6
&&
1937 (memcmp(bcast
, &in6addr_any
, sizeof(in6addr_any
)) ||
1938 memcmp(acast
, &in6addr_any
, sizeof(in6addr_any
))))
1941 err
= netlink_transaction(&nlh
, nlmsg
, answer
);
1943 netlink_close(&nlh
);
1949 int lxc_ipv6_addr_add(int ifindex
, struct in6_addr
*addr
,
1950 struct in6_addr
*mcast
, struct in6_addr
*acast
,
1953 return ip_addr_add(AF_INET6
, ifindex
, addr
, mcast
, acast
, prefix
);
1956 int lxc_ipv4_addr_add(int ifindex
, struct in_addr
*addr
, struct in_addr
*bcast
,
1959 return ip_addr_add(AF_INET
, ifindex
, addr
, bcast
, NULL
, prefix
);
1962 /* Find an IFA_LOCAL (or IFA_ADDRESS if not IFA_LOCAL is present) address from
1963 * the given RTM_NEWADDR message. Allocates memory for the address and stores
1964 * that pointer in *res (so res should be an in_addr** or in6_addr**).
1966 #pragma GCC diagnostic push
1967 #pragma GCC diagnostic ignored "-Wcast-align"
1969 static int ifa_get_local_ip(int family
, struct nlmsghdr
*msg
, void **res
)
1972 struct ifaddrmsg
*ifa
= NLMSG_DATA(msg
);
1973 struct rtattr
*rta
= IFA_RTA(ifa
);
1974 int attr_len
= NLMSG_PAYLOAD(msg
, sizeof(struct ifaddrmsg
));
1976 if (ifa
->ifa_family
!= family
)
1979 addrlen
= family
== AF_INET
? sizeof(struct in_addr
)
1980 : sizeof(struct in6_addr
);
1982 /* Loop over the rtattr's in this message */
1983 while (RTA_OK(rta
, attr_len
)) {
1984 /* Found a local address for the requested interface,
1987 if (rta
->rta_type
== IFA_LOCAL
||
1988 rta
->rta_type
== IFA_ADDRESS
) {
1989 /* Sanity check. The family check above should make sure
1990 * the address length is correct, but check here just in
1993 if (RTA_PAYLOAD(rta
) != addrlen
)
1996 /* We might have found an IFA_ADDRESS before, which we
1997 * now overwrite with an IFA_LOCAL.
2000 *res
= malloc(addrlen
);
2005 memcpy(*res
, RTA_DATA(rta
), addrlen
);
2006 if (rta
->rta_type
== IFA_LOCAL
)
2009 rta
= RTA_NEXT(rta
, attr_len
);
2014 #pragma GCC diagnostic pop
2016 static int ip_addr_get(int family
, int ifindex
, void **res
)
2018 int answer_len
, err
;
2019 struct ifaddrmsg
*ifa
;
2020 struct nl_handler nlh
;
2021 struct nlmsghdr
*msg
;
2022 int readmore
= 0, recv_len
= 0;
2023 struct nlmsg
*answer
= NULL
, *nlmsg
= NULL
;
2025 err
= netlink_open(&nlh
, NETLINK_ROUTE
);
2030 nlmsg
= nlmsg_alloc(NLMSG_GOOD_SIZE
);
2034 answer
= nlmsg_alloc_reserve(NLMSG_GOOD_SIZE
);
2038 /* Save the answer buffer length, since it will be overwritten on the
2039 * first receive (and we might need to receive more than once).
2041 answer_len
= answer
->nlmsghdr
->nlmsg_len
;
2043 nlmsg
->nlmsghdr
->nlmsg_flags
= NLM_F_REQUEST
| NLM_F_ROOT
;
2044 nlmsg
->nlmsghdr
->nlmsg_type
= RTM_GETADDR
;
2046 ifa
= nlmsg_reserve(nlmsg
, sizeof(struct ifaddrmsg
));
2049 ifa
->ifa_family
= family
;
2051 /* Send the request for addresses, which returns all addresses on all
2054 err
= netlink_send(&nlh
, nlmsg
);
2058 #pragma GCC diagnostic push
2059 #pragma GCC diagnostic ignored "-Wcast-align"
2062 /* Restore the answer buffer length, it might have been
2063 * overwritten by a previous receive.
2065 answer
->nlmsghdr
->nlmsg_len
= answer_len
;
2067 /* Get the (next) batch of reply messages. */
2068 err
= netlink_rcv(&nlh
, answer
);
2075 /* Satisfy the typing for the netlink macros. */
2076 msg
= answer
->nlmsghdr
;
2078 while (NLMSG_OK(msg
, recv_len
)) {
2079 /* Stop reading if we see an error message. */
2080 if (msg
->nlmsg_type
== NLMSG_ERROR
) {
2081 struct nlmsgerr
*errmsg
=
2082 (struct nlmsgerr
*)NLMSG_DATA(msg
);
2083 err
= errmsg
->error
;
2087 /* Stop reading if we see a NLMSG_DONE message. */
2088 if (msg
->nlmsg_type
== NLMSG_DONE
) {
2093 if (msg
->nlmsg_type
!= RTM_NEWADDR
) {
2098 ifa
= (struct ifaddrmsg
*)NLMSG_DATA(msg
);
2099 if (ifa
->ifa_index
== ifindex
) {
2100 if (ifa_get_local_ip(family
, msg
, res
) < 0) {
2105 /* Found a result, stop searching. */
2110 /* Keep reading more data from the socket if the last
2111 * message had the NLF_F_MULTI flag set.
2113 readmore
= (msg
->nlmsg_flags
& NLM_F_MULTI
);
2115 /* Look at the next message received in this buffer. */
2116 msg
= NLMSG_NEXT(msg
, recv_len
);
2120 #pragma GCC diagnostic pop
2122 /* If we end up here, we didn't find any result, so signal an
2128 netlink_close(&nlh
);
2134 int lxc_ipv6_addr_get(int ifindex
, struct in6_addr
**res
)
2136 return ip_addr_get(AF_INET6
, ifindex
, (void **)res
);
2139 int lxc_ipv4_addr_get(int ifindex
, struct in_addr
**res
)
2141 return ip_addr_get(AF_INET
, ifindex
, (void **)res
);
2144 static int ip_gateway_add(int family
, int ifindex
, void *gw
)
2147 struct nl_handler nlh
;
2149 struct nlmsg
*answer
= NULL
, *nlmsg
= NULL
;
2151 addrlen
= family
== AF_INET
? sizeof(struct in_addr
)
2152 : sizeof(struct in6_addr
);
2154 err
= netlink_open(&nlh
, NETLINK_ROUTE
);
2159 nlmsg
= nlmsg_alloc(NLMSG_GOOD_SIZE
);
2163 answer
= nlmsg_alloc_reserve(NLMSG_GOOD_SIZE
);
2167 nlmsg
->nlmsghdr
->nlmsg_flags
=
2168 NLM_F_ACK
| NLM_F_REQUEST
| NLM_F_CREATE
| NLM_F_EXCL
;
2169 nlmsg
->nlmsghdr
->nlmsg_type
= RTM_NEWROUTE
;
2171 rt
= nlmsg_reserve(nlmsg
, sizeof(struct rtmsg
));
2174 rt
->rtm_family
= family
;
2175 rt
->rtm_table
= RT_TABLE_MAIN
;
2176 rt
->rtm_scope
= RT_SCOPE_UNIVERSE
;
2177 rt
->rtm_protocol
= RTPROT_BOOT
;
2178 rt
->rtm_type
= RTN_UNICAST
;
2179 /* "default" destination */
2180 rt
->rtm_dst_len
= 0;
2184 /* If gateway address not supplied, then a device route will be created instead */
2186 if (nla_put_buffer(nlmsg
, RTA_GATEWAY
, gw
, addrlen
))
2190 /* Adding the interface index enables the use of link-local
2191 * addresses for the gateway.
2193 if (nla_put_u32(nlmsg
, RTA_OIF
, ifindex
))
2196 err
= netlink_transaction(&nlh
, nlmsg
, answer
);
2198 netlink_close(&nlh
);
2204 int lxc_ipv4_gateway_add(int ifindex
, struct in_addr
*gw
)
2206 return ip_gateway_add(AF_INET
, ifindex
, gw
);
2209 int lxc_ipv6_gateway_add(int ifindex
, struct in6_addr
*gw
)
2211 return ip_gateway_add(AF_INET6
, ifindex
, gw
);
2213 bool is_ovs_bridge(const char *bridge
)
2217 char brdirname
[22 + IFNAMSIZ
+ 1] = {0};
2219 ret
= snprintf(brdirname
, 22 + IFNAMSIZ
+ 1, "/sys/class/net/%s/bridge",
2221 if (ret
< 0 || (size_t)ret
>= 22 + IFNAMSIZ
+ 1)
2224 ret
= stat(brdirname
, &sb
);
2225 if (ret
< 0 && errno
== ENOENT
)
2231 struct ovs_veth_args
{
2236 /* Called from a background thread - when nic goes away, remove it from the
2239 static int lxc_ovs_delete_port_exec(void *data
)
2241 struct ovs_veth_args
*args
= data
;
2243 execlp("ovs-vsctl", "ovs-vsctl", "del-port", args
->bridge
, args
->nic
,
2248 int lxc_ovs_delete_port(const char *bridge
, const char *nic
)
2251 char cmd_output
[PATH_MAX
];
2252 struct ovs_veth_args args
;
2254 args
.bridge
= bridge
;
2256 ret
= run_command(cmd_output
, sizeof(cmd_output
),
2257 lxc_ovs_delete_port_exec
, (void *)&args
);
2259 ERROR("Failed to delete \"%s\" from openvswitch bridge \"%s\": "
2260 "%s", bridge
, nic
, cmd_output
);
2267 static int lxc_ovs_attach_bridge_exec(void *data
)
2269 struct ovs_veth_args
*args
= data
;
2271 execlp("ovs-vsctl", "ovs-vsctl", "add-port", args
->bridge
, args
->nic
,
2276 static int lxc_ovs_attach_bridge(const char *bridge
, const char *nic
)
2279 char cmd_output
[PATH_MAX
];
2280 struct ovs_veth_args args
;
2282 args
.bridge
= bridge
;
2284 ret
= run_command(cmd_output
, sizeof(cmd_output
),
2285 lxc_ovs_attach_bridge_exec
, (void *)&args
);
2287 ERROR("Failed to attach \"%s\" to openvswitch bridge \"%s\": %s",
2288 bridge
, nic
, cmd_output
);
2295 int lxc_bridge_attach(const char *bridge
, const char *ifname
)
2301 if (strlen(ifname
) >= IFNAMSIZ
)
2304 index
= if_nametoindex(ifname
);
2308 if (is_ovs_bridge(bridge
))
2309 return lxc_ovs_attach_bridge(bridge
, ifname
);
2311 fd
= socket(AF_INET
, SOCK_STREAM
| SOCK_CLOEXEC
, 0);
2315 retlen
= strlcpy(ifr
.ifr_name
, bridge
, IFNAMSIZ
);
2316 if (retlen
>= IFNAMSIZ
) {
2321 ifr
.ifr_name
[IFNAMSIZ
- 1] = '\0';
2322 ifr
.ifr_ifindex
= index
;
2323 err
= ioctl(fd
, SIOCBRADDIF
, &ifr
);
2331 static const char *const lxc_network_types
[LXC_NET_MAXCONFTYPE
+ 1] = {
2332 [LXC_NET_EMPTY
] = "empty",
2333 [LXC_NET_VETH
] = "veth",
2334 [LXC_NET_MACVLAN
] = "macvlan",
2335 [LXC_NET_IPVLAN
] = "ipvlan",
2336 [LXC_NET_PHYS
] = "phys",
2337 [LXC_NET_VLAN
] = "vlan",
2338 [LXC_NET_NONE
] = "none",
2341 const char *lxc_net_type_to_str(int type
)
2343 if (type
< 0 || type
> LXC_NET_MAXCONFTYPE
)
2346 return lxc_network_types
[type
];
2349 static const char padchar
[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
2351 char *lxc_mkifname(char *template)
2354 struct netns_ifaddrs
*ifa
, *ifaddr
;
2355 char name
[IFNAMSIZ
];
2356 bool exists
= false;
2361 seed
= randseed(false);
2364 (void)randseed(true);
2367 if (strlen(template) >= IFNAMSIZ
)
2370 /* Get all the network interfaces. */
2371 ret
= netns_getifaddrs(&ifaddr
, -1, &(bool){false});
2373 SYSERROR("Failed to get network interfaces");
2377 /* Generate random names until we find one that doesn't exist. */
2380 (void)strlcpy(name
, template, IFNAMSIZ
);
2384 for (i
= 0; i
< strlen(name
); i
++) {
2385 if (name
[i
] == 'X') {
2387 name
[i
] = padchar
[rand_r(&seed
) % strlen(padchar
)];
2389 name
[i
] = padchar
[rand() % strlen(padchar
)];
2394 for (ifa
= ifaddr
; ifa
!= NULL
; ifa
= ifa
->ifa_next
) {
2395 if (!strcmp(ifa
->ifa_name
, name
)) {
2405 netns_freeifaddrs(ifaddr
);
2406 (void)strlcpy(template, name
, strlen(template) + 1);
2411 int setup_private_host_hw_addr(char *veth1
)
2416 sockfd
= socket(AF_INET
, SOCK_DGRAM
| SOCK_CLOEXEC
, 0);
2420 err
= snprintf((char *)ifr
.ifr_name
, IFNAMSIZ
, "%s", veth1
);
2421 if (err
< 0 || (size_t)err
>= IFNAMSIZ
) {
2426 err
= ioctl(sockfd
, SIOCGIFHWADDR
, &ifr
);
2432 ifr
.ifr_hwaddr
.sa_data
[0] = 0xfe;
2433 err
= ioctl(sockfd
, SIOCSIFHWADDR
, &ifr
);
2441 int lxc_find_gateway_addresses(struct lxc_handler
*handler
)
2443 struct lxc_list
*network
= &handler
->conf
->network
;
2444 struct lxc_list
*iterator
;
2445 struct lxc_netdev
*netdev
;
2448 lxc_list_for_each(iterator
, network
) {
2449 netdev
= iterator
->elem
;
2451 if (!netdev
->ipv4_gateway_auto
&& !netdev
->ipv6_gateway_auto
)
2454 if (netdev
->type
!= LXC_NET_VETH
&& netdev
->type
!= LXC_NET_MACVLAN
) {
2455 ERROR("Automatic gateway detection is only supported "
2456 "for veth and macvlan");
2460 if (netdev
->link
[0] == '\0') {
2461 ERROR("Automatic gateway detection needs a link interface");
2465 link_index
= if_nametoindex(netdev
->link
);
2469 if (netdev
->ipv4_gateway_auto
) {
2470 if (lxc_ipv4_addr_get(link_index
, &netdev
->ipv4_gateway
)) {
2471 ERROR("Failed to automatically find ipv4 gateway "
2472 "address from link interface \"%s\"", netdev
->link
);
2477 if (netdev
->ipv6_gateway_auto
) {
2478 if (lxc_ipv6_addr_get(link_index
, &netdev
->ipv6_gateway
)) {
2479 ERROR("Failed to automatically find ipv6 gateway "
2480 "address from link interface \"%s\"", netdev
->link
);
2489 #define LXC_USERNIC_PATH LIBEXECDIR "/lxc/lxc-user-nic"
2490 static int lxc_create_network_unpriv_exec(const char *lxcpath
, const char *lxcname
,
2491 struct lxc_netdev
*netdev
, pid_t pid
, unsigned int hooks_version
)
2495 int bytes
, pipefd
[2];
2496 char *token
, *saveptr
= NULL
;
2497 char netdev_link
[IFNAMSIZ
];
2498 char buffer
[PATH_MAX
] = {0};
2501 if (netdev
->type
!= LXC_NET_VETH
) {
2502 ERROR("Network type %d not support for unprivileged use", netdev
->type
);
2508 SYSERROR("Failed to create pipe");
2514 SYSERROR("Failed to create new process");
2521 char pidstr
[INTTYPE_TO_STRLEN(pid_t
)];
2525 ret
= dup2(pipefd
[1], STDOUT_FILENO
);
2527 ret
= dup2(pipefd
[1], STDERR_FILENO
);
2530 SYSERROR("Failed to duplicate std{err,out} file descriptor");
2531 _exit(EXIT_FAILURE
);
2534 if (netdev
->link
[0] != '\0')
2535 retlen
= strlcpy(netdev_link
, netdev
->link
, IFNAMSIZ
);
2537 retlen
= strlcpy(netdev_link
, "none", IFNAMSIZ
);
2538 if (retlen
>= IFNAMSIZ
) {
2539 SYSERROR("Invalid network device name");
2540 _exit(EXIT_FAILURE
);
2543 ret
= snprintf(pidstr
, sizeof(pidstr
), "%d", pid
);
2544 if (ret
< 0 || ret
>= sizeof(pidstr
))
2545 _exit(EXIT_FAILURE
);
2546 pidstr
[sizeof(pidstr
) - 1] = '\0';
2548 INFO("Execing lxc-user-nic create %s %s %s veth %s %s", lxcpath
,
2549 lxcname
, pidstr
, netdev_link
,
2550 netdev
->name
[0] != '\0' ? netdev
->name
: "(null)");
2551 if (netdev
->name
[0] != '\0')
2552 execlp(LXC_USERNIC_PATH
, LXC_USERNIC_PATH
, "create",
2553 lxcpath
, lxcname
, pidstr
, "veth", netdev_link
,
2554 netdev
->name
, (char *)NULL
);
2556 execlp(LXC_USERNIC_PATH
, LXC_USERNIC_PATH
, "create",
2557 lxcpath
, lxcname
, pidstr
, "veth", netdev_link
,
2559 SYSERROR("Failed to execute lxc-user-nic");
2560 _exit(EXIT_FAILURE
);
2563 /* close the write-end of the pipe */
2566 bytes
= lxc_read_nointr(pipefd
[0], &buffer
, PATH_MAX
);
2568 SYSERROR("Failed to read from pipe file descriptor");
2571 buffer
[bytes
- 1] = '\0';
2574 ret
= wait_for_pid(child
);
2576 if (ret
!= 0 || bytes
< 0) {
2577 ERROR("lxc-user-nic failed to configure requested network: %s",
2578 buffer
[0] != '\0' ? buffer
: "(null)");
2581 TRACE("Received output \"%s\" from lxc-user-nic", buffer
);
2584 token
= strtok_r(buffer
, ":", &saveptr
);
2586 ERROR("Failed to parse lxc-user-nic output");
2591 * lxc-user-nic will take care of proper network device naming. So
2592 * netdev->name and netdev->created_name need to be identical to not
2593 * trigger another rename later on.
2595 retlen
= strlcpy(netdev
->name
, token
, IFNAMSIZ
);
2596 if (retlen
< IFNAMSIZ
)
2597 retlen
= strlcpy(netdev
->created_name
, token
, IFNAMSIZ
);
2598 if (retlen
>= IFNAMSIZ
) {
2599 ERROR("Container side veth device name returned by lxc-user-nic is too long");
2603 /* netdev->ifindex */
2604 token
= strtok_r(NULL
, ":", &saveptr
);
2606 ERROR("Failed to parse lxc-user-nic output");
2610 ret
= lxc_safe_int(token
, &netdev
->ifindex
);
2613 SYSERROR("Failed to convert string \"%s\" to integer", token
);
2617 /* netdev->priv.veth_attr.veth1 */
2618 token
= strtok_r(NULL
, ":", &saveptr
);
2620 ERROR("Failed to parse lxc-user-nic output");
2624 retlen
= strlcpy(netdev
->priv
.veth_attr
.veth1
, token
, IFNAMSIZ
);
2625 if (retlen
>= IFNAMSIZ
) {
2626 ERROR("Host side veth device name returned by lxc-user-nic is "
2631 /* netdev->priv.veth_attr.ifindex */
2632 token
= strtok_r(NULL
, ":", &saveptr
);
2634 ERROR("Failed to parse lxc-user-nic output");
2638 ret
= lxc_safe_int(token
, &netdev
->priv
.veth_attr
.ifindex
);
2641 SYSERROR("Failed to convert string \"%s\" to integer", token
);
2645 if (netdev
->upscript
) {
2649 netdev
->priv
.veth_attr
.veth1
,
2653 ret
= run_script_argv(lxcname
, hooks_version
, "net",
2654 netdev
->upscript
, "up", argv
);
2662 static int lxc_delete_network_unpriv_exec(const char *lxcpath
, const char *lxcname
,
2663 struct lxc_netdev
*netdev
,
2664 const char *netns_path
)
2669 char buffer
[PATH_MAX
] = {0};
2671 if (netdev
->type
!= LXC_NET_VETH
) {
2672 ERROR("Network type %d not support for unprivileged use", netdev
->type
);
2678 SYSERROR("Failed to create pipe");
2684 SYSERROR("Failed to create new process");
2695 ret
= dup2(pipefd
[1], STDOUT_FILENO
);
2697 ret
= dup2(pipefd
[1], STDERR_FILENO
);
2700 SYSERROR("Failed to duplicate std{err,out} file descriptor");
2701 _exit(EXIT_FAILURE
);
2704 if (netdev
->priv
.veth_attr
.pair
[0] != '\0')
2705 hostveth
= netdev
->priv
.veth_attr
.pair
;
2707 hostveth
= netdev
->priv
.veth_attr
.veth1
;
2708 if (hostveth
[0] == '\0') {
2709 SYSERROR("Host side veth device name is missing");
2710 _exit(EXIT_FAILURE
);
2713 if (netdev
->link
[0] == '\0') {
2714 SYSERROR("Network link for network device \"%s\" is "
2715 "missing", netdev
->priv
.veth_attr
.veth1
);
2716 _exit(EXIT_FAILURE
);
2719 INFO("Execing lxc-user-nic delete %s %s %s veth %s %s", lxcpath
,
2720 lxcname
, netns_path
, netdev
->link
, hostveth
);
2721 execlp(LXC_USERNIC_PATH
, LXC_USERNIC_PATH
, "delete", lxcpath
,
2722 lxcname
, netns_path
, "veth", netdev
->link
, hostveth
,
2724 SYSERROR("Failed to exec lxc-user-nic.");
2725 _exit(EXIT_FAILURE
);
2730 bytes
= lxc_read_nointr(pipefd
[0], &buffer
, PATH_MAX
);
2732 SYSERROR("Failed to read from pipe file descriptor.");
2735 buffer
[bytes
- 1] = '\0';
2738 ret
= wait_for_pid(child
);
2740 if (ret
!= 0 || bytes
< 0) {
2741 ERROR("lxc-user-nic failed to delete requested network: %s",
2742 buffer
[0] != '\0' ? buffer
: "(null)");
2749 bool lxc_delete_network_unpriv(struct lxc_handler
*handler
)
2752 struct lxc_list
*iterator
;
2753 struct lxc_list
*network
= &handler
->conf
->network
;
2754 /* strlen("/proc/") = 6
2756 * INTTYPE_TO_STRLEN(pid_t)
2758 * strlen("/fd/") = 4
2760 * INTTYPE_TO_STRLEN(int)
2764 char netns_path
[6 + INTTYPE_TO_STRLEN(pid_t
) + 4 + INTTYPE_TO_STRLEN(int) + 1];
2768 if (handler
->nsfd
[LXC_NS_NET
] < 0) {
2769 DEBUG("Cannot not guarantee safe deletion of network devices. "
2770 "Manual cleanup maybe needed");
2774 ret
= snprintf(netns_path
, sizeof(netns_path
), "/proc/%d/fd/%d",
2775 lxc_raw_getpid(), handler
->nsfd
[LXC_NS_NET
]);
2776 if (ret
< 0 || ret
>= sizeof(netns_path
))
2779 lxc_list_for_each(iterator
, network
) {
2780 char *hostveth
= NULL
;
2781 struct lxc_netdev
*netdev
= iterator
->elem
;
2783 /* We can only delete devices whose ifindex we have. If we don't
2784 * have the index it means that we didn't create it.
2786 if (!netdev
->ifindex
)
2789 if (netdev
->type
== LXC_NET_PHYS
) {
2790 ret
= lxc_netdev_rename_by_index(netdev
->ifindex
,
2793 WARN("Failed to rename interface with index %d "
2794 "to its initial name \"%s\"",
2795 netdev
->ifindex
, netdev
->link
);
2797 TRACE("Renamed interface with index %d to its "
2798 "initial name \"%s\"",
2799 netdev
->ifindex
, netdev
->link
);
2801 ret
= netdev_deconf
[netdev
->type
](handler
, netdev
);
2802 goto clear_ifindices
;
2805 ret
= netdev_deconf
[netdev
->type
](handler
, netdev
);
2807 WARN("Failed to deconfigure network device");
2809 if (netdev
->type
!= LXC_NET_VETH
)
2810 goto clear_ifindices
;
2812 if (netdev
->link
[0] == '\0' || !is_ovs_bridge(netdev
->link
))
2813 goto clear_ifindices
;
2815 if (netdev
->priv
.veth_attr
.pair
[0] != '\0')
2816 hostveth
= netdev
->priv
.veth_attr
.pair
;
2818 hostveth
= netdev
->priv
.veth_attr
.veth1
;
2819 if (hostveth
[0] == '\0')
2820 goto clear_ifindices
;
2822 ret
= lxc_delete_network_unpriv_exec(handler
->lxcpath
,
2823 handler
->name
, netdev
,
2826 WARN("Failed to remove port \"%s\" from openvswitch "
2827 "bridge \"%s\"", hostveth
, netdev
->link
);
2828 goto clear_ifindices
;
2830 INFO("Removed interface \"%s\" from \"%s\"", hostveth
,
2834 /* We need to clear any ifindices we recorded so liblxc won't
2835 * have cached stale data which would cause it to fail on reboot
2836 * we're we don't re-read the on-disk config file.
2838 netdev
->ifindex
= 0;
2839 if (netdev
->type
== LXC_NET_PHYS
) {
2840 netdev
->priv
.phys_attr
.ifindex
= 0;
2841 } else if (netdev
->type
== LXC_NET_VETH
) {
2842 netdev
->priv
.veth_attr
.veth1
[0] = '\0';
2843 netdev
->priv
.veth_attr
.ifindex
= 0;
2850 struct ip_proxy_args
{
2855 static int lxc_add_ip_neigh_proxy_exec_wrapper(void *data
)
2857 struct ip_proxy_args
*args
= data
;
2859 execlp("ip", "ip", "neigh", "add", "proxy", args
->ip
, "dev", args
->dev
, (char *)NULL
);
2863 static int lxc_del_ip_neigh_proxy_exec_wrapper(void *data
)
2865 struct ip_proxy_args
*args
= data
;
2867 execlp("ip", "ip", "neigh", "flush", "proxy", args
->ip
, "dev", args
->dev
, (char *)NULL
);
2871 static int lxc_add_ip_neigh_proxy(const char *ip
, const char *dev
)
2874 char cmd_output
[PATH_MAX
];
2875 struct ip_proxy_args args
= {
2880 ret
= run_command(cmd_output
, sizeof(cmd_output
), lxc_add_ip_neigh_proxy_exec_wrapper
, &args
);
2882 ERROR("Failed to add ip proxy \"%s\" to dev \"%s\": %s", ip
, dev
, cmd_output
);
2889 static int lxc_del_ip_neigh_proxy(const char *ip
, const char *dev
)
2892 char cmd_output
[PATH_MAX
];
2893 struct ip_proxy_args args
= {
2898 ret
= run_command(cmd_output
, sizeof(cmd_output
), lxc_del_ip_neigh_proxy_exec_wrapper
, &args
);
2900 ERROR("Failed to delete ip proxy \"%s\" to dev \"%s\": %s", ip
, dev
, cmd_output
);
2907 static int lxc_setup_l2proxy(struct lxc_netdev
*netdev
) {
2908 struct lxc_list
*cur
, *next
;
2909 struct lxc_inetdev
*inet4dev
;
2910 struct lxc_inet6dev
*inet6dev
;
2911 char bufinet4
[INET_ADDRSTRLEN
], bufinet6
[INET6_ADDRSTRLEN
];
2913 unsigned int lo_ifindex
= 0;
2915 /* If IPv4 addresses are specified, then check that sysctl is configured correctly. */
2916 if (!lxc_list_empty(&netdev
->ipv4
)) {
2917 /* Check for net.ipv4.conf.[link].forwarding=1 */
2918 if (lxc_is_ip_forwarding_enabled(netdev
->link
, AF_INET
) < 0) {
2919 ERROR("Requires sysctl net.ipv4.conf.%s.forwarding=1", netdev
->link
);
2920 return minus_one_set_errno(EINVAL
);
2924 /* If IPv6 addresses are specified, then check that sysctl is configured correctly. */
2925 if (!lxc_list_empty(&netdev
->ipv6
)) {
2926 /* Check for net.ipv6.conf.[link].proxy_ndp=1 */
2927 if (lxc_is_ip_neigh_proxy_enabled(netdev
->link
, AF_INET6
) < 0) {
2928 ERROR("Requires sysctl net.ipv6.conf.%s.proxy_ndp=1", netdev
->link
);
2929 return minus_one_set_errno(EINVAL
);
2932 /* Check for net.ipv6.conf.[link].forwarding=1 */
2933 if (lxc_is_ip_forwarding_enabled(netdev
->link
, AF_INET6
) < 0) {
2934 ERROR("Requires sysctl net.ipv6.conf.%s.forwarding=1", netdev
->link
);
2935 return minus_one_set_errno(EINVAL
);
2939 /* Perform IPVLAN specific checks. */
2940 if (netdev
->type
== LXC_NET_IPVLAN
) {
2941 /* Check mode is l3s as other modes do not work with l2proxy. */
2942 if (netdev
->priv
.ipvlan_attr
.mode
!= IPVLAN_MODE_L3S
) {
2943 ERROR("Requires ipvlan mode on dev \"%s\" be l3s when used with l2proxy", netdev
->link
);
2944 return minus_one_set_errno(EINVAL
);
2947 /* Retrieve local-loopback interface index for use with IPVLAN static routes. */
2948 lo_ifindex
= if_nametoindex(loop_device
);
2949 if (lo_ifindex
== 0) {
2950 ERROR("Failed to retrieve ifindex for \"%s\" routing cleanup", loop_device
);
2951 return minus_one_set_errno(EINVAL
);
2955 lxc_list_for_each_safe(cur
, &netdev
->ipv4
, next
) {
2956 inet4dev
= cur
->elem
;
2957 if (!inet_ntop(AF_INET
, &inet4dev
->addr
, bufinet4
, sizeof(bufinet4
)))
2958 return minus_one_set_errno(-errno
);
2960 if (lxc_add_ip_neigh_proxy(bufinet4
, netdev
->link
) < 0)
2961 return minus_one_set_errno(EINVAL
);
2963 /* IPVLAN requires a route to local-loopback to trigger l2proxy. */
2964 if (netdev
->type
== LXC_NET_IPVLAN
) {
2965 err
= lxc_ipv4_dest_add(lo_ifindex
, &inet4dev
->addr
, 32);
2967 ERROR("Failed to add ipv4 dest \"%s\" for network device \"%s\"", bufinet4
, loop_device
);
2968 return minus_one_set_errno(-err
);
2973 lxc_list_for_each_safe(cur
, &netdev
->ipv6
, next
) {
2974 inet6dev
= cur
->elem
;
2975 if (!inet_ntop(AF_INET6
, &inet6dev
->addr
, bufinet6
, sizeof(bufinet6
)))
2976 return minus_one_set_errno(-errno
);
2978 if (lxc_add_ip_neigh_proxy(bufinet6
, netdev
->link
) < 0)
2979 return minus_one_set_errno(EINVAL
);
2981 /* IPVLAN requires a route to local-loopback to trigger l2proxy. */
2982 if (netdev
->type
== LXC_NET_IPVLAN
) {
2983 err
= lxc_ipv6_dest_add(lo_ifindex
, &inet6dev
->addr
, 128);
2985 ERROR("Failed to add ipv6 dest \"%s\" for network device \"%s\"", bufinet6
, loop_device
);
2986 return minus_one_set_errno(-err
);
2994 static int lxc_delete_ipv4_l2proxy(struct in_addr
*ip
, char *link
, unsigned int lo_ifindex
) {
2995 char bufinet4
[INET_ADDRSTRLEN
];
2996 unsigned int errCount
= 0;
2998 if (!inet_ntop(AF_INET
, ip
, bufinet4
, sizeof(bufinet4
))) {
2999 SYSERROR("Failed to convert IP for l2proxy ipv4 removal on dev \"%s\"", link
);
3000 return minus_one_set_errno(EINVAL
);
3003 /* If a local-loopback ifindex supplied remove the static route to the lo device. */
3004 if (lo_ifindex
> 0) {
3005 if (lxc_ipv4_dest_del(lo_ifindex
, ip
, 32) < 0) {
3007 ERROR("Failed to delete ipv4 dest \"%s\" for network ifindex \"%u\"", bufinet4
, lo_ifindex
);
3011 /* If link is supplied remove the IP neigh proxy entry for this IP on the device. */
3012 if (link
[0] != '\0') {
3013 if (lxc_del_ip_neigh_proxy(bufinet4
, link
) < 0)
3018 return minus_one_set_errno(EINVAL
);
3023 static int lxc_delete_ipv6_l2proxy(struct in6_addr
*ip
, char *link
, unsigned int lo_ifindex
) {
3024 char bufinet6
[INET6_ADDRSTRLEN
];
3025 unsigned int errCount
= 0;
3027 if (!inet_ntop(AF_INET6
, ip
, bufinet6
, sizeof(bufinet6
))) {
3028 SYSERROR("Failed to convert IP for l2proxy ipv6 removal on dev \"%s\"", link
);
3029 return minus_one_set_errno(EINVAL
);
3032 /* If a local-loopback ifindex supplied remove the static route to the lo device. */
3033 if (lo_ifindex
> 0) {
3034 if (lxc_ipv6_dest_del(lo_ifindex
, ip
, 128) < 0) {
3036 ERROR("Failed to delete ipv6 dest \"%s\" for network ifindex \"%u\"", bufinet6
, lo_ifindex
);
3040 /* If link is supplied remove the IP neigh proxy entry for this IP on the device. */
3041 if (link
[0] != '\0') {
3042 if (lxc_del_ip_neigh_proxy(bufinet6
, link
) < 0)
3047 return minus_one_set_errno(EINVAL
);
3052 static int lxc_delete_l2proxy(struct lxc_netdev
*netdev
) {
3053 unsigned int lo_ifindex
= 0;
3054 unsigned int errCount
= 0;
3055 struct lxc_list
*cur
, *next
;
3056 struct lxc_inetdev
*inet4dev
;
3057 struct lxc_inet6dev
*inet6dev
;
3059 /* Perform IPVLAN specific checks. */
3060 if (netdev
->type
== LXC_NET_IPVLAN
) {
3061 /* Retrieve local-loopback interface index for use with IPVLAN static routes. */
3062 lo_ifindex
= if_nametoindex(loop_device
);
3063 if (lo_ifindex
== 0) {
3065 ERROR("Failed to retrieve ifindex for \"%s\" routing cleanup", loop_device
);
3069 lxc_list_for_each_safe(cur
, &netdev
->ipv4
, next
) {
3070 inet4dev
= cur
->elem
;
3071 if (lxc_delete_ipv4_l2proxy(&inet4dev
->addr
, netdev
->link
, lo_ifindex
) < 0)
3075 lxc_list_for_each_safe(cur
, &netdev
->ipv6
, next
) {
3076 inet6dev
= cur
->elem
;
3077 if (lxc_delete_ipv6_l2proxy(&inet6dev
->addr
, netdev
->link
, lo_ifindex
) < 0)
3082 return minus_one_set_errno(EINVAL
);
3087 static int lxc_create_network_priv(struct lxc_handler
*handler
)
3089 struct lxc_list
*iterator
;
3090 struct lxc_list
*network
= &handler
->conf
->network
;
3092 lxc_list_for_each(iterator
, network
) {
3093 struct lxc_netdev
*netdev
= iterator
->elem
;
3095 if (netdev
->type
< 0 || netdev
->type
> LXC_NET_MAXCONFTYPE
) {
3096 ERROR("Invalid network configuration type %d", netdev
->type
);
3100 /* Setup l2proxy entries if enabled and used with a link property */
3101 if (netdev
->l2proxy
&& netdev
->link
[0] != '\0') {
3102 if (lxc_setup_l2proxy(netdev
)) {
3103 ERROR("Failed to setup l2proxy");
3108 if (netdev_conf
[netdev
->type
](handler
, netdev
)) {
3109 ERROR("Failed to create network device");
3117 int lxc_network_move_created_netdev_priv(struct lxc_handler
*handler
)
3119 pid_t pid
= handler
->pid
;
3120 struct lxc_list
*network
= &handler
->conf
->network
;
3121 struct lxc_list
*iterator
;
3123 if (am_guest_unpriv())
3126 lxc_list_for_each(iterator
, network
) {
3128 char ifname
[IFNAMSIZ
];
3129 struct lxc_netdev
*netdev
= iterator
->elem
;
3131 if (!netdev
->ifindex
)
3134 /* retrieve the name of the interface */
3135 if (!if_indextoname(netdev
->ifindex
, ifname
)) {
3136 ERROR("No interface corresponding to ifindex \"%d\"",
3141 ret
= lxc_netdev_move_by_name(ifname
, pid
, NULL
);
3144 SYSERROR("Failed to move network device \"%s\" to network namespace %d",
3149 strlcpy(netdev
->created_name
, ifname
, IFNAMSIZ
);
3151 DEBUG("Moved network device \"%s\" to network namespace of %d",
3152 netdev
->created_name
, pid
);
3158 static int lxc_create_network_unpriv(struct lxc_handler
*handler
)
3160 int hooks_version
= handler
->conf
->hooks_version
;
3161 const char *lxcname
= handler
->name
;
3162 const char *lxcpath
= handler
->lxcpath
;
3163 struct lxc_list
*network
= &handler
->conf
->network
;
3164 pid_t pid
= handler
->pid
;
3165 struct lxc_list
*iterator
;
3167 lxc_list_for_each(iterator
, network
) {
3168 struct lxc_netdev
*netdev
= iterator
->elem
;
3170 if (netdev
->type
== LXC_NET_EMPTY
)
3173 if (netdev
->type
== LXC_NET_NONE
)
3176 if (netdev
->type
!= LXC_NET_VETH
) {
3177 ERROR("Networks of type %s are not supported by unprivileged containers",
3178 lxc_net_type_to_str(netdev
->type
));
3183 INFO("mtu ignored due to insufficient privilege");
3185 if (lxc_create_network_unpriv_exec(lxcpath
, lxcname
, netdev
,
3186 pid
, hooks_version
))
3193 bool lxc_delete_network_priv(struct lxc_handler
*handler
)
3196 struct lxc_list
*iterator
;
3197 struct lxc_list
*network
= &handler
->conf
->network
;
3199 lxc_list_for_each(iterator
, network
) {
3200 char *hostveth
= NULL
;
3201 struct lxc_netdev
*netdev
= iterator
->elem
;
3203 /* We can only delete devices whose ifindex we have. If we don't
3204 * have the index it means that we didn't create it.
3206 if (!netdev
->ifindex
)
3209 /* Delete l2proxy entries if enabled and used with a link property */
3210 if (netdev
->l2proxy
&& netdev
->link
[0] != '\0') {
3211 if (lxc_delete_l2proxy(netdev
))
3212 WARN("Failed to delete all l2proxy config");
3213 /* Don't return, let the network be cleaned up as normal. */
3216 if (netdev
->type
== LXC_NET_PHYS
) {
3217 ret
= lxc_netdev_rename_by_index(netdev
->ifindex
, netdev
->link
);
3219 WARN("Failed to rename interface with index %d "
3220 "from \"%s\" to its initial name \"%s\"",
3221 netdev
->ifindex
, netdev
->name
, netdev
->link
);
3223 TRACE("Renamed interface with index %d from "
3224 "\"%s\" to its initial name \"%s\"",
3225 netdev
->ifindex
, netdev
->name
,
3228 /* Restore original MTU */
3229 ret
= lxc_netdev_set_mtu(netdev
->link
, netdev
->priv
.phys_attr
.mtu
);
3231 WARN("Failed to set interface \"%s\" to its initial mtu \"%d\"",
3232 netdev
->link
, netdev
->priv
.phys_attr
.mtu
);
3234 TRACE("Restored interface \"%s\" to its initial mtu \"%d\"",
3235 netdev
->link
, netdev
->priv
.phys_attr
.mtu
);
3239 ret
= netdev_deconf
[netdev
->type
](handler
, netdev
);
3240 goto clear_ifindices
;
3243 ret
= netdev_deconf
[netdev
->type
](handler
, netdev
);
3245 WARN("Failed to deconfigure network device");
3247 /* Recent kernels remove the virtual interfaces when the network
3248 * namespace is destroyed but in case we did not move the
3249 * interface to the network namespace, we have to destroy it.
3251 ret
= lxc_netdev_delete_by_index(netdev
->ifindex
);
3253 if (errno
!= ENODEV
) {
3254 WARN("Failed to remove interface \"%s\" with index %d",
3255 netdev
->name
[0] != '\0' ? netdev
->name
: "(null)",
3257 goto clear_ifindices
;
3259 INFO("Interface \"%s\" with index %d already deleted or existing in different network namespace",
3260 netdev
->name
[0] != '\0' ? netdev
->name
: "(null)",
3263 INFO("Removed interface \"%s\" with index %d",
3264 netdev
->name
[0] != '\0' ? netdev
->name
: "(null)",
3267 if (netdev
->type
!= LXC_NET_VETH
)
3268 goto clear_ifindices
;
3270 /* Explicitly delete host veth device to prevent lingering
3271 * devices. We had issues in LXD around this.
3273 if (netdev
->priv
.veth_attr
.pair
[0] != '\0')
3274 hostveth
= netdev
->priv
.veth_attr
.pair
;
3276 hostveth
= netdev
->priv
.veth_attr
.veth1
;
3277 if (hostveth
[0] == '\0')
3278 goto clear_ifindices
;
3280 ret
= lxc_netdev_delete_by_name(hostveth
);
3282 WARN("Failed to remove interface \"%s\" from \"%s\"",
3283 hostveth
, netdev
->link
);
3284 goto clear_ifindices
;
3286 INFO("Removed interface \"%s\" from \"%s\"", hostveth
, netdev
->link
);
3288 if (netdev
->link
[0] == '\0' || !is_ovs_bridge(netdev
->link
)) {
3289 netdev
->priv
.veth_attr
.veth1
[0] = '\0';
3290 netdev
->ifindex
= 0;
3291 netdev
->priv
.veth_attr
.ifindex
= 0;
3292 goto clear_ifindices
;
3295 /* Delete the openvswitch port. */
3296 ret
= lxc_ovs_delete_port(netdev
->link
, hostveth
);
3298 WARN("Failed to remove port \"%s\" from openvswitch "
3299 "bridge \"%s\"", hostveth
, netdev
->link
);
3301 INFO("Removed port \"%s\" from openvswitch bridge \"%s\"",
3302 hostveth
, netdev
->link
);
3305 /* We need to clear any ifindices we recorded so liblxc won't
3306 * have cached stale data which would cause it to fail on reboot
3307 * we're we don't re-read the on-disk config file.
3309 netdev
->ifindex
= 0;
3310 if (netdev
->type
== LXC_NET_PHYS
) {
3311 netdev
->priv
.phys_attr
.ifindex
= 0;
3312 } else if (netdev
->type
== LXC_NET_VETH
) {
3313 netdev
->priv
.veth_attr
.veth1
[0] = '\0';
3314 netdev
->priv
.veth_attr
.ifindex
= 0;
3321 int lxc_requests_empty_network(struct lxc_handler
*handler
)
3323 struct lxc_list
*network
= &handler
->conf
->network
;
3324 struct lxc_list
*iterator
;
3325 bool found_none
= false, found_nic
= false;
3327 if (lxc_list_empty(network
))
3330 lxc_list_for_each(iterator
, network
) {
3331 struct lxc_netdev
*netdev
= iterator
->elem
;
3333 if (netdev
->type
== LXC_NET_NONE
)
3338 if (found_none
&& !found_nic
)
3343 /* try to move physical nics to the init netns */
3344 int lxc_restore_phys_nics_to_netns(struct lxc_handler
*handler
)
3348 char ifname
[IFNAMSIZ
];
3349 struct lxc_list
*iterator
;
3350 int netnsfd
= handler
->nsfd
[LXC_NS_NET
];
3351 struct lxc_conf
*conf
= handler
->conf
;
3353 /* We need CAP_NET_ADMIN in the parent namespace in order to setns() to
3354 * the parent network namespace. We won't have this capability if we are
3357 if (!handler
->am_root
)
3360 TRACE("Moving physical network devices back to parent network namespace");
3362 oldfd
= lxc_preserve_ns(handler
->monitor_pid
, "net");
3364 SYSERROR("Failed to preserve network namespace");
3368 ret
= setns(netnsfd
, CLONE_NEWNET
);
3370 SYSERROR("Failed to enter network namespace");
3375 lxc_list_for_each(iterator
, &conf
->network
) {
3376 struct lxc_netdev
*netdev
= iterator
->elem
;
3378 if (netdev
->type
!= LXC_NET_PHYS
)
3381 /* Retrieve the name of the interface in the container's network
3384 if (!if_indextoname(netdev
->ifindex
, ifname
)) {
3385 WARN("No interface corresponding to ifindex %d",
3390 ret
= lxc_netdev_move_by_index_fd(netdev
->ifindex
, oldfd
, netdev
->link
);
3392 WARN("Error moving network device \"%s\" back to "
3393 "network namespace", ifname
);
3395 TRACE("Moved network device \"%s\" back to network "
3396 "namespace", ifname
);
3399 ret
= setns(oldfd
, CLONE_NEWNET
);
3402 SYSERROR("Failed to enter network namespace");
3409 static int setup_hw_addr(char *hwaddr
, const char *ifname
)
3411 struct sockaddr sockaddr
;
3415 ret
= lxc_convert_mac(hwaddr
, &sockaddr
);
3418 SYSERROR("Mac address \"%s\" conversion failed", hwaddr
);
3422 memcpy(ifr
.ifr_name
, ifname
, IFNAMSIZ
);
3423 ifr
.ifr_name
[IFNAMSIZ
-1] = '\0';
3424 memcpy((char *) &ifr
.ifr_hwaddr
, (char *) &sockaddr
, sizeof(sockaddr
));
3426 fd
= socket(AF_INET
, SOCK_DGRAM
| SOCK_CLOEXEC
, 0);
3430 ret
= ioctl(fd
, SIOCSIFHWADDR
, &ifr
);
3432 SYSERROR("Failed to perform ioctl");
3436 DEBUG("Mac address \"%s\" on \"%s\" has been setup", hwaddr
,
3442 static int setup_ipv4_addr(struct lxc_list
*ip
, int ifindex
)
3444 struct lxc_list
*iterator
;
3447 lxc_list_for_each(iterator
, ip
) {
3448 struct lxc_inetdev
*inetdev
= iterator
->elem
;
3450 err
= lxc_ipv4_addr_add(ifindex
, &inetdev
->addr
,
3451 &inetdev
->bcast
, inetdev
->prefix
);
3454 SYSERROR("Failed to setup ipv4 address for network device "
3455 "with ifindex %d", ifindex
);
3463 static int setup_ipv6_addr(struct lxc_list
*ip
, int ifindex
)
3465 struct lxc_list
*iterator
;
3468 lxc_list_for_each(iterator
, ip
) {
3469 struct lxc_inet6dev
*inet6dev
= iterator
->elem
;
3471 err
= lxc_ipv6_addr_add(ifindex
, &inet6dev
->addr
,
3472 &inet6dev
->mcast
, &inet6dev
->acast
,
3476 SYSERROR("Failed to setup ipv6 address for network device "
3477 "with ifindex %d", ifindex
);
3485 static int lxc_setup_netdev_in_child_namespaces(struct lxc_netdev
*netdev
)
3487 char ifname
[IFNAMSIZ
];
3489 char *current_ifname
= ifname
;
3490 char bufinet4
[INET_ADDRSTRLEN
], bufinet6
[INET6_ADDRSTRLEN
];
3492 /* empty network namespace */
3493 if (!netdev
->ifindex
) {
3494 if (netdev
->flags
& IFF_UP
) {
3495 err
= lxc_netdev_up("lo");
3498 SYSERROR("Failed to set the loopback network device up");
3503 if (netdev
->type
== LXC_NET_EMPTY
)
3506 if (netdev
->type
== LXC_NET_NONE
)
3509 netdev
->ifindex
= if_nametoindex(netdev
->created_name
);
3510 if (!netdev
->ifindex
)
3511 SYSERROR("Failed to retrieve ifindex for network device with name %s",
3512 netdev
->name
?: "(null)");
3515 /* get the new ifindex in case of physical netdev */
3516 if (netdev
->type
== LXC_NET_PHYS
) {
3517 netdev
->ifindex
= if_nametoindex(netdev
->link
);
3518 if (!netdev
->ifindex
) {
3519 ERROR("Failed to get ifindex for network device \"%s\"",
3525 /* retrieve the name of the interface */
3526 if (!if_indextoname(netdev
->ifindex
, current_ifname
)) {
3527 SYSERROR("Failed to retrieve name for network device with ifindex %d",
3532 /* Default: let the system choose an interface name.
3533 * When the IFLA_IFNAME attribute is passed something like "<prefix>%d"
3534 * netlink will replace the format specifier with an appropriate index.
3536 if (netdev
->name
[0] == '\0') {
3537 if (netdev
->type
== LXC_NET_PHYS
)
3538 (void)strlcpy(netdev
->name
, netdev
->link
, IFNAMSIZ
);
3540 (void)strlcpy(netdev
->name
, "eth%d", IFNAMSIZ
);
3543 /* rename the interface name */
3544 if (strcmp(current_ifname
, netdev
->name
) != 0) {
3545 err
= lxc_netdev_rename_by_name(current_ifname
, netdev
->name
);
3548 SYSERROR("Failed to rename network device \"%s\" to \"%s\"",
3549 current_ifname
, netdev
->name
);
3553 TRACE("Renamed network device from \"%s\" to \"%s\"",
3554 current_ifname
, netdev
->name
);
3557 /* Re-read the name of the interface because its name has changed
3558 * and would be automatically allocated by the system
3560 if (!if_indextoname(netdev
->ifindex
, current_ifname
)) {
3561 ERROR("Failed get name for network device with ifindex %d",
3566 /* Now update the recorded name of the network device to reflect the
3567 * name of the network device in the child's network namespace. We will
3568 * later on send this information back to the parent.
3570 (void)strlcpy(netdev
->name
, current_ifname
, IFNAMSIZ
);
3572 /* set a mac address */
3573 if (netdev
->hwaddr
) {
3574 if (setup_hw_addr(netdev
->hwaddr
, current_ifname
)) {
3575 ERROR("Failed to setup hw address for network device \"%s\"",
3581 /* setup ipv4 addresses on the interface */
3582 if (setup_ipv4_addr(&netdev
->ipv4
, netdev
->ifindex
)) {
3583 ERROR("Failed to setup ip addresses for network device \"%s\"",
3588 /* setup ipv6 addresses on the interface */
3589 if (setup_ipv6_addr(&netdev
->ipv6
, netdev
->ifindex
)) {
3590 ERROR("Failed to setup ipv6 addresses for network device \"%s\"",
3595 /* set the network device up */
3596 if (netdev
->flags
& IFF_UP
) {
3597 err
= lxc_netdev_up(current_ifname
);
3600 SYSERROR("Failed to set network device \"%s\" up",
3605 /* the network is up, make the loopback up too */
3606 err
= lxc_netdev_up("lo");
3609 SYSERROR("Failed to set the loopback network device up");
3614 /* setup ipv4 gateway on the interface */
3615 if (netdev
->ipv4_gateway
|| netdev
->ipv4_gateway_dev
) {
3616 if (!(netdev
->flags
& IFF_UP
)) {
3617 ERROR("Cannot add ipv4 gateway for network device "
3618 "\"%s\" when not bringing up the interface", current_ifname
);
3622 if (lxc_list_empty(&netdev
->ipv4
)) {
3623 ERROR("Cannot add ipv4 gateway for network device "
3624 "\"%s\" when not assigning an address", current_ifname
);
3628 /* Setup device route if ipv4_gateway_dev is enabled */
3629 if (netdev
->ipv4_gateway_dev
) {
3630 err
= lxc_ipv4_gateway_add(netdev
->ifindex
, NULL
);
3632 SYSERROR("Failed to setup ipv4 gateway to network device \"%s\"",
3634 return minus_one_set_errno(-err
);
3637 /* Check the gateway address is valid */
3638 if (!inet_ntop(AF_INET
, netdev
->ipv4_gateway
, bufinet4
, sizeof(bufinet4
)))
3639 return minus_one_set_errno(errno
);
3641 /* Try adding a default route to the gateway address */
3642 err
= lxc_ipv4_gateway_add(netdev
->ifindex
, netdev
->ipv4_gateway
);
3644 /* If adding the default route fails, this could be because the
3645 * gateway address is in a different subnet to the container's address.
3646 * To work around this, we try adding a static device route to the
3647 * gateway address first, and then try again.
3649 err
= lxc_ipv4_dest_add(netdev
->ifindex
, netdev
->ipv4_gateway
, 32);
3652 SYSERROR("Failed to add ipv4 dest \"%s\" for network device \"%s\"",
3653 bufinet4
, current_ifname
);
3657 err
= lxc_ipv4_gateway_add(netdev
->ifindex
, netdev
->ipv4_gateway
);
3660 SYSERROR("Failed to setup ipv4 gateway \"%s\" for network device \"%s\"",
3661 bufinet4
, current_ifname
);
3668 /* setup ipv6 gateway on the interface */
3669 if (netdev
->ipv6_gateway
|| netdev
->ipv6_gateway_dev
) {
3670 if (!(netdev
->flags
& IFF_UP
)) {
3671 ERROR("Cannot add ipv6 gateway for network device \"%s\" when not bringing up the interface",
3676 if (lxc_list_empty(&netdev
->ipv6
) && !IN6_IS_ADDR_LINKLOCAL(netdev
->ipv6_gateway
)) {
3677 ERROR("Cannot add ipv6 gateway for network device \"%s\" when not assigning an address",
3682 /* Setup device route if ipv6_gateway_dev is enabled */
3683 if (netdev
->ipv6_gateway_dev
) {
3684 err
= lxc_ipv6_gateway_add(netdev
->ifindex
, NULL
);
3686 SYSERROR("Failed to setup ipv6 gateway to network device \"%s\"",
3688 return minus_one_set_errno(-err
);
3691 /* Check the gateway address is valid */
3692 if (!inet_ntop(AF_INET6
, netdev
->ipv6_gateway
, bufinet6
, sizeof(bufinet6
)))
3693 return minus_one_set_errno(errno
);
3695 /* Try adding a default route to the gateway address */
3696 err
= lxc_ipv6_gateway_add(netdev
->ifindex
, netdev
->ipv6_gateway
);
3698 /* If adding the default route fails, this could be because the
3699 * gateway address is in a different subnet to the container's address.
3700 * To work around this, we try adding a static device route to the
3701 * gateway address first, and then try again.
3703 err
= lxc_ipv6_dest_add(netdev
->ifindex
, netdev
->ipv6_gateway
, 128);
3706 SYSERROR("Failed to add ipv6 dest \"%s\" for network device \"%s\"",
3707 bufinet6
, current_ifname
);
3711 err
= lxc_ipv6_gateway_add(netdev
->ifindex
, netdev
->ipv6_gateway
);
3714 SYSERROR("Failed to setup ipv6 gateway \"%s\" for network device \"%s\"",
3715 bufinet6
, current_ifname
);
3722 DEBUG("Network device \"%s\" has been setup", current_ifname
);
3727 int lxc_setup_network_in_child_namespaces(const struct lxc_conf
*conf
,
3728 struct lxc_list
*network
)
3730 struct lxc_list
*iterator
;
3732 lxc_list_for_each(iterator
, network
) {
3733 struct lxc_netdev
*netdev
= iterator
->elem
;
3735 if (lxc_setup_netdev_in_child_namespaces(netdev
)) {
3736 ERROR("Failed to setup netdev");
3741 if (!lxc_list_empty(network
))
3742 INFO("Network has been setup");
3747 int lxc_network_send_veth_names_to_child(struct lxc_handler
*handler
)
3749 struct lxc_list
*iterator
;
3750 struct lxc_list
*network
= &handler
->conf
->network
;
3751 int data_sock
= handler
->data_sock
[0];
3753 lxc_list_for_each(iterator
, network
) {
3755 struct lxc_netdev
*netdev
= iterator
->elem
;
3757 if (netdev
->type
!= LXC_NET_VETH
)
3760 ret
= lxc_send_nointr(data_sock
, netdev
->name
, IFNAMSIZ
, MSG_NOSIGNAL
);
3764 ret
= lxc_send_nointr(data_sock
, netdev
->created_name
, IFNAMSIZ
, MSG_NOSIGNAL
);
3768 TRACE("Sent network device name \"%s\" to child", netdev
->created_name
);
3774 int lxc_network_recv_veth_names_from_parent(struct lxc_handler
*handler
)
3776 struct lxc_list
*iterator
;
3777 struct lxc_list
*network
= &handler
->conf
->network
;
3778 int data_sock
= handler
->data_sock
[1];
3780 lxc_list_for_each(iterator
, network
) {
3782 struct lxc_netdev
*netdev
= iterator
->elem
;
3784 if (netdev
->type
!= LXC_NET_VETH
)
3787 ret
= lxc_recv_nointr(data_sock
, netdev
->name
, IFNAMSIZ
, 0);
3791 ret
= lxc_recv_nointr(data_sock
, netdev
->created_name
, IFNAMSIZ
, 0);
3794 TRACE("Received network device name \"%s\" from parent", netdev
->created_name
);
3800 int lxc_network_send_name_and_ifindex_to_parent(struct lxc_handler
*handler
)
3802 struct lxc_list
*iterator
, *network
;
3803 int data_sock
= handler
->data_sock
[0];
3805 if (!handler
->am_root
)
3808 network
= &handler
->conf
->network
;
3809 lxc_list_for_each(iterator
, network
) {
3811 struct lxc_netdev
*netdev
= iterator
->elem
;
3813 /* Send network device name in the child's namespace to parent. */
3814 ret
= lxc_send_nointr(data_sock
, netdev
->name
, IFNAMSIZ
, MSG_NOSIGNAL
);
3818 /* Send network device ifindex in the child's namespace to
3821 ret
= lxc_send_nointr(data_sock
, &netdev
->ifindex
, sizeof(netdev
->ifindex
), MSG_NOSIGNAL
);
3826 if (!lxc_list_empty(network
))
3827 TRACE("Sent network device names and ifindices to parent");
3832 int lxc_network_recv_name_and_ifindex_from_child(struct lxc_handler
*handler
)
3834 struct lxc_list
*iterator
, *network
;
3835 int data_sock
= handler
->data_sock
[1];
3837 if (!handler
->am_root
)
3840 network
= &handler
->conf
->network
;
3841 lxc_list_for_each(iterator
, network
) {
3843 struct lxc_netdev
*netdev
= iterator
->elem
;
3845 /* Receive network device name in the child's namespace to
3848 ret
= lxc_recv_nointr(data_sock
, netdev
->name
, IFNAMSIZ
, 0);
3852 /* Receive network device ifindex in the child's namespace to
3855 ret
= lxc_recv_nointr(data_sock
, &netdev
->ifindex
, sizeof(netdev
->ifindex
), 0);
3863 void lxc_delete_network(struct lxc_handler
*handler
)
3867 if (handler
->am_root
)
3868 bret
= lxc_delete_network_priv(handler
);
3870 bret
= lxc_delete_network_unpriv(handler
);
3872 DEBUG("Failed to delete network devices");
3874 DEBUG("Deleted network devices");
3877 int lxc_netns_set_nsid(int fd
)
3880 char buf
[NLMSG_ALIGN(sizeof(struct nlmsghdr
)) +
3881 NLMSG_ALIGN(sizeof(struct rtgenmsg
)) +
3883 struct nl_handler nlh
;
3884 struct nlmsghdr
*hdr
;
3885 struct rtgenmsg
*msg
;
3887 const __s32 ns_id
= -1;
3888 const __u32 netns_fd
= fd
;
3890 ret
= netlink_open(&nlh
, NETLINK_ROUTE
);
3894 memset(buf
, 0, sizeof(buf
));
3896 #pragma GCC diagnostic push
3897 #pragma GCC diagnostic ignored "-Wcast-align"
3898 hdr
= (struct nlmsghdr
*)buf
;
3899 msg
= (struct rtgenmsg
*)NLMSG_DATA(hdr
);
3900 #pragma GCC diagnostic pop
3902 hdr
->nlmsg_len
= NLMSG_LENGTH(sizeof(*msg
));
3903 hdr
->nlmsg_type
= RTM_NEWNSID
;
3904 hdr
->nlmsg_flags
= NLM_F_REQUEST
| NLM_F_ACK
;
3906 hdr
->nlmsg_seq
= RTM_NEWNSID
;
3907 msg
->rtgen_family
= AF_UNSPEC
;
3909 ret
= addattr(hdr
, 1024, __LXC_NETNSA_FD
, &netns_fd
, sizeof(netns_fd
));
3913 ret
= addattr(hdr
, 1024, __LXC_NETNSA_NSID
, &ns_id
, sizeof(ns_id
));
3917 ret
= __netlink_transaction(&nlh
, hdr
, hdr
);
3920 saved_errno
= errno
;
3921 netlink_close(&nlh
);
3922 errno
= saved_errno
;
3927 static int parse_rtattr(struct rtattr
*tb
[], int max
, struct rtattr
*rta
, int len
)
3930 memset(tb
, 0, sizeof(struct rtattr
*) * (max
+ 1));
3932 while (RTA_OK(rta
, len
)) {
3933 unsigned short type
= rta
->rta_type
;
3935 if ((type
<= max
) && (!tb
[type
]))
3938 #pragma GCC diagnostic push
3939 #pragma GCC diagnostic ignored "-Wcast-align"
3940 rta
= RTA_NEXT(rta
, len
);
3941 #pragma GCC diagnostic pop
3947 static inline __s32
rta_getattr_s32(const struct rtattr
*rta
)
3949 return *(__s32
*)RTA_DATA(rta
);
3953 #define NETNS_RTA(r) \
3954 ((struct rtattr *)(((char *)(r)) + NLMSG_ALIGN(sizeof(struct rtgenmsg))))
3957 int lxc_netns_get_nsid(int fd
)
3961 char buf
[NLMSG_ALIGN(sizeof(struct nlmsghdr
)) +
3962 NLMSG_ALIGN(sizeof(struct rtgenmsg
)) +
3964 struct rtattr
*tb
[__LXC_NETNSA_MAX
+ 1];
3965 struct nl_handler nlh
;
3966 struct nlmsghdr
*hdr
;
3967 struct rtgenmsg
*msg
;
3969 __u32 netns_fd
= fd
;
3971 ret
= netlink_open(&nlh
, NETLINK_ROUTE
);
3975 memset(buf
, 0, sizeof(buf
));
3977 #pragma GCC diagnostic push
3978 #pragma GCC diagnostic ignored "-Wcast-align"
3979 hdr
= (struct nlmsghdr
*)buf
;
3980 msg
= (struct rtgenmsg
*)NLMSG_DATA(hdr
);
3981 #pragma GCC diagnostic pop
3983 hdr
->nlmsg_len
= NLMSG_LENGTH(sizeof(*msg
));
3984 hdr
->nlmsg_type
= RTM_GETNSID
;
3985 hdr
->nlmsg_flags
= NLM_F_REQUEST
| NLM_F_ACK
;
3987 hdr
->nlmsg_seq
= RTM_GETNSID
;
3988 msg
->rtgen_family
= AF_UNSPEC
;
3990 ret
= addattr(hdr
, 1024, __LXC_NETNSA_FD
, &netns_fd
, sizeof(netns_fd
));
3992 ret
= __netlink_transaction(&nlh
, hdr
, hdr
);
3994 saved_errno
= errno
;
3995 netlink_close(&nlh
);
3996 errno
= saved_errno
;
4001 msg
= NLMSG_DATA(hdr
);
4002 len
= hdr
->nlmsg_len
- NLMSG_SPACE(sizeof(*msg
));
4006 #pragma GCC diagnostic push
4007 #pragma GCC diagnostic ignored "-Wcast-align"
4008 parse_rtattr(tb
, __LXC_NETNSA_MAX
, NETNS_RTA(msg
), len
);
4009 if (tb
[__LXC_NETNSA_NSID
])
4010 return rta_getattr_s32(tb
[__LXC_NETNSA_NSID
]);
4011 #pragma GCC diagnostic pop
4016 int lxc_create_network(struct lxc_handler
*handler
)
4021 * Find gateway addresses from the link device, which is no longer
4022 * accessible inside the container. Do this before creating network
4023 * interfaces, since goto out_delete_net does not work before
4026 ret
= lxc_find_gateway_addresses(handler
);
4028 ERROR("Failed to find gateway addresses");
4032 if (handler
->am_root
) {
4033 ret
= lxc_create_network_priv(handler
);
4037 return lxc_network_move_created_netdev_priv(handler
);
4040 return lxc_create_network_unpriv(handler
);