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