1 ------------------------------------------------------------------------
3 --- @brief Internet control message protocol utility.
4 --- Utility functions
for the icmp_header
struct
5 --- defined in \ref headers.lua .
\n
9 --- - Icmp header utility
10 --- - Definition of Icmp packets
11 ------------------------------------------------------------------------
13 local
ffi = require
"ffi"
14 local
pkt = require
"packet"
19 local ntoh, hton = ntoh, hton
20 local ntoh16, hton16 = ntoh16, hton16
21 local
bor,
band,
bnot, rshift, lshift= bit.bor, bit.band, bit.bnot, bit.rshift, bit.lshift
22 local istype =
ffi.istype
23 local format =
string.format
26 -- ICMPv6 and ICMPv4 use different values
for the same types/codes which causes some complications when handling
this with only one header:
27 -- -
getString() does not work for ICMPv6 correctly without some ugly workarounds (basically adding 'ipv4' flags to
getString()'s of type/code and header)
28 -- currently
getString() simply does not recognise ICMPv6
31 -- remove messageBody, instead use
new packetCreate with additional header {
"ip4",
"messageBody" } or similar
34 ---------------------------------------------------------------------------
36 ---------------------------------------------------------------------------
38 --- Icmp4 protocol constants
41 --- Icmp4 type-code pair: echo reply
42 icmp.ECHO_REPLY = { type = 0, code = 0 }
43 --- Icmp4 type-code pair: echo request
44 icmp.ECHO_REQUEST = { type = 8, code = 0 }
46 --- Icmp4 type-code pair: destination unreachable - port unreachable
47 icmp.DST_UNR_PORT_UNR = { type = 3, code = 3 }
49 --- Icmp4 type-code pair: time exceeded - TTL exceeded
50 icmp.TIME_EXCEEDED_TTL_EXPIRED = { type = 11, code = 0 }
53 --------------------------------------------------------------------------
55 ---------------------------------------------------------------------------
57 --- Icmp6 protocol constants
60 --- Icmp6 type-code pair: echo request
61 icmp6.ECHO_REQUEST = { type = 128, code = 0 }
62 --- Icmp6 type-code pair: echo reply
63 icmp6.ECHO_REPLY = { type = 129, code = 0 }
66 ---------------------------------------------------------------------------
68 ---------------------------------------------------------------------------
70 --- Module
for icmp_header
struct (see \ref headers.lua).
75 --- @param
int Type of the
icmp header as 8 bit integer.
81 --- Retrieve the type.
82 --- @return Type as 8 bit integer.
87 --- Retrieve the type.
88 --- does not work for ICMPv6 (ICMPv6 uses different values)
89 --- @return Type as
string.
92 local cleartext = "unknown"
95 cleartext = "echo reply"
97 cleartext = "echo request"
99 cleartext = "dst. unr."
101 cleartext = "time exceeded"
104 return format("%s (%s)", type, cleartext)
108 --- @param
int Code of the
icmp header as 8 bit integer.
114 --- Retrieve the code.
115 --- @return Code as 8 bit integer.
120 --- Retrieve the code.
121 --- does not work for ICMPv6
122 --- @return Code as
string.
126 local cleartext = "unknown"
136 cleartext = "port unr."
141 cleartext = "ttl expired"
145 return format("%s (%s)", code, cleartext)
150 --- @param
int Checksum of the
icmp header as 16 bit integer.
153 self.cs = hton16(
int)
157 --- @param len Number of bytes that the
checksum will be computed over
159 len = len or sizeof(self)
165 --- @return Checksum as 16 bit integer.
167 return hton16(self.cs)
171 --- @return Checksum as
string.
176 --- Set the message body.
177 --- @param
int Message body of the
icmp header as TODO.
180 --self.body.uint8_t = body
183 --- Retrieve the message body.
184 --- @return Message body as TODO.
189 --- Retrieve the message body.
190 --- @return Message body as
string TODO.
195 --- Set all members of the
icmp header.
196 --- Per default, all members are
set to default values specified in the respective
set function.
197 --- Optional named arguments can be used to
set a member to a user-provided value.
198 --- @param args Table of named arguments. Available arguments: Type, Code, Checksum, MessageBody
199 --- @param pre prefix for namedArgs. Default '
icmp'.
201 ---
fill() --- only default values
202 ---
fill{ icmpCode=3 } --- all members are
set to
default values with the exception of icmpCode
208 self:
setType(args[pre ..
"Type"])
209 self:
setCode(args[pre ..
"Code"])
214 --- Retrieve the values of all members.
215 --- @param pre prefix
for namedArgs. Default
'icmp'.
216 --- @
return Table of named arguments. For a list of arguments see
"See also".
222 args[pre ..
"Type"] =
self:
getType()
223 args[pre .. "Code"] = self:
getCode()
230 --- Retrieve the values of all members.
231 --- Does not work correctly for ICMPv6 packets
232 --- @return Values in
string format.
240 --- Resolve which header comes after this one (in a packet).
241 --- For instance: in tcp/
udp based on the ports.
242 --- This function must exist and is only used when
get/
dump is executed on
243 --- an unknown (mbuf not yet casted to e.g. tcpv6 packet) packet (mbuf)
244 --- @return String next header (e.g. '
udp', '
icmp', nil)
249 --- Change the default values for namedArguments (for
fill/
get).
250 --- This can be used to for instance calculate a length value based on the total packet length.
252 --- This function must exist and is only used by packet.
fill.
253 --- @param pre The prefix used for the namedArgs, e.g. 'icmp'
254 --- @param namedArgs Table of named arguments (see See Also)
255 --- @param nextHeader The header following after this header in a packet
256 --- @param accumulatedLength The so far accumulated length for previous headers in a packet
257 --- @return Table of namedArgs
264 ------------------------------------------------------------------------
266 ------------------------------------------------------------------------
268 --- Cast the packet to an Icmp4 packet
270 --- Cast the packet to an Icmp6 packet
272 --- Cast the packet to either an Icmp4 (nil/true) or Icmp6 (false) packet, depending on the passed
boolean.
276 ------------------------------------------------------------------------
278 ------------------------------------------------------------------------
icmp ECHO_REQUEST
Icmp4 type-code pair: echo request.
local ffi
low-level dpdk wrapper
function checksum(data, len)
Calculate a 16 bit checksum.
function icmpHeader resolveNextHeader()
Resolve which header comes after this one (in a packet).
function icmpHeader getTypeString()
Retrieve the type.
function packetCreate(...)
Create struct and functions for a new packet.
local icmpHeader
Module for icmp_header struct (see headers.lua).
function mod band(mask1, mask2, result)
Bitwise and.
function icmpHeader getCodeString()
Retrieve the code.
function pkt dump(bytes)
Dumps the packet data cast to the best fitting packet struct.
function icmpHeader getMessageBody()
Retrieve the message body.
function icmpHeader setChecksum(int)
Set the checksum.
local icmp6
Icmp6 protocol constants.
function ipsecICV set(icv)
Set the IPsec ICV.
local udp
Udp protocol constants.
function mod bor(mask1, mask2, result)
Bitwise or.
function icmpHeader setDefaultNamedArgs(pre, namedArgs, nextHeader, accumulatedLength)
Change the default values for namedArguments (for fill/get).
function icmpHeader setCode(int)
Set the code.
function icmpHeader getMessageBodyString()
Retrieve the message body.
function icmpHeader calculateChecksum(len)
Calculate the checksum.
icmp DST_UNR_PORT_UNR
Icmp4 type-code pair: destination unreachable - port unreachable.
icmp TIME_EXCEEDED_TTL_EXPIRED
Icmp4 type-code pair: time exceeded - TTL exceeded.
function icmpHeader getChecksum()
Retrieve the checksum.
function icmpHeader setMessageBody(body)
Set the message body.
function icmpHeader getCode()
Retrieve the code.
function icmpHeader setType(int)
Set the type.
function icmpHeader fill(args, pre)
Set all members of the icmp header.
function icmpHeader getChecksumString()
Retrieve the checksum.
function icmpHeader getString()
Retrieve the values of all members.
function icmpHeader get(pre)
Retrieve the values of all members.
function packetDump(self, bytes)
Print a hex dump of a packet.
local eth
Ethernet protocol constants.
local ip6
IP6 protocol constants.
local pkt
Module for packets (rte_mbuf)
function mod bnot(mask, result)
Bitwise not.
n
Create a new array of memory buffers (initialized to nil).
pkt getIcmp4Packet
Cast the packet to an Icmp4 packet.
local icmp
Icmp4 protocol constants.
function icmpHeader getType()
Retrieve the type.
pkt getIcmp6Packet
Cast the packet to an Icmp6 packet.
icmp ECHO_REPLY
Icmp4 type-code pair: echo reply.