X-Git-Url: http://git.archive.openwrt.org/?p=project%2Fodhcpd.git;a=blobdiff_plain;f=src%2Frouter.c;h=30f1609b2a315514b492b4e34767ab24d3d7d9ec;hp=85ca6b0ef66f31e53718ec14a6b94c2bd42f8aa3;hb=f66103ef4e100b4c3d375553cb916cb924cc40e5;hpb=5dad295c283a8ab8101d58ec3d8ead49a3a62a19 diff --git a/src/router.c b/src/router.c index 85ca6b0..30f1609 100644 --- a/src/router.c +++ b/src/router.c @@ -38,7 +38,6 @@ static void sigusr1_refresh(int signal); static struct odhcpd_event router_event = {.uloop = {.fd = -1}, .handle_dgram = handle_icmpv6, }; static FILE *fp_route = NULL; -#define RA_IOV_LEN 7 #define TIME_LEFT(t1, now) ((t1) != UINT32_MAX ? (t1) - (now) : UINT32_MAX) @@ -114,16 +113,15 @@ int setup_router_interface(struct interface *iface, bool enable) } else { void *mreq = &all_routers; - if (iface->ra == RELAYD_RELAY && iface->master) { + if (iface->ra == MODE_RELAY && iface->master) { mreq = &all_nodes; forward_router_solicitation(iface); - } else if (iface->ra == RELAYD_SERVER && !iface->master) { + } else if (iface->ra == MODE_SERVER && !iface->master) { iface->timer_rs.cb = trigger_router_advert; uloop_timeout_set(&iface->timer_rs, 1000); - ndp_rqs_addr6_dump(); } - if (iface->ra == RELAYD_RELAY || (iface->ra == RELAYD_SERVER && !iface->master)) + if (iface->ra == MODE_RELAY || (iface->ra == MODE_SERVER && !iface->master)) setsockopt(router_event.uloop.fd, IPPROTO_IPV6, IPV6_ADD_MEMBERSHIP, mreq, sizeof(all_nodes)); } @@ -136,7 +134,7 @@ static void sigusr1_refresh(_unused int signal) { struct interface *iface; list_for_each_entry(iface, &interfaces, head) - if (iface->ra == RELAYD_SERVER && !iface->master) + if (iface->ra == MODE_SERVER && !iface->master) uloop_timeout_set(&iface->timer_rs, 1000); } @@ -183,7 +181,9 @@ static bool parse_routes(struct odhcpd_ipaddr *n, ssize_t len) char line[512], ifname[16]; bool found_default = false; - struct odhcpd_ipaddr p = {IN6ADDR_ANY_INIT, 0, 0, 0, 0}; + struct odhcpd_ipaddr p = { .addr.in6 = IN6ADDR_ANY_INIT, .prefix = 0, + .dprefix = 0, .preferred = 0, .valid = 0}; + while (fgets(line, sizeof(line), fp_route)) { uint32_t rflags; if (sscanf(line, "00000000000000000000000000000000 00 " @@ -192,15 +192,15 @@ static bool parse_routes(struct odhcpd_ipaddr *n, ssize_t len) found_default = true; } else if (sscanf(line, "%8" SCNx32 "%8" SCNx32 "%*8" SCNx32 "%*8" SCNx32 " %hhx %*s " "%*s 00000000000000000000000000000000 %*s %*s %*s %" SCNx32 " lo", - &p.addr.s6_addr32[0], &p.addr.s6_addr32[1], &p.prefix, &rflags) && + &p.addr.in6.s6_addr32[0], &p.addr.in6.s6_addr32[1], &p.prefix, &rflags) && p.prefix > 0 && (rflags & RTF_NONEXTHOP) && (rflags & RTF_REJECT)) { // Find source prefixes by scanning through unreachable-routes - p.addr.s6_addr32[0] = htonl(p.addr.s6_addr32[0]); - p.addr.s6_addr32[1] = htonl(p.addr.s6_addr32[1]); + p.addr.in6.s6_addr32[0] = htonl(p.addr.in6.s6_addr32[0]); + p.addr.in6.s6_addr32[1] = htonl(p.addr.in6.s6_addr32[1]); for (ssize_t i = 0; i < len; ++i) { if (n[i].prefix <= 64 && n[i].prefix >= p.prefix && - !odhcpd_bmemcmp(&p.addr, &n[i].addr, p.prefix)) { + !odhcpd_bmemcmp(&p.addr.in6, &n[i].addr.in6, p.prefix)) { n[i].dprefix = p.prefix; break; } @@ -255,6 +255,17 @@ static uint16_t calc_ra_lifetime(struct interface *iface, uint32_t maxival) return lifetime; } +enum { + IOV_RA_ADV=0, + IOV_RA_PFXS, + IOV_RA_ROUTES, + IOV_RA_DNS, + IOV_RA_DNS_ADDR, + IOV_RA_SEARCH, + IOV_RA_ADV_INTERVAL, + IOV_RA_TOTAL, +}; + // Router Advert server mode static uint64_t send_router_advert(struct interface *iface, const struct in6_addr *from) { @@ -287,7 +298,7 @@ static uint64_t send_router_advert(struct interface *iface, const struct in6_add if (iface->dhcpv6) adv.h.nd_ra_flags_reserved = ND_RA_FLAG_OTHER; - if (iface->managed >= RELAYD_MANAGED_MFLAG) + if (iface->ra_managed >= RA_MANAGED_MFLAG) adv.h.nd_ra_flags_reserved |= ND_RA_FLAG_MANAGED; if (iface->route_preference < 0) @@ -343,7 +354,7 @@ static uint64_t send_router_advert(struct interface *iface, const struct in6_add if (addr->prefix > 96 || addr->valid <= (uint32_t)now) { char namebuf[INET6_ADDRSTRLEN]; - inet_ntop(AF_INET6, addr, namebuf, sizeof(namebuf)); + inet_ntop(AF_INET6, &addr->addr.in6, namebuf, sizeof(namebuf)); syslog(LOG_INFO, "Address %s (prefix %d, valid %u) not suitable as RA prefix on %s", namebuf, addr->prefix, addr->valid, iface->ifname); continue; @@ -353,7 +364,7 @@ static uint64_t send_router_advert(struct interface *iface, const struct in6_add for (size_t i = 0; i < pfxs_cnt; ++i) { if (addr->prefix == pfxs[i].nd_opt_pi_prefix_len && !odhcpd_bmemcmp(&pfxs[i].nd_opt_pi_prefix, - &addr->addr, addr->prefix)) + &addr->addr.in6, addr->prefix)) p = &pfxs[i]; } @@ -386,10 +397,10 @@ static uint64_t send_router_advert(struct interface *iface, const struct in6_add if (minvalid > valid) minvalid = valid; - if (!IN6_IS_ADDR_ULA(&addr->addr) || iface->default_router) + if (!IN6_IS_ADDR_ULA(&addr->addr.in6) || iface->default_router) valid_prefix = true; - odhcpd_bmemcpy(&p->nd_opt_pi_prefix, &addr->addr, + odhcpd_bmemcpy(&p->nd_opt_pi_prefix, &addr->addr.in6, (iface->ra_advrouter) ? 128 : addr->prefix); p->nd_opt_pi_type = ND_OPT_PREFIX_INFORMATION; p->nd_opt_pi_len = 4; @@ -397,7 +408,7 @@ static uint64_t send_router_advert(struct interface *iface, const struct in6_add p->nd_opt_pi_flags_reserved = 0; if (!iface->ra_not_onlink) p->nd_opt_pi_flags_reserved |= ND_OPT_PI_FLAG_ONLINK; - if (iface->managed < RELAYD_MANAGED_NO_AFLAG && addr->prefix <= 64) + if (iface->ra_managed < RA_MANAGED_NO_AFLAG && addr->prefix <= 64) p->nd_opt_pi_flags_reserved |= ND_OPT_PI_FLAG_AUTO; if (iface->ra_advrouter) p->nd_opt_pi_flags_reserved |= ND_OPT_PI_FLAG_RADDR; @@ -488,10 +499,10 @@ static uint64_t send_router_advert(struct interface *iface, const struct in6_add (addr->dprefix == 64 && addr->prefix == 64)) { continue; // Address not suitable } else if (addr->dprefix > 32) { - addr->addr.s6_addr32[1] &= htonl(~((1U << (64 - addr->dprefix)) - 1)); + addr->addr.in6.s6_addr32[1] &= htonl(~((1U << (64 - addr->dprefix)) - 1)); } else if (addr->dprefix <= 32) { - addr->addr.s6_addr32[0] &= htonl(~((1U << (32 - addr->dprefix)) - 1)); - addr->addr.s6_addr32[1] = 0; + addr->addr.in6.s6_addr32[0] &= htonl(~((1U << (32 - addr->dprefix)) - 1)); + addr->addr.in6.s6_addr32[1] = 0; } tmp = realloc(routes, sizeof(*routes) * (routes_cnt + 1)); @@ -512,8 +523,8 @@ static uint64_t send_router_advert(struct interface *iface, const struct in6_add else if (iface->route_preference > 0) routes[routes_cnt].flags |= ND_RA_PREF_HIGH; routes[routes_cnt].lifetime = htonl(TIME_LEFT(addr->valid, now)); - routes[routes_cnt].addr[0] = addr->addr.s6_addr32[0]; - routes[routes_cnt].addr[1] = addr->addr.s6_addr32[1]; + routes[routes_cnt].addr[0] = addr->addr.in6.s6_addr32[0]; + routes[routes_cnt].addr[1] = addr->addr.in6.s6_addr32[1]; routes[routes_cnt].addr[2] = 0; routes[routes_cnt].addr[3] = 0; @@ -529,14 +540,14 @@ static uint64_t send_router_advert(struct interface *iface, const struct in6_add .data = {0, 0, (maxival*1000) >> 24, (maxival*1000) >> 16, (maxival*1000) >> 8, maxival*1000} }; - struct iovec iov[RA_IOV_LEN] = { - {&adv, sizeof(adv)}, - {pfxs, pfxs_cnt * sizeof(*pfxs)}, - {routes, routes_cnt * sizeof(*routes)}, - {&dns, (dns_cnt) ? sizeof(dns) : 0}, - {dns_addr, dns_cnt * sizeof(*dns_addr)}, - {search, search->len * 8}, - {&adv_interval, adv_interval.len * 8}}; + struct iovec iov[IOV_RA_TOTAL] = { + [IOV_RA_ADV] = {&adv, sizeof(adv)}, + [IOV_RA_PFXS] = {pfxs, pfxs_cnt * sizeof(*pfxs)}, + [IOV_RA_ROUTES] = {routes, routes_cnt * sizeof(*routes)}, + [IOV_RA_DNS] = {&dns, (dns_cnt) ? sizeof(dns) : 0}, + [IOV_RA_DNS_ADDR] = {dns_addr, dns_cnt * sizeof(*dns_addr)}, + [IOV_RA_SEARCH] = {search, search->len * 8}, + [IOV_RA_ADV_INTERVAL] = {&adv_interval, adv_interval.len * 8}}; struct sockaddr_in6 dest = {AF_INET6, 0, 0, ALL_IPV6_NODES, 0}; if (from && !IN6_IS_ADDR_UNSPECIFIED(from)) @@ -573,10 +584,10 @@ static void handle_icmpv6(void *addr, void *data, size_t len, if (!router_icmpv6_valid(addr, data, len)) return; - if ((iface->ra == RELAYD_SERVER && !iface->master)) { // Server mode + if ((iface->ra == MODE_SERVER && !iface->master)) { // Server mode if (hdr->icmp6_type == ND_ROUTER_SOLICIT) send_router_advert(iface, &from->sin6_addr); - } else if (iface->ra == RELAYD_RELAY) { // Relay mode + } else if (iface->ra == MODE_RELAY) { // Relay mode if (hdr->icmp6_type == ND_ROUTER_ADVERT && iface->master) forward_router_advertisement(data, len); else if (hdr->icmp6_type == ND_ROUTER_SOLICIT && !iface->master) @@ -635,7 +646,7 @@ static void forward_router_advertisement(uint8_t *data, size_t len) struct interface *iface; list_for_each_entry(iface, &interfaces, head) { - if (iface->ra != RELAYD_RELAY || iface->master) + if (iface->ra != MODE_RELAY || iface->master) continue; // Fixup source hardware address option