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"
40 static const struct blobmsg_policy route_attr[__ROUTE_MAX] = {
41 [ROUTE_INTERFACE] = { .name = "interface", .type = BLOBMSG_TYPE_STRING },
42 [ROUTE_TARGET] = { .name = "target", .type = BLOBMSG_TYPE_STRING },
43 [ROUTE_MASK] = { .name = "netmask", .type = BLOBMSG_TYPE_STRING },
44 [ROUTE_GATEWAY] = { .name = "gateway", .type = BLOBMSG_TYPE_STRING },
45 [ROUTE_METRIC] = { .name = "metric", .type = BLOBMSG_TYPE_INT32 },
46 [ROUTE_MTU] = { .name = "mtu", .type = BLOBMSG_TYPE_INT32 },
47 [ROUTE_VALID] = { .name = "valid", .type = BLOBMSG_TYPE_INT32 },
50 const struct config_param_list route_attr_list = {
51 .n_params = __ROUTE_MAX,
56 struct list_head prefixes = LIST_HEAD_INIT(prefixes);
57 static struct device_prefix *ula_prefix = NULL;
58 static struct uloop_timeout valid_until_timeout;
62 clear_if_addr(union if_addr *a, int mask)
64 int m_bytes = (mask + 7) / 8;
65 uint8_t m_clear = (1 << (m_bytes * 8 - mask)) - 1;
66 uint8_t *p = (uint8_t *) a;
68 if (m_bytes < sizeof(a))
69 memset(p + m_bytes, 0, sizeof(a) - m_bytes);
71 p[m_bytes - 1] &= ~m_clear;
75 match_if_addr(union if_addr *a1, union if_addr *a2, int mask)
77 union if_addr *p1, *p2;
79 p1 = alloca(sizeof(*a1));
80 p2 = alloca(sizeof(*a2));
82 memcpy(p1, a1, sizeof(*a1));
83 clear_if_addr(p1, mask);
84 memcpy(p2, a2, sizeof(*a2));
85 clear_if_addr(p2, mask);
87 return !memcmp(p1, p2, sizeof(*p1));
91 __find_ip_addr_target(struct interface_ip_settings *ip, union if_addr *a, bool v6)
93 struct device_addr *addr;
95 vlist_for_each_element(&ip->addr, addr, node) {
99 if (v6 != ((addr->flags & DEVADDR_FAMILY) == DEVADDR_INET6))
102 if (!match_if_addr(&addr->addr, a, addr->mask))
112 __find_ip_route_target(struct interface_ip_settings *ip, union if_addr *a,
113 bool v6, struct device_route **res)
115 struct device_route *route;
117 vlist_for_each_element(&ip->route, route, node) {
121 if (v6 != ((route->flags & DEVADDR_FAMILY) == DEVADDR_INET6))
124 if (!match_if_addr(&route->addr, a, route->mask))
127 if (!*res || route->mask < (*res)->mask)
133 interface_ip_find_addr_target(struct interface *iface, union if_addr *a, bool v6)
135 return __find_ip_addr_target(&iface->proto_ip, a, v6) ||
136 __find_ip_addr_target(&iface->config_ip, a, v6);
140 interface_ip_find_route_target(struct interface *iface, union if_addr *a,
141 bool v6, struct device_route **route)
143 __find_ip_route_target(&iface->proto_ip, a, v6, route);
144 __find_ip_route_target(&iface->config_ip, a, v6, route);
148 interface_ip_add_target_route(union if_addr *addr, bool v6)
150 struct interface *iface;
151 struct device_route *route, *r_next = NULL;
152 bool defaultroute_target = false;
153 int addrsize = v6 ? sizeof(addr->in6) : sizeof(addr->in);
155 route = calloc(1, sizeof(*route));
159 route->flags = v6 ? DEVADDR_INET6 : DEVADDR_INET4;
160 route->mask = v6 ? 128 : 32;
161 if (memcmp(&route->addr, addr, addrsize) == 0)
162 defaultroute_target = true;
164 memcpy(&route->addr, addr, addrsize);
166 vlist_for_each_element(&interfaces, iface, node) {
167 /* look for locally addressable target first */
168 if (interface_ip_find_addr_target(iface, addr, v6))
171 /* do not stop at the first route, let the lookup compare
172 * masks to find the best match */
173 interface_ip_find_route_target(iface, addr, v6, &r_next);
181 iface = r_next->iface;
182 memcpy(&route->nexthop, &r_next->nexthop, sizeof(route->nexthop));
183 route->mtu = r_next->mtu;
184 route->metric = r_next->metric;
187 route->iface = iface;
188 if (defaultroute_target)
191 vlist_add(&iface->host_routes, &route->node, &route->flags);
196 interface_ip_add_route(struct interface *iface, struct blob_attr *attr, bool v6)
198 struct interface_ip_settings *ip;
199 struct blob_attr *tb[__ROUTE_MAX], *cur;
200 struct device_route *route;
201 int af = v6 ? AF_INET6 : AF_INET;
203 blobmsg_parse(route_attr, __ROUTE_MAX, tb, blobmsg_data(attr), blobmsg_data_len(attr));
206 if ((cur = tb[ROUTE_INTERFACE]) == NULL)
209 iface = vlist_find(&interfaces, blobmsg_data(cur), iface, node);
213 ip = &iface->config_ip;
215 ip = &iface->proto_ip;
218 route = calloc(1, sizeof(*route));
222 route->flags = v6 ? DEVADDR_INET6 : DEVADDR_INET4;
223 route->mask = v6 ? 128 : 32;
224 if ((cur = tb[ROUTE_MASK]) != NULL) {
225 route->mask = parse_netmask_string(blobmsg_data(cur), v6);
226 if (route->mask > (v6 ? 128 : 32))
230 if ((cur = tb[ROUTE_TARGET]) != NULL) {
231 if (!parse_ip_and_netmask(af, blobmsg_data(cur), &route->addr, &route->mask)) {
232 DPRINTF("Failed to parse route target: %s\n", (char *) blobmsg_data(cur));
237 if ((cur = tb[ROUTE_GATEWAY]) != NULL) {
238 if (!inet_pton(af, blobmsg_data(cur), &route->nexthop)) {
239 DPRINTF("Failed to parse route gateway: %s\n", (char *) blobmsg_data(cur));
244 if ((cur = tb[ROUTE_METRIC]) != NULL) {
245 route->metric = blobmsg_get_u32(cur);
246 route->flags |= DEVROUTE_METRIC;
249 if ((cur = tb[ROUTE_MTU]) != NULL) {
250 route->mtu = blobmsg_get_u32(cur);
251 route->flags |= DEVROUTE_MTU;
254 if ((cur = tb[ROUTE_VALID]) != NULL)
255 route->valid_until = system_get_rtime() + blobmsg_get_u32(cur);
257 vlist_add(&ip->route, &route->node, &route->flags);
265 addr_cmp(const void *k1, const void *k2, void *ptr)
267 return memcmp(k1, k2, sizeof(struct device_addr) -
268 offsetof(struct device_addr, flags));
272 route_cmp(const void *k1, const void *k2, void *ptr)
274 return memcmp(k1, k2, sizeof(struct device_route) -
275 offsetof(struct device_route, flags));
279 prefix_cmp(const void *k1, const void *k2, void *ptr)
281 return memcmp(k1, k2, sizeof(struct device_prefix) -
282 offsetof(struct device_prefix, addr));
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 if (!(addr->flags & DEVADDR_OFFLINK)) {
303 route.flags &= ~DEVADDR_KERNEL;
304 route.metric = iface->metric;
305 system_add_route(dev, &route);
308 if (!(addr->flags & DEVADDR_OFFLINK))
309 system_del_route(dev, &route);
314 interface_update_proto_addr(struct vlist_tree *tree,
315 struct vlist_node *node_new,
316 struct vlist_node *node_old)
318 struct interface_ip_settings *ip;
319 struct interface *iface;
321 struct device_addr *a_new = NULL, *a_old = NULL;
324 ip = container_of(tree, struct interface_ip_settings, addr);
326 dev = iface->l3_dev.dev;
329 a_new = container_of(node_new, struct device_addr, node);
331 if ((a_new->flags & DEVADDR_FAMILY) == DEVADDR_INET4 &&
335 uint32_t *a = (uint32_t *) &a_new->addr;
337 mask >>= a_new->mask;
338 a_new->broadcast = *a | htonl(mask);
343 a_old = container_of(node_old, struct device_addr, node);
345 if (a_new && a_old) {
348 if (a_old->flags != a_new->flags ||
349 a_old->valid_until != a_new->valid_until ||
350 a_old->preferred_until != a_new->preferred_until)
353 if ((a_new->flags & DEVADDR_FAMILY) == DEVADDR_INET4 &&
354 a_new->broadcast != a_old->broadcast)
359 if (!(a_old->flags & DEVADDR_EXTERNAL) && a_old->enabled && !keep) {
360 interface_handle_subnet_route(iface, a_old, false);
361 system_del_address(dev, a_old);
367 a_new->enabled = true;
368 if (!(a_new->flags & DEVADDR_EXTERNAL) && !keep) {
369 system_add_address(dev, a_new);
370 if ((a_new->flags & DEVADDR_OFFLINK) || iface->metric)
371 interface_handle_subnet_route(iface, a_new, true);
377 enable_route(struct interface_ip_settings *ip, struct device_route *route)
379 if (ip->no_defaultroute && !route->mask)
386 interface_update_proto_route(struct vlist_tree *tree,
387 struct vlist_node *node_new,
388 struct vlist_node *node_old)
390 struct interface_ip_settings *ip;
391 struct interface *iface;
393 struct device_route *route_old, *route_new;
396 ip = container_of(tree, struct interface_ip_settings, route);
398 dev = iface->l3_dev.dev;
400 route_old = container_of(node_old, struct device_route, node);
401 route_new = container_of(node_new, struct device_route, node);
403 if (node_old && node_new)
404 keep = !memcmp(&route_old->nexthop, &route_new->nexthop, sizeof(route_old->nexthop));
407 if (!(route_old->flags & DEVADDR_EXTERNAL) && route_old->enabled && !keep)
408 system_del_route(dev, route_old);
413 bool _enabled = enable_route(ip, route_new);
415 if (!(route_new->flags & DEVROUTE_METRIC))
416 route_new->metric = iface->metric;
418 if (!(route_new->flags & DEVADDR_EXTERNAL) && !keep && _enabled)
419 system_add_route(dev, route_new);
421 route_new->iface = iface;
422 route_new->enabled = _enabled;
427 interface_update_host_route(struct vlist_tree *tree,
428 struct vlist_node *node_new,
429 struct vlist_node *node_old)
431 struct interface *iface;
433 struct device_route *route_old, *route_new;
435 iface = container_of(tree, struct interface, host_routes);
436 dev = iface->l3_dev.dev;
438 route_old = container_of(node_old, struct device_route, node);
439 route_new = container_of(node_new, struct device_route, node);
442 system_del_route(dev, route_old);
447 system_add_route(dev, route_new);
452 interface_set_prefix_address(struct interface *iface, bool add,
453 struct device_prefix_assignment *assignment)
455 struct interface *uplink = assignment->prefix->iface;
456 if (!iface->l3_dev.dev)
459 struct device *l3_downlink = iface->l3_dev.dev;
461 struct device_addr addr;
462 memset(&addr, 0, sizeof(addr));
463 addr.addr.in6 = assignment->addr;
464 addr.mask = assignment->length;
465 addr.flags = DEVADDR_INET6;
466 addr.preferred_until = assignment->prefix->preferred_until;
467 addr.valid_until = assignment->prefix->valid_until;
470 if (assignment->enabled) {
471 time_t now = system_get_rtime();
472 addr.preferred_until = now;
473 if (!addr.valid_until || addr.valid_until - now > 7200)
474 addr.valid_until = now + 7200;
475 system_add_address(l3_downlink, &addr);
478 system_add_address(l3_downlink, &addr);
480 if (uplink && uplink->l3_dev.dev) {
481 int mtu = system_update_ipv6_mtu(
482 uplink->l3_dev.dev, 0);
484 system_update_ipv6_mtu(l3_downlink, mtu);
487 assignment->enabled = add;
492 interface_update_prefix_assignments(struct vlist_tree *tree,
493 struct vlist_node *node_new,
494 struct vlist_node *node_old)
496 struct device_prefix_assignment *old, *new;
497 old = container_of(node_old, struct device_prefix_assignment, node);
498 new = container_of(node_new, struct device_prefix_assignment, node);
500 // Assignments persist across interface reloads etc.
501 // so use indirection to avoid dangling pointers
502 struct interface *iface = vlist_find(&interfaces,
503 (node_new) ? new->name : old->name, iface, node);
505 if (node_old && node_new) {
506 new->addr = old->addr;
507 new->length = old->length;
508 } else if (node_old) {
510 interface_set_prefix_address(iface, false, old);
512 } else if (node_new) {
513 struct device_prefix *prefix = new->prefix;
514 uint64_t want = 1ULL << (64 - new->length);
515 prefix->avail &= ~(want - 1);
516 prefix->avail -= want;
519 uint64_t assigned = ~prefix->avail;
520 assigned &= (1ULL << (64 - prefix->length)) - 1;
521 assigned &= ~(want - 1);
524 new->addr = prefix->addr;
525 new->addr.s6_addr32[0] |=
526 htonl(assigned >> 32);
527 new->addr.s6_addr32[1] |=
528 htonl(assigned & 0xffffffffU);
529 new->addr.s6_addr[15] += 1;
532 if (node_new && (iface->state == IFS_UP || iface->state == IFS_SETUP))
533 interface_set_prefix_address(iface, true, new);
538 interface_ip_set_prefix_assignment(struct device_prefix *prefix,
539 struct interface *iface, uint8_t length)
541 struct device_prefix_assignment *assignment;
543 if (!length || length > 64) {
544 assignment = vlist_find(prefix->assignments, iface->name, assignment, node);
546 interface_set_prefix_address(iface, false, assignment);
548 uint64_t want = 1ULL << (64 - length);
551 if (prefix->avail < want && prefix->avail > 0) {
553 want = 1ULL << (64 - ++length);
554 } while (want > prefix->avail);
557 if (prefix->avail < want)
560 assignment = calloc_a(sizeof(*assignment),
561 &name, strlen(iface->name) + 1);
562 assignment->prefix = prefix;
563 assignment->length = length;
564 assignment->name = strcpy(name, iface->name);
566 vlist_add(prefix->assignments, &assignment->node, assignment->name);
571 interface_update_prefix(struct vlist_tree *tree,
572 struct vlist_node *node_new,
573 struct vlist_node *node_old)
575 struct device_prefix *prefix_old, *prefix_new;
576 prefix_old = container_of(node_old, struct device_prefix, node);
577 prefix_new = container_of(node_new, struct device_prefix, node);
579 struct device_route route;
580 memset(&route, 0, sizeof(route));
581 route.flags = DEVADDR_INET6;
582 route.metric = INT32_MAX;
583 route.mask = (node_new) ? prefix_new->length : prefix_old->length;
584 route.addr.in6 = (node_new) ? prefix_new->addr : prefix_old->addr;
586 if (node_old && node_new) {
587 prefix_new->avail = prefix_old->avail;
588 prefix_new->assignments = prefix_old->assignments;
589 prefix_old->assignments = NULL;
591 // Update all assignments
592 struct device_prefix_assignment *assignment;
593 struct vlist_tree *assignments = prefix_new->assignments;
594 vlist_for_each_element(assignments, assignment, node) {
595 assignment->prefix = prefix_new;
596 assignments->update(assignments,
597 &assignment->node, &assignment->node);
599 } else if (node_new) {
600 prefix_new->avail = 1ULL << (64 - prefix_new->length);
601 prefix_new->assignments = calloc(1, sizeof(*prefix_new->assignments));
602 vlist_init(prefix_new->assignments, avl_strcmp,
603 interface_update_prefix_assignments);
605 // Create initial assignments for interfaces
606 struct interface *iface;
607 vlist_for_each_element(&interfaces, iface, node)
608 interface_ip_set_prefix_assignment(prefix_new, iface,
609 iface->proto_ip.assignment_length);
611 // Set null-route to avoid routing loops
612 system_add_route(NULL, &route);
617 system_del_route(NULL, &route);
619 list_del(&prefix_old->head);
621 if (prefix_old->assignments) {
622 vlist_flush_all(prefix_old->assignments);
623 free(prefix_old->assignments);
629 list_add(&prefix_new->head, &prefixes);
632 struct device_prefix*
633 interface_ip_add_device_prefix(struct interface *iface, struct in6_addr *addr,
634 uint8_t length, time_t valid_until, time_t preferred_until)
636 struct device_prefix *prefix = calloc(1, sizeof(*prefix));
637 prefix->length = length;
638 prefix->addr = *addr;
639 prefix->preferred_until = preferred_until;
640 prefix->valid_until = valid_until;
641 prefix->iface = iface;
644 vlist_add(&iface->proto_ip.prefix, &prefix->node, &prefix->addr);
646 interface_update_prefix(NULL, &prefix->node, NULL);
652 interface_ip_set_ula_prefix(const char *prefix)
654 char buf[INET6_ADDRSTRLEN + 4] = {0}, *saveptr;
656 strncpy(buf, prefix, sizeof(buf) - 1);
657 char *prefixaddr = strtok_r(buf, "/", &saveptr);
659 struct in6_addr addr;
660 if (!prefixaddr || inet_pton(AF_INET6, prefixaddr, &addr) < 1) {
662 interface_update_prefix(NULL, NULL, &ula_prefix->node);
669 char *prefixlen = strtok_r(NULL, ",", &saveptr);
670 if (!prefixlen || (length = atoi(prefixlen)) < 1 || length > 64)
673 if (!ula_prefix || !IN6_ARE_ADDR_EQUAL(&addr, &ula_prefix->addr) ||
674 ula_prefix->length != length) {
676 interface_update_prefix(NULL, NULL, &ula_prefix->node);
678 ula_prefix = interface_ip_add_device_prefix(NULL, &addr, length, 0, 0);
683 interface_add_dns_server(struct interface_ip_settings *ip, const char *str)
685 struct dns_server *s;
687 s = calloc(1, sizeof(*s));
692 if (inet_pton(s->af, str, &s->addr.in))
696 if (inet_pton(s->af, str, &s->addr.in))
703 D(INTERFACE, "Add IPv%c DNS server: %s\n",
704 s->af == AF_INET6 ? '6' : '4', str);
705 vlist_simple_add(&ip->dns_servers, &s->node);
709 interface_add_dns_server_list(struct interface_ip_settings *ip, struct blob_attr *list)
711 struct blob_attr *cur;
714 blobmsg_for_each_attr(cur, list, rem) {
715 if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING)
718 if (!blobmsg_check_attr(cur, NULL))
721 interface_add_dns_server(ip, blobmsg_data(cur));
726 interface_add_dns_search_domain(struct interface_ip_settings *ip, const char *str)
728 struct dns_search_domain *s;
729 int len = strlen(str);
731 s = calloc(1, sizeof(*s) + len + 1);
735 D(INTERFACE, "Add DNS search domain: %s\n", str);
736 memcpy(s->name, str, len);
737 vlist_simple_add(&ip->dns_search, &s->node);
741 interface_add_dns_search_list(struct interface_ip_settings *ip, struct blob_attr *list)
743 struct blob_attr *cur;
746 blobmsg_for_each_attr(cur, list, rem) {
747 if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING)
750 if (!blobmsg_check_attr(cur, NULL))
753 interface_add_dns_search_domain(ip, blobmsg_data(cur));
758 write_resolv_conf_entries(FILE *f, struct interface_ip_settings *ip)
760 struct dns_server *s;
761 struct dns_search_domain *d;
765 vlist_simple_for_each_element(&ip->dns_servers, s, node) {
766 str = inet_ntop(s->af, &s->addr, buf, sizeof(buf));
770 fprintf(f, "nameserver %s\n", str);
773 vlist_simple_for_each_element(&ip->dns_search, d, node) {
774 fprintf(f, "search %s\n", d->name);
779 interface_write_resolv_conf(void)
781 struct interface *iface;
782 char *path = alloca(strlen(resolv_conf) + 5);
785 sprintf(path, "%s.tmp", resolv_conf);
787 f = fopen(path, "w");
789 D(INTERFACE, "Failed to open %s for writing\n", path);
793 vlist_for_each_element(&interfaces, iface, node) {
794 if (iface->state != IFS_UP)
797 if (vlist_simple_empty(&iface->proto_ip.dns_search) &&
798 vlist_simple_empty(&iface->proto_ip.dns_servers) &&
799 vlist_simple_empty(&iface->config_ip.dns_search) &&
800 vlist_simple_empty(&iface->config_ip.dns_servers))
803 fprintf(f, "# Interface %s\n", iface->name);
804 write_resolv_conf_entries(f, &iface->config_ip);
805 if (!iface->proto_ip.no_dns)
806 write_resolv_conf_entries(f, &iface->proto_ip);
809 if (rename(path, resolv_conf) < 0) {
810 D(INTERFACE, "Failed to replace %s\n", resolv_conf);
815 void interface_ip_set_enabled(struct interface_ip_settings *ip, bool enabled)
817 struct device_addr *addr;
818 struct device_route *route;
821 ip->enabled = enabled;
822 dev = ip->iface->l3_dev.dev;
826 vlist_for_each_element(&ip->addr, addr, node) {
827 if (addr->enabled == enabled)
831 system_add_address(dev, addr);
833 system_del_address(dev, addr);
834 addr->enabled = enabled;
837 vlist_for_each_element(&ip->route, route, node) {
838 bool _enabled = enabled;
840 if (!enable_route(ip, route))
843 if (route->enabled == _enabled)
847 if (!(route->flags & DEVROUTE_METRIC))
848 route->metric = ip->iface->metric;
850 system_add_route(dev, route);
852 system_del_route(dev, route);
853 route->enabled = _enabled;
858 interface_ip_update_start(struct interface_ip_settings *ip)
860 if (ip != &ip->iface->config_ip) {
861 vlist_simple_update(&ip->dns_servers);
862 vlist_simple_update(&ip->dns_search);
864 vlist_update(&ip->route);
865 vlist_update(&ip->addr);
866 vlist_update(&ip->prefix);
870 interface_ip_update_complete(struct interface_ip_settings *ip)
872 vlist_simple_flush(&ip->dns_servers);
873 vlist_simple_flush(&ip->dns_search);
874 vlist_flush(&ip->route);
875 vlist_flush(&ip->addr);
876 vlist_flush(&ip->prefix);
877 interface_write_resolv_conf();
881 interface_ip_flush(struct interface_ip_settings *ip)
883 if (ip == &ip->iface->proto_ip)
884 vlist_flush_all(&ip->iface->host_routes);
885 vlist_simple_flush_all(&ip->dns_servers);
886 vlist_simple_flush_all(&ip->dns_search);
887 vlist_flush_all(&ip->route);
888 vlist_flush_all(&ip->addr);
889 vlist_flush_all(&ip->prefix);
893 __interface_ip_init(struct interface_ip_settings *ip, struct interface *iface)
897 vlist_simple_init(&ip->dns_search, struct dns_search_domain, node);
898 vlist_simple_init(&ip->dns_servers, struct dns_server, node);
899 vlist_init(&ip->route, route_cmp, interface_update_proto_route);
900 vlist_init(&ip->addr, addr_cmp, interface_update_proto_addr);
901 vlist_init(&ip->prefix, prefix_cmp, interface_update_prefix);
905 interface_ip_init(struct interface *iface)
907 __interface_ip_init(&iface->proto_ip, iface);
908 __interface_ip_init(&iface->config_ip, iface);
909 vlist_init(&iface->host_routes, route_cmp, interface_update_host_route);
914 interface_ip_valid_until_handler(struct uloop_timeout *t)
916 time_t now = system_get_rtime();
917 struct interface *iface;
918 vlist_for_each_element(&interfaces, iface, node) {
919 if (iface->state != IFS_UP)
922 struct device_addr *addr, *addrp;
923 struct device_route *route, *routep;
924 struct device_prefix *pref, *prefp;
926 vlist_for_each_element_safe(&iface->proto_ip.addr, addr, node, addrp)
927 if (addr->valid_until && addr->valid_until < now)
928 vlist_delete(&iface->proto_ip.addr, &addr->node);
930 vlist_for_each_element_safe(&iface->proto_ip.route, route, node, routep)
931 if (route->valid_until && route->valid_until < now)
932 vlist_delete(&iface->proto_ip.route, &route->node);
934 vlist_for_each_element_safe(&iface->proto_ip.prefix, pref, node, prefp)
935 if (pref->valid_until && pref->valid_until < now)
936 vlist_delete(&iface->proto_ip.prefix, &pref->node);
940 uloop_timeout_set(t, 1000);
944 interface_ip_init_worker(void)
946 valid_until_timeout.cb = interface_ip_valid_until_handler;
947 uloop_timeout_set(&valid_until_timeout, 1000);