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