X-Git-Url: http://git.archive.openwrt.org/?p=project%2Fmdnsd.git;a=blobdiff_plain;f=interface.c;h=2a695a1b10cbb1169e8bdc4d6ac36145553237eb;hp=bcd6f13609e9fe8056a984ad5cdb0b7ed14c5c4c;hb=891447c1a9db8a55f769241c94f4400e16f43dd9;hpb=df298a725d5ef2c1aa5591775b316d0afecbf713 diff --git a/interface.c b/interface.c index bcd6f13..2a695a1 100644 --- a/interface.c +++ b/interface.c @@ -12,15 +12,18 @@ * GNU General Public License for more details. */ +#define _GNU_SOURCE #include #include #include #include #include #include -#include +#include #include +#include +#include #include #include #include @@ -36,14 +39,11 @@ #include "dns.h" #include "announce.h" -int -interface_send_packet(struct interface *iface, struct iovec *iov, int iov_len) +static int +interface_send_packet4(struct interface *iface, struct iovec *iov, int iov_len) { static size_t cmsg_data[( CMSG_SPACE(sizeof(struct in_pktinfo)) / sizeof(size_t)) + 1]; - static struct sockaddr_in a = { - .sin_family = AF_INET, - .sin_port = htons(MCAST_PORT), - }; + static struct sockaddr_in a; static struct msghdr m = { .msg_name = (struct sockaddr *) &a, .msg_namelen = sizeof(a), @@ -54,6 +54,8 @@ interface_send_packet(struct interface *iface, struct iovec *iov, int iov_len) struct cmsghdr *cmsg; int fd = iface->fd.fd; + a.sin_family = AF_INET; + a.sin_port = htons(MCAST_PORT); m.msg_iov = iov; m.msg_iovlen = iov_len; @@ -71,6 +73,49 @@ interface_send_packet(struct interface *iface, struct iovec *iov, int iov_len) return sendmsg(fd, &m, 0); } +static int +interface_send_packet6(struct interface *iface, struct iovec *iov, int iov_len) +{ + static size_t cmsg_data[( CMSG_SPACE(sizeof(struct in6_pktinfo)) / sizeof(size_t)) + 1]; + static struct sockaddr_in6 a; + static struct msghdr m = { + .msg_name = (struct sockaddr *) &a, + .msg_namelen = sizeof(a), + .msg_control = cmsg_data, + .msg_controllen = CMSG_LEN(sizeof(struct in6_pktinfo)), + }; + struct in6_pktinfo *pkti; + struct cmsghdr *cmsg; + int fd = iface->fd.fd; + + a.sin6_family = AF_INET6; + a.sin6_port = htons(MCAST_PORT); + m.msg_iov = iov; + m.msg_iovlen = iov_len; + + memset(cmsg_data, 0, sizeof(cmsg_data)); + cmsg = CMSG_FIRSTHDR(&m); + cmsg->cmsg_len = m.msg_controllen; + cmsg->cmsg_level = IPPROTO_IPV6; + cmsg->cmsg_type = IPV6_PKTINFO; + + pkti = (struct in6_pktinfo*) CMSG_DATA(cmsg); + pkti->ipi6_ifindex = iface->ifindex; + + inet_pton(AF_INET6, MCAST_ADDR6, &a.sin6_addr); + + return sendmsg(fd, &m, 0); +} + +int +interface_send_packet(struct interface *iface, struct iovec *iov, int iov_len) +{ + if (iface->v6) + return interface_send_packet6(iface, iov, iov_len); + + return interface_send_packet4(iface, iov, iov_len); +} + static void interface_close(struct interface *iface) { if (iface->fd.fd < 0) @@ -89,11 +134,89 @@ static void interface_free(struct interface *iface) } static void -read_socket(struct uloop_fd *u, unsigned int events) +read_socket4(struct uloop_fd *u, unsigned int events) +{ + struct interface *iface = container_of(u, struct interface, fd); + static uint8_t buffer[8 * 1024]; + struct iovec iov[1]; + char cmsg[CMSG_SPACE(sizeof(struct in_pktinfo)) + CMSG_SPACE(sizeof(int)) + 1]; + struct cmsghdr *cmsgptr; + struct msghdr msg; + socklen_t len; + struct sockaddr_in from; + int flags = 0, ifindex = -1; + uint8_t ttl = 0; + struct in_pktinfo *inp = NULL; + + if (u->eof) { + interface_close(iface); + uloop_timeout_set(&iface->reconnect, 1000); + return; + } + + iov[0].iov_base = buffer; + iov[0].iov_len = sizeof(buffer); + + memset(&msg, 0, sizeof(msg)); + msg.msg_name = (struct sockaddr *) &from; + msg.msg_namelen = sizeof(struct sockaddr_in); + msg.msg_iov = iov; + msg.msg_iovlen = 1; + msg.msg_control = &cmsg; + msg.msg_controllen = sizeof(cmsg); + + len = recvmsg(u->fd, &msg, flags); + if (len == -1) { + perror("read failed"); + return; + } + for (cmsgptr = CMSG_FIRSTHDR(&msg); cmsgptr != NULL; cmsgptr = CMSG_NXTHDR(&msg, cmsgptr)) { + void *c = CMSG_DATA(cmsgptr); + + switch (cmsgptr->cmsg_type) { + case IP_PKTINFO: + inp = ((struct in_pktinfo *) c); + break; + + case IP_TTL: + ttl = (uint8_t) *((int *) c); + break; + + default: + fprintf(stderr, "unknown cmsg %x\n", cmsgptr->cmsg_type); + break; + } + } + + if (0) { + char buf[256]; + + inet_ntop(AF_INET, &from.sin_addr, buf, 256); + fprintf(stderr, "%s:%s[%d]%s:%d\n", __FILE__, __func__, __LINE__, buf, from.sin_port); + inet_ntop(AF_INET, &inp->ipi_spec_dst, buf, 256); + fprintf(stderr, "%s:%s[%d]%s:%d\n", __FILE__, __func__, __LINE__, buf, from.sin_port); + inet_ntop(AF_INET, &inp->ipi_addr, buf, 256); + fprintf(stderr, "%s:%s[%d]%s:%d\n", __FILE__, __func__, __LINE__, buf, from.sin_port); + } + + if (inp->ipi_ifindex != iface->ifindex) + fprintf(stderr, "invalid iface index %d != %d\n", ifindex, iface->ifindex); + else if (ttl == 255) + dns_handle_packet(iface, buffer, len, 0); +} + +static void +read_socket6(struct uloop_fd *u, unsigned int events) { struct interface *iface = container_of(u, struct interface, fd); static uint8_t buffer[8 * 1024]; - int len; + struct iovec iov[1]; + char cmsg6[CMSG_SPACE(sizeof(struct in6_pktinfo)) + 1]; + struct cmsghdr *cmsgptr; + struct msghdr msg; + socklen_t len; + struct sockaddr_in6 from; + int flags = 0, ifindex = -1; if (u->eof) { interface_close(iface); @@ -101,21 +224,41 @@ read_socket(struct uloop_fd *u, unsigned int events) return; } - len = read(u->fd, buffer, sizeof(buffer)); - if (len < 1) { - fprintf(stderr, "read failed: %s\n", strerror(errno)); + iov[0].iov_base = buffer; + iov[0].iov_len = sizeof(buffer); + + memset(&msg, 0, sizeof(msg)); + msg.msg_name = (struct sockaddr *) &from; + msg.msg_namelen = (iface->v6) ? (sizeof(struct sockaddr_in6)) : (sizeof(struct sockaddr_in)); + msg.msg_iov = iov; + msg.msg_iovlen = 1; + msg.msg_control = &cmsg6; + msg.msg_controllen = sizeof(cmsg6); + + len = recvmsg(u->fd, &msg, flags); + if (len == -1) { + perror("read failed"); return; } + for (cmsgptr = CMSG_FIRSTHDR(&msg); cmsgptr != NULL; cmsgptr = CMSG_NXTHDR(&msg, cmsgptr)) { + void *c = CMSG_DATA(cmsgptr); - dns_handle_packet(iface, buffer, len); + if (cmsgptr->cmsg_level == IPPROTO_IP && cmsgptr->cmsg_type == IP_PKTINFO) + ifindex = ((struct in_pktinfo *) c)->ipi_ifindex; + else if (cmsgptr->cmsg_level == IPPROTO_IPV6 && cmsgptr->cmsg_type == IPV6_PKTINFO) + ifindex = ((struct in6_pktinfo *) c)->ipi6_ifindex; + } + if (ifindex != iface->ifindex) + fprintf(stderr, "invalid iface index %d != %d\n", ifindex, iface->ifindex); + else + dns_handle_packet(iface, buffer, len, 0); } static int -interface_socket_setup(struct interface *iface) +interface_mcast_setup4(struct interface *iface) { struct ip_mreqn mreq; uint8_t ttl = 255; - int yes = 1; int no = 0; struct sockaddr_in sa = { 0 }; int fd = iface->fd.fd; @@ -132,9 +275,6 @@ interface_socket_setup(struct interface *iface) if (setsockopt(fd, IPPROTO_IP, IP_MULTICAST_TTL, &ttl, sizeof(ttl)) < 0) fprintf(stderr, "ioctl failed: IP_MULTICAST_TTL\n"); - if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(yes)) < 0) - fprintf(stderr, "ioctl failed: SO_REUSEADDR\n"); - /* Some network drivers have issues with dropping membership of * mcast groups when the iface is down, but don't allow rejoining * when it comes back up. This is an ugly workaround @@ -149,30 +289,110 @@ interface_socket_setup(struct interface *iface) return -1; } - if (setsockopt(fd, IPPROTO_IP, IP_RECVTTL, &yes, sizeof(yes)) < 0) + if (setsockopt(fd, IPPROTO_IP, IP_MULTICAST_LOOP, &no, sizeof(no)) < 0) + fprintf(stderr, "ioctl failed: IP_MULTICAST_LOOP\n"); + + return 0; +} + +static int +interface_socket_setup6(struct interface *iface) +{ + struct ipv6_mreq mreq; + int ttl = 255; + int no = 0; + struct sockaddr_in6 sa = { 0 }; + int fd = iface->fd.fd; + + sa.sin6_family = AF_INET6; + sa.sin6_port = htons(MCAST_PORT); + inet_pton(AF_INET6, MCAST_ADDR6, &sa.sin6_addr); + + memset(&mreq, 0, sizeof(mreq)); + mreq.ipv6mr_multiaddr = sa.sin6_addr; + mreq.ipv6mr_interface = iface->ifindex; + + if (setsockopt(fd, IPPROTO_IPV6, IPV6_MULTICAST_HOPS, &ttl, sizeof(ttl)) < 0) + fprintf(stderr, "ioctl failed: IPV6_MULTICAST_HOPS\n"); + + setsockopt(fd, IPPROTO_IPV6, IPV6_LEAVE_GROUP, &mreq, sizeof(mreq)); + if (setsockopt(fd, IPPROTO_IPV6, IPV6_ADD_MEMBERSHIP, &mreq, sizeof(mreq)) < 0) { + fprintf(stderr, "failed to join multicast group: %s\n", strerror(errno)); + close(fd); + fd = -1; + return -1; + } + + if (setsockopt(fd, IPPROTO_IPV6, IPV6_MULTICAST_LOOP, &no, sizeof(no)) < 0) + fprintf(stderr, "ioctl failed: IPV6_MULTICAST_LOOP\n"); + + return 0; +} + +static void +reconnect_socket4(struct uloop_timeout *timeout) +{ + struct interface *iface = container_of(timeout, struct interface, reconnect); + int yes = 1; + + iface->fd.fd = usock(USOCK_UDP | USOCK_SERVER | USOCK_NONBLOCK | USOCK_IPV4ONLY, iface->mcast_addr, "5353"); + if (iface->fd.fd < 0) { + fprintf(stderr, "failed to add listener %s: %s\n", iface->mcast_addr, strerror(errno)); + goto retry; + } + + if (setsockopt(iface->fd.fd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(yes)) < 0) + fprintf(stderr, "ioctl failed: SO_REUSEADDR\n"); + + if (setsockopt(iface->fd.fd, IPPROTO_IP, IP_RECVTTL, &yes, sizeof(yes)) < 0) fprintf(stderr, "ioctl failed: IP_RECVTTL\n"); - if (setsockopt(fd, IPPROTO_IP, IP_PKTINFO, &yes, sizeof(yes)) < 0) + if (setsockopt(iface->fd.fd, IPPROTO_IP, IP_PKTINFO, &yes, sizeof(yes)) < 0) fprintf(stderr, "ioctl failed: IP_PKTINFO\n"); - if (setsockopt(fd, IPPROTO_IP, IP_MULTICAST_LOOP, &no, sizeof(no)) < 0) - fprintf(stderr, "ioctl failed: IP_MULTICAST_LOOP\n"); + if (interface_mcast_setup4(iface)) { + iface->fd.fd = -1; + goto retry; + } - return 0; + uloop_fd_add(&iface->fd, ULOOP_READ); + dns_send_question(iface, "_services._dns-sd._udp.local", TYPE_PTR); + announce_init(iface); + + return; + +retry: + uloop_timeout_set(timeout, 1000); } static void -reconnect_socket(struct uloop_timeout *timeout) +reconnect_socket6(struct uloop_timeout *timeout) { struct interface *iface = container_of(timeout, struct interface, reconnect); + char mcast_addr[128]; + int ttl = 255; + int yes = 1; - iface->fd.fd = usock(USOCK_UDP | USOCK_SERVER | USOCK_NONBLOCK, MCAST_ADDR, "5353"); + snprintf(mcast_addr, sizeof(mcast_addr), "%s%%%s", iface->mcast_addr, iface->name); + iface->fd.fd = usock(USOCK_UDP | USOCK_SERVER | USOCK_NONBLOCK | USOCK_IPV6ONLY, mcast_addr, "5353"); if (iface->fd.fd < 0) { - fprintf(stderr, "failed to add listener: %s\n", strerror(errno)); + fprintf(stderr, "failed to add listener %s: %s\n", mcast_addr, strerror(errno)); goto retry; } - if (interface_socket_setup(iface)) { + if (setsockopt(iface->fd.fd, IPPROTO_IPV6, IPV6_UNICAST_HOPS, &ttl, sizeof(ttl)) < 0) + fprintf(stderr, "ioctl failed: IPV6_UNICAST_HOPS\n"); + + if (setsockopt(iface->fd.fd, IPPROTO_IPV6, IPV6_RECVPKTINFO, &yes, sizeof(yes)) < 0) + fprintf(stderr, "ioctl failed: IPV6_RECVPKTINFO\n"); + + if (setsockopt(iface->fd.fd, IPPROTO_IPV6, IPV6_RECVHOPLIMIT, &yes, sizeof(yes)) < 0) + fprintf(stderr, "ioctl failed: IPV6_RECVHOPLIMIT\n"); + + if (setsockopt(iface->fd.fd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(yes)) < 0) + fprintf(stderr, "ioctl failed: SO_REUSEADDR\n"); + + if (interface_socket_setup6(iface)) { iface->fd.fd = -1; goto retry; } @@ -189,8 +409,13 @@ retry: static void interface_start(struct interface *iface) { - iface->fd.cb = read_socket; - iface->reconnect.cb = reconnect_socket; + if (iface->v6) { + iface->fd.cb = read_socket6; + iface->reconnect.cb = reconnect_socket6; + } else { + iface->fd.cb = read_socket4; + iface->reconnect.cb = reconnect_socket4; + } uloop_timeout_set(&iface->reconnect, 100); } @@ -211,56 +436,83 @@ iface_update_cb(struct vlist_tree *tree, struct vlist_node *node_new, } } -static int -get_iface_ipv4(struct interface *iface) -{ - struct sockaddr_in *sin; - struct ifreq ir; - int sock, ret = -1; - - sock = socket(AF_INET, SOCK_DGRAM, 0); - if (sock < 0) - return -1; - - memset(&ir, 0, sizeof(struct ifreq)); - strncpy(ir.ifr_name, iface->name, sizeof(ir.ifr_name)); - - ret = ioctl(sock, SIOCGIFADDR, &ir); - if (ret < 0) - goto out; - - sin = (struct sockaddr_in *) &ir.ifr_addr; - memcpy(&iface->v4_addr, &sin->sin_addr, sizeof(iface->v4_addr)); - -out: - close(sock); - return ret; -} - -int interface_add(const char *name) +static struct interface* _interface_add(const char *name, int v6) { struct interface *iface; char *name_buf; + char *id_buf; iface = calloc_a(sizeof(*iface), - &name_buf, strlen(name) + 1); + &name_buf, strlen(name) + 1, + &id_buf, strlen(name) + 3); + sprintf(id_buf, "%d_%s", v6, name); iface->name = strcpy(name_buf, name); + iface->id = id_buf; iface->ifindex = if_nametoindex(name); iface->fd.fd = -1; + iface->v6 = v6; + if (v6) + iface->mcast_addr = MCAST_ADDR6; + else + iface->mcast_addr = MCAST_ADDR; if (iface->ifindex <= 0) goto error; - if (get_iface_ipv4(iface)) - goto error; - - vlist_add(&interfaces, &iface->node, name); - return 0; + vlist_add(&interfaces, &iface->node, iface->id); + return iface; error: free(iface); - return -1; + return NULL; +} + +int interface_add(const char *name) +{ + struct interface *v4 = NULL, *v6 = NULL; + struct ifaddrs *ifap, *ifa; + + getifaddrs(&ifap); + + for (ifa = ifap; ifa; ifa = ifa->ifa_next) { + if (strcmp(ifa->ifa_name, name)) + continue; + if (ifa->ifa_addr->sa_family == AF_INET && !v4) { + struct sockaddr_in *sa; + + if (cfg_proto && (cfg_proto != 4)) + continue; + + v4 = _interface_add(name, 0); + if (!v4) + continue; + + sa = (struct sockaddr_in *) ifa->ifa_addr; + memcpy(&v4->v4_addr, &sa->sin_addr, sizeof(v4->v4_addr)); + } + + if (ifa->ifa_addr->sa_family == AF_INET6 && !v6) { + uint8_t ll_prefix[] = {0xfe, 0x80 }; + struct sockaddr_in6 *sa6; + + if (cfg_proto && (cfg_proto != 6)) + continue; + + sa6 = (struct sockaddr_in6 *) ifa->ifa_addr; + if (memcmp(&sa6->sin6_addr, &ll_prefix, 2)) + continue; + + v6 = _interface_add(name, 1); + if (!v6) + continue; + memcpy(&v6->v6_addr, &sa6->sin6_addr, sizeof(v6->v6_addr)); + } + } + + freeifaddrs(ifap); + + return !v4 && !v6; } VLIST_TREE(interfaces, avl_strcmp, iface_update_cb, false, false);