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 static unsigned int min_update_interval
= DEFAULT_MIN_UPDATE_INTERVAL
;
57 int main(_unused
int argc
, char* const argv
[])
59 // Allocate ressources
60 const char *pidfile
= NULL
;
61 const char *script
= "/usr/sbin/odhcp6c-update";
67 enum odhcp6c_ia_mode ia_na_mode
= IA_MODE_TRY
;
68 enum odhcp6c_ia_mode ia_pd_mode
= IA_MODE_NONE
;
69 int ia_pd_iaid_index
= 0;
70 static struct in6_addr ifid
= IN6ADDR_ANY_INIT
;
71 int sol_timeout
= DHCPV6_SOL_MAX_RT
;
74 int bfd_interval
= 0, bfd_loss
= 3;
77 bool help
= false, daemonize
= false;
80 unsigned int client_options
= DHCPV6_CLIENT_FQDN
| DHCPV6_ACCEPT_RECONFIGURE
;
82 while ((c
= getopt(argc
, argv
, "S::N:V:P:FB:c:i:r:Ru:s:kt:m:hedp:fa")) != -1) {
85 allow_slaac_only
= (optarg
) ? atoi(optarg
) : -1;
89 if (!strcmp(optarg
, "force")) {
90 ia_na_mode
= IA_MODE_FORCE
;
91 allow_slaac_only
= -1;
92 } else if (!strcmp(optarg
, "none")) {
93 ia_na_mode
= IA_MODE_NONE
;
94 } else if (!strcmp(optarg
, "try")) {
95 ia_na_mode
= IA_MODE_TRY
;
102 l
= script_unhexlify(buf
, sizeof(buf
), optarg
);
106 odhcp6c_add_state(STATE_VENDORCLASS
, buf
, l
);
110 if (ia_pd_mode
== IA_MODE_NONE
)
111 ia_pd_mode
= IA_MODE_TRY
;
113 if (allow_slaac_only
>= 0 && allow_slaac_only
< 10)
114 allow_slaac_only
= 10;
119 int prefix_length
= strtoul(optarg
, &iaid_begin
, 10);
121 if (*iaid_begin
!= '\0' && *iaid_begin
!= ',' && *iaid_begin
!= ':') {
122 syslog(LOG_ERR
, "invalid argument: '%s'", optarg
);
126 struct odhcp6c_request_prefix prefix
= { 0, prefix_length
};
128 if (*iaid_begin
== ',' && (iaid_len
= strlen(iaid_begin
)) > 1)
129 memcpy(&prefix
.iaid
, iaid_begin
+ 1, iaid_len
> 4 ? 4 : iaid_len
);
130 else if (*iaid_begin
== ':')
131 prefix
.iaid
= htonl((uint32_t)strtoul(&iaid_begin
[1], NULL
, 16));
133 prefix
.iaid
= htonl(++ia_pd_iaid_index
);
135 odhcp6c_add_state(STATE_IA_PD_INIT
, &prefix
, sizeof(prefix
));
140 allow_slaac_only
= -1;
141 ia_pd_mode
= IA_MODE_FORCE
;
146 bfd_interval
= atoi(optarg
);
151 l
= script_unhexlify(&buf
[4], sizeof(buf
) - 4, optarg
);
154 buf
[1] = DHCPV6_OPT_CLIENTID
;
157 odhcp6c_add_state(STATE_CLIENT_ID
, buf
, l
+ 4);
164 if (inet_pton(AF_INET6
, optarg
, &ifid
) != 1)
171 opttype
= htons(strtoul(optarg
, &optpos
, 10));
172 if (optpos
== optarg
)
176 odhcp6c_add_state(STATE_ORO
, &opttype
, 2);
181 client_options
|= DHCPV6_STRICT_OPTIONS
;
185 optlen
= htons(strlen(optarg
));
186 odhcp6c_add_state(STATE_USERCLASS
, &optlen
, 2);
187 odhcp6c_add_state(STATE_USERCLASS
, optarg
, strlen(optarg
));
199 sol_timeout
= atoi(optarg
);
203 min_update_interval
= atoi(optarg
);
207 logopt
|= LOG_PERROR
;
219 client_options
&= ~DHCPV6_CLIENT_FQDN
;
223 client_options
&= ~DHCPV6_ACCEPT_RECONFIGURE
;
232 openlog("odhcp6c", logopt
, LOG_DAEMON
);
233 const char *ifname
= argv
[optind
];
238 signal(SIGIO
, sighandler
);
239 signal(SIGHUP
, sighandler
);
240 signal(SIGINT
, sighandler
);
241 signal(SIGCHLD
, sighandler
);
242 signal(SIGTERM
, sighandler
);
243 signal(SIGUSR1
, sighandler
);
244 signal(SIGUSR2
, sighandler
);
246 if ((urandom_fd
= open("/dev/urandom", O_CLOEXEC
| O_RDONLY
)) < 0 ||
247 init_dhcpv6(ifname
, client_options
, sol_timeout
) ||
248 ra_init(ifname
, &ifid
) || script_init(script
, ifname
)) {
249 syslog(LOG_ERR
, "failed to initialize: %s", strerror(errno
));
254 openlog("odhcp6c", LOG_PID
, LOG_DAEMON
); // Disable LOG_PERROR
256 syslog(LOG_ERR
, "Failed to daemonize: %s",
263 snprintf(pidbuf
, sizeof(pidbuf
),
264 "/var/run/odhcp6c.%s.pid", ifname
);
268 int fd
= open(pidfile
, O_WRONLY
| O_CREAT
, 0644);
271 int len
= snprintf(buf
, sizeof(buf
), "%i\n", getpid());
277 script_call("started");
279 while (!signal_term
) { // Main logic
280 odhcp6c_clear_state(STATE_SERVER_ID
);
281 odhcp6c_clear_state(STATE_IA_NA
);
282 odhcp6c_clear_state(STATE_IA_PD
);
283 odhcp6c_clear_state(STATE_SNTP_IP
);
284 odhcp6c_clear_state(STATE_NTP_IP
);
285 odhcp6c_clear_state(STATE_NTP_FQDN
);
286 odhcp6c_clear_state(STATE_SIP_IP
);
287 odhcp6c_clear_state(STATE_SIP_FQDN
);
288 dhcpv6_set_ia_mode(ia_na_mode
, ia_pd_mode
);
291 syslog(LOG_NOTICE
, "(re)starting transaction on %s", ifname
);
293 signal_usr1
= signal_usr2
= false;
294 int mode
= dhcpv6_request(DHCPV6_MSG_SOLICIT
);
295 odhcp6c_signal_process();
301 int res
= dhcpv6_request(mode
== DHCPV6_STATELESS
?
302 DHCPV6_MSG_INFO_REQ
: DHCPV6_MSG_REQUEST
);
303 bool signalled
= odhcp6c_signal_process();
307 else if (signalled
) {
312 mode
= dhcpv6_promote_server_cand();
313 } while (mode
> DHCPV6_UNKNOWN
);
319 case DHCPV6_STATELESS
:
321 syslog(LOG_NOTICE
, "entering stateless-mode on %s", ifname
);
323 while (!signal_usr2
&& !signal_term
) {
325 script_call("informed");
327 int res
= dhcpv6_poll_reconfigure();
328 odhcp6c_signal_process();
334 signal_usr1
= false; // Acknowledged
337 if (signal_usr2
|| signal_term
)
340 res
= dhcpv6_request(DHCPV6_MSG_INFO_REQ
);
341 odhcp6c_signal_process();
349 case DHCPV6_STATEFUL
:
350 script_call("bound");
352 syslog(LOG_NOTICE
, "entering stateful-mode on %s", ifname
);
354 if (bfd_interval
> 0)
355 bfd_start(ifname
, bfd_loss
, bfd_interval
);
358 while (!signal_usr2
&& !signal_term
) {
360 // Wait for T1 to expire or until we get a reconfigure
361 int res
= dhcpv6_poll_reconfigure();
362 odhcp6c_signal_process();
364 script_call("updated");
368 // Handle signal, if necessary
370 signal_usr1
= false; // Acknowledged
371 if (signal_usr2
|| signal_term
)
372 break; // Other signal type
374 // Send renew as T1 expired
375 size_t ia_pd_len
, ia_na_len
;
376 odhcp6c_get_state(STATE_IA_PD
, &ia_pd_len
);
377 odhcp6c_get_state(STATE_IA_NA
, &ia_na_len
);
379 // If we have any IAs, send renew, otherwise request
380 if (ia_pd_len
== 0 && ia_na_len
== 0)
381 res
= dhcpv6_request(DHCPV6_MSG_REQUEST
);
383 res
= dhcpv6_request(DHCPV6_MSG_RENEW
);
385 odhcp6c_signal_process();
386 if (res
> 0) { // Renew was succesfull
388 script_call("updated");
389 continue; // Renew was successful
392 odhcp6c_clear_state(STATE_SERVER_ID
); // Remove binding
394 // If we have IAs, try rebind otherwise restart
395 res
= dhcpv6_request(DHCPV6_MSG_REBIND
);
396 odhcp6c_signal_process();
399 script_call("rebound");
413 size_t ia_pd_len
, ia_na_len
, server_id_len
;
414 odhcp6c_get_state(STATE_IA_PD
, &ia_pd_len
);
415 odhcp6c_get_state(STATE_IA_NA
, &ia_na_len
);
416 odhcp6c_get_state(STATE_SERVER_ID
, &server_id_len
);
418 // Add all prefixes to lost prefixes
420 script_call("unbound");
422 if (server_id_len
> 0 && (ia_pd_len
> 0 || ia_na_len
> 0) && release
)
423 dhcpv6_request(DHCPV6_MSG_RELEASE
);
425 odhcp6c_clear_state(STATE_IA_NA
);
426 odhcp6c_clear_state(STATE_IA_PD
);
429 script_call("stopped");
434 static int usage(void)
437 "Usage: odhcp6c [options] <interface>\n"
438 "\nFeature options:\n"
439 " -S <time> Wait at least <time> sec for a DHCP-server (0)\n"
440 " -N <mode> Mode for requesting addresses [try|force|none]\n"
441 " -P <length> Request IPv6-Prefix (0 = auto)\n"
442 " -F Force IPv6-Prefix\n"
443 " -V <class> Set vendor-class option (base-16 encoded)\n"
445 " -B <interval> Enable BFD ping check\n"
447 " -c <clientid> Override client-ID (base-16 encoded)\n"
448 " -i <iface-id> Use a custom interface identifier for RA handling\n"
449 " -r <options> Options to be requested (comma-separated)\n"
450 " -R Do not request any options except those specified with -r\n"
451 " -s <script> Status update script (/usr/sbin/odhcp6c-update)\n"
452 " -a Don't send Accept Reconfigure option\n"
453 " -f Don't send Client FQDN option\n"
454 " -k Don't send a RELEASE when stopping\n"
455 " -t <seconds> Maximum timeout for DHCPv6-SOLICIT (3600)\n"
456 " -m <seconds> Minimum time between accepting updates (60)\n"
457 "\nInvocation options:\n"
458 " -p <pidfile> Set pidfile (/var/run/odhcp6c.pid)\n"
460 " -e Write logmessages to stderr\n"
461 //" -v Increase logging verbosity\n"
462 " -h Show this help\n\n";
463 write(STDERR_FILENO
, buf
, sizeof(buf
));
468 // Don't want to pull-in librt and libpthread just for a monotonic clock...
469 uint64_t odhcp6c_get_milli_time(void)
471 struct timespec t
= {0, 0};
472 syscall(SYS_clock_gettime
, CLOCK_MONOTONIC
, &t
);
473 return t
.tv_sec
* 1000 + t
.tv_nsec
/ 1000000;
477 static uint8_t* odhcp6c_resize_state(enum odhcp6c_state state
, ssize_t len
)
480 return state_data
[state
] + state_len
[state
];
481 else if (state_len
[state
] + len
> 1024)
484 uint8_t *n
= realloc(state_data
[state
], state_len
[state
] + len
);
485 if (n
|| state_len
[state
] + len
== 0) {
486 state_data
[state
] = n
;
487 n
+= state_len
[state
];
488 state_len
[state
] += len
;
494 bool odhcp6c_signal_process(void)
499 bool ra_updated
= ra_process();
504 if (ra_updated
&& (bound
|| allow_slaac_only
== 0))
505 script_call("ra-updated"); // Immediate process urgent events
506 else if (ra_updated
&& !bound
&& allow_slaac_only
> 0)
507 script_delay_call("ra-updated", allow_slaac_only
);
514 return signal_usr1
|| signal_usr2
|| signal_term
;
518 void odhcp6c_clear_state(enum odhcp6c_state state
)
520 state_len
[state
] = 0;
524 void odhcp6c_add_state(enum odhcp6c_state state
, const void *data
, size_t len
)
526 uint8_t *n
= odhcp6c_resize_state(state
, len
);
528 memcpy(n
, data
, len
);
531 void odhcp6c_insert_state(enum odhcp6c_state state
, size_t offset
, const void *data
, size_t len
)
533 ssize_t len_after
= state_len
[state
] - offset
;
537 uint8_t *n
= odhcp6c_resize_state(state
, len
);
539 uint8_t *sdata
= state_data
[state
];
541 memmove(sdata
+ offset
+ len
, sdata
+ offset
, len_after
);
542 memcpy(sdata
+ offset
, data
, len
);
546 size_t odhcp6c_remove_state(enum odhcp6c_state state
, size_t offset
, size_t len
)
548 uint8_t *data
= state_data
[state
];
549 ssize_t len_after
= state_len
[state
] - (offset
+ len
);
551 return state_len
[state
];
553 memmove(data
+ offset
, data
+ offset
+ len
, len_after
);
554 return state_len
[state
] -= len
;
558 void* odhcp6c_move_state(enum odhcp6c_state state
, size_t *len
)
560 *len
= state_len
[state
];
561 void *data
= state_data
[state
];
563 state_len
[state
] = 0;
564 state_data
[state
] = NULL
;
570 void* odhcp6c_get_state(enum odhcp6c_state state
, size_t *len
)
572 *len
= state_len
[state
];
573 return state_data
[state
];
577 struct odhcp6c_entry
* odhcp6c_find_entry(enum odhcp6c_state state
, const struct odhcp6c_entry
*new)
579 size_t len
, cmplen
= offsetof(struct odhcp6c_entry
, target
) + new->length
/ 8;
580 struct odhcp6c_entry
*start
= odhcp6c_get_state(state
, &len
);
581 struct odhcp6c_entry
*x
= NULL
;
583 for (struct odhcp6c_entry
*c
= start
; !x
&& c
< &start
[len
/sizeof(*c
)]; ++c
)
584 if (!memcmp(c
, new, cmplen
))
591 bool odhcp6c_update_entry_safe(enum odhcp6c_state state
, struct odhcp6c_entry
*new, uint32_t safe
)
594 struct odhcp6c_entry
*x
= odhcp6c_find_entry(state
, new);
595 struct odhcp6c_entry
*start
= odhcp6c_get_state(state
, &len
);
597 if (x
&& x
->valid
> new->valid
&& new->valid
< safe
)
600 if (new->valid
> 0) {
602 if (new->valid
>= x
->valid
&& new->valid
!= UINT32_MAX
&&
603 new->valid
- x
->valid
< min_update_interval
&&
604 new->preferred
>= x
->preferred
&&
605 new->preferred
!= UINT32_MAX
&&
606 new->preferred
- x
->preferred
< min_update_interval
&&
607 x
->class == new->class)
609 x
->valid
= new->valid
;
610 x
->preferred
= new->preferred
;
613 x
->class = new->class;
616 odhcp6c_add_state(state
, new, sizeof(*new));
619 odhcp6c_remove_state(state
, (x
- start
) * sizeof(*x
), sizeof(*x
));
625 bool odhcp6c_update_entry(enum odhcp6c_state state
, struct odhcp6c_entry
*new)
627 return odhcp6c_update_entry_safe(state
, new, 0);
631 static void odhcp6c_expire_list(enum odhcp6c_state state
, uint32_t elapsed
)
634 struct odhcp6c_entry
*start
= odhcp6c_get_state(state
, &len
);
635 for (struct odhcp6c_entry
*c
= start
; c
< &start
[len
/ sizeof(*c
)]; ++c
) {
638 else if (c
->t1
!= UINT32_MAX
)
643 else if (c
->t2
!= UINT32_MAX
)
646 if (c
->preferred
< elapsed
)
648 else if (c
->preferred
!= UINT32_MAX
)
649 c
->preferred
-= elapsed
;
651 if (c
->valid
< elapsed
)
653 else if (c
->valid
!= UINT32_MAX
)
657 odhcp6c_remove_state(state
, (c
- start
) * sizeof(*c
), sizeof(*c
));
662 void odhcp6c_expire(void)
664 time_t now
= odhcp6c_get_milli_time() / 1000;
665 uint32_t elapsed
= (last_update
> 0) ? now
- last_update
: 0;
668 odhcp6c_expire_list(STATE_RA_PREFIX
, elapsed
);
669 odhcp6c_expire_list(STATE_RA_ROUTE
, elapsed
);
670 odhcp6c_expire_list(STATE_RA_DNS
, elapsed
);
671 odhcp6c_expire_list(STATE_IA_NA
, elapsed
);
672 odhcp6c_expire_list(STATE_IA_PD
, elapsed
);
676 uint32_t odhcp6c_elapsed(void)
678 return odhcp6c_get_milli_time() / 1000 - last_update
;
682 void odhcp6c_random(void *buf
, size_t len
)
684 read(urandom_fd
, buf
, len
);
687 bool odhcp6c_is_bound(void)
692 static void sighandler(int signal
)
694 if (signal
== SIGCHLD
)
695 while (waitpid(-1, NULL
, WNOHANG
) > 0);
696 else if (signal
== SIGUSR1
)
698 else if (signal
== SIGUSR2
)
700 else if (signal
== SIGIO
)