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 int do_signal
= 0;
47 static int urandom_fd
= -1, allow_slaac_only
= 0;
48 static bool bound
= false, release
= true;
49 static time_t last_update
= 0;
52 int main(_unused
int argc
, char* const argv
[])
54 // Allocate ressources
55 const char *pidfile
= NULL
;
56 const char *script
= "/usr/sbin/odhcp6c-update";
61 enum odhcp6c_ia_mode ia_na_mode
= IA_MODE_TRY
;
62 enum odhcp6c_ia_mode ia_pd_mode
= IA_MODE_TRY
;
63 static struct in6_addr ifid
= IN6ADDR_ANY_INIT
;
64 int sol_timeout
= 120;
67 int bfd_interval
= 0, bfd_loss
= 3;
70 bool help
= false, daemonize
= false;
72 int c
, request_pd
= 0;
73 while ((c
= getopt(argc
, argv
, "S::N:P:FB:c:i:r:s:kt:hedp:")) != -1) {
76 allow_slaac_only
= (optarg
) ? atoi(optarg
) : -1;
80 if (!strcmp(optarg
, "force")) {
81 ia_na_mode
= IA_MODE_FORCE
;
82 allow_slaac_only
= -1;
83 } else if (!strcmp(optarg
, "none")) {
84 ia_na_mode
= IA_MODE_NONE
;
85 } else if (!strcmp(optarg
, "try")) {
86 ia_na_mode
= IA_MODE_TRY
;
93 if (allow_slaac_only
>= 0 && allow_slaac_only
< 10)
94 allow_slaac_only
= 10;
96 request_pd
= strtoul(optarg
, NULL
, 10);
103 allow_slaac_only
= -1;
104 ia_pd_mode
= IA_MODE_FORCE
;
109 bfd_interval
= atoi(optarg
);
114 l
= script_unhexlify(&buf
[4], sizeof(buf
) - 4, optarg
);
117 buf
[1] = DHCPV6_OPT_CLIENTID
;
120 odhcp6c_add_state(STATE_CLIENT_ID
, buf
, l
+ 4);
127 if (inet_pton(AF_INET6
, optarg
, &ifid
) != 1)
134 opttype
= htons(strtoul(optarg
, &optpos
, 10));
135 if (optpos
== optarg
)
139 odhcp6c_add_state(STATE_ORO
, &opttype
, 2);
152 sol_timeout
= atoi(optarg
);
156 logopt
|= LOG_PERROR
;
173 openlog("odhcp6c", logopt
, LOG_DAEMON
);
174 const char *ifname
= argv
[optind
];
179 signal(SIGIO
, sighandler
);
180 signal(SIGHUP
, sighandler
);
181 signal(SIGINT
, sighandler
);
182 signal(SIGCHLD
, sighandler
);
183 signal(SIGTERM
, sighandler
);
184 signal(SIGUSR1
, sighandler
);
185 signal(SIGUSR2
, sighandler
);
187 if ((urandom_fd
= open("/dev/urandom", O_CLOEXEC
| O_RDONLY
)) < 0 ||
188 init_dhcpv6(ifname
, request_pd
, sol_timeout
) ||
189 ra_init(ifname
, &ifid
) || script_init(script
, ifname
)) {
190 syslog(LOG_ERR
, "failed to initialize: %s", strerror(errno
));
195 openlog("odhcp6c", LOG_PID
, LOG_DAEMON
); // Disable LOG_PERROR
197 syslog(LOG_ERR
, "Failed to daemonize: %s",
204 snprintf(pidbuf
, sizeof(pidbuf
),
205 "/var/run/odhcp6c.%s.pid", ifname
);
209 int fd
= open(pidfile
, O_WRONLY
| O_CREAT
);
212 int len
= snprintf(buf
, sizeof(buf
), "%i\n", getpid());
218 script_call("started");
220 while (do_signal
!= SIGTERM
) { // Main logic
221 odhcp6c_clear_state(STATE_SERVER_ID
);
222 odhcp6c_clear_state(STATE_IA_NA
);
223 odhcp6c_clear_state(STATE_IA_PD
);
224 odhcp6c_clear_state(STATE_SNTP_IP
);
225 odhcp6c_clear_state(STATE_SNTP_FQDN
);
226 odhcp6c_clear_state(STATE_SIP_IP
);
227 odhcp6c_clear_state(STATE_SIP_FQDN
);
228 dhcpv6_set_ia_mode(ia_na_mode
, ia_pd_mode
);
231 // Server candidates need deep-delete
233 struct dhcpv6_server_cand
*cand
= odhcp6c_get_state(STATE_SERVER_CAND
, &cand_len
);
234 for (size_t i
= 0; i
< cand_len
/ sizeof(*cand
); ++i
) {
238 odhcp6c_clear_state(STATE_SERVER_CAND
);
240 syslog(LOG_NOTICE
, "(re)starting transaction on %s", ifname
);
243 int res
= dhcpv6_request(DHCPV6_MSG_SOLICIT
);
244 odhcp6c_signal_process();
247 continue; // Might happen if we got a signal
248 } else if (res
== DHCPV6_STATELESS
) { // Stateless mode
249 while (do_signal
== 0 || do_signal
== SIGUSR1
) {
252 res
= dhcpv6_request(DHCPV6_MSG_INFO_REQ
);
253 odhcp6c_signal_process();
254 if (do_signal
== SIGUSR1
)
259 script_call("informed");
262 syslog(LOG_NOTICE
, "entering stateless-mode on %s", ifname
);
264 if (dhcpv6_poll_reconfigure() > 0)
265 script_call("informed");
272 if (dhcpv6_request(DHCPV6_MSG_REQUEST
) <= 0)
275 odhcp6c_signal_process();
276 script_call("bound");
278 syslog(LOG_NOTICE
, "entering stateful-mode on %s", ifname
);
280 if (bfd_interval
> 0)
281 bfd_start(ifname
, bfd_loss
, bfd_interval
);
284 while (do_signal
== 0 || do_signal
== SIGUSR1
) {
286 // Wait for T1 to expire or until we get a reconfigure
287 int res
= dhcpv6_poll_reconfigure();
288 odhcp6c_signal_process();
290 script_call("updated");
294 // Handle signal, if necessary
295 if (do_signal
== SIGUSR1
)
296 do_signal
= 0; // Acknowledged
297 else if (do_signal
> 0)
298 break; // Other signal type
300 size_t ia_pd_len
, ia_na_len
, ia_pd_new
, ia_na_new
;
301 odhcp6c_get_state(STATE_IA_PD
, &ia_pd_len
);
302 odhcp6c_get_state(STATE_IA_NA
, &ia_na_len
);
304 // If we have any IAs, send renew, otherwise request
306 if (ia_pd_len
== 0 && ia_na_len
== 0)
307 r
= dhcpv6_request(DHCPV6_MSG_REQUEST
);
309 r
= dhcpv6_request(DHCPV6_MSG_RENEW
);
310 odhcp6c_signal_process();
311 if (r
> 0) { // Renew was succesfull
313 script_call("updated");
314 continue; // Renew was successful
317 odhcp6c_clear_state(STATE_SERVER_ID
); // Remove binding
319 // If we have IAs, try rebind otherwise restart
320 res
= dhcpv6_request(DHCPV6_MSG_REBIND
);
321 odhcp6c_signal_process();
323 odhcp6c_get_state(STATE_IA_PD
, &ia_pd_new
);
324 odhcp6c_get_state(STATE_IA_NA
, &ia_na_new
);
325 if (res
<= 0 || (ia_pd_new
== 0 && ia_pd_len
) ||
326 (ia_na_new
== 0 && ia_na_len
))
327 break; // We lost all our IAs, restart
329 script_call("rebound");
337 size_t ia_pd_len
, ia_na_len
, server_id_len
;
338 odhcp6c_get_state(STATE_IA_PD
, &ia_pd_len
);
339 odhcp6c_get_state(STATE_IA_NA
, &ia_na_len
);
340 odhcp6c_get_state(STATE_SERVER_ID
, &server_id_len
);
342 // Add all prefixes to lost prefixes
344 script_call("unbound");
346 if (server_id_len
> 0 && (ia_pd_len
> 0 || ia_na_len
> 0) && release
)
347 dhcpv6_request(DHCPV6_MSG_RELEASE
);
349 odhcp6c_clear_state(STATE_IA_NA
);
350 odhcp6c_clear_state(STATE_IA_PD
);
353 script_call("stopped");
358 static int usage(void)
361 "Usage: odhcp6c [options] <interface>\n"
362 "\nFeature options:\n"
363 " -S <time> Wait at least <time> sec for a DHCP-server (0)\n"
364 " -N <mode> Mode for requesting addresses [try|force|none]\n"
365 " -P <length> Request IPv6-Prefix (0 = auto)\n"
366 " -F Force IPv6-Prefix\n"
368 " -B <interval> Enable BFD ping check\n"
370 " -c <clientid> Override client-ID (base-16 encoded)\n"
371 " -i <iface-id> Use a custom interface identifier for RA handling\n"
372 " -r <options> Options to be requested (comma-separated)\n"
373 " -s <script> Status update script (/usr/sbin/odhcp6c-update)\n"
374 " -k Don't send a RELEASE when stopping\n"
375 " -t <seconds> Maximum timeout for DHCPv6-SOLICIT (120)\n"
376 "\nInvocation options:\n"
377 " -p <pidfile> Set pidfile (/var/run/6relayd.pid)\n"
379 " -e Write logmessages to stderr\n"
380 //" -v Increase logging verbosity\n"
381 " -h Show this help\n\n";
382 write(STDERR_FILENO
, buf
, sizeof(buf
));
387 // Don't want to pull-in librt and libpthread just for a monotonic clock...
388 uint64_t odhcp6c_get_milli_time(void)
390 struct timespec t
= {0, 0};
391 syscall(SYS_clock_gettime
, CLOCK_MONOTONIC
, &t
);
392 return t
.tv_sec
* 1000 + t
.tv_nsec
/ 1000000;
396 static uint8_t* odhcp6c_resize_state(enum odhcp6c_state state
, ssize_t len
)
399 return state_data
[state
] + state_len
[state
];
400 else if (state_len
[state
] + len
> 1024)
403 uint8_t *n
= realloc(state_data
[state
], state_len
[state
] + len
);
404 if (n
|| state_len
[state
] + len
== 0) {
405 state_data
[state
] = n
;
406 n
+= state_len
[state
];
407 state_len
[state
] += len
;
413 bool odhcp6c_signal_process(void)
415 if (do_signal
== SIGIO
) {
417 bool ra_updated
= ra_process();
422 if (ra_updated
&& (bound
|| allow_slaac_only
== 0))
423 script_call("ra-updated"); // Immediate process urgent events
424 else if (ra_updated
&& !bound
&& allow_slaac_only
> 0)
425 script_delay_call("ra-updated", allow_slaac_only
);
432 return do_signal
!= 0;
436 void odhcp6c_clear_state(enum odhcp6c_state state
)
438 state_len
[state
] = 0;
442 void odhcp6c_add_state(enum odhcp6c_state state
, const void *data
, size_t len
)
444 uint8_t *n
= odhcp6c_resize_state(state
, len
);
446 memcpy(n
, data
, len
);
450 size_t odhcp6c_remove_state(enum odhcp6c_state state
, size_t offset
, size_t len
)
452 uint8_t *data
= state_data
[state
];
453 ssize_t len_after
= state_len
[state
] - (offset
+ len
);
455 return state_len
[state
];
457 memmove(data
+ offset
, data
+ offset
+ len
, len_after
);
458 return state_len
[state
] -= len
;
462 void* odhcp6c_move_state(enum odhcp6c_state state
, size_t *len
)
464 *len
= state_len
[state
];
465 void *data
= state_data
[state
];
467 state_len
[state
] = 0;
468 state_data
[state
] = NULL
;
474 void* odhcp6c_get_state(enum odhcp6c_state state
, size_t *len
)
476 *len
= state_len
[state
];
477 return state_data
[state
];
481 struct odhcp6c_entry
* odhcp6c_find_entry(enum odhcp6c_state state
, const struct odhcp6c_entry
*new)
483 size_t len
, cmplen
= offsetof(struct odhcp6c_entry
, target
) + new->length
/ 8;
484 struct odhcp6c_entry
*start
= odhcp6c_get_state(state
, &len
);
485 struct odhcp6c_entry
*x
= NULL
;
487 for (struct odhcp6c_entry
*c
= start
; !x
&& c
< &start
[len
/sizeof(*c
)]; ++c
)
488 if (!memcmp(c
, new, cmplen
))
495 bool odhcp6c_update_entry_safe(enum odhcp6c_state state
, struct odhcp6c_entry
*new, uint32_t safe
)
498 struct odhcp6c_entry
*x
= odhcp6c_find_entry(state
, new);
499 struct odhcp6c_entry
*start
= odhcp6c_get_state(state
, &len
);
501 if (x
&& x
->valid
> new->valid
&& new->valid
< safe
)
504 if (new->valid
> 0) {
506 if (new->valid
>= x
->valid
&& new->valid
- x
->valid
< 60 &&
507 new->preferred
>= x
->preferred
&&
508 new->preferred
- x
->preferred
< 60 &&
509 x
->class == new->class)
511 x
->valid
= new->valid
;
512 x
->preferred
= new->preferred
;
515 x
->class = new->class;
517 odhcp6c_add_state(state
, new, sizeof(*new));
520 odhcp6c_remove_state(state
, (x
- start
) * sizeof(*x
), sizeof(*x
));
526 bool odhcp6c_update_entry(enum odhcp6c_state state
, struct odhcp6c_entry
*new)
528 return odhcp6c_update_entry_safe(state
, new, 0);
532 static void odhcp6c_expire_list(enum odhcp6c_state state
, uint32_t elapsed
)
535 struct odhcp6c_entry
*start
= odhcp6c_get_state(state
, &len
);
536 for (struct odhcp6c_entry
*c
= start
; c
< &start
[len
/ sizeof(*c
)]; ++c
) {
539 else if (c
->t1
!= UINT32_MAX
)
544 else if (c
->t2
!= UINT32_MAX
)
547 if (c
->preferred
< elapsed
)
549 else if (c
->preferred
!= UINT32_MAX
)
550 c
->preferred
-= elapsed
;
552 if (c
->valid
< elapsed
)
554 else if (c
->valid
!= UINT32_MAX
)
558 odhcp6c_remove_state(state
, (c
- start
) * sizeof(*c
), sizeof(*c
));
563 void odhcp6c_expire(void)
565 time_t now
= odhcp6c_get_milli_time() / 1000;
566 uint32_t elapsed
= (last_update
> 0) ? now
- last_update
: 0;
569 odhcp6c_expire_list(STATE_RA_PREFIX
, elapsed
);
570 odhcp6c_expire_list(STATE_RA_ROUTE
, elapsed
);
571 odhcp6c_expire_list(STATE_RA_DNS
, elapsed
);
572 odhcp6c_expire_list(STATE_IA_NA
, elapsed
);
573 odhcp6c_expire_list(STATE_IA_PD
, elapsed
);
577 uint32_t odhcp6c_elapsed(void)
579 return odhcp6c_get_milli_time() / 1000 - last_update
;
583 void odhcp6c_random(void *buf
, size_t len
)
585 read(urandom_fd
, buf
, len
);
588 bool odhcp6c_is_bound(void)
593 static void sighandler(int signal
)
595 if (signal
== SIGCHLD
)
596 while (waitpid(-1, NULL
, WNOHANG
) > 0);
597 else if (signal
== SIGUSR1
)
599 else if (signal
== SIGUSR2
)
601 else if (signal
== SIGIO
)