1 // SPDX-License-Identifier: GPL-2.0-only
2 /* Copyright (c) 2017 Facebook
3 */
4 #include <stddef.h>
5 #include <string.h>
6 #include <linux/bpf.h>
7 #include <linux/pkt_cls.h>
8 #include <bpf/bpf_helpers.h>
9
10 int _version SEC("version") = 1;
11
12 #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
13 #define TEST_FIELD(TYPE, FIELD, MASK) \
14 { \
15 TYPE tmp = *(volatile TYPE *)&skb->FIELD; \
16 if (tmp != ((*(volatile __u32 *)&skb->FIELD) & MASK)) \
17 return TC_ACT_SHOT; \
18 }
19 #else
20 #define TEST_FIELD_OFFSET(a, b) ((sizeof(a) - sizeof(b)) / sizeof(b))
21 #define TEST_FIELD(TYPE, FIELD, MASK) \
22 { \
23 TYPE tmp = *((volatile TYPE *)&skb->FIELD + \
24 TEST_FIELD_OFFSET(skb->FIELD, TYPE)); \
25 if (tmp != ((*(volatile __u32 *)&skb->FIELD) & MASK)) \
26 return TC_ACT_SHOT; \
27 }
28 #endif
29
30 SEC("classifier/test_pkt_md_access")
test_pkt_md_access(struct __sk_buff * skb)31 int test_pkt_md_access(struct __sk_buff *skb)
32 {
33 TEST_FIELD(__u8, len, 0xFF);
34 TEST_FIELD(__u16, len, 0xFFFF);
35 TEST_FIELD(__u32, len, 0xFFFFFFFF);
36 TEST_FIELD(__u16, protocol, 0xFFFF);
37 TEST_FIELD(__u32, protocol, 0xFFFFFFFF);
38 TEST_FIELD(__u8, hash, 0xFF);
39 TEST_FIELD(__u16, hash, 0xFFFF);
40 TEST_FIELD(__u32, hash, 0xFFFFFFFF);
41
42 return TC_ACT_OK;
43 }
44