/src/PcapPlusPlus/Packet++/header/VxlanLayer.h
Line | Count | Source (jump to first uncovered line) |
1 | | #pragma once |
2 | | |
3 | | #include "Layer.h" |
4 | | |
5 | | /// @file |
6 | | |
7 | | /// @namespace pcpp |
8 | | /// @brief The main namespace for the PcapPlusPlus lib |
9 | | namespace pcpp |
10 | | { |
11 | | /// @struct vxlan_header |
12 | | /// Represents a VXLAN protocol header |
13 | | #pragma pack(push, 1) |
14 | | struct vxlan_header |
15 | | { |
16 | | #if (BYTE_ORDER == LITTLE_ENDIAN) |
17 | | /// Reserved bits |
18 | | uint16_t reserved6_8 : 3; |
19 | | /// VNI present flag |
20 | | uint16_t vniPresentFlag : 1; |
21 | | /// Reserved bits |
22 | | uint16_t reserved2_4 : 3; |
23 | | /// GBP flag |
24 | | uint16_t gbpFlag : 1; |
25 | | /// Reserved bits |
26 | | uint16_t reserved14_16 : 3; |
27 | | /// Policy applied flag |
28 | | uint16_t policyAppliedFlag : 1; |
29 | | /// Reserved bits |
30 | | uint16_t reserved11_12 : 2; |
31 | | /// Don't learn flag |
32 | | uint16_t dontLearnFlag : 1; |
33 | | /// Reserved bits |
34 | | uint16_t reserved9 : 1; |
35 | | #else |
36 | | /// Reserved bits |
37 | | uint16_t reserved9 : 1; |
38 | | /// Don't learn flag |
39 | | uint16_t dontLearnFlag : 1; |
40 | | /// Reserved bits |
41 | | uint16_t reserved11_12 : 2; |
42 | | /// Policy applied flag |
43 | | uint16_t policyAppliedFlag : 1; |
44 | | /// Reserved bits |
45 | | uint16_t reserved14_16 : 3; |
46 | | /// GBP flag |
47 | | uint16_t gbpFlag : 1; |
48 | | /// Reserved bits |
49 | | uint16_t reserved2_4 : 3; |
50 | | /// VNI present flag |
51 | | uint16_t vniPresentFlag : 1; |
52 | | /// Reserved bits |
53 | | uint16_t reserved6_8 : 3; |
54 | | #endif |
55 | | |
56 | | /// Group Policy ID |
57 | | uint16_t groupPolicyID; |
58 | | |
59 | | /// VXLAN Network ID (VNI) |
60 | | uint32_t vni : 24; |
61 | | /// Reserved bits |
62 | | uint32_t pad : 8; |
63 | | }; |
64 | | #pragma pack(pop) |
65 | | static_assert(sizeof(vxlan_header) == 8, "vxlan_header size is not 8 bytes"); |
66 | | |
67 | | /// @class VxlanLayer |
68 | | /// Represents a VXLAN (Virtual eXtensible Local Area Network) protocol layer |
69 | | class VxlanLayer : public Layer |
70 | | { |
71 | | public: |
72 | | /// A constructor that creates the layer from an existing packet raw data |
73 | | /// @param[in] data A pointer to the raw data |
74 | | /// @param[in] dataLen Size of the data in bytes |
75 | | /// @param[in] prevLayer A pointer to the previous layer |
76 | | /// @param[in] packet A pointer to the Packet instance where layer will be stored in |
77 | | VxlanLayer(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet) |
78 | 0 | : Layer(data, dataLen, prevLayer, packet, VXLAN) |
79 | 0 | {} |
80 | | |
81 | | /// A constructor that creates a new VXLAN header and allocates the data. Note: the VNI present flag is set |
82 | | /// automatically |
83 | | /// @param[in] vni VNI (VXLAN Network ID) to set. Optional parameter (default is 0) |
84 | | /// @param[in] groupPolicyID Group Policy ID to set. Optional parameter (default is 0) |
85 | | /// @param[in] setGbpFlag Set GBP flag. Optional parameter (default is false) |
86 | | /// @param[in] setPolicyAppliedFlag Set Policy Applied flag. Optional parameter (default is false) |
87 | | /// @param[in] setDontLearnFlag Set Don't Learn flag. Optional parameter (default is false) |
88 | | explicit VxlanLayer(uint32_t vni = 0, uint16_t groupPolicyID = 0, bool setGbpFlag = false, |
89 | | bool setPolicyAppliedFlag = false, bool setDontLearnFlag = false); |
90 | | |
91 | | ~VxlanLayer() override = default; |
92 | | |
93 | | /// Get a pointer to the VXLAN header. Notice this points directly to the data, so every change will change the |
94 | | /// actual packet data |
95 | | /// @return A pointer to the vxlan_header |
96 | | vxlan_header* getVxlanHeader() const |
97 | 0 | { |
98 | 0 | return reinterpret_cast<vxlan_header*>(m_Data); |
99 | 0 | } |
100 | | |
101 | | /// @return The VXLAN Network ID (VNI) value |
102 | | uint32_t getVNI() const; |
103 | | |
104 | | /// Set VXLAN Network ID (VNI) value |
105 | | /// @param[in] vni VNI value to set |
106 | | void setVNI(uint32_t vni); |
107 | | |
108 | | /// A static method that checks whether the port is considered as VxLAN |
109 | | /// @param[in] port The port number to be checked |
110 | | static bool isVxlanPort(uint16_t port) |
111 | 96.2k | { |
112 | 96.2k | return port == 4789; |
113 | 96.2k | } |
114 | | |
115 | | // implement abstract methods |
116 | | |
117 | | /// Next layer for VXLAN is always Ethernet |
118 | | void parseNextLayer() override; |
119 | | |
120 | | /// @return Size of vxlan_header |
121 | | size_t getHeaderLen() const override |
122 | 0 | { |
123 | 0 | return sizeof(vxlan_header); |
124 | 0 | } |
125 | | |
126 | | /// Does nothing for this layer |
127 | | void computeCalculateFields() override |
128 | 0 | {} |
129 | | |
130 | | std::string toString() const override; |
131 | | |
132 | | OsiModelLayer getOsiModelLayer() const override |
133 | 0 | { |
134 | 0 | return OsiModelDataLinkLayer; |
135 | 0 | } |
136 | | }; |
137 | | } // namespace pcpp |