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