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