Readd fixed ICMPv6 validity check
authorSteven Barth <steven@midlink.org>
Mon, 23 Dec 2013 13:06:45 +0000 (14:06 +0100)
committerSteven Barth <steven@midlink.org>
Mon, 23 Dec 2013 13:06:45 +0000 (14:06 +0100)
src/router.c

index 5f3921d2164b0e91b0e5a081f1b0ec6177a13328..7b81f71ebec07591764fc9802216798e6251a5bb 100644 (file)
@@ -130,12 +130,50 @@ static void sigusr1_refresh(_unused int signal)
                        uloop_timeout_set(&iface->timer_rs, 1000);
 }
 
+static bool router_icmpv6_valid(struct sockaddr_in6 *source, uint8_t *data, size_t len)
+{
+       struct icmp6_hdr *hdr = (struct icmp6_hdr *)data;
+       struct icmpv6_opt *opt, *end = (struct icmpv6_opt*)&data[len];
+
+       /* Hoplimit is already checked in odhcpd_receive_packets */
+       if (len < sizeof(*hdr) || hdr->icmp6_code)
+               return false;
+
+       switch (hdr->icmp6_type) {
+       case ND_ROUTER_ADVERT:
+               if (!IN6_IS_ADDR_LINKLOCAL(&source->sin6_addr))
+                       return false;
+
+               opt = (struct icmpv6_opt *)((struct nd_router_advert *)data + 1);
+               break;
+
+       case ND_ROUTER_SOLICIT:
+               opt = (struct icmpv6_opt *)((struct nd_router_solicit *)data + 1);
+               break;
+
+       default:
+               return false;
+       }
+
+       icmpv6_for_each_option(opt, opt, end)
+               if (opt->type == ND_OPT_SOURCE_LINKADDR &&
+                               IN6_IS_ADDR_UNSPECIFIED(&source->sin6_addr) &&
+                               hdr->icmp6_type == ND_ROUTER_SOLICIT)
+                       return false;
+
+       // Check all options parsed successfully
+       return opt == end;
+}
 
 // Event handler for incoming ICMPv6 packets
-static void handle_icmpv6(_unused void *addr, void *data, size_t len,
+static void handle_icmpv6(void *addr, void *data, size_t len,
                struct interface *iface)
 {
        struct icmp6_hdr *hdr = data;
+
+       if (!router_icmpv6_valid(addr, data, len))
+               return;
+
        if ((iface->ra == RELAYD_SERVER && !iface->master)) { // Server mode
                if (hdr->icmp6_type == ND_ROUTER_SOLICIT)
                        send_router_advert(&iface->timer_rs);