@@ -48,6 +48,7 @@ struct qrtr_ctrl_pkt {
/* setsockopt / getsockopt */
#define QRTR_REPORT_ENDPOINT 1
+#define QRTR_BIND_ENDPOINT 2
/* CMSG */
#define QRTR_ENDPOINT 1
@@ -98,6 +98,7 @@ struct qrtr_sock {
struct sockaddr_qrtr us;
struct sockaddr_qrtr peer;
unsigned long flags;
+ u32 bound_endpoint;
};
static inline struct qrtr_sock *qrtr_sk(struct sock *sk)
@@ -587,9 +588,13 @@ int qrtr_endpoint_post(struct qrtr_endpoint *ep, const void *data, size_t len)
if (!ipc)
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) {
+ if (sock_queue_rcv_skb(&ipc->sk, skb)) {
+ qrtr_port_put(ipc);
+ goto err;
+ }
}
qrtr_port_put(ipc);
@@ -928,29 +933,41 @@ static int qrtr_local_enqueue(struct qrtr_node *node, struct sk_buff *skb,
{
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. */
@@ -1034,7 +1051,8 @@ static int qrtr_sendmsg(struct socket *sock, struct msghdr *msg, size_t len)
} else if (addr->sq_node == ipc->us.sq_node) {
enqueue_fn = qrtr_local_enqueue;
} else {
- endpoint_id = msg_endpoint_id;
+ endpoint_id = msg_endpoint_id ?
+ msg_endpoint_id : ipc->bound_endpoint;
node = qrtr_node_lookup(endpoint_id, addr->sq_node);
if (!node) {
@@ -1313,6 +1331,9 @@ static int qrtr_setsockopt(struct socket *sock, int level, int optname,
case QRTR_REPORT_ENDPOINT:
assign_bit(QRTR_F_REPORT_ENDPOINT, &ipc->flags, val);
break;
+ case QRTR_BIND_ENDPOINT:
+ ipc->bound_endpoint = val;
+ break;
default:
rc = -ENOPROTOOPT;
}
@@ -1346,6 +1367,9 @@ static int qrtr_getsockopt(struct socket *sock, int level, int optname,
case QRTR_REPORT_ENDPOINT:
val = test_bit(QRTR_F_REPORT_ENDPOINT, &ipc->flags);
break;
+ case QRTR_BIND_ENDPOINT:
+ val = ipc->bound_endpoint;
+ break;
default:
rc = -ENOPROTOOPT;
}