2 * Copyright (C) 2012 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.
28 #include <sys/syscall.h>
33 static void sighandler(int signal);
34 static int usage(void);
37 static uint8_t *state_data[_STATE_MAX] = {NULL};
38 static size_t state_len[_STATE_MAX] = {0};
40 static volatile int do_signal = 0;
43 int main(_unused int argc, char* const argv[])
45 openlog("odhcp6c", LOG_PERROR | LOG_PID, LOG_DAEMON);
47 // Allocate ressources
48 const char *pidfile = NULL;
49 const char *script = "/usr/sbin/odhcp6c-update";
54 enum odhcp6c_ia_mode ia_na_mode = IA_MODE_TRY;
56 bool help = false, daemonize = false;
57 int c, request_pd = 0;
58 while ((c = getopt(argc, argv, "N:P:c:r:s:hdp:")) != -1) {
61 if (!strcmp(optarg, "force"))
62 ia_na_mode = IA_MODE_FORCE;
63 else if (!strcmp(optarg, "none"))
64 ia_na_mode = IA_MODE_NONE;
65 else if (!strcmp(optarg, "try"))
66 ia_na_mode = IA_MODE_TRY;
72 request_pd = strtoul(optarg, NULL, 10);
78 l = script_unhexlify(&buf[4], sizeof(buf) - 4, optarg);
81 buf[1] = DHCPV6_OPT_CLIENTID;
84 odhcp6c_add_state(STATE_CLIENT_ID, buf, l + 4);
93 opttype = htons(strtoul(optarg, &optpos, 10));
98 odhcp6c_add_state(STATE_ORO, &opttype, 2);
120 const char *ifname = argv[optind];
125 if (init_dhcpv6(ifname, request_pd) || init_rtnetlink() ||
126 script_init(script, ifname)) {
127 syslog(LOG_ERR, "failed to initialize: %s", strerror(errno));
131 signal(SIGHUP, sighandler);
132 signal(SIGINT, sighandler);
133 signal(SIGCHLD, sighandler);
134 signal(SIGTERM, sighandler);
135 signal(SIGUSR1, sighandler);
136 signal(SIGUSR2, sighandler);
139 openlog("odhcp6c", LOG_PID, LOG_DAEMON); // Disable LOG_PERROR
141 syslog(LOG_ERR, "Failed to daemonize: %s",
148 snprintf(pidbuf, sizeof(pidbuf),
149 "/var/run/odhcp6c.%s.pid", ifname);
153 int fd = open(pidfile, O_WRONLY | O_CREAT);
156 int len = snprintf(buf, sizeof(buf), "%i\n", getpid());
162 script_call("started");
164 while (do_signal != SIGTERM) { // Main logic
165 odhcp6c_clear_state(STATE_SERVER_ID);
166 odhcp6c_clear_state(STATE_SERVER_CAND);
167 odhcp6c_clear_state(STATE_IA_PD);
168 odhcp6c_clear_state(STATE_SNTP_IP);
169 odhcp6c_clear_state(STATE_SNTP_FQDN);
170 odhcp6c_clear_state(STATE_SIP_IP);
171 odhcp6c_clear_state(STATE_SIP_FQDN);
172 dhcpv6_set_ia_na_mode(ia_na_mode);
175 int res = dhcpv6_request(DHCPV6_MSG_SOLICIT);
178 continue; // Might happen if we got a signal
179 } else if (res == DHCPV6_STATELESS) { // Stateless mode
180 while (do_signal == 0 || do_signal == SIGUSR1) {
183 res = dhcpv6_request(DHCPV6_MSG_INFO_REQ);
184 if (do_signal == SIGUSR1)
189 script_call("informed");
191 if (dhcpv6_poll_reconfigure() > 0)
192 script_call("informed");
199 if (dhcpv6_request(DHCPV6_MSG_REQUEST) < 0)
202 script_call("bound");
204 while (do_signal == 0 || do_signal == SIGUSR1) {
206 // Wait for T1 to expire or until we get a reconfigure
207 int res = dhcpv6_poll_reconfigure();
210 script_call("updated");
215 // Handle signal, if necessary
216 if (do_signal == SIGUSR1)
217 do_signal = 0; // Acknowledged
218 else if (do_signal > 0)
219 break; // Other signal type
221 size_t ia_pd_len, ia_na_len, ia_pd_new, ia_na_new;
222 odhcp6c_get_state(STATE_IA_PD, &ia_pd_len);
223 odhcp6c_get_state(STATE_IA_NA, &ia_na_len);
225 // If we have any IAs, send renew, otherwise request
227 if (ia_pd_len == 0 && ia_na_len == 0)
228 r = dhcpv6_request(DHCPV6_MSG_REQUEST);
230 r = dhcpv6_request(DHCPV6_MSG_RENEW);
231 if (r > 0) // Publish updates
232 script_call("updated");
234 continue; // Renew was successful
236 odhcp6c_clear_state(STATE_SERVER_ID); // Remove binding
238 // If we have IAs, try rebind otherwise restart
239 res = dhcpv6_request(DHCPV6_MSG_REBIND);
241 odhcp6c_get_state(STATE_IA_PD, &ia_pd_new);
242 odhcp6c_get_state(STATE_IA_NA, &ia_na_new);
243 if (res < 0 || (ia_pd_new == 0 && ia_pd_len) ||
244 (ia_na_new == 0 && ia_na_len))
245 break; // We lost all our IAs, restart
247 script_call("rebound");
251 size_t ia_pd_len, ia_na_len, server_id_len;
252 odhcp6c_get_state(STATE_IA_PD, &ia_pd_len);
253 odhcp6c_get_state(STATE_IA_NA, &ia_na_len);
254 odhcp6c_get_state(STATE_SERVER_ID, &server_id_len);
256 // Add all prefixes to lost prefixes
257 odhcp6c_clear_state(STATE_IA_PD);
258 script_call("unbound");
260 // Remove assigned addresses
262 dhcpv6_remove_addrs();
264 if (server_id_len > 0 && (ia_pd_len > 0 || ia_na_len > 0))
265 dhcpv6_request(DHCPV6_MSG_RELEASE);
268 script_call("stopped");
273 static int usage(void)
276 "Usage: odhcp6c [options] <interface>\n"
277 "\nFeature options:\n"
278 " -N <mode> Mode for requesting addresses [try|force|none]\n"
279 " -P <length> Request IPv6-Prefix (0 = auto)\n"
280 " -c <clientid> Override client-ID (base-16 encoded)\n"
281 " -r <options> Options to be requested (comma-separated)\n"
282 " -s <script> Status update script (/usr/sbin/odhcp6c-update)\n"
283 "\nInvocation options:\n"
284 " -p <pidfile> Set pidfile (/var/run/6relayd.pid)\n"
286 //" -v Increase logging verbosity\n"
287 " -h Show this help\n\n";
288 write(STDERR_FILENO, buf, sizeof(buf));
293 // Don't want to pull-in librt and libpthread just for a monotonic clock...
294 uint64_t odhcp6c_get_milli_time(void)
296 struct timespec t = {0, 0};
297 syscall(SYS_clock_gettime, CLOCK_MONOTONIC, &t);
298 return t.tv_sec * 1000 + t.tv_nsec / 1000000;
302 static uint8_t* odhcp6c_resize_state(enum odhcp6c_state state, ssize_t len)
305 return state_data[state] + state_len[state];
307 uint8_t *n = realloc(state_data[state], state_len[state] + len);
308 if (n || state_len[state] + len == 0) {
309 state_data[state] = n;
310 n += state_len[state];
311 state_len[state] += len;
317 bool odhcp6c_signal_is_pending(void)
319 return do_signal != 0;
323 void odhcp6c_clear_state(enum odhcp6c_state state)
325 state_len[state] = 0;
329 void odhcp6c_add_state(enum odhcp6c_state state, const void *data, size_t len)
331 uint8_t *n = odhcp6c_resize_state(state, len);
333 memcpy(n, data, len);
337 size_t odhcp6c_remove_state(enum odhcp6c_state state, size_t offset, size_t len)
339 uint8_t *data = state_data[state];
340 ssize_t len_after = state_len[state] - (offset + len);
342 return state_len[state];
344 memmove(data + offset, data + offset + len, len_after);
345 return state_len[state] -= len;
349 bool odhcp6c_commit_state(enum odhcp6c_state state, size_t old_len)
351 size_t new_len = state_len[state] - old_len;
352 uint8_t *old_data = state_data[state], *new_data = old_data + old_len;
353 bool upd = new_len != old_len || memcmp(old_data, new_data, new_len);
355 memmove(old_data, new_data, new_len);
356 odhcp6c_resize_state(state, -old_len);
362 void* odhcp6c_get_state(enum odhcp6c_state state, size_t *len)
364 *len = state_len[state];
365 return state_data[state];
369 static void sighandler(int signal)
371 if (signal == SIGCHLD)
372 while (waitpid(-1, NULL, WNOHANG) > 0);
373 else if (signal == SIGUSR1)
375 else if (signal == SIGUSR2)