1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2018 Facebook
5 * BPF program to automatically reflect TOS option from received syn packet
7 * Use "bpftool cgroup attach $cg sock_ops $prog" to load this BPF program.
10 #include <uapi/linux/bpf.h>
11 #include <uapi/linux/tcp.h>
12 #include <uapi/linux/if_ether.h>
13 #include <uapi/linux/if_packet.h>
14 #include <uapi/linux/ip.h>
15 #include <uapi/linux/ipv6.h>
16 #include <uapi/linux/in.h>
17 #include <linux/socket.h>
18 #include <bpf/bpf_helpers.h>
19 #include <bpf/bpf_endian.h>
24 int bpf_basertt(struct bpf_sock_ops *skops)
26 char header[sizeof(struct ipv6hdr)];
38 bpf_printk("BPF command: %d\n", op);
41 case BPF_SOCK_OPS_TCP_LISTEN_CB:
42 rv = bpf_setsockopt(skops, SOL_TCP, TCP_SAVE_SYN,
43 &save_syn, sizeof(save_syn));
45 case BPF_SOCK_OPS_PASSIVE_ESTABLISHED_CB:
46 if (skops->family == AF_INET)
47 hdr_size = sizeof(struct iphdr);
49 hdr_size = sizeof(struct ipv6hdr);
50 rv = bpf_getsockopt(skops, SOL_TCP, TCP_SAVED_SYN,
53 if (skops->family == AF_INET) {
54 hdr = (struct iphdr *) header;
57 bpf_setsockopt(skops, SOL_IP, IP_TOS,
60 hdr6 = (struct ipv6hdr *) header;
61 tos = ((hdr6->priority) << 4 |
62 (hdr6->flow_lbl[0]) >> 4);
64 bpf_setsockopt(skops, SOL_IPV6,
75 bpf_printk("Returning %d\n", rv);
80 char _license[] SEC("license") = "GPL";