1 // SPDX-License-Identifier: GPL-2.0 2 /* Copyright (c) 2019 Facebook */ 3 #include <linux/stddef.h> 4 #include <linux/if_ether.h> 5 #include <linux/ipv6.h> 6 #include <linux/bpf.h> 7 #include <linux/tcp.h> 8 #include <bpf/bpf_helpers.h> 9 #include <bpf/bpf_endian.h> 10 #include <bpf/bpf_tracing.h> 11 12 struct sk_buff { 13 unsigned int len; 14 }; 15 16 __u64 test_result = 0; 17 SEC("fexit/test_pkt_access") 18 int BPF_PROG(test_main, struct sk_buff *skb, int ret) 19 { 20 int len; 21 22 __builtin_preserve_access_index(({ 23 len = skb->len; 24 })); 25 if (len != 74 || ret != 0) 26 return 0; 27 test_result = 1; 28 return 0; 29 } 30 31 __u64 test_result_subprog1 = 0; 32 SEC("fexit/test_pkt_access_subprog1") 33 int BPF_PROG(test_subprog1, struct sk_buff *skb, int ret) 34 { 35 int len; 36 37 __builtin_preserve_access_index(({ 38 len = skb->len; 39 })); 40 if (len != 74 || ret != 148) 41 return 0; 42 test_result_subprog1 = 1; 43 return 0; 44 } 45 46 /* Though test_pkt_access_subprog2() is defined in C as: 47 * static __attribute__ ((noinline)) 48 * int test_pkt_access_subprog2(int val, volatile struct __sk_buff *skb) 49 * { 50 * return skb->len * val; 51 * } 52 * llvm optimizations remove 'int val' argument and generate BPF assembly: 53 * r0 = *(u32 *)(r1 + 0) 54 * w0 <<= 1 55 * exit 56 * Before llvm23, in such case the verifier falls back to conservative and 57 * tracing program can access arguments and return value as u64 58 * instead of accurate types. With llvm23, the true signature 59 * int test_pkt_access_subprog2(volatile struct __sk_buff *skb) 60 * is available in btf. 61 */ 62 #if __clang_major__ >= 23 63 struct args_subprog2 { 64 __u64 args[1]; 65 __u64 ret; 66 }; 67 #else 68 struct args_subprog2 { 69 __u64 args[5]; 70 __u64 ret; 71 }; 72 #endif 73 __u64 test_result_subprog2 = 0; 74 SEC("fexit/test_pkt_access_subprog2") 75 int test_subprog2(struct args_subprog2 *ctx) 76 { 77 struct sk_buff *skb = (void *)ctx->args[0]; 78 __u64 ret; 79 int len; 80 81 bpf_probe_read_kernel(&len, sizeof(len), 82 __builtin_preserve_access_index(&skb->len)); 83 84 ret = ctx->ret; 85 /* bpf_prog_test_load() loads "test_pkt_access.bpf.o" with 86 * BPF_F_TEST_RND_HI32 which randomizes upper 32 bits after BPF_ALU32 87 * insns. Hence after 'w0 <<= 1' upper bits of $rax are random. That is 88 * expected and correct. Trim them. 89 */ 90 ret = (__u32) ret; 91 if (len != 74 || ret != 148) 92 return 0; 93 test_result_subprog2 = 1; 94 return 0; 95 } 96 97 __u64 test_result_subprog3 = 0; 98 SEC("fexit/test_pkt_access_subprog3") 99 int BPF_PROG(test_subprog3, int val, struct sk_buff *skb, int ret) 100 { 101 int len; 102 103 __builtin_preserve_access_index(({ 104 len = skb->len; 105 })); 106 if (len != 74 || ret != 74 * val || val != 3) 107 return 0; 108 test_result_subprog3 = 1; 109 return 0; 110 } 111 112 __u64 test_get_skb_len = 0; 113 SEC("freplace/get_skb_len") 114 int new_get_skb_len(struct __sk_buff *skb) 115 { 116 int len = skb->len; 117 118 if (len != 74) 119 return 0; 120 test_get_skb_len = 1; 121 return 74; /* original get_skb_len() returns skb->len */ 122 } 123 124 __u64 test_get_skb_ifindex = 0; 125 SEC("freplace/get_skb_ifindex") 126 int new_get_skb_ifindex(int val, struct __sk_buff *skb, int var) 127 { 128 void *data_end = (void *)(long)skb->data_end; 129 void *data = (void *)(long)skb->data; 130 struct ipv6hdr ip6, *ip6p; 131 int ifindex = skb->ifindex; 132 133 /* check that BPF extension can read packet via direct packet access */ 134 if (data + 14 + sizeof(ip6) > data_end) 135 return 0; 136 ip6p = data + 14; 137 138 if (ip6p->nexthdr != 6 || ip6p->payload_len != __bpf_constant_htons(123)) 139 return 0; 140 141 /* check that legacy packet access helper works too */ 142 if (bpf_skb_load_bytes(skb, 14, &ip6, sizeof(ip6)) < 0) 143 return 0; 144 ip6p = &ip6; 145 if (ip6p->nexthdr != 6 || ip6p->payload_len != __bpf_constant_htons(123)) 146 return 0; 147 148 if (ifindex != 1 || val != 3 || var != 1) 149 return 0; 150 test_get_skb_ifindex = 1; 151 return 3; /* original get_skb_ifindex() returns val * ifindex * var */ 152 } 153 154 volatile __u64 test_get_constant = 0; 155 SEC("freplace/get_constant") 156 int new_get_constant(long val) 157 { 158 if (val != 123) 159 return 0; 160 test_get_constant = 1; 161 return test_get_constant; /* original get_constant() returns val - 122 */ 162 } 163 164 __u64 test_pkt_write_access_subprog = 0; 165 SEC("freplace/test_pkt_write_access_subprog") 166 int new_test_pkt_write_access_subprog(struct __sk_buff *skb, __u32 off) 167 { 168 169 void *data = (void *)(long)skb->data; 170 void *data_end = (void *)(long)skb->data_end; 171 struct tcphdr *tcp; 172 173 if (off > sizeof(struct ethhdr) + sizeof(struct ipv6hdr)) 174 return -1; 175 176 tcp = data + off; 177 if (tcp + 1 > data_end) 178 return -1; 179 180 /* make modifications to the packet data */ 181 tcp->check++; 182 tcp->syn = 0; 183 184 test_pkt_write_access_subprog = 1; 185 return 0; 186 } 187 188 char _license[] SEC("license") = "GPL"; 189