2 * Copyright (C) 2012-2014 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.
29 #include <sys/syscall.h>
30 #include <arpa/inet.h>
37 static void sighandler(int signal
);
38 static int usage(void);
40 static uint8_t *state_data
[_STATE_MAX
] = {NULL
};
41 static size_t state_len
[_STATE_MAX
] = {0};
43 static volatile bool signal_io
= false;
44 static volatile bool signal_usr1
= false;
45 static volatile bool signal_usr2
= false;
46 static volatile bool signal_term
= false;
48 static int urandom_fd
= -1, allow_slaac_only
= 0;
49 static bool bound
= false, release
= true;
50 static time_t last_update
= 0;
52 static unsigned int min_update_interval
= DEFAULT_MIN_UPDATE_INTERVAL
;
54 int main(_unused
int argc
, char* const argv
[])
56 // Allocate ressources
57 const char *pidfile
= NULL
;
58 const char *script
= "/usr/sbin/odhcp6c-update";
64 enum odhcp6c_ia_mode ia_na_mode
= IA_MODE_TRY
;
65 enum odhcp6c_ia_mode ia_pd_mode
= IA_MODE_NONE
;
66 int ia_pd_iaid_index
= 0;
67 static struct in6_addr ifid
= IN6ADDR_ANY_INIT
;
68 int sol_timeout
= DHCPV6_SOL_MAX_RT
;
72 bool help
= false, daemonize
= false;
75 unsigned int client_options
= DHCPV6_CLIENT_FQDN
| DHCPV6_ACCEPT_RECONFIGURE
;
77 while ((c
= getopt(argc
, argv
, "S::N:V:P:FB:c:i:r:Ru:s:kt:m:hedp:fav")) != -1) {
80 allow_slaac_only
= (optarg
) ? atoi(optarg
) : -1;
84 if (!strcmp(optarg
, "force")) {
85 ia_na_mode
= IA_MODE_FORCE
;
86 allow_slaac_only
= -1;
87 } else if (!strcmp(optarg
, "none")) {
88 ia_na_mode
= IA_MODE_NONE
;
89 } else if (!strcmp(optarg
, "try")) {
90 ia_na_mode
= IA_MODE_TRY
;
97 l
= script_unhexlify(buf
, sizeof(buf
), optarg
);
101 odhcp6c_add_state(STATE_VENDORCLASS
, buf
, l
);
105 if (ia_pd_mode
== IA_MODE_NONE
)
106 ia_pd_mode
= IA_MODE_TRY
;
108 if (allow_slaac_only
>= 0 && allow_slaac_only
< 10)
109 allow_slaac_only
= 10;
114 int prefix_length
= strtoul(optarg
, &iaid_begin
, 10);
116 if (*iaid_begin
!= '\0' && *iaid_begin
!= ',' && *iaid_begin
!= ':') {
117 syslog(LOG_ERR
, "invalid argument: '%s'", optarg
);
121 struct odhcp6c_request_prefix prefix
= { 0, prefix_length
};
123 if (*iaid_begin
== ',' && (iaid_len
= strlen(iaid_begin
)) > 1)
124 memcpy(&prefix
.iaid
, iaid_begin
+ 1, iaid_len
> 4 ? 4 : iaid_len
);
125 else if (*iaid_begin
== ':')
126 prefix
.iaid
= htonl((uint32_t)strtoul(&iaid_begin
[1], NULL
, 16));
128 prefix
.iaid
= htonl(++ia_pd_iaid_index
);
130 odhcp6c_add_state(STATE_IA_PD_INIT
, &prefix
, sizeof(prefix
));
135 allow_slaac_only
= -1;
136 ia_pd_mode
= IA_MODE_FORCE
;
140 l
= script_unhexlify(&buf
[4], sizeof(buf
) - 4, optarg
);
143 buf
[1] = DHCPV6_OPT_CLIENTID
;
146 odhcp6c_add_state(STATE_CLIENT_ID
, buf
, l
+ 4);
153 if (inet_pton(AF_INET6
, optarg
, &ifid
) != 1)
160 opttype
= htons(strtoul(optarg
, &optpos
, 10));
161 if (optpos
== optarg
)
165 odhcp6c_add_state(STATE_ORO
, &opttype
, 2);
170 client_options
|= DHCPV6_STRICT_OPTIONS
;
174 optlen
= htons(strlen(optarg
));
175 odhcp6c_add_state(STATE_USERCLASS
, &optlen
, 2);
176 odhcp6c_add_state(STATE_USERCLASS
, optarg
, strlen(optarg
));
188 sol_timeout
= atoi(optarg
);
192 min_update_interval
= atoi(optarg
);
196 logopt
|= LOG_PERROR
;
208 client_options
&= ~DHCPV6_CLIENT_FQDN
;
212 client_options
&= ~DHCPV6_ACCEPT_RECONFIGURE
;
225 openlog("odhcp6c", logopt
, LOG_DAEMON
);
227 setlogmask(LOG_UPTO(LOG_WARNING
));
229 const char *ifname
= argv
[optind
];
234 signal(SIGIO
, sighandler
);
235 signal(SIGHUP
, sighandler
);
236 signal(SIGINT
, sighandler
);
237 signal(SIGCHLD
, sighandler
);
238 signal(SIGTERM
, sighandler
);
239 signal(SIGUSR1
, sighandler
);
240 signal(SIGUSR2
, sighandler
);
242 if ((urandom_fd
= open("/dev/urandom", O_CLOEXEC
| O_RDONLY
)) < 0 ||
243 init_dhcpv6(ifname
, client_options
, sol_timeout
) ||
244 ra_init(ifname
, &ifid
) || script_init(script
, ifname
)) {
245 syslog(LOG_ERR
, "failed to initialize: %s", strerror(errno
));
250 openlog("odhcp6c", LOG_PID
, LOG_DAEMON
); // Disable LOG_PERROR
252 syslog(LOG_ERR
, "Failed to daemonize: %s",
259 snprintf(pidbuf
, sizeof(pidbuf
),
260 "/var/run/odhcp6c.%s.pid", ifname
);
264 int fd
= open(pidfile
, O_WRONLY
| O_CREAT
, 0644);
267 int len
= snprintf(buf
, sizeof(buf
), "%i\n", getpid());
273 script_call("started");
275 while (!signal_term
) { // Main logic
276 odhcp6c_clear_state(STATE_SERVER_ID
);
277 odhcp6c_clear_state(STATE_SERVER_ADDR
);
278 odhcp6c_clear_state(STATE_IA_NA
);
279 odhcp6c_clear_state(STATE_IA_PD
);
280 odhcp6c_clear_state(STATE_SNTP_IP
);
281 odhcp6c_clear_state(STATE_NTP_IP
);
282 odhcp6c_clear_state(STATE_NTP_FQDN
);
283 odhcp6c_clear_state(STATE_SIP_IP
);
284 odhcp6c_clear_state(STATE_SIP_FQDN
);
285 dhcpv6_set_ia_mode(ia_na_mode
, ia_pd_mode
);
288 syslog(LOG_NOTICE
, "(re)starting transaction on %s", ifname
);
290 signal_usr1
= signal_usr2
= false;
291 int mode
= dhcpv6_request(DHCPV6_MSG_SOLICIT
);
292 odhcp6c_signal_process();
298 int res
= dhcpv6_request(mode
== DHCPV6_STATELESS
?
299 DHCPV6_MSG_INFO_REQ
: DHCPV6_MSG_REQUEST
);
300 bool signalled
= odhcp6c_signal_process();
304 else if (signalled
) {
309 mode
= dhcpv6_promote_server_cand();
310 } while (mode
> DHCPV6_UNKNOWN
);
316 case DHCPV6_STATELESS
:
318 syslog(LOG_NOTICE
, "entering stateless-mode on %s", ifname
);
320 while (!signal_usr2
&& !signal_term
) {
322 script_call("informed");
324 int res
= dhcpv6_poll_reconfigure();
325 odhcp6c_signal_process();
331 signal_usr1
= false; // Acknowledged
334 if (signal_usr2
|| signal_term
)
337 res
= dhcpv6_request(DHCPV6_MSG_INFO_REQ
);
338 odhcp6c_signal_process();
346 case DHCPV6_STATEFUL
:
348 script_call("bound");
349 syslog(LOG_NOTICE
, "entering stateful-mode on %s", ifname
);
351 while (!signal_usr2
&& !signal_term
) {
353 // Wait for T1 to expire or until we get a reconfigure
354 int res
= dhcpv6_poll_reconfigure();
355 odhcp6c_signal_process();
357 script_call("updated");
361 // Handle signal, if necessary
363 signal_usr1
= false; // Acknowledged
364 if (signal_usr2
|| signal_term
)
365 break; // Other signal type
367 // Send renew as T1 expired
368 res
= dhcpv6_request(DHCPV6_MSG_RENEW
);
369 odhcp6c_signal_process();
370 if (res
> 0) { // Renew was succesfull
372 script_call("updated");
373 continue; // Renew was successful
376 odhcp6c_clear_state(STATE_SERVER_ID
); // Remove binding
377 odhcp6c_clear_state(STATE_SERVER_ADDR
);
379 size_t ia_pd_len
, ia_na_len
;
380 odhcp6c_get_state(STATE_IA_PD
, &ia_pd_len
);
381 odhcp6c_get_state(STATE_IA_NA
, &ia_na_len
);
383 if (ia_pd_len
== 0 && ia_na_len
== 0)
386 // If we have IAs, try rebind otherwise restart
387 res
= dhcpv6_request(DHCPV6_MSG_REBIND
);
388 odhcp6c_signal_process();
391 script_call("rebound");
402 size_t ia_pd_len
, ia_na_len
, server_id_len
;
403 odhcp6c_get_state(STATE_IA_PD
, &ia_pd_len
);
404 odhcp6c_get_state(STATE_IA_NA
, &ia_na_len
);
405 odhcp6c_get_state(STATE_SERVER_ID
, &server_id_len
);
407 // Add all prefixes to lost prefixes
409 script_call("unbound");
411 if (server_id_len
> 0 && (ia_pd_len
> 0 || ia_na_len
> 0) && release
)
412 dhcpv6_request(DHCPV6_MSG_RELEASE
);
414 odhcp6c_clear_state(STATE_IA_NA
);
415 odhcp6c_clear_state(STATE_IA_PD
);
418 script_call("stopped");
423 static int usage(void)
426 "Usage: odhcp6c [options] <interface>\n"
427 "\nFeature options:\n"
428 " -S <time> Wait at least <time> sec for a DHCP-server (0)\n"
429 " -N <mode> Mode for requesting addresses [try|force|none]\n"
430 " -P <length> Request IPv6-Prefix (0 = auto)\n"
431 " -F Force IPv6-Prefix\n"
432 " -V <class> Set vendor-class option (base-16 encoded)\n"
433 " -u <user-class> Set user-class option string\n"
434 " -c <clientid> Override client-ID (base-16 encoded)\n"
435 " -i <iface-id> Use a custom interface identifier for RA handling\n"
436 " -r <options> Options to be requested (comma-separated)\n"
437 " -R Do not request any options except those specified with -r\n"
438 " -s <script> Status update script (/usr/sbin/odhcp6c-update)\n"
439 " -a Don't send Accept Reconfigure option\n"
440 " -f Don't send Client FQDN option\n"
441 " -k Don't send a RELEASE when stopping\n"
442 " -t <seconds> Maximum timeout for DHCPv6-SOLICIT (3600)\n"
443 " -m <seconds> Minimum time between accepting updates (30)\n"
444 "\nInvocation options:\n"
445 " -p <pidfile> Set pidfile (/var/run/odhcp6c.pid)\n"
447 " -e Write logmessages to stderr\n"
448 " -v Increase logging verbosity\n"
449 " -h Show this help\n\n";
450 write(STDERR_FILENO
, buf
, sizeof(buf
));
455 // Don't want to pull-in librt and libpthread just for a monotonic clock...
456 uint64_t odhcp6c_get_milli_time(void)
458 struct timespec t
= {0, 0};
459 syscall(SYS_clock_gettime
, CLOCK_MONOTONIC
, &t
);
460 return ((uint64_t)t
.tv_sec
) * 1000 + ((uint64_t)t
.tv_nsec
) / 1000000;
464 static uint8_t* odhcp6c_resize_state(enum odhcp6c_state state
, ssize_t len
)
467 return state_data
[state
] + state_len
[state
];
468 else if (state_len
[state
] + len
> 1024)
471 uint8_t *n
= realloc(state_data
[state
], state_len
[state
] + len
);
472 if (n
|| state_len
[state
] + len
== 0) {
473 state_data
[state
] = n
;
474 n
+= state_len
[state
];
475 state_len
[state
] += len
;
481 bool odhcp6c_signal_process(void)
486 bool ra_updated
= ra_process();
491 if (ra_updated
&& (bound
|| allow_slaac_only
>= 0))
492 script_call("ra-updated"); // Immediate process urgent events
495 return signal_usr1
|| signal_usr2
|| signal_term
;
499 void odhcp6c_clear_state(enum odhcp6c_state state
)
501 state_len
[state
] = 0;
505 void odhcp6c_add_state(enum odhcp6c_state state
, const void *data
, size_t len
)
507 uint8_t *n
= odhcp6c_resize_state(state
, len
);
509 memcpy(n
, data
, len
);
512 void odhcp6c_insert_state(enum odhcp6c_state state
, size_t offset
, const void *data
, size_t len
)
514 ssize_t len_after
= state_len
[state
] - offset
;
518 uint8_t *n
= odhcp6c_resize_state(state
, len
);
520 uint8_t *sdata
= state_data
[state
];
522 memmove(sdata
+ offset
+ len
, sdata
+ offset
, len_after
);
523 memcpy(sdata
+ offset
, data
, len
);
527 size_t odhcp6c_remove_state(enum odhcp6c_state state
, size_t offset
, size_t len
)
529 uint8_t *data
= state_data
[state
];
530 ssize_t len_after
= state_len
[state
] - (offset
+ len
);
532 return state_len
[state
];
534 memmove(data
+ offset
, data
+ offset
+ len
, len_after
);
535 return state_len
[state
] -= len
;
539 void* odhcp6c_move_state(enum odhcp6c_state state
, size_t *len
)
541 *len
= state_len
[state
];
542 void *data
= state_data
[state
];
544 state_len
[state
] = 0;
545 state_data
[state
] = NULL
;
551 void* odhcp6c_get_state(enum odhcp6c_state state
, size_t *len
)
553 *len
= state_len
[state
];
554 return state_data
[state
];
558 struct odhcp6c_entry
* odhcp6c_find_entry(enum odhcp6c_state state
, const struct odhcp6c_entry
*new)
560 size_t len
, cmplen
= offsetof(struct odhcp6c_entry
, target
) + new->length
/ 8;
561 struct odhcp6c_entry
*start
= odhcp6c_get_state(state
, &len
);
562 struct odhcp6c_entry
*x
= NULL
;
564 for (struct odhcp6c_entry
*c
= start
; !x
&& c
< &start
[len
/sizeof(*c
)]; ++c
)
565 if (!memcmp(c
, new, cmplen
))
572 bool odhcp6c_update_entry_safe(enum odhcp6c_state state
, struct odhcp6c_entry
*new, uint32_t safe
)
575 struct odhcp6c_entry
*x
= odhcp6c_find_entry(state
, new);
576 struct odhcp6c_entry
*start
= odhcp6c_get_state(state
, &len
);
578 if (x
&& x
->valid
> new->valid
&& new->valid
< safe
)
581 if (new->valid
> 0) {
583 if (new->valid
>= x
->valid
&& new->valid
!= UINT32_MAX
&&
584 new->valid
- x
->valid
< min_update_interval
&&
585 new->preferred
>= x
->preferred
&&
586 new->preferred
!= UINT32_MAX
&&
587 new->preferred
- x
->preferred
< min_update_interval
&&
588 x
->class == new->class)
590 x
->valid
= new->valid
;
591 x
->preferred
= new->preferred
;
594 x
->class = new->class;
597 odhcp6c_add_state(state
, new, sizeof(*new));
600 odhcp6c_remove_state(state
, (x
- start
) * sizeof(*x
), sizeof(*x
));
606 bool odhcp6c_update_entry(enum odhcp6c_state state
, struct odhcp6c_entry
*new)
608 return odhcp6c_update_entry_safe(state
, new, 0);
612 static void odhcp6c_expire_list(enum odhcp6c_state state
, uint32_t elapsed
)
615 struct odhcp6c_entry
*start
= odhcp6c_get_state(state
, &len
);
616 for (struct odhcp6c_entry
*c
= start
; c
< &start
[len
/ sizeof(*c
)]; ++c
) {
619 else if (c
->t1
!= UINT32_MAX
)
624 else if (c
->t2
!= UINT32_MAX
)
627 if (c
->preferred
< elapsed
)
629 else if (c
->preferred
!= UINT32_MAX
)
630 c
->preferred
-= elapsed
;
632 if (c
->valid
< elapsed
)
634 else if (c
->valid
!= UINT32_MAX
)
638 odhcp6c_remove_state(state
, (c
- start
) * sizeof(*c
), sizeof(*c
));
643 void odhcp6c_expire(void)
645 time_t now
= odhcp6c_get_milli_time() / 1000;
646 uint32_t elapsed
= (last_update
> 0) ? now
- last_update
: 0;
649 odhcp6c_expire_list(STATE_RA_PREFIX
, elapsed
);
650 odhcp6c_expire_list(STATE_RA_ROUTE
, elapsed
);
651 odhcp6c_expire_list(STATE_RA_DNS
, elapsed
);
652 odhcp6c_expire_list(STATE_IA_NA
, elapsed
);
653 odhcp6c_expire_list(STATE_IA_PD
, elapsed
);
657 uint32_t odhcp6c_elapsed(void)
659 return odhcp6c_get_milli_time() / 1000 - last_update
;
663 void odhcp6c_random(void *buf
, size_t len
)
665 read(urandom_fd
, buf
, len
);
668 bool odhcp6c_is_bound(void)
673 static void sighandler(int signal
)
675 if (signal
== SIGCHLD
)
676 while (waitpid(-1, NULL
, WNOHANG
) > 0);
677 else if (signal
== SIGUSR1
)
679 else if (signal
== SIGUSR2
)
681 else if (signal
== SIGIO
)