odhcp6c: fix possible stack corruption when parsing proc if_inet6
[project/odhcp6c.git] / src / odhcp6c.c
1 /**
2 * Copyright (C) 2012-2014 Steven Barth <steven@midlink.org>
3 * Copyright (C) 2017 Hans Dedecker <dedeckeh@gmail.com>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License v2 as published by
7 * the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 */
15
16 #include <time.h>
17 #include <errno.h>
18 #include <ctype.h>
19 #include <fcntl.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <stddef.h>
23 #include <unistd.h>
24 #include <syslog.h>
25 #include <signal.h>
26 #include <string.h>
27 #include <strings.h>
28 #include <stdbool.h>
29
30 #include <net/if.h>
31 #include <sys/syscall.h>
32 #include <arpa/inet.h>
33 #include <linux/if_addr.h>
34
35 #include "odhcp6c.h"
36 #include "ra.h"
37
38
39 #ifndef IN6_IS_ADDR_UNIQUELOCAL
40 #define IN6_IS_ADDR_UNIQUELOCAL(a) \
41 ((((__const uint32_t *) (a))[0] & htonl (0xfe000000)) \
42 == htonl (0xfc000000))
43 #endif
44
45 static void sighandler(int signal);
46 static int usage(void);
47
48 static uint8_t *state_data[_STATE_MAX] = {NULL};
49 static size_t state_len[_STATE_MAX] = {0};
50
51 static volatile bool signal_io = false;
52 static volatile bool signal_usr1 = false;
53 static volatile bool signal_usr2 = false;
54 static volatile bool signal_term = false;
55
56 static int urandom_fd = -1, allow_slaac_only = 0;
57 static bool bound = false, release = true, ra = false;
58 static time_t last_update = 0;
59 static char *ifname = NULL;
60
61 static unsigned int min_update_interval = DEFAULT_MIN_UPDATE_INTERVAL;
62 static unsigned int script_sync_delay = 10;
63 static unsigned int script_accu_delay = 1;
64
65 int main(_unused int argc, char* const argv[])
66 {
67 // Allocate ressources
68 const char *pidfile = NULL;
69 const char *script = "/usr/sbin/odhcp6c-update";
70 ssize_t l;
71 uint8_t buf[134];
72 char *optpos;
73 uint16_t opttype;
74 uint16_t optlen;
75 enum odhcp6c_ia_mode ia_na_mode = IA_MODE_TRY;
76 enum odhcp6c_ia_mode ia_pd_mode = IA_MODE_NONE;
77 int ia_pd_iaid_index = 0;
78 static struct in6_addr ifid = IN6ADDR_ANY_INIT;
79 int sol_timeout = DHCPV6_SOL_MAX_RT;
80 int verbosity = 0;
81
82
83 bool help = false, daemonize = false;
84 int logopt = LOG_PID;
85 int c;
86 unsigned int client_options = DHCPV6_CLIENT_FQDN | DHCPV6_ACCEPT_RECONFIGURE;
87 unsigned int ra_options = RA_RDNSS_DEFAULT_LIFETIME;
88
89 while ((c = getopt(argc, argv, "S::N:V:P:FB:c:i:r:Ru:s:kt:m:Lhedp:fav")) != -1) {
90 switch (c) {
91 case 'S':
92 allow_slaac_only = (optarg) ? atoi(optarg) : -1;
93 break;
94
95 case 'N':
96 if (!strcmp(optarg, "force")) {
97 ia_na_mode = IA_MODE_FORCE;
98 allow_slaac_only = -1;
99 } else if (!strcmp(optarg, "none")) {
100 ia_na_mode = IA_MODE_NONE;
101 } else if (!strcmp(optarg, "try")) {
102 ia_na_mode = IA_MODE_TRY;
103 } else{
104 help = true;
105 }
106 break;
107
108 case 'V':
109 l = script_unhexlify(buf, sizeof(buf), optarg);
110 if (!l)
111 help=true;
112
113 odhcp6c_add_state(STATE_VENDORCLASS, buf, l);
114
115 break;
116 case 'P':
117 if (ia_pd_mode == IA_MODE_NONE)
118 ia_pd_mode = IA_MODE_TRY;
119
120 if (allow_slaac_only >= 0 && allow_slaac_only < 10)
121 allow_slaac_only = 10;
122
123 char *iaid_begin;
124 int iaid_len = 0;
125
126 int prefix_length = strtoul(optarg, &iaid_begin, 10);
127
128 if (*iaid_begin != '\0' && *iaid_begin != ',' && *iaid_begin != ':') {
129 syslog(LOG_ERR, "invalid argument: '%s'", optarg);
130 return 1;
131 }
132
133 struct odhcp6c_request_prefix prefix = { 0, prefix_length };
134
135 if (*iaid_begin == ',' && (iaid_len = strlen(iaid_begin)) > 1)
136 memcpy(&prefix.iaid, iaid_begin + 1, iaid_len > 4 ? 4 : iaid_len);
137 else if (*iaid_begin == ':')
138 prefix.iaid = htonl((uint32_t)strtoul(&iaid_begin[1], NULL, 16));
139 else
140 prefix.iaid = htonl(++ia_pd_iaid_index);
141
142 odhcp6c_add_state(STATE_IA_PD_INIT, &prefix, sizeof(prefix));
143
144 break;
145
146 case 'F':
147 allow_slaac_only = -1;
148 ia_pd_mode = IA_MODE_FORCE;
149 break;
150
151 case 'c':
152 l = script_unhexlify(&buf[4], sizeof(buf) - 4, optarg);
153 if (l > 0) {
154 buf[0] = 0;
155 buf[1] = DHCPV6_OPT_CLIENTID;
156 buf[2] = 0;
157 buf[3] = l;
158 odhcp6c_add_state(STATE_CLIENT_ID, buf, l + 4);
159 } else {
160 help = true;
161 }
162 break;
163
164 case 'i':
165 if (inet_pton(AF_INET6, optarg, &ifid) != 1)
166 help = true;
167 break;
168
169 case 'r':
170 optpos = optarg;
171 while (optpos[0]) {
172 opttype = htons(strtoul(optarg, &optpos, 10));
173 if (optpos == optarg)
174 break;
175 else if (optpos[0])
176 optarg = &optpos[1];
177 odhcp6c_add_state(STATE_ORO, &opttype, 2);
178 }
179 break;
180
181 case 'R':
182 client_options |= DHCPV6_STRICT_OPTIONS;
183 break;
184
185 case 'u':
186 optlen = htons(strlen(optarg));
187 odhcp6c_add_state(STATE_USERCLASS, &optlen, 2);
188 odhcp6c_add_state(STATE_USERCLASS, optarg, strlen(optarg));
189 break;
190
191 case 's':
192 script = optarg;
193 break;
194
195 case 'k':
196 release = false;
197 break;
198
199 case 't':
200 sol_timeout = atoi(optarg);
201 break;
202
203 case 'm':
204 min_update_interval = atoi(optarg);
205 break;
206
207 case 'L':
208 ra_options &= ~RA_RDNSS_DEFAULT_LIFETIME;
209 break;
210
211 case 'e':
212 logopt |= LOG_PERROR;
213 break;
214
215 case 'd':
216 daemonize = true;
217 break;
218
219 case 'p':
220 pidfile = optarg;
221 break;
222
223 case 'f':
224 client_options &= ~DHCPV6_CLIENT_FQDN;
225 break;
226
227 case 'a':
228 client_options &= ~DHCPV6_ACCEPT_RECONFIGURE;
229 break;
230
231 case 'v':
232 ++verbosity;
233 break;
234
235 default:
236 help = true;
237 break;
238 }
239 }
240
241 if (allow_slaac_only > 0)
242 script_sync_delay = allow_slaac_only;
243
244 openlog("odhcp6c", logopt, LOG_DAEMON);
245 if (!verbosity)
246 setlogmask(LOG_UPTO(LOG_WARNING));
247
248 ifname = argv[optind];
249
250 if (help || !ifname)
251 return usage();
252
253 signal(SIGIO, sighandler);
254 signal(SIGHUP, sighandler);
255 signal(SIGINT, sighandler);
256 signal(SIGTERM, sighandler);
257 signal(SIGUSR1, sighandler);
258 signal(SIGUSR2, sighandler);
259
260 if ((urandom_fd = open("/dev/urandom", O_CLOEXEC | O_RDONLY)) < 0 ||
261 init_dhcpv6(ifname, client_options, sol_timeout) ||
262 ra_init(ifname, &ifid, ra_options) ||
263 script_init(script, ifname)) {
264 syslog(LOG_ERR, "failed to initialize: %s", strerror(errno));
265 return 3;
266 }
267
268 if (daemonize) {
269 openlog("odhcp6c", LOG_PID, LOG_DAEMON); // Disable LOG_PERROR
270 if (daemon(0, 0)) {
271 syslog(LOG_ERR, "Failed to daemonize: %s",
272 strerror(errno));
273 return 4;
274 }
275
276 if (!pidfile) {
277 snprintf((char*)buf, sizeof(buf), "/var/run/odhcp6c.%s.pid", ifname);
278 pidfile = (char*)buf;
279 }
280
281 FILE *fp = fopen(pidfile, "w");
282 if (fp) {
283 fprintf(fp, "%i\n", getpid());
284 fclose(fp);
285 }
286 }
287
288 script_call("started", 0, false);
289
290 while (!signal_term) { // Main logic
291 odhcp6c_clear_state(STATE_SERVER_ID);
292 odhcp6c_clear_state(STATE_SERVER_ADDR);
293 odhcp6c_clear_state(STATE_IA_NA);
294 odhcp6c_clear_state(STATE_IA_PD);
295 odhcp6c_clear_state(STATE_SNTP_IP);
296 odhcp6c_clear_state(STATE_NTP_IP);
297 odhcp6c_clear_state(STATE_NTP_FQDN);
298 odhcp6c_clear_state(STATE_SIP_IP);
299 odhcp6c_clear_state(STATE_SIP_FQDN);
300 bound = false;
301
302 syslog(LOG_NOTICE, "(re)starting transaction on %s", ifname);
303
304 signal_usr1 = signal_usr2 = false;
305 int mode = dhcpv6_set_ia_mode(ia_na_mode, ia_pd_mode);
306 if (mode != DHCPV6_STATELESS)
307 mode = dhcpv6_request(DHCPV6_MSG_SOLICIT);
308 odhcp6c_signal_process();
309
310 if (mode < 0)
311 continue;
312
313 do {
314 int res = dhcpv6_request(mode == DHCPV6_STATELESS ?
315 DHCPV6_MSG_INFO_REQ : DHCPV6_MSG_REQUEST);
316 bool signalled = odhcp6c_signal_process();
317
318 if (res > 0)
319 break;
320 else if (signalled) {
321 mode = -1;
322 break;
323 }
324
325 mode = dhcpv6_promote_server_cand();
326 } while (mode > DHCPV6_UNKNOWN);
327
328 if (mode < 0)
329 continue;
330
331 switch (mode) {
332 case DHCPV6_STATELESS:
333 bound = true;
334 syslog(LOG_NOTICE, "entering stateless-mode on %s", ifname);
335
336 while (!signal_usr2 && !signal_term) {
337 signal_usr1 = false;
338 script_call("informed", script_sync_delay, true);
339
340 int res = dhcpv6_poll_reconfigure();
341 odhcp6c_signal_process();
342
343 if (res > 0)
344 continue;
345
346 if (signal_usr1) {
347 signal_usr1 = false; // Acknowledged
348 continue;
349 }
350 if (signal_usr2 || signal_term)
351 break;
352
353 res = dhcpv6_request(DHCPV6_MSG_INFO_REQ);
354 odhcp6c_signal_process();
355 if (signal_usr1)
356 continue;
357 else if (res < 0)
358 break;
359 }
360 break;
361
362 case DHCPV6_STATEFUL:
363 bound = true;
364 script_call("bound", script_sync_delay, true);
365 syslog(LOG_NOTICE, "entering stateful-mode on %s", ifname);
366
367 while (!signal_usr2 && !signal_term) {
368 // Renew Cycle
369 // Wait for T1 to expire or until we get a reconfigure
370 int res = dhcpv6_poll_reconfigure();
371 odhcp6c_signal_process();
372 if (res > 0) {
373 script_call("updated", 0, false);
374 continue;
375 }
376
377 // Handle signal, if necessary
378 if (signal_usr1)
379 signal_usr1 = false; // Acknowledged
380 if (signal_usr2 || signal_term)
381 break; // Other signal type
382
383 // Send renew as T1 expired
384 res = dhcpv6_request(DHCPV6_MSG_RENEW);
385 odhcp6c_signal_process();
386 if (res > 0) { // Renew was succesfull
387 // Publish updates
388 script_call("updated", 0, false);
389 continue; // Renew was successful
390 }
391
392 odhcp6c_clear_state(STATE_SERVER_ID); // Remove binding
393 odhcp6c_clear_state(STATE_SERVER_ADDR);
394
395 size_t ia_pd_len, ia_na_len;
396 odhcp6c_get_state(STATE_IA_PD, &ia_pd_len);
397 odhcp6c_get_state(STATE_IA_NA, &ia_na_len);
398
399 if (ia_pd_len == 0 && ia_na_len == 0)
400 break;
401
402 // If we have IAs, try rebind otherwise restart
403 res = dhcpv6_request(DHCPV6_MSG_REBIND);
404 odhcp6c_signal_process();
405
406 if (res > 0)
407 script_call("rebound", 0, true);
408 else {
409 break;
410 }
411 }
412 break;
413
414 default:
415 break;
416 }
417
418 odhcp6c_expire();
419
420 size_t ia_pd_len, ia_na_len, server_id_len;
421 odhcp6c_get_state(STATE_IA_PD, &ia_pd_len);
422 odhcp6c_get_state(STATE_IA_NA, &ia_na_len);
423 odhcp6c_get_state(STATE_SERVER_ID, &server_id_len);
424
425 // Add all prefixes to lost prefixes
426 bound = false;
427 script_call("unbound", 0, true);
428
429 if (server_id_len > 0 && (ia_pd_len > 0 || ia_na_len > 0) && release)
430 dhcpv6_request(DHCPV6_MSG_RELEASE);
431
432 odhcp6c_clear_state(STATE_IA_NA);
433 odhcp6c_clear_state(STATE_IA_PD);
434 }
435
436 script_call("stopped", 0, true);
437 return 0;
438 }
439
440
441 static int usage(void)
442 {
443 const char buf[] =
444 "Usage: odhcp6c [options] <interface>\n"
445 "\nFeature options:\n"
446 " -S <time> Wait at least <time> sec for a DHCP-server (0)\n"
447 " -N <mode> Mode for requesting addresses [try|force|none]\n"
448 " -P <length> Request IPv6-Prefix (0 = auto)\n"
449 " -F Force IPv6-Prefix\n"
450 " -V <class> Set vendor-class option (base-16 encoded)\n"
451 " -u <user-class> Set user-class option string\n"
452 " -c <clientid> Override client-ID (base-16 encoded 16-bit type + value)\n"
453 " -i <iface-id> Use a custom interface identifier for RA handling\n"
454 " -r <options> Options to be requested (comma-separated)\n"
455 " -R Do not request any options except those specified with -r\n"
456 " -s <script> Status update script (/usr/sbin/odhcp6c-update)\n"
457 " -a Don't send Accept Reconfigure option\n"
458 " -f Don't send Client FQDN option\n"
459 " -k Don't send a RELEASE when stopping\n"
460 " -t <seconds> Maximum timeout for DHCPv6-SOLICIT (120)\n"
461 " -m <seconds> Minimum time between accepting updates (30)\n"
462 " -L Ignore default lifetime for RDNSS records\n"
463 "\nInvocation options:\n"
464 " -p <pidfile> Set pidfile (/var/run/odhcp6c.pid)\n"
465 " -d Daemonize\n"
466 " -e Write logmessages to stderr\n"
467 " -v Increase logging verbosity\n"
468 " -h Show this help\n\n";
469 fputs(buf, stderr);
470 return 1;
471 }
472
473
474 // Don't want to pull-in librt and libpthread just for a monotonic clock...
475 uint64_t odhcp6c_get_milli_time(void)
476 {
477 struct timespec t = {0, 0};
478 syscall(SYS_clock_gettime, CLOCK_MONOTONIC, &t);
479 return ((uint64_t)t.tv_sec) * 1000 + ((uint64_t)t.tv_nsec) / 1000000;
480 }
481
482
483 static uint8_t* odhcp6c_resize_state(enum odhcp6c_state state, ssize_t len)
484 {
485 if (len == 0)
486 return state_data[state] + state_len[state];
487 else if (state_len[state] + len > 1024)
488 return NULL;
489
490 uint8_t *n = realloc(state_data[state], state_len[state] + len);
491 if (n || state_len[state] + len == 0) {
492 state_data[state] = n;
493 n += state_len[state];
494 state_len[state] += len;
495 }
496 return n;
497 }
498
499
500 bool odhcp6c_signal_process(void)
501 {
502 while (signal_io) {
503 signal_io = false;
504
505 bool ra_updated = ra_process();
506
507 if (ra_link_up()) {
508 signal_usr2 = true;
509 ra = false;
510 }
511
512 if (ra_updated && (bound || allow_slaac_only >= 0)) {
513 script_call("ra-updated", (!ra && !bound) ?
514 script_sync_delay : script_accu_delay, false);
515 ra = true;
516 }
517 }
518
519 return signal_usr1 || signal_usr2 || signal_term;
520 }
521
522
523 void odhcp6c_clear_state(enum odhcp6c_state state)
524 {
525 state_len[state] = 0;
526 }
527
528
529 void odhcp6c_add_state(enum odhcp6c_state state, const void *data, size_t len)
530 {
531 uint8_t *n = odhcp6c_resize_state(state, len);
532 if (n)
533 memcpy(n, data, len);
534 }
535
536 int odhcp6c_insert_state(enum odhcp6c_state state, size_t offset, const void *data, size_t len)
537 {
538 ssize_t len_after = state_len[state] - offset;
539 if (len_after < 0)
540 return -1;
541
542 uint8_t *n = odhcp6c_resize_state(state, len);
543 if (n) {
544 uint8_t *sdata = state_data[state];
545
546 memmove(sdata + offset + len, sdata + offset, len_after);
547 memcpy(sdata + offset, data, len);
548 }
549
550 return 0;
551 }
552
553 size_t odhcp6c_remove_state(enum odhcp6c_state state, size_t offset, size_t len)
554 {
555 uint8_t *data = state_data[state];
556 ssize_t len_after = state_len[state] - (offset + len);
557 if (len_after < 0)
558 return state_len[state];
559
560 memmove(data + offset, data + offset + len, len_after);
561 return state_len[state] -= len;
562 }
563
564
565 void* odhcp6c_move_state(enum odhcp6c_state state, size_t *len)
566 {
567 *len = state_len[state];
568 void *data = state_data[state];
569
570 state_len[state] = 0;
571 state_data[state] = NULL;
572
573 return data;
574 }
575
576
577 void* odhcp6c_get_state(enum odhcp6c_state state, size_t *len)
578 {
579 *len = state_len[state];
580 return state_data[state];
581 }
582
583
584 static struct odhcp6c_entry* odhcp6c_find_entry(enum odhcp6c_state state, const struct odhcp6c_entry *new)
585 {
586 size_t len, cmplen = offsetof(struct odhcp6c_entry, target) + ((new->length + 7) / 8);
587 uint8_t *start = odhcp6c_get_state(state, &len);
588
589 for (struct odhcp6c_entry *c = (struct odhcp6c_entry*)start;
590 (uint8_t*)c < &start[len] &&
591 (uint8_t*)odhcp6c_next_entry(c) <= &start[len];
592 c = odhcp6c_next_entry(c))
593 if (!memcmp(c, new, cmplen) && !memcmp(c->auxtarget, new->auxtarget, new->auxlen))
594 return c;
595
596 return NULL;
597 }
598
599
600 bool odhcp6c_update_entry(enum odhcp6c_state state, struct odhcp6c_entry *new,
601 uint32_t safe, bool filterexcess)
602 {
603 size_t len;
604 struct odhcp6c_entry *x = odhcp6c_find_entry(state, new);
605 uint8_t *start = odhcp6c_get_state(state, &len);
606
607 if (x && x->valid > new->valid && new->valid < safe)
608 new->valid = safe;
609
610 if (new->valid > 0) {
611 if (x) {
612 if (filterexcess && new->valid >= x->valid &&
613 new->valid != UINT32_MAX &&
614 new->valid - x->valid < min_update_interval &&
615 new->preferred >= x->preferred &&
616 new->preferred != UINT32_MAX &&
617 new->preferred - x->preferred < min_update_interval)
618 return false;
619 x->valid = new->valid;
620 x->preferred = new->preferred;
621 x->t1 = new->t1;
622 x->t2 = new->t2;
623 x->iaid = new->iaid;
624 } else {
625 odhcp6c_add_state(state, new, odhcp6c_entry_size(new));
626 }
627 } else if (x) {
628 odhcp6c_remove_state(state, ((uint8_t*)x) - start, odhcp6c_entry_size(x));
629 }
630 return true;
631 }
632
633
634 static void odhcp6c_expire_list(enum odhcp6c_state state, uint32_t elapsed)
635 {
636 size_t len;
637 uint8_t *start = odhcp6c_get_state(state, &len);
638 for (struct odhcp6c_entry *c = (struct odhcp6c_entry*)start;
639 (uint8_t*)c < &start[len] &&
640 (uint8_t*)odhcp6c_next_entry(c) <= &start[len];
641 ) {
642 if (c->t1 < elapsed)
643 c->t1 = 0;
644 else if (c->t1 != UINT32_MAX)
645 c->t1 -= elapsed;
646
647 if (c->t2 < elapsed)
648 c->t2 = 0;
649 else if (c->t2 != UINT32_MAX)
650 c->t2 -= elapsed;
651
652 if (c->preferred < elapsed)
653 c->preferred = 0;
654 else if (c->preferred != UINT32_MAX)
655 c->preferred -= elapsed;
656
657 if (c->valid < elapsed)
658 c->valid = 0;
659 else if (c->valid != UINT32_MAX)
660 c->valid -= elapsed;
661
662 if (!c->valid) {
663 odhcp6c_remove_state(state, ((uint8_t*)c) - start, odhcp6c_entry_size(c));
664 start = odhcp6c_get_state(state, &len);
665 } else {
666 c = odhcp6c_next_entry(c);
667 }
668 }
669 }
670
671
672 void odhcp6c_expire(void)
673 {
674 time_t now = odhcp6c_get_milli_time() / 1000;
675 uint32_t elapsed = (last_update > 0) ? now - last_update : 0;
676 last_update = now;
677
678 odhcp6c_expire_list(STATE_RA_PREFIX, elapsed);
679 odhcp6c_expire_list(STATE_RA_ROUTE, elapsed);
680 odhcp6c_expire_list(STATE_RA_DNS, elapsed);
681 odhcp6c_expire_list(STATE_RA_SEARCH, elapsed);
682 odhcp6c_expire_list(STATE_IA_NA, elapsed);
683 odhcp6c_expire_list(STATE_IA_PD, elapsed);
684 }
685
686
687 uint32_t odhcp6c_elapsed(void)
688 {
689 return odhcp6c_get_milli_time() / 1000 - last_update;
690 }
691
692
693 int odhcp6c_random(void *buf, size_t len)
694 {
695 return read(urandom_fd, buf, len);
696 }
697
698 bool odhcp6c_is_bound(void)
699 {
700 return bound;
701 }
702
703 bool odhcp6c_addr_in_scope(const struct in6_addr *addr)
704 {
705 FILE *fd = fopen("/proc/net/if_inet6", "r");
706 int len;
707 char buf[256];
708
709 if (fd == NULL)
710 return false;
711
712 while (fgets(buf, sizeof(buf), fd)) {
713 struct in6_addr inet6_addr;
714 uint32_t flags, dummy;
715 unsigned int i;
716 char name[IF_NAMESIZE], addr_buf[33];
717
718 len = strlen(buf);
719
720 if ((len <= 0) || buf[len - 1] != '\n')
721 return false;
722
723 buf[--len] = '\0';
724
725 if (sscanf(buf, "%s %x %x %x %x %s",
726 addr_buf, &dummy, &dummy, &dummy, &flags, name) != 6)
727 return false;
728
729 if (strcmp(name, ifname) ||
730 (flags & (IFA_F_DADFAILED | IFA_F_TENTATIVE | IFA_F_DEPRECATED)))
731 continue;
732
733 for (i = 0; i < strlen(addr_buf); i++) {
734 if (!isxdigit(addr_buf[i]) || isupper(addr_buf[i]))
735 return false;
736 }
737
738 memset(&inet6_addr, 0, sizeof(inet6_addr));
739 for (i = 0; i < (strlen(addr_buf) / 2); i++) {
740 unsigned char byte;
741 static const char hex[] = "0123456789abcdef";
742 byte = ((index(hex, addr_buf[i * 2]) - hex) << 4) |
743 (index(hex, addr_buf[i * 2 + 1]) - hex);
744 inet6_addr.s6_addr[i] = byte;
745 }
746
747 if ((IN6_IS_ADDR_LINKLOCAL(&inet6_addr) == IN6_IS_ADDR_LINKLOCAL(addr)) &&
748 (IN6_IS_ADDR_UNIQUELOCAL(&inet6_addr) == IN6_IS_ADDR_UNIQUELOCAL(addr)))
749 return true;
750 }
751 return false;
752 }
753
754 static void sighandler(int signal)
755 {
756 if (signal == SIGUSR1)
757 signal_usr1 = true;
758 else if (signal == SIGUSR2)
759 signal_usr2 = true;
760 else if (signal == SIGIO)
761 signal_io = true;
762 else
763 signal_term = true;
764 }