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, strict_options
= false;
77 while ((c
= getopt(argc
, argv
, "S::N:P:FB:c:i:r:Rs:kt:hedp:")) != -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 if (allow_slaac_only
>= 0 && allow_slaac_only
< 10)
98 allow_slaac_only
= 10;
103 int prefix_length
= strtoul(optarg
, &iaid_begin
, 10);
105 if (*iaid_begin
!= '\0' && *iaid_begin
!= ',') {
106 syslog(LOG_ERR
, "invalid argument: '%s'", optarg
);
110 struct odhcp6c_request_prefix prefix
= { 0, prefix_length
};
112 if (*iaid_begin
== ',' && (iaid_len
= strlen(iaid_begin
)) > 1)
113 memcpy(&prefix
.iaid
, iaid_begin
+ 1, iaid_len
> 4 ? 4 : iaid_len
);
115 prefix
.iaid
= ++ia_pd_iaid_index
;
117 odhcp6c_add_state(STATE_IA_PD_INIT
, &prefix
, sizeof(prefix
));
122 allow_slaac_only
= -1;
123 ia_pd_mode
= IA_MODE_FORCE
;
128 bfd_interval
= atoi(optarg
);
133 l
= script_unhexlify(&buf
[4], sizeof(buf
) - 4, optarg
);
136 buf
[1] = DHCPV6_OPT_CLIENTID
;
139 odhcp6c_add_state(STATE_CLIENT_ID
, buf
, l
+ 4);
146 if (inet_pton(AF_INET6
, optarg
, &ifid
) != 1)
153 opttype
= htons(strtoul(optarg
, &optpos
, 10));
154 if (optpos
== optarg
)
158 odhcp6c_add_state(STATE_ORO
, &opttype
, 2);
163 strict_options
= true;
175 sol_timeout
= atoi(optarg
);
179 logopt
|= LOG_PERROR
;
196 openlog("odhcp6c", logopt
, LOG_DAEMON
);
197 const char *ifname
= argv
[optind
];
202 signal(SIGIO
, sighandler
);
203 signal(SIGHUP
, sighandler
);
204 signal(SIGINT
, sighandler
);
205 signal(SIGCHLD
, sighandler
);
206 signal(SIGTERM
, sighandler
);
207 signal(SIGUSR1
, sighandler
);
208 signal(SIGUSR2
, sighandler
);
210 if ((urandom_fd
= open("/dev/urandom", O_CLOEXEC
| O_RDONLY
)) < 0 ||
211 init_dhcpv6(ifname
, strict_options
, sol_timeout
) ||
212 ra_init(ifname
, &ifid
) || script_init(script
, ifname
)) {
213 syslog(LOG_ERR
, "failed to initialize: %s", strerror(errno
));
218 openlog("odhcp6c", LOG_PID
, LOG_DAEMON
); // Disable LOG_PERROR
220 syslog(LOG_ERR
, "Failed to daemonize: %s",
227 snprintf(pidbuf
, sizeof(pidbuf
),
228 "/var/run/odhcp6c.%s.pid", ifname
);
232 int fd
= open(pidfile
, O_WRONLY
| O_CREAT
);
235 int len
= snprintf(buf
, sizeof(buf
), "%i\n", getpid());
241 script_call("started");
243 while (!signal_term
) { // Main logic
244 odhcp6c_clear_state(STATE_SERVER_ID
);
245 odhcp6c_clear_state(STATE_IA_NA
);
246 odhcp6c_clear_state(STATE_IA_PD
);
247 odhcp6c_clear_state(STATE_SNTP_IP
);
248 odhcp6c_clear_state(STATE_NTP_IP
);
249 odhcp6c_clear_state(STATE_NTP_FQDN
);
250 odhcp6c_clear_state(STATE_SIP_IP
);
251 odhcp6c_clear_state(STATE_SIP_FQDN
);
252 dhcpv6_set_ia_mode(ia_na_mode
, ia_pd_mode
);
255 syslog(LOG_NOTICE
, "(re)starting transaction on %s", ifname
);
257 signal_usr1
= signal_usr2
= false;
258 int mode
= dhcpv6_request(DHCPV6_MSG_SOLICIT
);
259 odhcp6c_signal_process();
265 int res
= dhcpv6_request(mode
== DHCPV6_STATELESS
?
266 DHCPV6_MSG_INFO_REQ
: DHCPV6_MSG_REQUEST
);
267 bool signalled
= odhcp6c_signal_process();
271 else if (signalled
) {
276 mode
= dhcpv6_promote_server_cand();
277 } while (mode
> DHCPV6_UNKNOWN
);
283 case DHCPV6_STATELESS
:
285 syslog(LOG_NOTICE
, "entering stateless-mode on %s", ifname
);
287 while (!signal_usr2
&& !signal_term
) {
289 script_call("informed");
291 int res
= dhcpv6_poll_reconfigure();
292 odhcp6c_signal_process();
298 signal_usr1
= false; // Acknowledged
301 if (signal_usr2
|| signal_term
)
304 res
= dhcpv6_request(DHCPV6_MSG_INFO_REQ
);
305 odhcp6c_signal_process();
313 case DHCPV6_STATEFUL
:
314 script_call("bound");
316 syslog(LOG_NOTICE
, "entering stateful-mode on %s", ifname
);
318 if (bfd_interval
> 0)
319 bfd_start(ifname
, bfd_loss
, bfd_interval
);
322 while (!signal_usr2
&& !signal_term
) {
324 // Wait for T1 to expire or until we get a reconfigure
325 int res
= dhcpv6_poll_reconfigure();
326 odhcp6c_signal_process();
328 script_call("updated");
332 // Handle signal, if necessary
334 signal_usr1
= false; // Acknowledged
335 if (signal_usr2
|| signal_term
)
336 break; // Other signal type
338 // Send renew as T1 expired
339 size_t ia_pd_len
, ia_na_len
;
340 odhcp6c_get_state(STATE_IA_PD
, &ia_pd_len
);
341 odhcp6c_get_state(STATE_IA_NA
, &ia_na_len
);
343 // If we have any IAs, send renew, otherwise request
344 if (ia_pd_len
== 0 && ia_na_len
== 0)
345 res
= dhcpv6_request(DHCPV6_MSG_REQUEST
);
347 res
= dhcpv6_request(DHCPV6_MSG_RENEW
);
349 odhcp6c_signal_process();
350 if (res
> 0) { // Renew was succesfull
352 script_call("updated");
353 continue; // Renew was successful
356 odhcp6c_clear_state(STATE_SERVER_ID
); // Remove binding
358 // If we have IAs, try rebind otherwise restart
359 res
= dhcpv6_request(DHCPV6_MSG_REBIND
);
360 odhcp6c_signal_process();
363 script_call("rebound");
377 size_t ia_pd_len
, ia_na_len
, server_id_len
;
378 odhcp6c_get_state(STATE_IA_PD
, &ia_pd_len
);
379 odhcp6c_get_state(STATE_IA_NA
, &ia_na_len
);
380 odhcp6c_get_state(STATE_SERVER_ID
, &server_id_len
);
382 // Add all prefixes to lost prefixes
384 script_call("unbound");
386 if (server_id_len
> 0 && (ia_pd_len
> 0 || ia_na_len
> 0) && release
)
387 dhcpv6_request(DHCPV6_MSG_RELEASE
);
389 odhcp6c_clear_state(STATE_IA_NA
);
390 odhcp6c_clear_state(STATE_IA_PD
);
393 script_call("stopped");
398 static int usage(void)
401 "Usage: odhcp6c [options] <interface>\n"
402 "\nFeature options:\n"
403 " -S <time> Wait at least <time> sec for a DHCP-server (0)\n"
404 " -N <mode> Mode for requesting addresses [try|force|none]\n"
405 " -P <length> Request IPv6-Prefix (0 = auto)\n"
406 " -F Force IPv6-Prefix\n"
408 " -B <interval> Enable BFD ping check\n"
410 " -c <clientid> Override client-ID (base-16 encoded)\n"
411 " -i <iface-id> Use a custom interface identifier for RA handling\n"
412 " -r <options> Options to be requested (comma-separated)\n"
413 " -R Do not request any options except those specified with -r\n"
414 " -s <script> Status update script (/usr/sbin/odhcp6c-update)\n"
415 " -k Don't send a RELEASE when stopping\n"
416 " -t <seconds> Maximum timeout for DHCPv6-SOLICIT (120)\n"
417 "\nInvocation options:\n"
418 " -p <pidfile> Set pidfile (/var/run/6relayd.pid)\n"
420 " -e Write logmessages to stderr\n"
421 //" -v Increase logging verbosity\n"
422 " -h Show this help\n\n";
423 write(STDERR_FILENO
, buf
, sizeof(buf
));
428 // Don't want to pull-in librt and libpthread just for a monotonic clock...
429 uint64_t odhcp6c_get_milli_time(void)
431 struct timespec t
= {0, 0};
432 syscall(SYS_clock_gettime
, CLOCK_MONOTONIC
, &t
);
433 return t
.tv_sec
* 1000 + t
.tv_nsec
/ 1000000;
437 static uint8_t* odhcp6c_resize_state(enum odhcp6c_state state
, ssize_t len
)
440 return state_data
[state
] + state_len
[state
];
441 else if (state_len
[state
] + len
> 1024)
444 uint8_t *n
= realloc(state_data
[state
], state_len
[state
] + len
);
445 if (n
|| state_len
[state
] + len
== 0) {
446 state_data
[state
] = n
;
447 n
+= state_len
[state
];
448 state_len
[state
] += len
;
454 bool odhcp6c_signal_process(void)
459 bool ra_updated
= ra_process();
464 if (ra_updated
&& (bound
|| allow_slaac_only
== 0))
465 script_call("ra-updated"); // Immediate process urgent events
466 else if (ra_updated
&& !bound
&& allow_slaac_only
> 0)
467 script_delay_call("ra-updated", allow_slaac_only
);
474 return signal_usr1
|| signal_usr2
|| signal_term
;
478 void odhcp6c_clear_state(enum odhcp6c_state state
)
480 state_len
[state
] = 0;
484 void odhcp6c_add_state(enum odhcp6c_state state
, const void *data
, size_t len
)
486 uint8_t *n
= odhcp6c_resize_state(state
, len
);
488 memcpy(n
, data
, len
);
491 void odhcp6c_insert_state(enum odhcp6c_state state
, size_t offset
, const void *data
, size_t len
)
493 ssize_t len_after
= state_len
[state
] - offset
;
497 uint8_t *n
= odhcp6c_resize_state(state
, len
);
499 uint8_t *sdata
= state_data
[state
];
501 memmove(sdata
+ offset
+ len
, sdata
+ offset
, len_after
);
502 memcpy(sdata
+ offset
, data
, len
);
506 size_t odhcp6c_remove_state(enum odhcp6c_state state
, size_t offset
, size_t len
)
508 uint8_t *data
= state_data
[state
];
509 ssize_t len_after
= state_len
[state
] - (offset
+ len
);
511 return state_len
[state
];
513 memmove(data
+ offset
, data
+ offset
+ len
, len_after
);
514 return state_len
[state
] -= len
;
518 void* odhcp6c_move_state(enum odhcp6c_state state
, size_t *len
)
520 *len
= state_len
[state
];
521 void *data
= state_data
[state
];
523 state_len
[state
] = 0;
524 state_data
[state
] = NULL
;
530 void* odhcp6c_get_state(enum odhcp6c_state state
, size_t *len
)
532 *len
= state_len
[state
];
533 return state_data
[state
];
537 struct odhcp6c_entry
* odhcp6c_find_entry(enum odhcp6c_state state
, const struct odhcp6c_entry
*new)
539 size_t len
, cmplen
= offsetof(struct odhcp6c_entry
, target
) + new->length
/ 8;
540 struct odhcp6c_entry
*start
= odhcp6c_get_state(state
, &len
);
541 struct odhcp6c_entry
*x
= NULL
;
543 for (struct odhcp6c_entry
*c
= start
; !x
&& c
< &start
[len
/sizeof(*c
)]; ++c
)
544 if (!memcmp(c
, new, cmplen
))
551 bool odhcp6c_update_entry_safe(enum odhcp6c_state state
, struct odhcp6c_entry
*new, uint32_t safe
)
554 struct odhcp6c_entry
*x
= odhcp6c_find_entry(state
, new);
555 struct odhcp6c_entry
*start
= odhcp6c_get_state(state
, &len
);
557 if (x
&& x
->valid
> new->valid
&& new->valid
< safe
)
560 if (new->valid
> 0) {
562 if (new->valid
>= x
->valid
&& new->valid
- x
->valid
< 60 &&
563 new->preferred
>= x
->preferred
&&
564 new->preferred
- x
->preferred
< 60 &&
565 x
->class == new->class)
567 x
->valid
= new->valid
;
568 x
->preferred
= new->preferred
;
571 x
->class = new->class;
574 odhcp6c_add_state(state
, new, sizeof(*new));
577 odhcp6c_remove_state(state
, (x
- start
) * sizeof(*x
), sizeof(*x
));
583 bool odhcp6c_update_entry(enum odhcp6c_state state
, struct odhcp6c_entry
*new)
585 return odhcp6c_update_entry_safe(state
, new, 0);
589 static void odhcp6c_expire_list(enum odhcp6c_state state
, uint32_t elapsed
)
592 struct odhcp6c_entry
*start
= odhcp6c_get_state(state
, &len
);
593 for (struct odhcp6c_entry
*c
= start
; c
< &start
[len
/ sizeof(*c
)]; ++c
) {
596 else if (c
->t1
!= UINT32_MAX
)
601 else if (c
->t2
!= UINT32_MAX
)
604 if (c
->preferred
< elapsed
)
606 else if (c
->preferred
!= UINT32_MAX
)
607 c
->preferred
-= elapsed
;
609 if (c
->valid
< elapsed
)
611 else if (c
->valid
!= UINT32_MAX
)
615 odhcp6c_remove_state(state
, (c
- start
) * sizeof(*c
), sizeof(*c
));
620 void odhcp6c_expire(void)
622 time_t now
= odhcp6c_get_milli_time() / 1000;
623 uint32_t elapsed
= (last_update
> 0) ? now
- last_update
: 0;
626 odhcp6c_expire_list(STATE_RA_PREFIX
, elapsed
);
627 odhcp6c_expire_list(STATE_RA_ROUTE
, elapsed
);
628 odhcp6c_expire_list(STATE_RA_DNS
, elapsed
);
629 odhcp6c_expire_list(STATE_IA_NA
, elapsed
);
630 odhcp6c_expire_list(STATE_IA_PD
, elapsed
);
634 uint32_t odhcp6c_elapsed(void)
636 return odhcp6c_get_milli_time() / 1000 - last_update
;
640 void odhcp6c_random(void *buf
, size_t len
)
642 read(urandom_fd
, buf
, len
);
645 bool odhcp6c_is_bound(void)
650 static void sighandler(int signal
)
652 if (signal
== SIGCHLD
)
653 while (waitpid(-1, NULL
, WNOHANG
) > 0);
654 else if (signal
== SIGUSR1
)
656 else if (signal
== SIGUSR2
)
658 else if (signal
== SIGIO
)