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