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.
28 #include <sys/syscall.h>
29 #include <arpa/inet.h>
36 static void sighandler(int signal
);
37 static int usage(void);
39 static uint8_t *state_data
[_STATE_MAX
] = {NULL
};
40 static size_t state_len
[_STATE_MAX
] = {0};
42 static volatile bool signal_io
= false;
43 static volatile bool signal_usr1
= false;
44 static volatile bool signal_usr2
= false;
45 static volatile bool signal_term
= false;
47 static int urandom_fd
= -1, allow_slaac_only
= 0;
48 static bool bound
= false, release
= true, ra
= false;
49 static time_t last_update
= 0;
51 static unsigned int min_update_interval
= DEFAULT_MIN_UPDATE_INTERVAL
;
52 static unsigned int script_sync_delay
= 10;
53 static unsigned int script_accu_delay
= 1;
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
;
73 bool help
= false, daemonize
= false;
76 unsigned int client_options
= DHCPV6_CLIENT_FQDN
| DHCPV6_ACCEPT_RECONFIGURE
;
78 while ((c
= getopt(argc
, argv
, "S::N:V:P:FB:c:i:r:Ru:s:kt:m:hedp:fav")) != -1) {
81 allow_slaac_only
= (optarg
) ? atoi(optarg
) : -1;
85 if (!strcmp(optarg
, "force")) {
86 ia_na_mode
= IA_MODE_FORCE
;
87 allow_slaac_only
= -1;
88 } else if (!strcmp(optarg
, "none")) {
89 ia_na_mode
= IA_MODE_NONE
;
90 } else if (!strcmp(optarg
, "try")) {
91 ia_na_mode
= IA_MODE_TRY
;
98 l
= script_unhexlify(buf
, sizeof(buf
), optarg
);
102 odhcp6c_add_state(STATE_VENDORCLASS
, buf
, l
);
106 if (ia_pd_mode
== IA_MODE_NONE
)
107 ia_pd_mode
= IA_MODE_TRY
;
109 if (allow_slaac_only
>= 0 && allow_slaac_only
< 10)
110 allow_slaac_only
= 10;
115 int prefix_length
= strtoul(optarg
, &iaid_begin
, 10);
117 if (*iaid_begin
!= '\0' && *iaid_begin
!= ',' && *iaid_begin
!= ':') {
118 syslog(LOG_ERR
, "invalid argument: '%s'", optarg
);
122 struct odhcp6c_request_prefix prefix
= { 0, prefix_length
};
124 if (*iaid_begin
== ',' && (iaid_len
= strlen(iaid_begin
)) > 1)
125 memcpy(&prefix
.iaid
, iaid_begin
+ 1, iaid_len
> 4 ? 4 : iaid_len
);
126 else if (*iaid_begin
== ':')
127 prefix
.iaid
= htonl((uint32_t)strtoul(&iaid_begin
[1], NULL
, 16));
129 prefix
.iaid
= htonl(++ia_pd_iaid_index
);
131 odhcp6c_add_state(STATE_IA_PD_INIT
, &prefix
, sizeof(prefix
));
136 allow_slaac_only
= -1;
137 ia_pd_mode
= IA_MODE_FORCE
;
141 l
= script_unhexlify(&buf
[4], sizeof(buf
) - 4, optarg
);
144 buf
[1] = DHCPV6_OPT_CLIENTID
;
147 odhcp6c_add_state(STATE_CLIENT_ID
, buf
, l
+ 4);
154 if (inet_pton(AF_INET6
, optarg
, &ifid
) != 1)
161 opttype
= htons(strtoul(optarg
, &optpos
, 10));
162 if (optpos
== optarg
)
166 odhcp6c_add_state(STATE_ORO
, &opttype
, 2);
171 client_options
|= DHCPV6_STRICT_OPTIONS
;
175 optlen
= htons(strlen(optarg
));
176 odhcp6c_add_state(STATE_USERCLASS
, &optlen
, 2);
177 odhcp6c_add_state(STATE_USERCLASS
, optarg
, strlen(optarg
));
189 sol_timeout
= atoi(optarg
);
193 min_update_interval
= atoi(optarg
);
197 logopt
|= LOG_PERROR
;
209 client_options
&= ~DHCPV6_CLIENT_FQDN
;
213 client_options
&= ~DHCPV6_ACCEPT_RECONFIGURE
;
226 if (allow_slaac_only
> 0)
227 script_sync_delay
= allow_slaac_only
;
229 openlog("odhcp6c", logopt
, LOG_DAEMON
);
231 setlogmask(LOG_UPTO(LOG_WARNING
));
233 const char *ifname
= argv
[optind
];
238 signal(SIGIO
, sighandler
);
239 signal(SIGHUP
, sighandler
);
240 signal(SIGINT
, sighandler
);
241 signal(SIGTERM
, sighandler
);
242 signal(SIGUSR1
, sighandler
);
243 signal(SIGUSR2
, sighandler
);
245 if ((urandom_fd
= open("/dev/urandom", O_CLOEXEC
| O_RDONLY
)) < 0 ||
246 init_dhcpv6(ifname
, client_options
, sol_timeout
) ||
247 ra_init(ifname
, &ifid
) || script_init(script
, ifname
)) {
248 syslog(LOG_ERR
, "failed to initialize: %s", strerror(errno
));
253 openlog("odhcp6c", LOG_PID
, LOG_DAEMON
); // Disable LOG_PERROR
255 syslog(LOG_ERR
, "Failed to daemonize: %s",
261 snprintf((char*)buf
, sizeof(buf
), "/var/run/odhcp6c.%s.pid", ifname
);
262 pidfile
= (char*)buf
;
265 FILE *fp
= fopen(pidfile
, "w");
267 fprintf(fp
, "%i\n", getpid());
272 script_call("started", 0, false);
274 while (!signal_term
) { // Main logic
275 odhcp6c_clear_state(STATE_SERVER_ID
);
276 odhcp6c_clear_state(STATE_SERVER_ADDR
);
277 odhcp6c_clear_state(STATE_IA_NA
);
278 odhcp6c_clear_state(STATE_IA_PD
);
279 odhcp6c_clear_state(STATE_SNTP_IP
);
280 odhcp6c_clear_state(STATE_NTP_IP
);
281 odhcp6c_clear_state(STATE_NTP_FQDN
);
282 odhcp6c_clear_state(STATE_SIP_IP
);
283 odhcp6c_clear_state(STATE_SIP_FQDN
);
286 syslog(LOG_NOTICE
, "(re)starting transaction on %s", ifname
);
288 signal_usr1
= signal_usr2
= false;
289 int mode
= dhcpv6_set_ia_mode(ia_na_mode
, ia_pd_mode
);
290 if (mode
!= DHCPV6_STATELESS
)
291 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", script_sync_delay
, true);
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", script_sync_delay
, true);
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", 0, false);
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", 0, false);
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", 0, true);
404 size_t ia_pd_len
, ia_na_len
, server_id_len
;
405 odhcp6c_get_state(STATE_IA_PD
, &ia_pd_len
);
406 odhcp6c_get_state(STATE_IA_NA
, &ia_na_len
);
407 odhcp6c_get_state(STATE_SERVER_ID
, &server_id_len
);
409 // Add all prefixes to lost prefixes
411 script_call("unbound", 0, true);
413 if (server_id_len
> 0 && (ia_pd_len
> 0 || ia_na_len
> 0) && release
)
414 dhcpv6_request(DHCPV6_MSG_RELEASE
);
416 odhcp6c_clear_state(STATE_IA_NA
);
417 odhcp6c_clear_state(STATE_IA_PD
);
420 script_call("stopped", 0, true);
425 static int usage(void)
428 "Usage: odhcp6c [options] <interface>\n"
429 "\nFeature options:\n"
430 " -S <time> Wait at least <time> sec for a DHCP-server (0)\n"
431 " -N <mode> Mode for requesting addresses [try|force|none]\n"
432 " -P <length> Request IPv6-Prefix (0 = auto)\n"
433 " -F Force IPv6-Prefix\n"
434 " -V <class> Set vendor-class option (base-16 encoded)\n"
435 " -u <user-class> Set user-class option string\n"
436 " -c <clientid> Override client-ID (base-16 encoded 16-bit type + value)\n"
437 " -i <iface-id> Use a custom interface identifier for RA handling\n"
438 " -r <options> Options to be requested (comma-separated)\n"
439 " -R Do not request any options except those specified with -r\n"
440 " -s <script> Status update script (/usr/sbin/odhcp6c-update)\n"
441 " -a Don't send Accept Reconfigure option\n"
442 " -f Don't send Client FQDN option\n"
443 " -k Don't send a RELEASE when stopping\n"
444 " -t <seconds> Maximum timeout for DHCPv6-SOLICIT (120)\n"
445 " -m <seconds> Minimum time between accepting updates (30)\n"
446 "\nInvocation options:\n"
447 " -p <pidfile> Set pidfile (/var/run/odhcp6c.pid)\n"
449 " -e Write logmessages to stderr\n"
450 " -v Increase logging verbosity\n"
451 " -h Show this help\n\n";
457 // Don't want to pull-in librt and libpthread just for a monotonic clock...
458 uint64_t odhcp6c_get_milli_time(void)
460 struct timespec t
= {0, 0};
461 syscall(SYS_clock_gettime
, CLOCK_MONOTONIC
, &t
);
462 return ((uint64_t)t
.tv_sec
) * 1000 + ((uint64_t)t
.tv_nsec
) / 1000000;
466 static uint8_t* odhcp6c_resize_state(enum odhcp6c_state state
, ssize_t len
)
469 return state_data
[state
] + state_len
[state
];
470 else if (state_len
[state
] + len
> 1024)
473 uint8_t *n
= realloc(state_data
[state
], state_len
[state
] + len
);
474 if (n
|| state_len
[state
] + len
== 0) {
475 state_data
[state
] = n
;
476 n
+= state_len
[state
];
477 state_len
[state
] += len
;
483 bool odhcp6c_signal_process(void)
488 bool ra_updated
= ra_process();
495 if (ra_updated
&& (bound
|| allow_slaac_only
>= 0)) {
496 script_call("ra-updated", (!ra
&& !bound
) ?
497 script_sync_delay
: script_accu_delay
, false);
502 return signal_usr1
|| signal_usr2
|| signal_term
;
506 void odhcp6c_clear_state(enum odhcp6c_state state
)
508 state_len
[state
] = 0;
512 void odhcp6c_add_state(enum odhcp6c_state state
, const void *data
, size_t len
)
514 uint8_t *n
= odhcp6c_resize_state(state
, len
);
516 memcpy(n
, data
, len
);
519 int odhcp6c_insert_state(enum odhcp6c_state state
, size_t offset
, const void *data
, size_t len
)
521 ssize_t len_after
= state_len
[state
] - offset
;
525 uint8_t *n
= odhcp6c_resize_state(state
, len
);
527 uint8_t *sdata
= state_data
[state
];
529 memmove(sdata
+ offset
+ len
, sdata
+ offset
, len_after
);
530 memcpy(sdata
+ offset
, data
, len
);
536 size_t odhcp6c_remove_state(enum odhcp6c_state state
, size_t offset
, size_t len
)
538 uint8_t *data
= state_data
[state
];
539 ssize_t len_after
= state_len
[state
] - (offset
+ len
);
541 return state_len
[state
];
543 memmove(data
+ offset
, data
+ offset
+ len
, len_after
);
544 return state_len
[state
] -= len
;
548 void* odhcp6c_move_state(enum odhcp6c_state state
, size_t *len
)
550 *len
= state_len
[state
];
551 void *data
= state_data
[state
];
553 state_len
[state
] = 0;
554 state_data
[state
] = NULL
;
560 void* odhcp6c_get_state(enum odhcp6c_state state
, size_t *len
)
562 *len
= state_len
[state
];
563 return state_data
[state
];
567 static struct odhcp6c_entry
* odhcp6c_find_entry(enum odhcp6c_state state
, const struct odhcp6c_entry
*new)
569 size_t len
, cmplen
= offsetof(struct odhcp6c_entry
, target
) + ((new->length
+ 7) / 8);
570 uint8_t *start
= odhcp6c_get_state(state
, &len
);
572 for (struct odhcp6c_entry
*c
= (struct odhcp6c_entry
*)start
;
573 (uint8_t*)c
< &start
[len
] && &c
->auxtarget
[c
->auxlen
] <= &start
[len
];
574 c
= (struct odhcp6c_entry
*)(&c
->auxtarget
[c
->auxlen
]))
575 if (!memcmp(c
, new, cmplen
) && !memcmp(c
->auxtarget
, new->auxtarget
, new->auxlen
))
582 bool odhcp6c_update_entry(enum odhcp6c_state state
, struct odhcp6c_entry
*new,
583 uint32_t safe
, bool filterexcess
)
586 struct odhcp6c_entry
*x
= odhcp6c_find_entry(state
, new);
587 uint8_t *start
= odhcp6c_get_state(state
, &len
);
589 if (x
&& x
->valid
> new->valid
&& new->valid
< safe
)
592 if (new->valid
> 0) {
594 if (filterexcess
&& new->valid
>= x
->valid
&&
595 new->valid
!= UINT32_MAX
&&
596 new->valid
- x
->valid
< min_update_interval
&&
597 new->preferred
>= x
->preferred
&&
598 new->preferred
!= UINT32_MAX
&&
599 new->preferred
- x
->preferred
< min_update_interval
)
601 x
->valid
= new->valid
;
602 x
->preferred
= new->preferred
;
607 odhcp6c_add_state(state
, new, sizeof(*new) + new->auxlen
);
610 odhcp6c_remove_state(state
, ((uint8_t*)x
) - start
, sizeof(*x
) + x
->auxlen
);
616 static void odhcp6c_expire_list(enum odhcp6c_state state
, uint32_t elapsed
)
619 uint8_t *start
= odhcp6c_get_state(state
, &len
);
620 for (struct odhcp6c_entry
*c
= (struct odhcp6c_entry
*)start
;
621 (uint8_t*)c
< &start
[len
] && &c
->auxtarget
[c
->auxlen
] <= &start
[len
];
625 else if (c
->t1
!= UINT32_MAX
)
630 else if (c
->t2
!= UINT32_MAX
)
633 if (c
->preferred
< elapsed
)
635 else if (c
->preferred
!= UINT32_MAX
)
636 c
->preferred
-= elapsed
;
638 if (c
->valid
< elapsed
)
640 else if (c
->valid
!= UINT32_MAX
)
644 odhcp6c_remove_state(state
, ((uint8_t*)c
) - start
, sizeof(*c
) + c
->auxlen
);
645 start
= odhcp6c_get_state(state
, &len
);
647 c
= (struct odhcp6c_entry
*)(&c
->auxtarget
[c
->auxlen
]);
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_RA_SEARCH
, elapsed
);
663 odhcp6c_expire_list(STATE_IA_NA
, elapsed
);
664 odhcp6c_expire_list(STATE_IA_PD
, elapsed
);
668 uint32_t odhcp6c_elapsed(void)
670 return odhcp6c_get_milli_time() / 1000 - last_update
;
674 int odhcp6c_random(void *buf
, size_t len
)
676 return read(urandom_fd
, buf
, len
);
679 bool odhcp6c_is_bound(void)
684 static void sighandler(int signal
)
686 if (signal
== SIGUSR1
)
688 else if (signal
== SIGUSR2
)
690 else if (signal
== SIGIO
)