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