08b675728244a44923bd7b7221e6de15183b39b8
[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  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2
10  * as published by the Free Software Foundation
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  */
17 #define _GNU_SOURCE
18
19 #include <sys/socket.h>
20 #include <sys/ioctl.h>
21 #include <sys/stat.h>
22 #include <sys/syscall.h>
23
24 #include <net/if.h>
25 #include <net/if_arp.h>
26
27 #include <arpa/inet.h>
28 #include <netinet/in.h>
29
30 #include <linux/rtnetlink.h>
31 #include <linux/sockios.h>
32 #include <linux/ip.h>
33 #include <linux/if_link.h>
34 #include <linux/if_vlan.h>
35 #include <linux/if_bridge.h>
36 #include <linux/if_tunnel.h>
37 #include <linux/ip6_tunnel.h>
38 #include <linux/ethtool.h>
39 #include <linux/fib_rules.h>
40 #include <linux/version.h>
41
42 #ifndef RTN_FAILED_POLICY
43 #define RTN_FAILED_POLICY 12
44 #endif
45
46 #ifndef RT_TABLE_PRELOCAL
47 #define RT_TABLE_PRELOCAL 128
48 #endif
49
50 #ifndef IFA_F_NOPREFIXROUTE
51 #define IFA_F_NOPREFIXROUTE 0x200
52 #endif
53
54 #include <string.h>
55 #include <fcntl.h>
56 #include <glob.h>
57 #include <time.h>
58 #include <unistd.h>
59
60 #include <netlink/msg.h>
61 #include <netlink/attr.h>
62 #include <netlink/socket.h>
63 #include <libubox/uloop.h>
64
65 #include "netifd.h"
66 #include "device.h"
67 #include "system.h"
68
69 struct event_socket {
70         struct uloop_fd uloop;
71         struct nl_sock *sock;
72         int bufsize;
73 };
74
75 static int sock_ioctl = -1;
76 static struct nl_sock *sock_rtnl = NULL;
77
78 static int cb_rtnl_event(struct nl_msg *msg, void *arg);
79 static void handle_hotplug_event(struct uloop_fd *u, unsigned int events);
80
81 static char dev_buf[256];
82
83 static void
84 handler_nl_event(struct uloop_fd *u, unsigned int events)
85 {
86         struct event_socket *ev = container_of(u, struct event_socket, uloop);
87         int err;
88         socklen_t errlen = sizeof(err);
89
90         if (!u->error) {
91                 nl_recvmsgs_default(ev->sock);
92                 return;
93         }
94
95         if (getsockopt(u->fd, SOL_SOCKET, SO_ERROR, (void *)&err, &errlen))
96                 goto abort;
97
98         switch(err) {
99         case ENOBUFS:
100                 // Increase rx buffer size on netlink socket
101                 ev->bufsize *= 2;
102                 if (nl_socket_set_buffer_size(ev->sock, ev->bufsize, 0))
103                         goto abort;
104
105                 // Request full dump since some info got dropped
106                 struct rtgenmsg msg = { .rtgen_family = AF_UNSPEC };
107                 nl_send_simple(ev->sock, RTM_GETLINK, NLM_F_DUMP, &msg, sizeof(msg));
108                 break;
109
110         default:
111                 goto abort;
112         }
113         u->error = false;
114         return;
115
116 abort:
117         uloop_fd_delete(&ev->uloop);
118         return;
119 }
120
121 static struct nl_sock *
122 create_socket(int protocol, int groups)
123 {
124         struct nl_sock *sock;
125
126         sock = nl_socket_alloc();
127         if (!sock)
128                 return NULL;
129
130         if (groups)
131                 nl_join_groups(sock, groups);
132
133         if (nl_connect(sock, protocol))
134                 return NULL;
135
136         return sock;
137 }
138
139 static bool
140 create_raw_event_socket(struct event_socket *ev, int protocol, int groups,
141                         uloop_fd_handler cb, int flags)
142 {
143         ev->sock = create_socket(protocol, groups);
144         if (!ev->sock)
145                 return false;
146
147         ev->uloop.fd = nl_socket_get_fd(ev->sock);
148         ev->uloop.cb = cb;
149         if (uloop_fd_add(&ev->uloop, ULOOP_READ|flags))
150                 return false;
151
152         return true;
153 }
154
155 static bool
156 create_event_socket(struct event_socket *ev, int protocol,
157                     int (*cb)(struct nl_msg *msg, void *arg))
158 {
159         if (!create_raw_event_socket(ev, protocol, 0, handler_nl_event, ULOOP_ERROR_CB))
160                 return false;
161
162         // Install the valid custom callback handler
163         nl_socket_modify_cb(ev->sock, NL_CB_VALID, NL_CB_CUSTOM, cb, NULL);
164
165         // Disable sequence number checking on event sockets
166         nl_socket_disable_seq_check(ev->sock);
167
168         // Increase rx buffer size to 65K on event sockets
169         ev->bufsize = 65535;
170         if (nl_socket_set_buffer_size(ev->sock, ev->bufsize, 0))
171                 return false;
172
173         return true;
174 }
175
176 static bool
177 system_rtn_aton(const char *src, unsigned int *dst)
178 {
179         char *e;
180         unsigned int n;
181
182         if (!strcmp(src, "local"))
183                 n = RTN_LOCAL;
184         else if (!strcmp(src, "nat"))
185                 n = RTN_NAT;
186         else if (!strcmp(src, "broadcast"))
187                 n = RTN_BROADCAST;
188         else if (!strcmp(src, "anycast"))
189                 n = RTN_ANYCAST;
190         else if (!strcmp(src, "multicast"))
191                 n = RTN_MULTICAST;
192         else if (!strcmp(src, "prohibit"))
193                 n = RTN_PROHIBIT;
194         else if (!strcmp(src, "unreachable"))
195                 n = RTN_UNREACHABLE;
196         else if (!strcmp(src, "blackhole"))
197                 n = RTN_BLACKHOLE;
198         else if (!strcmp(src, "xresolve"))
199                 n = RTN_XRESOLVE;
200         else if (!strcmp(src, "unicast"))
201                 n = RTN_UNICAST;
202         else if (!strcmp(src, "throw"))
203                 n = RTN_THROW;
204         else if (!strcmp(src, "failed_policy"))
205                 n = RTN_FAILED_POLICY;
206         else {
207                 n = strtoul(src, &e, 0);
208                 if (!e || *e || e == src || n > 255)
209                         return false;
210         }
211
212         *dst = n;
213         return true;
214 }
215
216 static bool
217 system_tos_aton(const char *src, unsigned *dst)
218 {
219         char *e;
220
221         *dst = strtoul(src, &e, 16);
222         if (e == src || *e || *dst > 255)
223                 return false;
224
225         return true;
226 }
227
228 int system_init(void)
229 {
230         static struct event_socket rtnl_event;
231         static struct event_socket hotplug_event;
232
233         sock_ioctl = socket(AF_LOCAL, SOCK_DGRAM, 0);
234         system_fd_set_cloexec(sock_ioctl);
235
236         // Prepare socket for routing / address control
237         sock_rtnl = create_socket(NETLINK_ROUTE, 0);
238         if (!sock_rtnl)
239                 return -1;
240
241         if (!create_event_socket(&rtnl_event, NETLINK_ROUTE, cb_rtnl_event))
242                 return -1;
243
244         if (!create_raw_event_socket(&hotplug_event, NETLINK_KOBJECT_UEVENT, 1,
245                                      handle_hotplug_event, 0))
246                 return -1;
247
248         // Receive network link events form kernel
249         nl_socket_add_membership(rtnl_event.sock, RTNLGRP_LINK);
250
251         return 0;
252 }
253
254 static void system_set_sysctl(const char *path, const char *val)
255 {
256         int fd;
257
258         fd = open(path, O_WRONLY);
259         if (fd < 0)
260                 return;
261
262         if (write(fd, val, strlen(val))) {}
263         close(fd);
264 }
265
266 static void system_set_dev_sysctl(const char *path, const char *device, const char *val)
267 {
268         snprintf(dev_buf, sizeof(dev_buf), path, device);
269         system_set_sysctl(dev_buf, val);
270 }
271
272 static void system_set_disable_ipv6(struct device *dev, const char *val)
273 {
274         system_set_dev_sysctl("/proc/sys/net/ipv6/conf/%s/disable_ipv6", dev->ifname, val);
275 }
276
277 static void system_set_rpfilter(struct device *dev, const char *val)
278 {
279         system_set_dev_sysctl("/proc/sys/net/ipv4/conf/%s/rp_filter", dev->ifname, val);
280 }
281
282 static void system_set_acceptlocal(struct device *dev, const char *val)
283 {
284         system_set_dev_sysctl("/proc/sys/net/ipv4/conf/%s/accept_local", dev->ifname, val);
285 }
286
287 static void system_set_igmpversion(struct device *dev, const char *val)
288 {
289         system_set_dev_sysctl("/proc/sys/net/ipv4/conf/%s/force_igmp_version", dev->ifname, val);
290 }
291
292 static void system_set_mldversion(struct device *dev, const char *val)
293 {
294         system_set_dev_sysctl("/proc/sys/net/ipv6/conf/%s/force_mld_version", dev->ifname, val);
295 }
296
297 static void system_set_neigh4reachabletime(struct device *dev, const char *val)
298 {
299         system_set_dev_sysctl("/proc/sys/net/ipv4/neigh/%s/base_reachable_time_ms", dev->ifname, val);
300 }
301
302 static void system_set_neigh6reachabletime(struct device *dev, const char *val)
303 {
304         system_set_dev_sysctl("/proc/sys/net/ipv6/neigh/%s/base_reachable_time_ms", dev->ifname, val);
305 }
306
307 static int system_get_sysctl(const char *path, char *buf, const size_t buf_sz)
308 {
309         int fd = -1, ret = -1;
310
311         fd = open(path, O_RDONLY);
312         if (fd < 0)
313                 goto out;
314
315         ssize_t len = read(fd, buf, buf_sz - 1);
316         if (len < 0)
317                 goto out;
318
319         ret = buf[len] = 0;
320
321 out:
322         if (fd >= 0)
323                 close(fd);
324
325         return ret;
326 }
327
328 static int
329 system_get_dev_sysctl(const char *path, const char *device, char *buf, const size_t buf_sz)
330 {
331         snprintf(dev_buf, sizeof(dev_buf), path, device);
332         return system_get_sysctl(dev_buf, buf, buf_sz);
333 }
334
335 static int system_get_disable_ipv6(struct device *dev, char *buf, const size_t buf_sz)
336 {
337         return system_get_dev_sysctl("/proc/sys/net/ipv6/conf/%s/disable_ipv6",
338                         dev->ifname, buf, buf_sz);
339 }
340
341 static int system_get_rpfilter(struct device *dev, char *buf, const size_t buf_sz)
342 {
343         return system_get_dev_sysctl("/proc/sys/net/ipv4/conf/%s/rp_filter",
344                         dev->ifname, buf, buf_sz);
345 }
346
347 static int system_get_acceptlocal(struct device *dev, char *buf, const size_t buf_sz)
348 {
349         return system_get_dev_sysctl("/proc/sys/net/ipv4/conf/%s/accept_local",
350                         dev->ifname, buf, buf_sz);
351 }
352
353 static int system_get_igmpversion(struct device *dev, char *buf, const size_t buf_sz)
354 {
355         return system_get_dev_sysctl("/proc/sys/net/ipv4/conf/%s/force_igmp_version",
356                         dev->ifname, buf, buf_sz);
357 }
358
359 static int system_get_mldversion(struct device *dev, char *buf, const size_t buf_sz)
360 {
361         return system_get_dev_sysctl("/proc/sys/net/ipv6/conf/%s/force_mld_version",
362                         dev->ifname, buf, buf_sz);
363 }
364
365 static int system_get_neigh4reachabletime(struct device *dev, char *buf, const size_t buf_sz)
366 {
367         return system_get_dev_sysctl("/proc/sys/net/ipv4/neigh/%s/base_reachable_time_ms",
368                         dev->ifname, buf, buf_sz);
369 }
370
371 static int system_get_neigh6reachabletime(struct device *dev, char *buf, const size_t buf_sz)
372 {
373         return system_get_dev_sysctl("/proc/sys/net/ipv6/neigh/%s/base_reachable_time_ms",
374                         dev->ifname, buf, buf_sz);
375 }
376
377 // Evaluate netlink messages
378 static int cb_rtnl_event(struct nl_msg *msg, void *arg)
379 {
380         struct nlmsghdr *nh = nlmsg_hdr(msg);
381         struct nlattr *nla[__IFLA_MAX];
382         int link_state = 0;
383         char buf[10];
384
385         if (nh->nlmsg_type != RTM_NEWLINK)
386                 goto out;
387
388         nlmsg_parse(nh, sizeof(struct ifinfomsg), nla, __IFLA_MAX - 1, NULL);
389         if (!nla[IFLA_IFNAME])
390                 goto out;
391
392         struct device *dev = device_get(nla_data(nla[IFLA_IFNAME]), false);
393         if (!dev || dev->type->keep_link_status)
394                 goto out;
395
396         if (!system_get_dev_sysctl("/sys/class/net/%s/carrier", dev->ifname, buf, sizeof(buf)))
397                 link_state = strtoul(buf, NULL, 0);
398
399         device_set_link(dev, link_state ? true : false);
400
401 out:
402         return 0;
403 }
404
405 static void
406 handle_hotplug_msg(char *data, int size)
407 {
408         const char *subsystem = NULL, *interface = NULL;
409         char *cur, *end, *sep;
410         struct device *dev;
411         int skip;
412         bool add;
413
414         if (!strncmp(data, "add@", 4))
415                 add = true;
416         else if (!strncmp(data, "remove@", 7))
417                 add = false;
418         else
419                 return;
420
421         skip = strlen(data) + 1;
422         end = data + size;
423
424         for (cur = data + skip; cur < end; cur += skip) {
425                 skip = strlen(cur) + 1;
426
427                 sep = strchr(cur, '=');
428                 if (!sep)
429                         continue;
430
431                 *sep = 0;
432                 if (!strcmp(cur, "INTERFACE"))
433                         interface = sep + 1;
434                 else if (!strcmp(cur, "SUBSYSTEM")) {
435                         subsystem = sep + 1;
436                         if (strcmp(subsystem, "net") != 0)
437                                 return;
438                 }
439                 if (subsystem && interface)
440                         goto found;
441         }
442         return;
443
444 found:
445         dev = device_get(interface, false);
446         if (!dev)
447                 return;
448
449         if (dev->type != &simple_device_type)
450                 return;
451
452         if (add && system_if_force_external(dev->ifname))
453                 return;
454
455         device_set_present(dev, add);
456 }
457
458 static void
459 handle_hotplug_event(struct uloop_fd *u, unsigned int events)
460 {
461         struct event_socket *ev = container_of(u, struct event_socket, uloop);
462         struct sockaddr_nl nla;
463         unsigned char *buf = NULL;
464         int size;
465
466         while ((size = nl_recv(ev->sock, &nla, &buf, NULL)) > 0) {
467                 if (nla.nl_pid == 0)
468                         handle_hotplug_msg((char *) buf, size);
469
470                 free(buf);
471         }
472 }
473
474 static int system_rtnl_call(struct nl_msg *msg)
475 {
476         int ret;
477
478         ret = nl_send_auto_complete(sock_rtnl, msg);
479         nlmsg_free(msg);
480
481         if (ret < 0)
482                 return ret;
483
484         return nl_wait_for_ack(sock_rtnl);
485 }
486
487 int system_bridge_delbr(struct device *bridge)
488 {
489         return ioctl(sock_ioctl, SIOCBRDELBR, bridge->ifname);
490 }
491
492 static int system_bridge_if(const char *bridge, struct device *dev, int cmd, void *data)
493 {
494         struct ifreq ifr;
495
496         memset(&ifr, 0, sizeof(ifr));
497         if (dev)
498                 ifr.ifr_ifindex = dev->ifindex;
499         else
500                 ifr.ifr_data = data;
501         strncpy(ifr.ifr_name, bridge, sizeof(ifr.ifr_name));
502         return ioctl(sock_ioctl, cmd, &ifr);
503 }
504
505 static bool system_is_bridge(const char *name, char *buf, int buflen)
506 {
507         struct stat st;
508
509         snprintf(buf, buflen, "/sys/devices/virtual/net/%s/bridge", name);
510         if (stat(buf, &st) < 0)
511                 return false;
512
513         return true;
514 }
515
516 static char *system_get_bridge(const char *name, char *buf, int buflen)
517 {
518         char *path;
519         ssize_t len = -1;
520         glob_t gl;
521
522         snprintf(buf, buflen, "/sys/devices/virtual/net/*/brif/%s/bridge", name);
523         if (glob(buf, GLOB_NOSORT, NULL, &gl) < 0)
524                 return NULL;
525
526         if (gl.gl_pathc > 0)
527                 len = readlink(gl.gl_pathv[0], buf, buflen);
528
529         globfree(&gl);
530
531         if (len < 0)
532                 return NULL;
533
534         buf[len] = 0;
535         path = strrchr(buf, '/');
536         if (!path)
537                 return NULL;
538
539         return path + 1;
540 }
541
542 static void system_bridge_set_wireless(const char *bridge, const char *dev)
543 {
544         snprintf(dev_buf, sizeof(dev_buf),
545                  "/sys/devices/virtual/net/%s/brif/%s/multicast_to_unicast",
546                  bridge, dev);
547         system_set_sysctl(dev_buf, "1");
548 }
549
550 int system_bridge_addif(struct device *bridge, struct device *dev)
551 {
552         char *oldbr;
553         int ret = 0;
554
555         oldbr = system_get_bridge(dev->ifname, dev_buf, sizeof(dev_buf));
556         if (!oldbr || strcmp(oldbr, bridge->ifname) != 0)
557                 ret = system_bridge_if(bridge->ifname, dev, SIOCBRADDIF, NULL);
558
559         if (dev->wireless)
560                 system_bridge_set_wireless(bridge->ifname, dev->ifname);
561
562         return ret;
563 }
564
565 int system_bridge_delif(struct device *bridge, struct device *dev)
566 {
567         return system_bridge_if(bridge->ifname, dev, SIOCBRDELIF, NULL);
568 }
569
570 int system_if_resolve(struct device *dev)
571 {
572         struct ifreq ifr;
573         strncpy(ifr.ifr_name, dev->ifname, sizeof(ifr.ifr_name));
574         if (!ioctl(sock_ioctl, SIOCGIFINDEX, &ifr))
575                 return ifr.ifr_ifindex;
576         else
577                 return 0;
578 }
579
580 static int system_if_flags(const char *ifname, unsigned add, unsigned rem)
581 {
582         struct ifreq ifr;
583
584         memset(&ifr, 0, sizeof(ifr));
585         strncpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
586         ioctl(sock_ioctl, SIOCGIFFLAGS, &ifr);
587         ifr.ifr_flags |= add;
588         ifr.ifr_flags &= ~rem;
589         return ioctl(sock_ioctl, SIOCSIFFLAGS, &ifr);
590 }
591
592 struct clear_data {
593         struct nl_msg *msg;
594         struct device *dev;
595         int type;
596         int size;
597         int af;
598 };
599
600
601 static bool check_ifaddr(struct nlmsghdr *hdr, int ifindex)
602 {
603         struct ifaddrmsg *ifa = NLMSG_DATA(hdr);
604
605         return ifa->ifa_index == ifindex;
606 }
607
608 static bool check_route(struct nlmsghdr *hdr, int ifindex)
609 {
610         struct rtmsg *r = NLMSG_DATA(hdr);
611         struct nlattr *tb[__RTA_MAX];
612
613         if (r->rtm_protocol == RTPROT_KERNEL &&
614             r->rtm_family == AF_INET6)
615                 return false;
616
617         nlmsg_parse(hdr, sizeof(struct rtmsg), tb, __RTA_MAX - 1, NULL);
618         if (!tb[RTA_OIF])
619                 return false;
620
621         return *(int *)RTA_DATA(tb[RTA_OIF]) == ifindex;
622 }
623
624 static bool check_rule(struct nlmsghdr *hdr, int ifindex)
625 {
626         return true;
627 }
628
629 static int cb_clear_event(struct nl_msg *msg, void *arg)
630 {
631         struct clear_data *clr = arg;
632         struct nlmsghdr *hdr = nlmsg_hdr(msg);
633         bool (*cb)(struct nlmsghdr *, int ifindex);
634         int type;
635
636         switch(clr->type) {
637         case RTM_GETADDR:
638                 type = RTM_DELADDR;
639                 if (hdr->nlmsg_type != RTM_NEWADDR)
640                         return NL_SKIP;
641
642                 cb = check_ifaddr;
643                 break;
644         case RTM_GETROUTE:
645                 type = RTM_DELROUTE;
646                 if (hdr->nlmsg_type != RTM_NEWROUTE)
647                         return NL_SKIP;
648
649                 cb = check_route;
650                 break;
651         case RTM_GETRULE:
652                 type = RTM_DELRULE;
653                 if (hdr->nlmsg_type != RTM_NEWRULE)
654                         return NL_SKIP;
655
656                 cb = check_rule;
657                 break;
658         default:
659                 return NL_SKIP;
660         }
661
662         if (!cb(hdr, clr->dev ? clr->dev->ifindex : 0))
663                 return NL_SKIP;
664
665         if (type == RTM_DELRULE)
666                 D(SYSTEM, "Remove a rule\n");
667         else
668                 D(SYSTEM, "Remove %s from device %s\n",
669                   type == RTM_DELADDR ? "an address" : "a route",
670                   clr->dev->ifname);
671         memcpy(nlmsg_hdr(clr->msg), hdr, hdr->nlmsg_len);
672         hdr = nlmsg_hdr(clr->msg);
673         hdr->nlmsg_type = type;
674         hdr->nlmsg_flags = NLM_F_REQUEST;
675
676         nl_socket_disable_auto_ack(sock_rtnl);
677         nl_send_auto_complete(sock_rtnl, clr->msg);
678         nl_socket_enable_auto_ack(sock_rtnl);
679
680         return NL_SKIP;
681 }
682
683 static int
684 cb_finish_event(struct nl_msg *msg, void *arg)
685 {
686         int *pending = arg;
687         *pending = 0;
688         return NL_STOP;
689 }
690
691 static int
692 error_handler(struct sockaddr_nl *nla, struct nlmsgerr *err, void *arg)
693 {
694         int *pending = arg;
695         *pending = err->error;
696         return NL_STOP;
697 }
698
699 static void
700 system_if_clear_entries(struct device *dev, int type, int af)
701 {
702         struct clear_data clr;
703         struct nl_cb *cb = nl_cb_alloc(NL_CB_DEFAULT);
704         struct rtmsg rtm = {
705                 .rtm_family = af,
706                 .rtm_flags = RTM_F_CLONED,
707         };
708         int flags = NLM_F_DUMP;
709         int pending = 1;
710
711         clr.af = af;
712         clr.dev = dev;
713         clr.type = type;
714         switch (type) {
715         case RTM_GETADDR:
716         case RTM_GETRULE:
717                 clr.size = sizeof(struct rtgenmsg);
718                 break;
719         case RTM_GETROUTE:
720                 clr.size = sizeof(struct rtmsg);
721                 break;
722         default:
723                 return;
724         }
725
726         if (!cb)
727                 return;
728
729         clr.msg = nlmsg_alloc_simple(type, flags);
730         if (!clr.msg)
731                 goto out;
732
733         nlmsg_append(clr.msg, &rtm, clr.size, 0);
734         nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM, cb_clear_event, &clr);
735         nl_cb_set(cb, NL_CB_FINISH, NL_CB_CUSTOM, cb_finish_event, &pending);
736         nl_cb_err(cb, NL_CB_CUSTOM, error_handler, &pending);
737
738         nl_send_auto_complete(sock_rtnl, clr.msg);
739         while (pending > 0)
740                 nl_recvmsgs(sock_rtnl, cb);
741
742         nlmsg_free(clr.msg);
743 out:
744         nl_cb_put(cb);
745 }
746
747 /*
748  * Clear bridge (membership) state and bring down device
749  */
750 void system_if_clear_state(struct device *dev)
751 {
752         static char buf[256];
753         char *bridge;
754
755         device_set_ifindex(dev, system_if_resolve(dev));
756         if (dev->external || !dev->ifindex)
757                 return;
758
759         system_if_flags(dev->ifname, 0, IFF_UP);
760
761         if (system_is_bridge(dev->ifname, buf, sizeof(buf))) {
762                 D(SYSTEM, "Delete existing bridge named '%s'\n", dev->ifname);
763                 system_bridge_delbr(dev);
764                 return;
765         }
766
767         bridge = system_get_bridge(dev->ifname, buf, sizeof(buf));
768         if (bridge) {
769                 D(SYSTEM, "Remove device '%s' from bridge '%s'\n", dev->ifname, bridge);
770                 system_bridge_if(bridge, dev, SIOCBRDELIF, NULL);
771         }
772
773         system_if_clear_entries(dev, RTM_GETROUTE, AF_INET);
774         system_if_clear_entries(dev, RTM_GETADDR, AF_INET);
775         system_if_clear_entries(dev, RTM_GETROUTE, AF_INET6);
776         system_if_clear_entries(dev, RTM_GETADDR, AF_INET6);
777         system_set_disable_ipv6(dev, "0");
778 }
779
780 static inline unsigned long
781 sec_to_jiffies(int val)
782 {
783         return (unsigned long) val * 100;
784 }
785
786 int system_bridge_addbr(struct device *bridge, struct bridge_config *cfg)
787 {
788         unsigned long args[4] = {};
789
790         if (ioctl(sock_ioctl, SIOCBRADDBR, bridge->ifname) < 0)
791                 return -1;
792
793         args[0] = BRCTL_SET_BRIDGE_STP_STATE;
794         args[1] = !!cfg->stp;
795         system_bridge_if(bridge->ifname, NULL, SIOCDEVPRIVATE, &args);
796
797         args[0] = BRCTL_SET_BRIDGE_FORWARD_DELAY;
798         args[1] = sec_to_jiffies(cfg->forward_delay);
799         system_bridge_if(bridge->ifname, NULL, SIOCDEVPRIVATE, &args);
800
801         system_set_dev_sysctl("/sys/devices/virtual/net/%s/bridge/multicast_snooping",
802                 bridge->ifname, cfg->igmp_snoop ? "1" : "0");
803
804         system_set_dev_sysctl("/sys/devices/virtual/net/%s/bridge/multicast_querier",
805                 bridge->ifname, cfg->multicast_querier ? "1" : "0");
806
807         args[0] = BRCTL_SET_BRIDGE_PRIORITY;
808         args[1] = cfg->priority;
809         system_bridge_if(bridge->ifname, NULL, SIOCDEVPRIVATE, &args);
810
811         if (cfg->flags & BRIDGE_OPT_AGEING_TIME) {
812                 args[0] = BRCTL_SET_AGEING_TIME;
813                 args[1] = sec_to_jiffies(cfg->ageing_time);
814                 system_bridge_if(bridge->ifname, NULL, SIOCDEVPRIVATE, &args);
815         }
816
817         if (cfg->flags & BRIDGE_OPT_HELLO_TIME) {
818                 args[0] = BRCTL_SET_BRIDGE_HELLO_TIME;
819                 args[1] = sec_to_jiffies(cfg->hello_time);
820                 system_bridge_if(bridge->ifname, NULL, SIOCDEVPRIVATE, &args);
821         }
822
823         if (cfg->flags & BRIDGE_OPT_MAX_AGE) {
824                 args[0] = BRCTL_SET_BRIDGE_MAX_AGE;
825                 args[1] = sec_to_jiffies(cfg->max_age);
826                 system_bridge_if(bridge->ifname, NULL, SIOCDEVPRIVATE, &args);
827         }
828
829         return 0;
830 }
831
832 int system_macvlan_add(struct device *macvlan, struct device *dev, struct macvlan_config *cfg)
833 {
834         struct nl_msg *msg;
835         struct nlattr *linkinfo, *data;
836         struct ifinfomsg iim = { .ifi_family = AF_UNSPEC, };
837         int i, rv;
838         static const struct {
839                 const char *name;
840                 enum macvlan_mode val;
841         } modes[] = {
842                 { "private", MACVLAN_MODE_PRIVATE },
843                 { "vepa", MACVLAN_MODE_VEPA },
844                 { "bridge", MACVLAN_MODE_BRIDGE },
845                 { "passthru", MACVLAN_MODE_PASSTHRU },
846         };
847
848         msg = nlmsg_alloc_simple(RTM_NEWLINK, NLM_F_REQUEST | NLM_F_CREATE | NLM_F_EXCL);
849
850         if (!msg)
851                 return -1;
852
853         nlmsg_append(msg, &iim, sizeof(iim), 0);
854
855         if (cfg->flags & MACVLAN_OPT_MACADDR)
856                 nla_put(msg, IFLA_ADDRESS, sizeof(cfg->macaddr), cfg->macaddr);
857         nla_put_string(msg, IFLA_IFNAME, macvlan->ifname);
858         nla_put_u32(msg, IFLA_LINK, dev->ifindex);
859
860         if (!(linkinfo = nla_nest_start(msg, IFLA_LINKINFO)))
861                 goto nla_put_failure;
862
863         nla_put_string(msg, IFLA_INFO_KIND, "macvlan");
864
865         if (!(data = nla_nest_start(msg, IFLA_INFO_DATA)))
866                 goto nla_put_failure;
867
868         if (cfg->mode) {
869                 for (i = 0; i < ARRAY_SIZE(modes); i++) {
870                         if (strcmp(cfg->mode, modes[i].name) != 0)
871                                 continue;
872
873                         nla_put_u32(msg, IFLA_MACVLAN_MODE, modes[i].val);
874                         break;
875                 }
876         }
877
878         nla_nest_end(msg, data);
879         nla_nest_end(msg, linkinfo);
880
881         rv = system_rtnl_call(msg);
882         if (rv)
883                 D(SYSTEM, "Error adding macvlan '%s' over '%s': %d\n", macvlan->ifname, dev->ifname, rv);
884
885         return rv;
886
887 nla_put_failure:
888         nlmsg_free(msg);
889         return -ENOMEM;
890 }
891
892 static int system_link_del(const char *ifname)
893 {
894         struct nl_msg *msg;
895         struct ifinfomsg iim = {
896                 .ifi_family = AF_UNSPEC,
897                 .ifi_index = 0,
898         };
899
900         msg = nlmsg_alloc_simple(RTM_DELLINK, NLM_F_REQUEST);
901
902         if (!msg)
903                 return -1;
904
905         nlmsg_append(msg, &iim, sizeof(iim), 0);
906         nla_put_string(msg, IFLA_IFNAME, ifname);
907         return system_rtnl_call(msg);
908 }
909
910 int system_macvlan_del(struct device *macvlan)
911 {
912         return system_link_del(macvlan->ifname);
913 }
914
915 static int system_vlan(struct device *dev, int id)
916 {
917         struct vlan_ioctl_args ifr = {
918                 .cmd = SET_VLAN_NAME_TYPE_CMD,
919                 .u.name_type = VLAN_NAME_TYPE_RAW_PLUS_VID_NO_PAD,
920         };
921
922         ioctl(sock_ioctl, SIOCSIFVLAN, &ifr);
923
924         if (id < 0) {
925                 ifr.cmd = DEL_VLAN_CMD;
926                 ifr.u.VID = 0;
927         } else {
928                 ifr.cmd = ADD_VLAN_CMD;
929                 ifr.u.VID = id;
930         }
931         strncpy(ifr.device1, dev->ifname, sizeof(ifr.device1));
932         return ioctl(sock_ioctl, SIOCSIFVLAN, &ifr);
933 }
934
935 int system_vlan_add(struct device *dev, int id)
936 {
937         return system_vlan(dev, id);
938 }
939
940 int system_vlan_del(struct device *dev)
941 {
942         return system_vlan(dev, -1);
943 }
944
945 int system_vlandev_add(struct device *vlandev, struct device *dev, struct vlandev_config *cfg)
946 {
947         struct nl_msg *msg;
948         struct nlattr *linkinfo, *data;
949         struct ifinfomsg iim = { .ifi_family = AF_UNSPEC };
950         int rv;
951
952         msg = nlmsg_alloc_simple(RTM_NEWLINK, NLM_F_REQUEST | NLM_F_CREATE | NLM_F_EXCL);
953
954         if (!msg)
955                 return -1;
956
957         nlmsg_append(msg, &iim, sizeof(iim), 0);
958         nla_put_string(msg, IFLA_IFNAME, vlandev->ifname);
959         nla_put_u32(msg, IFLA_LINK, dev->ifindex);
960         
961         if (!(linkinfo = nla_nest_start(msg, IFLA_LINKINFO)))
962                 goto nla_put_failure;
963         
964         nla_put_string(msg, IFLA_INFO_KIND, "vlan");
965
966         if (!(data = nla_nest_start(msg, IFLA_INFO_DATA)))
967                 goto nla_put_failure;
968
969         nla_put_u16(msg, IFLA_VLAN_ID, cfg->vid);
970
971 #if LINUX_VERSION_CODE >= KERNEL_VERSION(3,10,0)
972         nla_put_u16(msg, IFLA_VLAN_PROTOCOL, htons(cfg->proto));
973 #else
974         if(cfg->proto == VLAN_PROTO_8021AD)
975                 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);
976 #endif
977
978         nla_nest_end(msg, data);
979         nla_nest_end(msg, linkinfo);
980
981         rv = system_rtnl_call(msg);
982         if (rv)
983                 D(SYSTEM, "Error adding vlandev '%s' over '%s': %d\n", vlandev->ifname, dev->ifname, rv);
984
985         return rv;
986
987 nla_put_failure:
988         nlmsg_free(msg);
989         return -ENOMEM;
990 }
991
992 int system_vlandev_del(struct device *vlandev)
993 {
994         return system_link_del(vlandev->ifname);
995 }
996
997 static void
998 system_if_get_settings(struct device *dev, struct device_settings *s)
999 {
1000         struct ifreq ifr;
1001         char buf[10];
1002
1003         memset(&ifr, 0, sizeof(ifr));
1004         strncpy(ifr.ifr_name, dev->ifname, sizeof(ifr.ifr_name));
1005
1006         if (ioctl(sock_ioctl, SIOCGIFMTU, &ifr) == 0) {
1007                 s->mtu = ifr.ifr_mtu;
1008                 s->flags |= DEV_OPT_MTU;
1009         }
1010
1011         if (ioctl(sock_ioctl, SIOCGIFTXQLEN, &ifr) == 0) {
1012                 s->txqueuelen = ifr.ifr_qlen;
1013                 s->flags |= DEV_OPT_TXQUEUELEN;
1014         }
1015
1016         if (ioctl(sock_ioctl, SIOCGIFHWADDR, &ifr) == 0) {
1017                 memcpy(s->macaddr, &ifr.ifr_hwaddr.sa_data, sizeof(s->macaddr));
1018                 s->flags |= DEV_OPT_MACADDR;
1019         }
1020
1021         if (!system_get_disable_ipv6(dev, buf, sizeof(buf))) {
1022                 s->ipv6 = !strtoul(buf, NULL, 0);
1023                 s->flags |= DEV_OPT_IPV6;
1024         }
1025
1026         if (ioctl(sock_ioctl, SIOCGIFFLAGS, &ifr) == 0) {
1027                 s->promisc = ifr.ifr_flags & IFF_PROMISC;
1028                 s->flags |= DEV_OPT_PROMISC;
1029         }
1030
1031         if (!system_get_rpfilter(dev, buf, sizeof(buf))) {
1032                 s->rpfilter = strtoul(buf, NULL, 0);
1033                 s->flags |= DEV_OPT_RPFILTER;
1034         }
1035
1036         if (!system_get_acceptlocal(dev, buf, sizeof(buf))) {
1037                 s->acceptlocal = strtoul(buf, NULL, 0);
1038                 s->flags |= DEV_OPT_ACCEPTLOCAL;
1039         }
1040
1041         if (!system_get_igmpversion(dev, buf, sizeof(buf))) {
1042                 s->igmpversion = strtoul(buf, NULL, 0);
1043                 s->flags |= DEV_OPT_IGMPVERSION;
1044         }
1045
1046         if (!system_get_mldversion(dev, buf, sizeof(buf))) {
1047                 s->mldversion = strtoul(buf, NULL, 0);
1048                 s->flags |= DEV_OPT_MLDVERSION;
1049         }
1050
1051         if (!system_get_neigh4reachabletime(dev, buf, sizeof(buf))) {
1052                 s->neigh4reachabletime = strtoul(buf, NULL, 0);
1053                 s->flags |= DEV_OPT_NEIGHREACHABLETIME;
1054         }
1055
1056         if (!system_get_neigh6reachabletime(dev, buf, sizeof(buf))) {
1057                 s->neigh6reachabletime = strtoul(buf, NULL, 0);
1058                 s->flags |= DEV_OPT_NEIGHREACHABLETIME;
1059         }
1060 }
1061
1062 static void
1063 system_if_set_rps_xps_val(const char *path, int val)
1064 {
1065         char val_buf[8];
1066         glob_t gl;
1067         int i;
1068
1069         if (glob(path, 0, NULL, &gl))
1070                 return;
1071
1072         snprintf(val_buf, sizeof(val_buf), "%x", val);
1073         for (i = 0; i < gl.gl_pathc; i++)
1074                 system_set_sysctl(gl.gl_pathv[i], val_buf);
1075 }
1076
1077 static void
1078 system_if_apply_rps_xps(struct device *dev, struct device_settings *s)
1079 {
1080         long n_cpus = sysconf(_SC_NPROCESSORS_ONLN);
1081         int val;
1082
1083         if (n_cpus < 2)
1084                 return;
1085
1086         val = (1 << n_cpus) - 1;
1087         snprintf(dev_buf, sizeof(dev_buf), "/sys/class/net/%s/queues/*/rps_cpus", dev->ifname);
1088         system_if_set_rps_xps_val(dev_buf, s->rps ? val : 0);
1089
1090         snprintf(dev_buf, sizeof(dev_buf), "/sys/class/net/%s/queues/*/xps_cpus", dev->ifname);
1091         system_if_set_rps_xps_val(dev_buf, s->xps ? val : 0);
1092 }
1093
1094 void
1095 system_if_apply_settings(struct device *dev, struct device_settings *s, unsigned int apply_mask)
1096 {
1097         struct ifreq ifr;
1098
1099         memset(&ifr, 0, sizeof(ifr));
1100         strncpy(ifr.ifr_name, dev->ifname, sizeof(ifr.ifr_name));
1101         if (s->flags & DEV_OPT_MTU & apply_mask) {
1102                 ifr.ifr_mtu = s->mtu;
1103                 if (ioctl(sock_ioctl, SIOCSIFMTU, &ifr) < 0)
1104                         s->flags &= ~DEV_OPT_MTU;
1105         }
1106         if (s->flags & DEV_OPT_TXQUEUELEN & apply_mask) {
1107                 ifr.ifr_qlen = s->txqueuelen;
1108                 if (ioctl(sock_ioctl, SIOCSIFTXQLEN, &ifr) < 0)
1109                         s->flags &= ~DEV_OPT_TXQUEUELEN;
1110         }
1111         if ((s->flags & DEV_OPT_MACADDR & apply_mask) && !dev->external) {
1112                 ifr.ifr_hwaddr.sa_family = ARPHRD_ETHER;
1113                 memcpy(&ifr.ifr_hwaddr.sa_data, s->macaddr, sizeof(s->macaddr));
1114                 if (ioctl(sock_ioctl, SIOCSIFHWADDR, &ifr) < 0)
1115                         s->flags &= ~DEV_OPT_MACADDR;
1116         }
1117         if (s->flags & DEV_OPT_IPV6 & apply_mask)
1118                 system_set_disable_ipv6(dev, s->ipv6 ? "0" : "1");
1119         if (s->flags & DEV_OPT_PROMISC & apply_mask) {
1120                 if (system_if_flags(dev->ifname, s->promisc ? IFF_PROMISC : 0,
1121                                     !s->promisc ? IFF_PROMISC : 0) < 0)
1122                         s->flags &= ~DEV_OPT_PROMISC;
1123         }
1124         if (s->flags & DEV_OPT_RPFILTER & apply_mask) {
1125                 char buf[2];
1126
1127                 snprintf(buf, sizeof(buf), "%d", s->rpfilter);
1128                 system_set_rpfilter(dev, buf);
1129         }
1130         if (s->flags & DEV_OPT_ACCEPTLOCAL & apply_mask)
1131                 system_set_acceptlocal(dev, s->acceptlocal ? "1" : "0");
1132         if (s->flags & DEV_OPT_IGMPVERSION & apply_mask) {
1133                 char buf[2];
1134
1135                 snprintf(buf, sizeof(buf), "%d", s->igmpversion);
1136                 system_set_igmpversion(dev, buf);
1137         }
1138         if (s->flags & DEV_OPT_MLDVERSION & apply_mask) {
1139                 char buf[2];
1140
1141                 snprintf(buf, sizeof(buf), "%d", s->mldversion);
1142                 system_set_mldversion(dev, buf);
1143         }
1144         if (s->flags & DEV_OPT_NEIGHREACHABLETIME & apply_mask) {
1145                 char buf[12];
1146
1147                 snprintf(buf, sizeof(buf), "%d", s->neigh4reachabletime);
1148                 system_set_neigh4reachabletime(dev, buf);
1149                 snprintf(buf, sizeof(buf), "%d", s->neigh6reachabletime);
1150                 system_set_neigh6reachabletime(dev, buf);
1151         }
1152
1153         system_if_apply_rps_xps(dev, s);
1154 }
1155
1156 int system_if_up(struct device *dev)
1157 {
1158         system_if_get_settings(dev, &dev->orig_settings);
1159         /* Only keep orig settings based on what needs to be set */
1160         dev->orig_settings.flags &= dev->settings.flags;
1161         system_if_apply_settings(dev, &dev->settings, dev->settings.flags);
1162         return system_if_flags(dev->ifname, IFF_UP, 0);
1163 }
1164
1165 int system_if_down(struct device *dev)
1166 {
1167         int ret = system_if_flags(dev->ifname, 0, IFF_UP);
1168         system_if_apply_settings(dev, &dev->orig_settings, dev->orig_settings.flags);
1169         return ret;
1170 }
1171
1172 struct if_check_data {
1173         struct device *dev;
1174         int pending;
1175         int ret;
1176 };
1177
1178 #ifndef IFF_LOWER_UP
1179 #define IFF_LOWER_UP    0x10000
1180 #endif
1181
1182 static int cb_if_check_valid(struct nl_msg *msg, void *arg)
1183 {
1184         struct nlmsghdr *nh = nlmsg_hdr(msg);
1185         struct ifinfomsg *ifi = NLMSG_DATA(nh);
1186         struct if_check_data *chk = (struct if_check_data *)arg;
1187
1188         if (nh->nlmsg_type != RTM_NEWLINK)
1189                 return NL_SKIP;
1190
1191         device_set_present(chk->dev, ifi->ifi_index > 0 ? true : false);
1192         device_set_link(chk->dev, ifi->ifi_flags & IFF_LOWER_UP ? true : false);
1193
1194         return NL_OK;
1195 }
1196
1197 static int cb_if_check_ack(struct nl_msg *msg, void *arg)
1198 {
1199         struct if_check_data *chk = (struct if_check_data *)arg;
1200         chk->pending = 0;
1201         return NL_STOP;
1202 }
1203
1204 static int cb_if_check_error(struct sockaddr_nl *nla, struct nlmsgerr *err, void *arg)
1205 {
1206         struct if_check_data *chk = (struct if_check_data *)arg;
1207
1208         device_set_present(chk->dev, false);
1209         device_set_link(chk->dev, false);
1210         chk->pending = err->error;
1211
1212         return NL_STOP;
1213 }
1214
1215 int system_if_check(struct device *dev)
1216 {
1217         struct nl_cb *cb = nl_cb_alloc(NL_CB_DEFAULT);
1218         struct nl_msg *msg;
1219         struct ifinfomsg ifi = {
1220                 .ifi_family = AF_UNSPEC,
1221                 .ifi_index = 0,
1222         };
1223         struct if_check_data chk = {
1224                 .dev = dev,
1225                 .pending = 1,
1226         };
1227         int ret = 1;
1228
1229         msg = nlmsg_alloc_simple(RTM_GETLINK, 0);
1230         if (!msg || nlmsg_append(msg, &ifi, sizeof(ifi), 0) ||
1231             nla_put_string(msg, IFLA_IFNAME, dev->ifname))
1232                 goto out;
1233
1234         nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM, cb_if_check_valid, &chk);
1235         nl_cb_set(cb, NL_CB_ACK, NL_CB_CUSTOM, cb_if_check_ack, &chk);
1236         nl_cb_err(cb, NL_CB_CUSTOM, cb_if_check_error, &chk);
1237
1238         nl_send_auto_complete(sock_rtnl, msg);
1239         while (chk.pending > 0)
1240                 nl_recvmsgs(sock_rtnl, cb);
1241
1242         nlmsg_free(msg);
1243         ret = chk.pending;
1244
1245 out:
1246         nl_cb_put(cb);
1247         return ret;
1248 }
1249
1250 struct device *
1251 system_if_get_parent(struct device *dev)
1252 {
1253         char buf[64], *devname;
1254         int ifindex, iflink, len;
1255         FILE *f;
1256
1257         snprintf(buf, sizeof(buf), "/sys/class/net/%s/iflink", dev->ifname);
1258         f = fopen(buf, "r");
1259         if (!f)
1260                 return NULL;
1261
1262         len = fread(buf, 1, sizeof(buf) - 1, f);
1263         fclose(f);
1264
1265         if (len <= 0)
1266                 return NULL;
1267
1268         buf[len] = 0;
1269         iflink = strtoul(buf, NULL, 0);
1270         ifindex = system_if_resolve(dev);
1271         if (!iflink || iflink == ifindex)
1272                 return NULL;
1273
1274         devname = if_indextoname(iflink, buf);
1275         if (!devname)
1276                 return NULL;
1277
1278         return device_get(devname, true);
1279 }
1280
1281 static bool
1282 read_string_file(int dir_fd, const char *file, char *buf, int len)
1283 {
1284         bool ret = false;
1285         char *c;
1286         int fd;
1287
1288         fd = openat(dir_fd, file, O_RDONLY);
1289         if (fd < 0)
1290                 return false;
1291
1292 retry:
1293         len = read(fd, buf, len - 1);
1294         if (len < 0) {
1295                 if (errno == EINTR)
1296                         goto retry;
1297         } else if (len > 0) {
1298                         buf[len] = 0;
1299
1300                         c = strchr(buf, '\n');
1301                         if (c)
1302                                 *c = 0;
1303
1304                         ret = true;
1305         }
1306
1307         close(fd);
1308
1309         return ret;
1310 }
1311
1312 static bool
1313 read_uint64_file(int dir_fd, const char *file, uint64_t *val)
1314 {
1315         char buf[64];
1316         bool ret = false;
1317
1318         ret = read_string_file(dir_fd, file, buf, sizeof(buf));
1319         if (ret)
1320                 *val = strtoull(buf, NULL, 0);
1321
1322         return ret;
1323 }
1324
1325 /* Assume advertised flags == supported flags */
1326 static const struct {
1327         uint32_t mask;
1328         const char *name;
1329 } ethtool_link_modes[] = {
1330         { ADVERTISED_10baseT_Half, "10H" },
1331         { ADVERTISED_10baseT_Full, "10F" },
1332         { ADVERTISED_100baseT_Half, "100H" },
1333         { ADVERTISED_100baseT_Full, "100F" },
1334         { ADVERTISED_1000baseT_Half, "1000H" },
1335         { ADVERTISED_1000baseT_Full, "1000F" },
1336 };
1337
1338 static void system_add_link_modes(struct blob_buf *b, __u32 mask)
1339 {
1340         int i;
1341         for (i = 0; i < ARRAY_SIZE(ethtool_link_modes); i++) {
1342                 if (mask & ethtool_link_modes[i].mask)
1343                         blobmsg_add_string(b, NULL, ethtool_link_modes[i].name);
1344         }
1345 }
1346
1347 bool
1348 system_if_force_external(const char *ifname)
1349 {
1350         char buf[64];
1351         struct stat s;
1352
1353         snprintf(buf, sizeof(buf), "/sys/class/net/%s/phy80211", ifname);
1354         return stat(buf, &s) == 0;
1355 }
1356
1357 int
1358 system_if_dump_info(struct device *dev, struct blob_buf *b)
1359 {
1360         struct ethtool_cmd ecmd;
1361         struct ifreq ifr;
1362         char buf[64], *s;
1363         void *c;
1364         int dir_fd;
1365
1366         snprintf(buf, sizeof(buf), "/sys/class/net/%s", dev->ifname);
1367         dir_fd = open(buf, O_DIRECTORY);
1368
1369         memset(&ecmd, 0, sizeof(ecmd));
1370         memset(&ifr, 0, sizeof(ifr));
1371         strcpy(ifr.ifr_name, dev->ifname);
1372         ifr.ifr_data = (caddr_t) &ecmd;
1373         ecmd.cmd = ETHTOOL_GSET;
1374
1375         if (ioctl(sock_ioctl, SIOCETHTOOL, &ifr) == 0) {
1376                 c = blobmsg_open_array(b, "link-advertising");
1377                 system_add_link_modes(b, ecmd.advertising);
1378                 blobmsg_close_array(b, c);
1379
1380                 c = blobmsg_open_array(b, "link-supported");
1381                 system_add_link_modes(b, ecmd.supported);
1382                 blobmsg_close_array(b, c);
1383
1384                 s = blobmsg_alloc_string_buffer(b, "speed", 8);
1385                 snprintf(s, 8, "%d%c", ethtool_cmd_speed(&ecmd),
1386                         ecmd.duplex == DUPLEX_HALF ? 'H' : 'F');
1387                 blobmsg_add_string_buffer(b);
1388         }
1389
1390         close(dir_fd);
1391         return 0;
1392 }
1393
1394 int
1395 system_if_dump_stats(struct device *dev, struct blob_buf *b)
1396 {
1397         const char *const counters[] = {
1398                 "collisions",     "rx_frame_errors",   "tx_compressed",
1399                 "multicast",      "rx_length_errors",  "tx_dropped",
1400                 "rx_bytes",       "rx_missed_errors",  "tx_errors",
1401                 "rx_compressed",  "rx_over_errors",    "tx_fifo_errors",
1402                 "rx_crc_errors",  "rx_packets",        "tx_heartbeat_errors",
1403                 "rx_dropped",     "tx_aborted_errors", "tx_packets",
1404                 "rx_errors",      "tx_bytes",          "tx_window_errors",
1405                 "rx_fifo_errors", "tx_carrier_errors",
1406         };
1407         char buf[64];
1408         int stats_dir;
1409         int i;
1410         uint64_t val = 0;
1411
1412         snprintf(buf, sizeof(buf), "/sys/class/net/%s/statistics", dev->ifname);
1413         stats_dir = open(buf, O_DIRECTORY);
1414         if (stats_dir < 0)
1415                 return -1;
1416
1417         for (i = 0; i < ARRAY_SIZE(counters); i++)
1418                 if (read_uint64_file(stats_dir, counters[i], &val))
1419                         blobmsg_add_u64(b, counters[i], val);
1420
1421         close(stats_dir);
1422         return 0;
1423 }
1424
1425 static int system_addr(struct device *dev, struct device_addr *addr, int cmd)
1426 {
1427         bool v4 = ((addr->flags & DEVADDR_FAMILY) == DEVADDR_INET4);
1428         int alen = v4 ? 4 : 16;
1429         unsigned int flags = 0;
1430         struct ifaddrmsg ifa = {
1431                 .ifa_family = (alen == 4) ? AF_INET : AF_INET6,
1432                 .ifa_prefixlen = addr->mask,
1433                 .ifa_index = dev->ifindex,
1434                 .ifa_flags = (addr->flags & DEVADDR_OFFLINK) ? IFA_F_NOPREFIXROUTE : 0,
1435         };
1436
1437         struct nl_msg *msg;
1438         if (cmd == RTM_NEWADDR)
1439                 flags |= NLM_F_CREATE | NLM_F_REPLACE;
1440
1441         msg = nlmsg_alloc_simple(cmd, flags);
1442         if (!msg)
1443                 return -1;
1444
1445         nlmsg_append(msg, &ifa, sizeof(ifa), 0);
1446         nla_put(msg, IFA_LOCAL, alen, &addr->addr);
1447         if (v4) {
1448                 if (addr->broadcast)
1449                         nla_put_u32(msg, IFA_BROADCAST, addr->broadcast);
1450                 if (addr->point_to_point)
1451                         nla_put_u32(msg, IFA_ADDRESS, addr->point_to_point);
1452         } else {
1453                 time_t now = system_get_rtime();
1454                 struct ifa_cacheinfo cinfo = {0xffffffffU, 0xffffffffU, 0, 0};
1455
1456                 if (addr->preferred_until) {
1457                         int64_t preferred = addr->preferred_until - now;
1458                         if (preferred < 0)
1459                                 preferred = 0;
1460                         else if (preferred > UINT32_MAX)
1461                                 preferred = UINT32_MAX;
1462
1463                         cinfo.ifa_prefered = preferred;
1464                 }
1465
1466                 if (addr->valid_until) {
1467                         int64_t valid = addr->valid_until - now;
1468                         if (valid <= 0)
1469                                 return -1;
1470                         else if (valid > UINT32_MAX)
1471                                 valid = UINT32_MAX;
1472
1473                         cinfo.ifa_valid = valid;
1474                 }
1475
1476                 nla_put(msg, IFA_CACHEINFO, sizeof(cinfo), &cinfo);
1477         }
1478
1479         return system_rtnl_call(msg);
1480 }
1481
1482 int system_add_address(struct device *dev, struct device_addr *addr)
1483 {
1484         return system_addr(dev, addr, RTM_NEWADDR);
1485 }
1486
1487 int system_del_address(struct device *dev, struct device_addr *addr)
1488 {
1489         return system_addr(dev, addr, RTM_DELADDR);
1490 }
1491
1492 static int system_rt(struct device *dev, struct device_route *route, int cmd)
1493 {
1494         int alen = ((route->flags & DEVADDR_FAMILY) == DEVADDR_INET4) ? 4 : 16;
1495         bool have_gw;
1496         unsigned int flags = 0;
1497
1498         if (alen == 4)
1499                 have_gw = !!route->nexthop.in.s_addr;
1500         else
1501                 have_gw = route->nexthop.in6.s6_addr32[0] ||
1502                         route->nexthop.in6.s6_addr32[1] ||
1503                         route->nexthop.in6.s6_addr32[2] ||
1504                         route->nexthop.in6.s6_addr32[3];
1505
1506         unsigned int table = (route->flags & (DEVROUTE_TABLE | DEVROUTE_SRCTABLE))
1507                         ? route->table : RT_TABLE_MAIN;
1508
1509         struct rtmsg rtm = {
1510                 .rtm_family = (alen == 4) ? AF_INET : AF_INET6,
1511                 .rtm_dst_len = route->mask,
1512                 .rtm_src_len = route->sourcemask,
1513                 .rtm_table = (table < 256) ? table : RT_TABLE_UNSPEC,
1514                 .rtm_protocol = (route->flags & DEVADDR_KERNEL) ? RTPROT_KERNEL : RTPROT_STATIC,
1515                 .rtm_scope = RT_SCOPE_NOWHERE,
1516                 .rtm_type = (cmd == RTM_DELROUTE) ? 0: RTN_UNICAST,
1517                 .rtm_flags = (route->flags & DEVROUTE_ONLINK) ? RTNH_F_ONLINK : 0,
1518         };
1519         struct nl_msg *msg;
1520
1521         if (cmd == RTM_NEWROUTE) {
1522                 flags |= NLM_F_CREATE | NLM_F_REPLACE;
1523
1524                 if (!dev) { // Add null-route
1525                         rtm.rtm_scope = RT_SCOPE_UNIVERSE;
1526                         rtm.rtm_type = RTN_UNREACHABLE;
1527                 }
1528                 else
1529                         rtm.rtm_scope = (have_gw) ? RT_SCOPE_UNIVERSE : RT_SCOPE_LINK;
1530         }
1531
1532         if (route->flags & DEVROUTE_TYPE) {
1533                 rtm.rtm_type = route->type;
1534                 if (!(route->flags & (DEVROUTE_TABLE | DEVROUTE_SRCTABLE))) {
1535                         if (rtm.rtm_type == RTN_LOCAL || rtm.rtm_type == RTN_BROADCAST ||
1536                             rtm.rtm_type == RTN_NAT || rtm.rtm_type == RTN_ANYCAST)
1537                                 rtm.rtm_table = RT_TABLE_LOCAL;
1538                 }
1539
1540                 if (rtm.rtm_type == RTN_LOCAL || rtm.rtm_type == RTN_NAT) {
1541                         rtm.rtm_scope = RT_SCOPE_HOST;
1542                 } else if (rtm.rtm_type == RTN_BROADCAST || rtm.rtm_type == RTN_MULTICAST ||
1543                                 rtm.rtm_type == RTN_ANYCAST) {
1544                         rtm.rtm_scope = RT_SCOPE_LINK;
1545                 } else if (rtm.rtm_type == RTN_BLACKHOLE || rtm.rtm_type == RTN_UNREACHABLE ||
1546                                 rtm.rtm_type == RTN_PROHIBIT || rtm.rtm_type == RTN_FAILED_POLICY) {
1547                         rtm.rtm_scope = RT_SCOPE_UNIVERSE;
1548                         dev = NULL;
1549                 }
1550         }
1551
1552         msg = nlmsg_alloc_simple(cmd, flags);
1553         if (!msg)
1554                 return -1;
1555
1556         nlmsg_append(msg, &rtm, sizeof(rtm), 0);
1557
1558         if (route->mask)
1559                 nla_put(msg, RTA_DST, alen, &route->addr);
1560
1561         if (route->sourcemask) {
1562                 if (rtm.rtm_family == AF_INET)
1563                         nla_put(msg, RTA_PREFSRC, alen, &route->source);
1564                 else
1565                         nla_put(msg, RTA_SRC, alen, &route->source);
1566         }
1567
1568         if (route->metric > 0)
1569                 nla_put_u32(msg, RTA_PRIORITY, route->metric);
1570
1571         if (have_gw)
1572                 nla_put(msg, RTA_GATEWAY, alen, &route->nexthop);
1573
1574         if (dev)
1575                 nla_put_u32(msg, RTA_OIF, dev->ifindex);
1576
1577         if (table >= 256)
1578                 nla_put_u32(msg, RTA_TABLE, table);
1579
1580         if (route->flags & DEVROUTE_MTU) {
1581                 struct nlattr *metrics;
1582
1583                 if (!(metrics = nla_nest_start(msg, RTA_METRICS)))
1584                         goto nla_put_failure;
1585
1586                 nla_put_u32(msg, RTAX_MTU, route->mtu);
1587
1588                 nla_nest_end(msg, metrics);
1589         }
1590
1591         return system_rtnl_call(msg);
1592
1593 nla_put_failure:
1594         nlmsg_free(msg);
1595         return -ENOMEM;
1596 }
1597
1598 int system_add_route(struct device *dev, struct device_route *route)
1599 {
1600         return system_rt(dev, route, RTM_NEWROUTE);
1601 }
1602
1603 int system_del_route(struct device *dev, struct device_route *route)
1604 {
1605         return system_rt(dev, route, RTM_DELROUTE);
1606 }
1607
1608 int system_flush_routes(void)
1609 {
1610         const char *names[] = {
1611                 "/proc/sys/net/ipv4/route/flush",
1612                 "/proc/sys/net/ipv6/route/flush"
1613         };
1614         int fd, i;
1615
1616         for (i = 0; i < ARRAY_SIZE(names); i++) {
1617                 fd = open(names[i], O_WRONLY);
1618                 if (fd < 0)
1619                         continue;
1620
1621                 if (write(fd, "-1", 2)) {}
1622                 close(fd);
1623         }
1624         return 0;
1625 }
1626
1627 bool system_resolve_rt_type(const char *type, unsigned int *id)
1628 {
1629         return system_rtn_aton(type, id);
1630 }
1631
1632 bool system_resolve_rt_table(const char *name, unsigned int *id)
1633 {
1634         FILE *f;
1635         char *e, buf[128];
1636         unsigned int n, table = RT_TABLE_UNSPEC;
1637
1638         /* first try to parse table as number */
1639         if ((n = strtoul(name, &e, 0)) > 0 && !*e)
1640                 table = n;
1641
1642         /* handle well known aliases */
1643         else if (!strcmp(name, "default"))
1644                 table = RT_TABLE_DEFAULT;
1645         else if (!strcmp(name, "main"))
1646                 table = RT_TABLE_MAIN;
1647         else if (!strcmp(name, "local"))
1648                 table = RT_TABLE_LOCAL;
1649         else if (!strcmp(name, "prelocal"))
1650                 table = RT_TABLE_PRELOCAL;
1651
1652         /* try to look up name in /etc/iproute2/rt_tables */
1653         else if ((f = fopen("/etc/iproute2/rt_tables", "r")) != NULL)
1654         {
1655                 while (fgets(buf, sizeof(buf) - 1, f) != NULL)
1656                 {
1657                         if ((e = strtok(buf, " \t\n")) == NULL || *e == '#')
1658                                 continue;
1659
1660                         n = strtoul(e, NULL, 10);
1661                         e = strtok(NULL, " \t\n");
1662
1663                         if (e && !strcmp(e, name))
1664                         {
1665                                 table = n;
1666                                 break;
1667                         }
1668                 }
1669
1670                 fclose(f);
1671         }
1672
1673         if (table == RT_TABLE_UNSPEC)
1674                 return false;
1675
1676         *id = table;
1677         return true;
1678 }
1679
1680 bool system_is_default_rt_table(unsigned int id)
1681 {
1682         return (id == RT_TABLE_MAIN);
1683 }
1684
1685 bool system_resolve_rpfilter(const char *filter, unsigned int *id)
1686 {
1687         char *e;
1688         unsigned int n;
1689
1690         if (!strcmp(filter, "strict"))
1691                 n = 1;
1692         else if (!strcmp(filter, "loose"))
1693                 n = 2;
1694         else {
1695                 n = strtoul(filter, &e, 0);
1696                 if (*e || e == filter || n > 2)
1697                         return false;
1698         }
1699
1700         *id = n;
1701         return true;
1702 }
1703
1704 static int system_iprule(struct iprule *rule, int cmd)
1705 {
1706         int alen = ((rule->flags & IPRULE_FAMILY) == IPRULE_INET4) ? 4 : 16;
1707
1708         struct nl_msg *msg;
1709         struct rtmsg rtm = {
1710                 .rtm_family = (alen == 4) ? AF_INET : AF_INET6,
1711                 .rtm_protocol = RTPROT_STATIC,
1712                 .rtm_scope = RT_SCOPE_UNIVERSE,
1713                 .rtm_table = RT_TABLE_UNSPEC,
1714                 .rtm_type = RTN_UNSPEC,
1715                 .rtm_flags = 0,
1716         };
1717
1718         if (cmd == RTM_NEWRULE) {
1719                 rtm.rtm_type = RTN_UNICAST;
1720                 rtm.rtm_flags |= NLM_F_REPLACE | NLM_F_EXCL;
1721         }
1722
1723         if (rule->invert)
1724                 rtm.rtm_flags |= FIB_RULE_INVERT;
1725
1726         if (rule->flags & IPRULE_SRC)
1727                 rtm.rtm_src_len = rule->src_mask;
1728
1729         if (rule->flags & IPRULE_DEST)
1730                 rtm.rtm_dst_len = rule->dest_mask;
1731
1732         if (rule->flags & IPRULE_TOS)
1733                 rtm.rtm_tos = rule->tos;
1734
1735         if (rule->flags & IPRULE_LOOKUP) {
1736                 if (rule->lookup < 256)
1737                         rtm.rtm_table = rule->lookup;
1738         }
1739
1740         if (rule->flags & IPRULE_ACTION)
1741                 rtm.rtm_type = rule->action;
1742         else if (rule->flags & IPRULE_GOTO)
1743                 rtm.rtm_type = FR_ACT_GOTO;
1744         else if (!(rule->flags & (IPRULE_LOOKUP | IPRULE_ACTION | IPRULE_GOTO)))
1745                 rtm.rtm_type = FR_ACT_NOP;
1746
1747         msg = nlmsg_alloc_simple(cmd, NLM_F_REQUEST);
1748
1749         if (!msg)
1750                 return -1;
1751
1752         nlmsg_append(msg, &rtm, sizeof(rtm), 0);
1753
1754         if (rule->flags & IPRULE_IN)
1755                 nla_put(msg, FRA_IFNAME, strlen(rule->in_dev) + 1, rule->in_dev);
1756
1757         if (rule->flags & IPRULE_OUT)
1758                 nla_put(msg, FRA_OIFNAME, strlen(rule->out_dev) + 1, rule->out_dev);
1759
1760         if (rule->flags & IPRULE_SRC)
1761                 nla_put(msg, FRA_SRC, alen, &rule->src_addr);
1762
1763         if (rule->flags & IPRULE_DEST)
1764                 nla_put(msg, FRA_DST, alen, &rule->dest_addr);
1765
1766         if (rule->flags & IPRULE_PRIORITY)
1767                 nla_put_u32(msg, FRA_PRIORITY, rule->priority);
1768         else if (cmd == RTM_NEWRULE)
1769                 nla_put_u32(msg, FRA_PRIORITY, rule->order);
1770
1771         if (rule->flags & IPRULE_FWMARK)
1772                 nla_put_u32(msg, FRA_FWMARK, rule->fwmark);
1773
1774         if (rule->flags & IPRULE_FWMASK)
1775                 nla_put_u32(msg, FRA_FWMASK, rule->fwmask);
1776
1777         if (rule->flags & IPRULE_LOOKUP) {
1778                 if (rule->lookup >= 256)
1779                         nla_put_u32(msg, FRA_TABLE, rule->lookup);
1780         }
1781
1782         if (rule->flags & IPRULE_GOTO)
1783                 nla_put_u32(msg, FRA_GOTO, rule->gotoid);
1784
1785         return system_rtnl_call(msg);
1786 }
1787
1788 int system_add_iprule(struct iprule *rule)
1789 {
1790         return system_iprule(rule, RTM_NEWRULE);
1791 }
1792
1793 int system_del_iprule(struct iprule *rule)
1794 {
1795         return system_iprule(rule, RTM_DELRULE);
1796 }
1797
1798 int system_flush_iprules(void)
1799 {
1800         int rv = 0;
1801         struct iprule rule;
1802
1803         system_if_clear_entries(NULL, RTM_GETRULE, AF_INET);
1804         system_if_clear_entries(NULL, RTM_GETRULE, AF_INET6);
1805
1806         memset(&rule, 0, sizeof(rule));
1807
1808
1809         rule.flags = IPRULE_INET4 | IPRULE_PRIORITY | IPRULE_LOOKUP;
1810
1811         rule.priority = 0;
1812         rule.lookup = RT_TABLE_PRELOCAL;
1813         rv |= system_iprule(&rule, RTM_NEWRULE);
1814
1815         rule.priority = 1;
1816         rule.lookup = RT_TABLE_LOCAL;
1817         rv |= system_iprule(&rule, RTM_NEWRULE);
1818
1819         rule.priority = 32766;
1820         rule.lookup = RT_TABLE_MAIN;
1821         rv |= system_iprule(&rule, RTM_NEWRULE);
1822
1823         rule.priority = 32767;
1824         rule.lookup = RT_TABLE_DEFAULT;
1825         rv |= system_iprule(&rule, RTM_NEWRULE);
1826
1827
1828         rule.flags = IPRULE_INET6 | IPRULE_PRIORITY | IPRULE_LOOKUP;
1829
1830         rule.priority = 0;
1831         rule.lookup = RT_TABLE_PRELOCAL;
1832         rv |= system_iprule(&rule, RTM_NEWRULE);
1833
1834         rule.priority = 1;
1835         rule.lookup = RT_TABLE_LOCAL;
1836         rv |= system_iprule(&rule, RTM_NEWRULE);
1837
1838         rule.priority = 32766;
1839         rule.lookup = RT_TABLE_MAIN;
1840         rv |= system_iprule(&rule, RTM_NEWRULE);
1841
1842         return rv;
1843 }
1844
1845 bool system_resolve_iprule_action(const char *action, unsigned int *id)
1846 {
1847         return system_rtn_aton(action, id);
1848 }
1849
1850 time_t system_get_rtime(void)
1851 {
1852         struct timespec ts;
1853         struct timeval tv;
1854
1855         if (syscall(__NR_clock_gettime, CLOCK_MONOTONIC, &ts) == 0)
1856                 return ts.tv_sec;
1857
1858         if (gettimeofday(&tv, NULL) == 0)
1859                 return tv.tv_sec;
1860
1861         return 0;
1862 }
1863
1864 #ifndef IP_DF
1865 #define IP_DF       0x4000
1866 #endif
1867
1868 static int tunnel_ioctl(const char *name, int cmd, void *p)
1869 {
1870         struct ifreq ifr;
1871
1872         memset(&ifr, 0, sizeof(ifr));
1873         strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
1874         ifr.ifr_ifru.ifru_data = p;
1875         return ioctl(sock_ioctl, cmd, &ifr);
1876 }
1877
1878 #ifdef IFLA_IPTUN_MAX
1879 #define IP6_FLOWINFO_TCLASS     htonl(0x0FF00000)
1880 static int system_add_gre_tunnel(const char *name, const char *kind,
1881                                  const unsigned int link, struct blob_attr **tb, bool v6)
1882 {
1883         struct nl_msg *nlm;
1884         struct ifinfomsg ifi = { .ifi_family = AF_UNSPEC, };
1885         struct blob_attr *cur;
1886         uint32_t ikey = 0, okey = 0, flags = 0, flowinfo = 0;
1887         uint16_t iflags = 0, oflags = 0;
1888         uint8_t tos = 0;
1889         int ret = 0, ttl = 64;
1890
1891         nlm = nlmsg_alloc_simple(RTM_NEWLINK, NLM_F_REQUEST | NLM_F_REPLACE | NLM_F_CREATE);
1892         if (!nlm)
1893                 return -1;
1894
1895         nlmsg_append(nlm, &ifi, sizeof(ifi), 0);
1896         nla_put_string(nlm, IFLA_IFNAME, name);
1897
1898         struct nlattr *linkinfo = nla_nest_start(nlm, IFLA_LINKINFO);
1899         if (!linkinfo) {
1900                 ret = -ENOMEM;
1901                 goto failure;
1902         }
1903
1904         nla_put_string(nlm, IFLA_INFO_KIND, kind);
1905         struct nlattr *infodata = nla_nest_start(nlm, IFLA_INFO_DATA);
1906         if (!infodata) {
1907                 ret = -ENOMEM;
1908                 goto failure;
1909         }
1910
1911         if (link)
1912                 nla_put_u32(nlm, IFLA_GRE_LINK, link);
1913
1914         if ((cur = tb[TUNNEL_ATTR_TTL]))
1915                 ttl = blobmsg_get_u32(cur);
1916
1917         nla_put_u8(nlm, IFLA_GRE_TTL, ttl);
1918
1919         if ((cur = tb[TUNNEL_ATTR_TOS])) {
1920                 char *str = blobmsg_get_string(cur);
1921                 if (strcmp(str, "inherit")) {
1922                         unsigned uval;
1923
1924                         if (!system_tos_aton(str, &uval)) {
1925                                 ret = -EINVAL;
1926                                 goto failure;
1927                         }
1928
1929                         if (v6)
1930                                 flowinfo |= htonl(uval << 20) & IP6_FLOWINFO_TCLASS;
1931                         else
1932                                 tos = uval;
1933                 } else {
1934                         if (v6)
1935                                 flags |= IP6_TNL_F_USE_ORIG_TCLASS;
1936                         else
1937                                 tos = 1;
1938                 }
1939         }
1940
1941         if ((cur = tb[TUNNEL_ATTR_INFO]) && (blobmsg_type(cur) == BLOBMSG_TYPE_STRING)) {
1942                 uint8_t icsum, ocsum, iseqno, oseqno;
1943                 if (sscanf(blobmsg_get_string(cur), "%u,%u,%hhu,%hhu,%hhu,%hhu",
1944                         &ikey, &okey, &icsum, &ocsum, &iseqno, &oseqno) < 6) {
1945                         ret = -EINVAL;
1946                         goto failure;
1947                 }
1948
1949                 if (ikey)
1950                         iflags |= GRE_KEY;
1951
1952                 if (okey)
1953                         oflags |= GRE_KEY;
1954
1955                 if (icsum)
1956                         iflags |= GRE_CSUM;
1957
1958                 if (ocsum)
1959                         oflags |= GRE_CSUM;
1960
1961                 if (iseqno)
1962                         iflags |= GRE_SEQ;
1963
1964                 if (oseqno)
1965                         oflags |= GRE_SEQ;
1966         }
1967
1968         if (v6) {
1969                 struct in6_addr in6buf;
1970                 if ((cur = tb[TUNNEL_ATTR_LOCAL])) {
1971                         if (inet_pton(AF_INET6, blobmsg_data(cur), &in6buf) < 1) {
1972                                 ret = -EINVAL;
1973                                 goto failure;
1974                         }
1975                         nla_put(nlm, IFLA_GRE_LOCAL, sizeof(in6buf), &in6buf);
1976                 }
1977
1978                 if ((cur = tb[TUNNEL_ATTR_REMOTE])) {
1979                         if (inet_pton(AF_INET6, blobmsg_data(cur), &in6buf) < 1) {
1980                                 ret = -EINVAL;
1981                                 goto failure;
1982                         }
1983                         nla_put(nlm, IFLA_GRE_REMOTE, sizeof(in6buf), &in6buf);
1984                 }
1985                 nla_put_u8(nlm, IFLA_GRE_ENCAP_LIMIT, 4);
1986
1987                 if (flowinfo)
1988                         nla_put_u32(nlm, IFLA_GRE_FLOWINFO, flowinfo);
1989
1990                 if (flags)
1991                         nla_put_u32(nlm, IFLA_GRE_FLAGS, flags);
1992         } else {
1993                 struct in_addr inbuf;
1994                 bool set_df = true;
1995
1996                 if ((cur = tb[TUNNEL_ATTR_LOCAL])) {
1997                         if (inet_pton(AF_INET, blobmsg_data(cur), &inbuf) < 1) {
1998                                 ret = -EINVAL;
1999                                 goto failure;
2000                         }
2001                         nla_put(nlm, IFLA_GRE_LOCAL, sizeof(inbuf), &inbuf);
2002                 }
2003
2004                 if ((cur = tb[TUNNEL_ATTR_REMOTE])) {
2005                         if (inet_pton(AF_INET, blobmsg_data(cur), &inbuf) < 1) {
2006                                 ret = -EINVAL;
2007                                 goto failure;
2008                         }
2009                         nla_put(nlm, IFLA_GRE_REMOTE, sizeof(inbuf), &inbuf);
2010
2011                         if (IN_MULTICAST(ntohl(inbuf.s_addr))) {
2012                                 if (!okey) {
2013                                         okey = inbuf.s_addr;
2014                                         oflags |= GRE_KEY;
2015                                 }
2016
2017                                 if (!ikey) {
2018                                         ikey = inbuf.s_addr;
2019                                         iflags |= GRE_KEY;
2020                                 }
2021                         }
2022                 }
2023
2024                 if ((cur = tb[TUNNEL_ATTR_DF]))
2025                         set_df = blobmsg_get_bool(cur);
2026
2027                 /* ttl !=0 and nopmtudisc are incompatible */
2028                 if (ttl && !set_df) {
2029                         ret = -EINVAL;
2030                         goto failure;
2031                 }
2032
2033                 nla_put_u8(nlm, IFLA_GRE_PMTUDISC, set_df ? 1 : 0);
2034
2035                 nla_put_u8(nlm, IFLA_GRE_TOS, tos);
2036         }
2037
2038         if (oflags)
2039                 nla_put_u16(nlm, IFLA_GRE_OFLAGS, oflags);
2040
2041         if (iflags)
2042                 nla_put_u16(nlm, IFLA_GRE_IFLAGS, iflags);
2043
2044         if (okey)
2045                 nla_put_u32(nlm, IFLA_GRE_OKEY, okey);
2046
2047         if (ikey)
2048                 nla_put_u32(nlm, IFLA_GRE_IKEY, ikey);
2049
2050         nla_nest_end(nlm, infodata);
2051         nla_nest_end(nlm, linkinfo);
2052
2053         return system_rtnl_call(nlm);
2054
2055 failure:
2056         nlmsg_free(nlm);
2057         return ret;
2058 }
2059 #endif
2060
2061 static int system_add_proto_tunnel(const char *name, const uint8_t proto, const unsigned int link, struct blob_attr **tb)
2062 {
2063         struct blob_attr *cur;
2064         bool set_df = true;
2065         struct ip_tunnel_parm p  = {
2066                 .link = link,
2067                 .iph = {
2068                         .version = 4,
2069                         .ihl = 5,
2070                         .protocol = proto,
2071                 }
2072         };
2073
2074         if ((cur = tb[TUNNEL_ATTR_LOCAL]) &&
2075                         inet_pton(AF_INET, blobmsg_data(cur), &p.iph.saddr) < 1)
2076                 return -EINVAL;
2077
2078         if ((cur = tb[TUNNEL_ATTR_REMOTE]) &&
2079                         inet_pton(AF_INET, blobmsg_data(cur), &p.iph.daddr) < 1)
2080                 return -EINVAL;
2081
2082         if ((cur = tb[TUNNEL_ATTR_DF]))
2083                 set_df = blobmsg_get_bool(cur);
2084
2085         if ((cur = tb[TUNNEL_ATTR_TTL]))
2086                 p.iph.ttl = blobmsg_get_u32(cur);
2087
2088         if ((cur = tb[TUNNEL_ATTR_TOS])) {
2089                 char *str = blobmsg_get_string(cur);
2090                 if (strcmp(str, "inherit")) {
2091                         unsigned uval;
2092
2093                         if (!system_tos_aton(str, &uval))
2094                                 return -EINVAL;
2095
2096                         p.iph.tos = uval;
2097                 } else
2098                         p.iph.tos = 1;
2099         }
2100
2101         p.iph.frag_off = set_df ? htons(IP_DF) : 0;
2102         /* ttl !=0 and nopmtudisc are incompatible */
2103         if (p.iph.ttl && p.iph.frag_off == 0)
2104                 return -EINVAL;
2105
2106         strncpy(p.name, name, sizeof(p.name));
2107
2108         switch (p.iph.protocol) {
2109         case IPPROTO_IPIP:
2110                 return tunnel_ioctl("tunl0", SIOCADDTUNNEL, &p);
2111         case IPPROTO_IPV6:
2112                 return tunnel_ioctl("sit0", SIOCADDTUNNEL, &p);
2113         default:
2114                 break;
2115         }
2116         return -1;
2117 }
2118
2119 static int __system_del_ip_tunnel(const char *name, struct blob_attr **tb)
2120 {
2121         struct blob_attr *cur;
2122         const char *str;
2123
2124         if (!(cur = tb[TUNNEL_ATTR_TYPE]))
2125                 return -EINVAL;
2126         str = blobmsg_data(cur);
2127
2128         if (!strcmp(str, "greip") || !strcmp(str, "gretapip") ||
2129             !strcmp(str, "greip6") || !strcmp(str, "gretapip6"))
2130                 return system_link_del(name);
2131         else
2132                 return tunnel_ioctl(name, SIOCDELTUNNEL, NULL);
2133 }
2134
2135 int system_del_ip_tunnel(const char *name, struct blob_attr *attr)
2136 {
2137         struct blob_attr *tb[__TUNNEL_ATTR_MAX];
2138
2139         blobmsg_parse(tunnel_attr_list.params, __TUNNEL_ATTR_MAX, tb,
2140                 blob_data(attr), blob_len(attr));
2141
2142         return __system_del_ip_tunnel(name, tb);
2143 }
2144
2145 int system_update_ipv6_mtu(struct device *dev, int mtu)
2146 {
2147         int ret = -1;
2148         char buf[64];
2149         snprintf(buf, sizeof(buf), "/proc/sys/net/ipv6/conf/%s/mtu",
2150                         dev->ifname);
2151
2152         int fd = open(buf, O_RDWR);
2153         ssize_t len = read(fd, buf, sizeof(buf) - 1);
2154         if (len < 0)
2155                 goto out;
2156
2157         buf[len] = 0;
2158         ret = atoi(buf);
2159
2160         if (!mtu || ret <= mtu)
2161                 goto out;
2162
2163         lseek(fd, 0, SEEK_SET);
2164         if (write(fd, buf, snprintf(buf, sizeof(buf), "%i", mtu)) <= 0)
2165                 ret = -1;
2166
2167 out:
2168         close(fd);
2169         return ret;
2170 }
2171
2172 int system_add_ip_tunnel(const char *name, struct blob_attr *attr)
2173 {
2174         struct blob_attr *tb[__TUNNEL_ATTR_MAX];
2175         struct blob_attr *cur;
2176         const char *str;
2177
2178         blobmsg_parse(tunnel_attr_list.params, __TUNNEL_ATTR_MAX, tb,
2179                 blob_data(attr), blob_len(attr));
2180
2181         __system_del_ip_tunnel(name, tb);
2182
2183         if (!(cur = tb[TUNNEL_ATTR_TYPE]))
2184                 return -EINVAL;
2185         str = blobmsg_data(cur);
2186
2187         unsigned int ttl = 0;
2188         if ((cur = tb[TUNNEL_ATTR_TTL])) {
2189                 ttl = blobmsg_get_u32(cur);
2190                 if (ttl > 255)
2191                         return -EINVAL;
2192         }
2193
2194         unsigned int link = 0;
2195         if ((cur = tb[TUNNEL_ATTR_LINK])) {
2196                 struct interface *iface = vlist_find(&interfaces, blobmsg_data(cur), iface, node);
2197                 if (!iface)
2198                         return -EINVAL;
2199
2200                 if (iface->l3_dev.dev)
2201                         link = iface->l3_dev.dev->ifindex;
2202         }
2203
2204         if (!strcmp(str, "sit")) {
2205                 if (system_add_proto_tunnel(name, IPPROTO_IPV6, link, tb) < 0)
2206                         return -1;
2207
2208 #ifdef SIOCADD6RD
2209                 if ((cur = tb[TUNNEL_ATTR_6RD_PREFIX])) {
2210                         unsigned int mask;
2211                         struct ip_tunnel_6rd p6;
2212
2213                         memset(&p6, 0, sizeof(p6));
2214
2215                         if (!parse_ip_and_netmask(AF_INET6, blobmsg_data(cur),
2216                                                 &p6.prefix, &mask) || mask > 128)
2217                                 return -EINVAL;
2218                         p6.prefixlen = mask;
2219
2220                         if ((cur = tb[TUNNEL_ATTR_6RD_RELAY_PREFIX])) {
2221                                 if (!parse_ip_and_netmask(AF_INET, blobmsg_data(cur),
2222                                                         &p6.relay_prefix, &mask) || mask > 32)
2223                                         return -EINVAL;
2224                                 p6.relay_prefixlen = mask;
2225                         }
2226
2227                         if (tunnel_ioctl(name, SIOCADD6RD, &p6) < 0) {
2228                                 __system_del_ip_tunnel(name, tb);
2229                                 return -1;
2230                         }
2231                 }
2232 #endif
2233 #ifdef IFLA_IPTUN_MAX
2234         } else if (!strcmp(str, "ipip6")) {
2235                 struct nl_msg *nlm = nlmsg_alloc_simple(RTM_NEWLINK,
2236                                 NLM_F_REQUEST | NLM_F_REPLACE | NLM_F_CREATE);
2237                 struct ifinfomsg ifi = { .ifi_family = AF_UNSPEC };
2238                 int ret = 0;
2239
2240                 if (!nlm)
2241                         return -1;
2242
2243                 nlmsg_append(nlm, &ifi, sizeof(ifi), 0);
2244                 nla_put_string(nlm, IFLA_IFNAME, name);
2245
2246                 if (link)
2247                         nla_put_u32(nlm, IFLA_LINK, link);
2248
2249                 struct nlattr *linkinfo = nla_nest_start(nlm, IFLA_LINKINFO);
2250                 if (!linkinfo) {
2251                         ret = -ENOMEM;
2252                         goto failure;
2253                 }
2254                 nla_put_string(nlm, IFLA_INFO_KIND, "ip6tnl");
2255                 struct nlattr *infodata = nla_nest_start(nlm, IFLA_INFO_DATA);
2256                 if (!infodata) {
2257                         ret = -ENOMEM;
2258                         goto failure;
2259                 }
2260
2261                 if (link)
2262                         nla_put_u32(nlm, IFLA_IPTUN_LINK, link);
2263
2264                 nla_put_u8(nlm, IFLA_IPTUN_PROTO, IPPROTO_IPIP);
2265                 nla_put_u8(nlm, IFLA_IPTUN_TTL, (ttl) ? ttl : 64);
2266                 nla_put_u8(nlm, IFLA_IPTUN_ENCAP_LIMIT, 4);
2267
2268                 struct in6_addr in6buf;
2269                 if ((cur = tb[TUNNEL_ATTR_LOCAL])) {
2270                         if (inet_pton(AF_INET6, blobmsg_data(cur), &in6buf) < 1) {
2271                                 ret = -EINVAL;
2272                                 goto failure;
2273                         }
2274                         nla_put(nlm, IFLA_IPTUN_LOCAL, sizeof(in6buf), &in6buf);
2275                 }
2276
2277                 if ((cur = tb[TUNNEL_ATTR_REMOTE])) {
2278                         if (inet_pton(AF_INET6, blobmsg_data(cur), &in6buf) < 1) {
2279                                 ret = -EINVAL;
2280                                 goto failure;
2281                         }
2282                         nla_put(nlm, IFLA_IPTUN_REMOTE, sizeof(in6buf), &in6buf);
2283                 }
2284
2285 #ifdef IFLA_IPTUN_FMR_MAX
2286                 if ((cur = tb[TUNNEL_ATTR_FMRS])) {
2287                         struct nlattr *fmrs = nla_nest_start(nlm, IFLA_IPTUN_FMRS);
2288
2289                         struct blob_attr *fmr;
2290                         unsigned rem, fmrcnt = 0;
2291                         blobmsg_for_each_attr(fmr, cur, rem) {
2292                                 if (blobmsg_type(fmr) != BLOBMSG_TYPE_STRING)
2293                                         continue;
2294
2295                                 unsigned ip4len, ip6len, ealen, offset = 6;
2296                                 char ip6buf[48];
2297                                 char ip4buf[16];
2298
2299                                 if (sscanf(blobmsg_get_string(fmr), "%47[^/]/%u,%15[^/]/%u,%u,%u",
2300                                                 ip6buf, &ip6len, ip4buf, &ip4len, &ealen, &offset) < 5) {
2301                                         ret = -EINVAL;
2302                                         goto failure;
2303                                 }
2304
2305                                 struct in6_addr ip6prefix;
2306                                 struct in_addr ip4prefix;
2307                                 if (inet_pton(AF_INET6, ip6buf, &ip6prefix) != 1 ||
2308                                                 inet_pton(AF_INET, ip4buf, &ip4prefix) != 1) {
2309                                         ret = -EINVAL;
2310                                         goto failure;
2311                                 }
2312
2313                                 struct nlattr *rule = nla_nest_start(nlm, ++fmrcnt);
2314
2315                                 nla_put(nlm, IFLA_IPTUN_FMR_IP6_PREFIX, sizeof(ip6prefix), &ip6prefix);
2316                                 nla_put(nlm, IFLA_IPTUN_FMR_IP4_PREFIX, sizeof(ip4prefix), &ip4prefix);
2317                                 nla_put_u8(nlm, IFLA_IPTUN_FMR_IP6_PREFIX_LEN, ip6len);
2318                                 nla_put_u8(nlm, IFLA_IPTUN_FMR_IP4_PREFIX_LEN, ip4len);
2319                                 nla_put_u8(nlm, IFLA_IPTUN_FMR_EA_LEN, ealen);
2320                                 nla_put_u8(nlm, IFLA_IPTUN_FMR_OFFSET, offset);
2321
2322                                 nla_nest_end(nlm, rule);
2323                         }
2324
2325                         nla_nest_end(nlm, fmrs);
2326                 }
2327 #endif
2328
2329                 nla_nest_end(nlm, infodata);
2330                 nla_nest_end(nlm, linkinfo);
2331
2332                 return system_rtnl_call(nlm);
2333 failure:
2334                 nlmsg_free(nlm);
2335                 return ret;
2336         } else if (!strcmp(str, "greip")) {
2337                 return system_add_gre_tunnel(name, "gre", link, tb, false);
2338         } else if (!strcmp(str, "gretapip"))  {
2339                 return system_add_gre_tunnel(name, "gretap", link, tb, false);
2340         } else if (!strcmp(str, "greip6")) {
2341                 return system_add_gre_tunnel(name, "ip6gre", link, tb, true);
2342         } else if (!strcmp(str, "gretapip6")) {
2343                 return system_add_gre_tunnel(name, "ip6gretap", link, tb, true);
2344 #endif
2345         } else if (!strcmp(str, "ipip")) {
2346                 return system_add_proto_tunnel(name, IPPROTO_IPIP, link, tb);
2347         }
2348         else
2349                 return -EINVAL;
2350
2351         return 0;
2352 }