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