2 * Copyright (C) 2012-2014 Steven Barth <steven@midlink.org>
3 * Copyright (C) 2017-2018 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 script_sync_delay
= 10;
61 static unsigned int script_accu_delay
= 1;
63 int main(_unused
int argc
, char* const argv
[])
65 static struct in6_addr ifid
= IN6ADDR_ANY_INIT
;
67 const char *pidfile
= NULL
;
68 const char *script
= "/usr/sbin/odhcp6c-update";
74 enum odhcp6c_ia_mode ia_na_mode
= IA_MODE_TRY
;
75 enum odhcp6c_ia_mode ia_pd_mode
= IA_MODE_NONE
;
76 int ia_pd_iaid_index
= 0;
77 int sol_timeout
= DHCPV6_SOL_MAX_RT
;
79 bool help
= false, daemonize
= false;
82 unsigned int client_options
= DHCPV6_CLIENT_FQDN
| DHCPV6_ACCEPT_RECONFIGURE
;
83 unsigned int ra_options
= RA_RDNSS_DEFAULT_LIFETIME
;
84 unsigned int ra_holdoff_interval
= RA_MIN_ADV_INTERVAL
;
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
);
107 if (odhcp6c_add_state(STATE_VENDORCLASS
, buf
, l
)) {
108 syslog(LOG_ERR
, "Failed to set vendor-class option");
116 if (ia_pd_mode
== IA_MODE_NONE
)
117 ia_pd_mode
= IA_MODE_TRY
;
119 if (allow_slaac_only
>= 0 && allow_slaac_only
< 10)
120 allow_slaac_only
= 10;
124 int prefix_length
= strtoul(optarg
, &iaid_begin
, 10);
126 if (*iaid_begin
!= '\0' && *iaid_begin
!= ',' && *iaid_begin
!= ':') {
127 syslog(LOG_ERR
, "invalid argument: '%s'", optarg
);
131 struct odhcp6c_request_prefix prefix
= { 0, prefix_length
};
133 if (*iaid_begin
== ',' && (iaid_len
= strlen(iaid_begin
)) > 1)
134 memcpy(&prefix
.iaid
, iaid_begin
+ 1, iaid_len
> 4 ? 4 : iaid_len
);
135 else if (*iaid_begin
== ':')
136 prefix
.iaid
= htonl((uint32_t)strtoul(&iaid_begin
[1], NULL
, 16));
138 prefix
.iaid
= htonl(++ia_pd_iaid_index
);
140 if (odhcp6c_add_state(STATE_IA_PD_INIT
, &prefix
, sizeof(prefix
))) {
141 syslog(LOG_ERR
, "Failed to set request IPv6-Prefix");
147 allow_slaac_only
= -1;
148 ia_pd_mode
= IA_MODE_FORCE
;
152 l
= script_unhexlify(&buf
[4], sizeof(buf
) - 4, optarg
);
155 buf
[1] = DHCPV6_OPT_CLIENTID
;
158 if (odhcp6c_add_state(STATE_CLIENT_ID
, buf
, l
+ 4)) {
159 syslog(LOG_ERR
, "Failed to override client-ID");
167 if (inet_pton(AF_INET6
, optarg
, &ifid
) != 1)
174 opttype
= htons(strtoul(optarg
, &optpos
, 10));
175 if (optpos
== optarg
)
180 if (odhcp6c_add_state(STATE_ORO
, &opttype
, 2)) {
181 syslog(LOG_ERR
, "Failed to add requested option");
188 client_options
|= DHCPV6_STRICT_OPTIONS
;
192 optlen
= htons(strlen(optarg
));
193 if (odhcp6c_add_state(STATE_USERCLASS
, &optlen
, 2) ||
194 odhcp6c_add_state(STATE_USERCLASS
, optarg
, strlen(optarg
))) {
195 syslog(LOG_ERR
, "Failed to set user-class option");
209 sol_timeout
= atoi(optarg
);
213 ra_holdoff_interval
= atoi(optarg
);
217 ra_options
&= ~RA_RDNSS_DEFAULT_LIFETIME
;
221 logopt
|= LOG_PERROR
;
233 client_options
&= ~DHCPV6_CLIENT_FQDN
;
237 client_options
&= ~DHCPV6_ACCEPT_RECONFIGURE
;
250 if (allow_slaac_only
> 0)
251 script_sync_delay
= allow_slaac_only
;
253 openlog("odhcp6c", logopt
, LOG_DAEMON
);
255 setlogmask(LOG_UPTO(LOG_WARNING
));
257 ifname
= argv
[optind
];
262 signal(SIGIO
, sighandler
);
263 signal(SIGHUP
, sighandler
);
264 signal(SIGINT
, sighandler
);
265 signal(SIGTERM
, sighandler
);
266 signal(SIGUSR1
, sighandler
);
267 signal(SIGUSR2
, sighandler
);
269 if ((urandom_fd
= open("/dev/urandom", O_CLOEXEC
| O_RDONLY
)) < 0 ||
270 init_dhcpv6(ifname
, client_options
, sol_timeout
) ||
271 ra_init(ifname
, &ifid
, ra_options
, ra_holdoff_interval
) ||
272 script_init(script
, ifname
)) {
273 syslog(LOG_ERR
, "failed to initialize: %s", strerror(errno
));
278 openlog("odhcp6c", LOG_PID
, LOG_DAEMON
); // Disable LOG_PERROR
280 syslog(LOG_ERR
, "Failed to daemonize: %s",
286 snprintf((char*)buf
, sizeof(buf
), "/var/run/odhcp6c.%s.pid", ifname
);
287 pidfile
= (char*)buf
;
290 FILE *fp
= fopen(pidfile
, "w");
292 fprintf(fp
, "%i\n", getpid());
297 script_call("started", 0, false);
299 while (!signal_term
) { // Main logic
300 odhcp6c_clear_state(STATE_SERVER_ID
);
301 odhcp6c_clear_state(STATE_SERVER_ADDR
);
302 odhcp6c_clear_state(STATE_IA_NA
);
303 odhcp6c_clear_state(STATE_IA_PD
);
304 odhcp6c_clear_state(STATE_SNTP_IP
);
305 odhcp6c_clear_state(STATE_NTP_IP
);
306 odhcp6c_clear_state(STATE_NTP_FQDN
);
307 odhcp6c_clear_state(STATE_SIP_IP
);
308 odhcp6c_clear_state(STATE_SIP_FQDN
);
311 syslog(LOG_NOTICE
, "(re)starting transaction on %s", ifname
);
313 signal_usr1
= signal_usr2
= false;
314 int mode
= dhcpv6_set_ia_mode(ia_na_mode
, ia_pd_mode
);
315 if (mode
!= DHCPV6_STATELESS
)
316 mode
= dhcpv6_request(DHCPV6_MSG_SOLICIT
);
318 odhcp6c_signal_process();
324 int res
= dhcpv6_request(mode
== DHCPV6_STATELESS
?
325 DHCPV6_MSG_INFO_REQ
: DHCPV6_MSG_REQUEST
);
326 bool signalled
= odhcp6c_signal_process();
330 else if (signalled
) {
335 mode
= dhcpv6_promote_server_cand();
336 } while (mode
> DHCPV6_UNKNOWN
);
342 case DHCPV6_STATELESS
:
344 syslog(LOG_NOTICE
, "entering stateless-mode on %s", ifname
);
346 while (!signal_usr2
&& !signal_term
) {
348 script_call("informed", script_sync_delay
, true);
350 int res
= dhcpv6_poll_reconfigure();
351 odhcp6c_signal_process();
357 signal_usr1
= false; // Acknowledged
361 if (signal_usr2
|| signal_term
)
364 res
= dhcpv6_request(DHCPV6_MSG_INFO_REQ
);
365 odhcp6c_signal_process();
374 case DHCPV6_STATEFUL
:
376 script_call("bound", script_sync_delay
, true);
377 syslog(LOG_NOTICE
, "entering stateful-mode on %s", ifname
);
379 while (!signal_usr2
&& !signal_term
) {
381 // Wait for T1 to expire or until we get a reconfigure
382 int res
= dhcpv6_poll_reconfigure();
383 odhcp6c_signal_process();
385 script_call("updated", 0, false);
389 // Handle signal, if necessary
391 signal_usr1
= false; // Acknowledged
393 if (signal_usr2
|| signal_term
)
394 break; // Other signal type
396 // Send renew as T1 expired
397 res
= dhcpv6_request(DHCPV6_MSG_RENEW
);
398 odhcp6c_signal_process();
400 if (res
> 0) { // Renew was succesfull
402 script_call("updated", 0, false);
403 continue; // Renew was successful
406 odhcp6c_clear_state(STATE_SERVER_ID
); // Remove binding
407 odhcp6c_clear_state(STATE_SERVER_ADDR
);
409 size_t ia_pd_len
, ia_na_len
;
410 odhcp6c_get_state(STATE_IA_PD
, &ia_pd_len
);
411 odhcp6c_get_state(STATE_IA_NA
, &ia_na_len
);
413 if (ia_pd_len
== 0 && ia_na_len
== 0)
416 // If we have IAs, try rebind otherwise restart
417 res
= dhcpv6_request(DHCPV6_MSG_REBIND
);
418 odhcp6c_signal_process();
421 script_call("rebound", 0, true);
433 size_t ia_pd_len
, ia_na_len
, server_id_len
;
434 odhcp6c_get_state(STATE_IA_PD
, &ia_pd_len
);
435 odhcp6c_get_state(STATE_IA_NA
, &ia_na_len
);
436 odhcp6c_get_state(STATE_SERVER_ID
, &server_id_len
);
438 // Add all prefixes to lost prefixes
440 script_call("unbound", 0, true);
442 if (server_id_len
> 0 && (ia_pd_len
> 0 || ia_na_len
> 0) && release
)
443 dhcpv6_request(DHCPV6_MSG_RELEASE
);
445 odhcp6c_clear_state(STATE_IA_NA
);
446 odhcp6c_clear_state(STATE_IA_PD
);
449 script_call("stopped", 0, true);
454 static int usage(void)
457 "Usage: odhcp6c [options] <interface>\n"
458 "\nFeature options:\n"
459 " -S <time> Wait at least <time> sec for a DHCP-server (0)\n"
460 " -N <mode> Mode for requesting addresses [try|force|none]\n"
461 " -P <length> Request IPv6-Prefix (0 = auto)\n"
462 " -F Force IPv6-Prefix\n"
463 " -V <class> Set vendor-class option (base-16 encoded)\n"
464 " -u <user-class> Set user-class option string\n"
465 " -c <clientid> Override client-ID (base-16 encoded 16-bit type + value)\n"
466 " -i <iface-id> Use a custom interface identifier for RA handling\n"
467 " -r <options> Options to be requested (comma-separated)\n"
468 " -R Do not request any options except those specified with -r\n"
469 " -s <script> Status update script (/usr/sbin/odhcp6c-update)\n"
470 " -a Don't send Accept Reconfigure option\n"
471 " -f Don't send Client FQDN option\n"
472 " -k Don't send a RELEASE when stopping\n"
473 " -t <seconds> Maximum timeout for DHCPv6-SOLICIT (120)\n"
474 " -m <seconds> Minimum time between accepting RA updates (3)\n"
475 " -L Ignore default lifetime for RDNSS records\n"
476 "\nInvocation options:\n"
477 " -p <pidfile> Set pidfile (/var/run/odhcp6c.pid)\n"
479 " -e Write logmessages to stderr\n"
480 " -v Increase logging verbosity\n"
481 " -h Show this help\n\n";
487 // Don't want to pull-in librt and libpthread just for a monotonic clock...
488 uint64_t odhcp6c_get_milli_time(void)
490 struct timespec t
= {0, 0};
491 syscall(SYS_clock_gettime
, CLOCK_MONOTONIC
, &t
);
493 return ((uint64_t)t
.tv_sec
) * 1000 + ((uint64_t)t
.tv_nsec
) / 1000000;
496 static uint8_t* odhcp6c_resize_state(enum odhcp6c_state state
, ssize_t len
)
499 return state_data
[state
] + state_len
[state
];
500 else if (state_len
[state
] + len
> 1024)
503 uint8_t *n
= realloc(state_data
[state
], state_len
[state
] + len
);
505 if (n
|| state_len
[state
] + len
== 0) {
506 state_data
[state
] = n
;
507 n
+= state_len
[state
];
508 state_len
[state
] += len
;
514 bool odhcp6c_signal_process(void)
519 bool ra_updated
= ra_process();
526 if (ra_updated
&& (bound
|| allow_slaac_only
>= 0)) {
527 script_call("ra-updated", (!ra
&& !bound
) ?
528 script_sync_delay
: script_accu_delay
, false);
533 return signal_usr1
|| signal_usr2
|| signal_term
;
536 void odhcp6c_clear_state(enum odhcp6c_state state
)
538 state_len
[state
] = 0;
541 int odhcp6c_add_state(enum odhcp6c_state state
, const void *data
, size_t len
)
543 uint8_t *n
= odhcp6c_resize_state(state
, len
);
548 memcpy(n
, data
, len
);
553 int odhcp6c_insert_state(enum odhcp6c_state state
, size_t offset
, const void *data
, size_t len
)
555 ssize_t len_after
= state_len
[state
] - offset
;
559 uint8_t *n
= odhcp6c_resize_state(state
, len
);
562 uint8_t *sdata
= state_data
[state
];
564 memmove(sdata
+ offset
+ len
, sdata
+ offset
, len_after
);
565 memcpy(sdata
+ offset
, data
, len
);
571 size_t odhcp6c_remove_state(enum odhcp6c_state state
, size_t offset
, size_t len
)
573 uint8_t *data
= state_data
[state
];
574 ssize_t len_after
= state_len
[state
] - (offset
+ len
);
577 return state_len
[state
];
579 memmove(data
+ offset
, data
+ offset
+ len
, len_after
);
581 return state_len
[state
] -= len
;
584 void* odhcp6c_move_state(enum odhcp6c_state state
, size_t *len
)
586 *len
= state_len
[state
];
587 void *data
= state_data
[state
];
589 state_len
[state
] = 0;
590 state_data
[state
] = NULL
;
595 void* odhcp6c_get_state(enum odhcp6c_state state
, size_t *len
)
597 *len
= state_len
[state
];
599 return state_data
[state
];
602 static struct odhcp6c_entry
* odhcp6c_find_entry(enum odhcp6c_state state
, const struct odhcp6c_entry
*new)
604 size_t len
, cmplen
= offsetof(struct odhcp6c_entry
, target
) + ((new->length
+ 7) / 8);
605 uint8_t *start
= odhcp6c_get_state(state
, &len
);
607 for (struct odhcp6c_entry
*c
= (struct odhcp6c_entry
*)start
;
608 (uint8_t*)c
< &start
[len
] &&
609 (uint8_t*)odhcp6c_next_entry(c
) <= &start
[len
];
610 c
= odhcp6c_next_entry(c
)) {
611 if (!memcmp(c
, new, cmplen
) && !memcmp(c
->auxtarget
, new->auxtarget
, new->auxlen
))
618 bool odhcp6c_update_entry(enum odhcp6c_state state
, struct odhcp6c_entry
*new,
619 uint32_t safe
, unsigned int holdoff_interval
)
622 struct odhcp6c_entry
*x
= odhcp6c_find_entry(state
, new);
623 uint8_t *start
= odhcp6c_get_state(state
, &len
);
625 if (x
&& x
->valid
> new->valid
&& new->valid
< safe
)
628 if (new->valid
> 0) {
630 if (holdoff_interval
&& new->valid
>= x
->valid
&&
631 new->valid
!= UINT32_MAX
&&
632 new->valid
- x
->valid
< holdoff_interval
&&
633 new->preferred
>= x
->preferred
&&
634 new->preferred
!= UINT32_MAX
&&
635 new->preferred
- x
->preferred
< holdoff_interval
)
638 x
->valid
= new->valid
;
639 x
->preferred
= new->preferred
;
643 } else if (odhcp6c_add_state(state
, new, odhcp6c_entry_size(new)))
646 odhcp6c_remove_state(state
, ((uint8_t*)x
) - start
, odhcp6c_entry_size(x
));
651 static void odhcp6c_expire_list(enum odhcp6c_state state
, uint32_t elapsed
)
654 uint8_t *start
= odhcp6c_get_state(state
, &len
);
656 for (struct odhcp6c_entry
*c
= (struct odhcp6c_entry
*)start
;
657 (uint8_t*)c
< &start
[len
] &&
658 (uint8_t*)odhcp6c_next_entry(c
) <= &start
[len
];
662 else if (c
->t1
!= UINT32_MAX
)
667 else if (c
->t2
!= UINT32_MAX
)
670 if (c
->preferred
< elapsed
)
672 else if (c
->preferred
!= UINT32_MAX
)
673 c
->preferred
-= elapsed
;
675 if (c
->valid
< elapsed
)
677 else if (c
->valid
!= UINT32_MAX
)
681 odhcp6c_remove_state(state
, ((uint8_t*)c
) - start
, odhcp6c_entry_size(c
));
682 start
= odhcp6c_get_state(state
, &len
);
684 c
= odhcp6c_next_entry(c
);
688 void odhcp6c_expire(void)
690 time_t now
= odhcp6c_get_milli_time() / 1000;
691 uint32_t elapsed
= (last_update
> 0) ? now
- last_update
: 0;
695 odhcp6c_expire_list(STATE_RA_PREFIX
, elapsed
);
696 odhcp6c_expire_list(STATE_RA_ROUTE
, elapsed
);
697 odhcp6c_expire_list(STATE_RA_DNS
, elapsed
);
698 odhcp6c_expire_list(STATE_RA_SEARCH
, elapsed
);
699 odhcp6c_expire_list(STATE_IA_NA
, elapsed
);
700 odhcp6c_expire_list(STATE_IA_PD
, elapsed
);
703 uint32_t odhcp6c_elapsed(void)
705 return odhcp6c_get_milli_time() / 1000 - last_update
;
708 int odhcp6c_random(void *buf
, size_t len
)
710 return read(urandom_fd
, buf
, len
);
713 bool odhcp6c_is_bound(void)
718 bool odhcp6c_addr_in_scope(const struct in6_addr
*addr
)
720 FILE *fd
= fopen("/proc/net/if_inet6", "r");
727 while (fgets(buf
, sizeof(buf
), fd
)) {
728 struct in6_addr inet6_addr
;
729 uint32_t flags
, dummy
;
731 char name
[IF_NAMESIZE
], addr_buf
[33];
735 if ((len
<= 0) || buf
[len
- 1] != '\n')
740 if (sscanf(buf
, "%s %x %x %x %x %s",
741 addr_buf
, &dummy
, &dummy
, &dummy
, &flags
, name
) != 6)
744 if (strcmp(name
, ifname
) ||
745 (flags
& (IFA_F_DADFAILED
| IFA_F_TENTATIVE
| IFA_F_DEPRECATED
)))
748 for (i
= 0; i
< strlen(addr_buf
); i
++) {
749 if (!isxdigit(addr_buf
[i
]) || isupper(addr_buf
[i
]))
753 memset(&inet6_addr
, 0, sizeof(inet6_addr
));
754 for (i
= 0; i
< (strlen(addr_buf
) / 2); i
++) {
756 static const char hex
[] = "0123456789abcdef";
757 byte
= ((index(hex
, addr_buf
[i
* 2]) - hex
) << 4) |
758 (index(hex
, addr_buf
[i
* 2 + 1]) - hex
);
759 inet6_addr
.s6_addr
[i
] = byte
;
762 if ((IN6_IS_ADDR_LINKLOCAL(&inet6_addr
) == IN6_IS_ADDR_LINKLOCAL(addr
)) &&
763 (IN6_IS_ADDR_UNIQUELOCAL(&inet6_addr
) == IN6_IS_ADDR_UNIQUELOCAL(addr
)))
770 static void sighandler(int signal
)
772 if (signal
== SIGUSR1
)
774 else if (signal
== SIGUSR2
)
776 else if (signal
== SIGIO
)