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