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 device_prefix_assignment
*assignment
,
453 const struct device_prefix
*prefix
, struct interface
*iface
, bool add
)
455 const struct interface
*uplink
= 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
= prefix
->addr
;
464 addr
.addr
.in6
.s6_addr32
[1] |= htonl(assignment
->assigned
);
465 addr
.addr
.in6
.s6_addr
[15] += 1;
466 addr
.mask
= assignment
->length
;
467 addr
.flags
= DEVADDR_INET6
;
468 addr
.preferred_until
= prefix
->preferred_until
;
469 addr
.valid_until
= prefix
->valid_until
;
471 if (!add
&& assignment
->enabled
) {
472 time_t now
= system_get_rtime();
473 addr
.preferred_until
= now
;
474 if (!addr
.valid_until
|| addr
.valid_until
- now
> 7200)
475 addr
.valid_until
= now
+ 7200;
476 system_add_address(l3_downlink
, &addr
);
477 assignment
->enabled
= false;
478 } else if (add
&& (iface
->state
== IFS_UP
|| iface
->state
== IFS_SETUP
)) {
479 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
);
486 assignment
->enabled
= true;
490 static bool interface_prefix_assign(struct list_head
*list
,
491 struct device_prefix_assignment
*assign
)
493 int32_t current
= 0, asize
= (1 << (64 - assign
->length
)) - 1;
494 struct device_prefix_assignment
*c
;
495 list_for_each_entry(c
, list
, head
) {
496 if (assign
->assigned
!= -1) {
497 if (assign
->assigned
> current
&& assign
->assigned
+ asize
< c
->assigned
) {
498 list_add_tail(&assign
->head
, &c
->head
);
501 } else if (assign
->assigned
== -1) {
502 current
= (current
+ asize
) & (~asize
);
503 if (current
+ asize
< c
->assigned
) {
504 assign
->assigned
= current
;
505 list_add_tail(&assign
->head
, &c
->head
);
509 current
= (c
->assigned
+ (1 << (64 - c
->length
)));
514 static void interface_update_prefix_assignments(struct device_prefix
*prefix
, bool setup
)
516 struct device_prefix_assignment
*c
;
517 struct interface
*iface
;
519 // Delete all assignments
520 while (!list_empty(&prefix
->assignments
)) {
521 c
= list_first_entry(&prefix
->assignments
,
522 struct device_prefix_assignment
, head
);
523 if ((iface
= vlist_find(&interfaces
, c
->name
, iface
, node
)))
524 interface_set_prefix_address(c
, prefix
, iface
, false);
532 // End-of-assignment sentinel
533 c
= malloc(sizeof(*c
) + 1);
534 c
->assigned
= 1 << (64 - prefix
->length
);
537 list_add(&c
->head
, &prefix
->assignments
);
540 if (prefix
->excl_length
> 0) {
541 const char name
[] = "!excluded";
542 c
= malloc(sizeof(*c
) + sizeof(name
));
543 c
->assigned
= ntohl(prefix
->excl_addr
.s6_addr32
[1]) &
544 ((1 << (64 - prefix
->length
)) - 1);
545 c
->length
= prefix
->excl_length
;
546 memcpy(c
->name
, name
, sizeof(name
));
547 list_add(&c
->head
, &prefix
->assignments
);
550 struct list_head assign_later
= LIST_HEAD_INIT(assign_later
);
551 vlist_for_each_element(&interfaces
, iface
, node
) {
552 if (iface
->config_ip
.assignment_length
< 48 ||
553 iface
->config_ip
.assignment_length
> 64)
556 size_t namelen
= strlen(iface
->name
) + 1;
557 c
= malloc(sizeof(*c
) + namelen
);
558 c
->length
= iface
->config_ip
.assignment_length
;
559 c
->assigned
= iface
->config_ip
.assignment_hint
;
561 memcpy(c
->name
, iface
->name
, namelen
);
563 // First process all custom assignments, put all others in later-list
564 if (c
->assigned
== -1 || !interface_prefix_assign(&prefix
->assignments
, c
)) {
565 if (c
->assigned
!= -1) {
567 netifd_log_message(L_WARNING
, "Failed to assign requested subprefix "
568 "of size %hhu for %s, trying other\n", c
->length
, c
->name
);
570 list_add_tail(&c
->head
, &assign_later
);
574 // Then try to assign all other + failed custom assignments
575 while (!list_empty(&assign_later
)) {
576 c
= list_first_entry(&assign_later
, struct device_prefix_assignment
, head
);
578 if (!interface_prefix_assign(&prefix
->assignments
, c
)) {
579 netifd_log_message(L_WARNING
, "Failed to assign subprefix "
580 "of size %hhu for %s\n", c
->length
, c
->name
);
585 list_for_each_entry(c
, &prefix
->assignments
, head
)
586 if ((iface
= vlist_find(&interfaces
, c
->name
, iface
, node
)))
587 interface_set_prefix_address(c
, prefix
, iface
, true);
591 void interface_refresh_assignments(bool hint
)
593 static bool refresh
= false;
594 if (!hint
&& refresh
) {
595 struct device_prefix
*p
;
596 list_for_each_entry(p
, &prefixes
, head
)
597 interface_update_prefix_assignments(p
, true);
604 interface_update_prefix(struct vlist_tree
*tree
,
605 struct vlist_node
*node_new
,
606 struct vlist_node
*node_old
)
608 struct device_prefix
*prefix_old
, *prefix_new
;
609 prefix_old
= container_of(node_old
, struct device_prefix
, node
);
610 prefix_new
= container_of(node_new
, struct device_prefix
, node
);
612 struct device_route route
;
613 memset(&route
, 0, sizeof(route
));
614 route
.flags
= DEVADDR_INET6
;
615 route
.metric
= INT32_MAX
;
616 route
.mask
= (node_new
) ? prefix_new
->length
: prefix_old
->length
;
617 route
.addr
.in6
= (node_new
) ? prefix_new
->addr
: prefix_old
->addr
;
619 struct device_prefix_assignment
*c
;
620 struct interface
*iface
;
622 if (node_old
&& node_new
) {
623 // Move assignments and refresh addresses to update valid times
624 list_splice_init(&prefix_old
->assignments
, &prefix_new
->assignments
);
626 list_for_each_entry(c
, &prefix_new
->assignments
, head
)
627 if ((iface
= vlist_find(&interfaces
, c
->name
, iface
, node
)))
628 interface_set_prefix_address(c
, prefix_new
, iface
, true);
629 } else if (node_new
) {
630 // Set null-route to avoid routing loops
631 system_add_route(NULL
, &route
);
633 INIT_LIST_HEAD(&prefix_new
->assignments
);
634 interface_update_prefix_assignments(prefix_new
, true);
635 } else if (node_old
) {
636 interface_update_prefix_assignments(prefix_old
, false);
639 system_del_route(NULL
, &route
);
643 list_del(&prefix_old
->head
);
648 list_add(&prefix_new
->head
, &prefixes
);
652 struct device_prefix
*
653 interface_ip_add_device_prefix(struct interface
*iface
, struct in6_addr
*addr
,
654 uint8_t length
, time_t valid_until
, time_t preferred_until
,
655 struct in6_addr
*excl_addr
, uint8_t excl_length
)
657 struct device_prefix
*prefix
= calloc(1, sizeof(*prefix
));
658 prefix
->length
= length
;
659 prefix
->addr
= *addr
;
660 prefix
->preferred_until
= preferred_until
;
661 prefix
->valid_until
= valid_until
;
662 prefix
->iface
= iface
;
665 prefix
->excl_addr
= *excl_addr
;
666 prefix
->excl_length
= excl_length
;
670 vlist_add(&iface
->proto_ip
.prefix
, &prefix
->node
, &prefix
->addr
);
672 interface_update_prefix(NULL
, &prefix
->node
, NULL
);
678 interface_ip_set_ula_prefix(const char *prefix
)
680 char buf
[INET6_ADDRSTRLEN
+ 4] = {0}, *saveptr
;
682 strncpy(buf
, prefix
, sizeof(buf
) - 1);
683 char *prefixaddr
= strtok_r(buf
, "/", &saveptr
);
685 struct in6_addr addr
;
686 if (!prefixaddr
|| inet_pton(AF_INET6
, prefixaddr
, &addr
) < 1) {
688 interface_update_prefix(NULL
, NULL
, &ula_prefix
->node
);
695 char *prefixlen
= strtok_r(NULL
, ",", &saveptr
);
696 if (!prefixlen
|| (length
= atoi(prefixlen
)) < 1 || length
> 64)
699 if (!ula_prefix
|| !IN6_ARE_ADDR_EQUAL(&addr
, &ula_prefix
->addr
) ||
700 ula_prefix
->length
!= length
) {
702 interface_update_prefix(NULL
, NULL
, &ula_prefix
->node
);
704 ula_prefix
= interface_ip_add_device_prefix(NULL
, &addr
, length
,
710 interface_add_dns_server(struct interface_ip_settings
*ip
, const char *str
)
712 struct dns_server
*s
;
714 s
= calloc(1, sizeof(*s
));
719 if (inet_pton(s
->af
, str
, &s
->addr
.in
))
723 if (inet_pton(s
->af
, str
, &s
->addr
.in
))
730 D(INTERFACE
, "Add IPv%c DNS server: %s\n",
731 s
->af
== AF_INET6
? '6' : '4', str
);
732 vlist_simple_add(&ip
->dns_servers
, &s
->node
);
736 interface_add_dns_server_list(struct interface_ip_settings
*ip
, struct blob_attr
*list
)
738 struct blob_attr
*cur
;
741 blobmsg_for_each_attr(cur
, list
, rem
) {
742 if (blobmsg_type(cur
) != BLOBMSG_TYPE_STRING
)
745 if (!blobmsg_check_attr(cur
, NULL
))
748 interface_add_dns_server(ip
, blobmsg_data(cur
));
753 interface_add_dns_search_domain(struct interface_ip_settings
*ip
, const char *str
)
755 struct dns_search_domain
*s
;
756 int len
= strlen(str
);
758 s
= calloc(1, sizeof(*s
) + len
+ 1);
762 D(INTERFACE
, "Add DNS search domain: %s\n", str
);
763 memcpy(s
->name
, str
, len
);
764 vlist_simple_add(&ip
->dns_search
, &s
->node
);
768 interface_add_dns_search_list(struct interface_ip_settings
*ip
, struct blob_attr
*list
)
770 struct blob_attr
*cur
;
773 blobmsg_for_each_attr(cur
, list
, rem
) {
774 if (blobmsg_type(cur
) != BLOBMSG_TYPE_STRING
)
777 if (!blobmsg_check_attr(cur
, NULL
))
780 interface_add_dns_search_domain(ip
, blobmsg_data(cur
));
785 write_resolv_conf_entries(FILE *f
, struct interface_ip_settings
*ip
)
787 struct dns_server
*s
;
788 struct dns_search_domain
*d
;
790 char buf
[INET6_ADDRSTRLEN
];
792 vlist_simple_for_each_element(&ip
->dns_servers
, s
, node
) {
793 str
= inet_ntop(s
->af
, &s
->addr
, buf
, sizeof(buf
));
797 fprintf(f
, "nameserver %s\n", str
);
800 vlist_simple_for_each_element(&ip
->dns_search
, d
, node
) {
801 fprintf(f
, "search %s\n", d
->name
);
806 interface_write_resolv_conf(void)
808 struct interface
*iface
;
809 char *path
= alloca(strlen(resolv_conf
) + 5);
811 uint32_t crcold
, crcnew
;
813 sprintf(path
, "%s.tmp", resolv_conf
);
815 f
= fopen(path
, "w+");
817 D(INTERFACE
, "Failed to open %s for writing\n", path
);
821 vlist_for_each_element(&interfaces
, iface
, node
) {
822 if (iface
->state
!= IFS_UP
)
825 if (vlist_simple_empty(&iface
->proto_ip
.dns_search
) &&
826 vlist_simple_empty(&iface
->proto_ip
.dns_servers
) &&
827 vlist_simple_empty(&iface
->config_ip
.dns_search
) &&
828 vlist_simple_empty(&iface
->config_ip
.dns_servers
))
831 fprintf(f
, "# Interface %s\n", iface
->name
);
832 write_resolv_conf_entries(f
, &iface
->config_ip
);
833 if (!iface
->proto_ip
.no_dns
)
834 write_resolv_conf_entries(f
, &iface
->proto_ip
);
838 crcnew
= crc32_file(f
);
842 f
= fopen(resolv_conf
, "r");
844 crcold
= crc32_file(f
);
848 if (crcold
== crcnew
) {
850 } else if (rename(path
, resolv_conf
) < 0) {
851 D(INTERFACE
, "Failed to replace %s\n", resolv_conf
);
856 void interface_ip_set_enabled(struct interface_ip_settings
*ip
, bool enabled
)
858 struct device_addr
*addr
;
859 struct device_route
*route
;
862 ip
->enabled
= enabled
;
863 dev
= ip
->iface
->l3_dev
.dev
;
867 vlist_for_each_element(&ip
->addr
, addr
, node
) {
868 if (addr
->enabled
== enabled
)
872 system_add_address(dev
, addr
);
874 system_del_address(dev
, addr
);
875 addr
->enabled
= enabled
;
878 vlist_for_each_element(&ip
->route
, route
, node
) {
879 bool _enabled
= enabled
;
881 if (!enable_route(ip
, route
))
884 if (route
->enabled
== _enabled
)
888 if (!(route
->flags
& DEVROUTE_METRIC
))
889 route
->metric
= ip
->iface
->metric
;
891 system_add_route(dev
, route
);
893 system_del_route(dev
, route
);
894 route
->enabled
= _enabled
;
897 struct device_prefix
*c
;
898 struct device_prefix_assignment
*a
;
899 list_for_each_entry(c
, &prefixes
, head
)
900 list_for_each_entry(a
, &c
->assignments
, head
)
901 if (!strcmp(a
->name
, ip
->iface
->name
))
902 interface_set_prefix_address(a
, c
, ip
->iface
, enabled
);
906 interface_ip_update_start(struct interface_ip_settings
*ip
)
908 if (ip
!= &ip
->iface
->config_ip
) {
909 vlist_simple_update(&ip
->dns_servers
);
910 vlist_simple_update(&ip
->dns_search
);
912 vlist_update(&ip
->route
);
913 vlist_update(&ip
->addr
);
914 vlist_update(&ip
->prefix
);
918 interface_ip_update_complete(struct interface_ip_settings
*ip
)
920 vlist_simple_flush(&ip
->dns_servers
);
921 vlist_simple_flush(&ip
->dns_search
);
922 vlist_flush(&ip
->route
);
923 vlist_flush(&ip
->addr
);
924 vlist_flush(&ip
->prefix
);
925 interface_write_resolv_conf();
929 interface_ip_flush(struct interface_ip_settings
*ip
)
931 if (ip
== &ip
->iface
->proto_ip
)
932 vlist_flush_all(&ip
->iface
->host_routes
);
933 vlist_simple_flush_all(&ip
->dns_servers
);
934 vlist_simple_flush_all(&ip
->dns_search
);
935 vlist_flush_all(&ip
->route
);
936 vlist_flush_all(&ip
->addr
);
937 vlist_flush_all(&ip
->prefix
);
941 __interface_ip_init(struct interface_ip_settings
*ip
, struct interface
*iface
)
945 vlist_simple_init(&ip
->dns_search
, struct dns_search_domain
, node
);
946 vlist_simple_init(&ip
->dns_servers
, struct dns_server
, node
);
947 vlist_init(&ip
->route
, route_cmp
, interface_update_proto_route
);
948 vlist_init(&ip
->addr
, addr_cmp
, interface_update_proto_addr
);
949 vlist_init(&ip
->prefix
, prefix_cmp
, interface_update_prefix
);
953 interface_ip_init(struct interface
*iface
)
955 __interface_ip_init(&iface
->proto_ip
, iface
);
956 __interface_ip_init(&iface
->config_ip
, iface
);
957 vlist_init(&iface
->host_routes
, route_cmp
, interface_update_host_route
);
962 interface_ip_valid_until_handler(struct uloop_timeout
*t
)
964 time_t now
= system_get_rtime();
965 struct interface
*iface
;
966 vlist_for_each_element(&interfaces
, iface
, node
) {
967 if (iface
->state
!= IFS_UP
)
970 struct device_addr
*addr
, *addrp
;
971 struct device_route
*route
, *routep
;
972 struct device_prefix
*pref
, *prefp
;
974 vlist_for_each_element_safe(&iface
->proto_ip
.addr
, addr
, node
, addrp
)
975 if (addr
->valid_until
&& addr
->valid_until
< now
)
976 vlist_delete(&iface
->proto_ip
.addr
, &addr
->node
);
978 vlist_for_each_element_safe(&iface
->proto_ip
.route
, route
, node
, routep
)
979 if (route
->valid_until
&& route
->valid_until
< now
)
980 vlist_delete(&iface
->proto_ip
.route
, &route
->node
);
982 vlist_for_each_element_safe(&iface
->proto_ip
.prefix
, pref
, node
, prefp
)
983 if (pref
->valid_until
&& pref
->valid_until
< now
)
984 vlist_delete(&iface
->proto_ip
.prefix
, &pref
->node
);
988 uloop_timeout_set(t
, 1000);
992 interface_ip_init_worker(void)
994 valid_until_timeout
.cb
= interface_ip_valid_until_handler
;
995 uloop_timeout_set(&valid_until_timeout
, 1000);