1 /* SPDX-License-Identifier: GPL-2.0 */
2 /* Copyright (c) 2017-2018 Covalent IO, Inc. http://covalent.io */
6 #include <linux/if_ether.h>
7 #include <linux/if_packet.h>
9 #include <linux/ipv6.h>
11 #include <linux/udp.h>
12 #include <linux/tcp.h>
13 #include <linux/pkt_cls.h>
14 #include <sys/socket.h>
15 #include <bpf/bpf_helpers.h>
16 #include <bpf/bpf_endian.h>
19 /* Sockmap sample program connects a client and a backend together
22 * client:X <---> frontend:80 client:X <---> backend:80
24 * For simplicity we hard code values here and bind 1:1. The hard
25 * coded values are part of the setup in sockmap.sh script that
26 * is associated with this BPF program.
28 * The bpf_printk is verbose and prints information as connections
29 * are established and verdicts are decided.
33 __uint(type, TEST_MAP_TYPE);
34 __uint(max_entries, 20);
35 __uint(key_size, sizeof(int));
36 __uint(value_size, sizeof(int));
37 } sock_map SEC(".maps");
40 __uint(type, TEST_MAP_TYPE);
41 __uint(max_entries, 20);
42 __uint(key_size, sizeof(int));
43 __uint(value_size, sizeof(int));
44 } sock_map_txmsg SEC(".maps");
47 __uint(type, TEST_MAP_TYPE);
48 __uint(max_entries, 20);
49 __uint(key_size, sizeof(int));
50 __uint(value_size, sizeof(int));
51 } sock_map_redir SEC(".maps");
54 __uint(type, BPF_MAP_TYPE_ARRAY);
55 __uint(max_entries, 1);
58 } sock_apply_bytes SEC(".maps");
61 __uint(type, BPF_MAP_TYPE_ARRAY);
62 __uint(max_entries, 1);
65 } sock_cork_bytes SEC(".maps");
68 __uint(type, BPF_MAP_TYPE_ARRAY);
69 __uint(max_entries, 6);
72 } sock_bytes SEC(".maps");
75 __uint(type, BPF_MAP_TYPE_ARRAY);
76 __uint(max_entries, 1);
79 } sock_redir_flags SEC(".maps");
82 __uint(type, BPF_MAP_TYPE_ARRAY);
83 __uint(max_entries, 3);
86 } sock_skb_opts SEC(".maps");
89 __uint(type, TEST_MAP_TYPE);
90 __uint(max_entries, 20);
91 __uint(key_size, sizeof(int));
92 __uint(value_size, sizeof(int));
93 } tls_sock_map SEC(".maps");
95 SEC("sk_skb/stream_parser")
96 int bpf_prog1(struct __sk_buff *skb)
100 f = bpf_map_lookup_elem(&sock_skb_opts, &two);
107 SEC("sk_skb/stream_verdict")
108 int bpf_prog2(struct __sk_buff *skb)
110 __u32 lport = skb->local_port;
111 __u32 rport = skb->remote_port;
112 int len, *f, ret, zero = 0;
121 len = (__u32)skb->data_end - (__u32)skb->data;
124 f = bpf_map_lookup_elem(&sock_skb_opts, &zero);
131 return bpf_sk_redirect_map(skb, &sock_map, ret, flags);
133 return bpf_sk_redirect_hash(skb, &sock_map, &ret, flags);
138 static inline void bpf_write_pass(struct __sk_buff *skb, int offset)
140 int err = bpf_skb_pull_data(skb, 6 + offset);
147 c = (char *)(long)skb->data;
148 data_end = (void *)(long)skb->data_end;
150 if (c + 5 + offset < data_end)
151 memcpy(c + offset, "PASS", 4);
154 SEC("sk_skb/stream_verdict")
155 int bpf_prog3(struct __sk_buff *skb)
157 int err, *f, ret = SK_PASS;
160 f = bpf_map_lookup_elem(&sock_skb_opts, &one);
167 err = bpf_skb_adjust_room(skb, -13, 0, 0);
170 err = bpf_skb_adjust_room(skb, 4, 0, 0);
173 bpf_write_pass(skb, 0);
175 return bpf_sk_redirect_map(skb, &tls_sock_map, ret, flags);
177 return bpf_sk_redirect_hash(skb, &tls_sock_map, &ret, flags);
180 err = bpf_skb_adjust_room(skb, 4, 0, 0);
183 bpf_write_pass(skb, 13);
188 int bpf_sockmap(struct bpf_sock_ops *skops)
193 op = (int) skops->op;
196 case BPF_SOCK_OPS_PASSIVE_ESTABLISHED_CB:
197 lport = skops->local_port;
198 rport = skops->remote_port;
200 if (lport == 10000) {
203 bpf_sock_map_update(skops, &sock_map, &ret,
206 bpf_sock_hash_update(skops, &sock_map, &ret,
211 case BPF_SOCK_OPS_ACTIVE_ESTABLISHED_CB:
212 lport = skops->local_port;
213 rport = skops->remote_port;
215 if (bpf_ntohl(rport) == 10001) {
218 bpf_sock_map_update(skops, &sock_map, &ret,
221 bpf_sock_hash_update(skops, &sock_map, &ret,
234 int bpf_prog4(struct sk_msg_md *msg)
236 int *bytes, zero = 0, one = 1, two = 2, three = 3, four = 4, five = 5;
237 int *start, *end, *start_push, *end_push, *start_pop, *pop, err = 0;
239 bytes = bpf_map_lookup_elem(&sock_apply_bytes, &zero);
241 bpf_msg_apply_bytes(msg, *bytes);
242 bytes = bpf_map_lookup_elem(&sock_cork_bytes, &zero);
244 bpf_msg_cork_bytes(msg, *bytes);
245 start = bpf_map_lookup_elem(&sock_bytes, &zero);
246 end = bpf_map_lookup_elem(&sock_bytes, &one);
248 bpf_msg_pull_data(msg, *start, *end, 0);
249 start_push = bpf_map_lookup_elem(&sock_bytes, &two);
250 end_push = bpf_map_lookup_elem(&sock_bytes, &three);
251 if (start_push && end_push) {
252 err = bpf_msg_push_data(msg, *start_push, *end_push, 0);
256 start_pop = bpf_map_lookup_elem(&sock_bytes, &four);
257 pop = bpf_map_lookup_elem(&sock_bytes, &five);
258 if (start_pop && pop)
259 bpf_msg_pop_data(msg, *start_pop, *pop, 0);
264 int bpf_prog6(struct sk_msg_md *msg)
266 int zero = 0, one = 1, two = 2, three = 3, four = 4, five = 5, key = 0;
267 int *bytes, *start, *end, *start_push, *end_push, *start_pop, *pop, *f;
271 bytes = bpf_map_lookup_elem(&sock_apply_bytes, &zero);
273 bpf_msg_apply_bytes(msg, *bytes);
274 bytes = bpf_map_lookup_elem(&sock_cork_bytes, &zero);
276 bpf_msg_cork_bytes(msg, *bytes);
278 start = bpf_map_lookup_elem(&sock_bytes, &zero);
279 end = bpf_map_lookup_elem(&sock_bytes, &one);
281 bpf_msg_pull_data(msg, *start, *end, 0);
283 start_push = bpf_map_lookup_elem(&sock_bytes, &two);
284 end_push = bpf_map_lookup_elem(&sock_bytes, &three);
285 if (start_push && end_push) {
286 err = bpf_msg_push_data(msg, *start_push, *end_push, 0);
291 start_pop = bpf_map_lookup_elem(&sock_bytes, &four);
292 pop = bpf_map_lookup_elem(&sock_bytes, &five);
293 if (start_pop && pop)
294 bpf_msg_pop_data(msg, *start_pop, *pop, 0);
296 f = bpf_map_lookup_elem(&sock_redir_flags, &zero);
302 return bpf_msg_redirect_map(msg, &sock_map_redir, key, flags);
304 return bpf_msg_redirect_hash(msg, &sock_map_redir, &key, flags);
309 int bpf_prog8(struct sk_msg_md *msg)
311 void *data_end = (void *)(long) msg->data_end;
312 void *data = (void *)(long) msg->data;
313 int ret = 0, *bytes, zero = 0;
315 bytes = bpf_map_lookup_elem(&sock_apply_bytes, &zero);
317 ret = bpf_msg_apply_bytes(msg, *bytes);
331 int bpf_prog9(struct sk_msg_md *msg)
333 void *data_end = (void *)(long) msg->data_end;
334 void *data = (void *)(long) msg->data;
335 int ret = 0, *bytes, zero = 0;
337 bytes = bpf_map_lookup_elem(&sock_cork_bytes, &zero);
339 if (((__u64)data_end - (__u64)data) >= *bytes)
341 ret = bpf_msg_cork_bytes(msg, *bytes);
349 int bpf_prog10(struct sk_msg_md *msg)
351 int *bytes, *start, *end, *start_push, *end_push, *start_pop, *pop;
352 int zero = 0, one = 1, two = 2, three = 3, four = 4, five = 5, err = 0;
354 bytes = bpf_map_lookup_elem(&sock_apply_bytes, &zero);
356 bpf_msg_apply_bytes(msg, *bytes);
357 bytes = bpf_map_lookup_elem(&sock_cork_bytes, &zero);
359 bpf_msg_cork_bytes(msg, *bytes);
360 start = bpf_map_lookup_elem(&sock_bytes, &zero);
361 end = bpf_map_lookup_elem(&sock_bytes, &one);
363 bpf_msg_pull_data(msg, *start, *end, 0);
364 start_push = bpf_map_lookup_elem(&sock_bytes, &two);
365 end_push = bpf_map_lookup_elem(&sock_bytes, &three);
366 if (start_push && end_push) {
367 err = bpf_msg_push_data(msg, *start_push, *end_push, 0);
371 start_pop = bpf_map_lookup_elem(&sock_bytes, &four);
372 pop = bpf_map_lookup_elem(&sock_bytes, &five);
373 if (start_pop && pop)
374 bpf_msg_pop_data(msg, *start_pop, *pop, 0);
378 char _license[] SEC("license") = "GPL";