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