msquic: macOS Dual-Stack sockets don't report back sockopts

Describe the bug

Working on macOS support (#18), and trying to implement everything over one (v6) socket. This is very similar to how the current windows / linux datapath behave. Currently I designate AF_INET or AF_INET6 socket depending on the local address or the remote address (listener / connection) info. But ideally everything would just be v6 or v4-mapped-v6 over one socket.

When setting up an AF_INET6 socket and setting the socket options to get back packet information (destination address, interface info, etc), the information is never set in the msg_control buffer.

(.NET appears to have seen this too OS X does not provide IPv4 packet info for dual-stack sockets.)

I have a “minimal” PoC that shows this behavior with an IPv6 socket and v4-mapped addresses.

Steps to reproduce the behavior

  1. Compile code below on macOS; $ gcc test6.c -o test6 -g -O0 -glldb
  2. On another machine, setup a netcat listener over udp; $ while :; do echo -ne "QQQQ" | nc -k -u -lvp 4433; done
  3. Run test6 $ ./test6 host 4433
  4. See output analogous to this:
[:(] SETSOCKOPT FAILED: setsockopt(sockfd, IPPROTO_IPV6, IPV6_PKTINFO, &Option, sizeof(Option)) = -1, errno = 22
setsockopt: Invalid argument
[:(] SETSOCKOPT FAILED: setsockopt(sockfd, IPPROTO_IP, IP_RECVDSTADDR, &Option, sizeof(Option)) = -1, errno = 22
setsockopt: Invalid argument
[:(] SETSOCKOPT FAILED: setsockopt(sockfd, IPPROTO_IP, IP_PKTINFO, &Option, sizeof(Option)) = -1, errno = 22
setsockopt: Invalid argument
[:(] SETSOCKOPT FAILED: setsockopt(sockfd, IPPROTO_IP, IP_RECVIF, &Option, sizeof(Option)) = -1, errno = 22
setsockopt: Invalid argument
Socket established. Binded & connected.
[kevent] ident = 4, filter = 65535, flags = 33, fflags = 0, data = 25, udata = 0x0
[!!] Received 4 bytes...51 51 51 51 51

The expected behavior here is to hit the __asm__("int3") inside of the handle_recv. However, no control msg options are ever set.

proof of concept:

// See netinet6/in6.h:46 for an explanation
#define __APPLE_USE_RFC_3542 1
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/errno.h>
#include <sys/socket.h>
#include <sys/sysctl.h>
#include <sys/event.h>
#include <sys/time.h>
#include <netdb.h>

#define LOGSOCKOPT(x) { \
    int __retv = (x); \
    if (__retv == -1) { \
        printf("[:(] SETSOCKOPT FAILED: %s = -1, errno = %d\n", #x, errno); \
        perror("setsockopt"); \
    } \
}

void do_kqueue(int kq, int sockfd);
void handle_recv(struct msghdr *recvmsghdr, int packet_len);

int main(int argc, char **argv) {
    if (argc < 3) {
        printf("./bin <host> <port>\n");
        exit(4);
    }

    struct addrinfo hints = { };
    struct addrinfo *raw_addr = NULL;

    struct sockaddr_in6 local_addr = { };
    local_addr.sin6_family = AF_INET6;

    struct sockaddr_in6 remote_addr = { }; 

    // Want to talk v4 over v6
    hints.ai_family = AF_INET;
    hints.ai_flags = AI_CANONNAME | AI_PASSIVE;

    int ret = getaddrinfo(argv[1], NULL, &hints, &raw_addr);
    if (ret != 0) {
        printf("Couldn't get addrinfo. ret = %d, errno = %d\n", ret, errno);
        exit(5);
    }

    int sockfd = socket(AF_INET6, SOCK_DGRAM, 0);
    if (sockfd == -1) {
        printf("Couldn't create socket. errno = %d\n", errno);
        exit(6);
    }

    int Option = 0;

    Option = FALSE;
    LOGSOCKOPT(setsockopt(sockfd, IPPROTO_IPV6, IPV6_V6ONLY, &Option, sizeof(Option)));

    // Many of these will fail
    // Just try all the combinations
    Option = TRUE;
    LOGSOCKOPT(setsockopt(sockfd, IPPROTO_IPV6, IPV6_RECVPKTINFO, &Option, sizeof(Option)));
    Option = TRUE;
    LOGSOCKOPT(setsockopt(sockfd, IPPROTO_IPV6, IPV6_PKTINFO, &Option, sizeof(Option)));
    Option = TRUE;
    LOGSOCKOPT(setsockopt(sockfd, IPPROTO_IP, IP_RECVDSTADDR, &Option, sizeof(Option)));
    Option = TRUE;
    LOGSOCKOPT(setsockopt(sockfd, IPPROTO_IP, IP_PKTINFO, &Option, sizeof(Option)));
    Option = TRUE;
    LOGSOCKOPT(setsockopt(sockfd, IPPROTO_IPV6, IP_RECVIF, &Option, sizeof(Option)));
    Option = TRUE;
    LOGSOCKOPT(setsockopt(sockfd, IPPROTO_IP, IP_RECVIF, &Option, sizeof(Option)));

    ret = bind(sockfd, (const struct sockaddr *)&local_addr, sizeof(local_addr));
    if (ret == -1) {
        printf("Couldn't bind. errno = %d\n", ret);
        exit(7);
    }

    if (raw_addr->ai_addr->sa_family != AF_INET) {
        printf("want ipv4 here. Try again.\n");
        exit(8);
    }

    struct sockaddr_in *v4addr = (struct sockaddr_in *)raw_addr->ai_addr;

    // map v4 to v6
    remote_addr.sin6_family = AF_INET6;
    remote_addr.sin6_port = htons(atol(argv[2]));
    memset(&(remote_addr.sin6_addr.s6_addr[10]), 0xff, 2);
    memcpy(&(remote_addr.sin6_addr.s6_addr[12]), &v4addr->sin_addr.s_addr, 4);

    // now remote_addr is a v4-mapped v6 address
    ret = connect(sockfd, (const struct sockaddr *)&remote_addr, sizeof(remote_addr));
    if (ret == -1) {
        printf("Couldn't connect. ret = %d, errno = %d\n", ret, errno);
        exit(9);
    }

    printf("Socket established. Binded & connected.\n");

    int kq = kqueue();

    if (kq == -1) {
        printf("Failed to kqueue. errno = %d\n", errno);
        exit(10);
    }

    // add fd to kqueue
    struct kevent evSet = { };
    EV_SET(&evSet, sockfd, EVFILT_READ, EV_ADD | EV_CLEAR, 0, 0, (void *)NULL);
    if (kevent(kq, &evSet, 1, NULL, 0, NULL) < 0)  {
        printf("kevent failed??\n");
        exit(11);
    }

    // send pkt to notify remote 
    sendto(sockfd, "ping", 4, 0, NULL, 0);

    do_kqueue(kq, sockfd);

    return 100;
}

void do_kqueue(int kq, int sockfd) {
    int ret = 0;

    char control_space[CMSG_SPACE(8192)] = {};
    struct msghdr recvmsghdr = {};
    char packet_space[1500] = {};

    struct iovec recv_iov;
    recv_iov.iov_len = 1200; // just a safe buffer from the 1500 mut
    recv_iov.iov_base = &packet_space;

    recvmsghdr.msg_iov = &recv_iov;
    recvmsghdr.msg_iovlen = 1;
    recvmsghdr.msg_control = &control_space;
    recvmsghdr.msg_controllen = sizeof(control_space);
    recvmsghdr.msg_flags = 0;

    struct kevent evSet;
    struct kevent evList[32];

    while ((1)) {
        int nev = kevent(kq, NULL, 0, evList, 32, NULL);
        if (nev < 1) __asm__("int3");

        for (int i = 0; i < nev; i++) {
            printf("[kevent] ident = %zd, filter = %hu, flags = %hd, fflags = %d, data = %zd, udata = %p\n", 
                    evList[i].ident, evList[i].filter, evList[i].flags, evList[i].fflags, evList[i].data, evList[i].udata);

            if (evList[i].filter == EVFILT_READ) {
                ret = recvmsg(sockfd, &recvmsghdr, 0);
                if (ret != -1) handle_recv(&recvmsghdr, ret);
            }
            else {
                printf("Unhandled evlist filter type: %d\n", evList[i].filter);
            }
        }
    }
}

void handle_recv(struct msghdr *recvmsghdr, int packet_len) {
    printf("[!!] Received %d bytes...", packet_len);

    for (int i = 0; i < MIN(packet_len, 5); i++) {
        printf("%02x ", ((char *)recvmsghdr->msg_iov->iov_base)[i]);
    }

    printf("\n");

    struct cmsghdr *cmsg;
    for (cmsg = CMSG_FIRSTHDR(recvmsghdr); cmsg != NULL; cmsg = CMSG_NXTHDR(recvmsghdr, cmsg)) {
        // this never gets hit
        __asm__("int3");

        printf("cmsg_level: %d, cmsg_type: %d\n", cmsg->cmsg_level, cmsg->cmsg_type);
    }
}

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Comments: 15 (6 by maintainers)

Most upvoted comments

Ah, interesting and thanks for the minimal example! Will investigate, and please do file a bug about this and post the number once you do.

The number appears to be FB7742331 😃