]> git.decadent.org.uk Git - odhcp6c.git/blob - src/odhcp6c.c
fix NTP server option handling
[odhcp6c.git] / src / odhcp6c.c
1 /**
2  * Copyright (C) 2012-2013 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 #include <arpa/inet.h>
31
32 #include "odhcp6c.h"
33 #include "ra.h"
34
35 #ifdef EXT_BFD_PING
36 #include "bfd.h"
37 #endif
38
39
40 static void sighandler(int signal);
41 static int usage(void);
42
43 static uint8_t *state_data[_STATE_MAX] = {NULL};
44 static size_t state_len[_STATE_MAX] = {0};
45
46 static volatile bool signal_io = false;
47 static volatile bool signal_usr1 = false;
48 static volatile bool signal_usr2 = false;
49 static volatile bool signal_term = false;
50
51 static int urandom_fd = -1, allow_slaac_only = 0;
52 static bool bound = false, release = true;
53 static time_t last_update = 0;
54
55
56 int main(_unused int argc, char* const argv[])
57 {
58         // Allocate ressources
59         const char *pidfile = NULL;
60         const char *script = "/usr/sbin/odhcp6c-update";
61         ssize_t l;
62         uint8_t buf[134];
63         char *optpos;
64         uint16_t opttype;
65         enum odhcp6c_ia_mode ia_na_mode = IA_MODE_TRY;
66         enum odhcp6c_ia_mode ia_pd_mode = IA_MODE_TRY;
67         static struct in6_addr ifid = IN6ADDR_ANY_INIT;
68         int sol_timeout = DHCPV6_SOL_MAX_RT;
69
70 #ifdef EXT_BFD_PING
71         int bfd_interval = 0, bfd_loss = 3;
72 #endif
73
74         bool help = false, daemonize = false;
75         int logopt = LOG_PID;
76         int c, request_pd = 0;
77         while ((c = getopt(argc, argv, "S::N:P:FB:c:i:r:s:kt:hedp:")) != -1) {
78                 switch (c) {
79                 case 'S':
80                         allow_slaac_only = (optarg) ? atoi(optarg) : -1;
81                         break;
82
83                 case 'N':
84                         if (!strcmp(optarg, "force")) {
85                                 ia_na_mode = IA_MODE_FORCE;
86                                 allow_slaac_only = -1;
87                         } else if (!strcmp(optarg, "none")) {
88                                 ia_na_mode = IA_MODE_NONE;
89                         } else if (!strcmp(optarg, "try")) {
90                                 ia_na_mode = IA_MODE_TRY;
91                         } else{
92                                 help = true;
93                         }
94                         break;
95
96                 case 'P':
97                         if (allow_slaac_only >= 0 && allow_slaac_only < 10)
98                                 allow_slaac_only = 10;
99
100                         request_pd = strtoul(optarg, NULL, 10);
101                         if (request_pd == 0)
102                                 request_pd = -1;
103
104                         break;
105
106                 case 'F':
107                         allow_slaac_only = -1;
108                         ia_pd_mode = IA_MODE_FORCE;
109                         break;
110
111 #ifdef EXT_BFD_PING
112                 case 'B':
113                         bfd_interval = atoi(optarg);
114                         break;
115 #endif
116
117                 case 'c':
118                         l = script_unhexlify(&buf[4], sizeof(buf) - 4, optarg);
119                         if (l > 0) {
120                                 buf[0] = 0;
121                                 buf[1] = DHCPV6_OPT_CLIENTID;
122                                 buf[2] = 0;
123                                 buf[3] = l;
124                                 odhcp6c_add_state(STATE_CLIENT_ID, buf, l + 4);
125                         } else {
126                                 help = true;
127                         }
128                         break;
129
130                 case 'i':
131                         if (inet_pton(AF_INET6, optarg, &ifid) != 1)
132                                 help = true;
133                         break;
134
135                 case 'r':
136                         optpos = optarg;
137                         while (optpos[0]) {
138                                 opttype = htons(strtoul(optarg, &optpos, 10));
139                                 if (optpos == optarg)
140                                         break;
141                                 else if (optpos[0])
142                                         optarg = &optpos[1];
143                                 odhcp6c_add_state(STATE_ORO, &opttype, 2);
144                         }
145                         break;
146
147                 case 's':
148                         script = optarg;
149                         break;
150
151                 case 'k':
152                         release = false;
153                         break;
154
155                 case 't':
156                         sol_timeout = atoi(optarg);
157                         break;
158
159                 case 'e':
160                         logopt |= LOG_PERROR;
161                         break;
162
163                 case 'd':
164                         daemonize = true;
165                         break;
166
167                 case 'p':
168                         pidfile = optarg;
169                         break;
170
171                 default:
172                         help = true;
173                         break;
174                 }
175         }
176
177         openlog("odhcp6c", logopt, LOG_DAEMON);
178         const char *ifname = argv[optind];
179
180         if (help || !ifname)
181                 return usage();
182
183         signal(SIGIO, sighandler);
184         signal(SIGHUP, sighandler);
185         signal(SIGINT, sighandler);
186         signal(SIGCHLD, sighandler);
187         signal(SIGTERM, sighandler);
188         signal(SIGUSR1, sighandler);
189         signal(SIGUSR2, sighandler);
190
191         if ((urandom_fd = open("/dev/urandom", O_CLOEXEC | O_RDONLY)) < 0 ||
192                         init_dhcpv6(ifname, request_pd, sol_timeout) ||
193                         ra_init(ifname, &ifid) || script_init(script, ifname)) {
194                 syslog(LOG_ERR, "failed to initialize: %s", strerror(errno));
195                 return 3;
196         }
197
198         if (daemonize) {
199                 openlog("odhcp6c", LOG_PID, LOG_DAEMON); // Disable LOG_PERROR
200                 if (daemon(0, 0)) {
201                         syslog(LOG_ERR, "Failed to daemonize: %s",
202                                         strerror(errno));
203                         return 4;
204                 }
205
206                 char pidbuf[128];
207                 if (!pidfile) {
208                         snprintf(pidbuf, sizeof(pidbuf),
209                                         "/var/run/odhcp6c.%s.pid", ifname);
210                         pidfile = pidbuf;
211                 }
212
213                 int fd = open(pidfile, O_WRONLY | O_CREAT);
214                 if (fd >= 0) {
215                         char buf[8];
216                         int len = snprintf(buf, sizeof(buf), "%i\n", getpid());
217                         write(fd, buf, len);
218                         close(fd);
219                 }
220         }
221
222         script_call("started");
223
224         while (!signal_term) { // Main logic
225                 odhcp6c_clear_state(STATE_SERVER_ID);
226                 odhcp6c_clear_state(STATE_IA_NA);
227                 odhcp6c_clear_state(STATE_IA_PD);
228                 odhcp6c_clear_state(STATE_SNTP_IP);
229                 odhcp6c_clear_state(STATE_NTP_IP);
230                 odhcp6c_clear_state(STATE_NTP_FQDN);
231                 odhcp6c_clear_state(STATE_SIP_IP);
232                 odhcp6c_clear_state(STATE_SIP_FQDN);
233                 dhcpv6_set_ia_mode(ia_na_mode, ia_pd_mode);
234                 bound = false;
235
236                 syslog(LOG_NOTICE, "(re)starting transaction on %s", ifname);
237
238                 signal_usr1 = signal_usr2 = false;
239                 int mode = dhcpv6_request(DHCPV6_MSG_SOLICIT);
240                 odhcp6c_signal_process();
241
242                 if (mode < 0)
243                         continue;
244
245                 do {
246                         int res = dhcpv6_request(mode == DHCPV6_STATELESS ?
247                                         DHCPV6_MSG_INFO_REQ : DHCPV6_MSG_REQUEST);
248                         bool signalled = odhcp6c_signal_process();
249
250                         if (res > 0)
251                                 break;
252                         else if (signalled) {
253                                 mode = -1;
254                                 break;
255                         }
256
257                         mode = dhcpv6_promote_server_cand();
258                 } while (mode > DHCPV6_UNKNOWN);
259
260                 if (mode < 0)
261                         continue;
262
263                 switch (mode) {
264                 case DHCPV6_STATELESS:
265                         bound = true;
266                         syslog(LOG_NOTICE, "entering stateless-mode on %s", ifname);
267
268                         while (!signal_usr2 && !signal_term) {
269                                 signal_usr1 = false;
270                                 script_call("informed");
271
272                                 int res = dhcpv6_poll_reconfigure();
273                                 odhcp6c_signal_process();
274
275                                 if (res > 0)
276                                         continue;
277
278                                 if (signal_usr1) {
279                                         signal_usr1 = false; // Acknowledged
280                                         continue;
281                                 }
282                                 if (signal_usr2 || signal_term)
283                                         break;
284
285                                 res = dhcpv6_request(DHCPV6_MSG_INFO_REQ);
286                                 odhcp6c_signal_process();
287                                 if (signal_usr1)
288                                         continue;
289                                 else if (res < 0)
290                                         break;
291                         }
292                         break;
293
294                 case DHCPV6_STATEFUL:
295                         script_call("bound");
296                         bound = true;
297                         syslog(LOG_NOTICE, "entering stateful-mode on %s", ifname);
298 #ifdef EXT_BFD_PING
299                         if (bfd_interval > 0)
300                                 bfd_start(ifname, bfd_loss, bfd_interval);
301 #endif
302
303                         while (!signal_usr2 && !signal_term) {
304                                 // Renew Cycle
305                                 // Wait for T1 to expire or until we get a reconfigure
306                                 int res = dhcpv6_poll_reconfigure();
307                                 odhcp6c_signal_process();
308                                 if (res > 0) {
309                                         script_call("updated");
310                                         continue;
311                                 }
312
313                                 // Handle signal, if necessary
314                                 if (signal_usr1)
315                                         signal_usr1 = false; // Acknowledged
316                                 if (signal_usr2 || signal_term)
317                                         break; // Other signal type
318
319                                 // Send renew as T1 expired
320                                 size_t ia_pd_len, ia_na_len;
321                                 odhcp6c_get_state(STATE_IA_PD, &ia_pd_len);
322                                 odhcp6c_get_state(STATE_IA_NA, &ia_na_len);
323
324                                 // If we have any IAs, send renew, otherwise request
325                                 if (ia_pd_len == 0 && ia_na_len == 0)
326                                         res = dhcpv6_request(DHCPV6_MSG_REQUEST);
327                                 else
328                                         res = dhcpv6_request(DHCPV6_MSG_RENEW);
329
330                                 odhcp6c_signal_process();
331                                 if (res > 0) { // Renew was succesfull
332                                         // Publish updates
333                                         script_call("updated");
334                                         continue; // Renew was successful
335                                 }
336
337                                 odhcp6c_clear_state(STATE_SERVER_ID); // Remove binding
338
339                                 // If we have IAs, try rebind otherwise restart
340                                 res = dhcpv6_request(DHCPV6_MSG_REBIND);
341                                 odhcp6c_signal_process();
342
343                                 if (res > 0)
344                                         script_call("rebound");
345                                 else {
346 #ifdef EXT_BFD_PING
347                                         bfd_stop();
348 #endif
349                                         break;
350                                 }
351                         }
352                         break;
353
354                 default:
355                         break;
356                 }
357
358                 size_t ia_pd_len, ia_na_len, server_id_len;
359                 odhcp6c_get_state(STATE_IA_PD, &ia_pd_len);
360                 odhcp6c_get_state(STATE_IA_NA, &ia_na_len);
361                 odhcp6c_get_state(STATE_SERVER_ID, &server_id_len);
362
363                 // Add all prefixes to lost prefixes
364                 bound = false;
365                 script_call("unbound");
366
367                 if (server_id_len > 0 && (ia_pd_len > 0 || ia_na_len > 0) && release)
368                         dhcpv6_request(DHCPV6_MSG_RELEASE);
369
370                 odhcp6c_clear_state(STATE_IA_NA);
371                 odhcp6c_clear_state(STATE_IA_PD);
372         }
373
374         script_call("stopped");
375         return 0;
376 }
377
378
379 static int usage(void)
380 {
381         const char buf[] =
382         "Usage: odhcp6c [options] <interface>\n"
383         "\nFeature options:\n"
384         "       -S <time>       Wait at least <time> sec for a DHCP-server (0)\n"
385         "       -N <mode>       Mode for requesting addresses [try|force|none]\n"
386         "       -P <length>     Request IPv6-Prefix (0 = auto)\n"
387         "       -F              Force IPv6-Prefix\n"
388 #ifdef EXT_BFD_PING
389         "       -B <interval>   Enable BFD ping check\n"
390 #endif
391         "       -c <clientid>   Override client-ID (base-16 encoded)\n"
392         "       -i <iface-id>   Use a custom interface identifier for RA handling\n"
393         "       -r <options>    Options to be requested (comma-separated)\n"
394         "       -s <script>     Status update script (/usr/sbin/odhcp6c-update)\n"
395         "       -k              Don't send a RELEASE when stopping\n"
396         "       -t <seconds>    Maximum timeout for DHCPv6-SOLICIT (120)\n"
397         "\nInvocation options:\n"
398         "       -p <pidfile>    Set pidfile (/var/run/6relayd.pid)\n"
399         "       -d              Daemonize\n"
400         "       -e              Write logmessages to stderr\n"
401         //"     -v              Increase logging verbosity\n"
402         "       -h              Show this help\n\n";
403         write(STDERR_FILENO, buf, sizeof(buf));
404         return 1;
405 }
406
407
408 // Don't want to pull-in librt and libpthread just for a monotonic clock...
409 uint64_t odhcp6c_get_milli_time(void)
410 {
411         struct timespec t = {0, 0};
412         syscall(SYS_clock_gettime, CLOCK_MONOTONIC, &t);
413         return t.tv_sec * 1000 + t.tv_nsec / 1000000;
414 }
415
416
417 static uint8_t* odhcp6c_resize_state(enum odhcp6c_state state, ssize_t len)
418 {
419         if (len == 0)
420                 return state_data[state] + state_len[state];
421         else if (state_len[state] + len > 1024)
422                 return NULL;
423
424         uint8_t *n = realloc(state_data[state], state_len[state] + len);
425         if (n || state_len[state] + len == 0) {
426                 state_data[state] = n;
427                 n += state_len[state];
428                 state_len[state] += len;
429         }
430         return n;
431 }
432
433
434 bool odhcp6c_signal_process(void)
435 {
436         while (signal_io) {
437                 signal_io = false;
438
439                 bool ra_updated = ra_process();
440
441                 if (ra_link_up())
442                         signal_usr2 = true;
443
444                 if (ra_updated && (bound || allow_slaac_only == 0))
445                         script_call("ra-updated"); // Immediate process urgent events
446                 else if (ra_updated && !bound && allow_slaac_only > 0)
447                         script_delay_call("ra-updated", allow_slaac_only);
448
449 #ifdef EXT_BFD_PING
450                 bfd_receive();
451 #endif
452         }
453
454         return signal_usr1 || signal_usr2 || signal_term;
455 }
456
457
458 void odhcp6c_clear_state(enum odhcp6c_state state)
459 {
460         state_len[state] = 0;
461 }
462
463
464 void odhcp6c_add_state(enum odhcp6c_state state, const void *data, size_t len)
465 {
466         uint8_t *n = odhcp6c_resize_state(state, len);
467         if (n)
468                 memcpy(n, data, len);
469 }
470
471 void odhcp6c_insert_state(enum odhcp6c_state state, size_t offset, const void *data, size_t len)
472 {
473         ssize_t len_after = state_len[state] - offset;
474         if (len_after < 0)
475                 return;
476
477         uint8_t *n = odhcp6c_resize_state(state, len);
478         if (n) {
479                 uint8_t *sdata = state_data[state];
480
481                 memmove(sdata + offset + len, sdata + offset, len_after);
482                 memcpy(sdata + offset, data, len);
483         }
484 }
485
486 size_t odhcp6c_remove_state(enum odhcp6c_state state, size_t offset, size_t len)
487 {
488         uint8_t *data = state_data[state];
489         ssize_t len_after = state_len[state] - (offset + len);
490         if (len_after < 0)
491                 return state_len[state];
492
493         memmove(data + offset, data + offset + len, len_after);
494         return state_len[state] -= len;
495 }
496
497
498 void* odhcp6c_move_state(enum odhcp6c_state state, size_t *len)
499 {
500         *len = state_len[state];
501         void *data = state_data[state];
502
503         state_len[state] = 0;
504         state_data[state] = NULL;
505
506         return data;
507 }
508
509
510 void* odhcp6c_get_state(enum odhcp6c_state state, size_t *len)
511 {
512         *len = state_len[state];
513         return state_data[state];
514 }
515
516
517 struct odhcp6c_entry* odhcp6c_find_entry(enum odhcp6c_state state, const struct odhcp6c_entry *new)
518 {
519         size_t len, cmplen = offsetof(struct odhcp6c_entry, target) + new->length / 8;
520         struct odhcp6c_entry *start = odhcp6c_get_state(state, &len);
521         struct odhcp6c_entry *x = NULL;
522
523         for (struct odhcp6c_entry *c = start; !x && c < &start[len/sizeof(*c)]; ++c)
524                 if (!memcmp(c, new, cmplen))
525                         return c;
526
527         return NULL;
528 }
529
530
531 bool odhcp6c_update_entry_safe(enum odhcp6c_state state, struct odhcp6c_entry *new, uint32_t safe)
532 {
533         size_t len;
534         struct odhcp6c_entry *x = odhcp6c_find_entry(state, new);
535         struct odhcp6c_entry *start = odhcp6c_get_state(state, &len);
536
537         if (x && x->valid > new->valid && new->valid < safe)
538                 new->valid = safe;
539
540         if (new->valid > 0) {
541                 if (x) {
542                         if (new->valid >= x->valid && new->valid - x->valid < 60 &&
543                                         new->preferred >= x->preferred &&
544                                         new->preferred - x->preferred < 60 &&
545                                         x->class == new->class)
546                                 return false;
547                         x->valid = new->valid;
548                         x->preferred = new->preferred;
549                         x->t1 = new->t1;
550                         x->t2 = new->t2;
551                         x->class = new->class;
552                 } else {
553                         odhcp6c_add_state(state, new, sizeof(*new));
554                 }
555         } else if (x) {
556                 odhcp6c_remove_state(state, (x - start) * sizeof(*x), sizeof(*x));
557         }
558         return true;
559 }
560
561
562 bool odhcp6c_update_entry(enum odhcp6c_state state, struct odhcp6c_entry *new)
563 {
564         return odhcp6c_update_entry_safe(state, new, 0);
565 }
566
567
568 static void odhcp6c_expire_list(enum odhcp6c_state state, uint32_t elapsed)
569 {
570         size_t len;
571         struct odhcp6c_entry *start = odhcp6c_get_state(state, &len);
572         for (struct odhcp6c_entry *c = start; c < &start[len / sizeof(*c)]; ++c) {
573                 if (c->t1 < elapsed)
574                         c->t1 = 0;
575                 else if (c->t1 != UINT32_MAX)
576                         c->t1 -= elapsed;
577
578                 if (c->t2 < elapsed)
579                         c->t2 = 0;
580                 else if (c->t2 != UINT32_MAX)
581                         c->t2 -= elapsed;
582
583                 if (c->preferred < elapsed)
584                         c->preferred = 0;
585                 else if (c->preferred != UINT32_MAX)
586                         c->preferred -= elapsed;
587
588                 if (c->valid < elapsed)
589                         c->valid = 0;
590                 else if (c->valid != UINT32_MAX)
591                         c->valid -= elapsed;
592
593                 if (!c->valid)
594                         odhcp6c_remove_state(state, (c - start) * sizeof(*c), sizeof(*c));
595         }
596 }
597
598
599 void odhcp6c_expire(void)
600 {
601         time_t now = odhcp6c_get_milli_time() / 1000;
602         uint32_t elapsed = (last_update > 0) ? now - last_update : 0;
603         last_update = now;
604
605         odhcp6c_expire_list(STATE_RA_PREFIX, elapsed);
606         odhcp6c_expire_list(STATE_RA_ROUTE, elapsed);
607         odhcp6c_expire_list(STATE_RA_DNS, elapsed);
608         odhcp6c_expire_list(STATE_IA_NA, elapsed);
609         odhcp6c_expire_list(STATE_IA_PD, elapsed);
610 }
611
612
613 uint32_t odhcp6c_elapsed(void)
614 {
615         return odhcp6c_get_milli_time() / 1000 - last_update;
616 }
617
618
619 void odhcp6c_random(void *buf, size_t len)
620 {
621         read(urandom_fd, buf, len);
622 }
623
624 bool odhcp6c_is_bound(void)
625 {
626         return bound;
627 }
628
629 static void sighandler(int signal)
630 {
631         if (signal == SIGCHLD)
632                 while (waitpid(-1, NULL, WNOHANG) > 0);
633         else if (signal == SIGUSR1)
634                 signal_usr1 = true;
635         else if (signal == SIGUSR2)
636                 signal_usr2 = true;
637         else if (signal == SIGIO)
638                 signal_io = true;
639         else
640                 signal_term = true;
641 }