5ba4d9ecd4dcbfc9e63d1a6ed534c520bfd201ef
[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, .dhcp_hostsfile = 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 PD_MIN_LEN_MAX (64-2) // must delegate at least 2 bits of prefix
43
44 #define OAF_DHCPV6 (OAF_DHCPV6_NA | OAF_DHCPV6_PD)
45
46 enum {
47 IFACE_ATTR_INTERFACE,
48 IFACE_ATTR_IFNAME,
49 IFACE_ATTR_NETWORKID,
50 IFACE_ATTR_DYNAMICDHCP,
51 IFACE_ATTR_LEASETIME,
52 IFACE_ATTR_LIMIT,
53 IFACE_ATTR_START,
54 IFACE_ATTR_MASTER,
55 IFACE_ATTR_UPSTREAM,
56 IFACE_ATTR_RA,
57 IFACE_ATTR_DHCPV4,
58 IFACE_ATTR_DHCPV6,
59 IFACE_ATTR_NDP,
60 IFACE_ATTR_ROUTER,
61 IFACE_ATTR_DNS,
62 IFACE_ATTR_DNS_SERVICE,
63 IFACE_ATTR_DOMAIN,
64 IFACE_ATTR_FILTER_CLASS,
65 IFACE_ATTR_DHCPV4_FORCERECONF,
66 IFACE_ATTR_DHCPV6_RAW,
67 IFACE_ATTR_DHCPV6_ASSIGNALL,
68 IFACE_ATTR_DHCPV6_PD,
69 IFACE_ATTR_DHCPV6_PD_MIN_LEN,
70 IFACE_ATTR_DHCPV6_NA,
71 IFACE_ATTR_DHCPV6_HOSTID_LEN,
72 IFACE_ATTR_RA_DEFAULT,
73 IFACE_ATTR_RA_MANAGEMENT,
74 IFACE_ATTR_RA_FLAGS,
75 IFACE_ATTR_RA_SLAAC,
76 IFACE_ATTR_RA_OFFLINK,
77 IFACE_ATTR_RA_PREFERENCE,
78 IFACE_ATTR_RA_ADVROUTER,
79 IFACE_ATTR_RA_MININTERVAL,
80 IFACE_ATTR_RA_MAXINTERVAL,
81 IFACE_ATTR_RA_LIFETIME,
82 IFACE_ATTR_RA_USELEASETIME,
83 IFACE_ATTR_RA_REACHABLETIME,
84 IFACE_ATTR_RA_RETRANSTIME,
85 IFACE_ATTR_RA_HOPLIMIT,
86 IFACE_ATTR_RA_MTU,
87 IFACE_ATTR_RA_DNS,
88 IFACE_ATTR_RA_PREF64,
89 IFACE_ATTR_PD_MANAGER,
90 IFACE_ATTR_PD_CER,
91 IFACE_ATTR_NDPROXY_ROUTING,
92 IFACE_ATTR_NDPROXY_SLAVE,
93 IFACE_ATTR_PREFIX_FILTER,
94 IFACE_ATTR_PREFERRED_LIFETIME,
95 IFACE_ATTR_NTP,
96 IFACE_ATTR_MAX
97 };
98
99 static const struct blobmsg_policy iface_attrs[IFACE_ATTR_MAX] = {
100 [IFACE_ATTR_INTERFACE] = { .name = "interface", .type = BLOBMSG_TYPE_STRING },
101 [IFACE_ATTR_IFNAME] = { .name = "ifname", .type = BLOBMSG_TYPE_STRING },
102 [IFACE_ATTR_NETWORKID] = { .name = "networkid", .type = BLOBMSG_TYPE_STRING },
103 [IFACE_ATTR_DYNAMICDHCP] = { .name = "dynamicdhcp", .type = BLOBMSG_TYPE_BOOL },
104 [IFACE_ATTR_LEASETIME] = { .name = "leasetime", .type = BLOBMSG_TYPE_STRING },
105 [IFACE_ATTR_START] = { .name = "start", .type = BLOBMSG_TYPE_INT32 },
106 [IFACE_ATTR_LIMIT] = { .name = "limit", .type = BLOBMSG_TYPE_INT32 },
107 [IFACE_ATTR_MASTER] = { .name = "master", .type = BLOBMSG_TYPE_BOOL },
108 [IFACE_ATTR_UPSTREAM] = { .name = "upstream", .type = BLOBMSG_TYPE_ARRAY },
109 [IFACE_ATTR_RA] = { .name = "ra", .type = BLOBMSG_TYPE_STRING },
110 [IFACE_ATTR_DHCPV4] = { .name = "dhcpv4", .type = BLOBMSG_TYPE_STRING },
111 [IFACE_ATTR_DHCPV6] = { .name = "dhcpv6", .type = BLOBMSG_TYPE_STRING },
112 [IFACE_ATTR_NDP] = { .name = "ndp", .type = BLOBMSG_TYPE_STRING },
113 [IFACE_ATTR_ROUTER] = { .name = "router", .type = BLOBMSG_TYPE_ARRAY },
114 [IFACE_ATTR_DNS] = { .name = "dns", .type = BLOBMSG_TYPE_ARRAY },
115 [IFACE_ATTR_DNS_SERVICE] = { .name = "dns_service", .type = BLOBMSG_TYPE_BOOL },
116 [IFACE_ATTR_DOMAIN] = { .name = "domain", .type = BLOBMSG_TYPE_ARRAY },
117 [IFACE_ATTR_FILTER_CLASS] = { .name = "filter_class", .type = BLOBMSG_TYPE_STRING },
118 [IFACE_ATTR_DHCPV4_FORCERECONF] = { .name = "dhcpv4_forcereconf", .type = BLOBMSG_TYPE_BOOL },
119 [IFACE_ATTR_DHCPV6_RAW] = { .name = "dhcpv6_raw", .type = BLOBMSG_TYPE_STRING },
120 [IFACE_ATTR_DHCPV6_ASSIGNALL] = { .name ="dhcpv6_assignall", .type = BLOBMSG_TYPE_BOOL },
121 [IFACE_ATTR_DHCPV6_PD] = { .name = "dhcpv6_pd", .type = BLOBMSG_TYPE_BOOL },
122 [IFACE_ATTR_DHCPV6_PD_MIN_LEN] = { .name = "dhcpv6_pd_min_len", .type = BLOBMSG_TYPE_INT32 },
123 [IFACE_ATTR_DHCPV6_NA] = { .name = "dhcpv6_na", .type = BLOBMSG_TYPE_BOOL },
124 [IFACE_ATTR_DHCPV6_HOSTID_LEN] = { .name = "dhcpv6_hostidlength", .type = BLOBMSG_TYPE_INT32 },
125 [IFACE_ATTR_PD_MANAGER] = { .name = "pd_manager", .type = BLOBMSG_TYPE_STRING },
126 [IFACE_ATTR_PD_CER] = { .name = "pd_cer", .type = BLOBMSG_TYPE_STRING },
127 [IFACE_ATTR_RA_DEFAULT] = { .name = "ra_default", .type = BLOBMSG_TYPE_INT32 },
128 [IFACE_ATTR_RA_MANAGEMENT] = { .name = "ra_management", .type = BLOBMSG_TYPE_INT32 },
129 [IFACE_ATTR_RA_FLAGS] = { .name = "ra_flags", . type = BLOBMSG_TYPE_ARRAY },
130 [IFACE_ATTR_RA_SLAAC] = { .name = "ra_slaac", .type = BLOBMSG_TYPE_BOOL },
131 [IFACE_ATTR_RA_OFFLINK] = { .name = "ra_offlink", .type = BLOBMSG_TYPE_BOOL },
132 [IFACE_ATTR_RA_PREFERENCE] = { .name = "ra_preference", .type = BLOBMSG_TYPE_STRING },
133 [IFACE_ATTR_RA_ADVROUTER] = { .name = "ra_advrouter", .type = BLOBMSG_TYPE_BOOL },
134 [IFACE_ATTR_RA_MININTERVAL] = { .name = "ra_mininterval", .type = BLOBMSG_TYPE_INT32 },
135 [IFACE_ATTR_RA_MAXINTERVAL] = { .name = "ra_maxinterval", .type = BLOBMSG_TYPE_INT32 },
136 [IFACE_ATTR_RA_LIFETIME] = { .name = "ra_lifetime", .type = BLOBMSG_TYPE_INT32 },
137 [IFACE_ATTR_RA_USELEASETIME] = { .name = "ra_useleasetime", .type = BLOBMSG_TYPE_BOOL },
138 [IFACE_ATTR_RA_REACHABLETIME] = { .name = "ra_reachabletime", .type = BLOBMSG_TYPE_INT32 },
139 [IFACE_ATTR_RA_RETRANSTIME] = { .name = "ra_retranstime", .type = BLOBMSG_TYPE_INT32 },
140 [IFACE_ATTR_RA_HOPLIMIT] = { .name = "ra_hoplimit", .type = BLOBMSG_TYPE_INT32 },
141 [IFACE_ATTR_RA_MTU] = { .name = "ra_mtu", .type = BLOBMSG_TYPE_INT32 },
142 [IFACE_ATTR_RA_DNS] = { .name = "ra_dns", .type = BLOBMSG_TYPE_BOOL },
143 [IFACE_ATTR_RA_PREF64] = { .name = "ra_pref64", .type = BLOBMSG_TYPE_STRING },
144 [IFACE_ATTR_NDPROXY_ROUTING] = { .name = "ndproxy_routing", .type = BLOBMSG_TYPE_BOOL },
145 [IFACE_ATTR_NDPROXY_SLAVE] = { .name = "ndproxy_slave", .type = BLOBMSG_TYPE_BOOL },
146 [IFACE_ATTR_PREFIX_FILTER] = { .name = "prefix_filter", .type = BLOBMSG_TYPE_STRING },
147 [IFACE_ATTR_PREFERRED_LIFETIME] = { .name = "preferred_lifetime", .type = BLOBMSG_TYPE_STRING },
148 [IFACE_ATTR_NTP] = { .name = "ntp", .type = BLOBMSG_TYPE_ARRAY },
149 };
150
151 static const struct uci_blob_param_info iface_attr_info[IFACE_ATTR_MAX] = {
152 [IFACE_ATTR_UPSTREAM] = { .type = BLOBMSG_TYPE_STRING },
153 [IFACE_ATTR_DNS] = { .type = BLOBMSG_TYPE_STRING },
154 [IFACE_ATTR_DOMAIN] = { .type = BLOBMSG_TYPE_STRING },
155 };
156
157 const struct uci_blob_param_list interface_attr_list = {
158 .n_params = IFACE_ATTR_MAX,
159 .params = iface_attrs,
160 .info = iface_attr_info,
161 };
162
163 const struct blobmsg_policy lease_attrs[LEASE_ATTR_MAX] = {
164 [LEASE_ATTR_IP] = { .name = "ip", .type = BLOBMSG_TYPE_STRING },
165 [LEASE_ATTR_MAC] = { .name = "mac", .type = BLOBMSG_TYPE_STRING },
166 [LEASE_ATTR_DUID] = { .name = "duid", .type = BLOBMSG_TYPE_STRING },
167 [LEASE_ATTR_HOSTID] = { .name = "hostid", .type = BLOBMSG_TYPE_STRING },
168 [LEASE_ATTR_LEASETIME] = { .name = "leasetime", .type = BLOBMSG_TYPE_STRING },
169 [LEASE_ATTR_NAME] = { .name = "name", .type = BLOBMSG_TYPE_STRING },
170 };
171
172 const struct uci_blob_param_list lease_attr_list = {
173 .n_params = LEASE_ATTR_MAX,
174 .params = lease_attrs,
175 };
176
177 enum {
178 ODHCPD_ATTR_LEGACY,
179 ODHCPD_ATTR_MAINDHCP,
180 ODHCPD_ATTR_LEASEFILE,
181 ODHCPD_ATTR_LEASETRIGGER,
182 ODHCPD_ATTR_LOGLEVEL,
183 ODHCPD_ATTR_HOSTSFILE,
184 ODHCPD_ATTR_MAX
185 };
186
187 static const struct blobmsg_policy odhcpd_attrs[ODHCPD_ATTR_MAX] = {
188 [ODHCPD_ATTR_LEGACY] = { .name = "legacy", .type = BLOBMSG_TYPE_BOOL },
189 [ODHCPD_ATTR_MAINDHCP] = { .name = "maindhcp", .type = BLOBMSG_TYPE_BOOL },
190 [ODHCPD_ATTR_LEASEFILE] = { .name = "leasefile", .type = BLOBMSG_TYPE_STRING },
191 [ODHCPD_ATTR_LEASETRIGGER] = { .name = "leasetrigger", .type = BLOBMSG_TYPE_STRING },
192 [ODHCPD_ATTR_LOGLEVEL] = { .name = "loglevel", .type = BLOBMSG_TYPE_INT32 },
193 [ODHCPD_ATTR_HOSTSFILE] = { .name = "hostsfile", .type = BLOBMSG_TYPE_STRING },
194 };
195
196 const struct uci_blob_param_list odhcpd_attr_list = {
197 .n_params = ODHCPD_ATTR_MAX,
198 .params = odhcpd_attrs,
199 };
200
201 static const struct { const char *name; uint8_t flag; } ra_flags[] = {
202 { .name = "managed-config", .flag = ND_RA_FLAG_MANAGED },
203 { .name = "other-config", .flag = ND_RA_FLAG_OTHER },
204 { .name = "home-agent", .flag = ND_RA_FLAG_HOME_AGENT },
205 { .name = "none", . flag = 0 },
206 { .name = NULL, },
207 };
208
209 static void set_interface_defaults(struct interface *iface)
210 {
211 iface->ignore = true;
212 iface->dhcpv4 = MODE_DISABLED;
213 iface->dhcpv6 = MODE_DISABLED;
214 iface->ra = MODE_DISABLED;
215 iface->ndp = MODE_DISABLED;
216 iface->learn_routes = 1;
217 iface->dhcp_leasetime = 43200;
218 iface->preferred_lifetime = 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_pd_min_len = 0;
224 iface->dhcpv6_na = true;
225 iface->dhcpv6_hostid_len = HOSTID_LEN_DEFAULT;
226 iface->dns_service = true;
227 iface->ra_flags = ND_RA_FLAG_OTHER;
228 iface->ra_slaac = true;
229 iface->ra_maxinterval = 600;
230 iface->ra_mininterval = iface->ra_maxinterval/3;
231 iface->ra_lifetime = -1;
232 iface->ra_dns = true;
233 }
234
235 static void clean_interface(struct interface *iface)
236 {
237 free(iface->dns);
238 free(iface->search);
239 free(iface->upstream);
240 free(iface->dhcpv4_router);
241 free(iface->dhcpv4_dns);
242 free(iface->dhcpv6_raw);
243 free(iface->filter_class);
244 free(iface->dhcpv4_ntp);
245 free(iface->dhcpv6_ntp);
246 free(iface->dhcpv6_sntp);
247 memset(&iface->ra, 0, sizeof(*iface) - offsetof(struct interface, ra));
248 set_interface_defaults(iface);
249 }
250
251 static void close_interface(struct interface *iface)
252 {
253 avl_delete(&interfaces, &iface->avl);
254
255 router_setup_interface(iface, false);
256 dhcpv6_setup_interface(iface, false);
257 ndp_setup_interface(iface, false);
258 #ifdef DHCPV4_SUPPORT
259 dhcpv4_setup_interface(iface, false);
260 #endif
261
262 clean_interface(iface);
263 free(iface->addr4);
264 free(iface->addr6);
265 free(iface->invalid_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_HOSTSFILE])) {
332 free(config.dhcp_hostsfile);
333 config.dhcp_hostsfile = strdup(blobmsg_get_string(c));
334 }
335
336 if ((c = tb[ODHCPD_ATTR_LEASETRIGGER])) {
337 free(config.dhcp_cb);
338 config.dhcp_cb = strdup(blobmsg_get_string(c));
339 }
340
341 if ((c = tb[ODHCPD_ATTR_LOGLEVEL])) {
342 int log_level = (blobmsg_get_u32(c) & LOG_PRIMASK);
343
344 if (config.log_level != log_level) {
345 config.log_level = log_level;
346 setlogmask(LOG_UPTO(config.log_level));
347 }
348 }
349 }
350
351 static double parse_leasetime(struct blob_attr *c) {
352 char *val = blobmsg_get_string(c), *endptr = NULL;
353 double time = strcmp(val, "infinite") ? strtod(val, &endptr) : UINT32_MAX;
354
355 if (time && endptr && endptr[0]) {
356 if (endptr[0] == 's')
357 time *= 1;
358 else if (endptr[0] == 'm')
359 time *= 60;
360 else if (endptr[0] == 'h')
361 time *= 3600;
362 else if (endptr[0] == 'd')
363 time *= 24 * 3600;
364 else if (endptr[0] == 'w')
365 time *= 7 * 24 * 3600;
366 else
367 goto err;
368 }
369
370 if (time < 60)
371 time = 60;
372
373 return time;
374
375 err:
376 return -1;
377 }
378
379 static void free_lease(struct lease *l)
380 {
381 free(l->hostname);
382 free(l);
383 }
384
385
386 int set_lease_from_blobmsg(struct blob_attr *ba)
387 {
388 struct blob_attr *tb[LEASE_ATTR_MAX], *c;
389 struct lease *l;
390 size_t duidlen = 0;
391 uint8_t *duid;
392
393 blobmsg_parse(lease_attrs, LEASE_ATTR_MAX, tb, blob_data(ba), blob_len(ba));
394
395 if ((c = tb[LEASE_ATTR_DUID]))
396 duidlen = (blobmsg_data_len(c) - 1) / 2;
397
398 l = calloc_a(sizeof(*l), &duid, duidlen);
399 if (!l)
400 goto err;
401
402 if ((c = tb[LEASE_ATTR_MAC]))
403 if (!ether_aton_r(blobmsg_get_string(c), &l->mac))
404 goto err;
405
406 if ((c = tb[LEASE_ATTR_DUID])) {
407 ssize_t len;
408
409 l->duid = duid;
410 len = odhcpd_unhexlify(l->duid, duidlen, blobmsg_get_string(c));
411
412 if (len < 0)
413 goto err;
414
415 l->duid_len = len;
416 }
417
418 if ((c = tb[LEASE_ATTR_NAME])) {
419 l->hostname = strdup(blobmsg_get_string(c));
420 if (!l->hostname || !odhcpd_valid_hostname(l->hostname))
421 goto err;
422 }
423
424 if ((c = tb[LEASE_ATTR_IP]))
425 if (inet_pton(AF_INET, blobmsg_get_string(c), &l->ipaddr) < 0)
426 goto err;
427
428 if ((c = tb[LEASE_ATTR_HOSTID])) {
429 errno = 0;
430 l->hostid = strtoull(blobmsg_get_string(c), NULL, 16);
431 if (errno)
432 goto err;
433 } else {
434 uint32_t i4a = ntohl(l->ipaddr) & 0xff;
435 l->hostid = ((i4a / 100) << 8) | (((i4a % 100) / 10) << 4) | (i4a % 10);
436 }
437
438 if ((c = tb[LEASE_ATTR_LEASETIME])) {
439 double time = parse_leasetime(c);
440 if (time < 0)
441 goto err;
442
443 l->leasetime = time;
444 }
445
446 INIT_LIST_HEAD(&l->assignments);
447 vlist_add(&leases, &l->node, l);
448 return 0;
449
450 err:
451 if (l)
452 free_lease(l);
453
454 return -1;
455 }
456
457 static int set_lease_from_uci(struct uci_section *s)
458 {
459 blob_buf_init(&b, 0);
460 uci_to_blob(&b, s, &lease_attr_list);
461
462 return set_lease_from_blobmsg(b.head);
463 }
464
465 /* Parse NTP Options for DHCPv6 Address */
466 static int parse_ntp_options(uint16_t *dhcpv6_ntp_len, struct in6_addr addr6, uint8_t **dhcpv6_ntp)
467 {
468 uint16_t sub_opt = 0, sub_len = htons(IPV6_ADDR_LEN);
469 uint16_t ntp_len = IPV6_ADDR_LEN + 4;
470 uint8_t *ntp = *dhcpv6_ntp;
471 size_t pos = *dhcpv6_ntp_len;
472
473 ntp = realloc(ntp, pos + ntp_len);
474 if (!ntp)
475 return -1;
476
477 *dhcpv6_ntp = ntp;
478
479 if (IN6_IS_ADDR_MULTICAST(&addr6))
480 sub_opt = htons(NTP_SUBOPTION_MC_ADDR);
481 else
482 sub_opt = htons(NTP_SUBOPTION_SRV_ADDR);
483
484 memcpy(ntp + pos, &sub_opt, sizeof(sub_opt));
485 pos += sizeof(sub_opt);
486 memcpy(ntp + pos, &sub_len, sizeof(sub_len));
487 pos += sizeof(sub_len);
488 memcpy(ntp + pos, &addr6, IPV6_ADDR_LEN);
489
490 *dhcpv6_ntp_len += ntp_len;
491
492 return 0;
493 }
494
495 /* Parse NTP Options for FQDN */
496 static int parse_ntp_fqdn(uint16_t *dhcpv6_ntp_len, char *fqdn, uint8_t **dhcpv6_ntp)
497 {
498 size_t fqdn_len = strlen(fqdn);
499 uint16_t sub_opt = 0, sub_len = 0, ntp_len = 0;
500 uint8_t *ntp = *dhcpv6_ntp;
501 size_t pos = *dhcpv6_ntp_len;
502 uint8_t buf[256] = {0};
503
504 if (fqdn_len > 0 && fqdn[fqdn_len - 1] == '.')
505 fqdn[fqdn_len - 1] = 0;
506
507 int len = dn_comp(fqdn, buf, sizeof(buf), NULL, NULL);
508 if (len <= 0)
509 return -1;
510
511 ntp_len = len + 4;
512
513 ntp = realloc(ntp, pos + ntp_len);
514 if (!ntp)
515 return -1;
516
517 *dhcpv6_ntp = ntp;
518
519 sub_opt = htons(NTP_SUBOPTION_SRV_FQDN);
520 sub_len = htons(len);
521
522 memcpy(ntp + pos, &sub_opt, sizeof(sub_opt));
523 pos += sizeof(sub_opt);
524 memcpy(ntp + pos, &sub_len, sizeof(sub_len));
525 pos += sizeof(sub_len);
526 memcpy(ntp + pos, buf, len);
527
528 *dhcpv6_ntp_len += ntp_len;
529
530 return 0;
531 }
532
533 int config_parse_interface(void *data, size_t len, const char *name, bool overwrite)
534 {
535 struct odhcpd_ipaddr *addrs = NULL;
536 struct interface *iface;
537 struct blob_attr *tb[IFACE_ATTR_MAX], *c;
538 ssize_t addrs_len;
539 bool get_addrs = false;
540 int mode;
541 const char *ifname = NULL;
542
543 blobmsg_parse(iface_attrs, IFACE_ATTR_MAX, tb, data, len);
544
545 if (tb[IFACE_ATTR_INTERFACE])
546 name = blobmsg_get_string(tb[IFACE_ATTR_INTERFACE]);
547
548 if (!name)
549 return -1;
550
551 iface = avl_find_element(&interfaces, name, iface, avl);
552 if (!iface) {
553 char *new_name;
554
555 iface = calloc_a(sizeof(*iface), &new_name, strlen(name) + 1);
556 if (!iface)
557 return -1;
558
559 iface->name = strcpy(new_name, name);
560 iface->avl.key = iface->name;
561 iface->router_event.uloop.fd = -1;
562 iface->dhcpv6_event.uloop.fd = -1;
563 iface->ndp_event.uloop.fd = -1;
564 iface->ndp_ping_fd = -1;
565 iface->dhcpv4_event.uloop.fd = -1;
566 INIT_LIST_HEAD(&iface->ia_assignments);
567 INIT_LIST_HEAD(&iface->dhcpv4_assignments);
568 INIT_LIST_HEAD(&iface->dhcpv4_fr_ips);
569
570 set_interface_defaults(iface);
571
572 avl_insert(&interfaces, &iface->avl);
573 get_addrs = overwrite = true;
574 }
575
576 if (overwrite) {
577 if ((c = tb[IFACE_ATTR_IFNAME]))
578 ifname = blobmsg_get_string(c);
579 else if ((c = tb[IFACE_ATTR_NETWORKID]))
580 ifname = blobmsg_get_string(c);
581 }
582
583 #ifdef WITH_UBUS
584 if (overwrite || !iface->ifname)
585 ifname = ubus_get_ifname(name);
586 #endif
587
588 if (!iface->ifname && !ifname)
589 goto err;
590
591 if (ifname) {
592 free(iface->ifname);
593 iface->ifname = strdup(ifname);
594
595 if (!iface->ifname)
596 goto err;
597
598 if (!iface->ifindex &&
599 (iface->ifindex = if_nametoindex(iface->ifname)) <= 0)
600 goto err;
601
602 if ((iface->ifflags = odhcpd_get_flags(iface)) < 0)
603 goto err;
604 }
605
606 if (get_addrs) {
607 addrs_len = netlink_get_interface_addrs(iface->ifindex,
608 true, &iface->addr6);
609
610 if (addrs_len > 0)
611 iface->addr6_len = addrs_len;
612
613 addrs_len = netlink_get_interface_addrs(iface->ifindex,
614 false, &iface->addr4);
615 if (addrs_len > 0)
616 iface->addr4_len = addrs_len;
617 }
618
619 addrs_len = netlink_get_interface_linklocal(iface->ifindex, &addrs);
620 if (addrs_len > 0) {
621 for (ssize_t i = 0; i < addrs_len; i++) {
622 struct odhcpd_ipaddr *addr = &addrs[i];
623
624 if (!addr->tentative) {
625 iface->have_link_local = true;
626 break;
627 }
628 }
629 }
630
631 iface->inuse = true;
632
633 if ((c = tb[IFACE_ATTR_DYNAMICDHCP]))
634 iface->no_dynamic_dhcp = !blobmsg_get_bool(c);
635
636 if ((c = tb[IFACE_ATTR_LEASETIME])) {
637 double time = parse_leasetime(c);
638
639 if (time >= 0)
640 iface->dhcp_leasetime = time;
641 else
642 syslog(LOG_ERR, "Invalid %s value configured for interface '%s'",
643 iface_attrs[IFACE_ATTR_LEASETIME].name, iface->name);
644
645 }
646
647 if ((c = tb[IFACE_ATTR_PREFERRED_LIFETIME])) {
648 double time = parse_leasetime(c);
649
650 if (time >= 0)
651 iface->preferred_lifetime = time;
652 else
653 syslog(LOG_ERR, "Invalid %s value configured for interface '%s'",
654 iface_attrs[IFACE_ATTR_PREFERRED_LIFETIME].name, iface->name);
655
656 }
657
658 if ((c = tb[IFACE_ATTR_START])) {
659 iface->dhcpv4_start.s_addr = htonl(blobmsg_get_u32(c));
660 iface->dhcpv4_end.s_addr = htonl(ntohl(iface->dhcpv4_start.s_addr) +
661 LIMIT_DEFAULT - 1);
662
663 if (config.main_dhcpv4 && config.legacy)
664 iface->dhcpv4 = MODE_SERVER;
665 }
666
667 if ((c = tb[IFACE_ATTR_LIMIT]))
668 iface->dhcpv4_end.s_addr = htonl(ntohl(iface->dhcpv4_start.s_addr) +
669 blobmsg_get_u32(c) - 1);
670
671 if ((c = tb[IFACE_ATTR_MASTER]))
672 iface->master = blobmsg_get_bool(c);
673
674 if (overwrite && (c = tb[IFACE_ATTR_UPSTREAM])) {
675 struct blob_attr *cur;
676 unsigned rem;
677
678 blobmsg_for_each_attr(cur, c, rem) {
679 if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING || !blobmsg_check_attr(cur, false))
680 continue;
681
682 iface->upstream = realloc(iface->upstream,
683 iface->upstream_len + blobmsg_data_len(cur));
684 if (!iface->upstream)
685 goto err;
686
687 memcpy(iface->upstream + iface->upstream_len, blobmsg_get_string(cur), blobmsg_data_len(cur));
688 iface->upstream_len += blobmsg_data_len(cur);
689 }
690 }
691
692 if ((c = tb[IFACE_ATTR_RA])) {
693 if ((mode = parse_mode(blobmsg_get_string(c))) >= 0) {
694 iface->ra = mode;
695
696 if (iface->ra != MODE_DISABLED)
697 iface->ignore = false;
698 } else
699 syslog(LOG_ERR, "Invalid %s mode configured for interface '%s'",
700 iface_attrs[IFACE_ATTR_RA].name, iface->name);
701 }
702
703 if ((c = tb[IFACE_ATTR_DHCPV4])) {
704 if ((mode = parse_mode(blobmsg_get_string(c))) >= 0) {
705 if (config.main_dhcpv4) {
706 iface->dhcpv4 = mode;
707
708 if (iface->dhcpv4 != MODE_DISABLED)
709 iface->ignore = false;
710 }
711 } else
712 syslog(LOG_ERR, "Invalid %s mode configured for interface %s",
713 iface_attrs[IFACE_ATTR_DHCPV4].name, iface->name);
714 }
715
716 if ((c = tb[IFACE_ATTR_DHCPV6])) {
717 if ((mode = parse_mode(blobmsg_get_string(c))) >= 0) {
718 iface->dhcpv6 = mode;
719
720 if (iface->dhcpv6 != MODE_DISABLED)
721 iface->ignore = false;
722 } else
723 syslog(LOG_ERR, "Invalid %s mode configured for interface '%s'",
724 iface_attrs[IFACE_ATTR_DHCPV6].name, iface->name);
725 }
726
727 if ((c = tb[IFACE_ATTR_NDP])) {
728 if ((mode = parse_mode(blobmsg_get_string(c))) >= 0) {
729 iface->ndp = mode;
730
731 if (iface->ndp != MODE_DISABLED)
732 iface->ignore = false;
733 } else
734 syslog(LOG_ERR, "Invalid %s mode configured for interface '%s'",
735 iface_attrs[IFACE_ATTR_NDP].name, iface->name);
736 }
737
738 if ((c = tb[IFACE_ATTR_ROUTER])) {
739 struct blob_attr *cur;
740 unsigned rem;
741
742 blobmsg_for_each_attr(cur, c, rem) {
743 struct in_addr addr4;
744
745 if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING || !blobmsg_check_attr(cur, false))
746 continue;
747
748 if (inet_pton(AF_INET, blobmsg_get_string(cur), &addr4) == 1) {
749 iface->dhcpv4_router = realloc(iface->dhcpv4_router,
750 (++iface->dhcpv4_router_cnt) * sizeof(*iface->dhcpv4_router));
751 if (!iface->dhcpv4_router)
752 goto err;
753
754 iface->dhcpv4_router[iface->dhcpv4_router_cnt - 1] = addr4;
755 } else
756 syslog(LOG_ERR, "Invalid %s value configured for interface '%s'",
757 iface_attrs[IFACE_ATTR_ROUTER].name, iface->name);
758 }
759 }
760
761 if ((c = tb[IFACE_ATTR_DNS])) {
762 struct blob_attr *cur;
763 unsigned rem;
764
765 iface->always_rewrite_dns = true;
766 blobmsg_for_each_attr(cur, c, rem) {
767 struct in_addr addr4;
768 struct in6_addr addr6;
769
770 if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING || !blobmsg_check_attr(cur, false))
771 continue;
772
773 if (inet_pton(AF_INET, blobmsg_get_string(cur), &addr4) == 1) {
774 if (addr4.s_addr == INADDR_ANY) {
775 syslog(LOG_ERR, "Invalid %s value configured for interface '%s'",
776 iface_attrs[IFACE_ATTR_DNS].name, iface->name);
777
778 continue;
779 }
780
781 iface->dhcpv4_dns = realloc(iface->dhcpv4_dns,
782 (++iface->dhcpv4_dns_cnt) * sizeof(*iface->dhcpv4_dns));
783 if (!iface->dhcpv4_dns)
784 goto err;
785
786 iface->dhcpv4_dns[iface->dhcpv4_dns_cnt - 1] = addr4;
787 } else if (inet_pton(AF_INET6, blobmsg_get_string(cur), &addr6) == 1) {
788 if (IN6_IS_ADDR_UNSPECIFIED(&addr6)) {
789 syslog(LOG_ERR, "Invalid %s value configured for interface '%s'",
790 iface_attrs[IFACE_ATTR_DNS].name, iface->name);
791
792 continue;
793 }
794
795 iface->dns = realloc(iface->dns,
796 (++iface->dns_cnt) * sizeof(*iface->dns));
797 if (!iface->dns)
798 goto err;
799
800 iface->dns[iface->dns_cnt - 1] = addr6;
801 } else
802 syslog(LOG_ERR, "Invalid %s value configured for interface '%s'",
803 iface_attrs[IFACE_ATTR_DNS].name, iface->name);
804 }
805 }
806
807 if ((c = tb[IFACE_ATTR_DNS_SERVICE]))
808 iface->dns_service = blobmsg_get_bool(c);
809
810 if ((c = tb[IFACE_ATTR_DOMAIN])) {
811 struct blob_attr *cur;
812 unsigned rem;
813
814 blobmsg_for_each_attr(cur, c, rem) {
815 uint8_t buf[256];
816 char *domain = blobmsg_get_string(cur);
817 size_t domainlen = strlen(domain);
818 int len;
819
820 if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING || !blobmsg_check_attr(cur, false))
821 continue;
822
823 domain = blobmsg_get_string(cur);
824 domainlen = strlen(domain);
825
826 if (domainlen > 0 && domain[domainlen - 1] == '.')
827 domain[domainlen - 1] = 0;
828
829 len = dn_comp(domain, buf, sizeof(buf), NULL, NULL);
830 if (len <= 0) {
831 syslog(LOG_ERR, "Invalid %s value configured for interface '%s'",
832 iface_attrs[IFACE_ATTR_DOMAIN].name, iface->name);
833
834 continue;
835 }
836
837 iface->search = realloc(iface->search, iface->search_len + len);
838 if (!iface->search)
839 goto err;
840
841 memcpy(&iface->search[iface->search_len], buf, len);
842 iface->search_len += len;
843 }
844 }
845
846 if ((c = tb[IFACE_ATTR_FILTER_CLASS])) {
847 iface->filter_class = realloc(iface->filter_class, blobmsg_data_len(c) + 1);
848 memcpy(iface->filter_class, blobmsg_get_string(c), blobmsg_data_len(c) + 1);
849 }
850
851 if ((c = tb[IFACE_ATTR_DHCPV4_FORCERECONF]))
852 iface->dhcpv4_forcereconf = blobmsg_get_bool(c);
853
854 if ((c = tb[IFACE_ATTR_DHCPV6_RAW])) {
855 iface->dhcpv6_raw_len = blobmsg_data_len(c) / 2;
856 iface->dhcpv6_raw = realloc(iface->dhcpv6_raw, iface->dhcpv6_raw_len);
857 odhcpd_unhexlify(iface->dhcpv6_raw, iface->dhcpv6_raw_len, blobmsg_get_string(c));
858 }
859
860 if ((c = tb[IFACE_ATTR_DHCPV6_ASSIGNALL]))
861 iface->dhcpv6_assignall = blobmsg_get_bool(c);
862
863 if ((c = tb[IFACE_ATTR_DHCPV6_PD]))
864 iface->dhcpv6_pd = blobmsg_get_bool(c);
865
866 if ((c = tb[IFACE_ATTR_DHCPV6_PD_MIN_LEN])) {
867 uint32_t pd_min_len = blobmsg_get_u32(c);
868 if (pd_min_len != 0 && pd_min_len <= PD_MIN_LEN_MAX)
869 iface->dhcpv6_pd_min_len = pd_min_len;
870 else
871 syslog(LOG_ERR, "Invalid %s value configured for interface '%s'",
872 iface_attrs[IFACE_ATTR_DHCPV6_PD_MIN_LEN].name, iface->name);
873 }
874
875 if ((c = tb[IFACE_ATTR_DHCPV6_NA]))
876 iface->dhcpv6_na = blobmsg_get_bool(c);
877
878 if ((c = tb[IFACE_ATTR_DHCPV6_HOSTID_LEN])) {
879 uint32_t hostid_len = blobmsg_get_u32(c);
880
881 if (hostid_len >= HOSTID_LEN_MIN && hostid_len <= HOSTID_LEN_MAX)
882 iface->dhcpv6_hostid_len = hostid_len;
883 else
884 syslog(LOG_ERR, "Invalid %s value configured for interface '%s'",
885 iface_attrs[IFACE_ATTR_DHCPV6_HOSTID_LEN].name, iface->name);
886
887 }
888
889 if ((c = tb[IFACE_ATTR_RA_DEFAULT]))
890 iface->default_router = blobmsg_get_u32(c);
891
892 if (!tb[IFACE_ATTR_RA_FLAGS] && !tb[IFACE_ATTR_RA_SLAAC] &&
893 (c = tb[IFACE_ATTR_RA_MANAGEMENT])) {
894 switch (blobmsg_get_u32(c)) {
895 case 0:
896 iface->ra_flags = ND_RA_FLAG_OTHER;
897 iface->ra_slaac = true;
898 break;
899 case 1:
900 iface->ra_flags = ND_RA_FLAG_OTHER|ND_RA_FLAG_MANAGED;
901 iface->ra_slaac = true;
902 break;
903 case 2:
904 iface->ra_flags = ND_RA_FLAG_OTHER|ND_RA_FLAG_MANAGED;
905 iface->ra_slaac = false;
906 break;
907 default:
908 break;
909 }
910 }
911
912 if ((c = tb[IFACE_ATTR_RA_FLAGS])) {
913 iface->ra_flags = 0;
914
915 if (parse_ra_flags(&iface->ra_flags, c) < 0)
916 syslog(LOG_ERR, "Invalid %s value configured for interface '%s'",
917 iface_attrs[IFACE_ATTR_RA_FLAGS].name, iface->name);
918 }
919
920 if ((c = tb[IFACE_ATTR_RA_REACHABLETIME])) {
921 uint32_t ra_reachabletime = blobmsg_get_u32(c);
922
923 if (ra_reachabletime <= 3600000)
924 iface->ra_reachabletime = ra_reachabletime;
925 else
926 syslog(LOG_ERR, "Invalid %s value configured for interface '%s'",
927 iface_attrs[IFACE_ATTR_RA_REACHABLETIME].name, iface->name);
928 }
929
930 if ((c = tb[IFACE_ATTR_RA_RETRANSTIME])) {
931 uint32_t ra_retranstime = blobmsg_get_u32(c);
932
933 if (ra_retranstime <= 60000)
934 iface->ra_retranstime = ra_retranstime;
935 else
936 syslog(LOG_ERR, "Invalid %s value configured for interface '%s'",
937 iface_attrs[IFACE_ATTR_RA_RETRANSTIME].name, iface->name);
938 }
939
940 if ((c = tb[IFACE_ATTR_RA_HOPLIMIT])) {
941 uint32_t ra_hoplimit = blobmsg_get_u32(c);
942
943 if (ra_hoplimit <= 255)
944 iface->ra_hoplimit = ra_hoplimit;
945 else
946 syslog(LOG_ERR, "Invalid %s value configured for interface '%s'",
947 iface_attrs[IFACE_ATTR_RA_HOPLIMIT].name, iface->name);
948 }
949
950 if ((c = tb[IFACE_ATTR_RA_MTU])) {
951 uint32_t ra_mtu = blobmsg_get_u32(c);
952
953 if (ra_mtu >= 1280 || ra_mtu <= 65535)
954 iface->ra_mtu = ra_mtu;
955 else
956 syslog(LOG_ERR, "Invalid %s value configured for interface '%s'",
957 iface_attrs[IFACE_ATTR_RA_MTU].name, iface->name);
958 }
959
960 if ((c = tb[IFACE_ATTR_RA_SLAAC]))
961 iface->ra_slaac = blobmsg_get_bool(c);
962
963 if ((c = tb[IFACE_ATTR_RA_OFFLINK]))
964 iface->ra_not_onlink = blobmsg_get_bool(c);
965
966 if ((c = tb[IFACE_ATTR_RA_ADVROUTER]))
967 iface->ra_advrouter = blobmsg_get_bool(c);
968
969 if ((c = tb[IFACE_ATTR_RA_MININTERVAL]))
970 iface->ra_mininterval = blobmsg_get_u32(c);
971
972 if ((c = tb[IFACE_ATTR_RA_MAXINTERVAL]))
973 iface->ra_maxinterval = blobmsg_get_u32(c);
974
975 if ((c = tb[IFACE_ATTR_RA_LIFETIME]))
976 iface->ra_lifetime = blobmsg_get_u32(c);
977
978 if ((c = tb[IFACE_ATTR_RA_USELEASETIME]))
979 iface->ra_useleasetime = blobmsg_get_bool(c);
980
981 if ((c = tb[IFACE_ATTR_RA_DNS]))
982 iface->ra_dns = blobmsg_get_bool(c);
983
984 if ((c = tb[IFACE_ATTR_RA_PREF64])) {
985 const char *str = blobmsg_get_string(c);
986 char *astr = malloc(strlen(str) + 1);
987 char *delim;
988 int l;
989
990 if (!astr || !strcpy(astr, str) ||
991 (delim = strchr(astr, '/')) == NULL || (*(delim++) = 0) ||
992 sscanf(delim, "%i", &l) == 0 || l > 128 ||
993 inet_pton(AF_INET6, astr, &iface->pref64_addr) == 0)
994 iface->pref64_length = 0;
995 else
996 iface->pref64_length = l;
997
998 if (astr)
999 free(astr);
1000 }
1001
1002 if ((c = tb[IFACE_ATTR_RA_PREFERENCE])) {
1003 const char *prio = blobmsg_get_string(c);
1004
1005 if (!strcmp(prio, "high"))
1006 iface->route_preference = 1;
1007 else if (!strcmp(prio, "low"))
1008 iface->route_preference = -1;
1009 else if (!strcmp(prio, "medium") || !strcmp(prio, "default"))
1010 iface->route_preference = 0;
1011 else
1012 syslog(LOG_ERR, "Invalid %s mode configured for interface '%s'",
1013 iface_attrs[IFACE_ATTR_RA_PREFERENCE].name, iface->name);
1014 }
1015
1016 if ((c = tb[IFACE_ATTR_PD_MANAGER]))
1017 strncpy(iface->dhcpv6_pd_manager, blobmsg_get_string(c),
1018 sizeof(iface->dhcpv6_pd_manager) - 1);
1019
1020 if ((c = tb[IFACE_ATTR_PD_CER]) &&
1021 inet_pton(AF_INET6, blobmsg_get_string(c), &iface->dhcpv6_pd_cer) < 1)
1022 syslog(LOG_ERR, "Invalid %s value configured for interface '%s'",
1023 iface_attrs[IFACE_ATTR_PD_CER].name, iface->name);
1024
1025 if ((c = tb[IFACE_ATTR_NDPROXY_ROUTING]))
1026 iface->learn_routes = blobmsg_get_bool(c);
1027
1028 if ((c = tb[IFACE_ATTR_NDPROXY_SLAVE]))
1029 iface->external = blobmsg_get_bool(c);
1030
1031 if ((c = tb[IFACE_ATTR_PREFIX_FILTER])) {
1032 const char *str = blobmsg_get_string(c);
1033 char *astr = malloc(strlen(str) + 1);
1034 char *delim;
1035 int l;
1036
1037 if (!astr || !strcpy(astr, str) ||
1038 (delim = strchr(astr, '/')) == NULL || (*(delim++) = 0) ||
1039 sscanf(delim, "%i", &l) == 0 || l > 128 ||
1040 inet_pton(AF_INET6, astr, &iface->pio_filter_addr) == 0)
1041 iface->pio_filter_length = 0;
1042 else
1043 iface->pio_filter_length = l;
1044
1045 if (astr)
1046 free(astr);
1047 }
1048
1049 if (overwrite && (c = tb[IFACE_ATTR_NTP])) {
1050 struct blob_attr *cur;
1051 unsigned rem;
1052
1053 blobmsg_for_each_attr(cur, c, rem) {
1054 if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING || !blobmsg_check_attr(cur, false))
1055 continue;
1056
1057 char *str = blobmsg_get_string(cur);
1058 struct in_addr addr4;
1059 struct in6_addr addr6;
1060
1061 if (inet_pton(AF_INET, str, &addr4) == 1) {
1062 if (addr4.s_addr == INADDR_ANY)
1063 goto err;
1064
1065 iface->dhcpv4_ntp = realloc(iface->dhcpv4_ntp,
1066 (++iface->dhcpv4_ntp_cnt) * sizeof(*iface->dhcpv4_ntp));
1067 if (!iface->dhcpv4_ntp)
1068 goto err;
1069
1070 iface->dhcpv4_ntp[iface->dhcpv4_ntp_cnt - 1] = addr4;
1071 } else if (inet_pton(AF_INET6, str, &addr6) == 1) {
1072 if (IN6_IS_ADDR_UNSPECIFIED(&addr6))
1073 goto err;
1074
1075 iface->dhcpv6_sntp = realloc(iface->dhcpv6_sntp,
1076 (++iface->dhcpv6_sntp_cnt) * sizeof(*iface->dhcpv6_sntp));
1077 if (!iface->dhcpv6_sntp)
1078 goto err;
1079
1080 iface->dhcpv6_sntp[iface->dhcpv6_sntp_cnt - 1] = addr6;
1081
1082 if (!parse_ntp_options(&iface->dhcpv6_ntp_len, addr6, &iface->dhcpv6_ntp))
1083 iface->dhcpv6_ntp_cnt++;
1084 } else {
1085 if (!parse_ntp_fqdn(&iface->dhcpv6_ntp_len, str, &iface->dhcpv6_ntp))
1086 iface->dhcpv6_ntp_cnt++;
1087 }
1088 }
1089 }
1090
1091 return 0;
1092
1093 err:
1094 close_interface(iface);
1095 return -1;
1096 }
1097
1098 static int set_interface(struct uci_section *s)
1099 {
1100 blob_buf_init(&b, 0);
1101 uci_to_blob(&b, s, &interface_attr_list);
1102
1103 return config_parse_interface(blob_data(b.head), blob_len(b.head), s->e.name, true);
1104 }
1105
1106 static void lease_delete_assignments(struct lease *l, bool v6)
1107 {
1108 struct dhcp_assignment *a, *tmp;
1109 unsigned int flag = v6 ? OAF_DHCPV6 : OAF_DHCPV4;
1110
1111 list_for_each_entry_safe(a, tmp, &l->assignments, lease_list) {
1112 if (a->flags & flag)
1113 free_assignment(a);
1114 }
1115 }
1116
1117 static void lease_update_assignments(struct lease *l)
1118 {
1119 struct dhcp_assignment *a;
1120
1121 list_for_each_entry(a, &l->assignments, lease_list) {
1122 if (a->hostname)
1123 free(a->hostname);
1124 a->hostname = NULL;
1125
1126 if (l->hostname)
1127 a->hostname = strdup(l->hostname);
1128
1129 a->leasetime = l->leasetime;
1130 }
1131 }
1132
1133 static int lease_cmp(const void *k1, const void *k2, _unused void *ptr)
1134 {
1135 const struct lease *l1 = k1, *l2 = k2;
1136 int cmp = 0;
1137
1138 if (l1->duid_len != l2->duid_len)
1139 return l1->duid_len - l2->duid_len;
1140
1141 if (l1->duid_len && l2->duid_len)
1142 cmp = memcmp(l1->duid, l2->duid, l1->duid_len);
1143
1144 if (cmp)
1145 return cmp;
1146
1147 return memcmp(l1->mac.ether_addr_octet, l2->mac.ether_addr_octet,
1148 sizeof(l1->mac.ether_addr_octet));
1149 }
1150
1151 static void lease_change_config(struct lease *l_old, struct lease *l_new)
1152 {
1153 bool update = false;
1154
1155 if ((!!l_new->hostname != !!l_old->hostname) ||
1156 (l_new->hostname && strcmp(l_new->hostname, l_old->hostname))) {
1157 free(l_old->hostname);
1158 l_old->hostname = NULL;
1159
1160 if (l_new->hostname)
1161 l_old->hostname = strdup(l_new->hostname);
1162
1163 update = true;
1164 }
1165
1166 if (l_old->leasetime != l_new->leasetime) {
1167 l_old->leasetime = l_new->leasetime;
1168 update = true;
1169 }
1170
1171 if (l_old->ipaddr != l_new->ipaddr) {
1172 l_old->ipaddr = l_new->ipaddr;
1173 lease_delete_assignments(l_old, false);
1174 }
1175
1176 if (l_old->hostid != l_new->hostid) {
1177 l_old->hostid = l_new->hostid;
1178 lease_delete_assignments(l_old, true);
1179 }
1180
1181 if (update)
1182 lease_update_assignments(l_old);
1183
1184 free_lease(l_new);
1185 }
1186
1187 static void lease_delete(struct lease *l)
1188 {
1189 struct dhcp_assignment *a, *tmp;
1190
1191 list_for_each_entry_safe(a, tmp, &l->assignments, lease_list)
1192 free_assignment(a);
1193
1194 free_lease(l);
1195 }
1196
1197 static void lease_update(_unused struct vlist_tree *tree, struct vlist_node *node_new,
1198 struct vlist_node *node_old)
1199 {
1200 struct lease *lease_new = container_of(node_new, struct lease, node);
1201 struct lease *lease_old = container_of(node_old, struct lease, node);
1202
1203 if (node_old && node_new)
1204 lease_change_config(lease_old, lease_new);
1205 else if (node_old)
1206 lease_delete(lease_old);
1207 }
1208
1209 struct lease *config_find_lease_by_duid(const uint8_t *duid, const uint16_t len)
1210 {
1211 struct lease *l;
1212
1213 vlist_for_each_element(&leases, l, node) {
1214 if (l->duid_len == len && !memcmp(l->duid, duid, len))
1215 return l;
1216 }
1217
1218 return NULL;
1219 }
1220
1221 struct lease *config_find_lease_by_mac(const uint8_t *mac)
1222 {
1223 struct lease *l;
1224
1225 vlist_for_each_element(&leases, l, node) {
1226 if (!memcmp(l->mac.ether_addr_octet, mac,
1227 sizeof(l->mac.ether_addr_octet)))
1228 return l;
1229 }
1230
1231 return NULL;
1232 }
1233
1234 struct lease *config_find_lease_by_hostid(const uint64_t hostid)
1235 {
1236 struct lease *l;
1237
1238 vlist_for_each_element(&leases, l, node) {
1239 if (l->hostid == hostid)
1240 return l;
1241 }
1242
1243 return NULL;
1244 }
1245
1246 struct lease *config_find_lease_by_ipaddr(const uint32_t ipaddr)
1247 {
1248 struct lease *l;
1249
1250 vlist_for_each_element(&leases, l, node) {
1251 if (l->ipaddr == ipaddr)
1252 return l;
1253 }
1254
1255 return NULL;
1256 }
1257
1258 void reload_services(struct interface *iface)
1259 {
1260 if (iface->ifflags & IFF_RUNNING) {
1261 syslog(LOG_DEBUG, "Enabling services with %s running", iface->ifname);
1262 router_setup_interface(iface, iface->ra != MODE_DISABLED);
1263 dhcpv6_setup_interface(iface, iface->dhcpv6 != MODE_DISABLED);
1264 ndp_setup_interface(iface, iface->ndp != MODE_DISABLED);
1265 #ifdef DHCPV4_SUPPORT
1266 dhcpv4_setup_interface(iface, iface->dhcpv4 != MODE_DISABLED);
1267 #endif
1268 } else {
1269 syslog(LOG_DEBUG, "Disabling services with %s not running", iface->ifname);
1270 router_setup_interface(iface, false);
1271 dhcpv6_setup_interface(iface, false);
1272 ndp_setup_interface(iface, false);
1273 #ifdef DHCPV4_SUPPORT
1274 dhcpv4_setup_interface(iface, false);
1275 #endif
1276 }
1277 }
1278
1279 void odhcpd_reload(void)
1280 {
1281 struct uci_context *uci = uci_alloc_context();
1282 struct interface *master = NULL, *i, *tmp;
1283
1284 if (!uci)
1285 return;
1286
1287 vlist_update(&leases);
1288 avl_for_each_element(&interfaces, i, avl)
1289 clean_interface(i);
1290
1291 struct uci_package *dhcp = NULL;
1292 if (!uci_load(uci, "dhcp", &dhcp)) {
1293 struct uci_element *e;
1294
1295 /* 1. Global settings */
1296 uci_foreach_element(&dhcp->sections, e) {
1297 struct uci_section *s = uci_to_section(e);
1298 if (!strcmp(s->type, "odhcpd"))
1299 set_config(s);
1300 }
1301
1302 /* 2. DHCP pools */
1303 uci_foreach_element(&dhcp->sections, e) {
1304 struct uci_section *s = uci_to_section(e);
1305 if (!strcmp(s->type, "dhcp"))
1306 set_interface(s);
1307 }
1308
1309 /* 3. Static leases */
1310 uci_foreach_element(&dhcp->sections, e) {
1311 struct uci_section* s = uci_to_section(e);
1312 if (!strcmp(s->type, "host"))
1313 set_lease_from_uci(s);
1314 }
1315 }
1316
1317 if (config.dhcp_statefile) {
1318 char *path = strdup(config.dhcp_statefile);
1319
1320 mkdir_p(dirname(path), 0755);
1321 free(path);
1322 }
1323
1324 vlist_flush(&leases);
1325
1326 #ifdef WITH_UBUS
1327 ubus_apply_network();
1328 #endif
1329
1330 bool any_dhcpv6_slave = false, any_ra_slave = false, any_ndp_slave = false;
1331
1332 /* Test for */
1333 avl_for_each_element(&interfaces, i, avl) {
1334 if (i->master)
1335 continue;
1336
1337 if (i->dhcpv6 == MODE_HYBRID || i->dhcpv6 == MODE_RELAY)
1338 any_dhcpv6_slave = true;
1339
1340 if (i->ra == MODE_HYBRID || i->ra == MODE_RELAY)
1341 any_ra_slave = true;
1342
1343 if (i->ndp == MODE_HYBRID || i->ndp == MODE_RELAY)
1344 any_ndp_slave = true;
1345 }
1346
1347 /* Evaluate hybrid mode for master */
1348 avl_for_each_element(&interfaces, i, avl) {
1349 if (!i->master)
1350 continue;
1351
1352 enum odhcpd_mode hybrid_mode = MODE_DISABLED;
1353 #ifdef WITH_UBUS
1354 if (!ubus_has_prefix(i->name, i->ifname))
1355 hybrid_mode = MODE_RELAY;
1356 #endif
1357
1358 if (i->dhcpv6 == MODE_HYBRID)
1359 i->dhcpv6 = hybrid_mode;
1360
1361 if (i->dhcpv6 == MODE_RELAY && !any_dhcpv6_slave)
1362 i->dhcpv6 = MODE_DISABLED;
1363
1364 if (i->ra == MODE_HYBRID)
1365 i->ra = hybrid_mode;
1366
1367 if (i->ra == MODE_RELAY && !any_ra_slave)
1368 i->ra = MODE_DISABLED;
1369
1370 if (i->ndp == MODE_HYBRID)
1371 i->ndp = hybrid_mode;
1372
1373 if (i->ndp == MODE_RELAY && !any_ndp_slave)
1374 i->ndp = MODE_DISABLED;
1375
1376 if (i->dhcpv6 == MODE_RELAY || i->ra == MODE_RELAY || i->ndp == MODE_RELAY)
1377 master = i;
1378 }
1379
1380
1381 avl_for_each_element_safe(&interfaces, i, avl, tmp) {
1382 if (i->inuse && i->ifflags & IFF_RUNNING) {
1383 /* Resolve hybrid mode */
1384 if (i->dhcpv6 == MODE_HYBRID)
1385 i->dhcpv6 = (master && master->dhcpv6 == MODE_RELAY) ?
1386 MODE_RELAY : MODE_SERVER;
1387
1388 if (i->ra == MODE_HYBRID)
1389 i->ra = (master && master->ra == MODE_RELAY) ?
1390 MODE_RELAY : MODE_SERVER;
1391
1392 if (i->ndp == MODE_HYBRID)
1393 i->ndp = (master && master->ndp == MODE_RELAY) ?
1394 MODE_RELAY : MODE_DISABLED;
1395
1396 reload_services(i);
1397 } else
1398 close_interface(i);
1399 }
1400
1401 uci_unload(uci, dhcp);
1402 uci_free_context(uci);
1403 }
1404
1405 static void handle_signal(int signal)
1406 {
1407 char b[1] = {0};
1408
1409 if (signal == SIGHUP) {
1410 if (write(reload_pipe[1], b, sizeof(b)) < 0) {}
1411 } else
1412 uloop_end();
1413 }
1414
1415 static void reload_cb(struct uloop_fd *u, _unused unsigned int events)
1416 {
1417 char b[512];
1418 if (read(u->fd, b, sizeof(b)) < 0) {}
1419
1420 odhcpd_reload();
1421 }
1422
1423 static struct uloop_fd reload_fd = { .fd = -1, .cb = reload_cb };
1424
1425 void odhcpd_run(void)
1426 {
1427 if (pipe2(reload_pipe, O_NONBLOCK | O_CLOEXEC) < 0) {}
1428
1429 reload_fd.fd = reload_pipe[0];
1430 uloop_fd_add(&reload_fd, ULOOP_READ);
1431
1432 signal(SIGTERM, handle_signal);
1433 signal(SIGINT, handle_signal);
1434 signal(SIGHUP, handle_signal);
1435
1436 #ifdef WITH_UBUS
1437 while (ubus_init())
1438 sleep(1);
1439 #endif
1440
1441 odhcpd_reload();
1442 uloop_run();
1443 }