2 * Copyright (C) 2012-2014 Steven Barth <steven@midlink.org>
3 * Copyright (C) 2017 Hans Dedecker <dedeckeh@gmail.com>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License v2 as published by
7 * the Free Software Foundation.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
31 #include <sys/syscall.h>
32 #include <arpa/inet.h>
33 #include <linux/if_addr.h>
38 #ifndef IN6_IS_ADDR_UNIQUELOCAL
39 #define IN6_IS_ADDR_UNIQUELOCAL(a) \
40 ((((__const uint32_t *) (a))[0] & htonl (0xfe000000)) \
41 == htonl (0xfc000000))
44 static void sighandler(int signal
);
45 static int usage(void);
47 static uint8_t *state_data
[_STATE_MAX
] = {NULL
};
48 static size_t state_len
[_STATE_MAX
] = {0};
50 static volatile bool signal_io
= false;
51 static volatile bool signal_usr1
= false;
52 static volatile bool signal_usr2
= false;
53 static volatile bool signal_term
= false;
55 static int urandom_fd
= -1, allow_slaac_only
= 0;
56 static bool bound
= false, release
= true, ra
= false;
57 static time_t last_update
= 0;
58 static char *ifname
= NULL
;
60 static unsigned int min_update_interval
= DEFAULT_MIN_UPDATE_INTERVAL
;
61 static unsigned int script_sync_delay
= 10;
62 static unsigned int script_accu_delay
= 1;
64 int main(_unused
int argc
, char* const argv
[])
66 static struct in6_addr ifid
= IN6ADDR_ANY_INIT
;
68 const char *pidfile
= NULL
;
69 const char *script
= "/usr/sbin/odhcp6c-update";
75 enum odhcp6c_ia_mode ia_na_mode
= IA_MODE_TRY
;
76 enum odhcp6c_ia_mode ia_pd_mode
= IA_MODE_NONE
;
77 int ia_pd_iaid_index
= 0;
78 int sol_timeout
= DHCPV6_SOL_MAX_RT
;
80 bool help
= false, daemonize
= false;
83 unsigned int client_options
= DHCPV6_CLIENT_FQDN
| DHCPV6_ACCEPT_RECONFIGURE
;
84 unsigned int ra_options
= RA_RDNSS_DEFAULT_LIFETIME
;
86 while ((c
= getopt(argc
, argv
, "S::N:V:P:FB:c:i:r:Ru:s:kt:m:Lhedp:fav")) != -1) {
89 allow_slaac_only
= (optarg
) ? atoi(optarg
) : -1;
93 if (!strcmp(optarg
, "force")) {
94 ia_na_mode
= IA_MODE_FORCE
;
95 allow_slaac_only
= -1;
96 } else if (!strcmp(optarg
, "none"))
97 ia_na_mode
= IA_MODE_NONE
;
98 else if (!strcmp(optarg
, "try"))
99 ia_na_mode
= IA_MODE_TRY
;
105 l
= script_unhexlify(buf
, sizeof(buf
), optarg
);
109 odhcp6c_add_state(STATE_VENDORCLASS
, buf
, l
);
113 if (ia_pd_mode
== IA_MODE_NONE
)
114 ia_pd_mode
= IA_MODE_TRY
;
116 if (allow_slaac_only
>= 0 && allow_slaac_only
< 10)
117 allow_slaac_only
= 10;
121 int prefix_length
= strtoul(optarg
, &iaid_begin
, 10);
123 if (*iaid_begin
!= '\0' && *iaid_begin
!= ',' && *iaid_begin
!= ':') {
124 syslog(LOG_ERR
, "invalid argument: '%s'", optarg
);
128 struct odhcp6c_request_prefix prefix
= { 0, prefix_length
};
130 if (*iaid_begin
== ',' && (iaid_len
= strlen(iaid_begin
)) > 1)
131 memcpy(&prefix
.iaid
, iaid_begin
+ 1, iaid_len
> 4 ? 4 : iaid_len
);
132 else if (*iaid_begin
== ':')
133 prefix
.iaid
= htonl((uint32_t)strtoul(&iaid_begin
[1], NULL
, 16));
135 prefix
.iaid
= htonl(++ia_pd_iaid_index
);
137 odhcp6c_add_state(STATE_IA_PD_INIT
, &prefix
, sizeof(prefix
));
141 allow_slaac_only
= -1;
142 ia_pd_mode
= IA_MODE_FORCE
;
146 l
= script_unhexlify(&buf
[4], sizeof(buf
) - 4, optarg
);
149 buf
[1] = DHCPV6_OPT_CLIENTID
;
152 odhcp6c_add_state(STATE_CLIENT_ID
, buf
, l
+ 4);
158 if (inet_pton(AF_INET6
, optarg
, &ifid
) != 1)
165 opttype
= htons(strtoul(optarg
, &optpos
, 10));
166 if (optpos
== optarg
)
171 odhcp6c_add_state(STATE_ORO
, &opttype
, 2);
176 client_options
|= DHCPV6_STRICT_OPTIONS
;
180 optlen
= htons(strlen(optarg
));
181 odhcp6c_add_state(STATE_USERCLASS
, &optlen
, 2);
182 odhcp6c_add_state(STATE_USERCLASS
, optarg
, strlen(optarg
));
194 sol_timeout
= atoi(optarg
);
198 min_update_interval
= atoi(optarg
);
202 ra_options
&= ~RA_RDNSS_DEFAULT_LIFETIME
;
206 logopt
|= LOG_PERROR
;
218 client_options
&= ~DHCPV6_CLIENT_FQDN
;
222 client_options
&= ~DHCPV6_ACCEPT_RECONFIGURE
;
235 if (allow_slaac_only
> 0)
236 script_sync_delay
= allow_slaac_only
;
238 openlog("odhcp6c", logopt
, LOG_DAEMON
);
240 setlogmask(LOG_UPTO(LOG_WARNING
));
242 ifname
= argv
[optind
];
247 signal(SIGIO
, sighandler
);
248 signal(SIGHUP
, sighandler
);
249 signal(SIGINT
, sighandler
);
250 signal(SIGTERM
, sighandler
);
251 signal(SIGUSR1
, sighandler
);
252 signal(SIGUSR2
, sighandler
);
254 if ((urandom_fd
= open("/dev/urandom", O_CLOEXEC
| O_RDONLY
)) < 0 ||
255 init_dhcpv6(ifname
, client_options
, sol_timeout
) ||
256 ra_init(ifname
, &ifid
, ra_options
) ||
257 script_init(script
, ifname
)) {
258 syslog(LOG_ERR
, "failed to initialize: %s", strerror(errno
));
263 openlog("odhcp6c", LOG_PID
, LOG_DAEMON
); // Disable LOG_PERROR
265 syslog(LOG_ERR
, "Failed to daemonize: %s",
271 snprintf((char*)buf
, sizeof(buf
), "/var/run/odhcp6c.%s.pid", ifname
);
272 pidfile
= (char*)buf
;
275 FILE *fp
= fopen(pidfile
, "w");
277 fprintf(fp
, "%i\n", getpid());
282 script_call("started", 0, false);
284 while (!signal_term
) { // Main logic
285 odhcp6c_clear_state(STATE_SERVER_ID
);
286 odhcp6c_clear_state(STATE_SERVER_ADDR
);
287 odhcp6c_clear_state(STATE_IA_NA
);
288 odhcp6c_clear_state(STATE_IA_PD
);
289 odhcp6c_clear_state(STATE_SNTP_IP
);
290 odhcp6c_clear_state(STATE_NTP_IP
);
291 odhcp6c_clear_state(STATE_NTP_FQDN
);
292 odhcp6c_clear_state(STATE_SIP_IP
);
293 odhcp6c_clear_state(STATE_SIP_FQDN
);
296 syslog(LOG_NOTICE
, "(re)starting transaction on %s", ifname
);
298 signal_usr1
= signal_usr2
= false;
299 int mode
= dhcpv6_set_ia_mode(ia_na_mode
, ia_pd_mode
);
300 if (mode
!= DHCPV6_STATELESS
)
301 mode
= dhcpv6_request(DHCPV6_MSG_SOLICIT
);
303 odhcp6c_signal_process();
309 int res
= dhcpv6_request(mode
== DHCPV6_STATELESS
?
310 DHCPV6_MSG_INFO_REQ
: DHCPV6_MSG_REQUEST
);
311 bool signalled
= odhcp6c_signal_process();
315 else if (signalled
) {
320 mode
= dhcpv6_promote_server_cand();
321 } while (mode
> DHCPV6_UNKNOWN
);
327 case DHCPV6_STATELESS
:
329 syslog(LOG_NOTICE
, "entering stateless-mode on %s", ifname
);
331 while (!signal_usr2
&& !signal_term
) {
333 script_call("informed", script_sync_delay
, true);
335 int res
= dhcpv6_poll_reconfigure();
336 odhcp6c_signal_process();
342 signal_usr1
= false; // Acknowledged
346 if (signal_usr2
|| signal_term
)
349 res
= dhcpv6_request(DHCPV6_MSG_INFO_REQ
);
350 odhcp6c_signal_process();
359 case DHCPV6_STATEFUL
:
361 script_call("bound", script_sync_delay
, true);
362 syslog(LOG_NOTICE
, "entering stateful-mode on %s", ifname
);
364 while (!signal_usr2
&& !signal_term
) {
366 // Wait for T1 to expire or until we get a reconfigure
367 int res
= dhcpv6_poll_reconfigure();
368 odhcp6c_signal_process();
370 script_call("updated", 0, false);
374 // Handle signal, if necessary
376 signal_usr1
= false; // Acknowledged
378 if (signal_usr2
|| signal_term
)
379 break; // Other signal type
381 // Send renew as T1 expired
382 res
= dhcpv6_request(DHCPV6_MSG_RENEW
);
383 odhcp6c_signal_process();
385 if (res
> 0) { // Renew was succesfull
387 script_call("updated", 0, false);
388 continue; // Renew was successful
391 odhcp6c_clear_state(STATE_SERVER_ID
); // Remove binding
392 odhcp6c_clear_state(STATE_SERVER_ADDR
);
394 size_t ia_pd_len
, ia_na_len
;
395 odhcp6c_get_state(STATE_IA_PD
, &ia_pd_len
);
396 odhcp6c_get_state(STATE_IA_NA
, &ia_na_len
);
398 if (ia_pd_len
== 0 && ia_na_len
== 0)
401 // If we have IAs, try rebind otherwise restart
402 res
= dhcpv6_request(DHCPV6_MSG_REBIND
);
403 odhcp6c_signal_process();
406 script_call("rebound", 0, true);
418 size_t ia_pd_len
, ia_na_len
, server_id_len
;
419 odhcp6c_get_state(STATE_IA_PD
, &ia_pd_len
);
420 odhcp6c_get_state(STATE_IA_NA
, &ia_na_len
);
421 odhcp6c_get_state(STATE_SERVER_ID
, &server_id_len
);
423 // Add all prefixes to lost prefixes
425 script_call("unbound", 0, true);
427 if (server_id_len
> 0 && (ia_pd_len
> 0 || ia_na_len
> 0) && release
)
428 dhcpv6_request(DHCPV6_MSG_RELEASE
);
430 odhcp6c_clear_state(STATE_IA_NA
);
431 odhcp6c_clear_state(STATE_IA_PD
);
434 script_call("stopped", 0, true);
439 static int usage(void)
442 "Usage: odhcp6c [options] <interface>\n"
443 "\nFeature options:\n"
444 " -S <time> Wait at least <time> sec for a DHCP-server (0)\n"
445 " -N <mode> Mode for requesting addresses [try|force|none]\n"
446 " -P <length> Request IPv6-Prefix (0 = auto)\n"
447 " -F Force IPv6-Prefix\n"
448 " -V <class> Set vendor-class option (base-16 encoded)\n"
449 " -u <user-class> Set user-class option string\n"
450 " -c <clientid> Override client-ID (base-16 encoded 16-bit type + value)\n"
451 " -i <iface-id> Use a custom interface identifier for RA handling\n"
452 " -r <options> Options to be requested (comma-separated)\n"
453 " -R Do not request any options except those specified with -r\n"
454 " -s <script> Status update script (/usr/sbin/odhcp6c-update)\n"
455 " -a Don't send Accept Reconfigure option\n"
456 " -f Don't send Client FQDN option\n"
457 " -k Don't send a RELEASE when stopping\n"
458 " -t <seconds> Maximum timeout for DHCPv6-SOLICIT (120)\n"
459 " -m <seconds> Minimum time between accepting updates (30)\n"
460 " -L Ignore default lifetime for RDNSS records\n"
461 "\nInvocation options:\n"
462 " -p <pidfile> Set pidfile (/var/run/odhcp6c.pid)\n"
464 " -e Write logmessages to stderr\n"
465 " -v Increase logging verbosity\n"
466 " -h Show this help\n\n";
472 // Don't want to pull-in librt and libpthread just for a monotonic clock...
473 uint64_t odhcp6c_get_milli_time(void)
475 struct timespec t
= {0, 0};
476 syscall(SYS_clock_gettime
, CLOCK_MONOTONIC
, &t
);
478 return ((uint64_t)t
.tv_sec
) * 1000 + ((uint64_t)t
.tv_nsec
) / 1000000;
481 static uint8_t* odhcp6c_resize_state(enum odhcp6c_state state
, ssize_t len
)
484 return state_data
[state
] + state_len
[state
];
485 else if (state_len
[state
] + len
> 1024)
488 uint8_t *n
= realloc(state_data
[state
], state_len
[state
] + len
);
490 if (n
|| state_len
[state
] + len
== 0) {
491 state_data
[state
] = n
;
492 n
+= state_len
[state
];
493 state_len
[state
] += len
;
499 bool odhcp6c_signal_process(void)
504 bool ra_updated
= ra_process();
511 if (ra_updated
&& (bound
|| allow_slaac_only
>= 0)) {
512 script_call("ra-updated", (!ra
&& !bound
) ?
513 script_sync_delay
: script_accu_delay
, false);
518 return signal_usr1
|| signal_usr2
|| signal_term
;
521 void odhcp6c_clear_state(enum odhcp6c_state state
)
523 state_len
[state
] = 0;
526 void odhcp6c_add_state(enum odhcp6c_state state
, const void *data
, size_t len
)
528 uint8_t *n
= odhcp6c_resize_state(state
, len
);
531 memcpy(n
, data
, len
);
534 int odhcp6c_insert_state(enum odhcp6c_state state
, size_t offset
, const void *data
, size_t len
)
536 ssize_t len_after
= state_len
[state
] - offset
;
540 uint8_t *n
= odhcp6c_resize_state(state
, len
);
543 uint8_t *sdata
= state_data
[state
];
545 memmove(sdata
+ offset
+ len
, sdata
+ offset
, len_after
);
546 memcpy(sdata
+ offset
, data
, len
);
552 size_t odhcp6c_remove_state(enum odhcp6c_state state
, size_t offset
, size_t len
)
554 uint8_t *data
= state_data
[state
];
555 ssize_t len_after
= state_len
[state
] - (offset
+ len
);
558 return state_len
[state
];
560 memmove(data
+ offset
, data
+ offset
+ len
, len_after
);
562 return state_len
[state
] -= len
;
565 void* odhcp6c_move_state(enum odhcp6c_state state
, size_t *len
)
567 *len
= state_len
[state
];
568 void *data
= state_data
[state
];
570 state_len
[state
] = 0;
571 state_data
[state
] = NULL
;
576 void* odhcp6c_get_state(enum odhcp6c_state state
, size_t *len
)
578 *len
= state_len
[state
];
580 return state_data
[state
];
583 static struct odhcp6c_entry
* odhcp6c_find_entry(enum odhcp6c_state state
, const struct odhcp6c_entry
*new)
585 size_t len
, cmplen
= offsetof(struct odhcp6c_entry
, target
) + ((new->length
+ 7) / 8);
586 uint8_t *start
= odhcp6c_get_state(state
, &len
);
588 for (struct odhcp6c_entry
*c
= (struct odhcp6c_entry
*)start
;
589 (uint8_t*)c
< &start
[len
] &&
590 (uint8_t*)odhcp6c_next_entry(c
) <= &start
[len
];
591 c
= odhcp6c_next_entry(c
)) {
592 if (!memcmp(c
, new, cmplen
) && !memcmp(c
->auxtarget
, new->auxtarget
, new->auxlen
))
599 bool odhcp6c_update_entry(enum odhcp6c_state state
, struct odhcp6c_entry
*new,
600 uint32_t safe
, bool filterexcess
)
603 struct odhcp6c_entry
*x
= odhcp6c_find_entry(state
, new);
604 uint8_t *start
= odhcp6c_get_state(state
, &len
);
606 if (x
&& x
->valid
> new->valid
&& new->valid
< safe
)
609 if (new->valid
> 0) {
611 if (filterexcess
&& new->valid
>= x
->valid
&&
612 new->valid
!= UINT32_MAX
&&
613 new->valid
- x
->valid
< min_update_interval
&&
614 new->preferred
>= x
->preferred
&&
615 new->preferred
!= UINT32_MAX
&&
616 new->preferred
- x
->preferred
< min_update_interval
)
619 x
->valid
= new->valid
;
620 x
->preferred
= new->preferred
;
625 odhcp6c_add_state(state
, new, odhcp6c_entry_size(new));
627 odhcp6c_remove_state(state
, ((uint8_t*)x
) - start
, odhcp6c_entry_size(x
));
632 static void odhcp6c_expire_list(enum odhcp6c_state state
, uint32_t elapsed
)
635 uint8_t *start
= odhcp6c_get_state(state
, &len
);
637 for (struct odhcp6c_entry
*c
= (struct odhcp6c_entry
*)start
;
638 (uint8_t*)c
< &start
[len
] &&
639 (uint8_t*)odhcp6c_next_entry(c
) <= &start
[len
];
643 else if (c
->t1
!= UINT32_MAX
)
648 else if (c
->t2
!= UINT32_MAX
)
651 if (c
->preferred
< elapsed
)
653 else if (c
->preferred
!= UINT32_MAX
)
654 c
->preferred
-= elapsed
;
656 if (c
->valid
< elapsed
)
658 else if (c
->valid
!= UINT32_MAX
)
662 odhcp6c_remove_state(state
, ((uint8_t*)c
) - start
, odhcp6c_entry_size(c
));
663 start
= odhcp6c_get_state(state
, &len
);
665 c
= odhcp6c_next_entry(c
);
669 void odhcp6c_expire(void)
671 time_t now
= odhcp6c_get_milli_time() / 1000;
672 uint32_t elapsed
= (last_update
> 0) ? now
- last_update
: 0;
676 odhcp6c_expire_list(STATE_RA_PREFIX
, elapsed
);
677 odhcp6c_expire_list(STATE_RA_ROUTE
, elapsed
);
678 odhcp6c_expire_list(STATE_RA_DNS
, elapsed
);
679 odhcp6c_expire_list(STATE_RA_SEARCH
, elapsed
);
680 odhcp6c_expire_list(STATE_IA_NA
, elapsed
);
681 odhcp6c_expire_list(STATE_IA_PD
, elapsed
);
684 uint32_t odhcp6c_elapsed(void)
686 return odhcp6c_get_milli_time() / 1000 - last_update
;
689 int odhcp6c_random(void *buf
, size_t len
)
691 return read(urandom_fd
, buf
, len
);
694 bool odhcp6c_is_bound(void)
699 bool odhcp6c_addr_in_scope(const struct in6_addr
*addr
)
701 FILE *fd
= fopen("/proc/net/if_inet6", "r");
708 while (fgets(buf
, sizeof(buf
), fd
)) {
709 struct in6_addr inet6_addr
;
710 uint32_t flags
, dummy
;
712 char name
[IF_NAMESIZE
], addr_buf
[33];
716 if ((len
<= 0) || buf
[len
- 1] != '\n')
721 if (sscanf(buf
, "%s %x %x %x %x %s",
722 addr_buf
, &dummy
, &dummy
, &dummy
, &flags
, name
) != 6)
725 if (strcmp(name
, ifname
) ||
726 (flags
& (IFA_F_DADFAILED
| IFA_F_TENTATIVE
| IFA_F_DEPRECATED
)))
729 for (i
= 0; i
< strlen(addr_buf
); i
++) {
730 if (!isxdigit(addr_buf
[i
]) || isupper(addr_buf
[i
]))
734 memset(&inet6_addr
, 0, sizeof(inet6_addr
));
735 for (i
= 0; i
< (strlen(addr_buf
) / 2); i
++) {
737 static const char hex
[] = "0123456789abcdef";
738 byte
= ((index(hex
, addr_buf
[i
* 2]) - hex
) << 4) |
739 (index(hex
, addr_buf
[i
* 2 + 1]) - hex
);
740 inet6_addr
.s6_addr
[i
] = byte
;
743 if ((IN6_IS_ADDR_LINKLOCAL(&inet6_addr
) == IN6_IS_ADDR_LINKLOCAL(addr
)) &&
744 (IN6_IS_ADDR_UNIQUELOCAL(&inet6_addr
) == IN6_IS_ADDR_UNIQUELOCAL(addr
)))
751 static void sighandler(int signal
)
753 if (signal
== SIGUSR1
)
755 else if (signal
== SIGUSR2
)
757 else if (signal
== SIGIO
)