2 * netifd - network interface daemon
3 * Copyright (C) 2012 Felix Fietkau <nbd@openwrt.org>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2
7 * as published by the Free Software Foundation
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
19 #include <arpa/inet.h>
23 #include "interface.h"
24 #include "interface-ip.h"
39 static const struct blobmsg_policy route_attr[__ROUTE_MAX] = {
40 [ROUTE_INTERFACE] = { .name = "interface", .type = BLOBMSG_TYPE_STRING },
41 [ROUTE_TARGET] = { .name = "target", .type = BLOBMSG_TYPE_STRING },
42 [ROUTE_MASK] = { .name = "netmask", .type = BLOBMSG_TYPE_STRING },
43 [ROUTE_GATEWAY] = { .name = "gateway", .type = BLOBMSG_TYPE_STRING },
44 [ROUTE_METRIC] = { .name = "metric", .type = BLOBMSG_TYPE_INT32 },
45 [ROUTE_MTU] = { .name = "mtu", .type = BLOBMSG_TYPE_INT32 },
48 const struct config_param_list route_attr_list = {
49 .n_params = __ROUTE_MAX,
54 struct list_head prefixes = LIST_HEAD_INIT(prefixes);
55 static struct device_prefix *ula_prefix = NULL;
59 clear_if_addr(union if_addr *a, int mask)
61 int m_bytes = (mask + 7) / 8;
62 uint8_t m_clear = (1 << (m_bytes * 8 - mask)) - 1;
63 uint8_t *p = (uint8_t *) a;
65 if (m_bytes < sizeof(a))
66 memset(p + m_bytes, 0, sizeof(a) - m_bytes);
68 p[m_bytes - 1] &= ~m_clear;
72 match_if_addr(union if_addr *a1, union if_addr *a2, int mask)
74 union if_addr *p1, *p2;
76 p1 = alloca(sizeof(*a1));
77 p2 = alloca(sizeof(*a2));
79 memcpy(p1, a1, sizeof(*a1));
80 clear_if_addr(p1, mask);
81 memcpy(p2, a2, sizeof(*a2));
82 clear_if_addr(p2, mask);
84 return !memcmp(p1, p2, sizeof(*p1));
88 __find_ip_addr_target(struct interface_ip_settings *ip, union if_addr *a, bool v6)
90 struct device_addr *addr;
92 vlist_for_each_element(&ip->addr, addr, node) {
96 if (v6 != ((addr->flags & DEVADDR_FAMILY) == DEVADDR_INET6))
99 if (!match_if_addr(&addr->addr, a, addr->mask))
109 __find_ip_route_target(struct interface_ip_settings *ip, union if_addr *a,
110 bool v6, struct device_route **res)
112 struct device_route *route;
114 vlist_for_each_element(&ip->route, route, node) {
118 if (v6 != ((route->flags & DEVADDR_FAMILY) == DEVADDR_INET6))
121 if (!match_if_addr(&route->addr, a, route->mask))
124 if (!*res || route->mask < (*res)->mask)
130 interface_ip_find_addr_target(struct interface *iface, union if_addr *a, bool v6)
132 return __find_ip_addr_target(&iface->proto_ip, a, v6) ||
133 __find_ip_addr_target(&iface->config_ip, a, v6);
137 interface_ip_find_route_target(struct interface *iface, union if_addr *a,
138 bool v6, struct device_route **route)
140 __find_ip_route_target(&iface->proto_ip, a, v6, route);
141 __find_ip_route_target(&iface->config_ip, a, v6, route);
145 interface_ip_add_target_route(union if_addr *addr, bool v6)
147 struct interface *iface;
148 struct device_route *route, *r_next = NULL;
149 bool defaultroute_target = false;
150 int addrsize = v6 ? sizeof(addr->in6) : sizeof(addr->in);
152 route = calloc(1, sizeof(*route));
156 route->flags = v6 ? DEVADDR_INET6 : DEVADDR_INET4;
157 route->mask = v6 ? 128 : 32;
158 if (memcmp(&route->addr, addr, addrsize) == 0)
159 defaultroute_target = true;
161 memcpy(&route->addr, addr, addrsize);
163 vlist_for_each_element(&interfaces, iface, node) {
164 /* look for locally addressable target first */
165 if (interface_ip_find_addr_target(iface, addr, v6))
168 /* do not stop at the first route, let the lookup compare
169 * masks to find the best match */
170 interface_ip_find_route_target(iface, addr, v6, &r_next);
178 iface = r_next->iface;
179 memcpy(&route->nexthop, &r_next->nexthop, sizeof(route->nexthop));
180 route->mtu = r_next->mtu;
181 route->metric = r_next->metric;
184 route->iface = iface;
185 if (defaultroute_target)
188 vlist_add(&iface->host_routes, &route->node, &route->flags);
193 interface_ip_add_route(struct interface *iface, struct blob_attr *attr, bool v6)
195 struct interface_ip_settings *ip;
196 struct blob_attr *tb[__ROUTE_MAX], *cur;
197 struct device_route *route;
198 int af = v6 ? AF_INET6 : AF_INET;
200 blobmsg_parse(route_attr, __ROUTE_MAX, tb, blobmsg_data(attr), blobmsg_data_len(attr));
203 if ((cur = tb[ROUTE_INTERFACE]) == NULL)
206 iface = vlist_find(&interfaces, blobmsg_data(cur), iface, node);
210 ip = &iface->config_ip;
212 ip = &iface->proto_ip;
215 route = calloc(1, sizeof(*route));
219 route->flags = v6 ? DEVADDR_INET6 : DEVADDR_INET4;
220 route->mask = v6 ? 128 : 32;
221 if ((cur = tb[ROUTE_MASK]) != NULL) {
222 route->mask = parse_netmask_string(blobmsg_data(cur), v6);
223 if (route->mask > (v6 ? 128 : 32))
227 if ((cur = tb[ROUTE_TARGET]) != NULL) {
228 if (!parse_ip_and_netmask(af, blobmsg_data(cur), &route->addr, &route->mask)) {
229 DPRINTF("Failed to parse route target: %s\n", (char *) blobmsg_data(cur));
234 if ((cur = tb[ROUTE_GATEWAY]) != NULL) {
235 if (!inet_pton(af, blobmsg_data(cur), &route->nexthop)) {
236 DPRINTF("Failed to parse route gateway: %s\n", (char *) blobmsg_data(cur));
241 if ((cur = tb[ROUTE_METRIC]) != NULL) {
242 route->metric = blobmsg_get_u32(cur);
243 route->flags |= DEVROUTE_METRIC;
246 if ((cur = tb[ROUTE_MTU]) != NULL) {
247 route->mtu = blobmsg_get_u32(cur);
248 route->flags |= DEVROUTE_MTU;
251 vlist_add(&ip->route, &route->node, &route->flags);
259 addr_cmp(const void *k1, const void *k2, void *ptr)
261 return memcmp(k1, k2, sizeof(struct device_addr) -
262 offsetof(struct device_addr, flags));
266 route_cmp(const void *k1, const void *k2, void *ptr)
268 return memcmp(k1, k2, sizeof(struct device_route) -
269 offsetof(struct device_route, flags));
273 prefix_cmp(const void *k1, const void *k2, void *ptr)
275 return memcmp(k1, k2, sizeof(struct device_prefix) -
276 offsetof(struct device_prefix, addr));
280 prefix_assignment_cmp(const void *k1, const void *k2, void *ptr)
282 return strcmp((const char*)k1, (const char*)k2);
286 interface_handle_subnet_route(struct interface *iface, struct device_addr *addr, bool add)
288 struct device *dev = iface->l3_dev.dev;
289 struct device_route route;
291 memset(&route, 0, sizeof(route));
293 route.flags = addr->flags;
294 route.mask = addr->mask;
295 memcpy(&route.addr, &addr->addr, sizeof(route.addr));
296 clear_if_addr(&route.addr, route.mask);
299 route.flags |= DEVADDR_KERNEL;
300 system_del_route(dev, &route);
302 route.flags &= ~DEVADDR_KERNEL;
303 route.metric = iface->metric;
304 system_add_route(dev, &route);
306 system_del_route(dev, &route);
311 interface_update_proto_addr(struct vlist_tree *tree,
312 struct vlist_node *node_new,
313 struct vlist_node *node_old)
315 struct interface_ip_settings *ip;
316 struct interface *iface;
318 struct device_addr *a_new = NULL, *a_old = NULL;
321 ip = container_of(tree, struct interface_ip_settings, addr);
323 dev = iface->l3_dev.dev;
326 a_new = container_of(node_new, struct device_addr, node);
328 if ((a_new->flags & DEVADDR_FAMILY) == DEVADDR_INET4 &&
332 uint32_t *a = (uint32_t *) &a_new->addr;
334 mask >>= a_new->mask;
335 a_new->broadcast = *a | htonl(mask);
340 a_old = container_of(node_old, struct device_addr, node);
342 if (a_new && a_old) {
345 if (a_old->flags != a_new->flags)
348 if ((a_new->flags & DEVADDR_FAMILY) == DEVADDR_INET4 &&
349 a_new->broadcast != a_old->broadcast)
354 if (!(a_old->flags & DEVADDR_EXTERNAL) && a_old->enabled && !keep) {
355 interface_handle_subnet_route(iface, a_old, false);
356 system_del_address(dev, a_old);
362 a_new->enabled = true;
363 if (!(a_new->flags & DEVADDR_EXTERNAL) && !keep) {
364 system_add_address(dev, a_new);
366 interface_handle_subnet_route(iface, a_new, true);
372 enable_route(struct interface_ip_settings *ip, struct device_route *route)
374 if (ip->no_defaultroute && !route->mask)
381 interface_update_proto_route(struct vlist_tree *tree,
382 struct vlist_node *node_new,
383 struct vlist_node *node_old)
385 struct interface_ip_settings *ip;
386 struct interface *iface;
388 struct device_route *route_old, *route_new;
391 ip = container_of(tree, struct interface_ip_settings, route);
393 dev = iface->l3_dev.dev;
395 route_old = container_of(node_old, struct device_route, node);
396 route_new = container_of(node_new, struct device_route, node);
398 if (node_old && node_new)
399 keep = !memcmp(&route_old->nexthop, &route_new->nexthop, sizeof(route_old->nexthop));
402 if (!(route_old->flags & DEVADDR_EXTERNAL) && route_old->enabled && !keep)
403 system_del_route(dev, route_old);
408 bool _enabled = enable_route(ip, route_new);
410 if (!(route_new->flags & DEVROUTE_METRIC))
411 route_new->metric = iface->metric;
413 if (!(route_new->flags & DEVADDR_EXTERNAL) && !keep && _enabled)
414 system_add_route(dev, route_new);
416 route_new->iface = iface;
417 route_new->enabled = _enabled;
422 interface_update_host_route(struct vlist_tree *tree,
423 struct vlist_node *node_new,
424 struct vlist_node *node_old)
426 struct interface *iface;
428 struct device_route *route_old, *route_new;
430 iface = container_of(tree, struct interface, host_routes);
431 dev = iface->l3_dev.dev;
433 route_old = container_of(node_old, struct device_route, node);
434 route_new = container_of(node_new, struct device_route, node);
437 system_del_route(dev, route_old);
442 system_add_route(dev, route_new);
447 interface_set_prefix_address(struct interface *iface, bool add,
448 struct device_prefix_assignment *assignment)
450 struct interface *uplink = assignment->prefix->iface;
451 if (!iface->l3_dev.dev)
454 struct device *l3_downlink = iface->l3_dev.dev;
456 struct device_addr addr;
457 memset(&addr, 0, sizeof(addr));
458 addr.addr.in6 = assignment->addr;
459 addr.mask = assignment->length;
460 addr.flags = DEVADDR_INET6;
461 addr.preferred_until = assignment->prefix->preferred_until;
462 addr.valid_until = assignment->prefix->valid_until;
465 if (assignment->enabled)
466 system_del_address(l3_downlink, &addr);
468 system_add_address(l3_downlink, &addr);
470 if (uplink && uplink->l3_dev.dev) {
471 int mtu = system_update_ipv6_mtu(
472 uplink->l3_dev.dev, 0);
474 system_update_ipv6_mtu(l3_downlink, mtu);
477 assignment->enabled = add;
482 interface_update_prefix_assignments(struct vlist_tree *tree,
483 struct vlist_node *node_new,
484 struct vlist_node *node_old)
486 struct device_prefix_assignment *old, *new;
487 old = container_of(node_old, struct device_prefix_assignment, node);
488 new = container_of(node_new, struct device_prefix_assignment, node);
490 // Assignments persist across interface reloads etc.
491 // so use indirection to avoid dangling pointers
492 struct interface *iface = vlist_find(&interfaces,
493 (node_new) ? new->name : old->name, iface, node);
495 if (node_old && node_new) {
496 new->addr = old->addr;
497 new->length = old->length;
498 } else if (node_old) {
500 interface_set_prefix_address(iface, false, old);
503 } else if (node_new) {
504 struct device_prefix *prefix = new->prefix;
505 uint64_t want = 1ULL << (64 - new->length);
506 prefix->avail &= ~(want - 1);
507 prefix->avail -= want;
510 uint64_t assigned = ~prefix->avail;
511 assigned &= (1ULL << (64 - prefix->length)) - 1;
512 assigned &= ~(want - 1);
515 new->addr = prefix->addr;
516 new->addr.s6_addr32[0] |=
517 htonl(assigned >> 32);
518 new->addr.s6_addr32[1] |=
519 htonl(assigned & 0xffffffffU);
520 new->addr.s6_addr[15] += 1;
523 if (node_new && (iface->state == IFS_UP || iface->state == IFS_SETUP))
524 interface_set_prefix_address(iface, true, new);
529 interface_ip_set_prefix_assignment(struct device_prefix *prefix,
530 struct interface *iface, uint8_t length)
532 if (!length || length > 64) {
533 struct device_prefix_assignment *assignment = vlist_find(
534 prefix->assignments, &iface, assignment, node);
536 interface_set_prefix_address(iface, false, assignment);
538 uint8_t length = iface->proto_ip.assignment_length;
539 uint64_t want = 1ULL << (64 - length);
540 if (prefix->avail < want && prefix->avail > 0) {
542 want = 1ULL << (64 - ++length);
543 } while (want > prefix->avail);
546 if (prefix->avail < want)
550 struct device_prefix_assignment *assignment = calloc(1, sizeof(*assignment));
551 assignment->prefix = prefix;
552 assignment->length = length;
553 assignment->name = strdup(iface->name);
555 vlist_add(prefix->assignments, &assignment->node, assignment->name);
560 interface_update_prefix(struct vlist_tree *tree,
561 struct vlist_node *node_new,
562 struct vlist_node *node_old)
564 struct device_prefix *prefix_old, *prefix_new;
565 prefix_old = container_of(node_old, struct device_prefix, node);
566 prefix_new = container_of(node_new, struct device_prefix, node);
568 struct device_route route;
569 memset(&route, 0, sizeof(route));
570 route.flags = DEVADDR_INET6;
571 route.metric = INT32_MAX;
572 route.mask = (node_new) ? prefix_new->length : prefix_old->length;
573 route.addr.in6 = (node_new) ? prefix_new->addr : prefix_old->addr;
575 if (node_old && node_new) {
576 prefix_new->avail = prefix_old->avail;
577 prefix_new->assignments = prefix_old->assignments;
578 prefix_old->assignments = NULL;
580 // Update all assignments
581 struct device_prefix_assignment *assignment;
582 struct vlist_tree *assignments = prefix_new->assignments;
583 vlist_for_each_element(assignments, assignment, node)
584 assignments->update(assignments,
585 &assignment->node, &assignment->node);
586 } else if (node_new) {
587 prefix_new->avail = 1ULL << (64 - prefix_new->length);
588 prefix_new->assignments = calloc(1, sizeof(*prefix_new->assignments));
589 vlist_init(prefix_new->assignments, prefix_assignment_cmp,
590 interface_update_prefix_assignments);
592 // Create initial assignments for interfaces
593 struct interface *iface;
594 vlist_for_each_element(&interfaces, iface, node)
595 interface_ip_set_prefix_assignment(prefix_new, iface,
596 iface->proto_ip.assignment_length);
598 list_add(&prefix_new->head, &prefixes);
600 // Set null-route to avoid routing loops
601 system_add_route(NULL, &route);
606 system_del_route(NULL, &route);
608 list_del(&prefix_old->head);
610 if (prefix_old->assignments) {
611 vlist_flush_all(prefix_old->assignments);
612 free(prefix_old->assignments);
619 interface_ip_add_device_prefix(struct interface *iface, struct in6_addr *addr,
620 uint8_t length, time_t valid_until, time_t preferred_until)
622 struct device_prefix *prefix = calloc(1, sizeof(*prefix));
623 prefix->length = length;
624 prefix->addr = *addr;
625 prefix->preferred_until = preferred_until;
626 prefix->valid_until = valid_until;
627 prefix->iface = iface;
630 vlist_add(&iface->proto_ip.prefix, &prefix->node, &prefix->addr);
632 interface_update_prefix(NULL, &prefix->node, NULL);
636 interface_ip_set_ula_prefix(const char *prefix)
638 char buf[INET6_ADDRSTRLEN + 4] = {0}, *saveptr;
639 strncpy(buf, prefix, sizeof(buf) - 1);
640 char *prefixaddr = strtok_r(buf, "/", &saveptr);
642 struct in6_addr addr;
643 if (!prefixaddr || inet_pton(AF_INET6, prefixaddr, &addr) < 1)
647 char *prefixlen = strtok_r(NULL, ",", &saveptr);
648 if (!prefixlen || (length = atoi(prefixlen)) < 1 || length > 64)
651 if (ula_prefix && (!IN6_ARE_ADDR_EQUAL(&addr, &ula_prefix->addr) ||
652 ula_prefix->length != length)) {
653 interface_update_prefix(NULL, NULL, &ula_prefix->node);
657 interface_ip_add_device_prefix(NULL, &addr, length, 0, 0);
661 interface_add_dns_server(struct interface_ip_settings *ip, const char *str)
663 struct dns_server *s;
665 s = calloc(1, sizeof(*s));
670 if (inet_pton(s->af, str, &s->addr.in))
674 if (inet_pton(s->af, str, &s->addr.in))
681 D(INTERFACE, "Add IPv%c DNS server: %s\n",
682 s->af == AF_INET6 ? '6' : '4', str);
683 vlist_simple_add(&ip->dns_servers, &s->node);
687 interface_add_dns_server_list(struct interface_ip_settings *ip, struct blob_attr *list)
689 struct blob_attr *cur;
692 blobmsg_for_each_attr(cur, list, rem) {
693 if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING)
696 if (!blobmsg_check_attr(cur, NULL))
699 interface_add_dns_server(ip, blobmsg_data(cur));
704 interface_add_dns_search_domain(struct interface_ip_settings *ip, const char *str)
706 struct dns_search_domain *s;
707 int len = strlen(str);
709 s = calloc(1, sizeof(*s) + len + 1);
713 D(INTERFACE, "Add DNS search domain: %s\n", str);
714 memcpy(s->name, str, len);
715 vlist_simple_add(&ip->dns_search, &s->node);
719 interface_add_dns_search_list(struct interface_ip_settings *ip, struct blob_attr *list)
721 struct blob_attr *cur;
724 blobmsg_for_each_attr(cur, list, rem) {
725 if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING)
728 if (!blobmsg_check_attr(cur, NULL))
731 interface_add_dns_search_domain(ip, blobmsg_data(cur));
736 write_resolv_conf_entries(FILE *f, struct interface_ip_settings *ip)
738 struct dns_server *s;
739 struct dns_search_domain *d;
743 vlist_simple_for_each_element(&ip->dns_servers, s, node) {
744 str = inet_ntop(s->af, &s->addr, buf, sizeof(buf));
748 fprintf(f, "nameserver %s\n", str);
751 vlist_simple_for_each_element(&ip->dns_search, d, node) {
752 fprintf(f, "search %s\n", d->name);
757 interface_write_resolv_conf(void)
759 struct interface *iface;
760 char *path = alloca(strlen(resolv_conf) + 5);
763 sprintf(path, "%s.tmp", resolv_conf);
765 f = fopen(path, "w");
767 D(INTERFACE, "Failed to open %s for writing\n", path);
771 vlist_for_each_element(&interfaces, iface, node) {
772 if (iface->state != IFS_UP)
775 if (vlist_simple_empty(&iface->proto_ip.dns_search) &&
776 vlist_simple_empty(&iface->proto_ip.dns_servers) &&
777 vlist_simple_empty(&iface->config_ip.dns_search) &&
778 vlist_simple_empty(&iface->config_ip.dns_servers))
781 fprintf(f, "# Interface %s\n", iface->name);
782 write_resolv_conf_entries(f, &iface->config_ip);
783 if (!iface->proto_ip.no_dns)
784 write_resolv_conf_entries(f, &iface->proto_ip);
787 if (rename(path, resolv_conf) < 0) {
788 D(INTERFACE, "Failed to replace %s\n", resolv_conf);
793 void interface_ip_set_enabled(struct interface_ip_settings *ip, bool enabled)
795 struct device_addr *addr;
796 struct device_route *route;
799 ip->enabled = enabled;
800 dev = ip->iface->l3_dev.dev;
804 vlist_for_each_element(&ip->addr, addr, node) {
805 if (addr->enabled == enabled)
809 system_add_address(dev, addr);
811 system_del_address(dev, addr);
812 addr->enabled = enabled;
815 vlist_for_each_element(&ip->route, route, node) {
816 bool _enabled = enabled;
818 if (!enable_route(ip, route))
821 if (route->enabled == _enabled)
825 if (!(route->flags & DEVROUTE_METRIC))
826 route->metric = ip->iface->metric;
828 system_add_route(dev, route);
830 system_del_route(dev, route);
831 route->enabled = _enabled;
836 interface_ip_update_start(struct interface_ip_settings *ip)
838 if (ip != &ip->iface->config_ip) {
839 vlist_simple_update(&ip->dns_servers);
840 vlist_simple_update(&ip->dns_search);
842 vlist_update(&ip->route);
843 vlist_update(&ip->addr);
844 vlist_update(&ip->prefix);
848 interface_ip_update_complete(struct interface_ip_settings *ip)
850 vlist_simple_flush(&ip->dns_servers);
851 vlist_simple_flush(&ip->dns_search);
852 vlist_flush(&ip->route);
853 vlist_flush(&ip->addr);
854 vlist_flush(&ip->prefix);
855 interface_write_resolv_conf();
859 interface_ip_flush(struct interface_ip_settings *ip)
861 if (ip == &ip->iface->proto_ip)
862 vlist_flush_all(&ip->iface->host_routes);
863 vlist_simple_flush_all(&ip->dns_servers);
864 vlist_simple_flush_all(&ip->dns_search);
865 vlist_flush_all(&ip->route);
866 vlist_flush_all(&ip->addr);
867 vlist_flush_all(&ip->prefix);
871 __interface_ip_init(struct interface_ip_settings *ip, struct interface *iface)
875 vlist_simple_init(&ip->dns_search, struct dns_search_domain, node);
876 vlist_simple_init(&ip->dns_servers, struct dns_server, node);
877 vlist_init(&ip->route, route_cmp, interface_update_proto_route);
878 vlist_init(&ip->addr, addr_cmp, interface_update_proto_addr);
879 vlist_init(&ip->prefix, prefix_cmp, interface_update_prefix);
883 interface_ip_init(struct interface *iface)
885 __interface_ip_init(&iface->proto_ip, iface);
886 __interface_ip_init(&iface->config_ip, iface);
887 vlist_init(&iface->host_routes, route_cmp, interface_update_host_route);