system-linux: fix deletion of ip tunnels (FS#4058)
[project/netifd.git] / system-linux.c
1 /*
2 * netifd - network interface daemon
3 * Copyright (C) 2012 Felix Fietkau <nbd@openwrt.org>
4 * Copyright (C) 2013 Jo-Philipp Wich <jow@openwrt.org>
5 * Copyright (C) 2013 Steven Barth <steven@midlink.org>
6 * Copyright (C) 2014 Gioacchino Mazzurco <gio@eigenlab.org>
7 * Copyright (C) 2017 Matthias Schiffer <mschiffer@universe-factory.net>
8 * Copyright (C) 2018 Hans Dedecker <dedeckeh@gmail.com>
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2
12 * as published by the Free Software Foundation
13 *
14 * This program 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
17 * GNU General Public License for more details.
18 */
19 #define _GNU_SOURCE
20
21 #include <sys/socket.h>
22 #include <sys/ioctl.h>
23 #include <sys/stat.h>
24 #include <sys/syscall.h>
25
26 #include <net/if.h>
27 #include <net/if_arp.h>
28
29 #include <limits.h>
30 #include <arpa/inet.h>
31 #include <netinet/in.h>
32 #include <netinet/ether.h>
33
34 #include <linux/rtnetlink.h>
35 #include <linux/neighbour.h>
36 #include <linux/sockios.h>
37 #include <linux/ip.h>
38 #include <linux/if_addr.h>
39 #include <linux/if_link.h>
40 #include <linux/if_vlan.h>
41 #include <linux/if_bridge.h>
42 #include <linux/if_tunnel.h>
43 #include <linux/ip6_tunnel.h>
44 #include <linux/ethtool.h>
45 #include <linux/fib_rules.h>
46 #include <linux/veth.h>
47 #include <linux/version.h>
48
49 #include <sched.h>
50
51 #ifndef RTN_FAILED_POLICY
52 #define RTN_FAILED_POLICY 12
53 #endif
54
55 #ifndef IFA_F_NOPREFIXROUTE
56 #define IFA_F_NOPREFIXROUTE 0x200
57 #endif
58
59 #ifndef IFA_FLAGS
60 #define IFA_FLAGS (IFA_MULTICAST + 1)
61 #endif
62
63 #include <string.h>
64 #include <fcntl.h>
65 #include <glob.h>
66 #include <time.h>
67 #include <unistd.h>
68
69 #include <netlink/msg.h>
70 #include <netlink/attr.h>
71 #include <netlink/socket.h>
72 #include <libubox/uloop.h>
73
74 #include "netifd.h"
75 #include "device.h"
76 #include "system.h"
77 #include "utils.h"
78
79 struct event_socket {
80 struct uloop_fd uloop;
81 struct nl_sock *sock;
82 int bufsize;
83 };
84
85 static int sock_ioctl = -1;
86 static struct nl_sock *sock_rtnl = NULL;
87
88 static int cb_rtnl_event(struct nl_msg *msg, void *arg);
89 static void handle_hotplug_event(struct uloop_fd *u, unsigned int events);
90 static int system_add_proto_tunnel(const char *name, const uint8_t proto,
91 const unsigned int link, struct blob_attr **tb);
92
93 static char dev_buf[256];
94
95 static void
96 handler_nl_event(struct uloop_fd *u, unsigned int events)
97 {
98 struct event_socket *ev = container_of(u, struct event_socket, uloop);
99 int err;
100 socklen_t errlen = sizeof(err);
101
102 if (!u->error) {
103 nl_recvmsgs_default(ev->sock);
104 return;
105 }
106
107 if (getsockopt(u->fd, SOL_SOCKET, SO_ERROR, (void *)&err, &errlen))
108 goto abort;
109
110 switch(err) {
111 case ENOBUFS:
112 /* Increase rx buffer size on netlink socket */
113 ev->bufsize *= 2;
114 if (nl_socket_set_buffer_size(ev->sock, ev->bufsize, 0))
115 goto abort;
116
117 /* Request full dump since some info got dropped */
118 struct rtgenmsg msg = { .rtgen_family = AF_UNSPEC };
119 nl_send_simple(ev->sock, RTM_GETLINK, NLM_F_DUMP, &msg, sizeof(msg));
120 break;
121
122 default:
123 goto abort;
124 }
125 u->error = false;
126 return;
127
128 abort:
129 uloop_fd_delete(&ev->uloop);
130 return;
131 }
132
133 static struct nl_sock *
134 create_socket(int protocol, int groups)
135 {
136 struct nl_sock *sock;
137
138 sock = nl_socket_alloc();
139 if (!sock)
140 return NULL;
141
142 if (groups)
143 nl_join_groups(sock, groups);
144
145 if (nl_connect(sock, protocol)) {
146 nl_socket_free(sock);
147 return NULL;
148 }
149
150 return sock;
151 }
152
153 static bool
154 create_raw_event_socket(struct event_socket *ev, int protocol, int groups,
155 uloop_fd_handler cb, int flags)
156 {
157 ev->sock = create_socket(protocol, groups);
158 if (!ev->sock)
159 return false;
160
161 ev->uloop.fd = nl_socket_get_fd(ev->sock);
162 ev->uloop.cb = cb;
163 if (uloop_fd_add(&ev->uloop, ULOOP_READ|flags))
164 return false;
165
166 return true;
167 }
168
169 static bool
170 create_event_socket(struct event_socket *ev, int protocol,
171 int (*cb)(struct nl_msg *msg, void *arg))
172 {
173 if (!create_raw_event_socket(ev, protocol, 0, handler_nl_event, ULOOP_ERROR_CB))
174 return false;
175
176 /* Install the valid custom callback handler */
177 nl_socket_modify_cb(ev->sock, NL_CB_VALID, NL_CB_CUSTOM, cb, NULL);
178
179 /* Disable sequence number checking on event sockets */
180 nl_socket_disable_seq_check(ev->sock);
181
182 /* Increase rx buffer size to 65K on event sockets */
183 ev->bufsize = 65535;
184 if (nl_socket_set_buffer_size(ev->sock, ev->bufsize, 0))
185 return false;
186
187 return true;
188 }
189
190 static bool
191 create_hotplug_event_socket(struct event_socket *ev, int protocol,
192 void (*cb)(struct uloop_fd *u, unsigned int events))
193 {
194 if (!create_raw_event_socket(ev, protocol, 1, cb, ULOOP_ERROR_CB))
195 return false;
196
197 /* Increase rx buffer size to 65K on event sockets */
198 ev->bufsize = 65535;
199 if (nl_socket_set_buffer_size(ev->sock, ev->bufsize, 0))
200 return false;
201
202 return true;
203 }
204
205 static bool
206 system_rtn_aton(const char *src, unsigned int *dst)
207 {
208 char *e;
209 unsigned int n;
210
211 if (!strcmp(src, "local"))
212 n = RTN_LOCAL;
213 else if (!strcmp(src, "nat"))
214 n = RTN_NAT;
215 else if (!strcmp(src, "broadcast"))
216 n = RTN_BROADCAST;
217 else if (!strcmp(src, "anycast"))
218 n = RTN_ANYCAST;
219 else if (!strcmp(src, "multicast"))
220 n = RTN_MULTICAST;
221 else if (!strcmp(src, "prohibit"))
222 n = RTN_PROHIBIT;
223 else if (!strcmp(src, "unreachable"))
224 n = RTN_UNREACHABLE;
225 else if (!strcmp(src, "blackhole"))
226 n = RTN_BLACKHOLE;
227 else if (!strcmp(src, "xresolve"))
228 n = RTN_XRESOLVE;
229 else if (!strcmp(src, "unicast"))
230 n = RTN_UNICAST;
231 else if (!strcmp(src, "throw"))
232 n = RTN_THROW;
233 else if (!strcmp(src, "failed_policy"))
234 n = RTN_FAILED_POLICY;
235 else {
236 n = strtoul(src, &e, 0);
237 if (!e || *e || e == src || n > 255)
238 return false;
239 }
240
241 *dst = n;
242 return true;
243 }
244
245 static bool
246 system_tos_aton(const char *src, unsigned *dst)
247 {
248 char *e;
249
250 *dst = strtoul(src, &e, 16);
251 if (e == src || *e || *dst > 255)
252 return false;
253
254 return true;
255 }
256
257 int system_init(void)
258 {
259 static struct event_socket rtnl_event;
260 static struct event_socket hotplug_event;
261
262 sock_ioctl = socket(AF_LOCAL, SOCK_DGRAM, 0);
263 system_fd_set_cloexec(sock_ioctl);
264
265 /* Prepare socket for routing / address control */
266 sock_rtnl = create_socket(NETLINK_ROUTE, 0);
267 if (!sock_rtnl)
268 return -1;
269
270 if (!create_event_socket(&rtnl_event, NETLINK_ROUTE, cb_rtnl_event))
271 return -1;
272
273 if (!create_hotplug_event_socket(&hotplug_event, NETLINK_KOBJECT_UEVENT,
274 handle_hotplug_event))
275 return -1;
276
277 /* Receive network link events form kernel */
278 nl_socket_add_membership(rtnl_event.sock, RTNLGRP_LINK);
279
280 return 0;
281 }
282
283 static void system_set_sysctl(const char *path, const char *val)
284 {
285 int fd;
286
287 fd = open(path, O_WRONLY);
288 if (fd < 0)
289 return;
290
291 if (write(fd, val, strlen(val))) {}
292 close(fd);
293 }
294
295 static void system_set_dev_sysctl(const char *path, const char *device, const char *val)
296 {
297 snprintf(dev_buf, sizeof(dev_buf), path, device);
298 system_set_sysctl(dev_buf, val);
299 }
300
301 static void system_set_disable_ipv6(struct device *dev, const char *val)
302 {
303 system_set_dev_sysctl("/proc/sys/net/ipv6/conf/%s/disable_ipv6", dev->ifname, val);
304 }
305
306 static void system_set_ip6segmentrouting(struct device *dev, const char *val)
307 {
308 system_set_dev_sysctl("/proc/sys/net/ipv6/conf/%s/seg6_enabled", dev->ifname, val);
309 }
310
311 static void system_set_rpfilter(struct device *dev, const char *val)
312 {
313 system_set_dev_sysctl("/proc/sys/net/ipv4/conf/%s/rp_filter", dev->ifname, val);
314 }
315
316 static void system_set_acceptlocal(struct device *dev, const char *val)
317 {
318 system_set_dev_sysctl("/proc/sys/net/ipv4/conf/%s/accept_local", dev->ifname, val);
319 }
320
321 static void system_set_igmpversion(struct device *dev, const char *val)
322 {
323 system_set_dev_sysctl("/proc/sys/net/ipv4/conf/%s/force_igmp_version", dev->ifname, val);
324 }
325
326 static void system_set_mldversion(struct device *dev, const char *val)
327 {
328 system_set_dev_sysctl("/proc/sys/net/ipv6/conf/%s/force_mld_version", dev->ifname, val);
329 }
330
331 static void system_set_neigh4reachabletime(struct device *dev, const char *val)
332 {
333 system_set_dev_sysctl("/proc/sys/net/ipv4/neigh/%s/base_reachable_time_ms", dev->ifname, val);
334 }
335
336 static void system_set_neigh6reachabletime(struct device *dev, const char *val)
337 {
338 system_set_dev_sysctl("/proc/sys/net/ipv6/neigh/%s/base_reachable_time_ms", dev->ifname, val);
339 }
340
341 static void system_set_neigh4gcstaletime(struct device *dev, const char *val)
342 {
343 system_set_dev_sysctl("/proc/sys/net/ipv4/neigh/%s/gc_stale_time", dev->ifname, val);
344 }
345
346 static void system_set_neigh6gcstaletime(struct device *dev, const char *val)
347 {
348 system_set_dev_sysctl("/proc/sys/net/ipv6/neigh/%s/gc_stale_time", dev->ifname, val);
349 }
350
351 static void system_set_neigh4locktime(struct device *dev, const char *val)
352 {
353 system_set_dev_sysctl("/proc/sys/net/ipv4/neigh/%s/locktime", dev->ifname, val);
354 }
355
356 static void system_set_dadtransmits(struct device *dev, const char *val)
357 {
358 system_set_dev_sysctl("/proc/sys/net/ipv6/conf/%s/dad_transmits", dev->ifname, val);
359 }
360
361 static void system_set_sendredirects(struct device *dev, const char *val)
362 {
363 system_set_dev_sysctl("/proc/sys/net/ipv4/conf/%s/send_redirects", dev->ifname, val);
364 }
365
366 static void system_set_drop_v4_unicast_in_l2_multicast(struct device *dev, const char *val)
367 {
368 system_set_dev_sysctl("/proc/sys/net/ipv4/conf/%s/drop_unicast_in_l2_multicast", dev->ifname, val);
369 }
370
371 static void system_set_drop_v6_unicast_in_l2_multicast(struct device *dev, const char *val)
372 {
373 system_set_dev_sysctl("/proc/sys/net/ipv6/conf/%s/drop_unicast_in_l2_multicast", dev->ifname, val);
374 }
375
376 static void system_set_drop_gratuitous_arp(struct device *dev, const char *val)
377 {
378 system_set_dev_sysctl("/proc/sys/net/ipv4/conf/%s/drop_gratuitous_arp", dev->ifname, val);
379 }
380
381 static void system_set_drop_unsolicited_na(struct device *dev, const char *val)
382 {
383 system_set_dev_sysctl("/proc/sys/net/ipv6/conf/%s/drop_unsolicited_na", dev->ifname, val);
384 }
385
386 static void system_set_arp_accept(struct device *dev, const char *val)
387 {
388 system_set_dev_sysctl("/proc/sys/net/ipv4/conf/%s/arp_accept", dev->ifname, val);
389 }
390
391 static void system_bridge_set_multicast_to_unicast(struct device *dev, const char *val)
392 {
393 system_set_dev_sysctl("/sys/class/net/%s/brport/multicast_to_unicast", dev->ifname, val);
394 }
395
396 static void system_bridge_set_multicast_fast_leave(struct device *dev, const char *val)
397 {
398 system_set_dev_sysctl("/sys/class/net/%s/brport/multicast_fast_leave", dev->ifname, val);
399 }
400
401 static void system_bridge_set_hairpin_mode(struct device *dev, const char *val)
402 {
403 system_set_dev_sysctl("/sys/class/net/%s/brport/hairpin_mode", dev->ifname, val);
404 }
405
406 static void system_bridge_set_isolated(struct device *dev, const char *val)
407 {
408 system_set_dev_sysctl("/sys/class/net/%s/brport/isolated", dev->ifname, val);
409 }
410
411 static void system_bridge_set_multicast_router(struct device *dev, const char *val, bool bridge)
412 {
413 system_set_dev_sysctl(bridge ? "/sys/class/net/%s/bridge/multicast_router" :
414 "/sys/class/net/%s/brport/multicast_router",
415 dev->ifname, val);
416 }
417
418 static void system_bridge_set_robustness(struct device *dev, const char *val)
419 {
420 system_set_dev_sysctl("/sys/devices/virtual/net/%s/bridge/multicast_startup_query_count",
421 dev->ifname, val);
422 system_set_dev_sysctl("/sys/devices/virtual/net/%s/bridge/multicast_last_member_count",
423 dev->ifname, val);
424 }
425
426 static void system_bridge_set_query_interval(struct device *dev, const char *val)
427 {
428 system_set_dev_sysctl("/sys/devices/virtual/net/%s/bridge/multicast_query_interval",
429 dev->ifname, val);
430 }
431
432 static void system_bridge_set_query_response_interval(struct device *dev, const char *val)
433 {
434 system_set_dev_sysctl("/sys/devices/virtual/net/%s/bridge/multicast_query_response_interval",
435 dev->ifname, val);
436 }
437
438 static void system_bridge_set_last_member_interval(struct device *dev, const char *val)
439 {
440 system_set_dev_sysctl("/sys/devices/virtual/net/%s/bridge/multicast_last_member_interval",
441 dev->ifname, val);
442 }
443
444 static void system_bridge_set_membership_interval(struct device *dev, const char *val)
445 {
446 system_set_dev_sysctl("/sys/devices/virtual/net/%s/bridge/multicast_membership_interval",
447 dev->ifname, val);
448 }
449
450 static void system_bridge_set_other_querier_timeout(struct device *dev, const char *val)
451 {
452 system_set_dev_sysctl("/sys/devices/virtual/net/%s/bridge/multicast_querier_interval",
453 dev->ifname, val);
454 }
455
456 static void system_bridge_set_startup_query_interval(struct device *dev, const char *val)
457 {
458 system_set_dev_sysctl("/sys/devices/virtual/net/%s/bridge/multicast_startup_query_interval",
459 dev->ifname, val);
460 }
461
462 static void system_bridge_set_stp_state(struct device *dev, const char *val)
463 {
464 system_set_dev_sysctl("/sys/devices/virtual/net/%s/bridge/stp_state", dev->ifname, val);
465 }
466
467 static void system_bridge_set_forward_delay(struct device *dev, const char *val)
468 {
469 system_set_dev_sysctl("/sys/devices/virtual/net/%s/bridge/forward_delay", dev->ifname, val);
470 }
471
472 static void system_bridge_set_priority(struct device *dev, const char *val)
473 {
474 system_set_dev_sysctl("/sys/devices/virtual/net/%s/bridge/priority", dev->ifname, val);
475 }
476
477 static void system_bridge_set_ageing_time(struct device *dev, const char *val)
478 {
479 system_set_dev_sysctl("/sys/devices/virtual/net/%s/bridge/ageing_time", dev->ifname, val);
480 }
481
482 static void system_bridge_set_hello_time(struct device *dev, const char *val)
483 {
484 system_set_dev_sysctl("/sys/devices/virtual/net/%s/bridge/hello_time", dev->ifname, val);
485 }
486
487 static void system_bridge_set_max_age(struct device *dev, const char *val)
488 {
489 system_set_dev_sysctl("/sys/devices/virtual/net/%s/bridge/max_age", dev->ifname, val);
490 }
491
492 static void system_bridge_set_learning(struct device *dev, const char *val)
493 {
494 system_set_dev_sysctl("/sys/class/net/%s/brport/learning", dev->ifname, val);
495 }
496
497 static void system_bridge_set_unicast_flood(struct device *dev, const char *val)
498 {
499 system_set_dev_sysctl("/sys/class/net/%s/brport/unicast_flood", dev->ifname, val);
500 }
501
502 static void system_bridge_set_vlan_filtering(struct device *dev, const char *val)
503 {
504 system_set_dev_sysctl("/sys/devices/virtual/net/%s/bridge/vlan_filtering", dev->ifname, val);
505 }
506
507 static int system_get_sysctl(const char *path, char *buf, const size_t buf_sz)
508 {
509 int fd = -1, ret = -1;
510
511 fd = open(path, O_RDONLY);
512 if (fd < 0)
513 goto out;
514
515 ssize_t len = read(fd, buf, buf_sz - 1);
516 if (len < 0)
517 goto out;
518
519 ret = buf[len] = 0;
520
521 out:
522 if (fd >= 0)
523 close(fd);
524
525 return ret;
526 }
527
528 static int
529 system_get_dev_sysctl(const char *path, const char *device, char *buf, const size_t buf_sz)
530 {
531 snprintf(dev_buf, sizeof(dev_buf), path, device);
532 return system_get_sysctl(dev_buf, buf, buf_sz);
533 }
534
535 static int system_get_disable_ipv6(struct device *dev, char *buf, const size_t buf_sz)
536 {
537 return system_get_dev_sysctl("/proc/sys/net/ipv6/conf/%s/disable_ipv6",
538 dev->ifname, buf, buf_sz);
539 }
540
541 static int system_get_ip6segmentrouting(struct device *dev, char *buf, const size_t buf_sz)
542 {
543 return system_get_dev_sysctl("/proc/sys/net/ipv6/conf/%s/seg6_enabled",
544 dev->ifname, buf, buf_sz);
545 }
546
547 static int system_get_rpfilter(struct device *dev, char *buf, const size_t buf_sz)
548 {
549 return system_get_dev_sysctl("/proc/sys/net/ipv4/conf/%s/rp_filter",
550 dev->ifname, buf, buf_sz);
551 }
552
553 static int system_get_acceptlocal(struct device *dev, char *buf, const size_t buf_sz)
554 {
555 return system_get_dev_sysctl("/proc/sys/net/ipv4/conf/%s/accept_local",
556 dev->ifname, buf, buf_sz);
557 }
558
559 static int system_get_igmpversion(struct device *dev, char *buf, const size_t buf_sz)
560 {
561 return system_get_dev_sysctl("/proc/sys/net/ipv4/conf/%s/force_igmp_version",
562 dev->ifname, buf, buf_sz);
563 }
564
565 static int system_get_mldversion(struct device *dev, char *buf, const size_t buf_sz)
566 {
567 return system_get_dev_sysctl("/proc/sys/net/ipv6/conf/%s/force_mld_version",
568 dev->ifname, buf, buf_sz);
569 }
570
571 static int system_get_neigh4reachabletime(struct device *dev, char *buf, const size_t buf_sz)
572 {
573 return system_get_dev_sysctl("/proc/sys/net/ipv4/neigh/%s/base_reachable_time_ms",
574 dev->ifname, buf, buf_sz);
575 }
576
577 static int system_get_neigh6reachabletime(struct device *dev, char *buf, const size_t buf_sz)
578 {
579 return system_get_dev_sysctl("/proc/sys/net/ipv6/neigh/%s/base_reachable_time_ms",
580 dev->ifname, buf, buf_sz);
581 }
582
583 static int system_get_neigh4gcstaletime(struct device *dev, char *buf, const size_t buf_sz)
584 {
585 return system_get_dev_sysctl("/proc/sys/net/ipv4/neigh/%s/gc_stale_time",
586 dev->ifname, buf, buf_sz);
587 }
588
589 static int system_get_neigh6gcstaletime(struct device *dev, char *buf, const size_t buf_sz)
590 {
591 return system_get_dev_sysctl("/proc/sys/net/ipv6/neigh/%s/gc_stale_time",
592 dev->ifname, buf, buf_sz);
593 }
594
595 static int system_get_neigh4locktime(struct device *dev, char *buf, const size_t buf_sz)
596 {
597 return system_get_dev_sysctl("/proc/sys/net/ipv4/neigh/%s/locktime",
598 dev->ifname, buf, buf_sz);
599 }
600
601 static int system_get_dadtransmits(struct device *dev, char *buf, const size_t buf_sz)
602 {
603 return system_get_dev_sysctl("/proc/sys/net/ipv6/conf/%s/dad_transmits",
604 dev->ifname, buf, buf_sz);
605 }
606
607 static int system_get_sendredirects(struct device *dev, char *buf, const size_t buf_sz)
608 {
609 return system_get_dev_sysctl("/proc/sys/net/ipv4/conf/%s/send_redirects",
610 dev->ifname, buf, buf_sz);
611 }
612
613
614 static int system_get_drop_v4_unicast_in_l2_multicast(struct device *dev, char *buf, const size_t buf_sz)
615 {
616 return system_get_dev_sysctl("/proc/sys/net/ipv4/conf/%s/drop_unicast_in_l2_multicast",
617 dev->ifname, buf, buf_sz);
618 }
619
620 static int system_get_drop_v6_unicast_in_l2_multicast(struct device *dev, char *buf, const size_t buf_sz)
621 {
622 return system_get_dev_sysctl("/proc/sys/net/ipv6/conf/%s/drop_unicast_in_l2_multicast",
623 dev->ifname, buf, buf_sz);
624 }
625
626 static int system_get_drop_gratuitous_arp(struct device *dev, char *buf, const size_t buf_sz)
627 {
628 return system_get_dev_sysctl("/proc/sys/net/ipv4/conf/%s/drop_gratuitous_arp",
629 dev->ifname, buf, buf_sz);
630 }
631
632 static int system_get_drop_unsolicited_na(struct device *dev, char *buf, const size_t buf_sz)
633 {
634 return system_get_dev_sysctl("/proc/sys/net/ipv6/conf/%s/drop_unsolicited_na",
635 dev->ifname, buf, buf_sz);
636 }
637
638 static int system_get_arp_accept(struct device *dev, char *buf, const size_t buf_sz)
639 {
640 return system_get_dev_sysctl("/proc/sys/net/ipv4/conf/%s/arp_accept",
641 dev->ifname, buf, buf_sz);
642 }
643
644 /* Evaluate netlink messages */
645 static int cb_rtnl_event(struct nl_msg *msg, void *arg)
646 {
647 struct nlmsghdr *nh = nlmsg_hdr(msg);
648 struct nlattr *nla[__IFLA_MAX];
649 int link_state = 0;
650 char buf[10];
651
652 if (nh->nlmsg_type != RTM_NEWLINK)
653 goto out;
654
655 nlmsg_parse(nh, sizeof(struct ifinfomsg), nla, __IFLA_MAX - 1, NULL);
656 if (!nla[IFLA_IFNAME])
657 goto out;
658
659 struct device *dev = device_find(nla_data(nla[IFLA_IFNAME]));
660 if (!dev)
661 goto out;
662
663 if (!system_get_dev_sysctl("/sys/class/net/%s/carrier", dev->ifname, buf, sizeof(buf)))
664 link_state = strtoul(buf, NULL, 0);
665
666 if (dev->type == &simple_device_type)
667 device_set_present(dev, true);
668
669 device_set_link(dev, link_state ? true : false);
670
671 out:
672 return 0;
673 }
674
675 static void
676 handle_hotplug_msg(char *data, int size)
677 {
678 const char *subsystem = NULL, *interface = NULL, *interface_old = NULL;
679 char *cur, *end, *sep;
680 int skip;
681 bool add;
682
683 if (!strncmp(data, "add@", 4) || !strncmp(data, "move@", 5))
684 add = true;
685 else if (!strncmp(data, "remove@", 7))
686 add = false;
687 else
688 return;
689
690 skip = strlen(data) + 1;
691 end = data + size;
692
693 for (cur = data + skip; cur < end; cur += skip) {
694 skip = strlen(cur) + 1;
695
696 sep = strchr(cur, '=');
697 if (!sep)
698 continue;
699
700 *sep = 0;
701 if (!strcmp(cur, "INTERFACE"))
702 interface = sep + 1;
703 else if (!strcmp(cur, "SUBSYSTEM")) {
704 subsystem = sep + 1;
705 if (strcmp(subsystem, "net") != 0)
706 return;
707 } else if (!strcmp(cur, "DEVPATH_OLD")) {
708 interface_old = strrchr(sep + 1, '/');
709 if (interface_old)
710 interface_old++;
711 }
712 }
713
714 if (!subsystem || !interface)
715 return;
716
717 if (interface_old)
718 device_hotplug_event(interface_old, false);
719
720 device_hotplug_event(interface, add);
721 }
722
723 static void
724 handle_hotplug_event(struct uloop_fd *u, unsigned int events)
725 {
726 struct event_socket *ev = container_of(u, struct event_socket, uloop);
727 struct sockaddr_nl nla;
728 unsigned char *buf = NULL;
729 int size;
730 int err;
731 socklen_t errlen = sizeof(err);
732
733 if (!u->error) {
734 while ((size = nl_recv(ev->sock, &nla, &buf, NULL)) > 0) {
735 if (nla.nl_pid == 0)
736 handle_hotplug_msg((char *) buf, size);
737
738 free(buf);
739 }
740 return;
741 }
742
743 if (getsockopt(u->fd, SOL_SOCKET, SO_ERROR, (void *)&err, &errlen))
744 goto abort;
745
746 switch(err) {
747 case ENOBUFS:
748 /* Increase rx buffer size on netlink socket */
749 ev->bufsize *= 2;
750 if (nl_socket_set_buffer_size(ev->sock, ev->bufsize, 0))
751 goto abort;
752 break;
753
754 default:
755 goto abort;
756 }
757 u->error = false;
758 return;
759
760 abort:
761 uloop_fd_delete(&ev->uloop);
762 return;
763 }
764
765 static int system_rtnl_call(struct nl_msg *msg)
766 {
767 int ret;
768
769 ret = nl_send_auto_complete(sock_rtnl, msg);
770 nlmsg_free(msg);
771
772 if (ret < 0)
773 return ret;
774
775 return nl_wait_for_ack(sock_rtnl);
776 }
777
778 int system_bridge_delbr(struct device *bridge)
779 {
780 return ioctl(sock_ioctl, SIOCBRDELBR, bridge->ifname);
781 }
782
783 static int system_bridge_if(const char *bridge, struct device *dev, int cmd, void *data)
784 {
785 struct ifreq ifr;
786
787 memset(&ifr, 0, sizeof(ifr));
788 if (dev)
789 ifr.ifr_ifindex = dev->ifindex;
790 else
791 ifr.ifr_data = data;
792 strncpy(ifr.ifr_name, bridge, sizeof(ifr.ifr_name) - 1);
793 return ioctl(sock_ioctl, cmd, &ifr);
794 }
795
796 static bool system_is_bridge(const char *name, char *buf, int buflen)
797 {
798 struct stat st;
799
800 snprintf(buf, buflen, "/sys/devices/virtual/net/%s/bridge", name);
801 if (stat(buf, &st) < 0)
802 return false;
803
804 return true;
805 }
806
807 static char *system_get_bridge(const char *name, char *buf, int buflen)
808 {
809 char *path;
810 ssize_t len = -1;
811 glob_t gl;
812
813 snprintf(buf, buflen, "/sys/devices/virtual/net/*/brif/%s/bridge", name);
814 if (glob(buf, GLOB_NOSORT, NULL, &gl) < 0)
815 return NULL;
816
817 if (gl.gl_pathc > 0)
818 len = readlink(gl.gl_pathv[0], buf, buflen);
819
820 globfree(&gl);
821
822 if (len < 0)
823 return NULL;
824
825 buf[len] = 0;
826 path = strrchr(buf, '/');
827 if (!path)
828 return NULL;
829
830 return path + 1;
831 }
832
833 static void
834 system_bridge_set_wireless(struct device *bridge, struct device *dev)
835 {
836 bool mcast_to_ucast = dev->wireless_ap;
837 bool hairpin = true;
838
839 if (bridge->settings.flags & DEV_OPT_MULTICAST_TO_UNICAST &&
840 !bridge->settings.multicast_to_unicast)
841 mcast_to_ucast = false;
842
843 if (!mcast_to_ucast || dev->wireless_isolate)
844 hairpin = false;
845
846 system_bridge_set_multicast_to_unicast(dev, mcast_to_ucast ? "1" : "0");
847 system_bridge_set_hairpin_mode(dev, hairpin ? "1" : "0");
848 }
849
850 int system_bridge_addif(struct device *bridge, struct device *dev)
851 {
852 char buf[64];
853 char *oldbr;
854 int tries = 0;
855 int ret;
856
857 retry:
858 ret = 0;
859 oldbr = system_get_bridge(dev->ifname, dev_buf, sizeof(dev_buf));
860 if (!oldbr || strcmp(oldbr, bridge->ifname) != 0) {
861 ret = system_bridge_if(bridge->ifname, dev, SIOCBRADDIF, NULL);
862 tries++;
863 D(SYSTEM, "Failed to add device '%s' to bridge '%s' (tries=%d): %s\n",
864 dev->ifname, bridge->ifname, tries, strerror(errno));
865 if (tries <= 3)
866 goto retry;
867 }
868
869 if (dev->wireless)
870 system_bridge_set_wireless(bridge, dev);
871
872 if (dev->settings.flags & DEV_OPT_MULTICAST_ROUTER) {
873 snprintf(buf, sizeof(buf), "%u", dev->settings.multicast_router);
874 system_bridge_set_multicast_router(dev, buf, false);
875 }
876
877 if (dev->settings.flags & DEV_OPT_MULTICAST_FAST_LEAVE &&
878 dev->settings.multicast_fast_leave)
879 system_bridge_set_multicast_fast_leave(dev, "1");
880
881 if (dev->settings.flags & DEV_OPT_LEARNING &&
882 !dev->settings.learning)
883 system_bridge_set_learning(dev, "0");
884
885 if (dev->settings.flags & DEV_OPT_UNICAST_FLOOD &&
886 !dev->settings.unicast_flood)
887 system_bridge_set_unicast_flood(dev, "0");
888
889 if (dev->settings.flags & DEV_OPT_ISOLATE &&
890 dev->settings.isolate)
891 system_bridge_set_isolated(dev, "1");
892
893 return ret;
894 }
895
896 int system_bridge_delif(struct device *bridge, struct device *dev)
897 {
898 return system_bridge_if(bridge->ifname, dev, SIOCBRDELIF, NULL);
899 }
900
901 int system_bridge_vlan(const char *iface, uint16_t vid, bool add, unsigned int vflags)
902 {
903 struct ifinfomsg ifi = { .ifi_family = PF_BRIDGE, };
904 struct bridge_vlan_info vinfo = { .vid = vid, };
905 unsigned short flags = 0;
906 struct nlattr *afspec;
907 struct nl_msg *nlm;
908 int ret = 0;
909
910 ifi.ifi_index = if_nametoindex(iface);
911 if (!ifi.ifi_index)
912 return -1;
913
914 nlm = nlmsg_alloc_simple(add ? RTM_SETLINK : RTM_DELLINK, NLM_F_REQUEST);
915 if (!nlm)
916 return -1;
917
918 nlmsg_append(nlm, &ifi, sizeof(ifi), 0);
919
920 if (vflags & BRVLAN_F_SELF)
921 flags |= BRIDGE_FLAGS_SELF;
922
923 if (vflags & BRVLAN_F_PVID)
924 vinfo.flags |= BRIDGE_VLAN_INFO_PVID;
925
926 if (vflags & BRVLAN_F_UNTAGGED)
927 vinfo.flags |= BRIDGE_VLAN_INFO_UNTAGGED;
928
929 afspec = nla_nest_start(nlm, IFLA_AF_SPEC);
930 if (!afspec) {
931 ret = -ENOMEM;
932 goto failure;
933 }
934
935 if (flags)
936 nla_put_u16(nlm, IFLA_BRIDGE_FLAGS, flags);
937
938 nla_put(nlm, IFLA_BRIDGE_VLAN_INFO, sizeof(vinfo), &vinfo);
939 nla_nest_end(nlm, afspec);
940
941 return system_rtnl_call(nlm);
942
943 failure:
944 nlmsg_free(nlm);
945 return ret;
946 }
947
948 int system_if_resolve(struct device *dev)
949 {
950 struct ifreq ifr;
951
952 memset(&ifr, 0, sizeof(ifr));
953 strncpy(ifr.ifr_name, dev->ifname, sizeof(ifr.ifr_name) - 1);
954 if (!ioctl(sock_ioctl, SIOCGIFINDEX, &ifr))
955 return ifr.ifr_ifindex;
956 else
957 return 0;
958 }
959
960 static int system_if_flags(const char *ifname, unsigned add, unsigned rem)
961 {
962 struct ifreq ifr;
963
964 memset(&ifr, 0, sizeof(ifr));
965 strncpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name) - 1);
966 if (ioctl(sock_ioctl, SIOCGIFFLAGS, &ifr) < 0)
967 return -1;
968
969 ifr.ifr_flags |= add;
970 ifr.ifr_flags &= ~rem;
971 return ioctl(sock_ioctl, SIOCSIFFLAGS, &ifr);
972 }
973
974 struct clear_data {
975 struct nl_msg *msg;
976 struct device *dev;
977 int type;
978 int size;
979 int af;
980 };
981
982
983 static bool check_ifaddr(struct nlmsghdr *hdr, int ifindex)
984 {
985 struct ifaddrmsg *ifa = NLMSG_DATA(hdr);
986
987 return ifa->ifa_index == ifindex;
988 }
989
990 static bool check_route(struct nlmsghdr *hdr, int ifindex)
991 {
992 struct rtmsg *r = NLMSG_DATA(hdr);
993 struct nlattr *tb[__RTA_MAX];
994
995 if (r->rtm_protocol == RTPROT_KERNEL &&
996 r->rtm_family == AF_INET6)
997 return false;
998
999 nlmsg_parse(hdr, sizeof(struct rtmsg), tb, __RTA_MAX - 1, NULL);
1000 if (!tb[RTA_OIF])
1001 return false;
1002
1003 return *(int *)RTA_DATA(tb[RTA_OIF]) == ifindex;
1004 }
1005
1006 static bool check_rule(struct nlmsghdr *hdr, int ifindex)
1007 {
1008 return true;
1009 }
1010
1011 static int cb_clear_event(struct nl_msg *msg, void *arg)
1012 {
1013 struct clear_data *clr = arg;
1014 struct nlmsghdr *hdr = nlmsg_hdr(msg);
1015 bool (*cb)(struct nlmsghdr *, int ifindex);
1016 int type, ret;
1017
1018 switch(clr->type) {
1019 case RTM_GETADDR:
1020 type = RTM_DELADDR;
1021 if (hdr->nlmsg_type != RTM_NEWADDR)
1022 return NL_SKIP;
1023
1024 cb = check_ifaddr;
1025 break;
1026 case RTM_GETROUTE:
1027 type = RTM_DELROUTE;
1028 if (hdr->nlmsg_type != RTM_NEWROUTE)
1029 return NL_SKIP;
1030
1031 cb = check_route;
1032 break;
1033 case RTM_GETRULE:
1034 type = RTM_DELRULE;
1035 if (hdr->nlmsg_type != RTM_NEWRULE)
1036 return NL_SKIP;
1037
1038 cb = check_rule;
1039 break;
1040 default:
1041 return NL_SKIP;
1042 }
1043
1044 if (!cb(hdr, clr->dev ? clr->dev->ifindex : 0))
1045 return NL_SKIP;
1046
1047 if (type == RTM_DELRULE)
1048 D(SYSTEM, "Remove a rule\n");
1049 else
1050 D(SYSTEM, "Remove %s from device %s\n",
1051 type == RTM_DELADDR ? "an address" : "a route",
1052 clr->dev->ifname);
1053
1054 memcpy(nlmsg_hdr(clr->msg), hdr, hdr->nlmsg_len);
1055 hdr = nlmsg_hdr(clr->msg);
1056 hdr->nlmsg_type = type;
1057 hdr->nlmsg_flags = NLM_F_REQUEST;
1058
1059 nl_socket_disable_auto_ack(sock_rtnl);
1060 ret = nl_send_auto_complete(sock_rtnl, clr->msg);
1061 if (ret < 0) {
1062 if (type == RTM_DELRULE)
1063 D(SYSTEM, "Error deleting a rule: %d\n", ret);
1064 else
1065 D(SYSTEM, "Error deleting %s from device '%s': %d\n",
1066 type == RTM_DELADDR ? "an address" : "a route",
1067 clr->dev->ifname, ret);
1068 }
1069
1070 nl_socket_enable_auto_ack(sock_rtnl);
1071
1072 return NL_SKIP;
1073 }
1074
1075 static int
1076 cb_finish_event(struct nl_msg *msg, void *arg)
1077 {
1078 int *pending = arg;
1079 *pending = 0;
1080 return NL_STOP;
1081 }
1082
1083 static int
1084 error_handler(struct sockaddr_nl *nla, struct nlmsgerr *err, void *arg)
1085 {
1086 int *pending = arg;
1087 *pending = err->error;
1088 return NL_STOP;
1089 }
1090
1091 static void
1092 system_if_clear_entries(struct device *dev, int type, int af)
1093 {
1094 struct clear_data clr;
1095 struct nl_cb *cb;
1096 struct rtmsg rtm = {
1097 .rtm_family = af,
1098 .rtm_flags = RTM_F_CLONED,
1099 };
1100 int flags = NLM_F_DUMP;
1101 int pending = 1;
1102
1103 clr.af = af;
1104 clr.dev = dev;
1105 clr.type = type;
1106 switch (type) {
1107 case RTM_GETADDR:
1108 case RTM_GETRULE:
1109 clr.size = sizeof(struct rtgenmsg);
1110 break;
1111 case RTM_GETROUTE:
1112 clr.size = sizeof(struct rtmsg);
1113 break;
1114 default:
1115 return;
1116 }
1117
1118 cb = nl_cb_alloc(NL_CB_DEFAULT);
1119 if (!cb)
1120 return;
1121
1122 clr.msg = nlmsg_alloc_simple(type, flags);
1123 if (!clr.msg)
1124 goto out;
1125
1126 nlmsg_append(clr.msg, &rtm, clr.size, 0);
1127 nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM, cb_clear_event, &clr);
1128 nl_cb_set(cb, NL_CB_FINISH, NL_CB_CUSTOM, cb_finish_event, &pending);
1129 nl_cb_err(cb, NL_CB_CUSTOM, error_handler, &pending);
1130
1131 if (nl_send_auto_complete(sock_rtnl, clr.msg) < 0)
1132 goto free;
1133
1134 while (pending > 0)
1135 nl_recvmsgs(sock_rtnl, cb);
1136
1137 free:
1138 nlmsg_free(clr.msg);
1139 out:
1140 nl_cb_put(cb);
1141 }
1142
1143 /*
1144 * Clear bridge (membership) state and bring down device
1145 */
1146 void system_if_clear_state(struct device *dev)
1147 {
1148 static char buf[256];
1149 char *bridge;
1150 device_set_ifindex(dev, system_if_resolve(dev));
1151
1152 if (dev->external || !dev->ifindex)
1153 return;
1154
1155 system_if_flags(dev->ifname, 0, IFF_UP);
1156
1157 if (system_is_bridge(dev->ifname, buf, sizeof(buf))) {
1158 D(SYSTEM, "Delete existing bridge named '%s'\n", dev->ifname);
1159 system_bridge_delbr(dev);
1160 return;
1161 }
1162
1163 bridge = system_get_bridge(dev->ifname, buf, sizeof(buf));
1164 if (bridge) {
1165 D(SYSTEM, "Remove device '%s' from bridge '%s'\n", dev->ifname, bridge);
1166 system_bridge_if(bridge, dev, SIOCBRDELIF, NULL);
1167 }
1168
1169 system_if_clear_entries(dev, RTM_GETROUTE, AF_INET);
1170 system_if_clear_entries(dev, RTM_GETADDR, AF_INET);
1171 system_if_clear_entries(dev, RTM_GETROUTE, AF_INET6);
1172 system_if_clear_entries(dev, RTM_GETADDR, AF_INET6);
1173 system_if_clear_entries(dev, RTM_GETNEIGH, AF_INET);
1174 system_if_clear_entries(dev, RTM_GETNEIGH, AF_INET6);
1175 system_set_disable_ipv6(dev, "0");
1176 }
1177
1178 static inline unsigned long
1179 sec_to_jiffies(int val)
1180 {
1181 return (unsigned long) val * 100;
1182 }
1183
1184 static void system_bridge_conf_multicast_deps(struct device *bridge,
1185 struct bridge_config *cfg,
1186 char *buf,
1187 int buf_len)
1188 {
1189 int val;
1190
1191 if (cfg->flags & BRIDGE_OPT_ROBUSTNESS ||
1192 cfg->flags & BRIDGE_OPT_QUERY_INTERVAL ||
1193 cfg->flags & BRIDGE_OPT_QUERY_RESPONSE_INTERVAL) {
1194 val = cfg->robustness * cfg->query_interval +
1195 cfg->query_response_interval;
1196
1197 snprintf(buf, buf_len, "%i", val);
1198 system_bridge_set_membership_interval(bridge, buf);
1199
1200 val = cfg->robustness * cfg->query_interval +
1201 cfg->query_response_interval / 2;
1202
1203 snprintf(buf, buf_len, "%i", val);
1204 system_bridge_set_other_querier_timeout(bridge, buf);
1205 }
1206
1207 if (cfg->flags & BRIDGE_OPT_QUERY_INTERVAL) {
1208 val = cfg->query_interval / 4;
1209
1210 snprintf(buf, buf_len, "%i", val);
1211 system_bridge_set_startup_query_interval(bridge, buf);
1212 }
1213 }
1214
1215 static void system_bridge_conf_multicast(struct device *bridge,
1216 struct bridge_config *cfg,
1217 char *buf,
1218 int buf_len)
1219 {
1220 system_set_dev_sysctl("/sys/devices/virtual/net/%s/bridge/multicast_snooping",
1221 bridge->ifname, cfg->igmp_snoop ? "1" : "0");
1222
1223 system_set_dev_sysctl("/sys/devices/virtual/net/%s/bridge/multicast_querier",
1224 bridge->ifname, cfg->multicast_querier ? "1" : "0");
1225
1226 snprintf(buf, buf_len, "%i", cfg->hash_max);
1227 system_set_dev_sysctl("/sys/devices/virtual/net/%s/bridge/hash_max",
1228 bridge->ifname, buf);
1229
1230 if (bridge->settings.flags & DEV_OPT_MULTICAST_ROUTER) {
1231 snprintf(buf, buf_len, "%u", bridge->settings.multicast_router);
1232 system_bridge_set_multicast_router(bridge, buf, true);
1233 }
1234
1235 if (cfg->flags & BRIDGE_OPT_ROBUSTNESS) {
1236 snprintf(buf, buf_len, "%i", cfg->robustness);
1237 system_bridge_set_robustness(bridge, buf);
1238 }
1239
1240 if (cfg->flags & BRIDGE_OPT_QUERY_INTERVAL) {
1241 snprintf(buf, buf_len, "%i", cfg->query_interval);
1242 system_bridge_set_query_interval(bridge, buf);
1243 }
1244
1245 if (cfg->flags & BRIDGE_OPT_QUERY_RESPONSE_INTERVAL) {
1246 snprintf(buf, buf_len, "%i", cfg->query_response_interval);
1247 system_bridge_set_query_response_interval(bridge, buf);
1248 }
1249
1250 if (cfg->flags & BRIDGE_OPT_LAST_MEMBER_INTERVAL) {
1251 snprintf(buf, buf_len, "%i", cfg->last_member_interval);
1252 system_bridge_set_last_member_interval(bridge, buf);
1253 }
1254
1255 system_bridge_conf_multicast_deps(bridge, cfg, buf, buf_len);
1256 }
1257
1258 int system_bridge_addbr(struct device *bridge, struct bridge_config *cfg)
1259 {
1260 char buf[64];
1261
1262 if (ioctl(sock_ioctl, SIOCBRADDBR, bridge->ifname) < 0)
1263 return -1;
1264
1265 system_bridge_set_stp_state(bridge, cfg->stp ? "1" : "0");
1266
1267 snprintf(buf, sizeof(buf), "%lu", sec_to_jiffies(cfg->forward_delay));
1268 system_bridge_set_forward_delay(bridge, buf);
1269
1270 system_bridge_conf_multicast(bridge, cfg, buf, sizeof(buf));
1271 system_bridge_set_vlan_filtering(bridge, cfg->vlan_filtering ? "1" : "0");
1272
1273 snprintf(buf, sizeof(buf), "%d", cfg->priority);
1274 system_bridge_set_priority(bridge, buf);
1275
1276 if (cfg->flags & BRIDGE_OPT_AGEING_TIME) {
1277 snprintf(buf, sizeof(buf), "%lu", sec_to_jiffies(cfg->ageing_time));
1278 system_bridge_set_ageing_time(bridge, buf);
1279 }
1280
1281 if (cfg->flags & BRIDGE_OPT_HELLO_TIME) {
1282 snprintf(buf, sizeof(buf), "%lu", sec_to_jiffies(cfg->hello_time));
1283 system_bridge_set_hello_time(bridge, buf);
1284 }
1285
1286 if (cfg->flags & BRIDGE_OPT_MAX_AGE) {
1287 snprintf(buf, sizeof(buf), "%lu", sec_to_jiffies(cfg->max_age));
1288 system_bridge_set_max_age(bridge, buf);
1289 }
1290
1291 return 0;
1292 }
1293
1294 int system_macvlan_add(struct device *macvlan, struct device *dev, struct macvlan_config *cfg)
1295 {
1296 struct nl_msg *msg;
1297 struct nlattr *linkinfo, *data;
1298 struct ifinfomsg iim = { .ifi_family = AF_UNSPEC, };
1299 int i, rv;
1300 static const struct {
1301 const char *name;
1302 enum macvlan_mode val;
1303 } modes[] = {
1304 { "private", MACVLAN_MODE_PRIVATE },
1305 { "vepa", MACVLAN_MODE_VEPA },
1306 { "bridge", MACVLAN_MODE_BRIDGE },
1307 { "passthru", MACVLAN_MODE_PASSTHRU },
1308 };
1309
1310 msg = nlmsg_alloc_simple(RTM_NEWLINK, NLM_F_REQUEST | NLM_F_CREATE | NLM_F_EXCL);
1311
1312 if (!msg)
1313 return -1;
1314
1315 nlmsg_append(msg, &iim, sizeof(iim), 0);
1316
1317 if (cfg->flags & MACVLAN_OPT_MACADDR)
1318 nla_put(msg, IFLA_ADDRESS, sizeof(cfg->macaddr), cfg->macaddr);
1319 nla_put_string(msg, IFLA_IFNAME, macvlan->ifname);
1320 nla_put_u32(msg, IFLA_LINK, dev->ifindex);
1321
1322 if (!(linkinfo = nla_nest_start(msg, IFLA_LINKINFO)))
1323 goto nla_put_failure;
1324
1325 nla_put_string(msg, IFLA_INFO_KIND, "macvlan");
1326
1327 if (!(data = nla_nest_start(msg, IFLA_INFO_DATA)))
1328 goto nla_put_failure;
1329
1330 if (cfg->mode) {
1331 for (i = 0; i < ARRAY_SIZE(modes); i++) {
1332 if (strcmp(cfg->mode, modes[i].name) != 0)
1333 continue;
1334
1335 nla_put_u32(msg, IFLA_MACVLAN_MODE, modes[i].val);
1336 break;
1337 }
1338 }
1339
1340 nla_nest_end(msg, data);
1341 nla_nest_end(msg, linkinfo);
1342
1343 rv = system_rtnl_call(msg);
1344 if (rv)
1345 D(SYSTEM, "Error adding macvlan '%s' over '%s': %d\n", macvlan->ifname, dev->ifname, rv);
1346
1347 return rv;
1348
1349 nla_put_failure:
1350 nlmsg_free(msg);
1351 return -ENOMEM;
1352 }
1353
1354 int system_link_netns_move(struct device *dev, int netns_fd, const char *target_ifname)
1355 {
1356 struct nl_msg *msg;
1357 struct ifinfomsg iim = {
1358 .ifi_family = AF_UNSPEC,
1359 };
1360
1361 if (!dev)
1362 return -1;
1363
1364 iim.ifi_index = system_if_resolve(dev);
1365 msg = nlmsg_alloc_simple(RTM_NEWLINK, NLM_F_REQUEST);
1366
1367 if (!msg)
1368 return -1;
1369
1370 nlmsg_append(msg, &iim, sizeof(iim), 0);
1371 if (target_ifname)
1372 nla_put_string(msg, IFLA_IFNAME, target_ifname);
1373
1374 nla_put_u32(msg, IFLA_NET_NS_FD, netns_fd);
1375 return system_rtnl_call(msg);
1376 }
1377
1378 static int system_link_del(const char *ifname)
1379 {
1380 struct nl_msg *msg;
1381 struct ifinfomsg iim = {
1382 .ifi_family = AF_UNSPEC,
1383 .ifi_index = 0,
1384 };
1385
1386 msg = nlmsg_alloc_simple(RTM_DELLINK, NLM_F_REQUEST);
1387
1388 if (!msg)
1389 return -1;
1390
1391 nlmsg_append(msg, &iim, sizeof(iim), 0);
1392 nla_put_string(msg, IFLA_IFNAME, ifname);
1393 return system_rtnl_call(msg);
1394 }
1395
1396 int system_macvlan_del(struct device *macvlan)
1397 {
1398 return system_link_del(macvlan->ifname);
1399 }
1400
1401 int system_netns_open(const pid_t target_ns)
1402 {
1403 char pid_net_path[PATH_MAX];
1404
1405 snprintf(pid_net_path, sizeof(pid_net_path), "/proc/%u/ns/net", target_ns);
1406
1407 return open(pid_net_path, O_RDONLY);
1408 }
1409
1410 int system_netns_set(int netns_fd)
1411 {
1412 return setns(netns_fd, CLONE_NEWNET);
1413 }
1414
1415 int system_veth_add(struct device *veth, struct veth_config *cfg)
1416 {
1417 struct nl_msg *msg;
1418 struct ifinfomsg empty_iim = {};
1419 struct nlattr *linkinfo, *data, *veth_info;
1420 int rv;
1421
1422 msg = nlmsg_alloc_simple(RTM_NEWLINK, NLM_F_REQUEST | NLM_F_CREATE | NLM_F_EXCL);
1423
1424 if (!msg)
1425 return -1;
1426
1427 nlmsg_append(msg, &empty_iim, sizeof(empty_iim), 0);
1428
1429 if (cfg->flags & VETH_OPT_MACADDR)
1430 nla_put(msg, IFLA_ADDRESS, sizeof(cfg->macaddr), cfg->macaddr);
1431 nla_put_string(msg, IFLA_IFNAME, veth->ifname);
1432
1433 if (!(linkinfo = nla_nest_start(msg, IFLA_LINKINFO)))
1434 goto nla_put_failure;
1435
1436 nla_put_string(msg, IFLA_INFO_KIND, "veth");
1437
1438 if (!(data = nla_nest_start(msg, IFLA_INFO_DATA)))
1439 goto nla_put_failure;
1440
1441 if (!(veth_info = nla_nest_start(msg, VETH_INFO_PEER)))
1442 goto nla_put_failure;
1443
1444 nlmsg_append(msg, &empty_iim, sizeof(empty_iim), 0);
1445
1446 if (cfg->flags & VETH_OPT_PEER_NAME)
1447 nla_put_string(msg, IFLA_IFNAME, cfg->peer_name);
1448 if (cfg->flags & VETH_OPT_PEER_MACADDR)
1449 nla_put(msg, IFLA_ADDRESS, sizeof(cfg->peer_macaddr), cfg->peer_macaddr);
1450
1451 nla_nest_end(msg, veth_info);
1452 nla_nest_end(msg, data);
1453 nla_nest_end(msg, linkinfo);
1454
1455 rv = system_rtnl_call(msg);
1456 if (rv) {
1457 if (cfg->flags & VETH_OPT_PEER_NAME)
1458 D(SYSTEM, "Error adding veth '%s' with peer '%s': %d\n", veth->ifname, cfg->peer_name, rv);
1459 else
1460 D(SYSTEM, "Error adding veth '%s': %d\n", veth->ifname, rv);
1461 }
1462
1463 return rv;
1464
1465 nla_put_failure:
1466 nlmsg_free(msg);
1467 return -ENOMEM;
1468 }
1469
1470 int system_veth_del(struct device *veth)
1471 {
1472 return system_link_del(veth->ifname);
1473 }
1474
1475 static int system_vlan(struct device *dev, int id)
1476 {
1477 struct vlan_ioctl_args ifr = {
1478 .cmd = SET_VLAN_NAME_TYPE_CMD,
1479 .u.name_type = VLAN_NAME_TYPE_RAW_PLUS_VID_NO_PAD,
1480 };
1481
1482 if (ioctl(sock_ioctl, SIOCSIFVLAN, &ifr) < 0)
1483 return -1;
1484
1485 if (id < 0) {
1486 ifr.cmd = DEL_VLAN_CMD;
1487 ifr.u.VID = 0;
1488 } else {
1489 ifr.cmd = ADD_VLAN_CMD;
1490 ifr.u.VID = id;
1491 }
1492 strncpy(ifr.device1, dev->ifname, sizeof(ifr.device1));
1493 return ioctl(sock_ioctl, SIOCSIFVLAN, &ifr);
1494 }
1495
1496 int system_vlan_add(struct device *dev, int id)
1497 {
1498 return system_vlan(dev, id);
1499 }
1500
1501 int system_vlan_del(struct device *dev)
1502 {
1503 return system_vlan(dev, -1);
1504 }
1505
1506 int system_vlandev_add(struct device *vlandev, struct device *dev, struct vlandev_config *cfg)
1507 {
1508 struct nl_msg *msg;
1509 struct nlattr *linkinfo, *data, *qos;
1510 struct ifinfomsg iim = { .ifi_family = AF_UNSPEC };
1511 struct vlan_qos_mapping *elem;
1512 struct ifla_vlan_qos_mapping nl_qos_map;
1513 int rv;
1514
1515 msg = nlmsg_alloc_simple(RTM_NEWLINK, NLM_F_REQUEST | NLM_F_CREATE | NLM_F_EXCL);
1516
1517 if (!msg)
1518 return -1;
1519
1520 nlmsg_append(msg, &iim, sizeof(iim), 0);
1521 nla_put_string(msg, IFLA_IFNAME, vlandev->ifname);
1522 nla_put_u32(msg, IFLA_LINK, dev->ifindex);
1523
1524 if (!(linkinfo = nla_nest_start(msg, IFLA_LINKINFO)))
1525 goto nla_put_failure;
1526
1527 nla_put_string(msg, IFLA_INFO_KIND, "vlan");
1528
1529 if (!(data = nla_nest_start(msg, IFLA_INFO_DATA)))
1530 goto nla_put_failure;
1531
1532 nla_put_u16(msg, IFLA_VLAN_ID, cfg->vid);
1533
1534 #if LINUX_VERSION_CODE >= KERNEL_VERSION(3,10,0)
1535 nla_put_u16(msg, IFLA_VLAN_PROTOCOL, htons(cfg->proto));
1536 #else
1537 if(cfg->proto == VLAN_PROTO_8021AD)
1538 netifd_log_message(L_WARNING, "%s Your kernel is older than linux 3.10.0, 802.1ad is not supported defaulting to 802.1q", vlandev->type->name);
1539 #endif
1540
1541 if (!(qos = nla_nest_start(msg, IFLA_VLAN_INGRESS_QOS)))
1542 goto nla_put_failure;
1543
1544 vlist_simple_for_each_element(&cfg->ingress_qos_mapping_list, elem, node) {
1545 nl_qos_map.from = elem->from;
1546 nl_qos_map.to = elem->to;
1547 nla_put(msg, IFLA_VLAN_QOS_MAPPING, sizeof(nl_qos_map), &nl_qos_map);
1548 }
1549 nla_nest_end(msg, qos);
1550
1551 if (!(qos = nla_nest_start(msg, IFLA_VLAN_EGRESS_QOS)))
1552 goto nla_put_failure;
1553
1554 vlist_simple_for_each_element(&cfg->egress_qos_mapping_list, elem, node) {
1555 nl_qos_map.from = elem->from;
1556 nl_qos_map.to = elem->to;
1557 nla_put(msg, IFLA_VLAN_QOS_MAPPING, sizeof(nl_qos_map), &nl_qos_map);
1558 }
1559 nla_nest_end(msg, qos);
1560
1561 nla_nest_end(msg, data);
1562 nla_nest_end(msg, linkinfo);
1563
1564 rv = system_rtnl_call(msg);
1565 if (rv)
1566 D(SYSTEM, "Error adding vlandev '%s' over '%s': %d\n", vlandev->ifname, dev->ifname, rv);
1567
1568 return rv;
1569
1570 nla_put_failure:
1571 nlmsg_free(msg);
1572 return -ENOMEM;
1573 }
1574
1575 int system_vlandev_del(struct device *vlandev)
1576 {
1577 return system_link_del(vlandev->ifname);
1578 }
1579
1580 void
1581 system_if_get_settings(struct device *dev, struct device_settings *s)
1582 {
1583 struct ifreq ifr;
1584 char buf[10];
1585
1586 memset(&ifr, 0, sizeof(ifr));
1587 strncpy(ifr.ifr_name, dev->ifname, sizeof(ifr.ifr_name) - 1);
1588
1589 if (ioctl(sock_ioctl, SIOCGIFMTU, &ifr) == 0) {
1590 s->mtu = ifr.ifr_mtu;
1591 s->flags |= DEV_OPT_MTU;
1592 }
1593
1594 s->mtu6 = system_update_ipv6_mtu(dev, 0);
1595 if (s->mtu6 > 0)
1596 s->flags |= DEV_OPT_MTU6;
1597
1598 if (ioctl(sock_ioctl, SIOCGIFTXQLEN, &ifr) == 0) {
1599 s->txqueuelen = ifr.ifr_qlen;
1600 s->flags |= DEV_OPT_TXQUEUELEN;
1601 }
1602
1603 if (ioctl(sock_ioctl, SIOCGIFHWADDR, &ifr) == 0) {
1604 memcpy(s->macaddr, &ifr.ifr_hwaddr.sa_data, sizeof(s->macaddr));
1605 s->flags |= DEV_OPT_MACADDR;
1606 }
1607
1608 if (!system_get_disable_ipv6(dev, buf, sizeof(buf))) {
1609 s->ipv6 = !strtoul(buf, NULL, 0);
1610 s->flags |= DEV_OPT_IPV6;
1611 }
1612
1613 if (!system_get_ip6segmentrouting(dev, buf, sizeof(buf))) {
1614 s->ip6segmentrouting = strtoul(buf, NULL, 0);
1615 s->flags |= DEV_OPT_IP6SEGMENTROUTING;
1616 }
1617
1618 if (ioctl(sock_ioctl, SIOCGIFFLAGS, &ifr) == 0) {
1619 s->promisc = ifr.ifr_flags & IFF_PROMISC;
1620 s->flags |= DEV_OPT_PROMISC;
1621
1622 s->multicast = ifr.ifr_flags & IFF_MULTICAST;
1623 s->flags |= DEV_OPT_MULTICAST;
1624 }
1625
1626 if (!system_get_rpfilter(dev, buf, sizeof(buf))) {
1627 s->rpfilter = strtoul(buf, NULL, 0);
1628 s->flags |= DEV_OPT_RPFILTER;
1629 }
1630
1631 if (!system_get_acceptlocal(dev, buf, sizeof(buf))) {
1632 s->acceptlocal = strtoul(buf, NULL, 0);
1633 s->flags |= DEV_OPT_ACCEPTLOCAL;
1634 }
1635
1636 if (!system_get_igmpversion(dev, buf, sizeof(buf))) {
1637 s->igmpversion = strtoul(buf, NULL, 0);
1638 s->flags |= DEV_OPT_IGMPVERSION;
1639 }
1640
1641 if (!system_get_mldversion(dev, buf, sizeof(buf))) {
1642 s->mldversion = strtoul(buf, NULL, 0);
1643 s->flags |= DEV_OPT_MLDVERSION;
1644 }
1645
1646 if (!system_get_neigh4reachabletime(dev, buf, sizeof(buf))) {
1647 s->neigh4reachabletime = strtoul(buf, NULL, 0);
1648 s->flags |= DEV_OPT_NEIGHREACHABLETIME;
1649 }
1650
1651 if (!system_get_neigh6reachabletime(dev, buf, sizeof(buf))) {
1652 s->neigh6reachabletime = strtoul(buf, NULL, 0);
1653 s->flags |= DEV_OPT_NEIGHREACHABLETIME;
1654 }
1655
1656 if (!system_get_neigh4locktime(dev, buf, sizeof(buf))) {
1657 s->neigh4locktime = strtol(buf, NULL, 0);
1658 s->flags |= DEV_OPT_NEIGHLOCKTIME;
1659 }
1660
1661 if (!system_get_neigh4gcstaletime(dev, buf, sizeof(buf))) {
1662 s->neigh4gcstaletime = strtoul(buf, NULL, 0);
1663 s->flags |= DEV_OPT_NEIGHGCSTALETIME;
1664 }
1665
1666 if (!system_get_neigh6gcstaletime(dev, buf, sizeof(buf))) {
1667 s->neigh6gcstaletime = strtoul(buf, NULL, 0);
1668 s->flags |= DEV_OPT_NEIGHGCSTALETIME;
1669 }
1670
1671 if (!system_get_dadtransmits(dev, buf, sizeof(buf))) {
1672 s->dadtransmits = strtoul(buf, NULL, 0);
1673 s->flags |= DEV_OPT_DADTRANSMITS;
1674 }
1675
1676 if (!system_get_sendredirects(dev, buf, sizeof(buf))) {
1677 s->sendredirects = strtoul(buf, NULL, 0);
1678 s->flags |= DEV_OPT_SENDREDIRECTS;
1679 }
1680
1681 if (!system_get_drop_v4_unicast_in_l2_multicast(dev, buf, sizeof(buf))) {
1682 s->drop_v4_unicast_in_l2_multicast = strtoul(buf, NULL, 0);
1683 s->flags |= DEV_OPT_DROP_V4_UNICAST_IN_L2_MULTICAST;
1684 }
1685
1686 if (!system_get_drop_v6_unicast_in_l2_multicast(dev, buf, sizeof(buf))) {
1687 s->drop_v6_unicast_in_l2_multicast = strtoul(buf, NULL, 0);
1688 s->flags |= DEV_OPT_DROP_V6_UNICAST_IN_L2_MULTICAST;
1689 }
1690
1691 if (!system_get_drop_gratuitous_arp(dev, buf, sizeof(buf))) {
1692 s->drop_gratuitous_arp = strtoul(buf, NULL, 0);
1693 s->flags |= DEV_OPT_DROP_GRATUITOUS_ARP;
1694 }
1695
1696 if (!system_get_drop_unsolicited_na(dev, buf, sizeof(buf))) {
1697 s->drop_unsolicited_na = strtoul(buf, NULL, 0);
1698 s->flags |= DEV_OPT_DROP_UNSOLICITED_NA;
1699 }
1700
1701 if (!system_get_arp_accept(dev, buf, sizeof(buf))) {
1702 s->arp_accept = strtoul(buf, NULL, 0);
1703 s->flags |= DEV_OPT_ARP_ACCEPT;
1704 }
1705 }
1706
1707 void
1708 system_if_apply_settings(struct device *dev, struct device_settings *s, unsigned int apply_mask)
1709 {
1710 struct ifreq ifr;
1711 char buf[12];
1712
1713 apply_mask &= s->flags;
1714
1715 memset(&ifr, 0, sizeof(ifr));
1716 strncpy(ifr.ifr_name, dev->ifname, sizeof(ifr.ifr_name) - 1);
1717 if (apply_mask & DEV_OPT_MTU) {
1718 ifr.ifr_mtu = s->mtu;
1719 if (ioctl(sock_ioctl, SIOCSIFMTU, &ifr) < 0)
1720 s->flags &= ~DEV_OPT_MTU;
1721 }
1722 if (apply_mask & DEV_OPT_MTU6) {
1723 system_update_ipv6_mtu(dev, s->mtu6);
1724 }
1725 if (apply_mask & DEV_OPT_TXQUEUELEN) {
1726 ifr.ifr_qlen = s->txqueuelen;
1727 if (ioctl(sock_ioctl, SIOCSIFTXQLEN, &ifr) < 0)
1728 s->flags &= ~DEV_OPT_TXQUEUELEN;
1729 }
1730 if ((apply_mask & (DEV_OPT_MACADDR | DEV_OPT_DEFAULT_MACADDR)) && !dev->external) {
1731 ifr.ifr_hwaddr.sa_family = ARPHRD_ETHER;
1732 memcpy(&ifr.ifr_hwaddr.sa_data, s->macaddr, sizeof(s->macaddr));
1733 if (ioctl(sock_ioctl, SIOCSIFHWADDR, &ifr) < 0)
1734 s->flags &= ~DEV_OPT_MACADDR;
1735 }
1736 if (apply_mask & DEV_OPT_IPV6)
1737 system_set_disable_ipv6(dev, s->ipv6 ? "0" : "1");
1738 if (s->flags & DEV_OPT_IP6SEGMENTROUTING & apply_mask) {
1739 struct device dummy = {
1740 .ifname = "all",
1741 };
1742 bool ip6segmentrouting = device_check_ip6segmentrouting();
1743
1744 system_set_ip6segmentrouting(dev, s->ip6segmentrouting ? "1" : "0");
1745 system_set_ip6segmentrouting(&dummy, ip6segmentrouting ? "1" : "0");
1746 }
1747 if (apply_mask & DEV_OPT_PROMISC) {
1748 if (system_if_flags(dev->ifname, s->promisc ? IFF_PROMISC : 0,
1749 !s->promisc ? IFF_PROMISC : 0) < 0)
1750 s->flags &= ~DEV_OPT_PROMISC;
1751 }
1752 if (apply_mask & DEV_OPT_RPFILTER) {
1753 snprintf(buf, sizeof(buf), "%u", s->rpfilter);
1754 system_set_rpfilter(dev, buf);
1755 }
1756 if (apply_mask & DEV_OPT_ACCEPTLOCAL)
1757 system_set_acceptlocal(dev, s->acceptlocal ? "1" : "0");
1758 if (apply_mask & DEV_OPT_IGMPVERSION) {
1759 snprintf(buf, sizeof(buf), "%u", s->igmpversion);
1760 system_set_igmpversion(dev, buf);
1761 }
1762 if (apply_mask & DEV_OPT_MLDVERSION) {
1763 snprintf(buf, sizeof(buf), "%u", s->mldversion);
1764 system_set_mldversion(dev, buf);
1765 }
1766 if (apply_mask & DEV_OPT_NEIGHREACHABLETIME) {
1767 snprintf(buf, sizeof(buf), "%u", s->neigh4reachabletime);
1768 system_set_neigh4reachabletime(dev, buf);
1769 snprintf(buf, sizeof(buf), "%u", s->neigh6reachabletime);
1770 system_set_neigh6reachabletime(dev, buf);
1771 }
1772 if (apply_mask & DEV_OPT_NEIGHLOCKTIME) {
1773 snprintf(buf, sizeof(buf), "%d", s->neigh4locktime);
1774 system_set_neigh4locktime(dev, buf);
1775 }
1776 if (apply_mask & DEV_OPT_NEIGHGCSTALETIME) {
1777 snprintf(buf, sizeof(buf), "%u", s->neigh4gcstaletime);
1778 system_set_neigh4gcstaletime(dev, buf);
1779 snprintf(buf, sizeof(buf), "%u", s->neigh6gcstaletime);
1780 system_set_neigh6gcstaletime(dev, buf);
1781 }
1782 if (apply_mask & DEV_OPT_DADTRANSMITS) {
1783 snprintf(buf, sizeof(buf), "%u", s->dadtransmits);
1784 system_set_dadtransmits(dev, buf);
1785 }
1786 if (apply_mask & DEV_OPT_MULTICAST) {
1787 if (system_if_flags(dev->ifname, s->multicast ? IFF_MULTICAST : 0,
1788 !s->multicast ? IFF_MULTICAST : 0) < 0)
1789 s->flags &= ~DEV_OPT_MULTICAST;
1790 }
1791 if (apply_mask & DEV_OPT_SENDREDIRECTS)
1792 system_set_sendredirects(dev, s->sendredirects ? "1" : "0");
1793 if (apply_mask & DEV_OPT_DROP_V4_UNICAST_IN_L2_MULTICAST)
1794 system_set_drop_v4_unicast_in_l2_multicast(dev, s->drop_v4_unicast_in_l2_multicast ? "1" : "0");
1795 if (apply_mask & DEV_OPT_DROP_V6_UNICAST_IN_L2_MULTICAST)
1796 system_set_drop_v6_unicast_in_l2_multicast(dev, s->drop_v6_unicast_in_l2_multicast ? "1" : "0");
1797 if (apply_mask & DEV_OPT_DROP_GRATUITOUS_ARP)
1798 system_set_drop_gratuitous_arp(dev, s->drop_gratuitous_arp ? "1" : "0");
1799 if (apply_mask & DEV_OPT_DROP_UNSOLICITED_NA)
1800 system_set_drop_unsolicited_na(dev, s->drop_unsolicited_na ? "1" : "0");
1801 if (apply_mask & DEV_OPT_ARP_ACCEPT)
1802 system_set_arp_accept(dev, s->arp_accept ? "1" : "0");
1803 }
1804
1805 int system_if_up(struct device *dev)
1806 {
1807 return system_if_flags(dev->ifname, IFF_UP, 0);
1808 }
1809
1810 int system_if_down(struct device *dev)
1811 {
1812 return system_if_flags(dev->ifname, 0, IFF_UP);
1813 }
1814
1815 struct if_check_data {
1816 struct device *dev;
1817 int pending;
1818 int ret;
1819 };
1820
1821 #ifndef IFF_LOWER_UP
1822 #define IFF_LOWER_UP 0x10000
1823 #endif
1824
1825 static int cb_if_check_valid(struct nl_msg *msg, void *arg)
1826 {
1827 struct nlmsghdr *nh = nlmsg_hdr(msg);
1828 struct ifinfomsg *ifi = NLMSG_DATA(nh);
1829 struct if_check_data *chk = (struct if_check_data *)arg;
1830
1831 if (nh->nlmsg_type != RTM_NEWLINK)
1832 return NL_SKIP;
1833
1834 if (chk->dev->type == &simple_device_type)
1835 device_set_present(chk->dev, ifi->ifi_index > 0 ? true : false);
1836 device_set_link(chk->dev, ifi->ifi_flags & IFF_LOWER_UP ? true : false);
1837
1838 return NL_OK;
1839 }
1840
1841 static int cb_if_check_ack(struct nl_msg *msg, void *arg)
1842 {
1843 struct if_check_data *chk = (struct if_check_data *)arg;
1844 chk->pending = 0;
1845 return NL_STOP;
1846 }
1847
1848 static int cb_if_check_error(struct sockaddr_nl *nla, struct nlmsgerr *err, void *arg)
1849 {
1850 struct if_check_data *chk = (struct if_check_data *)arg;
1851
1852 if (chk->dev->type == &simple_device_type)
1853 device_set_present(chk->dev, false);
1854 device_set_link(chk->dev, false);
1855 chk->pending = err->error;
1856
1857 return NL_STOP;
1858 }
1859
1860 struct bridge_vlan_check_data {
1861 struct device *check_dev;
1862 int ifindex;
1863 int ret;
1864 bool pending;
1865 };
1866
1867 static void bridge_vlan_check_port(struct bridge_vlan_check_data *data,
1868 struct bridge_vlan_port *port,
1869 struct bridge_vlan_info *vinfo)
1870 {
1871 uint16_t flags = 0, diff, mask;
1872
1873 if (port->flags & BRVLAN_F_PVID)
1874 flags |= BRIDGE_VLAN_INFO_PVID;
1875 if (port->flags & BRVLAN_F_UNTAGGED)
1876 flags |= BRIDGE_VLAN_INFO_UNTAGGED;
1877
1878 diff = vinfo->flags ^ flags;
1879 mask = BRVLAN_F_UNTAGGED | (flags & BRIDGE_VLAN_INFO_PVID);
1880 if (diff & mask) {
1881 data->ret = 1;
1882 data->pending = false;
1883 }
1884
1885 port->check = 1;
1886 }
1887
1888 static void bridge_vlan_check_attr(struct bridge_vlan_check_data *data,
1889 struct rtattr *attr)
1890 {
1891 struct bridge_vlan_hotplug_port *port;
1892 struct bridge_vlan_info *vinfo;
1893 struct bridge_vlan *vlan;
1894 struct rtattr *cur;
1895 int rem = RTA_PAYLOAD(attr);
1896 int i;
1897
1898 for (cur = RTA_DATA(attr); RTA_OK(cur, rem); cur = RTA_NEXT(cur, rem)) {
1899 if (cur->rta_type != IFLA_BRIDGE_VLAN_INFO)
1900 continue;
1901
1902 vinfo = RTA_DATA(cur);
1903 vlan = vlist_find(&data->check_dev->vlans, &vinfo->vid, vlan, node);
1904 if (!vlan) {
1905 data->ret = 1;
1906 data->pending = false;
1907 return;
1908 }
1909
1910 for (i = 0; i < vlan->n_ports; i++)
1911 if (!vlan->ports[i].check)
1912 bridge_vlan_check_port(data, &vlan->ports[i], vinfo);
1913
1914 list_for_each_entry(port, &vlan->hotplug_ports, list)
1915 if (!port->port.check)
1916 bridge_vlan_check_port(data, &port->port, vinfo);
1917 }
1918 }
1919
1920 static int bridge_vlan_check_cb(struct nl_msg *msg, void *arg)
1921 {
1922 struct bridge_vlan_check_data *data = arg;
1923 struct nlmsghdr *nh = nlmsg_hdr(msg);
1924 struct ifinfomsg *ifi = NLMSG_DATA(nh);
1925 struct rtattr *attr;
1926 int rem;
1927
1928 if (nh->nlmsg_type != RTM_NEWLINK)
1929 return NL_SKIP;
1930
1931 if (ifi->ifi_family != AF_BRIDGE)
1932 return NL_SKIP;
1933
1934 if (ifi->ifi_index != data->ifindex)
1935 return NL_SKIP;
1936
1937 attr = IFLA_RTA(ifi);
1938 rem = nh->nlmsg_len - NLMSG_LENGTH(sizeof(*ifi));
1939 while (RTA_OK(attr, rem)) {
1940 if (attr->rta_type == IFLA_AF_SPEC)
1941 bridge_vlan_check_attr(data, attr);
1942
1943 attr = RTA_NEXT(attr, rem);
1944 }
1945
1946 return NL_SKIP;
1947 }
1948
1949 static int bridge_vlan_ack_cb(struct nl_msg *msg, void *arg)
1950 {
1951 struct bridge_vlan_check_data *data = arg;
1952 data->pending = false;
1953 return NL_STOP;
1954 }
1955
1956 static int bridge_vlan_error_cb(struct sockaddr_nl *nla, struct nlmsgerr *err, void *arg)
1957 {
1958 struct bridge_vlan_check_data *data = arg;
1959 data->pending = false;
1960 return NL_STOP;
1961 }
1962
1963 int system_bridge_vlan_check(struct device *dev, char *ifname)
1964 {
1965 struct bridge_vlan_check_data data = {
1966 .check_dev = dev,
1967 .ifindex = if_nametoindex(ifname),
1968 .ret = -1,
1969 .pending = true,
1970 };
1971 static struct ifinfomsg ifi = {
1972 .ifi_family = AF_BRIDGE
1973 };
1974 static struct rtattr ext_req = {
1975 .rta_type = IFLA_EXT_MASK,
1976 .rta_len = RTA_LENGTH(sizeof(uint32_t)),
1977 };
1978 uint32_t filter = RTEXT_FILTER_BRVLAN;
1979 struct nl_cb *cb = nl_cb_alloc(NL_CB_DEFAULT);
1980 struct bridge_vlan *vlan;
1981 struct nl_msg *msg;
1982 int i;
1983
1984 if (!data.ifindex)
1985 return 0;
1986
1987 msg = nlmsg_alloc_simple(RTM_GETLINK, NLM_F_DUMP);
1988
1989 if (nlmsg_append(msg, &ifi, sizeof(ifi), 0) ||
1990 nlmsg_append(msg, &ext_req, sizeof(ext_req), NLMSG_ALIGNTO) ||
1991 nlmsg_append(msg, &filter, sizeof(filter), 0))
1992 goto free;
1993
1994 vlist_for_each_element(&dev->vlans, vlan, node) {
1995 struct bridge_vlan_hotplug_port *port;
1996
1997 for (i = 0; i < vlan->n_ports; i++) {
1998 if (!strcmp(vlan->ports[i].ifname, ifname))
1999 vlan->ports[i].check = 0;
2000 else
2001 vlan->ports[i].check = -1;
2002 }
2003
2004 list_for_each_entry(port, &vlan->hotplug_ports, list) {
2005 if (!strcmp(port->port.ifname, ifname))
2006 port->port.check = 0;
2007 else
2008 port->port.check = -1;
2009 }
2010 }
2011
2012 nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM, bridge_vlan_check_cb, &data);
2013 nl_cb_set(cb, NL_CB_FINISH, NL_CB_CUSTOM, bridge_vlan_ack_cb, &data);
2014 nl_cb_set(cb, NL_CB_ACK, NL_CB_CUSTOM, bridge_vlan_ack_cb, &data);
2015 nl_cb_err(cb, NL_CB_CUSTOM, bridge_vlan_error_cb, &data);
2016
2017 if (nl_send_auto_complete(sock_rtnl, msg) < 0)
2018 goto free;
2019
2020 data.ret = 0;
2021 while (data.pending)
2022 nl_recvmsgs(sock_rtnl, cb);
2023
2024 vlist_for_each_element(&dev->vlans, vlan, node) {
2025 struct bridge_vlan_hotplug_port *port;
2026
2027 for (i = 0; i < vlan->n_ports; i++) {
2028 if (!vlan->ports[i].check) {
2029 data.ret = 1;
2030 break;
2031 }
2032 }
2033
2034 list_for_each_entry(port, &vlan->hotplug_ports, list) {
2035 if (!port->port.check) {
2036 data.ret = 1;
2037 break;
2038 }
2039 }
2040 }
2041
2042 goto out;
2043
2044 free:
2045 nlmsg_free(msg);
2046 out:
2047 nl_cb_put(cb);
2048 return data.ret;
2049 }
2050
2051 int system_if_check(struct device *dev)
2052 {
2053 struct nl_cb *cb = nl_cb_alloc(NL_CB_DEFAULT);
2054 struct nl_msg *msg;
2055 struct ifinfomsg ifi = {
2056 .ifi_family = AF_UNSPEC,
2057 .ifi_index = 0,
2058 };
2059 struct if_check_data chk = {
2060 .dev = dev,
2061 .pending = 1,
2062 };
2063 int ret = 1;
2064
2065 if (!cb)
2066 return ret;
2067
2068 msg = nlmsg_alloc_simple(RTM_GETLINK, 0);
2069 if (!msg)
2070 goto out;
2071
2072 if (nlmsg_append(msg, &ifi, sizeof(ifi), 0) ||
2073 nla_put_string(msg, IFLA_IFNAME, dev->ifname))
2074 goto free;
2075
2076 nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM, cb_if_check_valid, &chk);
2077 nl_cb_set(cb, NL_CB_ACK, NL_CB_CUSTOM, cb_if_check_ack, &chk);
2078 nl_cb_err(cb, NL_CB_CUSTOM, cb_if_check_error, &chk);
2079
2080 ret = nl_send_auto_complete(sock_rtnl, msg);
2081 if (ret < 0)
2082 goto free;
2083
2084 while (chk.pending > 0)
2085 nl_recvmsgs(sock_rtnl, cb);
2086
2087 ret = chk.pending;
2088
2089 free:
2090 nlmsg_free(msg);
2091 out:
2092 nl_cb_put(cb);
2093 return ret;
2094 }
2095
2096 struct device *
2097 system_if_get_parent(struct device *dev)
2098 {
2099 char buf[64], *devname;
2100 int ifindex, iflink, len;
2101 FILE *f;
2102
2103 snprintf(buf, sizeof(buf), "/sys/class/net/%s/iflink", dev->ifname);
2104 f = fopen(buf, "r");
2105 if (!f)
2106 return NULL;
2107
2108 len = fread(buf, 1, sizeof(buf) - 1, f);
2109 fclose(f);
2110
2111 if (len <= 0)
2112 return NULL;
2113
2114 buf[len] = 0;
2115 iflink = strtoul(buf, NULL, 0);
2116 ifindex = system_if_resolve(dev);
2117 if (!iflink || iflink == ifindex)
2118 return NULL;
2119
2120 devname = if_indextoname(iflink, buf);
2121 if (!devname)
2122 return NULL;
2123
2124 return device_get(devname, true);
2125 }
2126
2127 static bool
2128 read_string_file(int dir_fd, const char *file, char *buf, int len)
2129 {
2130 bool ret = false;
2131 char *c;
2132 int fd;
2133
2134 fd = openat(dir_fd, file, O_RDONLY);
2135 if (fd < 0)
2136 return false;
2137
2138 retry:
2139 len = read(fd, buf, len - 1);
2140 if (len < 0) {
2141 if (errno == EINTR)
2142 goto retry;
2143 } else if (len > 0) {
2144 buf[len] = 0;
2145
2146 c = strchr(buf, '\n');
2147 if (c)
2148 *c = 0;
2149
2150 ret = true;
2151 }
2152
2153 close(fd);
2154
2155 return ret;
2156 }
2157
2158 static bool
2159 read_uint64_file(int dir_fd, const char *file, uint64_t *val)
2160 {
2161 char buf[64];
2162 bool ret = false;
2163
2164 ret = read_string_file(dir_fd, file, buf, sizeof(buf));
2165 if (ret)
2166 *val = strtoull(buf, NULL, 0);
2167
2168 return ret;
2169 }
2170
2171 /* Assume advertised flags == supported flags */
2172 static const struct {
2173 uint32_t mask;
2174 const char *name;
2175 } ethtool_link_modes[] = {
2176 { ADVERTISED_10baseT_Half, "10baseT-H" },
2177 { ADVERTISED_10baseT_Full, "10baseT-F" },
2178 { ADVERTISED_100baseT_Half, "100baseT-H" },
2179 { ADVERTISED_100baseT_Full, "100baseT-F" },
2180 { ADVERTISED_1000baseT_Half, "1000baseT-H" },
2181 { ADVERTISED_1000baseT_Full, "1000baseT-F" },
2182 { ADVERTISED_1000baseKX_Full, "1000baseKX-F" },
2183 { ADVERTISED_2500baseX_Full, "2500baseX-F" },
2184 { ADVERTISED_10000baseT_Full, "10000baseT-F" },
2185 { ADVERTISED_10000baseKX4_Full, "10000baseKX4-F" },
2186 { ADVERTISED_10000baseKR_Full, "10000baseKR-F" },
2187 { ADVERTISED_20000baseMLD2_Full, "20000baseMLD2-F" },
2188 { ADVERTISED_20000baseKR2_Full, "20000baseKR2-F" },
2189 { ADVERTISED_40000baseKR4_Full, "40000baseKR4-F" },
2190 { ADVERTISED_40000baseCR4_Full, "40000baseCR4-F" },
2191 { ADVERTISED_40000baseSR4_Full, "40000baseSR4-F" },
2192 { ADVERTISED_40000baseLR4_Full, "40000baseLR4-F" },
2193 #ifdef ADVERTISED_56000baseKR4_Full
2194 { ADVERTISED_56000baseKR4_Full, "56000baseKR4-F" },
2195 { ADVERTISED_56000baseCR4_Full, "56000baseCR4-F" },
2196 { ADVERTISED_56000baseSR4_Full, "56000baseSR4-F" },
2197 { ADVERTISED_56000baseLR4_Full, "56000baseLR4-F" },
2198 #endif
2199 };
2200
2201 static void system_add_link_modes(struct blob_buf *b, __u32 mask)
2202 {
2203 int i;
2204 for (i = 0; i < ARRAY_SIZE(ethtool_link_modes); i++) {
2205 if (mask & ethtool_link_modes[i].mask)
2206 blobmsg_add_string(b, NULL, ethtool_link_modes[i].name);
2207 }
2208 }
2209
2210 bool
2211 system_if_force_external(const char *ifname)
2212 {
2213 char buf[64];
2214 struct stat s;
2215
2216 snprintf(buf, sizeof(buf), "/sys/class/net/%s/phy80211", ifname);
2217 return stat(buf, &s) == 0;
2218 }
2219
2220 int
2221 system_if_dump_info(struct device *dev, struct blob_buf *b)
2222 {
2223 struct ethtool_cmd ecmd;
2224 struct ifreq ifr;
2225 char *s;
2226 void *c;
2227
2228 memset(&ecmd, 0, sizeof(ecmd));
2229 memset(&ifr, 0, sizeof(ifr));
2230 strncpy(ifr.ifr_name, dev->ifname, sizeof(ifr.ifr_name) - 1);
2231 ifr.ifr_data = (caddr_t) &ecmd;
2232 ecmd.cmd = ETHTOOL_GSET;
2233
2234 if (ioctl(sock_ioctl, SIOCETHTOOL, &ifr) == 0) {
2235 c = blobmsg_open_array(b, "link-advertising");
2236 system_add_link_modes(b, ecmd.advertising);
2237 blobmsg_close_array(b, c);
2238
2239 c = blobmsg_open_array(b, "link-partner-advertising");
2240 system_add_link_modes(b, ecmd.lp_advertising);
2241 blobmsg_close_array(b, c);
2242
2243 c = blobmsg_open_array(b, "link-supported");
2244 system_add_link_modes(b, ecmd.supported);
2245 blobmsg_close_array(b, c);
2246
2247 s = blobmsg_alloc_string_buffer(b, "speed", 8);
2248 snprintf(s, 8, "%d%c", ethtool_cmd_speed(&ecmd),
2249 ecmd.duplex == DUPLEX_HALF ? 'H' : 'F');
2250 blobmsg_add_string_buffer(b);
2251
2252 blobmsg_add_u8(b, "autoneg", !!ecmd.autoneg);
2253 }
2254
2255 return 0;
2256 }
2257
2258 int
2259 system_if_dump_stats(struct device *dev, struct blob_buf *b)
2260 {
2261 const char *const counters[] = {
2262 "collisions", "rx_frame_errors", "tx_compressed",
2263 "multicast", "rx_length_errors", "tx_dropped",
2264 "rx_bytes", "rx_missed_errors", "tx_errors",
2265 "rx_compressed", "rx_over_errors", "tx_fifo_errors",
2266 "rx_crc_errors", "rx_packets", "tx_heartbeat_errors",
2267 "rx_dropped", "tx_aborted_errors", "tx_packets",
2268 "rx_errors", "tx_bytes", "tx_window_errors",
2269 "rx_fifo_errors", "tx_carrier_errors",
2270 };
2271 char buf[64];
2272 int stats_dir;
2273 int i;
2274 uint64_t val = 0;
2275
2276 snprintf(buf, sizeof(buf), "/sys/class/net/%s/statistics", dev->ifname);
2277 stats_dir = open(buf, O_DIRECTORY);
2278 if (stats_dir < 0)
2279 return -1;
2280
2281 for (i = 0; i < ARRAY_SIZE(counters); i++)
2282 if (read_uint64_file(stats_dir, counters[i], &val))
2283 blobmsg_add_u64(b, counters[i], val);
2284
2285 close(stats_dir);
2286 return 0;
2287 }
2288
2289 static int system_addr(struct device *dev, struct device_addr *addr, int cmd)
2290 {
2291 bool v4 = ((addr->flags & DEVADDR_FAMILY) == DEVADDR_INET4);
2292 int alen = v4 ? 4 : 16;
2293 unsigned int flags = 0;
2294 struct ifaddrmsg ifa = {
2295 .ifa_family = (alen == 4) ? AF_INET : AF_INET6,
2296 .ifa_prefixlen = addr->mask,
2297 .ifa_index = dev->ifindex,
2298 };
2299
2300 struct nl_msg *msg;
2301 if (cmd == RTM_NEWADDR)
2302 flags |= NLM_F_CREATE | NLM_F_REPLACE;
2303
2304 msg = nlmsg_alloc_simple(cmd, flags);
2305 if (!msg)
2306 return -1;
2307
2308 nlmsg_append(msg, &ifa, sizeof(ifa), 0);
2309 nla_put(msg, IFA_LOCAL, alen, &addr->addr);
2310 if (v4) {
2311 if (addr->broadcast)
2312 nla_put_u32(msg, IFA_BROADCAST, addr->broadcast);
2313 if (addr->point_to_point)
2314 nla_put_u32(msg, IFA_ADDRESS, addr->point_to_point);
2315 } else {
2316 time_t now = system_get_rtime();
2317 struct ifa_cacheinfo cinfo = {0xffffffffU, 0xffffffffU, 0, 0};
2318
2319 if (addr->preferred_until) {
2320 int64_t preferred = addr->preferred_until - now;
2321 if (preferred < 0)
2322 preferred = 0;
2323 else if (preferred > UINT32_MAX)
2324 preferred = UINT32_MAX;
2325
2326 cinfo.ifa_prefered = preferred;
2327 }
2328
2329 if (addr->valid_until) {
2330 int64_t valid = addr->valid_until - now;
2331 if (valid <= 0) {
2332 nlmsg_free(msg);
2333 return -1;
2334 }
2335 else if (valid > UINT32_MAX)
2336 valid = UINT32_MAX;
2337
2338 cinfo.ifa_valid = valid;
2339 }
2340
2341 nla_put(msg, IFA_CACHEINFO, sizeof(cinfo), &cinfo);
2342
2343 if (cmd == RTM_NEWADDR && (addr->flags & DEVADDR_OFFLINK))
2344 nla_put_u32(msg, IFA_FLAGS, IFA_F_NOPREFIXROUTE);
2345 }
2346
2347 return system_rtnl_call(msg);
2348 }
2349
2350 int system_add_address(struct device *dev, struct device_addr *addr)
2351 {
2352 return system_addr(dev, addr, RTM_NEWADDR);
2353 }
2354
2355 int system_del_address(struct device *dev, struct device_addr *addr)
2356 {
2357 return system_addr(dev, addr, RTM_DELADDR);
2358 }
2359
2360 static int system_neigh(struct device *dev, struct device_neighbor *neighbor, int cmd)
2361 {
2362 int alen = ((neighbor->flags & DEVADDR_FAMILY) == DEVADDR_INET4) ? 4 : 16;
2363 unsigned int flags = 0;
2364 struct ndmsg ndm = {
2365 .ndm_family = (alen == 4) ? AF_INET : AF_INET6,
2366 .ndm_ifindex = dev->ifindex,
2367 .ndm_state = NUD_PERMANENT,
2368 .ndm_flags = (neighbor->proxy ? NTF_PROXY : 0) | (neighbor->router ? NTF_ROUTER : 0),
2369 };
2370 struct nl_msg *msg;
2371
2372 if (cmd == RTM_NEWNEIGH)
2373 flags |= NLM_F_CREATE | NLM_F_REPLACE;
2374
2375 msg = nlmsg_alloc_simple(cmd, flags);
2376
2377 if (!msg)
2378 return -1;
2379
2380 nlmsg_append(msg, &ndm, sizeof(ndm), 0);
2381
2382 nla_put(msg, NDA_DST, alen, &neighbor->addr);
2383 if (neighbor->flags & DEVNEIGH_MAC)
2384 nla_put(msg, NDA_LLADDR, sizeof(neighbor->macaddr), &neighbor->macaddr);
2385
2386
2387 return system_rtnl_call(msg);
2388 }
2389
2390 int system_add_neighbor(struct device *dev, struct device_neighbor *neighbor)
2391 {
2392 return system_neigh(dev, neighbor, RTM_NEWNEIGH);
2393 }
2394
2395 int system_del_neighbor(struct device *dev, struct device_neighbor *neighbor)
2396 {
2397 return system_neigh(dev, neighbor, RTM_DELNEIGH);
2398 }
2399
2400 static int system_rt(struct device *dev, struct device_route *route, int cmd)
2401 {
2402 int alen = ((route->flags & DEVADDR_FAMILY) == DEVADDR_INET4) ? 4 : 16;
2403 bool have_gw;
2404 unsigned int flags = 0;
2405
2406 if (alen == 4)
2407 have_gw = !!route->nexthop.in.s_addr;
2408 else
2409 have_gw = route->nexthop.in6.s6_addr32[0] ||
2410 route->nexthop.in6.s6_addr32[1] ||
2411 route->nexthop.in6.s6_addr32[2] ||
2412 route->nexthop.in6.s6_addr32[3];
2413
2414 unsigned int table = (route->flags & (DEVROUTE_TABLE | DEVROUTE_SRCTABLE))
2415 ? route->table : RT_TABLE_MAIN;
2416
2417 struct rtmsg rtm = {
2418 .rtm_family = (alen == 4) ? AF_INET : AF_INET6,
2419 .rtm_dst_len = route->mask,
2420 .rtm_src_len = route->sourcemask,
2421 .rtm_table = (table < 256) ? table : RT_TABLE_UNSPEC,
2422 .rtm_protocol = (route->flags & DEVROUTE_PROTO) ? route->proto : RTPROT_STATIC,
2423 .rtm_scope = RT_SCOPE_NOWHERE,
2424 .rtm_type = (cmd == RTM_DELROUTE) ? 0: RTN_UNICAST,
2425 .rtm_flags = (route->flags & DEVROUTE_ONLINK) ? RTNH_F_ONLINK : 0,
2426 };
2427 struct nl_msg *msg;
2428
2429 if (cmd == RTM_NEWROUTE) {
2430 flags |= NLM_F_CREATE | NLM_F_REPLACE;
2431
2432 if (!dev) { /* Add null-route */
2433 rtm.rtm_scope = RT_SCOPE_UNIVERSE;
2434 rtm.rtm_type = RTN_UNREACHABLE;
2435 }
2436 else
2437 rtm.rtm_scope = (have_gw) ? RT_SCOPE_UNIVERSE : RT_SCOPE_LINK;
2438 }
2439
2440 if (route->flags & DEVROUTE_TYPE) {
2441 rtm.rtm_type = route->type;
2442 if (!(route->flags & (DEVROUTE_TABLE | DEVROUTE_SRCTABLE))) {
2443 if (rtm.rtm_type == RTN_LOCAL || rtm.rtm_type == RTN_BROADCAST ||
2444 rtm.rtm_type == RTN_NAT || rtm.rtm_type == RTN_ANYCAST)
2445 rtm.rtm_table = RT_TABLE_LOCAL;
2446 }
2447
2448 if (rtm.rtm_type == RTN_LOCAL || rtm.rtm_type == RTN_NAT) {
2449 rtm.rtm_scope = RT_SCOPE_HOST;
2450 } else if (rtm.rtm_type == RTN_BROADCAST || rtm.rtm_type == RTN_MULTICAST ||
2451 rtm.rtm_type == RTN_ANYCAST) {
2452 rtm.rtm_scope = RT_SCOPE_LINK;
2453 } else if (rtm.rtm_type == RTN_BLACKHOLE || rtm.rtm_type == RTN_UNREACHABLE ||
2454 rtm.rtm_type == RTN_PROHIBIT || rtm.rtm_type == RTN_FAILED_POLICY ||
2455 rtm.rtm_type == RTN_THROW) {
2456 rtm.rtm_scope = RT_SCOPE_UNIVERSE;
2457 dev = NULL;
2458 }
2459 }
2460
2461 msg = nlmsg_alloc_simple(cmd, flags);
2462 if (!msg)
2463 return -1;
2464
2465 nlmsg_append(msg, &rtm, sizeof(rtm), 0);
2466
2467 if (route->mask)
2468 nla_put(msg, RTA_DST, alen, &route->addr);
2469
2470 if (route->sourcemask) {
2471 if (rtm.rtm_family == AF_INET)
2472 nla_put(msg, RTA_PREFSRC, alen, &route->source);
2473 else
2474 nla_put(msg, RTA_SRC, alen, &route->source);
2475 }
2476
2477 if (route->metric > 0)
2478 nla_put_u32(msg, RTA_PRIORITY, route->metric);
2479
2480 if (have_gw)
2481 nla_put(msg, RTA_GATEWAY, alen, &route->nexthop);
2482
2483 if (dev)
2484 nla_put_u32(msg, RTA_OIF, dev->ifindex);
2485
2486 if (table >= 256)
2487 nla_put_u32(msg, RTA_TABLE, table);
2488
2489 if (route->flags & DEVROUTE_MTU) {
2490 struct nlattr *metrics;
2491
2492 if (!(metrics = nla_nest_start(msg, RTA_METRICS)))
2493 goto nla_put_failure;
2494
2495 nla_put_u32(msg, RTAX_MTU, route->mtu);
2496
2497 nla_nest_end(msg, metrics);
2498 }
2499
2500 return system_rtnl_call(msg);
2501
2502 nla_put_failure:
2503 nlmsg_free(msg);
2504 return -ENOMEM;
2505 }
2506
2507 int system_add_route(struct device *dev, struct device_route *route)
2508 {
2509 return system_rt(dev, route, RTM_NEWROUTE);
2510 }
2511
2512 int system_del_route(struct device *dev, struct device_route *route)
2513 {
2514 return system_rt(dev, route, RTM_DELROUTE);
2515 }
2516
2517 int system_flush_routes(void)
2518 {
2519 const char *names[] = {
2520 "/proc/sys/net/ipv4/route/flush",
2521 "/proc/sys/net/ipv6/route/flush"
2522 };
2523 int fd, i;
2524
2525 for (i = 0; i < ARRAY_SIZE(names); i++) {
2526 fd = open(names[i], O_WRONLY);
2527 if (fd < 0)
2528 continue;
2529
2530 if (write(fd, "-1", 2)) {}
2531 close(fd);
2532 }
2533 return 0;
2534 }
2535
2536 bool system_resolve_rt_type(const char *type, unsigned int *id)
2537 {
2538 return system_rtn_aton(type, id);
2539 }
2540
2541 bool system_resolve_rt_proto(const char *type, unsigned int *id)
2542 {
2543 FILE *f;
2544 char *e, buf[128];
2545 unsigned int n, proto = 256;
2546 n = strtoul(type, &e, 0);
2547 if (!*e && e != type)
2548 proto = n;
2549 else if (!strcmp(type, "unspec"))
2550 proto = RTPROT_UNSPEC;
2551 else if (!strcmp(type, "kernel"))
2552 proto = RTPROT_KERNEL;
2553 else if (!strcmp(type, "boot"))
2554 proto = RTPROT_BOOT;
2555 else if (!strcmp(type, "static"))
2556 proto = RTPROT_STATIC;
2557 else if ((f = fopen("/etc/iproute2/rt_protos", "r")) != NULL) {
2558 while (fgets(buf, sizeof(buf) - 1, f) != NULL) {
2559 if ((e = strtok(buf, " \t\n")) == NULL || *e == '#')
2560 continue;
2561
2562 n = strtoul(e, NULL, 10);
2563 e = strtok(NULL, " \t\n");
2564
2565 if (e && !strcmp(e, type)) {
2566 proto = n;
2567 break;
2568 }
2569 }
2570 fclose(f);
2571 }
2572
2573 if (proto > 255)
2574 return false;
2575
2576 *id = proto;
2577 return true;
2578 }
2579
2580 bool system_resolve_rt_table(const char *name, unsigned int *id)
2581 {
2582 FILE *f;
2583 char *e, buf[128];
2584 unsigned int n, table = RT_TABLE_UNSPEC;
2585
2586 /* first try to parse table as number */
2587 if ((n = strtoul(name, &e, 0)) > 0 && !*e)
2588 table = n;
2589
2590 /* handle well known aliases */
2591 else if (!strcmp(name, "default"))
2592 table = RT_TABLE_DEFAULT;
2593 else if (!strcmp(name, "main"))
2594 table = RT_TABLE_MAIN;
2595 else if (!strcmp(name, "local"))
2596 table = RT_TABLE_LOCAL;
2597
2598 /* try to look up name in /etc/iproute2/rt_tables */
2599 else if ((f = fopen("/etc/iproute2/rt_tables", "r")) != NULL)
2600 {
2601 while (fgets(buf, sizeof(buf) - 1, f) != NULL)
2602 {
2603 if ((e = strtok(buf, " \t\n")) == NULL || *e == '#')
2604 continue;
2605
2606 n = strtoul(e, NULL, 10);
2607 e = strtok(NULL, " \t\n");
2608
2609 if (e && !strcmp(e, name))
2610 {
2611 table = n;
2612 break;
2613 }
2614 }
2615
2616 fclose(f);
2617 }
2618
2619 if (table == RT_TABLE_UNSPEC)
2620 return false;
2621
2622 *id = table;
2623 return true;
2624 }
2625
2626 bool system_is_default_rt_table(unsigned int id)
2627 {
2628 return (id == RT_TABLE_MAIN);
2629 }
2630
2631 bool system_resolve_rpfilter(const char *filter, unsigned int *id)
2632 {
2633 char *e;
2634 unsigned int n;
2635
2636 if (!strcmp(filter, "strict"))
2637 n = 1;
2638 else if (!strcmp(filter, "loose"))
2639 n = 2;
2640 else {
2641 n = strtoul(filter, &e, 0);
2642 if (*e || e == filter || n > 2)
2643 return false;
2644 }
2645
2646 *id = n;
2647 return true;
2648 }
2649
2650 static int system_iprule(struct iprule *rule, int cmd)
2651 {
2652 int alen = ((rule->flags & IPRULE_FAMILY) == IPRULE_INET4) ? 4 : 16;
2653
2654 struct nl_msg *msg;
2655 struct rtmsg rtm = {
2656 .rtm_family = (alen == 4) ? AF_INET : AF_INET6,
2657 .rtm_protocol = RTPROT_STATIC,
2658 .rtm_scope = RT_SCOPE_UNIVERSE,
2659 .rtm_table = RT_TABLE_UNSPEC,
2660 .rtm_type = RTN_UNSPEC,
2661 .rtm_flags = 0,
2662 };
2663
2664 if (cmd == RTM_NEWRULE)
2665 rtm.rtm_type = RTN_UNICAST;
2666
2667 if (rule->invert)
2668 rtm.rtm_flags |= FIB_RULE_INVERT;
2669
2670 if (rule->flags & IPRULE_SRC)
2671 rtm.rtm_src_len = rule->src_mask;
2672
2673 if (rule->flags & IPRULE_DEST)
2674 rtm.rtm_dst_len = rule->dest_mask;
2675
2676 if (rule->flags & IPRULE_TOS)
2677 rtm.rtm_tos = rule->tos;
2678
2679 if (rule->flags & IPRULE_LOOKUP) {
2680 if (rule->lookup < 256)
2681 rtm.rtm_table = rule->lookup;
2682 }
2683
2684 if (rule->flags & IPRULE_ACTION)
2685 rtm.rtm_type = rule->action;
2686 else if (rule->flags & IPRULE_GOTO)
2687 rtm.rtm_type = FR_ACT_GOTO;
2688 else if (!(rule->flags & (IPRULE_LOOKUP | IPRULE_ACTION | IPRULE_GOTO)))
2689 rtm.rtm_type = FR_ACT_NOP;
2690
2691 msg = nlmsg_alloc_simple(cmd, NLM_F_REQUEST);
2692
2693 if (!msg)
2694 return -1;
2695
2696 nlmsg_append(msg, &rtm, sizeof(rtm), 0);
2697
2698 if (rule->flags & IPRULE_IN)
2699 nla_put(msg, FRA_IFNAME, strlen(rule->in_dev) + 1, rule->in_dev);
2700
2701 if (rule->flags & IPRULE_OUT)
2702 nla_put(msg, FRA_OIFNAME, strlen(rule->out_dev) + 1, rule->out_dev);
2703
2704 if (rule->flags & IPRULE_SRC)
2705 nla_put(msg, FRA_SRC, alen, &rule->src_addr);
2706
2707 if (rule->flags & IPRULE_DEST)
2708 nla_put(msg, FRA_DST, alen, &rule->dest_addr);
2709
2710 if (rule->flags & IPRULE_PRIORITY)
2711 nla_put_u32(msg, FRA_PRIORITY, rule->priority);
2712 else if (cmd == RTM_NEWRULE)
2713 nla_put_u32(msg, FRA_PRIORITY, rule->order);
2714
2715 if (rule->flags & IPRULE_FWMARK)
2716 nla_put_u32(msg, FRA_FWMARK, rule->fwmark);
2717
2718 if (rule->flags & IPRULE_FWMASK)
2719 nla_put_u32(msg, FRA_FWMASK, rule->fwmask);
2720
2721 if (rule->flags & IPRULE_LOOKUP) {
2722 if (rule->lookup >= 256)
2723 nla_put_u32(msg, FRA_TABLE, rule->lookup);
2724 }
2725
2726 if (rule->flags & IPRULE_SUP_PREFIXLEN)
2727 nla_put_u32(msg, FRA_SUPPRESS_PREFIXLEN, rule->sup_prefixlen);
2728
2729 if (rule->flags & IPRULE_GOTO)
2730 nla_put_u32(msg, FRA_GOTO, rule->gotoid);
2731
2732 return system_rtnl_call(msg);
2733 }
2734
2735 int system_add_iprule(struct iprule *rule)
2736 {
2737 return system_iprule(rule, RTM_NEWRULE);
2738 }
2739
2740 int system_del_iprule(struct iprule *rule)
2741 {
2742 return system_iprule(rule, RTM_DELRULE);
2743 }
2744
2745 int system_flush_iprules(void)
2746 {
2747 int rv = 0;
2748 struct iprule rule;
2749
2750 system_if_clear_entries(NULL, RTM_GETRULE, AF_INET);
2751 system_if_clear_entries(NULL, RTM_GETRULE, AF_INET6);
2752
2753 memset(&rule, 0, sizeof(rule));
2754
2755
2756 rule.flags = IPRULE_INET4 | IPRULE_PRIORITY | IPRULE_LOOKUP;
2757
2758 rule.priority = 0;
2759 rule.lookup = RT_TABLE_LOCAL;
2760 rv |= system_iprule(&rule, RTM_NEWRULE);
2761
2762 rule.priority = 32766;
2763 rule.lookup = RT_TABLE_MAIN;
2764 rv |= system_iprule(&rule, RTM_NEWRULE);
2765
2766 rule.priority = 32767;
2767 rule.lookup = RT_TABLE_DEFAULT;
2768 rv |= system_iprule(&rule, RTM_NEWRULE);
2769
2770
2771 rule.flags = IPRULE_INET6 | IPRULE_PRIORITY | IPRULE_LOOKUP;
2772
2773 rule.priority = 0;
2774 rule.lookup = RT_TABLE_LOCAL;
2775 rv |= system_iprule(&rule, RTM_NEWRULE);
2776
2777 rule.priority = 32766;
2778 rule.lookup = RT_TABLE_MAIN;
2779 rv |= system_iprule(&rule, RTM_NEWRULE);
2780
2781 return rv;
2782 }
2783
2784 bool system_resolve_iprule_action(const char *action, unsigned int *id)
2785 {
2786 return system_rtn_aton(action, id);
2787 }
2788
2789 time_t system_get_rtime(void)
2790 {
2791 struct timespec ts;
2792 struct timeval tv;
2793
2794 if (clock_gettime(CLOCK_MONOTONIC, &ts) == 0)
2795 return ts.tv_sec;
2796
2797 if (gettimeofday(&tv, NULL) == 0)
2798 return tv.tv_sec;
2799
2800 return 0;
2801 }
2802
2803 #ifndef IP_DF
2804 #define IP_DF 0x4000
2805 #endif
2806
2807 static int tunnel_ioctl(const char *name, int cmd, void *p)
2808 {
2809 struct ifreq ifr;
2810
2811 memset(&ifr, 0, sizeof(ifr));
2812 strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name) - 1);
2813 ifr.ifr_ifru.ifru_data = p;
2814 return ioctl(sock_ioctl, cmd, &ifr);
2815 }
2816
2817 #ifdef IFLA_IPTUN_MAX
2818 static int system_add_ip6_tunnel(const char *name, const unsigned int link,
2819 struct blob_attr **tb)
2820 {
2821 struct nl_msg *nlm = nlmsg_alloc_simple(RTM_NEWLINK,
2822 NLM_F_REQUEST | NLM_F_REPLACE | NLM_F_CREATE);
2823 struct ifinfomsg ifi = { .ifi_family = AF_UNSPEC };
2824 struct blob_attr *cur;
2825 int ret = 0, ttl = 0;
2826
2827 if (!nlm)
2828 return -1;
2829
2830 nlmsg_append(nlm, &ifi, sizeof(ifi), 0);
2831 nla_put_string(nlm, IFLA_IFNAME, name);
2832
2833 if (link)
2834 nla_put_u32(nlm, IFLA_LINK, link);
2835
2836 struct nlattr *linkinfo = nla_nest_start(nlm, IFLA_LINKINFO);
2837 if (!linkinfo) {
2838 ret = -ENOMEM;
2839 goto failure;
2840 }
2841
2842 nla_put_string(nlm, IFLA_INFO_KIND, "ip6tnl");
2843 struct nlattr *infodata = nla_nest_start(nlm, IFLA_INFO_DATA);
2844 if (!infodata) {
2845 ret = -ENOMEM;
2846 goto failure;
2847 }
2848
2849 if (link)
2850 nla_put_u32(nlm, IFLA_IPTUN_LINK, link);
2851
2852 if ((cur = tb[TUNNEL_ATTR_TTL]))
2853 ttl = blobmsg_get_u32(cur);
2854
2855 nla_put_u8(nlm, IFLA_IPTUN_PROTO, IPPROTO_IPIP);
2856 nla_put_u8(nlm, IFLA_IPTUN_TTL, (ttl) ? ttl : 64);
2857
2858 struct in6_addr in6buf;
2859 if ((cur = tb[TUNNEL_ATTR_LOCAL])) {
2860 if (inet_pton(AF_INET6, blobmsg_data(cur), &in6buf) < 1) {
2861 ret = -EINVAL;
2862 goto failure;
2863 }
2864 nla_put(nlm, IFLA_IPTUN_LOCAL, sizeof(in6buf), &in6buf);
2865 }
2866
2867 if ((cur = tb[TUNNEL_ATTR_REMOTE])) {
2868 if (inet_pton(AF_INET6, blobmsg_data(cur), &in6buf) < 1) {
2869 ret = -EINVAL;
2870 goto failure;
2871 }
2872 nla_put(nlm, IFLA_IPTUN_REMOTE, sizeof(in6buf), &in6buf);
2873 }
2874
2875 if ((cur = tb[TUNNEL_ATTR_DATA])) {
2876 struct blob_attr *tb_data[__IPIP6_DATA_ATTR_MAX];
2877 uint32_t tun_flags = IP6_TNL_F_IGN_ENCAP_LIMIT;
2878
2879 blobmsg_parse(ipip6_data_attr_list.params, __IPIP6_DATA_ATTR_MAX, tb_data,
2880 blobmsg_data(cur), blobmsg_len(cur));
2881
2882 if ((cur = tb_data[IPIP6_DATA_ENCAPLIMIT])) {
2883 char *str = blobmsg_get_string(cur);
2884
2885 if (strcmp(str, "ignore")) {
2886 char *e;
2887 unsigned encap_limit = strtoul(str, &e, 0);
2888
2889 if (e == str || *e || encap_limit > 255) {
2890 ret = -EINVAL;
2891 goto failure;
2892 }
2893
2894 nla_put_u8(nlm, IFLA_IPTUN_ENCAP_LIMIT, encap_limit);
2895 tun_flags &= ~IP6_TNL_F_IGN_ENCAP_LIMIT;
2896 }
2897 }
2898
2899 #ifdef IFLA_IPTUN_FMR_MAX
2900 if ((cur = tb_data[IPIP6_DATA_FMRS])) {
2901 struct blob_attr *rcur;
2902 unsigned rrem, fmrcnt = 0;
2903 struct nlattr *fmrs = nla_nest_start(nlm, IFLA_IPTUN_FMRS);
2904
2905 if (!fmrs) {
2906 ret = -ENOMEM;
2907 goto failure;
2908 }
2909
2910 blobmsg_for_each_attr(rcur, cur, rrem) {
2911 struct blob_attr *tb_fmr[__FMR_DATA_ATTR_MAX], *tb_cur;
2912 struct in6_addr ip6prefix;
2913 struct in_addr ip4prefix;
2914 unsigned ip4len, ip6len, ealen, offset;
2915
2916 blobmsg_parse(fmr_data_attr_list.params, __FMR_DATA_ATTR_MAX, tb_fmr,
2917 blobmsg_data(rcur), blobmsg_len(rcur));
2918
2919 if (!(tb_cur = tb_fmr[FMR_DATA_PREFIX6]) ||
2920 !parse_ip_and_netmask(AF_INET6,
2921 blobmsg_data(tb_cur), &ip6prefix,
2922 &ip6len)) {
2923 ret = -EINVAL;
2924 goto failure;
2925 }
2926
2927 if (!(tb_cur = tb_fmr[FMR_DATA_PREFIX4]) ||
2928 !parse_ip_and_netmask(AF_INET,
2929 blobmsg_data(tb_cur), &ip4prefix,
2930 &ip4len)) {
2931 ret = -EINVAL;
2932 goto failure;
2933 }
2934
2935 if (!(tb_cur = tb_fmr[FMR_DATA_EALEN])) {
2936 ret = -EINVAL;
2937 goto failure;
2938 }
2939 ealen = blobmsg_get_u32(tb_cur);
2940
2941 if (!(tb_cur = tb_fmr[FMR_DATA_OFFSET])) {
2942 ret = -EINVAL;
2943 goto failure;
2944 }
2945 offset = blobmsg_get_u32(tb_cur);
2946
2947 struct nlattr *rule = nla_nest_start(nlm, ++fmrcnt);
2948 if (!rule) {
2949 ret = -ENOMEM;
2950 goto failure;
2951 }
2952
2953 nla_put(nlm, IFLA_IPTUN_FMR_IP6_PREFIX, sizeof(ip6prefix), &ip6prefix);
2954 nla_put(nlm, IFLA_IPTUN_FMR_IP4_PREFIX, sizeof(ip4prefix), &ip4prefix);
2955 nla_put_u8(nlm, IFLA_IPTUN_FMR_IP6_PREFIX_LEN, ip6len);
2956 nla_put_u8(nlm, IFLA_IPTUN_FMR_IP4_PREFIX_LEN, ip4len);
2957 nla_put_u8(nlm, IFLA_IPTUN_FMR_EA_LEN, ealen);
2958 nla_put_u8(nlm, IFLA_IPTUN_FMR_OFFSET, offset);
2959
2960 nla_nest_end(nlm, rule);
2961 }
2962
2963 nla_nest_end(nlm, fmrs);
2964 }
2965 #endif
2966 if (tun_flags)
2967 nla_put_u32(nlm, IFLA_IPTUN_FLAGS, tun_flags);
2968 }
2969
2970 nla_nest_end(nlm, infodata);
2971 nla_nest_end(nlm, linkinfo);
2972
2973 return system_rtnl_call(nlm);
2974
2975 failure:
2976 nlmsg_free(nlm);
2977 return ret;
2978 }
2979 #endif
2980
2981 #ifdef IFLA_IPTUN_MAX
2982 #define IP6_FLOWINFO_TCLASS htonl(0x0FF00000)
2983 static int system_add_gre_tunnel(const char *name, const char *kind,
2984 const unsigned int link, struct blob_attr **tb, bool v6)
2985 {
2986 struct nl_msg *nlm;
2987 struct ifinfomsg ifi = { .ifi_family = AF_UNSPEC, };
2988 struct blob_attr *cur;
2989 uint32_t ikey = 0, okey = 0, flowinfo = 0, flags6 = IP6_TNL_F_IGN_ENCAP_LIMIT;
2990 uint16_t iflags = 0, oflags = 0;
2991 uint8_t tos = 0;
2992 int ret = 0, ttl = 0;
2993 unsigned encap_limit = 0;
2994
2995 nlm = nlmsg_alloc_simple(RTM_NEWLINK, NLM_F_REQUEST | NLM_F_REPLACE | NLM_F_CREATE);
2996 if (!nlm)
2997 return -1;
2998
2999 nlmsg_append(nlm, &ifi, sizeof(ifi), 0);
3000 nla_put_string(nlm, IFLA_IFNAME, name);
3001
3002 struct nlattr *linkinfo = nla_nest_start(nlm, IFLA_LINKINFO);
3003 if (!linkinfo) {
3004 ret = -ENOMEM;
3005 goto failure;
3006 }
3007
3008 nla_put_string(nlm, IFLA_INFO_KIND, kind);
3009 struct nlattr *infodata = nla_nest_start(nlm, IFLA_INFO_DATA);
3010 if (!infodata) {
3011 ret = -ENOMEM;
3012 goto failure;
3013 }
3014
3015 if (link)
3016 nla_put_u32(nlm, IFLA_GRE_LINK, link);
3017
3018 if ((cur = tb[TUNNEL_ATTR_TTL]))
3019 ttl = blobmsg_get_u32(cur);
3020
3021 if ((cur = tb[TUNNEL_ATTR_TOS])) {
3022 char *str = blobmsg_get_string(cur);
3023 if (strcmp(str, "inherit")) {
3024 unsigned uval;
3025
3026 if (!system_tos_aton(str, &uval)) {
3027 ret = -EINVAL;
3028 goto failure;
3029 }
3030
3031 if (v6)
3032 flowinfo |= htonl(uval << 20) & IP6_FLOWINFO_TCLASS;
3033 else
3034 tos = uval;
3035 } else {
3036 if (v6)
3037 flags6 |= IP6_TNL_F_USE_ORIG_TCLASS;
3038 else
3039 tos = 1;
3040 }
3041 }
3042
3043 if ((cur = tb[TUNNEL_ATTR_DATA])) {
3044 struct blob_attr *tb_data[__GRE_DATA_ATTR_MAX];
3045
3046 blobmsg_parse(gre_data_attr_list.params, __GRE_DATA_ATTR_MAX, tb_data,
3047 blobmsg_data(cur), blobmsg_len(cur));
3048
3049 if ((cur = tb_data[GRE_DATA_IKEY])) {
3050 if ((ikey = blobmsg_get_u32(cur)))
3051 iflags |= GRE_KEY;
3052 }
3053
3054 if ((cur = tb_data[GRE_DATA_OKEY])) {
3055 if ((okey = blobmsg_get_u32(cur)))
3056 oflags |= GRE_KEY;
3057 }
3058
3059 if ((cur = tb_data[GRE_DATA_ICSUM])) {
3060 if (blobmsg_get_bool(cur))
3061 iflags |= GRE_CSUM;
3062 }
3063
3064 if ((cur = tb_data[GRE_DATA_OCSUM])) {
3065 if (blobmsg_get_bool(cur))
3066 oflags |= GRE_CSUM;
3067 }
3068
3069 if ((cur = tb_data[GRE_DATA_ISEQNO])) {
3070 if (blobmsg_get_bool(cur))
3071 iflags |= GRE_SEQ;
3072 }
3073
3074 if ((cur = tb_data[GRE_DATA_OSEQNO])) {
3075 if (blobmsg_get_bool(cur))
3076 oflags |= GRE_SEQ;
3077 }
3078
3079 if ((cur = tb_data[GRE_DATA_ENCAPLIMIT])) {
3080 char *str = blobmsg_get_string(cur);
3081
3082 if (strcmp(str, "ignore")) {
3083 char *e;
3084
3085 encap_limit = strtoul(str, &e, 0);
3086
3087 if (e == str || *e || encap_limit > 255) {
3088 ret = -EINVAL;
3089 goto failure;
3090 }
3091
3092 flags6 &= ~IP6_TNL_F_IGN_ENCAP_LIMIT;
3093 }
3094 }
3095 }
3096
3097 if (v6) {
3098 struct in6_addr in6buf;
3099 if ((cur = tb[TUNNEL_ATTR_LOCAL])) {
3100 if (inet_pton(AF_INET6, blobmsg_data(cur), &in6buf) < 1) {
3101 ret = -EINVAL;
3102 goto failure;
3103 }
3104 nla_put(nlm, IFLA_GRE_LOCAL, sizeof(in6buf), &in6buf);
3105 }
3106
3107 if ((cur = tb[TUNNEL_ATTR_REMOTE])) {
3108 if (inet_pton(AF_INET6, blobmsg_data(cur), &in6buf) < 1) {
3109 ret = -EINVAL;
3110 goto failure;
3111 }
3112 nla_put(nlm, IFLA_GRE_REMOTE, sizeof(in6buf), &in6buf);
3113 }
3114
3115 if (!(flags6 & IP6_TNL_F_IGN_ENCAP_LIMIT))
3116 nla_put_u8(nlm, IFLA_GRE_ENCAP_LIMIT, encap_limit);
3117
3118 if (flowinfo)
3119 nla_put_u32(nlm, IFLA_GRE_FLOWINFO, flowinfo);
3120
3121 if (flags6)
3122 nla_put_u32(nlm, IFLA_GRE_FLAGS, flags6);
3123
3124 if (!ttl)
3125 ttl = 64;
3126 } else {
3127 struct in_addr inbuf;
3128 bool set_df = true;
3129
3130 if ((cur = tb[TUNNEL_ATTR_LOCAL])) {
3131 if (inet_pton(AF_INET, blobmsg_data(cur), &inbuf) < 1) {
3132 ret = -EINVAL;
3133 goto failure;
3134 }
3135 nla_put(nlm, IFLA_GRE_LOCAL, sizeof(inbuf), &inbuf);
3136 }
3137
3138 if ((cur = tb[TUNNEL_ATTR_REMOTE])) {
3139 if (inet_pton(AF_INET, blobmsg_data(cur), &inbuf) < 1) {
3140 ret = -EINVAL;
3141 goto failure;
3142 }
3143 nla_put(nlm, IFLA_GRE_REMOTE, sizeof(inbuf), &inbuf);
3144
3145 if (IN_MULTICAST(ntohl(inbuf.s_addr))) {
3146 if (!okey) {
3147 okey = inbuf.s_addr;
3148 oflags |= GRE_KEY;
3149 }
3150
3151 if (!ikey) {
3152 ikey = inbuf.s_addr;
3153 iflags |= GRE_KEY;
3154 }
3155 }
3156 }
3157
3158 if ((cur = tb[TUNNEL_ATTR_DF]))
3159 set_df = blobmsg_get_bool(cur);
3160
3161 if (!set_df) {
3162 /* ttl != 0 and nopmtudisc are incompatible */
3163 if (ttl) {
3164 ret = -EINVAL;
3165 goto failure;
3166 }
3167 } else if (!ttl)
3168 ttl = 64;
3169
3170 nla_put_u8(nlm, IFLA_GRE_PMTUDISC, set_df ? 1 : 0);
3171
3172 nla_put_u8(nlm, IFLA_GRE_TOS, tos);
3173 }
3174
3175 if (ttl)
3176 nla_put_u8(nlm, IFLA_GRE_TTL, ttl);
3177
3178 if (oflags)
3179 nla_put_u16(nlm, IFLA_GRE_OFLAGS, oflags);
3180
3181 if (iflags)
3182 nla_put_u16(nlm, IFLA_GRE_IFLAGS, iflags);
3183
3184 if (okey)
3185 nla_put_u32(nlm, IFLA_GRE_OKEY, htonl(okey));
3186
3187 if (ikey)
3188 nla_put_u32(nlm, IFLA_GRE_IKEY, htonl(ikey));
3189
3190 nla_nest_end(nlm, infodata);
3191 nla_nest_end(nlm, linkinfo);
3192
3193 return system_rtnl_call(nlm);
3194
3195 failure:
3196 nlmsg_free(nlm);
3197 return ret;
3198 }
3199 #endif
3200
3201 #ifdef IFLA_VTI_MAX
3202 static int system_add_vti_tunnel(const char *name, const char *kind,
3203 const unsigned int link, struct blob_attr **tb, bool v6)
3204 {
3205 struct nl_msg *nlm;
3206 struct ifinfomsg ifi = { .ifi_family = AF_UNSPEC, };
3207 struct blob_attr *cur;
3208 int ret = 0;
3209
3210 nlm = nlmsg_alloc_simple(RTM_NEWLINK, NLM_F_REQUEST | NLM_F_REPLACE | NLM_F_CREATE);
3211 if (!nlm)
3212 return -1;
3213
3214 nlmsg_append(nlm, &ifi, sizeof(ifi), 0);
3215 nla_put_string(nlm, IFLA_IFNAME, name);
3216
3217 struct nlattr *linkinfo = nla_nest_start(nlm, IFLA_LINKINFO);
3218 if (!linkinfo) {
3219 ret = -ENOMEM;
3220 goto failure;
3221 }
3222
3223 nla_put_string(nlm, IFLA_INFO_KIND, kind);
3224 struct nlattr *infodata = nla_nest_start(nlm, IFLA_INFO_DATA);
3225 if (!infodata) {
3226 ret = -ENOMEM;
3227 goto failure;
3228 }
3229
3230 if (link)
3231 nla_put_u32(nlm, IFLA_VTI_LINK, link);
3232
3233 if (v6) {
3234 struct in6_addr in6buf;
3235 if ((cur = tb[TUNNEL_ATTR_LOCAL])) {
3236 if (inet_pton(AF_INET6, blobmsg_data(cur), &in6buf) < 1) {
3237 ret = -EINVAL;
3238 goto failure;
3239 }
3240 nla_put(nlm, IFLA_VTI_LOCAL, sizeof(in6buf), &in6buf);
3241 }
3242
3243 if ((cur = tb[TUNNEL_ATTR_REMOTE])) {
3244 if (inet_pton(AF_INET6, blobmsg_data(cur), &in6buf) < 1) {
3245 ret = -EINVAL;
3246 goto failure;
3247 }
3248 nla_put(nlm, IFLA_VTI_REMOTE, sizeof(in6buf), &in6buf);
3249 }
3250
3251 } else {
3252 struct in_addr inbuf;
3253
3254 if ((cur = tb[TUNNEL_ATTR_LOCAL])) {
3255 if (inet_pton(AF_INET, blobmsg_data(cur), &inbuf) < 1) {
3256 ret = -EINVAL;
3257 goto failure;
3258 }
3259 nla_put(nlm, IFLA_VTI_LOCAL, sizeof(inbuf), &inbuf);
3260 }
3261
3262 if ((cur = tb[TUNNEL_ATTR_REMOTE])) {
3263 if (inet_pton(AF_INET, blobmsg_data(cur), &inbuf) < 1) {
3264 ret = -EINVAL;
3265 goto failure;
3266 }
3267 nla_put(nlm, IFLA_VTI_REMOTE, sizeof(inbuf), &inbuf);
3268 }
3269
3270 }
3271
3272 if ((cur = tb[TUNNEL_ATTR_DATA])) {
3273 struct blob_attr *tb_data[__VTI_DATA_ATTR_MAX];
3274 uint32_t ikey = 0, okey = 0;
3275
3276 blobmsg_parse(vti_data_attr_list.params, __VTI_DATA_ATTR_MAX, tb_data,
3277 blobmsg_data(cur), blobmsg_len(cur));
3278
3279 if ((cur = tb_data[VTI_DATA_IKEY])) {
3280 if ((ikey = blobmsg_get_u32(cur)))
3281 nla_put_u32(nlm, IFLA_VTI_IKEY, htonl(ikey));
3282 }
3283
3284 if ((cur = tb_data[VTI_DATA_OKEY])) {
3285 if ((okey = blobmsg_get_u32(cur)))
3286 nla_put_u32(nlm, IFLA_VTI_OKEY, htonl(okey));
3287 }
3288 }
3289
3290 nla_nest_end(nlm, infodata);
3291 nla_nest_end(nlm, linkinfo);
3292
3293 return system_rtnl_call(nlm);
3294
3295 failure:
3296 nlmsg_free(nlm);
3297 return ret;
3298 }
3299 #endif
3300
3301 #ifdef IFLA_XFRM_MAX
3302 static int system_add_xfrm_tunnel(const char *name, const char *kind,
3303 const unsigned int link, struct blob_attr **tb)
3304 {
3305 struct nl_msg *nlm;
3306 struct ifinfomsg ifi = { .ifi_family = AF_UNSPEC, };
3307 struct blob_attr *cur;
3308 int ret = 0;
3309
3310 nlm = nlmsg_alloc_simple(RTM_NEWLINK, NLM_F_REQUEST | NLM_F_REPLACE | NLM_F_CREATE);
3311 if (!nlm)
3312 return -1;
3313
3314 nlmsg_append(nlm, &ifi, sizeof(ifi), 0);
3315 nla_put_string(nlm, IFLA_IFNAME, name);
3316
3317 struct nlattr *linkinfo = nla_nest_start(nlm, IFLA_LINKINFO);
3318 if (!linkinfo) {
3319 ret = -ENOMEM;
3320 goto failure;
3321 }
3322
3323 nla_put_string(nlm, IFLA_INFO_KIND, kind);
3324 struct nlattr *infodata = nla_nest_start(nlm, IFLA_INFO_DATA);
3325 if (!infodata) {
3326 ret = -ENOMEM;
3327 goto failure;
3328 }
3329
3330 if (link)
3331 nla_put_u32(nlm, IFLA_XFRM_LINK, link);
3332
3333 if ((cur = tb[TUNNEL_ATTR_DATA])) {
3334 struct blob_attr *tb_data[__XFRM_DATA_ATTR_MAX];
3335 uint32_t if_id = 0;
3336
3337 blobmsg_parse(xfrm_data_attr_list.params, __XFRM_DATA_ATTR_MAX, tb_data,
3338 blobmsg_data(cur), blobmsg_len(cur));
3339
3340 if ((cur = tb_data[XFRM_DATA_IF_ID])) {
3341 if ((if_id = blobmsg_get_u32(cur)))
3342 nla_put_u32(nlm, IFLA_XFRM_IF_ID, if_id);
3343 }
3344
3345 }
3346
3347 nla_nest_end(nlm, infodata);
3348 nla_nest_end(nlm, linkinfo);
3349
3350 return system_rtnl_call(nlm);
3351
3352 failure:
3353 nlmsg_free(nlm);
3354 return ret;
3355 }
3356 #endif
3357
3358 #ifdef IFLA_VXLAN_MAX
3359 static void system_vxlan_map_bool_attr(struct nl_msg *msg, struct blob_attr **tb_data, int attrtype, int vxlandatatype, bool invert) {
3360 struct blob_attr *cur;
3361 if ((cur = tb_data[vxlandatatype])) {
3362 bool val = blobmsg_get_bool(cur);
3363 if (invert)
3364 val = !val;
3365
3366 if ((attrtype == IFLA_VXLAN_GBP) && val)
3367 nla_put_flag(msg, attrtype);
3368 else
3369 nla_put_u8(msg, attrtype, val);
3370
3371 }
3372 }
3373
3374 static int system_add_vxlan(const char *name, const unsigned int link, struct blob_attr **tb, bool v6)
3375 {
3376 struct blob_attr *tb_data[__VXLAN_DATA_ATTR_MAX];
3377 struct nl_msg *msg;
3378 struct nlattr *linkinfo, *data;
3379 struct ifinfomsg iim = { .ifi_family = AF_UNSPEC, };
3380 struct blob_attr *cur;
3381 int ret = 0;
3382
3383 if ((cur = tb[TUNNEL_ATTR_DATA]))
3384 blobmsg_parse(vxlan_data_attr_list.params, __VXLAN_DATA_ATTR_MAX, tb_data,
3385 blobmsg_data(cur), blobmsg_len(cur));
3386 else
3387 return -EINVAL;
3388
3389 msg = nlmsg_alloc_simple(RTM_NEWLINK, NLM_F_REQUEST | NLM_F_CREATE | NLM_F_EXCL);
3390
3391 if (!msg)
3392 return -1;
3393
3394 nlmsg_append(msg, &iim, sizeof(iim), 0);
3395
3396 nla_put_string(msg, IFLA_IFNAME, name);
3397
3398 if ((cur = tb_data[VXLAN_DATA_ATTR_MACADDR])) {
3399 struct ether_addr *ea = ether_aton(blobmsg_get_string(cur));
3400 if (!ea) {
3401 ret = -EINVAL;
3402 goto failure;
3403 }
3404
3405 nla_put(msg, IFLA_ADDRESS, ETH_ALEN, ea);
3406 }
3407
3408 if ((cur = tb[TUNNEL_ATTR_MTU])) {
3409 uint32_t mtu = blobmsg_get_u32(cur);
3410 nla_put_u32(msg, IFLA_MTU, mtu);
3411 }
3412
3413 if (!(linkinfo = nla_nest_start(msg, IFLA_LINKINFO))) {
3414 ret = -ENOMEM;
3415 goto failure;
3416 }
3417
3418 nla_put_string(msg, IFLA_INFO_KIND, "vxlan");
3419
3420 if (!(data = nla_nest_start(msg, IFLA_INFO_DATA))) {
3421 ret = -ENOMEM;
3422 goto failure;
3423 }
3424
3425 if (link)
3426 nla_put_u32(msg, IFLA_VXLAN_LINK, link);
3427
3428 if ((cur = tb_data[VXLAN_DATA_ATTR_ID])) {
3429 uint32_t id = blobmsg_get_u32(cur);
3430 if (id >= (1u << 24) - 1) {
3431 ret = -EINVAL;
3432 goto failure;
3433 }
3434
3435 nla_put_u32(msg, IFLA_VXLAN_ID, id);
3436 }
3437
3438 if (v6) {
3439 struct in6_addr in6buf;
3440 if ((cur = tb[TUNNEL_ATTR_LOCAL])) {
3441 if (inet_pton(AF_INET6, blobmsg_data(cur), &in6buf) < 1) {
3442 ret = -EINVAL;
3443 goto failure;
3444 }
3445 nla_put(msg, IFLA_VXLAN_LOCAL6, sizeof(in6buf), &in6buf);
3446 }
3447
3448 if ((cur = tb[TUNNEL_ATTR_REMOTE])) {
3449 if (inet_pton(AF_INET6, blobmsg_data(cur), &in6buf) < 1) {
3450 ret = -EINVAL;
3451 goto failure;
3452 }
3453 nla_put(msg, IFLA_VXLAN_GROUP6, sizeof(in6buf), &in6buf);
3454 }
3455 } else {
3456 struct in_addr inbuf;
3457
3458 if ((cur = tb[TUNNEL_ATTR_LOCAL])) {
3459 if (inet_pton(AF_INET, blobmsg_data(cur), &inbuf) < 1) {
3460 ret = -EINVAL;
3461 goto failure;
3462 }
3463 nla_put(msg, IFLA_VXLAN_LOCAL, sizeof(inbuf), &inbuf);
3464 }
3465
3466 if ((cur = tb[TUNNEL_ATTR_REMOTE])) {
3467 if (inet_pton(AF_INET, blobmsg_data(cur), &inbuf) < 1) {
3468 ret = -EINVAL;
3469 goto failure;
3470 }
3471 nla_put(msg, IFLA_VXLAN_GROUP, sizeof(inbuf), &inbuf);
3472 }
3473 }
3474
3475 uint32_t port = 4789;
3476 if ((cur = tb_data[VXLAN_DATA_ATTR_PORT])) {
3477 port = blobmsg_get_u32(cur);
3478 if (port < 1 || port > 65535) {
3479 ret = -EINVAL;
3480 goto failure;
3481 }
3482 }
3483 nla_put_u16(msg, IFLA_VXLAN_PORT, htons(port));
3484
3485 if ((cur = tb_data[VXLAN_DATA_ATTR_SRCPORTMIN])) {
3486 struct ifla_vxlan_port_range srcports = {0,0};
3487
3488 uint32_t low = blobmsg_get_u32(cur);
3489 if (low < 1 || low > 65535 - 1) {
3490 ret = -EINVAL;
3491 goto failure;
3492 }
3493
3494 srcports.low = htons((uint16_t) low);
3495 srcports.high = htons((uint16_t) (low+1));
3496
3497 if ((cur = tb_data[VXLAN_DATA_ATTR_SRCPORTMAX])) {
3498 uint32_t high = blobmsg_get_u32(cur);
3499 if (high < 1 || high > 65535) {
3500 ret = -EINVAL;
3501 goto failure;
3502 }
3503
3504 if (high > low)
3505 srcports.high = htons((uint16_t) high);
3506 }
3507
3508 nla_put(msg, IFLA_VXLAN_PORT_RANGE, sizeof(srcports), &srcports);
3509 }
3510
3511 system_vxlan_map_bool_attr(msg, tb_data, IFLA_VXLAN_UDP_CSUM, VXLAN_DATA_ATTR_TXCSUM, false);
3512 system_vxlan_map_bool_attr(msg, tb_data, IFLA_VXLAN_UDP_ZERO_CSUM6_RX, VXLAN_DATA_ATTR_RXCSUM, true);
3513 system_vxlan_map_bool_attr(msg, tb_data, IFLA_VXLAN_UDP_ZERO_CSUM6_TX, VXLAN_DATA_ATTR_TXCSUM, true);
3514 system_vxlan_map_bool_attr(msg, tb_data, IFLA_VXLAN_LEARNING, VXLAN_DATA_ATTR_LEARNING, false);
3515 system_vxlan_map_bool_attr(msg, tb_data, IFLA_VXLAN_RSC , VXLAN_DATA_ATTR_RSC, false);
3516 system_vxlan_map_bool_attr(msg, tb_data, IFLA_VXLAN_PROXY , VXLAN_DATA_ATTR_PROXY, false);
3517 system_vxlan_map_bool_attr(msg, tb_data, IFLA_VXLAN_L2MISS , VXLAN_DATA_ATTR_L2MISS, false);
3518 system_vxlan_map_bool_attr(msg, tb_data, IFLA_VXLAN_L3MISS , VXLAN_DATA_ATTR_L3MISS, false);
3519 system_vxlan_map_bool_attr(msg, tb_data, IFLA_VXLAN_GBP , VXLAN_DATA_ATTR_GBP, false);
3520
3521 if ((cur = tb_data[VXLAN_DATA_ATTR_AGEING])) {
3522 uint32_t ageing = blobmsg_get_u32(cur);
3523 nla_put_u32(msg, IFLA_VXLAN_AGEING, ageing);
3524 }
3525
3526 if ((cur = tb_data[VXLAN_DATA_ATTR_LIMIT])) {
3527 uint32_t maxaddress = blobmsg_get_u32(cur);
3528 nla_put_u32(msg, IFLA_VXLAN_LIMIT, maxaddress);
3529 }
3530
3531 if ((cur = tb[TUNNEL_ATTR_TOS])) {
3532 char *str = blobmsg_get_string(cur);
3533 unsigned tos = 1;
3534
3535 if (strcmp(str, "inherit")) {
3536 if (!system_tos_aton(str, &tos)) {
3537 ret = -EINVAL;
3538 goto failure;
3539 }
3540 }
3541
3542 nla_put_u8(msg, IFLA_VXLAN_TOS, tos);
3543 }
3544
3545 if ((cur = tb[TUNNEL_ATTR_TTL])) {
3546 uint32_t ttl = blobmsg_get_u32(cur);
3547 if (ttl < 1 || ttl > 255) {
3548 ret = -EINVAL;
3549 goto failure;
3550 }
3551
3552 nla_put_u8(msg, IFLA_VXLAN_TTL, ttl);
3553 }
3554
3555 nla_nest_end(msg, data);
3556 nla_nest_end(msg, linkinfo);
3557
3558 ret = system_rtnl_call(msg);
3559 if (ret)
3560 D(SYSTEM, "Error adding vxlan '%s': %d\n", name, ret);
3561
3562 return ret;
3563
3564 failure:
3565 nlmsg_free(msg);
3566 return ret;
3567 }
3568 #endif
3569
3570 static int system_add_sit_tunnel(const char *name, const unsigned int link, struct blob_attr **tb)
3571 {
3572 struct blob_attr *cur;
3573 int ret = 0;
3574
3575 if (system_add_proto_tunnel(name, IPPROTO_IPV6, link, tb) < 0)
3576 return -1;
3577
3578 #ifdef SIOCADD6RD
3579 if ((cur = tb[TUNNEL_ATTR_DATA])) {
3580 struct blob_attr *tb_data[__SIXRD_DATA_ATTR_MAX];
3581 unsigned int mask;
3582 struct ip_tunnel_6rd p6;
3583
3584 blobmsg_parse(sixrd_data_attr_list.params, __SIXRD_DATA_ATTR_MAX, tb_data,
3585 blobmsg_data(cur), blobmsg_len(cur));
3586
3587 memset(&p6, 0, sizeof(p6));
3588
3589 if ((cur = tb_data[SIXRD_DATA_PREFIX])) {
3590 if (!parse_ip_and_netmask(AF_INET6, blobmsg_data(cur),
3591 &p6.prefix, &mask) || mask > 128) {
3592 ret = -EINVAL;
3593 goto failure;
3594 }
3595
3596 p6.prefixlen = mask;
3597 }
3598
3599 if ((cur = tb_data[SIXRD_DATA_RELAY_PREFIX])) {
3600 if (!parse_ip_and_netmask(AF_INET, blobmsg_data(cur),
3601 &p6.relay_prefix, &mask) || mask > 32) {
3602 ret = -EINVAL;
3603 goto failure;
3604 }
3605
3606 p6.relay_prefixlen = mask;
3607 }
3608
3609 if (tunnel_ioctl(name, SIOCADD6RD, &p6) < 0) {
3610 ret = -1;
3611 goto failure;
3612 }
3613 }
3614 #endif
3615
3616 return ret;
3617
3618 failure:
3619 system_link_del(name);
3620 return ret;
3621 }
3622
3623 static int system_add_proto_tunnel(const char *name, const uint8_t proto, const unsigned int link, struct blob_attr **tb)
3624 {
3625 struct blob_attr *cur;
3626 bool set_df = true;
3627 struct ip_tunnel_parm p = {
3628 .link = link,
3629 .iph = {
3630 .version = 4,
3631 .ihl = 5,
3632 .protocol = proto,
3633 }
3634 };
3635
3636 if ((cur = tb[TUNNEL_ATTR_LOCAL]) &&
3637 inet_pton(AF_INET, blobmsg_data(cur), &p.iph.saddr) < 1)
3638 return -EINVAL;
3639
3640 if ((cur = tb[TUNNEL_ATTR_REMOTE]) &&
3641 inet_pton(AF_INET, blobmsg_data(cur), &p.iph.daddr) < 1)
3642 return -EINVAL;
3643
3644 if ((cur = tb[TUNNEL_ATTR_DF]))
3645 set_df = blobmsg_get_bool(cur);
3646
3647 if ((cur = tb[TUNNEL_ATTR_TTL]))
3648 p.iph.ttl = blobmsg_get_u32(cur);
3649
3650 if ((cur = tb[TUNNEL_ATTR_TOS])) {
3651 char *str = blobmsg_get_string(cur);
3652 if (strcmp(str, "inherit")) {
3653 unsigned uval;
3654
3655 if (!system_tos_aton(str, &uval))
3656 return -EINVAL;
3657
3658 p.iph.tos = uval;
3659 } else
3660 p.iph.tos = 1;
3661 }
3662
3663 p.iph.frag_off = set_df ? htons(IP_DF) : 0;
3664 /* ttl !=0 and nopmtudisc are incompatible */
3665 if (p.iph.ttl && p.iph.frag_off == 0)
3666 return -EINVAL;
3667
3668 strncpy(p.name, name, sizeof(p.name) - 1);
3669
3670 switch (p.iph.protocol) {
3671 case IPPROTO_IPIP:
3672 return tunnel_ioctl("tunl0", SIOCADDTUNNEL, &p);
3673 case IPPROTO_IPV6:
3674 return tunnel_ioctl("sit0", SIOCADDTUNNEL, &p);
3675 default:
3676 break;
3677 }
3678 return -1;
3679 }
3680
3681 int system_del_ip_tunnel(const struct device *dev)
3682 {
3683 return system_link_del(dev->ifname);
3684 }
3685
3686 int system_update_ipv6_mtu(struct device *dev, int mtu)
3687 {
3688 int ret = -1;
3689 char buf[64];
3690 int fd;
3691
3692 snprintf(buf, sizeof(buf), "/proc/sys/net/ipv6/conf/%s/mtu",
3693 dev->ifname);
3694
3695 fd = open(buf, O_RDWR);
3696 if (fd < 0)
3697 return ret;
3698
3699 if (!mtu) {
3700 ssize_t len = read(fd, buf, sizeof(buf) - 1);
3701 if (len < 0)
3702 goto out;
3703
3704 buf[len] = 0;
3705 ret = atoi(buf);
3706 } else {
3707 if (write(fd, buf, snprintf(buf, sizeof(buf), "%i", mtu)) > 0)
3708 ret = mtu;
3709 }
3710
3711 out:
3712 close(fd);
3713 return ret;
3714 }
3715
3716 int system_add_ip_tunnel(const struct device *dev, struct blob_attr *attr)
3717 {
3718 struct blob_attr *tb[__TUNNEL_ATTR_MAX];
3719 struct blob_attr *cur;
3720 const char *str;
3721
3722 blobmsg_parse(tunnel_attr_list.params, __TUNNEL_ATTR_MAX, tb,
3723 blob_data(attr), blob_len(attr));
3724
3725 system_link_del(dev->ifname);
3726
3727 if (!(cur = tb[TUNNEL_ATTR_TYPE]))
3728 return -EINVAL;
3729 str = blobmsg_data(cur);
3730
3731 unsigned int ttl = 0;
3732 if ((cur = tb[TUNNEL_ATTR_TTL])) {
3733 ttl = blobmsg_get_u32(cur);
3734 if (ttl > 255)
3735 return -EINVAL;
3736 }
3737
3738 unsigned int link = 0;
3739 if ((cur = tb[TUNNEL_ATTR_LINK])) {
3740 struct interface *iface = vlist_find(&interfaces, blobmsg_data(cur), iface, node);
3741 if (!iface)
3742 return -EINVAL;
3743
3744 if (iface->l3_dev.dev)
3745 link = iface->l3_dev.dev->ifindex;
3746 }
3747
3748 if (!strcmp(str, "sit"))
3749 return system_add_sit_tunnel(dev->ifname, link, tb);
3750 #ifdef IFLA_IPTUN_MAX
3751 else if (!strcmp(str, "ipip6")) {
3752 return system_add_ip6_tunnel(dev->ifname, link, tb);
3753 } else if (!strcmp(str, "greip")) {
3754 return system_add_gre_tunnel(dev->ifname, "gre", link, tb, false);
3755 } else if (!strcmp(str, "gretapip")) {
3756 return system_add_gre_tunnel(dev->ifname, "gretap", link, tb, false);
3757 } else if (!strcmp(str, "greip6")) {
3758 return system_add_gre_tunnel(dev->ifname, "ip6gre", link, tb, true);
3759 } else if (!strcmp(str, "gretapip6")) {
3760 return system_add_gre_tunnel(dev->ifname, "ip6gretap", link, tb, true);
3761 #ifdef IFLA_VTI_MAX
3762 } else if (!strcmp(str, "vtiip")) {
3763 return system_add_vti_tunnel(dev->ifname, "vti", link, tb, false);
3764 } else if (!strcmp(str, "vtiip6")) {
3765 return system_add_vti_tunnel(dev->ifname, "vti6", link, tb, true);
3766 #endif
3767 #ifdef IFLA_XFRM_MAX
3768 } else if (!strcmp(str, "xfrm")) {
3769 return system_add_xfrm_tunnel(dev->ifname, "xfrm", link, tb);
3770 #endif
3771 #ifdef IFLA_VXLAN_MAX
3772 } else if(!strcmp(str, "vxlan")) {
3773 return system_add_vxlan(dev->ifname, link, tb, false);
3774 } else if(!strcmp(str, "vxlan6")) {
3775 return system_add_vxlan(dev->ifname, link, tb, true);
3776 #endif
3777 #endif
3778 } else if (!strcmp(str, "ipip")) {
3779 return system_add_proto_tunnel(dev->ifname, IPPROTO_IPIP, link, tb);
3780 }
3781 else
3782 return -EINVAL;
3783
3784 return 0;
3785 }