2 * Copyright (C) 2012-2013 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";
64 enum odhcp6c_ia_mode ia_na_mode
= IA_MODE_TRY
;
65 enum odhcp6c_ia_mode ia_pd_mode
= IA_MODE_TRY
;
66 int ia_pd_iaid_index
= 0;
67 static struct in6_addr ifid
= IN6ADDR_ANY_INIT
;
68 int sol_timeout
= DHCPV6_SOL_MAX_RT
;
71 int bfd_interval
= 0, bfd_loss
= 3;
74 bool help
= false, daemonize
= false;
77 unsigned int client_options
= DHCPV6_CLIENT_FQDN
| DHCPV6_ACCEPT_RECONFIGURE
;
79 while ((c
= getopt(argc
, argv
, "S::N:V:P:FB:c:i:r:Rs:kt:hedp:fa")) != -1) {
82 allow_slaac_only
= (optarg
) ? atoi(optarg
) : -1;
86 if (!strcmp(optarg
, "force")) {
87 ia_na_mode
= IA_MODE_FORCE
;
88 allow_slaac_only
= -1;
89 } else if (!strcmp(optarg
, "none")) {
90 ia_na_mode
= IA_MODE_NONE
;
91 } else if (!strcmp(optarg
, "try")) {
92 ia_na_mode
= IA_MODE_TRY
;
99 l
= script_unhexlify(buf
, sizeof(buf
), optarg
);
103 odhcp6c_add_state(STATE_VENDORCLASS
, buf
, l
);
107 if (allow_slaac_only
>= 0 && allow_slaac_only
< 10)
108 allow_slaac_only
= 10;
113 int prefix_length
= strtoul(optarg
, &iaid_begin
, 10);
115 if (*iaid_begin
!= '\0' && *iaid_begin
!= ',') {
116 syslog(LOG_ERR
, "invalid argument: '%s'", optarg
);
120 struct odhcp6c_request_prefix prefix
= { 0, prefix_length
};
122 if (*iaid_begin
== ',' && (iaid_len
= strlen(iaid_begin
)) > 1)
123 memcpy(&prefix
.iaid
, iaid_begin
+ 1, iaid_len
> 4 ? 4 : iaid_len
);
125 prefix
.iaid
= ++ia_pd_iaid_index
;
127 odhcp6c_add_state(STATE_IA_PD_INIT
, &prefix
, sizeof(prefix
));
132 allow_slaac_only
= -1;
133 ia_pd_mode
= IA_MODE_FORCE
;
138 bfd_interval
= atoi(optarg
);
143 l
= script_unhexlify(&buf
[4], sizeof(buf
) - 4, optarg
);
146 buf
[1] = DHCPV6_OPT_CLIENTID
;
149 odhcp6c_add_state(STATE_CLIENT_ID
, buf
, l
+ 4);
156 if (inet_pton(AF_INET6
, optarg
, &ifid
) != 1)
163 opttype
= htons(strtoul(optarg
, &optpos
, 10));
164 if (optpos
== optarg
)
168 odhcp6c_add_state(STATE_ORO
, &opttype
, 2);
173 client_options
|= DHCPV6_STRICT_OPTIONS
;
185 sol_timeout
= atoi(optarg
);
189 logopt
|= LOG_PERROR
;
201 client_options
&= ~DHCPV6_CLIENT_FQDN
;
205 client_options
&= ~DHCPV6_ACCEPT_RECONFIGURE
;
214 openlog("odhcp6c", logopt
, LOG_DAEMON
);
215 const char *ifname
= argv
[optind
];
220 signal(SIGIO
, sighandler
);
221 signal(SIGHUP
, sighandler
);
222 signal(SIGINT
, sighandler
);
223 signal(SIGCHLD
, sighandler
);
224 signal(SIGTERM
, sighandler
);
225 signal(SIGUSR1
, sighandler
);
226 signal(SIGUSR2
, sighandler
);
228 if ((urandom_fd
= open("/dev/urandom", O_CLOEXEC
| O_RDONLY
)) < 0 ||
229 init_dhcpv6(ifname
, client_options
, sol_timeout
) ||
230 ra_init(ifname
, &ifid
) || script_init(script
, ifname
)) {
231 syslog(LOG_ERR
, "failed to initialize: %s", strerror(errno
));
236 openlog("odhcp6c", LOG_PID
, LOG_DAEMON
); // Disable LOG_PERROR
238 syslog(LOG_ERR
, "Failed to daemonize: %s",
245 snprintf(pidbuf
, sizeof(pidbuf
),
246 "/var/run/odhcp6c.%s.pid", ifname
);
250 int fd
= open(pidfile
, O_WRONLY
| O_CREAT
);
253 int len
= snprintf(buf
, sizeof(buf
), "%i\n", getpid());
259 script_call("started");
261 while (!signal_term
) { // Main logic
262 odhcp6c_clear_state(STATE_SERVER_ID
);
263 odhcp6c_clear_state(STATE_IA_NA
);
264 odhcp6c_clear_state(STATE_IA_PD
);
265 odhcp6c_clear_state(STATE_SNTP_IP
);
266 odhcp6c_clear_state(STATE_NTP_IP
);
267 odhcp6c_clear_state(STATE_NTP_FQDN
);
268 odhcp6c_clear_state(STATE_SIP_IP
);
269 odhcp6c_clear_state(STATE_SIP_FQDN
);
270 dhcpv6_set_ia_mode(ia_na_mode
, ia_pd_mode
);
273 syslog(LOG_NOTICE
, "(re)starting transaction on %s", ifname
);
275 signal_usr1
= signal_usr2
= false;
276 int mode
= dhcpv6_request(DHCPV6_MSG_SOLICIT
);
277 odhcp6c_signal_process();
283 int res
= dhcpv6_request(mode
== DHCPV6_STATELESS
?
284 DHCPV6_MSG_INFO_REQ
: DHCPV6_MSG_REQUEST
);
285 bool signalled
= odhcp6c_signal_process();
289 else if (signalled
) {
294 mode
= dhcpv6_promote_server_cand();
295 } while (mode
> DHCPV6_UNKNOWN
);
301 case DHCPV6_STATELESS
:
303 syslog(LOG_NOTICE
, "entering stateless-mode on %s", ifname
);
305 while (!signal_usr2
&& !signal_term
) {
307 script_call("informed");
309 int res
= dhcpv6_poll_reconfigure();
310 odhcp6c_signal_process();
316 signal_usr1
= false; // Acknowledged
319 if (signal_usr2
|| signal_term
)
322 res
= dhcpv6_request(DHCPV6_MSG_INFO_REQ
);
323 odhcp6c_signal_process();
331 case DHCPV6_STATEFUL
:
332 script_call("bound");
334 syslog(LOG_NOTICE
, "entering stateful-mode on %s", ifname
);
336 if (bfd_interval
> 0)
337 bfd_start(ifname
, bfd_loss
, bfd_interval
);
340 while (!signal_usr2
&& !signal_term
) {
342 // Wait for T1 to expire or until we get a reconfigure
343 int res
= dhcpv6_poll_reconfigure();
344 odhcp6c_signal_process();
346 script_call("updated");
350 // Handle signal, if necessary
352 signal_usr1
= false; // Acknowledged
353 if (signal_usr2
|| signal_term
)
354 break; // Other signal type
356 // Send renew as T1 expired
357 size_t ia_pd_len
, ia_na_len
;
358 odhcp6c_get_state(STATE_IA_PD
, &ia_pd_len
);
359 odhcp6c_get_state(STATE_IA_NA
, &ia_na_len
);
361 // If we have any IAs, send renew, otherwise request
362 if (ia_pd_len
== 0 && ia_na_len
== 0)
363 res
= dhcpv6_request(DHCPV6_MSG_REQUEST
);
365 res
= dhcpv6_request(DHCPV6_MSG_RENEW
);
367 odhcp6c_signal_process();
368 if (res
> 0) { // Renew was succesfull
370 script_call("updated");
371 continue; // Renew was successful
374 odhcp6c_clear_state(STATE_SERVER_ID
); // Remove binding
376 // If we have IAs, try rebind otherwise restart
377 res
= dhcpv6_request(DHCPV6_MSG_REBIND
);
378 odhcp6c_signal_process();
381 script_call("rebound");
395 size_t ia_pd_len
, ia_na_len
, server_id_len
;
396 odhcp6c_get_state(STATE_IA_PD
, &ia_pd_len
);
397 odhcp6c_get_state(STATE_IA_NA
, &ia_na_len
);
398 odhcp6c_get_state(STATE_SERVER_ID
, &server_id_len
);
400 // Add all prefixes to lost prefixes
402 script_call("unbound");
404 if (server_id_len
> 0 && (ia_pd_len
> 0 || ia_na_len
> 0) && release
)
405 dhcpv6_request(DHCPV6_MSG_RELEASE
);
407 odhcp6c_clear_state(STATE_IA_NA
);
408 odhcp6c_clear_state(STATE_IA_PD
);
411 script_call("stopped");
416 static int usage(void)
419 "Usage: odhcp6c [options] <interface>\n"
420 "\nFeature options:\n"
421 " -S <time> Wait at least <time> sec for a DHCP-server (0)\n"
422 " -N <mode> Mode for requesting addresses [try|force|none]\n"
423 " -P <length> Request IPv6-Prefix (0 = auto)\n"
424 " -F Force IPv6-Prefix\n"
425 " -V <hex-string> Set vendor-class option. string length must be a multiple of 2\n"
427 " -B <interval> Enable BFD ping check\n"
429 " -c <clientid> Override client-ID (base-16 encoded)\n"
430 " -i <iface-id> Use a custom interface identifier for RA handling\n"
431 " -r <options> Options to be requested (comma-separated)\n"
432 " -R Do not request any options except those specified with -r\n"
433 " -s <script> Status update script (/usr/sbin/odhcp6c-update)\n"
434 " -a Don't send Accept Reconfigure option\n"
435 " -f Don't send Client FQDN option\n"
436 " -k Don't send a RELEASE when stopping\n"
437 " -t <seconds> Maximum timeout for DHCPv6-SOLICIT (120)\n"
438 "\nInvocation options:\n"
439 " -p <pidfile> Set pidfile (/var/run/6relayd.pid)\n"
441 " -e Write logmessages to stderr\n"
442 //" -v Increase logging verbosity\n"
443 " -h Show this help\n\n";
444 write(STDERR_FILENO
, buf
, sizeof(buf
));
449 // Don't want to pull-in librt and libpthread just for a monotonic clock...
450 uint64_t odhcp6c_get_milli_time(void)
452 struct timespec t
= {0, 0};
453 syscall(SYS_clock_gettime
, CLOCK_MONOTONIC
, &t
);
454 return t
.tv_sec
* 1000 + t
.tv_nsec
/ 1000000;
458 static uint8_t* odhcp6c_resize_state(enum odhcp6c_state state
, ssize_t len
)
461 return state_data
[state
] + state_len
[state
];
462 else if (state_len
[state
] + len
> 1024)
465 uint8_t *n
= realloc(state_data
[state
], state_len
[state
] + len
);
466 if (n
|| state_len
[state
] + len
== 0) {
467 state_data
[state
] = n
;
468 n
+= state_len
[state
];
469 state_len
[state
] += len
;
475 bool odhcp6c_signal_process(void)
480 bool ra_updated
= ra_process();
485 if (ra_updated
&& (bound
|| allow_slaac_only
== 0))
486 script_call("ra-updated"); // Immediate process urgent events
487 else if (ra_updated
&& !bound
&& allow_slaac_only
> 0)
488 script_delay_call("ra-updated", allow_slaac_only
);
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
- x
->valid
< 60 &&
584 new->preferred
>= x
->preferred
&&
585 new->preferred
- x
->preferred
< 60 &&
586 x
->class == new->class)
588 x
->valid
= new->valid
;
589 x
->preferred
= new->preferred
;
592 x
->class = new->class;
595 odhcp6c_add_state(state
, new, sizeof(*new));
598 odhcp6c_remove_state(state
, (x
- start
) * sizeof(*x
), sizeof(*x
));
604 bool odhcp6c_update_entry(enum odhcp6c_state state
, struct odhcp6c_entry
*new)
606 return odhcp6c_update_entry_safe(state
, new, 0);
610 static void odhcp6c_expire_list(enum odhcp6c_state state
, uint32_t elapsed
)
613 struct odhcp6c_entry
*start
= odhcp6c_get_state(state
, &len
);
614 for (struct odhcp6c_entry
*c
= start
; c
< &start
[len
/ sizeof(*c
)]; ++c
) {
617 else if (c
->t1
!= UINT32_MAX
)
622 else if (c
->t2
!= UINT32_MAX
)
625 if (c
->preferred
< elapsed
)
627 else if (c
->preferred
!= UINT32_MAX
)
628 c
->preferred
-= elapsed
;
630 if (c
->valid
< elapsed
)
632 else if (c
->valid
!= UINT32_MAX
)
636 odhcp6c_remove_state(state
, (c
- start
) * sizeof(*c
), sizeof(*c
));
641 void odhcp6c_expire(void)
643 time_t now
= odhcp6c_get_milli_time() / 1000;
644 uint32_t elapsed
= (last_update
> 0) ? now
- last_update
: 0;
647 odhcp6c_expire_list(STATE_RA_PREFIX
, elapsed
);
648 odhcp6c_expire_list(STATE_RA_ROUTE
, elapsed
);
649 odhcp6c_expire_list(STATE_RA_DNS
, elapsed
);
650 odhcp6c_expire_list(STATE_IA_NA
, elapsed
);
651 odhcp6c_expire_list(STATE_IA_PD
, elapsed
);
655 uint32_t odhcp6c_elapsed(void)
657 return odhcp6c_get_milli_time() / 1000 - last_update
;
661 void odhcp6c_random(void *buf
, size_t len
)
663 read(urandom_fd
, buf
, len
);
666 bool odhcp6c_is_bound(void)
671 static void sighandler(int signal
)
673 if (signal
== SIGCHLD
)
674 while (waitpid(-1, NULL
, WNOHANG
) > 0);
675 else if (signal
== SIGUSR1
)
677 else if (signal
== SIGUSR2
)
679 else if (signal
== SIGIO
)