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
33 #include <arpa/inet.h>
34 #include <linux/netlink.h>
35 #include <linux/rtnetlink.h>
36 #include <linux/sockios.h>
37 #include <net/ethernet.h>
39 #include <net/if_arp.h>
40 #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>
51 #include "file_utils.h"
61 #include <../include/ifaddrs.h>
65 #include "include/strlcpy.h"
68 lxc_log_define(network
, lxc
);
70 typedef int (*instantiate_cb
)(struct lxc_handler
*, struct lxc_netdev
*);
72 static int instantiate_veth(struct lxc_handler
*handler
, struct lxc_netdev
*netdev
)
74 int bridge_index
, err
;
76 char veth1buf
[IFNAMSIZ
], veth2buf
[IFNAMSIZ
];
79 if (netdev
->priv
.veth_attr
.pair
[0] != '\0') {
80 veth1
= netdev
->priv
.veth_attr
.pair
;
81 if (handler
->conf
->reboot
)
82 lxc_netdev_delete_by_name(veth1
);
84 err
= snprintf(veth1buf
, sizeof(veth1buf
), "vethXXXXXX");
85 if (err
< 0 || (size_t)err
>= sizeof(veth1buf
))
88 veth1
= lxc_mkifname(veth1buf
);
92 /* store away for deconf */
93 memcpy(netdev
->priv
.veth_attr
.veth1
, veth1
, IFNAMSIZ
);
96 err
= snprintf(veth2buf
, sizeof(veth2buf
), "vethXXXXXX");
97 if (err
< 0 || (size_t)err
>= sizeof(veth2buf
))
100 veth2
= lxc_mkifname(veth2buf
);
104 err
= lxc_veth_create(veth1
, veth2
);
107 SYSERROR("Failed to create veth pair \"%s\" and \"%s\"", veth1
, veth2
);
111 /* changing the high byte of the mac address to 0xfe, the bridge interface
112 * will always keep the host's mac address and not take the mac address
114 err
= setup_private_host_hw_addr(veth1
);
117 SYSERROR("Failed to change mac address of host interface \"%s\"", veth1
);
121 /* Retrieve ifindex of the host's veth device. */
122 netdev
->priv
.veth_attr
.ifindex
= if_nametoindex(veth1
);
123 if (!netdev
->priv
.veth_attr
.ifindex
) {
124 ERROR("Failed to retrieve ifindex for \"%s\"", veth1
);
128 /* Note that we're retrieving the container's ifindex in the host's
129 * network namespace because we need it to move the device from the
130 * host's network namespace to the container's network namespace later
133 netdev
->ifindex
= if_nametoindex(veth2
);
134 if (!netdev
->ifindex
) {
135 ERROR("Failed to retrieve ifindex for \"%s\"", veth2
);
140 if (lxc_safe_uint(netdev
->mtu
, &mtu
) < 0)
141 WARN("Failed to parse mtu");
143 INFO("Retrieved mtu %d", mtu
);
144 } else if (netdev
->link
[0] != '\0') {
145 bridge_index
= if_nametoindex(netdev
->link
);
147 mtu
= netdev_get_mtu(bridge_index
);
148 INFO("Retrieved mtu %d from %s", mtu
, netdev
->link
);
150 mtu
= netdev_get_mtu(netdev
->ifindex
);
151 INFO("Retrieved mtu %d from %s", mtu
, veth2
);
156 err
= lxc_netdev_set_mtu(veth1
, mtu
);
158 err
= lxc_netdev_set_mtu(veth2
, mtu
);
162 SYSERROR("Failed to set mtu \"%d\" for veth pair \"%s\" "
163 "and \"%s\"", mtu
, veth1
, veth2
);
168 if (netdev
->link
[0] != '\0') {
169 err
= lxc_bridge_attach(netdev
->link
, veth1
);
172 SYSERROR("Failed to attach \"%s\" to bridge \"%s\"",
173 veth1
, netdev
->link
);
176 INFO("Attached \"%s\" to bridge \"%s\"", veth1
, netdev
->link
);
179 err
= lxc_netdev_up(veth1
);
182 SYSERROR("Failed to set \"%s\" up", veth1
);
186 if (netdev
->upscript
) {
194 err
= run_script_argv(handler
->name
,
195 handler
->conf
->hooks_version
, "net",
196 netdev
->upscript
, "up", argv
);
201 DEBUG("Instantiated veth \"%s/%s\", index is \"%d\"", veth1
, veth2
,
207 if (netdev
->ifindex
!= 0)
208 lxc_netdev_delete_by_name(veth1
);
212 static int instantiate_macvlan(struct lxc_handler
*handler
, struct lxc_netdev
*netdev
)
214 char peerbuf
[IFNAMSIZ
], *peer
;
217 if (netdev
->link
[0] == '\0') {
218 ERROR("No link for macvlan network device specified");
222 err
= snprintf(peerbuf
, sizeof(peerbuf
), "mcXXXXXX");
223 if (err
< 0 || (size_t)err
>= sizeof(peerbuf
))
226 peer
= lxc_mkifname(peerbuf
);
230 err
= lxc_macvlan_create(netdev
->link
, peer
,
231 netdev
->priv
.macvlan_attr
.mode
);
234 SYSERROR("Failed to create macvlan interface \"%s\" on \"%s\"",
239 netdev
->ifindex
= if_nametoindex(peer
);
240 if (!netdev
->ifindex
) {
241 ERROR("Failed to retrieve ifindex for \"%s\"", peer
);
245 if (netdev
->upscript
) {
252 err
= run_script_argv(handler
->name
,
253 handler
->conf
->hooks_version
, "net",
254 netdev
->upscript
, "up", argv
);
259 DEBUG("Instantiated macvlan \"%s\" with ifindex is %d and mode %d",
260 peer
, netdev
->ifindex
, netdev
->priv
.macvlan_attr
.mode
);
265 lxc_netdev_delete_by_name(peer
);
269 static int instantiate_vlan(struct lxc_handler
*handler
, struct lxc_netdev
*netdev
)
273 static uint16_t vlan_cntr
= 0;
274 unsigned int mtu
= 0;
276 if (netdev
->link
[0] == '\0') {
277 ERROR("No link for vlan network device specified");
281 err
= snprintf(peer
, sizeof(peer
), "vlan%d-%d", netdev
->priv
.vlan_attr
.vid
, vlan_cntr
++);
282 if (err
< 0 || (size_t)err
>= sizeof(peer
))
285 err
= lxc_vlan_create(netdev
->link
, peer
, netdev
->priv
.vlan_attr
.vid
);
288 SYSERROR("Failed to create vlan interface \"%s\" on \"%s\"",
293 netdev
->ifindex
= if_nametoindex(peer
);
294 if (!netdev
->ifindex
) {
295 ERROR("Failed to retrieve ifindex for \"%s\"", peer
);
296 lxc_netdev_delete_by_name(peer
);
300 DEBUG("Instantiated vlan \"%s\" with ifindex is \"%d\" (vlan1000)",
301 peer
, netdev
->ifindex
);
303 if (lxc_safe_uint(netdev
->mtu
, &mtu
) < 0) {
304 ERROR("Failed to retrieve mtu from \"%d\"/\"%s\".",
306 netdev
->name
[0] != '\0' ? netdev
->name
: "(null)");
310 err
= lxc_netdev_set_mtu(peer
, mtu
);
313 SYSERROR("Failed to set mtu \"%s\" for \"%s\"",
315 lxc_netdev_delete_by_name(peer
);
323 static int instantiate_phys(struct lxc_handler
*handler
, struct lxc_netdev
*netdev
)
332 if (netdev
->link
[0] == '\0') {
333 ERROR("No link for physical interface specified");
337 /* Note that we're retrieving the container's ifindex in the host's
338 * network namespace because we need it to move the device from the
339 * host's network namespace to the container's network namespace later
341 * Note that netdev->link will contain the name of the physical network
342 * device in the host's namespace.
344 netdev
->ifindex
= if_nametoindex(netdev
->link
);
345 if (!netdev
->ifindex
) {
346 ERROR("Failed to retrieve ifindex for \"%s\"", netdev
->link
);
350 /* Store the ifindex of the host's network device in the host's
353 netdev
->priv
.phys_attr
.ifindex
= netdev
->ifindex
;
355 if (!netdev
->upscript
)
358 ret
= run_script_argv(handler
->name
, handler
->conf
->hooks_version
,
359 "net", netdev
->upscript
, "up", argv
);
366 static int instantiate_empty(struct lxc_handler
*handler
, struct lxc_netdev
*netdev
)
375 if (!netdev
->upscript
)
378 ret
= run_script_argv(handler
->name
, handler
->conf
->hooks_version
,
379 "net", netdev
->upscript
, "up", argv
);
386 static int instantiate_none(struct lxc_handler
*handler
, struct lxc_netdev
*netdev
)
392 static instantiate_cb netdev_conf
[LXC_NET_MAXCONFTYPE
+ 1] = {
393 [LXC_NET_VETH
] = instantiate_veth
,
394 [LXC_NET_MACVLAN
] = instantiate_macvlan
,
395 [LXC_NET_VLAN
] = instantiate_vlan
,
396 [LXC_NET_PHYS
] = instantiate_phys
,
397 [LXC_NET_EMPTY
] = instantiate_empty
,
398 [LXC_NET_NONE
] = instantiate_none
,
401 static int shutdown_veth(struct lxc_handler
*handler
, struct lxc_netdev
*netdev
)
411 if (!netdev
->downscript
)
414 if (netdev
->priv
.veth_attr
.pair
[0] != '\0')
415 argv
[2] = netdev
->priv
.veth_attr
.pair
;
417 argv
[2] = netdev
->priv
.veth_attr
.veth1
;
419 ret
= run_script_argv(handler
->name
,
420 handler
->conf
->hooks_version
, "net",
421 netdev
->downscript
, "down", argv
);
428 static int shutdown_macvlan(struct lxc_handler
*handler
, struct lxc_netdev
*netdev
)
437 if (!netdev
->downscript
)
440 ret
= run_script_argv(handler
->name
, handler
->conf
->hooks_version
,
441 "net", netdev
->downscript
, "down", argv
);
448 static int shutdown_vlan(struct lxc_handler
*handler
, struct lxc_netdev
*netdev
)
453 static int shutdown_phys(struct lxc_handler
*handler
, struct lxc_netdev
*netdev
)
462 if (!netdev
->downscript
)
465 ret
= run_script_argv(handler
->name
, handler
->conf
->hooks_version
,
466 "net", netdev
->downscript
, "down", argv
);
473 static int shutdown_empty(struct lxc_handler
*handler
, struct lxc_netdev
*netdev
)
481 if (!netdev
->downscript
)
484 ret
= run_script_argv(handler
->name
, handler
->conf
->hooks_version
,
485 "net", netdev
->downscript
, "down", argv
);
492 static int shutdown_none(struct lxc_handler
*handler
, struct lxc_netdev
*netdev
)
497 static instantiate_cb netdev_deconf
[LXC_NET_MAXCONFTYPE
+ 1] = {
498 [LXC_NET_VETH
] = shutdown_veth
,
499 [LXC_NET_MACVLAN
] = shutdown_macvlan
,
500 [LXC_NET_VLAN
] = shutdown_vlan
,
501 [LXC_NET_PHYS
] = shutdown_phys
,
502 [LXC_NET_EMPTY
] = shutdown_empty
,
503 [LXC_NET_NONE
] = shutdown_none
,
506 int lxc_netdev_move_by_index(int ifindex
, pid_t pid
, const char *ifname
)
509 struct nl_handler nlh
;
510 struct ifinfomsg
*ifi
;
511 struct nlmsg
*nlmsg
= NULL
;
513 err
= netlink_open(&nlh
, NETLINK_ROUTE
);
518 nlmsg
= nlmsg_alloc(NLMSG_GOOD_SIZE
);
522 nlmsg
->nlmsghdr
->nlmsg_flags
= NLM_F_REQUEST
| NLM_F_ACK
;
523 nlmsg
->nlmsghdr
->nlmsg_type
= RTM_NEWLINK
;
525 ifi
= nlmsg_reserve(nlmsg
, sizeof(struct ifinfomsg
));
528 ifi
->ifi_family
= AF_UNSPEC
;
529 ifi
->ifi_index
= ifindex
;
531 if (nla_put_u32(nlmsg
, IFLA_NET_NS_PID
, pid
))
534 if (ifname
!= NULL
) {
535 if (nla_put_string(nlmsg
, IFLA_IFNAME
, ifname
))
539 err
= netlink_transaction(&nlh
, nlmsg
, nlmsg
);
546 /* If we are asked to move a wireless interface, then we must actually move its
547 * phyN device. Detect that condition and return the physname here. The physname
548 * will be passed to lxc_netdev_move_wlan() which will free it when done.
550 #define PHYSNAME "/sys/class/net/%s/phy80211/name"
551 static char *is_wlan(const char *ifname
)
558 char *physname
= NULL
;
560 len
= strlen(ifname
) + strlen(PHYSNAME
) - 1;
561 path
= alloca(len
+ 1);
562 ret
= snprintf(path
, len
, PHYSNAME
, ifname
);
563 if (ret
< 0 || (size_t)ret
>= len
)
566 f
= fopen(path
, "r");
570 /* Feh - sb.st_size is always 4096. */
571 fseek(f
, 0, SEEK_END
);
573 fseek(f
, 0, SEEK_SET
);
579 physname
= malloc(physlen
+ 1);
585 memset(physname
, 0, physlen
+ 1);
586 ret
= fread(physname
, 1, physlen
, f
);
591 for (i
= 0; i
< physlen
; i
++) {
592 if (physname
[i
] == '\n')
595 if (physname
[i
] == '\0')
606 static int lxc_netdev_rename_by_name_in_netns(pid_t pid
, const char *old
,
616 return wait_for_pid(fpid
);
618 if (!switch_to_ns(pid
, "net"))
621 _exit(lxc_netdev_rename_by_name(old
, new));
624 static int lxc_netdev_move_wlan(char *physname
, const char *ifname
, pid_t pid
,
631 /* Move phyN into the container. TODO - do this using netlink.
632 * However, IIUC this involves a bit more complicated work to talk to
633 * the 80211 module, so for now just call out to iw.
635 cmd
= on_path("iw", NULL
);
646 sprintf(pidstr
, "%d", pid
);
647 execlp("iw", "iw", "phy", physname
, "set", "netns", pidstr
,
652 if (wait_for_pid(fpid
))
657 err
= lxc_netdev_rename_by_name_in_netns(pid
, ifname
, newname
);
664 int lxc_netdev_move_by_name(const char *ifname
, pid_t pid
, const char* newname
)
672 index
= if_nametoindex(ifname
);
676 physname
= is_wlan(ifname
);
678 return lxc_netdev_move_wlan(physname
, ifname
, pid
, newname
);
680 return lxc_netdev_move_by_index(index
, pid
, newname
);
683 int lxc_netdev_delete_by_index(int ifindex
)
686 struct ifinfomsg
*ifi
;
687 struct nl_handler nlh
;
688 struct nlmsg
*answer
= NULL
, *nlmsg
= NULL
;
690 err
= netlink_open(&nlh
, NETLINK_ROUTE
);
695 nlmsg
= nlmsg_alloc(NLMSG_GOOD_SIZE
);
699 answer
= nlmsg_alloc_reserve(NLMSG_GOOD_SIZE
);
703 nlmsg
->nlmsghdr
->nlmsg_flags
= NLM_F_ACK
| NLM_F_REQUEST
;
704 nlmsg
->nlmsghdr
->nlmsg_type
= RTM_DELLINK
;
706 ifi
= nlmsg_reserve(nlmsg
, sizeof(struct ifinfomsg
));
709 ifi
->ifi_family
= AF_UNSPEC
;
710 ifi
->ifi_index
= ifindex
;
712 err
= netlink_transaction(&nlh
, nlmsg
, answer
);
720 int lxc_netdev_delete_by_name(const char *name
)
724 index
= if_nametoindex(name
);
728 return lxc_netdev_delete_by_index(index
);
731 int lxc_netdev_rename_by_index(int ifindex
, const char *newname
)
734 struct ifinfomsg
*ifi
;
735 struct nl_handler nlh
;
736 struct nlmsg
*answer
= NULL
, *nlmsg
= NULL
;
738 err
= netlink_open(&nlh
, NETLINK_ROUTE
);
742 len
= strlen(newname
);
743 if (len
== 1 || len
>= IFNAMSIZ
)
747 nlmsg
= nlmsg_alloc(NLMSG_GOOD_SIZE
);
751 answer
= nlmsg_alloc_reserve(NLMSG_GOOD_SIZE
);
755 nlmsg
->nlmsghdr
->nlmsg_flags
= NLM_F_ACK
| NLM_F_REQUEST
;
756 nlmsg
->nlmsghdr
->nlmsg_type
= RTM_NEWLINK
;
758 ifi
= nlmsg_reserve(nlmsg
, sizeof(struct ifinfomsg
));
761 ifi
->ifi_family
= AF_UNSPEC
;
762 ifi
->ifi_index
= ifindex
;
764 if (nla_put_string(nlmsg
, IFLA_IFNAME
, newname
))
767 err
= netlink_transaction(&nlh
, nlmsg
, answer
);
775 int lxc_netdev_rename_by_name(const char *oldname
, const char *newname
)
779 len
= strlen(oldname
);
780 if (len
== 1 || len
>= IFNAMSIZ
)
783 index
= if_nametoindex(oldname
);
787 return lxc_netdev_rename_by_index(index
, newname
);
790 int netdev_set_flag(const char *name
, int flag
)
793 struct ifinfomsg
*ifi
;
794 struct nl_handler nlh
;
795 struct nlmsg
*answer
= NULL
, *nlmsg
= NULL
;
797 err
= netlink_open(&nlh
, NETLINK_ROUTE
);
803 if (len
== 1 || len
>= IFNAMSIZ
)
807 nlmsg
= nlmsg_alloc(NLMSG_GOOD_SIZE
);
811 answer
= nlmsg_alloc_reserve(NLMSG_GOOD_SIZE
);
816 index
= if_nametoindex(name
);
820 nlmsg
->nlmsghdr
->nlmsg_flags
= NLM_F_REQUEST
| NLM_F_ACK
;
821 nlmsg
->nlmsghdr
->nlmsg_type
= RTM_NEWLINK
;
823 ifi
= nlmsg_reserve(nlmsg
, sizeof(struct ifinfomsg
));
828 ifi
->ifi_family
= AF_UNSPEC
;
829 ifi
->ifi_index
= index
;
830 ifi
->ifi_change
|= IFF_UP
;
831 ifi
->ifi_flags
|= flag
;
833 err
= netlink_transaction(&nlh
, nlmsg
, answer
);
841 int netdev_get_flag(const char *name
, int *flag
)
844 struct ifinfomsg
*ifi
;
845 struct nl_handler nlh
;
846 struct nlmsg
*answer
= NULL
, *nlmsg
= NULL
;
851 err
= netlink_open(&nlh
, NETLINK_ROUTE
);
857 if (len
== 1 || len
>= IFNAMSIZ
)
861 nlmsg
= nlmsg_alloc(NLMSG_GOOD_SIZE
);
865 answer
= nlmsg_alloc_reserve(NLMSG_GOOD_SIZE
);
870 index
= if_nametoindex(name
);
874 nlmsg
->nlmsghdr
->nlmsg_flags
= NLM_F_REQUEST
;
875 nlmsg
->nlmsghdr
->nlmsg_type
= RTM_GETLINK
;
877 ifi
= nlmsg_reserve(nlmsg
, sizeof(struct ifinfomsg
));
882 ifi
->ifi_family
= AF_UNSPEC
;
883 ifi
->ifi_index
= index
;
885 err
= netlink_transaction(&nlh
, nlmsg
, answer
);
889 ifi
= NLMSG_DATA(answer
->nlmsghdr
);
891 *flag
= ifi
->ifi_flags
;
900 * \brief Check a interface is up or not.
902 * \param name: name for the interface.
905 * 0 means interface is down.
906 * 1 means interface is up.
907 * Others means error happened, and ret-value is the error number.
909 int lxc_netdev_isup(const char *name
)
913 err
= netdev_get_flag(name
, &flag
);
923 int netdev_get_mtu(int ifindex
)
925 int answer_len
, err
, res
;
926 struct nl_handler nlh
;
927 struct ifinfomsg
*ifi
;
928 struct nlmsghdr
*msg
;
929 int readmore
= 0, recv_len
= 0;
930 struct nlmsg
*answer
= NULL
, *nlmsg
= NULL
;
932 err
= netlink_open(&nlh
, NETLINK_ROUTE
);
937 nlmsg
= nlmsg_alloc(NLMSG_GOOD_SIZE
);
941 answer
= nlmsg_alloc_reserve(NLMSG_GOOD_SIZE
);
945 /* Save the answer buffer length, since it will be overwritten
946 * on the first receive (and we might need to receive more than
949 answer_len
= answer
->nlmsghdr
->nlmsg_len
;
951 nlmsg
->nlmsghdr
->nlmsg_flags
= NLM_F_REQUEST
| NLM_F_DUMP
;
952 nlmsg
->nlmsghdr
->nlmsg_type
= RTM_GETLINK
;
954 ifi
= nlmsg_reserve(nlmsg
, sizeof(struct ifinfomsg
));
957 ifi
->ifi_family
= AF_UNSPEC
;
959 /* Send the request for addresses, which returns all addresses
960 * on all interfaces. */
961 err
= netlink_send(&nlh
, nlmsg
);
966 /* Restore the answer buffer length, it might have been
967 * overwritten by a previous receive.
969 answer
->nlmsghdr
->nlmsg_len
= answer_len
;
971 /* Get the (next) batch of reply messages */
972 err
= netlink_rcv(&nlh
, answer
);
978 /* Satisfy the typing for the netlink macros */
979 msg
= answer
->nlmsghdr
;
981 while (NLMSG_OK(msg
, recv_len
)) {
983 /* Stop reading if we see an error message */
984 if (msg
->nlmsg_type
== NLMSG_ERROR
) {
985 struct nlmsgerr
*errmsg
=
986 (struct nlmsgerr
*)NLMSG_DATA(msg
);
991 /* Stop reading if we see a NLMSG_DONE message */
992 if (msg
->nlmsg_type
== NLMSG_DONE
) {
997 ifi
= NLMSG_DATA(msg
);
998 if (ifi
->ifi_index
== ifindex
) {
999 struct rtattr
*rta
= IFLA_RTA(ifi
);
1001 msg
->nlmsg_len
- NLMSG_LENGTH(sizeof(*ifi
));
1003 while (RTA_OK(rta
, attr_len
)) {
1004 /* Found a local address for the
1005 * requested interface, return it.
1007 if (rta
->rta_type
== IFLA_MTU
) {
1008 memcpy(&res
, RTA_DATA(rta
),
1013 rta
= RTA_NEXT(rta
, attr_len
);
1017 /* Keep reading more data from the socket if the last
1018 * message had the NLF_F_MULTI flag set.
1020 readmore
= (msg
->nlmsg_flags
& NLM_F_MULTI
);
1022 /* Look at the next message received in this buffer. */
1023 msg
= NLMSG_NEXT(msg
, recv_len
);
1027 /* If we end up here, we didn't find any result, so signal an error. */
1031 netlink_close(&nlh
);
1037 int lxc_netdev_set_mtu(const char *name
, int mtu
)
1039 int err
, index
, len
;
1040 struct ifinfomsg
*ifi
;
1041 struct nl_handler nlh
;
1042 struct nlmsg
*answer
= NULL
, *nlmsg
= NULL
;
1044 err
= netlink_open(&nlh
, NETLINK_ROUTE
);
1050 if (len
== 1 || len
>= IFNAMSIZ
)
1054 nlmsg
= nlmsg_alloc(NLMSG_GOOD_SIZE
);
1058 answer
= nlmsg_alloc_reserve(NLMSG_GOOD_SIZE
);
1063 index
= if_nametoindex(name
);
1067 nlmsg
->nlmsghdr
->nlmsg_flags
= NLM_F_REQUEST
| NLM_F_ACK
;
1068 nlmsg
->nlmsghdr
->nlmsg_type
= RTM_NEWLINK
;
1070 ifi
= nlmsg_reserve(nlmsg
, sizeof(struct ifinfomsg
));
1075 ifi
->ifi_family
= AF_UNSPEC
;
1076 ifi
->ifi_index
= index
;
1078 if (nla_put_u32(nlmsg
, IFLA_MTU
, mtu
))
1081 err
= netlink_transaction(&nlh
, nlmsg
, answer
);
1083 netlink_close(&nlh
);
1089 int lxc_netdev_up(const char *name
)
1091 return netdev_set_flag(name
, IFF_UP
);
1094 int lxc_netdev_down(const char *name
)
1096 return netdev_set_flag(name
, 0);
1099 int lxc_veth_create(const char *name1
, const char *name2
)
1102 struct ifinfomsg
*ifi
;
1103 struct nl_handler nlh
;
1104 struct rtattr
*nest1
, *nest2
, *nest3
;
1105 struct nlmsg
*answer
= NULL
, *nlmsg
= NULL
;
1107 err
= netlink_open(&nlh
, NETLINK_ROUTE
);
1112 len
= strlen(name1
);
1113 if (len
== 1 || len
>= IFNAMSIZ
)
1116 len
= strlen(name2
);
1117 if (len
== 1 || len
>= IFNAMSIZ
)
1121 nlmsg
= nlmsg_alloc(NLMSG_GOOD_SIZE
);
1125 answer
= nlmsg_alloc_reserve(NLMSG_GOOD_SIZE
);
1129 nlmsg
->nlmsghdr
->nlmsg_flags
=
1130 NLM_F_REQUEST
| NLM_F_CREATE
| NLM_F_EXCL
| NLM_F_ACK
;
1131 nlmsg
->nlmsghdr
->nlmsg_type
= RTM_NEWLINK
;
1133 ifi
= nlmsg_reserve(nlmsg
, sizeof(struct ifinfomsg
));
1136 ifi
->ifi_family
= AF_UNSPEC
;
1139 nest1
= nla_begin_nested(nlmsg
, IFLA_LINKINFO
);
1143 if (nla_put_string(nlmsg
, IFLA_INFO_KIND
, "veth"))
1146 nest2
= nla_begin_nested(nlmsg
, IFLA_INFO_DATA
);
1150 nest3
= nla_begin_nested(nlmsg
, VETH_INFO_PEER
);
1154 ifi
= nlmsg_reserve(nlmsg
, sizeof(struct ifinfomsg
));
1160 if (nla_put_string(nlmsg
, IFLA_IFNAME
, name2
))
1163 nla_end_nested(nlmsg
, nest3
);
1164 nla_end_nested(nlmsg
, nest2
);
1165 nla_end_nested(nlmsg
, nest1
);
1167 if (nla_put_string(nlmsg
, IFLA_IFNAME
, name1
))
1170 err
= netlink_transaction(&nlh
, nlmsg
, answer
);
1172 netlink_close(&nlh
);
1178 /* TODO: merge with lxc_macvlan_create */
1179 int lxc_vlan_create(const char *master
, const char *name
, unsigned short vlanid
)
1181 int err
, len
, lindex
;
1182 struct ifinfomsg
*ifi
;
1183 struct nl_handler nlh
;
1184 struct rtattr
*nest
, *nest2
;
1185 struct nlmsg
*answer
= NULL
, *nlmsg
= NULL
;
1187 err
= netlink_open(&nlh
, NETLINK_ROUTE
);
1192 len
= strlen(master
);
1193 if (len
== 1 || len
>= IFNAMSIZ
)
1197 if (len
== 1 || len
>= IFNAMSIZ
)
1201 nlmsg
= nlmsg_alloc(NLMSG_GOOD_SIZE
);
1205 answer
= nlmsg_alloc_reserve(NLMSG_GOOD_SIZE
);
1210 lindex
= if_nametoindex(master
);
1214 nlmsg
->nlmsghdr
->nlmsg_flags
=
1215 NLM_F_REQUEST
| NLM_F_CREATE
| NLM_F_EXCL
| NLM_F_ACK
;
1216 nlmsg
->nlmsghdr
->nlmsg_type
= RTM_NEWLINK
;
1218 ifi
= nlmsg_reserve(nlmsg
, sizeof(struct ifinfomsg
));
1223 ifi
->ifi_family
= AF_UNSPEC
;
1225 nest
= nla_begin_nested(nlmsg
, IFLA_LINKINFO
);
1229 if (nla_put_string(nlmsg
, IFLA_INFO_KIND
, "vlan"))
1232 nest2
= nla_begin_nested(nlmsg
, IFLA_INFO_DATA
);
1236 if (nla_put_u16(nlmsg
, IFLA_VLAN_ID
, vlanid
))
1239 nla_end_nested(nlmsg
, nest2
);
1240 nla_end_nested(nlmsg
, nest
);
1242 if (nla_put_u32(nlmsg
, IFLA_LINK
, lindex
))
1245 if (nla_put_string(nlmsg
, IFLA_IFNAME
, name
))
1248 err
= netlink_transaction(&nlh
, nlmsg
, answer
);
1254 netlink_close(&nlh
);
1258 int lxc_macvlan_create(const char *master
, const char *name
, int mode
)
1260 int err
, index
, len
;
1261 struct ifinfomsg
*ifi
;
1262 struct nl_handler nlh
;
1263 struct rtattr
*nest
, *nest2
;
1264 struct nlmsg
*answer
= NULL
, *nlmsg
= NULL
;
1266 err
= netlink_open(&nlh
, NETLINK_ROUTE
);
1271 len
= strlen(master
);
1272 if (len
== 1 || len
>= IFNAMSIZ
)
1276 if (len
== 1 || len
>= IFNAMSIZ
)
1280 nlmsg
= nlmsg_alloc(NLMSG_GOOD_SIZE
);
1284 answer
= nlmsg_alloc_reserve(NLMSG_GOOD_SIZE
);
1289 index
= if_nametoindex(master
);
1293 nlmsg
->nlmsghdr
->nlmsg_flags
=
1294 NLM_F_REQUEST
| NLM_F_CREATE
| NLM_F_EXCL
| NLM_F_ACK
;
1295 nlmsg
->nlmsghdr
->nlmsg_type
= RTM_NEWLINK
;
1297 ifi
= nlmsg_reserve(nlmsg
, sizeof(struct ifinfomsg
));
1302 ifi
->ifi_family
= AF_UNSPEC
;
1304 nest
= nla_begin_nested(nlmsg
, IFLA_LINKINFO
);
1308 if (nla_put_string(nlmsg
, IFLA_INFO_KIND
, "macvlan"))
1312 nest2
= nla_begin_nested(nlmsg
, IFLA_INFO_DATA
);
1316 if (nla_put_u32(nlmsg
, IFLA_MACVLAN_MODE
, mode
))
1319 nla_end_nested(nlmsg
, nest2
);
1322 nla_end_nested(nlmsg
, nest
);
1324 if (nla_put_u32(nlmsg
, IFLA_LINK
, index
))
1327 if (nla_put_string(nlmsg
, IFLA_IFNAME
, name
))
1330 err
= netlink_transaction(&nlh
, nlmsg
, answer
);
1332 netlink_close(&nlh
);
1338 static int proc_sys_net_write(const char *path
, const char *value
)
1343 fd
= open(path
, O_WRONLY
);
1347 if (lxc_write_nointr(fd
, value
, strlen(value
)) < 0)
1354 static int neigh_proxy_set(const char *ifname
, int family
, int flag
)
1357 char path
[MAXPATHLEN
];
1359 if (family
!= AF_INET
&& family
!= AF_INET6
)
1362 ret
= snprintf(path
, MAXPATHLEN
, "/proc/sys/net/%s/conf/%s/%s",
1363 family
== AF_INET
? "ipv4" : "ipv6", ifname
,
1364 family
== AF_INET
? "proxy_arp" : "proxy_ndp");
1365 if (ret
< 0 || (size_t)ret
>= MAXPATHLEN
)
1368 return proc_sys_net_write(path
, flag
? "1" : "0");
1371 int lxc_neigh_proxy_on(const char *name
, int family
)
1373 return neigh_proxy_set(name
, family
, 1);
1376 int lxc_neigh_proxy_off(const char *name
, int family
)
1378 return neigh_proxy_set(name
, family
, 0);
1381 int lxc_convert_mac(char *macaddr
, struct sockaddr
*sockaddr
)
1386 unsigned char *data
;
1388 sockaddr
->sa_family
= ARPHRD_ETHER
;
1389 data
= (unsigned char *)sockaddr
->sa_data
;
1391 while ((*macaddr
!= '\0') && (i
< ETH_ALEN
)) {
1395 else if (c
>= 'a' && c
<= 'f')
1397 else if (c
>= 'A' && c
<= 'F')
1406 else if (c
>= 'a' && c
<= 'f')
1407 val
|= c
- 'a' + 10;
1408 else if (c
>= 'A' && c
<= 'F')
1409 val
|= c
- 'A' + 10;
1410 else if (c
== ':' || c
== 0)
1416 *data
++ = (unsigned char)(val
& 0377);
1419 if (*macaddr
== ':')
1426 static int ip_addr_add(int family
, int ifindex
, void *addr
, void *bcast
,
1427 void *acast
, int prefix
)
1430 struct ifaddrmsg
*ifa
;
1431 struct nl_handler nlh
;
1432 struct nlmsg
*answer
= NULL
, *nlmsg
= NULL
;
1434 addrlen
= family
== AF_INET
? sizeof(struct in_addr
)
1435 : sizeof(struct in6_addr
);
1437 err
= netlink_open(&nlh
, NETLINK_ROUTE
);
1442 nlmsg
= nlmsg_alloc(NLMSG_GOOD_SIZE
);
1446 answer
= nlmsg_alloc_reserve(NLMSG_GOOD_SIZE
);
1450 nlmsg
->nlmsghdr
->nlmsg_flags
=
1451 NLM_F_ACK
| NLM_F_REQUEST
| NLM_F_CREATE
| NLM_F_EXCL
;
1452 nlmsg
->nlmsghdr
->nlmsg_type
= RTM_NEWADDR
;
1454 ifa
= nlmsg_reserve(nlmsg
, sizeof(struct ifaddrmsg
));
1457 ifa
->ifa_prefixlen
= prefix
;
1458 ifa
->ifa_index
= ifindex
;
1459 ifa
->ifa_family
= family
;
1463 if (nla_put_buffer(nlmsg
, IFA_LOCAL
, addr
, addrlen
))
1466 if (nla_put_buffer(nlmsg
, IFA_ADDRESS
, addr
, addrlen
))
1469 if (nla_put_buffer(nlmsg
, IFA_BROADCAST
, bcast
, addrlen
))
1472 /* TODO: multicast, anycast with ipv6 */
1473 err
= -EPROTONOSUPPORT
;
1474 if (family
== AF_INET6
&&
1475 (memcmp(bcast
, &in6addr_any
, sizeof(in6addr_any
)) ||
1476 memcmp(acast
, &in6addr_any
, sizeof(in6addr_any
))))
1479 err
= netlink_transaction(&nlh
, nlmsg
, answer
);
1481 netlink_close(&nlh
);
1487 int lxc_ipv6_addr_add(int ifindex
, struct in6_addr
*addr
,
1488 struct in6_addr
*mcast
, struct in6_addr
*acast
,
1491 return ip_addr_add(AF_INET6
, ifindex
, addr
, mcast
, acast
, prefix
);
1494 int lxc_ipv4_addr_add(int ifindex
, struct in_addr
*addr
, struct in_addr
*bcast
,
1497 return ip_addr_add(AF_INET
, ifindex
, addr
, bcast
, NULL
, prefix
);
1500 /* Find an IFA_LOCAL (or IFA_ADDRESS if not IFA_LOCAL is present) address from
1501 * the given RTM_NEWADDR message. Allocates memory for the address and stores
1502 * that pointer in *res (so res should be an in_addr** or in6_addr**).
1504 static int ifa_get_local_ip(int family
, struct nlmsghdr
*msg
, void **res
)
1507 struct ifaddrmsg
*ifa
= NLMSG_DATA(msg
);
1508 struct rtattr
*rta
= IFA_RTA(ifa
);
1509 int attr_len
= NLMSG_PAYLOAD(msg
, sizeof(struct ifaddrmsg
));
1511 if (ifa
->ifa_family
!= family
)
1514 addrlen
= family
== AF_INET
? sizeof(struct in_addr
)
1515 : sizeof(struct in6_addr
);
1517 /* Loop over the rtattr's in this message */
1518 while (RTA_OK(rta
, attr_len
)) {
1519 /* Found a local address for the requested interface,
1522 if (rta
->rta_type
== IFA_LOCAL
||
1523 rta
->rta_type
== IFA_ADDRESS
) {
1524 /* Sanity check. The family check above should make sure
1525 * the address length is correct, but check here just in
1528 if (RTA_PAYLOAD(rta
) != addrlen
)
1531 /* We might have found an IFA_ADDRESS before, which we
1532 * now overwrite with an IFA_LOCAL.
1535 *res
= malloc(addrlen
);
1540 memcpy(*res
, RTA_DATA(rta
), addrlen
);
1541 if (rta
->rta_type
== IFA_LOCAL
)
1544 rta
= RTA_NEXT(rta
, attr_len
);
1549 static int ip_addr_get(int family
, int ifindex
, void **res
)
1551 int answer_len
, err
;
1552 struct ifaddrmsg
*ifa
;
1553 struct nl_handler nlh
;
1554 struct nlmsghdr
*msg
;
1555 int readmore
= 0, recv_len
= 0;
1556 struct nlmsg
*answer
= NULL
, *nlmsg
= NULL
;
1558 err
= netlink_open(&nlh
, NETLINK_ROUTE
);
1563 nlmsg
= nlmsg_alloc(NLMSG_GOOD_SIZE
);
1567 answer
= nlmsg_alloc_reserve(NLMSG_GOOD_SIZE
);
1571 /* Save the answer buffer length, since it will be overwritten on the
1572 * first receive (and we might need to receive more than once).
1574 answer_len
= answer
->nlmsghdr
->nlmsg_len
;
1576 nlmsg
->nlmsghdr
->nlmsg_flags
= NLM_F_REQUEST
| NLM_F_ROOT
;
1577 nlmsg
->nlmsghdr
->nlmsg_type
= RTM_GETADDR
;
1579 ifa
= nlmsg_reserve(nlmsg
, sizeof(struct ifaddrmsg
));
1582 ifa
->ifa_family
= family
;
1584 /* Send the request for addresses, which returns all addresses on all
1587 err
= netlink_send(&nlh
, nlmsg
);
1592 /* Restore the answer buffer length, it might have been
1593 * overwritten by a previous receive.
1595 answer
->nlmsghdr
->nlmsg_len
= answer_len
;
1597 /* Get the (next) batch of reply messages. */
1598 err
= netlink_rcv(&nlh
, answer
);
1605 /* Satisfy the typing for the netlink macros. */
1606 msg
= answer
->nlmsghdr
;
1608 while (NLMSG_OK(msg
, recv_len
)) {
1609 /* Stop reading if we see an error message. */
1610 if (msg
->nlmsg_type
== NLMSG_ERROR
) {
1611 struct nlmsgerr
*errmsg
=
1612 (struct nlmsgerr
*)NLMSG_DATA(msg
);
1613 err
= errmsg
->error
;
1617 /* Stop reading if we see a NLMSG_DONE message. */
1618 if (msg
->nlmsg_type
== NLMSG_DONE
) {
1623 if (msg
->nlmsg_type
!= RTM_NEWADDR
) {
1628 ifa
= (struct ifaddrmsg
*)NLMSG_DATA(msg
);
1629 if (ifa
->ifa_index
== ifindex
) {
1630 if (ifa_get_local_ip(family
, msg
, res
) < 0) {
1635 /* Found a result, stop searching. */
1640 /* Keep reading more data from the socket if the last
1641 * message had the NLF_F_MULTI flag set.
1643 readmore
= (msg
->nlmsg_flags
& NLM_F_MULTI
);
1645 /* Look at the next message received in this buffer. */
1646 msg
= NLMSG_NEXT(msg
, recv_len
);
1650 /* If we end up here, we didn't find any result, so signal an
1656 netlink_close(&nlh
);
1662 int lxc_ipv6_addr_get(int ifindex
, struct in6_addr
**res
)
1664 return ip_addr_get(AF_INET6
, ifindex
, (void **)res
);
1667 int lxc_ipv4_addr_get(int ifindex
, struct in_addr
**res
)
1669 return ip_addr_get(AF_INET
, ifindex
, (void **)res
);
1672 static int ip_gateway_add(int family
, int ifindex
, void *gw
)
1675 struct nl_handler nlh
;
1677 struct nlmsg
*answer
= NULL
, *nlmsg
= NULL
;
1679 addrlen
= family
== AF_INET
? sizeof(struct in_addr
)
1680 : sizeof(struct in6_addr
);
1682 err
= netlink_open(&nlh
, NETLINK_ROUTE
);
1687 nlmsg
= nlmsg_alloc(NLMSG_GOOD_SIZE
);
1691 answer
= nlmsg_alloc_reserve(NLMSG_GOOD_SIZE
);
1695 nlmsg
->nlmsghdr
->nlmsg_flags
=
1696 NLM_F_ACK
| NLM_F_REQUEST
| NLM_F_CREATE
| NLM_F_EXCL
;
1697 nlmsg
->nlmsghdr
->nlmsg_type
= RTM_NEWROUTE
;
1699 rt
= nlmsg_reserve(nlmsg
, sizeof(struct rtmsg
));
1702 rt
->rtm_family
= family
;
1703 rt
->rtm_table
= RT_TABLE_MAIN
;
1704 rt
->rtm_scope
= RT_SCOPE_UNIVERSE
;
1705 rt
->rtm_protocol
= RTPROT_BOOT
;
1706 rt
->rtm_type
= RTN_UNICAST
;
1707 /* "default" destination */
1708 rt
->rtm_dst_len
= 0;
1711 if (nla_put_buffer(nlmsg
, RTA_GATEWAY
, gw
, addrlen
))
1714 /* Adding the interface index enables the use of link-local
1715 * addresses for the gateway.
1717 if (nla_put_u32(nlmsg
, RTA_OIF
, ifindex
))
1720 err
= netlink_transaction(&nlh
, nlmsg
, answer
);
1722 netlink_close(&nlh
);
1728 int lxc_ipv4_gateway_add(int ifindex
, struct in_addr
*gw
)
1730 return ip_gateway_add(AF_INET
, ifindex
, gw
);
1733 int lxc_ipv6_gateway_add(int ifindex
, struct in6_addr
*gw
)
1735 return ip_gateway_add(AF_INET6
, ifindex
, gw
);
1738 static int ip_route_dest_add(int family
, int ifindex
, void *dest
)
1741 struct nl_handler nlh
;
1743 struct nlmsg
*answer
= NULL
, *nlmsg
= NULL
;
1745 addrlen
= family
== AF_INET
? sizeof(struct in_addr
)
1746 : sizeof(struct in6_addr
);
1748 err
= netlink_open(&nlh
, NETLINK_ROUTE
);
1753 nlmsg
= nlmsg_alloc(NLMSG_GOOD_SIZE
);
1757 answer
= nlmsg_alloc_reserve(NLMSG_GOOD_SIZE
);
1761 nlmsg
->nlmsghdr
->nlmsg_flags
=
1762 NLM_F_ACK
| NLM_F_REQUEST
| NLM_F_CREATE
| NLM_F_EXCL
;
1763 nlmsg
->nlmsghdr
->nlmsg_type
= RTM_NEWROUTE
;
1765 rt
= nlmsg_reserve(nlmsg
, sizeof(struct rtmsg
));
1768 rt
->rtm_family
= family
;
1769 rt
->rtm_table
= RT_TABLE_MAIN
;
1770 rt
->rtm_scope
= RT_SCOPE_LINK
;
1771 rt
->rtm_protocol
= RTPROT_BOOT
;
1772 rt
->rtm_type
= RTN_UNICAST
;
1773 rt
->rtm_dst_len
= addrlen
* 8;
1776 if (nla_put_buffer(nlmsg
, RTA_DST
, dest
, addrlen
))
1778 if (nla_put_u32(nlmsg
, RTA_OIF
, ifindex
))
1780 err
= netlink_transaction(&nlh
, nlmsg
, answer
);
1782 netlink_close(&nlh
);
1788 int lxc_ipv4_dest_add(int ifindex
, struct in_addr
*dest
)
1790 return ip_route_dest_add(AF_INET
, ifindex
, dest
);
1793 int lxc_ipv6_dest_add(int ifindex
, struct in6_addr
*dest
)
1795 return ip_route_dest_add(AF_INET6
, ifindex
, dest
);
1798 bool is_ovs_bridge(const char *bridge
)
1802 char brdirname
[22 + IFNAMSIZ
+ 1] = {0};
1804 ret
= snprintf(brdirname
, 22 + IFNAMSIZ
+ 1, "/sys/class/net/%s/bridge",
1806 if (ret
< 0 || (size_t)ret
>= 22 + IFNAMSIZ
+ 1)
1809 ret
= stat(brdirname
, &sb
);
1810 if (ret
< 0 && errno
== ENOENT
)
1816 struct ovs_veth_args
{
1821 /* Called from a background thread - when nic goes away, remove it from the
1824 static int lxc_ovs_delete_port_exec(void *data
)
1826 struct ovs_veth_args
*args
= data
;
1828 execlp("ovs-vsctl", "ovs-vsctl", "del-port", args
->bridge
, args
->nic
,
1833 int lxc_ovs_delete_port(const char *bridge
, const char *nic
)
1836 char cmd_output
[MAXPATHLEN
];
1837 struct ovs_veth_args args
;
1839 args
.bridge
= bridge
;
1841 ret
= run_command(cmd_output
, sizeof(cmd_output
),
1842 lxc_ovs_delete_port_exec
, (void *)&args
);
1844 ERROR("Failed to delete \"%s\" from openvswitch bridge \"%s\": "
1845 "%s", bridge
, nic
, cmd_output
);
1852 static int lxc_ovs_attach_bridge_exec(void *data
)
1854 struct ovs_veth_args
*args
= data
;
1856 execlp("ovs-vsctl", "ovs-vsctl", "add-port", args
->bridge
, args
->nic
,
1861 static int lxc_ovs_attach_bridge(const char *bridge
, const char *nic
)
1864 char cmd_output
[MAXPATHLEN
];
1865 struct ovs_veth_args args
;
1867 args
.bridge
= bridge
;
1869 ret
= run_command(cmd_output
, sizeof(cmd_output
),
1870 lxc_ovs_attach_bridge_exec
, (void *)&args
);
1872 ERROR("Failed to attach \"%s\" to openvswitch bridge \"%s\": %s",
1873 bridge
, nic
, cmd_output
);
1880 int lxc_bridge_attach(const char *bridge
, const char *ifname
)
1886 if (strlen(ifname
) >= IFNAMSIZ
)
1889 index
= if_nametoindex(ifname
);
1893 if (is_ovs_bridge(bridge
))
1894 return lxc_ovs_attach_bridge(bridge
, ifname
);
1896 fd
= socket(AF_INET
, SOCK_STREAM
, 0);
1900 retlen
= strlcpy(ifr
.ifr_name
, bridge
, IFNAMSIZ
);
1901 if (retlen
>= IFNAMSIZ
) {
1906 ifr
.ifr_name
[IFNAMSIZ
- 1] = '\0';
1907 ifr
.ifr_ifindex
= index
;
1908 err
= ioctl(fd
, SIOCBRADDIF
, &ifr
);
1916 static const char *const lxc_network_types
[LXC_NET_MAXCONFTYPE
+ 1] = {
1917 [LXC_NET_EMPTY
] = "empty",
1918 [LXC_NET_VETH
] = "veth",
1919 [LXC_NET_MACVLAN
] = "macvlan",
1920 [LXC_NET_PHYS
] = "phys",
1921 [LXC_NET_VLAN
] = "vlan",
1922 [LXC_NET_NONE
] = "none",
1925 const char *lxc_net_type_to_str(int type
)
1927 if (type
< 0 || type
> LXC_NET_MAXCONFTYPE
)
1930 return lxc_network_types
[type
];
1933 static const char padchar
[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
1935 char *lxc_mkifname(char *template)
1938 struct ifaddrs
*ifa
, *ifaddr
;
1939 char name
[IFNAMSIZ
];
1940 bool exists
= false;
1945 seed
= randseed(false);
1948 (void)randseed(true);
1951 if (strlen(template) >= IFNAMSIZ
)
1954 /* Get all the network interfaces. */
1955 ret
= getifaddrs(&ifaddr
);
1957 SYSERROR("Failed to get network interfaces");
1961 /* Generate random names until we find one that doesn't exist. */
1964 (void)strlcpy(name
, template, IFNAMSIZ
);
1968 for (i
= 0; i
< strlen(name
); i
++) {
1969 if (name
[i
] == 'X') {
1971 name
[i
] = padchar
[rand_r(&seed
) % (strlen(padchar
) - 1)];
1973 name
[i
] = padchar
[rand() % (strlen(padchar
) - 1)];
1978 for (ifa
= ifaddr
; ifa
!= NULL
; ifa
= ifa
->ifa_next
) {
1979 if (!strcmp(ifa
->ifa_name
, name
)) {
1989 freeifaddrs(ifaddr
);
1990 (void)strlcpy(template, name
, strlen(template) + 1);
1995 int setup_private_host_hw_addr(char *veth1
)
2000 sockfd
= socket(AF_INET
, SOCK_DGRAM
, 0);
2004 err
= snprintf((char *)ifr
.ifr_name
, IFNAMSIZ
, "%s", veth1
);
2005 if (err
< 0 || (size_t)err
>= IFNAMSIZ
) {
2010 err
= ioctl(sockfd
, SIOCGIFHWADDR
, &ifr
);
2016 ifr
.ifr_hwaddr
.sa_data
[0] = 0xfe;
2017 err
= ioctl(sockfd
, SIOCSIFHWADDR
, &ifr
);
2025 int lxc_find_gateway_addresses(struct lxc_handler
*handler
)
2027 struct lxc_list
*network
= &handler
->conf
->network
;
2028 struct lxc_list
*iterator
;
2029 struct lxc_netdev
*netdev
;
2032 lxc_list_for_each(iterator
, network
) {
2033 netdev
= iterator
->elem
;
2035 if (!netdev
->ipv4_gateway_auto
&& !netdev
->ipv6_gateway_auto
)
2038 if (netdev
->type
!= LXC_NET_VETH
&& netdev
->type
!= LXC_NET_MACVLAN
) {
2039 ERROR("Automatic gateway detection is only supported "
2040 "for veth and macvlan");
2044 if (netdev
->link
[0] == '\0') {
2045 ERROR("Automatic gateway detection needs a link interface");
2049 link_index
= if_nametoindex(netdev
->link
);
2053 if (netdev
->ipv4_gateway_auto
) {
2054 if (lxc_ipv4_addr_get(link_index
, &netdev
->ipv4_gateway
)) {
2055 ERROR("Failed to automatically find ipv4 gateway "
2056 "address from link interface \"%s\"", netdev
->link
);
2061 if (netdev
->ipv6_gateway_auto
) {
2062 if (lxc_ipv6_addr_get(link_index
, &netdev
->ipv6_gateway
)) {
2063 ERROR("Failed to automatically find ipv6 gateway "
2064 "address from link interface \"%s\"", netdev
->link
);
2073 #define LXC_USERNIC_PATH LIBEXECDIR "/lxc/lxc-user-nic"
2074 static int lxc_create_network_unpriv_exec(const char *lxcpath
, const char *lxcname
,
2075 struct lxc_netdev
*netdev
, pid_t pid
, unsigned int hooks_version
)
2079 int bytes
, pipefd
[2];
2080 char *token
, *saveptr
= NULL
;
2081 char netdev_link
[IFNAMSIZ
];
2082 char buffer
[MAXPATHLEN
] = {0};
2085 if (netdev
->type
!= LXC_NET_VETH
) {
2086 ERROR("Network type %d not support for unprivileged use", netdev
->type
);
2092 SYSERROR("Failed to create pipe");
2098 SYSERROR("Failed to create new process");
2107 char pidstr
[INTTYPE_TO_STRLEN(pid_t
)];
2111 ret
= dup2(pipefd
[1], STDOUT_FILENO
);
2113 ret
= dup2(pipefd
[1], STDERR_FILENO
);
2116 SYSERROR("Failed to duplicate std{err,out} file descriptor");
2117 _exit(EXIT_FAILURE
);
2120 if (netdev
->link
[0] != '\0')
2121 retlen
= strlcpy(netdev_link
, netdev
->link
, IFNAMSIZ
);
2123 retlen
= strlcpy(netdev_link
, "none", IFNAMSIZ
);
2124 if (retlen
>= IFNAMSIZ
) {
2125 SYSERROR("Invalid network device name");
2126 _exit(EXIT_FAILURE
);
2129 ret
= snprintf(pidstr
, sizeof(pidstr
), "%d", pid
);
2130 if (ret
< 0 || ret
>= sizeof(pidstr
))
2131 _exit(EXIT_FAILURE
);
2132 pidstr
[sizeof(pidstr
) - 1] = '\0';
2134 INFO("Execing lxc-user-nic create %s %s %s veth %s %s", lxcpath
,
2135 lxcname
, pidstr
, netdev_link
,
2136 netdev
->name
[0] != '\0' ? netdev
->name
: "(null)");
2137 if (netdev
->name
[0] != '\0')
2138 execlp(LXC_USERNIC_PATH
, LXC_USERNIC_PATH
, "create",
2139 lxcpath
, lxcname
, pidstr
, "veth", netdev_link
,
2140 netdev
->name
, (char *)NULL
);
2142 execlp(LXC_USERNIC_PATH
, LXC_USERNIC_PATH
, "create",
2143 lxcpath
, lxcname
, pidstr
, "veth", netdev_link
,
2145 SYSERROR("Failed to execute lxc-user-nic");
2146 _exit(EXIT_FAILURE
);
2149 /* close the write-end of the pipe */
2152 bytes
= lxc_read_nointr(pipefd
[0], &buffer
, MAXPATHLEN
);
2154 SYSERROR("Failed to read from pipe file descriptor");
2157 buffer
[bytes
- 1] = '\0';
2160 ret
= wait_for_pid(child
);
2162 if (ret
!= 0 || bytes
< 0) {
2163 ERROR("lxc-user-nic failed to configure requested network: %s",
2164 buffer
[0] != '\0' ? buffer
: "(null)");
2167 TRACE("Received output \"%s\" from lxc-user-nic", buffer
);
2170 token
= strtok_r(buffer
, ":", &saveptr
);
2172 ERROR("Failed to parse lxc-user-nic output");
2176 memset(netdev
->name
, 0, IFNAMSIZ
);
2177 memcpy(netdev
->name
, token
, IFNAMSIZ
- 1);
2179 /* netdev->ifindex */
2180 token
= strtok_r(NULL
, ":", &saveptr
);
2182 ERROR("Failed to parse lxc-user-nic output");
2186 ret
= lxc_safe_int(token
, &netdev
->ifindex
);
2189 SYSERROR("Failed to convert string \"%s\" to integer", token
);
2193 /* netdev->priv.veth_attr.veth1 */
2194 token
= strtok_r(NULL
, ":", &saveptr
);
2196 ERROR("Failed to parse lxc-user-nic output");
2200 retlen
= strlcpy(netdev
->priv
.veth_attr
.veth1
, token
, IFNAMSIZ
);
2201 if (retlen
>= IFNAMSIZ
) {
2202 ERROR("Host side veth device name returned by lxc-user-nic is "
2207 /* netdev->priv.veth_attr.ifindex */
2208 token
= strtok_r(NULL
, ":", &saveptr
);
2210 ERROR("Failed to parse lxc-user-nic output");
2214 ret
= lxc_safe_int(token
, &netdev
->priv
.veth_attr
.ifindex
);
2217 SYSERROR("Failed to convert string \"%s\" to integer", token
);
2221 if (netdev
->upscript
) {
2225 netdev
->priv
.veth_attr
.veth1
,
2229 ret
= run_script_argv(lxcname
,
2230 hooks_version
, "net",
2231 netdev
->upscript
, "up", argv
);
2239 static int lxc_delete_network_unpriv_exec(const char *lxcpath
, const char *lxcname
,
2240 struct lxc_netdev
*netdev
,
2241 const char *netns_path
)
2246 char buffer
[MAXPATHLEN
] = {0};
2248 if (netdev
->type
!= LXC_NET_VETH
) {
2249 ERROR("Network type %d not support for unprivileged use", netdev
->type
);
2255 SYSERROR("Failed to create pipe");
2261 SYSERROR("Failed to create new process");
2273 ret
= dup2(pipefd
[1], STDOUT_FILENO
);
2275 ret
= dup2(pipefd
[1], STDERR_FILENO
);
2278 SYSERROR("Failed to duplicate std{err,out} file descriptor");
2279 _exit(EXIT_FAILURE
);
2282 if (netdev
->priv
.veth_attr
.pair
[0] != '\0')
2283 hostveth
= netdev
->priv
.veth_attr
.pair
;
2285 hostveth
= netdev
->priv
.veth_attr
.veth1
;
2286 if (hostveth
[0] == '\0') {
2287 SYSERROR("Host side veth device name is missing");
2288 _exit(EXIT_FAILURE
);
2291 if (netdev
->link
[0] == '\0') {
2292 SYSERROR("Network link for network device \"%s\" is "
2293 "missing", netdev
->priv
.veth_attr
.veth1
);
2294 _exit(EXIT_FAILURE
);
2297 INFO("Execing lxc-user-nic delete %s %s %s veth %s %s", lxcpath
,
2298 lxcname
, netns_path
, netdev
->link
, hostveth
);
2299 execlp(LXC_USERNIC_PATH
, LXC_USERNIC_PATH
, "delete", lxcpath
,
2300 lxcname
, netns_path
, "veth", netdev
->link
, hostveth
,
2302 SYSERROR("Failed to exec lxc-user-nic.");
2303 _exit(EXIT_FAILURE
);
2308 bytes
= lxc_read_nointr(pipefd
[0], &buffer
, MAXPATHLEN
);
2310 SYSERROR("Failed to read from pipe file descriptor.");
2313 buffer
[bytes
- 1] = '\0';
2316 ret
= wait_for_pid(child
);
2318 if (ret
!= 0 || bytes
< 0) {
2319 ERROR("lxc-user-nic failed to delete requested network: %s",
2320 buffer
[0] != '\0' ? buffer
: "(null)");
2327 bool lxc_delete_network_unpriv(struct lxc_handler
*handler
)
2330 struct lxc_list
*iterator
;
2331 struct lxc_list
*network
= &handler
->conf
->network
;
2332 /* strlen("/proc/") = 6
2334 * INTTYPE_TO_STRLEN(pid_t)
2336 * strlen("/fd/") = 4
2338 * INTTYPE_TO_STRLEN(int)
2342 char netns_path
[6 + INTTYPE_TO_STRLEN(pid_t
) + 4 + INTTYPE_TO_STRLEN(int) + 1];
2346 if (handler
->nsfd
[LXC_NS_NET
] < 0) {
2347 DEBUG("Cannot not guarantee safe deletion of network devices. "
2348 "Manual cleanup maybe needed");
2352 ret
= snprintf(netns_path
, sizeof(netns_path
), "/proc/%d/fd/%d",
2353 lxc_raw_getpid(), handler
->nsfd
[LXC_NS_NET
]);
2354 if (ret
< 0 || ret
>= sizeof(netns_path
))
2357 lxc_list_for_each(iterator
, network
) {
2358 char *hostveth
= NULL
;
2359 struct lxc_netdev
*netdev
= iterator
->elem
;
2361 /* We can only delete devices whose ifindex we have. If we don't
2362 * have the index it means that we didn't create it.
2364 if (!netdev
->ifindex
)
2367 if (netdev
->type
== LXC_NET_PHYS
) {
2368 ret
= lxc_netdev_rename_by_index(netdev
->ifindex
,
2371 WARN("Failed to rename interface with index %d "
2372 "to its initial name \"%s\"",
2373 netdev
->ifindex
, netdev
->link
);
2375 TRACE("Renamed interface with index %d to its "
2376 "initial name \"%s\"",
2377 netdev
->ifindex
, netdev
->link
);
2378 goto clear_ifindices
;
2381 ret
= netdev_deconf
[netdev
->type
](handler
, netdev
);
2383 WARN("Failed to deconfigure network device");
2385 if (netdev
->type
!= LXC_NET_VETH
)
2386 goto clear_ifindices
;
2388 if (netdev
->link
[0] == '\0' || !is_ovs_bridge(netdev
->link
))
2389 goto clear_ifindices
;
2391 if (netdev
->priv
.veth_attr
.pair
[0] != '\0')
2392 hostveth
= netdev
->priv
.veth_attr
.pair
;
2394 hostveth
= netdev
->priv
.veth_attr
.veth1
;
2395 if (hostveth
[0] == '\0')
2396 goto clear_ifindices
;
2398 ret
= lxc_delete_network_unpriv_exec(handler
->lxcpath
,
2399 handler
->name
, netdev
,
2402 WARN("Failed to remove port \"%s\" from openvswitch "
2403 "bridge \"%s\"", hostveth
, netdev
->link
);
2404 goto clear_ifindices
;
2406 INFO("Removed interface \"%s\" from \"%s\"", hostveth
,
2410 /* We need to clear any ifindeces we recorded so liblxc won't
2411 * have cached stale data which would cause it to fail on reboot
2412 * we're we don't re-read the on-disk config file.
2414 netdev
->ifindex
= 0;
2415 if (netdev
->type
== LXC_NET_PHYS
) {
2416 netdev
->priv
.phys_attr
.ifindex
= 0;
2417 } else if (netdev
->type
== LXC_NET_VETH
) {
2418 netdev
->priv
.veth_attr
.veth1
[0] = '\0';
2419 netdev
->priv
.veth_attr
.ifindex
= 0;
2426 int lxc_create_network_priv(struct lxc_handler
*handler
)
2428 struct lxc_list
*iterator
;
2429 struct lxc_list
*network
= &handler
->conf
->network
;
2431 if (!handler
->am_root
)
2434 lxc_list_for_each(iterator
, network
) {
2435 struct lxc_netdev
*netdev
= iterator
->elem
;
2437 if (netdev
->type
< 0 || netdev
->type
> LXC_NET_MAXCONFTYPE
) {
2438 ERROR("Invalid network configuration type %d", netdev
->type
);
2442 if (netdev_conf
[netdev
->type
](handler
, netdev
)) {
2443 ERROR("Failed to create network device");
2452 int lxc_network_move_created_netdev_priv(const char *lxcpath
, const char *lxcname
,
2453 struct lxc_list
*network
, pid_t pid
)
2456 char ifname
[IFNAMSIZ
];
2457 struct lxc_list
*iterator
;
2459 if (am_guest_unpriv())
2462 lxc_list_for_each(iterator
, network
) {
2463 struct lxc_netdev
*netdev
= iterator
->elem
;
2465 if (!netdev
->ifindex
)
2468 /* retrieve the name of the interface */
2469 if (!if_indextoname(netdev
->ifindex
, ifname
)) {
2470 ERROR("No interface corresponding to ifindex \"%d\"",
2475 ret
= lxc_netdev_move_by_name(ifname
, pid
, NULL
);
2478 SYSERROR("Failed to move network device \"%s\" to "
2479 "network namespace %d", ifname
, pid
);
2483 DEBUG("Moved network device \"%s\"/\"%s\" to network namespace "
2485 ifname
, netdev
->name
[0] != '\0' ? netdev
->name
: "(null)",
2492 int lxc_create_network_unpriv(const char *lxcpath
, const char *lxcname
,
2493 struct lxc_list
*network
, pid_t pid
, unsigned int hooks_version
)
2495 struct lxc_list
*iterator
;
2497 if (!am_guest_unpriv())
2500 lxc_list_for_each(iterator
, network
) {
2501 struct lxc_netdev
*netdev
= iterator
->elem
;
2503 if (netdev
->type
== LXC_NET_EMPTY
)
2506 if (netdev
->type
== LXC_NET_NONE
)
2509 if (netdev
->type
!= LXC_NET_VETH
) {
2510 ERROR("Networks of type %s are not supported by "
2511 "unprivileged containers",
2512 lxc_net_type_to_str(netdev
->type
));
2517 INFO("mtu ignored due to insufficient privilege");
2519 if (lxc_create_network_unpriv_exec(lxcpath
, lxcname
, netdev
, pid
, hooks_version
))
2526 bool lxc_delete_network_priv(struct lxc_handler
*handler
)
2529 struct lxc_list
*iterator
;
2530 struct lxc_list
*network
= &handler
->conf
->network
;
2532 lxc_list_for_each(iterator
, network
) {
2533 char *hostveth
= NULL
;
2534 struct lxc_netdev
*netdev
= iterator
->elem
;
2536 /* We can only delete devices whose ifindex we have. If we don't
2537 * have the index it means that we didn't create it.
2539 if (!netdev
->ifindex
)
2542 if (netdev
->type
== LXC_NET_PHYS
) {
2543 ret
= lxc_netdev_rename_by_index(netdev
->ifindex
, netdev
->link
);
2545 WARN("Failed to rename interface with index %d "
2546 "from \"%s\" to its initial name \"%s\"",
2547 netdev
->ifindex
, netdev
->name
, netdev
->link
);
2549 TRACE("Renamed interface with index %d from "
2550 "\"%s\" to its initial name \"%s\"",
2551 netdev
->ifindex
, netdev
->name
,
2553 goto clear_ifindices
;
2556 ret
= netdev_deconf
[netdev
->type
](handler
, netdev
);
2558 WARN("Failed to deconfigure network device");
2560 /* Recent kernels remove the virtual interfaces when the network
2561 * namespace is destroyed but in case we did not move the
2562 * interface to the network namespace, we have to destroy it.
2564 ret
= lxc_netdev_delete_by_index(netdev
->ifindex
);
2565 if (-ret
== ENODEV
) {
2566 INFO("Interface \"%s\" with index %d already "
2567 "deleted or existing in different network "
2569 netdev
->name
[0] != '\0' ? netdev
->name
: "(null)",
2571 } else if (ret
< 0) {
2573 SYSWARN("Failed to remove interface \"%s\" with index %d",
2574 netdev
->name
[0] != '\0' ? netdev
->name
: "(null)",
2576 goto clear_ifindices
;
2578 INFO("Removed interface \"%s\" with index %d",
2579 netdev
->name
[0] != '\0' ? netdev
->name
: "(null)",
2582 if (netdev
->type
!= LXC_NET_VETH
)
2583 goto clear_ifindices
;
2585 /* Explicitly delete host veth device to prevent lingering
2586 * devices. We had issues in LXD around this.
2588 if (netdev
->priv
.veth_attr
.pair
[0] != '\0')
2589 hostveth
= netdev
->priv
.veth_attr
.pair
;
2591 hostveth
= netdev
->priv
.veth_attr
.veth1
;
2592 if (hostveth
[0] == '\0')
2593 goto clear_ifindices
;
2595 ret
= lxc_netdev_delete_by_name(hostveth
);
2598 SYSWARN("Failed to remove interface \"%s\" from \"%s\"",
2599 hostveth
, netdev
->link
);
2600 goto clear_ifindices
;
2602 INFO("Removed interface \"%s\" from \"%s\"", hostveth
, netdev
->link
);
2604 if (netdev
->link
[0] == '\0' || !is_ovs_bridge(netdev
->link
)) {
2605 netdev
->priv
.veth_attr
.veth1
[0] = '\0';
2606 netdev
->ifindex
= 0;
2607 netdev
->priv
.veth_attr
.ifindex
= 0;
2608 goto clear_ifindices
;
2611 /* Delete the openvswitch port. */
2612 ret
= lxc_ovs_delete_port(netdev
->link
, hostveth
);
2614 WARN("Failed to remove port \"%s\" from openvswitch "
2615 "bridge \"%s\"", hostveth
, netdev
->link
);
2617 INFO("Removed port \"%s\" from openvswitch bridge \"%s\"",
2618 hostveth
, netdev
->link
);
2621 /* We need to clear any ifindeces we recorded so liblxc won't
2622 * have cached stale data which would cause it to fail on reboot
2623 * we're we don't re-read the on-disk config file.
2625 netdev
->ifindex
= 0;
2626 if (netdev
->type
== LXC_NET_PHYS
) {
2627 netdev
->priv
.phys_attr
.ifindex
= 0;
2628 } else if (netdev
->type
== LXC_NET_VETH
) {
2629 netdev
->priv
.veth_attr
.veth1
[0] = '\0';
2630 netdev
->priv
.veth_attr
.ifindex
= 0;
2637 int lxc_requests_empty_network(struct lxc_handler
*handler
)
2639 struct lxc_list
*network
= &handler
->conf
->network
;
2640 struct lxc_list
*iterator
;
2641 bool found_none
= false, found_nic
= false;
2643 if (lxc_list_empty(network
))
2646 lxc_list_for_each(iterator
, network
) {
2647 struct lxc_netdev
*netdev
= iterator
->elem
;
2649 if (netdev
->type
== LXC_NET_NONE
)
2654 if (found_none
&& !found_nic
)
2659 /* try to move physical nics to the init netns */
2660 int lxc_restore_phys_nics_to_netns(struct lxc_handler
*handler
)
2664 char ifname
[IFNAMSIZ
];
2665 struct lxc_list
*iterator
;
2666 int netnsfd
= handler
->nsfd
[LXC_NS_NET
];
2667 struct lxc_conf
*conf
= handler
->conf
;
2669 /* We need CAP_NET_ADMIN in the parent namespace in order to setns() to
2670 * the parent network namespace. We won't have this capability if we are
2673 if (!handler
->am_root
)
2676 TRACE("Moving physical network devices back to parent network namespace");
2678 oldfd
= lxc_preserve_ns(lxc_raw_getpid(), "net");
2680 SYSERROR("Failed to preserve network namespace");
2684 ret
= setns(netnsfd
, CLONE_NEWNET
);
2686 SYSERROR("Failed to enter network namespace");
2691 lxc_list_for_each(iterator
, &conf
->network
) {
2692 struct lxc_netdev
*netdev
= iterator
->elem
;
2694 if (netdev
->type
!= LXC_NET_PHYS
)
2697 /* Retrieve the name of the interface in the container's network
2700 if (!if_indextoname(netdev
->ifindex
, ifname
)) {
2701 WARN("No interface corresponding to ifindex %d",
2706 ret
= lxc_netdev_move_by_name(ifname
, 1, netdev
->link
);
2708 WARN("Error moving network device \"%s\" back to "
2709 "network namespace", ifname
);
2711 TRACE("Moved network device \"%s\" back to network "
2712 "namespace", ifname
);
2715 ret
= setns(oldfd
, CLONE_NEWNET
);
2718 SYSERROR("Failed to enter network namespace");
2725 static int setup_hw_addr(char *hwaddr
, const char *ifname
)
2727 struct sockaddr sockaddr
;
2731 ret
= lxc_convert_mac(hwaddr
, &sockaddr
);
2734 SYSERROR("Mac address \"%s\" conversion failed", hwaddr
);
2738 memcpy(ifr
.ifr_name
, ifname
, IFNAMSIZ
);
2739 ifr
.ifr_name
[IFNAMSIZ
-1] = '\0';
2740 memcpy((char *) &ifr
.ifr_hwaddr
, (char *) &sockaddr
, sizeof(sockaddr
));
2742 fd
= socket(AF_INET
, SOCK_DGRAM
, 0);
2746 ret
= ioctl(fd
, SIOCSIFHWADDR
, &ifr
);
2748 SYSERROR("Failed to perform ioctl");
2752 DEBUG("Mac address \"%s\" on \"%s\" has been setup", hwaddr
,
2758 static int setup_ipv4_addr(struct lxc_list
*ip
, int ifindex
)
2760 struct lxc_list
*iterator
;
2763 lxc_list_for_each(iterator
, ip
) {
2764 struct lxc_inetdev
*inetdev
= iterator
->elem
;
2766 err
= lxc_ipv4_addr_add(ifindex
, &inetdev
->addr
,
2767 &inetdev
->bcast
, inetdev
->prefix
);
2770 SYSERROR("Failed to setup ipv4 address for network device "
2771 "with eifindex %d", ifindex
);
2779 static int setup_ipv6_addr(struct lxc_list
*ip
, int ifindex
)
2781 struct lxc_list
*iterator
;
2784 lxc_list_for_each(iterator
, ip
) {
2785 struct lxc_inet6dev
*inet6dev
= iterator
->elem
;
2787 err
= lxc_ipv6_addr_add(ifindex
, &inet6dev
->addr
,
2788 &inet6dev
->mcast
, &inet6dev
->acast
,
2792 SYSERROR("Failed to setup ipv6 address for network device "
2793 "with eifindex %d", ifindex
);
2801 static int lxc_setup_netdev_in_child_namespaces(struct lxc_netdev
*netdev
)
2803 char ifname
[IFNAMSIZ
];
2805 const char *net_type_name
;
2806 char *current_ifname
= ifname
;
2808 /* empty network namespace */
2809 if (!netdev
->ifindex
) {
2810 if (netdev
->flags
& IFF_UP
) {
2811 err
= lxc_netdev_up("lo");
2814 SYSERROR("Failed to set the loopback network device up");
2819 if (netdev
->type
== LXC_NET_EMPTY
)
2822 if (netdev
->type
== LXC_NET_NONE
)
2825 if (netdev
->type
!= LXC_NET_VETH
) {
2826 net_type_name
= lxc_net_type_to_str(netdev
->type
);
2827 ERROR("%s networks are not supported for containers "
2828 "not setup up by privileged users", net_type_name
);
2832 netdev
->ifindex
= if_nametoindex(netdev
->name
);
2835 /* get the new ifindex in case of physical netdev */
2836 if (netdev
->type
== LXC_NET_PHYS
) {
2837 netdev
->ifindex
= if_nametoindex(netdev
->link
);
2838 if (!netdev
->ifindex
) {
2839 ERROR("Failed to get ifindex for network device \"%s\"",
2845 /* retrieve the name of the interface */
2846 if (!if_indextoname(netdev
->ifindex
, current_ifname
)) {
2847 ERROR("Failed get name for network device with ifindex %d",
2852 /* Default: let the system to choose one interface name.
2853 * When the IFLA_IFNAME attribute is passed something like "<prefix>%d"
2854 * netlink will replace the format specifier with an appropriate index.
2856 if (netdev
->name
[0] == '\0') {
2857 if (netdev
->type
== LXC_NET_PHYS
)
2858 (void)strlcpy(netdev
->name
, netdev
->link
, IFNAMSIZ
);
2860 (void)strlcpy(netdev
->name
, "eth%d", IFNAMSIZ
);
2863 /* rename the interface name */
2864 if (strcmp(ifname
, netdev
->name
) != 0) {
2865 err
= lxc_netdev_rename_by_name(ifname
, netdev
->name
);
2868 SYSERROR("Failed to rename network device \"%s\" to \"%s\"",
2869 ifname
, netdev
->name
);
2874 /* Re-read the name of the interface because its name has changed
2875 * and would be automatically allocated by the system
2877 if (!if_indextoname(netdev
->ifindex
, current_ifname
)) {
2878 ERROR("Failed get name for network device with ifindex %d",
2883 /* Now update the recorded name of the network device to reflect the
2884 * name of the network device in the child's network namespace. We will
2885 * later on send this information back to the parent.
2887 (void)strlcpy(netdev
->name
, current_ifname
, IFNAMSIZ
);
2889 /* set a mac address */
2890 if (netdev
->hwaddr
) {
2891 if (setup_hw_addr(netdev
->hwaddr
, current_ifname
)) {
2892 ERROR("Failed to setup hw address for network device \"%s\"",
2898 /* setup ipv4 addresses on the interface */
2899 if (setup_ipv4_addr(&netdev
->ipv4
, netdev
->ifindex
)) {
2900 ERROR("Failed to setup ip addresses for network device \"%s\"",
2905 /* setup ipv6 addresses on the interface */
2906 if (setup_ipv6_addr(&netdev
->ipv6
, netdev
->ifindex
)) {
2907 ERROR("Failed to setup ipv6 addresses for network device \"%s\"",
2912 /* set the network device up */
2913 if (netdev
->flags
& IFF_UP
) {
2916 err
= lxc_netdev_up(current_ifname
);
2919 SYSERROR("Failed to set network device \"%s\" up",
2924 /* the network is up, make the loopback up too */
2925 err
= lxc_netdev_up("lo");
2928 SYSERROR("Failed to set the loopback network device up");
2933 /* We can only set up the default routes after bringing
2934 * up the interface, sine bringing up the interface adds
2935 * the link-local routes and we can't add a default
2936 * route if the gateway is not reachable. */
2938 /* setup ipv4 gateway on the interface */
2939 if (netdev
->ipv4_gateway
) {
2940 if (!(netdev
->flags
& IFF_UP
)) {
2941 ERROR("Cannot add ipv4 gateway for network device "
2942 "\"%s\" when not bringing up the interface", ifname
);
2946 if (lxc_list_empty(&netdev
->ipv4
)) {
2947 ERROR("Cannot add ipv4 gateway for network device "
2948 "\"%s\" when not assigning an address", ifname
);
2952 err
= lxc_ipv4_gateway_add(netdev
->ifindex
, netdev
->ipv4_gateway
);
2954 err
= lxc_ipv4_dest_add(netdev
->ifindex
, netdev
->ipv4_gateway
);
2957 SYSERROR("Failed to add ipv4 dest for network device \"%s\"",
2961 err
= lxc_ipv4_gateway_add(netdev
->ifindex
, netdev
->ipv4_gateway
);
2964 SYSERROR("Failed to setup ipv4 gateway for network device \"%s\"",
2967 if (netdev
->ipv4_gateway_auto
) {
2968 char buf
[INET_ADDRSTRLEN
];
2969 inet_ntop(AF_INET
, netdev
->ipv4_gateway
, buf
, sizeof(buf
));
2970 ERROR("Fried to set autodetected ipv4 gateway \"%s\"", buf
);
2977 /* setup ipv6 gateway on the interface */
2978 if (netdev
->ipv6_gateway
) {
2979 if (!(netdev
->flags
& IFF_UP
)) {
2980 ERROR("Cannot add ipv6 gateway for network device "
2981 "\"%s\" when not bringing up the interface", ifname
);
2985 if (lxc_list_empty(&netdev
->ipv6
) && !IN6_IS_ADDR_LINKLOCAL(netdev
->ipv6_gateway
)) {
2986 ERROR("Cannot add ipv6 gateway for network device "
2987 "\"%s\" when not assigning an address", ifname
);
2991 err
= lxc_ipv6_gateway_add(netdev
->ifindex
, netdev
->ipv6_gateway
);
2993 err
= lxc_ipv6_dest_add(netdev
->ifindex
, netdev
->ipv6_gateway
);
2996 SYSERROR("Failed to add ipv6 dest for network device \"%s\"",
3000 err
= lxc_ipv6_gateway_add(netdev
->ifindex
, netdev
->ipv6_gateway
);
3003 SYSERROR("Failed to setup ipv6 gateway for network device \"%s\"",
3006 if (netdev
->ipv6_gateway_auto
) {
3007 char buf
[INET6_ADDRSTRLEN
];
3008 inet_ntop(AF_INET6
, netdev
->ipv6_gateway
, buf
, sizeof(buf
));
3009 ERROR("Tried to set autodetected ipv6 "
3010 "gateway for network device "
3018 DEBUG("Network device \"%s\" has been setup", current_ifname
);
3023 int lxc_setup_network_in_child_namespaces(const struct lxc_conf
*conf
,
3024 struct lxc_list
*network
)
3026 struct lxc_list
*iterator
;
3027 struct lxc_netdev
*netdev
;
3029 lxc_list_for_each(iterator
, network
) {
3030 netdev
= iterator
->elem
;
3032 if (lxc_setup_netdev_in_child_namespaces(netdev
)) {
3033 ERROR("failed to setup netdev");
3038 if (!lxc_list_empty(network
))
3039 INFO("network has been setup");
3044 int lxc_network_send_veth_names_to_child(struct lxc_handler
*handler
)
3046 struct lxc_list
*iterator
;
3047 struct lxc_list
*network
= &handler
->conf
->network
;
3048 int data_sock
= handler
->data_sock
[0];
3050 if (handler
->am_root
)
3053 lxc_list_for_each(iterator
, network
) {
3055 struct lxc_netdev
*netdev
= iterator
->elem
;
3057 if (netdev
->type
!= LXC_NET_VETH
)
3060 ret
= lxc_send_nointr(data_sock
, netdev
->name
, IFNAMSIZ
, MSG_NOSIGNAL
);
3063 TRACE("Sent network device name \"%s\" to child", netdev
->name
);
3069 int lxc_network_recv_veth_names_from_parent(struct lxc_handler
*handler
)
3071 struct lxc_list
*iterator
;
3072 struct lxc_list
*network
= &handler
->conf
->network
;
3073 int data_sock
= handler
->data_sock
[1];
3075 if (handler
->am_root
)
3078 lxc_list_for_each(iterator
, network
) {
3080 struct lxc_netdev
*netdev
= iterator
->elem
;
3082 if (netdev
->type
!= LXC_NET_VETH
)
3085 ret
= lxc_recv_nointr(data_sock
, netdev
->name
, IFNAMSIZ
, 0);
3088 TRACE("Received network device name \"%s\" from parent", netdev
->name
);
3094 int lxc_network_send_name_and_ifindex_to_parent(struct lxc_handler
*handler
)
3096 struct lxc_list
*iterator
, *network
;
3097 int data_sock
= handler
->data_sock
[0];
3099 if (!handler
->am_root
)
3102 network
= &handler
->conf
->network
;
3103 lxc_list_for_each(iterator
, network
) {
3105 struct lxc_netdev
*netdev
= iterator
->elem
;
3107 /* Send network device name in the child's namespace to parent. */
3108 ret
= lxc_send_nointr(data_sock
, netdev
->name
, IFNAMSIZ
, MSG_NOSIGNAL
);
3112 /* Send network device ifindex in the child's namespace to
3115 ret
= lxc_send_nointr(data_sock
, &netdev
->ifindex
, sizeof(netdev
->ifindex
), MSG_NOSIGNAL
);
3120 TRACE("Sent network device names and ifindeces to parent");
3124 int lxc_network_recv_name_and_ifindex_from_child(struct lxc_handler
*handler
)
3126 struct lxc_list
*iterator
, *network
;
3127 int data_sock
= handler
->data_sock
[1];
3129 if (!handler
->am_root
)
3132 network
= &handler
->conf
->network
;
3133 lxc_list_for_each(iterator
, network
) {
3135 struct lxc_netdev
*netdev
= iterator
->elem
;
3137 /* Receive network device name in the child's namespace to
3140 ret
= lxc_recv_nointr(data_sock
, netdev
->name
, IFNAMSIZ
, 0);
3144 /* Receive network device ifindex in the child's namespace to
3147 ret
= lxc_recv_nointr(data_sock
, &netdev
->ifindex
, sizeof(netdev
->ifindex
), 0);
3155 void lxc_delete_network(struct lxc_handler
*handler
)
3159 if (handler
->am_root
)
3160 bret
= lxc_delete_network_priv(handler
);
3162 bret
= lxc_delete_network_unpriv(handler
);
3164 DEBUG("Failed to delete network devices");
3166 DEBUG("Deleted network devices");
3169 int addattr(struct nlmsghdr
*n
, size_t maxlen
, int type
, const void *data
, size_t alen
)
3171 int len
= RTA_LENGTH(alen
);
3174 if (NLMSG_ALIGN(n
->nlmsg_len
) + RTA_ALIGN(len
) > maxlen
)
3177 rta
= NLMSG_TAIL(n
);
3178 rta
->rta_type
= type
;
3181 memcpy(RTA_DATA(rta
), data
, alen
);
3182 n
->nlmsg_len
= NLMSG_ALIGN(n
->nlmsg_len
) + RTA_ALIGN(len
);
3187 /* Attributes of RTM_NEWNSID/RTM_GETNSID messages */
3190 #define __LXC_NETNSA_NSID_NOT_ASSIGNED -1
3197 int lxc_netns_set_nsid(int fd
)
3200 char buf
[NLMSG_ALIGN(sizeof(struct nlmsghdr
)) +
3201 NLMSG_ALIGN(sizeof(struct rtgenmsg
)) +
3203 struct nl_handler nlh
;
3204 struct nlmsghdr
*hdr
;
3205 struct rtgenmsg
*msg
;
3207 __u32 netns_fd
= fd
;
3209 ret
= netlink_open(&nlh
, NETLINK_ROUTE
);
3213 memset(buf
, 0, sizeof(buf
));
3214 hdr
= (struct nlmsghdr
*)buf
;
3215 msg
= (struct rtgenmsg
*)NLMSG_DATA(hdr
);
3217 hdr
->nlmsg_len
= NLMSG_LENGTH(sizeof(*msg
));
3218 hdr
->nlmsg_type
= RTM_NEWNSID
;
3219 hdr
->nlmsg_flags
= NLM_F_REQUEST
| NLM_F_ACK
;
3221 hdr
->nlmsg_seq
= RTM_NEWNSID
;
3222 msg
->rtgen_family
= AF_UNSPEC
;
3224 addattr(hdr
, 1024, __LXC_NETNSA_FD
, &netns_fd
, sizeof(netns_fd
));
3225 addattr(hdr
, 1024, __LXC_NETNSA_NSID
, &ns_id
, sizeof(ns_id
));
3227 ret
= __netlink_transaction(&nlh
, hdr
, hdr
);
3228 netlink_close(&nlh
);