All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH RFC] net/unix: SO_REUSEPORT for AF_UNIX
@ 2015-06-28  9:52 Conrad Hoffmann
  2015-06-29  4:05 ` Alex Gartrell
  0 siblings, 1 reply; 3+ messages in thread
From: Conrad Hoffmann @ 2015-06-28  9:52 UTC (permalink / raw)
  To: netdev; +Cc: Conrad Hoffmann

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(-)

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;
 		}
-- 
2.4.4

^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH RFC] net/unix: SO_REUSEPORT for AF_UNIX
  2015-06-28  9:52 [PATCH RFC] net/unix: SO_REUSEPORT for AF_UNIX Conrad Hoffmann
@ 2015-06-29  4:05 ` Alex Gartrell
  2015-06-29 11:17   ` Conrad Hoffmann
  0 siblings, 1 reply; 3+ messages in thread
From: Alex Gartrell @ 2015-06-29  4:05 UTC (permalink / raw)
  To: Conrad Hoffmann; +Cc: netdev

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.

-- 
Alex Gartrell

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH RFC] net/unix: SO_REUSEPORT for AF_UNIX
  2015-06-29  4:05 ` Alex Gartrell
@ 2015-06-29 11:17   ` Conrad Hoffmann
  0 siblings, 0 replies; 3+ messages in thread
From: Conrad Hoffmann @ 2015-06-29 11:17 UTC (permalink / raw)
  To: Alex Gartrell; +Cc: netdev

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

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2015-06-29 11:18 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-06-28  9:52 [PATCH RFC] net/unix: SO_REUSEPORT for AF_UNIX Conrad Hoffmann
2015-06-29  4:05 ` Alex Gartrell
2015-06-29 11:17   ` Conrad Hoffmann

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.