]> git.proxmox.com Git - mirror_lxc.git/blob - src/lxc/network.c
38c7a75ba3218a0dffccb9f657b3d1752ccd6d19
[mirror_lxc.git] / src / lxc / network.c
1 /*
2 * lxc: linux Container library
3 *
4 * (C) Copyright IBM Corp. 2007, 2008
5 *
6 * Authors:
7 * Daniel Lezcano <daniel.lezcano at free.fr>
8 *
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.
13 *
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.
18 *
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
22 */
23
24 #ifndef _GNU_SOURCE
25 #define _GNU_SOURCE 1
26 #endif
27 #include <arpa/inet.h>
28 #include <ctype.h>
29 #include <errno.h>
30 #include <fcntl.h>
31 #include <linux/netlink.h>
32 #include <linux/rtnetlink.h>
33 #include <linux/sockios.h>
34 #include <net/ethernet.h>
35 #include <net/if.h>
36 #include <net/if_arp.h>
37 #include <netinet/in.h>
38 #include <stdio.h>
39 #include <stdlib.h>
40 #include <string.h>
41 #include <sys/inotify.h>
42 #include <sys/ioctl.h>
43 #include <sys/param.h>
44 #include <sys/socket.h>
45 #include <sys/stat.h>
46 #include <sys/types.h>
47 #include <time.h>
48 #include <unistd.h>
49
50 #include "../include/netns_ifaddrs.h"
51 #include "af_unix.h"
52 #include "conf.h"
53 #include "config.h"
54 #include "file_utils.h"
55 #include "log.h"
56 #include "macro.h"
57 #include "memory_utils.h"
58 #include "network.h"
59 #include "nl.h"
60 #include "raw_syscalls.h"
61 #include "syscall_wrappers.h"
62 #include "utils.h"
63
64 #ifndef HAVE_STRLCPY
65 #include "include/strlcpy.h"
66 #endif
67
68 lxc_log_define(network, lxc);
69
70 typedef int (*instantiate_cb)(struct lxc_handler *, struct lxc_netdev *);
71 static const char loop_device[] = "lo";
72
73 static int lxc_ip_route_dest(__u16 nlmsg_type, int family, int ifindex, void *dest, unsigned int netmask)
74 {
75 int addrlen, err;
76 struct nl_handler nlh;
77 struct rtmsg *rt;
78 struct nlmsg *answer = NULL, *nlmsg = NULL;
79
80 addrlen = family == AF_INET ? sizeof(struct in_addr)
81 : sizeof(struct in6_addr);
82
83 err = netlink_open(&nlh, NETLINK_ROUTE);
84 if (err)
85 return err;
86
87 err = -ENOMEM;
88 nlmsg = nlmsg_alloc(NLMSG_GOOD_SIZE);
89 if (!nlmsg)
90 goto out;
91
92 answer = nlmsg_alloc_reserve(NLMSG_GOOD_SIZE);
93 if (!answer)
94 goto out;
95
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;
99
100 rt = nlmsg_reserve(nlmsg, sizeof(struct rtmsg));
101 if (!rt)
102 goto out;
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;
109
110 err = -EINVAL;
111 if (nla_put_buffer(nlmsg, RTA_DST, dest, addrlen))
112 goto out;
113 if (nla_put_u32(nlmsg, RTA_OIF, ifindex))
114 goto out;
115 err = netlink_transaction(&nlh, nlmsg, answer);
116 out:
117 netlink_close(&nlh);
118 nlmsg_free(answer);
119 nlmsg_free(nlmsg);
120 return err;
121 }
122
123 static int lxc_ipv4_dest_add(int ifindex, struct in_addr *dest, unsigned int netmask)
124 {
125 return lxc_ip_route_dest(RTM_NEWROUTE, AF_INET, ifindex, dest, netmask);
126 }
127
128 static int lxc_ipv6_dest_add(int ifindex, struct in6_addr *dest, unsigned int netmask)
129 {
130 return lxc_ip_route_dest(RTM_NEWROUTE, AF_INET6, ifindex, dest, netmask);
131 }
132
133 static int lxc_ipv4_dest_del(int ifindex, struct in_addr *dest, unsigned int netmask)
134 {
135 return lxc_ip_route_dest(RTM_DELROUTE, AF_INET, ifindex, dest, netmask);
136 }
137
138 static int lxc_ipv6_dest_del(int ifindex, struct in6_addr *dest, unsigned int netmask)
139 {
140 return lxc_ip_route_dest(RTM_DELROUTE, AF_INET6, ifindex, dest, netmask);
141 }
142
143 static int lxc_setup_ipv4_routes(struct lxc_list *ip, int ifindex)
144 {
145 struct lxc_list *iterator;
146 int err;
147
148 lxc_list_for_each(iterator, ip) {
149 struct lxc_inetdev *inetdev = iterator->elem;
150
151 err = lxc_ipv4_dest_add(ifindex, &inetdev->addr, inetdev->prefix);
152 if (err) {
153 SYSERROR("Failed to setup ipv4 route for network device "
154 "with ifindex %d", ifindex);
155 return minus_one_set_errno(-err);
156 }
157 }
158
159 return 0;
160 }
161
162 static int lxc_setup_ipv6_routes(struct lxc_list *ip, int ifindex)
163 {
164 struct lxc_list *iterator;
165 int err;
166
167 lxc_list_for_each(iterator, ip) {
168 struct lxc_inet6dev *inet6dev = iterator->elem;
169
170 err = lxc_ipv6_dest_add(ifindex, &inet6dev->addr, inet6dev->prefix);
171 if (err) {
172 SYSERROR("Failed to setup ipv6 route for network device "
173 "with ifindex %d", ifindex);
174 return minus_one_set_errno(-err);
175 }
176 }
177
178 return 0;
179 }
180
181 static int instantiate_veth(struct lxc_handler *handler, struct lxc_netdev *netdev)
182 {
183 int bridge_index, err;
184 char *veth1, *veth2;
185 char veth1buf[IFNAMSIZ], veth2buf[IFNAMSIZ];
186 unsigned int mtu = 0;
187
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);
192 } else {
193 err = snprintf(veth1buf, sizeof(veth1buf), "vethXXXXXX");
194 if (err < 0 || (size_t)err >= sizeof(veth1buf))
195 return -1;
196
197 veth1 = lxc_mkifname(veth1buf);
198 if (!veth1)
199 return -1;
200
201 /* store away for deconf */
202 memcpy(netdev->priv.veth_attr.veth1, veth1, IFNAMSIZ);
203 }
204
205 err = snprintf(veth2buf, sizeof(veth2buf), "vethXXXXXX");
206 if (err < 0 || (size_t)err >= sizeof(veth2buf))
207 return -1;
208
209 veth2 = lxc_mkifname(veth2buf);
210 if (!veth2)
211 goto out_delete;
212
213 err = lxc_veth_create(veth1, veth2);
214 if (err) {
215 errno = -err;
216 SYSERROR("Failed to create veth pair \"%s\" and \"%s\"", veth1, veth2);
217 goto out_delete;
218 }
219
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
222 * of a container */
223 err = setup_private_host_hw_addr(veth1);
224 if (err) {
225 errno = -err;
226 SYSERROR("Failed to change mac address of host interface \"%s\"", veth1);
227 goto out_delete;
228 }
229
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);
234 goto out_delete;
235 }
236
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
240 * on.
241 */
242 netdev->ifindex = if_nametoindex(veth2);
243 if (!netdev->ifindex) {
244 ERROR("Failed to retrieve ifindex for \"%s\"", veth2);
245 goto out_delete;
246 }
247
248 if (netdev->mtu) {
249 if (lxc_safe_uint(netdev->mtu, &mtu) < 0)
250 WARN("Failed to parse mtu");
251 else
252 INFO("Retrieved mtu %d", mtu);
253 } else if (netdev->link[0] != '\0') {
254 bridge_index = if_nametoindex(netdev->link);
255 if (bridge_index) {
256 mtu = netdev_get_mtu(bridge_index);
257 INFO("Retrieved mtu %d from %s", mtu, netdev->link);
258 } else {
259 mtu = netdev_get_mtu(netdev->ifindex);
260 INFO("Retrieved mtu %d from %s", mtu, veth2);
261 }
262 }
263
264 if (mtu) {
265 err = lxc_netdev_set_mtu(veth1, mtu);
266 if (!err)
267 err = lxc_netdev_set_mtu(veth2, mtu);
268
269 if (err) {
270 errno = -err;
271 SYSERROR("Failed to set mtu \"%d\" for veth pair \"%s\" "
272 "and \"%s\"", mtu, veth1, veth2);
273 goto out_delete;
274 }
275 }
276
277 if (netdev->link[0] != '\0') {
278 err = lxc_bridge_attach(netdev->link, veth1);
279 if (err) {
280 errno = -err;
281 SYSERROR("Failed to attach \"%s\" to bridge \"%s\"",
282 veth1, netdev->link);
283 goto out_delete;
284 }
285 INFO("Attached \"%s\" to bridge \"%s\"", veth1, netdev->link);
286 }
287
288 err = lxc_netdev_up(veth1);
289 if (err) {
290 errno = -err;
291 SYSERROR("Failed to set \"%s\" up", veth1);
292 goto out_delete;
293 }
294
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);
298 goto out_delete;
299 }
300
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);
304 goto out_delete;
305 }
306
307 if (netdev->upscript) {
308 char *argv[] = {
309 "veth",
310 netdev->link,
311 veth1,
312 NULL,
313 };
314
315 err = run_script_argv(handler->name,
316 handler->conf->hooks_version, "net",
317 netdev->upscript, "up", argv);
318 if (err < 0)
319 goto out_delete;
320 }
321
322 DEBUG("Instantiated veth \"%s/%s\", index is \"%d\"", veth1, veth2,
323 netdev->ifindex);
324
325 return 0;
326
327 out_delete:
328 if (netdev->ifindex != 0)
329 lxc_netdev_delete_by_name(veth1);
330 return -1;
331 }
332
333 static int instantiate_macvlan(struct lxc_handler *handler, struct lxc_netdev *netdev)
334 {
335 char peer[IFNAMSIZ];
336 int err;
337 unsigned int mtu = 0;
338
339 if (netdev->link[0] == '\0') {
340 ERROR("No link for macvlan network device specified");
341 return -1;
342 }
343
344 err = snprintf(peer, sizeof(peer), "mcXXXXXX");
345 if (err < 0 || (size_t)err >= sizeof(peer))
346 return -1;
347
348 if (!lxc_mkifname(peer))
349 return -1;
350
351 err = lxc_macvlan_create(netdev->link, peer,
352 netdev->priv.macvlan_attr.mode);
353 if (err) {
354 errno = -err;
355 SYSERROR("Failed to create macvlan interface \"%s\" on \"%s\"",
356 peer, netdev->link);
357 goto on_error;
358 }
359
360 strlcpy(netdev->created_name, peer, IFNAMSIZ);
361
362 netdev->ifindex = if_nametoindex(peer);
363 if (!netdev->ifindex) {
364 ERROR("Failed to retrieve ifindex for \"%s\"", peer);
365 goto on_error;
366 }
367
368 if (netdev->mtu) {
369 err = lxc_safe_uint(netdev->mtu, &mtu);
370 if (err < 0) {
371 errno = -err;
372 SYSERROR("Failed to parse mtu \"%s\" for interface \"%s\"", netdev->mtu, peer);
373 goto on_error;
374 }
375
376 err = lxc_netdev_set_mtu(peer, mtu);
377 if (err < 0) {
378 errno = -err;
379 SYSERROR("Failed to set mtu \"%s\" for interface \"%s\"", netdev->mtu, peer);
380 goto on_error;
381 }
382 }
383
384 if (netdev->upscript) {
385 char *argv[] = {
386 "macvlan",
387 netdev->link,
388 NULL,
389 };
390
391 err = run_script_argv(handler->name,
392 handler->conf->hooks_version, "net",
393 netdev->upscript, "up", argv);
394 if (err < 0)
395 goto on_error;
396 }
397
398 DEBUG("Instantiated macvlan \"%s\" with ifindex is %d and mode %d",
399 peer, netdev->ifindex, netdev->priv.macvlan_attr.mode);
400
401 return 0;
402
403 on_error:
404 lxc_netdev_delete_by_name(peer);
405 return -1;
406 }
407
408 static int lxc_ipvlan_create(const char *master, const char *name, int mode, int isolation)
409 {
410 int err, index, len;
411 struct ifinfomsg *ifi;
412 struct nl_handler nlh;
413 struct rtattr *nest, *nest2;
414 struct nlmsg *answer = NULL, *nlmsg = NULL;
415
416 len = strlen(master);
417 if (len == 1 || len >= IFNAMSIZ)
418 return minus_one_set_errno(EINVAL);
419
420 len = strlen(name);
421 if (len == 1 || len >= IFNAMSIZ)
422 return minus_one_set_errno(EINVAL);
423
424 index = if_nametoindex(master);
425 if (!index)
426 return minus_one_set_errno(EINVAL);
427
428 err = netlink_open(&nlh, NETLINK_ROUTE);
429 if (err)
430 return minus_one_set_errno(-err);
431
432 err = -ENOMEM;
433 nlmsg = nlmsg_alloc(NLMSG_GOOD_SIZE);
434 if (!nlmsg)
435 goto out;
436
437 answer = nlmsg_alloc_reserve(NLMSG_GOOD_SIZE);
438 if (!answer)
439 goto out;
440
441 nlmsg->nlmsghdr->nlmsg_flags = NLM_F_REQUEST | NLM_F_CREATE | NLM_F_EXCL | NLM_F_ACK;
442 nlmsg->nlmsghdr->nlmsg_type = RTM_NEWLINK;
443
444 ifi = nlmsg_reserve(nlmsg, sizeof(struct ifinfomsg));
445 if (!ifi) {
446 goto out;
447 }
448 ifi->ifi_family = AF_UNSPEC;
449
450 err = -EPROTO;
451 nest = nla_begin_nested(nlmsg, IFLA_LINKINFO);
452 if (!nest)
453 goto out;
454
455 if (nla_put_string(nlmsg, IFLA_INFO_KIND, "ipvlan"))
456 goto out;
457
458 if (mode) {
459 nest2 = nla_begin_nested(nlmsg, IFLA_INFO_DATA);
460 if (!nest2)
461 goto out;
462
463 if (nla_put_u32(nlmsg, IFLA_IPVLAN_MODE, mode))
464 goto out;
465
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.
468 */
469 if (isolation > 0) {
470 if (nla_put_u16(nlmsg, IFLA_IPVLAN_ISOLATION, isolation))
471 goto out;
472 }
473
474 nla_end_nested(nlmsg, nest2);
475 }
476
477 nla_end_nested(nlmsg, nest);
478
479 if (nla_put_u32(nlmsg, IFLA_LINK, index))
480 goto out;
481
482 if (nla_put_string(nlmsg, IFLA_IFNAME, name))
483 goto out;
484
485 err = netlink_transaction(&nlh, nlmsg, answer);
486 out:
487 netlink_close(&nlh);
488 nlmsg_free(answer);
489 nlmsg_free(nlmsg);
490 if (err < 0)
491 return minus_one_set_errno(-err);
492 return 0;
493 }
494
495 static int instantiate_ipvlan(struct lxc_handler *handler, struct lxc_netdev *netdev)
496 {
497 char peer[IFNAMSIZ];
498 int err;
499 unsigned int mtu = 0;
500
501 if (netdev->link[0] == '\0') {
502 ERROR("No link for ipvlan network device specified");
503 return -1;
504 }
505
506 err = snprintf(peer, sizeof(peer), "ipXXXXXX");
507 if (err < 0 || (size_t)err >= sizeof(peer))
508 return -1;
509
510 if (!lxc_mkifname(peer))
511 return -1;
512
513 err = lxc_ipvlan_create(netdev->link, peer, netdev->priv.ipvlan_attr.mode,
514 netdev->priv.ipvlan_attr.isolation);
515 if (err) {
516 SYSERROR("Failed to create ipvlan interface \"%s\" on \"%s\"",
517 peer, netdev->link);
518 goto on_error;
519 }
520
521 strlcpy(netdev->created_name, peer, IFNAMSIZ);
522
523 netdev->ifindex = if_nametoindex(peer);
524 if (!netdev->ifindex) {
525 ERROR("Failed to retrieve ifindex for \"%s\"", peer);
526 goto on_error;
527 }
528
529 if (netdev->mtu) {
530 err = lxc_safe_uint(netdev->mtu, &mtu);
531 if (err < 0) {
532 errno = -err;
533 SYSERROR("Failed to parse mtu \"%s\" for interface \"%s\"",
534 netdev->mtu, peer);
535 goto on_error;
536 }
537
538 err = lxc_netdev_set_mtu(peer, mtu);
539 if (err < 0) {
540 errno = -err;
541 SYSERROR("Failed to set mtu \"%s\" for interface \"%s\"",
542 netdev->mtu, peer);
543 goto on_error;
544 }
545 }
546
547 if (netdev->upscript) {
548 char *argv[] = {
549 "ipvlan",
550 netdev->link,
551 NULL,
552 };
553
554 err = run_script_argv(handler->name, handler->conf->hooks_version,
555 "net", netdev->upscript, "up", argv);
556 if (err < 0)
557 goto on_error;
558 }
559
560 DEBUG("Instantiated ipvlan \"%s\" with ifindex is %d and mode %d", peer,
561 netdev->ifindex, netdev->priv.macvlan_attr.mode);
562
563 return 0;
564
565 on_error:
566 lxc_netdev_delete_by_name(peer);
567 return -1;
568 }
569
570 static int instantiate_vlan(struct lxc_handler *handler, struct lxc_netdev *netdev)
571 {
572 char peer[IFNAMSIZ];
573 int err;
574 static uint16_t vlan_cntr = 0;
575 unsigned int mtu = 0;
576
577 if (netdev->link[0] == '\0') {
578 ERROR("No link for vlan network device specified");
579 return -1;
580 }
581
582 err = snprintf(peer, sizeof(peer), "vlan%d-%d",
583 netdev->priv.vlan_attr.vid, vlan_cntr++);
584 if (err < 0 || (size_t)err >= sizeof(peer))
585 return -1;
586
587 err = lxc_vlan_create(netdev->link, peer, netdev->priv.vlan_attr.vid);
588 if (err) {
589 errno = -err;
590 SYSERROR("Failed to create vlan interface \"%s\" on \"%s\"",
591 peer, netdev->link);
592 return -1;
593 }
594
595 strlcpy(netdev->created_name, peer, IFNAMSIZ);
596
597 netdev->ifindex = if_nametoindex(peer);
598 if (!netdev->ifindex) {
599 ERROR("Failed to retrieve ifindex for \"%s\"", peer);
600 goto on_error;
601 }
602
603 if (netdev->mtu) {
604 err = lxc_safe_uint(netdev->mtu, &mtu);
605 if (err < 0) {
606 errno = -err;
607 SYSERROR("Failed to parse mtu \"%s\" for interface \"%s\"",
608 netdev->mtu, peer);
609 goto on_error;
610 }
611
612 err = lxc_netdev_set_mtu(peer, mtu);
613 if (err) {
614 errno = -err;
615 SYSERROR("Failed to set mtu \"%s\" for interface \"%s\"",
616 netdev->mtu, peer);
617 goto on_error;
618 }
619 }
620
621 if (netdev->upscript) {
622 char *argv[] = {
623 "vlan",
624 netdev->link,
625 NULL,
626 };
627
628 err = run_script_argv(handler->name, handler->conf->hooks_version,
629 "net", netdev->upscript, "up", argv);
630 if (err < 0) {
631 goto on_error;
632 }
633 }
634
635 DEBUG("Instantiated vlan \"%s\" with ifindex is \"%d\"", peer,
636 netdev->ifindex);
637
638 return 0;
639
640 on_error:
641 lxc_netdev_delete_by_name(peer);
642 return -1;
643 }
644
645 static int instantiate_phys(struct lxc_handler *handler, struct lxc_netdev *netdev)
646 {
647 int err, mtu_orig = 0;
648 unsigned int mtu = 0;
649
650 if (netdev->link[0] == '\0') {
651 ERROR("No link for physical interface specified");
652 return -1;
653 }
654
655 /*
656 * Note that we're retrieving the container's ifindex in the host's
657 * network namespace because we need it to move the device from the
658 * host's network namespace to the container's network namespace later
659 * on.
660 * Note that netdev->link will contain the name of the physical network
661 * device in the host's namespace.
662 */
663 netdev->ifindex = if_nametoindex(netdev->link);
664 if (!netdev->ifindex) {
665 ERROR("Failed to retrieve ifindex for \"%s\"", netdev->link);
666 return -1;
667 }
668
669 strlcpy(netdev->created_name, netdev->link, IFNAMSIZ);
670
671 /*
672 * Store the ifindex of the host's network device in the host's
673 * namespace.
674 */
675 netdev->priv.phys_attr.ifindex = netdev->ifindex;
676
677 /*
678 * Get original device MTU setting and store for restoration after
679 * container shutdown.
680 */
681 mtu_orig = netdev_get_mtu(netdev->ifindex);
682 if (mtu_orig < 0) {
683 SYSERROR("Failed to get original mtu for interface \"%s\"", netdev->link);
684 return minus_one_set_errno(-mtu_orig);
685 }
686
687 netdev->priv.phys_attr.mtu = mtu_orig;
688
689 if (netdev->mtu) {
690 err = lxc_safe_uint(netdev->mtu, &mtu);
691 if (err < 0) {
692 errno = -err;
693 SYSERROR("Failed to parse mtu \"%s\" for interface \"%s\"",
694 netdev->mtu, netdev->link);
695 return -1;
696 }
697
698 err = lxc_netdev_set_mtu(netdev->link, mtu);
699 if (err < 0) {
700 errno = -err;
701 SYSERROR("Failed to set mtu \"%s\" for interface \"%s\"",
702 netdev->mtu, netdev->link);
703 return -1;
704 }
705 }
706
707 if (netdev->upscript) {
708 char *argv[] = {
709 "phys",
710 netdev->link,
711 NULL,
712 };
713
714 err = run_script_argv(handler->name, handler->conf->hooks_version,
715 "net", netdev->upscript, "up", argv);
716 if (err < 0) {
717 return -1;
718 }
719 }
720
721 DEBUG("Instantiated phys \"%s\" with ifindex is \"%d\"", netdev->link,
722 netdev->ifindex);
723
724 return 0;
725 }
726
727 static int instantiate_empty(struct lxc_handler *handler, struct lxc_netdev *netdev)
728 {
729 int ret;
730 char *argv[] = {
731 "empty",
732 NULL,
733 };
734
735 netdev->ifindex = 0;
736 if (!netdev->upscript)
737 return 0;
738
739 ret = run_script_argv(handler->name, handler->conf->hooks_version,
740 "net", netdev->upscript, "up", argv);
741 if (ret < 0)
742 return -1;
743
744 return 0;
745 }
746
747 static int instantiate_none(struct lxc_handler *handler, struct lxc_netdev *netdev)
748 {
749 netdev->ifindex = 0;
750 return 0;
751 }
752
753 static instantiate_cb netdev_conf[LXC_NET_MAXCONFTYPE + 1] = {
754 [LXC_NET_VETH] = instantiate_veth,
755 [LXC_NET_MACVLAN] = instantiate_macvlan,
756 [LXC_NET_IPVLAN] = instantiate_ipvlan,
757 [LXC_NET_VLAN] = instantiate_vlan,
758 [LXC_NET_PHYS] = instantiate_phys,
759 [LXC_NET_EMPTY] = instantiate_empty,
760 [LXC_NET_NONE] = instantiate_none,
761 };
762
763 static int shutdown_veth(struct lxc_handler *handler, struct lxc_netdev *netdev)
764 {
765 int ret;
766 char *argv[] = {
767 "veth",
768 netdev->link,
769 NULL,
770 NULL,
771 };
772
773 if (!netdev->downscript)
774 return 0;
775
776 if (netdev->priv.veth_attr.pair[0] != '\0')
777 argv[2] = netdev->priv.veth_attr.pair;
778 else
779 argv[2] = netdev->priv.veth_attr.veth1;
780
781 ret = run_script_argv(handler->name,
782 handler->conf->hooks_version, "net",
783 netdev->downscript, "down", argv);
784 if (ret < 0)
785 return -1;
786
787 return 0;
788 }
789
790 static int shutdown_macvlan(struct lxc_handler *handler, struct lxc_netdev *netdev)
791 {
792 int ret;
793 char *argv[] = {
794 "macvlan",
795 netdev->link,
796 NULL,
797 };
798
799 if (!netdev->downscript)
800 return 0;
801
802 ret = run_script_argv(handler->name, handler->conf->hooks_version,
803 "net", netdev->downscript, "down", argv);
804 if (ret < 0)
805 return -1;
806
807 return 0;
808 }
809
810 static int shutdown_ipvlan(struct lxc_handler *handler, struct lxc_netdev *netdev)
811 {
812 int ret;
813 char *argv[] = {
814 "ipvlan",
815 netdev->link,
816 NULL,
817 };
818
819 if (!netdev->downscript)
820 return 0;
821
822 ret = run_script_argv(handler->name, handler->conf->hooks_version,
823 "net", netdev->downscript, "down", argv);
824 if (ret < 0)
825 return -1;
826
827 return 0;
828 }
829
830 static int shutdown_vlan(struct lxc_handler *handler, struct lxc_netdev *netdev)
831 {
832 int ret;
833 char *argv[] = {
834 "vlan",
835 netdev->link,
836 NULL,
837 };
838
839 if (!netdev->downscript)
840 return 0;
841
842 ret = run_script_argv(handler->name, handler->conf->hooks_version,
843 "net", netdev->downscript, "down", argv);
844 if (ret < 0)
845 return -1;
846
847 return 0;
848 }
849
850 static int shutdown_phys(struct lxc_handler *handler, struct lxc_netdev *netdev)
851 {
852 int ret;
853 char *argv[] = {
854 "phys",
855 netdev->link,
856 NULL,
857 };
858
859 if (!netdev->downscript)
860 return 0;
861
862 ret = run_script_argv(handler->name, handler->conf->hooks_version,
863 "net", netdev->downscript, "down", argv);
864 if (ret < 0)
865 return -1;
866
867 return 0;
868 }
869
870 static int shutdown_empty(struct lxc_handler *handler, struct lxc_netdev *netdev)
871 {
872 int ret;
873 char *argv[] = {
874 "empty",
875 NULL,
876 };
877
878 if (!netdev->downscript)
879 return 0;
880
881 ret = run_script_argv(handler->name, handler->conf->hooks_version,
882 "net", netdev->downscript, "down", argv);
883 if (ret < 0)
884 return -1;
885
886 return 0;
887 }
888
889 static int shutdown_none(struct lxc_handler *handler, struct lxc_netdev *netdev)
890 {
891 return 0;
892 }
893
894 static instantiate_cb netdev_deconf[LXC_NET_MAXCONFTYPE + 1] = {
895 [LXC_NET_VETH] = shutdown_veth,
896 [LXC_NET_MACVLAN] = shutdown_macvlan,
897 [LXC_NET_IPVLAN] = shutdown_ipvlan,
898 [LXC_NET_VLAN] = shutdown_vlan,
899 [LXC_NET_PHYS] = shutdown_phys,
900 [LXC_NET_EMPTY] = shutdown_empty,
901 [LXC_NET_NONE] = shutdown_none,
902 };
903
904 static int lxc_netdev_move_by_index_fd(int ifindex, int fd, const char *ifname)
905 {
906 int err;
907 struct nl_handler nlh;
908 struct ifinfomsg *ifi;
909 struct nlmsg *nlmsg = NULL;
910
911 err = netlink_open(&nlh, NETLINK_ROUTE);
912 if (err)
913 return err;
914
915 err = -ENOMEM;
916 nlmsg = nlmsg_alloc(NLMSG_GOOD_SIZE);
917 if (!nlmsg)
918 goto out;
919
920 nlmsg->nlmsghdr->nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK;
921 nlmsg->nlmsghdr->nlmsg_type = RTM_NEWLINK;
922
923 ifi = nlmsg_reserve(nlmsg, sizeof(struct ifinfomsg));
924 if (!ifi)
925 goto out;
926 ifi->ifi_family = AF_UNSPEC;
927 ifi->ifi_index = ifindex;
928
929 if (nla_put_u32(nlmsg, IFLA_NET_NS_FD, fd))
930 goto out;
931
932 if (ifname != NULL) {
933 if (nla_put_string(nlmsg, IFLA_IFNAME, ifname))
934 goto out;
935 }
936
937 err = netlink_transaction(&nlh, nlmsg, nlmsg);
938 out:
939 netlink_close(&nlh);
940 nlmsg_free(nlmsg);
941 return err;
942 }
943
944 int lxc_netdev_move_by_index(int ifindex, pid_t pid, const char *ifname)
945 {
946 int err;
947 struct nl_handler nlh;
948 struct ifinfomsg *ifi;
949 struct nlmsg *nlmsg = NULL;
950
951 err = netlink_open(&nlh, NETLINK_ROUTE);
952 if (err)
953 return err;
954
955 err = -ENOMEM;
956 nlmsg = nlmsg_alloc(NLMSG_GOOD_SIZE);
957 if (!nlmsg)
958 goto out;
959
960 nlmsg->nlmsghdr->nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK;
961 nlmsg->nlmsghdr->nlmsg_type = RTM_NEWLINK;
962
963 ifi = nlmsg_reserve(nlmsg, sizeof(struct ifinfomsg));
964 if (!ifi)
965 goto out;
966 ifi->ifi_family = AF_UNSPEC;
967 ifi->ifi_index = ifindex;
968
969 if (nla_put_u32(nlmsg, IFLA_NET_NS_PID, pid))
970 goto out;
971
972 if (ifname != NULL) {
973 if (nla_put_string(nlmsg, IFLA_IFNAME, ifname))
974 goto out;
975 }
976
977 err = netlink_transaction(&nlh, nlmsg, nlmsg);
978 out:
979 netlink_close(&nlh);
980 nlmsg_free(nlmsg);
981 return err;
982 }
983
984 /* If we are asked to move a wireless interface, then we must actually move its
985 * phyN device. Detect that condition and return the physname here. The physname
986 * will be passed to lxc_netdev_move_wlan() which will free it when done.
987 */
988 #define PHYSNAME "/sys/class/net/%s/phy80211/name"
989 static char *is_wlan(const char *ifname)
990 {
991 __do_free char *path = NULL;
992 int i, ret;
993 long physlen;
994 size_t len;
995 FILE *f;
996 char *physname = NULL;
997
998 len = strlen(ifname) + strlen(PHYSNAME) - 1;
999 path = must_realloc(NULL, len + 1);
1000 ret = snprintf(path, len, PHYSNAME, ifname);
1001 if (ret < 0 || (size_t)ret >= len)
1002 goto bad;
1003
1004 f = fopen(path, "r");
1005 if (!f)
1006 goto bad;
1007
1008 /* Feh - sb.st_size is always 4096. */
1009 fseek(f, 0, SEEK_END);
1010 physlen = ftell(f);
1011 fseek(f, 0, SEEK_SET);
1012 if (physlen < 0) {
1013 fclose(f);
1014 goto bad;
1015 }
1016
1017 physname = malloc(physlen + 1);
1018 if (!physname) {
1019 fclose(f);
1020 goto bad;
1021 }
1022
1023 memset(physname, 0, physlen + 1);
1024 ret = fread(physname, 1, physlen, f);
1025 fclose(f);
1026 if (ret < 0)
1027 goto bad;
1028
1029 for (i = 0; i < physlen; i++) {
1030 if (physname[i] == '\n')
1031 physname[i] = '\0';
1032
1033 if (physname[i] == '\0')
1034 break;
1035 }
1036
1037 return physname;
1038
1039 bad:
1040 free(physname);
1041 return NULL;
1042 }
1043
1044 static int lxc_netdev_rename_by_name_in_netns(pid_t pid, const char *old,
1045 const char *new)
1046 {
1047 pid_t fpid;
1048
1049 fpid = fork();
1050 if (fpid < 0)
1051 return -1;
1052
1053 if (fpid != 0)
1054 return wait_for_pid(fpid);
1055
1056 if (!switch_to_ns(pid, "net"))
1057 return -1;
1058
1059 _exit(lxc_netdev_rename_by_name(old, new));
1060 }
1061
1062 static int lxc_netdev_move_wlan(char *physname, const char *ifname, pid_t pid,
1063 const char *newname)
1064 {
1065 char *cmd;
1066 pid_t fpid;
1067 int err = -1;
1068
1069 /* Move phyN into the container. TODO - do this using netlink.
1070 * However, IIUC this involves a bit more complicated work to talk to
1071 * the 80211 module, so for now just call out to iw.
1072 */
1073 cmd = on_path("iw", NULL);
1074 if (!cmd)
1075 goto out1;
1076 free(cmd);
1077
1078 fpid = fork();
1079 if (fpid < 0)
1080 goto out1;
1081
1082 if (fpid == 0) {
1083 char pidstr[30];
1084 sprintf(pidstr, "%d", pid);
1085 execlp("iw", "iw", "phy", physname, "set", "netns", pidstr,
1086 (char *)NULL);
1087 _exit(EXIT_FAILURE);
1088 }
1089
1090 if (wait_for_pid(fpid))
1091 goto out1;
1092
1093 err = 0;
1094 if (newname)
1095 err = lxc_netdev_rename_by_name_in_netns(pid, ifname, newname);
1096
1097 out1:
1098 free(physname);
1099 return err;
1100 }
1101
1102 int lxc_netdev_move_by_name(const char *ifname, pid_t pid, const char* newname)
1103 {
1104 int index;
1105 char *physname;
1106
1107 if (!ifname)
1108 return -EINVAL;
1109
1110 index = if_nametoindex(ifname);
1111 if (!index)
1112 return -EINVAL;
1113
1114 physname = is_wlan(ifname);
1115 if (physname)
1116 return lxc_netdev_move_wlan(physname, ifname, pid, newname);
1117
1118 return lxc_netdev_move_by_index(index, pid, newname);
1119 }
1120
1121 int lxc_netdev_delete_by_index(int ifindex)
1122 {
1123 int err;
1124 struct ifinfomsg *ifi;
1125 struct nl_handler nlh;
1126 struct nlmsg *answer = NULL, *nlmsg = NULL;
1127
1128 err = netlink_open(&nlh, NETLINK_ROUTE);
1129 if (err)
1130 return err;
1131
1132 err = -ENOMEM;
1133 nlmsg = nlmsg_alloc(NLMSG_GOOD_SIZE);
1134 if (!nlmsg)
1135 goto out;
1136
1137 answer = nlmsg_alloc_reserve(NLMSG_GOOD_SIZE);
1138 if (!answer)
1139 goto out;
1140
1141 nlmsg->nlmsghdr->nlmsg_flags = NLM_F_ACK | NLM_F_REQUEST;
1142 nlmsg->nlmsghdr->nlmsg_type = RTM_DELLINK;
1143
1144 ifi = nlmsg_reserve(nlmsg, sizeof(struct ifinfomsg));
1145 if (!ifi)
1146 goto out;
1147 ifi->ifi_family = AF_UNSPEC;
1148 ifi->ifi_index = ifindex;
1149
1150 err = netlink_transaction(&nlh, nlmsg, answer);
1151 out:
1152 netlink_close(&nlh);
1153 nlmsg_free(answer);
1154 nlmsg_free(nlmsg);
1155 return err;
1156 }
1157
1158 int lxc_netdev_delete_by_name(const char *name)
1159 {
1160 int index;
1161
1162 index = if_nametoindex(name);
1163 if (!index)
1164 return -EINVAL;
1165
1166 return lxc_netdev_delete_by_index(index);
1167 }
1168
1169 int lxc_netdev_rename_by_index(int ifindex, const char *newname)
1170 {
1171 int err, len;
1172 struct ifinfomsg *ifi;
1173 struct nl_handler nlh;
1174 struct nlmsg *answer = NULL, *nlmsg = NULL;
1175
1176 err = netlink_open(&nlh, NETLINK_ROUTE);
1177 if (err)
1178 return err;
1179
1180 len = strlen(newname);
1181 if (len == 1 || len >= IFNAMSIZ) {
1182 err = -EINVAL;
1183 goto out;
1184 }
1185
1186 err = -ENOMEM;
1187 nlmsg = nlmsg_alloc(NLMSG_GOOD_SIZE);
1188 if (!nlmsg)
1189 goto out;
1190
1191 answer = nlmsg_alloc_reserve(NLMSG_GOOD_SIZE);
1192 if (!answer)
1193 goto out;
1194
1195 nlmsg->nlmsghdr->nlmsg_flags = NLM_F_ACK | NLM_F_REQUEST;
1196 nlmsg->nlmsghdr->nlmsg_type = RTM_NEWLINK;
1197
1198 ifi = nlmsg_reserve(nlmsg, sizeof(struct ifinfomsg));
1199 if (!ifi)
1200 goto out;
1201 ifi->ifi_family = AF_UNSPEC;
1202 ifi->ifi_index = ifindex;
1203
1204 if (nla_put_string(nlmsg, IFLA_IFNAME, newname))
1205 goto out;
1206
1207 err = netlink_transaction(&nlh, nlmsg, answer);
1208 out:
1209 netlink_close(&nlh);
1210 nlmsg_free(answer);
1211 nlmsg_free(nlmsg);
1212 return err;
1213 }
1214
1215 int lxc_netdev_rename_by_name(const char *oldname, const char *newname)
1216 {
1217 int len, index;
1218
1219 len = strlen(oldname);
1220 if (len == 1 || len >= IFNAMSIZ)
1221 return -EINVAL;
1222
1223 index = if_nametoindex(oldname);
1224 if (!index)
1225 return -EINVAL;
1226
1227 return lxc_netdev_rename_by_index(index, newname);
1228 }
1229
1230 int netdev_set_flag(const char *name, int flag)
1231 {
1232 int err, index, len;
1233 struct ifinfomsg *ifi;
1234 struct nl_handler nlh;
1235 struct nlmsg *answer = NULL, *nlmsg = NULL;
1236
1237 err = netlink_open(&nlh, NETLINK_ROUTE);
1238 if (err)
1239 return err;
1240
1241 err = -EINVAL;
1242 len = strlen(name);
1243 if (len == 1 || len >= IFNAMSIZ)
1244 goto out;
1245
1246 err = -ENOMEM;
1247 nlmsg = nlmsg_alloc(NLMSG_GOOD_SIZE);
1248 if (!nlmsg)
1249 goto out;
1250
1251 answer = nlmsg_alloc_reserve(NLMSG_GOOD_SIZE);
1252 if (!answer)
1253 goto out;
1254
1255 err = -EINVAL;
1256 index = if_nametoindex(name);
1257 if (!index)
1258 goto out;
1259
1260 nlmsg->nlmsghdr->nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK;
1261 nlmsg->nlmsghdr->nlmsg_type = RTM_NEWLINK;
1262
1263 ifi = nlmsg_reserve(nlmsg, sizeof(struct ifinfomsg));
1264 if (!ifi) {
1265 err = -ENOMEM;
1266 goto out;
1267 }
1268 ifi->ifi_family = AF_UNSPEC;
1269 ifi->ifi_index = index;
1270 ifi->ifi_change |= IFF_UP;
1271 ifi->ifi_flags |= flag;
1272
1273 err = netlink_transaction(&nlh, nlmsg, answer);
1274 out:
1275 netlink_close(&nlh);
1276 nlmsg_free(nlmsg);
1277 nlmsg_free(answer);
1278 return err;
1279 }
1280
1281 int netdev_get_flag(const char *name, int *flag)
1282 {
1283 int err, index, len;
1284 struct ifinfomsg *ifi;
1285 struct nl_handler nlh;
1286 struct nlmsg *answer = NULL, *nlmsg = NULL;
1287
1288 if (!name)
1289 return -EINVAL;
1290
1291 err = netlink_open(&nlh, NETLINK_ROUTE);
1292 if (err)
1293 return err;
1294
1295 err = -EINVAL;
1296 len = strlen(name);
1297 if (len == 1 || len >= IFNAMSIZ)
1298 goto out;
1299
1300 err = -ENOMEM;
1301 nlmsg = nlmsg_alloc(NLMSG_GOOD_SIZE);
1302 if (!nlmsg)
1303 goto out;
1304
1305 answer = nlmsg_alloc_reserve(NLMSG_GOOD_SIZE);
1306 if (!answer)
1307 goto out;
1308
1309 err = -EINVAL;
1310 index = if_nametoindex(name);
1311 if (!index)
1312 goto out;
1313
1314 nlmsg->nlmsghdr->nlmsg_flags = NLM_F_REQUEST;
1315 nlmsg->nlmsghdr->nlmsg_type = RTM_GETLINK;
1316
1317 ifi = nlmsg_reserve(nlmsg, sizeof(struct ifinfomsg));
1318 if (!ifi) {
1319 err = -ENOMEM;
1320 goto out;
1321 }
1322 ifi->ifi_family = AF_UNSPEC;
1323 ifi->ifi_index = index;
1324
1325 err = netlink_transaction(&nlh, nlmsg, answer);
1326 if (err)
1327 goto out;
1328
1329 ifi = NLMSG_DATA(answer->nlmsghdr);
1330
1331 *flag = ifi->ifi_flags;
1332 out:
1333 netlink_close(&nlh);
1334 nlmsg_free(nlmsg);
1335 nlmsg_free(answer);
1336 return err;
1337 }
1338
1339 /*
1340 * \brief Check a interface is up or not.
1341 *
1342 * \param name: name for the interface.
1343 *
1344 * \return int.
1345 * 0 means interface is down.
1346 * 1 means interface is up.
1347 * Others means error happened, and ret-value is the error number.
1348 */
1349 int lxc_netdev_isup(const char *name)
1350 {
1351 int err, flag;
1352
1353 err = netdev_get_flag(name, &flag);
1354 if (err)
1355 return err;
1356
1357 if (flag & IFF_UP)
1358 return 1;
1359
1360 return 0;
1361 }
1362
1363 int netdev_get_mtu(int ifindex)
1364 {
1365 int answer_len, err, res;
1366 struct nl_handler nlh;
1367 struct ifinfomsg *ifi;
1368 struct nlmsghdr *msg;
1369 int readmore = 0, recv_len = 0;
1370 struct nlmsg *answer = NULL, *nlmsg = NULL;
1371
1372 err = netlink_open(&nlh, NETLINK_ROUTE);
1373 if (err)
1374 return err;
1375
1376 err = -ENOMEM;
1377 nlmsg = nlmsg_alloc(NLMSG_GOOD_SIZE);
1378 if (!nlmsg)
1379 goto out;
1380
1381 answer = nlmsg_alloc_reserve(NLMSG_GOOD_SIZE);
1382 if (!answer)
1383 goto out;
1384
1385 /* Save the answer buffer length, since it will be overwritten
1386 * on the first receive (and we might need to receive more than
1387 * once.
1388 */
1389 answer_len = answer->nlmsghdr->nlmsg_len;
1390
1391 nlmsg->nlmsghdr->nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP;
1392 nlmsg->nlmsghdr->nlmsg_type = RTM_GETLINK;
1393
1394 ifi = nlmsg_reserve(nlmsg, sizeof(struct ifinfomsg));
1395 if (!ifi)
1396 goto out;
1397 ifi->ifi_family = AF_UNSPEC;
1398
1399 /* Send the request for addresses, which returns all addresses
1400 * on all interfaces. */
1401 err = netlink_send(&nlh, nlmsg);
1402 if (err < 0)
1403 goto out;
1404
1405 #pragma GCC diagnostic push
1406 #pragma GCC diagnostic ignored "-Wcast-align"
1407
1408 do {
1409 /* Restore the answer buffer length, it might have been
1410 * overwritten by a previous receive.
1411 */
1412 answer->nlmsghdr->nlmsg_len = answer_len;
1413
1414 /* Get the (next) batch of reply messages */
1415 err = netlink_rcv(&nlh, answer);
1416 if (err < 0)
1417 goto out;
1418
1419 recv_len = err;
1420
1421 /* Satisfy the typing for the netlink macros */
1422 msg = answer->nlmsghdr;
1423
1424 while (NLMSG_OK(msg, recv_len)) {
1425
1426 /* Stop reading if we see an error message */
1427 if (msg->nlmsg_type == NLMSG_ERROR) {
1428 struct nlmsgerr *errmsg =
1429 (struct nlmsgerr *)NLMSG_DATA(msg);
1430 err = errmsg->error;
1431 goto out;
1432 }
1433
1434 /* Stop reading if we see a NLMSG_DONE message */
1435 if (msg->nlmsg_type == NLMSG_DONE) {
1436 readmore = 0;
1437 break;
1438 }
1439
1440 ifi = NLMSG_DATA(msg);
1441 if (ifi->ifi_index == ifindex) {
1442 struct rtattr *rta = IFLA_RTA(ifi);
1443 int attr_len =
1444 msg->nlmsg_len - NLMSG_LENGTH(sizeof(*ifi));
1445 res = 0;
1446 while (RTA_OK(rta, attr_len)) {
1447 /* Found a local address for the
1448 * requested interface, return it.
1449 */
1450 if (rta->rta_type == IFLA_MTU) {
1451 memcpy(&res, RTA_DATA(rta),
1452 sizeof(int));
1453 err = res;
1454 goto out;
1455 }
1456 rta = RTA_NEXT(rta, attr_len);
1457 }
1458 }
1459
1460 /* Keep reading more data from the socket if the last
1461 * message had the NLF_F_MULTI flag set.
1462 */
1463 readmore = (msg->nlmsg_flags & NLM_F_MULTI);
1464
1465 /* Look at the next message received in this buffer. */
1466 msg = NLMSG_NEXT(msg, recv_len);
1467 }
1468 } while (readmore);
1469
1470 #pragma GCC diagnostic pop
1471
1472 /* If we end up here, we didn't find any result, so signal an error. */
1473 err = -1;
1474
1475 out:
1476 netlink_close(&nlh);
1477 nlmsg_free(answer);
1478 nlmsg_free(nlmsg);
1479 return err;
1480 }
1481
1482 int lxc_netdev_set_mtu(const char *name, int mtu)
1483 {
1484 int err, index, len;
1485 struct ifinfomsg *ifi;
1486 struct nl_handler nlh;
1487 struct nlmsg *answer = NULL, *nlmsg = NULL;
1488
1489 err = netlink_open(&nlh, NETLINK_ROUTE);
1490 if (err)
1491 return err;
1492
1493 err = -EINVAL;
1494 len = strlen(name);
1495 if (len == 1 || len >= IFNAMSIZ)
1496 goto out;
1497
1498 err = -ENOMEM;
1499 nlmsg = nlmsg_alloc(NLMSG_GOOD_SIZE);
1500 if (!nlmsg)
1501 goto out;
1502
1503 answer = nlmsg_alloc_reserve(NLMSG_GOOD_SIZE);
1504 if (!answer)
1505 goto out;
1506
1507 err = -EINVAL;
1508 index = if_nametoindex(name);
1509 if (!index)
1510 goto out;
1511
1512 nlmsg->nlmsghdr->nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK;
1513 nlmsg->nlmsghdr->nlmsg_type = RTM_NEWLINK;
1514
1515 ifi = nlmsg_reserve(nlmsg, sizeof(struct ifinfomsg));
1516 if (!ifi) {
1517 err = -ENOMEM;
1518 goto out;
1519 }
1520 ifi->ifi_family = AF_UNSPEC;
1521 ifi->ifi_index = index;
1522
1523 if (nla_put_u32(nlmsg, IFLA_MTU, mtu))
1524 goto out;
1525
1526 err = netlink_transaction(&nlh, nlmsg, answer);
1527 out:
1528 netlink_close(&nlh);
1529 nlmsg_free(nlmsg);
1530 nlmsg_free(answer);
1531 return err;
1532 }
1533
1534 int lxc_netdev_up(const char *name)
1535 {
1536 return netdev_set_flag(name, IFF_UP);
1537 }
1538
1539 int lxc_netdev_down(const char *name)
1540 {
1541 return netdev_set_flag(name, 0);
1542 }
1543
1544 int lxc_veth_create(const char *name1, const char *name2)
1545 {
1546 int err, len;
1547 struct ifinfomsg *ifi;
1548 struct nl_handler nlh;
1549 struct rtattr *nest1, *nest2, *nest3;
1550 struct nlmsg *answer = NULL, *nlmsg = NULL;
1551
1552 err = netlink_open(&nlh, NETLINK_ROUTE);
1553 if (err)
1554 return err;
1555
1556 err = -EINVAL;
1557 len = strlen(name1);
1558 if (len == 1 || len >= IFNAMSIZ)
1559 goto out;
1560
1561 len = strlen(name2);
1562 if (len == 1 || len >= IFNAMSIZ)
1563 goto out;
1564
1565 err = -ENOMEM;
1566 nlmsg = nlmsg_alloc(NLMSG_GOOD_SIZE);
1567 if (!nlmsg)
1568 goto out;
1569
1570 answer = nlmsg_alloc_reserve(NLMSG_GOOD_SIZE);
1571 if (!answer)
1572 goto out;
1573
1574 nlmsg->nlmsghdr->nlmsg_flags =
1575 NLM_F_REQUEST | NLM_F_CREATE | NLM_F_EXCL | NLM_F_ACK;
1576 nlmsg->nlmsghdr->nlmsg_type = RTM_NEWLINK;
1577
1578 ifi = nlmsg_reserve(nlmsg, sizeof(struct ifinfomsg));
1579 if (!ifi)
1580 goto out;
1581 ifi->ifi_family = AF_UNSPEC;
1582
1583 err = -EINVAL;
1584 nest1 = nla_begin_nested(nlmsg, IFLA_LINKINFO);
1585 if (!nest1)
1586 goto out;
1587
1588 if (nla_put_string(nlmsg, IFLA_INFO_KIND, "veth"))
1589 goto out;
1590
1591 nest2 = nla_begin_nested(nlmsg, IFLA_INFO_DATA);
1592 if (!nest2)
1593 goto out;
1594
1595 nest3 = nla_begin_nested(nlmsg, VETH_INFO_PEER);
1596 if (!nest3)
1597 goto out;
1598
1599 ifi = nlmsg_reserve(nlmsg, sizeof(struct ifinfomsg));
1600 if (!ifi) {
1601 err = -ENOMEM;
1602 goto out;
1603 }
1604
1605 if (nla_put_string(nlmsg, IFLA_IFNAME, name2))
1606 goto out;
1607
1608 nla_end_nested(nlmsg, nest3);
1609 nla_end_nested(nlmsg, nest2);
1610 nla_end_nested(nlmsg, nest1);
1611
1612 if (nla_put_string(nlmsg, IFLA_IFNAME, name1))
1613 goto out;
1614
1615 err = netlink_transaction(&nlh, nlmsg, answer);
1616 out:
1617 netlink_close(&nlh);
1618 nlmsg_free(answer);
1619 nlmsg_free(nlmsg);
1620 return err;
1621 }
1622
1623 /* TODO: merge with lxc_macvlan_create */
1624 int lxc_vlan_create(const char *master, const char *name, unsigned short vlanid)
1625 {
1626 int err, len, lindex;
1627 struct ifinfomsg *ifi;
1628 struct nl_handler nlh;
1629 struct rtattr *nest, *nest2;
1630 struct nlmsg *answer = NULL, *nlmsg = NULL;
1631
1632 err = netlink_open(&nlh, NETLINK_ROUTE);
1633 if (err)
1634 return err;
1635
1636 err = -EINVAL;
1637 len = strlen(master);
1638 if (len == 1 || len >= IFNAMSIZ)
1639 goto err3;
1640
1641 len = strlen(name);
1642 if (len == 1 || len >= IFNAMSIZ)
1643 goto err3;
1644
1645 err = -ENOMEM;
1646 nlmsg = nlmsg_alloc(NLMSG_GOOD_SIZE);
1647 if (!nlmsg)
1648 goto err3;
1649
1650 answer = nlmsg_alloc_reserve(NLMSG_GOOD_SIZE);
1651 if (!answer)
1652 goto err2;
1653
1654 err = -EINVAL;
1655 lindex = if_nametoindex(master);
1656 if (!lindex)
1657 goto err1;
1658
1659 nlmsg->nlmsghdr->nlmsg_flags =
1660 NLM_F_REQUEST | NLM_F_CREATE | NLM_F_EXCL | NLM_F_ACK;
1661 nlmsg->nlmsghdr->nlmsg_type = RTM_NEWLINK;
1662
1663 ifi = nlmsg_reserve(nlmsg, sizeof(struct ifinfomsg));
1664 if (!ifi) {
1665 err = -ENOMEM;
1666 goto err1;
1667 }
1668 ifi->ifi_family = AF_UNSPEC;
1669
1670 nest = nla_begin_nested(nlmsg, IFLA_LINKINFO);
1671 if (!nest)
1672 goto err1;
1673
1674 if (nla_put_string(nlmsg, IFLA_INFO_KIND, "vlan"))
1675 goto err1;
1676
1677 nest2 = nla_begin_nested(nlmsg, IFLA_INFO_DATA);
1678 if (!nest2)
1679 goto err1;
1680
1681 if (nla_put_u16(nlmsg, IFLA_VLAN_ID, vlanid))
1682 goto err1;
1683
1684 nla_end_nested(nlmsg, nest2);
1685 nla_end_nested(nlmsg, nest);
1686
1687 if (nla_put_u32(nlmsg, IFLA_LINK, lindex))
1688 goto err1;
1689
1690 if (nla_put_string(nlmsg, IFLA_IFNAME, name))
1691 goto err1;
1692
1693 err = netlink_transaction(&nlh, nlmsg, answer);
1694 err1:
1695 nlmsg_free(answer);
1696 err2:
1697 nlmsg_free(nlmsg);
1698 err3:
1699 netlink_close(&nlh);
1700 return err;
1701 }
1702
1703 int lxc_macvlan_create(const char *master, const char *name, int mode)
1704 {
1705 int err, index, len;
1706 struct ifinfomsg *ifi;
1707 struct nl_handler nlh;
1708 struct rtattr *nest, *nest2;
1709 struct nlmsg *answer = NULL, *nlmsg = NULL;
1710
1711 err = netlink_open(&nlh, NETLINK_ROUTE);
1712 if (err)
1713 return err;
1714
1715 err = -EINVAL;
1716 len = strlen(master);
1717 if (len == 1 || len >= IFNAMSIZ)
1718 goto out;
1719
1720 len = strlen(name);
1721 if (len == 1 || len >= IFNAMSIZ)
1722 goto out;
1723
1724 err = -ENOMEM;
1725 nlmsg = nlmsg_alloc(NLMSG_GOOD_SIZE);
1726 if (!nlmsg)
1727 goto out;
1728
1729 answer = nlmsg_alloc_reserve(NLMSG_GOOD_SIZE);
1730 if (!answer)
1731 goto out;
1732
1733 err = -EINVAL;
1734 index = if_nametoindex(master);
1735 if (!index)
1736 goto out;
1737
1738 nlmsg->nlmsghdr->nlmsg_flags =
1739 NLM_F_REQUEST | NLM_F_CREATE | NLM_F_EXCL | NLM_F_ACK;
1740 nlmsg->nlmsghdr->nlmsg_type = RTM_NEWLINK;
1741
1742 ifi = nlmsg_reserve(nlmsg, sizeof(struct ifinfomsg));
1743 if (!ifi) {
1744 err = -ENOMEM;
1745 goto out;
1746 }
1747 ifi->ifi_family = AF_UNSPEC;
1748
1749 nest = nla_begin_nested(nlmsg, IFLA_LINKINFO);
1750 if (!nest)
1751 goto out;
1752
1753 if (nla_put_string(nlmsg, IFLA_INFO_KIND, "macvlan"))
1754 goto out;
1755
1756 if (mode) {
1757 nest2 = nla_begin_nested(nlmsg, IFLA_INFO_DATA);
1758 if (!nest2)
1759 goto out;
1760
1761 if (nla_put_u32(nlmsg, IFLA_MACVLAN_MODE, mode))
1762 goto out;
1763
1764 nla_end_nested(nlmsg, nest2);
1765 }
1766
1767 nla_end_nested(nlmsg, nest);
1768
1769 if (nla_put_u32(nlmsg, IFLA_LINK, index))
1770 goto out;
1771
1772 if (nla_put_string(nlmsg, IFLA_IFNAME, name))
1773 goto out;
1774
1775 err = netlink_transaction(&nlh, nlmsg, answer);
1776 out:
1777 netlink_close(&nlh);
1778 nlmsg_free(answer);
1779 nlmsg_free(nlmsg);
1780 return err;
1781 }
1782
1783 static int proc_sys_net_write(const char *path, const char *value)
1784 {
1785 int fd;
1786 int err = 0;
1787
1788 fd = open(path, O_WRONLY);
1789 if (fd < 0)
1790 return -errno;
1791
1792 if (lxc_write_nointr(fd, value, strlen(value)) < 0)
1793 err = -errno;
1794
1795 close(fd);
1796 return err;
1797 }
1798
1799 static int lxc_is_ip_forwarding_enabled(const char *ifname, int family)
1800 {
1801 int ret;
1802 char path[PATH_MAX];
1803 char buf[1] = "";
1804
1805 if (family != AF_INET && family != AF_INET6)
1806 return minus_one_set_errno(EINVAL);
1807
1808 ret = snprintf(path, PATH_MAX, "/proc/sys/net/%s/conf/%s/%s",
1809 family == AF_INET ? "ipv4" : "ipv6", ifname,
1810 "forwarding");
1811 if (ret < 0 || (size_t)ret >= PATH_MAX)
1812 return minus_one_set_errno(E2BIG);
1813
1814 return lxc_read_file_expect(path, buf, 1, "1");
1815 }
1816
1817 static int neigh_proxy_set(const char *ifname, int family, int flag)
1818 {
1819 int ret;
1820 char path[PATH_MAX];
1821
1822 if (family != AF_INET && family != AF_INET6)
1823 return -EINVAL;
1824
1825 ret = snprintf(path, PATH_MAX, "/proc/sys/net/%s/conf/%s/%s",
1826 family == AF_INET ? "ipv4" : "ipv6", ifname,
1827 family == AF_INET ? "proxy_arp" : "proxy_ndp");
1828 if (ret < 0 || (size_t)ret >= PATH_MAX)
1829 return -E2BIG;
1830
1831 return proc_sys_net_write(path, flag ? "1" : "0");
1832 }
1833
1834 static int lxc_is_ip_neigh_proxy_enabled(const char *ifname, int family)
1835 {
1836 int ret;
1837 char path[PATH_MAX];
1838 char buf[1] = "";
1839
1840 if (family != AF_INET && family != AF_INET6)
1841 return minus_one_set_errno(EINVAL);
1842
1843 ret = snprintf(path, PATH_MAX, "/proc/sys/net/%s/conf/%s/%s",
1844 family == AF_INET ? "ipv4" : "ipv6", ifname,
1845 family == AF_INET ? "proxy_arp" : "proxy_ndp");
1846 if (ret < 0 || (size_t)ret >= PATH_MAX)
1847 return minus_one_set_errno(E2BIG);
1848
1849 return lxc_read_file_expect(path, buf, 1, "1");
1850 }
1851
1852 int lxc_neigh_proxy_on(const char *name, int family)
1853 {
1854 return neigh_proxy_set(name, family, 1);
1855 }
1856
1857 int lxc_neigh_proxy_off(const char *name, int family)
1858 {
1859 return neigh_proxy_set(name, family, 0);
1860 }
1861
1862 int lxc_convert_mac(char *macaddr, struct sockaddr *sockaddr)
1863 {
1864 int i = 0;
1865 unsigned val;
1866 char c;
1867 unsigned char *data;
1868
1869 sockaddr->sa_family = ARPHRD_ETHER;
1870 data = (unsigned char *)sockaddr->sa_data;
1871
1872 while ((*macaddr != '\0') && (i < ETH_ALEN)) {
1873 c = *macaddr++;
1874 if (isdigit(c))
1875 val = c - '0';
1876 else if (c >= 'a' && c <= 'f')
1877 val = c - 'a' + 10;
1878 else if (c >= 'A' && c <= 'F')
1879 val = c - 'A' + 10;
1880 else
1881 return -EINVAL;
1882
1883 val <<= 4;
1884 c = *macaddr;
1885 if (isdigit(c))
1886 val |= c - '0';
1887 else if (c >= 'a' && c <= 'f')
1888 val |= c - 'a' + 10;
1889 else if (c >= 'A' && c <= 'F')
1890 val |= c - 'A' + 10;
1891 else if (c == ':' || c == 0)
1892 val >>= 4;
1893 else
1894 return -EINVAL;
1895 if (c != 0)
1896 macaddr++;
1897 *data++ = (unsigned char)(val & 0377);
1898 i++;
1899
1900 if (*macaddr == ':')
1901 macaddr++;
1902 }
1903
1904 return 0;
1905 }
1906
1907 static int ip_addr_add(int family, int ifindex, void *addr, void *bcast,
1908 void *acast, int prefix)
1909 {
1910 int addrlen, err;
1911 struct ifaddrmsg *ifa;
1912 struct nl_handler nlh;
1913 struct nlmsg *answer = NULL, *nlmsg = NULL;
1914
1915 addrlen = family == AF_INET ? sizeof(struct in_addr)
1916 : sizeof(struct in6_addr);
1917
1918 err = netlink_open(&nlh, NETLINK_ROUTE);
1919 if (err)
1920 return err;
1921
1922 err = -ENOMEM;
1923 nlmsg = nlmsg_alloc(NLMSG_GOOD_SIZE);
1924 if (!nlmsg)
1925 goto out;
1926
1927 answer = nlmsg_alloc_reserve(NLMSG_GOOD_SIZE);
1928 if (!answer)
1929 goto out;
1930
1931 nlmsg->nlmsghdr->nlmsg_flags =
1932 NLM_F_ACK | NLM_F_REQUEST | NLM_F_CREATE | NLM_F_EXCL;
1933 nlmsg->nlmsghdr->nlmsg_type = RTM_NEWADDR;
1934
1935 ifa = nlmsg_reserve(nlmsg, sizeof(struct ifaddrmsg));
1936 if (!ifa)
1937 goto out;
1938 ifa->ifa_prefixlen = prefix;
1939 ifa->ifa_index = ifindex;
1940 ifa->ifa_family = family;
1941 ifa->ifa_scope = 0;
1942
1943 err = -EINVAL;
1944 if (nla_put_buffer(nlmsg, IFA_LOCAL, addr, addrlen))
1945 goto out;
1946
1947 if (nla_put_buffer(nlmsg, IFA_ADDRESS, addr, addrlen))
1948 goto out;
1949
1950 if (nla_put_buffer(nlmsg, IFA_BROADCAST, bcast, addrlen))
1951 goto out;
1952
1953 /* TODO: multicast, anycast with ipv6 */
1954 err = -EPROTONOSUPPORT;
1955 if (family == AF_INET6 &&
1956 (memcmp(bcast, &in6addr_any, sizeof(in6addr_any)) ||
1957 memcmp(acast, &in6addr_any, sizeof(in6addr_any))))
1958 goto out;
1959
1960 err = netlink_transaction(&nlh, nlmsg, answer);
1961 out:
1962 netlink_close(&nlh);
1963 nlmsg_free(answer);
1964 nlmsg_free(nlmsg);
1965 return err;
1966 }
1967
1968 int lxc_ipv6_addr_add(int ifindex, struct in6_addr *addr,
1969 struct in6_addr *mcast, struct in6_addr *acast,
1970 int prefix)
1971 {
1972 return ip_addr_add(AF_INET6, ifindex, addr, mcast, acast, prefix);
1973 }
1974
1975 int lxc_ipv4_addr_add(int ifindex, struct in_addr *addr, struct in_addr *bcast,
1976 int prefix)
1977 {
1978 return ip_addr_add(AF_INET, ifindex, addr, bcast, NULL, prefix);
1979 }
1980
1981 /* Find an IFA_LOCAL (or IFA_ADDRESS if not IFA_LOCAL is present) address from
1982 * the given RTM_NEWADDR message. Allocates memory for the address and stores
1983 * that pointer in *res (so res should be an in_addr** or in6_addr**).
1984 */
1985 #pragma GCC diagnostic push
1986 #pragma GCC diagnostic ignored "-Wcast-align"
1987
1988 static int ifa_get_local_ip(int family, struct nlmsghdr *msg, void **res)
1989 {
1990 int addrlen;
1991 struct ifaddrmsg *ifa = NLMSG_DATA(msg);
1992 struct rtattr *rta = IFA_RTA(ifa);
1993 int attr_len = NLMSG_PAYLOAD(msg, sizeof(struct ifaddrmsg));
1994
1995 if (ifa->ifa_family != family)
1996 return 0;
1997
1998 addrlen = family == AF_INET ? sizeof(struct in_addr)
1999 : sizeof(struct in6_addr);
2000
2001 /* Loop over the rtattr's in this message */
2002 while (RTA_OK(rta, attr_len)) {
2003 /* Found a local address for the requested interface,
2004 * return it.
2005 */
2006 if (rta->rta_type == IFA_LOCAL ||
2007 rta->rta_type == IFA_ADDRESS) {
2008 /* Sanity check. The family check above should make sure
2009 * the address length is correct, but check here just in
2010 * case.
2011 */
2012 if (RTA_PAYLOAD(rta) != addrlen)
2013 return -1;
2014
2015 /* We might have found an IFA_ADDRESS before, which we
2016 * now overwrite with an IFA_LOCAL.
2017 */
2018 if (!*res) {
2019 *res = malloc(addrlen);
2020 if (!*res)
2021 return -1;
2022 }
2023
2024 memcpy(*res, RTA_DATA(rta), addrlen);
2025 if (rta->rta_type == IFA_LOCAL)
2026 break;
2027 }
2028 rta = RTA_NEXT(rta, attr_len);
2029 }
2030 return 0;
2031 }
2032
2033 #pragma GCC diagnostic pop
2034
2035 static int ip_addr_get(int family, int ifindex, void **res)
2036 {
2037 int answer_len, err;
2038 struct ifaddrmsg *ifa;
2039 struct nl_handler nlh;
2040 struct nlmsghdr *msg;
2041 int readmore = 0, recv_len = 0;
2042 struct nlmsg *answer = NULL, *nlmsg = NULL;
2043
2044 err = netlink_open(&nlh, NETLINK_ROUTE);
2045 if (err)
2046 return err;
2047
2048 err = -ENOMEM;
2049 nlmsg = nlmsg_alloc(NLMSG_GOOD_SIZE);
2050 if (!nlmsg)
2051 goto out;
2052
2053 answer = nlmsg_alloc_reserve(NLMSG_GOOD_SIZE);
2054 if (!answer)
2055 goto out;
2056
2057 /* Save the answer buffer length, since it will be overwritten on the
2058 * first receive (and we might need to receive more than once).
2059 */
2060 answer_len = answer->nlmsghdr->nlmsg_len;
2061
2062 nlmsg->nlmsghdr->nlmsg_flags = NLM_F_REQUEST | NLM_F_ROOT;
2063 nlmsg->nlmsghdr->nlmsg_type = RTM_GETADDR;
2064
2065 ifa = nlmsg_reserve(nlmsg, sizeof(struct ifaddrmsg));
2066 if (!ifa)
2067 goto out;
2068 ifa->ifa_family = family;
2069
2070 /* Send the request for addresses, which returns all addresses on all
2071 * interfaces.
2072 */
2073 err = netlink_send(&nlh, nlmsg);
2074 if (err < 0)
2075 goto out;
2076
2077 #pragma GCC diagnostic push
2078 #pragma GCC diagnostic ignored "-Wcast-align"
2079
2080 do {
2081 /* Restore the answer buffer length, it might have been
2082 * overwritten by a previous receive.
2083 */
2084 answer->nlmsghdr->nlmsg_len = answer_len;
2085
2086 /* Get the (next) batch of reply messages. */
2087 err = netlink_rcv(&nlh, answer);
2088 if (err < 0)
2089 goto out;
2090
2091 recv_len = err;
2092 err = 0;
2093
2094 /* Satisfy the typing for the netlink macros. */
2095 msg = answer->nlmsghdr;
2096
2097 while (NLMSG_OK(msg, recv_len)) {
2098 /* Stop reading if we see an error message. */
2099 if (msg->nlmsg_type == NLMSG_ERROR) {
2100 struct nlmsgerr *errmsg =
2101 (struct nlmsgerr *)NLMSG_DATA(msg);
2102 err = errmsg->error;
2103 goto out;
2104 }
2105
2106 /* Stop reading if we see a NLMSG_DONE message. */
2107 if (msg->nlmsg_type == NLMSG_DONE) {
2108 readmore = 0;
2109 break;
2110 }
2111
2112 if (msg->nlmsg_type != RTM_NEWADDR) {
2113 err = -1;
2114 goto out;
2115 }
2116
2117 ifa = (struct ifaddrmsg *)NLMSG_DATA(msg);
2118 if (ifa->ifa_index == ifindex) {
2119 if (ifa_get_local_ip(family, msg, res) < 0) {
2120 err = -1;
2121 goto out;
2122 }
2123
2124 /* Found a result, stop searching. */
2125 if (*res)
2126 goto out;
2127 }
2128
2129 /* Keep reading more data from the socket if the last
2130 * message had the NLF_F_MULTI flag set.
2131 */
2132 readmore = (msg->nlmsg_flags & NLM_F_MULTI);
2133
2134 /* Look at the next message received in this buffer. */
2135 msg = NLMSG_NEXT(msg, recv_len);
2136 }
2137 } while (readmore);
2138
2139 #pragma GCC diagnostic pop
2140
2141 /* If we end up here, we didn't find any result, so signal an
2142 * error.
2143 */
2144 err = -1;
2145
2146 out:
2147 netlink_close(&nlh);
2148 nlmsg_free(answer);
2149 nlmsg_free(nlmsg);
2150 return err;
2151 }
2152
2153 int lxc_ipv6_addr_get(int ifindex, struct in6_addr **res)
2154 {
2155 return ip_addr_get(AF_INET6, ifindex, (void **)res);
2156 }
2157
2158 int lxc_ipv4_addr_get(int ifindex, struct in_addr **res)
2159 {
2160 return ip_addr_get(AF_INET, ifindex, (void **)res);
2161 }
2162
2163 static int ip_gateway_add(int family, int ifindex, void *gw)
2164 {
2165 int addrlen, err;
2166 struct nl_handler nlh;
2167 struct rtmsg *rt;
2168 struct nlmsg *answer = NULL, *nlmsg = NULL;
2169
2170 addrlen = family == AF_INET ? sizeof(struct in_addr)
2171 : sizeof(struct in6_addr);
2172
2173 err = netlink_open(&nlh, NETLINK_ROUTE);
2174 if (err)
2175 return err;
2176
2177 err = -ENOMEM;
2178 nlmsg = nlmsg_alloc(NLMSG_GOOD_SIZE);
2179 if (!nlmsg)
2180 goto out;
2181
2182 answer = nlmsg_alloc_reserve(NLMSG_GOOD_SIZE);
2183 if (!answer)
2184 goto out;
2185
2186 nlmsg->nlmsghdr->nlmsg_flags =
2187 NLM_F_ACK | NLM_F_REQUEST | NLM_F_CREATE | NLM_F_EXCL;
2188 nlmsg->nlmsghdr->nlmsg_type = RTM_NEWROUTE;
2189
2190 rt = nlmsg_reserve(nlmsg, sizeof(struct rtmsg));
2191 if (!rt)
2192 goto out;
2193 rt->rtm_family = family;
2194 rt->rtm_table = RT_TABLE_MAIN;
2195 rt->rtm_scope = RT_SCOPE_UNIVERSE;
2196 rt->rtm_protocol = RTPROT_BOOT;
2197 rt->rtm_type = RTN_UNICAST;
2198 /* "default" destination */
2199 rt->rtm_dst_len = 0;
2200
2201 err = -EINVAL;
2202
2203 /* If gateway address not supplied, then a device route will be created instead */
2204 if (gw != NULL) {
2205 if (nla_put_buffer(nlmsg, RTA_GATEWAY, gw, addrlen))
2206 goto out;
2207 }
2208
2209 /* Adding the interface index enables the use of link-local
2210 * addresses for the gateway.
2211 */
2212 if (nla_put_u32(nlmsg, RTA_OIF, ifindex))
2213 goto out;
2214
2215 err = netlink_transaction(&nlh, nlmsg, answer);
2216 out:
2217 netlink_close(&nlh);
2218 nlmsg_free(answer);
2219 nlmsg_free(nlmsg);
2220 return err;
2221 }
2222
2223 int lxc_ipv4_gateway_add(int ifindex, struct in_addr *gw)
2224 {
2225 return ip_gateway_add(AF_INET, ifindex, gw);
2226 }
2227
2228 int lxc_ipv6_gateway_add(int ifindex, struct in6_addr *gw)
2229 {
2230 return ip_gateway_add(AF_INET6, ifindex, gw);
2231 }
2232 bool is_ovs_bridge(const char *bridge)
2233 {
2234 int ret;
2235 struct stat sb;
2236 char brdirname[22 + IFNAMSIZ + 1] = {0};
2237
2238 ret = snprintf(brdirname, 22 + IFNAMSIZ + 1, "/sys/class/net/%s/bridge",
2239 bridge);
2240 if (ret < 0 || (size_t)ret >= 22 + IFNAMSIZ + 1)
2241 return false;
2242
2243 ret = stat(brdirname, &sb);
2244 if (ret < 0 && errno == ENOENT)
2245 return true;
2246
2247 return false;
2248 }
2249
2250 struct ovs_veth_args {
2251 const char *bridge;
2252 const char *nic;
2253 };
2254
2255 /* Called from a background thread - when nic goes away, remove it from the
2256 * bridge.
2257 */
2258 static int lxc_ovs_delete_port_exec(void *data)
2259 {
2260 struct ovs_veth_args *args = data;
2261
2262 execlp("ovs-vsctl", "ovs-vsctl", "del-port", args->bridge, args->nic,
2263 (char *)NULL);
2264 return -1;
2265 }
2266
2267 int lxc_ovs_delete_port(const char *bridge, const char *nic)
2268 {
2269 int ret;
2270 char cmd_output[PATH_MAX];
2271 struct ovs_veth_args args;
2272
2273 args.bridge = bridge;
2274 args.nic = nic;
2275 ret = run_command(cmd_output, sizeof(cmd_output),
2276 lxc_ovs_delete_port_exec, (void *)&args);
2277 if (ret < 0) {
2278 ERROR("Failed to delete \"%s\" from openvswitch bridge \"%s\": "
2279 "%s", bridge, nic, cmd_output);
2280 return -1;
2281 }
2282
2283 return 0;
2284 }
2285
2286 static int lxc_ovs_attach_bridge_exec(void *data)
2287 {
2288 struct ovs_veth_args *args = data;
2289
2290 execlp("ovs-vsctl", "ovs-vsctl", "add-port", args->bridge, args->nic,
2291 (char *)NULL);
2292 return -1;
2293 }
2294
2295 static int lxc_ovs_attach_bridge(const char *bridge, const char *nic)
2296 {
2297 int ret;
2298 char cmd_output[PATH_MAX];
2299 struct ovs_veth_args args;
2300
2301 args.bridge = bridge;
2302 args.nic = nic;
2303 ret = run_command(cmd_output, sizeof(cmd_output),
2304 lxc_ovs_attach_bridge_exec, (void *)&args);
2305 if (ret < 0) {
2306 ERROR("Failed to attach \"%s\" to openvswitch bridge \"%s\": %s",
2307 bridge, nic, cmd_output);
2308 return -1;
2309 }
2310
2311 return 0;
2312 }
2313
2314 int lxc_bridge_attach(const char *bridge, const char *ifname)
2315 {
2316 int err, fd, index;
2317 size_t retlen;
2318 struct ifreq ifr;
2319
2320 if (strlen(ifname) >= IFNAMSIZ)
2321 return -EINVAL;
2322
2323 index = if_nametoindex(ifname);
2324 if (!index)
2325 return -EINVAL;
2326
2327 if (is_ovs_bridge(bridge))
2328 return lxc_ovs_attach_bridge(bridge, ifname);
2329
2330 fd = socket(AF_INET, SOCK_STREAM | SOCK_CLOEXEC, 0);
2331 if (fd < 0)
2332 return -errno;
2333
2334 retlen = strlcpy(ifr.ifr_name, bridge, IFNAMSIZ);
2335 if (retlen >= IFNAMSIZ) {
2336 close(fd);
2337 return -E2BIG;
2338 }
2339
2340 ifr.ifr_name[IFNAMSIZ - 1] = '\0';
2341 ifr.ifr_ifindex = index;
2342 err = ioctl(fd, SIOCBRADDIF, &ifr);
2343 close(fd);
2344 if (err)
2345 err = -errno;
2346
2347 return err;
2348 }
2349
2350 static const char *const lxc_network_types[LXC_NET_MAXCONFTYPE + 1] = {
2351 [LXC_NET_EMPTY] = "empty",
2352 [LXC_NET_VETH] = "veth",
2353 [LXC_NET_MACVLAN] = "macvlan",
2354 [LXC_NET_IPVLAN] = "ipvlan",
2355 [LXC_NET_PHYS] = "phys",
2356 [LXC_NET_VLAN] = "vlan",
2357 [LXC_NET_NONE] = "none",
2358 };
2359
2360 const char *lxc_net_type_to_str(int type)
2361 {
2362 if (type < 0 || type > LXC_NET_MAXCONFTYPE)
2363 return NULL;
2364
2365 return lxc_network_types[type];
2366 }
2367
2368 static const char padchar[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
2369
2370 char *lxc_mkifname(char *template)
2371 {
2372 int ret;
2373 struct netns_ifaddrs *ifa, *ifaddr;
2374 char name[IFNAMSIZ];
2375 bool exists = false;
2376 size_t i = 0;
2377 #ifdef HAVE_RAND_R
2378 unsigned int seed;
2379
2380 seed = randseed(false);
2381 #else
2382
2383 (void)randseed(true);
2384 #endif
2385
2386 if (strlen(template) >= IFNAMSIZ)
2387 return NULL;
2388
2389 /* Get all the network interfaces. */
2390 ret = netns_getifaddrs(&ifaddr, -1, &(bool){false});
2391 if (ret < 0) {
2392 SYSERROR("Failed to get network interfaces");
2393 return NULL;
2394 }
2395
2396 /* Generate random names until we find one that doesn't exist. */
2397 for (;;) {
2398 name[0] = '\0';
2399 (void)strlcpy(name, template, IFNAMSIZ);
2400
2401 exists = false;
2402
2403 for (i = 0; i < strlen(name); i++) {
2404 if (name[i] == 'X') {
2405 #ifdef HAVE_RAND_R
2406 name[i] = padchar[rand_r(&seed) % strlen(padchar)];
2407 #else
2408 name[i] = padchar[rand() % strlen(padchar)];
2409 #endif
2410 }
2411 }
2412
2413 for (ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next) {
2414 if (!strcmp(ifa->ifa_name, name)) {
2415 exists = true;
2416 break;
2417 }
2418 }
2419
2420 if (!exists)
2421 break;
2422 }
2423
2424 netns_freeifaddrs(ifaddr);
2425 (void)strlcpy(template, name, strlen(template) + 1);
2426
2427 return template;
2428 }
2429
2430 int setup_private_host_hw_addr(char *veth1)
2431 {
2432 int err, sockfd;
2433 struct ifreq ifr;
2434
2435 sockfd = socket(AF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0);
2436 if (sockfd < 0)
2437 return -errno;
2438
2439 err = snprintf((char *)ifr.ifr_name, IFNAMSIZ, "%s", veth1);
2440 if (err < 0 || (size_t)err >= IFNAMSIZ) {
2441 close(sockfd);
2442 return -E2BIG;
2443 }
2444
2445 err = ioctl(sockfd, SIOCGIFHWADDR, &ifr);
2446 if (err < 0) {
2447 close(sockfd);
2448 return -errno;
2449 }
2450
2451 ifr.ifr_hwaddr.sa_data[0] = 0xfe;
2452 err = ioctl(sockfd, SIOCSIFHWADDR, &ifr);
2453 close(sockfd);
2454 if (err < 0)
2455 return -errno;
2456
2457 return 0;
2458 }
2459
2460 int lxc_find_gateway_addresses(struct lxc_handler *handler)
2461 {
2462 struct lxc_list *network = &handler->conf->network;
2463 struct lxc_list *iterator;
2464 struct lxc_netdev *netdev;
2465 int link_index;
2466
2467 lxc_list_for_each(iterator, network) {
2468 netdev = iterator->elem;
2469
2470 if (!netdev->ipv4_gateway_auto && !netdev->ipv6_gateway_auto)
2471 continue;
2472
2473 if (netdev->type != LXC_NET_VETH && netdev->type != LXC_NET_MACVLAN) {
2474 ERROR("Automatic gateway detection is only supported "
2475 "for veth and macvlan");
2476 return -1;
2477 }
2478
2479 if (netdev->link[0] == '\0') {
2480 ERROR("Automatic gateway detection needs a link interface");
2481 return -1;
2482 }
2483
2484 link_index = if_nametoindex(netdev->link);
2485 if (!link_index)
2486 return -EINVAL;
2487
2488 if (netdev->ipv4_gateway_auto) {
2489 if (lxc_ipv4_addr_get(link_index, &netdev->ipv4_gateway)) {
2490 ERROR("Failed to automatically find ipv4 gateway "
2491 "address from link interface \"%s\"", netdev->link);
2492 return -1;
2493 }
2494 }
2495
2496 if (netdev->ipv6_gateway_auto) {
2497 if (lxc_ipv6_addr_get(link_index, &netdev->ipv6_gateway)) {
2498 ERROR("Failed to automatically find ipv6 gateway "
2499 "address from link interface \"%s\"", netdev->link);
2500 return -1;
2501 }
2502 }
2503 }
2504
2505 return 0;
2506 }
2507
2508 #define LXC_USERNIC_PATH LIBEXECDIR "/lxc/lxc-user-nic"
2509 static int lxc_create_network_unpriv_exec(const char *lxcpath, const char *lxcname,
2510 struct lxc_netdev *netdev, pid_t pid, unsigned int hooks_version)
2511 {
2512 int ret;
2513 pid_t child;
2514 int bytes, pipefd[2];
2515 char *token, *saveptr = NULL;
2516 char netdev_link[IFNAMSIZ];
2517 char buffer[PATH_MAX] = {0};
2518 size_t retlen;
2519
2520 if (netdev->type != LXC_NET_VETH) {
2521 ERROR("Network type %d not support for unprivileged use", netdev->type);
2522 return -1;
2523 }
2524
2525 ret = pipe(pipefd);
2526 if (ret < 0) {
2527 SYSERROR("Failed to create pipe");
2528 return -1;
2529 }
2530
2531 child = fork();
2532 if (child < 0) {
2533 SYSERROR("Failed to create new process");
2534 close(pipefd[0]);
2535 close(pipefd[1]);
2536 return -1;
2537 }
2538
2539 if (child == 0) {
2540 char pidstr[INTTYPE_TO_STRLEN(pid_t)];
2541
2542 close(pipefd[0]);
2543
2544 ret = dup2(pipefd[1], STDOUT_FILENO);
2545 if (ret >= 0)
2546 ret = dup2(pipefd[1], STDERR_FILENO);
2547 close(pipefd[1]);
2548 if (ret < 0) {
2549 SYSERROR("Failed to duplicate std{err,out} file descriptor");
2550 _exit(EXIT_FAILURE);
2551 }
2552
2553 if (netdev->link[0] != '\0')
2554 retlen = strlcpy(netdev_link, netdev->link, IFNAMSIZ);
2555 else
2556 retlen = strlcpy(netdev_link, "none", IFNAMSIZ);
2557 if (retlen >= IFNAMSIZ) {
2558 SYSERROR("Invalid network device name");
2559 _exit(EXIT_FAILURE);
2560 }
2561
2562 ret = snprintf(pidstr, sizeof(pidstr), "%d", pid);
2563 if (ret < 0 || ret >= sizeof(pidstr))
2564 _exit(EXIT_FAILURE);
2565 pidstr[sizeof(pidstr) - 1] = '\0';
2566
2567 INFO("Execing lxc-user-nic create %s %s %s veth %s %s", lxcpath,
2568 lxcname, pidstr, netdev_link,
2569 netdev->name[0] != '\0' ? netdev->name : "(null)");
2570 if (netdev->name[0] != '\0')
2571 execlp(LXC_USERNIC_PATH, LXC_USERNIC_PATH, "create",
2572 lxcpath, lxcname, pidstr, "veth", netdev_link,
2573 netdev->name, (char *)NULL);
2574 else
2575 execlp(LXC_USERNIC_PATH, LXC_USERNIC_PATH, "create",
2576 lxcpath, lxcname, pidstr, "veth", netdev_link,
2577 (char *)NULL);
2578 SYSERROR("Failed to execute lxc-user-nic");
2579 _exit(EXIT_FAILURE);
2580 }
2581
2582 /* close the write-end of the pipe */
2583 close(pipefd[1]);
2584
2585 bytes = lxc_read_nointr(pipefd[0], &buffer, PATH_MAX);
2586 if (bytes < 0) {
2587 SYSERROR("Failed to read from pipe file descriptor");
2588 close(pipefd[0]);
2589 } else {
2590 buffer[bytes - 1] = '\0';
2591 }
2592
2593 ret = wait_for_pid(child);
2594 close(pipefd[0]);
2595 if (ret != 0 || bytes < 0) {
2596 ERROR("lxc-user-nic failed to configure requested network: %s",
2597 buffer[0] != '\0' ? buffer : "(null)");
2598 return -1;
2599 }
2600 TRACE("Received output \"%s\" from lxc-user-nic", buffer);
2601
2602 /* netdev->name */
2603 token = strtok_r(buffer, ":", &saveptr);
2604 if (!token) {
2605 ERROR("Failed to parse lxc-user-nic output");
2606 return -1;
2607 }
2608
2609 /*
2610 * lxc-user-nic will take care of proper network device naming. So
2611 * netdev->name and netdev->created_name need to be identical to not
2612 * trigger another rename later on.
2613 */
2614 retlen = strlcpy(netdev->name, token, IFNAMSIZ);
2615 if (retlen < IFNAMSIZ)
2616 retlen = strlcpy(netdev->created_name, token, IFNAMSIZ);
2617 if (retlen >= IFNAMSIZ) {
2618 ERROR("Container side veth device name returned by lxc-user-nic is too long");
2619 return -E2BIG;
2620 }
2621
2622 /* netdev->ifindex */
2623 token = strtok_r(NULL, ":", &saveptr);
2624 if (!token) {
2625 ERROR("Failed to parse lxc-user-nic output");
2626 return -1;
2627 }
2628
2629 ret = lxc_safe_int(token, &netdev->ifindex);
2630 if (ret < 0) {
2631 errno = -ret;
2632 SYSERROR("Failed to convert string \"%s\" to integer", token);
2633 return -1;
2634 }
2635
2636 /* netdev->priv.veth_attr.veth1 */
2637 token = strtok_r(NULL, ":", &saveptr);
2638 if (!token) {
2639 ERROR("Failed to parse lxc-user-nic output");
2640 return -1;
2641 }
2642
2643 retlen = strlcpy(netdev->priv.veth_attr.veth1, token, IFNAMSIZ);
2644 if (retlen >= IFNAMSIZ) {
2645 ERROR("Host side veth device name returned by lxc-user-nic is "
2646 "too long");
2647 return -E2BIG;
2648 }
2649
2650 /* netdev->priv.veth_attr.ifindex */
2651 token = strtok_r(NULL, ":", &saveptr);
2652 if (!token) {
2653 ERROR("Failed to parse lxc-user-nic output");
2654 return -1;
2655 }
2656
2657 ret = lxc_safe_int(token, &netdev->priv.veth_attr.ifindex);
2658 if (ret < 0) {
2659 errno = -ret;
2660 SYSERROR("Failed to convert string \"%s\" to integer", token);
2661 return -1;
2662 }
2663
2664 if (netdev->upscript) {
2665 char *argv[] = {
2666 "veth",
2667 netdev->link,
2668 netdev->priv.veth_attr.veth1,
2669 NULL,
2670 };
2671
2672 ret = run_script_argv(lxcname, hooks_version, "net",
2673 netdev->upscript, "up", argv);
2674 if (ret < 0)
2675 return -1;
2676 }
2677
2678 return 0;
2679 }
2680
2681 static int lxc_delete_network_unpriv_exec(const char *lxcpath, const char *lxcname,
2682 struct lxc_netdev *netdev,
2683 const char *netns_path)
2684 {
2685 int bytes, ret;
2686 pid_t child;
2687 int pipefd[2];
2688 char buffer[PATH_MAX] = {0};
2689
2690 if (netdev->type != LXC_NET_VETH) {
2691 ERROR("Network type %d not support for unprivileged use", netdev->type);
2692 return -1;
2693 }
2694
2695 ret = pipe(pipefd);
2696 if (ret < 0) {
2697 SYSERROR("Failed to create pipe");
2698 return -1;
2699 }
2700
2701 child = fork();
2702 if (child < 0) {
2703 SYSERROR("Failed to create new process");
2704 close(pipefd[0]);
2705 close(pipefd[1]);
2706 return -1;
2707 }
2708
2709 if (child == 0) {
2710 char *hostveth;
2711
2712 close(pipefd[0]);
2713
2714 ret = dup2(pipefd[1], STDOUT_FILENO);
2715 if (ret >= 0)
2716 ret = dup2(pipefd[1], STDERR_FILENO);
2717 close(pipefd[1]);
2718 if (ret < 0) {
2719 SYSERROR("Failed to duplicate std{err,out} file descriptor");
2720 _exit(EXIT_FAILURE);
2721 }
2722
2723 if (netdev->priv.veth_attr.pair[0] != '\0')
2724 hostveth = netdev->priv.veth_attr.pair;
2725 else
2726 hostveth = netdev->priv.veth_attr.veth1;
2727 if (hostveth[0] == '\0') {
2728 SYSERROR("Host side veth device name is missing");
2729 _exit(EXIT_FAILURE);
2730 }
2731
2732 if (netdev->link[0] == '\0') {
2733 SYSERROR("Network link for network device \"%s\" is "
2734 "missing", netdev->priv.veth_attr.veth1);
2735 _exit(EXIT_FAILURE);
2736 }
2737
2738 INFO("Execing lxc-user-nic delete %s %s %s veth %s %s", lxcpath,
2739 lxcname, netns_path, netdev->link, hostveth);
2740 execlp(LXC_USERNIC_PATH, LXC_USERNIC_PATH, "delete", lxcpath,
2741 lxcname, netns_path, "veth", netdev->link, hostveth,
2742 (char *)NULL);
2743 SYSERROR("Failed to exec lxc-user-nic.");
2744 _exit(EXIT_FAILURE);
2745 }
2746
2747 close(pipefd[1]);
2748
2749 bytes = lxc_read_nointr(pipefd[0], &buffer, PATH_MAX);
2750 if (bytes < 0) {
2751 SYSERROR("Failed to read from pipe file descriptor.");
2752 close(pipefd[0]);
2753 } else {
2754 buffer[bytes - 1] = '\0';
2755 }
2756
2757 ret = wait_for_pid(child);
2758 close(pipefd[0]);
2759 if (ret != 0 || bytes < 0) {
2760 ERROR("lxc-user-nic failed to delete requested network: %s",
2761 buffer[0] != '\0' ? buffer : "(null)");
2762 return -1;
2763 }
2764
2765 return 0;
2766 }
2767
2768 bool lxc_delete_network_unpriv(struct lxc_handler *handler)
2769 {
2770 int ret;
2771 struct lxc_list *iterator;
2772 struct lxc_list *network = &handler->conf->network;
2773 /* strlen("/proc/") = 6
2774 * +
2775 * INTTYPE_TO_STRLEN(pid_t)
2776 * +
2777 * strlen("/fd/") = 4
2778 * +
2779 * INTTYPE_TO_STRLEN(int)
2780 * +
2781 * \0
2782 */
2783 char netns_path[6 + INTTYPE_TO_STRLEN(pid_t) + 4 + INTTYPE_TO_STRLEN(int) + 1];
2784
2785 *netns_path = '\0';
2786
2787 if (handler->nsfd[LXC_NS_NET] < 0) {
2788 DEBUG("Cannot not guarantee safe deletion of network devices. "
2789 "Manual cleanup maybe needed");
2790 return false;
2791 }
2792
2793 ret = snprintf(netns_path, sizeof(netns_path), "/proc/%d/fd/%d",
2794 lxc_raw_getpid(), handler->nsfd[LXC_NS_NET]);
2795 if (ret < 0 || ret >= sizeof(netns_path))
2796 return false;
2797
2798 lxc_list_for_each(iterator, network) {
2799 char *hostveth = NULL;
2800 struct lxc_netdev *netdev = iterator->elem;
2801
2802 /* We can only delete devices whose ifindex we have. If we don't
2803 * have the index it means that we didn't create it.
2804 */
2805 if (!netdev->ifindex)
2806 continue;
2807
2808 if (netdev->type == LXC_NET_PHYS) {
2809 ret = lxc_netdev_rename_by_index(netdev->ifindex,
2810 netdev->link);
2811 if (ret < 0)
2812 WARN("Failed to rename interface with index %d "
2813 "to its initial name \"%s\"",
2814 netdev->ifindex, netdev->link);
2815 else
2816 TRACE("Renamed interface with index %d to its "
2817 "initial name \"%s\"",
2818 netdev->ifindex, netdev->link);
2819
2820 ret = netdev_deconf[netdev->type](handler, netdev);
2821 goto clear_ifindices;
2822 }
2823
2824 ret = netdev_deconf[netdev->type](handler, netdev);
2825 if (ret < 0)
2826 WARN("Failed to deconfigure network device");
2827
2828 if (netdev->type != LXC_NET_VETH)
2829 goto clear_ifindices;
2830
2831 if (netdev->link[0] == '\0' || !is_ovs_bridge(netdev->link))
2832 goto clear_ifindices;
2833
2834 if (netdev->priv.veth_attr.pair[0] != '\0')
2835 hostveth = netdev->priv.veth_attr.pair;
2836 else
2837 hostveth = netdev->priv.veth_attr.veth1;
2838 if (hostveth[0] == '\0')
2839 goto clear_ifindices;
2840
2841 ret = lxc_delete_network_unpriv_exec(handler->lxcpath,
2842 handler->name, netdev,
2843 netns_path);
2844 if (ret < 0) {
2845 WARN("Failed to remove port \"%s\" from openvswitch "
2846 "bridge \"%s\"", hostveth, netdev->link);
2847 goto clear_ifindices;
2848 }
2849 INFO("Removed interface \"%s\" from \"%s\"", hostveth,
2850 netdev->link);
2851
2852 clear_ifindices:
2853 /* We need to clear any ifindices we recorded so liblxc won't
2854 * have cached stale data which would cause it to fail on reboot
2855 * we're we don't re-read the on-disk config file.
2856 */
2857 netdev->ifindex = 0;
2858 if (netdev->type == LXC_NET_PHYS) {
2859 netdev->priv.phys_attr.ifindex = 0;
2860 } else if (netdev->type == LXC_NET_VETH) {
2861 netdev->priv.veth_attr.veth1[0] = '\0';
2862 netdev->priv.veth_attr.ifindex = 0;
2863 }
2864 }
2865
2866 return true;
2867 }
2868
2869 struct ip_proxy_args {
2870 const char *ip;
2871 const char *dev;
2872 };
2873
2874 static int lxc_add_ip_neigh_proxy_exec_wrapper(void *data)
2875 {
2876 struct ip_proxy_args *args = data;
2877
2878 execlp("ip", "ip", "neigh", "add", "proxy", args->ip, "dev", args->dev, (char *)NULL);
2879 return -1;
2880 }
2881
2882 static int lxc_del_ip_neigh_proxy_exec_wrapper(void *data)
2883 {
2884 struct ip_proxy_args *args = data;
2885
2886 execlp("ip", "ip", "neigh", "flush", "proxy", args->ip, "dev", args->dev, (char *)NULL);
2887 return -1;
2888 }
2889
2890 static int lxc_add_ip_neigh_proxy(const char *ip, const char *dev)
2891 {
2892 int ret;
2893 char cmd_output[PATH_MAX];
2894 struct ip_proxy_args args = {
2895 .ip = ip,
2896 .dev = dev,
2897 };
2898
2899 ret = run_command(cmd_output, sizeof(cmd_output), lxc_add_ip_neigh_proxy_exec_wrapper, &args);
2900 if (ret < 0) {
2901 ERROR("Failed to add ip proxy \"%s\" to dev \"%s\": %s", ip, dev, cmd_output);
2902 return -1;
2903 }
2904
2905 return 0;
2906 }
2907
2908 static int lxc_del_ip_neigh_proxy(const char *ip, const char *dev)
2909 {
2910 int ret;
2911 char cmd_output[PATH_MAX];
2912 struct ip_proxy_args args = {
2913 .ip = ip,
2914 .dev = dev,
2915 };
2916
2917 ret = run_command(cmd_output, sizeof(cmd_output), lxc_del_ip_neigh_proxy_exec_wrapper, &args);
2918 if (ret < 0) {
2919 ERROR("Failed to delete ip proxy \"%s\" to dev \"%s\": %s", ip, dev, cmd_output);
2920 return -1;
2921 }
2922
2923 return 0;
2924 }
2925
2926 static int lxc_setup_l2proxy(struct lxc_netdev *netdev) {
2927 struct lxc_list *cur, *next;
2928 struct lxc_inetdev *inet4dev;
2929 struct lxc_inet6dev *inet6dev;
2930 char bufinet4[INET_ADDRSTRLEN], bufinet6[INET6_ADDRSTRLEN];
2931 int err = 0;
2932 unsigned int lo_ifindex = 0;
2933
2934 /* If IPv4 addresses are specified, then check that sysctl is configured correctly. */
2935 if (!lxc_list_empty(&netdev->ipv4)) {
2936 /* Check for net.ipv4.conf.[link].forwarding=1 */
2937 if (lxc_is_ip_forwarding_enabled(netdev->link, AF_INET) < 0) {
2938 ERROR("Requires sysctl net.ipv4.conf.%s.forwarding=1", netdev->link);
2939 return minus_one_set_errno(EINVAL);
2940 }
2941 }
2942
2943 /* If IPv6 addresses are specified, then check that sysctl is configured correctly. */
2944 if (!lxc_list_empty(&netdev->ipv6)) {
2945 /* Check for net.ipv6.conf.[link].proxy_ndp=1 */
2946 if (lxc_is_ip_neigh_proxy_enabled(netdev->link, AF_INET6) < 0) {
2947 ERROR("Requires sysctl net.ipv6.conf.%s.proxy_ndp=1", netdev->link);
2948 return minus_one_set_errno(EINVAL);
2949 }
2950
2951 /* Check for net.ipv6.conf.[link].forwarding=1 */
2952 if (lxc_is_ip_forwarding_enabled(netdev->link, AF_INET6) < 0) {
2953 ERROR("Requires sysctl net.ipv6.conf.%s.forwarding=1", netdev->link);
2954 return minus_one_set_errno(EINVAL);
2955 }
2956 }
2957
2958 /* Perform IPVLAN specific checks. */
2959 if (netdev->type == LXC_NET_IPVLAN) {
2960 /* Check mode is l3s as other modes do not work with l2proxy. */
2961 if (netdev->priv.ipvlan_attr.mode != IPVLAN_MODE_L3S) {
2962 ERROR("Requires ipvlan mode on dev \"%s\" be l3s when used with l2proxy", netdev->link);
2963 return minus_one_set_errno(EINVAL);
2964 }
2965
2966 /* Retrieve local-loopback interface index for use with IPVLAN static routes. */
2967 lo_ifindex = if_nametoindex(loop_device);
2968 if (lo_ifindex == 0) {
2969 ERROR("Failed to retrieve ifindex for \"%s\" routing cleanup", loop_device);
2970 return minus_one_set_errno(EINVAL);
2971 }
2972 }
2973
2974 lxc_list_for_each_safe(cur, &netdev->ipv4, next) {
2975 inet4dev = cur->elem;
2976 if (!inet_ntop(AF_INET, &inet4dev->addr, bufinet4, sizeof(bufinet4)))
2977 return minus_one_set_errno(-errno);
2978
2979 if (lxc_add_ip_neigh_proxy(bufinet4, netdev->link) < 0)
2980 return minus_one_set_errno(EINVAL);
2981
2982 /* IPVLAN requires a route to local-loopback to trigger l2proxy. */
2983 if (netdev->type == LXC_NET_IPVLAN) {
2984 err = lxc_ipv4_dest_add(lo_ifindex, &inet4dev->addr, 32);
2985 if (err < 0) {
2986 ERROR("Failed to add ipv4 dest \"%s\" for network device \"%s\"", bufinet4, loop_device);
2987 return minus_one_set_errno(-err);
2988 }
2989 }
2990 }
2991
2992 lxc_list_for_each_safe(cur, &netdev->ipv6, next) {
2993 inet6dev = cur->elem;
2994 if (!inet_ntop(AF_INET6, &inet6dev->addr, bufinet6, sizeof(bufinet6)))
2995 return minus_one_set_errno(-errno);
2996
2997 if (lxc_add_ip_neigh_proxy(bufinet6, netdev->link) < 0)
2998 return minus_one_set_errno(EINVAL);
2999
3000 /* IPVLAN requires a route to local-loopback to trigger l2proxy. */
3001 if (netdev->type == LXC_NET_IPVLAN) {
3002 err = lxc_ipv6_dest_add(lo_ifindex, &inet6dev->addr, 128);
3003 if (err < 0) {
3004 ERROR("Failed to add ipv6 dest \"%s\" for network device \"%s\"", bufinet6, loop_device);
3005 return minus_one_set_errno(-err);
3006 }
3007 }
3008 }
3009
3010 return 0;
3011 }
3012
3013 static int lxc_delete_ipv4_l2proxy(struct in_addr *ip, char *link, unsigned int lo_ifindex) {
3014 char bufinet4[INET_ADDRSTRLEN];
3015 unsigned int errCount = 0;
3016
3017 if (!inet_ntop(AF_INET, ip, bufinet4, sizeof(bufinet4))) {
3018 SYSERROR("Failed to convert IP for l2proxy ipv4 removal on dev \"%s\"", link);
3019 return minus_one_set_errno(EINVAL);
3020 }
3021
3022 /* If a local-loopback ifindex supplied remove the static route to the lo device. */
3023 if (lo_ifindex > 0) {
3024 if (lxc_ipv4_dest_del(lo_ifindex, ip, 32) < 0) {
3025 errCount++;
3026 ERROR("Failed to delete ipv4 dest \"%s\" for network ifindex \"%u\"", bufinet4, lo_ifindex);
3027 }
3028 }
3029
3030 /* If link is supplied remove the IP neigh proxy entry for this IP on the device. */
3031 if (link[0] != '\0') {
3032 if (lxc_del_ip_neigh_proxy(bufinet4, link) < 0)
3033 errCount++;
3034 }
3035
3036 if (errCount > 0)
3037 return minus_one_set_errno(EINVAL);
3038
3039 return 0;
3040 }
3041
3042 static int lxc_delete_ipv6_l2proxy(struct in6_addr *ip, char *link, unsigned int lo_ifindex) {
3043 char bufinet6[INET6_ADDRSTRLEN];
3044 unsigned int errCount = 0;
3045
3046 if (!inet_ntop(AF_INET6, ip, bufinet6, sizeof(bufinet6))) {
3047 SYSERROR("Failed to convert IP for l2proxy ipv6 removal on dev \"%s\"", link);
3048 return minus_one_set_errno(EINVAL);
3049 }
3050
3051 /* If a local-loopback ifindex supplied remove the static route to the lo device. */
3052 if (lo_ifindex > 0) {
3053 if (lxc_ipv6_dest_del(lo_ifindex, ip, 128) < 0) {
3054 errCount++;
3055 ERROR("Failed to delete ipv6 dest \"%s\" for network ifindex \"%u\"", bufinet6, lo_ifindex);
3056 }
3057 }
3058
3059 /* If link is supplied remove the IP neigh proxy entry for this IP on the device. */
3060 if (link[0] != '\0') {
3061 if (lxc_del_ip_neigh_proxy(bufinet6, link) < 0)
3062 errCount++;
3063 }
3064
3065 if (errCount > 0)
3066 return minus_one_set_errno(EINVAL);
3067
3068 return 0;
3069 }
3070
3071 static int lxc_delete_l2proxy(struct lxc_netdev *netdev) {
3072 unsigned int lo_ifindex = 0;
3073 unsigned int errCount = 0;
3074 struct lxc_list *cur, *next;
3075 struct lxc_inetdev *inet4dev;
3076 struct lxc_inet6dev *inet6dev;
3077
3078 /* Perform IPVLAN specific checks. */
3079 if (netdev->type == LXC_NET_IPVLAN) {
3080 /* Retrieve local-loopback interface index for use with IPVLAN static routes. */
3081 lo_ifindex = if_nametoindex(loop_device);
3082 if (lo_ifindex == 0) {
3083 errCount++;
3084 ERROR("Failed to retrieve ifindex for \"%s\" routing cleanup", loop_device);
3085 }
3086 }
3087
3088 lxc_list_for_each_safe(cur, &netdev->ipv4, next) {
3089 inet4dev = cur->elem;
3090 if (lxc_delete_ipv4_l2proxy(&inet4dev->addr, netdev->link, lo_ifindex) < 0)
3091 errCount++;
3092 }
3093
3094 lxc_list_for_each_safe(cur, &netdev->ipv6, next) {
3095 inet6dev = cur->elem;
3096 if (lxc_delete_ipv6_l2proxy(&inet6dev->addr, netdev->link, lo_ifindex) < 0)
3097 errCount++;
3098 }
3099
3100 if (errCount > 0)
3101 return minus_one_set_errno(EINVAL);
3102
3103 return 0;
3104 }
3105
3106 static int lxc_create_network_priv(struct lxc_handler *handler)
3107 {
3108 struct lxc_list *iterator;
3109 struct lxc_list *network = &handler->conf->network;
3110
3111 lxc_list_for_each(iterator, network) {
3112 struct lxc_netdev *netdev = iterator->elem;
3113
3114 if (netdev->type < 0 || netdev->type > LXC_NET_MAXCONFTYPE) {
3115 ERROR("Invalid network configuration type %d", netdev->type);
3116 return -1;
3117 }
3118
3119 /* Setup l2proxy entries if enabled and used with a link property */
3120 if (netdev->l2proxy && netdev->link[0] != '\0') {
3121 if (lxc_setup_l2proxy(netdev)) {
3122 ERROR("Failed to setup l2proxy");
3123 return -1;
3124 }
3125 }
3126
3127 if (netdev_conf[netdev->type](handler, netdev)) {
3128 ERROR("Failed to create network device");
3129 return -1;
3130 }
3131 }
3132
3133 return 0;
3134 }
3135
3136 int lxc_network_move_created_netdev_priv(struct lxc_handler *handler)
3137 {
3138 pid_t pid = handler->pid;
3139 struct lxc_list *network = &handler->conf->network;
3140 struct lxc_list *iterator;
3141
3142 if (am_guest_unpriv())
3143 return 0;
3144
3145 lxc_list_for_each(iterator, network) {
3146 int ret;
3147 char ifname[IFNAMSIZ];
3148 struct lxc_netdev *netdev = iterator->elem;
3149
3150 if (!netdev->ifindex)
3151 continue;
3152
3153 /* retrieve the name of the interface */
3154 if (!if_indextoname(netdev->ifindex, ifname)) {
3155 ERROR("No interface corresponding to ifindex \"%d\"",
3156 netdev->ifindex);
3157 return -1;
3158 }
3159
3160 ret = lxc_netdev_move_by_name(ifname, pid, NULL);
3161 if (ret) {
3162 errno = -ret;
3163 SYSERROR("Failed to move network device \"%s\" to network namespace %d",
3164 ifname, pid);
3165 return -1;
3166 }
3167
3168 strlcpy(netdev->created_name, ifname, IFNAMSIZ);
3169
3170 DEBUG("Moved network device \"%s\" to network namespace of %d",
3171 netdev->created_name, pid);
3172 }
3173
3174 return 0;
3175 }
3176
3177 static int lxc_create_network_unpriv(struct lxc_handler *handler)
3178 {
3179 int hooks_version = handler->conf->hooks_version;
3180 const char *lxcname = handler->name;
3181 const char *lxcpath = handler->lxcpath;
3182 struct lxc_list *network = &handler->conf->network;
3183 pid_t pid = handler->pid;
3184 struct lxc_list *iterator;
3185
3186 lxc_list_for_each(iterator, network) {
3187 struct lxc_netdev *netdev = iterator->elem;
3188
3189 if (netdev->type == LXC_NET_EMPTY)
3190 continue;
3191
3192 if (netdev->type == LXC_NET_NONE)
3193 continue;
3194
3195 if (netdev->type != LXC_NET_VETH) {
3196 ERROR("Networks of type %s are not supported by unprivileged containers",
3197 lxc_net_type_to_str(netdev->type));
3198 return -1;
3199 }
3200
3201 if (netdev->mtu)
3202 INFO("mtu ignored due to insufficient privilege");
3203
3204 if (lxc_create_network_unpriv_exec(lxcpath, lxcname, netdev,
3205 pid, hooks_version))
3206 return -1;
3207 }
3208
3209 return 0;
3210 }
3211
3212 bool lxc_delete_network_priv(struct lxc_handler *handler)
3213 {
3214 int ret;
3215 struct lxc_list *iterator;
3216 struct lxc_list *network = &handler->conf->network;
3217
3218 lxc_list_for_each(iterator, network) {
3219 char *hostveth = NULL;
3220 struct lxc_netdev *netdev = iterator->elem;
3221
3222 /* We can only delete devices whose ifindex we have. If we don't
3223 * have the index it means that we didn't create it.
3224 */
3225 if (!netdev->ifindex)
3226 continue;
3227
3228 /* Delete l2proxy entries if enabled and used with a link property */
3229 if (netdev->l2proxy && netdev->link[0] != '\0') {
3230 if (lxc_delete_l2proxy(netdev))
3231 WARN("Failed to delete all l2proxy config");
3232 /* Don't return, let the network be cleaned up as normal. */
3233 }
3234
3235 if (netdev->type == LXC_NET_PHYS) {
3236 ret = lxc_netdev_rename_by_index(netdev->ifindex, netdev->link);
3237 if (ret < 0)
3238 WARN("Failed to rename interface with index %d "
3239 "from \"%s\" to its initial name \"%s\"",
3240 netdev->ifindex, netdev->name, netdev->link);
3241 else {
3242 TRACE("Renamed interface with index %d from "
3243 "\"%s\" to its initial name \"%s\"",
3244 netdev->ifindex, netdev->name,
3245 netdev->link);
3246
3247 /* Restore original MTU */
3248 ret = lxc_netdev_set_mtu(netdev->link, netdev->priv.phys_attr.mtu);
3249 if (ret < 0) {
3250 WARN("Failed to set interface \"%s\" to its initial mtu \"%d\"",
3251 netdev->link, netdev->priv.phys_attr.mtu);
3252 } else {
3253 TRACE("Restored interface \"%s\" to its initial mtu \"%d\"",
3254 netdev->link, netdev->priv.phys_attr.mtu);
3255 }
3256 }
3257
3258 ret = netdev_deconf[netdev->type](handler, netdev);
3259 goto clear_ifindices;
3260 }
3261
3262 ret = netdev_deconf[netdev->type](handler, netdev);
3263 if (ret < 0)
3264 WARN("Failed to deconfigure network device");
3265
3266 /* Recent kernels remove the virtual interfaces when the network
3267 * namespace is destroyed but in case we did not move the
3268 * interface to the network namespace, we have to destroy it.
3269 */
3270 ret = lxc_netdev_delete_by_index(netdev->ifindex);
3271 if (ret < 0) {
3272 if (errno != ENODEV) {
3273 WARN("Failed to remove interface \"%s\" with index %d",
3274 netdev->name[0] != '\0' ? netdev->name : "(null)",
3275 netdev->ifindex);
3276 goto clear_ifindices;
3277 }
3278 INFO("Interface \"%s\" with index %d already deleted or existing in different network namespace",
3279 netdev->name[0] != '\0' ? netdev->name : "(null)",
3280 netdev->ifindex);
3281 }
3282 INFO("Removed interface \"%s\" with index %d",
3283 netdev->name[0] != '\0' ? netdev->name : "(null)",
3284 netdev->ifindex);
3285
3286 if (netdev->type != LXC_NET_VETH)
3287 goto clear_ifindices;
3288
3289 /* Explicitly delete host veth device to prevent lingering
3290 * devices. We had issues in LXD around this.
3291 */
3292 if (netdev->priv.veth_attr.pair[0] != '\0')
3293 hostveth = netdev->priv.veth_attr.pair;
3294 else
3295 hostveth = netdev->priv.veth_attr.veth1;
3296 if (hostveth[0] == '\0')
3297 goto clear_ifindices;
3298
3299 ret = lxc_netdev_delete_by_name(hostveth);
3300 if (ret < 0) {
3301 WARN("Failed to remove interface \"%s\" from \"%s\"",
3302 hostveth, netdev->link);
3303 goto clear_ifindices;
3304 }
3305 INFO("Removed interface \"%s\" from \"%s\"", hostveth, netdev->link);
3306
3307 if (netdev->link[0] == '\0' || !is_ovs_bridge(netdev->link)) {
3308 netdev->priv.veth_attr.veth1[0] = '\0';
3309 netdev->ifindex = 0;
3310 netdev->priv.veth_attr.ifindex = 0;
3311 goto clear_ifindices;
3312 }
3313
3314 /* Delete the openvswitch port. */
3315 ret = lxc_ovs_delete_port(netdev->link, hostveth);
3316 if (ret < 0)
3317 WARN("Failed to remove port \"%s\" from openvswitch "
3318 "bridge \"%s\"", hostveth, netdev->link);
3319 else
3320 INFO("Removed port \"%s\" from openvswitch bridge \"%s\"",
3321 hostveth, netdev->link);
3322
3323 clear_ifindices:
3324 /* We need to clear any ifindices we recorded so liblxc won't
3325 * have cached stale data which would cause it to fail on reboot
3326 * we're we don't re-read the on-disk config file.
3327 */
3328 netdev->ifindex = 0;
3329 if (netdev->type == LXC_NET_PHYS) {
3330 netdev->priv.phys_attr.ifindex = 0;
3331 } else if (netdev->type == LXC_NET_VETH) {
3332 netdev->priv.veth_attr.veth1[0] = '\0';
3333 netdev->priv.veth_attr.ifindex = 0;
3334 }
3335 }
3336
3337 return true;
3338 }
3339
3340 int lxc_requests_empty_network(struct lxc_handler *handler)
3341 {
3342 struct lxc_list *network = &handler->conf->network;
3343 struct lxc_list *iterator;
3344 bool found_none = false, found_nic = false;
3345
3346 if (lxc_list_empty(network))
3347 return 0;
3348
3349 lxc_list_for_each(iterator, network) {
3350 struct lxc_netdev *netdev = iterator->elem;
3351
3352 if (netdev->type == LXC_NET_NONE)
3353 found_none = true;
3354 else
3355 found_nic = true;
3356 }
3357 if (found_none && !found_nic)
3358 return 1;
3359 return 0;
3360 }
3361
3362 /* try to move physical nics to the init netns */
3363 int lxc_restore_phys_nics_to_netns(struct lxc_handler *handler)
3364 {
3365 int ret;
3366 int oldfd;
3367 char ifname[IFNAMSIZ];
3368 struct lxc_list *iterator;
3369 int netnsfd = handler->nsfd[LXC_NS_NET];
3370 struct lxc_conf *conf = handler->conf;
3371
3372 /* We need CAP_NET_ADMIN in the parent namespace in order to setns() to
3373 * the parent network namespace. We won't have this capability if we are
3374 * unprivileged.
3375 */
3376 if (!handler->am_root)
3377 return 0;
3378
3379 TRACE("Moving physical network devices back to parent network namespace");
3380
3381 oldfd = lxc_preserve_ns(handler->monitor_pid, "net");
3382 if (oldfd < 0) {
3383 SYSERROR("Failed to preserve network namespace");
3384 return -1;
3385 }
3386
3387 ret = setns(netnsfd, CLONE_NEWNET);
3388 if (ret < 0) {
3389 SYSERROR("Failed to enter network namespace");
3390 close(oldfd);
3391 return -1;
3392 }
3393
3394 lxc_list_for_each(iterator, &conf->network) {
3395 struct lxc_netdev *netdev = iterator->elem;
3396
3397 if (netdev->type != LXC_NET_PHYS)
3398 continue;
3399
3400 /* Retrieve the name of the interface in the container's network
3401 * namespace.
3402 */
3403 if (!if_indextoname(netdev->ifindex, ifname)) {
3404 WARN("No interface corresponding to ifindex %d",
3405 netdev->ifindex);
3406 continue;
3407 }
3408
3409 ret = lxc_netdev_move_by_index_fd(netdev->ifindex, oldfd, netdev->link);
3410 if (ret < 0)
3411 WARN("Error moving network device \"%s\" back to "
3412 "network namespace", ifname);
3413 else
3414 TRACE("Moved network device \"%s\" back to network "
3415 "namespace", ifname);
3416 }
3417
3418 ret = setns(oldfd, CLONE_NEWNET);
3419 close(oldfd);
3420 if (ret < 0) {
3421 SYSERROR("Failed to enter network namespace");
3422 return -1;
3423 }
3424
3425 return 0;
3426 }
3427
3428 static int setup_hw_addr(char *hwaddr, const char *ifname)
3429 {
3430 struct sockaddr sockaddr;
3431 struct ifreq ifr;
3432 int ret, fd;
3433
3434 ret = lxc_convert_mac(hwaddr, &sockaddr);
3435 if (ret) {
3436 errno = -ret;
3437 SYSERROR("Mac address \"%s\" conversion failed", hwaddr);
3438 return -1;
3439 }
3440
3441 memcpy(ifr.ifr_name, ifname, IFNAMSIZ);
3442 ifr.ifr_name[IFNAMSIZ-1] = '\0';
3443 memcpy((char *) &ifr.ifr_hwaddr, (char *) &sockaddr, sizeof(sockaddr));
3444
3445 fd = socket(AF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0);
3446 if (fd < 0)
3447 return -1;
3448
3449 ret = ioctl(fd, SIOCSIFHWADDR, &ifr);
3450 if (ret)
3451 SYSERROR("Failed to perform ioctl");
3452
3453 close(fd);
3454
3455 DEBUG("Mac address \"%s\" on \"%s\" has been setup", hwaddr,
3456 ifr.ifr_name);
3457
3458 return ret;
3459 }
3460
3461 static int setup_ipv4_addr(struct lxc_list *ip, int ifindex)
3462 {
3463 struct lxc_list *iterator;
3464 int err;
3465
3466 lxc_list_for_each(iterator, ip) {
3467 struct lxc_inetdev *inetdev = iterator->elem;
3468
3469 err = lxc_ipv4_addr_add(ifindex, &inetdev->addr,
3470 &inetdev->bcast, inetdev->prefix);
3471 if (err) {
3472 errno = -err;
3473 SYSERROR("Failed to setup ipv4 address for network device "
3474 "with ifindex %d", ifindex);
3475 return -1;
3476 }
3477 }
3478
3479 return 0;
3480 }
3481
3482 static int setup_ipv6_addr(struct lxc_list *ip, int ifindex)
3483 {
3484 struct lxc_list *iterator;
3485 int err;
3486
3487 lxc_list_for_each(iterator, ip) {
3488 struct lxc_inet6dev *inet6dev = iterator->elem;
3489
3490 err = lxc_ipv6_addr_add(ifindex, &inet6dev->addr,
3491 &inet6dev->mcast, &inet6dev->acast,
3492 inet6dev->prefix);
3493 if (err) {
3494 errno = -err;
3495 SYSERROR("Failed to setup ipv6 address for network device "
3496 "with ifindex %d", ifindex);
3497 return -1;
3498 }
3499 }
3500
3501 return 0;
3502 }
3503
3504 static int lxc_setup_netdev_in_child_namespaces(struct lxc_netdev *netdev)
3505 {
3506 char ifname[IFNAMSIZ];
3507 int err;
3508 char *current_ifname = ifname;
3509 char bufinet4[INET_ADDRSTRLEN], bufinet6[INET6_ADDRSTRLEN];
3510
3511 /* empty network namespace */
3512 if (!netdev->ifindex) {
3513 if (netdev->flags & IFF_UP) {
3514 err = lxc_netdev_up("lo");
3515 if (err) {
3516 errno = -err;
3517 SYSERROR("Failed to set the loopback network device up");
3518 return -1;
3519 }
3520 }
3521
3522 if (netdev->type == LXC_NET_EMPTY)
3523 return 0;
3524
3525 if (netdev->type == LXC_NET_NONE)
3526 return 0;
3527
3528 netdev->ifindex = if_nametoindex(netdev->created_name);
3529 if (!netdev->ifindex)
3530 SYSERROR("Failed to retrieve ifindex for network device with name %s",
3531 netdev->name ?: "(null)");
3532 }
3533
3534 /* get the new ifindex in case of physical netdev */
3535 if (netdev->type == LXC_NET_PHYS) {
3536 netdev->ifindex = if_nametoindex(netdev->link);
3537 if (!netdev->ifindex) {
3538 ERROR("Failed to get ifindex for network device \"%s\"",
3539 netdev->link);
3540 return -1;
3541 }
3542 }
3543
3544 /* retrieve the name of the interface */
3545 if (!if_indextoname(netdev->ifindex, current_ifname)) {
3546 SYSERROR("Failed to retrieve name for network device with ifindex %d",
3547 netdev->ifindex);
3548 return -1;
3549 }
3550
3551 /* Default: let the system choose an interface name.
3552 * When the IFLA_IFNAME attribute is passed something like "<prefix>%d"
3553 * netlink will replace the format specifier with an appropriate index.
3554 */
3555 if (netdev->name[0] == '\0') {
3556 if (netdev->type == LXC_NET_PHYS)
3557 (void)strlcpy(netdev->name, netdev->link, IFNAMSIZ);
3558 else
3559 (void)strlcpy(netdev->name, "eth%d", IFNAMSIZ);
3560 }
3561
3562 /* rename the interface name */
3563 if (strcmp(current_ifname, netdev->name) != 0) {
3564 err = lxc_netdev_rename_by_name(current_ifname, netdev->name);
3565 if (err) {
3566 errno = -err;
3567 SYSERROR("Failed to rename network device \"%s\" to \"%s\"",
3568 current_ifname, netdev->name);
3569 return -1;
3570 }
3571
3572 TRACE("Renamed network device from \"%s\" to \"%s\"",
3573 current_ifname, netdev->name);
3574 }
3575
3576 /* Re-read the name of the interface because its name has changed
3577 * and would be automatically allocated by the system
3578 */
3579 if (!if_indextoname(netdev->ifindex, current_ifname)) {
3580 ERROR("Failed get name for network device with ifindex %d",
3581 netdev->ifindex);
3582 return -1;
3583 }
3584
3585 /* Now update the recorded name of the network device to reflect the
3586 * name of the network device in the child's network namespace. We will
3587 * later on send this information back to the parent.
3588 */
3589 (void)strlcpy(netdev->name, current_ifname, IFNAMSIZ);
3590
3591 /* set a mac address */
3592 if (netdev->hwaddr) {
3593 if (setup_hw_addr(netdev->hwaddr, current_ifname)) {
3594 ERROR("Failed to setup hw address for network device \"%s\"",
3595 current_ifname);
3596 return -1;
3597 }
3598 }
3599
3600 /* setup ipv4 addresses on the interface */
3601 if (setup_ipv4_addr(&netdev->ipv4, netdev->ifindex)) {
3602 ERROR("Failed to setup ip addresses for network device \"%s\"",
3603 current_ifname);
3604 return -1;
3605 }
3606
3607 /* setup ipv6 addresses on the interface */
3608 if (setup_ipv6_addr(&netdev->ipv6, netdev->ifindex)) {
3609 ERROR("Failed to setup ipv6 addresses for network device \"%s\"",
3610 current_ifname);
3611 return -1;
3612 }
3613
3614 /* set the network device up */
3615 if (netdev->flags & IFF_UP) {
3616 err = lxc_netdev_up(current_ifname);
3617 if (err) {
3618 errno = -err;
3619 SYSERROR("Failed to set network device \"%s\" up",
3620 current_ifname);
3621 return -1;
3622 }
3623
3624 /* the network is up, make the loopback up too */
3625 err = lxc_netdev_up("lo");
3626 if (err) {
3627 errno = -err;
3628 SYSERROR("Failed to set the loopback network device up");
3629 return -1;
3630 }
3631 }
3632
3633 /* setup ipv4 gateway on the interface */
3634 if (netdev->ipv4_gateway || netdev->ipv4_gateway_dev) {
3635 if (!(netdev->flags & IFF_UP)) {
3636 ERROR("Cannot add ipv4 gateway for network device "
3637 "\"%s\" when not bringing up the interface", current_ifname);
3638 return -1;
3639 }
3640
3641 if (lxc_list_empty(&netdev->ipv4)) {
3642 ERROR("Cannot add ipv4 gateway for network device "
3643 "\"%s\" when not assigning an address", current_ifname);
3644 return -1;
3645 }
3646
3647 /* Setup device route if ipv4_gateway_dev is enabled */
3648 if (netdev->ipv4_gateway_dev) {
3649 err = lxc_ipv4_gateway_add(netdev->ifindex, NULL);
3650 if (err < 0) {
3651 SYSERROR("Failed to setup ipv4 gateway to network device \"%s\"",
3652 current_ifname);
3653 return minus_one_set_errno(-err);
3654 }
3655 } else {
3656 /* Check the gateway address is valid */
3657 if (!inet_ntop(AF_INET, netdev->ipv4_gateway, bufinet4, sizeof(bufinet4)))
3658 return minus_one_set_errno(errno);
3659
3660 /* Try adding a default route to the gateway address */
3661 err = lxc_ipv4_gateway_add(netdev->ifindex, netdev->ipv4_gateway);
3662 if (err < 0) {
3663 /* If adding the default route fails, this could be because the
3664 * gateway address is in a different subnet to the container's address.
3665 * To work around this, we try adding a static device route to the
3666 * gateway address first, and then try again.
3667 */
3668 err = lxc_ipv4_dest_add(netdev->ifindex, netdev->ipv4_gateway, 32);
3669 if (err < 0) {
3670 errno = -err;
3671 SYSERROR("Failed to add ipv4 dest \"%s\" for network device \"%s\"",
3672 bufinet4, current_ifname);
3673 return -1;
3674 }
3675
3676 err = lxc_ipv4_gateway_add(netdev->ifindex, netdev->ipv4_gateway);
3677 if (err < 0) {
3678 errno = -err;
3679 SYSERROR("Failed to setup ipv4 gateway \"%s\" for network device \"%s\"",
3680 bufinet4, current_ifname);
3681 return -1;
3682 }
3683 }
3684 }
3685 }
3686
3687 /* setup ipv6 gateway on the interface */
3688 if (netdev->ipv6_gateway || netdev->ipv6_gateway_dev) {
3689 if (!(netdev->flags & IFF_UP)) {
3690 ERROR("Cannot add ipv6 gateway for network device \"%s\" when not bringing up the interface",
3691 current_ifname);
3692 return -1;
3693 }
3694
3695 if (lxc_list_empty(&netdev->ipv6) && !IN6_IS_ADDR_LINKLOCAL(netdev->ipv6_gateway)) {
3696 ERROR("Cannot add ipv6 gateway for network device \"%s\" when not assigning an address",
3697 current_ifname);
3698 return -1;
3699 }
3700
3701 /* Setup device route if ipv6_gateway_dev is enabled */
3702 if (netdev->ipv6_gateway_dev) {
3703 err = lxc_ipv6_gateway_add(netdev->ifindex, NULL);
3704 if (err < 0) {
3705 SYSERROR("Failed to setup ipv6 gateway to network device \"%s\"",
3706 current_ifname);
3707 return minus_one_set_errno(-err);
3708 }
3709 } else {
3710 /* Check the gateway address is valid */
3711 if (!inet_ntop(AF_INET6, netdev->ipv6_gateway, bufinet6, sizeof(bufinet6)))
3712 return minus_one_set_errno(errno);
3713
3714 /* Try adding a default route to the gateway address */
3715 err = lxc_ipv6_gateway_add(netdev->ifindex, netdev->ipv6_gateway);
3716 if (err < 0) {
3717 /* If adding the default route fails, this could be because the
3718 * gateway address is in a different subnet to the container's address.
3719 * To work around this, we try adding a static device route to the
3720 * gateway address first, and then try again.
3721 */
3722 err = lxc_ipv6_dest_add(netdev->ifindex, netdev->ipv6_gateway, 128);
3723 if (err < 0) {
3724 errno = -err;
3725 SYSERROR("Failed to add ipv6 dest \"%s\" for network device \"%s\"",
3726 bufinet6, current_ifname);
3727 return -1;
3728 }
3729
3730 err = lxc_ipv6_gateway_add(netdev->ifindex, netdev->ipv6_gateway);
3731 if (err < 0) {
3732 errno = -err;
3733 SYSERROR("Failed to setup ipv6 gateway \"%s\" for network device \"%s\"",
3734 bufinet6, current_ifname);
3735 return -1;
3736 }
3737 }
3738 }
3739 }
3740
3741 DEBUG("Network device \"%s\" has been setup", current_ifname);
3742
3743 return 0;
3744 }
3745
3746 int lxc_setup_network_in_child_namespaces(const struct lxc_conf *conf,
3747 struct lxc_list *network)
3748 {
3749 struct lxc_list *iterator;
3750
3751 lxc_list_for_each(iterator, network) {
3752 struct lxc_netdev *netdev = iterator->elem;
3753
3754 if (lxc_setup_netdev_in_child_namespaces(netdev)) {
3755 ERROR("Failed to setup netdev");
3756 return -1;
3757 }
3758 }
3759
3760 if (!lxc_list_empty(network))
3761 INFO("Network has been setup");
3762
3763 return 0;
3764 }
3765
3766 int lxc_network_send_veth_names_to_child(struct lxc_handler *handler)
3767 {
3768 struct lxc_list *iterator;
3769 struct lxc_list *network = &handler->conf->network;
3770 int data_sock = handler->data_sock[0];
3771
3772 lxc_list_for_each(iterator, network) {
3773 int ret;
3774 struct lxc_netdev *netdev = iterator->elem;
3775
3776 if (netdev->type != LXC_NET_VETH)
3777 continue;
3778
3779 ret = lxc_send_nointr(data_sock, netdev->name, IFNAMSIZ, MSG_NOSIGNAL);
3780 if (ret < 0)
3781 return -1;
3782
3783 ret = lxc_send_nointr(data_sock, netdev->created_name, IFNAMSIZ, MSG_NOSIGNAL);
3784 if (ret < 0)
3785 return -1;
3786
3787 TRACE("Sent network device name \"%s\" to child", netdev->created_name);
3788 }
3789
3790 return 0;
3791 }
3792
3793 int lxc_network_recv_veth_names_from_parent(struct lxc_handler *handler)
3794 {
3795 struct lxc_list *iterator;
3796 struct lxc_list *network = &handler->conf->network;
3797 int data_sock = handler->data_sock[1];
3798
3799 lxc_list_for_each(iterator, network) {
3800 int ret;
3801 struct lxc_netdev *netdev = iterator->elem;
3802
3803 if (netdev->type != LXC_NET_VETH)
3804 continue;
3805
3806 ret = lxc_recv_nointr(data_sock, netdev->name, IFNAMSIZ, 0);
3807 if (ret < 0)
3808 return -1;
3809
3810 ret = lxc_recv_nointr(data_sock, netdev->created_name, IFNAMSIZ, 0);
3811 if (ret < 0)
3812 return -1;
3813 TRACE("Received network device name \"%s\" from parent", netdev->created_name);
3814 }
3815
3816 return 0;
3817 }
3818
3819 int lxc_network_send_name_and_ifindex_to_parent(struct lxc_handler *handler)
3820 {
3821 struct lxc_list *iterator, *network;
3822 int data_sock = handler->data_sock[0];
3823
3824 if (!handler->am_root)
3825 return 0;
3826
3827 network = &handler->conf->network;
3828 lxc_list_for_each(iterator, network) {
3829 int ret;
3830 struct lxc_netdev *netdev = iterator->elem;
3831
3832 /* Send network device name in the child's namespace to parent. */
3833 ret = lxc_send_nointr(data_sock, netdev->name, IFNAMSIZ, MSG_NOSIGNAL);
3834 if (ret < 0)
3835 return -1;
3836
3837 /* Send network device ifindex in the child's namespace to
3838 * parent.
3839 */
3840 ret = lxc_send_nointr(data_sock, &netdev->ifindex, sizeof(netdev->ifindex), MSG_NOSIGNAL);
3841 if (ret < 0)
3842 return -1;
3843 }
3844
3845 if (!lxc_list_empty(network))
3846 TRACE("Sent network device names and ifindices to parent");
3847
3848 return 0;
3849 }
3850
3851 int lxc_network_recv_name_and_ifindex_from_child(struct lxc_handler *handler)
3852 {
3853 struct lxc_list *iterator, *network;
3854 int data_sock = handler->data_sock[1];
3855
3856 if (!handler->am_root)
3857 return 0;
3858
3859 network = &handler->conf->network;
3860 lxc_list_for_each(iterator, network) {
3861 int ret;
3862 struct lxc_netdev *netdev = iterator->elem;
3863
3864 /* Receive network device name in the child's namespace to
3865 * parent.
3866 */
3867 ret = lxc_recv_nointr(data_sock, netdev->name, IFNAMSIZ, 0);
3868 if (ret < 0)
3869 return -1;
3870
3871 /* Receive network device ifindex in the child's namespace to
3872 * parent.
3873 */
3874 ret = lxc_recv_nointr(data_sock, &netdev->ifindex, sizeof(netdev->ifindex), 0);
3875 if (ret < 0)
3876 return -1;
3877 }
3878
3879 return 0;
3880 }
3881
3882 void lxc_delete_network(struct lxc_handler *handler)
3883 {
3884 bool bret;
3885
3886 if (handler->am_root)
3887 bret = lxc_delete_network_priv(handler);
3888 else
3889 bret = lxc_delete_network_unpriv(handler);
3890 if (!bret)
3891 DEBUG("Failed to delete network devices");
3892 else
3893 DEBUG("Deleted network devices");
3894 }
3895
3896 int lxc_netns_set_nsid(int fd)
3897 {
3898 int ret;
3899 char buf[NLMSG_ALIGN(sizeof(struct nlmsghdr)) +
3900 NLMSG_ALIGN(sizeof(struct rtgenmsg)) +
3901 NLMSG_ALIGN(1024)];
3902 struct nl_handler nlh;
3903 struct nlmsghdr *hdr;
3904 struct rtgenmsg *msg;
3905 int saved_errno;
3906 const __s32 ns_id = -1;
3907 const __u32 netns_fd = fd;
3908
3909 ret = netlink_open(&nlh, NETLINK_ROUTE);
3910 if (ret < 0)
3911 return -1;
3912
3913 memset(buf, 0, sizeof(buf));
3914
3915 #pragma GCC diagnostic push
3916 #pragma GCC diagnostic ignored "-Wcast-align"
3917 hdr = (struct nlmsghdr *)buf;
3918 msg = (struct rtgenmsg *)NLMSG_DATA(hdr);
3919 #pragma GCC diagnostic pop
3920
3921 hdr->nlmsg_len = NLMSG_LENGTH(sizeof(*msg));
3922 hdr->nlmsg_type = RTM_NEWNSID;
3923 hdr->nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK;
3924 hdr->nlmsg_pid = 0;
3925 hdr->nlmsg_seq = RTM_NEWNSID;
3926 msg->rtgen_family = AF_UNSPEC;
3927
3928 ret = addattr(hdr, 1024, __LXC_NETNSA_FD, &netns_fd, sizeof(netns_fd));
3929 if (ret < 0)
3930 goto on_error;
3931
3932 ret = addattr(hdr, 1024, __LXC_NETNSA_NSID, &ns_id, sizeof(ns_id));
3933 if (ret < 0)
3934 goto on_error;
3935
3936 ret = __netlink_transaction(&nlh, hdr, hdr);
3937
3938 on_error:
3939 saved_errno = errno;
3940 netlink_close(&nlh);
3941 errno = saved_errno;
3942
3943 return ret;
3944 }
3945
3946 static int parse_rtattr(struct rtattr *tb[], int max, struct rtattr *rta, int len)
3947 {
3948
3949 memset(tb, 0, sizeof(struct rtattr *) * (max + 1));
3950
3951 while (RTA_OK(rta, len)) {
3952 unsigned short type = rta->rta_type;
3953
3954 if ((type <= max) && (!tb[type]))
3955 tb[type] = rta;
3956
3957 #pragma GCC diagnostic push
3958 #pragma GCC diagnostic ignored "-Wcast-align"
3959 rta = RTA_NEXT(rta, len);
3960 #pragma GCC diagnostic pop
3961 }
3962
3963 return 0;
3964 }
3965
3966 static inline __s32 rta_getattr_s32(const struct rtattr *rta)
3967 {
3968 return *(__s32 *)RTA_DATA(rta);
3969 }
3970
3971 #ifndef NETNS_RTA
3972 #define NETNS_RTA(r) \
3973 ((struct rtattr *)(((char *)(r)) + NLMSG_ALIGN(sizeof(struct rtgenmsg))))
3974 #endif
3975
3976 int lxc_netns_get_nsid(int fd)
3977 {
3978 int ret;
3979 ssize_t len;
3980 char buf[NLMSG_ALIGN(sizeof(struct nlmsghdr)) +
3981 NLMSG_ALIGN(sizeof(struct rtgenmsg)) +
3982 NLMSG_ALIGN(1024)];
3983 struct rtattr *tb[__LXC_NETNSA_MAX + 1];
3984 struct nl_handler nlh;
3985 struct nlmsghdr *hdr;
3986 struct rtgenmsg *msg;
3987 int saved_errno;
3988 __u32 netns_fd = fd;
3989
3990 ret = netlink_open(&nlh, NETLINK_ROUTE);
3991 if (ret < 0)
3992 return -1;
3993
3994 memset(buf, 0, sizeof(buf));
3995
3996 #pragma GCC diagnostic push
3997 #pragma GCC diagnostic ignored "-Wcast-align"
3998 hdr = (struct nlmsghdr *)buf;
3999 msg = (struct rtgenmsg *)NLMSG_DATA(hdr);
4000 #pragma GCC diagnostic pop
4001
4002 hdr->nlmsg_len = NLMSG_LENGTH(sizeof(*msg));
4003 hdr->nlmsg_type = RTM_GETNSID;
4004 hdr->nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK;
4005 hdr->nlmsg_pid = 0;
4006 hdr->nlmsg_seq = RTM_GETNSID;
4007 msg->rtgen_family = AF_UNSPEC;
4008
4009 ret = addattr(hdr, 1024, __LXC_NETNSA_FD, &netns_fd, sizeof(netns_fd));
4010 if (ret == 0)
4011 ret = __netlink_transaction(&nlh, hdr, hdr);
4012
4013 saved_errno = errno;
4014 netlink_close(&nlh);
4015 errno = saved_errno;
4016 if (ret < 0)
4017 return -1;
4018
4019 errno = EINVAL;
4020 msg = NLMSG_DATA(hdr);
4021 len = hdr->nlmsg_len - NLMSG_SPACE(sizeof(*msg));
4022 if (len < 0)
4023 return -1;
4024
4025 #pragma GCC diagnostic push
4026 #pragma GCC diagnostic ignored "-Wcast-align"
4027 parse_rtattr(tb, __LXC_NETNSA_MAX, NETNS_RTA(msg), len);
4028 if (tb[__LXC_NETNSA_NSID])
4029 return rta_getattr_s32(tb[__LXC_NETNSA_NSID]);
4030 #pragma GCC diagnostic pop
4031
4032 return -1;
4033 }
4034
4035 int lxc_create_network(struct lxc_handler *handler)
4036 {
4037 int ret;
4038
4039 /*
4040 * Find gateway addresses from the link device, which is no longer
4041 * accessible inside the container. Do this before creating network
4042 * interfaces, since goto out_delete_net does not work before
4043 * lxc_clone.
4044 */
4045 ret = lxc_find_gateway_addresses(handler);
4046 if (ret) {
4047 ERROR("Failed to find gateway addresses");
4048 return -1;
4049 }
4050
4051 if (handler->am_root) {
4052 ret = lxc_create_network_priv(handler);
4053 if (ret)
4054 return -1;
4055
4056 return lxc_network_move_created_netdev_priv(handler);
4057 }
4058
4059 return lxc_create_network_unpriv(handler);
4060 }