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>
36 static void sighandler(int signal);
37 static int usage(void);
39 static uint8_t *state_data[_STATE_MAX] = {NULL};
40 static size_t state_len[_STATE_MAX] = {0};
42 static volatile int do_signal = 0;
43 static int urandom_fd = -1, allow_slaac_only = 0;
44 static bool bound = false, release = true;
45 static time_t last_update = 0;
48 int main(_unused int argc, char* const argv[])
50 // Allocate ressources
51 const char *pidfile = NULL;
52 const char *script = "/usr/sbin/odhcp6c-update";
57 enum odhcp6c_ia_mode ia_na_mode = IA_MODE_TRY;
58 enum odhcp6c_ia_mode ia_pd_mode = IA_MODE_TRY;
59 static struct in6_addr ifid = IN6ADDR_ANY_INIT;
60 int sol_timeout = 120;
62 bool help = false, daemonize = false;
64 int c, request_pd = 0;
65 while ((c = getopt(argc, argv, "S::N:P:Fc:i:r:s:kt:hedp:")) != -1) {
68 allow_slaac_only = (optarg) ? atoi(optarg) : -1;
72 if (!strcmp(optarg, "force")) {
73 ia_na_mode = IA_MODE_FORCE;
74 allow_slaac_only = -1;
75 } else if (!strcmp(optarg, "none")) {
76 ia_na_mode = IA_MODE_NONE;
77 } else if (!strcmp(optarg, "try")) {
78 ia_na_mode = IA_MODE_TRY;
85 if (allow_slaac_only >= 0 && allow_slaac_only < 10)
86 allow_slaac_only = 10;
88 request_pd = strtoul(optarg, NULL, 10);
92 ia_pd_mode = IA_MODE_TRY;
96 allow_slaac_only = -1;
97 ia_pd_mode = IA_MODE_FORCE;
101 l = script_unhexlify(&buf[4], sizeof(buf) - 4, optarg);
104 buf[1] = DHCPV6_OPT_CLIENTID;
107 odhcp6c_add_state(STATE_CLIENT_ID, buf, l + 4);
114 if (inet_pton(AF_INET6, optarg, &ifid) != 1)
121 opttype = htons(strtoul(optarg, &optpos, 10));
122 if (optpos == optarg)
126 odhcp6c_add_state(STATE_ORO, &opttype, 2);
139 sol_timeout = atoi(optarg);
143 logopt |= LOG_PERROR;
160 openlog("odhcp6c", logopt, LOG_DAEMON);
161 const char *ifname = argv[optind];
166 signal(SIGIO, sighandler);
167 signal(SIGHUP, sighandler);
168 signal(SIGINT, sighandler);
169 signal(SIGCHLD, sighandler);
170 signal(SIGTERM, sighandler);
171 signal(SIGUSR1, sighandler);
172 signal(SIGUSR2, sighandler);
174 if ((urandom_fd = open("/dev/urandom", O_CLOEXEC | O_RDONLY)) < 0 ||
175 init_dhcpv6(ifname, request_pd, sol_timeout) ||
176 ra_init(ifname, &ifid) || script_init(script, ifname)) {
177 syslog(LOG_ERR, "failed to initialize: %s", strerror(errno));
182 openlog("odhcp6c", LOG_PID, LOG_DAEMON); // Disable LOG_PERROR
184 syslog(LOG_ERR, "Failed to daemonize: %s",
191 snprintf(pidbuf, sizeof(pidbuf),
192 "/var/run/odhcp6c.%s.pid", ifname);
196 int fd = open(pidfile, O_WRONLY | O_CREAT);
199 int len = snprintf(buf, sizeof(buf), "%i\n", getpid());
205 script_call("started");
207 while (do_signal != SIGTERM) { // Main logic
208 odhcp6c_clear_state(STATE_SERVER_ID);
209 odhcp6c_clear_state(STATE_IA_NA);
210 odhcp6c_clear_state(STATE_IA_PD);
211 odhcp6c_clear_state(STATE_SNTP_IP);
212 odhcp6c_clear_state(STATE_SNTP_FQDN);
213 odhcp6c_clear_state(STATE_SIP_IP);
214 odhcp6c_clear_state(STATE_SIP_FQDN);
215 dhcpv6_set_ia_mode(ia_na_mode, ia_pd_mode);
218 // Server candidates need deep-delete
220 struct dhcpv6_server_cand *cand = odhcp6c_get_state(STATE_SERVER_CAND, &cand_len);
221 for (size_t i = 0; i < cand_len / sizeof(*cand); ++i) {
225 odhcp6c_clear_state(STATE_SERVER_CAND);
227 syslog(LOG_NOTICE, "(re)starting transaction on %s", ifname);
230 int res = dhcpv6_request(DHCPV6_MSG_SOLICIT);
231 odhcp6c_signal_process();
234 continue; // Might happen if we got a signal
235 } else if (res == DHCPV6_STATELESS) { // Stateless mode
236 while (do_signal == 0 || do_signal == SIGUSR1) {
239 res = dhcpv6_request(DHCPV6_MSG_INFO_REQ);
240 odhcp6c_signal_process();
241 if (do_signal == SIGUSR1)
246 script_call("informed");
249 syslog(LOG_NOTICE, "entering stateless-mode on %s", ifname);
251 if (dhcpv6_poll_reconfigure() > 0)
252 script_call("informed");
259 if (dhcpv6_request(DHCPV6_MSG_REQUEST) <= 0)
262 odhcp6c_signal_process();
263 script_call("bound");
265 syslog(LOG_NOTICE, "entering stateful-mode on %s", ifname);
267 while (do_signal == 0 || do_signal == SIGUSR1) {
269 // Wait for T1 to expire or until we get a reconfigure
270 int res = dhcpv6_poll_reconfigure();
271 odhcp6c_signal_process();
273 script_call("updated");
277 // Handle signal, if necessary
278 if (do_signal == SIGUSR1)
279 do_signal = 0; // Acknowledged
280 else if (do_signal > 0)
281 break; // Other signal type
283 size_t ia_pd_len, ia_na_len, ia_pd_new, ia_na_new;
284 odhcp6c_get_state(STATE_IA_PD, &ia_pd_len);
285 odhcp6c_get_state(STATE_IA_NA, &ia_na_len);
287 // If we have any IAs, send renew, otherwise request
289 if (ia_pd_len == 0 && ia_na_len == 0)
290 r = dhcpv6_request(DHCPV6_MSG_REQUEST);
292 r = dhcpv6_request(DHCPV6_MSG_RENEW);
293 odhcp6c_signal_process();
294 if (r > 0) { // Renew was succesfull
296 script_call("updated");
297 continue; // Renew was successful
300 odhcp6c_clear_state(STATE_SERVER_ID); // Remove binding
302 // If we have IAs, try rebind otherwise restart
303 res = dhcpv6_request(DHCPV6_MSG_REBIND);
304 odhcp6c_signal_process();
306 odhcp6c_get_state(STATE_IA_PD, &ia_pd_new);
307 odhcp6c_get_state(STATE_IA_NA, &ia_na_new);
308 if (res <= 0 || (ia_pd_new == 0 && ia_pd_len) ||
309 (ia_na_new == 0 && ia_na_len))
310 break; // We lost all our IAs, restart
312 script_call("rebound");
316 size_t ia_pd_len, ia_na_len, server_id_len;
317 odhcp6c_get_state(STATE_IA_PD, &ia_pd_len);
318 odhcp6c_get_state(STATE_IA_NA, &ia_na_len);
319 odhcp6c_get_state(STATE_SERVER_ID, &server_id_len);
321 // Add all prefixes to lost prefixes
323 script_call("unbound");
325 if (server_id_len > 0 && (ia_pd_len > 0 || ia_na_len > 0) && release)
326 dhcpv6_request(DHCPV6_MSG_RELEASE);
328 odhcp6c_clear_state(STATE_IA_NA);
329 odhcp6c_clear_state(STATE_IA_PD);
332 script_call("stopped");
337 static int usage(void)
340 "Usage: odhcp6c [options] <interface>\n"
341 "\nFeature options:\n"
342 " -S <time> Wait at least <time> sec for a DHCP-server (0)\n"
343 " -N <mode> Mode for requesting addresses [try|force|none]\n"
344 " -P <length> Request IPv6-Prefix (0 = auto)\n"
345 " -F Force IPv6-Prefix\n"
346 " -c <clientid> Override client-ID (base-16 encoded)\n"
347 " -i <iface-id> Use a custom interface identifier for RA handling\n"
348 " -r <options> Options to be requested (comma-separated)\n"
349 " -s <script> Status update script (/usr/sbin/odhcp6c-update)\n"
350 " -k Don't send a RELEASE when stopping\n"
351 " -t <seconds> Maximum timeout for DHCPv6-SOLICIT (120)\n"
352 "\nInvocation options:\n"
353 " -p <pidfile> Set pidfile (/var/run/6relayd.pid)\n"
355 " -e Write logmessages to stderr\n"
356 //" -v Increase logging verbosity\n"
357 " -h Show this help\n\n";
358 write(STDERR_FILENO, buf, sizeof(buf));
363 // Don't want to pull-in librt and libpthread just for a monotonic clock...
364 uint64_t odhcp6c_get_milli_time(void)
366 struct timespec t = {0, 0};
367 syscall(SYS_clock_gettime, CLOCK_MONOTONIC, &t);
368 return t.tv_sec * 1000 + t.tv_nsec / 1000000;
372 static uint8_t* odhcp6c_resize_state(enum odhcp6c_state state, ssize_t len)
375 return state_data[state] + state_len[state];
376 else if (state_len[state] + len > 1024)
379 uint8_t *n = realloc(state_data[state], state_len[state] + len);
380 if (n || state_len[state] + len == 0) {
381 state_data[state] = n;
382 n += state_len[state];
383 state_len[state] += len;
389 bool odhcp6c_signal_process(void)
391 if (do_signal == SIGIO) {
393 bool ra_updated = ra_process();
395 if (ra_updated && (bound || allow_slaac_only == 0))
396 script_call("ra-updated"); // Immediate process urgent events
397 else if (ra_updated && !bound && allow_slaac_only > 0)
398 script_delay_call("ra-updated", allow_slaac_only);
401 return do_signal != 0;
405 void odhcp6c_clear_state(enum odhcp6c_state state)
407 state_len[state] = 0;
411 void odhcp6c_add_state(enum odhcp6c_state state, const void *data, size_t len)
413 uint8_t *n = odhcp6c_resize_state(state, len);
415 memcpy(n, data, len);
419 size_t odhcp6c_remove_state(enum odhcp6c_state state, size_t offset, size_t len)
421 uint8_t *data = state_data[state];
422 ssize_t len_after = state_len[state] - (offset + len);
424 return state_len[state];
426 memmove(data + offset, data + offset + len, len_after);
427 return state_len[state] -= len;
431 void* odhcp6c_move_state(enum odhcp6c_state state, size_t *len)
433 *len = state_len[state];
434 void *data = state_data[state];
436 state_len[state] = 0;
437 state_data[state] = NULL;
443 void* odhcp6c_get_state(enum odhcp6c_state state, size_t *len)
445 *len = state_len[state];
446 return state_data[state];
450 struct odhcp6c_entry* odhcp6c_find_entry(enum odhcp6c_state state, const struct odhcp6c_entry *new)
452 size_t len, cmplen = offsetof(struct odhcp6c_entry, target) + new->length / 8;
453 struct odhcp6c_entry *start = odhcp6c_get_state(state, &len);
454 struct odhcp6c_entry *x = NULL;
456 for (struct odhcp6c_entry *c = start; !x && c < &start[len/sizeof(*c)]; ++c)
457 if (!memcmp(c, new, cmplen))
464 bool odhcp6c_update_entry_safe(enum odhcp6c_state state, struct odhcp6c_entry *new, uint32_t safe)
467 struct odhcp6c_entry *x = odhcp6c_find_entry(state, new);
468 struct odhcp6c_entry *start = odhcp6c_get_state(state, &len);
471 if (x && x->valid > new->valid && new->valid < safe)
474 if (new->valid > 0) {
476 if (new->valid >= x->valid && new->valid - x->valid < 60 &&
477 new->preferred >= x->preferred &&
478 new->preferred - x->preferred < 60 &&
479 x->class == new->class)
481 x->valid = new->valid;
482 x->preferred = new->preferred;
485 x->class = new->class;
487 odhcp6c_add_state(state, new, sizeof(*new));
490 odhcp6c_remove_state(state, (x - start) * sizeof(*x), sizeof(*x));
496 bool odhcp6c_update_entry(enum odhcp6c_state state, struct odhcp6c_entry *new)
498 return odhcp6c_update_entry_safe(state, new, 0);
502 static void odhcp6c_expire_list(enum odhcp6c_state state, uint32_t elapsed)
505 struct odhcp6c_entry *start = odhcp6c_get_state(state, &len);
506 for (struct odhcp6c_entry *c = start; c < &start[len / sizeof(*c)]; ++c) {
509 else if (c->t1 != UINT32_MAX)
514 else if (c->t2 != UINT32_MAX)
517 if (c->preferred < elapsed)
519 else if (c->preferred != UINT32_MAX)
520 c->preferred -= elapsed;
522 if (c->valid < elapsed)
524 else if (c->valid != UINT32_MAX)
528 odhcp6c_remove_state(state, (c - start) * sizeof(*c), sizeof(*c));
533 void odhcp6c_expire(void)
535 time_t now = odhcp6c_get_milli_time() / 1000;
536 uint32_t elapsed = (last_update > 0) ? now - last_update : 0;
539 odhcp6c_expire_list(STATE_RA_PREFIX, elapsed);
540 odhcp6c_expire_list(STATE_RA_ROUTE, elapsed);
541 odhcp6c_expire_list(STATE_RA_DNS, elapsed);
542 odhcp6c_expire_list(STATE_IA_NA, elapsed);
543 odhcp6c_expire_list(STATE_IA_PD, elapsed);
547 uint32_t odhcp6c_elapsed(void)
549 return odhcp6c_get_milli_time() / 1000 - last_update;
553 void odhcp6c_random(void *buf, size_t len)
555 read(urandom_fd, buf, len);
558 bool odhcp6c_is_bound(void)
563 static void sighandler(int signal)
565 if (signal == SIGCHLD)
566 while (waitpid(-1, NULL, WNOHANG) > 0);
567 else if (signal == SIGUSR1)
569 else if (signal == SIGUSR2)
571 else if (signal == SIGIO)