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"
41 static const struct blobmsg_policy route_attr[__ROUTE_MAX] = {
42 [ROUTE_INTERFACE] = { .name = "interface", .type = BLOBMSG_TYPE_STRING },
43 [ROUTE_TARGET] = { .name = "target", .type = BLOBMSG_TYPE_STRING },
44 [ROUTE_MASK] = { .name = "netmask", .type = BLOBMSG_TYPE_STRING },
45 [ROUTE_GATEWAY] = { .name = "gateway", .type = BLOBMSG_TYPE_STRING },
46 [ROUTE_METRIC] = { .name = "metric", .type = BLOBMSG_TYPE_INT32 },
47 [ROUTE_MTU] = { .name = "mtu", .type = BLOBMSG_TYPE_INT32 },
48 [ROUTE_TABLE] = { .name = "table", .type = BLOBMSG_TYPE_STRING },
49 [ROUTE_VALID] = { .name = "valid", .type = BLOBMSG_TYPE_INT32 },
52 const struct config_param_list route_attr_list = {
53 .n_params = __ROUTE_MAX,
58 struct list_head prefixes = LIST_HEAD_INIT(prefixes);
59 static struct device_prefix *ula_prefix = NULL;
60 static struct uloop_timeout valid_until_timeout;
64 clear_if_addr(union if_addr *a, int mask)
66 int m_bytes = (mask + 7) / 8;
67 uint8_t m_clear = (1 << (m_bytes * 8 - mask)) - 1;
68 uint8_t *p = (uint8_t *) a;
70 if (m_bytes < sizeof(a))
71 memset(p + m_bytes, 0, sizeof(a) - m_bytes);
73 p[m_bytes - 1] &= ~m_clear;
77 match_if_addr(union if_addr *a1, union if_addr *a2, int mask)
79 union if_addr *p1, *p2;
81 p1 = alloca(sizeof(*a1));
82 p2 = alloca(sizeof(*a2));
84 memcpy(p1, a1, sizeof(*a1));
85 clear_if_addr(p1, mask);
86 memcpy(p2, a2, sizeof(*a2));
87 clear_if_addr(p2, mask);
89 return !memcmp(p1, p2, sizeof(*p1));
93 __find_ip_addr_target(struct interface_ip_settings *ip, union if_addr *a, bool v6)
95 struct device_addr *addr;
97 vlist_for_each_element(&ip->addr, addr, node) {
101 if (v6 != ((addr->flags & DEVADDR_FAMILY) == DEVADDR_INET6))
104 if (!match_if_addr(&addr->addr, a, addr->mask))
114 __find_ip_route_target(struct interface_ip_settings *ip, union if_addr *a,
115 bool v6, struct device_route **res)
117 struct device_route *route;
119 vlist_for_each_element(&ip->route, route, node) {
123 if (v6 != ((route->flags & DEVADDR_FAMILY) == DEVADDR_INET6))
126 if (!match_if_addr(&route->addr, a, route->mask))
129 if (route->flags & DEVROUTE_TABLE)
132 if (!*res || route->mask < (*res)->mask)
138 interface_ip_find_addr_target(struct interface *iface, union if_addr *a, bool v6)
140 return __find_ip_addr_target(&iface->proto_ip, a, v6) ||
141 __find_ip_addr_target(&iface->config_ip, a, v6);
145 interface_ip_find_route_target(struct interface *iface, union if_addr *a,
146 bool v6, struct device_route **route)
148 __find_ip_route_target(&iface->proto_ip, a, v6, route);
149 __find_ip_route_target(&iface->config_ip, a, v6, route);
153 interface_ip_add_target_route(union if_addr *addr, bool v6)
155 struct interface *iface;
156 struct device_route *route, *r_next = NULL;
157 bool defaultroute_target = false;
158 int addrsize = v6 ? sizeof(addr->in6) : sizeof(addr->in);
160 route = calloc(1, sizeof(*route));
164 route->flags = v6 ? DEVADDR_INET6 : DEVADDR_INET4;
165 route->mask = v6 ? 128 : 32;
166 if (memcmp(&route->addr, addr, addrsize) == 0)
167 defaultroute_target = true;
169 memcpy(&route->addr, addr, addrsize);
171 vlist_for_each_element(&interfaces, iface, node) {
172 /* look for locally addressable target first */
173 if (interface_ip_find_addr_target(iface, addr, v6))
176 /* do not stop at the first route, let the lookup compare
177 * masks to find the best match */
178 interface_ip_find_route_target(iface, addr, v6, &r_next);
186 iface = r_next->iface;
187 memcpy(&route->nexthop, &r_next->nexthop, sizeof(route->nexthop));
188 route->mtu = r_next->mtu;
189 route->metric = r_next->metric;
192 route->iface = iface;
193 if (defaultroute_target)
196 vlist_add(&iface->host_routes, &route->node, &route->flags);
201 interface_ip_add_route(struct interface *iface, struct blob_attr *attr, bool v6)
203 struct interface_ip_settings *ip;
204 struct blob_attr *tb[__ROUTE_MAX], *cur;
205 struct device_route *route;
206 int af = v6 ? AF_INET6 : AF_INET;
208 blobmsg_parse(route_attr, __ROUTE_MAX, tb, blobmsg_data(attr), blobmsg_data_len(attr));
211 if ((cur = tb[ROUTE_INTERFACE]) == NULL)
214 iface = vlist_find(&interfaces, blobmsg_data(cur), iface, node);
218 ip = &iface->config_ip;
220 ip = &iface->proto_ip;
223 route = calloc(1, sizeof(*route));
227 route->flags = v6 ? DEVADDR_INET6 : DEVADDR_INET4;
228 route->mask = v6 ? 128 : 32;
229 if ((cur = tb[ROUTE_MASK]) != NULL) {
230 route->mask = parse_netmask_string(blobmsg_data(cur), v6);
231 if (route->mask > (v6 ? 128 : 32))
235 if ((cur = tb[ROUTE_TARGET]) != NULL) {
236 if (!parse_ip_and_netmask(af, blobmsg_data(cur), &route->addr, &route->mask)) {
237 DPRINTF("Failed to parse route target: %s\n", (char *) blobmsg_data(cur));
242 if ((cur = tb[ROUTE_GATEWAY]) != NULL) {
243 if (!inet_pton(af, blobmsg_data(cur), &route->nexthop)) {
244 DPRINTF("Failed to parse route gateway: %s\n", (char *) blobmsg_data(cur));
249 if ((cur = tb[ROUTE_METRIC]) != NULL) {
250 route->metric = blobmsg_get_u32(cur);
251 route->flags |= DEVROUTE_METRIC;
254 if ((cur = tb[ROUTE_MTU]) != NULL) {
255 route->mtu = blobmsg_get_u32(cur);
256 route->flags |= DEVROUTE_MTU;
259 if ((cur = tb[ROUTE_TABLE]) != NULL) {
260 if (!system_resolve_rt_table(blobmsg_data(cur), &route->table)) {
261 DPRINTF("Failed to resolve routing table: %s\n", (char *) blobmsg_data(cur));
266 route->flags |= DEVROUTE_TABLE;
269 if ((cur = tb[ROUTE_VALID]) != NULL)
270 route->valid_until = system_get_rtime() + blobmsg_get_u32(cur);
272 vlist_add(&ip->route, &route->node, &route->flags);
280 addr_cmp(const void *k1, const void *k2, void *ptr)
282 return memcmp(k1, k2, sizeof(struct device_addr) -
283 offsetof(struct device_addr, flags));
287 route_cmp(const void *k1, const void *k2, void *ptr)
289 return memcmp(k1, k2, sizeof(struct device_route) -
290 offsetof(struct device_route, flags));
294 prefix_cmp(const void *k1, const void *k2, void *ptr)
296 return memcmp(k1, k2, sizeof(struct device_prefix) -
297 offsetof(struct device_prefix, addr));
301 interface_handle_subnet_route(struct interface *iface, struct device_addr *addr, bool add)
303 struct device *dev = iface->l3_dev.dev;
304 struct device_route route;
306 memset(&route, 0, sizeof(route));
308 route.flags = addr->flags;
309 route.mask = addr->mask;
310 memcpy(&route.addr, &addr->addr, sizeof(route.addr));
311 clear_if_addr(&route.addr, route.mask);
314 route.flags |= DEVADDR_KERNEL;
315 system_del_route(dev, &route);
317 if (!(addr->flags & DEVADDR_OFFLINK)) {
318 route.flags &= ~DEVADDR_KERNEL;
319 route.metric = iface->metric;
320 system_add_route(dev, &route);
323 if (!(addr->flags & DEVADDR_OFFLINK))
324 system_del_route(dev, &route);
329 interface_update_proto_addr(struct vlist_tree *tree,
330 struct vlist_node *node_new,
331 struct vlist_node *node_old)
333 struct interface_ip_settings *ip;
334 struct interface *iface;
336 struct device_addr *a_new = NULL, *a_old = NULL;
339 ip = container_of(tree, struct interface_ip_settings, addr);
341 dev = iface->l3_dev.dev;
344 a_new = container_of(node_new, struct device_addr, node);
346 if ((a_new->flags & DEVADDR_FAMILY) == DEVADDR_INET4 &&
350 uint32_t *a = (uint32_t *) &a_new->addr;
352 mask >>= a_new->mask;
353 a_new->broadcast = *a | htonl(mask);
358 a_old = container_of(node_old, struct device_addr, node);
360 if (a_new && a_old) {
363 if (a_old->flags != a_new->flags ||
364 a_old->valid_until != a_new->valid_until ||
365 a_old->preferred_until != a_new->preferred_until)
368 if ((a_new->flags & DEVADDR_FAMILY) == DEVADDR_INET4 &&
369 a_new->broadcast != a_old->broadcast)
374 if (!(a_old->flags & DEVADDR_EXTERNAL) && a_old->enabled && !keep) {
375 interface_handle_subnet_route(iface, a_old, false);
376 system_del_address(dev, a_old);
382 a_new->enabled = true;
383 if (!(a_new->flags & DEVADDR_EXTERNAL) && !keep) {
384 system_add_address(dev, a_new);
385 if ((a_new->flags & DEVADDR_OFFLINK) || iface->metric)
386 interface_handle_subnet_route(iface, a_new, true);
392 enable_route(struct interface_ip_settings *ip, struct device_route *route)
394 if (ip->no_defaultroute && !route->mask)
401 interface_update_proto_route(struct vlist_tree *tree,
402 struct vlist_node *node_new,
403 struct vlist_node *node_old)
405 struct interface_ip_settings *ip;
406 struct interface *iface;
408 struct device_route *route_old, *route_new;
411 ip = container_of(tree, struct interface_ip_settings, route);
413 dev = iface->l3_dev.dev;
415 route_old = container_of(node_old, struct device_route, node);
416 route_new = container_of(node_new, struct device_route, node);
418 if (node_old && node_new)
419 keep = !memcmp(&route_old->nexthop, &route_new->nexthop, sizeof(route_old->nexthop));
422 if (!(route_old->flags & DEVADDR_EXTERNAL) && route_old->enabled && !keep)
423 system_del_route(dev, route_old);
428 bool _enabled = enable_route(ip, route_new);
430 if (!(route_new->flags & DEVROUTE_METRIC))
431 route_new->metric = iface->metric;
433 if (!(route_new->flags & DEVADDR_EXTERNAL) && !keep && _enabled)
434 system_add_route(dev, route_new);
436 route_new->iface = iface;
437 route_new->enabled = _enabled;
442 interface_update_host_route(struct vlist_tree *tree,
443 struct vlist_node *node_new,
444 struct vlist_node *node_old)
446 struct interface *iface;
448 struct device_route *route_old, *route_new;
450 iface = container_of(tree, struct interface, host_routes);
451 dev = iface->l3_dev.dev;
453 route_old = container_of(node_old, struct device_route, node);
454 route_new = container_of(node_new, struct device_route, node);
457 system_del_route(dev, route_old);
462 system_add_route(dev, route_new);
467 interface_set_prefix_address(struct device_prefix_assignment *assignment,
468 const struct device_prefix *prefix, struct interface *iface, bool add)
470 const struct interface *uplink = prefix->iface;
471 if (!iface->l3_dev.dev)
474 struct device *l3_downlink = iface->l3_dev.dev;
476 struct device_addr addr;
477 memset(&addr, 0, sizeof(addr));
478 addr.addr.in6 = prefix->addr;
479 addr.addr.in6.s6_addr32[1] |= htonl(assignment->assigned);
480 addr.addr.in6.s6_addr[15] += 1;
481 addr.mask = assignment->length;
482 addr.flags = DEVADDR_INET6;
483 addr.preferred_until = prefix->preferred_until;
484 addr.valid_until = prefix->valid_until;
486 if (!add && assignment->enabled) {
487 time_t now = system_get_rtime();
488 addr.preferred_until = now;
489 if (!addr.valid_until || addr.valid_until - now > 7200)
490 addr.valid_until = now + 7200;
491 system_add_address(l3_downlink, &addr);
492 assignment->enabled = false;
493 } else if (add && (iface->state == IFS_UP || iface->state == IFS_SETUP)) {
494 system_add_address(l3_downlink, &addr);
495 if (uplink && uplink->l3_dev.dev) {
496 int mtu = system_update_ipv6_mtu(
497 uplink->l3_dev.dev, 0);
499 system_update_ipv6_mtu(l3_downlink, mtu);
501 assignment->enabled = true;
505 static bool interface_prefix_assign(struct list_head *list,
506 struct device_prefix_assignment *assign)
508 int32_t current = 0, asize = (1 << (64 - assign->length)) - 1;
509 struct device_prefix_assignment *c;
510 list_for_each_entry(c, list, head) {
511 if (assign->assigned != -1) {
512 if (assign->assigned > current && assign->assigned + asize < c->assigned) {
513 list_add_tail(&assign->head, &c->head);
516 } else if (assign->assigned == -1) {
517 current = (current + asize) & (~asize);
518 if (current + asize < c->assigned) {
519 assign->assigned = current;
520 list_add_tail(&assign->head, &c->head);
524 current = (c->assigned + (1 << (64 - c->length)));
529 static void interface_update_prefix_assignments(struct device_prefix *prefix, bool setup)
531 struct device_prefix_assignment *c;
532 struct interface *iface;
534 // Delete all assignments
535 while (!list_empty(&prefix->assignments)) {
536 c = list_first_entry(&prefix->assignments,
537 struct device_prefix_assignment, head);
538 if ((iface = vlist_find(&interfaces, c->name, iface, node)))
539 interface_set_prefix_address(c, prefix, iface, false);
547 // End-of-assignment sentinel
548 c = malloc(sizeof(*c) + 1);
549 c->assigned = 1 << (64 - prefix->length);
552 list_add(&c->head, &prefix->assignments);
555 if (prefix->excl_length > 0) {
556 const char name[] = "!excluded";
557 c = malloc(sizeof(*c) + sizeof(name));
558 c->assigned = ntohl(prefix->excl_addr.s6_addr32[1]) &
559 ((1 << (64 - prefix->length)) - 1);
560 c->length = prefix->excl_length;
561 memcpy(c->name, name, sizeof(name));
562 list_add(&c->head, &prefix->assignments);
565 struct list_head assign_later = LIST_HEAD_INIT(assign_later);
566 vlist_for_each_element(&interfaces, iface, node) {
567 if (iface->config_ip.assignment_length < 48 ||
568 iface->config_ip.assignment_length > 64)
571 size_t namelen = strlen(iface->name) + 1;
572 c = malloc(sizeof(*c) + namelen);
573 c->length = iface->config_ip.assignment_length;
574 c->assigned = iface->config_ip.assignment_hint;
576 memcpy(c->name, iface->name, namelen);
578 // First process all custom assignments, put all others in later-list
579 if (c->assigned == -1 || !interface_prefix_assign(&prefix->assignments, c)) {
580 if (c->assigned != -1) {
582 netifd_log_message(L_WARNING, "Failed to assign requested subprefix "
583 "of size %hhu for %s, trying other\n", c->length, c->name);
585 list_add_tail(&c->head, &assign_later);
589 // Then try to assign all other + failed custom assignments
590 while (!list_empty(&assign_later)) {
591 c = list_first_entry(&assign_later, struct device_prefix_assignment, head);
593 if (!interface_prefix_assign(&prefix->assignments, c)) {
594 netifd_log_message(L_WARNING, "Failed to assign subprefix "
595 "of size %hhu for %s\n", c->length, c->name);
600 list_for_each_entry(c, &prefix->assignments, head)
601 if ((iface = vlist_find(&interfaces, c->name, iface, node)))
602 interface_set_prefix_address(c, prefix, iface, true);
606 void interface_refresh_assignments(bool hint)
608 static bool refresh = false;
609 if (!hint && refresh) {
610 struct device_prefix *p;
611 list_for_each_entry(p, &prefixes, head)
612 interface_update_prefix_assignments(p, true);
619 interface_update_prefix(struct vlist_tree *tree,
620 struct vlist_node *node_new,
621 struct vlist_node *node_old)
623 struct device_prefix *prefix_old, *prefix_new;
624 prefix_old = container_of(node_old, struct device_prefix, node);
625 prefix_new = container_of(node_new, struct device_prefix, node);
627 struct device_route route;
628 memset(&route, 0, sizeof(route));
629 route.flags = DEVADDR_INET6;
630 route.metric = INT32_MAX;
631 route.mask = (node_new) ? prefix_new->length : prefix_old->length;
632 route.addr.in6 = (node_new) ? prefix_new->addr : prefix_old->addr;
634 struct device_prefix_assignment *c;
635 struct interface *iface;
637 if (node_old && node_new) {
638 // Move assignments and refresh addresses to update valid times
639 list_splice_init(&prefix_old->assignments, &prefix_new->assignments);
641 list_for_each_entry(c, &prefix_new->assignments, head)
642 if ((iface = vlist_find(&interfaces, c->name, iface, node)))
643 interface_set_prefix_address(c, prefix_new, iface, true);
644 } else if (node_new) {
645 // Set null-route to avoid routing loops
646 system_add_route(NULL, &route);
648 INIT_LIST_HEAD(&prefix_new->assignments);
649 interface_update_prefix_assignments(prefix_new, true);
650 } else if (node_old) {
651 interface_update_prefix_assignments(prefix_old, false);
654 system_del_route(NULL, &route);
658 list_del(&prefix_old->head);
663 list_add(&prefix_new->head, &prefixes);
667 struct device_prefix*
668 interface_ip_add_device_prefix(struct interface *iface, struct in6_addr *addr,
669 uint8_t length, time_t valid_until, time_t preferred_until,
670 struct in6_addr *excl_addr, uint8_t excl_length)
672 struct device_prefix *prefix = calloc(1, sizeof(*prefix));
673 prefix->length = length;
674 prefix->addr = *addr;
675 prefix->preferred_until = preferred_until;
676 prefix->valid_until = valid_until;
677 prefix->iface = iface;
680 prefix->excl_addr = *excl_addr;
681 prefix->excl_length = excl_length;
685 vlist_add(&iface->proto_ip.prefix, &prefix->node, &prefix->addr);
687 interface_update_prefix(NULL, &prefix->node, NULL);
693 interface_ip_set_ula_prefix(const char *prefix)
695 char buf[INET6_ADDRSTRLEN + 4] = {0}, *saveptr;
697 strncpy(buf, prefix, sizeof(buf) - 1);
698 char *prefixaddr = strtok_r(buf, "/", &saveptr);
700 struct in6_addr addr;
701 if (!prefixaddr || inet_pton(AF_INET6, prefixaddr, &addr) < 1) {
703 interface_update_prefix(NULL, NULL, &ula_prefix->node);
710 char *prefixlen = strtok_r(NULL, ",", &saveptr);
711 if (!prefixlen || (length = atoi(prefixlen)) < 1 || length > 64)
714 if (!ula_prefix || !IN6_ARE_ADDR_EQUAL(&addr, &ula_prefix->addr) ||
715 ula_prefix->length != length) {
717 interface_update_prefix(NULL, NULL, &ula_prefix->node);
719 ula_prefix = interface_ip_add_device_prefix(NULL, &addr, length,
725 interface_add_dns_server(struct interface_ip_settings *ip, const char *str)
727 struct dns_server *s;
729 s = calloc(1, sizeof(*s));
734 if (inet_pton(s->af, str, &s->addr.in))
738 if (inet_pton(s->af, str, &s->addr.in))
745 D(INTERFACE, "Add IPv%c DNS server: %s\n",
746 s->af == AF_INET6 ? '6' : '4', str);
747 vlist_simple_add(&ip->dns_servers, &s->node);
751 interface_add_dns_server_list(struct interface_ip_settings *ip, struct blob_attr *list)
753 struct blob_attr *cur;
756 blobmsg_for_each_attr(cur, list, rem) {
757 if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING)
760 if (!blobmsg_check_attr(cur, NULL))
763 interface_add_dns_server(ip, blobmsg_data(cur));
768 interface_add_dns_search_domain(struct interface_ip_settings *ip, const char *str)
770 struct dns_search_domain *s;
771 int len = strlen(str);
773 s = calloc(1, sizeof(*s) + len + 1);
777 D(INTERFACE, "Add DNS search domain: %s\n", str);
778 memcpy(s->name, str, len);
779 vlist_simple_add(&ip->dns_search, &s->node);
783 interface_add_dns_search_list(struct interface_ip_settings *ip, struct blob_attr *list)
785 struct blob_attr *cur;
788 blobmsg_for_each_attr(cur, list, rem) {
789 if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING)
792 if (!blobmsg_check_attr(cur, NULL))
795 interface_add_dns_search_domain(ip, blobmsg_data(cur));
800 write_resolv_conf_entries(FILE *f, struct interface_ip_settings *ip)
802 struct dns_server *s;
803 struct dns_search_domain *d;
805 char buf[INET6_ADDRSTRLEN];
807 vlist_simple_for_each_element(&ip->dns_servers, s, node) {
808 str = inet_ntop(s->af, &s->addr, buf, sizeof(buf));
812 fprintf(f, "nameserver %s\n", str);
815 vlist_simple_for_each_element(&ip->dns_search, d, node) {
816 fprintf(f, "search %s\n", d->name);
821 interface_write_resolv_conf(void)
823 struct interface *iface;
824 char *path = alloca(strlen(resolv_conf) + 5);
826 uint32_t crcold, crcnew;
828 sprintf(path, "%s.tmp", resolv_conf);
830 f = fopen(path, "w+");
832 D(INTERFACE, "Failed to open %s for writing\n", path);
836 vlist_for_each_element(&interfaces, iface, node) {
837 if (iface->state != IFS_UP)
840 if (vlist_simple_empty(&iface->proto_ip.dns_search) &&
841 vlist_simple_empty(&iface->proto_ip.dns_servers) &&
842 vlist_simple_empty(&iface->config_ip.dns_search) &&
843 vlist_simple_empty(&iface->config_ip.dns_servers))
846 fprintf(f, "# Interface %s\n", iface->name);
847 write_resolv_conf_entries(f, &iface->config_ip);
848 if (!iface->proto_ip.no_dns)
849 write_resolv_conf_entries(f, &iface->proto_ip);
853 crcnew = crc32_file(f);
857 f = fopen(resolv_conf, "r");
859 crcold = crc32_file(f);
863 if (crcold == crcnew) {
865 } else if (rename(path, resolv_conf) < 0) {
866 D(INTERFACE, "Failed to replace %s\n", resolv_conf);
871 void interface_ip_set_enabled(struct interface_ip_settings *ip, bool enabled)
873 struct device_addr *addr;
874 struct device_route *route;
877 ip->enabled = enabled;
878 dev = ip->iface->l3_dev.dev;
882 vlist_for_each_element(&ip->addr, addr, node) {
883 if (addr->enabled == enabled)
887 system_add_address(dev, addr);
889 system_del_address(dev, addr);
890 addr->enabled = enabled;
893 vlist_for_each_element(&ip->route, route, node) {
894 bool _enabled = enabled;
896 if (!enable_route(ip, route))
899 if (route->enabled == _enabled)
903 if (!(route->flags & DEVROUTE_METRIC))
904 route->metric = ip->iface->metric;
906 system_add_route(dev, route);
908 system_del_route(dev, route);
909 route->enabled = _enabled;
912 struct device_prefix *c;
913 struct device_prefix_assignment *a;
914 list_for_each_entry(c, &prefixes, head)
915 list_for_each_entry(a, &c->assignments, head)
916 if (!strcmp(a->name, ip->iface->name))
917 interface_set_prefix_address(a, c, ip->iface, enabled);
921 interface_ip_update_start(struct interface_ip_settings *ip)
923 if (ip != &ip->iface->config_ip) {
924 vlist_simple_update(&ip->dns_servers);
925 vlist_simple_update(&ip->dns_search);
927 vlist_update(&ip->route);
928 vlist_update(&ip->addr);
929 vlist_update(&ip->prefix);
933 interface_ip_update_complete(struct interface_ip_settings *ip)
935 vlist_simple_flush(&ip->dns_servers);
936 vlist_simple_flush(&ip->dns_search);
937 vlist_flush(&ip->route);
938 vlist_flush(&ip->addr);
939 vlist_flush(&ip->prefix);
940 interface_write_resolv_conf();
944 interface_ip_flush(struct interface_ip_settings *ip)
946 if (ip == &ip->iface->proto_ip)
947 vlist_flush_all(&ip->iface->host_routes);
948 vlist_simple_flush_all(&ip->dns_servers);
949 vlist_simple_flush_all(&ip->dns_search);
950 vlist_flush_all(&ip->route);
951 vlist_flush_all(&ip->addr);
952 vlist_flush_all(&ip->prefix);
956 __interface_ip_init(struct interface_ip_settings *ip, struct interface *iface)
960 vlist_simple_init(&ip->dns_search, struct dns_search_domain, node);
961 vlist_simple_init(&ip->dns_servers, struct dns_server, node);
962 vlist_init(&ip->route, route_cmp, interface_update_proto_route);
963 vlist_init(&ip->addr, addr_cmp, interface_update_proto_addr);
964 vlist_init(&ip->prefix, prefix_cmp, interface_update_prefix);
968 interface_ip_init(struct interface *iface)
970 __interface_ip_init(&iface->proto_ip, iface);
971 __interface_ip_init(&iface->config_ip, iface);
972 vlist_init(&iface->host_routes, route_cmp, interface_update_host_route);
977 interface_ip_valid_until_handler(struct uloop_timeout *t)
979 time_t now = system_get_rtime();
980 struct interface *iface;
981 vlist_for_each_element(&interfaces, iface, node) {
982 if (iface->state != IFS_UP)
985 struct device_addr *addr, *addrp;
986 struct device_route *route, *routep;
987 struct device_prefix *pref, *prefp;
989 vlist_for_each_element_safe(&iface->proto_ip.addr, addr, node, addrp)
990 if (addr->valid_until && addr->valid_until < now)
991 vlist_delete(&iface->proto_ip.addr, &addr->node);
993 vlist_for_each_element_safe(&iface->proto_ip.route, route, node, routep)
994 if (route->valid_until && route->valid_until < now)
995 vlist_delete(&iface->proto_ip.route, &route->node);
997 vlist_for_each_element_safe(&iface->proto_ip.prefix, pref, node, prefp)
998 if (pref->valid_until && pref->valid_until < now)
999 vlist_delete(&iface->proto_ip.prefix, &pref->node);
1003 uloop_timeout_set(t, 1000);
1007 interface_ip_init_worker(void)
1009 valid_until_timeout.cb = interface_ip_valid_until_handler;
1010 uloop_timeout_set(&valid_until_timeout, 1000);