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>
40 static void sighandler(int signal
);
41 static int usage(void);
43 static uint8_t *state_data
[_STATE_MAX
] = {NULL
};
44 static size_t state_len
[_STATE_MAX
] = {0};
46 static volatile bool signal_io
= false;
47 static volatile bool signal_usr1
= false;
48 static volatile bool signal_usr2
= false;
49 static volatile bool signal_term
= false;
51 static int urandom_fd
= -1, allow_slaac_only
= 0;
52 static bool bound
= false, release
= true;
53 static time_t last_update
= 0;
55 int main(_unused
int argc
, char* const argv
[])
57 // Allocate ressources
58 const char *pidfile
= NULL
;
59 const char *script
= "/usr/sbin/odhcp6c-update";
65 enum odhcp6c_ia_mode ia_na_mode
= IA_MODE_TRY
;
66 enum odhcp6c_ia_mode ia_pd_mode
= IA_MODE_NONE
;
67 int ia_pd_iaid_index
= 0;
68 static struct in6_addr ifid
= IN6ADDR_ANY_INIT
;
69 int sol_timeout
= DHCPV6_SOL_MAX_RT
;
72 int bfd_interval
= 0, bfd_loss
= 3;
75 bool help
= false, daemonize
= false;
78 unsigned int client_options
= DHCPV6_CLIENT_FQDN
| DHCPV6_ACCEPT_RECONFIGURE
;
80 while ((c
= getopt(argc
, argv
, "S::N:V:P:FB:c:i:r:Ru:s:kt:hedp:fa")) != -1) {
83 allow_slaac_only
= (optarg
) ? atoi(optarg
) : -1;
87 if (!strcmp(optarg
, "force")) {
88 ia_na_mode
= IA_MODE_FORCE
;
89 allow_slaac_only
= -1;
90 } else if (!strcmp(optarg
, "none")) {
91 ia_na_mode
= IA_MODE_NONE
;
92 } else if (!strcmp(optarg
, "try")) {
93 ia_na_mode
= IA_MODE_TRY
;
100 l
= script_unhexlify(buf
, sizeof(buf
), optarg
);
104 odhcp6c_add_state(STATE_VENDORCLASS
, buf
, l
);
108 if (ia_pd_mode
== IA_MODE_NONE
)
109 ia_pd_mode
= IA_MODE_TRY
;
111 if (allow_slaac_only
>= 0 && allow_slaac_only
< 10)
112 allow_slaac_only
= 10;
117 int prefix_length
= strtoul(optarg
, &iaid_begin
, 10);
119 if (*iaid_begin
!= '\0' && *iaid_begin
!= ',' && *iaid_begin
!= ':') {
120 syslog(LOG_ERR
, "invalid argument: '%s'", optarg
);
124 struct odhcp6c_request_prefix prefix
= { 0, prefix_length
};
126 if (*iaid_begin
== ',' && (iaid_len
= strlen(iaid_begin
)) > 1)
127 memcpy(&prefix
.iaid
, iaid_begin
+ 1, iaid_len
> 4 ? 4 : iaid_len
);
128 else if (*iaid_begin
== ':')
129 prefix
.iaid
= htonl((uint32_t)strtoul(&iaid_begin
[1], NULL
, 16));
131 prefix
.iaid
= htonl(++ia_pd_iaid_index
);
133 odhcp6c_add_state(STATE_IA_PD_INIT
, &prefix
, sizeof(prefix
));
138 allow_slaac_only
= -1;
139 ia_pd_mode
= IA_MODE_FORCE
;
144 bfd_interval
= atoi(optarg
);
149 l
= script_unhexlify(&buf
[4], sizeof(buf
) - 4, optarg
);
152 buf
[1] = DHCPV6_OPT_CLIENTID
;
155 odhcp6c_add_state(STATE_CLIENT_ID
, buf
, l
+ 4);
162 if (inet_pton(AF_INET6
, optarg
, &ifid
) != 1)
169 opttype
= htons(strtoul(optarg
, &optpos
, 10));
170 if (optpos
== optarg
)
174 odhcp6c_add_state(STATE_ORO
, &opttype
, 2);
179 client_options
|= DHCPV6_STRICT_OPTIONS
;
183 optlen
= htons(strlen(optarg
));
184 odhcp6c_add_state(STATE_USERCLASS
, &optlen
, 2);
185 odhcp6c_add_state(STATE_USERCLASS
, optarg
, strlen(optarg
));
197 sol_timeout
= atoi(optarg
);
201 logopt
|= LOG_PERROR
;
213 client_options
&= ~DHCPV6_CLIENT_FQDN
;
217 client_options
&= ~DHCPV6_ACCEPT_RECONFIGURE
;
226 openlog("odhcp6c", logopt
, LOG_DAEMON
);
227 const char *ifname
= argv
[optind
];
232 signal(SIGIO
, sighandler
);
233 signal(SIGHUP
, sighandler
);
234 signal(SIGINT
, sighandler
);
235 signal(SIGCHLD
, sighandler
);
236 signal(SIGTERM
, sighandler
);
237 signal(SIGUSR1
, sighandler
);
238 signal(SIGUSR2
, sighandler
);
240 if ((urandom_fd
= open("/dev/urandom", O_CLOEXEC
| O_RDONLY
)) < 0 ||
241 init_dhcpv6(ifname
, client_options
, sol_timeout
) ||
242 ra_init(ifname
, &ifid
) || script_init(script
, ifname
)) {
243 syslog(LOG_ERR
, "failed to initialize: %s", strerror(errno
));
248 openlog("odhcp6c", LOG_PID
, LOG_DAEMON
); // Disable LOG_PERROR
250 syslog(LOG_ERR
, "Failed to daemonize: %s",
257 snprintf(pidbuf
, sizeof(pidbuf
),
258 "/var/run/odhcp6c.%s.pid", ifname
);
262 int fd
= open(pidfile
, O_WRONLY
| O_CREAT
);
265 int len
= snprintf(buf
, sizeof(buf
), "%i\n", getpid());
271 script_call("started");
273 while (!signal_term
) { // Main logic
274 odhcp6c_clear_state(STATE_SERVER_ID
);
275 odhcp6c_clear_state(STATE_IA_NA
);
276 odhcp6c_clear_state(STATE_IA_PD
);
277 odhcp6c_clear_state(STATE_SNTP_IP
);
278 odhcp6c_clear_state(STATE_NTP_IP
);
279 odhcp6c_clear_state(STATE_NTP_FQDN
);
280 odhcp6c_clear_state(STATE_SIP_IP
);
281 odhcp6c_clear_state(STATE_SIP_FQDN
);
282 dhcpv6_set_ia_mode(ia_na_mode
, ia_pd_mode
);
285 syslog(LOG_NOTICE
, "(re)starting transaction on %s", ifname
);
287 signal_usr1
= signal_usr2
= false;
288 int mode
= dhcpv6_request(DHCPV6_MSG_SOLICIT
);
289 odhcp6c_signal_process();
295 int res
= dhcpv6_request(mode
== DHCPV6_STATELESS
?
296 DHCPV6_MSG_INFO_REQ
: DHCPV6_MSG_REQUEST
);
297 bool signalled
= odhcp6c_signal_process();
301 else if (signalled
) {
306 mode
= dhcpv6_promote_server_cand();
307 } while (mode
> DHCPV6_UNKNOWN
);
313 case DHCPV6_STATELESS
:
315 syslog(LOG_NOTICE
, "entering stateless-mode on %s", ifname
);
317 while (!signal_usr2
&& !signal_term
) {
319 script_call("informed");
321 int res
= dhcpv6_poll_reconfigure();
322 odhcp6c_signal_process();
328 signal_usr1
= false; // Acknowledged
331 if (signal_usr2
|| signal_term
)
334 res
= dhcpv6_request(DHCPV6_MSG_INFO_REQ
);
335 odhcp6c_signal_process();
343 case DHCPV6_STATEFUL
:
344 script_call("bound");
346 syslog(LOG_NOTICE
, "entering stateful-mode on %s", ifname
);
348 if (bfd_interval
> 0)
349 bfd_start(ifname
, bfd_loss
, bfd_interval
);
352 while (!signal_usr2
&& !signal_term
) {
354 // Wait for T1 to expire or until we get a reconfigure
355 int res
= dhcpv6_poll_reconfigure();
356 odhcp6c_signal_process();
358 script_call("updated");
362 // Handle signal, if necessary
364 signal_usr1
= false; // Acknowledged
365 if (signal_usr2
|| signal_term
)
366 break; // Other signal type
368 // Send renew as T1 expired
369 size_t ia_pd_len
, ia_na_len
;
370 odhcp6c_get_state(STATE_IA_PD
, &ia_pd_len
);
371 odhcp6c_get_state(STATE_IA_NA
, &ia_na_len
);
373 // If we have any IAs, send renew, otherwise request
374 if (ia_pd_len
== 0 && ia_na_len
== 0)
375 res
= dhcpv6_request(DHCPV6_MSG_REQUEST
);
377 res
= dhcpv6_request(DHCPV6_MSG_RENEW
);
379 odhcp6c_signal_process();
380 if (res
> 0) { // Renew was succesfull
382 script_call("updated");
383 continue; // Renew was successful
386 odhcp6c_clear_state(STATE_SERVER_ID
); // Remove binding
388 // If we have IAs, try rebind otherwise restart
389 res
= dhcpv6_request(DHCPV6_MSG_REBIND
);
390 odhcp6c_signal_process();
393 script_call("rebound");
407 size_t ia_pd_len
, ia_na_len
, server_id_len
;
408 odhcp6c_get_state(STATE_IA_PD
, &ia_pd_len
);
409 odhcp6c_get_state(STATE_IA_NA
, &ia_na_len
);
410 odhcp6c_get_state(STATE_SERVER_ID
, &server_id_len
);
412 // Add all prefixes to lost prefixes
414 script_call("unbound");
416 if (server_id_len
> 0 && (ia_pd_len
> 0 || ia_na_len
> 0) && release
)
417 dhcpv6_request(DHCPV6_MSG_RELEASE
);
419 odhcp6c_clear_state(STATE_IA_NA
);
420 odhcp6c_clear_state(STATE_IA_PD
);
423 script_call("stopped");
428 static int usage(void)
431 "Usage: odhcp6c [options] <interface>\n"
432 "\nFeature options:\n"
433 " -S <time> Wait at least <time> sec for a DHCP-server (0)\n"
434 " -N <mode> Mode for requesting addresses [try|force|none]\n"
435 " -P <length> Request IPv6-Prefix (0 = auto)\n"
436 " -F Force IPv6-Prefix\n"
437 " -V <hex-string> Set vendor-class option. string length must be a multiple of 2\n"
439 " -B <interval> Enable BFD ping check\n"
441 " -c <clientid> Override client-ID (base-16 encoded)\n"
442 " -i <iface-id> Use a custom interface identifier for RA handling\n"
443 " -r <options> Options to be requested (comma-separated)\n"
444 " -R Do not request any options except those specified with -r\n"
445 " -s <script> Status update script (/usr/sbin/odhcp6c-update)\n"
446 " -a Don't send Accept Reconfigure option\n"
447 " -f Don't send Client FQDN option\n"
448 " -k Don't send a RELEASE when stopping\n"
449 " -t <seconds> Maximum timeout for DHCPv6-SOLICIT (120)\n"
450 "\nInvocation options:\n"
451 " -p <pidfile> Set pidfile (/var/run/odhcp6c.pid)\n"
453 " -e Write logmessages to stderr\n"
454 //" -v Increase logging verbosity\n"
455 " -h Show this help\n\n";
456 write(STDERR_FILENO
, buf
, sizeof(buf
));
461 // Don't want to pull-in librt and libpthread just for a monotonic clock...
462 uint64_t odhcp6c_get_milli_time(void)
464 struct timespec t
= {0, 0};
465 syscall(SYS_clock_gettime
, CLOCK_MONOTONIC
, &t
);
466 return t
.tv_sec
* 1000 + t
.tv_nsec
/ 1000000;
470 static uint8_t* odhcp6c_resize_state(enum odhcp6c_state state
, ssize_t len
)
473 return state_data
[state
] + state_len
[state
];
474 else if (state_len
[state
] + len
> 1024)
477 uint8_t *n
= realloc(state_data
[state
], state_len
[state
] + len
);
478 if (n
|| state_len
[state
] + len
== 0) {
479 state_data
[state
] = n
;
480 n
+= state_len
[state
];
481 state_len
[state
] += len
;
487 bool odhcp6c_signal_process(void)
492 bool ra_updated
= ra_process();
497 if (ra_updated
&& (bound
|| allow_slaac_only
== 0))
498 script_call("ra-updated"); // Immediate process urgent events
499 else if (ra_updated
&& !bound
&& allow_slaac_only
> 0)
500 script_delay_call("ra-updated", allow_slaac_only
);
507 return signal_usr1
|| signal_usr2
|| signal_term
;
511 void odhcp6c_clear_state(enum odhcp6c_state state
)
513 state_len
[state
] = 0;
517 void odhcp6c_add_state(enum odhcp6c_state state
, const void *data
, size_t len
)
519 uint8_t *n
= odhcp6c_resize_state(state
, len
);
521 memcpy(n
, data
, len
);
524 void odhcp6c_insert_state(enum odhcp6c_state state
, size_t offset
, const void *data
, size_t len
)
526 ssize_t len_after
= state_len
[state
] - offset
;
530 uint8_t *n
= odhcp6c_resize_state(state
, len
);
532 uint8_t *sdata
= state_data
[state
];
534 memmove(sdata
+ offset
+ len
, sdata
+ offset
, len_after
);
535 memcpy(sdata
+ offset
, data
, len
);
539 size_t odhcp6c_remove_state(enum odhcp6c_state state
, size_t offset
, size_t len
)
541 uint8_t *data
= state_data
[state
];
542 ssize_t len_after
= state_len
[state
] - (offset
+ len
);
544 return state_len
[state
];
546 memmove(data
+ offset
, data
+ offset
+ len
, len_after
);
547 return state_len
[state
] -= len
;
551 void* odhcp6c_move_state(enum odhcp6c_state state
, size_t *len
)
553 *len
= state_len
[state
];
554 void *data
= state_data
[state
];
556 state_len
[state
] = 0;
557 state_data
[state
] = NULL
;
563 void* odhcp6c_get_state(enum odhcp6c_state state
, size_t *len
)
565 *len
= state_len
[state
];
566 return state_data
[state
];
570 struct odhcp6c_entry
* odhcp6c_find_entry(enum odhcp6c_state state
, const struct odhcp6c_entry
*new)
572 size_t len
, cmplen
= offsetof(struct odhcp6c_entry
, target
) + new->length
/ 8;
573 struct odhcp6c_entry
*start
= odhcp6c_get_state(state
, &len
);
574 struct odhcp6c_entry
*x
= NULL
;
576 for (struct odhcp6c_entry
*c
= start
; !x
&& c
< &start
[len
/sizeof(*c
)]; ++c
)
577 if (!memcmp(c
, new, cmplen
))
584 bool odhcp6c_update_entry_safe(enum odhcp6c_state state
, struct odhcp6c_entry
*new, uint32_t safe
)
587 struct odhcp6c_entry
*x
= odhcp6c_find_entry(state
, new);
588 struct odhcp6c_entry
*start
= odhcp6c_get_state(state
, &len
);
590 if (x
&& x
->valid
> new->valid
&& new->valid
< safe
)
593 if (new->valid
> 0) {
595 if (new->valid
>= x
->valid
&& new->valid
- x
->valid
< 60 &&
596 new->preferred
>= x
->preferred
&&
597 new->preferred
- x
->preferred
< 60 &&
598 x
->class == new->class)
600 x
->valid
= new->valid
;
601 x
->preferred
= new->preferred
;
604 x
->class = new->class;
607 odhcp6c_add_state(state
, new, sizeof(*new));
610 odhcp6c_remove_state(state
, (x
- start
) * sizeof(*x
), sizeof(*x
));
616 bool odhcp6c_update_entry(enum odhcp6c_state state
, struct odhcp6c_entry
*new)
618 return odhcp6c_update_entry_safe(state
, new, 0);
622 static void odhcp6c_expire_list(enum odhcp6c_state state
, uint32_t elapsed
)
625 struct odhcp6c_entry
*start
= odhcp6c_get_state(state
, &len
);
626 for (struct odhcp6c_entry
*c
= start
; c
< &start
[len
/ sizeof(*c
)]; ++c
) {
629 else if (c
->t1
!= UINT32_MAX
)
634 else if (c
->t2
!= UINT32_MAX
)
637 if (c
->preferred
< elapsed
)
639 else if (c
->preferred
!= UINT32_MAX
)
640 c
->preferred
-= elapsed
;
642 if (c
->valid
< elapsed
)
644 else if (c
->valid
!= UINT32_MAX
)
648 odhcp6c_remove_state(state
, (c
- start
) * sizeof(*c
), sizeof(*c
));
653 void odhcp6c_expire(void)
655 time_t now
= odhcp6c_get_milli_time() / 1000;
656 uint32_t elapsed
= (last_update
> 0) ? now
- last_update
: 0;
659 odhcp6c_expire_list(STATE_RA_PREFIX
, elapsed
);
660 odhcp6c_expire_list(STATE_RA_ROUTE
, elapsed
);
661 odhcp6c_expire_list(STATE_RA_DNS
, elapsed
);
662 odhcp6c_expire_list(STATE_IA_NA
, elapsed
);
663 odhcp6c_expire_list(STATE_IA_PD
, elapsed
);
667 uint32_t odhcp6c_elapsed(void)
669 return odhcp6c_get_milli_time() / 1000 - last_update
;
673 void odhcp6c_random(void *buf
, size_t len
)
675 read(urandom_fd
, buf
, len
);
678 bool odhcp6c_is_bound(void)
683 static void sighandler(int signal
)
685 if (signal
== SIGCHLD
)
686 while (waitpid(-1, NULL
, WNOHANG
) > 0);
687 else if (signal
== SIGUSR1
)
689 else if (signal
== SIGUSR2
)
691 else if (signal
== SIGIO
)