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"
47 static const struct blobmsg_policy route_attr
[__ROUTE_MAX
] = {
48 [ROUTE_INTERFACE
] = { .name
= "interface", .type
= BLOBMSG_TYPE_STRING
},
49 [ROUTE_TARGET
] = { .name
= "target", .type
= BLOBMSG_TYPE_STRING
},
50 [ROUTE_MASK
] = { .name
= "netmask", .type
= BLOBMSG_TYPE_STRING
},
51 [ROUTE_GATEWAY
] = { .name
= "gateway", .type
= BLOBMSG_TYPE_STRING
},
52 [ROUTE_METRIC
] = { .name
= "metric", .type
= BLOBMSG_TYPE_INT32
},
53 [ROUTE_MTU
] = { .name
= "mtu", .type
= BLOBMSG_TYPE_INT32
},
54 [ROUTE_TABLE
] = { .name
= "table", .type
= BLOBMSG_TYPE_STRING
},
55 [ROUTE_VALID
] = { .name
= "valid", .type
= BLOBMSG_TYPE_INT32
},
56 [ROUTE_SOURCE
] = { .name
= "source", .type
= BLOBMSG_TYPE_STRING
},
57 [ROUTE_ONLINK
] = { .name
= "onlink", .type
= BLOBMSG_TYPE_BOOL
},
58 [ROUTE_TYPE
] = { .name
= "type", .type
= BLOBMSG_TYPE_STRING
},
59 [ROUTE_PROTO
] = { .name
= "proto", .type
= BLOBMSG_TYPE_STRING
},
62 const struct uci_blob_param_list route_attr_list
= {
63 .n_params
= __ROUTE_MAX
,
68 struct list_head prefixes
= LIST_HEAD_INIT(prefixes
);
69 static struct device_prefix
*ula_prefix
= NULL
;
70 static struct uloop_timeout valid_until_timeout
;
74 clear_if_addr(union if_addr
*a
, int mask
)
76 int m_bytes
= (mask
+ 7) / 8;
77 uint8_t m_clear
= (1 << (m_bytes
* 8 - mask
)) - 1;
78 uint8_t *p
= (uint8_t *) a
;
80 if (m_bytes
< sizeof(*a
))
81 memset(p
+ m_bytes
, 0, sizeof(*a
) - m_bytes
);
83 p
[m_bytes
- 1] &= ~m_clear
;
87 match_if_addr(union if_addr
*a1
, union if_addr
*a2
, int mask
)
89 union if_addr
*p1
, *p2
;
91 p1
= alloca(sizeof(*a1
));
92 p2
= alloca(sizeof(*a2
));
94 memcpy(p1
, a1
, sizeof(*a1
));
95 clear_if_addr(p1
, mask
);
96 memcpy(p2
, a2
, sizeof(*a2
));
97 clear_if_addr(p2
, mask
);
99 return !memcmp(p1
, p2
, sizeof(*p1
));
102 static int set_ip_source_policy(bool add
, bool v6
, unsigned int priority
,
103 const union if_addr
*addr
, uint8_t mask
, unsigned int table
,
104 struct interface
*in_iface
, const char *action
, bool src
)
106 struct iprule rule
= {
107 .flags
= IPRULE_PRIORITY
,
113 rule
.flags
|= IPRULE_SRC
;
114 rule
.src_addr
= *addr
;
115 rule
.src_mask
= mask
;
117 rule
.flags
|= IPRULE_DEST
;
118 rule
.dest_addr
= *addr
;
119 rule
.dest_mask
= mask
;
124 rule
.flags
|= IPRULE_LOOKUP
;
130 rule
.flags
|= IPRULE_ACTION
;
131 system_resolve_iprule_action(action
, &rule
.action
);
134 if (in_iface
&& in_iface
->l3_dev
.dev
) {
135 rule
.flags
|= IPRULE_IN
;
136 strcpy(rule
.in_dev
, in_iface
->l3_dev
.dev
->ifname
);
139 rule
.flags
|= (v6
) ? IPRULE_INET6
: IPRULE_INET4
;
141 return (add
) ? system_add_iprule(&rule
) : system_del_iprule(&rule
);
144 static int set_ip_lo_policy(bool add
, bool v6
, struct interface
*iface
)
146 struct iprule rule
= {
147 .flags
= IPRULE_IN
| IPRULE_LOOKUP
| IPRULE_PRIORITY
,
148 .priority
= IPRULE_PRIORITY_NW
+ iface
->l3_dev
.dev
->ifindex
,
149 .lookup
= (v6
) ? iface
->ip6table
: iface
->ip4table
,
156 rule
.flags
|= (v6
) ? IPRULE_INET6
: IPRULE_INET4
;
158 return (add
) ? system_add_iprule(&rule
) : system_del_iprule(&rule
);
162 __find_ip_addr_target(struct interface_ip_settings
*ip
, union if_addr
*a
, bool v6
)
164 struct device_addr
*addr
;
166 vlist_for_each_element(&ip
->addr
, addr
, node
) {
170 if (v6
!= ((addr
->flags
& DEVADDR_FAMILY
) == DEVADDR_INET6
))
173 // Handle offlink addresses correctly
174 unsigned int mask
= addr
->mask
;
175 if ((addr
->flags
& DEVADDR_FAMILY
) == DEVADDR_INET6
&&
176 (addr
->flags
& DEVADDR_OFFLINK
))
179 if (!match_if_addr(&addr
->addr
, a
, mask
))
189 __find_ip_route_target(struct interface_ip_settings
*ip
, union if_addr
*a
,
190 bool v6
, struct device_route
**res
)
192 struct device_route
*route
;
194 vlist_for_each_element(&ip
->route
, route
, node
) {
198 if (v6
!= ((route
->flags
& DEVADDR_FAMILY
) == DEVADDR_INET6
))
201 if (!match_if_addr(&route
->addr
, a
, route
->mask
))
204 if (route
->flags
& DEVROUTE_TABLE
)
207 if (!*res
|| route
->mask
< (*res
)->mask
)
213 interface_ip_find_addr_target(struct interface
*iface
, union if_addr
*a
, bool v6
)
215 return __find_ip_addr_target(&iface
->proto_ip
, a
, v6
) ||
216 __find_ip_addr_target(&iface
->config_ip
, a
, v6
);
220 interface_ip_find_route_target(struct interface
*iface
, union if_addr
*a
,
221 bool v6
, struct device_route
**route
)
223 __find_ip_route_target(&iface
->proto_ip
, a
, v6
, route
);
224 __find_ip_route_target(&iface
->config_ip
, a
, v6
, route
);
228 interface_ip_add_target_route(union if_addr
*addr
, bool v6
, struct interface
*iface
)
230 struct device_route
*route
, *r_next
= NULL
;
231 bool defaultroute_target
= false;
232 int addrsize
= v6
? sizeof(addr
->in6
) : sizeof(addr
->in
);
234 route
= calloc(1, sizeof(*route
));
238 route
->flags
= v6
? DEVADDR_INET6
: DEVADDR_INET4
;
239 route
->mask
= v6
? 128 : 32;
240 if (memcmp(&route
->addr
, addr
, addrsize
) == 0)
241 defaultroute_target
= true;
243 memcpy(&route
->addr
, addr
, addrsize
);
246 /* look for locally addressable target first */
247 if (interface_ip_find_addr_target(iface
, addr
, v6
))
250 /* do not stop at the first route, let the lookup compare
251 * masks to find the best match */
252 interface_ip_find_route_target(iface
, addr
, v6
, &r_next
);
254 vlist_for_each_element(&interfaces
, iface
, node
) {
255 /* look for locally addressable target first */
256 if (interface_ip_find_addr_target(iface
, addr
, v6
))
259 /* do not stop at the first route, let the lookup compare
260 * masks to find the best match */
261 interface_ip_find_route_target(iface
, addr
, v6
, &r_next
);
270 iface
= r_next
->iface
;
271 memcpy(&route
->nexthop
, &r_next
->nexthop
, sizeof(route
->nexthop
));
272 route
->mtu
= r_next
->mtu
;
273 route
->metric
= r_next
->metric
;
274 route
->table
= r_next
->table
;
277 route
->iface
= iface
;
278 if (defaultroute_target
)
281 vlist_add(&iface
->host_routes
, &route
->node
, route
);
286 interface_set_route_info(struct interface
*iface
, struct device_route
*route
)
288 bool v6
= ((route
->flags
& DEVADDR_FAMILY
) == DEVADDR_INET6
);
293 if (!(route
->flags
& DEVROUTE_METRIC
))
294 route
->metric
= iface
->metric
;
296 if (!(route
->flags
& DEVROUTE_TABLE
)) {
297 route
->table
= (v6
) ? iface
->ip6table
: iface
->ip4table
;
299 route
->flags
|= DEVROUTE_SRCTABLE
;
304 interface_ip_add_route(struct interface
*iface
, struct blob_attr
*attr
, bool v6
)
306 struct interface_ip_settings
*ip
;
307 struct blob_attr
*tb
[__ROUTE_MAX
], *cur
;
308 struct device_route
*route
;
309 int af
= v6
? AF_INET6
: AF_INET
;
311 blobmsg_parse(route_attr
, __ROUTE_MAX
, tb
, blobmsg_data(attr
), blobmsg_data_len(attr
));
314 if ((cur
= tb
[ROUTE_INTERFACE
]) == NULL
)
317 iface
= vlist_find(&interfaces
, blobmsg_data(cur
), iface
, node
);
321 ip
= &iface
->config_ip
;
323 ip
= &iface
->proto_ip
;
326 route
= calloc(1, sizeof(*route
));
330 route
->flags
= v6
? DEVADDR_INET6
: DEVADDR_INET4
;
331 route
->mask
= v6
? 128 : 32;
332 if ((cur
= tb
[ROUTE_MASK
]) != NULL
) {
333 route
->mask
= parse_netmask_string(blobmsg_data(cur
), v6
);
334 if (route
->mask
> (v6
? 128 : 32))
338 if ((cur
= tb
[ROUTE_TARGET
]) != NULL
) {
339 if (!parse_ip_and_netmask(af
, blobmsg_data(cur
), &route
->addr
, &route
->mask
)) {
340 DPRINTF("Failed to parse route target: %s\n", (char *) blobmsg_data(cur
));
345 if ((cur
= tb
[ROUTE_GATEWAY
]) != NULL
) {
346 if (!inet_pton(af
, blobmsg_data(cur
), &route
->nexthop
)) {
347 DPRINTF("Failed to parse route gateway: %s\n", (char *) blobmsg_data(cur
));
352 if ((cur
= tb
[ROUTE_METRIC
]) != NULL
) {
353 route
->metric
= blobmsg_get_u32(cur
);
354 route
->flags
|= DEVROUTE_METRIC
;
357 if ((cur
= tb
[ROUTE_MTU
]) != NULL
) {
358 route
->mtu
= blobmsg_get_u32(cur
);
359 route
->flags
|= DEVROUTE_MTU
;
362 // Use source-based routing
363 if ((cur
= tb
[ROUTE_SOURCE
]) != NULL
) {
364 char *saveptr
, *source
= alloca(blobmsg_data_len(cur
));
365 memcpy(source
, blobmsg_data(cur
), blobmsg_data_len(cur
));
367 const char *addr
= strtok_r(source
, "/", &saveptr
);
368 const char *mask
= strtok_r(NULL
, "/", &saveptr
);
370 if (!addr
|| inet_pton(af
, addr
, &route
->source
) < 1) {
371 DPRINTF("Failed to parse route source: %s\n", addr
? addr
: "NULL");
375 route
->sourcemask
= (mask
) ? atoi(mask
) : ((af
== AF_INET6
) ? 128 : 32);
378 if ((cur
= tb
[ROUTE_ONLINK
]) != NULL
&& blobmsg_get_bool(cur
))
379 route
->flags
|= DEVROUTE_ONLINK
;
381 if ((cur
= tb
[ROUTE_TABLE
]) != NULL
) {
382 if (!system_resolve_rt_table(blobmsg_data(cur
), &route
->table
)) {
383 DPRINTF("Failed to resolve routing table: %s\n", (char *) blobmsg_data(cur
));
387 /* only set the table flag if not using the main (default) table */
388 if (system_is_default_rt_table(route
->table
))
392 route
->flags
|= DEVROUTE_TABLE
;
395 if ((cur
= tb
[ROUTE_VALID
]) != NULL
) {
396 int64_t valid
= blobmsg_get_u32(cur
);
397 int64_t valid_until
= valid
+ (int64_t)system_get_rtime();
398 if (valid_until
<= LONG_MAX
&& valid
!= 0xffffffffLL
) // Catch overflow
399 route
->valid_until
= valid_until
;
402 if ((cur
= tb
[ROUTE_TYPE
]) != NULL
) {
403 if (!system_resolve_rt_type(blobmsg_data(cur
), &route
->type
)) {
404 DPRINTF("Failed to resolve routing type: %s\n", (char *) blobmsg_data(cur
));
407 route
->flags
|= DEVROUTE_TYPE
;
410 if ((cur
= tb
[ROUTE_PROTO
]) != NULL
) {
411 if (!system_resolve_rt_proto(blobmsg_data(cur
), &route
->proto
)) {
412 DPRINTF("Failed to resolve proto type: %s\n", (char *) blobmsg_data(cur
));
415 route
->flags
|= DEVROUTE_PROTO
;
418 interface_set_route_info(iface
, route
);
419 vlist_add(&ip
->route
, &route
->node
, route
);
427 addr_cmp(const void *k1
, const void *k2
, void *ptr
)
429 return memcmp(k1
, k2
, sizeof(struct device_addr
) -
430 offsetof(struct device_addr
, flags
));
434 route_cmp(const void *k1
, const void *k2
, void *ptr
)
436 const struct device_route
*r1
= k1
, *r2
= k2
;
438 if (r1
->mask
!= r2
->mask
)
439 return r2
->mask
- r1
->mask
;
441 if (r1
->metric
!= r2
->metric
)
442 return r1
->metric
- r2
->metric
;
444 if (r1
->flags
!= r2
->flags
)
445 return r2
->flags
- r1
->flags
;
447 if (r1
->sourcemask
!= r2
->sourcemask
)
448 return r1
->sourcemask
- r2
->sourcemask
;
450 if (r1
->table
!= r2
->table
)
451 return r1
->table
- r2
->table
;
453 int maskcmp
= memcmp(&r1
->source
, &r2
->source
, sizeof(r1
->source
));
457 return memcmp(&r1
->addr
, &r2
->addr
, sizeof(r1
->addr
));
461 prefix_cmp(const void *k1
, const void *k2
, void *ptr
)
463 return memcmp(k1
, k2
, offsetof(struct device_prefix
, pclass
) -
464 offsetof(struct device_prefix
, addr
));
468 interface_handle_subnet_route(struct interface
*iface
, struct device_addr
*addr
, bool add
)
470 struct device
*dev
= iface
->l3_dev
.dev
;
471 struct device_route
*r
= &addr
->subnet
;
473 if (addr
->flags
& DEVADDR_OFFLINK
)
477 if (!addr
->subnet
.iface
)
480 system_del_route(dev
, r
);
481 memset(r
, 0, sizeof(*r
));
486 r
->flags
= addr
->flags
;
487 r
->mask
= addr
->mask
;
488 memcpy(&r
->addr
, &addr
->addr
, sizeof(r
->addr
));
489 clear_if_addr(&r
->addr
, r
->mask
);
491 if (!system_resolve_rt_proto("kernel", &r
->proto
))
494 r
->flags
|= DEVROUTE_PROTO
;
495 system_del_route(dev
, r
);
497 r
->flags
&= ~DEVROUTE_PROTO
;
498 interface_set_route_info(iface
, r
);
500 system_add_route(dev
, r
);
504 interface_add_addr_rules(struct device_addr
*addr
, bool enabled
)
506 bool v6
= (addr
->flags
& DEVADDR_FAMILY
) == DEVADDR_INET6
;
508 set_ip_source_policy(enabled
, v6
, IPRULE_PRIORITY_ADDR
, &addr
->addr
,
509 (v6
) ? 128 : 32, addr
->policy_table
, NULL
, NULL
,
511 set_ip_source_policy(enabled
, v6
, IPRULE_PRIORITY_ADDR_MASK
,
512 &addr
->addr
, addr
->mask
, addr
->policy_table
, NULL
,
517 interface_update_proto_addr(struct vlist_tree
*tree
,
518 struct vlist_node
*node_new
,
519 struct vlist_node
*node_old
)
521 struct interface_ip_settings
*ip
;
522 struct interface
*iface
;
524 struct device_addr
*a_new
= NULL
, *a_old
= NULL
;
525 bool replace
= false;
529 ip
= container_of(tree
, struct interface_ip_settings
, addr
);
531 dev
= iface
->l3_dev
.dev
;
533 if (!node_new
|| !node_old
)
534 iface
->updated
|= IUF_ADDRESS
;
537 a_new
= container_of(node_new
, struct device_addr
, node
);
539 if ((a_new
->flags
& DEVADDR_FAMILY
) == DEVADDR_INET4
&&
542 /* /31 and /32 addressing need 255.255.255.255
543 * as broadcast address. */
544 if (a_new
->mask
>= 31) {
545 a_new
->broadcast
= (uint32_t) ~0;
548 uint32_t *a
= (uint32_t *) &a_new
->addr
;
550 mask
>>= a_new
->mask
;
551 a_new
->broadcast
= *a
| htonl(mask
);
557 a_old
= container_of(node_old
, struct device_addr
, node
);
559 if (a_new
&& a_old
) {
562 if (a_old
->flags
!= a_new
->flags
|| a_old
->failed
)
565 if (a_old
->valid_until
!= a_new
->valid_until
||
566 a_old
->preferred_until
!= a_new
->preferred_until
)
569 if ((a_new
->flags
& DEVADDR_FAMILY
) == DEVADDR_INET4
&&
570 a_new
->broadcast
!= a_old
->broadcast
)
575 if (a_old
->enabled
&& !keep
) {
576 //This is needed for source routing to work correctly. If a device
577 //has two connections to a network using the same subnet, adding
578 //only the network-rule will cause packets to be routed through the
579 //first matching network (source IP matches both masks).
580 if (a_old
->policy_table
)
581 interface_add_addr_rules(a_old
, false);
583 if (!(a_old
->flags
& DEVADDR_EXTERNAL
)) {
584 interface_handle_subnet_route(iface
, a_old
, false);
585 system_del_address(dev
, a_old
);
593 a_new
->enabled
= true;
595 if ((a_new
->flags
& DEVADDR_FAMILY
) == DEVADDR_INET6
)
598 a_new
->policy_table
= (v6
) ? iface
->ip6table
: iface
->ip4table
;
600 if (!keep
|| replace
) {
601 if (!(a_new
->flags
& DEVADDR_EXTERNAL
)) {
602 if (system_add_address(dev
, a_new
))
603 a_new
->failed
= true;
605 if (iface
->metric
|| a_new
->policy_table
)
606 interface_handle_subnet_route(iface
, a_new
, true);
610 if (a_new
->policy_table
)
611 interface_add_addr_rules(a_new
, true);
618 enable_route(struct interface_ip_settings
*ip
, struct device_route
*route
)
620 if (ip
->no_defaultroute
&& !route
->mask
)
627 interface_update_proto_route(struct vlist_tree
*tree
,
628 struct vlist_node
*node_new
,
629 struct vlist_node
*node_old
)
631 struct interface_ip_settings
*ip
;
632 struct interface
*iface
;
634 struct device_route
*route_old
, *route_new
;
637 ip
= container_of(tree
, struct interface_ip_settings
, route
);
639 dev
= iface
->l3_dev
.dev
;
641 if (!node_new
|| !node_old
)
642 iface
->updated
|= IUF_ROUTE
;
644 route_old
= container_of(node_old
, struct device_route
, node
);
645 route_new
= container_of(node_new
, struct device_route
, node
);
647 if (node_old
&& node_new
)
648 keep
= !memcmp(&route_old
->nexthop
, &route_new
->nexthop
, sizeof(route_old
->nexthop
)) &&
649 (route_old
->mtu
== route_new
->mtu
) && (route_old
->type
== route_new
->type
) &&
650 (route_old
->proto
== route_new
->proto
) && !route_old
->failed
;
653 if (!(route_old
->flags
& DEVADDR_EXTERNAL
) && route_old
->enabled
&& !keep
)
654 system_del_route(dev
, route_old
);
660 bool _enabled
= enable_route(ip
, route_new
);
662 if (!(route_new
->flags
& DEVADDR_EXTERNAL
) && !keep
&& _enabled
)
663 if (system_add_route(dev
, route_new
))
664 route_new
->failed
= true;
666 route_new
->iface
= iface
;
667 route_new
->enabled
= _enabled
;
672 interface_update_host_route(struct vlist_tree
*tree
,
673 struct vlist_node
*node_new
,
674 struct vlist_node
*node_old
)
676 struct interface
*iface
;
678 struct device_route
*route_old
, *route_new
;
680 iface
= container_of(tree
, struct interface
, host_routes
);
681 dev
= iface
->l3_dev
.dev
;
683 route_old
= container_of(node_old
, struct device_route
, node
);
684 route_new
= container_of(node_new
, struct device_route
, node
);
687 system_del_route(dev
, route_old
);
692 if (system_add_route(dev
, route_new
))
693 route_new
->failed
= true;
698 random_ifaceid(struct in6_addr
*addr
)
700 static bool initialized
= false;
705 gettimeofday(&t
, NULL
);
706 seed
= t
.tv_sec
^ t
.tv_usec
^ getpid();
710 addr
->s6_addr32
[2] = (uint32_t)mrand48();
711 addr
->s6_addr32
[3] = (uint32_t)mrand48();
715 eui64_ifaceid(struct interface
*iface
, struct in6_addr
*addr
)
717 /* get mac address */
718 uint8_t *macaddr
= iface
->l3_dev
.dev
->settings
.macaddr
;
719 uint8_t *ifaceid
= addr
->s6_addr
+ 8;
720 memcpy(ifaceid
,macaddr
,3);
721 memcpy(ifaceid
+ 5,macaddr
+ 3, 3);
728 generate_ifaceid(struct interface
*iface
, struct in6_addr
*addr
)
730 /* generate new iface id */
731 switch (iface
->assignment_iface_id_selection
) {
734 /* copy host part from assignment_fixed_iface_id */
735 memcpy(addr
->s6_addr
+ 8, iface
->assignment_fixed_iface_id
.s6_addr
+ 8, 8);
738 /* randomize last 64 bits */
739 random_ifaceid(addr
);
743 eui64_ifaceid(iface
, addr
);
749 interface_set_prefix_address(struct device_prefix_assignment
*assignment
,
750 const struct device_prefix
*prefix
, struct interface
*iface
, bool add
)
752 const struct interface
*uplink
= prefix
->iface
;
753 if (!iface
->l3_dev
.dev
)
756 struct device
*l3_downlink
= iface
->l3_dev
.dev
;
758 struct device_addr addr
;
759 struct device_route route
;
760 memset(&addr
, 0, sizeof(addr
));
761 memset(&route
, 0, sizeof(route
));
763 if (IN6_IS_ADDR_UNSPECIFIED(&assignment
->addr
)) {
764 addr
.addr
.in6
= prefix
->addr
;
765 addr
.addr
.in6
.s6_addr32
[1] |= htonl(assignment
->assigned
);
766 generate_ifaceid(iface
, &addr
.addr
.in6
);
767 assignment
->addr
= addr
.addr
.in6
;
770 addr
.addr
.in6
= assignment
->addr
;
772 addr
.mask
= assignment
->length
;
773 addr
.flags
= DEVADDR_INET6
| DEVADDR_OFFLINK
;
774 addr
.preferred_until
= prefix
->preferred_until
;
775 addr
.valid_until
= prefix
->valid_until
;
777 route
.flags
= DEVADDR_INET6
;
778 route
.mask
= addr
.mask
< 64 ? 64 : addr
.mask
;
779 route
.addr
= addr
.addr
;
780 clear_if_addr(&route
.addr
, route
.mask
);
781 interface_set_route_info(iface
, &route
);
783 if (!add
&& assignment
->enabled
) {
784 time_t now
= system_get_rtime();
785 addr
.preferred_until
= now
;
786 if (!addr
.valid_until
|| addr
.valid_until
- now
> 7200)
787 addr
.valid_until
= now
+ 7200;
790 if (prefix
->iface
->ip6table
)
791 set_ip_source_policy(false, true, IPRULE_PRIORITY_NW
, &addr
.addr
,
792 addr
.mask
, prefix
->iface
->ip6table
, iface
, NULL
, true);
794 set_ip_source_policy(false, true, IPRULE_PRIORITY_REJECT
, &addr
.addr
,
795 addr
.mask
, 0, iface
, "unreachable", true);
798 system_del_route(l3_downlink
, &route
);
799 system_add_address(l3_downlink
, &addr
);
801 assignment
->enabled
= false;
802 } else if (add
&& (iface
->state
== IFS_UP
|| iface
->state
== IFS_SETUP
) &&
803 !system_add_address(l3_downlink
, &addr
)) {
805 if (prefix
->iface
&& !assignment
->enabled
) {
806 set_ip_source_policy(true, true, IPRULE_PRIORITY_REJECT
, &addr
.addr
,
807 addr
.mask
, 0, iface
, "unreachable", true);
809 if (prefix
->iface
->ip6table
)
810 set_ip_source_policy(true, true, IPRULE_PRIORITY_NW
, &addr
.addr
,
811 addr
.mask
, prefix
->iface
->ip6table
, iface
, NULL
, true);
814 route
.metric
= iface
->metric
;
815 system_add_route(l3_downlink
, &route
);
817 if (uplink
&& uplink
->l3_dev
.dev
&& !(l3_downlink
->settings
.flags
& DEV_OPT_MTU6
)) {
818 int mtu
= system_update_ipv6_mtu(uplink
->l3_dev
.dev
, 0);
819 int mtu_old
= system_update_ipv6_mtu(l3_downlink
, 0);
821 if (mtu
> 0 && mtu_old
> mtu
)
822 system_update_ipv6_mtu(l3_downlink
, mtu
);
825 assignment
->enabled
= true;
829 static bool interface_prefix_assign(struct list_head
*list
,
830 struct device_prefix_assignment
*assign
)
832 int32_t current
= 0, asize
= (1 << (64 - assign
->length
)) - 1;
833 struct device_prefix_assignment
*c
;
834 list_for_each_entry(c
, list
, head
) {
835 if (assign
->assigned
!= -1) {
836 if (assign
->assigned
>= current
&& assign
->assigned
+ asize
< c
->assigned
) {
837 list_add_tail(&assign
->head
, &c
->head
);
840 } else if (assign
->assigned
== -1) {
841 current
= (current
+ asize
) & (~asize
);
842 if (current
+ asize
< c
->assigned
) {
843 assign
->assigned
= current
;
844 list_add_tail(&assign
->head
, &c
->head
);
848 current
= (c
->assigned
+ (1 << (64 - c
->length
)));
854 * Sorting of assignment entries:
855 * Primary on assignment length: smallest assignment first
856 * Secondary on assignment weight: highest weight first
857 * Finally alphabetical order of interface names
859 static int prefix_assignment_cmp(const void *k1
, const void *k2
, void *ptr
)
861 const struct device_prefix_assignment
*a1
= k1
, *a2
= k2
;
863 if (a1
->length
!= a2
->length
)
864 return a1
->length
- a2
->length
;
866 if (a1
->weight
!= a2
->weight
)
867 return a2
->weight
- a1
->weight
;
869 return strcmp(a1
->name
, a2
->name
);
872 static void interface_update_prefix_assignments(struct device_prefix
*prefix
, bool setup
)
874 struct device_prefix_assignment
*c
;
875 struct interface
*iface
;
877 // Delete all assignments
878 while (!list_empty(&prefix
->assignments
)) {
879 c
= list_first_entry(&prefix
->assignments
,
880 struct device_prefix_assignment
, head
);
881 if ((iface
= vlist_find(&interfaces
, c
->name
, iface
, node
)))
882 interface_set_prefix_address(c
, prefix
, iface
, false);
890 // End-of-assignment sentinel
891 c
= malloc(sizeof(*c
) + 1);
895 c
->assigned
= 1 << (64 - prefix
->length
);
898 c
->addr
= in6addr_any
;
899 list_add(&c
->head
, &prefix
->assignments
);
902 if (prefix
->excl_length
> 0) {
903 const char name
[] = "!excluded";
904 c
= malloc(sizeof(*c
) + sizeof(name
));
906 c
->assigned
= ntohl(prefix
->excl_addr
.s6_addr32
[1]) &
907 ((1 << (64 - prefix
->length
)) - 1);
908 c
->length
= prefix
->excl_length
;
909 c
->addr
= in6addr_any
;
910 memcpy(c
->name
, name
, sizeof(name
));
911 list_add(&c
->head
, &prefix
->assignments
);
915 bool assigned_any
= false;
917 struct avl_node node
;
919 struct avl_tree assign_later
;
921 avl_init(&assign_later
, prefix_assignment_cmp
, false, NULL
);
923 vlist_for_each_element(&interfaces
, iface
, node
) {
924 if (iface
->assignment_length
< 48 ||
925 iface
->assignment_length
> 64)
928 // Test whether there is a matching class
929 if (!list_empty(&iface
->assignment_classes
)) {
932 struct interface_assignment_class
*c
;
933 list_for_each_entry(c
, &iface
->assignment_classes
, head
) {
934 if (!strcmp(c
->name
, prefix
->pclass
)) {
944 size_t namelen
= strlen(iface
->name
) + 1;
945 c
= malloc(sizeof(*c
) + namelen
);
949 c
->length
= iface
->assignment_length
;
950 c
->assigned
= iface
->assignment_hint
;
951 c
->weight
= iface
->assignment_weight
;
952 c
->addr
= in6addr_any
;
954 memcpy(c
->name
, iface
->name
, namelen
);
956 // First process all custom assignments, put all others in later-list
957 if (c
->assigned
== -1 || !interface_prefix_assign(&prefix
->assignments
, c
)) {
958 if (c
->assigned
!= -1) {
960 netifd_log_message(L_WARNING
, "Failed to assign requested subprefix "
961 "of size %hhu for %s, trying other\n", c
->length
, c
->name
);
964 entry
= calloc(1, sizeof(*entry
));
969 avl_insert(&assign_later
, &entry
->node
);
972 if (c
->assigned
!= -1)
976 /* Then try to assign all other + failed custom assignments */
977 avl_for_each_element_safe(&assign_later
, entry
, node
, n_entry
) {
978 bool assigned
= false;
980 c
= (struct device_prefix_assignment
*)entry
->node
.key
;
981 avl_delete(&assign_later
, &entry
->node
);
984 assigned
= interface_prefix_assign(&prefix
->assignments
, c
);
985 } while (!assigned
&& ++c
->length
<= 64);
988 netifd_log_message(L_WARNING
, "Failed to assign subprefix "
989 "of size %hhu for %s\n", c
->length
, c
->name
);
997 list_for_each_entry(c
, &prefix
->assignments
, head
)
998 if ((iface
= vlist_find(&interfaces
, c
->name
, iface
, node
)))
999 interface_set_prefix_address(c
, prefix
, iface
, true);
1002 netifd_log_message(L_WARNING
, "You have delegated IPv6-prefixes but haven't assigned them "
1003 "to any interface. Did you forget to set option ip6assign on your lan-interfaces?");
1007 void interface_refresh_assignments(bool hint
)
1009 static bool refresh
= false;
1010 if (!hint
&& refresh
) {
1011 struct device_prefix
*p
;
1012 list_for_each_entry(p
, &prefixes
, head
)
1013 interface_update_prefix_assignments(p
, true);
1020 interface_update_prefix(struct vlist_tree
*tree
,
1021 struct vlist_node
*node_new
,
1022 struct vlist_node
*node_old
)
1024 struct device_prefix
*prefix_old
, *prefix_new
;
1025 prefix_old
= container_of(node_old
, struct device_prefix
, node
);
1026 prefix_new
= container_of(node_new
, struct device_prefix
, node
);
1028 struct interface_ip_settings
*ip
= container_of(tree
, struct interface_ip_settings
, prefix
);
1029 if (tree
&& (!node_new
|| !node_old
))
1030 ip
->iface
->updated
|= IUF_PREFIX
;
1032 struct device_route route
;
1033 memset(&route
, 0, sizeof(route
));
1034 route
.flags
= DEVADDR_INET6
;
1035 route
.metric
= INT32_MAX
;
1036 route
.mask
= (node_new
) ? prefix_new
->length
: prefix_old
->length
;
1037 route
.addr
.in6
= (node_new
) ? prefix_new
->addr
: prefix_old
->addr
;
1040 struct device_prefix_assignment
*c
;
1041 struct interface
*iface
;
1043 if (node_old
&& node_new
) {
1044 // Move assignments and refresh addresses to update valid times
1045 list_splice(&prefix_old
->assignments
, &prefix_new
->assignments
);
1047 list_for_each_entry(c
, &prefix_new
->assignments
, head
)
1048 if ((iface
= vlist_find(&interfaces
, c
->name
, iface
, node
)))
1049 interface_set_prefix_address(c
, prefix_new
, iface
, true);
1051 if (prefix_new
->preferred_until
!= prefix_old
->preferred_until
||
1052 prefix_new
->valid_until
!= prefix_old
->valid_until
)
1053 ip
->iface
->updated
|= IUF_PREFIX
;
1054 } else if (node_new
) {
1055 // Set null-route to avoid routing loops
1056 system_add_route(NULL
, &route
);
1058 if (!prefix_new
->iface
|| !prefix_new
->iface
->proto_ip
.no_delegation
)
1059 interface_update_prefix_assignments(prefix_new
, true);
1060 } else if (node_old
) {
1061 // Remove null-route
1062 interface_update_prefix_assignments(prefix_old
, false);
1063 system_del_route(NULL
, &route
);
1067 if (prefix_old
->head
.next
)
1068 list_del(&prefix_old
->head
);
1072 if (node_new
&& (!prefix_new
->iface
|| !prefix_new
->iface
->proto_ip
.no_delegation
))
1073 list_add(&prefix_new
->head
, &prefixes
);
1077 struct device_prefix
*
1078 interface_ip_add_device_prefix(struct interface
*iface
, struct in6_addr
*addr
,
1079 uint8_t length
, time_t valid_until
, time_t preferred_until
,
1080 struct in6_addr
*excl_addr
, uint8_t excl_length
, const char *pclass
)
1083 pclass
= (iface
) ? iface
->name
: "local";
1085 struct device_prefix
*prefix
= calloc(1, sizeof(*prefix
) + strlen(pclass
) + 1);
1089 prefix
->length
= length
;
1090 prefix
->addr
= *addr
;
1091 prefix
->preferred_until
= preferred_until
;
1092 prefix
->valid_until
= valid_until
;
1093 prefix
->iface
= iface
;
1094 INIT_LIST_HEAD(&prefix
->assignments
);
1097 prefix
->excl_addr
= *excl_addr
;
1098 prefix
->excl_length
= excl_length
;
1101 strcpy(prefix
->pclass
, pclass
);
1104 vlist_add(&iface
->proto_ip
.prefix
, &prefix
->node
, &prefix
->addr
);
1106 interface_update_prefix(NULL
, &prefix
->node
, NULL
);
1112 interface_ip_set_ula_prefix(const char *prefix
)
1114 char buf
[INET6_ADDRSTRLEN
+ 4] = {0}, *saveptr
;
1116 strncpy(buf
, prefix
, sizeof(buf
) - 1);
1117 char *prefixaddr
= strtok_r(buf
, "/", &saveptr
);
1119 struct in6_addr addr
;
1120 if (!prefixaddr
|| inet_pton(AF_INET6
, prefixaddr
, &addr
) < 1) {
1122 interface_update_prefix(NULL
, NULL
, &ula_prefix
->node
);
1129 char *prefixlen
= strtok_r(NULL
, ",", &saveptr
);
1130 if (!prefixlen
|| (length
= atoi(prefixlen
)) < 1 || length
> 64)
1133 if (!ula_prefix
|| !IN6_ARE_ADDR_EQUAL(&addr
, &ula_prefix
->addr
) ||
1134 ula_prefix
->length
!= length
) {
1136 interface_update_prefix(NULL
, NULL
, &ula_prefix
->node
);
1138 ula_prefix
= interface_ip_add_device_prefix(NULL
, &addr
, length
,
1139 0, 0, NULL
, 0, NULL
);
1144 interface_add_dns_server(struct interface_ip_settings
*ip
, const char *str
)
1146 struct dns_server
*s
;
1148 s
= calloc(1, sizeof(*s
));
1153 if (inet_pton(s
->af
, str
, &s
->addr
.in
))
1157 if (inet_pton(s
->af
, str
, &s
->addr
.in
))
1164 D(INTERFACE
, "Add IPv%c DNS server: %s\n",
1165 s
->af
== AF_INET6
? '6' : '4', str
);
1166 vlist_simple_add(&ip
->dns_servers
, &s
->node
);
1170 interface_add_dns_server_list(struct interface_ip_settings
*ip
, struct blob_attr
*list
)
1172 struct blob_attr
*cur
;
1175 blobmsg_for_each_attr(cur
, list
, rem
) {
1176 if (blobmsg_type(cur
) != BLOBMSG_TYPE_STRING
)
1179 if (!blobmsg_check_attr(cur
, NULL
))
1182 interface_add_dns_server(ip
, blobmsg_data(cur
));
1187 interface_add_dns_search_domain(struct interface_ip_settings
*ip
, const char *str
)
1189 struct dns_search_domain
*s
;
1190 int len
= strlen(str
);
1192 s
= calloc(1, sizeof(*s
) + len
+ 1);
1196 D(INTERFACE
, "Add DNS search domain: %s\n", str
);
1197 memcpy(s
->name
, str
, len
);
1198 vlist_simple_add(&ip
->dns_search
, &s
->node
);
1202 interface_add_dns_search_list(struct interface_ip_settings
*ip
, struct blob_attr
*list
)
1204 struct blob_attr
*cur
;
1207 blobmsg_for_each_attr(cur
, list
, rem
) {
1208 if (blobmsg_type(cur
) != BLOBMSG_TYPE_STRING
)
1211 if (!blobmsg_check_attr(cur
, NULL
))
1214 interface_add_dns_search_domain(ip
, blobmsg_data(cur
));
1219 write_resolv_conf_entries(FILE *f
, struct interface_ip_settings
*ip
, const char *dev
)
1221 struct dns_server
*s
;
1222 struct dns_search_domain
*d
;
1224 char buf
[INET6_ADDRSTRLEN
];
1226 vlist_simple_for_each_element(&ip
->dns_servers
, s
, node
) {
1227 str
= inet_ntop(s
->af
, &s
->addr
, buf
, sizeof(buf
));
1231 if (s
->af
== AF_INET6
&& IN6_IS_ADDR_LINKLOCAL(&s
->addr
.in6
))
1232 fprintf(f
, "nameserver %s%%%s\n", str
, dev
);
1234 fprintf(f
, "nameserver %s\n", str
);
1237 vlist_simple_for_each_element(&ip
->dns_search
, d
, node
) {
1238 fprintf(f
, "search %s\n", d
->name
);
1242 /* Sorting of interface resolver entries : */
1243 /* Primary on interface dns_metric : lowest metric first */
1244 /* Secondary on interface metric : lowest metric first */
1245 /* Finally alphabetical order of interface names */
1246 static int resolv_conf_iface_cmp(const void *k1
, const void *k2
, void *ptr
)
1248 const struct interface
*iface1
= k1
, *iface2
= k2
;
1250 if (iface1
->dns_metric
!= iface2
->dns_metric
)
1251 return iface1
->dns_metric
- iface2
->dns_metric
;
1253 if (iface1
->metric
!= iface2
->metric
)
1254 return iface1
->metric
- iface2
->metric
;
1256 return strcmp(iface1
->name
, iface2
->name
);
1260 __interface_write_dns_entries(FILE *f
)
1262 struct interface
*iface
;
1264 struct avl_node node
;
1266 struct avl_tree resolv_conf_iface_entries
;
1268 avl_init(&resolv_conf_iface_entries
, resolv_conf_iface_cmp
, false, NULL
);
1270 vlist_for_each_element(&interfaces
, iface
, node
) {
1271 if (iface
->state
!= IFS_UP
)
1274 if (vlist_simple_empty(&iface
->proto_ip
.dns_search
) &&
1275 vlist_simple_empty(&iface
->proto_ip
.dns_servers
) &&
1276 vlist_simple_empty(&iface
->config_ip
.dns_search
) &&
1277 vlist_simple_empty(&iface
->config_ip
.dns_servers
))
1280 entry
= calloc(1, sizeof(*entry
));
1284 entry
->node
.key
= iface
;
1285 avl_insert(&resolv_conf_iface_entries
, &entry
->node
);
1288 avl_for_each_element(&resolv_conf_iface_entries
, entry
, node
) {
1289 iface
= (struct interface
*)entry
->node
.key
;
1290 struct device
*dev
= iface
->l3_dev
.dev
;
1292 fprintf(f
, "# Interface %s\n", iface
->name
);
1294 write_resolv_conf_entries(f
, &iface
->config_ip
, dev
->ifname
);
1296 if (!iface
->proto_ip
.no_dns
)
1297 write_resolv_conf_entries(f
, &iface
->proto_ip
, dev
->ifname
);
1300 avl_remove_all_elements(&resolv_conf_iface_entries
, entry
, node
, n_entry
)
1305 interface_write_resolv_conf(void)
1307 char *path
= alloca(strlen(resolv_conf
) + 5);
1309 uint32_t crcold
, crcnew
;
1311 sprintf(path
, "%s.tmp", resolv_conf
);
1313 f
= fopen(path
, "w+");
1315 D(INTERFACE
, "Failed to open %s for writing\n", path
);
1319 __interface_write_dns_entries(f
);
1323 crcnew
= crc32_file(f
);
1326 crcold
= crcnew
+ 1;
1327 f
= fopen(resolv_conf
, "r");
1329 crcold
= crc32_file(f
);
1333 if (crcold
== crcnew
) {
1335 } else if (rename(path
, resolv_conf
) < 0) {
1336 D(INTERFACE
, "Failed to replace %s\n", resolv_conf
);
1341 void interface_ip_set_enabled(struct interface_ip_settings
*ip
, bool enabled
)
1343 struct device_addr
*addr
;
1344 struct device_route
*route
;
1346 struct interface
*iface
;
1348 ip
->enabled
= enabled
;
1350 dev
= iface
->l3_dev
.dev
;
1354 vlist_for_each_element(&ip
->addr
, addr
, node
) {
1355 bool v6
= ((addr
->flags
& DEVADDR_FAMILY
) == DEVADDR_INET6
) ? true : false;
1357 if (addr
->flags
& DEVADDR_EXTERNAL
)
1360 if (addr
->enabled
== enabled
)
1364 system_add_address(dev
, addr
);
1366 addr
->policy_table
= (v6
) ? iface
->ip6table
: iface
->ip4table
;
1367 if (iface
->metric
|| addr
->policy_table
)
1368 interface_handle_subnet_route(iface
, addr
, true);
1370 if (addr
->policy_table
)
1371 interface_add_addr_rules(addr
, true);
1373 interface_handle_subnet_route(iface
, addr
, false);
1374 system_del_address(dev
, addr
);
1376 if (addr
->policy_table
)
1377 interface_add_addr_rules(addr
, false);
1379 addr
->enabled
= enabled
;
1382 vlist_for_each_element(&ip
->route
, route
, node
) {
1383 bool _enabled
= enabled
;
1385 if (route
->flags
& DEVADDR_EXTERNAL
)
1388 if (!enable_route(ip
, route
))
1391 if (route
->enabled
== _enabled
)
1395 interface_set_route_info(ip
->iface
, route
);
1397 if (system_add_route(dev
, route
))
1398 route
->failed
= true;
1400 system_del_route(dev
, route
);
1401 route
->enabled
= _enabled
;
1404 struct device_prefix
*c
;
1405 struct device_prefix_assignment
*a
;
1406 list_for_each_entry(c
, &prefixes
, head
)
1407 list_for_each_entry(a
, &c
->assignments
, head
)
1408 if (!strcmp(a
->name
, ip
->iface
->name
))
1409 interface_set_prefix_address(a
, c
, ip
->iface
, enabled
);
1411 if (ip
->iface
&& ip
->iface
->policy_rules_set
!= enabled
&&
1412 ip
->iface
->l3_dev
.dev
) {
1413 set_ip_lo_policy(enabled
, true, ip
->iface
);
1414 set_ip_lo_policy(enabled
, false, ip
->iface
);
1416 set_ip_source_policy(enabled
, true, IPRULE_PRIORITY_REJECT
+ ip
->iface
->l3_dev
.dev
->ifindex
,
1417 NULL
, 0, 0, ip
->iface
, "failed_policy", true);
1418 ip
->iface
->policy_rules_set
= enabled
;
1423 interface_ip_update_start(struct interface_ip_settings
*ip
)
1425 if (ip
!= &ip
->iface
->config_ip
) {
1426 vlist_simple_update(&ip
->dns_servers
);
1427 vlist_simple_update(&ip
->dns_search
);
1429 vlist_update(&ip
->route
);
1430 vlist_update(&ip
->addr
);
1431 vlist_update(&ip
->prefix
);
1435 interface_ip_update_complete(struct interface_ip_settings
*ip
)
1437 vlist_simple_flush(&ip
->dns_servers
);
1438 vlist_simple_flush(&ip
->dns_search
);
1439 vlist_flush(&ip
->route
);
1440 vlist_flush(&ip
->addr
);
1441 vlist_flush(&ip
->prefix
);
1442 interface_write_resolv_conf();
1446 interface_ip_flush(struct interface_ip_settings
*ip
)
1448 if (ip
== &ip
->iface
->proto_ip
)
1449 vlist_flush_all(&ip
->iface
->host_routes
);
1450 vlist_simple_flush_all(&ip
->dns_servers
);
1451 vlist_simple_flush_all(&ip
->dns_search
);
1452 vlist_flush_all(&ip
->route
);
1453 vlist_flush_all(&ip
->addr
);
1454 vlist_flush_all(&ip
->prefix
);
1458 __interface_ip_init(struct interface_ip_settings
*ip
, struct interface
*iface
)
1462 vlist_simple_init(&ip
->dns_search
, struct dns_search_domain
, node
);
1463 vlist_simple_init(&ip
->dns_servers
, struct dns_server
, node
);
1464 vlist_init(&ip
->route
, route_cmp
, interface_update_proto_route
);
1465 vlist_init(&ip
->addr
, addr_cmp
, interface_update_proto_addr
);
1466 vlist_init(&ip
->prefix
, prefix_cmp
, interface_update_prefix
);
1470 interface_ip_init(struct interface
*iface
)
1472 __interface_ip_init(&iface
->proto_ip
, iface
);
1473 __interface_ip_init(&iface
->config_ip
, iface
);
1474 vlist_init(&iface
->host_routes
, route_cmp
, interface_update_host_route
);
1478 interface_ip_valid_until_handler(struct uloop_timeout
*t
)
1480 time_t now
= system_get_rtime();
1481 struct interface
*iface
;
1482 vlist_for_each_element(&interfaces
, iface
, node
) {
1483 if (iface
->state
!= IFS_UP
)
1486 struct device_addr
*addr
, *addrp
;
1487 struct device_route
*route
, *routep
;
1488 struct device_prefix
*pref
, *prefp
;
1490 vlist_for_each_element_safe(&iface
->proto_ip
.addr
, addr
, node
, addrp
)
1491 if (addr
->valid_until
&& addr
->valid_until
< now
)
1492 vlist_delete(&iface
->proto_ip
.addr
, &addr
->node
);
1494 vlist_for_each_element_safe(&iface
->proto_ip
.route
, route
, node
, routep
)
1495 if (route
->valid_until
&& route
->valid_until
< now
)
1496 vlist_delete(&iface
->proto_ip
.route
, &route
->node
);
1498 vlist_for_each_element_safe(&iface
->proto_ip
.prefix
, pref
, node
, prefp
)
1499 if (pref
->valid_until
&& pref
->valid_until
< now
)
1500 vlist_delete(&iface
->proto_ip
.prefix
, &pref
->node
);
1504 uloop_timeout_set(t
, 1000);
1508 interface_ip_init_worker(void)
1510 valid_until_timeout
.cb
= interface_ip_valid_until_handler
;
1511 uloop_timeout_set(&valid_until_timeout
, 1000);