2 * Copyright (C) 2012 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>
35 static void sighandler(int signal
);
36 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;
44 static bool bound
= false, allow_slaac_only
= false;
47 int main(_unused
int argc
, char* const argv
[])
49 openlog("odhcp6c", LOG_PERROR
| LOG_PID
, LOG_DAEMON
);
51 // Allocate ressources
52 const char *pidfile
= NULL
;
53 const char *script
= "/usr/sbin/odhcp6c-update";
58 enum odhcp6c_ia_mode ia_na_mode
= IA_MODE_TRY
;
60 bool help
= false, daemonize
= false;
61 int c
, request_pd
= 0;
62 while ((c
= getopt(argc
, argv
, "SN:P:c:r:s:hdp:")) != -1) {
65 allow_slaac_only
= true;
69 if (!strcmp(optarg
, "force"))
70 ia_na_mode
= IA_MODE_FORCE
;
71 else if (!strcmp(optarg
, "none"))
72 ia_na_mode
= IA_MODE_NONE
;
73 else if (!strcmp(optarg
, "try"))
74 ia_na_mode
= IA_MODE_TRY
;
80 request_pd
= strtoul(optarg
, NULL
, 10);
86 l
= script_unhexlify(&buf
[4], sizeof(buf
) - 4, optarg
);
89 buf
[1] = DHCPV6_OPT_CLIENTID
;
92 odhcp6c_add_state(STATE_CLIENT_ID
, buf
, l
+ 4);
101 opttype
= htons(strtoul(optarg
, &optpos
, 10));
102 if (optpos
== optarg
)
106 odhcp6c_add_state(STATE_ORO
, &opttype
, 2);
128 const char *ifname
= argv
[optind
];
133 if ((urandom_fd
= open("/dev/urandom", O_CLOEXEC
| O_RDONLY
)) < 0 ||
134 init_dhcpv6(ifname
, request_pd
) || ra_init(ifname
) ||
135 script_init(script
, ifname
)) {
136 syslog(LOG_ERR
, "failed to initialize: %s", strerror(errno
));
140 signal(SIGIO
, sighandler
);
141 signal(SIGHUP
, sighandler
);
142 signal(SIGINT
, sighandler
);
143 signal(SIGCHLD
, sighandler
);
144 signal(SIGTERM
, sighandler
);
145 signal(SIGUSR1
, sighandler
);
146 signal(SIGUSR2
, sighandler
);
149 openlog("odhcp6c", LOG_PID
, LOG_DAEMON
); // Disable LOG_PERROR
151 syslog(LOG_ERR
, "Failed to daemonize: %s",
158 snprintf(pidbuf
, sizeof(pidbuf
),
159 "/var/run/odhcp6c.%s.pid", ifname
);
163 int fd
= open(pidfile
, O_WRONLY
| O_CREAT
);
166 int len
= snprintf(buf
, sizeof(buf
), "%i\n", getpid());
172 script_call("started");
174 while (do_signal
!= SIGTERM
) { // Main logic
175 odhcp6c_clear_state(STATE_SERVER_ID
);
176 odhcp6c_clear_state(STATE_SERVER_CAND
);
177 odhcp6c_clear_state(STATE_IA_PD
);
178 odhcp6c_clear_state(STATE_SNTP_IP
);
179 odhcp6c_clear_state(STATE_SNTP_FQDN
);
180 odhcp6c_clear_state(STATE_SIP_IP
);
181 odhcp6c_clear_state(STATE_SIP_FQDN
);
182 dhcpv6_set_ia_na_mode(ia_na_mode
);
186 int res
= dhcpv6_request(DHCPV6_MSG_SOLICIT
);
187 odhcp6c_signal_process();
190 continue; // Might happen if we got a signal
191 } else if (res
== DHCPV6_STATELESS
) { // Stateless mode
192 while (do_signal
== 0 || do_signal
== SIGUSR1
) {
195 res
= dhcpv6_request(DHCPV6_MSG_INFO_REQ
);
196 odhcp6c_signal_process();
197 if (do_signal
== SIGUSR1
)
202 script_call("informed");
206 if (dhcpv6_poll_reconfigure() > 0)
207 script_call("informed");
214 if (dhcpv6_request(DHCPV6_MSG_REQUEST
) < 0)
217 odhcp6c_signal_process();
218 script_call("bound");
221 while (do_signal
== 0 || do_signal
== SIGUSR1
) {
223 // Wait for T1 to expire or until we get a reconfigure
224 int res
= dhcpv6_poll_reconfigure();
225 odhcp6c_signal_process();
228 script_call("updated");
233 // Handle signal, if necessary
234 if (do_signal
== SIGUSR1
)
235 do_signal
= 0; // Acknowledged
236 else if (do_signal
> 0)
237 break; // Other signal type
239 size_t ia_pd_len
, ia_na_len
, ia_pd_new
, ia_na_new
;
240 odhcp6c_get_state(STATE_IA_PD
, &ia_pd_len
);
241 odhcp6c_get_state(STATE_IA_NA
, &ia_na_len
);
243 // If we have any IAs, send renew, otherwise request
245 if (ia_pd_len
== 0 && ia_na_len
== 0)
246 r
= dhcpv6_request(DHCPV6_MSG_REQUEST
);
248 r
= dhcpv6_request(DHCPV6_MSG_RENEW
);
249 odhcp6c_signal_process();
250 if (r
> 0) // Publish updates
251 script_call("updated");
253 continue; // Renew was successful
255 odhcp6c_clear_state(STATE_SERVER_ID
); // Remove binding
257 // If we have IAs, try rebind otherwise restart
258 res
= dhcpv6_request(DHCPV6_MSG_REBIND
);
259 odhcp6c_signal_process();
261 odhcp6c_get_state(STATE_IA_PD
, &ia_pd_new
);
262 odhcp6c_get_state(STATE_IA_NA
, &ia_na_new
);
263 if (res
< 0 || (ia_pd_new
== 0 && ia_pd_len
) ||
264 (ia_na_new
== 0 && ia_na_len
))
265 break; // We lost all our IAs, restart
267 script_call("rebound");
271 size_t ia_pd_len
, ia_na_len
, server_id_len
;
272 odhcp6c_get_state(STATE_IA_PD
, &ia_pd_len
);
273 odhcp6c_get_state(STATE_IA_NA
, &ia_na_len
);
274 odhcp6c_get_state(STATE_SERVER_ID
, &server_id_len
);
276 // Add all prefixes to lost prefixes
278 script_call("unbound");
280 if (server_id_len
> 0 && (ia_pd_len
> 0 || ia_na_len
> 0))
281 dhcpv6_request(DHCPV6_MSG_RELEASE
);
283 odhcp6c_clear_state(STATE_IA_NA
);
284 odhcp6c_clear_state(STATE_IA_PD
);
287 script_call("stopped");
292 static int usage(void)
295 "Usage: odhcp6c [options] <interface>\n"
296 "\nFeature options:\n"
297 " -S Allow SLAAC-only assignment\n"
298 " -N <mode> Mode for requesting addresses [try|force|none]\n"
299 " -P <length> Request IPv6-Prefix (0 = auto)\n"
300 " -c <clientid> Override client-ID (base-16 encoded)\n"
301 " -r <options> Options to be requested (comma-separated)\n"
302 " -s <script> Status update script (/usr/sbin/odhcp6c-update)\n"
303 "\nInvocation options:\n"
304 " -p <pidfile> Set pidfile (/var/run/6relayd.pid)\n"
306 //" -v Increase logging verbosity\n"
307 " -h Show this help\n\n";
308 write(STDERR_FILENO
, buf
, sizeof(buf
));
313 // Don't want to pull-in librt and libpthread just for a monotonic clock...
314 uint64_t odhcp6c_get_milli_time(void)
316 struct timespec t
= {0, 0};
317 syscall(SYS_clock_gettime
, CLOCK_MONOTONIC
, &t
);
318 return t
.tv_sec
* 1000 + t
.tv_nsec
/ 1000000;
322 static uint8_t* odhcp6c_resize_state(enum odhcp6c_state state
, ssize_t len
)
325 return state_data
[state
] + state_len
[state
];
326 else if (state_len
[state
] + len
> 1024)
329 uint8_t *n
= realloc(state_data
[state
], state_len
[state
] + len
);
330 if (n
|| state_len
[state
] + len
== 0) {
331 state_data
[state
] = n
;
332 n
+= state_len
[state
];
333 state_len
[state
] += len
;
339 bool odhcp6c_signal_process(void)
341 if (do_signal
== SIGIO
) {
343 bool updated
= ra_process();
344 updated
|= ra_rtnl_process();
345 if (updated
&& (bound
|| allow_slaac_only
)) {
347 script_call("ra-updated");
351 return do_signal
!= 0;
355 void odhcp6c_clear_state(enum odhcp6c_state state
)
357 state_len
[state
] = 0;
361 void odhcp6c_add_state(enum odhcp6c_state state
, const void *data
, size_t len
)
363 uint8_t *n
= odhcp6c_resize_state(state
, len
);
365 memcpy(n
, data
, len
);
369 size_t odhcp6c_remove_state(enum odhcp6c_state state
, size_t offset
, size_t len
)
371 uint8_t *data
= state_data
[state
];
372 ssize_t len_after
= state_len
[state
] - (offset
+ len
);
374 return state_len
[state
];
376 memmove(data
+ offset
, data
+ offset
+ len
, len_after
);
377 return state_len
[state
] -= len
;
381 void* odhcp6c_get_state(enum odhcp6c_state state
, size_t *len
)
383 *len
= state_len
[state
];
384 return state_data
[state
];
388 struct odhcp6c_entry
* odhcp6c_find_entry(enum odhcp6c_state state
, const struct odhcp6c_entry
*new)
390 size_t len
, cmplen
= offsetof(struct odhcp6c_entry
, target
) + new->length
/ 8;
391 struct odhcp6c_entry
*start
= odhcp6c_get_state(state
, &len
);
392 struct odhcp6c_entry
*x
= NULL
;
394 for (struct odhcp6c_entry
*c
= start
; !x
&& c
< &start
[len
/sizeof(*c
)]; ++c
)
395 if (!memcmp(c
, new, cmplen
))
402 void odhcp6c_update_entry_safe(enum odhcp6c_state state
, struct odhcp6c_entry
*new, uint32_t safe
)
405 struct odhcp6c_entry
*x
= odhcp6c_find_entry(state
, new);
406 struct odhcp6c_entry
*start
= odhcp6c_get_state(state
, &len
);
408 if (x
&& x
->valid
> new->valid
&& new->valid
< safe
)
411 if (new->valid
> 0) {
413 x
->valid
= new->valid
;
414 x
->preferred
= new->preferred
;
416 odhcp6c_add_state(state
, new, sizeof(*new));
419 odhcp6c_remove_state(state
, (x
- start
) * sizeof(*x
), sizeof(*x
));
424 void odhcp6c_update_entry(enum odhcp6c_state state
, struct odhcp6c_entry
*new)
426 odhcp6c_update_entry_safe(state
, new, 0);
430 static void odhcp6c_expire_list(enum odhcp6c_state state
, uint32_t elapsed
)
433 struct odhcp6c_entry
*start
= odhcp6c_get_state(state
, &len
);
434 for (struct odhcp6c_entry
*c
= start
; c
< &start
[len
/ sizeof(*c
)]; ++c
) {
435 if (c
->preferred
< elapsed
)
437 else if (c
->preferred
!= UINT32_MAX
)
438 c
->preferred
-= elapsed
;
440 if (c
->valid
< elapsed
)
442 else if (c
->valid
!= UINT32_MAX
)
446 odhcp6c_remove_state(state
, (c
- start
) * sizeof(*c
), sizeof(*c
));
451 void odhcp6c_expire(void)
453 static time_t last_update
= 0;
454 time_t now
= odhcp6c_get_milli_time() / 1000;
456 uint32_t elapsed
= now
- last_update
;
459 odhcp6c_expire_list(STATE_RA_PREFIX
, elapsed
);
460 odhcp6c_expire_list(STATE_RA_ROUTE
, elapsed
);
461 odhcp6c_expire_list(STATE_RA_DNS
, elapsed
);
462 odhcp6c_expire_list(STATE_IA_NA
, elapsed
);
463 odhcp6c_expire_list(STATE_IA_PD
, elapsed
);
467 void odhcp6c_random(void *buf
, size_t len
)
469 read(urandom_fd
, buf
, len
);
473 static void sighandler(int signal
)
475 if (signal
== SIGCHLD
)
476 while (waitpid(-1, NULL
, WNOHANG
) > 0);
477 else if (signal
== SIGUSR1
)
479 else if (signal
== SIGUSR2
)
481 else if (signal
== SIGIO
)