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>
39 #ifndef IN6_IS_ADDR_UNIQUELOCAL
40 #define IN6_IS_ADDR_UNIQUELOCAL(a) \
41 ((((__const uint32_t *) (a))[0] & htonl (0xfe000000)) \
42 == htonl (0xfc000000))
45 static void sighandler(int signal
);
46 static int usage(void);
48 static uint8_t *state_data
[_STATE_MAX
] = {NULL
};
49 static size_t state_len
[_STATE_MAX
] = {0};
51 static volatile bool signal_io
= false;
52 static volatile bool signal_usr1
= false;
53 static volatile bool signal_usr2
= false;
54 static volatile bool signal_term
= false;
56 static int urandom_fd
= -1, allow_slaac_only
= 0;
57 static bool bound
= false, release
= true, ra
= false;
58 static time_t last_update
= 0;
59 static char *ifname
= NULL
;
61 static unsigned int min_update_interval
= DEFAULT_MIN_UPDATE_INTERVAL
;
62 static unsigned int script_sync_delay
= 10;
63 static unsigned int script_accu_delay
= 1;
65 int main(_unused
int argc
, char* const argv
[])
67 // Allocate ressources
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 static struct in6_addr ifid
= IN6ADDR_ANY_INIT
;
79 int sol_timeout
= DHCPV6_SOL_MAX_RT
;
83 bool help
= false, daemonize
= false;
86 unsigned int client_options
= DHCPV6_CLIENT_FQDN
| DHCPV6_ACCEPT_RECONFIGURE
;
87 unsigned int ra_options
= RA_RDNSS_DEFAULT_LIFETIME
;
89 while ((c
= getopt(argc
, argv
, "S::N:V:P:FB:c:i:r:Ru:s:kt:m:Lhedp:fav")) != -1) {
92 allow_slaac_only
= (optarg
) ? atoi(optarg
) : -1;
96 if (!strcmp(optarg
, "force")) {
97 ia_na_mode
= IA_MODE_FORCE
;
98 allow_slaac_only
= -1;
99 } else if (!strcmp(optarg
, "none")) {
100 ia_na_mode
= IA_MODE_NONE
;
101 } else if (!strcmp(optarg
, "try")) {
102 ia_na_mode
= IA_MODE_TRY
;
109 l
= script_unhexlify(buf
, sizeof(buf
), optarg
);
113 odhcp6c_add_state(STATE_VENDORCLASS
, buf
, l
);
117 if (ia_pd_mode
== IA_MODE_NONE
)
118 ia_pd_mode
= IA_MODE_TRY
;
120 if (allow_slaac_only
>= 0 && allow_slaac_only
< 10)
121 allow_slaac_only
= 10;
126 int prefix_length
= strtoul(optarg
, &iaid_begin
, 10);
128 if (*iaid_begin
!= '\0' && *iaid_begin
!= ',' && *iaid_begin
!= ':') {
129 syslog(LOG_ERR
, "invalid argument: '%s'", optarg
);
133 struct odhcp6c_request_prefix prefix
= { 0, prefix_length
};
135 if (*iaid_begin
== ',' && (iaid_len
= strlen(iaid_begin
)) > 1)
136 memcpy(&prefix
.iaid
, iaid_begin
+ 1, iaid_len
> 4 ? 4 : iaid_len
);
137 else if (*iaid_begin
== ':')
138 prefix
.iaid
= htonl((uint32_t)strtoul(&iaid_begin
[1], NULL
, 16));
140 prefix
.iaid
= htonl(++ia_pd_iaid_index
);
142 odhcp6c_add_state(STATE_IA_PD_INIT
, &prefix
, sizeof(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 odhcp6c_add_state(STATE_CLIENT_ID
, buf
, l
+ 4);
165 if (inet_pton(AF_INET6
, optarg
, &ifid
) != 1)
172 opttype
= htons(strtoul(optarg
, &optpos
, 10));
173 if (optpos
== optarg
)
177 odhcp6c_add_state(STATE_ORO
, &opttype
, 2);
182 client_options
|= DHCPV6_STRICT_OPTIONS
;
186 optlen
= htons(strlen(optarg
));
187 odhcp6c_add_state(STATE_USERCLASS
, &optlen
, 2);
188 odhcp6c_add_state(STATE_USERCLASS
, optarg
, strlen(optarg
));
200 sol_timeout
= atoi(optarg
);
204 min_update_interval
= atoi(optarg
);
208 ra_options
&= ~RA_RDNSS_DEFAULT_LIFETIME
;
212 logopt
|= LOG_PERROR
;
224 client_options
&= ~DHCPV6_CLIENT_FQDN
;
228 client_options
&= ~DHCPV6_ACCEPT_RECONFIGURE
;
241 if (allow_slaac_only
> 0)
242 script_sync_delay
= allow_slaac_only
;
244 openlog("odhcp6c", logopt
, LOG_DAEMON
);
246 setlogmask(LOG_UPTO(LOG_WARNING
));
248 ifname
= argv
[optind
];
253 signal(SIGIO
, sighandler
);
254 signal(SIGHUP
, sighandler
);
255 signal(SIGINT
, sighandler
);
256 signal(SIGTERM
, sighandler
);
257 signal(SIGUSR1
, sighandler
);
258 signal(SIGUSR2
, sighandler
);
260 if ((urandom_fd
= open("/dev/urandom", O_CLOEXEC
| O_RDONLY
)) < 0 ||
261 init_dhcpv6(ifname
, client_options
, sol_timeout
) ||
262 ra_init(ifname
, &ifid
, ra_options
) ||
263 script_init(script
, ifname
)) {
264 syslog(LOG_ERR
, "failed to initialize: %s", strerror(errno
));
269 openlog("odhcp6c", LOG_PID
, LOG_DAEMON
); // Disable LOG_PERROR
271 syslog(LOG_ERR
, "Failed to daemonize: %s",
277 snprintf((char*)buf
, sizeof(buf
), "/var/run/odhcp6c.%s.pid", ifname
);
278 pidfile
= (char*)buf
;
281 FILE *fp
= fopen(pidfile
, "w");
283 fprintf(fp
, "%i\n", getpid());
288 script_call("started", 0, false);
290 while (!signal_term
) { // Main logic
291 odhcp6c_clear_state(STATE_SERVER_ID
);
292 odhcp6c_clear_state(STATE_SERVER_ADDR
);
293 odhcp6c_clear_state(STATE_IA_NA
);
294 odhcp6c_clear_state(STATE_IA_PD
);
295 odhcp6c_clear_state(STATE_SNTP_IP
);
296 odhcp6c_clear_state(STATE_NTP_IP
);
297 odhcp6c_clear_state(STATE_NTP_FQDN
);
298 odhcp6c_clear_state(STATE_SIP_IP
);
299 odhcp6c_clear_state(STATE_SIP_FQDN
);
302 syslog(LOG_NOTICE
, "(re)starting transaction on %s", ifname
);
304 signal_usr1
= signal_usr2
= false;
305 int mode
= dhcpv6_set_ia_mode(ia_na_mode
, ia_pd_mode
);
306 if (mode
!= DHCPV6_STATELESS
)
307 mode
= dhcpv6_request(DHCPV6_MSG_SOLICIT
);
308 odhcp6c_signal_process();
314 int res
= dhcpv6_request(mode
== DHCPV6_STATELESS
?
315 DHCPV6_MSG_INFO_REQ
: DHCPV6_MSG_REQUEST
);
316 bool signalled
= odhcp6c_signal_process();
320 else if (signalled
) {
325 mode
= dhcpv6_promote_server_cand();
326 } while (mode
> DHCPV6_UNKNOWN
);
332 case DHCPV6_STATELESS
:
334 syslog(LOG_NOTICE
, "entering stateless-mode on %s", ifname
);
336 while (!signal_usr2
&& !signal_term
) {
338 script_call("informed", script_sync_delay
, true);
340 int res
= dhcpv6_poll_reconfigure();
341 odhcp6c_signal_process();
347 signal_usr1
= false; // Acknowledged
350 if (signal_usr2
|| signal_term
)
353 res
= dhcpv6_request(DHCPV6_MSG_INFO_REQ
);
354 odhcp6c_signal_process();
362 case DHCPV6_STATEFUL
:
364 script_call("bound", script_sync_delay
, true);
365 syslog(LOG_NOTICE
, "entering stateful-mode on %s", ifname
);
367 while (!signal_usr2
&& !signal_term
) {
369 // Wait for T1 to expire or until we get a reconfigure
370 int res
= dhcpv6_poll_reconfigure();
371 odhcp6c_signal_process();
373 script_call("updated", 0, false);
377 // Handle signal, if necessary
379 signal_usr1
= false; // Acknowledged
380 if (signal_usr2
|| signal_term
)
381 break; // Other signal type
383 // Send renew as T1 expired
384 res
= dhcpv6_request(DHCPV6_MSG_RENEW
);
385 odhcp6c_signal_process();
386 if (res
> 0) { // Renew was succesfull
388 script_call("updated", 0, false);
389 continue; // Renew was successful
392 odhcp6c_clear_state(STATE_SERVER_ID
); // Remove binding
393 odhcp6c_clear_state(STATE_SERVER_ADDR
);
395 size_t ia_pd_len
, ia_na_len
;
396 odhcp6c_get_state(STATE_IA_PD
, &ia_pd_len
);
397 odhcp6c_get_state(STATE_IA_NA
, &ia_na_len
);
399 if (ia_pd_len
== 0 && ia_na_len
== 0)
402 // If we have IAs, try rebind otherwise restart
403 res
= dhcpv6_request(DHCPV6_MSG_REBIND
);
404 odhcp6c_signal_process();
407 script_call("rebound", 0, true);
420 size_t ia_pd_len
, ia_na_len
, server_id_len
;
421 odhcp6c_get_state(STATE_IA_PD
, &ia_pd_len
);
422 odhcp6c_get_state(STATE_IA_NA
, &ia_na_len
);
423 odhcp6c_get_state(STATE_SERVER_ID
, &server_id_len
);
425 // Add all prefixes to lost prefixes
427 script_call("unbound", 0, true);
429 if (server_id_len
> 0 && (ia_pd_len
> 0 || ia_na_len
> 0) && release
)
430 dhcpv6_request(DHCPV6_MSG_RELEASE
);
432 odhcp6c_clear_state(STATE_IA_NA
);
433 odhcp6c_clear_state(STATE_IA_PD
);
436 script_call("stopped", 0, true);
441 static int usage(void)
444 "Usage: odhcp6c [options] <interface>\n"
445 "\nFeature options:\n"
446 " -S <time> Wait at least <time> sec for a DHCP-server (0)\n"
447 " -N <mode> Mode for requesting addresses [try|force|none]\n"
448 " -P <length> Request IPv6-Prefix (0 = auto)\n"
449 " -F Force IPv6-Prefix\n"
450 " -V <class> Set vendor-class option (base-16 encoded)\n"
451 " -u <user-class> Set user-class option string\n"
452 " -c <clientid> Override client-ID (base-16 encoded 16-bit type + value)\n"
453 " -i <iface-id> Use a custom interface identifier for RA handling\n"
454 " -r <options> Options to be requested (comma-separated)\n"
455 " -R Do not request any options except those specified with -r\n"
456 " -s <script> Status update script (/usr/sbin/odhcp6c-update)\n"
457 " -a Don't send Accept Reconfigure option\n"
458 " -f Don't send Client FQDN option\n"
459 " -k Don't send a RELEASE when stopping\n"
460 " -t <seconds> Maximum timeout for DHCPv6-SOLICIT (120)\n"
461 " -m <seconds> Minimum time between accepting updates (30)\n"
462 " -L Ignore default lifetime for RDNSS records\n"
463 "\nInvocation options:\n"
464 " -p <pidfile> Set pidfile (/var/run/odhcp6c.pid)\n"
466 " -e Write logmessages to stderr\n"
467 " -v Increase logging verbosity\n"
468 " -h Show this help\n\n";
474 // Don't want to pull-in librt and libpthread just for a monotonic clock...
475 uint64_t odhcp6c_get_milli_time(void)
477 struct timespec t
= {0, 0};
478 syscall(SYS_clock_gettime
, CLOCK_MONOTONIC
, &t
);
479 return ((uint64_t)t
.tv_sec
) * 1000 + ((uint64_t)t
.tv_nsec
) / 1000000;
483 static uint8_t* odhcp6c_resize_state(enum odhcp6c_state state
, ssize_t len
)
486 return state_data
[state
] + state_len
[state
];
487 else if (state_len
[state
] + len
> 1024)
490 uint8_t *n
= realloc(state_data
[state
], state_len
[state
] + len
);
491 if (n
|| state_len
[state
] + len
== 0) {
492 state_data
[state
] = n
;
493 n
+= state_len
[state
];
494 state_len
[state
] += len
;
500 bool odhcp6c_signal_process(void)
505 bool ra_updated
= ra_process();
512 if (ra_updated
&& (bound
|| allow_slaac_only
>= 0)) {
513 script_call("ra-updated", (!ra
&& !bound
) ?
514 script_sync_delay
: script_accu_delay
, false);
519 return signal_usr1
|| signal_usr2
|| signal_term
;
523 void odhcp6c_clear_state(enum odhcp6c_state state
)
525 state_len
[state
] = 0;
529 void odhcp6c_add_state(enum odhcp6c_state state
, const void *data
, size_t len
)
531 uint8_t *n
= odhcp6c_resize_state(state
, len
);
533 memcpy(n
, data
, len
);
536 int odhcp6c_insert_state(enum odhcp6c_state state
, size_t offset
, const void *data
, size_t len
)
538 ssize_t len_after
= state_len
[state
] - offset
;
542 uint8_t *n
= odhcp6c_resize_state(state
, len
);
544 uint8_t *sdata
= state_data
[state
];
546 memmove(sdata
+ offset
+ len
, sdata
+ offset
, len_after
);
547 memcpy(sdata
+ offset
, data
, len
);
553 size_t odhcp6c_remove_state(enum odhcp6c_state state
, size_t offset
, size_t len
)
555 uint8_t *data
= state_data
[state
];
556 ssize_t len_after
= state_len
[state
] - (offset
+ len
);
558 return state_len
[state
];
560 memmove(data
+ offset
, data
+ offset
+ len
, len_after
);
561 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
;
577 void* odhcp6c_get_state(enum odhcp6c_state state
, size_t *len
)
579 *len
= state_len
[state
];
580 return state_data
[state
];
584 static struct odhcp6c_entry
* odhcp6c_find_entry(enum odhcp6c_state state
, const struct odhcp6c_entry
*new)
586 size_t len
, cmplen
= offsetof(struct odhcp6c_entry
, target
) + ((new->length
+ 7) / 8);
587 uint8_t *start
= odhcp6c_get_state(state
, &len
);
589 for (struct odhcp6c_entry
*c
= (struct odhcp6c_entry
*)start
;
590 (uint8_t*)c
< &start
[len
] &&
591 (uint8_t*)odhcp6c_next_entry(c
) <= &start
[len
];
592 c
= odhcp6c_next_entry(c
))
593 if (!memcmp(c
, new, cmplen
) && !memcmp(c
->auxtarget
, new->auxtarget
, new->auxlen
))
600 bool odhcp6c_update_entry(enum odhcp6c_state state
, struct odhcp6c_entry
*new,
601 uint32_t safe
, bool filterexcess
)
604 struct odhcp6c_entry
*x
= odhcp6c_find_entry(state
, new);
605 uint8_t *start
= odhcp6c_get_state(state
, &len
);
607 if (x
&& x
->valid
> new->valid
&& new->valid
< safe
)
610 if (new->valid
> 0) {
612 if (filterexcess
&& new->valid
>= x
->valid
&&
613 new->valid
!= UINT32_MAX
&&
614 new->valid
- x
->valid
< min_update_interval
&&
615 new->preferred
>= x
->preferred
&&
616 new->preferred
!= UINT32_MAX
&&
617 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));
628 odhcp6c_remove_state(state
, ((uint8_t*)x
) - start
, odhcp6c_entry_size(x
));
634 static void odhcp6c_expire_list(enum odhcp6c_state state
, uint32_t elapsed
)
637 uint8_t *start
= odhcp6c_get_state(state
, &len
);
638 for (struct odhcp6c_entry
*c
= (struct odhcp6c_entry
*)start
;
639 (uint8_t*)c
< &start
[len
] &&
640 (uint8_t*)odhcp6c_next_entry(c
) <= &start
[len
];
644 else if (c
->t1
!= UINT32_MAX
)
649 else if (c
->t2
!= UINT32_MAX
)
652 if (c
->preferred
< elapsed
)
654 else if (c
->preferred
!= UINT32_MAX
)
655 c
->preferred
-= elapsed
;
657 if (c
->valid
< elapsed
)
659 else if (c
->valid
!= UINT32_MAX
)
663 odhcp6c_remove_state(state
, ((uint8_t*)c
) - start
, odhcp6c_entry_size(c
));
664 start
= odhcp6c_get_state(state
, &len
);
666 c
= odhcp6c_next_entry(c
);
672 void odhcp6c_expire(void)
674 time_t now
= odhcp6c_get_milli_time() / 1000;
675 uint32_t elapsed
= (last_update
> 0) ? now
- last_update
: 0;
678 odhcp6c_expire_list(STATE_RA_PREFIX
, elapsed
);
679 odhcp6c_expire_list(STATE_RA_ROUTE
, elapsed
);
680 odhcp6c_expire_list(STATE_RA_DNS
, elapsed
);
681 odhcp6c_expire_list(STATE_RA_SEARCH
, elapsed
);
682 odhcp6c_expire_list(STATE_IA_NA
, elapsed
);
683 odhcp6c_expire_list(STATE_IA_PD
, elapsed
);
687 uint32_t odhcp6c_elapsed(void)
689 return odhcp6c_get_milli_time() / 1000 - last_update
;
693 int odhcp6c_random(void *buf
, size_t len
)
695 return read(urandom_fd
, buf
, len
);
698 bool odhcp6c_is_bound(void)
703 bool odhcp6c_addr_in_scope(const struct in6_addr
*addr
)
705 FILE *fd
= fopen("/proc/net/if_inet6", "r");
712 while (fgets(buf
, sizeof(buf
), fd
)) {
713 struct in6_addr inet6_addr
;
714 uint32_t flags
, dummy
;
716 char name
[IF_NAMESIZE
], addr_buf
[33];
720 if ((len
<= 0) || buf
[len
- 1] != '\n')
725 if (sscanf(buf
, "%s %x %x %x %x %s",
726 addr_buf
, &dummy
, &dummy
, &dummy
, &flags
, name
) != 6)
729 if (strcmp(name
, ifname
) ||
730 (flags
& (IFA_F_DADFAILED
| IFA_F_TENTATIVE
| IFA_F_DEPRECATED
)))
733 for (i
= 0; i
< strlen(addr_buf
); i
++) {
734 if (!isxdigit(addr_buf
[i
]) || isupper(addr_buf
[i
]))
738 memset(&inet6_addr
, 0, sizeof(inet6_addr
));
739 for (i
= 0; i
< (strlen(addr_buf
) / 2); i
++) {
741 static const char hex
[] = "0123456789abcdef";
742 byte
= ((index(hex
, addr_buf
[i
* 2]) - hex
) << 4) |
743 (index(hex
, addr_buf
[i
* 2 + 1]) - hex
);
744 inet6_addr
.s6_addr
[i
] = byte
;
747 if ((IN6_IS_ADDR_LINKLOCAL(&inet6_addr
) == IN6_IS_ADDR_LINKLOCAL(addr
)) &&
748 (IN6_IS_ADDR_UNIQUELOCAL(&inet6_addr
) == IN6_IS_ADDR_UNIQUELOCAL(addr
)))
754 static void sighandler(int signal
)
756 if (signal
== SIGUSR1
)
758 else if (signal
== SIGUSR2
)
760 else if (signal
== SIGIO
)