1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
| | /* SPDX-License-Identifier: (GPL-2.0-or-later OR BSD-2-Clause) */
/* SPDX-FileCopyrightText: 2021 The xdp-tutorial Authors */
// SPDX-FileCopyrightText: 2025 Yureka Lilian <yureka@cyberchaos.dev>
// SPDX-FileCopyrightText: 2025 Demi Marie Obenour <demiobenour@gmail.com>
/*
* This file contains a helper for validating a VLAN tag,
* as well as structs used by both BPF programs.
*/
#ifndef HELPERS_H
#define HELPERS_H
#include <linux/bpf.h>
#include <bpf/bpf_endian.h>
#include <bpf/bpf_helpers.h>
#define VLAN_ETHTYPE 0x8100 /* Ethertype for tagged frames */
struct ethhdr {
struct {
__u8 destination_mac[6];
__u8 source_mac[6];
} mac_addresses;
__be16 h_proto;
};
struct vlan_hdr {
__be16 h_vlan_TCI;
__be16 h_vlan_encapsulated_proto;
};
struct maybe_tagged_ethhdr {
union {
struct {
struct ethhdr eth;
struct vlan_hdr vlan;
} tagged;
struct {
struct vlan_hdr pad;
struct ethhdr eth;
} untagged;
};
};
// The router doesn't support the PCP and DEI bits
// and they are not part of the VLAN tag.
// Therefore, ensure they are unset.
// Also reject VLAN 0, which is reserved.
static __always_inline bool vlan_tag_is_valid(__u32 tag)
{
return tag >= 1 && tag <= 0x0FFF;
}
#endif /* HELPERS_H */
|