X-Git-Url: https://git.decadent.org.uk/gitweb/?p=odhcp6c.git;a=blobdiff_plain;f=src%2Fra.c;h=3e36f94692e14921cb9aa94132d032181cf654e1;hp=58c87417e2929ca91ee0cd23403462b05d44c2db;hb=9c7c654cb2d5ac6ac536f603cd5a9372416e91da;hpb=63358b8f56aa4eb9976c09bf34e9e04f37d6a2a8 diff --git a/src/ra.c b/src/ra.c index 58c8741..3e36f94 100644 --- a/src/ra.c +++ b/src/ra.c @@ -1,5 +1,5 @@ /** - * Copyright (C) 2012-2013 Steven Barth + * Copyright (C) 2012-2014 Steven Barth * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License v2 as published by @@ -13,6 +13,7 @@ */ #include +#include #include #include #include @@ -24,6 +25,7 @@ #include #include #include +#include #include #include @@ -214,7 +216,7 @@ static bool ra_icmpv6_valid(struct sockaddr_in6 *source, int hlim, uint8_t *data default: return false; } - + icmpv6_for_each_option(opt, opt, end) ; @@ -225,23 +227,36 @@ bool ra_process(void) { bool found = false; bool changed = false; + bool has_lladdr = !IN6_IS_ADDR_UNSPECIFIED(&lladdr); uint8_t buf[1500], cmsg_buf[128]; struct nd_router_advert *adv = (struct nd_router_advert*)buf; - struct odhcp6c_entry entry = {IN6ADDR_ANY_INIT, 0, 0, IN6ADDR_ANY_INIT, 0, 0, 0, 0, 0}; + struct odhcp6c_entry entry = {IN6ADDR_ANY_INIT, 0, 0, IN6ADDR_ANY_INIT, 0, 0, 0, 0, 0, 0}; const struct in6_addr any = IN6ADDR_ANY_INIT; - if (IN6_IS_ADDR_UNSPECIFIED(&lladdr)) { + if (!has_lladdr) { // Autodetect interface-id if not specified - FILE *fp = fopen("/proc/net/if_inet6", "r"); - if (fp) { - char addrbuf[33], ifbuf[16]; - while (fscanf(fp, "%32s %*x %*x %*x %*x %15s", addrbuf, ifbuf) == 2) { - if (!strcmp(ifbuf, if_name)) { - script_unhexlify((uint8_t*)&lladdr, sizeof(lladdr), addrbuf); + struct ifaddrs *ifaddr, *ifa; + + if (getifaddrs(&ifaddr) == 0) { + for (ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next) { + struct sockaddr_in6 *addr; + + if (ifa->ifa_addr == NULL || ifa->ifa_addr->sa_family != AF_INET6) + continue; + + addr = (struct sockaddr_in6*)ifa->ifa_addr; + + if (!IN6_IS_ADDR_LINKLOCAL(&addr->sin6_addr)) + continue; + + if (!strcmp(ifa->ifa_name, if_name)) { + lladdr = addr->sin6_addr; + has_lladdr = true; break; } } - fclose(fp); + + freeifaddrs(ifaddr); } } @@ -255,6 +270,9 @@ bool ra_process(void) if (len <= 0) break; + if (!has_lladdr) + continue; + int hlim = 0; for (struct cmsghdr *ch = CMSG_FIRSTHDR(&msg); ch != NULL; ch = CMSG_NXTHDR(&msg, ch)) @@ -293,11 +311,13 @@ bool ra_process(void) update_proc("conf", "hop_limit", adv->nd_ra_curhoplimit); // Parse ND parameters - if (ntohl(adv->nd_ra_reachable) <= 3600000) - update_proc("neigh", "base_reachable_time_ms", ntohl(adv->nd_ra_reachable)); + uint32_t reachable = ntohl(adv->nd_ra_reachable); + if (reachable > 0 && reachable <= 3600000) + update_proc("neigh", "base_reachable_time_ms", reachable); - if (ntohl(adv->nd_ra_retransmit) <= 60000) - update_proc("neigh", "retrans_time_ms", ntohl(adv->nd_ra_retransmit)); + uint32_t retransmit = ntohl(adv->nd_ra_retransmit); + if (retransmit > 0 && retransmit <= 60000) + update_proc("neigh", "retrans_time_ms", retransmit); // Evaluate options