[RFC PATCH v1 04/10] net: qrtr: Report sender endpoint in aux data

Denis Kenzior posted 10 patches 1 month, 1 week ago
[RFC PATCH v1 04/10] net: qrtr: Report sender endpoint in aux data
Posted by Denis Kenzior 1 month, 1 week ago
Introduce support for reporting the remote endpoint that generated a
given QRTR message to clients using AF_QIPCRTR family sockets. This is
achieved by including QRTR_ENDPOINT auxiliary data, which carries the
endpoint identifier of the message sender.  To receive this auxiliary
data, clients must explicitly opt-in by using setsockopt with the
QRTR_REPORT_ENDPOINT option enabled.

Implementation of getsockopt and setsockopt is provided.  An additional
level 'SOL_QRTR' is added to socket.h for use by AF_QIPCRTR family
sockets.

Signed-off-by: Denis Kenzior <denkenz@gmail.com>
Reviewed-by: Marcel Holtmann <marcel@holtmann.org>
Reviewed-by: Andy Gross <agross@kernel.org>
---
 include/linux/socket.h    |  1 +
 include/uapi/linux/qrtr.h |  6 +++
 net/qrtr/af_qrtr.c        | 87 +++++++++++++++++++++++++++++++++++++++
 3 files changed, 94 insertions(+)

diff --git a/include/linux/socket.h b/include/linux/socket.h
index d18cc47e89bd..7491884340cf 100644
--- a/include/linux/socket.h
+++ b/include/linux/socket.h
@@ -386,6 +386,7 @@ struct ucred {
 #define SOL_MCTP	285
 #define SOL_SMC		286
 #define SOL_VSOCK	287
+#define SOL_QRTR	288
 
 /* IPX options */
 #define IPX_TYPE	1
diff --git a/include/uapi/linux/qrtr.h b/include/uapi/linux/qrtr.h
index f7e2fb3d752b..6d0911984a05 100644
--- a/include/uapi/linux/qrtr.h
+++ b/include/uapi/linux/qrtr.h
@@ -46,4 +46,10 @@ struct qrtr_ctrl_pkt {
 	};
 } __packed;
 
+/* setsockopt / getsockopt */
+#define QRTR_REPORT_ENDPOINT 1
+
+/* CMSG */
+#define QRTR_ENDPOINT 1
+
 #endif /* _LINUX_QRTR_H */
diff --git a/net/qrtr/af_qrtr.c b/net/qrtr/af_qrtr.c
index e83d491a8da9..cb7bd1c71e6d 100644
--- a/net/qrtr/af_qrtr.c
+++ b/net/qrtr/af_qrtr.c
@@ -26,6 +26,10 @@
 
 #define QRTR_PORT_CTRL_LEGACY 0xffff
 
+enum {
+	QRTR_F_REPORT_ENDPOINT,
+};
+
 /**
  * struct qrtr_hdr_v1 - (I|R)PCrouter packet header version 1
  * @version: protocol version
@@ -79,6 +83,7 @@ struct qrtr_cb {
 	u32 src_port;
 	u32 dst_node;
 	u32 dst_port;
+	u32 endpoint_id;
 
 	u8 type;
 	u8 confirm_rx;
@@ -92,6 +97,7 @@ struct qrtr_sock {
 	struct sock sk;
 	struct sockaddr_qrtr us;
 	struct sockaddr_qrtr peer;
+	unsigned long flags;
 };
 
 static inline struct qrtr_sock *qrtr_sk(struct sock *sk)
@@ -513,6 +519,8 @@ int qrtr_endpoint_post(struct qrtr_endpoint *ep, const void *data, size_t len)
 	if (cb->dst_port == QRTR_PORT_CTRL_LEGACY)
 		cb->dst_port = QRTR_PORT_CTRL;
 
+	cb->endpoint_id = ep->id;
+
 	if (!size || len != ALIGN(size, 4) + hdrlen)
 		goto err;
 
@@ -1064,6 +1072,7 @@ static int qrtr_recvmsg(struct socket *sock, struct msghdr *msg,
 			size_t size, int flags)
 {
 	DECLARE_SOCKADDR(struct sockaddr_qrtr *, addr, msg->msg_name);
+	struct qrtr_sock *ipc = qrtr_sk(sock->sk);
 	struct sock *sk = sock->sk;
 	struct sk_buff *skb;
 	struct qrtr_cb *cb;
@@ -1089,6 +1098,10 @@ static int qrtr_recvmsg(struct socket *sock, struct msghdr *msg,
 		msg->msg_flags |= MSG_TRUNC;
 	}
 
+	if (cb->endpoint_id && test_bit(QRTR_F_REPORT_ENDPOINT, &ipc->flags))
+		put_cmsg(msg, SOL_QRTR, QRTR_ENDPOINT,
+			 sizeof(cb->endpoint_id), &cb->endpoint_id);
+
 	rc = skb_copy_datagram_msg(skb, 0, msg, copied);
 	if (rc < 0)
 		goto out;
@@ -1234,6 +1247,78 @@ static int qrtr_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
 	return rc;
 }
 
+static int qrtr_setsockopt(struct socket *sock, int level, int optname,
+			   sockptr_t optval, unsigned int optlen)
+{
+	struct qrtr_sock *ipc = qrtr_sk(sock->sk);
+	struct sock *sk = sock->sk;
+	unsigned int val = 0;
+	int rc = 0;
+
+	if (level != SOL_QRTR)
+		return -ENOPROTOOPT;
+
+	if (optlen >= sizeof(val) &&
+	    copy_from_sockptr(&val, optval, sizeof(val)))
+		return -EFAULT;
+
+	lock_sock(sk);
+
+	switch (optname) {
+	case QRTR_REPORT_ENDPOINT:
+		assign_bit(QRTR_F_REPORT_ENDPOINT, &ipc->flags, val);
+		break;
+	default:
+		rc = -ENOPROTOOPT;
+	}
+
+	release_sock(sk);
+
+	return rc;
+}
+
+static int qrtr_getsockopt(struct socket *sock, int level, int optname,
+			   char __user *optval, int __user *optlen)
+{
+	struct qrtr_sock *ipc = qrtr_sk(sock->sk);
+	struct sock *sk = sock->sk;
+	unsigned int val;
+	int len;
+	int rc = 0;
+
+	if (level != SOL_QRTR)
+		return -ENOPROTOOPT;
+
+	if (get_user(len, optlen))
+		return -EFAULT;
+
+	if (len < sizeof(val))
+		return -EINVAL;
+
+	lock_sock(sk);
+
+	switch (optname) {
+	case QRTR_REPORT_ENDPOINT:
+		val = test_bit(QRTR_F_REPORT_ENDPOINT, &ipc->flags);
+		break;
+	default:
+		rc = -ENOPROTOOPT;
+	}
+
+	release_sock(sk);
+
+	if (rc)
+		return rc;
+
+	len = sizeof(int);
+
+	if (put_user(len, optlen) ||
+	    copy_to_user(optval, &val, len))
+		rc = -EFAULT;
+
+	return rc;
+}
+
 static int qrtr_release(struct socket *sock)
 {
 	struct sock *sk = sock->sk;
@@ -1281,6 +1366,8 @@ static const struct proto_ops qrtr_proto_ops = {
 	.shutdown	= sock_no_shutdown,
 	.release	= qrtr_release,
 	.mmap		= sock_no_mmap,
+	.setsockopt	= qrtr_setsockopt,
+	.getsockopt	= qrtr_getsockopt,
 };
 
 static struct proto qrtr_proto = {
-- 
2.45.2
Re: [RFC PATCH v1 04/10] net: qrtr: Report sender endpoint in aux data
Posted by Kuniyuki Iwashima 1 month, 1 week ago
From: Denis Kenzior <denkenz@gmail.com>
Date: Fri, 18 Oct 2024 13:18:22 -0500
> @@ -1234,6 +1247,78 @@ static int qrtr_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
>  	return rc;
>  }
>  
> +static int qrtr_setsockopt(struct socket *sock, int level, int optname,
> +			   sockptr_t optval, unsigned int optlen)
> +{
> +	struct qrtr_sock *ipc = qrtr_sk(sock->sk);
> +	struct sock *sk = sock->sk;
> +	unsigned int val = 0;
> +	int rc = 0;
> +
> +	if (level != SOL_QRTR)
> +		return -ENOPROTOOPT;
> +
> +	if (optlen >= sizeof(val) &&
> +	    copy_from_sockptr(&val, optval, sizeof(val)))
> +		return -EFAULT;
> +
> +	lock_sock(sk);

This seems unnecessary to me.

sk_setsockopt(), do_ip_setsockopt(), and do_ipv6_setsockopt() do not
hold lock_sock() for assign_bit().

Also, QRTR_BIND_ENDPOINT in a later patch will not need lock_sock()
neither.  The value is u32, so you can use WRITE_ONCE() here and
READ_ONCE() in getsockopt().


> +
> +	switch (optname) {
> +	case QRTR_REPORT_ENDPOINT:
> +		assign_bit(QRTR_F_REPORT_ENDPOINT, &ipc->flags, val);
> +		break;
> +	default:
> +		rc = -ENOPROTOOPT;
> +	}
> +
> +	release_sock(sk);
> +
> +	return rc;
> +}
> +
> +static int qrtr_getsockopt(struct socket *sock, int level, int optname,
> +			   char __user *optval, int __user *optlen)
> +{
> +	struct qrtr_sock *ipc = qrtr_sk(sock->sk);
> +	struct sock *sk = sock->sk;
> +	unsigned int val;
> +	int len;
> +	int rc = 0;
> +
> +	if (level != SOL_QRTR)
> +		return -ENOPROTOOPT;
> +
> +	if (get_user(len, optlen))
> +		return -EFAULT;
> +
> +	if (len < sizeof(val))
> +		return -EINVAL;
> +
> +	lock_sock(sk);

Same remark.


> +
> +	switch (optname) {
> +	case QRTR_REPORT_ENDPOINT:
> +		val = test_bit(QRTR_F_REPORT_ENDPOINT, &ipc->flags);
> +		break;
> +	default:
> +		rc = -ENOPROTOOPT;
> +	}
> +
> +	release_sock(sk);
> +
> +	if (rc)
> +		return rc;
> +
> +	len = sizeof(int);
> +
> +	if (put_user(len, optlen) ||
> +	    copy_to_user(optval, &val, len))
> +		rc = -EFAULT;
> +
> +	return rc;
> +}
> +
>  static int qrtr_release(struct socket *sock)
>  {
>  	struct sock *sk = sock->sk;
Re: [RFC PATCH v1 04/10] net: qrtr: Report sender endpoint in aux data
Posted by Denis Kenzior 1 month ago
On 10/18/24 7:22 PM, Kuniyuki Iwashima wrote:
> From: Denis Kenzior <denkenz@gmail.com>
> Date: Fri, 18 Oct 2024 13:18:22 -0500
>> @@ -1234,6 +1247,78 @@ static int qrtr_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
>>   	return rc;
>>   }
>>   
>> +static int qrtr_setsockopt(struct socket *sock, int level, int optname,
>> +			   sockptr_t optval, unsigned int optlen)
>> +{
>> +	struct qrtr_sock *ipc = qrtr_sk(sock->sk);
>> +	struct sock *sk = sock->sk;
>> +	unsigned int val = 0;
>> +	int rc = 0;
>> +
>> +	if (level != SOL_QRTR)
>> +		return -ENOPROTOOPT;
>> +
>> +	if (optlen >= sizeof(val) &&
>> +	    copy_from_sockptr(&val, optval, sizeof(val)))
>> +		return -EFAULT;
>> +
>> +	lock_sock(sk);
> 
> This seems unnecessary to me.
> 
> sk_setsockopt(), do_ip_setsockopt(), and do_ipv6_setsockopt() do not
> hold lock_sock() for assign_bit().

Indeed, thanks for spotting that.  I'll fix this in the next version.  I'll also 
drop lock_sock/release_sock in qrtr_sock_set_report_endpoint (patch 9).

> 
> Also, QRTR_BIND_ENDPOINT in a later patch will not need lock_sock()
> neither.  The value is u32, so you can use WRITE_ONCE() here and
> READ_ONCE() in getsockopt().
> 

Makes sense, I'll fix this as well.

> 
>> +
>> +	switch (optname) {
>> +	case QRTR_REPORT_ENDPOINT:
>> +		assign_bit(QRTR_F_REPORT_ENDPOINT, &ipc->flags, val);
>> +		break;
>> +	default:
>> +		rc = -ENOPROTOOPT;
>> +	}
>> +
>> +	release_sock(sk);
>> +
>> +	return rc;
>> +}
>> +
>> +static int qrtr_getsockopt(struct socket *sock, int level, int optname,
>> +			   char __user *optval, int __user *optlen)
>> +{
>> +	struct qrtr_sock *ipc = qrtr_sk(sock->sk);
>> +	struct sock *sk = sock->sk;
>> +	unsigned int val;
>> +	int len;
>> +	int rc = 0;
>> +
>> +	if (level != SOL_QRTR)
>> +		return -ENOPROTOOPT;
>> +
>> +	if (get_user(len, optlen))
>> +		return -EFAULT;
>> +
>> +	if (len < sizeof(val))
>> +		return -EINVAL;
>> +
>> +	lock_sock(sk);
> 
> Same remark.
> 
> 
>> +
>> +	switch (optname) {
>> +	case QRTR_REPORT_ENDPOINT:
>> +		val = test_bit(QRTR_F_REPORT_ENDPOINT, &ipc->flags);
>> +		break;
>> +	default:
>> +		rc = -ENOPROTOOPT;
>> +	}
>> +
>> +	release_sock(sk);
>> +
>> +	if (rc)
>> +		return rc;
>> +
>> +	len = sizeof(int);
>> +
>> +	if (put_user(len, optlen) ||
>> +	    copy_to_user(optval, &val, len))
>> +		rc = -EFAULT;
>> +
>> +	return rc;
>> +}
>> +
>>   static int qrtr_release(struct socket *sock)
>>   {
>>   	struct sock *sk = sock->sk;