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.
22 #include <arpa/inet.h>
23 #include <netinet/in.h>
27 #include "interface.h"
28 #include "interface-ip.h"
50 static const struct blobmsg_policy route_attr
[__ROUTE_MAX
] = {
51 [ROUTE_INTERFACE
] = { .name
= "interface", .type
= BLOBMSG_TYPE_STRING
},
52 [ROUTE_TARGET
] = { .name
= "target", .type
= BLOBMSG_TYPE_STRING
},
53 [ROUTE_MASK
] = { .name
= "netmask", .type
= BLOBMSG_TYPE_STRING
},
54 [ROUTE_GATEWAY
] = { .name
= "gateway", .type
= BLOBMSG_TYPE_STRING
},
55 [ROUTE_METRIC
] = { .name
= "metric", .type
= BLOBMSG_TYPE_INT32
},
56 [ROUTE_MTU
] = { .name
= "mtu", .type
= BLOBMSG_TYPE_INT32
},
57 [ROUTE_TABLE
] = { .name
= "table", .type
= BLOBMSG_TYPE_STRING
},
58 [ROUTE_VALID
] = { .name
= "valid", .type
= BLOBMSG_TYPE_INT32
},
59 [ROUTE_SOURCE
] = { .name
= "source", .type
= BLOBMSG_TYPE_STRING
},
60 [ROUTE_ONLINK
] = { .name
= "onlink", .type
= BLOBMSG_TYPE_BOOL
},
61 [ROUTE_TYPE
] = { .name
= "type", .type
= BLOBMSG_TYPE_STRING
},
62 [ROUTE_PROTO
] = { .name
= "proto", .type
= BLOBMSG_TYPE_STRING
},
63 [ROUTE_DISABLED
] = { .name
= "disabled", .type
= BLOBMSG_TYPE_BOOL
},
66 const struct uci_blob_param_list route_attr_list
= {
67 .n_params
= __ROUTE_MAX
,
80 static const struct blobmsg_policy neighbor_attr
[__NEIGHBOR_MAX
]={
81 [NEIGHBOR_INTERFACE
]= { .name
= "interface", .type
= BLOBMSG_TYPE_STRING
},
82 [NEIGHBOR_ADDRESS
]= { .name
= "ipaddr", .type
= BLOBMSG_TYPE_STRING
},
83 [NEIGHBOR_MAC
]= { .name
= "mac", .type
= BLOBMSG_TYPE_STRING
},
84 [NEIGHBOR_PROXY
]= { .name
= "proxy", .type
= BLOBMSG_TYPE_BOOL
},
85 [NEIGHBOR_ROUTER
]= {.name
= "router", .type
= BLOBMSG_TYPE_BOOL
},
88 const struct uci_blob_param_list neighbor_attr_list
= {
89 .n_params
= __NEIGHBOR_MAX
,
90 .params
= neighbor_attr
,
94 struct list_head prefixes
= LIST_HEAD_INIT(prefixes
);
95 static struct device_prefix
*ula_prefix
= NULL
;
96 static struct uloop_timeout valid_until_timeout
;
100 clear_if_addr(union if_addr
*a
, int mask
)
102 size_t m_bytes
= (mask
+ 7) / 8;
103 uint8_t m_clear
= (1 << (m_bytes
* 8 - mask
)) - 1;
104 uint8_t *p
= (uint8_t *) a
;
106 if (m_bytes
< sizeof(*a
))
107 memset(p
+ m_bytes
, 0, sizeof(*a
) - m_bytes
);
109 p
[m_bytes
- 1] &= ~m_clear
;
113 match_if_addr(union if_addr
*a1
, union if_addr
*a2
, int mask
)
115 union if_addr
*p1
, *p2
;
117 p1
= alloca(sizeof(*a1
));
118 p2
= alloca(sizeof(*a2
));
120 memcpy(p1
, a1
, sizeof(*a1
));
121 clear_if_addr(p1
, mask
);
122 memcpy(p2
, a2
, sizeof(*a2
));
123 clear_if_addr(p2
, mask
);
125 return !memcmp(p1
, p2
, sizeof(*p1
));
128 static int set_ip_source_policy(bool add
, bool v6
, unsigned int priority
,
129 const union if_addr
*addr
, uint8_t mask
, unsigned int table
,
130 struct interface
*in_iface
, const char *action
, bool src
)
132 struct iprule rule
= {
133 .flags
= IPRULE_PRIORITY
,
139 rule
.flags
|= IPRULE_SRC
;
140 rule
.src_addr
= *addr
;
141 rule
.src_mask
= mask
;
143 rule
.flags
|= IPRULE_DEST
;
144 rule
.dest_addr
= *addr
;
145 rule
.dest_mask
= mask
;
150 rule
.flags
|= IPRULE_LOOKUP
;
156 rule
.flags
|= IPRULE_ACTION
;
157 system_resolve_iprule_action(action
, &rule
.action
);
160 if (in_iface
&& in_iface
->l3_dev
.dev
) {
161 rule
.flags
|= IPRULE_IN
;
162 strcpy(rule
.in_dev
, in_iface
->l3_dev
.dev
->ifname
);
165 rule
.flags
|= (v6
) ? IPRULE_INET6
: IPRULE_INET4
;
167 return (add
) ? system_add_iprule(&rule
) : system_del_iprule(&rule
);
170 static int set_ip_lo_policy(bool add
, bool v6
, struct interface
*iface
)
172 struct iprule rule
= {
173 .flags
= IPRULE_IN
| IPRULE_LOOKUP
| IPRULE_PRIORITY
,
174 .priority
= IPRULE_PRIORITY_NW
+ iface
->l3_dev
.dev
->ifindex
,
175 .lookup
= (v6
) ? iface
->ip6table
: iface
->ip4table
,
182 rule
.flags
|= (v6
) ? IPRULE_INET6
: IPRULE_INET4
;
184 return (add
) ? system_add_iprule(&rule
) : system_del_iprule(&rule
);
188 __find_ip_addr_target(struct interface_ip_settings
*ip
, union if_addr
*a
, bool v6
)
190 struct device_addr
*addr
;
192 vlist_for_each_element(&ip
->addr
, addr
, node
) {
196 if (v6
!= ((addr
->flags
& DEVADDR_FAMILY
) == DEVADDR_INET6
))
199 if (((addr
->flags
& DEVADDR_FAMILY
) == DEVADDR_INET4
) &&
200 addr
->point_to_point
&& a
->in
.s_addr
== addr
->point_to_point
)
203 /* Handle offlink addresses correctly */
204 unsigned int mask
= addr
->mask
;
205 if ((addr
->flags
& DEVADDR_FAMILY
) == DEVADDR_INET6
&&
206 (addr
->flags
& DEVADDR_OFFLINK
))
209 if (!match_if_addr(&addr
->addr
, a
, mask
))
219 __find_ip_route_target(struct interface_ip_settings
*ip
, union if_addr
*a
,
220 bool v6
, struct device_route
**res
)
222 struct device_route
*route
;
224 vlist_for_each_element(&ip
->route
, route
, node
) {
228 if (v6
!= ((route
->flags
& DEVADDR_FAMILY
) == DEVADDR_INET6
))
231 if (!match_if_addr(&route
->addr
, a
, route
->mask
))
234 if (route
->flags
& DEVROUTE_TABLE
)
237 if (!*res
|| route
->mask
> (*res
)->mask
||
238 ((route
->mask
== (*res
)->mask
) && (route
->flags
& DEVROUTE_METRIC
)
239 && (route
->metric
< (*res
)->metric
)))
245 interface_ip_find_addr_target(struct interface
*iface
, union if_addr
*a
, bool v6
)
247 return __find_ip_addr_target(&iface
->proto_ip
, a
, v6
) ||
248 __find_ip_addr_target(&iface
->config_ip
, a
, v6
);
252 interface_ip_find_route_target(struct interface
*iface
, union if_addr
*a
,
253 bool v6
, struct device_route
**route
)
255 __find_ip_route_target(&iface
->proto_ip
, a
, v6
, route
);
256 __find_ip_route_target(&iface
->config_ip
, a
, v6
, route
);
260 interface_ip_add_target_route(union if_addr
*addr
, bool v6
, struct interface
*iface
,
263 struct device_route
*route
, *r_next
= NULL
;
264 bool defaultroute_target
= false;
265 union if_addr addr_zero
;
266 int addrsize
= v6
? sizeof(addr
->in6
) : sizeof(addr
->in
);
267 struct interface
*exclude_iface
= NULL
;
270 exclude_iface
= iface
;
274 memset(&addr_zero
, 0, sizeof(addr_zero
));
275 if (memcmp(&addr_zero
, addr
, addrsize
) == 0)
276 defaultroute_target
= true;
279 /* look for locally addressable target first */
280 if (interface_ip_find_addr_target(iface
, addr
, v6
))
283 /* do not stop at the first route, let the lookup compare
284 * masks to find the best match */
285 interface_ip_find_route_target(iface
, addr
, v6
, &r_next
);
287 vlist_for_each_element(&interfaces
, iface
, node
) {
288 if (iface
== exclude_iface
)
291 /* look for locally addressable target first */
292 if (interface_ip_find_addr_target(iface
, addr
, v6
))
295 /* do not stop at the first route, let the lookup compare
296 * masks to find the best match */
297 interface_ip_find_route_target(iface
, addr
, v6
, &r_next
);
304 iface
= r_next
->iface
;
305 if (defaultroute_target
)
308 route
= calloc(1, sizeof(*route
));
312 route
->flags
= v6
? DEVADDR_INET6
: DEVADDR_INET4
;
313 route
->mask
= v6
? 128 : 32;
314 memcpy(&route
->addr
, addr
, addrsize
);
315 memcpy(&route
->nexthop
, &r_next
->nexthop
, sizeof(route
->nexthop
));
316 route
->mtu
= r_next
->mtu
;
317 route
->metric
= r_next
->metric
;
318 route
->table
= r_next
->table
;
319 route
->iface
= iface
;
320 vlist_add(&iface
->host_routes
, &route
->node
, route
);
326 interface_set_route_info(struct interface
*iface
, struct device_route
*route
)
328 bool v6
= ((route
->flags
& DEVADDR_FAMILY
) == DEVADDR_INET6
);
333 if (!(route
->flags
& DEVROUTE_METRIC
))
334 route
->metric
= iface
->metric
;
336 if (!(route
->flags
& DEVROUTE_TABLE
)) {
337 route
->table
= (v6
) ? iface
->ip6table
: iface
->ip4table
;
339 route
->flags
|= DEVROUTE_SRCTABLE
;
344 interface_ip_add_neighbor(struct interface
*iface
, struct blob_attr
*attr
, bool v6
)
346 struct interface_ip_settings
*ip
;
347 struct blob_attr
*tb
[__NEIGHBOR_MAX
], *cur
;
348 struct device_neighbor
*neighbor
;
349 int af
= v6
? AF_INET6
: AF_INET
;
350 struct ether_addr
*ea
;
352 blobmsg_parse(neighbor_attr
, __NEIGHBOR_MAX
, tb
, blobmsg_data(attr
), blobmsg_data_len(attr
));
355 if ((cur
= tb
[NEIGHBOR_INTERFACE
]) == NULL
)
358 iface
= vlist_find(&interfaces
, blobmsg_data(cur
), iface
, node
);
363 ip
= &iface
->config_ip
;
365 ip
= &iface
->proto_ip
;
367 neighbor
= calloc(1,sizeof(*neighbor
));
371 neighbor
->flags
= v6
? DEVADDR_INET6
: DEVADDR_INET4
;
373 if ((cur
= tb
[NEIGHBOR_ADDRESS
]) != NULL
){
374 if (!inet_pton(af
, blobmsg_data(cur
), &neighbor
->addr
))
379 if ((cur
= tb
[NEIGHBOR_MAC
]) != NULL
) {
380 neighbor
->flags
|= DEVNEIGH_MAC
;
381 ea
= ether_aton(blobmsg_data(cur
));
385 memcpy(neighbor
->macaddr
, ea
, 6);
388 if ((cur
= tb
[NEIGHBOR_PROXY
]) != NULL
)
389 neighbor
->proxy
= blobmsg_get_bool(cur
);
391 if ((cur
= tb
[NEIGHBOR_ROUTER
]) != NULL
)
392 neighbor
->router
= blobmsg_get_bool(cur
);
394 vlist_add(&ip
->neighbor
, &neighbor
->node
, neighbor
);
402 interface_ip_add_route(struct interface
*iface
, struct blob_attr
*attr
, bool v6
)
404 struct interface_ip_settings
*ip
;
405 struct blob_attr
*tb
[__ROUTE_MAX
], *cur
;
406 struct device_route
*route
;
407 int af
= v6
? AF_INET6
: AF_INET
;
409 blobmsg_parse(route_attr
, __ROUTE_MAX
, tb
, blobmsg_data(attr
), blobmsg_data_len(attr
));
411 if ((cur
= tb
[ROUTE_DISABLED
]) != NULL
&& blobmsg_get_bool(cur
))
415 if ((cur
= tb
[ROUTE_INTERFACE
]) == NULL
)
418 iface
= vlist_find(&interfaces
, blobmsg_data(cur
), iface
, node
);
422 ip
= &iface
->config_ip
;
424 ip
= &iface
->proto_ip
;
427 route
= calloc(1, sizeof(*route
));
431 route
->flags
= v6
? DEVADDR_INET6
: DEVADDR_INET4
;
432 route
->mask
= v6
? 128 : 32;
433 if ((cur
= tb
[ROUTE_MASK
]) != NULL
) {
434 route
->mask
= parse_netmask_string(blobmsg_data(cur
), v6
);
435 if (route
->mask
> (v6
? 128 : 32))
439 if ((cur
= tb
[ROUTE_TARGET
]) != NULL
) {
440 if (!parse_ip_and_netmask(af
, blobmsg_data(cur
), &route
->addr
, &route
->mask
)) {
441 DPRINTF("Failed to parse route target: %s\n", (char *) blobmsg_data(cur
));
446 if ((cur
= tb
[ROUTE_GATEWAY
]) != NULL
) {
447 if (!inet_pton(af
, blobmsg_data(cur
), &route
->nexthop
)) {
448 DPRINTF("Failed to parse route gateway: %s\n", (char *) blobmsg_data(cur
));
453 if ((cur
= tb
[ROUTE_METRIC
]) != NULL
) {
454 route
->metric
= blobmsg_get_u32(cur
);
455 route
->flags
|= DEVROUTE_METRIC
;
458 if ((cur
= tb
[ROUTE_MTU
]) != NULL
) {
459 route
->mtu
= blobmsg_get_u32(cur
);
460 route
->flags
|= DEVROUTE_MTU
;
463 /* Use source-based routing */
464 if ((cur
= tb
[ROUTE_SOURCE
]) != NULL
) {
465 char *saveptr
, *source
= alloca(blobmsg_data_len(cur
));
466 memcpy(source
, blobmsg_data(cur
), blobmsg_data_len(cur
));
468 const char *addr
= strtok_r(source
, "/", &saveptr
);
469 const char *mask
= strtok_r(NULL
, "/", &saveptr
);
471 if (!addr
|| inet_pton(af
, addr
, &route
->source
) < 1) {
472 DPRINTF("Failed to parse route source: %s\n", addr
? addr
: "NULL");
476 route
->sourcemask
= (mask
) ? atoi(mask
) : ((af
== AF_INET6
) ? 128 : 32);
479 if ((cur
= tb
[ROUTE_ONLINK
]) != NULL
&& blobmsg_get_bool(cur
))
480 route
->flags
|= DEVROUTE_ONLINK
;
482 if ((cur
= tb
[ROUTE_TABLE
]) != NULL
) {
483 if (!system_resolve_rt_table(blobmsg_data(cur
), &route
->table
)) {
484 DPRINTF("Failed to resolve routing table: %s\n", (char *) blobmsg_data(cur
));
488 /* only set the table flag if not using the main (default) table */
489 if (system_is_default_rt_table(route
->table
))
493 route
->flags
|= DEVROUTE_TABLE
;
496 if ((cur
= tb
[ROUTE_VALID
]) != NULL
) {
497 int64_t valid
= blobmsg_get_u32(cur
);
498 int64_t valid_until
= valid
+ (int64_t)system_get_rtime();
499 if (valid_until
<= LONG_MAX
&& valid
!= 0xffffffffLL
) /* Catch overflow */
500 route
->valid_until
= valid_until
;
503 if ((cur
= tb
[ROUTE_TYPE
]) != NULL
) {
504 if (!system_resolve_rt_type(blobmsg_data(cur
), &route
->type
)) {
505 DPRINTF("Failed to resolve routing type: %s\n", (char *) blobmsg_data(cur
));
508 route
->flags
|= DEVROUTE_TYPE
;
511 if ((cur
= tb
[ROUTE_PROTO
]) != NULL
) {
512 if (!system_resolve_rt_proto(blobmsg_data(cur
), &route
->proto
)) {
513 DPRINTF("Failed to resolve proto type: %s\n", (char *) blobmsg_data(cur
));
516 route
->flags
|= DEVROUTE_PROTO
;
519 interface_set_route_info(iface
, route
);
520 vlist_add(&ip
->route
, &route
->node
, route
);
528 addr_cmp(const void *k1
, const void *k2
, void *ptr
)
530 const struct device_addr
*a1
= k1
;
531 const struct device_addr
*a2
= k2
;
532 const int cmp_offset
= offsetof(struct device_addr
, flags
);
533 const int cmp_size
= sizeof(struct device_addr
) - cmp_offset
;
535 if (a1
->index
!= a2
->index
)
536 return a1
->index
- a2
->index
;
537 return memcmp(k1
+cmp_offset
, k2
+cmp_offset
, cmp_size
);
541 neighbor_cmp(const void *k1
, const void *k2
, void *ptr
)
543 const struct device_neighbor
*n1
= k1
, *n2
= k2
;
545 return memcmp(&n1
->addr
, &n2
->addr
, sizeof(n2
->addr
));
549 route_cmp(const void *k1
, const void *k2
, void *ptr
)
551 const struct device_route
*r1
= k1
, *r2
= k2
;
553 if (r1
->mask
!= r2
->mask
)
554 return r2
->mask
- r1
->mask
;
556 if (r1
->metric
!= r2
->metric
)
557 return r1
->metric
- r2
->metric
;
559 if (r1
->flags
!= r2
->flags
)
560 return r2
->flags
- r1
->flags
;
562 if (r1
->sourcemask
!= r2
->sourcemask
)
563 return r1
->sourcemask
- r2
->sourcemask
;
565 if (r1
->table
!= r2
->table
)
566 return r1
->table
- r2
->table
;
568 int maskcmp
= memcmp(&r1
->source
, &r2
->source
, sizeof(r1
->source
));
572 return memcmp(&r1
->addr
, &r2
->addr
, sizeof(r1
->addr
));
576 prefix_cmp(const void *k1
, const void *k2
, void *ptr
)
578 return memcmp(k1
, k2
, offsetof(struct device_prefix
, pclass
) -
579 offsetof(struct device_prefix
, addr
));
583 interface_handle_subnet_route(struct interface
*iface
, struct device_addr
*addr
, bool add
)
585 struct device
*dev
= iface
->l3_dev
.dev
;
586 struct device_route
*r
= &addr
->subnet
;
588 if (addr
->flags
& DEVADDR_OFFLINK
)
592 if (!addr
->subnet
.iface
)
595 system_del_route(dev
, r
);
596 memset(r
, 0, sizeof(*r
));
601 r
->flags
= addr
->flags
;
602 r
->mask
= addr
->mask
;
603 memcpy(&r
->addr
, &addr
->addr
, sizeof(r
->addr
));
604 clear_if_addr(&r
->addr
, r
->mask
);
606 if (!system_resolve_rt_proto("kernel", &r
->proto
))
609 r
->flags
|= DEVROUTE_PROTO
;
610 system_del_route(dev
, r
);
612 r
->flags
&= ~DEVROUTE_PROTO
;
613 interface_set_route_info(iface
, r
);
615 system_add_route(dev
, r
);
619 interface_add_addr_rules(struct device_addr
*addr
, bool enabled
)
621 bool v6
= (addr
->flags
& DEVADDR_FAMILY
) == DEVADDR_INET6
;
623 set_ip_source_policy(enabled
, v6
, IPRULE_PRIORITY_ADDR
, &addr
->addr
,
624 (v6
) ? 128 : 32, addr
->policy_table
, NULL
, NULL
,
626 set_ip_source_policy(enabled
, v6
, IPRULE_PRIORITY_ADDR_MASK
,
627 &addr
->addr
, addr
->mask
, addr
->policy_table
, NULL
,
632 interface_update_proto_addr(struct vlist_tree
*tree
,
633 struct vlist_node
*node_new
,
634 struct vlist_node
*node_old
)
636 struct interface_ip_settings
*ip
;
637 struct interface
*iface
;
639 struct device_addr
*a_new
= NULL
, *a_old
= NULL
;
640 bool replace
= false;
644 ip
= container_of(tree
, struct interface_ip_settings
, addr
);
646 dev
= iface
->l3_dev
.dev
;
648 if (!node_new
|| !node_old
)
649 iface
->updated
|= IUF_ADDRESS
;
652 a_new
= container_of(node_new
, struct device_addr
, node
);
654 if ((a_new
->flags
& DEVADDR_FAMILY
) == DEVADDR_INET4
&&
657 /* /31 and /32 addressing need 255.255.255.255
658 * as broadcast address. */
659 if (a_new
->mask
>= 31) {
660 a_new
->broadcast
= (uint32_t) ~0;
663 uint32_t *a
= (uint32_t *) &a_new
->addr
;
665 mask
>>= a_new
->mask
;
666 a_new
->broadcast
= *a
| htonl(mask
);
672 a_old
= container_of(node_old
, struct device_addr
, node
);
674 if (a_new
&& a_old
) {
677 if (a_old
->flags
!= a_new
->flags
|| a_old
->failed
)
680 if (a_old
->valid_until
!= a_new
->valid_until
||
681 a_old
->preferred_until
!= a_new
->preferred_until
)
684 if (((a_new
->flags
& DEVADDR_FAMILY
) == DEVADDR_INET4
) &&
685 (a_new
->broadcast
!= a_old
->broadcast
||
686 a_new
->point_to_point
!= a_old
->point_to_point
))
691 if (a_old
->enabled
&& !keep
) {
693 * This is needed for source routing to work correctly. If a device
694 * has two connections to a network using the same subnet, adding
695 * only the network-rule will cause packets to be routed through the
696 * first matching network (source IP matches both masks)
698 if (a_old
->policy_table
)
699 interface_add_addr_rules(a_old
, false);
701 if (!(a_old
->flags
& DEVADDR_EXTERNAL
)) {
702 interface_handle_subnet_route(iface
, a_old
, false);
703 system_del_address(dev
, a_old
);
705 if ((a_old
->flags
& DEVADDR_OFFLINK
) && (a_old
->mask
< (v6
? 128 : 32))) {
706 struct device_route route
;
708 memset(&route
, 0, sizeof(route
));
709 route
.flags
= v6
? DEVADDR_INET6
: DEVADDR_INET4
;
710 route
.metric
= INT32_MAX
;
711 route
.mask
= a_old
->mask
;
712 route
.addr
= a_old
->addr
;
714 clear_if_addr(&route
.addr
, route
.mask
);
716 /* Delete null-route */
717 system_del_route(NULL
, &route
);
727 a_new
->enabled
= true;
729 if ((a_new
->flags
& DEVADDR_FAMILY
) == DEVADDR_INET6
)
732 a_new
->policy_table
= (v6
) ? iface
->ip6table
: iface
->ip4table
;
734 if (!keep
|| replace
) {
735 if (!(a_new
->flags
& DEVADDR_EXTERNAL
)) {
736 if (system_add_address(dev
, a_new
))
737 a_new
->failed
= true;
739 if (iface
->metric
|| a_new
->policy_table
)
740 interface_handle_subnet_route(iface
, a_new
, true);
744 if (!(a_new
->flags
& DEVADDR_EXTERNAL
) &&
745 (a_new
->flags
& DEVADDR_OFFLINK
) &&
746 (a_new
->mask
< (v6
? 128 : 32))) {
747 struct device_route route
;
749 memset(&route
, 0, sizeof(route
));
750 route
.flags
= v6
? DEVADDR_INET6
: DEVADDR_INET4
;
751 route
.metric
= INT32_MAX
;
752 route
.mask
= a_new
->mask
;
753 route
.addr
= a_new
->addr
;
755 clear_if_addr(&route
.addr
, route
.mask
);
758 * In case off link is specifed as address property
759 * add null-route to avoid routing loops
761 system_add_route(NULL
, &route
);
764 if (a_new
->policy_table
)
765 interface_add_addr_rules(a_new
, true);
772 enable_route(struct interface_ip_settings
*ip
, struct device_route
*route
)
774 if (ip
->no_defaultroute
&& !route
->mask
)
781 interface_update_proto_neighbor(struct vlist_tree
*tree
,
782 struct vlist_node
* node_new
,
783 struct vlist_node
*node_old
)
786 struct device_neighbor
*neighbor_old
, *neighbor_new
;
787 struct interface_ip_settings
*ip
;
790 ip
= container_of(tree
, struct interface_ip_settings
, neighbor
);
791 dev
= ip
->iface
->l3_dev
.dev
;
793 neighbor_old
= container_of(node_old
, struct device_neighbor
, node
);
794 neighbor_new
= container_of(node_new
, struct device_neighbor
, node
);
796 if (node_old
&& node_new
) {
797 keep
= (!memcmp(neighbor_old
->macaddr
, neighbor_new
->macaddr
, sizeof(neighbor_old
->macaddr
)) &&
798 (neighbor_old
->proxy
== neighbor_new
->proxy
) &&
799 (neighbor_old
->router
== neighbor_new
->router
));
803 if (!keep
&& neighbor_old
->enabled
)
804 system_del_neighbor(dev
, neighbor_old
);
810 if (!keep
&& ip
->enabled
)
811 if (system_add_neighbor(dev
, neighbor_new
))
812 neighbor_new
->failed
= true;
814 neighbor_new
->enabled
= ip
->enabled
;
819 __interface_update_route(struct interface_ip_settings
*ip
,
820 struct vlist_node
*node_new
,
821 struct vlist_node
*node_old
)
823 struct interface
*iface
= ip
->iface
;
825 struct device_route
*route_old
, *route_new
;
828 dev
= iface
->l3_dev
.dev
;
830 if (!node_new
|| !node_old
)
831 iface
->updated
|= IUF_ROUTE
;
833 route_old
= container_of(node_old
, struct device_route
, node
);
834 route_new
= container_of(node_new
, struct device_route
, node
);
836 if (node_old
&& node_new
)
837 keep
= !memcmp(&route_old
->nexthop
, &route_new
->nexthop
, sizeof(route_old
->nexthop
)) &&
838 (route_old
->mtu
== route_new
->mtu
) && (route_old
->type
== route_new
->type
) &&
839 (route_old
->proto
== route_new
->proto
) && !route_old
->failed
;
842 if (!(route_old
->flags
& DEVADDR_EXTERNAL
) && route_old
->enabled
&& !keep
)
843 system_del_route(dev
, route_old
);
849 bool _enabled
= enable_route(ip
, route_new
);
851 if (!(route_new
->flags
& DEVADDR_EXTERNAL
) && !keep
&& _enabled
)
852 if (system_add_route(dev
, route_new
))
853 route_new
->failed
= true;
855 route_new
->iface
= iface
;
856 route_new
->enabled
= _enabled
;
861 interface_update_proto_route(struct vlist_tree
*tree
,
862 struct vlist_node
*node_new
,
863 struct vlist_node
*node_old
)
865 struct interface_ip_settings
*ip
;
867 ip
= container_of(tree
, struct interface_ip_settings
, route
);
868 __interface_update_route(ip
, node_new
, node_old
);
872 interface_update_host_route(struct vlist_tree
*tree
,
873 struct vlist_node
*node_new
,
874 struct vlist_node
*node_old
)
876 struct interface
*iface
;
878 iface
= container_of(tree
, struct interface
, host_routes
);
879 __interface_update_route(&iface
->proto_ip
, node_new
, node_old
);
883 random_ifaceid(struct in6_addr
*addr
)
885 static bool initialized
= false;
890 gettimeofday(&t
, NULL
);
891 seed
= t
.tv_sec
^ t
.tv_usec
^ getpid();
895 addr
->s6_addr32
[2] = (uint32_t)mrand48();
896 addr
->s6_addr32
[3] = (uint32_t)mrand48();
900 eui64_ifaceid(struct interface
*iface
, struct in6_addr
*addr
)
902 struct device_settings st
;
904 device_merge_settings(iface
->l3_dev
.dev
, &st
);
906 if (!(st
.flags
& DEV_OPT_MACADDR
))
909 /* get mac address */
910 uint8_t *ifaceid
= addr
->s6_addr
+ 8;
911 memcpy(ifaceid
, st
.macaddr
, 3);
912 memcpy(ifaceid
+ 5, st
.macaddr
+ 3, 3);
921 generate_ifaceid(struct interface
*iface
, struct in6_addr
*addr
)
925 /* generate new iface id */
926 switch (iface
->assignment_iface_id_selection
) {
929 /* copy host part from assignment_fixed_iface_id */
930 memcpy(addr
->s6_addr
+ 8, iface
->assignment_fixed_iface_id
.s6_addr
+ 8, 8);
933 /* randomize last 64 bits */
934 random_ifaceid(addr
);
938 ret
= eui64_ifaceid(iface
, addr
);
948 interface_set_prefix_address(struct device_prefix_assignment
*assignment
,
949 const struct device_prefix
*prefix
, struct interface
*iface
, bool add
)
951 const struct interface
*uplink
= prefix
->iface
;
952 if (!iface
->l3_dev
.dev
)
955 struct device
*l3_downlink
= iface
->l3_dev
.dev
;
957 struct device_addr addr
;
958 struct device_route route
;
959 memset(&addr
, 0, sizeof(addr
));
960 memset(&route
, 0, sizeof(route
));
962 addr
.addr
.in6
= assignment
->addr
;
963 addr
.mask
= assignment
->length
;
964 addr
.flags
= DEVADDR_INET6
;
965 addr
.preferred_until
= prefix
->preferred_until
;
966 addr
.valid_until
= prefix
->valid_until
;
968 route
.flags
= DEVADDR_INET6
;
969 route
.mask
= addr
.mask
< 64 ? 64 : addr
.mask
;
970 route
.addr
= addr
.addr
;
972 if (!add
&& assignment
->enabled
) {
973 time_t now
= system_get_rtime();
975 if (addr
.valid_until
&& addr
.valid_until
- 1 <= now
) {
976 addr
.valid_until
= 0;
977 addr
.preferred_until
= 0;
979 /* Address is still valid; pass its ownership to kernel (see L-14 RFC 7084). */
980 addr
.preferred_until
= now
;
982 if (!addr
.valid_until
|| addr
.valid_until
> now
+ 7200)
983 addr
.valid_until
= now
+ 7200;
987 set_ip_source_policy(false, true, IPRULE_PRIORITY_ADDR_MASK
, &addr
.addr
,
988 addr
.mask
< 64 ? 64 : addr
.mask
, iface
->ip6table
, NULL
, NULL
, false);
991 if (prefix
->iface
->ip6table
)
992 set_ip_source_policy(false, true, IPRULE_PRIORITY_NW
, &addr
.addr
,
993 addr
.mask
, prefix
->iface
->ip6table
, iface
, NULL
, true);
995 set_ip_source_policy(false, true, IPRULE_PRIORITY_REJECT
, &addr
.addr
,
996 addr
.mask
, 0, iface
, "unreachable", true);
999 clear_if_addr(&route
.addr
, route
.mask
);
1000 interface_set_route_info(iface
, &route
);
1002 system_del_route(l3_downlink
, &route
);
1003 if (addr
.valid_until
)
1004 system_add_address(l3_downlink
, &addr
);
1006 system_del_address(l3_downlink
, &addr
);
1008 assignment
->addr
= in6addr_any
;
1009 assignment
->enabled
= false;
1010 } else if (add
&& (iface
->state
== IFS_UP
|| iface
->state
== IFS_SETUP
)) {
1011 if (IN6_IS_ADDR_UNSPECIFIED(&addr
.addr
.in6
)) {
1012 addr
.addr
.in6
= prefix
->addr
;
1013 addr
.addr
.in6
.s6_addr32
[1] |= htonl(assignment
->assigned
);
1014 if (!generate_ifaceid(iface
, &addr
.addr
.in6
))
1017 assignment
->addr
= addr
.addr
.in6
;
1018 route
.addr
= addr
.addr
;
1021 addr
.flags
|= DEVADDR_OFFLINK
;
1022 if (system_add_address(l3_downlink
, &addr
))
1025 if (!assignment
->enabled
) {
1026 if (iface
->ip6table
)
1027 set_ip_source_policy(true, true, IPRULE_PRIORITY_ADDR_MASK
, &addr
.addr
,
1028 addr
.mask
< 64 ? 64 : addr
.mask
, iface
->ip6table
, NULL
, NULL
, false);
1030 if (prefix
->iface
) {
1031 set_ip_source_policy(true, true, IPRULE_PRIORITY_REJECT
, &addr
.addr
,
1032 addr
.mask
, 0, iface
, "unreachable", true);
1034 if (prefix
->iface
->ip6table
)
1035 set_ip_source_policy(true, true, IPRULE_PRIORITY_NW
, &addr
.addr
,
1036 addr
.mask
, prefix
->iface
->ip6table
, iface
, NULL
, true);
1040 clear_if_addr(&route
.addr
, route
.mask
);
1041 interface_set_route_info(iface
, &route
);
1043 system_add_route(l3_downlink
, &route
);
1045 if (uplink
&& uplink
->l3_dev
.dev
&& !(l3_downlink
->settings
.flags
& DEV_OPT_MTU6
)) {
1046 int mtu
= system_update_ipv6_mtu(uplink
->l3_dev
.dev
, 0);
1047 int mtu_old
= system_update_ipv6_mtu(l3_downlink
, 0);
1049 if (mtu
> 0 && mtu_old
!= mtu
) {
1050 if (system_update_ipv6_mtu(l3_downlink
, mtu
) < 0 && mtu
< mtu_old
)
1051 netifd_log_message(L_WARNING
, "Failed to set IPv6 mtu to %d "
1052 "on interface '%s'\n", mtu
, iface
->name
);
1056 assignment
->enabled
= true;
1060 static bool interface_prefix_assign(struct list_head
*list
,
1061 struct device_prefix_assignment
*assign
)
1063 int32_t current
= 0, asize
= (1 << (64 - assign
->length
)) - 1;
1064 struct device_prefix_assignment
*c
;
1065 list_for_each_entry(c
, list
, head
) {
1066 if (assign
->assigned
!= -1) {
1067 if (assign
->assigned
>= current
&& assign
->assigned
+ asize
< c
->assigned
) {
1068 list_add_tail(&assign
->head
, &c
->head
);
1071 } else if (assign
->assigned
== -1) {
1072 current
= (current
+ asize
) & (~asize
);
1073 if (current
+ asize
< c
->assigned
) {
1074 assign
->assigned
= current
;
1075 list_add_tail(&assign
->head
, &c
->head
);
1079 current
= (c
->assigned
+ (1 << (64 - c
->length
)));
1085 * Sorting of assignment entries:
1086 * Primary on assignment length: smallest assignment first
1087 * Secondary on assignment weight: highest weight first
1088 * Finally alphabetical order of interface names
1090 static int prefix_assignment_cmp(const void *k1
, const void *k2
, void *ptr
)
1092 const struct device_prefix_assignment
*a1
= k1
, *a2
= k2
;
1094 if (a1
->length
!= a2
->length
)
1095 return a1
->length
- a2
->length
;
1097 if (a1
->weight
!= a2
->weight
)
1098 return a2
->weight
- a1
->weight
;
1100 return strcmp(a1
->name
, a2
->name
);
1103 static void interface_update_prefix_assignments(struct device_prefix
*prefix
, bool setup
)
1105 struct device_prefix_assignment
*c
;
1106 struct interface
*iface
;
1108 /* Delete all assignments */
1109 while (!list_empty(&prefix
->assignments
)) {
1110 c
= list_first_entry(&prefix
->assignments
,
1111 struct device_prefix_assignment
, head
);
1112 if ((iface
= vlist_find(&interfaces
, c
->name
, iface
, node
)))
1113 interface_set_prefix_address(c
, prefix
, iface
, false);
1121 /* End-of-assignment sentinel */
1122 c
= malloc(sizeof(*c
) + 1);
1126 c
->assigned
= 1 << (64 - prefix
->length
);
1129 c
->addr
= in6addr_any
;
1130 list_add(&c
->head
, &prefix
->assignments
);
1132 /* Excluded prefix */
1133 if (prefix
->excl_length
> 0) {
1134 const char name
[] = "!excluded";
1135 c
= malloc(sizeof(*c
) + sizeof(name
));
1137 c
->assigned
= ntohl(prefix
->excl_addr
.s6_addr32
[1]) &
1138 ((1 << (64 - prefix
->length
)) - 1);
1139 c
->length
= prefix
->excl_length
;
1140 c
->addr
= in6addr_any
;
1141 memcpy(c
->name
, name
, sizeof(name
));
1142 list_add(&c
->head
, &prefix
->assignments
);
1146 bool assigned_any
= false;
1148 struct avl_node node
;
1150 struct avl_tree assign_later
;
1152 avl_init(&assign_later
, prefix_assignment_cmp
, false, NULL
);
1154 vlist_for_each_element(&interfaces
, iface
, node
) {
1155 if (iface
->assignment_length
< 48 ||
1156 iface
->assignment_length
> 64)
1159 /* Test whether there is a matching class */
1160 if (!list_empty(&iface
->assignment_classes
)) {
1163 struct interface_assignment_class
*c
;
1164 list_for_each_entry(c
, &iface
->assignment_classes
, head
) {
1165 if (!strcmp(c
->name
, prefix
->pclass
)) {
1175 size_t namelen
= strlen(iface
->name
) + 1;
1176 c
= malloc(sizeof(*c
) + namelen
);
1180 c
->length
= iface
->assignment_length
;
1181 c
->assigned
= iface
->assignment_hint
;
1182 c
->weight
= iface
->assignment_weight
;
1183 c
->addr
= in6addr_any
;
1185 memcpy(c
->name
, iface
->name
, namelen
);
1187 /* First process all custom assignments, put all others in later-list */
1188 if (c
->assigned
== -1 || !interface_prefix_assign(&prefix
->assignments
, c
)) {
1189 if (c
->assigned
!= -1) {
1191 netifd_log_message(L_WARNING
, "Failed to assign requested subprefix "
1192 "of size %hhu for %s, trying other\n", c
->length
, c
->name
);
1195 entry
= calloc(1, sizeof(*entry
));
1201 entry
->node
.key
= c
;
1202 avl_insert(&assign_later
, &entry
->node
);
1205 if (c
->assigned
!= -1)
1206 assigned_any
= true;
1209 /* Then try to assign all other + failed custom assignments */
1210 avl_for_each_element_safe(&assign_later
, entry
, node
, n_entry
) {
1211 bool assigned
= false;
1213 c
= (struct device_prefix_assignment
*)entry
->node
.key
;
1214 avl_delete(&assign_later
, &entry
->node
);
1217 assigned
= interface_prefix_assign(&prefix
->assignments
, c
);
1218 } while (!assigned
&& ++c
->length
<= 64);
1221 netifd_log_message(L_WARNING
, "Failed to assign subprefix "
1222 "of size %hhu for %s\n", c
->length
, c
->name
);
1225 assigned_any
= true;
1230 list_for_each_entry(c
, &prefix
->assignments
, head
)
1231 if ((iface
= vlist_find(&interfaces
, c
->name
, iface
, node
)))
1232 interface_set_prefix_address(c
, prefix
, iface
, true);
1235 netifd_log_message(L_WARNING
, "You have delegated IPv6-prefixes but haven't assigned them "
1236 "to any interface. Did you forget to set option ip6assign on your lan-interfaces?");
1240 void interface_refresh_assignments(bool hint
)
1242 static bool refresh
= false;
1243 if (!hint
&& refresh
) {
1244 struct device_prefix
*p
;
1245 time_t now
= system_get_rtime();
1247 list_for_each_entry(p
, &prefixes
, head
) {
1248 bool valid
= !(p
->valid_until
&& p
->valid_until
- 1 <= now
);
1250 interface_update_prefix_assignments(p
, valid
);
1256 void interface_update_prefix_delegation(struct interface_ip_settings
*ip
)
1258 struct device_prefix
*prefix
;
1259 time_t now
= system_get_rtime();
1261 vlist_for_each_element(&ip
->prefix
, prefix
, node
) {
1262 bool valid
= !(prefix
->valid_until
&& prefix
->valid_until
- 1 <= now
);
1264 interface_update_prefix_assignments(prefix
, !ip
->no_delegation
&& valid
);
1266 if (ip
->no_delegation
) {
1267 if (prefix
->head
.next
)
1268 list_del(&prefix
->head
);
1270 list_add(&prefix
->head
, &prefixes
);
1275 interface_update_prefix(struct vlist_tree
*tree
,
1276 struct vlist_node
*node_new
,
1277 struct vlist_node
*node_old
)
1279 struct device_prefix
*prefix_old
, *prefix_new
;
1280 prefix_old
= container_of(node_old
, struct device_prefix
, node
);
1281 prefix_new
= container_of(node_new
, struct device_prefix
, node
);
1283 struct interface_ip_settings
*ip
= container_of(tree
, struct interface_ip_settings
, prefix
);
1284 if (tree
&& (!node_new
|| !node_old
))
1285 ip
->iface
->updated
|= IUF_PREFIX
;
1287 struct device_route route
;
1288 memset(&route
, 0, sizeof(route
));
1289 route
.flags
= DEVADDR_INET6
;
1290 route
.metric
= INT32_MAX
;
1291 route
.mask
= (node_new
) ? prefix_new
->length
: prefix_old
->length
;
1292 route
.addr
.in6
= (node_new
) ? prefix_new
->addr
: prefix_old
->addr
;
1294 struct device_prefix_assignment
*c
;
1295 struct interface
*iface
;
1296 bool new_valid
= node_new
&& !(prefix_new
->valid_until
&& prefix_new
->valid_until
- 1 <= system_get_rtime());
1298 if (node_old
&& node_new
) {
1299 /* Move assignments and refresh addresses to update valid times */
1300 list_splice(&prefix_old
->assignments
, &prefix_new
->assignments
);
1302 list_for_each_entry(c
, &prefix_new
->assignments
, head
)
1303 if ((iface
= vlist_find(&interfaces
, c
->name
, iface
, node
)))
1304 interface_set_prefix_address(c
, prefix_new
, iface
, new_valid
);
1306 if (prefix_new
->preferred_until
!= prefix_old
->preferred_until
||
1307 prefix_new
->valid_until
!= prefix_old
->valid_until
)
1308 ip
->iface
->updated
|= IUF_PREFIX
;
1309 } else if (node_new
) {
1310 /* Set null-route to avoid routing loops */
1311 system_add_route(NULL
, &route
);
1313 if (!prefix_new
->iface
|| !prefix_new
->iface
->proto_ip
.no_delegation
)
1314 interface_update_prefix_assignments(prefix_new
, new_valid
);
1315 } else if (node_old
) {
1316 /* Remove null-route */
1317 interface_update_prefix_assignments(prefix_old
, false);
1318 system_del_route(NULL
, &route
);
1322 if (prefix_old
->head
.next
)
1323 list_del(&prefix_old
->head
);
1327 if (node_new
&& (!prefix_new
->iface
|| !prefix_new
->iface
->proto_ip
.no_delegation
))
1328 list_add(&prefix_new
->head
, &prefixes
);
1332 struct device_prefix
*
1333 interface_ip_add_device_prefix(struct interface
*iface
, struct in6_addr
*addr
,
1334 uint8_t length
, time_t valid_until
, time_t preferred_until
,
1335 struct in6_addr
*excl_addr
, uint8_t excl_length
, const char *pclass
)
1337 union if_addr a
= { .in6
= *addr
};
1340 pclass
= (iface
) ? iface
->name
: "local";
1342 struct device_prefix
*prefix
= calloc(1, sizeof(*prefix
) + strlen(pclass
) + 1);
1346 clear_if_addr(&a
, length
);
1348 prefix
->length
= length
;
1349 prefix
->addr
= a
.in6
;
1350 prefix
->preferred_until
= preferred_until
;
1351 prefix
->valid_until
= valid_until
;
1352 prefix
->iface
= iface
;
1353 INIT_LIST_HEAD(&prefix
->assignments
);
1356 prefix
->excl_addr
= *excl_addr
;
1357 prefix
->excl_length
= excl_length
;
1360 strcpy(prefix
->pclass
, pclass
);
1363 vlist_add(&iface
->proto_ip
.prefix
, &prefix
->node
, &prefix
->addr
);
1365 interface_update_prefix(NULL
, &prefix
->node
, NULL
);
1371 interface_ip_set_ula_prefix(const char *prefix
)
1373 char buf
[INET6_ADDRSTRLEN
+ 4] = {0}, *saveptr
;
1375 strncpy(buf
, prefix
, sizeof(buf
) - 1);
1376 char *prefixaddr
= strtok_r(buf
, "/", &saveptr
);
1378 struct in6_addr addr
;
1379 if (!prefixaddr
|| inet_pton(AF_INET6
, prefixaddr
, &addr
) < 1) {
1381 interface_update_prefix(NULL
, NULL
, &ula_prefix
->node
);
1388 char *prefixlen
= strtok_r(NULL
, ",", &saveptr
);
1389 if (!prefixlen
|| (length
= atoi(prefixlen
)) < 1 || length
> 64)
1392 if (!ula_prefix
|| !IN6_ARE_ADDR_EQUAL(&addr
, &ula_prefix
->addr
) ||
1393 ula_prefix
->length
!= length
) {
1395 interface_update_prefix(NULL
, NULL
, &ula_prefix
->node
);
1397 ula_prefix
= interface_ip_add_device_prefix(NULL
, &addr
, length
,
1398 0, 0, NULL
, 0, NULL
);
1403 interface_add_dns_server(struct interface_ip_settings
*ip
, const char *str
)
1405 struct dns_server
*s
;
1407 s
= calloc(1, sizeof(*s
));
1412 if (inet_pton(s
->af
, str
, &s
->addr
.in
))
1416 if (inet_pton(s
->af
, str
, &s
->addr
.in
))
1423 D(INTERFACE
, "Add IPv%c DNS server: %s\n",
1424 s
->af
== AF_INET6
? '6' : '4', str
);
1425 vlist_simple_add(&ip
->dns_servers
, &s
->node
);
1429 interface_add_dns_server_list(struct interface_ip_settings
*ip
, struct blob_attr
*list
)
1431 struct blob_attr
*cur
;
1434 blobmsg_for_each_attr(cur
, list
, rem
) {
1435 if (blobmsg_type(cur
) != BLOBMSG_TYPE_STRING
)
1438 if (!blobmsg_check_attr(cur
, false))
1441 interface_add_dns_server(ip
, blobmsg_data(cur
));
1446 interface_add_dns_search_domain(struct interface_ip_settings
*ip
, const char *str
)
1448 struct dns_search_domain
*s
;
1449 int len
= strlen(str
);
1451 s
= calloc(1, sizeof(*s
) + len
+ 1);
1455 D(INTERFACE
, "Add DNS search domain: %s\n", str
);
1456 memcpy(s
->name
, str
, len
);
1457 vlist_simple_add(&ip
->dns_search
, &s
->node
);
1461 interface_add_dns_search_list(struct interface_ip_settings
*ip
, struct blob_attr
*list
)
1463 struct blob_attr
*cur
;
1466 blobmsg_for_each_attr(cur
, list
, rem
) {
1467 if (blobmsg_type(cur
) != BLOBMSG_TYPE_STRING
)
1470 if (!blobmsg_check_attr(cur
, false))
1473 interface_add_dns_search_domain(ip
, blobmsg_data(cur
));
1478 write_resolv_conf_entries(FILE *f
, struct interface_ip_settings
*ip
, const char *dev
)
1480 struct dns_server
*s
;
1481 struct dns_search_domain
*d
;
1483 char buf
[INET6_ADDRSTRLEN
];
1485 vlist_simple_for_each_element(&ip
->dns_servers
, s
, node
) {
1486 str
= inet_ntop(s
->af
, &s
->addr
, buf
, sizeof(buf
));
1490 if (s
->af
== AF_INET6
&& IN6_IS_ADDR_LINKLOCAL(&s
->addr
.in6
))
1491 fprintf(f
, "nameserver %s%%%s\n", str
, dev
);
1493 fprintf(f
, "nameserver %s\n", str
);
1496 vlist_simple_for_each_element(&ip
->dns_search
, d
, node
) {
1497 fprintf(f
, "search %s\n", d
->name
);
1501 /* Sorting of interface resolver entries : */
1502 /* Primary on interface dns_metric : lowest metric first */
1503 /* Secondary on interface metric : lowest metric first */
1504 /* Finally alphabetical order of interface names */
1505 static int resolv_conf_iface_cmp(const void *k1
, const void *k2
, void *ptr
)
1507 const struct interface
*iface1
= k1
, *iface2
= k2
;
1509 if (iface1
->dns_metric
!= iface2
->dns_metric
)
1510 return iface1
->dns_metric
- iface2
->dns_metric
;
1512 if (iface1
->metric
!= iface2
->metric
)
1513 return iface1
->metric
- iface2
->metric
;
1515 return strcmp(iface1
->name
, iface2
->name
);
1519 __interface_write_dns_entries(FILE *f
, const char *jail
)
1521 struct interface
*iface
;
1523 struct avl_node node
;
1525 struct avl_tree resolv_conf_iface_entries
;
1527 avl_init(&resolv_conf_iface_entries
, resolv_conf_iface_cmp
, false, NULL
);
1529 vlist_for_each_element(&interfaces
, iface
, node
) {
1530 if (iface
->state
!= IFS_UP
)
1533 if (jail
&& (!iface
->jail
|| strcmp(jail
, iface
->jail
)))
1536 if (vlist_simple_empty(&iface
->proto_ip
.dns_search
) &&
1537 vlist_simple_empty(&iface
->proto_ip
.dns_servers
) &&
1538 vlist_simple_empty(&iface
->config_ip
.dns_search
) &&
1539 vlist_simple_empty(&iface
->config_ip
.dns_servers
))
1542 entry
= calloc(1, sizeof(*entry
));
1546 entry
->node
.key
= iface
;
1547 avl_insert(&resolv_conf_iface_entries
, &entry
->node
);
1550 avl_for_each_element(&resolv_conf_iface_entries
, entry
, node
) {
1551 iface
= (struct interface
*)entry
->node
.key
;
1552 struct device
*dev
= iface
->l3_dev
.dev
;
1554 fprintf(f
, "# Interface %s\n", iface
->name
);
1556 write_resolv_conf_entries(f
, &iface
->config_ip
, dev
->ifname
);
1558 if (!iface
->proto_ip
.no_dns
)
1559 write_resolv_conf_entries(f
, &iface
->proto_ip
, dev
->ifname
);
1562 avl_remove_all_elements(&resolv_conf_iface_entries
, entry
, node
, n_entry
)
1567 interface_write_resolv_conf(const char *jail
)
1569 size_t plen
= (jail
? strlen(jail
) + 1 : 0 ) +
1570 (strlen(resolv_conf
) >= strlen(DEFAULT_RESOLV_CONF
) ?
1571 strlen(resolv_conf
) : strlen(DEFAULT_RESOLV_CONF
) ) + 1;
1572 char *path
= alloca(plen
);
1573 char *dpath
= alloca(plen
);
1574 char *tmppath
= alloca(plen
+ 4);
1576 uint32_t crcold
, crcnew
;
1579 sprintf(path
, "/tmp/resolv.conf-%s.d/resolv.conf.auto", jail
);
1580 strcpy(dpath
, path
);
1581 dpath
= dirname(dpath
);
1584 strcpy(path
, resolv_conf
);
1587 sprintf(tmppath
, "%s.tmp", path
);
1589 f
= fopen(tmppath
, "w+");
1591 D(INTERFACE
, "Failed to open %s for writing\n", path
);
1595 __interface_write_dns_entries(f
, jail
);
1599 crcnew
= crc32_file(f
);
1602 crcold
= crcnew
+ 1;
1603 f
= fopen(path
, "r");
1605 crcold
= crc32_file(f
);
1609 if (crcold
== crcnew
) {
1611 } else if (rename(tmppath
, path
) < 0) {
1612 D(INTERFACE
, "Failed to replace %s\n", path
);
1618 interface_ip_set_route_enabled(struct interface_ip_settings
*ip
,
1619 struct device_route
*route
, bool enabled
)
1621 struct device
*dev
= ip
->iface
->l3_dev
.dev
;
1623 if (route
->flags
& DEVADDR_EXTERNAL
)
1626 if (!enable_route(ip
, route
))
1629 if (route
->enabled
== enabled
)
1633 interface_set_route_info(ip
->iface
, route
);
1635 if (system_add_route(dev
, route
))
1636 route
->failed
= true;
1638 system_del_route(dev
, route
);
1640 route
->enabled
= enabled
;
1643 void interface_ip_set_enabled(struct interface_ip_settings
*ip
, bool enabled
)
1645 struct device_addr
*addr
;
1646 struct device_route
*route
;
1647 struct device_neighbor
*neighbor
;
1649 struct interface
*iface
;
1651 ip
->enabled
= enabled
;
1653 dev
= iface
->l3_dev
.dev
;
1657 vlist_for_each_element(&ip
->addr
, addr
, node
) {
1658 bool v6
= ((addr
->flags
& DEVADDR_FAMILY
) == DEVADDR_INET6
) ? true : false;
1660 if (addr
->flags
& DEVADDR_EXTERNAL
)
1663 if (addr
->enabled
== enabled
)
1667 system_add_address(dev
, addr
);
1669 addr
->policy_table
= (v6
) ? iface
->ip6table
: iface
->ip4table
;
1670 if (iface
->metric
|| addr
->policy_table
)
1671 interface_handle_subnet_route(iface
, addr
, true);
1673 if ((addr
->flags
& DEVADDR_OFFLINK
) && (addr
->mask
< (v6
? 128 : 32))) {
1674 struct device_route route
;
1676 memset(&route
, 0, sizeof(route
));
1677 route
.flags
= v6
? DEVADDR_INET6
: DEVADDR_INET4
;
1678 route
.metric
= INT32_MAX
;
1679 route
.mask
= addr
->mask
;
1680 route
.addr
= addr
->addr
;
1682 clear_if_addr(&route
.addr
, route
.mask
);
1685 * In case off link is specifed as address property
1686 * add null-route to avoid routing loops
1688 system_add_route(NULL
, &route
);
1691 if (addr
->policy_table
)
1692 interface_add_addr_rules(addr
, true);
1694 interface_handle_subnet_route(iface
, addr
, false);
1695 system_del_address(dev
, addr
);
1697 if ((addr
->flags
& DEVADDR_OFFLINK
) && (addr
->mask
< (v6
? 128 : 32))) {
1698 struct device_route route
;
1700 memset(&route
, 0, sizeof(route
));
1701 route
.flags
= v6
? DEVADDR_INET6
: DEVADDR_INET4
;
1702 route
.metric
= INT32_MAX
;
1703 route
.mask
= addr
->mask
;
1704 route
.addr
= addr
->addr
;
1706 clear_if_addr(&route
.addr
, route
.mask
);
1708 /* Delete null-route */
1709 system_del_route(NULL
, &route
);
1712 if (addr
->policy_table
)
1713 interface_add_addr_rules(addr
, false);
1715 addr
->enabled
= enabled
;
1718 vlist_for_each_element(&ip
->route
, route
, node
)
1719 interface_ip_set_route_enabled(ip
, route
, enabled
);
1720 if (ip
== &iface
->proto_ip
)
1721 vlist_for_each_element(&iface
->host_routes
, route
, node
)
1722 interface_ip_set_route_enabled(ip
, route
, enabled
);
1724 vlist_for_each_element(&ip
->neighbor
, neighbor
, node
) {
1725 if (neighbor
->enabled
== enabled
)
1729 if(system_add_neighbor(dev
, neighbor
))
1730 neighbor
->failed
= true;
1732 system_del_neighbor(dev
, neighbor
);
1734 neighbor
->enabled
= enabled
;
1737 struct device_prefix
*c
;
1738 struct device_prefix_assignment
*a
;
1739 list_for_each_entry(c
, &prefixes
, head
)
1740 list_for_each_entry(a
, &c
->assignments
, head
)
1741 if (!strcmp(a
->name
, ip
->iface
->name
))
1742 interface_set_prefix_address(a
, c
, ip
->iface
, enabled
);
1744 if (ip
->iface
->policy_rules_set
!= enabled
&&
1745 ip
->iface
->l3_dev
.dev
) {
1746 if (ip
->iface
->l3_dev
.dev
->settings
.ipv6
) {
1747 set_ip_lo_policy(enabled
, true, ip
->iface
);
1748 set_ip_source_policy(enabled
, true, IPRULE_PRIORITY_REJECT
+ ip
->iface
->l3_dev
.dev
->ifindex
,
1749 NULL
, 0, 0, ip
->iface
, "failed_policy", true);
1751 set_ip_lo_policy(enabled
, false, ip
->iface
);
1753 ip
->iface
->policy_rules_set
= enabled
;
1758 interface_ip_update_start(struct interface_ip_settings
*ip
)
1760 if (ip
!= &ip
->iface
->config_ip
) {
1761 vlist_simple_update(&ip
->dns_servers
);
1762 vlist_simple_update(&ip
->dns_search
);
1764 vlist_update(&ip
->route
);
1765 vlist_update(&ip
->addr
);
1766 vlist_update(&ip
->prefix
);
1767 vlist_update(&ip
->neighbor
);
1771 interface_ip_update_complete(struct interface_ip_settings
*ip
)
1773 vlist_simple_flush(&ip
->dns_servers
);
1774 vlist_simple_flush(&ip
->dns_search
);
1775 vlist_flush(&ip
->route
);
1776 vlist_flush(&ip
->addr
);
1777 vlist_flush(&ip
->prefix
);
1778 vlist_flush(&ip
->neighbor
);
1779 interface_write_resolv_conf(ip
->iface
->jail
);
1783 interface_ip_flush(struct interface_ip_settings
*ip
)
1785 if (ip
== &ip
->iface
->proto_ip
)
1786 vlist_flush_all(&ip
->iface
->host_routes
);
1787 vlist_simple_flush_all(&ip
->dns_servers
);
1788 vlist_simple_flush_all(&ip
->dns_search
);
1789 vlist_flush_all(&ip
->route
);
1790 vlist_flush_all(&ip
->addr
);
1791 vlist_flush_all(&ip
->neighbor
);
1792 vlist_flush_all(&ip
->prefix
);
1796 __interface_ip_init(struct interface_ip_settings
*ip
, struct interface
*iface
)
1800 vlist_simple_init(&ip
->dns_search
, struct dns_search_domain
, node
);
1801 vlist_simple_init(&ip
->dns_servers
, struct dns_server
, node
);
1802 vlist_init(&ip
->route
, route_cmp
, interface_update_proto_route
);
1803 vlist_init(&ip
->neighbor
, neighbor_cmp
, interface_update_proto_neighbor
);
1804 vlist_init(&ip
->addr
, addr_cmp
, interface_update_proto_addr
);
1805 vlist_init(&ip
->prefix
, prefix_cmp
, interface_update_prefix
);
1809 interface_ip_init(struct interface
*iface
)
1811 __interface_ip_init(&iface
->proto_ip
, iface
);
1812 __interface_ip_init(&iface
->config_ip
, iface
);
1813 vlist_init(&iface
->host_routes
, route_cmp
, interface_update_host_route
);
1817 interface_ip_valid_until_handler(struct uloop_timeout
*t
)
1819 time_t now
= system_get_rtime();
1820 struct interface
*iface
;
1821 vlist_for_each_element(&interfaces
, iface
, node
) {
1822 if (iface
->state
!= IFS_UP
)
1825 struct device_addr
*addr
, *addrp
;
1826 struct device_route
*route
, *routep
;
1827 struct device_prefix
*pref
, *prefp
;
1829 vlist_for_each_element_safe(&iface
->proto_ip
.addr
, addr
, node
, addrp
)
1830 if (addr
->valid_until
&& addr
->valid_until
< now
)
1831 vlist_delete(&iface
->proto_ip
.addr
, &addr
->node
);
1833 vlist_for_each_element_safe(&iface
->proto_ip
.route
, route
, node
, routep
)
1834 if (route
->valid_until
&& route
->valid_until
< now
)
1835 vlist_delete(&iface
->proto_ip
.route
, &route
->node
);
1837 vlist_for_each_element_safe(&iface
->proto_ip
.prefix
, pref
, node
, prefp
)
1838 if (pref
->valid_until
&& pref
->valid_until
< now
)
1839 vlist_delete(&iface
->proto_ip
.prefix
, &pref
->node
);
1843 uloop_timeout_set(t
, 1000);
1847 interface_ip_init_worker(void)
1849 valid_until_timeout
.cb
= interface_ip_valid_until_handler
;
1850 uloop_timeout_set(&valid_until_timeout
, 1000);