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