1 ------------------------------------------------------------------------
3 --- @brief Transmission control protocol (TCP) utility.
4 --- Utility functions for the tcp_header struct
5 --- defined in \ref headers.lua . \
n
8 --- - TCP header utility
9 --- - Definition of TCP packets
10 ------------------------------------------------------------------------
13 local
pkt = require "packet"
18 local ntoh, hton = ntoh, hton
19 local ntoh16, hton16 = ntoh16, hton16
20 local
bor,
band,
bnot, rshift, lshift= bit.bor, bit.band, bit.bnot, bit.rshift, bit.lshift
21 local istype =
ffi.istype
22 local format =
string.format
25 ------------------------------------------------------------------------------
27 ------------------------------------------------------------------------------
30 ------------------------------------------------------------------------------
32 ------------------------------------------------------------------------------
34 --- Module for tcp_header struct (see \ref headers.lua).
38 --- Set the source port.
39 --- @param
int Port as 16 bit integer.
42 self.src = hton16(
int)
45 --- Retrieve the source port.
46 --- @return Port as 16 bit integer.
48 return hton16(self.src)
51 --- Retrieve the source port.
52 --- @return Port in
string format.
57 --- Set the destination port.
58 --- @param
int Port as 16 bit integer.
61 self.dst = hton16(
int)
64 --- Retrieve the destination port.
65 --- @return Port as 16 bit integer.
67 return hton16(self.dst)
70 --- Retrieve the destination port.
71 --- @return Port in
string format.
76 --- Set the sequence number.
77 --- @param
int Sequence number as 8 bit integer.
83 --- Retrieve the sequence number.
84 --- @return Seq number as 8 bit integer.
89 --- Retrieve the sequence number.
90 --- @return Sequence number in
string format.
95 --- Set the acknowledgement number.
96 --- @param
int Ack number as 8 bit integer.
102 --- Retrieve the acknowledgement number.
103 --- @return Seq number as 8 bit integer.
105 return hton(self.ack)
108 --- Retrieve the acknowledgement number.
109 --- @return Ack number in
string format.
114 --- Set the data offset.
115 --- @param
int Offset as 4 bit integer. Header size is counted in 32 bit words (min. 5 (no options), max. 15)
118 int =
band(lshift(
int, 4), 0xf0) --
fill to 8 bits
121 old =
band(old, 0x0f) -- remove old value
123 self.offset =
bor(old,
int)
126 --- Retrieve the data offset.
127 --- @return Offset as 4 bit integer.
129 return
band(rshift(self.offset, 4), 0x0f)
132 --- Retrieve the data offset.
133 --- @return Offset in
string format.
138 --- Set the reserved field.
139 --- @param
int Reserved field as 6 bit integer.
146 -- first, offset field
147 off =
band(rshift(
int, 2), 0x0f) --
fill to 8 bits (4 highest to 4 lowest bits)
150 old =
band(old, 0xf0) -- remove old value
152 self.offset =
bor(old, off)
154 -- secondly, flags field
155 fla = lshift(
int, 6) --
fill to 8 bits (2 lowest to 2 highest bits)
158 old =
band(old, 0x3f) -- remove old values
160 self.flags =
bor(old, fla)
163 --- Retrieve the reserved field.
164 --- @return Reserved field as 6 bit integer.
166 res = lshift(
band(self.offset, 0x0f), 2) -- 4 lowest from offset to 4 highest from reserved
167 res =
bor(res, rshift(self.flags, 6)) -- 2 highest from flags to 2 lowest from reserved
171 --- Retrieve the reserved field.
172 --- @return Reserved field in
string format.
178 --- @param
int Flags as 6 bit integer.
179 --- @todo TODO RFC 3168 specifies
new CWR and ECE flags (reserved reduced to 4 bit)
183 int =
band(
int, 0x3f) --
fill to 8 bits
186 old =
band(old, 0xc0) -- remove old values
188 self.flags =
bor(old,
int)
191 --- Retrieve the flags.
192 --- @return Flags as 6 bit integer.
194 return
band(self.flags, 0x3f)
197 --- Retrieve the flags.
198 --- @return Flags in
string format.
200 return format("0x%02x", self:
getFlags())
203 --- Set the Urg flag.
205 self.flags =
bor(self.flags, 0x20)
208 --- Unset the Urg flag.
210 self.flags =
band(self.flags, 0xdf)
213 --- Retrieve the Urg flag.
214 --- @return Flag as 1 bit integer.
216 return rshift(
band(self.flags, 0x20), 5)
219 --- Retrieve the Urg flag.
220 --- @return Flag in
string format.
222 if self:
getUrg() == 1 then
229 --- Set the Ack flag.
231 self.flags =
bor(self.flags, 0x10)
234 --- Unset the Ack flag.
236 self.flags =
band(self.flags, 0xef)
239 --- Retrieve the Ack flag.
240 --- @return Flag as 1 bit integer.
242 return rshift(
band(self.flags, 0x10), 4)
245 --- Retrieve the Ack flag.
246 --- @return Flag in
string format.
248 if self:
getAck() == 1 then
255 --- Set the Psh flag.
257 self.flags =
bor(self.flags, 0x08)
260 --- Unset the Psh flag.
262 self.flags =
band(self.flags, 0xf7)
265 --- Retrieve the Psh flag.
266 --- @return Flag as 1 bit integer.
268 return rshift(
band(self.flags, 0x08), 3)
271 --- Retrieve the Psh flag.
272 --- @return Flag in
string format.
274 if self:
getPsh() == 1 then
281 --- Set the Rst flag.
283 self.flags =
bor(self.flags, 0x04)
286 --- Unset the Rst flag.
288 self.flags =
band(self.flags, 0xfb)
291 --- Retrieve the Rst flag.
292 --- @return Flag as 1 bit integer.
294 return rshift(
band(self.flags, 0x04), 2)
297 --- Retrieve the Rst flag.
298 --- @return Flag in
string format.
300 if self:
getRst() == 1 then
307 --- Set the Syn flag.
309 self.flags =
bor(self.flags, 0x02)
312 --- Unset the Syn flag.
314 self.flags =
band(self.flags, 0xfd)
317 --- Retrieve the Syn flag.
318 --- @return Flag as 1 bit integer.
320 return rshift(
band(self.flags, 0x02), 1)
323 --- Retrieve the Syn flag.
324 --- @return Flag in
string format.
326 if self:
getSyn() == 1 then
333 --- Set the Fin flag.
335 self.flags =
bor(self.flags, 0x01)
338 --- Unset the Fin flag.
340 self.flags =
band(self.flags, 0xfe)
343 --- Retrieve the Fin flag.
344 --- @return Flag as 1 bit integer.
346 return
band(self.flags, 0x01)
349 --- Retrieve the Fin flag.
350 --- @return Flag in
string format.
352 if self:
getFin() == 1 then
359 --- Set the window field.
360 --- @param
int Window as 16 bit integer.
363 self.window = hton16(
int)
366 --- Retrieve the window field.
367 --- @return Window as 16 bit integer.
369 return hton16(self.window)
372 --- Retrieve the window field.
373 --- @return Window in
string format.
379 --- @param
int Checksum as 16 bit integer.
382 self.cs = hton16(
int)
386 --- @param len Number of bytes to calculate the
checksum over.
392 --- @return Checksum as 16 bit integer.
394 return hton16(self.cs)
398 --- @return Checksum in
string format.
403 --- Set the urgent pointer.
404 --- @param
int Urgent pointer as 16 bit integer.
407 self.urg = hton16(
int)
410 --- Retrieve the urgent pointer.
411 --- @return Urgent pointer as 16 bit integer.
413 return hton16(self.urg)
416 --- Retrieve the urgent pointer.
417 --- @return Urgent pointer in
string format.
422 -- TODO how do we want to handle options (problem is tcp header variable length array of uint8[] followed by payload variable length array (uint8[]))
428 --- Set all members of the
ip header.
429 --- Per default, all members are
set to default values specified in the respective
set function.
430 --- Optional named arguments can be used to
set a member to a user-provided value.
431 --- @param args Table of named arguments. Available arguments: Src, Dst, SeqNumber, AckNumber, DataOffset, Reserved, Flags, Urg, Ack, Psh, Rst, Syn, Fin, Window, Checksum, UrgentPointer
432 --- @param pre prefix for namedArgs. Default 'tcp'.
434 ---
fill() --- only default values
435 ---
fill{ tcpSrc=1234, ipTTL=100 } --- all members are
set to
default values with the exception of tcpSrc
448 if args[pre ..
"Urg"] and args[pre ..
"Urg"] ~= 0 then
451 if args[pre .. "Ack"] and args[pre .. "Ack"] ~= 0 then
454 if args[pre .. "Psh"] and args[pre .. "Psh"] ~= 0 then
457 if args[pre .. "Rst"] and args[pre .. "Rst"] ~= 0 then
460 if args[pre .. "Syn"] and args[pre .. "Syn"] ~= 0 then
463 if args[pre .. "Fin"] and args[pre .. "Fin"] ~= 0 then
471 --- Retrieve the values of all members.
472 --- @return Table of named arguments. For a list of arguments see "See also".
484 args[pre .. "Flags"] = self:
getFlags()
485 args[pre .. "Urg"] = self:
getUrg()
486 args[pre .. "Ack"] = self:
getAck()
487 args[pre .. "Psh"] = self:
getPsh()
488 args[pre .. "Rst"] = self:
getRst()
489 args[pre .. "Syn"] = self:
getSyn()
490 args[pre .. "Fin"] = self:
getFin()
498 --- Retrieve the values of all members.
499 --- @return Values in
string format.
503 .. " seq
# " .. self:getSeqNumberString()
519 --- Resolve which header comes after this one (in a packet).
520 --- For instance: in tcp/
udp based on the ports.
521 --- This function must exist and is only used when
get/
dump is executed on
522 --- an unknown (mbuf not yet casted to e.g. tcpv6 packet) packet (mbuf)
523 --- @return String next header (e.g. '
udp', '
icmp', nil)
528 --- Change the default values for namedArguments (for
fill/
get).
529 --- This can be used to for instance calculate a length value based on the total packet length.
531 --- This function must exist and is only used by packet.
fill.
532 --- @param pre The prefix used for the namedArgs, e.g. 'tcp'
533 --- @param namedArgs Table of named arguments (see See Also)
534 --- @param nextHeader The header following after this header in a packet
535 --- @param accumulatedLength The so far accumulated length for previous headers in a packet
536 --- @return Table of namedArgs
543 ----------------------------------------------------------------------------------
545 ----------------------------------------------------------------------------------
547 --- Cast the packet to a Tcp (IP4) packet
549 --- Cast the packet to a Tcp (IP6) packet
551 --- Cast the packet to a Tcp packet, either using IP4 (nil/true) or IP6 (false), depending on the passed
boolean.
553 ip4 = ip4 == nil or ip4
561 ------------------------------------------------------------------------------------
563 ------------------------------------------------------------------------------------
function tcpHeader getAckString()
Retrieve the Ack flag.
function tcpHeader resolveNextHeader()
Resolve which header comes after this one (in a packet).
local ffi
low-level dpdk wrapper
function tcpHeader setDstPort(int)
Set the destination port.
function tcpHeader setSyn()
Set the Syn flag.
function checksum(data, len)
Calculate a 16 bit checksum.
function tcpHeader setUrgentPointer(int)
Set the urgent pointer.
function tcpHeader setAck()
Set the Ack flag.
function tcpHeader unsetFin()
Unset the Fin flag.
function tcpHeader unsetUrg()
Unset the Urg flag.
function packetCreate(...)
Create struct and functions for a new packet.
function tcpHeader setReserved(int)
Set the reserved field.
function tcpHeader getUrgentPointer()
Retrieve the urgent pointer.
function mod band(mask1, mask2, result)
Bitwise and.
function tcpHeader setPsh()
Set the Psh flag.
function tcpHeader get(pre)
Retrieve the values of all members.
function pkt dump(bytes)
Dumps the packet data cast to the best fitting packet struct.
function tcpHeader getFin()
Retrieve the Fin flag.
function tcpHeader unsetAck()
Unset the Ack flag.
function tcpHeader getAck()
Retrieve the Ack flag.
function tcpHeader unsetRst()
Unset the Rst flag.
function tcpHeader setWindow(int)
Set the window field.
function ipsecICV set(icv)
Set the IPsec ICV.
function tcpHeader getReservedString()
Retrieve the reserved field.
function tcpHeader setFin()
Set the Fin flag.
function tcpHeader getDataOffset()
Retrieve the data offset.
local udp
Udp protocol constants.
function tcpHeader getChecksumString()
Retrieve the checksum.
function tcpHeader getSrcPortString()
Retrieve the source port.
local tcpHeader
Module for tcp_header struct (see headers.lua).
function tcpHeader getFlagsString()
Retrieve the flags.
function tcpHeader getDstPort()
Retrieve the destination port.
function mod bor(mask1, mask2, result)
Bitwise or.
function tcpHeader setSrcPort(int)
Set the source port.
function tcpHeader getDataOffsetString()
Retrieve the data offset.
function tcpHeader getRst()
Retrieve the Rst flag.
function tcpHeader getUrgentPointerString()
Retrieve the urgent pointer.
function tcpHeader getSeqNumberString()
Retrieve the sequence number.
function tcpHeader getWindowString()
Retrieve the window field.
function tcpHeader getFlags()
Retrieve the flags.
function tcpHeader getDstPortString()
Retrieve the destination port.
function tcpHeader getSeqNumber()
Retrieve the sequence number.
function tcpHeader getSrcPort()
Retrieve the source port.
function tcpHeader setSeqNumber(int)
Set the sequence number.
function tcpHeader getSyn()
Retrieve the Syn flag.
function tcpHeader getPshString()
Retrieve the Psh flag.
function tcpHeader getReserved()
Retrieve the reserved field.
function tcpHeader fill(args, pre)
Set all members of the ip header.
function tcpHeader unsetSyn()
Unset the Syn flag.
function tcpHeader setDefaultNamedArgs(pre, namedArgs, nextHeader, accumulatedLength)
Change the default values for namedArguments (for fill/get).
function tcpHeader getString()
Retrieve the values of all members.
function tcpHeader setRst()
Set the Rst flag.
pkt getTcp6Packet
Cast the packet to a Tcp (IP6) packet.
pkt getTcp4Packet
Cast the packet to a Tcp (IP4) packet.
function tcpHeader getUrgString()
Retrieve the Urg flag.
local eth
Ethernet protocol constants.
local ip6
IP6 protocol constants.
function tcpHeader setUrg()
Set the Urg flag.
function tcpHeader setAckNumber(int)
Set the acknowledgement number.
function tcpHeader getAckNumberString()
Retrieve the acknowledgement number.
local ip
IP4 protocol constants.
pkt getTcpPacket
Cast the packet to a Tcp packet, either using IP4 (nil/true) or IP6 (false), depending on the passed ...
function tcpHeader getPsh()
Retrieve the Psh flag.
local pkt
Module for packets (rte_mbuf)
function mod bnot(mask, result)
Bitwise not.
function tcpHeader getFinString()
Retrieve the Fin flag.
n
Create a new array of memory buffers (initialized to nil).
function tcpHeader setDataOffset(int)
Set the data offset.
function tcpHeader setFlags(int)
Set the flags.
function tcpHeader getSynString()
Retrieve the Syn flag.
function tcpHeader getAckNumber()
Retrieve the acknowledgement number.
function tcpHeader calculateChecksum(len)
Calculate the checksum.
function tcpHeader getRstString()
Retrieve the Rst flag.
local icmp
Icmp4 protocol constants.
function tcpHeader setChecksum(int)
Set the checksum.
function tcpHeader getChecksum()
Retrieve the checksum.
function tcpHeader unsetPsh()
Unset the Psh flag.
function tcpHeader getUrg()
Retrieve the Urg flag.
function tcpHeader getWindow()
Retrieve the window field.