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
);
136 static int set_ip_lo_policy(bool add
, bool v6
, struct interface
*iface
)
138 struct iprule rule
= {
139 .flags
= IPRULE_IN
| IPRULE_LOOKUP
| IPRULE_PRIORITY
,
140 .priority
= IPRULE_PRIORITY_NW
+ iface
->l3_dev
.dev
->ifindex
,
141 .lookup
= (v6
) ? iface
->ip6table
: iface
->ip4table
,
148 rule
.flags
|= (v6
) ? IPRULE_INET6
: IPRULE_INET4
;
150 return (add
) ? system_add_iprule(&rule
) : system_del_iprule(&rule
);
154 __find_ip_addr_target(struct interface_ip_settings
*ip
, union if_addr
*a
, bool v6
)
156 struct device_addr
*addr
;
158 vlist_for_each_element(&ip
->addr
, addr
, node
) {
162 if (v6
!= ((addr
->flags
& DEVADDR_FAMILY
) == DEVADDR_INET6
))
165 // Handle offlink addresses correctly
166 unsigned int mask
= addr
->mask
;
167 if ((addr
->flags
& DEVADDR_FAMILY
) == DEVADDR_INET6
&&
168 (addr
->flags
& DEVADDR_OFFLINK
))
171 if (!match_if_addr(&addr
->addr
, a
, mask
))
181 __find_ip_route_target(struct interface_ip_settings
*ip
, union if_addr
*a
,
182 bool v6
, struct device_route
**res
)
184 struct device_route
*route
;
186 vlist_for_each_element(&ip
->route
, route
, node
) {
190 if (v6
!= ((route
->flags
& DEVADDR_FAMILY
) == DEVADDR_INET6
))
193 if (!match_if_addr(&route
->addr
, a
, route
->mask
))
196 if (route
->flags
& DEVROUTE_TABLE
)
199 if (!*res
|| route
->mask
< (*res
)->mask
)
205 interface_ip_find_addr_target(struct interface
*iface
, union if_addr
*a
, bool v6
)
207 return __find_ip_addr_target(&iface
->proto_ip
, a
, v6
) ||
208 __find_ip_addr_target(&iface
->config_ip
, a
, v6
);
212 interface_ip_find_route_target(struct interface
*iface
, union if_addr
*a
,
213 bool v6
, struct device_route
**route
)
215 __find_ip_route_target(&iface
->proto_ip
, a
, v6
, route
);
216 __find_ip_route_target(&iface
->config_ip
, a
, v6
, route
);
220 interface_ip_add_target_route(union if_addr
*addr
, bool v6
, struct interface
*iface
)
222 struct device_route
*route
, *r_next
= NULL
;
223 bool defaultroute_target
= false;
224 int addrsize
= v6
? sizeof(addr
->in6
) : sizeof(addr
->in
);
226 route
= calloc(1, sizeof(*route
));
230 route
->flags
= v6
? DEVADDR_INET6
: DEVADDR_INET4
;
231 route
->mask
= v6
? 128 : 32;
232 if (memcmp(&route
->addr
, addr
, addrsize
) == 0)
233 defaultroute_target
= true;
235 memcpy(&route
->addr
, addr
, addrsize
);
238 /* look for locally addressable target first */
239 if (interface_ip_find_addr_target(iface
, addr
, v6
))
242 /* do not stop at the first route, let the lookup compare
243 * masks to find the best match */
244 interface_ip_find_route_target(iface
, addr
, v6
, &r_next
);
246 vlist_for_each_element(&interfaces
, iface
, node
) {
247 /* look for locally addressable target first */
248 if (interface_ip_find_addr_target(iface
, addr
, v6
))
251 /* do not stop at the first route, let the lookup compare
252 * masks to find the best match */
253 interface_ip_find_route_target(iface
, addr
, v6
, &r_next
);
262 iface
= r_next
->iface
;
263 memcpy(&route
->nexthop
, &r_next
->nexthop
, sizeof(route
->nexthop
));
264 route
->mtu
= r_next
->mtu
;
265 route
->metric
= r_next
->metric
;
266 route
->table
= r_next
->table
;
269 route
->iface
= iface
;
270 if (defaultroute_target
)
273 vlist_add(&iface
->host_routes
, &route
->node
, route
);
278 interface_ip_add_route(struct interface
*iface
, struct blob_attr
*attr
, bool v6
)
280 struct interface_ip_settings
*ip
;
281 struct blob_attr
*tb
[__ROUTE_MAX
], *cur
;
282 struct device_route
*route
;
283 int af
= v6
? AF_INET6
: AF_INET
;
284 bool is_proto_route
= !!iface
;
286 blobmsg_parse(route_attr
, __ROUTE_MAX
, tb
, blobmsg_data(attr
), blobmsg_data_len(attr
));
289 if ((cur
= tb
[ROUTE_INTERFACE
]) == NULL
)
292 iface
= vlist_find(&interfaces
, blobmsg_data(cur
), iface
, node
);
296 ip
= &iface
->config_ip
;
298 ip
= &iface
->proto_ip
;
301 route
= calloc(1, sizeof(*route
));
305 route
->flags
= v6
? DEVADDR_INET6
: DEVADDR_INET4
;
306 route
->mask
= v6
? 128 : 32;
307 if ((cur
= tb
[ROUTE_MASK
]) != NULL
) {
308 route
->mask
= parse_netmask_string(blobmsg_data(cur
), v6
);
309 if (route
->mask
> (v6
? 128 : 32))
313 if ((cur
= tb
[ROUTE_TARGET
]) != NULL
) {
314 if (!parse_ip_and_netmask(af
, blobmsg_data(cur
), &route
->addr
, &route
->mask
)) {
315 DPRINTF("Failed to parse route target: %s\n", (char *) blobmsg_data(cur
));
320 if ((cur
= tb
[ROUTE_GATEWAY
]) != NULL
) {
321 if (!inet_pton(af
, blobmsg_data(cur
), &route
->nexthop
)) {
322 DPRINTF("Failed to parse route gateway: %s\n", (char *) blobmsg_data(cur
));
327 if ((cur
= tb
[ROUTE_METRIC
]) != NULL
) {
328 route
->metric
= blobmsg_get_u32(cur
);
329 route
->flags
|= DEVROUTE_METRIC
;
331 route
->metric
= iface
->metric
;
333 if ((cur
= tb
[ROUTE_MTU
]) != NULL
) {
334 route
->mtu
= blobmsg_get_u32(cur
);
335 route
->flags
|= DEVROUTE_MTU
;
338 // Use source-based routing
339 if ((cur
= tb
[ROUTE_SOURCE
]) != NULL
) {
340 char *saveptr
, *source
= alloca(blobmsg_data_len(cur
));
341 memcpy(source
, blobmsg_data(cur
), blobmsg_data_len(cur
));
343 const char *addr
= strtok_r(source
, "/", &saveptr
);
344 const char *mask
= strtok_r(NULL
, "/", &saveptr
);
346 if (!addr
|| inet_pton(af
, addr
, &route
->source
) < 1) {
347 DPRINTF("Failed to parse route source: %s\n", addr
);
351 route
->sourcemask
= (mask
) ? atoi(mask
) : ((af
== AF_INET6
) ? 128 : 32);
354 if ((cur
= tb
[ROUTE_ONLINK
]) != NULL
&& blobmsg_get_bool(cur
))
355 route
->flags
|= DEVROUTE_ONLINK
;
357 if (is_proto_route
) {
358 route
->table
= (v6
) ? iface
->ip6table
: iface
->ip4table
;
359 route
->flags
|= DEVROUTE_SRCTABLE
;
362 if ((cur
= tb
[ROUTE_TABLE
]) != NULL
) {
363 if (!system_resolve_rt_table(blobmsg_data(cur
), &route
->table
)) {
364 DPRINTF("Failed to resolve routing table: %s\n", (char *) blobmsg_data(cur
));
368 /* only set the table flag if not using the main (default) table */
369 if (system_is_default_rt_table(route
->table
))
373 route
->flags
|= DEVROUTE_TABLE
;
376 if ((cur
= tb
[ROUTE_VALID
]) != NULL
) {
377 int64_t valid
= blobmsg_get_u32(cur
);
378 int64_t valid_until
= valid
+ (int64_t)system_get_rtime();
379 if (valid_until
<= LONG_MAX
&& valid
!= 0xffffffffLL
) // Catch overflow
380 route
->valid_until
= valid_until
;
383 if ((cur
= tb
[ROUTE_TYPE
]) != NULL
) {
384 if (!system_resolve_rt_type(blobmsg_data(cur
), &route
->type
)) {
385 DPRINTF("Failed to resolve routing type: %s\n", (char *) blobmsg_data(cur
));
388 route
->flags
|= DEVROUTE_TYPE
;
391 vlist_add(&ip
->route
, &route
->node
, route
);
399 addr_cmp(const void *k1
, const void *k2
, void *ptr
)
401 return memcmp(k1
, k2
, sizeof(struct device_addr
) -
402 offsetof(struct device_addr
, flags
));
406 route_cmp(const void *k1
, const void *k2
, void *ptr
)
408 const struct device_route
*r1
= k1
, *r2
= k2
;
410 if (r1
->mask
!= r2
->mask
)
411 return r2
->mask
- r1
->mask
;
413 if (r1
->metric
!= r2
->metric
)
414 return r1
->metric
- r2
->metric
;
416 if (r1
->flags
!= r2
->flags
)
417 return r2
->flags
- r1
->flags
;
419 if (r1
->sourcemask
!= r2
->sourcemask
)
420 return r1
->sourcemask
- r2
->sourcemask
;
422 if (r1
->table
!= r2
->table
)
423 return r1
->table
- r2
->table
;
425 int maskcmp
= memcmp(&r1
->source
, &r2
->source
, sizeof(r1
->source
));
429 return memcmp(&r1
->addr
, &r2
->addr
, sizeof(r1
->addr
));
433 prefix_cmp(const void *k1
, const void *k2
, void *ptr
)
435 return memcmp(k1
, k2
, offsetof(struct device_prefix
, pclass
) -
436 offsetof(struct device_prefix
, addr
));
440 interface_handle_subnet_route(struct interface
*iface
, struct device_addr
*addr
, bool add
)
442 struct device
*dev
= iface
->l3_dev
.dev
;
443 struct device_route route
;
445 if (addr
->flags
& DEVADDR_OFFLINK
)
448 memset(&route
, 0, sizeof(route
));
450 route
.flags
= addr
->flags
;
451 route
.mask
= addr
->mask
;
452 memcpy(&route
.addr
, &addr
->addr
, sizeof(route
.addr
));
453 clear_if_addr(&route
.addr
, route
.mask
);
456 route
.flags
|= DEVADDR_KERNEL
;
457 system_del_route(dev
, &route
);
459 route
.flags
&= ~DEVADDR_KERNEL
;
460 route
.metric
= iface
->metric
;
461 system_add_route(dev
, &route
);
463 system_del_route(dev
, &route
);
468 interface_update_proto_addr(struct vlist_tree
*tree
,
469 struct vlist_node
*node_new
,
470 struct vlist_node
*node_old
)
472 struct interface_ip_settings
*ip
;
473 struct interface
*iface
;
475 struct device_addr
*a_new
= NULL
, *a_old
= NULL
;
476 bool replace
= false;
480 ip
= container_of(tree
, struct interface_ip_settings
, addr
);
482 dev
= iface
->l3_dev
.dev
;
484 if (!node_new
|| !node_old
)
485 iface
->updated
|= IUF_ADDRESS
;
488 a_new
= container_of(node_new
, struct device_addr
, node
);
490 if ((a_new
->flags
& DEVADDR_FAMILY
) == DEVADDR_INET4
&&
493 /* /31 and /32 addressing need 255.255.255.255
494 * as broadcast address. */
495 if (a_new
->mask
>= 31) {
496 a_new
->broadcast
= (uint32_t) ~0;
499 uint32_t *a
= (uint32_t *) &a_new
->addr
;
501 mask
>>= a_new
->mask
;
502 a_new
->broadcast
= *a
| htonl(mask
);
508 a_old
= container_of(node_old
, struct device_addr
, node
);
510 if (a_new
&& a_old
) {
513 if (a_old
->flags
!= a_new
->flags
|| a_old
->failed
)
516 if (a_old
->valid_until
!= a_new
->valid_until
||
517 a_old
->preferred_until
!= a_new
->preferred_until
)
520 if ((a_new
->flags
& DEVADDR_FAMILY
) == DEVADDR_INET4
&&
521 a_new
->broadcast
!= a_old
->broadcast
)
526 if (a_old
->enabled
&& !keep
) {
527 if ((a_old
->flags
& DEVADDR_FAMILY
) == DEVADDR_INET6
)
530 //This is needed for source routing to work correctly. If a device
531 //has two connections to a network using the same subnet, adding
532 //only the network-rule will cause packets to be routed through the
533 //first matching network (source IP matches both masks).
534 if (a_old
->policy_table
) {
535 set_ip_source_policy(false, v6
, IPRULE_PRIORITY_ADDR
, &a_old
->addr
,
536 (v6
) ? 128 : 32, a_old
->policy_table
, NULL
, NULL
);
538 if (a_old
->mask
!= ((v6
) ? 128 : 32))
539 set_ip_source_policy(false, v6
, IPRULE_PRIORITY_NW
, &a_old
->addr
,
540 a_old
->mask
, a_old
->policy_table
, NULL
, NULL
);
543 if (!(a_old
->flags
& DEVADDR_EXTERNAL
)) {
544 interface_handle_subnet_route(iface
, a_old
, false);
545 system_del_address(dev
, a_old
);
553 a_new
->enabled
= true;
555 if ((a_new
->flags
& DEVADDR_FAMILY
) == DEVADDR_INET6
)
558 a_new
->policy_table
= (v6
) ? iface
->ip6table
: iface
->ip4table
;
560 if (!keep
|| replace
) {
561 if (!(a_new
->flags
& DEVADDR_EXTERNAL
)) {
562 if (system_add_address(dev
, a_new
))
563 a_new
->failed
= true;
566 interface_handle_subnet_route(iface
, a_new
, true);
570 if (a_new
->policy_table
) {
571 set_ip_source_policy(true, v6
, IPRULE_PRIORITY_ADDR
, &a_new
->addr
,
572 (v6
) ? 128 : 32, a_new
->policy_table
, NULL
, NULL
);
574 if (a_new
->mask
!= ((v6
) ? 128 : 32))
575 set_ip_source_policy(true, v6
, IPRULE_PRIORITY_NW
, &a_new
->addr
,
576 a_new
->mask
, a_new
->policy_table
, NULL
, NULL
);
584 enable_route(struct interface_ip_settings
*ip
, struct device_route
*route
)
586 if (ip
->no_defaultroute
&& !route
->mask
)
593 interface_update_proto_route(struct vlist_tree
*tree
,
594 struct vlist_node
*node_new
,
595 struct vlist_node
*node_old
)
597 struct interface_ip_settings
*ip
;
598 struct interface
*iface
;
600 struct device_route
*route_old
, *route_new
;
603 ip
= container_of(tree
, struct interface_ip_settings
, route
);
605 dev
= iface
->l3_dev
.dev
;
607 if (!node_new
|| !node_old
)
608 iface
->updated
|= IUF_ROUTE
;
610 route_old
= container_of(node_old
, struct device_route
, node
);
611 route_new
= container_of(node_new
, struct device_route
, node
);
613 if (node_old
&& node_new
)
614 keep
= !memcmp(&route_old
->nexthop
, &route_new
->nexthop
, sizeof(route_old
->nexthop
)) &&
615 (route_old
->mtu
== route_new
->mtu
) && (route_old
->type
== route_new
->type
) &&
619 if (!(route_old
->flags
& DEVADDR_EXTERNAL
) && route_old
->enabled
&& !keep
)
620 system_del_route(dev
, route_old
);
626 bool _enabled
= enable_route(ip
, route_new
);
628 if (!(route_new
->flags
& DEVADDR_EXTERNAL
) && !keep
&& _enabled
)
629 if (system_add_route(dev
, route_new
))
630 route_new
->failed
= true;
632 route_new
->iface
= iface
;
633 route_new
->enabled
= _enabled
;
638 interface_update_host_route(struct vlist_tree
*tree
,
639 struct vlist_node
*node_new
,
640 struct vlist_node
*node_old
)
642 struct interface
*iface
;
644 struct device_route
*route_old
, *route_new
;
646 iface
= container_of(tree
, struct interface
, host_routes
);
647 dev
= iface
->l3_dev
.dev
;
649 route_old
= container_of(node_old
, struct device_route
, node
);
650 route_new
= container_of(node_new
, struct device_route
, node
);
653 system_del_route(dev
, route_old
);
658 if (system_add_route(dev
, route_new
))
659 route_new
->failed
= true;
664 random_ifaceid(struct in6_addr
*addr
)
666 static bool initialized
= false;
671 gettimeofday(&t
, NULL
);
672 seed
= t
.tv_sec
^ t
.tv_usec
^ getpid();
676 addr
->s6_addr32
[2] = (uint32_t)mrand48();
677 addr
->s6_addr32
[3] = (uint32_t)mrand48();
681 eui64_ifaceid(struct interface
*iface
, struct in6_addr
*addr
)
683 /* get mac address */
684 uint8_t *macaddr
= iface
->l3_dev
.dev
->settings
.macaddr
;
685 uint8_t *ifaceid
= addr
->s6_addr
+ 8;
686 memcpy(ifaceid
,macaddr
,3);
687 memcpy(ifaceid
+ 5,macaddr
+ 3, 3);
694 generate_ifaceid(struct interface
*iface
, struct in6_addr
*addr
)
696 /* generate new iface id */
697 switch (iface
->assignment_iface_id_selection
) {
700 /* copy host part from assignment_fixed_iface_id */
701 memcpy(addr
->s6_addr
+ 8, iface
->assignment_fixed_iface_id
.s6_addr
+ 8, 8);
704 /* randomize last 64 bits */
705 random_ifaceid(addr
);
709 eui64_ifaceid(iface
, addr
);
715 interface_set_prefix_address(struct device_prefix_assignment
*assignment
,
716 const struct device_prefix
*prefix
, struct interface
*iface
, bool add
)
718 const struct interface
*uplink
= prefix
->iface
;
719 if (!iface
->l3_dev
.dev
)
722 struct device
*l3_downlink
= iface
->l3_dev
.dev
;
724 struct device_addr addr
;
725 struct device_route route
;
726 memset(&addr
, 0, sizeof(addr
));
727 memset(&route
, 0, sizeof(route
));
729 if (IN6_IS_ADDR_UNSPECIFIED(&assignment
->addr
)) {
730 addr
.addr
.in6
= prefix
->addr
;
731 addr
.addr
.in6
.s6_addr32
[1] |= htonl(assignment
->assigned
);
732 generate_ifaceid(iface
, &addr
.addr
.in6
);
733 assignment
->addr
= addr
.addr
.in6
;
736 addr
.addr
.in6
= assignment
->addr
;
738 addr
.mask
= assignment
->length
;
739 addr
.flags
= DEVADDR_INET6
| DEVADDR_OFFLINK
;
740 addr
.preferred_until
= prefix
->preferred_until
;
741 addr
.valid_until
= prefix
->valid_until
;
743 route
.flags
= DEVADDR_INET6
;
744 route
.mask
= addr
.mask
< 64 ? 64 : addr
.mask
;
745 route
.addr
= addr
.addr
;
746 clear_if_addr(&route
.addr
, route
.mask
);
748 if (!add
&& assignment
->enabled
) {
749 time_t now
= system_get_rtime();
750 addr
.preferred_until
= now
;
751 if (!addr
.valid_until
|| addr
.valid_until
- now
> 7200)
752 addr
.valid_until
= now
+ 7200;
755 if (prefix
->iface
->ip6table
)
756 set_ip_source_policy(false, true, IPRULE_PRIORITY_NW
, &addr
.addr
,
757 addr
.mask
, prefix
->iface
->ip6table
, iface
, NULL
);
759 set_ip_source_policy(false, true, IPRULE_PRIORITY_REJECT
, &addr
.addr
,
760 addr
.mask
, 0, iface
, "unreachable");
763 system_del_route(l3_downlink
, &route
);
764 system_add_address(l3_downlink
, &addr
);
766 assignment
->enabled
= false;
767 } else if (add
&& (iface
->state
== IFS_UP
|| iface
->state
== IFS_SETUP
) &&
768 !system_add_address(l3_downlink
, &addr
)) {
770 if (prefix
->iface
&& !assignment
->enabled
) {
771 set_ip_source_policy(true, true, IPRULE_PRIORITY_REJECT
, &addr
.addr
,
772 addr
.mask
, 0, iface
, "unreachable");
774 if (prefix
->iface
->ip6table
)
775 set_ip_source_policy(true, true, IPRULE_PRIORITY_NW
, &addr
.addr
,
776 addr
.mask
, prefix
->iface
->ip6table
, iface
, NULL
);
779 route
.metric
= iface
->metric
;
780 system_add_route(l3_downlink
, &route
);
782 if (uplink
&& uplink
->l3_dev
.dev
&& !(l3_downlink
->settings
.flags
& DEV_OPT_MTU6
)) {
783 int mtu
= system_update_ipv6_mtu(uplink
->l3_dev
.dev
, 0);
784 int mtu_old
= system_update_ipv6_mtu(l3_downlink
, 0);
786 if (mtu
> 0 && mtu_old
> mtu
)
787 system_update_ipv6_mtu(l3_downlink
, mtu
);
790 assignment
->enabled
= true;
794 static bool interface_prefix_assign(struct list_head
*list
,
795 struct device_prefix_assignment
*assign
)
797 int32_t current
= 0, asize
= (1 << (64 - assign
->length
)) - 1;
798 struct device_prefix_assignment
*c
;
799 list_for_each_entry(c
, list
, head
) {
800 if (assign
->assigned
!= -1) {
801 if (assign
->assigned
>= current
&& assign
->assigned
+ asize
< c
->assigned
) {
802 list_add_tail(&assign
->head
, &c
->head
);
805 } else if (assign
->assigned
== -1) {
806 current
= (current
+ asize
) & (~asize
);
807 if (current
+ asize
< c
->assigned
) {
808 assign
->assigned
= current
;
809 list_add_tail(&assign
->head
, &c
->head
);
813 current
= (c
->assigned
+ (1 << (64 - c
->length
)));
818 static void interface_update_prefix_assignments(struct device_prefix
*prefix
, bool setup
)
820 struct device_prefix_assignment
*c
;
821 struct interface
*iface
;
823 // Delete all assignments
824 while (!list_empty(&prefix
->assignments
)) {
825 c
= list_first_entry(&prefix
->assignments
,
826 struct device_prefix_assignment
, head
);
827 if ((iface
= vlist_find(&interfaces
, c
->name
, iface
, node
)))
828 interface_set_prefix_address(c
, prefix
, iface
, false);
836 // End-of-assignment sentinel
837 c
= malloc(sizeof(*c
) + 1);
838 c
->assigned
= 1 << (64 - prefix
->length
);
841 c
->addr
= in6addr_any
;
842 list_add(&c
->head
, &prefix
->assignments
);
845 if (prefix
->excl_length
> 0) {
846 const char name
[] = "!excluded";
847 c
= malloc(sizeof(*c
) + sizeof(name
));
848 c
->assigned
= ntohl(prefix
->excl_addr
.s6_addr32
[1]) &
849 ((1 << (64 - prefix
->length
)) - 1);
850 c
->length
= prefix
->excl_length
;
851 c
->addr
= in6addr_any
;
852 memcpy(c
->name
, name
, sizeof(name
));
853 list_add(&c
->head
, &prefix
->assignments
);
856 bool assigned_any
= false;
857 struct list_head assign_later
= LIST_HEAD_INIT(assign_later
);
858 vlist_for_each_element(&interfaces
, iface
, node
) {
859 if (iface
->assignment_length
< 48 ||
860 iface
->assignment_length
> 64)
863 // Test whether there is a matching class
864 if (!list_empty(&iface
->assignment_classes
)) {
867 struct interface_assignment_class
*c
;
868 list_for_each_entry(c
, &iface
->assignment_classes
, head
) {
869 if (!strcmp(c
->name
, prefix
->pclass
)) {
879 size_t namelen
= strlen(iface
->name
) + 1;
880 c
= malloc(sizeof(*c
) + namelen
);
881 c
->length
= iface
->assignment_length
;
882 c
->assigned
= iface
->assignment_hint
;
883 c
->addr
= in6addr_any
;
885 memcpy(c
->name
, iface
->name
, namelen
);
887 // First process all custom assignments, put all others in later-list
888 if (c
->assigned
== -1 || !interface_prefix_assign(&prefix
->assignments
, c
)) {
889 if (c
->assigned
!= -1) {
891 netifd_log_message(L_WARNING
, "Failed to assign requested subprefix "
892 "of size %hhu for %s, trying other\n", c
->length
, c
->name
);
895 struct list_head
*next
= &assign_later
;
896 struct device_prefix_assignment
*n
;
897 list_for_each_entry(n
, &assign_later
, head
) {
898 if (n
->length
< c
->length
) {
903 list_add_tail(&c
->head
, next
);
906 if (c
->assigned
!= -1)
910 // Then try to assign all other + failed custom assignments
911 while (!list_empty(&assign_later
)) {
912 c
= list_first_entry(&assign_later
, struct device_prefix_assignment
, head
);
915 bool assigned
= false;
917 assigned
= interface_prefix_assign(&prefix
->assignments
, c
);
918 } while (!assigned
&& ++c
->length
<= 64);
921 netifd_log_message(L_WARNING
, "Failed to assign subprefix "
922 "of size %hhu for %s\n", c
->length
, c
->name
);
929 list_for_each_entry(c
, &prefix
->assignments
, head
)
930 if ((iface
= vlist_find(&interfaces
, c
->name
, iface
, node
)))
931 interface_set_prefix_address(c
, prefix
, iface
, true);
934 netifd_log_message(L_WARNING
, "You have delegated IPv6-prefixes but haven't assigned them "
935 "to any interface. Did you forget to set option ip6assign on your lan-interfaces?");
939 void interface_refresh_assignments(bool hint
)
941 static bool refresh
= false;
942 if (!hint
&& refresh
) {
943 struct device_prefix
*p
;
944 list_for_each_entry(p
, &prefixes
, head
)
945 interface_update_prefix_assignments(p
, true);
952 interface_update_prefix(struct vlist_tree
*tree
,
953 struct vlist_node
*node_new
,
954 struct vlist_node
*node_old
)
956 struct device_prefix
*prefix_old
, *prefix_new
;
957 prefix_old
= container_of(node_old
, struct device_prefix
, node
);
958 prefix_new
= container_of(node_new
, struct device_prefix
, node
);
960 struct interface_ip_settings
*ip
= container_of(tree
, struct interface_ip_settings
, prefix
);
961 if (tree
&& (!node_new
|| !node_old
))
962 ip
->iface
->updated
|= IUF_PREFIX
;
964 struct device_route route
;
965 memset(&route
, 0, sizeof(route
));
966 route
.flags
= DEVADDR_INET6
;
967 route
.metric
= INT32_MAX
;
968 route
.mask
= (node_new
) ? prefix_new
->length
: prefix_old
->length
;
969 route
.addr
.in6
= (node_new
) ? prefix_new
->addr
: prefix_old
->addr
;
972 struct device_prefix_assignment
*c
;
973 struct interface
*iface
;
975 if (node_old
&& node_new
) {
976 // Move assignments and refresh addresses to update valid times
977 list_splice(&prefix_old
->assignments
, &prefix_new
->assignments
);
979 list_for_each_entry(c
, &prefix_new
->assignments
, head
)
980 if ((iface
= vlist_find(&interfaces
, c
->name
, iface
, node
)))
981 interface_set_prefix_address(c
, prefix_new
, iface
, true);
982 } else if (node_new
) {
983 // Set null-route to avoid routing loops
984 system_add_route(NULL
, &route
);
986 if (!prefix_new
->iface
|| !prefix_new
->iface
->proto_ip
.no_delegation
)
987 interface_update_prefix_assignments(prefix_new
, true);
988 } else if (node_old
) {
990 interface_update_prefix_assignments(prefix_old
, false);
991 system_del_route(NULL
, &route
);
995 if (prefix_old
->head
.next
)
996 list_del(&prefix_old
->head
);
1000 if (node_new
&& (!prefix_new
->iface
|| !prefix_new
->iface
->proto_ip
.no_delegation
))
1001 list_add(&prefix_new
->head
, &prefixes
);
1005 struct device_prefix
*
1006 interface_ip_add_device_prefix(struct interface
*iface
, struct in6_addr
*addr
,
1007 uint8_t length
, time_t valid_until
, time_t preferred_until
,
1008 struct in6_addr
*excl_addr
, uint8_t excl_length
, const char *pclass
)
1011 pclass
= (iface
) ? iface
->name
: "local";
1013 struct device_prefix
*prefix
= calloc(1, sizeof(*prefix
) + strlen(pclass
) + 1);
1014 prefix
->length
= length
;
1015 prefix
->addr
= *addr
;
1016 prefix
->preferred_until
= preferred_until
;
1017 prefix
->valid_until
= valid_until
;
1018 prefix
->iface
= iface
;
1019 INIT_LIST_HEAD(&prefix
->assignments
);
1022 prefix
->excl_addr
= *excl_addr
;
1023 prefix
->excl_length
= excl_length
;
1026 strcpy(prefix
->pclass
, pclass
);
1029 vlist_add(&iface
->proto_ip
.prefix
, &prefix
->node
, &prefix
->addr
);
1031 interface_update_prefix(NULL
, &prefix
->node
, NULL
);
1037 interface_ip_set_ula_prefix(const char *prefix
)
1039 char buf
[INET6_ADDRSTRLEN
+ 4] = {0}, *saveptr
;
1041 strncpy(buf
, prefix
, sizeof(buf
) - 1);
1042 char *prefixaddr
= strtok_r(buf
, "/", &saveptr
);
1044 struct in6_addr addr
;
1045 if (!prefixaddr
|| inet_pton(AF_INET6
, prefixaddr
, &addr
) < 1) {
1047 interface_update_prefix(NULL
, NULL
, &ula_prefix
->node
);
1054 char *prefixlen
= strtok_r(NULL
, ",", &saveptr
);
1055 if (!prefixlen
|| (length
= atoi(prefixlen
)) < 1 || length
> 64)
1058 if (!ula_prefix
|| !IN6_ARE_ADDR_EQUAL(&addr
, &ula_prefix
->addr
) ||
1059 ula_prefix
->length
!= length
) {
1061 interface_update_prefix(NULL
, NULL
, &ula_prefix
->node
);
1063 ula_prefix
= interface_ip_add_device_prefix(NULL
, &addr
, length
,
1064 0, 0, NULL
, 0, NULL
);
1069 interface_add_dns_server(struct interface_ip_settings
*ip
, const char *str
)
1071 struct dns_server
*s
;
1073 s
= calloc(1, sizeof(*s
));
1078 if (inet_pton(s
->af
, str
, &s
->addr
.in
))
1082 if (inet_pton(s
->af
, str
, &s
->addr
.in
))
1089 D(INTERFACE
, "Add IPv%c DNS server: %s\n",
1090 s
->af
== AF_INET6
? '6' : '4', str
);
1091 vlist_simple_add(&ip
->dns_servers
, &s
->node
);
1095 interface_add_dns_server_list(struct interface_ip_settings
*ip
, struct blob_attr
*list
)
1097 struct blob_attr
*cur
;
1100 blobmsg_for_each_attr(cur
, list
, rem
) {
1101 if (blobmsg_type(cur
) != BLOBMSG_TYPE_STRING
)
1104 if (!blobmsg_check_attr(cur
, NULL
))
1107 interface_add_dns_server(ip
, blobmsg_data(cur
));
1112 interface_add_dns_search_domain(struct interface_ip_settings
*ip
, const char *str
)
1114 struct dns_search_domain
*s
;
1115 int len
= strlen(str
);
1117 s
= calloc(1, sizeof(*s
) + len
+ 1);
1121 D(INTERFACE
, "Add DNS search domain: %s\n", str
);
1122 memcpy(s
->name
, str
, len
);
1123 vlist_simple_add(&ip
->dns_search
, &s
->node
);
1127 interface_add_dns_search_list(struct interface_ip_settings
*ip
, struct blob_attr
*list
)
1129 struct blob_attr
*cur
;
1132 blobmsg_for_each_attr(cur
, list
, rem
) {
1133 if (blobmsg_type(cur
) != BLOBMSG_TYPE_STRING
)
1136 if (!blobmsg_check_attr(cur
, NULL
))
1139 interface_add_dns_search_domain(ip
, blobmsg_data(cur
));
1144 write_resolv_conf_entries(FILE *f
, struct interface_ip_settings
*ip
, const char *dev
)
1146 struct dns_server
*s
;
1147 struct dns_search_domain
*d
;
1149 char buf
[INET6_ADDRSTRLEN
];
1151 vlist_simple_for_each_element(&ip
->dns_servers
, s
, node
) {
1152 str
= inet_ntop(s
->af
, &s
->addr
, buf
, sizeof(buf
));
1156 if (s
->af
== AF_INET6
&& IN6_IS_ADDR_LINKLOCAL(&s
->addr
.in6
))
1157 fprintf(f
, "nameserver %s%%%s\n", str
, dev
);
1159 fprintf(f
, "nameserver %s\n", str
);
1162 vlist_simple_for_each_element(&ip
->dns_search
, d
, node
) {
1163 fprintf(f
, "search %s\n", d
->name
);
1168 interface_write_resolv_conf(void)
1170 struct interface
*iface
;
1171 char *path
= alloca(strlen(resolv_conf
) + 5);
1173 uint32_t crcold
, crcnew
;
1175 sprintf(path
, "%s.tmp", resolv_conf
);
1177 f
= fopen(path
, "w+");
1179 D(INTERFACE
, "Failed to open %s for writing\n", path
);
1183 vlist_for_each_element(&interfaces
, iface
, node
) {
1184 if (iface
->state
!= IFS_UP
)
1187 if (vlist_simple_empty(&iface
->proto_ip
.dns_search
) &&
1188 vlist_simple_empty(&iface
->proto_ip
.dns_servers
) &&
1189 vlist_simple_empty(&iface
->config_ip
.dns_search
) &&
1190 vlist_simple_empty(&iface
->config_ip
.dns_servers
))
1193 fprintf(f
, "# Interface %s\n", iface
->name
);
1194 write_resolv_conf_entries(f
, &iface
->config_ip
, iface
->ifname
);
1195 if (!iface
->proto_ip
.no_dns
)
1196 write_resolv_conf_entries(f
, &iface
->proto_ip
, iface
->ifname
);
1200 crcnew
= crc32_file(f
);
1203 crcold
= crcnew
+ 1;
1204 f
= fopen(resolv_conf
, "r");
1206 crcold
= crc32_file(f
);
1210 if (crcold
== crcnew
) {
1212 } else if (rename(path
, resolv_conf
) < 0) {
1213 D(INTERFACE
, "Failed to replace %s\n", resolv_conf
);
1218 void interface_ip_set_enabled(struct interface_ip_settings
*ip
, bool enabled
)
1220 struct device_addr
*addr
;
1221 struct device_route
*route
;
1223 struct interface
*iface
;
1225 ip
->enabled
= enabled
;
1227 dev
= iface
->l3_dev
.dev
;
1231 vlist_for_each_element(&ip
->addr
, addr
, node
) {
1232 bool v6
= ((addr
->flags
& DEVADDR_FAMILY
) == DEVADDR_INET6
) ? true : false;
1234 if (addr
->enabled
== enabled
)
1238 system_add_address(dev
, addr
);
1240 interface_handle_subnet_route(iface
, addr
, true);
1242 addr
->policy_table
= (v6
) ? iface
->ip6table
: iface
->ip4table
;
1243 if (addr
->policy_table
) {
1244 set_ip_source_policy(true, v6
, IPRULE_PRIORITY_ADDR
, &addr
->addr
,
1245 (v6
) ? 128 : 32, addr
->policy_table
, NULL
, NULL
);
1247 if (addr
->mask
!= ((v6
) ? 128 : 32))
1248 set_ip_source_policy(true, v6
, IPRULE_PRIORITY_NW
, &addr
->addr
,
1249 addr
->mask
, addr
->policy_table
, NULL
, NULL
);
1252 interface_handle_subnet_route(iface
, addr
, false);
1253 system_del_address(dev
, addr
);
1255 if (addr
->policy_table
) {
1256 set_ip_source_policy(false, v6
, IPRULE_PRIORITY_ADDR
, &addr
->addr
,
1257 (v6
) ? 128 : 32, addr
->policy_table
, NULL
, NULL
);
1259 if (addr
->mask
!= ((v6
) ? 128 : 32))
1260 set_ip_source_policy(false, v6
, IPRULE_PRIORITY_NW
, &addr
->addr
,
1261 addr
->mask
, addr
->policy_table
, NULL
, NULL
);
1264 addr
->enabled
= enabled
;
1267 vlist_for_each_element(&ip
->route
, route
, node
) {
1268 bool _enabled
= enabled
;
1270 if (!enable_route(ip
, route
))
1273 if (route
->enabled
== _enabled
)
1277 if (!(route
->flags
& DEVROUTE_METRIC
))
1278 route
->metric
= ip
->iface
->metric
;
1280 if (!(route
->flags
& DEVROUTE_TABLE
)) {
1281 route
->flags
&= ~DEVROUTE_SRCTABLE
;
1282 route
->table
= ((route
->flags
& DEVADDR_FAMILY
) == DEVADDR_INET6
) ?
1283 iface
->ip6table
: iface
->ip4table
;
1286 route
->flags
|= DEVROUTE_SRCTABLE
;
1289 if (system_add_route(dev
, route
))
1290 route
->failed
= true;
1292 system_del_route(dev
, route
);
1293 route
->enabled
= _enabled
;
1296 struct device_prefix
*c
;
1297 struct device_prefix_assignment
*a
;
1298 list_for_each_entry(c
, &prefixes
, head
)
1299 list_for_each_entry(a
, &c
->assignments
, head
)
1300 if (!strcmp(a
->name
, ip
->iface
->name
))
1301 interface_set_prefix_address(a
, c
, ip
->iface
, enabled
);
1303 if (ip
->iface
&& ip
->iface
->l3_dev
.dev
) {
1304 set_ip_lo_policy(enabled
, true, ip
->iface
);
1305 set_ip_lo_policy(enabled
, false, ip
->iface
);
1307 set_ip_source_policy(enabled
, true, IPRULE_PRIORITY_REJECT
+ ip
->iface
->l3_dev
.dev
->ifindex
,
1308 NULL
, 0, 0, ip
->iface
, "failed_policy");
1313 interface_ip_update_start(struct interface_ip_settings
*ip
)
1315 if (ip
!= &ip
->iface
->config_ip
) {
1316 vlist_simple_update(&ip
->dns_servers
);
1317 vlist_simple_update(&ip
->dns_search
);
1319 vlist_update(&ip
->route
);
1320 vlist_update(&ip
->addr
);
1321 vlist_update(&ip
->prefix
);
1325 interface_ip_update_complete(struct interface_ip_settings
*ip
)
1327 vlist_simple_flush(&ip
->dns_servers
);
1328 vlist_simple_flush(&ip
->dns_search
);
1329 vlist_flush(&ip
->route
);
1330 vlist_flush(&ip
->addr
);
1331 vlist_flush(&ip
->prefix
);
1332 interface_write_resolv_conf();
1336 interface_ip_flush(struct interface_ip_settings
*ip
)
1338 if (ip
== &ip
->iface
->proto_ip
)
1339 vlist_flush_all(&ip
->iface
->host_routes
);
1340 vlist_simple_flush_all(&ip
->dns_servers
);
1341 vlist_simple_flush_all(&ip
->dns_search
);
1342 vlist_flush_all(&ip
->route
);
1343 vlist_flush_all(&ip
->addr
);
1344 vlist_flush_all(&ip
->prefix
);
1348 __interface_ip_init(struct interface_ip_settings
*ip
, struct interface
*iface
)
1352 vlist_simple_init(&ip
->dns_search
, struct dns_search_domain
, node
);
1353 vlist_simple_init(&ip
->dns_servers
, struct dns_server
, node
);
1354 vlist_init(&ip
->route
, route_cmp
, interface_update_proto_route
);
1355 vlist_init(&ip
->addr
, addr_cmp
, interface_update_proto_addr
);
1356 vlist_init(&ip
->prefix
, prefix_cmp
, interface_update_prefix
);
1360 interface_ip_init(struct interface
*iface
)
1362 __interface_ip_init(&iface
->proto_ip
, iface
);
1363 __interface_ip_init(&iface
->config_ip
, iface
);
1364 vlist_init(&iface
->host_routes
, route_cmp
, interface_update_host_route
);
1368 interface_ip_valid_until_handler(struct uloop_timeout
*t
)
1370 time_t now
= system_get_rtime();
1371 struct interface
*iface
;
1372 vlist_for_each_element(&interfaces
, iface
, node
) {
1373 if (iface
->state
!= IFS_UP
)
1376 struct device_addr
*addr
, *addrp
;
1377 struct device_route
*route
, *routep
;
1378 struct device_prefix
*pref
, *prefp
;
1380 vlist_for_each_element_safe(&iface
->proto_ip
.addr
, addr
, node
, addrp
)
1381 if (addr
->valid_until
&& addr
->valid_until
< now
)
1382 vlist_delete(&iface
->proto_ip
.addr
, &addr
->node
);
1384 vlist_for_each_element_safe(&iface
->proto_ip
.route
, route
, node
, routep
)
1385 if (route
->valid_until
&& route
->valid_until
< now
)
1386 vlist_delete(&iface
->proto_ip
.route
, &route
->node
);
1388 vlist_for_each_element_safe(&iface
->proto_ip
.prefix
, pref
, node
, prefp
)
1389 if (pref
->valid_until
&& pref
->valid_until
< now
)
1390 vlist_delete(&iface
->proto_ip
.prefix
, &pref
->node
);
1394 uloop_timeout_set(t
, 1000);
1398 interface_ip_init_worker(void)
1400 valid_until_timeout
.cb
= interface_ip_valid_until_handler
;
1401 uloop_timeout_set(&valid_until_timeout
, 1000);