device: extend device settings flags to 64 bit
[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 * Copyright (C) 2017 Matthias Schiffer <mschiffer@universe-factory.net>
8 * Copyright (C) 2018 Hans Dedecker <dedeckeh@gmail.com>
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2
12 * as published by the Free Software Foundation
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 */
19 #define _GNU_SOURCE
20
21 #include <sys/socket.h>
22 #include <sys/ioctl.h>
23 #include <sys/stat.h>
24 #include <sys/syscall.h>
25
26 #include <net/if.h>
27 #include <net/if_arp.h>
28
29 #include <limits.h>
30 #include <arpa/inet.h>
31 #include <netinet/in.h>
32 #include <netinet/ether.h>
33
34 #include <linux/rtnetlink.h>
35 #include <linux/neighbour.h>
36 #include <linux/sockios.h>
37 #include <linux/ip.h>
38 #include <linux/if_addr.h>
39 #include <linux/if_link.h>
40 #include <linux/if_vlan.h>
41 #include <linux/if_bridge.h>
42 #include <linux/if_tunnel.h>
43 #include <linux/ip6_tunnel.h>
44 #include <linux/ethtool.h>
45 #include <linux/fib_rules.h>
46 #include <linux/veth.h>
47 #include <linux/version.h>
48
49 #include <sched.h>
50
51 #ifndef RTN_FAILED_POLICY
52 #define RTN_FAILED_POLICY 12
53 #endif
54
55 #ifndef IFA_F_NOPREFIXROUTE
56 #define IFA_F_NOPREFIXROUTE 0x200
57 #endif
58
59 #ifndef IFA_FLAGS
60 #define IFA_FLAGS (IFA_MULTICAST + 1)
61 #endif
62
63 #include <string.h>
64 #include <fcntl.h>
65 #include <glob.h>
66 #include <time.h>
67 #include <unistd.h>
68
69 #include <netlink/msg.h>
70 #include <netlink/attr.h>
71 #include <netlink/socket.h>
72 #include <libubox/uloop.h>
73
74 #include "netifd.h"
75 #include "device.h"
76 #include "system.h"
77 #include "utils.h"
78
79 struct event_socket {
80 struct uloop_fd uloop;
81 struct nl_sock *sock;
82 int bufsize;
83 };
84
85 static int sock_ioctl = -1;
86 static struct nl_sock *sock_rtnl = NULL;
87
88 static int cb_rtnl_event(struct nl_msg *msg, void *arg);
89 static void handle_hotplug_event(struct uloop_fd *u, unsigned int events);
90 static int system_add_proto_tunnel(const char *name, const uint8_t proto,
91 const unsigned int link, struct blob_attr **tb);
92 static int __system_del_ip_tunnel(const char *name, struct blob_attr **tb);
93
94 static char dev_buf[256];
95
96 static void
97 handler_nl_event(struct uloop_fd *u, unsigned int events)
98 {
99 struct event_socket *ev = container_of(u, struct event_socket, uloop);
100 int err;
101 socklen_t errlen = sizeof(err);
102
103 if (!u->error) {
104 nl_recvmsgs_default(ev->sock);
105 return;
106 }
107
108 if (getsockopt(u->fd, SOL_SOCKET, SO_ERROR, (void *)&err, &errlen))
109 goto abort;
110
111 switch(err) {
112 case ENOBUFS:
113 /* Increase rx buffer size on netlink socket */
114 ev->bufsize *= 2;
115 if (nl_socket_set_buffer_size(ev->sock, ev->bufsize, 0))
116 goto abort;
117
118 /* Request full dump since some info got dropped */
119 struct rtgenmsg msg = { .rtgen_family = AF_UNSPEC };
120 nl_send_simple(ev->sock, RTM_GETLINK, NLM_F_DUMP, &msg, sizeof(msg));
121 break;
122
123 default:
124 goto abort;
125 }
126 u->error = false;
127 return;
128
129 abort:
130 uloop_fd_delete(&ev->uloop);
131 return;
132 }
133
134 static struct nl_sock *
135 create_socket(int protocol, int groups)
136 {
137 struct nl_sock *sock;
138
139 sock = nl_socket_alloc();
140 if (!sock)
141 return NULL;
142
143 if (groups)
144 nl_join_groups(sock, groups);
145
146 if (nl_connect(sock, protocol)) {
147 nl_socket_free(sock);
148 return NULL;
149 }
150
151 return sock;
152 }
153
154 static bool
155 create_raw_event_socket(struct event_socket *ev, int protocol, int groups,
156 uloop_fd_handler cb, int flags)
157 {
158 ev->sock = create_socket(protocol, groups);
159 if (!ev->sock)
160 return false;
161
162 ev->uloop.fd = nl_socket_get_fd(ev->sock);
163 ev->uloop.cb = cb;
164 if (uloop_fd_add(&ev->uloop, ULOOP_READ|flags))
165 return false;
166
167 return true;
168 }
169
170 static bool
171 create_event_socket(struct event_socket *ev, int protocol,
172 int (*cb)(struct nl_msg *msg, void *arg))
173 {
174 if (!create_raw_event_socket(ev, protocol, 0, handler_nl_event, ULOOP_ERROR_CB))
175 return false;
176
177 /* Install the valid custom callback handler */
178 nl_socket_modify_cb(ev->sock, NL_CB_VALID, NL_CB_CUSTOM, cb, NULL);
179
180 /* Disable sequence number checking on event sockets */
181 nl_socket_disable_seq_check(ev->sock);
182
183 /* Increase rx buffer size to 65K on event sockets */
184 ev->bufsize = 65535;
185 if (nl_socket_set_buffer_size(ev->sock, ev->bufsize, 0))
186 return false;
187
188 return true;
189 }
190
191 static bool
192 create_hotplug_event_socket(struct event_socket *ev, int protocol,
193 void (*cb)(struct uloop_fd *u, unsigned int events))
194 {
195 if (!create_raw_event_socket(ev, protocol, 1, cb, ULOOP_ERROR_CB))
196 return false;
197
198 /* Increase rx buffer size to 65K on event sockets */
199 ev->bufsize = 65535;
200 if (nl_socket_set_buffer_size(ev->sock, ev->bufsize, 0))
201 return false;
202
203 return true;
204 }
205
206 static bool
207 system_rtn_aton(const char *src, unsigned int *dst)
208 {
209 char *e;
210 unsigned int n;
211
212 if (!strcmp(src, "local"))
213 n = RTN_LOCAL;
214 else if (!strcmp(src, "nat"))
215 n = RTN_NAT;
216 else if (!strcmp(src, "broadcast"))
217 n = RTN_BROADCAST;
218 else if (!strcmp(src, "anycast"))
219 n = RTN_ANYCAST;
220 else if (!strcmp(src, "multicast"))
221 n = RTN_MULTICAST;
222 else if (!strcmp(src, "prohibit"))
223 n = RTN_PROHIBIT;
224 else if (!strcmp(src, "unreachable"))
225 n = RTN_UNREACHABLE;
226 else if (!strcmp(src, "blackhole"))
227 n = RTN_BLACKHOLE;
228 else if (!strcmp(src, "xresolve"))
229 n = RTN_XRESOLVE;
230 else if (!strcmp(src, "unicast"))
231 n = RTN_UNICAST;
232 else if (!strcmp(src, "throw"))
233 n = RTN_THROW;
234 else if (!strcmp(src, "failed_policy"))
235 n = RTN_FAILED_POLICY;
236 else {
237 n = strtoul(src, &e, 0);
238 if (!e || *e || e == src || n > 255)
239 return false;
240 }
241
242 *dst = n;
243 return true;
244 }
245
246 static bool
247 system_tos_aton(const char *src, unsigned *dst)
248 {
249 char *e;
250
251 *dst = strtoul(src, &e, 16);
252 if (e == src || *e || *dst > 255)
253 return false;
254
255 return true;
256 }
257
258 int system_init(void)
259 {
260 static struct event_socket rtnl_event;
261 static struct event_socket hotplug_event;
262
263 sock_ioctl = socket(AF_LOCAL, SOCK_DGRAM, 0);
264 system_fd_set_cloexec(sock_ioctl);
265
266 /* Prepare socket for routing / address control */
267 sock_rtnl = create_socket(NETLINK_ROUTE, 0);
268 if (!sock_rtnl)
269 return -1;
270
271 if (!create_event_socket(&rtnl_event, NETLINK_ROUTE, cb_rtnl_event))
272 return -1;
273
274 if (!create_hotplug_event_socket(&hotplug_event, NETLINK_KOBJECT_UEVENT,
275 handle_hotplug_event))
276 return -1;
277
278 /* Receive network link events form kernel */
279 nl_socket_add_membership(rtnl_event.sock, RTNLGRP_LINK);
280
281 return 0;
282 }
283
284 static void system_set_sysctl(const char *path, const char *val)
285 {
286 int fd;
287
288 fd = open(path, O_WRONLY);
289 if (fd < 0)
290 return;
291
292 if (write(fd, val, strlen(val))) {}
293 close(fd);
294 }
295
296 static void system_set_dev_sysctl(const char *path, const char *device, const char *val)
297 {
298 snprintf(dev_buf, sizeof(dev_buf), path, device);
299 system_set_sysctl(dev_buf, val);
300 }
301
302 static void system_set_disable_ipv6(struct device *dev, const char *val)
303 {
304 system_set_dev_sysctl("/proc/sys/net/ipv6/conf/%s/disable_ipv6", dev->ifname, val);
305 }
306
307 static void system_set_ip6segmentrouting(struct device *dev, const char *val)
308 {
309 system_set_dev_sysctl("/proc/sys/net/ipv6/conf/%s/seg6_enabled", dev->ifname, val);
310 }
311
312 static void system_set_rpfilter(struct device *dev, const char *val)
313 {
314 system_set_dev_sysctl("/proc/sys/net/ipv4/conf/%s/rp_filter", dev->ifname, val);
315 }
316
317 static void system_set_acceptlocal(struct device *dev, const char *val)
318 {
319 system_set_dev_sysctl("/proc/sys/net/ipv4/conf/%s/accept_local", dev->ifname, val);
320 }
321
322 static void system_set_igmpversion(struct device *dev, const char *val)
323 {
324 system_set_dev_sysctl("/proc/sys/net/ipv4/conf/%s/force_igmp_version", dev->ifname, val);
325 }
326
327 static void system_set_mldversion(struct device *dev, const char *val)
328 {
329 system_set_dev_sysctl("/proc/sys/net/ipv6/conf/%s/force_mld_version", dev->ifname, val);
330 }
331
332 static void system_set_neigh4reachabletime(struct device *dev, const char *val)
333 {
334 system_set_dev_sysctl("/proc/sys/net/ipv4/neigh/%s/base_reachable_time_ms", dev->ifname, val);
335 }
336
337 static void system_set_neigh6reachabletime(struct device *dev, const char *val)
338 {
339 system_set_dev_sysctl("/proc/sys/net/ipv6/neigh/%s/base_reachable_time_ms", dev->ifname, val);
340 }
341
342 static void system_set_neigh4gcstaletime(struct device *dev, const char *val)
343 {
344 system_set_dev_sysctl("/proc/sys/net/ipv4/neigh/%s/gc_stale_time", dev->ifname, val);
345 }
346
347 static void system_set_neigh6gcstaletime(struct device *dev, const char *val)
348 {
349 system_set_dev_sysctl("/proc/sys/net/ipv6/neigh/%s/gc_stale_time", dev->ifname, val);
350 }
351
352 static void system_set_neigh4locktime(struct device *dev, const char *val)
353 {
354 system_set_dev_sysctl("/proc/sys/net/ipv4/neigh/%s/locktime", dev->ifname, val);
355 }
356
357 static void system_set_dadtransmits(struct device *dev, const char *val)
358 {
359 system_set_dev_sysctl("/proc/sys/net/ipv6/conf/%s/dad_transmits", dev->ifname, val);
360 }
361
362 static void system_set_sendredirects(struct device *dev, const char *val)
363 {
364 system_set_dev_sysctl("/proc/sys/net/ipv4/conf/%s/send_redirects", dev->ifname, val);
365 }
366
367 static void system_set_drop_v4_unicast_in_l2_multicast(struct device *dev, const char *val)
368 {
369 system_set_dev_sysctl("/proc/sys/net/ipv4/conf/%s/drop_unicast_in_l2_multicast", dev->ifname, val);
370 }
371
372 static void system_set_drop_v6_unicast_in_l2_multicast(struct device *dev, const char *val)
373 {
374 system_set_dev_sysctl("/proc/sys/net/ipv6/conf/%s/drop_unicast_in_l2_multicast", dev->ifname, val);
375 }
376
377 static void system_set_drop_gratuitous_arp(struct device *dev, const char *val)
378 {
379 system_set_dev_sysctl("/proc/sys/net/ipv4/conf/%s/drop_gratuitous_arp", dev->ifname, val);
380 }
381
382 static void system_set_drop_unsolicited_na(struct device *dev, const char *val)
383 {
384 system_set_dev_sysctl("/proc/sys/net/ipv6/conf/%s/drop_unsolicited_na", dev->ifname, val);
385 }
386
387 static void system_set_arp_accept(struct device *dev, const char *val)
388 {
389 system_set_dev_sysctl("/proc/sys/net/ipv4/conf/%s/arp_accept", dev->ifname, val);
390 }
391
392 static void system_bridge_set_multicast_to_unicast(struct device *dev, const char *val)
393 {
394 system_set_dev_sysctl("/sys/class/net/%s/brport/multicast_to_unicast", dev->ifname, val);
395 }
396
397 static void system_bridge_set_multicast_fast_leave(struct device *dev, const char *val)
398 {
399 system_set_dev_sysctl("/sys/class/net/%s/brport/multicast_fast_leave", dev->ifname, val);
400 }
401
402 static void system_bridge_set_hairpin_mode(struct device *dev, const char *val)
403 {
404 system_set_dev_sysctl("/sys/class/net/%s/brport/hairpin_mode", dev->ifname, val);
405 }
406
407 static void system_bridge_set_isolated(struct device *dev, const char *val)
408 {
409 system_set_dev_sysctl("/sys/class/net/%s/brport/isolated", dev->ifname, val);
410 }
411
412 static void system_bridge_set_multicast_router(struct device *dev, const char *val, bool bridge)
413 {
414 system_set_dev_sysctl(bridge ? "/sys/class/net/%s/bridge/multicast_router" :
415 "/sys/class/net/%s/brport/multicast_router",
416 dev->ifname, val);
417 }
418
419 static void system_bridge_set_robustness(struct device *dev, const char *val)
420 {
421 system_set_dev_sysctl("/sys/devices/virtual/net/%s/bridge/multicast_startup_query_count",
422 dev->ifname, val);
423 system_set_dev_sysctl("/sys/devices/virtual/net/%s/bridge/multicast_last_member_count",
424 dev->ifname, val);
425 }
426
427 static void system_bridge_set_query_interval(struct device *dev, const char *val)
428 {
429 system_set_dev_sysctl("/sys/devices/virtual/net/%s/bridge/multicast_query_interval",
430 dev->ifname, val);
431 }
432
433 static void system_bridge_set_query_response_interval(struct device *dev, const char *val)
434 {
435 system_set_dev_sysctl("/sys/devices/virtual/net/%s/bridge/multicast_query_response_interval",
436 dev->ifname, val);
437 }
438
439 static void system_bridge_set_last_member_interval(struct device *dev, const char *val)
440 {
441 system_set_dev_sysctl("/sys/devices/virtual/net/%s/bridge/multicast_last_member_interval",
442 dev->ifname, val);
443 }
444
445 static void system_bridge_set_membership_interval(struct device *dev, const char *val)
446 {
447 system_set_dev_sysctl("/sys/devices/virtual/net/%s/bridge/multicast_membership_interval",
448 dev->ifname, val);
449 }
450
451 static void system_bridge_set_other_querier_timeout(struct device *dev, const char *val)
452 {
453 system_set_dev_sysctl("/sys/devices/virtual/net/%s/bridge/multicast_querier_interval",
454 dev->ifname, val);
455 }
456
457 static void system_bridge_set_startup_query_interval(struct device *dev, const char *val)
458 {
459 system_set_dev_sysctl("/sys/devices/virtual/net/%s/bridge/multicast_startup_query_interval",
460 dev->ifname, val);
461 }
462
463 static void system_bridge_set_stp_state(struct device *dev, const char *val)
464 {
465 system_set_dev_sysctl("/sys/devices/virtual/net/%s/bridge/stp_state", dev->ifname, val);
466 }
467
468 static void system_bridge_set_forward_delay(struct device *dev, const char *val)
469 {
470 system_set_dev_sysctl("/sys/devices/virtual/net/%s/bridge/forward_delay", dev->ifname, val);
471 }
472
473 static void system_bridge_set_priority(struct device *dev, const char *val)
474 {
475 system_set_dev_sysctl("/sys/devices/virtual/net/%s/bridge/priority", dev->ifname, val);
476 }
477
478 static void system_bridge_set_ageing_time(struct device *dev, const char *val)
479 {
480 system_set_dev_sysctl("/sys/devices/virtual/net/%s/bridge/ageing_time", dev->ifname, val);
481 }
482
483 static void system_bridge_set_hello_time(struct device *dev, const char *val)
484 {
485 system_set_dev_sysctl("/sys/devices/virtual/net/%s/bridge/hello_time", dev->ifname, val);
486 }
487
488 static void system_bridge_set_max_age(struct device *dev, const char *val)
489 {
490 system_set_dev_sysctl("/sys/devices/virtual/net/%s/bridge/max_age", dev->ifname, val);
491 }
492
493 static void system_bridge_set_learning(struct device *dev, const char *val)
494 {
495 system_set_dev_sysctl("/sys/class/net/%s/brport/learning", dev->ifname, val);
496 }
497
498 static void system_bridge_set_unicast_flood(struct device *dev, const char *val)
499 {
500 system_set_dev_sysctl("/sys/class/net/%s/brport/unicast_flood", dev->ifname, val);
501 }
502
503 static void system_bridge_set_vlan_filtering(struct device *dev, const char *val)
504 {
505 system_set_dev_sysctl("/sys/devices/virtual/net/%s/bridge/vlan_filtering", dev->ifname, val);
506 }
507
508 static int system_get_sysctl(const char *path, char *buf, const size_t buf_sz)
509 {
510 int fd = -1, ret = -1;
511
512 fd = open(path, O_RDONLY);
513 if (fd < 0)
514 goto out;
515
516 ssize_t len = read(fd, buf, buf_sz - 1);
517 if (len < 0)
518 goto out;
519
520 ret = buf[len] = 0;
521
522 out:
523 if (fd >= 0)
524 close(fd);
525
526 return ret;
527 }
528
529 static int
530 system_get_dev_sysctl(const char *path, const char *device, char *buf, const size_t buf_sz)
531 {
532 snprintf(dev_buf, sizeof(dev_buf), path, device);
533 return system_get_sysctl(dev_buf, buf, buf_sz);
534 }
535
536 static int system_get_disable_ipv6(struct device *dev, char *buf, const size_t buf_sz)
537 {
538 return system_get_dev_sysctl("/proc/sys/net/ipv6/conf/%s/disable_ipv6",
539 dev->ifname, buf, buf_sz);
540 }
541
542 static int system_get_ip6segmentrouting(struct device *dev, char *buf, const size_t buf_sz)
543 {
544 return system_get_dev_sysctl("/proc/sys/net/ipv6/conf/%s/seg6_enabled",
545 dev->ifname, buf, buf_sz);
546 }
547
548 static int system_get_rpfilter(struct device *dev, char *buf, const size_t buf_sz)
549 {
550 return system_get_dev_sysctl("/proc/sys/net/ipv4/conf/%s/rp_filter",
551 dev->ifname, buf, buf_sz);
552 }
553
554 static int system_get_acceptlocal(struct device *dev, char *buf, const size_t buf_sz)
555 {
556 return system_get_dev_sysctl("/proc/sys/net/ipv4/conf/%s/accept_local",
557 dev->ifname, buf, buf_sz);
558 }
559
560 static int system_get_igmpversion(struct device *dev, char *buf, const size_t buf_sz)
561 {
562 return system_get_dev_sysctl("/proc/sys/net/ipv4/conf/%s/force_igmp_version",
563 dev->ifname, buf, buf_sz);
564 }
565
566 static int system_get_mldversion(struct device *dev, char *buf, const size_t buf_sz)
567 {
568 return system_get_dev_sysctl("/proc/sys/net/ipv6/conf/%s/force_mld_version",
569 dev->ifname, buf, buf_sz);
570 }
571
572 static int system_get_neigh4reachabletime(struct device *dev, char *buf, const size_t buf_sz)
573 {
574 return system_get_dev_sysctl("/proc/sys/net/ipv4/neigh/%s/base_reachable_time_ms",
575 dev->ifname, buf, buf_sz);
576 }
577
578 static int system_get_neigh6reachabletime(struct device *dev, char *buf, const size_t buf_sz)
579 {
580 return system_get_dev_sysctl("/proc/sys/net/ipv6/neigh/%s/base_reachable_time_ms",
581 dev->ifname, buf, buf_sz);
582 }
583
584 static int system_get_neigh4gcstaletime(struct device *dev, char *buf, const size_t buf_sz)
585 {
586 return system_get_dev_sysctl("/proc/sys/net/ipv4/neigh/%s/gc_stale_time",
587 dev->ifname, buf, buf_sz);
588 }
589
590 static int system_get_neigh6gcstaletime(struct device *dev, char *buf, const size_t buf_sz)
591 {
592 return system_get_dev_sysctl("/proc/sys/net/ipv6/neigh/%s/gc_stale_time",
593 dev->ifname, buf, buf_sz);
594 }
595
596 static int system_get_neigh4locktime(struct device *dev, char *buf, const size_t buf_sz)
597 {
598 return system_get_dev_sysctl("/proc/sys/net/ipv4/neigh/%s/locktime",
599 dev->ifname, buf, buf_sz);
600 }
601
602 static int system_get_dadtransmits(struct device *dev, char *buf, const size_t buf_sz)
603 {
604 return system_get_dev_sysctl("/proc/sys/net/ipv6/conf/%s/dad_transmits",
605 dev->ifname, buf, buf_sz);
606 }
607
608 static int system_get_sendredirects(struct device *dev, char *buf, const size_t buf_sz)
609 {
610 return system_get_dev_sysctl("/proc/sys/net/ipv4/conf/%s/send_redirects",
611 dev->ifname, buf, buf_sz);
612 }
613
614
615 static int system_get_drop_v4_unicast_in_l2_multicast(struct device *dev, char *buf, const size_t buf_sz)
616 {
617 return system_get_dev_sysctl("/proc/sys/net/ipv4/conf/%s/drop_unicast_in_l2_multicast",
618 dev->ifname, buf, buf_sz);
619 }
620
621 static int system_get_drop_v6_unicast_in_l2_multicast(struct device *dev, char *buf, const size_t buf_sz)
622 {
623 return system_get_dev_sysctl("/proc/sys/net/ipv6/conf/%s/drop_unicast_in_l2_multicast",
624 dev->ifname, buf, buf_sz);
625 }
626
627 static int system_get_drop_gratuitous_arp(struct device *dev, char *buf, const size_t buf_sz)
628 {
629 return system_get_dev_sysctl("/proc/sys/net/ipv4/conf/%s/drop_gratuitous_arp",
630 dev->ifname, buf, buf_sz);
631 }
632
633 static int system_get_drop_unsolicited_na(struct device *dev, char *buf, const size_t buf_sz)
634 {
635 return system_get_dev_sysctl("/proc/sys/net/ipv6/conf/%s/drop_unsolicited_na",
636 dev->ifname, buf, buf_sz);
637 }
638
639 static int system_get_arp_accept(struct device *dev, char *buf, const size_t buf_sz)
640 {
641 return system_get_dev_sysctl("/proc/sys/net/ipv4/conf/%s/arp_accept",
642 dev->ifname, buf, buf_sz);
643 }
644
645 /* Evaluate netlink messages */
646 static int cb_rtnl_event(struct nl_msg *msg, void *arg)
647 {
648 struct nlmsghdr *nh = nlmsg_hdr(msg);
649 struct nlattr *nla[__IFLA_MAX];
650 int link_state = 0;
651 char buf[10];
652
653 if (nh->nlmsg_type != RTM_NEWLINK)
654 goto out;
655
656 nlmsg_parse(nh, sizeof(struct ifinfomsg), nla, __IFLA_MAX - 1, NULL);
657 if (!nla[IFLA_IFNAME])
658 goto out;
659
660 struct device *dev = device_find(nla_data(nla[IFLA_IFNAME]));
661 if (!dev)
662 goto out;
663
664 if (!system_get_dev_sysctl("/sys/class/net/%s/carrier", dev->ifname, buf, sizeof(buf)))
665 link_state = strtoul(buf, NULL, 0);
666
667 if (dev->type == &simple_device_type)
668 device_set_present(dev, true);
669
670 device_set_link(dev, link_state ? true : false);
671
672 out:
673 return 0;
674 }
675
676 static void
677 handle_hotplug_msg(char *data, int size)
678 {
679 const char *subsystem = NULL, *interface = NULL, *interface_old = NULL;
680 char *cur, *end, *sep;
681 int skip;
682 bool add;
683
684 if (!strncmp(data, "add@", 4) || !strncmp(data, "move@", 5))
685 add = true;
686 else if (!strncmp(data, "remove@", 7))
687 add = false;
688 else
689 return;
690
691 skip = strlen(data) + 1;
692 end = data + size;
693
694 for (cur = data + skip; cur < end; cur += skip) {
695 skip = strlen(cur) + 1;
696
697 sep = strchr(cur, '=');
698 if (!sep)
699 continue;
700
701 *sep = 0;
702 if (!strcmp(cur, "INTERFACE"))
703 interface = sep + 1;
704 else if (!strcmp(cur, "SUBSYSTEM")) {
705 subsystem = sep + 1;
706 if (strcmp(subsystem, "net") != 0)
707 return;
708 } else if (!strcmp(cur, "DEVPATH_OLD")) {
709 interface_old = strrchr(sep + 1, '/');
710 if (interface_old)
711 interface_old++;
712 }
713 }
714
715 if (!subsystem || !interface)
716 return;
717
718 if (interface_old)
719 device_hotplug_event(interface_old, false);
720
721 device_hotplug_event(interface, add);
722 }
723
724 static void
725 handle_hotplug_event(struct uloop_fd *u, unsigned int events)
726 {
727 struct event_socket *ev = container_of(u, struct event_socket, uloop);
728 struct sockaddr_nl nla;
729 unsigned char *buf = NULL;
730 int size;
731 int err;
732 socklen_t errlen = sizeof(err);
733
734 if (!u->error) {
735 while ((size = nl_recv(ev->sock, &nla, &buf, NULL)) > 0) {
736 if (nla.nl_pid == 0)
737 handle_hotplug_msg((char *) buf, size);
738
739 free(buf);
740 }
741 return;
742 }
743
744 if (getsockopt(u->fd, SOL_SOCKET, SO_ERROR, (void *)&err, &errlen))
745 goto abort;
746
747 switch(err) {
748 case ENOBUFS:
749 /* Increase rx buffer size on netlink socket */
750 ev->bufsize *= 2;
751 if (nl_socket_set_buffer_size(ev->sock, ev->bufsize, 0))
752 goto abort;
753 break;
754
755 default:
756 goto abort;
757 }
758 u->error = false;
759 return;
760
761 abort:
762 uloop_fd_delete(&ev->uloop);
763 return;
764 }
765
766 static int system_rtnl_call(struct nl_msg *msg)
767 {
768 int ret;
769
770 ret = nl_send_auto_complete(sock_rtnl, msg);
771 nlmsg_free(msg);
772
773 if (ret < 0)
774 return ret;
775
776 return nl_wait_for_ack(sock_rtnl);
777 }
778
779 int system_bridge_delbr(struct device *bridge)
780 {
781 return ioctl(sock_ioctl, SIOCBRDELBR, bridge->ifname);
782 }
783
784 static int system_bridge_if(const char *bridge, struct device *dev, int cmd, void *data)
785 {
786 struct ifreq ifr;
787
788 memset(&ifr, 0, sizeof(ifr));
789 if (dev)
790 ifr.ifr_ifindex = dev->ifindex;
791 else
792 ifr.ifr_data = data;
793 strncpy(ifr.ifr_name, bridge, sizeof(ifr.ifr_name) - 1);
794 return ioctl(sock_ioctl, cmd, &ifr);
795 }
796
797 static bool system_is_bridge(const char *name, char *buf, int buflen)
798 {
799 struct stat st;
800
801 snprintf(buf, buflen, "/sys/devices/virtual/net/%s/bridge", name);
802 if (stat(buf, &st) < 0)
803 return false;
804
805 return true;
806 }
807
808 static char *system_get_bridge(const char *name, char *buf, int buflen)
809 {
810 char *path;
811 ssize_t len = -1;
812 glob_t gl;
813
814 snprintf(buf, buflen, "/sys/devices/virtual/net/*/brif/%s/bridge", name);
815 if (glob(buf, GLOB_NOSORT, NULL, &gl) < 0)
816 return NULL;
817
818 if (gl.gl_pathc > 0)
819 len = readlink(gl.gl_pathv[0], buf, buflen);
820
821 globfree(&gl);
822
823 if (len < 0)
824 return NULL;
825
826 buf[len] = 0;
827 path = strrchr(buf, '/');
828 if (!path)
829 return NULL;
830
831 return path + 1;
832 }
833
834 static void
835 system_bridge_set_wireless(struct device *bridge, struct device *dev)
836 {
837 bool mcast_to_ucast = dev->wireless_ap;
838 bool hairpin = true;
839
840 if (bridge->settings.flags & DEV_OPT_MULTICAST_TO_UNICAST &&
841 !bridge->settings.multicast_to_unicast)
842 mcast_to_ucast = false;
843
844 if (!mcast_to_ucast || dev->wireless_isolate)
845 hairpin = false;
846
847 system_bridge_set_multicast_to_unicast(dev, mcast_to_ucast ? "1" : "0");
848 system_bridge_set_hairpin_mode(dev, hairpin ? "1" : "0");
849 }
850
851 int system_bridge_addif(struct device *bridge, struct device *dev)
852 {
853 char buf[64];
854 char *oldbr;
855 int tries = 0;
856 int ret;
857
858 retry:
859 ret = 0;
860 oldbr = system_get_bridge(dev->ifname, dev_buf, sizeof(dev_buf));
861 if (!oldbr || strcmp(oldbr, bridge->ifname) != 0) {
862 ret = system_bridge_if(bridge->ifname, dev, SIOCBRADDIF, NULL);
863 tries++;
864 D(SYSTEM, "Failed to add device '%s' to bridge '%s' (tries=%d): %s\n",
865 dev->ifname, bridge->ifname, tries, strerror(errno));
866 if (tries <= 3)
867 goto retry;
868 }
869
870 if (dev->wireless)
871 system_bridge_set_wireless(bridge, dev);
872
873 if (dev->settings.flags & DEV_OPT_MULTICAST_ROUTER) {
874 snprintf(buf, sizeof(buf), "%u", dev->settings.multicast_router);
875 system_bridge_set_multicast_router(dev, buf, false);
876 }
877
878 if (dev->settings.flags & DEV_OPT_MULTICAST_FAST_LEAVE &&
879 dev->settings.multicast_fast_leave)
880 system_bridge_set_multicast_fast_leave(dev, "1");
881
882 if (dev->settings.flags & DEV_OPT_LEARNING &&
883 !dev->settings.learning)
884 system_bridge_set_learning(dev, "0");
885
886 if (dev->settings.flags & DEV_OPT_UNICAST_FLOOD &&
887 !dev->settings.unicast_flood)
888 system_bridge_set_unicast_flood(dev, "0");
889
890 if (dev->settings.flags & DEV_OPT_ISOLATE &&
891 dev->settings.isolate)
892 system_bridge_set_isolated(dev, "1");
893
894 return ret;
895 }
896
897 int system_bridge_delif(struct device *bridge, struct device *dev)
898 {
899 return system_bridge_if(bridge->ifname, dev, SIOCBRDELIF, NULL);
900 }
901
902 int system_bridge_vlan(const char *iface, uint16_t vid, bool add, unsigned int vflags)
903 {
904 struct ifinfomsg ifi = { .ifi_family = PF_BRIDGE, };
905 struct bridge_vlan_info vinfo = { .vid = vid, };
906 unsigned short flags = 0;
907 struct nlattr *afspec;
908 struct nl_msg *nlm;
909 int ret = 0;
910
911 ifi.ifi_index = if_nametoindex(iface);
912 if (!ifi.ifi_index)
913 return -1;
914
915 nlm = nlmsg_alloc_simple(add ? RTM_SETLINK : RTM_DELLINK, NLM_F_REQUEST);
916 if (!nlm)
917 return -1;
918
919 nlmsg_append(nlm, &ifi, sizeof(ifi), 0);
920
921 if (vflags & BRVLAN_F_SELF)
922 flags |= BRIDGE_FLAGS_SELF;
923
924 if (vflags & BRVLAN_F_PVID)
925 vinfo.flags |= BRIDGE_VLAN_INFO_PVID;
926
927 if (vflags & BRVLAN_F_UNTAGGED)
928 vinfo.flags |= BRIDGE_VLAN_INFO_UNTAGGED;
929
930 afspec = nla_nest_start(nlm, IFLA_AF_SPEC);
931 if (!afspec) {
932 ret = -ENOMEM;
933 goto failure;
934 }
935
936 if (flags)
937 nla_put_u16(nlm, IFLA_BRIDGE_FLAGS, flags);
938
939 nla_put(nlm, IFLA_BRIDGE_VLAN_INFO, sizeof(vinfo), &vinfo);
940 nla_nest_end(nlm, afspec);
941
942 return system_rtnl_call(nlm);
943
944 failure:
945 nlmsg_free(nlm);
946 return ret;
947 }
948
949 int system_if_resolve(struct device *dev)
950 {
951 struct ifreq ifr;
952
953 memset(&ifr, 0, sizeof(ifr));
954 strncpy(ifr.ifr_name, dev->ifname, sizeof(ifr.ifr_name) - 1);
955 if (!ioctl(sock_ioctl, SIOCGIFINDEX, &ifr))
956 return ifr.ifr_ifindex;
957 else
958 return 0;
959 }
960
961 static int system_if_flags(const char *ifname, unsigned add, unsigned rem)
962 {
963 struct ifreq ifr;
964
965 memset(&ifr, 0, sizeof(ifr));
966 strncpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name) - 1);
967 if (ioctl(sock_ioctl, SIOCGIFFLAGS, &ifr) < 0)
968 return -1;
969
970 ifr.ifr_flags |= add;
971 ifr.ifr_flags &= ~rem;
972 return ioctl(sock_ioctl, SIOCSIFFLAGS, &ifr);
973 }
974
975 struct clear_data {
976 struct nl_msg *msg;
977 struct device *dev;
978 int type;
979 int size;
980 int af;
981 };
982
983
984 static bool check_ifaddr(struct nlmsghdr *hdr, int ifindex)
985 {
986 struct ifaddrmsg *ifa = NLMSG_DATA(hdr);
987
988 return ifa->ifa_index == ifindex;
989 }
990
991 static bool check_route(struct nlmsghdr *hdr, int ifindex)
992 {
993 struct rtmsg *r = NLMSG_DATA(hdr);
994 struct nlattr *tb[__RTA_MAX];
995
996 if (r->rtm_protocol == RTPROT_KERNEL &&
997 r->rtm_family == AF_INET6)
998 return false;
999
1000 nlmsg_parse(hdr, sizeof(struct rtmsg), tb, __RTA_MAX - 1, NULL);
1001 if (!tb[RTA_OIF])
1002 return false;
1003
1004 return *(int *)RTA_DATA(tb[RTA_OIF]) == ifindex;
1005 }
1006
1007 static bool check_rule(struct nlmsghdr *hdr, int ifindex)
1008 {
1009 return true;
1010 }
1011
1012 static int cb_clear_event(struct nl_msg *msg, void *arg)
1013 {
1014 struct clear_data *clr = arg;
1015 struct nlmsghdr *hdr = nlmsg_hdr(msg);
1016 bool (*cb)(struct nlmsghdr *, int ifindex);
1017 int type, ret;
1018
1019 switch(clr->type) {
1020 case RTM_GETADDR:
1021 type = RTM_DELADDR;
1022 if (hdr->nlmsg_type != RTM_NEWADDR)
1023 return NL_SKIP;
1024
1025 cb = check_ifaddr;
1026 break;
1027 case RTM_GETROUTE:
1028 type = RTM_DELROUTE;
1029 if (hdr->nlmsg_type != RTM_NEWROUTE)
1030 return NL_SKIP;
1031
1032 cb = check_route;
1033 break;
1034 case RTM_GETRULE:
1035 type = RTM_DELRULE;
1036 if (hdr->nlmsg_type != RTM_NEWRULE)
1037 return NL_SKIP;
1038
1039 cb = check_rule;
1040 break;
1041 default:
1042 return NL_SKIP;
1043 }
1044
1045 if (!cb(hdr, clr->dev ? clr->dev->ifindex : 0))
1046 return NL_SKIP;
1047
1048 if (type == RTM_DELRULE)
1049 D(SYSTEM, "Remove a rule\n");
1050 else
1051 D(SYSTEM, "Remove %s from device %s\n",
1052 type == RTM_DELADDR ? "an address" : "a route",
1053 clr->dev->ifname);
1054
1055 memcpy(nlmsg_hdr(clr->msg), hdr, hdr->nlmsg_len);
1056 hdr = nlmsg_hdr(clr->msg);
1057 hdr->nlmsg_type = type;
1058 hdr->nlmsg_flags = NLM_F_REQUEST;
1059
1060 nl_socket_disable_auto_ack(sock_rtnl);
1061 ret = nl_send_auto_complete(sock_rtnl, clr->msg);
1062 if (ret < 0) {
1063 if (type == RTM_DELRULE)
1064 D(SYSTEM, "Error deleting a rule: %d\n", ret);
1065 else
1066 D(SYSTEM, "Error deleting %s from device '%s': %d\n",
1067 type == RTM_DELADDR ? "an address" : "a route",
1068 clr->dev->ifname, ret);
1069 }
1070
1071 nl_socket_enable_auto_ack(sock_rtnl);
1072
1073 return NL_SKIP;
1074 }
1075
1076 static int
1077 cb_finish_event(struct nl_msg *msg, void *arg)
1078 {
1079 int *pending = arg;
1080 *pending = 0;
1081 return NL_STOP;
1082 }
1083
1084 static int
1085 error_handler(struct sockaddr_nl *nla, struct nlmsgerr *err, void *arg)
1086 {
1087 int *pending = arg;
1088 *pending = err->error;
1089 return NL_STOP;
1090 }
1091
1092 static void
1093 system_if_clear_entries(struct device *dev, int type, int af)
1094 {
1095 struct clear_data clr;
1096 struct nl_cb *cb;
1097 struct rtmsg rtm = {
1098 .rtm_family = af,
1099 .rtm_flags = RTM_F_CLONED,
1100 };
1101 int flags = NLM_F_DUMP;
1102 int pending = 1;
1103
1104 clr.af = af;
1105 clr.dev = dev;
1106 clr.type = type;
1107 switch (type) {
1108 case RTM_GETADDR:
1109 case RTM_GETRULE:
1110 clr.size = sizeof(struct rtgenmsg);
1111 break;
1112 case RTM_GETROUTE:
1113 clr.size = sizeof(struct rtmsg);
1114 break;
1115 default:
1116 return;
1117 }
1118
1119 cb = nl_cb_alloc(NL_CB_DEFAULT);
1120 if (!cb)
1121 return;
1122
1123 clr.msg = nlmsg_alloc_simple(type, flags);
1124 if (!clr.msg)
1125 goto out;
1126
1127 nlmsg_append(clr.msg, &rtm, clr.size, 0);
1128 nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM, cb_clear_event, &clr);
1129 nl_cb_set(cb, NL_CB_FINISH, NL_CB_CUSTOM, cb_finish_event, &pending);
1130 nl_cb_err(cb, NL_CB_CUSTOM, error_handler, &pending);
1131
1132 if (nl_send_auto_complete(sock_rtnl, clr.msg) < 0)
1133 goto free;
1134
1135 while (pending > 0)
1136 nl_recvmsgs(sock_rtnl, cb);
1137
1138 free:
1139 nlmsg_free(clr.msg);
1140 out:
1141 nl_cb_put(cb);
1142 }
1143
1144 /*
1145 * Clear bridge (membership) state and bring down device
1146 */
1147 void system_if_clear_state(struct device *dev)
1148 {
1149 static char buf[256];
1150 char *bridge;
1151 device_set_ifindex(dev, system_if_resolve(dev));
1152
1153 if (dev->external || !dev->ifindex)
1154 return;
1155
1156 system_if_flags(dev->ifname, 0, IFF_UP);
1157
1158 if (system_is_bridge(dev->ifname, buf, sizeof(buf))) {
1159 D(SYSTEM, "Delete existing bridge named '%s'\n", dev->ifname);
1160 system_bridge_delbr(dev);
1161 return;
1162 }
1163
1164 bridge = system_get_bridge(dev->ifname, buf, sizeof(buf));
1165 if (bridge) {
1166 D(SYSTEM, "Remove device '%s' from bridge '%s'\n", dev->ifname, bridge);
1167 system_bridge_if(bridge, dev, SIOCBRDELIF, NULL);
1168 }
1169
1170 system_if_clear_entries(dev, RTM_GETROUTE, AF_INET);
1171 system_if_clear_entries(dev, RTM_GETADDR, AF_INET);
1172 system_if_clear_entries(dev, RTM_GETROUTE, AF_INET6);
1173 system_if_clear_entries(dev, RTM_GETADDR, AF_INET6);
1174 system_if_clear_entries(dev, RTM_GETNEIGH, AF_INET);
1175 system_if_clear_entries(dev, RTM_GETNEIGH, AF_INET6);
1176 system_set_disable_ipv6(dev, "0");
1177 }
1178
1179 static inline unsigned long
1180 sec_to_jiffies(int val)
1181 {
1182 return (unsigned long) val * 100;
1183 }
1184
1185 static void system_bridge_conf_multicast_deps(struct device *bridge,
1186 struct bridge_config *cfg,
1187 char *buf,
1188 int buf_len)
1189 {
1190 int val;
1191
1192 if (cfg->flags & BRIDGE_OPT_ROBUSTNESS ||
1193 cfg->flags & BRIDGE_OPT_QUERY_INTERVAL ||
1194 cfg->flags & BRIDGE_OPT_QUERY_RESPONSE_INTERVAL) {
1195 val = cfg->robustness * cfg->query_interval +
1196 cfg->query_response_interval;
1197
1198 snprintf(buf, buf_len, "%i", val);
1199 system_bridge_set_membership_interval(bridge, buf);
1200
1201 val = cfg->robustness * cfg->query_interval +
1202 cfg->query_response_interval / 2;
1203
1204 snprintf(buf, buf_len, "%i", val);
1205 system_bridge_set_other_querier_timeout(bridge, buf);
1206 }
1207
1208 if (cfg->flags & BRIDGE_OPT_QUERY_INTERVAL) {
1209 val = cfg->query_interval / 4;
1210
1211 snprintf(buf, buf_len, "%i", val);
1212 system_bridge_set_startup_query_interval(bridge, buf);
1213 }
1214 }
1215
1216 static void system_bridge_conf_multicast(struct device *bridge,
1217 struct bridge_config *cfg,
1218 char *buf,
1219 int buf_len)
1220 {
1221 system_set_dev_sysctl("/sys/devices/virtual/net/%s/bridge/multicast_snooping",
1222 bridge->ifname, cfg->igmp_snoop ? "1" : "0");
1223
1224 system_set_dev_sysctl("/sys/devices/virtual/net/%s/bridge/multicast_querier",
1225 bridge->ifname, cfg->multicast_querier ? "1" : "0");
1226
1227 snprintf(buf, buf_len, "%i", cfg->hash_max);
1228 system_set_dev_sysctl("/sys/devices/virtual/net/%s/bridge/hash_max",
1229 bridge->ifname, buf);
1230
1231 if (bridge->settings.flags & DEV_OPT_MULTICAST_ROUTER) {
1232 snprintf(buf, buf_len, "%u", bridge->settings.multicast_router);
1233 system_bridge_set_multicast_router(bridge, buf, true);
1234 }
1235
1236 if (cfg->flags & BRIDGE_OPT_ROBUSTNESS) {
1237 snprintf(buf, buf_len, "%i", cfg->robustness);
1238 system_bridge_set_robustness(bridge, buf);
1239 }
1240
1241 if (cfg->flags & BRIDGE_OPT_QUERY_INTERVAL) {
1242 snprintf(buf, buf_len, "%i", cfg->query_interval);
1243 system_bridge_set_query_interval(bridge, buf);
1244 }
1245
1246 if (cfg->flags & BRIDGE_OPT_QUERY_RESPONSE_INTERVAL) {
1247 snprintf(buf, buf_len, "%i", cfg->query_response_interval);
1248 system_bridge_set_query_response_interval(bridge, buf);
1249 }
1250
1251 if (cfg->flags & BRIDGE_OPT_LAST_MEMBER_INTERVAL) {
1252 snprintf(buf, buf_len, "%i", cfg->last_member_interval);
1253 system_bridge_set_last_member_interval(bridge, buf);
1254 }
1255
1256 system_bridge_conf_multicast_deps(bridge, cfg, buf, buf_len);
1257 }
1258
1259 int system_bridge_addbr(struct device *bridge, struct bridge_config *cfg)
1260 {
1261 char buf[64];
1262
1263 if (ioctl(sock_ioctl, SIOCBRADDBR, bridge->ifname) < 0)
1264 return -1;
1265
1266 system_bridge_set_stp_state(bridge, cfg->stp ? "1" : "0");
1267
1268 snprintf(buf, sizeof(buf), "%lu", sec_to_jiffies(cfg->forward_delay));
1269 system_bridge_set_forward_delay(bridge, buf);
1270
1271 system_bridge_conf_multicast(bridge, cfg, buf, sizeof(buf));
1272 system_bridge_set_vlan_filtering(bridge, cfg->vlan_filtering ? "1" : "0");
1273
1274 snprintf(buf, sizeof(buf), "%d", cfg->priority);
1275 system_bridge_set_priority(bridge, buf);
1276
1277 if (cfg->flags & BRIDGE_OPT_AGEING_TIME) {
1278 snprintf(buf, sizeof(buf), "%lu", sec_to_jiffies(cfg->ageing_time));
1279 system_bridge_set_ageing_time(bridge, buf);
1280 }
1281
1282 if (cfg->flags & BRIDGE_OPT_HELLO_TIME) {
1283 snprintf(buf, sizeof(buf), "%lu", sec_to_jiffies(cfg->hello_time));
1284 system_bridge_set_hello_time(bridge, buf);
1285 }
1286
1287 if (cfg->flags & BRIDGE_OPT_MAX_AGE) {
1288 snprintf(buf, sizeof(buf), "%lu", sec_to_jiffies(cfg->max_age));
1289 system_bridge_set_max_age(bridge, buf);
1290 }
1291
1292 return 0;
1293 }
1294
1295 int system_macvlan_add(struct device *macvlan, struct device *dev, struct macvlan_config *cfg)
1296 {
1297 struct nl_msg *msg;
1298 struct nlattr *linkinfo, *data;
1299 struct ifinfomsg iim = { .ifi_family = AF_UNSPEC, };
1300 int i, rv;
1301 static const struct {
1302 const char *name;
1303 enum macvlan_mode val;
1304 } modes[] = {
1305 { "private", MACVLAN_MODE_PRIVATE },
1306 { "vepa", MACVLAN_MODE_VEPA },
1307 { "bridge", MACVLAN_MODE_BRIDGE },
1308 { "passthru", MACVLAN_MODE_PASSTHRU },
1309 };
1310
1311 msg = nlmsg_alloc_simple(RTM_NEWLINK, NLM_F_REQUEST | NLM_F_CREATE | NLM_F_EXCL);
1312
1313 if (!msg)
1314 return -1;
1315
1316 nlmsg_append(msg, &iim, sizeof(iim), 0);
1317
1318 if (cfg->flags & MACVLAN_OPT_MACADDR)
1319 nla_put(msg, IFLA_ADDRESS, sizeof(cfg->macaddr), cfg->macaddr);
1320 nla_put_string(msg, IFLA_IFNAME, macvlan->ifname);
1321 nla_put_u32(msg, IFLA_LINK, dev->ifindex);
1322
1323 if (!(linkinfo = nla_nest_start(msg, IFLA_LINKINFO)))
1324 goto nla_put_failure;
1325
1326 nla_put_string(msg, IFLA_INFO_KIND, "macvlan");
1327
1328 if (!(data = nla_nest_start(msg, IFLA_INFO_DATA)))
1329 goto nla_put_failure;
1330
1331 if (cfg->mode) {
1332 for (i = 0; i < ARRAY_SIZE(modes); i++) {
1333 if (strcmp(cfg->mode, modes[i].name) != 0)
1334 continue;
1335
1336 nla_put_u32(msg, IFLA_MACVLAN_MODE, modes[i].val);
1337 break;
1338 }
1339 }
1340
1341 nla_nest_end(msg, data);
1342 nla_nest_end(msg, linkinfo);
1343
1344 rv = system_rtnl_call(msg);
1345 if (rv)
1346 D(SYSTEM, "Error adding macvlan '%s' over '%s': %d\n", macvlan->ifname, dev->ifname, rv);
1347
1348 return rv;
1349
1350 nla_put_failure:
1351 nlmsg_free(msg);
1352 return -ENOMEM;
1353 }
1354
1355 int system_link_netns_move(struct device *dev, int netns_fd, const char *target_ifname)
1356 {
1357 struct nl_msg *msg;
1358 struct ifinfomsg iim = {
1359 .ifi_family = AF_UNSPEC,
1360 };
1361
1362 if (!dev)
1363 return -1;
1364
1365 iim.ifi_index = system_if_resolve(dev);
1366 msg = nlmsg_alloc_simple(RTM_NEWLINK, NLM_F_REQUEST);
1367
1368 if (!msg)
1369 return -1;
1370
1371 nlmsg_append(msg, &iim, sizeof(iim), 0);
1372 if (target_ifname)
1373 nla_put_string(msg, IFLA_IFNAME, target_ifname);
1374
1375 nla_put_u32(msg, IFLA_NET_NS_FD, netns_fd);
1376 return system_rtnl_call(msg);
1377 }
1378
1379 static int system_link_del(const char *ifname)
1380 {
1381 struct nl_msg *msg;
1382 struct ifinfomsg iim = {
1383 .ifi_family = AF_UNSPEC,
1384 .ifi_index = 0,
1385 };
1386
1387 msg = nlmsg_alloc_simple(RTM_DELLINK, NLM_F_REQUEST);
1388
1389 if (!msg)
1390 return -1;
1391
1392 nlmsg_append(msg, &iim, sizeof(iim), 0);
1393 nla_put_string(msg, IFLA_IFNAME, ifname);
1394 return system_rtnl_call(msg);
1395 }
1396
1397 int system_macvlan_del(struct device *macvlan)
1398 {
1399 return system_link_del(macvlan->ifname);
1400 }
1401
1402 int system_netns_open(const pid_t target_ns)
1403 {
1404 char pid_net_path[PATH_MAX];
1405
1406 snprintf(pid_net_path, sizeof(pid_net_path), "/proc/%u/ns/net", target_ns);
1407
1408 return open(pid_net_path, O_RDONLY);
1409 }
1410
1411 int system_netns_set(int netns_fd)
1412 {
1413 return setns(netns_fd, CLONE_NEWNET);
1414 }
1415
1416 int system_veth_add(struct device *veth, struct veth_config *cfg)
1417 {
1418 struct nl_msg *msg;
1419 struct ifinfomsg empty_iim = {};
1420 struct nlattr *linkinfo, *data, *veth_info;
1421 int rv;
1422
1423 msg = nlmsg_alloc_simple(RTM_NEWLINK, NLM_F_REQUEST | NLM_F_CREATE | NLM_F_EXCL);
1424
1425 if (!msg)
1426 return -1;
1427
1428 nlmsg_append(msg, &empty_iim, sizeof(empty_iim), 0);
1429
1430 if (cfg->flags & VETH_OPT_MACADDR)
1431 nla_put(msg, IFLA_ADDRESS, sizeof(cfg->macaddr), cfg->macaddr);
1432 nla_put_string(msg, IFLA_IFNAME, veth->ifname);
1433
1434 if (!(linkinfo = nla_nest_start(msg, IFLA_LINKINFO)))
1435 goto nla_put_failure;
1436
1437 nla_put_string(msg, IFLA_INFO_KIND, "veth");
1438
1439 if (!(data = nla_nest_start(msg, IFLA_INFO_DATA)))
1440 goto nla_put_failure;
1441
1442 if (!(veth_info = nla_nest_start(msg, VETH_INFO_PEER)))
1443 goto nla_put_failure;
1444
1445 nlmsg_append(msg, &empty_iim, sizeof(empty_iim), 0);
1446
1447 if (cfg->flags & VETH_OPT_PEER_NAME)
1448 nla_put_string(msg, IFLA_IFNAME, cfg->peer_name);
1449 if (cfg->flags & VETH_OPT_PEER_MACADDR)
1450 nla_put(msg, IFLA_ADDRESS, sizeof(cfg->peer_macaddr), cfg->peer_macaddr);
1451
1452 nla_nest_end(msg, veth_info);
1453 nla_nest_end(msg, data);
1454 nla_nest_end(msg, linkinfo);
1455
1456 rv = system_rtnl_call(msg);
1457 if (rv) {
1458 if (cfg->flags & VETH_OPT_PEER_NAME)
1459 D(SYSTEM, "Error adding veth '%s' with peer '%s': %d\n", veth->ifname, cfg->peer_name, rv);
1460 else
1461 D(SYSTEM, "Error adding veth '%s': %d\n", veth->ifname, rv);
1462 }
1463
1464 return rv;
1465
1466 nla_put_failure:
1467 nlmsg_free(msg);
1468 return -ENOMEM;
1469 }
1470
1471 int system_veth_del(struct device *veth)
1472 {
1473 return system_link_del(veth->ifname);
1474 }
1475
1476 static int system_vlan(struct device *dev, int id)
1477 {
1478 struct vlan_ioctl_args ifr = {
1479 .cmd = SET_VLAN_NAME_TYPE_CMD,
1480 .u.name_type = VLAN_NAME_TYPE_RAW_PLUS_VID_NO_PAD,
1481 };
1482
1483 if (ioctl(sock_ioctl, SIOCSIFVLAN, &ifr) < 0)
1484 return -1;
1485
1486 if (id < 0) {
1487 ifr.cmd = DEL_VLAN_CMD;
1488 ifr.u.VID = 0;
1489 } else {
1490 ifr.cmd = ADD_VLAN_CMD;
1491 ifr.u.VID = id;
1492 }
1493 strncpy(ifr.device1, dev->ifname, sizeof(ifr.device1));
1494 return ioctl(sock_ioctl, SIOCSIFVLAN, &ifr);
1495 }
1496
1497 int system_vlan_add(struct device *dev, int id)
1498 {
1499 return system_vlan(dev, id);
1500 }
1501
1502 int system_vlan_del(struct device *dev)
1503 {
1504 return system_vlan(dev, -1);
1505 }
1506
1507 int system_vlandev_add(struct device *vlandev, struct device *dev, struct vlandev_config *cfg)
1508 {
1509 struct nl_msg *msg;
1510 struct nlattr *linkinfo, *data, *qos;
1511 struct ifinfomsg iim = { .ifi_family = AF_UNSPEC };
1512 struct vlan_qos_mapping *elem;
1513 struct ifla_vlan_qos_mapping nl_qos_map;
1514 int rv;
1515
1516 msg = nlmsg_alloc_simple(RTM_NEWLINK, NLM_F_REQUEST | NLM_F_CREATE | NLM_F_EXCL);
1517
1518 if (!msg)
1519 return -1;
1520
1521 nlmsg_append(msg, &iim, sizeof(iim), 0);
1522 nla_put_string(msg, IFLA_IFNAME, vlandev->ifname);
1523 nla_put_u32(msg, IFLA_LINK, dev->ifindex);
1524
1525 if (!(linkinfo = nla_nest_start(msg, IFLA_LINKINFO)))
1526 goto nla_put_failure;
1527
1528 nla_put_string(msg, IFLA_INFO_KIND, "vlan");
1529
1530 if (!(data = nla_nest_start(msg, IFLA_INFO_DATA)))
1531 goto nla_put_failure;
1532
1533 nla_put_u16(msg, IFLA_VLAN_ID, cfg->vid);
1534
1535 #if LINUX_VERSION_CODE >= KERNEL_VERSION(3,10,0)
1536 nla_put_u16(msg, IFLA_VLAN_PROTOCOL, htons(cfg->proto));
1537 #else
1538 if(cfg->proto == VLAN_PROTO_8021AD)
1539 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);
1540 #endif
1541
1542 if (!(qos = nla_nest_start(msg, IFLA_VLAN_INGRESS_QOS)))
1543 goto nla_put_failure;
1544
1545 vlist_simple_for_each_element(&cfg->ingress_qos_mapping_list, elem, node) {
1546 nl_qos_map.from = elem->from;
1547 nl_qos_map.to = elem->to;
1548 nla_put(msg, IFLA_VLAN_QOS_MAPPING, sizeof(nl_qos_map), &nl_qos_map);
1549 }
1550 nla_nest_end(msg, qos);
1551
1552 if (!(qos = nla_nest_start(msg, IFLA_VLAN_EGRESS_QOS)))
1553 goto nla_put_failure;
1554
1555 vlist_simple_for_each_element(&cfg->egress_qos_mapping_list, elem, node) {
1556 nl_qos_map.from = elem->from;
1557 nl_qos_map.to = elem->to;
1558 nla_put(msg, IFLA_VLAN_QOS_MAPPING, sizeof(nl_qos_map), &nl_qos_map);
1559 }
1560 nla_nest_end(msg, qos);
1561
1562 nla_nest_end(msg, data);
1563 nla_nest_end(msg, linkinfo);
1564
1565 rv = system_rtnl_call(msg);
1566 if (rv)
1567 D(SYSTEM, "Error adding vlandev '%s' over '%s': %d\n", vlandev->ifname, dev->ifname, rv);
1568
1569 return rv;
1570
1571 nla_put_failure:
1572 nlmsg_free(msg);
1573 return -ENOMEM;
1574 }
1575
1576 int system_vlandev_del(struct device *vlandev)
1577 {
1578 return system_link_del(vlandev->ifname);
1579 }
1580
1581 void
1582 system_if_get_settings(struct device *dev, struct device_settings *s)
1583 {
1584 struct ifreq ifr;
1585 char buf[10];
1586
1587 memset(&ifr, 0, sizeof(ifr));
1588 strncpy(ifr.ifr_name, dev->ifname, sizeof(ifr.ifr_name) - 1);
1589
1590 if (ioctl(sock_ioctl, SIOCGIFMTU, &ifr) == 0) {
1591 s->mtu = ifr.ifr_mtu;
1592 s->flags |= DEV_OPT_MTU;
1593 }
1594
1595 s->mtu6 = system_update_ipv6_mtu(dev, 0);
1596 if (s->mtu6 > 0)
1597 s->flags |= DEV_OPT_MTU6;
1598
1599 if (ioctl(sock_ioctl, SIOCGIFTXQLEN, &ifr) == 0) {
1600 s->txqueuelen = ifr.ifr_qlen;
1601 s->flags |= DEV_OPT_TXQUEUELEN;
1602 }
1603
1604 if (ioctl(sock_ioctl, SIOCGIFHWADDR, &ifr) == 0) {
1605 memcpy(s->macaddr, &ifr.ifr_hwaddr.sa_data, sizeof(s->macaddr));
1606 s->flags |= DEV_OPT_MACADDR;
1607 }
1608
1609 if (!system_get_disable_ipv6(dev, buf, sizeof(buf))) {
1610 s->ipv6 = !strtoul(buf, NULL, 0);
1611 s->flags |= DEV_OPT_IPV6;
1612 }
1613
1614 if (!system_get_ip6segmentrouting(dev, buf, sizeof(buf))) {
1615 s->ip6segmentrouting = strtoul(buf, NULL, 0);
1616 s->flags |= DEV_OPT_IP6SEGMENTROUTING;
1617 }
1618
1619 if (ioctl(sock_ioctl, SIOCGIFFLAGS, &ifr) == 0) {
1620 s->promisc = ifr.ifr_flags & IFF_PROMISC;
1621 s->flags |= DEV_OPT_PROMISC;
1622
1623 s->multicast = ifr.ifr_flags & IFF_MULTICAST;
1624 s->flags |= DEV_OPT_MULTICAST;
1625 }
1626
1627 if (!system_get_rpfilter(dev, buf, sizeof(buf))) {
1628 s->rpfilter = strtoul(buf, NULL, 0);
1629 s->flags |= DEV_OPT_RPFILTER;
1630 }
1631
1632 if (!system_get_acceptlocal(dev, buf, sizeof(buf))) {
1633 s->acceptlocal = strtoul(buf, NULL, 0);
1634 s->flags |= DEV_OPT_ACCEPTLOCAL;
1635 }
1636
1637 if (!system_get_igmpversion(dev, buf, sizeof(buf))) {
1638 s->igmpversion = strtoul(buf, NULL, 0);
1639 s->flags |= DEV_OPT_IGMPVERSION;
1640 }
1641
1642 if (!system_get_mldversion(dev, buf, sizeof(buf))) {
1643 s->mldversion = strtoul(buf, NULL, 0);
1644 s->flags |= DEV_OPT_MLDVERSION;
1645 }
1646
1647 if (!system_get_neigh4reachabletime(dev, buf, sizeof(buf))) {
1648 s->neigh4reachabletime = strtoul(buf, NULL, 0);
1649 s->flags |= DEV_OPT_NEIGHREACHABLETIME;
1650 }
1651
1652 if (!system_get_neigh6reachabletime(dev, buf, sizeof(buf))) {
1653 s->neigh6reachabletime = strtoul(buf, NULL, 0);
1654 s->flags |= DEV_OPT_NEIGHREACHABLETIME;
1655 }
1656
1657 if (!system_get_neigh4locktime(dev, buf, sizeof(buf))) {
1658 s->neigh4locktime = strtol(buf, NULL, 0);
1659 s->flags |= DEV_OPT_NEIGHLOCKTIME;
1660 }
1661
1662 if (!system_get_neigh4gcstaletime(dev, buf, sizeof(buf))) {
1663 s->neigh4gcstaletime = strtoul(buf, NULL, 0);
1664 s->flags |= DEV_OPT_NEIGHGCSTALETIME;
1665 }
1666
1667 if (!system_get_neigh6gcstaletime(dev, buf, sizeof(buf))) {
1668 s->neigh6gcstaletime = strtoul(buf, NULL, 0);
1669 s->flags |= DEV_OPT_NEIGHGCSTALETIME;
1670 }
1671
1672 if (!system_get_dadtransmits(dev, buf, sizeof(buf))) {
1673 s->dadtransmits = strtoul(buf, NULL, 0);
1674 s->flags |= DEV_OPT_DADTRANSMITS;
1675 }
1676
1677 if (!system_get_sendredirects(dev, buf, sizeof(buf))) {
1678 s->sendredirects = strtoul(buf, NULL, 0);
1679 s->flags |= DEV_OPT_SENDREDIRECTS;
1680 }
1681
1682 if (!system_get_drop_v4_unicast_in_l2_multicast(dev, buf, sizeof(buf))) {
1683 s->drop_v4_unicast_in_l2_multicast = strtoul(buf, NULL, 0);
1684 s->flags |= DEV_OPT_DROP_V4_UNICAST_IN_L2_MULTICAST;
1685 }
1686
1687 if (!system_get_drop_v6_unicast_in_l2_multicast(dev, buf, sizeof(buf))) {
1688 s->drop_v6_unicast_in_l2_multicast = strtoul(buf, NULL, 0);
1689 s->flags |= DEV_OPT_DROP_V6_UNICAST_IN_L2_MULTICAST;
1690 }
1691
1692 if (!system_get_drop_gratuitous_arp(dev, buf, sizeof(buf))) {
1693 s->drop_gratuitous_arp = strtoul(buf, NULL, 0);
1694 s->flags |= DEV_OPT_DROP_GRATUITOUS_ARP;
1695 }
1696
1697 if (!system_get_drop_unsolicited_na(dev, buf, sizeof(buf))) {
1698 s->drop_unsolicited_na = strtoul(buf, NULL, 0);
1699 s->flags |= DEV_OPT_DROP_UNSOLICITED_NA;
1700 }
1701
1702 if (!system_get_arp_accept(dev, buf, sizeof(buf))) {
1703 s->arp_accept = strtoul(buf, NULL, 0);
1704 s->flags |= DEV_OPT_ARP_ACCEPT;
1705 }
1706 }
1707
1708 void
1709 system_if_apply_settings(struct device *dev, struct device_settings *s, uint64_t apply_mask)
1710 {
1711 struct ifreq ifr;
1712 char buf[12];
1713
1714 apply_mask &= s->flags;
1715
1716 memset(&ifr, 0, sizeof(ifr));
1717 strncpy(ifr.ifr_name, dev->ifname, sizeof(ifr.ifr_name) - 1);
1718 if (apply_mask & DEV_OPT_MTU) {
1719 ifr.ifr_mtu = s->mtu;
1720 if (ioctl(sock_ioctl, SIOCSIFMTU, &ifr) < 0)
1721 s->flags &= ~DEV_OPT_MTU;
1722 }
1723 if (apply_mask & DEV_OPT_MTU6) {
1724 system_update_ipv6_mtu(dev, s->mtu6);
1725 }
1726 if (apply_mask & DEV_OPT_TXQUEUELEN) {
1727 ifr.ifr_qlen = s->txqueuelen;
1728 if (ioctl(sock_ioctl, SIOCSIFTXQLEN, &ifr) < 0)
1729 s->flags &= ~DEV_OPT_TXQUEUELEN;
1730 }
1731 if ((apply_mask & (DEV_OPT_MACADDR | DEV_OPT_DEFAULT_MACADDR)) && !dev->external) {
1732 ifr.ifr_hwaddr.sa_family = ARPHRD_ETHER;
1733 memcpy(&ifr.ifr_hwaddr.sa_data, s->macaddr, sizeof(s->macaddr));
1734 if (ioctl(sock_ioctl, SIOCSIFHWADDR, &ifr) < 0)
1735 s->flags &= ~DEV_OPT_MACADDR;
1736 }
1737 if (apply_mask & DEV_OPT_IPV6)
1738 system_set_disable_ipv6(dev, s->ipv6 ? "0" : "1");
1739 if (s->flags & DEV_OPT_IP6SEGMENTROUTING & apply_mask) {
1740 struct device dummy = {
1741 .ifname = "all",
1742 };
1743 bool ip6segmentrouting = device_check_ip6segmentrouting();
1744
1745 system_set_ip6segmentrouting(dev, s->ip6segmentrouting ? "1" : "0");
1746 system_set_ip6segmentrouting(&dummy, ip6segmentrouting ? "1" : "0");
1747 }
1748 if (apply_mask & DEV_OPT_PROMISC) {
1749 if (system_if_flags(dev->ifname, s->promisc ? IFF_PROMISC : 0,
1750 !s->promisc ? IFF_PROMISC : 0) < 0)
1751 s->flags &= ~DEV_OPT_PROMISC;
1752 }
1753 if (apply_mask & DEV_OPT_RPFILTER) {
1754 snprintf(buf, sizeof(buf), "%u", s->rpfilter);
1755 system_set_rpfilter(dev, buf);
1756 }
1757 if (apply_mask & DEV_OPT_ACCEPTLOCAL)
1758 system_set_acceptlocal(dev, s->acceptlocal ? "1" : "0");
1759 if (apply_mask & DEV_OPT_IGMPVERSION) {
1760 snprintf(buf, sizeof(buf), "%u", s->igmpversion);
1761 system_set_igmpversion(dev, buf);
1762 }
1763 if (apply_mask & DEV_OPT_MLDVERSION) {
1764 snprintf(buf, sizeof(buf), "%u", s->mldversion);
1765 system_set_mldversion(dev, buf);
1766 }
1767 if (apply_mask & DEV_OPT_NEIGHREACHABLETIME) {
1768 snprintf(buf, sizeof(buf), "%u", s->neigh4reachabletime);
1769 system_set_neigh4reachabletime(dev, buf);
1770 snprintf(buf, sizeof(buf), "%u", s->neigh6reachabletime);
1771 system_set_neigh6reachabletime(dev, buf);
1772 }
1773 if (apply_mask & DEV_OPT_NEIGHLOCKTIME) {
1774 snprintf(buf, sizeof(buf), "%d", s->neigh4locktime);
1775 system_set_neigh4locktime(dev, buf);
1776 }
1777 if (apply_mask & DEV_OPT_NEIGHGCSTALETIME) {
1778 snprintf(buf, sizeof(buf), "%u", s->neigh4gcstaletime);
1779 system_set_neigh4gcstaletime(dev, buf);
1780 snprintf(buf, sizeof(buf), "%u", s->neigh6gcstaletime);
1781 system_set_neigh6gcstaletime(dev, buf);
1782 }
1783 if (apply_mask & DEV_OPT_DADTRANSMITS) {
1784 snprintf(buf, sizeof(buf), "%u", s->dadtransmits);
1785 system_set_dadtransmits(dev, buf);
1786 }
1787 if (apply_mask & DEV_OPT_MULTICAST) {
1788 if (system_if_flags(dev->ifname, s->multicast ? IFF_MULTICAST : 0,
1789 !s->multicast ? IFF_MULTICAST : 0) < 0)
1790 s->flags &= ~DEV_OPT_MULTICAST;
1791 }
1792 if (apply_mask & DEV_OPT_SENDREDIRECTS)
1793 system_set_sendredirects(dev, s->sendredirects ? "1" : "0");
1794 if (apply_mask & DEV_OPT_DROP_V4_UNICAST_IN_L2_MULTICAST)
1795 system_set_drop_v4_unicast_in_l2_multicast(dev, s->drop_v4_unicast_in_l2_multicast ? "1" : "0");
1796 if (apply_mask & DEV_OPT_DROP_V6_UNICAST_IN_L2_MULTICAST)
1797 system_set_drop_v6_unicast_in_l2_multicast(dev, s->drop_v6_unicast_in_l2_multicast ? "1" : "0");
1798 if (apply_mask & DEV_OPT_DROP_GRATUITOUS_ARP)
1799 system_set_drop_gratuitous_arp(dev, s->drop_gratuitous_arp ? "1" : "0");
1800 if (apply_mask & DEV_OPT_DROP_UNSOLICITED_NA)
1801 system_set_drop_unsolicited_na(dev, s->drop_unsolicited_na ? "1" : "0");
1802 if (apply_mask & DEV_OPT_ARP_ACCEPT)
1803 system_set_arp_accept(dev, s->arp_accept ? "1" : "0");
1804 }
1805
1806 int system_if_up(struct device *dev)
1807 {
1808 return system_if_flags(dev->ifname, IFF_UP, 0);
1809 }
1810
1811 int system_if_down(struct device *dev)
1812 {
1813 return system_if_flags(dev->ifname, 0, IFF_UP);
1814 }
1815
1816 struct if_check_data {
1817 struct device *dev;
1818 int pending;
1819 int ret;
1820 };
1821
1822 #ifndef IFF_LOWER_UP
1823 #define IFF_LOWER_UP 0x10000
1824 #endif
1825
1826 static int cb_if_check_valid(struct nl_msg *msg, void *arg)
1827 {
1828 struct nlmsghdr *nh = nlmsg_hdr(msg);
1829 struct ifinfomsg *ifi = NLMSG_DATA(nh);
1830 struct if_check_data *chk = (struct if_check_data *)arg;
1831
1832 if (nh->nlmsg_type != RTM_NEWLINK)
1833 return NL_SKIP;
1834
1835 if (chk->dev->type == &simple_device_type)
1836 device_set_present(chk->dev, ifi->ifi_index > 0 ? true : false);
1837 device_set_link(chk->dev, ifi->ifi_flags & IFF_LOWER_UP ? true : false);
1838
1839 return NL_OK;
1840 }
1841
1842 static int cb_if_check_ack(struct nl_msg *msg, void *arg)
1843 {
1844 struct if_check_data *chk = (struct if_check_data *)arg;
1845 chk->pending = 0;
1846 return NL_STOP;
1847 }
1848
1849 static int cb_if_check_error(struct sockaddr_nl *nla, struct nlmsgerr *err, void *arg)
1850 {
1851 struct if_check_data *chk = (struct if_check_data *)arg;
1852
1853 if (chk->dev->type == &simple_device_type)
1854 device_set_present(chk->dev, false);
1855 device_set_link(chk->dev, false);
1856 chk->pending = err->error;
1857
1858 return NL_STOP;
1859 }
1860
1861 struct bridge_vlan_check_data {
1862 struct device *check_dev;
1863 int ifindex;
1864 int ret;
1865 bool pending;
1866 };
1867
1868 static void bridge_vlan_check_port(struct bridge_vlan_check_data *data,
1869 struct bridge_vlan_port *port,
1870 struct bridge_vlan_info *vinfo)
1871 {
1872 uint16_t flags = 0, diff, mask;
1873
1874 if (port->flags & BRVLAN_F_PVID)
1875 flags |= BRIDGE_VLAN_INFO_PVID;
1876 if (port->flags & BRVLAN_F_UNTAGGED)
1877 flags |= BRIDGE_VLAN_INFO_UNTAGGED;
1878
1879 diff = vinfo->flags ^ flags;
1880 mask = BRVLAN_F_UNTAGGED | (flags & BRIDGE_VLAN_INFO_PVID);
1881 if (diff & mask) {
1882 data->ret = 1;
1883 data->pending = false;
1884 }
1885
1886 port->check = 1;
1887 }
1888
1889 static void bridge_vlan_check_attr(struct bridge_vlan_check_data *data,
1890 struct rtattr *attr)
1891 {
1892 struct bridge_vlan_hotplug_port *port;
1893 struct bridge_vlan_info *vinfo;
1894 struct bridge_vlan *vlan;
1895 struct rtattr *cur;
1896 int rem = RTA_PAYLOAD(attr);
1897 int i;
1898
1899 for (cur = RTA_DATA(attr); RTA_OK(cur, rem); cur = RTA_NEXT(cur, rem)) {
1900 if (cur->rta_type != IFLA_BRIDGE_VLAN_INFO)
1901 continue;
1902
1903 vinfo = RTA_DATA(cur);
1904 vlan = vlist_find(&data->check_dev->vlans, &vinfo->vid, vlan, node);
1905 if (!vlan) {
1906 data->ret = 1;
1907 data->pending = false;
1908 return;
1909 }
1910
1911 for (i = 0; i < vlan->n_ports; i++)
1912 if (!vlan->ports[i].check)
1913 bridge_vlan_check_port(data, &vlan->ports[i], vinfo);
1914
1915 list_for_each_entry(port, &vlan->hotplug_ports, list)
1916 if (!port->port.check)
1917 bridge_vlan_check_port(data, &port->port, vinfo);
1918 }
1919 }
1920
1921 static int bridge_vlan_check_cb(struct nl_msg *msg, void *arg)
1922 {
1923 struct bridge_vlan_check_data *data = arg;
1924 struct nlmsghdr *nh = nlmsg_hdr(msg);
1925 struct ifinfomsg *ifi = NLMSG_DATA(nh);
1926 struct rtattr *attr;
1927 int rem;
1928
1929 if (nh->nlmsg_type != RTM_NEWLINK)
1930 return NL_SKIP;
1931
1932 if (ifi->ifi_family != AF_BRIDGE)
1933 return NL_SKIP;
1934
1935 if (ifi->ifi_index != data->ifindex)
1936 return NL_SKIP;
1937
1938 attr = IFLA_RTA(ifi);
1939 rem = nh->nlmsg_len - NLMSG_LENGTH(sizeof(*ifi));
1940 while (RTA_OK(attr, rem)) {
1941 if (attr->rta_type == IFLA_AF_SPEC)
1942 bridge_vlan_check_attr(data, attr);
1943
1944 attr = RTA_NEXT(attr, rem);
1945 }
1946
1947 return NL_SKIP;
1948 }
1949
1950 static int bridge_vlan_ack_cb(struct nl_msg *msg, void *arg)
1951 {
1952 struct bridge_vlan_check_data *data = arg;
1953 data->pending = false;
1954 return NL_STOP;
1955 }
1956
1957 static int bridge_vlan_error_cb(struct sockaddr_nl *nla, struct nlmsgerr *err, void *arg)
1958 {
1959 struct bridge_vlan_check_data *data = arg;
1960 data->pending = false;
1961 return NL_STOP;
1962 }
1963
1964 int system_bridge_vlan_check(struct device *dev, char *ifname)
1965 {
1966 struct bridge_vlan_check_data data = {
1967 .check_dev = dev,
1968 .ifindex = if_nametoindex(ifname),
1969 .ret = -1,
1970 .pending = true,
1971 };
1972 static struct ifinfomsg ifi = {
1973 .ifi_family = AF_BRIDGE
1974 };
1975 static struct rtattr ext_req = {
1976 .rta_type = IFLA_EXT_MASK,
1977 .rta_len = RTA_LENGTH(sizeof(uint32_t)),
1978 };
1979 uint32_t filter = RTEXT_FILTER_BRVLAN;
1980 struct nl_cb *cb = nl_cb_alloc(NL_CB_DEFAULT);
1981 struct bridge_vlan *vlan;
1982 struct nl_msg *msg;
1983 int i;
1984
1985 if (!data.ifindex)
1986 return 0;
1987
1988 msg = nlmsg_alloc_simple(RTM_GETLINK, NLM_F_DUMP);
1989
1990 if (nlmsg_append(msg, &ifi, sizeof(ifi), 0) ||
1991 nlmsg_append(msg, &ext_req, sizeof(ext_req), NLMSG_ALIGNTO) ||
1992 nlmsg_append(msg, &filter, sizeof(filter), 0))
1993 goto free;
1994
1995 vlist_for_each_element(&dev->vlans, vlan, node) {
1996 struct bridge_vlan_hotplug_port *port;
1997
1998 for (i = 0; i < vlan->n_ports; i++) {
1999 if (!strcmp(vlan->ports[i].ifname, ifname))
2000 vlan->ports[i].check = 0;
2001 else
2002 vlan->ports[i].check = -1;
2003 }
2004
2005 list_for_each_entry(port, &vlan->hotplug_ports, list) {
2006 if (!strcmp(port->port.ifname, ifname))
2007 port->port.check = 0;
2008 else
2009 port->port.check = -1;
2010 }
2011 }
2012
2013 nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM, bridge_vlan_check_cb, &data);
2014 nl_cb_set(cb, NL_CB_FINISH, NL_CB_CUSTOM, bridge_vlan_ack_cb, &data);
2015 nl_cb_set(cb, NL_CB_ACK, NL_CB_CUSTOM, bridge_vlan_ack_cb, &data);
2016 nl_cb_err(cb, NL_CB_CUSTOM, bridge_vlan_error_cb, &data);
2017
2018 if (nl_send_auto_complete(sock_rtnl, msg) < 0)
2019 goto free;
2020
2021 data.ret = 0;
2022 while (data.pending)
2023 nl_recvmsgs(sock_rtnl, cb);
2024
2025 vlist_for_each_element(&dev->vlans, vlan, node) {
2026 struct bridge_vlan_hotplug_port *port;
2027
2028 for (i = 0; i < vlan->n_ports; i++) {
2029 if (!vlan->ports[i].check) {
2030 data.ret = 1;
2031 break;
2032 }
2033 }
2034
2035 list_for_each_entry(port, &vlan->hotplug_ports, list) {
2036 if (!port->port.check) {
2037 data.ret = 1;
2038 break;
2039 }
2040 }
2041 }
2042
2043 goto out;
2044
2045 free:
2046 nlmsg_free(msg);
2047 out:
2048 nl_cb_put(cb);
2049 return data.ret;
2050 }
2051
2052 int system_if_check(struct device *dev)
2053 {
2054 struct nl_cb *cb = nl_cb_alloc(NL_CB_DEFAULT);
2055 struct nl_msg *msg;
2056 struct ifinfomsg ifi = {
2057 .ifi_family = AF_UNSPEC,
2058 .ifi_index = 0,
2059 };
2060 struct if_check_data chk = {
2061 .dev = dev,
2062 .pending = 1,
2063 };
2064 int ret = 1;
2065
2066 if (!cb)
2067 return ret;
2068
2069 msg = nlmsg_alloc_simple(RTM_GETLINK, 0);
2070 if (!msg)
2071 goto out;
2072
2073 if (nlmsg_append(msg, &ifi, sizeof(ifi), 0) ||
2074 nla_put_string(msg, IFLA_IFNAME, dev->ifname))
2075 goto free;
2076
2077 nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM, cb_if_check_valid, &chk);
2078 nl_cb_set(cb, NL_CB_ACK, NL_CB_CUSTOM, cb_if_check_ack, &chk);
2079 nl_cb_err(cb, NL_CB_CUSTOM, cb_if_check_error, &chk);
2080
2081 ret = nl_send_auto_complete(sock_rtnl, msg);
2082 if (ret < 0)
2083 goto free;
2084
2085 while (chk.pending > 0)
2086 nl_recvmsgs(sock_rtnl, cb);
2087
2088 ret = chk.pending;
2089
2090 free:
2091 nlmsg_free(msg);
2092 out:
2093 nl_cb_put(cb);
2094 return ret;
2095 }
2096
2097 struct device *
2098 system_if_get_parent(struct device *dev)
2099 {
2100 char buf[64], *devname;
2101 int ifindex, iflink, len;
2102 FILE *f;
2103
2104 snprintf(buf, sizeof(buf), "/sys/class/net/%s/iflink", dev->ifname);
2105 f = fopen(buf, "r");
2106 if (!f)
2107 return NULL;
2108
2109 len = fread(buf, 1, sizeof(buf) - 1, f);
2110 fclose(f);
2111
2112 if (len <= 0)
2113 return NULL;
2114
2115 buf[len] = 0;
2116 iflink = strtoul(buf, NULL, 0);
2117 ifindex = system_if_resolve(dev);
2118 if (!iflink || iflink == ifindex)
2119 return NULL;
2120
2121 devname = if_indextoname(iflink, buf);
2122 if (!devname)
2123 return NULL;
2124
2125 return device_get(devname, true);
2126 }
2127
2128 static bool
2129 read_string_file(int dir_fd, const char *file, char *buf, int len)
2130 {
2131 bool ret = false;
2132 char *c;
2133 int fd;
2134
2135 fd = openat(dir_fd, file, O_RDONLY);
2136 if (fd < 0)
2137 return false;
2138
2139 retry:
2140 len = read(fd, buf, len - 1);
2141 if (len < 0) {
2142 if (errno == EINTR)
2143 goto retry;
2144 } else if (len > 0) {
2145 buf[len] = 0;
2146
2147 c = strchr(buf, '\n');
2148 if (c)
2149 *c = 0;
2150
2151 ret = true;
2152 }
2153
2154 close(fd);
2155
2156 return ret;
2157 }
2158
2159 static bool
2160 read_uint64_file(int dir_fd, const char *file, uint64_t *val)
2161 {
2162 char buf[64];
2163 bool ret = false;
2164
2165 ret = read_string_file(dir_fd, file, buf, sizeof(buf));
2166 if (ret)
2167 *val = strtoull(buf, NULL, 0);
2168
2169 return ret;
2170 }
2171
2172 /* Assume advertised flags == supported flags */
2173 static const struct {
2174 uint32_t mask;
2175 const char *name;
2176 } ethtool_link_modes[] = {
2177 { ADVERTISED_10baseT_Half, "10baseT-H" },
2178 { ADVERTISED_10baseT_Full, "10baseT-F" },
2179 { ADVERTISED_100baseT_Half, "100baseT-H" },
2180 { ADVERTISED_100baseT_Full, "100baseT-F" },
2181 { ADVERTISED_1000baseT_Half, "1000baseT-H" },
2182 { ADVERTISED_1000baseT_Full, "1000baseT-F" },
2183 { ADVERTISED_1000baseKX_Full, "1000baseKX-F" },
2184 { ADVERTISED_2500baseX_Full, "2500baseX-F" },
2185 { ADVERTISED_10000baseT_Full, "10000baseT-F" },
2186 { ADVERTISED_10000baseKX4_Full, "10000baseKX4-F" },
2187 { ADVERTISED_10000baseKR_Full, "10000baseKR-F" },
2188 { ADVERTISED_20000baseMLD2_Full, "20000baseMLD2-F" },
2189 { ADVERTISED_20000baseKR2_Full, "20000baseKR2-F" },
2190 { ADVERTISED_40000baseKR4_Full, "40000baseKR4-F" },
2191 { ADVERTISED_40000baseCR4_Full, "40000baseCR4-F" },
2192 { ADVERTISED_40000baseSR4_Full, "40000baseSR4-F" },
2193 { ADVERTISED_40000baseLR4_Full, "40000baseLR4-F" },
2194 #ifdef ADVERTISED_56000baseKR4_Full
2195 { ADVERTISED_56000baseKR4_Full, "56000baseKR4-F" },
2196 { ADVERTISED_56000baseCR4_Full, "56000baseCR4-F" },
2197 { ADVERTISED_56000baseSR4_Full, "56000baseSR4-F" },
2198 { ADVERTISED_56000baseLR4_Full, "56000baseLR4-F" },
2199 #endif
2200 };
2201
2202 static void system_add_link_modes(struct blob_buf *b, __u32 mask)
2203 {
2204 int i;
2205 for (i = 0; i < ARRAY_SIZE(ethtool_link_modes); i++) {
2206 if (mask & ethtool_link_modes[i].mask)
2207 blobmsg_add_string(b, NULL, ethtool_link_modes[i].name);
2208 }
2209 }
2210
2211 bool
2212 system_if_force_external(const char *ifname)
2213 {
2214 char buf[64];
2215 struct stat s;
2216
2217 snprintf(buf, sizeof(buf), "/sys/class/net/%s/phy80211", ifname);
2218 return stat(buf, &s) == 0;
2219 }
2220
2221 int
2222 system_if_dump_info(struct device *dev, struct blob_buf *b)
2223 {
2224 struct ethtool_cmd ecmd;
2225 struct ifreq ifr;
2226 char *s;
2227 void *c;
2228
2229 memset(&ecmd, 0, sizeof(ecmd));
2230 memset(&ifr, 0, sizeof(ifr));
2231 strncpy(ifr.ifr_name, dev->ifname, sizeof(ifr.ifr_name) - 1);
2232 ifr.ifr_data = (caddr_t) &ecmd;
2233 ecmd.cmd = ETHTOOL_GSET;
2234
2235 if (ioctl(sock_ioctl, SIOCETHTOOL, &ifr) == 0) {
2236 c = blobmsg_open_array(b, "link-advertising");
2237 system_add_link_modes(b, ecmd.advertising);
2238 blobmsg_close_array(b, c);
2239
2240 c = blobmsg_open_array(b, "link-partner-advertising");
2241 system_add_link_modes(b, ecmd.lp_advertising);
2242 blobmsg_close_array(b, c);
2243
2244 c = blobmsg_open_array(b, "link-supported");
2245 system_add_link_modes(b, ecmd.supported);
2246 blobmsg_close_array(b, c);
2247
2248 s = blobmsg_alloc_string_buffer(b, "speed", 8);
2249 snprintf(s, 8, "%d%c", ethtool_cmd_speed(&ecmd),
2250 ecmd.duplex == DUPLEX_HALF ? 'H' : 'F');
2251 blobmsg_add_string_buffer(b);
2252
2253 blobmsg_add_u8(b, "autoneg", !!ecmd.autoneg);
2254 }
2255
2256 return 0;
2257 }
2258
2259 int
2260 system_if_dump_stats(struct device *dev, struct blob_buf *b)
2261 {
2262 const char *const counters[] = {
2263 "collisions", "rx_frame_errors", "tx_compressed",
2264 "multicast", "rx_length_errors", "tx_dropped",
2265 "rx_bytes", "rx_missed_errors", "tx_errors",
2266 "rx_compressed", "rx_over_errors", "tx_fifo_errors",
2267 "rx_crc_errors", "rx_packets", "tx_heartbeat_errors",
2268 "rx_dropped", "tx_aborted_errors", "tx_packets",
2269 "rx_errors", "tx_bytes", "tx_window_errors",
2270 "rx_fifo_errors", "tx_carrier_errors",
2271 };
2272 char buf[64];
2273 int stats_dir;
2274 int i;
2275 uint64_t val = 0;
2276
2277 snprintf(buf, sizeof(buf), "/sys/class/net/%s/statistics", dev->ifname);
2278 stats_dir = open(buf, O_DIRECTORY);
2279 if (stats_dir < 0)
2280 return -1;
2281
2282 for (i = 0; i < ARRAY_SIZE(counters); i++)
2283 if (read_uint64_file(stats_dir, counters[i], &val))
2284 blobmsg_add_u64(b, counters[i], val);
2285
2286 close(stats_dir);
2287 return 0;
2288 }
2289
2290 static int system_addr(struct device *dev, struct device_addr *addr, int cmd)
2291 {
2292 bool v4 = ((addr->flags & DEVADDR_FAMILY) == DEVADDR_INET4);
2293 int alen = v4 ? 4 : 16;
2294 unsigned int flags = 0;
2295 struct ifaddrmsg ifa = {
2296 .ifa_family = (alen == 4) ? AF_INET : AF_INET6,
2297 .ifa_prefixlen = addr->mask,
2298 .ifa_index = dev->ifindex,
2299 };
2300
2301 struct nl_msg *msg;
2302 if (cmd == RTM_NEWADDR)
2303 flags |= NLM_F_CREATE | NLM_F_REPLACE;
2304
2305 msg = nlmsg_alloc_simple(cmd, flags);
2306 if (!msg)
2307 return -1;
2308
2309 nlmsg_append(msg, &ifa, sizeof(ifa), 0);
2310 nla_put(msg, IFA_LOCAL, alen, &addr->addr);
2311 if (v4) {
2312 if (addr->broadcast)
2313 nla_put_u32(msg, IFA_BROADCAST, addr->broadcast);
2314 if (addr->point_to_point)
2315 nla_put_u32(msg, IFA_ADDRESS, addr->point_to_point);
2316 } else {
2317 time_t now = system_get_rtime();
2318 struct ifa_cacheinfo cinfo = {0xffffffffU, 0xffffffffU, 0, 0};
2319
2320 if (addr->preferred_until) {
2321 int64_t preferred = addr->preferred_until - now;
2322 if (preferred < 0)
2323 preferred = 0;
2324 else if (preferred > UINT32_MAX)
2325 preferred = UINT32_MAX;
2326
2327 cinfo.ifa_prefered = preferred;
2328 }
2329
2330 if (addr->valid_until) {
2331 int64_t valid = addr->valid_until - now;
2332 if (valid <= 0) {
2333 nlmsg_free(msg);
2334 return -1;
2335 }
2336 else if (valid > UINT32_MAX)
2337 valid = UINT32_MAX;
2338
2339 cinfo.ifa_valid = valid;
2340 }
2341
2342 nla_put(msg, IFA_CACHEINFO, sizeof(cinfo), &cinfo);
2343
2344 if (cmd == RTM_NEWADDR && (addr->flags & DEVADDR_OFFLINK))
2345 nla_put_u32(msg, IFA_FLAGS, IFA_F_NOPREFIXROUTE);
2346 }
2347
2348 return system_rtnl_call(msg);
2349 }
2350
2351 int system_add_address(struct device *dev, struct device_addr *addr)
2352 {
2353 return system_addr(dev, addr, RTM_NEWADDR);
2354 }
2355
2356 int system_del_address(struct device *dev, struct device_addr *addr)
2357 {
2358 return system_addr(dev, addr, RTM_DELADDR);
2359 }
2360
2361 static int system_neigh(struct device *dev, struct device_neighbor *neighbor, int cmd)
2362 {
2363 int alen = ((neighbor->flags & DEVADDR_FAMILY) == DEVADDR_INET4) ? 4 : 16;
2364 unsigned int flags = 0;
2365 struct ndmsg ndm = {
2366 .ndm_family = (alen == 4) ? AF_INET : AF_INET6,
2367 .ndm_ifindex = dev->ifindex,
2368 .ndm_state = NUD_PERMANENT,
2369 .ndm_flags = (neighbor->proxy ? NTF_PROXY : 0) | (neighbor->router ? NTF_ROUTER : 0),
2370 };
2371 struct nl_msg *msg;
2372
2373 if (cmd == RTM_NEWNEIGH)
2374 flags |= NLM_F_CREATE | NLM_F_REPLACE;
2375
2376 msg = nlmsg_alloc_simple(cmd, flags);
2377
2378 if (!msg)
2379 return -1;
2380
2381 nlmsg_append(msg, &ndm, sizeof(ndm), 0);
2382
2383 nla_put(msg, NDA_DST, alen, &neighbor->addr);
2384 if (neighbor->flags & DEVNEIGH_MAC)
2385 nla_put(msg, NDA_LLADDR, sizeof(neighbor->macaddr), &neighbor->macaddr);
2386
2387
2388 return system_rtnl_call(msg);
2389 }
2390
2391 int system_add_neighbor(struct device *dev, struct device_neighbor *neighbor)
2392 {
2393 return system_neigh(dev, neighbor, RTM_NEWNEIGH);
2394 }
2395
2396 int system_del_neighbor(struct device *dev, struct device_neighbor *neighbor)
2397 {
2398 return system_neigh(dev, neighbor, RTM_DELNEIGH);
2399 }
2400
2401 static int system_rt(struct device *dev, struct device_route *route, int cmd)
2402 {
2403 int alen = ((route->flags & DEVADDR_FAMILY) == DEVADDR_INET4) ? 4 : 16;
2404 bool have_gw;
2405 unsigned int flags = 0;
2406
2407 if (alen == 4)
2408 have_gw = !!route->nexthop.in.s_addr;
2409 else
2410 have_gw = route->nexthop.in6.s6_addr32[0] ||
2411 route->nexthop.in6.s6_addr32[1] ||
2412 route->nexthop.in6.s6_addr32[2] ||
2413 route->nexthop.in6.s6_addr32[3];
2414
2415 unsigned int table = (route->flags & (DEVROUTE_TABLE | DEVROUTE_SRCTABLE))
2416 ? route->table : RT_TABLE_MAIN;
2417
2418 struct rtmsg rtm = {
2419 .rtm_family = (alen == 4) ? AF_INET : AF_INET6,
2420 .rtm_dst_len = route->mask,
2421 .rtm_src_len = route->sourcemask,
2422 .rtm_table = (table < 256) ? table : RT_TABLE_UNSPEC,
2423 .rtm_protocol = (route->flags & DEVROUTE_PROTO) ? route->proto : RTPROT_STATIC,
2424 .rtm_scope = RT_SCOPE_NOWHERE,
2425 .rtm_type = (cmd == RTM_DELROUTE) ? 0: RTN_UNICAST,
2426 .rtm_flags = (route->flags & DEVROUTE_ONLINK) ? RTNH_F_ONLINK : 0,
2427 };
2428 struct nl_msg *msg;
2429
2430 if (cmd == RTM_NEWROUTE) {
2431 flags |= NLM_F_CREATE | NLM_F_REPLACE;
2432
2433 if (!dev) { /* Add null-route */
2434 rtm.rtm_scope = RT_SCOPE_UNIVERSE;
2435 rtm.rtm_type = RTN_UNREACHABLE;
2436 }
2437 else
2438 rtm.rtm_scope = (have_gw) ? RT_SCOPE_UNIVERSE : RT_SCOPE_LINK;
2439 }
2440
2441 if (route->flags & DEVROUTE_TYPE) {
2442 rtm.rtm_type = route->type;
2443 if (!(route->flags & (DEVROUTE_TABLE | DEVROUTE_SRCTABLE))) {
2444 if (rtm.rtm_type == RTN_LOCAL || rtm.rtm_type == RTN_BROADCAST ||
2445 rtm.rtm_type == RTN_NAT || rtm.rtm_type == RTN_ANYCAST)
2446 rtm.rtm_table = RT_TABLE_LOCAL;
2447 }
2448
2449 if (rtm.rtm_type == RTN_LOCAL || rtm.rtm_type == RTN_NAT) {
2450 rtm.rtm_scope = RT_SCOPE_HOST;
2451 } else if (rtm.rtm_type == RTN_BROADCAST || rtm.rtm_type == RTN_MULTICAST ||
2452 rtm.rtm_type == RTN_ANYCAST) {
2453 rtm.rtm_scope = RT_SCOPE_LINK;
2454 } else if (rtm.rtm_type == RTN_BLACKHOLE || rtm.rtm_type == RTN_UNREACHABLE ||
2455 rtm.rtm_type == RTN_PROHIBIT || rtm.rtm_type == RTN_FAILED_POLICY ||
2456 rtm.rtm_type == RTN_THROW) {
2457 rtm.rtm_scope = RT_SCOPE_UNIVERSE;
2458 dev = NULL;
2459 }
2460 }
2461
2462 msg = nlmsg_alloc_simple(cmd, flags);
2463 if (!msg)
2464 return -1;
2465
2466 nlmsg_append(msg, &rtm, sizeof(rtm), 0);
2467
2468 if (route->mask)
2469 nla_put(msg, RTA_DST, alen, &route->addr);
2470
2471 if (route->sourcemask) {
2472 if (rtm.rtm_family == AF_INET)
2473 nla_put(msg, RTA_PREFSRC, alen, &route->source);
2474 else
2475 nla_put(msg, RTA_SRC, alen, &route->source);
2476 }
2477
2478 if (route->metric > 0)
2479 nla_put_u32(msg, RTA_PRIORITY, route->metric);
2480
2481 if (have_gw)
2482 nla_put(msg, RTA_GATEWAY, alen, &route->nexthop);
2483
2484 if (dev)
2485 nla_put_u32(msg, RTA_OIF, dev->ifindex);
2486
2487 if (table >= 256)
2488 nla_put_u32(msg, RTA_TABLE, table);
2489
2490 if (route->flags & DEVROUTE_MTU) {
2491 struct nlattr *metrics;
2492
2493 if (!(metrics = nla_nest_start(msg, RTA_METRICS)))
2494 goto nla_put_failure;
2495
2496 nla_put_u32(msg, RTAX_MTU, route->mtu);
2497
2498 nla_nest_end(msg, metrics);
2499 }
2500
2501 return system_rtnl_call(msg);
2502
2503 nla_put_failure:
2504 nlmsg_free(msg);
2505 return -ENOMEM;
2506 }
2507
2508 int system_add_route(struct device *dev, struct device_route *route)
2509 {
2510 return system_rt(dev, route, RTM_NEWROUTE);
2511 }
2512
2513 int system_del_route(struct device *dev, struct device_route *route)
2514 {
2515 return system_rt(dev, route, RTM_DELROUTE);
2516 }
2517
2518 int system_flush_routes(void)
2519 {
2520 const char *names[] = {
2521 "/proc/sys/net/ipv4/route/flush",
2522 "/proc/sys/net/ipv6/route/flush"
2523 };
2524 int fd, i;
2525
2526 for (i = 0; i < ARRAY_SIZE(names); i++) {
2527 fd = open(names[i], O_WRONLY);
2528 if (fd < 0)
2529 continue;
2530
2531 if (write(fd, "-1", 2)) {}
2532 close(fd);
2533 }
2534 return 0;
2535 }
2536
2537 bool system_resolve_rt_type(const char *type, unsigned int *id)
2538 {
2539 return system_rtn_aton(type, id);
2540 }
2541
2542 bool system_resolve_rt_proto(const char *type, unsigned int *id)
2543 {
2544 FILE *f;
2545 char *e, buf[128];
2546 unsigned int n, proto = 256;
2547 n = strtoul(type, &e, 0);
2548 if (!*e && e != type)
2549 proto = n;
2550 else if (!strcmp(type, "unspec"))
2551 proto = RTPROT_UNSPEC;
2552 else if (!strcmp(type, "kernel"))
2553 proto = RTPROT_KERNEL;
2554 else if (!strcmp(type, "boot"))
2555 proto = RTPROT_BOOT;
2556 else if (!strcmp(type, "static"))
2557 proto = RTPROT_STATIC;
2558 else if ((f = fopen("/etc/iproute2/rt_protos", "r")) != NULL) {
2559 while (fgets(buf, sizeof(buf) - 1, f) != NULL) {
2560 if ((e = strtok(buf, " \t\n")) == NULL || *e == '#')
2561 continue;
2562
2563 n = strtoul(e, NULL, 10);
2564 e = strtok(NULL, " \t\n");
2565
2566 if (e && !strcmp(e, type)) {
2567 proto = n;
2568 break;
2569 }
2570 }
2571 fclose(f);
2572 }
2573
2574 if (proto > 255)
2575 return false;
2576
2577 *id = proto;
2578 return true;
2579 }
2580
2581 bool system_resolve_rt_table(const char *name, unsigned int *id)
2582 {
2583 FILE *f;
2584 char *e, buf[128];
2585 unsigned int n, table = RT_TABLE_UNSPEC;
2586
2587 /* first try to parse table as number */
2588 if ((n = strtoul(name, &e, 0)) > 0 && !*e)
2589 table = n;
2590
2591 /* handle well known aliases */
2592 else if (!strcmp(name, "default"))
2593 table = RT_TABLE_DEFAULT;
2594 else if (!strcmp(name, "main"))
2595 table = RT_TABLE_MAIN;
2596 else if (!strcmp(name, "local"))
2597 table = RT_TABLE_LOCAL;
2598
2599 /* try to look up name in /etc/iproute2/rt_tables */
2600 else if ((f = fopen("/etc/iproute2/rt_tables", "r")) != NULL)
2601 {
2602 while (fgets(buf, sizeof(buf) - 1, f) != NULL)
2603 {
2604 if ((e = strtok(buf, " \t\n")) == NULL || *e == '#')
2605 continue;
2606
2607 n = strtoul(e, NULL, 10);
2608 e = strtok(NULL, " \t\n");
2609
2610 if (e && !strcmp(e, name))
2611 {
2612 table = n;
2613 break;
2614 }
2615 }
2616
2617 fclose(f);
2618 }
2619
2620 if (table == RT_TABLE_UNSPEC)
2621 return false;
2622
2623 *id = table;
2624 return true;
2625 }
2626
2627 bool system_is_default_rt_table(unsigned int id)
2628 {
2629 return (id == RT_TABLE_MAIN);
2630 }
2631
2632 bool system_resolve_rpfilter(const char *filter, unsigned int *id)
2633 {
2634 char *e;
2635 unsigned int n;
2636
2637 if (!strcmp(filter, "strict"))
2638 n = 1;
2639 else if (!strcmp(filter, "loose"))
2640 n = 2;
2641 else {
2642 n = strtoul(filter, &e, 0);
2643 if (*e || e == filter || n > 2)
2644 return false;
2645 }
2646
2647 *id = n;
2648 return true;
2649 }
2650
2651 static int system_iprule(struct iprule *rule, int cmd)
2652 {
2653 int alen = ((rule->flags & IPRULE_FAMILY) == IPRULE_INET4) ? 4 : 16;
2654
2655 struct nl_msg *msg;
2656 struct rtmsg rtm = {
2657 .rtm_family = (alen == 4) ? AF_INET : AF_INET6,
2658 .rtm_protocol = RTPROT_STATIC,
2659 .rtm_scope = RT_SCOPE_UNIVERSE,
2660 .rtm_table = RT_TABLE_UNSPEC,
2661 .rtm_type = RTN_UNSPEC,
2662 .rtm_flags = 0,
2663 };
2664
2665 if (cmd == RTM_NEWRULE)
2666 rtm.rtm_type = RTN_UNICAST;
2667
2668 if (rule->invert)
2669 rtm.rtm_flags |= FIB_RULE_INVERT;
2670
2671 if (rule->flags & IPRULE_SRC)
2672 rtm.rtm_src_len = rule->src_mask;
2673
2674 if (rule->flags & IPRULE_DEST)
2675 rtm.rtm_dst_len = rule->dest_mask;
2676
2677 if (rule->flags & IPRULE_TOS)
2678 rtm.rtm_tos = rule->tos;
2679
2680 if (rule->flags & IPRULE_LOOKUP) {
2681 if (rule->lookup < 256)
2682 rtm.rtm_table = rule->lookup;
2683 }
2684
2685 if (rule->flags & IPRULE_ACTION)
2686 rtm.rtm_type = rule->action;
2687 else if (rule->flags & IPRULE_GOTO)
2688 rtm.rtm_type = FR_ACT_GOTO;
2689 else if (!(rule->flags & (IPRULE_LOOKUP | IPRULE_ACTION | IPRULE_GOTO)))
2690 rtm.rtm_type = FR_ACT_NOP;
2691
2692 msg = nlmsg_alloc_simple(cmd, NLM_F_REQUEST);
2693
2694 if (!msg)
2695 return -1;
2696
2697 nlmsg_append(msg, &rtm, sizeof(rtm), 0);
2698
2699 if (rule->flags & IPRULE_IN)
2700 nla_put(msg, FRA_IFNAME, strlen(rule->in_dev) + 1, rule->in_dev);
2701
2702 if (rule->flags & IPRULE_OUT)
2703 nla_put(msg, FRA_OIFNAME, strlen(rule->out_dev) + 1, rule->out_dev);
2704
2705 if (rule->flags & IPRULE_SRC)
2706 nla_put(msg, FRA_SRC, alen, &rule->src_addr);
2707
2708 if (rule->flags & IPRULE_DEST)
2709 nla_put(msg, FRA_DST, alen, &rule->dest_addr);
2710
2711 if (rule->flags & IPRULE_PRIORITY)
2712 nla_put_u32(msg, FRA_PRIORITY, rule->priority);
2713 else if (cmd == RTM_NEWRULE)
2714 nla_put_u32(msg, FRA_PRIORITY, rule->order);
2715
2716 if (rule->flags & IPRULE_FWMARK)
2717 nla_put_u32(msg, FRA_FWMARK, rule->fwmark);
2718
2719 if (rule->flags & IPRULE_FWMASK)
2720 nla_put_u32(msg, FRA_FWMASK, rule->fwmask);
2721
2722 if (rule->flags & IPRULE_LOOKUP) {
2723 if (rule->lookup >= 256)
2724 nla_put_u32(msg, FRA_TABLE, rule->lookup);
2725 }
2726
2727 if (rule->flags & IPRULE_SUP_PREFIXLEN)
2728 nla_put_u32(msg, FRA_SUPPRESS_PREFIXLEN, rule->sup_prefixlen);
2729
2730 if (rule->flags & IPRULE_GOTO)
2731 nla_put_u32(msg, FRA_GOTO, rule->gotoid);
2732
2733 return system_rtnl_call(msg);
2734 }
2735
2736 int system_add_iprule(struct iprule *rule)
2737 {
2738 return system_iprule(rule, RTM_NEWRULE);
2739 }
2740
2741 int system_del_iprule(struct iprule *rule)
2742 {
2743 return system_iprule(rule, RTM_DELRULE);
2744 }
2745
2746 int system_flush_iprules(void)
2747 {
2748 int rv = 0;
2749 struct iprule rule;
2750
2751 system_if_clear_entries(NULL, RTM_GETRULE, AF_INET);
2752 system_if_clear_entries(NULL, RTM_GETRULE, AF_INET6);
2753
2754 memset(&rule, 0, sizeof(rule));
2755
2756
2757 rule.flags = IPRULE_INET4 | IPRULE_PRIORITY | IPRULE_LOOKUP;
2758
2759 rule.priority = 0;
2760 rule.lookup = RT_TABLE_LOCAL;
2761 rv |= system_iprule(&rule, RTM_NEWRULE);
2762
2763 rule.priority = 32766;
2764 rule.lookup = RT_TABLE_MAIN;
2765 rv |= system_iprule(&rule, RTM_NEWRULE);
2766
2767 rule.priority = 32767;
2768 rule.lookup = RT_TABLE_DEFAULT;
2769 rv |= system_iprule(&rule, RTM_NEWRULE);
2770
2771
2772 rule.flags = IPRULE_INET6 | IPRULE_PRIORITY | IPRULE_LOOKUP;
2773
2774 rule.priority = 0;
2775 rule.lookup = RT_TABLE_LOCAL;
2776 rv |= system_iprule(&rule, RTM_NEWRULE);
2777
2778 rule.priority = 32766;
2779 rule.lookup = RT_TABLE_MAIN;
2780 rv |= system_iprule(&rule, RTM_NEWRULE);
2781
2782 return rv;
2783 }
2784
2785 bool system_resolve_iprule_action(const char *action, unsigned int *id)
2786 {
2787 return system_rtn_aton(action, id);
2788 }
2789
2790 time_t system_get_rtime(void)
2791 {
2792 struct timespec ts;
2793 struct timeval tv;
2794
2795 if (clock_gettime(CLOCK_MONOTONIC, &ts) == 0)
2796 return ts.tv_sec;
2797
2798 if (gettimeofday(&tv, NULL) == 0)
2799 return tv.tv_sec;
2800
2801 return 0;
2802 }
2803
2804 #ifndef IP_DF
2805 #define IP_DF 0x4000
2806 #endif
2807
2808 static int tunnel_ioctl(const char *name, int cmd, void *p)
2809 {
2810 struct ifreq ifr;
2811
2812 memset(&ifr, 0, sizeof(ifr));
2813 strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name) - 1);
2814 ifr.ifr_ifru.ifru_data = p;
2815 return ioctl(sock_ioctl, cmd, &ifr);
2816 }
2817
2818 #ifdef IFLA_IPTUN_MAX
2819 static int system_add_ip6_tunnel(const char *name, const unsigned int link,
2820 struct blob_attr **tb)
2821 {
2822 struct nl_msg *nlm = nlmsg_alloc_simple(RTM_NEWLINK,
2823 NLM_F_REQUEST | NLM_F_REPLACE | NLM_F_CREATE);
2824 struct ifinfomsg ifi = { .ifi_family = AF_UNSPEC };
2825 struct blob_attr *cur;
2826 int ret = 0, ttl = 0;
2827
2828 if (!nlm)
2829 return -1;
2830
2831 nlmsg_append(nlm, &ifi, sizeof(ifi), 0);
2832 nla_put_string(nlm, IFLA_IFNAME, name);
2833
2834 if (link)
2835 nla_put_u32(nlm, IFLA_LINK, link);
2836
2837 struct nlattr *linkinfo = nla_nest_start(nlm, IFLA_LINKINFO);
2838 if (!linkinfo) {
2839 ret = -ENOMEM;
2840 goto failure;
2841 }
2842
2843 nla_put_string(nlm, IFLA_INFO_KIND, "ip6tnl");
2844 struct nlattr *infodata = nla_nest_start(nlm, IFLA_INFO_DATA);
2845 if (!infodata) {
2846 ret = -ENOMEM;
2847 goto failure;
2848 }
2849
2850 if (link)
2851 nla_put_u32(nlm, IFLA_IPTUN_LINK, link);
2852
2853 if ((cur = tb[TUNNEL_ATTR_TTL]))
2854 ttl = blobmsg_get_u32(cur);
2855
2856 nla_put_u8(nlm, IFLA_IPTUN_PROTO, IPPROTO_IPIP);
2857 nla_put_u8(nlm, IFLA_IPTUN_TTL, (ttl) ? ttl : 64);
2858
2859 struct in6_addr in6buf;
2860 if ((cur = tb[TUNNEL_ATTR_LOCAL])) {
2861 if (inet_pton(AF_INET6, blobmsg_data(cur), &in6buf) < 1) {
2862 ret = -EINVAL;
2863 goto failure;
2864 }
2865 nla_put(nlm, IFLA_IPTUN_LOCAL, sizeof(in6buf), &in6buf);
2866 }
2867
2868 if ((cur = tb[TUNNEL_ATTR_REMOTE])) {
2869 if (inet_pton(AF_INET6, blobmsg_data(cur), &in6buf) < 1) {
2870 ret = -EINVAL;
2871 goto failure;
2872 }
2873 nla_put(nlm, IFLA_IPTUN_REMOTE, sizeof(in6buf), &in6buf);
2874 }
2875
2876 if ((cur = tb[TUNNEL_ATTR_DATA])) {
2877 struct blob_attr *tb_data[__IPIP6_DATA_ATTR_MAX];
2878 uint32_t tun_flags = IP6_TNL_F_IGN_ENCAP_LIMIT;
2879
2880 blobmsg_parse(ipip6_data_attr_list.params, __IPIP6_DATA_ATTR_MAX, tb_data,
2881 blobmsg_data(cur), blobmsg_len(cur));
2882
2883 if ((cur = tb_data[IPIP6_DATA_ENCAPLIMIT])) {
2884 char *str = blobmsg_get_string(cur);
2885
2886 if (strcmp(str, "ignore")) {
2887 char *e;
2888 unsigned encap_limit = strtoul(str, &e, 0);
2889
2890 if (e == str || *e || encap_limit > 255) {
2891 ret = -EINVAL;
2892 goto failure;
2893 }
2894
2895 nla_put_u8(nlm, IFLA_IPTUN_ENCAP_LIMIT, encap_limit);
2896 tun_flags &= ~IP6_TNL_F_IGN_ENCAP_LIMIT;
2897 }
2898 }
2899
2900 #ifdef IFLA_IPTUN_FMR_MAX
2901 if ((cur = tb_data[IPIP6_DATA_FMRS])) {
2902 struct blob_attr *rcur;
2903 unsigned rrem, fmrcnt = 0;
2904 struct nlattr *fmrs = nla_nest_start(nlm, IFLA_IPTUN_FMRS);
2905
2906 if (!fmrs) {
2907 ret = -ENOMEM;
2908 goto failure;
2909 }
2910
2911 blobmsg_for_each_attr(rcur, cur, rrem) {
2912 struct blob_attr *tb_fmr[__FMR_DATA_ATTR_MAX], *tb_cur;
2913 struct in6_addr ip6prefix;
2914 struct in_addr ip4prefix;
2915 unsigned ip4len, ip6len, ealen, offset;
2916
2917 blobmsg_parse(fmr_data_attr_list.params, __FMR_DATA_ATTR_MAX, tb_fmr,
2918 blobmsg_data(rcur), blobmsg_len(rcur));
2919
2920 if (!(tb_cur = tb_fmr[FMR_DATA_PREFIX6]) ||
2921 !parse_ip_and_netmask(AF_INET6,
2922 blobmsg_data(tb_cur), &ip6prefix,
2923 &ip6len)) {
2924 ret = -EINVAL;
2925 goto failure;
2926 }
2927
2928 if (!(tb_cur = tb_fmr[FMR_DATA_PREFIX4]) ||
2929 !parse_ip_and_netmask(AF_INET,
2930 blobmsg_data(tb_cur), &ip4prefix,
2931 &ip4len)) {
2932 ret = -EINVAL;
2933 goto failure;
2934 }
2935
2936 if (!(tb_cur = tb_fmr[FMR_DATA_EALEN])) {
2937 ret = -EINVAL;
2938 goto failure;
2939 }
2940 ealen = blobmsg_get_u32(tb_cur);
2941
2942 if (!(tb_cur = tb_fmr[FMR_DATA_OFFSET])) {
2943 ret = -EINVAL;
2944 goto failure;
2945 }
2946 offset = blobmsg_get_u32(tb_cur);
2947
2948 struct nlattr *rule = nla_nest_start(nlm, ++fmrcnt);
2949 if (!rule) {
2950 ret = -ENOMEM;
2951 goto failure;
2952 }
2953
2954 nla_put(nlm, IFLA_IPTUN_FMR_IP6_PREFIX, sizeof(ip6prefix), &ip6prefix);
2955 nla_put(nlm, IFLA_IPTUN_FMR_IP4_PREFIX, sizeof(ip4prefix), &ip4prefix);
2956 nla_put_u8(nlm, IFLA_IPTUN_FMR_IP6_PREFIX_LEN, ip6len);
2957 nla_put_u8(nlm, IFLA_IPTUN_FMR_IP4_PREFIX_LEN, ip4len);
2958 nla_put_u8(nlm, IFLA_IPTUN_FMR_EA_LEN, ealen);
2959 nla_put_u8(nlm, IFLA_IPTUN_FMR_OFFSET, offset);
2960
2961 nla_nest_end(nlm, rule);
2962 }
2963
2964 nla_nest_end(nlm, fmrs);
2965 }
2966 #endif
2967 if (tun_flags)
2968 nla_put_u32(nlm, IFLA_IPTUN_FLAGS, tun_flags);
2969 }
2970
2971 nla_nest_end(nlm, infodata);
2972 nla_nest_end(nlm, linkinfo);
2973
2974 return system_rtnl_call(nlm);
2975
2976 failure:
2977 nlmsg_free(nlm);
2978 return ret;
2979 }
2980 #endif
2981
2982 #ifdef IFLA_IPTUN_MAX
2983 #define IP6_FLOWINFO_TCLASS htonl(0x0FF00000)
2984 static int system_add_gre_tunnel(const char *name, const char *kind,
2985 const unsigned int link, struct blob_attr **tb, bool v6)
2986 {
2987 struct nl_msg *nlm;
2988 struct ifinfomsg ifi = { .ifi_family = AF_UNSPEC, };
2989 struct blob_attr *cur;
2990 uint32_t ikey = 0, okey = 0, flowinfo = 0, flags6 = IP6_TNL_F_IGN_ENCAP_LIMIT;
2991 uint16_t iflags = 0, oflags = 0;
2992 uint8_t tos = 0;
2993 int ret = 0, ttl = 0;
2994 unsigned encap_limit = 0;
2995
2996 nlm = nlmsg_alloc_simple(RTM_NEWLINK, NLM_F_REQUEST | NLM_F_REPLACE | NLM_F_CREATE);
2997 if (!nlm)
2998 return -1;
2999
3000 nlmsg_append(nlm, &ifi, sizeof(ifi), 0);
3001 nla_put_string(nlm, IFLA_IFNAME, name);
3002
3003 struct nlattr *linkinfo = nla_nest_start(nlm, IFLA_LINKINFO);
3004 if (!linkinfo) {
3005 ret = -ENOMEM;
3006 goto failure;
3007 }
3008
3009 nla_put_string(nlm, IFLA_INFO_KIND, kind);
3010 struct nlattr *infodata = nla_nest_start(nlm, IFLA_INFO_DATA);
3011 if (!infodata) {
3012 ret = -ENOMEM;
3013 goto failure;
3014 }
3015
3016 if (link)
3017 nla_put_u32(nlm, IFLA_GRE_LINK, link);
3018
3019 if ((cur = tb[TUNNEL_ATTR_TTL]))
3020 ttl = blobmsg_get_u32(cur);
3021
3022 if ((cur = tb[TUNNEL_ATTR_TOS])) {
3023 char *str = blobmsg_get_string(cur);
3024 if (strcmp(str, "inherit")) {
3025 unsigned uval;
3026
3027 if (!system_tos_aton(str, &uval)) {
3028 ret = -EINVAL;
3029 goto failure;
3030 }
3031
3032 if (v6)
3033 flowinfo |= htonl(uval << 20) & IP6_FLOWINFO_TCLASS;
3034 else
3035 tos = uval;
3036 } else {
3037 if (v6)
3038 flags6 |= IP6_TNL_F_USE_ORIG_TCLASS;
3039 else
3040 tos = 1;
3041 }
3042 }
3043
3044 if ((cur = tb[TUNNEL_ATTR_DATA])) {
3045 struct blob_attr *tb_data[__GRE_DATA_ATTR_MAX];
3046
3047 blobmsg_parse(gre_data_attr_list.params, __GRE_DATA_ATTR_MAX, tb_data,
3048 blobmsg_data(cur), blobmsg_len(cur));
3049
3050 if ((cur = tb_data[GRE_DATA_IKEY])) {
3051 if ((ikey = blobmsg_get_u32(cur)))
3052 iflags |= GRE_KEY;
3053 }
3054
3055 if ((cur = tb_data[GRE_DATA_OKEY])) {
3056 if ((okey = blobmsg_get_u32(cur)))
3057 oflags |= GRE_KEY;
3058 }
3059
3060 if ((cur = tb_data[GRE_DATA_ICSUM])) {
3061 if (blobmsg_get_bool(cur))
3062 iflags |= GRE_CSUM;
3063 }
3064
3065 if ((cur = tb_data[GRE_DATA_OCSUM])) {
3066 if (blobmsg_get_bool(cur))
3067 oflags |= GRE_CSUM;
3068 }
3069
3070 if ((cur = tb_data[GRE_DATA_ISEQNO])) {
3071 if (blobmsg_get_bool(cur))
3072 iflags |= GRE_SEQ;
3073 }
3074
3075 if ((cur = tb_data[GRE_DATA_OSEQNO])) {
3076 if (blobmsg_get_bool(cur))
3077 oflags |= GRE_SEQ;
3078 }
3079
3080 if ((cur = tb_data[GRE_DATA_ENCAPLIMIT])) {
3081 char *str = blobmsg_get_string(cur);
3082
3083 if (strcmp(str, "ignore")) {
3084 char *e;
3085
3086 encap_limit = strtoul(str, &e, 0);
3087
3088 if (e == str || *e || encap_limit > 255) {
3089 ret = -EINVAL;
3090 goto failure;
3091 }
3092
3093 flags6 &= ~IP6_TNL_F_IGN_ENCAP_LIMIT;
3094 }
3095 }
3096 }
3097
3098 if (v6) {
3099 struct in6_addr in6buf;
3100 if ((cur = tb[TUNNEL_ATTR_LOCAL])) {
3101 if (inet_pton(AF_INET6, blobmsg_data(cur), &in6buf) < 1) {
3102 ret = -EINVAL;
3103 goto failure;
3104 }
3105 nla_put(nlm, IFLA_GRE_LOCAL, sizeof(in6buf), &in6buf);
3106 }
3107
3108 if ((cur = tb[TUNNEL_ATTR_REMOTE])) {
3109 if (inet_pton(AF_INET6, blobmsg_data(cur), &in6buf) < 1) {
3110 ret = -EINVAL;
3111 goto failure;
3112 }
3113 nla_put(nlm, IFLA_GRE_REMOTE, sizeof(in6buf), &in6buf);
3114 }
3115
3116 if (!(flags6 & IP6_TNL_F_IGN_ENCAP_LIMIT))
3117 nla_put_u8(nlm, IFLA_GRE_ENCAP_LIMIT, encap_limit);
3118
3119 if (flowinfo)
3120 nla_put_u32(nlm, IFLA_GRE_FLOWINFO, flowinfo);
3121
3122 if (flags6)
3123 nla_put_u32(nlm, IFLA_GRE_FLAGS, flags6);
3124
3125 if (!ttl)
3126 ttl = 64;
3127 } else {
3128 struct in_addr inbuf;
3129 bool set_df = true;
3130
3131 if ((cur = tb[TUNNEL_ATTR_LOCAL])) {
3132 if (inet_pton(AF_INET, blobmsg_data(cur), &inbuf) < 1) {
3133 ret = -EINVAL;
3134 goto failure;
3135 }
3136 nla_put(nlm, IFLA_GRE_LOCAL, sizeof(inbuf), &inbuf);
3137 }
3138
3139 if ((cur = tb[TUNNEL_ATTR_REMOTE])) {
3140 if (inet_pton(AF_INET, blobmsg_data(cur), &inbuf) < 1) {
3141 ret = -EINVAL;
3142 goto failure;
3143 }
3144 nla_put(nlm, IFLA_GRE_REMOTE, sizeof(inbuf), &inbuf);
3145
3146 if (IN_MULTICAST(ntohl(inbuf.s_addr))) {
3147 if (!okey) {
3148 okey = inbuf.s_addr;
3149 oflags |= GRE_KEY;
3150 }
3151
3152 if (!ikey) {
3153 ikey = inbuf.s_addr;
3154 iflags |= GRE_KEY;
3155 }
3156 }
3157 }
3158
3159 if ((cur = tb[TUNNEL_ATTR_DF]))
3160 set_df = blobmsg_get_bool(cur);
3161
3162 if (!set_df) {
3163 /* ttl != 0 and nopmtudisc are incompatible */
3164 if (ttl) {
3165 ret = -EINVAL;
3166 goto failure;
3167 }
3168 } else if (!ttl)
3169 ttl = 64;
3170
3171 nla_put_u8(nlm, IFLA_GRE_PMTUDISC, set_df ? 1 : 0);
3172
3173 nla_put_u8(nlm, IFLA_GRE_TOS, tos);
3174 }
3175
3176 if (ttl)
3177 nla_put_u8(nlm, IFLA_GRE_TTL, ttl);
3178
3179 if (oflags)
3180 nla_put_u16(nlm, IFLA_GRE_OFLAGS, oflags);
3181
3182 if (iflags)
3183 nla_put_u16(nlm, IFLA_GRE_IFLAGS, iflags);
3184
3185 if (okey)
3186 nla_put_u32(nlm, IFLA_GRE_OKEY, htonl(okey));
3187
3188 if (ikey)
3189 nla_put_u32(nlm, IFLA_GRE_IKEY, htonl(ikey));
3190
3191 nla_nest_end(nlm, infodata);
3192 nla_nest_end(nlm, linkinfo);
3193
3194 return system_rtnl_call(nlm);
3195
3196 failure:
3197 nlmsg_free(nlm);
3198 return ret;
3199 }
3200 #endif
3201
3202 #ifdef IFLA_VTI_MAX
3203 static int system_add_vti_tunnel(const char *name, const char *kind,
3204 const unsigned int link, struct blob_attr **tb, bool v6)
3205 {
3206 struct nl_msg *nlm;
3207 struct ifinfomsg ifi = { .ifi_family = AF_UNSPEC, };
3208 struct blob_attr *cur;
3209 int ret = 0;
3210
3211 nlm = nlmsg_alloc_simple(RTM_NEWLINK, NLM_F_REQUEST | NLM_F_REPLACE | NLM_F_CREATE);
3212 if (!nlm)
3213 return -1;
3214
3215 nlmsg_append(nlm, &ifi, sizeof(ifi), 0);
3216 nla_put_string(nlm, IFLA_IFNAME, name);
3217
3218 struct nlattr *linkinfo = nla_nest_start(nlm, IFLA_LINKINFO);
3219 if (!linkinfo) {
3220 ret = -ENOMEM;
3221 goto failure;
3222 }
3223
3224 nla_put_string(nlm, IFLA_INFO_KIND, kind);
3225 struct nlattr *infodata = nla_nest_start(nlm, IFLA_INFO_DATA);
3226 if (!infodata) {
3227 ret = -ENOMEM;
3228 goto failure;
3229 }
3230
3231 if (link)
3232 nla_put_u32(nlm, IFLA_VTI_LINK, link);
3233
3234 if (v6) {
3235 struct in6_addr in6buf;
3236 if ((cur = tb[TUNNEL_ATTR_LOCAL])) {
3237 if (inet_pton(AF_INET6, blobmsg_data(cur), &in6buf) < 1) {
3238 ret = -EINVAL;
3239 goto failure;
3240 }
3241 nla_put(nlm, IFLA_VTI_LOCAL, sizeof(in6buf), &in6buf);
3242 }
3243
3244 if ((cur = tb[TUNNEL_ATTR_REMOTE])) {
3245 if (inet_pton(AF_INET6, blobmsg_data(cur), &in6buf) < 1) {
3246 ret = -EINVAL;
3247 goto failure;
3248 }
3249 nla_put(nlm, IFLA_VTI_REMOTE, sizeof(in6buf), &in6buf);
3250 }
3251
3252 } else {
3253 struct in_addr inbuf;
3254
3255 if ((cur = tb[TUNNEL_ATTR_LOCAL])) {
3256 if (inet_pton(AF_INET, blobmsg_data(cur), &inbuf) < 1) {
3257 ret = -EINVAL;
3258 goto failure;
3259 }
3260 nla_put(nlm, IFLA_VTI_LOCAL, sizeof(inbuf), &inbuf);
3261 }
3262
3263 if ((cur = tb[TUNNEL_ATTR_REMOTE])) {
3264 if (inet_pton(AF_INET, blobmsg_data(cur), &inbuf) < 1) {
3265 ret = -EINVAL;
3266 goto failure;
3267 }
3268 nla_put(nlm, IFLA_VTI_REMOTE, sizeof(inbuf), &inbuf);
3269 }
3270
3271 }
3272
3273 if ((cur = tb[TUNNEL_ATTR_DATA])) {
3274 struct blob_attr *tb_data[__VTI_DATA_ATTR_MAX];
3275 uint32_t ikey = 0, okey = 0;
3276
3277 blobmsg_parse(vti_data_attr_list.params, __VTI_DATA_ATTR_MAX, tb_data,
3278 blobmsg_data(cur), blobmsg_len(cur));
3279
3280 if ((cur = tb_data[VTI_DATA_IKEY])) {
3281 if ((ikey = blobmsg_get_u32(cur)))
3282 nla_put_u32(nlm, IFLA_VTI_IKEY, htonl(ikey));
3283 }
3284
3285 if ((cur = tb_data[VTI_DATA_OKEY])) {
3286 if ((okey = blobmsg_get_u32(cur)))
3287 nla_put_u32(nlm, IFLA_VTI_OKEY, htonl(okey));
3288 }
3289 }
3290
3291 nla_nest_end(nlm, infodata);
3292 nla_nest_end(nlm, linkinfo);
3293
3294 return system_rtnl_call(nlm);
3295
3296 failure:
3297 nlmsg_free(nlm);
3298 return ret;
3299 }
3300 #endif
3301
3302 #ifdef IFLA_XFRM_MAX
3303 static int system_add_xfrm_tunnel(const char *name, const char *kind,
3304 const unsigned int link, struct blob_attr **tb)
3305 {
3306 struct nl_msg *nlm;
3307 struct ifinfomsg ifi = { .ifi_family = AF_UNSPEC, };
3308 struct blob_attr *cur;
3309 int ret = 0;
3310
3311 nlm = nlmsg_alloc_simple(RTM_NEWLINK, NLM_F_REQUEST | NLM_F_REPLACE | NLM_F_CREATE);
3312 if (!nlm)
3313 return -1;
3314
3315 nlmsg_append(nlm, &ifi, sizeof(ifi), 0);
3316 nla_put_string(nlm, IFLA_IFNAME, name);
3317
3318 struct nlattr *linkinfo = nla_nest_start(nlm, IFLA_LINKINFO);
3319 if (!linkinfo) {
3320 ret = -ENOMEM;
3321 goto failure;
3322 }
3323
3324 nla_put_string(nlm, IFLA_INFO_KIND, kind);
3325 struct nlattr *infodata = nla_nest_start(nlm, IFLA_INFO_DATA);
3326 if (!infodata) {
3327 ret = -ENOMEM;
3328 goto failure;
3329 }
3330
3331 if (link)
3332 nla_put_u32(nlm, IFLA_XFRM_LINK, link);
3333
3334 if ((cur = tb[TUNNEL_ATTR_DATA])) {
3335 struct blob_attr *tb_data[__XFRM_DATA_ATTR_MAX];
3336 uint32_t if_id = 0;
3337
3338 blobmsg_parse(xfrm_data_attr_list.params, __XFRM_DATA_ATTR_MAX, tb_data,
3339 blobmsg_data(cur), blobmsg_len(cur));
3340
3341 if ((cur = tb_data[XFRM_DATA_IF_ID])) {
3342 if ((if_id = blobmsg_get_u32(cur)))
3343 nla_put_u32(nlm, IFLA_XFRM_IF_ID, if_id);
3344 }
3345
3346 }
3347
3348 nla_nest_end(nlm, infodata);
3349 nla_nest_end(nlm, linkinfo);
3350
3351 return system_rtnl_call(nlm);
3352
3353 failure:
3354 nlmsg_free(nlm);
3355 return ret;
3356 }
3357 #endif
3358
3359 #ifdef IFLA_VXLAN_MAX
3360 static void system_vxlan_map_bool_attr(struct nl_msg *msg, struct blob_attr **tb_data, int attrtype, int vxlandatatype, bool invert) {
3361 struct blob_attr *cur;
3362 if ((cur = tb_data[vxlandatatype])) {
3363 bool val = blobmsg_get_bool(cur);
3364 if (invert)
3365 val = !val;
3366
3367 if ((attrtype == IFLA_VXLAN_GBP) && val)
3368 nla_put_flag(msg, attrtype);
3369 else
3370 nla_put_u8(msg, attrtype, val);
3371
3372 }
3373 }
3374
3375 static int system_add_vxlan(const char *name, const unsigned int link, struct blob_attr **tb, bool v6)
3376 {
3377 struct blob_attr *tb_data[__VXLAN_DATA_ATTR_MAX];
3378 struct nl_msg *msg;
3379 struct nlattr *linkinfo, *data;
3380 struct ifinfomsg iim = { .ifi_family = AF_UNSPEC, };
3381 struct blob_attr *cur;
3382 int ret = 0;
3383
3384 if ((cur = tb[TUNNEL_ATTR_DATA]))
3385 blobmsg_parse(vxlan_data_attr_list.params, __VXLAN_DATA_ATTR_MAX, tb_data,
3386 blobmsg_data(cur), blobmsg_len(cur));
3387 else
3388 return -EINVAL;
3389
3390 msg = nlmsg_alloc_simple(RTM_NEWLINK, NLM_F_REQUEST | NLM_F_CREATE | NLM_F_EXCL);
3391
3392 if (!msg)
3393 return -1;
3394
3395 nlmsg_append(msg, &iim, sizeof(iim), 0);
3396
3397 nla_put_string(msg, IFLA_IFNAME, name);
3398
3399 if ((cur = tb_data[VXLAN_DATA_ATTR_MACADDR])) {
3400 struct ether_addr *ea = ether_aton(blobmsg_get_string(cur));
3401 if (!ea) {
3402 ret = -EINVAL;
3403 goto failure;
3404 }
3405
3406 nla_put(msg, IFLA_ADDRESS, ETH_ALEN, ea);
3407 }
3408
3409 if ((cur = tb[TUNNEL_ATTR_MTU])) {
3410 uint32_t mtu = blobmsg_get_u32(cur);
3411 nla_put_u32(msg, IFLA_MTU, mtu);
3412 }
3413
3414 if (!(linkinfo = nla_nest_start(msg, IFLA_LINKINFO))) {
3415 ret = -ENOMEM;
3416 goto failure;
3417 }
3418
3419 nla_put_string(msg, IFLA_INFO_KIND, "vxlan");
3420
3421 if (!(data = nla_nest_start(msg, IFLA_INFO_DATA))) {
3422 ret = -ENOMEM;
3423 goto failure;
3424 }
3425
3426 if (link)
3427 nla_put_u32(msg, IFLA_VXLAN_LINK, link);
3428
3429 if ((cur = tb_data[VXLAN_DATA_ATTR_ID])) {
3430 uint32_t id = blobmsg_get_u32(cur);
3431 if (id >= (1u << 24) - 1) {
3432 ret = -EINVAL;
3433 goto failure;
3434 }
3435
3436 nla_put_u32(msg, IFLA_VXLAN_ID, id);
3437 }
3438
3439 if (v6) {
3440 struct in6_addr in6buf;
3441 if ((cur = tb[TUNNEL_ATTR_LOCAL])) {
3442 if (inet_pton(AF_INET6, blobmsg_data(cur), &in6buf) < 1) {
3443 ret = -EINVAL;
3444 goto failure;
3445 }
3446 nla_put(msg, IFLA_VXLAN_LOCAL6, sizeof(in6buf), &in6buf);
3447 }
3448
3449 if ((cur = tb[TUNNEL_ATTR_REMOTE])) {
3450 if (inet_pton(AF_INET6, blobmsg_data(cur), &in6buf) < 1) {
3451 ret = -EINVAL;
3452 goto failure;
3453 }
3454 nla_put(msg, IFLA_VXLAN_GROUP6, sizeof(in6buf), &in6buf);
3455 }
3456 } else {
3457 struct in_addr inbuf;
3458
3459 if ((cur = tb[TUNNEL_ATTR_LOCAL])) {
3460 if (inet_pton(AF_INET, blobmsg_data(cur), &inbuf) < 1) {
3461 ret = -EINVAL;
3462 goto failure;
3463 }
3464 nla_put(msg, IFLA_VXLAN_LOCAL, sizeof(inbuf), &inbuf);
3465 }
3466
3467 if ((cur = tb[TUNNEL_ATTR_REMOTE])) {
3468 if (inet_pton(AF_INET, blobmsg_data(cur), &inbuf) < 1) {
3469 ret = -EINVAL;
3470 goto failure;
3471 }
3472 nla_put(msg, IFLA_VXLAN_GROUP, sizeof(inbuf), &inbuf);
3473 }
3474 }
3475
3476 uint32_t port = 4789;
3477 if ((cur = tb_data[VXLAN_DATA_ATTR_PORT])) {
3478 port = blobmsg_get_u32(cur);
3479 if (port < 1 || port > 65535) {
3480 ret = -EINVAL;
3481 goto failure;
3482 }
3483 }
3484 nla_put_u16(msg, IFLA_VXLAN_PORT, htons(port));
3485
3486 if ((cur = tb_data[VXLAN_DATA_ATTR_SRCPORTMIN])) {
3487 struct ifla_vxlan_port_range srcports = {0,0};
3488
3489 uint32_t low = blobmsg_get_u32(cur);
3490 if (low < 1 || low > 65535 - 1) {
3491 ret = -EINVAL;
3492 goto failure;
3493 }
3494
3495 srcports.low = htons((uint16_t) low);
3496 srcports.high = htons((uint16_t) (low+1));
3497
3498 if ((cur = tb_data[VXLAN_DATA_ATTR_SRCPORTMAX])) {
3499 uint32_t high = blobmsg_get_u32(cur);
3500 if (high < 1 || high > 65535) {
3501 ret = -EINVAL;
3502 goto failure;
3503 }
3504
3505 if (high > low)
3506 srcports.high = htons((uint16_t) high);
3507 }
3508
3509 nla_put(msg, IFLA_VXLAN_PORT_RANGE, sizeof(srcports), &srcports);
3510 }
3511
3512 system_vxlan_map_bool_attr(msg, tb_data, IFLA_VXLAN_UDP_CSUM, VXLAN_DATA_ATTR_TXCSUM, false);
3513 system_vxlan_map_bool_attr(msg, tb_data, IFLA_VXLAN_UDP_ZERO_CSUM6_RX, VXLAN_DATA_ATTR_RXCSUM, true);
3514 system_vxlan_map_bool_attr(msg, tb_data, IFLA_VXLAN_UDP_ZERO_CSUM6_TX, VXLAN_DATA_ATTR_TXCSUM, true);
3515 system_vxlan_map_bool_attr(msg, tb_data, IFLA_VXLAN_LEARNING, VXLAN_DATA_ATTR_LEARNING, false);
3516 system_vxlan_map_bool_attr(msg, tb_data, IFLA_VXLAN_RSC , VXLAN_DATA_ATTR_RSC, false);
3517 system_vxlan_map_bool_attr(msg, tb_data, IFLA_VXLAN_PROXY , VXLAN_DATA_ATTR_PROXY, false);
3518 system_vxlan_map_bool_attr(msg, tb_data, IFLA_VXLAN_L2MISS , VXLAN_DATA_ATTR_L2MISS, false);
3519 system_vxlan_map_bool_attr(msg, tb_data, IFLA_VXLAN_L3MISS , VXLAN_DATA_ATTR_L3MISS, false);
3520 system_vxlan_map_bool_attr(msg, tb_data, IFLA_VXLAN_GBP , VXLAN_DATA_ATTR_GBP, false);
3521
3522 if ((cur = tb_data[VXLAN_DATA_ATTR_AGEING])) {
3523 uint32_t ageing = blobmsg_get_u32(cur);
3524 nla_put_u32(msg, IFLA_VXLAN_AGEING, ageing);
3525 }
3526
3527 if ((cur = tb_data[VXLAN_DATA_ATTR_LIMIT])) {
3528 uint32_t maxaddress = blobmsg_get_u32(cur);
3529 nla_put_u32(msg, IFLA_VXLAN_LIMIT, maxaddress);
3530 }
3531
3532 if ((cur = tb[TUNNEL_ATTR_TOS])) {
3533 char *str = blobmsg_get_string(cur);
3534 unsigned tos = 1;
3535
3536 if (strcmp(str, "inherit")) {
3537 if (!system_tos_aton(str, &tos)) {
3538 ret = -EINVAL;
3539 goto failure;
3540 }
3541 }
3542
3543 nla_put_u8(msg, IFLA_VXLAN_TOS, tos);
3544 }
3545
3546 if ((cur = tb[TUNNEL_ATTR_TTL])) {
3547 uint32_t ttl = blobmsg_get_u32(cur);
3548 if (ttl < 1 || ttl > 255) {
3549 ret = -EINVAL;
3550 goto failure;
3551 }
3552
3553 nla_put_u8(msg, IFLA_VXLAN_TTL, ttl);
3554 }
3555
3556 nla_nest_end(msg, data);
3557 nla_nest_end(msg, linkinfo);
3558
3559 ret = system_rtnl_call(msg);
3560 if (ret)
3561 D(SYSTEM, "Error adding vxlan '%s': %d\n", name, ret);
3562
3563 return ret;
3564
3565 failure:
3566 nlmsg_free(msg);
3567 return ret;
3568 }
3569 #endif
3570
3571 static int system_add_sit_tunnel(const char *name, const unsigned int link, struct blob_attr **tb)
3572 {
3573 struct blob_attr *cur;
3574 int ret = 0;
3575
3576 if (system_add_proto_tunnel(name, IPPROTO_IPV6, link, tb) < 0)
3577 return -1;
3578
3579 #ifdef SIOCADD6RD
3580 if ((cur = tb[TUNNEL_ATTR_DATA])) {
3581 struct blob_attr *tb_data[__SIXRD_DATA_ATTR_MAX];
3582 unsigned int mask;
3583 struct ip_tunnel_6rd p6;
3584
3585 blobmsg_parse(sixrd_data_attr_list.params, __SIXRD_DATA_ATTR_MAX, tb_data,
3586 blobmsg_data(cur), blobmsg_len(cur));
3587
3588 memset(&p6, 0, sizeof(p6));
3589
3590 if ((cur = tb_data[SIXRD_DATA_PREFIX])) {
3591 if (!parse_ip_and_netmask(AF_INET6, blobmsg_data(cur),
3592 &p6.prefix, &mask) || mask > 128) {
3593 ret = -EINVAL;
3594 goto failure;
3595 }
3596
3597 p6.prefixlen = mask;
3598 }
3599
3600 if ((cur = tb_data[SIXRD_DATA_RELAY_PREFIX])) {
3601 if (!parse_ip_and_netmask(AF_INET, blobmsg_data(cur),
3602 &p6.relay_prefix, &mask) || mask > 32) {
3603 ret = -EINVAL;
3604 goto failure;
3605 }
3606
3607 p6.relay_prefixlen = mask;
3608 }
3609
3610 if (tunnel_ioctl(name, SIOCADD6RD, &p6) < 0) {
3611 ret = -1;
3612 goto failure;
3613 }
3614 }
3615 #endif
3616
3617 return ret;
3618
3619 failure:
3620 __system_del_ip_tunnel(name, tb);
3621 return ret;
3622 }
3623
3624 static int system_add_proto_tunnel(const char *name, const uint8_t proto, const unsigned int link, struct blob_attr **tb)
3625 {
3626 struct blob_attr *cur;
3627 bool set_df = true;
3628 struct ip_tunnel_parm p = {
3629 .link = link,
3630 .iph = {
3631 .version = 4,
3632 .ihl = 5,
3633 .protocol = proto,
3634 }
3635 };
3636
3637 if ((cur = tb[TUNNEL_ATTR_LOCAL]) &&
3638 inet_pton(AF_INET, blobmsg_data(cur), &p.iph.saddr) < 1)
3639 return -EINVAL;
3640
3641 if ((cur = tb[TUNNEL_ATTR_REMOTE]) &&
3642 inet_pton(AF_INET, blobmsg_data(cur), &p.iph.daddr) < 1)
3643 return -EINVAL;
3644
3645 if ((cur = tb[TUNNEL_ATTR_DF]))
3646 set_df = blobmsg_get_bool(cur);
3647
3648 if ((cur = tb[TUNNEL_ATTR_TTL]))
3649 p.iph.ttl = blobmsg_get_u32(cur);
3650
3651 if ((cur = tb[TUNNEL_ATTR_TOS])) {
3652 char *str = blobmsg_get_string(cur);
3653 if (strcmp(str, "inherit")) {
3654 unsigned uval;
3655
3656 if (!system_tos_aton(str, &uval))
3657 return -EINVAL;
3658
3659 p.iph.tos = uval;
3660 } else
3661 p.iph.tos = 1;
3662 }
3663
3664 p.iph.frag_off = set_df ? htons(IP_DF) : 0;
3665 /* ttl !=0 and nopmtudisc are incompatible */
3666 if (p.iph.ttl && p.iph.frag_off == 0)
3667 return -EINVAL;
3668
3669 strncpy(p.name, name, sizeof(p.name) - 1);
3670
3671 switch (p.iph.protocol) {
3672 case IPPROTO_IPIP:
3673 return tunnel_ioctl("tunl0", SIOCADDTUNNEL, &p);
3674 case IPPROTO_IPV6:
3675 return tunnel_ioctl("sit0", SIOCADDTUNNEL, &p);
3676 default:
3677 break;
3678 }
3679 return -1;
3680 }
3681
3682 static int __system_del_ip_tunnel(const char *name, struct blob_attr **tb)
3683 {
3684 struct blob_attr *cur;
3685 const char *str;
3686
3687 if (!(cur = tb[TUNNEL_ATTR_TYPE]))
3688 return -EINVAL;
3689 str = blobmsg_data(cur);
3690
3691 if (!strcmp(str, "greip") || !strcmp(str, "gretapip") ||
3692 !strcmp(str, "greip6") || !strcmp(str, "gretapip6") ||
3693 !strcmp(str, "vtiip") || !strcmp(str, "vtiip6") ||
3694 !strcmp(str, "vxlan") || !strcmp(str, "vxlan6") ||
3695 !strcmp(str, "xfrm"))
3696 return system_link_del(name);
3697 else
3698 return tunnel_ioctl(name, SIOCDELTUNNEL, NULL);
3699 }
3700
3701 int system_del_ip_tunnel(const char *name, struct blob_attr *attr)
3702 {
3703 struct blob_attr *tb[__TUNNEL_ATTR_MAX];
3704
3705 blobmsg_parse(tunnel_attr_list.params, __TUNNEL_ATTR_MAX, tb,
3706 blob_data(attr), blob_len(attr));
3707
3708 return __system_del_ip_tunnel(name, tb);
3709 }
3710
3711 int system_update_ipv6_mtu(struct device *dev, int mtu)
3712 {
3713 int ret = -1;
3714 char buf[64];
3715 int fd;
3716
3717 snprintf(buf, sizeof(buf), "/proc/sys/net/ipv6/conf/%s/mtu",
3718 dev->ifname);
3719
3720 fd = open(buf, O_RDWR);
3721 if (fd < 0)
3722 return ret;
3723
3724 if (!mtu) {
3725 ssize_t len = read(fd, buf, sizeof(buf) - 1);
3726 if (len < 0)
3727 goto out;
3728
3729 buf[len] = 0;
3730 ret = atoi(buf);
3731 } else {
3732 if (write(fd, buf, snprintf(buf, sizeof(buf), "%i", mtu)) > 0)
3733 ret = mtu;
3734 }
3735
3736 out:
3737 close(fd);
3738 return ret;
3739 }
3740
3741 int system_add_ip_tunnel(const char *name, struct blob_attr *attr)
3742 {
3743 struct blob_attr *tb[__TUNNEL_ATTR_MAX];
3744 struct blob_attr *cur;
3745 const char *str;
3746
3747 blobmsg_parse(tunnel_attr_list.params, __TUNNEL_ATTR_MAX, tb,
3748 blob_data(attr), blob_len(attr));
3749
3750 __system_del_ip_tunnel(name, tb);
3751
3752 if (!(cur = tb[TUNNEL_ATTR_TYPE]))
3753 return -EINVAL;
3754 str = blobmsg_data(cur);
3755
3756 unsigned int ttl = 0;
3757 if ((cur = tb[TUNNEL_ATTR_TTL])) {
3758 ttl = blobmsg_get_u32(cur);
3759 if (ttl > 255)
3760 return -EINVAL;
3761 }
3762
3763 unsigned int link = 0;
3764 if ((cur = tb[TUNNEL_ATTR_LINK])) {
3765 struct interface *iface = vlist_find(&interfaces, blobmsg_data(cur), iface, node);
3766 if (!iface)
3767 return -EINVAL;
3768
3769 if (iface->l3_dev.dev)
3770 link = iface->l3_dev.dev->ifindex;
3771 }
3772
3773 if (!strcmp(str, "sit"))
3774 return system_add_sit_tunnel(name, link, tb);
3775 #ifdef IFLA_IPTUN_MAX
3776 else if (!strcmp(str, "ipip6")) {
3777 return system_add_ip6_tunnel(name, link, tb);
3778 } else if (!strcmp(str, "greip")) {
3779 return system_add_gre_tunnel(name, "gre", link, tb, false);
3780 } else if (!strcmp(str, "gretapip")) {
3781 return system_add_gre_tunnel(name, "gretap", link, tb, false);
3782 } else if (!strcmp(str, "greip6")) {
3783 return system_add_gre_tunnel(name, "ip6gre", link, tb, true);
3784 } else if (!strcmp(str, "gretapip6")) {
3785 return system_add_gre_tunnel(name, "ip6gretap", link, tb, true);
3786 #ifdef IFLA_VTI_MAX
3787 } else if (!strcmp(str, "vtiip")) {
3788 return system_add_vti_tunnel(name, "vti", link, tb, false);
3789 } else if (!strcmp(str, "vtiip6")) {
3790 return system_add_vti_tunnel(name, "vti6", link, tb, true);
3791 #endif
3792 #ifdef IFLA_XFRM_MAX
3793 } else if (!strcmp(str, "xfrm")) {
3794 return system_add_xfrm_tunnel(name, "xfrm", link, tb);
3795 #endif
3796 #ifdef IFLA_VXLAN_MAX
3797 } else if(!strcmp(str, "vxlan")) {
3798 return system_add_vxlan(name, link, tb, false);
3799 } else if(!strcmp(str, "vxlan6")) {
3800 return system_add_vxlan(name, link, tb, true);
3801 #endif
3802 #endif
3803 } else if (!strcmp(str, "ipip")) {
3804 return system_add_proto_tunnel(name, IPPROTO_IPIP, link, tb);
3805 }
3806 else
3807 return -EINVAL;
3808
3809 return 0;
3810 }