def ARP_Request_handler(self, packet, packet_in): ARP_Dst = self.route_table.get(str(packet.payload.protodst)) if ARP_Dst[2] == 'H': packet.dst = adr.EthAddr(ARP_Dst[0]) self.send_Packet(frame=packet, out_port=ARP_Dst[3]) #host -> router elif ARP_Dst[2] == 'R': if str(packet.payload.protodst) == '10.0.1.1': arp_reply = pkt.arp() arp_reply.hwsrc = adr.EthAddr('00:00:00:00:11:05') arp_reply.hwdst = packet.payload.hwsrc arp_reply.opcode = pkt.arp.REPLY arp_reply.protosrc = packet.payload.protodst arp_reply.protodst = packet.payload.protosrc #make ARP packet -> Frame ether = pkt.ethernet() ether.type = pkt.ethernet.ARP_TYPE ether.dst = packet.src ether.src = packet.dst ether.payload = arp_reply #send Frame self.send_Packet(frame=ether, out_port=packet_in.in_port) elif str(packet.payload.protodst) == '10.0.2.1': arp_reply = pkt.arp() arp_reply.hwsrc = adr.EthAddr('00:00:00:00:11:06') arp_reply.hwdst = packet.payload.hwsrc arp_reply.opcode = pkt.arp.REPLY arp_reply.protosrc = packet.payload.protodst arp_reply.protodst = packet.payload.protosrc #make ARP packet -> Frame ether = pkt.ethernet() ether.type = pkt.ethernet.ARP_TYPE ether.dst = packet.src ether.src = packet.dst ether.payload = arp_reply #send Frame self.send_Packet(frame=ether, out_port=packet_in.in_port) elif str(packet.payload.protodst) == '10.0.3.1': arp_reply = pkt.arp() arp_reply.hwsrc = adr.EthAddr('00:00:00:00:11:07') arp_reply.hwdst = packet.payload.hwsrc arp_reply.opcode = pkt.arp.REPLY arp_reply.protosrc = packet.payload.protodst arp_reply.protodst = packet.payload.protosrc #make ARP packet -> Frame ether = pkt.ethernet() ether.type = pkt.ethernet.ARP_TYPE ether.dst = packet.src ether.src = packet.dst ether.payload = arp_reply #send Frame self.send_Packet(frame=ether, out_port=packet_in.in_port)
def _handle_arp(self, event, ether_pkt, arp_pkt): # Update the ARP table with the info for the sender. self._packet_logger.action('Add ARP Entry', [ ('IP Address', arp_pkt.protosrc), ('MAC Address', arp_pkt.hwsrc) ]) self._arp_table.add(arp_pkt.protosrc, arp_pkt.hwsrc) if not is_special_mac(arp_pkt.hwdst): # If the hardware destination is a normal MAC address, add it to the ARP table. self._packet_logger.action('Add ARP Entry', [ ('IP Address', arp_pkt.protodst), ('MAC Address', arp_pkt.hwdst) ]) self._arp_table.add(arp_pkt.protodst, arp_pkt.hwdst) if arp_pkt.opcode == arp.REQUEST: # Try to find a known MAC address for the requested IP address and send a reply ourselves. requested_ip = arp_pkt.protodst requested_mac = self._arp_table.lookup(requested_ip) self._packet_logger.metric('ARP Target', [ ('IP', requested_ip), ('MAC', requested_mac) ]) if requested_mac: self._packet_logger.action('ARP Reply', [ ('Requested MAC', requested_mac), ('Out Port', event.ofp.in_port) ]) arp_reply = arp(hwsrc = requested_mac, hwdst = arp_pkt.hwsrc, opcode = arp.REPLY, protosrc = requested_ip, protodst = arp_pkt.protosrc) ether = ethernet(type = ethernet.ARP_TYPE, dst = arp_pkt.hwsrc, src = requested_mac, payload = arp_reply) self._send_packet(ether, event.ofp.in_port) return True
def _handle_PacketIn(event): packet = event.parsed if packet.type == packet.ARP_TYPE: if packet.payload.opcode == arp.REQUEST: arp_reply = arp() arp_reply.hwsrc = table[ packet.payload.protodst] #<requested mac address> arp_reply.hwdst = packet.src arp_reply.opcode = arp.REPLY arp_reply.protosrc = packet.payload.protodst #<IP of requested mac-associated machine> arp_reply.protodst = packet.payload.protosrc ether = ethernet() ether.type = ethernet.ARP_TYPE ether.dst = packet.src ether.src = table[ packet.payload.protodst] #<requested mac address> ether.payload = arp_reply #send this packet to the switch msg = of.ofp_packet_out() msg.data = ether.pack() msg.actions.append(of.ofp_action_output(port=event.ofp.in_port)) event.connection.send(msg) #see section below on this topic elif packet.payload.opcode == arp.REPLY: print "It's a reply; do something cool" else: print "Some other ARP opcode, probably do something smart here"
def gratuitousARP(self,vs): if vs is None: vswitches = self.net[0xaabbcc].vswitch else: vswitches = dict(only=vs) for k in vswitches.keys(): v = vswitches[k] pkt_eth = pkt.ethernet(dst=pkt.ETHER_BROADCAST,src=v.hw_addr) pkt_eth.type = pkt.ethernet.VLAN_TYPE pkt_vlan = pkt.vlan(id=v.vstag) pkt_vlan.eth_type = pkt.ethernet.ARP_TYPE grat = pkt.arp() grat.opcode = pkt.arp.REQUEST grat.hwsrc = pkt_eth.src grat.hwdst = pkt_eth.dst grat.payload = b'\x0f' * 8 pkt_eth.payload=pkt_vlan pkt_vlan.payload=grat dpid = self.net[0xaabbcc].getDPID(v) msg = of.ofp_packet_out(data=pkt_eth) msg.actions.append(of.ofp_action_output(port = v.uplink)) try: conn = core.openflow.getConnection(dpid) self.log.debug("Sending gratuitousARP for %s" % v) conn.send(msg) except: raise RuntimeError("can't send msg to vSwitch %s" % v)
def handle_arp(self, packet, in_port): """ Controller Handles ARP packet """ arp = packet.find("arp") if packet.payload.opcode == arp.REPLY: # Server or client reply host = {'ip': arp.protosrc, 'mac': arp.hwsrc, 'port': in_port} if arp.protosrc in self.server_ips: log.debug("Server ARP reply :" + str(arp.hwsrc)) self.servers.append(host) else: log.debug("Client ARP reply :" + str(arp.hwsrc)) self.clients.append(host) elif packet.payload.opcode == arp.REQUEST: # Request for balancer log.debug("ARP request for Load balancer") packet = pkt.ethernet(type=pkt.ethernet.ARP_TYPE, src=self.mac, dst=arp.hwsrc) packet.payload = pkt.arp(opcode=pkt.arp.REPLY, hwtype=pkt.arp.HW_TYPE_ETHERNET, prototype=pkt.arp.PROTO_TYPE_IP, hwsrc=self.mac, hwdst=arp.hwsrc, protosrc=self.ip, protodst=arp.protosrc) msg = of.ofp_packet_out(data=packet.pack(), action=of.ofp_action_output(port=in_port)) self.connection.send(msg)
def router_main(self): while True: try: dev,ts,pkt = self.net.recv_packet(timeout=1.0) except SrpyNoPackets: # log_debug("Timeout waiting for packets") continue except SrpyShutdown: return if pkt.type == pkt.ARP_TYPE: arp_request = pkt.payload for intf in self.net.interfaces(): if (intf.ipaddr==arp_request.protodst): arp_reply = pktlib.arp() arp_reply.protodst = arp_request.protosrc arp_reply.protosrc = intf.ipaddr arp_reply.hwsrc = intf.ethaddr arp_reply.hwdst = arp_request.hwsrc arp_reply.opcode = pktlib.arp.REPLY ether = pktlib.ethernet() ether.type = ether.ARP_TYPE ether.src = intf.ethaddr ether.dst = arp_request.hwsrc ether.set_payload(arp_reply) self.net.send_packet(dev, ether) #self.net.send_packet(dev, arp_reply) break
def send_arp_request(self, event, route, packet, match, nextHopIp): if nextHopIp in self.outstandingarp and time.time( ) > self.outstandingarp[nextHopIp] + self.ARP_TIMEOUT: return self.outstandingarp[nextHopIp] = time.time() r = pkt.arp() r.hwtype = r.HW_TYPE_ETHERNET r.prototype = r.PROTO_TYPE_IP r.hwlen = 6 r.protolen = r.protolen r.opcode = r.REQUEST r.hwdst = ETHER_BROADCAST r.protodst = nextHopIp r.hwsrc = self.port2Mac[self.intf2Port[route.intf]] r.protosrc = IPAddr(ROUTERS_IPS[route.intf]) #r.protodst = packet.next.dstip e = ethernet(type=ethernet.ARP_TYPE, src=r.hwsrc, dst=r.hwdst) e.set_payload(r) log.debug("%s ARPing for %s on behalf of %s" % (route.intf, r.protodst, r.protosrc)) msg = of.ofp_packet_out() msg.data = e.pack() #msg.actions.append(of.ofp_action_output(port = of.OFPP_FLOOD)) msg.actions.append( of.ofp_action_output(port=self.intf2Port[route.intf])) msg.in_port = event.port event.connection.send(msg)
def perform(self, event): packet = event.parse() a = packet.find("arp") # Create arp reply r. r = pkt.arp() r.hwtype = a.hwtype r.prototype = a.prototype r.hwlen = a.hwlen r.protolen = a.protolen r.opcode = r.REPLY r.hwdst = a.hwsrc r.protodst = a.protosrc r.protosrc = a.protodst r.hwsrc = self.macAddr # Create ethernet packet e. e = pkt.ethernet(type=packet.type, src=self.macAddr, dst=packet.src) e.payload = r # Create PacketOut msg. msg = of.ofp_packet_out() msg.data = e.pack() msg.in_port = event.port msg.actions.append(of.ofp_action_output(port=of.OFPP_IN_PORT)) event.connection.send(msg)
def arpReply(self, e): ''' This method builds and sends ARP reply in response to ARP Request. e = payload of original IP packet received ''' # Create ARP reply header (Ethernet) if (e.protodst in self.interface): dev = self.interface[e.protodst][1] ethpkt = pktlib.ethernet() ethpkt.src = self.interface[e.protodst][2] ethpkt.dst = e.hwsrc ethpkt.type = ethpkt.ARP_TYPE arp_rep = pktlib.arp() arp_rep.opcode = pktlib.arp.REPLY arp_rep.protosrc = e.protodst arp_rep.protodst = e.protosrc arp_rep.hwsrc = self.interface[e.protodst][2] arp_rep.hwdst = e.hwsrc # Encapsulate eth packet ethpkt.set_payload(arp_rep) # Send it back to the src address self.net.send_packet(dev,ethpkt) return
def gratuitousARP(self, vs): if vs is None: vswitches = self.net[0xaabbcc].vswitch else: vswitches = dict(only=vs) for k in vswitches.keys(): v = vswitches[k] pkt_eth = pkt.ethernet(dst=pkt.ETHER_BROADCAST, src=v.hw_addr) pkt_eth.type = pkt.ethernet.VLAN_TYPE pkt_vlan = pkt.vlan(id=v.vstag) pkt_vlan.eth_type = pkt.ethernet.ARP_TYPE grat = pkt.arp() grat.opcode = pkt.arp.REQUEST grat.hwsrc = pkt_eth.src grat.hwdst = pkt_eth.dst grat.payload = b'\x0f' * 8 pkt_eth.payload = pkt_vlan pkt_vlan.payload = grat dpid = self.net[0xaabbcc].getDPID(v) msg = of.ofp_packet_out(data=pkt_eth) msg.actions.append(of.ofp_action_output(port=v.uplink)) try: conn = core.openflow.getConnection(dpid) self.log.debug("Sending gratuitousARP for %s" % v) conn.send(msg) except: raise RuntimeError("can't send msg to vSwitch %s" % v)
def _send_arp_request(self, nw_dst): # If the ARP is for an IP on the internal network, use the internal gateway IP # address as the source and flood it. if IPAddr(nw_dst).in_network(self._network, netmask = self._netmask): proto_src = self._gateway_ip out_port = of.OFPP_FLOOD else: # Otherwise, use the external NAT IP address and send it out the uplink. proto_src = self._nat_ip out_port = self._uplink_port self._packet_logger.action('Send ARP Request', [ ('Source MAC', self._gateway_mac), ('Dest MAC', SpecialMacs.ARP_REQUEST_DESTINATION), ('Source IP', proto_src), ('Dest IP', nw_dst) ]) arp_request = arp( hwsrc = self._gateway_mac, hwdst = SpecialMacs.ARP_REQUEST_DESTINATION, opcode = arp.REQUEST, protosrc = proto_src, protodst = nw_dst) ether = ethernet(type = ethernet.ARP_TYPE, src = self._gateway_mac, dst = SpecialMacs.BROADCAST, payload = arp_request) self._send_packet(ether, out_port)
def send_arp_request(self, event, route, packet, match, nextHopIp): if nextHopIp in self.outstandingarp and time.time() > self.outstandingarp[nextHopIp] + self.ARP_TIMEOUT: return self.outstandingarp[nextHopIp] = time.time() r = pkt.arp() r.hwtype = r.HW_TYPE_ETHERNET r.prototype = r.PROTO_TYPE_IP r.hwlen = 6 r.protolen = r.protolen r.opcode = r.REQUEST r.hwdst = ETHER_BROADCAST r.protodst = nextHopIp r.hwsrc = self.port2Mac[self.intf2Port[route.intf]] r.protosrc = IPAddr(ROUTERS_IPS[route.intf]) #r.protodst = packet.next.dstip e = ethernet(type=ethernet.ARP_TYPE, src=r.hwsrc, dst=r.hwdst) e.set_payload(r) log.debug("%s ARPing for %s on behalf of %s" % (route.intf, r.protodst, r.protosrc)) msg = of.ofp_packet_out() msg.data = e.pack() #msg.actions.append(of.ofp_action_output(port = of.OFPP_FLOOD)) msg.actions.append(of.ofp_action_output(port = self.intf2Port[route.intf])) msg.in_port = event.port event.connection.send(msg)
def send_arp_reply(self, event): # SOURCE: referenced from l3_learning packet = event.parsed arp_request = packet.payload requested_ip = arp_request.protodst.toStr() # get eth answer to arp request self.id_gen.ingestByIP(requested_ip) mac_str = self.id_gen.getMac() hardware_answer = EthAddr(mac_str) # generate an arp packet arp_reply = pkt.arp() arp_reply.opcode = arp_request.REPLY # IP addrs are just flipped arp_reply.protosrc = arp_request.protodst arp_reply.protodst = arp_request.protosrc arp_reply.hwdst = packet.src # hardware dest is the requester's eth arp_reply.hwsrc = hardware_answer # hardware src is the mac answer #encapsulate this in an ethernet packet e = pkt.ethernet(dst=packet.src, src=hardware_answer) e.type = e.ARP_TYPE e.set_payload(arp_reply) #send this packet msg = of.ofp_packet_out() msg.data = e.pack() msg.actions.append(of.ofp_action_output(port=of.OFPP_IN_PORT)) msg.in_port = event.port self.connection.send(msg)
def _arp_for_real_servers (): for rs in [server for server in _real_servers if server not in _arp_table]: log.debug("ARPing in UpEvent for real server %s", str(rs)) q = arp() q.opcode = arp.REQUEST q.protodst = rs con = None for con in core.openflow.connections: if con is not None: break else: log.info("can't get any connection") return True e = ethernet(type=ethernet.ARP_TYPE, dst=ETHER_BROADCAST) e.payload = q log.debug("%s ARPing in UpEvent for real server %s", dpid_to_str(con.dpid), str(rs)) msg = of.ofp_packet_out() msg.data = e.pack() msg.actions.append(of.ofp_action_output(port = of.OFPP_FLOOD)) # FIXME assume port number starts from 1 # There will be a problem if port 1 is the # only port that will join flooding (spanning_tree) msg.in_port = 1 con.send(msg) res = False for server in _real_servers: if server not in _arp_table: res = True break return res
def arpBuilder(self, dev, pkt, nexthop, retry): ''' This method builds and sends ARP request ''' if (pkt.type == pkt.IP_TYPE): e=pkt.payload if dev in self.devInterface: tempeth = self.devInterface[dev][0] tempip = self.devInterface[dev][1] ethpkt = pktlib.ethernet() ethpkt.src = tempeth ethpkt.dst = ETHER_BROADCAST ethpkt.type = ethpkt.ARP_TYPE # arp header arp_req = pktlib.arp() arp_req.opcode = pktlib.arp.REQUEST arp_req.protosrc = tempip if (nexthop==IPAddr("0")): arp_req.protodst = e.dstip else: arp_req.protodst = nexthop arp_req.hwsrc = tempeth arp_req.hwdst = EthAddr('ff:ff:ff:ff:ff:ff') # Encapsulate the packet and send it ethpkt.set_payload(arp_req) self.arpRequest(dev, pkt, ethpkt, retry)
def send_arp_req(self, event): packet, packet_in, dpid = getpacket(event) rte = self.find_route(packet.next.dstip, dpid) if rte == None: return a = pkt.arp() a.opcode = a.REQUEST a.protodst = packet.next.dstip a.protosrc = IPAddr(rte[3]) a.hwtype = a.HW_TYPE_ETHERNET a.prototype = a.PROTO_TYPE_IP a.hwlen = 6 a.hwdst = pkt.ETHER_BROADCAST a.hwsrc = self.mac[dpid] e = pkt.ethernet(type=packet.ARP_TYPE, src=a.hwsrc, dst=pkt.ETHER_BROADCAST) e.set_payload(a) for p in rte[4]: if packet_in.in_port != p: msg = of.ofp_packet_out() msg.data = e.pack() msg.actions.append(of.ofp_action_output(port=p)) log.debug("send arp req (%s) port %d", str(a.protodst), p) self.connection[dpid].send(msg) log.debug("done")
def _send_arp_reply(self, packet_eth, in_port): # parse ARP request, get destination mac address arp_req = packet_eth.payload ip_src, ip_dst = arp_req.protosrc, arp_req.protodst mac_src, mac_dst = packet_eth.src, EthAddr( comm.ip_to_mac(ip_dst.toStr())) # create an ARP response packet arp_resp = pkt.arp() arp_resp.opcode = pkt.arp.REPLY arp_resp.protosrc = ip_dst arp_resp.protodst = ip_src arp_resp.hwsrc = mac_dst arp_resp.hwdst = mac_src # pack inside a ethernet frame reply = pkt.ethernet(src=mac_dst, dst=mac_src) reply.type = pkt.ethernet.ARP_TYPE reply.set_payload(arp_resp) # send response msg = of.ofp_packet_out() msg.data = reply.pack() msg.actions.append(of.ofp_action_output(port=of.OFPP_IN_PORT)) msg.in_port = in_port self._conn.send(msg) log.debug("send_arp_reply | dpid={} | ({},{}) => ({},{})".format( self._dpid, ip_src.toStr(), mac_src.toStr(), ip_dst.toStr(), mac_dst.toStr()))
def _arp_for_real_servers(): for rs in [server for server in _real_servers if server not in _arp_table]: log.debug("ARPing in UpEvent for real server %s", str(rs)) q = arp() q.opcode = arp.REQUEST q.protodst = rs con = None for con in core.openflow.connections: if con is not None: break else: log.info("can't get any connection") return True e = ethernet(type=ethernet.ARP_TYPE, dst=ETHER_BROADCAST) e.payload = q log.debug("%s ARPing in UpEvent for real server %s", dpid_to_str(con.dpid), str(rs)) msg = of.ofp_packet_out() msg.data = e.pack() msg.actions.append(of.ofp_action_output(port=of.OFPP_FLOOD)) # FIXME assume port number starts from 1 # There will be a problem if port 1 is the # only port that will join flooding (spanning_tree) msg.in_port = 1 con.send(msg) res = False for server in _real_servers: if server not in _arp_table: res = True break return res
def _handle_PacketIn (event): packet = event.parsed if packet.find("arp"): # Reply to ARP a = packet.find("arp") if a.opcode == a.REQUEST: r = pkt.arp() r.hwtype = a.hwtype r.prototype = a.prototype r.hwlen = a.hwlen r.protolen = a.protolen r.opcode = r.REPLY r.hwdst = a.hwsrc r.protodst = a.protosrc r.protosrc = a.protodst r.hwsrc = EthAddr("02:00:DE:AD:BE:EF") e = pkt.ethernet(type=packet.type, src=r.hwsrc, dst=a.hwsrc) e.payload = r msg = of.ofp_packet_out() msg.data = e.pack() msg.actions.append(of.ofp_action_output(port = of.OFPP_IN_PORT)) msg.in_port = event.port event.connection.send(msg) log.info("%s ARPed for %s", r.protodst, r.protosrc) elif packet.find("icmp"): # Reply to pings # Make the ping reply icmp = pkt.icmp() icmp.type = pkt.TYPE_ECHO_REPLY icmp.payload = packet.find("icmp").payload # Make the IP packet around it ipp = pkt.ipv4() ipp.protocol = ipp.ICMP_PROTOCOL ipp.srcip = packet.find("ipv4").dstip ipp.dstip = packet.find("ipv4").srcip # Ethernet around that... e = pkt.ethernet() e.src = packet.dst e.dst = packet.src e.type = e.IP_TYPE # Hook them up... ipp.payload = icmp e.payload = ipp # Send it back to the input port msg = of.ofp_packet_out() msg.actions.append(of.ofp_action_output(port = of.OFPP_IN_PORT)) msg.data = e.pack() msg.in_port = event.port event.connection.send(msg) log.debug("%s pinged %s", ipp.dstip, ipp.srcip)
def _handle_PacketIn(event): packet = event.parsed if packet.find("arp"): # Reply to ARP a = packet.find("arp") if a.opcode == a.REQUEST: r = pkt.arp() r.hwtype = a.hwtype r.prototype = a.prototype r.hwlen = a.hwlen r.protolen = a.protolen r.opcode = r.REPLY r.hwdst = a.hwsrc r.protodst = a.protosrc r.protosrc = a.protodst r.hwsrc = EthAddr("02:00:DE:AD:BE:EF") e = pkt.ethernet(type=packet.type, src=r.hwsrc, dst=a.hwsrc) e.payload = r msg = of.ofp_packet_out() msg.data = e.pack() msg.actions.append(of.ofp_action_output(port=of.OFPP_IN_PORT)) msg.in_port = event.port event.connection.send(msg) log.info("%s ARPed for %s", r.protodst, r.protosrc) elif packet.find("icmp"): # Reply to pings # Make the ping reply icmp = pkt.icmp() icmp.type = pkt.TYPE_ECHO_REPLY icmp.payload = packet.find("icmp").payload # Make the IP packet around it ipp = pkt.ipv4() ipp.protocol = ipp.ICMP_PROTOCOL ipp.srcip = packet.find("ipv4").dstip ipp.dstip = packet.find("ipv4").srcip # Ethernet around that... e = pkt.ethernet() e.src = packet.dst e.dst = packet.src e.type = e.IP_TYPE # Hook them up... ipp.payload = icmp e.payload = ipp # Send it back to the input port msg = of.ofp_packet_out() msg.actions.append(of.ofp_action_output(port=of.OFPP_IN_PORT)) msg.data = e.pack() msg.in_port = event.port event.connection.send(msg) log.debug("%s pinged %s", ipp.dstip, ipp.srcip)
def create_arp_respond(arp_req, answer): arp_rep = pkt.arp() arp_rep.opcode = arp_req.payload.REPLY arp_rep.hwdst = arp_req.src arp_rep.protodst = arp_req.payload.protosrc arp_rep.protosrc = arp_req.payload.protodst arp_rep.hwsrc = answer return arp_rep
def generate_arp_response(self, a, packet_in,dpid): r = pkt.arp(hwtype = a.hwtype, prototype = a.prototype, hwlen = a.hwlen, protolen = a.protolen, opcode = pkt.arp.REPLY, hwdst = a.hwsrc, protodst = a.protosrc, protosrc = a.protodst, hwsrc = adr.EthAddr('EF:EF:EF:EF:EF:EF')) e = ethernet(type = pkt.ethernet.ARP_TYPE, src = adr.EthAddr('EF:EF:EF:EF:EF:EF'), dst=a.hwsrc, payload = r) msg = of.ofp_packet_out() msg.data = e.pack() msg.actions.append(of.ofp_action_output(port = packet_in.in_port)) log.debug(" answering for arp from %s: MAC for %s is %s", str(a.protosrc), str(r.protosrc), str(r.hwsrc)) self.connection.send(msg) log.debug('ARP Reply Sent...')
def _handle_PacketIn (self, event): """ Packets not handled by the router rules will be forwarded to this method to be handled by the controller """ packet = event.parsed # This is the parsed packet data. if not packet.parsed: log.warning("Ignoring incomplete packet") return packet_in = event.ofp # The actual ofp_packet_in message. core_mac_addr = EthAddr("de:ed:be:ef:ca:fe") port_num = packet_in.in_port mac_addr_src = EthAddr(packet.src) # packet receives ARP request needs to respond an ARP message if packet.type == packet.ARP_TYPE: arp_packet = packet.find('arp') ip_addr_host = arp_packet.protosrc if not ip_addr_host in ip_learn_set: # cores21 install a rule on the address and port msg = of.ofp_flow_mod() msg.priority = 33001 msg.match.nw_dst = IPAddr(ip_addr_host) # arp translate from ip to mac msg.actions.append(of.ofp_action_dl_addr.set_dst(mac_addr_src)) msg.actions.append(of.ofp_action_output(port = port_num)) msg.match.dl_type = 0x0806 self.connection.send(msg) # ip address msg.priority = 33002 msg.match.dl_type = 0x0800 self.connection.send(msg) ip_learn_set.add(ip_addr_host) # reply to ARP messages with arbitrary MAC addr to assume control arp_reply = arp() arp_reply.hwsrc = core_mac_addr arp_reply.hwdst = packet.src arp_reply.opcode = arp.REPLY arp_reply.protosrc = arp_packet.protodst arp_reply.protodst = packet.payload.protosrc # ethernet wrap packets ether = ethernet() ether.type = ethernet.ARP_TYPE ether.src = core_mac_addr ether.dst = packet.src # resend the packet ether.payload = arp_reply self.resend_packet(ether, port_num) else: # if packet isn't an ARP packet do nothing print ("Unhandled packet from " + str(self.connection.dpid) + ":" + packet.dump())
def _handle_PacketIn(self, event): """ Packets not handled by the router rules will be forwarded to this method to be handled by the controller """ packet = event.parsed # This is the parsed packet data. if not packet.parsed: log.warning("Ignoring incomplete packet") return packet_in = event.ofp # The actual ofp_packet_in message. # should only be triggered on the core switch as other switches # already have a rule to flood ARP packets if packet.type == packet.ARP_TYPE: arp_packet = packet.find('arp') mac_addr_src = packet.src ip_addr_src = arp_packet.protosrc src_port = packet_in.in_port if not ip_addr_src in ips_seen: core_rule = of.ofp_flow_mod() core_rule.priority = len(ips_seen) * 2 + 1 core_rule.match.nw_dst = IPAddr(ip_addr_src) # rule edits header of incoming packets to the correct MAC addr # and routes them to correct port core_rule.actions.append( of.ofp_action_dl_addr.set_dst(EthAddr(mac_addr_src))) core_rule.actions.append(of.ofp_action_output(port=src_port)) core_rule.match.dl_type = 0x0806 self.connection.send(core_rule) core_rule.priority = len(ips_seen) * 2 + 2 core_rule.match.dl_dst = None core_rule.match.dl_type = 0x0800 self.connection.send(core_rule) ips_seen.add(ip_addr_src) # reply to ARP messages with arbitrary MAC addr to assume control arp_reply = arp() arp_reply.hwsrc = EthAddr("de:ed:be:ef:ca:fe") arp_reply.hwdst = packet.src arp_reply.opcode = arp.REPLY arp_reply.protosrc = arp_packet.protodst arp_reply.protodst = packet.payload.protosrc ether = ethernet() ether.type = ethernet.ARP_TYPE ether.dst = packet.src ether.src = EthAddr("de:ad:be:ef:ca:fe") ether.payload = arp_reply self.resend_packet(ether, src_port) else: # if packet isn't an ARP packet do nothing print("Unhandled packet from " + str(self.connection.dpid) + ":" + packet.dump())
def make_arp_request(other_host): src_addr = access_link.host.interfaces[0].hw_addr eth = ethernet(src=src_addr, dst=EthAddr('ff:ff:ff:ff:ff:ff'),type=ethernet.ARP_TYPE) a = arp(opcode=arp.REQUEST,hwsrc=src_addr, hwdst=EthAddr('00:00:00:00:00:00'), protosrc=access_link.host.interfaces[0].ips[0], protodst=other_host.interfaces[0].ips[0]) eth.payload = a return eth
def make_arp_reply(other_host): dst_addr = access_link.host.interfaces[0].hw_addr src_addr = other_host.interfaces[0].hw_addr eth = ethernet(src=src_addr, dst=dst_addr,type=ethernet.ARP_TYPE) a = arp(opcode=arp.REPLY,hwsrc=src_addr, hwdst=dst_addr, protosrc=other_host.interfaces[0].ips[0], protodst=access_link.host.interfaces[0].ips[0]) eth.payload = a return eth
def _handle_vip_arp(self, ether_pkt, arp_pkt, event): if arp_pkt.opcode == arp.REQUEST and arp_pkt.protodst == self._vip: self._arp_table.add(arp_pkt.protosrc, ether_pkt.src) self.packet_logger.action('ARP VIP Reply', [ ('Target IP', arp_pkt.protodst), ('Target MAC', self._vip_mac) ]) arp_reply = arp(hwsrc = self._vip_mac, hwdst = ether_pkt.src, opcode = arp.REPLY, protosrc = arp_pkt.protodst, protodst = arp_pkt.protosrc) ether = ethernet(type = ethernet.ARP_TYPE, dst = ether_pkt.src, src = self._vip_mac, payload = arp_reply) self._send_packet(ether, event.ofp.in_port) else: self._drop_packet(event, 'Inbound ARP not REQUEST targeted at load balancer VIP.') return True
def make_arp_reply(other_host): dst_addr = access_link.host.interfaces[0].hw_addr src_addr = other_host.interfaces[0].hw_addr eth = ethernet(src=src_addr, dst=dst_addr, type=ethernet.ARP_TYPE) a = arp(opcode=arp.REPLY, hwsrc=src_addr, hwdst=dst_addr, protosrc=other_host.interfaces[0].ips[0], protodst=access_link.host.interfaces[0].ips[0]) eth.payload = a return eth
def createARPReply(net_id,vstag,hotom_addr,eth): hw_addr_src = fromHotOMtoEthAddr(hotom_addr) eth_reply = pkt.ethernet(dst=eth.src,src=hw_addr_src) eth_reply.type = pkt.ethernet.ARP_TYPE arp_reply = pkt.arp(opcode=pkt.arp.REPLY) arp_reply.hwsrc = hw_addr_src arp_reply.hwdst = eth.src arp_reply.protosrc = eth.payload.protodst arp_reply.protodst = eth.payload.protosrc eth_reply.payload = arp_reply return eth_reply
def _send_arp(self, dstip, src_eth, src_ip, send_function): r = pkt.arp() r.opcode = r.REQUEST r.hwdst = pkt.ETHERNET.ETHER_BROADCAST r.protodst = dstip r.hwsrc = src_eth r.protosrc = src_ip e = pkt.ethernet(type=pkt.ethernet.ARP_TYPE, src=r.hwsrc, dst=r.hwdst) e.payload = r log.debug("Sending ARP for %s", dstip) send_function(e.pack())
def createARPResponse(self, pkt_eth, hw_addr): '''Get ARP query ethernet frame and create ARP response''' hw_src = addNetIDEthAddr(hw_addr, self.net_id) eth = pkt.ethernet(dst=pkt_eth.src, src=hw_src) eth.type = pkt.ethernet.ARP_TYPE arp = pkt.arp(opcode=pkt.arp.REPLY) arp.hwsrc = hw_src arp.hwdst = pkt_eth.src arp.protosrc = pkt_eth.payload.protodst arp.protodst = pkt_eth.payload.protosrc eth.payload = arp return eth
def _handle_arp(event): eth_pkt = event.parsed arp_pkt = event.parsed.payload org_mac = eth_pkt.src.toStr() pmac_src = adrs.EthAddr(actual_pmac[org_mac]) #gratituos arp is when the src and dst ip in arp are same. check it and update tables in case of new host. or else return if arp_pkt.opcode == arp_pkt.REQUEST: dst_ip = arp_pkt.protodst.toStr() #print 'ARP request : ip:{0}'.format(dst_ip) if dst_ip in arp_table: #we know the mapping. respond back to inp port OFPP_IN_PORT. arp_reply = pkt.arp() arp_reply.hwsrc = adrs.EthAddr(arp_table[dst_ip]) arp_reply.hwdst = eth_pkt.src arp_reply.opcode = pkt.arp.REPLY arp_reply.protosrc = arp_pkt.protodst arp_reply.protodst = arp_pkt.protosrc ether = pkt.ethernet() ether.type = pkt.ethernet.ARP_TYPE ether.dst = eth_pkt.src ether.src = arp_reply.hwsrc ether.payload = arp_reply msg = of.ofp_packet_out() #msg.in_port = event.port #msg.actions.append(of.ofp_action_output(port = of.OFPP_IN_PORT)) msg.actions.append(of.ofp_action_output(port = event.port)) msg.data = ether.pack() event.connection.send(msg) else: #bcast with src changed to sources pmac. also if its this switch, put inport as the it came from, so that it wont go to it #print 'Broadcasting since we dont have ARP mapping' arp_pkt.hwsrc = pmac_src eth_pkt.src = pmac_src #edge bcast: msg = of.ofp_packet_out() for i in range(half_num_pods + 1, num_pods + 1):#pkt out to host ports msg.actions.append(of.ofp_action_output(port = i)) msg.data = eth_pkt.pack() other_switches = edge_switches - set([event.dpid]) for es in other_switches: core.openflow.connections[es].send(msg) msg.actions.pop(event.port - half_num_pods - 1) event.connection.send(msg) elif arp_pkt.opcode == arp_pkt.REPLY: #print 'ARP reply : src ip:{0}, dst ip:{1}'.format(arp_pkt.protosrc.toStr(), arp_pkt.protodst.toStr()) arp_pkt.hwdst = adrs.EthAddr(pmac_actual[arp_pkt.hwdst.toStr()]) arp_pkt.hwsrc = pmac_src eth_pkt.src = pmac_src msg = of.ofp_packet_out() msg.actions.append(of.ofp_action_output(port = of.OFPP_TABLE)) msg.data = eth_pkt.pack() core.openflow.connections[cs[0]].send(msg)
def createARPResponse(self,pkt_eth,hw_addr): '''Get ARP query ethernet frame and create ARP response''' hw_src = addNetIDEthAddr(hw_addr,self.net_id) eth = pkt.ethernet(dst=pkt_eth.src,src=hw_src) eth.type = pkt.ethernet.ARP_TYPE arp = pkt.arp(opcode=pkt.arp.REPLY) arp.hwsrc = hw_src arp.hwdst = pkt_eth.src arp.protosrc = pkt_eth.payload.protodst arp.protodst = pkt_eth.payload.protosrc eth.payload = arp return eth
def arp_send1(self, packet, packet_in): arpPacketProtocol = packet.payload if arpPacketProtocol.opcode == pkt.arp.REPLY: log.debug("ARP reply reveived") self.mac_to_port1[packet.src] = packet_in.in_port log.debug("Display the mac_to_port dictionary)(REPLY)") #log.debug(self.mac_to_port) self.act_like_switch1(packet, packet_in) elif arpPacketProtocol.opcode == pkt.arp.REQUEST: if str(arpPacketProtocol.protodst) == "10.0.1.1": # ARP reply arpResponse = pkt.arp() arpResponse.hwsrc = adr.EthAddr("10:10:10:10:10:10") arpResponse.hwdst = arpPacketProtocol.hwsrc arpResponse.opcode = pkt.arp.REPLY arpResponse.protosrc = arpPacketProtocol.protodst arpResponse.protodst = arpPacketProtocol.protosrc #add header to the arp packet, usign the ethernet frame arpFrame = pkt.ethernet() arpFrame.type = pkt.ethernet.ARP_TYPE arpFrame.dst = packet.src arpFrame.src = adr.EthAddr("10:10:10:10:10:10") arpFrame.payload = arpResponse #send the packet msg = of.ofp_packet_out() msg.data = arpFrame.pack() action = of.ofp_action_output(port=packet_in.in_port) msg.actions.append(action) self.connection.send(msg) #add the mac address to the port table## self.mac_to_port1[packet.src] = packet_in.in_port log.debug("Display the mac_to_port dictionary(REQUEST)") log.debug(self.mac_to_port1) else: find_ip = 0 for ip in self.network1.keys(): if ip == str(arpPacketProtocol.protodst): find_ip = ip if find_ip == 0: log.debug( "ARP: The dst ip is not in the network, we can't find it's mac address" ) else: self.act_like_switch1(packet, packet_in) else: pass
def createGratuitousARP(self): eth = pkt.ethernet(dst=pkt.ETHER_BROADCAST, src=self.hw_addr) eth.type = pkt.ethernet.VLAN_TYPE vlan = pkt.vlan(id=self.vstag) vlan.eth_type = pkt.ethernet.ARP_TYPE grat = pkt.arp() grat.opcode = pkt.arp.REQUEST grat.hwsrc = eth.src grat.hwdst = eth.dst grat.payload = b'\x0f' * 8 eth.payload = vlan vlan.payload = grat return eth
def create_arp_reply(self, header,pkt): arp_reply = pktlib.arp() #creates an empty header to use to send reply arp_reply.opcode = pktlib.arp.REPLY #fills in opcode arp_reply.hwsrc = self.ip_eth_dict[header.protodst] #is mac addr connected to protodst from REQ arp_reply.hwdst = header.hwsrc #hwdst is hwsrc from REQ arp_reply.protosrc = header.protodst #protosrc is dst from REQ arp_reply.protodst = header.protosrc #protodst is src from REQ ether = ethernet() #creates an ethernet packet ether.type = ethernet.ARP_TYPE #NECESSARY?? ether.src = arp_reply.hwsrc #SRC, is this right??? ether.dst = pkt.src #broadcasts message out...? ether.set_payload(arp_reply) #adds the payload which == None return ether
def createGratuitousARP(self): eth = pkt.ethernet(dst=pkt.ETHER_BROADCAST,src=self.hw_addr) eth.type = pkt.ethernet.VLAN_TYPE vlan = pkt.vlan(id=self.vstag) vlan.eth_type = pkt.ethernet.ARP_TYPE grat = pkt.arp() grat.opcode = pkt.arp.REQUEST grat.hwsrc = eth.src grat.hwdst = eth.dst grat.payload = b'\x0f' * 8 eth.payload=vlan vlan.payload=grat return eth
def arp_query(self, src_interface, dst_interface, payload_content=None): arp_req = arp() hw_src = self._choose_eth_addr(src_interface) arp_req.hwsrc = hw_src arp_req.hwdst = EthAddr(b"\xff\xff\xff\xff\xff\xff") arp_req.opcode = arp.REQUEST arp_req.protosrc = self._choose_ip_addr(src_interface) arp_req.protodst = self._choose_ip_addr(dst_interface) ether = ethernet() ether.type = ethernet.ARP_TYPE ether.dst = EthAddr(b"\xff\xff\xff\xff\xff\xff") ether.src = hw_src ether.payload = arp_req return ether
def send_arp_response(packet,match,event,dstMac): r = arp() r.opcode = arp.REPLY r.hwdst = match.dl_src r.protosrc = match.nw_dst r.protodst = match.nw_src r.hwsrc = dstMac e = ethernet(type=ethernet.ARP_TYPE, src=r.hwsrc, dst=r.hwdst) e.set_payload(r) msg = of.ofp_packet_out() msg.data = e.pack() msg.actions.append(of.ofp_action_output(port = of.OFPP_IN_PORT)) msg.in_port = event.port event.connection.send(msg)
def _handle_PacketIn (event): # after receive the PacketIn message, handle it. get the information about the packet, get the #Learn the desintation IP address and fill up routing table, according to my store of edge list, get the port number #I need to handle the ARP request from each subnet first. packet = event.parsed #print packet.src if packet.type == ethernet.IPV6_TYPE: return srcSwitch = "s" + str(event.dpid) print srcSwitch print packet.type,event.port #match = of.ofp_match.from_packet(packet) if isinstance(packet.next, arp): #This solves the problem of turning every ARP into IP packets a = packet.next #destinationIP = a.protodst #dstIPtest = getKey(destinationIP) #test = network1.GetPortNumAndMacAddr(srcSwitch, dstIPtest) if a.prototype == arp.PROTO_TYPE_IP: if a.hwtype == arp.HW_TYPE_ETHERNET: if a.protosrc != 0: arpTable[str(a.protosrc)] = packet.src print arpTable _send_paused_traffic(event.dpid,str(a.protosrc),event.port) if a.opcode == a.REQUEST: if str(a.protodst) in arpTable: r = pkt.arp() r.hwtype = a.hwtype r.prototype = a.prototype r.hwlen = a.hwlen r.protolen = a.protolen r.opcode = a.REPLY r.hwdst = a.hwsrc #r.hwsrc = switchMac[a.protodst] r.hwsrc = arpTable[str(a.protodst)] r.protodst = a.protosrc #print a.protodst.toRaw #print (r.protodst.toStr == "192.168.70.2") #if(r.protodst == IPAddr("192.168.70.2")): #if(r.protodst == IPAddr("192.168.10.1")): #r.hwsrc = EthAddr("52:fa:e1:4c:d1:6c") r.protosrc = a.protodst #a.protodst is the destination IP addresses e = pkt.ethernet(type=packet.type, src=r.hwsrc, dst=a.hwsrc) e.payload = r msg = of.ofp_packet_out() msg.data = e.pack() msg.actions.append(of.ofp_action_output(port = of.OFPP_IN_PORT)) msg.in_port = event.port event.connection.send(msg) else: msg = of.ofp_packet_out(in_port = event.port, action = of.ofp_action_output(port = of.OFPP_IN_PORT)) event.connection.send(msg)
def make_arp(self, packet): p = packetlib.ethernet() p.src = packet["srcmac"] p.dst = packet["dstmac"] p.type = packetlib.ethernet.ARP_TYPE p.next = packetlib.arp(prev=p) p.next.hwsrc = packet["srcmac"] p.next.hwdst = packet["dstmac"] p.next.protosrc = packet["srcip"] p.next.protodst = packet["dstip"] p.next.opcode = packet['protocol'] return p
def send_arp_request(self, IPdestinationAddress, IPsourceAddress): print "SEND_ARP_REQUEST" outInterface = self.routingTable[ IPdestinationAddress] #prelevo interfaccia di uscita dalla routing table dato l'ip di destinazione arp_request = pkt.arp() arp_request.hwsrc = EthAddr("1a:72:4f:e7:0f:46") arp_request.opcode = pkt.arp.REQUEST arp_request.protosrc = IPsourceAddress #ip del sorgente arp_request.protodst = IPdestinationAddress ether = pkt.ethernet() ether.type = pkt.ethernet.ARP_TYPE ether.dst = pkt.ETHER_BROADCAST #il pkt viene inviato in broadcast e solo l'host interessato risponderà ether.src = EthAddr("1a:72:4f:e7:0f:46") ether.payload = arp_request self.resend_packet(ether, outInterface)
def make_pox_arp(self, packet): p = packetlib.ethernet() p.src = packetaddr.EthAddr(packet["srcmac"].to_bytes()) p.dst = packetaddr.EthAddr(packet["dstmac"].to_bytes()) p.type = 2054 p.next = packetlib.arp(prev=p) p.next.hwsrc = packetaddr.EthAddr(packet["srcmac"].to_bytes()) p.next.hwdst = packetaddr.EthAddr(packet["dstmac"].to_bytes()) p.next.protosrc = packetaddr.IPAddr(packet["srcip"].to_bytes()) p.next.protodst = packetaddr.IPAddr(packet["dstip"].to_bytes()) p.next.opcode = packet['protocol'] return p
def send_server_arp(self, server): arppkt = pkt.ethernet( type = pkt.ethernet.ARP_TYPE, src = self.mac, dst = pkt.ETHER_BROADCAST) arppkt.payload = pkt.arp( opcode = pkt.arp.REQUEST, hwtype = pkt.arp.HW_TYPE_ETHERNET, prototype = pkt.arp.PROTO_TYPE_IP, hwsrc = self.mac, protodst = server, protosrc = self.svc_ip) msg = of.ofp_packet_out( data = arppkt.pack(), action = of.ofp_action_output(port = of.OFPP_FLOOD)) self.connection.send(msg)
def ARP_Request_handler(self, packet, packet_in): #make ARP reply arp_reply = pkt.arp() arp_reply.hwsrc = adr.EthAddr('00:00:00:00:00:10') arp_reply.hwdst = packet.payload.hwsrc arp_reply.opcode = pkt.arp.REPLY arp_reply.protosrc = packet.payload.protodst arp_reply.protodst = packet.payload.protosrc #make ARP packet -> Frame ether = pkt.ethernet() ether.type = pkt.ethernet.ARP_TYPE ether.dst = packet.src ether.src = packet.dst ether.payload = arp_reply #send Frame self.send_Packet(frame=ether, out_port=packet_in.in_port)
def _handle_PacketIn(self, event): if event.dpid not in [1, 2, 7, 8]: return packet = event.parsed """Add MAC & IP to Tables""" self.mac_to_port[packet.src] = event.port if (self.mac_to_ip.get(packet.src, None)) is None: self.mac_to_ip[packet.src] = "10.10.{:d}.{:d}".format( (6 - event.dpid % 2), self.addr) self.addr += 1 """Catch and Process ARP Packets""" if isinstance(packet.next, pkt.arp): if event.dpid not in [1, 2, 7, 8]: return elif packet.next.opcode == pkt.arp.REQUEST: #print (packet.next.hwtype, packet.next.prototype, str(packet.next.hwsrc), str(packet.next.hwdst), str(packet.next.hwlen), packet.next.opcode, packet.next.protolen, str(packet.next.protosrc), str(packet.next.protodst)); r = pkt.arp() r.hwtype = packet.next.hwtype r.prototype = packet.next.prototype r.hwlen = packet.next.hwlen r.protolen = packet.next.protolen r.opcode = pkt.arp.REPLY r.hwdst = packet.next.hwsrc mac = self.arp_table.get(packet.next.protodst, None) if mac is not None: r.hwsrc = mac r.protodst = packet.next.protosrc r.protosrc = packet.next.protodst e = pkt.ethernet(type=packet.type, src=r.hwsrc, dst=r.hwdst) e.payload = r log.info("{:s} answering ARP for {:s}".format( dpid_to_str(event.dpid), str(r.protosrc))) msg = of.ofp_packet_out( data=e.pack(), in_port=event.port, actions=of.ofp_action_output(port=of.OFPP_LOCAL)) self.connection.send(msg) """Catch and Process IPv4 Packets""" if isinstance(packet.next, pkt.ipv4): #packet.next.ttl -= 1; log.debug("({:d}.{:d}): {:s} -> {:s}".format( event.dpid, event.port, packet.next.srcip, packet.next.dstip)) return
def _handle_arp(self, event, ether_pkt, arp_pkt): src_ip = arp_pkt.protosrc src_mac = ether_pkt.src # Update the ARP table with the info for the sender. self._arp_table.add(src_ip, src_mac) if arp_pkt.opcode == arp.REQUEST: # Try to find a known MAC address for the requested IP address and send a reply ourselves. requested_ip = arp_pkt.protodst requested_mac = self._arp_table.lookup(requested_ip) self._packet_logger.metric('ARP Target', [ ('IP', requested_ip), ('MAC', requested_mac) ]) if requested_mac: self._packet_logger.action('ARP Reply', [ ('Requested MAC', requested_mac) ]) arp_reply = arp(hwsrc = requested_mac, hwdst = src_mac, opcode = arp.REPLY, protosrc = requested_ip, protodst = src_ip) ether = ethernet(type = ethernet.ARP_TYPE, dst = src_mac, src = requested_mac, payload = arp_reply) self._send_packet(ether, event.ofp.in_port) return True
def send_gratuitous_arps(self): '''Send ARPs for our own interfaces to each connected node''' for pnum,pinfo in self.ports.iteritems(): # construct an ARP request for one of our known interfaces. # controller isn't included in any of these ports, so these # are only ports connected to other switches arp = pktlib.arp() arp.opcode = pktlib.arp.REQUEST arp.hwsrc = EthAddr(pinfo.localmac) arp.protosrc = int(IPv4Address(pinfo.localip)) arp.protodst = int(IPv4Address(pinfo.remoteip)) ethernet = pktlib.ethernet() ethernet.dst = pktlib.ETHER_BROADCAST ethernet.src = EthAddr(pinfo.localmac) ethernet.payload = arp ethernet.type = ethernet.ARP_TYPE flet = packet_to_flowlet(ethernet) pinfo.link.flowlet_arrival(flet, self.name, pinfo.link.egress_node_name)
def send_arp_response(self, packet, match, event): # reply to ARP request #import pdb; pdb.set_trace() r = arp() r.opcode = arp.REPLY r.hwdst = match.dl_src r.protosrc = match.nw_dst r.protodst = match.nw_src r.hwsrc = self.port2Mac[event.port] self.arpTable[match.nw_src] = match.dl_src e = ethernet(type=packet.ARP_TYPE, src=r.hwsrc, dst=r.hwdst) e.set_payload(r) log.debug("%s:%i %i answering ARP for %s" % (self.name, event.dpid, event.port, str(r.protosrc))) msg = of.ofp_packet_out() msg.data = e.pack() msg.actions.append(of.ofp_action_output(port = of.OFPP_IN_PORT)) msg.in_port = event.port event.connection.send(msg)
def send_instead_arp_reply (event, strmacaddr, arp_packet) : arp_reply = pkt.arp () arp_reply.hwsrc = of.EthAddr (strmacaddr) arp_reply.hwdst = arp_packet.hwsrc arp_reply.opcode = pkt.arp.REPLY arp_reply.protosrc = arp_packet.protodst arp_reply.protodst = arp_packet.protosrc ether = pkt.ethernet () ether.type = pkt.ethernet.ARP_TYPE ether.src = of.EthAddr (strmacaddr) ether.dst = arp_packet.hwsrc ether.payload = arp_reply msg = of.ofp_packet_out () msg.actions.append (of.ofp_action_output (port = event.port)) msg.data = ether event.connection.send (msg)
def reply_arp(cls, event, mac_addr): arp_reply = pkt.arp() arp_reply.hwsrc = mac_addr arp_reply.hwdst = event.parsed.src arp_reply.opcode = pkt.arp.REPLY arp_reply.protosrc = event.parsed.payload.protodst arp_reply.protodst = event.parsed.payload.protosrc ether = pkt.ethernet() ether.type = pkt.ethernet.ARP_TYPE ether.dst = event.parsed.src ether.src = mac_addr ether.payload = arp_reply #send this packet to the switch msg = of.ofp_packet_out() msg.data = ether.pack() msg.actions.append(of.ofp_action_output(port = of.OFPP_IN_PORT)) msg.in_port = event.port event.connection.send(msg)