2 * Copyright (C) 2012-2013 Steven Barth <steven@midlink.org>
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.
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.
31 #include <sys/syscall.h>
32 #include <arpa/inet.h>
33 #include <linux/if_addr.h>
42 #ifndef IN6_IS_ADDR_UNIQUELOCAL
43 #define IN6_IS_ADDR_UNIQUELOCAL(a) \
44 ((((__const uint32_t *) (a))[0] & htonl (0xfe000000)) \
45 == htonl (0xfc000000))
48 static void sighandler(int signal
);
49 static int usage(void);
51 static uint8_t *state_data
[_STATE_MAX
] = {NULL
};
52 static size_t state_len
[_STATE_MAX
] = {0};
54 static volatile int do_signal
= 0;
55 static int urandom_fd
= -1, allow_slaac_only
= 0;
56 static bool bound
= false, release
= true;
57 static time_t last_update
= 0;
58 static char *ifname
= NULL
;
60 int main(_unused
int argc
, char* const argv
[])
62 // Allocate ressources
63 const char *pidfile
= NULL
;
64 const char *script
= "/usr/sbin/odhcp6c-update";
69 enum odhcp6c_ia_mode ia_na_mode
= IA_MODE_TRY
;
70 enum odhcp6c_ia_mode ia_pd_mode
= IA_MODE_TRY
;
71 static struct in6_addr ifid
= IN6ADDR_ANY_INIT
;
72 int sol_timeout
= DHCPV6_SOL_MAX_RT
;
75 int bfd_interval
= 0, bfd_loss
= 3;
78 bool help
= false, daemonize
= false;
80 int c
, request_pd
= 0;
81 while ((c
= getopt(argc
, argv
, "S::N:P:FB:c:i:r:s:kt:hedp:")) != -1) {
84 allow_slaac_only
= (optarg
) ? atoi(optarg
) : -1;
88 if (!strcmp(optarg
, "force")) {
89 ia_na_mode
= IA_MODE_FORCE
;
90 allow_slaac_only
= -1;
91 } else if (!strcmp(optarg
, "none")) {
92 ia_na_mode
= IA_MODE_NONE
;
93 } else if (!strcmp(optarg
, "try")) {
94 ia_na_mode
= IA_MODE_TRY
;
101 if (allow_slaac_only
>= 0 && allow_slaac_only
< 10)
102 allow_slaac_only
= 10;
104 request_pd
= strtoul(optarg
, NULL
, 10);
111 allow_slaac_only
= -1;
112 ia_pd_mode
= IA_MODE_FORCE
;
117 bfd_interval
= atoi(optarg
);
122 l
= script_unhexlify(&buf
[4], sizeof(buf
) - 4, optarg
);
125 buf
[1] = DHCPV6_OPT_CLIENTID
;
128 odhcp6c_add_state(STATE_CLIENT_ID
, buf
, l
+ 4);
135 if (inet_pton(AF_INET6
, optarg
, &ifid
) != 1)
142 opttype
= htons(strtoul(optarg
, &optpos
, 10));
143 if (optpos
== optarg
)
147 odhcp6c_add_state(STATE_ORO
, &opttype
, 2);
160 sol_timeout
= atoi(optarg
);
164 logopt
|= LOG_PERROR
;
181 openlog("odhcp6c", logopt
, LOG_DAEMON
);
182 ifname
= argv
[optind
];
187 signal(SIGIO
, sighandler
);
188 signal(SIGHUP
, sighandler
);
189 signal(SIGINT
, sighandler
);
190 signal(SIGCHLD
, sighandler
);
191 signal(SIGTERM
, sighandler
);
192 signal(SIGUSR1
, sighandler
);
193 signal(SIGUSR2
, sighandler
);
195 if ((urandom_fd
= open("/dev/urandom", O_CLOEXEC
| O_RDONLY
)) < 0 ||
196 init_dhcpv6(ifname
, request_pd
, sol_timeout
) ||
197 ra_init(ifname
, &ifid
) || script_init(script
, ifname
)) {
198 syslog(LOG_ERR
, "failed to initialize: %s", strerror(errno
));
203 openlog("odhcp6c", LOG_PID
, LOG_DAEMON
); // Disable LOG_PERROR
205 syslog(LOG_ERR
, "Failed to daemonize: %s",
212 snprintf(pidbuf
, sizeof(pidbuf
),
213 "/var/run/odhcp6c.%s.pid", ifname
);
217 int fd
= open(pidfile
, O_WRONLY
| O_CREAT
);
220 int len
= snprintf(buf
, sizeof(buf
), "%i\n", getpid());
226 script_call("started");
228 while (do_signal
!= SIGTERM
) { // Main logic
229 odhcp6c_clear_state(STATE_SERVER_ID
);
230 odhcp6c_clear_state(STATE_IA_NA
);
231 odhcp6c_clear_state(STATE_IA_PD
);
232 odhcp6c_clear_state(STATE_SNTP_IP
);
233 odhcp6c_clear_state(STATE_SNTP_FQDN
);
234 odhcp6c_clear_state(STATE_SIP_IP
);
235 odhcp6c_clear_state(STATE_SIP_FQDN
);
236 dhcpv6_set_ia_mode(ia_na_mode
, ia_pd_mode
);
239 syslog(LOG_NOTICE
, "(re)starting transaction on %s", ifname
);
242 int mode
= dhcpv6_request(DHCPV6_MSG_SOLICIT
);
243 odhcp6c_signal_process();
249 int res
= dhcpv6_request(mode
== DHCPV6_STATELESS
?
250 DHCPV6_MSG_INFO_REQ
: DHCPV6_MSG_REQUEST
);
252 odhcp6c_signal_process();
255 else if (do_signal
> 0) {
260 mode
= dhcpv6_promote_server_cand();
261 } while (mode
> DHCPV6_UNKNOWN
);
267 case DHCPV6_STATELESS
:
269 syslog(LOG_NOTICE
, "entering stateless-mode on %s", ifname
);
271 while (do_signal
== 0 || do_signal
== SIGUSR1
) {
273 script_call("informed");
275 int res
= dhcpv6_poll_reconfigure();
276 odhcp6c_signal_process();
281 if (do_signal
== SIGUSR1
) {
282 do_signal
= 0; // Acknowledged
284 } else if (do_signal
> 0)
287 res
= dhcpv6_request(DHCPV6_MSG_INFO_REQ
);
288 odhcp6c_signal_process();
289 if (do_signal
== SIGUSR1
)
296 case DHCPV6_STATEFUL
:
297 script_call("bound");
299 syslog(LOG_NOTICE
, "entering stateful-mode on %s", ifname
);
301 if (bfd_interval
> 0)
302 bfd_start(ifname
, bfd_loss
, bfd_interval
);
305 while (do_signal
== 0 || do_signal
== SIGUSR1
) {
307 // Wait for T1 to expire or until we get a reconfigure
308 int res
= dhcpv6_poll_reconfigure();
309 odhcp6c_signal_process();
311 script_call("updated");
315 // Handle signal, if necessary
316 if (do_signal
== SIGUSR1
)
317 do_signal
= 0; // Acknowledged
318 else if (do_signal
> 0)
319 break; // Other signal type
321 // Send renew as T1 expired
322 res
= dhcpv6_request(DHCPV6_MSG_RENEW
);
323 odhcp6c_signal_process();
324 if (res
> 0) { // Renew was succesfull
326 script_call("updated");
327 continue; // Renew was successful
330 odhcp6c_clear_state(STATE_SERVER_ID
); // Remove binding
332 // If we have IAs, try rebind otherwise restart
333 res
= dhcpv6_request(DHCPV6_MSG_REBIND
);
334 odhcp6c_signal_process();
337 script_call("rebound");
351 size_t ia_pd_len
, ia_na_len
, server_id_len
;
352 odhcp6c_get_state(STATE_IA_PD
, &ia_pd_len
);
353 odhcp6c_get_state(STATE_IA_NA
, &ia_na_len
);
354 odhcp6c_get_state(STATE_SERVER_ID
, &server_id_len
);
356 // Add all prefixes to lost prefixes
358 script_call("unbound");
360 if (server_id_len
> 0 && (ia_pd_len
> 0 || ia_na_len
> 0) && release
)
361 dhcpv6_request(DHCPV6_MSG_RELEASE
);
363 odhcp6c_clear_state(STATE_IA_NA
);
364 odhcp6c_clear_state(STATE_IA_PD
);
367 script_call("stopped");
372 static int usage(void)
375 "Usage: odhcp6c [options] <interface>\n"
376 "\nFeature options:\n"
377 " -S <time> Wait at least <time> sec for a DHCP-server (0)\n"
378 " -N <mode> Mode for requesting addresses [try|force|none]\n"
379 " -P <length> Request IPv6-Prefix (0 = auto)\n"
380 " -F Force IPv6-Prefix\n"
382 " -B <interval> Enable BFD ping check\n"
384 " -c <clientid> Override client-ID (base-16 encoded)\n"
385 " -i <iface-id> Use a custom interface identifier for RA handling\n"
386 " -r <options> Options to be requested (comma-separated)\n"
387 " -s <script> Status update script (/usr/sbin/odhcp6c-update)\n"
388 " -k Don't send a RELEASE when stopping\n"
389 " -t <seconds> Maximum timeout for DHCPv6-SOLICIT (120)\n"
390 "\nInvocation options:\n"
391 " -p <pidfile> Set pidfile (/var/run/6relayd.pid)\n"
393 " -e Write logmessages to stderr\n"
394 //" -v Increase logging verbosity\n"
395 " -h Show this help\n\n";
396 write(STDERR_FILENO
, buf
, sizeof(buf
));
401 // Don't want to pull-in librt and libpthread just for a monotonic clock...
402 uint64_t odhcp6c_get_milli_time(void)
404 struct timespec t
= {0, 0};
405 syscall(SYS_clock_gettime
, CLOCK_MONOTONIC
, &t
);
406 return t
.tv_sec
* 1000 + t
.tv_nsec
/ 1000000;
410 static uint8_t* odhcp6c_resize_state(enum odhcp6c_state state
, ssize_t len
)
413 return state_data
[state
] + state_len
[state
];
414 else if (state_len
[state
] + len
> 1024)
417 uint8_t *n
= realloc(state_data
[state
], state_len
[state
] + len
);
418 if (n
|| state_len
[state
] + len
== 0) {
419 state_data
[state
] = n
;
420 n
+= state_len
[state
];
421 state_len
[state
] += len
;
427 bool odhcp6c_signal_process(void)
429 if (do_signal
== SIGIO
) {
431 bool ra_updated
= ra_process();
436 if (ra_updated
&& (bound
|| allow_slaac_only
== 0))
437 script_call("ra-updated"); // Immediate process urgent events
438 else if (ra_updated
&& !bound
&& allow_slaac_only
> 0)
439 script_delay_call("ra-updated", allow_slaac_only
);
446 return do_signal
!= 0;
450 void odhcp6c_clear_state(enum odhcp6c_state state
)
452 state_len
[state
] = 0;
456 void odhcp6c_add_state(enum odhcp6c_state state
, const void *data
, size_t len
)
458 uint8_t *n
= odhcp6c_resize_state(state
, len
);
460 memcpy(n
, data
, len
);
463 void odhcp6c_insert_state(enum odhcp6c_state state
, size_t offset
, const void *data
, size_t len
)
465 ssize_t len_after
= state_len
[state
] - offset
;
469 uint8_t *n
= odhcp6c_resize_state(state
, len
);
471 uint8_t *sdata
= state_data
[state
];
473 memmove(sdata
+ offset
+ len
, sdata
+ offset
, len_after
);
474 memcpy(sdata
+ offset
, data
, len
);
478 size_t odhcp6c_remove_state(enum odhcp6c_state state
, size_t offset
, size_t len
)
480 uint8_t *data
= state_data
[state
];
481 ssize_t len_after
= state_len
[state
] - (offset
+ len
);
483 return state_len
[state
];
485 memmove(data
+ offset
, data
+ offset
+ len
, len_after
);
486 return state_len
[state
] -= len
;
490 void* odhcp6c_move_state(enum odhcp6c_state state
, size_t *len
)
492 *len
= state_len
[state
];
493 void *data
= state_data
[state
];
495 state_len
[state
] = 0;
496 state_data
[state
] = NULL
;
502 void* odhcp6c_get_state(enum odhcp6c_state state
, size_t *len
)
504 *len
= state_len
[state
];
505 return state_data
[state
];
509 struct odhcp6c_entry
* odhcp6c_find_entry(enum odhcp6c_state state
, const struct odhcp6c_entry
*new)
511 size_t len
, cmplen
= offsetof(struct odhcp6c_entry
, target
) + new->length
/ 8;
512 struct odhcp6c_entry
*start
= odhcp6c_get_state(state
, &len
);
513 struct odhcp6c_entry
*x
= NULL
;
515 for (struct odhcp6c_entry
*c
= start
; !x
&& c
< &start
[len
/sizeof(*c
)]; ++c
)
516 if (!memcmp(c
, new, cmplen
))
523 bool odhcp6c_update_entry_safe(enum odhcp6c_state state
, struct odhcp6c_entry
*new, uint32_t safe
)
526 struct odhcp6c_entry
*x
= odhcp6c_find_entry(state
, new);
527 struct odhcp6c_entry
*start
= odhcp6c_get_state(state
, &len
);
529 if (x
&& x
->valid
> new->valid
&& new->valid
< safe
)
532 if (new->valid
> 0) {
534 if (new->valid
>= x
->valid
&& new->valid
- x
->valid
< 60 &&
535 new->preferred
>= x
->preferred
&&
536 new->preferred
- x
->preferred
< 60 &&
537 x
->class == new->class)
539 x
->valid
= new->valid
;
540 x
->preferred
= new->preferred
;
543 x
->class = new->class;
545 odhcp6c_add_state(state
, new, sizeof(*new));
548 odhcp6c_remove_state(state
, (x
- start
) * sizeof(*x
), sizeof(*x
));
554 bool odhcp6c_update_entry(enum odhcp6c_state state
, struct odhcp6c_entry
*new)
556 return odhcp6c_update_entry_safe(state
, new, 0);
560 static void odhcp6c_expire_list(enum odhcp6c_state state
, uint32_t elapsed
)
563 struct odhcp6c_entry
*start
= odhcp6c_get_state(state
, &len
);
564 for (struct odhcp6c_entry
*c
= start
; c
< &start
[len
/ sizeof(*c
)]; ++c
) {
567 else if (c
->t1
!= UINT32_MAX
)
572 else if (c
->t2
!= UINT32_MAX
)
575 if (c
->preferred
< elapsed
)
577 else if (c
->preferred
!= UINT32_MAX
)
578 c
->preferred
-= elapsed
;
580 if (c
->valid
< elapsed
)
582 else if (c
->valid
!= UINT32_MAX
)
586 odhcp6c_remove_state(state
, (c
- start
) * sizeof(*c
), sizeof(*c
));
591 void odhcp6c_expire(void)
593 time_t now
= odhcp6c_get_milli_time() / 1000;
594 uint32_t elapsed
= (last_update
> 0) ? now
- last_update
: 0;
597 odhcp6c_expire_list(STATE_RA_PREFIX
, elapsed
);
598 odhcp6c_expire_list(STATE_RA_ROUTE
, elapsed
);
599 odhcp6c_expire_list(STATE_RA_DNS
, elapsed
);
600 odhcp6c_expire_list(STATE_IA_NA
, elapsed
);
601 odhcp6c_expire_list(STATE_IA_PD
, elapsed
);
605 uint32_t odhcp6c_elapsed(void)
607 return odhcp6c_get_milli_time() / 1000 - last_update
;
611 void odhcp6c_random(void *buf
, size_t len
)
613 read(urandom_fd
, buf
, len
);
617 bool odhcp6c_is_bound(void)
623 bool odhcp6c_addr_in_scope(const struct in6_addr
*addr
)
625 FILE *fd
= fopen("/proc/net/if_inet6", "r");
632 while (fgets(buf
, sizeof(buf
), fd
)) {
633 struct in6_addr inet6_addr
;
634 uint32_t flags
, dummy
;
636 char name
[8], addr_buf
[32];
640 if ((len
<= 0) || buf
[len
- 1] != '\n')
645 if (sscanf(buf
, "%s %x %x %x %x %s",
646 addr_buf
, &dummy
, &dummy
, &dummy
, &flags
, name
) != 6)
649 if (strcmp(name
, ifname
) ||
650 (flags
& (IFA_F_DADFAILED
| IFA_F_TENTATIVE
| IFA_F_DEPRECATED
)))
653 for (i
= 0; i
< sizeof(addr_buf
); i
++) {
654 if (!isxdigit(addr_buf
[i
]) || isupper(addr_buf
[i
]))
658 memset(&inet6_addr
, 0, sizeof(inet6_addr
));
659 for (i
= 0; i
< (sizeof(addr_buf
) / 2); i
++) {
661 static const char hex
[] = "0123456789abcdef";
662 byte
= ((index(hex
, addr_buf
[i
* 2]) - hex
) << 4) |
663 (index(hex
, addr_buf
[i
* 2 + 1]) - hex
);
664 inet6_addr
.s6_addr
[i
] = byte
;
667 if ((IN6_IS_ADDR_LINKLOCAL(&inet6_addr
) == IN6_IS_ADDR_LINKLOCAL(addr
)) &&
668 (IN6_IS_ADDR_UNIQUELOCAL(&inet6_addr
) == IN6_IS_ADDR_UNIQUELOCAL(addr
)))
674 static void sighandler(int signal
)
676 if (signal
== SIGCHLD
)
677 while (waitpid(-1, NULL
, WNOHANG
) > 0);
678 else if (signal
== SIGUSR1
)
680 else if (signal
== SIGUSR2
)
682 else if (signal
== SIGIO
)