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");
655 /* Note that we're retrieving the container's ifindex in the host's
656 * network namespace because we need it to move the device from the
657 * host's network namespace to the container's network namespace later
659 * Note that netdev->link will contain the name of the physical network
660 * device in the host's namespace.
662 netdev
->ifindex
= if_nametoindex(netdev
->link
);
663 if (!netdev
->ifindex
) {
664 ERROR("Failed to retrieve ifindex for \"%s\"", netdev
->link
);
668 /* Store the ifindex of the host's network device in the host's
671 netdev
->priv
.phys_attr
.ifindex
= netdev
->ifindex
;
673 /* Get original device MTU setting and store for restoration after container shutdown. */
674 mtu_orig
= netdev_get_mtu(netdev
->ifindex
);
676 SYSERROR("Failed to get original mtu for interface \"%s\"", netdev
->link
);
677 return minus_one_set_errno(-mtu_orig
);
680 netdev
->priv
.phys_attr
.mtu
= mtu_orig
;
683 err
= lxc_safe_uint(netdev
->mtu
, &mtu
);
686 SYSERROR("Failed to parse mtu \"%s\" for interface \"%s\"", netdev
->mtu
, netdev
->link
);
690 err
= lxc_netdev_set_mtu(netdev
->link
, mtu
);
693 SYSERROR("Failed to set mtu \"%s\" for interface \"%s\"", netdev
->mtu
, netdev
->link
);
698 if (netdev
->upscript
) {
705 err
= run_script_argv(handler
->name
,
706 handler
->conf
->hooks_version
, "net",
707 netdev
->upscript
, "up", argv
);
713 DEBUG("Instantiated phys \"%s\" with ifindex is \"%d\"", netdev
->link
, netdev
->ifindex
);
718 static int instantiate_empty(struct lxc_handler
*handler
, struct lxc_netdev
*netdev
)
727 if (!netdev
->upscript
)
730 ret
= run_script_argv(handler
->name
, handler
->conf
->hooks_version
,
731 "net", netdev
->upscript
, "up", argv
);
738 static int instantiate_none(struct lxc_handler
*handler
, struct lxc_netdev
*netdev
)
744 static instantiate_cb netdev_conf
[LXC_NET_MAXCONFTYPE
+ 1] = {
745 [LXC_NET_VETH
] = instantiate_veth
,
746 [LXC_NET_MACVLAN
] = instantiate_macvlan
,
747 [LXC_NET_IPVLAN
] = instantiate_ipvlan
,
748 [LXC_NET_VLAN
] = instantiate_vlan
,
749 [LXC_NET_PHYS
] = instantiate_phys
,
750 [LXC_NET_EMPTY
] = instantiate_empty
,
751 [LXC_NET_NONE
] = instantiate_none
,
754 static int shutdown_veth(struct lxc_handler
*handler
, struct lxc_netdev
*netdev
)
764 if (!netdev
->downscript
)
767 if (netdev
->priv
.veth_attr
.pair
[0] != '\0')
768 argv
[2] = netdev
->priv
.veth_attr
.pair
;
770 argv
[2] = netdev
->priv
.veth_attr
.veth1
;
772 ret
= run_script_argv(handler
->name
,
773 handler
->conf
->hooks_version
, "net",
774 netdev
->downscript
, "down", argv
);
781 static int shutdown_macvlan(struct lxc_handler
*handler
, struct lxc_netdev
*netdev
)
790 if (!netdev
->downscript
)
793 ret
= run_script_argv(handler
->name
, handler
->conf
->hooks_version
,
794 "net", netdev
->downscript
, "down", argv
);
801 static int shutdown_ipvlan(struct lxc_handler
*handler
, struct lxc_netdev
*netdev
)
810 if (!netdev
->downscript
)
813 ret
= run_script_argv(handler
->name
, handler
->conf
->hooks_version
,
814 "net", netdev
->downscript
, "down", argv
);
821 static int shutdown_vlan(struct lxc_handler
*handler
, struct lxc_netdev
*netdev
)
830 if (!netdev
->downscript
)
833 ret
= run_script_argv(handler
->name
, handler
->conf
->hooks_version
,
834 "net", netdev
->downscript
, "down", argv
);
841 static int shutdown_phys(struct lxc_handler
*handler
, struct lxc_netdev
*netdev
)
850 if (!netdev
->downscript
)
853 ret
= run_script_argv(handler
->name
, handler
->conf
->hooks_version
,
854 "net", netdev
->downscript
, "down", argv
);
861 static int shutdown_empty(struct lxc_handler
*handler
, struct lxc_netdev
*netdev
)
869 if (!netdev
->downscript
)
872 ret
= run_script_argv(handler
->name
, handler
->conf
->hooks_version
,
873 "net", netdev
->downscript
, "down", argv
);
880 static int shutdown_none(struct lxc_handler
*handler
, struct lxc_netdev
*netdev
)
885 static instantiate_cb netdev_deconf
[LXC_NET_MAXCONFTYPE
+ 1] = {
886 [LXC_NET_VETH
] = shutdown_veth
,
887 [LXC_NET_MACVLAN
] = shutdown_macvlan
,
888 [LXC_NET_IPVLAN
] = shutdown_ipvlan
,
889 [LXC_NET_VLAN
] = shutdown_vlan
,
890 [LXC_NET_PHYS
] = shutdown_phys
,
891 [LXC_NET_EMPTY
] = shutdown_empty
,
892 [LXC_NET_NONE
] = shutdown_none
,
895 static int lxc_netdev_move_by_index_fd(int ifindex
, int fd
, const char *ifname
)
898 struct nl_handler nlh
;
899 struct ifinfomsg
*ifi
;
900 struct nlmsg
*nlmsg
= NULL
;
902 err
= netlink_open(&nlh
, NETLINK_ROUTE
);
907 nlmsg
= nlmsg_alloc(NLMSG_GOOD_SIZE
);
911 nlmsg
->nlmsghdr
->nlmsg_flags
= NLM_F_REQUEST
| NLM_F_ACK
;
912 nlmsg
->nlmsghdr
->nlmsg_type
= RTM_NEWLINK
;
914 ifi
= nlmsg_reserve(nlmsg
, sizeof(struct ifinfomsg
));
917 ifi
->ifi_family
= AF_UNSPEC
;
918 ifi
->ifi_index
= ifindex
;
920 if (nla_put_u32(nlmsg
, IFLA_NET_NS_FD
, fd
))
923 if (ifname
!= NULL
) {
924 if (nla_put_string(nlmsg
, IFLA_IFNAME
, ifname
))
928 err
= netlink_transaction(&nlh
, nlmsg
, nlmsg
);
935 int lxc_netdev_move_by_index(int ifindex
, pid_t pid
, const char *ifname
)
938 struct nl_handler nlh
;
939 struct ifinfomsg
*ifi
;
940 struct nlmsg
*nlmsg
= NULL
;
942 err
= netlink_open(&nlh
, NETLINK_ROUTE
);
947 nlmsg
= nlmsg_alloc(NLMSG_GOOD_SIZE
);
951 nlmsg
->nlmsghdr
->nlmsg_flags
= NLM_F_REQUEST
| NLM_F_ACK
;
952 nlmsg
->nlmsghdr
->nlmsg_type
= RTM_NEWLINK
;
954 ifi
= nlmsg_reserve(nlmsg
, sizeof(struct ifinfomsg
));
957 ifi
->ifi_family
= AF_UNSPEC
;
958 ifi
->ifi_index
= ifindex
;
960 if (nla_put_u32(nlmsg
, IFLA_NET_NS_PID
, pid
))
963 if (ifname
!= NULL
) {
964 if (nla_put_string(nlmsg
, IFLA_IFNAME
, ifname
))
968 err
= netlink_transaction(&nlh
, nlmsg
, nlmsg
);
975 /* If we are asked to move a wireless interface, then we must actually move its
976 * phyN device. Detect that condition and return the physname here. The physname
977 * will be passed to lxc_netdev_move_wlan() which will free it when done.
979 #define PHYSNAME "/sys/class/net/%s/phy80211/name"
980 static char *is_wlan(const char *ifname
)
982 __do_free
char *path
= NULL
;
987 char *physname
= NULL
;
989 len
= strlen(ifname
) + strlen(PHYSNAME
) - 1;
990 path
= must_realloc(NULL
, len
+ 1);
991 ret
= snprintf(path
, len
, PHYSNAME
, ifname
);
992 if (ret
< 0 || (size_t)ret
>= len
)
995 f
= fopen(path
, "r");
999 /* Feh - sb.st_size is always 4096. */
1000 fseek(f
, 0, SEEK_END
);
1002 fseek(f
, 0, SEEK_SET
);
1008 physname
= malloc(physlen
+ 1);
1014 memset(physname
, 0, physlen
+ 1);
1015 ret
= fread(physname
, 1, physlen
, f
);
1020 for (i
= 0; i
< physlen
; i
++) {
1021 if (physname
[i
] == '\n')
1024 if (physname
[i
] == '\0')
1035 static int lxc_netdev_rename_by_name_in_netns(pid_t pid
, const char *old
,
1045 return wait_for_pid(fpid
);
1047 if (!switch_to_ns(pid
, "net"))
1050 _exit(lxc_netdev_rename_by_name(old
, new));
1053 static int lxc_netdev_move_wlan(char *physname
, const char *ifname
, pid_t pid
,
1054 const char *newname
)
1060 /* Move phyN into the container. TODO - do this using netlink.
1061 * However, IIUC this involves a bit more complicated work to talk to
1062 * the 80211 module, so for now just call out to iw.
1064 cmd
= on_path("iw", NULL
);
1075 sprintf(pidstr
, "%d", pid
);
1076 execlp("iw", "iw", "phy", physname
, "set", "netns", pidstr
,
1078 _exit(EXIT_FAILURE
);
1081 if (wait_for_pid(fpid
))
1086 err
= lxc_netdev_rename_by_name_in_netns(pid
, ifname
, newname
);
1093 int lxc_netdev_move_by_name(const char *ifname
, pid_t pid
, const char* newname
)
1101 index
= if_nametoindex(ifname
);
1105 physname
= is_wlan(ifname
);
1107 return lxc_netdev_move_wlan(physname
, ifname
, pid
, newname
);
1109 return lxc_netdev_move_by_index(index
, pid
, newname
);
1112 int lxc_netdev_delete_by_index(int ifindex
)
1115 struct ifinfomsg
*ifi
;
1116 struct nl_handler nlh
;
1117 struct nlmsg
*answer
= NULL
, *nlmsg
= NULL
;
1119 err
= netlink_open(&nlh
, NETLINK_ROUTE
);
1124 nlmsg
= nlmsg_alloc(NLMSG_GOOD_SIZE
);
1128 answer
= nlmsg_alloc_reserve(NLMSG_GOOD_SIZE
);
1132 nlmsg
->nlmsghdr
->nlmsg_flags
= NLM_F_ACK
| NLM_F_REQUEST
;
1133 nlmsg
->nlmsghdr
->nlmsg_type
= RTM_DELLINK
;
1135 ifi
= nlmsg_reserve(nlmsg
, sizeof(struct ifinfomsg
));
1138 ifi
->ifi_family
= AF_UNSPEC
;
1139 ifi
->ifi_index
= ifindex
;
1141 err
= netlink_transaction(&nlh
, nlmsg
, answer
);
1143 netlink_close(&nlh
);
1149 int lxc_netdev_delete_by_name(const char *name
)
1153 index
= if_nametoindex(name
);
1157 return lxc_netdev_delete_by_index(index
);
1160 int lxc_netdev_rename_by_index(int ifindex
, const char *newname
)
1163 struct ifinfomsg
*ifi
;
1164 struct nl_handler nlh
;
1165 struct nlmsg
*answer
= NULL
, *nlmsg
= NULL
;
1167 err
= netlink_open(&nlh
, NETLINK_ROUTE
);
1171 len
= strlen(newname
);
1172 if (len
== 1 || len
>= IFNAMSIZ
) {
1178 nlmsg
= nlmsg_alloc(NLMSG_GOOD_SIZE
);
1182 answer
= nlmsg_alloc_reserve(NLMSG_GOOD_SIZE
);
1186 nlmsg
->nlmsghdr
->nlmsg_flags
= NLM_F_ACK
| NLM_F_REQUEST
;
1187 nlmsg
->nlmsghdr
->nlmsg_type
= RTM_NEWLINK
;
1189 ifi
= nlmsg_reserve(nlmsg
, sizeof(struct ifinfomsg
));
1192 ifi
->ifi_family
= AF_UNSPEC
;
1193 ifi
->ifi_index
= ifindex
;
1195 if (nla_put_string(nlmsg
, IFLA_IFNAME
, newname
))
1198 err
= netlink_transaction(&nlh
, nlmsg
, answer
);
1200 netlink_close(&nlh
);
1206 int lxc_netdev_rename_by_name(const char *oldname
, const char *newname
)
1210 len
= strlen(oldname
);
1211 if (len
== 1 || len
>= IFNAMSIZ
)
1214 index
= if_nametoindex(oldname
);
1218 return lxc_netdev_rename_by_index(index
, newname
);
1221 int netdev_set_flag(const char *name
, int flag
)
1223 int err
, index
, len
;
1224 struct ifinfomsg
*ifi
;
1225 struct nl_handler nlh
;
1226 struct nlmsg
*answer
= NULL
, *nlmsg
= NULL
;
1228 err
= netlink_open(&nlh
, NETLINK_ROUTE
);
1234 if (len
== 1 || len
>= IFNAMSIZ
)
1238 nlmsg
= nlmsg_alloc(NLMSG_GOOD_SIZE
);
1242 answer
= nlmsg_alloc_reserve(NLMSG_GOOD_SIZE
);
1247 index
= if_nametoindex(name
);
1251 nlmsg
->nlmsghdr
->nlmsg_flags
= NLM_F_REQUEST
| NLM_F_ACK
;
1252 nlmsg
->nlmsghdr
->nlmsg_type
= RTM_NEWLINK
;
1254 ifi
= nlmsg_reserve(nlmsg
, sizeof(struct ifinfomsg
));
1259 ifi
->ifi_family
= AF_UNSPEC
;
1260 ifi
->ifi_index
= index
;
1261 ifi
->ifi_change
|= IFF_UP
;
1262 ifi
->ifi_flags
|= flag
;
1264 err
= netlink_transaction(&nlh
, nlmsg
, answer
);
1266 netlink_close(&nlh
);
1272 int netdev_get_flag(const char *name
, int *flag
)
1274 int err
, index
, len
;
1275 struct ifinfomsg
*ifi
;
1276 struct nl_handler nlh
;
1277 struct nlmsg
*answer
= NULL
, *nlmsg
= NULL
;
1282 err
= netlink_open(&nlh
, NETLINK_ROUTE
);
1288 if (len
== 1 || len
>= IFNAMSIZ
)
1292 nlmsg
= nlmsg_alloc(NLMSG_GOOD_SIZE
);
1296 answer
= nlmsg_alloc_reserve(NLMSG_GOOD_SIZE
);
1301 index
= if_nametoindex(name
);
1305 nlmsg
->nlmsghdr
->nlmsg_flags
= NLM_F_REQUEST
;
1306 nlmsg
->nlmsghdr
->nlmsg_type
= RTM_GETLINK
;
1308 ifi
= nlmsg_reserve(nlmsg
, sizeof(struct ifinfomsg
));
1313 ifi
->ifi_family
= AF_UNSPEC
;
1314 ifi
->ifi_index
= index
;
1316 err
= netlink_transaction(&nlh
, nlmsg
, answer
);
1320 ifi
= NLMSG_DATA(answer
->nlmsghdr
);
1322 *flag
= ifi
->ifi_flags
;
1324 netlink_close(&nlh
);
1331 * \brief Check a interface is up or not.
1333 * \param name: name for the interface.
1336 * 0 means interface is down.
1337 * 1 means interface is up.
1338 * Others means error happened, and ret-value is the error number.
1340 int lxc_netdev_isup(const char *name
)
1344 err
= netdev_get_flag(name
, &flag
);
1354 int netdev_get_mtu(int ifindex
)
1356 int answer_len
, err
, res
;
1357 struct nl_handler nlh
;
1358 struct ifinfomsg
*ifi
;
1359 struct nlmsghdr
*msg
;
1360 int readmore
= 0, recv_len
= 0;
1361 struct nlmsg
*answer
= NULL
, *nlmsg
= NULL
;
1363 err
= netlink_open(&nlh
, NETLINK_ROUTE
);
1368 nlmsg
= nlmsg_alloc(NLMSG_GOOD_SIZE
);
1372 answer
= nlmsg_alloc_reserve(NLMSG_GOOD_SIZE
);
1376 /* Save the answer buffer length, since it will be overwritten
1377 * on the first receive (and we might need to receive more than
1380 answer_len
= answer
->nlmsghdr
->nlmsg_len
;
1382 nlmsg
->nlmsghdr
->nlmsg_flags
= NLM_F_REQUEST
| NLM_F_DUMP
;
1383 nlmsg
->nlmsghdr
->nlmsg_type
= RTM_GETLINK
;
1385 ifi
= nlmsg_reserve(nlmsg
, sizeof(struct ifinfomsg
));
1388 ifi
->ifi_family
= AF_UNSPEC
;
1390 /* Send the request for addresses, which returns all addresses
1391 * on all interfaces. */
1392 err
= netlink_send(&nlh
, nlmsg
);
1396 #pragma GCC diagnostic push
1397 #pragma GCC diagnostic ignored "-Wcast-align"
1400 /* Restore the answer buffer length, it might have been
1401 * overwritten by a previous receive.
1403 answer
->nlmsghdr
->nlmsg_len
= answer_len
;
1405 /* Get the (next) batch of reply messages */
1406 err
= netlink_rcv(&nlh
, answer
);
1412 /* Satisfy the typing for the netlink macros */
1413 msg
= answer
->nlmsghdr
;
1415 while (NLMSG_OK(msg
, recv_len
)) {
1417 /* Stop reading if we see an error message */
1418 if (msg
->nlmsg_type
== NLMSG_ERROR
) {
1419 struct nlmsgerr
*errmsg
=
1420 (struct nlmsgerr
*)NLMSG_DATA(msg
);
1421 err
= errmsg
->error
;
1425 /* Stop reading if we see a NLMSG_DONE message */
1426 if (msg
->nlmsg_type
== NLMSG_DONE
) {
1431 ifi
= NLMSG_DATA(msg
);
1432 if (ifi
->ifi_index
== ifindex
) {
1433 struct rtattr
*rta
= IFLA_RTA(ifi
);
1435 msg
->nlmsg_len
- NLMSG_LENGTH(sizeof(*ifi
));
1437 while (RTA_OK(rta
, attr_len
)) {
1438 /* Found a local address for the
1439 * requested interface, return it.
1441 if (rta
->rta_type
== IFLA_MTU
) {
1442 memcpy(&res
, RTA_DATA(rta
),
1447 rta
= RTA_NEXT(rta
, attr_len
);
1451 /* Keep reading more data from the socket if the last
1452 * message had the NLF_F_MULTI flag set.
1454 readmore
= (msg
->nlmsg_flags
& NLM_F_MULTI
);
1456 /* Look at the next message received in this buffer. */
1457 msg
= NLMSG_NEXT(msg
, recv_len
);
1461 #pragma GCC diagnostic pop
1463 /* If we end up here, we didn't find any result, so signal an error. */
1467 netlink_close(&nlh
);
1473 int lxc_netdev_set_mtu(const char *name
, int mtu
)
1475 int err
, index
, len
;
1476 struct ifinfomsg
*ifi
;
1477 struct nl_handler nlh
;
1478 struct nlmsg
*answer
= NULL
, *nlmsg
= NULL
;
1480 err
= netlink_open(&nlh
, NETLINK_ROUTE
);
1486 if (len
== 1 || len
>= IFNAMSIZ
)
1490 nlmsg
= nlmsg_alloc(NLMSG_GOOD_SIZE
);
1494 answer
= nlmsg_alloc_reserve(NLMSG_GOOD_SIZE
);
1499 index
= if_nametoindex(name
);
1503 nlmsg
->nlmsghdr
->nlmsg_flags
= NLM_F_REQUEST
| NLM_F_ACK
;
1504 nlmsg
->nlmsghdr
->nlmsg_type
= RTM_NEWLINK
;
1506 ifi
= nlmsg_reserve(nlmsg
, sizeof(struct ifinfomsg
));
1511 ifi
->ifi_family
= AF_UNSPEC
;
1512 ifi
->ifi_index
= index
;
1514 if (nla_put_u32(nlmsg
, IFLA_MTU
, mtu
))
1517 err
= netlink_transaction(&nlh
, nlmsg
, answer
);
1519 netlink_close(&nlh
);
1525 int lxc_netdev_up(const char *name
)
1527 return netdev_set_flag(name
, IFF_UP
);
1530 int lxc_netdev_down(const char *name
)
1532 return netdev_set_flag(name
, 0);
1535 int lxc_veth_create(const char *name1
, const char *name2
)
1538 struct ifinfomsg
*ifi
;
1539 struct nl_handler nlh
;
1540 struct rtattr
*nest1
, *nest2
, *nest3
;
1541 struct nlmsg
*answer
= NULL
, *nlmsg
= NULL
;
1543 err
= netlink_open(&nlh
, NETLINK_ROUTE
);
1548 len
= strlen(name1
);
1549 if (len
== 1 || len
>= IFNAMSIZ
)
1552 len
= strlen(name2
);
1553 if (len
== 1 || len
>= IFNAMSIZ
)
1557 nlmsg
= nlmsg_alloc(NLMSG_GOOD_SIZE
);
1561 answer
= nlmsg_alloc_reserve(NLMSG_GOOD_SIZE
);
1565 nlmsg
->nlmsghdr
->nlmsg_flags
=
1566 NLM_F_REQUEST
| NLM_F_CREATE
| NLM_F_EXCL
| NLM_F_ACK
;
1567 nlmsg
->nlmsghdr
->nlmsg_type
= RTM_NEWLINK
;
1569 ifi
= nlmsg_reserve(nlmsg
, sizeof(struct ifinfomsg
));
1572 ifi
->ifi_family
= AF_UNSPEC
;
1575 nest1
= nla_begin_nested(nlmsg
, IFLA_LINKINFO
);
1579 if (nla_put_string(nlmsg
, IFLA_INFO_KIND
, "veth"))
1582 nest2
= nla_begin_nested(nlmsg
, IFLA_INFO_DATA
);
1586 nest3
= nla_begin_nested(nlmsg
, VETH_INFO_PEER
);
1590 ifi
= nlmsg_reserve(nlmsg
, sizeof(struct ifinfomsg
));
1596 if (nla_put_string(nlmsg
, IFLA_IFNAME
, name2
))
1599 nla_end_nested(nlmsg
, nest3
);
1600 nla_end_nested(nlmsg
, nest2
);
1601 nla_end_nested(nlmsg
, nest1
);
1603 if (nla_put_string(nlmsg
, IFLA_IFNAME
, name1
))
1606 err
= netlink_transaction(&nlh
, nlmsg
, answer
);
1608 netlink_close(&nlh
);
1614 /* TODO: merge with lxc_macvlan_create */
1615 int lxc_vlan_create(const char *master
, const char *name
, unsigned short vlanid
)
1617 int err
, len
, lindex
;
1618 struct ifinfomsg
*ifi
;
1619 struct nl_handler nlh
;
1620 struct rtattr
*nest
, *nest2
;
1621 struct nlmsg
*answer
= NULL
, *nlmsg
= NULL
;
1623 err
= netlink_open(&nlh
, NETLINK_ROUTE
);
1628 len
= strlen(master
);
1629 if (len
== 1 || len
>= IFNAMSIZ
)
1633 if (len
== 1 || len
>= IFNAMSIZ
)
1637 nlmsg
= nlmsg_alloc(NLMSG_GOOD_SIZE
);
1641 answer
= nlmsg_alloc_reserve(NLMSG_GOOD_SIZE
);
1646 lindex
= if_nametoindex(master
);
1650 nlmsg
->nlmsghdr
->nlmsg_flags
=
1651 NLM_F_REQUEST
| NLM_F_CREATE
| NLM_F_EXCL
| NLM_F_ACK
;
1652 nlmsg
->nlmsghdr
->nlmsg_type
= RTM_NEWLINK
;
1654 ifi
= nlmsg_reserve(nlmsg
, sizeof(struct ifinfomsg
));
1659 ifi
->ifi_family
= AF_UNSPEC
;
1661 nest
= nla_begin_nested(nlmsg
, IFLA_LINKINFO
);
1665 if (nla_put_string(nlmsg
, IFLA_INFO_KIND
, "vlan"))
1668 nest2
= nla_begin_nested(nlmsg
, IFLA_INFO_DATA
);
1672 if (nla_put_u16(nlmsg
, IFLA_VLAN_ID
, vlanid
))
1675 nla_end_nested(nlmsg
, nest2
);
1676 nla_end_nested(nlmsg
, nest
);
1678 if (nla_put_u32(nlmsg
, IFLA_LINK
, lindex
))
1681 if (nla_put_string(nlmsg
, IFLA_IFNAME
, name
))
1684 err
= netlink_transaction(&nlh
, nlmsg
, answer
);
1690 netlink_close(&nlh
);
1694 int lxc_macvlan_create(const char *master
, const char *name
, int mode
)
1696 int err
, index
, len
;
1697 struct ifinfomsg
*ifi
;
1698 struct nl_handler nlh
;
1699 struct rtattr
*nest
, *nest2
;
1700 struct nlmsg
*answer
= NULL
, *nlmsg
= NULL
;
1702 err
= netlink_open(&nlh
, NETLINK_ROUTE
);
1707 len
= strlen(master
);
1708 if (len
== 1 || len
>= IFNAMSIZ
)
1712 if (len
== 1 || len
>= IFNAMSIZ
)
1716 nlmsg
= nlmsg_alloc(NLMSG_GOOD_SIZE
);
1720 answer
= nlmsg_alloc_reserve(NLMSG_GOOD_SIZE
);
1725 index
= if_nametoindex(master
);
1729 nlmsg
->nlmsghdr
->nlmsg_flags
=
1730 NLM_F_REQUEST
| NLM_F_CREATE
| NLM_F_EXCL
| NLM_F_ACK
;
1731 nlmsg
->nlmsghdr
->nlmsg_type
= RTM_NEWLINK
;
1733 ifi
= nlmsg_reserve(nlmsg
, sizeof(struct ifinfomsg
));
1738 ifi
->ifi_family
= AF_UNSPEC
;
1740 nest
= nla_begin_nested(nlmsg
, IFLA_LINKINFO
);
1744 if (nla_put_string(nlmsg
, IFLA_INFO_KIND
, "macvlan"))
1748 nest2
= nla_begin_nested(nlmsg
, IFLA_INFO_DATA
);
1752 if (nla_put_u32(nlmsg
, IFLA_MACVLAN_MODE
, mode
))
1755 nla_end_nested(nlmsg
, nest2
);
1758 nla_end_nested(nlmsg
, nest
);
1760 if (nla_put_u32(nlmsg
, IFLA_LINK
, index
))
1763 if (nla_put_string(nlmsg
, IFLA_IFNAME
, name
))
1766 err
= netlink_transaction(&nlh
, nlmsg
, answer
);
1768 netlink_close(&nlh
);
1774 static int proc_sys_net_write(const char *path
, const char *value
)
1779 fd
= open(path
, O_WRONLY
);
1783 if (lxc_write_nointr(fd
, value
, strlen(value
)) < 0)
1790 static int lxc_is_ip_forwarding_enabled(const char *ifname
, int family
)
1793 char path
[PATH_MAX
];
1796 if (family
!= AF_INET
&& family
!= AF_INET6
)
1797 return minus_one_set_errno(EINVAL
);
1799 ret
= snprintf(path
, PATH_MAX
, "/proc/sys/net/%s/conf/%s/%s",
1800 family
== AF_INET
? "ipv4" : "ipv6", ifname
,
1802 if (ret
< 0 || (size_t)ret
>= PATH_MAX
)
1803 return minus_one_set_errno(E2BIG
);
1805 return lxc_read_file_expect(path
, buf
, 1, "1");
1808 static int neigh_proxy_set(const char *ifname
, int family
, int flag
)
1811 char path
[PATH_MAX
];
1813 if (family
!= AF_INET
&& family
!= AF_INET6
)
1816 ret
= snprintf(path
, PATH_MAX
, "/proc/sys/net/%s/conf/%s/%s",
1817 family
== AF_INET
? "ipv4" : "ipv6", ifname
,
1818 family
== AF_INET
? "proxy_arp" : "proxy_ndp");
1819 if (ret
< 0 || (size_t)ret
>= PATH_MAX
)
1822 return proc_sys_net_write(path
, flag
? "1" : "0");
1825 static int lxc_is_ip_neigh_proxy_enabled(const char *ifname
, int family
)
1828 char path
[PATH_MAX
];
1831 if (family
!= AF_INET
&& family
!= AF_INET6
)
1832 return minus_one_set_errno(EINVAL
);
1834 ret
= snprintf(path
, PATH_MAX
, "/proc/sys/net/%s/conf/%s/%s",
1835 family
== AF_INET
? "ipv4" : "ipv6", ifname
,
1836 family
== AF_INET
? "proxy_arp" : "proxy_ndp");
1837 if (ret
< 0 || (size_t)ret
>= PATH_MAX
)
1838 return minus_one_set_errno(E2BIG
);
1840 return lxc_read_file_expect(path
, buf
, 1, "1");
1843 int lxc_neigh_proxy_on(const char *name
, int family
)
1845 return neigh_proxy_set(name
, family
, 1);
1848 int lxc_neigh_proxy_off(const char *name
, int family
)
1850 return neigh_proxy_set(name
, family
, 0);
1853 int lxc_convert_mac(char *macaddr
, struct sockaddr
*sockaddr
)
1858 unsigned char *data
;
1860 sockaddr
->sa_family
= ARPHRD_ETHER
;
1861 data
= (unsigned char *)sockaddr
->sa_data
;
1863 while ((*macaddr
!= '\0') && (i
< ETH_ALEN
)) {
1867 else if (c
>= 'a' && c
<= 'f')
1869 else if (c
>= 'A' && c
<= 'F')
1878 else if (c
>= 'a' && c
<= 'f')
1879 val
|= c
- 'a' + 10;
1880 else if (c
>= 'A' && c
<= 'F')
1881 val
|= c
- 'A' + 10;
1882 else if (c
== ':' || c
== 0)
1888 *data
++ = (unsigned char)(val
& 0377);
1891 if (*macaddr
== ':')
1898 static int ip_addr_add(int family
, int ifindex
, void *addr
, void *bcast
,
1899 void *acast
, int prefix
)
1902 struct ifaddrmsg
*ifa
;
1903 struct nl_handler nlh
;
1904 struct nlmsg
*answer
= NULL
, *nlmsg
= NULL
;
1906 addrlen
= family
== AF_INET
? sizeof(struct in_addr
)
1907 : sizeof(struct in6_addr
);
1909 err
= netlink_open(&nlh
, NETLINK_ROUTE
);
1914 nlmsg
= nlmsg_alloc(NLMSG_GOOD_SIZE
);
1918 answer
= nlmsg_alloc_reserve(NLMSG_GOOD_SIZE
);
1922 nlmsg
->nlmsghdr
->nlmsg_flags
=
1923 NLM_F_ACK
| NLM_F_REQUEST
| NLM_F_CREATE
| NLM_F_EXCL
;
1924 nlmsg
->nlmsghdr
->nlmsg_type
= RTM_NEWADDR
;
1926 ifa
= nlmsg_reserve(nlmsg
, sizeof(struct ifaddrmsg
));
1929 ifa
->ifa_prefixlen
= prefix
;
1930 ifa
->ifa_index
= ifindex
;
1931 ifa
->ifa_family
= family
;
1935 if (nla_put_buffer(nlmsg
, IFA_LOCAL
, addr
, addrlen
))
1938 if (nla_put_buffer(nlmsg
, IFA_ADDRESS
, addr
, addrlen
))
1941 if (nla_put_buffer(nlmsg
, IFA_BROADCAST
, bcast
, addrlen
))
1944 /* TODO: multicast, anycast with ipv6 */
1945 err
= -EPROTONOSUPPORT
;
1946 if (family
== AF_INET6
&&
1947 (memcmp(bcast
, &in6addr_any
, sizeof(in6addr_any
)) ||
1948 memcmp(acast
, &in6addr_any
, sizeof(in6addr_any
))))
1951 err
= netlink_transaction(&nlh
, nlmsg
, answer
);
1953 netlink_close(&nlh
);
1959 int lxc_ipv6_addr_add(int ifindex
, struct in6_addr
*addr
,
1960 struct in6_addr
*mcast
, struct in6_addr
*acast
,
1963 return ip_addr_add(AF_INET6
, ifindex
, addr
, mcast
, acast
, prefix
);
1966 int lxc_ipv4_addr_add(int ifindex
, struct in_addr
*addr
, struct in_addr
*bcast
,
1969 return ip_addr_add(AF_INET
, ifindex
, addr
, bcast
, NULL
, prefix
);
1972 /* Find an IFA_LOCAL (or IFA_ADDRESS if not IFA_LOCAL is present) address from
1973 * the given RTM_NEWADDR message. Allocates memory for the address and stores
1974 * that pointer in *res (so res should be an in_addr** or in6_addr**).
1976 #pragma GCC diagnostic push
1977 #pragma GCC diagnostic ignored "-Wcast-align"
1979 static int ifa_get_local_ip(int family
, struct nlmsghdr
*msg
, void **res
)
1982 struct ifaddrmsg
*ifa
= NLMSG_DATA(msg
);
1983 struct rtattr
*rta
= IFA_RTA(ifa
);
1984 int attr_len
= NLMSG_PAYLOAD(msg
, sizeof(struct ifaddrmsg
));
1986 if (ifa
->ifa_family
!= family
)
1989 addrlen
= family
== AF_INET
? sizeof(struct in_addr
)
1990 : sizeof(struct in6_addr
);
1992 /* Loop over the rtattr's in this message */
1993 while (RTA_OK(rta
, attr_len
)) {
1994 /* Found a local address for the requested interface,
1997 if (rta
->rta_type
== IFA_LOCAL
||
1998 rta
->rta_type
== IFA_ADDRESS
) {
1999 /* Sanity check. The family check above should make sure
2000 * the address length is correct, but check here just in
2003 if (RTA_PAYLOAD(rta
) != addrlen
)
2006 /* We might have found an IFA_ADDRESS before, which we
2007 * now overwrite with an IFA_LOCAL.
2010 *res
= malloc(addrlen
);
2015 memcpy(*res
, RTA_DATA(rta
), addrlen
);
2016 if (rta
->rta_type
== IFA_LOCAL
)
2019 rta
= RTA_NEXT(rta
, attr_len
);
2024 #pragma GCC diagnostic pop
2026 static int ip_addr_get(int family
, int ifindex
, void **res
)
2028 int answer_len
, err
;
2029 struct ifaddrmsg
*ifa
;
2030 struct nl_handler nlh
;
2031 struct nlmsghdr
*msg
;
2032 int readmore
= 0, recv_len
= 0;
2033 struct nlmsg
*answer
= NULL
, *nlmsg
= NULL
;
2035 err
= netlink_open(&nlh
, NETLINK_ROUTE
);
2040 nlmsg
= nlmsg_alloc(NLMSG_GOOD_SIZE
);
2044 answer
= nlmsg_alloc_reserve(NLMSG_GOOD_SIZE
);
2048 /* Save the answer buffer length, since it will be overwritten on the
2049 * first receive (and we might need to receive more than once).
2051 answer_len
= answer
->nlmsghdr
->nlmsg_len
;
2053 nlmsg
->nlmsghdr
->nlmsg_flags
= NLM_F_REQUEST
| NLM_F_ROOT
;
2054 nlmsg
->nlmsghdr
->nlmsg_type
= RTM_GETADDR
;
2056 ifa
= nlmsg_reserve(nlmsg
, sizeof(struct ifaddrmsg
));
2059 ifa
->ifa_family
= family
;
2061 /* Send the request for addresses, which returns all addresses on all
2064 err
= netlink_send(&nlh
, nlmsg
);
2068 #pragma GCC diagnostic push
2069 #pragma GCC diagnostic ignored "-Wcast-align"
2072 /* Restore the answer buffer length, it might have been
2073 * overwritten by a previous receive.
2075 answer
->nlmsghdr
->nlmsg_len
= answer_len
;
2077 /* Get the (next) batch of reply messages. */
2078 err
= netlink_rcv(&nlh
, answer
);
2085 /* Satisfy the typing for the netlink macros. */
2086 msg
= answer
->nlmsghdr
;
2088 while (NLMSG_OK(msg
, recv_len
)) {
2089 /* Stop reading if we see an error message. */
2090 if (msg
->nlmsg_type
== NLMSG_ERROR
) {
2091 struct nlmsgerr
*errmsg
=
2092 (struct nlmsgerr
*)NLMSG_DATA(msg
);
2093 err
= errmsg
->error
;
2097 /* Stop reading if we see a NLMSG_DONE message. */
2098 if (msg
->nlmsg_type
== NLMSG_DONE
) {
2103 if (msg
->nlmsg_type
!= RTM_NEWADDR
) {
2108 ifa
= (struct ifaddrmsg
*)NLMSG_DATA(msg
);
2109 if (ifa
->ifa_index
== ifindex
) {
2110 if (ifa_get_local_ip(family
, msg
, res
) < 0) {
2115 /* Found a result, stop searching. */
2120 /* Keep reading more data from the socket if the last
2121 * message had the NLF_F_MULTI flag set.
2123 readmore
= (msg
->nlmsg_flags
& NLM_F_MULTI
);
2125 /* Look at the next message received in this buffer. */
2126 msg
= NLMSG_NEXT(msg
, recv_len
);
2130 #pragma GCC diagnostic pop
2132 /* If we end up here, we didn't find any result, so signal an
2138 netlink_close(&nlh
);
2144 int lxc_ipv6_addr_get(int ifindex
, struct in6_addr
**res
)
2146 return ip_addr_get(AF_INET6
, ifindex
, (void **)res
);
2149 int lxc_ipv4_addr_get(int ifindex
, struct in_addr
**res
)
2151 return ip_addr_get(AF_INET
, ifindex
, (void **)res
);
2154 static int ip_gateway_add(int family
, int ifindex
, void *gw
)
2157 struct nl_handler nlh
;
2159 struct nlmsg
*answer
= NULL
, *nlmsg
= NULL
;
2161 addrlen
= family
== AF_INET
? sizeof(struct in_addr
)
2162 : sizeof(struct in6_addr
);
2164 err
= netlink_open(&nlh
, NETLINK_ROUTE
);
2169 nlmsg
= nlmsg_alloc(NLMSG_GOOD_SIZE
);
2173 answer
= nlmsg_alloc_reserve(NLMSG_GOOD_SIZE
);
2177 nlmsg
->nlmsghdr
->nlmsg_flags
=
2178 NLM_F_ACK
| NLM_F_REQUEST
| NLM_F_CREATE
| NLM_F_EXCL
;
2179 nlmsg
->nlmsghdr
->nlmsg_type
= RTM_NEWROUTE
;
2181 rt
= nlmsg_reserve(nlmsg
, sizeof(struct rtmsg
));
2184 rt
->rtm_family
= family
;
2185 rt
->rtm_table
= RT_TABLE_MAIN
;
2186 rt
->rtm_scope
= RT_SCOPE_UNIVERSE
;
2187 rt
->rtm_protocol
= RTPROT_BOOT
;
2188 rt
->rtm_type
= RTN_UNICAST
;
2189 /* "default" destination */
2190 rt
->rtm_dst_len
= 0;
2194 /* If gateway address not supplied, then a device route will be created instead */
2196 if (nla_put_buffer(nlmsg
, RTA_GATEWAY
, gw
, addrlen
))
2200 /* Adding the interface index enables the use of link-local
2201 * addresses for the gateway.
2203 if (nla_put_u32(nlmsg
, RTA_OIF
, ifindex
))
2206 err
= netlink_transaction(&nlh
, nlmsg
, answer
);
2208 netlink_close(&nlh
);
2214 int lxc_ipv4_gateway_add(int ifindex
, struct in_addr
*gw
)
2216 return ip_gateway_add(AF_INET
, ifindex
, gw
);
2219 int lxc_ipv6_gateway_add(int ifindex
, struct in6_addr
*gw
)
2221 return ip_gateway_add(AF_INET6
, ifindex
, gw
);
2223 bool is_ovs_bridge(const char *bridge
)
2227 char brdirname
[22 + IFNAMSIZ
+ 1] = {0};
2229 ret
= snprintf(brdirname
, 22 + IFNAMSIZ
+ 1, "/sys/class/net/%s/bridge",
2231 if (ret
< 0 || (size_t)ret
>= 22 + IFNAMSIZ
+ 1)
2234 ret
= stat(brdirname
, &sb
);
2235 if (ret
< 0 && errno
== ENOENT
)
2241 struct ovs_veth_args
{
2246 /* Called from a background thread - when nic goes away, remove it from the
2249 static int lxc_ovs_delete_port_exec(void *data
)
2251 struct ovs_veth_args
*args
= data
;
2253 execlp("ovs-vsctl", "ovs-vsctl", "del-port", args
->bridge
, args
->nic
,
2258 int lxc_ovs_delete_port(const char *bridge
, const char *nic
)
2261 char cmd_output
[PATH_MAX
];
2262 struct ovs_veth_args args
;
2264 args
.bridge
= bridge
;
2266 ret
= run_command(cmd_output
, sizeof(cmd_output
),
2267 lxc_ovs_delete_port_exec
, (void *)&args
);
2269 ERROR("Failed to delete \"%s\" from openvswitch bridge \"%s\": "
2270 "%s", bridge
, nic
, cmd_output
);
2277 static int lxc_ovs_attach_bridge_exec(void *data
)
2279 struct ovs_veth_args
*args
= data
;
2281 execlp("ovs-vsctl", "ovs-vsctl", "add-port", args
->bridge
, args
->nic
,
2286 static int lxc_ovs_attach_bridge(const char *bridge
, const char *nic
)
2289 char cmd_output
[PATH_MAX
];
2290 struct ovs_veth_args args
;
2292 args
.bridge
= bridge
;
2294 ret
= run_command(cmd_output
, sizeof(cmd_output
),
2295 lxc_ovs_attach_bridge_exec
, (void *)&args
);
2297 ERROR("Failed to attach \"%s\" to openvswitch bridge \"%s\": %s",
2298 bridge
, nic
, cmd_output
);
2305 int lxc_bridge_attach(const char *bridge
, const char *ifname
)
2311 if (strlen(ifname
) >= IFNAMSIZ
)
2314 index
= if_nametoindex(ifname
);
2318 if (is_ovs_bridge(bridge
))
2319 return lxc_ovs_attach_bridge(bridge
, ifname
);
2321 fd
= socket(AF_INET
, SOCK_STREAM
| SOCK_CLOEXEC
, 0);
2325 retlen
= strlcpy(ifr
.ifr_name
, bridge
, IFNAMSIZ
);
2326 if (retlen
>= IFNAMSIZ
) {
2331 ifr
.ifr_name
[IFNAMSIZ
- 1] = '\0';
2332 ifr
.ifr_ifindex
= index
;
2333 err
= ioctl(fd
, SIOCBRADDIF
, &ifr
);
2341 static const char *const lxc_network_types
[LXC_NET_MAXCONFTYPE
+ 1] = {
2342 [LXC_NET_EMPTY
] = "empty",
2343 [LXC_NET_VETH
] = "veth",
2344 [LXC_NET_MACVLAN
] = "macvlan",
2345 [LXC_NET_IPVLAN
] = "ipvlan",
2346 [LXC_NET_PHYS
] = "phys",
2347 [LXC_NET_VLAN
] = "vlan",
2348 [LXC_NET_NONE
] = "none",
2351 const char *lxc_net_type_to_str(int type
)
2353 if (type
< 0 || type
> LXC_NET_MAXCONFTYPE
)
2356 return lxc_network_types
[type
];
2359 static const char padchar
[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
2361 char *lxc_mkifname(char *template)
2364 struct netns_ifaddrs
*ifa
, *ifaddr
;
2365 char name
[IFNAMSIZ
];
2366 bool exists
= false;
2371 seed
= randseed(false);
2374 (void)randseed(true);
2377 if (strlen(template) >= IFNAMSIZ
)
2380 /* Get all the network interfaces. */
2381 ret
= netns_getifaddrs(&ifaddr
, -1, &(bool){false});
2383 SYSERROR("Failed to get network interfaces");
2387 /* Generate random names until we find one that doesn't exist. */
2390 (void)strlcpy(name
, template, IFNAMSIZ
);
2394 for (i
= 0; i
< strlen(name
); i
++) {
2395 if (name
[i
] == 'X') {
2397 name
[i
] = padchar
[rand_r(&seed
) % strlen(padchar
)];
2399 name
[i
] = padchar
[rand() % strlen(padchar
)];
2404 for (ifa
= ifaddr
; ifa
!= NULL
; ifa
= ifa
->ifa_next
) {
2405 if (!strcmp(ifa
->ifa_name
, name
)) {
2415 netns_freeifaddrs(ifaddr
);
2416 (void)strlcpy(template, name
, strlen(template) + 1);
2421 int setup_private_host_hw_addr(char *veth1
)
2426 sockfd
= socket(AF_INET
, SOCK_DGRAM
| SOCK_CLOEXEC
, 0);
2430 err
= snprintf((char *)ifr
.ifr_name
, IFNAMSIZ
, "%s", veth1
);
2431 if (err
< 0 || (size_t)err
>= IFNAMSIZ
) {
2436 err
= ioctl(sockfd
, SIOCGIFHWADDR
, &ifr
);
2442 ifr
.ifr_hwaddr
.sa_data
[0] = 0xfe;
2443 err
= ioctl(sockfd
, SIOCSIFHWADDR
, &ifr
);
2451 int lxc_find_gateway_addresses(struct lxc_handler
*handler
)
2453 struct lxc_list
*network
= &handler
->conf
->network
;
2454 struct lxc_list
*iterator
;
2455 struct lxc_netdev
*netdev
;
2458 lxc_list_for_each(iterator
, network
) {
2459 netdev
= iterator
->elem
;
2461 if (!netdev
->ipv4_gateway_auto
&& !netdev
->ipv6_gateway_auto
)
2464 if (netdev
->type
!= LXC_NET_VETH
&& netdev
->type
!= LXC_NET_MACVLAN
) {
2465 ERROR("Automatic gateway detection is only supported "
2466 "for veth and macvlan");
2470 if (netdev
->link
[0] == '\0') {
2471 ERROR("Automatic gateway detection needs a link interface");
2475 link_index
= if_nametoindex(netdev
->link
);
2479 if (netdev
->ipv4_gateway_auto
) {
2480 if (lxc_ipv4_addr_get(link_index
, &netdev
->ipv4_gateway
)) {
2481 ERROR("Failed to automatically find ipv4 gateway "
2482 "address from link interface \"%s\"", netdev
->link
);
2487 if (netdev
->ipv6_gateway_auto
) {
2488 if (lxc_ipv6_addr_get(link_index
, &netdev
->ipv6_gateway
)) {
2489 ERROR("Failed to automatically find ipv6 gateway "
2490 "address from link interface \"%s\"", netdev
->link
);
2499 #define LXC_USERNIC_PATH LIBEXECDIR "/lxc/lxc-user-nic"
2500 static int lxc_create_network_unpriv_exec(const char *lxcpath
, const char *lxcname
,
2501 struct lxc_netdev
*netdev
, pid_t pid
, unsigned int hooks_version
)
2505 int bytes
, pipefd
[2];
2506 char *token
, *saveptr
= NULL
;
2507 char netdev_link
[IFNAMSIZ
];
2508 char buffer
[PATH_MAX
] = {0};
2511 if (netdev
->type
!= LXC_NET_VETH
) {
2512 ERROR("Network type %d not support for unprivileged use", netdev
->type
);
2518 SYSERROR("Failed to create pipe");
2524 SYSERROR("Failed to create new process");
2531 char pidstr
[INTTYPE_TO_STRLEN(pid_t
)];
2535 ret
= dup2(pipefd
[1], STDOUT_FILENO
);
2537 ret
= dup2(pipefd
[1], STDERR_FILENO
);
2540 SYSERROR("Failed to duplicate std{err,out} file descriptor");
2541 _exit(EXIT_FAILURE
);
2544 if (netdev
->link
[0] != '\0')
2545 retlen
= strlcpy(netdev_link
, netdev
->link
, IFNAMSIZ
);
2547 retlen
= strlcpy(netdev_link
, "none", IFNAMSIZ
);
2548 if (retlen
>= IFNAMSIZ
) {
2549 SYSERROR("Invalid network device name");
2550 _exit(EXIT_FAILURE
);
2553 ret
= snprintf(pidstr
, sizeof(pidstr
), "%d", pid
);
2554 if (ret
< 0 || ret
>= sizeof(pidstr
))
2555 _exit(EXIT_FAILURE
);
2556 pidstr
[sizeof(pidstr
) - 1] = '\0';
2558 INFO("Execing lxc-user-nic create %s %s %s veth %s %s", lxcpath
,
2559 lxcname
, pidstr
, netdev_link
,
2560 netdev
->name
[0] != '\0' ? netdev
->name
: "(null)");
2561 if (netdev
->name
[0] != '\0')
2562 execlp(LXC_USERNIC_PATH
, LXC_USERNIC_PATH
, "create",
2563 lxcpath
, lxcname
, pidstr
, "veth", netdev_link
,
2564 netdev
->name
, (char *)NULL
);
2566 execlp(LXC_USERNIC_PATH
, LXC_USERNIC_PATH
, "create",
2567 lxcpath
, lxcname
, pidstr
, "veth", netdev_link
,
2569 SYSERROR("Failed to execute lxc-user-nic");
2570 _exit(EXIT_FAILURE
);
2573 /* close the write-end of the pipe */
2576 bytes
= lxc_read_nointr(pipefd
[0], &buffer
, PATH_MAX
);
2578 SYSERROR("Failed to read from pipe file descriptor");
2581 buffer
[bytes
- 1] = '\0';
2584 ret
= wait_for_pid(child
);
2586 if (ret
!= 0 || bytes
< 0) {
2587 ERROR("lxc-user-nic failed to configure requested network: %s",
2588 buffer
[0] != '\0' ? buffer
: "(null)");
2591 TRACE("Received output \"%s\" from lxc-user-nic", buffer
);
2594 token
= strtok_r(buffer
, ":", &saveptr
);
2596 ERROR("Failed to parse lxc-user-nic output");
2601 * lxc-user-nic will take care of proper network device naming. So
2602 * netdev->name and netdev->created_name need to be identical to not
2603 * trigger another rename later on.
2605 retlen
= strlcpy(netdev
->name
, token
, IFNAMSIZ
);
2606 if (retlen
< IFNAMSIZ
)
2607 retlen
= strlcpy(netdev
->created_name
, token
, IFNAMSIZ
);
2608 if (retlen
>= IFNAMSIZ
) {
2609 ERROR("Container side veth device name returned by lxc-user-nic is too long");
2613 /* netdev->ifindex */
2614 token
= strtok_r(NULL
, ":", &saveptr
);
2616 ERROR("Failed to parse lxc-user-nic output");
2620 ret
= lxc_safe_int(token
, &netdev
->ifindex
);
2623 SYSERROR("Failed to convert string \"%s\" to integer", token
);
2627 /* netdev->priv.veth_attr.veth1 */
2628 token
= strtok_r(NULL
, ":", &saveptr
);
2630 ERROR("Failed to parse lxc-user-nic output");
2634 retlen
= strlcpy(netdev
->priv
.veth_attr
.veth1
, token
, IFNAMSIZ
);
2635 if (retlen
>= IFNAMSIZ
) {
2636 ERROR("Host side veth device name returned by lxc-user-nic is "
2641 /* netdev->priv.veth_attr.ifindex */
2642 token
= strtok_r(NULL
, ":", &saveptr
);
2644 ERROR("Failed to parse lxc-user-nic output");
2648 ret
= lxc_safe_int(token
, &netdev
->priv
.veth_attr
.ifindex
);
2651 SYSERROR("Failed to convert string \"%s\" to integer", token
);
2655 if (netdev
->upscript
) {
2659 netdev
->priv
.veth_attr
.veth1
,
2663 ret
= run_script_argv(lxcname
, hooks_version
, "net",
2664 netdev
->upscript
, "up", argv
);
2672 static int lxc_delete_network_unpriv_exec(const char *lxcpath
, const char *lxcname
,
2673 struct lxc_netdev
*netdev
,
2674 const char *netns_path
)
2679 char buffer
[PATH_MAX
] = {0};
2681 if (netdev
->type
!= LXC_NET_VETH
) {
2682 ERROR("Network type %d not support for unprivileged use", netdev
->type
);
2688 SYSERROR("Failed to create pipe");
2694 SYSERROR("Failed to create new process");
2705 ret
= dup2(pipefd
[1], STDOUT_FILENO
);
2707 ret
= dup2(pipefd
[1], STDERR_FILENO
);
2710 SYSERROR("Failed to duplicate std{err,out} file descriptor");
2711 _exit(EXIT_FAILURE
);
2714 if (netdev
->priv
.veth_attr
.pair
[0] != '\0')
2715 hostveth
= netdev
->priv
.veth_attr
.pair
;
2717 hostveth
= netdev
->priv
.veth_attr
.veth1
;
2718 if (hostveth
[0] == '\0') {
2719 SYSERROR("Host side veth device name is missing");
2720 _exit(EXIT_FAILURE
);
2723 if (netdev
->link
[0] == '\0') {
2724 SYSERROR("Network link for network device \"%s\" is "
2725 "missing", netdev
->priv
.veth_attr
.veth1
);
2726 _exit(EXIT_FAILURE
);
2729 INFO("Execing lxc-user-nic delete %s %s %s veth %s %s", lxcpath
,
2730 lxcname
, netns_path
, netdev
->link
, hostveth
);
2731 execlp(LXC_USERNIC_PATH
, LXC_USERNIC_PATH
, "delete", lxcpath
,
2732 lxcname
, netns_path
, "veth", netdev
->link
, hostveth
,
2734 SYSERROR("Failed to exec lxc-user-nic.");
2735 _exit(EXIT_FAILURE
);
2740 bytes
= lxc_read_nointr(pipefd
[0], &buffer
, PATH_MAX
);
2742 SYSERROR("Failed to read from pipe file descriptor.");
2745 buffer
[bytes
- 1] = '\0';
2748 ret
= wait_for_pid(child
);
2750 if (ret
!= 0 || bytes
< 0) {
2751 ERROR("lxc-user-nic failed to delete requested network: %s",
2752 buffer
[0] != '\0' ? buffer
: "(null)");
2759 bool lxc_delete_network_unpriv(struct lxc_handler
*handler
)
2762 struct lxc_list
*iterator
;
2763 struct lxc_list
*network
= &handler
->conf
->network
;
2764 /* strlen("/proc/") = 6
2766 * INTTYPE_TO_STRLEN(pid_t)
2768 * strlen("/fd/") = 4
2770 * INTTYPE_TO_STRLEN(int)
2774 char netns_path
[6 + INTTYPE_TO_STRLEN(pid_t
) + 4 + INTTYPE_TO_STRLEN(int) + 1];
2778 if (handler
->nsfd
[LXC_NS_NET
] < 0) {
2779 DEBUG("Cannot not guarantee safe deletion of network devices. "
2780 "Manual cleanup maybe needed");
2784 ret
= snprintf(netns_path
, sizeof(netns_path
), "/proc/%d/fd/%d",
2785 lxc_raw_getpid(), handler
->nsfd
[LXC_NS_NET
]);
2786 if (ret
< 0 || ret
>= sizeof(netns_path
))
2789 lxc_list_for_each(iterator
, network
) {
2790 char *hostveth
= NULL
;
2791 struct lxc_netdev
*netdev
= iterator
->elem
;
2793 /* We can only delete devices whose ifindex we have. If we don't
2794 * have the index it means that we didn't create it.
2796 if (!netdev
->ifindex
)
2799 if (netdev
->type
== LXC_NET_PHYS
) {
2800 ret
= lxc_netdev_rename_by_index(netdev
->ifindex
,
2803 WARN("Failed to rename interface with index %d "
2804 "to its initial name \"%s\"",
2805 netdev
->ifindex
, netdev
->link
);
2807 TRACE("Renamed interface with index %d to its "
2808 "initial name \"%s\"",
2809 netdev
->ifindex
, netdev
->link
);
2811 ret
= netdev_deconf
[netdev
->type
](handler
, netdev
);
2812 goto clear_ifindices
;
2815 ret
= netdev_deconf
[netdev
->type
](handler
, netdev
);
2817 WARN("Failed to deconfigure network device");
2819 if (netdev
->type
!= LXC_NET_VETH
)
2820 goto clear_ifindices
;
2822 if (netdev
->link
[0] == '\0' || !is_ovs_bridge(netdev
->link
))
2823 goto clear_ifindices
;
2825 if (netdev
->priv
.veth_attr
.pair
[0] != '\0')
2826 hostveth
= netdev
->priv
.veth_attr
.pair
;
2828 hostveth
= netdev
->priv
.veth_attr
.veth1
;
2829 if (hostveth
[0] == '\0')
2830 goto clear_ifindices
;
2832 ret
= lxc_delete_network_unpriv_exec(handler
->lxcpath
,
2833 handler
->name
, netdev
,
2836 WARN("Failed to remove port \"%s\" from openvswitch "
2837 "bridge \"%s\"", hostveth
, netdev
->link
);
2838 goto clear_ifindices
;
2840 INFO("Removed interface \"%s\" from \"%s\"", hostveth
,
2844 /* We need to clear any ifindices we recorded so liblxc won't
2845 * have cached stale data which would cause it to fail on reboot
2846 * we're we don't re-read the on-disk config file.
2848 netdev
->ifindex
= 0;
2849 if (netdev
->type
== LXC_NET_PHYS
) {
2850 netdev
->priv
.phys_attr
.ifindex
= 0;
2851 } else if (netdev
->type
== LXC_NET_VETH
) {
2852 netdev
->priv
.veth_attr
.veth1
[0] = '\0';
2853 netdev
->priv
.veth_attr
.ifindex
= 0;
2860 struct ip_proxy_args
{
2865 static int lxc_add_ip_neigh_proxy_exec_wrapper(void *data
)
2867 struct ip_proxy_args
*args
= data
;
2869 execlp("ip", "ip", "neigh", "add", "proxy", args
->ip
, "dev", args
->dev
, (char *)NULL
);
2873 static int lxc_del_ip_neigh_proxy_exec_wrapper(void *data
)
2875 struct ip_proxy_args
*args
= data
;
2877 execlp("ip", "ip", "neigh", "flush", "proxy", args
->ip
, "dev", args
->dev
, (char *)NULL
);
2881 static int lxc_add_ip_neigh_proxy(const char *ip
, const char *dev
)
2884 char cmd_output
[PATH_MAX
];
2885 struct ip_proxy_args args
= {
2890 ret
= run_command(cmd_output
, sizeof(cmd_output
), lxc_add_ip_neigh_proxy_exec_wrapper
, &args
);
2892 ERROR("Failed to add ip proxy \"%s\" to dev \"%s\": %s", ip
, dev
, cmd_output
);
2899 static int lxc_del_ip_neigh_proxy(const char *ip
, const char *dev
)
2902 char cmd_output
[PATH_MAX
];
2903 struct ip_proxy_args args
= {
2908 ret
= run_command(cmd_output
, sizeof(cmd_output
), lxc_del_ip_neigh_proxy_exec_wrapper
, &args
);
2910 ERROR("Failed to delete ip proxy \"%s\" to dev \"%s\": %s", ip
, dev
, cmd_output
);
2917 static int lxc_setup_l2proxy(struct lxc_netdev
*netdev
) {
2918 struct lxc_list
*cur
, *next
;
2919 struct lxc_inetdev
*inet4dev
;
2920 struct lxc_inet6dev
*inet6dev
;
2921 char bufinet4
[INET_ADDRSTRLEN
], bufinet6
[INET6_ADDRSTRLEN
];
2923 unsigned int lo_ifindex
= 0;
2925 /* If IPv4 addresses are specified, then check that sysctl is configured correctly. */
2926 if (!lxc_list_empty(&netdev
->ipv4
)) {
2927 /* Check for net.ipv4.conf.[link].forwarding=1 */
2928 if (lxc_is_ip_forwarding_enabled(netdev
->link
, AF_INET
) < 0) {
2929 ERROR("Requires sysctl net.ipv4.conf.%s.forwarding=1", netdev
->link
);
2930 return minus_one_set_errno(EINVAL
);
2934 /* If IPv6 addresses are specified, then check that sysctl is configured correctly. */
2935 if (!lxc_list_empty(&netdev
->ipv6
)) {
2936 /* Check for net.ipv6.conf.[link].proxy_ndp=1 */
2937 if (lxc_is_ip_neigh_proxy_enabled(netdev
->link
, AF_INET6
) < 0) {
2938 ERROR("Requires sysctl net.ipv6.conf.%s.proxy_ndp=1", netdev
->link
);
2939 return minus_one_set_errno(EINVAL
);
2942 /* Check for net.ipv6.conf.[link].forwarding=1 */
2943 if (lxc_is_ip_forwarding_enabled(netdev
->link
, AF_INET6
) < 0) {
2944 ERROR("Requires sysctl net.ipv6.conf.%s.forwarding=1", netdev
->link
);
2945 return minus_one_set_errno(EINVAL
);
2949 /* Perform IPVLAN specific checks. */
2950 if (netdev
->type
== LXC_NET_IPVLAN
) {
2951 /* Check mode is l3s as other modes do not work with l2proxy. */
2952 if (netdev
->priv
.ipvlan_attr
.mode
!= IPVLAN_MODE_L3S
) {
2953 ERROR("Requires ipvlan mode on dev \"%s\" be l3s when used with l2proxy", netdev
->link
);
2954 return minus_one_set_errno(EINVAL
);
2957 /* Retrieve local-loopback interface index for use with IPVLAN static routes. */
2958 lo_ifindex
= if_nametoindex(loop_device
);
2959 if (lo_ifindex
== 0) {
2960 ERROR("Failed to retrieve ifindex for \"%s\" routing cleanup", loop_device
);
2961 return minus_one_set_errno(EINVAL
);
2965 lxc_list_for_each_safe(cur
, &netdev
->ipv4
, next
) {
2966 inet4dev
= cur
->elem
;
2967 if (!inet_ntop(AF_INET
, &inet4dev
->addr
, bufinet4
, sizeof(bufinet4
)))
2968 return minus_one_set_errno(-errno
);
2970 if (lxc_add_ip_neigh_proxy(bufinet4
, netdev
->link
) < 0)
2971 return minus_one_set_errno(EINVAL
);
2973 /* IPVLAN requires a route to local-loopback to trigger l2proxy. */
2974 if (netdev
->type
== LXC_NET_IPVLAN
) {
2975 err
= lxc_ipv4_dest_add(lo_ifindex
, &inet4dev
->addr
, 32);
2977 ERROR("Failed to add ipv4 dest \"%s\" for network device \"%s\"", bufinet4
, loop_device
);
2978 return minus_one_set_errno(-err
);
2983 lxc_list_for_each_safe(cur
, &netdev
->ipv6
, next
) {
2984 inet6dev
= cur
->elem
;
2985 if (!inet_ntop(AF_INET6
, &inet6dev
->addr
, bufinet6
, sizeof(bufinet6
)))
2986 return minus_one_set_errno(-errno
);
2988 if (lxc_add_ip_neigh_proxy(bufinet6
, netdev
->link
) < 0)
2989 return minus_one_set_errno(EINVAL
);
2991 /* IPVLAN requires a route to local-loopback to trigger l2proxy. */
2992 if (netdev
->type
== LXC_NET_IPVLAN
) {
2993 err
= lxc_ipv6_dest_add(lo_ifindex
, &inet6dev
->addr
, 128);
2995 ERROR("Failed to add ipv6 dest \"%s\" for network device \"%s\"", bufinet6
, loop_device
);
2996 return minus_one_set_errno(-err
);
3004 static int lxc_delete_ipv4_l2proxy(struct in_addr
*ip
, char *link
, unsigned int lo_ifindex
) {
3005 char bufinet4
[INET_ADDRSTRLEN
];
3006 unsigned int errCount
= 0;
3008 if (!inet_ntop(AF_INET
, ip
, bufinet4
, sizeof(bufinet4
))) {
3009 SYSERROR("Failed to convert IP for l2proxy ipv4 removal on dev \"%s\"", link
);
3010 return minus_one_set_errno(EINVAL
);
3013 /* If a local-loopback ifindex supplied remove the static route to the lo device. */
3014 if (lo_ifindex
> 0) {
3015 if (lxc_ipv4_dest_del(lo_ifindex
, ip
, 32) < 0) {
3017 ERROR("Failed to delete ipv4 dest \"%s\" for network ifindex \"%u\"", bufinet4
, lo_ifindex
);
3021 /* If link is supplied remove the IP neigh proxy entry for this IP on the device. */
3022 if (link
[0] != '\0') {
3023 if (lxc_del_ip_neigh_proxy(bufinet4
, link
) < 0)
3028 return minus_one_set_errno(EINVAL
);
3033 static int lxc_delete_ipv6_l2proxy(struct in6_addr
*ip
, char *link
, unsigned int lo_ifindex
) {
3034 char bufinet6
[INET6_ADDRSTRLEN
];
3035 unsigned int errCount
= 0;
3037 if (!inet_ntop(AF_INET6
, ip
, bufinet6
, sizeof(bufinet6
))) {
3038 SYSERROR("Failed to convert IP for l2proxy ipv6 removal on dev \"%s\"", link
);
3039 return minus_one_set_errno(EINVAL
);
3042 /* If a local-loopback ifindex supplied remove the static route to the lo device. */
3043 if (lo_ifindex
> 0) {
3044 if (lxc_ipv6_dest_del(lo_ifindex
, ip
, 128) < 0) {
3046 ERROR("Failed to delete ipv6 dest \"%s\" for network ifindex \"%u\"", bufinet6
, lo_ifindex
);
3050 /* If link is supplied remove the IP neigh proxy entry for this IP on the device. */
3051 if (link
[0] != '\0') {
3052 if (lxc_del_ip_neigh_proxy(bufinet6
, link
) < 0)
3057 return minus_one_set_errno(EINVAL
);
3062 static int lxc_delete_l2proxy(struct lxc_netdev
*netdev
) {
3063 unsigned int lo_ifindex
= 0;
3064 unsigned int errCount
= 0;
3065 struct lxc_list
*cur
, *next
;
3066 struct lxc_inetdev
*inet4dev
;
3067 struct lxc_inet6dev
*inet6dev
;
3069 /* Perform IPVLAN specific checks. */
3070 if (netdev
->type
== LXC_NET_IPVLAN
) {
3071 /* Retrieve local-loopback interface index for use with IPVLAN static routes. */
3072 lo_ifindex
= if_nametoindex(loop_device
);
3073 if (lo_ifindex
== 0) {
3075 ERROR("Failed to retrieve ifindex for \"%s\" routing cleanup", loop_device
);
3079 lxc_list_for_each_safe(cur
, &netdev
->ipv4
, next
) {
3080 inet4dev
= cur
->elem
;
3081 if (lxc_delete_ipv4_l2proxy(&inet4dev
->addr
, netdev
->link
, lo_ifindex
) < 0)
3085 lxc_list_for_each_safe(cur
, &netdev
->ipv6
, next
) {
3086 inet6dev
= cur
->elem
;
3087 if (lxc_delete_ipv6_l2proxy(&inet6dev
->addr
, netdev
->link
, lo_ifindex
) < 0)
3092 return minus_one_set_errno(EINVAL
);
3097 static int lxc_create_network_priv(struct lxc_handler
*handler
)
3099 struct lxc_list
*iterator
;
3100 struct lxc_list
*network
= &handler
->conf
->network
;
3102 lxc_list_for_each(iterator
, network
) {
3103 struct lxc_netdev
*netdev
= iterator
->elem
;
3105 if (netdev
->type
< 0 || netdev
->type
> LXC_NET_MAXCONFTYPE
) {
3106 ERROR("Invalid network configuration type %d", netdev
->type
);
3110 /* Setup l2proxy entries if enabled and used with a link property */
3111 if (netdev
->l2proxy
&& netdev
->link
[0] != '\0') {
3112 if (lxc_setup_l2proxy(netdev
)) {
3113 ERROR("Failed to setup l2proxy");
3118 if (netdev_conf
[netdev
->type
](handler
, netdev
)) {
3119 ERROR("Failed to create network device");
3127 int lxc_network_move_created_netdev_priv(struct lxc_handler
*handler
)
3129 pid_t pid
= handler
->pid
;
3130 struct lxc_list
*network
= &handler
->conf
->network
;
3131 struct lxc_list
*iterator
;
3133 if (am_guest_unpriv())
3136 lxc_list_for_each(iterator
, network
) {
3138 char ifname
[IFNAMSIZ
];
3139 struct lxc_netdev
*netdev
= iterator
->elem
;
3141 if (!netdev
->ifindex
)
3144 /* retrieve the name of the interface */
3145 if (!if_indextoname(netdev
->ifindex
, ifname
)) {
3146 ERROR("No interface corresponding to ifindex \"%d\"",
3151 ret
= lxc_netdev_move_by_name(ifname
, pid
, NULL
);
3154 SYSERROR("Failed to move network device \"%s\" to network namespace %d",
3159 strlcpy(netdev
->created_name
, ifname
, IFNAMSIZ
);
3161 DEBUG("Moved network device \"%s\" to network namespace of %d",
3162 netdev
->created_name
, pid
);
3168 static int lxc_create_network_unpriv(struct lxc_handler
*handler
)
3170 int hooks_version
= handler
->conf
->hooks_version
;
3171 const char *lxcname
= handler
->name
;
3172 const char *lxcpath
= handler
->lxcpath
;
3173 struct lxc_list
*network
= &handler
->conf
->network
;
3174 pid_t pid
= handler
->pid
;
3175 struct lxc_list
*iterator
;
3177 lxc_list_for_each(iterator
, network
) {
3178 struct lxc_netdev
*netdev
= iterator
->elem
;
3180 if (netdev
->type
== LXC_NET_EMPTY
)
3183 if (netdev
->type
== LXC_NET_NONE
)
3186 if (netdev
->type
!= LXC_NET_VETH
) {
3187 ERROR("Networks of type %s are not supported by unprivileged containers",
3188 lxc_net_type_to_str(netdev
->type
));
3193 INFO("mtu ignored due to insufficient privilege");
3195 if (lxc_create_network_unpriv_exec(lxcpath
, lxcname
, netdev
,
3196 pid
, hooks_version
))
3203 bool lxc_delete_network_priv(struct lxc_handler
*handler
)
3206 struct lxc_list
*iterator
;
3207 struct lxc_list
*network
= &handler
->conf
->network
;
3209 lxc_list_for_each(iterator
, network
) {
3210 char *hostveth
= NULL
;
3211 struct lxc_netdev
*netdev
= iterator
->elem
;
3213 /* We can only delete devices whose ifindex we have. If we don't
3214 * have the index it means that we didn't create it.
3216 if (!netdev
->ifindex
)
3219 /* Delete l2proxy entries if enabled and used with a link property */
3220 if (netdev
->l2proxy
&& netdev
->link
[0] != '\0') {
3221 if (lxc_delete_l2proxy(netdev
))
3222 WARN("Failed to delete all l2proxy config");
3223 /* Don't return, let the network be cleaned up as normal. */
3226 if (netdev
->type
== LXC_NET_PHYS
) {
3227 ret
= lxc_netdev_rename_by_index(netdev
->ifindex
, netdev
->link
);
3229 WARN("Failed to rename interface with index %d "
3230 "from \"%s\" to its initial name \"%s\"",
3231 netdev
->ifindex
, netdev
->name
, netdev
->link
);
3233 TRACE("Renamed interface with index %d from "
3234 "\"%s\" to its initial name \"%s\"",
3235 netdev
->ifindex
, netdev
->name
,
3238 /* Restore original MTU */
3239 ret
= lxc_netdev_set_mtu(netdev
->link
, netdev
->priv
.phys_attr
.mtu
);
3241 WARN("Failed to set interface \"%s\" to its initial mtu \"%d\"",
3242 netdev
->link
, netdev
->priv
.phys_attr
.mtu
);
3244 TRACE("Restored interface \"%s\" to its initial mtu \"%d\"",
3245 netdev
->link
, netdev
->priv
.phys_attr
.mtu
);
3249 ret
= netdev_deconf
[netdev
->type
](handler
, netdev
);
3250 goto clear_ifindices
;
3253 ret
= netdev_deconf
[netdev
->type
](handler
, netdev
);
3255 WARN("Failed to deconfigure network device");
3257 /* Recent kernels remove the virtual interfaces when the network
3258 * namespace is destroyed but in case we did not move the
3259 * interface to the network namespace, we have to destroy it.
3261 ret
= lxc_netdev_delete_by_index(netdev
->ifindex
);
3263 if (errno
!= ENODEV
) {
3264 WARN("Failed to remove interface \"%s\" with index %d",
3265 netdev
->name
[0] != '\0' ? netdev
->name
: "(null)",
3267 goto clear_ifindices
;
3269 INFO("Interface \"%s\" with index %d already deleted or existing in different network namespace",
3270 netdev
->name
[0] != '\0' ? netdev
->name
: "(null)",
3273 INFO("Removed interface \"%s\" with index %d",
3274 netdev
->name
[0] != '\0' ? netdev
->name
: "(null)",
3277 if (netdev
->type
!= LXC_NET_VETH
)
3278 goto clear_ifindices
;
3280 /* Explicitly delete host veth device to prevent lingering
3281 * devices. We had issues in LXD around this.
3283 if (netdev
->priv
.veth_attr
.pair
[0] != '\0')
3284 hostveth
= netdev
->priv
.veth_attr
.pair
;
3286 hostveth
= netdev
->priv
.veth_attr
.veth1
;
3287 if (hostveth
[0] == '\0')
3288 goto clear_ifindices
;
3290 ret
= lxc_netdev_delete_by_name(hostveth
);
3292 WARN("Failed to remove interface \"%s\" from \"%s\"",
3293 hostveth
, netdev
->link
);
3294 goto clear_ifindices
;
3296 INFO("Removed interface \"%s\" from \"%s\"", hostveth
, netdev
->link
);
3298 if (netdev
->link
[0] == '\0' || !is_ovs_bridge(netdev
->link
)) {
3299 netdev
->priv
.veth_attr
.veth1
[0] = '\0';
3300 netdev
->ifindex
= 0;
3301 netdev
->priv
.veth_attr
.ifindex
= 0;
3302 goto clear_ifindices
;
3305 /* Delete the openvswitch port. */
3306 ret
= lxc_ovs_delete_port(netdev
->link
, hostveth
);
3308 WARN("Failed to remove port \"%s\" from openvswitch "
3309 "bridge \"%s\"", hostveth
, netdev
->link
);
3311 INFO("Removed port \"%s\" from openvswitch bridge \"%s\"",
3312 hostveth
, netdev
->link
);
3315 /* We need to clear any ifindices we recorded so liblxc won't
3316 * have cached stale data which would cause it to fail on reboot
3317 * we're we don't re-read the on-disk config file.
3319 netdev
->ifindex
= 0;
3320 if (netdev
->type
== LXC_NET_PHYS
) {
3321 netdev
->priv
.phys_attr
.ifindex
= 0;
3322 } else if (netdev
->type
== LXC_NET_VETH
) {
3323 netdev
->priv
.veth_attr
.veth1
[0] = '\0';
3324 netdev
->priv
.veth_attr
.ifindex
= 0;
3331 int lxc_requests_empty_network(struct lxc_handler
*handler
)
3333 struct lxc_list
*network
= &handler
->conf
->network
;
3334 struct lxc_list
*iterator
;
3335 bool found_none
= false, found_nic
= false;
3337 if (lxc_list_empty(network
))
3340 lxc_list_for_each(iterator
, network
) {
3341 struct lxc_netdev
*netdev
= iterator
->elem
;
3343 if (netdev
->type
== LXC_NET_NONE
)
3348 if (found_none
&& !found_nic
)
3353 /* try to move physical nics to the init netns */
3354 int lxc_restore_phys_nics_to_netns(struct lxc_handler
*handler
)
3358 char ifname
[IFNAMSIZ
];
3359 struct lxc_list
*iterator
;
3360 int netnsfd
= handler
->nsfd
[LXC_NS_NET
];
3361 struct lxc_conf
*conf
= handler
->conf
;
3363 /* We need CAP_NET_ADMIN in the parent namespace in order to setns() to
3364 * the parent network namespace. We won't have this capability if we are
3367 if (!handler
->am_root
)
3370 TRACE("Moving physical network devices back to parent network namespace");
3372 oldfd
= lxc_preserve_ns(handler
->monitor_pid
, "net");
3374 SYSERROR("Failed to preserve network namespace");
3378 ret
= setns(netnsfd
, CLONE_NEWNET
);
3380 SYSERROR("Failed to enter network namespace");
3385 lxc_list_for_each(iterator
, &conf
->network
) {
3386 struct lxc_netdev
*netdev
= iterator
->elem
;
3388 if (netdev
->type
!= LXC_NET_PHYS
)
3391 /* Retrieve the name of the interface in the container's network
3394 if (!if_indextoname(netdev
->ifindex
, ifname
)) {
3395 WARN("No interface corresponding to ifindex %d",
3400 ret
= lxc_netdev_move_by_index_fd(netdev
->ifindex
, oldfd
, netdev
->link
);
3402 WARN("Error moving network device \"%s\" back to "
3403 "network namespace", ifname
);
3405 TRACE("Moved network device \"%s\" back to network "
3406 "namespace", ifname
);
3409 ret
= setns(oldfd
, CLONE_NEWNET
);
3412 SYSERROR("Failed to enter network namespace");
3419 static int setup_hw_addr(char *hwaddr
, const char *ifname
)
3421 struct sockaddr sockaddr
;
3425 ret
= lxc_convert_mac(hwaddr
, &sockaddr
);
3428 SYSERROR("Mac address \"%s\" conversion failed", hwaddr
);
3432 memcpy(ifr
.ifr_name
, ifname
, IFNAMSIZ
);
3433 ifr
.ifr_name
[IFNAMSIZ
-1] = '\0';
3434 memcpy((char *) &ifr
.ifr_hwaddr
, (char *) &sockaddr
, sizeof(sockaddr
));
3436 fd
= socket(AF_INET
, SOCK_DGRAM
| SOCK_CLOEXEC
, 0);
3440 ret
= ioctl(fd
, SIOCSIFHWADDR
, &ifr
);
3442 SYSERROR("Failed to perform ioctl");
3446 DEBUG("Mac address \"%s\" on \"%s\" has been setup", hwaddr
,
3452 static int setup_ipv4_addr(struct lxc_list
*ip
, int ifindex
)
3454 struct lxc_list
*iterator
;
3457 lxc_list_for_each(iterator
, ip
) {
3458 struct lxc_inetdev
*inetdev
= iterator
->elem
;
3460 err
= lxc_ipv4_addr_add(ifindex
, &inetdev
->addr
,
3461 &inetdev
->bcast
, inetdev
->prefix
);
3464 SYSERROR("Failed to setup ipv4 address for network device "
3465 "with ifindex %d", ifindex
);
3473 static int setup_ipv6_addr(struct lxc_list
*ip
, int ifindex
)
3475 struct lxc_list
*iterator
;
3478 lxc_list_for_each(iterator
, ip
) {
3479 struct lxc_inet6dev
*inet6dev
= iterator
->elem
;
3481 err
= lxc_ipv6_addr_add(ifindex
, &inet6dev
->addr
,
3482 &inet6dev
->mcast
, &inet6dev
->acast
,
3486 SYSERROR("Failed to setup ipv6 address for network device "
3487 "with ifindex %d", ifindex
);
3495 static int lxc_setup_netdev_in_child_namespaces(struct lxc_netdev
*netdev
)
3497 char ifname
[IFNAMSIZ
];
3499 char *current_ifname
= ifname
;
3500 char bufinet4
[INET_ADDRSTRLEN
], bufinet6
[INET6_ADDRSTRLEN
];
3502 /* empty network namespace */
3503 if (!netdev
->ifindex
) {
3504 if (netdev
->flags
& IFF_UP
) {
3505 err
= lxc_netdev_up("lo");
3508 SYSERROR("Failed to set the loopback network device up");
3513 if (netdev
->type
== LXC_NET_EMPTY
)
3516 if (netdev
->type
== LXC_NET_NONE
)
3519 netdev
->ifindex
= if_nametoindex(netdev
->created_name
);
3520 if (!netdev
->ifindex
)
3521 SYSERROR("Failed to retrieve ifindex for network device with name %s",
3522 netdev
->name
?: "(null)");
3525 /* get the new ifindex in case of physical netdev */
3526 if (netdev
->type
== LXC_NET_PHYS
) {
3527 netdev
->ifindex
= if_nametoindex(netdev
->link
);
3528 if (!netdev
->ifindex
) {
3529 ERROR("Failed to get ifindex for network device \"%s\"",
3535 /* retrieve the name of the interface */
3536 if (!if_indextoname(netdev
->ifindex
, current_ifname
)) {
3537 SYSERROR("Failed to retrieve name for network device with ifindex %d",
3542 /* Default: let the system choose an interface name.
3543 * When the IFLA_IFNAME attribute is passed something like "<prefix>%d"
3544 * netlink will replace the format specifier with an appropriate index.
3546 if (netdev
->name
[0] == '\0') {
3547 if (netdev
->type
== LXC_NET_PHYS
)
3548 (void)strlcpy(netdev
->name
, netdev
->link
, IFNAMSIZ
);
3550 (void)strlcpy(netdev
->name
, "eth%d", IFNAMSIZ
);
3553 /* rename the interface name */
3554 if (strcmp(current_ifname
, netdev
->name
) != 0) {
3555 err
= lxc_netdev_rename_by_name(current_ifname
, netdev
->name
);
3558 SYSERROR("Failed to rename network device \"%s\" to \"%s\"",
3559 current_ifname
, netdev
->name
);
3563 TRACE("Renamed network device from \"%s\" to \"%s\"",
3564 current_ifname
, netdev
->name
);
3567 /* Re-read the name of the interface because its name has changed
3568 * and would be automatically allocated by the system
3570 if (!if_indextoname(netdev
->ifindex
, current_ifname
)) {
3571 ERROR("Failed get name for network device with ifindex %d",
3576 /* Now update the recorded name of the network device to reflect the
3577 * name of the network device in the child's network namespace. We will
3578 * later on send this information back to the parent.
3580 (void)strlcpy(netdev
->name
, current_ifname
, IFNAMSIZ
);
3582 /* set a mac address */
3583 if (netdev
->hwaddr
) {
3584 if (setup_hw_addr(netdev
->hwaddr
, current_ifname
)) {
3585 ERROR("Failed to setup hw address for network device \"%s\"",
3591 /* setup ipv4 addresses on the interface */
3592 if (setup_ipv4_addr(&netdev
->ipv4
, netdev
->ifindex
)) {
3593 ERROR("Failed to setup ip addresses for network device \"%s\"",
3598 /* setup ipv6 addresses on the interface */
3599 if (setup_ipv6_addr(&netdev
->ipv6
, netdev
->ifindex
)) {
3600 ERROR("Failed to setup ipv6 addresses for network device \"%s\"",
3605 /* set the network device up */
3606 if (netdev
->flags
& IFF_UP
) {
3607 err
= lxc_netdev_up(current_ifname
);
3610 SYSERROR("Failed to set network device \"%s\" up",
3615 /* the network is up, make the loopback up too */
3616 err
= lxc_netdev_up("lo");
3619 SYSERROR("Failed to set the loopback network device up");
3624 /* setup ipv4 gateway on the interface */
3625 if (netdev
->ipv4_gateway
|| netdev
->ipv4_gateway_dev
) {
3626 if (!(netdev
->flags
& IFF_UP
)) {
3627 ERROR("Cannot add ipv4 gateway for network device "
3628 "\"%s\" when not bringing up the interface", current_ifname
);
3632 if (lxc_list_empty(&netdev
->ipv4
)) {
3633 ERROR("Cannot add ipv4 gateway for network device "
3634 "\"%s\" when not assigning an address", current_ifname
);
3638 /* Setup device route if ipv4_gateway_dev is enabled */
3639 if (netdev
->ipv4_gateway_dev
) {
3640 err
= lxc_ipv4_gateway_add(netdev
->ifindex
, NULL
);
3642 SYSERROR("Failed to setup ipv4 gateway to network device \"%s\"",
3644 return minus_one_set_errno(-err
);
3647 /* Check the gateway address is valid */
3648 if (!inet_ntop(AF_INET
, netdev
->ipv4_gateway
, bufinet4
, sizeof(bufinet4
)))
3649 return minus_one_set_errno(errno
);
3651 /* Try adding a default route to the gateway address */
3652 err
= lxc_ipv4_gateway_add(netdev
->ifindex
, netdev
->ipv4_gateway
);
3654 /* If adding the default route fails, this could be because the
3655 * gateway address is in a different subnet to the container's address.
3656 * To work around this, we try adding a static device route to the
3657 * gateway address first, and then try again.
3659 err
= lxc_ipv4_dest_add(netdev
->ifindex
, netdev
->ipv4_gateway
, 32);
3662 SYSERROR("Failed to add ipv4 dest \"%s\" for network device \"%s\"",
3663 bufinet4
, current_ifname
);
3667 err
= lxc_ipv4_gateway_add(netdev
->ifindex
, netdev
->ipv4_gateway
);
3670 SYSERROR("Failed to setup ipv4 gateway \"%s\" for network device \"%s\"",
3671 bufinet4
, current_ifname
);
3678 /* setup ipv6 gateway on the interface */
3679 if (netdev
->ipv6_gateway
|| netdev
->ipv6_gateway_dev
) {
3680 if (!(netdev
->flags
& IFF_UP
)) {
3681 ERROR("Cannot add ipv6 gateway for network device \"%s\" when not bringing up the interface",
3686 if (lxc_list_empty(&netdev
->ipv6
) && !IN6_IS_ADDR_LINKLOCAL(netdev
->ipv6_gateway
)) {
3687 ERROR("Cannot add ipv6 gateway for network device \"%s\" when not assigning an address",
3692 /* Setup device route if ipv6_gateway_dev is enabled */
3693 if (netdev
->ipv6_gateway_dev
) {
3694 err
= lxc_ipv6_gateway_add(netdev
->ifindex
, NULL
);
3696 SYSERROR("Failed to setup ipv6 gateway to network device \"%s\"",
3698 return minus_one_set_errno(-err
);
3701 /* Check the gateway address is valid */
3702 if (!inet_ntop(AF_INET6
, netdev
->ipv6_gateway
, bufinet6
, sizeof(bufinet6
)))
3703 return minus_one_set_errno(errno
);
3705 /* Try adding a default route to the gateway address */
3706 err
= lxc_ipv6_gateway_add(netdev
->ifindex
, netdev
->ipv6_gateway
);
3708 /* If adding the default route fails, this could be because the
3709 * gateway address is in a different subnet to the container's address.
3710 * To work around this, we try adding a static device route to the
3711 * gateway address first, and then try again.
3713 err
= lxc_ipv6_dest_add(netdev
->ifindex
, netdev
->ipv6_gateway
, 128);
3716 SYSERROR("Failed to add ipv6 dest \"%s\" for network device \"%s\"",
3717 bufinet6
, current_ifname
);
3721 err
= lxc_ipv6_gateway_add(netdev
->ifindex
, netdev
->ipv6_gateway
);
3724 SYSERROR("Failed to setup ipv6 gateway \"%s\" for network device \"%s\"",
3725 bufinet6
, current_ifname
);
3732 DEBUG("Network device \"%s\" has been setup", current_ifname
);
3737 int lxc_setup_network_in_child_namespaces(const struct lxc_conf
*conf
,
3738 struct lxc_list
*network
)
3740 struct lxc_list
*iterator
;
3742 lxc_list_for_each(iterator
, network
) {
3743 struct lxc_netdev
*netdev
= iterator
->elem
;
3745 if (lxc_setup_netdev_in_child_namespaces(netdev
)) {
3746 ERROR("Failed to setup netdev");
3751 if (!lxc_list_empty(network
))
3752 INFO("Network has been setup");
3757 int lxc_network_send_veth_names_to_child(struct lxc_handler
*handler
)
3759 struct lxc_list
*iterator
;
3760 struct lxc_list
*network
= &handler
->conf
->network
;
3761 int data_sock
= handler
->data_sock
[0];
3763 lxc_list_for_each(iterator
, network
) {
3765 struct lxc_netdev
*netdev
= iterator
->elem
;
3767 if (netdev
->type
!= LXC_NET_VETH
)
3770 ret
= lxc_send_nointr(data_sock
, netdev
->name
, IFNAMSIZ
, MSG_NOSIGNAL
);
3774 ret
= lxc_send_nointr(data_sock
, netdev
->created_name
, IFNAMSIZ
, MSG_NOSIGNAL
);
3778 TRACE("Sent network device name \"%s\" to child", netdev
->created_name
);
3784 int lxc_network_recv_veth_names_from_parent(struct lxc_handler
*handler
)
3786 struct lxc_list
*iterator
;
3787 struct lxc_list
*network
= &handler
->conf
->network
;
3788 int data_sock
= handler
->data_sock
[1];
3790 lxc_list_for_each(iterator
, network
) {
3792 struct lxc_netdev
*netdev
= iterator
->elem
;
3794 if (netdev
->type
!= LXC_NET_VETH
)
3797 ret
= lxc_recv_nointr(data_sock
, netdev
->name
, IFNAMSIZ
, 0);
3801 ret
= lxc_recv_nointr(data_sock
, netdev
->created_name
, IFNAMSIZ
, 0);
3804 TRACE("Received network device name \"%s\" from parent", netdev
->created_name
);
3810 int lxc_network_send_name_and_ifindex_to_parent(struct lxc_handler
*handler
)
3812 struct lxc_list
*iterator
, *network
;
3813 int data_sock
= handler
->data_sock
[0];
3815 if (!handler
->am_root
)
3818 network
= &handler
->conf
->network
;
3819 lxc_list_for_each(iterator
, network
) {
3821 struct lxc_netdev
*netdev
= iterator
->elem
;
3823 /* Send network device name in the child's namespace to parent. */
3824 ret
= lxc_send_nointr(data_sock
, netdev
->name
, IFNAMSIZ
, MSG_NOSIGNAL
);
3828 /* Send network device ifindex in the child's namespace to
3831 ret
= lxc_send_nointr(data_sock
, &netdev
->ifindex
, sizeof(netdev
->ifindex
), MSG_NOSIGNAL
);
3836 if (!lxc_list_empty(network
))
3837 TRACE("Sent network device names and ifindices to parent");
3842 int lxc_network_recv_name_and_ifindex_from_child(struct lxc_handler
*handler
)
3844 struct lxc_list
*iterator
, *network
;
3845 int data_sock
= handler
->data_sock
[1];
3847 if (!handler
->am_root
)
3850 network
= &handler
->conf
->network
;
3851 lxc_list_for_each(iterator
, network
) {
3853 struct lxc_netdev
*netdev
= iterator
->elem
;
3855 /* Receive network device name in the child's namespace to
3858 ret
= lxc_recv_nointr(data_sock
, netdev
->name
, IFNAMSIZ
, 0);
3862 /* Receive network device ifindex in the child's namespace to
3865 ret
= lxc_recv_nointr(data_sock
, &netdev
->ifindex
, sizeof(netdev
->ifindex
), 0);
3873 void lxc_delete_network(struct lxc_handler
*handler
)
3877 if (handler
->am_root
)
3878 bret
= lxc_delete_network_priv(handler
);
3880 bret
= lxc_delete_network_unpriv(handler
);
3882 DEBUG("Failed to delete network devices");
3884 DEBUG("Deleted network devices");
3887 int lxc_netns_set_nsid(int fd
)
3890 char buf
[NLMSG_ALIGN(sizeof(struct nlmsghdr
)) +
3891 NLMSG_ALIGN(sizeof(struct rtgenmsg
)) +
3893 struct nl_handler nlh
;
3894 struct nlmsghdr
*hdr
;
3895 struct rtgenmsg
*msg
;
3897 const __s32 ns_id
= -1;
3898 const __u32 netns_fd
= fd
;
3900 ret
= netlink_open(&nlh
, NETLINK_ROUTE
);
3904 memset(buf
, 0, sizeof(buf
));
3906 #pragma GCC diagnostic push
3907 #pragma GCC diagnostic ignored "-Wcast-align"
3908 hdr
= (struct nlmsghdr
*)buf
;
3909 msg
= (struct rtgenmsg
*)NLMSG_DATA(hdr
);
3910 #pragma GCC diagnostic pop
3912 hdr
->nlmsg_len
= NLMSG_LENGTH(sizeof(*msg
));
3913 hdr
->nlmsg_type
= RTM_NEWNSID
;
3914 hdr
->nlmsg_flags
= NLM_F_REQUEST
| NLM_F_ACK
;
3916 hdr
->nlmsg_seq
= RTM_NEWNSID
;
3917 msg
->rtgen_family
= AF_UNSPEC
;
3919 ret
= addattr(hdr
, 1024, __LXC_NETNSA_FD
, &netns_fd
, sizeof(netns_fd
));
3923 ret
= addattr(hdr
, 1024, __LXC_NETNSA_NSID
, &ns_id
, sizeof(ns_id
));
3927 ret
= __netlink_transaction(&nlh
, hdr
, hdr
);
3930 saved_errno
= errno
;
3931 netlink_close(&nlh
);
3932 errno
= saved_errno
;
3937 static int parse_rtattr(struct rtattr
*tb
[], int max
, struct rtattr
*rta
, int len
)
3940 memset(tb
, 0, sizeof(struct rtattr
*) * (max
+ 1));
3942 while (RTA_OK(rta
, len
)) {
3943 unsigned short type
= rta
->rta_type
;
3945 if ((type
<= max
) && (!tb
[type
]))
3948 #pragma GCC diagnostic push
3949 #pragma GCC diagnostic ignored "-Wcast-align"
3950 rta
= RTA_NEXT(rta
, len
);
3951 #pragma GCC diagnostic pop
3957 static inline __s32
rta_getattr_s32(const struct rtattr
*rta
)
3959 return *(__s32
*)RTA_DATA(rta
);
3963 #define NETNS_RTA(r) \
3964 ((struct rtattr *)(((char *)(r)) + NLMSG_ALIGN(sizeof(struct rtgenmsg))))
3967 int lxc_netns_get_nsid(int fd
)
3971 char buf
[NLMSG_ALIGN(sizeof(struct nlmsghdr
)) +
3972 NLMSG_ALIGN(sizeof(struct rtgenmsg
)) +
3974 struct rtattr
*tb
[__LXC_NETNSA_MAX
+ 1];
3975 struct nl_handler nlh
;
3976 struct nlmsghdr
*hdr
;
3977 struct rtgenmsg
*msg
;
3979 __u32 netns_fd
= fd
;
3981 ret
= netlink_open(&nlh
, NETLINK_ROUTE
);
3985 memset(buf
, 0, sizeof(buf
));
3987 #pragma GCC diagnostic push
3988 #pragma GCC diagnostic ignored "-Wcast-align"
3989 hdr
= (struct nlmsghdr
*)buf
;
3990 msg
= (struct rtgenmsg
*)NLMSG_DATA(hdr
);
3991 #pragma GCC diagnostic pop
3993 hdr
->nlmsg_len
= NLMSG_LENGTH(sizeof(*msg
));
3994 hdr
->nlmsg_type
= RTM_GETNSID
;
3995 hdr
->nlmsg_flags
= NLM_F_REQUEST
| NLM_F_ACK
;
3997 hdr
->nlmsg_seq
= RTM_GETNSID
;
3998 msg
->rtgen_family
= AF_UNSPEC
;
4000 ret
= addattr(hdr
, 1024, __LXC_NETNSA_FD
, &netns_fd
, sizeof(netns_fd
));
4002 ret
= __netlink_transaction(&nlh
, hdr
, hdr
);
4004 saved_errno
= errno
;
4005 netlink_close(&nlh
);
4006 errno
= saved_errno
;
4011 msg
= NLMSG_DATA(hdr
);
4012 len
= hdr
->nlmsg_len
- NLMSG_SPACE(sizeof(*msg
));
4016 #pragma GCC diagnostic push
4017 #pragma GCC diagnostic ignored "-Wcast-align"
4018 parse_rtattr(tb
, __LXC_NETNSA_MAX
, NETNS_RTA(msg
), len
);
4019 if (tb
[__LXC_NETNSA_NSID
])
4020 return rta_getattr_s32(tb
[__LXC_NETNSA_NSID
]);
4021 #pragma GCC diagnostic pop
4026 int lxc_create_network(struct lxc_handler
*handler
)
4031 * Find gateway addresses from the link device, which is no longer
4032 * accessible inside the container. Do this before creating network
4033 * interfaces, since goto out_delete_net does not work before
4036 ret
= lxc_find_gateway_addresses(handler
);
4038 ERROR("Failed to find gateway addresses");
4042 if (handler
->am_root
) {
4043 ret
= lxc_create_network_priv(handler
);
4047 return lxc_network_move_created_netdev_priv(handler
);
4050 return lxc_create_network_unpriv(handler
);