Since inception on NetBSD, IP_HDRINCL for RAW sockets requires ip_len and ip_off to by sent in host byte order. https://nxr.netbsd.org/xref/src/sys/netinet/raw_ip.c#369 This makes literally no sense because reading from the same socket you get ip_len and ip_off in network byte order. Writing to a bpf socket, ip_len and ip_off have to be done in network byte order. No other part of the system behaves like this (that I know of) and it's not documented in ip(4). Every other OS does not do this. Or if they did, they don't do it now. Our Linux compat support for this socket option does nothing special here, so it won't work for Linux binaries. There is even a comment by a NetBSD developer in the dhclient code complaining that the raw socket code didn't work on NetBSD and this is likely why as the ancient code there assumes network byte ordering for these fields. Attached is a patch to fix this by adding IP_HDRINCL_RAW and mapping the linux compat socket option to this. This allows old binaries and old software to compile and work as before. Portable software should then be written thusly, assuming all target platforms are modern. #ifdef IP_HDRINCL_RAW setsockopt(s, IPPROTO_IP, IP_HDRINCL_RAW, &hincl, sizeof(hincl)); #else #ifdef __NetBSD__ // Either error here or handle byte swapping yourself as before #endif setsockopt(s, IPPROTO_IP, IP_HDRINCL, &hincl, sizeof(hincl)); #endif Comments, as always, welcome. Roy
Attachment:
IP_HDRINCL_RAW.patch
Description: Binary data