2015-01-30 13:34:13 -05:00
|
|
|
import math
|
2015-01-29 13:17:45 -05:00
|
|
|
|
2015-02-26 03:41:47 -05:00
|
|
|
from misoclib.liteeth.common import *
|
|
|
|
from misoclib.liteeth.test.common import *
|
2015-01-29 13:17:45 -05:00
|
|
|
|
2015-02-26 03:41:47 -05:00
|
|
|
from misoclib.liteeth.test.model import mac
|
2015-01-29 18:03:16 -05:00
|
|
|
|
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()):
|
2015-02-11 05:28:15 -05:00
|
|
|
r += k + " : 0x{:0x}\n".format(getattr(self,k))
|
2015-01-29 13:17:45 -05:00
|
|
|
r += "payload: "
|
|
|
|
for d in self:
|
2015-02-11 05:28:15 -05:00
|
|
|
r += "{:02x}".format(d)
|
2015-01-29 13:17:45 -05:00
|
|
|
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)
|
2015-02-04 16:51:11 -05:00
|
|
|
mac_packet.target_mac = packet.target_mac
|
|
|
|
mac_packet.sender_mac = packet.sender_mac
|
2015-01-29 18:03:16 -05:00
|
|
|
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-02-05 17:30:50 -05:00
|
|
|
if len(packet) != eth_min_len-arp_header_len:
|
2015-01-29 13:17:45 -05:00
|
|
|
raise ValueError
|
2015-02-04 16:51:11 -05:00
|
|
|
if packet.hwtype != arp_hwtype_ethernet:
|
2015-01-29 13:17:45 -05:00
|
|
|
raise ValueError
|
2015-02-04 16:51:11 -05:00
|
|
|
if packet.proto != arp_proto_ip:
|
2015-01-29 13:17:45 -05:00
|
|
|
raise ValueError
|
2015-02-04 16:51:11 -05:00
|
|
|
if packet.hwsize != 6:
|
2015-01-29 13:17:45 -05:00
|
|
|
raise ValueError
|
2015-02-04 16:51:11 -05:00
|
|
|
if packet.protosize != 4:
|
2015-01-29 13:17:45 -05:00
|
|
|
raise ValueError
|
2015-02-04 16:51:11 -05:00
|
|
|
if packet.opcode == arp_opcode_request:
|
2015-01-29 13:17:45 -05:00
|
|
|
self.process_request(packet)
|
2015-02-04 16:51:11 -05:00
|
|
|
elif packet.opcode == arp_opcode_reply:
|
2015-01-29 13:17:45 -05:00
|
|
|
self.process_reply(packet)
|
2015-02-04 13:03:49 -05:00
|
|
|
|
2015-01-29 13:17:45 -05:00
|
|
|
def process_request(self, request):
|
2015-02-04 16:51:11 -05:00
|
|
|
if request.target_ip == self.ip_address:
|
2015-02-05 17:30:50 -05:00
|
|
|
reply = ARPPacket([0]*(eth_min_len-arp_header_len))
|
2015-02-04 16:51:11 -05:00
|
|
|
reply.hwtype = arp_hwtype_ethernet
|
|
|
|
reply.proto = arp_proto_ip
|
|
|
|
reply.opcode = arp_opcode_reply
|
|
|
|
reply.hwsize = 6
|
|
|
|
reply.protosize = 4
|
|
|
|
reply.sender_mac = self.mac_address
|
|
|
|
reply.sender_ip = self.ip_address
|
|
|
|
reply.target_mac = request.sender_mac
|
|
|
|
reply.target_ip = request.sender_ip
|
2015-01-29 13:17:45 -05:00
|
|
|
self.send(reply)
|
|
|
|
|
|
|
|
def process_reply(self, reply):
|
2015-02-04 16:51:11 -05:00
|
|
|
self.table[reply.sender_ip] = reply.sender_mac
|
2015-01-29 13:17:45 -05:00
|
|
|
|
|
|
|
def request(self, ip_address):
|
2015-02-05 17:30:50 -05:00
|
|
|
request = ARPPacket([0]*(eth_min_len-arp_header_len))
|
2015-02-04 16:51:11 -05:00
|
|
|
request.hwtype = arp_hwtype_ethernet
|
|
|
|
request.proto = arp_proto_ip
|
|
|
|
request.opcode = arp_opcode_request
|
|
|
|
request.hwsize = 6
|
|
|
|
request.protosize = 4
|
|
|
|
request.sender_mac = self.mac_address
|
|
|
|
request.sender_ip = self.ip_address
|
|
|
|
request.target_mac = 0xffffffffffff
|
|
|
|
request.target_ip = ip_address
|
2015-01-29 18:02:11 -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-29 18:02:11 -05:00
|
|
|
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))
|