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;
56 int main(_unused
int argc
, char* const argv
[])
58 // Allocate ressources
59 const char *pidfile
= NULL
;
60 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_TRY
;
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;
76 int c
, request_pd
= 0;
77 while ((c
= getopt(argc
, argv
, "S::N:P:FB:c:i:r:s: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;
100 request_pd
= strtoul(optarg
, NULL
, 10);
107 allow_slaac_only
= -1;
108 ia_pd_mode
= IA_MODE_FORCE
;
113 bfd_interval
= atoi(optarg
);
118 l
= script_unhexlify(&buf
[4], sizeof(buf
) - 4, optarg
);
121 buf
[1] = DHCPV6_OPT_CLIENTID
;
124 odhcp6c_add_state(STATE_CLIENT_ID
, buf
, l
+ 4);
131 if (inet_pton(AF_INET6
, optarg
, &ifid
) != 1)
138 opttype
= htons(strtoul(optarg
, &optpos
, 10));
139 if (optpos
== optarg
)
143 odhcp6c_add_state(STATE_ORO
, &opttype
, 2);
156 sol_timeout
= atoi(optarg
);
160 logopt
|= LOG_PERROR
;
177 openlog("odhcp6c", logopt
, LOG_DAEMON
);
178 const char *ifname
= argv
[optind
];
183 signal(SIGIO
, sighandler
);
184 signal(SIGHUP
, sighandler
);
185 signal(SIGINT
, sighandler
);
186 signal(SIGCHLD
, sighandler
);
187 signal(SIGTERM
, sighandler
);
188 signal(SIGUSR1
, sighandler
);
189 signal(SIGUSR2
, sighandler
);
191 if ((urandom_fd
= open("/dev/urandom", O_CLOEXEC
| O_RDONLY
)) < 0 ||
192 init_dhcpv6(ifname
, request_pd
, sol_timeout
) ||
193 ra_init(ifname
, &ifid
) || script_init(script
, ifname
)) {
194 syslog(LOG_ERR
, "failed to initialize: %s", strerror(errno
));
199 openlog("odhcp6c", LOG_PID
, LOG_DAEMON
); // Disable LOG_PERROR
201 syslog(LOG_ERR
, "Failed to daemonize: %s",
208 snprintf(pidbuf
, sizeof(pidbuf
),
209 "/var/run/odhcp6c.%s.pid", ifname
);
213 int fd
= open(pidfile
, O_WRONLY
| O_CREAT
);
216 int len
= snprintf(buf
, sizeof(buf
), "%i\n", getpid());
222 script_call("started");
224 while (!signal_term
) { // Main logic
225 odhcp6c_clear_state(STATE_SERVER_ID
);
226 odhcp6c_clear_state(STATE_IA_NA
);
227 odhcp6c_clear_state(STATE_IA_PD
);
228 odhcp6c_clear_state(STATE_SNTP_IP
);
229 odhcp6c_clear_state(STATE_SNTP_FQDN
);
230 odhcp6c_clear_state(STATE_SIP_IP
);
231 odhcp6c_clear_state(STATE_SIP_FQDN
);
232 dhcpv6_set_ia_mode(ia_na_mode
, ia_pd_mode
);
235 syslog(LOG_NOTICE
, "(re)starting transaction on %s", ifname
);
237 signal_usr1
= signal_usr2
= false;
238 int mode
= dhcpv6_request(DHCPV6_MSG_SOLICIT
);
239 odhcp6c_signal_process();
245 int res
= dhcpv6_request(mode
== DHCPV6_STATELESS
?
246 DHCPV6_MSG_INFO_REQ
: DHCPV6_MSG_REQUEST
);
247 bool signalled
= odhcp6c_signal_process();
251 else if (signalled
) {
256 mode
= dhcpv6_promote_server_cand();
257 } while (mode
> DHCPV6_UNKNOWN
);
263 case DHCPV6_STATELESS
:
265 syslog(LOG_NOTICE
, "entering stateless-mode on %s", ifname
);
267 while (!signal_usr2
&& !signal_term
) {
269 script_call("informed");
271 int res
= dhcpv6_poll_reconfigure();
272 odhcp6c_signal_process();
278 signal_usr1
= false; // Acknowledged
281 if (signal_usr2
|| signal_term
)
284 res
= dhcpv6_request(DHCPV6_MSG_INFO_REQ
);
285 odhcp6c_signal_process();
293 case DHCPV6_STATEFUL
:
294 script_call("bound");
296 syslog(LOG_NOTICE
, "entering stateful-mode on %s", ifname
);
298 if (bfd_interval
> 0)
299 bfd_start(ifname
, bfd_loss
, bfd_interval
);
302 while (!signal_usr2
&& !signal_term
) {
304 // Wait for T1 to expire or until we get a reconfigure
305 int res
= dhcpv6_poll_reconfigure();
306 odhcp6c_signal_process();
308 script_call("updated");
312 // Handle signal, if necessary
314 signal_usr1
= false; // Acknowledged
315 if (signal_usr2
|| signal_term
)
316 break; // Other signal type
318 // Send renew as T1 expired
319 size_t ia_pd_len
, ia_na_len
;
320 odhcp6c_get_state(STATE_IA_PD
, &ia_pd_len
);
321 odhcp6c_get_state(STATE_IA_NA
, &ia_na_len
);
323 // If we have any IAs, send renew, otherwise request
324 if (ia_pd_len
== 0 && ia_na_len
== 0)
325 res
= dhcpv6_request(DHCPV6_MSG_REQUEST
);
327 res
= dhcpv6_request(DHCPV6_MSG_RENEW
);
329 odhcp6c_signal_process();
330 if (res
> 0) { // Renew was succesfull
332 script_call("updated");
333 continue; // Renew was successful
336 odhcp6c_clear_state(STATE_SERVER_ID
); // Remove binding
338 // If we have IAs, try rebind otherwise restart
339 res
= dhcpv6_request(DHCPV6_MSG_REBIND
);
340 odhcp6c_signal_process();
343 script_call("rebound");
357 size_t ia_pd_len
, ia_na_len
, server_id_len
;
358 odhcp6c_get_state(STATE_IA_PD
, &ia_pd_len
);
359 odhcp6c_get_state(STATE_IA_NA
, &ia_na_len
);
360 odhcp6c_get_state(STATE_SERVER_ID
, &server_id_len
);
362 // Add all prefixes to lost prefixes
364 script_call("unbound");
366 if (server_id_len
> 0 && (ia_pd_len
> 0 || ia_na_len
> 0) && release
)
367 dhcpv6_request(DHCPV6_MSG_RELEASE
);
369 odhcp6c_clear_state(STATE_IA_NA
);
370 odhcp6c_clear_state(STATE_IA_PD
);
373 script_call("stopped");
378 static int usage(void)
381 "Usage: odhcp6c [options] <interface>\n"
382 "\nFeature options:\n"
383 " -S <time> Wait at least <time> sec for a DHCP-server (0)\n"
384 " -N <mode> Mode for requesting addresses [try|force|none]\n"
385 " -P <length> Request IPv6-Prefix (0 = auto)\n"
386 " -F Force IPv6-Prefix\n"
388 " -B <interval> Enable BFD ping check\n"
390 " -c <clientid> Override client-ID (base-16 encoded)\n"
391 " -i <iface-id> Use a custom interface identifier for RA handling\n"
392 " -r <options> Options to be requested (comma-separated)\n"
393 " -s <script> Status update script (/usr/sbin/odhcp6c-update)\n"
394 " -k Don't send a RELEASE when stopping\n"
395 " -t <seconds> Maximum timeout for DHCPv6-SOLICIT (120)\n"
396 "\nInvocation options:\n"
397 " -p <pidfile> Set pidfile (/var/run/6relayd.pid)\n"
399 " -e Write logmessages to stderr\n"
400 //" -v Increase logging verbosity\n"
401 " -h Show this help\n\n";
402 write(STDERR_FILENO
, buf
, sizeof(buf
));
407 // Don't want to pull-in librt and libpthread just for a monotonic clock...
408 uint64_t odhcp6c_get_milli_time(void)
410 struct timespec t
= {0, 0};
411 syscall(SYS_clock_gettime
, CLOCK_MONOTONIC
, &t
);
412 return t
.tv_sec
* 1000 + t
.tv_nsec
/ 1000000;
416 static uint8_t* odhcp6c_resize_state(enum odhcp6c_state state
, ssize_t len
)
419 return state_data
[state
] + state_len
[state
];
420 else if (state_len
[state
] + len
> 1024)
423 uint8_t *n
= realloc(state_data
[state
], state_len
[state
] + len
);
424 if (n
|| state_len
[state
] + len
== 0) {
425 state_data
[state
] = n
;
426 n
+= state_len
[state
];
427 state_len
[state
] += len
;
433 bool odhcp6c_signal_process(void)
438 bool ra_updated
= ra_process();
443 if (ra_updated
&& (bound
|| allow_slaac_only
== 0))
444 script_call("ra-updated"); // Immediate process urgent events
445 else if (ra_updated
&& !bound
&& allow_slaac_only
> 0)
446 script_delay_call("ra-updated", allow_slaac_only
);
453 return signal_usr1
|| signal_usr2
|| signal_term
;
457 void odhcp6c_clear_state(enum odhcp6c_state state
)
459 state_len
[state
] = 0;
463 void odhcp6c_add_state(enum odhcp6c_state state
, const void *data
, size_t len
)
465 uint8_t *n
= odhcp6c_resize_state(state
, len
);
467 memcpy(n
, data
, len
);
470 void odhcp6c_insert_state(enum odhcp6c_state state
, size_t offset
, const void *data
, size_t len
)
472 ssize_t len_after
= state_len
[state
] - offset
;
476 uint8_t *n
= odhcp6c_resize_state(state
, len
);
478 uint8_t *sdata
= state_data
[state
];
480 memmove(sdata
+ offset
+ len
, sdata
+ offset
, len_after
);
481 memcpy(sdata
+ offset
, data
, len
);
485 size_t odhcp6c_remove_state(enum odhcp6c_state state
, size_t offset
, size_t len
)
487 uint8_t *data
= state_data
[state
];
488 ssize_t len_after
= state_len
[state
] - (offset
+ len
);
490 return state_len
[state
];
492 memmove(data
+ offset
, data
+ offset
+ len
, len_after
);
493 return state_len
[state
] -= len
;
497 void* odhcp6c_move_state(enum odhcp6c_state state
, size_t *len
)
499 *len
= state_len
[state
];
500 void *data
= state_data
[state
];
502 state_len
[state
] = 0;
503 state_data
[state
] = NULL
;
509 void* odhcp6c_get_state(enum odhcp6c_state state
, size_t *len
)
511 *len
= state_len
[state
];
512 return state_data
[state
];
516 struct odhcp6c_entry
* odhcp6c_find_entry(enum odhcp6c_state state
, const struct odhcp6c_entry
*new)
518 size_t len
, cmplen
= offsetof(struct odhcp6c_entry
, target
) + new->length
/ 8;
519 struct odhcp6c_entry
*start
= odhcp6c_get_state(state
, &len
);
520 struct odhcp6c_entry
*x
= NULL
;
522 for (struct odhcp6c_entry
*c
= start
; !x
&& c
< &start
[len
/sizeof(*c
)]; ++c
)
523 if (!memcmp(c
, new, cmplen
))
530 bool odhcp6c_update_entry_safe(enum odhcp6c_state state
, struct odhcp6c_entry
*new, uint32_t safe
)
533 struct odhcp6c_entry
*x
= odhcp6c_find_entry(state
, new);
534 struct odhcp6c_entry
*start
= odhcp6c_get_state(state
, &len
);
536 if (x
&& x
->valid
> new->valid
&& new->valid
< safe
)
539 if (new->valid
> 0) {
541 if (new->valid
>= x
->valid
&& new->valid
- x
->valid
< 60 &&
542 new->preferred
>= x
->preferred
&&
543 new->preferred
- x
->preferred
< 60 &&
544 x
->class == new->class)
546 x
->valid
= new->valid
;
547 x
->preferred
= new->preferred
;
550 x
->class = new->class;
552 odhcp6c_add_state(state
, new, sizeof(*new));
555 odhcp6c_remove_state(state
, (x
- start
) * sizeof(*x
), sizeof(*x
));
561 bool odhcp6c_update_entry(enum odhcp6c_state state
, struct odhcp6c_entry
*new)
563 return odhcp6c_update_entry_safe(state
, new, 0);
567 static void odhcp6c_expire_list(enum odhcp6c_state state
, uint32_t elapsed
)
570 struct odhcp6c_entry
*start
= odhcp6c_get_state(state
, &len
);
571 for (struct odhcp6c_entry
*c
= start
; c
< &start
[len
/ sizeof(*c
)]; ++c
) {
574 else if (c
->t1
!= UINT32_MAX
)
579 else if (c
->t2
!= UINT32_MAX
)
582 if (c
->preferred
< elapsed
)
584 else if (c
->preferred
!= UINT32_MAX
)
585 c
->preferred
-= elapsed
;
587 if (c
->valid
< elapsed
)
589 else if (c
->valid
!= UINT32_MAX
)
593 odhcp6c_remove_state(state
, (c
- start
) * sizeof(*c
), sizeof(*c
));
598 void odhcp6c_expire(void)
600 time_t now
= odhcp6c_get_milli_time() / 1000;
601 uint32_t elapsed
= (last_update
> 0) ? now
- last_update
: 0;
604 odhcp6c_expire_list(STATE_RA_PREFIX
, elapsed
);
605 odhcp6c_expire_list(STATE_RA_ROUTE
, elapsed
);
606 odhcp6c_expire_list(STATE_RA_DNS
, elapsed
);
607 odhcp6c_expire_list(STATE_IA_NA
, elapsed
);
608 odhcp6c_expire_list(STATE_IA_PD
, elapsed
);
612 uint32_t odhcp6c_elapsed(void)
614 return odhcp6c_get_milli_time() / 1000 - last_update
;
618 void odhcp6c_random(void *buf
, size_t len
)
620 read(urandom_fd
, buf
, len
);
623 bool odhcp6c_is_bound(void)
628 static void sighandler(int signal
)
630 if (signal
== SIGCHLD
)
631 while (waitpid(-1, NULL
, WNOHANG
) > 0);
632 else if (signal
== SIGUSR1
)
634 else if (signal
== SIGUSR2
)
636 else if (signal
== SIGIO
)