/src/PcapPlusPlus/Packet++/src/IPv6Extensions.cpp
Line | Count | Source |
1 | | #define LOG_MODULE PacketLogModuleIPv6ExtensionLayer |
2 | | |
3 | | #include "EndianPortable.h" |
4 | | #include "IPv6Extensions.h" |
5 | | #include "IPv6Layer.h" |
6 | | #include "IPv4Layer.h" |
7 | | #include "UdpLayer.h" |
8 | | #include "TcpLayer.h" |
9 | | |
10 | | namespace pcpp |
11 | | { |
12 | | |
13 | | // ============= |
14 | | // IPv6Extension |
15 | | // ============= |
16 | | |
17 | | IPv6Extension& IPv6Extension::operator=(const IPv6Extension& other) |
18 | 0 | { |
19 | | // notice this is not necessarily safe - it assumes the current extension has enough memory allocated to consume |
20 | | // the other extension. That's why the assignment operator isn't public (it's currently used only inside |
21 | | // IPv6Layer) |
22 | 0 | memcpy(getDataPtr(), other.getDataPtr(), other.getExtensionLen()); |
23 | 0 | m_NextHeader = nullptr; |
24 | 0 | m_ExtType = other.m_ExtType; |
25 | |
|
26 | 0 | return *this; |
27 | 0 | } |
28 | | |
29 | | uint8_t* IPv6Extension::getDataPtr() const |
30 | 73.4k | { |
31 | 73.4k | if (m_DataContainer != nullptr) |
32 | 73.4k | return m_DataContainer->getDataPtr(m_Offset); |
33 | | |
34 | 0 | return m_ShadowData; |
35 | 73.4k | } |
36 | | |
37 | | void IPv6Extension::initShadowPtr(size_t size) |
38 | 0 | { |
39 | 0 | m_ShadowData = new uint8_t[size]; |
40 | 0 | } |
41 | | |
42 | | IPv6Extension::~IPv6Extension() |
43 | 20.7k | { |
44 | 20.7k | if (m_ShadowData != nullptr) |
45 | 0 | delete[] m_ShadowData; |
46 | 20.7k | } |
47 | | |
48 | | // ======================= |
49 | | // IPv6FragmentationHeader |
50 | | // ======================= |
51 | | |
52 | | IPv6FragmentationHeader::IPv6FragmentationHeader(uint32_t fragId, uint16_t fragOffset, bool lastFragment) |
53 | 0 | { |
54 | 0 | initShadowPtr(sizeof(ipv6_frag_header)); |
55 | 0 | m_ExtType = IPv6Fragmentation; |
56 | 0 | memset(getDataPtr(), 0, sizeof(ipv6_frag_header)); |
57 | |
|
58 | 0 | ipv6_frag_header* fragHdr = getFragHeader(); |
59 | 0 | fragHdr->nextHeader = 0; |
60 | 0 | fragHdr->headerLen = 0; |
61 | 0 | fragHdr->id = htobe32(fragId); |
62 | |
|
63 | 0 | fragOffset /= 8; |
64 | 0 | fragOffset = htobe16(fragOffset << 3) & static_cast<uint16_t>(0xf8ff); |
65 | 0 | if (!lastFragment) |
66 | 0 | fragOffset = fragOffset | 0x0100; |
67 | |
|
68 | 0 | fragHdr->fragOffsetAndFlags = fragOffset; |
69 | 0 | } |
70 | | |
71 | | bool IPv6FragmentationHeader::isFirstFragment() const |
72 | 0 | { |
73 | 0 | return (getFragmentOffset() == 0); |
74 | 0 | } |
75 | | |
76 | | bool IPv6FragmentationHeader::isLastFragment() const |
77 | 0 | { |
78 | 0 | return (!isMoreFragments()); |
79 | 0 | } |
80 | | |
81 | | bool IPv6FragmentationHeader::isMoreFragments() const |
82 | 0 | { |
83 | 0 | uint8_t isMoreFragsBit = (getFragHeader()->fragOffsetAndFlags & (uint16_t)0x0100) >> 8; |
84 | 0 | return (isMoreFragsBit == 1); |
85 | 0 | } |
86 | | |
87 | | uint16_t IPv6FragmentationHeader::getFragmentOffset() const |
88 | 0 | { |
89 | 0 | uint16_t fragOffset = (be16toh(getFragHeader()->fragOffsetAndFlags & (uint16_t)0xf8ff) >> 3) * 8; |
90 | 0 | return fragOffset; |
91 | 0 | } |
92 | | |
93 | | // ==================== |
94 | | // IPv6TLVOptionBuilder |
95 | | // ==================== |
96 | | |
97 | | IPv6TLVOptionHeader::IPv6Option IPv6TLVOptionHeader::IPv6TLVOptionBuilder::build() const |
98 | 0 | { |
99 | 0 | auto recType = static_cast<uint8_t>(m_RecType); |
100 | 0 | bool isPad0 = (recType == IPv6Option::Pad0OptionType); |
101 | |
|
102 | 0 | size_t optionTotalSize = sizeof(uint8_t); |
103 | 0 | if (!isPad0) |
104 | 0 | { |
105 | 0 | optionTotalSize += sizeof(uint8_t) + m_RecValueLen; |
106 | 0 | } |
107 | |
|
108 | 0 | auto* recordBuffer = new uint8_t[optionTotalSize]; |
109 | 0 | memset(recordBuffer, 0, optionTotalSize); |
110 | |
|
111 | 0 | if (!isPad0) |
112 | 0 | { |
113 | 0 | recordBuffer[0] = recType; |
114 | 0 | recordBuffer[1] = static_cast<uint8_t>(m_RecValueLen); |
115 | 0 | if (m_RecValueLen > 0) |
116 | 0 | { |
117 | 0 | memcpy(recordBuffer + 2, m_RecValue, m_RecValueLen); |
118 | 0 | } |
119 | 0 | } |
120 | |
|
121 | 0 | return IPv6Option(recordBuffer); |
122 | 0 | } |
123 | | |
124 | | // =================== |
125 | | // IPv6TLVOptionHeader |
126 | | // =================== |
127 | | |
128 | | IPv6TLVOptionHeader::IPv6Option IPv6TLVOptionHeader::getOption(uint8_t optionType) const |
129 | 0 | { |
130 | 0 | return m_OptionReader.getTLVRecord(optionType, getDataPtr() + sizeof(ipv6_ext_base_header), |
131 | 0 | getExtensionLen() - sizeof(ipv6_ext_base_header)); |
132 | 0 | } |
133 | | |
134 | | IPv6TLVOptionHeader::IPv6Option IPv6TLVOptionHeader::getFirstOption() const |
135 | 0 | { |
136 | 0 | return m_OptionReader.getFirstTLVRecord(getDataPtr() + sizeof(ipv6_ext_base_header), |
137 | 0 | getExtensionLen() - sizeof(ipv6_ext_base_header)); |
138 | 0 | } |
139 | | |
140 | | IPv6TLVOptionHeader::IPv6Option IPv6TLVOptionHeader::getNextOption(IPv6TLVOptionHeader::IPv6Option& option) const |
141 | 0 | { |
142 | 0 | return m_OptionReader.getNextTLVRecord(option, getDataPtr() + sizeof(ipv6_ext_base_header), |
143 | 0 | getExtensionLen() - sizeof(ipv6_ext_base_header)); |
144 | 0 | } |
145 | | |
146 | | size_t IPv6TLVOptionHeader::getOptionCount() const |
147 | 0 | { |
148 | 0 | return m_OptionReader.getTLVRecordCount(getDataPtr() + sizeof(ipv6_ext_base_header), |
149 | 0 | getExtensionLen() - sizeof(ipv6_ext_base_header)); |
150 | 0 | } |
151 | | |
152 | | IPv6TLVOptionHeader::IPv6TLVOptionHeader(const std::vector<IPv6TLVOptionBuilder>& options) |
153 | 0 | { |
154 | 0 | m_ExtType = IPv6ExtensionUnknown; |
155 | 0 | m_OptionReader.changeTLVRecordCount(options.size()); |
156 | |
|
157 | 0 | size_t totalSize = sizeof(uint16_t); // nextHeader + headerLen |
158 | |
|
159 | 0 | for (const auto& iter : options) |
160 | 0 | { |
161 | 0 | IPv6Option option = iter.build(); |
162 | 0 | totalSize += option.getTotalSize(); |
163 | 0 | option.purgeRecordData(); |
164 | 0 | } |
165 | |
|
166 | 0 | while (totalSize % 8 != 0) |
167 | 0 | totalSize++; |
168 | |
|
169 | 0 | initShadowPtr(totalSize); |
170 | 0 | memset(getDataPtr(), 0, totalSize); |
171 | |
|
172 | 0 | getBaseHeader()->headerLen = ((totalSize / 8) - 1); |
173 | |
|
174 | 0 | size_t offset = sizeof(uint16_t); |
175 | |
|
176 | 0 | for (const auto& iter : options) |
177 | 0 | { |
178 | 0 | IPv6Option option = iter.build(); |
179 | 0 | memcpy(getDataPtr() + offset, option.getRecordBasePtr(), option.getTotalSize()); |
180 | 0 | offset += option.getTotalSize(); |
181 | 0 | option.purgeRecordData(); |
182 | 0 | } |
183 | 0 | } |
184 | | |
185 | | IPv6TLVOptionHeader::IPv6TLVOptionHeader(IDataContainer* dataContainer, size_t offset) |
186 | 18.5k | : IPv6Extension(dataContainer, offset) |
187 | 18.5k | {} |
188 | | |
189 | | // ================= |
190 | | // IPv6RoutingHeader |
191 | | // ================= |
192 | | |
193 | | IPv6RoutingHeader::IPv6RoutingHeader(uint8_t routingType, uint8_t segmentsLeft, |
194 | | const uint8_t* additionalRoutingData, size_t additionalRoutingDataLen) |
195 | 0 | { |
196 | 0 | size_t totalSize = sizeof(ipv6_routing_header) + additionalRoutingDataLen; |
197 | 0 | while (totalSize % 8 != 0) |
198 | 0 | totalSize++; |
199 | |
|
200 | 0 | initShadowPtr(totalSize); |
201 | 0 | memset(getDataPtr(), 0, totalSize); |
202 | |
|
203 | 0 | m_ExtType = IPv6Routing; |
204 | |
|
205 | 0 | ipv6_routing_header* routingHeader = getRoutingHeader(); |
206 | 0 | routingHeader->nextHeader = 0; |
207 | 0 | routingHeader->headerLen = ((totalSize / 8) - 1); |
208 | 0 | routingHeader->routingType = routingType; |
209 | 0 | routingHeader->segmentsLeft = segmentsLeft; |
210 | |
|
211 | 0 | if (additionalRoutingDataLen > 0 && additionalRoutingData != nullptr) |
212 | 0 | { |
213 | 0 | uint8_t* additionalDataPtr = getDataPtr() + sizeof(ipv6_routing_header); |
214 | 0 | memcpy(additionalDataPtr, additionalRoutingData, additionalRoutingDataLen); |
215 | 0 | } |
216 | 0 | } |
217 | | |
218 | | uint8_t* IPv6RoutingHeader::getRoutingAdditionalData() const |
219 | 0 | { |
220 | 0 | if (getExtensionLen() > sizeof(ipv6_routing_header)) |
221 | 0 | return getDataPtr() + sizeof(ipv6_routing_header); |
222 | | |
223 | 0 | return nullptr; |
224 | 0 | } |
225 | | |
226 | | size_t IPv6RoutingHeader::getRoutingAdditionalDataLength() const |
227 | 0 | { |
228 | 0 | int result = getExtensionLen() - sizeof(ipv6_routing_header); |
229 | 0 | if (result < 0) |
230 | 0 | return (size_t)0; |
231 | | |
232 | 0 | return (size_t)result; |
233 | 0 | } |
234 | | |
235 | | IPv6Address IPv6RoutingHeader::getRoutingAdditionalDataAsIPv6Address(size_t offset) const |
236 | 0 | { |
237 | |
|
238 | 0 | size_t routingAddDataLen = getRoutingAdditionalDataLength(); |
239 | 0 | if (routingAddDataLen - offset >= 16) |
240 | 0 | return IPv6Address(getRoutingAdditionalData() + offset); |
241 | | |
242 | 0 | return IPv6Address(); |
243 | 0 | } |
244 | | |
245 | | // ======================== |
246 | | // IPv6AuthenticationHeader |
247 | | // ======================== |
248 | | |
249 | | IPv6AuthenticationHeader::IPv6AuthenticationHeader(uint32_t securityParametersIndex, uint32_t sequenceNumber, |
250 | | const uint8_t* integrityCheckValue, |
251 | | size_t integrityCheckValueLen) |
252 | 0 | { |
253 | 0 | size_t totalSize = sizeof(ipv6_authentication_header) + integrityCheckValueLen; |
254 | 0 | while (totalSize % 8 != 0) |
255 | 0 | totalSize++; |
256 | |
|
257 | 0 | initShadowPtr(totalSize); |
258 | 0 | memset(getDataPtr(), 0, totalSize); |
259 | |
|
260 | 0 | m_ExtType = IPv6AuthenticationHdr; |
261 | |
|
262 | 0 | ipv6_authentication_header* authHeader = getAuthHeader(); |
263 | 0 | authHeader->nextHeader = 0; |
264 | 0 | authHeader->headerLen = ((totalSize / 4) - 2); |
265 | 0 | authHeader->securityParametersIndex = htobe32(securityParametersIndex); |
266 | 0 | authHeader->sequenceNumber = htobe32(sequenceNumber); |
267 | |
|
268 | 0 | if (integrityCheckValueLen > 0 && integrityCheckValue != nullptr) |
269 | 0 | { |
270 | 0 | uint8_t* icvPtr = getDataPtr() + sizeof(ipv6_authentication_header); |
271 | 0 | memcpy(icvPtr, integrityCheckValue, integrityCheckValueLen); |
272 | 0 | } |
273 | 0 | } |
274 | | |
275 | | uint8_t* IPv6AuthenticationHeader::getIntegrityCheckValue() const |
276 | 0 | { |
277 | 0 | if (getExtensionLen() > sizeof(ipv6_authentication_header)) |
278 | 0 | return getDataPtr() + sizeof(ipv6_authentication_header); |
279 | | |
280 | 0 | return nullptr; |
281 | 0 | } |
282 | | |
283 | | size_t IPv6AuthenticationHeader::getIntegrityCheckValueLength() const |
284 | 0 | { |
285 | 0 | int result = getExtensionLen() - sizeof(ipv6_authentication_header); |
286 | 0 | if (result < 0) |
287 | 0 | return (size_t)0; |
288 | | |
289 | 0 | return (size_t)result; |
290 | 0 | } |
291 | | |
292 | | } // namespace pcpp |