14 #include <libubox/utils.h>
15 #include <libubox/avl.h>
16 #include <libubox/avl-cmp.h>
17 #include <libubox/list.h>
18 #include <libubox/vlist.h>
22 static struct blob_buf b
;
23 static int reload_pipe
[2] = { -1, -1 };
25 static int lease_cmp(const void *k1
, const void *k2
, void *ptr
);
26 static void lease_update(struct vlist_tree
*tree
, struct vlist_node
*node_new
,
27 struct vlist_node
*node_old
);
29 struct vlist_tree leases
= VLIST_TREE_INIT(leases
, lease_cmp
, lease_update
, true, false);
30 AVL_TREE(interfaces
, avl_strcmp
, false, NULL
);
31 struct config config
= {.legacy
= false, .main_dhcpv4
= false,
32 .dhcp_cb
= NULL
, .dhcp_statefile
= NULL
,
33 .log_level
= LOG_WARNING
};
35 #define START_DEFAULT 100
36 #define LIMIT_DEFAULT 150
42 IFACE_ATTR_DYNAMICDHCP
,
56 IFACE_ATTR_FILTER_CLASS
,
57 IFACE_ATTR_DHCPV4_FORCERECONF
,
58 IFACE_ATTR_DHCPV6_RAW
,
59 IFACE_ATTR_DHCPV6_ASSIGNALL
,
62 IFACE_ATTR_RA_DEFAULT
,
63 IFACE_ATTR_RA_MANAGEMENT
,
64 IFACE_ATTR_RA_OFFLINK
,
65 IFACE_ATTR_RA_PREFERENCE
,
66 IFACE_ATTR_RA_ADVROUTER
,
67 IFACE_ATTR_RA_MININTERVAL
,
68 IFACE_ATTR_RA_MAXINTERVAL
,
69 IFACE_ATTR_RA_LIFETIME
,
70 IFACE_ATTR_RA_USELEASETIME
,
71 IFACE_ATTR_RA_REACHABLETIME
,
72 IFACE_ATTR_RA_RETRANSTIME
,
73 IFACE_ATTR_RA_HOPLIMIT
,
76 IFACE_ATTR_PD_MANAGER
,
78 IFACE_ATTR_NDPROXY_ROUTING
,
79 IFACE_ATTR_NDPROXY_SLAVE
,
80 IFACE_ATTR_PREFIX_FILTER
,
84 static const struct blobmsg_policy iface_attrs
[IFACE_ATTR_MAX
] = {
85 [IFACE_ATTR_INTERFACE
] = { .name
= "interface", .type
= BLOBMSG_TYPE_STRING
},
86 [IFACE_ATTR_IFNAME
] = { .name
= "ifname", .type
= BLOBMSG_TYPE_STRING
},
87 [IFACE_ATTR_NETWORKID
] = { .name
= "networkid", .type
= BLOBMSG_TYPE_STRING
},
88 [IFACE_ATTR_DYNAMICDHCP
] = { .name
= "dynamicdhcp", .type
= BLOBMSG_TYPE_BOOL
},
89 [IFACE_ATTR_IGNORE
] = { .name
= "ignore", .type
= BLOBMSG_TYPE_BOOL
},
90 [IFACE_ATTR_LEASETIME
] = { .name
= "leasetime", .type
= BLOBMSG_TYPE_STRING
},
91 [IFACE_ATTR_START
] = { .name
= "start", .type
= BLOBMSG_TYPE_INT32
},
92 [IFACE_ATTR_LIMIT
] = { .name
= "limit", .type
= BLOBMSG_TYPE_INT32
},
93 [IFACE_ATTR_MASTER
] = { .name
= "master", .type
= BLOBMSG_TYPE_BOOL
},
94 [IFACE_ATTR_UPSTREAM
] = { .name
= "upstream", .type
= BLOBMSG_TYPE_ARRAY
},
95 [IFACE_ATTR_RA
] = { .name
= "ra", .type
= BLOBMSG_TYPE_STRING
},
96 [IFACE_ATTR_DHCPV4
] = { .name
= "dhcpv4", .type
= BLOBMSG_TYPE_STRING
},
97 [IFACE_ATTR_DHCPV6
] = { .name
= "dhcpv6", .type
= BLOBMSG_TYPE_STRING
},
98 [IFACE_ATTR_NDP
] = { .name
= "ndp", .type
= BLOBMSG_TYPE_STRING
},
99 [IFACE_ATTR_ROUTER
] = { .name
= "router", .type
= BLOBMSG_TYPE_ARRAY
},
100 [IFACE_ATTR_DNS
] = { .name
= "dns", .type
= BLOBMSG_TYPE_ARRAY
},
101 [IFACE_ATTR_DOMAIN
] = { .name
= "domain", .type
= BLOBMSG_TYPE_ARRAY
},
102 [IFACE_ATTR_FILTER_CLASS
] = { .name
= "filter_class", .type
= BLOBMSG_TYPE_STRING
},
103 [IFACE_ATTR_DHCPV4_FORCERECONF
] = { .name
= "dhcpv4_forcereconf", .type
= BLOBMSG_TYPE_BOOL
},
104 [IFACE_ATTR_DHCPV6_RAW
] = { .name
= "dhcpv6_raw", .type
= BLOBMSG_TYPE_STRING
},
105 [IFACE_ATTR_DHCPV6_ASSIGNALL
] = { .name
="dhcpv6_assignall", .type
= BLOBMSG_TYPE_BOOL
},
106 [IFACE_ATTR_DHCPV6_PD
] = { .name
= "dhcpv6_pd", .type
= BLOBMSG_TYPE_BOOL
},
107 [IFACE_ATTR_DHCPV6_NA
] = { .name
= "dhcpv6_na", .type
= BLOBMSG_TYPE_BOOL
},
108 [IFACE_ATTR_PD_MANAGER
] = { .name
= "pd_manager", .type
= BLOBMSG_TYPE_STRING
},
109 [IFACE_ATTR_PD_CER
] = { .name
= "pd_cer", .type
= BLOBMSG_TYPE_STRING
},
110 [IFACE_ATTR_RA_DEFAULT
] = { .name
= "ra_default", .type
= BLOBMSG_TYPE_INT32
},
111 [IFACE_ATTR_RA_MANAGEMENT
] = { .name
= "ra_management", .type
= BLOBMSG_TYPE_INT32
},
112 [IFACE_ATTR_RA_OFFLINK
] = { .name
= "ra_offlink", .type
= BLOBMSG_TYPE_BOOL
},
113 [IFACE_ATTR_RA_PREFERENCE
] = { .name
= "ra_preference", .type
= BLOBMSG_TYPE_STRING
},
114 [IFACE_ATTR_RA_ADVROUTER
] = { .name
= "ra_advrouter", .type
= BLOBMSG_TYPE_BOOL
},
115 [IFACE_ATTR_RA_MININTERVAL
] = { .name
= "ra_mininterval", .type
= BLOBMSG_TYPE_INT32
},
116 [IFACE_ATTR_RA_MAXINTERVAL
] = { .name
= "ra_maxinterval", .type
= BLOBMSG_TYPE_INT32
},
117 [IFACE_ATTR_RA_LIFETIME
] = { .name
= "ra_lifetime", .type
= BLOBMSG_TYPE_INT32
},
118 [IFACE_ATTR_RA_USELEASETIME
] = { .name
= "ra_useleasetime", .type
= BLOBMSG_TYPE_BOOL
},
119 [IFACE_ATTR_RA_REACHABLETIME
] = { .name
= "ra_reachabletime", .type
= BLOBMSG_TYPE_INT32
},
120 [IFACE_ATTR_RA_RETRANSTIME
] = { .name
= "ra_retranstime", .type
= BLOBMSG_TYPE_INT32
},
121 [IFACE_ATTR_RA_HOPLIMIT
] = { .name
= "ra_hoplimit", .type
= BLOBMSG_TYPE_INT32
},
122 [IFACE_ATTR_RA_MTU
] = { .name
= "ra_mtu", .type
= BLOBMSG_TYPE_INT32
},
123 [IFACE_ATTR_RA_DNS
] = { .name
= "ra_dns", .type
= BLOBMSG_TYPE_BOOL
},
124 [IFACE_ATTR_NDPROXY_ROUTING
] = { .name
= "ndproxy_routing", .type
= BLOBMSG_TYPE_BOOL
},
125 [IFACE_ATTR_NDPROXY_SLAVE
] = { .name
= "ndproxy_slave", .type
= BLOBMSG_TYPE_BOOL
},
126 [IFACE_ATTR_PREFIX_FILTER
] = { .name
= "prefix_filter", .type
= BLOBMSG_TYPE_STRING
},
129 static const struct uci_blob_param_info iface_attr_info
[IFACE_ATTR_MAX
] = {
130 [IFACE_ATTR_UPSTREAM
] = { .type
= BLOBMSG_TYPE_STRING
},
131 [IFACE_ATTR_DNS
] = { .type
= BLOBMSG_TYPE_STRING
},
132 [IFACE_ATTR_DOMAIN
] = { .type
= BLOBMSG_TYPE_STRING
},
135 const struct uci_blob_param_list interface_attr_list
= {
136 .n_params
= IFACE_ATTR_MAX
,
137 .params
= iface_attrs
,
138 .info
= iface_attr_info
,
146 LEASE_ATTR_LEASETIME
,
151 static const struct blobmsg_policy lease_attrs
[LEASE_ATTR_MAX
] = {
152 [LEASE_ATTR_IP
] = { .name
= "ip", .type
= BLOBMSG_TYPE_STRING
},
153 [LEASE_ATTR_MAC
] = { .name
= "mac", .type
= BLOBMSG_TYPE_STRING
},
154 [LEASE_ATTR_DUID
] = { .name
= "duid", .type
= BLOBMSG_TYPE_STRING
},
155 [LEASE_ATTR_HOSTID
] = { .name
= "hostid", .type
= BLOBMSG_TYPE_STRING
},
156 [LEASE_ATTR_LEASETIME
] = { .name
= "leasetime", .type
= BLOBMSG_TYPE_STRING
},
157 [LEASE_ATTR_NAME
] = { .name
= "name", .type
= BLOBMSG_TYPE_STRING
},
160 const struct uci_blob_param_list lease_attr_list
= {
161 .n_params
= LEASE_ATTR_MAX
,
162 .params
= lease_attrs
,
167 ODHCPD_ATTR_MAINDHCP
,
168 ODHCPD_ATTR_LEASEFILE
,
169 ODHCPD_ATTR_LEASETRIGGER
,
170 ODHCPD_ATTR_LOGLEVEL
,
174 static const struct blobmsg_policy odhcpd_attrs
[ODHCPD_ATTR_MAX
] = {
175 [ODHCPD_ATTR_LEGACY
] = { .name
= "legacy", .type
= BLOBMSG_TYPE_BOOL
},
176 [ODHCPD_ATTR_MAINDHCP
] = { .name
= "maindhcp", .type
= BLOBMSG_TYPE_BOOL
},
177 [ODHCPD_ATTR_LEASEFILE
] = { .name
= "leasefile", .type
= BLOBMSG_TYPE_STRING
},
178 [ODHCPD_ATTR_LEASETRIGGER
] = { .name
= "leasetrigger", .type
= BLOBMSG_TYPE_STRING
},
179 [ODHCPD_ATTR_LOGLEVEL
] = { .name
= "loglevel", .type
= BLOBMSG_TYPE_INT32
},
182 const struct uci_blob_param_list odhcpd_attr_list
= {
183 .n_params
= ODHCPD_ATTR_MAX
,
184 .params
= odhcpd_attrs
,
187 static int mkdir_p(char *dir
, mode_t mask
)
189 char *l
= strrchr(dir
, '/');
197 if (mkdir_p(dir
, mask
))
202 ret
= mkdir(dir
, mask
);
203 if (ret
&& errno
== EEXIST
)
207 syslog(LOG_ERR
, "mkdir(%s, %d) failed: %m\n", dir
, mask
);
212 static void set_interface_defaults(struct interface
*iface
)
214 iface
->learn_routes
= 1;
215 iface
->dhcpv4_leasetime
= 43200;
216 iface
->dhcpv4_start
.s_addr
= htonl(START_DEFAULT
);
217 iface
->dhcpv4_end
.s_addr
= htonl(START_DEFAULT
+ LIMIT_DEFAULT
- 1);
218 iface
->dhcpv6_assignall
= true;
219 iface
->dhcpv6_pd
= true;
220 iface
->dhcpv6_na
= true;
221 iface
->ra_managed
= RA_MANAGED_MFLAG
;
222 iface
->ra_maxinterval
= 600;
223 iface
->ra_mininterval
= iface
->ra_maxinterval
/3;
224 iface
->ra_lifetime
= -1;
225 iface
->ra_dns
= true;
228 static void clean_interface(struct interface
*iface
)
232 free(iface
->upstream
);
233 free(iface
->dhcpv4_router
);
234 free(iface
->dhcpv4_dns
);
235 free(iface
->dhcpv6_raw
);
236 free(iface
->filter_class
);
237 memset(&iface
->ra
, 0, sizeof(*iface
) - offsetof(struct interface
, ra
));
238 set_interface_defaults(iface
);
241 static void close_interface(struct interface
*iface
)
243 avl_delete(&interfaces
, &iface
->avl
);
245 router_setup_interface(iface
, false);
246 dhcpv6_setup_interface(iface
, false);
247 ndp_setup_interface(iface
, false);
248 #ifdef DHCPV4_SUPPORT
249 dhcpv4_setup_interface(iface
, false);
252 clean_interface(iface
);
259 static int parse_mode(const char *mode
)
261 if (!strcmp(mode
, "disabled"))
262 return MODE_DISABLED
;
263 else if (!strcmp(mode
, "server"))
265 else if (!strcmp(mode
, "relay"))
267 else if (!strcmp(mode
, "hybrid"))
273 static void set_config(struct uci_section
*s
)
275 struct blob_attr
*tb
[ODHCPD_ATTR_MAX
], *c
;
277 blob_buf_init(&b
, 0);
278 uci_to_blob(&b
, s
, &odhcpd_attr_list
);
279 blobmsg_parse(odhcpd_attrs
, ODHCPD_ATTR_MAX
, tb
, blob_data(b
.head
), blob_len(b
.head
));
281 if ((c
= tb
[ODHCPD_ATTR_LEGACY
]))
282 config
.legacy
= blobmsg_get_bool(c
);
284 if ((c
= tb
[ODHCPD_ATTR_MAINDHCP
]))
285 config
.main_dhcpv4
= blobmsg_get_bool(c
);
287 if ((c
= tb
[ODHCPD_ATTR_LEASEFILE
])) {
288 free(config
.dhcp_statefile
);
289 config
.dhcp_statefile
= strdup(blobmsg_get_string(c
));
292 if ((c
= tb
[ODHCPD_ATTR_LEASETRIGGER
])) {
293 free(config
.dhcp_cb
);
294 config
.dhcp_cb
= strdup(blobmsg_get_string(c
));
297 if ((c
= tb
[ODHCPD_ATTR_LOGLEVEL
])) {
298 int log_level
= (blobmsg_get_u32(c
) & LOG_PRIMASK
);
300 if (config
.log_level
!= log_level
) {
301 config
.log_level
= log_level
;
302 setlogmask(LOG_UPTO(config
.log_level
));
307 static double parse_leasetime(struct blob_attr
*c
) {
308 char *val
= blobmsg_get_string(c
), *endptr
= NULL
;
309 double time
= strcmp(val
, "infinite") ? strtod(val
, &endptr
) : UINT32_MAX
;
311 if (time
&& endptr
&& endptr
[0]) {
312 if (endptr
[0] == 's')
314 else if (endptr
[0] == 'm')
316 else if (endptr
[0] == 'h')
318 else if (endptr
[0] == 'd')
320 else if (endptr
[0] == 'w')
321 time
*= 7 * 24 * 3600;
335 static void free_lease(struct lease
*l
)
341 static int set_lease(struct uci_section
*s
)
343 struct blob_attr
*tb
[LEASE_ATTR_MAX
], *c
;
348 blob_buf_init(&b
, 0);
349 uci_to_blob(&b
, s
, &lease_attr_list
);
350 blobmsg_parse(lease_attrs
, LEASE_ATTR_MAX
, tb
, blob_data(b
.head
), blob_len(b
.head
));
352 if ((c
= tb
[LEASE_ATTR_DUID
]))
353 duidlen
= (blobmsg_data_len(c
) - 1) / 2;
355 l
= calloc_a(sizeof(*l
), &duid
, duidlen
);
359 if ((c
= tb
[LEASE_ATTR_MAC
]))
360 if (!ether_aton_r(blobmsg_get_string(c
), &l
->mac
))
363 if ((c
= tb
[LEASE_ATTR_DUID
])) {
367 len
= odhcpd_unhexlify(l
->duid
, duidlen
, blobmsg_get_string(c
));
375 if ((c
= tb
[LEASE_ATTR_NAME
])) {
376 l
->hostname
= strdup(blobmsg_get_string(c
));
377 if (!l
->hostname
|| !odhcpd_valid_hostname(l
->hostname
))
381 if ((c
= tb
[LEASE_ATTR_IP
]))
382 if (inet_pton(AF_INET
, blobmsg_get_string(c
), &l
->ipaddr
) < 0)
385 if ((c
= tb
[LEASE_ATTR_HOSTID
])) {
387 l
->hostid
= strtoul(blobmsg_get_string(c
), NULL
, 16);
391 uint32_t i4a
= ntohl(l
->ipaddr
) & 0xff;
392 l
->hostid
= ((i4a
/ 100) << 8) | (((i4a
% 100) / 10) << 4) | (i4a
% 10);
395 if ((c
= tb
[LEASE_ATTR_LEASETIME
])) {
396 double time
= parse_leasetime(c
);
403 INIT_LIST_HEAD(&l
->assignments
);
404 vlist_add(&leases
, &l
->node
, l
);
414 int config_parse_interface(void *data
, size_t len
, const char *name
, bool overwrite
)
416 struct interface
*iface
;
417 struct blob_attr
*tb
[IFACE_ATTR_MAX
], *c
;
418 bool get_addrs
= false;
420 blobmsg_parse(iface_attrs
, IFACE_ATTR_MAX
, tb
, data
, len
);
422 if (tb
[IFACE_ATTR_INTERFACE
])
423 name
= blobmsg_get_string(tb
[IFACE_ATTR_INTERFACE
]);
428 iface
= avl_find_element(&interfaces
, name
, iface
, avl
);
432 iface
= calloc_a(sizeof(*iface
), &new_name
, strlen(name
) + 1);
436 iface
->name
= strcpy(new_name
, name
);
437 iface
->avl
.key
= iface
->name
;
438 iface
->router_event
.uloop
.fd
= -1;
439 iface
->dhcpv6_event
.uloop
.fd
= -1;
440 iface
->ndp_event
.uloop
.fd
= -1;
441 iface
->ndp_ping_fd
= -1;
442 iface
->dhcpv4_event
.uloop
.fd
= -1;
443 INIT_LIST_HEAD(&iface
->ia_assignments
);
444 INIT_LIST_HEAD(&iface
->dhcpv4_assignments
);
445 INIT_LIST_HEAD(&iface
->dhcpv4_fr_ips
);
447 set_interface_defaults(iface
);
449 avl_insert(&interfaces
, &iface
->avl
);
450 get_addrs
= overwrite
= true;
453 const char *ifname
= NULL
;
455 if ((c
= tb
[IFACE_ATTR_IFNAME
]))
456 ifname
= blobmsg_get_string(c
);
457 else if ((c
= tb
[IFACE_ATTR_NETWORKID
]))
458 ifname
= blobmsg_get_string(c
);
462 if (overwrite
|| !iface
->ifname
)
463 ifname
= ubus_get_ifname(name
);
466 if (!iface
->ifname
&& !ifname
)
471 iface
->ifname
= strdup(ifname
);
476 if (!iface
->ifindex
&&
477 (iface
->ifindex
= if_nametoindex(iface
->ifname
)) <= 0)
482 ssize_t len
= netlink_get_interface_addrs(iface
->ifindex
,
483 true, &iface
->addr6
);
486 iface
->addr6_len
= len
;
488 len
= netlink_get_interface_addrs(iface
->ifindex
,
489 false, &iface
->addr4
);
491 iface
->addr4_len
= len
;
496 if ((c
= tb
[IFACE_ATTR_DYNAMICDHCP
]))
497 iface
->no_dynamic_dhcp
= !blobmsg_get_bool(c
);
499 if (overwrite
&& (c
= tb
[IFACE_ATTR_IGNORE
]))
500 iface
->ignore
= blobmsg_get_bool(c
);
502 if ((c
= tb
[IFACE_ATTR_LEASETIME
])) {
503 double time
= parse_leasetime(c
);
507 iface
->dhcpv4_leasetime
= time
;
510 if ((c
= tb
[IFACE_ATTR_START
])) {
511 iface
->dhcpv4_start
.s_addr
= htonl(blobmsg_get_u32(c
));
512 iface
->dhcpv4_end
.s_addr
= htonl(ntohl(iface
->dhcpv4_start
.s_addr
) +
515 if (config
.main_dhcpv4
&& config
.legacy
)
516 iface
->dhcpv4
= MODE_SERVER
;
519 if ((c
= tb
[IFACE_ATTR_LIMIT
]))
520 iface
->dhcpv4_end
.s_addr
= htonl(ntohl(iface
->dhcpv4_start
.s_addr
) +
521 blobmsg_get_u32(c
) - 1);
523 if ((c
= tb
[IFACE_ATTR_MASTER
]))
524 iface
->master
= blobmsg_get_bool(c
);
526 if (overwrite
&& (c
= tb
[IFACE_ATTR_UPSTREAM
])) {
527 struct blob_attr
*cur
;
530 blobmsg_for_each_attr(cur
, c
, rem
) {
531 if (blobmsg_type(cur
) != BLOBMSG_TYPE_STRING
|| !blobmsg_check_attr(cur
, false))
534 iface
->upstream
= realloc(iface
->upstream
,
535 iface
->upstream_len
+ blobmsg_data_len(cur
));
536 if (!iface
->upstream
)
539 memcpy(iface
->upstream
+ iface
->upstream_len
, blobmsg_get_string(cur
), blobmsg_data_len(cur
));
540 iface
->upstream_len
+= blobmsg_data_len(cur
);
545 if ((c
= tb
[IFACE_ATTR_RA
])) {
546 if ((mode
= parse_mode(blobmsg_get_string(c
))) >= 0)
552 if ((c
= tb
[IFACE_ATTR_DHCPV4
])) {
553 if ((mode
= parse_mode(blobmsg_get_string(c
))) >= 0) {
554 if (config
.main_dhcpv4
)
555 iface
->dhcpv4
= mode
;
561 if ((c
= tb
[IFACE_ATTR_DHCPV6
])) {
562 if ((mode
= parse_mode(blobmsg_get_string(c
))) >= 0)
563 iface
->dhcpv6
= mode
;
568 if ((c
= tb
[IFACE_ATTR_NDP
])) {
569 if ((mode
= parse_mode(blobmsg_get_string(c
))) >= 0)
575 if ((c
= tb
[IFACE_ATTR_ROUTER
])) {
576 struct blob_attr
*cur
;
579 blobmsg_for_each_attr(cur
, c
, rem
) {
580 if (blobmsg_type(cur
) != BLOBMSG_TYPE_STRING
|| !blobmsg_check_attr(cur
, false))
583 struct in_addr addr4
;
584 if (inet_pton(AF_INET
, blobmsg_get_string(cur
), &addr4
) == 1) {
585 iface
->dhcpv4_router
= realloc(iface
->dhcpv4_router
,
586 (++iface
->dhcpv4_router_cnt
) * sizeof(*iface
->dhcpv4_router
));
587 if (!iface
->dhcpv4_router
)
590 iface
->dhcpv4_router
[iface
->dhcpv4_router_cnt
- 1] = addr4
;
596 if ((c
= tb
[IFACE_ATTR_DNS
])) {
597 struct blob_attr
*cur
;
600 iface
->always_rewrite_dns
= true;
601 blobmsg_for_each_attr(cur
, c
, rem
) {
602 if (blobmsg_type(cur
) != BLOBMSG_TYPE_STRING
|| !blobmsg_check_attr(cur
, false))
605 struct in_addr addr4
;
606 struct in6_addr addr6
;
607 if (inet_pton(AF_INET
, blobmsg_get_string(cur
), &addr4
) == 1) {
608 if (addr4
.s_addr
== INADDR_ANY
)
611 iface
->dhcpv4_dns
= realloc(iface
->dhcpv4_dns
,
612 (++iface
->dhcpv4_dns_cnt
) * sizeof(*iface
->dhcpv4_dns
));
613 if (!iface
->dhcpv4_dns
)
616 iface
->dhcpv4_dns
[iface
->dhcpv4_dns_cnt
- 1] = addr4
;
617 } else if (inet_pton(AF_INET6
, blobmsg_get_string(cur
), &addr6
) == 1) {
618 if (IN6_IS_ADDR_UNSPECIFIED(&addr6
))
621 iface
->dns
= realloc(iface
->dns
,
622 (++iface
->dns_cnt
) * sizeof(*iface
->dns
));
626 iface
->dns
[iface
->dns_cnt
- 1] = addr6
;
632 if ((c
= tb
[IFACE_ATTR_DOMAIN
])) {
633 struct blob_attr
*cur
;
636 blobmsg_for_each_attr(cur
, c
, rem
) {
637 if (blobmsg_type(cur
) != BLOBMSG_TYPE_STRING
|| !blobmsg_check_attr(cur
, false))
641 char *domain
= blobmsg_get_string(cur
);
642 size_t domainlen
= strlen(domain
);
643 if (domainlen
> 0 && domain
[domainlen
- 1] == '.')
644 domain
[domainlen
- 1] = 0;
646 int len
= dn_comp(domain
, buf
, sizeof(buf
), NULL
, NULL
);
650 iface
->search
= realloc(iface
->search
, iface
->search_len
+ len
);
654 memcpy(&iface
->search
[iface
->search_len
], buf
, len
);
655 iface
->search_len
+= len
;
659 if ((c
= tb
[IFACE_ATTR_FILTER_CLASS
])) {
660 iface
->filter_class
= realloc(iface
->filter_class
, blobmsg_data_len(c
) + 1);
661 memcpy(iface
->filter_class
, blobmsg_get_string(c
), blobmsg_data_len(c
) + 1);
664 if ((c
= tb
[IFACE_ATTR_DHCPV4_FORCERECONF
]))
665 iface
->dhcpv4_forcereconf
= blobmsg_get_bool(c
);
667 if ((c
= tb
[IFACE_ATTR_DHCPV6_RAW
])) {
668 iface
->dhcpv6_raw_len
= blobmsg_data_len(c
) / 2;
669 iface
->dhcpv6_raw
= realloc(iface
->dhcpv6_raw
, iface
->dhcpv6_raw_len
);
670 odhcpd_unhexlify(iface
->dhcpv6_raw
, iface
->dhcpv6_raw_len
, blobmsg_get_string(c
));
673 if ((c
= tb
[IFACE_ATTR_DHCPV6_ASSIGNALL
]))
674 iface
->dhcpv6_assignall
= blobmsg_get_bool(c
);
676 if ((c
= tb
[IFACE_ATTR_DHCPV6_PD
]))
677 iface
->dhcpv6_pd
= blobmsg_get_bool(c
);
679 if ((c
= tb
[IFACE_ATTR_DHCPV6_NA
]))
680 iface
->dhcpv6_na
= blobmsg_get_bool(c
);
682 if ((c
= tb
[IFACE_ATTR_RA_DEFAULT
]))
683 iface
->default_router
= blobmsg_get_u32(c
);
685 if ((c
= tb
[IFACE_ATTR_RA_MANAGEMENT
]))
686 iface
->ra_managed
= blobmsg_get_u32(c
);
688 if ((c
= tb
[IFACE_ATTR_RA_REACHABLETIME
])) {
689 uint32_t ra_reachabletime
= blobmsg_get_u32(c
);
690 if (ra_reachabletime
> 3600000)
693 iface
->ra_reachabletime
= ra_reachabletime
;
696 if ((c
= tb
[IFACE_ATTR_RA_RETRANSTIME
])) {
697 uint32_t ra_retranstime
= blobmsg_get_u32(c
);
698 if (ra_retranstime
> 60000)
701 iface
->ra_retranstime
= ra_retranstime
;
704 if ((c
= tb
[IFACE_ATTR_RA_HOPLIMIT
])) {
705 uint32_t ra_hoplimit
= blobmsg_get_u32(c
);
706 if (ra_hoplimit
> 255)
709 iface
->ra_hoplimit
= ra_hoplimit
;
712 if ((c
= tb
[IFACE_ATTR_RA_MTU
])) {
713 uint32_t ra_mtu
= blobmsg_get_u32(c
);
714 if (ra_mtu
< 1280 || ra_mtu
> 65535)
717 iface
->ra_mtu
= ra_mtu
;
720 if ((c
= tb
[IFACE_ATTR_RA_OFFLINK
]))
721 iface
->ra_not_onlink
= blobmsg_get_bool(c
);
723 if ((c
= tb
[IFACE_ATTR_RA_ADVROUTER
]))
724 iface
->ra_advrouter
= blobmsg_get_bool(c
);
726 if ((c
= tb
[IFACE_ATTR_RA_MININTERVAL
]))
727 iface
->ra_mininterval
= blobmsg_get_u32(c
);
729 if ((c
= tb
[IFACE_ATTR_RA_MAXINTERVAL
]))
730 iface
->ra_maxinterval
= blobmsg_get_u32(c
);
732 if ((c
= tb
[IFACE_ATTR_RA_LIFETIME
]))
733 iface
->ra_lifetime
= blobmsg_get_u32(c
);
735 if ((c
= tb
[IFACE_ATTR_RA_USELEASETIME
]))
736 iface
->ra_useleasetime
= blobmsg_get_bool(c
);
738 if ((c
= tb
[IFACE_ATTR_RA_DNS
]))
739 iface
->ra_dns
= blobmsg_get_bool(c
);
741 if ((c
= tb
[IFACE_ATTR_RA_PREFERENCE
])) {
742 const char *prio
= blobmsg_get_string(c
);
744 if (!strcmp(prio
, "high"))
745 iface
->route_preference
= 1;
746 else if (!strcmp(prio
, "low"))
747 iface
->route_preference
= -1;
748 else if (!strcmp(prio
, "medium") || !strcmp(prio
, "default"))
749 iface
->route_preference
= 0;
754 if ((c
= tb
[IFACE_ATTR_PD_MANAGER
]))
755 strncpy(iface
->dhcpv6_pd_manager
, blobmsg_get_string(c
),
756 sizeof(iface
->dhcpv6_pd_manager
) - 1);
758 if ((c
= tb
[IFACE_ATTR_PD_CER
]) &&
759 inet_pton(AF_INET6
, blobmsg_get_string(c
), &iface
->dhcpv6_pd_cer
) < 1)
762 if ((c
= tb
[IFACE_ATTR_NDPROXY_ROUTING
]))
763 iface
->learn_routes
= blobmsg_get_bool(c
);
765 if ((c
= tb
[IFACE_ATTR_NDPROXY_SLAVE
]))
766 iface
->external
= blobmsg_get_bool(c
);
768 if ((c
= tb
[IFACE_ATTR_PREFIX_FILTER
])) {
769 const char *str
= blobmsg_get_string(c
);
770 char *astr
= malloc(strlen(str
) + 1);
774 if (!astr
|| !strcpy(astr
, str
) ||
775 (delim
= strchr(astr
, '/')) == NULL
|| (*(delim
++) = 0) ||
776 sscanf(delim
, "%i", &l
) == 0 || l
> 128 ||
777 inet_pton(AF_INET6
, astr
, &iface
->pio_filter_addr
) == 0)
778 iface
->pio_filter_length
= 0;
780 iface
->pio_filter_length
= l
;
789 close_interface(iface
);
793 static int set_interface(struct uci_section
*s
)
795 blob_buf_init(&b
, 0);
796 uci_to_blob(&b
, s
, &interface_attr_list
);
798 return config_parse_interface(blob_data(b
.head
), blob_len(b
.head
), s
->e
.name
, true);
801 static void lease_delete_assignments(struct lease
*l
, bool v6
)
803 struct dhcp_assignment
*a
, *tmp
;
804 unsigned int flag
= v6
? OAF_DHCPV6
: OAF_DHCPV4
;
806 list_for_each_entry_safe(a
, tmp
, &l
->assignments
, lease_list
) {
812 static void lease_update_assignments(struct lease
*l
)
814 struct dhcp_assignment
*a
;
816 list_for_each_entry(a
, &l
->assignments
, lease_list
) {
822 a
->hostname
= strdup(l
->hostname
);
824 a
->leasetime
= l
->leasetime
;
828 static int lease_cmp(const void *k1
, const void *k2
, _unused
void *ptr
)
830 const struct lease
*l1
= k1
, *l2
= k2
;
833 if (l1
->duid_len
!= l2
->duid_len
)
834 return l1
->duid_len
- l2
->duid_len
;
836 if (l1
->duid_len
&& l2
->duid_len
)
837 cmp
= memcmp(l1
->duid
, l2
->duid
, l1
->duid_len
);
842 return memcmp(l1
->mac
.ether_addr_octet
, l2
->mac
.ether_addr_octet
,
843 sizeof(l1
->mac
.ether_addr_octet
));
846 static void lease_change_config(struct lease
*l_old
, struct lease
*l_new
)
850 if ((!!l_new
->hostname
!= !!l_old
->hostname
) ||
851 (l_new
->hostname
&& strcmp(l_new
->hostname
, l_old
->hostname
))) {
852 free(l_old
->hostname
);
853 l_old
->hostname
= NULL
;
856 l_old
->hostname
= strdup(l_new
->hostname
);
861 if (l_old
->leasetime
!= l_new
->leasetime
) {
862 l_old
->leasetime
= l_new
->leasetime
;
866 if (l_old
->ipaddr
!= l_new
->ipaddr
) {
867 l_old
->ipaddr
= l_new
->ipaddr
;
868 lease_delete_assignments(l_old
, false);
871 if (l_old
->hostid
!= l_new
->hostid
) {
872 l_old
->hostid
= l_new
->hostid
;
873 lease_delete_assignments(l_old
, true);
877 lease_update_assignments(l_old
);
882 static void lease_delete(struct lease
*l
)
884 struct dhcp_assignment
*a
, *tmp
;
886 list_for_each_entry_safe(a
, tmp
, &l
->assignments
, lease_list
)
892 static void lease_update(_unused
struct vlist_tree
*tree
, struct vlist_node
*node_new
,
893 struct vlist_node
*node_old
)
895 struct lease
*lease_new
= container_of(node_new
, struct lease
, node
);
896 struct lease
*lease_old
= container_of(node_old
, struct lease
, node
);
898 if (node_old
&& node_new
)
899 lease_change_config(lease_old
, lease_new
);
901 lease_delete(lease_old
);
904 struct lease
*config_find_lease_by_duid(const uint8_t *duid
, const uint16_t len
)
908 vlist_for_each_element(&leases
, l
, node
) {
909 if (l
->duid_len
== len
&& !memcmp(l
->duid
, duid
, len
))
916 struct lease
*config_find_lease_by_mac(const uint8_t *mac
)
920 vlist_for_each_element(&leases
, l
, node
) {
921 if (!memcmp(l
->mac
.ether_addr_octet
, mac
,
922 sizeof(l
->mac
.ether_addr_octet
)))
929 struct lease
*config_find_lease_by_hostid(const uint32_t hostid
)
933 vlist_for_each_element(&leases
, l
, node
) {
934 if (l
->hostid
== hostid
)
941 struct lease
*config_find_lease_by_ipaddr(const uint32_t ipaddr
)
945 vlist_for_each_element(&leases
, l
, node
) {
946 if (l
->ipaddr
== ipaddr
)
953 void odhcpd_reload(void)
955 struct uci_context
*uci
= uci_alloc_context();
956 struct interface
*master
= NULL
, *i
, *tmp
;
961 vlist_update(&leases
);
962 avl_for_each_element(&interfaces
, i
, avl
)
965 struct uci_package
*dhcp
= NULL
;
966 if (!uci_load(uci
, "dhcp", &dhcp
)) {
967 struct uci_element
*e
;
969 /* 1. Global settings */
970 uci_foreach_element(&dhcp
->sections
, e
) {
971 struct uci_section
*s
= uci_to_section(e
);
972 if (!strcmp(s
->type
, "odhcpd"))
977 uci_foreach_element(&dhcp
->sections
, e
) {
978 struct uci_section
*s
= uci_to_section(e
);
979 if (!strcmp(s
->type
, "dhcp"))
983 /* 3. Static leases */
984 uci_foreach_element(&dhcp
->sections
, e
) {
985 struct uci_section
* s
= uci_to_section(e
);
986 if (!strcmp(s
->type
, "host"))
991 if (config
.dhcp_statefile
) {
992 char *path
= strdup(config
.dhcp_statefile
);
994 mkdir_p(dirname(path
), 0755);
998 vlist_flush(&leases
);
1001 ubus_apply_network();
1004 bool any_dhcpv6_slave
= false, any_ra_slave
= false, any_ndp_slave
= false;
1007 avl_for_each_element(&interfaces
, i
, avl
) {
1011 if (i
->dhcpv6
== MODE_HYBRID
|| i
->dhcpv6
== MODE_RELAY
)
1012 any_dhcpv6_slave
= true;
1014 if (i
->ra
== MODE_HYBRID
|| i
->ra
== MODE_RELAY
)
1015 any_ra_slave
= true;
1017 if (i
->ndp
== MODE_HYBRID
|| i
->ndp
== MODE_RELAY
)
1018 any_ndp_slave
= true;
1021 /* Evaluate hybrid mode for master */
1022 avl_for_each_element(&interfaces
, i
, avl
) {
1026 enum odhcpd_mode hybrid_mode
= MODE_DISABLED
;
1028 if (!ubus_has_prefix(i
->name
, i
->ifname
))
1029 hybrid_mode
= MODE_RELAY
;
1032 if (i
->dhcpv6
== MODE_HYBRID
)
1033 i
->dhcpv6
= hybrid_mode
;
1035 if (i
->dhcpv6
== MODE_RELAY
&& !any_dhcpv6_slave
)
1036 i
->dhcpv6
= MODE_DISABLED
;
1038 if (i
->ra
== MODE_HYBRID
)
1039 i
->ra
= hybrid_mode
;
1041 if (i
->ra
== MODE_RELAY
&& !any_ra_slave
)
1042 i
->ra
= MODE_DISABLED
;
1044 if (i
->ndp
== MODE_HYBRID
)
1045 i
->ndp
= hybrid_mode
;
1047 if (i
->ndp
== MODE_RELAY
&& !any_ndp_slave
)
1048 i
->ndp
= MODE_DISABLED
;
1050 if (i
->dhcpv6
== MODE_RELAY
|| i
->ra
== MODE_RELAY
|| i
->ndp
== MODE_RELAY
)
1055 avl_for_each_element_safe(&interfaces
, i
, avl
, tmp
) {
1057 /* Resolve hybrid mode */
1058 if (i
->dhcpv6
== MODE_HYBRID
)
1059 i
->dhcpv6
= (master
&& master
->dhcpv6
== MODE_RELAY
) ?
1060 MODE_RELAY
: MODE_SERVER
;
1062 if (i
->ra
== MODE_HYBRID
)
1063 i
->ra
= (master
&& master
->ra
== MODE_RELAY
) ?
1064 MODE_RELAY
: MODE_SERVER
;
1066 if (i
->ndp
== MODE_HYBRID
)
1067 i
->ndp
= (master
&& master
->ndp
== MODE_RELAY
) ?
1068 MODE_RELAY
: MODE_DISABLED
;
1070 router_setup_interface(i
, !i
->ignore
|| i
->ra
!= MODE_DISABLED
);
1071 dhcpv6_setup_interface(i
, !i
->ignore
|| i
->dhcpv6
!= MODE_DISABLED
);
1072 ndp_setup_interface(i
, !i
->ignore
|| i
->ndp
!= MODE_DISABLED
);
1073 #ifdef DHCPV4_SUPPORT
1074 dhcpv4_setup_interface(i
, !i
->ignore
|| i
->dhcpv4
!= MODE_DISABLED
);
1080 uci_unload(uci
, dhcp
);
1081 uci_free_context(uci
);
1084 static void handle_signal(int signal
)
1088 if (signal
== SIGHUP
) {
1089 if (write(reload_pipe
[1], b
, sizeof(b
)) < 0) {}
1094 static void reload_cb(struct uloop_fd
*u
, _unused
unsigned int events
)
1097 if (read(u
->fd
, b
, sizeof(b
)) < 0) {}
1102 static struct uloop_fd reload_fd
= { .fd
= -1, .cb
= reload_cb
};
1104 void odhcpd_run(void)
1106 if (pipe2(reload_pipe
, O_NONBLOCK
| O_CLOEXEC
) < 0) {}
1108 reload_fd
.fd
= reload_pipe
[0];
1109 uloop_fd_add(&reload_fd
, ULOOP_READ
);
1111 signal(SIGTERM
, handle_signal
);
1112 signal(SIGINT
, handle_signal
);
1113 signal(SIGHUP
, handle_signal
);