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