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