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

Denis Kenzior posted 10 patches 1 month, 1 week ago
[RFC PATCH v1 06/10] net: qrtr: Allow sendmsg to target an endpoint
Posted by Denis Kenzior 1 month, 1 week ago
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;
+
+	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
-- 
2.45.2
Re: [RFC PATCH v1 06/10] net: qrtr: Allow sendmsg to target an endpoint
Posted by Chris Lew 1 month ago

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
Re: [RFC PATCH v1 06/10] net: qrtr: Allow sendmsg to target an endpoint
Posted by Denis Kenzior 1 month ago
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