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