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>
21 #include <netinet/in.h>
25 #include "interface.h"
26 #include "interface-ip.h"
46 static const struct blobmsg_policy route_attr
[__ROUTE_MAX
] = {
47 [ROUTE_INTERFACE
] = { .name
= "interface", .type
= BLOBMSG_TYPE_STRING
},
48 [ROUTE_TARGET
] = { .name
= "target", .type
= BLOBMSG_TYPE_STRING
},
49 [ROUTE_MASK
] = { .name
= "netmask", .type
= BLOBMSG_TYPE_STRING
},
50 [ROUTE_GATEWAY
] = { .name
= "gateway", .type
= BLOBMSG_TYPE_STRING
},
51 [ROUTE_METRIC
] = { .name
= "metric", .type
= BLOBMSG_TYPE_INT32
},
52 [ROUTE_MTU
] = { .name
= "mtu", .type
= BLOBMSG_TYPE_INT32
},
53 [ROUTE_TABLE
] = { .name
= "table", .type
= BLOBMSG_TYPE_STRING
},
54 [ROUTE_VALID
] = { .name
= "valid", .type
= BLOBMSG_TYPE_INT32
},
55 [ROUTE_SOURCE
] = { .name
= "source", .type
= BLOBMSG_TYPE_STRING
},
56 [ROUTE_ONLINK
] = { .name
= "onlink", .type
= BLOBMSG_TYPE_BOOL
},
57 [ROUTE_TYPE
] = { .name
= "type", .type
= BLOBMSG_TYPE_STRING
}
60 const struct uci_blob_param_list route_attr_list
= {
61 .n_params
= __ROUTE_MAX
,
66 struct list_head prefixes
= LIST_HEAD_INIT(prefixes
);
67 static struct device_prefix
*ula_prefix
= NULL
;
68 static struct uloop_timeout valid_until_timeout
;
72 clear_if_addr(union if_addr
*a
, int mask
)
74 int m_bytes
= (mask
+ 7) / 8;
75 uint8_t m_clear
= (1 << (m_bytes
* 8 - mask
)) - 1;
76 uint8_t *p
= (uint8_t *) a
;
78 if (m_bytes
< sizeof(*a
))
79 memset(p
+ m_bytes
, 0, sizeof(*a
) - m_bytes
);
81 p
[m_bytes
- 1] &= ~m_clear
;
85 match_if_addr(union if_addr
*a1
, union if_addr
*a2
, int mask
)
87 union if_addr
*p1
, *p2
;
89 p1
= alloca(sizeof(*a1
));
90 p2
= alloca(sizeof(*a2
));
92 memcpy(p1
, a1
, sizeof(*a1
));
93 clear_if_addr(p1
, mask
);
94 memcpy(p2
, a2
, sizeof(*a2
));
95 clear_if_addr(p2
, mask
);
97 return !memcmp(p1
, p2
, sizeof(*p1
));
100 static int set_ip_source_policy(bool add
, bool v6
, unsigned int priority
,
101 const union if_addr
*addr
, uint8_t mask
, unsigned int table
,
102 struct interface
*in_iface
, const char *action
)
104 struct iprule rule
= {
105 .flags
= IPRULE_PRIORITY
,
110 rule
.flags
|= IPRULE_SRC
;
111 rule
.src_addr
= *addr
;
112 rule
.src_mask
= mask
;
116 rule
.flags
|= IPRULE_LOOKUP
;
122 rule
.flags
|= IPRULE_ACTION
;
123 system_resolve_iprule_action(action
, &rule
.action
);
126 if (in_iface
&& in_iface
->l3_dev
.dev
) {
127 rule
.flags
|= IPRULE_IN
;
128 strcpy(rule
.in_dev
, in_iface
->l3_dev
.dev
->ifname
);
131 rule
.flags
|= (v6
) ? IPRULE_INET6
: IPRULE_INET4
;
133 return (add
) ? system_add_iprule(&rule
) : system_del_iprule(&rule
);
137 __find_ip_addr_target(struct interface_ip_settings
*ip
, union if_addr
*a
, bool v6
)
139 struct device_addr
*addr
;
141 vlist_for_each_element(&ip
->addr
, addr
, node
) {
145 if (v6
!= ((addr
->flags
& DEVADDR_FAMILY
) == DEVADDR_INET6
))
148 // Handle offlink addresses correctly
149 unsigned int mask
= addr
->mask
;
150 if ((addr
->flags
& DEVADDR_FAMILY
) == DEVADDR_INET6
&&
151 (addr
->flags
& DEVADDR_OFFLINK
))
154 if (!match_if_addr(&addr
->addr
, a
, mask
))
164 __find_ip_route_target(struct interface_ip_settings
*ip
, union if_addr
*a
,
165 bool v6
, struct device_route
**res
)
167 struct device_route
*route
;
169 vlist_for_each_element(&ip
->route
, route
, node
) {
173 if (v6
!= ((route
->flags
& DEVADDR_FAMILY
) == DEVADDR_INET6
))
176 if (!match_if_addr(&route
->addr
, a
, route
->mask
))
179 if (route
->flags
& DEVROUTE_TABLE
)
182 if (!*res
|| route
->mask
< (*res
)->mask
)
188 interface_ip_find_addr_target(struct interface
*iface
, union if_addr
*a
, bool v6
)
190 return __find_ip_addr_target(&iface
->proto_ip
, a
, v6
) ||
191 __find_ip_addr_target(&iface
->config_ip
, a
, v6
);
195 interface_ip_find_route_target(struct interface
*iface
, union if_addr
*a
,
196 bool v6
, struct device_route
**route
)
198 __find_ip_route_target(&iface
->proto_ip
, a
, v6
, route
);
199 __find_ip_route_target(&iface
->config_ip
, a
, v6
, route
);
203 interface_ip_add_target_route(union if_addr
*addr
, bool v6
, struct interface
*iface
)
205 struct device_route
*route
, *r_next
= NULL
;
206 bool defaultroute_target
= false;
207 int addrsize
= v6
? sizeof(addr
->in6
) : sizeof(addr
->in
);
209 route
= calloc(1, sizeof(*route
));
213 route
->flags
= v6
? DEVADDR_INET6
: DEVADDR_INET4
;
214 route
->mask
= v6
? 128 : 32;
215 if (memcmp(&route
->addr
, addr
, addrsize
) == 0)
216 defaultroute_target
= true;
218 memcpy(&route
->addr
, addr
, addrsize
);
221 /* look for locally addressable target first */
222 if (interface_ip_find_addr_target(iface
, addr
, v6
))
225 /* do not stop at the first route, let the lookup compare
226 * masks to find the best match */
227 interface_ip_find_route_target(iface
, addr
, v6
, &r_next
);
229 vlist_for_each_element(&interfaces
, iface
, node
) {
230 /* look for locally addressable target first */
231 if (interface_ip_find_addr_target(iface
, addr
, v6
))
234 /* do not stop at the first route, let the lookup compare
235 * masks to find the best match */
236 interface_ip_find_route_target(iface
, addr
, v6
, &r_next
);
245 iface
= r_next
->iface
;
246 memcpy(&route
->nexthop
, &r_next
->nexthop
, sizeof(route
->nexthop
));
247 route
->mtu
= r_next
->mtu
;
248 route
->metric
= r_next
->metric
;
249 route
->table
= r_next
->table
;
252 route
->iface
= iface
;
253 if (defaultroute_target
)
256 vlist_add(&iface
->host_routes
, &route
->node
, route
);
261 interface_ip_add_route(struct interface
*iface
, struct blob_attr
*attr
, bool v6
)
263 struct interface_ip_settings
*ip
;
264 struct blob_attr
*tb
[__ROUTE_MAX
], *cur
;
265 struct device_route
*route
;
266 int af
= v6
? AF_INET6
: AF_INET
;
267 bool is_proto_route
= !!iface
;
269 blobmsg_parse(route_attr
, __ROUTE_MAX
, tb
, blobmsg_data(attr
), blobmsg_data_len(attr
));
272 if ((cur
= tb
[ROUTE_INTERFACE
]) == NULL
)
275 iface
= vlist_find(&interfaces
, blobmsg_data(cur
), iface
, node
);
279 ip
= &iface
->config_ip
;
281 ip
= &iface
->proto_ip
;
284 route
= calloc(1, sizeof(*route
));
288 route
->flags
= v6
? DEVADDR_INET6
: DEVADDR_INET4
;
289 route
->mask
= v6
? 128 : 32;
290 if ((cur
= tb
[ROUTE_MASK
]) != NULL
) {
291 route
->mask
= parse_netmask_string(blobmsg_data(cur
), v6
);
292 if (route
->mask
> (v6
? 128 : 32))
296 if ((cur
= tb
[ROUTE_TARGET
]) != NULL
) {
297 if (!parse_ip_and_netmask(af
, blobmsg_data(cur
), &route
->addr
, &route
->mask
)) {
298 DPRINTF("Failed to parse route target: %s\n", (char *) blobmsg_data(cur
));
303 if ((cur
= tb
[ROUTE_GATEWAY
]) != NULL
) {
304 if (!inet_pton(af
, blobmsg_data(cur
), &route
->nexthop
)) {
305 DPRINTF("Failed to parse route gateway: %s\n", (char *) blobmsg_data(cur
));
310 if ((cur
= tb
[ROUTE_METRIC
]) != NULL
) {
311 route
->metric
= blobmsg_get_u32(cur
);
312 route
->flags
|= DEVROUTE_METRIC
;
314 route
->metric
= iface
->metric
;
316 if ((cur
= tb
[ROUTE_MTU
]) != NULL
) {
317 route
->mtu
= blobmsg_get_u32(cur
);
318 route
->flags
|= DEVROUTE_MTU
;
321 // Use source-based routing
322 if ((cur
= tb
[ROUTE_SOURCE
]) != NULL
) {
323 char *saveptr
, *source
= alloca(blobmsg_data_len(cur
));
324 memcpy(source
, blobmsg_data(cur
), blobmsg_data_len(cur
));
326 const char *addr
= strtok_r(source
, "/", &saveptr
);
327 const char *mask
= strtok_r(NULL
, "/", &saveptr
);
329 if (!addr
|| inet_pton(af
, addr
, &route
->source
) < 1) {
330 DPRINTF("Failed to parse route source: %s\n", addr
);
334 route
->sourcemask
= (mask
) ? atoi(mask
) : ((af
== AF_INET6
) ? 128 : 32);
337 if ((cur
= tb
[ROUTE_ONLINK
]) != NULL
&& blobmsg_get_bool(cur
))
338 route
->flags
|= DEVROUTE_ONLINK
;
340 if (is_proto_route
) {
341 route
->table
= (v6
) ? iface
->ip6table
: iface
->ip4table
;
342 route
->flags
|= DEVROUTE_SRCTABLE
;
345 if ((cur
= tb
[ROUTE_TABLE
]) != NULL
) {
346 if (!system_resolve_rt_table(blobmsg_data(cur
), &route
->table
)) {
347 DPRINTF("Failed to resolve routing table: %s\n", (char *) blobmsg_data(cur
));
351 /* only set the table flag if not using the main (default) table */
352 if (system_is_default_rt_table(route
->table
))
356 route
->flags
|= DEVROUTE_TABLE
;
359 if ((cur
= tb
[ROUTE_VALID
]) != NULL
) {
360 int64_t valid
= blobmsg_get_u32(cur
);
361 int64_t valid_until
= valid
+ (int64_t)system_get_rtime();
362 if (valid_until
<= LONG_MAX
&& valid
!= 0xffffffffLL
) // Catch overflow
363 route
->valid_until
= valid_until
;
366 if ((cur
= tb
[ROUTE_TYPE
]) != NULL
) {
367 if (!system_resolve_rt_type(blobmsg_data(cur
), &route
->type
)) {
368 DPRINTF("Failed to resolve routing type: %s\n", (char *) blobmsg_data(cur
));
371 route
->flags
|= DEVROUTE_TYPE
;
374 vlist_add(&ip
->route
, &route
->node
, route
);
382 addr_cmp(const void *k1
, const void *k2
, void *ptr
)
384 return memcmp(k1
, k2
, sizeof(struct device_addr
) -
385 offsetof(struct device_addr
, flags
));
389 route_cmp(const void *k1
, const void *k2
, void *ptr
)
391 const struct device_route
*r1
= k1
, *r2
= k2
;
393 if (r1
->mask
!= r2
->mask
)
394 return r2
->mask
- r1
->mask
;
396 if (r1
->metric
!= r2
->metric
)
397 return r1
->metric
- r2
->metric
;
399 if (r1
->flags
!= r2
->flags
)
400 return r2
->flags
- r1
->flags
;
402 if (r1
->sourcemask
!= r2
->sourcemask
)
403 return r1
->sourcemask
- r2
->sourcemask
;
405 if (r1
->table
!= r2
->table
)
406 return r1
->table
- r2
->table
;
408 int maskcmp
= memcmp(&r1
->source
, &r2
->source
, sizeof(r1
->source
));
412 return memcmp(&r1
->addr
, &r2
->addr
, sizeof(r1
->addr
));
416 prefix_cmp(const void *k1
, const void *k2
, void *ptr
)
418 return memcmp(k1
, k2
, offsetof(struct device_prefix
, pclass
) -
419 offsetof(struct device_prefix
, addr
));
423 interface_handle_subnet_route(struct interface
*iface
, struct device_addr
*addr
, bool add
)
425 struct device
*dev
= iface
->l3_dev
.dev
;
426 struct device_route route
;
428 if (addr
->flags
& DEVADDR_OFFLINK
)
431 memset(&route
, 0, sizeof(route
));
433 route
.flags
= addr
->flags
;
434 route
.mask
= addr
->mask
;
435 memcpy(&route
.addr
, &addr
->addr
, sizeof(route
.addr
));
436 clear_if_addr(&route
.addr
, route
.mask
);
439 route
.flags
|= DEVADDR_KERNEL
;
440 system_del_route(dev
, &route
);
442 route
.flags
&= ~DEVADDR_KERNEL
;
443 route
.metric
= iface
->metric
;
444 system_add_route(dev
, &route
);
446 system_del_route(dev
, &route
);
451 interface_update_proto_addr(struct vlist_tree
*tree
,
452 struct vlist_node
*node_new
,
453 struct vlist_node
*node_old
)
455 struct interface_ip_settings
*ip
;
456 struct interface
*iface
;
458 struct device_addr
*a_new
= NULL
, *a_old
= NULL
;
459 bool replace
= false;
463 ip
= container_of(tree
, struct interface_ip_settings
, addr
);
465 dev
= iface
->l3_dev
.dev
;
467 if (!node_new
|| !node_old
)
468 iface
->updated
|= IUF_ADDRESS
;
471 a_new
= container_of(node_new
, struct device_addr
, node
);
473 if ((a_new
->flags
& DEVADDR_FAMILY
) == DEVADDR_INET4
&&
477 uint32_t *a
= (uint32_t *) &a_new
->addr
;
479 mask
>>= a_new
->mask
;
480 a_new
->broadcast
= *a
| htonl(mask
);
485 a_old
= container_of(node_old
, struct device_addr
, node
);
487 if (a_new
&& a_old
) {
490 if (a_old
->flags
!= a_new
->flags
|| a_old
->failed
)
493 if (a_old
->valid_until
!= a_new
->valid_until
||
494 a_old
->preferred_until
!= a_new
->preferred_until
)
497 if ((a_new
->flags
& DEVADDR_FAMILY
) == DEVADDR_INET4
&&
498 a_new
->broadcast
!= a_old
->broadcast
)
503 if (!(a_old
->flags
& DEVADDR_EXTERNAL
) && a_old
->enabled
&& !keep
) {
504 interface_handle_subnet_route(iface
, a_old
, false);
506 if ((a_old
->flags
& DEVADDR_FAMILY
) == DEVADDR_INET6
)
509 unsigned int table
= (v6
) ? iface
->ip6table
: iface
->ip4table
;
511 //This is needed for source routing to work correctly. If a device
512 //has two connections to a network using the same subnet, adding
513 //only the network-rule will cause packets to be routed through the
514 //first matching network (source IP matches both masks).
516 set_ip_source_policy(false, v6
, IPRULE_PRIORITY_ADDR
, &a_old
->addr
,
517 (v6
) ? 128 : 32, table
, NULL
, NULL
);
518 set_ip_source_policy(false, v6
, IPRULE_PRIORITY_NW
, &a_old
->addr
,
519 a_old
->mask
, table
, NULL
, NULL
);
522 system_del_address(dev
, a_old
);
529 a_new
->enabled
= true;
530 if (!(a_new
->flags
& DEVADDR_EXTERNAL
) && (!keep
|| replace
)) {
531 if (system_add_address(dev
, a_new
))
532 a_new
->failed
= true;
535 if ((a_new
->flags
& DEVADDR_FAMILY
) == DEVADDR_INET6
)
538 unsigned int table
= (v6
) ? iface
->ip6table
: iface
->ip4table
;
541 set_ip_source_policy(true, v6
, IPRULE_PRIORITY_ADDR
, &a_new
->addr
,
542 (v6
) ? 128 : 32, table
, NULL
, NULL
);
543 set_ip_source_policy(true, v6
, IPRULE_PRIORITY_NW
, &a_new
->addr
,
544 a_new
->mask
, table
, NULL
, NULL
);
549 interface_handle_subnet_route(iface
, a_new
, true);
555 enable_route(struct interface_ip_settings
*ip
, struct device_route
*route
)
557 if (ip
->no_defaultroute
&& !route
->mask
)
564 interface_update_proto_route(struct vlist_tree
*tree
,
565 struct vlist_node
*node_new
,
566 struct vlist_node
*node_old
)
568 struct interface_ip_settings
*ip
;
569 struct interface
*iface
;
571 struct device_route
*route_old
, *route_new
;
574 ip
= container_of(tree
, struct interface_ip_settings
, route
);
576 dev
= iface
->l3_dev
.dev
;
578 if (!node_new
|| !node_old
)
579 iface
->updated
|= IUF_ROUTE
;
581 route_old
= container_of(node_old
, struct device_route
, node
);
582 route_new
= container_of(node_new
, struct device_route
, node
);
584 if (node_old
&& node_new
)
585 keep
= !memcmp(&route_old
->nexthop
, &route_new
->nexthop
, sizeof(route_old
->nexthop
)) &&
586 (route_old
->mtu
== route_new
->mtu
) && (route_old
->type
== route_new
->type
) &&
587 (route_old
->valid_until
== route_new
->valid_until
) && !route_old
->failed
;
590 if (!(route_old
->flags
& DEVADDR_EXTERNAL
) && route_old
->enabled
&& !keep
)
591 system_del_route(dev
, route_old
);
597 bool _enabled
= enable_route(ip
, route_new
);
599 if (!(route_new
->flags
& DEVADDR_EXTERNAL
) && !keep
&& _enabled
)
600 if (system_add_route(dev
, route_new
))
601 route_new
->failed
= true;
603 route_new
->iface
= iface
;
604 route_new
->enabled
= _enabled
;
609 interface_update_host_route(struct vlist_tree
*tree
,
610 struct vlist_node
*node_new
,
611 struct vlist_node
*node_old
)
613 struct interface
*iface
;
615 struct device_route
*route_old
, *route_new
;
617 iface
= container_of(tree
, struct interface
, host_routes
);
618 dev
= iface
->l3_dev
.dev
;
620 route_old
= container_of(node_old
, struct device_route
, node
);
621 route_new
= container_of(node_new
, struct device_route
, node
);
624 system_del_route(dev
, route_old
);
629 if (system_add_route(dev
, route_new
))
630 route_new
->failed
= true;
635 random_ifaceid(struct in6_addr
*addr
)
637 static bool initialized
= false;
642 gettimeofday(&t
, NULL
);
643 seed
= t
.tv_sec
^ t
.tv_usec
^ getpid();
647 addr
->s6_addr32
[2] = (uint32_t)mrand48();
648 addr
->s6_addr32
[3] = (uint32_t)mrand48();
652 eui64_ifaceid(struct interface
*iface
, struct in6_addr
*addr
)
654 /* get mac address */
655 uint8_t *macaddr
= iface
->l3_dev
.dev
->settings
.macaddr
;
656 uint8_t *ifaceid
= addr
->s6_addr
+ 8;
657 memcpy(ifaceid
,macaddr
,3);
658 memcpy(ifaceid
+ 5,macaddr
+ 3, 3);
665 generate_ifaceid(struct interface
*iface
, struct in6_addr
*addr
)
667 /* generate new iface id */
668 switch (iface
->assignment_iface_id_selection
) {
671 /* copy host part from assignment_fixed_iface_id */
672 memcpy(addr
->s6_addr
+ 8, iface
->assignment_fixed_iface_id
.s6_addr
+ 8, 8);
675 /* randomize last 64 bits */
676 random_ifaceid(addr
);
680 eui64_ifaceid(iface
, addr
);
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 struct device_route route
;
697 memset(&addr
, 0, sizeof(addr
));
698 memset(&route
, 0, sizeof(route
));
700 if (IN6_IS_ADDR_UNSPECIFIED(&assignment
->addr
)) {
701 addr
.addr
.in6
= prefix
->addr
;
702 addr
.addr
.in6
.s6_addr32
[1] |= htonl(assignment
->assigned
);
703 generate_ifaceid(iface
, &addr
.addr
.in6
);
704 assignment
->addr
= addr
.addr
.in6
;
707 addr
.addr
.in6
= assignment
->addr
;
709 addr
.mask
= assignment
->length
;
710 addr
.flags
= DEVADDR_INET6
| DEVADDR_OFFLINK
;
711 addr
.preferred_until
= prefix
->preferred_until
;
712 addr
.valid_until
= prefix
->valid_until
;
714 route
.flags
= DEVADDR_INET6
;
715 route
.mask
= addr
.mask
< 64 ? 64 : addr
.mask
;
716 route
.addr
= addr
.addr
;
717 clear_if_addr(&route
.addr
, route
.mask
);
719 if (!add
&& assignment
->enabled
) {
720 time_t now
= system_get_rtime();
721 addr
.preferred_until
= now
;
722 if (!addr
.valid_until
|| addr
.valid_until
- now
> 7200)
723 addr
.valid_until
= now
+ 7200;
726 if (prefix
->iface
->ip6table
)
727 set_ip_source_policy(false, true, IPRULE_PRIORITY_NW
, &addr
.addr
,
728 addr
.mask
, prefix
->iface
->ip6table
, iface
, NULL
);
730 set_ip_source_policy(false, true, IPRULE_PRIORITY_REJECT
, &addr
.addr
,
731 addr
.mask
, 0, iface
, "unreachable");
734 system_del_route(l3_downlink
, &route
);
735 system_add_address(l3_downlink
, &addr
);
737 assignment
->enabled
= false;
738 } else if (add
&& (iface
->state
== IFS_UP
|| iface
->state
== IFS_SETUP
) &&
739 !system_add_address(l3_downlink
, &addr
)) {
741 if (prefix
->iface
&& !assignment
->enabled
) {
742 set_ip_source_policy(true, true, IPRULE_PRIORITY_REJECT
, &addr
.addr
,
743 addr
.mask
, 0, iface
, "unreachable");
745 if (prefix
->iface
->ip6table
)
746 set_ip_source_policy(true, true, IPRULE_PRIORITY_NW
, &addr
.addr
,
747 addr
.mask
, prefix
->iface
->ip6table
, iface
, NULL
);
750 route
.metric
= iface
->metric
;
751 system_add_route(l3_downlink
, &route
);
753 if (uplink
&& uplink
->l3_dev
.dev
&& !(l3_downlink
->settings
.flags
& DEV_OPT_MTU6
)) {
754 int mtu
= system_update_ipv6_mtu(uplink
->l3_dev
.dev
, 0);
755 int mtu_old
= system_update_ipv6_mtu(l3_downlink
, 0);
757 if (mtu
> 0 && mtu_old
> mtu
)
758 system_update_ipv6_mtu(l3_downlink
, mtu
);
761 assignment
->enabled
= true;
765 static bool interface_prefix_assign(struct list_head
*list
,
766 struct device_prefix_assignment
*assign
)
768 int32_t current
= 0, asize
= (1 << (64 - assign
->length
)) - 1;
769 struct device_prefix_assignment
*c
;
770 list_for_each_entry(c
, list
, head
) {
771 if (assign
->assigned
!= -1) {
772 if (assign
->assigned
>= current
&& assign
->assigned
+ asize
< c
->assigned
) {
773 list_add_tail(&assign
->head
, &c
->head
);
776 } else if (assign
->assigned
== -1) {
777 current
= (current
+ asize
) & (~asize
);
778 if (current
+ asize
< c
->assigned
) {
779 assign
->assigned
= current
;
780 list_add_tail(&assign
->head
, &c
->head
);
784 current
= (c
->assigned
+ (1 << (64 - c
->length
)));
789 static void interface_update_prefix_assignments(struct device_prefix
*prefix
, bool setup
)
791 struct device_prefix_assignment
*c
;
792 struct interface
*iface
;
794 // Delete all assignments
795 while (!list_empty(&prefix
->assignments
)) {
796 c
= list_first_entry(&prefix
->assignments
,
797 struct device_prefix_assignment
, head
);
798 if ((iface
= vlist_find(&interfaces
, c
->name
, iface
, node
)))
799 interface_set_prefix_address(c
, prefix
, iface
, false);
807 // End-of-assignment sentinel
808 c
= malloc(sizeof(*c
) + 1);
809 c
->assigned
= 1 << (64 - prefix
->length
);
812 c
->addr
= in6addr_any
;
813 list_add(&c
->head
, &prefix
->assignments
);
816 if (prefix
->excl_length
> 0) {
817 const char name
[] = "!excluded";
818 c
= malloc(sizeof(*c
) + sizeof(name
));
819 c
->assigned
= ntohl(prefix
->excl_addr
.s6_addr32
[1]) &
820 ((1 << (64 - prefix
->length
)) - 1);
821 c
->length
= prefix
->excl_length
;
822 c
->addr
= in6addr_any
;
823 memcpy(c
->name
, name
, sizeof(name
));
824 list_add(&c
->head
, &prefix
->assignments
);
827 bool assigned_any
= false;
828 struct list_head assign_later
= LIST_HEAD_INIT(assign_later
);
829 vlist_for_each_element(&interfaces
, iface
, node
) {
830 if (iface
->assignment_length
< 48 ||
831 iface
->assignment_length
> 64)
834 // Test whether there is a matching class
835 if (!list_empty(&iface
->assignment_classes
)) {
838 struct interface_assignment_class
*c
;
839 list_for_each_entry(c
, &iface
->assignment_classes
, head
) {
840 if (!strcmp(c
->name
, prefix
->pclass
)) {
850 size_t namelen
= strlen(iface
->name
) + 1;
851 c
= malloc(sizeof(*c
) + namelen
);
852 c
->length
= iface
->assignment_length
;
853 c
->assigned
= iface
->assignment_hint
;
854 c
->addr
= in6addr_any
;
856 memcpy(c
->name
, iface
->name
, namelen
);
858 // First process all custom assignments, put all others in later-list
859 if (c
->assigned
== -1 || !interface_prefix_assign(&prefix
->assignments
, c
)) {
860 if (c
->assigned
!= -1) {
862 netifd_log_message(L_WARNING
, "Failed to assign requested subprefix "
863 "of size %hhu for %s, trying other\n", c
->length
, c
->name
);
866 struct list_head
*next
= &assign_later
;
867 struct device_prefix_assignment
*n
;
868 list_for_each_entry(n
, &assign_later
, head
) {
869 if (n
->length
< c
->length
) {
874 list_add_tail(&c
->head
, next
);
877 if (c
->assigned
!= -1)
881 // Then try to assign all other + failed custom assignments
882 while (!list_empty(&assign_later
)) {
883 c
= list_first_entry(&assign_later
, struct device_prefix_assignment
, head
);
886 bool assigned
= false;
888 assigned
= interface_prefix_assign(&prefix
->assignments
, c
);
889 } while (!assigned
&& ++c
->length
<= 64);
892 netifd_log_message(L_WARNING
, "Failed to assign subprefix "
893 "of size %hhu for %s\n", c
->length
, c
->name
);
900 list_for_each_entry(c
, &prefix
->assignments
, head
)
901 if ((iface
= vlist_find(&interfaces
, c
->name
, iface
, node
)))
902 interface_set_prefix_address(c
, prefix
, iface
, true);
905 netifd_log_message(L_WARNING
, "You have delegated IPv6-prefixes but haven't assigned them "
906 "to any interface. Did you forget to set option ip6assign on your lan-interfaces?");
910 void interface_refresh_assignments(bool hint
)
912 static bool refresh
= false;
913 if (!hint
&& refresh
) {
914 struct device_prefix
*p
;
915 list_for_each_entry(p
, &prefixes
, head
)
916 interface_update_prefix_assignments(p
, true);
923 interface_update_prefix(struct vlist_tree
*tree
,
924 struct vlist_node
*node_new
,
925 struct vlist_node
*node_old
)
927 struct device_prefix
*prefix_old
, *prefix_new
;
928 prefix_old
= container_of(node_old
, struct device_prefix
, node
);
929 prefix_new
= container_of(node_new
, struct device_prefix
, node
);
931 struct interface_ip_settings
*ip
= container_of(tree
, struct interface_ip_settings
, prefix
);
932 if (tree
&& (!node_new
|| !node_old
))
933 ip
->iface
->updated
|= IUF_PREFIX
;
935 struct device_route route
;
936 memset(&route
, 0, sizeof(route
));
937 route
.flags
= DEVADDR_INET6
;
938 route
.metric
= INT32_MAX
;
939 route
.mask
= (node_new
) ? prefix_new
->length
: prefix_old
->length
;
940 route
.addr
.in6
= (node_new
) ? prefix_new
->addr
: prefix_old
->addr
;
943 struct device_prefix_assignment
*c
;
944 struct interface
*iface
;
946 if (node_old
&& node_new
) {
947 // Move assignments and refresh addresses to update valid times
948 list_splice(&prefix_old
->assignments
, &prefix_new
->assignments
);
950 list_for_each_entry(c
, &prefix_new
->assignments
, head
)
951 if ((iface
= vlist_find(&interfaces
, c
->name
, iface
, node
)))
952 interface_set_prefix_address(c
, prefix_new
, iface
, true);
953 } else if (node_new
) {
954 // Set null-route to avoid routing loops
955 system_add_route(NULL
, &route
);
957 if (!prefix_new
->iface
|| !prefix_new
->iface
->proto_ip
.no_delegation
)
958 interface_update_prefix_assignments(prefix_new
, true);
959 } else if (node_old
) {
961 interface_update_prefix_assignments(prefix_old
, false);
962 system_del_route(NULL
, &route
);
966 if (prefix_old
->head
.next
)
967 list_del(&prefix_old
->head
);
971 if (node_new
&& (!prefix_new
->iface
|| !prefix_new
->iface
->proto_ip
.no_delegation
))
972 list_add(&prefix_new
->head
, &prefixes
);
976 struct device_prefix
*
977 interface_ip_add_device_prefix(struct interface
*iface
, struct in6_addr
*addr
,
978 uint8_t length
, time_t valid_until
, time_t preferred_until
,
979 struct in6_addr
*excl_addr
, uint8_t excl_length
, const char *pclass
)
982 pclass
= (iface
) ? iface
->name
: "local";
984 struct device_prefix
*prefix
= calloc(1, sizeof(*prefix
) + strlen(pclass
) + 1);
985 prefix
->length
= length
;
986 prefix
->addr
= *addr
;
987 prefix
->preferred_until
= preferred_until
;
988 prefix
->valid_until
= valid_until
;
989 prefix
->iface
= iface
;
990 INIT_LIST_HEAD(&prefix
->assignments
);
993 prefix
->excl_addr
= *excl_addr
;
994 prefix
->excl_length
= excl_length
;
997 strcpy(prefix
->pclass
, pclass
);
1000 vlist_add(&iface
->proto_ip
.prefix
, &prefix
->node
, &prefix
->addr
);
1002 interface_update_prefix(NULL
, &prefix
->node
, NULL
);
1008 interface_ip_set_ula_prefix(const char *prefix
)
1010 char buf
[INET6_ADDRSTRLEN
+ 4] = {0}, *saveptr
;
1012 strncpy(buf
, prefix
, sizeof(buf
) - 1);
1013 char *prefixaddr
= strtok_r(buf
, "/", &saveptr
);
1015 struct in6_addr addr
;
1016 if (!prefixaddr
|| inet_pton(AF_INET6
, prefixaddr
, &addr
) < 1) {
1018 interface_update_prefix(NULL
, NULL
, &ula_prefix
->node
);
1025 char *prefixlen
= strtok_r(NULL
, ",", &saveptr
);
1026 if (!prefixlen
|| (length
= atoi(prefixlen
)) < 1 || length
> 64)
1029 if (!ula_prefix
|| !IN6_ARE_ADDR_EQUAL(&addr
, &ula_prefix
->addr
) ||
1030 ula_prefix
->length
!= length
) {
1032 interface_update_prefix(NULL
, NULL
, &ula_prefix
->node
);
1034 ula_prefix
= interface_ip_add_device_prefix(NULL
, &addr
, length
,
1035 0, 0, NULL
, 0, NULL
);
1040 interface_add_dns_server(struct interface_ip_settings
*ip
, const char *str
)
1042 struct dns_server
*s
;
1044 s
= calloc(1, sizeof(*s
));
1049 if (inet_pton(s
->af
, str
, &s
->addr
.in
))
1053 if (inet_pton(s
->af
, str
, &s
->addr
.in
))
1060 D(INTERFACE
, "Add IPv%c DNS server: %s\n",
1061 s
->af
== AF_INET6
? '6' : '4', str
);
1062 vlist_simple_add(&ip
->dns_servers
, &s
->node
);
1066 interface_add_dns_server_list(struct interface_ip_settings
*ip
, struct blob_attr
*list
)
1068 struct blob_attr
*cur
;
1071 blobmsg_for_each_attr(cur
, list
, rem
) {
1072 if (blobmsg_type(cur
) != BLOBMSG_TYPE_STRING
)
1075 if (!blobmsg_check_attr(cur
, NULL
))
1078 interface_add_dns_server(ip
, blobmsg_data(cur
));
1083 interface_add_dns_search_domain(struct interface_ip_settings
*ip
, const char *str
)
1085 struct dns_search_domain
*s
;
1086 int len
= strlen(str
);
1088 s
= calloc(1, sizeof(*s
) + len
+ 1);
1092 D(INTERFACE
, "Add DNS search domain: %s\n", str
);
1093 memcpy(s
->name
, str
, len
);
1094 vlist_simple_add(&ip
->dns_search
, &s
->node
);
1098 interface_add_dns_search_list(struct interface_ip_settings
*ip
, struct blob_attr
*list
)
1100 struct blob_attr
*cur
;
1103 blobmsg_for_each_attr(cur
, list
, rem
) {
1104 if (blobmsg_type(cur
) != BLOBMSG_TYPE_STRING
)
1107 if (!blobmsg_check_attr(cur
, NULL
))
1110 interface_add_dns_search_domain(ip
, blobmsg_data(cur
));
1115 write_resolv_conf_entries(FILE *f
, struct interface_ip_settings
*ip
, const char *dev
)
1117 struct dns_server
*s
;
1118 struct dns_search_domain
*d
;
1120 char buf
[INET6_ADDRSTRLEN
];
1122 vlist_simple_for_each_element(&ip
->dns_servers
, s
, node
) {
1123 str
= inet_ntop(s
->af
, &s
->addr
, buf
, sizeof(buf
));
1127 if (s
->af
== AF_INET6
&& IN6_IS_ADDR_LINKLOCAL(&s
->addr
.in6
))
1128 fprintf(f
, "nameserver %s%%%s\n", str
, dev
);
1130 fprintf(f
, "nameserver %s\n", str
);
1133 vlist_simple_for_each_element(&ip
->dns_search
, d
, node
) {
1134 fprintf(f
, "search %s\n", d
->name
);
1139 interface_write_resolv_conf(void)
1141 struct interface
*iface
;
1142 char *path
= alloca(strlen(resolv_conf
) + 5);
1144 uint32_t crcold
, crcnew
;
1146 sprintf(path
, "%s.tmp", resolv_conf
);
1148 f
= fopen(path
, "w+");
1150 D(INTERFACE
, "Failed to open %s for writing\n", path
);
1154 vlist_for_each_element(&interfaces
, iface
, node
) {
1155 if (iface
->state
!= IFS_UP
)
1158 if (vlist_simple_empty(&iface
->proto_ip
.dns_search
) &&
1159 vlist_simple_empty(&iface
->proto_ip
.dns_servers
) &&
1160 vlist_simple_empty(&iface
->config_ip
.dns_search
) &&
1161 vlist_simple_empty(&iface
->config_ip
.dns_servers
))
1164 fprintf(f
, "# Interface %s\n", iface
->name
);
1165 write_resolv_conf_entries(f
, &iface
->config_ip
, iface
->ifname
);
1166 if (!iface
->proto_ip
.no_dns
)
1167 write_resolv_conf_entries(f
, &iface
->proto_ip
, iface
->ifname
);
1171 crcnew
= crc32_file(f
);
1174 crcold
= crcnew
+ 1;
1175 f
= fopen(resolv_conf
, "r");
1177 crcold
= crc32_file(f
);
1181 if (crcold
== crcnew
) {
1183 } else if (rename(path
, resolv_conf
) < 0) {
1184 D(INTERFACE
, "Failed to replace %s\n", resolv_conf
);
1189 void interface_ip_set_enabled(struct interface_ip_settings
*ip
, bool enabled
)
1191 struct device_addr
*addr
;
1192 struct device_route
*route
;
1194 struct interface
*iface
;
1196 ip
->enabled
= enabled
;
1198 dev
= iface
->l3_dev
.dev
;
1202 vlist_for_each_element(&ip
->addr
, addr
, node
) {
1203 if (addr
->enabled
== enabled
)
1207 system_add_address(dev
, addr
);
1209 interface_handle_subnet_route(iface
, addr
, true);
1211 interface_handle_subnet_route(iface
, addr
, false);
1212 system_del_address(dev
, addr
);
1214 addr
->enabled
= enabled
;
1217 vlist_for_each_element(&ip
->route
, route
, node
) {
1218 bool _enabled
= enabled
;
1220 if (!enable_route(ip
, route
))
1223 if (route
->enabled
== _enabled
)
1227 if (!(route
->flags
& DEVROUTE_METRIC
))
1228 route
->metric
= ip
->iface
->metric
;
1230 if (system_add_route(dev
, route
))
1231 route
->failed
= true;
1233 system_del_route(dev
, route
);
1234 route
->enabled
= _enabled
;
1237 struct device_prefix
*c
;
1238 struct device_prefix_assignment
*a
;
1239 list_for_each_entry(c
, &prefixes
, head
)
1240 list_for_each_entry(a
, &c
->assignments
, head
)
1241 if (!strcmp(a
->name
, ip
->iface
->name
))
1242 interface_set_prefix_address(a
, c
, ip
->iface
, enabled
);
1244 if (ip
->iface
&& ip
->iface
->l3_dev
.dev
)
1245 set_ip_source_policy(enabled
, true, IPRULE_PRIORITY_REJECT
+ ip
->iface
->l3_dev
.dev
->ifindex
,
1246 NULL
, 0, 0, ip
->iface
, "failed_policy");
1250 interface_ip_update_start(struct interface_ip_settings
*ip
)
1252 if (ip
!= &ip
->iface
->config_ip
) {
1253 vlist_simple_update(&ip
->dns_servers
);
1254 vlist_simple_update(&ip
->dns_search
);
1256 vlist_update(&ip
->route
);
1257 vlist_update(&ip
->addr
);
1258 vlist_update(&ip
->prefix
);
1262 interface_ip_update_complete(struct interface_ip_settings
*ip
)
1264 vlist_simple_flush(&ip
->dns_servers
);
1265 vlist_simple_flush(&ip
->dns_search
);
1266 vlist_flush(&ip
->route
);
1267 vlist_flush(&ip
->addr
);
1268 vlist_flush(&ip
->prefix
);
1269 interface_write_resolv_conf();
1273 interface_ip_flush(struct interface_ip_settings
*ip
)
1275 if (ip
== &ip
->iface
->proto_ip
)
1276 vlist_flush_all(&ip
->iface
->host_routes
);
1277 vlist_simple_flush_all(&ip
->dns_servers
);
1278 vlist_simple_flush_all(&ip
->dns_search
);
1279 vlist_flush_all(&ip
->route
);
1280 vlist_flush_all(&ip
->addr
);
1281 vlist_flush_all(&ip
->prefix
);
1285 __interface_ip_init(struct interface_ip_settings
*ip
, struct interface
*iface
)
1289 vlist_simple_init(&ip
->dns_search
, struct dns_search_domain
, node
);
1290 vlist_simple_init(&ip
->dns_servers
, struct dns_server
, node
);
1291 vlist_init(&ip
->route
, route_cmp
, interface_update_proto_route
);
1292 vlist_init(&ip
->addr
, addr_cmp
, interface_update_proto_addr
);
1293 vlist_init(&ip
->prefix
, prefix_cmp
, interface_update_prefix
);
1297 interface_ip_init(struct interface
*iface
)
1299 __interface_ip_init(&iface
->proto_ip
, iface
);
1300 __interface_ip_init(&iface
->config_ip
, iface
);
1301 vlist_init(&iface
->host_routes
, route_cmp
, interface_update_host_route
);
1305 interface_ip_valid_until_handler(struct uloop_timeout
*t
)
1307 time_t now
= system_get_rtime();
1308 struct interface
*iface
;
1309 vlist_for_each_element(&interfaces
, iface
, node
) {
1310 if (iface
->state
!= IFS_UP
)
1313 struct device_addr
*addr
, *addrp
;
1314 struct device_route
*route
, *routep
;
1315 struct device_prefix
*pref
, *prefp
;
1317 vlist_for_each_element_safe(&iface
->proto_ip
.addr
, addr
, node
, addrp
)
1318 if (addr
->valid_until
&& addr
->valid_until
< now
)
1319 vlist_delete(&iface
->proto_ip
.addr
, &addr
->node
);
1321 vlist_for_each_element_safe(&iface
->proto_ip
.route
, route
, node
, routep
)
1322 if (route
->valid_until
&& route
->valid_until
< now
)
1323 vlist_delete(&iface
->proto_ip
.route
, &route
->node
);
1325 vlist_for_each_element_safe(&iface
->proto_ip
.prefix
, pref
, node
, prefp
)
1326 if (pref
->valid_until
&& pref
->valid_until
< now
)
1327 vlist_delete(&iface
->proto_ip
.prefix
, &pref
->node
);
1331 uloop_timeout_set(t
, 1000);
1335 interface_ip_init_worker(void)
1337 valid_until_timeout
.cb
= interface_ip_valid_until_handler
;
1338 uloop_timeout_set(&valid_until_timeout
, 1000);