X-Git-Url: https://git.librecmc.org/?a=blobdiff_plain;f=src%2Fodhcp6c.c;h=2fe41e5160158ed8501863e23a130a640b285034;hb=c7122ecef30af3570fe6484309db1b23325a7356;hp=1714d629b7cc38a7b3816cfaac58af5a2420baa0;hpb=23aa0c218808361c1252b73a8ba82a91ac9039e7;p=oweals%2Fodhcp6c.git diff --git a/src/odhcp6c.c b/src/odhcp6c.c index 1714d62..2fe41e5 100644 --- a/src/odhcp6c.c +++ b/src/odhcp6c.c @@ -14,6 +14,7 @@ #include #include +#include #include #include #include @@ -22,16 +23,23 @@ #include #include #include +#include #include #include #include #include +#include #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); @@ -47,6 +55,7 @@ static volatile bool signal_term = false; 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; @@ -74,8 +83,9 @@ int main(_unused int argc, char* const argv[]) 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; @@ -193,6 +203,10 @@ int main(_unused int argc, char* const argv[]) min_update_interval = atoi(optarg); break; + case 'L': + ra_options &= ~RA_RDNSS_DEFAULT_LIFETIME; + break; + case 'e': logopt |= LOG_PERROR; break; @@ -230,7 +244,7 @@ int main(_unused int argc, char* const argv[]) if (!verbosity) setlogmask(LOG_UPTO(LOG_WARNING)); - const char *ifname = argv[optind]; + ifname = argv[optind]; if (help || !ifname) return usage(); @@ -244,7 +258,8 @@ int main(_unused int argc, char* const argv[]) 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; } @@ -443,6 +458,7 @@ static int usage(void) " -k Don't send a RELEASE when stopping\n" " -t Maximum timeout for DHCPv6-SOLICIT (120)\n" " -m Minimum time between accepting updates (30)\n" + " -L Ignore default lifetime for RDNSS records\n" "\nInvocation options:\n" " -p Set pidfile (/var/run/odhcp6c.pid)\n" " -d Daemonize\n" @@ -570,8 +586,9 @@ static struct odhcp6c_entry* odhcp6c_find_entry(enum odhcp6c_state state, const 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; @@ -604,10 +621,10 @@ bool odhcp6c_update_entry(enum odhcp6c_state state, struct odhcp6c_entry *new, 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; } @@ -618,8 +635,9 @@ static void odhcp6c_expire_list(enum odhcp6c_state state, uint32_t elapsed) 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) @@ -640,8 +658,12 @@ static void odhcp6c_expire_list(enum odhcp6c_state state, uint32_t elapsed) 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); + } } } @@ -677,6 +699,57 @@ bool odhcp6c_is_bound(void) 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)