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