• source navigation  • diff markup  • identifier search  • freetext search  • 

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

This page was automatically generated by LXR 0.3.1.  •  OpenWrt