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