added command line switch for strict oro
[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 #include <arpa/inet.h>
31
32 #include "odhcp6c.h"
33 #include "ra.h"
34
35 #ifdef EXT_BFD_PING
36 #include "bfd.h"
37 #endif
38
39
40 static void sighandler(int signal);
41 static int usage(void);
42
43 static uint8_t *state_data[_STATE_MAX] = {NULL};
44 static size_t state_len[_STATE_MAX] = {0};
45
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;
50
51 static int urandom_fd = -1, allow_slaac_only = 0;
52 static bool bound = false, release = true;
53 static time_t last_update = 0;
54
55
56 int main(_unused int argc, char* const argv[])
57 {
58 // Allocate ressources
59 const char *pidfile = NULL;
60 const char *script = "/usr/sbin/odhcp6c-update";
61 ssize_t l;
62 uint8_t buf[134];
63 char *optpos;
64 uint16_t opttype;
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;
69
70 #ifdef EXT_BFD_PING
71 int bfd_interval = 0, bfd_loss = 3;
72 #endif
73
74 bool help = false, daemonize = false, strict_options = false;
75 int logopt = LOG_PID;
76 int c, request_pd = 0;
77 while ((c = getopt(argc, argv, "S::N:P:FB:c:i:r:Rs:kt:hedp:")) != -1) {
78 switch (c) {
79 case 'S':
80 allow_slaac_only = (optarg) ? atoi(optarg) : -1;
81 break;
82
83 case 'N':
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;
91 } else{
92 help = true;
93 }
94 break;
95
96 case 'P':
97 if (allow_slaac_only >= 0 && allow_slaac_only < 10)
98 allow_slaac_only = 10;
99
100 request_pd = strtoul(optarg, NULL, 10);
101 if (request_pd == 0)
102 request_pd = -1;
103
104 break;
105
106 case 'F':
107 allow_slaac_only = -1;
108 ia_pd_mode = IA_MODE_FORCE;
109 break;
110
111 #ifdef EXT_BFD_PING
112 case 'B':
113 bfd_interval = atoi(optarg);
114 break;
115 #endif
116
117 case 'c':
118 l = script_unhexlify(&buf[4], sizeof(buf) - 4, optarg);
119 if (l > 0) {
120 buf[0] = 0;
121 buf[1] = DHCPV6_OPT_CLIENTID;
122 buf[2] = 0;
123 buf[3] = l;
124 odhcp6c_add_state(STATE_CLIENT_ID, buf, l + 4);
125 } else {
126 help = true;
127 }
128 break;
129
130 case 'i':
131 if (inet_pton(AF_INET6, optarg, &ifid) != 1)
132 help = true;
133 break;
134
135 case 'r':
136 optpos = optarg;
137 while (optpos[0]) {
138 opttype = htons(strtoul(optarg, &optpos, 10));
139 if (optpos == optarg)
140 break;
141 else if (optpos[0])
142 optarg = &optpos[1];
143 odhcp6c_add_state(STATE_ORO, &opttype, 2);
144 }
145 break;
146
147 case 'R':
148 strict_options = true;
149 break;
150
151 case 's':
152 script = optarg;
153 break;
154
155 case 'k':
156 release = false;
157 break;
158
159 case 't':
160 sol_timeout = atoi(optarg);
161 break;
162
163 case 'e':
164 logopt |= LOG_PERROR;
165 break;
166
167 case 'd':
168 daemonize = true;
169 break;
170
171 case 'p':
172 pidfile = optarg;
173 break;
174
175 default:
176 help = true;
177 break;
178 }
179 }
180
181 openlog("odhcp6c", logopt, LOG_DAEMON);
182 const char *ifname = argv[optind];
183
184 if (help || !ifname)
185 return usage();
186
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);
194
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));
199 return 3;
200 }
201
202 if (daemonize) {
203 openlog("odhcp6c", LOG_PID, LOG_DAEMON); // Disable LOG_PERROR
204 if (daemon(0, 0)) {
205 syslog(LOG_ERR, "Failed to daemonize: %s",
206 strerror(errno));
207 return 4;
208 }
209
210 char pidbuf[128];
211 if (!pidfile) {
212 snprintf(pidbuf, sizeof(pidbuf),
213 "/var/run/odhcp6c.%s.pid", ifname);
214 pidfile = pidbuf;
215 }
216
217 int fd = open(pidfile, O_WRONLY | O_CREAT);
218 if (fd >= 0) {
219 char buf[8];
220 int len = snprintf(buf, sizeof(buf), "%i\n", getpid());
221 write(fd, buf, len);
222 close(fd);
223 }
224 }
225
226 script_call("started");
227
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);
238 bound = false;
239
240 syslog(LOG_NOTICE, "(re)starting transaction on %s", ifname);
241
242 signal_usr1 = signal_usr2 = false;
243 int mode = dhcpv6_request(DHCPV6_MSG_SOLICIT);
244 odhcp6c_signal_process();
245
246 if (mode < 0)
247 continue;
248
249 do {
250 int res = dhcpv6_request(mode == DHCPV6_STATELESS ?
251 DHCPV6_MSG_INFO_REQ : DHCPV6_MSG_REQUEST);
252 bool signalled = odhcp6c_signal_process();
253
254 if (res > 0)
255 break;
256 else if (signalled) {
257 mode = -1;
258 break;
259 }
260
261 mode = dhcpv6_promote_server_cand();
262 } while (mode > DHCPV6_UNKNOWN);
263
264 if (mode < 0)
265 continue;
266
267 switch (mode) {
268 case DHCPV6_STATELESS:
269 bound = true;
270 syslog(LOG_NOTICE, "entering stateless-mode on %s", ifname);
271
272 while (!signal_usr2 && !signal_term) {
273 signal_usr1 = false;
274 script_call("informed");
275
276 int res = dhcpv6_poll_reconfigure();
277 odhcp6c_signal_process();
278
279 if (res > 0)
280 continue;
281
282 if (signal_usr1) {
283 signal_usr1 = false; // Acknowledged
284 continue;
285 }
286 if (signal_usr2 || signal_term)
287 break;
288
289 res = dhcpv6_request(DHCPV6_MSG_INFO_REQ);
290 odhcp6c_signal_process();
291 if (signal_usr1)
292 continue;
293 else if (res < 0)
294 break;
295 }
296 break;
297
298 case DHCPV6_STATEFUL:
299 script_call("bound");
300 bound = true;
301 syslog(LOG_NOTICE, "entering stateful-mode on %s", ifname);
302 #ifdef EXT_BFD_PING
303 if (bfd_interval > 0)
304 bfd_start(ifname, bfd_loss, bfd_interval);
305 #endif
306
307 while (!signal_usr2 && !signal_term) {
308 // Renew Cycle
309 // Wait for T1 to expire or until we get a reconfigure
310 int res = dhcpv6_poll_reconfigure();
311 odhcp6c_signal_process();
312 if (res > 0) {
313 script_call("updated");
314 continue;
315 }
316
317 // Handle signal, if necessary
318 if (signal_usr1)
319 signal_usr1 = false; // Acknowledged
320 if (signal_usr2 || signal_term)
321 break; // Other signal type
322
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);
327
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);
331 else
332 res = dhcpv6_request(DHCPV6_MSG_RENEW);
333
334 odhcp6c_signal_process();
335 if (res > 0) { // Renew was succesfull
336 // Publish updates
337 script_call("updated");
338 continue; // Renew was successful
339 }
340
341 odhcp6c_clear_state(STATE_SERVER_ID); // Remove binding
342
343 // If we have IAs, try rebind otherwise restart
344 res = dhcpv6_request(DHCPV6_MSG_REBIND);
345 odhcp6c_signal_process();
346
347 if (res > 0)
348 script_call("rebound");
349 else {
350 #ifdef EXT_BFD_PING
351 bfd_stop();
352 #endif
353 break;
354 }
355 }
356 break;
357
358 default:
359 break;
360 }
361
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);
366
367 // Add all prefixes to lost prefixes
368 bound = false;
369 script_call("unbound");
370
371 if (server_id_len > 0 && (ia_pd_len > 0 || ia_na_len > 0) && release)
372 dhcpv6_request(DHCPV6_MSG_RELEASE);
373
374 odhcp6c_clear_state(STATE_IA_NA);
375 odhcp6c_clear_state(STATE_IA_PD);
376 }
377
378 script_call("stopped");
379 return 0;
380 }
381
382
383 static int usage(void)
384 {
385 const char buf[] =
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"
392 #ifdef EXT_BFD_PING
393 " -B <interval> Enable BFD ping check\n"
394 #endif
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"
404 " -d Daemonize\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));
409 return 1;
410 }
411
412
413 // Don't want to pull-in librt and libpthread just for a monotonic clock...
414 uint64_t odhcp6c_get_milli_time(void)
415 {
416 struct timespec t = {0, 0};
417 syscall(SYS_clock_gettime, CLOCK_MONOTONIC, &t);
418 return t.tv_sec * 1000 + t.tv_nsec / 1000000;
419 }
420
421
422 static uint8_t* odhcp6c_resize_state(enum odhcp6c_state state, ssize_t len)
423 {
424 if (len == 0)
425 return state_data[state] + state_len[state];
426 else if (state_len[state] + len > 1024)
427 return NULL;
428
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;
434 }
435 return n;
436 }
437
438
439 bool odhcp6c_signal_process(void)
440 {
441 while (signal_io) {
442 signal_io = false;
443
444 bool ra_updated = ra_process();
445
446 if (ra_link_up())
447 signal_usr2 = true;
448
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);
453
454 #ifdef EXT_BFD_PING
455 bfd_receive();
456 #endif
457 }
458
459 return signal_usr1 || signal_usr2 || signal_term;
460 }
461
462
463 void odhcp6c_clear_state(enum odhcp6c_state state)
464 {
465 state_len[state] = 0;
466 }
467
468
469 void odhcp6c_add_state(enum odhcp6c_state state, const void *data, size_t len)
470 {
471 uint8_t *n = odhcp6c_resize_state(state, len);
472 if (n)
473 memcpy(n, data, len);
474 }
475
476 void odhcp6c_insert_state(enum odhcp6c_state state, size_t offset, const void *data, size_t len)
477 {
478 ssize_t len_after = state_len[state] - offset;
479 if (len_after < 0)
480 return;
481
482 uint8_t *n = odhcp6c_resize_state(state, len);
483 if (n) {
484 uint8_t *sdata = state_data[state];
485
486 memmove(sdata + offset + len, sdata + offset, len_after);
487 memcpy(sdata + offset, data, len);
488 }
489 }
490
491 size_t odhcp6c_remove_state(enum odhcp6c_state state, size_t offset, size_t len)
492 {
493 uint8_t *data = state_data[state];
494 ssize_t len_after = state_len[state] - (offset + len);
495 if (len_after < 0)
496 return state_len[state];
497
498 memmove(data + offset, data + offset + len, len_after);
499 return state_len[state] -= len;
500 }
501
502
503 void* odhcp6c_move_state(enum odhcp6c_state state, size_t *len)
504 {
505 *len = state_len[state];
506 void *data = state_data[state];
507
508 state_len[state] = 0;
509 state_data[state] = NULL;
510
511 return data;
512 }
513
514
515 void* odhcp6c_get_state(enum odhcp6c_state state, size_t *len)
516 {
517 *len = state_len[state];
518 return state_data[state];
519 }
520
521
522 struct odhcp6c_entry* odhcp6c_find_entry(enum odhcp6c_state state, const struct odhcp6c_entry *new)
523 {
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;
527
528 for (struct odhcp6c_entry *c = start; !x && c < &start[len/sizeof(*c)]; ++c)
529 if (!memcmp(c, new, cmplen))
530 return c;
531
532 return NULL;
533 }
534
535
536 bool odhcp6c_update_entry_safe(enum odhcp6c_state state, struct odhcp6c_entry *new, uint32_t safe)
537 {
538 size_t len;
539 struct odhcp6c_entry *x = odhcp6c_find_entry(state, new);
540 struct odhcp6c_entry *start = odhcp6c_get_state(state, &len);
541
542 if (x && x->valid > new->valid && new->valid < safe)
543 new->valid = safe;
544
545 if (new->valid > 0) {
546 if (x) {
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)
551 return false;
552 x->valid = new->valid;
553 x->preferred = new->preferred;
554 x->t1 = new->t1;
555 x->t2 = new->t2;
556 x->class = new->class;
557 } else {
558 odhcp6c_add_state(state, new, sizeof(*new));
559 }
560 } else if (x) {
561 odhcp6c_remove_state(state, (x - start) * sizeof(*x), sizeof(*x));
562 }
563 return true;
564 }
565
566
567 bool odhcp6c_update_entry(enum odhcp6c_state state, struct odhcp6c_entry *new)
568 {
569 return odhcp6c_update_entry_safe(state, new, 0);
570 }
571
572
573 static void odhcp6c_expire_list(enum odhcp6c_state state, uint32_t elapsed)
574 {
575 size_t len;
576 struct odhcp6c_entry *start = odhcp6c_get_state(state, &len);
577 for (struct odhcp6c_entry *c = start; c < &start[len / sizeof(*c)]; ++c) {
578 if (c->t1 < elapsed)
579 c->t1 = 0;
580 else if (c->t1 != UINT32_MAX)
581 c->t1 -= elapsed;
582
583 if (c->t2 < elapsed)
584 c->t2 = 0;
585 else if (c->t2 != UINT32_MAX)
586 c->t2 -= elapsed;
587
588 if (c->preferred < elapsed)
589 c->preferred = 0;
590 else if (c->preferred != UINT32_MAX)
591 c->preferred -= elapsed;
592
593 if (c->valid < elapsed)
594 c->valid = 0;
595 else if (c->valid != UINT32_MAX)
596 c->valid -= elapsed;
597
598 if (!c->valid)
599 odhcp6c_remove_state(state, (c - start) * sizeof(*c), sizeof(*c));
600 }
601 }
602
603
604 void odhcp6c_expire(void)
605 {
606 time_t now = odhcp6c_get_milli_time() / 1000;
607 uint32_t elapsed = (last_update > 0) ? now - last_update : 0;
608 last_update = now;
609
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);
615 }
616
617
618 uint32_t odhcp6c_elapsed(void)
619 {
620 return odhcp6c_get_milli_time() / 1000 - last_update;
621 }
622
623
624 void odhcp6c_random(void *buf, size_t len)
625 {
626 read(urandom_fd, buf, len);
627 }
628
629 bool odhcp6c_is_bound(void)
630 {
631 return bound;
632 }
633
634 static void sighandler(int signal)
635 {
636 if (signal == SIGCHLD)
637 while (waitpid(-1, NULL, WNOHANG) > 0);
638 else if (signal == SIGUSR1)
639 signal_usr1 = true;
640 else if (signal == SIGUSR2)
641 signal_usr2 = true;
642 else if (signal == SIGIO)
643 signal_io = true;
644 else
645 signal_term = true;
646 }