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