2 * netifd - network interface daemon
3 * Copyright (C) 2012 Felix Fietkau <nbd@openwrt.org>
4 * Copyright (C) 2012 Steven Barth <steven@midlink.org>
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
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.
20 #include <arpa/inet.h>
24 #include "interface.h"
25 #include "interface-ip.h"
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
},
53 const struct config_param_list route_attr_list
= {
54 .n_params
= __ROUTE_MAX
,
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
;
65 clear_if_addr(union if_addr
*a
, int mask
)
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
;
71 if (m_bytes
< sizeof(a
))
72 memset(p
+ m_bytes
, 0, sizeof(a
) - m_bytes
);
74 p
[m_bytes
- 1] &= ~m_clear
;
78 match_if_addr(union if_addr
*a1
, union if_addr
*a2
, int mask
)
80 union if_addr
*p1
, *p2
;
82 p1
= alloca(sizeof(*a1
));
83 p2
= alloca(sizeof(*a2
));
85 memcpy(p1
, a1
, sizeof(*a1
));
86 clear_if_addr(p1
, mask
);
87 memcpy(p2
, a2
, sizeof(*a2
));
88 clear_if_addr(p2
, mask
);
90 return !memcmp(p1
, p2
, sizeof(*p1
));
94 __find_ip_addr_target(struct interface_ip_settings
*ip
, union if_addr
*a
, bool v6
)
96 struct device_addr
*addr
;
98 vlist_for_each_element(&ip
->addr
, addr
, node
) {
102 if (v6
!= ((addr
->flags
& DEVADDR_FAMILY
) == DEVADDR_INET6
))
105 if (!match_if_addr(&addr
->addr
, a
, addr
->mask
))
115 __find_ip_route_target(struct interface_ip_settings
*ip
, union if_addr
*a
,
116 bool v6
, struct device_route
**res
)
118 struct device_route
*route
;
120 vlist_for_each_element(&ip
->route
, route
, node
) {
124 if (v6
!= ((route
->flags
& DEVADDR_FAMILY
) == DEVADDR_INET6
))
127 if (!match_if_addr(&route
->addr
, a
, route
->mask
))
130 if (route
->flags
& DEVROUTE_TABLE
)
133 if (!*res
|| route
->mask
< (*res
)->mask
)
139 interface_ip_find_addr_target(struct interface
*iface
, union if_addr
*a
, bool v6
)
141 return __find_ip_addr_target(&iface
->proto_ip
, a
, v6
) ||
142 __find_ip_addr_target(&iface
->config_ip
, a
, v6
);
146 interface_ip_find_route_target(struct interface
*iface
, union if_addr
*a
,
147 bool v6
, struct device_route
**route
)
149 __find_ip_route_target(&iface
->proto_ip
, a
, v6
, route
);
150 __find_ip_route_target(&iface
->config_ip
, a
, v6
, route
);
154 interface_ip_add_target_route(union if_addr
*addr
, bool v6
)
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
);
161 route
= calloc(1, sizeof(*route
));
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;
170 memcpy(&route
->addr
, addr
, addrsize
);
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
))
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
);
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
;
193 route
->iface
= iface
;
194 if (defaultroute_target
)
197 vlist_add(&iface
->host_routes
, &route
->node
, route
);
202 interface_ip_add_route(struct interface
*iface
, struct blob_attr
*attr
, bool v6
)
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
;
209 blobmsg_parse(route_attr
, __ROUTE_MAX
, tb
, blobmsg_data(attr
), blobmsg_data_len(attr
));
212 if ((cur
= tb
[ROUTE_INTERFACE
]) == NULL
)
215 iface
= vlist_find(&interfaces
, blobmsg_data(cur
), iface
, node
);
219 ip
= &iface
->config_ip
;
221 ip
= &iface
->proto_ip
;
224 route
= calloc(1, sizeof(*route
));
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))
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
));
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
));
250 if ((cur
= tb
[ROUTE_METRIC
]) != NULL
) {
251 route
->metric
= blobmsg_get_u32(cur
);
252 route
->flags
|= DEVROUTE_METRIC
;
255 if ((cur
= tb
[ROUTE_MTU
]) != NULL
) {
256 route
->mtu
= blobmsg_get_u32(cur
);
257 route
->flags
|= DEVROUTE_MTU
;
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
));
267 route
->flags
|= DEVROUTE_TABLE
;
270 if ((cur
= tb
[ROUTE_VALID
]) != NULL
)
271 route
->valid_until
= system_get_rtime() + blobmsg_get_u32(cur
);
273 vlist_add(&ip
->route
, &route
->node
, route
);
281 addr_cmp(const void *k1
, const void *k2
, void *ptr
)
283 return memcmp(k1
, k2
, sizeof(struct device_addr
) -
284 offsetof(struct device_addr
, flags
));
288 route_cmp(const void *k1
, const void *k2
, void *ptr
)
290 const struct device_route
*r1
= k1
, *r2
= k2
;
292 if (r1
->mask
!= r2
->mask
)
293 return r2
->mask
- r1
->mask
;
295 if (r1
->metric
!= r2
->metric
)
296 return r1
->metric
- r2
->metric
;
298 if (r1
->flags
!= r2
->flags
)
299 return r2
->flags
- r1
->flags
;
301 return memcmp(&r1
->addr
, &r2
->addr
, sizeof(r1
->addr
));
305 prefix_cmp(const void *k1
, const void *k2
, void *ptr
)
307 return memcmp(k1
, k2
, sizeof(struct device_prefix
) -
308 offsetof(struct device_prefix
, addr
));
312 interface_handle_subnet_route(struct interface
*iface
, struct device_addr
*addr
, bool add
)
314 struct device
*dev
= iface
->l3_dev
.dev
;
315 struct device_route route
;
317 memset(&route
, 0, sizeof(route
));
319 route
.flags
= addr
->flags
;
320 route
.mask
= addr
->mask
;
321 memcpy(&route
.addr
, &addr
->addr
, sizeof(route
.addr
));
322 clear_if_addr(&route
.addr
, route
.mask
);
325 route
.flags
|= DEVADDR_KERNEL
;
326 system_del_route(dev
, &route
);
328 if (!(addr
->flags
& DEVADDR_OFFLINK
)) {
329 route
.flags
&= ~DEVADDR_KERNEL
;
330 route
.metric
= iface
->metric
;
331 system_add_route(dev
, &route
);
334 if (!(addr
->flags
& DEVADDR_OFFLINK
))
335 system_del_route(dev
, &route
);
340 interface_update_proto_addr(struct vlist_tree
*tree
,
341 struct vlist_node
*node_new
,
342 struct vlist_node
*node_old
)
344 struct interface_ip_settings
*ip
;
345 struct interface
*iface
;
347 struct device_addr
*a_new
= NULL
, *a_old
= NULL
;
350 ip
= container_of(tree
, struct interface_ip_settings
, addr
);
352 dev
= iface
->l3_dev
.dev
;
355 a_new
= container_of(node_new
, struct device_addr
, node
);
357 if ((a_new
->flags
& DEVADDR_FAMILY
) == DEVADDR_INET4
&&
361 uint32_t *a
= (uint32_t *) &a_new
->addr
;
363 mask
>>= a_new
->mask
;
364 a_new
->broadcast
= *a
| htonl(mask
);
369 a_old
= container_of(node_old
, struct device_addr
, node
);
371 if (a_new
&& a_old
) {
374 if (a_old
->flags
!= a_new
->flags
||
375 a_old
->valid_until
!= a_new
->valid_until
||
376 a_old
->preferred_until
!= a_new
->preferred_until
)
379 if ((a_new
->flags
& DEVADDR_FAMILY
) == DEVADDR_INET4
&&
380 a_new
->broadcast
!= a_old
->broadcast
)
385 if (!(a_old
->flags
& DEVADDR_EXTERNAL
) && a_old
->enabled
&& !keep
) {
386 interface_handle_subnet_route(iface
, a_old
, false);
387 system_del_address(dev
, a_old
);
393 a_new
->enabled
= true;
394 if (!(a_new
->flags
& DEVADDR_EXTERNAL
) && !keep
) {
395 system_add_address(dev
, a_new
);
396 if ((a_new
->flags
& DEVADDR_OFFLINK
) || iface
->metric
)
397 interface_handle_subnet_route(iface
, a_new
, true);
403 enable_route(struct interface_ip_settings
*ip
, struct device_route
*route
)
405 if (ip
->no_defaultroute
&& !route
->mask
)
412 interface_update_proto_route(struct vlist_tree
*tree
,
413 struct vlist_node
*node_new
,
414 struct vlist_node
*node_old
)
416 struct interface_ip_settings
*ip
;
417 struct interface
*iface
;
419 struct device_route
*route_old
, *route_new
;
422 ip
= container_of(tree
, struct interface_ip_settings
, route
);
424 dev
= iface
->l3_dev
.dev
;
426 route_old
= container_of(node_old
, struct device_route
, node
);
427 route_new
= container_of(node_new
, struct device_route
, node
);
429 if (node_old
&& node_new
)
430 keep
= !memcmp(&route_old
->nexthop
, &route_new
->nexthop
, sizeof(route_old
->nexthop
));
433 if (!(route_old
->flags
& DEVADDR_EXTERNAL
) && route_old
->enabled
&& !keep
)
434 system_del_route(dev
, route_old
);
439 bool _enabled
= enable_route(ip
, route_new
);
441 if (!(route_new
->flags
& DEVROUTE_METRIC
))
442 route_new
->metric
= iface
->metric
;
444 if (!(route_new
->flags
& DEVADDR_EXTERNAL
) && !keep
&& _enabled
)
445 system_add_route(dev
, route_new
);
447 route_new
->iface
= iface
;
448 route_new
->enabled
= _enabled
;
453 interface_update_host_route(struct vlist_tree
*tree
,
454 struct vlist_node
*node_new
,
455 struct vlist_node
*node_old
)
457 struct interface
*iface
;
459 struct device_route
*route_old
, *route_new
;
461 iface
= container_of(tree
, struct interface
, host_routes
);
462 dev
= iface
->l3_dev
.dev
;
464 route_old
= container_of(node_old
, struct device_route
, node
);
465 route_new
= container_of(node_new
, struct device_route
, node
);
468 system_del_route(dev
, route_old
);
473 system_add_route(dev
, route_new
);
478 interface_set_prefix_address(struct device_prefix_assignment
*assignment
,
479 const struct device_prefix
*prefix
, struct interface
*iface
, bool add
)
481 const struct interface
*uplink
= prefix
->iface
;
482 if (!iface
->l3_dev
.dev
)
485 struct device
*l3_downlink
= iface
->l3_dev
.dev
;
487 struct device_addr addr
;
488 memset(&addr
, 0, sizeof(addr
));
489 addr
.addr
.in6
= prefix
->addr
;
490 addr
.addr
.in6
.s6_addr32
[1] |= htonl(assignment
->assigned
);
491 addr
.addr
.in6
.s6_addr
[15] += 1;
492 addr
.mask
= assignment
->length
;
493 addr
.flags
= DEVADDR_INET6
;
494 addr
.preferred_until
= prefix
->preferred_until
;
495 addr
.valid_until
= prefix
->valid_until
;
497 if (!add
&& assignment
->enabled
) {
498 time_t now
= system_get_rtime();
499 addr
.preferred_until
= now
;
500 if (!addr
.valid_until
|| addr
.valid_until
- now
> 7200)
501 addr
.valid_until
= now
+ 7200;
502 system_add_address(l3_downlink
, &addr
);
503 assignment
->enabled
= false;
504 } else if (add
&& (iface
->state
== IFS_UP
|| iface
->state
== IFS_SETUP
)) {
505 system_add_address(l3_downlink
, &addr
);
506 if (uplink
&& uplink
->l3_dev
.dev
) {
507 int mtu
= system_update_ipv6_mtu(
508 uplink
->l3_dev
.dev
, 0);
510 system_update_ipv6_mtu(l3_downlink
, mtu
);
512 assignment
->enabled
= true;
516 static bool interface_prefix_assign(struct list_head
*list
,
517 struct device_prefix_assignment
*assign
)
519 int32_t current
= 0, asize
= (1 << (64 - assign
->length
)) - 1;
520 struct device_prefix_assignment
*c
;
521 list_for_each_entry(c
, list
, head
) {
522 if (assign
->assigned
!= -1) {
523 if (assign
->assigned
> current
&& assign
->assigned
+ asize
< c
->assigned
) {
524 list_add_tail(&assign
->head
, &c
->head
);
527 } else if (assign
->assigned
== -1) {
528 current
= (current
+ asize
) & (~asize
);
529 if (current
+ asize
< c
->assigned
) {
530 assign
->assigned
= current
;
531 list_add_tail(&assign
->head
, &c
->head
);
535 current
= (c
->assigned
+ (1 << (64 - c
->length
)));
540 static void interface_update_prefix_assignments(struct device_prefix
*prefix
, bool setup
)
542 struct device_prefix_assignment
*c
;
543 struct interface
*iface
;
545 // Delete all assignments
546 while (!list_empty(&prefix
->assignments
)) {
547 c
= list_first_entry(&prefix
->assignments
,
548 struct device_prefix_assignment
, head
);
549 if ((iface
= vlist_find(&interfaces
, c
->name
, iface
, node
)))
550 interface_set_prefix_address(c
, prefix
, iface
, false);
558 // End-of-assignment sentinel
559 c
= malloc(sizeof(*c
) + 1);
560 c
->assigned
= 1 << (64 - prefix
->length
);
563 list_add(&c
->head
, &prefix
->assignments
);
566 if (prefix
->excl_length
> 0) {
567 const char name
[] = "!excluded";
568 c
= malloc(sizeof(*c
) + sizeof(name
));
569 c
->assigned
= ntohl(prefix
->excl_addr
.s6_addr32
[1]) &
570 ((1 << (64 - prefix
->length
)) - 1);
571 c
->length
= prefix
->excl_length
;
572 memcpy(c
->name
, name
, sizeof(name
));
573 list_add(&c
->head
, &prefix
->assignments
);
576 struct list_head assign_later
= LIST_HEAD_INIT(assign_later
);
577 vlist_for_each_element(&interfaces
, iface
, node
) {
578 if (iface
->config_ip
.assignment_length
< 48 ||
579 iface
->config_ip
.assignment_length
> 64)
582 size_t namelen
= strlen(iface
->name
) + 1;
583 c
= malloc(sizeof(*c
) + namelen
);
584 c
->length
= iface
->config_ip
.assignment_length
;
585 c
->assigned
= iface
->config_ip
.assignment_hint
;
587 memcpy(c
->name
, iface
->name
, namelen
);
589 // First process all custom assignments, put all others in later-list
590 if (c
->assigned
== -1 || !interface_prefix_assign(&prefix
->assignments
, c
)) {
591 if (c
->assigned
!= -1) {
593 netifd_log_message(L_WARNING
, "Failed to assign requested subprefix "
594 "of size %hhu for %s, trying other\n", c
->length
, c
->name
);
596 list_add_tail(&c
->head
, &assign_later
);
600 // Then try to assign all other + failed custom assignments
601 while (!list_empty(&assign_later
)) {
602 c
= list_first_entry(&assign_later
, struct device_prefix_assignment
, head
);
604 if (!interface_prefix_assign(&prefix
->assignments
, c
)) {
605 netifd_log_message(L_WARNING
, "Failed to assign subprefix "
606 "of size %hhu for %s\n", c
->length
, c
->name
);
611 list_for_each_entry(c
, &prefix
->assignments
, head
)
612 if ((iface
= vlist_find(&interfaces
, c
->name
, iface
, node
)))
613 interface_set_prefix_address(c
, prefix
, iface
, true);
617 void interface_refresh_assignments(bool hint
)
619 static bool refresh
= false;
620 if (!hint
&& refresh
) {
621 struct device_prefix
*p
;
622 list_for_each_entry(p
, &prefixes
, head
)
623 interface_update_prefix_assignments(p
, true);
630 interface_update_prefix(struct vlist_tree
*tree
,
631 struct vlist_node
*node_new
,
632 struct vlist_node
*node_old
)
634 struct device_prefix
*prefix_old
, *prefix_new
;
635 prefix_old
= container_of(node_old
, struct device_prefix
, node
);
636 prefix_new
= container_of(node_new
, struct device_prefix
, node
);
638 struct device_route route
;
639 memset(&route
, 0, sizeof(route
));
640 route
.flags
= DEVADDR_INET6
;
641 route
.metric
= INT32_MAX
;
642 route
.mask
= (node_new
) ? prefix_new
->length
: prefix_old
->length
;
643 route
.addr
.in6
= (node_new
) ? prefix_new
->addr
: prefix_old
->addr
;
645 struct device_prefix_assignment
*c
;
646 struct interface
*iface
;
648 if (node_old
&& node_new
) {
649 // Move assignments and refresh addresses to update valid times
650 list_splice(&prefix_old
->assignments
, &prefix_new
->assignments
);
652 list_for_each_entry(c
, &prefix_new
->assignments
, head
)
653 if ((iface
= vlist_find(&interfaces
, c
->name
, iface
, node
)))
654 interface_set_prefix_address(c
, prefix_new
, iface
, true);
655 } else if (node_new
) {
656 // Set null-route to avoid routing loops
657 system_add_route(NULL
, &route
);
659 interface_update_prefix_assignments(prefix_new
, true);
660 } else if (node_old
) {
661 interface_update_prefix_assignments(prefix_old
, false);
664 system_del_route(NULL
, &route
);
668 list_del(&prefix_old
->head
);
673 list_add(&prefix_new
->head
, &prefixes
);
677 struct device_prefix
*
678 interface_ip_add_device_prefix(struct interface
*iface
, struct in6_addr
*addr
,
679 uint8_t length
, time_t valid_until
, time_t preferred_until
,
680 struct in6_addr
*excl_addr
, uint8_t excl_length
)
682 struct device_prefix
*prefix
= calloc(1, sizeof(*prefix
));
683 prefix
->length
= length
;
684 prefix
->addr
= *addr
;
685 prefix
->preferred_until
= preferred_until
;
686 prefix
->valid_until
= valid_until
;
687 prefix
->iface
= iface
;
688 INIT_LIST_HEAD(&prefix
->assignments
);
691 prefix
->excl_addr
= *excl_addr
;
692 prefix
->excl_length
= excl_length
;
696 vlist_add(&iface
->proto_ip
.prefix
, &prefix
->node
, &prefix
->addr
);
698 interface_update_prefix(NULL
, &prefix
->node
, NULL
);
704 interface_ip_set_ula_prefix(const char *prefix
)
706 char buf
[INET6_ADDRSTRLEN
+ 4] = {0}, *saveptr
;
708 strncpy(buf
, prefix
, sizeof(buf
) - 1);
709 char *prefixaddr
= strtok_r(buf
, "/", &saveptr
);
711 struct in6_addr addr
;
712 if (!prefixaddr
|| inet_pton(AF_INET6
, prefixaddr
, &addr
) < 1) {
714 interface_update_prefix(NULL
, NULL
, &ula_prefix
->node
);
721 char *prefixlen
= strtok_r(NULL
, ",", &saveptr
);
722 if (!prefixlen
|| (length
= atoi(prefixlen
)) < 1 || length
> 64)
725 if (!ula_prefix
|| !IN6_ARE_ADDR_EQUAL(&addr
, &ula_prefix
->addr
) ||
726 ula_prefix
->length
!= length
) {
728 interface_update_prefix(NULL
, NULL
, &ula_prefix
->node
);
730 ula_prefix
= interface_ip_add_device_prefix(NULL
, &addr
, length
,
736 interface_add_dns_server(struct interface_ip_settings
*ip
, const char *str
)
738 struct dns_server
*s
;
740 s
= calloc(1, sizeof(*s
));
745 if (inet_pton(s
->af
, str
, &s
->addr
.in
))
749 if (inet_pton(s
->af
, str
, &s
->addr
.in
))
756 D(INTERFACE
, "Add IPv%c DNS server: %s\n",
757 s
->af
== AF_INET6
? '6' : '4', str
);
758 vlist_simple_add(&ip
->dns_servers
, &s
->node
);
762 interface_add_dns_server_list(struct interface_ip_settings
*ip
, struct blob_attr
*list
)
764 struct blob_attr
*cur
;
767 blobmsg_for_each_attr(cur
, list
, rem
) {
768 if (blobmsg_type(cur
) != BLOBMSG_TYPE_STRING
)
771 if (!blobmsg_check_attr(cur
, NULL
))
774 interface_add_dns_server(ip
, blobmsg_data(cur
));
779 interface_add_dns_search_domain(struct interface_ip_settings
*ip
, const char *str
)
781 struct dns_search_domain
*s
;
782 int len
= strlen(str
);
784 s
= calloc(1, sizeof(*s
) + len
+ 1);
788 D(INTERFACE
, "Add DNS search domain: %s\n", str
);
789 memcpy(s
->name
, str
, len
);
790 vlist_simple_add(&ip
->dns_search
, &s
->node
);
794 interface_add_dns_search_list(struct interface_ip_settings
*ip
, struct blob_attr
*list
)
796 struct blob_attr
*cur
;
799 blobmsg_for_each_attr(cur
, list
, rem
) {
800 if (blobmsg_type(cur
) != BLOBMSG_TYPE_STRING
)
803 if (!blobmsg_check_attr(cur
, NULL
))
806 interface_add_dns_search_domain(ip
, blobmsg_data(cur
));
811 write_resolv_conf_entries(FILE *f
, struct interface_ip_settings
*ip
)
813 struct dns_server
*s
;
814 struct dns_search_domain
*d
;
816 char buf
[INET6_ADDRSTRLEN
];
818 vlist_simple_for_each_element(&ip
->dns_servers
, s
, node
) {
819 str
= inet_ntop(s
->af
, &s
->addr
, buf
, sizeof(buf
));
823 fprintf(f
, "nameserver %s\n", str
);
826 vlist_simple_for_each_element(&ip
->dns_search
, d
, node
) {
827 fprintf(f
, "search %s\n", d
->name
);
832 interface_write_resolv_conf(void)
834 struct interface
*iface
;
835 char *path
= alloca(strlen(resolv_conf
) + 5);
837 uint32_t crcold
, crcnew
;
839 sprintf(path
, "%s.tmp", resolv_conf
);
841 f
= fopen(path
, "w+");
843 D(INTERFACE
, "Failed to open %s for writing\n", path
);
847 vlist_for_each_element(&interfaces
, iface
, node
) {
848 if (iface
->state
!= IFS_UP
)
851 if (vlist_simple_empty(&iface
->proto_ip
.dns_search
) &&
852 vlist_simple_empty(&iface
->proto_ip
.dns_servers
) &&
853 vlist_simple_empty(&iface
->config_ip
.dns_search
) &&
854 vlist_simple_empty(&iface
->config_ip
.dns_servers
))
857 fprintf(f
, "# Interface %s\n", iface
->name
);
858 write_resolv_conf_entries(f
, &iface
->config_ip
);
859 if (!iface
->proto_ip
.no_dns
)
860 write_resolv_conf_entries(f
, &iface
->proto_ip
);
864 crcnew
= crc32_file(f
);
868 f
= fopen(resolv_conf
, "r");
870 crcold
= crc32_file(f
);
874 if (crcold
== crcnew
) {
876 } else if (rename(path
, resolv_conf
) < 0) {
877 D(INTERFACE
, "Failed to replace %s\n", resolv_conf
);
882 void interface_ip_set_enabled(struct interface_ip_settings
*ip
, bool enabled
)
884 struct device_addr
*addr
;
885 struct device_route
*route
;
888 ip
->enabled
= enabled
;
889 dev
= ip
->iface
->l3_dev
.dev
;
893 vlist_for_each_element(&ip
->addr
, addr
, node
) {
894 if (addr
->enabled
== enabled
)
898 system_add_address(dev
, addr
);
900 system_del_address(dev
, addr
);
901 addr
->enabled
= enabled
;
904 vlist_for_each_element(&ip
->route
, route
, node
) {
905 bool _enabled
= enabled
;
907 if (!enable_route(ip
, route
))
910 if (route
->enabled
== _enabled
)
914 if (!(route
->flags
& DEVROUTE_METRIC
))
915 route
->metric
= ip
->iface
->metric
;
917 system_add_route(dev
, route
);
919 system_del_route(dev
, route
);
920 route
->enabled
= _enabled
;
923 struct device_prefix
*c
;
924 struct device_prefix_assignment
*a
;
925 list_for_each_entry(c
, &prefixes
, head
)
926 list_for_each_entry(a
, &c
->assignments
, head
)
927 if (!strcmp(a
->name
, ip
->iface
->name
))
928 interface_set_prefix_address(a
, c
, ip
->iface
, enabled
);
932 interface_ip_update_start(struct interface_ip_settings
*ip
)
934 if (ip
!= &ip
->iface
->config_ip
) {
935 vlist_simple_update(&ip
->dns_servers
);
936 vlist_simple_update(&ip
->dns_search
);
938 vlist_update(&ip
->route
);
939 vlist_update(&ip
->addr
);
940 vlist_update(&ip
->prefix
);
944 interface_ip_update_complete(struct interface_ip_settings
*ip
)
946 vlist_simple_flush(&ip
->dns_servers
);
947 vlist_simple_flush(&ip
->dns_search
);
948 vlist_flush(&ip
->route
);
949 vlist_flush(&ip
->addr
);
950 vlist_flush(&ip
->prefix
);
951 interface_write_resolv_conf();
955 interface_ip_flush(struct interface_ip_settings
*ip
)
957 if (ip
== &ip
->iface
->proto_ip
)
958 vlist_flush_all(&ip
->iface
->host_routes
);
959 vlist_simple_flush_all(&ip
->dns_servers
);
960 vlist_simple_flush_all(&ip
->dns_search
);
961 vlist_flush_all(&ip
->route
);
962 vlist_flush_all(&ip
->addr
);
963 vlist_flush_all(&ip
->prefix
);
967 __interface_ip_init(struct interface_ip_settings
*ip
, struct interface
*iface
)
971 vlist_simple_init(&ip
->dns_search
, struct dns_search_domain
, node
);
972 vlist_simple_init(&ip
->dns_servers
, struct dns_server
, node
);
973 vlist_init(&ip
->route
, route_cmp
, interface_update_proto_route
);
974 vlist_init(&ip
->addr
, addr_cmp
, interface_update_proto_addr
);
975 vlist_init(&ip
->prefix
, prefix_cmp
, interface_update_prefix
);
979 interface_ip_init(struct interface
*iface
)
981 __interface_ip_init(&iface
->proto_ip
, iface
);
982 __interface_ip_init(&iface
->config_ip
, iface
);
983 vlist_init(&iface
->host_routes
, route_cmp
, interface_update_host_route
);
988 interface_ip_valid_until_handler(struct uloop_timeout
*t
)
990 time_t now
= system_get_rtime();
991 struct interface
*iface
;
992 vlist_for_each_element(&interfaces
, iface
, node
) {
993 if (iface
->state
!= IFS_UP
)
996 struct device_addr
*addr
, *addrp
;
997 struct device_route
*route
, *routep
;
998 struct device_prefix
*pref
, *prefp
;
1000 vlist_for_each_element_safe(&iface
->proto_ip
.addr
, addr
, node
, addrp
)
1001 if (addr
->valid_until
&& addr
->valid_until
< now
)
1002 vlist_delete(&iface
->proto_ip
.addr
, &addr
->node
);
1004 vlist_for_each_element_safe(&iface
->proto_ip
.route
, route
, node
, routep
)
1005 if (route
->valid_until
&& route
->valid_until
< now
)
1006 vlist_delete(&iface
->proto_ip
.route
, &route
->node
);
1008 vlist_for_each_element_safe(&iface
->proto_ip
.prefix
, pref
, node
, prefp
)
1009 if (pref
->valid_until
&& pref
->valid_until
< now
)
1010 vlist_delete(&iface
->proto_ip
.prefix
, &pref
->node
);
1014 uloop_timeout_set(t
, 1000);
1018 interface_ip_init_worker(void)
1020 valid_until_timeout
.cb
= interface_ip_valid_until_handler
;
1021 uloop_timeout_set(&valid_until_timeout
, 1000);