begin implementing diff based checksum in p4

This commit is contained in:
Nico Schottelius 2019-07-06 20:57:11 +02:00
commit a69c4060e2
5 changed files with 982 additions and 143 deletions

View file

@ -8,14 +8,44 @@ import struct
# stolen from scapy (little endian system)
def checksum_scapy(pkt):
# Even the length
if len(pkt) % 2 == 1:
pkt += b"\0"
# array: create an array of 16 bit values from the input
# and then sum it up -> this might be sligthly/much higher
# than 16 bit (additions!)
s = sum(array.array("H", pkt))
# add the (right shift 16) and the 16 right bits
# basically: assuming 32 bit (?): add the two 16 bit "words"
# together
# This might still exceed 16 bit!
s = (s >> 16) + (s & 0xffff)
# right shift 16 -> zero least significant bits,
# and add the upper bits to it
# So now we add anything that was left over from before
s += s >> 16
# 2 complement -- this is the only important part here ???
s = ~s
# right shift 8 bit -> maximum 8 bit are set (above code)
# then we and it with 1's -- ??? WTF??? -> 8 bit filter!
# -> first part is extracting 8 highest bits
#
# then we left shift the original value by 8 (???)
# and then we OR all of that
# -> second part is unclear
#
# Then we mask it again with 16 bit 1's -> cut off stuff
return (((s>>8)&0xff)|s<<8) & 0xffff
# a) Compare for TCP, UDP -> IPv6 does not have checksum!
# 1. convert to array of "bytes"
# 2. import into an array
@ -32,9 +62,9 @@ def checksum_scapy(pkt):
def sum_for_udp(packet):
sums = ""
sums += struct.pack("H", packet[UDP].sport)
sums += struct.pack("H", packet[UDP].dport)
sums += struct.pack("H", packet[UDP].len)
sums += struct.pack("H", packet[UDP].sport) # 16 bit
sums += struct.pack("H", packet[UDP].dport) # 16 bit
sums += struct.pack("H", packet[UDP].len) # 16 bit
return sums