Skip to content

Commit cff1c26

Browse files
committed
io_uring/net: allow filtering on IORING_OP_SOCKET data
Example population method for the BPF based opcode filtering. This exposes the socket family, type, and protocol to a registered BPF filter. This in turn enables the filter to make decisions based on what was passed in to the IORING_OP_SOCKET request type. Signed-off-by: Jens Axboe <axboe@kernel.dk>
1 parent d42eb05 commit cff1c26

4 files changed

Lines changed: 33 additions & 0 deletions

File tree

include/uapi/linux/io_uring/bpf_filter.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,13 @@ struct io_uring_bpf_ctx {
1616
__u8 sqe_flags;
1717
__u8 pdu_size; /* size of aux data for filter */
1818
__u8 pad[5];
19+
union {
20+
struct {
21+
__u32 family;
22+
__u32 type;
23+
__u32 protocol;
24+
} socket;
25+
};
1926
};
2027

2128
enum {

io_uring/bpf_filter.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,17 @@ static void io_uring_populate_bpf_ctx(struct io_uring_bpf_ctx *bctx,
3030
/* clear residual, anything from pdu_size and below */
3131
memset((void *) bctx + offsetof(struct io_uring_bpf_ctx, pdu_size), 0,
3232
sizeof(*bctx) - offsetof(struct io_uring_bpf_ctx, pdu_size));
33+
34+
/*
35+
* Opcodes can provide a handler fo populating more data into bctx,
36+
* for filters to use.
37+
*/
38+
switch (req->opcode) {
39+
case IORING_OP_SOCKET:
40+
bctx->pdu_size = sizeof(bctx->socket);
41+
io_socket_bpf_populate(bctx, req);
42+
break;
43+
}
3344
}
3445

3546
/*

io_uring/net.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1699,6 +1699,15 @@ int io_accept(struct io_kiocb *req, unsigned int issue_flags)
16991699
return IOU_COMPLETE;
17001700
}
17011701

1702+
void io_socket_bpf_populate(struct io_uring_bpf_ctx *bctx, struct io_kiocb *req)
1703+
{
1704+
struct io_socket *sock = io_kiocb_to_cmd(req, struct io_socket);
1705+
1706+
bctx->socket.family = sock->domain;
1707+
bctx->socket.type = sock->type;
1708+
bctx->socket.protocol = sock->protocol;
1709+
}
1710+
17021711
int io_socket_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
17031712
{
17041713
struct io_socket *sock = io_kiocb_to_cmd(req, struct io_socket);

io_uring/net.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include <linux/net.h>
44
#include <linux/uio.h>
55
#include <linux/io_uring_types.h>
6+
#include <uapi/linux/io_uring/bpf_filter.h>
67

78
struct io_async_msghdr {
89
#if defined(CONFIG_NET)
@@ -44,6 +45,7 @@ int io_accept(struct io_kiocb *req, unsigned int issue_flags);
4445

4546
int io_socket_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe);
4647
int io_socket(struct io_kiocb *req, unsigned int issue_flags);
48+
void io_socket_bpf_populate(struct io_uring_bpf_ctx *bctx, struct io_kiocb *req);
4749

4850
int io_connect_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe);
4951
int io_connect(struct io_kiocb *req, unsigned int issue_flags);
@@ -64,4 +66,8 @@ void io_netmsg_cache_free(const void *entry);
6466
static inline void io_netmsg_cache_free(const void *entry)
6567
{
6668
}
69+
static inline void io_socket_bpf_populate(struct io_uring_bpf_ctx *bctx,
70+
struct io_kiocb *req)
71+
{
72+
}
6773
#endif

0 commit comments

Comments
 (0)