1 // SPDX-License-Identifier: GPL-2.0 2 #include <stddef.h> 3 #include <string.h> 4 #include <netinet/in.h> 5 #include <linux/bpf.h> 6 #include <linux/if_ether.h> 7 #include <linux/if_packet.h> 8 #include <linux/ip.h> 9 #include <linux/ipv6.h> 10 #include <linux/types.h> 11 #include <linux/socket.h> 12 #include <linux/tcp.h> 13 #include <bpf/bpf_helpers.h> 14 #include <bpf/bpf_endian.h> 15 #include "bpf_tcp_helpers.h" 16 #include "test_tcpbpf.h" 17 18 struct tcpbpf_globals global = {}; 19 int _version SEC("version") = 1; 20 21 /** 22 * SOL_TCP is defined in <netinet/tcp.h> while 23 * TCP_SAVED_SYN is defined in already included <linux/tcp.h> 24 */ 25 #ifndef SOL_TCP 26 #define SOL_TCP 6 27 #endif 28 29 static __always_inline int get_tp_window_clamp(struct bpf_sock_ops *skops) 30 { 31 struct bpf_sock *sk; 32 struct tcp_sock *tp; 33 34 sk = skops->sk; 35 if (!sk) 36 return -1; 37 tp = bpf_skc_to_tcp_sock(sk); 38 if (!tp) 39 return -1; 40 return tp->window_clamp; 41 } 42 43 SEC("sockops") 44 int bpf_testcb(struct bpf_sock_ops *skops) 45 { 46 char header[sizeof(struct ipv6hdr) + sizeof(struct tcphdr)]; 47 struct bpf_sock_ops *reuse = skops; 48 struct tcphdr *thdr; 49 int window_clamp = 9216; 50 int good_call_rv = 0; 51 int bad_call_rv = 0; 52 int save_syn = 1; 53 int rv = -1; 54 int v = 0; 55 int op; 56 57 /* Test reading fields in bpf_sock_ops using single register */ 58 asm volatile ( 59 "%[reuse] = *(u32 *)(%[reuse] +96)" 60 : [reuse] "+r"(reuse) 61 :); 62 63 asm volatile ( 64 "%[op] = *(u32 *)(%[skops] +96)" 65 : [op] "+r"(op) 66 : [skops] "r"(skops) 67 :); 68 69 asm volatile ( 70 "r9 = %[skops];\n" 71 "r8 = *(u32 *)(r9 +164);\n" 72 "*(u32 *)(r9 +164) = r8;\n" 73 :: [skops] "r"(skops) 74 : "r9", "r8"); 75 76 asm volatile ( 77 "r1 = %[skops];\n" 78 "r1 = *(u64 *)(r1 +184);\n" 79 "if r1 == 0 goto +1;\n" 80 "r1 = *(u32 *)(r1 +4);\n" 81 :: [skops] "r"(skops):"r1"); 82 83 asm volatile ( 84 "r9 = %[skops];\n" 85 "r9 = *(u64 *)(r9 +184);\n" 86 "if r9 == 0 goto +1;\n" 87 "r9 = *(u32 *)(r9 +4);\n" 88 :: [skops] "r"(skops):"r9"); 89 90 asm volatile ( 91 "r1 = %[skops];\n" 92 "r2 = *(u64 *)(r1 +184);\n" 93 "if r2 == 0 goto +1;\n" 94 "r2 = *(u32 *)(r2 +4);\n" 95 :: [skops] "r"(skops):"r1", "r2"); 96 97 op = (int) skops->op; 98 99 global.event_map |= (1 << op); 100 101 switch (op) { 102 case BPF_SOCK_OPS_TCP_CONNECT_CB: 103 rv = bpf_setsockopt(skops, SOL_TCP, TCP_WINDOW_CLAMP, 104 &window_clamp, sizeof(window_clamp)); 105 global.window_clamp_client = get_tp_window_clamp(skops); 106 break; 107 case BPF_SOCK_OPS_ACTIVE_ESTABLISHED_CB: 108 /* Test failure to set largest cb flag (assumes not defined) */ 109 global.bad_cb_test_rv = bpf_sock_ops_cb_flags_set(skops, 0x80); 110 /* Set callback */ 111 global.good_cb_test_rv = bpf_sock_ops_cb_flags_set(skops, 112 BPF_SOCK_OPS_STATE_CB_FLAG); 113 break; 114 case BPF_SOCK_OPS_PASSIVE_ESTABLISHED_CB: 115 skops->sk_txhash = 0x12345f; 116 v = 0xff; 117 rv = bpf_setsockopt(skops, SOL_IPV6, IPV6_TCLASS, &v, 118 sizeof(v)); 119 if (skops->family == AF_INET6) { 120 v = bpf_getsockopt(skops, IPPROTO_TCP, TCP_SAVED_SYN, 121 header, (sizeof(struct ipv6hdr) + 122 sizeof(struct tcphdr))); 123 if (!v) { 124 int offset = sizeof(struct ipv6hdr); 125 126 thdr = (struct tcphdr *)(header + offset); 127 v = thdr->syn; 128 129 global.tcp_saved_syn = v; 130 } 131 } 132 rv = bpf_setsockopt(skops, SOL_TCP, TCP_WINDOW_CLAMP, 133 &window_clamp, sizeof(window_clamp)); 134 135 global.window_clamp_server = get_tp_window_clamp(skops); 136 break; 137 case BPF_SOCK_OPS_RTO_CB: 138 break; 139 case BPF_SOCK_OPS_RETRANS_CB: 140 break; 141 case BPF_SOCK_OPS_STATE_CB: 142 if (skops->args[1] == BPF_TCP_CLOSE) { 143 if (skops->args[0] == BPF_TCP_LISTEN) { 144 global.num_listen++; 145 } else { 146 global.total_retrans = skops->total_retrans; 147 global.data_segs_in = skops->data_segs_in; 148 global.data_segs_out = skops->data_segs_out; 149 global.bytes_received = skops->bytes_received; 150 global.bytes_acked = skops->bytes_acked; 151 } 152 global.num_close_events++; 153 } 154 break; 155 case BPF_SOCK_OPS_TCP_LISTEN_CB: 156 bpf_sock_ops_cb_flags_set(skops, BPF_SOCK_OPS_STATE_CB_FLAG); 157 v = bpf_setsockopt(skops, IPPROTO_TCP, TCP_SAVE_SYN, 158 &save_syn, sizeof(save_syn)); 159 /* Update global map w/ result of setsock opt */ 160 global.tcp_save_syn = v; 161 break; 162 default: 163 rv = -1; 164 } 165 skops->reply = rv; 166 return 1; 167 } 168 char _license[] SEC("license") = "GPL"; 169