Set a global default DHCPv6 exchange timeout
[project/odhcp6c.git] / src / odhcp6c.c
1 /**
2 * Copyright (C) 2012-2013 Steven Barth <steven@midlink.org>
3 *
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.
7 *
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.
12 *
13 */
14
15 #include <time.h>
16 #include <errno.h>
17 #include <fcntl.h>
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <stddef.h>
21 #include <unistd.h>
22 #include <syslog.h>
23 #include <signal.h>
24 #include <string.h>
25 #include <stdbool.h>
26
27 #include <net/if.h>
28 #include <sys/wait.h>
29 #include <sys/syscall.h>
30
31 #include "odhcp6c.h"
32 #include "ra.h"
33
34
35 static void sighandler(int signal);
36 static int usage(void);
37
38
39 static uint8_t *state_data[_STATE_MAX] = {NULL};
40 static size_t state_len[_STATE_MAX] = {0};
41
42 static volatile int do_signal = 0;
43 static int urandom_fd = -1, allow_slaac_only = 15;
44 static bool bound = false, release = true;
45
46
47 int main(_unused int argc, char* const argv[])
48 {
49 // Allocate ressources
50 const char *pidfile = NULL;
51 const char *script = "/usr/sbin/odhcp6c-update";
52 ssize_t l;
53 uint8_t buf[134];
54 char *optpos;
55 uint16_t opttype;
56 enum odhcp6c_ia_mode ia_na_mode = IA_MODE_TRY;
57
58 bool help = false, daemonize = false;
59 int logopt = LOG_PID;
60 int c, request_pd = 0;
61 while ((c = getopt(argc, argv, "S::N:P:c:r:s:khedp:")) != -1) {
62 switch (c) {
63 case 'S':
64 allow_slaac_only = (optarg) ? atoi(optarg) : -1;
65 break;
66
67 case 'N':
68 if (!strcmp(optarg, "force"))
69 ia_na_mode = IA_MODE_FORCE;
70 else if (!strcmp(optarg, "none"))
71 ia_na_mode = IA_MODE_NONE;
72 else if (!strcmp(optarg, "try"))
73 ia_na_mode = IA_MODE_TRY;
74 else
75 help = true;
76 break;
77
78 case 'P':
79 request_pd = strtoul(optarg, NULL, 10);
80 if (request_pd == 0)
81 request_pd = -1;
82 break;
83
84 case 'c':
85 l = script_unhexlify(&buf[4], sizeof(buf) - 4, optarg);
86 if (l > 0) {
87 buf[0] = 0;
88 buf[1] = DHCPV6_OPT_CLIENTID;
89 buf[2] = 0;
90 buf[3] = l;
91 odhcp6c_add_state(STATE_CLIENT_ID, buf, l + 4);
92 } else {
93 help = true;
94 }
95 break;
96
97 case 'r':
98 optpos = optarg;
99 while (optpos[0]) {
100 opttype = htons(strtoul(optarg, &optpos, 10));
101 if (optpos == optarg)
102 break;
103 else if (optpos[0])
104 optarg = &optpos[1];
105 odhcp6c_add_state(STATE_ORO, &opttype, 2);
106 }
107 break;
108
109 case 's':
110 script = optarg;
111 break;
112
113 case 'k':
114 release = false;
115 break;
116
117 case 'e':
118 logopt |= LOG_PERROR;
119 break;
120
121 case 'd':
122 daemonize = true;
123 break;
124
125 case 'p':
126 pidfile = optarg;
127 break;
128
129 default:
130 help = true;
131 break;
132 }
133 }
134
135 openlog("odhcp6c", logopt, LOG_DAEMON);
136 const char *ifname = argv[optind];
137
138 if (help || !ifname)
139 return usage();
140
141 signal(SIGIO, sighandler);
142 signal(SIGHUP, sighandler);
143 signal(SIGINT, sighandler);
144 signal(SIGCHLD, sighandler);
145 signal(SIGTERM, sighandler);
146 signal(SIGUSR1, sighandler);
147 signal(SIGUSR2, sighandler);
148
149 if ((urandom_fd = open("/dev/urandom", O_CLOEXEC | O_RDONLY)) < 0 ||
150 init_dhcpv6(ifname, request_pd) || ra_init(ifname) ||
151 script_init(script, ifname)) {
152 syslog(LOG_ERR, "failed to initialize: %s", strerror(errno));
153 return 3;
154 }
155
156 if (daemonize) {
157 openlog("odhcp6c", LOG_PID, LOG_DAEMON); // Disable LOG_PERROR
158 if (daemon(0, 0)) {
159 syslog(LOG_ERR, "Failed to daemonize: %s",
160 strerror(errno));
161 return 4;
162 }
163
164 char pidbuf[128];
165 if (!pidfile) {
166 snprintf(pidbuf, sizeof(pidbuf),
167 "/var/run/odhcp6c.%s.pid", ifname);
168 pidfile = pidbuf;
169 }
170
171 int fd = open(pidfile, O_WRONLY | O_CREAT);
172 if (fd >= 0) {
173 char buf[8];
174 int len = snprintf(buf, sizeof(buf), "%i\n", getpid());
175 write(fd, buf, len);
176 close(fd);
177 }
178 }
179
180 script_call("started");
181
182 while (do_signal != SIGTERM) { // Main logic
183 odhcp6c_clear_state(STATE_SERVER_ID);
184 odhcp6c_clear_state(STATE_SERVER_CAND);
185 odhcp6c_clear_state(STATE_IA_PD);
186 odhcp6c_clear_state(STATE_SNTP_IP);
187 odhcp6c_clear_state(STATE_SNTP_FQDN);
188 odhcp6c_clear_state(STATE_SIP_IP);
189 odhcp6c_clear_state(STATE_SIP_FQDN);
190 dhcpv6_set_ia_na_mode(ia_na_mode);
191 bound = false;
192
193 syslog(LOG_NOTICE, "(re)starting transaction on %s", ifname);
194
195 do_signal = 0;
196 int res = dhcpv6_request(DHCPV6_MSG_SOLICIT);
197 odhcp6c_signal_process();
198
199 if (res < 0) {
200 continue; // Might happen if we got a signal
201 } else if (res == DHCPV6_STATELESS) { // Stateless mode
202 while (do_signal == 0 || do_signal == SIGUSR1) {
203 do_signal = 0;
204
205 res = dhcpv6_request(DHCPV6_MSG_INFO_REQ);
206 odhcp6c_signal_process();
207 if (do_signal == SIGUSR1)
208 continue;
209 else if (res < 0)
210 break;
211 else if (res > 0)
212 script_call("informed");
213
214 bound = true;
215 syslog(LOG_NOTICE, "entering stateless-mode on %s", ifname);
216
217 if (dhcpv6_poll_reconfigure() > 0)
218 script_call("informed");
219 }
220
221 continue;
222 }
223
224 // Stateful mode
225 if (dhcpv6_request(DHCPV6_MSG_REQUEST) < 0)
226 continue;
227
228 odhcp6c_signal_process();
229 script_call("bound");
230 bound = true;
231 syslog(LOG_NOTICE, "entering stateful-mode on %s", ifname);
232
233 while (do_signal == 0 || do_signal == SIGUSR1) {
234 // Renew Cycle
235 // Wait for T1 to expire or until we get a reconfigure
236 int res = dhcpv6_poll_reconfigure();
237 odhcp6c_signal_process();
238 if (res >= 0) {
239 if (res > 0)
240 script_call("updated");
241
242 continue;
243 }
244
245 // Handle signal, if necessary
246 if (do_signal == SIGUSR1)
247 do_signal = 0; // Acknowledged
248 else if (do_signal > 0)
249 break; // Other signal type
250
251 size_t ia_pd_len, ia_na_len, ia_pd_new, ia_na_new;
252 odhcp6c_get_state(STATE_IA_PD, &ia_pd_len);
253 odhcp6c_get_state(STATE_IA_NA, &ia_na_len);
254
255 // If we have any IAs, send renew, otherwise request
256 int r;
257 if (ia_pd_len == 0 && ia_na_len == 0)
258 r = dhcpv6_request(DHCPV6_MSG_REQUEST);
259 else
260 r = dhcpv6_request(DHCPV6_MSG_RENEW);
261 odhcp6c_signal_process();
262 if (r > 0) // Publish updates
263 script_call("updated");
264 if (r >= 0)
265 continue; // Renew was successful
266
267 odhcp6c_clear_state(STATE_SERVER_ID); // Remove binding
268
269 // If we have IAs, try rebind otherwise restart
270 res = dhcpv6_request(DHCPV6_MSG_REBIND);
271 odhcp6c_signal_process();
272
273 odhcp6c_get_state(STATE_IA_PD, &ia_pd_new);
274 odhcp6c_get_state(STATE_IA_NA, &ia_na_new);
275 if (res < 0 || (ia_pd_new == 0 && ia_pd_len) ||
276 (ia_na_new == 0 && ia_na_len))
277 break; // We lost all our IAs, restart
278 else if (res > 0)
279 script_call("rebound");
280 }
281
282
283 size_t ia_pd_len, ia_na_len, server_id_len;
284 odhcp6c_get_state(STATE_IA_PD, &ia_pd_len);
285 odhcp6c_get_state(STATE_IA_NA, &ia_na_len);
286 odhcp6c_get_state(STATE_SERVER_ID, &server_id_len);
287
288 // Add all prefixes to lost prefixes
289 bound = false;
290 script_call("unbound");
291
292 if (server_id_len > 0 && (ia_pd_len > 0 || ia_na_len > 0) && release)
293 dhcpv6_request(DHCPV6_MSG_RELEASE);
294
295 odhcp6c_clear_state(STATE_IA_NA);
296 odhcp6c_clear_state(STATE_IA_PD);
297 }
298
299 script_call("stopped");
300 return 0;
301 }
302
303
304 static int usage(void)
305 {
306 const char buf[] =
307 "Usage: odhcp6c [options] <interface>\n"
308 "\nFeature options:\n"
309 " -S <time> Wait at least <time> sec for a DHCP-server (15)\n"
310 " -N <mode> Mode for requesting addresses [try|force|none]\n"
311 " -P <length> Request IPv6-Prefix (0 = auto)\n"
312 " -c <clientid> Override client-ID (base-16 encoded)\n"
313 " -r <options> Options to be requested (comma-separated)\n"
314 " -s <script> Status update script (/usr/sbin/odhcp6c-update)\n"
315 " -k Don't send a RELEASE when stopping\n"
316 "\nInvocation options:\n"
317 " -p <pidfile> Set pidfile (/var/run/6relayd.pid)\n"
318 " -d Daemonize\n"
319 " -e Write logmessages to stderr\n"
320 //" -v Increase logging verbosity\n"
321 " -h Show this help\n\n";
322 write(STDERR_FILENO, buf, sizeof(buf));
323 return 1;
324 }
325
326
327 // Don't want to pull-in librt and libpthread just for a monotonic clock...
328 uint64_t odhcp6c_get_milli_time(void)
329 {
330 struct timespec t = {0, 0};
331 syscall(SYS_clock_gettime, CLOCK_MONOTONIC, &t);
332 return t.tv_sec * 1000 + t.tv_nsec / 1000000;
333 }
334
335
336 static uint8_t* odhcp6c_resize_state(enum odhcp6c_state state, ssize_t len)
337 {
338 if (len == 0)
339 return state_data[state] + state_len[state];
340 else if (state_len[state] + len > 1024)
341 return NULL;
342
343 uint8_t *n = realloc(state_data[state], state_len[state] + len);
344 if (n || state_len[state] + len == 0) {
345 state_data[state] = n;
346 n += state_len[state];
347 state_len[state] += len;
348 }
349 return n;
350 }
351
352
353 bool odhcp6c_signal_process(void)
354 {
355 if (do_signal == SIGIO) {
356 do_signal = 0;
357 bool ra_updated = ra_process();
358
359 if (ra_rtnl_process() || (ra_updated && (bound || allow_slaac_only == 0)))
360 script_call("ra-updated"); // Immediate process urgent events
361 else if (ra_updated && !bound && allow_slaac_only > 0)
362 script_delay_call("ra-updated", allow_slaac_only);
363 }
364
365 return do_signal != 0;
366 }
367
368
369 void odhcp6c_clear_state(enum odhcp6c_state state)
370 {
371 state_len[state] = 0;
372 }
373
374
375 void odhcp6c_add_state(enum odhcp6c_state state, const void *data, size_t len)
376 {
377 uint8_t *n = odhcp6c_resize_state(state, len);
378 if (n)
379 memcpy(n, data, len);
380 }
381
382
383 size_t odhcp6c_remove_state(enum odhcp6c_state state, size_t offset, size_t len)
384 {
385 uint8_t *data = state_data[state];
386 ssize_t len_after = state_len[state] - (offset + len);
387 if (len_after < 0)
388 return state_len[state];
389
390 memmove(data + offset, data + offset + len, len_after);
391 return state_len[state] -= len;
392 }
393
394
395 void* odhcp6c_get_state(enum odhcp6c_state state, size_t *len)
396 {
397 *len = state_len[state];
398 return state_data[state];
399 }
400
401
402 struct odhcp6c_entry* odhcp6c_find_entry(enum odhcp6c_state state, const struct odhcp6c_entry *new)
403 {
404 size_t len, cmplen = offsetof(struct odhcp6c_entry, target) + new->length / 8;
405 struct odhcp6c_entry *start = odhcp6c_get_state(state, &len);
406 struct odhcp6c_entry *x = NULL;
407
408 for (struct odhcp6c_entry *c = start; !x && c < &start[len/sizeof(*c)]; ++c)
409 if (!memcmp(c, new, cmplen))
410 return c;
411
412 return NULL;
413 }
414
415
416 void odhcp6c_update_entry_safe(enum odhcp6c_state state, struct odhcp6c_entry *new, uint32_t safe)
417 {
418 size_t len;
419 struct odhcp6c_entry *x = odhcp6c_find_entry(state, new);
420 struct odhcp6c_entry *start = odhcp6c_get_state(state, &len);
421
422 if (x && x->valid > new->valid && new->valid < safe)
423 new->valid = safe;
424
425 if (new->valid > 0) {
426 if (x) {
427 x->valid = new->valid;
428 x->preferred = new->preferred;
429 } else {
430 odhcp6c_add_state(state, new, sizeof(*new));
431 }
432 } else if (x) {
433 odhcp6c_remove_state(state, (x - start) * sizeof(*x), sizeof(*x));
434 }
435 }
436
437
438 void odhcp6c_update_entry(enum odhcp6c_state state, struct odhcp6c_entry *new)
439 {
440 odhcp6c_update_entry_safe(state, new, 0);
441 }
442
443
444 static void odhcp6c_expire_list(enum odhcp6c_state state, uint32_t elapsed)
445 {
446 size_t len;
447 struct odhcp6c_entry *start = odhcp6c_get_state(state, &len);
448 for (struct odhcp6c_entry *c = start; c < &start[len / sizeof(*c)]; ++c) {
449 if (c->preferred < elapsed)
450 c->preferred = 0;
451 else if (c->preferred != UINT32_MAX)
452 c->preferred -= elapsed;
453
454 if (c->valid < elapsed)
455 c->valid = 0;
456 else if (c->valid != UINT32_MAX)
457 c->valid -= elapsed;
458
459 if (!c->valid)
460 odhcp6c_remove_state(state, (c - start) * sizeof(*c), sizeof(*c));
461 }
462 }
463
464
465 void odhcp6c_expire(void)
466 {
467 static time_t last_update = 0;
468 time_t now = odhcp6c_get_milli_time() / 1000;
469
470 uint32_t elapsed = now - last_update;
471 last_update = now;
472
473 odhcp6c_expire_list(STATE_RA_PREFIX, elapsed);
474 odhcp6c_expire_list(STATE_RA_ROUTE, elapsed);
475 odhcp6c_expire_list(STATE_RA_DNS, elapsed);
476 odhcp6c_expire_list(STATE_IA_NA, elapsed);
477 odhcp6c_expire_list(STATE_IA_PD, elapsed);
478 }
479
480
481 void odhcp6c_random(void *buf, size_t len)
482 {
483 read(urandom_fd, buf, len);
484 }
485
486
487 static void sighandler(int signal)
488 {
489 if (signal == SIGCHLD)
490 while (waitpid(-1, NULL, WNOHANG) > 0);
491 else if (signal == SIGUSR1)
492 do_signal = SIGUSR1;
493 else if (signal == SIGUSR2)
494 do_signal = SIGUSR2;
495 else if (signal == SIGIO)
496 do_signal = SIGIO;
497 else
498 do_signal = SIGTERM;
499 }