MoonGen
 All Files Functions Variables Pages
ah.lua
Go to the documentation of this file.
1 ------------------------------------------------------------------------
2 --- @file ah.lua
3 --- @brief AH utility.
4 --- Utility functions for the ah_header structs
5 --- defined in \ref headers.lua . \n
6 --- Includes:
7 --- - AH constants
8 --- - IPsec ICV
9 --- - AH header utility
10 --- - Definition of AH packets
11 ------------------------------------------------------------------------
12 
13 local ffi = require "ffi"
14 local pkt = require "packet"
15 
16 require "headers"
17 
18 ---------------------------------------------------------------------------
19 ---- ah constants
20 ---------------------------------------------------------------------------
21 
22 local ah = {}
23 
24 -------------------------------------------------------------------------------------
25 ---- IPsec IV -> see proto/esp.lua
26 -------------------------------------------------------------------------------------
27 
28 -------------------------------------------------------------------------------------
29 ---- IPsec ICV
30 -------------------------------------------------------------------------------------
31 
32 local ipsecICV = {}
33 ipsecICV.__index = ipsecICV
34 local ipsecICVType = ffi.typeof("union ipsec_icv")
35 
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
45 
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])
51 end
52 
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])
61  return icv
62 end
63 
64 --- Get the IPsec string.
65 --- @param icv IPsec ICV in string format.
66 function ipsecICV:getString(doByteSwap)
67  doByteSwap = doByteSwap or false
68  if doByteSwap then
69  self = self:get()
70  end
71 
72  return ("0x%08x%08x%08x%08x"):format(self.uint32[3], self.uint32[2], self.uint32[1], self.uint32[0])
73 end
74 
75 ---------------------------------------------------------------------------
76 ---- ah header
77 ---------------------------------------------------------------------------
78 
79 local ahHeader = {}
80 ahHeader.__index = ahHeader
81 
82 --- Set the SPI.
83 --- @param int SPI of the ah header as A bit integer.
84 function ahHeader:setSPI(int)
85  int = int or 0
86  self.spi = hton(int)
87 end
88 
89 --- Retrieve the SPI.
90 --- @return SPI as A bit integer.
91 function ahHeader:getSPI()
92  return hton(self.spi)
93 end
94 
95 --- Retrieve the SPI as string.
96 --- @return SPI as string.
97 function ahHeader:getSPIString()
98  return self:getSPI()
99 end
100 
101 --- Set the SQN.
102 --- @param int SQN of the ah header as A bit integer.
103 function ahHeader:setSQN(int)
104  int = int or 0
105  self.sqn = hton(int)
106 end
107 
108 --- Retrieve the SQN.
109 --- @return SQN as A bit integer.
110 function ahHeader:getSQN()
111  return hton(self.sqn)
112 end
113 
114 --- Retrieve the SQN as string.
115 --- @return SQN as string.
116 function ahHeader:getSQNString()
117  return self:getSQN()
118 end
119 
120 --- Set the IV.
121 --- @param int IV of the ah header as 'union ipsec_iv'.
122 function ahHeader:setIV(iv)
123  self.iv:set(iv)
124 end
125 
126 --- Retrieve the IV.
127 --- @return SPI as 'union ipsec_iv'.
128 function ahHeader:getIV()
129  return self.iv:get()
130 end
131 
132 --- Retrieve the IV as string.
133 --- @return IV as string.
134 function ahHeader:getIVString()
135  return self.iv:getString(true)
136 end
137 
138 --- Set the ICV.
139 --- @param int ICV of the ah header as...
140 function ahHeader:setICV(icv)
141  self.icv:set(icv)
142 end
143 
144 --- Retrieve the ICV.
145 --- @return SPI as...
146 function ahHeader:getICV()
147  return self.icv:get()
148 end
149 
150 --- Retrieve the ICV as string.
151 --- @return ICV as string.
152 function ahHeader:getICVString()
153  return self.icv:getString(true)
154 end
155 
156 --- Set the Next Header.
157 --- @param int Next Header of the ah header as A bit integer.
158 function ahHeader:setNextHeader(int)
159  int = int or 0
160  self.nextHeader = int
161 end
162 
163 --- Retrieve the Next Header.
164 --- @return Next Header as A bit integer.
165 function ahHeader:getNextHeader()
166  return self.nextHeader
167 end
168 
169 --- Retrieve the Next Header as string.
170 --- @return Next Header as string.
171 function ahHeader:getNextHeaderString()
172  return self:getNextHeader()
173 end
174 
175 --- Set the Length.
176 --- @param int Length of the ah header as A bit integer.
177 function ahHeader:setLength(int)
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)
183  self.len = int
184 end
185 
186 --- Retrieve the Length.
187 --- @return Length as A bit integer.
188 function ahHeader:getLength()
189  return self.len
190 end
191 
192 --- Retrieve the Length as string.
193 --- @return Length as string.
194 function ahHeader:getLengthString()
195  return self:getLength()
196 end
197 
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)
206  args = args or {}
207  pre = pre or "ah"
208 
209  self:setSPI(args[pre .. "SPI"])
210  self:setSQN(args[pre .. "SQN"])
211  self:setIV(args[pre .. "IV"])
212  self:setICV(args[pre .. "ICV"])
213  self:setNextHeader(args[pre .. "NextHeader"])
214  self:setLength(args[pre .. "Length"])
215 end
216 
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)
222  pre = pre or "ah"
223 
224  local args = {}
225  args[pre .. "SPI"] = self:getSPI()
226  args[pre .. "SQN"] = self:getSQN()
227  args[pre .. "IV"] = self:getIV()
228  args[pre .. "ICV"] = self:getICV()
229  args[pre .. "NextHeader"] = self:getNextHeader()
230  args[pre .. "Length"] = self:getLength()
231 
232  return args
233 end
234 
235 --- Retrieve the values of all members.
236 --- @return Values in string format.
237 function ahHeader:getString()
238  return "AH spi " .. self:getSPIString() .. " sqn " .. self:getSQNString() .. " iv " .. self:getIVString() .. " icv " .. self:getICVString() .. " next_hdr " .. self:getNextHeader() .. " len " .. self:getLength()
239 end
240 
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)
246 function ahHeader:resolveNextHeader()
247  return nil
248  --TODO: return self:getNextHeader()
249 end
250 
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
253 --- See proto/ip4.setDefaultNamedArgs as an example
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
261 function ahHeader:setDefaultNamedArgs(pre, namedArgs, nextHeader, accumulatedLength)
262  return namedArgs
263 end
264 
265 ----------------------------------------------------------------------------------
266 ---- Packets
267 ----------------------------------------------------------------------------------
268 
269 -- Ah4 packets should not be shorter than 70 bytes (cf. x540 datasheet page 308: SECP field)
270 pkt.getAh4Packet = packetCreate("eth", "ip4", "ah")
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
274 
275 ------------------------------------------------------------------------
276 ---- Metatypes
277 ------------------------------------------------------------------------
278 
279 --ffi.metatype("union ipsec_iv", ipsecIV)
280 ffi.metatype("union ipsec_icv", ipsecICV)
281 ffi.metatype("struct ah_header", ahHeader)
282 
283 return ah
local ffi
low-level dpdk wrapper
Definition: dpdkc.lua:6
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.
Definition: udp.lua:23
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.
Definition: ethernet.lua:24
function ahHeader getIV()
Retrieve the IV.
local ip6
IP6 protocol constants.
Definition: ip6.lua:25
local pkt
Module for packets (rte_mbuf)
Definition: packet.lua:20
function ahHeader getSPIString()
Retrieve the SPI as string.
n
Create a new array of memory buffers (initialized to nil).
Definition: memory.lua:76
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.