#include <time.h>
#include <errno.h>
+#include <ctype.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <syslog.h>
#include <signal.h>
#include <string.h>
+#include <strings.h>
#include <stdbool.h>
#include <net/if.h>
#include <sys/syscall.h>
#include <arpa/inet.h>
+#include <linux/if_addr.h>
#include "odhcp6c.h"
#include "ra.h"
+#ifndef IN6_IS_ADDR_UNIQUELOCAL
+#define IN6_IS_ADDR_UNIQUELOCAL(a) \
+ ((((__const uint32_t *) (a))[0] & htonl (0xfe000000)) \
+ == htonl (0xfc000000))
+#endif
static void sighandler(int signal);
static int usage(void);
static int urandom_fd = -1, allow_slaac_only = 0;
static bool bound = false, release = true, ra = false;
static time_t last_update = 0;
+static char *ifname = NULL;
static unsigned int min_update_interval = DEFAULT_MIN_UPDATE_INTERVAL;
static unsigned int script_sync_delay = 10;
int logopt = LOG_PID;
int c;
unsigned int client_options = DHCPV6_CLIENT_FQDN | DHCPV6_ACCEPT_RECONFIGURE;
+ unsigned int ra_options = RA_RDNSS_DEFAULT_LIFETIME;
- while ((c = getopt(argc, argv, "S::N:V:P:FB:c:i:r:Ru:s:kt:m:hedp:fav")) != -1) {
+ while ((c = getopt(argc, argv, "S::N:V:P:FB:c:i:r:Ru:s:kt:m:Lhedp:fav")) != -1) {
switch (c) {
case 'S':
allow_slaac_only = (optarg) ? atoi(optarg) : -1;
min_update_interval = atoi(optarg);
break;
+ case 'L':
+ ra_options &= ~RA_RDNSS_DEFAULT_LIFETIME;
+ break;
+
case 'e':
logopt |= LOG_PERROR;
break;
if (!verbosity)
setlogmask(LOG_UPTO(LOG_WARNING));
- const char *ifname = argv[optind];
+ ifname = argv[optind];
if (help || !ifname)
return usage();
if ((urandom_fd = open("/dev/urandom", O_CLOEXEC | O_RDONLY)) < 0 ||
init_dhcpv6(ifname, client_options, sol_timeout) ||
- ra_init(ifname, &ifid) || script_init(script, ifname)) {
+ ra_init(ifname, &ifid, ra_options) ||
+ script_init(script, ifname)) {
syslog(LOG_ERR, "failed to initialize: %s", strerror(errno));
return 3;
}
" -k Don't send a RELEASE when stopping\n"
" -t <seconds> Maximum timeout for DHCPv6-SOLICIT (120)\n"
" -m <seconds> Minimum time between accepting updates (30)\n"
+ " -L Ignore default lifetime for RDNSS records\n"
"\nInvocation options:\n"
" -p <pidfile> Set pidfile (/var/run/odhcp6c.pid)\n"
" -d Daemonize\n"
uint8_t *start = odhcp6c_get_state(state, &len);
for (struct odhcp6c_entry *c = (struct odhcp6c_entry*)start;
- (uint8_t*)c < &start[len] && &c->auxtarget[c->auxlen] <= &start[len];
- c = (struct odhcp6c_entry*)(&c->auxtarget[c->auxlen]))
+ (uint8_t*)c < &start[len] &&
+ (uint8_t*)odhcp6c_next_entry(c) <= &start[len];
+ c = odhcp6c_next_entry(c))
if (!memcmp(c, new, cmplen) && !memcmp(c->auxtarget, new->auxtarget, new->auxlen))
return c;
x->t2 = new->t2;
x->iaid = new->iaid;
} else {
- odhcp6c_add_state(state, new, sizeof(*new) + new->auxlen);
+ odhcp6c_add_state(state, new, odhcp6c_entry_size(new));
}
} else if (x) {
- odhcp6c_remove_state(state, ((uint8_t*)x) - start, sizeof(*x) + x->auxlen);
+ odhcp6c_remove_state(state, ((uint8_t*)x) - start, odhcp6c_entry_size(x));
}
return true;
}
size_t len;
uint8_t *start = odhcp6c_get_state(state, &len);
for (struct odhcp6c_entry *c = (struct odhcp6c_entry*)start;
- (uint8_t*)c < &start[len] && &c->auxtarget[c->auxlen] <= &start[len];
- c = (struct odhcp6c_entry*)(&c->auxtarget[c->auxlen])) {
+ (uint8_t*)c < &start[len] &&
+ (uint8_t*)odhcp6c_next_entry(c) <= &start[len];
+ ) {
if (c->t1 < elapsed)
c->t1 = 0;
else if (c->t1 != UINT32_MAX)
else if (c->valid != UINT32_MAX)
c->valid -= elapsed;
- if (!c->valid)
- odhcp6c_remove_state(state, ((uint8_t*)c) - start, sizeof(*c) + c->auxlen);
+ if (!c->valid) {
+ odhcp6c_remove_state(state, ((uint8_t*)c) - start, odhcp6c_entry_size(c));
+ start = odhcp6c_get_state(state, &len);
+ } else {
+ c = odhcp6c_next_entry(c);
+ }
}
}
return bound;
}
+bool odhcp6c_addr_in_scope(const struct in6_addr *addr)
+{
+ FILE *fd = fopen("/proc/net/if_inet6", "r");
+ int len;
+ char buf[256];
+
+ if (fd == NULL)
+ return false;
+
+ while (fgets(buf, sizeof(buf), fd)) {
+ struct in6_addr inet6_addr;
+ uint32_t flags, dummy;
+ unsigned int i;
+ char name[8], addr_buf[32];
+
+ len = strlen(buf);
+
+ if ((len <= 0) || buf[len - 1] != '\n')
+ return false;
+
+ buf[--len] = '\0';
+
+ if (sscanf(buf, "%s %x %x %x %x %s",
+ addr_buf, &dummy, &dummy, &dummy, &flags, name) != 6)
+ return false;
+
+ if (strcmp(name, ifname) ||
+ (flags & (IFA_F_DADFAILED | IFA_F_TENTATIVE | IFA_F_DEPRECATED)))
+ continue;
+
+ for (i = 0; i < sizeof(addr_buf); i++) {
+ if (!isxdigit(addr_buf[i]) || isupper(addr_buf[i]))
+ return false;
+ }
+
+ memset(&inet6_addr, 0, sizeof(inet6_addr));
+ for (i = 0; i < (sizeof(addr_buf) / 2); i++) {
+ unsigned char byte;
+ static const char hex[] = "0123456789abcdef";
+ byte = ((index(hex, addr_buf[i * 2]) - hex) << 4) |
+ (index(hex, addr_buf[i * 2 + 1]) - hex);
+ inet6_addr.s6_addr[i] = byte;
+ }
+
+ if ((IN6_IS_ADDR_LINKLOCAL(&inet6_addr) == IN6_IS_ADDR_LINKLOCAL(addr)) &&
+ (IN6_IS_ADDR_UNIQUELOCAL(&inet6_addr) == IN6_IS_ADDR_UNIQUELOCAL(addr)))
+ return true;
+ }
+ return false;
+}
+
static void sighandler(int signal)
{
if (signal == SIGUSR1)