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