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
)
499 unsigned int mtu
= 0;
501 if (netdev
->link
[0] == '\0') {
502 ERROR("No link for ipvlan network device specified");
506 err
= snprintf(peer
, sizeof(peer
), "ipXXXXXX");
507 if (err
< 0 || (size_t)err
>= sizeof(peer
))
510 if (!lxc_mkifname(peer
))
513 err
= lxc_ipvlan_create(netdev
->link
, peer
, netdev
->priv
.ipvlan_attr
.mode
,
514 netdev
->priv
.ipvlan_attr
.isolation
);
516 SYSERROR("Failed to create ipvlan interface \"%s\" on \"%s\"",
521 strlcpy(netdev
->created_name
, peer
, IFNAMSIZ
);
523 netdev
->ifindex
= if_nametoindex(peer
);
524 if (!netdev
->ifindex
) {
525 ERROR("Failed to retrieve ifindex for \"%s\"", peer
);
530 err
= lxc_safe_uint(netdev
->mtu
, &mtu
);
533 SYSERROR("Failed to parse mtu \"%s\" for interface \"%s\"",
538 err
= lxc_netdev_set_mtu(peer
, mtu
);
541 SYSERROR("Failed to set mtu \"%s\" for interface \"%s\"",
547 if (netdev
->upscript
) {
554 err
= run_script_argv(handler
->name
, handler
->conf
->hooks_version
,
555 "net", netdev
->upscript
, "up", argv
);
560 DEBUG("Instantiated ipvlan \"%s\" with ifindex is %d and mode %d", peer
,
561 netdev
->ifindex
, netdev
->priv
.macvlan_attr
.mode
);
566 lxc_netdev_delete_by_name(peer
);
570 static int instantiate_vlan(struct lxc_handler
*handler
, struct lxc_netdev
*netdev
)
574 static uint16_t vlan_cntr
= 0;
575 unsigned int mtu
= 0;
577 if (netdev
->link
[0] == '\0') {
578 ERROR("No link for vlan network device specified");
582 err
= snprintf(peer
, sizeof(peer
), "vlan%d-%d",
583 netdev
->priv
.vlan_attr
.vid
, vlan_cntr
++);
584 if (err
< 0 || (size_t)err
>= sizeof(peer
))
587 err
= lxc_vlan_create(netdev
->link
, peer
, netdev
->priv
.vlan_attr
.vid
);
590 SYSERROR("Failed to create vlan interface \"%s\" on \"%s\"",
595 strlcpy(netdev
->created_name
, peer
, IFNAMSIZ
);
597 netdev
->ifindex
= if_nametoindex(peer
);
598 if (!netdev
->ifindex
) {
599 ERROR("Failed to retrieve ifindex for \"%s\"", peer
);
604 err
= lxc_safe_uint(netdev
->mtu
, &mtu
);
607 SYSERROR("Failed to parse mtu \"%s\" for interface \"%s\"",
612 err
= lxc_netdev_set_mtu(peer
, mtu
);
615 SYSERROR("Failed to set mtu \"%s\" for interface \"%s\"",
621 if (netdev
->upscript
) {
628 err
= run_script_argv(handler
->name
, handler
->conf
->hooks_version
,
629 "net", netdev
->upscript
, "up", argv
);
635 DEBUG("Instantiated vlan \"%s\" with ifindex is \"%d\"", peer
,
641 lxc_netdev_delete_by_name(peer
);
645 static int instantiate_phys(struct lxc_handler
*handler
, struct lxc_netdev
*netdev
)
647 int err
, mtu_orig
= 0;
648 unsigned int mtu
= 0;
650 if (netdev
->link
[0] == '\0') {
651 ERROR("No link for physical interface specified");
656 * Note that we're retrieving the container's ifindex in the host's
657 * network namespace because we need it to move the device from the
658 * host's network namespace to the container's network namespace later
660 * Note that netdev->link will contain the name of the physical network
661 * device in the host's namespace.
663 netdev
->ifindex
= if_nametoindex(netdev
->link
);
664 if (!netdev
->ifindex
) {
665 ERROR("Failed to retrieve ifindex for \"%s\"", netdev
->link
);
669 strlcpy(netdev
->created_name
, netdev
->link
, IFNAMSIZ
);
672 * Store the ifindex of the host's network device in the host's
675 netdev
->priv
.phys_attr
.ifindex
= netdev
->ifindex
;
678 * Get original device MTU setting and store for restoration after
679 * container shutdown.
681 mtu_orig
= netdev_get_mtu(netdev
->ifindex
);
683 SYSERROR("Failed to get original mtu for interface \"%s\"", netdev
->link
);
684 return minus_one_set_errno(-mtu_orig
);
687 netdev
->priv
.phys_attr
.mtu
= mtu_orig
;
690 err
= lxc_safe_uint(netdev
->mtu
, &mtu
);
693 SYSERROR("Failed to parse mtu \"%s\" for interface \"%s\"",
694 netdev
->mtu
, netdev
->link
);
698 err
= lxc_netdev_set_mtu(netdev
->link
, mtu
);
701 SYSERROR("Failed to set mtu \"%s\" for interface \"%s\"",
702 netdev
->mtu
, netdev
->link
);
707 if (netdev
->upscript
) {
714 err
= run_script_argv(handler
->name
, handler
->conf
->hooks_version
,
715 "net", netdev
->upscript
, "up", argv
);
721 DEBUG("Instantiated phys \"%s\" with ifindex is \"%d\"", netdev
->link
,
727 static int instantiate_empty(struct lxc_handler
*handler
, struct lxc_netdev
*netdev
)
736 if (!netdev
->upscript
)
739 ret
= run_script_argv(handler
->name
, handler
->conf
->hooks_version
,
740 "net", netdev
->upscript
, "up", argv
);
747 static int instantiate_none(struct lxc_handler
*handler
, struct lxc_netdev
*netdev
)
753 static instantiate_cb netdev_conf
[LXC_NET_MAXCONFTYPE
+ 1] = {
754 [LXC_NET_VETH
] = instantiate_veth
,
755 [LXC_NET_MACVLAN
] = instantiate_macvlan
,
756 [LXC_NET_IPVLAN
] = instantiate_ipvlan
,
757 [LXC_NET_VLAN
] = instantiate_vlan
,
758 [LXC_NET_PHYS
] = instantiate_phys
,
759 [LXC_NET_EMPTY
] = instantiate_empty
,
760 [LXC_NET_NONE
] = instantiate_none
,
763 static int shutdown_veth(struct lxc_handler
*handler
, struct lxc_netdev
*netdev
)
773 if (!netdev
->downscript
)
776 if (netdev
->priv
.veth_attr
.pair
[0] != '\0')
777 argv
[2] = netdev
->priv
.veth_attr
.pair
;
779 argv
[2] = netdev
->priv
.veth_attr
.veth1
;
781 ret
= run_script_argv(handler
->name
,
782 handler
->conf
->hooks_version
, "net",
783 netdev
->downscript
, "down", argv
);
790 static int shutdown_macvlan(struct lxc_handler
*handler
, struct lxc_netdev
*netdev
)
799 if (!netdev
->downscript
)
802 ret
= run_script_argv(handler
->name
, handler
->conf
->hooks_version
,
803 "net", netdev
->downscript
, "down", argv
);
810 static int shutdown_ipvlan(struct lxc_handler
*handler
, struct lxc_netdev
*netdev
)
819 if (!netdev
->downscript
)
822 ret
= run_script_argv(handler
->name
, handler
->conf
->hooks_version
,
823 "net", netdev
->downscript
, "down", argv
);
830 static int shutdown_vlan(struct lxc_handler
*handler
, struct lxc_netdev
*netdev
)
839 if (!netdev
->downscript
)
842 ret
= run_script_argv(handler
->name
, handler
->conf
->hooks_version
,
843 "net", netdev
->downscript
, "down", argv
);
850 static int shutdown_phys(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_empty(struct lxc_handler
*handler
, struct lxc_netdev
*netdev
)
878 if (!netdev
->downscript
)
881 ret
= run_script_argv(handler
->name
, handler
->conf
->hooks_version
,
882 "net", netdev
->downscript
, "down", argv
);
889 static int shutdown_none(struct lxc_handler
*handler
, struct lxc_netdev
*netdev
)
894 static instantiate_cb netdev_deconf
[LXC_NET_MAXCONFTYPE
+ 1] = {
895 [LXC_NET_VETH
] = shutdown_veth
,
896 [LXC_NET_MACVLAN
] = shutdown_macvlan
,
897 [LXC_NET_IPVLAN
] = shutdown_ipvlan
,
898 [LXC_NET_VLAN
] = shutdown_vlan
,
899 [LXC_NET_PHYS
] = shutdown_phys
,
900 [LXC_NET_EMPTY
] = shutdown_empty
,
901 [LXC_NET_NONE
] = shutdown_none
,
904 static int lxc_netdev_move_by_index_fd(int ifindex
, int fd
, const char *ifname
)
907 struct nl_handler nlh
;
908 struct ifinfomsg
*ifi
;
909 struct nlmsg
*nlmsg
= NULL
;
911 err
= netlink_open(&nlh
, NETLINK_ROUTE
);
916 nlmsg
= nlmsg_alloc(NLMSG_GOOD_SIZE
);
920 nlmsg
->nlmsghdr
->nlmsg_flags
= NLM_F_REQUEST
| NLM_F_ACK
;
921 nlmsg
->nlmsghdr
->nlmsg_type
= RTM_NEWLINK
;
923 ifi
= nlmsg_reserve(nlmsg
, sizeof(struct ifinfomsg
));
926 ifi
->ifi_family
= AF_UNSPEC
;
927 ifi
->ifi_index
= ifindex
;
929 if (nla_put_u32(nlmsg
, IFLA_NET_NS_FD
, fd
))
932 if (ifname
!= NULL
) {
933 if (nla_put_string(nlmsg
, IFLA_IFNAME
, ifname
))
937 err
= netlink_transaction(&nlh
, nlmsg
, nlmsg
);
944 int lxc_netdev_move_by_index(int ifindex
, pid_t pid
, const char *ifname
)
947 struct nl_handler nlh
;
948 struct ifinfomsg
*ifi
;
949 struct nlmsg
*nlmsg
= NULL
;
951 err
= netlink_open(&nlh
, NETLINK_ROUTE
);
956 nlmsg
= nlmsg_alloc(NLMSG_GOOD_SIZE
);
960 nlmsg
->nlmsghdr
->nlmsg_flags
= NLM_F_REQUEST
| NLM_F_ACK
;
961 nlmsg
->nlmsghdr
->nlmsg_type
= RTM_NEWLINK
;
963 ifi
= nlmsg_reserve(nlmsg
, sizeof(struct ifinfomsg
));
966 ifi
->ifi_family
= AF_UNSPEC
;
967 ifi
->ifi_index
= ifindex
;
969 if (nla_put_u32(nlmsg
, IFLA_NET_NS_PID
, pid
))
972 if (ifname
!= NULL
) {
973 if (nla_put_string(nlmsg
, IFLA_IFNAME
, ifname
))
977 err
= netlink_transaction(&nlh
, nlmsg
, nlmsg
);
984 /* If we are asked to move a wireless interface, then we must actually move its
985 * phyN device. Detect that condition and return the physname here. The physname
986 * will be passed to lxc_netdev_move_wlan() which will free it when done.
988 #define PHYSNAME "/sys/class/net/%s/phy80211/name"
989 static char *is_wlan(const char *ifname
)
991 __do_free
char *path
= NULL
;
996 char *physname
= NULL
;
998 len
= strlen(ifname
) + strlen(PHYSNAME
) - 1;
999 path
= must_realloc(NULL
, len
+ 1);
1000 ret
= snprintf(path
, len
, PHYSNAME
, ifname
);
1001 if (ret
< 0 || (size_t)ret
>= len
)
1004 f
= fopen(path
, "r");
1008 /* Feh - sb.st_size is always 4096. */
1009 fseek(f
, 0, SEEK_END
);
1011 fseek(f
, 0, SEEK_SET
);
1017 physname
= malloc(physlen
+ 1);
1023 memset(physname
, 0, physlen
+ 1);
1024 ret
= fread(physname
, 1, physlen
, f
);
1029 for (i
= 0; i
< physlen
; i
++) {
1030 if (physname
[i
] == '\n')
1033 if (physname
[i
] == '\0')
1044 static int lxc_netdev_rename_by_name_in_netns(pid_t pid
, const char *old
,
1054 return wait_for_pid(fpid
);
1056 if (!switch_to_ns(pid
, "net"))
1059 _exit(lxc_netdev_rename_by_name(old
, new));
1062 static int lxc_netdev_move_wlan(char *physname
, const char *ifname
, pid_t pid
,
1063 const char *newname
)
1069 /* Move phyN into the container. TODO - do this using netlink.
1070 * However, IIUC this involves a bit more complicated work to talk to
1071 * the 80211 module, so for now just call out to iw.
1073 cmd
= on_path("iw", NULL
);
1084 sprintf(pidstr
, "%d", pid
);
1085 execlp("iw", "iw", "phy", physname
, "set", "netns", pidstr
,
1087 _exit(EXIT_FAILURE
);
1090 if (wait_for_pid(fpid
))
1095 err
= lxc_netdev_rename_by_name_in_netns(pid
, ifname
, newname
);
1102 int lxc_netdev_move_by_name(const char *ifname
, pid_t pid
, const char* newname
)
1110 index
= if_nametoindex(ifname
);
1114 physname
= is_wlan(ifname
);
1116 return lxc_netdev_move_wlan(physname
, ifname
, pid
, newname
);
1118 return lxc_netdev_move_by_index(index
, pid
, newname
);
1121 int lxc_netdev_delete_by_index(int ifindex
)
1124 struct ifinfomsg
*ifi
;
1125 struct nl_handler nlh
;
1126 struct nlmsg
*answer
= NULL
, *nlmsg
= NULL
;
1128 err
= netlink_open(&nlh
, NETLINK_ROUTE
);
1133 nlmsg
= nlmsg_alloc(NLMSG_GOOD_SIZE
);
1137 answer
= nlmsg_alloc_reserve(NLMSG_GOOD_SIZE
);
1141 nlmsg
->nlmsghdr
->nlmsg_flags
= NLM_F_ACK
| NLM_F_REQUEST
;
1142 nlmsg
->nlmsghdr
->nlmsg_type
= RTM_DELLINK
;
1144 ifi
= nlmsg_reserve(nlmsg
, sizeof(struct ifinfomsg
));
1147 ifi
->ifi_family
= AF_UNSPEC
;
1148 ifi
->ifi_index
= ifindex
;
1150 err
= netlink_transaction(&nlh
, nlmsg
, answer
);
1152 netlink_close(&nlh
);
1158 int lxc_netdev_delete_by_name(const char *name
)
1162 index
= if_nametoindex(name
);
1166 return lxc_netdev_delete_by_index(index
);
1169 int lxc_netdev_rename_by_index(int ifindex
, const char *newname
)
1172 struct ifinfomsg
*ifi
;
1173 struct nl_handler nlh
;
1174 struct nlmsg
*answer
= NULL
, *nlmsg
= NULL
;
1176 err
= netlink_open(&nlh
, NETLINK_ROUTE
);
1180 len
= strlen(newname
);
1181 if (len
== 1 || len
>= IFNAMSIZ
) {
1187 nlmsg
= nlmsg_alloc(NLMSG_GOOD_SIZE
);
1191 answer
= nlmsg_alloc_reserve(NLMSG_GOOD_SIZE
);
1195 nlmsg
->nlmsghdr
->nlmsg_flags
= NLM_F_ACK
| NLM_F_REQUEST
;
1196 nlmsg
->nlmsghdr
->nlmsg_type
= RTM_NEWLINK
;
1198 ifi
= nlmsg_reserve(nlmsg
, sizeof(struct ifinfomsg
));
1201 ifi
->ifi_family
= AF_UNSPEC
;
1202 ifi
->ifi_index
= ifindex
;
1204 if (nla_put_string(nlmsg
, IFLA_IFNAME
, newname
))
1207 err
= netlink_transaction(&nlh
, nlmsg
, answer
);
1209 netlink_close(&nlh
);
1215 int lxc_netdev_rename_by_name(const char *oldname
, const char *newname
)
1219 len
= strlen(oldname
);
1220 if (len
== 1 || len
>= IFNAMSIZ
)
1223 index
= if_nametoindex(oldname
);
1227 return lxc_netdev_rename_by_index(index
, newname
);
1230 int netdev_set_flag(const char *name
, int flag
)
1232 int err
, index
, len
;
1233 struct ifinfomsg
*ifi
;
1234 struct nl_handler nlh
;
1235 struct nlmsg
*answer
= NULL
, *nlmsg
= NULL
;
1237 err
= netlink_open(&nlh
, NETLINK_ROUTE
);
1243 if (len
== 1 || len
>= IFNAMSIZ
)
1247 nlmsg
= nlmsg_alloc(NLMSG_GOOD_SIZE
);
1251 answer
= nlmsg_alloc_reserve(NLMSG_GOOD_SIZE
);
1256 index
= if_nametoindex(name
);
1260 nlmsg
->nlmsghdr
->nlmsg_flags
= NLM_F_REQUEST
| NLM_F_ACK
;
1261 nlmsg
->nlmsghdr
->nlmsg_type
= RTM_NEWLINK
;
1263 ifi
= nlmsg_reserve(nlmsg
, sizeof(struct ifinfomsg
));
1268 ifi
->ifi_family
= AF_UNSPEC
;
1269 ifi
->ifi_index
= index
;
1270 ifi
->ifi_change
|= IFF_UP
;
1271 ifi
->ifi_flags
|= flag
;
1273 err
= netlink_transaction(&nlh
, nlmsg
, answer
);
1275 netlink_close(&nlh
);
1281 int netdev_get_flag(const char *name
, int *flag
)
1283 int err
, index
, len
;
1284 struct ifinfomsg
*ifi
;
1285 struct nl_handler nlh
;
1286 struct nlmsg
*answer
= NULL
, *nlmsg
= NULL
;
1291 err
= netlink_open(&nlh
, NETLINK_ROUTE
);
1297 if (len
== 1 || len
>= IFNAMSIZ
)
1301 nlmsg
= nlmsg_alloc(NLMSG_GOOD_SIZE
);
1305 answer
= nlmsg_alloc_reserve(NLMSG_GOOD_SIZE
);
1310 index
= if_nametoindex(name
);
1314 nlmsg
->nlmsghdr
->nlmsg_flags
= NLM_F_REQUEST
;
1315 nlmsg
->nlmsghdr
->nlmsg_type
= RTM_GETLINK
;
1317 ifi
= nlmsg_reserve(nlmsg
, sizeof(struct ifinfomsg
));
1322 ifi
->ifi_family
= AF_UNSPEC
;
1323 ifi
->ifi_index
= index
;
1325 err
= netlink_transaction(&nlh
, nlmsg
, answer
);
1329 ifi
= NLMSG_DATA(answer
->nlmsghdr
);
1331 *flag
= ifi
->ifi_flags
;
1333 netlink_close(&nlh
);
1340 * \brief Check a interface is up or not.
1342 * \param name: name for the interface.
1345 * 0 means interface is down.
1346 * 1 means interface is up.
1347 * Others means error happened, and ret-value is the error number.
1349 int lxc_netdev_isup(const char *name
)
1353 err
= netdev_get_flag(name
, &flag
);
1363 int netdev_get_mtu(int ifindex
)
1365 int answer_len
, err
, res
;
1366 struct nl_handler nlh
;
1367 struct ifinfomsg
*ifi
;
1368 struct nlmsghdr
*msg
;
1369 int readmore
= 0, recv_len
= 0;
1370 struct nlmsg
*answer
= NULL
, *nlmsg
= NULL
;
1372 err
= netlink_open(&nlh
, NETLINK_ROUTE
);
1377 nlmsg
= nlmsg_alloc(NLMSG_GOOD_SIZE
);
1381 answer
= nlmsg_alloc_reserve(NLMSG_GOOD_SIZE
);
1385 /* Save the answer buffer length, since it will be overwritten
1386 * on the first receive (and we might need to receive more than
1389 answer_len
= answer
->nlmsghdr
->nlmsg_len
;
1391 nlmsg
->nlmsghdr
->nlmsg_flags
= NLM_F_REQUEST
| NLM_F_DUMP
;
1392 nlmsg
->nlmsghdr
->nlmsg_type
= RTM_GETLINK
;
1394 ifi
= nlmsg_reserve(nlmsg
, sizeof(struct ifinfomsg
));
1397 ifi
->ifi_family
= AF_UNSPEC
;
1399 /* Send the request for addresses, which returns all addresses
1400 * on all interfaces. */
1401 err
= netlink_send(&nlh
, nlmsg
);
1405 #pragma GCC diagnostic push
1406 #pragma GCC diagnostic ignored "-Wcast-align"
1409 /* Restore the answer buffer length, it might have been
1410 * overwritten by a previous receive.
1412 answer
->nlmsghdr
->nlmsg_len
= answer_len
;
1414 /* Get the (next) batch of reply messages */
1415 err
= netlink_rcv(&nlh
, answer
);
1421 /* Satisfy the typing for the netlink macros */
1422 msg
= answer
->nlmsghdr
;
1424 while (NLMSG_OK(msg
, recv_len
)) {
1426 /* Stop reading if we see an error message */
1427 if (msg
->nlmsg_type
== NLMSG_ERROR
) {
1428 struct nlmsgerr
*errmsg
=
1429 (struct nlmsgerr
*)NLMSG_DATA(msg
);
1430 err
= errmsg
->error
;
1434 /* Stop reading if we see a NLMSG_DONE message */
1435 if (msg
->nlmsg_type
== NLMSG_DONE
) {
1440 ifi
= NLMSG_DATA(msg
);
1441 if (ifi
->ifi_index
== ifindex
) {
1442 struct rtattr
*rta
= IFLA_RTA(ifi
);
1444 msg
->nlmsg_len
- NLMSG_LENGTH(sizeof(*ifi
));
1446 while (RTA_OK(rta
, attr_len
)) {
1447 /* Found a local address for the
1448 * requested interface, return it.
1450 if (rta
->rta_type
== IFLA_MTU
) {
1451 memcpy(&res
, RTA_DATA(rta
),
1456 rta
= RTA_NEXT(rta
, attr_len
);
1460 /* Keep reading more data from the socket if the last
1461 * message had the NLF_F_MULTI flag set.
1463 readmore
= (msg
->nlmsg_flags
& NLM_F_MULTI
);
1465 /* Look at the next message received in this buffer. */
1466 msg
= NLMSG_NEXT(msg
, recv_len
);
1470 #pragma GCC diagnostic pop
1472 /* If we end up here, we didn't find any result, so signal an error. */
1476 netlink_close(&nlh
);
1482 int lxc_netdev_set_mtu(const char *name
, int mtu
)
1484 int err
, index
, len
;
1485 struct ifinfomsg
*ifi
;
1486 struct nl_handler nlh
;
1487 struct nlmsg
*answer
= NULL
, *nlmsg
= NULL
;
1489 err
= netlink_open(&nlh
, NETLINK_ROUTE
);
1495 if (len
== 1 || len
>= IFNAMSIZ
)
1499 nlmsg
= nlmsg_alloc(NLMSG_GOOD_SIZE
);
1503 answer
= nlmsg_alloc_reserve(NLMSG_GOOD_SIZE
);
1508 index
= if_nametoindex(name
);
1512 nlmsg
->nlmsghdr
->nlmsg_flags
= NLM_F_REQUEST
| NLM_F_ACK
;
1513 nlmsg
->nlmsghdr
->nlmsg_type
= RTM_NEWLINK
;
1515 ifi
= nlmsg_reserve(nlmsg
, sizeof(struct ifinfomsg
));
1520 ifi
->ifi_family
= AF_UNSPEC
;
1521 ifi
->ifi_index
= index
;
1523 if (nla_put_u32(nlmsg
, IFLA_MTU
, mtu
))
1526 err
= netlink_transaction(&nlh
, nlmsg
, answer
);
1528 netlink_close(&nlh
);
1534 int lxc_netdev_up(const char *name
)
1536 return netdev_set_flag(name
, IFF_UP
);
1539 int lxc_netdev_down(const char *name
)
1541 return netdev_set_flag(name
, 0);
1544 int lxc_veth_create(const char *name1
, const char *name2
)
1547 struct ifinfomsg
*ifi
;
1548 struct nl_handler nlh
;
1549 struct rtattr
*nest1
, *nest2
, *nest3
;
1550 struct nlmsg
*answer
= NULL
, *nlmsg
= NULL
;
1552 err
= netlink_open(&nlh
, NETLINK_ROUTE
);
1557 len
= strlen(name1
);
1558 if (len
== 1 || len
>= IFNAMSIZ
)
1561 len
= strlen(name2
);
1562 if (len
== 1 || len
>= IFNAMSIZ
)
1566 nlmsg
= nlmsg_alloc(NLMSG_GOOD_SIZE
);
1570 answer
= nlmsg_alloc_reserve(NLMSG_GOOD_SIZE
);
1574 nlmsg
->nlmsghdr
->nlmsg_flags
=
1575 NLM_F_REQUEST
| NLM_F_CREATE
| NLM_F_EXCL
| NLM_F_ACK
;
1576 nlmsg
->nlmsghdr
->nlmsg_type
= RTM_NEWLINK
;
1578 ifi
= nlmsg_reserve(nlmsg
, sizeof(struct ifinfomsg
));
1581 ifi
->ifi_family
= AF_UNSPEC
;
1584 nest1
= nla_begin_nested(nlmsg
, IFLA_LINKINFO
);
1588 if (nla_put_string(nlmsg
, IFLA_INFO_KIND
, "veth"))
1591 nest2
= nla_begin_nested(nlmsg
, IFLA_INFO_DATA
);
1595 nest3
= nla_begin_nested(nlmsg
, VETH_INFO_PEER
);
1599 ifi
= nlmsg_reserve(nlmsg
, sizeof(struct ifinfomsg
));
1605 if (nla_put_string(nlmsg
, IFLA_IFNAME
, name2
))
1608 nla_end_nested(nlmsg
, nest3
);
1609 nla_end_nested(nlmsg
, nest2
);
1610 nla_end_nested(nlmsg
, nest1
);
1612 if (nla_put_string(nlmsg
, IFLA_IFNAME
, name1
))
1615 err
= netlink_transaction(&nlh
, nlmsg
, answer
);
1617 netlink_close(&nlh
);
1623 /* TODO: merge with lxc_macvlan_create */
1624 int lxc_vlan_create(const char *master
, const char *name
, unsigned short vlanid
)
1626 int err
, len
, lindex
;
1627 struct ifinfomsg
*ifi
;
1628 struct nl_handler nlh
;
1629 struct rtattr
*nest
, *nest2
;
1630 struct nlmsg
*answer
= NULL
, *nlmsg
= NULL
;
1632 err
= netlink_open(&nlh
, NETLINK_ROUTE
);
1637 len
= strlen(master
);
1638 if (len
== 1 || len
>= IFNAMSIZ
)
1642 if (len
== 1 || len
>= IFNAMSIZ
)
1646 nlmsg
= nlmsg_alloc(NLMSG_GOOD_SIZE
);
1650 answer
= nlmsg_alloc_reserve(NLMSG_GOOD_SIZE
);
1655 lindex
= if_nametoindex(master
);
1659 nlmsg
->nlmsghdr
->nlmsg_flags
=
1660 NLM_F_REQUEST
| NLM_F_CREATE
| NLM_F_EXCL
| NLM_F_ACK
;
1661 nlmsg
->nlmsghdr
->nlmsg_type
= RTM_NEWLINK
;
1663 ifi
= nlmsg_reserve(nlmsg
, sizeof(struct ifinfomsg
));
1668 ifi
->ifi_family
= AF_UNSPEC
;
1670 nest
= nla_begin_nested(nlmsg
, IFLA_LINKINFO
);
1674 if (nla_put_string(nlmsg
, IFLA_INFO_KIND
, "vlan"))
1677 nest2
= nla_begin_nested(nlmsg
, IFLA_INFO_DATA
);
1681 if (nla_put_u16(nlmsg
, IFLA_VLAN_ID
, vlanid
))
1684 nla_end_nested(nlmsg
, nest2
);
1685 nla_end_nested(nlmsg
, nest
);
1687 if (nla_put_u32(nlmsg
, IFLA_LINK
, lindex
))
1690 if (nla_put_string(nlmsg
, IFLA_IFNAME
, name
))
1693 err
= netlink_transaction(&nlh
, nlmsg
, answer
);
1699 netlink_close(&nlh
);
1703 int lxc_macvlan_create(const char *master
, const char *name
, int mode
)
1705 int err
, index
, len
;
1706 struct ifinfomsg
*ifi
;
1707 struct nl_handler nlh
;
1708 struct rtattr
*nest
, *nest2
;
1709 struct nlmsg
*answer
= NULL
, *nlmsg
= NULL
;
1711 err
= netlink_open(&nlh
, NETLINK_ROUTE
);
1716 len
= strlen(master
);
1717 if (len
== 1 || len
>= IFNAMSIZ
)
1721 if (len
== 1 || len
>= IFNAMSIZ
)
1725 nlmsg
= nlmsg_alloc(NLMSG_GOOD_SIZE
);
1729 answer
= nlmsg_alloc_reserve(NLMSG_GOOD_SIZE
);
1734 index
= if_nametoindex(master
);
1738 nlmsg
->nlmsghdr
->nlmsg_flags
=
1739 NLM_F_REQUEST
| NLM_F_CREATE
| NLM_F_EXCL
| NLM_F_ACK
;
1740 nlmsg
->nlmsghdr
->nlmsg_type
= RTM_NEWLINK
;
1742 ifi
= nlmsg_reserve(nlmsg
, sizeof(struct ifinfomsg
));
1747 ifi
->ifi_family
= AF_UNSPEC
;
1749 nest
= nla_begin_nested(nlmsg
, IFLA_LINKINFO
);
1753 if (nla_put_string(nlmsg
, IFLA_INFO_KIND
, "macvlan"))
1757 nest2
= nla_begin_nested(nlmsg
, IFLA_INFO_DATA
);
1761 if (nla_put_u32(nlmsg
, IFLA_MACVLAN_MODE
, mode
))
1764 nla_end_nested(nlmsg
, nest2
);
1767 nla_end_nested(nlmsg
, nest
);
1769 if (nla_put_u32(nlmsg
, IFLA_LINK
, index
))
1772 if (nla_put_string(nlmsg
, IFLA_IFNAME
, name
))
1775 err
= netlink_transaction(&nlh
, nlmsg
, answer
);
1777 netlink_close(&nlh
);
1783 static int proc_sys_net_write(const char *path
, const char *value
)
1788 fd
= open(path
, O_WRONLY
);
1792 if (lxc_write_nointr(fd
, value
, strlen(value
)) < 0)
1799 static int lxc_is_ip_forwarding_enabled(const char *ifname
, int family
)
1802 char path
[PATH_MAX
];
1805 if (family
!= AF_INET
&& family
!= AF_INET6
)
1806 return minus_one_set_errno(EINVAL
);
1808 ret
= snprintf(path
, PATH_MAX
, "/proc/sys/net/%s/conf/%s/%s",
1809 family
== AF_INET
? "ipv4" : "ipv6", ifname
,
1811 if (ret
< 0 || (size_t)ret
>= PATH_MAX
)
1812 return minus_one_set_errno(E2BIG
);
1814 return lxc_read_file_expect(path
, buf
, 1, "1");
1817 static int neigh_proxy_set(const char *ifname
, int family
, int flag
)
1820 char path
[PATH_MAX
];
1822 if (family
!= AF_INET
&& family
!= AF_INET6
)
1825 ret
= snprintf(path
, PATH_MAX
, "/proc/sys/net/%s/conf/%s/%s",
1826 family
== AF_INET
? "ipv4" : "ipv6", ifname
,
1827 family
== AF_INET
? "proxy_arp" : "proxy_ndp");
1828 if (ret
< 0 || (size_t)ret
>= PATH_MAX
)
1831 return proc_sys_net_write(path
, flag
? "1" : "0");
1834 static int lxc_is_ip_neigh_proxy_enabled(const char *ifname
, int family
)
1837 char path
[PATH_MAX
];
1840 if (family
!= AF_INET
&& family
!= AF_INET6
)
1841 return minus_one_set_errno(EINVAL
);
1843 ret
= snprintf(path
, PATH_MAX
, "/proc/sys/net/%s/conf/%s/%s",
1844 family
== AF_INET
? "ipv4" : "ipv6", ifname
,
1845 family
== AF_INET
? "proxy_arp" : "proxy_ndp");
1846 if (ret
< 0 || (size_t)ret
>= PATH_MAX
)
1847 return minus_one_set_errno(E2BIG
);
1849 return lxc_read_file_expect(path
, buf
, 1, "1");
1852 int lxc_neigh_proxy_on(const char *name
, int family
)
1854 return neigh_proxy_set(name
, family
, 1);
1857 int lxc_neigh_proxy_off(const char *name
, int family
)
1859 return neigh_proxy_set(name
, family
, 0);
1862 int lxc_convert_mac(char *macaddr
, struct sockaddr
*sockaddr
)
1867 unsigned char *data
;
1869 sockaddr
->sa_family
= ARPHRD_ETHER
;
1870 data
= (unsigned char *)sockaddr
->sa_data
;
1872 while ((*macaddr
!= '\0') && (i
< ETH_ALEN
)) {
1876 else if (c
>= 'a' && c
<= 'f')
1878 else if (c
>= 'A' && c
<= 'F')
1887 else if (c
>= 'a' && c
<= 'f')
1888 val
|= c
- 'a' + 10;
1889 else if (c
>= 'A' && c
<= 'F')
1890 val
|= c
- 'A' + 10;
1891 else if (c
== ':' || c
== 0)
1897 *data
++ = (unsigned char)(val
& 0377);
1900 if (*macaddr
== ':')
1907 static int ip_addr_add(int family
, int ifindex
, void *addr
, void *bcast
,
1908 void *acast
, int prefix
)
1911 struct ifaddrmsg
*ifa
;
1912 struct nl_handler nlh
;
1913 struct nlmsg
*answer
= NULL
, *nlmsg
= NULL
;
1915 addrlen
= family
== AF_INET
? sizeof(struct in_addr
)
1916 : sizeof(struct in6_addr
);
1918 err
= netlink_open(&nlh
, NETLINK_ROUTE
);
1923 nlmsg
= nlmsg_alloc(NLMSG_GOOD_SIZE
);
1927 answer
= nlmsg_alloc_reserve(NLMSG_GOOD_SIZE
);
1931 nlmsg
->nlmsghdr
->nlmsg_flags
=
1932 NLM_F_ACK
| NLM_F_REQUEST
| NLM_F_CREATE
| NLM_F_EXCL
;
1933 nlmsg
->nlmsghdr
->nlmsg_type
= RTM_NEWADDR
;
1935 ifa
= nlmsg_reserve(nlmsg
, sizeof(struct ifaddrmsg
));
1938 ifa
->ifa_prefixlen
= prefix
;
1939 ifa
->ifa_index
= ifindex
;
1940 ifa
->ifa_family
= family
;
1944 if (nla_put_buffer(nlmsg
, IFA_LOCAL
, addr
, addrlen
))
1947 if (nla_put_buffer(nlmsg
, IFA_ADDRESS
, addr
, addrlen
))
1950 if (nla_put_buffer(nlmsg
, IFA_BROADCAST
, bcast
, addrlen
))
1953 /* TODO: multicast, anycast with ipv6 */
1954 err
= -EPROTONOSUPPORT
;
1955 if (family
== AF_INET6
&&
1956 (memcmp(bcast
, &in6addr_any
, sizeof(in6addr_any
)) ||
1957 memcmp(acast
, &in6addr_any
, sizeof(in6addr_any
))))
1960 err
= netlink_transaction(&nlh
, nlmsg
, answer
);
1962 netlink_close(&nlh
);
1968 int lxc_ipv6_addr_add(int ifindex
, struct in6_addr
*addr
,
1969 struct in6_addr
*mcast
, struct in6_addr
*acast
,
1972 return ip_addr_add(AF_INET6
, ifindex
, addr
, mcast
, acast
, prefix
);
1975 int lxc_ipv4_addr_add(int ifindex
, struct in_addr
*addr
, struct in_addr
*bcast
,
1978 return ip_addr_add(AF_INET
, ifindex
, addr
, bcast
, NULL
, prefix
);
1981 /* Find an IFA_LOCAL (or IFA_ADDRESS if not IFA_LOCAL is present) address from
1982 * the given RTM_NEWADDR message. Allocates memory for the address and stores
1983 * that pointer in *res (so res should be an in_addr** or in6_addr**).
1985 #pragma GCC diagnostic push
1986 #pragma GCC diagnostic ignored "-Wcast-align"
1988 static int ifa_get_local_ip(int family
, struct nlmsghdr
*msg
, void **res
)
1991 struct ifaddrmsg
*ifa
= NLMSG_DATA(msg
);
1992 struct rtattr
*rta
= IFA_RTA(ifa
);
1993 int attr_len
= NLMSG_PAYLOAD(msg
, sizeof(struct ifaddrmsg
));
1995 if (ifa
->ifa_family
!= family
)
1998 addrlen
= family
== AF_INET
? sizeof(struct in_addr
)
1999 : sizeof(struct in6_addr
);
2001 /* Loop over the rtattr's in this message */
2002 while (RTA_OK(rta
, attr_len
)) {
2003 /* Found a local address for the requested interface,
2006 if (rta
->rta_type
== IFA_LOCAL
||
2007 rta
->rta_type
== IFA_ADDRESS
) {
2008 /* Sanity check. The family check above should make sure
2009 * the address length is correct, but check here just in
2012 if (RTA_PAYLOAD(rta
) != addrlen
)
2015 /* We might have found an IFA_ADDRESS before, which we
2016 * now overwrite with an IFA_LOCAL.
2019 *res
= malloc(addrlen
);
2024 memcpy(*res
, RTA_DATA(rta
), addrlen
);
2025 if (rta
->rta_type
== IFA_LOCAL
)
2028 rta
= RTA_NEXT(rta
, attr_len
);
2033 #pragma GCC diagnostic pop
2035 static int ip_addr_get(int family
, int ifindex
, void **res
)
2037 int answer_len
, err
;
2038 struct ifaddrmsg
*ifa
;
2039 struct nl_handler nlh
;
2040 struct nlmsghdr
*msg
;
2041 int readmore
= 0, recv_len
= 0;
2042 struct nlmsg
*answer
= NULL
, *nlmsg
= NULL
;
2044 err
= netlink_open(&nlh
, NETLINK_ROUTE
);
2049 nlmsg
= nlmsg_alloc(NLMSG_GOOD_SIZE
);
2053 answer
= nlmsg_alloc_reserve(NLMSG_GOOD_SIZE
);
2057 /* Save the answer buffer length, since it will be overwritten on the
2058 * first receive (and we might need to receive more than once).
2060 answer_len
= answer
->nlmsghdr
->nlmsg_len
;
2062 nlmsg
->nlmsghdr
->nlmsg_flags
= NLM_F_REQUEST
| NLM_F_ROOT
;
2063 nlmsg
->nlmsghdr
->nlmsg_type
= RTM_GETADDR
;
2065 ifa
= nlmsg_reserve(nlmsg
, sizeof(struct ifaddrmsg
));
2068 ifa
->ifa_family
= family
;
2070 /* Send the request for addresses, which returns all addresses on all
2073 err
= netlink_send(&nlh
, nlmsg
);
2077 #pragma GCC diagnostic push
2078 #pragma GCC diagnostic ignored "-Wcast-align"
2081 /* Restore the answer buffer length, it might have been
2082 * overwritten by a previous receive.
2084 answer
->nlmsghdr
->nlmsg_len
= answer_len
;
2086 /* Get the (next) batch of reply messages. */
2087 err
= netlink_rcv(&nlh
, answer
);
2094 /* Satisfy the typing for the netlink macros. */
2095 msg
= answer
->nlmsghdr
;
2097 while (NLMSG_OK(msg
, recv_len
)) {
2098 /* Stop reading if we see an error message. */
2099 if (msg
->nlmsg_type
== NLMSG_ERROR
) {
2100 struct nlmsgerr
*errmsg
=
2101 (struct nlmsgerr
*)NLMSG_DATA(msg
);
2102 err
= errmsg
->error
;
2106 /* Stop reading if we see a NLMSG_DONE message. */
2107 if (msg
->nlmsg_type
== NLMSG_DONE
) {
2112 if (msg
->nlmsg_type
!= RTM_NEWADDR
) {
2117 ifa
= (struct ifaddrmsg
*)NLMSG_DATA(msg
);
2118 if (ifa
->ifa_index
== ifindex
) {
2119 if (ifa_get_local_ip(family
, msg
, res
) < 0) {
2124 /* Found a result, stop searching. */
2129 /* Keep reading more data from the socket if the last
2130 * message had the NLF_F_MULTI flag set.
2132 readmore
= (msg
->nlmsg_flags
& NLM_F_MULTI
);
2134 /* Look at the next message received in this buffer. */
2135 msg
= NLMSG_NEXT(msg
, recv_len
);
2139 #pragma GCC diagnostic pop
2141 /* If we end up here, we didn't find any result, so signal an
2147 netlink_close(&nlh
);
2153 int lxc_ipv6_addr_get(int ifindex
, struct in6_addr
**res
)
2155 return ip_addr_get(AF_INET6
, ifindex
, (void **)res
);
2158 int lxc_ipv4_addr_get(int ifindex
, struct in_addr
**res
)
2160 return ip_addr_get(AF_INET
, ifindex
, (void **)res
);
2163 static int ip_gateway_add(int family
, int ifindex
, void *gw
)
2166 struct nl_handler nlh
;
2168 struct nlmsg
*answer
= NULL
, *nlmsg
= NULL
;
2170 addrlen
= family
== AF_INET
? sizeof(struct in_addr
)
2171 : sizeof(struct in6_addr
);
2173 err
= netlink_open(&nlh
, NETLINK_ROUTE
);
2178 nlmsg
= nlmsg_alloc(NLMSG_GOOD_SIZE
);
2182 answer
= nlmsg_alloc_reserve(NLMSG_GOOD_SIZE
);
2186 nlmsg
->nlmsghdr
->nlmsg_flags
=
2187 NLM_F_ACK
| NLM_F_REQUEST
| NLM_F_CREATE
| NLM_F_EXCL
;
2188 nlmsg
->nlmsghdr
->nlmsg_type
= RTM_NEWROUTE
;
2190 rt
= nlmsg_reserve(nlmsg
, sizeof(struct rtmsg
));
2193 rt
->rtm_family
= family
;
2194 rt
->rtm_table
= RT_TABLE_MAIN
;
2195 rt
->rtm_scope
= RT_SCOPE_UNIVERSE
;
2196 rt
->rtm_protocol
= RTPROT_BOOT
;
2197 rt
->rtm_type
= RTN_UNICAST
;
2198 /* "default" destination */
2199 rt
->rtm_dst_len
= 0;
2203 /* If gateway address not supplied, then a device route will be created instead */
2205 if (nla_put_buffer(nlmsg
, RTA_GATEWAY
, gw
, addrlen
))
2209 /* Adding the interface index enables the use of link-local
2210 * addresses for the gateway.
2212 if (nla_put_u32(nlmsg
, RTA_OIF
, ifindex
))
2215 err
= netlink_transaction(&nlh
, nlmsg
, answer
);
2217 netlink_close(&nlh
);
2223 int lxc_ipv4_gateway_add(int ifindex
, struct in_addr
*gw
)
2225 return ip_gateway_add(AF_INET
, ifindex
, gw
);
2228 int lxc_ipv6_gateway_add(int ifindex
, struct in6_addr
*gw
)
2230 return ip_gateway_add(AF_INET6
, ifindex
, gw
);
2232 bool is_ovs_bridge(const char *bridge
)
2236 char brdirname
[22 + IFNAMSIZ
+ 1] = {0};
2238 ret
= snprintf(brdirname
, 22 + IFNAMSIZ
+ 1, "/sys/class/net/%s/bridge",
2240 if (ret
< 0 || (size_t)ret
>= 22 + IFNAMSIZ
+ 1)
2243 ret
= stat(brdirname
, &sb
);
2244 if (ret
< 0 && errno
== ENOENT
)
2250 struct ovs_veth_args
{
2255 /* Called from a background thread - when nic goes away, remove it from the
2258 static int lxc_ovs_delete_port_exec(void *data
)
2260 struct ovs_veth_args
*args
= data
;
2262 execlp("ovs-vsctl", "ovs-vsctl", "del-port", args
->bridge
, args
->nic
,
2267 int lxc_ovs_delete_port(const char *bridge
, const char *nic
)
2270 char cmd_output
[PATH_MAX
];
2271 struct ovs_veth_args args
;
2273 args
.bridge
= bridge
;
2275 ret
= run_command(cmd_output
, sizeof(cmd_output
),
2276 lxc_ovs_delete_port_exec
, (void *)&args
);
2278 ERROR("Failed to delete \"%s\" from openvswitch bridge \"%s\": "
2279 "%s", bridge
, nic
, cmd_output
);
2286 static int lxc_ovs_attach_bridge_exec(void *data
)
2288 struct ovs_veth_args
*args
= data
;
2290 execlp("ovs-vsctl", "ovs-vsctl", "add-port", args
->bridge
, args
->nic
,
2295 static int lxc_ovs_attach_bridge(const char *bridge
, const char *nic
)
2298 char cmd_output
[PATH_MAX
];
2299 struct ovs_veth_args args
;
2301 args
.bridge
= bridge
;
2303 ret
= run_command(cmd_output
, sizeof(cmd_output
),
2304 lxc_ovs_attach_bridge_exec
, (void *)&args
);
2306 ERROR("Failed to attach \"%s\" to openvswitch bridge \"%s\": %s",
2307 bridge
, nic
, cmd_output
);
2314 int lxc_bridge_attach(const char *bridge
, const char *ifname
)
2320 if (strlen(ifname
) >= IFNAMSIZ
)
2323 index
= if_nametoindex(ifname
);
2327 if (is_ovs_bridge(bridge
))
2328 return lxc_ovs_attach_bridge(bridge
, ifname
);
2330 fd
= socket(AF_INET
, SOCK_STREAM
| SOCK_CLOEXEC
, 0);
2334 retlen
= strlcpy(ifr
.ifr_name
, bridge
, IFNAMSIZ
);
2335 if (retlen
>= IFNAMSIZ
) {
2340 ifr
.ifr_name
[IFNAMSIZ
- 1] = '\0';
2341 ifr
.ifr_ifindex
= index
;
2342 err
= ioctl(fd
, SIOCBRADDIF
, &ifr
);
2350 static const char *const lxc_network_types
[LXC_NET_MAXCONFTYPE
+ 1] = {
2351 [LXC_NET_EMPTY
] = "empty",
2352 [LXC_NET_VETH
] = "veth",
2353 [LXC_NET_MACVLAN
] = "macvlan",
2354 [LXC_NET_IPVLAN
] = "ipvlan",
2355 [LXC_NET_PHYS
] = "phys",
2356 [LXC_NET_VLAN
] = "vlan",
2357 [LXC_NET_NONE
] = "none",
2360 const char *lxc_net_type_to_str(int type
)
2362 if (type
< 0 || type
> LXC_NET_MAXCONFTYPE
)
2365 return lxc_network_types
[type
];
2368 static const char padchar
[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
2370 char *lxc_mkifname(char *template)
2373 struct netns_ifaddrs
*ifa
, *ifaddr
;
2374 char name
[IFNAMSIZ
];
2375 bool exists
= false;
2380 seed
= randseed(false);
2383 (void)randseed(true);
2386 if (strlen(template) >= IFNAMSIZ
)
2389 /* Get all the network interfaces. */
2390 ret
= netns_getifaddrs(&ifaddr
, -1, &(bool){false});
2392 SYSERROR("Failed to get network interfaces");
2396 /* Generate random names until we find one that doesn't exist. */
2399 (void)strlcpy(name
, template, IFNAMSIZ
);
2403 for (i
= 0; i
< strlen(name
); i
++) {
2404 if (name
[i
] == 'X') {
2406 name
[i
] = padchar
[rand_r(&seed
) % strlen(padchar
)];
2408 name
[i
] = padchar
[rand() % strlen(padchar
)];
2413 for (ifa
= ifaddr
; ifa
!= NULL
; ifa
= ifa
->ifa_next
) {
2414 if (!strcmp(ifa
->ifa_name
, name
)) {
2424 netns_freeifaddrs(ifaddr
);
2425 (void)strlcpy(template, name
, strlen(template) + 1);
2430 int setup_private_host_hw_addr(char *veth1
)
2435 sockfd
= socket(AF_INET
, SOCK_DGRAM
| SOCK_CLOEXEC
, 0);
2439 err
= snprintf((char *)ifr
.ifr_name
, IFNAMSIZ
, "%s", veth1
);
2440 if (err
< 0 || (size_t)err
>= IFNAMSIZ
) {
2445 err
= ioctl(sockfd
, SIOCGIFHWADDR
, &ifr
);
2451 ifr
.ifr_hwaddr
.sa_data
[0] = 0xfe;
2452 err
= ioctl(sockfd
, SIOCSIFHWADDR
, &ifr
);
2460 int lxc_find_gateway_addresses(struct lxc_handler
*handler
)
2462 struct lxc_list
*network
= &handler
->conf
->network
;
2463 struct lxc_list
*iterator
;
2464 struct lxc_netdev
*netdev
;
2467 lxc_list_for_each(iterator
, network
) {
2468 netdev
= iterator
->elem
;
2470 if (!netdev
->ipv4_gateway_auto
&& !netdev
->ipv6_gateway_auto
)
2473 if (netdev
->type
!= LXC_NET_VETH
&& netdev
->type
!= LXC_NET_MACVLAN
) {
2474 ERROR("Automatic gateway detection is only supported "
2475 "for veth and macvlan");
2479 if (netdev
->link
[0] == '\0') {
2480 ERROR("Automatic gateway detection needs a link interface");
2484 link_index
= if_nametoindex(netdev
->link
);
2488 if (netdev
->ipv4_gateway_auto
) {
2489 if (lxc_ipv4_addr_get(link_index
, &netdev
->ipv4_gateway
)) {
2490 ERROR("Failed to automatically find ipv4 gateway "
2491 "address from link interface \"%s\"", netdev
->link
);
2496 if (netdev
->ipv6_gateway_auto
) {
2497 if (lxc_ipv6_addr_get(link_index
, &netdev
->ipv6_gateway
)) {
2498 ERROR("Failed to automatically find ipv6 gateway "
2499 "address from link interface \"%s\"", netdev
->link
);
2508 #define LXC_USERNIC_PATH LIBEXECDIR "/lxc/lxc-user-nic"
2509 static int lxc_create_network_unpriv_exec(const char *lxcpath
, const char *lxcname
,
2510 struct lxc_netdev
*netdev
, pid_t pid
, unsigned int hooks_version
)
2514 int bytes
, pipefd
[2];
2515 char *token
, *saveptr
= NULL
;
2516 char netdev_link
[IFNAMSIZ
];
2517 char buffer
[PATH_MAX
] = {0};
2520 if (netdev
->type
!= LXC_NET_VETH
) {
2521 ERROR("Network type %d not support for unprivileged use", netdev
->type
);
2527 SYSERROR("Failed to create pipe");
2533 SYSERROR("Failed to create new process");
2540 char pidstr
[INTTYPE_TO_STRLEN(pid_t
)];
2544 ret
= dup2(pipefd
[1], STDOUT_FILENO
);
2546 ret
= dup2(pipefd
[1], STDERR_FILENO
);
2549 SYSERROR("Failed to duplicate std{err,out} file descriptor");
2550 _exit(EXIT_FAILURE
);
2553 if (netdev
->link
[0] != '\0')
2554 retlen
= strlcpy(netdev_link
, netdev
->link
, IFNAMSIZ
);
2556 retlen
= strlcpy(netdev_link
, "none", IFNAMSIZ
);
2557 if (retlen
>= IFNAMSIZ
) {
2558 SYSERROR("Invalid network device name");
2559 _exit(EXIT_FAILURE
);
2562 ret
= snprintf(pidstr
, sizeof(pidstr
), "%d", pid
);
2563 if (ret
< 0 || ret
>= sizeof(pidstr
))
2564 _exit(EXIT_FAILURE
);
2565 pidstr
[sizeof(pidstr
) - 1] = '\0';
2567 INFO("Execing lxc-user-nic create %s %s %s veth %s %s", lxcpath
,
2568 lxcname
, pidstr
, netdev_link
,
2569 netdev
->name
[0] != '\0' ? netdev
->name
: "(null)");
2570 if (netdev
->name
[0] != '\0')
2571 execlp(LXC_USERNIC_PATH
, LXC_USERNIC_PATH
, "create",
2572 lxcpath
, lxcname
, pidstr
, "veth", netdev_link
,
2573 netdev
->name
, (char *)NULL
);
2575 execlp(LXC_USERNIC_PATH
, LXC_USERNIC_PATH
, "create",
2576 lxcpath
, lxcname
, pidstr
, "veth", netdev_link
,
2578 SYSERROR("Failed to execute lxc-user-nic");
2579 _exit(EXIT_FAILURE
);
2582 /* close the write-end of the pipe */
2585 bytes
= lxc_read_nointr(pipefd
[0], &buffer
, PATH_MAX
);
2587 SYSERROR("Failed to read from pipe file descriptor");
2590 buffer
[bytes
- 1] = '\0';
2593 ret
= wait_for_pid(child
);
2595 if (ret
!= 0 || bytes
< 0) {
2596 ERROR("lxc-user-nic failed to configure requested network: %s",
2597 buffer
[0] != '\0' ? buffer
: "(null)");
2600 TRACE("Received output \"%s\" from lxc-user-nic", buffer
);
2603 token
= strtok_r(buffer
, ":", &saveptr
);
2605 ERROR("Failed to parse lxc-user-nic output");
2610 * lxc-user-nic will take care of proper network device naming. So
2611 * netdev->name and netdev->created_name need to be identical to not
2612 * trigger another rename later on.
2614 retlen
= strlcpy(netdev
->name
, token
, IFNAMSIZ
);
2615 if (retlen
< IFNAMSIZ
)
2616 retlen
= strlcpy(netdev
->created_name
, token
, IFNAMSIZ
);
2617 if (retlen
>= IFNAMSIZ
) {
2618 ERROR("Container side veth device name returned by lxc-user-nic is too long");
2622 /* netdev->ifindex */
2623 token
= strtok_r(NULL
, ":", &saveptr
);
2625 ERROR("Failed to parse lxc-user-nic output");
2629 ret
= lxc_safe_int(token
, &netdev
->ifindex
);
2632 SYSERROR("Failed to convert string \"%s\" to integer", token
);
2636 /* netdev->priv.veth_attr.veth1 */
2637 token
= strtok_r(NULL
, ":", &saveptr
);
2639 ERROR("Failed to parse lxc-user-nic output");
2643 retlen
= strlcpy(netdev
->priv
.veth_attr
.veth1
, token
, IFNAMSIZ
);
2644 if (retlen
>= IFNAMSIZ
) {
2645 ERROR("Host side veth device name returned by lxc-user-nic is "
2650 /* netdev->priv.veth_attr.ifindex */
2651 token
= strtok_r(NULL
, ":", &saveptr
);
2653 ERROR("Failed to parse lxc-user-nic output");
2657 ret
= lxc_safe_int(token
, &netdev
->priv
.veth_attr
.ifindex
);
2660 SYSERROR("Failed to convert string \"%s\" to integer", token
);
2664 if (netdev
->upscript
) {
2668 netdev
->priv
.veth_attr
.veth1
,
2672 ret
= run_script_argv(lxcname
, hooks_version
, "net",
2673 netdev
->upscript
, "up", argv
);
2681 static int lxc_delete_network_unpriv_exec(const char *lxcpath
, const char *lxcname
,
2682 struct lxc_netdev
*netdev
,
2683 const char *netns_path
)
2688 char buffer
[PATH_MAX
] = {0};
2690 if (netdev
->type
!= LXC_NET_VETH
) {
2691 ERROR("Network type %d not support for unprivileged use", netdev
->type
);
2697 SYSERROR("Failed to create pipe");
2703 SYSERROR("Failed to create new process");
2714 ret
= dup2(pipefd
[1], STDOUT_FILENO
);
2716 ret
= dup2(pipefd
[1], STDERR_FILENO
);
2719 SYSERROR("Failed to duplicate std{err,out} file descriptor");
2720 _exit(EXIT_FAILURE
);
2723 if (netdev
->priv
.veth_attr
.pair
[0] != '\0')
2724 hostveth
= netdev
->priv
.veth_attr
.pair
;
2726 hostveth
= netdev
->priv
.veth_attr
.veth1
;
2727 if (hostveth
[0] == '\0') {
2728 SYSERROR("Host side veth device name is missing");
2729 _exit(EXIT_FAILURE
);
2732 if (netdev
->link
[0] == '\0') {
2733 SYSERROR("Network link for network device \"%s\" is "
2734 "missing", netdev
->priv
.veth_attr
.veth1
);
2735 _exit(EXIT_FAILURE
);
2738 INFO("Execing lxc-user-nic delete %s %s %s veth %s %s", lxcpath
,
2739 lxcname
, netns_path
, netdev
->link
, hostveth
);
2740 execlp(LXC_USERNIC_PATH
, LXC_USERNIC_PATH
, "delete", lxcpath
,
2741 lxcname
, netns_path
, "veth", netdev
->link
, hostveth
,
2743 SYSERROR("Failed to exec lxc-user-nic.");
2744 _exit(EXIT_FAILURE
);
2749 bytes
= lxc_read_nointr(pipefd
[0], &buffer
, PATH_MAX
);
2751 SYSERROR("Failed to read from pipe file descriptor.");
2754 buffer
[bytes
- 1] = '\0';
2757 ret
= wait_for_pid(child
);
2759 if (ret
!= 0 || bytes
< 0) {
2760 ERROR("lxc-user-nic failed to delete requested network: %s",
2761 buffer
[0] != '\0' ? buffer
: "(null)");
2768 bool lxc_delete_network_unpriv(struct lxc_handler
*handler
)
2771 struct lxc_list
*iterator
;
2772 struct lxc_list
*network
= &handler
->conf
->network
;
2773 /* strlen("/proc/") = 6
2775 * INTTYPE_TO_STRLEN(pid_t)
2777 * strlen("/fd/") = 4
2779 * INTTYPE_TO_STRLEN(int)
2783 char netns_path
[6 + INTTYPE_TO_STRLEN(pid_t
) + 4 + INTTYPE_TO_STRLEN(int) + 1];
2787 if (handler
->nsfd
[LXC_NS_NET
] < 0) {
2788 DEBUG("Cannot not guarantee safe deletion of network devices. "
2789 "Manual cleanup maybe needed");
2793 ret
= snprintf(netns_path
, sizeof(netns_path
), "/proc/%d/fd/%d",
2794 lxc_raw_getpid(), handler
->nsfd
[LXC_NS_NET
]);
2795 if (ret
< 0 || ret
>= sizeof(netns_path
))
2798 lxc_list_for_each(iterator
, network
) {
2799 char *hostveth
= NULL
;
2800 struct lxc_netdev
*netdev
= iterator
->elem
;
2802 /* We can only delete devices whose ifindex we have. If we don't
2803 * have the index it means that we didn't create it.
2805 if (!netdev
->ifindex
)
2808 if (netdev
->type
== LXC_NET_PHYS
) {
2809 ret
= lxc_netdev_rename_by_index(netdev
->ifindex
,
2812 WARN("Failed to rename interface with index %d "
2813 "to its initial name \"%s\"",
2814 netdev
->ifindex
, netdev
->link
);
2816 TRACE("Renamed interface with index %d to its "
2817 "initial name \"%s\"",
2818 netdev
->ifindex
, netdev
->link
);
2820 ret
= netdev_deconf
[netdev
->type
](handler
, netdev
);
2821 goto clear_ifindices
;
2824 ret
= netdev_deconf
[netdev
->type
](handler
, netdev
);
2826 WARN("Failed to deconfigure network device");
2828 if (netdev
->type
!= LXC_NET_VETH
)
2829 goto clear_ifindices
;
2831 if (netdev
->link
[0] == '\0' || !is_ovs_bridge(netdev
->link
))
2832 goto clear_ifindices
;
2834 if (netdev
->priv
.veth_attr
.pair
[0] != '\0')
2835 hostveth
= netdev
->priv
.veth_attr
.pair
;
2837 hostveth
= netdev
->priv
.veth_attr
.veth1
;
2838 if (hostveth
[0] == '\0')
2839 goto clear_ifindices
;
2841 ret
= lxc_delete_network_unpriv_exec(handler
->lxcpath
,
2842 handler
->name
, netdev
,
2845 WARN("Failed to remove port \"%s\" from openvswitch "
2846 "bridge \"%s\"", hostveth
, netdev
->link
);
2847 goto clear_ifindices
;
2849 INFO("Removed interface \"%s\" from \"%s\"", hostveth
,
2853 /* We need to clear any ifindices we recorded so liblxc won't
2854 * have cached stale data which would cause it to fail on reboot
2855 * we're we don't re-read the on-disk config file.
2857 netdev
->ifindex
= 0;
2858 if (netdev
->type
== LXC_NET_PHYS
) {
2859 netdev
->priv
.phys_attr
.ifindex
= 0;
2860 } else if (netdev
->type
== LXC_NET_VETH
) {
2861 netdev
->priv
.veth_attr
.veth1
[0] = '\0';
2862 netdev
->priv
.veth_attr
.ifindex
= 0;
2869 struct ip_proxy_args
{
2874 static int lxc_add_ip_neigh_proxy_exec_wrapper(void *data
)
2876 struct ip_proxy_args
*args
= data
;
2878 execlp("ip", "ip", "neigh", "add", "proxy", args
->ip
, "dev", args
->dev
, (char *)NULL
);
2882 static int lxc_del_ip_neigh_proxy_exec_wrapper(void *data
)
2884 struct ip_proxy_args
*args
= data
;
2886 execlp("ip", "ip", "neigh", "flush", "proxy", args
->ip
, "dev", args
->dev
, (char *)NULL
);
2890 static int lxc_add_ip_neigh_proxy(const char *ip
, const char *dev
)
2893 char cmd_output
[PATH_MAX
];
2894 struct ip_proxy_args args
= {
2899 ret
= run_command(cmd_output
, sizeof(cmd_output
), lxc_add_ip_neigh_proxy_exec_wrapper
, &args
);
2901 ERROR("Failed to add ip proxy \"%s\" to dev \"%s\": %s", ip
, dev
, cmd_output
);
2908 static int lxc_del_ip_neigh_proxy(const char *ip
, const char *dev
)
2911 char cmd_output
[PATH_MAX
];
2912 struct ip_proxy_args args
= {
2917 ret
= run_command(cmd_output
, sizeof(cmd_output
), lxc_del_ip_neigh_proxy_exec_wrapper
, &args
);
2919 ERROR("Failed to delete ip proxy \"%s\" to dev \"%s\": %s", ip
, dev
, cmd_output
);
2926 static int lxc_setup_l2proxy(struct lxc_netdev
*netdev
) {
2927 struct lxc_list
*cur
, *next
;
2928 struct lxc_inetdev
*inet4dev
;
2929 struct lxc_inet6dev
*inet6dev
;
2930 char bufinet4
[INET_ADDRSTRLEN
], bufinet6
[INET6_ADDRSTRLEN
];
2932 unsigned int lo_ifindex
= 0;
2934 /* If IPv4 addresses are specified, then check that sysctl is configured correctly. */
2935 if (!lxc_list_empty(&netdev
->ipv4
)) {
2936 /* Check for net.ipv4.conf.[link].forwarding=1 */
2937 if (lxc_is_ip_forwarding_enabled(netdev
->link
, AF_INET
) < 0) {
2938 ERROR("Requires sysctl net.ipv4.conf.%s.forwarding=1", netdev
->link
);
2939 return minus_one_set_errno(EINVAL
);
2943 /* If IPv6 addresses are specified, then check that sysctl is configured correctly. */
2944 if (!lxc_list_empty(&netdev
->ipv6
)) {
2945 /* Check for net.ipv6.conf.[link].proxy_ndp=1 */
2946 if (lxc_is_ip_neigh_proxy_enabled(netdev
->link
, AF_INET6
) < 0) {
2947 ERROR("Requires sysctl net.ipv6.conf.%s.proxy_ndp=1", netdev
->link
);
2948 return minus_one_set_errno(EINVAL
);
2951 /* Check for net.ipv6.conf.[link].forwarding=1 */
2952 if (lxc_is_ip_forwarding_enabled(netdev
->link
, AF_INET6
) < 0) {
2953 ERROR("Requires sysctl net.ipv6.conf.%s.forwarding=1", netdev
->link
);
2954 return minus_one_set_errno(EINVAL
);
2958 /* Perform IPVLAN specific checks. */
2959 if (netdev
->type
== LXC_NET_IPVLAN
) {
2960 /* Check mode is l3s as other modes do not work with l2proxy. */
2961 if (netdev
->priv
.ipvlan_attr
.mode
!= IPVLAN_MODE_L3S
) {
2962 ERROR("Requires ipvlan mode on dev \"%s\" be l3s when used with l2proxy", netdev
->link
);
2963 return minus_one_set_errno(EINVAL
);
2966 /* Retrieve local-loopback interface index for use with IPVLAN static routes. */
2967 lo_ifindex
= if_nametoindex(loop_device
);
2968 if (lo_ifindex
== 0) {
2969 ERROR("Failed to retrieve ifindex for \"%s\" routing cleanup", loop_device
);
2970 return minus_one_set_errno(EINVAL
);
2974 lxc_list_for_each_safe(cur
, &netdev
->ipv4
, next
) {
2975 inet4dev
= cur
->elem
;
2976 if (!inet_ntop(AF_INET
, &inet4dev
->addr
, bufinet4
, sizeof(bufinet4
)))
2977 return minus_one_set_errno(-errno
);
2979 if (lxc_add_ip_neigh_proxy(bufinet4
, netdev
->link
) < 0)
2980 return minus_one_set_errno(EINVAL
);
2982 /* IPVLAN requires a route to local-loopback to trigger l2proxy. */
2983 if (netdev
->type
== LXC_NET_IPVLAN
) {
2984 err
= lxc_ipv4_dest_add(lo_ifindex
, &inet4dev
->addr
, 32);
2986 ERROR("Failed to add ipv4 dest \"%s\" for network device \"%s\"", bufinet4
, loop_device
);
2987 return minus_one_set_errno(-err
);
2992 lxc_list_for_each_safe(cur
, &netdev
->ipv6
, next
) {
2993 inet6dev
= cur
->elem
;
2994 if (!inet_ntop(AF_INET6
, &inet6dev
->addr
, bufinet6
, sizeof(bufinet6
)))
2995 return minus_one_set_errno(-errno
);
2997 if (lxc_add_ip_neigh_proxy(bufinet6
, netdev
->link
) < 0)
2998 return minus_one_set_errno(EINVAL
);
3000 /* IPVLAN requires a route to local-loopback to trigger l2proxy. */
3001 if (netdev
->type
== LXC_NET_IPVLAN
) {
3002 err
= lxc_ipv6_dest_add(lo_ifindex
, &inet6dev
->addr
, 128);
3004 ERROR("Failed to add ipv6 dest \"%s\" for network device \"%s\"", bufinet6
, loop_device
);
3005 return minus_one_set_errno(-err
);
3013 static int lxc_delete_ipv4_l2proxy(struct in_addr
*ip
, char *link
, unsigned int lo_ifindex
) {
3014 char bufinet4
[INET_ADDRSTRLEN
];
3015 unsigned int errCount
= 0;
3017 if (!inet_ntop(AF_INET
, ip
, bufinet4
, sizeof(bufinet4
))) {
3018 SYSERROR("Failed to convert IP for l2proxy ipv4 removal on dev \"%s\"", link
);
3019 return minus_one_set_errno(EINVAL
);
3022 /* If a local-loopback ifindex supplied remove the static route to the lo device. */
3023 if (lo_ifindex
> 0) {
3024 if (lxc_ipv4_dest_del(lo_ifindex
, ip
, 32) < 0) {
3026 ERROR("Failed to delete ipv4 dest \"%s\" for network ifindex \"%u\"", bufinet4
, lo_ifindex
);
3030 /* If link is supplied remove the IP neigh proxy entry for this IP on the device. */
3031 if (link
[0] != '\0') {
3032 if (lxc_del_ip_neigh_proxy(bufinet4
, link
) < 0)
3037 return minus_one_set_errno(EINVAL
);
3042 static int lxc_delete_ipv6_l2proxy(struct in6_addr
*ip
, char *link
, unsigned int lo_ifindex
) {
3043 char bufinet6
[INET6_ADDRSTRLEN
];
3044 unsigned int errCount
= 0;
3046 if (!inet_ntop(AF_INET6
, ip
, bufinet6
, sizeof(bufinet6
))) {
3047 SYSERROR("Failed to convert IP for l2proxy ipv6 removal on dev \"%s\"", link
);
3048 return minus_one_set_errno(EINVAL
);
3051 /* If a local-loopback ifindex supplied remove the static route to the lo device. */
3052 if (lo_ifindex
> 0) {
3053 if (lxc_ipv6_dest_del(lo_ifindex
, ip
, 128) < 0) {
3055 ERROR("Failed to delete ipv6 dest \"%s\" for network ifindex \"%u\"", bufinet6
, lo_ifindex
);
3059 /* If link is supplied remove the IP neigh proxy entry for this IP on the device. */
3060 if (link
[0] != '\0') {
3061 if (lxc_del_ip_neigh_proxy(bufinet6
, link
) < 0)
3066 return minus_one_set_errno(EINVAL
);
3071 static int lxc_delete_l2proxy(struct lxc_netdev
*netdev
) {
3072 unsigned int lo_ifindex
= 0;
3073 unsigned int errCount
= 0;
3074 struct lxc_list
*cur
, *next
;
3075 struct lxc_inetdev
*inet4dev
;
3076 struct lxc_inet6dev
*inet6dev
;
3078 /* Perform IPVLAN specific checks. */
3079 if (netdev
->type
== LXC_NET_IPVLAN
) {
3080 /* Retrieve local-loopback interface index for use with IPVLAN static routes. */
3081 lo_ifindex
= if_nametoindex(loop_device
);
3082 if (lo_ifindex
== 0) {
3084 ERROR("Failed to retrieve ifindex for \"%s\" routing cleanup", loop_device
);
3088 lxc_list_for_each_safe(cur
, &netdev
->ipv4
, next
) {
3089 inet4dev
= cur
->elem
;
3090 if (lxc_delete_ipv4_l2proxy(&inet4dev
->addr
, netdev
->link
, lo_ifindex
) < 0)
3094 lxc_list_for_each_safe(cur
, &netdev
->ipv6
, next
) {
3095 inet6dev
= cur
->elem
;
3096 if (lxc_delete_ipv6_l2proxy(&inet6dev
->addr
, netdev
->link
, lo_ifindex
) < 0)
3101 return minus_one_set_errno(EINVAL
);
3106 static int lxc_create_network_priv(struct lxc_handler
*handler
)
3108 struct lxc_list
*iterator
;
3109 struct lxc_list
*network
= &handler
->conf
->network
;
3111 lxc_list_for_each(iterator
, network
) {
3112 struct lxc_netdev
*netdev
= iterator
->elem
;
3114 if (netdev
->type
< 0 || netdev
->type
> LXC_NET_MAXCONFTYPE
) {
3115 ERROR("Invalid network configuration type %d", netdev
->type
);
3119 /* Setup l2proxy entries if enabled and used with a link property */
3120 if (netdev
->l2proxy
&& netdev
->link
[0] != '\0') {
3121 if (lxc_setup_l2proxy(netdev
)) {
3122 ERROR("Failed to setup l2proxy");
3127 if (netdev_conf
[netdev
->type
](handler
, netdev
)) {
3128 ERROR("Failed to create network device");
3136 int lxc_network_move_created_netdev_priv(struct lxc_handler
*handler
)
3138 pid_t pid
= handler
->pid
;
3139 struct lxc_list
*network
= &handler
->conf
->network
;
3140 struct lxc_list
*iterator
;
3142 if (am_guest_unpriv())
3145 lxc_list_for_each(iterator
, network
) {
3147 char ifname
[IFNAMSIZ
];
3148 struct lxc_netdev
*netdev
= iterator
->elem
;
3150 if (!netdev
->ifindex
)
3153 /* retrieve the name of the interface */
3154 if (!if_indextoname(netdev
->ifindex
, ifname
)) {
3155 ERROR("No interface corresponding to ifindex \"%d\"",
3160 ret
= lxc_netdev_move_by_name(ifname
, pid
, NULL
);
3163 SYSERROR("Failed to move network device \"%s\" to network namespace %d",
3168 strlcpy(netdev
->created_name
, ifname
, IFNAMSIZ
);
3170 DEBUG("Moved network device \"%s\" to network namespace of %d",
3171 netdev
->created_name
, pid
);
3177 static int lxc_create_network_unpriv(struct lxc_handler
*handler
)
3179 int hooks_version
= handler
->conf
->hooks_version
;
3180 const char *lxcname
= handler
->name
;
3181 const char *lxcpath
= handler
->lxcpath
;
3182 struct lxc_list
*network
= &handler
->conf
->network
;
3183 pid_t pid
= handler
->pid
;
3184 struct lxc_list
*iterator
;
3186 lxc_list_for_each(iterator
, network
) {
3187 struct lxc_netdev
*netdev
= iterator
->elem
;
3189 if (netdev
->type
== LXC_NET_EMPTY
)
3192 if (netdev
->type
== LXC_NET_NONE
)
3195 if (netdev
->type
!= LXC_NET_VETH
) {
3196 ERROR("Networks of type %s are not supported by unprivileged containers",
3197 lxc_net_type_to_str(netdev
->type
));
3202 INFO("mtu ignored due to insufficient privilege");
3204 if (lxc_create_network_unpriv_exec(lxcpath
, lxcname
, netdev
,
3205 pid
, hooks_version
))
3212 bool lxc_delete_network_priv(struct lxc_handler
*handler
)
3215 struct lxc_list
*iterator
;
3216 struct lxc_list
*network
= &handler
->conf
->network
;
3218 lxc_list_for_each(iterator
, network
) {
3219 char *hostveth
= NULL
;
3220 struct lxc_netdev
*netdev
= iterator
->elem
;
3222 /* We can only delete devices whose ifindex we have. If we don't
3223 * have the index it means that we didn't create it.
3225 if (!netdev
->ifindex
)
3228 /* Delete l2proxy entries if enabled and used with a link property */
3229 if (netdev
->l2proxy
&& netdev
->link
[0] != '\0') {
3230 if (lxc_delete_l2proxy(netdev
))
3231 WARN("Failed to delete all l2proxy config");
3232 /* Don't return, let the network be cleaned up as normal. */
3235 if (netdev
->type
== LXC_NET_PHYS
) {
3236 ret
= lxc_netdev_rename_by_index(netdev
->ifindex
, netdev
->link
);
3238 WARN("Failed to rename interface with index %d "
3239 "from \"%s\" to its initial name \"%s\"",
3240 netdev
->ifindex
, netdev
->name
, netdev
->link
);
3242 TRACE("Renamed interface with index %d from "
3243 "\"%s\" to its initial name \"%s\"",
3244 netdev
->ifindex
, netdev
->name
,
3247 /* Restore original MTU */
3248 ret
= lxc_netdev_set_mtu(netdev
->link
, netdev
->priv
.phys_attr
.mtu
);
3250 WARN("Failed to set interface \"%s\" to its initial mtu \"%d\"",
3251 netdev
->link
, netdev
->priv
.phys_attr
.mtu
);
3253 TRACE("Restored interface \"%s\" to its initial mtu \"%d\"",
3254 netdev
->link
, netdev
->priv
.phys_attr
.mtu
);
3258 ret
= netdev_deconf
[netdev
->type
](handler
, netdev
);
3259 goto clear_ifindices
;
3262 ret
= netdev_deconf
[netdev
->type
](handler
, netdev
);
3264 WARN("Failed to deconfigure network device");
3266 /* Recent kernels remove the virtual interfaces when the network
3267 * namespace is destroyed but in case we did not move the
3268 * interface to the network namespace, we have to destroy it.
3270 ret
= lxc_netdev_delete_by_index(netdev
->ifindex
);
3272 if (errno
!= ENODEV
) {
3273 WARN("Failed to remove interface \"%s\" with index %d",
3274 netdev
->name
[0] != '\0' ? netdev
->name
: "(null)",
3276 goto clear_ifindices
;
3278 INFO("Interface \"%s\" with index %d already deleted or existing in different network namespace",
3279 netdev
->name
[0] != '\0' ? netdev
->name
: "(null)",
3282 INFO("Removed interface \"%s\" with index %d",
3283 netdev
->name
[0] != '\0' ? netdev
->name
: "(null)",
3286 if (netdev
->type
!= LXC_NET_VETH
)
3287 goto clear_ifindices
;
3289 /* Explicitly delete host veth device to prevent lingering
3290 * devices. We had issues in LXD around this.
3292 if (netdev
->priv
.veth_attr
.pair
[0] != '\0')
3293 hostveth
= netdev
->priv
.veth_attr
.pair
;
3295 hostveth
= netdev
->priv
.veth_attr
.veth1
;
3296 if (hostveth
[0] == '\0')
3297 goto clear_ifindices
;
3299 ret
= lxc_netdev_delete_by_name(hostveth
);
3301 WARN("Failed to remove interface \"%s\" from \"%s\"",
3302 hostveth
, netdev
->link
);
3303 goto clear_ifindices
;
3305 INFO("Removed interface \"%s\" from \"%s\"", hostveth
, netdev
->link
);
3307 if (netdev
->link
[0] == '\0' || !is_ovs_bridge(netdev
->link
)) {
3308 netdev
->priv
.veth_attr
.veth1
[0] = '\0';
3309 netdev
->ifindex
= 0;
3310 netdev
->priv
.veth_attr
.ifindex
= 0;
3311 goto clear_ifindices
;
3314 /* Delete the openvswitch port. */
3315 ret
= lxc_ovs_delete_port(netdev
->link
, hostveth
);
3317 WARN("Failed to remove port \"%s\" from openvswitch "
3318 "bridge \"%s\"", hostveth
, netdev
->link
);
3320 INFO("Removed port \"%s\" from openvswitch bridge \"%s\"",
3321 hostveth
, netdev
->link
);
3324 /* We need to clear any ifindices we recorded so liblxc won't
3325 * have cached stale data which would cause it to fail on reboot
3326 * we're we don't re-read the on-disk config file.
3328 netdev
->ifindex
= 0;
3329 if (netdev
->type
== LXC_NET_PHYS
) {
3330 netdev
->priv
.phys_attr
.ifindex
= 0;
3331 } else if (netdev
->type
== LXC_NET_VETH
) {
3332 netdev
->priv
.veth_attr
.veth1
[0] = '\0';
3333 netdev
->priv
.veth_attr
.ifindex
= 0;
3340 int lxc_requests_empty_network(struct lxc_handler
*handler
)
3342 struct lxc_list
*network
= &handler
->conf
->network
;
3343 struct lxc_list
*iterator
;
3344 bool found_none
= false, found_nic
= false;
3346 if (lxc_list_empty(network
))
3349 lxc_list_for_each(iterator
, network
) {
3350 struct lxc_netdev
*netdev
= iterator
->elem
;
3352 if (netdev
->type
== LXC_NET_NONE
)
3357 if (found_none
&& !found_nic
)
3362 /* try to move physical nics to the init netns */
3363 int lxc_restore_phys_nics_to_netns(struct lxc_handler
*handler
)
3367 char ifname
[IFNAMSIZ
];
3368 struct lxc_list
*iterator
;
3369 int netnsfd
= handler
->nsfd
[LXC_NS_NET
];
3370 struct lxc_conf
*conf
= handler
->conf
;
3372 /* We need CAP_NET_ADMIN in the parent namespace in order to setns() to
3373 * the parent network namespace. We won't have this capability if we are
3376 if (!handler
->am_root
)
3379 TRACE("Moving physical network devices back to parent network namespace");
3381 oldfd
= lxc_preserve_ns(handler
->monitor_pid
, "net");
3383 SYSERROR("Failed to preserve network namespace");
3387 ret
= setns(netnsfd
, CLONE_NEWNET
);
3389 SYSERROR("Failed to enter network namespace");
3394 lxc_list_for_each(iterator
, &conf
->network
) {
3395 struct lxc_netdev
*netdev
= iterator
->elem
;
3397 if (netdev
->type
!= LXC_NET_PHYS
)
3400 /* Retrieve the name of the interface in the container's network
3403 if (!if_indextoname(netdev
->ifindex
, ifname
)) {
3404 WARN("No interface corresponding to ifindex %d",
3409 ret
= lxc_netdev_move_by_index_fd(netdev
->ifindex
, oldfd
, netdev
->link
);
3411 WARN("Error moving network device \"%s\" back to "
3412 "network namespace", ifname
);
3414 TRACE("Moved network device \"%s\" back to network "
3415 "namespace", ifname
);
3418 ret
= setns(oldfd
, CLONE_NEWNET
);
3421 SYSERROR("Failed to enter network namespace");
3428 static int setup_hw_addr(char *hwaddr
, const char *ifname
)
3430 struct sockaddr sockaddr
;
3434 ret
= lxc_convert_mac(hwaddr
, &sockaddr
);
3437 SYSERROR("Mac address \"%s\" conversion failed", hwaddr
);
3441 memcpy(ifr
.ifr_name
, ifname
, IFNAMSIZ
);
3442 ifr
.ifr_name
[IFNAMSIZ
-1] = '\0';
3443 memcpy((char *) &ifr
.ifr_hwaddr
, (char *) &sockaddr
, sizeof(sockaddr
));
3445 fd
= socket(AF_INET
, SOCK_DGRAM
| SOCK_CLOEXEC
, 0);
3449 ret
= ioctl(fd
, SIOCSIFHWADDR
, &ifr
);
3451 SYSERROR("Failed to perform ioctl");
3455 DEBUG("Mac address \"%s\" on \"%s\" has been setup", hwaddr
,
3461 static int setup_ipv4_addr(struct lxc_list
*ip
, int ifindex
)
3463 struct lxc_list
*iterator
;
3466 lxc_list_for_each(iterator
, ip
) {
3467 struct lxc_inetdev
*inetdev
= iterator
->elem
;
3469 err
= lxc_ipv4_addr_add(ifindex
, &inetdev
->addr
,
3470 &inetdev
->bcast
, inetdev
->prefix
);
3473 SYSERROR("Failed to setup ipv4 address for network device "
3474 "with ifindex %d", ifindex
);
3482 static int setup_ipv6_addr(struct lxc_list
*ip
, int ifindex
)
3484 struct lxc_list
*iterator
;
3487 lxc_list_for_each(iterator
, ip
) {
3488 struct lxc_inet6dev
*inet6dev
= iterator
->elem
;
3490 err
= lxc_ipv6_addr_add(ifindex
, &inet6dev
->addr
,
3491 &inet6dev
->mcast
, &inet6dev
->acast
,
3495 SYSERROR("Failed to setup ipv6 address for network device "
3496 "with ifindex %d", ifindex
);
3504 static int lxc_setup_netdev_in_child_namespaces(struct lxc_netdev
*netdev
)
3506 char ifname
[IFNAMSIZ
];
3508 char *current_ifname
= ifname
;
3509 char bufinet4
[INET_ADDRSTRLEN
], bufinet6
[INET6_ADDRSTRLEN
];
3511 /* empty network namespace */
3512 if (!netdev
->ifindex
) {
3513 if (netdev
->flags
& IFF_UP
) {
3514 err
= lxc_netdev_up("lo");
3517 SYSERROR("Failed to set the loopback network device up");
3522 if (netdev
->type
== LXC_NET_EMPTY
)
3525 if (netdev
->type
== LXC_NET_NONE
)
3528 netdev
->ifindex
= if_nametoindex(netdev
->created_name
);
3529 if (!netdev
->ifindex
)
3530 SYSERROR("Failed to retrieve ifindex for network device with name %s",
3531 netdev
->name
?: "(null)");
3534 /* get the new ifindex in case of physical netdev */
3535 if (netdev
->type
== LXC_NET_PHYS
) {
3536 netdev
->ifindex
= if_nametoindex(netdev
->link
);
3537 if (!netdev
->ifindex
) {
3538 ERROR("Failed to get ifindex for network device \"%s\"",
3544 /* retrieve the name of the interface */
3545 if (!if_indextoname(netdev
->ifindex
, current_ifname
)) {
3546 SYSERROR("Failed to retrieve name for network device with ifindex %d",
3551 /* Default: let the system choose an interface name.
3552 * When the IFLA_IFNAME attribute is passed something like "<prefix>%d"
3553 * netlink will replace the format specifier with an appropriate index.
3555 if (netdev
->name
[0] == '\0') {
3556 if (netdev
->type
== LXC_NET_PHYS
)
3557 (void)strlcpy(netdev
->name
, netdev
->link
, IFNAMSIZ
);
3559 (void)strlcpy(netdev
->name
, "eth%d", IFNAMSIZ
);
3562 /* rename the interface name */
3563 if (strcmp(current_ifname
, netdev
->name
) != 0) {
3564 err
= lxc_netdev_rename_by_name(current_ifname
, netdev
->name
);
3567 SYSERROR("Failed to rename network device \"%s\" to \"%s\"",
3568 current_ifname
, netdev
->name
);
3572 TRACE("Renamed network device from \"%s\" to \"%s\"",
3573 current_ifname
, netdev
->name
);
3576 /* Re-read the name of the interface because its name has changed
3577 * and would be automatically allocated by the system
3579 if (!if_indextoname(netdev
->ifindex
, current_ifname
)) {
3580 ERROR("Failed get name for network device with ifindex %d",
3585 /* Now update the recorded name of the network device to reflect the
3586 * name of the network device in the child's network namespace. We will
3587 * later on send this information back to the parent.
3589 (void)strlcpy(netdev
->name
, current_ifname
, IFNAMSIZ
);
3591 /* set a mac address */
3592 if (netdev
->hwaddr
) {
3593 if (setup_hw_addr(netdev
->hwaddr
, current_ifname
)) {
3594 ERROR("Failed to setup hw address for network device \"%s\"",
3600 /* setup ipv4 addresses on the interface */
3601 if (setup_ipv4_addr(&netdev
->ipv4
, netdev
->ifindex
)) {
3602 ERROR("Failed to setup ip addresses for network device \"%s\"",
3607 /* setup ipv6 addresses on the interface */
3608 if (setup_ipv6_addr(&netdev
->ipv6
, netdev
->ifindex
)) {
3609 ERROR("Failed to setup ipv6 addresses for network device \"%s\"",
3614 /* set the network device up */
3615 if (netdev
->flags
& IFF_UP
) {
3616 err
= lxc_netdev_up(current_ifname
);
3619 SYSERROR("Failed to set network device \"%s\" up",
3624 /* the network is up, make the loopback up too */
3625 err
= lxc_netdev_up("lo");
3628 SYSERROR("Failed to set the loopback network device up");
3633 /* setup ipv4 gateway on the interface */
3634 if (netdev
->ipv4_gateway
|| netdev
->ipv4_gateway_dev
) {
3635 if (!(netdev
->flags
& IFF_UP
)) {
3636 ERROR("Cannot add ipv4 gateway for network device "
3637 "\"%s\" when not bringing up the interface", current_ifname
);
3641 if (lxc_list_empty(&netdev
->ipv4
)) {
3642 ERROR("Cannot add ipv4 gateway for network device "
3643 "\"%s\" when not assigning an address", current_ifname
);
3647 /* Setup device route if ipv4_gateway_dev is enabled */
3648 if (netdev
->ipv4_gateway_dev
) {
3649 err
= lxc_ipv4_gateway_add(netdev
->ifindex
, NULL
);
3651 SYSERROR("Failed to setup ipv4 gateway to network device \"%s\"",
3653 return minus_one_set_errno(-err
);
3656 /* Check the gateway address is valid */
3657 if (!inet_ntop(AF_INET
, netdev
->ipv4_gateway
, bufinet4
, sizeof(bufinet4
)))
3658 return minus_one_set_errno(errno
);
3660 /* Try adding a default route to the gateway address */
3661 err
= lxc_ipv4_gateway_add(netdev
->ifindex
, netdev
->ipv4_gateway
);
3663 /* If adding the default route fails, this could be because the
3664 * gateway address is in a different subnet to the container's address.
3665 * To work around this, we try adding a static device route to the
3666 * gateway address first, and then try again.
3668 err
= lxc_ipv4_dest_add(netdev
->ifindex
, netdev
->ipv4_gateway
, 32);
3671 SYSERROR("Failed to add ipv4 dest \"%s\" for network device \"%s\"",
3672 bufinet4
, current_ifname
);
3676 err
= lxc_ipv4_gateway_add(netdev
->ifindex
, netdev
->ipv4_gateway
);
3679 SYSERROR("Failed to setup ipv4 gateway \"%s\" for network device \"%s\"",
3680 bufinet4
, current_ifname
);
3687 /* setup ipv6 gateway on the interface */
3688 if (netdev
->ipv6_gateway
|| netdev
->ipv6_gateway_dev
) {
3689 if (!(netdev
->flags
& IFF_UP
)) {
3690 ERROR("Cannot add ipv6 gateway for network device \"%s\" when not bringing up the interface",
3695 if (lxc_list_empty(&netdev
->ipv6
) && !IN6_IS_ADDR_LINKLOCAL(netdev
->ipv6_gateway
)) {
3696 ERROR("Cannot add ipv6 gateway for network device \"%s\" when not assigning an address",
3701 /* Setup device route if ipv6_gateway_dev is enabled */
3702 if (netdev
->ipv6_gateway_dev
) {
3703 err
= lxc_ipv6_gateway_add(netdev
->ifindex
, NULL
);
3705 SYSERROR("Failed to setup ipv6 gateway to network device \"%s\"",
3707 return minus_one_set_errno(-err
);
3710 /* Check the gateway address is valid */
3711 if (!inet_ntop(AF_INET6
, netdev
->ipv6_gateway
, bufinet6
, sizeof(bufinet6
)))
3712 return minus_one_set_errno(errno
);
3714 /* Try adding a default route to the gateway address */
3715 err
= lxc_ipv6_gateway_add(netdev
->ifindex
, netdev
->ipv6_gateway
);
3717 /* If adding the default route fails, this could be because the
3718 * gateway address is in a different subnet to the container's address.
3719 * To work around this, we try adding a static device route to the
3720 * gateway address first, and then try again.
3722 err
= lxc_ipv6_dest_add(netdev
->ifindex
, netdev
->ipv6_gateway
, 128);
3725 SYSERROR("Failed to add ipv6 dest \"%s\" for network device \"%s\"",
3726 bufinet6
, current_ifname
);
3730 err
= lxc_ipv6_gateway_add(netdev
->ifindex
, netdev
->ipv6_gateway
);
3733 SYSERROR("Failed to setup ipv6 gateway \"%s\" for network device \"%s\"",
3734 bufinet6
, current_ifname
);
3741 DEBUG("Network device \"%s\" has been setup", current_ifname
);
3746 int lxc_setup_network_in_child_namespaces(const struct lxc_conf
*conf
,
3747 struct lxc_list
*network
)
3749 struct lxc_list
*iterator
;
3751 lxc_list_for_each(iterator
, network
) {
3752 struct lxc_netdev
*netdev
= iterator
->elem
;
3754 if (lxc_setup_netdev_in_child_namespaces(netdev
)) {
3755 ERROR("Failed to setup netdev");
3760 if (!lxc_list_empty(network
))
3761 INFO("Network has been setup");
3766 int lxc_network_send_veth_names_to_child(struct lxc_handler
*handler
)
3768 struct lxc_list
*iterator
;
3769 struct lxc_list
*network
= &handler
->conf
->network
;
3770 int data_sock
= handler
->data_sock
[0];
3772 lxc_list_for_each(iterator
, network
) {
3774 struct lxc_netdev
*netdev
= iterator
->elem
;
3776 if (netdev
->type
!= LXC_NET_VETH
)
3779 ret
= lxc_send_nointr(data_sock
, netdev
->name
, IFNAMSIZ
, MSG_NOSIGNAL
);
3783 ret
= lxc_send_nointr(data_sock
, netdev
->created_name
, IFNAMSIZ
, MSG_NOSIGNAL
);
3787 TRACE("Sent network device name \"%s\" to child", netdev
->created_name
);
3793 int lxc_network_recv_veth_names_from_parent(struct lxc_handler
*handler
)
3795 struct lxc_list
*iterator
;
3796 struct lxc_list
*network
= &handler
->conf
->network
;
3797 int data_sock
= handler
->data_sock
[1];
3799 lxc_list_for_each(iterator
, network
) {
3801 struct lxc_netdev
*netdev
= iterator
->elem
;
3803 if (netdev
->type
!= LXC_NET_VETH
)
3806 ret
= lxc_recv_nointr(data_sock
, netdev
->name
, IFNAMSIZ
, 0);
3810 ret
= lxc_recv_nointr(data_sock
, netdev
->created_name
, IFNAMSIZ
, 0);
3813 TRACE("Received network device name \"%s\" from parent", netdev
->created_name
);
3819 int lxc_network_send_name_and_ifindex_to_parent(struct lxc_handler
*handler
)
3821 struct lxc_list
*iterator
, *network
;
3822 int data_sock
= handler
->data_sock
[0];
3824 if (!handler
->am_root
)
3827 network
= &handler
->conf
->network
;
3828 lxc_list_for_each(iterator
, network
) {
3830 struct lxc_netdev
*netdev
= iterator
->elem
;
3832 /* Send network device name in the child's namespace to parent. */
3833 ret
= lxc_send_nointr(data_sock
, netdev
->name
, IFNAMSIZ
, MSG_NOSIGNAL
);
3837 /* Send network device ifindex in the child's namespace to
3840 ret
= lxc_send_nointr(data_sock
, &netdev
->ifindex
, sizeof(netdev
->ifindex
), MSG_NOSIGNAL
);
3845 if (!lxc_list_empty(network
))
3846 TRACE("Sent network device names and ifindices to parent");
3851 int lxc_network_recv_name_and_ifindex_from_child(struct lxc_handler
*handler
)
3853 struct lxc_list
*iterator
, *network
;
3854 int data_sock
= handler
->data_sock
[1];
3856 if (!handler
->am_root
)
3859 network
= &handler
->conf
->network
;
3860 lxc_list_for_each(iterator
, network
) {
3862 struct lxc_netdev
*netdev
= iterator
->elem
;
3864 /* Receive network device name in the child's namespace to
3867 ret
= lxc_recv_nointr(data_sock
, netdev
->name
, IFNAMSIZ
, 0);
3871 /* Receive network device ifindex in the child's namespace to
3874 ret
= lxc_recv_nointr(data_sock
, &netdev
->ifindex
, sizeof(netdev
->ifindex
), 0);
3882 void lxc_delete_network(struct lxc_handler
*handler
)
3886 if (handler
->am_root
)
3887 bret
= lxc_delete_network_priv(handler
);
3889 bret
= lxc_delete_network_unpriv(handler
);
3891 DEBUG("Failed to delete network devices");
3893 DEBUG("Deleted network devices");
3896 int lxc_netns_set_nsid(int fd
)
3899 char buf
[NLMSG_ALIGN(sizeof(struct nlmsghdr
)) +
3900 NLMSG_ALIGN(sizeof(struct rtgenmsg
)) +
3902 struct nl_handler nlh
;
3903 struct nlmsghdr
*hdr
;
3904 struct rtgenmsg
*msg
;
3906 const __s32 ns_id
= -1;
3907 const __u32 netns_fd
= fd
;
3909 ret
= netlink_open(&nlh
, NETLINK_ROUTE
);
3913 memset(buf
, 0, sizeof(buf
));
3915 #pragma GCC diagnostic push
3916 #pragma GCC diagnostic ignored "-Wcast-align"
3917 hdr
= (struct nlmsghdr
*)buf
;
3918 msg
= (struct rtgenmsg
*)NLMSG_DATA(hdr
);
3919 #pragma GCC diagnostic pop
3921 hdr
->nlmsg_len
= NLMSG_LENGTH(sizeof(*msg
));
3922 hdr
->nlmsg_type
= RTM_NEWNSID
;
3923 hdr
->nlmsg_flags
= NLM_F_REQUEST
| NLM_F_ACK
;
3925 hdr
->nlmsg_seq
= RTM_NEWNSID
;
3926 msg
->rtgen_family
= AF_UNSPEC
;
3928 ret
= addattr(hdr
, 1024, __LXC_NETNSA_FD
, &netns_fd
, sizeof(netns_fd
));
3932 ret
= addattr(hdr
, 1024, __LXC_NETNSA_NSID
, &ns_id
, sizeof(ns_id
));
3936 ret
= __netlink_transaction(&nlh
, hdr
, hdr
);
3939 saved_errno
= errno
;
3940 netlink_close(&nlh
);
3941 errno
= saved_errno
;
3946 static int parse_rtattr(struct rtattr
*tb
[], int max
, struct rtattr
*rta
, int len
)
3949 memset(tb
, 0, sizeof(struct rtattr
*) * (max
+ 1));
3951 while (RTA_OK(rta
, len
)) {
3952 unsigned short type
= rta
->rta_type
;
3954 if ((type
<= max
) && (!tb
[type
]))
3957 #pragma GCC diagnostic push
3958 #pragma GCC diagnostic ignored "-Wcast-align"
3959 rta
= RTA_NEXT(rta
, len
);
3960 #pragma GCC diagnostic pop
3966 static inline __s32
rta_getattr_s32(const struct rtattr
*rta
)
3968 return *(__s32
*)RTA_DATA(rta
);
3972 #define NETNS_RTA(r) \
3973 ((struct rtattr *)(((char *)(r)) + NLMSG_ALIGN(sizeof(struct rtgenmsg))))
3976 int lxc_netns_get_nsid(int fd
)
3980 char buf
[NLMSG_ALIGN(sizeof(struct nlmsghdr
)) +
3981 NLMSG_ALIGN(sizeof(struct rtgenmsg
)) +
3983 struct rtattr
*tb
[__LXC_NETNSA_MAX
+ 1];
3984 struct nl_handler nlh
;
3985 struct nlmsghdr
*hdr
;
3986 struct rtgenmsg
*msg
;
3988 __u32 netns_fd
= fd
;
3990 ret
= netlink_open(&nlh
, NETLINK_ROUTE
);
3994 memset(buf
, 0, sizeof(buf
));
3996 #pragma GCC diagnostic push
3997 #pragma GCC diagnostic ignored "-Wcast-align"
3998 hdr
= (struct nlmsghdr
*)buf
;
3999 msg
= (struct rtgenmsg
*)NLMSG_DATA(hdr
);
4000 #pragma GCC diagnostic pop
4002 hdr
->nlmsg_len
= NLMSG_LENGTH(sizeof(*msg
));
4003 hdr
->nlmsg_type
= RTM_GETNSID
;
4004 hdr
->nlmsg_flags
= NLM_F_REQUEST
| NLM_F_ACK
;
4006 hdr
->nlmsg_seq
= RTM_GETNSID
;
4007 msg
->rtgen_family
= AF_UNSPEC
;
4009 ret
= addattr(hdr
, 1024, __LXC_NETNSA_FD
, &netns_fd
, sizeof(netns_fd
));
4011 ret
= __netlink_transaction(&nlh
, hdr
, hdr
);
4013 saved_errno
= errno
;
4014 netlink_close(&nlh
);
4015 errno
= saved_errno
;
4020 msg
= NLMSG_DATA(hdr
);
4021 len
= hdr
->nlmsg_len
- NLMSG_SPACE(sizeof(*msg
));
4025 #pragma GCC diagnostic push
4026 #pragma GCC diagnostic ignored "-Wcast-align"
4027 parse_rtattr(tb
, __LXC_NETNSA_MAX
, NETNS_RTA(msg
), len
);
4028 if (tb
[__LXC_NETNSA_NSID
])
4029 return rta_getattr_s32(tb
[__LXC_NETNSA_NSID
]);
4030 #pragma GCC diagnostic pop
4035 int lxc_create_network(struct lxc_handler
*handler
)
4040 * Find gateway addresses from the link device, which is no longer
4041 * accessible inside the container. Do this before creating network
4042 * interfaces, since goto out_delete_net does not work before
4045 ret
= lxc_find_gateway_addresses(handler
);
4047 ERROR("Failed to find gateway addresses");
4051 if (handler
->am_root
) {
4052 ret
= lxc_create_network_priv(handler
);
4056 return lxc_network_move_created_netdev_priv(handler
);
4059 return lxc_create_network_unpriv(handler
);