From owner-freebsd-net@FreeBSD.ORG Wed Apr 8 21:21:21 2015 Return-Path: Delivered-To: net@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [8.8.178.115]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by hub.freebsd.org (Postfix) with ESMTPS id 32242E06 for ; Wed, 8 Apr 2015 21:21:21 +0000 (UTC) Received: from shell1.rawbw.com (shell1.rawbw.com [198.144.192.42]) by mx1.freebsd.org (Postfix) with ESMTP id 012C3BDF for ; Wed, 8 Apr 2015 21:21:20 +0000 (UTC) Received: from yuri.doctorlan.com (c-50-184-63-128.hsd1.ca.comcast.net [50.184.63.128]) (authenticated bits=0) by shell1.rawbw.com (8.14.9/8.14.9) with ESMTP id t38LLCC3080218 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES128-SHA bits=128 verify=NO); Wed, 8 Apr 2015 14:21:13 -0700 (PDT) (envelope-from yuri@rawbw.com) X-Authentication-Warning: shell1.rawbw.com: Host c-50-184-63-128.hsd1.ca.comcast.net [50.184.63.128] claimed to be yuri.doctorlan.com Message-ID: <55259BC7.6040502@rawbw.com> Date: Wed, 08 Apr 2015 14:21:11 -0700 From: Yuri User-Agent: Mozilla/5.0 (X11; FreeBSD amd64; rv:31.0) Gecko/20100101 Thunderbird/31.6.0 MIME-Version: 1.0 To: net@freebsd.org Subject: Re: Socket bound to 0.0.0.0 never receives broadcasts with non-zero IP source address References: <55248957.60109@rawbw.com> <878ue2n6lu.fsf@corbe.net> In-Reply-To: <878ue2n6lu.fsf@corbe.net> Content-Type: text/plain; charset=windows-1252; format=flowed Content-Transfer-Encoding: 7bit Cc: Daniel Corbe X-BeenThere: freebsd-net@freebsd.org X-Mailman-Version: 2.1.18-1 Precedence: list List-Id: Networking and TCP/IP with FreeBSD List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 08 Apr 2015 21:21:21 -0000 On 04/08/2015 05:32, Daniel Corbe wrote: > If nobody answers this question by the time I get home I'll try and > help; however, in the mean time I do have a couple of suggestions. > > Have you tried writing the equivalent program in C using the sockets > API? IE is this a python specific problem or a sockets problem in > general? > > The second thing is you may just want to try using raw sockets instead. I verified before with ktrace, and now, following your suggestion, rewrote it in C, and result is the same. When I change SOCK_DGRAM->SOCK_RAW it keeps getting some other packets, sin_port doesn't seem to matter. Also, UDP is the practically important case. Unless there is some reasonable explanation why ip source address can influence reception, I believe this is a bug in kernel. And pretty important one, because it can hurt DHCP servers. Yuri --- C program exhibiting the problem --- #include #include #include #include #include #define CK(func, call...) if ((call) < 0) {perror(#func); exit(1);} int main() { int sock, one = 1; ssize_t count; struct sockaddr_in sa; char buffer[4096]; struct sockaddr_storage src_addr; struct iovec iov[1]; iov[0].iov_base=buffer; iov[0].iov_len=sizeof(buffer); struct msghdr msg; msg.msg_name=&src_addr; msg.msg_namelen=sizeof(src_addr); msg.msg_iov=iov; msg.msg_iovlen=1; msg.msg_control=0; msg.msg_controllen=0; CK(socket, sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) CK(setsockopt, setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one))) CK(setsockopt, setsockopt(sock, SOL_SOCKET, SO_BROADCAST, &one, sizeof(one))) sa.sin_family = AF_INET; sa.sin_addr.s_addr = htonl(INADDR_ANY); //sa.sin_port = htons(67); sa.sin_port = htons(1767); CK(bind, bind(sock, (const struct sockaddr*)&sa, sizeof(sa))) printf("Waiting for broadcast\n"); CK(recvmsg, count = recvmsg(sock, &msg, 0)) printf("Received broadcast packet: %zd bytes\n", count); return 0; }