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 strlcpy(netdev
->created_name
, peer
, IFNAMSIZ
);
362 netdev
->ifindex
= if_nametoindex(peer
);
363 if (!netdev
->ifindex
) {
364 ERROR("Failed to retrieve ifindex for \"%s\"", peer
);
369 err
= lxc_safe_uint(netdev
->mtu
, &mtu
);
372 SYSERROR("Failed to parse mtu \"%s\" for interface \"%s\"", netdev
->mtu
, peer
);
376 err
= lxc_netdev_set_mtu(peer
, mtu
);
379 SYSERROR("Failed to set mtu \"%s\" for interface \"%s\"", netdev
->mtu
, peer
);
384 if (netdev
->upscript
) {
391 err
= run_script_argv(handler
->name
,
392 handler
->conf
->hooks_version
, "net",
393 netdev
->upscript
, "up", argv
);
398 DEBUG("Instantiated macvlan \"%s\" with ifindex is %d and mode %d",
399 peer
, netdev
->ifindex
, netdev
->priv
.macvlan_attr
.mode
);
404 lxc_netdev_delete_by_name(peer
);
408 static int lxc_ipvlan_create(const char *master
, const char *name
, int mode
, int isolation
)
411 struct ifinfomsg
*ifi
;
412 struct nl_handler nlh
;
413 struct rtattr
*nest
, *nest2
;
414 struct nlmsg
*answer
= NULL
, *nlmsg
= NULL
;
416 len
= strlen(master
);
417 if (len
== 1 || len
>= IFNAMSIZ
)
418 return minus_one_set_errno(EINVAL
);
421 if (len
== 1 || len
>= IFNAMSIZ
)
422 return minus_one_set_errno(EINVAL
);
424 index
= if_nametoindex(master
);
426 return minus_one_set_errno(EINVAL
);
428 err
= netlink_open(&nlh
, NETLINK_ROUTE
);
430 return minus_one_set_errno(-err
);
433 nlmsg
= nlmsg_alloc(NLMSG_GOOD_SIZE
);
437 answer
= nlmsg_alloc_reserve(NLMSG_GOOD_SIZE
);
441 nlmsg
->nlmsghdr
->nlmsg_flags
= NLM_F_REQUEST
| NLM_F_CREATE
| NLM_F_EXCL
| NLM_F_ACK
;
442 nlmsg
->nlmsghdr
->nlmsg_type
= RTM_NEWLINK
;
444 ifi
= nlmsg_reserve(nlmsg
, sizeof(struct ifinfomsg
));
448 ifi
->ifi_family
= AF_UNSPEC
;
451 nest
= nla_begin_nested(nlmsg
, IFLA_LINKINFO
);
455 if (nla_put_string(nlmsg
, IFLA_INFO_KIND
, "ipvlan"))
459 nest2
= nla_begin_nested(nlmsg
, IFLA_INFO_DATA
);
463 if (nla_put_u32(nlmsg
, IFLA_IPVLAN_MODE
, mode
))
466 /* if_link.h does not define the isolation flag value for bridge mode so we define it as 0
467 * and only send mode if mode >0 as default mode is bridge anyway according to ipvlan docs.
470 if (nla_put_u16(nlmsg
, IFLA_IPVLAN_ISOLATION
, isolation
))
474 nla_end_nested(nlmsg
, nest2
);
477 nla_end_nested(nlmsg
, nest
);
479 if (nla_put_u32(nlmsg
, IFLA_LINK
, index
))
482 if (nla_put_string(nlmsg
, IFLA_IFNAME
, name
))
485 err
= netlink_transaction(&nlh
, nlmsg
, answer
);
491 return minus_one_set_errno(-err
);
495 static int instantiate_ipvlan(struct lxc_handler
*handler
, struct lxc_netdev
*netdev
)
497 char peerbuf
[IFNAMSIZ
], *peer
;
499 unsigned int mtu
= 0;
501 if (netdev
->link
[0] == '\0') {
502 ERROR("No link for ipvlan network device specified");
506 err
= snprintf(peerbuf
, sizeof(peerbuf
), "ipXXXXXX");
507 if (err
< 0 || (size_t)err
>= sizeof(peerbuf
))
510 peer
= lxc_mkifname(peerbuf
);
514 err
= lxc_ipvlan_create(netdev
->link
, peer
, netdev
->priv
.ipvlan_attr
.mode
, netdev
->priv
.ipvlan_attr
.isolation
);
516 SYSERROR("Failed to create ipvlan interface \"%s\" on \"%s\"", peer
, netdev
->link
);
520 netdev
->ifindex
= if_nametoindex(peer
);
521 if (!netdev
->ifindex
) {
522 ERROR("Failed to retrieve ifindex for \"%s\"", peer
);
527 err
= lxc_safe_uint(netdev
->mtu
, &mtu
);
530 SYSERROR("Failed to parse mtu \"%s\" for interface \"%s\"", netdev
->mtu
, peer
);
534 err
= lxc_netdev_set_mtu(peer
, mtu
);
537 SYSERROR("Failed to set mtu \"%s\" for interface \"%s\"", netdev
->mtu
, peer
);
542 if (netdev
->upscript
) {
549 err
= run_script_argv(handler
->name
,
550 handler
->conf
->hooks_version
, "net",
551 netdev
->upscript
, "up", argv
);
556 DEBUG("Instantiated ipvlan \"%s\" with ifindex is %d and mode %d",
557 peer
, netdev
->ifindex
, netdev
->priv
.macvlan_attr
.mode
);
562 lxc_netdev_delete_by_name(peer
);
566 static int instantiate_vlan(struct lxc_handler
*handler
, struct lxc_netdev
*netdev
)
570 static uint16_t vlan_cntr
= 0;
571 unsigned int mtu
= 0;
573 if (netdev
->link
[0] == '\0') {
574 ERROR("No link for vlan network device specified");
578 err
= snprintf(peer
, sizeof(peer
), "vlan%d-%d", netdev
->priv
.vlan_attr
.vid
, vlan_cntr
++);
579 if (err
< 0 || (size_t)err
>= sizeof(peer
))
582 err
= lxc_vlan_create(netdev
->link
, peer
, netdev
->priv
.vlan_attr
.vid
);
585 SYSERROR("Failed to create vlan interface \"%s\" on \"%s\"",
590 netdev
->ifindex
= if_nametoindex(peer
);
591 if (!netdev
->ifindex
) {
592 ERROR("Failed to retrieve ifindex for \"%s\"", peer
);
597 err
= lxc_safe_uint(netdev
->mtu
, &mtu
);
600 SYSERROR("Failed to parse mtu \"%s\" for interface \"%s\"", netdev
->mtu
, peer
);
604 err
= lxc_netdev_set_mtu(peer
, mtu
);
607 SYSERROR("Failed to set mtu \"%s\" for interface \"%s\"", netdev
->mtu
, peer
);
612 if (netdev
->upscript
) {
619 err
= run_script_argv(handler
->name
,
620 handler
->conf
->hooks_version
, "net",
621 netdev
->upscript
, "up", argv
);
627 DEBUG("Instantiated vlan \"%s\" with ifindex is \"%d\"",
628 peer
, netdev
->ifindex
);
633 lxc_netdev_delete_by_name(peer
);
637 static int instantiate_phys(struct lxc_handler
*handler
, struct lxc_netdev
*netdev
)
639 int err
, mtu_orig
= 0;
640 unsigned int mtu
= 0;
642 if (netdev
->link
[0] == '\0') {
643 ERROR("No link for physical interface specified");
647 /* Note that we're retrieving the container's ifindex in the host's
648 * network namespace because we need it to move the device from the
649 * host's network namespace to the container's network namespace later
651 * Note that netdev->link will contain the name of the physical network
652 * device in the host's namespace.
654 netdev
->ifindex
= if_nametoindex(netdev
->link
);
655 if (!netdev
->ifindex
) {
656 ERROR("Failed to retrieve ifindex for \"%s\"", netdev
->link
);
660 /* Store the ifindex of the host's network device in the host's
663 netdev
->priv
.phys_attr
.ifindex
= netdev
->ifindex
;
665 /* Get original device MTU setting and store for restoration after container shutdown. */
666 mtu_orig
= netdev_get_mtu(netdev
->ifindex
);
668 SYSERROR("Failed to get original mtu for interface \"%s\"", netdev
->link
);
669 return minus_one_set_errno(-mtu_orig
);
672 netdev
->priv
.phys_attr
.mtu
= mtu_orig
;
675 err
= lxc_safe_uint(netdev
->mtu
, &mtu
);
678 SYSERROR("Failed to parse mtu \"%s\" for interface \"%s\"", netdev
->mtu
, netdev
->link
);
682 err
= lxc_netdev_set_mtu(netdev
->link
, mtu
);
685 SYSERROR("Failed to set mtu \"%s\" for interface \"%s\"", netdev
->mtu
, netdev
->link
);
690 if (netdev
->upscript
) {
697 err
= run_script_argv(handler
->name
,
698 handler
->conf
->hooks_version
, "net",
699 netdev
->upscript
, "up", argv
);
705 DEBUG("Instantiated phys \"%s\" with ifindex is \"%d\"", netdev
->link
, netdev
->ifindex
);
710 static int instantiate_empty(struct lxc_handler
*handler
, struct lxc_netdev
*netdev
)
719 if (!netdev
->upscript
)
722 ret
= run_script_argv(handler
->name
, handler
->conf
->hooks_version
,
723 "net", netdev
->upscript
, "up", argv
);
730 static int instantiate_none(struct lxc_handler
*handler
, struct lxc_netdev
*netdev
)
736 static instantiate_cb netdev_conf
[LXC_NET_MAXCONFTYPE
+ 1] = {
737 [LXC_NET_VETH
] = instantiate_veth
,
738 [LXC_NET_MACVLAN
] = instantiate_macvlan
,
739 [LXC_NET_IPVLAN
] = instantiate_ipvlan
,
740 [LXC_NET_VLAN
] = instantiate_vlan
,
741 [LXC_NET_PHYS
] = instantiate_phys
,
742 [LXC_NET_EMPTY
] = instantiate_empty
,
743 [LXC_NET_NONE
] = instantiate_none
,
746 static int shutdown_veth(struct lxc_handler
*handler
, struct lxc_netdev
*netdev
)
756 if (!netdev
->downscript
)
759 if (netdev
->priv
.veth_attr
.pair
[0] != '\0')
760 argv
[2] = netdev
->priv
.veth_attr
.pair
;
762 argv
[2] = netdev
->priv
.veth_attr
.veth1
;
764 ret
= run_script_argv(handler
->name
,
765 handler
->conf
->hooks_version
, "net",
766 netdev
->downscript
, "down", argv
);
773 static int shutdown_macvlan(struct lxc_handler
*handler
, struct lxc_netdev
*netdev
)
782 if (!netdev
->downscript
)
785 ret
= run_script_argv(handler
->name
, handler
->conf
->hooks_version
,
786 "net", netdev
->downscript
, "down", argv
);
793 static int shutdown_ipvlan(struct lxc_handler
*handler
, struct lxc_netdev
*netdev
)
802 if (!netdev
->downscript
)
805 ret
= run_script_argv(handler
->name
, handler
->conf
->hooks_version
,
806 "net", netdev
->downscript
, "down", argv
);
813 static int shutdown_vlan(struct lxc_handler
*handler
, struct lxc_netdev
*netdev
)
822 if (!netdev
->downscript
)
825 ret
= run_script_argv(handler
->name
, handler
->conf
->hooks_version
,
826 "net", netdev
->downscript
, "down", argv
);
833 static int shutdown_phys(struct lxc_handler
*handler
, struct lxc_netdev
*netdev
)
842 if (!netdev
->downscript
)
845 ret
= run_script_argv(handler
->name
, handler
->conf
->hooks_version
,
846 "net", netdev
->downscript
, "down", argv
);
853 static int shutdown_empty(struct lxc_handler
*handler
, struct lxc_netdev
*netdev
)
861 if (!netdev
->downscript
)
864 ret
= run_script_argv(handler
->name
, handler
->conf
->hooks_version
,
865 "net", netdev
->downscript
, "down", argv
);
872 static int shutdown_none(struct lxc_handler
*handler
, struct lxc_netdev
*netdev
)
877 static instantiate_cb netdev_deconf
[LXC_NET_MAXCONFTYPE
+ 1] = {
878 [LXC_NET_VETH
] = shutdown_veth
,
879 [LXC_NET_MACVLAN
] = shutdown_macvlan
,
880 [LXC_NET_IPVLAN
] = shutdown_ipvlan
,
881 [LXC_NET_VLAN
] = shutdown_vlan
,
882 [LXC_NET_PHYS
] = shutdown_phys
,
883 [LXC_NET_EMPTY
] = shutdown_empty
,
884 [LXC_NET_NONE
] = shutdown_none
,
887 static int lxc_netdev_move_by_index_fd(int ifindex
, int fd
, const char *ifname
)
890 struct nl_handler nlh
;
891 struct ifinfomsg
*ifi
;
892 struct nlmsg
*nlmsg
= NULL
;
894 err
= netlink_open(&nlh
, NETLINK_ROUTE
);
899 nlmsg
= nlmsg_alloc(NLMSG_GOOD_SIZE
);
903 nlmsg
->nlmsghdr
->nlmsg_flags
= NLM_F_REQUEST
| NLM_F_ACK
;
904 nlmsg
->nlmsghdr
->nlmsg_type
= RTM_NEWLINK
;
906 ifi
= nlmsg_reserve(nlmsg
, sizeof(struct ifinfomsg
));
909 ifi
->ifi_family
= AF_UNSPEC
;
910 ifi
->ifi_index
= ifindex
;
912 if (nla_put_u32(nlmsg
, IFLA_NET_NS_FD
, fd
))
915 if (ifname
!= NULL
) {
916 if (nla_put_string(nlmsg
, IFLA_IFNAME
, ifname
))
920 err
= netlink_transaction(&nlh
, nlmsg
, nlmsg
);
927 int lxc_netdev_move_by_index(int ifindex
, pid_t pid
, const char *ifname
)
930 struct nl_handler nlh
;
931 struct ifinfomsg
*ifi
;
932 struct nlmsg
*nlmsg
= NULL
;
934 err
= netlink_open(&nlh
, NETLINK_ROUTE
);
939 nlmsg
= nlmsg_alloc(NLMSG_GOOD_SIZE
);
943 nlmsg
->nlmsghdr
->nlmsg_flags
= NLM_F_REQUEST
| NLM_F_ACK
;
944 nlmsg
->nlmsghdr
->nlmsg_type
= RTM_NEWLINK
;
946 ifi
= nlmsg_reserve(nlmsg
, sizeof(struct ifinfomsg
));
949 ifi
->ifi_family
= AF_UNSPEC
;
950 ifi
->ifi_index
= ifindex
;
952 if (nla_put_u32(nlmsg
, IFLA_NET_NS_PID
, pid
))
955 if (ifname
!= NULL
) {
956 if (nla_put_string(nlmsg
, IFLA_IFNAME
, ifname
))
960 err
= netlink_transaction(&nlh
, nlmsg
, nlmsg
);
967 /* If we are asked to move a wireless interface, then we must actually move its
968 * phyN device. Detect that condition and return the physname here. The physname
969 * will be passed to lxc_netdev_move_wlan() which will free it when done.
971 #define PHYSNAME "/sys/class/net/%s/phy80211/name"
972 static char *is_wlan(const char *ifname
)
974 __do_free
char *path
= NULL
;
979 char *physname
= NULL
;
981 len
= strlen(ifname
) + strlen(PHYSNAME
) - 1;
982 path
= must_realloc(NULL
, len
+ 1);
983 ret
= snprintf(path
, len
, PHYSNAME
, ifname
);
984 if (ret
< 0 || (size_t)ret
>= len
)
987 f
= fopen(path
, "r");
991 /* Feh - sb.st_size is always 4096. */
992 fseek(f
, 0, SEEK_END
);
994 fseek(f
, 0, SEEK_SET
);
1000 physname
= malloc(physlen
+ 1);
1006 memset(physname
, 0, physlen
+ 1);
1007 ret
= fread(physname
, 1, physlen
, f
);
1012 for (i
= 0; i
< physlen
; i
++) {
1013 if (physname
[i
] == '\n')
1016 if (physname
[i
] == '\0')
1027 static int lxc_netdev_rename_by_name_in_netns(pid_t pid
, const char *old
,
1037 return wait_for_pid(fpid
);
1039 if (!switch_to_ns(pid
, "net"))
1042 _exit(lxc_netdev_rename_by_name(old
, new));
1045 static int lxc_netdev_move_wlan(char *physname
, const char *ifname
, pid_t pid
,
1046 const char *newname
)
1052 /* Move phyN into the container. TODO - do this using netlink.
1053 * However, IIUC this involves a bit more complicated work to talk to
1054 * the 80211 module, so for now just call out to iw.
1056 cmd
= on_path("iw", NULL
);
1067 sprintf(pidstr
, "%d", pid
);
1068 execlp("iw", "iw", "phy", physname
, "set", "netns", pidstr
,
1070 _exit(EXIT_FAILURE
);
1073 if (wait_for_pid(fpid
))
1078 err
= lxc_netdev_rename_by_name_in_netns(pid
, ifname
, newname
);
1085 int lxc_netdev_move_by_name(const char *ifname
, pid_t pid
, const char* newname
)
1093 index
= if_nametoindex(ifname
);
1097 physname
= is_wlan(ifname
);
1099 return lxc_netdev_move_wlan(physname
, ifname
, pid
, newname
);
1101 return lxc_netdev_move_by_index(index
, pid
, newname
);
1104 int lxc_netdev_delete_by_index(int ifindex
)
1107 struct ifinfomsg
*ifi
;
1108 struct nl_handler nlh
;
1109 struct nlmsg
*answer
= NULL
, *nlmsg
= NULL
;
1111 err
= netlink_open(&nlh
, NETLINK_ROUTE
);
1116 nlmsg
= nlmsg_alloc(NLMSG_GOOD_SIZE
);
1120 answer
= nlmsg_alloc_reserve(NLMSG_GOOD_SIZE
);
1124 nlmsg
->nlmsghdr
->nlmsg_flags
= NLM_F_ACK
| NLM_F_REQUEST
;
1125 nlmsg
->nlmsghdr
->nlmsg_type
= RTM_DELLINK
;
1127 ifi
= nlmsg_reserve(nlmsg
, sizeof(struct ifinfomsg
));
1130 ifi
->ifi_family
= AF_UNSPEC
;
1131 ifi
->ifi_index
= ifindex
;
1133 err
= netlink_transaction(&nlh
, nlmsg
, answer
);
1135 netlink_close(&nlh
);
1141 int lxc_netdev_delete_by_name(const char *name
)
1145 index
= if_nametoindex(name
);
1149 return lxc_netdev_delete_by_index(index
);
1152 int lxc_netdev_rename_by_index(int ifindex
, const char *newname
)
1155 struct ifinfomsg
*ifi
;
1156 struct nl_handler nlh
;
1157 struct nlmsg
*answer
= NULL
, *nlmsg
= NULL
;
1159 err
= netlink_open(&nlh
, NETLINK_ROUTE
);
1163 len
= strlen(newname
);
1164 if (len
== 1 || len
>= IFNAMSIZ
) {
1170 nlmsg
= nlmsg_alloc(NLMSG_GOOD_SIZE
);
1174 answer
= nlmsg_alloc_reserve(NLMSG_GOOD_SIZE
);
1178 nlmsg
->nlmsghdr
->nlmsg_flags
= NLM_F_ACK
| NLM_F_REQUEST
;
1179 nlmsg
->nlmsghdr
->nlmsg_type
= RTM_NEWLINK
;
1181 ifi
= nlmsg_reserve(nlmsg
, sizeof(struct ifinfomsg
));
1184 ifi
->ifi_family
= AF_UNSPEC
;
1185 ifi
->ifi_index
= ifindex
;
1187 if (nla_put_string(nlmsg
, IFLA_IFNAME
, newname
))
1190 err
= netlink_transaction(&nlh
, nlmsg
, answer
);
1192 netlink_close(&nlh
);
1198 int lxc_netdev_rename_by_name(const char *oldname
, const char *newname
)
1202 len
= strlen(oldname
);
1203 if (len
== 1 || len
>= IFNAMSIZ
)
1206 index
= if_nametoindex(oldname
);
1210 return lxc_netdev_rename_by_index(index
, newname
);
1213 int netdev_set_flag(const char *name
, int flag
)
1215 int err
, index
, len
;
1216 struct ifinfomsg
*ifi
;
1217 struct nl_handler nlh
;
1218 struct nlmsg
*answer
= NULL
, *nlmsg
= NULL
;
1220 err
= netlink_open(&nlh
, NETLINK_ROUTE
);
1226 if (len
== 1 || len
>= IFNAMSIZ
)
1230 nlmsg
= nlmsg_alloc(NLMSG_GOOD_SIZE
);
1234 answer
= nlmsg_alloc_reserve(NLMSG_GOOD_SIZE
);
1239 index
= if_nametoindex(name
);
1243 nlmsg
->nlmsghdr
->nlmsg_flags
= NLM_F_REQUEST
| NLM_F_ACK
;
1244 nlmsg
->nlmsghdr
->nlmsg_type
= RTM_NEWLINK
;
1246 ifi
= nlmsg_reserve(nlmsg
, sizeof(struct ifinfomsg
));
1251 ifi
->ifi_family
= AF_UNSPEC
;
1252 ifi
->ifi_index
= index
;
1253 ifi
->ifi_change
|= IFF_UP
;
1254 ifi
->ifi_flags
|= flag
;
1256 err
= netlink_transaction(&nlh
, nlmsg
, answer
);
1258 netlink_close(&nlh
);
1264 int netdev_get_flag(const char *name
, int *flag
)
1266 int err
, index
, len
;
1267 struct ifinfomsg
*ifi
;
1268 struct nl_handler nlh
;
1269 struct nlmsg
*answer
= NULL
, *nlmsg
= NULL
;
1274 err
= netlink_open(&nlh
, NETLINK_ROUTE
);
1280 if (len
== 1 || len
>= IFNAMSIZ
)
1284 nlmsg
= nlmsg_alloc(NLMSG_GOOD_SIZE
);
1288 answer
= nlmsg_alloc_reserve(NLMSG_GOOD_SIZE
);
1293 index
= if_nametoindex(name
);
1297 nlmsg
->nlmsghdr
->nlmsg_flags
= NLM_F_REQUEST
;
1298 nlmsg
->nlmsghdr
->nlmsg_type
= RTM_GETLINK
;
1300 ifi
= nlmsg_reserve(nlmsg
, sizeof(struct ifinfomsg
));
1305 ifi
->ifi_family
= AF_UNSPEC
;
1306 ifi
->ifi_index
= index
;
1308 err
= netlink_transaction(&nlh
, nlmsg
, answer
);
1312 ifi
= NLMSG_DATA(answer
->nlmsghdr
);
1314 *flag
= ifi
->ifi_flags
;
1316 netlink_close(&nlh
);
1323 * \brief Check a interface is up or not.
1325 * \param name: name for the interface.
1328 * 0 means interface is down.
1329 * 1 means interface is up.
1330 * Others means error happened, and ret-value is the error number.
1332 int lxc_netdev_isup(const char *name
)
1336 err
= netdev_get_flag(name
, &flag
);
1346 int netdev_get_mtu(int ifindex
)
1348 int answer_len
, err
, res
;
1349 struct nl_handler nlh
;
1350 struct ifinfomsg
*ifi
;
1351 struct nlmsghdr
*msg
;
1352 int readmore
= 0, recv_len
= 0;
1353 struct nlmsg
*answer
= NULL
, *nlmsg
= NULL
;
1355 err
= netlink_open(&nlh
, NETLINK_ROUTE
);
1360 nlmsg
= nlmsg_alloc(NLMSG_GOOD_SIZE
);
1364 answer
= nlmsg_alloc_reserve(NLMSG_GOOD_SIZE
);
1368 /* Save the answer buffer length, since it will be overwritten
1369 * on the first receive (and we might need to receive more than
1372 answer_len
= answer
->nlmsghdr
->nlmsg_len
;
1374 nlmsg
->nlmsghdr
->nlmsg_flags
= NLM_F_REQUEST
| NLM_F_DUMP
;
1375 nlmsg
->nlmsghdr
->nlmsg_type
= RTM_GETLINK
;
1377 ifi
= nlmsg_reserve(nlmsg
, sizeof(struct ifinfomsg
));
1380 ifi
->ifi_family
= AF_UNSPEC
;
1382 /* Send the request for addresses, which returns all addresses
1383 * on all interfaces. */
1384 err
= netlink_send(&nlh
, nlmsg
);
1388 #pragma GCC diagnostic push
1389 #pragma GCC diagnostic ignored "-Wcast-align"
1392 /* Restore the answer buffer length, it might have been
1393 * overwritten by a previous receive.
1395 answer
->nlmsghdr
->nlmsg_len
= answer_len
;
1397 /* Get the (next) batch of reply messages */
1398 err
= netlink_rcv(&nlh
, answer
);
1404 /* Satisfy the typing for the netlink macros */
1405 msg
= answer
->nlmsghdr
;
1407 while (NLMSG_OK(msg
, recv_len
)) {
1409 /* Stop reading if we see an error message */
1410 if (msg
->nlmsg_type
== NLMSG_ERROR
) {
1411 struct nlmsgerr
*errmsg
=
1412 (struct nlmsgerr
*)NLMSG_DATA(msg
);
1413 err
= errmsg
->error
;
1417 /* Stop reading if we see a NLMSG_DONE message */
1418 if (msg
->nlmsg_type
== NLMSG_DONE
) {
1423 ifi
= NLMSG_DATA(msg
);
1424 if (ifi
->ifi_index
== ifindex
) {
1425 struct rtattr
*rta
= IFLA_RTA(ifi
);
1427 msg
->nlmsg_len
- NLMSG_LENGTH(sizeof(*ifi
));
1429 while (RTA_OK(rta
, attr_len
)) {
1430 /* Found a local address for the
1431 * requested interface, return it.
1433 if (rta
->rta_type
== IFLA_MTU
) {
1434 memcpy(&res
, RTA_DATA(rta
),
1439 rta
= RTA_NEXT(rta
, attr_len
);
1443 /* Keep reading more data from the socket if the last
1444 * message had the NLF_F_MULTI flag set.
1446 readmore
= (msg
->nlmsg_flags
& NLM_F_MULTI
);
1448 /* Look at the next message received in this buffer. */
1449 msg
= NLMSG_NEXT(msg
, recv_len
);
1453 #pragma GCC diagnostic pop
1455 /* If we end up here, we didn't find any result, so signal an error. */
1459 netlink_close(&nlh
);
1465 int lxc_netdev_set_mtu(const char *name
, int mtu
)
1467 int err
, index
, len
;
1468 struct ifinfomsg
*ifi
;
1469 struct nl_handler nlh
;
1470 struct nlmsg
*answer
= NULL
, *nlmsg
= NULL
;
1472 err
= netlink_open(&nlh
, NETLINK_ROUTE
);
1478 if (len
== 1 || len
>= IFNAMSIZ
)
1482 nlmsg
= nlmsg_alloc(NLMSG_GOOD_SIZE
);
1486 answer
= nlmsg_alloc_reserve(NLMSG_GOOD_SIZE
);
1491 index
= if_nametoindex(name
);
1495 nlmsg
->nlmsghdr
->nlmsg_flags
= NLM_F_REQUEST
| NLM_F_ACK
;
1496 nlmsg
->nlmsghdr
->nlmsg_type
= RTM_NEWLINK
;
1498 ifi
= nlmsg_reserve(nlmsg
, sizeof(struct ifinfomsg
));
1503 ifi
->ifi_family
= AF_UNSPEC
;
1504 ifi
->ifi_index
= index
;
1506 if (nla_put_u32(nlmsg
, IFLA_MTU
, mtu
))
1509 err
= netlink_transaction(&nlh
, nlmsg
, answer
);
1511 netlink_close(&nlh
);
1517 int lxc_netdev_up(const char *name
)
1519 return netdev_set_flag(name
, IFF_UP
);
1522 int lxc_netdev_down(const char *name
)
1524 return netdev_set_flag(name
, 0);
1527 int lxc_veth_create(const char *name1
, const char *name2
)
1530 struct ifinfomsg
*ifi
;
1531 struct nl_handler nlh
;
1532 struct rtattr
*nest1
, *nest2
, *nest3
;
1533 struct nlmsg
*answer
= NULL
, *nlmsg
= NULL
;
1535 err
= netlink_open(&nlh
, NETLINK_ROUTE
);
1540 len
= strlen(name1
);
1541 if (len
== 1 || len
>= IFNAMSIZ
)
1544 len
= strlen(name2
);
1545 if (len
== 1 || len
>= IFNAMSIZ
)
1549 nlmsg
= nlmsg_alloc(NLMSG_GOOD_SIZE
);
1553 answer
= nlmsg_alloc_reserve(NLMSG_GOOD_SIZE
);
1557 nlmsg
->nlmsghdr
->nlmsg_flags
=
1558 NLM_F_REQUEST
| NLM_F_CREATE
| NLM_F_EXCL
| NLM_F_ACK
;
1559 nlmsg
->nlmsghdr
->nlmsg_type
= RTM_NEWLINK
;
1561 ifi
= nlmsg_reserve(nlmsg
, sizeof(struct ifinfomsg
));
1564 ifi
->ifi_family
= AF_UNSPEC
;
1567 nest1
= nla_begin_nested(nlmsg
, IFLA_LINKINFO
);
1571 if (nla_put_string(nlmsg
, IFLA_INFO_KIND
, "veth"))
1574 nest2
= nla_begin_nested(nlmsg
, IFLA_INFO_DATA
);
1578 nest3
= nla_begin_nested(nlmsg
, VETH_INFO_PEER
);
1582 ifi
= nlmsg_reserve(nlmsg
, sizeof(struct ifinfomsg
));
1588 if (nla_put_string(nlmsg
, IFLA_IFNAME
, name2
))
1591 nla_end_nested(nlmsg
, nest3
);
1592 nla_end_nested(nlmsg
, nest2
);
1593 nla_end_nested(nlmsg
, nest1
);
1595 if (nla_put_string(nlmsg
, IFLA_IFNAME
, name1
))
1598 err
= netlink_transaction(&nlh
, nlmsg
, answer
);
1600 netlink_close(&nlh
);
1606 /* TODO: merge with lxc_macvlan_create */
1607 int lxc_vlan_create(const char *master
, const char *name
, unsigned short vlanid
)
1609 int err
, len
, lindex
;
1610 struct ifinfomsg
*ifi
;
1611 struct nl_handler nlh
;
1612 struct rtattr
*nest
, *nest2
;
1613 struct nlmsg
*answer
= NULL
, *nlmsg
= NULL
;
1615 err
= netlink_open(&nlh
, NETLINK_ROUTE
);
1620 len
= strlen(master
);
1621 if (len
== 1 || len
>= IFNAMSIZ
)
1625 if (len
== 1 || len
>= IFNAMSIZ
)
1629 nlmsg
= nlmsg_alloc(NLMSG_GOOD_SIZE
);
1633 answer
= nlmsg_alloc_reserve(NLMSG_GOOD_SIZE
);
1638 lindex
= if_nametoindex(master
);
1642 nlmsg
->nlmsghdr
->nlmsg_flags
=
1643 NLM_F_REQUEST
| NLM_F_CREATE
| NLM_F_EXCL
| NLM_F_ACK
;
1644 nlmsg
->nlmsghdr
->nlmsg_type
= RTM_NEWLINK
;
1646 ifi
= nlmsg_reserve(nlmsg
, sizeof(struct ifinfomsg
));
1651 ifi
->ifi_family
= AF_UNSPEC
;
1653 nest
= nla_begin_nested(nlmsg
, IFLA_LINKINFO
);
1657 if (nla_put_string(nlmsg
, IFLA_INFO_KIND
, "vlan"))
1660 nest2
= nla_begin_nested(nlmsg
, IFLA_INFO_DATA
);
1664 if (nla_put_u16(nlmsg
, IFLA_VLAN_ID
, vlanid
))
1667 nla_end_nested(nlmsg
, nest2
);
1668 nla_end_nested(nlmsg
, nest
);
1670 if (nla_put_u32(nlmsg
, IFLA_LINK
, lindex
))
1673 if (nla_put_string(nlmsg
, IFLA_IFNAME
, name
))
1676 err
= netlink_transaction(&nlh
, nlmsg
, answer
);
1682 netlink_close(&nlh
);
1686 int lxc_macvlan_create(const char *master
, const char *name
, int mode
)
1688 int err
, index
, len
;
1689 struct ifinfomsg
*ifi
;
1690 struct nl_handler nlh
;
1691 struct rtattr
*nest
, *nest2
;
1692 struct nlmsg
*answer
= NULL
, *nlmsg
= NULL
;
1694 err
= netlink_open(&nlh
, NETLINK_ROUTE
);
1699 len
= strlen(master
);
1700 if (len
== 1 || len
>= IFNAMSIZ
)
1704 if (len
== 1 || len
>= IFNAMSIZ
)
1708 nlmsg
= nlmsg_alloc(NLMSG_GOOD_SIZE
);
1712 answer
= nlmsg_alloc_reserve(NLMSG_GOOD_SIZE
);
1717 index
= if_nametoindex(master
);
1721 nlmsg
->nlmsghdr
->nlmsg_flags
=
1722 NLM_F_REQUEST
| NLM_F_CREATE
| NLM_F_EXCL
| NLM_F_ACK
;
1723 nlmsg
->nlmsghdr
->nlmsg_type
= RTM_NEWLINK
;
1725 ifi
= nlmsg_reserve(nlmsg
, sizeof(struct ifinfomsg
));
1730 ifi
->ifi_family
= AF_UNSPEC
;
1732 nest
= nla_begin_nested(nlmsg
, IFLA_LINKINFO
);
1736 if (nla_put_string(nlmsg
, IFLA_INFO_KIND
, "macvlan"))
1740 nest2
= nla_begin_nested(nlmsg
, IFLA_INFO_DATA
);
1744 if (nla_put_u32(nlmsg
, IFLA_MACVLAN_MODE
, mode
))
1747 nla_end_nested(nlmsg
, nest2
);
1750 nla_end_nested(nlmsg
, nest
);
1752 if (nla_put_u32(nlmsg
, IFLA_LINK
, index
))
1755 if (nla_put_string(nlmsg
, IFLA_IFNAME
, name
))
1758 err
= netlink_transaction(&nlh
, nlmsg
, answer
);
1760 netlink_close(&nlh
);
1766 static int proc_sys_net_write(const char *path
, const char *value
)
1771 fd
= open(path
, O_WRONLY
);
1775 if (lxc_write_nointr(fd
, value
, strlen(value
)) < 0)
1782 static int lxc_is_ip_forwarding_enabled(const char *ifname
, int family
)
1785 char path
[PATH_MAX
];
1788 if (family
!= AF_INET
&& family
!= AF_INET6
)
1789 return minus_one_set_errno(EINVAL
);
1791 ret
= snprintf(path
, PATH_MAX
, "/proc/sys/net/%s/conf/%s/%s",
1792 family
== AF_INET
? "ipv4" : "ipv6", ifname
,
1794 if (ret
< 0 || (size_t)ret
>= PATH_MAX
)
1795 return minus_one_set_errno(E2BIG
);
1797 return lxc_read_file_expect(path
, buf
, 1, "1");
1800 static int neigh_proxy_set(const char *ifname
, int family
, int flag
)
1803 char path
[PATH_MAX
];
1805 if (family
!= AF_INET
&& family
!= AF_INET6
)
1808 ret
= snprintf(path
, PATH_MAX
, "/proc/sys/net/%s/conf/%s/%s",
1809 family
== AF_INET
? "ipv4" : "ipv6", ifname
,
1810 family
== AF_INET
? "proxy_arp" : "proxy_ndp");
1811 if (ret
< 0 || (size_t)ret
>= PATH_MAX
)
1814 return proc_sys_net_write(path
, flag
? "1" : "0");
1817 static int lxc_is_ip_neigh_proxy_enabled(const char *ifname
, int family
)
1820 char path
[PATH_MAX
];
1823 if (family
!= AF_INET
&& family
!= AF_INET6
)
1824 return minus_one_set_errno(EINVAL
);
1826 ret
= snprintf(path
, PATH_MAX
, "/proc/sys/net/%s/conf/%s/%s",
1827 family
== AF_INET
? "ipv4" : "ipv6", ifname
,
1828 family
== AF_INET
? "proxy_arp" : "proxy_ndp");
1829 if (ret
< 0 || (size_t)ret
>= PATH_MAX
)
1830 return minus_one_set_errno(E2BIG
);
1832 return lxc_read_file_expect(path
, buf
, 1, "1");
1835 int lxc_neigh_proxy_on(const char *name
, int family
)
1837 return neigh_proxy_set(name
, family
, 1);
1840 int lxc_neigh_proxy_off(const char *name
, int family
)
1842 return neigh_proxy_set(name
, family
, 0);
1845 int lxc_convert_mac(char *macaddr
, struct sockaddr
*sockaddr
)
1850 unsigned char *data
;
1852 sockaddr
->sa_family
= ARPHRD_ETHER
;
1853 data
= (unsigned char *)sockaddr
->sa_data
;
1855 while ((*macaddr
!= '\0') && (i
< ETH_ALEN
)) {
1859 else if (c
>= 'a' && c
<= 'f')
1861 else if (c
>= 'A' && c
<= 'F')
1870 else if (c
>= 'a' && c
<= 'f')
1871 val
|= c
- 'a' + 10;
1872 else if (c
>= 'A' && c
<= 'F')
1873 val
|= c
- 'A' + 10;
1874 else if (c
== ':' || c
== 0)
1880 *data
++ = (unsigned char)(val
& 0377);
1883 if (*macaddr
== ':')
1890 static int ip_addr_add(int family
, int ifindex
, void *addr
, void *bcast
,
1891 void *acast
, int prefix
)
1894 struct ifaddrmsg
*ifa
;
1895 struct nl_handler nlh
;
1896 struct nlmsg
*answer
= NULL
, *nlmsg
= NULL
;
1898 addrlen
= family
== AF_INET
? sizeof(struct in_addr
)
1899 : sizeof(struct in6_addr
);
1901 err
= netlink_open(&nlh
, NETLINK_ROUTE
);
1906 nlmsg
= nlmsg_alloc(NLMSG_GOOD_SIZE
);
1910 answer
= nlmsg_alloc_reserve(NLMSG_GOOD_SIZE
);
1914 nlmsg
->nlmsghdr
->nlmsg_flags
=
1915 NLM_F_ACK
| NLM_F_REQUEST
| NLM_F_CREATE
| NLM_F_EXCL
;
1916 nlmsg
->nlmsghdr
->nlmsg_type
= RTM_NEWADDR
;
1918 ifa
= nlmsg_reserve(nlmsg
, sizeof(struct ifaddrmsg
));
1921 ifa
->ifa_prefixlen
= prefix
;
1922 ifa
->ifa_index
= ifindex
;
1923 ifa
->ifa_family
= family
;
1927 if (nla_put_buffer(nlmsg
, IFA_LOCAL
, addr
, addrlen
))
1930 if (nla_put_buffer(nlmsg
, IFA_ADDRESS
, addr
, addrlen
))
1933 if (nla_put_buffer(nlmsg
, IFA_BROADCAST
, bcast
, addrlen
))
1936 /* TODO: multicast, anycast with ipv6 */
1937 err
= -EPROTONOSUPPORT
;
1938 if (family
== AF_INET6
&&
1939 (memcmp(bcast
, &in6addr_any
, sizeof(in6addr_any
)) ||
1940 memcmp(acast
, &in6addr_any
, sizeof(in6addr_any
))))
1943 err
= netlink_transaction(&nlh
, nlmsg
, answer
);
1945 netlink_close(&nlh
);
1951 int lxc_ipv6_addr_add(int ifindex
, struct in6_addr
*addr
,
1952 struct in6_addr
*mcast
, struct in6_addr
*acast
,
1955 return ip_addr_add(AF_INET6
, ifindex
, addr
, mcast
, acast
, prefix
);
1958 int lxc_ipv4_addr_add(int ifindex
, struct in_addr
*addr
, struct in_addr
*bcast
,
1961 return ip_addr_add(AF_INET
, ifindex
, addr
, bcast
, NULL
, prefix
);
1964 /* Find an IFA_LOCAL (or IFA_ADDRESS if not IFA_LOCAL is present) address from
1965 * the given RTM_NEWADDR message. Allocates memory for the address and stores
1966 * that pointer in *res (so res should be an in_addr** or in6_addr**).
1968 #pragma GCC diagnostic push
1969 #pragma GCC diagnostic ignored "-Wcast-align"
1971 static int ifa_get_local_ip(int family
, struct nlmsghdr
*msg
, void **res
)
1974 struct ifaddrmsg
*ifa
= NLMSG_DATA(msg
);
1975 struct rtattr
*rta
= IFA_RTA(ifa
);
1976 int attr_len
= NLMSG_PAYLOAD(msg
, sizeof(struct ifaddrmsg
));
1978 if (ifa
->ifa_family
!= family
)
1981 addrlen
= family
== AF_INET
? sizeof(struct in_addr
)
1982 : sizeof(struct in6_addr
);
1984 /* Loop over the rtattr's in this message */
1985 while (RTA_OK(rta
, attr_len
)) {
1986 /* Found a local address for the requested interface,
1989 if (rta
->rta_type
== IFA_LOCAL
||
1990 rta
->rta_type
== IFA_ADDRESS
) {
1991 /* Sanity check. The family check above should make sure
1992 * the address length is correct, but check here just in
1995 if (RTA_PAYLOAD(rta
) != addrlen
)
1998 /* We might have found an IFA_ADDRESS before, which we
1999 * now overwrite with an IFA_LOCAL.
2002 *res
= malloc(addrlen
);
2007 memcpy(*res
, RTA_DATA(rta
), addrlen
);
2008 if (rta
->rta_type
== IFA_LOCAL
)
2011 rta
= RTA_NEXT(rta
, attr_len
);
2016 #pragma GCC diagnostic pop
2018 static int ip_addr_get(int family
, int ifindex
, void **res
)
2020 int answer_len
, err
;
2021 struct ifaddrmsg
*ifa
;
2022 struct nl_handler nlh
;
2023 struct nlmsghdr
*msg
;
2024 int readmore
= 0, recv_len
= 0;
2025 struct nlmsg
*answer
= NULL
, *nlmsg
= NULL
;
2027 err
= netlink_open(&nlh
, NETLINK_ROUTE
);
2032 nlmsg
= nlmsg_alloc(NLMSG_GOOD_SIZE
);
2036 answer
= nlmsg_alloc_reserve(NLMSG_GOOD_SIZE
);
2040 /* Save the answer buffer length, since it will be overwritten on the
2041 * first receive (and we might need to receive more than once).
2043 answer_len
= answer
->nlmsghdr
->nlmsg_len
;
2045 nlmsg
->nlmsghdr
->nlmsg_flags
= NLM_F_REQUEST
| NLM_F_ROOT
;
2046 nlmsg
->nlmsghdr
->nlmsg_type
= RTM_GETADDR
;
2048 ifa
= nlmsg_reserve(nlmsg
, sizeof(struct ifaddrmsg
));
2051 ifa
->ifa_family
= family
;
2053 /* Send the request for addresses, which returns all addresses on all
2056 err
= netlink_send(&nlh
, nlmsg
);
2060 #pragma GCC diagnostic push
2061 #pragma GCC diagnostic ignored "-Wcast-align"
2064 /* Restore the answer buffer length, it might have been
2065 * overwritten by a previous receive.
2067 answer
->nlmsghdr
->nlmsg_len
= answer_len
;
2069 /* Get the (next) batch of reply messages. */
2070 err
= netlink_rcv(&nlh
, answer
);
2077 /* Satisfy the typing for the netlink macros. */
2078 msg
= answer
->nlmsghdr
;
2080 while (NLMSG_OK(msg
, recv_len
)) {
2081 /* Stop reading if we see an error message. */
2082 if (msg
->nlmsg_type
== NLMSG_ERROR
) {
2083 struct nlmsgerr
*errmsg
=
2084 (struct nlmsgerr
*)NLMSG_DATA(msg
);
2085 err
= errmsg
->error
;
2089 /* Stop reading if we see a NLMSG_DONE message. */
2090 if (msg
->nlmsg_type
== NLMSG_DONE
) {
2095 if (msg
->nlmsg_type
!= RTM_NEWADDR
) {
2100 ifa
= (struct ifaddrmsg
*)NLMSG_DATA(msg
);
2101 if (ifa
->ifa_index
== ifindex
) {
2102 if (ifa_get_local_ip(family
, msg
, res
) < 0) {
2107 /* Found a result, stop searching. */
2112 /* Keep reading more data from the socket if the last
2113 * message had the NLF_F_MULTI flag set.
2115 readmore
= (msg
->nlmsg_flags
& NLM_F_MULTI
);
2117 /* Look at the next message received in this buffer. */
2118 msg
= NLMSG_NEXT(msg
, recv_len
);
2122 #pragma GCC diagnostic pop
2124 /* If we end up here, we didn't find any result, so signal an
2130 netlink_close(&nlh
);
2136 int lxc_ipv6_addr_get(int ifindex
, struct in6_addr
**res
)
2138 return ip_addr_get(AF_INET6
, ifindex
, (void **)res
);
2141 int lxc_ipv4_addr_get(int ifindex
, struct in_addr
**res
)
2143 return ip_addr_get(AF_INET
, ifindex
, (void **)res
);
2146 static int ip_gateway_add(int family
, int ifindex
, void *gw
)
2149 struct nl_handler nlh
;
2151 struct nlmsg
*answer
= NULL
, *nlmsg
= NULL
;
2153 addrlen
= family
== AF_INET
? sizeof(struct in_addr
)
2154 : sizeof(struct in6_addr
);
2156 err
= netlink_open(&nlh
, NETLINK_ROUTE
);
2161 nlmsg
= nlmsg_alloc(NLMSG_GOOD_SIZE
);
2165 answer
= nlmsg_alloc_reserve(NLMSG_GOOD_SIZE
);
2169 nlmsg
->nlmsghdr
->nlmsg_flags
=
2170 NLM_F_ACK
| NLM_F_REQUEST
| NLM_F_CREATE
| NLM_F_EXCL
;
2171 nlmsg
->nlmsghdr
->nlmsg_type
= RTM_NEWROUTE
;
2173 rt
= nlmsg_reserve(nlmsg
, sizeof(struct rtmsg
));
2176 rt
->rtm_family
= family
;
2177 rt
->rtm_table
= RT_TABLE_MAIN
;
2178 rt
->rtm_scope
= RT_SCOPE_UNIVERSE
;
2179 rt
->rtm_protocol
= RTPROT_BOOT
;
2180 rt
->rtm_type
= RTN_UNICAST
;
2181 /* "default" destination */
2182 rt
->rtm_dst_len
= 0;
2186 /* If gateway address not supplied, then a device route will be created instead */
2188 if (nla_put_buffer(nlmsg
, RTA_GATEWAY
, gw
, addrlen
))
2192 /* Adding the interface index enables the use of link-local
2193 * addresses for the gateway.
2195 if (nla_put_u32(nlmsg
, RTA_OIF
, ifindex
))
2198 err
= netlink_transaction(&nlh
, nlmsg
, answer
);
2200 netlink_close(&nlh
);
2206 int lxc_ipv4_gateway_add(int ifindex
, struct in_addr
*gw
)
2208 return ip_gateway_add(AF_INET
, ifindex
, gw
);
2211 int lxc_ipv6_gateway_add(int ifindex
, struct in6_addr
*gw
)
2213 return ip_gateway_add(AF_INET6
, ifindex
, gw
);
2215 bool is_ovs_bridge(const char *bridge
)
2219 char brdirname
[22 + IFNAMSIZ
+ 1] = {0};
2221 ret
= snprintf(brdirname
, 22 + IFNAMSIZ
+ 1, "/sys/class/net/%s/bridge",
2223 if (ret
< 0 || (size_t)ret
>= 22 + IFNAMSIZ
+ 1)
2226 ret
= stat(brdirname
, &sb
);
2227 if (ret
< 0 && errno
== ENOENT
)
2233 struct ovs_veth_args
{
2238 /* Called from a background thread - when nic goes away, remove it from the
2241 static int lxc_ovs_delete_port_exec(void *data
)
2243 struct ovs_veth_args
*args
= data
;
2245 execlp("ovs-vsctl", "ovs-vsctl", "del-port", args
->bridge
, args
->nic
,
2250 int lxc_ovs_delete_port(const char *bridge
, const char *nic
)
2253 char cmd_output
[PATH_MAX
];
2254 struct ovs_veth_args args
;
2256 args
.bridge
= bridge
;
2258 ret
= run_command(cmd_output
, sizeof(cmd_output
),
2259 lxc_ovs_delete_port_exec
, (void *)&args
);
2261 ERROR("Failed to delete \"%s\" from openvswitch bridge \"%s\": "
2262 "%s", bridge
, nic
, cmd_output
);
2269 static int lxc_ovs_attach_bridge_exec(void *data
)
2271 struct ovs_veth_args
*args
= data
;
2273 execlp("ovs-vsctl", "ovs-vsctl", "add-port", args
->bridge
, args
->nic
,
2278 static int lxc_ovs_attach_bridge(const char *bridge
, const char *nic
)
2281 char cmd_output
[PATH_MAX
];
2282 struct ovs_veth_args args
;
2284 args
.bridge
= bridge
;
2286 ret
= run_command(cmd_output
, sizeof(cmd_output
),
2287 lxc_ovs_attach_bridge_exec
, (void *)&args
);
2289 ERROR("Failed to attach \"%s\" to openvswitch bridge \"%s\": %s",
2290 bridge
, nic
, cmd_output
);
2297 int lxc_bridge_attach(const char *bridge
, const char *ifname
)
2303 if (strlen(ifname
) >= IFNAMSIZ
)
2306 index
= if_nametoindex(ifname
);
2310 if (is_ovs_bridge(bridge
))
2311 return lxc_ovs_attach_bridge(bridge
, ifname
);
2313 fd
= socket(AF_INET
, SOCK_STREAM
| SOCK_CLOEXEC
, 0);
2317 retlen
= strlcpy(ifr
.ifr_name
, bridge
, IFNAMSIZ
);
2318 if (retlen
>= IFNAMSIZ
) {
2323 ifr
.ifr_name
[IFNAMSIZ
- 1] = '\0';
2324 ifr
.ifr_ifindex
= index
;
2325 err
= ioctl(fd
, SIOCBRADDIF
, &ifr
);
2333 static const char *const lxc_network_types
[LXC_NET_MAXCONFTYPE
+ 1] = {
2334 [LXC_NET_EMPTY
] = "empty",
2335 [LXC_NET_VETH
] = "veth",
2336 [LXC_NET_MACVLAN
] = "macvlan",
2337 [LXC_NET_IPVLAN
] = "ipvlan",
2338 [LXC_NET_PHYS
] = "phys",
2339 [LXC_NET_VLAN
] = "vlan",
2340 [LXC_NET_NONE
] = "none",
2343 const char *lxc_net_type_to_str(int type
)
2345 if (type
< 0 || type
> LXC_NET_MAXCONFTYPE
)
2348 return lxc_network_types
[type
];
2351 static const char padchar
[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
2353 char *lxc_mkifname(char *template)
2356 struct netns_ifaddrs
*ifa
, *ifaddr
;
2357 char name
[IFNAMSIZ
];
2358 bool exists
= false;
2363 seed
= randseed(false);
2366 (void)randseed(true);
2369 if (strlen(template) >= IFNAMSIZ
)
2372 /* Get all the network interfaces. */
2373 ret
= netns_getifaddrs(&ifaddr
, -1, &(bool){false});
2375 SYSERROR("Failed to get network interfaces");
2379 /* Generate random names until we find one that doesn't exist. */
2382 (void)strlcpy(name
, template, IFNAMSIZ
);
2386 for (i
= 0; i
< strlen(name
); i
++) {
2387 if (name
[i
] == 'X') {
2389 name
[i
] = padchar
[rand_r(&seed
) % strlen(padchar
)];
2391 name
[i
] = padchar
[rand() % strlen(padchar
)];
2396 for (ifa
= ifaddr
; ifa
!= NULL
; ifa
= ifa
->ifa_next
) {
2397 if (!strcmp(ifa
->ifa_name
, name
)) {
2407 netns_freeifaddrs(ifaddr
);
2408 (void)strlcpy(template, name
, strlen(template) + 1);
2413 int setup_private_host_hw_addr(char *veth1
)
2418 sockfd
= socket(AF_INET
, SOCK_DGRAM
| SOCK_CLOEXEC
, 0);
2422 err
= snprintf((char *)ifr
.ifr_name
, IFNAMSIZ
, "%s", veth1
);
2423 if (err
< 0 || (size_t)err
>= IFNAMSIZ
) {
2428 err
= ioctl(sockfd
, SIOCGIFHWADDR
, &ifr
);
2434 ifr
.ifr_hwaddr
.sa_data
[0] = 0xfe;
2435 err
= ioctl(sockfd
, SIOCSIFHWADDR
, &ifr
);
2443 int lxc_find_gateway_addresses(struct lxc_handler
*handler
)
2445 struct lxc_list
*network
= &handler
->conf
->network
;
2446 struct lxc_list
*iterator
;
2447 struct lxc_netdev
*netdev
;
2450 lxc_list_for_each(iterator
, network
) {
2451 netdev
= iterator
->elem
;
2453 if (!netdev
->ipv4_gateway_auto
&& !netdev
->ipv6_gateway_auto
)
2456 if (netdev
->type
!= LXC_NET_VETH
&& netdev
->type
!= LXC_NET_MACVLAN
) {
2457 ERROR("Automatic gateway detection is only supported "
2458 "for veth and macvlan");
2462 if (netdev
->link
[0] == '\0') {
2463 ERROR("Automatic gateway detection needs a link interface");
2467 link_index
= if_nametoindex(netdev
->link
);
2471 if (netdev
->ipv4_gateway_auto
) {
2472 if (lxc_ipv4_addr_get(link_index
, &netdev
->ipv4_gateway
)) {
2473 ERROR("Failed to automatically find ipv4 gateway "
2474 "address from link interface \"%s\"", netdev
->link
);
2479 if (netdev
->ipv6_gateway_auto
) {
2480 if (lxc_ipv6_addr_get(link_index
, &netdev
->ipv6_gateway
)) {
2481 ERROR("Failed to automatically find ipv6 gateway "
2482 "address from link interface \"%s\"", netdev
->link
);
2491 #define LXC_USERNIC_PATH LIBEXECDIR "/lxc/lxc-user-nic"
2492 static int lxc_create_network_unpriv_exec(const char *lxcpath
, const char *lxcname
,
2493 struct lxc_netdev
*netdev
, pid_t pid
, unsigned int hooks_version
)
2497 int bytes
, pipefd
[2];
2498 char *token
, *saveptr
= NULL
;
2499 char netdev_link
[IFNAMSIZ
];
2500 char buffer
[PATH_MAX
] = {0};
2503 if (netdev
->type
!= LXC_NET_VETH
) {
2504 ERROR("Network type %d not support for unprivileged use", netdev
->type
);
2510 SYSERROR("Failed to create pipe");
2516 SYSERROR("Failed to create new process");
2523 char pidstr
[INTTYPE_TO_STRLEN(pid_t
)];
2527 ret
= dup2(pipefd
[1], STDOUT_FILENO
);
2529 ret
= dup2(pipefd
[1], STDERR_FILENO
);
2532 SYSERROR("Failed to duplicate std{err,out} file descriptor");
2533 _exit(EXIT_FAILURE
);
2536 if (netdev
->link
[0] != '\0')
2537 retlen
= strlcpy(netdev_link
, netdev
->link
, IFNAMSIZ
);
2539 retlen
= strlcpy(netdev_link
, "none", IFNAMSIZ
);
2540 if (retlen
>= IFNAMSIZ
) {
2541 SYSERROR("Invalid network device name");
2542 _exit(EXIT_FAILURE
);
2545 ret
= snprintf(pidstr
, sizeof(pidstr
), "%d", pid
);
2546 if (ret
< 0 || ret
>= sizeof(pidstr
))
2547 _exit(EXIT_FAILURE
);
2548 pidstr
[sizeof(pidstr
) - 1] = '\0';
2550 INFO("Execing lxc-user-nic create %s %s %s veth %s %s", lxcpath
,
2551 lxcname
, pidstr
, netdev_link
,
2552 netdev
->name
[0] != '\0' ? netdev
->name
: "(null)");
2553 if (netdev
->name
[0] != '\0')
2554 execlp(LXC_USERNIC_PATH
, LXC_USERNIC_PATH
, "create",
2555 lxcpath
, lxcname
, pidstr
, "veth", netdev_link
,
2556 netdev
->name
, (char *)NULL
);
2558 execlp(LXC_USERNIC_PATH
, LXC_USERNIC_PATH
, "create",
2559 lxcpath
, lxcname
, pidstr
, "veth", netdev_link
,
2561 SYSERROR("Failed to execute lxc-user-nic");
2562 _exit(EXIT_FAILURE
);
2565 /* close the write-end of the pipe */
2568 bytes
= lxc_read_nointr(pipefd
[0], &buffer
, PATH_MAX
);
2570 SYSERROR("Failed to read from pipe file descriptor");
2573 buffer
[bytes
- 1] = '\0';
2576 ret
= wait_for_pid(child
);
2578 if (ret
!= 0 || bytes
< 0) {
2579 ERROR("lxc-user-nic failed to configure requested network: %s",
2580 buffer
[0] != '\0' ? buffer
: "(null)");
2583 TRACE("Received output \"%s\" from lxc-user-nic", buffer
);
2586 token
= strtok_r(buffer
, ":", &saveptr
);
2588 ERROR("Failed to parse lxc-user-nic output");
2593 * lxc-user-nic will take care of proper network device naming. So
2594 * netdev->name and netdev->created_name need to be identical to not
2595 * trigger another rename later on.
2597 retlen
= strlcpy(netdev
->name
, token
, IFNAMSIZ
);
2598 if (retlen
< IFNAMSIZ
)
2599 retlen
= strlcpy(netdev
->created_name
, token
, IFNAMSIZ
);
2600 if (retlen
>= IFNAMSIZ
) {
2601 ERROR("Container side veth device name returned by lxc-user-nic is too long");
2605 /* netdev->ifindex */
2606 token
= strtok_r(NULL
, ":", &saveptr
);
2608 ERROR("Failed to parse lxc-user-nic output");
2612 ret
= lxc_safe_int(token
, &netdev
->ifindex
);
2615 SYSERROR("Failed to convert string \"%s\" to integer", token
);
2619 /* netdev->priv.veth_attr.veth1 */
2620 token
= strtok_r(NULL
, ":", &saveptr
);
2622 ERROR("Failed to parse lxc-user-nic output");
2626 retlen
= strlcpy(netdev
->priv
.veth_attr
.veth1
, token
, IFNAMSIZ
);
2627 if (retlen
>= IFNAMSIZ
) {
2628 ERROR("Host side veth device name returned by lxc-user-nic is "
2633 /* netdev->priv.veth_attr.ifindex */
2634 token
= strtok_r(NULL
, ":", &saveptr
);
2636 ERROR("Failed to parse lxc-user-nic output");
2640 ret
= lxc_safe_int(token
, &netdev
->priv
.veth_attr
.ifindex
);
2643 SYSERROR("Failed to convert string \"%s\" to integer", token
);
2647 if (netdev
->upscript
) {
2651 netdev
->priv
.veth_attr
.veth1
,
2655 ret
= run_script_argv(lxcname
, hooks_version
, "net",
2656 netdev
->upscript
, "up", argv
);
2664 static int lxc_delete_network_unpriv_exec(const char *lxcpath
, const char *lxcname
,
2665 struct lxc_netdev
*netdev
,
2666 const char *netns_path
)
2671 char buffer
[PATH_MAX
] = {0};
2673 if (netdev
->type
!= LXC_NET_VETH
) {
2674 ERROR("Network type %d not support for unprivileged use", netdev
->type
);
2680 SYSERROR("Failed to create pipe");
2686 SYSERROR("Failed to create new process");
2697 ret
= dup2(pipefd
[1], STDOUT_FILENO
);
2699 ret
= dup2(pipefd
[1], STDERR_FILENO
);
2702 SYSERROR("Failed to duplicate std{err,out} file descriptor");
2703 _exit(EXIT_FAILURE
);
2706 if (netdev
->priv
.veth_attr
.pair
[0] != '\0')
2707 hostveth
= netdev
->priv
.veth_attr
.pair
;
2709 hostveth
= netdev
->priv
.veth_attr
.veth1
;
2710 if (hostveth
[0] == '\0') {
2711 SYSERROR("Host side veth device name is missing");
2712 _exit(EXIT_FAILURE
);
2715 if (netdev
->link
[0] == '\0') {
2716 SYSERROR("Network link for network device \"%s\" is "
2717 "missing", netdev
->priv
.veth_attr
.veth1
);
2718 _exit(EXIT_FAILURE
);
2721 INFO("Execing lxc-user-nic delete %s %s %s veth %s %s", lxcpath
,
2722 lxcname
, netns_path
, netdev
->link
, hostveth
);
2723 execlp(LXC_USERNIC_PATH
, LXC_USERNIC_PATH
, "delete", lxcpath
,
2724 lxcname
, netns_path
, "veth", netdev
->link
, hostveth
,
2726 SYSERROR("Failed to exec lxc-user-nic.");
2727 _exit(EXIT_FAILURE
);
2732 bytes
= lxc_read_nointr(pipefd
[0], &buffer
, PATH_MAX
);
2734 SYSERROR("Failed to read from pipe file descriptor.");
2737 buffer
[bytes
- 1] = '\0';
2740 ret
= wait_for_pid(child
);
2742 if (ret
!= 0 || bytes
< 0) {
2743 ERROR("lxc-user-nic failed to delete requested network: %s",
2744 buffer
[0] != '\0' ? buffer
: "(null)");
2751 bool lxc_delete_network_unpriv(struct lxc_handler
*handler
)
2754 struct lxc_list
*iterator
;
2755 struct lxc_list
*network
= &handler
->conf
->network
;
2756 /* strlen("/proc/") = 6
2758 * INTTYPE_TO_STRLEN(pid_t)
2760 * strlen("/fd/") = 4
2762 * INTTYPE_TO_STRLEN(int)
2766 char netns_path
[6 + INTTYPE_TO_STRLEN(pid_t
) + 4 + INTTYPE_TO_STRLEN(int) + 1];
2770 if (handler
->nsfd
[LXC_NS_NET
] < 0) {
2771 DEBUG("Cannot not guarantee safe deletion of network devices. "
2772 "Manual cleanup maybe needed");
2776 ret
= snprintf(netns_path
, sizeof(netns_path
), "/proc/%d/fd/%d",
2777 lxc_raw_getpid(), handler
->nsfd
[LXC_NS_NET
]);
2778 if (ret
< 0 || ret
>= sizeof(netns_path
))
2781 lxc_list_for_each(iterator
, network
) {
2782 char *hostveth
= NULL
;
2783 struct lxc_netdev
*netdev
= iterator
->elem
;
2785 /* We can only delete devices whose ifindex we have. If we don't
2786 * have the index it means that we didn't create it.
2788 if (!netdev
->ifindex
)
2791 if (netdev
->type
== LXC_NET_PHYS
) {
2792 ret
= lxc_netdev_rename_by_index(netdev
->ifindex
,
2795 WARN("Failed to rename interface with index %d "
2796 "to its initial name \"%s\"",
2797 netdev
->ifindex
, netdev
->link
);
2799 TRACE("Renamed interface with index %d to its "
2800 "initial name \"%s\"",
2801 netdev
->ifindex
, netdev
->link
);
2803 ret
= netdev_deconf
[netdev
->type
](handler
, netdev
);
2804 goto clear_ifindices
;
2807 ret
= netdev_deconf
[netdev
->type
](handler
, netdev
);
2809 WARN("Failed to deconfigure network device");
2811 if (netdev
->type
!= LXC_NET_VETH
)
2812 goto clear_ifindices
;
2814 if (netdev
->link
[0] == '\0' || !is_ovs_bridge(netdev
->link
))
2815 goto clear_ifindices
;
2817 if (netdev
->priv
.veth_attr
.pair
[0] != '\0')
2818 hostveth
= netdev
->priv
.veth_attr
.pair
;
2820 hostveth
= netdev
->priv
.veth_attr
.veth1
;
2821 if (hostveth
[0] == '\0')
2822 goto clear_ifindices
;
2824 ret
= lxc_delete_network_unpriv_exec(handler
->lxcpath
,
2825 handler
->name
, netdev
,
2828 WARN("Failed to remove port \"%s\" from openvswitch "
2829 "bridge \"%s\"", hostveth
, netdev
->link
);
2830 goto clear_ifindices
;
2832 INFO("Removed interface \"%s\" from \"%s\"", hostveth
,
2836 /* We need to clear any ifindices we recorded so liblxc won't
2837 * have cached stale data which would cause it to fail on reboot
2838 * we're we don't re-read the on-disk config file.
2840 netdev
->ifindex
= 0;
2841 if (netdev
->type
== LXC_NET_PHYS
) {
2842 netdev
->priv
.phys_attr
.ifindex
= 0;
2843 } else if (netdev
->type
== LXC_NET_VETH
) {
2844 netdev
->priv
.veth_attr
.veth1
[0] = '\0';
2845 netdev
->priv
.veth_attr
.ifindex
= 0;
2852 struct ip_proxy_args
{
2857 static int lxc_add_ip_neigh_proxy_exec_wrapper(void *data
)
2859 struct ip_proxy_args
*args
= data
;
2861 execlp("ip", "ip", "neigh", "add", "proxy", args
->ip
, "dev", args
->dev
, (char *)NULL
);
2865 static int lxc_del_ip_neigh_proxy_exec_wrapper(void *data
)
2867 struct ip_proxy_args
*args
= data
;
2869 execlp("ip", "ip", "neigh", "flush", "proxy", args
->ip
, "dev", args
->dev
, (char *)NULL
);
2873 static int lxc_add_ip_neigh_proxy(const char *ip
, const char *dev
)
2876 char cmd_output
[PATH_MAX
];
2877 struct ip_proxy_args args
= {
2882 ret
= run_command(cmd_output
, sizeof(cmd_output
), lxc_add_ip_neigh_proxy_exec_wrapper
, &args
);
2884 ERROR("Failed to add ip proxy \"%s\" to dev \"%s\": %s", ip
, dev
, cmd_output
);
2891 static int lxc_del_ip_neigh_proxy(const char *ip
, const char *dev
)
2894 char cmd_output
[PATH_MAX
];
2895 struct ip_proxy_args args
= {
2900 ret
= run_command(cmd_output
, sizeof(cmd_output
), lxc_del_ip_neigh_proxy_exec_wrapper
, &args
);
2902 ERROR("Failed to delete ip proxy \"%s\" to dev \"%s\": %s", ip
, dev
, cmd_output
);
2909 static int lxc_setup_l2proxy(struct lxc_netdev
*netdev
) {
2910 struct lxc_list
*cur
, *next
;
2911 struct lxc_inetdev
*inet4dev
;
2912 struct lxc_inet6dev
*inet6dev
;
2913 char bufinet4
[INET_ADDRSTRLEN
], bufinet6
[INET6_ADDRSTRLEN
];
2915 unsigned int lo_ifindex
= 0;
2917 /* If IPv4 addresses are specified, then check that sysctl is configured correctly. */
2918 if (!lxc_list_empty(&netdev
->ipv4
)) {
2919 /* Check for net.ipv4.conf.[link].forwarding=1 */
2920 if (lxc_is_ip_forwarding_enabled(netdev
->link
, AF_INET
) < 0) {
2921 ERROR("Requires sysctl net.ipv4.conf.%s.forwarding=1", netdev
->link
);
2922 return minus_one_set_errno(EINVAL
);
2926 /* If IPv6 addresses are specified, then check that sysctl is configured correctly. */
2927 if (!lxc_list_empty(&netdev
->ipv6
)) {
2928 /* Check for net.ipv6.conf.[link].proxy_ndp=1 */
2929 if (lxc_is_ip_neigh_proxy_enabled(netdev
->link
, AF_INET6
) < 0) {
2930 ERROR("Requires sysctl net.ipv6.conf.%s.proxy_ndp=1", netdev
->link
);
2931 return minus_one_set_errno(EINVAL
);
2934 /* Check for net.ipv6.conf.[link].forwarding=1 */
2935 if (lxc_is_ip_forwarding_enabled(netdev
->link
, AF_INET6
) < 0) {
2936 ERROR("Requires sysctl net.ipv6.conf.%s.forwarding=1", netdev
->link
);
2937 return minus_one_set_errno(EINVAL
);
2941 /* Perform IPVLAN specific checks. */
2942 if (netdev
->type
== LXC_NET_IPVLAN
) {
2943 /* Check mode is l3s as other modes do not work with l2proxy. */
2944 if (netdev
->priv
.ipvlan_attr
.mode
!= IPVLAN_MODE_L3S
) {
2945 ERROR("Requires ipvlan mode on dev \"%s\" be l3s when used with l2proxy", netdev
->link
);
2946 return minus_one_set_errno(EINVAL
);
2949 /* Retrieve local-loopback interface index for use with IPVLAN static routes. */
2950 lo_ifindex
= if_nametoindex(loop_device
);
2951 if (lo_ifindex
== 0) {
2952 ERROR("Failed to retrieve ifindex for \"%s\" routing cleanup", loop_device
);
2953 return minus_one_set_errno(EINVAL
);
2957 lxc_list_for_each_safe(cur
, &netdev
->ipv4
, next
) {
2958 inet4dev
= cur
->elem
;
2959 if (!inet_ntop(AF_INET
, &inet4dev
->addr
, bufinet4
, sizeof(bufinet4
)))
2960 return minus_one_set_errno(-errno
);
2962 if (lxc_add_ip_neigh_proxy(bufinet4
, netdev
->link
) < 0)
2963 return minus_one_set_errno(EINVAL
);
2965 /* IPVLAN requires a route to local-loopback to trigger l2proxy. */
2966 if (netdev
->type
== LXC_NET_IPVLAN
) {
2967 err
= lxc_ipv4_dest_add(lo_ifindex
, &inet4dev
->addr
, 32);
2969 ERROR("Failed to add ipv4 dest \"%s\" for network device \"%s\"", bufinet4
, loop_device
);
2970 return minus_one_set_errno(-err
);
2975 lxc_list_for_each_safe(cur
, &netdev
->ipv6
, next
) {
2976 inet6dev
= cur
->elem
;
2977 if (!inet_ntop(AF_INET6
, &inet6dev
->addr
, bufinet6
, sizeof(bufinet6
)))
2978 return minus_one_set_errno(-errno
);
2980 if (lxc_add_ip_neigh_proxy(bufinet6
, netdev
->link
) < 0)
2981 return minus_one_set_errno(EINVAL
);
2983 /* IPVLAN requires a route to local-loopback to trigger l2proxy. */
2984 if (netdev
->type
== LXC_NET_IPVLAN
) {
2985 err
= lxc_ipv6_dest_add(lo_ifindex
, &inet6dev
->addr
, 128);
2987 ERROR("Failed to add ipv6 dest \"%s\" for network device \"%s\"", bufinet6
, loop_device
);
2988 return minus_one_set_errno(-err
);
2996 static int lxc_delete_ipv4_l2proxy(struct in_addr
*ip
, char *link
, unsigned int lo_ifindex
) {
2997 char bufinet4
[INET_ADDRSTRLEN
];
2998 unsigned int errCount
= 0;
3000 if (!inet_ntop(AF_INET
, ip
, bufinet4
, sizeof(bufinet4
))) {
3001 SYSERROR("Failed to convert IP for l2proxy ipv4 removal on dev \"%s\"", link
);
3002 return minus_one_set_errno(EINVAL
);
3005 /* If a local-loopback ifindex supplied remove the static route to the lo device. */
3006 if (lo_ifindex
> 0) {
3007 if (lxc_ipv4_dest_del(lo_ifindex
, ip
, 32) < 0) {
3009 ERROR("Failed to delete ipv4 dest \"%s\" for network ifindex \"%u\"", bufinet4
, lo_ifindex
);
3013 /* If link is supplied remove the IP neigh proxy entry for this IP on the device. */
3014 if (link
[0] != '\0') {
3015 if (lxc_del_ip_neigh_proxy(bufinet4
, link
) < 0)
3020 return minus_one_set_errno(EINVAL
);
3025 static int lxc_delete_ipv6_l2proxy(struct in6_addr
*ip
, char *link
, unsigned int lo_ifindex
) {
3026 char bufinet6
[INET6_ADDRSTRLEN
];
3027 unsigned int errCount
= 0;
3029 if (!inet_ntop(AF_INET6
, ip
, bufinet6
, sizeof(bufinet6
))) {
3030 SYSERROR("Failed to convert IP for l2proxy ipv6 removal on dev \"%s\"", link
);
3031 return minus_one_set_errno(EINVAL
);
3034 /* If a local-loopback ifindex supplied remove the static route to the lo device. */
3035 if (lo_ifindex
> 0) {
3036 if (lxc_ipv6_dest_del(lo_ifindex
, ip
, 128) < 0) {
3038 ERROR("Failed to delete ipv6 dest \"%s\" for network ifindex \"%u\"", bufinet6
, lo_ifindex
);
3042 /* If link is supplied remove the IP neigh proxy entry for this IP on the device. */
3043 if (link
[0] != '\0') {
3044 if (lxc_del_ip_neigh_proxy(bufinet6
, link
) < 0)
3049 return minus_one_set_errno(EINVAL
);
3054 static int lxc_delete_l2proxy(struct lxc_netdev
*netdev
) {
3055 unsigned int lo_ifindex
= 0;
3056 unsigned int errCount
= 0;
3057 struct lxc_list
*cur
, *next
;
3058 struct lxc_inetdev
*inet4dev
;
3059 struct lxc_inet6dev
*inet6dev
;
3061 /* Perform IPVLAN specific checks. */
3062 if (netdev
->type
== LXC_NET_IPVLAN
) {
3063 /* Retrieve local-loopback interface index for use with IPVLAN static routes. */
3064 lo_ifindex
= if_nametoindex(loop_device
);
3065 if (lo_ifindex
== 0) {
3067 ERROR("Failed to retrieve ifindex for \"%s\" routing cleanup", loop_device
);
3071 lxc_list_for_each_safe(cur
, &netdev
->ipv4
, next
) {
3072 inet4dev
= cur
->elem
;
3073 if (lxc_delete_ipv4_l2proxy(&inet4dev
->addr
, netdev
->link
, lo_ifindex
) < 0)
3077 lxc_list_for_each_safe(cur
, &netdev
->ipv6
, next
) {
3078 inet6dev
= cur
->elem
;
3079 if (lxc_delete_ipv6_l2proxy(&inet6dev
->addr
, netdev
->link
, lo_ifindex
) < 0)
3084 return minus_one_set_errno(EINVAL
);
3089 static int lxc_create_network_priv(struct lxc_handler
*handler
)
3091 struct lxc_list
*iterator
;
3092 struct lxc_list
*network
= &handler
->conf
->network
;
3094 lxc_list_for_each(iterator
, network
) {
3095 struct lxc_netdev
*netdev
= iterator
->elem
;
3097 if (netdev
->type
< 0 || netdev
->type
> LXC_NET_MAXCONFTYPE
) {
3098 ERROR("Invalid network configuration type %d", netdev
->type
);
3102 /* Setup l2proxy entries if enabled and used with a link property */
3103 if (netdev
->l2proxy
&& netdev
->link
[0] != '\0') {
3104 if (lxc_setup_l2proxy(netdev
)) {
3105 ERROR("Failed to setup l2proxy");
3110 if (netdev_conf
[netdev
->type
](handler
, netdev
)) {
3111 ERROR("Failed to create network device");
3119 int lxc_network_move_created_netdev_priv(struct lxc_handler
*handler
)
3121 pid_t pid
= handler
->pid
;
3122 struct lxc_list
*network
= &handler
->conf
->network
;
3123 struct lxc_list
*iterator
;
3125 if (am_guest_unpriv())
3128 lxc_list_for_each(iterator
, network
) {
3130 char ifname
[IFNAMSIZ
];
3131 struct lxc_netdev
*netdev
= iterator
->elem
;
3133 if (!netdev
->ifindex
)
3136 /* retrieve the name of the interface */
3137 if (!if_indextoname(netdev
->ifindex
, ifname
)) {
3138 ERROR("No interface corresponding to ifindex \"%d\"",
3143 ret
= lxc_netdev_move_by_name(ifname
, pid
, NULL
);
3146 SYSERROR("Failed to move network device \"%s\" to network namespace %d",
3151 strlcpy(netdev
->created_name
, ifname
, IFNAMSIZ
);
3153 DEBUG("Moved network device \"%s\" to network namespace of %d",
3154 netdev
->created_name
, pid
);
3160 static int lxc_create_network_unpriv(struct lxc_handler
*handler
)
3162 int hooks_version
= handler
->conf
->hooks_version
;
3163 const char *lxcname
= handler
->name
;
3164 const char *lxcpath
= handler
->lxcpath
;
3165 struct lxc_list
*network
= &handler
->conf
->network
;
3166 pid_t pid
= handler
->pid
;
3167 struct lxc_list
*iterator
;
3169 lxc_list_for_each(iterator
, network
) {
3170 struct lxc_netdev
*netdev
= iterator
->elem
;
3172 if (netdev
->type
== LXC_NET_EMPTY
)
3175 if (netdev
->type
== LXC_NET_NONE
)
3178 if (netdev
->type
!= LXC_NET_VETH
) {
3179 ERROR("Networks of type %s are not supported by unprivileged containers",
3180 lxc_net_type_to_str(netdev
->type
));
3185 INFO("mtu ignored due to insufficient privilege");
3187 if (lxc_create_network_unpriv_exec(lxcpath
, lxcname
, netdev
,
3188 pid
, hooks_version
))
3195 bool lxc_delete_network_priv(struct lxc_handler
*handler
)
3198 struct lxc_list
*iterator
;
3199 struct lxc_list
*network
= &handler
->conf
->network
;
3201 lxc_list_for_each(iterator
, network
) {
3202 char *hostveth
= NULL
;
3203 struct lxc_netdev
*netdev
= iterator
->elem
;
3205 /* We can only delete devices whose ifindex we have. If we don't
3206 * have the index it means that we didn't create it.
3208 if (!netdev
->ifindex
)
3211 /* Delete l2proxy entries if enabled and used with a link property */
3212 if (netdev
->l2proxy
&& netdev
->link
[0] != '\0') {
3213 if (lxc_delete_l2proxy(netdev
))
3214 WARN("Failed to delete all l2proxy config");
3215 /* Don't return, let the network be cleaned up as normal. */
3218 if (netdev
->type
== LXC_NET_PHYS
) {
3219 ret
= lxc_netdev_rename_by_index(netdev
->ifindex
, netdev
->link
);
3221 WARN("Failed to rename interface with index %d "
3222 "from \"%s\" to its initial name \"%s\"",
3223 netdev
->ifindex
, netdev
->name
, netdev
->link
);
3225 TRACE("Renamed interface with index %d from "
3226 "\"%s\" to its initial name \"%s\"",
3227 netdev
->ifindex
, netdev
->name
,
3230 /* Restore original MTU */
3231 ret
= lxc_netdev_set_mtu(netdev
->link
, netdev
->priv
.phys_attr
.mtu
);
3233 WARN("Failed to set interface \"%s\" to its initial mtu \"%d\"",
3234 netdev
->link
, netdev
->priv
.phys_attr
.mtu
);
3236 TRACE("Restored interface \"%s\" to its initial mtu \"%d\"",
3237 netdev
->link
, netdev
->priv
.phys_attr
.mtu
);
3241 ret
= netdev_deconf
[netdev
->type
](handler
, netdev
);
3242 goto clear_ifindices
;
3245 ret
= netdev_deconf
[netdev
->type
](handler
, netdev
);
3247 WARN("Failed to deconfigure network device");
3249 /* Recent kernels remove the virtual interfaces when the network
3250 * namespace is destroyed but in case we did not move the
3251 * interface to the network namespace, we have to destroy it.
3253 ret
= lxc_netdev_delete_by_index(netdev
->ifindex
);
3255 if (errno
!= ENODEV
) {
3256 WARN("Failed to remove interface \"%s\" with index %d",
3257 netdev
->name
[0] != '\0' ? netdev
->name
: "(null)",
3259 goto clear_ifindices
;
3261 INFO("Interface \"%s\" with index %d already deleted or existing in different network namespace",
3262 netdev
->name
[0] != '\0' ? netdev
->name
: "(null)",
3265 INFO("Removed interface \"%s\" with index %d",
3266 netdev
->name
[0] != '\0' ? netdev
->name
: "(null)",
3269 if (netdev
->type
!= LXC_NET_VETH
)
3270 goto clear_ifindices
;
3272 /* Explicitly delete host veth device to prevent lingering
3273 * devices. We had issues in LXD around this.
3275 if (netdev
->priv
.veth_attr
.pair
[0] != '\0')
3276 hostveth
= netdev
->priv
.veth_attr
.pair
;
3278 hostveth
= netdev
->priv
.veth_attr
.veth1
;
3279 if (hostveth
[0] == '\0')
3280 goto clear_ifindices
;
3282 ret
= lxc_netdev_delete_by_name(hostveth
);
3284 WARN("Failed to remove interface \"%s\" from \"%s\"",
3285 hostveth
, netdev
->link
);
3286 goto clear_ifindices
;
3288 INFO("Removed interface \"%s\" from \"%s\"", hostveth
, netdev
->link
);
3290 if (netdev
->link
[0] == '\0' || !is_ovs_bridge(netdev
->link
)) {
3291 netdev
->priv
.veth_attr
.veth1
[0] = '\0';
3292 netdev
->ifindex
= 0;
3293 netdev
->priv
.veth_attr
.ifindex
= 0;
3294 goto clear_ifindices
;
3297 /* Delete the openvswitch port. */
3298 ret
= lxc_ovs_delete_port(netdev
->link
, hostveth
);
3300 WARN("Failed to remove port \"%s\" from openvswitch "
3301 "bridge \"%s\"", hostveth
, netdev
->link
);
3303 INFO("Removed port \"%s\" from openvswitch bridge \"%s\"",
3304 hostveth
, netdev
->link
);
3307 /* We need to clear any ifindices we recorded so liblxc won't
3308 * have cached stale data which would cause it to fail on reboot
3309 * we're we don't re-read the on-disk config file.
3311 netdev
->ifindex
= 0;
3312 if (netdev
->type
== LXC_NET_PHYS
) {
3313 netdev
->priv
.phys_attr
.ifindex
= 0;
3314 } else if (netdev
->type
== LXC_NET_VETH
) {
3315 netdev
->priv
.veth_attr
.veth1
[0] = '\0';
3316 netdev
->priv
.veth_attr
.ifindex
= 0;
3323 int lxc_requests_empty_network(struct lxc_handler
*handler
)
3325 struct lxc_list
*network
= &handler
->conf
->network
;
3326 struct lxc_list
*iterator
;
3327 bool found_none
= false, found_nic
= false;
3329 if (lxc_list_empty(network
))
3332 lxc_list_for_each(iterator
, network
) {
3333 struct lxc_netdev
*netdev
= iterator
->elem
;
3335 if (netdev
->type
== LXC_NET_NONE
)
3340 if (found_none
&& !found_nic
)
3345 /* try to move physical nics to the init netns */
3346 int lxc_restore_phys_nics_to_netns(struct lxc_handler
*handler
)
3350 char ifname
[IFNAMSIZ
];
3351 struct lxc_list
*iterator
;
3352 int netnsfd
= handler
->nsfd
[LXC_NS_NET
];
3353 struct lxc_conf
*conf
= handler
->conf
;
3355 /* We need CAP_NET_ADMIN in the parent namespace in order to setns() to
3356 * the parent network namespace. We won't have this capability if we are
3359 if (!handler
->am_root
)
3362 TRACE("Moving physical network devices back to parent network namespace");
3364 oldfd
= lxc_preserve_ns(handler
->monitor_pid
, "net");
3366 SYSERROR("Failed to preserve network namespace");
3370 ret
= setns(netnsfd
, CLONE_NEWNET
);
3372 SYSERROR("Failed to enter network namespace");
3377 lxc_list_for_each(iterator
, &conf
->network
) {
3378 struct lxc_netdev
*netdev
= iterator
->elem
;
3380 if (netdev
->type
!= LXC_NET_PHYS
)
3383 /* Retrieve the name of the interface in the container's network
3386 if (!if_indextoname(netdev
->ifindex
, ifname
)) {
3387 WARN("No interface corresponding to ifindex %d",
3392 ret
= lxc_netdev_move_by_index_fd(netdev
->ifindex
, oldfd
, netdev
->link
);
3394 WARN("Error moving network device \"%s\" back to "
3395 "network namespace", ifname
);
3397 TRACE("Moved network device \"%s\" back to network "
3398 "namespace", ifname
);
3401 ret
= setns(oldfd
, CLONE_NEWNET
);
3404 SYSERROR("Failed to enter network namespace");
3411 static int setup_hw_addr(char *hwaddr
, const char *ifname
)
3413 struct sockaddr sockaddr
;
3417 ret
= lxc_convert_mac(hwaddr
, &sockaddr
);
3420 SYSERROR("Mac address \"%s\" conversion failed", hwaddr
);
3424 memcpy(ifr
.ifr_name
, ifname
, IFNAMSIZ
);
3425 ifr
.ifr_name
[IFNAMSIZ
-1] = '\0';
3426 memcpy((char *) &ifr
.ifr_hwaddr
, (char *) &sockaddr
, sizeof(sockaddr
));
3428 fd
= socket(AF_INET
, SOCK_DGRAM
| SOCK_CLOEXEC
, 0);
3432 ret
= ioctl(fd
, SIOCSIFHWADDR
, &ifr
);
3434 SYSERROR("Failed to perform ioctl");
3438 DEBUG("Mac address \"%s\" on \"%s\" has been setup", hwaddr
,
3444 static int setup_ipv4_addr(struct lxc_list
*ip
, int ifindex
)
3446 struct lxc_list
*iterator
;
3449 lxc_list_for_each(iterator
, ip
) {
3450 struct lxc_inetdev
*inetdev
= iterator
->elem
;
3452 err
= lxc_ipv4_addr_add(ifindex
, &inetdev
->addr
,
3453 &inetdev
->bcast
, inetdev
->prefix
);
3456 SYSERROR("Failed to setup ipv4 address for network device "
3457 "with ifindex %d", ifindex
);
3465 static int setup_ipv6_addr(struct lxc_list
*ip
, int ifindex
)
3467 struct lxc_list
*iterator
;
3470 lxc_list_for_each(iterator
, ip
) {
3471 struct lxc_inet6dev
*inet6dev
= iterator
->elem
;
3473 err
= lxc_ipv6_addr_add(ifindex
, &inet6dev
->addr
,
3474 &inet6dev
->mcast
, &inet6dev
->acast
,
3478 SYSERROR("Failed to setup ipv6 address for network device "
3479 "with ifindex %d", ifindex
);
3487 static int lxc_setup_netdev_in_child_namespaces(struct lxc_netdev
*netdev
)
3489 char ifname
[IFNAMSIZ
];
3491 char *current_ifname
= ifname
;
3492 char bufinet4
[INET_ADDRSTRLEN
], bufinet6
[INET6_ADDRSTRLEN
];
3494 /* empty network namespace */
3495 if (!netdev
->ifindex
) {
3496 if (netdev
->flags
& IFF_UP
) {
3497 err
= lxc_netdev_up("lo");
3500 SYSERROR("Failed to set the loopback network device up");
3505 if (netdev
->type
== LXC_NET_EMPTY
)
3508 if (netdev
->type
== LXC_NET_NONE
)
3511 netdev
->ifindex
= if_nametoindex(netdev
->created_name
);
3512 if (!netdev
->ifindex
)
3513 SYSERROR("Failed to retrieve ifindex for network device with name %s",
3514 netdev
->name
?: "(null)");
3517 /* get the new ifindex in case of physical netdev */
3518 if (netdev
->type
== LXC_NET_PHYS
) {
3519 netdev
->ifindex
= if_nametoindex(netdev
->link
);
3520 if (!netdev
->ifindex
) {
3521 ERROR("Failed to get ifindex for network device \"%s\"",
3527 /* retrieve the name of the interface */
3528 if (!if_indextoname(netdev
->ifindex
, current_ifname
)) {
3529 SYSERROR("Failed to retrieve name for network device with ifindex %d",
3534 /* Default: let the system choose an interface name.
3535 * When the IFLA_IFNAME attribute is passed something like "<prefix>%d"
3536 * netlink will replace the format specifier with an appropriate index.
3538 if (netdev
->name
[0] == '\0') {
3539 if (netdev
->type
== LXC_NET_PHYS
)
3540 (void)strlcpy(netdev
->name
, netdev
->link
, IFNAMSIZ
);
3542 (void)strlcpy(netdev
->name
, "eth%d", IFNAMSIZ
);
3545 /* rename the interface name */
3546 if (strcmp(current_ifname
, netdev
->name
) != 0) {
3547 err
= lxc_netdev_rename_by_name(current_ifname
, netdev
->name
);
3550 SYSERROR("Failed to rename network device \"%s\" to \"%s\"",
3551 current_ifname
, netdev
->name
);
3555 TRACE("Renamed network device from \"%s\" to \"%s\"",
3556 current_ifname
, netdev
->name
);
3559 /* Re-read the name of the interface because its name has changed
3560 * and would be automatically allocated by the system
3562 if (!if_indextoname(netdev
->ifindex
, current_ifname
)) {
3563 ERROR("Failed get name for network device with ifindex %d",
3568 /* Now update the recorded name of the network device to reflect the
3569 * name of the network device in the child's network namespace. We will
3570 * later on send this information back to the parent.
3572 (void)strlcpy(netdev
->name
, current_ifname
, IFNAMSIZ
);
3574 /* set a mac address */
3575 if (netdev
->hwaddr
) {
3576 if (setup_hw_addr(netdev
->hwaddr
, current_ifname
)) {
3577 ERROR("Failed to setup hw address for network device \"%s\"",
3583 /* setup ipv4 addresses on the interface */
3584 if (setup_ipv4_addr(&netdev
->ipv4
, netdev
->ifindex
)) {
3585 ERROR("Failed to setup ip addresses for network device \"%s\"",
3590 /* setup ipv6 addresses on the interface */
3591 if (setup_ipv6_addr(&netdev
->ipv6
, netdev
->ifindex
)) {
3592 ERROR("Failed to setup ipv6 addresses for network device \"%s\"",
3597 /* set the network device up */
3598 if (netdev
->flags
& IFF_UP
) {
3599 err
= lxc_netdev_up(current_ifname
);
3602 SYSERROR("Failed to set network device \"%s\" up",
3607 /* the network is up, make the loopback up too */
3608 err
= lxc_netdev_up("lo");
3611 SYSERROR("Failed to set the loopback network device up");
3616 /* setup ipv4 gateway on the interface */
3617 if (netdev
->ipv4_gateway
|| netdev
->ipv4_gateway_dev
) {
3618 if (!(netdev
->flags
& IFF_UP
)) {
3619 ERROR("Cannot add ipv4 gateway for network device "
3620 "\"%s\" when not bringing up the interface", current_ifname
);
3624 if (lxc_list_empty(&netdev
->ipv4
)) {
3625 ERROR("Cannot add ipv4 gateway for network device "
3626 "\"%s\" when not assigning an address", current_ifname
);
3630 /* Setup device route if ipv4_gateway_dev is enabled */
3631 if (netdev
->ipv4_gateway_dev
) {
3632 err
= lxc_ipv4_gateway_add(netdev
->ifindex
, NULL
);
3634 SYSERROR("Failed to setup ipv4 gateway to network device \"%s\"",
3636 return minus_one_set_errno(-err
);
3639 /* Check the gateway address is valid */
3640 if (!inet_ntop(AF_INET
, netdev
->ipv4_gateway
, bufinet4
, sizeof(bufinet4
)))
3641 return minus_one_set_errno(errno
);
3643 /* Try adding a default route to the gateway address */
3644 err
= lxc_ipv4_gateway_add(netdev
->ifindex
, netdev
->ipv4_gateway
);
3646 /* If adding the default route fails, this could be because the
3647 * gateway address is in a different subnet to the container's address.
3648 * To work around this, we try adding a static device route to the
3649 * gateway address first, and then try again.
3651 err
= lxc_ipv4_dest_add(netdev
->ifindex
, netdev
->ipv4_gateway
, 32);
3654 SYSERROR("Failed to add ipv4 dest \"%s\" for network device \"%s\"",
3655 bufinet4
, current_ifname
);
3659 err
= lxc_ipv4_gateway_add(netdev
->ifindex
, netdev
->ipv4_gateway
);
3662 SYSERROR("Failed to setup ipv4 gateway \"%s\" for network device \"%s\"",
3663 bufinet4
, current_ifname
);
3670 /* setup ipv6 gateway on the interface */
3671 if (netdev
->ipv6_gateway
|| netdev
->ipv6_gateway_dev
) {
3672 if (!(netdev
->flags
& IFF_UP
)) {
3673 ERROR("Cannot add ipv6 gateway for network device \"%s\" when not bringing up the interface",
3678 if (lxc_list_empty(&netdev
->ipv6
) && !IN6_IS_ADDR_LINKLOCAL(netdev
->ipv6_gateway
)) {
3679 ERROR("Cannot add ipv6 gateway for network device \"%s\" when not assigning an address",
3684 /* Setup device route if ipv6_gateway_dev is enabled */
3685 if (netdev
->ipv6_gateway_dev
) {
3686 err
= lxc_ipv6_gateway_add(netdev
->ifindex
, NULL
);
3688 SYSERROR("Failed to setup ipv6 gateway to network device \"%s\"",
3690 return minus_one_set_errno(-err
);
3693 /* Check the gateway address is valid */
3694 if (!inet_ntop(AF_INET6
, netdev
->ipv6_gateway
, bufinet6
, sizeof(bufinet6
)))
3695 return minus_one_set_errno(errno
);
3697 /* Try adding a default route to the gateway address */
3698 err
= lxc_ipv6_gateway_add(netdev
->ifindex
, netdev
->ipv6_gateway
);
3700 /* If adding the default route fails, this could be because the
3701 * gateway address is in a different subnet to the container's address.
3702 * To work around this, we try adding a static device route to the
3703 * gateway address first, and then try again.
3705 err
= lxc_ipv6_dest_add(netdev
->ifindex
, netdev
->ipv6_gateway
, 128);
3708 SYSERROR("Failed to add ipv6 dest \"%s\" for network device \"%s\"",
3709 bufinet6
, current_ifname
);
3713 err
= lxc_ipv6_gateway_add(netdev
->ifindex
, netdev
->ipv6_gateway
);
3716 SYSERROR("Failed to setup ipv6 gateway \"%s\" for network device \"%s\"",
3717 bufinet6
, current_ifname
);
3724 DEBUG("Network device \"%s\" has been setup", current_ifname
);
3729 int lxc_setup_network_in_child_namespaces(const struct lxc_conf
*conf
,
3730 struct lxc_list
*network
)
3732 struct lxc_list
*iterator
;
3734 lxc_list_for_each(iterator
, network
) {
3735 struct lxc_netdev
*netdev
= iterator
->elem
;
3737 if (lxc_setup_netdev_in_child_namespaces(netdev
)) {
3738 ERROR("Failed to setup netdev");
3743 if (!lxc_list_empty(network
))
3744 INFO("Network has been setup");
3749 int lxc_network_send_veth_names_to_child(struct lxc_handler
*handler
)
3751 struct lxc_list
*iterator
;
3752 struct lxc_list
*network
= &handler
->conf
->network
;
3753 int data_sock
= handler
->data_sock
[0];
3755 lxc_list_for_each(iterator
, network
) {
3757 struct lxc_netdev
*netdev
= iterator
->elem
;
3759 if (netdev
->type
!= LXC_NET_VETH
)
3762 ret
= lxc_send_nointr(data_sock
, netdev
->name
, IFNAMSIZ
, MSG_NOSIGNAL
);
3766 ret
= lxc_send_nointr(data_sock
, netdev
->created_name
, IFNAMSIZ
, MSG_NOSIGNAL
);
3770 TRACE("Sent network device name \"%s\" to child", netdev
->created_name
);
3776 int lxc_network_recv_veth_names_from_parent(struct lxc_handler
*handler
)
3778 struct lxc_list
*iterator
;
3779 struct lxc_list
*network
= &handler
->conf
->network
;
3780 int data_sock
= handler
->data_sock
[1];
3782 lxc_list_for_each(iterator
, network
) {
3784 struct lxc_netdev
*netdev
= iterator
->elem
;
3786 if (netdev
->type
!= LXC_NET_VETH
)
3789 ret
= lxc_recv_nointr(data_sock
, netdev
->name
, IFNAMSIZ
, 0);
3793 ret
= lxc_recv_nointr(data_sock
, netdev
->created_name
, IFNAMSIZ
, 0);
3796 TRACE("Received network device name \"%s\" from parent", netdev
->created_name
);
3802 int lxc_network_send_name_and_ifindex_to_parent(struct lxc_handler
*handler
)
3804 struct lxc_list
*iterator
, *network
;
3805 int data_sock
= handler
->data_sock
[0];
3807 if (!handler
->am_root
)
3810 network
= &handler
->conf
->network
;
3811 lxc_list_for_each(iterator
, network
) {
3813 struct lxc_netdev
*netdev
= iterator
->elem
;
3815 /* Send network device name in the child's namespace to parent. */
3816 ret
= lxc_send_nointr(data_sock
, netdev
->name
, IFNAMSIZ
, MSG_NOSIGNAL
);
3820 /* Send network device ifindex in the child's namespace to
3823 ret
= lxc_send_nointr(data_sock
, &netdev
->ifindex
, sizeof(netdev
->ifindex
), MSG_NOSIGNAL
);
3828 if (!lxc_list_empty(network
))
3829 TRACE("Sent network device names and ifindices to parent");
3834 int lxc_network_recv_name_and_ifindex_from_child(struct lxc_handler
*handler
)
3836 struct lxc_list
*iterator
, *network
;
3837 int data_sock
= handler
->data_sock
[1];
3839 if (!handler
->am_root
)
3842 network
= &handler
->conf
->network
;
3843 lxc_list_for_each(iterator
, network
) {
3845 struct lxc_netdev
*netdev
= iterator
->elem
;
3847 /* Receive network device name in the child's namespace to
3850 ret
= lxc_recv_nointr(data_sock
, netdev
->name
, IFNAMSIZ
, 0);
3854 /* Receive network device ifindex in the child's namespace to
3857 ret
= lxc_recv_nointr(data_sock
, &netdev
->ifindex
, sizeof(netdev
->ifindex
), 0);
3865 void lxc_delete_network(struct lxc_handler
*handler
)
3869 if (handler
->am_root
)
3870 bret
= lxc_delete_network_priv(handler
);
3872 bret
= lxc_delete_network_unpriv(handler
);
3874 DEBUG("Failed to delete network devices");
3876 DEBUG("Deleted network devices");
3879 int lxc_netns_set_nsid(int fd
)
3882 char buf
[NLMSG_ALIGN(sizeof(struct nlmsghdr
)) +
3883 NLMSG_ALIGN(sizeof(struct rtgenmsg
)) +
3885 struct nl_handler nlh
;
3886 struct nlmsghdr
*hdr
;
3887 struct rtgenmsg
*msg
;
3889 const __s32 ns_id
= -1;
3890 const __u32 netns_fd
= fd
;
3892 ret
= netlink_open(&nlh
, NETLINK_ROUTE
);
3896 memset(buf
, 0, sizeof(buf
));
3898 #pragma GCC diagnostic push
3899 #pragma GCC diagnostic ignored "-Wcast-align"
3900 hdr
= (struct nlmsghdr
*)buf
;
3901 msg
= (struct rtgenmsg
*)NLMSG_DATA(hdr
);
3902 #pragma GCC diagnostic pop
3904 hdr
->nlmsg_len
= NLMSG_LENGTH(sizeof(*msg
));
3905 hdr
->nlmsg_type
= RTM_NEWNSID
;
3906 hdr
->nlmsg_flags
= NLM_F_REQUEST
| NLM_F_ACK
;
3908 hdr
->nlmsg_seq
= RTM_NEWNSID
;
3909 msg
->rtgen_family
= AF_UNSPEC
;
3911 ret
= addattr(hdr
, 1024, __LXC_NETNSA_FD
, &netns_fd
, sizeof(netns_fd
));
3915 ret
= addattr(hdr
, 1024, __LXC_NETNSA_NSID
, &ns_id
, sizeof(ns_id
));
3919 ret
= __netlink_transaction(&nlh
, hdr
, hdr
);
3922 saved_errno
= errno
;
3923 netlink_close(&nlh
);
3924 errno
= saved_errno
;
3929 static int parse_rtattr(struct rtattr
*tb
[], int max
, struct rtattr
*rta
, int len
)
3932 memset(tb
, 0, sizeof(struct rtattr
*) * (max
+ 1));
3934 while (RTA_OK(rta
, len
)) {
3935 unsigned short type
= rta
->rta_type
;
3937 if ((type
<= max
) && (!tb
[type
]))
3940 #pragma GCC diagnostic push
3941 #pragma GCC diagnostic ignored "-Wcast-align"
3942 rta
= RTA_NEXT(rta
, len
);
3943 #pragma GCC diagnostic pop
3949 static inline __s32
rta_getattr_s32(const struct rtattr
*rta
)
3951 return *(__s32
*)RTA_DATA(rta
);
3955 #define NETNS_RTA(r) \
3956 ((struct rtattr *)(((char *)(r)) + NLMSG_ALIGN(sizeof(struct rtgenmsg))))
3959 int lxc_netns_get_nsid(int fd
)
3963 char buf
[NLMSG_ALIGN(sizeof(struct nlmsghdr
)) +
3964 NLMSG_ALIGN(sizeof(struct rtgenmsg
)) +
3966 struct rtattr
*tb
[__LXC_NETNSA_MAX
+ 1];
3967 struct nl_handler nlh
;
3968 struct nlmsghdr
*hdr
;
3969 struct rtgenmsg
*msg
;
3971 __u32 netns_fd
= fd
;
3973 ret
= netlink_open(&nlh
, NETLINK_ROUTE
);
3977 memset(buf
, 0, sizeof(buf
));
3979 #pragma GCC diagnostic push
3980 #pragma GCC diagnostic ignored "-Wcast-align"
3981 hdr
= (struct nlmsghdr
*)buf
;
3982 msg
= (struct rtgenmsg
*)NLMSG_DATA(hdr
);
3983 #pragma GCC diagnostic pop
3985 hdr
->nlmsg_len
= NLMSG_LENGTH(sizeof(*msg
));
3986 hdr
->nlmsg_type
= RTM_GETNSID
;
3987 hdr
->nlmsg_flags
= NLM_F_REQUEST
| NLM_F_ACK
;
3989 hdr
->nlmsg_seq
= RTM_GETNSID
;
3990 msg
->rtgen_family
= AF_UNSPEC
;
3992 ret
= addattr(hdr
, 1024, __LXC_NETNSA_FD
, &netns_fd
, sizeof(netns_fd
));
3994 ret
= __netlink_transaction(&nlh
, hdr
, hdr
);
3996 saved_errno
= errno
;
3997 netlink_close(&nlh
);
3998 errno
= saved_errno
;
4003 msg
= NLMSG_DATA(hdr
);
4004 len
= hdr
->nlmsg_len
- NLMSG_SPACE(sizeof(*msg
));
4008 #pragma GCC diagnostic push
4009 #pragma GCC diagnostic ignored "-Wcast-align"
4010 parse_rtattr(tb
, __LXC_NETNSA_MAX
, NETNS_RTA(msg
), len
);
4011 if (tb
[__LXC_NETNSA_NSID
])
4012 return rta_getattr_s32(tb
[__LXC_NETNSA_NSID
]);
4013 #pragma GCC diagnostic pop
4018 int lxc_create_network(struct lxc_handler
*handler
)
4023 * Find gateway addresses from the link device, which is no longer
4024 * accessible inside the container. Do this before creating network
4025 * interfaces, since goto out_delete_net does not work before
4028 ret
= lxc_find_gateway_addresses(handler
);
4030 ERROR("Failed to find gateway addresses");
4034 if (handler
->am_root
) {
4035 ret
= lxc_create_network_priv(handler
);
4039 return lxc_network_move_created_netdev_priv(handler
);
4042 return lxc_create_network_unpriv(handler
);