treewide: give file descriptors safe initial value
[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 <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 set_interface_defaults(iface);
444
445 avl_insert(&interfaces, &iface->avl);
446 get_addrs = overwrite = true;
447 }
448
449 const char *ifname = NULL;
450 if (overwrite) {
451 if ((c = tb[IFACE_ATTR_IFNAME]))
452 ifname = blobmsg_get_string(c);
453 else if ((c = tb[IFACE_ATTR_NETWORKID]))
454 ifname = blobmsg_get_string(c);
455 }
456
457 #ifdef WITH_UBUS
458 if (overwrite || !iface->ifname)
459 ifname = ubus_get_ifname(name);
460 #endif
461
462 if (!iface->ifname && !ifname)
463 goto err;
464
465 if (ifname) {
466 free(iface->ifname);
467 iface->ifname = strdup(ifname);
468
469 if (!iface->ifname)
470 goto err;
471
472 if (!iface->ifindex &&
473 (iface->ifindex = if_nametoindex(iface->ifname)) <= 0)
474 goto err;
475 }
476
477 if (get_addrs) {
478 ssize_t len = netlink_get_interface_addrs(iface->ifindex,
479 true, &iface->addr6);
480
481 if (len > 0)
482 iface->addr6_len = len;
483
484 len = netlink_get_interface_addrs(iface->ifindex,
485 false, &iface->addr4);
486 if (len > 0)
487 iface->addr4_len = len;
488 }
489
490 iface->inuse = true;
491
492 if ((c = tb[IFACE_ATTR_DYNAMICDHCP]))
493 iface->no_dynamic_dhcp = !blobmsg_get_bool(c);
494
495 if (overwrite && (c = tb[IFACE_ATTR_IGNORE]))
496 iface->ignore = blobmsg_get_bool(c);
497
498 if ((c = tb[IFACE_ATTR_LEASETIME])) {
499 double time = parse_leasetime(c);
500 if (time < 0)
501 goto err;
502
503 iface->dhcpv4_leasetime = time;
504 }
505
506 if ((c = tb[IFACE_ATTR_START])) {
507 iface->dhcpv4_start.s_addr = htonl(blobmsg_get_u32(c));
508 iface->dhcpv4_end.s_addr = htonl(ntohl(iface->dhcpv4_start.s_addr) +
509 LIMIT_DEFAULT - 1);
510
511 if (config.main_dhcpv4 && config.legacy)
512 iface->dhcpv4 = MODE_SERVER;
513 }
514
515 if ((c = tb[IFACE_ATTR_LIMIT]))
516 iface->dhcpv4_end.s_addr = htonl(ntohl(iface->dhcpv4_start.s_addr) +
517 blobmsg_get_u32(c) - 1);
518
519 if ((c = tb[IFACE_ATTR_MASTER]))
520 iface->master = blobmsg_get_bool(c);
521
522 if (overwrite && (c = tb[IFACE_ATTR_UPSTREAM])) {
523 struct blob_attr *cur;
524 unsigned rem;
525
526 blobmsg_for_each_attr(cur, c, rem) {
527 if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING || !blobmsg_check_attr(cur, false))
528 continue;
529
530 iface->upstream = realloc(iface->upstream,
531 iface->upstream_len + blobmsg_data_len(cur));
532 if (!iface->upstream)
533 goto err;
534
535 memcpy(iface->upstream + iface->upstream_len, blobmsg_get_string(cur), blobmsg_data_len(cur));
536 iface->upstream_len += blobmsg_data_len(cur);
537 }
538 }
539
540 int mode;
541 if ((c = tb[IFACE_ATTR_RA])) {
542 if ((mode = parse_mode(blobmsg_get_string(c))) >= 0)
543 iface->ra = mode;
544 else
545 goto err;
546 }
547
548 if ((c = tb[IFACE_ATTR_DHCPV4])) {
549 if ((mode = parse_mode(blobmsg_get_string(c))) >= 0) {
550 if (config.main_dhcpv4)
551 iface->dhcpv4 = mode;
552 }
553 else
554 goto err;
555 }
556
557 if ((c = tb[IFACE_ATTR_DHCPV6])) {
558 if ((mode = parse_mode(blobmsg_get_string(c))) >= 0)
559 iface->dhcpv6 = mode;
560 else
561 goto err;
562 }
563
564 if ((c = tb[IFACE_ATTR_NDP])) {
565 if ((mode = parse_mode(blobmsg_get_string(c))) >= 0)
566 iface->ndp = mode;
567 else
568 goto err;
569 }
570
571 if ((c = tb[IFACE_ATTR_ROUTER])) {
572 struct blob_attr *cur;
573 unsigned rem;
574
575 blobmsg_for_each_attr(cur, c, rem) {
576 if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING || !blobmsg_check_attr(cur, false))
577 continue;
578
579 struct in_addr addr4;
580 if (inet_pton(AF_INET, blobmsg_get_string(cur), &addr4) == 1) {
581 iface->dhcpv4_router = realloc(iface->dhcpv4_router,
582 (++iface->dhcpv4_router_cnt) * sizeof(*iface->dhcpv4_router));
583 if (!iface->dhcpv4_router)
584 goto err;
585
586 iface->dhcpv4_router[iface->dhcpv4_router_cnt - 1] = addr4;
587 } else
588 goto err;
589 }
590 }
591
592 if ((c = tb[IFACE_ATTR_DNS])) {
593 struct blob_attr *cur;
594 unsigned rem;
595
596 iface->always_rewrite_dns = true;
597 blobmsg_for_each_attr(cur, c, rem) {
598 if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING || !blobmsg_check_attr(cur, false))
599 continue;
600
601 struct in_addr addr4;
602 struct in6_addr addr6;
603 if (inet_pton(AF_INET, blobmsg_get_string(cur), &addr4) == 1) {
604 if (addr4.s_addr == INADDR_ANY)
605 goto err;
606
607 iface->dhcpv4_dns = realloc(iface->dhcpv4_dns,
608 (++iface->dhcpv4_dns_cnt) * sizeof(*iface->dhcpv4_dns));
609 if (!iface->dhcpv4_dns)
610 goto err;
611
612 iface->dhcpv4_dns[iface->dhcpv4_dns_cnt - 1] = addr4;
613 } else if (inet_pton(AF_INET6, blobmsg_get_string(cur), &addr6) == 1) {
614 if (IN6_IS_ADDR_UNSPECIFIED(&addr6))
615 goto err;
616
617 iface->dns = realloc(iface->dns,
618 (++iface->dns_cnt) * sizeof(*iface->dns));
619 if (!iface->dns)
620 goto err;
621
622 iface->dns[iface->dns_cnt - 1] = addr6;
623 } else
624 goto err;
625 }
626 }
627
628 if ((c = tb[IFACE_ATTR_DOMAIN])) {
629 struct blob_attr *cur;
630 unsigned rem;
631
632 blobmsg_for_each_attr(cur, c, rem) {
633 if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING || !blobmsg_check_attr(cur, false))
634 continue;
635
636 uint8_t buf[256];
637 char *domain = blobmsg_get_string(cur);
638 size_t domainlen = strlen(domain);
639 if (domainlen > 0 && domain[domainlen - 1] == '.')
640 domain[domainlen - 1] = 0;
641
642 int len = dn_comp(domain, buf, sizeof(buf), NULL, NULL);
643 if (len <= 0)
644 goto err;
645
646 iface->search = realloc(iface->search, iface->search_len + len);
647 if (!iface->search)
648 goto err;
649
650 memcpy(&iface->search[iface->search_len], buf, len);
651 iface->search_len += len;
652 }
653 }
654
655 if ((c = tb[IFACE_ATTR_FILTER_CLASS])) {
656 iface->filter_class = realloc(iface->filter_class, blobmsg_data_len(c) + 1);
657 memcpy(iface->filter_class, blobmsg_get_string(c), blobmsg_data_len(c) + 1);
658 }
659
660 if ((c = tb[IFACE_ATTR_DHCPV4_FORCERECONF]))
661 iface->dhcpv4_forcereconf = blobmsg_get_bool(c);
662
663 if ((c = tb[IFACE_ATTR_DHCPV6_RAW])) {
664 iface->dhcpv6_raw_len = blobmsg_data_len(c) / 2;
665 iface->dhcpv6_raw = realloc(iface->dhcpv6_raw, iface->dhcpv6_raw_len);
666 odhcpd_unhexlify(iface->dhcpv6_raw, iface->dhcpv6_raw_len, blobmsg_get_string(c));
667 }
668
669 if ((c = tb[IFACE_ATTR_DHCPV6_ASSIGNALL]))
670 iface->dhcpv6_assignall = blobmsg_get_bool(c);
671
672 if ((c = tb[IFACE_ATTR_DHCPV6_PD]))
673 iface->dhcpv6_pd = blobmsg_get_bool(c);
674
675 if ((c = tb[IFACE_ATTR_DHCPV6_NA]))
676 iface->dhcpv6_na = blobmsg_get_bool(c);
677
678 if ((c = tb[IFACE_ATTR_RA_DEFAULT]))
679 iface->default_router = blobmsg_get_u32(c);
680
681 if ((c = tb[IFACE_ATTR_RA_MANAGEMENT]))
682 iface->ra_managed = blobmsg_get_u32(c);
683
684 if ((c = tb[IFACE_ATTR_RA_REACHABLETIME])) {
685 uint32_t ra_reachabletime = blobmsg_get_u32(c);
686 if (ra_reachabletime > 3600000)
687 goto err;
688
689 iface->ra_reachabletime = ra_reachabletime;
690 }
691
692 if ((c = tb[IFACE_ATTR_RA_RETRANSTIME])) {
693 uint32_t ra_retranstime = blobmsg_get_u32(c);
694 if (ra_retranstime > 60000)
695 goto err;
696
697 iface->ra_retranstime = ra_retranstime;
698 }
699
700 if ((c = tb[IFACE_ATTR_RA_HOPLIMIT])) {
701 uint32_t ra_hoplimit = blobmsg_get_u32(c);
702 if (ra_hoplimit > 255)
703 goto err;
704
705 iface->ra_hoplimit = ra_hoplimit;
706 }
707
708 if ((c = tb[IFACE_ATTR_RA_MTU])) {
709 uint32_t ra_mtu = blobmsg_get_u32(c);
710 if (ra_mtu < 1280 || ra_mtu > 65535)
711 goto err;
712
713 iface->ra_mtu = ra_mtu;
714 }
715
716 if ((c = tb[IFACE_ATTR_RA_OFFLINK]))
717 iface->ra_not_onlink = blobmsg_get_bool(c);
718
719 if ((c = tb[IFACE_ATTR_RA_ADVROUTER]))
720 iface->ra_advrouter = blobmsg_get_bool(c);
721
722 if ((c = tb[IFACE_ATTR_RA_MININTERVAL]))
723 iface->ra_mininterval = blobmsg_get_u32(c);
724
725 if ((c = tb[IFACE_ATTR_RA_MAXINTERVAL]))
726 iface->ra_maxinterval = blobmsg_get_u32(c);
727
728 if ((c = tb[IFACE_ATTR_RA_LIFETIME]))
729 iface->ra_lifetime = blobmsg_get_u32(c);
730
731 if ((c = tb[IFACE_ATTR_RA_USELEASETIME]))
732 iface->ra_useleasetime = blobmsg_get_bool(c);
733
734 if ((c = tb[IFACE_ATTR_RA_DNS]))
735 iface->ra_dns = blobmsg_get_bool(c);
736
737 if ((c = tb[IFACE_ATTR_RA_PREFERENCE])) {
738 const char *prio = blobmsg_get_string(c);
739
740 if (!strcmp(prio, "high"))
741 iface->route_preference = 1;
742 else if (!strcmp(prio, "low"))
743 iface->route_preference = -1;
744 else if (!strcmp(prio, "medium") || !strcmp(prio, "default"))
745 iface->route_preference = 0;
746 else
747 goto err;
748 }
749
750 if ((c = tb[IFACE_ATTR_PD_MANAGER]))
751 strncpy(iface->dhcpv6_pd_manager, blobmsg_get_string(c),
752 sizeof(iface->dhcpv6_pd_manager) - 1);
753
754 if ((c = tb[IFACE_ATTR_PD_CER]) &&
755 inet_pton(AF_INET6, blobmsg_get_string(c), &iface->dhcpv6_pd_cer) < 1)
756 goto err;
757
758 if ((c = tb[IFACE_ATTR_NDPROXY_ROUTING]))
759 iface->learn_routes = blobmsg_get_bool(c);
760
761 if ((c = tb[IFACE_ATTR_NDPROXY_SLAVE]))
762 iface->external = blobmsg_get_bool(c);
763
764 if ((c = tb[IFACE_ATTR_PREFIX_FILTER])) {
765 const char *str = blobmsg_get_string(c);
766 char *astr = malloc(strlen(str) + 1);
767 char *delim;
768 int l;
769
770 if (!astr || !strcpy(astr, str) ||
771 (delim = strchr(astr, '/')) == NULL || (*(delim++) = 0) ||
772 sscanf(delim, "%i", &l) == 0 || l > 128 ||
773 inet_pton(AF_INET6, astr, &iface->pio_filter_addr) == 0)
774 iface->pio_filter_length = 0;
775 else
776 iface->pio_filter_length = l;
777
778 if (astr)
779 free(astr);
780 }
781
782 return 0;
783
784 err:
785 close_interface(iface);
786 return -1;
787 }
788
789 static int set_interface(struct uci_section *s)
790 {
791 blob_buf_init(&b, 0);
792 uci_to_blob(&b, s, &interface_attr_list);
793
794 return config_parse_interface(blob_data(b.head), blob_len(b.head), s->e.name, true);
795 }
796
797 static void lease_delete_assignments(struct lease *l, bool v6)
798 {
799 struct dhcp_assignment *a, *tmp;
800 unsigned int flag = v6 ? OAF_DHCPV6 : OAF_DHCPV4;
801
802 list_for_each_entry_safe(a, tmp, &l->assignments, lease_list) {
803 if (a->flags & flag)
804 free_assignment(a);
805 }
806 }
807
808 static void lease_update_assignments(struct lease *l)
809 {
810 struct dhcp_assignment *a;
811
812 list_for_each_entry(a, &l->assignments, lease_list) {
813 if (a->hostname)
814 free(a->hostname);
815 a->hostname = NULL;
816
817 if (l->hostname)
818 a->hostname = strdup(l->hostname);
819
820 a->leasetime = l->leasetime;
821 }
822 }
823
824 static int lease_cmp(const void *k1, const void *k2, _unused void *ptr)
825 {
826 const struct lease *l1 = k1, *l2 = k2;
827 int cmp = 0;
828
829 if (l1->duid_len != l2->duid_len)
830 return l1->duid_len - l2->duid_len;
831
832 if (l1->duid_len && l2->duid_len)
833 cmp = memcmp(l1->duid, l2->duid, l1->duid_len);
834
835 if (cmp)
836 return cmp;
837
838 return memcmp(l1->mac.ether_addr_octet, l2->mac.ether_addr_octet,
839 sizeof(l1->mac.ether_addr_octet));
840 }
841
842 static void lease_change_config(struct lease *l_old, struct lease *l_new)
843 {
844 bool update = false;
845
846 if ((!!l_new->hostname != !!l_old->hostname) ||
847 (l_new->hostname && strcmp(l_new->hostname, l_old->hostname))) {
848 free(l_old->hostname);
849 l_old->hostname = NULL;
850
851 if (l_new->hostname)
852 l_old->hostname = strdup(l_new->hostname);
853
854 update = true;
855 }
856
857 if (l_old->leasetime != l_new->leasetime) {
858 l_old->leasetime = l_new->leasetime;
859 update = true;
860 }
861
862 if (l_old->ipaddr != l_new->ipaddr) {
863 l_old->ipaddr = l_new->ipaddr;
864 lease_delete_assignments(l_old, false);
865 }
866
867 if (l_old->hostid != l_new->hostid) {
868 l_old->hostid = l_new->hostid;
869 lease_delete_assignments(l_old, true);
870 }
871
872 if (update)
873 lease_update_assignments(l_old);
874
875 free_lease(l_new);
876 }
877
878 static void lease_delete(struct lease *l)
879 {
880 struct dhcp_assignment *a;
881
882 list_for_each_entry(a, &l->assignments, lease_list)
883 free_assignment(a);
884
885 free_lease(l);
886 }
887
888 static void lease_update(_unused struct vlist_tree *tree, struct vlist_node *node_new,
889 struct vlist_node *node_old)
890 {
891 struct lease *lease_new = container_of(node_new, struct lease, node);
892 struct lease *lease_old = container_of(node_old, struct lease, node);
893
894 if (node_old && node_new)
895 lease_change_config(lease_old, lease_new);
896 else if (node_old)
897 lease_delete(lease_old);
898 }
899
900 struct lease *config_find_lease_by_duid(const uint8_t *duid, const uint16_t len)
901 {
902 struct lease *l;
903
904 vlist_for_each_element(&leases, l, node) {
905 if (l->duid_len == len && !memcmp(l->duid, duid, len))
906 return l;
907 }
908
909 return NULL;
910 }
911
912 struct lease *config_find_lease_by_mac(const uint8_t *mac)
913 {
914 struct lease *l;
915
916 vlist_for_each_element(&leases, l, node) {
917 if (!memcmp(l->mac.ether_addr_octet, mac,
918 sizeof(l->mac.ether_addr_octet)))
919 return l;
920 }
921
922 return NULL;
923 }
924
925 struct lease *config_find_lease_by_hostid(const uint32_t hostid)
926 {
927 struct lease *l;
928
929 vlist_for_each_element(&leases, l, node) {
930 if (l->hostid == hostid)
931 return l;
932 }
933
934 return NULL;
935 }
936
937 struct lease *config_find_lease_by_ipaddr(const uint32_t ipaddr)
938 {
939 struct lease *l;
940
941 vlist_for_each_element(&leases, l, node) {
942 if (l->ipaddr == ipaddr)
943 return l;
944 }
945
946 return NULL;
947 }
948
949 void odhcpd_reload(void)
950 {
951 struct uci_context *uci = uci_alloc_context();
952 struct interface *master = NULL, *i, *tmp;
953
954 if (!uci)
955 return;
956
957 vlist_update(&leases);
958 avl_for_each_element(&interfaces, i, avl)
959 clean_interface(i);
960
961 struct uci_package *dhcp = NULL;
962 if (!uci_load(uci, "dhcp", &dhcp)) {
963 struct uci_element *e;
964 uci_foreach_element(&dhcp->sections, e) {
965 struct uci_section *s = uci_to_section(e);
966 if (!strcmp(s->type, "host"))
967 set_lease(s);
968 else if (!strcmp(s->type, "odhcpd"))
969 set_config(s);
970 }
971
972 uci_foreach_element(&dhcp->sections, e) {
973 struct uci_section *s = uci_to_section(e);
974 if (!strcmp(s->type, "dhcp"))
975 set_interface(s);
976 }
977 }
978
979 if (config.dhcp_statefile) {
980 char *path = strdup(config.dhcp_statefile);
981
982 mkdir_p(dirname(path), 0755);
983 free(path);
984 }
985
986 vlist_flush(&leases);
987
988 #ifdef WITH_UBUS
989 ubus_apply_network();
990 #endif
991
992 bool any_dhcpv6_slave = false, any_ra_slave = false, any_ndp_slave = false;
993
994 /* Test for */
995 avl_for_each_element(&interfaces, i, avl) {
996 if (i->master)
997 continue;
998
999 if (i->dhcpv6 == MODE_HYBRID || i->dhcpv6 == MODE_RELAY)
1000 any_dhcpv6_slave = true;
1001
1002 if (i->ra == MODE_HYBRID || i->ra == MODE_RELAY)
1003 any_ra_slave = true;
1004
1005 if (i->ndp == MODE_HYBRID || i->ndp == MODE_RELAY)
1006 any_ndp_slave = true;
1007 }
1008
1009 /* Evaluate hybrid mode for master */
1010 avl_for_each_element(&interfaces, i, avl) {
1011 if (!i->master)
1012 continue;
1013
1014 enum odhcpd_mode hybrid_mode = MODE_DISABLED;
1015 #ifdef WITH_UBUS
1016 if (!ubus_has_prefix(i->name, i->ifname))
1017 hybrid_mode = MODE_RELAY;
1018 #endif
1019
1020 if (i->dhcpv6 == MODE_HYBRID)
1021 i->dhcpv6 = hybrid_mode;
1022
1023 if (i->dhcpv6 == MODE_RELAY && !any_dhcpv6_slave)
1024 i->dhcpv6 = MODE_DISABLED;
1025
1026 if (i->ra == MODE_HYBRID)
1027 i->ra = hybrid_mode;
1028
1029 if (i->ra == MODE_RELAY && !any_ra_slave)
1030 i->ra = MODE_DISABLED;
1031
1032 if (i->ndp == MODE_HYBRID)
1033 i->ndp = hybrid_mode;
1034
1035 if (i->ndp == MODE_RELAY && !any_ndp_slave)
1036 i->ndp = MODE_DISABLED;
1037
1038 if (i->dhcpv6 == MODE_RELAY || i->ra == MODE_RELAY || i->ndp == MODE_RELAY)
1039 master = i;
1040 }
1041
1042
1043 avl_for_each_element_safe(&interfaces, i, avl, tmp) {
1044 if (i->inuse) {
1045 /* Resolve hybrid mode */
1046 if (i->dhcpv6 == MODE_HYBRID)
1047 i->dhcpv6 = (master && master->dhcpv6 == MODE_RELAY) ?
1048 MODE_RELAY : MODE_SERVER;
1049
1050 if (i->ra == MODE_HYBRID)
1051 i->ra = (master && master->ra == MODE_RELAY) ?
1052 MODE_RELAY : MODE_SERVER;
1053
1054 if (i->ndp == MODE_HYBRID)
1055 i->ndp = (master && master->ndp == MODE_RELAY) ?
1056 MODE_RELAY : MODE_DISABLED;
1057
1058 router_setup_interface(i, !i->ignore || i->ra != MODE_DISABLED);
1059 dhcpv6_setup_interface(i, !i->ignore || i->dhcpv6 != MODE_DISABLED);
1060 ndp_setup_interface(i, !i->ignore || i->ndp != MODE_DISABLED);
1061 #ifdef DHCPV4_SUPPORT
1062 dhcpv4_setup_interface(i, !i->ignore || i->dhcpv4 != MODE_DISABLED);
1063 #endif
1064 } else
1065 close_interface(i);
1066 }
1067
1068 uci_unload(uci, dhcp);
1069 uci_free_context(uci);
1070 }
1071
1072 static void handle_signal(int signal)
1073 {
1074 char b[1] = {0};
1075
1076 if (signal == SIGHUP) {
1077 if (write(reload_pipe[1], b, sizeof(b)) < 0) {}
1078 } else
1079 uloop_end();
1080 }
1081
1082 static void reload_cb(struct uloop_fd *u, _unused unsigned int events)
1083 {
1084 char b[512];
1085 if (read(u->fd, b, sizeof(b)) < 0) {}
1086
1087 odhcpd_reload();
1088 }
1089
1090 static struct uloop_fd reload_fd = { .fd = -1, .cb = reload_cb };
1091
1092 void odhcpd_run(void)
1093 {
1094 if (pipe2(reload_pipe, O_NONBLOCK | O_CLOEXEC) < 0) {}
1095
1096 reload_fd.fd = reload_pipe[0];
1097 uloop_fd_add(&reload_fd, ULOOP_READ);
1098
1099 signal(SIGTERM, handle_signal);
1100 signal(SIGINT, handle_signal);
1101 signal(SIGHUP, handle_signal);
1102
1103 #ifdef WITH_UBUS
1104 while (ubus_init())
1105 sleep(1);
1106 #endif
1107
1108 odhcpd_reload();
1109 uloop_run();
1110 }