2015-01-30 13:34:13 -05:00
|
|
|
import math
|
2015-01-30 12:07:26 -05:00
|
|
|
|
2015-02-26 03:41:47 -05:00
|
|
|
from misoclib.liteeth.common import *
|
|
|
|
from misoclib.liteeth.test.common import *
|
2015-01-30 12:07:26 -05:00
|
|
|
|
2015-02-26 03:41:47 -05:00
|
|
|
from misoclib.liteeth.test.model import mac
|
2015-01-30 12:07:26 -05:00
|
|
|
|
|
|
|
def print_ip(s):
|
|
|
|
print_with_prefix(s, "[IP]")
|
|
|
|
|
2015-02-04 13:03:49 -05:00
|
|
|
def carry_around_add(a, b):
|
|
|
|
c = a + b
|
|
|
|
return (c & 0xffff) + (c >> 16)
|
|
|
|
|
|
|
|
def checksum(msg):
|
|
|
|
s = 0
|
|
|
|
for i in range(0, len(msg), 2):
|
|
|
|
w = msg[i] + (msg[i+1] << 8)
|
|
|
|
s = carry_around_add(s, w)
|
|
|
|
return ~s & 0xffff
|
|
|
|
|
2015-01-30 12:07:26 -05:00
|
|
|
# IP model
|
|
|
|
class IPPacket(Packet):
|
|
|
|
def __init__(self, init=[]):
|
|
|
|
Packet.__init__(self, init)
|
|
|
|
|
2015-02-04 13:03:49 -05:00
|
|
|
def get_checksum(self):
|
|
|
|
return self[10] | (self[11] << 8)
|
|
|
|
|
|
|
|
def check_checksum(self):
|
|
|
|
return checksum(self[:ipv4_header_len]) == 0
|
|
|
|
|
2015-01-30 12:07:26 -05:00
|
|
|
def decode(self):
|
|
|
|
header = []
|
|
|
|
for byte in self[:ipv4_header_len]:
|
|
|
|
header.append(self.pop(0))
|
|
|
|
for k, v in sorted(ipv4_header.items()):
|
|
|
|
setattr(self, k, get_field_data(v, header))
|
|
|
|
|
|
|
|
def encode(self):
|
|
|
|
header = 0
|
|
|
|
for k, v in sorted(ipv4_header.items()):
|
|
|
|
value = merge_bytes(split_bytes(getattr(self, k), math.ceil(v.width/8)), "little")
|
|
|
|
header += (value << v.offset+(v.byte*8))
|
|
|
|
for d in split_bytes(header, ipv4_header_len):
|
|
|
|
self.insert(0, d)
|
|
|
|
|
2015-02-04 13:03:49 -05:00
|
|
|
def insert_checksum(self):
|
|
|
|
self[10] = 0
|
|
|
|
self[11] = 0
|
|
|
|
c = checksum(self[:ipv4_header_len])
|
|
|
|
self[10] = c & 0xff
|
|
|
|
self[11] = (c >> 8) & 0xff
|
|
|
|
|
2015-01-30 12:07:26 -05:00
|
|
|
def __repr__(self):
|
|
|
|
r = "--------\n"
|
|
|
|
for k in sorted(ipv4_header.keys()):
|
2015-02-11 05:28:15 -05:00
|
|
|
r += k + " : 0x{:0x}\n".format(getattr(self,k))
|
2015-01-30 12:07:26 -05:00
|
|
|
r += "payload: "
|
|
|
|
for d in self:
|
2015-02-11 05:28:15 -05:00
|
|
|
r += "{:02x}".format(d)
|
2015-01-30 12:07:26 -05:00
|
|
|
return r
|
|
|
|
|
|
|
|
class IP(Module):
|
2015-01-30 13:14:05 -05:00
|
|
|
def __init__(self, mac, mac_address, ip_address, debug=False, loopback=False):
|
2015-01-30 12:07:26 -05:00
|
|
|
self.mac = mac
|
|
|
|
self.mac_address = mac_address
|
|
|
|
self.ip_address = ip_address
|
|
|
|
self.debug = debug
|
2015-01-30 13:14:05 -05:00
|
|
|
self.loopback = loopback
|
2015-01-30 12:07:26 -05:00
|
|
|
self.rx_packet = IPPacket()
|
|
|
|
self.table = {}
|
|
|
|
self.request_pending = False
|
2015-02-06 06:22:24 -05:00
|
|
|
|
2015-02-04 14:30:17 -05:00
|
|
|
self.udp_callback = None
|
2015-02-06 03:08:05 -05:00
|
|
|
self.icmp_callback = None
|
2015-01-30 12:07:26 -05:00
|
|
|
|
|
|
|
self.mac.set_ip_callback(self.callback)
|
|
|
|
|
2015-02-04 14:30:17 -05:00
|
|
|
def set_udp_callback(self, callback):
|
|
|
|
self.udp_callback = callback
|
|
|
|
|
2015-02-06 03:08:05 -05:00
|
|
|
def set_icmp_callback(self, callback):
|
|
|
|
self.icmp_callback = callback
|
|
|
|
|
2015-01-30 12:07:26 -05:00
|
|
|
def send(self, packet):
|
|
|
|
packet.encode()
|
2015-02-04 13:03:49 -05:00
|
|
|
packet.insert_checksum()
|
2015-01-30 12:07:26 -05:00
|
|
|
if self.debug:
|
|
|
|
print_ip(">>>>>>>>")
|
|
|
|
print_ip(packet)
|
|
|
|
mac_packet = mac.MACPacket(packet)
|
2015-02-04 16:51:11 -05:00
|
|
|
mac_packet.target_mac = 0x12345678abcd # XXX
|
|
|
|
mac_packet.sender_mac = self.mac_address
|
2015-01-30 12:07:26 -05:00
|
|
|
mac_packet.ethernet_type = ethernet_type_ip
|
|
|
|
self.mac.send(mac_packet)
|
|
|
|
|
|
|
|
def callback(self, packet):
|
|
|
|
packet = IPPacket(packet)
|
2015-02-04 13:03:49 -05:00
|
|
|
if not packet.check_checksum():
|
|
|
|
received = packet.get_checksum()
|
|
|
|
packet.insert_checksum()
|
|
|
|
expected = packet.get_checksum()
|
2015-02-11 05:28:15 -05:00
|
|
|
raise ValueError("Checksum error received {:04x} / expected {:04x}".format(received, expected))
|
2015-01-30 12:07:26 -05:00
|
|
|
packet.decode()
|
|
|
|
if self.debug:
|
|
|
|
print_ip("<<<<<<<<")
|
|
|
|
print_ip(packet)
|
2015-01-30 13:14:05 -05:00
|
|
|
if self.loopback:
|
|
|
|
self.send(packet)
|
|
|
|
else:
|
2015-02-04 14:30:17 -05:00
|
|
|
if packet.version != 0x4:
|
|
|
|
raise ValueError
|
|
|
|
if packet.ihl != 0x5:
|
|
|
|
raise ValueError
|
2015-01-30 13:14:05 -05:00
|
|
|
self.process(packet)
|
2015-01-30 12:07:26 -05:00
|
|
|
|
|
|
|
def process(self, packet):
|
2015-02-04 14:30:17 -05:00
|
|
|
if packet.protocol == udp_protocol:
|
|
|
|
if self.udp_callback is not None:
|
|
|
|
self.udp_callback(packet)
|
2015-02-06 03:08:05 -05:00
|
|
|
elif packet.protocol == icmp_protocol:
|
|
|
|
if self.icmp_callback is not None:
|
|
|
|
self.icmp_callback(packet)
|
2015-02-04 13:03:49 -05:00
|
|
|
|
2015-01-30 12:07:26 -05:00
|
|
|
if __name__ == "__main__":
|
2015-02-26 03:41:47 -05:00
|
|
|
from misoclib.liteeth.test.model.dumps import *
|
|
|
|
from misoclib.liteeth.test.model.mac import *
|
2015-01-30 12:07:26 -05:00
|
|
|
errors = 0
|
2015-02-04 14:30:17 -05:00
|
|
|
# UDP packet
|
2015-01-30 12:07:26 -05:00
|
|
|
packet = MACPacket(udp)
|
|
|
|
packet.decode_remove_header()
|
|
|
|
#print(packet)
|
|
|
|
packet = IPPacket(packet)
|
|
|
|
# check decoding
|
2015-02-04 13:03:49 -05:00
|
|
|
errors += not packet.check_checksum()
|
2015-01-30 12:07:26 -05:00
|
|
|
packet.decode()
|
2015-01-30 13:34:13 -05:00
|
|
|
#print(packet)
|
2015-01-30 12:07:26 -05:00
|
|
|
errors += verify_packet(packet, {})
|
|
|
|
# check encoding
|
|
|
|
packet.encode()
|
2015-02-04 13:03:49 -05:00
|
|
|
packet.insert_checksum()
|
|
|
|
errors += not packet.check_checksum()
|
2015-01-30 12:07:26 -05:00
|
|
|
packet.decode()
|
|
|
|
#print(packet)
|
|
|
|
errors += verify_packet(packet, {})
|
|
|
|
|
|
|
|
print("ip errors " + str(errors))
|