bc856037f17c2c2369122b056fde25f2e74f7984
[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)
256 {
257         bool overwrite = !!name;
258         struct blob_attr *tb[IFACE_ATTR_MAX], *c;
259         blobmsg_parse(iface_attrs, IFACE_ATTR_MAX, tb, blob_data(b), blob_len(b));
260
261         if (tb[IFACE_ATTR_INTERFACE])
262                 name = blobmsg_data(tb[IFACE_ATTR_INTERFACE]);
263
264         struct interface *iface = get_interface(name);
265         if (!iface) {
266                 iface = calloc(1, sizeof(*iface));
267                 strncpy(iface->name, name, sizeof(iface->name) - 1);
268                 list_add(&iface->head, &interfaces);
269         } else {
270                 clean_interface(iface);
271         }
272
273         const char *ifname = NULL;
274 #ifdef WITH_UBUS
275         if (overwrite)
276                 ifname = ubus_get_ifname(name);
277 #endif
278         if ((c = tb[IFACE_ATTR_IFNAME]))
279                 ifname = blobmsg_get_string(c);
280
281         strncpy(iface->ifname, ifname, sizeof(iface->ifname) - 1);
282         iface->inuse = true;
283
284         if (overwrite)
285                 clean_interface(iface);
286
287         if ((c = tb[IFACE_ATTR_DYNAMICDHCP]))
288                 iface->no_dynamic_dhcp = !blobmsg_get_bool(c);
289
290         if ((c = tb[IFACE_ATTR_IGNORE]))
291                 iface->ignore = blobmsg_get_bool(c);
292
293         if ((c = tb[IFACE_ATTR_LEASETIME])) {
294                 char *val = blobmsg_get_string(c), *endptr;
295                 double time = strtod(val, &endptr);
296                 if (time && endptr[0]) {
297                         if (endptr[0] == 's')
298                                 time *= 1;
299                         else if (endptr[0] == 'm')
300                                 time *= 60;
301                         else if (endptr[0] == 'h')
302                                 time *= 3600;
303                         else if (endptr[0] == 'd')
304                                 time *= 24 * 3600;
305                         else if (endptr[0] == 'w')
306                                 time *= 7 * 24 * 3600;
307                         else
308                                 goto err;
309                 }
310
311                 if (time >= 60)
312                         iface->dhcpv4_leasetime = time;
313         }
314
315         if ((c = tb[IFACE_ATTR_START])) {
316                 iface->dhcpv4_start.s_addr = htonl(blobmsg_get_u32(c));
317
318                 if (config.legacy)
319                         iface->dhcpv4 = RELAYD_SERVER;
320         }
321
322         if ((c = tb[IFACE_ATTR_LIMIT]))
323                 iface->dhcpv4_end.s_addr = htonl(
324                                 ntohl(iface->dhcpv4_start.s_addr) + blobmsg_get_u32(c));
325
326         if ((c = tb[IFACE_ATTR_MASTER]))
327                 iface->master = blobmsg_get_bool(c);
328
329         if ((c = tb[IFACE_ATTR_UPSTREAM])) {
330                 struct blob_attr *cur;
331                 int rem;
332
333                 blobmsg_for_each_attr(cur, c, rem) {
334                         if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING || !blobmsg_check_attr(cur, NULL))
335                                 continue;
336
337                         iface->upstream = realloc(iface->upstream,
338                                         iface->upstream_len + blobmsg_data_len(cur));
339                         memcpy(iface->upstream + iface->upstream_len, blobmsg_get_string(cur), blobmsg_data_len(cur));
340                         iface->upstream_len += blobmsg_data_len(cur);
341                 }
342         }
343
344         if ((c = tb[IFACE_ATTR_RA]))
345                 if ((iface->ra = parse_mode(blobmsg_get_string(c))) < 0)
346                         goto err;
347
348         if ((c = tb[IFACE_ATTR_DHCPV4]))
349                 if ((iface->dhcpv4 = parse_mode(blobmsg_get_string(c))) < 0)
350                         goto err;
351
352         if ((c = tb[IFACE_ATTR_DHCPV6]))
353                 if ((iface->dhcpv6 = parse_mode(blobmsg_get_string(c))) < 0)
354                         goto err;
355
356         if ((c = tb[IFACE_ATTR_NDPROXY]))
357                 iface->ndp = blobmsg_get_bool(c) ? RELAYD_RELAY : RELAYD_DISABLED;
358
359         if ((c = tb[IFACE_ATTR_DNS])) {
360                 struct blob_attr *cur;
361                 int rem;
362
363                 blobmsg_for_each_attr(cur, c, rem) {
364                         if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING || !blobmsg_check_attr(cur, NULL))
365                                 continue;
366
367                         struct in_addr addr4;
368                         struct in6_addr addr6;
369                         if (inet_pton(AF_INET, blobmsg_get_string(cur), &addr4) == 1) {
370                                 iface->dhcpv4_dns = realloc(iface->dhcpv4_dns,
371                                                 (++iface->dhcpv4_dns_cnt) * sizeof(*iface->dhcpv4_dns));
372                                 iface->dhcpv4_dns[iface->dhcpv4_dns_cnt - 1] = addr4;
373                         } else if (inet_pton(AF_INET6, blobmsg_get_string(cur), &addr6) == 1) {
374                                 iface->dns = realloc(iface->dns,
375                                                 (++iface->dns_cnt) * sizeof(*iface->dns));
376                                 iface->dns[iface->dns_cnt - 1] = addr6;
377                         } else {
378                                 goto err;
379                         }
380                 }
381         }
382
383         if ((c = tb[IFACE_ATTR_DOMAIN])) {
384                 struct blob_attr *cur;
385                 int rem;
386
387                 blobmsg_for_each_attr(cur, c, rem) {
388                         if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING || !blobmsg_check_attr(cur, NULL))
389                                 continue;
390
391                         uint8_t buf[256];
392                         int len = dn_comp(blobmsg_get_string(cur), buf, sizeof(buf), NULL, NULL);
393                         if (len <= 0)
394                                 goto err;
395
396                         iface->search = realloc(iface->search, iface->search_len + len);
397                         memcpy(&iface->search[iface->search_len], buf, len);
398                         iface->search_len += len;
399                 }
400         }
401
402         if ((c = tb[IFACE_ATTR_ULA_COMPAT]))
403                 iface->deprecate_ula_if_public_avail = blobmsg_get_bool(c);
404
405         if ((c = tb[IFACE_ATTR_RA_DEFAULT]))
406                 iface->default_router = blobmsg_get_u32(c);
407
408         if ((c = tb[IFACE_ATTR_RA_MANAGEMENT]))
409                 iface->managed = blobmsg_get_u32(c);
410
411         if ((c = tb[IFACE_ATTR_RA_OFFLINK]))
412                 iface->ra_not_onlink = blobmsg_get_bool(c);
413
414         if ((c = tb[IFACE_ATTR_RA_PREFERENCE])) {
415                 const char *prio = blobmsg_get_string(c);
416
417                 if (!strcmp(prio, "high"))
418                         iface->route_preference = 1;
419                 else if (!strcmp(prio, "low"))
420                         iface->route_preference = -1;
421                 else if (!strcmp(prio, "medium") || !strcmp(prio, "default"))
422                         iface->route_preference = 0;
423                 else
424                         goto err;
425         }
426
427         if ((c = tb[IFACE_ATTR_NDPROXY_ROUTING]))
428                 iface->learn_routes = blobmsg_get_bool(c);
429
430         if ((c = tb[IFACE_ATTR_NDPROXY_SLAVE]))
431                 iface->external = blobmsg_get_bool(c);
432
433         if ((c = tb[IFACE_ATTR_NDPROXY_STATIC])) {
434                 struct blob_attr *cur;
435                 int rem;
436
437                 blobmsg_for_each_attr(cur, c, rem) {
438                         if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING || !blobmsg_check_attr(cur, NULL))
439                                 continue;
440
441                         int len = blobmsg_data_len(cur);
442                         iface->static_ndp = realloc(iface->static_ndp, iface->static_ndp_len + len);
443                         memcpy(&iface->static_ndp[iface->static_ndp_len], blobmsg_get_string(cur), len);
444                         iface->static_ndp_len += len;
445                 }
446         }
447
448         iface->ignore = (iface->ifindex = if_nametoindex(iface->ifname)) < 0;
449         return 0;
450
451 err:
452         close_interface(iface);
453         return -1;
454 }
455
456 static int set_interface(struct uci_section *s)
457 {
458         blob_buf_init(&b, 0);
459         uci_to_blob(&b, s, &interface_attr_list);
460         return config_parse_interface(b.head, s->e.name);
461 }
462
463
464 static volatile bool do_reload = false;
465 static void set_stop(int signal)
466 {
467         uloop_end();
468         do_reload = (signal == SIGHUP);
469 }
470
471 void odhcpd_run(void)
472 {
473         struct uci_context *uci = uci_alloc_context();
474         signal(SIGTERM, set_stop);
475         signal(SIGHUP, set_stop);
476         signal(SIGINT, set_stop);
477
478         do {
479                 do_reload = false;
480
481                 struct lease *l;
482                 list_for_each_entry(l, &leases, head) {
483                         list_del(&l->head);
484                         free(l->duid);
485                         free(l);
486                 }
487
488                 struct uci_package *dhcp = NULL;
489                 if (!uci_load(uci, "dhcp", &dhcp)) {
490                         struct uci_element *e;
491                         uci_foreach_element(&dhcp->sections, e) {
492                                 struct uci_section *s = uci_to_section(e);
493                                 if (!strcmp(s->type, "lease"))
494                                         set_lease(s);
495                                 else if (!strcmp(s->type, "odhcpd"))
496                                         set_config(s);
497                         }
498
499                         uci_foreach_element(&dhcp->sections, e) {
500                                 struct uci_section *s = uci_to_section(e);
501                                 if (!strcmp(s->type, "dhcp"))
502                                         set_interface(s);
503                         }
504                 }
505
506 #ifdef WITH_UBUS
507                 ubus_apply_network();
508 #endif
509
510                 // Evaluate hybrid mode for master
511                 struct interface *master = NULL, *i;
512                 list_for_each_entry(i, &interfaces, head) {
513                         if (!i->master)
514                                 continue;
515
516                         enum odhcpd_mode hybrid_mode = RELAYD_DISABLED;
517                         if (i->dhcpv6 == RELAYD_HYBRID)
518                                 i->dhcpv6 = hybrid_mode;
519
520                         if (i->ra == RELAYD_HYBRID)
521                                 i->ra = hybrid_mode;
522
523                         if (i->ndp == RELAYD_HYBRID)
524                                 i->ndp = hybrid_mode;
525
526                         if (i->dhcpv6 == RELAYD_RELAY || i->ra == RELAYD_RELAY || i->ndp == RELAYD_RELAY)
527                                 master = i;
528                 }
529
530
531                 list_for_each_entry(i, &interfaces, head) {
532                         if (i->inuse && !i->ignore) {
533                                 // Resolve hybrid mode
534                                 if (i->dhcpv6 == RELAYD_HYBRID)
535                                         i->dhcpv6 = (master && master->dhcpv6 == RELAYD_RELAY) ?
536                                                         RELAYD_RELAY : RELAYD_SERVER;
537
538                                 if (i->ra == RELAYD_HYBRID)
539                                         i->ra = (master && master->ra == RELAYD_RELAY) ?
540                                                         RELAYD_RELAY : RELAYD_SERVER;
541
542                                 if (i->ndp == RELAYD_HYBRID)
543                                         i->ndp = (master && master->ndp == RELAYD_RELAY) ?
544                                                         RELAYD_RELAY : RELAYD_SERVER;
545
546                                 setup_router_interface(i, true);
547                                 setup_dhcpv6_interface(i, true);
548                                 setup_ndp_interface(i, true);
549                                 setup_dhcpv4_interface(i, true);
550                         } else {
551                                 close_interface(i);
552                         }
553                 }
554
555                 uloop_run();
556
557                 if (dhcp)
558                         uci_unload(uci, dhcp);
559         } while (do_reload);
560 }
561