diff mbox

[RFC] net/unix: SO_REUSEPORT for AF_UNIX

Message ID 1435485139-16866-1-git-send-email-ch@bitfehler.net
State RFC, archived
Delegated to: David Miller
Headers show

Commit Message

Conrad Hoffmann June 28, 2015, 9:52 a.m. UTC
Support the SO_REUSEPORT option for AF_UNIX (aka AF_LOCAL) sockets. Note
that unlike the IP implementations, the semantics for AF_UNIX sockets are
those of the original BSD implementation, i.e. each socket that
successfully reuses a port completely takes over from the previous
listener.

The vast majority of software does an unlink() before bind() on UNIX
sockets. This also effectively takes over the socket from the previous
listener (given sufficient permissions), but leads to a short window of
time where connections are refused because the socket doesn't exist. One
can now achieve the same behaviour without dropping a single connection by
using SO_REUSEPORT and not calling unlink() before bind().

The restrictions on this are the same as for the IP implementation:
listening socket on the given path must exist, also have SO_REUSEPORT set
and have the same uid.

Signed-off-by: Conrad Hoffmann <ch@bitfehler.net>
---
 net/unix/af_unix.c | 31 +++++++++++++++++++++++++++++--
 1 file changed, 29 insertions(+), 2 deletions(-)

Comments

Alex Gartrell June 29, 2015, 4:05 a.m. UTC | #1
On Sun, Jun 28, 2015 at 2:52 AM, Conrad Hoffmann <ch@bitfehler.net> wrote:
> Support the SO_REUSEPORT option for AF_UNIX (aka AF_LOCAL) sockets. Note
> that unlike the IP implementations, the semantics for AF_UNIX sockets are
> those of the original BSD implementation, i.e. each socket that
> successfully reuses a port completely takes over from the previous
> listener.

This is a really weird corner case from a user's perspective.  I think
sharing it is more reasonable, as you can always signal the other
process out of band.
Conrad Hoffmann June 29, 2015, 11:17 a.m. UTC | #2
Hi,

On 06/29/2015 06:05 AM, Alex Gartrell wrote:
> On Sun, Jun 28, 2015 at 2:52 AM, Conrad Hoffmann <ch@bitfehler.net> wrote:
>> Support the SO_REUSEPORT option for AF_UNIX (aka AF_LOCAL) sockets. Note
>> that unlike the IP implementations, the semantics for AF_UNIX sockets are
>> those of the original BSD implementation, i.e. each socket that
>> successfully reuses a port completely takes over from the previous
>> listener.
> 
> This is a really weird corner case from a user's perspective.  I think
> sharing it is more reasonable, as you can always signal the other
> process out of band.

Thanks for the feedback. While I don't think the use case is that weird I
understand that the shared solution would accomodate for both this and
other use case, thus being the superior one. I'll see what I can do.

Conrad
--
To unsubscribe from this list: send the line "unsubscribe netdev" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
diff mbox

Patch

diff --git a/net/unix/af_unix.c b/net/unix/af_unix.c
index 03ee4d3..ef57199 100644
--- a/net/unix/af_unix.c
+++ b/net/unix/af_unix.c
@@ -326,6 +326,29 @@  found:
 	return s;
 }
 
+static bool unix_port_reusable(struct sock *sk, const char *sun_path,
+			       struct path *path)
+{
+	struct sock *owner;
+	bool ret;
+
+	if (!sk->sk_reuseport)
+		return false;
+
+	if (kern_path(sun_path, LOOKUP_FOLLOW, path))
+		return false;
+
+	owner = unix_find_socket_byinode(d_backing_inode(path->dentry));
+	if (!owner)
+		return false;
+
+	ret = owner->sk_reuseport &&
+	      owner->sk_type == sk->sk_type &&
+	      uid_eq(sock_i_uid(sk), sock_i_uid(owner));
+	sock_put(owner);
+	return ret;
+}
+
 static inline int unix_writable(struct sock *sk)
 {
 	return (atomic_read(&sk->sk_wmem_alloc) << 2) <= sk->sk_sndbuf;
@@ -914,9 +937,13 @@  static int unix_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
 		umode_t mode = S_IFSOCK |
 		       (SOCK_INODE(sock)->i_mode & ~current_umask());
 		err = unix_mknod(sun_path, mode, &path);
-		if (err) {
-			if (err == -EEXIST)
+		if (err == -EEXIST) {
+			if (unix_port_reusable(sk, sun_path, &path))
+				err = 0;
+			else
 				err = -EADDRINUSE;
+		}
+		if (err) {
 			unix_release_addr(addr);
 			goto out_up;
 		}