Fix upstream detection
[project/odhcpd.git] / src / config.c
1 #include <resolv.h>
2 #include <signal.h>
3 #include <arpa/inet.h>
4
5 #include <uci.h>
6 #include <uci_blob.h>
7
8 #include "odhcpd.h"
9
10 static struct blob_buf b;
11 struct list_head leases = LIST_HEAD_INIT(leases);
12 struct list_head interfaces = LIST_HEAD_INIT(interfaces);
13 struct config config = {false, NULL, NULL};
14
15 enum {
16 IFACE_ATTR_INTERFACE,
17 IFACE_ATTR_IFNAME,
18 IFACE_ATTR_DYNAMICDHCP,
19 IFACE_ATTR_IGNORE,
20 IFACE_ATTR_LEASETIME,
21 IFACE_ATTR_LIMIT,
22 IFACE_ATTR_START,
23 IFACE_ATTR_MASTER,
24 IFACE_ATTR_UPSTREAM,
25 IFACE_ATTR_RA,
26 IFACE_ATTR_DHCPV4,
27 IFACE_ATTR_DHCPV6,
28 IFACE_ATTR_NDPROXY,
29 IFACE_ATTR_DNS,
30 IFACE_ATTR_DOMAIN,
31 IFACE_ATTR_ULA_COMPAT,
32 IFACE_ATTR_RA_DEFAULT,
33 IFACE_ATTR_RA_MANAGEMENT,
34 IFACE_ATTR_RA_OFFLINK,
35 IFACE_ATTR_RA_PREFERENCE,
36 IFACE_ATTR_NDPROXY_ROUTING,
37 IFACE_ATTR_NDPROXY_SLAVE,
38 IFACE_ATTR_NDPROXY_STATIC,
39 IFACE_ATTR_MAX
40 };
41
42 static const struct blobmsg_policy iface_attrs[IFACE_ATTR_MAX] = {
43 [IFACE_ATTR_INTERFACE] = { .name = "interface", .type = BLOBMSG_TYPE_STRING },
44 [IFACE_ATTR_IFNAME] = { .name = "ifname", .type = BLOBMSG_TYPE_STRING },
45 [IFACE_ATTR_DYNAMICDHCP] = { .name = "dynamicdhcp", .type = BLOBMSG_TYPE_BOOL },
46 [IFACE_ATTR_IGNORE] = { .name = "ignore", .type = BLOBMSG_TYPE_BOOL },
47 [IFACE_ATTR_LEASETIME] = { .name = "leasetime", .type = BLOBMSG_TYPE_STRING },
48 [IFACE_ATTR_START] = { .name = "start", .type = BLOBMSG_TYPE_INT32 },
49 [IFACE_ATTR_LIMIT] = { .name = "limit", .type = BLOBMSG_TYPE_INT32 },
50 [IFACE_ATTR_MASTER] = { .name = "master", .type = BLOBMSG_TYPE_BOOL },
51 [IFACE_ATTR_UPSTREAM] = { .name = "upstream", .type = BLOBMSG_TYPE_ARRAY },
52 [IFACE_ATTR_RA] = { .name = "ra", .type = BLOBMSG_TYPE_STRING },
53 [IFACE_ATTR_DHCPV4] = { .name = "dhcpv4", .type = BLOBMSG_TYPE_STRING },
54 [IFACE_ATTR_DHCPV6] = { .name = "dhcpv6", .type = BLOBMSG_TYPE_STRING },
55 [IFACE_ATTR_NDPROXY] = { .name = "ndproxy", .type = BLOBMSG_TYPE_BOOL },
56 [IFACE_ATTR_DNS] = { .name = "dns", .type = BLOBMSG_TYPE_ARRAY },
57 [IFACE_ATTR_DOMAIN] = { .name = "domain", .type = BLOBMSG_TYPE_ARRAY },
58 [IFACE_ATTR_ULA_COMPAT] = { .name = "ula_compat", .type = BLOBMSG_TYPE_BOOL },
59 [IFACE_ATTR_RA_DEFAULT] = { .name = "ra_default", .type = BLOBMSG_TYPE_INT32 },
60 [IFACE_ATTR_RA_MANAGEMENT] = { .name = "ra_management", .type = BLOBMSG_TYPE_INT32 },
61 [IFACE_ATTR_RA_OFFLINK] = { .name = "ra_offlink", .type = BLOBMSG_TYPE_BOOL },
62 [IFACE_ATTR_RA_PREFERENCE] = { .name = "ra_preference", .type = BLOBMSG_TYPE_STRING },
63 [IFACE_ATTR_NDPROXY_ROUTING] = { .name = "ndproxy_routing", .type = BLOBMSG_TYPE_BOOL },
64 [IFACE_ATTR_NDPROXY_SLAVE] = { .name = "ndproxy_slave", .type = BLOBMSG_TYPE_BOOL },
65 [IFACE_ATTR_NDPROXY_STATIC] = { .name = "ndproxy_static", .type = BLOBMSG_TYPE_ARRAY },
66 };
67
68 static const struct uci_blob_param_info iface_attr_info[IFACE_ATTR_MAX] = {
69 [IFACE_ATTR_UPSTREAM] = { .type = BLOBMSG_TYPE_STRING },
70 [IFACE_ATTR_DNS] = { .type = BLOBMSG_TYPE_STRING },
71 [IFACE_ATTR_DOMAIN] = { .type = BLOBMSG_TYPE_STRING },
72 [IFACE_ATTR_NDPROXY_STATIC] = { .type = BLOBMSG_TYPE_STRING },
73 };
74
75 const struct uci_blob_param_list interface_attr_list = {
76 .n_params = IFACE_ATTR_MAX,
77 .params = iface_attrs,
78 .info = iface_attr_info,
79 };
80
81
82 enum {
83 LEASE_ATTR_IP,
84 LEASE_ATTR_MAC,
85 LEASE_ATTR_DUID,
86 LEASE_ATTR_HOSTID,
87 LEASE_ATTR_HOSTNAME,
88 LEASE_ATTR_MAX
89 };
90
91
92 static const struct blobmsg_policy lease_attrs[LEASE_ATTR_MAX] = {
93 [LEASE_ATTR_IP] = { .name = "ip", .type = BLOBMSG_TYPE_STRING },
94 [LEASE_ATTR_MAC] = { .name = "mac", .type = BLOBMSG_TYPE_STRING },
95 [LEASE_ATTR_DUID] = { .name = "duid", .type = BLOBMSG_TYPE_STRING },
96 [LEASE_ATTR_HOSTID] = { .name = "hostid", .type = BLOBMSG_TYPE_STRING },
97 [LEASE_ATTR_HOSTNAME] = { .name = "hostname", .type = BLOBMSG_TYPE_STRING },
98 };
99
100
101 const struct uci_blob_param_list lease_attr_list = {
102 .n_params = LEASE_ATTR_MAX,
103 .params = lease_attrs,
104 };
105
106
107 enum {
108 ODHCPD_ATTR_LEGACY,
109 ODHCPD_ATTR_LEASEFILE,
110 ODHCPD_ATTR_LEASETRIGGER,
111 ODHCPD_ATTR_MAX
112 };
113
114
115 static const struct blobmsg_policy odhcpd_attrs[LEASE_ATTR_MAX] = {
116 [ODHCPD_ATTR_LEGACY] = { .name = "legacy", .type = BLOBMSG_TYPE_BOOL },
117 [ODHCPD_ATTR_LEASEFILE] = { .name = "leasefile", .type = BLOBMSG_TYPE_STRING },
118 [ODHCPD_ATTR_LEASETRIGGER] = { .name = "leasetrigger", .type = BLOBMSG_TYPE_STRING },
119 };
120
121
122 const struct uci_blob_param_list odhcpd_attr_list = {
123 .n_params = ODHCPD_ATTR_MAX,
124 .params = odhcpd_attrs,
125 };
126
127
128 static struct interface* get_interface(const char *name)
129 {
130 struct interface *c;
131 list_for_each_entry(c, &interfaces, head)
132 if (!strcmp(c->name, name))
133 return c;
134 return NULL;
135 }
136
137
138 static void clean_interface(struct interface *iface)
139 {
140 free(iface->dns);
141 free(iface->search);
142 free(iface->upstream);
143 free(iface->static_ndp);
144 free(iface->dhcpv4_dns);
145 memset(&iface->ra, 0, sizeof(*iface) - offsetof(struct interface, ra));
146 }
147
148
149 static void close_interface(struct interface *iface)
150 {
151 if (iface->head.next)
152 list_del(&iface->head);
153
154 setup_router_interface(iface, false);
155 setup_dhcpv6_interface(iface, false);
156 setup_ndp_interface(iface, false);
157 setup_dhcpv4_interface(iface, false);
158
159 clean_interface(iface);
160 free(iface);
161 }
162
163
164 static int parse_mode(const char *mode)
165 {
166 if (!strcmp(mode, "disabled")) {
167 return RELAYD_DISABLED;
168 } else if (!strcmp(mode, "server")) {
169 return RELAYD_SERVER;
170 } else if (!strcmp(mode, "relay")) {
171 return RELAYD_RELAY;
172 } else if (!strcmp(mode, "hybrid")) {
173 return RELAYD_HYBRID;
174 } else {
175 return -1;
176 }
177 }
178
179
180 static void set_config(struct uci_section *s)
181 {
182 struct blob_attr *tb[ODHCPD_ATTR_MAX], *c;
183
184 blob_buf_init(&b, 0);
185 uci_to_blob(&b, s, &lease_attr_list);
186 blobmsg_parse(lease_attrs, ODHCPD_ATTR_MAX, tb, blob_data(b.head), blob_len(b.head));
187
188 if ((c = tb[ODHCPD_ATTR_LEGACY]))
189 config.legacy = blobmsg_get_bool(c);
190
191 if ((c = tb[ODHCPD_ATTR_LEASEFILE])) {
192 free(config.dhcp_statefile);
193 config.dhcp_statefile = strdup(blobmsg_get_string(c));
194 }
195
196 if ((c = tb[ODHCPD_ATTR_LEASETRIGGER])) {
197 free(config.dhcp_cb);
198 config.dhcp_cb = strdup(blobmsg_get_string(c));
199 }
200 }
201
202
203 static int set_lease(struct uci_section *s)
204 {
205 struct blob_attr *tb[LEASE_ATTR_MAX], *c;
206
207 blob_buf_init(&b, 0);
208 uci_to_blob(&b, s, &lease_attr_list);
209 blobmsg_parse(lease_attrs, LEASE_ATTR_MAX, tb, blob_data(b.head), blob_len(b.head));
210
211 size_t hostlen = 1;
212 if ((c = tb[LEASE_ATTR_HOSTNAME]))
213 hostlen = blobmsg_data_len(c);
214
215 struct lease *lease = calloc(1, sizeof(*lease) + hostlen);
216
217 if (hostlen > 1)
218 memcpy(lease->hostname, blobmsg_get_string(c), hostlen);
219
220 if ((c = tb[LEASE_ATTR_IP]))
221 if (inet_pton(AF_INET, blobmsg_get_string(c), &lease->ipaddr) < 0)
222 goto err;
223
224 if ((c = tb[LEASE_ATTR_MAC]))
225 if (!ether_aton_r(blobmsg_get_string(c), &lease->mac))
226 goto err;
227
228 if ((c = tb[LEASE_ATTR_DUID])) {
229 size_t duidlen = (blobmsg_data_len(c) - 1) / 2;
230 lease->duid = malloc(duidlen);
231 ssize_t len = odhcpd_unhexlify(lease->duid,
232 duidlen, blobmsg_get_string(c));
233
234 if (len < 0)
235 goto err;
236
237 lease->duid_len = len;
238 }
239
240 if ((c = tb[LEASE_ATTR_HOSTID]))
241 if (odhcpd_unhexlify((uint8_t*)&lease->hostid, sizeof(lease->hostid),
242 blobmsg_get_string(c)) < 0)
243 goto err;
244
245 list_add(&lease->head, &leases);
246 return 0;
247
248 err:
249 free(lease->duid);
250 free(lease);
251 return -1;
252 }
253
254
255 int config_parse_interface(struct blob_attr *b, const char *name, bool overwrite)
256 {
257 struct blob_attr *tb[IFACE_ATTR_MAX], *c;
258 blobmsg_parse(iface_attrs, IFACE_ATTR_MAX, tb, blob_data(b), blob_len(b));
259
260 if (tb[IFACE_ATTR_INTERFACE])
261 name = blobmsg_data(tb[IFACE_ATTR_INTERFACE]);
262
263 struct interface *iface = get_interface(name);
264 if (!iface) {
265 iface = calloc(1, sizeof(*iface));
266 strncpy(iface->name, name, sizeof(iface->name) - 1);
267 list_add(&iface->head, &interfaces);
268 } else {
269 clean_interface(iface);
270 }
271
272 const char *ifname = NULL;
273 #ifdef WITH_UBUS
274 if (overwrite)
275 ifname = ubus_get_ifname(name);
276 #endif
277 if ((c = tb[IFACE_ATTR_IFNAME]))
278 ifname = blobmsg_get_string(c);
279
280 strncpy(iface->ifname, ifname, sizeof(iface->ifname) - 1);
281 iface->inuse = true;
282
283 if (overwrite)
284 clean_interface(iface);
285
286 if ((c = tb[IFACE_ATTR_DYNAMICDHCP]))
287 iface->no_dynamic_dhcp = !blobmsg_get_bool(c);
288
289 if ((c = tb[IFACE_ATTR_IGNORE]))
290 iface->ignore = blobmsg_get_bool(c);
291
292 if ((c = tb[IFACE_ATTR_LEASETIME])) {
293 char *val = blobmsg_get_string(c), *endptr;
294 double time = strtod(val, &endptr);
295 if (time && endptr[0]) {
296 if (endptr[0] == 's')
297 time *= 1;
298 else if (endptr[0] == 'm')
299 time *= 60;
300 else if (endptr[0] == 'h')
301 time *= 3600;
302 else if (endptr[0] == 'd')
303 time *= 24 * 3600;
304 else if (endptr[0] == 'w')
305 time *= 7 * 24 * 3600;
306 else
307 goto err;
308 }
309
310 if (time >= 60)
311 iface->dhcpv4_leasetime = time;
312 }
313
314 if ((c = tb[IFACE_ATTR_START])) {
315 iface->dhcpv4_start.s_addr = htonl(blobmsg_get_u32(c));
316
317 if (config.legacy)
318 iface->dhcpv4 = RELAYD_SERVER;
319 }
320
321 if ((c = tb[IFACE_ATTR_LIMIT]))
322 iface->dhcpv4_end.s_addr = htonl(
323 ntohl(iface->dhcpv4_start.s_addr) + blobmsg_get_u32(c));
324
325 if ((c = tb[IFACE_ATTR_MASTER]))
326 iface->master = blobmsg_get_bool(c);
327
328 if ((c = tb[IFACE_ATTR_UPSTREAM])) {
329 struct blob_attr *cur;
330 int rem;
331
332 blobmsg_for_each_attr(cur, c, rem) {
333 if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING || !blobmsg_check_attr(cur, NULL))
334 continue;
335
336 iface->upstream = realloc(iface->upstream,
337 iface->upstream_len + blobmsg_data_len(cur));
338 memcpy(iface->upstream + iface->upstream_len, blobmsg_get_string(cur), blobmsg_data_len(cur));
339 iface->upstream_len += blobmsg_data_len(cur);
340 }
341 }
342
343 if ((c = tb[IFACE_ATTR_RA]))
344 if ((iface->ra = parse_mode(blobmsg_get_string(c))) < 0)
345 goto err;
346
347 if ((c = tb[IFACE_ATTR_DHCPV4]))
348 if ((iface->dhcpv4 = parse_mode(blobmsg_get_string(c))) < 0)
349 goto err;
350
351 if ((c = tb[IFACE_ATTR_DHCPV6]))
352 if ((iface->dhcpv6 = parse_mode(blobmsg_get_string(c))) < 0)
353 goto err;
354
355 if ((c = tb[IFACE_ATTR_NDPROXY]))
356 iface->ndp = blobmsg_get_bool(c) ? RELAYD_RELAY : RELAYD_DISABLED;
357
358 if ((c = tb[IFACE_ATTR_DNS])) {
359 struct blob_attr *cur;
360 int rem;
361
362 blobmsg_for_each_attr(cur, c, rem) {
363 if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING || !blobmsg_check_attr(cur, NULL))
364 continue;
365
366 struct in_addr addr4;
367 struct in6_addr addr6;
368 if (inet_pton(AF_INET, blobmsg_get_string(cur), &addr4) == 1) {
369 iface->dhcpv4_dns = realloc(iface->dhcpv4_dns,
370 (++iface->dhcpv4_dns_cnt) * sizeof(*iface->dhcpv4_dns));
371 iface->dhcpv4_dns[iface->dhcpv4_dns_cnt - 1] = addr4;
372 } else if (inet_pton(AF_INET6, blobmsg_get_string(cur), &addr6) == 1) {
373 iface->dns = realloc(iface->dns,
374 (++iface->dns_cnt) * sizeof(*iface->dns));
375 iface->dns[iface->dns_cnt - 1] = addr6;
376 } else {
377 goto err;
378 }
379 }
380 }
381
382 if ((c = tb[IFACE_ATTR_DOMAIN])) {
383 struct blob_attr *cur;
384 int rem;
385
386 blobmsg_for_each_attr(cur, c, rem) {
387 if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING || !blobmsg_check_attr(cur, NULL))
388 continue;
389
390 uint8_t buf[256];
391 int len = dn_comp(blobmsg_get_string(cur), buf, sizeof(buf), NULL, NULL);
392 if (len <= 0)
393 goto err;
394
395 iface->search = realloc(iface->search, iface->search_len + len);
396 memcpy(&iface->search[iface->search_len], buf, len);
397 iface->search_len += len;
398 }
399 }
400
401 if ((c = tb[IFACE_ATTR_ULA_COMPAT]))
402 iface->deprecate_ula_if_public_avail = blobmsg_get_bool(c);
403
404 if ((c = tb[IFACE_ATTR_RA_DEFAULT]))
405 iface->default_router = blobmsg_get_u32(c);
406
407 if ((c = tb[IFACE_ATTR_RA_MANAGEMENT]))
408 iface->managed = blobmsg_get_u32(c);
409
410 if ((c = tb[IFACE_ATTR_RA_OFFLINK]))
411 iface->ra_not_onlink = blobmsg_get_bool(c);
412
413 if ((c = tb[IFACE_ATTR_RA_PREFERENCE])) {
414 const char *prio = blobmsg_get_string(c);
415
416 if (!strcmp(prio, "high"))
417 iface->route_preference = 1;
418 else if (!strcmp(prio, "low"))
419 iface->route_preference = -1;
420 else if (!strcmp(prio, "medium") || !strcmp(prio, "default"))
421 iface->route_preference = 0;
422 else
423 goto err;
424 }
425
426 if ((c = tb[IFACE_ATTR_NDPROXY_ROUTING]))
427 iface->learn_routes = blobmsg_get_bool(c);
428
429 if ((c = tb[IFACE_ATTR_NDPROXY_SLAVE]))
430 iface->external = blobmsg_get_bool(c);
431
432 if ((c = tb[IFACE_ATTR_NDPROXY_STATIC])) {
433 struct blob_attr *cur;
434 int rem;
435
436 blobmsg_for_each_attr(cur, c, rem) {
437 if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING || !blobmsg_check_attr(cur, NULL))
438 continue;
439
440 int len = blobmsg_data_len(cur);
441 iface->static_ndp = realloc(iface->static_ndp, iface->static_ndp_len + len);
442 memcpy(&iface->static_ndp[iface->static_ndp_len], blobmsg_get_string(cur), len);
443 iface->static_ndp_len += len;
444 }
445 }
446
447 iface->ignore = (iface->ifindex = if_nametoindex(iface->ifname)) < 0;
448 return 0;
449
450 err:
451 close_interface(iface);
452 return -1;
453 }
454
455 static int set_interface(struct uci_section *s)
456 {
457 blob_buf_init(&b, 0);
458 uci_to_blob(&b, s, &interface_attr_list);
459 return config_parse_interface(b.head, s->e.name, true);
460 }
461
462
463 static volatile bool do_reload = false;
464 static void set_stop(int signal)
465 {
466 uloop_end();
467 do_reload = (signal == SIGHUP);
468 }
469
470 void odhcpd_run(void)
471 {
472 struct uci_context *uci = uci_alloc_context();
473 signal(SIGTERM, set_stop);
474 signal(SIGHUP, set_stop);
475 signal(SIGINT, set_stop);
476
477 do {
478 do_reload = false;
479
480 struct lease *l;
481 list_for_each_entry(l, &leases, head) {
482 list_del(&l->head);
483 free(l->duid);
484 free(l);
485 }
486
487 struct uci_package *dhcp = NULL;
488 if (!uci_load(uci, "dhcp", &dhcp)) {
489 struct uci_element *e;
490 uci_foreach_element(&dhcp->sections, e) {
491 struct uci_section *s = uci_to_section(e);
492 if (!strcmp(s->type, "lease"))
493 set_lease(s);
494 else if (!strcmp(s->type, "odhcpd"))
495 set_config(s);
496 }
497
498 uci_foreach_element(&dhcp->sections, e) {
499 struct uci_section *s = uci_to_section(e);
500 if (!strcmp(s->type, "dhcp"))
501 set_interface(s);
502 }
503 }
504
505 #ifdef WITH_UBUS
506 ubus_apply_network();
507 #endif
508
509 // Evaluate hybrid mode for master
510 struct interface *master = NULL, *i;
511 list_for_each_entry(i, &interfaces, head) {
512 if (!i->master)
513 continue;
514
515 enum odhcpd_mode hybrid_mode = RELAYD_DISABLED;
516 if (i->dhcpv6 == RELAYD_HYBRID)
517 i->dhcpv6 = hybrid_mode;
518
519 if (i->ra == RELAYD_HYBRID)
520 i->ra = hybrid_mode;
521
522 if (i->ndp == RELAYD_HYBRID)
523 i->ndp = hybrid_mode;
524
525 if (i->dhcpv6 == RELAYD_RELAY || i->ra == RELAYD_RELAY || i->ndp == RELAYD_RELAY)
526 master = i;
527 }
528
529
530 list_for_each_entry(i, &interfaces, head) {
531 if (i->inuse && !i->ignore) {
532 // Resolve hybrid mode
533 if (i->dhcpv6 == RELAYD_HYBRID)
534 i->dhcpv6 = (master && master->dhcpv6 == RELAYD_RELAY) ?
535 RELAYD_RELAY : RELAYD_SERVER;
536
537 if (i->ra == RELAYD_HYBRID)
538 i->ra = (master && master->ra == RELAYD_RELAY) ?
539 RELAYD_RELAY : RELAYD_SERVER;
540
541 if (i->ndp == RELAYD_HYBRID)
542 i->ndp = (master && master->ndp == RELAYD_RELAY) ?
543 RELAYD_RELAY : RELAYD_SERVER;
544
545 setup_router_interface(i, true);
546 setup_dhcpv6_interface(i, true);
547 setup_ndp_interface(i, true);
548 setup_dhcpv4_interface(i, true);
549 } else {
550 close_interface(i);
551 }
552 }
553
554 uloop_run();
555
556 if (dhcp)
557 uci_unload(uci, dhcp);
558 } while (do_reload);
559 }
560