1 ------------------------------------------------------------------------
3 --- @brief ESP utility.
4 --- Utility functions
for the esp_header structs
5 --- defined in \ref headers.lua .
\n
9 --- - ESP header utility
10 --- - Definition of esp packets
11 ------------------------------------------------------------------------
13 local
ffi = require
"ffi"
14 local
pkt = require
"packet"
15 local math = require
"math"
20 ---------------------------------------------------------------------------
22 ---------------------------------------------------------------------------
26 -------------------------------------------------------------------------------------
28 -------------------------------------------------------------------------------------
31 ipsecIV.__index = ipsecIV
32 local ipsecIVType =
ffi.typeof(
"union ipsec_iv")
35 --- @param iv IPsec IV in
'union ipsec_iv' format.
36 function ipsecIV:
set(iv)
37 local random_iv =
ffi.
new("union ipsec_iv")
38 random_iv.uint32[0] = math.random(0, 2^32-1)
39 random_iv.uint32[1] = math.random(0, 2^32-1)
41 local iv = iv or random_iv
42 self.uint32[0] = hton(iv.uint32[1])
43 self.uint32[1] = hton(iv.uint32[0])
46 --- Retrieve the IPsec IV.
47 --- @return IV in 'union ipsec_iv' format.
48 function ipsecIV:
get()
49 local iv = ipsecIVType()
50 iv.uint32[0] = hton(self.uint32[1])
51 iv.uint32[1] = hton(self.uint32[0])
55 --- Get the IPsec IV as
string.
56 --- @param iv IPsec IV in
string format.
58 doByteSwap = doByteSwap or false
63 return ("0x%08x%08x"):format(self.uint32[1], self.uint32[0])
66 ---------------------------------------------------------------------------
68 ---------------------------------------------------------------------------
71 espHeader.__index = espHeader
74 --- @param
int SPI of the esp header as A bit integer.
75 function espHeader:
setSPI(
int)
81 --- @return SPI as A bit integer.
82 function espHeader:
getSPI()
86 --- Retrieve the SPI as
string.
87 --- @return SPI as
string.
89 return ("0x%08x"):format(self.spi)
93 --- @param
int SQN of the esp header as A bit integer.
94 function espHeader:
setSQN(
int)
100 --- @return SQN as A bit integer.
101 function espHeader:
getSQN()
102 return hton(self.sqn)
105 --- Retrieve the SQN as
string.
106 --- @return SQN as
string.
112 --- @param
int IV of the esp header as 'union ipsec_iv'.
113 function espHeader:
setIV(iv)
118 --- @return SPI as 'union ipsec_iv'.
119 function espHeader:
getIV()
123 --- Retrieve the IV as
string.
124 --- @return IV as
string.
129 --- Set all members of the esp header.
130 --- Per default, all members are
set to default values specified in the respective
set function.
131 --- Optional named arguments can be used to
set a member to a user-provided value.
132 --- @param args Table of named arguments. Available arguments: espSPI, espSQN
133 --- @param pre prefix for namedArgs. Default 'esp'.
134 --- @usage
fill() -- only default values
135 --- @usage
fill{ espXYZ=1 } -- all members are
set to
default values with the exception of espXYZ, ...
136 function espHeader:
fill(args, pre)
140 self:
setSPI(args[pre ..
"SPI"])
141 self:
setSQN(args[pre ..
"SQN"])
142 self:
setIV(args[pre ..
"IV"])
145 --- Retrieve the values of all members.
146 --- @param pre prefix
for namedArgs. Default
'esp'.
147 --- @
return Table of named arguments. For a list of arguments see
"See also".
148 --- @see espHeader:
fill
149 function espHeader:
get(pre)
153 args[pre ..
"SPI"] =
self:
getSPI()
154 args[pre .. "SQN"] = self:
getSQN()
155 args[pre .. "IV"] = self:
getIV()
160 --- Retrieve the values of all members.
161 --- @return Values in
string format.
163 --TODO:
add data from ESP trailer
167 --- Resolve which header comes after this one (in a packet)
168 --- For instance: in tcp/
udp based on the ports
169 --- This function must exist and is only used when
get/
dump is executed on
170 --- an unknown (mbuf not yet casted to e.g. tcpv6 packet) packet (mbuf)
171 --- @return String next header (e.g. '
eth', 'ip4', nil)
173 --TODO: next_header field is in ESP trailer
177 --- Change the default values for namedArguments (for
fill/
get)
178 --- This can be used to for instance calculate a length value based on the total packet length
180 --- This function must exist and is only used by packet.
fill
181 --- @param pre The prefix used for the namedArgs, e.g. 'esp'
182 --- @param namedArgs Table of named arguments (see See more)
183 --- @param nextHeader The header following after this header in a packet
184 --- @param accumulatedLength The so far accumulated length for previous headers in a packet
185 --- @return Table of namedArgs
186 --- @see espHeader:
fill
191 ----------------------------------------------------------------------------------
193 ----------------------------------------------------------------------------------
195 -- Esp4 packets should not be shorter than 70 bytes (cf. x540 datasheet page 308: SECP field)
197 -- Esp6 packets should not be shorter than 90 bytes (cf. x540 datasheet page 308: SECP field)
199 pkt.getEspPacket = function(self, ip4) ip4 = ip4 == nil or ip4 if ip4 then return
pkt.getEsp4Packet(self) else return
pkt.getEsp6Packet(self) end end
201 ------------------------------------------------------------------------
203 ------------------------------------------------------------------------
205 ffi.metatype("union ipsec_iv", ipsecIV)
206 ffi.metatype("struct esp_header", espHeader)
function espHeader getIVString()
Retrieve the IV as string.
local ffi
low-level dpdk wrapper
function ipsecIV set(iv)
Set the IPsec IV.
function espHeader getSPIString()
Retrieve the SPI as string.
function ipsecIV getString(doByteSwap)
Get the IPsec IV as string.
function packetCreate(...)
Create struct and functions for a new packet.
function pkt dump(bytes)
Dumps the packet data cast to the best fitting packet struct.
local udp
Udp protocol constants.
function espHeader fill(args, pre)
Set all members of the esp header.
function espHeader getSQN()
Retrieve the SQN.
function espHeader getSPI()
Retrieve the SPI.
function ip4Addr add(val)
Add a number to an IPv4 address in-place.
function espHeader setIV(iv)
Set the IV.
function ipsecIV get()
Retrieve the IPsec IV.
local eth
Ethernet protocol constants.
function espHeader setDefaultNamedArgs(pre, namedArgs, nextHeader, accumulatedLength)
Change the default values for namedArguments (for fill/get) This can be used to for instance calculat...
local ip6
IP6 protocol constants.
function espHeader setSQN(int)
Set the SQN.
local pkt
Module for packets (rte_mbuf)
function espHeader resolveNextHeader()
Resolve which header comes after this one (in a packet) For instance: in tcp/udp based on the ports T...
function espHeader getSQNString()
Retrieve the SQN as string.
n
Create a new array of memory buffers (initialized to nil).
function espHeader setSPI(int)
Set the SPI.
function espHeader getIV()
Retrieve the IV.