diff mbox series

[RFC,v1,06/10] net: qrtr: Allow sendmsg to target an endpoint

Message ID 20241018181842.1368394-7-denkenz@gmail.com (mailing list archive)
State RFC
Delegated to: Netdev Maintainers
Headers show
Series QRTR Multi-endpoint support | expand

Checks

Context Check Description
netdev/series_format warning Target tree name not specified in the subject
netdev/tree_selection success Guessed tree name to be net-next, async
netdev/ynl success Generated files up to date; no warnings/errors; no diff in generated;
netdev/fixes_present success Fixes tag not required for -next series
netdev/header_inline success No static functions without inline keyword in header files
netdev/build_32bit fail Errors and warnings before: 6 this patch: 7
netdev/build_tools success No tools touched, skip
netdev/cc_maintainers success CCed 6 of 6 maintainers
netdev/build_clang success Errors and warnings before: 3 this patch: 3
netdev/verify_signedoff success Signed-off-by tag matches author and committer
netdev/deprecated_api success None detected
netdev/check_selftest success No net selftest shell script
netdev/verify_fixes success No Fixes tag
netdev/build_allmodconfig_warn success Errors and warnings before: 7 this patch: 7
netdev/checkpatch success total: 0 errors, 0 warnings, 0 checks, 149 lines checked
netdev/build_clang_rust success No Rust files in patch. Skipping build
netdev/kdoc success Errors and warnings before: 1 this patch: 1
netdev/source_inline success Was 0 now: 0

Commit Message

Denis Kenzior Oct. 18, 2024, 6:18 p.m. UTC
Allow QIPCRTR family sockets to include QRTR_ENDPOINT auxiliary data
as part of the sendmsg system call.  By including this parameter, the
client can ask the kernel to route the message to a given endpoint, in
situations where multiple endpoints with conflicting node identifier
sets exist in the system.

For legacy clients, or clients that do not include QRTR_ENDPOINT data,
the endpoint is looked up, as before, by only using the node identifier
of the destination qrtr socket address.

Signed-off-by: Denis Kenzior <denkenz@gmail.com>
Reviewed-by: Marcel Holtmann <marcel@holtmann.org>
Reviewed-by: Andy Gross <agross@kernel.org>
---
 net/qrtr/af_qrtr.c | 80 +++++++++++++++++++++++++++++++++-------------
 net/qrtr/qrtr.h    |  2 ++
 2 files changed, 60 insertions(+), 22 deletions(-)

Comments

Chris Lew Oct. 22, 2024, 11:58 p.m. UTC | #1
On 10/18/2024 11:18 AM, Denis Kenzior wrote:
> Allow QIPCRTR family sockets to include QRTR_ENDPOINT auxiliary data
> as part of the sendmsg system call.  By including this parameter, the
> client can ask the kernel to route the message to a given endpoint, in
> situations where multiple endpoints with conflicting node identifier
> sets exist in the system.
> 
> For legacy clients, or clients that do not include QRTR_ENDPOINT data,
> the endpoint is looked up, as before, by only using the node identifier
> of the destination qrtr socket address.
> 
> Signed-off-by: Denis Kenzior <denkenz@gmail.com>
> Reviewed-by: Marcel Holtmann <marcel@holtmann.org>
> Reviewed-by: Andy Gross <agross@kernel.org>
> ---
>   net/qrtr/af_qrtr.c | 80 +++++++++++++++++++++++++++++++++-------------
>   net/qrtr/qrtr.h    |  2 ++
>   2 files changed, 60 insertions(+), 22 deletions(-)
> 
> diff --git a/net/qrtr/af_qrtr.c b/net/qrtr/af_qrtr.c
> index 568ccb1d8574..23749a0b0c15 100644
> --- a/net/qrtr/af_qrtr.c
> +++ b/net/qrtr/af_qrtr.c
> @@ -106,6 +106,36 @@ static inline struct qrtr_sock *qrtr_sk(struct sock *sk)
>   	return container_of(sk, struct qrtr_sock, sk);
>   }
>   
> +int qrtr_msg_get_endpoint(struct msghdr *msg, u32 *out_endpoint_id)
> +{
> +	struct cmsghdr *cmsg;
> +	u32 endpoint_id = 0;
> +
> +	for_each_cmsghdr(cmsg, msg) {
> +		if (!CMSG_OK(msg, cmsg))
> +			return -EINVAL;
> +
> +		if (cmsg->cmsg_level != SOL_QRTR)
> +			continue;
> +
> +		if (cmsg->cmsg_type != QRTR_ENDPOINT)
> +			return -EINVAL;
> +
> +		if (cmsg->cmsg_len < CMSG_LEN(sizeof(u32)))
> +			return -EINVAL;
> +
> +		/* Endpoint ids start at 1 */
> +		endpoint_id = *(u32 *)CMSG_DATA(cmsg);
> +		if (!endpoint_id)
> +			return -EINVAL;
> +	}
> +
> +	if (out_endpoint_id)
> +		*out_endpoint_id = endpoint_id;

In the case when there is no cmsg attached to the msg. Would it be safer 
to assign out_endpoint_id to 0 before returning?

I see that in qrtr_sendmsg() there is a risk of using msg_endpoint_id 
without it being initialized or assigned a value in this function.

> +
> +	return 0;
> +}
> +
>   static unsigned int qrtr_local_nid = 1;
>   
>   /* for node ids */
> @@ -404,14 +434,16 @@ static int qrtr_node_enqueue(struct qrtr_node *node, struct sk_buff *skb,
>    *
>    * callers must release with qrtr_node_release()
>    */
> -static struct qrtr_node *qrtr_node_lookup(unsigned int nid)
> +static struct qrtr_node *qrtr_node_lookup(unsigned int endpoint_id,
> +					  unsigned int nid)
>   {
>   	struct qrtr_node *node;
>   	unsigned long flags;
> +	unsigned long key = (unsigned long)endpoint_id << 32 | nid;
>   
>   	mutex_lock(&qrtr_node_lock);
>   	spin_lock_irqsave(&qrtr_nodes_lock, flags);
> -	node = radix_tree_lookup(&qrtr_nodes, nid);
> +	node = radix_tree_lookup(&qrtr_nodes, key);
>   	node = qrtr_node_acquire(node);
>   	spin_unlock_irqrestore(&qrtr_nodes_lock, flags);
>   	mutex_unlock(&qrtr_node_lock);
> @@ -953,6 +985,7 @@ static int qrtr_sendmsg(struct socket *sock, struct msghdr *msg, size_t len)
>   	struct qrtr_sock *ipc = qrtr_sk(sock->sk);
>   	struct sock *sk = sock->sk;
>   	struct qrtr_node *node;
> +	u32 msg_endpoint_id;
>   	u32 endpoint_id = qrtr_local_nid;
>   	struct sk_buff *skb;
>   	size_t plen;
> @@ -965,46 +998,48 @@ static int qrtr_sendmsg(struct socket *sock, struct msghdr *msg, size_t len)
>   	if (len > 65535)
>   		return -EMSGSIZE;
>   
> +	rc = qrtr_msg_get_endpoint(msg, &msg_endpoint_id);
> +	if (rc < 0)
> +		return rc;
> +
>   	lock_sock(sk);
>   
>   	if (addr) {
> -		if (msg->msg_namelen < sizeof(*addr)) {
> -			release_sock(sk);
> -			return -EINVAL;
> -		}
> +		rc = -EINVAL;
>   
> -		if (addr->sq_family != AF_QIPCRTR) {
> -			release_sock(sk);
> -			return -EINVAL;
> -		}
> +		if (msg->msg_namelen < sizeof(*addr))
> +			goto release_sock;
> +
> +		if (addr->sq_family != AF_QIPCRTR)
> +			goto release_sock;
>   
>   		rc = qrtr_autobind(sock);
> -		if (rc) {
> -			release_sock(sk);
> -			return rc;
> -		}
> +		if (rc)
> +			goto release_sock;
>   	} else if (sk->sk_state == TCP_ESTABLISHED) {
>   		addr = &ipc->peer;
>   	} else {
> -		release_sock(sk);
> -		return -ENOTCONN;
> +		rc = -ENOTCONN;
> +		goto release_sock;
>   	}
>   
>   	node = NULL;
>   	if (addr->sq_node == QRTR_NODE_BCAST) {
>   		if (addr->sq_port != QRTR_PORT_CTRL &&
>   		    qrtr_local_nid != QRTR_NODE_BCAST) {
> -			release_sock(sk);
> -			return -ENOTCONN;
> +			rc = -ENOTCONN;
> +			goto release_sock;
>   		}
>   		enqueue_fn = qrtr_bcast_enqueue;
>   	} else if (addr->sq_node == ipc->us.sq_node) {
>   		enqueue_fn = qrtr_local_enqueue;
>   	} else {
> -		node = qrtr_node_lookup(addr->sq_node);
> +		endpoint_id = msg_endpoint_id;
> +
> +		node = qrtr_node_lookup(endpoint_id, addr->sq_node);
>   		if (!node) {
> -			release_sock(sk);
> -			return -ECONNRESET;
> +			rc = endpoint_id ? -ENXIO : -ECONNRESET;
> +			goto release_sock;
>   		}
>   		enqueue_fn = qrtr_node_enqueue;
>   	}
> @@ -1043,6 +1078,7 @@ static int qrtr_sendmsg(struct socket *sock, struct msghdr *msg, size_t len)
>   
>   out_node:
>   	qrtr_node_release(node);
> +release_sock:
>   	release_sock(sk);
>   
>   	return rc;
> @@ -1057,7 +1093,7 @@ static int qrtr_send_resume_tx(struct qrtr_cb *cb)
>   	struct sk_buff *skb;
>   	int ret;
>   
> -	node = qrtr_node_lookup(remote.sq_node);
> +	node = qrtr_node_lookup(cb->endpoint_id, remote.sq_node);
>   	if (!node)
>   		return -EINVAL;
>   
> diff --git a/net/qrtr/qrtr.h b/net/qrtr/qrtr.h
> index 11b897af05e6..22fcecbf8de2 100644
> --- a/net/qrtr/qrtr.h
> +++ b/net/qrtr/qrtr.h
> @@ -34,4 +34,6 @@ int qrtr_ns_init(void);
>   
>   void qrtr_ns_remove(void);
>   
> +int qrtr_msg_get_endpoint(struct msghdr *msg, u32 *out_endpoint_id);
> +
>   #endif
Denis Kenzior Oct. 24, 2024, 5:40 p.m. UTC | #2
Hi Chris,

>> @@ -106,6 +106,36 @@ static inline struct qrtr_sock *qrtr_sk(struct sock *sk)
>>       return container_of(sk, struct qrtr_sock, sk);
>>   }
>> +int qrtr_msg_get_endpoint(struct msghdr *msg, u32 *out_endpoint_id)
>> +{
>> +    struct cmsghdr *cmsg;
>> +    u32 endpoint_id = 0;
>> +
>> +    for_each_cmsghdr(cmsg, msg) {
>> +        if (!CMSG_OK(msg, cmsg))
>> +            return -EINVAL;
>> +
>> +        if (cmsg->cmsg_level != SOL_QRTR)
>> +            continue;
>> +
>> +        if (cmsg->cmsg_type != QRTR_ENDPOINT)
>> +            return -EINVAL;
>> +
>> +        if (cmsg->cmsg_len < CMSG_LEN(sizeof(u32)))
>> +            return -EINVAL;
>> +
>> +        /* Endpoint ids start at 1 */
>> +        endpoint_id = *(u32 *)CMSG_DATA(cmsg);
>> +        if (!endpoint_id)
>> +            return -EINVAL;
>> +    }
>> +
>> +    if (out_endpoint_id)
>> +        *out_endpoint_id = endpoint_id;
> 
> In the case when there is no cmsg attached to the msg. Would it be safer to 
> assign out_endpoint_id to 0 before returning?

Hmm, isn't that what happens?  endpoint_id is initialized to 0 in the 
declaration block, so if no cmsg headers are present, out_endpoint_id will get a 
0 assigned.

> 
> I see that in qrtr_sendmsg() there is a risk of using msg_endpoint_id without it 
> being initialized or assigned a value in this function.

Calling this function in qrtr_sendmsg() should always assign msg_endpoint_id 
unless an error occurred.

Regards,
-Denis
diff mbox series

Patch

diff --git a/net/qrtr/af_qrtr.c b/net/qrtr/af_qrtr.c
index 568ccb1d8574..23749a0b0c15 100644
--- a/net/qrtr/af_qrtr.c
+++ b/net/qrtr/af_qrtr.c
@@ -106,6 +106,36 @@  static inline struct qrtr_sock *qrtr_sk(struct sock *sk)
 	return container_of(sk, struct qrtr_sock, sk);
 }
 
+int qrtr_msg_get_endpoint(struct msghdr *msg, u32 *out_endpoint_id)
+{
+	struct cmsghdr *cmsg;
+	u32 endpoint_id = 0;
+
+	for_each_cmsghdr(cmsg, msg) {
+		if (!CMSG_OK(msg, cmsg))
+			return -EINVAL;
+
+		if (cmsg->cmsg_level != SOL_QRTR)
+			continue;
+
+		if (cmsg->cmsg_type != QRTR_ENDPOINT)
+			return -EINVAL;
+
+		if (cmsg->cmsg_len < CMSG_LEN(sizeof(u32)))
+			return -EINVAL;
+
+		/* Endpoint ids start at 1 */
+		endpoint_id = *(u32 *)CMSG_DATA(cmsg);
+		if (!endpoint_id)
+			return -EINVAL;
+	}
+
+	if (out_endpoint_id)
+		*out_endpoint_id = endpoint_id;
+
+	return 0;
+}
+
 static unsigned int qrtr_local_nid = 1;
 
 /* for node ids */
@@ -404,14 +434,16 @@  static int qrtr_node_enqueue(struct qrtr_node *node, struct sk_buff *skb,
  *
  * callers must release with qrtr_node_release()
  */
-static struct qrtr_node *qrtr_node_lookup(unsigned int nid)
+static struct qrtr_node *qrtr_node_lookup(unsigned int endpoint_id,
+					  unsigned int nid)
 {
 	struct qrtr_node *node;
 	unsigned long flags;
+	unsigned long key = (unsigned long)endpoint_id << 32 | nid;
 
 	mutex_lock(&qrtr_node_lock);
 	spin_lock_irqsave(&qrtr_nodes_lock, flags);
-	node = radix_tree_lookup(&qrtr_nodes, nid);
+	node = radix_tree_lookup(&qrtr_nodes, key);
 	node = qrtr_node_acquire(node);
 	spin_unlock_irqrestore(&qrtr_nodes_lock, flags);
 	mutex_unlock(&qrtr_node_lock);
@@ -953,6 +985,7 @@  static int qrtr_sendmsg(struct socket *sock, struct msghdr *msg, size_t len)
 	struct qrtr_sock *ipc = qrtr_sk(sock->sk);
 	struct sock *sk = sock->sk;
 	struct qrtr_node *node;
+	u32 msg_endpoint_id;
 	u32 endpoint_id = qrtr_local_nid;
 	struct sk_buff *skb;
 	size_t plen;
@@ -965,46 +998,48 @@  static int qrtr_sendmsg(struct socket *sock, struct msghdr *msg, size_t len)
 	if (len > 65535)
 		return -EMSGSIZE;
 
+	rc = qrtr_msg_get_endpoint(msg, &msg_endpoint_id);
+	if (rc < 0)
+		return rc;
+
 	lock_sock(sk);
 
 	if (addr) {
-		if (msg->msg_namelen < sizeof(*addr)) {
-			release_sock(sk);
-			return -EINVAL;
-		}
+		rc = -EINVAL;
 
-		if (addr->sq_family != AF_QIPCRTR) {
-			release_sock(sk);
-			return -EINVAL;
-		}
+		if (msg->msg_namelen < sizeof(*addr))
+			goto release_sock;
+
+		if (addr->sq_family != AF_QIPCRTR)
+			goto release_sock;
 
 		rc = qrtr_autobind(sock);
-		if (rc) {
-			release_sock(sk);
-			return rc;
-		}
+		if (rc)
+			goto release_sock;
 	} else if (sk->sk_state == TCP_ESTABLISHED) {
 		addr = &ipc->peer;
 	} else {
-		release_sock(sk);
-		return -ENOTCONN;
+		rc = -ENOTCONN;
+		goto release_sock;
 	}
 
 	node = NULL;
 	if (addr->sq_node == QRTR_NODE_BCAST) {
 		if (addr->sq_port != QRTR_PORT_CTRL &&
 		    qrtr_local_nid != QRTR_NODE_BCAST) {
-			release_sock(sk);
-			return -ENOTCONN;
+			rc = -ENOTCONN;
+			goto release_sock;
 		}
 		enqueue_fn = qrtr_bcast_enqueue;
 	} else if (addr->sq_node == ipc->us.sq_node) {
 		enqueue_fn = qrtr_local_enqueue;
 	} else {
-		node = qrtr_node_lookup(addr->sq_node);
+		endpoint_id = msg_endpoint_id;
+
+		node = qrtr_node_lookup(endpoint_id, addr->sq_node);
 		if (!node) {
-			release_sock(sk);
-			return -ECONNRESET;
+			rc = endpoint_id ? -ENXIO : -ECONNRESET;
+			goto release_sock;
 		}
 		enqueue_fn = qrtr_node_enqueue;
 	}
@@ -1043,6 +1078,7 @@  static int qrtr_sendmsg(struct socket *sock, struct msghdr *msg, size_t len)
 
 out_node:
 	qrtr_node_release(node);
+release_sock:
 	release_sock(sk);
 
 	return rc;
@@ -1057,7 +1093,7 @@  static int qrtr_send_resume_tx(struct qrtr_cb *cb)
 	struct sk_buff *skb;
 	int ret;
 
-	node = qrtr_node_lookup(remote.sq_node);
+	node = qrtr_node_lookup(cb->endpoint_id, remote.sq_node);
 	if (!node)
 		return -EINVAL;
 
diff --git a/net/qrtr/qrtr.h b/net/qrtr/qrtr.h
index 11b897af05e6..22fcecbf8de2 100644
--- a/net/qrtr/qrtr.h
+++ b/net/qrtr/qrtr.h
@@ -34,4 +34,6 @@  int qrtr_ns_init(void);
 
 void qrtr_ns_remove(void);
 
+int qrtr_msg_get_endpoint(struct msghdr *msg, u32 *out_endpoint_id);
+
 #endif