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