forked from seladb/PcapPlusPlus
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIcmpV6Layer.cpp
More file actions
170 lines (138 loc) · 4.68 KB
/
IcmpV6Layer.cpp
File metadata and controls
170 lines (138 loc) · 4.68 KB
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
#define LOG_MODULE PacketLogModuleIcmpV6Layer
#include "IcmpV6Layer.h"
#include "EndianPortable.h"
#include "IPv4Layer.h"
#include "IPv6Layer.h"
#include "NdpLayer.h"
#include "PacketUtils.h"
#include "PayloadLayer.h"
#include <sstream>
// IcmpV6Layer
namespace pcpp
{
Layer* IcmpV6Layer::parseIcmpV6Layer(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet)
{
if (dataLen < sizeof(icmpv6hdr))
return new PayloadLayer(data, dataLen, prevLayer, packet);
icmpv6hdr* hdr = (icmpv6hdr*)data;
ICMPv6MessageType messageType = static_cast<ICMPv6MessageType>(hdr->type);
switch (messageType)
{
case ICMPv6MessageType::ICMPv6_ECHO_REQUEST:
case ICMPv6MessageType::ICMPv6_ECHO_REPLY:
return new ICMPv6EchoLayer(data, dataLen, prevLayer, packet);
case ICMPv6MessageType::ICMPv6_NEIGHBOR_SOLICITATION:
return new NDPNeighborSolicitationLayer(data, dataLen, prevLayer, packet);
case ICMPv6MessageType::ICMPv6_NEIGHBOR_ADVERTISEMENT:
return new NDPNeighborAdvertisementLayer(data, dataLen, prevLayer, packet);
case ICMPv6MessageType::ICMPv6_UNKNOWN_MESSAGE:
return new PayloadLayer(data, dataLen, prevLayer, packet);
default:
return new IcmpV6Layer(data, dataLen, prevLayer, packet);
}
}
IcmpV6Layer::IcmpV6Layer(ICMPv6MessageType msgType, uint8_t code, const uint8_t* data, size_t dataLen)
{
m_DataLen = sizeof(icmpv6hdr) + dataLen;
m_Data = new uint8_t[m_DataLen];
memset(m_Data, 0, m_DataLen);
m_Protocol = ICMPv6;
icmpv6hdr* hdr = (icmpv6hdr*)m_Data;
hdr->type = static_cast<uint8_t>(msgType);
hdr->code = code;
if (data != nullptr && dataLen > 0)
memcpy(m_Data + sizeof(icmpv6hdr), data, dataLen);
}
ICMPv6MessageType IcmpV6Layer::getMessageType() const
{
return static_cast<ICMPv6MessageType>(getIcmpv6Header()->type);
}
uint8_t IcmpV6Layer::getCode() const
{
return getIcmpv6Header()->code;
}
uint16_t IcmpV6Layer::getChecksum() const
{
return be16toh(getIcmpv6Header()->checksum);
}
void IcmpV6Layer::computeCalculateFields()
{
calculateChecksum();
}
void IcmpV6Layer::calculateChecksum()
{
// Pseudo header of 40 bytes which is composed as follows(in order):
// - 16 bytes for the source address
// - 16 bytes for the destination address
// - 4 bytes big endian payload length(the same value as in the IPv6 header)
// - 3 bytes zero + 1 byte nextheader( 58 decimal) big endian
getIcmpv6Header()->checksum = 0;
if (m_PrevLayer != nullptr)
{
auto prevLayerAsIPv6 = static_cast<IPv6Layer*>(m_PrevLayer);
ScalarBuffer<uint16_t> vec[2];
vec[0].buffer = (uint16_t*)m_Data;
vec[0].len = m_DataLen;
const unsigned int pseudoHeaderLen = 40;
const unsigned int bigEndianLen = htobe32(m_DataLen);
const unsigned int bigEndianNextHeader = htobe32(PACKETPP_IPPROTO_ICMPV6);
uint16_t pseudoHeader[pseudoHeaderLen / 2];
prevLayerAsIPv6->getSrcIPv6Address().copyTo(reinterpret_cast<uint8_t*>(pseudoHeader));
prevLayerAsIPv6->getDstIPv6Address().copyTo(reinterpret_cast<uint8_t*>(pseudoHeader + 8));
memcpy(&pseudoHeader[16], &bigEndianLen, sizeof(uint32_t));
memcpy(&pseudoHeader[18], &bigEndianNextHeader, sizeof(uint32_t));
vec[1].buffer = pseudoHeader;
vec[1].len = pseudoHeaderLen;
// Calculate and write checksum
getIcmpv6Header()->checksum = htobe16(computeChecksum(vec, 2));
}
}
std::string IcmpV6Layer::toString() const
{
std::ostringstream typeStream;
typeStream << (int)getMessageType();
return "ICMPv6 Layer, Message type: " + typeStream.str();
}
//
// ICMPv6EchoLayer
//
ICMPv6EchoLayer::ICMPv6EchoLayer(ICMPv6EchoType echoType, uint16_t id, uint16_t sequence, const uint8_t* data,
size_t dataLen)
{
m_DataLen = sizeof(icmpv6_echo_hdr) + dataLen;
m_Data = new uint8_t[m_DataLen];
memset(m_Data, 0, m_DataLen);
m_Protocol = ICMPv6;
icmpv6_echo_hdr* header = getEchoHeader();
switch (echoType)
{
case REPLY:
header->type = static_cast<uint8_t>(ICMPv6MessageType::ICMPv6_ECHO_REPLY);
break;
case REQUEST:
default:
header->type = static_cast<uint8_t>(ICMPv6MessageType::ICMPv6_ECHO_REQUEST);
break;
}
header->code = 0;
header->checksum = 0;
header->id = htobe16(id);
header->sequence = htobe16(sequence);
if (data != nullptr && dataLen > 0)
memcpy(getEchoDataPtr(), data, dataLen);
}
uint16_t ICMPv6EchoLayer::getIdentifier() const
{
return be16toh(getEchoHeader()->id);
}
uint16_t ICMPv6EchoLayer::getSequenceNr() const
{
return be16toh(getEchoHeader()->sequence);
}
std::string ICMPv6EchoLayer::toString() const
{
std::ostringstream typeStream;
typeStream << (int)getMessageType();
return "ICMPv6 Layer, Echo Request/Reply Message (type: " + typeStream.str() + ")";
}
} // namespace pcpp