1 // SPDX-License-Identifier: GPL-2.0
4 #include <bpf/bpf_helpers.h>
5 #include <bpf/bpf_endian.h>
7 #include "bpf_tracing_net.h"
11 int bpf_sock_destroy(struct sock_common *sk) __ksym;
14 __uint(type, BPF_MAP_TYPE_ARRAY);
15 __uint(max_entries, 1);
18 } tcp_conn_sockets SEC(".maps");
21 __uint(type, BPF_MAP_TYPE_ARRAY);
22 __uint(max_entries, 1);
25 } udp_conn_sockets SEC(".maps");
27 SEC("cgroup/connect6")
28 int sock_connect(struct bpf_sock_addr *ctx)
30 __u64 sock_cookie = 0;
34 if (ctx->family != AF_INET6 || ctx->user_family != AF_INET6)
37 sock_cookie = bpf_get_socket_cookie(ctx);
38 if (ctx->protocol == IPPROTO_TCP)
39 bpf_map_update_elem(&tcp_conn_sockets, &key, &sock_cookie, 0);
40 else if (ctx->protocol == IPPROTO_UDP)
41 bpf_map_update_elem(&udp_conn_sockets, &keyc, &sock_cookie, 0);
49 int iter_tcp6_client(struct bpf_iter__tcp *ctx)
51 struct sock_common *sk_common = ctx->sk_common;
52 __u64 sock_cookie = 0;
59 if (sk_common->skc_family != AF_INET6)
62 sock_cookie = bpf_get_socket_cookie(sk_common);
63 val = bpf_map_lookup_elem(&tcp_conn_sockets, &key);
66 /* Destroy connected client sockets. */
67 if (sock_cookie == *val)
68 bpf_sock_destroy(sk_common);
74 int iter_tcp6_server(struct bpf_iter__tcp *ctx)
76 struct sock_common *sk_common = ctx->sk_common;
77 const struct inet_connection_sock *icsk;
78 const struct inet_sock *inet;
79 struct tcp6_sock *tcp_sk;
85 if (sk_common->skc_family != AF_INET6)
88 tcp_sk = bpf_skc_to_tcp6_sock(sk_common);
92 icsk = &tcp_sk->tcp.inet_conn;
93 inet = &icsk->icsk_inet;
94 srcp = inet->inet_sport;
96 /* Destroy server sockets. */
97 if (srcp == serv_port)
98 bpf_sock_destroy(sk_common);
105 int iter_udp6_client(struct bpf_iter__udp *ctx)
107 struct udp_sock *udp_sk = ctx->udp_sk;
108 struct sock *sk = (struct sock *) udp_sk;
109 __u64 sock_cookie = 0, *val;
115 sock_cookie = bpf_get_socket_cookie(sk);
116 val = bpf_map_lookup_elem(&udp_conn_sockets, &key);
119 /* Destroy connected client sockets. */
120 if (sock_cookie == *val)
121 bpf_sock_destroy((struct sock_common *)sk);
127 int iter_udp6_server(struct bpf_iter__udp *ctx)
129 struct udp_sock *udp_sk = ctx->udp_sk;
130 struct sock *sk = (struct sock *) udp_sk;
131 struct inet_sock *inet;
137 inet = &udp_sk->inet;
138 srcp = inet->inet_sport;
139 if (srcp == serv_port)
140 bpf_sock_destroy((struct sock_common *)sk);
145 char _license[] SEC("license") = "GPL";