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