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