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