| Seq | Timestamp | Type | Name | Duration |
|---|---|---|---|---|
| 0/0 | 2026/09/02 03:18 | flow | patch-triage |
2mArgs:null Results: {
"EnableConfigs": [
"QRTR",
"QRTR_TUN",
"QRTR_MHI"
],
"FocusSymbols": [
"qrtr_setsockopt",
"qrtr_getsockopt",
"qrtr_sendmsg",
"qrtr_recvmsg",
"qrtr_msg_get_endpoint",
"qrtr_sock_set_report_endpoint",
"qrtr_endpoint_post",
"qrtr_endpoint_register",
"qrtr_endpoint_unregister"
],
"KMSANReasoning": "The patch series introduces endpoint-aware routing and filtering for Qualcomm IPC Router (QRTR) sockets, adding new setsockopt/getsockopt options (QRTR_REPORT_ENDPOINT, QRTR_BIND_ENDPOINT), control messages (QRTR_ENDPOINT), and multi-endpoint radix tree/xarray lookups.\n\nAnalysis of uninitialized memory and info-leak risks:\n1. Kernel-to-user copying in getsockopt (QRTR_REPORT_ENDPOINT, QRTR_BIND_ENDPOINT): 'val' is an unsigned int explicitly initialized in each supported switch branch before calling copy_to_user() for sizeof(int) bytes. No padding or uninitialized stack bytes are copied.\n2. CMSG handling in recvmsg: put_cmsg() is called with \u0026cb-\u003eendpoint_id (a u32), which is unconditionally initialized in all packet ingress paths (qrtr_endpoint_post and qrtr_local_enqueue).\n3. CMSG parsing in qrtr_msg_get_endpoint: the output parameter is initialized to 0 or the parsed cmsg value on success, preventing uninitialized reads in qrtr_sendmsg and qrtr_ns_worker.\n4. Heap and socket allocations: all newly introduced structures (struct qrtr_ns_endpoint, struct qrtr_server, struct qrtr_node) use kzalloc/kzalloc_obj, and QRTR sockets are allocated via sk_alloc (which zero-initializes socket memory).\n\nKASAN vs KMSAN applicability:\nThe changes primarily involve radix tree restructuring, cyclic xarray ID management, bit manipulation for 32/64-bit index keys, and socket endpoint matching. Risks associated with these changes include use-after-free, out-of-bounds radix tree access, reference counting bugs, or lock ordering, all of which are caught by standard KASAN and LOCKDEP instrumentation. Because no uninitialized memory usage or kernel-to-user information leaks are introduced, a dedicated KMSAN fuzzing session is not warranted.",
"NeedsKMSAN": false,
"Reasoning": "The patch adds endpoint binding and reporting support to the QRTR (Qualcomm IPC Router) subsystem. It adds setsockopt/getsockopt options at SOL_QRTR, CMSG control message handling for QRTR_ENDPOINT, and updates node lookup and name service logic with radix trees and xarrays. These features are reachable via AF_QIPCRTR sockets and QRTR tun interfaces.",
"WorthFuzzing": true
} |
| 1/1 | 2026/09/02 03:18 | action | read-patch-diff |
0mArgs:null Results: {
"PatchDiff": "commit 3c9f36563269acf4748064ee241321ef1673c592\nAuthor: syz-cluster \u003ctriage@syzkaller.com\u003e\nDate: Wed Sep 2 03:18:43 2026 +0000\n\n syz-cluster: applied patch under review\n\ndiff --git a/include/linux/socket.h b/include/linux/socket.h\nindex 2a8d7b14f1d11..4af1dd816dc35 100644\n--- a/include/linux/socket.h\n+++ b/include/linux/socket.h\n@@ -400,6 +400,7 @@ struct ucred {\n #define SOL_MCTP\t285\n #define SOL_SMC\t\t286\n #define SOL_VSOCK\t287\n+#define SOL_QRTR\t288\n \n /* IPX options */\n #define IPX_TYPE\t1\ndiff --git a/include/uapi/linux/qrtr.h b/include/uapi/linux/qrtr.h\nindex f7e2fb3d752b5..0a8667b049c3d 100644\n--- a/include/uapi/linux/qrtr.h\n+++ b/include/uapi/linux/qrtr.h\n@@ -46,4 +46,11 @@ struct qrtr_ctrl_pkt {\n \t};\n } __packed;\n \n+/* setsockopt / getsockopt */\n+#define QRTR_REPORT_ENDPOINT 1\n+#define QRTR_BIND_ENDPOINT 2\n+\n+/* CMSG */\n+#define QRTR_ENDPOINT 1\n+\n #endif /* _LINUX_QRTR_H */\ndiff --git a/net/qrtr/af_qrtr.c b/net/qrtr/af_qrtr.c\nindex a30fa56e6aa31..7c50d32b11015 100644\n--- a/net/qrtr/af_qrtr.c\n+++ b/net/qrtr/af_qrtr.c\n@@ -22,9 +22,14 @@\n #define QRTR_MAX_EPH_SOCKET 0x7fff\n #define QRTR_EPH_PORT_RANGE \\\n \t\tXA_LIMIT(QRTR_MIN_EPH_SOCKET, QRTR_MAX_EPH_SOCKET)\n+#define QRTR_ENDPOINT_RANGE XA_LIMIT(qrtr_local_nid + 1, INT_MAX)\n \n #define QRTR_PORT_CTRL_LEGACY 0xffff\n \n+enum {\n+\tQRTR_F_REPORT_ENDPOINT,\n+};\n+\n /**\n * struct qrtr_hdr_v1 - (I|R)PCrouter packet header version 1\n * @version: protocol version\n@@ -78,6 +83,7 @@ struct qrtr_cb {\n \tu32 src_port;\n \tu32 dst_node;\n \tu32 dst_port;\n+\tu32 endpoint_id;\n \n \tu8 type;\n \tu8 confirm_rx;\n@@ -91,6 +97,8 @@ struct qrtr_sock {\n \tstruct sock sk;\n \tstruct sockaddr_qrtr us;\n \tstruct sockaddr_qrtr peer;\n+\tunsigned long flags;\n+\tu32 bound_endpoint;\n };\n \n static inline struct qrtr_sock *qrtr_sk(struct sock *sk)\n@@ -99,6 +107,43 @@ static inline struct qrtr_sock *qrtr_sk(struct sock *sk)\n \treturn container_of(sk, struct qrtr_sock, sk);\n }\n \n+void qrtr_sock_set_report_endpoint(struct sock *sk)\n+{\n+\tstruct qrtr_sock *ipc = qrtr_sk(sk);\n+\n+\tassign_bit(QRTR_F_REPORT_ENDPOINT, \u0026ipc-\u003eflags, 1);\n+}\n+\n+int qrtr_msg_get_endpoint(struct msghdr *msg, u32 *out_endpoint_id)\n+{\n+\tstruct cmsghdr *cmsg;\n+\tu32 endpoint_id = 0;\n+\n+\tfor_each_cmsghdr(cmsg, msg) {\n+\t\tif (!CMSG_OK(msg, cmsg))\n+\t\t\treturn -EINVAL;\n+\n+\t\tif (cmsg-\u003ecmsg_level != SOL_QRTR)\n+\t\t\tcontinue;\n+\n+\t\tif (cmsg-\u003ecmsg_type != QRTR_ENDPOINT)\n+\t\t\treturn -EINVAL;\n+\n+\t\tif (cmsg-\u003ecmsg_len \u003c CMSG_LEN(sizeof(u32)))\n+\t\t\treturn -EINVAL;\n+\n+\t\t/* Endpoint ids start at 1 */\n+\t\tendpoint_id = *(u32 *)CMSG_DATA(cmsg);\n+\t\tif (!endpoint_id)\n+\t\t\treturn -EINVAL;\n+\t}\n+\n+\tif (out_endpoint_id)\n+\t\t*out_endpoint_id = endpoint_id;\n+\n+\treturn 0;\n+}\n+\n static unsigned int qrtr_local_nid = 1;\n \n /* for node ids */\n@@ -109,9 +154,37 @@ static LIST_HEAD(qrtr_all_nodes);\n /* lock for qrtr_all_nodes and node reference */\n static DEFINE_MUTEX(qrtr_node_lock);\n \n+/* endpoint id allocation management */\n+static DEFINE_XARRAY_ALLOC(qrtr_endpoints);\n+static u32 next_endpoint_id;\n+\n /* local port allocation management */\n static DEFINE_XARRAY_ALLOC(qrtr_ports);\n \n+/* The xarray API uses fixed unsigned long keys and we will have to make\n+ * do with that.\n+ * These keys are often a combination of node IDs and port numbers or\n+ * endpoint IDs and node IDs (all currently u32).\n+ * Using the high 32 bits for the node/endpoint ID and the low 32 bits for the\n+ * port number/node ID will work fine to create keys on platforms where\n+ * unsigned long is 64 bits wide, but obviously is not be possible on\n+ * platforms where unsigned long is smaller.\n+ * Virtually split up unsigned long in half and assign the upper bits to\n+ * node/endpoint IDs and the lower bits to the port number/node ID, however\n+ * big that may be.\n+ */\n+#define QRTR_XARRAY_INDEX_BITS (8 /* CHAR_BIT */ * sizeof(unsigned long))\n+#define QRTR_INDEX_HALF_BITS (QRTR_XARRAY_INDEX_BITS \u003e\u003e 1)\n+\n+#define QRTR_INDEX_HALF_UNSIGNED_MAX ((~(unsigned long)(0)) \u003e\u003e QRTR_INDEX_HALF_BITS)\n+#define QRTR_INDEX_HALF_UNSIGNED_MIN ((unsigned long)(0))\n+\n+#define QRTR_INDEX_HALF_SIGNED_MAX ((long)(QRTR_INDEX_HALF_UNSIGNED_MAX) \u003e\u003e 1)\n+#define QRTR_INDEX_HALF_SIGNED_MIN ((long)(-1) - QRTR_INDEX_HALF_SIGNED_MAX)\n+\n+/* endpoint not defined, i.e. endpoint 0 */\n+static struct qrtr_node_lookup_helper helper0;\n+\n /**\n * struct qrtr_node - endpoint node\n * @ep_lock: lock for endpoint management and callbacks\n@@ -152,9 +225,11 @@ struct qrtr_tx_flow {\n #define QRTR_TX_FLOW_LOW\t5\n \n static int qrtr_local_enqueue(struct qrtr_node *node, struct sk_buff *skb,\n+\t\t\t u32 endpoint_id,\n \t\t\t int type, struct sockaddr_qrtr *from,\n \t\t\t struct sockaddr_qrtr *to);\n static int qrtr_bcast_enqueue(struct qrtr_node *node, struct sk_buff *skb,\n+\t\t\t u32 endpoint_id,\n \t\t\t int type, struct sockaddr_qrtr *from,\n \t\t\t struct sockaddr_qrtr *to);\n static struct qrtr_sock *qrtr_port_lookup(int port);\n@@ -169,9 +244,11 @@ static void __qrtr_node_release(struct kref *kref)\n {\n \tstruct qrtr_node *node = container_of(kref, struct qrtr_node, ref);\n \tstruct radix_tree_iter iter;\n+\tstruct radix_tree_iter iter2;\n \tstruct qrtr_tx_flow *flow;\n \tunsigned long flags;\n \tvoid __rcu **slot;\n+\tvoid __rcu **slot2;\n \tunsigned long index;\n \n \tspin_lock_irqsave(\u0026qrtr_nodes_lock, flags);\n@@ -179,8 +256,18 @@ static void __qrtr_node_release(struct kref *kref)\n \t * multiple entries pointing to our released node, delete them all.\n \t */\n \tradix_tree_for_each_slot(slot, \u0026qrtr_nodes, \u0026iter, 0) {\n-\t\tif (*slot == node)\n+\t\tstruct qrtr_node_lookup_helper *helper = *slot;\n+\n+\t\tif (!helper)\n+\t\t\tcontinue;\n+\t\tradix_tree_for_each_slot(slot2, \u0026helper-\u003enodes, \u0026iter2, 0) {\n+\t\t\tif (*slot2 == node)\n+\t\t\t\tradix_tree_iter_delete(\u0026helper-\u003enodes, \u0026iter2, slot2);\n+\t\t}\n+\t\tif (helper != \u0026helper0 \u0026\u0026 radix_tree_empty(\u0026helper-\u003enodes)) {\n \t\t\tradix_tree_iter_delete(\u0026qrtr_nodes, \u0026iter, slot);\n+\t\t\thelper-\u003eadded = 0;\n+\t\t}\n \t}\n \tspin_unlock_irqrestore(\u0026qrtr_nodes_lock, flags);\n \n@@ -216,16 +303,23 @@ static void qrtr_node_release(struct qrtr_node *node)\n * qrtr_tx_resume() - reset flow control counter\n * @node:\tqrtr_node that the QRTR_TYPE_RESUME_TX packet arrived on\n * @skb:\tresume_tx packet\n+ *\n+ * Return: 0 on success; negative error code on failure\n */\n-static void qrtr_tx_resume(struct qrtr_node *node, struct sk_buff *skb)\n+static int qrtr_tx_resume(struct qrtr_node *node, struct sk_buff *skb)\n {\n \tstruct qrtr_ctrl_pkt *pkt = (struct qrtr_ctrl_pkt *)skb-\u003edata;\n \tu64 remote_node = le32_to_cpu(pkt-\u003eclient.node);\n \tu32 remote_port = le32_to_cpu(pkt-\u003eclient.port);\n \tstruct qrtr_tx_flow *flow;\n-\tunsigned long key;\n+\tunsigned long key = 0;\n+\n+\t/* Don't check node/port for the valid range, use only low\n+\t * 16 bits on 32-bit architectures.\n+\t */\n \n-\tkey = remote_node \u003c\u003c 32 | remote_port;\n+\tkey = ((unsigned long)(remote_node) \u003c\u003c QRTR_INDEX_HALF_BITS) |\n+\t ((unsigned long)(remote_port) \u0026 QRTR_INDEX_HALF_UNSIGNED_MAX);\n \n \tflow = xa_load(\u0026node-\u003eqrtr_tx_flow, key);\n \tif (flow) {\n@@ -236,6 +330,8 @@ static void qrtr_tx_resume(struct qrtr_node *node, struct sk_buff *skb)\n \t}\n \n \tconsume_skb(skb);\n+\n+\treturn 0;\n }\n \n /**\n@@ -256,11 +352,18 @@ static void qrtr_tx_resume(struct qrtr_node *node, struct sk_buff *skb)\n static int qrtr_tx_wait(struct qrtr_node *node, int dest_node, int dest_port,\n \t\t\tint type)\n {\n-\tunsigned long key = (u64)dest_node \u003c\u003c 32 | dest_port;\n+\tunsigned long key = 0;\n \tstruct qrtr_tx_flow *flow;\n \tint confirm_rx = 0;\n \tint ret;\n \n+\t/* Don't check node/port for the valid range, use only low\n+\t * 16 bits on 32-bit architectures.\n+\t */\n+\n+\tkey = ((unsigned long)(dest_node) \u003c\u003c QRTR_INDEX_HALF_BITS) |\n+\t ((unsigned long)(dest_port) \u0026 QRTR_INDEX_HALF_UNSIGNED_MAX);\n+\n \t/* Never set confirm_rx on non-data packets */\n \tif (type != QRTR_TYPE_DATA)\n \t\treturn 0;\n@@ -317,23 +420,35 @@ static int qrtr_tx_wait(struct qrtr_node *node, int dest_node, int dest_port,\n * message associated with the dropped confirm_rx message.\n * Work around this by marking the flow as having a failed transmission and\n * cause the next transmission attempt to be sent with the confirm_rx.\n+ *\n+ * Return: 0 on success; negative error code on failure\n */\n-static void qrtr_tx_flow_failed(struct qrtr_node *node, int dest_node,\n-\t\t\t\tint dest_port)\n+static int qrtr_tx_flow_failed(struct qrtr_node *node, int dest_node,\n+\t\t\t int dest_port)\n {\n-\tunsigned long key = (u64)dest_node \u003c\u003c 32 | dest_port;\n+\tunsigned long key = 0;\n \tstruct qrtr_tx_flow *flow;\n \n+\t/* Don't check node/port for the valid range, use only low\n+\t * 16 bits on 32-bit architectures.\n+\t */\n+\n+\tkey = ((unsigned long)(dest_node) \u003c\u003c QRTR_INDEX_HALF_BITS) |\n+\t ((unsigned long)(dest_port) \u0026 QRTR_INDEX_HALF_UNSIGNED_MAX);\n+\n \tflow = xa_load(\u0026node-\u003eqrtr_tx_flow, key);\n \tif (flow) {\n \t\tspin_lock_irq(\u0026flow-\u003eresume_tx.lock);\n \t\tflow-\u003etx_failed = 1;\n \t\tspin_unlock_irq(\u0026flow-\u003eresume_tx.lock);\n \t}\n+\n+\treturn 0;\n }\n \n /* Pass an outgoing packet socket buffer to the endpoint driver. */\n static int qrtr_node_enqueue(struct qrtr_node *node, struct sk_buff *skb,\n+\t\t\t u32 endpoint_id,\n \t\t\t int type, struct sockaddr_qrtr *from,\n \t\t\t struct sockaddr_qrtr *to)\n {\n@@ -377,7 +492,7 @@ static int qrtr_node_enqueue(struct qrtr_node *node, struct sk_buff *skb,\n \t/* Need to ensure that a subsequent message carries the otherwise lost\n \t * confirm_rx flag if we dropped this one */\n \tif (rc \u0026\u0026 confirm_rx)\n-\t\tqrtr_tx_flow_failed(node, to-\u003esq_node, to-\u003esq_port);\n+\t\trc = qrtr_tx_flow_failed(node, to-\u003esq_node, to-\u003esq_port);\n \n \treturn rc;\n }\n@@ -386,14 +501,18 @@ static int qrtr_node_enqueue(struct qrtr_node *node, struct sk_buff *skb,\n *\n * callers must release with qrtr_node_release()\n */\n-static struct qrtr_node *qrtr_node_lookup(unsigned int nid)\n+static struct qrtr_node *qrtr_node_lookup(unsigned int endpoint_id,\n+\t\t\t\t\t unsigned int nid)\n {\n-\tstruct qrtr_node *node;\n+\tstruct qrtr_node *node = NULL;\n+\tstruct qrtr_node_lookup_helper *helper = NULL;\n \tunsigned long flags;\n \n \tmutex_lock(\u0026qrtr_node_lock);\n \tspin_lock_irqsave(\u0026qrtr_nodes_lock, flags);\n-\tnode = radix_tree_lookup(\u0026qrtr_nodes, nid);\n+\thelper = radix_tree_lookup(\u0026qrtr_nodes, endpoint_id);\n+\tif (helper)\n+\t\tnode = radix_tree_lookup(\u0026helper-\u003enodes, nid);\n \tnode = qrtr_node_acquire(node);\n \tspin_unlock_irqrestore(\u0026qrtr_nodes_lock, flags);\n \tmutex_unlock(\u0026qrtr_node_lock);\n@@ -405,19 +524,51 @@ static struct qrtr_node *qrtr_node_lookup(unsigned int nid)\n *\n * This is mostly useful for automatic node id assignment, based on\n * the source id in the incoming packet.\n+ *\n+ * Return: 0 on success; negative error code on failure\n */\n-static void qrtr_node_assign(struct qrtr_node *node, unsigned int nid)\n+static int qrtr_node_assign(struct qrtr_node *node, unsigned int nid)\n {\n \tunsigned long flags;\n+\tint rc = 0;\n+\tint did_insert0 = 0;\n \n \tif (nid == QRTR_EP_NID_AUTO)\n-\t\treturn;\n+\t\treturn 0;\n \n \tspin_lock_irqsave(\u0026qrtr_nodes_lock, flags);\n-\tradix_tree_insert(\u0026qrtr_nodes, nid, node);\n+\n+\tif (!radix_tree_lookup(\u0026helper0.nodes, nid)) {\n+\t\trc = radix_tree_insert(\u0026helper0.nodes, nid, node);\n+\t\tif (rc)\n+\t\t\tgoto err_lock;\n+\t\tdid_insert0 = 1;\n+\t}\n+\tif (!node-\u003eep-\u003ehelper.added) {\n+\t\tINIT_RADIX_TREE(\u0026node-\u003eep-\u003ehelper.nodes, GFP_ATOMIC);\n+\t\trc = radix_tree_insert(\u0026qrtr_nodes, node-\u003eep-\u003eid,\n+\t\t\t\t \u0026node-\u003eep-\u003ehelper);\n+\t\tif (rc)\n+\t\t\tgoto err_insert0;\n+\t\tnode-\u003eep-\u003ehelper.added = 1;\n+\t}\n+\trc = radix_tree_insert(\u0026node-\u003eep-\u003ehelper.nodes, nid, node);\n+\tif (rc \u0026\u0026 rc != -EEXIST)\n+\t\tgoto err_insert0; // Don't revert helper insertion\n+\n \tif (node-\u003enid == QRTR_EP_NID_AUTO)\n \t\tnode-\u003enid = nid;\n \tspin_unlock_irqrestore(\u0026qrtr_nodes_lock, flags);\n+\n+\treturn 0;\n+\n+err_insert0:\n+\tif (did_insert0)\n+\t\tradix_tree_delete(\u0026helper0.nodes, nid);\n+\n+err_lock:\n+\tspin_unlock_irqrestore(\u0026qrtr_nodes_lock, flags);\n+\treturn rc;\n }\n \n /**\n@@ -439,6 +590,7 @@ int qrtr_endpoint_post(struct qrtr_endpoint *ep, const void *data, size_t len)\n \tsize_t size;\n \tunsigned int ver;\n \tsize_t hdrlen;\n+\tint ret = -EINVAL;\n \n \tif (len == 0 || len \u0026 3)\n \t\treturn -EINVAL;\n@@ -496,9 +648,16 @@ int qrtr_endpoint_post(struct qrtr_endpoint *ep, const void *data, size_t len)\n \tif (cb-\u003edst_port == QRTR_PORT_CTRL_LEGACY)\n \t\tcb-\u003edst_port = QRTR_PORT_CTRL;\n \n+\tcb-\u003eendpoint_id = ep-\u003eid;\n+\n \tif (!size || size \u003e len || len != ALIGN(size, 4) + hdrlen)\n \t\tgoto err;\n \n+\t/* Don't allow remote lookups */\n+\tif (cb-\u003etype == QRTR_TYPE_NEW_LOOKUP ||\n+\t cb-\u003etype == QRTR_TYPE_DEL_LOOKUP)\n+\t\tgoto err;\n+\n \tif ((cb-\u003etype == QRTR_TYPE_NEW_SERVER ||\n \t cb-\u003etype == QRTR_TYPE_RESUME_TX) \u0026\u0026\n \t size \u003c sizeof(struct qrtr_ctrl_pkt))\n@@ -510,26 +669,39 @@ int qrtr_endpoint_post(struct qrtr_endpoint *ep, const void *data, size_t len)\n \n \tskb_put_data(skb, data + hdrlen, size);\n \n-\tqrtr_node_assign(node, cb-\u003esrc_node);\n+\tret = qrtr_node_assign(node, cb-\u003esrc_node);\n+\tif (ret)\n+\t\tgoto err;\n \n \tif (cb-\u003etype == QRTR_TYPE_NEW_SERVER) {\n \t\t/* Remote node endpoint can bridge other distant nodes */\n \t\tconst struct qrtr_ctrl_pkt *pkt;\n \n \t\tpkt = data + hdrlen;\n-\t\tqrtr_node_assign(node, le32_to_cpu(pkt-\u003eserver.node));\n+\t\tret = qrtr_node_assign(node, le32_to_cpu(pkt-\u003eserver.node));\n+\t\tif (ret)\n+\t\t\tgoto err;\n \t}\n \n \tif (cb-\u003etype == QRTR_TYPE_RESUME_TX) {\n-\t\tqrtr_tx_resume(node, skb);\n+\t\tret = qrtr_tx_resume(node, skb);\n+\t\tif (ret)\n+\t\t\tgoto err;\n \t} else {\n \t\tipc = qrtr_port_lookup(cb-\u003edst_port);\n-\t\tif (!ipc)\n+\t\tif (!ipc) {\n+\t\t\tret = -EINVAL;\n \t\t\tgoto err;\n+\t\t}\n \n-\t\tif (sock_queue_rcv_skb(\u0026ipc-\u003esk, skb)) {\n-\t\t\tqrtr_port_put(ipc);\n-\t\t\tgoto err;\n+\t\t/* Sockets bound to an endpoint only rx from that endpoint */\n+\t\tif (!ipc-\u003ebound_endpoint ||\n+\t\t ipc-\u003ebound_endpoint == cb-\u003eendpoint_id) {\n+\t\t\tret = sock_queue_rcv_skb(\u0026ipc-\u003esk, skb);\n+\t\t\tif (ret) {\n+\t\t\t\tqrtr_port_put(ipc);\n+\t\t\t\tgoto err;\n+\t\t\t}\n \t\t}\n \n \t\tqrtr_port_put(ipc);\n@@ -539,7 +711,7 @@ int qrtr_endpoint_post(struct qrtr_endpoint *ep, const void *data, size_t len)\n \n err:\n \tkfree_skb(skb);\n-\treturn -EINVAL;\n+\treturn ret;\n \n }\n EXPORT_SYMBOL_GPL(qrtr_endpoint_post);\n@@ -581,6 +753,8 @@ static struct sk_buff *qrtr_alloc_ctrl_packet(struct qrtr_ctrl_pkt **pkt,\n int qrtr_endpoint_register(struct qrtr_endpoint *ep, unsigned int nid)\n {\n \tstruct qrtr_node *node;\n+\tu32 endpoint_id;\n+\tint rc;\n \n \tif (!ep || !ep-\u003exmit)\n \t\treturn -EINVAL;\n@@ -589,6 +763,13 @@ int qrtr_endpoint_register(struct qrtr_endpoint *ep, unsigned int nid)\n \tif (!node)\n \t\treturn -ENOMEM;\n \n+\trc = xa_alloc_cyclic(\u0026qrtr_endpoints, \u0026endpoint_id, NULL,\n+\t\t\t QRTR_ENDPOINT_RANGE, \u0026next_endpoint_id,\n+\t\t\t GFP_KERNEL);\n+\n+\tif (rc \u003c 0)\n+\t\tgoto free_node;\n+\n \tkref_init(\u0026node-\u003eref);\n \tmutex_init(\u0026node-\u003eep_lock);\n \tskb_queue_head_init(\u0026node-\u003erx_queue);\n@@ -598,14 +779,21 @@ int qrtr_endpoint_register(struct qrtr_endpoint *ep, unsigned int nid)\n \txa_init(\u0026node-\u003eqrtr_tx_flow);\n \tmutex_init(\u0026node-\u003eqrtr_tx_lock);\n \n-\tqrtr_node_assign(node, nid);\n+\trc = qrtr_node_assign(node, nid);\n+\tif (rc \u003c 0)\n+\t\tgoto free_node;\n \n \tmutex_lock(\u0026qrtr_node_lock);\n \tlist_add(\u0026node-\u003eitem, \u0026qrtr_all_nodes);\n \tmutex_unlock(\u0026qrtr_node_lock);\n \tep-\u003enode = node;\n+\tep-\u003eid = endpoint_id;\n+\tep-\u003ehelper.added = 0;\n \n \treturn 0;\n+free_node:\n+\tkfree(node);\n+\treturn rc;\n }\n EXPORT_SYMBOL_GPL(qrtr_endpoint_register);\n \n@@ -619,29 +807,46 @@ void qrtr_endpoint_unregister(struct qrtr_endpoint *ep)\n \tstruct sockaddr_qrtr src = {AF_QIPCRTR, node-\u003enid, QRTR_PORT_CTRL};\n \tstruct sockaddr_qrtr dst = {AF_QIPCRTR, qrtr_local_nid, QRTR_PORT_CTRL};\n \tstruct radix_tree_iter iter;\n+\tstruct radix_tree_iter iter2;\n \tstruct qrtr_ctrl_pkt *pkt;\n \tstruct qrtr_tx_flow *flow;\n \tstruct sk_buff *skb;\n \tunsigned long flags;\n \tunsigned long index;\n \tvoid __rcu **slot;\n+\tvoid __rcu **slot2;\n+\tu32 endpoint_id;\n \n \tmutex_lock(\u0026node-\u003eep_lock);\n+\tendpoint_id = node-\u003eep-\u003eid;\n \tnode-\u003eep = NULL;\n \tmutex_unlock(\u0026node-\u003eep_lock);\n \n \t/* Notify the local controller about the event */\n \tspin_lock_irqsave(\u0026qrtr_nodes_lock, flags);\n \tradix_tree_for_each_slot(slot, \u0026qrtr_nodes, \u0026iter, 0) {\n-\t\tif (*slot != node)\n+\t\tstruct qrtr_node_lookup_helper *helper = *slot;\n+\n+\t\tif (!helper)\n \t\t\tcontinue;\n-\t\tsrc.sq_node = iter.index;\n-\t\tskb = qrtr_alloc_ctrl_packet(\u0026pkt, GFP_ATOMIC);\n-\t\tif (skb) {\n-\t\t\tpkt-\u003ecmd = cpu_to_le32(QRTR_TYPE_BYE);\n-\t\t\tqrtr_local_enqueue(NULL, skb, QRTR_TYPE_BYE, \u0026src, \u0026dst);\n+\t\tradix_tree_for_each_slot(slot2, \u0026helper-\u003enodes, \u0026iter2, 0) {\n+\t\t\tif (*slot2 != node)\n+\t\t\t\tcontinue;\n+\t\t\tsrc.sq_node = iter.index;\n+\t\t\tskb = qrtr_alloc_ctrl_packet(\u0026pkt, GFP_ATOMIC);\n+\t\t\tif (skb) {\n+\t\t\t\tpkt-\u003ecmd = cpu_to_le32(QRTR_TYPE_BYE);\n+\t\t\t\tqrtr_local_enqueue(NULL, skb, endpoint_id,\n+\t\t\t\t\t\t QRTR_TYPE_BYE, \u0026src, \u0026dst);\n+\t\t\t}\n \t\t}\n \t}\n+\tif (ep-\u003ehelper.added) {\n+\t\tradix_tree_for_each_slot(slot, \u0026ep-\u003ehelper.nodes, \u0026iter, 0)\n+\t\t\tradix_tree_iter_delete(\u0026ep-\u003ehelper.nodes, \u0026iter, slot);\n+\t\tradix_tree_delete(\u0026qrtr_nodes, ep-\u003eid);\n+\t\tep-\u003ehelper.added = 0;\n+\t}\n \tspin_unlock_irqrestore(\u0026qrtr_nodes_lock, flags);\n \n \t/* Wake up any transmitters waiting for resume-tx from the node */\n@@ -651,6 +856,10 @@ void qrtr_endpoint_unregister(struct qrtr_endpoint *ep)\n \tmutex_unlock(\u0026node-\u003eqrtr_tx_lock);\n \n \tqrtr_node_release(node);\n+\n+\txa_erase(\u0026qrtr_endpoints, endpoint_id);\n+\n+\tep-\u003eid = 0;\n \tep-\u003enode = NULL;\n }\n EXPORT_SYMBOL_GPL(qrtr_endpoint_unregister);\n@@ -700,8 +909,8 @@ static void qrtr_port_remove(struct qrtr_sock *ipc)\n \t\tpkt-\u003eclient.port = cpu_to_le32(ipc-\u003eus.sq_port);\n \n \t\tskb_set_owner_w(skb, \u0026ipc-\u003esk);\n-\t\tqrtr_bcast_enqueue(NULL, skb, QRTR_TYPE_DEL_CLIENT, \u0026ipc-\u003eus,\n-\t\t\t\t \u0026to);\n+\t\tqrtr_bcast_enqueue(NULL, skb, qrtr_local_nid,\n+\t\t\t\t QRTR_TYPE_DEL_CLIENT, \u0026ipc-\u003eus, \u0026to);\n \t}\n \n \tif (port == QRTR_PORT_CTRL)\n@@ -841,37 +1050,52 @@ static int qrtr_bind(struct socket *sock, struct sockaddr_unsized *saddr, int le\n \n /* Queue packet to local peer socket. */\n static int qrtr_local_enqueue(struct qrtr_node *node, struct sk_buff *skb,\n+\t\t\t u32 endpoint_id,\n \t\t\t int type, struct sockaddr_qrtr *from,\n \t\t\t struct sockaddr_qrtr *to)\n {\n \tstruct qrtr_sock *ipc;\n \tstruct qrtr_cb *cb;\n+\tint ret = -ENODEV;\n \n \tipc = qrtr_port_lookup(to-\u003esq_port);\n-\tif (!ipc || \u0026ipc-\u003esk == skb-\u003esk) { /* do not send to self */\n-\t\tif (ipc)\n-\t\t\tqrtr_port_put(ipc);\n-\t\tkfree_skb(skb);\n-\t\treturn -ENODEV;\n-\t}\n+\tif (!ipc)\n+\t\tgoto done;\n+\n+\tif (\u0026ipc-\u003esk == skb-\u003esk) /* do not send to self */\n+\t\tgoto done;\n+\n+\t/*\n+\t * Filter out unwanted packets that are not on behalf of the bound\n+\t * endpoint. Certain special packets (such as an empty NEW_SERVER\n+\t * packet that serves as a sentinel value) always go through.\n+\t */\n+\tif (endpoint_id \u0026\u0026 ipc-\u003ebound_endpoint \u0026\u0026\n+\t ipc-\u003ebound_endpoint != endpoint_id)\n+\t\tgoto done;\n \n \tcb = (struct qrtr_cb *)skb-\u003ecb;\n \tcb-\u003esrc_node = from-\u003esq_node;\n \tcb-\u003esrc_port = from-\u003esq_port;\n+\tcb-\u003eendpoint_id = endpoint_id;\n \n-\tif (sock_queue_rcv_skb(\u0026ipc-\u003esk, skb)) {\n-\t\tqrtr_port_put(ipc);\n-\t\tkfree_skb(skb);\n-\t\treturn -ENOSPC;\n-\t}\n+\tret = -ENOSPC;\n+\tif (sock_queue_rcv_skb(\u0026ipc-\u003esk, skb))\n+\t\tgoto done;\n \n \tqrtr_port_put(ipc);\n \n \treturn 0;\n+done:\n+\tif (ipc)\n+\t\tqrtr_port_put(ipc);\n+\tkfree_skb(skb);\n+\treturn ret;\n }\n \n /* Queue packet for broadcast. */\n static int qrtr_bcast_enqueue(struct qrtr_node *node, struct sk_buff *skb,\n+\t\t\t u32 endpoint_id,\n \t\t\t int type, struct sockaddr_qrtr *from,\n \t\t\t struct sockaddr_qrtr *to)\n {\n@@ -883,11 +1107,11 @@ static int qrtr_bcast_enqueue(struct qrtr_node *node, struct sk_buff *skb,\n \t\tif (!skbn)\n \t\t\tbreak;\n \t\tskb_set_owner_w(skbn, skb-\u003esk);\n-\t\tqrtr_node_enqueue(node, skbn, type, from, to);\n+\t\tqrtr_node_enqueue(node, skbn, endpoint_id, type, from, to);\n \t}\n \tmutex_unlock(\u0026qrtr_node_lock);\n \n-\tqrtr_local_enqueue(NULL, skb, type, from, to);\n+\tqrtr_local_enqueue(NULL, skb, endpoint_id, type, from, to);\n \n \treturn 0;\n }\n@@ -895,12 +1119,16 @@ static int qrtr_bcast_enqueue(struct qrtr_node *node, struct sk_buff *skb,\n static int qrtr_sendmsg(struct socket *sock, struct msghdr *msg, size_t len)\n {\n \tDECLARE_SOCKADDR(struct sockaddr_qrtr *, addr, msg-\u003emsg_name);\n-\tint (*enqueue_fn)(struct qrtr_node *, struct sk_buff *, int,\n-\t\t\t struct sockaddr_qrtr *, struct sockaddr_qrtr *);\n+\tint (*enqueue_fn)(struct qrtr_node *node, struct sk_buff *skb,\n+\t\t\t u32 endpoint_id, int type,\n+\t\t\t struct sockaddr_qrtr *from,\n+\t\t\t struct sockaddr_qrtr *to);\n \t__le32 qrtr_type = cpu_to_le32(QRTR_TYPE_DATA);\n \tstruct qrtr_sock *ipc = qrtr_sk(sock-\u003esk);\n \tstruct sock *sk = sock-\u003esk;\n \tstruct qrtr_node *node;\n+\tu32 msg_endpoint_id;\n+\tu32 endpoint_id = qrtr_local_nid;\n \tstruct sk_buff *skb;\n \tsize_t plen;\n \tu32 type;\n@@ -912,46 +1140,49 @@ static int qrtr_sendmsg(struct socket *sock, struct msghdr *msg, size_t len)\n \tif (len \u003e 65535)\n \t\treturn -EMSGSIZE;\n \n+\trc = qrtr_msg_get_endpoint(msg, \u0026msg_endpoint_id);\n+\tif (rc \u003c 0)\n+\t\treturn rc;\n+\n \tlock_sock(sk);\n \n \tif (addr) {\n-\t\tif (msg-\u003emsg_namelen \u003c sizeof(*addr)) {\n-\t\t\trelease_sock(sk);\n-\t\t\treturn -EINVAL;\n-\t\t}\n+\t\trc = -EINVAL;\n \n-\t\tif (addr-\u003esq_family != AF_QIPCRTR) {\n-\t\t\trelease_sock(sk);\n-\t\t\treturn -EINVAL;\n-\t\t}\n+\t\tif (msg-\u003emsg_namelen \u003c sizeof(*addr))\n+\t\t\tgoto release_sock;\n+\n+\t\tif (addr-\u003esq_family != AF_QIPCRTR)\n+\t\t\tgoto release_sock;\n \n \t\trc = qrtr_autobind(sock);\n-\t\tif (rc) {\n-\t\t\trelease_sock(sk);\n-\t\t\treturn rc;\n-\t\t}\n+\t\tif (rc)\n+\t\t\tgoto release_sock;\n \t} else if (sk-\u003esk_state == TCP_ESTABLISHED) {\n \t\taddr = \u0026ipc-\u003epeer;\n \t} else {\n-\t\trelease_sock(sk);\n-\t\treturn -ENOTCONN;\n+\t\trc = -ENOTCONN;\n+\t\tgoto release_sock;\n \t}\n \n \tnode = NULL;\n \tif (addr-\u003esq_node == QRTR_NODE_BCAST) {\n \t\tif (addr-\u003esq_port != QRTR_PORT_CTRL \u0026\u0026\n \t\t qrtr_local_nid != QRTR_NODE_BCAST) {\n-\t\t\trelease_sock(sk);\n-\t\t\treturn -ENOTCONN;\n+\t\t\trc = -ENOTCONN;\n+\t\t\tgoto release_sock;\n \t\t}\n \t\tenqueue_fn = qrtr_bcast_enqueue;\n \t} else if (addr-\u003esq_node == ipc-\u003eus.sq_node) {\n \t\tenqueue_fn = qrtr_local_enqueue;\n \t} else {\n-\t\tnode = qrtr_node_lookup(addr-\u003esq_node);\n+\t\tendpoint_id = msg_endpoint_id ?\n+\t\t\t msg_endpoint_id : ipc-\u003ebound_endpoint;\n+\n+\t\tnode = qrtr_node_lookup(endpoint_id, addr-\u003esq_node);\n \t\tif (!node) {\n-\t\t\trelease_sock(sk);\n-\t\t\treturn -ECONNRESET;\n+\t\t\trc = endpoint_id ? -ENXIO : -ECONNRESET;\n+\t\t\tgoto release_sock;\n \t\t}\n \t\tenqueue_fn = qrtr_node_enqueue;\n \t}\n@@ -981,15 +1212,21 @@ static int qrtr_sendmsg(struct socket *sock, struct msghdr *msg, size_t len)\n \n \t\t/* control messages already require the type as 'command' */\n \t\tskb_copy_bits(skb, 0, \u0026qrtr_type, 4);\n+\t\t/*\n+\t\t * Allow local name service to make packets appear as if\n+\t\t * they originated remotely\n+\t\t */\n+\t\tendpoint_id = msg_endpoint_id;\n \t}\n \n \ttype = le32_to_cpu(qrtr_type);\n-\trc = enqueue_fn(node, skb, type, \u0026ipc-\u003eus, addr);\n+\trc = enqueue_fn(node, skb, endpoint_id, type, \u0026ipc-\u003eus, addr);\n \tif (rc \u003e= 0)\n \t\trc = len;\n \n out_node:\n \tqrtr_node_release(node);\n+release_sock:\n \trelease_sock(sk);\n \n \treturn rc;\n@@ -1004,7 +1241,7 @@ static int qrtr_send_resume_tx(struct qrtr_cb *cb)\n \tstruct sk_buff *skb;\n \tint ret;\n \n-\tnode = qrtr_node_lookup(remote.sq_node);\n+\tnode = qrtr_node_lookup(cb-\u003eendpoint_id, remote.sq_node);\n \tif (!node)\n \t\treturn -EINVAL;\n \n@@ -1018,7 +1255,8 @@ static int qrtr_send_resume_tx(struct qrtr_cb *cb)\n \tpkt-\u003eclient.node = cpu_to_le32(cb-\u003edst_node);\n \tpkt-\u003eclient.port = cpu_to_le32(cb-\u003edst_port);\n \n-\tret = qrtr_node_enqueue(node, skb, QRTR_TYPE_RESUME_TX, \u0026local, \u0026remote);\n+\tret = qrtr_node_enqueue(node, skb, cb-\u003eendpoint_id,\n+\t\t\t\tQRTR_TYPE_RESUME_TX, \u0026local, \u0026remote);\n \n \tqrtr_node_release(node);\n \n@@ -1029,6 +1267,7 @@ static int qrtr_recvmsg(struct socket *sock, struct msghdr *msg,\n \t\t\tsize_t size, int flags)\n {\n \tDECLARE_SOCKADDR(struct sockaddr_qrtr *, addr, msg-\u003emsg_name);\n+\tstruct qrtr_sock *ipc = qrtr_sk(sock-\u003esk);\n \tstruct sock *sk = sock-\u003esk;\n \tstruct sk_buff *skb;\n \tstruct qrtr_cb *cb;\n@@ -1054,6 +1293,10 @@ static int qrtr_recvmsg(struct socket *sock, struct msghdr *msg,\n \t\tmsg-\u003emsg_flags |= MSG_TRUNC;\n \t}\n \n+\tif (cb-\u003eendpoint_id \u0026\u0026 test_bit(QRTR_F_REPORT_ENDPOINT, \u0026ipc-\u003eflags))\n+\t\tput_cmsg(msg, SOL_QRTR, QRTR_ENDPOINT,\n+\t\t\t sizeof(cb-\u003eendpoint_id), \u0026cb-\u003eendpoint_id);\n+\n \trc = skb_copy_datagram_msg(skb, 0, msg, copied);\n \tif (rc \u003c 0)\n \t\tgoto out;\n@@ -1199,6 +1442,74 @@ static int qrtr_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)\n \treturn rc;\n }\n \n+static int qrtr_setsockopt(struct socket *sock, int level, int optname,\n+\t\t\t sockptr_t optval, unsigned int optlen)\n+{\n+\tstruct qrtr_sock *ipc = qrtr_sk(sock-\u003esk);\n+\tunsigned int val = 0;\n+\tint rc = 0;\n+\n+\tif (level != SOL_QRTR)\n+\t\treturn -ENOPROTOOPT;\n+\n+\tif (optlen \u003e= sizeof(val) \u0026\u0026\n+\t copy_from_sockptr(\u0026val, optval, sizeof(val)))\n+\t\treturn -EFAULT;\n+\n+\tswitch (optname) {\n+\tcase QRTR_REPORT_ENDPOINT:\n+\t\tassign_bit(QRTR_F_REPORT_ENDPOINT, \u0026ipc-\u003eflags, val);\n+\t\tbreak;\n+\tcase QRTR_BIND_ENDPOINT:\n+\t\tWRITE_ONCE(ipc-\u003ebound_endpoint, val);\n+\t\tbreak;\n+\tdefault:\n+\t\trc = -ENOPROTOOPT;\n+\t}\n+\n+\treturn rc;\n+}\n+\n+static int qrtr_getsockopt(struct socket *sock, int level, int optname,\n+\t\t\t char __user *optval, int __user *optlen)\n+{\n+\tstruct qrtr_sock *ipc = qrtr_sk(sock-\u003esk);\n+\tunsigned int val;\n+\tint len;\n+\tint rc = 0;\n+\n+\tif (level != SOL_QRTR)\n+\t\treturn -ENOPROTOOPT;\n+\n+\tif (get_user(len, optlen))\n+\t\treturn -EFAULT;\n+\n+\tif (len \u003c sizeof(val))\n+\t\treturn -EINVAL;\n+\n+\tswitch (optname) {\n+\tcase QRTR_REPORT_ENDPOINT:\n+\t\tval = test_bit(QRTR_F_REPORT_ENDPOINT, \u0026ipc-\u003eflags);\n+\t\tbreak;\n+\tcase QRTR_BIND_ENDPOINT:\n+\t\tval = READ_ONCE(ipc-\u003ebound_endpoint);\n+\t\tbreak;\n+\tdefault:\n+\t\trc = -ENOPROTOOPT;\n+\t}\n+\n+\tif (rc)\n+\t\treturn rc;\n+\n+\tlen = sizeof(int);\n+\n+\tif (put_user(len, optlen) ||\n+\t copy_to_user(optval, \u0026val, len))\n+\t\trc = -EFAULT;\n+\n+\treturn rc;\n+}\n+\n static int qrtr_release(struct socket *sock)\n {\n \tstruct sock *sk = sock-\u003esk;\n@@ -1246,6 +1557,8 @@ static const struct proto_ops qrtr_proto_ops = {\n \t.shutdown\t= sock_no_shutdown,\n \t.release\t= qrtr_release,\n \t.mmap\t\t= sock_no_mmap,\n+\t.setsockopt\t= qrtr_setsockopt,\n+\t.getsockopt\t= qrtr_getsockopt,\n };\n \n static struct proto qrtr_proto = {\n@@ -1310,8 +1623,17 @@ static int __init qrtr_proto_init(void)\n \tif (rc)\n \t\tgoto err_sock;\n \n+\trc = radix_tree_insert(\u0026qrtr_nodes, 0, \u0026helper0);\n+\tif (rc)\n+\t\tgoto err_ns;\n+\n+\tINIT_RADIX_TREE(\u0026helper0.nodes, GFP_ATOMIC);\n+\thelper0.added = 1;\n+\n \treturn 0;\n \n+err_ns:\n+\tqrtr_ns_remove();\n err_sock:\n \tsock_unregister(qrtr_family.family);\n err_proto:\n@@ -1322,6 +1644,7 @@ postcore_initcall(qrtr_proto_init);\n \n static void __exit qrtr_proto_fini(void)\n {\n+\tradix_tree_delete(\u0026qrtr_nodes, 0);\n \tqrtr_ns_remove();\n \tsock_unregister(qrtr_family.family);\n \tproto_unregister(\u0026qrtr_proto);\ndiff --git a/net/qrtr/mhi.c b/net/qrtr/mhi.c\nindex 3990da1a65dc4..630ce0da060e5 100644\n--- a/net/qrtr/mhi.c\n+++ b/net/qrtr/mhi.c\n@@ -106,6 +106,16 @@ static int qcom_mhi_qrtr_queue_dl_buffers(struct mhi_device *mhi_dev)\n \treturn 0;\n }\n \n+static ssize_t endpoint_show(struct device *dev,\n+\t\t\t struct device_attribute *attr, char *buf)\n+{\n+\tstruct qrtr_mhi_dev *qdev = dev_get_drvdata(dev);\n+\n+\treturn sprintf(buf, \"%u\\n\", qdev-\u003eep.id);\n+}\n+\n+static DEVICE_ATTR_RO(endpoint);\n+\n static int qcom_mhi_qrtr_probe(struct mhi_device *mhi_dev,\n \t\t\t const struct mhi_device_id *id)\n {\n@@ -135,6 +145,9 @@ static int qcom_mhi_qrtr_probe(struct mhi_device *mhi_dev,\n \tif (rc)\n \t\tgoto err_unregister;\n \n+\tif (device_create_file(\u0026mhi_dev-\u003edev, \u0026dev_attr_endpoint) \u003c 0)\n+\t\tdev_err(qdev-\u003edev, \"Failed to create endpoint attribute\\n\");\n+\n \tdev_dbg(qdev-\u003edev, \"Qualcomm MHI QRTR driver probed\\n\");\n \n \treturn 0;\n@@ -151,6 +164,7 @@ static void qcom_mhi_qrtr_remove(struct mhi_device *mhi_dev)\n {\n \tstruct qrtr_mhi_dev *qdev = dev_get_drvdata(\u0026mhi_dev-\u003edev);\n \n+\tdevice_remove_file(\u0026mhi_dev-\u003edev, \u0026dev_attr_endpoint);\n \tqrtr_endpoint_unregister(\u0026qdev-\u003eep);\n \tmhi_unprepare_from_transfer(mhi_dev);\n \tdev_set_drvdata(\u0026mhi_dev-\u003edev, NULL);\ndiff --git a/net/qrtr/ns.c b/net/qrtr/ns.c\nindex e5b2adb161d92..d171035b88f69 100644\n--- a/net/qrtr/ns.c\n+++ b/net/qrtr/ns.c\n@@ -5,6 +5,7 @@\n * Copyright (c) 2020, Linaro Ltd.\n */\n \n+#define pr_fmt(fmt) \"QRTR NS: \"fmt\n #include \u003clinux/module.h\u003e\n #include \u003clinux/qrtr.h\u003e\n #include \u003clinux/workqueue.h\u003e\n@@ -16,7 +17,7 @@\n #define CREATE_TRACE_POINTS\n #include \u003ctrace/events/qrtr.h\u003e\n \n-static DEFINE_XARRAY(nodes);\n+static DEFINE_XARRAY(endpoints);\n \n static struct {\n \tstruct socket *sock;\n@@ -62,6 +63,7 @@ struct qrtr_server {\n \n \tunsigned int node;\n \tunsigned int port;\n+\tu32 endpoint_id;\n \n \tstruct list_head qli;\n };\n@@ -72,6 +74,11 @@ struct qrtr_node {\n \tu32 server_count;\n };\n \n+struct qrtr_ns_endpoint {\n+\tunsigned int id;\n+\tstruct xarray nodes;\n+};\n+\n /* Max nodes, server, lookup limits are chosen based on the current platform\n * requirements. If the requirement changes in the future, these values can be\n * increased.\n@@ -82,35 +89,90 @@ struct qrtr_node {\n \n static u16 node_count;\n \n-static struct qrtr_node *node_get(unsigned int node_id)\n+static struct qrtr_node *node_get(u32 endpoint_id, unsigned int node_id)\n {\n+\tstruct qrtr_ns_endpoint *endpoint;\n \tstruct qrtr_node *node;\n+\tbool new_endpoint = false;\n+\n+\tendpoint = xa_load(\u0026endpoints, endpoint_id);\n+\tif (!endpoint) {\n+\t\tendpoint = kzalloc_obj(*endpoint);\n+\t\tif (!endpoint)\n+\t\t\treturn NULL;\n+\n+\t\tendpoint-\u003eid = endpoint_id;\n+\t\txa_init(\u0026endpoint-\u003enodes);\n+\n+\t\tif (xa_store(\u0026endpoints, endpoint_id, endpoint, GFP_KERNEL)) {\n+\t\t\tkfree(endpoint);\n+\t\t\treturn NULL;\n+\t\t}\n+\n+\t\tnew_endpoint = true;\n+\t}\n \n-\tnode = xa_load(\u0026nodes, node_id);\n+\tnode = xa_load(\u0026endpoint-\u003enodes, node_id);\n \tif (node)\n \t\treturn node;\n \n \tif (node_count \u003e= QRTR_NS_MAX_NODES) {\n \t\tpr_err_ratelimited(\"QRTR clients exceed max node limit!\\n\");\n-\t\treturn NULL;\n+\t\tgoto error;\n \t}\n \n \t/* If node didn't exist, allocate and insert it to the tree */\n \tnode = kzalloc_obj(*node);\n \tif (!node)\n-\t\treturn NULL;\n+\t\tgoto error;\n \n \tnode-\u003eid = node_id;\n \txa_init(\u0026node-\u003eservers);\n \n-\tif (xa_store(\u0026nodes, node_id, node, GFP_KERNEL)) {\n+\tif (xa_store(\u0026endpoint-\u003enodes, node_id, node, GFP_KERNEL)) {\n \t\tkfree(node);\n-\t\treturn NULL;\n+\t\tgoto error;\n \t}\n \n \tnode_count++;\n \n \treturn node;\n+error:\n+\tif (new_endpoint) {\n+\t\txa_erase(\u0026endpoints, endpoint_id);\n+\t\tkfree(endpoint);\n+\t}\n+\n+\treturn NULL;\n+}\n+\n+static void node_erase(u32 endpoint_id, unsigned int node_id)\n+{\n+\tstruct qrtr_ns_endpoint *endpoint;\n+\tstruct qrtr_node *node;\n+\n+\tendpoint = xa_load(\u0026endpoints, endpoint_id);\n+\tif (!endpoint)\n+\t\treturn;\n+\n+\tnode = xa_load(\u0026endpoint-\u003enodes, node_id);\n+\tif (!node)\n+\t\treturn;\n+\n+\txa_erase(\u0026endpoint-\u003enodes, node_id);\n+\tkfree(node);\n+\tnode_count--;\n+}\n+\n+static struct qrtr_node *node_lookup(u32 endpoint_id, unsigned int node_id)\n+{\n+\tstruct qrtr_ns_endpoint *endpoint;\n+\n+\tendpoint = xa_load(\u0026endpoints, endpoint_id);\n+\tif (!endpoint)\n+\t\treturn NULL;\n+\n+\treturn xa_load(\u0026endpoint-\u003enodes, node_id);\n }\n \n static int server_match(const struct qrtr_server *srv,\n@@ -126,19 +188,42 @@ static int server_match(const struct qrtr_server *srv,\n \treturn (srv-\u003einstance \u0026 ifilter) == f-\u003einstance;\n }\n \n-static int service_announce_new(struct sockaddr_qrtr *dest,\n-\t\t\t\tstruct qrtr_server *srv)\n+static int qrtr_ns_sendmsg(u32 endpoint_id, struct sockaddr_qrtr *dest,\n+\t\t\t struct qrtr_ctrl_pkt *pkt)\n {\n-\tstruct qrtr_ctrl_pkt pkt;\n \tstruct msghdr msg = { };\n \tstruct kvec iv;\n+\tu8 control[CMSG_SPACE(sizeof(endpoint_id))];\n+\tstruct cmsghdr *cmsg;\n+\n+\tiv.iov_base = pkt;\n+\tiv.iov_len = sizeof(*pkt);\n+\n+\tmsg.msg_name = (struct sockaddr *)dest;\n+\tmsg.msg_namelen = sizeof(*dest);\n+\n+\tif (endpoint_id) {\n+\t\tmsg.msg_control = control;\n+\t\tmsg.msg_controllen = sizeof(control);\n+\n+\t\tcmsg = CMSG_FIRSTHDR(\u0026msg);\n+\t\tcmsg-\u003ecmsg_level = SOL_QRTR;\n+\t\tcmsg-\u003ecmsg_type = QRTR_ENDPOINT;\n+\t\tcmsg-\u003ecmsg_len = CMSG_LEN(sizeof(endpoint_id));\n+\t\tmemcpy(CMSG_DATA(cmsg), \u0026endpoint_id, sizeof(endpoint_id));\n+\t}\n+\n+\treturn kernel_sendmsg(qrtr_ns.sock, \u0026msg, \u0026iv, 1, sizeof(*pkt));\n+}\n+\n+static int service_announce_new(u32 endpoint_id, struct sockaddr_qrtr *dest,\n+\t\t\t\tstruct qrtr_server *srv)\n+{\n+\tstruct qrtr_ctrl_pkt pkt;\n \n \ttrace_qrtr_ns_service_announce_new(srv-\u003eservice, srv-\u003einstance,\n \t\t\t\t\t srv-\u003enode, srv-\u003eport);\n \n-\tiv.iov_base = \u0026pkt;\n-\tiv.iov_len = sizeof(pkt);\n-\n \tmemset(\u0026pkt, 0, sizeof(pkt));\n \tpkt.cmd = cpu_to_le32(QRTR_TYPE_NEW_SERVER);\n \tpkt.server.service = cpu_to_le32(srv-\u003eservice);\n@@ -146,26 +231,18 @@ static int service_announce_new(struct sockaddr_qrtr *dest,\n \tpkt.server.node = cpu_to_le32(srv-\u003enode);\n \tpkt.server.port = cpu_to_le32(srv-\u003eport);\n \n-\tmsg.msg_name = (struct sockaddr *)dest;\n-\tmsg.msg_namelen = sizeof(*dest);\n-\n-\treturn kernel_sendmsg(qrtr_ns.sock, \u0026msg, \u0026iv, 1, sizeof(pkt));\n+\treturn qrtr_ns_sendmsg(endpoint_id, dest, \u0026pkt);\n }\n \n-static void service_announce_del(struct sockaddr_qrtr *dest,\n+static void service_announce_del(u32 endpoint_id, struct sockaddr_qrtr *dest,\n \t\t\t\t struct qrtr_server *srv)\n {\n \tstruct qrtr_ctrl_pkt pkt;\n-\tstruct msghdr msg = { };\n-\tstruct kvec iv;\n \tint ret;\n \n \ttrace_qrtr_ns_service_announce_del(srv-\u003eservice, srv-\u003einstance,\n \t\t\t\t\t srv-\u003enode, srv-\u003eport);\n \n-\tiv.iov_base = \u0026pkt;\n-\tiv.iov_len = sizeof(pkt);\n-\n \tmemset(\u0026pkt, 0, sizeof(pkt));\n \tpkt.cmd = cpu_to_le32(QRTR_TYPE_DEL_SERVER);\n \tpkt.server.service = cpu_to_le32(srv-\u003eservice);\n@@ -173,26 +250,24 @@ static void service_announce_del(struct sockaddr_qrtr *dest,\n \tpkt.server.node = cpu_to_le32(srv-\u003enode);\n \tpkt.server.port = cpu_to_le32(srv-\u003eport);\n \n-\tmsg.msg_name = (struct sockaddr *)dest;\n-\tmsg.msg_namelen = sizeof(*dest);\n-\n-\tret = kernel_sendmsg(qrtr_ns.sock, \u0026msg, \u0026iv, 1, sizeof(pkt));\n+\tret = qrtr_ns_sendmsg(endpoint_id, dest, \u0026pkt);\n \tif (ret \u003c 0 \u0026\u0026 ret != -ENODEV)\n \t\tpr_err(\"failed to announce del service\\n\");\n \n \treturn;\n }\n \n-static void lookup_notify(struct sockaddr_qrtr *to, struct qrtr_server *srv,\n-\t\t\t bool new)\n+static void lookup_notify(u32 endpoint_id, struct sockaddr_qrtr *to,\n+\t\t\t struct qrtr_server *srv, bool new)\n {\n \tstruct qrtr_ctrl_pkt pkt;\n-\tstruct msghdr msg = { };\n-\tstruct kvec iv;\n \tint ret;\n \n-\tiv.iov_base = \u0026pkt;\n-\tiv.iov_len = sizeof(pkt);\n+\t/*\n+\t * Notify a local client at @to about a server change. The aux data\n+\t * will look as if it came from the endpoint that reported the event\n+\t * (remote or local)\n+\t */\n \n \tmemset(\u0026pkt, 0, sizeof(pkt));\n \tpkt.cmd = new ? cpu_to_le32(QRTR_TYPE_NEW_SERVER) :\n@@ -204,28 +279,25 @@ static void lookup_notify(struct sockaddr_qrtr *to, struct qrtr_server *srv,\n \t\tpkt.server.port = cpu_to_le32(srv-\u003eport);\n \t}\n \n-\tmsg.msg_name = (struct sockaddr *)to;\n-\tmsg.msg_namelen = sizeof(*to);\n-\n-\tret = kernel_sendmsg(qrtr_ns.sock, \u0026msg, \u0026iv, 1, sizeof(pkt));\n+\tret = qrtr_ns_sendmsg(endpoint_id, to, \u0026pkt);\n \tif (ret \u003c 0 \u0026\u0026 ret != -ENODEV)\n \t\tpr_err(\"failed to send lookup notification\\n\");\n }\n \n-static int announce_servers(struct sockaddr_qrtr *sq)\n+static int announce_servers(u32 endpoint_id, struct sockaddr_qrtr *sq)\n {\n \tstruct qrtr_server *srv;\n \tstruct qrtr_node *node;\n \tunsigned long index;\n \tint ret;\n \n-\tnode = node_get(qrtr_ns.local_node);\n+\tnode = node_lookup(qrtr_ns.local_node, qrtr_ns.local_node);\n \tif (!node)\n \t\treturn 0;\n \n-\t/* Announce the list of servers registered in this node */\n+\t/* Announce servers registered on local endpoint to remote endpoint */\n \txa_for_each(\u0026node-\u003eservers, index, srv) {\n-\t\tret = service_announce_new(sq, srv);\n+\t\tret = service_announce_new(endpoint_id, sq, srv);\n \t\tif (ret \u003c 0) {\n \t\t\tif (ret == -ENODEV)\n \t\t\t\tcontinue;\n@@ -237,7 +309,8 @@ static int announce_servers(struct sockaddr_qrtr *sq)\n \treturn 0;\n }\n \n-static struct qrtr_server *server_add(unsigned int service,\n+static struct qrtr_server *server_add(u32 endpoint_id,\n+\t\t\t\t unsigned int service,\n \t\t\t\t unsigned int instance,\n \t\t\t\t unsigned int node_id,\n \t\t\t\t unsigned int port)\n@@ -249,7 +322,7 @@ static struct qrtr_server *server_add(unsigned int service,\n \tif (!service || !port)\n \t\treturn NULL;\n \n-\tnode = node_get(node_id);\n+\tnode = node_get(endpoint_id, node_id);\n \tif (!node)\n \t\treturn NULL;\n \n@@ -293,7 +366,8 @@ static struct qrtr_server *server_add(unsigned int service,\n \treturn NULL;\n }\n \n-static int server_del(struct qrtr_node *node, unsigned int port, bool bcast)\n+static int server_del(u32 endpoint_id, struct qrtr_node *node,\n+\t\t unsigned int port, bool bcast)\n {\n \tstruct qrtr_lookup *lookup;\n \tstruct qrtr_server *srv;\n@@ -305,9 +379,10 @@ static int server_del(struct qrtr_node *node, unsigned int port, bool bcast)\n \n \txa_erase(\u0026node-\u003eservers, port);\n \n-\t/* Broadcast the removal of local servers */\n+\t/* Broadcast the removal of local servers to remote endpoints */\n \tif (srv-\u003enode == qrtr_ns.local_node \u0026\u0026 bcast)\n-\t\tservice_announce_del(\u0026qrtr_ns.bcast_sq, srv);\n+\t\tservice_announce_del(qrtr_ns.local_node,\n+\t\t\t\t \u0026qrtr_ns.bcast_sq, srv);\n \n \t/* Announce the service's disappearance to observers */\n \tlist_for_each(li, \u0026qrtr_ns.lookups) {\n@@ -317,7 +392,7 @@ static int server_del(struct qrtr_node *node, unsigned int port, bool bcast)\n \t\tif (lookup-\u003einstance \u0026\u0026 lookup-\u003einstance != srv-\u003einstance)\n \t\t\tcontinue;\n \n-\t\tlookup_notify(\u0026lookup-\u003esq, srv, false);\n+\t\tlookup_notify(endpoint_id, \u0026lookup-\u003esq, srv, false);\n \t}\n \n \tkfree(srv);\n@@ -326,23 +401,15 @@ static int server_del(struct qrtr_node *node, unsigned int port, bool bcast)\n \treturn 0;\n }\n \n-static int say_hello(struct sockaddr_qrtr *dest)\n+static int say_hello(u32 endpoint_id, struct sockaddr_qrtr *dest)\n {\n \tstruct qrtr_ctrl_pkt pkt;\n-\tstruct msghdr msg = { };\n-\tstruct kvec iv;\n \tint ret;\n \n-\tiv.iov_base = \u0026pkt;\n-\tiv.iov_len = sizeof(pkt);\n-\n \tmemset(\u0026pkt, 0, sizeof(pkt));\n \tpkt.cmd = cpu_to_le32(QRTR_TYPE_HELLO);\n \n-\tmsg.msg_name = (struct sockaddr *)dest;\n-\tmsg.msg_namelen = sizeof(*dest);\n-\n-\tret = kernel_sendmsg(qrtr_ns.sock, \u0026msg, \u0026iv, 1, sizeof(pkt));\n+\tret = qrtr_ns_sendmsg(endpoint_id, dest, \u0026pkt);\n \tif (ret \u003c 0)\n \t\tpr_err(\"failed to send hello msg\\n\");\n \n@@ -350,42 +417,38 @@ static int say_hello(struct sockaddr_qrtr *dest)\n }\n \n /* Announce the list of servers registered on the local node */\n-static int ctrl_cmd_hello(struct sockaddr_qrtr *sq)\n+static int ctrl_cmd_hello(u32 endpoint_id, struct sockaddr_qrtr *sq)\n {\n \tint ret;\n \n-\tret = say_hello(sq);\n+\t/* Send Hello and New Server messages to remote endpoint */\n+\tret = say_hello(endpoint_id, sq);\n \tif (ret \u003c 0)\n \t\treturn ret;\n \n-\treturn announce_servers(sq);\n+\treturn announce_servers(endpoint_id, sq);\n }\n \n-static int ctrl_cmd_bye(struct sockaddr_qrtr *from)\n+static int ctrl_cmd_bye(u32 endpoint_id, struct sockaddr_qrtr *from)\n {\n \tstruct qrtr_node *local_node;\n \tstruct qrtr_ctrl_pkt pkt;\n \tstruct qrtr_server *srv;\n \tstruct sockaddr_qrtr sq;\n-\tstruct msghdr msg = { };\n \tstruct qrtr_node *node;\n \tunsigned long index;\n-\tstruct kvec iv;\n \tint ret = 0;\n \n-\tiv.iov_base = \u0026pkt;\n-\tiv.iov_len = sizeof(pkt);\n-\n-\tnode = node_get(from-\u003esq_node);\n+\tnode = node_get(endpoint_id, from-\u003esq_node);\n \tif (!node)\n \t\treturn 0;\n \n \t/* Advertise removal of this client to all servers of remote node */\n \txa_for_each(\u0026node-\u003eservers, index, srv)\n-\t\tserver_del(node, srv-\u003eport, true);\n+\t\tserver_del(endpoint_id, node, srv-\u003eport, true);\n \n \t/* Advertise the removal of this client to all local servers */\n-\tlocal_node = node_get(qrtr_ns.local_node);\n+\tlocal_node = node_get(qrtr_ns.local_node, qrtr_ns.local_node);\n \tif (!local_node) {\n \t\tret = 0;\n \t\tgoto delete_node;\n@@ -400,10 +463,8 @@ static int ctrl_cmd_bye(struct sockaddr_qrtr *from)\n \t\tsq.sq_node = srv-\u003enode;\n \t\tsq.sq_port = srv-\u003eport;\n \n-\t\tmsg.msg_name = (struct sockaddr *)\u0026sq;\n-\t\tmsg.msg_namelen = sizeof(sq);\n-\n-\t\tret = kernel_sendmsg(qrtr_ns.sock, \u0026msg, \u0026iv, 1, sizeof(pkt));\n+\t\t/* Bye will look as if it came from endpoint_id */\n+\t\tret = qrtr_ns_sendmsg(endpoint_id, \u0026sq, \u0026pkt);\n \t\tif (ret \u003c 0 \u0026\u0026 ret != -ENODEV) {\n \t\t\tpr_err(\"failed to send bye cmd\\n\");\n \t\t\tgoto delete_node;\n@@ -414,32 +475,25 @@ static int ctrl_cmd_bye(struct sockaddr_qrtr *from)\n \tret = 0;\n \n delete_node:\n-\txa_erase(\u0026nodes, from-\u003esq_node);\n-\tkfree(node);\n-\tnode_count--;\n+\tnode_erase(endpoint_id, from-\u003esq_node);\n \n \treturn ret;\n }\n \n-static int ctrl_cmd_del_client(struct sockaddr_qrtr *from,\n+static int ctrl_cmd_del_client(u32 endpoint_id, struct sockaddr_qrtr *from,\n \t\t\t unsigned int node_id, unsigned int port)\n {\n \tstruct qrtr_node *local_node;\n \tstruct qrtr_lookup *lookup;\n \tstruct qrtr_ctrl_pkt pkt;\n-\tstruct msghdr msg = { };\n \tstruct qrtr_server *srv;\n \tstruct sockaddr_qrtr sq;\n \tstruct qrtr_node *node;\n \tstruct list_head *tmp;\n \tstruct list_head *li;\n \tunsigned long index;\n-\tstruct kvec iv;\n \tint ret;\n \n-\tiv.iov_base = \u0026pkt;\n-\tiv.iov_len = sizeof(pkt);\n-\n \t/* Don't accept spoofed messages */\n \tif (from-\u003esq_node != node_id)\n \t\treturn -EINVAL;\n@@ -465,12 +519,12 @@ static int ctrl_cmd_del_client(struct sockaddr_qrtr *from,\n \t * DEL_SERVER. Neighbours would've already removed the server belonging\n \t * to this port due to the DEL_CLIENT broadcast from qrtr_port_remove().\n \t */\n-\tnode = node_get(node_id);\n+\tnode = node_lookup(endpoint_id, node_id);\n \tif (node)\n-\t\tserver_del(node, port, false);\n+\t\tserver_del(endpoint_id, node, port, false);\n \n \t/* Advertise the removal of this client to all local servers */\n-\tlocal_node = node_get(qrtr_ns.local_node);\n+\tlocal_node = node_lookup(qrtr_ns.local_node, qrtr_ns.local_node);\n \tif (!local_node)\n \t\treturn 0;\n \n@@ -484,10 +538,8 @@ static int ctrl_cmd_del_client(struct sockaddr_qrtr *from,\n \t\tsq.sq_node = srv-\u003enode;\n \t\tsq.sq_port = srv-\u003eport;\n \n-\t\tmsg.msg_name = (struct sockaddr *)\u0026sq;\n-\t\tmsg.msg_namelen = sizeof(sq);\n-\n-\t\tret = kernel_sendmsg(qrtr_ns.sock, \u0026msg, \u0026iv, 1, sizeof(pkt));\n+\t\t/* Del Client will look as if it came from endpoint_id */\n+\t\tret = qrtr_ns_sendmsg(endpoint_id, \u0026sq, \u0026pkt);\n \t\tif (ret \u003c 0 \u0026\u0026 ret != -ENODEV) {\n \t\t\tpr_err(\"failed to send del client cmd\\n\");\n \t\t\treturn ret;\n@@ -496,7 +548,7 @@ static int ctrl_cmd_del_client(struct sockaddr_qrtr *from,\n \treturn 0;\n }\n \n-static int ctrl_cmd_new_server(struct sockaddr_qrtr *from,\n+static int ctrl_cmd_new_server(u32 endpoint_id, struct sockaddr_qrtr *from,\n \t\t\t unsigned int service, unsigned int instance,\n \t\t\t unsigned int node_id, unsigned int port)\n {\n@@ -511,12 +563,16 @@ static int ctrl_cmd_new_server(struct sockaddr_qrtr *from,\n \t\tport = from-\u003esq_port;\n \t}\n \n-\tsrv = server_add(service, instance, node_id, port);\n-\tif (!srv)\n+\tsrv = server_add(endpoint_id, service, instance, node_id, port);\n+\tif (!srv) {\n+\t\tpr_err(\"Failed to add server\\n\");\n \t\treturn -EINVAL;\n+\t}\n \n \tif (srv-\u003enode == qrtr_ns.local_node) {\n-\t\tret = service_announce_new(\u0026qrtr_ns.bcast_sq, srv);\n+\t\t/* Broadcast local server info to all peer endpoints */\n+\t\tret = service_announce_new(qrtr_ns.local_node,\n+\t\t\t\t\t \u0026qrtr_ns.bcast_sq, srv);\n \t\tif (ret \u003c 0) {\n \t\t\tpr_err(\"failed to announce new service\\n\");\n \t\t\treturn ret;\n@@ -531,13 +587,13 @@ static int ctrl_cmd_new_server(struct sockaddr_qrtr *from,\n \t\tif (lookup-\u003einstance \u0026\u0026 lookup-\u003einstance != instance)\n \t\t\tcontinue;\n \n-\t\tlookup_notify(\u0026lookup-\u003esq, srv, true);\n+\t\tlookup_notify(endpoint_id, \u0026lookup-\u003esq, srv, true);\n \t}\n \n \treturn ret;\n }\n \n-static int ctrl_cmd_del_server(struct sockaddr_qrtr *from,\n+static int ctrl_cmd_del_server(u32 endpoint_id, struct sockaddr_qrtr *from,\n \t\t\t unsigned int service, unsigned int instance,\n \t\t\t unsigned int node_id, unsigned int port)\n {\n@@ -553,24 +609,22 @@ static int ctrl_cmd_del_server(struct sockaddr_qrtr *from,\n \tif (from-\u003esq_node == qrtr_ns.local_node \u0026\u0026 from-\u003esq_port != port)\n \t\treturn -EINVAL;\n \n-\tnode = node_get(node_id);\n+\tnode = node_lookup(endpoint_id, node_id);\n \tif (!node)\n \t\treturn -ENOENT;\n \n-\tserver_del(node, port, true);\n+\tserver_del(endpoint_id, node, port, true);\n \n \treturn 0;\n }\n \n-static int ctrl_cmd_new_lookup(struct sockaddr_qrtr *from,\n+static int ctrl_cmd_new_lookup(u32 endpoint_id, struct sockaddr_qrtr *from,\n \t\t\t unsigned int service, unsigned int instance)\n {\n \tstruct qrtr_server_filter filter;\n \tstruct qrtr_lookup *lookup;\n-\tstruct qrtr_server *srv;\n-\tstruct qrtr_node *node;\n-\tunsigned long node_idx;\n-\tunsigned long srv_idx;\n+\tunsigned long id;\n+\tstruct qrtr_ns_endpoint *endpoint;\n \n \t/* Accept only local observers */\n \tif (from-\u003esq_node != qrtr_ns.local_node)\n@@ -595,22 +649,30 @@ static int ctrl_cmd_new_lookup(struct sockaddr_qrtr *from,\n \tfilter.service = service;\n \tfilter.instance = instance;\n \n-\txa_for_each(\u0026nodes, node_idx, node) {\n-\t\txa_for_each(\u0026node-\u003eservers, srv_idx, srv) {\n-\t\t\tif (!server_match(srv, \u0026filter))\n-\t\t\t\tcontinue;\n+\txa_for_each(\u0026endpoints, id, endpoint) {\n+\t\tstruct qrtr_node *node;\n+\t\tunsigned long node_idx;\n \n-\t\t\tlookup_notify(from, srv, true);\n+\t\txa_for_each(\u0026endpoint-\u003enodes, node_idx, node) {\n+\t\t\tunsigned long srv_idx;\n+\t\t\tstruct qrtr_server *srv;\n+\n+\t\t\txa_for_each(\u0026node-\u003eservers, srv_idx, srv) {\n+\t\t\t\tif (!server_match(srv, \u0026filter))\n+\t\t\t\t\tcontinue;\n+\n+\t\t\t\tlookup_notify(id, from, srv, true);\n+\t\t\t}\n \t\t}\n \t}\n \n \t/* Empty notification, to indicate end of listing */\n-\tlookup_notify(from, NULL, true);\n+\tlookup_notify(0, from, NULL, true);\n \n \treturn 0;\n }\n \n-static void ctrl_cmd_del_lookup(struct sockaddr_qrtr *from,\n+static void ctrl_cmd_del_lookup(u32 endpoint_id, struct sockaddr_qrtr *from,\n \t\t\t\tunsigned int service, unsigned int instance)\n {\n \tstruct qrtr_lookup *lookup;\n@@ -644,6 +706,7 @@ static void qrtr_ns_worker(struct work_struct *work)\n \tssize_t msglen;\n \tvoid *recv_buf;\n \tstruct kvec iv;\n+\tu8 control[32];\n \tint ret;\n \n \tmsg.msg_name = (struct sockaddr *)\u0026sq;\n@@ -654,8 +717,12 @@ static void qrtr_ns_worker(struct work_struct *work)\n \t\treturn;\n \n \tfor (;;) {\n+\t\tu32 endpoint_id;\n+\n \t\tiv.iov_base = recv_buf;\n \t\tiv.iov_len = recv_buf_size;\n+\t\tmsg.msg_control = control;\n+\t\tmsg.msg_controllen = sizeof(control);\n \n \t\tmsglen = kernel_recvmsg(qrtr_ns.sock, \u0026msg, \u0026iv, 1,\n \t\t\t\t\tiv.iov_len, MSG_DONTWAIT);\n@@ -668,6 +735,19 @@ static void qrtr_ns_worker(struct work_struct *work)\n \t\t\tbreak;\n \t\t}\n \n+\t\t/* AUX data is written direct into the control buffer */\n+\t\tmsg.msg_control = control;\n+\t\tmsg.msg_controllen = sizeof(control) - msg.msg_controllen;\n+\n+\t\tret = qrtr_msg_get_endpoint(\u0026msg, \u0026endpoint_id);\n+\t\tif (ret \u003c 0) {\n+\t\t\tpr_err(\"error receiving endpoint id: %d\\n\", ret);\n+\t\t\tbreak;\n+\t\t}\n+\n+\t\tif ((size_t)msglen \u003c sizeof(*pkt))\n+\t\t\tbreak;\n+\n \t\tpkt = recv_buf;\n \t\tcmd = le32_to_cpu(pkt-\u003ecmd);\n \t\tif (cmd \u003c ARRAY_SIZE(qrtr_ctrl_pkt_strings) \u0026\u0026\n@@ -678,25 +758,25 @@ static void qrtr_ns_worker(struct work_struct *work)\n \t\tret = 0;\n \t\tswitch (cmd) {\n \t\tcase QRTR_TYPE_HELLO:\n-\t\t\tret = ctrl_cmd_hello(\u0026sq);\n+\t\t\tret = ctrl_cmd_hello(endpoint_id, \u0026sq);\n \t\t\tbreak;\n \t\tcase QRTR_TYPE_BYE:\n-\t\t\tret = ctrl_cmd_bye(\u0026sq);\n+\t\t\tret = ctrl_cmd_bye(endpoint_id, \u0026sq);\n \t\t\tbreak;\n \t\tcase QRTR_TYPE_DEL_CLIENT:\n-\t\t\tret = ctrl_cmd_del_client(\u0026sq,\n+\t\t\tret = ctrl_cmd_del_client(endpoint_id, \u0026sq,\n \t\t\t\t\tle32_to_cpu(pkt-\u003eclient.node),\n \t\t\t\t\tle32_to_cpu(pkt-\u003eclient.port));\n \t\t\tbreak;\n \t\tcase QRTR_TYPE_NEW_SERVER:\n-\t\t\tret = ctrl_cmd_new_server(\u0026sq,\n+\t\t\tret = ctrl_cmd_new_server(endpoint_id, \u0026sq,\n \t\t\t\t\tle32_to_cpu(pkt-\u003eserver.service),\n \t\t\t\t\tle32_to_cpu(pkt-\u003eserver.instance),\n \t\t\t\t\tle32_to_cpu(pkt-\u003eserver.node),\n \t\t\t\t\tle32_to_cpu(pkt-\u003eserver.port));\n \t\t\tbreak;\n \t\tcase QRTR_TYPE_DEL_SERVER:\n-\t\t\tret = ctrl_cmd_del_server(\u0026sq,\n+\t\t\tret = ctrl_cmd_del_server(endpoint_id, \u0026sq,\n \t\t\t\t\t le32_to_cpu(pkt-\u003eserver.service),\n \t\t\t\t\t le32_to_cpu(pkt-\u003eserver.instance),\n \t\t\t\t\t le32_to_cpu(pkt-\u003eserver.node),\n@@ -707,12 +787,12 @@ static void qrtr_ns_worker(struct work_struct *work)\n \t\tcase QRTR_TYPE_RESUME_TX:\n \t\t\tbreak;\n \t\tcase QRTR_TYPE_NEW_LOOKUP:\n-\t\t\tret = ctrl_cmd_new_lookup(\u0026sq,\n+\t\t\tret = ctrl_cmd_new_lookup(endpoint_id, \u0026sq,\n \t\t\t\t\t le32_to_cpu(pkt-\u003eserver.service),\n \t\t\t\t\t le32_to_cpu(pkt-\u003eserver.instance));\n \t\t\tbreak;\n \t\tcase QRTR_TYPE_DEL_LOOKUP:\n-\t\t\tctrl_cmd_del_lookup(\u0026sq,\n+\t\t\tctrl_cmd_del_lookup(endpoint_id, \u0026sq,\n \t\t\t\t le32_to_cpu(pkt-\u003eserver.service),\n \t\t\t\t le32_to_cpu(pkt-\u003eserver.instance));\n \t\t\tbreak;\n@@ -746,6 +826,8 @@ int qrtr_ns_init(void)\n \tif (ret \u003c 0)\n \t\treturn ret;\n \n+\tqrtr_sock_set_report_endpoint(qrtr_ns.sock-\u003esk);\n+\n \tret = kernel_getsockname(qrtr_ns.sock, (struct sockaddr *)\u0026sq);\n \tif (ret \u003c 0) {\n \t\tpr_err(\"failed to get socket name\\n\");\n@@ -774,7 +856,7 @@ int qrtr_ns_init(void)\n \tqrtr_ns.bcast_sq.sq_node = QRTR_NODE_BCAST;\n \tqrtr_ns.bcast_sq.sq_port = QRTR_PORT_CTRL;\n \n-\tret = say_hello(\u0026qrtr_ns.bcast_sq);\n+\tret = say_hello(qrtr_ns.local_node, \u0026qrtr_ns.bcast_sq);\n \tif (ret \u003c 0)\n \t\tgoto err_wq;\n \ndiff --git a/net/qrtr/qrtr.h b/net/qrtr/qrtr.h\nindex 3f2d28696062a..affc24f426c64 100644\n--- a/net/qrtr/qrtr.h\n+++ b/net/qrtr/qrtr.h\n@@ -9,6 +9,15 @@ struct sk_buff;\n /* endpoint node id auto assignment */\n #define QRTR_EP_NID_AUTO (-1)\n \n+/**\n+ * struct qrtr_node_lookup_helper - node lookup helper, stored in qrtr_nodes\n+ * @nodes: nodes, indexed by node_id\n+ */\n+struct qrtr_node_lookup_helper {\n+\tint added;\n+\tstruct radix_tree_root nodes;\n+};\n+\n /**\n * struct qrtr_endpoint - endpoint handle\n * @xmit: Callback for outgoing packets\n@@ -21,6 +30,8 @@ struct qrtr_endpoint {\n \tint (*xmit)(struct qrtr_endpoint *ep, struct sk_buff *skb);\n \t/* private: not for endpoint use */\n \tstruct qrtr_node *node;\n+\tstruct qrtr_node_lookup_helper helper;\n+\tu32 id;\n };\n \n int qrtr_endpoint_register(struct qrtr_endpoint *ep, unsigned int nid);\n@@ -33,4 +44,7 @@ int qrtr_ns_init(void);\n \n void qrtr_ns_remove(void);\n \n+int qrtr_msg_get_endpoint(struct msghdr *msg, u32 *out_endpoint_id);\n+void qrtr_sock_set_report_endpoint(struct sock *sk);\n+\n #endif\n"
} |
| 2/1 | 2026/09/02 03:18 | agent | patch-evaluator |
0mModel:core Args: null Results: {
"EnableConfigs": [
"QRTR",
"QRTR_TUN",
"QRTR_MHI"
],
"FocusSymbols": [
"qrtr_setsockopt",
"qrtr_getsockopt",
"qrtr_sendmsg",
"qrtr_recvmsg",
"qrtr_msg_get_endpoint",
"qrtr_sock_set_report_endpoint",
"qrtr_endpoint_post",
"qrtr_endpoint_register",
"qrtr_endpoint_unregister"
],
"Reasoning": "The patch adds endpoint binding and reporting support to the QRTR (Qualcomm IPC Router) subsystem. It adds setsockopt/getsockopt options at SOL_QRTR, CMSG control message handling for QRTR_ENDPOINT, and updates node lookup and name service logic with radix trees and xarrays. These features are reachable via AF_QIPCRTR sockets and QRTR tun interfaces.",
"WorthFuzzing": true
}Instruction: You are an expert Linux kernel maintainer. Your job is to review a provided patch series and determine if it makes functional changes to the kernel that should be fuzzed. IMPORTANT: The changes have ALREADY been applied and committed as the HEAD commit in your workspace. Do NOT rely on your internal knowledge of the kernel. You must actively use your code access tools to examine the actual source code and confirm any assumptions. Return WorthFuzzing=false if the patch only contains: - Modifications to Documentation/, Kconfig files, or code comments. - Purely decorative changes, such as logging (e.g., pr_err, printk) or tracepoints. - Changes to numeric constants or macros that do not functionally alter execution flow. - Code paths that are impossible to reach in virtualized environments like GCE or QEMU, even when utilizing software-emulated hardware (e.g., usb gadget, mac80211_hwsim). - Code in vendor-specific PCIe switch, SmartNIC, or GPU drivers (e.g., mlxsw, pds_core, qed, ionic, amdgpu) that require physical PCIe hardware cards not emulated in standard QEMU. - Driver .remove, .shutdown, or pci_unregister_driver teardown callbacks (e.g., igb_remove) that are executed only during PCI hot-unplug or sysfs driver unbind operations. If it modifies reachable core kernel logic, drivers, or architectures, use your code search tools to verify the code can be executed, then return WorthFuzzing=true. When returning WorthFuzzing=true, you MUST ALSO: 1. Extract any specific kernel functions that should be heavily fuzzed into FocusSymbols. Avoid listing generic hot-path functions to prevent skewed test distributions. Prefer non-static, non-inlined API entrypoint functions over internal static helper functions (which are inlined by the compiler and do not have distinct symbol addresses). 2. Identify any specific CONFIG_ options required to properly test this new/modified feature. Go and look into the Kconfig files and check for ifdefs around the code, do not make assumptions. Also check "depends on" lines in Kconfig to include any non-standard parent subsystem configs needed for Kbuild to compile the code statically into vmlinux. List them in the EnableConfigs output array, and DO NOT add a 'CONFIG_' prefix (e.g., return "NET_IPV4" instead of "CONFIG_NET_IPV4"). Prefer calling several tools at the same time to save round-trips. Use set-results tool to provide results of the analysis. It must be called exactly once before the final reply. Ignore results of this tool. Prompt: For your convenience, here is the diff of the changes:
commit 3c9f36563269acf4748064ee241321ef1673c592
Author: syz-cluster <triage@syzkaller.com>
Date: Wed Sep 2 03:18:43 2026 +0000
syz-cluster: applied patch under review
diff --git a/include/linux/socket.h b/include/linux/socket.h
index 2a8d7b14f1d11..4af1dd816dc35 100644
--- a/include/linux/socket.h
+++ b/include/linux/socket.h
@@ -400,6 +400,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 f7e2fb3d752b5..0a8667b049c3d 100644
--- a/include/uapi/linux/qrtr.h
+++ b/include/uapi/linux/qrtr.h
@@ -46,4 +46,11 @@ struct qrtr_ctrl_pkt {
};
} __packed;
+/* setsockopt / getsockopt */
+#define QRTR_REPORT_ENDPOINT 1
+#define QRTR_BIND_ENDPOINT 2
+
+/* CMSG */
+#define QRTR_ENDPOINT 1
+
#endif /* _LINUX_QRTR_H */
diff --git a/net/qrtr/af_qrtr.c b/net/qrtr/af_qrtr.c
index a30fa56e6aa31..7c50d32b11015 100644
--- a/net/qrtr/af_qrtr.c
+++ b/net/qrtr/af_qrtr.c
@@ -22,9 +22,14 @@
#define QRTR_MAX_EPH_SOCKET 0x7fff
#define QRTR_EPH_PORT_RANGE \
XA_LIMIT(QRTR_MIN_EPH_SOCKET, QRTR_MAX_EPH_SOCKET)
+#define QRTR_ENDPOINT_RANGE XA_LIMIT(qrtr_local_nid + 1, INT_MAX)
#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
@@ -78,6 +83,7 @@ struct qrtr_cb {
u32 src_port;
u32 dst_node;
u32 dst_port;
+ u32 endpoint_id;
u8 type;
u8 confirm_rx;
@@ -91,6 +97,8 @@ struct qrtr_sock {
struct sock sk;
struct sockaddr_qrtr us;
struct sockaddr_qrtr peer;
+ unsigned long flags;
+ u32 bound_endpoint;
};
static inline struct qrtr_sock *qrtr_sk(struct sock *sk)
@@ -99,6 +107,43 @@ static inline struct qrtr_sock *qrtr_sk(struct sock *sk)
return container_of(sk, struct qrtr_sock, sk);
}
+void qrtr_sock_set_report_endpoint(struct sock *sk)
+{
+ struct qrtr_sock *ipc = qrtr_sk(sk);
+
+ assign_bit(QRTR_F_REPORT_ENDPOINT, &ipc->flags, 1);
+}
+
+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 */
@@ -109,9 +154,37 @@ static LIST_HEAD(qrtr_all_nodes);
/* lock for qrtr_all_nodes and node reference */
static DEFINE_MUTEX(qrtr_node_lock);
+/* endpoint id allocation management */
+static DEFINE_XARRAY_ALLOC(qrtr_endpoints);
+static u32 next_endpoint_id;
+
/* local port allocation management */
static DEFINE_XARRAY_ALLOC(qrtr_ports);
+/* The xarray API uses fixed unsigned long keys and we will have to make
+ * do with that.
+ * These keys are often a combination of node IDs and port numbers or
+ * endpoint IDs and node IDs (all currently u32).
+ * Using the high 32 bits for the node/endpoint ID and the low 32 bits for the
+ * port number/node ID will work fine to create keys on platforms where
+ * unsigned long is 64 bits wide, but obviously is not be possible on
+ * platforms where unsigned long is smaller.
+ * Virtually split up unsigned long in half and assign the upper bits to
+ * node/endpoint IDs and the lower bits to the port number/node ID, however
+ * big that may be.
+ */
+#define QRTR_XARRAY_INDEX_BITS (8 /* CHAR_BIT */ * sizeof(unsigned long))
+#define QRTR_INDEX_HALF_BITS (QRTR_XARRAY_INDEX_BITS >> 1)
+
+#define QRTR_INDEX_HALF_UNSIGNED_MAX ((~(unsigned long)(0)) >> QRTR_INDEX_HALF_BITS)
+#define QRTR_INDEX_HALF_UNSIGNED_MIN ((unsigned long)(0))
+
+#define QRTR_INDEX_HALF_SIGNED_MAX ((long)(QRTR_INDEX_HALF_UNSIGNED_MAX) >> 1)
+#define QRTR_INDEX_HALF_SIGNED_MIN ((long)(-1) - QRTR_INDEX_HALF_SIGNED_MAX)
+
+/* endpoint not defined, i.e. endpoint 0 */
+static struct qrtr_node_lookup_helper helper0;
+
/**
* struct qrtr_node - endpoint node
* @ep_lock: lock for endpoint management and callbacks
@@ -152,9 +225,11 @@ struct qrtr_tx_flow {
#define QRTR_TX_FLOW_LOW 5
static int qrtr_local_enqueue(struct qrtr_node *node, struct sk_buff *skb,
+ u32 endpoint_id,
int type, struct sockaddr_qrtr *from,
struct sockaddr_qrtr *to);
static int qrtr_bcast_enqueue(struct qrtr_node *node, struct sk_buff *skb,
+ u32 endpoint_id,
int type, struct sockaddr_qrtr *from,
struct sockaddr_qrtr *to);
static struct qrtr_sock *qrtr_port_lookup(int port);
@@ -169,9 +244,11 @@ static void __qrtr_node_release(struct kref *kref)
{
struct qrtr_node *node = container_of(kref, struct qrtr_node, ref);
struct radix_tree_iter iter;
+ struct radix_tree_iter iter2;
struct qrtr_tx_flow *flow;
unsigned long flags;
void __rcu **slot;
+ void __rcu **slot2;
unsigned long index;
spin_lock_irqsave(&qrtr_nodes_lock, flags);
@@ -179,8 +256,18 @@ static void __qrtr_node_release(struct kref *kref)
* multiple entries pointing to our released node, delete them all.
*/
radix_tree_for_each_slot(slot, &qrtr_nodes, &iter, 0) {
- if (*slot == node)
+ struct qrtr_node_lookup_helper *helper = *slot;
+
+ if (!helper)
+ continue;
+ radix_tree_for_each_slot(slot2, &helper->nodes, &iter2, 0) {
+ if (*slot2 == node)
+ radix_tree_iter_delete(&helper->nodes, &iter2, slot2);
+ }
+ if (helper != &helper0 && radix_tree_empty(&helper->nodes)) {
radix_tree_iter_delete(&qrtr_nodes, &iter, slot);
+ helper->added = 0;
+ }
}
spin_unlock_irqrestore(&qrtr_nodes_lock, flags);
@@ -216,16 +303,23 @@ static void qrtr_node_release(struct qrtr_node *node)
* qrtr_tx_resume() - reset flow control counter
* @node: qrtr_node that the QRTR_TYPE_RESUME_TX packet arrived on
* @skb: resume_tx packet
+ *
+ * Return: 0 on success; negative error code on failure
*/
-static void qrtr_tx_resume(struct qrtr_node *node, struct sk_buff *skb)
+static int qrtr_tx_resume(struct qrtr_node *node, struct sk_buff *skb)
{
struct qrtr_ctrl_pkt *pkt = (struct qrtr_ctrl_pkt *)skb->data;
u64 remote_node = le32_to_cpu(pkt->client.node);
u32 remote_port = le32_to_cpu(pkt->client.port);
struct qrtr_tx_flow *flow;
- unsigned long key;
+ unsigned long key = 0;
+
+ /* Don't check node/port for the valid range, use only low
+ * 16 bits on 32-bit architectures.
+ */
- key = remote_node << 32 | remote_port;
+ key = ((unsigned long)(remote_node) << QRTR_INDEX_HALF_BITS) |
+ ((unsigned long)(remote_port) & QRTR_INDEX_HALF_UNSIGNED_MAX);
flow = xa_load(&node->qrtr_tx_flow, key);
if (flow) {
@@ -236,6 +330,8 @@ static void qrtr_tx_resume(struct qrtr_node *node, struct sk_buff *skb)
}
consume_skb(skb);
+
+ return 0;
}
/**
@@ -256,11 +352,18 @@ static void qrtr_tx_resume(struct qrtr_node *node, struct sk_buff *skb)
static int qrtr_tx_wait(struct qrtr_node *node, int dest_node, int dest_port,
int type)
{
- unsigned long key = (u64)dest_node << 32 | dest_port;
+ unsigned long key = 0;
struct qrtr_tx_flow *flow;
int confirm_rx = 0;
int ret;
+ /* Don't check node/port for the valid range, use only low
+ * 16 bits on 32-bit architectures.
+ */
+
+ key = ((unsigned long)(dest_node) << QRTR_INDEX_HALF_BITS) |
+ ((unsigned long)(dest_port) & QRTR_INDEX_HALF_UNSIGNED_MAX);
+
/* Never set confirm_rx on non-data packets */
if (type != QRTR_TYPE_DATA)
return 0;
@@ -317,23 +420,35 @@ static int qrtr_tx_wait(struct qrtr_node *node, int dest_node, int dest_port,
* message associated with the dropped confirm_rx message.
* Work around this by marking the flow as having a failed transmission and
* cause the next transmission attempt to be sent with the confirm_rx.
+ *
+ * Return: 0 on success; negative error code on failure
*/
-static void qrtr_tx_flow_failed(struct qrtr_node *node, int dest_node,
- int dest_port)
+static int qrtr_tx_flow_failed(struct qrtr_node *node, int dest_node,
+ int dest_port)
{
- unsigned long key = (u64)dest_node << 32 | dest_port;
+ unsigned long key = 0;
struct qrtr_tx_flow *flow;
+ /* Don't check node/port for the valid range, use only low
+ * 16 bits on 32-bit architectures.
+ */
+
+ key = ((unsigned long)(dest_node) << QRTR_INDEX_HALF_BITS) |
+ ((unsigned long)(dest_port) & QRTR_INDEX_HALF_UNSIGNED_MAX);
+
flow = xa_load(&node->qrtr_tx_flow, key);
if (flow) {
spin_lock_irq(&flow->resume_tx.lock);
flow->tx_failed = 1;
spin_unlock_irq(&flow->resume_tx.lock);
}
+
+ return 0;
}
/* Pass an outgoing packet socket buffer to the endpoint driver. */
static int qrtr_node_enqueue(struct qrtr_node *node, struct sk_buff *skb,
+ u32 endpoint_id,
int type, struct sockaddr_qrtr *from,
struct sockaddr_qrtr *to)
{
@@ -377,7 +492,7 @@ static int qrtr_node_enqueue(struct qrtr_node *node, struct sk_buff *skb,
/* Need to ensure that a subsequent message carries the otherwise lost
* confirm_rx flag if we dropped this one */
if (rc && confirm_rx)
- qrtr_tx_flow_failed(node, to->sq_node, to->sq_port);
+ rc = qrtr_tx_flow_failed(node, to->sq_node, to->sq_port);
return rc;
}
@@ -386,14 +501,18 @@ 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;
+ struct qrtr_node *node = NULL;
+ struct qrtr_node_lookup_helper *helper = NULL;
unsigned long flags;
mutex_lock(&qrtr_node_lock);
spin_lock_irqsave(&qrtr_nodes_lock, flags);
- node = radix_tree_lookup(&qrtr_nodes, nid);
+ helper = radix_tree_lookup(&qrtr_nodes, endpoint_id);
+ if (helper)
+ node = radix_tree_lookup(&helper->nodes, nid);
node = qrtr_node_acquire(node);
spin_unlock_irqrestore(&qrtr_nodes_lock, flags);
mutex_unlock(&qrtr_node_lock);
@@ -405,19 +524,51 @@ static struct qrtr_node *qrtr_node_lookup(unsigned int nid)
*
* This is mostly useful for automatic node id assignment, based on
* the source id in the incoming packet.
+ *
+ * Return: 0 on success; negative error code on failure
*/
-static void qrtr_node_assign(struct qrtr_node *node, unsigned int nid)
+static int qrtr_node_assign(struct qrtr_node *node, unsigned int nid)
{
unsigned long flags;
+ int rc = 0;
+ int did_insert0 = 0;
if (nid == QRTR_EP_NID_AUTO)
- return;
+ return 0;
spin_lock_irqsave(&qrtr_nodes_lock, flags);
- radix_tree_insert(&qrtr_nodes, nid, node);
+
+ if (!radix_tree_lookup(&helper0.nodes, nid)) {
+ rc = radix_tree_insert(&helper0.nodes, nid, node);
+ if (rc)
+ goto err_lock;
+ did_insert0 = 1;
+ }
+ if (!node->ep->helper.added) {
+ INIT_RADIX_TREE(&node->ep->helper.nodes, GFP_ATOMIC);
+ rc = radix_tree_insert(&qrtr_nodes, node->ep->id,
+ &node->ep->helper);
+ if (rc)
+ goto err_insert0;
+ node->ep->helper.added = 1;
+ }
+ rc = radix_tree_insert(&node->ep->helper.nodes, nid, node);
+ if (rc && rc != -EEXIST)
+ goto err_insert0; // Don't revert helper insertion
+
if (node->nid == QRTR_EP_NID_AUTO)
node->nid = nid;
spin_unlock_irqrestore(&qrtr_nodes_lock, flags);
+
+ return 0;
+
+err_insert0:
+ if (did_insert0)
+ radix_tree_delete(&helper0.nodes, nid);
+
+err_lock:
+ spin_unlock_irqrestore(&qrtr_nodes_lock, flags);
+ return rc;
}
/**
@@ -439,6 +590,7 @@ int qrtr_endpoint_post(struct qrtr_endpoint *ep, const void *data, size_t len)
size_t size;
unsigned int ver;
size_t hdrlen;
+ int ret = -EINVAL;
if (len == 0 || len & 3)
return -EINVAL;
@@ -496,9 +648,16 @@ 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 || size > len || len != ALIGN(size, 4) + hdrlen)
goto err;
+ /* Don't allow remote lookups */
+ if (cb->type == QRTR_TYPE_NEW_LOOKUP ||
+ cb->type == QRTR_TYPE_DEL_LOOKUP)
+ goto err;
+
if ((cb->type == QRTR_TYPE_NEW_SERVER ||
cb->type == QRTR_TYPE_RESUME_TX) &&
size < sizeof(struct qrtr_ctrl_pkt))
@@ -510,26 +669,39 @@ int qrtr_endpoint_post(struct qrtr_endpoint *ep, const void *data, size_t len)
skb_put_data(skb, data + hdrlen, size);
- qrtr_node_assign(node, cb->src_node);
+ ret = qrtr_node_assign(node, cb->src_node);
+ if (ret)
+ goto err;
if (cb->type == QRTR_TYPE_NEW_SERVER) {
/* Remote node endpoint can bridge other distant nodes */
const struct qrtr_ctrl_pkt *pkt;
pkt = data + hdrlen;
- qrtr_node_assign(node, le32_to_cpu(pkt->server.node));
+ ret = qrtr_node_assign(node, le32_to_cpu(pkt->server.node));
+ if (ret)
+ goto err;
}
if (cb->type == QRTR_TYPE_RESUME_TX) {
- qrtr_tx_resume(node, skb);
+ ret = qrtr_tx_resume(node, skb);
+ if (ret)
+ goto err;
} else {
ipc = qrtr_port_lookup(cb->dst_port);
- if (!ipc)
+ if (!ipc) {
+ ret = -EINVAL;
goto err;
+ }
- if (sock_queue_rcv_skb(&ipc->sk, skb)) {
- qrtr_port_put(ipc);
- goto err;
+ /* Sockets bound to an endpoint only rx from that endpoint */
+ if (!ipc->bound_endpoint ||
+ ipc->bound_endpoint == cb->endpoint_id) {
+ ret = sock_queue_rcv_skb(&ipc->sk, skb);
+ if (ret) {
+ qrtr_port_put(ipc);
+ goto err;
+ }
}
qrtr_port_put(ipc);
@@ -539,7 +711,7 @@ int qrtr_endpoint_post(struct qrtr_endpoint *ep, const void *data, size_t len)
err:
kfree_skb(skb);
- return -EINVAL;
+ return ret;
}
EXPORT_SYMBOL_GPL(qrtr_endpoint_post);
@@ -581,6 +753,8 @@ static struct sk_buff *qrtr_alloc_ctrl_packet(struct qrtr_ctrl_pkt **pkt,
int qrtr_endpoint_register(struct qrtr_endpoint *ep, unsigned int nid)
{
struct qrtr_node *node;
+ u32 endpoint_id;
+ int rc;
if (!ep || !ep->xmit)
return -EINVAL;
@@ -589,6 +763,13 @@ int qrtr_endpoint_register(struct qrtr_endpoint *ep, unsigned int nid)
if (!node)
return -ENOMEM;
+ rc = xa_alloc_cyclic(&qrtr_endpoints, &endpoint_id, NULL,
+ QRTR_ENDPOINT_RANGE, &next_endpoint_id,
+ GFP_KERNEL);
+
+ if (rc < 0)
+ goto free_node;
+
kref_init(&node->ref);
mutex_init(&node->ep_lock);
skb_queue_head_init(&node->rx_queue);
@@ -598,14 +779,21 @@ int qrtr_endpoint_register(struct qrtr_endpoint *ep, unsigned int nid)
xa_init(&node->qrtr_tx_flow);
mutex_init(&node->qrtr_tx_lock);
- qrtr_node_assign(node, nid);
+ rc = qrtr_node_assign(node, nid);
+ if (rc < 0)
+ goto free_node;
mutex_lock(&qrtr_node_lock);
list_add(&node->item, &qrtr_all_nodes);
mutex_unlock(&qrtr_node_lock);
ep->node = node;
+ ep->id = endpoint_id;
+ ep->helper.added = 0;
return 0;
+free_node:
+ kfree(node);
+ return rc;
}
EXPORT_SYMBOL_GPL(qrtr_endpoint_register);
@@ -619,29 +807,46 @@ void qrtr_endpoint_unregister(struct qrtr_endpoint *ep)
struct sockaddr_qrtr src = {AF_QIPCRTR, node->nid, QRTR_PORT_CTRL};
struct sockaddr_qrtr dst = {AF_QIPCRTR, qrtr_local_nid, QRTR_PORT_CTRL};
struct radix_tree_iter iter;
+ struct radix_tree_iter iter2;
struct qrtr_ctrl_pkt *pkt;
struct qrtr_tx_flow *flow;
struct sk_buff *skb;
unsigned long flags;
unsigned long index;
void __rcu **slot;
+ void __rcu **slot2;
+ u32 endpoint_id;
mutex_lock(&node->ep_lock);
+ endpoint_id = node->ep->id;
node->ep = NULL;
mutex_unlock(&node->ep_lock);
/* Notify the local controller about the event */
spin_lock_irqsave(&qrtr_nodes_lock, flags);
radix_tree_for_each_slot(slot, &qrtr_nodes, &iter, 0) {
- if (*slot != node)
+ struct qrtr_node_lookup_helper *helper = *slot;
+
+ if (!helper)
continue;
- src.sq_node = iter.index;
- skb = qrtr_alloc_ctrl_packet(&pkt, GFP_ATOMIC);
- if (skb) {
- pkt->cmd = cpu_to_le32(QRTR_TYPE_BYE);
- qrtr_local_enqueue(NULL, skb, QRTR_TYPE_BYE, &src, &dst);
+ radix_tree_for_each_slot(slot2, &helper->nodes, &iter2, 0) {
+ if (*slot2 != node)
+ continue;
+ src.sq_node = iter.index;
+ skb = qrtr_alloc_ctrl_packet(&pkt, GFP_ATOMIC);
+ if (skb) {
+ pkt->cmd = cpu_to_le32(QRTR_TYPE_BYE);
+ qrtr_local_enqueue(NULL, skb, endpoint_id,
+ QRTR_TYPE_BYE, &src, &dst);
+ }
}
}
+ if (ep->helper.added) {
+ radix_tree_for_each_slot(slot, &ep->helper.nodes, &iter, 0)
+ radix_tree_iter_delete(&ep->helper.nodes, &iter, slot);
+ radix_tree_delete(&qrtr_nodes, ep->id);
+ ep->helper.added = 0;
+ }
spin_unlock_irqrestore(&qrtr_nodes_lock, flags);
/* Wake up any transmitters waiting for resume-tx from the node */
@@ -651,6 +856,10 @@ void qrtr_endpoint_unregister(struct qrtr_endpoint *ep)
mutex_unlock(&node->qrtr_tx_lock);
qrtr_node_release(node);
+
+ xa_erase(&qrtr_endpoints, endpoint_id);
+
+ ep->id = 0;
ep->node = NULL;
}
EXPORT_SYMBOL_GPL(qrtr_endpoint_unregister);
@@ -700,8 +909,8 @@ static void qrtr_port_remove(struct qrtr_sock *ipc)
pkt->client.port = cpu_to_le32(ipc->us.sq_port);
skb_set_owner_w(skb, &ipc->sk);
- qrtr_bcast_enqueue(NULL, skb, QRTR_TYPE_DEL_CLIENT, &ipc->us,
- &to);
+ qrtr_bcast_enqueue(NULL, skb, qrtr_local_nid,
+ QRTR_TYPE_DEL_CLIENT, &ipc->us, &to);
}
if (port == QRTR_PORT_CTRL)
@@ -841,37 +1050,52 @@ static int qrtr_bind(struct socket *sock, struct sockaddr_unsized *saddr, int le
/* Queue packet to local peer socket. */
static int qrtr_local_enqueue(struct qrtr_node *node, struct sk_buff *skb,
+ u32 endpoint_id,
int type, struct sockaddr_qrtr *from,
struct sockaddr_qrtr *to)
{
struct qrtr_sock *ipc;
struct qrtr_cb *cb;
+ int ret = -ENODEV;
ipc = qrtr_port_lookup(to->sq_port);
- if (!ipc || &ipc->sk == skb->sk) { /* do not send to self */
- if (ipc)
- qrtr_port_put(ipc);
- kfree_skb(skb);
- return -ENODEV;
- }
+ if (!ipc)
+ goto done;
+
+ if (&ipc->sk == skb->sk) /* do not send to self */
+ goto done;
+
+ /*
+ * Filter out unwanted packets that are not on behalf of the bound
+ * endpoint. Certain special packets (such as an empty NEW_SERVER
+ * packet that serves as a sentinel value) always go through.
+ */
+ if (endpoint_id && ipc->bound_endpoint &&
+ ipc->bound_endpoint != endpoint_id)
+ goto done;
cb = (struct qrtr_cb *)skb->cb;
cb->src_node = from->sq_node;
cb->src_port = from->sq_port;
+ cb->endpoint_id = endpoint_id;
- if (sock_queue_rcv_skb(&ipc->sk, skb)) {
- qrtr_port_put(ipc);
- kfree_skb(skb);
- return -ENOSPC;
- }
+ ret = -ENOSPC;
+ if (sock_queue_rcv_skb(&ipc->sk, skb))
+ goto done;
qrtr_port_put(ipc);
return 0;
+done:
+ if (ipc)
+ qrtr_port_put(ipc);
+ kfree_skb(skb);
+ return ret;
}
/* Queue packet for broadcast. */
static int qrtr_bcast_enqueue(struct qrtr_node *node, struct sk_buff *skb,
+ u32 endpoint_id,
int type, struct sockaddr_qrtr *from,
struct sockaddr_qrtr *to)
{
@@ -883,11 +1107,11 @@ static int qrtr_bcast_enqueue(struct qrtr_node *node, struct sk_buff *skb,
if (!skbn)
break;
skb_set_owner_w(skbn, skb->sk);
- qrtr_node_enqueue(node, skbn, type, from, to);
+ qrtr_node_enqueue(node, skbn, endpoint_id, type, from, to);
}
mutex_unlock(&qrtr_node_lock);
- qrtr_local_enqueue(NULL, skb, type, from, to);
+ qrtr_local_enqueue(NULL, skb, endpoint_id, type, from, to);
return 0;
}
@@ -895,12 +1119,16 @@ static int qrtr_bcast_enqueue(struct qrtr_node *node, struct sk_buff *skb,
static int qrtr_sendmsg(struct socket *sock, struct msghdr *msg, size_t len)
{
DECLARE_SOCKADDR(struct sockaddr_qrtr *, addr, msg->msg_name);
- int (*enqueue_fn)(struct qrtr_node *, struct sk_buff *, int,
- struct sockaddr_qrtr *, struct sockaddr_qrtr *);
+ int (*enqueue_fn)(struct qrtr_node *node, struct sk_buff *skb,
+ u32 endpoint_id, int type,
+ struct sockaddr_qrtr *from,
+ struct sockaddr_qrtr *to);
__le32 qrtr_type = cpu_to_le32(QRTR_TYPE_DATA);
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;
u32 type;
@@ -912,46 +1140,49 @@ 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 ?
+ msg_endpoint_id : ipc->bound_endpoint;
+
+ 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;
}
@@ -981,15 +1212,21 @@ static int qrtr_sendmsg(struct socket *sock, struct msghdr *msg, size_t len)
/* control messages already require the type as 'command' */
skb_copy_bits(skb, 0, &qrtr_type, 4);
+ /*
+ * Allow local name service to make packets appear as if
+ * they originated remotely
+ */
+ endpoint_id = msg_endpoint_id;
}
type = le32_to_cpu(qrtr_type);
- rc = enqueue_fn(node, skb, type, &ipc->us, addr);
+ rc = enqueue_fn(node, skb, endpoint_id, type, &ipc->us, addr);
if (rc >= 0)
rc = len;
out_node:
qrtr_node_release(node);
+release_sock:
release_sock(sk);
return rc;
@@ -1004,7 +1241,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;
@@ -1018,7 +1255,8 @@ static int qrtr_send_resume_tx(struct qrtr_cb *cb)
pkt->client.node = cpu_to_le32(cb->dst_node);
pkt->client.port = cpu_to_le32(cb->dst_port);
- ret = qrtr_node_enqueue(node, skb, QRTR_TYPE_RESUME_TX, &local, &remote);
+ ret = qrtr_node_enqueue(node, skb, cb->endpoint_id,
+ QRTR_TYPE_RESUME_TX, &local, &remote);
qrtr_node_release(node);
@@ -1029,6 +1267,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;
@@ -1054,6 +1293,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;
@@ -1199,6 +1442,74 @@ 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);
+ 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;
+
+ switch (optname) {
+ case QRTR_REPORT_ENDPOINT:
+ assign_bit(QRTR_F_REPORT_ENDPOINT, &ipc->flags, val);
+ break;
+ case QRTR_BIND_ENDPOINT:
+ WRITE_ONCE(ipc->bound_endpoint, val);
+ break;
+ default:
+ rc = -ENOPROTOOPT;
+ }
+
+ 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);
+ 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;
+
+ switch (optname) {
+ case QRTR_REPORT_ENDPOINT:
+ val = test_bit(QRTR_F_REPORT_ENDPOINT, &ipc->flags);
+ break;
+ case QRTR_BIND_ENDPOINT:
+ val = READ_ONCE(ipc->bound_endpoint);
+ break;
+ default:
+ rc = -ENOPROTOOPT;
+ }
+
+ 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;
@@ -1246,6 +1557,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 = {
@@ -1310,8 +1623,17 @@ static int __init qrtr_proto_init(void)
if (rc)
goto err_sock;
+ rc = radix_tree_insert(&qrtr_nodes, 0, &helper0);
+ if (rc)
+ goto err_ns;
+
+ INIT_RADIX_TREE(&helper0.nodes, GFP_ATOMIC);
+ helper0.added = 1;
+
return 0;
+err_ns:
+ qrtr_ns_remove();
err_sock:
sock_unregister(qrtr_family.family);
err_proto:
@@ -1322,6 +1644,7 @@ postcore_initcall(qrtr_proto_init);
static void __exit qrtr_proto_fini(void)
{
+ radix_tree_delete(&qrtr_nodes, 0);
qrtr_ns_remove();
sock_unregister(qrtr_family.family);
proto_unregister(&qrtr_proto);
diff --git a/net/qrtr/mhi.c b/net/qrtr/mhi.c
index 3990da1a65dc4..630ce0da060e5 100644
--- a/net/qrtr/mhi.c
+++ b/net/qrtr/mhi.c
@@ -106,6 +106,16 @@ static int qcom_mhi_qrtr_queue_dl_buffers(struct mhi_device *mhi_dev)
return 0;
}
+static ssize_t endpoint_show(struct device *dev,
+ struct device_attribute *attr, char *buf)
+{
+ struct qrtr_mhi_dev *qdev = dev_get_drvdata(dev);
+
+ return sprintf(buf, "%u\n", qdev->ep.id);
+}
+
+static DEVICE_ATTR_RO(endpoint);
+
static int qcom_mhi_qrtr_probe(struct mhi_device *mhi_dev,
const struct mhi_device_id *id)
{
@@ -135,6 +145,9 @@ static int qcom_mhi_qrtr_probe(struct mhi_device *mhi_dev,
if (rc)
goto err_unregister;
+ if (device_create_file(&mhi_dev->dev, &dev_attr_endpoint) < 0)
+ dev_err(qdev->dev, "Failed to create endpoint attribute\n");
+
dev_dbg(qdev->dev, "Qualcomm MHI QRTR driver probed\n");
return 0;
@@ -151,6 +164,7 @@ static void qcom_mhi_qrtr_remove(struct mhi_device *mhi_dev)
{
struct qrtr_mhi_dev *qdev = dev_get_drvdata(&mhi_dev->dev);
+ device_remove_file(&mhi_dev->dev, &dev_attr_endpoint);
qrtr_endpoint_unregister(&qdev->ep);
mhi_unprepare_from_transfer(mhi_dev);
dev_set_drvdata(&mhi_dev->dev, NULL);
diff --git a/net/qrtr/ns.c b/net/qrtr/ns.c
index e5b2adb161d92..d171035b88f69 100644
--- a/net/qrtr/ns.c
+++ b/net/qrtr/ns.c
@@ -5,6 +5,7 @@
* Copyright (c) 2020, Linaro Ltd.
*/
+#define pr_fmt(fmt) "QRTR NS: "fmt
#include <linux/module.h>
#include <linux/qrtr.h>
#include <linux/workqueue.h>
@@ -16,7 +17,7 @@
#define CREATE_TRACE_POINTS
#include <trace/events/qrtr.h>
-static DEFINE_XARRAY(nodes);
+static DEFINE_XARRAY(endpoints);
static struct {
struct socket *sock;
@@ -62,6 +63,7 @@ struct qrtr_server {
unsigned int node;
unsigned int port;
+ u32 endpoint_id;
struct list_head qli;
};
@@ -72,6 +74,11 @@ struct qrtr_node {
u32 server_count;
};
+struct qrtr_ns_endpoint {
+ unsigned int id;
+ struct xarray nodes;
+};
+
/* Max nodes, server, lookup limits are chosen based on the current platform
* requirements. If the requirement changes in the future, these values can be
* increased.
@@ -82,35 +89,90 @@ struct qrtr_node {
static u16 node_count;
-static struct qrtr_node *node_get(unsigned int node_id)
+static struct qrtr_node *node_get(u32 endpoint_id, unsigned int node_id)
{
+ struct qrtr_ns_endpoint *endpoint;
struct qrtr_node *node;
+ bool new_endpoint = false;
+
+ endpoint = xa_load(&endpoints, endpoint_id);
+ if (!endpoint) {
+ endpoint = kzalloc_obj(*endpoint);
+ if (!endpoint)
+ return NULL;
+
+ endpoint->id = endpoint_id;
+ xa_init(&endpoint->nodes);
+
+ if (xa_store(&endpoints, endpoint_id, endpoint, GFP_KERNEL)) {
+ kfree(endpoint);
+ return NULL;
+ }
+
+ new_endpoint = true;
+ }
- node = xa_load(&nodes, node_id);
+ node = xa_load(&endpoint->nodes, node_id);
if (node)
return node;
if (node_count >= QRTR_NS_MAX_NODES) {
pr_err_ratelimited("QRTR clients exceed max node limit!\n");
- return NULL;
+ goto error;
}
/* If node didn't exist, allocate and insert it to the tree */
node = kzalloc_obj(*node);
if (!node)
- return NULL;
+ goto error;
node->id = node_id;
xa_init(&node->servers);
- if (xa_store(&nodes, node_id, node, GFP_KERNEL)) {
+ if (xa_store(&endpoint->nodes, node_id, node, GFP_KERNEL)) {
kfree(node);
- return NULL;
+ goto error;
}
node_count++;
return node;
+error:
+ if (new_endpoint) {
+ xa_erase(&endpoints, endpoint_id);
+ kfree(endpoint);
+ }
+
+ return NULL;
+}
+
+static void node_erase(u32 endpoint_id, unsigned int node_id)
+{
+ struct qrtr_ns_endpoint *endpoint;
+ struct qrtr_node *node;
+
+ endpoint = xa_load(&endpoints, endpoint_id);
+ if (!endpoint)
+ return;
+
+ node = xa_load(&endpoint->nodes, node_id);
+ if (!node)
+ return;
+
+ xa_erase(&endpoint->nodes, node_id);
+ kfree(node);
+ node_count--;
+}
+
+static struct qrtr_node *node_lookup(u32 endpoint_id, unsigned int node_id)
+{
+ struct qrtr_ns_endpoint *endpoint;
+
+ endpoint = xa_load(&endpoints, endpoint_id);
+ if (!endpoint)
+ return NULL;
+
+ return xa_load(&endpoint->nodes, node_id);
}
static int server_match(const struct qrtr_server *srv,
@@ -126,19 +188,42 @@ static int server_match(const struct qrtr_server *srv,
return (srv->instance & ifilter) == f->instance;
}
-static int service_announce_new(struct sockaddr_qrtr *dest,
- struct qrtr_server *srv)
+static int qrtr_ns_sendmsg(u32 endpoint_id, struct sockaddr_qrtr *dest,
+ struct qrtr_ctrl_pkt *pkt)
{
- struct qrtr_ctrl_pkt pkt;
struct msghdr msg = { };
struct kvec iv;
+ u8 control[CMSG_SPACE(sizeof(endpoint_id))];
+ struct cmsghdr *cmsg;
+
+ iv.iov_base = pkt;
+ iv.iov_len = sizeof(*pkt);
+
+ msg.msg_name = (struct sockaddr *)dest;
+ msg.msg_namelen = sizeof(*dest);
+
+ if (endpoint_id) {
+ msg.msg_control = control;
+ msg.msg_controllen = sizeof(control);
+
+ cmsg = CMSG_FIRSTHDR(&msg);
+ cmsg->cmsg_level = SOL_QRTR;
+ cmsg->cmsg_type = QRTR_ENDPOINT;
+ cmsg->cmsg_len = CMSG_LEN(sizeof(endpoint_id));
+ memcpy(CMSG_DATA(cmsg), &endpoint_id, sizeof(endpoint_id));
+ }
+
+ return kernel_sendmsg(qrtr_ns.sock, &msg, &iv, 1, sizeof(*pkt));
+}
+
+static int service_announce_new(u32 endpoint_id, struct sockaddr_qrtr *dest,
+ struct qrtr_server *srv)
+{
+ struct qrtr_ctrl_pkt pkt;
trace_qrtr_ns_service_announce_new(srv->service, srv->instance,
srv->node, srv->port);
- iv.iov_base = &pkt;
- iv.iov_len = sizeof(pkt);
-
memset(&pkt, 0, sizeof(pkt));
pkt.cmd = cpu_to_le32(QRTR_TYPE_NEW_SERVER);
pkt.server.service = cpu_to_le32(srv->service);
@@ -146,26 +231,18 @@ static int service_announce_new(struct sockaddr_qrtr *dest,
pkt.server.node = cpu_to_le32(srv->node);
pkt.server.port = cpu_to_le32(srv->port);
- msg.msg_name = (struct sockaddr *)dest;
- msg.msg_namelen = sizeof(*dest);
-
- return kernel_sendmsg(qrtr_ns.sock, &msg, &iv, 1, sizeof(pkt));
+ return qrtr_ns_sendmsg(endpoint_id, dest, &pkt);
}
-static void service_announce_del(struct sockaddr_qrtr *dest,
+static void service_announce_del(u32 endpoint_id, struct sockaddr_qrtr *dest,
struct qrtr_server *srv)
{
struct qrtr_ctrl_pkt pkt;
- struct msghdr msg = { };
- struct kvec iv;
int ret;
trace_qrtr_ns_service_announce_del(srv->service, srv->instance,
srv->node, srv->port);
- iv.iov_base = &pkt;
- iv.iov_len = sizeof(pkt);
-
memset(&pkt, 0, sizeof(pkt));
pkt.cmd = cpu_to_le32(QRTR_TYPE_DEL_SERVER);
pkt.server.service = cpu_to_le32(srv->service);
@@ -173,26 +250,24 @@ static void service_announce_del(struct sockaddr_qrtr *dest,
pkt.server.node = cpu_to_le32(srv->node);
pkt.server.port = cpu_to_le32(srv->port);
- msg.msg_name = (struct sockaddr *)dest;
- msg.msg_namelen = sizeof(*dest);
-
- ret = kernel_sendmsg(qrtr_ns.sock, &msg, &iv, 1, sizeof(pkt));
+ ret = qrtr_ns_sendmsg(endpoint_id, dest, &pkt);
if (ret < 0 && ret != -ENODEV)
pr_err("failed to announce del service\n");
return;
}
-static void lookup_notify(struct sockaddr_qrtr *to, struct qrtr_server *srv,
- bool new)
+static void lookup_notify(u32 endpoint_id, struct sockaddr_qrtr *to,
+ struct qrtr_server *srv, bool new)
{
struct qrtr_ctrl_pkt pkt;
- struct msghdr msg = { };
- struct kvec iv;
int ret;
- iv.iov_base = &pkt;
- iv.iov_len = sizeof(pkt);
+ /*
+ * Notify a local client at @to about a server change. The aux data
+ * will look as if it came from the endpoint that reported the event
+ * (remote or local)
+ */
memset(&pkt, 0, sizeof(pkt));
pkt.cmd = new ? cpu_to_le32(QRTR_TYPE_NEW_SERVER) :
@@ -204,28 +279,25 @@ static void lookup_notify(struct sockaddr_qrtr *to, struct qrtr_server *srv,
pkt.server.port = cpu_to_le32(srv->port);
}
- msg.msg_name = (struct sockaddr *)to;
- msg.msg_namelen = sizeof(*to);
-
- ret = kernel_sendmsg(qrtr_ns.sock, &msg, &iv, 1, sizeof(pkt));
+ ret = qrtr_ns_sendmsg(endpoint_id, to, &pkt);
if (ret < 0 && ret != -ENODEV)
pr_err("failed to send lookup notification\n");
}
-static int announce_servers(struct sockaddr_qrtr *sq)
+static int announce_servers(u32 endpoint_id, struct sockaddr_qrtr *sq)
{
struct qrtr_server *srv;
struct qrtr_node *node;
unsigned long index;
int ret;
- node = node_get(qrtr_ns.local_node);
+ node = node_lookup(qrtr_ns.local_node, qrtr_ns.local_node);
if (!node)
return 0;
- /* Announce the list of servers registered in this node */
+ /* Announce servers registered on local endpoint to remote endpoint */
xa_for_each(&node->servers, index, srv) {
- ret = service_announce_new(sq, srv);
+ ret = service_announce_new(endpoint_id, sq, srv);
if (ret < 0) {
if (ret == -ENODEV)
continue;
@@ -237,7 +309,8 @@ static int announce_servers(struct sockaddr_qrtr *sq)
return 0;
}
-static struct qrtr_server *server_add(unsigned int service,
+static struct qrtr_server *server_add(u32 endpoint_id,
+ unsigned int service,
unsigned int instance,
unsigned int node_id,
unsigned int port)
@@ -249,7 +322,7 @@ static struct qrtr_server *server_add(unsigned int service,
if (!service || !port)
return NULL;
- node = node_get(node_id);
+ node = node_get(endpoint_id, node_id);
if (!node)
return NULL;
@@ -293,7 +366,8 @@ static struct qrtr_server *server_add(unsigned int service,
return NULL;
}
-static int server_del(struct qrtr_node *node, unsigned int port, bool bcast)
+static int server_del(u32 endpoint_id, struct qrtr_node *node,
+ unsigned int port, bool bcast)
{
struct qrtr_lookup *lookup;
struct qrtr_server *srv;
@@ -305,9 +379,10 @@ static int server_del(struct qrtr_node *node, unsigned int port, bool bcast)
xa_erase(&node->servers, port);
- /* Broadcast the removal of local servers */
+ /* Broadcast the removal of local servers to remote endpoints */
if (srv->node == qrtr_ns.local_node && bcast)
- service_announce_del(&qrtr_ns.bcast_sq, srv);
+ service_announce_del(qrtr_ns.local_node,
+ &qrtr_ns.bcast_sq, srv);
/* Announce the service's disappearance to observers */
list_for_each(li, &qrtr_ns.lookups) {
@@ -317,7 +392,7 @@ static int server_del(struct qrtr_node *node, unsigned int port, bool bcast)
if (lookup->instance && lookup->instance != srv->instance)
continue;
- lookup_notify(&lookup->sq, srv, false);
+ lookup_notify(endpoint_id, &lookup->sq, srv, false);
}
kfree(srv);
@@ -326,23 +401,15 @@ static int server_del(struct qrtr_node *node, unsigned int port, bool bcast)
return 0;
}
-static int say_hello(struct sockaddr_qrtr *dest)
+static int say_hello(u32 endpoint_id, struct sockaddr_qrtr *dest)
{
struct qrtr_ctrl_pkt pkt;
- struct msghdr msg = { };
- struct kvec iv;
int ret;
- iv.iov_base = &pkt;
- iv.iov_len = sizeof(pkt);
-
memset(&pkt, 0, sizeof(pkt));
pkt.cmd = cpu_to_le32(QRTR_TYPE_HELLO);
- msg.msg_name = (struct sockaddr *)dest;
- msg.msg_namelen = sizeof(*dest);
-
- ret = kernel_sendmsg(qrtr_ns.sock, &msg, &iv, 1, sizeof(pkt));
+ ret = qrtr_ns_sendmsg(endpoint_id, dest, &pkt);
if (ret < 0)
pr_err("failed to send hello msg\n");
@@ -350,42 +417,38 @@ static int say_hello(struct sockaddr_qrtr *dest)
}
/* Announce the list of servers registered on the local node */
-static int ctrl_cmd_hello(struct sockaddr_qrtr *sq)
+static int ctrl_cmd_hello(u32 endpoint_id, struct sockaddr_qrtr *sq)
{
int ret;
- ret = say_hello(sq);
+ /* Send Hello and New Server messages to remote endpoint */
+ ret = say_hello(endpoint_id, sq);
if (ret < 0)
return ret;
- return announce_servers(sq);
+ return announce_servers(endpoint_id, sq);
}
-static int ctrl_cmd_bye(struct sockaddr_qrtr *from)
+static int ctrl_cmd_bye(u32 endpoint_id, struct sockaddr_qrtr *from)
{
struct qrtr_node *local_node;
struct qrtr_ctrl_pkt pkt;
struct qrtr_server *srv;
struct sockaddr_qrtr sq;
- struct msghdr msg = { };
struct qrtr_node *node;
unsigned long index;
- struct kvec iv;
int ret = 0;
- iv.iov_base = &pkt;
- iv.iov_len = sizeof(pkt);
-
- node = node_get(from->sq_node);
+ node = node_get(endpoint_id, from->sq_node);
if (!node)
return 0;
/* Advertise removal of this client to all servers of remote node */
xa_for_each(&node->servers, index, srv)
- server_del(node, srv->port, true);
+ server_del(endpoint_id, node, srv->port, true);
/* Advertise the removal of this client to all local servers */
- local_node = node_get(qrtr_ns.local_node);
+ local_node = node_get(qrtr_ns.local_node, qrtr_ns.local_node);
if (!local_node) {
ret = 0;
goto delete_node;
@@ -400,10 +463,8 @@ static int ctrl_cmd_bye(struct sockaddr_qrtr *from)
sq.sq_node = srv->node;
sq.sq_port = srv->port;
- msg.msg_name = (struct sockaddr *)&sq;
- msg.msg_namelen = sizeof(sq);
-
- ret = kernel_sendmsg(qrtr_ns.sock, &msg, &iv, 1, sizeof(pkt));
+ /* Bye will look as if it came from endpoint_id */
+ ret = qrtr_ns_sendmsg(endpoint_id, &sq, &pkt);
if (ret < 0 && ret != -ENODEV) {
pr_err("failed to send bye cmd\n");
goto delete_node;
@@ -414,32 +475,25 @@ static int ctrl_cmd_bye(struct sockaddr_qrtr *from)
ret = 0;
delete_node:
- xa_erase(&nodes, from->sq_node);
- kfree(node);
- node_count--;
+ node_erase(endpoint_id, from->sq_node);
return ret;
}
-static int ctrl_cmd_del_client(struct sockaddr_qrtr *from,
+static int ctrl_cmd_del_client(u32 endpoint_id, struct sockaddr_qrtr *from,
unsigned int node_id, unsigned int port)
{
struct qrtr_node *local_node;
struct qrtr_lookup *lookup;
struct qrtr_ctrl_pkt pkt;
- struct msghdr msg = { };
struct qrtr_server *srv;
struct sockaddr_qrtr sq;
struct qrtr_node *node;
struct list_head *tmp;
struct list_head *li;
unsigned long index;
- struct kvec iv;
int ret;
- iv.iov_base = &pkt;
- iv.iov_len = sizeof(pkt);
-
/* Don't accept spoofed messages */
if (from->sq_node != node_id)
return -EINVAL;
@@ -465,12 +519,12 @@ static int ctrl_cmd_del_client(struct sockaddr_qrtr *from,
* DEL_SERVER. Neighbours would've already removed the server belonging
* to this port due to the DEL_CLIENT broadcast from qrtr_port_remove().
*/
- node = node_get(node_id);
+ node = node_lookup(endpoint_id, node_id);
if (node)
- server_del(node, port, false);
+ server_del(endpoint_id, node, port, false);
/* Advertise the removal of this client to all local servers */
- local_node = node_get(qrtr_ns.local_node);
+ local_node = node_lookup(qrtr_ns.local_node, qrtr_ns.local_node);
if (!local_node)
return 0;
@@ -484,10 +538,8 @@ static int ctrl_cmd_del_client(struct sockaddr_qrtr *from,
sq.sq_node = srv->node;
sq.sq_port = srv->port;
- msg.msg_name = (struct sockaddr *)&sq;
- msg.msg_namelen = sizeof(sq);
-
- ret = kernel_sendmsg(qrtr_ns.sock, &msg, &iv, 1, sizeof(pkt));
+ /* Del Client will look as if it came from endpoint_id */
+ ret = qrtr_ns_sendmsg(endpoint_id, &sq, &pkt);
if (ret < 0 && ret != -ENODEV) {
pr_err("failed to send del client cmd\n");
return ret;
@@ -496,7 +548,7 @@ static int ctrl_cmd_del_client(struct sockaddr_qrtr *from,
return 0;
}
-static int ctrl_cmd_new_server(struct sockaddr_qrtr *from,
+static int ctrl_cmd_new_server(u32 endpoint_id, struct sockaddr_qrtr *from,
unsigned int service, unsigned int instance,
unsigned int node_id, unsigned int port)
{
@@ -511,12 +563,16 @@ static int ctrl_cmd_new_server(struct sockaddr_qrtr *from,
port = from->sq_port;
}
- srv = server_add(service, instance, node_id, port);
- if (!srv)
+ srv = server_add(endpoint_id, service, instance, node_id, port);
+ if (!srv) {
+ pr_err("Failed to add server\n");
return -EINVAL;
+ }
if (srv->node == qrtr_ns.local_node) {
- ret = service_announce_new(&qrtr_ns.bcast_sq, srv);
+ /* Broadcast local server info to all peer endpoints */
+ ret = service_announce_new(qrtr_ns.local_node,
+ &qrtr_ns.bcast_sq, srv);
if (ret < 0) {
pr_err("failed to announce new service\n");
return ret;
@@ -531,13 +587,13 @@ static int ctrl_cmd_new_server(struct sockaddr_qrtr *from,
if (lookup->instance && lookup->instance != instance)
continue;
- lookup_notify(&lookup->sq, srv, true);
+ lookup_notify(endpoint_id, &lookup->sq, srv, true);
}
return ret;
}
-static int ctrl_cmd_del_server(struct sockaddr_qrtr *from,
+static int ctrl_cmd_del_server(u32 endpoint_id, struct sockaddr_qrtr *from,
unsigned int service, unsigned int instance,
unsigned int node_id, unsigned int port)
{
@@ -553,24 +609,22 @@ static int ctrl_cmd_del_server(struct sockaddr_qrtr *from,
if (from->sq_node == qrtr_ns.local_node && from->sq_port != port)
return -EINVAL;
- node = node_get(node_id);
+ node = node_lookup(endpoint_id, node_id);
if (!node)
return -ENOENT;
- server_del(node, port, true);
+ server_del(endpoint_id, node, port, true);
return 0;
}
-static int ctrl_cmd_new_lookup(struct sockaddr_qrtr *from,
+static int ctrl_cmd_new_lookup(u32 endpoint_id, struct sockaddr_qrtr *from,
unsigned int service, unsigned int instance)
{
struct qrtr_server_filter filter;
struct qrtr_lookup *lookup;
- struct qrtr_server *srv;
- struct qrtr_node *node;
- unsigned long node_idx;
- unsigned long srv_idx;
+ unsigned long id;
+ struct qrtr_ns_endpoint *endpoint;
/* Accept only local observers */
if (from->sq_node != qrtr_ns.local_node)
@@ -595,22 +649,30 @@ static int ctrl_cmd_new_lookup(struct sockaddr_qrtr *from,
filter.service = service;
filter.instance = instance;
- xa_for_each(&nodes, node_idx, node) {
- xa_for_each(&node->servers, srv_idx, srv) {
- if (!server_match(srv, &filter))
- continue;
+ xa_for_each(&endpoints, id, endpoint) {
+ struct qrtr_node *node;
+ unsigned long node_idx;
- lookup_notify(from, srv, true);
+ xa_for_each(&endpoint->nodes, node_idx, node) {
+ unsigned long srv_idx;
+ struct qrtr_server *srv;
+
+ xa_for_each(&node->servers, srv_idx, srv) {
+ if (!server_match(srv, &filter))
+ continue;
+
+ lookup_notify(id, from, srv, true);
+ }
}
}
/* Empty notification, to indicate end of listing */
- lookup_notify(from, NULL, true);
+ lookup_notify(0, from, NULL, true);
return 0;
}
-static void ctrl_cmd_del_lookup(struct sockaddr_qrtr *from,
+static void ctrl_cmd_del_lookup(u32 endpoint_id, struct sockaddr_qrtr *from,
unsigned int service, unsigned int instance)
{
struct qrtr_lookup *lookup;
@@ -644,6 +706,7 @@ static void qrtr_ns_worker(struct work_struct *work)
ssize_t msglen;
void *recv_buf;
struct kvec iv;
+ u8 control[32];
int ret;
msg.msg_name = (struct sockaddr *)&sq;
@@ -654,8 +717,12 @@ static void qrtr_ns_worker(struct work_struct *work)
return;
for (;;) {
+ u32 endpoint_id;
+
iv.iov_base = recv_buf;
iv.iov_len = recv_buf_size;
+ msg.msg_control = control;
+ msg.msg_controllen = sizeof(control);
msglen = kernel_recvmsg(qrtr_ns.sock, &msg, &iv, 1,
iv.iov_len, MSG_DONTWAIT);
@@ -668,6 +735,19 @@ static void qrtr_ns_worker(struct work_struct *work)
break;
}
+ /* AUX data is written direct into the control buffer */
+ msg.msg_control = control;
+ msg.msg_controllen = sizeof(control) - msg.msg_controllen;
+
+ ret = qrtr_msg_get_endpoint(&msg, &endpoint_id);
+ if (ret < 0) {
+ pr_err("error receiving endpoint id: %d\n", ret);
+ break;
+ }
+
+ if ((size_t)msglen < sizeof(*pkt))
+ break;
+
pkt = recv_buf;
cmd = le32_to_cpu(pkt->cmd);
if (cmd < ARRAY_SIZE(qrtr_ctrl_pkt_strings) &&
@@ -678,25 +758,25 @@ static void qrtr_ns_worker(struct work_struct *work)
ret = 0;
switch (cmd) {
case QRTR_TYPE_HELLO:
- ret = ctrl_cmd_hello(&sq);
+ ret = ctrl_cmd_hello(endpoint_id, &sq);
break;
case QRTR_TYPE_BYE:
- ret = ctrl_cmd_bye(&sq);
+ ret = ctrl_cmd_bye(endpoint_id, &sq);
break;
case QRTR_TYPE_DEL_CLIENT:
- ret = ctrl_cmd_del_client(&sq,
+ ret = ctrl_cmd_del_client(endpoint_id, &sq,
le32_to_cpu(pkt->client.node),
le32_to_cpu(pkt->client.port));
break;
case QRTR_TYPE_NEW_SERVER:
- ret = ctrl_cmd_new_server(&sq,
+ ret = ctrl_cmd_new_server(endpoint_id, &sq,
le32_to_cpu(pkt->server.service),
le32_to_cpu(pkt->server.instance),
le32_to_cpu(pkt->server.node),
le32_to_cpu(pkt->server.port));
break;
case QRTR_TYPE_DEL_SERVER:
- ret = ctrl_cmd_del_server(&sq,
+ ret = ctrl_cmd_del_server(endpoint_id, &sq,
le32_to_cpu(pkt->server.service),
le32_to_cpu(pkt->server.instance),
le32_to_cpu(pkt->server.node),
@@ -707,12 +787,12 @@ static void qrtr_ns_worker(struct work_struct *work)
case QRTR_TYPE_RESUME_TX:
break;
case QRTR_TYPE_NEW_LOOKUP:
- ret = ctrl_cmd_new_lookup(&sq,
+ ret = ctrl_cmd_new_lookup(endpoint_id, &sq,
le32_to_cpu(pkt->server.service),
le32_to_cpu(pkt->server.instance));
break;
case QRTR_TYPE_DEL_LOOKUP:
- ctrl_cmd_del_lookup(&sq,
+ ctrl_cmd_del_lookup(endpoint_id, &sq,
le32_to_cpu(pkt->server.service),
le32_to_cpu(pkt->server.instance));
break;
@@ -746,6 +826,8 @@ int qrtr_ns_init(void)
if (ret < 0)
return ret;
+ qrtr_sock_set_report_endpoint(qrtr_ns.sock->sk);
+
ret = kernel_getsockname(qrtr_ns.sock, (struct sockaddr *)&sq);
if (ret < 0) {
pr_err("failed to get socket name\n");
@@ -774,7 +856,7 @@ int qrtr_ns_init(void)
qrtr_ns.bcast_sq.sq_node = QRTR_NODE_BCAST;
qrtr_ns.bcast_sq.sq_port = QRTR_PORT_CTRL;
- ret = say_hello(&qrtr_ns.bcast_sq);
+ ret = say_hello(qrtr_ns.local_node, &qrtr_ns.bcast_sq);
if (ret < 0)
goto err_wq;
diff --git a/net/qrtr/qrtr.h b/net/qrtr/qrtr.h
index 3f2d28696062a..affc24f426c64 100644
--- a/net/qrtr/qrtr.h
+++ b/net/qrtr/qrtr.h
@@ -9,6 +9,15 @@ struct sk_buff;
/* endpoint node id auto assignment */
#define QRTR_EP_NID_AUTO (-1)
+/**
+ * struct qrtr_node_lookup_helper - node lookup helper, stored in qrtr_nodes
+ * @nodes: nodes, indexed by node_id
+ */
+struct qrtr_node_lookup_helper {
+ int added;
+ struct radix_tree_root nodes;
+};
+
/**
* struct qrtr_endpoint - endpoint handle
* @xmit: Callback for outgoing packets
@@ -21,6 +30,8 @@ struct qrtr_endpoint {
int (*xmit)(struct qrtr_endpoint *ep, struct sk_buff *skb);
/* private: not for endpoint use */
struct qrtr_node *node;
+ struct qrtr_node_lookup_helper helper;
+ u32 id;
};
int qrtr_endpoint_register(struct qrtr_endpoint *ep, unsigned int nid);
@@ -33,4 +44,7 @@ int qrtr_ns_init(void);
void qrtr_ns_remove(void);
+int qrtr_msg_get_endpoint(struct msghdr *msg, u32 *out_endpoint_id);
+void qrtr_sock_set_report_endpoint(struct sock *sk);
+
#endif
|
| 3/2 | 2026/09/02 03:18 | llm | patch-evaluator |
0mModel:gemini-3.7-flash Args: null Results: null Tokens: input: 22147 output: 27 thoughts: 124 Thoughts: **Analyzing a QRTR Patch for Fuzzing Potential** Okay, so this patch is touching a handful of files: `net/qrtr/af_qrtr.c`, `net/qrtr/ns.c`, `net/qrtr/mhi.c`, `net/qrtr/qrtr.h`, plus some header files in `include/linux/socket.h` and `include/uapi/linux/qrtr.h`. My initial thought is, let's dissect this. Is there anything here that screams "fuzz me!"? Is it worth the effort? We'll need to figure out what functionality is being changed. First, I should probably check the Kconfig files related to QRTR and see what configurations are involved, and what's enabled by default. I'll fire up `grepper` (or if it's simpler, `read-file` and a bit of manual searching) to grab the necessary Kconfig bits. Identifying dependencies early is crucial. The goal here is to determine the necessary build configs to get the QRTR components built. Once I have a good handle on that, I'll need to identify the key symbols modified by the patch that would make the best targets for fuzzing. |
| 4/2 | 2026/09/02 03:18 | tool | grepper |
0mArgs:{
"Expression": "config QRTR",
"PathPrefix": "net/qrtr"
}Results: {
"Output": "net/qrtr/Kconfig-4-\nnet/qrtr/Kconfig:5:config QRTR\nnet/qrtr/Kconfig-6-\ttristate \"Qualcomm IPC Router support\"\n--\nnet/qrtr/Kconfig=15=if QRTR\nnet/qrtr/Kconfig-16-\nnet/qrtr/Kconfig:17:config QRTR_SMD\nnet/qrtr/Kconfig-18-\ttristate \"SMD IPC Router channels\"\n--\nnet/qrtr/Kconfig-23-\nnet/qrtr/Kconfig:24:config QRTR_TUN\nnet/qrtr/Kconfig-25-\ttristate \"TUN device for Qualcomm IPC Router\"\n--\nnet/qrtr/Kconfig-30-\nnet/qrtr/Kconfig:31:config QRTR_MHI\nnet/qrtr/Kconfig-32-\ttristate \"MHI IPC Router channels\"\n"
} |
| 5/2 | 2026/09/02 03:18 | llm | patch-evaluator |
0mModel:gemini-3.7-flash Args: null Results: null Tokens: input: 22802 output: 33 thoughts: 21 Thoughts: Here's my take on the thought process, as if it were my own: **Diving into the QRTR Configuration** Okay, time to get my hands dirty. I need to understand the configuration options and dependencies for the QRTR driver, so I'm going to start by reading the `net/qrtr/Kconfig` file. This is the bedrock; it's where the conditional logic lives. Understanding the dependencies *before* I even touch the code will save me a ton of headache later. Kconfig will lay out the building blocks: what needs to be enabled, what it relies on, and what the potential build-time customizations are. This is standard operating procedure - no need to reinvent the wheel, and no need to waste time guessing. Let's see what secrets this Kconfig holds. |
| 6/2 | 2026/09/02 03:18 | tool | read-file |
0mArgs:{
"File": "net/qrtr/Kconfig",
"FirstLine": 1,
"LineCount": 50
}Results: {
"Contents": " 1:\t# SPDX-License-Identifier: GPL-2.0-only\n 2:\t# Qualcomm IPC Router configuration\n 3:\t#\n 4:\t\n 5:\tconfig QRTR\n 6:\t\ttristate \"Qualcomm IPC Router support\"\n 7:\t\thelp\n 8:\t\t Say Y if you intend to use Qualcomm IPC router protocol. The\n 9:\t\t protocol is used to communicate with services provided by other\n 10:\t\t hardware blocks in the system.\n 11:\t\n 12:\t\t In order to do service lookups, a userspace daemon is required to\n 13:\t\t maintain a service listing.\n 14:\t\n 15:\tif QRTR\n 16:\t\n 17:\tconfig QRTR_SMD\n 18:\t\ttristate \"SMD IPC Router channels\"\n 19:\t\tdepends on RPMSG || (COMPILE_TEST \u0026\u0026 RPMSG=n)\n 20:\t\thelp\n 21:\t\t Say Y here to support SMD based ipcrouter channels. SMD is the\n 22:\t\t most common transport for IPC Router.\n 23:\t\n 24:\tconfig QRTR_TUN\n 25:\t\ttristate \"TUN device for Qualcomm IPC Router\"\n 26:\t\thelp\n 27:\t\t Say Y here to expose a character device that allows user space to\n 28:\t\t implement endpoints of QRTR, for purpose of tunneling data to other\n 29:\t\t hosts or testing purposes.\n 30:\t\n 31:\tconfig QRTR_MHI\n 32:\t\ttristate \"MHI IPC Router channels\"\n 33:\t\tdepends on MHI_BUS\n 34:\t\thelp\n 35:\t\t Say Y here to support MHI based ipcrouter channels. MHI is the\n 36:\t\t transport used for communicating to external modems.\n 37:\t\n 38:\tendif # QRTR\n"
} |
| 7/2 | 2026/09/02 03:18 | llm | patch-evaluator |
0mModel:gemini-3.7-flash Args: null Results: null Tokens: input: 23512 output: 32 thoughts: 32 Thoughts: Okay, here's my interpretation of that thought process, from an expert's perspective: **Diving into the QRTR Build Process** Right, so I need to understand the build process for the QRTR (Quick Response Transport for Real-Time) functionality within this kernel module. The most logical starting point to trace the compilation of `af_qrtr.c` and `ns.c` is the `net/qrtr/Makefile`. That's where all the rules for building the relevant objects and linking them into the final module are likely defined. Checking the Makefile should provide the dependencies, compiler flags, and linking commands, allowing me to dissect how these key source files are handled and how their compiled output fits into the overall QRTR structure. Knowing this will give me a solid foundation for understanding the system. |
| 8/2 | 2026/09/02 03:19 | tool | read-file |
0mArgs:{
"File": "net/qrtr/Makefile",
"FirstLine": 1,
"LineCount": 20
}Results: {
"Contents": " 1:\t# SPDX-License-Identifier: GPL-2.0-only\n 2:\tobj-$(CONFIG_QRTR) += qrtr.o\n 3:\tqrtr-y\t:= af_qrtr.o ns.o\n 4:\t\n 5:\tobj-$(CONFIG_QRTR_SMD) += qrtr-smd.o\n 6:\tqrtr-smd-y\t:= smd.o\n 7:\tobj-$(CONFIG_QRTR_TUN) += qrtr-tun.o\n 8:\tqrtr-tun-y\t:= tun.o\n 9:\tobj-$(CONFIG_QRTR_MHI) += qrtr-mhi.o\n 10:\tqrtr-mhi-y\t:= mhi.o\n"
} |
| 9/2 | 2026/09/02 03:19 | llm | patch-evaluator |
0mModel:gemini-3.7-flash Args: null Results: null Tokens: input: 23944 output: 28 thoughts: 108 Thoughts: **QRTR Socket Creation Deep Dive** Okay, so if `CONFIG_QRTR` is enabled, I know we get `af_qrtr.o` and `ns.o` built. Pretty standard for enabling a feature. And of course, `QRTR_TUN` gives userspace the flexibility to handle QRTR endpoints through `/dev/qrtr-tun` – smart, allows for custom implementations. But I'm curious about the raw sockets. Users can also directly open QRTR sockets using `socket(AF_QIPCRTR, SOCK_DGRAM, 0)`. That's where it gets interesting, as it gets to how the AF_QIPCRTR family behaves. I need to understand how *that* socket creation process actually works, and `net/qrtr/af_qrtr.c` is the obvious place to start. Let's dig in and see what's happening under the hood. I want to see how the kernel handles the socket calls, what data structures are involved, and how the communication with the hardware is set up. This'll likely be related to the bus implementation and the way the QRTR messages are routed. Let's see if there's any cleverness in the way the socket is created and configured. |
| 10/2 | 2026/09/02 03:19 | tool | grepper |
0mArgs:{
"Expression": "qrtr_create",
"PathPrefix": "net/qrtr"
}Results: {
"Output": "net/qrtr/af_qrtr.c=1564=static struct proto qrtr_proto = {\n--\nnet/qrtr/af_qrtr.c-1569-\nnet/qrtr/af_qrtr.c:1570:static int qrtr_create(struct net *net, struct socket *sock,\nnet/qrtr/af_qrtr.c-1571-\t\t int protocol, int kern)\n--\nnet/qrtr/af_qrtr.c=1604=static const struct net_proto_family qrtr_family = {\n--\nnet/qrtr/af_qrtr.c-1606-\t.family\t= AF_QIPCRTR,\nnet/qrtr/af_qrtr.c:1607:\t.create\t= qrtr_create,\nnet/qrtr/af_qrtr.c-1608-};\n"
} |
| 11/2 | 2026/09/02 03:19 | llm | patch-evaluator |
0mModel:gemini-3.7-flash Args: null Results: null Tokens: input: 24582 output: 31 thoughts: 1519 Thoughts: **Fuzzing Target Identification: QRTR Socket and Nameservice** Okay, let's break this down. First, I need to understand the functional changes. It looks like significant work has been done on the QRTR socket layer, specifically with the introduction of new socket options (`SOL_QRTR`, `QRTR_REPORT_ENDPOINT`, `QRTR_BIND_ENDPOINT`) via `qrtr_setsockopt` and `qrtr_getsockopt`. There's also now CMSG support for `QRTR_ENDPOINT` within `qrtr_sendmsg` and `qrtr_recvmsg`. That's a huge potential attack surface right there – socket option handlers and message processing are notoriously prone to vulnerabilities. The addition of support for multiple endpoints, coupled with the use of xarrays and radix trees for endpoint tracking, is particularly interesting. Data structures like those are often where things go sideways during fuzzing. The updates to `qrtr_endpoint_post`, `qrtr_endpoint_register`, and `qrtr_endpoint_unregister` are definitely worth scrutinizing. The nameservice implementation (`net/qrtr/ns.c`) now tracking endpoints and the flow control changes (`qrtr_local_enqueue`, etc.) make this even more attractive. The sysfs attribute `endpoint` created by `qcom_mhi_qrtr_probe` is another potential avenue. Considering this is core networking logic, exposed to userspace through standard socket calls and potentially through `/dev/qrtr-tun` or local QRTR communication, this is definitely worth fuzzing. The changes are significant, touching new socket options, new CMSG processing, hierarchical indexing structures, flow control, and nameservice messaging. It's also reachable in virtualized environments. Now, let's identify the specific functions to focus on. Based on the documentation, I need to focus on non-static, non-inlined API entrypoints. Let's see what's been modified. Looking at the changed functions in `af_qrtr.c`, `ns.c`, and `mhi.c`, I need to extract public functions. Here is the list of changed functions: * `qrtr_sock_set_report_endpoint` * `qrtr_msg_get_endpoint` * `qrtr_tx_resume` * `qrtr_tx_wait` * `qrtr_tx_flow_failed` * `qrtr_node_enqueue` * `qrtr_endpoint_post` * `qrtr_endpoint_register` * `qrtr_endpoint_unregister` * `qrtr_local_enqueue` * `qrtr_bcast_enqueue` * `qrtr_sendmsg` * `qrtr_recvmsg` * `qrtr_setsockopt` * `qrtr_getsockopt` * `qrtr_ns_sendmsg` * `qrtr_ns_init` The following are exported / public entrypoints or socket ops: * `qrtr_endpoint_post` * `qrtr_endpoint_register` * `qrtr_endpoint_unregister` * `qrtr_setsockopt` * `qrtr_getsockopt` * `qrtr_sendmsg` * `qrtr_recvmsg` * `qrtr_msg_get_endpoint` * `qrtr_sock_set_report_endpoint` * `qrtr_ns_init` These functions are either socket operations or exported. The critical ones for fuzzing, according to the guideline are: * `qrtr_setsockopt` * `qrtr_getsockopt` * `qrtr_sendmsg` * `qrtr_recvmsg` * `qrtr_endpoint_post` * `qrtr_endpoint_register` * `qrtr_endpoint_unregister` * `qrtr_msg_get_endpoint` * `qrtr_sock_set_report_endpoint` These will be my `FocusSymbols`. Finally, make sure `CONFIG_QRTR` is enabled, and there are no obscure dependencies that are not enabled. |
| 12/2 | 2026/09/02 03:19 | tool | grepper |
0mArgs:{
"Expression": "source \"net/qrtr/Kconfig\"",
"PathPrefix": "net"
}Results: {
"Output": "net/Kconfig=281=source \"net/l3mdev/Kconfig\"\nnet/Kconfig:282:source \"net/qrtr/Kconfig\"\nnet/Kconfig-283-source \"net/ncsi/Kconfig\"\n"
} |
| 13/2 | 2026/09/02 03:19 | llm | patch-evaluator |
0mModel:gemini-3.7-flash Args: null Results: null Tokens: input: 27147 output: 214 thoughts: 584 Thoughts: **My Assessment of the QRTR Patch for Fuzzing** Okay, let's break this down. First, I need to make sure I understand the dependencies. It looks like we're dealing with the Qualcomm IPC Router (QRTR) networking subsystem. The core is enabled by `CONFIG_QRTR`, which pulls in `qrtr.o`, containing `af_qrtr.o` and `ns.o`. From the Kconfig, `CONFIG_QRTR` itself is the primary config we need, and it seems like `CONFIG_NET` is the standard parent. Then there are some optional modules: `CONFIG_QRTR_TUN` which enables user-space endpoint implementation, useful for fuzzing; and `CONFIG_QRTR_MHI`, which depends on `MHI_BUS` and was touched in the patch (endpoint attribute). The patch itself is the key here. It seems to implement endpoint filtering and binding support in QRTR. Specifically, this means new `setsockopt` and `getsockopt` calls, `QRTR_REPORT_ENDPOINT` and `QRTR_BIND_ENDPOINT`, under the `SOL_QRTR` socket option. I see they've also added control message handling for `QRTR_ENDPOINT` with `sendmsg` and `recvmsg`. Importantly, they've reworked internal node routing and nameservice using xarrays/radix trees within `af_qrtr.c` and `ns.c`. This is all directly accessible to userspace via `AF_QIPCRTR` sockets. Looking at the "WorthFuzzing" section, I get a clear list of what's been touched, these are the functions I'm most interested in targeting: * `qrtr_setsockopt` * `qrtr_getsockopt` * `qrtr_sendmsg` * `qrtr_recvmsg` * `qrtr_msg_get_endpoint` * `qrtr_sock_set_report_endpoint` * `qrtr_endpoint_post` * `qrtr_endpoint_register` * `qrtr_endpoint_unregister` The core configs for this are `QRTR`, `QRTR_TUN`, and `QRTR_MHI`. So, in summary: the patch significantly alters how QRTR endpoints are managed. It introduces new socket options, adds control message handling, and modifies the underlying data structures for routing. This means we should prioritize fuzzing those new socket options, the `sendmsg` and `recvmsg` paths, and the new endpoint management routines. This should be a good starting point for testing this. Let's get the fuzzing started. |
| 14/2 | 2026/09/02 03:19 | tool | set-results |
0mArgs:{
"EnableConfigs": [
"QRTR",
"QRTR_TUN",
"QRTR_MHI"
],
"FocusSymbols": [
"qrtr_setsockopt",
"qrtr_getsockopt",
"qrtr_sendmsg",
"qrtr_recvmsg",
"qrtr_msg_get_endpoint",
"qrtr_sock_set_report_endpoint",
"qrtr_endpoint_post",
"qrtr_endpoint_register",
"qrtr_endpoint_unregister"
],
"Reasoning": "The patch adds endpoint binding and reporting support to the QRTR (Qualcomm IPC Router) subsystem. It adds setsockopt/getsockopt options at SOL_QRTR, CMSG control message handling for QRTR_ENDPOINT, and updates node lookup and name service logic with radix trees and xarrays. These features are reachable via AF_QIPCRTR sockets and QRTR tun interfaces.",
"WorthFuzzing": true
}Results: {
"EnableConfigs": [
"QRTR",
"QRTR_TUN",
"QRTR_MHI"
],
"FocusSymbols": [
"qrtr_setsockopt",
"qrtr_getsockopt",
"qrtr_sendmsg",
"qrtr_recvmsg",
"qrtr_msg_get_endpoint",
"qrtr_sock_set_report_endpoint",
"qrtr_endpoint_post",
"qrtr_endpoint_register",
"qrtr_endpoint_unregister"
],
"Reasoning": "The patch adds endpoint binding and reporting support to the QRTR (Qualcomm IPC Router) subsystem. It adds setsockopt/getsockopt options at SOL_QRTR, CMSG control message handling for QRTR_ENDPOINT, and updates node lookup and name service logic with radix trees and xarrays. These features are reachable via AF_QIPCRTR sockets and QRTR tun interfaces.",
"WorthFuzzing": true
} |
| 15/1 | 2026/09/02 03:19 | action | If |
1mArgs:{
"WorthFuzzing": true
}Results: null |
| 16/2 | 2026/09/02 03:19 | agent | kmsan-evaluator |
1mModel:core Args: null Results: {
"KMSANReasoning": "The patch series introduces endpoint-aware routing and filtering for Qualcomm IPC Router (QRTR) sockets, adding new setsockopt/getsockopt options (QRTR_REPORT_ENDPOINT, QRTR_BIND_ENDPOINT), control messages (QRTR_ENDPOINT), and multi-endpoint radix tree/xarray lookups.\n\nAnalysis of uninitialized memory and info-leak risks:\n1. Kernel-to-user copying in getsockopt (QRTR_REPORT_ENDPOINT, QRTR_BIND_ENDPOINT): 'val' is an unsigned int explicitly initialized in each supported switch branch before calling copy_to_user() for sizeof(int) bytes. No padding or uninitialized stack bytes are copied.\n2. CMSG handling in recvmsg: put_cmsg() is called with \u0026cb-\u003eendpoint_id (a u32), which is unconditionally initialized in all packet ingress paths (qrtr_endpoint_post and qrtr_local_enqueue).\n3. CMSG parsing in qrtr_msg_get_endpoint: the output parameter is initialized to 0 or the parsed cmsg value on success, preventing uninitialized reads in qrtr_sendmsg and qrtr_ns_worker.\n4. Heap and socket allocations: all newly introduced structures (struct qrtr_ns_endpoint, struct qrtr_server, struct qrtr_node) use kzalloc/kzalloc_obj, and QRTR sockets are allocated via sk_alloc (which zero-initializes socket memory).\n\nKASAN vs KMSAN applicability:\nThe changes primarily involve radix tree restructuring, cyclic xarray ID management, bit manipulation for 32/64-bit index keys, and socket endpoint matching. Risks associated with these changes include use-after-free, out-of-bounds radix tree access, reference counting bugs, or lock ordering, all of which are caught by standard KASAN and LOCKDEP instrumentation. Because no uninitialized memory usage or kernel-to-user information leaks are introduced, a dedicated KMSAN fuzzing session is not warranted.",
"NeedsKMSAN": false
}Instruction: You are an expert Linux kernel security engineer specializing in kernel memory
error detectors (KASAN and KMSAN). Your job is to review the provided patch series and
determine if the code changes justify spawning a dedicated KMSAN (KernelMemorySanitizer)
fuzzing session in addition to standard KASAN fuzzing.
CRITICAL DISTINCTION BETWEEN KASAN AND KMSAN:
- Standard KASAN kernel builds (upstream-apparmor-kasan.config) already enable
a comprehensive suite of debugging tools and sanitizers, including KASAN
(out-of-bounds accesses, use-after-free, double free, invalid free), LOCKDEP
(locking bugs and deadlocks), UB-sanitizers, and memory corruption checks.
- KMSAN (KernelMemorySanitizer) detects reads of UNINITIALIZED memory (stack, heap,
or page allocations) and kernel-to-user memory info-leaks.
Rule: THERE IS NO SENSE IN RUNNING A KMSAN SESSION IF A BUG CAN BE CAUGHT BY KASAN,
LOCKDEP, OR OTHER STANDARD BUG DETECTORS.
A dedicated KMSAN fuzzing session incurs significant resource costs. You must ONLY
set NeedsKMSAN=true if the code changes introduce or expose UNINITIALIZED MEMORY risks
that are detected ONLY by KMSAN.
Look holistically at the patch series and surrounding code. Even if no direct
uninitialized field accesses or new buffer allocations are added in the diff itself,
a patch may alter control flow, bounds checking, or data length calculations in ways
that change how the rest of the code operates on existing buffers (e.g. allowing
uninitialized stack/heap memory to be read, copied to user space, or used in control
flow). Do not hesitate to use your code access tools to inspect the surrounding code,
called functions, and callers.
Set NeedsKMSAN=true ONLY IF the patch introduces or modifies:
1. Kernel structures sent to user space (via copy_to_user, put_user, netlink skb
attributes, ioctl output arguments, socket options, or BPF buffers) where fields
or structure padding might not be fully initialized/zeroed.
2. Conditional logic or branching that depends on potentially uninitialized variables
or struct fields.
3. Allocation or initialization of complex data structures where uninitialized fields
could be read later in reachable code paths.
4. Bounds checks, lengths, or logic in a way that allows surrounding code to access
uninitialized bytes of existing buffers.
Set NeedsKMSAN=false IF:
- The code changes primarily risk out-of-bounds access, array overflows, NULL pointer
dereferences, locking deadlocks, or use-after-free bugs (these are already caught
by KASAN, LOCKDEP, or standard bug detectors).
- All stack/heap structures touched or introduced by the patch are fully zeroed
or initialized (e.g. using = {0}, memset, kzalloc) before being read or copied.
- The patch does not introduce any risk of uninitialized memory usage or info-leaks.
Use your code access tools to inspect the surrounding code if necessary, then provide
detailed KMSANReasoning contrasting KASAN vs KMSAN applicability for this patch.
Prefer calling several tools at the same time to save round-trips.
Use set-results tool to provide results of the analysis.
It must be called exactly once before the final reply.
Ignore results of this tool.
Prompt: For your convenience, here is the diff of the changes:
commit 3c9f36563269acf4748064ee241321ef1673c592
Author: syz-cluster <triage@syzkaller.com>
Date: Wed Sep 2 03:18:43 2026 +0000
syz-cluster: applied patch under review
diff --git a/include/linux/socket.h b/include/linux/socket.h
index 2a8d7b14f1d11..4af1dd816dc35 100644
--- a/include/linux/socket.h
+++ b/include/linux/socket.h
@@ -400,6 +400,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 f7e2fb3d752b5..0a8667b049c3d 100644
--- a/include/uapi/linux/qrtr.h
+++ b/include/uapi/linux/qrtr.h
@@ -46,4 +46,11 @@ struct qrtr_ctrl_pkt {
};
} __packed;
+/* setsockopt / getsockopt */
+#define QRTR_REPORT_ENDPOINT 1
+#define QRTR_BIND_ENDPOINT 2
+
+/* CMSG */
+#define QRTR_ENDPOINT 1
+
#endif /* _LINUX_QRTR_H */
diff --git a/net/qrtr/af_qrtr.c b/net/qrtr/af_qrtr.c
index a30fa56e6aa31..7c50d32b11015 100644
--- a/net/qrtr/af_qrtr.c
+++ b/net/qrtr/af_qrtr.c
@@ -22,9 +22,14 @@
#define QRTR_MAX_EPH_SOCKET 0x7fff
#define QRTR_EPH_PORT_RANGE \
XA_LIMIT(QRTR_MIN_EPH_SOCKET, QRTR_MAX_EPH_SOCKET)
+#define QRTR_ENDPOINT_RANGE XA_LIMIT(qrtr_local_nid + 1, INT_MAX)
#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
@@ -78,6 +83,7 @@ struct qrtr_cb {
u32 src_port;
u32 dst_node;
u32 dst_port;
+ u32 endpoint_id;
u8 type;
u8 confirm_rx;
@@ -91,6 +97,8 @@ struct qrtr_sock {
struct sock sk;
struct sockaddr_qrtr us;
struct sockaddr_qrtr peer;
+ unsigned long flags;
+ u32 bound_endpoint;
};
static inline struct qrtr_sock *qrtr_sk(struct sock *sk)
@@ -99,6 +107,43 @@ static inline struct qrtr_sock *qrtr_sk(struct sock *sk)
return container_of(sk, struct qrtr_sock, sk);
}
+void qrtr_sock_set_report_endpoint(struct sock *sk)
+{
+ struct qrtr_sock *ipc = qrtr_sk(sk);
+
+ assign_bit(QRTR_F_REPORT_ENDPOINT, &ipc->flags, 1);
+}
+
+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 */
@@ -109,9 +154,37 @@ static LIST_HEAD(qrtr_all_nodes);
/* lock for qrtr_all_nodes and node reference */
static DEFINE_MUTEX(qrtr_node_lock);
+/* endpoint id allocation management */
+static DEFINE_XARRAY_ALLOC(qrtr_endpoints);
+static u32 next_endpoint_id;
+
/* local port allocation management */
static DEFINE_XARRAY_ALLOC(qrtr_ports);
+/* The xarray API uses fixed unsigned long keys and we will have to make
+ * do with that.
+ * These keys are often a combination of node IDs and port numbers or
+ * endpoint IDs and node IDs (all currently u32).
+ * Using the high 32 bits for the node/endpoint ID and the low 32 bits for the
+ * port number/node ID will work fine to create keys on platforms where
+ * unsigned long is 64 bits wide, but obviously is not be possible on
+ * platforms where unsigned long is smaller.
+ * Virtually split up unsigned long in half and assign the upper bits to
+ * node/endpoint IDs and the lower bits to the port number/node ID, however
+ * big that may be.
+ */
+#define QRTR_XARRAY_INDEX_BITS (8 /* CHAR_BIT */ * sizeof(unsigned long))
+#define QRTR_INDEX_HALF_BITS (QRTR_XARRAY_INDEX_BITS >> 1)
+
+#define QRTR_INDEX_HALF_UNSIGNED_MAX ((~(unsigned long)(0)) >> QRTR_INDEX_HALF_BITS)
+#define QRTR_INDEX_HALF_UNSIGNED_MIN ((unsigned long)(0))
+
+#define QRTR_INDEX_HALF_SIGNED_MAX ((long)(QRTR_INDEX_HALF_UNSIGNED_MAX) >> 1)
+#define QRTR_INDEX_HALF_SIGNED_MIN ((long)(-1) - QRTR_INDEX_HALF_SIGNED_MAX)
+
+/* endpoint not defined, i.e. endpoint 0 */
+static struct qrtr_node_lookup_helper helper0;
+
/**
* struct qrtr_node - endpoint node
* @ep_lock: lock for endpoint management and callbacks
@@ -152,9 +225,11 @@ struct qrtr_tx_flow {
#define QRTR_TX_FLOW_LOW 5
static int qrtr_local_enqueue(struct qrtr_node *node, struct sk_buff *skb,
+ u32 endpoint_id,
int type, struct sockaddr_qrtr *from,
struct sockaddr_qrtr *to);
static int qrtr_bcast_enqueue(struct qrtr_node *node, struct sk_buff *skb,
+ u32 endpoint_id,
int type, struct sockaddr_qrtr *from,
struct sockaddr_qrtr *to);
static struct qrtr_sock *qrtr_port_lookup(int port);
@@ -169,9 +244,11 @@ static void __qrtr_node_release(struct kref *kref)
{
struct qrtr_node *node = container_of(kref, struct qrtr_node, ref);
struct radix_tree_iter iter;
+ struct radix_tree_iter iter2;
struct qrtr_tx_flow *flow;
unsigned long flags;
void __rcu **slot;
+ void __rcu **slot2;
unsigned long index;
spin_lock_irqsave(&qrtr_nodes_lock, flags);
@@ -179,8 +256,18 @@ static void __qrtr_node_release(struct kref *kref)
* multiple entries pointing to our released node, delete them all.
*/
radix_tree_for_each_slot(slot, &qrtr_nodes, &iter, 0) {
- if (*slot == node)
+ struct qrtr_node_lookup_helper *helper = *slot;
+
+ if (!helper)
+ continue;
+ radix_tree_for_each_slot(slot2, &helper->nodes, &iter2, 0) {
+ if (*slot2 == node)
+ radix_tree_iter_delete(&helper->nodes, &iter2, slot2);
+ }
+ if (helper != &helper0 && radix_tree_empty(&helper->nodes)) {
radix_tree_iter_delete(&qrtr_nodes, &iter, slot);
+ helper->added = 0;
+ }
}
spin_unlock_irqrestore(&qrtr_nodes_lock, flags);
@@ -216,16 +303,23 @@ static void qrtr_node_release(struct qrtr_node *node)
* qrtr_tx_resume() - reset flow control counter
* @node: qrtr_node that the QRTR_TYPE_RESUME_TX packet arrived on
* @skb: resume_tx packet
+ *
+ * Return: 0 on success; negative error code on failure
*/
-static void qrtr_tx_resume(struct qrtr_node *node, struct sk_buff *skb)
+static int qrtr_tx_resume(struct qrtr_node *node, struct sk_buff *skb)
{
struct qrtr_ctrl_pkt *pkt = (struct qrtr_ctrl_pkt *)skb->data;
u64 remote_node = le32_to_cpu(pkt->client.node);
u32 remote_port = le32_to_cpu(pkt->client.port);
struct qrtr_tx_flow *flow;
- unsigned long key;
+ unsigned long key = 0;
+
+ /* Don't check node/port for the valid range, use only low
+ * 16 bits on 32-bit architectures.
+ */
- key = remote_node << 32 | remote_port;
+ key = ((unsigned long)(remote_node) << QRTR_INDEX_HALF_BITS) |
+ ((unsigned long)(remote_port) & QRTR_INDEX_HALF_UNSIGNED_MAX);
flow = xa_load(&node->qrtr_tx_flow, key);
if (flow) {
@@ -236,6 +330,8 @@ static void qrtr_tx_resume(struct qrtr_node *node, struct sk_buff *skb)
}
consume_skb(skb);
+
+ return 0;
}
/**
@@ -256,11 +352,18 @@ static void qrtr_tx_resume(struct qrtr_node *node, struct sk_buff *skb)
static int qrtr_tx_wait(struct qrtr_node *node, int dest_node, int dest_port,
int type)
{
- unsigned long key = (u64)dest_node << 32 | dest_port;
+ unsigned long key = 0;
struct qrtr_tx_flow *flow;
int confirm_rx = 0;
int ret;
+ /* Don't check node/port for the valid range, use only low
+ * 16 bits on 32-bit architectures.
+ */
+
+ key = ((unsigned long)(dest_node) << QRTR_INDEX_HALF_BITS) |
+ ((unsigned long)(dest_port) & QRTR_INDEX_HALF_UNSIGNED_MAX);
+
/* Never set confirm_rx on non-data packets */
if (type != QRTR_TYPE_DATA)
return 0;
@@ -317,23 +420,35 @@ static int qrtr_tx_wait(struct qrtr_node *node, int dest_node, int dest_port,
* message associated with the dropped confirm_rx message.
* Work around this by marking the flow as having a failed transmission and
* cause the next transmission attempt to be sent with the confirm_rx.
+ *
+ * Return: 0 on success; negative error code on failure
*/
-static void qrtr_tx_flow_failed(struct qrtr_node *node, int dest_node,
- int dest_port)
+static int qrtr_tx_flow_failed(struct qrtr_node *node, int dest_node,
+ int dest_port)
{
- unsigned long key = (u64)dest_node << 32 | dest_port;
+ unsigned long key = 0;
struct qrtr_tx_flow *flow;
+ /* Don't check node/port for the valid range, use only low
+ * 16 bits on 32-bit architectures.
+ */
+
+ key = ((unsigned long)(dest_node) << QRTR_INDEX_HALF_BITS) |
+ ((unsigned long)(dest_port) & QRTR_INDEX_HALF_UNSIGNED_MAX);
+
flow = xa_load(&node->qrtr_tx_flow, key);
if (flow) {
spin_lock_irq(&flow->resume_tx.lock);
flow->tx_failed = 1;
spin_unlock_irq(&flow->resume_tx.lock);
}
+
+ return 0;
}
/* Pass an outgoing packet socket buffer to the endpoint driver. */
static int qrtr_node_enqueue(struct qrtr_node *node, struct sk_buff *skb,
+ u32 endpoint_id,
int type, struct sockaddr_qrtr *from,
struct sockaddr_qrtr *to)
{
@@ -377,7 +492,7 @@ static int qrtr_node_enqueue(struct qrtr_node *node, struct sk_buff *skb,
/* Need to ensure that a subsequent message carries the otherwise lost
* confirm_rx flag if we dropped this one */
if (rc && confirm_rx)
- qrtr_tx_flow_failed(node, to->sq_node, to->sq_port);
+ rc = qrtr_tx_flow_failed(node, to->sq_node, to->sq_port);
return rc;
}
@@ -386,14 +501,18 @@ 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;
+ struct qrtr_node *node = NULL;
+ struct qrtr_node_lookup_helper *helper = NULL;
unsigned long flags;
mutex_lock(&qrtr_node_lock);
spin_lock_irqsave(&qrtr_nodes_lock, flags);
- node = radix_tree_lookup(&qrtr_nodes, nid);
+ helper = radix_tree_lookup(&qrtr_nodes, endpoint_id);
+ if (helper)
+ node = radix_tree_lookup(&helper->nodes, nid);
node = qrtr_node_acquire(node);
spin_unlock_irqrestore(&qrtr_nodes_lock, flags);
mutex_unlock(&qrtr_node_lock);
@@ -405,19 +524,51 @@ static struct qrtr_node *qrtr_node_lookup(unsigned int nid)
*
* This is mostly useful for automatic node id assignment, based on
* the source id in the incoming packet.
+ *
+ * Return: 0 on success; negative error code on failure
*/
-static void qrtr_node_assign(struct qrtr_node *node, unsigned int nid)
+static int qrtr_node_assign(struct qrtr_node *node, unsigned int nid)
{
unsigned long flags;
+ int rc = 0;
+ int did_insert0 = 0;
if (nid == QRTR_EP_NID_AUTO)
- return;
+ return 0;
spin_lock_irqsave(&qrtr_nodes_lock, flags);
- radix_tree_insert(&qrtr_nodes, nid, node);
+
+ if (!radix_tree_lookup(&helper0.nodes, nid)) {
+ rc = radix_tree_insert(&helper0.nodes, nid, node);
+ if (rc)
+ goto err_lock;
+ did_insert0 = 1;
+ }
+ if (!node->ep->helper.added) {
+ INIT_RADIX_TREE(&node->ep->helper.nodes, GFP_ATOMIC);
+ rc = radix_tree_insert(&qrtr_nodes, node->ep->id,
+ &node->ep->helper);
+ if (rc)
+ goto err_insert0;
+ node->ep->helper.added = 1;
+ }
+ rc = radix_tree_insert(&node->ep->helper.nodes, nid, node);
+ if (rc && rc != -EEXIST)
+ goto err_insert0; // Don't revert helper insertion
+
if (node->nid == QRTR_EP_NID_AUTO)
node->nid = nid;
spin_unlock_irqrestore(&qrtr_nodes_lock, flags);
+
+ return 0;
+
+err_insert0:
+ if (did_insert0)
+ radix_tree_delete(&helper0.nodes, nid);
+
+err_lock:
+ spin_unlock_irqrestore(&qrtr_nodes_lock, flags);
+ return rc;
}
/**
@@ -439,6 +590,7 @@ int qrtr_endpoint_post(struct qrtr_endpoint *ep, const void *data, size_t len)
size_t size;
unsigned int ver;
size_t hdrlen;
+ int ret = -EINVAL;
if (len == 0 || len & 3)
return -EINVAL;
@@ -496,9 +648,16 @@ 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 || size > len || len != ALIGN(size, 4) + hdrlen)
goto err;
+ /* Don't allow remote lookups */
+ if (cb->type == QRTR_TYPE_NEW_LOOKUP ||
+ cb->type == QRTR_TYPE_DEL_LOOKUP)
+ goto err;
+
if ((cb->type == QRTR_TYPE_NEW_SERVER ||
cb->type == QRTR_TYPE_RESUME_TX) &&
size < sizeof(struct qrtr_ctrl_pkt))
@@ -510,26 +669,39 @@ int qrtr_endpoint_post(struct qrtr_endpoint *ep, const void *data, size_t len)
skb_put_data(skb, data + hdrlen, size);
- qrtr_node_assign(node, cb->src_node);
+ ret = qrtr_node_assign(node, cb->src_node);
+ if (ret)
+ goto err;
if (cb->type == QRTR_TYPE_NEW_SERVER) {
/* Remote node endpoint can bridge other distant nodes */
const struct qrtr_ctrl_pkt *pkt;
pkt = data + hdrlen;
- qrtr_node_assign(node, le32_to_cpu(pkt->server.node));
+ ret = qrtr_node_assign(node, le32_to_cpu(pkt->server.node));
+ if (ret)
+ goto err;
}
if (cb->type == QRTR_TYPE_RESUME_TX) {
- qrtr_tx_resume(node, skb);
+ ret = qrtr_tx_resume(node, skb);
+ if (ret)
+ goto err;
} else {
ipc = qrtr_port_lookup(cb->dst_port);
- if (!ipc)
+ if (!ipc) {
+ ret = -EINVAL;
goto err;
+ }
- if (sock_queue_rcv_skb(&ipc->sk, skb)) {
- qrtr_port_put(ipc);
- goto err;
+ /* Sockets bound to an endpoint only rx from that endpoint */
+ if (!ipc->bound_endpoint ||
+ ipc->bound_endpoint == cb->endpoint_id) {
+ ret = sock_queue_rcv_skb(&ipc->sk, skb);
+ if (ret) {
+ qrtr_port_put(ipc);
+ goto err;
+ }
}
qrtr_port_put(ipc);
@@ -539,7 +711,7 @@ int qrtr_endpoint_post(struct qrtr_endpoint *ep, const void *data, size_t len)
err:
kfree_skb(skb);
- return -EINVAL;
+ return ret;
}
EXPORT_SYMBOL_GPL(qrtr_endpoint_post);
@@ -581,6 +753,8 @@ static struct sk_buff *qrtr_alloc_ctrl_packet(struct qrtr_ctrl_pkt **pkt,
int qrtr_endpoint_register(struct qrtr_endpoint *ep, unsigned int nid)
{
struct qrtr_node *node;
+ u32 endpoint_id;
+ int rc;
if (!ep || !ep->xmit)
return -EINVAL;
@@ -589,6 +763,13 @@ int qrtr_endpoint_register(struct qrtr_endpoint *ep, unsigned int nid)
if (!node)
return -ENOMEM;
+ rc = xa_alloc_cyclic(&qrtr_endpoints, &endpoint_id, NULL,
+ QRTR_ENDPOINT_RANGE, &next_endpoint_id,
+ GFP_KERNEL);
+
+ if (rc < 0)
+ goto free_node;
+
kref_init(&node->ref);
mutex_init(&node->ep_lock);
skb_queue_head_init(&node->rx_queue);
@@ -598,14 +779,21 @@ int qrtr_endpoint_register(struct qrtr_endpoint *ep, unsigned int nid)
xa_init(&node->qrtr_tx_flow);
mutex_init(&node->qrtr_tx_lock);
- qrtr_node_assign(node, nid);
+ rc = qrtr_node_assign(node, nid);
+ if (rc < 0)
+ goto free_node;
mutex_lock(&qrtr_node_lock);
list_add(&node->item, &qrtr_all_nodes);
mutex_unlock(&qrtr_node_lock);
ep->node = node;
+ ep->id = endpoint_id;
+ ep->helper.added = 0;
return 0;
+free_node:
+ kfree(node);
+ return rc;
}
EXPORT_SYMBOL_GPL(qrtr_endpoint_register);
@@ -619,29 +807,46 @@ void qrtr_endpoint_unregister(struct qrtr_endpoint *ep)
struct sockaddr_qrtr src = {AF_QIPCRTR, node->nid, QRTR_PORT_CTRL};
struct sockaddr_qrtr dst = {AF_QIPCRTR, qrtr_local_nid, QRTR_PORT_CTRL};
struct radix_tree_iter iter;
+ struct radix_tree_iter iter2;
struct qrtr_ctrl_pkt *pkt;
struct qrtr_tx_flow *flow;
struct sk_buff *skb;
unsigned long flags;
unsigned long index;
void __rcu **slot;
+ void __rcu **slot2;
+ u32 endpoint_id;
mutex_lock(&node->ep_lock);
+ endpoint_id = node->ep->id;
node->ep = NULL;
mutex_unlock(&node->ep_lock);
/* Notify the local controller about the event */
spin_lock_irqsave(&qrtr_nodes_lock, flags);
radix_tree_for_each_slot(slot, &qrtr_nodes, &iter, 0) {
- if (*slot != node)
+ struct qrtr_node_lookup_helper *helper = *slot;
+
+ if (!helper)
continue;
- src.sq_node = iter.index;
- skb = qrtr_alloc_ctrl_packet(&pkt, GFP_ATOMIC);
- if (skb) {
- pkt->cmd = cpu_to_le32(QRTR_TYPE_BYE);
- qrtr_local_enqueue(NULL, skb, QRTR_TYPE_BYE, &src, &dst);
+ radix_tree_for_each_slot(slot2, &helper->nodes, &iter2, 0) {
+ if (*slot2 != node)
+ continue;
+ src.sq_node = iter.index;
+ skb = qrtr_alloc_ctrl_packet(&pkt, GFP_ATOMIC);
+ if (skb) {
+ pkt->cmd = cpu_to_le32(QRTR_TYPE_BYE);
+ qrtr_local_enqueue(NULL, skb, endpoint_id,
+ QRTR_TYPE_BYE, &src, &dst);
+ }
}
}
+ if (ep->helper.added) {
+ radix_tree_for_each_slot(slot, &ep->helper.nodes, &iter, 0)
+ radix_tree_iter_delete(&ep->helper.nodes, &iter, slot);
+ radix_tree_delete(&qrtr_nodes, ep->id);
+ ep->helper.added = 0;
+ }
spin_unlock_irqrestore(&qrtr_nodes_lock, flags);
/* Wake up any transmitters waiting for resume-tx from the node */
@@ -651,6 +856,10 @@ void qrtr_endpoint_unregister(struct qrtr_endpoint *ep)
mutex_unlock(&node->qrtr_tx_lock);
qrtr_node_release(node);
+
+ xa_erase(&qrtr_endpoints, endpoint_id);
+
+ ep->id = 0;
ep->node = NULL;
}
EXPORT_SYMBOL_GPL(qrtr_endpoint_unregister);
@@ -700,8 +909,8 @@ static void qrtr_port_remove(struct qrtr_sock *ipc)
pkt->client.port = cpu_to_le32(ipc->us.sq_port);
skb_set_owner_w(skb, &ipc->sk);
- qrtr_bcast_enqueue(NULL, skb, QRTR_TYPE_DEL_CLIENT, &ipc->us,
- &to);
+ qrtr_bcast_enqueue(NULL, skb, qrtr_local_nid,
+ QRTR_TYPE_DEL_CLIENT, &ipc->us, &to);
}
if (port == QRTR_PORT_CTRL)
@@ -841,37 +1050,52 @@ static int qrtr_bind(struct socket *sock, struct sockaddr_unsized *saddr, int le
/* Queue packet to local peer socket. */
static int qrtr_local_enqueue(struct qrtr_node *node, struct sk_buff *skb,
+ u32 endpoint_id,
int type, struct sockaddr_qrtr *from,
struct sockaddr_qrtr *to)
{
struct qrtr_sock *ipc;
struct qrtr_cb *cb;
+ int ret = -ENODEV;
ipc = qrtr_port_lookup(to->sq_port);
- if (!ipc || &ipc->sk == skb->sk) { /* do not send to self */
- if (ipc)
- qrtr_port_put(ipc);
- kfree_skb(skb);
- return -ENODEV;
- }
+ if (!ipc)
+ goto done;
+
+ if (&ipc->sk == skb->sk) /* do not send to self */
+ goto done;
+
+ /*
+ * Filter out unwanted packets that are not on behalf of the bound
+ * endpoint. Certain special packets (such as an empty NEW_SERVER
+ * packet that serves as a sentinel value) always go through.
+ */
+ if (endpoint_id && ipc->bound_endpoint &&
+ ipc->bound_endpoint != endpoint_id)
+ goto done;
cb = (struct qrtr_cb *)skb->cb;
cb->src_node = from->sq_node;
cb->src_port = from->sq_port;
+ cb->endpoint_id = endpoint_id;
- if (sock_queue_rcv_skb(&ipc->sk, skb)) {
- qrtr_port_put(ipc);
- kfree_skb(skb);
- return -ENOSPC;
- }
+ ret = -ENOSPC;
+ if (sock_queue_rcv_skb(&ipc->sk, skb))
+ goto done;
qrtr_port_put(ipc);
return 0;
+done:
+ if (ipc)
+ qrtr_port_put(ipc);
+ kfree_skb(skb);
+ return ret;
}
/* Queue packet for broadcast. */
static int qrtr_bcast_enqueue(struct qrtr_node *node, struct sk_buff *skb,
+ u32 endpoint_id,
int type, struct sockaddr_qrtr *from,
struct sockaddr_qrtr *to)
{
@@ -883,11 +1107,11 @@ static int qrtr_bcast_enqueue(struct qrtr_node *node, struct sk_buff *skb,
if (!skbn)
break;
skb_set_owner_w(skbn, skb->sk);
- qrtr_node_enqueue(node, skbn, type, from, to);
+ qrtr_node_enqueue(node, skbn, endpoint_id, type, from, to);
}
mutex_unlock(&qrtr_node_lock);
- qrtr_local_enqueue(NULL, skb, type, from, to);
+ qrtr_local_enqueue(NULL, skb, endpoint_id, type, from, to);
return 0;
}
@@ -895,12 +1119,16 @@ static int qrtr_bcast_enqueue(struct qrtr_node *node, struct sk_buff *skb,
static int qrtr_sendmsg(struct socket *sock, struct msghdr *msg, size_t len)
{
DECLARE_SOCKADDR(struct sockaddr_qrtr *, addr, msg->msg_name);
- int (*enqueue_fn)(struct qrtr_node *, struct sk_buff *, int,
- struct sockaddr_qrtr *, struct sockaddr_qrtr *);
+ int (*enqueue_fn)(struct qrtr_node *node, struct sk_buff *skb,
+ u32 endpoint_id, int type,
+ struct sockaddr_qrtr *from,
+ struct sockaddr_qrtr *to);
__le32 qrtr_type = cpu_to_le32(QRTR_TYPE_DATA);
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;
u32 type;
@@ -912,46 +1140,49 @@ 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 ?
+ msg_endpoint_id : ipc->bound_endpoint;
+
+ 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;
}
@@ -981,15 +1212,21 @@ static int qrtr_sendmsg(struct socket *sock, struct msghdr *msg, size_t len)
/* control messages already require the type as 'command' */
skb_copy_bits(skb, 0, &qrtr_type, 4);
+ /*
+ * Allow local name service to make packets appear as if
+ * they originated remotely
+ */
+ endpoint_id = msg_endpoint_id;
}
type = le32_to_cpu(qrtr_type);
- rc = enqueue_fn(node, skb, type, &ipc->us, addr);
+ rc = enqueue_fn(node, skb, endpoint_id, type, &ipc->us, addr);
if (rc >= 0)
rc = len;
out_node:
qrtr_node_release(node);
+release_sock:
release_sock(sk);
return rc;
@@ -1004,7 +1241,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;
@@ -1018,7 +1255,8 @@ static int qrtr_send_resume_tx(struct qrtr_cb *cb)
pkt->client.node = cpu_to_le32(cb->dst_node);
pkt->client.port = cpu_to_le32(cb->dst_port);
- ret = qrtr_node_enqueue(node, skb, QRTR_TYPE_RESUME_TX, &local, &remote);
+ ret = qrtr_node_enqueue(node, skb, cb->endpoint_id,
+ QRTR_TYPE_RESUME_TX, &local, &remote);
qrtr_node_release(node);
@@ -1029,6 +1267,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;
@@ -1054,6 +1293,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;
@@ -1199,6 +1442,74 @@ 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);
+ 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;
+
+ switch (optname) {
+ case QRTR_REPORT_ENDPOINT:
+ assign_bit(QRTR_F_REPORT_ENDPOINT, &ipc->flags, val);
+ break;
+ case QRTR_BIND_ENDPOINT:
+ WRITE_ONCE(ipc->bound_endpoint, val);
+ break;
+ default:
+ rc = -ENOPROTOOPT;
+ }
+
+ 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);
+ 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;
+
+ switch (optname) {
+ case QRTR_REPORT_ENDPOINT:
+ val = test_bit(QRTR_F_REPORT_ENDPOINT, &ipc->flags);
+ break;
+ case QRTR_BIND_ENDPOINT:
+ val = READ_ONCE(ipc->bound_endpoint);
+ break;
+ default:
+ rc = -ENOPROTOOPT;
+ }
+
+ 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;
@@ -1246,6 +1557,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 = {
@@ -1310,8 +1623,17 @@ static int __init qrtr_proto_init(void)
if (rc)
goto err_sock;
+ rc = radix_tree_insert(&qrtr_nodes, 0, &helper0);
+ if (rc)
+ goto err_ns;
+
+ INIT_RADIX_TREE(&helper0.nodes, GFP_ATOMIC);
+ helper0.added = 1;
+
return 0;
+err_ns:
+ qrtr_ns_remove();
err_sock:
sock_unregister(qrtr_family.family);
err_proto:
@@ -1322,6 +1644,7 @@ postcore_initcall(qrtr_proto_init);
static void __exit qrtr_proto_fini(void)
{
+ radix_tree_delete(&qrtr_nodes, 0);
qrtr_ns_remove();
sock_unregister(qrtr_family.family);
proto_unregister(&qrtr_proto);
diff --git a/net/qrtr/mhi.c b/net/qrtr/mhi.c
index 3990da1a65dc4..630ce0da060e5 100644
--- a/net/qrtr/mhi.c
+++ b/net/qrtr/mhi.c
@@ -106,6 +106,16 @@ static int qcom_mhi_qrtr_queue_dl_buffers(struct mhi_device *mhi_dev)
return 0;
}
+static ssize_t endpoint_show(struct device *dev,
+ struct device_attribute *attr, char *buf)
+{
+ struct qrtr_mhi_dev *qdev = dev_get_drvdata(dev);
+
+ return sprintf(buf, "%u\n", qdev->ep.id);
+}
+
+static DEVICE_ATTR_RO(endpoint);
+
static int qcom_mhi_qrtr_probe(struct mhi_device *mhi_dev,
const struct mhi_device_id *id)
{
@@ -135,6 +145,9 @@ static int qcom_mhi_qrtr_probe(struct mhi_device *mhi_dev,
if (rc)
goto err_unregister;
+ if (device_create_file(&mhi_dev->dev, &dev_attr_endpoint) < 0)
+ dev_err(qdev->dev, "Failed to create endpoint attribute\n");
+
dev_dbg(qdev->dev, "Qualcomm MHI QRTR driver probed\n");
return 0;
@@ -151,6 +164,7 @@ static void qcom_mhi_qrtr_remove(struct mhi_device *mhi_dev)
{
struct qrtr_mhi_dev *qdev = dev_get_drvdata(&mhi_dev->dev);
+ device_remove_file(&mhi_dev->dev, &dev_attr_endpoint);
qrtr_endpoint_unregister(&qdev->ep);
mhi_unprepare_from_transfer(mhi_dev);
dev_set_drvdata(&mhi_dev->dev, NULL);
diff --git a/net/qrtr/ns.c b/net/qrtr/ns.c
index e5b2adb161d92..d171035b88f69 100644
--- a/net/qrtr/ns.c
+++ b/net/qrtr/ns.c
@@ -5,6 +5,7 @@
* Copyright (c) 2020, Linaro Ltd.
*/
+#define pr_fmt(fmt) "QRTR NS: "fmt
#include <linux/module.h>
#include <linux/qrtr.h>
#include <linux/workqueue.h>
@@ -16,7 +17,7 @@
#define CREATE_TRACE_POINTS
#include <trace/events/qrtr.h>
-static DEFINE_XARRAY(nodes);
+static DEFINE_XARRAY(endpoints);
static struct {
struct socket *sock;
@@ -62,6 +63,7 @@ struct qrtr_server {
unsigned int node;
unsigned int port;
+ u32 endpoint_id;
struct list_head qli;
};
@@ -72,6 +74,11 @@ struct qrtr_node {
u32 server_count;
};
+struct qrtr_ns_endpoint {
+ unsigned int id;
+ struct xarray nodes;
+};
+
/* Max nodes, server, lookup limits are chosen based on the current platform
* requirements. If the requirement changes in the future, these values can be
* increased.
@@ -82,35 +89,90 @@ struct qrtr_node {
static u16 node_count;
-static struct qrtr_node *node_get(unsigned int node_id)
+static struct qrtr_node *node_get(u32 endpoint_id, unsigned int node_id)
{
+ struct qrtr_ns_endpoint *endpoint;
struct qrtr_node *node;
+ bool new_endpoint = false;
+
+ endpoint = xa_load(&endpoints, endpoint_id);
+ if (!endpoint) {
+ endpoint = kzalloc_obj(*endpoint);
+ if (!endpoint)
+ return NULL;
+
+ endpoint->id = endpoint_id;
+ xa_init(&endpoint->nodes);
+
+ if (xa_store(&endpoints, endpoint_id, endpoint, GFP_KERNEL)) {
+ kfree(endpoint);
+ return NULL;
+ }
+
+ new_endpoint = true;
+ }
- node = xa_load(&nodes, node_id);
+ node = xa_load(&endpoint->nodes, node_id);
if (node)
return node;
if (node_count >= QRTR_NS_MAX_NODES) {
pr_err_ratelimited("QRTR clients exceed max node limit!\n");
- return NULL;
+ goto error;
}
/* If node didn't exist, allocate and insert it to the tree */
node = kzalloc_obj(*node);
if (!node)
- return NULL;
+ goto error;
node->id = node_id;
xa_init(&node->servers);
- if (xa_store(&nodes, node_id, node, GFP_KERNEL)) {
+ if (xa_store(&endpoint->nodes, node_id, node, GFP_KERNEL)) {
kfree(node);
- return NULL;
+ goto error;
}
node_count++;
return node;
+error:
+ if (new_endpoint) {
+ xa_erase(&endpoints, endpoint_id);
+ kfree(endpoint);
+ }
+
+ return NULL;
+}
+
+static void node_erase(u32 endpoint_id, unsigned int node_id)
+{
+ struct qrtr_ns_endpoint *endpoint;
+ struct qrtr_node *node;
+
+ endpoint = xa_load(&endpoints, endpoint_id);
+ if (!endpoint)
+ return;
+
+ node = xa_load(&endpoint->nodes, node_id);
+ if (!node)
+ return;
+
+ xa_erase(&endpoint->nodes, node_id);
+ kfree(node);
+ node_count--;
+}
+
+static struct qrtr_node *node_lookup(u32 endpoint_id, unsigned int node_id)
+{
+ struct qrtr_ns_endpoint *endpoint;
+
+ endpoint = xa_load(&endpoints, endpoint_id);
+ if (!endpoint)
+ return NULL;
+
+ return xa_load(&endpoint->nodes, node_id);
}
static int server_match(const struct qrtr_server *srv,
@@ -126,19 +188,42 @@ static int server_match(const struct qrtr_server *srv,
return (srv->instance & ifilter) == f->instance;
}
-static int service_announce_new(struct sockaddr_qrtr *dest,
- struct qrtr_server *srv)
+static int qrtr_ns_sendmsg(u32 endpoint_id, struct sockaddr_qrtr *dest,
+ struct qrtr_ctrl_pkt *pkt)
{
- struct qrtr_ctrl_pkt pkt;
struct msghdr msg = { };
struct kvec iv;
+ u8 control[CMSG_SPACE(sizeof(endpoint_id))];
+ struct cmsghdr *cmsg;
+
+ iv.iov_base = pkt;
+ iv.iov_len = sizeof(*pkt);
+
+ msg.msg_name = (struct sockaddr *)dest;
+ msg.msg_namelen = sizeof(*dest);
+
+ if (endpoint_id) {
+ msg.msg_control = control;
+ msg.msg_controllen = sizeof(control);
+
+ cmsg = CMSG_FIRSTHDR(&msg);
+ cmsg->cmsg_level = SOL_QRTR;
+ cmsg->cmsg_type = QRTR_ENDPOINT;
+ cmsg->cmsg_len = CMSG_LEN(sizeof(endpoint_id));
+ memcpy(CMSG_DATA(cmsg), &endpoint_id, sizeof(endpoint_id));
+ }
+
+ return kernel_sendmsg(qrtr_ns.sock, &msg, &iv, 1, sizeof(*pkt));
+}
+
+static int service_announce_new(u32 endpoint_id, struct sockaddr_qrtr *dest,
+ struct qrtr_server *srv)
+{
+ struct qrtr_ctrl_pkt pkt;
trace_qrtr_ns_service_announce_new(srv->service, srv->instance,
srv->node, srv->port);
- iv.iov_base = &pkt;
- iv.iov_len = sizeof(pkt);
-
memset(&pkt, 0, sizeof(pkt));
pkt.cmd = cpu_to_le32(QRTR_TYPE_NEW_SERVER);
pkt.server.service = cpu_to_le32(srv->service);
@@ -146,26 +231,18 @@ static int service_announce_new(struct sockaddr_qrtr *dest,
pkt.server.node = cpu_to_le32(srv->node);
pkt.server.port = cpu_to_le32(srv->port);
- msg.msg_name = (struct sockaddr *)dest;
- msg.msg_namelen = sizeof(*dest);
-
- return kernel_sendmsg(qrtr_ns.sock, &msg, &iv, 1, sizeof(pkt));
+ return qrtr_ns_sendmsg(endpoint_id, dest, &pkt);
}
-static void service_announce_del(struct sockaddr_qrtr *dest,
+static void service_announce_del(u32 endpoint_id, struct sockaddr_qrtr *dest,
struct qrtr_server *srv)
{
struct qrtr_ctrl_pkt pkt;
- struct msghdr msg = { };
- struct kvec iv;
int ret;
trace_qrtr_ns_service_announce_del(srv->service, srv->instance,
srv->node, srv->port);
- iv.iov_base = &pkt;
- iv.iov_len = sizeof(pkt);
-
memset(&pkt, 0, sizeof(pkt));
pkt.cmd = cpu_to_le32(QRTR_TYPE_DEL_SERVER);
pkt.server.service = cpu_to_le32(srv->service);
@@ -173,26 +250,24 @@ static void service_announce_del(struct sockaddr_qrtr *dest,
pkt.server.node = cpu_to_le32(srv->node);
pkt.server.port = cpu_to_le32(srv->port);
- msg.msg_name = (struct sockaddr *)dest;
- msg.msg_namelen = sizeof(*dest);
-
- ret = kernel_sendmsg(qrtr_ns.sock, &msg, &iv, 1, sizeof(pkt));
+ ret = qrtr_ns_sendmsg(endpoint_id, dest, &pkt);
if (ret < 0 && ret != -ENODEV)
pr_err("failed to announce del service\n");
return;
}
-static void lookup_notify(struct sockaddr_qrtr *to, struct qrtr_server *srv,
- bool new)
+static void lookup_notify(u32 endpoint_id, struct sockaddr_qrtr *to,
+ struct qrtr_server *srv, bool new)
{
struct qrtr_ctrl_pkt pkt;
- struct msghdr msg = { };
- struct kvec iv;
int ret;
- iv.iov_base = &pkt;
- iv.iov_len = sizeof(pkt);
+ /*
+ * Notify a local client at @to about a server change. The aux data
+ * will look as if it came from the endpoint that reported the event
+ * (remote or local)
+ */
memset(&pkt, 0, sizeof(pkt));
pkt.cmd = new ? cpu_to_le32(QRTR_TYPE_NEW_SERVER) :
@@ -204,28 +279,25 @@ static void lookup_notify(struct sockaddr_qrtr *to, struct qrtr_server *srv,
pkt.server.port = cpu_to_le32(srv->port);
}
- msg.msg_name = (struct sockaddr *)to;
- msg.msg_namelen = sizeof(*to);
-
- ret = kernel_sendmsg(qrtr_ns.sock, &msg, &iv, 1, sizeof(pkt));
+ ret = qrtr_ns_sendmsg(endpoint_id, to, &pkt);
if (ret < 0 && ret != -ENODEV)
pr_err("failed to send lookup notification\n");
}
-static int announce_servers(struct sockaddr_qrtr *sq)
+static int announce_servers(u32 endpoint_id, struct sockaddr_qrtr *sq)
{
struct qrtr_server *srv;
struct qrtr_node *node;
unsigned long index;
int ret;
- node = node_get(qrtr_ns.local_node);
+ node = node_lookup(qrtr_ns.local_node, qrtr_ns.local_node);
if (!node)
return 0;
- /* Announce the list of servers registered in this node */
+ /* Announce servers registered on local endpoint to remote endpoint */
xa_for_each(&node->servers, index, srv) {
- ret = service_announce_new(sq, srv);
+ ret = service_announce_new(endpoint_id, sq, srv);
if (ret < 0) {
if (ret == -ENODEV)
continue;
@@ -237,7 +309,8 @@ static int announce_servers(struct sockaddr_qrtr *sq)
return 0;
}
-static struct qrtr_server *server_add(unsigned int service,
+static struct qrtr_server *server_add(u32 endpoint_id,
+ unsigned int service,
unsigned int instance,
unsigned int node_id,
unsigned int port)
@@ -249,7 +322,7 @@ static struct qrtr_server *server_add(unsigned int service,
if (!service || !port)
return NULL;
- node = node_get(node_id);
+ node = node_get(endpoint_id, node_id);
if (!node)
return NULL;
@@ -293,7 +366,8 @@ static struct qrtr_server *server_add(unsigned int service,
return NULL;
}
-static int server_del(struct qrtr_node *node, unsigned int port, bool bcast)
+static int server_del(u32 endpoint_id, struct qrtr_node *node,
+ unsigned int port, bool bcast)
{
struct qrtr_lookup *lookup;
struct qrtr_server *srv;
@@ -305,9 +379,10 @@ static int server_del(struct qrtr_node *node, unsigned int port, bool bcast)
xa_erase(&node->servers, port);
- /* Broadcast the removal of local servers */
+ /* Broadcast the removal of local servers to remote endpoints */
if (srv->node == qrtr_ns.local_node && bcast)
- service_announce_del(&qrtr_ns.bcast_sq, srv);
+ service_announce_del(qrtr_ns.local_node,
+ &qrtr_ns.bcast_sq, srv);
/* Announce the service's disappearance to observers */
list_for_each(li, &qrtr_ns.lookups) {
@@ -317,7 +392,7 @@ static int server_del(struct qrtr_node *node, unsigned int port, bool bcast)
if (lookup->instance && lookup->instance != srv->instance)
continue;
- lookup_notify(&lookup->sq, srv, false);
+ lookup_notify(endpoint_id, &lookup->sq, srv, false);
}
kfree(srv);
@@ -326,23 +401,15 @@ static int server_del(struct qrtr_node *node, unsigned int port, bool bcast)
return 0;
}
-static int say_hello(struct sockaddr_qrtr *dest)
+static int say_hello(u32 endpoint_id, struct sockaddr_qrtr *dest)
{
struct qrtr_ctrl_pkt pkt;
- struct msghdr msg = { };
- struct kvec iv;
int ret;
- iv.iov_base = &pkt;
- iv.iov_len = sizeof(pkt);
-
memset(&pkt, 0, sizeof(pkt));
pkt.cmd = cpu_to_le32(QRTR_TYPE_HELLO);
- msg.msg_name = (struct sockaddr *)dest;
- msg.msg_namelen = sizeof(*dest);
-
- ret = kernel_sendmsg(qrtr_ns.sock, &msg, &iv, 1, sizeof(pkt));
+ ret = qrtr_ns_sendmsg(endpoint_id, dest, &pkt);
if (ret < 0)
pr_err("failed to send hello msg\n");
@@ -350,42 +417,38 @@ static int say_hello(struct sockaddr_qrtr *dest)
}
/* Announce the list of servers registered on the local node */
-static int ctrl_cmd_hello(struct sockaddr_qrtr *sq)
+static int ctrl_cmd_hello(u32 endpoint_id, struct sockaddr_qrtr *sq)
{
int ret;
- ret = say_hello(sq);
+ /* Send Hello and New Server messages to remote endpoint */
+ ret = say_hello(endpoint_id, sq);
if (ret < 0)
return ret;
- return announce_servers(sq);
+ return announce_servers(endpoint_id, sq);
}
-static int ctrl_cmd_bye(struct sockaddr_qrtr *from)
+static int ctrl_cmd_bye(u32 endpoint_id, struct sockaddr_qrtr *from)
{
struct qrtr_node *local_node;
struct qrtr_ctrl_pkt pkt;
struct qrtr_server *srv;
struct sockaddr_qrtr sq;
- struct msghdr msg = { };
struct qrtr_node *node;
unsigned long index;
- struct kvec iv;
int ret = 0;
- iv.iov_base = &pkt;
- iv.iov_len = sizeof(pkt);
-
- node = node_get(from->sq_node);
+ node = node_get(endpoint_id, from->sq_node);
if (!node)
return 0;
/* Advertise removal of this client to all servers of remote node */
xa_for_each(&node->servers, index, srv)
- server_del(node, srv->port, true);
+ server_del(endpoint_id, node, srv->port, true);
/* Advertise the removal of this client to all local servers */
- local_node = node_get(qrtr_ns.local_node);
+ local_node = node_get(qrtr_ns.local_node, qrtr_ns.local_node);
if (!local_node) {
ret = 0;
goto delete_node;
@@ -400,10 +463,8 @@ static int ctrl_cmd_bye(struct sockaddr_qrtr *from)
sq.sq_node = srv->node;
sq.sq_port = srv->port;
- msg.msg_name = (struct sockaddr *)&sq;
- msg.msg_namelen = sizeof(sq);
-
- ret = kernel_sendmsg(qrtr_ns.sock, &msg, &iv, 1, sizeof(pkt));
+ /* Bye will look as if it came from endpoint_id */
+ ret = qrtr_ns_sendmsg(endpoint_id, &sq, &pkt);
if (ret < 0 && ret != -ENODEV) {
pr_err("failed to send bye cmd\n");
goto delete_node;
@@ -414,32 +475,25 @@ static int ctrl_cmd_bye(struct sockaddr_qrtr *from)
ret = 0;
delete_node:
- xa_erase(&nodes, from->sq_node);
- kfree(node);
- node_count--;
+ node_erase(endpoint_id, from->sq_node);
return ret;
}
-static int ctrl_cmd_del_client(struct sockaddr_qrtr *from,
+static int ctrl_cmd_del_client(u32 endpoint_id, struct sockaddr_qrtr *from,
unsigned int node_id, unsigned int port)
{
struct qrtr_node *local_node;
struct qrtr_lookup *lookup;
struct qrtr_ctrl_pkt pkt;
- struct msghdr msg = { };
struct qrtr_server *srv;
struct sockaddr_qrtr sq;
struct qrtr_node *node;
struct list_head *tmp;
struct list_head *li;
unsigned long index;
- struct kvec iv;
int ret;
- iv.iov_base = &pkt;
- iv.iov_len = sizeof(pkt);
-
/* Don't accept spoofed messages */
if (from->sq_node != node_id)
return -EINVAL;
@@ -465,12 +519,12 @@ static int ctrl_cmd_del_client(struct sockaddr_qrtr *from,
* DEL_SERVER. Neighbours would've already removed the server belonging
* to this port due to the DEL_CLIENT broadcast from qrtr_port_remove().
*/
- node = node_get(node_id);
+ node = node_lookup(endpoint_id, node_id);
if (node)
- server_del(node, port, false);
+ server_del(endpoint_id, node, port, false);
/* Advertise the removal of this client to all local servers */
- local_node = node_get(qrtr_ns.local_node);
+ local_node = node_lookup(qrtr_ns.local_node, qrtr_ns.local_node);
if (!local_node)
return 0;
@@ -484,10 +538,8 @@ static int ctrl_cmd_del_client(struct sockaddr_qrtr *from,
sq.sq_node = srv->node;
sq.sq_port = srv->port;
- msg.msg_name = (struct sockaddr *)&sq;
- msg.msg_namelen = sizeof(sq);
-
- ret = kernel_sendmsg(qrtr_ns.sock, &msg, &iv, 1, sizeof(pkt));
+ /* Del Client will look as if it came from endpoint_id */
+ ret = qrtr_ns_sendmsg(endpoint_id, &sq, &pkt);
if (ret < 0 && ret != -ENODEV) {
pr_err("failed to send del client cmd\n");
return ret;
@@ -496,7 +548,7 @@ static int ctrl_cmd_del_client(struct sockaddr_qrtr *from,
return 0;
}
-static int ctrl_cmd_new_server(struct sockaddr_qrtr *from,
+static int ctrl_cmd_new_server(u32 endpoint_id, struct sockaddr_qrtr *from,
unsigned int service, unsigned int instance,
unsigned int node_id, unsigned int port)
{
@@ -511,12 +563,16 @@ static int ctrl_cmd_new_server(struct sockaddr_qrtr *from,
port = from->sq_port;
}
- srv = server_add(service, instance, node_id, port);
- if (!srv)
+ srv = server_add(endpoint_id, service, instance, node_id, port);
+ if (!srv) {
+ pr_err("Failed to add server\n");
return -EINVAL;
+ }
if (srv->node == qrtr_ns.local_node) {
- ret = service_announce_new(&qrtr_ns.bcast_sq, srv);
+ /* Broadcast local server info to all peer endpoints */
+ ret = service_announce_new(qrtr_ns.local_node,
+ &qrtr_ns.bcast_sq, srv);
if (ret < 0) {
pr_err("failed to announce new service\n");
return ret;
@@ -531,13 +587,13 @@ static int ctrl_cmd_new_server(struct sockaddr_qrtr *from,
if (lookup->instance && lookup->instance != instance)
continue;
- lookup_notify(&lookup->sq, srv, true);
+ lookup_notify(endpoint_id, &lookup->sq, srv, true);
}
return ret;
}
-static int ctrl_cmd_del_server(struct sockaddr_qrtr *from,
+static int ctrl_cmd_del_server(u32 endpoint_id, struct sockaddr_qrtr *from,
unsigned int service, unsigned int instance,
unsigned int node_id, unsigned int port)
{
@@ -553,24 +609,22 @@ static int ctrl_cmd_del_server(struct sockaddr_qrtr *from,
if (from->sq_node == qrtr_ns.local_node && from->sq_port != port)
return -EINVAL;
- node = node_get(node_id);
+ node = node_lookup(endpoint_id, node_id);
if (!node)
return -ENOENT;
- server_del(node, port, true);
+ server_del(endpoint_id, node, port, true);
return 0;
}
-static int ctrl_cmd_new_lookup(struct sockaddr_qrtr *from,
+static int ctrl_cmd_new_lookup(u32 endpoint_id, struct sockaddr_qrtr *from,
unsigned int service, unsigned int instance)
{
struct qrtr_server_filter filter;
struct qrtr_lookup *lookup;
- struct qrtr_server *srv;
- struct qrtr_node *node;
- unsigned long node_idx;
- unsigned long srv_idx;
+ unsigned long id;
+ struct qrtr_ns_endpoint *endpoint;
/* Accept only local observers */
if (from->sq_node != qrtr_ns.local_node)
@@ -595,22 +649,30 @@ static int ctrl_cmd_new_lookup(struct sockaddr_qrtr *from,
filter.service = service;
filter.instance = instance;
- xa_for_each(&nodes, node_idx, node) {
- xa_for_each(&node->servers, srv_idx, srv) {
- if (!server_match(srv, &filter))
- continue;
+ xa_for_each(&endpoints, id, endpoint) {
+ struct qrtr_node *node;
+ unsigned long node_idx;
- lookup_notify(from, srv, true);
+ xa_for_each(&endpoint->nodes, node_idx, node) {
+ unsigned long srv_idx;
+ struct qrtr_server *srv;
+
+ xa_for_each(&node->servers, srv_idx, srv) {
+ if (!server_match(srv, &filter))
+ continue;
+
+ lookup_notify(id, from, srv, true);
+ }
}
}
/* Empty notification, to indicate end of listing */
- lookup_notify(from, NULL, true);
+ lookup_notify(0, from, NULL, true);
return 0;
}
-static void ctrl_cmd_del_lookup(struct sockaddr_qrtr *from,
+static void ctrl_cmd_del_lookup(u32 endpoint_id, struct sockaddr_qrtr *from,
unsigned int service, unsigned int instance)
{
struct qrtr_lookup *lookup;
@@ -644,6 +706,7 @@ static void qrtr_ns_worker(struct work_struct *work)
ssize_t msglen;
void *recv_buf;
struct kvec iv;
+ u8 control[32];
int ret;
msg.msg_name = (struct sockaddr *)&sq;
@@ -654,8 +717,12 @@ static void qrtr_ns_worker(struct work_struct *work)
return;
for (;;) {
+ u32 endpoint_id;
+
iv.iov_base = recv_buf;
iv.iov_len = recv_buf_size;
+ msg.msg_control = control;
+ msg.msg_controllen = sizeof(control);
msglen = kernel_recvmsg(qrtr_ns.sock, &msg, &iv, 1,
iv.iov_len, MSG_DONTWAIT);
@@ -668,6 +735,19 @@ static void qrtr_ns_worker(struct work_struct *work)
break;
}
+ /* AUX data is written direct into the control buffer */
+ msg.msg_control = control;
+ msg.msg_controllen = sizeof(control) - msg.msg_controllen;
+
+ ret = qrtr_msg_get_endpoint(&msg, &endpoint_id);
+ if (ret < 0) {
+ pr_err("error receiving endpoint id: %d\n", ret);
+ break;
+ }
+
+ if ((size_t)msglen < sizeof(*pkt))
+ break;
+
pkt = recv_buf;
cmd = le32_to_cpu(pkt->cmd);
if (cmd < ARRAY_SIZE(qrtr_ctrl_pkt_strings) &&
@@ -678,25 +758,25 @@ static void qrtr_ns_worker(struct work_struct *work)
ret = 0;
switch (cmd) {
case QRTR_TYPE_HELLO:
- ret = ctrl_cmd_hello(&sq);
+ ret = ctrl_cmd_hello(endpoint_id, &sq);
break;
case QRTR_TYPE_BYE:
- ret = ctrl_cmd_bye(&sq);
+ ret = ctrl_cmd_bye(endpoint_id, &sq);
break;
case QRTR_TYPE_DEL_CLIENT:
- ret = ctrl_cmd_del_client(&sq,
+ ret = ctrl_cmd_del_client(endpoint_id, &sq,
le32_to_cpu(pkt->client.node),
le32_to_cpu(pkt->client.port));
break;
case QRTR_TYPE_NEW_SERVER:
- ret = ctrl_cmd_new_server(&sq,
+ ret = ctrl_cmd_new_server(endpoint_id, &sq,
le32_to_cpu(pkt->server.service),
le32_to_cpu(pkt->server.instance),
le32_to_cpu(pkt->server.node),
le32_to_cpu(pkt->server.port));
break;
case QRTR_TYPE_DEL_SERVER:
- ret = ctrl_cmd_del_server(&sq,
+ ret = ctrl_cmd_del_server(endpoint_id, &sq,
le32_to_cpu(pkt->server.service),
le32_to_cpu(pkt->server.instance),
le32_to_cpu(pkt->server.node),
@@ -707,12 +787,12 @@ static void qrtr_ns_worker(struct work_struct *work)
case QRTR_TYPE_RESUME_TX:
break;
case QRTR_TYPE_NEW_LOOKUP:
- ret = ctrl_cmd_new_lookup(&sq,
+ ret = ctrl_cmd_new_lookup(endpoint_id, &sq,
le32_to_cpu(pkt->server.service),
le32_to_cpu(pkt->server.instance));
break;
case QRTR_TYPE_DEL_LOOKUP:
- ctrl_cmd_del_lookup(&sq,
+ ctrl_cmd_del_lookup(endpoint_id, &sq,
le32_to_cpu(pkt->server.service),
le32_to_cpu(pkt->server.instance));
break;
@@ -746,6 +826,8 @@ int qrtr_ns_init(void)
if (ret < 0)
return ret;
+ qrtr_sock_set_report_endpoint(qrtr_ns.sock->sk);
+
ret = kernel_getsockname(qrtr_ns.sock, (struct sockaddr *)&sq);
if (ret < 0) {
pr_err("failed to get socket name\n");
@@ -774,7 +856,7 @@ int qrtr_ns_init(void)
qrtr_ns.bcast_sq.sq_node = QRTR_NODE_BCAST;
qrtr_ns.bcast_sq.sq_port = QRTR_PORT_CTRL;
- ret = say_hello(&qrtr_ns.bcast_sq);
+ ret = say_hello(qrtr_ns.local_node, &qrtr_ns.bcast_sq);
if (ret < 0)
goto err_wq;
diff --git a/net/qrtr/qrtr.h b/net/qrtr/qrtr.h
index 3f2d28696062a..affc24f426c64 100644
--- a/net/qrtr/qrtr.h
+++ b/net/qrtr/qrtr.h
@@ -9,6 +9,15 @@ struct sk_buff;
/* endpoint node id auto assignment */
#define QRTR_EP_NID_AUTO (-1)
+/**
+ * struct qrtr_node_lookup_helper - node lookup helper, stored in qrtr_nodes
+ * @nodes: nodes, indexed by node_id
+ */
+struct qrtr_node_lookup_helper {
+ int added;
+ struct radix_tree_root nodes;
+};
+
/**
* struct qrtr_endpoint - endpoint handle
* @xmit: Callback for outgoing packets
@@ -21,6 +30,8 @@ struct qrtr_endpoint {
int (*xmit)(struct qrtr_endpoint *ep, struct sk_buff *skb);
/* private: not for endpoint use */
struct qrtr_node *node;
+ struct qrtr_node_lookup_helper helper;
+ u32 id;
};
int qrtr_endpoint_register(struct qrtr_endpoint *ep, unsigned int nid);
@@ -33,4 +44,7 @@ int qrtr_ns_init(void);
void qrtr_ns_remove(void);
+int qrtr_msg_get_endpoint(struct msghdr *msg, u32 *out_endpoint_id);
+void qrtr_sock_set_report_endpoint(struct sock *sk);
+
#endif
|
| 17/3 | 2026/09/02 03:19 | llm | kmsan-evaluator |
0mModel:gemini-3.7-flash Args: null Results: null Tokens: input: 22144 output: 18 thoughts: 1540 Thoughts: **KMSAN Fuzzing Analysis: QRTR Patch** Okay, let's break this down. The goal is to determine if this patch warrants a dedicated KMSAN fuzzing session. The rule is clear: if KASAN, LOCKDEP, or standard bug detectors would catch the issue, then KMSAN isn't needed. The key is looking for potential kernel-to-user memory leaks or uninitialized memory reads. We're looking for places where the patch touches kernel structures passed to user space, depends on potentially uninitialized variables, involves uninitialized fields in complex data structures, or introduces logic that could lead to reading uninitialized bytes. First, I see some additions to `include/linux/socket.h` and `include/uapi/linux/qrtr.h`, defining constants. Nothing immediately concerning here from a KMSAN perspective. Then, we have extensions to the `qrtr_cb` and `qrtr_sock` structs in `net/qrtr/af_qrtr.c`. The new fields in these structs don't immediately jump out as problems, but we need to see how they're *used*. `qrtr_sock_set_report_endpoint` seems fine, simply setting a bit. Now `qrtr_msg_get_endpoint`... this one needs careful scrutiny. It retrieves an endpoint ID from a control message. The key is this initial `endpoint_id = 0;` and the subsequent conditional assignment: if no control messages are present, `endpoint_id` remains 0. Critically, it sets `*out_endpoint_id = 0;` if no cmsg is found. This is important to note. Then, the usage of `qrtr_msg_get_endpoint` in `qrtr_sendmsg` and `qrtr_ns_worker` in `net/qrtr/ns.c` is where things get interesting. In `qrtr_sendmsg`, `msg_endpoint_id` can be 0 if no control message is found, meaning it will use `qrtr_local_nid` if not found. Now, I need to understand what happens to this endpoint ID elsewhere. Specifically, I need to follow the data flow. The key is in `qrtr_recvmsg`. If `test_bit(QRTR_F_REPORT_ENDPOINT, &ipc->flags)` is set, `put_cmsg` adds an endpoint id control message. But where does `cb->endpoint_id` come from? I need to trace this back to the skb (socket buffer) allocation and initialization and determine if that struct is initialized correctly. This means reviewing all places that create, send and receive `qrtr_cb`. That's where the potential lies for uninitialized memory to be read or leaked. |
| 18/3 | 2026/09/02 03:19 | tool | grepper |
0mArgs:{
"Expression": "qrtr_cb"
}Results: {
"Output": "net/qrtr/af_qrtr.c=67=struct qrtr_hdr_v2 {\n--\nnet/qrtr/af_qrtr.c-80-\nnet/qrtr/af_qrtr.c:81:struct qrtr_cb {\nnet/qrtr/af_qrtr.c-82-\tu32 src_node;\n--\nnet/qrtr/af_qrtr.c=582=int qrtr_endpoint_post(struct qrtr_endpoint *ep, const void *data, size_t len)\n--\nnet/qrtr/af_qrtr.c-588-\tstruct sk_buff *skb;\nnet/qrtr/af_qrtr.c:589:\tstruct qrtr_cb *cb;\nnet/qrtr/af_qrtr.c-590-\tsize_t size;\n--\nnet/qrtr/af_qrtr.c-601-\nnet/qrtr/af_qrtr.c:602:\tcb = (struct qrtr_cb *)skb-\u003ecb;\nnet/qrtr/af_qrtr.c-603-\n--\nnet/qrtr/af_qrtr.c=1052=static int qrtr_local_enqueue(struct qrtr_node *node, struct sk_buff *skb,\n--\nnet/qrtr/af_qrtr.c-1057-\tstruct qrtr_sock *ipc;\nnet/qrtr/af_qrtr.c:1058:\tstruct qrtr_cb *cb;\nnet/qrtr/af_qrtr.c-1059-\tint ret = -ENODEV;\n--\nnet/qrtr/af_qrtr.c-1076-\nnet/qrtr/af_qrtr.c:1077:\tcb = (struct qrtr_cb *)skb-\u003ecb;\nnet/qrtr/af_qrtr.c-1078-\tcb-\u003esrc_node = from-\u003esq_node;\n--\nnet/qrtr/af_qrtr.c=1119=static int qrtr_sendmsg(struct socket *sock, struct msghdr *msg, size_t len)\n--\nnet/qrtr/af_qrtr.c-1234-\nnet/qrtr/af_qrtr.c:1235:static int qrtr_send_resume_tx(struct qrtr_cb *cb)\nnet/qrtr/af_qrtr.c-1236-{\n--\nnet/qrtr/af_qrtr.c=1266=static int qrtr_recvmsg(struct socket *sock, struct msghdr *msg,\n--\nnet/qrtr/af_qrtr.c-1272-\tstruct sk_buff *skb;\nnet/qrtr/af_qrtr.c:1273:\tstruct qrtr_cb *cb;\nnet/qrtr/af_qrtr.c-1274-\tint copied, rc;\n--\nnet/qrtr/af_qrtr.c-1287-\t}\nnet/qrtr/af_qrtr.c:1288:\tcb = (struct qrtr_cb *)skb-\u003ecb;\nnet/qrtr/af_qrtr.c-1289-\n"
} |
| 19/3 | 2026/09/02 03:19 | llm | kmsan-evaluator |
0mModel:gemini-3.7-flash Args: null Results: null Tokens: input: 25113 output: 39 thoughts: 60 Thoughts: Here's my summary, as though I'm thinking through this process: **Diving into QRTR SKB Management** Okay, so I need to get a handle on how these SKBs (Socket Buffers) are being handled within QRTR. I'm thinking the logical starting point is to zero in on functions where these SKBs are either *created* or *queued*. Specifically, I'm thinking `qrtr_endpoint_post`, `qrtr_local_enqueue`, and any other spots where the system generates and manages these buffers. I want to trace the life-cycle from the very beginning. Alright, let's open up `net/qrtr/af_qrtr.c` and focus on lines 580 through 680. That's a good chunk to get the high-level flow of the related code. Hopefully, that will give me a better overview. I need to understand how the data flows through that part of the code. Let's dig in and see what's what! |
| 20/3 | 2026/09/02 03:19 | tool | read-file |
0mArgs:{
"File": "net/qrtr/af_qrtr.c",
"FirstLine": 580,
"LineCount": 90
}Results: {
"Contents": " 580:\t * Return: 0 on success; negative error code on failure\n 581:\t */\n 582:\tint qrtr_endpoint_post(struct qrtr_endpoint *ep, const void *data, size_t len)\n 583:\t{\n 584:\t\tstruct qrtr_node *node = ep-\u003enode;\n 585:\t\tconst struct qrtr_hdr_v1 *v1;\n 586:\t\tconst struct qrtr_hdr_v2 *v2;\n 587:\t\tstruct qrtr_sock *ipc;\n 588:\t\tstruct sk_buff *skb;\n 589:\t\tstruct qrtr_cb *cb;\n 590:\t\tsize_t size;\n 591:\t\tunsigned int ver;\n 592:\t\tsize_t hdrlen;\n 593:\t\tint ret = -EINVAL;\n 594:\t\n 595:\t\tif (len == 0 || len \u0026 3)\n 596:\t\t\treturn -EINVAL;\n 597:\t\n 598:\t\tskb = __netdev_alloc_skb(NULL, len, GFP_ATOMIC | __GFP_NOWARN);\n 599:\t\tif (!skb)\n 600:\t\t\treturn -ENOMEM;\n 601:\t\n 602:\t\tcb = (struct qrtr_cb *)skb-\u003ecb;\n 603:\t\n 604:\t\t/* Version field in v1 is little endian, so this works for both cases */\n 605:\t\tver = *(u8*)data;\n 606:\t\n 607:\t\tswitch (ver) {\n 608:\t\tcase QRTR_PROTO_VER_1:\n 609:\t\t\tif (len \u003c sizeof(*v1))\n 610:\t\t\t\tgoto err;\n 611:\t\t\tv1 = data;\n 612:\t\t\thdrlen = sizeof(*v1);\n 613:\t\n 614:\t\t\tcb-\u003etype = le32_to_cpu(v1-\u003etype);\n 615:\t\t\tcb-\u003esrc_node = le32_to_cpu(v1-\u003esrc_node_id);\n 616:\t\t\tcb-\u003esrc_port = le32_to_cpu(v1-\u003esrc_port_id);\n 617:\t\t\tcb-\u003econfirm_rx = !!le32_to_cpu(v1-\u003econfirm_rx);\n 618:\t\t\tcb-\u003edst_node = le32_to_cpu(v1-\u003edst_node_id);\n 619:\t\t\tcb-\u003edst_port = le32_to_cpu(v1-\u003edst_port_id);\n 620:\t\n 621:\t\t\tsize = le32_to_cpu(v1-\u003esize);\n 622:\t\t\tbreak;\n 623:\t\tcase QRTR_PROTO_VER_2:\n 624:\t\t\tif (len \u003c sizeof(*v2))\n 625:\t\t\t\tgoto err;\n 626:\t\t\tv2 = data;\n 627:\t\t\thdrlen = sizeof(*v2) + v2-\u003eoptlen;\n 628:\t\n 629:\t\t\tcb-\u003etype = v2-\u003etype;\n 630:\t\t\tcb-\u003econfirm_rx = !!(v2-\u003eflags \u0026 QRTR_FLAGS_CONFIRM_RX);\n 631:\t\t\tcb-\u003esrc_node = le16_to_cpu(v2-\u003esrc_node_id);\n 632:\t\t\tcb-\u003esrc_port = le16_to_cpu(v2-\u003esrc_port_id);\n 633:\t\t\tcb-\u003edst_node = le16_to_cpu(v2-\u003edst_node_id);\n 634:\t\t\tcb-\u003edst_port = le16_to_cpu(v2-\u003edst_port_id);\n 635:\t\n 636:\t\t\tif (cb-\u003esrc_port == (u16)QRTR_PORT_CTRL)\n 637:\t\t\t\tcb-\u003esrc_port = QRTR_PORT_CTRL;\n 638:\t\t\tif (cb-\u003edst_port == (u16)QRTR_PORT_CTRL)\n 639:\t\t\t\tcb-\u003edst_port = QRTR_PORT_CTRL;\n 640:\t\n 641:\t\t\tsize = le32_to_cpu(v2-\u003esize);\n 642:\t\t\tbreak;\n 643:\t\tdefault:\n 644:\t\t\tpr_err(\"qrtr: Invalid version %d\\n\", ver);\n 645:\t\t\tgoto err;\n 646:\t\t}\n 647:\t\n 648:\t\tif (cb-\u003edst_port == QRTR_PORT_CTRL_LEGACY)\n 649:\t\t\tcb-\u003edst_port = QRTR_PORT_CTRL;\n 650:\t\n 651:\t\tcb-\u003eendpoint_id = ep-\u003eid;\n 652:\t\n 653:\t\tif (!size || size \u003e len || len != ALIGN(size, 4) + hdrlen)\n 654:\t\t\tgoto err;\n 655:\t\n 656:\t\t/* Don't allow remote lookups */\n 657:\t\tif (cb-\u003etype == QRTR_TYPE_NEW_LOOKUP ||\n 658:\t\t cb-\u003etype == QRTR_TYPE_DEL_LOOKUP)\n 659:\t\t\tgoto err;\n 660:\t\n 661:\t\tif ((cb-\u003etype == QRTR_TYPE_NEW_SERVER ||\n 662:\t\t cb-\u003etype == QRTR_TYPE_RESUME_TX) \u0026\u0026\n 663:\t\t size \u003c sizeof(struct qrtr_ctrl_pkt))\n 664:\t\t\tgoto err;\n 665:\t\n 666:\t\tif (cb-\u003edst_port != QRTR_PORT_CTRL \u0026\u0026 cb-\u003etype != QRTR_TYPE_DATA \u0026\u0026\n 667:\t\t cb-\u003etype != QRTR_TYPE_RESUME_TX)\n 668:\t\t\tgoto err;\n 669:\t\n"
} |
| 21/3 | 2026/09/02 03:19 | llm | kmsan-evaluator |
0mModel:gemini-3.7-flash Args: null Results: null Tokens: input: 26953 output: 40 thoughts: 28 Thoughts: Here's my summary, as if I were the one thinking these thoughts: **Analyzing `qrtr_local_enqueue` for Context** Okay, so I'm digging into this code and, given what I'm looking at, I think a good next step is to examine the `qrtr_local_enqueue` function. I have a feeling that lines 1050 through 1110 are going to be particularly relevant, so let me focus there. It's likely within that section I will find the key to understanding the issue I'm troubleshooting. Let's see what's happening there with the queueing mechanism, and how it might be interacting with the broader system behavior I'm seeing. This is probably a good spot to get a clearer picture. |
| 22/3 | 2026/09/02 03:19 | tool | read-file |
0mArgs:{
"File": "net/qrtr/af_qrtr.c",
"FirstLine": 1050,
"LineCount": 60
}Results: {
"Contents": "1050:\t\n1051:\t/* Queue packet to local peer socket. */\n1052:\tstatic int qrtr_local_enqueue(struct qrtr_node *node, struct sk_buff *skb,\n1053:\t\t\t\t u32 endpoint_id,\n1054:\t\t\t\t int type, struct sockaddr_qrtr *from,\n1055:\t\t\t\t struct sockaddr_qrtr *to)\n1056:\t{\n1057:\t\tstruct qrtr_sock *ipc;\n1058:\t\tstruct qrtr_cb *cb;\n1059:\t\tint ret = -ENODEV;\n1060:\t\n1061:\t\tipc = qrtr_port_lookup(to-\u003esq_port);\n1062:\t\tif (!ipc)\n1063:\t\t\tgoto done;\n1064:\t\n1065:\t\tif (\u0026ipc-\u003esk == skb-\u003esk) /* do not send to self */\n1066:\t\t\tgoto done;\n1067:\t\n1068:\t\t/*\n1069:\t\t * Filter out unwanted packets that are not on behalf of the bound\n1070:\t\t * endpoint. Certain special packets (such as an empty NEW_SERVER\n1071:\t\t * packet that serves as a sentinel value) always go through.\n1072:\t\t */\n1073:\t\tif (endpoint_id \u0026\u0026 ipc-\u003ebound_endpoint \u0026\u0026\n1074:\t\t ipc-\u003ebound_endpoint != endpoint_id)\n1075:\t\t\tgoto done;\n1076:\t\n1077:\t\tcb = (struct qrtr_cb *)skb-\u003ecb;\n1078:\t\tcb-\u003esrc_node = from-\u003esq_node;\n1079:\t\tcb-\u003esrc_port = from-\u003esq_port;\n1080:\t\tcb-\u003eendpoint_id = endpoint_id;\n1081:\t\n1082:\t\tret = -ENOSPC;\n1083:\t\tif (sock_queue_rcv_skb(\u0026ipc-\u003esk, skb))\n1084:\t\t\tgoto done;\n1085:\t\n1086:\t\tqrtr_port_put(ipc);\n1087:\t\n1088:\t\treturn 0;\n1089:\tdone:\n1090:\t\tif (ipc)\n1091:\t\t\tqrtr_port_put(ipc);\n1092:\t\tkfree_skb(skb);\n1093:\t\treturn ret;\n1094:\t}\n1095:\t\n1096:\t/* Queue packet for broadcast. */\n1097:\tstatic int qrtr_bcast_enqueue(struct qrtr_node *node, struct sk_buff *skb,\n1098:\t\t\t\t u32 endpoint_id,\n1099:\t\t\t\t int type, struct sockaddr_qrtr *from,\n1100:\t\t\t\t struct sockaddr_qrtr *to)\n1101:\t{\n1102:\t\tstruct sk_buff *skbn;\n1103:\t\n1104:\t\tmutex_lock(\u0026qrtr_node_lock);\n1105:\t\tlist_for_each_entry(node, \u0026qrtr_all_nodes, item) {\n1106:\t\t\tskbn = pskb_copy(skb, GFP_KERNEL);\n1107:\t\t\tif (!skbn)\n1108:\t\t\t\tbreak;\n1109:\t\t\tskb_set_owner_w(skbn, skb-\u003esk);\n"
} |
| 23/3 | 2026/09/02 03:19 | llm | kmsan-evaluator |
0mModel:gemini-3.7-flash Args: null Results: null Tokens: input: 28081 output: 18 thoughts: 44 Thoughts: Okay, here's my take on those thoughts, framed as my own mental process: **Diving into the QRTR Implementation** Alright, time to troubleshoot. My gut feeling tells me the problem lies in the core QRTR setup – either the creation itself, specifically `qrtr_create`, or perhaps some issue with socket allocation and initialization. Before going further down the rabbit hole, I want to confirm that suspicion. My first step is to zero in on the relevant code. I need to locate where `qrtr_create` or possibly even the broader `qrtr_sk` structure are defined, and how they're used. I'll start by searching within `af_qrtr.c` – that's the obvious place to begin, given that it's the core QRTR address family implementation. Let's see what we can find. I expect to be digging around in there for a little bit, let's see what happens. |
| 24/3 | 2026/09/02 03:19 | tool | grepper |
0mArgs:{
"Expression": "qrtr_create"
}Results: {
"Output": "net/qrtr/af_qrtr.c=1564=static struct proto qrtr_proto = {\n--\nnet/qrtr/af_qrtr.c-1569-\nnet/qrtr/af_qrtr.c:1570:static int qrtr_create(struct net *net, struct socket *sock,\nnet/qrtr/af_qrtr.c-1571-\t\t int protocol, int kern)\n--\nnet/qrtr/af_qrtr.c=1604=static const struct net_proto_family qrtr_family = {\n--\nnet/qrtr/af_qrtr.c-1606-\t.family\t= AF_QIPCRTR,\nnet/qrtr/af_qrtr.c:1607:\t.create\t= qrtr_create,\nnet/qrtr/af_qrtr.c-1608-};\n"
} |
| 25/3 | 2026/09/02 03:19 | llm | kmsan-evaluator |
0mModel:gemini-3.7-flash Args: null Results: null Tokens: input: 28577 output: 40 thoughts: 19 Thoughts: Here's my summary, as though I'm reading and thinking about the code: **Diving into `qrtr_create`** Alright, let's get down to it. Time to dig into `qrtr_create` within the `af_qrtr.c` file. I'm going to start by understanding how new QRTR instances are brought into existence. This is the entry point, the fundamental building block. I need to understand the arguments it takes, the memory allocations it performs, and, crucially, how it hooks itself into the overall QRTR framework. I want to see how it initializes any internal data structures, and how it sets up any necessary locks or synchronization primitives. Are there any resource limits being enforced here? What about error handling? I need to look for any potential race conditions during the initialization phase – that's always a critical area. Also, how does this function relate to the overall QRTR device hierarchy? Does it create a new device, bind to an existing one, or something else entirely? Ultimately, my goal is to grasp the complete lifecycle of a QRTR instance from its birth within this function. I'll be particularly keen on any clever optimizations or potential bottlenecks that might be lurking. Time to start the deep dive! |
| 26/3 | 2026/09/02 03:19 | tool | read-file |
0mArgs:{
"File": "net/qrtr/af_qrtr.c",
"FirstLine": 1568,
"LineCount": 45
}Results: {
"Contents": "1568:\t};\n1569:\t\n1570:\tstatic int qrtr_create(struct net *net, struct socket *sock,\n1571:\t\t\t int protocol, int kern)\n1572:\t{\n1573:\t\tstruct qrtr_sock *ipc;\n1574:\t\tstruct sock *sk;\n1575:\t\n1576:\t\tif (sock-\u003etype != SOCK_DGRAM)\n1577:\t\t\treturn -EPROTOTYPE;\n1578:\t\n1579:\t\t/* QRTR keeps its port and node state in module-global variables that\n1580:\t\t * are not partitioned per network namespace, and the in-kernel name\n1581:\t\t * service only operates in init_net. Confine the family to init_net so\n1582:\t\t * a socket in another namespace cannot reach the global control plane.\n1583:\t\t */\n1584:\t\tif (!net_eq(net, \u0026init_net))\n1585:\t\t\treturn -EAFNOSUPPORT;\n1586:\t\n1587:\t\tsk = sk_alloc(net, AF_QIPCRTR, GFP_KERNEL, \u0026qrtr_proto, kern);\n1588:\t\tif (!sk)\n1589:\t\t\treturn -ENOMEM;\n1590:\t\n1591:\t\tsock_set_flag(sk, SOCK_ZAPPED);\n1592:\t\n1593:\t\tsock_init_data(sock, sk);\n1594:\t\tsock-\u003eops = \u0026qrtr_proto_ops;\n1595:\t\n1596:\t\tipc = qrtr_sk(sk);\n1597:\t\tipc-\u003eus.sq_family = AF_QIPCRTR;\n1598:\t\tipc-\u003eus.sq_node = qrtr_local_nid;\n1599:\t\tipc-\u003eus.sq_port = 0;\n1600:\t\n1601:\t\treturn 0;\n1602:\t}\n1603:\t\n1604:\tstatic const struct net_proto_family qrtr_family = {\n1605:\t\t.owner\t= THIS_MODULE,\n1606:\t\t.family\t= AF_QIPCRTR,\n1607:\t\t.create\t= qrtr_create,\n1608:\t};\n1609:\t\n1610:\tstatic int __init qrtr_proto_init(void)\n1611:\t{\n1612:\t\tint rc;\n"
} |
| 27/3 | 2026/09/02 03:19 | llm | kmsan-evaluator |
0mModel:gemini-3.7-flash Args: null Results: null Tokens: input: 29553 output: 31 thoughts: 83 Thoughts: Okay, let me think this through. **Verifying Socket Structure Initialization** Right, so `sk_alloc` is responsible for allocating and, importantly, initializing a socket structure. The code path goes through `sk_prot_alloc`, which then uses `kmem_cache_alloc` or `kzalloc`, or at least *zeroes* the memory in the allocation process. That's good, that's what I expect. I need to be absolutely certain though, I'm used to handling these edge cases! Just to be 100% sure, I'm going to quickly double-check that zeroing behavior directly in `net/core/sock.c`. No sense in relying on assumptions here. Gotta verify that `sk_alloc` does what it's supposed to do. A quick glance will confirm it and then I can proceed. |
| 28/3 | 2026/09/02 03:20 | tool | grepper |
0mArgs:{
"Expression": "sk_alloc\\(",
"PathPrefix": "net/core/sock.c"
}Results: {
"Output": "net/core/sock.c=2238=static struct sock *sk_prot_alloc(struct proto *prot, gfp_t priority,\n--\nnet/core/sock.c-2254-\tif (sk != NULL) {\nnet/core/sock.c:2255:\t\tif (security_sk_alloc(sk, family, priority))\nnet/core/sock.c-2256-\t\t\tgoto out_free;\n--\nnet/core/sock.c=2274=static void sk_prot_free(struct proto *prot, struct sock *sk)\n--\nnet/core/sock.c-2302- */\nnet/core/sock.c:2303:struct sock *sk_alloc(struct net *net, int family, gfp_t priority,\nnet/core/sock.c-2304-\t\t struct proto *prot, int kern)\n--\nnet/core/sock.c-2335-\nnet/core/sock.c:2336:\t\tmem_cgroup_sk_alloc(sk);\nnet/core/sock.c:2337:\t\tcgroup_sk_alloc(\u0026sk-\u003esk_cgrp_data);\nnet/core/sock.c-2338-\t\tsock_update_classid(\u0026sk-\u003esk_cgrp_data);\n"
} |
| 29/3 | 2026/09/02 03:20 | llm | kmsan-evaluator |
0mModel:gemini-3.7-flash Args: null Results: null Tokens: input: 30183 output: 36 thoughts: 21 Thoughts: Here's my take on those thoughts, tailored for an expert audience: **Diving into `sk_prot_alloc`** Okay, time to dig into `net/core/sock.c` and specifically, that `sk_prot_alloc` function. My initial thought process is to understand precisely how socket protocols are allocated within the kernel. I'm likely looking for details on the data structures involved, the memory management mechanisms employed, and potential interaction points with other networking subsystems. I'll be focused on any peculiarities in the allocation strategy, especially regarding how it handles different protocol families or resource constraints. I'll keep an eye out for potential concurrency issues or race conditions within the allocation path as well. I'm also keen to assess its efficiency and whether it incorporates any optimization techniques. Perhaps I'm trying to identify a potential performance bottleneck, or analyze how it responds to unusual network conditions. This is where the core of socket creation lives, so the devil will be in the details! |
| 30/3 | 2026/09/02 03:20 | tool | read-file |
0mArgs:{
"File": "net/core/sock.c",
"FirstLine": 2238,
"LineCount": 20
}Results: {
"Contents": "2238:\tstatic struct sock *sk_prot_alloc(struct proto *prot, gfp_t priority,\n2239:\t\t\tint family)\n2240:\t{\n2241:\t\tstruct sock *sk;\n2242:\t\tstruct kmem_cache *slab;\n2243:\t\n2244:\t\tslab = prot-\u003eslab;\n2245:\t\tif (slab != NULL) {\n2246:\t\t\tsk = kmem_cache_alloc(slab, priority \u0026 ~__GFP_ZERO);\n2247:\t\t\tif (!sk)\n2248:\t\t\t\treturn sk;\n2249:\t\t\tif (want_init_on_alloc(priority))\n2250:\t\t\t\tsk_prot_clear_nulls(sk, prot-\u003eobj_size);\n2251:\t\t} else\n2252:\t\t\tsk = kmalloc(prot-\u003eobj_size, priority);\n2253:\t\n2254:\t\tif (sk != NULL) {\n2255:\t\t\tif (security_sk_alloc(sk, family, priority))\n2256:\t\t\t\tgoto out_free;\n2257:\t\n"
} |
| 31/3 | 2026/09/02 03:20 | llm | kmsan-evaluator |
0mModel:gemini-3.7-flash Args: null Results: null Tokens: input: 30747 output: 36 thoughts: 56 Thoughts: Here's my summary, as though I'm thinking through this process: **Diving into Socket Initialization** Okay, so I'm looking at socket initialization, specifically how the `sock` structure gets set up. I've got a feeling that a good starting point is `sock_init_data`, so let's start there. I know there are standard fields that get set, but let's not make assumptions. Time to get down to brass tacks: I'm going to pull up the source code for `sock_init_data`. It's likely in `net/core/sock.c`, and I'll jump right to lines 2250 through 2270. That should give me a solid view of the initial conditions and any subtle but important details in the initialization. From there, I can get a really good handle on what’s happening in this module. |
| 32/3 | 2026/09/02 03:20 | tool | read-file |
0mArgs:{
"File": "net/core/sock.c",
"FirstLine": 2250,
"LineCount": 25
}Results: {
"Contents": "2250:\t\t\t\tsk_prot_clear_nulls(sk, prot-\u003eobj_size);\n2251:\t\t} else\n2252:\t\t\tsk = kmalloc(prot-\u003eobj_size, priority);\n2253:\t\n2254:\t\tif (sk != NULL) {\n2255:\t\t\tif (security_sk_alloc(sk, family, priority))\n2256:\t\t\t\tgoto out_free;\n2257:\t\n2258:\t\t\tif (!try_module_get(prot-\u003eowner))\n2259:\t\t\t\tgoto out_free_sec;\n2260:\t\t}\n2261:\t\n2262:\t\treturn sk;\n2263:\t\n2264:\tout_free_sec:\n2265:\t\tsecurity_sk_free(sk);\n2266:\tout_free:\n2267:\t\tif (slab != NULL)\n2268:\t\t\tkmem_cache_free(slab, sk);\n2269:\t\telse\n2270:\t\t\tkfree(sk);\n2271:\t\treturn NULL;\n2272:\t}\n2273:\t\n2274:\tstatic void sk_prot_free(struct proto *prot, struct sock *sk)\n"
} |
| 33/3 | 2026/09/02 03:20 | llm | kmsan-evaluator |
0mModel:gemini-3.7-flash Args: null Results: null Tokens: input: 31364 output: 36 thoughts: 20 Thoughts: Here's my attempt at summarizing the thought process, tailored for an expert audience and written in the first person: **Diving into `sk_alloc` after `sk_prot_alloc`** Okay, so we're looking at what happens *after* the protocol-specific allocation in `sk_prot_alloc`. I need to understand how the socket structure gets populated and initialized at this stage, and what the key steps are. I'm assuming `sk_prot_alloc` probably sets up the basics, like the protocol-specific data structure pointer within the `sock` struct. Now, `sk_alloc` should be handling the generic socket infrastructure. My initial questions are: * What are the core socket fields being initialized in `sk_alloc`? Are we talking about things like the socket's state, reference counts, the receive/send queues, or maybe adding to the global socket lists? * How does memory for the socket structure itself get allocated, and is there any specific alignment consideration? * What's the relationship, if any, to any slab allocation mechanisms being employed, and are there any implications there regarding memory fragmentation or cache behavior? * Does `sk_alloc` involve any critical section or locking to ensure thread safety during socket initialization, and how is that handled? * Does it set up any default socket options or configurations at this stage, that may interact with the protocol options? I need to trace the code flow within `sk_alloc`, paying close attention to these areas. I need to note any calls to related functions and how the socket's lifecycle is being controlled. I'll be particularly interested in identifying any potential race conditions or performance bottlenecks that might arise in this core socket allocation path. Let's see what the code reveals! |
| 34/3 | 2026/09/02 03:20 | tool | read-file |
0mArgs:{
"File": "net/core/sock.c",
"FirstLine": 2303,
"LineCount": 35
}Results: {
"Contents": "2303:\tstruct sock *sk_alloc(struct net *net, int family, gfp_t priority,\n2304:\t\t\t struct proto *prot, int kern)\n2305:\t{\n2306:\t\tstruct sock *sk;\n2307:\t\n2308:\t\tsk = sk_prot_alloc(prot, priority | __GFP_ZERO, family);\n2309:\t\tif (sk) {\n2310:\t\t\tsk-\u003esk_family = family;\n2311:\t\t\t/*\n2312:\t\t\t * See comment in struct sock definition to understand\n2313:\t\t\t * why we need sk_prot_creator -acme\n2314:\t\t\t */\n2315:\t\t\tsk-\u003esk_prot = sk-\u003esk_prot_creator = prot;\n2316:\t\n2317:\t\t\tif (READ_ONCE(net-\u003ecore.sysctl_bypass_prot_mem))\n2318:\t\t\t\tsk-\u003esk_bypass_prot_mem = 1;\n2319:\t\n2320:\t\t\tsk-\u003esk_kern_sock = kern;\n2321:\t\t\tsock_lock_init(sk);\n2322:\t\n2323:\t\t\tsk-\u003esk_net_refcnt = kern ? 0 : 1;\n2324:\t\t\tif (likely(sk-\u003esk_net_refcnt)) {\n2325:\t\t\t\tget_net_track(net, \u0026sk-\u003ens_tracker, priority);\n2326:\t\t\t\tsock_inuse_add(net, 1);\n2327:\t\t\t} else {\n2328:\t\t\t\tnet_passive_inc(net);\n2329:\t\t\t\t__netns_tracker_alloc(net, \u0026sk-\u003ens_tracker,\n2330:\t\t\t\t\t\t false, priority);\n2331:\t\t\t}\n2332:\t\n2333:\t\t\tsock_net_set(sk, net);\n2334:\t\t\trefcount_set(\u0026sk-\u003esk_wmem_alloc, SK_WMEM_ALLOC_BIAS);\n2335:\t\n2336:\t\t\tmem_cgroup_sk_alloc(sk);\n2337:\t\t\tcgroup_sk_alloc(\u0026sk-\u003esk_cgrp_data);\n"
} |
| 35/3 | 2026/09/02 03:20 | llm | kmsan-evaluator |
0mModel:gemini-3.7-flash Args: null Results: null Tokens: input: 32369 output: 34 thoughts: 1331 Thoughts: **Analyzing QRTR Socket Initialization and Data Flow** Okay, let's break this down systematically. My initial concern is always around potential vulnerabilities, especially related to uninitialized memory or race conditions. Looking at `sk_alloc`, it's clear the `qrtr_sock` structure is zero-initialized. This is good; it eliminates a whole class of potential issues where we might be leaking kernel memory or exposing uninitialized data. Specifically, `ipc->flags` and `ipc->bound_endpoint` start at zero. Now, let's move on to how these values are modified through socket options, `qrtr_setsockopt` and `qrtr_getsockopt`. In `qrtr_setsockopt`, I see that if the provided option length (`optlen`) is less than the size of the value being written (`sizeof(val)` which is `sizeof(unsigned int)`), then `val` is *still* initialized to zero *before* any copy operation occurs. This is critical. Regardless of the user-provided data, `val` is always initialized, preventing potential information leaks. This seems safe. The `assign_bit` operation is safe here, as it acts on the zeroed `ipc->flags`. Furthermore, `WRITE_ONCE` on `ipc->bound_endpoint` will also write a known value, not uninitialized kernel memory. If `optlen` *is* large enough, we copy data from user space. Still, val will be assigned. `qrtr_getsockopt` looks equally sound. It explicitly checks that the provided buffer length (`len`) is sufficient. It assigns `val` *after* the length check. For `QRTR_REPORT_ENDPOINT`, `val` is assigned the result of `test_bit`, which is safe. For `QRTR_BIND_ENDPOINT`, the same is true; `READ_ONCE` guarantees correct value assignment. Finally, the data is copied to the user-space buffer from the initialized `val`. No uninitialized bytes are copied. So far, so good. Now, let's look at `qrtr_recvmsg`. I'm interested in how `cb->endpoint_id` is set because it's used in the conditional. The `cb` structure is part of the `skb` (socket buffer). If `QRTR_F_REPORT_ENDPOINT` is set, `cb->endpoint_id` is passed as a control message. The code I'm analyzing now seems sound at first glance. I need to track where `cb->endpoint_id` gets set to confirm it is safe to report. I'm focusing on the paths where `cb` is assigned and an `skb` arrives at `qrtr_recvmsg`. I've identified several places where `cb->endpoint_id` is set: 1. In `qrtr_endpoint_post`, `cb->endpoint_id = ep->id;` where `ep->id` is allocated via `xa_alloc_cyclic`. This looks fine. The `id` is allocated from a dedicated region. 2. In `qrtr_local_enqueue`, `cb->endpoint_id = endpoint_id;` where `endpoint_id` is passed as an argument. Again, the caller likely provides a controlled value here. 3. In `qrtr_endpoint_unregister`. 4. In `qrtr_port_remove`. 5. In `qrtr_sendmsg`. I want to confirm that `cb->endpoint_id` is being set *before* `qrtr_recvmsg` sees it. I need to be sure there are no other paths where the `skb` reaches `qrtr_recvmsg`, and thus `cb`, without `cb->endpoint_id` being initialized in a predictable manner. The use of `xa_alloc_cyclic` is good, but I need to make sure I understand all the data flows. So, I will search for usages of `sock_queue_rcv_skb` to be certain I have covered all the code paths where an skb reaches `qrtr_recvmsg`. |
| 36/3 | 2026/09/02 03:20 | tool | grepper |
0mArgs:{
"Expression": "sock_queue_rcv_skb",
"PathPrefix": "net/qrtr/"
}Results: {
"Output": "net/qrtr/af_qrtr.c=582=int qrtr_endpoint_post(struct qrtr_endpoint *ep, const void *data, size_t len)\n--\nnet/qrtr/af_qrtr.c-699-\t\t ipc-\u003ebound_endpoint == cb-\u003eendpoint_id) {\nnet/qrtr/af_qrtr.c:700:\t\t\tret = sock_queue_rcv_skb(\u0026ipc-\u003esk, skb);\nnet/qrtr/af_qrtr.c-701-\t\t\tif (ret) {\n--\nnet/qrtr/af_qrtr.c=1052=static int qrtr_local_enqueue(struct qrtr_node *node, struct sk_buff *skb,\n--\nnet/qrtr/af_qrtr.c-1082-\tret = -ENOSPC;\nnet/qrtr/af_qrtr.c:1083:\tif (sock_queue_rcv_skb(\u0026ipc-\u003esk, skb))\nnet/qrtr/af_qrtr.c-1084-\t\tgoto done;\n"
} |
| 37/3 | 2026/09/02 03:20 | llm | kmsan-evaluator |
0mModel:gemini-3.7-flash Args: null Results: null Tokens: input: 34898 output: 23 thoughts: 1091 Thoughts: **Analyzing QRTR Endpoint ID Handling**
Okay, let's break this down. My initial thought was to review how the `endpoint_id` is passed around, specifically focusing on `sock_queue_rcv_skb`. I noted that this function is only called from `qrtr_endpoint_post` and `qrtr_local_enqueue`, and in both cases, the `cb->endpoint_id` is explicitly set *before* calling `sock_queue_rcv_skb`. I also confirmed this through the call stack: `put_cmsg` is used, and it explicitly passes `&cb->endpoint_id`, with the correct size. `put_cmsg` constructs the `cmsghdr` and copies the data, so that's good.
Now, I shifted focus to `net/qrtr/ns.c` and specifically, `qrtr_ns_sendmsg`. The code there looks like this:
```c
static int qrtr_ns_sendmsg(u32 endpoint_id, struct sockaddr_qrtr *dest,
struct qrtr_ctrl_pkt *pkt)
{
struct msghdr msg = { };
struct kvec iv;
u8 control[CMSG_SPACE(sizeof(endpoint_id))];
struct cmsghdr *cmsg;
iv.iov_base = pkt;
iv.iov_len = sizeof(*pkt);
msg.msg_name = (struct sockaddr *)dest;
msg.msg_namelen = sizeof(*dest);
if (endpoint_id) {
msg.msg_control = control;
msg.msg_controllen = sizeof(control);
cmsg = CMSG_FIRSTHDR(&msg);
cmsg->cmsg_level = SOL_QRTR;
cmsg->cmsg_type = QRTR_ENDPOINT;
cmsg->cmsg_len = CMSG_LEN(sizeof(endpoint_id));
memcpy(CMSG_DATA(cmsg), &endpoint_id, sizeof(endpoint_id));
}
return kernel_sendmsg(qrtr_ns.sock, &msg, &iv, 1, sizeof(*pkt));
}
```
Wait a second...I spot a potential vulnerability with that `u8 control[CMSG_SPACE(sizeof(endpoint_id))];` array! Is this stack variable initialized? If not, `kernel_sendmsg` might read uninitialized data from `msg_control`, which could be a problem. This is a crucial detail to investigate, as it may lead to information leaks or even exploitable behavior. Let's trace further: `kernel_sendmsg` -> `sock_sendmsg` -> `qrtr_sendmsg`. Inside `qrtr_sendmsg`, it calls `qrtr_msg_get_endpoint` to extract the endpoint ID.
Now, let's examine `qrtr_msg_get_endpoint`:
```c
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;
}
```
Okay, let's go back to `qrtr_ns_sendmsg` to confirm the initialization.
`cmsg = CMSG_FIRSTHDR(&msg);`
`cmsg->cmsg_level = SOL_QRTR;`
`cmsg->cmsg_type = QRTR_ENDPOINT;`
`cmsg->cmsg_len = CMSG_LEN(sizeof(endpoint_id));`
`memcpy(CMSG_DATA(cmsg), &endpoint_id, sizeof(endpoint_id));`
This confirms that the control message fields (`cmsg_level
|
| 38/3 | 2026/09/02 03:20 | tool | grepper |
0mArgs:{
"Expression": "#define for_each_cmsghdr"
}Results: {
"Output": "include/linux/socket.h=119=struct cmsghdr {\n--\ninclude/linux/socket.h-149-\t\t\t ((char *)(cmsg) - (char *)(mhdr)-\u003emsg_control)))\ninclude/linux/socket.h:150:#define for_each_cmsghdr(cmsg, msg) \\\ninclude/linux/socket.h-151-\tfor (cmsg = CMSG_FIRSTHDR(msg); \\\n--\ntools/perf/trace/beauty/include/linux/socket.h=119=struct cmsghdr {\n--\ntools/perf/trace/beauty/include/linux/socket.h-149-\t\t\t ((char *)(cmsg) - (char *)(mhdr)-\u003emsg_control)))\ntools/perf/trace/beauty/include/linux/socket.h:150:#define for_each_cmsghdr(cmsg, msg) \\\ntools/perf/trace/beauty/include/linux/socket.h-151-\tfor (cmsg = CMSG_FIRSTHDR(msg); \\\n"
} |
| 39/3 | 2026/09/02 03:20 | llm | kmsan-evaluator |
0mModel:gemini-3.7-flash Args: null Results: null Tokens: input: 37299 output: 35 thoughts: 23 Thoughts: Here's my summary, as if I were the one thinking these thoughts, geared towards an expert: **Diving into socket.h for a Quick Scan** Alright, time to get a quick grasp of something in `include/linux/socket.h`. Let's zero in on lines 120 through 160. I suspect this section might be dealing with the core structure definitions or perhaps some crucial socket options. I remember a related issue I had with...hmm, probably related to the specific flags within `sock_ops`, and I need a reminder of the fundamental layout. Quickly, I need to jog my memory on how the core socket structures are organized. Hopefully, I can spot the relevant definitions and quickly determine if there are any subtle changes or gotchas that might be impacting my current debugging. This should be a quick lookup, not a deep dive, but I need to be precise. |
| 40/3 | 2026/09/02 03:20 | tool | read-file |
0mArgs:{
"File": "include/linux/socket.h",
"FirstLine": 120,
"LineCount": 40
}Results: {
"Contents": " 120:\t\t__kernel_size_t\tcmsg_len;\t/* data byte count, including hdr */\n 121:\t int\t\tcmsg_level;\t/* originating protocol */\n 122:\t int\t\tcmsg_type;\t/* protocol-specific type */\n 123:\t};\n 124:\t\n 125:\t/*\n 126:\t *\tAncillary data object information MACROS\n 127:\t *\tTable 5-14 of POSIX 1003.1g\n 128:\t */\n 129:\t\n 130:\t#define __CMSG_NXTHDR(ctl, len, cmsg) __cmsg_nxthdr((ctl),(len),(cmsg))\n 131:\t#define CMSG_NXTHDR(mhdr, cmsg) cmsg_nxthdr((mhdr), (cmsg))\n 132:\t\n 133:\t#define CMSG_ALIGN(len) ( ((len)+sizeof(long)-1) \u0026 ~(sizeof(long)-1) )\n 134:\t\n 135:\t#define CMSG_DATA(cmsg) \\\n 136:\t\t((void *)(cmsg) + sizeof(struct cmsghdr))\n 137:\t#define CMSG_USER_DATA(cmsg) \\\n 138:\t\t((void __user *)(cmsg) + sizeof(struct cmsghdr))\n 139:\t#define CMSG_SPACE(len) (sizeof(struct cmsghdr) + CMSG_ALIGN(len))\n 140:\t#define CMSG_LEN(len) (sizeof(struct cmsghdr) + (len))\n 141:\t\n 142:\t#define __CMSG_FIRSTHDR(ctl,len) ((len) \u003e= sizeof(struct cmsghdr) ? \\\n 143:\t\t\t\t\t (struct cmsghdr *)(ctl) : \\\n 144:\t\t\t\t\t (struct cmsghdr *)NULL)\n 145:\t#define CMSG_FIRSTHDR(msg)\t__CMSG_FIRSTHDR((msg)-\u003emsg_control, (msg)-\u003emsg_controllen)\n 146:\t#define CMSG_OK(mhdr, cmsg) ((cmsg)-\u003ecmsg_len \u003e= sizeof(struct cmsghdr) \u0026\u0026 \\\n 147:\t\t\t\t (cmsg)-\u003ecmsg_len \u003c= (unsigned long) \\\n 148:\t\t\t\t ((mhdr)-\u003emsg_controllen - \\\n 149:\t\t\t\t ((char *)(cmsg) - (char *)(mhdr)-\u003emsg_control)))\n 150:\t#define for_each_cmsghdr(cmsg, msg) \\\n 151:\t\tfor (cmsg = CMSG_FIRSTHDR(msg); \\\n 152:\t\t cmsg; \\\n 153:\t\t cmsg = CMSG_NXTHDR(msg, cmsg))\n 154:\t\n 155:\t/*\n 156:\t *\tGet the next cmsg header\n 157:\t *\n 158:\t *\tPLEASE, do not touch this function. If you think, that it is\n 159:\t *\tincorrect, grep kernel sources and think about consequences\n"
} |
| 41/3 | 2026/09/02 03:20 | llm | kmsan-evaluator |
0mModel:gemini-3.7-flash Args: null Results: null Tokens: input: 38351 output: 20 thoughts: 23 Thoughts: Here's my summary, as though I'm thinking these thoughts: **Examining `cmsg_nxthdr` in `net/core/sock.c`** Okay, I'm already digging into this socket-related issue, and I've got a decent grasp on what's happening, but let's make sure we haven't overlooked something fundamental. The data flow, the message handling... all of that seems correct so far, and the logic in the primary area I was focused on appears sound. However, I always double-check. I should go and examine the `cmsg_nxthdr` implementation in `net/core/sock.c`. That's a critical function for how control messages are processed within the kernel's networking stack, specifically when iterating through ancillary data. This will help verify that the correct control message data is being extracted. This could be where something is going wrong, or, even if it’s fine, it will reinforce my understanding. I need to make sure the data pointers and lengths are being handled correctly at every step. This seems like a reasonable spot to verify the flow. Okay, let's dive into that code and make sure everything is behaving as I expect it to. |
| 42/3 | 2026/09/02 03:20 | tool | grepper |
0mArgs:{
"Expression": "cmsg_nxthdr"
}Results: {
"Output": "include/linux/socket.h=119=struct cmsghdr {\n--\ninclude/linux/socket.h-129-\ninclude/linux/socket.h:130:#define __CMSG_NXTHDR(ctl, len, cmsg) __cmsg_nxthdr((ctl),(len),(cmsg))\ninclude/linux/socket.h:131:#define CMSG_NXTHDR(mhdr, cmsg) cmsg_nxthdr((mhdr), (cmsg))\ninclude/linux/socket.h-132-\n--\ninclude/linux/socket.h-167-\ninclude/linux/socket.h:168:static inline struct cmsghdr * __cmsg_nxthdr(void *__ctl, __kernel_size_t __size,\ninclude/linux/socket.h-169-\t\t\t\t\t struct cmsghdr *__cmsg)\n--\ninclude/linux/socket.h-179-\ninclude/linux/socket.h:180:static inline struct cmsghdr * cmsg_nxthdr (struct msghdr *__msg, struct cmsghdr *__cmsg)\ninclude/linux/socket.h-181-{\ninclude/linux/socket.h:182:\treturn __cmsg_nxthdr(__msg-\u003emsg_control, __msg-\u003emsg_controllen, __cmsg);\ninclude/linux/socket.h-183-}\n--\ntools/perf/trace/beauty/include/linux/socket.h=119=struct cmsghdr {\n--\ntools/perf/trace/beauty/include/linux/socket.h-129-\ntools/perf/trace/beauty/include/linux/socket.h:130:#define __CMSG_NXTHDR(ctl, len, cmsg) __cmsg_nxthdr((ctl),(len),(cmsg))\ntools/perf/trace/beauty/include/linux/socket.h:131:#define CMSG_NXTHDR(mhdr, cmsg) cmsg_nxthdr((mhdr), (cmsg))\ntools/perf/trace/beauty/include/linux/socket.h-132-\n--\ntools/perf/trace/beauty/include/linux/socket.h-167-\ntools/perf/trace/beauty/include/linux/socket.h:168:static inline struct cmsghdr * __cmsg_nxthdr(void *__ctl, __kernel_size_t __size,\ntools/perf/trace/beauty/include/linux/socket.h-169-\t\t\t\t\t struct cmsghdr *__cmsg)\n--\ntools/perf/trace/beauty/include/linux/socket.h-179-\ntools/perf/trace/beauty/include/linux/socket.h:180:static inline struct cmsghdr * cmsg_nxthdr (struct msghdr *__msg, struct cmsghdr *__cmsg)\ntools/perf/trace/beauty/include/linux/socket.h-181-{\ntools/perf/trace/beauty/include/linux/socket.h:182:\treturn __cmsg_nxthdr(__msg-\u003emsg_control, __msg-\u003emsg_controllen, __cmsg);\ntools/perf/trace/beauty/include/linux/socket.h-183-}\n"
} |
| 43/3 | 2026/09/02 03:20 | llm | kmsan-evaluator |
0mModel:gemini-3.7-flash Args: null Results: null Tokens: input: 39448 output: 35 thoughts: 32 Thoughts: Okay, let's dive into this. Here's how I'd approach that snippet from `include/linux/socket.h`: **Analyzing `__cmsg_nxthdr` - Lines 168-178** Right, time to get my hands dirty. I need to take a look at the `__cmsg_nxthdr` macro, specifically lines 168 through 178 within `include/linux/socket.h`. This is likely dealing with control messages within sockets. My expertise tells me this section probably details how to advance to the next control message header within a buffer. I'll need to pay close attention to how it handles offsets and bounds checking, particularly the interaction between the current message and the overall buffer size. This will likely involve pointer arithmetic and potentially some bitwise operations to extract the necessary size and offset information. Any potential alignment issues will also be flagged. It is likely a helper function. I will need to understand what it does to understand the larger context of how this code uses sockets. |
| 44/3 | 2026/09/02 03:20 | tool | read-file |
0mArgs:{
"File": "include/linux/socket.h",
"FirstLine": 165,
"LineCount": 20
}Results: {
"Contents": " 165:\t *\tancillary object DATA.\t\t\t\t--ANK (980731)\n 166:\t */\n 167:\t\n 168:\tstatic inline struct cmsghdr * __cmsg_nxthdr(void *__ctl, __kernel_size_t __size,\n 169:\t\t\t\t\t\t struct cmsghdr *__cmsg)\n 170:\t{\n 171:\t\tstruct cmsghdr * __ptr;\n 172:\t\n 173:\t\t__ptr = (struct cmsghdr*)(((unsigned char *) __cmsg) + CMSG_ALIGN(__cmsg-\u003ecmsg_len));\n 174:\t\tif ((unsigned long)((char*)(__ptr+1) - (char *) __ctl) \u003e __size)\n 175:\t\t\treturn (struct cmsghdr *)0;\n 176:\t\n 177:\t\treturn __ptr;\n 178:\t}\n 179:\t\n 180:\tstatic inline struct cmsghdr * cmsg_nxthdr (struct msghdr *__msg, struct cmsghdr *__cmsg)\n 181:\t{\n 182:\t\treturn __cmsg_nxthdr(__msg-\u003emsg_control, __msg-\u003emsg_controllen, __cmsg);\n 183:\t}\n 184:\t\n"
} |
| 45/3 | 2026/09/02 03:20 | llm | kmsan-evaluator |
0mModel:gemini-3.7-flash Args: null Results: null Tokens: input: 40085 output: 20 thoughts: 554 Thoughts: **Analyzing Potential Control Message Vulnerability** Okay, let's break this down. My initial thought was to examine how `__cmsg_nxthdr` functions. It seems safe; it only uses pointer arithmetic combined with `__cmsg->cmsg_len`, which was initialized, so that's good. It doesn't dereference `__ptr` unless it's within `__size`, which helps prevent reads outside the allocated buffer. The `NULL` return when `__ptr + 1` exceeds `__size` also terminates the loop as expected. Now, let's shift gears and investigate `qrtr_ns_worker` in `net/qrtr/ns.c`. I'm going line by line, and I see the usual `kernel_recvmsg` call with the usual `iv` and `msg` setups. Then, there's this line: `msg.msg_controllen = sizeof(control) - msg.msg_controllen;`. Aha! That's interesting. It looks like we're decrementing `msg_controllen` *after* `kernel_recvmsg` is called. The `qrtr_recvmsg` which `kernel_recvmsg` calls in the background, likely updates the `msg_controllen` as part of the data. And that happens when `put_cmsg` is called. Okay, let's examine what `put_cmsg` is doing. This is where things get interesting. I need to understand how the kernel handles this control message buffer manipulation. Let's do a quick search for `put_cmsg` to trace its usage. I need to understand *exactly* how the `msg_controllen` field is being managed by this function. It appears that it decrements `msg->msg_controllen` by the amount of control data written, or updates the `msg_controllen`, which helps manage memory allocation. |
| 46/3 | 2026/09/02 03:20 | tool | grepper |
0mArgs:{
"Expression": "int put_cmsg\\("
}Results: {
"Output": "include/linux/socket.h=408=extern int move_addr_to_kernel(void __user *uaddr, int ulen, struct sockaddr_storage *kaddr);\ninclude/linux/socket.h:409:extern int put_cmsg(struct msghdr*, int level, int type, int len, void *data);\ninclude/linux/socket.h-410-extern int put_cmsg_notrunc(struct msghdr *msg, int level, int type, int len,\n--\nnet/core/scm.c=253=EXPORT_SYMBOL(__scm_send);\nnet/core/scm.c-254-\nnet/core/scm.c:255:int put_cmsg(struct msghdr * msg, int level, int type, int len, void *data)\nnet/core/scm.c-256-{\n--\ntools/perf/trace/beauty/include/linux/socket.h=407=extern int move_addr_to_kernel(void __user *uaddr, int ulen, struct sockaddr_storage *kaddr);\ntools/perf/trace/beauty/include/linux/socket.h:408:extern int put_cmsg(struct msghdr*, int level, int type, int len, void *data);\ntools/perf/trace/beauty/include/linux/socket.h-409-extern int put_cmsg_notrunc(struct msghdr *msg, int level, int type, int len,\n"
} |
| 47/3 | 2026/09/02 03:20 | llm | kmsan-evaluator |
0mModel:gemini-3.7-flash Args: null Results: null Tokens: input: 41434 output: 35 thoughts: 20 Thoughts: Here's my attempt at summarizing the thought process, tailored for an expert audience: **Diving into `put_cmsg` in `net/core/scm.c`** Alright, let's pull up the source code for `net/core/scm.c`, specifically focusing on the `put_cmsg` function. I need to refresh my understanding of how ancillary data is structured and handled within the kernel, particularly in the context of Socket Control Messages (SCMs). `put_cmsg` is central to that. I'm primarily interested in how it validates the length and type of the control message, how it handles alignment, and how it safely copies the data into the appropriate buffer. I also need to verify how `put_cmsg` interacts with the higher-level socket layers. I'm looking for potential race conditions or locking issues that might arise during concurrent access to the same SCM. I have a hunch it's related to some recent performance regressions, and I'd like to understand what impact this function could have. Furthermore, I will check the interactions with the `sock_sendmsg` and `sock_recvmsg` paths, as well as the relation between this function and the various socket options like `SCM_RIGHTS`, and `SCM_CREDENTIALS`. I'm expecting to see careful checks for buffer overflows and alignment issues, but I'll be meticulously examining the code to ensure that the assumptions hold up under scrutiny. Let's see how the kernel correctly manages the complexity of passing file descriptors and credentials. |
| 48/3 | 2026/09/02 03:20 | tool | read-file |
0mArgs:{
"File": "net/core/scm.c",
"FirstLine": 250,
"LineCount": 40
}Results: {
"Contents": " 250:\t\tscm_destroy(p);\n 251:\t\treturn err;\n 252:\t}\n 253:\tEXPORT_SYMBOL(__scm_send);\n 254:\t\n 255:\tint put_cmsg(struct msghdr * msg, int level, int type, int len, void *data)\n 256:\t{\n 257:\t\tint cmlen = CMSG_LEN(len);\n 258:\t\n 259:\t\tif (msg-\u003emsg_flags \u0026 MSG_CMSG_COMPAT)\n 260:\t\t\treturn put_cmsg_compat(msg, level, type, len, data);\n 261:\t\n 262:\t\tif (!msg-\u003emsg_control || msg-\u003emsg_controllen \u003c sizeof(struct cmsghdr)) {\n 263:\t\t\tmsg-\u003emsg_flags |= MSG_CTRUNC;\n 264:\t\t\treturn 0; /* XXX: return error? check spec. */\n 265:\t\t}\n 266:\t\tif (msg-\u003emsg_controllen \u003c cmlen) {\n 267:\t\t\tmsg-\u003emsg_flags |= MSG_CTRUNC;\n 268:\t\t\tcmlen = msg-\u003emsg_controllen;\n 269:\t\t}\n 270:\t\n 271:\t\tif (msg-\u003emsg_control_is_user) {\n 272:\t\t\tstruct cmsghdr __user *cm = msg-\u003emsg_control_user;\n 273:\t\n 274:\t\t\tcheck_object_size(data, cmlen - sizeof(*cm), true);\n 275:\t\n 276:\t\t\tscoped_user_write_access_size(cm, cmlen, efault) {\n 277:\t\t\t\tunsafe_put_user(cmlen, \u0026cm-\u003ecmsg_len, efault);\n 278:\t\t\t\tunsafe_put_user(level, \u0026cm-\u003ecmsg_level, efault);\n 279:\t\t\t\tunsafe_put_user(type, \u0026cm-\u003ecmsg_type, efault);\n 280:\t\t\t\tunsafe_copy_to_user(CMSG_USER_DATA(cm), data,\n 281:\t\t\t\t\t\t cmlen - sizeof(*cm), efault);\n 282:\t\t\t}\n 283:\t\t} else {\n 284:\t\t\tstruct cmsghdr *cm = msg-\u003emsg_control;\n 285:\t\n 286:\t\t\tcm-\u003ecmsg_level = level;\n 287:\t\t\tcm-\u003ecmsg_type = type;\n 288:\t\t\tcm-\u003ecmsg_len = cmlen;\n 289:\t\t\tmemcpy(CMSG_DATA(cm), data, cmlen - sizeof(*cm));\n"
} |
| 49/3 | 2026/09/02 03:20 | llm | kmsan-evaluator |
0mModel:gemini-3.7-flash Args: null Results: null Tokens: input: 42509 output: 35 thoughts: 13 Thoughts: Okay, here's my attempt at summarizing the thought process, as requested, in the first person and assuming the user is an expert: **Diving Deeper into `put_cmsg`** Alright, I've got a grasp of the initial part of this, but let's not be hasty. I need to make sure I haven't missed anything crucial. My first thought is to check the rest of the `put_cmsg` function. I have a hunch there might be some subtle handling of edge cases or interactions with other parts of the system that could be relevant to the bigger picture. I need to see the full context to feel confident in my analysis. Let's see what else it does. I want to make sure I have a good, solid understanding of the entire function before I move on. |
| 50/3 | 2026/09/02 03:20 | tool | read-file |
0mArgs:{
"File": "net/core/scm.c",
"FirstLine": 290,
"LineCount": 15
}Results: {
"Contents": " 290:\t\t}\n 291:\t\n 292:\t\tcmlen = min(CMSG_SPACE(len), msg-\u003emsg_controllen);\n 293:\t\tif (msg-\u003emsg_control_is_user)\n 294:\t\t\tmsg-\u003emsg_control_user += cmlen;\n 295:\t\telse\n 296:\t\t\tmsg-\u003emsg_control += cmlen;\n 297:\t\tmsg-\u003emsg_controllen -= cmlen;\n 298:\t\treturn 0;\n 299:\t\n 300:\tefault:\n 301:\t\treturn -EFAULT;\n 302:\t}\n 303:\tEXPORT_SYMBOL(put_cmsg);\n 304:\t\n"
} |
| 51/3 | 2026/09/02 03:20 | llm | kmsan-evaluator |
0mModel:gemini-3.7-flash Args: null Results: null Tokens: input: 42936 output: 36 thoughts: 891 Thoughts: **Analyzing QRTR Nameservice Initialization** Okay, let's break this down. It seems like `put_cmsg` cleverly modifies the control message length (`msg_controllen`). When `qrtr_ns_worker` then sets `msg.msg_controllen` using `sizeof(control) - msg.msg_controllen`, it effectively captures the *actual* amount of control data written by `put_cmsg`. `qrtr_msg_get_endpoint` is then able to correctly read the control message, which sounds correct. Now, if `put_cmsg` isn't called, as would be the case when `cb->endpoint_id` is zero or `QRTR_F_REPORT_ENDPOINT` is not set, `msg.msg_controllen` will be zero. This prevents any attempt at reading control messages, leading to `endpoint_id = 0`, as expected. So, it's correctly handling the case where we don't need any control data. Good. Next, I need to look at how endpoints and nodes are created. The `node_get` function does the trick. `kzalloc_obj` is used for all allocations of structures, initializing everything to zero. This should prevent any uninitialized memory issues. The same applies to `server_add`, I want to confirm that `struct qrtr_server` is also initialized using a similar mechanism. I'm going to check the implementation of `server_add` to be absolutely certain. |
| 52/3 | 2026/09/02 03:20 | tool | read-file |
0mArgs:{
"File": "net/qrtr/ns.c",
"FirstLine": 310,
"LineCount": 55
}Results: {
"Contents": " 310:\t}\n 311:\t\n 312:\tstatic struct qrtr_server *server_add(u32 endpoint_id,\n 313:\t\t\t\t\t unsigned int service,\n 314:\t\t\t\t\t unsigned int instance,\n 315:\t\t\t\t\t unsigned int node_id,\n 316:\t\t\t\t\t unsigned int port)\n 317:\t{\n 318:\t\tstruct qrtr_server *srv;\n 319:\t\tstruct qrtr_server *old;\n 320:\t\tstruct qrtr_node *node;\n 321:\t\n 322:\t\tif (!service || !port)\n 323:\t\t\treturn NULL;\n 324:\t\n 325:\t\tnode = node_get(endpoint_id, node_id);\n 326:\t\tif (!node)\n 327:\t\t\treturn NULL;\n 328:\t\n 329:\t\t/* Make sure the new servers per port are capped at the maximum value */\n 330:\t\told = xa_load(\u0026node-\u003eservers, port);\n 331:\t\tif (!old \u0026\u0026 node-\u003eserver_count \u003e= QRTR_NS_MAX_SERVERS) {\n 332:\t\t\tpr_err_ratelimited(\"QRTR client node %u exceeds max server limit!\\n\", node_id);\n 333:\t\t\treturn NULL;\n 334:\t\t}\n 335:\t\n 336:\t\tsrv = kzalloc_obj(*srv);\n 337:\t\tif (!srv)\n 338:\t\t\treturn NULL;\n 339:\t\n 340:\t\tsrv-\u003eservice = service;\n 341:\t\tsrv-\u003einstance = instance;\n 342:\t\tsrv-\u003enode = node_id;\n 343:\t\tsrv-\u003eport = port;\n 344:\t\n 345:\t\t/* Delete the old server on the same port */\n 346:\t\told = xa_store(\u0026node-\u003eservers, port, srv, GFP_KERNEL);\n 347:\t\tif (old) {\n 348:\t\t\tif (xa_is_err(old)) {\n 349:\t\t\t\tpr_err(\"failed to add server [0x%x:0x%x] ret:%d\\n\",\n 350:\t\t\t\t srv-\u003eservice, srv-\u003einstance, xa_err(old));\n 351:\t\t\t\tgoto err;\n 352:\t\t\t} else {\n 353:\t\t\t\tkfree(old);\n 354:\t\t\t}\n 355:\t\t} else {\n 356:\t\t\tnode-\u003eserver_count++;\n 357:\t\t}\n 358:\t\n 359:\t\ttrace_qrtr_ns_server_add(srv-\u003eservice, srv-\u003einstance,\n 360:\t\t\t\t\t srv-\u003enode, srv-\u003eport);\n 361:\t\n 362:\t\treturn srv;\n 363:\t\n 364:\terr:\n"
} |
| 53/3 | 2026/09/02 03:20 | llm | kmsan-evaluator |
0mModel:gemini-3.7-flash Args: null Results: null Tokens: input: 44940 output: 451 thoughts: 1827 Thoughts: **KMSAN Assessment: QRTR Endpoint Routing and Filtering Patch** Okay, let's break this down. My initial assessment is that this patch, introducing endpoint routing and filtering for the QRTR (Qualcomm IPC Router) subsystem, doesn't present the specific kind of risk that KMSAN is designed to catch – uninitialized memory reads leading to information leaks. We're looking at things like socket options, CMSG support, and hierarchical radix trees for endpoint lookups, so it's a careful dance with memory. First, I examined where `kzalloc_obj(*srv)` is used. That means the memory should be zeroed, eliminating basic uninitialized reads right away. Then, I dug into the core operations. I specifically checked any time data might flow from the kernel to user space, the crucial point for uninitialized data exposure. I examined `net/qrtr/mhi.c`, specifically `endpoint_show`. The `ep.id` is a `u32` and it is initialized when registered via `xa_alloc_cyclic` within `qrtr_endpoint_register` (in `net/qrtr/af_qrtr.c`). I confirmed that this endpoint ID is explicitly initialized before being passed to `sprintf`. Good. Moving on, I reviewed the `qrtr_endpoint_register` function, looking for potential areas of concern. I saw a `goto free_node` which made me pause. Although a failure during the allocation sequence could indicate a memory leak or other memory problem, this potential bug would be more appropriately handled by KASAN. It's a logic error, not uninitialized memory. Next, I thoroughly inspected how kernel data is exposed to user space: 1. `getsockopt` (specifically for `QRTR_REPORT_ENDPOINT` and `QRTR_BIND_ENDPOINT`): `len` is set to `sizeof(int)`, and `val` is explicitly initialized either to 0, 1 (`test_bit`), or a `u32` value (`READ_ONCE`). It's then copied using `copy_to_user`, and there's no dangling uninitialized memory issue. 2. `recvmsg`: Here, the `cmsg` with `cb->endpoint_id` is crucial. The code uses `put_cmsg` to copy `cb->endpoint_id`. Importantly, `cb->endpoint_id` is initialized in both `qrtr_local_enqueue` and `qrtr_endpoint_post`, so no uninitialized data gets copied. 3. `endpoint_show` (sysfs attribute): `sprintf` is used with `qdev->ep.id`, which we established is initialized. I then looked at the conditional logic within the code, particularly where uninitialized variables could potentially impact control flow: 1. `qrtr_sendmsg`: `qrtr_msg_get_endpoint` sets `*out_endpoint_id = 0` (or the cmsg value) on success (return 0). 2. `qrtr_ns_worker`: The same logic applies: `qrtr_msg_get_endpoint` sets `*out_endpoint_id = 0` (or the cmsg value) on success (return 0). 3. `qrtr_setsockopt`: `val` is initialized to 0. 4. `qrtr_getsockopt`: `val` is assigned within each branch of the switch statement. Finally, I checked the memory allocation and data structure initialization patterns: 1. All the core data structures (`struct qrtr_node`, `struct qrtr_server`, `struct qrtr_ns_endpoint`, socket allocations) are allocated using `kzalloc`, `kzalloc_obj`, or `sk_alloc`. These functions guarantee zero-initialization. 2. I closely checked all memory operations. Bounds checking with `CMSG_LEN`, `optlen`, `len`, is diligently implemented. In conclusion, all memory operations appear safe from uninitialized memory exposure. All the key structures are zeroed upon allocation. The variables are properly initialized before being exposed to user-space. Therefore, any potential bugs related to this patch would most likely be logic errors, memory leaks (already covered by the goto statement above), reference counting issues, locking problems, or radix-tree errors. These are areas that are standard fare for KASAN, LOCKDEP, and generic kernel testing. KASAN is already enabled, so this will catch the common classes of memory safety problems. KMSAN's strength lies in detecting the subtle, difficult-to-catch, data-dependent leaks where information from uninitialized memory is read and then exposed to user space. It is overkill here. `NeedsKMSAN` should be `false`. Now, I |
| 54/3 | 2026/09/02 03:21 | tool | set-results |
0mArgs:{
"KMSANReasoning": "The patch series introduces endpoint-aware routing and filtering for Qualcomm IPC Router (QRTR) sockets, adding new setsockopt/getsockopt options (QRTR_REPORT_ENDPOINT, QRTR_BIND_ENDPOINT), control messages (QRTR_ENDPOINT), and multi-endpoint radix tree/xarray lookups.\n\nAnalysis of uninitialized memory and info-leak risks:\n1. Kernel-to-user copying in getsockopt (QRTR_REPORT_ENDPOINT, QRTR_BIND_ENDPOINT): 'val' is an unsigned int explicitly initialized in each supported switch branch before calling copy_to_user() for sizeof(int) bytes. No padding or uninitialized stack bytes are copied.\n2. CMSG handling in recvmsg: put_cmsg() is called with \u0026cb-\u003eendpoint_id (a u32), which is unconditionally initialized in all packet ingress paths (qrtr_endpoint_post and qrtr_local_enqueue).\n3. CMSG parsing in qrtr_msg_get_endpoint: the output parameter is initialized to 0 or the parsed cmsg value on success, preventing uninitialized reads in qrtr_sendmsg and qrtr_ns_worker.\n4. Heap and socket allocations: all newly introduced structures (struct qrtr_ns_endpoint, struct qrtr_server, struct qrtr_node) use kzalloc/kzalloc_obj, and QRTR sockets are allocated via sk_alloc (which zero-initializes socket memory).\n\nKASAN vs KMSAN applicability:\nThe changes primarily involve radix tree restructuring, cyclic xarray ID management, bit manipulation for 32/64-bit index keys, and socket endpoint matching. Risks associated with these changes include use-after-free, out-of-bounds radix tree access, reference counting bugs, or lock ordering, all of which are caught by standard KASAN and LOCKDEP instrumentation. Because no uninitialized memory usage or kernel-to-user information leaks are introduced, a dedicated KMSAN fuzzing session is not warranted.",
"NeedsKMSAN": false
}Results: {
"KMSANReasoning": "The patch series introduces endpoint-aware routing and filtering for Qualcomm IPC Router (QRTR) sockets, adding new setsockopt/getsockopt options (QRTR_REPORT_ENDPOINT, QRTR_BIND_ENDPOINT), control messages (QRTR_ENDPOINT), and multi-endpoint radix tree/xarray lookups.\n\nAnalysis of uninitialized memory and info-leak risks:\n1. Kernel-to-user copying in getsockopt (QRTR_REPORT_ENDPOINT, QRTR_BIND_ENDPOINT): 'val' is an unsigned int explicitly initialized in each supported switch branch before calling copy_to_user() for sizeof(int) bytes. No padding or uninitialized stack bytes are copied.\n2. CMSG handling in recvmsg: put_cmsg() is called with \u0026cb-\u003eendpoint_id (a u32), which is unconditionally initialized in all packet ingress paths (qrtr_endpoint_post and qrtr_local_enqueue).\n3. CMSG parsing in qrtr_msg_get_endpoint: the output parameter is initialized to 0 or the parsed cmsg value on success, preventing uninitialized reads in qrtr_sendmsg and qrtr_ns_worker.\n4. Heap and socket allocations: all newly introduced structures (struct qrtr_ns_endpoint, struct qrtr_server, struct qrtr_node) use kzalloc/kzalloc_obj, and QRTR sockets are allocated via sk_alloc (which zero-initializes socket memory).\n\nKASAN vs KMSAN applicability:\nThe changes primarily involve radix tree restructuring, cyclic xarray ID management, bit manipulation for 32/64-bit index keys, and socket endpoint matching. Risks associated with these changes include use-after-free, out-of-bounds radix tree access, reference counting bugs, or lock ordering, all of which are caught by standard KASAN and LOCKDEP instrumentation. Because no uninitialized memory usage or kernel-to-user information leaks are introduced, a dedicated KMSAN fuzzing session is not warranted.",
"NeedsKMSAN": false
} |
| Total Calls | Total Tokens | Avg Tokens | Total Duration (Seconds) | Avg Duration (Seconds) |
|---|
| Total Calls | Total Duration (Seconds) | Avg Duration (Seconds) |
|---|