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