3372a6672580871bc1c461d27958bde37f07f4ee
[project/netifd.git] / system-linux.c
1 /*
2 * netifd - network interface daemon
3 * Copyright (C) 2012 Felix Fietkau <nbd@openwrt.org>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2
7 * as published by the Free Software Foundation
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 */
14 #define _GNU_SOURCE
15
16 #include <sys/socket.h>
17 #include <sys/ioctl.h>
18 #include <sys/stat.h>
19 #include <sys/syscall.h>
20
21 #include <net/if.h>
22 #include <net/if_arp.h>
23
24 #include <arpa/inet.h>
25 #include <netinet/in.h>
26
27 #include <linux/rtnetlink.h>
28 #include <linux/sockios.h>
29 #include <linux/ip.h>
30 #include <linux/if_vlan.h>
31 #include <linux/if_bridge.h>
32 #include <linux/if_tunnel.h>
33 #include <linux/ethtool.h>
34
35 #include <unistd.h>
36 #include <string.h>
37 #include <fcntl.h>
38 #include <glob.h>
39 #include <time.h>
40
41 #include <netlink/msg.h>
42 #include <netlink/attr.h>
43 #include <netlink/socket.h>
44 #include <libubox/uloop.h>
45
46 #include "netifd.h"
47 #include "device.h"
48 #include "system.h"
49
50 struct event_socket {
51 struct uloop_fd uloop;
52 struct nl_sock *sock;
53 struct nl_cb *cb;
54 };
55
56 static int sock_ioctl = -1;
57 static struct nl_sock *sock_rtnl = NULL;
58
59 static int cb_rtnl_event(struct nl_msg *msg, void *arg);
60 static void handle_hotplug_event(struct uloop_fd *u, unsigned int events);
61
62 static char dev_buf[256];
63
64 static void
65 handler_nl_event(struct uloop_fd *u, unsigned int events)
66 {
67 struct event_socket *ev = container_of(u, struct event_socket, uloop);
68 nl_recvmsgs(ev->sock, ev->cb);
69 }
70
71 static struct nl_sock *
72 create_socket(int protocol, int groups)
73 {
74 struct nl_sock *sock;
75
76 sock = nl_socket_alloc();
77 if (!sock)
78 return NULL;
79
80 if (groups)
81 nl_join_groups(sock, groups);
82
83 if (nl_connect(sock, protocol))
84 return NULL;
85
86 return sock;
87 }
88
89 static bool
90 create_raw_event_socket(struct event_socket *ev, int protocol, int groups,
91 uloop_fd_handler cb)
92 {
93 ev->sock = create_socket(protocol, groups);
94 if (!ev->sock)
95 return false;
96
97 ev->uloop.fd = nl_socket_get_fd(ev->sock);
98 ev->uloop.cb = cb;
99 uloop_fd_add(&ev->uloop, ULOOP_READ | ULOOP_EDGE_TRIGGER);
100 return true;
101 }
102
103 static bool
104 create_event_socket(struct event_socket *ev, int protocol,
105 int (*cb)(struct nl_msg *msg, void *arg))
106 {
107 // Prepare socket for link events
108 ev->cb = nl_cb_alloc(NL_CB_DEFAULT);
109 if (!ev->cb)
110 return false;
111
112 nl_cb_set(ev->cb, NL_CB_VALID, NL_CB_CUSTOM, cb, NULL);
113
114 return create_raw_event_socket(ev, protocol, 0, handler_nl_event);
115 }
116
117 int system_init(void)
118 {
119 static struct event_socket rtnl_event;
120 static struct event_socket hotplug_event;
121
122 sock_ioctl = socket(AF_LOCAL, SOCK_DGRAM, 0);
123 fcntl(sock_ioctl, F_SETFD, fcntl(sock_ioctl, F_GETFD) | FD_CLOEXEC);
124
125 // Prepare socket for routing / address control
126 sock_rtnl = create_socket(NETLINK_ROUTE, 0);
127 if (!sock_rtnl)
128 return -1;
129
130 if (!create_event_socket(&rtnl_event, NETLINK_ROUTE, cb_rtnl_event))
131 return -1;
132
133 if (!create_raw_event_socket(&hotplug_event, NETLINK_KOBJECT_UEVENT, 1,
134 handle_hotplug_event))
135 return -1;
136
137 // Receive network link events form kernel
138 nl_socket_add_membership(rtnl_event.sock, RTNLGRP_LINK);
139
140 return 0;
141 }
142
143 static void system_set_sysctl(const char *path, const char *val)
144 {
145 int fd;
146
147 fd = open(path, O_WRONLY);
148 if (fd < 0)
149 return;
150
151 write(fd, val, strlen(val));
152 close(fd);
153 }
154
155 static void system_set_dev_sysctl(const char *path, const char *device, const char *val)
156 {
157 snprintf(dev_buf, sizeof(dev_buf), path, val);
158 system_set_sysctl(dev_buf, val);
159 }
160
161 static void system_set_disable_ipv6(struct device *dev, const char *val)
162 {
163 system_set_dev_sysctl("/proc/sys/net/ipv6/conf/%s/disable_ipv6", dev->ifname, val);
164 }
165
166 // Evaluate netlink messages
167 static int cb_rtnl_event(struct nl_msg *msg, void *arg)
168 {
169 struct nlmsghdr *nh = nlmsg_hdr(msg);
170 struct ifinfomsg *ifi = NLMSG_DATA(nh);
171 struct nlattr *nla[__IFLA_MAX];
172
173 if (nh->nlmsg_type != RTM_DELLINK && nh->nlmsg_type != RTM_NEWLINK)
174 goto out;
175
176 nlmsg_parse(nh, sizeof(*ifi), nla, __IFLA_MAX - 1, NULL);
177 if (!nla[IFLA_IFNAME])
178 goto out;
179
180 struct device *dev = device_get(RTA_DATA(nla[IFLA_IFNAME]), false);
181 if (!dev)
182 goto out;
183
184 dev->ifindex = ifi->ifi_index;
185 /* TODO: parse link status */
186
187 out:
188 return 0;
189 }
190
191 static void
192 handle_hotplug_msg(char *data, int size)
193 {
194 const char *subsystem = NULL, *interface = NULL;
195 char *cur, *end, *sep;
196 struct device *dev;
197 int skip;
198 bool add;
199
200 if (!strncmp(data, "add@", 4))
201 add = true;
202 else if (!strncmp(data, "remove@", 7))
203 add = false;
204 else
205 return;
206
207 skip = strlen(data) + 1;
208 end = data + size;
209
210 for (cur = data + skip; cur < end; cur += skip) {
211 skip = strlen(cur) + 1;
212
213 sep = strchr(cur, '=');
214 if (!sep)
215 continue;
216
217 *sep = 0;
218 if (!strcmp(cur, "INTERFACE"))
219 interface = sep + 1;
220 else if (!strcmp(cur, "SUBSYSTEM")) {
221 subsystem = sep + 1;
222 if (strcmp(subsystem, "net") != 0)
223 return;
224 }
225 if (subsystem && interface)
226 goto found;
227 }
228 return;
229
230 found:
231 dev = device_get(interface, false);
232 if (!dev)
233 return;
234
235 if (dev->type != &simple_device_type)
236 return;
237
238 device_set_present(dev, add);
239 }
240
241 static void
242 handle_hotplug_event(struct uloop_fd *u, unsigned int events)
243 {
244 struct event_socket *ev = container_of(u, struct event_socket, uloop);
245 struct sockaddr_nl nla;
246 unsigned char *buf = NULL;
247 int size;
248
249 while ((size = nl_recv(ev->sock, &nla, &buf, NULL)) > 0) {
250 if (nla.nl_pid == 0)
251 handle_hotplug_msg((char *) buf, size);
252
253 free(buf);
254 }
255 }
256
257 static int system_rtnl_call(struct nl_msg *msg)
258 {
259 int ret;
260
261 ret = nl_send_auto_complete(sock_rtnl, msg);
262 nlmsg_free(msg);
263
264 if (ret < 0)
265 return ret;
266
267 return nl_wait_for_ack(sock_rtnl);
268 }
269
270 int system_bridge_delbr(struct device *bridge)
271 {
272 return ioctl(sock_ioctl, SIOCBRDELBR, bridge->ifname);
273 }
274
275 static int system_bridge_if(const char *bridge, struct device *dev, int cmd, void *data)
276 {
277 struct ifreq ifr;
278
279 memset(&ifr, 0, sizeof(ifr));
280 if (dev)
281 ifr.ifr_ifindex = dev->ifindex;
282 else
283 ifr.ifr_data = data;
284 strncpy(ifr.ifr_name, bridge, sizeof(ifr.ifr_name));
285 return ioctl(sock_ioctl, cmd, &ifr);
286 }
287
288 static bool system_is_bridge(const char *name, char *buf, int buflen)
289 {
290 struct stat st;
291
292 snprintf(buf, buflen, "/sys/devices/virtual/net/%s/bridge", name);
293 if (stat(buf, &st) < 0)
294 return false;
295
296 return true;
297 }
298
299 static char *system_get_bridge(const char *name, char *buf, int buflen)
300 {
301 char *path;
302 ssize_t len;
303 glob_t gl;
304
305 snprintf(buf, buflen, "/sys/devices/virtual/net/*/brif/%s/bridge", name);
306 if (glob(buf, GLOB_NOSORT, NULL, &gl) < 0)
307 return NULL;
308
309 if (gl.gl_pathc == 0)
310 return NULL;
311
312 len = readlink(gl.gl_pathv[0], buf, buflen);
313 if (len < 0)
314 return NULL;
315
316 buf[len] = 0;
317 path = strrchr(buf, '/');
318 if (!path)
319 return NULL;
320
321 return path + 1;
322 }
323
324 int system_bridge_addif(struct device *bridge, struct device *dev)
325 {
326 char *oldbr;
327
328 system_set_disable_ipv6(dev, "1");
329 oldbr = system_get_bridge(dev->ifname, dev_buf, sizeof(dev_buf));
330 if (oldbr && !strcmp(oldbr, bridge->ifname))
331 return 0;
332
333 return system_bridge_if(bridge->ifname, dev, SIOCBRADDIF, NULL);
334 }
335
336 int system_bridge_delif(struct device *bridge, struct device *dev)
337 {
338 system_set_disable_ipv6(dev, "0");
339 return system_bridge_if(bridge->ifname, dev, SIOCBRDELIF, NULL);
340 }
341
342 static int system_if_resolve(struct device *dev)
343 {
344 struct ifreq ifr;
345 strncpy(ifr.ifr_name, dev->ifname, sizeof(ifr.ifr_name));
346 if (!ioctl(sock_ioctl, SIOCGIFINDEX, &ifr))
347 return ifr.ifr_ifindex;
348 else
349 return 0;
350 }
351
352 static int system_if_flags(const char *ifname, unsigned add, unsigned rem)
353 {
354 struct ifreq ifr;
355
356 memset(&ifr, 0, sizeof(ifr));
357 strncpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
358 ioctl(sock_ioctl, SIOCGIFFLAGS, &ifr);
359 ifr.ifr_flags |= add;
360 ifr.ifr_flags &= ~rem;
361 return ioctl(sock_ioctl, SIOCSIFFLAGS, &ifr);
362 }
363
364 struct clear_data {
365 struct nl_msg *msg;
366 struct device *dev;
367 int type;
368 int size;
369 int af;
370 };
371
372
373 static bool check_ifaddr(struct nlmsghdr *hdr, int ifindex)
374 {
375 struct ifaddrmsg *ifa = NLMSG_DATA(hdr);
376
377 return ifa->ifa_index == ifindex;
378 }
379
380 static bool check_route(struct nlmsghdr *hdr, int ifindex)
381 {
382 struct nlattr *tb[__RTA_MAX];
383
384 nlmsg_parse(hdr, sizeof(struct rtmsg), tb, __RTA_MAX - 1, NULL);
385 if (!tb[RTA_OIF])
386 return false;
387
388 return *(int *)RTA_DATA(tb[RTA_OIF]) == ifindex;
389 }
390
391 static int cb_clear_event(struct nl_msg *msg, void *arg)
392 {
393 struct clear_data *clr = arg;
394 struct nlmsghdr *hdr = nlmsg_hdr(msg);
395 bool (*cb)(struct nlmsghdr *, int ifindex);
396 int type;
397
398 switch(clr->type) {
399 case RTM_GETADDR:
400 type = RTM_DELADDR;
401 if (hdr->nlmsg_type != RTM_NEWADDR)
402 return NL_SKIP;
403
404 cb = check_ifaddr;
405 break;
406 case RTM_GETROUTE:
407 type = RTM_DELROUTE;
408 if (hdr->nlmsg_type != RTM_NEWROUTE)
409 return NL_SKIP;
410
411 cb = check_route;
412 break;
413 default:
414 return NL_SKIP;
415 }
416
417 if (!cb(hdr, clr->dev->ifindex))
418 return NL_SKIP;
419
420 D(SYSTEM, "Remove %s from device %s\n",
421 type == RTM_DELADDR ? "an address" : "a route",
422 clr->dev->ifname);
423 memcpy(nlmsg_hdr(clr->msg), hdr, hdr->nlmsg_len);
424 hdr = nlmsg_hdr(clr->msg);
425 hdr->nlmsg_type = type;
426 hdr->nlmsg_flags = NLM_F_REQUEST;
427
428 if (!nl_send_auto_complete(sock_rtnl, clr->msg))
429 nl_wait_for_ack(sock_rtnl);
430
431 return NL_SKIP;
432 }
433
434 static int
435 cb_finish_event(struct nl_msg *msg, void *arg)
436 {
437 int *pending = arg;
438 *pending = 0;
439 return NL_STOP;
440 }
441
442 static int
443 error_handler(struct sockaddr_nl *nla, struct nlmsgerr *err, void *arg)
444 {
445 int *pending = arg;
446 *pending = err->error;
447 return NL_STOP;
448 }
449
450 static void
451 system_if_clear_entries(struct device *dev, int type, int af)
452 {
453 struct clear_data clr;
454 struct nl_cb *cb = nl_cb_alloc(NL_CB_DEFAULT);
455 struct rtmsg rtm = {
456 .rtm_family = af,
457 .rtm_flags = RTM_F_CLONED,
458 };
459 int flags = NLM_F_DUMP;
460 int pending = 1;
461
462 clr.af = af;
463 clr.dev = dev;
464 clr.type = type;
465 switch (type) {
466 case RTM_GETADDR:
467 clr.size = sizeof(struct rtgenmsg);
468 break;
469 case RTM_GETROUTE:
470 clr.size = sizeof(struct rtmsg);
471 break;
472 default:
473 return;
474 }
475
476 if (!cb)
477 return;
478
479 clr.msg = nlmsg_alloc_simple(type, flags);
480 if (!clr.msg)
481 goto out;
482
483 nlmsg_append(clr.msg, &rtm, clr.size, 0);
484 nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM, cb_clear_event, &clr);
485 nl_cb_set(cb, NL_CB_FINISH, NL_CB_CUSTOM, cb_finish_event, &pending);
486 nl_cb_err(cb, NL_CB_CUSTOM, error_handler, &pending);
487
488 nl_send_auto_complete(sock_rtnl, clr.msg);
489 while (pending > 0)
490 nl_recvmsgs(sock_rtnl, cb);
491
492 nlmsg_free(clr.msg);
493 out:
494 nl_cb_put(cb);
495 }
496
497 /*
498 * Clear bridge (membership) state and bring down device
499 */
500 void system_if_clear_state(struct device *dev)
501 {
502 static char buf[256];
503 char *bridge;
504
505 if (dev->external)
506 return;
507
508 dev->ifindex = system_if_resolve(dev);
509 if (!dev->ifindex)
510 return;
511
512 system_if_flags(dev->ifname, 0, IFF_UP);
513
514 if (system_is_bridge(dev->ifname, buf, sizeof(buf))) {
515 D(SYSTEM, "Delete existing bridge named '%s'\n", dev->ifname);
516 system_bridge_delbr(dev);
517 return;
518 }
519
520 bridge = system_get_bridge(dev->ifname, buf, sizeof(buf));
521 if (bridge) {
522 D(SYSTEM, "Remove device '%s' from bridge '%s'\n", dev->ifname, bridge);
523 system_bridge_if(bridge, dev, SIOCBRDELIF, NULL);
524 }
525
526 system_if_clear_entries(dev, RTM_GETROUTE, AF_INET);
527 system_if_clear_entries(dev, RTM_GETADDR, AF_INET);
528 system_if_clear_entries(dev, RTM_GETROUTE, AF_INET6);
529 system_if_clear_entries(dev, RTM_GETADDR, AF_INET6);
530 system_set_disable_ipv6(dev, "0");
531 }
532
533 static inline unsigned long
534 sec_to_jiffies(int val)
535 {
536 return (unsigned long) val * 100;
537 }
538
539 int system_bridge_addbr(struct device *bridge, struct bridge_config *cfg)
540 {
541 unsigned long args[4] = {};
542
543 if (ioctl(sock_ioctl, SIOCBRADDBR, bridge->ifname) < 0)
544 return -1;
545
546 args[0] = BRCTL_SET_BRIDGE_STP_STATE;
547 args[1] = !!cfg->stp;
548 system_bridge_if(bridge->ifname, NULL, SIOCDEVPRIVATE, &args);
549
550 args[0] = BRCTL_SET_BRIDGE_FORWARD_DELAY;
551 args[1] = sec_to_jiffies(cfg->forward_delay);
552 system_bridge_if(bridge->ifname, NULL, SIOCDEVPRIVATE, &args);
553
554 system_set_dev_sysctl("/sys/devices/virtual/net/%s/bridge/multicast_snooping",
555 bridge->ifname, cfg->igmp_snoop ? "1" : "0");
556
557 if (cfg->flags & BRIDGE_OPT_AGEING_TIME) {
558 args[0] = BRCTL_SET_AGEING_TIME;
559 args[1] = sec_to_jiffies(cfg->ageing_time);
560 system_bridge_if(bridge->ifname, NULL, SIOCDEVPRIVATE, &args);
561 }
562
563 if (cfg->flags & BRIDGE_OPT_HELLO_TIME) {
564 args[0] = BRCTL_SET_BRIDGE_HELLO_TIME;
565 args[1] = sec_to_jiffies(cfg->hello_time);
566 system_bridge_if(bridge->ifname, NULL, SIOCDEVPRIVATE, &args);
567 }
568
569 if (cfg->flags & BRIDGE_OPT_MAX_AGE) {
570 args[0] = BRCTL_SET_BRIDGE_MAX_AGE;
571 args[1] = sec_to_jiffies(cfg->max_age);
572 system_bridge_if(bridge->ifname, NULL, SIOCDEVPRIVATE, &args);
573 }
574
575 return 0;
576 }
577
578 static int system_vlan(struct device *dev, int id)
579 {
580 struct vlan_ioctl_args ifr = {
581 .cmd = SET_VLAN_NAME_TYPE_CMD,
582 .u.name_type = VLAN_NAME_TYPE_RAW_PLUS_VID_NO_PAD,
583 };
584
585 ioctl(sock_ioctl, SIOCSIFVLAN, &ifr);
586
587 if (id < 0) {
588 ifr.cmd = DEL_VLAN_CMD;
589 ifr.u.VID = 0;
590 } else {
591 ifr.cmd = ADD_VLAN_CMD;
592 ifr.u.VID = id;
593 }
594 strncpy(ifr.device1, dev->ifname, sizeof(ifr.device1));
595 return ioctl(sock_ioctl, SIOCSIFVLAN, &ifr);
596 }
597
598 int system_vlan_add(struct device *dev, int id)
599 {
600 return system_vlan(dev, id);
601 }
602
603 int system_vlan_del(struct device *dev)
604 {
605 return system_vlan(dev, -1);
606 }
607
608 static void
609 system_if_get_settings(struct device *dev, struct device_settings *s)
610 {
611 struct ifreq ifr;
612
613 memset(&ifr, 0, sizeof(ifr));
614 strncpy(ifr.ifr_name, dev->ifname, sizeof(ifr.ifr_name));
615
616 if (ioctl(sock_ioctl, SIOCGIFMTU, &ifr) == 0) {
617 s->mtu = ifr.ifr_mtu;
618 s->flags |= DEV_OPT_MTU;
619 }
620
621 if (ioctl(sock_ioctl, SIOCGIFTXQLEN, &ifr) == 0) {
622 s->txqueuelen = ifr.ifr_qlen;
623 s->flags |= DEV_OPT_TXQUEUELEN;
624 }
625
626 if (ioctl(sock_ioctl, SIOCGIFHWADDR, &ifr) == 0) {
627 memcpy(s->macaddr, &ifr.ifr_hwaddr.sa_data, sizeof(s->macaddr));
628 s->flags |= DEV_OPT_MACADDR;
629 }
630 }
631
632 static void
633 system_if_apply_settings(struct device *dev, struct device_settings *s)
634 {
635 struct ifreq ifr;
636
637 memset(&ifr, 0, sizeof(ifr));
638 strncpy(ifr.ifr_name, dev->ifname, sizeof(ifr.ifr_name));
639 if (s->flags & DEV_OPT_MTU) {
640 ifr.ifr_mtu = s->mtu;
641 if (ioctl(sock_ioctl, SIOCSIFMTU, &ifr) < 0)
642 s->flags &= ~DEV_OPT_MTU;
643 }
644 if (s->flags & DEV_OPT_TXQUEUELEN) {
645 ifr.ifr_qlen = s->txqueuelen;
646 if (ioctl(sock_ioctl, SIOCSIFTXQLEN, &ifr) < 0)
647 s->flags &= ~DEV_OPT_TXQUEUELEN;
648 }
649 if (s->flags & DEV_OPT_MACADDR) {
650 ifr.ifr_hwaddr.sa_family = ARPHRD_ETHER;
651 memcpy(&ifr.ifr_hwaddr.sa_data, s->macaddr, sizeof(s->macaddr));
652 if (ioctl(sock_ioctl, SIOCSIFHWADDR, &ifr) < 0)
653 s->flags &= ~DEV_OPT_MACADDR;
654 }
655 }
656
657 int system_if_up(struct device *dev)
658 {
659 system_if_get_settings(dev, &dev->orig_settings);
660 system_if_apply_settings(dev, &dev->settings);
661 dev->ifindex = system_if_resolve(dev);
662 return system_if_flags(dev->ifname, IFF_UP, 0);
663 }
664
665 int system_if_down(struct device *dev)
666 {
667 int ret = system_if_flags(dev->ifname, 0, IFF_UP);
668 dev->orig_settings.flags &= dev->settings.flags;
669 system_if_apply_settings(dev, &dev->orig_settings);
670 return ret;
671 }
672
673 int system_if_check(struct device *dev)
674 {
675 device_set_present(dev, (system_if_resolve(dev) > 0));
676 return 0;
677 }
678
679 struct device *
680 system_if_get_parent(struct device *dev)
681 {
682 char buf[64], *devname;
683 int ifindex, iflink, len;
684 FILE *f;
685
686 snprintf(buf, sizeof(buf), "/sys/class/net/%s/iflink", dev->ifname);
687 f = fopen(buf, "r");
688 if (!f)
689 return NULL;
690
691 len = fread(buf, 1, sizeof(buf) - 1, f);
692 fclose(f);
693
694 if (len <= 0)
695 return NULL;
696
697 buf[len] = 0;
698 iflink = strtoul(buf, NULL, 0);
699 ifindex = system_if_resolve(dev);
700 if (!iflink || iflink == ifindex)
701 return NULL;
702
703 devname = if_indextoname(iflink, buf);
704 if (!devname)
705 return NULL;
706
707 return device_get(devname, true);
708 }
709
710 static bool
711 read_string_file(int dir_fd, const char *file, char *buf, int len)
712 {
713 bool ret = false;
714 char *c;
715 int fd;
716
717 fd = openat(dir_fd, file, O_RDONLY);
718 if (fd < 0)
719 return false;
720
721 retry:
722 len = read(fd, buf, len - 1);
723 if (len < 0) {
724 if (errno == EINTR)
725 goto retry;
726 } else if (len > 0) {
727 buf[len] = 0;
728
729 c = strchr(buf, '\n');
730 if (c)
731 *c = 0;
732
733 ret = true;
734 }
735
736 close(fd);
737
738 return ret;
739 }
740
741 static bool
742 read_int_file(int dir_fd, const char *file, int *val)
743 {
744 char buf[64];
745 bool ret = false;
746
747 ret = read_string_file(dir_fd, file, buf, sizeof(buf));
748 if (ret)
749 *val = strtoul(buf, NULL, 0);
750
751 return ret;
752 }
753
754 /* Assume advertised flags == supported flags */
755 static const struct {
756 uint32_t mask;
757 const char *name;
758 } ethtool_link_modes[] = {
759 { ADVERTISED_10baseT_Half, "10H" },
760 { ADVERTISED_10baseT_Full, "10F" },
761 { ADVERTISED_100baseT_Half, "100H" },
762 { ADVERTISED_100baseT_Full, "100F" },
763 { ADVERTISED_1000baseT_Half, "1000H" },
764 { ADVERTISED_1000baseT_Full, "1000F" },
765 };
766
767 static void system_add_link_modes(struct blob_buf *b, __u32 mask)
768 {
769 int i;
770 for (i = 0; i < ARRAY_SIZE(ethtool_link_modes); i++) {
771 if (mask & ethtool_link_modes[i].mask)
772 blobmsg_add_string(b, NULL, ethtool_link_modes[i].name);
773 }
774 }
775
776 bool
777 system_if_force_external(const char *ifname)
778 {
779 char buf[64];
780 struct stat s;
781
782 snprintf(buf, sizeof(buf), "/sys/class/net/%s/phy80211", ifname);
783 return stat(buf, &s) == 0;
784 }
785
786 int
787 system_if_dump_info(struct device *dev, struct blob_buf *b)
788 {
789 struct ethtool_cmd ecmd;
790 struct ifreq ifr;
791 char buf[64], *s;
792 void *c;
793 int dir_fd, val = 0;
794
795 snprintf(buf, sizeof(buf), "/sys/class/net/%s", dev->ifname);
796 dir_fd = open(buf, O_DIRECTORY);
797
798 if (read_int_file(dir_fd, "carrier", &val))
799 blobmsg_add_u8(b, "link", !!val);
800
801 memset(&ecmd, 0, sizeof(ecmd));
802 memset(&ifr, 0, sizeof(ifr));
803 strcpy(ifr.ifr_name, dev->ifname);
804 ifr.ifr_data = (caddr_t) &ecmd;
805 ecmd.cmd = ETHTOOL_GSET;
806
807 if (ioctl(sock_ioctl, SIOCETHTOOL, &ifr) == 0) {
808 c = blobmsg_open_array(b, "link-advertising");
809 system_add_link_modes(b, ecmd.advertising);
810 blobmsg_close_array(b, c);
811
812 c = blobmsg_open_array(b, "link-supported");
813 system_add_link_modes(b, ecmd.supported);
814 blobmsg_close_array(b, c);
815
816 s = blobmsg_alloc_string_buffer(b, "speed", 8);
817 snprintf(s, 8, "%d%c", ethtool_cmd_speed(&ecmd),
818 ecmd.duplex == DUPLEX_HALF ? 'H' : 'F');
819 blobmsg_add_string_buffer(b);
820 }
821
822 close(dir_fd);
823 return 0;
824 }
825
826 int
827 system_if_dump_stats(struct device *dev, struct blob_buf *b)
828 {
829 const char *const counters[] = {
830 "collisions", "rx_frame_errors", "tx_compressed",
831 "multicast", "rx_length_errors", "tx_dropped",
832 "rx_bytes", "rx_missed_errors", "tx_errors",
833 "rx_compressed", "rx_over_errors", "tx_fifo_errors",
834 "rx_crc_errors", "rx_packets", "tx_heartbeat_errors",
835 "rx_dropped", "tx_aborted_errors", "tx_packets",
836 "rx_errors", "tx_bytes", "tx_window_errors",
837 "rx_fifo_errors", "tx_carrier_errors",
838 };
839 char buf[64];
840 int stats_dir;
841 int i, val = 0;
842
843 snprintf(buf, sizeof(buf), "/sys/class/net/%s/statistics", dev->ifname);
844 stats_dir = open(buf, O_DIRECTORY);
845 if (stats_dir < 0)
846 return -1;
847
848 for (i = 0; i < ARRAY_SIZE(counters); i++)
849 if (read_int_file(stats_dir, counters[i], &val))
850 blobmsg_add_u32(b, counters[i], val);
851
852 close(stats_dir);
853 return 0;
854 }
855
856 static int system_addr(struct device *dev, struct device_addr *addr, int cmd)
857 {
858 bool v4 = ((addr->flags & DEVADDR_FAMILY) == DEVADDR_INET4);
859 int alen = v4 ? 4 : 16;
860 struct ifaddrmsg ifa = {
861 .ifa_family = (alen == 4) ? AF_INET : AF_INET6,
862 .ifa_prefixlen = addr->mask,
863 .ifa_index = dev->ifindex,
864 };
865
866 struct nl_msg *msg;
867
868 msg = nlmsg_alloc_simple(cmd, 0);
869 if (!msg)
870 return -1;
871
872 nlmsg_append(msg, &ifa, sizeof(ifa), 0);
873 nla_put(msg, IFA_LOCAL, alen, &addr->addr);
874 if (v4) {
875 if (addr->broadcast)
876 nla_put_u32(msg, IFA_BROADCAST, addr->broadcast);
877 if (addr->point_to_point)
878 nla_put_u32(msg, IFA_ADDRESS, addr->point_to_point);
879 }
880
881 return system_rtnl_call(msg);
882 }
883
884 int system_add_address(struct device *dev, struct device_addr *addr)
885 {
886 return system_addr(dev, addr, RTM_NEWADDR);
887 }
888
889 int system_del_address(struct device *dev, struct device_addr *addr)
890 {
891 return system_addr(dev, addr, RTM_DELADDR);
892 }
893
894 static int system_rt(struct device *dev, struct device_route *route, int cmd)
895 {
896 int alen = ((route->flags & DEVADDR_FAMILY) == DEVADDR_INET4) ? 4 : 16;
897 bool have_gw;
898 unsigned int flags = 0;
899 int ifindex = dev->ifindex;
900
901 if (alen == 4)
902 have_gw = !!route->nexthop.in.s_addr;
903 else
904 have_gw = route->nexthop.in6.s6_addr32[0] ||
905 route->nexthop.in6.s6_addr32[1] ||
906 route->nexthop.in6.s6_addr32[2] ||
907 route->nexthop.in6.s6_addr32[3];
908
909 unsigned char scope = (cmd == RTM_DELROUTE) ? RT_SCOPE_NOWHERE :
910 (have_gw) ? RT_SCOPE_UNIVERSE : RT_SCOPE_LINK;
911
912 struct rtmsg rtm = {
913 .rtm_family = (alen == 4) ? AF_INET : AF_INET6,
914 .rtm_dst_len = route->mask,
915 .rtm_table = RT_TABLE_MAIN,
916 .rtm_protocol = (route->flags & DEVADDR_KERNEL) ? RTPROT_KERNEL : RTPROT_BOOT,
917 .rtm_scope = scope,
918 .rtm_type = (cmd == RTM_DELROUTE) ? 0: RTN_UNICAST,
919 };
920 struct nl_msg *msg;
921
922 if (cmd == RTM_NEWROUTE)
923 flags |= NLM_F_CREATE | NLM_F_REPLACE;
924
925 msg = nlmsg_alloc_simple(cmd, flags);
926 if (!msg)
927 return -1;
928
929 nlmsg_append(msg, &rtm, sizeof(rtm), 0);
930
931 if (route->mask)
932 nla_put(msg, RTA_DST, alen, &route->addr);
933
934 if (route->metric > 0)
935 nla_put_u32(msg, RTA_PRIORITY, route->metric);
936
937 if (have_gw)
938 nla_put(msg, RTA_GATEWAY, alen, &route->nexthop);
939
940 nla_put_u32(msg, RTA_OIF, ifindex);
941
942 return system_rtnl_call(msg);
943 }
944
945 int system_add_route(struct device *dev, struct device_route *route)
946 {
947 return system_rt(dev, route, RTM_NEWROUTE);
948 }
949
950 int system_del_route(struct device *dev, struct device_route *route)
951 {
952 return system_rt(dev, route, RTM_DELROUTE);
953 }
954
955 int system_flush_routes(void)
956 {
957 const char *names[] = {
958 "/proc/sys/net/ipv4/route/flush",
959 "/proc/sys/net/ipv6/route/flush"
960 };
961 int fd, i;
962
963 for (i = 0; i < ARRAY_SIZE(names); i++) {
964 fd = open(names[i], O_WRONLY);
965 if (fd < 0)
966 continue;
967
968 write(fd, "-1", 2);
969 close(fd);
970 }
971 return 0;
972 }
973
974 time_t system_get_rtime(void)
975 {
976 struct timespec ts;
977 struct timeval tv;
978
979 if (syscall(__NR_clock_gettime, CLOCK_MONOTONIC, &ts) == 0)
980 return ts.tv_sec;
981
982 if (gettimeofday(&tv, NULL) == 0)
983 return tv.tv_sec;
984
985 return 0;
986 }
987
988 #ifndef IP_DF
989 #define IP_DF 0x4000
990 #endif
991
992 static void tunnel_parm_init(struct ip_tunnel_parm *p)
993 {
994 memset(p, 0, sizeof(*p));
995 p->iph.version = 4;
996 p->iph.ihl = 5;
997 p->iph.frag_off = htons(IP_DF);
998 }
999
1000 static int tunnel_ioctl(const char *name, int cmd, void *p)
1001 {
1002 struct ifreq ifr;
1003
1004 memset(&ifr, 0, sizeof(ifr));
1005 strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
1006 ifr.ifr_ifru.ifru_data = p;
1007 return ioctl(sock_ioctl, cmd, &ifr);
1008 }
1009
1010 int system_del_ip_tunnel(const char *name)
1011 {
1012 struct ip_tunnel_parm p;
1013
1014 tunnel_parm_init(&p);
1015 return tunnel_ioctl(name, SIOCDELTUNNEL, &p);
1016 }
1017
1018 static int parse_ipaddr(struct blob_attr *attr, __be32 *addr)
1019 {
1020 if (!attr)
1021 return 1;
1022
1023 return inet_pton(AF_INET, blobmsg_data(attr), (void *) addr);
1024 }
1025
1026
1027 int system_add_ip_tunnel(const char *name, struct blob_attr *attr)
1028 {
1029 struct blob_attr *tb[__TUNNEL_ATTR_MAX];
1030 struct blob_attr *cur;
1031 struct ip_tunnel_parm p;
1032 const char *base, *str;
1033 bool is_sit;
1034
1035 system_del_ip_tunnel(name);
1036
1037 tunnel_parm_init(&p);
1038
1039 blobmsg_parse(tunnel_attr_list.params, __TUNNEL_ATTR_MAX, tb,
1040 blob_data(attr), blob_len(attr));
1041
1042 if (!(cur = tb[TUNNEL_ATTR_TYPE]))
1043 return -EINVAL;
1044 str = blobmsg_data(cur);
1045 is_sit = !strcmp(str, "sit");
1046
1047 if (is_sit) {
1048 p.iph.protocol = IPPROTO_IPV6;
1049 base = "sit0";
1050 } else
1051 return -EINVAL;
1052
1053 if (!parse_ipaddr(tb[TUNNEL_ATTR_LOCAL], &p.iph.saddr))
1054 return -EINVAL;
1055
1056 if (!parse_ipaddr(tb[TUNNEL_ATTR_REMOTE], &p.iph.daddr))
1057 return -EINVAL;
1058
1059 if ((cur = tb[TUNNEL_ATTR_TTL])) {
1060 unsigned int val = blobmsg_get_u32(cur);
1061
1062 if (val > 255)
1063 return -EINVAL;
1064
1065 p.iph.ttl = val;
1066 }
1067
1068 strncpy(p.name, name, sizeof(p.name));
1069 if (tunnel_ioctl(base, SIOCADDTUNNEL, &p) < 0)
1070 return -1;
1071
1072 #ifdef SIOCADD6RD
1073 cur = tb[TUNNEL_ATTR_6RD_PREFIX];
1074 if (cur && is_sit) {
1075 unsigned int mask;
1076 struct ip_tunnel_6rd p6;
1077
1078 memset(&p6, 0, sizeof(p6));
1079
1080 if (!parse_ip_and_netmask(AF_INET6, blobmsg_data(cur),
1081 &p6.prefix, &mask) || mask > 128)
1082 return -EINVAL;
1083 p6.prefixlen = mask;
1084
1085 if ((cur = tb[TUNNEL_ATTR_6RD_RELAY_PREFIX])) {
1086 if (!parse_ip_and_netmask(AF_INET, blobmsg_data(cur),
1087 &p6.relay_prefix, &mask) || mask > 32)
1088 return -EINVAL;
1089 p6.relay_prefixlen = mask;
1090 }
1091
1092 if (tunnel_ioctl(name, SIOCADD6RD, &p6) < 0) {
1093 system_del_ip_tunnel(name);
1094 return -1;
1095 }
1096 }
1097 #endif
1098
1099 return 0;
1100 }