Begin rewrite of NDP-relay
[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
7 #include <uci.h>
8 #include <uci_blob.h>
9
10 #include "odhcpd.h"
11
12 static struct blob_buf b;
13 static int reload_pipe[2];
14 struct list_head leases = LIST_HEAD_INIT(leases);
15 struct list_head interfaces = LIST_HEAD_INIT(interfaces);
16 struct config config = {false, NULL, NULL};
17
18 enum {
19 IFACE_ATTR_INTERFACE,
20 IFACE_ATTR_IFNAME,
21 IFACE_ATTR_NETWORKID,
22 IFACE_ATTR_DYNAMICDHCP,
23 IFACE_ATTR_IGNORE,
24 IFACE_ATTR_LEASETIME,
25 IFACE_ATTR_LIMIT,
26 IFACE_ATTR_START,
27 IFACE_ATTR_MASTER,
28 IFACE_ATTR_UPSTREAM,
29 IFACE_ATTR_RA,
30 IFACE_ATTR_DHCPV4,
31 IFACE_ATTR_DHCPV6,
32 IFACE_ATTR_NDP,
33 IFACE_ATTR_ROUTER,
34 IFACE_ATTR_DNS,
35 IFACE_ATTR_DOMAIN,
36 IFACE_ATTR_FILTER_CLASS,
37 IFACE_ATTR_DHCPV6_RAW,
38 IFACE_ATTR_RA_DEFAULT,
39 IFACE_ATTR_RA_MANAGEMENT,
40 IFACE_ATTR_RA_OFFLINK,
41 IFACE_ATTR_RA_PREFERENCE,
42 IFACE_ATTR_RA_ADVROUTER,
43 IFACE_ATTR_PD_MANAGER,
44 IFACE_ATTR_PD_CER,
45 IFACE_ATTR_NDPROXY_ROUTING,
46 IFACE_ATTR_NDPROXY_SLAVE,
47 IFACE_ATTR_MAX
48 };
49
50 static const struct blobmsg_policy iface_attrs[IFACE_ATTR_MAX] = {
51 [IFACE_ATTR_INTERFACE] = { .name = "interface", .type = BLOBMSG_TYPE_STRING },
52 [IFACE_ATTR_IFNAME] = { .name = "ifname", .type = BLOBMSG_TYPE_STRING },
53 [IFACE_ATTR_NETWORKID] = { .name = "networkid", .type = BLOBMSG_TYPE_STRING },
54 [IFACE_ATTR_DYNAMICDHCP] = { .name = "dynamicdhcp", .type = BLOBMSG_TYPE_BOOL },
55 [IFACE_ATTR_IGNORE] = { .name = "ignore", .type = BLOBMSG_TYPE_BOOL },
56 [IFACE_ATTR_LEASETIME] = { .name = "leasetime", .type = BLOBMSG_TYPE_STRING },
57 [IFACE_ATTR_START] = { .name = "start", .type = BLOBMSG_TYPE_INT32 },
58 [IFACE_ATTR_LIMIT] = { .name = "limit", .type = BLOBMSG_TYPE_INT32 },
59 [IFACE_ATTR_MASTER] = { .name = "master", .type = BLOBMSG_TYPE_BOOL },
60 [IFACE_ATTR_UPSTREAM] = { .name = "upstream", .type = BLOBMSG_TYPE_ARRAY },
61 [IFACE_ATTR_RA] = { .name = "ra", .type = BLOBMSG_TYPE_STRING },
62 [IFACE_ATTR_DHCPV4] = { .name = "dhcpv4", .type = BLOBMSG_TYPE_STRING },
63 [IFACE_ATTR_DHCPV6] = { .name = "dhcpv6", .type = BLOBMSG_TYPE_STRING },
64 [IFACE_ATTR_NDP] = { .name = "ndp", .type = BLOBMSG_TYPE_STRING },
65 [IFACE_ATTR_ROUTER] = { .name = "router", .type = BLOBMSG_TYPE_ARRAY },
66 [IFACE_ATTR_DNS] = { .name = "dns", .type = BLOBMSG_TYPE_ARRAY },
67 [IFACE_ATTR_DOMAIN] = { .name = "domain", .type = BLOBMSG_TYPE_ARRAY },
68 [IFACE_ATTR_FILTER_CLASS] = { .name = "filter_class", .type = BLOBMSG_TYPE_STRING },
69 [IFACE_ATTR_DHCPV6_RAW] = { .name = "dhcpv6_raw", .type = BLOBMSG_TYPE_STRING },
70 [IFACE_ATTR_PD_MANAGER] = { .name = "pd_manager", .type = BLOBMSG_TYPE_STRING },
71 [IFACE_ATTR_PD_CER] = { .name = "pd_cer", .type = BLOBMSG_TYPE_STRING },
72 [IFACE_ATTR_RA_DEFAULT] = { .name = "ra_default", .type = BLOBMSG_TYPE_INT32 },
73 [IFACE_ATTR_RA_MANAGEMENT] = { .name = "ra_management", .type = BLOBMSG_TYPE_INT32 },
74 [IFACE_ATTR_RA_OFFLINK] = { .name = "ra_offlink", .type = BLOBMSG_TYPE_BOOL },
75 [IFACE_ATTR_RA_PREFERENCE] = { .name = "ra_preference", .type = BLOBMSG_TYPE_STRING },
76 [IFACE_ATTR_RA_ADVROUTER] = { .name = "ra_advrouter", .type = BLOBMSG_TYPE_BOOL },
77 [IFACE_ATTR_NDPROXY_ROUTING] = { .name = "ndproxy_routing", .type = BLOBMSG_TYPE_BOOL },
78 [IFACE_ATTR_NDPROXY_SLAVE] = { .name = "ndproxy_slave", .type = BLOBMSG_TYPE_BOOL },
79 };
80
81 static const struct uci_blob_param_info iface_attr_info[IFACE_ATTR_MAX] = {
82 [IFACE_ATTR_UPSTREAM] = { .type = BLOBMSG_TYPE_STRING },
83 [IFACE_ATTR_DNS] = { .type = BLOBMSG_TYPE_STRING },
84 [IFACE_ATTR_DOMAIN] = { .type = BLOBMSG_TYPE_STRING },
85 };
86
87 const struct uci_blob_param_list interface_attr_list = {
88 .n_params = IFACE_ATTR_MAX,
89 .params = iface_attrs,
90 .info = iface_attr_info,
91 };
92
93
94 enum {
95 LEASE_ATTR_IP,
96 LEASE_ATTR_MAC,
97 LEASE_ATTR_DUID,
98 LEASE_ATTR_HOSTID,
99 LEASE_ATTR_NAME,
100 LEASE_ATTR_MAX
101 };
102
103
104 static const struct blobmsg_policy lease_attrs[LEASE_ATTR_MAX] = {
105 [LEASE_ATTR_IP] = { .name = "ip", .type = BLOBMSG_TYPE_STRING },
106 [LEASE_ATTR_MAC] = { .name = "mac", .type = BLOBMSG_TYPE_STRING },
107 [LEASE_ATTR_DUID] = { .name = "duid", .type = BLOBMSG_TYPE_STRING },
108 [LEASE_ATTR_HOSTID] = { .name = "hostid", .type = BLOBMSG_TYPE_STRING },
109 [LEASE_ATTR_NAME] = { .name = "name", .type = BLOBMSG_TYPE_STRING },
110 };
111
112
113 const struct uci_blob_param_list lease_attr_list = {
114 .n_params = LEASE_ATTR_MAX,
115 .params = lease_attrs,
116 };
117
118
119 enum {
120 ODHCPD_ATTR_MAINDHCP,
121 ODHCPD_ATTR_LEASEFILE,
122 ODHCPD_ATTR_LEASETRIGGER,
123 ODHCPD_ATTR_MAX
124 };
125
126
127 static const struct blobmsg_policy odhcpd_attrs[LEASE_ATTR_MAX] = {
128 [ODHCPD_ATTR_MAINDHCP] = { .name = "maindhcp", .type = BLOBMSG_TYPE_BOOL },
129 [ODHCPD_ATTR_LEASEFILE] = { .name = "leasefile", .type = BLOBMSG_TYPE_STRING },
130 [ODHCPD_ATTR_LEASETRIGGER] = { .name = "leasetrigger", .type = BLOBMSG_TYPE_STRING },
131 };
132
133
134 const struct uci_blob_param_list odhcpd_attr_list = {
135 .n_params = ODHCPD_ATTR_MAX,
136 .params = odhcpd_attrs,
137 };
138
139
140 static struct interface* get_interface(const char *name)
141 {
142 struct interface *c;
143 list_for_each_entry(c, &interfaces, head)
144 if (!strcmp(c->name, name))
145 return c;
146 return NULL;
147 }
148
149
150 static void clean_interface(struct interface *iface)
151 {
152 free(iface->dns);
153 free(iface->search);
154 free(iface->upstream);
155 free(iface->dhcpv4_router);
156 free(iface->dhcpv4_dns);
157 free(iface->dhcpv6_raw);
158 free(iface->filter_class);
159 memset(&iface->ra, 0, sizeof(*iface) - offsetof(struct interface, ra));
160 }
161
162
163 static void close_interface(struct interface *iface)
164 {
165 if (iface->head.next)
166 list_del(&iface->head);
167
168 setup_router_interface(iface, false);
169 setup_dhcpv6_interface(iface, false);
170 setup_ndp_interface(iface, false);
171 setup_dhcpv4_interface(iface, false);
172
173 clean_interface(iface);
174 free(iface);
175 }
176
177
178 static int parse_mode(const char *mode)
179 {
180 if (!strcmp(mode, "disabled")) {
181 return RELAYD_DISABLED;
182 } else if (!strcmp(mode, "server")) {
183 return RELAYD_SERVER;
184 } else if (!strcmp(mode, "relay")) {
185 return RELAYD_RELAY;
186 } else if (!strcmp(mode, "hybrid")) {
187 return RELAYD_HYBRID;
188 } else {
189 return -1;
190 }
191 }
192
193
194 static void set_config(struct uci_section *s)
195 {
196 struct blob_attr *tb[ODHCPD_ATTR_MAX], *c;
197
198 blob_buf_init(&b, 0);
199 uci_to_blob(&b, s, &odhcpd_attr_list);
200 blobmsg_parse(odhcpd_attrs, ODHCPD_ATTR_MAX, tb, blob_data(b.head), blob_len(b.head));
201
202 if ((c = tb[ODHCPD_ATTR_MAINDHCP]))
203 config.legacy = blobmsg_get_bool(c);
204
205 if ((c = tb[ODHCPD_ATTR_LEASEFILE])) {
206 free(config.dhcp_statefile);
207 config.dhcp_statefile = strdup(blobmsg_get_string(c));
208 }
209
210 if ((c = tb[ODHCPD_ATTR_LEASETRIGGER])) {
211 free(config.dhcp_cb);
212 config.dhcp_cb = strdup(blobmsg_get_string(c));
213 }
214 }
215
216
217 static int set_lease(struct uci_section *s)
218 {
219 struct blob_attr *tb[LEASE_ATTR_MAX], *c;
220
221 blob_buf_init(&b, 0);
222 uci_to_blob(&b, s, &lease_attr_list);
223 blobmsg_parse(lease_attrs, LEASE_ATTR_MAX, tb, blob_data(b.head), blob_len(b.head));
224
225 size_t hostlen = 1;
226 if ((c = tb[LEASE_ATTR_NAME]))
227 hostlen = blobmsg_data_len(c);
228
229 struct lease *lease = calloc(1, sizeof(*lease) + hostlen);
230 if (!lease)
231 goto err;
232
233 if (hostlen > 1)
234 memcpy(lease->hostname, blobmsg_get_string(c), hostlen);
235
236 if ((c = tb[LEASE_ATTR_IP]))
237 if (inet_pton(AF_INET, blobmsg_get_string(c), &lease->ipaddr) < 0)
238 goto err;
239
240 if ((c = tb[LEASE_ATTR_MAC]))
241 if (!ether_aton_r(blobmsg_get_string(c), &lease->mac))
242 goto err;
243
244 if ((c = tb[LEASE_ATTR_DUID])) {
245 size_t duidlen = (blobmsg_data_len(c) - 1) / 2;
246 lease->duid = malloc(duidlen);
247 if (!lease->duid)
248 goto err;
249
250 ssize_t len = odhcpd_unhexlify(lease->duid,
251 duidlen, blobmsg_get_string(c));
252
253 if (len < 0)
254 goto err;
255
256 lease->duid_len = len;
257 }
258
259 if ((c = tb[LEASE_ATTR_HOSTID])) {
260 errno = 0;
261 lease->hostid = strtoul(blobmsg_get_string(c), NULL, 16);
262 if (errno)
263 goto err;
264 }
265
266 list_add(&lease->head, &leases);
267 return 0;
268
269 err:
270 if (lease) {
271 free(lease->duid);
272 free(lease);
273 }
274 return -1;
275 }
276
277
278 int config_parse_interface(void *data, size_t len, const char *name, bool overwrite)
279 {
280 struct blob_attr *tb[IFACE_ATTR_MAX], *c;
281 blobmsg_parse(iface_attrs, IFACE_ATTR_MAX, tb, data, len);
282
283 if (tb[IFACE_ATTR_INTERFACE])
284 name = blobmsg_get_string(tb[IFACE_ATTR_INTERFACE]);
285
286 if (!name)
287 return -1;
288
289 struct interface *iface = get_interface(name);
290 if (!iface) {
291 iface = calloc(1, sizeof(*iface));
292 if (!iface)
293 return -1;
294
295 strncpy(iface->name, name, sizeof(iface->name) - 1);
296 list_add(&iface->head, &interfaces);
297 overwrite = true;
298 }
299
300 const char *ifname = NULL;
301 #ifdef WITH_UBUS
302 if (overwrite || !iface->ifname[0])
303 ifname = ubus_get_ifname(name);
304 #endif
305
306 if (overwrite) {
307 if ((c = tb[IFACE_ATTR_IFNAME]))
308 ifname = blobmsg_get_string(c);
309 else if ((c = tb[IFACE_ATTR_NETWORKID]))
310 ifname = blobmsg_get_string(c);
311 }
312
313 if (!iface->ifname[0] && !ifname)
314 return -1;
315
316 if (ifname)
317 strncpy(iface->ifname, ifname, sizeof(iface->ifname) - 1);
318
319 if ((iface->ifindex = if_nametoindex(iface->ifname)) <= 0)
320 return -1;
321
322 iface->inuse = true;
323
324 if ((c = tb[IFACE_ATTR_DYNAMICDHCP]))
325 iface->no_dynamic_dhcp = !blobmsg_get_bool(c);
326
327 if (overwrite && (c = tb[IFACE_ATTR_IGNORE]))
328 iface->ignore = blobmsg_get_bool(c);
329
330 if ((c = tb[IFACE_ATTR_LEASETIME])) {
331 char *val = blobmsg_get_string(c), *endptr;
332 double time = strtod(val, &endptr);
333 if (time && endptr[0]) {
334 if (endptr[0] == 's')
335 time *= 1;
336 else if (endptr[0] == 'm')
337 time *= 60;
338 else if (endptr[0] == 'h')
339 time *= 3600;
340 else if (endptr[0] == 'd')
341 time *= 24 * 3600;
342 else if (endptr[0] == 'w')
343 time *= 7 * 24 * 3600;
344 else
345 goto err;
346 }
347
348 if (time >= 60)
349 iface->dhcpv4_leasetime = time;
350 }
351
352 if ((c = tb[IFACE_ATTR_START])) {
353 iface->dhcpv4_start.s_addr = htonl(blobmsg_get_u32(c));
354
355 if (config.legacy)
356 iface->dhcpv4 = RELAYD_SERVER;
357 }
358
359 if ((c = tb[IFACE_ATTR_LIMIT]))
360 iface->dhcpv4_end.s_addr = htonl(
361 ntohl(iface->dhcpv4_start.s_addr) + blobmsg_get_u32(c));
362
363 if ((c = tb[IFACE_ATTR_MASTER]))
364 iface->master = blobmsg_get_bool(c);
365
366 if (overwrite && (c = tb[IFACE_ATTR_UPSTREAM])) {
367 struct blob_attr *cur;
368 unsigned rem;
369
370 blobmsg_for_each_attr(cur, c, rem) {
371 if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING || !blobmsg_check_attr(cur, NULL))
372 continue;
373
374 iface->upstream = realloc(iface->upstream,
375 iface->upstream_len + blobmsg_data_len(cur));
376 if (!iface->upstream)
377 goto err;
378
379 memcpy(iface->upstream + iface->upstream_len, blobmsg_get_string(cur), blobmsg_data_len(cur));
380 iface->upstream_len += blobmsg_data_len(cur);
381 }
382 }
383
384 int mode;
385 if ((c = tb[IFACE_ATTR_RA])) {
386 if ((mode = parse_mode(blobmsg_get_string(c))) >= 0)
387 iface->ra = mode;
388 else
389 goto err;
390 }
391
392 if ((c = tb[IFACE_ATTR_DHCPV4])) {
393 if ((mode = parse_mode(blobmsg_get_string(c))) >= 0)
394 iface->dhcpv4 = mode;
395 else
396 goto err;
397 }
398
399 if ((c = tb[IFACE_ATTR_DHCPV6])) {
400 if ((mode = parse_mode(blobmsg_get_string(c))) >= 0)
401 iface->dhcpv6 = mode;
402 else
403 goto err;
404 }
405
406 if ((c = tb[IFACE_ATTR_NDP])) {
407 if ((mode = parse_mode(blobmsg_get_string(c))) >= 0)
408 iface->ndp = mode;
409 else
410 goto err;
411 }
412
413 if ((c = tb[IFACE_ATTR_ROUTER])) {
414 struct blob_attr *cur;
415 unsigned rem;
416
417 blobmsg_for_each_attr(cur, c, rem) {
418 if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING || !blobmsg_check_attr(cur, NULL))
419 continue;
420
421 struct in_addr addr4;
422 if (inet_pton(AF_INET, blobmsg_get_string(cur), &addr4) == 1) {
423 iface->dhcpv4_router = realloc(iface->dhcpv4_router,
424 (++iface->dhcpv4_router_cnt) * sizeof(*iface->dhcpv4_router));
425 if (!iface->dhcpv4_router)
426 goto err;
427
428 iface->dhcpv4_router[iface->dhcpv4_router_cnt - 1] = addr4;
429 } else {
430 goto err;
431 }
432 }
433 }
434
435 if ((c = tb[IFACE_ATTR_DNS])) {
436 struct blob_attr *cur;
437 unsigned rem;
438
439 iface->always_rewrite_dns = true;
440 blobmsg_for_each_attr(cur, c, rem) {
441 if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING || !blobmsg_check_attr(cur, NULL))
442 continue;
443
444 struct in_addr addr4;
445 struct in6_addr addr6;
446 if (inet_pton(AF_INET, blobmsg_get_string(cur), &addr4) == 1) {
447 iface->dhcpv4_dns = realloc(iface->dhcpv4_dns,
448 (++iface->dhcpv4_dns_cnt) * sizeof(*iface->dhcpv4_dns));
449 if (!iface->dhcpv4_dns)
450 goto err;
451
452 iface->dhcpv4_dns[iface->dhcpv4_dns_cnt - 1] = addr4;
453 } else if (inet_pton(AF_INET6, blobmsg_get_string(cur), &addr6) == 1) {
454 iface->dns = realloc(iface->dns,
455 (++iface->dns_cnt) * sizeof(*iface->dns));
456 if (!iface->dns)
457 goto err;
458
459 iface->dns[iface->dns_cnt - 1] = addr6;
460 } else {
461 goto err;
462 }
463 }
464 }
465
466 if ((c = tb[IFACE_ATTR_DOMAIN])) {
467 struct blob_attr *cur;
468 unsigned rem;
469
470 blobmsg_for_each_attr(cur, c, rem) {
471 if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING || !blobmsg_check_attr(cur, NULL))
472 continue;
473
474 uint8_t buf[256];
475 char *domain = blobmsg_get_string(cur);
476 size_t domainlen = strlen(domain);
477 if (domainlen > 0 && domain[domainlen - 1] == '.')
478 domain[domainlen - 1] = 0;
479
480 int len = dn_comp(domain, buf, sizeof(buf), NULL, NULL);
481 if (len <= 0)
482 goto err;
483
484 iface->search = realloc(iface->search, iface->search_len + len);
485 if (!iface->search)
486 goto err;
487
488 memcpy(&iface->search[iface->search_len], buf, len);
489 iface->search_len += len;
490 }
491 }
492
493 if ((c = tb[IFACE_ATTR_FILTER_CLASS])) {
494 iface->filter_class = realloc(iface->filter_class, blobmsg_data_len(c) + 1);
495 memcpy(iface->filter_class, blobmsg_get_string(c), blobmsg_data_len(c) + 1);
496 }
497
498 if ((c = tb[IFACE_ATTR_DHCPV6_RAW])) {
499 iface->dhcpv6_raw_len = blobmsg_data_len(c) / 2;
500 iface->dhcpv6_raw = realloc(iface->dhcpv6_raw, iface->dhcpv6_raw_len);
501 odhcpd_unhexlify(iface->dhcpv6_raw, iface->dhcpv6_raw_len, blobmsg_get_string(c));
502 }
503
504 if ((c = tb[IFACE_ATTR_RA_DEFAULT]))
505 iface->default_router = blobmsg_get_u32(c);
506
507 if ((c = tb[IFACE_ATTR_RA_MANAGEMENT]))
508 iface->managed = blobmsg_get_u32(c);
509 else if (overwrite)
510 iface->managed = 1;
511
512 if ((c = tb[IFACE_ATTR_RA_OFFLINK]))
513 iface->ra_not_onlink = blobmsg_get_bool(c);
514
515 if ((c = tb[IFACE_ATTR_RA_ADVROUTER]))
516 iface->ra_advrouter = blobmsg_get_bool(c);
517
518 if ((c = tb[IFACE_ATTR_RA_PREFERENCE])) {
519 const char *prio = blobmsg_get_string(c);
520
521 if (!strcmp(prio, "high"))
522 iface->route_preference = 1;
523 else if (!strcmp(prio, "low"))
524 iface->route_preference = -1;
525 else if (!strcmp(prio, "medium") || !strcmp(prio, "default"))
526 iface->route_preference = 0;
527 else
528 goto err;
529 }
530
531 if ((c = tb[IFACE_ATTR_PD_MANAGER]))
532 strncpy(iface->dhcpv6_pd_manager, blobmsg_get_string(c),
533 sizeof(iface->dhcpv6_pd_manager) - 1);
534
535 if ((c = tb[IFACE_ATTR_PD_CER]) &&
536 inet_pton(AF_INET6, blobmsg_get_string(c), &iface->dhcpv6_pd_cer) < 1)
537 goto err;
538
539 if ((c = tb[IFACE_ATTR_NDPROXY_ROUTING]))
540 iface->learn_routes = blobmsg_get_bool(c);
541 else if (overwrite)
542 iface->learn_routes = true;
543
544 if ((c = tb[IFACE_ATTR_NDPROXY_SLAVE]))
545 iface->external = blobmsg_get_bool(c);
546
547 return 0;
548
549 err:
550 close_interface(iface);
551 return -1;
552 }
553
554 static int set_interface(struct uci_section *s)
555 {
556 blob_buf_init(&b, 0);
557 uci_to_blob(&b, s, &interface_attr_list);
558 return config_parse_interface(blob_data(b.head), blob_len(b.head), s->e.name, true);
559 }
560
561
562 void odhcpd_reload(void)
563 {
564 struct uci_context *uci = uci_alloc_context();
565 while (!list_empty(&leases)) {
566 struct lease *l = list_first_entry(&leases, struct lease, head);
567 list_del(&l->head);
568 free(l->duid);
569 free(l);
570 }
571
572 struct interface *master = NULL, *i, *n;
573
574 if (!uci)
575 return;
576
577 list_for_each_entry(i, &interfaces, head)
578 clean_interface(i);
579
580 struct uci_package *dhcp = NULL;
581 if (!uci_load(uci, "dhcp", &dhcp)) {
582 struct uci_element *e;
583 uci_foreach_element(&dhcp->sections, e) {
584 struct uci_section *s = uci_to_section(e);
585 if (!strcmp(s->type, "host"))
586 set_lease(s);
587 else if (!strcmp(s->type, "odhcpd"))
588 set_config(s);
589 }
590
591 uci_foreach_element(&dhcp->sections, e) {
592 struct uci_section *s = uci_to_section(e);
593 if (!strcmp(s->type, "dhcp"))
594 set_interface(s);
595 }
596 }
597
598
599 #ifdef WITH_UBUS
600 ubus_apply_network();
601 #endif
602
603 bool any_dhcpv6_slave = false, any_ra_slave = false, any_ndp_slave = false;
604
605 // Test for
606 list_for_each_entry(i, &interfaces, head) {
607 if (i->master)
608 continue;
609
610 if (i->dhcpv6 == RELAYD_HYBRID || i->dhcpv6 == RELAYD_RELAY)
611 any_dhcpv6_slave = true;
612
613 if (i->ra == RELAYD_HYBRID || i->ra == RELAYD_RELAY)
614 any_ra_slave = true;
615
616 if (i->ndp == RELAYD_HYBRID || i->ndp == RELAYD_RELAY)
617 any_ndp_slave = true;
618 }
619
620 // Evaluate hybrid mode for master
621 list_for_each_entry(i, &interfaces, head) {
622 if (!i->master)
623 continue;
624
625 enum odhcpd_mode hybrid_mode = RELAYD_DISABLED;
626 #ifdef WITH_UBUS
627 if (!ubus_has_prefix(i->name, i->ifname))
628 hybrid_mode = RELAYD_RELAY;
629 #endif
630
631 if (i->dhcpv6 == RELAYD_HYBRID)
632 i->dhcpv6 = hybrid_mode;
633
634 if (i->dhcpv6 == RELAYD_RELAY && !any_dhcpv6_slave)
635 i->dhcpv6 = RELAYD_DISABLED;
636
637 if (i->ra == RELAYD_HYBRID)
638 i->ra = hybrid_mode;
639
640 if (i->ra == RELAYD_RELAY && !any_ra_slave)
641 i->ra = RELAYD_DISABLED;
642
643 if (i->ndp == RELAYD_HYBRID)
644 i->ndp = hybrid_mode;
645
646 if (i->ndp == RELAYD_RELAY && !any_ndp_slave)
647 i->ndp = RELAYD_DISABLED;
648
649 if (i->dhcpv6 == RELAYD_RELAY || i->ra == RELAYD_RELAY || i->ndp == RELAYD_RELAY)
650 master = i;
651 }
652
653
654 list_for_each_entry_safe(i, n, &interfaces, head) {
655 if (i->inuse) {
656 // Resolve hybrid mode
657 if (i->dhcpv6 == RELAYD_HYBRID)
658 i->dhcpv6 = (master && master->dhcpv6 == RELAYD_RELAY) ?
659 RELAYD_RELAY : RELAYD_SERVER;
660
661 if (i->ra == RELAYD_HYBRID)
662 i->ra = (master && master->ra == RELAYD_RELAY) ?
663 RELAYD_RELAY : RELAYD_SERVER;
664
665 if (i->ndp == RELAYD_HYBRID)
666 i->ndp = (master && master->ndp == RELAYD_RELAY) ?
667 RELAYD_RELAY : RELAYD_DISABLED;
668
669 setup_router_interface(i, true);
670 setup_dhcpv6_interface(i, true);
671 setup_ndp_interface(i, true);
672 setup_dhcpv4_interface(i, true);
673 } else {
674 close_interface(i);
675 }
676 }
677
678 uci_unload(uci, dhcp);
679 uci_free_context(uci);
680 }
681
682
683 static void handle_signal(int signal)
684 {
685 char b[1] = {0};
686 if (signal == SIGHUP)
687 write(reload_pipe[1], b, sizeof(b));
688 else
689 uloop_end();
690 }
691
692
693
694 static void reload_cb(struct uloop_fd *u, _unused unsigned int events)
695 {
696 char b[512];
697 read(u->fd, b, sizeof(b));
698 odhcpd_reload();
699 }
700
701 static struct uloop_fd reload_fd = { .cb = reload_cb };
702
703 void odhcpd_run(void)
704 {
705 pipe2(reload_pipe, O_NONBLOCK | O_CLOEXEC);
706 reload_fd.fd = reload_pipe[0];
707 uloop_fd_add(&reload_fd, ULOOP_READ);
708
709 signal(SIGTERM, handle_signal);
710 signal(SIGINT, handle_signal);
711 signal(SIGHUP, handle_signal);
712
713 #ifdef WITH_UBUS
714 while (init_ubus())
715 sleep(1);
716 #endif
717
718 odhcpd_reload();
719 uloop_run();
720 }
721