Skip to content

Commit 1314ef5

Browse files
fomichevborkmann
authored andcommitted
bpf: export bpf_sock for BPF_PROG_TYPE_SOCK_OPS prog type
And let it use bpf_sk_storage_{get,delete} helpers to access socket storage. Kernel context (struct bpf_sock_ops_kern) already has sk member, so I just expose it to the BPF hooks. I use PTR_TO_SOCKET_OR_NULL and return NULL in !is_fullsock case. I also export bpf_tcp_sock to make it possible to access tcp socket stats. Cc: Martin Lau <kafai@fb.com> Signed-off-by: Stanislav Fomichev <sdf@google.com> Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
1 parent fb85c4a commit 1314ef5

2 files changed

Lines changed: 27 additions & 0 deletions

File tree

‎include/uapi/linux/bpf.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3299,6 +3299,7 @@ struct bpf_sock_ops {
32993299
__u32 sk_txhash;
33003300
__u64 bytes_received;
33013301
__u64 bytes_acked;
3302+
__bpf_md_ptr(struct bpf_sock *, sk);
33023303
};
33033304

33043305
/* Definitions for bpf_sock_ops_cb_flags */

‎net/core/filter.c‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6147,6 +6147,14 @@ sock_ops_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
61476147
return &bpf_get_local_storage_proto;
61486148
case BPF_FUNC_perf_event_output:
61496149
return &bpf_sockopt_event_output_proto;
6150+
case BPF_FUNC_sk_storage_get:
6151+
return &bpf_sk_storage_get_proto;
6152+
case BPF_FUNC_sk_storage_delete:
6153+
return &bpf_sk_storage_delete_proto;
6154+
#ifdef CONFIG_INET
6155+
case BPF_FUNC_tcp_sock:
6156+
return &bpf_tcp_sock_proto;
6157+
#endif /* CONFIG_INET */
61506158
default:
61516159
return bpf_base_func_proto(func_id);
61526160
}
@@ -6882,6 +6890,11 @@ static bool sock_ops_is_valid_access(int off, int size,
68826890
if (size != sizeof(__u64))
68836891
return false;
68846892
break;
6893+
case offsetof(struct bpf_sock_ops, sk):
6894+
if (size != sizeof(__u64))
6895+
return false;
6896+
info->reg_type = PTR_TO_SOCKET_OR_NULL;
6897+
break;
68856898
default:
68866899
if (size != size_default)
68876900
return false;
@@ -8053,6 +8066,19 @@ static u32 sock_ops_convert_ctx_access(enum bpf_access_type type,
80538066
SOCK_OPS_GET_OR_SET_FIELD(sk_txhash, sk_txhash,
80548067
struct sock, type);
80558068
break;
8069+
case offsetof(struct bpf_sock_ops, sk):
8070+
*insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
8071+
struct bpf_sock_ops_kern,
8072+
is_fullsock),
8073+
si->dst_reg, si->src_reg,
8074+
offsetof(struct bpf_sock_ops_kern,
8075+
is_fullsock));
8076+
*insn++ = BPF_JMP_IMM(BPF_JEQ, si->dst_reg, 0, 1);
8077+
*insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF(
8078+
struct bpf_sock_ops_kern, sk),
8079+
si->dst_reg, si->src_reg,
8080+
offsetof(struct bpf_sock_ops_kern, sk));
8081+
break;
80568082
}
80578083
return insn - insn_buf;
80588084
}

0 commit comments

Comments
 (0)