From owner-freebsd-net@FreeBSD.ORG Fri Jan 8 11:02:58 2010 Return-Path: Delivered-To: freebsd-net@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id C4813106568B for ; Fri, 8 Jan 2010 11:02:58 +0000 (UTC) (envelope-from jahuttun@gmail.com) Received: from mail-ew0-f226.google.com (mail-ew0-f226.google.com [209.85.219.226]) by mx1.freebsd.org (Postfix) with ESMTP id 5E4498FC12 for ; Fri, 8 Jan 2010 11:02:58 +0000 (UTC) Received: by ewy26 with SMTP id 26so17673262ewy.3 for ; Fri, 08 Jan 2010 03:02:54 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:mime-version:received:date:message-id:subject :from:to:content-type; bh=uEWXW3XCsJdNOBoFiNADaYY3VFJWUniJ0u8sTtVRaKc=; b=I2JaTNhEuhxZSwi58o/HOn1dVMqLpd15QUMSC1nkNBSfIYwECZs+dTsNIU9ZJueOi5 cb8/WKS1lyY8a7FSGMYH1ETvSc7LPe6X+TbmCd0IekcfArLL+Ps/YezF6i6D2TajMhko AZ6oa0ZZlAiczlMHtVN7cpL8+zUJOBM+wyjjw= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=mime-version:date:message-id:subject:from:to:content-type; b=pNJz3u/RwVRJEDmfnV16Oi3g3G1fScr42GK3inySf1gQcQNSwn+Q0VFLWmsIQEJy7i CAFXFM3bio1u6WCr2jtryTcDfUoQnJGY33Y8gPXV8651y58db8Y3ryOuy6vVERisykWZ bQZf1+TFDRMo3gYxpGcAzjvpVwljpvx/bC6i4= MIME-Version: 1.0 Received: by 10.216.89.70 with SMTP id b48mr2025999wef.160.1262947112631; Fri, 08 Jan 2010 02:38:32 -0800 (PST) Date: Fri, 8 Jan 2010 12:38:32 +0200 Message-ID: <4cd8d14e1001080238yfc2ee4cx6f261aa94f79a246@mail.gmail.com> From: Janne Huttunen To: freebsd-net@freebsd.org Content-Type: text/plain; charset=ISO-8859-1 Subject: Anon port selection X-BeenThere: freebsd-net@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: Networking and TCP/IP with FreeBSD List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 08 Jan 2010 11:02:58 -0000 Hi! The selection of anonymous port in FreeBSD seems to act a bit weird (bug?). This was first observed on actual use on FreeBSD 6.2, but I have verified that the it behaves the same on a December snapshot of CURRENT too. 1. A process creates an UDP socket and sends a packet from it (at which point a local port is assigned for it). 2. Another process creates an UDP socket, sets SO_REUSEADDR (or SO_REUSEPORT) and sends a packet from it (at which point a local port is assigned for it). Every now and then it happens that the second process gets the same local port as the first one. If the second process doesn't set the socket option this won't happen. Note however, that the first process does not have to cooperate in any way i.e. it does not set any options. Now, I'm fairly newbie when it comes to the FreeBSD IP stack, but it seems to me that this phenomenon is caused by the code in in_pcbconnect_setup(). If the local port is zero the in_pcbbind_setup() is called to select a port. That routine is called with the local address set to the source address selected for the outgoing packet, but when the port has been selected, it is committed with INADDR_ANY as the local address. Then when the second process in in_pcbbind_setup() tries to check if the port is already in use, it won't match the INADDR_ANY and assigns the same port again.