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>
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 int do_signal
= 0;
43 static int urandom_fd
= -1, allow_slaac_only
= 0;
44 static bool bound
= false, release
= true;
45 static time_t last_update
= 0;
48 int main(_unused
int argc
, char* const argv
[])
50 // Allocate ressources
51 const char *pidfile
= NULL
;
52 const char *script
= "/usr/sbin/odhcp6c-update";
57 enum odhcp6c_ia_mode ia_na_mode
= IA_MODE_TRY
;
58 enum odhcp6c_ia_mode ia_pd_mode
= IA_MODE_TRY
;
59 static struct in6_addr ifid
= IN6ADDR_ANY_INIT
;
60 int sol_timeout
= 120;
62 bool help
= false, daemonize
= false;
64 int c
, request_pd
= 0;
65 while ((c
= getopt(argc
, argv
, "S::N:P:Fc:i:r:s:kt:hedp:")) != -1) {
68 allow_slaac_only
= (optarg
) ? atoi(optarg
) : -1;
72 if (!strcmp(optarg
, "force")) {
73 ia_na_mode
= IA_MODE_FORCE
;
74 allow_slaac_only
= -1;
75 } else if (!strcmp(optarg
, "none")) {
76 ia_na_mode
= IA_MODE_NONE
;
77 } else if (!strcmp(optarg
, "try")) {
78 ia_na_mode
= IA_MODE_TRY
;
85 if (allow_slaac_only
>= 0 && allow_slaac_only
< 10)
86 allow_slaac_only
= 10;
88 request_pd
= strtoul(optarg
, NULL
, 10);
95 allow_slaac_only
= -1;
96 ia_pd_mode
= IA_MODE_FORCE
;
100 l
= script_unhexlify(&buf
[4], sizeof(buf
) - 4, optarg
);
103 buf
[1] = DHCPV6_OPT_CLIENTID
;
106 odhcp6c_add_state(STATE_CLIENT_ID
, buf
, l
+ 4);
113 if (inet_pton(AF_INET6
, optarg
, &ifid
) != 1)
120 opttype
= htons(strtoul(optarg
, &optpos
, 10));
121 if (optpos
== optarg
)
125 odhcp6c_add_state(STATE_ORO
, &opttype
, 2);
138 sol_timeout
= atoi(optarg
);
142 logopt
|= LOG_PERROR
;
159 openlog("odhcp6c", logopt
, LOG_DAEMON
);
160 const char *ifname
= argv
[optind
];
165 signal(SIGIO
, sighandler
);
166 signal(SIGHUP
, sighandler
);
167 signal(SIGINT
, sighandler
);
168 signal(SIGCHLD
, sighandler
);
169 signal(SIGTERM
, sighandler
);
170 signal(SIGUSR1
, sighandler
);
171 signal(SIGUSR2
, sighandler
);
173 if ((urandom_fd
= open("/dev/urandom", O_CLOEXEC
| O_RDONLY
)) < 0 ||
174 init_dhcpv6(ifname
, request_pd
, sol_timeout
) ||
175 ra_init(ifname
, &ifid
) || script_init(script
, ifname
)) {
176 syslog(LOG_ERR
, "failed to initialize: %s", strerror(errno
));
181 openlog("odhcp6c", LOG_PID
, LOG_DAEMON
); // Disable LOG_PERROR
183 syslog(LOG_ERR
, "Failed to daemonize: %s",
190 snprintf(pidbuf
, sizeof(pidbuf
),
191 "/var/run/odhcp6c.%s.pid", ifname
);
195 int fd
= open(pidfile
, O_WRONLY
| O_CREAT
);
198 int len
= snprintf(buf
, sizeof(buf
), "%i\n", getpid());
204 script_call("started");
206 while (do_signal
!= SIGTERM
) { // Main logic
207 odhcp6c_clear_state(STATE_SERVER_ID
);
208 odhcp6c_clear_state(STATE_IA_NA
);
209 odhcp6c_clear_state(STATE_IA_PD
);
210 odhcp6c_clear_state(STATE_SNTP_IP
);
211 odhcp6c_clear_state(STATE_SNTP_FQDN
);
212 odhcp6c_clear_state(STATE_SIP_IP
);
213 odhcp6c_clear_state(STATE_SIP_FQDN
);
214 dhcpv6_set_ia_mode(ia_na_mode
, ia_pd_mode
);
217 // Server candidates need deep-delete
219 struct dhcpv6_server_cand
*cand
= odhcp6c_get_state(STATE_SERVER_CAND
, &cand_len
);
220 for (size_t i
= 0; i
< cand_len
/ sizeof(*cand
); ++i
) {
224 odhcp6c_clear_state(STATE_SERVER_CAND
);
226 syslog(LOG_NOTICE
, "(re)starting transaction on %s", ifname
);
229 int res
= dhcpv6_request(DHCPV6_MSG_SOLICIT
);
230 odhcp6c_signal_process();
233 continue; // Might happen if we got a signal
234 } else if (res
== DHCPV6_STATELESS
) { // Stateless mode
235 while (do_signal
== 0 || do_signal
== SIGUSR1
) {
238 res
= dhcpv6_request(DHCPV6_MSG_INFO_REQ
);
239 odhcp6c_signal_process();
240 if (do_signal
== SIGUSR1
)
245 script_call("informed");
248 syslog(LOG_NOTICE
, "entering stateless-mode on %s", ifname
);
250 if (dhcpv6_poll_reconfigure() > 0)
251 script_call("informed");
258 if (dhcpv6_request(DHCPV6_MSG_REQUEST
) <= 0)
261 odhcp6c_signal_process();
262 script_call("bound");
264 syslog(LOG_NOTICE
, "entering stateful-mode on %s", ifname
);
266 while (do_signal
== 0 || do_signal
== SIGUSR1
) {
268 // Wait for T1 to expire or until we get a reconfigure
269 int res
= dhcpv6_poll_reconfigure();
270 odhcp6c_signal_process();
272 script_call("updated");
276 // Handle signal, if necessary
277 if (do_signal
== SIGUSR1
)
278 do_signal
= 0; // Acknowledged
279 else if (do_signal
> 0)
280 break; // Other signal type
282 size_t ia_pd_len
, ia_na_len
, ia_pd_new
, ia_na_new
;
283 odhcp6c_get_state(STATE_IA_PD
, &ia_pd_len
);
284 odhcp6c_get_state(STATE_IA_NA
, &ia_na_len
);
286 // If we have any IAs, send renew, otherwise request
288 if (ia_pd_len
== 0 && ia_na_len
== 0)
289 r
= dhcpv6_request(DHCPV6_MSG_REQUEST
);
291 r
= dhcpv6_request(DHCPV6_MSG_RENEW
);
292 odhcp6c_signal_process();
293 if (r
> 0) { // Renew was succesfull
295 script_call("updated");
296 continue; // Renew was successful
299 odhcp6c_clear_state(STATE_SERVER_ID
); // Remove binding
301 // If we have IAs, try rebind otherwise restart
302 res
= dhcpv6_request(DHCPV6_MSG_REBIND
);
303 odhcp6c_signal_process();
305 odhcp6c_get_state(STATE_IA_PD
, &ia_pd_new
);
306 odhcp6c_get_state(STATE_IA_NA
, &ia_na_new
);
307 if (res
<= 0 || (ia_pd_new
== 0 && ia_pd_len
) ||
308 (ia_na_new
== 0 && ia_na_len
))
309 break; // We lost all our IAs, restart
311 script_call("rebound");
315 size_t ia_pd_len
, ia_na_len
, server_id_len
;
316 odhcp6c_get_state(STATE_IA_PD
, &ia_pd_len
);
317 odhcp6c_get_state(STATE_IA_NA
, &ia_na_len
);
318 odhcp6c_get_state(STATE_SERVER_ID
, &server_id_len
);
320 // Add all prefixes to lost prefixes
322 script_call("unbound");
324 if (server_id_len
> 0 && (ia_pd_len
> 0 || ia_na_len
> 0) && release
)
325 dhcpv6_request(DHCPV6_MSG_RELEASE
);
327 odhcp6c_clear_state(STATE_IA_NA
);
328 odhcp6c_clear_state(STATE_IA_PD
);
331 script_call("stopped");
336 static int usage(void)
339 "Usage: odhcp6c [options] <interface>\n"
340 "\nFeature options:\n"
341 " -S <time> Wait at least <time> sec for a DHCP-server (0)\n"
342 " -N <mode> Mode for requesting addresses [try|force|none]\n"
343 " -P <length> Request IPv6-Prefix (0 = auto)\n"
344 " -F Force IPv6-Prefix\n"
345 " -c <clientid> Override client-ID (base-16 encoded)\n"
346 " -i <iface-id> Use a custom interface identifier for RA handling\n"
347 " -r <options> Options to be requested (comma-separated)\n"
348 " -s <script> Status update script (/usr/sbin/odhcp6c-update)\n"
349 " -k Don't send a RELEASE when stopping\n"
350 " -t <seconds> Maximum timeout for DHCPv6-SOLICIT (120)\n"
351 "\nInvocation options:\n"
352 " -p <pidfile> Set pidfile (/var/run/6relayd.pid)\n"
354 " -e Write logmessages to stderr\n"
355 //" -v Increase logging verbosity\n"
356 " -h Show this help\n\n";
357 write(STDERR_FILENO
, buf
, sizeof(buf
));
362 // Don't want to pull-in librt and libpthread just for a monotonic clock...
363 uint64_t odhcp6c_get_milli_time(void)
365 struct timespec t
= {0, 0};
366 syscall(SYS_clock_gettime
, CLOCK_MONOTONIC
, &t
);
367 return t
.tv_sec
* 1000 + t
.tv_nsec
/ 1000000;
371 static uint8_t* odhcp6c_resize_state(enum odhcp6c_state state
, ssize_t len
)
374 return state_data
[state
] + state_len
[state
];
375 else if (state_len
[state
] + len
> 1024)
378 uint8_t *n
= realloc(state_data
[state
], state_len
[state
] + len
);
379 if (n
|| state_len
[state
] + len
== 0) {
380 state_data
[state
] = n
;
381 n
+= state_len
[state
];
382 state_len
[state
] += len
;
388 bool odhcp6c_signal_process(void)
390 if (do_signal
== SIGIO
) {
392 bool ra_updated
= ra_process();
397 if (ra_updated
&& (bound
|| allow_slaac_only
== 0))
398 script_call("ra-updated"); // Immediate process urgent events
399 else if (ra_updated
&& !bound
&& allow_slaac_only
> 0)
400 script_delay_call("ra-updated", allow_slaac_only
);
403 return do_signal
!= 0;
407 void odhcp6c_clear_state(enum odhcp6c_state state
)
409 state_len
[state
] = 0;
413 void odhcp6c_add_state(enum odhcp6c_state state
, const void *data
, size_t len
)
415 uint8_t *n
= odhcp6c_resize_state(state
, len
);
417 memcpy(n
, data
, len
);
421 size_t odhcp6c_remove_state(enum odhcp6c_state state
, size_t offset
, size_t len
)
423 uint8_t *data
= state_data
[state
];
424 ssize_t len_after
= state_len
[state
] - (offset
+ len
);
426 return state_len
[state
];
428 memmove(data
+ offset
, data
+ offset
+ len
, len_after
);
429 return state_len
[state
] -= len
;
433 void* odhcp6c_move_state(enum odhcp6c_state state
, size_t *len
)
435 *len
= state_len
[state
];
436 void *data
= state_data
[state
];
438 state_len
[state
] = 0;
439 state_data
[state
] = NULL
;
445 void* odhcp6c_get_state(enum odhcp6c_state state
, size_t *len
)
447 *len
= state_len
[state
];
448 return state_data
[state
];
452 struct odhcp6c_entry
* odhcp6c_find_entry(enum odhcp6c_state state
, const struct odhcp6c_entry
*new)
454 size_t len
, cmplen
= offsetof(struct odhcp6c_entry
, target
) + new->length
/ 8;
455 struct odhcp6c_entry
*start
= odhcp6c_get_state(state
, &len
);
456 struct odhcp6c_entry
*x
= NULL
;
458 for (struct odhcp6c_entry
*c
= start
; !x
&& c
< &start
[len
/sizeof(*c
)]; ++c
)
459 if (!memcmp(c
, new, cmplen
))
466 bool odhcp6c_update_entry_safe(enum odhcp6c_state state
, struct odhcp6c_entry
*new, uint32_t safe
)
469 struct odhcp6c_entry
*x
= odhcp6c_find_entry(state
, new);
470 struct odhcp6c_entry
*start
= odhcp6c_get_state(state
, &len
);
472 if (x
&& x
->valid
> new->valid
&& new->valid
< safe
)
475 if (new->valid
> 0) {
477 if (new->valid
>= x
->valid
&& new->valid
- x
->valid
< 60 &&
478 new->preferred
>= x
->preferred
&&
479 new->preferred
- x
->preferred
< 60 &&
480 x
->class == new->class)
482 x
->valid
= new->valid
;
483 x
->preferred
= new->preferred
;
486 x
->class = new->class;
488 odhcp6c_add_state(state
, new, sizeof(*new));
491 odhcp6c_remove_state(state
, (x
- start
) * sizeof(*x
), sizeof(*x
));
497 bool odhcp6c_update_entry(enum odhcp6c_state state
, struct odhcp6c_entry
*new)
499 return odhcp6c_update_entry_safe(state
, new, 0);
503 static void odhcp6c_expire_list(enum odhcp6c_state state
, uint32_t elapsed
)
506 struct odhcp6c_entry
*start
= odhcp6c_get_state(state
, &len
);
507 for (struct odhcp6c_entry
*c
= start
; c
< &start
[len
/ sizeof(*c
)]; ++c
) {
510 else if (c
->t1
!= UINT32_MAX
)
515 else if (c
->t2
!= UINT32_MAX
)
518 if (c
->preferred
< elapsed
)
520 else if (c
->preferred
!= UINT32_MAX
)
521 c
->preferred
-= elapsed
;
523 if (c
->valid
< elapsed
)
525 else if (c
->valid
!= UINT32_MAX
)
529 odhcp6c_remove_state(state
, (c
- start
) * sizeof(*c
), sizeof(*c
));
534 void odhcp6c_expire(void)
536 time_t now
= odhcp6c_get_milli_time() / 1000;
537 uint32_t elapsed
= (last_update
> 0) ? now
- last_update
: 0;
540 odhcp6c_expire_list(STATE_RA_PREFIX
, elapsed
);
541 odhcp6c_expire_list(STATE_RA_ROUTE
, elapsed
);
542 odhcp6c_expire_list(STATE_RA_DNS
, elapsed
);
543 odhcp6c_expire_list(STATE_IA_NA
, elapsed
);
544 odhcp6c_expire_list(STATE_IA_PD
, elapsed
);
548 uint32_t odhcp6c_elapsed(void)
550 return odhcp6c_get_milli_time() / 1000 - last_update
;
554 void odhcp6c_random(void *buf
, size_t len
)
556 read(urandom_fd
, buf
, len
);
559 bool odhcp6c_is_bound(void)
564 static void sighandler(int signal
)
566 if (signal
== SIGCHLD
)
567 while (waitpid(-1, NULL
, WNOHANG
) > 0);
568 else if (signal
== SIGUSR1
)
570 else if (signal
== SIGUSR2
)
572 else if (signal
== SIGIO
)