config: log config parse failures to syslog
[project/odhcpd.git] / src / config.c
1 #include <fcntl.h>
2 #include <resolv.h>
3 #include <signal.h>
4 #include <arpa/inet.h>
5 #include <unistd.h>
6 #include <libgen.h>
7 #include <net/if.h>
8 #include <string.h>
9 #include <sys/stat.h>
10 #include <syslog.h>
11
12 #include <uci.h>
13 #include <uci_blob.h>
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>
19
20 #include "odhcpd.h"
21
22 static struct blob_buf b;
23 static int reload_pipe[2] = { -1, -1 };
24
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);
28
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};
34
35 #define START_DEFAULT 100
36 #define LIMIT_DEFAULT 150
37
38 #define OAF_DHCPV6 (OAF_DHCPV6_NA | OAF_DHCPV6_PD)
39
40 enum {
41 IFACE_ATTR_INTERFACE,
42 IFACE_ATTR_IFNAME,
43 IFACE_ATTR_NETWORKID,
44 IFACE_ATTR_DYNAMICDHCP,
45 IFACE_ATTR_LEASETIME,
46 IFACE_ATTR_LIMIT,
47 IFACE_ATTR_START,
48 IFACE_ATTR_MASTER,
49 IFACE_ATTR_UPSTREAM,
50 IFACE_ATTR_RA,
51 IFACE_ATTR_DHCPV4,
52 IFACE_ATTR_DHCPV6,
53 IFACE_ATTR_NDP,
54 IFACE_ATTR_ROUTER,
55 IFACE_ATTR_DNS,
56 IFACE_ATTR_DNS_SERVICE,
57 IFACE_ATTR_DOMAIN,
58 IFACE_ATTR_FILTER_CLASS,
59 IFACE_ATTR_DHCPV4_FORCERECONF,
60 IFACE_ATTR_DHCPV6_RAW,
61 IFACE_ATTR_DHCPV6_ASSIGNALL,
62 IFACE_ATTR_DHCPV6_PD,
63 IFACE_ATTR_DHCPV6_NA,
64 IFACE_ATTR_RA_DEFAULT,
65 IFACE_ATTR_RA_MANAGEMENT,
66 IFACE_ATTR_RA_FLAGS,
67 IFACE_ATTR_RA_SLAAC,
68 IFACE_ATTR_RA_OFFLINK,
69 IFACE_ATTR_RA_PREFERENCE,
70 IFACE_ATTR_RA_ADVROUTER,
71 IFACE_ATTR_RA_MININTERVAL,
72 IFACE_ATTR_RA_MAXINTERVAL,
73 IFACE_ATTR_RA_LIFETIME,
74 IFACE_ATTR_RA_USELEASETIME,
75 IFACE_ATTR_RA_REACHABLETIME,
76 IFACE_ATTR_RA_RETRANSTIME,
77 IFACE_ATTR_RA_HOPLIMIT,
78 IFACE_ATTR_RA_MTU,
79 IFACE_ATTR_RA_DNS,
80 IFACE_ATTR_PD_MANAGER,
81 IFACE_ATTR_PD_CER,
82 IFACE_ATTR_NDPROXY_ROUTING,
83 IFACE_ATTR_NDPROXY_SLAVE,
84 IFACE_ATTR_PREFIX_FILTER,
85 IFACE_ATTR_PREFERRED_LIFETIME,
86 IFACE_ATTR_MAX
87 };
88
89 static const struct blobmsg_policy iface_attrs[IFACE_ATTR_MAX] = {
90 [IFACE_ATTR_INTERFACE] = { .name = "interface", .type = BLOBMSG_TYPE_STRING },
91 [IFACE_ATTR_IFNAME] = { .name = "ifname", .type = BLOBMSG_TYPE_STRING },
92 [IFACE_ATTR_NETWORKID] = { .name = "networkid", .type = BLOBMSG_TYPE_STRING },
93 [IFACE_ATTR_DYNAMICDHCP] = { .name = "dynamicdhcp", .type = BLOBMSG_TYPE_BOOL },
94 [IFACE_ATTR_LEASETIME] = { .name = "leasetime", .type = BLOBMSG_TYPE_STRING },
95 [IFACE_ATTR_START] = { .name = "start", .type = BLOBMSG_TYPE_INT32 },
96 [IFACE_ATTR_LIMIT] = { .name = "limit", .type = BLOBMSG_TYPE_INT32 },
97 [IFACE_ATTR_MASTER] = { .name = "master", .type = BLOBMSG_TYPE_BOOL },
98 [IFACE_ATTR_UPSTREAM] = { .name = "upstream", .type = BLOBMSG_TYPE_ARRAY },
99 [IFACE_ATTR_RA] = { .name = "ra", .type = BLOBMSG_TYPE_STRING },
100 [IFACE_ATTR_DHCPV4] = { .name = "dhcpv4", .type = BLOBMSG_TYPE_STRING },
101 [IFACE_ATTR_DHCPV6] = { .name = "dhcpv6", .type = BLOBMSG_TYPE_STRING },
102 [IFACE_ATTR_NDP] = { .name = "ndp", .type = BLOBMSG_TYPE_STRING },
103 [IFACE_ATTR_ROUTER] = { .name = "router", .type = BLOBMSG_TYPE_ARRAY },
104 [IFACE_ATTR_DNS] = { .name = "dns", .type = BLOBMSG_TYPE_ARRAY },
105 [IFACE_ATTR_DNS_SERVICE] = { .name = "dns_service", .type = BLOBMSG_TYPE_BOOL },
106 [IFACE_ATTR_DOMAIN] = { .name = "domain", .type = BLOBMSG_TYPE_ARRAY },
107 [IFACE_ATTR_FILTER_CLASS] = { .name = "filter_class", .type = BLOBMSG_TYPE_STRING },
108 [IFACE_ATTR_DHCPV4_FORCERECONF] = { .name = "dhcpv4_forcereconf", .type = BLOBMSG_TYPE_BOOL },
109 [IFACE_ATTR_DHCPV6_RAW] = { .name = "dhcpv6_raw", .type = BLOBMSG_TYPE_STRING },
110 [IFACE_ATTR_DHCPV6_ASSIGNALL] = { .name ="dhcpv6_assignall", .type = BLOBMSG_TYPE_BOOL },
111 [IFACE_ATTR_DHCPV6_PD] = { .name = "dhcpv6_pd", .type = BLOBMSG_TYPE_BOOL },
112 [IFACE_ATTR_DHCPV6_NA] = { .name = "dhcpv6_na", .type = BLOBMSG_TYPE_BOOL },
113 [IFACE_ATTR_PD_MANAGER] = { .name = "pd_manager", .type = BLOBMSG_TYPE_STRING },
114 [IFACE_ATTR_PD_CER] = { .name = "pd_cer", .type = BLOBMSG_TYPE_STRING },
115 [IFACE_ATTR_RA_DEFAULT] = { .name = "ra_default", .type = BLOBMSG_TYPE_INT32 },
116 [IFACE_ATTR_RA_MANAGEMENT] = { .name = "ra_management", .type = BLOBMSG_TYPE_INT32 },
117 [IFACE_ATTR_RA_FLAGS] = { .name = "ra_flags", . type = BLOBMSG_TYPE_ARRAY },
118 [IFACE_ATTR_RA_SLAAC] = { .name = "ra_slaac", .type = BLOBMSG_TYPE_BOOL },
119 [IFACE_ATTR_RA_OFFLINK] = { .name = "ra_offlink", .type = BLOBMSG_TYPE_BOOL },
120 [IFACE_ATTR_RA_PREFERENCE] = { .name = "ra_preference", .type = BLOBMSG_TYPE_STRING },
121 [IFACE_ATTR_RA_ADVROUTER] = { .name = "ra_advrouter", .type = BLOBMSG_TYPE_BOOL },
122 [IFACE_ATTR_RA_MININTERVAL] = { .name = "ra_mininterval", .type = BLOBMSG_TYPE_INT32 },
123 [IFACE_ATTR_RA_MAXINTERVAL] = { .name = "ra_maxinterval", .type = BLOBMSG_TYPE_INT32 },
124 [IFACE_ATTR_RA_LIFETIME] = { .name = "ra_lifetime", .type = BLOBMSG_TYPE_INT32 },
125 [IFACE_ATTR_RA_USELEASETIME] = { .name = "ra_useleasetime", .type = BLOBMSG_TYPE_BOOL },
126 [IFACE_ATTR_RA_REACHABLETIME] = { .name = "ra_reachabletime", .type = BLOBMSG_TYPE_INT32 },
127 [IFACE_ATTR_RA_RETRANSTIME] = { .name = "ra_retranstime", .type = BLOBMSG_TYPE_INT32 },
128 [IFACE_ATTR_RA_HOPLIMIT] = { .name = "ra_hoplimit", .type = BLOBMSG_TYPE_INT32 },
129 [IFACE_ATTR_RA_MTU] = { .name = "ra_mtu", .type = BLOBMSG_TYPE_INT32 },
130 [IFACE_ATTR_RA_DNS] = { .name = "ra_dns", .type = BLOBMSG_TYPE_BOOL },
131 [IFACE_ATTR_NDPROXY_ROUTING] = { .name = "ndproxy_routing", .type = BLOBMSG_TYPE_BOOL },
132 [IFACE_ATTR_NDPROXY_SLAVE] = { .name = "ndproxy_slave", .type = BLOBMSG_TYPE_BOOL },
133 [IFACE_ATTR_PREFIX_FILTER] = { .name = "prefix_filter", .type = BLOBMSG_TYPE_STRING },
134 [IFACE_ATTR_PREFERRED_LIFETIME] = { .name = "preferred_lifetime", .type = BLOBMSG_TYPE_STRING },
135 };
136
137 static const struct uci_blob_param_info iface_attr_info[IFACE_ATTR_MAX] = {
138 [IFACE_ATTR_UPSTREAM] = { .type = BLOBMSG_TYPE_STRING },
139 [IFACE_ATTR_DNS] = { .type = BLOBMSG_TYPE_STRING },
140 [IFACE_ATTR_DOMAIN] = { .type = BLOBMSG_TYPE_STRING },
141 };
142
143 const struct uci_blob_param_list interface_attr_list = {
144 .n_params = IFACE_ATTR_MAX,
145 .params = iface_attrs,
146 .info = iface_attr_info,
147 };
148
149 const struct blobmsg_policy lease_attrs[LEASE_ATTR_MAX] = {
150 [LEASE_ATTR_IP] = { .name = "ip", .type = BLOBMSG_TYPE_STRING },
151 [LEASE_ATTR_MAC] = { .name = "mac", .type = BLOBMSG_TYPE_STRING },
152 [LEASE_ATTR_DUID] = { .name = "duid", .type = BLOBMSG_TYPE_STRING },
153 [LEASE_ATTR_HOSTID] = { .name = "hostid", .type = BLOBMSG_TYPE_STRING },
154 [LEASE_ATTR_LEASETIME] = { .name = "leasetime", .type = BLOBMSG_TYPE_STRING },
155 [LEASE_ATTR_NAME] = { .name = "name", .type = BLOBMSG_TYPE_STRING },
156 };
157
158 const struct uci_blob_param_list lease_attr_list = {
159 .n_params = LEASE_ATTR_MAX,
160 .params = lease_attrs,
161 };
162
163 enum {
164 ODHCPD_ATTR_LEGACY,
165 ODHCPD_ATTR_MAINDHCP,
166 ODHCPD_ATTR_LEASEFILE,
167 ODHCPD_ATTR_LEASETRIGGER,
168 ODHCPD_ATTR_LOGLEVEL,
169 ODHCPD_ATTR_MAX
170 };
171
172 static const struct blobmsg_policy odhcpd_attrs[ODHCPD_ATTR_MAX] = {
173 [ODHCPD_ATTR_LEGACY] = { .name = "legacy", .type = BLOBMSG_TYPE_BOOL },
174 [ODHCPD_ATTR_MAINDHCP] = { .name = "maindhcp", .type = BLOBMSG_TYPE_BOOL },
175 [ODHCPD_ATTR_LEASEFILE] = { .name = "leasefile", .type = BLOBMSG_TYPE_STRING },
176 [ODHCPD_ATTR_LEASETRIGGER] = { .name = "leasetrigger", .type = BLOBMSG_TYPE_STRING },
177 [ODHCPD_ATTR_LOGLEVEL] = { .name = "loglevel", .type = BLOBMSG_TYPE_INT32 },
178 };
179
180 const struct uci_blob_param_list odhcpd_attr_list = {
181 .n_params = ODHCPD_ATTR_MAX,
182 .params = odhcpd_attrs,
183 };
184
185 static const struct { const char *name; uint8_t flag; } ra_flags[] = {
186 { .name = "managed-config", .flag = ND_RA_FLAG_MANAGED },
187 { .name = "other-config", .flag = ND_RA_FLAG_OTHER },
188 { .name = "home-agent", .flag = ND_RA_FLAG_HOME_AGENT },
189 { .name = "none", . flag = 0 },
190 { .name = NULL, },
191 };
192
193 static void set_interface_defaults(struct interface *iface)
194 {
195 iface->ignore = true;
196 iface->dhcpv4 = MODE_DISABLED;
197 iface->dhcpv6 = MODE_DISABLED;
198 iface->ra = MODE_DISABLED;
199 iface->ndp = MODE_DISABLED;
200 iface->learn_routes = 1;
201 iface->dhcp_leasetime = 43200;
202 iface->preferred_lifetime = 43200;
203 iface->dhcpv4_start.s_addr = htonl(START_DEFAULT);
204 iface->dhcpv4_end.s_addr = htonl(START_DEFAULT + LIMIT_DEFAULT - 1);
205 iface->dhcpv6_assignall = true;
206 iface->dhcpv6_pd = true;
207 iface->dhcpv6_na = true;
208 iface->dns_service = true;
209 iface->ra_flags = ND_RA_FLAG_OTHER;
210 iface->ra_slaac = true;
211 iface->ra_maxinterval = 600;
212 iface->ra_mininterval = iface->ra_maxinterval/3;
213 iface->ra_lifetime = -1;
214 iface->ra_dns = true;
215 }
216
217 static void clean_interface(struct interface *iface)
218 {
219 free(iface->dns);
220 free(iface->search);
221 free(iface->upstream);
222 free(iface->dhcpv4_router);
223 free(iface->dhcpv4_dns);
224 free(iface->dhcpv6_raw);
225 free(iface->filter_class);
226 memset(&iface->ra, 0, sizeof(*iface) - offsetof(struct interface, ra));
227 set_interface_defaults(iface);
228 }
229
230 static void close_interface(struct interface *iface)
231 {
232 avl_delete(&interfaces, &iface->avl);
233
234 router_setup_interface(iface, false);
235 dhcpv6_setup_interface(iface, false);
236 ndp_setup_interface(iface, false);
237 #ifdef DHCPV4_SUPPORT
238 dhcpv4_setup_interface(iface, false);
239 #endif
240
241 clean_interface(iface);
242 free(iface->addr4);
243 free(iface->addr6);
244 free(iface->ifname);
245 free(iface);
246 }
247
248 static int parse_mode(const char *mode)
249 {
250 if (!strcmp(mode, "disabled"))
251 return MODE_DISABLED;
252 else if (!strcmp(mode, "server"))
253 return MODE_SERVER;
254 else if (!strcmp(mode, "relay"))
255 return MODE_RELAY;
256 else if (!strcmp(mode, "hybrid"))
257 return MODE_HYBRID;
258 else
259 return -1;
260 }
261
262 static int parse_ra_flags(uint8_t *flags, struct blob_attr *attr)
263 {
264 struct blob_attr *cur;
265 unsigned rem;
266
267 blobmsg_for_each_attr(cur, attr, rem) {
268 int i;
269
270 if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING)
271 continue;
272
273 if (!blobmsg_check_attr(cur, false))
274 continue;
275
276 for (i = 0; ra_flags[i].name; i++) {
277 if (!strcmp(ra_flags[i].name, blobmsg_get_string(cur))) {
278 *flags |= ra_flags[i].flag;
279 break;
280 }
281 }
282
283 if (!ra_flags[i].name)
284 return -1;
285 }
286
287 return 0;
288 }
289
290 static void set_config(struct uci_section *s)
291 {
292 struct blob_attr *tb[ODHCPD_ATTR_MAX], *c;
293
294 blob_buf_init(&b, 0);
295 uci_to_blob(&b, s, &odhcpd_attr_list);
296 blobmsg_parse(odhcpd_attrs, ODHCPD_ATTR_MAX, tb, blob_data(b.head), blob_len(b.head));
297
298 if ((c = tb[ODHCPD_ATTR_LEGACY]))
299 config.legacy = blobmsg_get_bool(c);
300
301 if ((c = tb[ODHCPD_ATTR_MAINDHCP]))
302 config.main_dhcpv4 = blobmsg_get_bool(c);
303
304 if ((c = tb[ODHCPD_ATTR_LEASEFILE])) {
305 free(config.dhcp_statefile);
306 config.dhcp_statefile = strdup(blobmsg_get_string(c));
307 }
308
309 if ((c = tb[ODHCPD_ATTR_LEASETRIGGER])) {
310 free(config.dhcp_cb);
311 config.dhcp_cb = strdup(blobmsg_get_string(c));
312 }
313
314 if ((c = tb[ODHCPD_ATTR_LOGLEVEL])) {
315 int log_level = (blobmsg_get_u32(c) & LOG_PRIMASK);
316
317 if (config.log_level != log_level) {
318 config.log_level = log_level;
319 setlogmask(LOG_UPTO(config.log_level));
320 }
321 }
322 }
323
324 static double parse_leasetime(struct blob_attr *c) {
325 char *val = blobmsg_get_string(c), *endptr = NULL;
326 double time = strcmp(val, "infinite") ? strtod(val, &endptr) : UINT32_MAX;
327
328 if (time && endptr && endptr[0]) {
329 if (endptr[0] == 's')
330 time *= 1;
331 else if (endptr[0] == 'm')
332 time *= 60;
333 else if (endptr[0] == 'h')
334 time *= 3600;
335 else if (endptr[0] == 'd')
336 time *= 24 * 3600;
337 else if (endptr[0] == 'w')
338 time *= 7 * 24 * 3600;
339 else
340 goto err;
341 }
342
343 if (time < 60)
344 time = 60;
345
346 return time;
347
348 err:
349 return -1;
350 }
351
352 static void free_lease(struct lease *l)
353 {
354 free(l->hostname);
355 free(l);
356 }
357
358
359 int set_lease_from_blobmsg(struct blob_attr *ba)
360 {
361 struct blob_attr *tb[LEASE_ATTR_MAX], *c;
362 struct lease *l;
363 size_t duidlen = 0;
364 uint8_t *duid;
365
366 blobmsg_parse(lease_attrs, LEASE_ATTR_MAX, tb, blob_data(ba), blob_len(ba));
367
368 if ((c = tb[LEASE_ATTR_DUID]))
369 duidlen = (blobmsg_data_len(c) - 1) / 2;
370
371 l = calloc_a(sizeof(*l), &duid, duidlen);
372 if (!l)
373 goto err;
374
375 if ((c = tb[LEASE_ATTR_MAC]))
376 if (!ether_aton_r(blobmsg_get_string(c), &l->mac))
377 goto err;
378
379 if ((c = tb[LEASE_ATTR_DUID])) {
380 ssize_t len;
381
382 l->duid = duid;
383 len = odhcpd_unhexlify(l->duid, duidlen, blobmsg_get_string(c));
384
385 if (len < 0)
386 goto err;
387
388 l->duid_len = len;
389 }
390
391 if ((c = tb[LEASE_ATTR_NAME])) {
392 l->hostname = strdup(blobmsg_get_string(c));
393 if (!l->hostname || !odhcpd_valid_hostname(l->hostname))
394 goto err;
395 }
396
397 if ((c = tb[LEASE_ATTR_IP]))
398 if (inet_pton(AF_INET, blobmsg_get_string(c), &l->ipaddr) < 0)
399 goto err;
400
401 if ((c = tb[LEASE_ATTR_HOSTID])) {
402 errno = 0;
403 l->hostid = strtoul(blobmsg_get_string(c), NULL, 16);
404 if (errno)
405 goto err;
406 } else {
407 uint32_t i4a = ntohl(l->ipaddr) & 0xff;
408 l->hostid = ((i4a / 100) << 8) | (((i4a % 100) / 10) << 4) | (i4a % 10);
409 }
410
411 if ((c = tb[LEASE_ATTR_LEASETIME])) {
412 double time = parse_leasetime(c);
413 if (time < 0)
414 goto err;
415
416 l->leasetime = time;
417 }
418
419 INIT_LIST_HEAD(&l->assignments);
420 vlist_add(&leases, &l->node, l);
421 return 0;
422
423 err:
424 if (l)
425 free_lease(l);
426
427 return -1;
428 }
429
430 static int set_lease_from_uci(struct uci_section *s)
431 {
432 blob_buf_init(&b, 0);
433 uci_to_blob(&b, s, &lease_attr_list);
434
435 return set_lease_from_blobmsg(b.head);
436 }
437
438 int config_parse_interface(void *data, size_t len, const char *name, bool overwrite)
439 {
440 struct interface *iface;
441 struct blob_attr *tb[IFACE_ATTR_MAX], *c;
442 bool get_addrs = false;
443 int mode;
444 const char *ifname = NULL;
445
446 blobmsg_parse(iface_attrs, IFACE_ATTR_MAX, tb, data, len);
447
448 if (tb[IFACE_ATTR_INTERFACE])
449 name = blobmsg_get_string(tb[IFACE_ATTR_INTERFACE]);
450
451 if (!name)
452 return -1;
453
454 iface = avl_find_element(&interfaces, name, iface, avl);
455 if (!iface) {
456 char *new_name;
457
458 iface = calloc_a(sizeof(*iface), &new_name, strlen(name) + 1);
459 if (!iface)
460 return -1;
461
462 iface->name = strcpy(new_name, name);
463 iface->avl.key = iface->name;
464 iface->router_event.uloop.fd = -1;
465 iface->dhcpv6_event.uloop.fd = -1;
466 iface->ndp_event.uloop.fd = -1;
467 iface->ndp_ping_fd = -1;
468 iface->dhcpv4_event.uloop.fd = -1;
469 INIT_LIST_HEAD(&iface->ia_assignments);
470 INIT_LIST_HEAD(&iface->dhcpv4_assignments);
471 INIT_LIST_HEAD(&iface->dhcpv4_fr_ips);
472
473 set_interface_defaults(iface);
474
475 avl_insert(&interfaces, &iface->avl);
476 get_addrs = overwrite = true;
477 }
478
479 if (overwrite) {
480 if ((c = tb[IFACE_ATTR_IFNAME]))
481 ifname = blobmsg_get_string(c);
482 else if ((c = tb[IFACE_ATTR_NETWORKID]))
483 ifname = blobmsg_get_string(c);
484 }
485
486 #ifdef WITH_UBUS
487 if (overwrite || !iface->ifname)
488 ifname = ubus_get_ifname(name);
489 #endif
490
491 if (!iface->ifname && !ifname)
492 goto err;
493
494 if (ifname) {
495 free(iface->ifname);
496 iface->ifname = strdup(ifname);
497
498 if (!iface->ifname)
499 goto err;
500
501 if (!iface->ifindex &&
502 (iface->ifindex = if_nametoindex(iface->ifname)) <= 0)
503 goto err;
504 }
505
506 if (get_addrs) {
507 ssize_t len = netlink_get_interface_addrs(iface->ifindex,
508 true, &iface->addr6);
509
510 if (len > 0)
511 iface->addr6_len = len;
512
513 len = netlink_get_interface_addrs(iface->ifindex,
514 false, &iface->addr4);
515 if (len > 0)
516 iface->addr4_len = len;
517 }
518
519 iface->inuse = true;
520
521 if ((c = tb[IFACE_ATTR_DYNAMICDHCP]))
522 iface->no_dynamic_dhcp = !blobmsg_get_bool(c);
523
524 if ((c = tb[IFACE_ATTR_LEASETIME])) {
525 double time = parse_leasetime(c);
526
527 if (time >= 0)
528 iface->dhcp_leasetime = time;
529 else
530 syslog(LOG_ERR, "Invalid %s value configured for interface '%s'",
531 iface_attrs[IFACE_ATTR_LEASETIME].name, iface->name);
532
533 }
534
535 if ((c = tb[IFACE_ATTR_PREFERRED_LIFETIME])) {
536 double time = parse_leasetime(c);
537
538 if (time >= 0)
539 iface->preferred_lifetime = time;
540 else
541 syslog(LOG_ERR, "Invalid %s value configured for interface '%s'",
542 iface_attrs[IFACE_ATTR_PREFERRED_LIFETIME].name, iface->name);
543
544 }
545
546 if ((c = tb[IFACE_ATTR_START])) {
547 iface->dhcpv4_start.s_addr = htonl(blobmsg_get_u32(c));
548 iface->dhcpv4_end.s_addr = htonl(ntohl(iface->dhcpv4_start.s_addr) +
549 LIMIT_DEFAULT - 1);
550
551 if (config.main_dhcpv4 && config.legacy)
552 iface->dhcpv4 = MODE_SERVER;
553 }
554
555 if ((c = tb[IFACE_ATTR_LIMIT]))
556 iface->dhcpv4_end.s_addr = htonl(ntohl(iface->dhcpv4_start.s_addr) +
557 blobmsg_get_u32(c) - 1);
558
559 if ((c = tb[IFACE_ATTR_MASTER]))
560 iface->master = blobmsg_get_bool(c);
561
562 if (overwrite && (c = tb[IFACE_ATTR_UPSTREAM])) {
563 struct blob_attr *cur;
564 unsigned rem;
565
566 blobmsg_for_each_attr(cur, c, rem) {
567 if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING || !blobmsg_check_attr(cur, false))
568 continue;
569
570 iface->upstream = realloc(iface->upstream,
571 iface->upstream_len + blobmsg_data_len(cur));
572 if (!iface->upstream)
573 goto err;
574
575 memcpy(iface->upstream + iface->upstream_len, blobmsg_get_string(cur), blobmsg_data_len(cur));
576 iface->upstream_len += blobmsg_data_len(cur);
577 }
578 }
579
580 if ((c = tb[IFACE_ATTR_RA])) {
581 if ((mode = parse_mode(blobmsg_get_string(c))) >= 0) {
582 iface->ra = mode;
583
584 if (iface->ra != MODE_DISABLED)
585 iface->ignore = false;
586 } else
587 syslog(LOG_ERR, "Invalid %s mode configured for interface '%s'",
588 iface_attrs[IFACE_ATTR_RA].name, iface->name);
589 }
590
591 if ((c = tb[IFACE_ATTR_DHCPV4])) {
592 if ((mode = parse_mode(blobmsg_get_string(c))) >= 0) {
593 if (config.main_dhcpv4) {
594 iface->dhcpv4 = mode;
595
596 if (iface->dhcpv4 != MODE_DISABLED)
597 iface->ignore = false;
598 }
599 } else
600 syslog(LOG_ERR, "Invalid %s mode configured for interface %s",
601 iface_attrs[IFACE_ATTR_DHCPV4].name, iface->name);
602 }
603
604 if ((c = tb[IFACE_ATTR_DHCPV6])) {
605 if ((mode = parse_mode(blobmsg_get_string(c))) >= 0) {
606 iface->dhcpv6 = mode;
607
608 if (iface->dhcpv6 != MODE_DISABLED)
609 iface->ignore = false;
610 } else
611 syslog(LOG_ERR, "Invalid %s mode configured for interface '%s'",
612 iface_attrs[IFACE_ATTR_DHCPV6].name, iface->name);
613 }
614
615 if ((c = tb[IFACE_ATTR_NDP])) {
616 if ((mode = parse_mode(blobmsg_get_string(c))) >= 0) {
617 iface->ndp = mode;
618
619 if (iface->ndp != MODE_DISABLED)
620 iface->ignore = false;
621 } else
622 syslog(LOG_ERR, "Invalid %s mode configured for interface '%s'",
623 iface_attrs[IFACE_ATTR_NDP].name, iface->name);
624 }
625
626 if ((c = tb[IFACE_ATTR_ROUTER])) {
627 struct blob_attr *cur;
628 unsigned rem;
629
630 blobmsg_for_each_attr(cur, c, rem) {
631 struct in_addr addr4;
632
633 if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING || !blobmsg_check_attr(cur, false))
634 continue;
635
636 if (inet_pton(AF_INET, blobmsg_get_string(cur), &addr4) == 1) {
637 iface->dhcpv4_router = realloc(iface->dhcpv4_router,
638 (++iface->dhcpv4_router_cnt) * sizeof(*iface->dhcpv4_router));
639 if (!iface->dhcpv4_router)
640 goto err;
641
642 iface->dhcpv4_router[iface->dhcpv4_router_cnt - 1] = addr4;
643 } else
644 syslog(LOG_ERR, "Invalid %s value configured for interface '%s'",
645 iface_attrs[IFACE_ATTR_ROUTER].name, iface->name);
646 }
647 }
648
649 if ((c = tb[IFACE_ATTR_DNS])) {
650 struct blob_attr *cur;
651 unsigned rem;
652
653 iface->always_rewrite_dns = true;
654 blobmsg_for_each_attr(cur, c, rem) {
655 struct in_addr addr4;
656 struct in6_addr addr6;
657
658 if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING || !blobmsg_check_attr(cur, false))
659 continue;
660
661 if (inet_pton(AF_INET, blobmsg_get_string(cur), &addr4) == 1) {
662 if (addr4.s_addr == INADDR_ANY) {
663 syslog(LOG_ERR, "Invalid %s value configured for interface '%s'",
664 iface_attrs[IFACE_ATTR_DNS].name, iface->name);
665
666 continue;
667 }
668
669 iface->dhcpv4_dns = realloc(iface->dhcpv4_dns,
670 (++iface->dhcpv4_dns_cnt) * sizeof(*iface->dhcpv4_dns));
671 if (!iface->dhcpv4_dns)
672 goto err;
673
674 iface->dhcpv4_dns[iface->dhcpv4_dns_cnt - 1] = addr4;
675 } else if (inet_pton(AF_INET6, blobmsg_get_string(cur), &addr6) == 1) {
676 if (IN6_IS_ADDR_UNSPECIFIED(&addr6)) {
677 syslog(LOG_ERR, "Invalid %s value configured for interface '%s'",
678 iface_attrs[IFACE_ATTR_DNS].name, iface->name);
679
680 continue;
681 }
682
683 iface->dns = realloc(iface->dns,
684 (++iface->dns_cnt) * sizeof(*iface->dns));
685 if (!iface->dns)
686 goto err;
687
688 iface->dns[iface->dns_cnt - 1] = addr6;
689 } else
690 syslog(LOG_ERR, "Invalid %s value configured for interface '%s'",
691 iface_attrs[IFACE_ATTR_DNS].name, iface->name);
692 }
693 }
694
695 if ((c = tb[IFACE_ATTR_DNS_SERVICE]))
696 iface->dns_service = blobmsg_get_bool(c);
697
698 if ((c = tb[IFACE_ATTR_DOMAIN])) {
699 struct blob_attr *cur;
700 unsigned rem;
701
702 blobmsg_for_each_attr(cur, c, rem) {
703 uint8_t buf[256];
704 char *domain = blobmsg_get_string(cur);
705 size_t domainlen = strlen(domain);
706 int len;
707
708 if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING || !blobmsg_check_attr(cur, false))
709 continue;
710
711 domain = blobmsg_get_string(cur);
712 domainlen = strlen(domain);
713
714 if (domainlen > 0 && domain[domainlen - 1] == '.')
715 domain[domainlen - 1] = 0;
716
717 len = dn_comp(domain, buf, sizeof(buf), NULL, NULL);
718 if (len <= 0) {
719 syslog(LOG_ERR, "Invalid %s value configured for interface '%s'",
720 iface_attrs[IFACE_ATTR_DOMAIN].name, iface->name);
721
722 continue;
723 }
724
725 iface->search = realloc(iface->search, iface->search_len + len);
726 if (!iface->search)
727 goto err;
728
729 memcpy(&iface->search[iface->search_len], buf, len);
730 iface->search_len += len;
731 }
732 }
733
734 if ((c = tb[IFACE_ATTR_FILTER_CLASS])) {
735 iface->filter_class = realloc(iface->filter_class, blobmsg_data_len(c) + 1);
736 memcpy(iface->filter_class, blobmsg_get_string(c), blobmsg_data_len(c) + 1);
737 }
738
739 if ((c = tb[IFACE_ATTR_DHCPV4_FORCERECONF]))
740 iface->dhcpv4_forcereconf = blobmsg_get_bool(c);
741
742 if ((c = tb[IFACE_ATTR_DHCPV6_RAW])) {
743 iface->dhcpv6_raw_len = blobmsg_data_len(c) / 2;
744 iface->dhcpv6_raw = realloc(iface->dhcpv6_raw, iface->dhcpv6_raw_len);
745 odhcpd_unhexlify(iface->dhcpv6_raw, iface->dhcpv6_raw_len, blobmsg_get_string(c));
746 }
747
748 if ((c = tb[IFACE_ATTR_DHCPV6_ASSIGNALL]))
749 iface->dhcpv6_assignall = blobmsg_get_bool(c);
750
751 if ((c = tb[IFACE_ATTR_DHCPV6_PD]))
752 iface->dhcpv6_pd = blobmsg_get_bool(c);
753
754 if ((c = tb[IFACE_ATTR_DHCPV6_NA]))
755 iface->dhcpv6_na = blobmsg_get_bool(c);
756
757 if ((c = tb[IFACE_ATTR_RA_DEFAULT]))
758 iface->default_router = blobmsg_get_u32(c);
759
760 if (!tb[IFACE_ATTR_RA_FLAGS] && !tb[IFACE_ATTR_RA_SLAAC] &&
761 (c = tb[IFACE_ATTR_RA_MANAGEMENT])) {
762 switch (blobmsg_get_u32(c)) {
763 case 0:
764 iface->ra_flags = ND_RA_FLAG_OTHER;
765 iface->ra_slaac = true;
766 break;
767 case 1:
768 iface->ra_flags = ND_RA_FLAG_OTHER|ND_RA_FLAG_MANAGED;
769 iface->ra_slaac = true;
770 break;
771 case 2:
772 iface->ra_flags = ND_RA_FLAG_OTHER|ND_RA_FLAG_MANAGED;
773 iface->ra_slaac = false;
774 break;
775 default:
776 break;
777 }
778 }
779
780 if ((c = tb[IFACE_ATTR_RA_FLAGS])) {
781 if (parse_ra_flags(&iface->ra_flags, c) < 0)
782 syslog(LOG_ERR, "Invalid %s value configured for interface '%s'",
783 iface_attrs[IFACE_ATTR_RA_FLAGS].name, iface->name);
784 }
785
786 if ((c = tb[IFACE_ATTR_RA_REACHABLETIME])) {
787 uint32_t ra_reachabletime = blobmsg_get_u32(c);
788
789 if (ra_reachabletime <= 3600000)
790 iface->ra_reachabletime = ra_reachabletime;
791 else
792 syslog(LOG_ERR, "Invalid %s value configured for interface '%s'",
793 iface_attrs[IFACE_ATTR_RA_REACHABLETIME].name, iface->name);
794 }
795
796 if ((c = tb[IFACE_ATTR_RA_RETRANSTIME])) {
797 uint32_t ra_retranstime = blobmsg_get_u32(c);
798
799 if (ra_retranstime <= 60000)
800 iface->ra_retranstime = ra_retranstime;
801 else
802 syslog(LOG_ERR, "Invalid %s value configured for interface '%s'",
803 iface_attrs[IFACE_ATTR_RA_RETRANSTIME].name, iface->name);
804 }
805
806 if ((c = tb[IFACE_ATTR_RA_HOPLIMIT])) {
807 uint32_t ra_hoplimit = blobmsg_get_u32(c);
808
809 if (ra_hoplimit <= 255)
810 iface->ra_hoplimit = ra_hoplimit;
811 else
812 syslog(LOG_ERR, "Invalid %s value configured for interface '%s'",
813 iface_attrs[IFACE_ATTR_RA_HOPLIMIT].name, iface->name);
814 }
815
816 if ((c = tb[IFACE_ATTR_RA_MTU])) {
817 uint32_t ra_mtu = blobmsg_get_u32(c);
818
819 if (ra_mtu >= 1280 || ra_mtu <= 65535)
820 iface->ra_mtu = ra_mtu;
821 else
822 syslog(LOG_ERR, "Invalid %s value configured for interface '%s'",
823 iface_attrs[IFACE_ATTR_RA_MTU].name, iface->name);
824 }
825
826 if ((c = tb[IFACE_ATTR_RA_SLAAC]))
827 iface->ra_slaac = blobmsg_get_bool(c);
828
829 if ((c = tb[IFACE_ATTR_RA_OFFLINK]))
830 iface->ra_not_onlink = blobmsg_get_bool(c);
831
832 if ((c = tb[IFACE_ATTR_RA_ADVROUTER]))
833 iface->ra_advrouter = blobmsg_get_bool(c);
834
835 if ((c = tb[IFACE_ATTR_RA_MININTERVAL]))
836 iface->ra_mininterval = blobmsg_get_u32(c);
837
838 if ((c = tb[IFACE_ATTR_RA_MAXINTERVAL]))
839 iface->ra_maxinterval = blobmsg_get_u32(c);
840
841 if ((c = tb[IFACE_ATTR_RA_LIFETIME]))
842 iface->ra_lifetime = blobmsg_get_u32(c);
843
844 if ((c = tb[IFACE_ATTR_RA_USELEASETIME]))
845 iface->ra_useleasetime = blobmsg_get_bool(c);
846
847 if ((c = tb[IFACE_ATTR_RA_DNS]))
848 iface->ra_dns = blobmsg_get_bool(c);
849
850 if ((c = tb[IFACE_ATTR_RA_PREFERENCE])) {
851 const char *prio = blobmsg_get_string(c);
852
853 if (!strcmp(prio, "high"))
854 iface->route_preference = 1;
855 else if (!strcmp(prio, "low"))
856 iface->route_preference = -1;
857 else if (!strcmp(prio, "medium") || !strcmp(prio, "default"))
858 iface->route_preference = 0;
859 else
860 syslog(LOG_ERR, "Invalid %s mode configured for interface '%s'",
861 iface_attrs[IFACE_ATTR_RA_PREFERENCE].name, iface->name);
862 }
863
864 if ((c = tb[IFACE_ATTR_PD_MANAGER]))
865 strncpy(iface->dhcpv6_pd_manager, blobmsg_get_string(c),
866 sizeof(iface->dhcpv6_pd_manager) - 1);
867
868 if ((c = tb[IFACE_ATTR_PD_CER]) &&
869 inet_pton(AF_INET6, blobmsg_get_string(c), &iface->dhcpv6_pd_cer) < 1)
870 syslog(LOG_ERR, "Invalid %s value configured for interface '%s'",
871 iface_attrs[IFACE_ATTR_PD_CER].name, iface->name);
872
873 if ((c = tb[IFACE_ATTR_NDPROXY_ROUTING]))
874 iface->learn_routes = blobmsg_get_bool(c);
875
876 if ((c = tb[IFACE_ATTR_NDPROXY_SLAVE]))
877 iface->external = blobmsg_get_bool(c);
878
879 if ((c = tb[IFACE_ATTR_PREFIX_FILTER])) {
880 const char *str = blobmsg_get_string(c);
881 char *astr = malloc(strlen(str) + 1);
882 char *delim;
883 int l;
884
885 if (!astr || !strcpy(astr, str) ||
886 (delim = strchr(astr, '/')) == NULL || (*(delim++) = 0) ||
887 sscanf(delim, "%i", &l) == 0 || l > 128 ||
888 inet_pton(AF_INET6, astr, &iface->pio_filter_addr) == 0)
889 iface->pio_filter_length = 0;
890 else
891 iface->pio_filter_length = l;
892
893 if (astr)
894 free(astr);
895 }
896
897 return 0;
898
899 err:
900 close_interface(iface);
901 return -1;
902 }
903
904 static int set_interface(struct uci_section *s)
905 {
906 blob_buf_init(&b, 0);
907 uci_to_blob(&b, s, &interface_attr_list);
908
909 return config_parse_interface(blob_data(b.head), blob_len(b.head), s->e.name, true);
910 }
911
912 static void lease_delete_assignments(struct lease *l, bool v6)
913 {
914 struct dhcp_assignment *a, *tmp;
915 unsigned int flag = v6 ? OAF_DHCPV6 : OAF_DHCPV4;
916
917 list_for_each_entry_safe(a, tmp, &l->assignments, lease_list) {
918 if (a->flags & flag)
919 free_assignment(a);
920 }
921 }
922
923 static void lease_update_assignments(struct lease *l)
924 {
925 struct dhcp_assignment *a;
926
927 list_for_each_entry(a, &l->assignments, lease_list) {
928 if (a->hostname)
929 free(a->hostname);
930 a->hostname = NULL;
931
932 if (l->hostname)
933 a->hostname = strdup(l->hostname);
934
935 a->leasetime = l->leasetime;
936 }
937 }
938
939 static int lease_cmp(const void *k1, const void *k2, _unused void *ptr)
940 {
941 const struct lease *l1 = k1, *l2 = k2;
942 int cmp = 0;
943
944 if (l1->duid_len != l2->duid_len)
945 return l1->duid_len - l2->duid_len;
946
947 if (l1->duid_len && l2->duid_len)
948 cmp = memcmp(l1->duid, l2->duid, l1->duid_len);
949
950 if (cmp)
951 return cmp;
952
953 return memcmp(l1->mac.ether_addr_octet, l2->mac.ether_addr_octet,
954 sizeof(l1->mac.ether_addr_octet));
955 }
956
957 static void lease_change_config(struct lease *l_old, struct lease *l_new)
958 {
959 bool update = false;
960
961 if ((!!l_new->hostname != !!l_old->hostname) ||
962 (l_new->hostname && strcmp(l_new->hostname, l_old->hostname))) {
963 free(l_old->hostname);
964 l_old->hostname = NULL;
965
966 if (l_new->hostname)
967 l_old->hostname = strdup(l_new->hostname);
968
969 update = true;
970 }
971
972 if (l_old->leasetime != l_new->leasetime) {
973 l_old->leasetime = l_new->leasetime;
974 update = true;
975 }
976
977 if (l_old->ipaddr != l_new->ipaddr) {
978 l_old->ipaddr = l_new->ipaddr;
979 lease_delete_assignments(l_old, false);
980 }
981
982 if (l_old->hostid != l_new->hostid) {
983 l_old->hostid = l_new->hostid;
984 lease_delete_assignments(l_old, true);
985 }
986
987 if (update)
988 lease_update_assignments(l_old);
989
990 free_lease(l_new);
991 }
992
993 static void lease_delete(struct lease *l)
994 {
995 struct dhcp_assignment *a, *tmp;
996
997 list_for_each_entry_safe(a, tmp, &l->assignments, lease_list)
998 free_assignment(a);
999
1000 free_lease(l);
1001 }
1002
1003 static void lease_update(_unused struct vlist_tree *tree, struct vlist_node *node_new,
1004 struct vlist_node *node_old)
1005 {
1006 struct lease *lease_new = container_of(node_new, struct lease, node);
1007 struct lease *lease_old = container_of(node_old, struct lease, node);
1008
1009 if (node_old && node_new)
1010 lease_change_config(lease_old, lease_new);
1011 else if (node_old)
1012 lease_delete(lease_old);
1013 }
1014
1015 struct lease *config_find_lease_by_duid(const uint8_t *duid, const uint16_t len)
1016 {
1017 struct lease *l;
1018
1019 vlist_for_each_element(&leases, l, node) {
1020 if (l->duid_len == len && !memcmp(l->duid, duid, len))
1021 return l;
1022 }
1023
1024 return NULL;
1025 }
1026
1027 struct lease *config_find_lease_by_mac(const uint8_t *mac)
1028 {
1029 struct lease *l;
1030
1031 vlist_for_each_element(&leases, l, node) {
1032 if (!memcmp(l->mac.ether_addr_octet, mac,
1033 sizeof(l->mac.ether_addr_octet)))
1034 return l;
1035 }
1036
1037 return NULL;
1038 }
1039
1040 struct lease *config_find_lease_by_hostid(const uint32_t hostid)
1041 {
1042 struct lease *l;
1043
1044 vlist_for_each_element(&leases, l, node) {
1045 if (l->hostid == hostid)
1046 return l;
1047 }
1048
1049 return NULL;
1050 }
1051
1052 struct lease *config_find_lease_by_ipaddr(const uint32_t ipaddr)
1053 {
1054 struct lease *l;
1055
1056 vlist_for_each_element(&leases, l, node) {
1057 if (l->ipaddr == ipaddr)
1058 return l;
1059 }
1060
1061 return NULL;
1062 }
1063
1064 void odhcpd_reload(void)
1065 {
1066 struct uci_context *uci = uci_alloc_context();
1067 struct interface *master = NULL, *i, *tmp;
1068
1069 if (!uci)
1070 return;
1071
1072 vlist_update(&leases);
1073 avl_for_each_element(&interfaces, i, avl)
1074 clean_interface(i);
1075
1076 struct uci_package *dhcp = NULL;
1077 if (!uci_load(uci, "dhcp", &dhcp)) {
1078 struct uci_element *e;
1079
1080 /* 1. Global settings */
1081 uci_foreach_element(&dhcp->sections, e) {
1082 struct uci_section *s = uci_to_section(e);
1083 if (!strcmp(s->type, "odhcpd"))
1084 set_config(s);
1085 }
1086
1087 /* 2. DHCP pools */
1088 uci_foreach_element(&dhcp->sections, e) {
1089 struct uci_section *s = uci_to_section(e);
1090 if (!strcmp(s->type, "dhcp"))
1091 set_interface(s);
1092 }
1093
1094 /* 3. Static leases */
1095 uci_foreach_element(&dhcp->sections, e) {
1096 struct uci_section* s = uci_to_section(e);
1097 if (!strcmp(s->type, "host"))
1098 set_lease_from_uci(s);
1099 }
1100 }
1101
1102 if (config.dhcp_statefile) {
1103 char *path = strdup(config.dhcp_statefile);
1104
1105 mkdir_p(dirname(path), 0755);
1106 free(path);
1107 }
1108
1109 vlist_flush(&leases);
1110
1111 #ifdef WITH_UBUS
1112 ubus_apply_network();
1113 #endif
1114
1115 bool any_dhcpv6_slave = false, any_ra_slave = false, any_ndp_slave = false;
1116
1117 /* Test for */
1118 avl_for_each_element(&interfaces, i, avl) {
1119 if (i->master)
1120 continue;
1121
1122 if (i->dhcpv6 == MODE_HYBRID || i->dhcpv6 == MODE_RELAY)
1123 any_dhcpv6_slave = true;
1124
1125 if (i->ra == MODE_HYBRID || i->ra == MODE_RELAY)
1126 any_ra_slave = true;
1127
1128 if (i->ndp == MODE_HYBRID || i->ndp == MODE_RELAY)
1129 any_ndp_slave = true;
1130 }
1131
1132 /* Evaluate hybrid mode for master */
1133 avl_for_each_element(&interfaces, i, avl) {
1134 if (!i->master)
1135 continue;
1136
1137 enum odhcpd_mode hybrid_mode = MODE_DISABLED;
1138 #ifdef WITH_UBUS
1139 if (!ubus_has_prefix(i->name, i->ifname))
1140 hybrid_mode = MODE_RELAY;
1141 #endif
1142
1143 if (i->dhcpv6 == MODE_HYBRID)
1144 i->dhcpv6 = hybrid_mode;
1145
1146 if (i->dhcpv6 == MODE_RELAY && !any_dhcpv6_slave)
1147 i->dhcpv6 = MODE_DISABLED;
1148
1149 if (i->ra == MODE_HYBRID)
1150 i->ra = hybrid_mode;
1151
1152 if (i->ra == MODE_RELAY && !any_ra_slave)
1153 i->ra = MODE_DISABLED;
1154
1155 if (i->ndp == MODE_HYBRID)
1156 i->ndp = hybrid_mode;
1157
1158 if (i->ndp == MODE_RELAY && !any_ndp_slave)
1159 i->ndp = MODE_DISABLED;
1160
1161 if (i->dhcpv6 == MODE_RELAY || i->ra == MODE_RELAY || i->ndp == MODE_RELAY)
1162 master = i;
1163 }
1164
1165
1166 avl_for_each_element_safe(&interfaces, i, avl, tmp) {
1167 if (i->inuse) {
1168 /* Resolve hybrid mode */
1169 if (i->dhcpv6 == MODE_HYBRID)
1170 i->dhcpv6 = (master && master->dhcpv6 == MODE_RELAY) ?
1171 MODE_RELAY : MODE_SERVER;
1172
1173 if (i->ra == MODE_HYBRID)
1174 i->ra = (master && master->ra == MODE_RELAY) ?
1175 MODE_RELAY : MODE_SERVER;
1176
1177 if (i->ndp == MODE_HYBRID)
1178 i->ndp = (master && master->ndp == MODE_RELAY) ?
1179 MODE_RELAY : MODE_DISABLED;
1180
1181 router_setup_interface(i, i->ra != MODE_DISABLED);
1182 dhcpv6_setup_interface(i, i->dhcpv6 != MODE_DISABLED);
1183 ndp_setup_interface(i, i->ndp != MODE_DISABLED);
1184 #ifdef DHCPV4_SUPPORT
1185 dhcpv4_setup_interface(i, i->dhcpv4 != MODE_DISABLED);
1186 #endif
1187 } else
1188 close_interface(i);
1189 }
1190
1191 uci_unload(uci, dhcp);
1192 uci_free_context(uci);
1193 }
1194
1195 static void handle_signal(int signal)
1196 {
1197 char b[1] = {0};
1198
1199 if (signal == SIGHUP) {
1200 if (write(reload_pipe[1], b, sizeof(b)) < 0) {}
1201 } else
1202 uloop_end();
1203 }
1204
1205 static void reload_cb(struct uloop_fd *u, _unused unsigned int events)
1206 {
1207 char b[512];
1208 if (read(u->fd, b, sizeof(b)) < 0) {}
1209
1210 odhcpd_reload();
1211 }
1212
1213 static struct uloop_fd reload_fd = { .fd = -1, .cb = reload_cb };
1214
1215 void odhcpd_run(void)
1216 {
1217 if (pipe2(reload_pipe, O_NONBLOCK | O_CLOEXEC) < 0) {}
1218
1219 reload_fd.fd = reload_pipe[0];
1220 uloop_fd_add(&reload_fd, ULOOP_READ);
1221
1222 signal(SIGTERM, handle_signal);
1223 signal(SIGINT, handle_signal);
1224 signal(SIGHUP, handle_signal);
1225
1226 #ifdef WITH_UBUS
1227 while (ubus_init())
1228 sleep(1);
1229 #endif
1230
1231 odhcpd_reload();
1232 uloop_run();
1233 }