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