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);
40 static uint8_t *state_data[_STATE_MAX] = {NULL};
41 static size_t state_len[_STATE_MAX] = {0};
43 static volatile int do_signal = 0;
44 static int urandom_fd = -1, allow_slaac_only = 0;
45 static bool bound = false, release = true;
46 static time_t last_update = 0;
49 int main(_unused int argc, char* const argv[])
51 // Allocate ressources
52 const char *pidfile = NULL;
53 const char *script = "/usr/sbin/odhcp6c-update";
58 enum odhcp6c_ia_mode ia_na_mode = IA_MODE_TRY;
59 static struct in6_addr ifid = IN6ADDR_ANY_INIT;
61 bool help = false, daemonize = false;
63 int c, request_pd = 0;
64 while ((c = getopt(argc, argv, "S::N:P:c:i:r:s:khedp:")) != -1) {
67 allow_slaac_only = (optarg) ? atoi(optarg) : -1;
71 if (!strcmp(optarg, "force"))
72 ia_na_mode = IA_MODE_FORCE;
73 else if (!strcmp(optarg, "none"))
74 ia_na_mode = IA_MODE_NONE;
75 else if (!strcmp(optarg, "try"))
76 ia_na_mode = IA_MODE_TRY;
82 if (allow_slaac_only >= 0 && allow_slaac_only < 10)
83 allow_slaac_only = 10;
85 request_pd = strtoul(optarg, NULL, 10);
91 l = script_unhexlify(&buf[4], sizeof(buf) - 4, optarg);
94 buf[1] = DHCPV6_OPT_CLIENTID;
97 odhcp6c_add_state(STATE_CLIENT_ID, buf, l + 4);
104 if (inet_pton(AF_INET6, optarg, &ifid) != 1)
111 opttype = htons(strtoul(optarg, &optpos, 10));
112 if (optpos == optarg)
116 odhcp6c_add_state(STATE_ORO, &opttype, 2);
129 logopt |= LOG_PERROR;
146 openlog("odhcp6c", logopt, LOG_DAEMON);
147 const char *ifname = argv[optind];
152 signal(SIGIO, sighandler);
153 signal(SIGHUP, sighandler);
154 signal(SIGINT, sighandler);
155 signal(SIGCHLD, sighandler);
156 signal(SIGTERM, sighandler);
157 signal(SIGUSR1, sighandler);
158 signal(SIGUSR2, sighandler);
160 if ((urandom_fd = open("/dev/urandom", O_CLOEXEC | O_RDONLY)) < 0 ||
161 init_dhcpv6(ifname, request_pd) || ra_init(ifname, &ifid) ||
162 script_init(script, ifname)) {
163 syslog(LOG_ERR, "failed to initialize: %s", strerror(errno));
168 openlog("odhcp6c", LOG_PID, LOG_DAEMON); // Disable LOG_PERROR
170 syslog(LOG_ERR, "Failed to daemonize: %s",
177 snprintf(pidbuf, sizeof(pidbuf),
178 "/var/run/odhcp6c.%s.pid", ifname);
182 int fd = open(pidfile, O_WRONLY | O_CREAT);
185 int len = snprintf(buf, sizeof(buf), "%i\n", getpid());
191 script_call("started");
193 while (do_signal != SIGTERM) { // Main logic
194 odhcp6c_clear_state(STATE_SERVER_ID);
195 odhcp6c_clear_state(STATE_IA_NA);
196 odhcp6c_clear_state(STATE_IA_PD);
197 odhcp6c_clear_state(STATE_SNTP_IP);
198 odhcp6c_clear_state(STATE_SNTP_FQDN);
199 odhcp6c_clear_state(STATE_SIP_IP);
200 odhcp6c_clear_state(STATE_SIP_FQDN);
201 dhcpv6_set_ia_na_mode(ia_na_mode);
204 // Server candidates need deep-delete
206 struct dhcpv6_server_cand *cand = odhcp6c_get_state(STATE_SERVER_CAND, &cand_len);
207 for (size_t i = 0; i < cand_len / sizeof(*cand); ++i) {
211 odhcp6c_clear_state(STATE_SERVER_CAND);
213 syslog(LOG_NOTICE, "(re)starting transaction on %s", ifname);
216 int res = dhcpv6_request(DHCPV6_MSG_SOLICIT);
217 odhcp6c_signal_process();
220 continue; // Might happen if we got a signal
221 } else if (res == DHCPV6_STATELESS) { // Stateless mode
222 while (do_signal == 0 || do_signal == SIGUSR1) {
225 res = dhcpv6_request(DHCPV6_MSG_INFO_REQ);
226 odhcp6c_signal_process();
227 if (do_signal == SIGUSR1)
232 script_call("informed");
235 syslog(LOG_NOTICE, "entering stateless-mode on %s", ifname);
237 if (dhcpv6_poll_reconfigure() > 0)
238 script_call("informed");
245 if (dhcpv6_request(DHCPV6_MSG_REQUEST) < 0)
248 odhcp6c_signal_process();
249 script_call("bound");
251 syslog(LOG_NOTICE, "entering stateful-mode on %s", ifname);
253 while (do_signal == 0 || do_signal == SIGUSR1) {
255 // Wait for T1 to expire or until we get a reconfigure
256 int res = dhcpv6_poll_reconfigure();
257 odhcp6c_signal_process();
260 script_call("updated");
265 // Handle signal, if necessary
266 if (do_signal == SIGUSR1)
267 do_signal = 0; // Acknowledged
268 else if (do_signal > 0)
269 break; // Other signal type
271 size_t ia_pd_len, ia_na_len, ia_pd_new, ia_na_new;
272 odhcp6c_get_state(STATE_IA_PD, &ia_pd_len);
273 odhcp6c_get_state(STATE_IA_NA, &ia_na_len);
275 // If we have any IAs, send renew, otherwise request
277 if (ia_pd_len == 0 && ia_na_len == 0)
278 r = dhcpv6_request(DHCPV6_MSG_REQUEST);
280 r = dhcpv6_request(DHCPV6_MSG_RENEW);
281 odhcp6c_signal_process();
282 if (r > 0) // Publish updates
283 script_call("updated");
285 continue; // Renew was successful
287 odhcp6c_clear_state(STATE_SERVER_ID); // Remove binding
289 // If we have IAs, try rebind otherwise restart
290 res = dhcpv6_request(DHCPV6_MSG_REBIND);
291 odhcp6c_signal_process();
293 odhcp6c_get_state(STATE_IA_PD, &ia_pd_new);
294 odhcp6c_get_state(STATE_IA_NA, &ia_na_new);
295 if (res < 0 || (ia_pd_new == 0 && ia_pd_len) ||
296 (ia_na_new == 0 && ia_na_len))
297 break; // We lost all our IAs, restart
299 script_call("rebound");
303 size_t ia_pd_len, ia_na_len, server_id_len;
304 odhcp6c_get_state(STATE_IA_PD, &ia_pd_len);
305 odhcp6c_get_state(STATE_IA_NA, &ia_na_len);
306 odhcp6c_get_state(STATE_SERVER_ID, &server_id_len);
308 // Add all prefixes to lost prefixes
310 script_call("unbound");
312 if (server_id_len > 0 && (ia_pd_len > 0 || ia_na_len > 0) && release)
313 dhcpv6_request(DHCPV6_MSG_RELEASE);
315 odhcp6c_clear_state(STATE_IA_NA);
316 odhcp6c_clear_state(STATE_IA_PD);
319 script_call("stopped");
324 static int usage(void)
327 "Usage: odhcp6c [options] <interface>\n"
328 "\nFeature options:\n"
329 " -S <time> Wait at least <time> sec for a DHCP-server (0)\n"
330 " -N <mode> Mode for requesting addresses [try|force|none]\n"
331 " -P <length> Request IPv6-Prefix (0 = auto)\n"
332 " -c <clientid> Override client-ID (base-16 encoded)\n"
333 " -i <iface-id> Use a custom interface identifier for RA handling\n"
334 " -r <options> Options to be requested (comma-separated)\n"
335 " -s <script> Status update script (/usr/sbin/odhcp6c-update)\n"
336 " -k Don't send a RELEASE when stopping\n"
337 "\nInvocation options:\n"
338 " -p <pidfile> Set pidfile (/var/run/6relayd.pid)\n"
340 " -e Write logmessages to stderr\n"
341 //" -v Increase logging verbosity\n"
342 " -h Show this help\n\n";
343 write(STDERR_FILENO, buf, sizeof(buf));
348 // Don't want to pull-in librt and libpthread just for a monotonic clock...
349 uint64_t odhcp6c_get_milli_time(void)
351 struct timespec t = {0, 0};
352 syscall(SYS_clock_gettime, CLOCK_MONOTONIC, &t);
353 return t.tv_sec * 1000 + t.tv_nsec / 1000000;
357 static uint8_t* odhcp6c_resize_state(enum odhcp6c_state state, ssize_t len)
360 return state_data[state] + state_len[state];
361 else if (state_len[state] + len > 1024)
364 uint8_t *n = realloc(state_data[state], state_len[state] + len);
365 if (n || state_len[state] + len == 0) {
366 state_data[state] = n;
367 n += state_len[state];
368 state_len[state] += len;
374 bool odhcp6c_signal_process(void)
376 if (do_signal == SIGIO) {
378 bool ra_rtnled = ra_rtnl_process();
379 bool ra_updated = ra_process();
381 if (ra_rtnled || (ra_updated && (bound || allow_slaac_only == 0)))
382 script_call("ra-updated"); // Immediate process urgent events
383 else if (ra_updated && !bound && allow_slaac_only > 0)
384 script_delay_call("ra-updated", allow_slaac_only);
387 return do_signal != 0;
391 void odhcp6c_clear_state(enum odhcp6c_state state)
393 state_len[state] = 0;
397 void odhcp6c_add_state(enum odhcp6c_state state, const void *data, size_t len)
399 uint8_t *n = odhcp6c_resize_state(state, len);
401 memcpy(n, data, len);
405 size_t odhcp6c_remove_state(enum odhcp6c_state state, size_t offset, size_t len)
407 uint8_t *data = state_data[state];
408 ssize_t len_after = state_len[state] - (offset + len);
410 return state_len[state];
412 memmove(data + offset, data + offset + len, len_after);
413 return state_len[state] -= len;
417 void* odhcp6c_move_state(enum odhcp6c_state state, size_t *len)
419 *len = state_len[state];
420 void *data = state_data[state];
422 state_len[state] = 0;
423 state_data[state] = NULL;
429 void* odhcp6c_get_state(enum odhcp6c_state state, size_t *len)
431 *len = state_len[state];
432 return state_data[state];
436 struct odhcp6c_entry* odhcp6c_find_entry(enum odhcp6c_state state, const struct odhcp6c_entry *new)
438 size_t len, cmplen = offsetof(struct odhcp6c_entry, target) + new->length / 8;
439 struct odhcp6c_entry *start = odhcp6c_get_state(state, &len);
440 struct odhcp6c_entry *x = NULL;
442 for (struct odhcp6c_entry *c = start; !x && c < &start[len/sizeof(*c)]; ++c)
443 if (!memcmp(c, new, cmplen))
450 void odhcp6c_update_entry_safe(enum odhcp6c_state state, struct odhcp6c_entry *new, uint32_t safe)
453 struct odhcp6c_entry *x = odhcp6c_find_entry(state, new);
454 struct odhcp6c_entry *start = odhcp6c_get_state(state, &len);
456 if (x && x->valid > new->valid && new->valid < safe)
459 if (new->valid > 0) {
461 x->valid = new->valid;
462 x->preferred = new->preferred;
463 x->prefix_class = new->prefix_class;
465 odhcp6c_add_state(state, new, sizeof(*new));
468 odhcp6c_remove_state(state, (x - start) * sizeof(*x), sizeof(*x));
473 void odhcp6c_update_entry(enum odhcp6c_state state, struct odhcp6c_entry *new)
475 odhcp6c_update_entry_safe(state, new, 0);
479 static void odhcp6c_expire_list(enum odhcp6c_state state, uint32_t elapsed)
482 struct odhcp6c_entry *start = odhcp6c_get_state(state, &len);
483 for (struct odhcp6c_entry *c = start; c < &start[len / sizeof(*c)]; ++c) {
484 if (c->preferred < elapsed)
486 else if (c->preferred != UINT32_MAX)
487 c->preferred -= elapsed;
489 if (c->valid < elapsed)
491 else if (c->valid != UINT32_MAX)
495 odhcp6c_remove_state(state, (c - start) * sizeof(*c), sizeof(*c));
500 void odhcp6c_expire(void)
502 time_t now = odhcp6c_get_milli_time() / 1000;
503 uint32_t elapsed = (last_update > 0) ? now - last_update : 0;
506 odhcp6c_expire_list(STATE_RA_PREFIX, elapsed);
507 odhcp6c_expire_list(STATE_RA_ROUTE, elapsed);
508 odhcp6c_expire_list(STATE_RA_DNS, elapsed);
509 odhcp6c_expire_list(STATE_IA_NA, elapsed);
510 odhcp6c_expire_list(STATE_IA_PD, elapsed);
514 uint32_t odhcp6c_elapsed(void)
516 return odhcp6c_get_milli_time() / 1000 - last_update;
520 void odhcp6c_random(void *buf, size_t len)
522 read(urandom_fd, buf, len);
526 static void sighandler(int signal)
528 if (signal == SIGCHLD)
529 while (waitpid(-1, NULL, WNOHANG) > 0);
530 else if (signal == SIGUSR1)
532 else if (signal == SIGUSR2)
534 else if (signal == SIGIO)