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_helpers.h"
19 #include "bpf_endian.h"
23 #define bpf_printk(fmt, ...) \
25 char ____fmt[] = fmt; \
26 bpf_trace_printk(____fmt, sizeof(____fmt), \
31 int bpf_basertt(struct bpf_sock_ops
*skops
)
33 char header
[sizeof(struct ipv6hdr
)];
45 bpf_printk("BPF command: %d\n", op
);
48 case BPF_SOCK_OPS_TCP_LISTEN_CB
:
49 rv
= bpf_setsockopt(skops
, SOL_TCP
, TCP_SAVE_SYN
,
50 &save_syn
, sizeof(save_syn
));
52 case BPF_SOCK_OPS_PASSIVE_ESTABLISHED_CB
:
53 if (skops
->family
== AF_INET
)
54 hdr_size
= sizeof(struct iphdr
);
56 hdr_size
= sizeof(struct ipv6hdr
);
57 rv
= bpf_getsockopt(skops
, SOL_TCP
, TCP_SAVED_SYN
,
60 if (skops
->family
== AF_INET
) {
61 hdr
= (struct iphdr
*) header
;
64 bpf_setsockopt(skops
, SOL_IP
, IP_TOS
,
67 hdr6
= (struct ipv6hdr
*) header
;
68 tos
= ((hdr6
->priority
) << 4 |
69 (hdr6
->flow_lbl
[0]) >> 4);
71 bpf_setsockopt(skops
, SOL_IPV6
,
82 bpf_printk("Returning %d\n", rv
);
87 char _license
[] SEC("license") = "GPL";