2 * Copyright (C) 2012-2013 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>
40 static void sighandler(int signal);
41 static int usage(void);
43 static uint8_t *state_data[_STATE_MAX] = {NULL};
44 static size_t state_len[_STATE_MAX] = {0};
46 static volatile bool signal_io = false;
47 static volatile bool signal_usr1 = false;
48 static volatile bool signal_usr2 = false;
49 static volatile bool signal_term = false;
51 static int urandom_fd = -1, allow_slaac_only = 0;
52 static bool bound = false, release = true;
53 static time_t last_update = 0;
56 int main(_unused int argc, char* const argv[])
58 // Allocate ressources
59 const char *pidfile = NULL;
60 const char *script = "/usr/sbin/odhcp6c-update";
65 enum odhcp6c_ia_mode ia_na_mode = IA_MODE_TRY;
66 enum odhcp6c_ia_mode ia_pd_mode = IA_MODE_TRY;
67 static struct in6_addr ifid = IN6ADDR_ANY_INIT;
68 int sol_timeout = DHCPV6_SOL_MAX_RT;
71 int bfd_interval = 0, bfd_loss = 3;
74 bool help = false, daemonize = false, strict_options = false;
76 int c, request_pd = 0;
77 while ((c = getopt(argc, argv, "S::N:P:FB:c:i:r:Rs:kt:hedp:")) != -1) {
80 allow_slaac_only = (optarg) ? atoi(optarg) : -1;
84 if (!strcmp(optarg, "force")) {
85 ia_na_mode = IA_MODE_FORCE;
86 allow_slaac_only = -1;
87 } else if (!strcmp(optarg, "none")) {
88 ia_na_mode = IA_MODE_NONE;
89 } else if (!strcmp(optarg, "try")) {
90 ia_na_mode = IA_MODE_TRY;
97 if (allow_slaac_only >= 0 && allow_slaac_only < 10)
98 allow_slaac_only = 10;
100 request_pd = strtoul(optarg, NULL, 10);
107 allow_slaac_only = -1;
108 ia_pd_mode = IA_MODE_FORCE;
113 bfd_interval = atoi(optarg);
118 l = script_unhexlify(&buf[4], sizeof(buf) - 4, optarg);
121 buf[1] = DHCPV6_OPT_CLIENTID;
124 odhcp6c_add_state(STATE_CLIENT_ID, buf, l + 4);
131 if (inet_pton(AF_INET6, optarg, &ifid) != 1)
138 opttype = htons(strtoul(optarg, &optpos, 10));
139 if (optpos == optarg)
143 odhcp6c_add_state(STATE_ORO, &opttype, 2);
148 strict_options = true;
160 sol_timeout = atoi(optarg);
164 logopt |= LOG_PERROR;
181 openlog("odhcp6c", logopt, LOG_DAEMON);
182 const char *ifname = argv[optind];
187 signal(SIGIO, sighandler);
188 signal(SIGHUP, sighandler);
189 signal(SIGINT, sighandler);
190 signal(SIGCHLD, sighandler);
191 signal(SIGTERM, sighandler);
192 signal(SIGUSR1, sighandler);
193 signal(SIGUSR2, sighandler);
195 if ((urandom_fd = open("/dev/urandom", O_CLOEXEC | O_RDONLY)) < 0 ||
196 init_dhcpv6(ifname, request_pd, strict_options, sol_timeout) ||
197 ra_init(ifname, &ifid) || script_init(script, ifname)) {
198 syslog(LOG_ERR, "failed to initialize: %s", strerror(errno));
203 openlog("odhcp6c", LOG_PID, LOG_DAEMON); // Disable LOG_PERROR
205 syslog(LOG_ERR, "Failed to daemonize: %s",
212 snprintf(pidbuf, sizeof(pidbuf),
213 "/var/run/odhcp6c.%s.pid", ifname);
217 int fd = open(pidfile, O_WRONLY | O_CREAT);
220 int len = snprintf(buf, sizeof(buf), "%i\n", getpid());
226 script_call("started");
228 while (!signal_term) { // Main logic
229 odhcp6c_clear_state(STATE_SERVER_ID);
230 odhcp6c_clear_state(STATE_IA_NA);
231 odhcp6c_clear_state(STATE_IA_PD);
232 odhcp6c_clear_state(STATE_SNTP_IP);
233 odhcp6c_clear_state(STATE_NTP_IP);
234 odhcp6c_clear_state(STATE_NTP_FQDN);
235 odhcp6c_clear_state(STATE_SIP_IP);
236 odhcp6c_clear_state(STATE_SIP_FQDN);
237 dhcpv6_set_ia_mode(ia_na_mode, ia_pd_mode);
240 syslog(LOG_NOTICE, "(re)starting transaction on %s", ifname);
242 signal_usr1 = signal_usr2 = false;
243 int mode = dhcpv6_request(DHCPV6_MSG_SOLICIT);
244 odhcp6c_signal_process();
250 int res = dhcpv6_request(mode == DHCPV6_STATELESS ?
251 DHCPV6_MSG_INFO_REQ : DHCPV6_MSG_REQUEST);
252 bool signalled = odhcp6c_signal_process();
256 else if (signalled) {
261 mode = dhcpv6_promote_server_cand();
262 } while (mode > DHCPV6_UNKNOWN);
268 case DHCPV6_STATELESS:
270 syslog(LOG_NOTICE, "entering stateless-mode on %s", ifname);
272 while (!signal_usr2 && !signal_term) {
274 script_call("informed");
276 int res = dhcpv6_poll_reconfigure();
277 odhcp6c_signal_process();
283 signal_usr1 = false; // Acknowledged
286 if (signal_usr2 || signal_term)
289 res = dhcpv6_request(DHCPV6_MSG_INFO_REQ);
290 odhcp6c_signal_process();
298 case DHCPV6_STATEFUL:
299 script_call("bound");
301 syslog(LOG_NOTICE, "entering stateful-mode on %s", ifname);
303 if (bfd_interval > 0)
304 bfd_start(ifname, bfd_loss, bfd_interval);
307 while (!signal_usr2 && !signal_term) {
309 // Wait for T1 to expire or until we get a reconfigure
310 int res = dhcpv6_poll_reconfigure();
311 odhcp6c_signal_process();
313 script_call("updated");
317 // Handle signal, if necessary
319 signal_usr1 = false; // Acknowledged
320 if (signal_usr2 || signal_term)
321 break; // Other signal type
323 // Send renew as T1 expired
324 size_t ia_pd_len, ia_na_len;
325 odhcp6c_get_state(STATE_IA_PD, &ia_pd_len);
326 odhcp6c_get_state(STATE_IA_NA, &ia_na_len);
328 // If we have any IAs, send renew, otherwise request
329 if (ia_pd_len == 0 && ia_na_len == 0)
330 res = dhcpv6_request(DHCPV6_MSG_REQUEST);
332 res = dhcpv6_request(DHCPV6_MSG_RENEW);
334 odhcp6c_signal_process();
335 if (res > 0) { // Renew was succesfull
337 script_call("updated");
338 continue; // Renew was successful
341 odhcp6c_clear_state(STATE_SERVER_ID); // Remove binding
343 // If we have IAs, try rebind otherwise restart
344 res = dhcpv6_request(DHCPV6_MSG_REBIND);
345 odhcp6c_signal_process();
348 script_call("rebound");
362 size_t ia_pd_len, ia_na_len, server_id_len;
363 odhcp6c_get_state(STATE_IA_PD, &ia_pd_len);
364 odhcp6c_get_state(STATE_IA_NA, &ia_na_len);
365 odhcp6c_get_state(STATE_SERVER_ID, &server_id_len);
367 // Add all prefixes to lost prefixes
369 script_call("unbound");
371 if (server_id_len > 0 && (ia_pd_len > 0 || ia_na_len > 0) && release)
372 dhcpv6_request(DHCPV6_MSG_RELEASE);
374 odhcp6c_clear_state(STATE_IA_NA);
375 odhcp6c_clear_state(STATE_IA_PD);
378 script_call("stopped");
383 static int usage(void)
386 "Usage: odhcp6c [options] <interface>\n"
387 "\nFeature options:\n"
388 " -S <time> Wait at least <time> sec for a DHCP-server (0)\n"
389 " -N <mode> Mode for requesting addresses [try|force|none]\n"
390 " -P <length> Request IPv6-Prefix (0 = auto)\n"
391 " -F Force IPv6-Prefix\n"
393 " -B <interval> Enable BFD ping check\n"
395 " -c <clientid> Override client-ID (base-16 encoded)\n"
396 " -i <iface-id> Use a custom interface identifier for RA handling\n"
397 " -r <options> Options to be requested (comma-separated)\n"
398 " -R Do not request any options except those specified with -r\n"
399 " -s <script> Status update script (/usr/sbin/odhcp6c-update)\n"
400 " -k Don't send a RELEASE when stopping\n"
401 " -t <seconds> Maximum timeout for DHCPv6-SOLICIT (120)\n"
402 "\nInvocation options:\n"
403 " -p <pidfile> Set pidfile (/var/run/6relayd.pid)\n"
405 " -e Write logmessages to stderr\n"
406 //" -v Increase logging verbosity\n"
407 " -h Show this help\n\n";
408 write(STDERR_FILENO, buf, sizeof(buf));
413 // Don't want to pull-in librt and libpthread just for a monotonic clock...
414 uint64_t odhcp6c_get_milli_time(void)
416 struct timespec t = {0, 0};
417 syscall(SYS_clock_gettime, CLOCK_MONOTONIC, &t);
418 return t.tv_sec * 1000 + t.tv_nsec / 1000000;
422 static uint8_t* odhcp6c_resize_state(enum odhcp6c_state state, ssize_t len)
425 return state_data[state] + state_len[state];
426 else if (state_len[state] + len > 1024)
429 uint8_t *n = realloc(state_data[state], state_len[state] + len);
430 if (n || state_len[state] + len == 0) {
431 state_data[state] = n;
432 n += state_len[state];
433 state_len[state] += len;
439 bool odhcp6c_signal_process(void)
444 bool ra_updated = ra_process();
449 if (ra_updated && (bound || allow_slaac_only == 0))
450 script_call("ra-updated"); // Immediate process urgent events
451 else if (ra_updated && !bound && allow_slaac_only > 0)
452 script_delay_call("ra-updated", allow_slaac_only);
459 return signal_usr1 || signal_usr2 || signal_term;
463 void odhcp6c_clear_state(enum odhcp6c_state state)
465 state_len[state] = 0;
469 void odhcp6c_add_state(enum odhcp6c_state state, const void *data, size_t len)
471 uint8_t *n = odhcp6c_resize_state(state, len);
473 memcpy(n, data, len);
476 void odhcp6c_insert_state(enum odhcp6c_state state, size_t offset, const void *data, size_t len)
478 ssize_t len_after = state_len[state] - offset;
482 uint8_t *n = odhcp6c_resize_state(state, len);
484 uint8_t *sdata = state_data[state];
486 memmove(sdata + offset + len, sdata + offset, len_after);
487 memcpy(sdata + offset, data, len);
491 size_t odhcp6c_remove_state(enum odhcp6c_state state, size_t offset, size_t len)
493 uint8_t *data = state_data[state];
494 ssize_t len_after = state_len[state] - (offset + len);
496 return state_len[state];
498 memmove(data + offset, data + offset + len, len_after);
499 return state_len[state] -= len;
503 void* odhcp6c_move_state(enum odhcp6c_state state, size_t *len)
505 *len = state_len[state];
506 void *data = state_data[state];
508 state_len[state] = 0;
509 state_data[state] = NULL;
515 void* odhcp6c_get_state(enum odhcp6c_state state, size_t *len)
517 *len = state_len[state];
518 return state_data[state];
522 struct odhcp6c_entry* odhcp6c_find_entry(enum odhcp6c_state state, const struct odhcp6c_entry *new)
524 size_t len, cmplen = offsetof(struct odhcp6c_entry, target) + new->length / 8;
525 struct odhcp6c_entry *start = odhcp6c_get_state(state, &len);
526 struct odhcp6c_entry *x = NULL;
528 for (struct odhcp6c_entry *c = start; !x && c < &start[len/sizeof(*c)]; ++c)
529 if (!memcmp(c, new, cmplen))
536 bool odhcp6c_update_entry_safe(enum odhcp6c_state state, struct odhcp6c_entry *new, uint32_t safe)
539 struct odhcp6c_entry *x = odhcp6c_find_entry(state, new);
540 struct odhcp6c_entry *start = odhcp6c_get_state(state, &len);
542 if (x && x->valid > new->valid && new->valid < safe)
545 if (new->valid > 0) {
547 if (new->valid >= x->valid && new->valid - x->valid < 60 &&
548 new->preferred >= x->preferred &&
549 new->preferred - x->preferred < 60 &&
550 x->class == new->class)
552 x->valid = new->valid;
553 x->preferred = new->preferred;
556 x->class = new->class;
558 odhcp6c_add_state(state, new, sizeof(*new));
561 odhcp6c_remove_state(state, (x - start) * sizeof(*x), sizeof(*x));
567 bool odhcp6c_update_entry(enum odhcp6c_state state, struct odhcp6c_entry *new)
569 return odhcp6c_update_entry_safe(state, new, 0);
573 static void odhcp6c_expire_list(enum odhcp6c_state state, uint32_t elapsed)
576 struct odhcp6c_entry *start = odhcp6c_get_state(state, &len);
577 for (struct odhcp6c_entry *c = start; c < &start[len / sizeof(*c)]; ++c) {
580 else if (c->t1 != UINT32_MAX)
585 else if (c->t2 != UINT32_MAX)
588 if (c->preferred < elapsed)
590 else if (c->preferred != UINT32_MAX)
591 c->preferred -= elapsed;
593 if (c->valid < elapsed)
595 else if (c->valid != UINT32_MAX)
599 odhcp6c_remove_state(state, (c - start) * sizeof(*c), sizeof(*c));
604 void odhcp6c_expire(void)
606 time_t now = odhcp6c_get_milli_time() / 1000;
607 uint32_t elapsed = (last_update > 0) ? now - last_update : 0;
610 odhcp6c_expire_list(STATE_RA_PREFIX, elapsed);
611 odhcp6c_expire_list(STATE_RA_ROUTE, elapsed);
612 odhcp6c_expire_list(STATE_RA_DNS, elapsed);
613 odhcp6c_expire_list(STATE_IA_NA, elapsed);
614 odhcp6c_expire_list(STATE_IA_PD, elapsed);
618 uint32_t odhcp6c_elapsed(void)
620 return odhcp6c_get_milli_time() / 1000 - last_update;
624 void odhcp6c_random(void *buf, size_t len)
626 read(urandom_fd, buf, len);
629 bool odhcp6c_is_bound(void)
634 static void sighandler(int signal)
636 if (signal == SIGCHLD)
637 while (waitpid(-1, NULL, WNOHANG) > 0);
638 else if (signal == SIGUSR1)
640 else if (signal == SIGUSR2)
642 else if (signal == SIGIO)