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