2015-01-29 13:17:45 -05:00
|
|
|
import math, binascii
|
|
|
|
|
|
|
|
from liteeth.common import *
|
|
|
|
from liteeth.mac.common import *
|
|
|
|
from liteeth.test.common import *
|
|
|
|
|
2015-01-29 18:03:16 -05:00
|
|
|
from liteeth.test.model import mac
|
|
|
|
|
2015-01-29 13:17:45 -05:00
|
|
|
def print_arp(s):
|
|
|
|
print_with_prefix(s, "[ARP]")
|
|
|
|
|
|
|
|
preamble = split_bytes(eth_preamble, 8)
|
|
|
|
|
|
|
|
# ARP model
|
|
|
|
class ARPPacket(Packet):
|
|
|
|
def __init__(self, init=[]):
|
|
|
|
Packet.__init__(self, init)
|
|
|
|
|
|
|
|
def decode(self):
|
|
|
|
header = []
|
|
|
|
for byte in self[:arp_header_len]:
|
|
|
|
header.append(self.pop(0))
|
|
|
|
for k, v in sorted(arp_header.items()):
|
|
|
|
setattr(self, k, get_field_data(v, header))
|
|
|
|
|
|
|
|
def encode(self):
|
|
|
|
header = 0
|
|
|
|
for k, v in sorted(arp_header.items()):
|
2015-01-29 18:02:11 -05:00
|
|
|
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, arp_header_len):
|
2015-01-29 13:17:45 -05:00
|
|
|
self.insert(0, d)
|
|
|
|
|
|
|
|
def __repr__(self):
|
|
|
|
r = "--------\n"
|
|
|
|
for k in sorted(arp_header.keys()):
|
|
|
|
r += k + " : 0x%x" %getattr(self,k) + "\n"
|
|
|
|
r += "payload: "
|
|
|
|
for d in self:
|
|
|
|
r += "%02x" %d
|
|
|
|
return r
|
|
|
|
|
|
|
|
class ARP(Module):
|
2015-01-29 18:03:16 -05:00
|
|
|
def __init__(self, mac, mac_address, ip_address, debug=False):
|
2015-01-29 13:17:45 -05:00
|
|
|
self.mac = mac
|
2015-01-29 18:03:16 -05:00
|
|
|
self.mac_address = mac_address
|
2015-01-29 13:17:45 -05:00
|
|
|
self.ip_address = ip_address
|
|
|
|
self.debug = debug
|
|
|
|
self.tx_packets = []
|
|
|
|
self.tx_packet = ARPPacket()
|
|
|
|
self.rx_packet = ARPPacket()
|
|
|
|
self.table = {}
|
|
|
|
self.request_pending = False
|
|
|
|
|
|
|
|
self.mac.set_arp_callback(self.callback)
|
|
|
|
|
|
|
|
def send(self, packet):
|
2015-01-29 18:03:16 -05:00
|
|
|
packet.encode()
|
2015-01-29 13:17:45 -05:00
|
|
|
if self.debug:
|
|
|
|
print_arp(">>>>>>>>")
|
|
|
|
print_arp(packet)
|
2015-01-29 18:03:16 -05:00
|
|
|
mac_packet = mac.MACPacket(packet)
|
|
|
|
mac_packet.destination_mac_address = packet.destination_mac_address
|
|
|
|
mac_packet.source_mac_address = packet.source_mac_address
|
|
|
|
mac_packet.ethernet_type = ethernet_type_arp
|
|
|
|
self.mac.send(mac_packet)
|
2015-01-29 13:17:45 -05:00
|
|
|
|
|
|
|
def callback(self, packet):
|
2015-01-29 18:03:16 -05:00
|
|
|
packet = ARPPacket(packet)
|
2015-01-29 13:17:45 -05:00
|
|
|
packet.decode()
|
|
|
|
if self.debug:
|
|
|
|
print_arp("<<<<<<<<")
|
|
|
|
print_arp(packet)
|
2015-01-29 18:03:16 -05:00
|
|
|
self.process(packet)
|
2015-01-29 13:17:45 -05:00
|
|
|
|
|
|
|
def process(self, packet):
|
2015-01-29 18:03:16 -05:00
|
|
|
if len(packet) != arp_packet_length-arp_header_len:
|
2015-01-29 13:17:45 -05:00
|
|
|
raise ValueError
|
|
|
|
if packet.hardware_type != arp_hwtype_ethernet:
|
|
|
|
raise ValueError
|
|
|
|
if packet.protocol_type != arp_proto_ip:
|
|
|
|
raise ValueError
|
|
|
|
if packet.hardware_address_length != 6:
|
|
|
|
raise ValueError
|
|
|
|
if packet.protocol_address_length != 4:
|
|
|
|
raise ValueError
|
|
|
|
if packet.operation == arp_opcode_request:
|
|
|
|
self.process_request(packet)
|
|
|
|
elif packet.operation == arp_opcode_reply:
|
|
|
|
self.process_reply(packet)
|
|
|
|
|
|
|
|
def process_request(self, request):
|
2015-01-29 18:02:11 -05:00
|
|
|
if request.destination_ip_address == self.ip_address:
|
2015-01-29 18:03:16 -05:00
|
|
|
reply = ARPPacket([0]*(arp_packet_length-arp_header_len))
|
2015-01-29 13:17:45 -05:00
|
|
|
reply.hardware_type = arp_hwtype_ethernet
|
|
|
|
reply.protocol_type = arp_proto_ip
|
2015-01-29 18:03:16 -05:00
|
|
|
reply.operation = arp_opcode_reply
|
2015-01-29 13:17:45 -05:00
|
|
|
reply.hardware_address_length = 6
|
|
|
|
reply.protocol_address_length = 4
|
|
|
|
reply.source_mac_address = self.mac_address
|
|
|
|
reply.source_ip_address = self.ip_address
|
|
|
|
reply.destination_mac_address = request.source_mac_address
|
|
|
|
reply.destination_ip_address = request.source_ip_address
|
|
|
|
self.send(reply)
|
|
|
|
|
|
|
|
def process_reply(self, reply):
|
|
|
|
self.table[reply.source_ip_address] = reply.source_mac_address
|
|
|
|
|
|
|
|
def request(self, ip_address):
|
2015-01-29 18:03:16 -05:00
|
|
|
request = ARPPacket([0]*(arp_packet_length-arp_header_len))
|
2015-01-29 13:17:45 -05:00
|
|
|
request.hardware_type = arp_hwtype_ethernet
|
|
|
|
request.protocol_type = arp_proto_ip
|
2015-01-29 18:03:16 -05:00
|
|
|
request.operation = arp_opcode_request
|
2015-01-29 13:17:45 -05:00
|
|
|
request.hardware_address_length = 6
|
|
|
|
request.protocol_address_length = 4
|
|
|
|
request.source_mac_address = self.mac_address
|
|
|
|
request.source_ip_address = self.ip_address
|
|
|
|
request.destination_mac_address = 0xffffffffffff
|
|
|
|
request.destination_ip_address = ip_address
|
2015-01-29 18:02:11 -05:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
from liteeth.test.model.dumps import *
|
|
|
|
from liteeth.test.model.mac import *
|
|
|
|
errors = 0
|
|
|
|
# ARP request
|
|
|
|
packet = MACPacket(arp_request)
|
|
|
|
packet.decode_remove_header()
|
|
|
|
packet = ARPPacket(packet)
|
|
|
|
# check decoding
|
|
|
|
packet.decode()
|
|
|
|
#print(packet)
|
|
|
|
errors += verify_packet(packet, arp_request_infos)
|
|
|
|
# check encoding
|
|
|
|
packet.encode()
|
|
|
|
packet.decode()
|
|
|
|
#print(packet)
|
|
|
|
errors += verify_packet(packet, arp_request_infos)
|
|
|
|
|
|
|
|
# ARP Reply
|
|
|
|
packet = MACPacket(arp_reply)
|
|
|
|
packet.decode_remove_header()
|
|
|
|
packet = ARPPacket(packet)
|
|
|
|
# check decoding
|
|
|
|
packet.decode()
|
|
|
|
#print(packet)
|
|
|
|
errors += verify_packet(packet, arp_reply_infos)
|
|
|
|
# check encoding
|
|
|
|
packet.encode()
|
|
|
|
packet.decode()
|
|
|
|
#print(packet)
|
|
|
|
errors += verify_packet(packet, arp_reply_infos)
|
|
|
|
|
|
|
|
print("arp errors " + str(errors))
|