def send_arp_request(self, src_mac, src_ip, dst_ip, port_key): arp_request_pkt = packet.Packet() arp_request_pkt.add_protocol( ethernet.ethernet(ethertype=ether.ETH_TYPE_ARP, src=src_mac)) arp_request_pkt.add_protocol( arp.arp(src_mac=src_mac, src_ip=src_ip, dst_ip=dst_ip)) self.dispatch_packet(arp_request_pkt, port_key)
def arp_reply(vid, eth_src, eth_dst, src_ip, dst_ip): """Return an ARP reply packet. Args: vid (int or None): VLAN VID to use (or None). eth_src (str): Ethernet source address. eth_dst (str): destination Ethernet MAC address. src_ip (ipaddress.IPv4Address): source IPv4 address. dst_ip (ipaddress.IPv4Address): destination IPv4 address. Returns: ryu.lib.packet.arp: serialized ARP reply packet. """ pkt = build_pkt_header(vid, eth_src, eth_dst, valve_of.ether.ETH_TYPE_ARP) arp_pkt = arp.arp(opcode=arp.ARP_REPLY, src_mac=eth_src, src_ip=src_ip, dst_mac=eth_dst, dst_ip=dst_ip) pkt.add_protocol(arp_pkt) pkt.serialize() return pkt
def arp_request(vid, eth_src, eth_dst, src_ip, dst_ip): """Return an ARP request packet. Args: vid (int or None): VLAN VID to use (or None). eth_src (str): Ethernet source address. eth_dst (str): Ethernet destination address. src_ip (ipaddress.IPv4Address): source IPv4 address. dst_ip (ipaddress.IPv4Address): requested IPv4 address. Returns: ryu.lib.packet.arp: serialized ARP request packet. """ pkt = build_pkt_header(vid, eth_src, eth_dst, valve_of.ether.ETH_TYPE_ARP) arp_pkt = arp.arp(opcode=arp.ARP_REQUEST, src_mac=eth_src, src_ip=str(src_ip), dst_mac=valve_of.mac.DONTCARE_STR, dst_ip=str(dst_ip)) pkt.add_protocol(arp_pkt) pkt.serialize() return pkt
def decode(nfa): """This function analyses nflog packet by using os-ken packet library.""" prefix = ffi.string(libnflog.nflog_get_prefix(nfa)) packet_hdr = libnflog.nflog_get_msg_packet_hdr(nfa) hw_proto = socket.ntohs(packet_hdr.hw_protocol) msg = '' msg_packet_hwhdr = libnflog.nflog_get_msg_packet_hwhdr(nfa) if msg_packet_hwhdr != ffi.NULL: packet_hwhdr = ffi.string(msg_packet_hwhdr) if len(packet_hwhdr) >= 12: dst, src = struct.unpack_from('!6s6s', packet_hwhdr) # Dump ethernet packet to get mac addresses eth = ethernet.ethernet(addrconv.mac.bin_to_text(dst), addrconv.mac.bin_to_text(src), ethertype=hw_proto) msg = str(eth) # Dump IP packet pkt = _payload(nfa) if hw_proto == ether_types.ETH_TYPE_IP: ip_pkt, proto, data = ipv4.ipv4().parser(pkt) msg += str(ip_pkt) proto_pkt, a, b = proto.parser(data) msg += str(proto_pkt) elif hw_proto == ether_types.ETH_TYPE_IPV6: ip_pkt, proto, data = ipv6.ipv6().parser(pkt) proto_pkt, a, b = proto.parser(data) msg += str(proto_pkt) elif hw_proto == ether_types.ETH_TYPE_ARP: ip_pkt, proto, data = arp.arp().parser(pkt) msg += str(ip_pkt) else: msg += "Does not support hw_proto: " + str(hw_proto) return { 'prefix': encodeutils.safe_decode(prefix), 'msg': encodeutils.safe_decode(msg) }
def _build_arp(self, opcode, dst_ip=HOST_IP): if opcode == arp.ARP_REQUEST: _eth_dst_mac = self.BROADCAST_MAC _arp_dst_mac = self.ZERO_MAC elif opcode == arp.ARP_REPLY: _eth_dst_mac = self.HOST_MAC _arp_dst_mac = self.HOST_MAC e = self._build_ether(ether.ETH_TYPE_ARP, _eth_dst_mac) a = arp.arp(hwtype=1, proto=ether.ETH_TYPE_IP, hlen=6, plen=4, opcode=opcode, src_mac=self.OSKEN_MAC, src_ip=self.OSKEN_IP, dst_mac=_arp_dst_mac, dst_ip=dst_ip) p = packet.Packet() p.add_protocol(e) p.add_protocol(a) p.serialize() return p
class Test_arp(unittest.TestCase): """ Test case for arp """ hwtype = 1 proto = 0x0800 hlen = 6 plen = 4 opcode = 1 src_mac = '00:07:0d:af:f4:54' src_ip = '24.166.172.1' dst_mac = '00:00:00:00:00:00' dst_ip = '24.166.173.159' fmt = arp._PACK_STR buf = pack(fmt, hwtype, proto, hlen, plen, opcode, addrconv.mac.text_to_bin(src_mac), addrconv.ipv4.text_to_bin(src_ip), addrconv.mac.text_to_bin(dst_mac), addrconv.ipv4.text_to_bin(dst_ip)) a = arp(hwtype, proto, hlen, plen, opcode, src_mac, src_ip, dst_mac, dst_ip) def setUp(self): pass def tearDown(self): pass def find_protocol(self, pkt, name): for p in pkt.protocols: if p.protocol_name == name: return p def test_init(self): eq_(self.hwtype, self.a.hwtype) eq_(self.proto, self.a.proto) eq_(self.hlen, self.a.hlen) eq_(self.plen, self.a.plen) eq_(self.opcode, self.a.opcode) eq_(self.src_mac, self.a.src_mac) eq_(self.src_ip, self.a.src_ip) eq_(self.dst_mac, self.a.dst_mac) eq_(self.dst_ip, self.a.dst_ip) def test_parser(self): _res = self.a.parser(self.buf) if type(_res) is tuple: res = _res[0] else: res = _res eq_(res.hwtype, self.hwtype) eq_(res.proto, self.proto) eq_(res.hlen, self.hlen) eq_(res.plen, self.plen) eq_(res.opcode, self.opcode) eq_(res.src_mac, self.src_mac) eq_(res.src_ip, self.src_ip) eq_(res.dst_mac, self.dst_mac) eq_(res.dst_ip, self.dst_ip) def test_serialize(self): data = bytearray() prev = None buf = self.a.serialize(data, prev) fmt = arp._PACK_STR res = struct.unpack(fmt, buf) eq_(res[0], self.hwtype) eq_(res[1], self.proto) eq_(res[2], self.hlen) eq_(res[3], self.plen) eq_(res[4], self.opcode) eq_(res[5], addrconv.mac.text_to_bin(self.src_mac)) eq_(res[6], addrconv.ipv4.text_to_bin(self.src_ip)) eq_(res[7], addrconv.mac.text_to_bin(self.dst_mac)) eq_(res[8], addrconv.ipv4.text_to_bin(self.dst_ip)) def _build_arp(self, vlan_enabled): if vlan_enabled is True: ethertype = ether.ETH_TYPE_8021Q v = vlan(1, 1, 3, ether.ETH_TYPE_ARP) else: ethertype = ether.ETH_TYPE_ARP e = ethernet(self.dst_mac, self.src_mac, ethertype) p = Packet() p.add_protocol(e) if vlan_enabled is True: p.add_protocol(v) p.add_protocol(self.a) p.serialize() return p def test_build_arp_vlan(self): p = self._build_arp(True) e = self.find_protocol(p, "ethernet") ok_(e) eq_(e.ethertype, ether.ETH_TYPE_8021Q) v = self.find_protocol(p, "vlan") ok_(v) eq_(v.ethertype, ether.ETH_TYPE_ARP) a = self.find_protocol(p, "arp") ok_(a) eq_(a.hwtype, self.hwtype) eq_(a.proto, self.proto) eq_(a.hlen, self.hlen) eq_(a.plen, self.plen) eq_(a.opcode, self.opcode) eq_(a.src_mac, self.src_mac) eq_(a.src_ip, self.src_ip) eq_(a.dst_mac, self.dst_mac) eq_(a.dst_ip, self.dst_ip) def test_build_arp_novlan(self): p = self._build_arp(False) e = self.find_protocol(p, "ethernet") ok_(e) eq_(e.ethertype, ether.ETH_TYPE_ARP) a = self.find_protocol(p, "arp") ok_(a) eq_(a.hwtype, self.hwtype) eq_(a.proto, self.proto) eq_(a.hlen, self.hlen) eq_(a.plen, self.plen) eq_(a.opcode, self.opcode) eq_(a.src_mac, self.src_mac) eq_(a.src_ip, self.src_ip) eq_(a.dst_mac, self.dst_mac) eq_(a.dst_ip, self.dst_ip) @raises(Exception) def test_malformed_arp(self): m_short_buf = self.buf[1:arp._MIN_LEN] arp.parser(m_short_buf) def test_json(self): jsondict = self.a.to_jsondict() a = arp.from_jsondict(jsondict['arp']) eq_(str(self.a), str(a))