Skip to content

Commit 478cfbd

Browse files
yonghong-songAlexei Starovoitov
authored andcommitted
bpf: Add bpf_skc_to_{tcp, tcp_timewait, tcp_request}_sock() helpers
Three more helpers are added to cast a sock_common pointer to an tcp_sock, tcp_timewait_sock or a tcp_request_sock for tracing programs. Signed-off-by: Yonghong Song <yhs@fb.com> Signed-off-by: Alexei Starovoitov <ast@kernel.org> Acked-by: Martin KaFai Lau <kafai@fb.com> Link: https://lore.kernel.org/bpf/20200623230811.3988277-1-yhs@fb.com
1 parent af7ec13 commit 478cfbd

File tree

6 files changed

+121
-2
lines changed

6 files changed

+121
-2
lines changed

include/linux/bpf.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1650,6 +1650,9 @@ extern const struct bpf_func_proto bpf_ringbuf_submit_proto;
16501650
extern const struct bpf_func_proto bpf_ringbuf_discard_proto;
16511651
extern const struct bpf_func_proto bpf_ringbuf_query_proto;
16521652
extern const struct bpf_func_proto bpf_skc_to_tcp6_sock_proto;
1653+
extern const struct bpf_func_proto bpf_skc_to_tcp_sock_proto;
1654+
extern const struct bpf_func_proto bpf_skc_to_tcp_timewait_sock_proto;
1655+
extern const struct bpf_func_proto bpf_skc_to_tcp_request_sock_proto;
16531656

16541657
const struct bpf_func_proto *bpf_tracing_func_proto(
16551658
enum bpf_func_id func_id, const struct bpf_prog *prog);

include/uapi/linux/bpf.h

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3261,6 +3261,24 @@ union bpf_attr {
32613261
* Dynamically cast a *sk* pointer to a *tcp6_sock* pointer.
32623262
* Return
32633263
* *sk* if casting is valid, or NULL otherwise.
3264+
*
3265+
* struct tcp_sock *bpf_skc_to_tcp_sock(void *sk)
3266+
* Description
3267+
* Dynamically cast a *sk* pointer to a *tcp_sock* pointer.
3268+
* Return
3269+
* *sk* if casting is valid, or NULL otherwise.
3270+
*
3271+
* struct tcp_timewait_sock *bpf_skc_to_tcp_timewait_sock(void *sk)
3272+
* Description
3273+
* Dynamically cast a *sk* pointer to a *tcp_timewait_sock* pointer.
3274+
* Return
3275+
* *sk* if casting is valid, or NULL otherwise.
3276+
*
3277+
* struct tcp_request_sock *bpf_skc_to_tcp_request_sock(void *sk)
3278+
* Description
3279+
* Dynamically cast a *sk* pointer to a *tcp_request_sock* pointer.
3280+
* Return
3281+
* *sk* if casting is valid, or NULL otherwise.
32643282
*/
32653283
#define __BPF_FUNC_MAPPER(FN) \
32663284
FN(unspec), \
@@ -3399,7 +3417,10 @@ union bpf_attr {
33993417
FN(ringbuf_discard), \
34003418
FN(ringbuf_query), \
34013419
FN(csum_level), \
3402-
FN(skc_to_tcp6_sock),
3420+
FN(skc_to_tcp6_sock), \
3421+
FN(skc_to_tcp_sock), \
3422+
FN(skc_to_tcp_timewait_sock), \
3423+
FN(skc_to_tcp_request_sock),
34033424

34043425
/* integer value in 'imm' field of BPF_CALL instruction selects which helper
34053426
* function eBPF program intends to call

kernel/trace/bpf_trace.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1517,6 +1517,12 @@ tracing_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
15171517
return &bpf_xdp_output_proto;
15181518
case BPF_FUNC_skc_to_tcp6_sock:
15191519
return &bpf_skc_to_tcp6_sock_proto;
1520+
case BPF_FUNC_skc_to_tcp_sock:
1521+
return &bpf_skc_to_tcp_sock_proto;
1522+
case BPF_FUNC_skc_to_tcp_timewait_sock:
1523+
return &bpf_skc_to_tcp_timewait_sock_proto;
1524+
case BPF_FUNC_skc_to_tcp_request_sock:
1525+
return &bpf_skc_to_tcp_request_sock_proto;
15201526
#endif
15211527
case BPF_FUNC_seq_printf:
15221528
return prog->expected_attach_type == BPF_TRACE_ITER ?

net/core/filter.c

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@
7474
#include <net/lwtunnel.h>
7575
#include <net/ipv6_stubs.h>
7676
#include <net/bpf_sk_storage.h>
77+
#include <net/transp_v6.h>
7778

7879
/**
7980
* sk_filter_trim_cap - run a packet through a socket filter
@@ -9307,3 +9308,64 @@ const struct bpf_func_proto bpf_skc_to_tcp6_sock_proto = {
93079308
.check_btf_id = check_arg_btf_id,
93089309
.ret_btf_id = &btf_sock_ids[BTF_SOCK_TYPE_TCP6],
93099310
};
9311+
9312+
BPF_CALL_1(bpf_skc_to_tcp_sock, struct sock *, sk)
9313+
{
9314+
if (sk_fullsock(sk) && sk->sk_protocol == IPPROTO_TCP)
9315+
return (unsigned long)sk;
9316+
9317+
return (unsigned long)NULL;
9318+
}
9319+
9320+
const struct bpf_func_proto bpf_skc_to_tcp_sock_proto = {
9321+
.func = bpf_skc_to_tcp_sock,
9322+
.gpl_only = false,
9323+
.ret_type = RET_PTR_TO_BTF_ID_OR_NULL,
9324+
.arg1_type = ARG_PTR_TO_BTF_ID,
9325+
.check_btf_id = check_arg_btf_id,
9326+
.ret_btf_id = &btf_sock_ids[BTF_SOCK_TYPE_TCP],
9327+
};
9328+
9329+
BPF_CALL_1(bpf_skc_to_tcp_timewait_sock, struct sock *, sk)
9330+
{
9331+
if (sk->sk_prot == &tcp_prot && sk->sk_state == TCP_TIME_WAIT)
9332+
return (unsigned long)sk;
9333+
9334+
#if IS_BUILTIN(CONFIG_IPV6)
9335+
if (sk->sk_prot == &tcpv6_prot && sk->sk_state == TCP_TIME_WAIT)
9336+
return (unsigned long)sk;
9337+
#endif
9338+
9339+
return (unsigned long)NULL;
9340+
}
9341+
9342+
const struct bpf_func_proto bpf_skc_to_tcp_timewait_sock_proto = {
9343+
.func = bpf_skc_to_tcp_timewait_sock,
9344+
.gpl_only = false,
9345+
.ret_type = RET_PTR_TO_BTF_ID_OR_NULL,
9346+
.arg1_type = ARG_PTR_TO_BTF_ID,
9347+
.check_btf_id = check_arg_btf_id,
9348+
.ret_btf_id = &btf_sock_ids[BTF_SOCK_TYPE_TCP_TW],
9349+
};
9350+
9351+
BPF_CALL_1(bpf_skc_to_tcp_request_sock, struct sock *, sk)
9352+
{
9353+
if (sk->sk_prot == &tcp_prot && sk->sk_state == TCP_NEW_SYN_RECV)
9354+
return (unsigned long)sk;
9355+
9356+
#if IS_BUILTIN(CONFIG_IPV6)
9357+
if (sk->sk_prot == &tcpv6_prot && sk->sk_state == TCP_NEW_SYN_RECV)
9358+
return (unsigned long)sk;
9359+
#endif
9360+
9361+
return (unsigned long)NULL;
9362+
}
9363+
9364+
const struct bpf_func_proto bpf_skc_to_tcp_request_sock_proto = {
9365+
.func = bpf_skc_to_tcp_request_sock,
9366+
.gpl_only = false,
9367+
.ret_type = RET_PTR_TO_BTF_ID_OR_NULL,
9368+
.arg1_type = ARG_PTR_TO_BTF_ID,
9369+
.check_btf_id = check_arg_btf_id,
9370+
.ret_btf_id = &btf_sock_ids[BTF_SOCK_TYPE_TCP_REQ],
9371+
};

scripts/bpf_helpers_doc.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -422,6 +422,9 @@ class PrinterHelpers(Printer):
422422
'struct tcphdr',
423423
'struct seq_file',
424424
'struct tcp6_sock',
425+
'struct tcp_sock',
426+
'struct tcp_timewait_sock',
427+
'struct tcp_request_sock',
425428

426429
'struct __sk_buff',
427430
'struct sk_msg_md',
@@ -460,6 +463,9 @@ class PrinterHelpers(Printer):
460463
'struct tcphdr',
461464
'struct seq_file',
462465
'struct tcp6_sock',
466+
'struct tcp_sock',
467+
'struct tcp_timewait_sock',
468+
'struct tcp_request_sock',
463469
}
464470
mapped_types = {
465471
'u8': '__u8',

tools/include/uapi/linux/bpf.h

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3261,6 +3261,24 @@ union bpf_attr {
32613261
* Dynamically cast a *sk* pointer to a *tcp6_sock* pointer.
32623262
* Return
32633263
* *sk* if casting is valid, or NULL otherwise.
3264+
*
3265+
* struct tcp_sock *bpf_skc_to_tcp_sock(void *sk)
3266+
* Description
3267+
* Dynamically cast a *sk* pointer to a *tcp_sock* pointer.
3268+
* Return
3269+
* *sk* if casting is valid, or NULL otherwise.
3270+
*
3271+
* struct tcp_timewait_sock *bpf_skc_to_tcp_timewait_sock(void *sk)
3272+
* Description
3273+
* Dynamically cast a *sk* pointer to a *tcp_timewait_sock* pointer.
3274+
* Return
3275+
* *sk* if casting is valid, or NULL otherwise.
3276+
*
3277+
* struct tcp_request_sock *bpf_skc_to_tcp_request_sock(void *sk)
3278+
* Description
3279+
* Dynamically cast a *sk* pointer to a *tcp_request_sock* pointer.
3280+
* Return
3281+
* *sk* if casting is valid, or NULL otherwise.
32643282
*/
32653283
#define __BPF_FUNC_MAPPER(FN) \
32663284
FN(unspec), \
@@ -3399,7 +3417,10 @@ union bpf_attr {
33993417
FN(ringbuf_discard), \
34003418
FN(ringbuf_query), \
34013419
FN(csum_level), \
3402-
FN(skc_to_tcp6_sock),
3420+
FN(skc_to_tcp6_sock), \
3421+
FN(skc_to_tcp_sock), \
3422+
FN(skc_to_tcp_timewait_sock), \
3423+
FN(skc_to_tcp_request_sock),
34033424

34043425
/* integer value in 'imm' field of BPF_CALL instruction selects which helper
34053426
* function eBPF program intends to call

0 commit comments

Comments
 (0)