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.
21 #include <arpa/inet.h>
25 #include "interface.h"
26 #include "interface-ip.h"
44 static const struct blobmsg_policy route_attr
[__ROUTE_MAX
] = {
45 [ROUTE_INTERFACE
] = { .name
= "interface", .type
= BLOBMSG_TYPE_STRING
},
46 [ROUTE_TARGET
] = { .name
= "target", .type
= BLOBMSG_TYPE_STRING
},
47 [ROUTE_MASK
] = { .name
= "netmask", .type
= BLOBMSG_TYPE_STRING
},
48 [ROUTE_GATEWAY
] = { .name
= "gateway", .type
= BLOBMSG_TYPE_STRING
},
49 [ROUTE_METRIC
] = { .name
= "metric", .type
= BLOBMSG_TYPE_INT32
},
50 [ROUTE_MTU
] = { .name
= "mtu", .type
= BLOBMSG_TYPE_INT32
},
51 [ROUTE_TABLE
] = { .name
= "table", .type
= BLOBMSG_TYPE_STRING
},
52 [ROUTE_VALID
] = { .name
= "valid", .type
= BLOBMSG_TYPE_INT32
},
53 [ROUTE_SOURCE
] = { .name
= "source", .type
= BLOBMSG_TYPE_STRING
},
56 const struct uci_blob_param_list route_attr_list
= {
57 .n_params
= __ROUTE_MAX
,
62 struct list_head prefixes
= LIST_HEAD_INIT(prefixes
);
63 static struct list_head source_tables
= LIST_HEAD_INIT(source_tables
);
64 static struct device_prefix
*ula_prefix
= NULL
;
65 static struct uloop_timeout valid_until_timeout
;
69 clear_if_addr(union if_addr
*a
, int mask
)
71 int m_bytes
= (mask
+ 7) / 8;
72 uint8_t m_clear
= (1 << (m_bytes
* 8 - mask
)) - 1;
73 uint8_t *p
= (uint8_t *) a
;
75 if (m_bytes
< sizeof(a
))
76 memset(p
+ m_bytes
, 0, sizeof(a
) - m_bytes
);
78 p
[m_bytes
- 1] &= ~m_clear
;
82 match_if_addr(union if_addr
*a1
, union if_addr
*a2
, int mask
)
84 union if_addr
*p1
, *p2
;
86 p1
= alloca(sizeof(*a1
));
87 p2
= alloca(sizeof(*a2
));
89 memcpy(p1
, a1
, sizeof(*a1
));
90 clear_if_addr(p1
, mask
);
91 memcpy(p2
, a2
, sizeof(*a2
));
92 clear_if_addr(p2
, mask
);
94 return !memcmp(p1
, p2
, sizeof(*p1
));
97 static int set_ip_source_policy(bool add
, bool v6
, unsigned int priority
,
98 const union if_addr
*addr
, uint8_t mask
, unsigned int table
,
99 struct interface
*in_iface
, const char *action
)
101 struct iprule rule
= {
102 .flags
= IPRULE_PRIORITY
,
107 rule
.flags
|= IPRULE_SRC
;
108 rule
.src_addr
= *addr
;
109 rule
.src_mask
= mask
;
113 rule
.flags
|= IPRULE_LOOKUP
;
119 rule
.flags
|= IPRULE_ACTION
;
120 system_resolve_iprule_action(action
, &rule
.action
);
123 if (in_iface
&& in_iface
->l3_dev
.dev
) {
124 rule
.flags
|= IPRULE_IN
;
125 strcpy(rule
.in_dev
, in_iface
->l3_dev
.dev
->ifname
);
128 rule
.flags
|= (v6
) ? IPRULE_INET6
: IPRULE_INET4
;
130 return (add
) ? system_add_iprule(&rule
) : system_del_iprule(&rule
);
133 static int set_ip_lo_policy(bool add
, bool v6
, struct interface
*iface
)
135 struct iprule rule
= {
136 .flags
= IPRULE_IN
| IPRULE_LOOKUP
| IPRULE_PRIORITY
,
137 .priority
= IPRULE_PRIORITY_NW
+ iface
->l3_dev
.dev
->ifindex
,
138 .lookup
= (v6
) ? iface
->ip6table
: iface
->ip4table
,
145 rule
.flags
|= (v6
) ? IPRULE_INET6
: IPRULE_INET4
;
147 return (add
) ? system_add_iprule(&rule
) : system_del_iprule(&rule
);
151 __find_ip_addr_target(struct interface_ip_settings
*ip
, union if_addr
*a
, bool v6
)
153 struct device_addr
*addr
;
155 vlist_for_each_element(&ip
->addr
, addr
, node
) {
159 if (v6
!= ((addr
->flags
& DEVADDR_FAMILY
) == DEVADDR_INET6
))
162 // Handle offlink addresses correctly
163 unsigned int mask
= addr
->mask
;
164 if ((addr
->flags
& DEVADDR_FAMILY
) == DEVADDR_INET6
&&
165 (addr
->flags
& DEVADDR_OFFLINK
))
168 if (!match_if_addr(&addr
->addr
, a
, mask
))
178 __find_ip_route_target(struct interface_ip_settings
*ip
, union if_addr
*a
,
179 bool v6
, struct device_route
**res
)
181 struct device_route
*route
;
183 vlist_for_each_element(&ip
->route
, route
, node
) {
187 if (v6
!= ((route
->flags
& DEVADDR_FAMILY
) == DEVADDR_INET6
))
190 if (!match_if_addr(&route
->addr
, a
, route
->mask
))
193 if (route
->flags
& DEVROUTE_TABLE
)
196 if (!*res
|| route
->mask
< (*res
)->mask
)
201 static struct device_source_table
*
202 find_source_table(const struct device_route
*route
)
204 struct device_source_table key
= {
205 .v6
= (route
->flags
& DEVADDR_FAMILY
) == DEVADDR_INET6
,
206 .addr
= route
->source
,
207 .mask
= route
->sourcemask
209 struct device_source_table
*c
;
210 list_for_each_entry(c
, &source_tables
, head
)
211 if (!memcmp(&c
->v6
, &key
.v6
, sizeof(key
) -
212 offsetof(struct device_source_table
, v6
)))
218 get_source_table(const struct device_route
*route
)
220 if (route
->table
|| route
->sourcemask
== 0)
223 struct device_source_table
*tbl
= find_source_table(route
);
226 tbl
= calloc(1, sizeof(*tbl
));
227 tbl
->addr
= route
->source
;
228 tbl
->mask
= route
->sourcemask
;
229 tbl
->v6
= (route
->flags
& DEVADDR_FAMILY
) == DEVADDR_INET6
;
230 tbl
->table
= IPRULE_PRIORITY_SOURCE
| (((~tbl
->mask
) & 0x7f) << 20);
232 struct list_head
*before
= NULL
;
233 struct device_source_table
*c
;
234 list_for_each_entry(c
, &source_tables
, head
) {
235 if (c
->table
> tbl
->table
) {
238 } else if (c
->table
== tbl
->table
) {
244 before
= &source_tables
;
246 list_add_tail(&tbl
->head
, before
);
247 set_ip_source_policy(true, tbl
->v6
, tbl
->table
, &tbl
->addr
,
248 tbl
->mask
, tbl
->table
, NULL
, NULL
);
256 put_source_table(const struct device_route
*route
)
258 struct device_source_table
*tbl
= find_source_table(route
);
259 if (tbl
&& tbl
->table
== route
->table
&& --tbl
->refcount
== 0) {
260 set_ip_source_policy(false, tbl
->v6
, tbl
->table
, &tbl
->addr
,
261 tbl
->mask
, tbl
->table
, NULL
, NULL
);
262 list_del(&tbl
->head
);
268 interface_ip_find_addr_target(struct interface
*iface
, union if_addr
*a
, bool v6
)
270 return __find_ip_addr_target(&iface
->proto_ip
, a
, v6
) ||
271 __find_ip_addr_target(&iface
->config_ip
, a
, v6
);
275 interface_ip_find_route_target(struct interface
*iface
, union if_addr
*a
,
276 bool v6
, struct device_route
**route
)
278 __find_ip_route_target(&iface
->proto_ip
, a
, v6
, route
);
279 __find_ip_route_target(&iface
->config_ip
, a
, v6
, route
);
283 interface_ip_add_target_route(union if_addr
*addr
, bool v6
, struct interface
*iface
)
285 struct device_route
*route
, *r_next
= NULL
;
286 bool defaultroute_target
= false;
287 int addrsize
= v6
? sizeof(addr
->in6
) : sizeof(addr
->in
);
289 route
= calloc(1, sizeof(*route
));
293 route
->flags
= v6
? DEVADDR_INET6
: DEVADDR_INET4
;
294 route
->mask
= v6
? 128 : 32;
295 if (memcmp(&route
->addr
, addr
, addrsize
) == 0)
296 defaultroute_target
= true;
298 memcpy(&route
->addr
, addr
, addrsize
);
301 /* look for locally addressable target first */
302 if (interface_ip_find_addr_target(iface
, addr
, v6
))
305 /* do not stop at the first route, let the lookup compare
306 * masks to find the best match */
307 interface_ip_find_route_target(iface
, addr
, v6
, &r_next
);
309 vlist_for_each_element(&interfaces
, iface
, node
) {
310 /* look for locally addressable target first */
311 if (interface_ip_find_addr_target(iface
, addr
, v6
))
314 /* do not stop at the first route, let the lookup compare
315 * masks to find the best match */
316 interface_ip_find_route_target(iface
, addr
, v6
, &r_next
);
325 iface
= r_next
->iface
;
326 memcpy(&route
->nexthop
, &r_next
->nexthop
, sizeof(route
->nexthop
));
327 route
->mtu
= r_next
->mtu
;
328 route
->metric
= r_next
->metric
;
329 route
->table
= r_next
->table
;
332 route
->iface
= iface
;
333 if (defaultroute_target
)
336 vlist_add(&iface
->host_routes
, &route
->node
, route
);
341 interface_ip_add_route(struct interface
*iface
, struct blob_attr
*attr
, bool v6
)
343 struct interface_ip_settings
*ip
;
344 struct blob_attr
*tb
[__ROUTE_MAX
], *cur
;
345 struct device_route
*route
;
346 int af
= v6
? AF_INET6
: AF_INET
;
347 bool is_proto_route
= !!iface
;
349 blobmsg_parse(route_attr
, __ROUTE_MAX
, tb
, blobmsg_data(attr
), blobmsg_data_len(attr
));
352 if ((cur
= tb
[ROUTE_INTERFACE
]) == NULL
)
355 iface
= vlist_find(&interfaces
, blobmsg_data(cur
), iface
, node
);
359 ip
= &iface
->config_ip
;
361 ip
= &iface
->proto_ip
;
364 route
= calloc(1, sizeof(*route
));
368 route
->flags
= v6
? DEVADDR_INET6
: DEVADDR_INET4
;
369 route
->mask
= v6
? 128 : 32;
370 if ((cur
= tb
[ROUTE_MASK
]) != NULL
) {
371 route
->mask
= parse_netmask_string(blobmsg_data(cur
), v6
);
372 if (route
->mask
> (v6
? 128 : 32))
376 if ((cur
= tb
[ROUTE_TARGET
]) != NULL
) {
377 if (!parse_ip_and_netmask(af
, blobmsg_data(cur
), &route
->addr
, &route
->mask
)) {
378 DPRINTF("Failed to parse route target: %s\n", (char *) blobmsg_data(cur
));
383 if ((cur
= tb
[ROUTE_GATEWAY
]) != NULL
) {
384 if (!inet_pton(af
, blobmsg_data(cur
), &route
->nexthop
)) {
385 DPRINTF("Failed to parse route gateway: %s\n", (char *) blobmsg_data(cur
));
390 if ((cur
= tb
[ROUTE_METRIC
]) != NULL
) {
391 route
->metric
= blobmsg_get_u32(cur
);
392 route
->flags
|= DEVROUTE_METRIC
;
395 if ((cur
= tb
[ROUTE_MTU
]) != NULL
) {
396 route
->mtu
= blobmsg_get_u32(cur
);
397 route
->flags
|= DEVROUTE_MTU
;
400 // Use source-based routing
401 if ((cur
= tb
[ROUTE_SOURCE
]) != NULL
) {
402 char *saveptr
, *source
= alloca(blobmsg_data_len(cur
));
403 memcpy(source
, blobmsg_data(cur
), blobmsg_data_len(cur
));
405 const char *addr
= strtok_r(source
, "/", &saveptr
);
406 const char *mask
= strtok_r(NULL
, "/", &saveptr
);
408 if (inet_pton(af
, addr
, &route
->source
) < 1) {
409 DPRINTF("Failed to parse route source: %s\n", addr
);
413 route
->sourcemask
= atoi(mask
);
414 } else if (is_proto_route
) {
415 route
->table
= (v6
) ? iface
->ip6table
: iface
->ip4table
;
416 route
->flags
|= DEVROUTE_SRCTABLE
;
419 if ((cur
= tb
[ROUTE_TABLE
]) != NULL
) {
420 if (!system_resolve_rt_table(blobmsg_data(cur
), &route
->table
)) {
421 DPRINTF("Failed to resolve routing table: %s\n", (char *) blobmsg_data(cur
));
426 route
->flags
|= DEVROUTE_TABLE
;
429 if ((cur
= tb
[ROUTE_VALID
]) != NULL
) {
430 int64_t valid
= blobmsg_get_u32(cur
);
431 int64_t valid_until
= valid
+ (int64_t)system_get_rtime();
432 if (valid_until
<= LONG_MAX
&& valid
!= 0xffffffffLL
) // Catch overflow
433 route
->valid_until
= valid_until
;
436 if (route
->sourcemask
) {
437 route
->table
= get_source_table(route
);
438 route
->flags
|= DEVROUTE_SRCTABLE
;
441 vlist_add(&ip
->route
, &route
->node
, route
);
449 addr_cmp(const void *k1
, const void *k2
, void *ptr
)
451 return memcmp(k1
, k2
, sizeof(struct device_addr
) -
452 offsetof(struct device_addr
, flags
));
456 route_cmp(const void *k1
, const void *k2
, void *ptr
)
458 const struct device_route
*r1
= k1
, *r2
= k2
;
460 if (r1
->mask
!= r2
->mask
)
461 return r2
->mask
- r1
->mask
;
463 if (r1
->metric
!= r2
->metric
)
464 return r1
->metric
- r2
->metric
;
466 if (r1
->flags
!= r2
->flags
)
467 return r2
->flags
- r1
->flags
;
469 if (r1
->sourcemask
!= r2
->sourcemask
)
470 return r1
->sourcemask
- r2
->sourcemask
;
472 int maskcmp
= memcmp(&r1
->source
, &r2
->source
, sizeof(r1
->source
));
476 return memcmp(&r1
->addr
, &r2
->addr
, sizeof(r1
->addr
));
480 prefix_cmp(const void *k1
, const void *k2
, void *ptr
)
482 return memcmp(k1
, k2
, offsetof(struct device_prefix
, pclass
) -
483 offsetof(struct device_prefix
, addr
));
487 interface_handle_subnet_route(struct interface
*iface
, struct device_addr
*addr
, bool add
)
489 struct device
*dev
= iface
->l3_dev
.dev
;
490 struct device_route route
;
492 memset(&route
, 0, sizeof(route
));
494 route
.flags
= addr
->flags
;
495 route
.mask
= addr
->mask
;
496 memcpy(&route
.addr
, &addr
->addr
, sizeof(route
.addr
));
497 clear_if_addr(&route
.addr
, route
.mask
);
500 route
.flags
|= DEVADDR_KERNEL
;
501 system_del_route(dev
, &route
);
503 if (!(addr
->flags
& DEVADDR_OFFLINK
)) {
504 route
.flags
&= ~DEVADDR_KERNEL
;
505 route
.metric
= iface
->metric
;
506 system_add_route(dev
, &route
);
509 if (!(addr
->flags
& DEVADDR_OFFLINK
))
510 system_del_route(dev
, &route
);
515 interface_update_proto_addr(struct vlist_tree
*tree
,
516 struct vlist_node
*node_new
,
517 struct vlist_node
*node_old
)
519 struct interface_ip_settings
*ip
;
520 struct interface
*iface
;
522 struct device_addr
*a_new
= NULL
, *a_old
= NULL
;
523 bool replace
= false;
527 ip
= container_of(tree
, struct interface_ip_settings
, addr
);
529 dev
= iface
->l3_dev
.dev
;
532 a_new
= container_of(node_new
, struct device_addr
, node
);
534 if ((a_new
->flags
& DEVADDR_FAMILY
) == DEVADDR_INET4
&&
538 uint32_t *a
= (uint32_t *) &a_new
->addr
;
540 mask
>>= a_new
->mask
;
541 a_new
->broadcast
= *a
| htonl(mask
);
546 a_old
= container_of(node_old
, struct device_addr
, node
);
548 if (a_new
&& a_old
) {
551 if (a_old
->flags
!= a_new
->flags
)
554 if (a_old
->valid_until
!= a_new
->valid_until
||
555 a_old
->preferred_until
!= a_new
->preferred_until
)
558 if ((a_new
->flags
& DEVADDR_FAMILY
) == DEVADDR_INET4
&&
559 a_new
->broadcast
!= a_old
->broadcast
)
564 if (!(a_old
->flags
& DEVADDR_EXTERNAL
) && a_old
->enabled
&& !keep
) {
565 interface_handle_subnet_route(iface
, a_old
, false);
567 if ((a_old
->flags
& DEVADDR_FAMILY
) == DEVADDR_INET6
)
570 //This is needed for source routing to work correctly. If a device
571 //has two connections to a network using the same subnet, adding
572 //only the network-rule will cause packets to be routed through the
573 //first matching network (source IP matches both masks).
574 set_ip_source_policy(false, v6
, IPRULE_PRIORITY_ADDR
, &a_old
->addr
,
575 (v6
) ? 128 : 32, (v6
) ? iface
->ip6table
: iface
->ip4table
,
577 set_ip_source_policy(false, v6
, IPRULE_PRIORITY_NW
, &a_old
->addr
,
578 a_old
->mask
, (v6
) ? iface
->ip6table
: iface
->ip4table
, NULL
, NULL
);
580 system_del_address(dev
, a_old
);
586 a_new
->enabled
= true;
587 if (!(a_new
->flags
& DEVADDR_EXTERNAL
) && (!keep
|| replace
)) {
588 system_add_address(dev
, a_new
);
591 if ((a_new
->flags
& DEVADDR_FAMILY
) == DEVADDR_INET6
)
594 set_ip_source_policy(true, v6
, IPRULE_PRIORITY_ADDR
, &a_new
->addr
,
595 (v6
) ? 128 : 32, (v6
) ? iface
->ip6table
: iface
->ip4table
,
597 set_ip_source_policy(true, v6
, IPRULE_PRIORITY_NW
, &a_new
->addr
,
598 a_new
->mask
, (v6
) ? iface
->ip6table
: iface
->ip4table
,
602 if ((a_new
->flags
& DEVADDR_OFFLINK
) || iface
->metric
)
603 interface_handle_subnet_route(iface
, a_new
, true);
609 enable_route(struct interface_ip_settings
*ip
, struct device_route
*route
)
611 if (ip
->no_defaultroute
&& !route
->mask
)
618 interface_update_proto_route(struct vlist_tree
*tree
,
619 struct vlist_node
*node_new
,
620 struct vlist_node
*node_old
)
622 struct interface_ip_settings
*ip
;
623 struct interface
*iface
;
625 struct device_route
*route_old
, *route_new
;
628 ip
= container_of(tree
, struct interface_ip_settings
, route
);
630 dev
= iface
->l3_dev
.dev
;
632 route_old
= container_of(node_old
, struct device_route
, node
);
633 route_new
= container_of(node_new
, struct device_route
, node
);
635 if (node_old
&& node_new
)
636 keep
= !memcmp(&route_old
->nexthop
, &route_new
->nexthop
, sizeof(route_old
->nexthop
));
639 if (!(route_old
->flags
& DEVADDR_EXTERNAL
) && route_old
->enabled
&& !keep
)
640 system_del_route(dev
, route_old
);
642 put_source_table(route_old
);
647 bool _enabled
= enable_route(ip
, route_new
);
649 if (!(route_new
->flags
& DEVROUTE_METRIC
))
650 route_new
->metric
= iface
->metric
;
652 if (!(route_new
->flags
& DEVADDR_EXTERNAL
) && !keep
&& _enabled
)
653 system_add_route(dev
, route_new
);
655 route_new
->iface
= iface
;
656 route_new
->enabled
= _enabled
;
661 interface_update_host_route(struct vlist_tree
*tree
,
662 struct vlist_node
*node_new
,
663 struct vlist_node
*node_old
)
665 struct interface
*iface
;
667 struct device_route
*route_old
, *route_new
;
669 iface
= container_of(tree
, struct interface
, host_routes
);
670 dev
= iface
->l3_dev
.dev
;
672 route_old
= container_of(node_old
, struct device_route
, node
);
673 route_new
= container_of(node_new
, struct device_route
, node
);
676 system_del_route(dev
, route_old
);
681 system_add_route(dev
, route_new
);
686 interface_set_prefix_address(struct device_prefix_assignment
*assignment
,
687 const struct device_prefix
*prefix
, struct interface
*iface
, bool add
)
689 const struct interface
*uplink
= prefix
->iface
;
690 if (!iface
->l3_dev
.dev
)
693 struct device
*l3_downlink
= iface
->l3_dev
.dev
;
695 struct device_addr addr
;
696 memset(&addr
, 0, sizeof(addr
));
697 addr
.addr
.in6
= prefix
->addr
;
698 addr
.addr
.in6
.s6_addr32
[1] |= htonl(assignment
->assigned
);
699 addr
.addr
.in6
.s6_addr
[15] += 1;
700 addr
.mask
= assignment
->length
;
701 addr
.flags
= DEVADDR_INET6
;
702 addr
.preferred_until
= prefix
->preferred_until
;
703 addr
.valid_until
= prefix
->valid_until
;
705 if (!add
&& assignment
->enabled
) {
706 time_t now
= system_get_rtime();
707 addr
.preferred_until
= now
;
708 if (!addr
.valid_until
|| addr
.valid_until
- now
> 7200)
709 addr
.valid_until
= now
+ 7200;
710 system_add_address(l3_downlink
, &addr
);
712 set_ip_source_policy(false, true, IPRULE_PRIORITY_NW
, &addr
.addr
,
713 addr
.mask
, prefix
->iface
->ip6table
, iface
, NULL
);
715 set_ip_source_policy(false, true, IPRULE_PRIORITY_REJECT
, &addr
.addr
,
716 addr
.mask
, 0, iface
, "unreachable");
719 assignment
->enabled
= false;
720 } else if (add
&& (iface
->state
== IFS_UP
|| iface
->state
== IFS_SETUP
)) {
721 system_add_address(l3_downlink
, &addr
);
722 if (prefix
->iface
&& !assignment
->enabled
) {
723 set_ip_source_policy(true, true, IPRULE_PRIORITY_REJECT
, &addr
.addr
,
724 addr
.mask
, 0, iface
, "unreachable");
726 set_ip_source_policy(true, true, IPRULE_PRIORITY_NW
, &addr
.addr
,
727 addr
.mask
, prefix
->iface
->ip6table
, iface
, NULL
);
729 if (uplink
&& uplink
->l3_dev
.dev
) {
730 int mtu
= system_update_ipv6_mtu(
731 uplink
->l3_dev
.dev
, 0);
733 system_update_ipv6_mtu(l3_downlink
, mtu
);
735 assignment
->enabled
= true;
739 static bool interface_prefix_assign(struct list_head
*list
,
740 struct device_prefix_assignment
*assign
)
742 int32_t current
= 0, asize
= (1 << (64 - assign
->length
)) - 1;
743 struct device_prefix_assignment
*c
;
744 list_for_each_entry(c
, list
, head
) {
745 if (assign
->assigned
!= -1) {
746 if (assign
->assigned
>= current
&& assign
->assigned
+ asize
< c
->assigned
) {
747 list_add_tail(&assign
->head
, &c
->head
);
750 } else if (assign
->assigned
== -1) {
751 current
= (current
+ asize
) & (~asize
);
752 if (current
+ asize
< c
->assigned
) {
753 assign
->assigned
= current
;
754 list_add_tail(&assign
->head
, &c
->head
);
758 current
= (c
->assigned
+ (1 << (64 - c
->length
)));
763 static void interface_update_prefix_assignments(struct device_prefix
*prefix
, bool setup
)
765 struct device_prefix_assignment
*c
;
766 struct interface
*iface
;
768 // Delete all assignments
769 while (!list_empty(&prefix
->assignments
)) {
770 c
= list_first_entry(&prefix
->assignments
,
771 struct device_prefix_assignment
, head
);
772 if ((iface
= vlist_find(&interfaces
, c
->name
, iface
, node
)))
773 interface_set_prefix_address(c
, prefix
, iface
, false);
781 // End-of-assignment sentinel
782 c
= malloc(sizeof(*c
) + 1);
783 c
->assigned
= 1 << (64 - prefix
->length
);
786 list_add(&c
->head
, &prefix
->assignments
);
789 if (prefix
->excl_length
> 0) {
790 const char name
[] = "!excluded";
791 c
= malloc(sizeof(*c
) + sizeof(name
));
792 c
->assigned
= ntohl(prefix
->excl_addr
.s6_addr32
[1]) &
793 ((1 << (64 - prefix
->length
)) - 1);
794 c
->length
= prefix
->excl_length
;
795 memcpy(c
->name
, name
, sizeof(name
));
796 list_add(&c
->head
, &prefix
->assignments
);
799 bool assigned_any
= false;
800 struct list_head assign_later
= LIST_HEAD_INIT(assign_later
);
801 vlist_for_each_element(&interfaces
, iface
, node
) {
802 if (iface
->assignment_length
< 48 ||
803 iface
->assignment_length
> 64)
806 // Test whether there is a matching class
807 if (!list_empty(&iface
->assignment_classes
)) {
810 struct interface_assignment_class
*c
;
811 list_for_each_entry(c
, &iface
->assignment_classes
, head
) {
812 if (!strcmp(c
->name
, prefix
->pclass
)) {
822 size_t namelen
= strlen(iface
->name
) + 1;
823 c
= malloc(sizeof(*c
) + namelen
);
824 c
->length
= iface
->assignment_length
;
825 c
->assigned
= iface
->assignment_hint
;
827 memcpy(c
->name
, iface
->name
, namelen
);
829 // First process all custom assignments, put all others in later-list
830 if (c
->assigned
== -1 || !interface_prefix_assign(&prefix
->assignments
, c
)) {
831 if (c
->assigned
!= -1) {
833 netifd_log_message(L_WARNING
, "Failed to assign requested subprefix "
834 "of size %hhu for %s, trying other\n", c
->length
, c
->name
);
837 struct list_head
*next
= &assign_later
;
838 struct device_prefix_assignment
*n
;
839 list_for_each_entry(n
, &assign_later
, head
) {
840 if (n
->length
< c
->length
) {
845 list_add_tail(&c
->head
, next
);
848 if (c
->assigned
!= -1)
852 // Then try to assign all other + failed custom assignments
853 while (!list_empty(&assign_later
)) {
854 c
= list_first_entry(&assign_later
, struct device_prefix_assignment
, head
);
857 bool assigned
= false;
859 assigned
= interface_prefix_assign(&prefix
->assignments
, c
);
860 } while (!assigned
&& ++c
->length
<= 64);
863 netifd_log_message(L_WARNING
, "Failed to assign subprefix "
864 "of size %hhu for %s\n", c
->length
, c
->name
);
871 list_for_each_entry(c
, &prefix
->assignments
, head
)
872 if ((iface
= vlist_find(&interfaces
, c
->name
, iface
, node
)))
873 interface_set_prefix_address(c
, prefix
, iface
, true);
876 netifd_log_message(L_WARNING
, "You have delegated IPv6-prefixes but haven't assigned them "
877 "to any interface. Did you forget to set option ip6assign on your lan-interfaces?");
881 void interface_refresh_assignments(bool hint
)
883 static bool refresh
= false;
884 if (!hint
&& refresh
) {
885 struct device_prefix
*p
;
886 list_for_each_entry(p
, &prefixes
, head
)
887 interface_update_prefix_assignments(p
, true);
894 interface_update_prefix(struct vlist_tree
*tree
,
895 struct vlist_node
*node_new
,
896 struct vlist_node
*node_old
)
898 struct device_prefix
*prefix_old
, *prefix_new
;
899 prefix_old
= container_of(node_old
, struct device_prefix
, node
);
900 prefix_new
= container_of(node_new
, struct device_prefix
, node
);
902 struct device_route route
;
903 memset(&route
, 0, sizeof(route
));
904 route
.flags
= DEVADDR_INET6
;
905 route
.metric
= INT32_MAX
;
906 route
.mask
= (node_new
) ? prefix_new
->length
: prefix_old
->length
;
907 route
.addr
.in6
= (node_new
) ? prefix_new
->addr
: prefix_old
->addr
;
910 struct device_prefix_assignment
*c
;
911 struct interface
*iface
;
913 if (node_old
&& node_new
) {
914 // Move assignments and refresh addresses to update valid times
915 list_splice(&prefix_old
->assignments
, &prefix_new
->assignments
);
917 list_for_each_entry(c
, &prefix_new
->assignments
, head
)
918 if ((iface
= vlist_find(&interfaces
, c
->name
, iface
, node
)))
919 interface_set_prefix_address(c
, prefix_new
, iface
, true);
920 } else if (node_new
) {
921 // Set null-route to avoid routing loops
922 system_add_route(NULL
, &route
);
923 interface_update_prefix_assignments(prefix_new
, true);
924 } else if (node_old
) {
926 interface_update_prefix_assignments(prefix_old
, false);
927 system_del_route(NULL
, &route
);
931 list_del(&prefix_old
->head
);
936 list_add(&prefix_new
->head
, &prefixes
);
940 struct device_prefix
*
941 interface_ip_add_device_prefix(struct interface
*iface
, struct in6_addr
*addr
,
942 uint8_t length
, time_t valid_until
, time_t preferred_until
,
943 struct in6_addr
*excl_addr
, uint8_t excl_length
, const char *pclass
)
946 pclass
= (iface
) ? iface
->name
: "local";
948 struct device_prefix
*prefix
= calloc(1, sizeof(*prefix
) + strlen(pclass
) + 1);
949 prefix
->length
= length
;
950 prefix
->addr
= *addr
;
951 prefix
->preferred_until
= preferred_until
;
952 prefix
->valid_until
= valid_until
;
953 prefix
->iface
= iface
;
954 INIT_LIST_HEAD(&prefix
->assignments
);
957 prefix
->excl_addr
= *excl_addr
;
958 prefix
->excl_length
= excl_length
;
961 strcpy(prefix
->pclass
, pclass
);
964 vlist_add(&iface
->proto_ip
.prefix
, &prefix
->node
, &prefix
->addr
);
966 interface_update_prefix(NULL
, &prefix
->node
, NULL
);
972 interface_ip_set_ula_prefix(const char *prefix
)
974 char buf
[INET6_ADDRSTRLEN
+ 4] = {0}, *saveptr
;
976 strncpy(buf
, prefix
, sizeof(buf
) - 1);
977 char *prefixaddr
= strtok_r(buf
, "/", &saveptr
);
979 struct in6_addr addr
;
980 if (!prefixaddr
|| inet_pton(AF_INET6
, prefixaddr
, &addr
) < 1) {
982 interface_update_prefix(NULL
, NULL
, &ula_prefix
->node
);
989 char *prefixlen
= strtok_r(NULL
, ",", &saveptr
);
990 if (!prefixlen
|| (length
= atoi(prefixlen
)) < 1 || length
> 64)
993 if (!ula_prefix
|| !IN6_ARE_ADDR_EQUAL(&addr
, &ula_prefix
->addr
) ||
994 ula_prefix
->length
!= length
) {
996 interface_update_prefix(NULL
, NULL
, &ula_prefix
->node
);
998 ula_prefix
= interface_ip_add_device_prefix(NULL
, &addr
, length
,
999 0, 0, NULL
, 0, NULL
);
1004 interface_add_dns_server(struct interface_ip_settings
*ip
, const char *str
)
1006 struct dns_server
*s
;
1008 s
= calloc(1, sizeof(*s
));
1013 if (inet_pton(s
->af
, str
, &s
->addr
.in
))
1017 if (inet_pton(s
->af
, str
, &s
->addr
.in
))
1024 D(INTERFACE
, "Add IPv%c DNS server: %s\n",
1025 s
->af
== AF_INET6
? '6' : '4', str
);
1026 vlist_simple_add(&ip
->dns_servers
, &s
->node
);
1030 interface_add_dns_server_list(struct interface_ip_settings
*ip
, struct blob_attr
*list
)
1032 struct blob_attr
*cur
;
1035 blobmsg_for_each_attr(cur
, list
, rem
) {
1036 if (blobmsg_type(cur
) != BLOBMSG_TYPE_STRING
)
1039 if (!blobmsg_check_attr(cur
, NULL
))
1042 interface_add_dns_server(ip
, blobmsg_data(cur
));
1047 interface_add_dns_search_domain(struct interface_ip_settings
*ip
, const char *str
)
1049 struct dns_search_domain
*s
;
1050 int len
= strlen(str
);
1052 s
= calloc(1, sizeof(*s
) + len
+ 1);
1056 D(INTERFACE
, "Add DNS search domain: %s\n", str
);
1057 memcpy(s
->name
, str
, len
);
1058 vlist_simple_add(&ip
->dns_search
, &s
->node
);
1062 interface_add_dns_search_list(struct interface_ip_settings
*ip
, struct blob_attr
*list
)
1064 struct blob_attr
*cur
;
1067 blobmsg_for_each_attr(cur
, list
, rem
) {
1068 if (blobmsg_type(cur
) != BLOBMSG_TYPE_STRING
)
1071 if (!blobmsg_check_attr(cur
, NULL
))
1074 interface_add_dns_search_domain(ip
, blobmsg_data(cur
));
1079 write_resolv_conf_entries(FILE *f
, struct interface_ip_settings
*ip
)
1081 struct dns_server
*s
;
1082 struct dns_search_domain
*d
;
1084 char buf
[INET6_ADDRSTRLEN
];
1086 vlist_simple_for_each_element(&ip
->dns_servers
, s
, node
) {
1087 str
= inet_ntop(s
->af
, &s
->addr
, buf
, sizeof(buf
));
1091 fprintf(f
, "nameserver %s\n", str
);
1094 vlist_simple_for_each_element(&ip
->dns_search
, d
, node
) {
1095 fprintf(f
, "search %s\n", d
->name
);
1100 interface_write_resolv_conf(void)
1102 struct interface
*iface
;
1103 char *path
= alloca(strlen(resolv_conf
) + 5);
1105 uint32_t crcold
, crcnew
;
1107 sprintf(path
, "%s.tmp", resolv_conf
);
1109 f
= fopen(path
, "w+");
1111 D(INTERFACE
, "Failed to open %s for writing\n", path
);
1115 vlist_for_each_element(&interfaces
, iface
, node
) {
1116 if (iface
->state
!= IFS_UP
)
1119 if (vlist_simple_empty(&iface
->proto_ip
.dns_search
) &&
1120 vlist_simple_empty(&iface
->proto_ip
.dns_servers
) &&
1121 vlist_simple_empty(&iface
->config_ip
.dns_search
) &&
1122 vlist_simple_empty(&iface
->config_ip
.dns_servers
))
1125 fprintf(f
, "# Interface %s\n", iface
->name
);
1126 write_resolv_conf_entries(f
, &iface
->config_ip
);
1127 if (!iface
->proto_ip
.no_dns
)
1128 write_resolv_conf_entries(f
, &iface
->proto_ip
);
1132 crcnew
= crc32_file(f
);
1135 crcold
= crcnew
+ 1;
1136 f
= fopen(resolv_conf
, "r");
1138 crcold
= crc32_file(f
);
1142 if (crcold
== crcnew
) {
1144 } else if (rename(path
, resolv_conf
) < 0) {
1145 D(INTERFACE
, "Failed to replace %s\n", resolv_conf
);
1150 void interface_ip_set_enabled(struct interface_ip_settings
*ip
, bool enabled
)
1152 struct device_addr
*addr
;
1153 struct device_route
*route
;
1156 ip
->enabled
= enabled
;
1157 dev
= ip
->iface
->l3_dev
.dev
;
1161 vlist_for_each_element(&ip
->addr
, addr
, node
) {
1162 if (addr
->enabled
== enabled
)
1166 system_add_address(dev
, addr
);
1168 system_del_address(dev
, addr
);
1169 addr
->enabled
= enabled
;
1172 vlist_for_each_element(&ip
->route
, route
, node
) {
1173 bool _enabled
= enabled
;
1175 if (!enable_route(ip
, route
))
1178 if (route
->enabled
== _enabled
)
1182 if (!(route
->flags
& DEVROUTE_METRIC
))
1183 route
->metric
= ip
->iface
->metric
;
1185 system_add_route(dev
, route
);
1187 system_del_route(dev
, route
);
1188 route
->enabled
= _enabled
;
1191 struct device_prefix
*c
;
1192 struct device_prefix_assignment
*a
;
1193 list_for_each_entry(c
, &prefixes
, head
)
1194 list_for_each_entry(a
, &c
->assignments
, head
)
1195 if (!strcmp(a
->name
, ip
->iface
->name
))
1196 interface_set_prefix_address(a
, c
, ip
->iface
, enabled
);
1198 if (ip
->iface
&& ip
->iface
->l3_dev
.dev
) {
1199 set_ip_lo_policy(enabled
, true, ip
->iface
);
1200 set_ip_lo_policy(enabled
, false, ip
->iface
);
1202 set_ip_source_policy(enabled
, true, IPRULE_PRIORITY_REJECT
+ ip
->iface
->l3_dev
.dev
->ifindex
,
1203 NULL
, 0, 0, ip
->iface
, "failed_policy");
1208 interface_ip_update_start(struct interface_ip_settings
*ip
)
1210 if (ip
!= &ip
->iface
->config_ip
) {
1211 vlist_simple_update(&ip
->dns_servers
);
1212 vlist_simple_update(&ip
->dns_search
);
1214 vlist_update(&ip
->route
);
1215 vlist_update(&ip
->addr
);
1216 vlist_update(&ip
->prefix
);
1220 interface_ip_update_complete(struct interface_ip_settings
*ip
)
1222 vlist_simple_flush(&ip
->dns_servers
);
1223 vlist_simple_flush(&ip
->dns_search
);
1224 vlist_flush(&ip
->route
);
1225 vlist_flush(&ip
->addr
);
1226 vlist_flush(&ip
->prefix
);
1227 interface_write_resolv_conf();
1231 interface_ip_flush(struct interface_ip_settings
*ip
)
1233 if (ip
== &ip
->iface
->proto_ip
)
1234 vlist_flush_all(&ip
->iface
->host_routes
);
1235 vlist_simple_flush_all(&ip
->dns_servers
);
1236 vlist_simple_flush_all(&ip
->dns_search
);
1237 vlist_flush_all(&ip
->route
);
1238 vlist_flush_all(&ip
->addr
);
1239 vlist_flush_all(&ip
->prefix
);
1243 __interface_ip_init(struct interface_ip_settings
*ip
, struct interface
*iface
)
1247 vlist_simple_init(&ip
->dns_search
, struct dns_search_domain
, node
);
1248 vlist_simple_init(&ip
->dns_servers
, struct dns_server
, node
);
1249 vlist_init(&ip
->route
, route_cmp
, interface_update_proto_route
);
1250 vlist_init(&ip
->addr
, addr_cmp
, interface_update_proto_addr
);
1251 vlist_init(&ip
->prefix
, prefix_cmp
, interface_update_prefix
);
1255 interface_ip_init(struct interface
*iface
)
1257 __interface_ip_init(&iface
->proto_ip
, iface
);
1258 __interface_ip_init(&iface
->config_ip
, iface
);
1259 vlist_init(&iface
->host_routes
, route_cmp
, interface_update_host_route
);
1264 interface_ip_valid_until_handler(struct uloop_timeout
*t
)
1266 time_t now
= system_get_rtime();
1267 struct interface
*iface
;
1268 vlist_for_each_element(&interfaces
, iface
, node
) {
1269 if (iface
->state
!= IFS_UP
)
1272 struct device_addr
*addr
, *addrp
;
1273 struct device_route
*route
, *routep
;
1274 struct device_prefix
*pref
, *prefp
;
1276 vlist_for_each_element_safe(&iface
->proto_ip
.addr
, addr
, node
, addrp
)
1277 if (addr
->valid_until
&& addr
->valid_until
< now
)
1278 vlist_delete(&iface
->proto_ip
.addr
, &addr
->node
);
1280 vlist_for_each_element_safe(&iface
->proto_ip
.route
, route
, node
, routep
)
1281 if (route
->valid_until
&& route
->valid_until
< now
)
1282 vlist_delete(&iface
->proto_ip
.route
, &route
->node
);
1284 vlist_for_each_element_safe(&iface
->proto_ip
.prefix
, pref
, node
, prefp
)
1285 if (pref
->valid_until
&& pref
->valid_until
< now
)
1286 vlist_delete(&iface
->proto_ip
.prefix
, &pref
->node
);
1290 uloop_timeout_set(t
, 1000);
1294 interface_ip_init_worker(void)
1296 valid_until_timeout
.cb
= interface_ip_valid_until_handler
;
1297 uloop_timeout_set(&valid_until_timeout
, 1000);