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