IPv6: Log a warning if users have prefixes but haven't assigned them
[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  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License version 2
9  * as published by the Free Software Foundation
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  */
16 #define _GNU_SOURCE
17
18 #include <sys/socket.h>
19 #include <sys/ioctl.h>
20 #include <sys/stat.h>
21 #include <sys/syscall.h>
22
23 #include <net/if.h>
24 #include <net/if_arp.h>
25
26 #include <arpa/inet.h>
27 #include <netinet/in.h>
28
29 #include <linux/rtnetlink.h>
30 #include <linux/sockios.h>
31 #include <linux/ip.h>
32 #include <linux/if_vlan.h>
33 #include <linux/if_bridge.h>
34 #include <linux/if_tunnel.h>
35 #include <linux/ip6_tunnel.h>
36 #include <linux/ethtool.h>
37 #include <linux/fib_rules.h>
38
39 #include <unistd.h>
40 #include <string.h>
41 #include <fcntl.h>
42 #include <glob.h>
43 #include <time.h>
44
45 #include <netlink/msg.h>
46 #include <netlink/attr.h>
47 #include <netlink/socket.h>
48 #include <libubox/uloop.h>
49
50 #include "netifd.h"
51 #include "device.h"
52 #include "system.h"
53
54 struct event_socket {
55         struct uloop_fd uloop;
56         struct nl_sock *sock;
57         struct nl_cb *cb;
58 };
59
60 static int sock_ioctl = -1;
61 static struct nl_sock *sock_rtnl = NULL;
62
63 static int cb_rtnl_event(struct nl_msg *msg, void *arg);
64 static void handle_hotplug_event(struct uloop_fd *u, unsigned int events);
65
66 static char dev_buf[256];
67
68 static void
69 handler_nl_event(struct uloop_fd *u, unsigned int events)
70 {
71         struct event_socket *ev = container_of(u, struct event_socket, uloop);
72         nl_recvmsgs(ev->sock, ev->cb);
73 }
74
75 static struct nl_sock *
76 create_socket(int protocol, int groups)
77 {
78         struct nl_sock *sock;
79
80         sock = nl_socket_alloc();
81         if (!sock)
82                 return NULL;
83
84         if (groups)
85                 nl_join_groups(sock, groups);
86
87         if (nl_connect(sock, protocol))
88                 return NULL;
89
90         return sock;
91 }
92
93 static bool
94 create_raw_event_socket(struct event_socket *ev, int protocol, int groups,
95                         uloop_fd_handler cb)
96 {
97         ev->sock = create_socket(protocol, groups);
98         if (!ev->sock)
99                 return false;
100
101         ev->uloop.fd = nl_socket_get_fd(ev->sock);
102         ev->uloop.cb = cb;
103         uloop_fd_add(&ev->uloop, ULOOP_READ | ULOOP_EDGE_TRIGGER);
104         return true;
105 }
106
107 static bool
108 create_event_socket(struct event_socket *ev, int protocol,
109                     int (*cb)(struct nl_msg *msg, void *arg))
110 {
111         // Prepare socket for link events
112         ev->cb = nl_cb_alloc(NL_CB_DEFAULT);
113         if (!ev->cb)
114                 return false;
115
116         nl_cb_set(ev->cb, NL_CB_VALID, NL_CB_CUSTOM, cb, NULL);
117
118         return create_raw_event_socket(ev, protocol, 0, handler_nl_event);
119 }
120
121 int system_init(void)
122 {
123         static struct event_socket rtnl_event;
124         static struct event_socket hotplug_event;
125
126         sock_ioctl = socket(AF_LOCAL, SOCK_DGRAM, 0);
127         fcntl(sock_ioctl, F_SETFD, fcntl(sock_ioctl, F_GETFD) | FD_CLOEXEC);
128
129         // Prepare socket for routing / address control
130         sock_rtnl = create_socket(NETLINK_ROUTE, 0);
131         if (!sock_rtnl)
132                 return -1;
133
134         if (!create_event_socket(&rtnl_event, NETLINK_ROUTE, cb_rtnl_event))
135                 return -1;
136
137         if (!create_raw_event_socket(&hotplug_event, NETLINK_KOBJECT_UEVENT, 1,
138                                      handle_hotplug_event))
139                 return -1;
140
141         // Receive network link events form kernel
142         nl_socket_add_membership(rtnl_event.sock, RTNLGRP_LINK);
143
144         return 0;
145 }
146
147 static void system_set_sysctl(const char *path, const char *val)
148 {
149         int fd;
150
151         fd = open(path, O_WRONLY);
152         if (fd < 0)
153                 return;
154
155         write(fd, val, strlen(val));
156         close(fd);
157 }
158
159 static void system_set_dev_sysctl(const char *path, const char *device, const char *val)
160 {
161         snprintf(dev_buf, sizeof(dev_buf), path, device);
162         system_set_sysctl(dev_buf, val);
163 }
164
165 static void system_set_disable_ipv6(struct device *dev, const char *val)
166 {
167         system_set_dev_sysctl("/proc/sys/net/ipv6/conf/%s/disable_ipv6", dev->ifname, val);
168 }
169
170 // Evaluate netlink messages
171 static int cb_rtnl_event(struct nl_msg *msg, void *arg)
172 {
173         struct nlmsghdr *nh = nlmsg_hdr(msg);
174         struct ifinfomsg *ifi = NLMSG_DATA(nh);
175         struct nlattr *nla[__IFLA_MAX];
176
177         if (nh->nlmsg_type != RTM_DELLINK && nh->nlmsg_type != RTM_NEWLINK)
178                 goto out;
179
180         nlmsg_parse(nh, sizeof(*ifi), nla, __IFLA_MAX - 1, NULL);
181         if (!nla[IFLA_IFNAME])
182                 goto out;
183
184         struct device *dev = device_get(RTA_DATA(nla[IFLA_IFNAME]), false);
185         if (!dev)
186                 goto out;
187
188         dev->ifindex = ifi->ifi_index;
189         /* TODO: parse link status */
190
191 out:
192         return 0;
193 }
194
195 static void
196 handle_hotplug_msg(char *data, int size)
197 {
198         const char *subsystem = NULL, *interface = NULL;
199         char *cur, *end, *sep;
200         struct device *dev;
201         int skip;
202         bool add;
203
204         if (!strncmp(data, "add@", 4))
205                 add = true;
206         else if (!strncmp(data, "remove@", 7))
207                 add = false;
208         else
209                 return;
210
211         skip = strlen(data) + 1;
212         end = data + size;
213
214         for (cur = data + skip; cur < end; cur += skip) {
215                 skip = strlen(cur) + 1;
216
217                 sep = strchr(cur, '=');
218                 if (!sep)
219                         continue;
220
221                 *sep = 0;
222                 if (!strcmp(cur, "INTERFACE"))
223                         interface = sep + 1;
224                 else if (!strcmp(cur, "SUBSYSTEM")) {
225                         subsystem = sep + 1;
226                         if (strcmp(subsystem, "net") != 0)
227                                 return;
228                 }
229                 if (subsystem && interface)
230                         goto found;
231         }
232         return;
233
234 found:
235         dev = device_get(interface, false);
236         if (!dev)
237                 return;
238
239         if (dev->type != &simple_device_type)
240                 return;
241
242         if (add && system_if_force_external(dev->ifname))
243                 return;
244
245         device_set_present(dev, add);
246 }
247
248 static void
249 handle_hotplug_event(struct uloop_fd *u, unsigned int events)
250 {
251         struct event_socket *ev = container_of(u, struct event_socket, uloop);
252         struct sockaddr_nl nla;
253         unsigned char *buf = NULL;
254         int size;
255
256         while ((size = nl_recv(ev->sock, &nla, &buf, NULL)) > 0) {
257                 if (nla.nl_pid == 0)
258                         handle_hotplug_msg((char *) buf, size);
259
260                 free(buf);
261         }
262 }
263
264 static int system_rtnl_call(struct nl_msg *msg)
265 {
266         int ret;
267
268         ret = nl_send_auto_complete(sock_rtnl, msg);
269         nlmsg_free(msg);
270
271         if (ret < 0)
272                 return ret;
273
274         return nl_wait_for_ack(sock_rtnl);
275 }
276
277 int system_bridge_delbr(struct device *bridge)
278 {
279         return ioctl(sock_ioctl, SIOCBRDELBR, bridge->ifname);
280 }
281
282 static int system_bridge_if(const char *bridge, struct device *dev, int cmd, void *data)
283 {
284         struct ifreq ifr;
285
286         memset(&ifr, 0, sizeof(ifr));
287         if (dev)
288                 ifr.ifr_ifindex = dev->ifindex;
289         else
290                 ifr.ifr_data = data;
291         strncpy(ifr.ifr_name, bridge, sizeof(ifr.ifr_name));
292         return ioctl(sock_ioctl, cmd, &ifr);
293 }
294
295 static bool system_is_bridge(const char *name, char *buf, int buflen)
296 {
297         struct stat st;
298
299         snprintf(buf, buflen, "/sys/devices/virtual/net/%s/bridge", name);
300         if (stat(buf, &st) < 0)
301                 return false;
302
303         return true;
304 }
305
306 static char *system_get_bridge(const char *name, char *buf, int buflen)
307 {
308         char *path;
309         ssize_t len;
310         glob_t gl;
311
312         snprintf(buf, buflen, "/sys/devices/virtual/net/*/brif/%s/bridge", name);
313         if (glob(buf, GLOB_NOSORT, NULL, &gl) < 0)
314                 return NULL;
315
316         if (gl.gl_pathc == 0)
317                 return NULL;
318
319         len = readlink(gl.gl_pathv[0], buf, buflen);
320         if (len < 0)
321                 return NULL;
322
323         buf[len] = 0;
324         path = strrchr(buf, '/');
325         if (!path)
326                 return NULL;
327
328         return path + 1;
329 }
330
331 int system_bridge_addif(struct device *bridge, struct device *dev)
332 {
333         char *oldbr;
334
335         system_set_disable_ipv6(dev, "1");
336         oldbr = system_get_bridge(dev->ifname, dev_buf, sizeof(dev_buf));
337         if (oldbr && !strcmp(oldbr, bridge->ifname))
338                 return 0;
339
340         return system_bridge_if(bridge->ifname, dev, SIOCBRADDIF, NULL);
341 }
342
343 int system_bridge_delif(struct device *bridge, struct device *dev)
344 {
345         system_set_disable_ipv6(dev, "0");
346         return system_bridge_if(bridge->ifname, dev, SIOCBRDELIF, NULL);
347 }
348
349 static int system_if_resolve(struct device *dev)
350 {
351         struct ifreq ifr;
352         strncpy(ifr.ifr_name, dev->ifname, sizeof(ifr.ifr_name));
353         if (!ioctl(sock_ioctl, SIOCGIFINDEX, &ifr))
354                 return ifr.ifr_ifindex;
355         else
356                 return 0;
357 }
358
359 static int system_if_flags(const char *ifname, unsigned add, unsigned rem)
360 {
361         struct ifreq ifr;
362
363         memset(&ifr, 0, sizeof(ifr));
364         strncpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
365         ioctl(sock_ioctl, SIOCGIFFLAGS, &ifr);
366         ifr.ifr_flags |= add;
367         ifr.ifr_flags &= ~rem;
368         return ioctl(sock_ioctl, SIOCSIFFLAGS, &ifr);
369 }
370
371 struct clear_data {
372         struct nl_msg *msg;
373         struct device *dev;
374         int type;
375         int size;
376         int af;
377 };
378
379
380 static bool check_ifaddr(struct nlmsghdr *hdr, int ifindex)
381 {
382         struct ifaddrmsg *ifa = NLMSG_DATA(hdr);
383
384         return ifa->ifa_index == ifindex;
385 }
386
387 static bool check_route(struct nlmsghdr *hdr, int ifindex)
388 {
389         struct rtmsg *r = NLMSG_DATA(hdr);
390         struct nlattr *tb[__RTA_MAX];
391
392         if (r->rtm_protocol == RTPROT_KERNEL &&
393             r->rtm_family == AF_INET6)
394                 return false;
395
396         nlmsg_parse(hdr, sizeof(struct rtmsg), tb, __RTA_MAX - 1, NULL);
397         if (!tb[RTA_OIF])
398                 return false;
399
400         return *(int *)RTA_DATA(tb[RTA_OIF]) == ifindex;
401 }
402
403 static bool check_rule(struct nlmsghdr *hdr, int ifindex)
404 {
405         return true;
406 }
407
408 static int cb_clear_event(struct nl_msg *msg, void *arg)
409 {
410         struct clear_data *clr = arg;
411         struct nlmsghdr *hdr = nlmsg_hdr(msg);
412         bool (*cb)(struct nlmsghdr *, int ifindex);
413         int type;
414
415         switch(clr->type) {
416         case RTM_GETADDR:
417                 type = RTM_DELADDR;
418                 if (hdr->nlmsg_type != RTM_NEWADDR)
419                         return NL_SKIP;
420
421                 cb = check_ifaddr;
422                 break;
423         case RTM_GETROUTE:
424                 type = RTM_DELROUTE;
425                 if (hdr->nlmsg_type != RTM_NEWROUTE)
426                         return NL_SKIP;
427
428                 cb = check_route;
429                 break;
430         case RTM_GETRULE:
431                 type = RTM_DELRULE;
432                 if (hdr->nlmsg_type != RTM_NEWRULE)
433                         return NL_SKIP;
434
435                 cb = check_rule;
436                 break;
437         default:
438                 return NL_SKIP;
439         }
440
441         if (!cb(hdr, clr->dev ? clr->dev->ifindex : 0))
442                 return NL_SKIP;
443
444         if (type == RTM_DELRULE)
445                 D(SYSTEM, "Remove a rule\n");
446         else
447                 D(SYSTEM, "Remove %s from device %s\n",
448                   type == RTM_DELADDR ? "an address" : "a route",
449                   clr->dev->ifname);
450         memcpy(nlmsg_hdr(clr->msg), hdr, hdr->nlmsg_len);
451         hdr = nlmsg_hdr(clr->msg);
452         hdr->nlmsg_type = type;
453         hdr->nlmsg_flags = NLM_F_REQUEST;
454
455         if (!nl_send_auto_complete(sock_rtnl, clr->msg))
456                 nl_wait_for_ack(sock_rtnl);
457
458         return NL_SKIP;
459 }
460
461 static int
462 cb_finish_event(struct nl_msg *msg, void *arg)
463 {
464         int *pending = arg;
465         *pending = 0;
466         return NL_STOP;
467 }
468
469 static int
470 error_handler(struct sockaddr_nl *nla, struct nlmsgerr *err, void *arg)
471 {
472         int *pending = arg;
473         *pending = err->error;
474         return NL_STOP;
475 }
476
477 static void
478 system_if_clear_entries(struct device *dev, int type, int af)
479 {
480         struct clear_data clr;
481         struct nl_cb *cb = nl_cb_alloc(NL_CB_DEFAULT);
482         struct rtmsg rtm = {
483                 .rtm_family = af,
484                 .rtm_flags = RTM_F_CLONED,
485         };
486         int flags = NLM_F_DUMP;
487         int pending = 1;
488
489         clr.af = af;
490         clr.dev = dev;
491         clr.type = type;
492         switch (type) {
493         case RTM_GETADDR:
494         case RTM_GETRULE:
495                 clr.size = sizeof(struct rtgenmsg);
496                 break;
497         case RTM_GETROUTE:
498                 clr.size = sizeof(struct rtmsg);
499                 break;
500         default:
501                 return;
502         }
503
504         if (!cb)
505                 return;
506
507         clr.msg = nlmsg_alloc_simple(type, flags);
508         if (!clr.msg)
509                 goto out;
510
511         nlmsg_append(clr.msg, &rtm, clr.size, 0);
512         nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM, cb_clear_event, &clr);
513         nl_cb_set(cb, NL_CB_FINISH, NL_CB_CUSTOM, cb_finish_event, &pending);
514         nl_cb_err(cb, NL_CB_CUSTOM, error_handler, &pending);
515
516         nl_send_auto_complete(sock_rtnl, clr.msg);
517         while (pending > 0)
518                 nl_recvmsgs(sock_rtnl, cb);
519
520         nlmsg_free(clr.msg);
521 out:
522         nl_cb_put(cb);
523 }
524
525 /*
526  * Clear bridge (membership) state and bring down device
527  */
528 void system_if_clear_state(struct device *dev)
529 {
530         static char buf[256];
531         char *bridge;
532
533         if (dev->external)
534                 return;
535
536         dev->ifindex = system_if_resolve(dev);
537         if (!dev->ifindex)
538                 return;
539
540         system_if_flags(dev->ifname, 0, IFF_UP);
541
542         if (system_is_bridge(dev->ifname, buf, sizeof(buf))) {
543                 D(SYSTEM, "Delete existing bridge named '%s'\n", dev->ifname);
544                 system_bridge_delbr(dev);
545                 return;
546         }
547
548         bridge = system_get_bridge(dev->ifname, buf, sizeof(buf));
549         if (bridge) {
550                 D(SYSTEM, "Remove device '%s' from bridge '%s'\n", dev->ifname, bridge);
551                 system_bridge_if(bridge, dev, SIOCBRDELIF, NULL);
552         }
553
554         system_if_clear_entries(dev, RTM_GETROUTE, AF_INET);
555         system_if_clear_entries(dev, RTM_GETADDR, AF_INET);
556         system_if_clear_entries(dev, RTM_GETROUTE, AF_INET6);
557         system_if_clear_entries(dev, RTM_GETADDR, AF_INET6);
558         system_set_disable_ipv6(dev, "0");
559 }
560
561 static inline unsigned long
562 sec_to_jiffies(int val)
563 {
564         return (unsigned long) val * 100;
565 }
566
567 int system_bridge_addbr(struct device *bridge, struct bridge_config *cfg)
568 {
569         unsigned long args[4] = {};
570
571         if (ioctl(sock_ioctl, SIOCBRADDBR, bridge->ifname) < 0)
572                 return -1;
573
574         args[0] = BRCTL_SET_BRIDGE_STP_STATE;
575         args[1] = !!cfg->stp;
576         system_bridge_if(bridge->ifname, NULL, SIOCDEVPRIVATE, &args);
577
578         args[0] = BRCTL_SET_BRIDGE_FORWARD_DELAY;
579         args[1] = sec_to_jiffies(cfg->forward_delay);
580         system_bridge_if(bridge->ifname, NULL, SIOCDEVPRIVATE, &args);
581
582         system_set_dev_sysctl("/sys/devices/virtual/net/%s/bridge/multicast_snooping",
583                 bridge->ifname, cfg->igmp_snoop ? "1" : "0");
584
585         args[0] = BRCTL_SET_BRIDGE_PRIORITY;
586         args[1] = cfg->priority;
587         system_bridge_if(bridge->ifname, NULL, SIOCDEVPRIVATE, &args);
588
589         if (cfg->flags & BRIDGE_OPT_AGEING_TIME) {
590                 args[0] = BRCTL_SET_AGEING_TIME;
591                 args[1] = sec_to_jiffies(cfg->ageing_time);
592                 system_bridge_if(bridge->ifname, NULL, SIOCDEVPRIVATE, &args);
593         }
594
595         if (cfg->flags & BRIDGE_OPT_HELLO_TIME) {
596                 args[0] = BRCTL_SET_BRIDGE_HELLO_TIME;
597                 args[1] = sec_to_jiffies(cfg->hello_time);
598                 system_bridge_if(bridge->ifname, NULL, SIOCDEVPRIVATE, &args);
599         }
600
601         if (cfg->flags & BRIDGE_OPT_MAX_AGE) {
602                 args[0] = BRCTL_SET_BRIDGE_MAX_AGE;
603                 args[1] = sec_to_jiffies(cfg->max_age);
604                 system_bridge_if(bridge->ifname, NULL, SIOCDEVPRIVATE, &args);
605         }
606
607         return 0;
608 }
609
610 static int system_vlan(struct device *dev, int id)
611 {
612         struct vlan_ioctl_args ifr = {
613                 .cmd = SET_VLAN_NAME_TYPE_CMD,
614                 .u.name_type = VLAN_NAME_TYPE_RAW_PLUS_VID_NO_PAD,
615         };
616
617         ioctl(sock_ioctl, SIOCSIFVLAN, &ifr);
618
619         if (id < 0) {
620                 ifr.cmd = DEL_VLAN_CMD;
621                 ifr.u.VID = 0;
622         } else {
623                 ifr.cmd = ADD_VLAN_CMD;
624                 ifr.u.VID = id;
625         }
626         strncpy(ifr.device1, dev->ifname, sizeof(ifr.device1));
627         return ioctl(sock_ioctl, SIOCSIFVLAN, &ifr);
628 }
629
630 int system_vlan_add(struct device *dev, int id)
631 {
632         return system_vlan(dev, id);
633 }
634
635 int system_vlan_del(struct device *dev)
636 {
637         return system_vlan(dev, -1);
638 }
639
640 static void
641 system_if_get_settings(struct device *dev, struct device_settings *s)
642 {
643         struct ifreq ifr;
644
645         memset(&ifr, 0, sizeof(ifr));
646         strncpy(ifr.ifr_name, dev->ifname, sizeof(ifr.ifr_name));
647
648         if (ioctl(sock_ioctl, SIOCGIFMTU, &ifr) == 0) {
649                 s->mtu = ifr.ifr_mtu;
650                 s->flags |= DEV_OPT_MTU;
651         }
652
653         if (ioctl(sock_ioctl, SIOCGIFTXQLEN, &ifr) == 0) {
654                 s->txqueuelen = ifr.ifr_qlen;
655                 s->flags |= DEV_OPT_TXQUEUELEN;
656         }
657
658         if (ioctl(sock_ioctl, SIOCGIFHWADDR, &ifr) == 0) {
659                 memcpy(s->macaddr, &ifr.ifr_hwaddr.sa_data, sizeof(s->macaddr));
660                 s->flags |= DEV_OPT_MACADDR;
661         }
662 }
663
664 void
665 system_if_apply_settings(struct device *dev, struct device_settings *s)
666 {
667         struct ifreq ifr;
668
669         memset(&ifr, 0, sizeof(ifr));
670         strncpy(ifr.ifr_name, dev->ifname, sizeof(ifr.ifr_name));
671         if (s->flags & DEV_OPT_MTU) {
672                 ifr.ifr_mtu = s->mtu;
673                 if (ioctl(sock_ioctl, SIOCSIFMTU, &ifr) < 0)
674                         s->flags &= ~DEV_OPT_MTU;
675         }
676         if (s->flags & DEV_OPT_TXQUEUELEN) {
677                 ifr.ifr_qlen = s->txqueuelen;
678                 if (ioctl(sock_ioctl, SIOCSIFTXQLEN, &ifr) < 0)
679                         s->flags &= ~DEV_OPT_TXQUEUELEN;
680         }
681         if ((s->flags & DEV_OPT_MACADDR) && !dev->external) {
682                 ifr.ifr_hwaddr.sa_family = ARPHRD_ETHER;
683                 memcpy(&ifr.ifr_hwaddr.sa_data, s->macaddr, sizeof(s->macaddr));
684                 if (ioctl(sock_ioctl, SIOCSIFHWADDR, &ifr) < 0)
685                         s->flags &= ~DEV_OPT_MACADDR;
686         }
687 }
688
689 int system_if_up(struct device *dev)
690 {
691         system_if_get_settings(dev, &dev->orig_settings);
692         system_if_apply_settings(dev, &dev->settings);
693         dev->ifindex = system_if_resolve(dev);
694         return system_if_flags(dev->ifname, IFF_UP, 0);
695 }
696
697 int system_if_down(struct device *dev)
698 {
699         int ret = system_if_flags(dev->ifname, 0, IFF_UP);
700         dev->orig_settings.flags &= dev->settings.flags;
701         system_if_apply_settings(dev, &dev->orig_settings);
702         return ret;
703 }
704
705 int system_if_check(struct device *dev)
706 {
707         device_set_present(dev, (system_if_resolve(dev) > 0));
708         return 0;
709 }
710
711 struct device *
712 system_if_get_parent(struct device *dev)
713 {
714         char buf[64], *devname;
715         int ifindex, iflink, len;
716         FILE *f;
717
718         snprintf(buf, sizeof(buf), "/sys/class/net/%s/iflink", dev->ifname);
719         f = fopen(buf, "r");
720         if (!f)
721                 return NULL;
722
723         len = fread(buf, 1, sizeof(buf) - 1, f);
724         fclose(f);
725
726         if (len <= 0)
727                 return NULL;
728
729         buf[len] = 0;
730         iflink = strtoul(buf, NULL, 0);
731         ifindex = system_if_resolve(dev);
732         if (!iflink || iflink == ifindex)
733                 return NULL;
734
735         devname = if_indextoname(iflink, buf);
736         if (!devname)
737                 return NULL;
738
739         return device_get(devname, true);
740 }
741
742 static bool
743 read_string_file(int dir_fd, const char *file, char *buf, int len)
744 {
745         bool ret = false;
746         char *c;
747         int fd;
748
749         fd = openat(dir_fd, file, O_RDONLY);
750         if (fd < 0)
751                 return false;
752
753 retry:
754         len = read(fd, buf, len - 1);
755         if (len < 0) {
756                 if (errno == EINTR)
757                         goto retry;
758         } else if (len > 0) {
759                         buf[len] = 0;
760
761                         c = strchr(buf, '\n');
762                         if (c)
763                                 *c = 0;
764
765                         ret = true;
766         }
767
768         close(fd);
769
770         return ret;
771 }
772
773 static bool
774 read_uint64_file(int dir_fd, const char *file, uint64_t *val)
775 {
776         char buf[64];
777         bool ret = false;
778
779         ret = read_string_file(dir_fd, file, buf, sizeof(buf));
780         if (ret)
781                 *val = strtoull(buf, NULL, 0);
782
783         return ret;
784 }
785
786 /* Assume advertised flags == supported flags */
787 static const struct {
788         uint32_t mask;
789         const char *name;
790 } ethtool_link_modes[] = {
791         { ADVERTISED_10baseT_Half, "10H" },
792         { ADVERTISED_10baseT_Full, "10F" },
793         { ADVERTISED_100baseT_Half, "100H" },
794         { ADVERTISED_100baseT_Full, "100F" },
795         { ADVERTISED_1000baseT_Half, "1000H" },
796         { ADVERTISED_1000baseT_Full, "1000F" },
797 };
798
799 static void system_add_link_modes(struct blob_buf *b, __u32 mask)
800 {
801         int i;
802         for (i = 0; i < ARRAY_SIZE(ethtool_link_modes); i++) {
803                 if (mask & ethtool_link_modes[i].mask)
804                         blobmsg_add_string(b, NULL, ethtool_link_modes[i].name);
805         }
806 }
807
808 bool
809 system_if_force_external(const char *ifname)
810 {
811         char buf[64];
812         struct stat s;
813
814         snprintf(buf, sizeof(buf), "/sys/class/net/%s/phy80211", ifname);
815         return stat(buf, &s) == 0;
816 }
817
818 int
819 system_if_dump_info(struct device *dev, struct blob_buf *b)
820 {
821         struct ethtool_cmd ecmd;
822         struct ifreq ifr;
823         char buf[64], *s;
824         void *c;
825         int dir_fd;
826         uint64_t val = 0;
827
828         snprintf(buf, sizeof(buf), "/sys/class/net/%s", dev->ifname);
829         dir_fd = open(buf, O_DIRECTORY);
830
831         if (read_uint64_file(dir_fd, "carrier", &val))
832                 blobmsg_add_u8(b, "link", !!val);
833
834         memset(&ecmd, 0, sizeof(ecmd));
835         memset(&ifr, 0, sizeof(ifr));
836         strcpy(ifr.ifr_name, dev->ifname);
837         ifr.ifr_data = (caddr_t) &ecmd;
838         ecmd.cmd = ETHTOOL_GSET;
839
840         if (ioctl(sock_ioctl, SIOCETHTOOL, &ifr) == 0) {
841                 c = blobmsg_open_array(b, "link-advertising");
842                 system_add_link_modes(b, ecmd.advertising);
843                 blobmsg_close_array(b, c);
844
845                 c = blobmsg_open_array(b, "link-supported");
846                 system_add_link_modes(b, ecmd.supported);
847                 blobmsg_close_array(b, c);
848
849                 s = blobmsg_alloc_string_buffer(b, "speed", 8);
850                 snprintf(s, 8, "%d%c", ethtool_cmd_speed(&ecmd),
851                         ecmd.duplex == DUPLEX_HALF ? 'H' : 'F');
852                 blobmsg_add_string_buffer(b);
853         }
854
855         close(dir_fd);
856         return 0;
857 }
858
859 int
860 system_if_dump_stats(struct device *dev, struct blob_buf *b)
861 {
862         const char *const counters[] = {
863                 "collisions",     "rx_frame_errors",   "tx_compressed",
864                 "multicast",      "rx_length_errors",  "tx_dropped",
865                 "rx_bytes",       "rx_missed_errors",  "tx_errors",
866                 "rx_compressed",  "rx_over_errors",    "tx_fifo_errors",
867                 "rx_crc_errors",  "rx_packets",        "tx_heartbeat_errors",
868                 "rx_dropped",     "tx_aborted_errors", "tx_packets",
869                 "rx_errors",      "tx_bytes",          "tx_window_errors",
870                 "rx_fifo_errors", "tx_carrier_errors",
871         };
872         char buf[64];
873         int stats_dir;
874         int i;
875         uint64_t val = 0;
876
877         snprintf(buf, sizeof(buf), "/sys/class/net/%s/statistics", dev->ifname);
878         stats_dir = open(buf, O_DIRECTORY);
879         if (stats_dir < 0)
880                 return -1;
881
882         for (i = 0; i < ARRAY_SIZE(counters); i++)
883                 if (read_uint64_file(stats_dir, counters[i], &val))
884                         blobmsg_add_u64(b, counters[i], val);
885
886         close(stats_dir);
887         return 0;
888 }
889
890 static int system_addr(struct device *dev, struct device_addr *addr, int cmd)
891 {
892         bool v4 = ((addr->flags & DEVADDR_FAMILY) == DEVADDR_INET4);
893         int alen = v4 ? 4 : 16;
894         unsigned int flags = 0;
895         struct ifaddrmsg ifa = {
896                 .ifa_family = (alen == 4) ? AF_INET : AF_INET6,
897                 .ifa_prefixlen = addr->mask,
898                 .ifa_index = dev->ifindex,
899         };
900
901         struct nl_msg *msg;
902         if (cmd == RTM_NEWADDR)
903                 flags |= NLM_F_CREATE | NLM_F_REPLACE;
904
905         msg = nlmsg_alloc_simple(cmd, flags);
906         if (!msg)
907                 return -1;
908
909         nlmsg_append(msg, &ifa, sizeof(ifa), 0);
910         nla_put(msg, IFA_LOCAL, alen, &addr->addr);
911         if (v4) {
912                 if (addr->broadcast)
913                         nla_put_u32(msg, IFA_BROADCAST, addr->broadcast);
914                 if (addr->point_to_point)
915                         nla_put_u32(msg, IFA_ADDRESS, addr->point_to_point);
916         } else {
917                 time_t now = system_get_rtime();
918                 struct ifa_cacheinfo cinfo = {0xffffffffU, 0xffffffffU, 0, 0};
919
920                 if (addr->preferred_until) {
921                         int64_t preferred = addr->preferred_until - now;
922                         if (preferred < 0)
923                                 preferred = 0;
924                         else if (preferred > UINT32_MAX)
925                                 preferred = UINT32_MAX;
926
927                         cinfo.ifa_prefered = preferred;
928                 }
929
930                 if (addr->valid_until) {
931                         int64_t valid = addr->valid_until - now;
932                         if (valid <= 0)
933                                 return -1;
934                         else if (valid > UINT32_MAX)
935                                 valid = UINT32_MAX;
936
937                         cinfo.ifa_valid = valid;
938                 }
939
940                 nla_put(msg, IFA_CACHEINFO, sizeof(cinfo), &cinfo);
941         }
942
943         return system_rtnl_call(msg);
944 }
945
946 int system_add_address(struct device *dev, struct device_addr *addr)
947 {
948         return system_addr(dev, addr, RTM_NEWADDR);
949 }
950
951 int system_del_address(struct device *dev, struct device_addr *addr)
952 {
953         return system_addr(dev, addr, RTM_DELADDR);
954 }
955
956 static int system_rt(struct device *dev, struct device_route *route, int cmd)
957 {
958         int alen = ((route->flags & DEVADDR_FAMILY) == DEVADDR_INET4) ? 4 : 16;
959         bool have_gw;
960         unsigned int flags = 0;
961
962         if (alen == 4)
963                 have_gw = !!route->nexthop.in.s_addr;
964         else
965                 have_gw = route->nexthop.in6.s6_addr32[0] ||
966                         route->nexthop.in6.s6_addr32[1] ||
967                         route->nexthop.in6.s6_addr32[2] ||
968                         route->nexthop.in6.s6_addr32[3];
969
970         unsigned char scope = (cmd == RTM_DELROUTE) ? RT_SCOPE_NOWHERE :
971                         (have_gw) ? RT_SCOPE_UNIVERSE : RT_SCOPE_LINK;
972
973         unsigned int table = (route->flags & (DEVROUTE_TABLE | DEVROUTE_SRCTABLE))
974                         ? route->table : RT_TABLE_MAIN;
975
976         struct rtmsg rtm = {
977                 .rtm_family = (alen == 4) ? AF_INET : AF_INET6,
978                 .rtm_dst_len = route->mask,
979                 .rtm_table = (table < 256) ? table : RT_TABLE_UNSPEC,
980                 .rtm_protocol = (route->flags & DEVADDR_KERNEL) ? RTPROT_KERNEL : RTPROT_STATIC,
981                 .rtm_scope = scope,
982                 .rtm_type = (cmd == RTM_DELROUTE) ? 0: RTN_UNICAST,
983         };
984         struct nl_msg *msg;
985
986         if (cmd == RTM_NEWROUTE) {
987                 flags |= NLM_F_CREATE | NLM_F_REPLACE;
988
989                 if (!dev) { // Add null-route
990                         rtm.rtm_scope = RT_SCOPE_UNIVERSE;
991                         rtm.rtm_type = RTN_UNREACHABLE;
992                 }
993         }
994
995         msg = nlmsg_alloc_simple(cmd, flags);
996         if (!msg)
997                 return -1;
998
999         nlmsg_append(msg, &rtm, sizeof(rtm), 0);
1000
1001         if (route->mask)
1002                 nla_put(msg, RTA_DST, alen, &route->addr);
1003
1004         if (route->metric > 0)
1005                 nla_put_u32(msg, RTA_PRIORITY, route->metric);
1006
1007         if (have_gw)
1008                 nla_put(msg, RTA_GATEWAY, alen, &route->nexthop);
1009
1010         if (dev)
1011                 nla_put_u32(msg, RTA_OIF, dev->ifindex);
1012
1013         if (table >= 256)
1014                 nla_put_u32(msg, RTA_TABLE, table);
1015
1016         return system_rtnl_call(msg);
1017 }
1018
1019 int system_add_route(struct device *dev, struct device_route *route)
1020 {
1021         return system_rt(dev, route, RTM_NEWROUTE);
1022 }
1023
1024 int system_del_route(struct device *dev, struct device_route *route)
1025 {
1026         return system_rt(dev, route, RTM_DELROUTE);
1027 }
1028
1029 int system_flush_routes(void)
1030 {
1031         const char *names[] = {
1032                 "/proc/sys/net/ipv4/route/flush",
1033                 "/proc/sys/net/ipv6/route/flush"
1034         };
1035         int fd, i;
1036
1037         for (i = 0; i < ARRAY_SIZE(names); i++) {
1038                 fd = open(names[i], O_WRONLY);
1039                 if (fd < 0)
1040                         continue;
1041
1042                 write(fd, "-1", 2);
1043                 close(fd);
1044         }
1045         return 0;
1046 }
1047
1048 bool system_resolve_rt_table(const char *name, unsigned int *id)
1049 {
1050         FILE *f;
1051         char *e, buf[128];
1052         unsigned int n, table = RT_TABLE_UNSPEC;
1053
1054         /* first try to parse table as number */
1055         if ((n = strtoul(name, &e, 0)) > 0 && !*e)
1056                 table = n;
1057
1058         /* handle well known aliases */
1059         else if (!strcmp(name, "default"))
1060                 table = RT_TABLE_DEFAULT;
1061         else if (!strcmp(name, "main"))
1062                 table = RT_TABLE_MAIN;
1063         else if (!strcmp(name, "local"))
1064                 table = RT_TABLE_LOCAL;
1065
1066         /* try to look up name in /etc/iproute2/rt_tables */
1067         else if ((f = fopen("/etc/iproute2/rt_tables", "r")) != NULL)
1068         {
1069                 while (fgets(buf, sizeof(buf) - 1, f) != NULL)
1070                 {
1071                         if ((e = strtok(buf, " \t\n")) == NULL || *e == '#')
1072                                 continue;
1073
1074                         n = strtoul(e, NULL, 10);
1075                         e = strtok(NULL, " \t\n");
1076
1077                         if (e && !strcmp(e, name))
1078                         {
1079                                 table = n;
1080                                 break;
1081                         }
1082                 }
1083
1084                 fclose(f);
1085         }
1086
1087         if (table == RT_TABLE_UNSPEC)
1088                 return false;
1089
1090         /* do not consider main table special */
1091         if (table == RT_TABLE_MAIN)
1092                 table = RT_TABLE_UNSPEC;
1093
1094         *id = table;
1095         return true;
1096 }
1097
1098 static int system_iprule(struct iprule *rule, int cmd)
1099 {
1100         int alen = ((rule->flags & IPRULE_FAMILY) == IPRULE_INET4) ? 4 : 16;
1101
1102         struct nl_msg *msg;
1103         struct rtmsg rtm = {
1104                 .rtm_family = (alen == 4) ? AF_INET : AF_INET6,
1105                 .rtm_protocol = RTPROT_STATIC,
1106                 .rtm_scope = RT_SCOPE_UNIVERSE,
1107                 .rtm_table = RT_TABLE_UNSPEC,
1108                 .rtm_type = RTN_UNSPEC,
1109                 .rtm_flags = 0,
1110         };
1111
1112         if (cmd == RTM_NEWRULE) {
1113                 rtm.rtm_type = RTN_UNICAST;
1114                 rtm.rtm_flags |= NLM_F_REPLACE | NLM_F_EXCL;
1115         }
1116
1117         if (rule->invert)
1118                 rtm.rtm_flags |= FIB_RULE_INVERT;
1119
1120         if (rule->flags & IPRULE_SRC)
1121                 rtm.rtm_src_len = rule->src_mask;
1122
1123         if (rule->flags & IPRULE_DEST)
1124                 rtm.rtm_dst_len = rule->dest_mask;
1125
1126         if (rule->flags & IPRULE_TOS)
1127                 rtm.rtm_tos = rule->tos;
1128
1129         if (rule->flags & IPRULE_LOOKUP) {
1130                 if (rule->lookup < 256)
1131                         rtm.rtm_table = rule->lookup;
1132         }
1133
1134         if (rule->flags & IPRULE_ACTION)
1135                 rtm.rtm_type = rule->action;
1136         else if (rule->flags & IPRULE_GOTO)
1137                 rtm.rtm_type = FR_ACT_GOTO;
1138         else if (!(rule->flags & (IPRULE_LOOKUP | IPRULE_ACTION | IPRULE_GOTO)))
1139                 rtm.rtm_type = FR_ACT_NOP;
1140
1141         msg = nlmsg_alloc_simple(cmd, NLM_F_REQUEST);
1142
1143         if (!msg)
1144                 return -1;
1145
1146         nlmsg_append(msg, &rtm, sizeof(rtm), 0);
1147
1148         if (rule->flags & IPRULE_IN)
1149                 nla_put(msg, FRA_IFNAME, strlen(rule->in_dev) + 1, rule->in_dev);
1150
1151         if (rule->flags & IPRULE_OUT)
1152                 nla_put(msg, FRA_OIFNAME, strlen(rule->out_dev) + 1, rule->out_dev);
1153
1154         if (rule->flags & IPRULE_SRC)
1155                 nla_put(msg, FRA_SRC, alen, &rule->src_addr);
1156
1157         if (rule->flags & IPRULE_DEST)
1158                 nla_put(msg, FRA_DST, alen, &rule->dest_addr);
1159
1160         if (rule->flags & IPRULE_PRIORITY)
1161                 nla_put_u32(msg, FRA_PRIORITY, rule->priority);
1162         else if (cmd == RTM_NEWRULE)
1163                 nla_put_u32(msg, FRA_PRIORITY, rule->order);
1164
1165         if (rule->flags & IPRULE_FWMARK)
1166                 nla_put_u32(msg, FRA_FWMARK, rule->fwmark);
1167
1168         if (rule->flags & IPRULE_FWMASK)
1169                 nla_put_u32(msg, FRA_FWMASK, rule->fwmask);
1170
1171         if (rule->flags & IPRULE_LOOKUP) {
1172                 if (rule->lookup >= 256)
1173                         nla_put_u32(msg, FRA_TABLE, rule->lookup);
1174         }
1175
1176         if (rule->flags & IPRULE_GOTO)
1177                 nla_put_u32(msg, FRA_GOTO, rule->gotoid);
1178
1179         return system_rtnl_call(msg);
1180 }
1181
1182 int system_add_iprule(struct iprule *rule)
1183 {
1184         return system_iprule(rule, RTM_NEWRULE);
1185 }
1186
1187 int system_del_iprule(struct iprule *rule)
1188 {
1189         return system_iprule(rule, RTM_DELRULE);
1190 }
1191
1192 int system_flush_iprules(void)
1193 {
1194         int rv = 0;
1195         struct iprule rule;
1196
1197         system_if_clear_entries(NULL, RTM_GETRULE, AF_INET);
1198         system_if_clear_entries(NULL, RTM_GETRULE, AF_INET6);
1199
1200         memset(&rule, 0, sizeof(rule));
1201
1202
1203         rule.flags = IPRULE_INET4 | IPRULE_PRIORITY | IPRULE_LOOKUP;
1204
1205         rule.priority = 0;
1206         rule.lookup = RT_TABLE_LOCAL;
1207         rv |= system_iprule(&rule, RTM_NEWRULE);
1208
1209         rule.priority = 32766;
1210         rule.lookup = RT_TABLE_MAIN;
1211         rv |= system_iprule(&rule, RTM_NEWRULE);
1212
1213         rule.priority = 32767;
1214         rule.lookup = RT_TABLE_DEFAULT;
1215         rv |= system_iprule(&rule, RTM_NEWRULE);
1216
1217
1218         rule.flags = IPRULE_INET6 | IPRULE_PRIORITY | IPRULE_LOOKUP;
1219
1220         rule.priority = 0;
1221         rule.lookup = RT_TABLE_LOCAL;
1222         rv |= system_iprule(&rule, RTM_NEWRULE);
1223
1224         rule.priority = 32766;
1225         rule.lookup = RT_TABLE_MAIN;
1226         rv |= system_iprule(&rule, RTM_NEWRULE);
1227
1228         return rv;
1229 }
1230
1231 bool system_resolve_iprule_action(const char *action, unsigned int *id)
1232 {
1233         char *e;
1234         unsigned int n;
1235
1236         if (!strcmp(action, "local"))
1237                 n = RTN_LOCAL;
1238         else if (!strcmp(action, "nat"))
1239                 n = RTN_NAT;
1240         else if (!strcmp(action, "broadcast"))
1241                 n = RTN_BROADCAST;
1242         else if (!strcmp(action, "anycast"))
1243                 n = RTN_ANYCAST;
1244         else if (!strcmp(action, "multicast"))
1245                 n = RTN_MULTICAST;
1246         else if (!strcmp(action, "prohibit"))
1247                 n = RTN_PROHIBIT;
1248         else if (!strcmp(action, "unreachable"))
1249                 n = RTN_UNREACHABLE;
1250         else if (!strcmp(action, "blackhole"))
1251                 n = RTN_BLACKHOLE;
1252         else if (!strcmp(action, "xresolve"))
1253                 n = RTN_XRESOLVE;
1254         else if (!strcmp(action, "unicast"))
1255                 n = RTN_UNICAST;
1256         else if (!strcmp(action, "throw"))
1257                 n = RTN_THROW;
1258         else {
1259                 n = strtoul(action, &e, 0);
1260                 if (!e || *e || e == action || n > 255)
1261                         return false;
1262         }
1263
1264         *id = n;
1265         return true;
1266 }
1267
1268 time_t system_get_rtime(void)
1269 {
1270         struct timespec ts;
1271         struct timeval tv;
1272
1273         if (syscall(__NR_clock_gettime, CLOCK_MONOTONIC, &ts) == 0)
1274                 return ts.tv_sec;
1275
1276         if (gettimeofday(&tv, NULL) == 0)
1277                 return tv.tv_sec;
1278
1279         return 0;
1280 }
1281
1282 #ifndef IP_DF
1283 #define IP_DF       0x4000
1284 #endif
1285
1286 static int tunnel_ioctl(const char *name, int cmd, void *p)
1287 {
1288         struct ifreq ifr;
1289
1290         memset(&ifr, 0, sizeof(ifr));
1291         strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
1292         ifr.ifr_ifru.ifru_data = p;
1293         return ioctl(sock_ioctl, cmd, &ifr);
1294 }
1295
1296 int system_del_ip_tunnel(const char *name)
1297 {
1298         return tunnel_ioctl(name, SIOCDELTUNNEL, NULL);
1299 }
1300
1301 int system_update_ipv6_mtu(struct device *dev, int mtu)
1302 {
1303         int ret = -1;
1304         char buf[64];
1305         snprintf(buf, sizeof(buf), "/proc/sys/net/ipv6/conf/%s/mtu",
1306                         dev->ifname);
1307
1308         int fd = open(buf, O_RDWR);
1309         ssize_t len = read(fd, buf, sizeof(buf) - 1);
1310         if (len < 0)
1311                 goto out;
1312
1313         buf[len] = 0;
1314         ret = atoi(buf);
1315
1316         if (!mtu || ret <= mtu)
1317                 goto out;
1318
1319         lseek(fd, 0, SEEK_SET);
1320         if (write(fd, buf, snprintf(buf, sizeof(buf), "%i", mtu)) <= 0)
1321                 ret = -1;
1322
1323 out:
1324         close(fd);
1325         return ret;
1326 }
1327
1328
1329 int system_add_ip_tunnel(const char *name, struct blob_attr *attr)
1330 {
1331         struct blob_attr *tb[__TUNNEL_ATTR_MAX];
1332         struct blob_attr *cur;
1333         const char *str;
1334
1335         system_del_ip_tunnel(name);
1336
1337         blobmsg_parse(tunnel_attr_list.params, __TUNNEL_ATTR_MAX, tb,
1338                 blob_data(attr), blob_len(attr));
1339
1340         if (!(cur = tb[TUNNEL_ATTR_TYPE]))
1341                 return -EINVAL;
1342         str = blobmsg_data(cur);
1343
1344         unsigned int ttl = 0;
1345         if ((cur = tb[TUNNEL_ATTR_TTL]) && (ttl = blobmsg_get_u32(cur)) > 255)
1346                 return -EINVAL;
1347
1348         unsigned int link = 0;
1349         if ((cur = tb[TUNNEL_ATTR_LINK])) {
1350                 struct interface *iface = vlist_find(&interfaces, blobmsg_data(cur), iface, node);
1351                 if (!iface)
1352                         return -EINVAL;
1353
1354                 if (iface->l3_dev.dev)
1355                         link = iface->l3_dev.dev->ifindex;
1356         }
1357
1358
1359         if (!strcmp(str, "sit")) {
1360                 struct ip_tunnel_parm p = {
1361                         .link = link,
1362                         .iph = {
1363                                 .version = 4,
1364                                 .ihl = 5,
1365                                 .frag_off = htons(IP_DF),
1366                                 .protocol = IPPROTO_IPV6,
1367                                 .ttl = ttl
1368                         }
1369                 };
1370
1371                 if ((cur = tb[TUNNEL_ATTR_LOCAL]) &&
1372                                 inet_pton(AF_INET, blobmsg_data(cur), &p.iph.saddr) < 1)
1373                         return -EINVAL;
1374
1375                 if ((cur = tb[TUNNEL_ATTR_REMOTE]) &&
1376                                 inet_pton(AF_INET, blobmsg_data(cur), &p.iph.daddr) < 1)
1377                         return -EINVAL;
1378
1379                 strncpy(p.name, name, sizeof(p.name));
1380                 if (tunnel_ioctl("sit0", SIOCADDTUNNEL, &p) < 0)
1381                         return -1;
1382
1383 #ifdef SIOCADD6RD
1384                 if ((cur = tb[TUNNEL_ATTR_6RD_PREFIX])) {
1385                         unsigned int mask;
1386                         struct ip_tunnel_6rd p6;
1387
1388                         memset(&p6, 0, sizeof(p6));
1389
1390                         if (!parse_ip_and_netmask(AF_INET6, blobmsg_data(cur),
1391                                                 &p6.prefix, &mask) || mask > 128)
1392                                 return -EINVAL;
1393                         p6.prefixlen = mask;
1394
1395                         if ((cur = tb[TUNNEL_ATTR_6RD_RELAY_PREFIX])) {
1396                                 if (!parse_ip_and_netmask(AF_INET, blobmsg_data(cur),
1397                                                         &p6.relay_prefix, &mask) || mask > 32)
1398                                         return -EINVAL;
1399                                 p6.relay_prefixlen = mask;
1400                         }
1401
1402                         if (tunnel_ioctl(name, SIOCADD6RD, &p6) < 0) {
1403                                 system_del_ip_tunnel(name);
1404                                 return -1;
1405                         }
1406                 }
1407 #endif
1408         } else if (!strcmp(str, "ipip6")) {
1409                 struct ip6_tnl_parm p = {
1410                         .link = link,
1411                         .proto = IPPROTO_IPIP,
1412                         .hop_limit = (ttl) ? ttl : 64,
1413                         .encap_limit = 4,
1414                 };
1415
1416                 if ((cur = tb[TUNNEL_ATTR_LOCAL]) &&
1417                                 inet_pton(AF_INET6, blobmsg_data(cur), &p.laddr) < 1)
1418                         return -EINVAL;
1419
1420                 if ((cur = tb[TUNNEL_ATTR_REMOTE]) &&
1421                                 inet_pton(AF_INET6, blobmsg_data(cur), &p.raddr) < 1)
1422                         return -EINVAL;
1423
1424                 strncpy(p.name, name, sizeof(p.name));
1425                 if (tunnel_ioctl("ip6tnl0", SIOCADDTUNNEL, &p) < 0)
1426                         return -1;
1427         } else
1428                 return -EINVAL;
1429
1430         return 0;
1431 }