From patchwork Mon Oct 24 12:54:52 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jiri Slaby X-Patchwork-Id: 78948 Delivered-To: patch@linaro.org Received: by 10.140.97.247 with SMTP id m110csp2562522qge; Mon, 24 Oct 2016 05:55:05 -0700 (PDT) X-Received: by 10.99.216.21 with SMTP id b21mr23754556pgh.110.1477313705533; Mon, 24 Oct 2016 05:55:05 -0700 (PDT) Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id q17si15271400pge.19.2016.10.24.05.55.05; Mon, 24 Oct 2016 05:55:05 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of netdev-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of netdev-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=netdev-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S938710AbcJXMy6 (ORCPT + 4 others); Mon, 24 Oct 2016 08:54:58 -0400 Received: from mail-wm0-f49.google.com ([74.125.82.49]:38584 "EHLO mail-wm0-f49.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S934873AbcJXMy5 (ORCPT ); Mon, 24 Oct 2016 08:54:57 -0400 Received: by mail-wm0-f49.google.com with SMTP id c78so114248703wme.1 for ; Mon, 24 Oct 2016 05:54:56 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:from:subject:to:message-id:date:user-agent :mime-version; bh=3a26lW/VVRJeaGASTNQp+8MVmVhjf6nhDi98kwr84EM=; b=bbiYVBhdUkCU2AJrfIICgMSw/Nxr0f/8RSChFUdhZ/Cw7m2EJZGRIYAmlk6TZX49N+ XxBIBNbNpZ8dnFLYp267LCybthziMIXCJ+znwqIhwBt+4KwQVtIOTKuHnxUlCC3+z0Ng kxl38P9wHxN0oMbsAEx9xq/pciHj0VK0P/3ktzeiqiPWIGMxUrjHWgSmxn4brQdFtwOM 92eai8y3uu9OAZ8kezzqVTASm5n8Df+U64SVxwZ3vjpNDn2MNvBTqfB1RO2SO7a9lEwU 5ybpPA8lE8JZGCAonAPhLKArB88XQ+Ej1IPYEG03Et5Qsh8GfdiKMfd7LjflLp0L5biq bZZA== X-Gm-Message-State: AA6/9RnW3N3xDIe3fNsxfZX+HVH6p4Ye5+SQmmUydVlph61CQS4bGz3WI3o2ZtbTzhYpug== X-Received: by 10.28.229.132 with SMTP id c126mr15571743wmh.110.1477313693974; Mon, 24 Oct 2016 05:54:53 -0700 (PDT) Received: from ?IPv6:2a01:4240:2e27:ad85:aaaa::19f? (f.9.1.0.0.0.0.0.0.0.0.0.a.a.a.a.5.8.d.a.7.2.e.2.0.4.2.4.1.0.a.2.v6.cust.nbox.cz. [2a01:4240:2e27:ad85:aaaa::19f]) by smtp.gmail.com with ESMTPSA id u64sm14660618wmd.6.2016.10.24.05.54.52 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 24 Oct 2016 05:54:53 -0700 (PDT) From: Jiri Slaby Subject: UDP does not autobind on recv To: ML netdev Message-ID: <5d0f78b4-f850-4f73-dbb7-6c3d15d99c86@suse.cz> Date: Mon, 24 Oct 2016 14:54:52 +0200 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101 Thunderbird/45.4.0 MIME-Version: 1.0 Sender: netdev-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org Hello, as per man 7 udp: In order to receive packets, the socket can be bound to a local address first by using bind(2). Otherwise, the socket layer will automatically assign a free local port out of the range defined by /proc/sys/net/ipv4 /ip_local_port_range and bind the socket to INADDR_ANY. I did not know that bind is unneeded, so I tried that. But it does not work with this piece of code: int main() { char buf[128]; int fd = socket(AF_INET, SOCK_DGRAM, 0); recv(fd, buf, sizeof(buf), 0); } The recv above never returns (even if I bomb all ports from the range). ss -ulpan is silent too. As a workaround, I can stick a dummy write/send before recv: write(fd, "", 0); And it starts working. ss suddenly displays a port which the program listens on. I think the UDP recv path should do inet_autobind as I have done in the attached patch. But my knowledge is very limited in that area, so I have no idea whether that is correct at all. thanks, -- js suse labs >From 57c320998feb2e1e705a4ab6d3bbcb74c6ae65f0 Mon Sep 17 00:00:00 2001 From: Jiri Slaby Date: Sat, 22 Oct 2016 12:10:53 +0200 Subject: [PATCH] net: autobind UDP on recv Signed-off-by: Jiri Slaby --- include/net/inet_common.h | 1 + net/ipv4/af_inet.c | 3 ++- net/ipv4/udp.c | 5 +++++ net/ipv6/udp.c | 5 +++++ 4 files changed, 13 insertions(+), 1 deletion(-) diff --git a/include/net/inet_common.h b/include/net/inet_common.h index 5d683428fced..ba224ed3dd36 100644 --- a/include/net/inet_common.h +++ b/include/net/inet_common.h @@ -27,6 +27,7 @@ ssize_t inet_sendpage(struct socket *sock, struct page *page, int offset, int inet_recvmsg(struct socket *sock, struct msghdr *msg, size_t size, int flags); int inet_shutdown(struct socket *sock, int how); +int inet_autobind(struct sock *sk); int inet_listen(struct socket *sock, int backlog); void inet_sock_destruct(struct sock *sk); int inet_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len); diff --git a/net/ipv4/af_inet.c b/net/ipv4/af_inet.c index 9648c97e541f..d23acb11cdb0 100644 --- a/net/ipv4/af_inet.c +++ b/net/ipv4/af_inet.c @@ -171,7 +171,7 @@ EXPORT_SYMBOL(inet_sock_destruct); * Automatically bind an unbound socket. */ -static int inet_autobind(struct sock *sk) +int inet_autobind(struct sock *sk) { struct inet_sock *inet; /* We may need to bind the socket. */ @@ -187,6 +187,7 @@ static int inet_autobind(struct sock *sk) release_sock(sk); return 0; } +EXPORT_SYMBOL_GPL(inet_autobind); /* * Move a socket into listening state. diff --git a/net/ipv4/udp.c b/net/ipv4/udp.c index 82fb78265f4b..ceb07c83af17 100644 --- a/net/ipv4/udp.c +++ b/net/ipv4/udp.c @@ -1360,6 +1360,11 @@ int udp_recvmsg(struct sock *sk, struct msghdr *msg, size_t len, int noblock, if (flags & MSG_ERRQUEUE) return ip_recv_error(sk, msg, len, addr_len); + /* We may need to bind the socket. */ + if (!inet_sk(sk)->inet_num && !sk->sk_prot->no_autobind && + inet_autobind(sk)) + return -EAGAIN; + try_again: peeking = off = sk_peek_offset(sk, flags); skb = __skb_recv_datagram(sk, flags | (noblock ? MSG_DONTWAIT : 0), diff --git a/net/ipv6/udp.c b/net/ipv6/udp.c index 71963b23d5a5..1c3dafc3d91e 100644 --- a/net/ipv6/udp.c +++ b/net/ipv6/udp.c @@ -341,6 +341,11 @@ int udpv6_recvmsg(struct sock *sk, struct msghdr *msg, size_t len, if (np->rxpmtu && np->rxopt.bits.rxpmtu) return ipv6_recv_rxpmtu(sk, msg, len, addr_len); + /* We may need to bind the socket. */ + if (!inet_sk(sk)->inet_num && !sk->sk_prot->no_autobind && + inet_autobind(sk)) + return -EAGAIN; + try_again: peeking = off = sk_peek_offset(sk, flags); skb = __skb_recv_datagram(sk, flags | (noblock ? MSG_DONTWAIT : 0), -- 2.10.1