MoonGen
 All Files Functions Variables Pages
esp.lua
Go to the documentation of this file.
1 ------------------------------------------------------------------------
2 --- @file esp.lua
3 --- @brief ESP utility.
4 --- Utility functions for the esp_header structs
5 --- defined in \ref headers.lua . \n
6 --- Includes:
7 --- - ESP constants
8 --- - IPsec IV
9 --- - ESP header utility
10 --- - Definition of esp packets
11 ------------------------------------------------------------------------
12 
13 local ffi = require "ffi"
14 local pkt = require "packet"
15 local math = require "math"
16 
17 require "headers"
18 
19 
20 ---------------------------------------------------------------------------
21 ---- esp constants
22 ---------------------------------------------------------------------------
23 
24 local esp = {}
25 
26 -------------------------------------------------------------------------------------
27 ---- IPsec IV
28 -------------------------------------------------------------------------------------
29 
30 local ipsecIV = {}
31 ipsecIV.__index = ipsecIV
32 local ipsecIVType = ffi.typeof("union ipsec_iv")
33 
34 --- Set the 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)
40 
41  local iv = iv or random_iv
42  self.uint32[0] = hton(iv.uint32[1])
43  self.uint32[1] = hton(iv.uint32[0])
44 end
45 
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])
52  return iv
53 end
54 
55 --- Get the IPsec IV as string.
56 --- @param iv IPsec IV in string format.
57 function ipsecIV:getString(doByteSwap)
58  doByteSwap = doByteSwap or false
59  if doByteSwap then
60  self = self:get()
61  end
62 
63  return ("0x%08x%08x"):format(self.uint32[1], self.uint32[0])
64 end
65 
66 ---------------------------------------------------------------------------
67 ---- esp header
68 ---------------------------------------------------------------------------
69 
70 local espHeader = {}
71 espHeader.__index = espHeader
72 
73 --- Set the SPI.
74 --- @param int SPI of the esp header as A bit integer.
75 function espHeader:setSPI(int)
76  int = int or 0
77  self.spi = hton(int)
78 end
79 
80 --- Retrieve the SPI.
81 --- @return SPI as A bit integer.
82 function espHeader:getSPI()
83  return hton(self.spi)
84 end
85 
86 --- Retrieve the SPI as string.
87 --- @return SPI as string.
88 function espHeader:getSPIString()
89  return ("0x%08x"):format(self.spi)
90 end
91 
92 --- Set the SQN.
93 --- @param int SQN of the esp header as A bit integer.
94 function espHeader:setSQN(int)
95  int = int or 0
96  self.sqn = hton(int)
97 end
98 
99 --- Retrieve the SQN.
100 --- @return SQN as A bit integer.
101 function espHeader:getSQN()
102  return hton(self.sqn)
103 end
104 
105 --- Retrieve the SQN as string.
106 --- @return SQN as string.
107 function espHeader:getSQNString()
108  return self:getSQN()
109 end
110 
111 --- Set the IV.
112 --- @param int IV of the esp header as 'union ipsec_iv'.
113 function espHeader:setIV(iv)
114  self.iv:set(iv)
115 end
116 
117 --- Retrieve the IV.
118 --- @return SPI as 'union ipsec_iv'.
119 function espHeader:getIV()
120  return self.iv:get()
121 end
122 
123 --- Retrieve the IV as string.
124 --- @return IV as string.
125 function espHeader:getIVString()
126  return self.iv:getString(true)
127 end
128 
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)
137  args = args or {}
138  pre = pre or "esp"
139 
140  self:setSPI(args[pre .. "SPI"])
141  self:setSQN(args[pre .. "SQN"])
142  self:setIV(args[pre .. "IV"])
143 end
144 
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)
150  pre = pre or "esp"
151 
152  local args = {}
153  args[pre .. "SPI"] = self:getSPI()
154  args[pre .. "SQN"] = self:getSQN()
155  args[pre .. "IV"] = self:getIV()
156 
157  return args
158 end
159 
160 --- Retrieve the values of all members.
161 --- @return Values in string format.
162 function espHeader:getString()
163  --TODO: add data from ESP trailer
164  return "ESP spi " .. self:getSPIString() .. " sqn " .. self:getSQNString() .. " iv " .. self:getIVString()
165 end
166 
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)
172 function espHeader:resolveNextHeader()
173  --TODO: next_header field is in ESP trailer
174  return nil
175 end
176 
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
179 --- See proto/ip4.setDefaultNamedArgs as an example
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
187 function espHeader:setDefaultNamedArgs(pre, namedArgs, nextHeader, accumulatedLength)
188  return namedArgs
189 end
190 
191 ----------------------------------------------------------------------------------
192 ---- Packets
193 ----------------------------------------------------------------------------------
194 
195 -- Esp4 packets should not be shorter than 70 bytes (cf. x540 datasheet page 308: SECP field)
196 pkt.getEsp4Packet = packetCreate("eth", "ip4", "esp")
197 -- Esp6 packets should not be shorter than 90 bytes (cf. x540 datasheet page 308: SECP field)
198 pkt.getEsp6Packet = packetCreate("eth", "ip6", "esp")
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
200 
201 ------------------------------------------------------------------------
202 ---- Metatypes
203 ------------------------------------------------------------------------
204 
205 ffi.metatype("union ipsec_iv", ipsecIV)
206 ffi.metatype("struct esp_header", espHeader)
207 
208 
209 return esp
function espHeader getIVString()
Retrieve the IV as string.
local ffi
low-level dpdk wrapper
Definition: dpdkc.lua:6
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.
function mod new(n)
local udp
Udp protocol constants.
Definition: udp.lua:23
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.
Definition: ethernet.lua:24
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.
Definition: ip6.lua:25
function espHeader setSQN(int)
Set the SQN.
local pkt
Module for packets (rte_mbuf)
Definition: packet.lua:20
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).
Definition: memory.lua:76
function espHeader setSPI(int)
Set the SPI.
function espHeader getIV()
Retrieve the IV.