Minor help string clarification for client-ID
[project/odhcp6c.git] / src / odhcp6c.c
1 /**
2 * Copyright (C) 2012-2014 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
36
37 static void sighandler(int signal);
38 static int usage(void);
39
40 static uint8_t *state_data[_STATE_MAX] = {NULL};
41 static size_t state_len[_STATE_MAX] = {0};
42
43 static volatile bool signal_io = false;
44 static volatile bool signal_usr1 = false;
45 static volatile bool signal_usr2 = false;
46 static volatile bool signal_term = false;
47
48 static int urandom_fd = -1, allow_slaac_only = 0;
49 static bool bound = false, release = true;
50 static time_t last_update = 0;
51
52 static unsigned int min_update_interval = DEFAULT_MIN_UPDATE_INTERVAL;
53
54 int main(_unused int argc, char* const argv[])
55 {
56 // Allocate ressources
57 const char *pidfile = NULL;
58 const char *script = "/usr/sbin/odhcp6c-update";
59 ssize_t l;
60 uint8_t buf[134];
61 char *optpos;
62 uint16_t opttype;
63 uint16_t optlen;
64 enum odhcp6c_ia_mode ia_na_mode = IA_MODE_TRY;
65 enum odhcp6c_ia_mode ia_pd_mode = IA_MODE_NONE;
66 int ia_pd_iaid_index = 0;
67 static struct in6_addr ifid = IN6ADDR_ANY_INIT;
68 int sol_timeout = DHCPV6_SOL_MAX_RT;
69 int verbosity = 0;
70
71
72 bool help = false, daemonize = false;
73 int logopt = LOG_PID;
74 int c;
75 unsigned int client_options = DHCPV6_CLIENT_FQDN | DHCPV6_ACCEPT_RECONFIGURE;
76
77 while ((c = getopt(argc, argv, "S::N:V:P:FB:c:i:r:Ru:s:kt:m:hedp:fav")) != -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 'V':
97 l = script_unhexlify(buf, sizeof(buf), optarg);
98 if (!l)
99 help=true;
100
101 odhcp6c_add_state(STATE_VENDORCLASS, buf, l);
102
103 break;
104 case 'P':
105 if (ia_pd_mode == IA_MODE_NONE)
106 ia_pd_mode = IA_MODE_TRY;
107
108 if (allow_slaac_only >= 0 && allow_slaac_only < 10)
109 allow_slaac_only = 10;
110
111 char *iaid_begin;
112 int iaid_len = 0;
113
114 int prefix_length = strtoul(optarg, &iaid_begin, 10);
115
116 if (*iaid_begin != '\0' && *iaid_begin != ',' && *iaid_begin != ':') {
117 syslog(LOG_ERR, "invalid argument: '%s'", optarg);
118 return 1;
119 }
120
121 struct odhcp6c_request_prefix prefix = { 0, prefix_length };
122
123 if (*iaid_begin == ',' && (iaid_len = strlen(iaid_begin)) > 1)
124 memcpy(&prefix.iaid, iaid_begin + 1, iaid_len > 4 ? 4 : iaid_len);
125 else if (*iaid_begin == ':')
126 prefix.iaid = htonl((uint32_t)strtoul(&iaid_begin[1], NULL, 16));
127 else
128 prefix.iaid = htonl(++ia_pd_iaid_index);
129
130 odhcp6c_add_state(STATE_IA_PD_INIT, &prefix, sizeof(prefix));
131
132 break;
133
134 case 'F':
135 allow_slaac_only = -1;
136 ia_pd_mode = IA_MODE_FORCE;
137 break;
138
139 case 'c':
140 l = script_unhexlify(&buf[4], sizeof(buf) - 4, optarg);
141 if (l > 0) {
142 buf[0] = 0;
143 buf[1] = DHCPV6_OPT_CLIENTID;
144 buf[2] = 0;
145 buf[3] = l;
146 odhcp6c_add_state(STATE_CLIENT_ID, buf, l + 4);
147 } else {
148 help = true;
149 }
150 break;
151
152 case 'i':
153 if (inet_pton(AF_INET6, optarg, &ifid) != 1)
154 help = true;
155 break;
156
157 case 'r':
158 optpos = optarg;
159 while (optpos[0]) {
160 opttype = htons(strtoul(optarg, &optpos, 10));
161 if (optpos == optarg)
162 break;
163 else if (optpos[0])
164 optarg = &optpos[1];
165 odhcp6c_add_state(STATE_ORO, &opttype, 2);
166 }
167 break;
168
169 case 'R':
170 client_options |= DHCPV6_STRICT_OPTIONS;
171 break;
172
173 case 'u':
174 optlen = htons(strlen(optarg));
175 odhcp6c_add_state(STATE_USERCLASS, &optlen, 2);
176 odhcp6c_add_state(STATE_USERCLASS, optarg, strlen(optarg));
177 break;
178
179 case 's':
180 script = optarg;
181 break;
182
183 case 'k':
184 release = false;
185 break;
186
187 case 't':
188 sol_timeout = atoi(optarg);
189 break;
190
191 case 'm':
192 min_update_interval = atoi(optarg);
193 break;
194
195 case 'e':
196 logopt |= LOG_PERROR;
197 break;
198
199 case 'd':
200 daemonize = true;
201 break;
202
203 case 'p':
204 pidfile = optarg;
205 break;
206
207 case 'f':
208 client_options &= ~DHCPV6_CLIENT_FQDN;
209 break;
210
211 case 'a':
212 client_options &= ~DHCPV6_ACCEPT_RECONFIGURE;
213 break;
214
215 case 'v':
216 ++verbosity;
217 break;
218
219 default:
220 help = true;
221 break;
222 }
223 }
224
225 openlog("odhcp6c", logopt, LOG_DAEMON);
226 if (!verbosity)
227 setlogmask(LOG_UPTO(LOG_WARNING));
228
229 const char *ifname = argv[optind];
230
231 if (help || !ifname)
232 return usage();
233
234 signal(SIGIO, sighandler);
235 signal(SIGHUP, sighandler);
236 signal(SIGINT, sighandler);
237 signal(SIGCHLD, sighandler);
238 signal(SIGTERM, sighandler);
239 signal(SIGUSR1, sighandler);
240 signal(SIGUSR2, sighandler);
241
242 if ((urandom_fd = open("/dev/urandom", O_CLOEXEC | O_RDONLY)) < 0 ||
243 init_dhcpv6(ifname, client_options, sol_timeout) ||
244 ra_init(ifname, &ifid) || script_init(script, ifname)) {
245 syslog(LOG_ERR, "failed to initialize: %s", strerror(errno));
246 return 3;
247 }
248
249 if (daemonize) {
250 openlog("odhcp6c", LOG_PID, LOG_DAEMON); // Disable LOG_PERROR
251 if (daemon(0, 0)) {
252 syslog(LOG_ERR, "Failed to daemonize: %s",
253 strerror(errno));
254 return 4;
255 }
256
257 if (!pidfile) {
258 snprintf((char*)buf, sizeof(buf), "/var/run/odhcp6c.%s.pid", ifname);
259 pidfile = (char*)buf;
260 }
261
262 FILE *fp = fopen(pidfile, "w");
263 if (fp) {
264 fprintf(fp, "%i\n", getpid());
265 fclose(fp);
266 }
267 }
268
269 script_call("started");
270
271 while (!signal_term) { // Main logic
272 odhcp6c_clear_state(STATE_SERVER_ID);
273 odhcp6c_clear_state(STATE_SERVER_ADDR);
274 odhcp6c_clear_state(STATE_IA_NA);
275 odhcp6c_clear_state(STATE_IA_PD);
276 odhcp6c_clear_state(STATE_SNTP_IP);
277 odhcp6c_clear_state(STATE_NTP_IP);
278 odhcp6c_clear_state(STATE_NTP_FQDN);
279 odhcp6c_clear_state(STATE_SIP_IP);
280 odhcp6c_clear_state(STATE_SIP_FQDN);
281 dhcpv6_set_ia_mode(ia_na_mode, ia_pd_mode);
282 bound = false;
283
284 syslog(LOG_NOTICE, "(re)starting transaction on %s", ifname);
285
286 signal_usr1 = signal_usr2 = false;
287 int mode = dhcpv6_request(DHCPV6_MSG_SOLICIT);
288 odhcp6c_signal_process();
289
290 if (mode < 0)
291 continue;
292
293 do {
294 int res = dhcpv6_request(mode == DHCPV6_STATELESS ?
295 DHCPV6_MSG_INFO_REQ : DHCPV6_MSG_REQUEST);
296 bool signalled = odhcp6c_signal_process();
297
298 if (res > 0)
299 break;
300 else if (signalled) {
301 mode = -1;
302 break;
303 }
304
305 mode = dhcpv6_promote_server_cand();
306 } while (mode > DHCPV6_UNKNOWN);
307
308 if (mode < 0)
309 continue;
310
311 switch (mode) {
312 case DHCPV6_STATELESS:
313 bound = true;
314 syslog(LOG_NOTICE, "entering stateless-mode on %s", ifname);
315
316 while (!signal_usr2 && !signal_term) {
317 signal_usr1 = false;
318 script_call("informed");
319
320 int res = dhcpv6_poll_reconfigure();
321 odhcp6c_signal_process();
322
323 if (res > 0)
324 continue;
325
326 if (signal_usr1) {
327 signal_usr1 = false; // Acknowledged
328 continue;
329 }
330 if (signal_usr2 || signal_term)
331 break;
332
333 res = dhcpv6_request(DHCPV6_MSG_INFO_REQ);
334 odhcp6c_signal_process();
335 if (signal_usr1)
336 continue;
337 else if (res < 0)
338 break;
339 }
340 break;
341
342 case DHCPV6_STATEFUL:
343 bound = true;
344 script_call("bound");
345 syslog(LOG_NOTICE, "entering stateful-mode on %s", ifname);
346
347 while (!signal_usr2 && !signal_term) {
348 // Renew Cycle
349 // Wait for T1 to expire or until we get a reconfigure
350 int res = dhcpv6_poll_reconfigure();
351 odhcp6c_signal_process();
352 if (res > 0) {
353 script_call("updated");
354 continue;
355 }
356
357 // Handle signal, if necessary
358 if (signal_usr1)
359 signal_usr1 = false; // Acknowledged
360 if (signal_usr2 || signal_term)
361 break; // Other signal type
362
363 // Send renew as T1 expired
364 res = dhcpv6_request(DHCPV6_MSG_RENEW);
365 odhcp6c_signal_process();
366 if (res > 0) { // Renew was succesfull
367 // Publish updates
368 script_call("updated");
369 continue; // Renew was successful
370 }
371
372 odhcp6c_clear_state(STATE_SERVER_ID); // Remove binding
373 odhcp6c_clear_state(STATE_SERVER_ADDR);
374
375 size_t ia_pd_len, ia_na_len;
376 odhcp6c_get_state(STATE_IA_PD, &ia_pd_len);
377 odhcp6c_get_state(STATE_IA_NA, &ia_na_len);
378
379 if (ia_pd_len == 0 && ia_na_len == 0)
380 break;
381
382 // If we have IAs, try rebind otherwise restart
383 res = dhcpv6_request(DHCPV6_MSG_REBIND);
384 odhcp6c_signal_process();
385
386 if (res > 0)
387 script_call("rebound");
388 else {
389 break;
390 }
391 }
392 break;
393
394 default:
395 break;
396 }
397
398 odhcp6c_expire();
399
400 size_t ia_pd_len, ia_na_len, server_id_len;
401 odhcp6c_get_state(STATE_IA_PD, &ia_pd_len);
402 odhcp6c_get_state(STATE_IA_NA, &ia_na_len);
403 odhcp6c_get_state(STATE_SERVER_ID, &server_id_len);
404
405 // Add all prefixes to lost prefixes
406 bound = false;
407 script_call("unbound");
408
409 if (server_id_len > 0 && (ia_pd_len > 0 || ia_na_len > 0) && release)
410 dhcpv6_request(DHCPV6_MSG_RELEASE);
411
412 odhcp6c_clear_state(STATE_IA_NA);
413 odhcp6c_clear_state(STATE_IA_PD);
414 }
415
416 script_call("stopped");
417 return 0;
418 }
419
420
421 static int usage(void)
422 {
423 const char buf[] =
424 "Usage: odhcp6c [options] <interface>\n"
425 "\nFeature options:\n"
426 " -S <time> Wait at least <time> sec for a DHCP-server (0)\n"
427 " -N <mode> Mode for requesting addresses [try|force|none]\n"
428 " -P <length> Request IPv6-Prefix (0 = auto)\n"
429 " -F Force IPv6-Prefix\n"
430 " -V <class> Set vendor-class option (base-16 encoded)\n"
431 " -u <user-class> Set user-class option string\n"
432 " -c <clientid> Override client-ID (base-16 encoded 16-bit type + value)\n"
433 " -i <iface-id> Use a custom interface identifier for RA handling\n"
434 " -r <options> Options to be requested (comma-separated)\n"
435 " -R Do not request any options except those specified with -r\n"
436 " -s <script> Status update script (/usr/sbin/odhcp6c-update)\n"
437 " -a Don't send Accept Reconfigure option\n"
438 " -f Don't send Client FQDN option\n"
439 " -k Don't send a RELEASE when stopping\n"
440 " -t <seconds> Maximum timeout for DHCPv6-SOLICIT (3600)\n"
441 " -m <seconds> Minimum time between accepting updates (30)\n"
442 "\nInvocation options:\n"
443 " -p <pidfile> Set pidfile (/var/run/odhcp6c.pid)\n"
444 " -d Daemonize\n"
445 " -e Write logmessages to stderr\n"
446 " -v Increase logging verbosity\n"
447 " -h Show this help\n\n";
448 write(STDERR_FILENO, buf, sizeof(buf));
449 return 1;
450 }
451
452
453 // Don't want to pull-in librt and libpthread just for a monotonic clock...
454 uint64_t odhcp6c_get_milli_time(void)
455 {
456 struct timespec t = {0, 0};
457 syscall(SYS_clock_gettime, CLOCK_MONOTONIC, &t);
458 return ((uint64_t)t.tv_sec) * 1000 + ((uint64_t)t.tv_nsec) / 1000000;
459 }
460
461
462 static uint8_t* odhcp6c_resize_state(enum odhcp6c_state state, ssize_t len)
463 {
464 if (len == 0)
465 return state_data[state] + state_len[state];
466 else if (state_len[state] + len > 1024)
467 return NULL;
468
469 uint8_t *n = realloc(state_data[state], state_len[state] + len);
470 if (n || state_len[state] + len == 0) {
471 state_data[state] = n;
472 n += state_len[state];
473 state_len[state] += len;
474 }
475 return n;
476 }
477
478
479 bool odhcp6c_signal_process(void)
480 {
481 while (signal_io) {
482 signal_io = false;
483
484 bool ra_updated = ra_process();
485
486 if (ra_link_up())
487 signal_usr2 = true;
488
489 if (ra_updated && (bound || allow_slaac_only >= 0))
490 script_call("ra-updated"); // Immediate process urgent events
491 }
492
493 return signal_usr1 || signal_usr2 || signal_term;
494 }
495
496
497 void odhcp6c_clear_state(enum odhcp6c_state state)
498 {
499 state_len[state] = 0;
500 }
501
502
503 void odhcp6c_add_state(enum odhcp6c_state state, const void *data, size_t len)
504 {
505 uint8_t *n = odhcp6c_resize_state(state, len);
506 if (n)
507 memcpy(n, data, len);
508 }
509
510 void odhcp6c_insert_state(enum odhcp6c_state state, size_t offset, const void *data, size_t len)
511 {
512 ssize_t len_after = state_len[state] - offset;
513 if (len_after < 0)
514 return;
515
516 uint8_t *n = odhcp6c_resize_state(state, len);
517 if (n) {
518 uint8_t *sdata = state_data[state];
519
520 memmove(sdata + offset + len, sdata + offset, len_after);
521 memcpy(sdata + offset, data, len);
522 }
523 }
524
525 size_t odhcp6c_remove_state(enum odhcp6c_state state, size_t offset, size_t len)
526 {
527 uint8_t *data = state_data[state];
528 ssize_t len_after = state_len[state] - (offset + len);
529 if (len_after < 0)
530 return state_len[state];
531
532 memmove(data + offset, data + offset + len, len_after);
533 return state_len[state] -= len;
534 }
535
536
537 void* odhcp6c_move_state(enum odhcp6c_state state, size_t *len)
538 {
539 *len = state_len[state];
540 void *data = state_data[state];
541
542 state_len[state] = 0;
543 state_data[state] = NULL;
544
545 return data;
546 }
547
548
549 void* odhcp6c_get_state(enum odhcp6c_state state, size_t *len)
550 {
551 *len = state_len[state];
552 return state_data[state];
553 }
554
555
556 struct odhcp6c_entry* odhcp6c_find_entry(enum odhcp6c_state state, const struct odhcp6c_entry *new)
557 {
558 size_t len, cmplen = offsetof(struct odhcp6c_entry, target) + new->length / 8;
559 struct odhcp6c_entry *start = odhcp6c_get_state(state, &len);
560 struct odhcp6c_entry *x = NULL;
561
562 for (struct odhcp6c_entry *c = start; !x && c < &start[len/sizeof(*c)]; ++c)
563 if (!memcmp(c, new, cmplen))
564 return c;
565
566 return NULL;
567 }
568
569
570 bool odhcp6c_update_entry(enum odhcp6c_state state, struct odhcp6c_entry *new,
571 uint32_t safe, bool filterexcess)
572 {
573 size_t len;
574 struct odhcp6c_entry *x = odhcp6c_find_entry(state, new);
575 struct odhcp6c_entry *start = odhcp6c_get_state(state, &len);
576
577 if (x && x->valid > new->valid && new->valid < safe)
578 new->valid = safe;
579
580 if (new->valid > 0) {
581 if (x) {
582 if (filterexcess && new->valid >= x->valid &&
583 new->valid != UINT32_MAX &&
584 new->valid - x->valid < min_update_interval &&
585 new->preferred >= x->preferred &&
586 new->preferred != UINT32_MAX &&
587 new->preferred - x->preferred < min_update_interval &&
588 x->class == new->class)
589 return false;
590 x->valid = new->valid;
591 x->preferred = new->preferred;
592 x->t1 = new->t1;
593 x->t2 = new->t2;
594 x->class = new->class;
595 x->iaid = new->iaid;
596 } else {
597 odhcp6c_add_state(state, new, sizeof(*new));
598 }
599 } else if (x) {
600 odhcp6c_remove_state(state, (x - start) * sizeof(*x), sizeof(*x));
601 }
602 return true;
603 }
604
605
606 static void odhcp6c_expire_list(enum odhcp6c_state state, uint32_t elapsed)
607 {
608 size_t len;
609 struct odhcp6c_entry *start = odhcp6c_get_state(state, &len);
610 for (struct odhcp6c_entry *c = start; c < &start[len / sizeof(*c)]; ++c) {
611 if (c->t1 < elapsed)
612 c->t1 = 0;
613 else if (c->t1 != UINT32_MAX)
614 c->t1 -= elapsed;
615
616 if (c->t2 < elapsed)
617 c->t2 = 0;
618 else if (c->t2 != UINT32_MAX)
619 c->t2 -= elapsed;
620
621 if (c->preferred < elapsed)
622 c->preferred = 0;
623 else if (c->preferred != UINT32_MAX)
624 c->preferred -= elapsed;
625
626 if (c->valid < elapsed)
627 c->valid = 0;
628 else if (c->valid != UINT32_MAX)
629 c->valid -= elapsed;
630
631 if (!c->valid)
632 odhcp6c_remove_state(state, (c - start) * sizeof(*c), sizeof(*c));
633 }
634 }
635
636
637 void odhcp6c_expire(void)
638 {
639 time_t now = odhcp6c_get_milli_time() / 1000;
640 uint32_t elapsed = (last_update > 0) ? now - last_update : 0;
641 last_update = now;
642
643 odhcp6c_expire_list(STATE_RA_PREFIX, elapsed);
644 odhcp6c_expire_list(STATE_RA_ROUTE, elapsed);
645 odhcp6c_expire_list(STATE_RA_DNS, elapsed);
646 odhcp6c_expire_list(STATE_IA_NA, elapsed);
647 odhcp6c_expire_list(STATE_IA_PD, elapsed);
648 }
649
650
651 uint32_t odhcp6c_elapsed(void)
652 {
653 return odhcp6c_get_milli_time() / 1000 - last_update;
654 }
655
656
657 void odhcp6c_random(void *buf, size_t len)
658 {
659 read(urandom_fd, buf, len);
660 }
661
662 bool odhcp6c_is_bound(void)
663 {
664 return bound;
665 }
666
667 static void sighandler(int signal)
668 {
669 if (signal == SIGCHLD)
670 while (waitpid(-1, NULL, WNOHANG) > 0);
671 else if (signal == SIGUSR1)
672 signal_usr1 = true;
673 else if (signal == SIGUSR2)
674 signal_usr2 = true;
675 else if (signal == SIGIO)
676 signal_io = true;
677 else
678 signal_term = true;
679 }