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"
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
},
54 const struct uci_blob_param_list route_attr_list
= {
55 .n_params
= __ROUTE_MAX
,
60 struct list_head prefixes
= LIST_HEAD_INIT(prefixes
);
61 static struct device_prefix
*ula_prefix
= NULL
;
62 static struct uloop_timeout valid_until_timeout
;
66 clear_if_addr(union if_addr
*a
, int mask
)
68 int m_bytes
= (mask
+ 7) / 8;
69 uint8_t m_clear
= (1 << (m_bytes
* 8 - mask
)) - 1;
70 uint8_t *p
= (uint8_t *) a
;
72 if (m_bytes
< sizeof(a
))
73 memset(p
+ m_bytes
, 0, sizeof(a
) - m_bytes
);
75 p
[m_bytes
- 1] &= ~m_clear
;
79 match_if_addr(union if_addr
*a1
, union if_addr
*a2
, int mask
)
81 union if_addr
*p1
, *p2
;
83 p1
= alloca(sizeof(*a1
));
84 p2
= alloca(sizeof(*a2
));
86 memcpy(p1
, a1
, sizeof(*a1
));
87 clear_if_addr(p1
, mask
);
88 memcpy(p2
, a2
, sizeof(*a2
));
89 clear_if_addr(p2
, mask
);
91 return !memcmp(p1
, p2
, sizeof(*p1
));
94 static int set_ip_source_policy(bool add
, bool v6
, unsigned int priority
,
95 const union if_addr
*addr
, uint8_t mask
, struct interface
*iface
,
96 struct interface
*in_iface
, const char *action
)
98 struct iprule rule
= {
99 .flags
= IPRULE_PRIORITY
,
104 rule
.flags
|= IPRULE_SRC
;
105 rule
.src_addr
= *addr
;
106 rule
.src_mask
= mask
;
110 rule
.flags
|= IPRULE_LOOKUP
;
111 rule
.lookup
= (v6
) ? iface
->ip6table
: iface
->ip4table
;
116 rule
.flags
|= IPRULE_ACTION
;
117 system_resolve_iprule_action(action
, &rule
.action
);
120 if (in_iface
&& in_iface
->l3_dev
.dev
) {
121 rule
.flags
|= IPRULE_IN
;
122 strcpy(rule
.in_dev
, in_iface
->l3_dev
.dev
->ifname
);
125 rule
.flags
|= (v6
) ? IPRULE_INET6
: IPRULE_INET4
;
127 return (add
) ? system_add_iprule(&rule
) : system_del_iprule(&rule
);
130 static int set_ip_lo_policy(bool add
, bool v6
, struct interface
*iface
)
132 struct iprule rule
= {
133 .flags
= IPRULE_IN
| IPRULE_LOOKUP
| IPRULE_PRIORITY
,
134 .priority
= IPRULE_PRIORITY_NW
+ iface
->l3_dev
.dev
->ifindex
,
135 .lookup
= (v6
) ? iface
->ip6table
: iface
->ip4table
,
142 rule
.flags
|= (v6
) ? IPRULE_INET6
: IPRULE_INET4
;
144 return (add
) ? system_add_iprule(&rule
) : system_del_iprule(&rule
);
148 __find_ip_addr_target(struct interface_ip_settings
*ip
, union if_addr
*a
, bool v6
)
150 struct device_addr
*addr
;
152 vlist_for_each_element(&ip
->addr
, addr
, node
) {
156 if (v6
!= ((addr
->flags
& DEVADDR_FAMILY
) == DEVADDR_INET6
))
159 // Handle offlink addresses correctly
160 unsigned int mask
= addr
->mask
;
161 if ((addr
->flags
& DEVADDR_FAMILY
) == DEVADDR_INET6
&&
162 (addr
->flags
& DEVADDR_OFFLINK
))
165 if (!match_if_addr(&addr
->addr
, a
, mask
))
175 __find_ip_route_target(struct interface_ip_settings
*ip
, union if_addr
*a
,
176 bool v6
, struct device_route
**res
)
178 struct device_route
*route
;
180 vlist_for_each_element(&ip
->route
, route
, node
) {
184 if (v6
!= ((route
->flags
& DEVADDR_FAMILY
) == DEVADDR_INET6
))
187 if (!match_if_addr(&route
->addr
, a
, route
->mask
))
190 if (route
->flags
& DEVROUTE_TABLE
)
193 if (!*res
|| route
->mask
< (*res
)->mask
)
199 interface_ip_find_addr_target(struct interface
*iface
, union if_addr
*a
, bool v6
)
201 return __find_ip_addr_target(&iface
->proto_ip
, a
, v6
) ||
202 __find_ip_addr_target(&iface
->config_ip
, a
, v6
);
206 interface_ip_find_route_target(struct interface
*iface
, union if_addr
*a
,
207 bool v6
, struct device_route
**route
)
209 __find_ip_route_target(&iface
->proto_ip
, a
, v6
, route
);
210 __find_ip_route_target(&iface
->config_ip
, a
, v6
, route
);
214 interface_ip_add_target_route(union if_addr
*addr
, bool v6
, struct interface
*iface
)
216 struct device_route
*route
, *r_next
= NULL
;
217 bool defaultroute_target
= false;
218 int addrsize
= v6
? sizeof(addr
->in6
) : sizeof(addr
->in
);
220 route
= calloc(1, sizeof(*route
));
224 route
->flags
= v6
? DEVADDR_INET6
: DEVADDR_INET4
;
225 route
->mask
= v6
? 128 : 32;
226 if (memcmp(&route
->addr
, addr
, addrsize
) == 0)
227 defaultroute_target
= true;
229 memcpy(&route
->addr
, addr
, addrsize
);
232 /* look for locally addressable target first */
233 if (interface_ip_find_addr_target(iface
, addr
, v6
))
236 /* do not stop at the first route, let the lookup compare
237 * masks to find the best match */
238 interface_ip_find_route_target(iface
, addr
, v6
, &r_next
);
240 vlist_for_each_element(&interfaces
, iface
, node
) {
241 /* look for locally addressable target first */
242 if (interface_ip_find_addr_target(iface
, addr
, v6
))
245 /* do not stop at the first route, let the lookup compare
246 * masks to find the best match */
247 interface_ip_find_route_target(iface
, addr
, v6
, &r_next
);
256 iface
= r_next
->iface
;
257 memcpy(&route
->nexthop
, &r_next
->nexthop
, sizeof(route
->nexthop
));
258 route
->mtu
= r_next
->mtu
;
259 route
->metric
= r_next
->metric
;
260 route
->table
= r_next
->table
;
263 route
->iface
= iface
;
264 if (defaultroute_target
)
267 vlist_add(&iface
->host_routes
, &route
->node
, route
);
272 interface_ip_add_route(struct interface
*iface
, struct blob_attr
*attr
, bool v6
)
274 struct interface_ip_settings
*ip
;
275 struct blob_attr
*tb
[__ROUTE_MAX
], *cur
;
276 struct device_route
*route
;
277 int af
= v6
? AF_INET6
: AF_INET
;
278 bool is_proto_route
= !!iface
;
280 blobmsg_parse(route_attr
, __ROUTE_MAX
, tb
, blobmsg_data(attr
), blobmsg_data_len(attr
));
283 if ((cur
= tb
[ROUTE_INTERFACE
]) == NULL
)
286 iface
= vlist_find(&interfaces
, blobmsg_data(cur
), iface
, node
);
290 ip
= &iface
->config_ip
;
292 ip
= &iface
->proto_ip
;
295 route
= calloc(1, sizeof(*route
));
299 route
->flags
= v6
? DEVADDR_INET6
: DEVADDR_INET4
;
300 route
->mask
= v6
? 128 : 32;
301 if ((cur
= tb
[ROUTE_MASK
]) != NULL
) {
302 route
->mask
= parse_netmask_string(blobmsg_data(cur
), v6
);
303 if (route
->mask
> (v6
? 128 : 32))
307 if ((cur
= tb
[ROUTE_TARGET
]) != NULL
) {
308 if (!parse_ip_and_netmask(af
, blobmsg_data(cur
), &route
->addr
, &route
->mask
)) {
309 DPRINTF("Failed to parse route target: %s\n", (char *) blobmsg_data(cur
));
314 if ((cur
= tb
[ROUTE_GATEWAY
]) != NULL
) {
315 if (!inet_pton(af
, blobmsg_data(cur
), &route
->nexthop
)) {
316 DPRINTF("Failed to parse route gateway: %s\n", (char *) blobmsg_data(cur
));
321 if ((cur
= tb
[ROUTE_METRIC
]) != NULL
) {
322 route
->metric
= blobmsg_get_u32(cur
);
323 route
->flags
|= DEVROUTE_METRIC
;
326 if ((cur
= tb
[ROUTE_MTU
]) != NULL
) {
327 route
->mtu
= blobmsg_get_u32(cur
);
328 route
->flags
|= DEVROUTE_MTU
;
331 // Use source-based routing
332 if (is_proto_route
) {
333 route
->table
= (v6
) ? iface
->ip6table
: iface
->ip4table
;
334 route
->flags
|= DEVROUTE_SRCTABLE
;
337 if ((cur
= tb
[ROUTE_TABLE
]) != NULL
) {
338 if (!system_resolve_rt_table(blobmsg_data(cur
), &route
->table
)) {
339 DPRINTF("Failed to resolve routing table: %s\n", (char *) blobmsg_data(cur
));
344 route
->flags
|= DEVROUTE_TABLE
;
347 if ((cur
= tb
[ROUTE_VALID
]) != NULL
) {
348 int64_t valid
= blobmsg_get_u32(cur
);
349 int64_t valid_until
= valid
+ (int64_t)system_get_rtime();
350 if (valid_until
<= LONG_MAX
&& valid
!= 0xffffffffLL
) // Catch overflow
351 route
->valid_until
= valid_until
;
354 vlist_add(&ip
->route
, &route
->node
, route
);
362 addr_cmp(const void *k1
, const void *k2
, void *ptr
)
364 return memcmp(k1
, k2
, sizeof(struct device_addr
) -
365 offsetof(struct device_addr
, flags
));
369 route_cmp(const void *k1
, const void *k2
, void *ptr
)
371 const struct device_route
*r1
= k1
, *r2
= k2
;
373 if (r1
->mask
!= r2
->mask
)
374 return r2
->mask
- r1
->mask
;
376 if (r1
->metric
!= r2
->metric
)
377 return r1
->metric
- r2
->metric
;
379 if (r1
->flags
!= r2
->flags
)
380 return r2
->flags
- r1
->flags
;
382 return memcmp(&r1
->addr
, &r2
->addr
, sizeof(r1
->addr
));
386 prefix_cmp(const void *k1
, const void *k2
, void *ptr
)
388 return memcmp(k1
, k2
, offsetof(struct device_prefix
, pclass
) -
389 offsetof(struct device_prefix
, addr
));
393 interface_handle_subnet_route(struct interface
*iface
, struct device_addr
*addr
, bool add
)
395 struct device
*dev
= iface
->l3_dev
.dev
;
396 struct device_route route
;
398 memset(&route
, 0, sizeof(route
));
400 route
.flags
= addr
->flags
;
401 route
.mask
= addr
->mask
;
402 memcpy(&route
.addr
, &addr
->addr
, sizeof(route
.addr
));
403 clear_if_addr(&route
.addr
, route
.mask
);
406 route
.flags
|= DEVADDR_KERNEL
;
407 system_del_route(dev
, &route
);
409 if (!(addr
->flags
& DEVADDR_OFFLINK
)) {
410 route
.flags
&= ~DEVADDR_KERNEL
;
411 route
.metric
= iface
->metric
;
412 system_add_route(dev
, &route
);
415 if (!(addr
->flags
& DEVADDR_OFFLINK
))
416 system_del_route(dev
, &route
);
421 interface_update_proto_addr(struct vlist_tree
*tree
,
422 struct vlist_node
*node_new
,
423 struct vlist_node
*node_old
)
425 struct interface_ip_settings
*ip
;
426 struct interface
*iface
;
428 struct device_addr
*a_new
= NULL
, *a_old
= NULL
;
432 ip
= container_of(tree
, struct interface_ip_settings
, addr
);
434 dev
= iface
->l3_dev
.dev
;
437 a_new
= container_of(node_new
, struct device_addr
, node
);
439 if ((a_new
->flags
& DEVADDR_FAMILY
) == DEVADDR_INET4
&&
443 uint32_t *a
= (uint32_t *) &a_new
->addr
;
445 mask
>>= a_new
->mask
;
446 a_new
->broadcast
= *a
| htonl(mask
);
451 a_old
= container_of(node_old
, struct device_addr
, node
);
453 if (a_new
&& a_old
) {
456 if (a_old
->flags
!= a_new
->flags
||
457 a_old
->valid_until
!= a_new
->valid_until
||
458 a_old
->preferred_until
!= a_new
->preferred_until
)
461 if ((a_new
->flags
& DEVADDR_FAMILY
) == DEVADDR_INET4
&&
462 a_new
->broadcast
!= a_old
->broadcast
)
467 if (!(a_old
->flags
& DEVADDR_EXTERNAL
) && a_old
->enabled
&& !keep
) {
468 interface_handle_subnet_route(iface
, a_old
, false);
470 if ((a_old
->flags
& DEVADDR_FAMILY
) == DEVADDR_INET6
)
473 //This is needed for source routing to work correctly. If a device
474 //has two connections to a network using the same subnet, adding
475 //only the network-rule will cause packets to be routed through the
476 //first matching network (source IP matches both masks).
477 set_ip_source_policy(false, v6
, IPRULE_PRIORITY_ADDR
, &a_old
->addr
,
478 (v6
) ? 128 : 32, iface
, NULL
, NULL
);
479 set_ip_source_policy(false, v6
, IPRULE_PRIORITY_NW
, &a_old
->addr
,
480 a_old
->mask
, iface
, NULL
, NULL
);
482 system_del_address(dev
, a_old
);
488 a_new
->enabled
= true;
489 if (!(a_new
->flags
& DEVADDR_EXTERNAL
) && !keep
) {
490 system_add_address(dev
, a_new
);
492 if ((a_new
->flags
& DEVADDR_FAMILY
) == DEVADDR_INET6
)
495 set_ip_source_policy(true, v6
, IPRULE_PRIORITY_ADDR
, &a_new
->addr
,
496 (v6
) ? 128 : 32, iface
, NULL
, NULL
);
497 set_ip_source_policy(true, v6
, IPRULE_PRIORITY_NW
, &a_new
->addr
,
498 a_new
->mask
, iface
, NULL
, NULL
);
500 if ((a_new
->flags
& DEVADDR_OFFLINK
) || iface
->metric
)
501 interface_handle_subnet_route(iface
, a_new
, true);
507 enable_route(struct interface_ip_settings
*ip
, struct device_route
*route
)
509 if (ip
->no_defaultroute
&& !route
->mask
)
516 interface_update_proto_route(struct vlist_tree
*tree
,
517 struct vlist_node
*node_new
,
518 struct vlist_node
*node_old
)
520 struct interface_ip_settings
*ip
;
521 struct interface
*iface
;
523 struct device_route
*route_old
, *route_new
;
526 ip
= container_of(tree
, struct interface_ip_settings
, route
);
528 dev
= iface
->l3_dev
.dev
;
530 route_old
= container_of(node_old
, struct device_route
, node
);
531 route_new
= container_of(node_new
, struct device_route
, node
);
533 if (node_old
&& node_new
)
534 keep
= !memcmp(&route_old
->nexthop
, &route_new
->nexthop
, sizeof(route_old
->nexthop
));
537 if (!(route_old
->flags
& DEVADDR_EXTERNAL
) && route_old
->enabled
&& !keep
)
538 system_del_route(dev
, route_old
);
543 bool _enabled
= enable_route(ip
, route_new
);
545 if (!(route_new
->flags
& DEVROUTE_METRIC
))
546 route_new
->metric
= iface
->metric
;
548 if (!(route_new
->flags
& DEVADDR_EXTERNAL
) && !keep
&& _enabled
)
549 system_add_route(dev
, route_new
);
551 route_new
->iface
= iface
;
552 route_new
->enabled
= _enabled
;
557 interface_update_host_route(struct vlist_tree
*tree
,
558 struct vlist_node
*node_new
,
559 struct vlist_node
*node_old
)
561 struct interface
*iface
;
563 struct device_route
*route_old
, *route_new
;
565 iface
= container_of(tree
, struct interface
, host_routes
);
566 dev
= iface
->l3_dev
.dev
;
568 route_old
= container_of(node_old
, struct device_route
, node
);
569 route_new
= container_of(node_new
, struct device_route
, node
);
572 system_del_route(dev
, route_old
);
577 system_add_route(dev
, route_new
);
582 interface_set_prefix_address(struct device_prefix_assignment
*assignment
,
583 const struct device_prefix
*prefix
, struct interface
*iface
, bool add
)
585 const struct interface
*uplink
= prefix
->iface
;
586 if (!iface
->l3_dev
.dev
)
589 struct device
*l3_downlink
= iface
->l3_dev
.dev
;
591 struct device_addr addr
;
592 memset(&addr
, 0, sizeof(addr
));
593 addr
.addr
.in6
= prefix
->addr
;
594 addr
.addr
.in6
.s6_addr32
[1] |= htonl(assignment
->assigned
);
595 addr
.addr
.in6
.s6_addr
[15] += 1;
596 addr
.mask
= assignment
->length
;
597 addr
.flags
= DEVADDR_INET6
;
598 addr
.preferred_until
= prefix
->preferred_until
;
599 addr
.valid_until
= prefix
->valid_until
;
601 if (!add
&& assignment
->enabled
) {
602 time_t now
= system_get_rtime();
603 addr
.preferred_until
= now
;
604 if (!addr
.valid_until
|| addr
.valid_until
- now
> 7200)
605 addr
.valid_until
= now
+ 7200;
606 system_add_address(l3_downlink
, &addr
);
608 set_ip_source_policy(false, true, IPRULE_PRIORITY_NW
, &addr
.addr
,
609 addr
.mask
, prefix
->iface
, iface
, NULL
);
611 set_ip_source_policy(false, true, IPRULE_PRIORITY_REJECT
, &addr
.addr
,
612 addr
.mask
, NULL
, iface
, "unreachable");
615 assignment
->enabled
= false;
616 } else if (add
&& (iface
->state
== IFS_UP
|| iface
->state
== IFS_SETUP
)) {
617 system_add_address(l3_downlink
, &addr
);
618 if (prefix
->iface
&& !assignment
->enabled
) {
619 set_ip_source_policy(true, true, IPRULE_PRIORITY_REJECT
, &addr
.addr
,
620 addr
.mask
, NULL
, iface
, "unreachable");
622 set_ip_source_policy(true, true, IPRULE_PRIORITY_NW
, &addr
.addr
,
623 addr
.mask
, prefix
->iface
, iface
, NULL
);
625 if (uplink
&& uplink
->l3_dev
.dev
) {
626 int mtu
= system_update_ipv6_mtu(
627 uplink
->l3_dev
.dev
, 0);
629 system_update_ipv6_mtu(l3_downlink
, mtu
);
631 assignment
->enabled
= true;
635 static bool interface_prefix_assign(struct list_head
*list
,
636 struct device_prefix_assignment
*assign
)
638 int32_t current
= 0, asize
= (1 << (64 - assign
->length
)) - 1;
639 struct device_prefix_assignment
*c
;
640 list_for_each_entry(c
, list
, head
) {
641 if (assign
->assigned
!= -1) {
642 if (assign
->assigned
> current
&& assign
->assigned
+ asize
< c
->assigned
) {
643 list_add_tail(&assign
->head
, &c
->head
);
646 } else if (assign
->assigned
== -1) {
647 current
= (current
+ asize
) & (~asize
);
648 if (current
+ asize
< c
->assigned
) {
649 assign
->assigned
= current
;
650 list_add_tail(&assign
->head
, &c
->head
);
654 current
= (c
->assigned
+ (1 << (64 - c
->length
)));
659 static void interface_update_prefix_assignments(struct device_prefix
*prefix
, bool setup
)
661 struct device_prefix_assignment
*c
;
662 struct interface
*iface
;
664 // Delete all assignments
665 while (!list_empty(&prefix
->assignments
)) {
666 c
= list_first_entry(&prefix
->assignments
,
667 struct device_prefix_assignment
, head
);
668 if ((iface
= vlist_find(&interfaces
, c
->name
, iface
, node
)))
669 interface_set_prefix_address(c
, prefix
, iface
, false);
677 // End-of-assignment sentinel
678 c
= malloc(sizeof(*c
) + 1);
679 c
->assigned
= 1 << (64 - prefix
->length
);
682 list_add(&c
->head
, &prefix
->assignments
);
685 if (prefix
->excl_length
> 0) {
686 const char name
[] = "!excluded";
687 c
= malloc(sizeof(*c
) + sizeof(name
));
688 c
->assigned
= ntohl(prefix
->excl_addr
.s6_addr32
[1]) &
689 ((1 << (64 - prefix
->length
)) - 1);
690 c
->length
= prefix
->excl_length
;
691 memcpy(c
->name
, name
, sizeof(name
));
692 list_add(&c
->head
, &prefix
->assignments
);
695 bool assigned_any
= false;
696 struct list_head assign_later
= LIST_HEAD_INIT(assign_later
);
697 vlist_for_each_element(&interfaces
, iface
, node
) {
698 if (iface
->assignment_length
< 48 ||
699 iface
->assignment_length
> 64)
702 // Test whether there is a matching class
703 if (!list_empty(&iface
->assignment_classes
)) {
706 struct interface_assignment_class
*c
;
707 list_for_each_entry(c
, &iface
->assignment_classes
, head
) {
708 if (!strcmp(c
->name
, prefix
->pclass
)) {
718 size_t namelen
= strlen(iface
->name
) + 1;
719 c
= malloc(sizeof(*c
) + namelen
);
720 c
->length
= iface
->assignment_length
;
721 c
->assigned
= iface
->assignment_hint
;
723 memcpy(c
->name
, iface
->name
, namelen
);
725 // First process all custom assignments, put all others in later-list
726 if (c
->assigned
== -1 || !interface_prefix_assign(&prefix
->assignments
, c
)) {
727 if (c
->assigned
!= -1) {
729 netifd_log_message(L_WARNING
, "Failed to assign requested subprefix "
730 "of size %hhu for %s, trying other\n", c
->length
, c
->name
);
733 struct list_head
*next
= &assign_later
;
734 struct device_prefix_assignment
*n
;
735 list_for_each_entry(n
, &assign_later
, head
) {
736 if (n
->length
> c
->length
) {
741 list_add_tail(&c
->head
, next
);
744 if (c
->assigned
!= -1)
748 // Then try to assign all other + failed custom assignments
749 while (!list_empty(&assign_later
)) {
750 c
= list_first_entry(&assign_later
, struct device_prefix_assignment
, head
);
753 bool assigned
= false;
755 assigned
= interface_prefix_assign(&prefix
->assignments
, c
);
756 } while (!assigned
&& ++c
->length
<= 64);
759 netifd_log_message(L_WARNING
, "Failed to assign subprefix "
760 "of size %hhu for %s\n", c
->length
, c
->name
);
767 list_for_each_entry(c
, &prefix
->assignments
, head
)
768 if ((iface
= vlist_find(&interfaces
, c
->name
, iface
, node
)))
769 interface_set_prefix_address(c
, prefix
, iface
, true);
772 netifd_log_message(L_WARNING
, "You have delegated IPv6-prefixes but haven't assigned them "
773 "to any interface. Did you forget to set option ip6assign on your lan-interfaces?");
777 void interface_refresh_assignments(bool hint
)
779 static bool refresh
= false;
780 if (!hint
&& refresh
) {
781 struct device_prefix
*p
;
782 list_for_each_entry(p
, &prefixes
, head
)
783 interface_update_prefix_assignments(p
, true);
790 interface_update_prefix(struct vlist_tree
*tree
,
791 struct vlist_node
*node_new
,
792 struct vlist_node
*node_old
)
794 struct device_prefix
*prefix_old
, *prefix_new
;
795 prefix_old
= container_of(node_old
, struct device_prefix
, node
);
796 prefix_new
= container_of(node_new
, struct device_prefix
, node
);
798 struct device_route route
;
799 memset(&route
, 0, sizeof(route
));
800 route
.flags
= DEVADDR_INET6
;
801 route
.metric
= INT32_MAX
;
802 route
.mask
= (node_new
) ? prefix_new
->length
: prefix_old
->length
;
803 route
.addr
.in6
= (node_new
) ? prefix_new
->addr
: prefix_old
->addr
;
806 struct device_prefix_assignment
*c
;
807 struct interface
*iface
;
809 if (node_old
&& node_new
) {
810 // Move assignments and refresh addresses to update valid times
811 list_splice(&prefix_old
->assignments
, &prefix_new
->assignments
);
813 list_for_each_entry(c
, &prefix_new
->assignments
, head
)
814 if ((iface
= vlist_find(&interfaces
, c
->name
, iface
, node
)))
815 interface_set_prefix_address(c
, prefix_new
, iface
, true);
816 } else if (node_new
) {
817 // Set null-route to avoid routing loops
818 system_add_route(NULL
, &route
);
819 interface_update_prefix_assignments(prefix_new
, true);
820 } else if (node_old
) {
822 interface_update_prefix_assignments(prefix_old
, false);
823 system_del_route(NULL
, &route
);
827 list_del(&prefix_old
->head
);
832 list_add(&prefix_new
->head
, &prefixes
);
836 struct device_prefix
*
837 interface_ip_add_device_prefix(struct interface
*iface
, struct in6_addr
*addr
,
838 uint8_t length
, time_t valid_until
, time_t preferred_until
,
839 struct in6_addr
*excl_addr
, uint8_t excl_length
, const char *pclass
)
842 pclass
= (iface
) ? iface
->name
: "local";
844 struct device_prefix
*prefix
= calloc(1, sizeof(*prefix
) + strlen(pclass
) + 1);
845 prefix
->length
= length
;
846 prefix
->addr
= *addr
;
847 prefix
->preferred_until
= preferred_until
;
848 prefix
->valid_until
= valid_until
;
849 prefix
->iface
= iface
;
850 INIT_LIST_HEAD(&prefix
->assignments
);
853 prefix
->excl_addr
= *excl_addr
;
854 prefix
->excl_length
= excl_length
;
857 strcpy(prefix
->pclass
, pclass
);
860 vlist_add(&iface
->proto_ip
.prefix
, &prefix
->node
, &prefix
->addr
);
862 interface_update_prefix(NULL
, &prefix
->node
, NULL
);
868 interface_ip_set_ula_prefix(const char *prefix
)
870 char buf
[INET6_ADDRSTRLEN
+ 4] = {0}, *saveptr
;
872 strncpy(buf
, prefix
, sizeof(buf
) - 1);
873 char *prefixaddr
= strtok_r(buf
, "/", &saveptr
);
875 struct in6_addr addr
;
876 if (!prefixaddr
|| inet_pton(AF_INET6
, prefixaddr
, &addr
) < 1) {
878 interface_update_prefix(NULL
, NULL
, &ula_prefix
->node
);
885 char *prefixlen
= strtok_r(NULL
, ",", &saveptr
);
886 if (!prefixlen
|| (length
= atoi(prefixlen
)) < 1 || length
> 64)
889 if (!ula_prefix
|| !IN6_ARE_ADDR_EQUAL(&addr
, &ula_prefix
->addr
) ||
890 ula_prefix
->length
!= length
) {
892 interface_update_prefix(NULL
, NULL
, &ula_prefix
->node
);
894 ula_prefix
= interface_ip_add_device_prefix(NULL
, &addr
, length
,
895 0, 0, NULL
, 0, NULL
);
900 interface_add_dns_server(struct interface_ip_settings
*ip
, const char *str
)
902 struct dns_server
*s
;
904 s
= calloc(1, sizeof(*s
));
909 if (inet_pton(s
->af
, str
, &s
->addr
.in
))
913 if (inet_pton(s
->af
, str
, &s
->addr
.in
))
920 D(INTERFACE
, "Add IPv%c DNS server: %s\n",
921 s
->af
== AF_INET6
? '6' : '4', str
);
922 vlist_simple_add(&ip
->dns_servers
, &s
->node
);
926 interface_add_dns_server_list(struct interface_ip_settings
*ip
, struct blob_attr
*list
)
928 struct blob_attr
*cur
;
931 blobmsg_for_each_attr(cur
, list
, rem
) {
932 if (blobmsg_type(cur
) != BLOBMSG_TYPE_STRING
)
935 if (!blobmsg_check_attr(cur
, NULL
))
938 interface_add_dns_server(ip
, blobmsg_data(cur
));
943 interface_add_dns_search_domain(struct interface_ip_settings
*ip
, const char *str
)
945 struct dns_search_domain
*s
;
946 int len
= strlen(str
);
948 s
= calloc(1, sizeof(*s
) + len
+ 1);
952 D(INTERFACE
, "Add DNS search domain: %s\n", str
);
953 memcpy(s
->name
, str
, len
);
954 vlist_simple_add(&ip
->dns_search
, &s
->node
);
958 interface_add_dns_search_list(struct interface_ip_settings
*ip
, struct blob_attr
*list
)
960 struct blob_attr
*cur
;
963 blobmsg_for_each_attr(cur
, list
, rem
) {
964 if (blobmsg_type(cur
) != BLOBMSG_TYPE_STRING
)
967 if (!blobmsg_check_attr(cur
, NULL
))
970 interface_add_dns_search_domain(ip
, blobmsg_data(cur
));
975 write_resolv_conf_entries(FILE *f
, struct interface_ip_settings
*ip
)
977 struct dns_server
*s
;
978 struct dns_search_domain
*d
;
980 char buf
[INET6_ADDRSTRLEN
];
982 vlist_simple_for_each_element(&ip
->dns_servers
, s
, node
) {
983 str
= inet_ntop(s
->af
, &s
->addr
, buf
, sizeof(buf
));
987 fprintf(f
, "nameserver %s\n", str
);
990 vlist_simple_for_each_element(&ip
->dns_search
, d
, node
) {
991 fprintf(f
, "search %s\n", d
->name
);
996 interface_write_resolv_conf(void)
998 struct interface
*iface
;
999 char *path
= alloca(strlen(resolv_conf
) + 5);
1001 uint32_t crcold
, crcnew
;
1003 sprintf(path
, "%s.tmp", resolv_conf
);
1005 f
= fopen(path
, "w+");
1007 D(INTERFACE
, "Failed to open %s for writing\n", path
);
1011 vlist_for_each_element(&interfaces
, iface
, node
) {
1012 if (iface
->state
!= IFS_UP
)
1015 if (vlist_simple_empty(&iface
->proto_ip
.dns_search
) &&
1016 vlist_simple_empty(&iface
->proto_ip
.dns_servers
) &&
1017 vlist_simple_empty(&iface
->config_ip
.dns_search
) &&
1018 vlist_simple_empty(&iface
->config_ip
.dns_servers
))
1021 fprintf(f
, "# Interface %s\n", iface
->name
);
1022 write_resolv_conf_entries(f
, &iface
->config_ip
);
1023 if (!iface
->proto_ip
.no_dns
)
1024 write_resolv_conf_entries(f
, &iface
->proto_ip
);
1028 crcnew
= crc32_file(f
);
1031 crcold
= crcnew
+ 1;
1032 f
= fopen(resolv_conf
, "r");
1034 crcold
= crc32_file(f
);
1038 if (crcold
== crcnew
) {
1040 } else if (rename(path
, resolv_conf
) < 0) {
1041 D(INTERFACE
, "Failed to replace %s\n", resolv_conf
);
1046 void interface_ip_set_enabled(struct interface_ip_settings
*ip
, bool enabled
)
1048 struct device_addr
*addr
;
1049 struct device_route
*route
;
1052 ip
->enabled
= enabled
;
1053 dev
= ip
->iface
->l3_dev
.dev
;
1057 vlist_for_each_element(&ip
->addr
, addr
, node
) {
1058 if (addr
->enabled
== enabled
)
1062 system_add_address(dev
, addr
);
1064 system_del_address(dev
, addr
);
1065 addr
->enabled
= enabled
;
1068 vlist_for_each_element(&ip
->route
, route
, node
) {
1069 bool _enabled
= enabled
;
1071 if (!enable_route(ip
, route
))
1074 if (route
->enabled
== _enabled
)
1078 if (!(route
->flags
& DEVROUTE_METRIC
))
1079 route
->metric
= ip
->iface
->metric
;
1081 system_add_route(dev
, route
);
1083 system_del_route(dev
, route
);
1084 route
->enabled
= _enabled
;
1087 struct device_prefix
*c
;
1088 struct device_prefix_assignment
*a
;
1089 list_for_each_entry(c
, &prefixes
, head
)
1090 list_for_each_entry(a
, &c
->assignments
, head
)
1091 if (!strcmp(a
->name
, ip
->iface
->name
))
1092 interface_set_prefix_address(a
, c
, ip
->iface
, enabled
);
1094 if (ip
->iface
&& ip
->iface
->l3_dev
.dev
) {
1095 set_ip_lo_policy(enabled
, true, ip
->iface
);
1096 set_ip_lo_policy(enabled
, false, ip
->iface
);
1098 set_ip_source_policy(enabled
, true, IPRULE_PRIORITY_REJECT
+ ip
->iface
->l3_dev
.dev
->ifindex
,
1099 NULL
, 0, NULL
, ip
->iface
, "failed_policy");
1104 interface_ip_update_start(struct interface_ip_settings
*ip
)
1106 if (ip
!= &ip
->iface
->config_ip
) {
1107 vlist_simple_update(&ip
->dns_servers
);
1108 vlist_simple_update(&ip
->dns_search
);
1110 vlist_update(&ip
->route
);
1111 vlist_update(&ip
->addr
);
1112 vlist_update(&ip
->prefix
);
1116 interface_ip_update_complete(struct interface_ip_settings
*ip
)
1118 vlist_simple_flush(&ip
->dns_servers
);
1119 vlist_simple_flush(&ip
->dns_search
);
1120 vlist_flush(&ip
->route
);
1121 vlist_flush(&ip
->addr
);
1122 vlist_flush(&ip
->prefix
);
1123 interface_write_resolv_conf();
1127 interface_ip_flush(struct interface_ip_settings
*ip
)
1129 if (ip
== &ip
->iface
->proto_ip
)
1130 vlist_flush_all(&ip
->iface
->host_routes
);
1131 vlist_simple_flush_all(&ip
->dns_servers
);
1132 vlist_simple_flush_all(&ip
->dns_search
);
1133 vlist_flush_all(&ip
->route
);
1134 vlist_flush_all(&ip
->addr
);
1135 vlist_flush_all(&ip
->prefix
);
1139 __interface_ip_init(struct interface_ip_settings
*ip
, struct interface
*iface
)
1143 vlist_simple_init(&ip
->dns_search
, struct dns_search_domain
, node
);
1144 vlist_simple_init(&ip
->dns_servers
, struct dns_server
, node
);
1145 vlist_init(&ip
->route
, route_cmp
, interface_update_proto_route
);
1146 vlist_init(&ip
->addr
, addr_cmp
, interface_update_proto_addr
);
1147 vlist_init(&ip
->prefix
, prefix_cmp
, interface_update_prefix
);
1151 interface_ip_init(struct interface
*iface
)
1153 __interface_ip_init(&iface
->proto_ip
, iface
);
1154 __interface_ip_init(&iface
->config_ip
, iface
);
1155 vlist_init(&iface
->host_routes
, route_cmp
, interface_update_host_route
);
1160 interface_ip_valid_until_handler(struct uloop_timeout
*t
)
1162 time_t now
= system_get_rtime();
1163 struct interface
*iface
;
1164 vlist_for_each_element(&interfaces
, iface
, node
) {
1165 if (iface
->state
!= IFS_UP
)
1168 struct device_addr
*addr
, *addrp
;
1169 struct device_route
*route
, *routep
;
1170 struct device_prefix
*pref
, *prefp
;
1172 vlist_for_each_element_safe(&iface
->proto_ip
.addr
, addr
, node
, addrp
)
1173 if (addr
->valid_until
&& addr
->valid_until
< now
)
1174 vlist_delete(&iface
->proto_ip
.addr
, &addr
->node
);
1176 vlist_for_each_element_safe(&iface
->proto_ip
.route
, route
, node
, routep
)
1177 if (route
->valid_until
&& route
->valid_until
< now
)
1178 vlist_delete(&iface
->proto_ip
.route
, &route
->node
);
1180 vlist_for_each_element_safe(&iface
->proto_ip
.prefix
, pref
, node
, prefp
)
1181 if (pref
->valid_until
&& pref
->valid_until
< now
)
1182 vlist_delete(&iface
->proto_ip
.prefix
, &pref
->node
);
1186 uloop_timeout_set(t
, 1000);
1190 interface_ip_init_worker(void)
1192 valid_until_timeout
.cb
= interface_ip_valid_until_handler
;
1193 uloop_timeout_set(&valid_until_timeout
, 1000);