2 * Copyright (C) 2012-2014 Steven Barth <steven@midlink.org>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License v2 as published by
6 * the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
29 #include <sys/syscall.h>
30 #include <arpa/inet.h>
37 static void sighandler(int signal);
38 static int usage(void);
40 static uint8_t *state_data[_STATE_MAX] = {NULL};
41 static size_t state_len[_STATE_MAX] = {0};
43 static volatile bool signal_io = false;
44 static volatile bool signal_usr1 = false;
45 static volatile bool signal_usr2 = false;
46 static volatile bool signal_term = false;
48 static int urandom_fd = -1, allow_slaac_only = 0;
49 static bool bound = false, release = true;
50 static time_t last_update = 0;
52 static unsigned int min_update_interval = DEFAULT_MIN_UPDATE_INTERVAL;
54 int main(_unused int argc, char* const argv[])
56 // Allocate ressources
57 const char *pidfile = NULL;
58 const char *script = "/usr/sbin/odhcp6c-update";
64 enum odhcp6c_ia_mode ia_na_mode = IA_MODE_TRY;
65 enum odhcp6c_ia_mode ia_pd_mode = IA_MODE_NONE;
66 int ia_pd_iaid_index = 0;
67 static struct in6_addr ifid = IN6ADDR_ANY_INIT;
68 int sol_timeout = DHCPV6_SOL_MAX_RT;
71 bool help = false, daemonize = false;
74 unsigned int client_options = DHCPV6_CLIENT_FQDN | DHCPV6_ACCEPT_RECONFIGURE;
76 while ((c = getopt(argc, argv, "S::N:V:P:FB:c:i:r:Ru:s:kt:m:hedp:fa")) != -1) {
79 allow_slaac_only = (optarg) ? atoi(optarg) : -1;
83 if (!strcmp(optarg, "force")) {
84 ia_na_mode = IA_MODE_FORCE;
85 allow_slaac_only = -1;
86 } else if (!strcmp(optarg, "none")) {
87 ia_na_mode = IA_MODE_NONE;
88 } else if (!strcmp(optarg, "try")) {
89 ia_na_mode = IA_MODE_TRY;
96 l = script_unhexlify(buf, sizeof(buf), optarg);
100 odhcp6c_add_state(STATE_VENDORCLASS, buf, l);
104 if (ia_pd_mode == IA_MODE_NONE)
105 ia_pd_mode = IA_MODE_TRY;
107 if (allow_slaac_only >= 0 && allow_slaac_only < 10)
108 allow_slaac_only = 10;
113 int prefix_length = strtoul(optarg, &iaid_begin, 10);
115 if (*iaid_begin != '\0' && *iaid_begin != ',' && *iaid_begin != ':') {
116 syslog(LOG_ERR, "invalid argument: '%s'", optarg);
120 struct odhcp6c_request_prefix prefix = { 0, prefix_length };
122 if (*iaid_begin == ',' && (iaid_len = strlen(iaid_begin)) > 1)
123 memcpy(&prefix.iaid, iaid_begin + 1, iaid_len > 4 ? 4 : iaid_len);
124 else if (*iaid_begin == ':')
125 prefix.iaid = htonl((uint32_t)strtoul(&iaid_begin[1], NULL, 16));
127 prefix.iaid = htonl(++ia_pd_iaid_index);
129 odhcp6c_add_state(STATE_IA_PD_INIT, &prefix, sizeof(prefix));
134 allow_slaac_only = -1;
135 ia_pd_mode = IA_MODE_FORCE;
139 l = script_unhexlify(&buf[4], sizeof(buf) - 4, optarg);
142 buf[1] = DHCPV6_OPT_CLIENTID;
145 odhcp6c_add_state(STATE_CLIENT_ID, buf, l + 4);
152 if (inet_pton(AF_INET6, optarg, &ifid) != 1)
159 opttype = htons(strtoul(optarg, &optpos, 10));
160 if (optpos == optarg)
164 odhcp6c_add_state(STATE_ORO, &opttype, 2);
169 client_options |= DHCPV6_STRICT_OPTIONS;
173 optlen = htons(strlen(optarg));
174 odhcp6c_add_state(STATE_USERCLASS, &optlen, 2);
175 odhcp6c_add_state(STATE_USERCLASS, optarg, strlen(optarg));
187 sol_timeout = atoi(optarg);
191 min_update_interval = atoi(optarg);
195 logopt |= LOG_PERROR;
207 client_options &= ~DHCPV6_CLIENT_FQDN;
211 client_options &= ~DHCPV6_ACCEPT_RECONFIGURE;
220 openlog("odhcp6c", logopt, LOG_DAEMON);
221 const char *ifname = argv[optind];
226 signal(SIGIO, sighandler);
227 signal(SIGHUP, sighandler);
228 signal(SIGINT, sighandler);
229 signal(SIGCHLD, sighandler);
230 signal(SIGTERM, sighandler);
231 signal(SIGUSR1, sighandler);
232 signal(SIGUSR2, sighandler);
234 if ((urandom_fd = open("/dev/urandom", O_CLOEXEC | O_RDONLY)) < 0 ||
235 init_dhcpv6(ifname, client_options, sol_timeout) ||
236 ra_init(ifname, &ifid) || script_init(script, ifname)) {
237 syslog(LOG_ERR, "failed to initialize: %s", strerror(errno));
242 openlog("odhcp6c", LOG_PID, LOG_DAEMON); // Disable LOG_PERROR
244 syslog(LOG_ERR, "Failed to daemonize: %s",
251 snprintf(pidbuf, sizeof(pidbuf),
252 "/var/run/odhcp6c.%s.pid", ifname);
256 int fd = open(pidfile, O_WRONLY | O_CREAT, 0644);
259 int len = snprintf(buf, sizeof(buf), "%i\n", getpid());
265 script_call("started");
267 while (!signal_term) { // Main logic
268 odhcp6c_clear_state(STATE_SERVER_ID);
269 odhcp6c_clear_state(STATE_SERVER_ADDR);
270 odhcp6c_clear_state(STATE_IA_NA);
271 odhcp6c_clear_state(STATE_IA_PD);
272 odhcp6c_clear_state(STATE_SNTP_IP);
273 odhcp6c_clear_state(STATE_NTP_IP);
274 odhcp6c_clear_state(STATE_NTP_FQDN);
275 odhcp6c_clear_state(STATE_SIP_IP);
276 odhcp6c_clear_state(STATE_SIP_FQDN);
277 dhcpv6_set_ia_mode(ia_na_mode, ia_pd_mode);
280 syslog(LOG_NOTICE, "(re)starting transaction on %s", ifname);
282 signal_usr1 = signal_usr2 = false;
283 int mode = dhcpv6_request(DHCPV6_MSG_SOLICIT);
284 odhcp6c_signal_process();
290 int res = dhcpv6_request(mode == DHCPV6_STATELESS ?
291 DHCPV6_MSG_INFO_REQ : DHCPV6_MSG_REQUEST);
292 bool signalled = odhcp6c_signal_process();
296 else if (signalled) {
301 mode = dhcpv6_promote_server_cand();
302 } while (mode > DHCPV6_UNKNOWN);
308 case DHCPV6_STATELESS:
310 syslog(LOG_NOTICE, "entering stateless-mode on %s", ifname);
312 while (!signal_usr2 && !signal_term) {
314 script_call("informed");
316 int res = dhcpv6_poll_reconfigure();
317 odhcp6c_signal_process();
323 signal_usr1 = false; // Acknowledged
326 if (signal_usr2 || signal_term)
329 res = dhcpv6_request(DHCPV6_MSG_INFO_REQ);
330 odhcp6c_signal_process();
338 case DHCPV6_STATEFUL:
339 script_call("bound");
341 syslog(LOG_NOTICE, "entering stateful-mode on %s", ifname);
343 while (!signal_usr2 && !signal_term) {
345 // Wait for T1 to expire or until we get a reconfigure
346 int res = dhcpv6_poll_reconfigure();
347 odhcp6c_signal_process();
349 script_call("updated");
353 // Handle signal, if necessary
355 signal_usr1 = false; // Acknowledged
356 if (signal_usr2 || signal_term)
357 break; // Other signal type
359 // Send renew as T1 expired
360 res = dhcpv6_request(DHCPV6_MSG_RENEW);
361 odhcp6c_signal_process();
362 if (res > 0) { // Renew was succesfull
364 script_call("updated");
365 continue; // Renew was successful
368 odhcp6c_clear_state(STATE_SERVER_ID); // Remove binding
369 odhcp6c_clear_state(STATE_SERVER_ADDR);
371 size_t ia_pd_len, ia_na_len;
372 odhcp6c_get_state(STATE_IA_PD, &ia_pd_len);
373 odhcp6c_get_state(STATE_IA_NA, &ia_na_len);
375 if (ia_pd_len == 0 && ia_na_len == 0)
378 // If we have IAs, try rebind otherwise restart
379 res = dhcpv6_request(DHCPV6_MSG_REBIND);
380 odhcp6c_signal_process();
383 script_call("rebound");
394 size_t ia_pd_len, ia_na_len, server_id_len;
395 odhcp6c_get_state(STATE_IA_PD, &ia_pd_len);
396 odhcp6c_get_state(STATE_IA_NA, &ia_na_len);
397 odhcp6c_get_state(STATE_SERVER_ID, &server_id_len);
399 // Add all prefixes to lost prefixes
401 script_call("unbound");
403 if (server_id_len > 0 && (ia_pd_len > 0 || ia_na_len > 0) && release)
404 dhcpv6_request(DHCPV6_MSG_RELEASE);
406 odhcp6c_clear_state(STATE_IA_NA);
407 odhcp6c_clear_state(STATE_IA_PD);
410 script_call("stopped");
415 static int usage(void)
418 "Usage: odhcp6c [options] <interface>\n"
419 "\nFeature options:\n"
420 " -S <time> Wait at least <time> sec for a DHCP-server (0)\n"
421 " -N <mode> Mode for requesting addresses [try|force|none]\n"
422 " -P <length> Request IPv6-Prefix (0 = auto)\n"
423 " -F Force IPv6-Prefix\n"
424 " -V <class> Set vendor-class option (base-16 encoded)\n"
425 " -u <user-class> Set user-class option string\n"
426 " -c <clientid> Override client-ID (base-16 encoded)\n"
427 " -i <iface-id> Use a custom interface identifier for RA handling\n"
428 " -r <options> Options to be requested (comma-separated)\n"
429 " -R Do not request any options except those specified with -r\n"
430 " -s <script> Status update script (/usr/sbin/odhcp6c-update)\n"
431 " -a Don't send Accept Reconfigure option\n"
432 " -f Don't send Client FQDN option\n"
433 " -k Don't send a RELEASE when stopping\n"
434 " -t <seconds> Maximum timeout for DHCPv6-SOLICIT (3600)\n"
435 " -m <seconds> Minimum time between accepting updates (30)\n"
436 "\nInvocation options:\n"
437 " -p <pidfile> Set pidfile (/var/run/odhcp6c.pid)\n"
439 " -e Write logmessages to stderr\n"
440 //" -v Increase logging verbosity\n"
441 " -h Show this help\n\n";
442 write(STDERR_FILENO, buf, sizeof(buf));
447 // Don't want to pull-in librt and libpthread just for a monotonic clock...
448 uint64_t odhcp6c_get_milli_time(void)
450 struct timespec t = {0, 0};
451 syscall(SYS_clock_gettime, CLOCK_MONOTONIC, &t);
452 return ((uint64_t)t.tv_sec) * 1000 + ((uint64_t)t.tv_nsec) / 1000000;
456 static uint8_t* odhcp6c_resize_state(enum odhcp6c_state state, ssize_t len)
459 return state_data[state] + state_len[state];
460 else if (state_len[state] + len > 1024)
463 uint8_t *n = realloc(state_data[state], state_len[state] + len);
464 if (n || state_len[state] + len == 0) {
465 state_data[state] = n;
466 n += state_len[state];
467 state_len[state] += len;
473 bool odhcp6c_signal_process(void)
478 bool ra_updated = ra_process();
483 if (ra_updated && (bound || allow_slaac_only >= 0))
484 script_call("ra-updated"); // Immediate process urgent events
487 return signal_usr1 || signal_usr2 || signal_term;
491 void odhcp6c_clear_state(enum odhcp6c_state state)
493 state_len[state] = 0;
497 void odhcp6c_add_state(enum odhcp6c_state state, const void *data, size_t len)
499 uint8_t *n = odhcp6c_resize_state(state, len);
501 memcpy(n, data, len);
504 void odhcp6c_insert_state(enum odhcp6c_state state, size_t offset, const void *data, size_t len)
506 ssize_t len_after = state_len[state] - offset;
510 uint8_t *n = odhcp6c_resize_state(state, len);
512 uint8_t *sdata = state_data[state];
514 memmove(sdata + offset + len, sdata + offset, len_after);
515 memcpy(sdata + offset, data, len);
519 size_t odhcp6c_remove_state(enum odhcp6c_state state, size_t offset, size_t len)
521 uint8_t *data = state_data[state];
522 ssize_t len_after = state_len[state] - (offset + len);
524 return state_len[state];
526 memmove(data + offset, data + offset + len, len_after);
527 return state_len[state] -= len;
531 void* odhcp6c_move_state(enum odhcp6c_state state, size_t *len)
533 *len = state_len[state];
534 void *data = state_data[state];
536 state_len[state] = 0;
537 state_data[state] = NULL;
543 void* odhcp6c_get_state(enum odhcp6c_state state, size_t *len)
545 *len = state_len[state];
546 return state_data[state];
550 struct odhcp6c_entry* odhcp6c_find_entry(enum odhcp6c_state state, const struct odhcp6c_entry *new)
552 size_t len, cmplen = offsetof(struct odhcp6c_entry, target) + new->length / 8;
553 struct odhcp6c_entry *start = odhcp6c_get_state(state, &len);
554 struct odhcp6c_entry *x = NULL;
556 for (struct odhcp6c_entry *c = start; !x && c < &start[len/sizeof(*c)]; ++c)
557 if (!memcmp(c, new, cmplen))
564 bool odhcp6c_update_entry_safe(enum odhcp6c_state state, struct odhcp6c_entry *new, uint32_t safe)
567 struct odhcp6c_entry *x = odhcp6c_find_entry(state, new);
568 struct odhcp6c_entry *start = odhcp6c_get_state(state, &len);
570 if (x && x->valid > new->valid && new->valid < safe)
573 if (new->valid > 0) {
575 if (new->valid >= x->valid && new->valid != UINT32_MAX &&
576 new->valid - x->valid < min_update_interval &&
577 new->preferred >= x->preferred &&
578 new->preferred != UINT32_MAX &&
579 new->preferred - x->preferred < min_update_interval &&
580 x->class == new->class)
582 x->valid = new->valid;
583 x->preferred = new->preferred;
586 x->class = new->class;
589 odhcp6c_add_state(state, new, sizeof(*new));
592 odhcp6c_remove_state(state, (x - start) * sizeof(*x), sizeof(*x));
598 bool odhcp6c_update_entry(enum odhcp6c_state state, struct odhcp6c_entry *new)
600 return odhcp6c_update_entry_safe(state, new, 0);
604 static void odhcp6c_expire_list(enum odhcp6c_state state, uint32_t elapsed)
607 struct odhcp6c_entry *start = odhcp6c_get_state(state, &len);
608 for (struct odhcp6c_entry *c = start; c < &start[len / sizeof(*c)]; ++c) {
611 else if (c->t1 != UINT32_MAX)
616 else if (c->t2 != UINT32_MAX)
619 if (c->preferred < elapsed)
621 else if (c->preferred != UINT32_MAX)
622 c->preferred -= elapsed;
624 if (c->valid < elapsed)
626 else if (c->valid != UINT32_MAX)
630 odhcp6c_remove_state(state, (c - start) * sizeof(*c), sizeof(*c));
635 void odhcp6c_expire(void)
637 time_t now = odhcp6c_get_milli_time() / 1000;
638 uint32_t elapsed = (last_update > 0) ? now - last_update : 0;
641 odhcp6c_expire_list(STATE_RA_PREFIX, elapsed);
642 odhcp6c_expire_list(STATE_RA_ROUTE, elapsed);
643 odhcp6c_expire_list(STATE_RA_DNS, elapsed);
644 odhcp6c_expire_list(STATE_IA_NA, elapsed);
645 odhcp6c_expire_list(STATE_IA_PD, elapsed);
649 uint32_t odhcp6c_elapsed(void)
651 return odhcp6c_get_milli_time() / 1000 - last_update;
655 void odhcp6c_random(void *buf, size_t len)
657 read(urandom_fd, buf, len);
660 bool odhcp6c_is_bound(void)
665 static void sighandler(int signal)
667 if (signal == SIGCHLD)
668 while (waitpid(-1, NULL, WNOHANG) > 0);
669 else if (signal == SIGUSR1)
671 else if (signal == SIGUSR2)
673 else if (signal == SIGIO)