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 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
);
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 ra_holdoff_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
, ra_holdoff_interval
) ||
257 script_init(script
, ifname
)) {
258 syslog(LOG_ERR
, "failed to initialize: %m");
263 openlog("odhcp6c", LOG_PID
, LOG_DAEMON
); // Disable LOG_PERROR
265 syslog(LOG_ERR
, "Failed to daemonize: %m");
270 snprintf((char*)buf
, sizeof(buf
), "/var/run/odhcp6c.%s.pid", ifname
);
271 pidfile
= (char*)buf
;
274 FILE *fp
= fopen(pidfile
, "w");
276 fprintf(fp
, "%i\n", getpid());
281 script_call("started", 0, false);
283 while (!signal_term
) { // Main logic
284 odhcp6c_clear_state(STATE_SERVER_ID
);
285 odhcp6c_clear_state(STATE_SERVER_ADDR
);
286 odhcp6c_clear_state(STATE_IA_NA
);
287 odhcp6c_clear_state(STATE_IA_PD
);
288 odhcp6c_clear_state(STATE_SNTP_IP
);
289 odhcp6c_clear_state(STATE_NTP_IP
);
290 odhcp6c_clear_state(STATE_NTP_FQDN
);
291 odhcp6c_clear_state(STATE_SIP_IP
);
292 odhcp6c_clear_state(STATE_SIP_FQDN
);
295 syslog(LOG_NOTICE
, "(re)starting transaction on %s", ifname
);
297 signal_usr1
= signal_usr2
= false;
298 int mode
= dhcpv6_set_ia_mode(ia_na_mode
, ia_pd_mode
);
299 if (mode
!= DHCPV6_STATELESS
)
300 mode
= dhcpv6_request(DHCPV6_MSG_SOLICIT
);
302 odhcp6c_signal_process();
308 int res
= dhcpv6_request(mode
== DHCPV6_STATELESS
?
309 DHCPV6_MSG_INFO_REQ
: DHCPV6_MSG_REQUEST
);
310 bool signalled
= odhcp6c_signal_process();
314 else if (signalled
) {
319 mode
= dhcpv6_promote_server_cand();
320 } while (mode
> DHCPV6_UNKNOWN
);
326 case DHCPV6_STATELESS
:
328 syslog(LOG_NOTICE
, "entering stateless-mode on %s", ifname
);
330 while (!signal_usr2
&& !signal_term
) {
332 script_call("informed", script_sync_delay
, true);
334 int res
= dhcpv6_poll_reconfigure();
335 odhcp6c_signal_process();
341 signal_usr1
= false; // Acknowledged
345 if (signal_usr2
|| signal_term
)
348 res
= dhcpv6_request(DHCPV6_MSG_INFO_REQ
);
349 odhcp6c_signal_process();
358 case DHCPV6_STATEFUL
:
360 script_call("bound", script_sync_delay
, true);
361 syslog(LOG_NOTICE
, "entering stateful-mode on %s", ifname
);
363 while (!signal_usr2
&& !signal_term
) {
365 // Wait for T1 to expire or until we get a reconfigure
366 int res
= dhcpv6_poll_reconfigure();
367 odhcp6c_signal_process();
369 script_call("updated", 0, false);
373 // Handle signal, if necessary
375 signal_usr1
= false; // Acknowledged
377 if (signal_usr2
|| signal_term
)
378 break; // Other signal type
380 // Send renew as T1 expired
381 res
= dhcpv6_request(DHCPV6_MSG_RENEW
);
382 odhcp6c_signal_process();
384 if (res
> 0) { // Renew was succesfull
386 script_call("updated", 0, false);
387 continue; // Renew was successful
390 odhcp6c_clear_state(STATE_SERVER_ID
); // Remove binding
391 odhcp6c_clear_state(STATE_SERVER_ADDR
);
393 size_t ia_pd_len
, ia_na_len
;
394 odhcp6c_get_state(STATE_IA_PD
, &ia_pd_len
);
395 odhcp6c_get_state(STATE_IA_NA
, &ia_na_len
);
397 if (ia_pd_len
== 0 && ia_na_len
== 0)
400 // If we have IAs, try rebind otherwise restart
401 res
= dhcpv6_request(DHCPV6_MSG_REBIND
);
402 odhcp6c_signal_process();
405 script_call("rebound", 0, true);
417 size_t ia_pd_len
, ia_na_len
, server_id_len
;
418 odhcp6c_get_state(STATE_IA_PD
, &ia_pd_len
);
419 odhcp6c_get_state(STATE_IA_NA
, &ia_na_len
);
420 odhcp6c_get_state(STATE_SERVER_ID
, &server_id_len
);
422 // Add all prefixes to lost prefixes
424 script_call("unbound", 0, true);
426 if (server_id_len
> 0 && (ia_pd_len
> 0 || ia_na_len
> 0) && release
)
427 dhcpv6_request(DHCPV6_MSG_RELEASE
);
429 odhcp6c_clear_state(STATE_IA_NA
);
430 odhcp6c_clear_state(STATE_IA_PD
);
433 script_call("stopped", 0, true);
438 static int usage(void)
441 "Usage: odhcp6c [options] <interface>\n"
442 "\nFeature options:\n"
443 " -S <time> Wait at least <time> sec for a DHCP-server (0)\n"
444 " -N <mode> Mode for requesting addresses [try|force|none]\n"
445 " -P <length> Request IPv6-Prefix (0 = auto)\n"
446 " -F Force IPv6-Prefix\n"
447 " -V <class> Set vendor-class option (base-16 encoded)\n"
448 " -u <user-class> Set user-class option string\n"
449 " -c <clientid> Override client-ID (base-16 encoded 16-bit type + value)\n"
450 " -i <iface-id> Use a custom interface identifier for RA handling\n"
451 " -r <options> Options to be requested (comma-separated)\n"
452 " -R Do not request any options except those specified with -r\n"
453 " -s <script> Status update script (/usr/sbin/odhcp6c-update)\n"
454 " -a Don't send Accept Reconfigure option\n"
455 " -f Don't send Client FQDN option\n"
456 " -k Don't send a RELEASE when stopping\n"
457 " -t <seconds> Maximum timeout for DHCPv6-SOLICIT (120)\n"
458 " -m <seconds> Minimum time between accepting RA updates (3)\n"
459 " -L Ignore default lifetime for RDNSS records\n"
460 "\nInvocation options:\n"
461 " -p <pidfile> Set pidfile (/var/run/odhcp6c.pid)\n"
463 " -e Write logmessages to stderr\n"
464 " -v Increase logging verbosity\n"
465 " -h Show this help\n\n";
471 // Don't want to pull-in librt and libpthread just for a monotonic clock...
472 uint64_t odhcp6c_get_milli_time(void)
474 struct timespec t
= {0, 0};
475 syscall(SYS_clock_gettime
, CLOCK_MONOTONIC
, &t
);
477 return ((uint64_t)t
.tv_sec
) * 1000 + ((uint64_t)t
.tv_nsec
) / 1000000;
480 static uint8_t* odhcp6c_resize_state(enum odhcp6c_state state
, ssize_t len
)
483 return state_data
[state
] + state_len
[state
];
484 else if (state_len
[state
] + len
> 1024)
487 uint8_t *n
= realloc(state_data
[state
], state_len
[state
] + len
);
489 if (n
|| state_len
[state
] + len
== 0) {
490 state_data
[state
] = n
;
491 n
+= state_len
[state
];
492 state_len
[state
] += len
;
498 bool odhcp6c_signal_process(void)
503 bool ra_updated
= ra_process();
510 if (ra_updated
&& (bound
|| allow_slaac_only
>= 0)) {
511 script_call("ra-updated", (!ra
&& !bound
) ?
512 script_sync_delay
: script_accu_delay
, false);
517 return signal_usr1
|| signal_usr2
|| signal_term
;
520 void odhcp6c_clear_state(enum odhcp6c_state state
)
522 state_len
[state
] = 0;
525 void odhcp6c_add_state(enum odhcp6c_state state
, const void *data
, size_t len
)
527 uint8_t *n
= odhcp6c_resize_state(state
, len
);
530 memcpy(n
, data
, len
);
533 int odhcp6c_insert_state(enum odhcp6c_state state
, size_t offset
, const void *data
, size_t len
)
535 ssize_t len_after
= state_len
[state
] - offset
;
539 uint8_t *n
= odhcp6c_resize_state(state
, len
);
542 uint8_t *sdata
= state_data
[state
];
544 memmove(sdata
+ offset
+ len
, sdata
+ offset
, len_after
);
545 memcpy(sdata
+ offset
, data
, len
);
551 size_t odhcp6c_remove_state(enum odhcp6c_state state
, size_t offset
, size_t len
)
553 uint8_t *data
= state_data
[state
];
554 ssize_t len_after
= state_len
[state
] - (offset
+ len
);
557 return state_len
[state
];
559 memmove(data
+ offset
, data
+ offset
+ len
, len_after
);
561 return state_len
[state
] -= len
;
564 void* odhcp6c_move_state(enum odhcp6c_state state
, size_t *len
)
566 *len
= state_len
[state
];
567 void *data
= state_data
[state
];
569 state_len
[state
] = 0;
570 state_data
[state
] = NULL
;
575 void* odhcp6c_get_state(enum odhcp6c_state state
, size_t *len
)
577 *len
= state_len
[state
];
579 return state_data
[state
];
582 static struct odhcp6c_entry
* odhcp6c_find_entry(enum odhcp6c_state state
, const struct odhcp6c_entry
*new)
584 size_t len
, cmplen
= offsetof(struct odhcp6c_entry
, target
) + ((new->length
+ 7) / 8);
585 uint8_t *start
= odhcp6c_get_state(state
, &len
);
587 for (struct odhcp6c_entry
*c
= (struct odhcp6c_entry
*)start
;
588 (uint8_t*)c
< &start
[len
] &&
589 (uint8_t*)odhcp6c_next_entry(c
) <= &start
[len
];
590 c
= odhcp6c_next_entry(c
)) {
591 if (!memcmp(c
, new, cmplen
) && !memcmp(c
->auxtarget
, new->auxtarget
, new->auxlen
))
598 bool odhcp6c_update_entry(enum odhcp6c_state state
, struct odhcp6c_entry
*new,
599 uint32_t safe
, unsigned int holdoff_interval
)
602 struct odhcp6c_entry
*x
= odhcp6c_find_entry(state
, new);
603 uint8_t *start
= odhcp6c_get_state(state
, &len
);
605 if (x
&& x
->valid
> new->valid
&& new->valid
< safe
)
608 if (new->valid
> 0) {
610 if (holdoff_interval
&& new->valid
>= x
->valid
&&
611 new->valid
!= UINT32_MAX
&&
612 new->valid
- x
->valid
< holdoff_interval
&&
613 new->preferred
>= x
->preferred
&&
614 new->preferred
!= UINT32_MAX
&&
615 new->preferred
- x
->preferred
< holdoff_interval
)
618 x
->valid
= new->valid
;
619 x
->preferred
= new->preferred
;
624 odhcp6c_add_state(state
, new, odhcp6c_entry_size(new));
626 odhcp6c_remove_state(state
, ((uint8_t*)x
) - start
, odhcp6c_entry_size(x
));
631 static void odhcp6c_expire_list(enum odhcp6c_state state
, uint32_t elapsed
)
634 uint8_t *start
= odhcp6c_get_state(state
, &len
);
636 for (struct odhcp6c_entry
*c
= (struct odhcp6c_entry
*)start
;
637 (uint8_t*)c
< &start
[len
] &&
638 (uint8_t*)odhcp6c_next_entry(c
) <= &start
[len
];
642 else if (c
->t1
!= UINT32_MAX
)
647 else if (c
->t2
!= UINT32_MAX
)
650 if (c
->preferred
< elapsed
)
652 else if (c
->preferred
!= UINT32_MAX
)
653 c
->preferred
-= elapsed
;
655 if (c
->valid
< elapsed
)
657 else if (c
->valid
!= UINT32_MAX
)
661 odhcp6c_remove_state(state
, ((uint8_t*)c
) - start
, odhcp6c_entry_size(c
));
662 start
= odhcp6c_get_state(state
, &len
);
664 c
= odhcp6c_next_entry(c
);
668 void odhcp6c_expire(void)
670 time_t now
= odhcp6c_get_milli_time() / 1000;
671 uint32_t elapsed
= (last_update
> 0) ? now
- last_update
: 0;
675 odhcp6c_expire_list(STATE_RA_PREFIX
, elapsed
);
676 odhcp6c_expire_list(STATE_RA_ROUTE
, elapsed
);
677 odhcp6c_expire_list(STATE_RA_DNS
, elapsed
);
678 odhcp6c_expire_list(STATE_RA_SEARCH
, elapsed
);
679 odhcp6c_expire_list(STATE_IA_NA
, elapsed
);
680 odhcp6c_expire_list(STATE_IA_PD
, elapsed
);
683 uint32_t odhcp6c_elapsed(void)
685 return odhcp6c_get_milli_time() / 1000 - last_update
;
688 int odhcp6c_random(void *buf
, size_t len
)
690 return read(urandom_fd
, buf
, len
);
693 bool odhcp6c_is_bound(void)
698 bool odhcp6c_addr_in_scope(const struct in6_addr
*addr
)
700 FILE *fd
= fopen("/proc/net/if_inet6", "r");
707 while (fgets(buf
, sizeof(buf
), fd
)) {
708 struct in6_addr inet6_addr
;
709 uint32_t flags
, dummy
;
711 char name
[IF_NAMESIZE
], addr_buf
[33];
715 if ((len
<= 0) || buf
[len
- 1] != '\n')
720 if (sscanf(buf
, "%s %x %x %x %x %s",
721 addr_buf
, &dummy
, &dummy
, &dummy
, &flags
, name
) != 6)
724 if (strcmp(name
, ifname
) ||
725 (flags
& (IFA_F_DADFAILED
| IFA_F_TENTATIVE
| IFA_F_DEPRECATED
)))
728 for (i
= 0; i
< strlen(addr_buf
); i
++) {
729 if (!isxdigit(addr_buf
[i
]) || isupper(addr_buf
[i
]))
733 memset(&inet6_addr
, 0, sizeof(inet6_addr
));
734 for (i
= 0; i
< (strlen(addr_buf
) / 2); i
++) {
736 static const char hex
[] = "0123456789abcdef";
737 byte
= ((index(hex
, addr_buf
[i
* 2]) - hex
) << 4) |
738 (index(hex
, addr_buf
[i
* 2 + 1]) - hex
);
739 inet6_addr
.s6_addr
[i
] = byte
;
742 if ((IN6_IS_ADDR_LINKLOCAL(&inet6_addr
) == IN6_IS_ADDR_LINKLOCAL(addr
)) &&
743 (IN6_IS_ADDR_UNIQUELOCAL(&inet6_addr
) == IN6_IS_ADDR_UNIQUELOCAL(addr
)))
750 static void sighandler(int signal
)
752 if (signal
== SIGUSR1
)
754 else if (signal
== SIGUSR2
)
756 else if (signal
== SIGIO
)