Add myself to header for IPv6 work
[project/netifd.git] / interface-ip.c
1 /*
2 * netifd - network interface daemon
3 * Copyright (C) 2012 Felix Fietkau <nbd@openwrt.org>
4 * Copyright (C) 2012 Steven Barth <steven@midlink.org>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2
8 * as published by the Free Software Foundation
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 */
15 #include <string.h>
16 #include <stdlib.h>
17 #include <stdio.h>
18 #include <unistd.h>
19
20 #include <arpa/inet.h>
21
22 #include "netifd.h"
23 #include "device.h"
24 #include "interface.h"
25 #include "interface-ip.h"
26 #include "proto.h"
27 #include "ubus.h"
28 #include "system.h"
29
30 enum {
31 ROUTE_INTERFACE,
32 ROUTE_TARGET,
33 ROUTE_MASK,
34 ROUTE_GATEWAY,
35 ROUTE_METRIC,
36 ROUTE_MTU,
37 ROUTE_VALID,
38 ROUTE_TABLE,
39 __ROUTE_MAX
40 };
41
42 static const struct blobmsg_policy route_attr[__ROUTE_MAX] = {
43 [ROUTE_INTERFACE] = { .name = "interface", .type = BLOBMSG_TYPE_STRING },
44 [ROUTE_TARGET] = { .name = "target", .type = BLOBMSG_TYPE_STRING },
45 [ROUTE_MASK] = { .name = "netmask", .type = BLOBMSG_TYPE_STRING },
46 [ROUTE_GATEWAY] = { .name = "gateway", .type = BLOBMSG_TYPE_STRING },
47 [ROUTE_METRIC] = { .name = "metric", .type = BLOBMSG_TYPE_INT32 },
48 [ROUTE_MTU] = { .name = "mtu", .type = BLOBMSG_TYPE_INT32 },
49 [ROUTE_TABLE] = { .name = "table", .type = BLOBMSG_TYPE_STRING },
50 [ROUTE_VALID] = { .name = "valid", .type = BLOBMSG_TYPE_INT32 },
51 };
52
53 const struct config_param_list route_attr_list = {
54 .n_params = __ROUTE_MAX,
55 .params = route_attr,
56 };
57
58
59 struct list_head prefixes = LIST_HEAD_INIT(prefixes);
60 static struct device_prefix *ula_prefix = NULL;
61 static struct uloop_timeout valid_until_timeout;
62
63
64 static void
65 clear_if_addr(union if_addr *a, int mask)
66 {
67 int m_bytes = (mask + 7) / 8;
68 uint8_t m_clear = (1 << (m_bytes * 8 - mask)) - 1;
69 uint8_t *p = (uint8_t *) a;
70
71 if (m_bytes < sizeof(a))
72 memset(p + m_bytes, 0, sizeof(a) - m_bytes);
73
74 p[m_bytes - 1] &= ~m_clear;
75 }
76
77 static bool
78 match_if_addr(union if_addr *a1, union if_addr *a2, int mask)
79 {
80 union if_addr *p1, *p2;
81
82 p1 = alloca(sizeof(*a1));
83 p2 = alloca(sizeof(*a2));
84
85 memcpy(p1, a1, sizeof(*a1));
86 clear_if_addr(p1, mask);
87 memcpy(p2, a2, sizeof(*a2));
88 clear_if_addr(p2, mask);
89
90 return !memcmp(p1, p2, sizeof(*p1));
91 }
92
93 static bool
94 __find_ip_addr_target(struct interface_ip_settings *ip, union if_addr *a, bool v6)
95 {
96 struct device_addr *addr;
97
98 vlist_for_each_element(&ip->addr, addr, node) {
99 if (!addr->enabled)
100 continue;
101
102 if (v6 != ((addr->flags & DEVADDR_FAMILY) == DEVADDR_INET6))
103 continue;
104
105 if (!match_if_addr(&addr->addr, a, addr->mask))
106 continue;
107
108 return true;
109 }
110
111 return false;
112 }
113
114 static void
115 __find_ip_route_target(struct interface_ip_settings *ip, union if_addr *a,
116 bool v6, struct device_route **res)
117 {
118 struct device_route *route;
119
120 vlist_for_each_element(&ip->route, route, node) {
121 if (!route->enabled)
122 continue;
123
124 if (v6 != ((route->flags & DEVADDR_FAMILY) == DEVADDR_INET6))
125 continue;
126
127 if (!match_if_addr(&route->addr, a, route->mask))
128 continue;
129
130 if (route->flags & DEVROUTE_TABLE)
131 continue;
132
133 if (!*res || route->mask < (*res)->mask)
134 *res = route;
135 }
136 }
137
138 static bool
139 interface_ip_find_addr_target(struct interface *iface, union if_addr *a, bool v6)
140 {
141 return __find_ip_addr_target(&iface->proto_ip, a, v6) ||
142 __find_ip_addr_target(&iface->config_ip, a, v6);
143 }
144
145 static void
146 interface_ip_find_route_target(struct interface *iface, union if_addr *a,
147 bool v6, struct device_route **route)
148 {
149 __find_ip_route_target(&iface->proto_ip, a, v6, route);
150 __find_ip_route_target(&iface->config_ip, a, v6, route);
151 }
152
153 struct interface *
154 interface_ip_add_target_route(union if_addr *addr, bool v6)
155 {
156 struct interface *iface;
157 struct device_route *route, *r_next = NULL;
158 bool defaultroute_target = false;
159 int addrsize = v6 ? sizeof(addr->in6) : sizeof(addr->in);
160
161 route = calloc(1, sizeof(*route));
162 if (!route)
163 return NULL;
164
165 route->flags = v6 ? DEVADDR_INET6 : DEVADDR_INET4;
166 route->mask = v6 ? 128 : 32;
167 if (memcmp(&route->addr, addr, addrsize) == 0)
168 defaultroute_target = true;
169 else
170 memcpy(&route->addr, addr, addrsize);
171
172 vlist_for_each_element(&interfaces, iface, node) {
173 /* look for locally addressable target first */
174 if (interface_ip_find_addr_target(iface, addr, v6))
175 goto done;
176
177 /* do not stop at the first route, let the lookup compare
178 * masks to find the best match */
179 interface_ip_find_route_target(iface, addr, v6, &r_next);
180 }
181
182 if (!r_next) {
183 free(route);
184 return NULL;
185 }
186
187 iface = r_next->iface;
188 memcpy(&route->nexthop, &r_next->nexthop, sizeof(route->nexthop));
189 route->mtu = r_next->mtu;
190 route->metric = r_next->metric;
191
192 done:
193 route->iface = iface;
194 if (defaultroute_target)
195 free(route);
196 else
197 vlist_add(&iface->host_routes, &route->node, &route->flags);
198 return iface;
199 }
200
201 void
202 interface_ip_add_route(struct interface *iface, struct blob_attr *attr, bool v6)
203 {
204 struct interface_ip_settings *ip;
205 struct blob_attr *tb[__ROUTE_MAX], *cur;
206 struct device_route *route;
207 int af = v6 ? AF_INET6 : AF_INET;
208
209 blobmsg_parse(route_attr, __ROUTE_MAX, tb, blobmsg_data(attr), blobmsg_data_len(attr));
210
211 if (!iface) {
212 if ((cur = tb[ROUTE_INTERFACE]) == NULL)
213 return;
214
215 iface = vlist_find(&interfaces, blobmsg_data(cur), iface, node);
216 if (!iface)
217 return;
218
219 ip = &iface->config_ip;
220 } else {
221 ip = &iface->proto_ip;
222 }
223
224 route = calloc(1, sizeof(*route));
225 if (!route)
226 return;
227
228 route->flags = v6 ? DEVADDR_INET6 : DEVADDR_INET4;
229 route->mask = v6 ? 128 : 32;
230 if ((cur = tb[ROUTE_MASK]) != NULL) {
231 route->mask = parse_netmask_string(blobmsg_data(cur), v6);
232 if (route->mask > (v6 ? 128 : 32))
233 goto error;
234 }
235
236 if ((cur = tb[ROUTE_TARGET]) != NULL) {
237 if (!parse_ip_and_netmask(af, blobmsg_data(cur), &route->addr, &route->mask)) {
238 DPRINTF("Failed to parse route target: %s\n", (char *) blobmsg_data(cur));
239 goto error;
240 }
241 }
242
243 if ((cur = tb[ROUTE_GATEWAY]) != NULL) {
244 if (!inet_pton(af, blobmsg_data(cur), &route->nexthop)) {
245 DPRINTF("Failed to parse route gateway: %s\n", (char *) blobmsg_data(cur));
246 goto error;
247 }
248 }
249
250 if ((cur = tb[ROUTE_METRIC]) != NULL) {
251 route->metric = blobmsg_get_u32(cur);
252 route->flags |= DEVROUTE_METRIC;
253 }
254
255 if ((cur = tb[ROUTE_MTU]) != NULL) {
256 route->mtu = blobmsg_get_u32(cur);
257 route->flags |= DEVROUTE_MTU;
258 }
259
260 if ((cur = tb[ROUTE_TABLE]) != NULL) {
261 if (!system_resolve_rt_table(blobmsg_data(cur), &route->table)) {
262 DPRINTF("Failed to resolve routing table: %s\n", (char *) blobmsg_data(cur));
263 goto error;
264 }
265
266 if (route->table)
267 route->flags |= DEVROUTE_TABLE;
268 }
269
270 if ((cur = tb[ROUTE_VALID]) != NULL)
271 route->valid_until = system_get_rtime() + blobmsg_get_u32(cur);
272
273 vlist_add(&ip->route, &route->node, &route->flags);
274 return;
275
276 error:
277 free(route);
278 }
279
280 static int
281 addr_cmp(const void *k1, const void *k2, void *ptr)
282 {
283 return memcmp(k1, k2, sizeof(struct device_addr) -
284 offsetof(struct device_addr, flags));
285 }
286
287 static int
288 route_cmp(const void *k1, const void *k2, void *ptr)
289 {
290 return memcmp(k1, k2, sizeof(struct device_route) -
291 offsetof(struct device_route, flags));
292 }
293
294 static int
295 prefix_cmp(const void *k1, const void *k2, void *ptr)
296 {
297 return memcmp(k1, k2, sizeof(struct device_prefix) -
298 offsetof(struct device_prefix, addr));
299 }
300
301 static void
302 interface_handle_subnet_route(struct interface *iface, struct device_addr *addr, bool add)
303 {
304 struct device *dev = iface->l3_dev.dev;
305 struct device_route route;
306
307 memset(&route, 0, sizeof(route));
308 route.iface = iface;
309 route.flags = addr->flags;
310 route.mask = addr->mask;
311 memcpy(&route.addr, &addr->addr, sizeof(route.addr));
312 clear_if_addr(&route.addr, route.mask);
313
314 if (add) {
315 route.flags |= DEVADDR_KERNEL;
316 system_del_route(dev, &route);
317
318 if (!(addr->flags & DEVADDR_OFFLINK)) {
319 route.flags &= ~DEVADDR_KERNEL;
320 route.metric = iface->metric;
321 system_add_route(dev, &route);
322 }
323 } else {
324 if (!(addr->flags & DEVADDR_OFFLINK))
325 system_del_route(dev, &route);
326 }
327 }
328
329 static void
330 interface_update_proto_addr(struct vlist_tree *tree,
331 struct vlist_node *node_new,
332 struct vlist_node *node_old)
333 {
334 struct interface_ip_settings *ip;
335 struct interface *iface;
336 struct device *dev;
337 struct device_addr *a_new = NULL, *a_old = NULL;
338 bool keep = false;
339
340 ip = container_of(tree, struct interface_ip_settings, addr);
341 iface = ip->iface;
342 dev = iface->l3_dev.dev;
343
344 if (node_new) {
345 a_new = container_of(node_new, struct device_addr, node);
346
347 if ((a_new->flags & DEVADDR_FAMILY) == DEVADDR_INET4 &&
348 !a_new->broadcast) {
349
350 uint32_t mask = ~0;
351 uint32_t *a = (uint32_t *) &a_new->addr;
352
353 mask >>= a_new->mask;
354 a_new->broadcast = *a | htonl(mask);
355 }
356 }
357
358 if (node_old)
359 a_old = container_of(node_old, struct device_addr, node);
360
361 if (a_new && a_old) {
362 keep = true;
363
364 if (a_old->flags != a_new->flags ||
365 a_old->valid_until != a_new->valid_until ||
366 a_old->preferred_until != a_new->preferred_until)
367 keep = false;
368
369 if ((a_new->flags & DEVADDR_FAMILY) == DEVADDR_INET4 &&
370 a_new->broadcast != a_old->broadcast)
371 keep = false;
372 }
373
374 if (node_old) {
375 if (!(a_old->flags & DEVADDR_EXTERNAL) && a_old->enabled && !keep) {
376 interface_handle_subnet_route(iface, a_old, false);
377 system_del_address(dev, a_old);
378 }
379 free(a_old);
380 }
381
382 if (node_new) {
383 a_new->enabled = true;
384 if (!(a_new->flags & DEVADDR_EXTERNAL) && !keep) {
385 system_add_address(dev, a_new);
386 if ((a_new->flags & DEVADDR_OFFLINK) || iface->metric)
387 interface_handle_subnet_route(iface, a_new, true);
388 }
389 }
390 }
391
392 static bool
393 enable_route(struct interface_ip_settings *ip, struct device_route *route)
394 {
395 if (ip->no_defaultroute && !route->mask)
396 return false;
397
398 return ip->enabled;
399 }
400
401 static void
402 interface_update_proto_route(struct vlist_tree *tree,
403 struct vlist_node *node_new,
404 struct vlist_node *node_old)
405 {
406 struct interface_ip_settings *ip;
407 struct interface *iface;
408 struct device *dev;
409 struct device_route *route_old, *route_new;
410 bool keep = false;
411
412 ip = container_of(tree, struct interface_ip_settings, route);
413 iface = ip->iface;
414 dev = iface->l3_dev.dev;
415
416 route_old = container_of(node_old, struct device_route, node);
417 route_new = container_of(node_new, struct device_route, node);
418
419 if (node_old && node_new)
420 keep = !memcmp(&route_old->nexthop, &route_new->nexthop, sizeof(route_old->nexthop));
421
422 if (node_old) {
423 if (!(route_old->flags & DEVADDR_EXTERNAL) && route_old->enabled && !keep)
424 system_del_route(dev, route_old);
425 free(route_old);
426 }
427
428 if (node_new) {
429 bool _enabled = enable_route(ip, route_new);
430
431 if (!(route_new->flags & DEVROUTE_METRIC))
432 route_new->metric = iface->metric;
433
434 if (!(route_new->flags & DEVADDR_EXTERNAL) && !keep && _enabled)
435 system_add_route(dev, route_new);
436
437 route_new->iface = iface;
438 route_new->enabled = _enabled;
439 }
440 }
441
442 static void
443 interface_update_host_route(struct vlist_tree *tree,
444 struct vlist_node *node_new,
445 struct vlist_node *node_old)
446 {
447 struct interface *iface;
448 struct device *dev;
449 struct device_route *route_old, *route_new;
450
451 iface = container_of(tree, struct interface, host_routes);
452 dev = iface->l3_dev.dev;
453
454 route_old = container_of(node_old, struct device_route, node);
455 route_new = container_of(node_new, struct device_route, node);
456
457 if (node_old) {
458 system_del_route(dev, route_old);
459 free(route_old);
460 }
461
462 if (node_new)
463 system_add_route(dev, route_new);
464 }
465
466
467 static void
468 interface_set_prefix_address(struct device_prefix_assignment *assignment,
469 const struct device_prefix *prefix, struct interface *iface, bool add)
470 {
471 const struct interface *uplink = prefix->iface;
472 if (!iface->l3_dev.dev)
473 return;
474
475 struct device *l3_downlink = iface->l3_dev.dev;
476
477 struct device_addr addr;
478 memset(&addr, 0, sizeof(addr));
479 addr.addr.in6 = prefix->addr;
480 addr.addr.in6.s6_addr32[1] |= htonl(assignment->assigned);
481 addr.addr.in6.s6_addr[15] += 1;
482 addr.mask = assignment->length;
483 addr.flags = DEVADDR_INET6;
484 addr.preferred_until = prefix->preferred_until;
485 addr.valid_until = prefix->valid_until;
486
487 if (!add && assignment->enabled) {
488 time_t now = system_get_rtime();
489 addr.preferred_until = now;
490 if (!addr.valid_until || addr.valid_until - now > 7200)
491 addr.valid_until = now + 7200;
492 system_add_address(l3_downlink, &addr);
493 assignment->enabled = false;
494 } else if (add && (iface->state == IFS_UP || iface->state == IFS_SETUP)) {
495 system_add_address(l3_downlink, &addr);
496 if (uplink && uplink->l3_dev.dev) {
497 int mtu = system_update_ipv6_mtu(
498 uplink->l3_dev.dev, 0);
499 if (mtu > 0)
500 system_update_ipv6_mtu(l3_downlink, mtu);
501 }
502 assignment->enabled = true;
503 }
504 }
505
506 static bool interface_prefix_assign(struct list_head *list,
507 struct device_prefix_assignment *assign)
508 {
509 int32_t current = 0, asize = (1 << (64 - assign->length)) - 1;
510 struct device_prefix_assignment *c;
511 list_for_each_entry(c, list, head) {
512 if (assign->assigned != -1) {
513 if (assign->assigned > current && assign->assigned + asize < c->assigned) {
514 list_add_tail(&assign->head, &c->head);
515 return true;
516 }
517 } else if (assign->assigned == -1) {
518 current = (current + asize) & (~asize);
519 if (current + asize < c->assigned) {
520 assign->assigned = current;
521 list_add_tail(&assign->head, &c->head);
522 return true;
523 }
524 }
525 current = (c->assigned + (1 << (64 - c->length)));
526 }
527 return false;
528 }
529
530 static void interface_update_prefix_assignments(struct device_prefix *prefix, bool setup)
531 {
532 struct device_prefix_assignment *c;
533 struct interface *iface;
534
535 // Delete all assignments
536 while (!list_empty(&prefix->assignments)) {
537 c = list_first_entry(&prefix->assignments,
538 struct device_prefix_assignment, head);
539 if ((iface = vlist_find(&interfaces, c->name, iface, node)))
540 interface_set_prefix_address(c, prefix, iface, false);
541 list_del(&c->head);
542 free(c);
543 }
544
545 if (!setup)
546 return;
547
548 // End-of-assignment sentinel
549 c = malloc(sizeof(*c) + 1);
550 c->assigned = 1 << (64 - prefix->length);
551 c->length = 64;
552 c->name[0] = 0;
553 list_add(&c->head, &prefix->assignments);
554
555 // Excluded prefix
556 if (prefix->excl_length > 0) {
557 const char name[] = "!excluded";
558 c = malloc(sizeof(*c) + sizeof(name));
559 c->assigned = ntohl(prefix->excl_addr.s6_addr32[1]) &
560 ((1 << (64 - prefix->length)) - 1);
561 c->length = prefix->excl_length;
562 memcpy(c->name, name, sizeof(name));
563 list_add(&c->head, &prefix->assignments);
564 }
565
566 struct list_head assign_later = LIST_HEAD_INIT(assign_later);
567 vlist_for_each_element(&interfaces, iface, node) {
568 if (iface->config_ip.assignment_length < 48 ||
569 iface->config_ip.assignment_length > 64)
570 continue;
571
572 size_t namelen = strlen(iface->name) + 1;
573 c = malloc(sizeof(*c) + namelen);
574 c->length = iface->config_ip.assignment_length;
575 c->assigned = iface->config_ip.assignment_hint;
576 c->enabled = false;
577 memcpy(c->name, iface->name, namelen);
578
579 // First process all custom assignments, put all others in later-list
580 if (c->assigned == -1 || !interface_prefix_assign(&prefix->assignments, c)) {
581 if (c->assigned != -1) {
582 c->assigned = -1;
583 netifd_log_message(L_WARNING, "Failed to assign requested subprefix "
584 "of size %hhu for %s, trying other\n", c->length, c->name);
585 }
586 list_add_tail(&c->head, &assign_later);
587 }
588 }
589
590 // Then try to assign all other + failed custom assignments
591 while (!list_empty(&assign_later)) {
592 c = list_first_entry(&assign_later, struct device_prefix_assignment, head);
593 list_del(&c->head);
594 if (!interface_prefix_assign(&prefix->assignments, c)) {
595 netifd_log_message(L_WARNING, "Failed to assign subprefix "
596 "of size %hhu for %s\n", c->length, c->name);
597 free(c);
598 }
599 }
600
601 list_for_each_entry(c, &prefix->assignments, head)
602 if ((iface = vlist_find(&interfaces, c->name, iface, node)))
603 interface_set_prefix_address(c, prefix, iface, true);
604 }
605
606
607 void interface_refresh_assignments(bool hint)
608 {
609 static bool refresh = false;
610 if (!hint && refresh) {
611 struct device_prefix *p;
612 list_for_each_entry(p, &prefixes, head)
613 interface_update_prefix_assignments(p, true);
614 }
615 refresh = hint;
616 }
617
618
619 static void
620 interface_update_prefix(struct vlist_tree *tree,
621 struct vlist_node *node_new,
622 struct vlist_node *node_old)
623 {
624 struct device_prefix *prefix_old, *prefix_new;
625 prefix_old = container_of(node_old, struct device_prefix, node);
626 prefix_new = container_of(node_new, struct device_prefix, node);
627
628 struct device_route route;
629 memset(&route, 0, sizeof(route));
630 route.flags = DEVADDR_INET6;
631 route.metric = INT32_MAX;
632 route.mask = (node_new) ? prefix_new->length : prefix_old->length;
633 route.addr.in6 = (node_new) ? prefix_new->addr : prefix_old->addr;
634
635 struct device_prefix_assignment *c;
636 struct interface *iface;
637
638 if (node_old && node_new) {
639 // Move assignments and refresh addresses to update valid times
640 list_splice_init(&prefix_old->assignments, &prefix_new->assignments);
641
642 list_for_each_entry(c, &prefix_new->assignments, head)
643 if ((iface = vlist_find(&interfaces, c->name, iface, node)))
644 interface_set_prefix_address(c, prefix_new, iface, true);
645 } else if (node_new) {
646 // Set null-route to avoid routing loops
647 system_add_route(NULL, &route);
648
649 INIT_LIST_HEAD(&prefix_new->assignments);
650 interface_update_prefix_assignments(prefix_new, true);
651 } else if (node_old) {
652 interface_update_prefix_assignments(prefix_old, false);
653
654 // Remove null-route
655 system_del_route(NULL, &route);
656 }
657
658 if (node_old) {
659 list_del(&prefix_old->head);
660 free(prefix_old);
661 }
662
663 if (node_new)
664 list_add(&prefix_new->head, &prefixes);
665
666 }
667
668 struct device_prefix*
669 interface_ip_add_device_prefix(struct interface *iface, struct in6_addr *addr,
670 uint8_t length, time_t valid_until, time_t preferred_until,
671 struct in6_addr *excl_addr, uint8_t excl_length)
672 {
673 struct device_prefix *prefix = calloc(1, sizeof(*prefix));
674 prefix->length = length;
675 prefix->addr = *addr;
676 prefix->preferred_until = preferred_until;
677 prefix->valid_until = valid_until;
678 prefix->iface = iface;
679
680 if (excl_addr) {
681 prefix->excl_addr = *excl_addr;
682 prefix->excl_length = excl_length;
683 }
684
685 if (iface)
686 vlist_add(&iface->proto_ip.prefix, &prefix->node, &prefix->addr);
687 else
688 interface_update_prefix(NULL, &prefix->node, NULL);
689
690 return prefix;
691 }
692
693 void
694 interface_ip_set_ula_prefix(const char *prefix)
695 {
696 char buf[INET6_ADDRSTRLEN + 4] = {0}, *saveptr;
697 if (prefix)
698 strncpy(buf, prefix, sizeof(buf) - 1);
699 char *prefixaddr = strtok_r(buf, "/", &saveptr);
700
701 struct in6_addr addr;
702 if (!prefixaddr || inet_pton(AF_INET6, prefixaddr, &addr) < 1) {
703 if (ula_prefix) {
704 interface_update_prefix(NULL, NULL, &ula_prefix->node);
705 ula_prefix = NULL;
706 }
707 return;
708 }
709
710 int length;
711 char *prefixlen = strtok_r(NULL, ",", &saveptr);
712 if (!prefixlen || (length = atoi(prefixlen)) < 1 || length > 64)
713 return;
714
715 if (!ula_prefix || !IN6_ARE_ADDR_EQUAL(&addr, &ula_prefix->addr) ||
716 ula_prefix->length != length) {
717 if (ula_prefix)
718 interface_update_prefix(NULL, NULL, &ula_prefix->node);
719
720 ula_prefix = interface_ip_add_device_prefix(NULL, &addr, length,
721 0, 0, NULL, 0);
722 }
723 }
724
725 void
726 interface_add_dns_server(struct interface_ip_settings *ip, const char *str)
727 {
728 struct dns_server *s;
729
730 s = calloc(1, sizeof(*s));
731 if (!s)
732 return;
733
734 s->af = AF_INET;
735 if (inet_pton(s->af, str, &s->addr.in))
736 goto add;
737
738 s->af = AF_INET6;
739 if (inet_pton(s->af, str, &s->addr.in))
740 goto add;
741
742 free(s);
743 return;
744
745 add:
746 D(INTERFACE, "Add IPv%c DNS server: %s\n",
747 s->af == AF_INET6 ? '6' : '4', str);
748 vlist_simple_add(&ip->dns_servers, &s->node);
749 }
750
751 void
752 interface_add_dns_server_list(struct interface_ip_settings *ip, struct blob_attr *list)
753 {
754 struct blob_attr *cur;
755 int rem;
756
757 blobmsg_for_each_attr(cur, list, rem) {
758 if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING)
759 continue;
760
761 if (!blobmsg_check_attr(cur, NULL))
762 continue;
763
764 interface_add_dns_server(ip, blobmsg_data(cur));
765 }
766 }
767
768 static void
769 interface_add_dns_search_domain(struct interface_ip_settings *ip, const char *str)
770 {
771 struct dns_search_domain *s;
772 int len = strlen(str);
773
774 s = calloc(1, sizeof(*s) + len + 1);
775 if (!s)
776 return;
777
778 D(INTERFACE, "Add DNS search domain: %s\n", str);
779 memcpy(s->name, str, len);
780 vlist_simple_add(&ip->dns_search, &s->node);
781 }
782
783 void
784 interface_add_dns_search_list(struct interface_ip_settings *ip, struct blob_attr *list)
785 {
786 struct blob_attr *cur;
787 int rem;
788
789 blobmsg_for_each_attr(cur, list, rem) {
790 if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING)
791 continue;
792
793 if (!blobmsg_check_attr(cur, NULL))
794 continue;
795
796 interface_add_dns_search_domain(ip, blobmsg_data(cur));
797 }
798 }
799
800 static void
801 write_resolv_conf_entries(FILE *f, struct interface_ip_settings *ip)
802 {
803 struct dns_server *s;
804 struct dns_search_domain *d;
805 const char *str;
806 char buf[INET6_ADDRSTRLEN];
807
808 vlist_simple_for_each_element(&ip->dns_servers, s, node) {
809 str = inet_ntop(s->af, &s->addr, buf, sizeof(buf));
810 if (!str)
811 continue;
812
813 fprintf(f, "nameserver %s\n", str);
814 }
815
816 vlist_simple_for_each_element(&ip->dns_search, d, node) {
817 fprintf(f, "search %s\n", d->name);
818 }
819 }
820
821 void
822 interface_write_resolv_conf(void)
823 {
824 struct interface *iface;
825 char *path = alloca(strlen(resolv_conf) + 5);
826 FILE *f;
827 uint32_t crcold, crcnew;
828
829 sprintf(path, "%s.tmp", resolv_conf);
830 unlink(path);
831 f = fopen(path, "w+");
832 if (!f) {
833 D(INTERFACE, "Failed to open %s for writing\n", path);
834 return;
835 }
836
837 vlist_for_each_element(&interfaces, iface, node) {
838 if (iface->state != IFS_UP)
839 continue;
840
841 if (vlist_simple_empty(&iface->proto_ip.dns_search) &&
842 vlist_simple_empty(&iface->proto_ip.dns_servers) &&
843 vlist_simple_empty(&iface->config_ip.dns_search) &&
844 vlist_simple_empty(&iface->config_ip.dns_servers))
845 continue;
846
847 fprintf(f, "# Interface %s\n", iface->name);
848 write_resolv_conf_entries(f, &iface->config_ip);
849 if (!iface->proto_ip.no_dns)
850 write_resolv_conf_entries(f, &iface->proto_ip);
851 }
852 fflush(f);
853 rewind(f);
854 crcnew = crc32_file(f);
855 fclose(f);
856
857 crcold = crcnew + 1;
858 f = fopen(resolv_conf, "r");
859 if (f) {
860 crcold = crc32_file(f);
861 fclose(f);
862 }
863
864 if (crcold == crcnew) {
865 unlink(path);
866 } else if (rename(path, resolv_conf) < 0) {
867 D(INTERFACE, "Failed to replace %s\n", resolv_conf);
868 unlink(path);
869 }
870 }
871
872 void interface_ip_set_enabled(struct interface_ip_settings *ip, bool enabled)
873 {
874 struct device_addr *addr;
875 struct device_route *route;
876 struct device *dev;
877
878 ip->enabled = enabled;
879 dev = ip->iface->l3_dev.dev;
880 if (!dev)
881 return;
882
883 vlist_for_each_element(&ip->addr, addr, node) {
884 if (addr->enabled == enabled)
885 continue;
886
887 if (enabled)
888 system_add_address(dev, addr);
889 else
890 system_del_address(dev, addr);
891 addr->enabled = enabled;
892 }
893
894 vlist_for_each_element(&ip->route, route, node) {
895 bool _enabled = enabled;
896
897 if (!enable_route(ip, route))
898 _enabled = false;
899
900 if (route->enabled == _enabled)
901 continue;
902
903 if (_enabled) {
904 if (!(route->flags & DEVROUTE_METRIC))
905 route->metric = ip->iface->metric;
906
907 system_add_route(dev, route);
908 } else
909 system_del_route(dev, route);
910 route->enabled = _enabled;
911 }
912
913 struct device_prefix *c;
914 struct device_prefix_assignment *a;
915 list_for_each_entry(c, &prefixes, head)
916 list_for_each_entry(a, &c->assignments, head)
917 if (!strcmp(a->name, ip->iface->name))
918 interface_set_prefix_address(a, c, ip->iface, enabled);
919 }
920
921 void
922 interface_ip_update_start(struct interface_ip_settings *ip)
923 {
924 if (ip != &ip->iface->config_ip) {
925 vlist_simple_update(&ip->dns_servers);
926 vlist_simple_update(&ip->dns_search);
927 }
928 vlist_update(&ip->route);
929 vlist_update(&ip->addr);
930 vlist_update(&ip->prefix);
931 }
932
933 void
934 interface_ip_update_complete(struct interface_ip_settings *ip)
935 {
936 vlist_simple_flush(&ip->dns_servers);
937 vlist_simple_flush(&ip->dns_search);
938 vlist_flush(&ip->route);
939 vlist_flush(&ip->addr);
940 vlist_flush(&ip->prefix);
941 interface_write_resolv_conf();
942 }
943
944 void
945 interface_ip_flush(struct interface_ip_settings *ip)
946 {
947 if (ip == &ip->iface->proto_ip)
948 vlist_flush_all(&ip->iface->host_routes);
949 vlist_simple_flush_all(&ip->dns_servers);
950 vlist_simple_flush_all(&ip->dns_search);
951 vlist_flush_all(&ip->route);
952 vlist_flush_all(&ip->addr);
953 vlist_flush_all(&ip->prefix);
954 }
955
956 static void
957 __interface_ip_init(struct interface_ip_settings *ip, struct interface *iface)
958 {
959 ip->iface = iface;
960 ip->enabled = true;
961 vlist_simple_init(&ip->dns_search, struct dns_search_domain, node);
962 vlist_simple_init(&ip->dns_servers, struct dns_server, node);
963 vlist_init(&ip->route, route_cmp, interface_update_proto_route);
964 vlist_init(&ip->addr, addr_cmp, interface_update_proto_addr);
965 vlist_init(&ip->prefix, prefix_cmp, interface_update_prefix);
966 }
967
968 void
969 interface_ip_init(struct interface *iface)
970 {
971 __interface_ip_init(&iface->proto_ip, iface);
972 __interface_ip_init(&iface->config_ip, iface);
973 vlist_init(&iface->host_routes, route_cmp, interface_update_host_route);
974
975 }
976
977 static void
978 interface_ip_valid_until_handler(struct uloop_timeout *t)
979 {
980 time_t now = system_get_rtime();
981 struct interface *iface;
982 vlist_for_each_element(&interfaces, iface, node) {
983 if (iface->state != IFS_UP)
984 continue;
985
986 struct device_addr *addr, *addrp;
987 struct device_route *route, *routep;
988 struct device_prefix *pref, *prefp;
989
990 vlist_for_each_element_safe(&iface->proto_ip.addr, addr, node, addrp)
991 if (addr->valid_until && addr->valid_until < now)
992 vlist_delete(&iface->proto_ip.addr, &addr->node);
993
994 vlist_for_each_element_safe(&iface->proto_ip.route, route, node, routep)
995 if (route->valid_until && route->valid_until < now)
996 vlist_delete(&iface->proto_ip.route, &route->node);
997
998 vlist_for_each_element_safe(&iface->proto_ip.prefix, pref, node, prefp)
999 if (pref->valid_until && pref->valid_until < now)
1000 vlist_delete(&iface->proto_ip.prefix, &pref->node);
1001
1002 }
1003
1004 uloop_timeout_set(t, 1000);
1005 }
1006
1007 static void __init
1008 interface_ip_init_worker(void)
1009 {
1010 valid_until_timeout.cb = interface_ip_valid_until_handler;
1011 uloop_timeout_set(&valid_until_timeout, 1000);
1012 }