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