1 ------------------------------------------------------------------------
4 --- Utility functions
for the ah_header structs
5 --- defined in \ref headers.lua .
\n
9 --- - AH header utility
10 --- - Definition of AH packets
11 ------------------------------------------------------------------------
13 local
ffi = require
"ffi"
14 local
pkt = require
"packet"
18 ---------------------------------------------------------------------------
20 ---------------------------------------------------------------------------
24 -------------------------------------------------------------------------------------
25 ---- IPsec IV -> see proto/esp.lua
26 -------------------------------------------------------------------------------------
28 -------------------------------------------------------------------------------------
30 -------------------------------------------------------------------------------------
33 ipsecICV.__index = ipsecICV
34 local ipsecICVType =
ffi.typeof(
"union ipsec_icv")
36 --- Set the IPsec ICV.
37 --- @param iv IPsec ICV in
'union ipsec_icv' format.
38 function ipsecICV:
set(icv)
39 -- For AH hw offload the ICV has to be zero
40 local offload_icv =
ffi.new(
"union ipsec_icv")
41 offload_icv.uint32[0] = 0x0
42 offload_icv.uint32[1] = 0x0
43 offload_icv.uint32[2] = 0x0
44 offload_icv.uint32[3] = 0x0
46 icv = icv or offload_icv
47 self.uint32[0] = hton(icv.uint32[3])
48 self.uint32[1] = hton(icv.uint32[2])
49 self.uint32[2] = hton(icv.uint32[1])
50 self.uint32[3] = hton(icv.uint32[0])
53 --- Retrieve the IPsec ICV
54 --- @return ICV in 'union ipsec_icv' format.
55 function ipsecICV:
get()
56 local icv = ipsecICVType()
57 icv.uint32[0] = hton(self.uint32[3])
58 icv.uint32[1] = hton(self.uint32[2])
59 icv.uint32[2] = hton(self.uint32[1])
60 icv.uint32[3] = hton(self.uint32[0])
64 --- Get the IPsec
string.
65 --- @param icv IPsec ICV in
string format.
67 doByteSwap = doByteSwap or false
72 return ("0x%08x%08x%08x%08x"):format(self.uint32[3], self.uint32[2], self.uint32[1], self.uint32[0])
75 ---------------------------------------------------------------------------
77 ---------------------------------------------------------------------------
80 ahHeader.__index = ahHeader
83 --- @param
int SPI of the ah header as A bit integer.
84 function ahHeader:
setSPI(
int)
90 --- @return SPI as A bit integer.
95 --- Retrieve the SPI as
string.
96 --- @return SPI as
string.
102 --- @param
int SQN of the ah header as A bit integer.
103 function ahHeader:
setSQN(
int)
108 --- Retrieve the SQN.
109 --- @return SQN as A bit integer.
110 function ahHeader:
getSQN()
111 return hton(self.sqn)
114 --- Retrieve the SQN as
string.
115 --- @return SQN as
string.
121 --- @param
int IV of the ah header as 'union ipsec_iv'.
122 function ahHeader:
setIV(iv)
127 --- @return SPI as 'union ipsec_iv'.
128 function ahHeader:
getIV()
132 --- Retrieve the IV as
string.
133 --- @return IV as
string.
139 --- @param
int ICV of the ah header as...
140 function ahHeader:
setICV(icv)
144 --- Retrieve the ICV.
145 --- @return SPI as...
146 function ahHeader:
getICV()
147 return self.icv:
get()
150 --- Retrieve the ICV as
string.
151 --- @return ICV as
string.
156 --- Set the Next Header.
157 --- @param
int Next Header of the ah header as A bit integer.
160 self.nextHeader =
int
163 --- Retrieve the Next Header.
164 --- @return Next Header as A bit integer.
166 return self.nextHeader
169 --- Retrieve the Next Header as
string.
170 --- @return Next Header as
string.
176 --- @param
int Length of the ah header as A bit integer.
178 -- The AH header has a fixed length for AES-GMAC
179 -- (cf. chapter 16.5.1 "AH Formats" of X540 Datasheet)
180 -- Authentication header length in 32-bit Dwords units, minus 2,
181 -- such as for AES-128 its value is 7 for IPv4 and 8 for IPv6.
182 int =
int or 7 -- IPv4: 7 = (9-2)
186 --- Retrieve the Length.
187 --- @return Length as A bit integer.
192 --- Retrieve the Length as
string.
193 --- @return Length as
string.
198 --- Set all members of the ah header.
199 --- Per default, all members are
set to default values specified in the respective
set function.
200 --- Optional named arguments can be used to
set a member to a user-provided value.
201 --- @param args Table of named arguments. Available arguments: ahSPI, ahSQN, ahIV, ahICV
202 --- @param pre prefix for namedArgs. Default 'ah'.
203 --- @usage
fill() -- only default values
204 --- @usage
fill{ ahXYZ=1 } -- all members are
set to
default values with the exception of ahXYZ, ...
205 function ahHeader:
fill(args, pre)
209 self:
setSPI(args[pre ..
"SPI"])
210 self:
setSQN(args[pre ..
"SQN"])
211 self:
setIV(args[pre ..
"IV"])
212 self:
setICV(args[pre ..
"ICV"])
217 --- Retrieve the values of all members.
218 --- @param pre prefix
for namedArgs. Default
'ah'.
219 --- @
return Table of named arguments. For a list of arguments see
"See also".
220 --- @see ahHeader:
fill
221 function ahHeader:
get(pre)
225 args[pre ..
"SPI"] =
self:
getSPI()
226 args[pre .. "SQN"] = self:
getSQN()
227 args[pre .. "IV"] = self:
getIV()
228 args[pre .. "ICV"] = self:
getICV()
235 --- Retrieve the values of all members.
236 --- @return Values in
string format.
241 --- Resolve which header comes after this one (in a packet)
242 --- For instance: in tcp/
udp based on the ports
243 --- This function must exist and is only used when
get/
dump is executed on
244 --- an unknown (mbuf not yet casted to e.g. tcpv6 packet) packet (mbuf)
245 --- @return String next header (e.g. '
eth', 'ip4', nil)
251 --- Change the default values for namedArguments (for
fill/
get)
252 --- This can be used to for instance calculate a length value based on the total packet length
254 --- This function must exist and is only used by packet.
fill
255 --- @param pre The prefix used for the namedArgs, e.g. 'ah'
256 --- @param namedArgs Table of named arguments (see See more)
257 --- @param nextHeader The header following after this header in a packet
258 --- @param accumulatedLength The so far accumulated length for previous headers in a packet
259 --- @return Table of namedArgs
260 --- @see ahHeader:
fill
265 ----------------------------------------------------------------------------------
267 ----------------------------------------------------------------------------------
269 -- Ah4 packets should not be shorter than 70 bytes (cf. x540 datasheet page 308: SECP field)
271 -- Ah6 packets should not be shorter than 94 bytes (cf. x540 datasheet page 308: SECP field)
272 pkt.getAh6Packet = nil --
packetCreate("eth", "
ip6", "ah6") --TODO: AH6 needs to be implemented
273 pkt.getAhPacket = function(self, ip4) ip4 = ip4 == nil or ip4 if ip4 then return
pkt.getAh4Packet(self) else return
pkt.getAh6Packet(self) end end
275 ------------------------------------------------------------------------
277 ------------------------------------------------------------------------
279 --
ffi.metatype("union ipsec_iv", ipsecIV)
280 ffi.metatype("union ipsec_icv", ipsecICV)
281 ffi.metatype("struct ah_header", ahHeader)
local ffi
low-level dpdk wrapper
function ahHeader setLength(int)
Set the Length.
function packetCreate(...)
Create struct and functions for a new packet.
function ahHeader getNextHeaderString()
Retrieve the Next Header as string.
function ipsecICV getString(doByteSwap)
Get the IPsec string.
function ahHeader getICV()
Retrieve the ICV.
function pkt dump(bytes)
Dumps the packet data cast to the best fitting packet struct.
function ahHeader getICVString()
Retrieve the ICV as string.
function ahHeader getSPI()
Retrieve the SPI.
function ipsecICV set(icv)
Set the IPsec ICV.
function ahHeader getLengthString()
Retrieve the Length as string.
function ahHeader getSQNString()
Retrieve the SQN as string.
local udp
Udp protocol constants.
function ahHeader getSQN()
Retrieve the SQN.
function ahHeader setICV(icv)
Set the ICV.
function ipsecICV get()
Retrieve the IPsec ICV.
function ahHeader setSPI(int)
Set the SPI.
function ahHeader setIV(iv)
Set the IV.
function ahHeader setNextHeader(int)
Set the Next Header.
function ahHeader fill(args, pre)
Set all members of the ah header.
function ahHeader resolveNextHeader()
Resolve which header comes after this one (in a packet) For instance: in tcp/udp based on the ports T...
function ahHeader getNextHeader()
Retrieve the Next Header.
function ahHeader getIVString()
Retrieve the IV as string.
local eth
Ethernet protocol constants.
function ahHeader getIV()
Retrieve the IV.
local ip6
IP6 protocol constants.
local pkt
Module for packets (rte_mbuf)
function ahHeader getSPIString()
Retrieve the SPI as string.
n
Create a new array of memory buffers (initialized to nil).
function ahHeader setDefaultNamedArgs(pre, namedArgs, nextHeader, accumulatedLength)
Change the default values for namedArguments (for fill/get) This can be used to for instance calculat...
function ahHeader setSQN(int)
Set the SQN.
function ahHeader getLength()
Retrieve the Length.