X-Git-Url: http://git.archive.openwrt.org/?p=project%2Fodhcpd.git;a=blobdiff_plain;f=src%2Fndp.c;h=44884be7395f83aa8c90c50d1f20eea393475c14;hp=e006c85261af4180147fb6b79b1d37a95f7a8e99;hb=428908569b8da45613891e21736c587894e0c449;hpb=56c6a4a72a0e086074f69d2ee5cc46079d179dac diff --git a/src/ndp.c b/src/ndp.c index e006c85..44884be 100644 --- a/src/ndp.c +++ b/src/ndp.c @@ -17,6 +17,7 @@ #include #include +#include #include #include #include @@ -32,9 +33,9 @@ static void handle_solicit(void *addr, void *data, size_t len, - struct interface *iface); + struct interface *iface, void *dest); static void handle_rtnetlink(void *addr, void *data, size_t len, - struct interface *iface); + struct interface *iface, void *dest); static struct ndp_neighbor* find_neighbor(struct in6_addr *addr, bool strict); static void modify_neighbor(struct in6_addr *addr, struct interface *iface, bool add); @@ -90,27 +91,12 @@ int init_ndp(void) send(rtnl_event.uloop.fd, &req2, sizeof(req2), MSG_DONTWAIT); odhcpd_register(&rtnl_event); - - // Create socket for intercepting NDP - int sock = socket(AF_PACKET, SOCK_DGRAM | SOCK_CLOEXEC | SOCK_NONBLOCK, - htons(ETH_P_ALL)); // ETH_P_ALL for ingress + egress - if (sock < 0) { - syslog(LOG_ERR, "Unable to open packet socket: %s", - strerror(errno)); - return -1; - } - - if (setsockopt(sock, SOL_SOCKET, SO_ATTACH_FILTER, - &bpf_prog, sizeof(bpf_prog))) { - syslog(LOG_ERR, "Failed to set BPF: %s", strerror(errno)); - return -1; - } - - ndp_event.uloop.fd = sock; - odhcpd_register(&ndp_event); - // Open ICMPv6 socket ping_socket = socket(AF_INET6, SOCK_RAW | SOCK_CLOEXEC, IPPROTO_ICMPV6); + if (ping_socket < 0) { + syslog(LOG_ERR, "Unable to open raw socket: %s", strerror(errno)); + return -1; + } int val = 2; setsockopt(ping_socket, IPPROTO_RAW, IPV6_CHECKSUM, &val, sizeof(val)); @@ -197,6 +183,39 @@ int setup_ndp_interface(struct interface *iface, bool enable) } } + bool enable_packet = false; + struct interface *i; + list_for_each_entry(i, &interfaces, head) { + if (i == iface && !enable) + continue; + + if (i->ndp == RELAYD_RELAY) + enable_packet = true; + } + + if (enable_packet && ndp_event.uloop.fd < 0) { + // Create socket for intercepting NDP + int sock = socket(AF_PACKET, SOCK_DGRAM | SOCK_CLOEXEC | SOCK_NONBLOCK, + htons(ETH_P_ALL)); // ETH_P_ALL for ingress + egress + if (sock < 0) { + syslog(LOG_ERR, "Unable to open packet socket: %s", + strerror(errno)); + return -1; + } + + if (setsockopt(sock, SOL_SOCKET, SO_ATTACH_FILTER, + &bpf_prog, sizeof(bpf_prog))) { + syslog(LOG_ERR, "Failed to set BPF: %s", strerror(errno)); + return -1; + } + + ndp_event.uloop.fd = sock; + odhcpd_register(&ndp_event); + } else if (!enable_packet && ndp_event.uloop.fd >= 0) { + close(ndp_event.uloop.fd); + ndp_event.uloop.fd = -1; + } + return 0; } @@ -219,7 +238,7 @@ static ssize_t ping6(struct in6_addr *addr, // Handle solicitations static void handle_solicit(void *addr, void *data, size_t len, - struct interface *iface) + struct interface *iface, _unused void *dest) { struct ip6_hdr *ip6 = data; struct nd_neighbor_solicit *req = (struct nd_neighbor_solicit*)&ip6[1]; @@ -431,7 +450,7 @@ static void modify_neighbor(struct in6_addr *addr, // Handler for neighbor cache entries from the kernel. This is our source // to learn and unlearn hosts on interfaces. static void handle_rtnetlink(_unused void *addr, void *data, size_t len, - _unused struct interface *iface) + _unused struct interface *iface, _unused void *dest) { for (struct nlmsghdr *nh = data; NLMSG_OK(nh, len); nh = NLMSG_NEXT(nh, len)) {