]> git.baikalelectronics.ru Git - kernel.git/commitdiff
bpf: Add bpf_skc_to_{tcp, tcp_timewait, tcp_request}_sock() helpers
authorYonghong Song <yhs@fb.com>
Tue, 23 Jun 2020 23:08:11 +0000 (16:08 -0700)
committerAlexei Starovoitov <ast@kernel.org>
Thu, 25 Jun 2020 01:37:59 +0000 (18:37 -0700)
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
include/linux/bpf.h
include/uapi/linux/bpf.h
kernel/trace/bpf_trace.c
net/core/filter.c
scripts/bpf_helpers_doc.py
tools/include/uapi/linux/bpf.h

index c0e38ad078487689c02de7abd71f9860530c5df0..c23998cf669953cb53d4c8340ea0099f80338493 100644 (file)
@@ -1650,6 +1650,9 @@ extern const struct bpf_func_proto bpf_ringbuf_submit_proto;
 extern const struct bpf_func_proto bpf_ringbuf_discard_proto;
 extern const struct bpf_func_proto bpf_ringbuf_query_proto;
 extern const struct bpf_func_proto bpf_skc_to_tcp6_sock_proto;
+extern const struct bpf_func_proto bpf_skc_to_tcp_sock_proto;
+extern const struct bpf_func_proto bpf_skc_to_tcp_timewait_sock_proto;
+extern const struct bpf_func_proto bpf_skc_to_tcp_request_sock_proto;
 
 const struct bpf_func_proto *bpf_tracing_func_proto(
        enum bpf_func_id func_id, const struct bpf_prog *prog);
index e90ad07b291a34fa8ff8e72a1eafe26e4ee87ecc..b9412ab275f37b82ea1d6ff2465e77709389accd 100644 (file)
@@ -3261,6 +3261,24 @@ union bpf_attr {
  *             Dynamically cast a *sk* pointer to a *tcp6_sock* pointer.
  *     Return
  *             *sk* if casting is valid, or NULL otherwise.
+ *
+ * struct tcp_sock *bpf_skc_to_tcp_sock(void *sk)
+ *     Description
+ *             Dynamically cast a *sk* pointer to a *tcp_sock* pointer.
+ *     Return
+ *             *sk* if casting is valid, or NULL otherwise.
+ *
+ * struct tcp_timewait_sock *bpf_skc_to_tcp_timewait_sock(void *sk)
+ *     Description
+ *             Dynamically cast a *sk* pointer to a *tcp_timewait_sock* pointer.
+ *     Return
+ *             *sk* if casting is valid, or NULL otherwise.
+ *
+ * struct tcp_request_sock *bpf_skc_to_tcp_request_sock(void *sk)
+ *     Description
+ *             Dynamically cast a *sk* pointer to a *tcp_request_sock* pointer.
+ *     Return
+ *             *sk* if casting is valid, or NULL otherwise.
  */
 #define __BPF_FUNC_MAPPER(FN)          \
        FN(unspec),                     \
@@ -3399,7 +3417,10 @@ union bpf_attr {
        FN(ringbuf_discard),            \
        FN(ringbuf_query),              \
        FN(csum_level),                 \
-       FN(skc_to_tcp6_sock),
+       FN(skc_to_tcp6_sock),           \
+       FN(skc_to_tcp_sock),            \
+       FN(skc_to_tcp_timewait_sock),   \
+       FN(skc_to_tcp_request_sock),
 
 /* integer value in 'imm' field of BPF_CALL instruction selects which helper
  * function eBPF program intends to call
index 2a97a268f533a1a6cd44e48588cd138cae13a6ee..48d935b0d87c7439c5c9990013d7bef412bfe4f7 100644 (file)
@@ -1517,6 +1517,12 @@ tracing_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
                return &bpf_xdp_output_proto;
        case BPF_FUNC_skc_to_tcp6_sock:
                return &bpf_skc_to_tcp6_sock_proto;
+       case BPF_FUNC_skc_to_tcp_sock:
+               return &bpf_skc_to_tcp_sock_proto;
+       case BPF_FUNC_skc_to_tcp_timewait_sock:
+               return &bpf_skc_to_tcp_timewait_sock_proto;
+       case BPF_FUNC_skc_to_tcp_request_sock:
+               return &bpf_skc_to_tcp_request_sock_proto;
 #endif
        case BPF_FUNC_seq_printf:
                return prog->expected_attach_type == BPF_TRACE_ITER ?
index 176e27d75c5197ddc06f3e94fba404f0923ab3f2..0b4e5aed7e20755cdb0567315f87648de08dcb34 100644 (file)
@@ -74,6 +74,7 @@
 #include <net/lwtunnel.h>
 #include <net/ipv6_stubs.h>
 #include <net/bpf_sk_storage.h>
+#include <net/transp_v6.h>
 
 /**
  *     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 = {
        .check_btf_id           = check_arg_btf_id,
        .ret_btf_id             = &btf_sock_ids[BTF_SOCK_TYPE_TCP6],
 };
+
+BPF_CALL_1(bpf_skc_to_tcp_sock, struct sock *, sk)
+{
+       if (sk_fullsock(sk) && sk->sk_protocol == IPPROTO_TCP)
+               return (unsigned long)sk;
+
+       return (unsigned long)NULL;
+}
+
+const struct bpf_func_proto bpf_skc_to_tcp_sock_proto = {
+       .func                   = bpf_skc_to_tcp_sock,
+       .gpl_only               = false,
+       .ret_type               = RET_PTR_TO_BTF_ID_OR_NULL,
+       .arg1_type              = ARG_PTR_TO_BTF_ID,
+       .check_btf_id           = check_arg_btf_id,
+       .ret_btf_id             = &btf_sock_ids[BTF_SOCK_TYPE_TCP],
+};
+
+BPF_CALL_1(bpf_skc_to_tcp_timewait_sock, struct sock *, sk)
+{
+       if (sk->sk_prot == &tcp_prot && sk->sk_state == TCP_TIME_WAIT)
+               return (unsigned long)sk;
+
+#if IS_BUILTIN(CONFIG_IPV6)
+       if (sk->sk_prot == &tcpv6_prot && sk->sk_state == TCP_TIME_WAIT)
+               return (unsigned long)sk;
+#endif
+
+       return (unsigned long)NULL;
+}
+
+const struct bpf_func_proto bpf_skc_to_tcp_timewait_sock_proto = {
+       .func                   = bpf_skc_to_tcp_timewait_sock,
+       .gpl_only               = false,
+       .ret_type               = RET_PTR_TO_BTF_ID_OR_NULL,
+       .arg1_type              = ARG_PTR_TO_BTF_ID,
+       .check_btf_id           = check_arg_btf_id,
+       .ret_btf_id             = &btf_sock_ids[BTF_SOCK_TYPE_TCP_TW],
+};
+
+BPF_CALL_1(bpf_skc_to_tcp_request_sock, struct sock *, sk)
+{
+       if (sk->sk_prot == &tcp_prot  && sk->sk_state == TCP_NEW_SYN_RECV)
+               return (unsigned long)sk;
+
+#if IS_BUILTIN(CONFIG_IPV6)
+       if (sk->sk_prot == &tcpv6_prot && sk->sk_state == TCP_NEW_SYN_RECV)
+               return (unsigned long)sk;
+#endif
+
+       return (unsigned long)NULL;
+}
+
+const struct bpf_func_proto bpf_skc_to_tcp_request_sock_proto = {
+       .func                   = bpf_skc_to_tcp_request_sock,
+       .gpl_only               = false,
+       .ret_type               = RET_PTR_TO_BTF_ID_OR_NULL,
+       .arg1_type              = ARG_PTR_TO_BTF_ID,
+       .check_btf_id           = check_arg_btf_id,
+       .ret_btf_id             = &btf_sock_ids[BTF_SOCK_TYPE_TCP_REQ],
+};
index 6c2f64118651d9294217ec0f3aff83419e6a3a3c..d886657c6aaa4e438809d28e0b413760f8086fad 100755 (executable)
@@ -422,6 +422,9 @@ class PrinterHelpers(Printer):
             'struct tcphdr',
             'struct seq_file',
             'struct tcp6_sock',
+            'struct tcp_sock',
+            'struct tcp_timewait_sock',
+            'struct tcp_request_sock',
 
             'struct __sk_buff',
             'struct sk_msg_md',
@@ -460,6 +463,9 @@ class PrinterHelpers(Printer):
             'struct tcphdr',
             'struct seq_file',
             'struct tcp6_sock',
+            'struct tcp_sock',
+            'struct tcp_timewait_sock',
+            'struct tcp_request_sock',
     }
     mapped_types = {
             'u8': '__u8',
index e90ad07b291a34fa8ff8e72a1eafe26e4ee87ecc..b9412ab275f37b82ea1d6ff2465e77709389accd 100644 (file)
@@ -3261,6 +3261,24 @@ union bpf_attr {
  *             Dynamically cast a *sk* pointer to a *tcp6_sock* pointer.
  *     Return
  *             *sk* if casting is valid, or NULL otherwise.
+ *
+ * struct tcp_sock *bpf_skc_to_tcp_sock(void *sk)
+ *     Description
+ *             Dynamically cast a *sk* pointer to a *tcp_sock* pointer.
+ *     Return
+ *             *sk* if casting is valid, or NULL otherwise.
+ *
+ * struct tcp_timewait_sock *bpf_skc_to_tcp_timewait_sock(void *sk)
+ *     Description
+ *             Dynamically cast a *sk* pointer to a *tcp_timewait_sock* pointer.
+ *     Return
+ *             *sk* if casting is valid, or NULL otherwise.
+ *
+ * struct tcp_request_sock *bpf_skc_to_tcp_request_sock(void *sk)
+ *     Description
+ *             Dynamically cast a *sk* pointer to a *tcp_request_sock* pointer.
+ *     Return
+ *             *sk* if casting is valid, or NULL otherwise.
  */
 #define __BPF_FUNC_MAPPER(FN)          \
        FN(unspec),                     \
@@ -3399,7 +3417,10 @@ union bpf_attr {
        FN(ringbuf_discard),            \
        FN(ringbuf_query),              \
        FN(csum_level),                 \
-       FN(skc_to_tcp6_sock),
+       FN(skc_to_tcp6_sock),           \
+       FN(skc_to_tcp_sock),            \
+       FN(skc_to_tcp_timewait_sock),   \
+       FN(skc_to_tcp_request_sock),
 
 /* integer value in 'imm' field of BPF_CALL instruction selects which helper
  * function eBPF program intends to call