Skip to contentSkip to conversation

Introduce getsockname io_uring_cmd

PATCH Superseded Gabriel Krisman Bertazi 2 participants 6 messages Nov 25, 2025

Subsystems (4)

v3 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

v3 cover Introduce getsockname io_uring_cmd

Since V2:
- Comment fix (Kuniyuki)
- Move sockaddr_storage into callee (Kuniyuki)
- Add r-b tags
Since V1:
- minor style fixes
- Resend with (more) maintainers cc'ed
- rebased to axboe/for-next.
--

This feature has been requested a few times in the liburing repository and Discord channels, such as in [1,2]. If anything, it also helps solve a long standing issue in the bind-listen test that results in occasional test failures.

The patchset is divided in three parts: Patch 1 merges the getpeername and getsockname implementation in the network layer, making further patches easier; Patch 2 splits out a helper used by io_uring, like done for other network commands; Finally, patch 3 plumbs the new command in io_uring.

The syscall path was tested by booting a Linux distro, which does all sorts of getsockname/getpeername syscalls. The io_uring side was tested with a couple of new liburing subtests available at:

https://github.com/krisman/liburing.git -b socket

Based on top of Jens' for-next.

[1] https://github.com/axboe/liburing/issues/1356
[2] https://discord.com/channels/1241076672589991966/1241076672589991970/1429975797912830074

---
CC: [email protected]
CC: [email protected]
CC: Jakub Kicinski <[email protected]>
CC: David S. Miller <[email protected]>
CC: Eric Dumazet <[email protected]>
CC: Kuniyuki Iwashima <[email protected]>
CC: Paolo Abeni <[email protected]>
CC: Willem de Bruijn <[email protected]>
CC: Simon Horman <[email protected]>

Gabriel Krisman Bertazi (3):
socket: Unify getsockname and getpeername implementation
socket: Split out a getsockname helper for io_uring
io_uring: Introduce getsockname io_uring cmd

 include/linux/socket.h        |  6 ++--
 include/uapi/linux/io_uring.h |  1 +
 io_uring/cmd_net.c            | 22 ++++++++++++
 net/compat.c                  |  4 +--
 net/socket.c                  | 67 +++++++++++------------------------
 5 files changed, 49 insertions(+), 51 deletions(-)
Signature · 1 line
2.51.0
#1 lore raw

PATCH v3 1/3 socket: Unify getsockname and getpeername implementation

They are already implemented by the same get_name hook in the protocol level. Bring the unification one level up to reduce code duplication in preparation to supporting these as io_uring operations.

---
v2 to v3:
- Adjust comment (Kuniyuki)

---
 include/linux/socket.h |  4 +--
 net/compat.c           |  4 +--
 net/socket.c           | 55 ++++++++++--------------------------------
 3 files changed, 16 insertions(+), 47 deletions(-)
include/linux/socket.h +1 -3
index 3b262487ec06..937fe331ff1e 100644
@@ -454,9 +454,7 @@ extern int __sys_connect(int fd, struct sockaddr __user *uservaddr,
 extern int __sys_listen(int fd, int backlog);
 extern int __sys_listen_socket(struct socket *sock, int backlog);
 extern int __sys_getsockname(int fd, struct sockaddr __user *usockaddr,
-			     int __user *usockaddr_len);
-extern int __sys_getpeername(int fd, struct sockaddr __user *usockaddr,
-			     int __user *usockaddr_len);
+			     int __user *usockaddr_len, int peer);
 extern int __sys_socketpair(int family, int type, int protocol,
 			    int __user *usockvec);
 extern int __sys_shutdown_sock(struct socket *sock, int how);
net/compat.c +2 -2
index 485db8ee9b28..2c9bd0edac99 100644
@@ -460,10 +460,10 @@ COMPAT_SYSCALL_DEFINE2(socketcall, int, call, u32 __user *, args)
 		ret = __sys_accept4(a0, compat_ptr(a1), compat_ptr(a[2]), 0);
 		break;
 	case SYS_GETSOCKNAME:
-		ret = __sys_getsockname(a0, compat_ptr(a1), compat_ptr(a[2]));
+		ret = __sys_getsockname(a0, compat_ptr(a1), compat_ptr(a[2]), 0);
 		break;
 	case SYS_GETPEERNAME:
-		ret = __sys_getpeername(a0, compat_ptr(a1), compat_ptr(a[2]));
+		ret = __sys_getsockname(a0, compat_ptr(a1), compat_ptr(a[2]), 1);
 		break;
 	case SYS_SOCKETPAIR:
 		ret = __sys_socketpair(a0, a1, a[2], compat_ptr(a[3]));
net/socket.c +13 -42

92 lines not on this page · raw message

Signature · 1 line
2.51.0
#2 lore raw

PATCH v3 2/3 socket: Split out a getsockname helper for io_uring

In reply to Gabriel Krisman Bertazi

Similar to getsockopt, split out a helper to check security and issue the operation from the main handler that can be used by io_uring.

---
v2 -> v3:
- Move sockaddr_storage parameter into do_getsockname(Kuniyuki)

---
 include/linux/socket.h |  2 ++
 net/socket.c           | 36 ++++++++++++++++++++----------------
 2 files changed, 22 insertions(+), 16 deletions(-)
include/linux/socket.h +2 -0
index 937fe331ff1e..8d580074ddea 100644
@@ -453,6 +453,8 @@ extern int __sys_connect(int fd, struct sockaddr __user *uservaddr,
 			 int addrlen);
 extern int __sys_listen(int fd, int backlog);
 extern int __sys_listen_socket(struct socket *sock, int backlog);
+extern int do_getsockname(struct socket *sock, int peer,
+			  struct sockaddr __user *usockaddr, int __user *usockaddr_len);
 extern int __sys_getsockname(int fd, struct sockaddr __user *usockaddr,
 			     int __user *usockaddr_len, int peer);
 extern int __sys_socketpair(int family, int type, int protocol,
net/socket.c +20 -16
index 208d92ccf0fb..89bac0a17e5a 100644
@@ -2127,39 +2127,43 @@ SYSCALL_DEFINE3(connect, int, fd, struct sockaddr __user *, uservaddr,
 	return __sys_connect(fd, uservaddr, addrlen);
 }
 
-/*
- *	Get the remote or local address ('name') of a socket object. Move the
- *	obtained name to user space.
- */
-int __sys_getsockname(int fd, struct sockaddr __user *usockaddr,
-		      int __user *usockaddr_len, int peer)
+int do_getsockname(struct socket *sock, int peer,
+		   struct sockaddr __user *usockaddr, int __user *usockaddr_len)
 {
-	struct socket *sock;
 	struct sockaddr_storage address;
-	CLASS(fd, f)(fd);
 	int err;
 
-	if (fd_empty(f))
-		return -EBADF;
-	sock = sock_from_file(fd_file(f));
-	if (unlikely(!sock))
-		return -ENOTSOCK;
-
 	if (peer)
 		err = security_socket_getpeername(sock);
 	else
 		err = security_socket_getsockname(sock);
 	if (err)
 		return err;
-
 	err = READ_ONCE(sock->ops)->getname(sock, (struct sockaddr *)&address, peer);
 	if (err < 0)
 		return err;
-
 	/* "err" is actually length in this case */
 	return move_addr_to_user(&address, err, usockaddr, usockaddr_len);
 }
 
+/*
+ *	Get the remote or local address ('name') of a socket object. Move the
+ *	obtained name to user space.
+ */
+int __sys_getsockname(int fd, struct sockaddr __user *usockaddr,
+		      int __user *usockaddr_len, int peer)
+{
+	struct socket *sock;
+	CLASS(fd, f)(fd);
+
+	if (fd_empty(f))
+		return -EBADF;
+	sock = sock_from_file(fd_file(f));
+	if (unlikely(!sock))
+		return -ENOTSOCK;
+	return do_getsockname(sock, peer, usockaddr, usockaddr_len);
+}
+
 SYSCALL_DEFINE3(getsockname, int, fd, struct sockaddr __user *, usockaddr,
 		int __user *, usockaddr_len)
 {
Signature · 1 line
2.51.0
#3 lore raw

PATCH v3 3/3 io_uring: Introduce getsockname io_uring cmd

In reply to Gabriel Krisman Bertazi

Introduce a socket-specific io_uring_cmd to support getsockname/getpeername via io_uring. I made this an io_uring_cmd instead of a new operation to avoid polluting the command namespace with what is exclusively a socket operation. In addition, since we don't need to conform to existing interfaces, this merges the getsockname/getpeername in a single operation, since the implementation is pretty much the same.

This has been frequently requested, for instance at [1] and more recently in the project Discord channel. The main use-case is to support fixed socket file descriptors.

[1] https://github.com/axboe/liburing/issues/1356

---
v2->v3:
Don't pass sockaddr_storage pointer parameter

---
 include/uapi/linux/io_uring.h |  1 +
 io_uring/cmd_net.c            | 22 ++++++++++++++++++++++
 2 files changed, 23 insertions(+)
include/uapi/linux/io_uring.h +1 -0
index 3d921cbb84f8..6a97c5376019 100644
@@ -1010,6 +1010,7 @@ enum io_uring_socket_op {
 	SOCKET_URING_OP_GETSOCKOPT,
 	SOCKET_URING_OP_SETSOCKOPT,
 	SOCKET_URING_OP_TX_TIMESTAMP,
+	SOCKET_URING_OP_GETSOCKNAME,
 };
 
 /*
io_uring/cmd_net.c +22 -0
index 27a09aa4c9d0..a2d76157df4f 100644
@@ -132,6 +132,26 @@ static int io_uring_cmd_timestamp(struct socket *sock,
 	return -EAGAIN;
 }
 
+static int io_uring_cmd_getsockname(struct socket *sock,
+				    struct io_uring_cmd *cmd,
+				    unsigned int issue_flags)
+{
+	const struct io_uring_sqe *sqe = cmd->sqe;
+	struct sockaddr __user *uaddr;
+	unsigned int peer;
+	int __user *ulen;
+
+	if (sqe->ioprio || sqe->__pad1 || sqe->len || sqe->rw_flags)
+		return -EINVAL;
+
+	uaddr = u64_to_user_ptr(READ_ONCE(sqe->addr));
+	ulen = u64_to_user_ptr(sqe->addr3);
+	peer = READ_ONCE(sqe->optlen);
+	if (peer > 1)
+		return -EINVAL;
+	return do_getsockname(sock, 0, uaddr, ulen);
+}
+
 int io_uring_cmd_sock(struct io_uring_cmd *cmd, unsigned int issue_flags)
 {
 	struct socket *sock = cmd->file->private_data;
@@ -159,6 +179,8 @@ int io_uring_cmd_sock(struct io_uring_cmd *cmd, unsigned int issue_flags)
 		return io_uring_cmd_setsockopt(sock, cmd, issue_flags);
 	case SOCKET_URING_OP_TX_TIMESTAMP:
 		return io_uring_cmd_timestamp(sock, cmd, issue_flags);
+	case SOCKET_URING_OP_GETSOCKNAME:
+		return io_uring_cmd_getsockname(sock, cmd, issue_flags);
 	default:
 		return -EOPNOTSUPP;
 	}
Signature · 1 line
2.51.0
#4 lore raw

Re: [PATCH v3 3/3] io_uring: Introduce getsockname io_uring cmd

Hi Gabriel,

Quoted 8 lines
io_uring/cmd_net.c +8 -0
+static int io_uring_cmd_getsockname(struct socket *sock,
+				    struct io_uring_cmd *cmd,
+				    unsigned int issue_flags)
+{
+	const struct io_uring_sqe *sqe = cmd->sqe;
+	struct sockaddr __user *uaddr;
+	unsigned int peer;
+	int __user *ulen;

io_uring/cmd_net.c static int io_uring_cmd_timestamp(struct socket *sock, · line 152

Quoted 10 lines
io_uring/cmd_net.c +10 -0
+
+	if (sqe->ioprio || sqe->__pad1 || sqe->len || sqe->rw_flags)
+		return -EINVAL;
+
+	uaddr = u64_to_user_ptr(READ_ONCE(sqe->addr));
+	ulen = u64_to_user_ptr(sqe->addr3);
+	peer = READ_ONCE(sqe->optlen);
+	if (peer > 1)
+		return -EINVAL;
+	return do_getsockname(sock, 0, uaddr, ulen);

I guess this should actually pass down 'peer' instead of '0'?

metze

#5 lore raw

Re: [PATCH v3 3/3] io_uring: Introduce getsockname io_uring cmd

Quoted 14 lines · Stefan Metzmacher

Hi Gabriel,

Quoted 12 lines
io_uring/cmd_net.c +12 -0
+static int io_uring_cmd_getsockname(struct socket *sock,
+				    struct io_uring_cmd *cmd,
+				    unsigned int issue_flags)
+{
+	const struct io_uring_sqe *sqe = cmd->sqe;
+	struct sockaddr __user *uaddr;
+	unsigned int peer;
+	int __user *ulen;
+
+	if (sqe->ioprio || sqe->__pad1 || sqe->len || sqe->rw_flags)
+		return -EINVAL;
+
Quoted 8 lines

io_uring/cmd_net.c static int io_uring_cmd_timestamp(struct socket *sock, · line 152

Quoted 6 lines
io_uring/cmd_net.c +6 -0
+	uaddr = u64_to_user_ptr(READ_ONCE(sqe->addr));
+	ulen = u64_to_user_ptr(sqe->addr3);
+	peer = READ_ONCE(sqe->optlen);
+	if (peer > 1)
+		return -EINVAL;
+	return do_getsockname(sock, 0, uaddr, ulen);

I guess this should actually pass down 'peer' instead of '0'?

Thanks for the catch. I guess two wrongs *do* make a right somethings. The getpeername test was peeking at the wrong socket, effectively testing the local port against itself, and it thus succeeded. Updated the test and will send a v4.

Signature · 1 line
Gabriel Krisman Bertazi