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"
43 static const struct blobmsg_policy route_attr
[__ROUTE_MAX
] = {
44 [ROUTE_INTERFACE
] = { .name
= "interface", .type
= BLOBMSG_TYPE_STRING
},
45 [ROUTE_TARGET
] = { .name
= "target", .type
= BLOBMSG_TYPE_STRING
},
46 [ROUTE_MASK
] = { .name
= "netmask", .type
= BLOBMSG_TYPE_STRING
},
47 [ROUTE_GATEWAY
] = { .name
= "gateway", .type
= BLOBMSG_TYPE_STRING
},
48 [ROUTE_METRIC
] = { .name
= "metric", .type
= BLOBMSG_TYPE_INT32
},
49 [ROUTE_MTU
] = { .name
= "mtu", .type
= BLOBMSG_TYPE_INT32
},
50 [ROUTE_TABLE
] = { .name
= "table", .type
= BLOBMSG_TYPE_STRING
},
51 [ROUTE_VALID
] = { .name
= "valid", .type
= BLOBMSG_TYPE_INT32
},
52 [ROUTE_SOURCE
] = { .name
= "source", .type
= BLOBMSG_TYPE_STRING
},
55 const struct uci_blob_param_list route_attr_list
= {
56 .n_params
= __ROUTE_MAX
,
61 struct list_head prefixes
= LIST_HEAD_INIT(prefixes
);
62 static struct device_prefix
*ula_prefix
= NULL
;
63 static struct uloop_timeout valid_until_timeout
;
67 clear_if_addr(union if_addr
*a
, int mask
)
69 int m_bytes
= (mask
+ 7) / 8;
70 uint8_t m_clear
= (1 << (m_bytes
* 8 - mask
)) - 1;
71 uint8_t *p
= (uint8_t *) a
;
73 if (m_bytes
< sizeof(a
))
74 memset(p
+ m_bytes
, 0, sizeof(a
) - m_bytes
);
76 p
[m_bytes
- 1] &= ~m_clear
;
80 match_if_addr(union if_addr
*a1
, union if_addr
*a2
, int mask
)
82 union if_addr
*p1
, *p2
;
84 p1
= alloca(sizeof(*a1
));
85 p2
= alloca(sizeof(*a2
));
87 memcpy(p1
, a1
, sizeof(*a1
));
88 clear_if_addr(p1
, mask
);
89 memcpy(p2
, a2
, sizeof(*a2
));
90 clear_if_addr(p2
, mask
);
92 return !memcmp(p1
, p2
, sizeof(*p1
));
95 static int set_ip_source_policy(bool add
, bool v6
, unsigned int priority
,
96 const union if_addr
*addr
, uint8_t mask
, unsigned int table
,
97 struct interface
*in_iface
, const char *action
)
99 struct iprule rule
= {
100 .flags
= IPRULE_PRIORITY
,
105 rule
.flags
|= IPRULE_SRC
;
106 rule
.src_addr
= *addr
;
107 rule
.src_mask
= mask
;
111 rule
.flags
|= IPRULE_LOOKUP
;
117 rule
.flags
|= IPRULE_ACTION
;
118 system_resolve_iprule_action(action
, &rule
.action
);
121 if (in_iface
&& in_iface
->l3_dev
.dev
) {
122 rule
.flags
|= IPRULE_IN
;
123 strcpy(rule
.in_dev
, in_iface
->l3_dev
.dev
->ifname
);
126 rule
.flags
|= (v6
) ? IPRULE_INET6
: IPRULE_INET4
;
128 return (add
) ? system_add_iprule(&rule
) : system_del_iprule(&rule
);
131 static int set_ip_lo_policy(bool add
, bool v6
, struct interface
*iface
)
133 struct iprule rule
= {
134 .flags
= IPRULE_IN
| IPRULE_LOOKUP
| IPRULE_PRIORITY
,
135 .priority
= IPRULE_PRIORITY_NW
+ iface
->l3_dev
.dev
->ifindex
,
136 .lookup
= (v6
) ? iface
->ip6table
: iface
->ip4table
,
143 rule
.flags
|= (v6
) ? IPRULE_INET6
: IPRULE_INET4
;
145 return (add
) ? system_add_iprule(&rule
) : system_del_iprule(&rule
);
149 __find_ip_addr_target(struct interface_ip_settings
*ip
, union if_addr
*a
, bool v6
)
151 struct device_addr
*addr
;
153 vlist_for_each_element(&ip
->addr
, addr
, node
) {
157 if (v6
!= ((addr
->flags
& DEVADDR_FAMILY
) == DEVADDR_INET6
))
160 // Handle offlink addresses correctly
161 unsigned int mask
= addr
->mask
;
162 if ((addr
->flags
& DEVADDR_FAMILY
) == DEVADDR_INET6
&&
163 (addr
->flags
& DEVADDR_OFFLINK
))
166 if (!match_if_addr(&addr
->addr
, a
, mask
))
176 __find_ip_route_target(struct interface_ip_settings
*ip
, union if_addr
*a
,
177 bool v6
, struct device_route
**res
)
179 struct device_route
*route
;
181 vlist_for_each_element(&ip
->route
, route
, node
) {
185 if (v6
!= ((route
->flags
& DEVADDR_FAMILY
) == DEVADDR_INET6
))
188 if (!match_if_addr(&route
->addr
, a
, route
->mask
))
191 if (route
->flags
& DEVROUTE_TABLE
)
194 if (!*res
|| route
->mask
< (*res
)->mask
)
200 interface_ip_find_addr_target(struct interface
*iface
, union if_addr
*a
, bool v6
)
202 return __find_ip_addr_target(&iface
->proto_ip
, a
, v6
) ||
203 __find_ip_addr_target(&iface
->config_ip
, a
, v6
);
207 interface_ip_find_route_target(struct interface
*iface
, union if_addr
*a
,
208 bool v6
, struct device_route
**route
)
210 __find_ip_route_target(&iface
->proto_ip
, a
, v6
, route
);
211 __find_ip_route_target(&iface
->config_ip
, a
, v6
, route
);
215 interface_ip_add_target_route(union if_addr
*addr
, bool v6
, struct interface
*iface
)
217 struct device_route
*route
, *r_next
= NULL
;
218 bool defaultroute_target
= false;
219 int addrsize
= v6
? sizeof(addr
->in6
) : sizeof(addr
->in
);
221 route
= calloc(1, sizeof(*route
));
225 route
->flags
= v6
? DEVADDR_INET6
: DEVADDR_INET4
;
226 route
->mask
= v6
? 128 : 32;
227 if (memcmp(&route
->addr
, addr
, addrsize
) == 0)
228 defaultroute_target
= true;
230 memcpy(&route
->addr
, addr
, addrsize
);
233 /* look for locally addressable target first */
234 if (interface_ip_find_addr_target(iface
, addr
, v6
))
237 /* do not stop at the first route, let the lookup compare
238 * masks to find the best match */
239 interface_ip_find_route_target(iface
, addr
, v6
, &r_next
);
241 vlist_for_each_element(&interfaces
, iface
, node
) {
242 /* look for locally addressable target first */
243 if (interface_ip_find_addr_target(iface
, addr
, v6
))
246 /* do not stop at the first route, let the lookup compare
247 * masks to find the best match */
248 interface_ip_find_route_target(iface
, addr
, v6
, &r_next
);
257 iface
= r_next
->iface
;
258 memcpy(&route
->nexthop
, &r_next
->nexthop
, sizeof(route
->nexthop
));
259 route
->mtu
= r_next
->mtu
;
260 route
->metric
= r_next
->metric
;
261 route
->table
= r_next
->table
;
264 route
->iface
= iface
;
265 if (defaultroute_target
)
268 vlist_add(&iface
->host_routes
, &route
->node
, route
);
273 interface_ip_add_route(struct interface
*iface
, struct blob_attr
*attr
, bool v6
)
275 struct interface_ip_settings
*ip
;
276 struct blob_attr
*tb
[__ROUTE_MAX
], *cur
;
277 struct device_route
*route
;
278 int af
= v6
? AF_INET6
: AF_INET
;
279 bool is_proto_route
= !!iface
;
281 blobmsg_parse(route_attr
, __ROUTE_MAX
, tb
, blobmsg_data(attr
), blobmsg_data_len(attr
));
284 if ((cur
= tb
[ROUTE_INTERFACE
]) == NULL
)
287 iface
= vlist_find(&interfaces
, blobmsg_data(cur
), iface
, node
);
291 ip
= &iface
->config_ip
;
293 ip
= &iface
->proto_ip
;
296 route
= calloc(1, sizeof(*route
));
300 route
->flags
= v6
? DEVADDR_INET6
: DEVADDR_INET4
;
301 route
->mask
= v6
? 128 : 32;
302 if ((cur
= tb
[ROUTE_MASK
]) != NULL
) {
303 route
->mask
= parse_netmask_string(blobmsg_data(cur
), v6
);
304 if (route
->mask
> (v6
? 128 : 32))
308 if ((cur
= tb
[ROUTE_TARGET
]) != NULL
) {
309 if (!parse_ip_and_netmask(af
, blobmsg_data(cur
), &route
->addr
, &route
->mask
)) {
310 DPRINTF("Failed to parse route target: %s\n", (char *) blobmsg_data(cur
));
315 if ((cur
= tb
[ROUTE_GATEWAY
]) != NULL
) {
316 if (!inet_pton(af
, blobmsg_data(cur
), &route
->nexthop
)) {
317 DPRINTF("Failed to parse route gateway: %s\n", (char *) blobmsg_data(cur
));
322 if ((cur
= tb
[ROUTE_METRIC
]) != NULL
) {
323 route
->metric
= blobmsg_get_u32(cur
);
324 route
->flags
|= DEVROUTE_METRIC
;
327 if ((cur
= tb
[ROUTE_MTU
]) != NULL
) {
328 route
->mtu
= blobmsg_get_u32(cur
);
329 route
->flags
|= DEVROUTE_MTU
;
332 // Use source-based routing
333 if ((cur
= tb
[ROUTE_SOURCE
]) != NULL
) {
334 char *saveptr
, *source
= alloca(blobmsg_data_len(cur
));
335 memcpy(source
, blobmsg_data(cur
), blobmsg_data_len(cur
));
337 const char *addr
= strtok_r(source
, "/", &saveptr
);
338 const char *mask
= strtok_r(NULL
, "/", &saveptr
);
340 if (inet_pton(af
, addr
, &route
->source
) < 1) {
341 DPRINTF("Failed to parse route source: %s\n", addr
);
345 route
->sourcemask
= atoi(mask
);
348 if (is_proto_route
) {
349 route
->table
= (v6
) ? iface
->ip6table
: iface
->ip4table
;
350 route
->flags
|= DEVROUTE_SRCTABLE
;
353 if ((cur
= tb
[ROUTE_TABLE
]) != NULL
) {
354 if (!system_resolve_rt_table(blobmsg_data(cur
), &route
->table
)) {
355 DPRINTF("Failed to resolve routing table: %s\n", (char *) blobmsg_data(cur
));
360 route
->flags
|= DEVROUTE_TABLE
;
363 if ((cur
= tb
[ROUTE_VALID
]) != NULL
) {
364 int64_t valid
= blobmsg_get_u32(cur
);
365 int64_t valid_until
= valid
+ (int64_t)system_get_rtime();
366 if (valid_until
<= LONG_MAX
&& valid
!= 0xffffffffLL
) // Catch overflow
367 route
->valid_until
= valid_until
;
370 vlist_add(&ip
->route
, &route
->node
, route
);
378 addr_cmp(const void *k1
, const void *k2
, void *ptr
)
380 return memcmp(k1
, k2
, sizeof(struct device_addr
) -
381 offsetof(struct device_addr
, flags
));
385 route_cmp(const void *k1
, const void *k2
, void *ptr
)
387 const struct device_route
*r1
= k1
, *r2
= k2
;
389 if (r1
->mask
!= r2
->mask
)
390 return r2
->mask
- r1
->mask
;
392 if (r1
->metric
!= r2
->metric
)
393 return r1
->metric
- r2
->metric
;
395 if (r1
->flags
!= r2
->flags
)
396 return r2
->flags
- r1
->flags
;
398 if (r1
->sourcemask
!= r2
->sourcemask
)
399 return r1
->sourcemask
- r2
->sourcemask
;
401 int maskcmp
= memcmp(&r1
->source
, &r2
->source
, sizeof(r1
->source
));
405 return memcmp(&r1
->addr
, &r2
->addr
, sizeof(r1
->addr
));
409 prefix_cmp(const void *k1
, const void *k2
, void *ptr
)
411 return memcmp(k1
, k2
, offsetof(struct device_prefix
, pclass
) -
412 offsetof(struct device_prefix
, addr
));
416 interface_handle_subnet_route(struct interface
*iface
, struct device_addr
*addr
, bool add
)
418 struct device
*dev
= iface
->l3_dev
.dev
;
419 struct device_route route
;
421 memset(&route
, 0, sizeof(route
));
423 route
.flags
= addr
->flags
;
424 route
.mask
= addr
->mask
;
425 memcpy(&route
.addr
, &addr
->addr
, sizeof(route
.addr
));
426 clear_if_addr(&route
.addr
, route
.mask
);
429 route
.flags
|= DEVADDR_KERNEL
;
430 system_del_route(dev
, &route
);
432 if (!(addr
->flags
& DEVADDR_OFFLINK
)) {
433 route
.flags
&= ~DEVADDR_KERNEL
;
434 route
.metric
= iface
->metric
;
435 system_add_route(dev
, &route
);
438 if (!(addr
->flags
& DEVADDR_OFFLINK
))
439 system_del_route(dev
, &route
);
444 interface_update_proto_addr(struct vlist_tree
*tree
,
445 struct vlist_node
*node_new
,
446 struct vlist_node
*node_old
)
448 struct interface_ip_settings
*ip
;
449 struct interface
*iface
;
451 struct device_addr
*a_new
= NULL
, *a_old
= NULL
;
452 bool replace
= false;
456 ip
= container_of(tree
, struct interface_ip_settings
, addr
);
458 dev
= iface
->l3_dev
.dev
;
461 a_new
= container_of(node_new
, struct device_addr
, node
);
463 if ((a_new
->flags
& DEVADDR_FAMILY
) == DEVADDR_INET4
&&
467 uint32_t *a
= (uint32_t *) &a_new
->addr
;
469 mask
>>= a_new
->mask
;
470 a_new
->broadcast
= *a
| htonl(mask
);
475 a_old
= container_of(node_old
, struct device_addr
, node
);
477 if (a_new
&& a_old
) {
480 if (a_old
->flags
!= a_new
->flags
)
483 if (a_old
->valid_until
!= a_new
->valid_until
||
484 a_old
->preferred_until
!= a_new
->preferred_until
)
487 if ((a_new
->flags
& DEVADDR_FAMILY
) == DEVADDR_INET4
&&
488 a_new
->broadcast
!= a_old
->broadcast
)
493 if (!(a_old
->flags
& DEVADDR_EXTERNAL
) && a_old
->enabled
&& !keep
) {
494 interface_handle_subnet_route(iface
, a_old
, false);
496 if ((a_old
->flags
& DEVADDR_FAMILY
) == DEVADDR_INET6
)
499 //This is needed for source routing to work correctly. If a device
500 //has two connections to a network using the same subnet, adding
501 //only the network-rule will cause packets to be routed through the
502 //first matching network (source IP matches both masks).
503 set_ip_source_policy(false, v6
, IPRULE_PRIORITY_ADDR
, &a_old
->addr
,
504 (v6
) ? 128 : 32, (v6
) ? iface
->ip6table
: iface
->ip4table
,
506 set_ip_source_policy(false, v6
, IPRULE_PRIORITY_NW
, &a_old
->addr
,
507 a_old
->mask
, (v6
) ? iface
->ip6table
: iface
->ip4table
, NULL
, NULL
);
509 system_del_address(dev
, a_old
);
516 a_new
->enabled
= true;
517 if (!(a_new
->flags
& DEVADDR_EXTERNAL
) && (!keep
|| replace
)) {
518 system_add_address(dev
, a_new
);
521 if ((a_new
->flags
& DEVADDR_FAMILY
) == DEVADDR_INET6
)
524 set_ip_source_policy(true, v6
, IPRULE_PRIORITY_ADDR
, &a_new
->addr
,
525 (v6
) ? 128 : 32, (v6
) ? iface
->ip6table
: iface
->ip4table
,
527 set_ip_source_policy(true, v6
, IPRULE_PRIORITY_NW
, &a_new
->addr
,
528 a_new
->mask
, (v6
) ? iface
->ip6table
: iface
->ip4table
,
532 if ((a_new
->flags
& DEVADDR_OFFLINK
) || iface
->metric
)
533 interface_handle_subnet_route(iface
, a_new
, true);
539 enable_route(struct interface_ip_settings
*ip
, struct device_route
*route
)
541 if (ip
->no_defaultroute
&& !route
->mask
)
548 interface_update_proto_route(struct vlist_tree
*tree
,
549 struct vlist_node
*node_new
,
550 struct vlist_node
*node_old
)
552 struct interface_ip_settings
*ip
;
553 struct interface
*iface
;
555 struct device_route
*route_old
, *route_new
;
558 ip
= container_of(tree
, struct interface_ip_settings
, route
);
560 dev
= iface
->l3_dev
.dev
;
562 route_old
= container_of(node_old
, struct device_route
, node
);
563 route_new
= container_of(node_new
, struct device_route
, node
);
565 if (node_old
&& node_new
)
566 keep
= !memcmp(&route_old
->nexthop
, &route_new
->nexthop
, sizeof(route_old
->nexthop
)) &&
567 (route_old
->table
== route_new
->table
);
570 if (!(route_old
->flags
& DEVADDR_EXTERNAL
) && route_old
->enabled
&& !keep
)
571 system_del_route(dev
, route_old
);
577 bool _enabled
= enable_route(ip
, route_new
);
579 if (!(route_new
->flags
& DEVROUTE_METRIC
))
580 route_new
->metric
= iface
->metric
;
582 if (!(route_new
->flags
& DEVADDR_EXTERNAL
) && !keep
&& _enabled
)
583 system_add_route(dev
, route_new
);
585 route_new
->iface
= iface
;
586 route_new
->enabled
= _enabled
;
591 interface_update_host_route(struct vlist_tree
*tree
,
592 struct vlist_node
*node_new
,
593 struct vlist_node
*node_old
)
595 struct interface
*iface
;
597 struct device_route
*route_old
, *route_new
;
599 iface
= container_of(tree
, struct interface
, host_routes
);
600 dev
= iface
->l3_dev
.dev
;
602 route_old
= container_of(node_old
, struct device_route
, node
);
603 route_new
= container_of(node_new
, struct device_route
, node
);
606 system_del_route(dev
, route_old
);
611 system_add_route(dev
, route_new
);
616 interface_set_prefix_address(struct device_prefix_assignment
*assignment
,
617 const struct device_prefix
*prefix
, struct interface
*iface
, bool add
)
619 const struct interface
*uplink
= prefix
->iface
;
620 if (!iface
->l3_dev
.dev
)
623 struct device
*l3_downlink
= iface
->l3_dev
.dev
;
625 struct device_addr addr
;
626 memset(&addr
, 0, sizeof(addr
));
627 addr
.addr
.in6
= prefix
->addr
;
628 addr
.addr
.in6
.s6_addr32
[1] |= htonl(assignment
->assigned
);
629 addr
.addr
.in6
.s6_addr
[15] += 1;
630 addr
.mask
= assignment
->length
;
631 addr
.flags
= DEVADDR_INET6
;
632 addr
.preferred_until
= prefix
->preferred_until
;
633 addr
.valid_until
= prefix
->valid_until
;
635 if (!add
&& assignment
->enabled
) {
636 time_t now
= system_get_rtime();
637 addr
.preferred_until
= now
;
638 if (!addr
.valid_until
|| addr
.valid_until
- now
> 7200)
639 addr
.valid_until
= now
+ 7200;
640 system_add_address(l3_downlink
, &addr
);
642 set_ip_source_policy(false, true, IPRULE_PRIORITY_NW
, &addr
.addr
,
643 addr
.mask
, prefix
->iface
->ip6table
, iface
, NULL
);
645 set_ip_source_policy(false, true, IPRULE_PRIORITY_REJECT
, &addr
.addr
,
646 addr
.mask
, 0, iface
, "unreachable");
649 assignment
->enabled
= false;
650 } else if (add
&& (iface
->state
== IFS_UP
|| iface
->state
== IFS_SETUP
)) {
651 system_add_address(l3_downlink
, &addr
);
652 if (prefix
->iface
&& !assignment
->enabled
) {
653 set_ip_source_policy(true, true, IPRULE_PRIORITY_REJECT
, &addr
.addr
,
654 addr
.mask
, 0, iface
, "unreachable");
656 set_ip_source_policy(true, true, IPRULE_PRIORITY_NW
, &addr
.addr
,
657 addr
.mask
, prefix
->iface
->ip6table
, iface
, NULL
);
659 if (uplink
&& uplink
->l3_dev
.dev
) {
660 int mtu
= system_update_ipv6_mtu(
661 uplink
->l3_dev
.dev
, 0);
663 system_update_ipv6_mtu(l3_downlink
, mtu
);
665 assignment
->enabled
= true;
669 static bool interface_prefix_assign(struct list_head
*list
,
670 struct device_prefix_assignment
*assign
)
672 int32_t current
= 0, asize
= (1 << (64 - assign
->length
)) - 1;
673 struct device_prefix_assignment
*c
;
674 list_for_each_entry(c
, list
, head
) {
675 if (assign
->assigned
!= -1) {
676 if (assign
->assigned
>= current
&& assign
->assigned
+ asize
< c
->assigned
) {
677 list_add_tail(&assign
->head
, &c
->head
);
680 } else if (assign
->assigned
== -1) {
681 current
= (current
+ asize
) & (~asize
);
682 if (current
+ asize
< c
->assigned
) {
683 assign
->assigned
= current
;
684 list_add_tail(&assign
->head
, &c
->head
);
688 current
= (c
->assigned
+ (1 << (64 - c
->length
)));
693 static void interface_update_prefix_assignments(struct device_prefix
*prefix
, bool setup
)
695 struct device_prefix_assignment
*c
;
696 struct interface
*iface
;
698 // Delete all assignments
699 while (!list_empty(&prefix
->assignments
)) {
700 c
= list_first_entry(&prefix
->assignments
,
701 struct device_prefix_assignment
, head
);
702 if ((iface
= vlist_find(&interfaces
, c
->name
, iface
, node
)))
703 interface_set_prefix_address(c
, prefix
, iface
, false);
711 // End-of-assignment sentinel
712 c
= malloc(sizeof(*c
) + 1);
713 c
->assigned
= 1 << (64 - prefix
->length
);
716 list_add(&c
->head
, &prefix
->assignments
);
719 if (prefix
->excl_length
> 0) {
720 const char name
[] = "!excluded";
721 c
= malloc(sizeof(*c
) + sizeof(name
));
722 c
->assigned
= ntohl(prefix
->excl_addr
.s6_addr32
[1]) &
723 ((1 << (64 - prefix
->length
)) - 1);
724 c
->length
= prefix
->excl_length
;
725 memcpy(c
->name
, name
, sizeof(name
));
726 list_add(&c
->head
, &prefix
->assignments
);
729 bool assigned_any
= false;
730 struct list_head assign_later
= LIST_HEAD_INIT(assign_later
);
731 vlist_for_each_element(&interfaces
, iface
, node
) {
732 if (iface
->assignment_length
< 48 ||
733 iface
->assignment_length
> 64)
736 // Test whether there is a matching class
737 if (!list_empty(&iface
->assignment_classes
)) {
740 struct interface_assignment_class
*c
;
741 list_for_each_entry(c
, &iface
->assignment_classes
, head
) {
742 if (!strcmp(c
->name
, prefix
->pclass
)) {
752 size_t namelen
= strlen(iface
->name
) + 1;
753 c
= malloc(sizeof(*c
) + namelen
);
754 c
->length
= iface
->assignment_length
;
755 c
->assigned
= iface
->assignment_hint
;
757 memcpy(c
->name
, iface
->name
, namelen
);
759 // First process all custom assignments, put all others in later-list
760 if (c
->assigned
== -1 || !interface_prefix_assign(&prefix
->assignments
, c
)) {
761 if (c
->assigned
!= -1) {
763 netifd_log_message(L_WARNING
, "Failed to assign requested subprefix "
764 "of size %hhu for %s, trying other\n", c
->length
, c
->name
);
767 struct list_head
*next
= &assign_later
;
768 struct device_prefix_assignment
*n
;
769 list_for_each_entry(n
, &assign_later
, head
) {
770 if (n
->length
< c
->length
) {
775 list_add_tail(&c
->head
, next
);
778 if (c
->assigned
!= -1)
782 // Then try to assign all other + failed custom assignments
783 while (!list_empty(&assign_later
)) {
784 c
= list_first_entry(&assign_later
, struct device_prefix_assignment
, head
);
787 bool assigned
= false;
789 assigned
= interface_prefix_assign(&prefix
->assignments
, c
);
790 } while (!assigned
&& ++c
->length
<= 64);
793 netifd_log_message(L_WARNING
, "Failed to assign subprefix "
794 "of size %hhu for %s\n", c
->length
, c
->name
);
801 list_for_each_entry(c
, &prefix
->assignments
, head
)
802 if ((iface
= vlist_find(&interfaces
, c
->name
, iface
, node
)))
803 interface_set_prefix_address(c
, prefix
, iface
, true);
806 netifd_log_message(L_WARNING
, "You have delegated IPv6-prefixes but haven't assigned them "
807 "to any interface. Did you forget to set option ip6assign on your lan-interfaces?");
811 void interface_refresh_assignments(bool hint
)
813 static bool refresh
= false;
814 if (!hint
&& refresh
) {
815 struct device_prefix
*p
;
816 list_for_each_entry(p
, &prefixes
, head
)
817 interface_update_prefix_assignments(p
, true);
824 interface_update_prefix(struct vlist_tree
*tree
,
825 struct vlist_node
*node_new
,
826 struct vlist_node
*node_old
)
828 struct device_prefix
*prefix_old
, *prefix_new
;
829 prefix_old
= container_of(node_old
, struct device_prefix
, node
);
830 prefix_new
= container_of(node_new
, struct device_prefix
, node
);
832 struct device_route route
;
833 memset(&route
, 0, sizeof(route
));
834 route
.flags
= DEVADDR_INET6
;
835 route
.metric
= INT32_MAX
;
836 route
.mask
= (node_new
) ? prefix_new
->length
: prefix_old
->length
;
837 route
.addr
.in6
= (node_new
) ? prefix_new
->addr
: prefix_old
->addr
;
840 struct device_prefix_assignment
*c
;
841 struct interface
*iface
;
843 if (node_old
&& node_new
) {
844 // Move assignments and refresh addresses to update valid times
845 list_splice(&prefix_old
->assignments
, &prefix_new
->assignments
);
847 list_for_each_entry(c
, &prefix_new
->assignments
, head
)
848 if ((iface
= vlist_find(&interfaces
, c
->name
, iface
, node
)))
849 interface_set_prefix_address(c
, prefix_new
, iface
, true);
850 } else if (node_new
) {
851 // Set null-route to avoid routing loops
852 system_add_route(NULL
, &route
);
854 if (!prefix_new
->iface
|| !prefix_new
->iface
->proto_ip
.no_delegation
)
855 interface_update_prefix_assignments(prefix_new
, true);
856 } else if (node_old
) {
858 interface_update_prefix_assignments(prefix_old
, false);
859 system_del_route(NULL
, &route
);
863 if (prefix_old
->head
.next
)
864 list_del(&prefix_old
->head
);
868 if (node_new
&& (!prefix_new
->iface
|| !prefix_new
->iface
->proto_ip
.no_delegation
))
869 list_add(&prefix_new
->head
, &prefixes
);
873 struct device_prefix
*
874 interface_ip_add_device_prefix(struct interface
*iface
, struct in6_addr
*addr
,
875 uint8_t length
, time_t valid_until
, time_t preferred_until
,
876 struct in6_addr
*excl_addr
, uint8_t excl_length
, const char *pclass
)
879 pclass
= (iface
) ? iface
->name
: "local";
881 struct device_prefix
*prefix
= calloc(1, sizeof(*prefix
) + strlen(pclass
) + 1);
882 prefix
->length
= length
;
883 prefix
->addr
= *addr
;
884 prefix
->preferred_until
= preferred_until
;
885 prefix
->valid_until
= valid_until
;
886 prefix
->iface
= iface
;
887 INIT_LIST_HEAD(&prefix
->assignments
);
890 prefix
->excl_addr
= *excl_addr
;
891 prefix
->excl_length
= excl_length
;
894 strcpy(prefix
->pclass
, pclass
);
897 vlist_add(&iface
->proto_ip
.prefix
, &prefix
->node
, &prefix
->addr
);
899 interface_update_prefix(NULL
, &prefix
->node
, NULL
);
905 interface_ip_set_ula_prefix(const char *prefix
)
907 char buf
[INET6_ADDRSTRLEN
+ 4] = {0}, *saveptr
;
909 strncpy(buf
, prefix
, sizeof(buf
) - 1);
910 char *prefixaddr
= strtok_r(buf
, "/", &saveptr
);
912 struct in6_addr addr
;
913 if (!prefixaddr
|| inet_pton(AF_INET6
, prefixaddr
, &addr
) < 1) {
915 interface_update_prefix(NULL
, NULL
, &ula_prefix
->node
);
922 char *prefixlen
= strtok_r(NULL
, ",", &saveptr
);
923 if (!prefixlen
|| (length
= atoi(prefixlen
)) < 1 || length
> 64)
926 if (!ula_prefix
|| !IN6_ARE_ADDR_EQUAL(&addr
, &ula_prefix
->addr
) ||
927 ula_prefix
->length
!= length
) {
929 interface_update_prefix(NULL
, NULL
, &ula_prefix
->node
);
931 ula_prefix
= interface_ip_add_device_prefix(NULL
, &addr
, length
,
932 0, 0, NULL
, 0, NULL
);
937 interface_add_dns_server(struct interface_ip_settings
*ip
, const char *str
)
939 struct dns_server
*s
;
941 s
= calloc(1, sizeof(*s
));
946 if (inet_pton(s
->af
, str
, &s
->addr
.in
))
950 if (inet_pton(s
->af
, str
, &s
->addr
.in
))
957 D(INTERFACE
, "Add IPv%c DNS server: %s\n",
958 s
->af
== AF_INET6
? '6' : '4', str
);
959 vlist_simple_add(&ip
->dns_servers
, &s
->node
);
963 interface_add_dns_server_list(struct interface_ip_settings
*ip
, struct blob_attr
*list
)
965 struct blob_attr
*cur
;
968 blobmsg_for_each_attr(cur
, list
, rem
) {
969 if (blobmsg_type(cur
) != BLOBMSG_TYPE_STRING
)
972 if (!blobmsg_check_attr(cur
, NULL
))
975 interface_add_dns_server(ip
, blobmsg_data(cur
));
980 interface_add_dns_search_domain(struct interface_ip_settings
*ip
, const char *str
)
982 struct dns_search_domain
*s
;
983 int len
= strlen(str
);
985 s
= calloc(1, sizeof(*s
) + len
+ 1);
989 D(INTERFACE
, "Add DNS search domain: %s\n", str
);
990 memcpy(s
->name
, str
, len
);
991 vlist_simple_add(&ip
->dns_search
, &s
->node
);
995 interface_add_dns_search_list(struct interface_ip_settings
*ip
, struct blob_attr
*list
)
997 struct blob_attr
*cur
;
1000 blobmsg_for_each_attr(cur
, list
, rem
) {
1001 if (blobmsg_type(cur
) != BLOBMSG_TYPE_STRING
)
1004 if (!blobmsg_check_attr(cur
, NULL
))
1007 interface_add_dns_search_domain(ip
, blobmsg_data(cur
));
1012 write_resolv_conf_entries(FILE *f
, struct interface_ip_settings
*ip
)
1014 struct dns_server
*s
;
1015 struct dns_search_domain
*d
;
1017 char buf
[INET6_ADDRSTRLEN
];
1019 vlist_simple_for_each_element(&ip
->dns_servers
, s
, node
) {
1020 str
= inet_ntop(s
->af
, &s
->addr
, buf
, sizeof(buf
));
1024 fprintf(f
, "nameserver %s\n", str
);
1027 vlist_simple_for_each_element(&ip
->dns_search
, d
, node
) {
1028 fprintf(f
, "search %s\n", d
->name
);
1033 interface_write_resolv_conf(void)
1035 struct interface
*iface
;
1036 char *path
= alloca(strlen(resolv_conf
) + 5);
1038 uint32_t crcold
, crcnew
;
1040 sprintf(path
, "%s.tmp", resolv_conf
);
1042 f
= fopen(path
, "w+");
1044 D(INTERFACE
, "Failed to open %s for writing\n", path
);
1048 vlist_for_each_element(&interfaces
, iface
, node
) {
1049 if (iface
->state
!= IFS_UP
)
1052 if (vlist_simple_empty(&iface
->proto_ip
.dns_search
) &&
1053 vlist_simple_empty(&iface
->proto_ip
.dns_servers
) &&
1054 vlist_simple_empty(&iface
->config_ip
.dns_search
) &&
1055 vlist_simple_empty(&iface
->config_ip
.dns_servers
))
1058 fprintf(f
, "# Interface %s\n", iface
->name
);
1059 write_resolv_conf_entries(f
, &iface
->config_ip
);
1060 if (!iface
->proto_ip
.no_dns
)
1061 write_resolv_conf_entries(f
, &iface
->proto_ip
);
1065 crcnew
= crc32_file(f
);
1068 crcold
= crcnew
+ 1;
1069 f
= fopen(resolv_conf
, "r");
1071 crcold
= crc32_file(f
);
1075 if (crcold
== crcnew
) {
1077 } else if (rename(path
, resolv_conf
) < 0) {
1078 D(INTERFACE
, "Failed to replace %s\n", resolv_conf
);
1083 void interface_ip_set_enabled(struct interface_ip_settings
*ip
, bool enabled
)
1085 struct device_addr
*addr
;
1086 struct device_route
*route
;
1089 ip
->enabled
= enabled
;
1090 dev
= ip
->iface
->l3_dev
.dev
;
1094 vlist_for_each_element(&ip
->addr
, addr
, node
) {
1095 if (addr
->enabled
== enabled
)
1099 system_add_address(dev
, addr
);
1101 system_del_address(dev
, addr
);
1102 addr
->enabled
= enabled
;
1105 vlist_for_each_element(&ip
->route
, route
, node
) {
1106 bool _enabled
= enabled
;
1108 if (!enable_route(ip
, route
))
1111 if (route
->enabled
== _enabled
)
1115 if (!(route
->flags
& DEVROUTE_METRIC
))
1116 route
->metric
= ip
->iface
->metric
;
1118 system_add_route(dev
, route
);
1120 system_del_route(dev
, route
);
1121 route
->enabled
= _enabled
;
1124 struct device_prefix
*c
;
1125 struct device_prefix_assignment
*a
;
1126 list_for_each_entry(c
, &prefixes
, head
)
1127 list_for_each_entry(a
, &c
->assignments
, head
)
1128 if (!strcmp(a
->name
, ip
->iface
->name
))
1129 interface_set_prefix_address(a
, c
, ip
->iface
, enabled
);
1131 if (ip
->iface
&& ip
->iface
->l3_dev
.dev
) {
1132 set_ip_lo_policy(enabled
, true, ip
->iface
);
1133 set_ip_lo_policy(enabled
, false, ip
->iface
);
1135 set_ip_source_policy(enabled
, true, IPRULE_PRIORITY_REJECT
+ ip
->iface
->l3_dev
.dev
->ifindex
,
1136 NULL
, 0, 0, ip
->iface
, "failed_policy");
1141 interface_ip_update_start(struct interface_ip_settings
*ip
)
1143 if (ip
!= &ip
->iface
->config_ip
) {
1144 vlist_simple_update(&ip
->dns_servers
);
1145 vlist_simple_update(&ip
->dns_search
);
1147 vlist_update(&ip
->route
);
1148 vlist_update(&ip
->addr
);
1149 vlist_update(&ip
->prefix
);
1153 interface_ip_update_complete(struct interface_ip_settings
*ip
)
1155 vlist_simple_flush(&ip
->dns_servers
);
1156 vlist_simple_flush(&ip
->dns_search
);
1157 vlist_flush(&ip
->route
);
1158 vlist_flush(&ip
->addr
);
1159 vlist_flush(&ip
->prefix
);
1160 interface_write_resolv_conf();
1164 interface_ip_flush(struct interface_ip_settings
*ip
)
1166 if (ip
== &ip
->iface
->proto_ip
)
1167 vlist_flush_all(&ip
->iface
->host_routes
);
1168 vlist_simple_flush_all(&ip
->dns_servers
);
1169 vlist_simple_flush_all(&ip
->dns_search
);
1170 vlist_flush_all(&ip
->route
);
1171 vlist_flush_all(&ip
->addr
);
1172 vlist_flush_all(&ip
->prefix
);
1176 __interface_ip_init(struct interface_ip_settings
*ip
, struct interface
*iface
)
1180 vlist_simple_init(&ip
->dns_search
, struct dns_search_domain
, node
);
1181 vlist_simple_init(&ip
->dns_servers
, struct dns_server
, node
);
1182 vlist_init(&ip
->route
, route_cmp
, interface_update_proto_route
);
1183 vlist_init(&ip
->addr
, addr_cmp
, interface_update_proto_addr
);
1184 vlist_init(&ip
->prefix
, prefix_cmp
, interface_update_prefix
);
1188 interface_ip_init(struct interface
*iface
)
1190 __interface_ip_init(&iface
->proto_ip
, iface
);
1191 __interface_ip_init(&iface
->config_ip
, iface
);
1192 vlist_init(&iface
->host_routes
, route_cmp
, interface_update_host_route
);
1197 interface_ip_valid_until_handler(struct uloop_timeout
*t
)
1199 time_t now
= system_get_rtime();
1200 struct interface
*iface
;
1201 vlist_for_each_element(&interfaces
, iface
, node
) {
1202 if (iface
->state
!= IFS_UP
)
1205 struct device_addr
*addr
, *addrp
;
1206 struct device_route
*route
, *routep
;
1207 struct device_prefix
*pref
, *prefp
;
1209 vlist_for_each_element_safe(&iface
->proto_ip
.addr
, addr
, node
, addrp
)
1210 if (addr
->valid_until
&& addr
->valid_until
< now
)
1211 vlist_delete(&iface
->proto_ip
.addr
, &addr
->node
);
1213 vlist_for_each_element_safe(&iface
->proto_ip
.route
, route
, node
, routep
)
1214 if (route
->valid_until
&& route
->valid_until
< now
)
1215 vlist_delete(&iface
->proto_ip
.route
, &route
->node
);
1217 vlist_for_each_element_safe(&iface
->proto_ip
.prefix
, pref
, node
, prefp
)
1218 if (pref
->valid_until
&& pref
->valid_until
< now
)
1219 vlist_delete(&iface
->proto_ip
.prefix
, &pref
->node
);
1223 uloop_timeout_set(t
, 1000);
1227 interface_ip_init_worker(void)
1229 valid_until_timeout
.cb
= interface_ip_valid_until_handler
;
1230 uloop_timeout_set(&valid_until_timeout
, 1000);