Skip to content

Commit 13d70f5

Browse files
jrfastabAlexei Starovoitov
authored andcommitted
bpf, sk_msg: Add get socket storage helpers
Add helpers to use local socket storage. Signed-off-by: John Fastabend <[email protected]> Signed-off-by: Daniel Borkmann <[email protected]> Acked-by: Yonghong Song <[email protected]> Link: https://lore.kernel.org/bpf/159033907577.12355.14740125020572756560.stgit@john-Precision-5820-Tower Signed-off-by: Alexei Starovoitov <[email protected]>
1 parent f470378 commit 13d70f5

File tree

3 files changed

+19
-0
lines changed

3 files changed

+19
-0
lines changed

include/uapi/linux/bpf.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3645,6 +3645,8 @@ struct sk_msg_md {
36453645
__u32 remote_port; /* Stored in network byte order */
36463646
__u32 local_port; /* stored in host byte order */
36473647
__u32 size; /* Total size of sk_msg */
3648+
3649+
__bpf_md_ptr(struct bpf_sock *, sk); /* current socket */
36483650
};
36493651

36503652
struct sk_reuseport_md {

net/core/filter.c

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6449,6 +6449,10 @@ sk_msg_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
64496449
return &bpf_get_current_uid_gid_proto;
64506450
case BPF_FUNC_get_current_pid_tgid:
64516451
return &bpf_get_current_pid_tgid_proto;
6452+
case BPF_FUNC_sk_storage_get:
6453+
return &bpf_sk_storage_get_proto;
6454+
case BPF_FUNC_sk_storage_delete:
6455+
return &bpf_sk_storage_delete_proto;
64526456
#ifdef CONFIG_CGROUPS
64536457
case BPF_FUNC_get_current_cgroup_id:
64546458
return &bpf_get_current_cgroup_id_proto;
@@ -7273,6 +7277,11 @@ static bool sk_msg_is_valid_access(int off, int size,
72737277
if (size != sizeof(__u64))
72747278
return false;
72757279
break;
7280+
case offsetof(struct sk_msg_md, sk):
7281+
if (size != sizeof(__u64))
7282+
return false;
7283+
info->reg_type = PTR_TO_SOCKET;
7284+
break;
72767285
case bpf_ctx_range(struct sk_msg_md, family):
72777286
case bpf_ctx_range(struct sk_msg_md, remote_ip4):
72787287
case bpf_ctx_range(struct sk_msg_md, local_ip4):
@@ -8609,6 +8618,12 @@ static u32 sk_msg_convert_ctx_access(enum bpf_access_type type,
86098618
si->dst_reg, si->src_reg,
86108619
offsetof(struct sk_msg_sg, size));
86118620
break;
8621+
8622+
case offsetof(struct sk_msg_md, sk):
8623+
*insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(struct sk_msg, sk),
8624+
si->dst_reg, si->src_reg,
8625+
offsetof(struct sk_msg, sk));
8626+
break;
86128627
}
86138628

86148629
return insn - insn_buf;

tools/include/uapi/linux/bpf.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3645,6 +3645,8 @@ struct sk_msg_md {
36453645
__u32 remote_port; /* Stored in network byte order */
36463646
__u32 local_port; /* stored in host byte order */
36473647
__u32 size; /* Total size of sk_msg */
3648+
3649+
__bpf_md_ptr(struct bpf_sock *, sk); /* current socket */
36483650
};
36493651

36503652
struct sk_reuseport_md {

0 commit comments

Comments
 (0)