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_NTP_IP
);
230 odhcp6c_clear_state(STATE_NTP_FQDN
);
231 odhcp6c_clear_state(STATE_SIP_IP
);
232 odhcp6c_clear_state(STATE_SIP_FQDN
);
233 dhcpv6_set_ia_mode(ia_na_mode
, ia_pd_mode
);
236 syslog(LOG_NOTICE
, "(re)starting transaction on %s", ifname
);
238 signal_usr1
= signal_usr2
= false;
239 int mode
= dhcpv6_request(DHCPV6_MSG_SOLICIT
);
240 odhcp6c_signal_process();
246 int res
= dhcpv6_request(mode
== DHCPV6_STATELESS
?
247 DHCPV6_MSG_INFO_REQ
: DHCPV6_MSG_REQUEST
);
248 bool signalled
= odhcp6c_signal_process();
252 else if (signalled
) {
257 mode
= dhcpv6_promote_server_cand();
258 } while (mode
> DHCPV6_UNKNOWN
);
264 case DHCPV6_STATELESS
:
266 syslog(LOG_NOTICE
, "entering stateless-mode on %s", ifname
);
268 while (!signal_usr2
&& !signal_term
) {
270 script_call("informed");
272 int res
= dhcpv6_poll_reconfigure();
273 odhcp6c_signal_process();
279 signal_usr1
= false; // Acknowledged
282 if (signal_usr2
|| signal_term
)
285 res
= dhcpv6_request(DHCPV6_MSG_INFO_REQ
);
286 odhcp6c_signal_process();
294 case DHCPV6_STATEFUL
:
295 script_call("bound");
297 syslog(LOG_NOTICE
, "entering stateful-mode on %s", ifname
);
299 if (bfd_interval
> 0)
300 bfd_start(ifname
, bfd_loss
, bfd_interval
);
303 while (!signal_usr2
&& !signal_term
) {
305 // Wait for T1 to expire or until we get a reconfigure
306 int res
= dhcpv6_poll_reconfigure();
307 odhcp6c_signal_process();
309 script_call("updated");
313 // Handle signal, if necessary
315 signal_usr1
= false; // Acknowledged
316 if (signal_usr2
|| signal_term
)
317 break; // Other signal type
319 // Send renew as T1 expired
320 size_t ia_pd_len
, ia_na_len
;
321 odhcp6c_get_state(STATE_IA_PD
, &ia_pd_len
);
322 odhcp6c_get_state(STATE_IA_NA
, &ia_na_len
);
324 // If we have any IAs, send renew, otherwise request
325 if (ia_pd_len
== 0 && ia_na_len
== 0)
326 res
= dhcpv6_request(DHCPV6_MSG_REQUEST
);
328 res
= dhcpv6_request(DHCPV6_MSG_RENEW
);
330 odhcp6c_signal_process();
331 if (res
> 0) { // Renew was succesfull
333 script_call("updated");
334 continue; // Renew was successful
337 odhcp6c_clear_state(STATE_SERVER_ID
); // Remove binding
339 // If we have IAs, try rebind otherwise restart
340 res
= dhcpv6_request(DHCPV6_MSG_REBIND
);
341 odhcp6c_signal_process();
344 script_call("rebound");
358 size_t ia_pd_len
, ia_na_len
, server_id_len
;
359 odhcp6c_get_state(STATE_IA_PD
, &ia_pd_len
);
360 odhcp6c_get_state(STATE_IA_NA
, &ia_na_len
);
361 odhcp6c_get_state(STATE_SERVER_ID
, &server_id_len
);
363 // Add all prefixes to lost prefixes
365 script_call("unbound");
367 if (server_id_len
> 0 && (ia_pd_len
> 0 || ia_na_len
> 0) && release
)
368 dhcpv6_request(DHCPV6_MSG_RELEASE
);
370 odhcp6c_clear_state(STATE_IA_NA
);
371 odhcp6c_clear_state(STATE_IA_PD
);
374 script_call("stopped");
379 static int usage(void)
382 "Usage: odhcp6c [options] <interface>\n"
383 "\nFeature options:\n"
384 " -S <time> Wait at least <time> sec for a DHCP-server (0)\n"
385 " -N <mode> Mode for requesting addresses [try|force|none]\n"
386 " -P <length> Request IPv6-Prefix (0 = auto)\n"
387 " -F Force IPv6-Prefix\n"
389 " -B <interval> Enable BFD ping check\n"
391 " -c <clientid> Override client-ID (base-16 encoded)\n"
392 " -i <iface-id> Use a custom interface identifier for RA handling\n"
393 " -r <options> Options to be requested (comma-separated)\n"
394 " -s <script> Status update script (/usr/sbin/odhcp6c-update)\n"
395 " -k Don't send a RELEASE when stopping\n"
396 " -t <seconds> Maximum timeout for DHCPv6-SOLICIT (120)\n"
397 "\nInvocation options:\n"
398 " -p <pidfile> Set pidfile (/var/run/6relayd.pid)\n"
400 " -e Write logmessages to stderr\n"
401 //" -v Increase logging verbosity\n"
402 " -h Show this help\n\n";
403 write(STDERR_FILENO
, buf
, sizeof(buf
));
408 // Don't want to pull-in librt and libpthread just for a monotonic clock...
409 uint64_t odhcp6c_get_milli_time(void)
411 struct timespec t
= {0, 0};
412 syscall(SYS_clock_gettime
, CLOCK_MONOTONIC
, &t
);
413 return t
.tv_sec
* 1000 + t
.tv_nsec
/ 1000000;
417 static uint8_t* odhcp6c_resize_state(enum odhcp6c_state state
, ssize_t len
)
420 return state_data
[state
] + state_len
[state
];
421 else if (state_len
[state
] + len
> 1024)
424 uint8_t *n
= realloc(state_data
[state
], state_len
[state
] + len
);
425 if (n
|| state_len
[state
] + len
== 0) {
426 state_data
[state
] = n
;
427 n
+= state_len
[state
];
428 state_len
[state
] += len
;
434 bool odhcp6c_signal_process(void)
439 bool ra_updated
= ra_process();
444 if (ra_updated
&& (bound
|| allow_slaac_only
== 0))
445 script_call("ra-updated"); // Immediate process urgent events
446 else if (ra_updated
&& !bound
&& allow_slaac_only
> 0)
447 script_delay_call("ra-updated", allow_slaac_only
);
454 return signal_usr1
|| signal_usr2
|| signal_term
;
458 void odhcp6c_clear_state(enum odhcp6c_state state
)
460 state_len
[state
] = 0;
464 void odhcp6c_add_state(enum odhcp6c_state state
, const void *data
, size_t len
)
466 uint8_t *n
= odhcp6c_resize_state(state
, len
);
468 memcpy(n
, data
, len
);
471 void odhcp6c_insert_state(enum odhcp6c_state state
, size_t offset
, const void *data
, size_t len
)
473 ssize_t len_after
= state_len
[state
] - offset
;
477 uint8_t *n
= odhcp6c_resize_state(state
, len
);
479 uint8_t *sdata
= state_data
[state
];
481 memmove(sdata
+ offset
+ len
, sdata
+ offset
, len_after
);
482 memcpy(sdata
+ offset
, data
, len
);
486 size_t odhcp6c_remove_state(enum odhcp6c_state state
, size_t offset
, size_t len
)
488 uint8_t *data
= state_data
[state
];
489 ssize_t len_after
= state_len
[state
] - (offset
+ len
);
491 return state_len
[state
];
493 memmove(data
+ offset
, data
+ offset
+ len
, len_after
);
494 return state_len
[state
] -= len
;
498 void* odhcp6c_move_state(enum odhcp6c_state state
, size_t *len
)
500 *len
= state_len
[state
];
501 void *data
= state_data
[state
];
503 state_len
[state
] = 0;
504 state_data
[state
] = NULL
;
510 void* odhcp6c_get_state(enum odhcp6c_state state
, size_t *len
)
512 *len
= state_len
[state
];
513 return state_data
[state
];
517 struct odhcp6c_entry
* odhcp6c_find_entry(enum odhcp6c_state state
, const struct odhcp6c_entry
*new)
519 size_t len
, cmplen
= offsetof(struct odhcp6c_entry
, target
) + new->length
/ 8;
520 struct odhcp6c_entry
*start
= odhcp6c_get_state(state
, &len
);
521 struct odhcp6c_entry
*x
= NULL
;
523 for (struct odhcp6c_entry
*c
= start
; !x
&& c
< &start
[len
/sizeof(*c
)]; ++c
)
524 if (!memcmp(c
, new, cmplen
))
531 bool odhcp6c_update_entry_safe(enum odhcp6c_state state
, struct odhcp6c_entry
*new, uint32_t safe
)
534 struct odhcp6c_entry
*x
= odhcp6c_find_entry(state
, new);
535 struct odhcp6c_entry
*start
= odhcp6c_get_state(state
, &len
);
537 if (x
&& x
->valid
> new->valid
&& new->valid
< safe
)
540 if (new->valid
> 0) {
542 if (new->valid
>= x
->valid
&& new->valid
- x
->valid
< 60 &&
543 new->preferred
>= x
->preferred
&&
544 new->preferred
- x
->preferred
< 60 &&
545 x
->class == new->class)
547 x
->valid
= new->valid
;
548 x
->preferred
= new->preferred
;
551 x
->class = new->class;
553 odhcp6c_add_state(state
, new, sizeof(*new));
556 odhcp6c_remove_state(state
, (x
- start
) * sizeof(*x
), sizeof(*x
));
562 bool odhcp6c_update_entry(enum odhcp6c_state state
, struct odhcp6c_entry
*new)
564 return odhcp6c_update_entry_safe(state
, new, 0);
568 static void odhcp6c_expire_list(enum odhcp6c_state state
, uint32_t elapsed
)
571 struct odhcp6c_entry
*start
= odhcp6c_get_state(state
, &len
);
572 for (struct odhcp6c_entry
*c
= start
; c
< &start
[len
/ sizeof(*c
)]; ++c
) {
575 else if (c
->t1
!= UINT32_MAX
)
580 else if (c
->t2
!= UINT32_MAX
)
583 if (c
->preferred
< elapsed
)
585 else if (c
->preferred
!= UINT32_MAX
)
586 c
->preferred
-= elapsed
;
588 if (c
->valid
< elapsed
)
590 else if (c
->valid
!= UINT32_MAX
)
594 odhcp6c_remove_state(state
, (c
- start
) * sizeof(*c
), sizeof(*c
));
599 void odhcp6c_expire(void)
601 time_t now
= odhcp6c_get_milli_time() / 1000;
602 uint32_t elapsed
= (last_update
> 0) ? now
- last_update
: 0;
605 odhcp6c_expire_list(STATE_RA_PREFIX
, elapsed
);
606 odhcp6c_expire_list(STATE_RA_ROUTE
, elapsed
);
607 odhcp6c_expire_list(STATE_RA_DNS
, elapsed
);
608 odhcp6c_expire_list(STATE_IA_NA
, elapsed
);
609 odhcp6c_expire_list(STATE_IA_PD
, elapsed
);
613 uint32_t odhcp6c_elapsed(void)
615 return odhcp6c_get_milli_time() / 1000 - last_update
;
619 void odhcp6c_random(void *buf
, size_t len
)
621 read(urandom_fd
, buf
, len
);
624 bool odhcp6c_is_bound(void)
629 static void sighandler(int signal
)
631 if (signal
== SIGCHLD
)
632 while (waitpid(-1, NULL
, WNOHANG
) > 0);
633 else if (signal
== SIGUSR1
)
635 else if (signal
== SIGUSR2
)
637 else if (signal
== SIGIO
)