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 netdev
->ifindex
= if_nametoindex(peer
);
522 if (!netdev
->ifindex
) {
523 ERROR("Failed to retrieve ifindex for \"%s\"", peer
);
528 err
= lxc_safe_uint(netdev
->mtu
, &mtu
);
531 SYSERROR("Failed to parse mtu \"%s\" for interface \"%s\"",
536 err
= lxc_netdev_set_mtu(peer
, mtu
);
539 SYSERROR("Failed to set mtu \"%s\" for interface \"%s\"",
545 if (netdev
->upscript
) {
552 err
= run_script_argv(handler
->name
, handler
->conf
->hooks_version
,
553 "net", netdev
->upscript
, "up", argv
);
558 DEBUG("Instantiated ipvlan \"%s\" with ifindex is %d and mode %d", peer
,
559 netdev
->ifindex
, netdev
->priv
.macvlan_attr
.mode
);
564 lxc_netdev_delete_by_name(peer
);
568 static int instantiate_vlan(struct lxc_handler
*handler
, struct lxc_netdev
*netdev
)
572 static uint16_t vlan_cntr
= 0;
573 unsigned int mtu
= 0;
575 if (netdev
->link
[0] == '\0') {
576 ERROR("No link for vlan network device specified");
580 err
= snprintf(peer
, sizeof(peer
), "vlan%d-%d", netdev
->priv
.vlan_attr
.vid
, vlan_cntr
++);
581 if (err
< 0 || (size_t)err
>= sizeof(peer
))
584 err
= lxc_vlan_create(netdev
->link
, peer
, netdev
->priv
.vlan_attr
.vid
);
587 SYSERROR("Failed to create vlan interface \"%s\" on \"%s\"",
592 netdev
->ifindex
= if_nametoindex(peer
);
593 if (!netdev
->ifindex
) {
594 ERROR("Failed to retrieve ifindex for \"%s\"", peer
);
599 err
= lxc_safe_uint(netdev
->mtu
, &mtu
);
602 SYSERROR("Failed to parse mtu \"%s\" for interface \"%s\"", netdev
->mtu
, peer
);
606 err
= lxc_netdev_set_mtu(peer
, mtu
);
609 SYSERROR("Failed to set mtu \"%s\" for interface \"%s\"", netdev
->mtu
, peer
);
614 if (netdev
->upscript
) {
621 err
= run_script_argv(handler
->name
,
622 handler
->conf
->hooks_version
, "net",
623 netdev
->upscript
, "up", argv
);
629 DEBUG("Instantiated vlan \"%s\" with ifindex is \"%d\"",
630 peer
, netdev
->ifindex
);
635 lxc_netdev_delete_by_name(peer
);
639 static int instantiate_phys(struct lxc_handler
*handler
, struct lxc_netdev
*netdev
)
641 int err
, mtu_orig
= 0;
642 unsigned int mtu
= 0;
644 if (netdev
->link
[0] == '\0') {
645 ERROR("No link for physical interface specified");
649 /* Note that we're retrieving the container's ifindex in the host's
650 * network namespace because we need it to move the device from the
651 * host's network namespace to the container's network namespace later
653 * Note that netdev->link will contain the name of the physical network
654 * device in the host's namespace.
656 netdev
->ifindex
= if_nametoindex(netdev
->link
);
657 if (!netdev
->ifindex
) {
658 ERROR("Failed to retrieve ifindex for \"%s\"", netdev
->link
);
662 /* Store the ifindex of the host's network device in the host's
665 netdev
->priv
.phys_attr
.ifindex
= netdev
->ifindex
;
667 /* Get original device MTU setting and store for restoration after container shutdown. */
668 mtu_orig
= netdev_get_mtu(netdev
->ifindex
);
670 SYSERROR("Failed to get original mtu for interface \"%s\"", netdev
->link
);
671 return minus_one_set_errno(-mtu_orig
);
674 netdev
->priv
.phys_attr
.mtu
= mtu_orig
;
677 err
= lxc_safe_uint(netdev
->mtu
, &mtu
);
680 SYSERROR("Failed to parse mtu \"%s\" for interface \"%s\"", netdev
->mtu
, netdev
->link
);
684 err
= lxc_netdev_set_mtu(netdev
->link
, mtu
);
687 SYSERROR("Failed to set mtu \"%s\" for interface \"%s\"", netdev
->mtu
, netdev
->link
);
692 if (netdev
->upscript
) {
699 err
= run_script_argv(handler
->name
,
700 handler
->conf
->hooks_version
, "net",
701 netdev
->upscript
, "up", argv
);
707 DEBUG("Instantiated phys \"%s\" with ifindex is \"%d\"", netdev
->link
, netdev
->ifindex
);
712 static int instantiate_empty(struct lxc_handler
*handler
, struct lxc_netdev
*netdev
)
721 if (!netdev
->upscript
)
724 ret
= run_script_argv(handler
->name
, handler
->conf
->hooks_version
,
725 "net", netdev
->upscript
, "up", argv
);
732 static int instantiate_none(struct lxc_handler
*handler
, struct lxc_netdev
*netdev
)
738 static instantiate_cb netdev_conf
[LXC_NET_MAXCONFTYPE
+ 1] = {
739 [LXC_NET_VETH
] = instantiate_veth
,
740 [LXC_NET_MACVLAN
] = instantiate_macvlan
,
741 [LXC_NET_IPVLAN
] = instantiate_ipvlan
,
742 [LXC_NET_VLAN
] = instantiate_vlan
,
743 [LXC_NET_PHYS
] = instantiate_phys
,
744 [LXC_NET_EMPTY
] = instantiate_empty
,
745 [LXC_NET_NONE
] = instantiate_none
,
748 static int shutdown_veth(struct lxc_handler
*handler
, struct lxc_netdev
*netdev
)
758 if (!netdev
->downscript
)
761 if (netdev
->priv
.veth_attr
.pair
[0] != '\0')
762 argv
[2] = netdev
->priv
.veth_attr
.pair
;
764 argv
[2] = netdev
->priv
.veth_attr
.veth1
;
766 ret
= run_script_argv(handler
->name
,
767 handler
->conf
->hooks_version
, "net",
768 netdev
->downscript
, "down", argv
);
775 static int shutdown_macvlan(struct lxc_handler
*handler
, struct lxc_netdev
*netdev
)
784 if (!netdev
->downscript
)
787 ret
= run_script_argv(handler
->name
, handler
->conf
->hooks_version
,
788 "net", netdev
->downscript
, "down", argv
);
795 static int shutdown_ipvlan(struct lxc_handler
*handler
, struct lxc_netdev
*netdev
)
804 if (!netdev
->downscript
)
807 ret
= run_script_argv(handler
->name
, handler
->conf
->hooks_version
,
808 "net", netdev
->downscript
, "down", argv
);
815 static int shutdown_vlan(struct lxc_handler
*handler
, struct lxc_netdev
*netdev
)
824 if (!netdev
->downscript
)
827 ret
= run_script_argv(handler
->name
, handler
->conf
->hooks_version
,
828 "net", netdev
->downscript
, "down", argv
);
835 static int shutdown_phys(struct lxc_handler
*handler
, struct lxc_netdev
*netdev
)
844 if (!netdev
->downscript
)
847 ret
= run_script_argv(handler
->name
, handler
->conf
->hooks_version
,
848 "net", netdev
->downscript
, "down", argv
);
855 static int shutdown_empty(struct lxc_handler
*handler
, struct lxc_netdev
*netdev
)
863 if (!netdev
->downscript
)
866 ret
= run_script_argv(handler
->name
, handler
->conf
->hooks_version
,
867 "net", netdev
->downscript
, "down", argv
);
874 static int shutdown_none(struct lxc_handler
*handler
, struct lxc_netdev
*netdev
)
879 static instantiate_cb netdev_deconf
[LXC_NET_MAXCONFTYPE
+ 1] = {
880 [LXC_NET_VETH
] = shutdown_veth
,
881 [LXC_NET_MACVLAN
] = shutdown_macvlan
,
882 [LXC_NET_IPVLAN
] = shutdown_ipvlan
,
883 [LXC_NET_VLAN
] = shutdown_vlan
,
884 [LXC_NET_PHYS
] = shutdown_phys
,
885 [LXC_NET_EMPTY
] = shutdown_empty
,
886 [LXC_NET_NONE
] = shutdown_none
,
889 static int lxc_netdev_move_by_index_fd(int ifindex
, int fd
, const char *ifname
)
892 struct nl_handler nlh
;
893 struct ifinfomsg
*ifi
;
894 struct nlmsg
*nlmsg
= NULL
;
896 err
= netlink_open(&nlh
, NETLINK_ROUTE
);
901 nlmsg
= nlmsg_alloc(NLMSG_GOOD_SIZE
);
905 nlmsg
->nlmsghdr
->nlmsg_flags
= NLM_F_REQUEST
| NLM_F_ACK
;
906 nlmsg
->nlmsghdr
->nlmsg_type
= RTM_NEWLINK
;
908 ifi
= nlmsg_reserve(nlmsg
, sizeof(struct ifinfomsg
));
911 ifi
->ifi_family
= AF_UNSPEC
;
912 ifi
->ifi_index
= ifindex
;
914 if (nla_put_u32(nlmsg
, IFLA_NET_NS_FD
, fd
))
917 if (ifname
!= NULL
) {
918 if (nla_put_string(nlmsg
, IFLA_IFNAME
, ifname
))
922 err
= netlink_transaction(&nlh
, nlmsg
, nlmsg
);
929 int lxc_netdev_move_by_index(int ifindex
, pid_t pid
, const char *ifname
)
932 struct nl_handler nlh
;
933 struct ifinfomsg
*ifi
;
934 struct nlmsg
*nlmsg
= NULL
;
936 err
= netlink_open(&nlh
, NETLINK_ROUTE
);
941 nlmsg
= nlmsg_alloc(NLMSG_GOOD_SIZE
);
945 nlmsg
->nlmsghdr
->nlmsg_flags
= NLM_F_REQUEST
| NLM_F_ACK
;
946 nlmsg
->nlmsghdr
->nlmsg_type
= RTM_NEWLINK
;
948 ifi
= nlmsg_reserve(nlmsg
, sizeof(struct ifinfomsg
));
951 ifi
->ifi_family
= AF_UNSPEC
;
952 ifi
->ifi_index
= ifindex
;
954 if (nla_put_u32(nlmsg
, IFLA_NET_NS_PID
, pid
))
957 if (ifname
!= NULL
) {
958 if (nla_put_string(nlmsg
, IFLA_IFNAME
, ifname
))
962 err
= netlink_transaction(&nlh
, nlmsg
, nlmsg
);
969 /* If we are asked to move a wireless interface, then we must actually move its
970 * phyN device. Detect that condition and return the physname here. The physname
971 * will be passed to lxc_netdev_move_wlan() which will free it when done.
973 #define PHYSNAME "/sys/class/net/%s/phy80211/name"
974 static char *is_wlan(const char *ifname
)
976 __do_free
char *path
= NULL
;
981 char *physname
= NULL
;
983 len
= strlen(ifname
) + strlen(PHYSNAME
) - 1;
984 path
= must_realloc(NULL
, len
+ 1);
985 ret
= snprintf(path
, len
, PHYSNAME
, ifname
);
986 if (ret
< 0 || (size_t)ret
>= len
)
989 f
= fopen(path
, "r");
993 /* Feh - sb.st_size is always 4096. */
994 fseek(f
, 0, SEEK_END
);
996 fseek(f
, 0, SEEK_SET
);
1002 physname
= malloc(physlen
+ 1);
1008 memset(physname
, 0, physlen
+ 1);
1009 ret
= fread(physname
, 1, physlen
, f
);
1014 for (i
= 0; i
< physlen
; i
++) {
1015 if (physname
[i
] == '\n')
1018 if (physname
[i
] == '\0')
1029 static int lxc_netdev_rename_by_name_in_netns(pid_t pid
, const char *old
,
1039 return wait_for_pid(fpid
);
1041 if (!switch_to_ns(pid
, "net"))
1044 _exit(lxc_netdev_rename_by_name(old
, new));
1047 static int lxc_netdev_move_wlan(char *physname
, const char *ifname
, pid_t pid
,
1048 const char *newname
)
1054 /* Move phyN into the container. TODO - do this using netlink.
1055 * However, IIUC this involves a bit more complicated work to talk to
1056 * the 80211 module, so for now just call out to iw.
1058 cmd
= on_path("iw", NULL
);
1069 sprintf(pidstr
, "%d", pid
);
1070 execlp("iw", "iw", "phy", physname
, "set", "netns", pidstr
,
1072 _exit(EXIT_FAILURE
);
1075 if (wait_for_pid(fpid
))
1080 err
= lxc_netdev_rename_by_name_in_netns(pid
, ifname
, newname
);
1087 int lxc_netdev_move_by_name(const char *ifname
, pid_t pid
, const char* newname
)
1095 index
= if_nametoindex(ifname
);
1099 physname
= is_wlan(ifname
);
1101 return lxc_netdev_move_wlan(physname
, ifname
, pid
, newname
);
1103 return lxc_netdev_move_by_index(index
, pid
, newname
);
1106 int lxc_netdev_delete_by_index(int ifindex
)
1109 struct ifinfomsg
*ifi
;
1110 struct nl_handler nlh
;
1111 struct nlmsg
*answer
= NULL
, *nlmsg
= NULL
;
1113 err
= netlink_open(&nlh
, NETLINK_ROUTE
);
1118 nlmsg
= nlmsg_alloc(NLMSG_GOOD_SIZE
);
1122 answer
= nlmsg_alloc_reserve(NLMSG_GOOD_SIZE
);
1126 nlmsg
->nlmsghdr
->nlmsg_flags
= NLM_F_ACK
| NLM_F_REQUEST
;
1127 nlmsg
->nlmsghdr
->nlmsg_type
= RTM_DELLINK
;
1129 ifi
= nlmsg_reserve(nlmsg
, sizeof(struct ifinfomsg
));
1132 ifi
->ifi_family
= AF_UNSPEC
;
1133 ifi
->ifi_index
= ifindex
;
1135 err
= netlink_transaction(&nlh
, nlmsg
, answer
);
1137 netlink_close(&nlh
);
1143 int lxc_netdev_delete_by_name(const char *name
)
1147 index
= if_nametoindex(name
);
1151 return lxc_netdev_delete_by_index(index
);
1154 int lxc_netdev_rename_by_index(int ifindex
, const char *newname
)
1157 struct ifinfomsg
*ifi
;
1158 struct nl_handler nlh
;
1159 struct nlmsg
*answer
= NULL
, *nlmsg
= NULL
;
1161 err
= netlink_open(&nlh
, NETLINK_ROUTE
);
1165 len
= strlen(newname
);
1166 if (len
== 1 || len
>= IFNAMSIZ
) {
1172 nlmsg
= nlmsg_alloc(NLMSG_GOOD_SIZE
);
1176 answer
= nlmsg_alloc_reserve(NLMSG_GOOD_SIZE
);
1180 nlmsg
->nlmsghdr
->nlmsg_flags
= NLM_F_ACK
| NLM_F_REQUEST
;
1181 nlmsg
->nlmsghdr
->nlmsg_type
= RTM_NEWLINK
;
1183 ifi
= nlmsg_reserve(nlmsg
, sizeof(struct ifinfomsg
));
1186 ifi
->ifi_family
= AF_UNSPEC
;
1187 ifi
->ifi_index
= ifindex
;
1189 if (nla_put_string(nlmsg
, IFLA_IFNAME
, newname
))
1192 err
= netlink_transaction(&nlh
, nlmsg
, answer
);
1194 netlink_close(&nlh
);
1200 int lxc_netdev_rename_by_name(const char *oldname
, const char *newname
)
1204 len
= strlen(oldname
);
1205 if (len
== 1 || len
>= IFNAMSIZ
)
1208 index
= if_nametoindex(oldname
);
1212 return lxc_netdev_rename_by_index(index
, newname
);
1215 int netdev_set_flag(const char *name
, int flag
)
1217 int err
, index
, len
;
1218 struct ifinfomsg
*ifi
;
1219 struct nl_handler nlh
;
1220 struct nlmsg
*answer
= NULL
, *nlmsg
= NULL
;
1222 err
= netlink_open(&nlh
, NETLINK_ROUTE
);
1228 if (len
== 1 || len
>= IFNAMSIZ
)
1232 nlmsg
= nlmsg_alloc(NLMSG_GOOD_SIZE
);
1236 answer
= nlmsg_alloc_reserve(NLMSG_GOOD_SIZE
);
1241 index
= if_nametoindex(name
);
1245 nlmsg
->nlmsghdr
->nlmsg_flags
= NLM_F_REQUEST
| NLM_F_ACK
;
1246 nlmsg
->nlmsghdr
->nlmsg_type
= RTM_NEWLINK
;
1248 ifi
= nlmsg_reserve(nlmsg
, sizeof(struct ifinfomsg
));
1253 ifi
->ifi_family
= AF_UNSPEC
;
1254 ifi
->ifi_index
= index
;
1255 ifi
->ifi_change
|= IFF_UP
;
1256 ifi
->ifi_flags
|= flag
;
1258 err
= netlink_transaction(&nlh
, nlmsg
, answer
);
1260 netlink_close(&nlh
);
1266 int netdev_get_flag(const char *name
, int *flag
)
1268 int err
, index
, len
;
1269 struct ifinfomsg
*ifi
;
1270 struct nl_handler nlh
;
1271 struct nlmsg
*answer
= NULL
, *nlmsg
= NULL
;
1276 err
= netlink_open(&nlh
, NETLINK_ROUTE
);
1282 if (len
== 1 || len
>= IFNAMSIZ
)
1286 nlmsg
= nlmsg_alloc(NLMSG_GOOD_SIZE
);
1290 answer
= nlmsg_alloc_reserve(NLMSG_GOOD_SIZE
);
1295 index
= if_nametoindex(name
);
1299 nlmsg
->nlmsghdr
->nlmsg_flags
= NLM_F_REQUEST
;
1300 nlmsg
->nlmsghdr
->nlmsg_type
= RTM_GETLINK
;
1302 ifi
= nlmsg_reserve(nlmsg
, sizeof(struct ifinfomsg
));
1307 ifi
->ifi_family
= AF_UNSPEC
;
1308 ifi
->ifi_index
= index
;
1310 err
= netlink_transaction(&nlh
, nlmsg
, answer
);
1314 ifi
= NLMSG_DATA(answer
->nlmsghdr
);
1316 *flag
= ifi
->ifi_flags
;
1318 netlink_close(&nlh
);
1325 * \brief Check a interface is up or not.
1327 * \param name: name for the interface.
1330 * 0 means interface is down.
1331 * 1 means interface is up.
1332 * Others means error happened, and ret-value is the error number.
1334 int lxc_netdev_isup(const char *name
)
1338 err
= netdev_get_flag(name
, &flag
);
1348 int netdev_get_mtu(int ifindex
)
1350 int answer_len
, err
, res
;
1351 struct nl_handler nlh
;
1352 struct ifinfomsg
*ifi
;
1353 struct nlmsghdr
*msg
;
1354 int readmore
= 0, recv_len
= 0;
1355 struct nlmsg
*answer
= NULL
, *nlmsg
= NULL
;
1357 err
= netlink_open(&nlh
, NETLINK_ROUTE
);
1362 nlmsg
= nlmsg_alloc(NLMSG_GOOD_SIZE
);
1366 answer
= nlmsg_alloc_reserve(NLMSG_GOOD_SIZE
);
1370 /* Save the answer buffer length, since it will be overwritten
1371 * on the first receive (and we might need to receive more than
1374 answer_len
= answer
->nlmsghdr
->nlmsg_len
;
1376 nlmsg
->nlmsghdr
->nlmsg_flags
= NLM_F_REQUEST
| NLM_F_DUMP
;
1377 nlmsg
->nlmsghdr
->nlmsg_type
= RTM_GETLINK
;
1379 ifi
= nlmsg_reserve(nlmsg
, sizeof(struct ifinfomsg
));
1382 ifi
->ifi_family
= AF_UNSPEC
;
1384 /* Send the request for addresses, which returns all addresses
1385 * on all interfaces. */
1386 err
= netlink_send(&nlh
, nlmsg
);
1390 #pragma GCC diagnostic push
1391 #pragma GCC diagnostic ignored "-Wcast-align"
1394 /* Restore the answer buffer length, it might have been
1395 * overwritten by a previous receive.
1397 answer
->nlmsghdr
->nlmsg_len
= answer_len
;
1399 /* Get the (next) batch of reply messages */
1400 err
= netlink_rcv(&nlh
, answer
);
1406 /* Satisfy the typing for the netlink macros */
1407 msg
= answer
->nlmsghdr
;
1409 while (NLMSG_OK(msg
, recv_len
)) {
1411 /* Stop reading if we see an error message */
1412 if (msg
->nlmsg_type
== NLMSG_ERROR
) {
1413 struct nlmsgerr
*errmsg
=
1414 (struct nlmsgerr
*)NLMSG_DATA(msg
);
1415 err
= errmsg
->error
;
1419 /* Stop reading if we see a NLMSG_DONE message */
1420 if (msg
->nlmsg_type
== NLMSG_DONE
) {
1425 ifi
= NLMSG_DATA(msg
);
1426 if (ifi
->ifi_index
== ifindex
) {
1427 struct rtattr
*rta
= IFLA_RTA(ifi
);
1429 msg
->nlmsg_len
- NLMSG_LENGTH(sizeof(*ifi
));
1431 while (RTA_OK(rta
, attr_len
)) {
1432 /* Found a local address for the
1433 * requested interface, return it.
1435 if (rta
->rta_type
== IFLA_MTU
) {
1436 memcpy(&res
, RTA_DATA(rta
),
1441 rta
= RTA_NEXT(rta
, attr_len
);
1445 /* Keep reading more data from the socket if the last
1446 * message had the NLF_F_MULTI flag set.
1448 readmore
= (msg
->nlmsg_flags
& NLM_F_MULTI
);
1450 /* Look at the next message received in this buffer. */
1451 msg
= NLMSG_NEXT(msg
, recv_len
);
1455 #pragma GCC diagnostic pop
1457 /* If we end up here, we didn't find any result, so signal an error. */
1461 netlink_close(&nlh
);
1467 int lxc_netdev_set_mtu(const char *name
, int mtu
)
1469 int err
, index
, len
;
1470 struct ifinfomsg
*ifi
;
1471 struct nl_handler nlh
;
1472 struct nlmsg
*answer
= NULL
, *nlmsg
= NULL
;
1474 err
= netlink_open(&nlh
, NETLINK_ROUTE
);
1480 if (len
== 1 || len
>= IFNAMSIZ
)
1484 nlmsg
= nlmsg_alloc(NLMSG_GOOD_SIZE
);
1488 answer
= nlmsg_alloc_reserve(NLMSG_GOOD_SIZE
);
1493 index
= if_nametoindex(name
);
1497 nlmsg
->nlmsghdr
->nlmsg_flags
= NLM_F_REQUEST
| NLM_F_ACK
;
1498 nlmsg
->nlmsghdr
->nlmsg_type
= RTM_NEWLINK
;
1500 ifi
= nlmsg_reserve(nlmsg
, sizeof(struct ifinfomsg
));
1505 ifi
->ifi_family
= AF_UNSPEC
;
1506 ifi
->ifi_index
= index
;
1508 if (nla_put_u32(nlmsg
, IFLA_MTU
, mtu
))
1511 err
= netlink_transaction(&nlh
, nlmsg
, answer
);
1513 netlink_close(&nlh
);
1519 int lxc_netdev_up(const char *name
)
1521 return netdev_set_flag(name
, IFF_UP
);
1524 int lxc_netdev_down(const char *name
)
1526 return netdev_set_flag(name
, 0);
1529 int lxc_veth_create(const char *name1
, const char *name2
)
1532 struct ifinfomsg
*ifi
;
1533 struct nl_handler nlh
;
1534 struct rtattr
*nest1
, *nest2
, *nest3
;
1535 struct nlmsg
*answer
= NULL
, *nlmsg
= NULL
;
1537 err
= netlink_open(&nlh
, NETLINK_ROUTE
);
1542 len
= strlen(name1
);
1543 if (len
== 1 || len
>= IFNAMSIZ
)
1546 len
= strlen(name2
);
1547 if (len
== 1 || len
>= IFNAMSIZ
)
1551 nlmsg
= nlmsg_alloc(NLMSG_GOOD_SIZE
);
1555 answer
= nlmsg_alloc_reserve(NLMSG_GOOD_SIZE
);
1559 nlmsg
->nlmsghdr
->nlmsg_flags
=
1560 NLM_F_REQUEST
| NLM_F_CREATE
| NLM_F_EXCL
| NLM_F_ACK
;
1561 nlmsg
->nlmsghdr
->nlmsg_type
= RTM_NEWLINK
;
1563 ifi
= nlmsg_reserve(nlmsg
, sizeof(struct ifinfomsg
));
1566 ifi
->ifi_family
= AF_UNSPEC
;
1569 nest1
= nla_begin_nested(nlmsg
, IFLA_LINKINFO
);
1573 if (nla_put_string(nlmsg
, IFLA_INFO_KIND
, "veth"))
1576 nest2
= nla_begin_nested(nlmsg
, IFLA_INFO_DATA
);
1580 nest3
= nla_begin_nested(nlmsg
, VETH_INFO_PEER
);
1584 ifi
= nlmsg_reserve(nlmsg
, sizeof(struct ifinfomsg
));
1590 if (nla_put_string(nlmsg
, IFLA_IFNAME
, name2
))
1593 nla_end_nested(nlmsg
, nest3
);
1594 nla_end_nested(nlmsg
, nest2
);
1595 nla_end_nested(nlmsg
, nest1
);
1597 if (nla_put_string(nlmsg
, IFLA_IFNAME
, name1
))
1600 err
= netlink_transaction(&nlh
, nlmsg
, answer
);
1602 netlink_close(&nlh
);
1608 /* TODO: merge with lxc_macvlan_create */
1609 int lxc_vlan_create(const char *master
, const char *name
, unsigned short vlanid
)
1611 int err
, len
, lindex
;
1612 struct ifinfomsg
*ifi
;
1613 struct nl_handler nlh
;
1614 struct rtattr
*nest
, *nest2
;
1615 struct nlmsg
*answer
= NULL
, *nlmsg
= NULL
;
1617 err
= netlink_open(&nlh
, NETLINK_ROUTE
);
1622 len
= strlen(master
);
1623 if (len
== 1 || len
>= IFNAMSIZ
)
1627 if (len
== 1 || len
>= IFNAMSIZ
)
1631 nlmsg
= nlmsg_alloc(NLMSG_GOOD_SIZE
);
1635 answer
= nlmsg_alloc_reserve(NLMSG_GOOD_SIZE
);
1640 lindex
= if_nametoindex(master
);
1644 nlmsg
->nlmsghdr
->nlmsg_flags
=
1645 NLM_F_REQUEST
| NLM_F_CREATE
| NLM_F_EXCL
| NLM_F_ACK
;
1646 nlmsg
->nlmsghdr
->nlmsg_type
= RTM_NEWLINK
;
1648 ifi
= nlmsg_reserve(nlmsg
, sizeof(struct ifinfomsg
));
1653 ifi
->ifi_family
= AF_UNSPEC
;
1655 nest
= nla_begin_nested(nlmsg
, IFLA_LINKINFO
);
1659 if (nla_put_string(nlmsg
, IFLA_INFO_KIND
, "vlan"))
1662 nest2
= nla_begin_nested(nlmsg
, IFLA_INFO_DATA
);
1666 if (nla_put_u16(nlmsg
, IFLA_VLAN_ID
, vlanid
))
1669 nla_end_nested(nlmsg
, nest2
);
1670 nla_end_nested(nlmsg
, nest
);
1672 if (nla_put_u32(nlmsg
, IFLA_LINK
, lindex
))
1675 if (nla_put_string(nlmsg
, IFLA_IFNAME
, name
))
1678 err
= netlink_transaction(&nlh
, nlmsg
, answer
);
1684 netlink_close(&nlh
);
1688 int lxc_macvlan_create(const char *master
, const char *name
, int mode
)
1690 int err
, index
, len
;
1691 struct ifinfomsg
*ifi
;
1692 struct nl_handler nlh
;
1693 struct rtattr
*nest
, *nest2
;
1694 struct nlmsg
*answer
= NULL
, *nlmsg
= NULL
;
1696 err
= netlink_open(&nlh
, NETLINK_ROUTE
);
1701 len
= strlen(master
);
1702 if (len
== 1 || len
>= IFNAMSIZ
)
1706 if (len
== 1 || len
>= IFNAMSIZ
)
1710 nlmsg
= nlmsg_alloc(NLMSG_GOOD_SIZE
);
1714 answer
= nlmsg_alloc_reserve(NLMSG_GOOD_SIZE
);
1719 index
= if_nametoindex(master
);
1723 nlmsg
->nlmsghdr
->nlmsg_flags
=
1724 NLM_F_REQUEST
| NLM_F_CREATE
| NLM_F_EXCL
| NLM_F_ACK
;
1725 nlmsg
->nlmsghdr
->nlmsg_type
= RTM_NEWLINK
;
1727 ifi
= nlmsg_reserve(nlmsg
, sizeof(struct ifinfomsg
));
1732 ifi
->ifi_family
= AF_UNSPEC
;
1734 nest
= nla_begin_nested(nlmsg
, IFLA_LINKINFO
);
1738 if (nla_put_string(nlmsg
, IFLA_INFO_KIND
, "macvlan"))
1742 nest2
= nla_begin_nested(nlmsg
, IFLA_INFO_DATA
);
1746 if (nla_put_u32(nlmsg
, IFLA_MACVLAN_MODE
, mode
))
1749 nla_end_nested(nlmsg
, nest2
);
1752 nla_end_nested(nlmsg
, nest
);
1754 if (nla_put_u32(nlmsg
, IFLA_LINK
, index
))
1757 if (nla_put_string(nlmsg
, IFLA_IFNAME
, name
))
1760 err
= netlink_transaction(&nlh
, nlmsg
, answer
);
1762 netlink_close(&nlh
);
1768 static int proc_sys_net_write(const char *path
, const char *value
)
1773 fd
= open(path
, O_WRONLY
);
1777 if (lxc_write_nointr(fd
, value
, strlen(value
)) < 0)
1784 static int lxc_is_ip_forwarding_enabled(const char *ifname
, int family
)
1787 char path
[PATH_MAX
];
1790 if (family
!= AF_INET
&& family
!= AF_INET6
)
1791 return minus_one_set_errno(EINVAL
);
1793 ret
= snprintf(path
, PATH_MAX
, "/proc/sys/net/%s/conf/%s/%s",
1794 family
== AF_INET
? "ipv4" : "ipv6", ifname
,
1796 if (ret
< 0 || (size_t)ret
>= PATH_MAX
)
1797 return minus_one_set_errno(E2BIG
);
1799 return lxc_read_file_expect(path
, buf
, 1, "1");
1802 static int neigh_proxy_set(const char *ifname
, int family
, int flag
)
1805 char path
[PATH_MAX
];
1807 if (family
!= AF_INET
&& family
!= AF_INET6
)
1810 ret
= snprintf(path
, PATH_MAX
, "/proc/sys/net/%s/conf/%s/%s",
1811 family
== AF_INET
? "ipv4" : "ipv6", ifname
,
1812 family
== AF_INET
? "proxy_arp" : "proxy_ndp");
1813 if (ret
< 0 || (size_t)ret
>= PATH_MAX
)
1816 return proc_sys_net_write(path
, flag
? "1" : "0");
1819 static int lxc_is_ip_neigh_proxy_enabled(const char *ifname
, int family
)
1822 char path
[PATH_MAX
];
1825 if (family
!= AF_INET
&& family
!= AF_INET6
)
1826 return minus_one_set_errno(EINVAL
);
1828 ret
= snprintf(path
, PATH_MAX
, "/proc/sys/net/%s/conf/%s/%s",
1829 family
== AF_INET
? "ipv4" : "ipv6", ifname
,
1830 family
== AF_INET
? "proxy_arp" : "proxy_ndp");
1831 if (ret
< 0 || (size_t)ret
>= PATH_MAX
)
1832 return minus_one_set_errno(E2BIG
);
1834 return lxc_read_file_expect(path
, buf
, 1, "1");
1837 int lxc_neigh_proxy_on(const char *name
, int family
)
1839 return neigh_proxy_set(name
, family
, 1);
1842 int lxc_neigh_proxy_off(const char *name
, int family
)
1844 return neigh_proxy_set(name
, family
, 0);
1847 int lxc_convert_mac(char *macaddr
, struct sockaddr
*sockaddr
)
1852 unsigned char *data
;
1854 sockaddr
->sa_family
= ARPHRD_ETHER
;
1855 data
= (unsigned char *)sockaddr
->sa_data
;
1857 while ((*macaddr
!= '\0') && (i
< ETH_ALEN
)) {
1861 else if (c
>= 'a' && c
<= 'f')
1863 else if (c
>= 'A' && c
<= 'F')
1872 else if (c
>= 'a' && c
<= 'f')
1873 val
|= c
- 'a' + 10;
1874 else if (c
>= 'A' && c
<= 'F')
1875 val
|= c
- 'A' + 10;
1876 else if (c
== ':' || c
== 0)
1882 *data
++ = (unsigned char)(val
& 0377);
1885 if (*macaddr
== ':')
1892 static int ip_addr_add(int family
, int ifindex
, void *addr
, void *bcast
,
1893 void *acast
, int prefix
)
1896 struct ifaddrmsg
*ifa
;
1897 struct nl_handler nlh
;
1898 struct nlmsg
*answer
= NULL
, *nlmsg
= NULL
;
1900 addrlen
= family
== AF_INET
? sizeof(struct in_addr
)
1901 : sizeof(struct in6_addr
);
1903 err
= netlink_open(&nlh
, NETLINK_ROUTE
);
1908 nlmsg
= nlmsg_alloc(NLMSG_GOOD_SIZE
);
1912 answer
= nlmsg_alloc_reserve(NLMSG_GOOD_SIZE
);
1916 nlmsg
->nlmsghdr
->nlmsg_flags
=
1917 NLM_F_ACK
| NLM_F_REQUEST
| NLM_F_CREATE
| NLM_F_EXCL
;
1918 nlmsg
->nlmsghdr
->nlmsg_type
= RTM_NEWADDR
;
1920 ifa
= nlmsg_reserve(nlmsg
, sizeof(struct ifaddrmsg
));
1923 ifa
->ifa_prefixlen
= prefix
;
1924 ifa
->ifa_index
= ifindex
;
1925 ifa
->ifa_family
= family
;
1929 if (nla_put_buffer(nlmsg
, IFA_LOCAL
, addr
, addrlen
))
1932 if (nla_put_buffer(nlmsg
, IFA_ADDRESS
, addr
, addrlen
))
1935 if (nla_put_buffer(nlmsg
, IFA_BROADCAST
, bcast
, addrlen
))
1938 /* TODO: multicast, anycast with ipv6 */
1939 err
= -EPROTONOSUPPORT
;
1940 if (family
== AF_INET6
&&
1941 (memcmp(bcast
, &in6addr_any
, sizeof(in6addr_any
)) ||
1942 memcmp(acast
, &in6addr_any
, sizeof(in6addr_any
))))
1945 err
= netlink_transaction(&nlh
, nlmsg
, answer
);
1947 netlink_close(&nlh
);
1953 int lxc_ipv6_addr_add(int ifindex
, struct in6_addr
*addr
,
1954 struct in6_addr
*mcast
, struct in6_addr
*acast
,
1957 return ip_addr_add(AF_INET6
, ifindex
, addr
, mcast
, acast
, prefix
);
1960 int lxc_ipv4_addr_add(int ifindex
, struct in_addr
*addr
, struct in_addr
*bcast
,
1963 return ip_addr_add(AF_INET
, ifindex
, addr
, bcast
, NULL
, prefix
);
1966 /* Find an IFA_LOCAL (or IFA_ADDRESS if not IFA_LOCAL is present) address from
1967 * the given RTM_NEWADDR message. Allocates memory for the address and stores
1968 * that pointer in *res (so res should be an in_addr** or in6_addr**).
1970 #pragma GCC diagnostic push
1971 #pragma GCC diagnostic ignored "-Wcast-align"
1973 static int ifa_get_local_ip(int family
, struct nlmsghdr
*msg
, void **res
)
1976 struct ifaddrmsg
*ifa
= NLMSG_DATA(msg
);
1977 struct rtattr
*rta
= IFA_RTA(ifa
);
1978 int attr_len
= NLMSG_PAYLOAD(msg
, sizeof(struct ifaddrmsg
));
1980 if (ifa
->ifa_family
!= family
)
1983 addrlen
= family
== AF_INET
? sizeof(struct in_addr
)
1984 : sizeof(struct in6_addr
);
1986 /* Loop over the rtattr's in this message */
1987 while (RTA_OK(rta
, attr_len
)) {
1988 /* Found a local address for the requested interface,
1991 if (rta
->rta_type
== IFA_LOCAL
||
1992 rta
->rta_type
== IFA_ADDRESS
) {
1993 /* Sanity check. The family check above should make sure
1994 * the address length is correct, but check here just in
1997 if (RTA_PAYLOAD(rta
) != addrlen
)
2000 /* We might have found an IFA_ADDRESS before, which we
2001 * now overwrite with an IFA_LOCAL.
2004 *res
= malloc(addrlen
);
2009 memcpy(*res
, RTA_DATA(rta
), addrlen
);
2010 if (rta
->rta_type
== IFA_LOCAL
)
2013 rta
= RTA_NEXT(rta
, attr_len
);
2018 #pragma GCC diagnostic pop
2020 static int ip_addr_get(int family
, int ifindex
, void **res
)
2022 int answer_len
, err
;
2023 struct ifaddrmsg
*ifa
;
2024 struct nl_handler nlh
;
2025 struct nlmsghdr
*msg
;
2026 int readmore
= 0, recv_len
= 0;
2027 struct nlmsg
*answer
= NULL
, *nlmsg
= NULL
;
2029 err
= netlink_open(&nlh
, NETLINK_ROUTE
);
2034 nlmsg
= nlmsg_alloc(NLMSG_GOOD_SIZE
);
2038 answer
= nlmsg_alloc_reserve(NLMSG_GOOD_SIZE
);
2042 /* Save the answer buffer length, since it will be overwritten on the
2043 * first receive (and we might need to receive more than once).
2045 answer_len
= answer
->nlmsghdr
->nlmsg_len
;
2047 nlmsg
->nlmsghdr
->nlmsg_flags
= NLM_F_REQUEST
| NLM_F_ROOT
;
2048 nlmsg
->nlmsghdr
->nlmsg_type
= RTM_GETADDR
;
2050 ifa
= nlmsg_reserve(nlmsg
, sizeof(struct ifaddrmsg
));
2053 ifa
->ifa_family
= family
;
2055 /* Send the request for addresses, which returns all addresses on all
2058 err
= netlink_send(&nlh
, nlmsg
);
2062 #pragma GCC diagnostic push
2063 #pragma GCC diagnostic ignored "-Wcast-align"
2066 /* Restore the answer buffer length, it might have been
2067 * overwritten by a previous receive.
2069 answer
->nlmsghdr
->nlmsg_len
= answer_len
;
2071 /* Get the (next) batch of reply messages. */
2072 err
= netlink_rcv(&nlh
, answer
);
2079 /* Satisfy the typing for the netlink macros. */
2080 msg
= answer
->nlmsghdr
;
2082 while (NLMSG_OK(msg
, recv_len
)) {
2083 /* Stop reading if we see an error message. */
2084 if (msg
->nlmsg_type
== NLMSG_ERROR
) {
2085 struct nlmsgerr
*errmsg
=
2086 (struct nlmsgerr
*)NLMSG_DATA(msg
);
2087 err
= errmsg
->error
;
2091 /* Stop reading if we see a NLMSG_DONE message. */
2092 if (msg
->nlmsg_type
== NLMSG_DONE
) {
2097 if (msg
->nlmsg_type
!= RTM_NEWADDR
) {
2102 ifa
= (struct ifaddrmsg
*)NLMSG_DATA(msg
);
2103 if (ifa
->ifa_index
== ifindex
) {
2104 if (ifa_get_local_ip(family
, msg
, res
) < 0) {
2109 /* Found a result, stop searching. */
2114 /* Keep reading more data from the socket if the last
2115 * message had the NLF_F_MULTI flag set.
2117 readmore
= (msg
->nlmsg_flags
& NLM_F_MULTI
);
2119 /* Look at the next message received in this buffer. */
2120 msg
= NLMSG_NEXT(msg
, recv_len
);
2124 #pragma GCC diagnostic pop
2126 /* If we end up here, we didn't find any result, so signal an
2132 netlink_close(&nlh
);
2138 int lxc_ipv6_addr_get(int ifindex
, struct in6_addr
**res
)
2140 return ip_addr_get(AF_INET6
, ifindex
, (void **)res
);
2143 int lxc_ipv4_addr_get(int ifindex
, struct in_addr
**res
)
2145 return ip_addr_get(AF_INET
, ifindex
, (void **)res
);
2148 static int ip_gateway_add(int family
, int ifindex
, void *gw
)
2151 struct nl_handler nlh
;
2153 struct nlmsg
*answer
= NULL
, *nlmsg
= NULL
;
2155 addrlen
= family
== AF_INET
? sizeof(struct in_addr
)
2156 : sizeof(struct in6_addr
);
2158 err
= netlink_open(&nlh
, NETLINK_ROUTE
);
2163 nlmsg
= nlmsg_alloc(NLMSG_GOOD_SIZE
);
2167 answer
= nlmsg_alloc_reserve(NLMSG_GOOD_SIZE
);
2171 nlmsg
->nlmsghdr
->nlmsg_flags
=
2172 NLM_F_ACK
| NLM_F_REQUEST
| NLM_F_CREATE
| NLM_F_EXCL
;
2173 nlmsg
->nlmsghdr
->nlmsg_type
= RTM_NEWROUTE
;
2175 rt
= nlmsg_reserve(nlmsg
, sizeof(struct rtmsg
));
2178 rt
->rtm_family
= family
;
2179 rt
->rtm_table
= RT_TABLE_MAIN
;
2180 rt
->rtm_scope
= RT_SCOPE_UNIVERSE
;
2181 rt
->rtm_protocol
= RTPROT_BOOT
;
2182 rt
->rtm_type
= RTN_UNICAST
;
2183 /* "default" destination */
2184 rt
->rtm_dst_len
= 0;
2188 /* If gateway address not supplied, then a device route will be created instead */
2190 if (nla_put_buffer(nlmsg
, RTA_GATEWAY
, gw
, addrlen
))
2194 /* Adding the interface index enables the use of link-local
2195 * addresses for the gateway.
2197 if (nla_put_u32(nlmsg
, RTA_OIF
, ifindex
))
2200 err
= netlink_transaction(&nlh
, nlmsg
, answer
);
2202 netlink_close(&nlh
);
2208 int lxc_ipv4_gateway_add(int ifindex
, struct in_addr
*gw
)
2210 return ip_gateway_add(AF_INET
, ifindex
, gw
);
2213 int lxc_ipv6_gateway_add(int ifindex
, struct in6_addr
*gw
)
2215 return ip_gateway_add(AF_INET6
, ifindex
, gw
);
2217 bool is_ovs_bridge(const char *bridge
)
2221 char brdirname
[22 + IFNAMSIZ
+ 1] = {0};
2223 ret
= snprintf(brdirname
, 22 + IFNAMSIZ
+ 1, "/sys/class/net/%s/bridge",
2225 if (ret
< 0 || (size_t)ret
>= 22 + IFNAMSIZ
+ 1)
2228 ret
= stat(brdirname
, &sb
);
2229 if (ret
< 0 && errno
== ENOENT
)
2235 struct ovs_veth_args
{
2240 /* Called from a background thread - when nic goes away, remove it from the
2243 static int lxc_ovs_delete_port_exec(void *data
)
2245 struct ovs_veth_args
*args
= data
;
2247 execlp("ovs-vsctl", "ovs-vsctl", "del-port", args
->bridge
, args
->nic
,
2252 int lxc_ovs_delete_port(const char *bridge
, const char *nic
)
2255 char cmd_output
[PATH_MAX
];
2256 struct ovs_veth_args args
;
2258 args
.bridge
= bridge
;
2260 ret
= run_command(cmd_output
, sizeof(cmd_output
),
2261 lxc_ovs_delete_port_exec
, (void *)&args
);
2263 ERROR("Failed to delete \"%s\" from openvswitch bridge \"%s\": "
2264 "%s", bridge
, nic
, cmd_output
);
2271 static int lxc_ovs_attach_bridge_exec(void *data
)
2273 struct ovs_veth_args
*args
= data
;
2275 execlp("ovs-vsctl", "ovs-vsctl", "add-port", args
->bridge
, args
->nic
,
2280 static int lxc_ovs_attach_bridge(const char *bridge
, const char *nic
)
2283 char cmd_output
[PATH_MAX
];
2284 struct ovs_veth_args args
;
2286 args
.bridge
= bridge
;
2288 ret
= run_command(cmd_output
, sizeof(cmd_output
),
2289 lxc_ovs_attach_bridge_exec
, (void *)&args
);
2291 ERROR("Failed to attach \"%s\" to openvswitch bridge \"%s\": %s",
2292 bridge
, nic
, cmd_output
);
2299 int lxc_bridge_attach(const char *bridge
, const char *ifname
)
2305 if (strlen(ifname
) >= IFNAMSIZ
)
2308 index
= if_nametoindex(ifname
);
2312 if (is_ovs_bridge(bridge
))
2313 return lxc_ovs_attach_bridge(bridge
, ifname
);
2315 fd
= socket(AF_INET
, SOCK_STREAM
| SOCK_CLOEXEC
, 0);
2319 retlen
= strlcpy(ifr
.ifr_name
, bridge
, IFNAMSIZ
);
2320 if (retlen
>= IFNAMSIZ
) {
2325 ifr
.ifr_name
[IFNAMSIZ
- 1] = '\0';
2326 ifr
.ifr_ifindex
= index
;
2327 err
= ioctl(fd
, SIOCBRADDIF
, &ifr
);
2335 static const char *const lxc_network_types
[LXC_NET_MAXCONFTYPE
+ 1] = {
2336 [LXC_NET_EMPTY
] = "empty",
2337 [LXC_NET_VETH
] = "veth",
2338 [LXC_NET_MACVLAN
] = "macvlan",
2339 [LXC_NET_IPVLAN
] = "ipvlan",
2340 [LXC_NET_PHYS
] = "phys",
2341 [LXC_NET_VLAN
] = "vlan",
2342 [LXC_NET_NONE
] = "none",
2345 const char *lxc_net_type_to_str(int type
)
2347 if (type
< 0 || type
> LXC_NET_MAXCONFTYPE
)
2350 return lxc_network_types
[type
];
2353 static const char padchar
[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
2355 char *lxc_mkifname(char *template)
2358 struct netns_ifaddrs
*ifa
, *ifaddr
;
2359 char name
[IFNAMSIZ
];
2360 bool exists
= false;
2365 seed
= randseed(false);
2368 (void)randseed(true);
2371 if (strlen(template) >= IFNAMSIZ
)
2374 /* Get all the network interfaces. */
2375 ret
= netns_getifaddrs(&ifaddr
, -1, &(bool){false});
2377 SYSERROR("Failed to get network interfaces");
2381 /* Generate random names until we find one that doesn't exist. */
2384 (void)strlcpy(name
, template, IFNAMSIZ
);
2388 for (i
= 0; i
< strlen(name
); i
++) {
2389 if (name
[i
] == 'X') {
2391 name
[i
] = padchar
[rand_r(&seed
) % strlen(padchar
)];
2393 name
[i
] = padchar
[rand() % strlen(padchar
)];
2398 for (ifa
= ifaddr
; ifa
!= NULL
; ifa
= ifa
->ifa_next
) {
2399 if (!strcmp(ifa
->ifa_name
, name
)) {
2409 netns_freeifaddrs(ifaddr
);
2410 (void)strlcpy(template, name
, strlen(template) + 1);
2415 int setup_private_host_hw_addr(char *veth1
)
2420 sockfd
= socket(AF_INET
, SOCK_DGRAM
| SOCK_CLOEXEC
, 0);
2424 err
= snprintf((char *)ifr
.ifr_name
, IFNAMSIZ
, "%s", veth1
);
2425 if (err
< 0 || (size_t)err
>= IFNAMSIZ
) {
2430 err
= ioctl(sockfd
, SIOCGIFHWADDR
, &ifr
);
2436 ifr
.ifr_hwaddr
.sa_data
[0] = 0xfe;
2437 err
= ioctl(sockfd
, SIOCSIFHWADDR
, &ifr
);
2445 int lxc_find_gateway_addresses(struct lxc_handler
*handler
)
2447 struct lxc_list
*network
= &handler
->conf
->network
;
2448 struct lxc_list
*iterator
;
2449 struct lxc_netdev
*netdev
;
2452 lxc_list_for_each(iterator
, network
) {
2453 netdev
= iterator
->elem
;
2455 if (!netdev
->ipv4_gateway_auto
&& !netdev
->ipv6_gateway_auto
)
2458 if (netdev
->type
!= LXC_NET_VETH
&& netdev
->type
!= LXC_NET_MACVLAN
) {
2459 ERROR("Automatic gateway detection is only supported "
2460 "for veth and macvlan");
2464 if (netdev
->link
[0] == '\0') {
2465 ERROR("Automatic gateway detection needs a link interface");
2469 link_index
= if_nametoindex(netdev
->link
);
2473 if (netdev
->ipv4_gateway_auto
) {
2474 if (lxc_ipv4_addr_get(link_index
, &netdev
->ipv4_gateway
)) {
2475 ERROR("Failed to automatically find ipv4 gateway "
2476 "address from link interface \"%s\"", netdev
->link
);
2481 if (netdev
->ipv6_gateway_auto
) {
2482 if (lxc_ipv6_addr_get(link_index
, &netdev
->ipv6_gateway
)) {
2483 ERROR("Failed to automatically find ipv6 gateway "
2484 "address from link interface \"%s\"", netdev
->link
);
2493 #define LXC_USERNIC_PATH LIBEXECDIR "/lxc/lxc-user-nic"
2494 static int lxc_create_network_unpriv_exec(const char *lxcpath
, const char *lxcname
,
2495 struct lxc_netdev
*netdev
, pid_t pid
, unsigned int hooks_version
)
2499 int bytes
, pipefd
[2];
2500 char *token
, *saveptr
= NULL
;
2501 char netdev_link
[IFNAMSIZ
];
2502 char buffer
[PATH_MAX
] = {0};
2505 if (netdev
->type
!= LXC_NET_VETH
) {
2506 ERROR("Network type %d not support for unprivileged use", netdev
->type
);
2512 SYSERROR("Failed to create pipe");
2518 SYSERROR("Failed to create new process");
2525 char pidstr
[INTTYPE_TO_STRLEN(pid_t
)];
2529 ret
= dup2(pipefd
[1], STDOUT_FILENO
);
2531 ret
= dup2(pipefd
[1], STDERR_FILENO
);
2534 SYSERROR("Failed to duplicate std{err,out} file descriptor");
2535 _exit(EXIT_FAILURE
);
2538 if (netdev
->link
[0] != '\0')
2539 retlen
= strlcpy(netdev_link
, netdev
->link
, IFNAMSIZ
);
2541 retlen
= strlcpy(netdev_link
, "none", IFNAMSIZ
);
2542 if (retlen
>= IFNAMSIZ
) {
2543 SYSERROR("Invalid network device name");
2544 _exit(EXIT_FAILURE
);
2547 ret
= snprintf(pidstr
, sizeof(pidstr
), "%d", pid
);
2548 if (ret
< 0 || ret
>= sizeof(pidstr
))
2549 _exit(EXIT_FAILURE
);
2550 pidstr
[sizeof(pidstr
) - 1] = '\0';
2552 INFO("Execing lxc-user-nic create %s %s %s veth %s %s", lxcpath
,
2553 lxcname
, pidstr
, netdev_link
,
2554 netdev
->name
[0] != '\0' ? netdev
->name
: "(null)");
2555 if (netdev
->name
[0] != '\0')
2556 execlp(LXC_USERNIC_PATH
, LXC_USERNIC_PATH
, "create",
2557 lxcpath
, lxcname
, pidstr
, "veth", netdev_link
,
2558 netdev
->name
, (char *)NULL
);
2560 execlp(LXC_USERNIC_PATH
, LXC_USERNIC_PATH
, "create",
2561 lxcpath
, lxcname
, pidstr
, "veth", netdev_link
,
2563 SYSERROR("Failed to execute lxc-user-nic");
2564 _exit(EXIT_FAILURE
);
2567 /* close the write-end of the pipe */
2570 bytes
= lxc_read_nointr(pipefd
[0], &buffer
, PATH_MAX
);
2572 SYSERROR("Failed to read from pipe file descriptor");
2575 buffer
[bytes
- 1] = '\0';
2578 ret
= wait_for_pid(child
);
2580 if (ret
!= 0 || bytes
< 0) {
2581 ERROR("lxc-user-nic failed to configure requested network: %s",
2582 buffer
[0] != '\0' ? buffer
: "(null)");
2585 TRACE("Received output \"%s\" from lxc-user-nic", buffer
);
2588 token
= strtok_r(buffer
, ":", &saveptr
);
2590 ERROR("Failed to parse lxc-user-nic output");
2595 * lxc-user-nic will take care of proper network device naming. So
2596 * netdev->name and netdev->created_name need to be identical to not
2597 * trigger another rename later on.
2599 retlen
= strlcpy(netdev
->name
, token
, IFNAMSIZ
);
2600 if (retlen
< IFNAMSIZ
)
2601 retlen
= strlcpy(netdev
->created_name
, token
, IFNAMSIZ
);
2602 if (retlen
>= IFNAMSIZ
) {
2603 ERROR("Container side veth device name returned by lxc-user-nic is too long");
2607 /* netdev->ifindex */
2608 token
= strtok_r(NULL
, ":", &saveptr
);
2610 ERROR("Failed to parse lxc-user-nic output");
2614 ret
= lxc_safe_int(token
, &netdev
->ifindex
);
2617 SYSERROR("Failed to convert string \"%s\" to integer", token
);
2621 /* netdev->priv.veth_attr.veth1 */
2622 token
= strtok_r(NULL
, ":", &saveptr
);
2624 ERROR("Failed to parse lxc-user-nic output");
2628 retlen
= strlcpy(netdev
->priv
.veth_attr
.veth1
, token
, IFNAMSIZ
);
2629 if (retlen
>= IFNAMSIZ
) {
2630 ERROR("Host side veth device name returned by lxc-user-nic is "
2635 /* netdev->priv.veth_attr.ifindex */
2636 token
= strtok_r(NULL
, ":", &saveptr
);
2638 ERROR("Failed to parse lxc-user-nic output");
2642 ret
= lxc_safe_int(token
, &netdev
->priv
.veth_attr
.ifindex
);
2645 SYSERROR("Failed to convert string \"%s\" to integer", token
);
2649 if (netdev
->upscript
) {
2653 netdev
->priv
.veth_attr
.veth1
,
2657 ret
= run_script_argv(lxcname
, hooks_version
, "net",
2658 netdev
->upscript
, "up", argv
);
2666 static int lxc_delete_network_unpriv_exec(const char *lxcpath
, const char *lxcname
,
2667 struct lxc_netdev
*netdev
,
2668 const char *netns_path
)
2673 char buffer
[PATH_MAX
] = {0};
2675 if (netdev
->type
!= LXC_NET_VETH
) {
2676 ERROR("Network type %d not support for unprivileged use", netdev
->type
);
2682 SYSERROR("Failed to create pipe");
2688 SYSERROR("Failed to create new process");
2699 ret
= dup2(pipefd
[1], STDOUT_FILENO
);
2701 ret
= dup2(pipefd
[1], STDERR_FILENO
);
2704 SYSERROR("Failed to duplicate std{err,out} file descriptor");
2705 _exit(EXIT_FAILURE
);
2708 if (netdev
->priv
.veth_attr
.pair
[0] != '\0')
2709 hostveth
= netdev
->priv
.veth_attr
.pair
;
2711 hostveth
= netdev
->priv
.veth_attr
.veth1
;
2712 if (hostveth
[0] == '\0') {
2713 SYSERROR("Host side veth device name is missing");
2714 _exit(EXIT_FAILURE
);
2717 if (netdev
->link
[0] == '\0') {
2718 SYSERROR("Network link for network device \"%s\" is "
2719 "missing", netdev
->priv
.veth_attr
.veth1
);
2720 _exit(EXIT_FAILURE
);
2723 INFO("Execing lxc-user-nic delete %s %s %s veth %s %s", lxcpath
,
2724 lxcname
, netns_path
, netdev
->link
, hostveth
);
2725 execlp(LXC_USERNIC_PATH
, LXC_USERNIC_PATH
, "delete", lxcpath
,
2726 lxcname
, netns_path
, "veth", netdev
->link
, hostveth
,
2728 SYSERROR("Failed to exec lxc-user-nic.");
2729 _exit(EXIT_FAILURE
);
2734 bytes
= lxc_read_nointr(pipefd
[0], &buffer
, PATH_MAX
);
2736 SYSERROR("Failed to read from pipe file descriptor.");
2739 buffer
[bytes
- 1] = '\0';
2742 ret
= wait_for_pid(child
);
2744 if (ret
!= 0 || bytes
< 0) {
2745 ERROR("lxc-user-nic failed to delete requested network: %s",
2746 buffer
[0] != '\0' ? buffer
: "(null)");
2753 bool lxc_delete_network_unpriv(struct lxc_handler
*handler
)
2756 struct lxc_list
*iterator
;
2757 struct lxc_list
*network
= &handler
->conf
->network
;
2758 /* strlen("/proc/") = 6
2760 * INTTYPE_TO_STRLEN(pid_t)
2762 * strlen("/fd/") = 4
2764 * INTTYPE_TO_STRLEN(int)
2768 char netns_path
[6 + INTTYPE_TO_STRLEN(pid_t
) + 4 + INTTYPE_TO_STRLEN(int) + 1];
2772 if (handler
->nsfd
[LXC_NS_NET
] < 0) {
2773 DEBUG("Cannot not guarantee safe deletion of network devices. "
2774 "Manual cleanup maybe needed");
2778 ret
= snprintf(netns_path
, sizeof(netns_path
), "/proc/%d/fd/%d",
2779 lxc_raw_getpid(), handler
->nsfd
[LXC_NS_NET
]);
2780 if (ret
< 0 || ret
>= sizeof(netns_path
))
2783 lxc_list_for_each(iterator
, network
) {
2784 char *hostveth
= NULL
;
2785 struct lxc_netdev
*netdev
= iterator
->elem
;
2787 /* We can only delete devices whose ifindex we have. If we don't
2788 * have the index it means that we didn't create it.
2790 if (!netdev
->ifindex
)
2793 if (netdev
->type
== LXC_NET_PHYS
) {
2794 ret
= lxc_netdev_rename_by_index(netdev
->ifindex
,
2797 WARN("Failed to rename interface with index %d "
2798 "to its initial name \"%s\"",
2799 netdev
->ifindex
, netdev
->link
);
2801 TRACE("Renamed interface with index %d to its "
2802 "initial name \"%s\"",
2803 netdev
->ifindex
, netdev
->link
);
2805 ret
= netdev_deconf
[netdev
->type
](handler
, netdev
);
2806 goto clear_ifindices
;
2809 ret
= netdev_deconf
[netdev
->type
](handler
, netdev
);
2811 WARN("Failed to deconfigure network device");
2813 if (netdev
->type
!= LXC_NET_VETH
)
2814 goto clear_ifindices
;
2816 if (netdev
->link
[0] == '\0' || !is_ovs_bridge(netdev
->link
))
2817 goto clear_ifindices
;
2819 if (netdev
->priv
.veth_attr
.pair
[0] != '\0')
2820 hostveth
= netdev
->priv
.veth_attr
.pair
;
2822 hostveth
= netdev
->priv
.veth_attr
.veth1
;
2823 if (hostveth
[0] == '\0')
2824 goto clear_ifindices
;
2826 ret
= lxc_delete_network_unpriv_exec(handler
->lxcpath
,
2827 handler
->name
, netdev
,
2830 WARN("Failed to remove port \"%s\" from openvswitch "
2831 "bridge \"%s\"", hostveth
, netdev
->link
);
2832 goto clear_ifindices
;
2834 INFO("Removed interface \"%s\" from \"%s\"", hostveth
,
2838 /* We need to clear any ifindices we recorded so liblxc won't
2839 * have cached stale data which would cause it to fail on reboot
2840 * we're we don't re-read the on-disk config file.
2842 netdev
->ifindex
= 0;
2843 if (netdev
->type
== LXC_NET_PHYS
) {
2844 netdev
->priv
.phys_attr
.ifindex
= 0;
2845 } else if (netdev
->type
== LXC_NET_VETH
) {
2846 netdev
->priv
.veth_attr
.veth1
[0] = '\0';
2847 netdev
->priv
.veth_attr
.ifindex
= 0;
2854 struct ip_proxy_args
{
2859 static int lxc_add_ip_neigh_proxy_exec_wrapper(void *data
)
2861 struct ip_proxy_args
*args
= data
;
2863 execlp("ip", "ip", "neigh", "add", "proxy", args
->ip
, "dev", args
->dev
, (char *)NULL
);
2867 static int lxc_del_ip_neigh_proxy_exec_wrapper(void *data
)
2869 struct ip_proxy_args
*args
= data
;
2871 execlp("ip", "ip", "neigh", "flush", "proxy", args
->ip
, "dev", args
->dev
, (char *)NULL
);
2875 static int lxc_add_ip_neigh_proxy(const char *ip
, const char *dev
)
2878 char cmd_output
[PATH_MAX
];
2879 struct ip_proxy_args args
= {
2884 ret
= run_command(cmd_output
, sizeof(cmd_output
), lxc_add_ip_neigh_proxy_exec_wrapper
, &args
);
2886 ERROR("Failed to add ip proxy \"%s\" to dev \"%s\": %s", ip
, dev
, cmd_output
);
2893 static int lxc_del_ip_neigh_proxy(const char *ip
, const char *dev
)
2896 char cmd_output
[PATH_MAX
];
2897 struct ip_proxy_args args
= {
2902 ret
= run_command(cmd_output
, sizeof(cmd_output
), lxc_del_ip_neigh_proxy_exec_wrapper
, &args
);
2904 ERROR("Failed to delete ip proxy \"%s\" to dev \"%s\": %s", ip
, dev
, cmd_output
);
2911 static int lxc_setup_l2proxy(struct lxc_netdev
*netdev
) {
2912 struct lxc_list
*cur
, *next
;
2913 struct lxc_inetdev
*inet4dev
;
2914 struct lxc_inet6dev
*inet6dev
;
2915 char bufinet4
[INET_ADDRSTRLEN
], bufinet6
[INET6_ADDRSTRLEN
];
2917 unsigned int lo_ifindex
= 0;
2919 /* If IPv4 addresses are specified, then check that sysctl is configured correctly. */
2920 if (!lxc_list_empty(&netdev
->ipv4
)) {
2921 /* Check for net.ipv4.conf.[link].forwarding=1 */
2922 if (lxc_is_ip_forwarding_enabled(netdev
->link
, AF_INET
) < 0) {
2923 ERROR("Requires sysctl net.ipv4.conf.%s.forwarding=1", netdev
->link
);
2924 return minus_one_set_errno(EINVAL
);
2928 /* If IPv6 addresses are specified, then check that sysctl is configured correctly. */
2929 if (!lxc_list_empty(&netdev
->ipv6
)) {
2930 /* Check for net.ipv6.conf.[link].proxy_ndp=1 */
2931 if (lxc_is_ip_neigh_proxy_enabled(netdev
->link
, AF_INET6
) < 0) {
2932 ERROR("Requires sysctl net.ipv6.conf.%s.proxy_ndp=1", netdev
->link
);
2933 return minus_one_set_errno(EINVAL
);
2936 /* Check for net.ipv6.conf.[link].forwarding=1 */
2937 if (lxc_is_ip_forwarding_enabled(netdev
->link
, AF_INET6
) < 0) {
2938 ERROR("Requires sysctl net.ipv6.conf.%s.forwarding=1", netdev
->link
);
2939 return minus_one_set_errno(EINVAL
);
2943 /* Perform IPVLAN specific checks. */
2944 if (netdev
->type
== LXC_NET_IPVLAN
) {
2945 /* Check mode is l3s as other modes do not work with l2proxy. */
2946 if (netdev
->priv
.ipvlan_attr
.mode
!= IPVLAN_MODE_L3S
) {
2947 ERROR("Requires ipvlan mode on dev \"%s\" be l3s when used with l2proxy", netdev
->link
);
2948 return minus_one_set_errno(EINVAL
);
2951 /* Retrieve local-loopback interface index for use with IPVLAN static routes. */
2952 lo_ifindex
= if_nametoindex(loop_device
);
2953 if (lo_ifindex
== 0) {
2954 ERROR("Failed to retrieve ifindex for \"%s\" routing cleanup", loop_device
);
2955 return minus_one_set_errno(EINVAL
);
2959 lxc_list_for_each_safe(cur
, &netdev
->ipv4
, next
) {
2960 inet4dev
= cur
->elem
;
2961 if (!inet_ntop(AF_INET
, &inet4dev
->addr
, bufinet4
, sizeof(bufinet4
)))
2962 return minus_one_set_errno(-errno
);
2964 if (lxc_add_ip_neigh_proxy(bufinet4
, netdev
->link
) < 0)
2965 return minus_one_set_errno(EINVAL
);
2967 /* IPVLAN requires a route to local-loopback to trigger l2proxy. */
2968 if (netdev
->type
== LXC_NET_IPVLAN
) {
2969 err
= lxc_ipv4_dest_add(lo_ifindex
, &inet4dev
->addr
, 32);
2971 ERROR("Failed to add ipv4 dest \"%s\" for network device \"%s\"", bufinet4
, loop_device
);
2972 return minus_one_set_errno(-err
);
2977 lxc_list_for_each_safe(cur
, &netdev
->ipv6
, next
) {
2978 inet6dev
= cur
->elem
;
2979 if (!inet_ntop(AF_INET6
, &inet6dev
->addr
, bufinet6
, sizeof(bufinet6
)))
2980 return minus_one_set_errno(-errno
);
2982 if (lxc_add_ip_neigh_proxy(bufinet6
, netdev
->link
) < 0)
2983 return minus_one_set_errno(EINVAL
);
2985 /* IPVLAN requires a route to local-loopback to trigger l2proxy. */
2986 if (netdev
->type
== LXC_NET_IPVLAN
) {
2987 err
= lxc_ipv6_dest_add(lo_ifindex
, &inet6dev
->addr
, 128);
2989 ERROR("Failed to add ipv6 dest \"%s\" for network device \"%s\"", bufinet6
, loop_device
);
2990 return minus_one_set_errno(-err
);
2998 static int lxc_delete_ipv4_l2proxy(struct in_addr
*ip
, char *link
, unsigned int lo_ifindex
) {
2999 char bufinet4
[INET_ADDRSTRLEN
];
3000 unsigned int errCount
= 0;
3002 if (!inet_ntop(AF_INET
, ip
, bufinet4
, sizeof(bufinet4
))) {
3003 SYSERROR("Failed to convert IP for l2proxy ipv4 removal on dev \"%s\"", link
);
3004 return minus_one_set_errno(EINVAL
);
3007 /* If a local-loopback ifindex supplied remove the static route to the lo device. */
3008 if (lo_ifindex
> 0) {
3009 if (lxc_ipv4_dest_del(lo_ifindex
, ip
, 32) < 0) {
3011 ERROR("Failed to delete ipv4 dest \"%s\" for network ifindex \"%u\"", bufinet4
, lo_ifindex
);
3015 /* If link is supplied remove the IP neigh proxy entry for this IP on the device. */
3016 if (link
[0] != '\0') {
3017 if (lxc_del_ip_neigh_proxy(bufinet4
, link
) < 0)
3022 return minus_one_set_errno(EINVAL
);
3027 static int lxc_delete_ipv6_l2proxy(struct in6_addr
*ip
, char *link
, unsigned int lo_ifindex
) {
3028 char bufinet6
[INET6_ADDRSTRLEN
];
3029 unsigned int errCount
= 0;
3031 if (!inet_ntop(AF_INET6
, ip
, bufinet6
, sizeof(bufinet6
))) {
3032 SYSERROR("Failed to convert IP for l2proxy ipv6 removal on dev \"%s\"", link
);
3033 return minus_one_set_errno(EINVAL
);
3036 /* If a local-loopback ifindex supplied remove the static route to the lo device. */
3037 if (lo_ifindex
> 0) {
3038 if (lxc_ipv6_dest_del(lo_ifindex
, ip
, 128) < 0) {
3040 ERROR("Failed to delete ipv6 dest \"%s\" for network ifindex \"%u\"", bufinet6
, lo_ifindex
);
3044 /* If link is supplied remove the IP neigh proxy entry for this IP on the device. */
3045 if (link
[0] != '\0') {
3046 if (lxc_del_ip_neigh_proxy(bufinet6
, link
) < 0)
3051 return minus_one_set_errno(EINVAL
);
3056 static int lxc_delete_l2proxy(struct lxc_netdev
*netdev
) {
3057 unsigned int lo_ifindex
= 0;
3058 unsigned int errCount
= 0;
3059 struct lxc_list
*cur
, *next
;
3060 struct lxc_inetdev
*inet4dev
;
3061 struct lxc_inet6dev
*inet6dev
;
3063 /* Perform IPVLAN specific checks. */
3064 if (netdev
->type
== LXC_NET_IPVLAN
) {
3065 /* Retrieve local-loopback interface index for use with IPVLAN static routes. */
3066 lo_ifindex
= if_nametoindex(loop_device
);
3067 if (lo_ifindex
== 0) {
3069 ERROR("Failed to retrieve ifindex for \"%s\" routing cleanup", loop_device
);
3073 lxc_list_for_each_safe(cur
, &netdev
->ipv4
, next
) {
3074 inet4dev
= cur
->elem
;
3075 if (lxc_delete_ipv4_l2proxy(&inet4dev
->addr
, netdev
->link
, lo_ifindex
) < 0)
3079 lxc_list_for_each_safe(cur
, &netdev
->ipv6
, next
) {
3080 inet6dev
= cur
->elem
;
3081 if (lxc_delete_ipv6_l2proxy(&inet6dev
->addr
, netdev
->link
, lo_ifindex
) < 0)
3086 return minus_one_set_errno(EINVAL
);
3091 static int lxc_create_network_priv(struct lxc_handler
*handler
)
3093 struct lxc_list
*iterator
;
3094 struct lxc_list
*network
= &handler
->conf
->network
;
3096 lxc_list_for_each(iterator
, network
) {
3097 struct lxc_netdev
*netdev
= iterator
->elem
;
3099 if (netdev
->type
< 0 || netdev
->type
> LXC_NET_MAXCONFTYPE
) {
3100 ERROR("Invalid network configuration type %d", netdev
->type
);
3104 /* Setup l2proxy entries if enabled and used with a link property */
3105 if (netdev
->l2proxy
&& netdev
->link
[0] != '\0') {
3106 if (lxc_setup_l2proxy(netdev
)) {
3107 ERROR("Failed to setup l2proxy");
3112 if (netdev_conf
[netdev
->type
](handler
, netdev
)) {
3113 ERROR("Failed to create network device");
3121 int lxc_network_move_created_netdev_priv(struct lxc_handler
*handler
)
3123 pid_t pid
= handler
->pid
;
3124 struct lxc_list
*network
= &handler
->conf
->network
;
3125 struct lxc_list
*iterator
;
3127 if (am_guest_unpriv())
3130 lxc_list_for_each(iterator
, network
) {
3132 char ifname
[IFNAMSIZ
];
3133 struct lxc_netdev
*netdev
= iterator
->elem
;
3135 if (!netdev
->ifindex
)
3138 /* retrieve the name of the interface */
3139 if (!if_indextoname(netdev
->ifindex
, ifname
)) {
3140 ERROR("No interface corresponding to ifindex \"%d\"",
3145 ret
= lxc_netdev_move_by_name(ifname
, pid
, NULL
);
3148 SYSERROR("Failed to move network device \"%s\" to network namespace %d",
3153 strlcpy(netdev
->created_name
, ifname
, IFNAMSIZ
);
3155 DEBUG("Moved network device \"%s\" to network namespace of %d",
3156 netdev
->created_name
, pid
);
3162 static int lxc_create_network_unpriv(struct lxc_handler
*handler
)
3164 int hooks_version
= handler
->conf
->hooks_version
;
3165 const char *lxcname
= handler
->name
;
3166 const char *lxcpath
= handler
->lxcpath
;
3167 struct lxc_list
*network
= &handler
->conf
->network
;
3168 pid_t pid
= handler
->pid
;
3169 struct lxc_list
*iterator
;
3171 lxc_list_for_each(iterator
, network
) {
3172 struct lxc_netdev
*netdev
= iterator
->elem
;
3174 if (netdev
->type
== LXC_NET_EMPTY
)
3177 if (netdev
->type
== LXC_NET_NONE
)
3180 if (netdev
->type
!= LXC_NET_VETH
) {
3181 ERROR("Networks of type %s are not supported by unprivileged containers",
3182 lxc_net_type_to_str(netdev
->type
));
3187 INFO("mtu ignored due to insufficient privilege");
3189 if (lxc_create_network_unpriv_exec(lxcpath
, lxcname
, netdev
,
3190 pid
, hooks_version
))
3197 bool lxc_delete_network_priv(struct lxc_handler
*handler
)
3200 struct lxc_list
*iterator
;
3201 struct lxc_list
*network
= &handler
->conf
->network
;
3203 lxc_list_for_each(iterator
, network
) {
3204 char *hostveth
= NULL
;
3205 struct lxc_netdev
*netdev
= iterator
->elem
;
3207 /* We can only delete devices whose ifindex we have. If we don't
3208 * have the index it means that we didn't create it.
3210 if (!netdev
->ifindex
)
3213 /* Delete l2proxy entries if enabled and used with a link property */
3214 if (netdev
->l2proxy
&& netdev
->link
[0] != '\0') {
3215 if (lxc_delete_l2proxy(netdev
))
3216 WARN("Failed to delete all l2proxy config");
3217 /* Don't return, let the network be cleaned up as normal. */
3220 if (netdev
->type
== LXC_NET_PHYS
) {
3221 ret
= lxc_netdev_rename_by_index(netdev
->ifindex
, netdev
->link
);
3223 WARN("Failed to rename interface with index %d "
3224 "from \"%s\" to its initial name \"%s\"",
3225 netdev
->ifindex
, netdev
->name
, netdev
->link
);
3227 TRACE("Renamed interface with index %d from "
3228 "\"%s\" to its initial name \"%s\"",
3229 netdev
->ifindex
, netdev
->name
,
3232 /* Restore original MTU */
3233 ret
= lxc_netdev_set_mtu(netdev
->link
, netdev
->priv
.phys_attr
.mtu
);
3235 WARN("Failed to set interface \"%s\" to its initial mtu \"%d\"",
3236 netdev
->link
, netdev
->priv
.phys_attr
.mtu
);
3238 TRACE("Restored interface \"%s\" to its initial mtu \"%d\"",
3239 netdev
->link
, netdev
->priv
.phys_attr
.mtu
);
3243 ret
= netdev_deconf
[netdev
->type
](handler
, netdev
);
3244 goto clear_ifindices
;
3247 ret
= netdev_deconf
[netdev
->type
](handler
, netdev
);
3249 WARN("Failed to deconfigure network device");
3251 /* Recent kernels remove the virtual interfaces when the network
3252 * namespace is destroyed but in case we did not move the
3253 * interface to the network namespace, we have to destroy it.
3255 ret
= lxc_netdev_delete_by_index(netdev
->ifindex
);
3257 if (errno
!= ENODEV
) {
3258 WARN("Failed to remove interface \"%s\" with index %d",
3259 netdev
->name
[0] != '\0' ? netdev
->name
: "(null)",
3261 goto clear_ifindices
;
3263 INFO("Interface \"%s\" with index %d already deleted or existing in different network namespace",
3264 netdev
->name
[0] != '\0' ? netdev
->name
: "(null)",
3267 INFO("Removed interface \"%s\" with index %d",
3268 netdev
->name
[0] != '\0' ? netdev
->name
: "(null)",
3271 if (netdev
->type
!= LXC_NET_VETH
)
3272 goto clear_ifindices
;
3274 /* Explicitly delete host veth device to prevent lingering
3275 * devices. We had issues in LXD around this.
3277 if (netdev
->priv
.veth_attr
.pair
[0] != '\0')
3278 hostveth
= netdev
->priv
.veth_attr
.pair
;
3280 hostveth
= netdev
->priv
.veth_attr
.veth1
;
3281 if (hostveth
[0] == '\0')
3282 goto clear_ifindices
;
3284 ret
= lxc_netdev_delete_by_name(hostveth
);
3286 WARN("Failed to remove interface \"%s\" from \"%s\"",
3287 hostveth
, netdev
->link
);
3288 goto clear_ifindices
;
3290 INFO("Removed interface \"%s\" from \"%s\"", hostveth
, netdev
->link
);
3292 if (netdev
->link
[0] == '\0' || !is_ovs_bridge(netdev
->link
)) {
3293 netdev
->priv
.veth_attr
.veth1
[0] = '\0';
3294 netdev
->ifindex
= 0;
3295 netdev
->priv
.veth_attr
.ifindex
= 0;
3296 goto clear_ifindices
;
3299 /* Delete the openvswitch port. */
3300 ret
= lxc_ovs_delete_port(netdev
->link
, hostveth
);
3302 WARN("Failed to remove port \"%s\" from openvswitch "
3303 "bridge \"%s\"", hostveth
, netdev
->link
);
3305 INFO("Removed port \"%s\" from openvswitch bridge \"%s\"",
3306 hostveth
, netdev
->link
);
3309 /* We need to clear any ifindices we recorded so liblxc won't
3310 * have cached stale data which would cause it to fail on reboot
3311 * we're we don't re-read the on-disk config file.
3313 netdev
->ifindex
= 0;
3314 if (netdev
->type
== LXC_NET_PHYS
) {
3315 netdev
->priv
.phys_attr
.ifindex
= 0;
3316 } else if (netdev
->type
== LXC_NET_VETH
) {
3317 netdev
->priv
.veth_attr
.veth1
[0] = '\0';
3318 netdev
->priv
.veth_attr
.ifindex
= 0;
3325 int lxc_requests_empty_network(struct lxc_handler
*handler
)
3327 struct lxc_list
*network
= &handler
->conf
->network
;
3328 struct lxc_list
*iterator
;
3329 bool found_none
= false, found_nic
= false;
3331 if (lxc_list_empty(network
))
3334 lxc_list_for_each(iterator
, network
) {
3335 struct lxc_netdev
*netdev
= iterator
->elem
;
3337 if (netdev
->type
== LXC_NET_NONE
)
3342 if (found_none
&& !found_nic
)
3347 /* try to move physical nics to the init netns */
3348 int lxc_restore_phys_nics_to_netns(struct lxc_handler
*handler
)
3352 char ifname
[IFNAMSIZ
];
3353 struct lxc_list
*iterator
;
3354 int netnsfd
= handler
->nsfd
[LXC_NS_NET
];
3355 struct lxc_conf
*conf
= handler
->conf
;
3357 /* We need CAP_NET_ADMIN in the parent namespace in order to setns() to
3358 * the parent network namespace. We won't have this capability if we are
3361 if (!handler
->am_root
)
3364 TRACE("Moving physical network devices back to parent network namespace");
3366 oldfd
= lxc_preserve_ns(handler
->monitor_pid
, "net");
3368 SYSERROR("Failed to preserve network namespace");
3372 ret
= setns(netnsfd
, CLONE_NEWNET
);
3374 SYSERROR("Failed to enter network namespace");
3379 lxc_list_for_each(iterator
, &conf
->network
) {
3380 struct lxc_netdev
*netdev
= iterator
->elem
;
3382 if (netdev
->type
!= LXC_NET_PHYS
)
3385 /* Retrieve the name of the interface in the container's network
3388 if (!if_indextoname(netdev
->ifindex
, ifname
)) {
3389 WARN("No interface corresponding to ifindex %d",
3394 ret
= lxc_netdev_move_by_index_fd(netdev
->ifindex
, oldfd
, netdev
->link
);
3396 WARN("Error moving network device \"%s\" back to "
3397 "network namespace", ifname
);
3399 TRACE("Moved network device \"%s\" back to network "
3400 "namespace", ifname
);
3403 ret
= setns(oldfd
, CLONE_NEWNET
);
3406 SYSERROR("Failed to enter network namespace");
3413 static int setup_hw_addr(char *hwaddr
, const char *ifname
)
3415 struct sockaddr sockaddr
;
3419 ret
= lxc_convert_mac(hwaddr
, &sockaddr
);
3422 SYSERROR("Mac address \"%s\" conversion failed", hwaddr
);
3426 memcpy(ifr
.ifr_name
, ifname
, IFNAMSIZ
);
3427 ifr
.ifr_name
[IFNAMSIZ
-1] = '\0';
3428 memcpy((char *) &ifr
.ifr_hwaddr
, (char *) &sockaddr
, sizeof(sockaddr
));
3430 fd
= socket(AF_INET
, SOCK_DGRAM
| SOCK_CLOEXEC
, 0);
3434 ret
= ioctl(fd
, SIOCSIFHWADDR
, &ifr
);
3436 SYSERROR("Failed to perform ioctl");
3440 DEBUG("Mac address \"%s\" on \"%s\" has been setup", hwaddr
,
3446 static int setup_ipv4_addr(struct lxc_list
*ip
, int ifindex
)
3448 struct lxc_list
*iterator
;
3451 lxc_list_for_each(iterator
, ip
) {
3452 struct lxc_inetdev
*inetdev
= iterator
->elem
;
3454 err
= lxc_ipv4_addr_add(ifindex
, &inetdev
->addr
,
3455 &inetdev
->bcast
, inetdev
->prefix
);
3458 SYSERROR("Failed to setup ipv4 address for network device "
3459 "with ifindex %d", ifindex
);
3467 static int setup_ipv6_addr(struct lxc_list
*ip
, int ifindex
)
3469 struct lxc_list
*iterator
;
3472 lxc_list_for_each(iterator
, ip
) {
3473 struct lxc_inet6dev
*inet6dev
= iterator
->elem
;
3475 err
= lxc_ipv6_addr_add(ifindex
, &inet6dev
->addr
,
3476 &inet6dev
->mcast
, &inet6dev
->acast
,
3480 SYSERROR("Failed to setup ipv6 address for network device "
3481 "with ifindex %d", ifindex
);
3489 static int lxc_setup_netdev_in_child_namespaces(struct lxc_netdev
*netdev
)
3491 char ifname
[IFNAMSIZ
];
3493 char *current_ifname
= ifname
;
3494 char bufinet4
[INET_ADDRSTRLEN
], bufinet6
[INET6_ADDRSTRLEN
];
3496 /* empty network namespace */
3497 if (!netdev
->ifindex
) {
3498 if (netdev
->flags
& IFF_UP
) {
3499 err
= lxc_netdev_up("lo");
3502 SYSERROR("Failed to set the loopback network device up");
3507 if (netdev
->type
== LXC_NET_EMPTY
)
3510 if (netdev
->type
== LXC_NET_NONE
)
3513 netdev
->ifindex
= if_nametoindex(netdev
->created_name
);
3514 if (!netdev
->ifindex
)
3515 SYSERROR("Failed to retrieve ifindex for network device with name %s",
3516 netdev
->name
?: "(null)");
3519 /* get the new ifindex in case of physical netdev */
3520 if (netdev
->type
== LXC_NET_PHYS
) {
3521 netdev
->ifindex
= if_nametoindex(netdev
->link
);
3522 if (!netdev
->ifindex
) {
3523 ERROR("Failed to get ifindex for network device \"%s\"",
3529 /* retrieve the name of the interface */
3530 if (!if_indextoname(netdev
->ifindex
, current_ifname
)) {
3531 SYSERROR("Failed to retrieve name for network device with ifindex %d",
3536 /* Default: let the system choose an interface name.
3537 * When the IFLA_IFNAME attribute is passed something like "<prefix>%d"
3538 * netlink will replace the format specifier with an appropriate index.
3540 if (netdev
->name
[0] == '\0') {
3541 if (netdev
->type
== LXC_NET_PHYS
)
3542 (void)strlcpy(netdev
->name
, netdev
->link
, IFNAMSIZ
);
3544 (void)strlcpy(netdev
->name
, "eth%d", IFNAMSIZ
);
3547 /* rename the interface name */
3548 if (strcmp(current_ifname
, netdev
->name
) != 0) {
3549 err
= lxc_netdev_rename_by_name(current_ifname
, netdev
->name
);
3552 SYSERROR("Failed to rename network device \"%s\" to \"%s\"",
3553 current_ifname
, netdev
->name
);
3557 TRACE("Renamed network device from \"%s\" to \"%s\"",
3558 current_ifname
, netdev
->name
);
3561 /* Re-read the name of the interface because its name has changed
3562 * and would be automatically allocated by the system
3564 if (!if_indextoname(netdev
->ifindex
, current_ifname
)) {
3565 ERROR("Failed get name for network device with ifindex %d",
3570 /* Now update the recorded name of the network device to reflect the
3571 * name of the network device in the child's network namespace. We will
3572 * later on send this information back to the parent.
3574 (void)strlcpy(netdev
->name
, current_ifname
, IFNAMSIZ
);
3576 /* set a mac address */
3577 if (netdev
->hwaddr
) {
3578 if (setup_hw_addr(netdev
->hwaddr
, current_ifname
)) {
3579 ERROR("Failed to setup hw address for network device \"%s\"",
3585 /* setup ipv4 addresses on the interface */
3586 if (setup_ipv4_addr(&netdev
->ipv4
, netdev
->ifindex
)) {
3587 ERROR("Failed to setup ip addresses for network device \"%s\"",
3592 /* setup ipv6 addresses on the interface */
3593 if (setup_ipv6_addr(&netdev
->ipv6
, netdev
->ifindex
)) {
3594 ERROR("Failed to setup ipv6 addresses for network device \"%s\"",
3599 /* set the network device up */
3600 if (netdev
->flags
& IFF_UP
) {
3601 err
= lxc_netdev_up(current_ifname
);
3604 SYSERROR("Failed to set network device \"%s\" up",
3609 /* the network is up, make the loopback up too */
3610 err
= lxc_netdev_up("lo");
3613 SYSERROR("Failed to set the loopback network device up");
3618 /* setup ipv4 gateway on the interface */
3619 if (netdev
->ipv4_gateway
|| netdev
->ipv4_gateway_dev
) {
3620 if (!(netdev
->flags
& IFF_UP
)) {
3621 ERROR("Cannot add ipv4 gateway for network device "
3622 "\"%s\" when not bringing up the interface", current_ifname
);
3626 if (lxc_list_empty(&netdev
->ipv4
)) {
3627 ERROR("Cannot add ipv4 gateway for network device "
3628 "\"%s\" when not assigning an address", current_ifname
);
3632 /* Setup device route if ipv4_gateway_dev is enabled */
3633 if (netdev
->ipv4_gateway_dev
) {
3634 err
= lxc_ipv4_gateway_add(netdev
->ifindex
, NULL
);
3636 SYSERROR("Failed to setup ipv4 gateway to network device \"%s\"",
3638 return minus_one_set_errno(-err
);
3641 /* Check the gateway address is valid */
3642 if (!inet_ntop(AF_INET
, netdev
->ipv4_gateway
, bufinet4
, sizeof(bufinet4
)))
3643 return minus_one_set_errno(errno
);
3645 /* Try adding a default route to the gateway address */
3646 err
= lxc_ipv4_gateway_add(netdev
->ifindex
, netdev
->ipv4_gateway
);
3648 /* If adding the default route fails, this could be because the
3649 * gateway address is in a different subnet to the container's address.
3650 * To work around this, we try adding a static device route to the
3651 * gateway address first, and then try again.
3653 err
= lxc_ipv4_dest_add(netdev
->ifindex
, netdev
->ipv4_gateway
, 32);
3656 SYSERROR("Failed to add ipv4 dest \"%s\" for network device \"%s\"",
3657 bufinet4
, current_ifname
);
3661 err
= lxc_ipv4_gateway_add(netdev
->ifindex
, netdev
->ipv4_gateway
);
3664 SYSERROR("Failed to setup ipv4 gateway \"%s\" for network device \"%s\"",
3665 bufinet4
, current_ifname
);
3672 /* setup ipv6 gateway on the interface */
3673 if (netdev
->ipv6_gateway
|| netdev
->ipv6_gateway_dev
) {
3674 if (!(netdev
->flags
& IFF_UP
)) {
3675 ERROR("Cannot add ipv6 gateway for network device \"%s\" when not bringing up the interface",
3680 if (lxc_list_empty(&netdev
->ipv6
) && !IN6_IS_ADDR_LINKLOCAL(netdev
->ipv6_gateway
)) {
3681 ERROR("Cannot add ipv6 gateway for network device \"%s\" when not assigning an address",
3686 /* Setup device route if ipv6_gateway_dev is enabled */
3687 if (netdev
->ipv6_gateway_dev
) {
3688 err
= lxc_ipv6_gateway_add(netdev
->ifindex
, NULL
);
3690 SYSERROR("Failed to setup ipv6 gateway to network device \"%s\"",
3692 return minus_one_set_errno(-err
);
3695 /* Check the gateway address is valid */
3696 if (!inet_ntop(AF_INET6
, netdev
->ipv6_gateway
, bufinet6
, sizeof(bufinet6
)))
3697 return minus_one_set_errno(errno
);
3699 /* Try adding a default route to the gateway address */
3700 err
= lxc_ipv6_gateway_add(netdev
->ifindex
, netdev
->ipv6_gateway
);
3702 /* If adding the default route fails, this could be because the
3703 * gateway address is in a different subnet to the container's address.
3704 * To work around this, we try adding a static device route to the
3705 * gateway address first, and then try again.
3707 err
= lxc_ipv6_dest_add(netdev
->ifindex
, netdev
->ipv6_gateway
, 128);
3710 SYSERROR("Failed to add ipv6 dest \"%s\" for network device \"%s\"",
3711 bufinet6
, current_ifname
);
3715 err
= lxc_ipv6_gateway_add(netdev
->ifindex
, netdev
->ipv6_gateway
);
3718 SYSERROR("Failed to setup ipv6 gateway \"%s\" for network device \"%s\"",
3719 bufinet6
, current_ifname
);
3726 DEBUG("Network device \"%s\" has been setup", current_ifname
);
3731 int lxc_setup_network_in_child_namespaces(const struct lxc_conf
*conf
,
3732 struct lxc_list
*network
)
3734 struct lxc_list
*iterator
;
3736 lxc_list_for_each(iterator
, network
) {
3737 struct lxc_netdev
*netdev
= iterator
->elem
;
3739 if (lxc_setup_netdev_in_child_namespaces(netdev
)) {
3740 ERROR("Failed to setup netdev");
3745 if (!lxc_list_empty(network
))
3746 INFO("Network has been setup");
3751 int lxc_network_send_veth_names_to_child(struct lxc_handler
*handler
)
3753 struct lxc_list
*iterator
;
3754 struct lxc_list
*network
= &handler
->conf
->network
;
3755 int data_sock
= handler
->data_sock
[0];
3757 lxc_list_for_each(iterator
, network
) {
3759 struct lxc_netdev
*netdev
= iterator
->elem
;
3761 if (netdev
->type
!= LXC_NET_VETH
)
3764 ret
= lxc_send_nointr(data_sock
, netdev
->name
, IFNAMSIZ
, MSG_NOSIGNAL
);
3768 ret
= lxc_send_nointr(data_sock
, netdev
->created_name
, IFNAMSIZ
, MSG_NOSIGNAL
);
3772 TRACE("Sent network device name \"%s\" to child", netdev
->created_name
);
3778 int lxc_network_recv_veth_names_from_parent(struct lxc_handler
*handler
)
3780 struct lxc_list
*iterator
;
3781 struct lxc_list
*network
= &handler
->conf
->network
;
3782 int data_sock
= handler
->data_sock
[1];
3784 lxc_list_for_each(iterator
, network
) {
3786 struct lxc_netdev
*netdev
= iterator
->elem
;
3788 if (netdev
->type
!= LXC_NET_VETH
)
3791 ret
= lxc_recv_nointr(data_sock
, netdev
->name
, IFNAMSIZ
, 0);
3795 ret
= lxc_recv_nointr(data_sock
, netdev
->created_name
, IFNAMSIZ
, 0);
3798 TRACE("Received network device name \"%s\" from parent", netdev
->created_name
);
3804 int lxc_network_send_name_and_ifindex_to_parent(struct lxc_handler
*handler
)
3806 struct lxc_list
*iterator
, *network
;
3807 int data_sock
= handler
->data_sock
[0];
3809 if (!handler
->am_root
)
3812 network
= &handler
->conf
->network
;
3813 lxc_list_for_each(iterator
, network
) {
3815 struct lxc_netdev
*netdev
= iterator
->elem
;
3817 /* Send network device name in the child's namespace to parent. */
3818 ret
= lxc_send_nointr(data_sock
, netdev
->name
, IFNAMSIZ
, MSG_NOSIGNAL
);
3822 /* Send network device ifindex in the child's namespace to
3825 ret
= lxc_send_nointr(data_sock
, &netdev
->ifindex
, sizeof(netdev
->ifindex
), MSG_NOSIGNAL
);
3830 if (!lxc_list_empty(network
))
3831 TRACE("Sent network device names and ifindices to parent");
3836 int lxc_network_recv_name_and_ifindex_from_child(struct lxc_handler
*handler
)
3838 struct lxc_list
*iterator
, *network
;
3839 int data_sock
= handler
->data_sock
[1];
3841 if (!handler
->am_root
)
3844 network
= &handler
->conf
->network
;
3845 lxc_list_for_each(iterator
, network
) {
3847 struct lxc_netdev
*netdev
= iterator
->elem
;
3849 /* Receive network device name in the child's namespace to
3852 ret
= lxc_recv_nointr(data_sock
, netdev
->name
, IFNAMSIZ
, 0);
3856 /* Receive network device ifindex in the child's namespace to
3859 ret
= lxc_recv_nointr(data_sock
, &netdev
->ifindex
, sizeof(netdev
->ifindex
), 0);
3867 void lxc_delete_network(struct lxc_handler
*handler
)
3871 if (handler
->am_root
)
3872 bret
= lxc_delete_network_priv(handler
);
3874 bret
= lxc_delete_network_unpriv(handler
);
3876 DEBUG("Failed to delete network devices");
3878 DEBUG("Deleted network devices");
3881 int lxc_netns_set_nsid(int fd
)
3884 char buf
[NLMSG_ALIGN(sizeof(struct nlmsghdr
)) +
3885 NLMSG_ALIGN(sizeof(struct rtgenmsg
)) +
3887 struct nl_handler nlh
;
3888 struct nlmsghdr
*hdr
;
3889 struct rtgenmsg
*msg
;
3891 const __s32 ns_id
= -1;
3892 const __u32 netns_fd
= fd
;
3894 ret
= netlink_open(&nlh
, NETLINK_ROUTE
);
3898 memset(buf
, 0, sizeof(buf
));
3900 #pragma GCC diagnostic push
3901 #pragma GCC diagnostic ignored "-Wcast-align"
3902 hdr
= (struct nlmsghdr
*)buf
;
3903 msg
= (struct rtgenmsg
*)NLMSG_DATA(hdr
);
3904 #pragma GCC diagnostic pop
3906 hdr
->nlmsg_len
= NLMSG_LENGTH(sizeof(*msg
));
3907 hdr
->nlmsg_type
= RTM_NEWNSID
;
3908 hdr
->nlmsg_flags
= NLM_F_REQUEST
| NLM_F_ACK
;
3910 hdr
->nlmsg_seq
= RTM_NEWNSID
;
3911 msg
->rtgen_family
= AF_UNSPEC
;
3913 ret
= addattr(hdr
, 1024, __LXC_NETNSA_FD
, &netns_fd
, sizeof(netns_fd
));
3917 ret
= addattr(hdr
, 1024, __LXC_NETNSA_NSID
, &ns_id
, sizeof(ns_id
));
3921 ret
= __netlink_transaction(&nlh
, hdr
, hdr
);
3924 saved_errno
= errno
;
3925 netlink_close(&nlh
);
3926 errno
= saved_errno
;
3931 static int parse_rtattr(struct rtattr
*tb
[], int max
, struct rtattr
*rta
, int len
)
3934 memset(tb
, 0, sizeof(struct rtattr
*) * (max
+ 1));
3936 while (RTA_OK(rta
, len
)) {
3937 unsigned short type
= rta
->rta_type
;
3939 if ((type
<= max
) && (!tb
[type
]))
3942 #pragma GCC diagnostic push
3943 #pragma GCC diagnostic ignored "-Wcast-align"
3944 rta
= RTA_NEXT(rta
, len
);
3945 #pragma GCC diagnostic pop
3951 static inline __s32
rta_getattr_s32(const struct rtattr
*rta
)
3953 return *(__s32
*)RTA_DATA(rta
);
3957 #define NETNS_RTA(r) \
3958 ((struct rtattr *)(((char *)(r)) + NLMSG_ALIGN(sizeof(struct rtgenmsg))))
3961 int lxc_netns_get_nsid(int fd
)
3965 char buf
[NLMSG_ALIGN(sizeof(struct nlmsghdr
)) +
3966 NLMSG_ALIGN(sizeof(struct rtgenmsg
)) +
3968 struct rtattr
*tb
[__LXC_NETNSA_MAX
+ 1];
3969 struct nl_handler nlh
;
3970 struct nlmsghdr
*hdr
;
3971 struct rtgenmsg
*msg
;
3973 __u32 netns_fd
= fd
;
3975 ret
= netlink_open(&nlh
, NETLINK_ROUTE
);
3979 memset(buf
, 0, sizeof(buf
));
3981 #pragma GCC diagnostic push
3982 #pragma GCC diagnostic ignored "-Wcast-align"
3983 hdr
= (struct nlmsghdr
*)buf
;
3984 msg
= (struct rtgenmsg
*)NLMSG_DATA(hdr
);
3985 #pragma GCC diagnostic pop
3987 hdr
->nlmsg_len
= NLMSG_LENGTH(sizeof(*msg
));
3988 hdr
->nlmsg_type
= RTM_GETNSID
;
3989 hdr
->nlmsg_flags
= NLM_F_REQUEST
| NLM_F_ACK
;
3991 hdr
->nlmsg_seq
= RTM_GETNSID
;
3992 msg
->rtgen_family
= AF_UNSPEC
;
3994 ret
= addattr(hdr
, 1024, __LXC_NETNSA_FD
, &netns_fd
, sizeof(netns_fd
));
3996 ret
= __netlink_transaction(&nlh
, hdr
, hdr
);
3998 saved_errno
= errno
;
3999 netlink_close(&nlh
);
4000 errno
= saved_errno
;
4005 msg
= NLMSG_DATA(hdr
);
4006 len
= hdr
->nlmsg_len
- NLMSG_SPACE(sizeof(*msg
));
4010 #pragma GCC diagnostic push
4011 #pragma GCC diagnostic ignored "-Wcast-align"
4012 parse_rtattr(tb
, __LXC_NETNSA_MAX
, NETNS_RTA(msg
), len
);
4013 if (tb
[__LXC_NETNSA_NSID
])
4014 return rta_getattr_s32(tb
[__LXC_NETNSA_NSID
]);
4015 #pragma GCC diagnostic pop
4020 int lxc_create_network(struct lxc_handler
*handler
)
4025 * Find gateway addresses from the link device, which is no longer
4026 * accessible inside the container. Do this before creating network
4027 * interfaces, since goto out_delete_net does not work before
4030 ret
= lxc_find_gateway_addresses(handler
);
4032 ERROR("Failed to find gateway addresses");
4036 if (handler
->am_root
) {
4037 ret
= lxc_create_network_priv(handler
);
4041 return lxc_network_move_created_netdev_priv(handler
);
4044 return lxc_create_network_unpriv(handler
);