def generate_example_trace_same_subnet(): # TODO: highly redundant trace = [] (patch_panel, switches, network_links, hosts, access_links) = topo_gen.create_mesh(num_switches=2) packet_events = [] ping_or_pong = "ping" for access_link in access_links: other_host = (set(hosts) - set([access_link.host])).pop() eth = ethernet(src=access_link.host.interfaces[0].mac,dst=other_host.interfaces[0].mac,type=ethernet.IP_TYPE) dst_ip_addr = other_host.interfaces[0].ips[0] ipp = ipv4(protocol=ipv4.ICMP_PROTOCOL, srcip=access_link.host.interfaces[0].ips[0], dstip=dst_ip_addr) if ping_or_pong == "ping": ping = icmp(type=TYPE_ECHO_REQUEST, payload=ping_or_pong) else: ping = icmp(type=TYPE_ECHO_REPLY, payload=ping_or_pong) ipp.payload = ping eth.payload = ipp packet_events.append(DataplaneEvent(access_link.interface, eth)) # ping ping (no responses) between fake hosts for _ in range(40): trace.append(packet_events[0]) trace.append(packet_events[1]) write_trace_log(trace, "traces/ping_pong_same_subnet.trace")
def generate_example_trace(): trace = [] mesh = topo.MeshTopology(num_switches=2) hosts = mesh.hosts access_links = mesh.access_links packet_events = [] ping_or_pong = "ping" for access_link in access_links: other_host = (set(hosts) - set([access_link.host])).pop() eth = ethernet(src=access_link.host.interfaces[0].hw_addr,dst=access_link.switch_port.hw_addr,type=ethernet.IP_TYPE) dst_ip_addr = other_host.interfaces[0].ips[0] ipp = ipv4(protocol=ipv4.ICMP_PROTOCOL, srcip=access_link.host.interfaces[0].ips[0], dstip=dst_ip_addr) if ping_or_pong == "ping": ping = icmp(type=TYPE_ECHO_REQUEST, payload=ping_or_pong) else: ping = icmp(type=TYPE_ECHO_REPLY, payload=ping_or_pong) ipp.payload = ping eth.payload = ipp packet_events.append(DataplaneEvent(access_link.interface, eth)) # ping ping (no responses) between fake hosts for _ in range(40): trace.append(packet_events[0]) trace.append(packet_events[1]) write_trace_log(trace, "dataplane_traces/ping_pong.trace")
def generate_example_trace(): trace = [] mesh = topo.MeshTopology(num_switches=2) hosts = mesh.hosts access_links = mesh.access_links packet_events = [] ping_or_pong = "ping" for access_link in access_links: other_host = (set(hosts) - set([access_link.host])).pop() eth = ethernet(src=access_link.host.interfaces[0].hw_addr, dst=access_link.switch_port.hw_addr, type=ethernet.IP_TYPE) dst_ip_addr = other_host.interfaces[0].ips[0] ipp = ipv4(protocol=ipv4.ICMP_PROTOCOL, srcip=access_link.host.interfaces[0].ips[0], dstip=dst_ip_addr) if ping_or_pong == "ping": ping = icmp(type=TYPE_ECHO_REQUEST, payload=ping_or_pong) else: ping = icmp(type=TYPE_ECHO_REPLY, payload=ping_or_pong) ipp.payload = ping eth.payload = ipp packet_events.append(DataplaneEvent(access_link.interface, eth)) # ping ping (no responses) between fake hosts for _ in range(40): trace.append(packet_events[0]) trace.append(packet_events[1]) write_trace_log(trace, "dataplane_traces/ping_pong.trace")
def make_ping(other_host): eth = ethernet(src=access_link.host.interfaces[0].hw_addr,dst=other_host.interfaces[0].hw_addr,type=ethernet.IP_TYPE) dst_ip_addr = other_host.interfaces[0].ips[0] ipp = ipv4(protocol=ipv4.ICMP_PROTOCOL, srcip=access_link.host.interfaces[0].ips[0], dstip=dst_ip_addr) if ping_or_pong == "ping": ping = icmp(type=TYPE_ECHO_REQUEST, payload=ping_or_pong) else: ping = icmp(type=TYPE_ECHO_REPLY, payload=ping_or_pong) ipp.payload = ping eth.payload = ipp return eth
def icmp_ping(self, interface, destination_interface): # randomly choose an in_port. e = ethernet() e.src = interface.hw_addr if destination_interface is not None: e.dst = destination_interface.hw_addr else: # TODO(cs): need a better way to create random MAC addresses e.dst = EthAddr(struct.pack("Q",self.random.randint(1,0xFF))[:6]) e.type = ethernet.IP_TYPE ipp = ipv4() ipp.protocol = ipv4.ICMP_PROTOCOL if hasattr(interface, 'ips'): ipp.srcip = self.random.choice(interface.ips) else: ipp.srcip = IPAddr(self.random.randint(0,0xFFFFFFFF)) if destination_interface is not None and hasattr(destination_interface, 'ips'): ipp.dstip = self.random.choice(destination_interface.ips) else: ipp.dstip = IPAddr(self.random.randint(0,0xFFFFFFFF)) ping = icmp() ping.type = self.random.choice([TYPE_ECHO_REQUEST,TYPE_ECHO_REPLY]) ping.payload = "PingPing" * 6 ipp.payload = ping e.payload = ipp return e
def envio_paquete_sonda(event, eth_packet, dst_port, src_port): global time_sleep while True: time.sleep(time_sleep) #log.debug("ENVIO PAQUETE SONDAAAAAAAAAAAAAA") icmp = pkt.icmp() icmp.type = pkt.TYPE_ECHO_REQUEST for interface in swpo.d[event.connection.dpid]: list1 = [time.time(), event.connection.dpid, interface] str1 = ','.join(str(j) for j in list1) echo = pkt.ICMP.echo(payload=str1) icmp.payload = echo ip_packet = eth_packet.payload i = pkt.ipv4(protocol=pkt.ipv4.ICMP_PROTOCOL, srcip=ip_packet.srcip, dstip=ip_packet.dstip) i.tos = 0x64 i.set_payload(icmp) e = pkt.ethernet(type=pkt.ethernet.IP_TYPE, src=eth_packet.src, dst=eth_packet.dst) e.set_payload(i) msg = of.ofp_packet_out(in_port=src_port) msg.data = e.pack() #log.debug("INTERFAZ: %s", interface) msg.actions.append(of.ofp_action_output(port=interface)) event.connection.send(msg) log.debug( "SE ENVIA PAQUETE SONDA: TOS: %s IP_SRC: %s IP_DEST: %s PROTOCOLO: %s PORT: %s" % (i.tos, ip_packet.srcip, ip_packet.dstip, pkt.ipv4.ICMP_PROTOCOL, of.OFPP_ALL))
def install_path(self, dst_sw, last_port, match, event): p = _get_path(self, dst_sw, event.port, last_port) if p is None: if (match.dl_type == pkt.ethernet.IP_TYPE and event.parsed.find('ipv4')): e = pkt.ethernet() e.src = EthAddr(dpid_to_str(self.dpid)) e.dst = match.dl_src e.type = e.IP_TYPE ipp = pkt.ipv4() ipp.protocol = ipp.ICMP_PROTOCOL ipp.srcip = match.nw_dst ipp.dstip = match.nw_src icmp = pkt.icmp() icmp.type = pkt.ICMP.TYPE_DEST_UNREACH icmp.code = pkt.ICMP.CODE_UNREACH_HOST orig_ip = event.parsed.find('ipv4') d = orig_ip.pack() d = d[:orig_ip.hl * 4 + 8] d = struct.pack("!HH", 0, 0) + d icmp.payload = d ipp.payload = icmp e.payload = ipp msg = of.ofp_packet_out() msg.actions.append(of.ofp_action_output(port=event.port)) msg.data = e.pack() self.connection.send(msg) return self._install_path(p, match, event.ofp) p = [(sw, out_port, in_port) for sw, in_port, out_port in p] self._install_path(p, match.flip())
def send_icmp_msg_large(self, event, src_ip=IP_ANY, dst_ip=IP_ANY, src_mac=ETHER_BROADCAST, dst_mac=ETHER_BROADCAST, payload=None, icmp_type=pkt.TYPE_ECHO_REPLY): icmp = pkt.icmp() icmp.type = icmp_type icmp.payload = payload # Make the IP packet around it ipp = pkt.ipv4() ipp.protocol = ipp.ICMP_PROTOCOL ipp.srcip = src_ip ipp.dstip = dst_ip e = pkt.ethernet() e.src = src_mac e.dst = dst_mac e.type = e.IP_TYPE ipp.payload = icmp e.payload = ipp 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)
def handle_unreachable(self, packet, packet_in): packet_unreachable = pkt.unreach() packet_unreachable.payload = packet.payload icmp_packet = pkt.icmp() icmp_packet.type = pkt.TYPE_DEST_UNREACH icmp_packet.payload = packet_unreachable ip_packet = pkt.ipv4() ip_packet.srcip = packet.payload.dstip ip_packet.dstip = packet.payload.srcip ip_packet.protocol = pkt.ipv4.ICMP_PROTOCOL ip_packet.payload = icmp_packet ether_packet = pkt.ethernet() ether_packet.type = pkt.ethernet.IP_TYPE ether_packet.dst = packet.src ether_packet.src = packet.dst ether_packet.payload = ip_packet msg = of.ofp_packet_out() msg.data = ether_packet.pack() action = of.ofp_action_output(port=packet_in.in_port) msg.actions.append(action) self.connection.send(msg)
def reachable_send(self, packet, packet_in): msgEcho = pkt.echo() msgEcho.seq = packet.payload.payload.payload.seq + 1 msgEcho.id = packet.payload.payload.payload.id #encapsulate the reachable ICMP packet icmpReachable = pkt.icmp() icmpReachable.type = pkt.TYPE_ECHO_REPLY icmpReachable.payload = msgEcho icmpPkt = pkt.ipv4() icmpPkt.srcip = packet.payload.dstip icmpPkt.dstip = packet.payload.srcip icmpPkt.protocol = pkt.ipv4.ICMP_PROTOCOL icmpPkt.payload = icmpReachable #encapsulate the packet into frame icmpFrame2 = pkt.ethernet() icmpFrame2.type = pkt.ethernet.IP_TYPE icmpFrame2.dst = packet.src icmpFrame2.src = packet.dst icmpFrame2.payload = icmpPkt msg = of.ofp_packet_out() msg.data = icmpFrame2.pack() action = of.ofp_action_output(port=packet_in.in_port) msg.actions.append(action) self.connection.send(msg)
def send_icmp_msg_large(self, event, src_ip = IP_ANY, dst_ip = IP_ANY, src_mac = ETHER_BROADCAST, dst_mac = ETHER_BROADCAST, payload = None, icmp_type = pkt.TYPE_ECHO_REPLY): icmp = pkt.icmp() icmp.type = icmp_type icmp.payload = payload # Make the IP packet around it ipp = pkt.ipv4() ipp.protocol = ipp.ICMP_PROTOCOL ipp.srcip = src_ip ipp.dstip = dst_ip e = pkt.ethernet() e.src = src_mac e.dst = dst_mac e.type = e.IP_TYPE ipp.payload = icmp e.payload = ipp 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)
def send_icmp_msg_small(self, packet, match, event, icmp_type = pkt.TYPE_ECHO_REPLY, payload = None): pload = payload if payload is not None or packet is None or packet.find("icmp") is None else packet.find("icmp").payload return self.send_icmp_msg_large(event, packet.find("ipv4").dstip, packet.find("ipv4").srcip, packet.dst, packet.src, pload, icmp_type) 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 e = pkt.ethernet() e.src = packet.dst e.dst = packet.src e.type = e.IP_TYPE ipp.payload = icmp e.payload = ipp 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 handleIP(self, event): dpid = self.connection.dpid packet = event.parsed ipLoad = packet.payload dstip = ipLoad.dstip log.debug('-----It is an ip packet-----' + str(dstip)) if ifEcho(ipLoad, dstip, self, event) != True: if ifReach(dstip, event, self) != True: reply = icmp() reply.type = TYPE_DEST_UNREACH reply.code = CODE_UNREACH_NET payLoad = ipLoad.pack()[:ipLoad.iplen + 8] import struct payLoad = struct.pack("!I", 0) + payLoad reply.payload = payLoad ipShell = ipv4() ipShell.protocol = ipv4.ICMP_PROTOCOL for portIP in portsIP[dpid]: selfip = portIP[PORT_IP] if (event.port == portIP[pPORT]): ipShell.srcip = selfip break ipShell.dstip = ipLoad.srcip ipShell.payload = reply etherShell = ethernet() etherShell.type = ethernet.IP_TYPE etherShell.src = packet.dst etherShell.dst = packet.src etherShell.payload = ipShell msg = of.ofp_packet_out() msg.data = etherShell.pack() msg.actions.append(of.ofp_action_output(port=event.port)) self.connection.send(msg)
def icmp_unknownhost(self, p, interfaceip, out_port): global icmpf, ip_pakt, eth u = icmp() u.type = TYPE_DEST_UNREACH u.code = 1 orig_ip = p.payload d = orig_ip.pack() d = d[:orig_ip.hl * 4 + 8] d = struct.pack("!HH", 0, 0) + d # network, unsigned short, unsigned short u.payload = d ip_pakt = ipv4() ip_pakt.protocol = ip_pakt.ICMP_PROTOCOL ip_pakt.srcip = interfaceip ip_pakt.dstip = p.payload.srcip #log.debug('Forward ICMP %s from src_IP %s and dst_IP %s' %(icmp_type, ip_pakt.srcip, ip_pakt.dstip)) eth = ethernet() eth.src = p.dst eth.dst = p.src eth.type = eth.IP_TYPE ip_pakt.payload = u eth.payload = ip_pakt self.resend_packet(eth, out_port)
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 tests(): f = Firewall() ip = ipv4() ip.srcip = IPAddr("172.16.42.1") ip.dstip = IPAddr("172.16.42.35") # 17 for udp, 6 tcp, 1 icmp ip.protocol = 6 icmppkt = pktlib.icmp() icmppkt.type = pktlib.TYPE_ECHO_REQUEST icmppkt.payload ="Hello, world" xudp = udp() xudp.srcport = 53 xudp.dstport = 53 xudp.payload = "Hello, world" xudp.len = 8 + len(xudp.payload) tcppkt = pktlib.tcp() tcppkt.SYN = 1 tcppkt.seq = 14 tcppkt.srcport = 80 tcppkt.dstport = 80 tcppkt.offset = 5 tcppkt.payload = "Hello, world" tcppkt.tcplen = 20 # ip.payload = xudp # ip.payload = icmppkt ip.payload = tcppkt f=Firewall() f.mainframe(ip, time.time())
def reply_to_ping(self, event): # Reply to pings packet=event.parsed # 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 packet=event.parsed 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 unreachable_send(self, packet, packet_in): msgUnreachable = pkt.unreach() msgUnreachable.payload = packet.payload icmpReachable = pkt.icmp() icmpReachable.type = pkt.TYPE_DEST_UNREACH icmpReachable.payload = msgUnreachable #encapsulate reachable ICMP packet icmpPkt = pkt.ipv4() icmpPkt.srcip = packet.payload.dstip #change the source ip to router's ip icmpPkt.dstip = packet.payload.srcip icmpPkt.protocol = pkt.ipv4.ICMP_PROTOCOL icmpPkt.payload = icmpReachable #encapsulate packet into frame icmpFrame = pkt.ethernet() icmpFrame.type = pkt.ethernet.IP_TYPE icmpFrame.dst = packet.src icmpFrame.src = packet.dst icmpFrame.payload = icmpPkt msg = of.ofp_packet_out() msg.data = icmpFrame.pack() action = of.ofp_action_output(port=packet_in.in_port) msg.actions.append(action) self.connection.send(msg)
def handle_reachable(self, packet, packet_in): # echo echo = pkt.echo() echo.seq = packet.payload.payload.payload.seq + 1 echo.id = packet.payload.payload.payload.id # icmp pakcet icmp_packet = pkt.icmp() icmp_packet.type = pkt.TYPE_ECHO_REPLY icmp_packet.payload = echo # ip packet ip_packet = pkt.ipv4() ip_packet.srcip = packet.payload.dstip ip_packet.dstip = packet.payload.srcip ip_packet.protocol = pkt.ipv4.ICMP_PROTOCOL ip_packet.payload = icmp_packet # ethernet packet ether_packet = pkt.ethernet() ether_packet.type = pkt.ethernet.IP_TYPE ether_packet.dst = packet.src ether_packet.src = packet.dst ether_packet.payload = ip_packet msg = of.ofp_packet_out() msg.data = ether_packet.pack() action = of.ofp_action_output(port=packet_in.in_port) msg.actions.append(action) self.connection.send(msg)
def send_ping(self, dpid=1, eth_dst=ETHER_ANY): con = core.openflow.getConnection(dpid) icmp = pkt.icmp() icmp.type = pkt.TYPE_ECHO_REQUEST echo = pkt.ICMP.echo(payload="SENDING PING") icmp.payload = echo #Make the IP packet around it ipp = pkt.ipv4() ipp.protocol = ipp.ICMP_PROTOCOL # Ethernet around that... e = pkt.ethernet() e.dst = eth_dst e.type = e.IP_TYPE # Hook them up... ipp.payload = icmp e.payload = ipp msg = of.ofp_packet_out() msg.actions.append(of.ofp_action_output(port=of.OFPP_TABLE)) msg.data = e.pack() con.send(msg) self.previous_ping_returned = False log.info("Sending ping to switch %s" % dpid)
def generate_example_trace_fat_tree(num_pods=4): # TODO(cs): highly redundant fat_tree = topo.FatTree(num_pods) (_, _, hosts, access_links) = (fat_tree.switches, fat_tree.network_links, fat_tree.hosts, fat_tree.access_links) host2pings = defaultdict(lambda: []) payload = "ping" for access_link in access_links: host = access_link.host other_hosts = list((set(hosts) - set([access_link.host]))) for other_host in other_hosts: eth = ethernet(src=access_link.host.interfaces[0].hw_addr,dst=other_host.interfaces[0].hw_addr,type=ethernet.IP_TYPE) dst_ip_addr = other_host.interfaces[0].ips[0] ipp = ipv4(protocol=ipv4.ICMP_PROTOCOL, srcip=access_link.host.interfaces[0].ips[0], dstip=dst_ip_addr) ping = icmp(type=TYPE_ECHO_REQUEST, payload=payload) ipp.payload = ping eth.payload = ipp host2pings[host].append(DataplaneEvent(access_link.interface, eth)) # ping pong (no responses) between fake hosts # (Some large number: TODO(cs): serialize a generator to disk) # Trace is [one ping from every host to a random other host] * 50000 trace = [] for _ in range(50000): for host, pings in host2pings.iteritems(): trace.append(random.choice(pings)) write_trace_log(trace, "dataplane_traces/ping_pong_fat_tree.trace")
def generate_example_trace_fat_tree(num_pods=4): # TODO(cs): highly redundant fat_tree = topo.FatTree(num_pods) (_, _, hosts, access_links) = (fat_tree.switches, fat_tree.network_links, fat_tree.hosts, fat_tree.access_links) host2pings = defaultdict(lambda: []) payload = "ping" for access_link in access_links: host = access_link.host other_hosts = list((set(hosts) - set([access_link.host]))) for other_host in other_hosts: eth = ethernet(src=access_link.host.interfaces[0].hw_addr, dst=other_host.interfaces[0].hw_addr, type=ethernet.IP_TYPE) dst_ip_addr = other_host.interfaces[0].ips[0] ipp = ipv4(protocol=ipv4.ICMP_PROTOCOL, srcip=access_link.host.interfaces[0].ips[0], dstip=dst_ip_addr) ping = icmp(type=TYPE_ECHO_REQUEST, payload=payload) ipp.payload = ping eth.payload = ipp host2pings[host].append(DataplaneEvent(access_link.interface, eth)) # ping pong (no responses) between fake hosts # (Some large number: TODO(cs): serialize a generator to disk) # Trace is [one ping from every host to a random other host] * 50000 trace = [] for _ in range(50000): for host, pings in host2pings.iteritems(): trace.append(random.choice(pings)) write_trace_log(trace, "dataplane_traces/ping_pong_fat_tree.trace")
def send_ping(self): con = core.openflow.getConnection(1) icmp = pkt.icmp() icmp.type = pkt.TYPE_ECHO_REQUEST echo = pkt.ICMP.echo(payload="SENDING PING") icmp.payload = echo #Make the IP packet around it ipp = pkt.ipv4() ipp.protocol = ipp.ICMP_PROTOCOL #ipp.srcip = IPAddr("127.2.0.1") #ipp.dstip = IPAddr("10.1.0.1") # Ethernet around that... e = pkt.ethernet() #e.src = EthAddr("02:00:DE:AD:BE:EF") #e.dst = EthAddr("00:00:00:00:00:11") e.type = e.IP_TYPE # Hook them up... ipp.payload = icmp e.payload = ipp msg = of.ofp_packet_out() msg.actions.append(of.ofp_action_output(port=of.OFPP_TABLE)) msg.data = e.pack() con.send(msg) log.debug("%s pinged %s", ipp.srcip, ipp.dstip)
def perform(self, event): packet = event.parse() n = packet.find("ipv4") i = packet.find("icmp") # Create echo reply icmp. icmp = pkt.icmp() icmp.type = pkt.TYPE_ECHO_REPLY icmp.payload = i.payload # Create ipv4 packet ipp. ipp = pkt.ipv4() ipp.protocol = ipp.ICMP_PROTOCOL ipp.srcip = n.dstip ipp.dstip = n.srcip # Create ethernet packet e. e = pkt.ethernet() e.src = packet.dst e.dst = packet.src e.type = e.IP_TYPE # Link the payloads. ipp.payload = icmp e.payload = ipp # 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 send_unreachable(self, event): packet, packet_in, dpid = getpacket(event) icmp = pkt.icmp() ip = packet.find('ipv4') rte = self.find_route(ip.srcip, dpid) if rte == None: return dstip = ip.dstip payload = ip.pack() payload = payload[:ip.hl * 4 + 8] payload = struct.pack("!HH", 0, 0) + payload icmp.payload = payload icmp.type = pkt.TYPE_DEST_UNREACH ip = pkt.ipv4() ip.protocol = ip.ICMP_PROTOCOL ip.srcip = IPAddr(rte[3]) ip.dstip = packet.find('ipv4').srcip ip.payload = icmp eth = pkt.ethernet() eth.src = packet.dst eth.dst = packet.src eth.type = eth.IP_TYPE eth.payload = ip self.resend_packet(eth.pack(), packet_in.in_port, dpid) log.debug("%s is unreachable to %s", str(dstip), str(ip.dstip))
def handle_icmp(self, event): packet, packet_in, dpid = getpacket(event) dstip = str(packet.find('ipv4').dstip) srcip = str(packet.find('ipv4').srcip) log.debug("recv icmp dst=%s src=%s", dstip, srcip) if self.find_route(dstip, dpid) == None: rte = self.find_route(srcip, dpid) return self.send_unreachable(event, rte) i = packet.find('icmp') if i.type == pkt.TYPE_ECHO_REQUEST: icmp = pkt.icmp() icmp.payload = i.payload icmp.type = pkt.TYPE_ECHO_REPLY ip = pkt.ipv4() ip.protocol = ip.ICMP_PROTOCOL ip.srcip = packet.find('ipv4').dstip ip.dstip = packet.find('ipv4').srcip ip.payload = icmp eth = pkt.ethernet() eth.src = packet.dst eth.dst = packet.src eth.type = eth.IP_TYPE eth.payload = ip self.resend_packet(eth.pack(), packet_in.in_port, dpid) log.debug("send icmp reply to %s port %d", str(ip.dstip), packet_in.in_port)
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 Ping(s1,s2): global t1 port1 = 0 port2 = 0 mac1 = 0 mac2 = 0 ip1 = 0 ip2 = 0 for item in Switch_set: if s1 == item: for item1 in Switch_set[item]: if s2 == Switch_set[item][item1][0]: port1 = item1 port2 = Switch_set[item][item1][1] break else: continue # print s1,s2 for dpid in ArpTable: if s1 == dpid: for ip in ArpTable[dpid]: if ArpTable[dpid][ip][0] == port1: ip1 = ip mac1 = ArpTable[dpid][ip][1] break elif s2 == dpid: for ip in ArpTable[dpid]: if ArpTable[dpid][ip][0] == port2: ip2 = ip mac2 = ArpTable[dpid][ip][1] break for i in [ip1,ip2,mac1,mac2]: if not i: return False icmp = pkt.icmp() icmp.type = pkt.ICMP.TYPE_ECHO_REQUEST icmp.payload = "PingPing" * 6 ipp = pkt.ipv4() ipp.protocol = ipv4.ICMP_PROTOCOL ipp.srcip = IPAddr(ip1) ipp.dstip = IPAddr(ip2) ipp.payload = icmp e = pkt.ethernet() e.src = mac1 e.dst = mac2 e.type = ethernet.IP_TYPE e.payload = ipp # print "Send Packets!" # print mac1,mac2,ip1,ip2 for i in range(10): msg = of.ofp_packet_out() msg.actions.append(of.ofp_action_output(port = port2)) msg.data = e.pack() core.openflow.sendToDPID(str_to_dpid(s2), msg) t1[(s1,s2)]+=time.time() # print t1 return True
def install_path(self, dst_sw, last_port, match, event): """ Attempts to install a path between this switch and some destination """ p = _get_path(self, dst_sw, event.port, last_port) src = (self.dpid, event.port) dst = (dst_sw.dpid, last_port) core.l2_multi.raiseEvent(PathInstalled(p, src, dst)) if p is None: log.warning("Can't get from %s to %s", match.dl_src, match.dl_dst) import pox.lib.packet as pkt if (match.dl_type == pkt.ethernet.IP_TYPE and event.parsed.find('ipv4')): # It's IP -- let's send a destination unreachable log.debug("Dest unreachable (%s -> %s)", match.dl_src, match.dl_dst) from pox.lib.addresses import EthAddr e = pkt.ethernet() e.src = EthAddr(dpid_to_str(self.dpid)) #FIXME: Hmm... e.dst = match.dl_src e.type = e.IP_TYPE ipp = pkt.ipv4() ipp.protocol = ipp.ICMP_PROTOCOL ipp.srcip = match.nw_dst #FIXME: Ridiculous ipp.dstip = match.nw_src icmp = pkt.icmp() icmp.type = pkt.ICMP.TYPE_DEST_UNREACH icmp.code = pkt.ICMP.CODE_UNREACH_HOST orig_ip = event.parsed.find('ipv4') d = orig_ip.pack() d = d[:orig_ip.hl * 4 + 8] import struct d = struct.pack("!HH", 0, 0) + d #FIXME: MTU icmp.payload = d ipp.payload = icmp e.payload = ipp msg = of.ofp_packet_out() msg.actions.append(of.ofp_action_output(port=event.port)) msg.data = e.pack() self.connection.send(msg) return log.debug("Installing path for %s -> %s %04x (%i hops)", match.dl_src, match.dl_dst, match.dl_type, len(p)) # We have a path -- install it self._install_path(p, match, event.ofp) # Now reverse it and install it backwards # (we'll just assume that will work) p = [(sw, out_port, in_port) for sw, in_port, out_port in p] self._install_path(p, match.flip())
def _do_ping(self, event): eth = event.parsed icmpp = event.parsed.find('icmp') ipp = event.parsed.find('ipv4') if not icmpp or not ipp: self.log.warn("Expected ICMP packet wasn't ICMP") return oport = self._conn.ports.get(event.port) if oport is None: self.log.warn("Got ICMP from non-existent hardware port") return if oport.hw_addr != event.parsed.dst: # This shouldn't happen since we install table entries specifically # for our own ports! self.log.warn("Got ping with wrong Ethernet address") return port = self._ports.get(event.port) if port is None: self.log.warn("Got ICMP from non-existent port") return if ipp.dstip not in self.all_ips: # Unlike ARP, we use all_ips and not port.ips because we want to # respond to any of our IP addresses. # This shouldn't happen since we install table entries specifically # for our own ports! self.log.warn("Got ping with wrong IP address") return if icmpp.type == pkt.ICMP.TYPE_ECHO_REQUEST: echop = icmpp.payload if not isinstance(echop, pkt.ICMP.echo): self.log.warn("Expected ICMP echo wasn't ICMP echo") return # Make the ping reply r_icmp = pkt.icmp() r_icmp.type = pkt.TYPE_ECHO_REPLY r_icmp.payload = echop # Make the IP packet around it r_ipp = pkt.ipv4() r_ipp.protocol = ipp.ICMP_PROTOCOL r_ipp.srcip = ipp.dstip r_ipp.dstip = ipp.srcip # Ethernet around that... r_e = pkt.ethernet() r_e.src = oport.hw_addr r_e.dst = event.parsed.src r_e.type = r_e.IP_TYPE # Hook them up... r_ipp.payload = r_icmp r_e.payload = r_ipp # Send it back to the input port msg = of.ofp_packet_out() msg.actions.append(of.ofp_action_output(port=event.port)) msg.data = r_e.pack() event.connection.send(msg)
def makeICMP(self, errorType, codeType, ippkt): icmppkt = pktlib.icmp() icmppkt.type = errorType icmppkt.code = codeType icmppkt.payload = pktlib.unreach() icmppkt.payload.payload = ippkt.dump()[:28] return icmppkt
def tests(): f = Firewall() ip1 = ipv4() ip1.srcip = IPAddr("172.16.42.1") ip1.dstip = IPAddr("10.0.0.2") ip1.protocol = 17 xudp1 = udp() xudp1.srcport = 53 xudp1.dstport = 53 xudp1.payload = "Hello, world" xudp1.len = 8 + len(xudp1.payload) ip1.payload = xudp1 ip2 = ipv4() ip2.srcip = IPAddr("172.16.40.1") ip2.dstip = IPAddr("10.0.0.1") ip2.protocol = ip2.ICMP_PROTOCOL icmppkt = icmp() icmppkt.type = pktlib.TYPE_ECHO_REQUEST ping = pktlib.echo() ping.id = 5 ping.seq = 10 icmppkt.payload = ping ip2.payload = icmppkt ip3 = ipv4() ip3.srcip = IPAddr("172.16.38.0") ip3.dstip = IPAddr("10.0.0.5") ip3.protocol = ip2.ICMP_PROTOCOL icmppkt = icmp() icmppkt.type = pktlib.TYPE_ECHO_REQUEST ping = pktlib.echo() ping.id = 5 ping.seq = 10 icmppkt.payload = ping ip3.payload = icmppkt i=0 while i<10: print "+"*70 f.update_token_buckets() f.allow(ip2) f.allow(ip3) time.sleep(0.5) i+=1
def icmp_echo_reply(self, icmp_data, packet, packet_in): log.debug("ICMP: router get request") icmp_echo = pkt.echo(seq=icmp_data.payload.seq + 1, id=icmp_data.payload.id) icmp_reply = pkt.icmp(type=pkt.TYPE_ECHO_REPLY, payload=icmp_echo) self.send_icmp_packet(icmp_reply, packet, packet_in)
def install_path(self, dst_sw, last_port, match, event): #print 'install_path' """ Attempts to install a path between this switch and some destination """ p = _get_path(self, dst_sw, event.port, last_port, match) print 'the path is ' + str(p) if p is None: log.warning("Can't get from %s to %s", match.dl_src, match.dl_dst) import pox.lib.packet as pkt if (match.dl_type == pkt.ethernet.IP_TYPE and event.parsed.find('ipv4')): # It's IP -- let's send a destination unreachable log.debug("Dest unreachable (%s -> %s)", match.dl_src, match.dl_dst) from pox.lib.addresses import EthAddr e = pkt.ethernet() e.src = EthAddr(dpid_to_str(self.dpid)) #FIXME: Hmm... e.dst = match.dl_src e.type = e.IP_TYPE ipp = pkt.ipv4() ipp.protocol = ipp.ICMP_PROTOCOL ipp.srcip = match.nw_dst #FIXME: Ridiculous ipp.dstip = match.nw_src icmp = pkt.icmp() icmp.type = pkt.ICMP.TYPE_DEST_UNREACH icmp.code = pkt.ICMP.CODE_UNREACH_HOST orig_ip = event.parsed.find('ipv4') d = orig_ip.pack() d = d[:orig_ip.hl * 4 + 8] import struct d = struct.pack("!HH", 0, 0) + d #FIXME: MTU icmp.payload = d ipp.payload = icmp e.payload = ipp msg = of.ofp_packet_out() msg.actions.append(of.ofp_action_output(port=event.port)) msg.data = e.pack() self.connection.send(msg) return log.debug("Installing path for %s -> %s %04x (%i hops)", match.dl_src, match.dl_dst, match.dl_type, len(p)) # We have a path -- install it self._install_path(p, match, event.ofp) # Now reverse it and install it backwards # (we'll just assume that will work) p = [(sw, out_port, in_port) for sw, in_port, out_port in p] self._install_path(p, match.flip())
def destination_unreachable_icmp(self, packet, packet_in): data = packet.payload log.debug("ICMP: destination unreachable") dst_unreach = pkt.unreach() dst_unreach.payload = data icmp_reply = pkt.icmp() icmp_reply.type = pkt.TYPE_DEST_UNREACH icmp_reply.payload = dst_unreach self.send_icmp_packet(icmp_reply, packet, packet_in)
def _handle_vip_icmp(self, ether_pkt, ip_pkt, icmp_pkt, event): if icmp_pkt.type == TYPE_ECHO_REQUEST and ip_pkt.dstip == self._vip: # Always respond to echo requests to the VIP. vip_mac = self._get_mac_for_port(event.ofp.in_port) self.packet_logger.action('VIP ECHO REPLY', [ ('From MAC', vip_mac) ]) icmp_reply = icmp(type = TYPE_ECHO_REPLY, payload = icmp_pkt.payload) ipv4_wrapper = ipv4(protocol = ipv4.ICMP_PROTOCOL, srcip = self._vip, dstip = ip_pkt.srcip, payload = icmp_reply) ether_wrapper = ethernet(type = ethernet.IP_TYPE, src = vip_mac, dst = ether_pkt.src, payload = ipv4_wrapper) self._send_packet(ether_wrapper, event.ofp.in_port) return True return super(LoadBalancingSwitch, self).handle_icmp(ether_pkt, ip_pkt, icmp_pkt, event)
def makeEcho(self, request): icmppkt = pktlib.icmp() icmppkt.type = pktlib.TYPE_ECHO_REPLY reply = pktlib.echo() reply.id = request.id reply.seq = request.seq reply.payload = request.payload icmppkt.payload = reply return icmppkt
def install_path(self, dst_sw, last_port, match, event): #buffer_id, packet): """ Cria uma lista de Switches e os instala. :param dst_sw: Switch de destino. :param last_port: Ultima porta acessada. :param match: Configurações iniciais. :param event: Utiliza o tamanho do seu buffer na instalação.. :return: Sem retorno. """ p = _get_path(self, dst_sw, last_port) if p is None: log.warning("Can't get from %s to %s", match.dl_src, match.dl_dst) import pox.lib.packet as pkt if (match.dl_type == pkt.ethernet.IP_TYPE and event.parsed.find('ipv4')): # It's IP -- let's send a destination unreachable log.debug("Dest unreachable (%s -> %s)", match.dl_src, match.dl_dst) from pox.lib.addresses import EthAddr e = pkt.ethernet() e.src = EthAddr(dpidToStr(self.dpid)) #FIXME: Hmm... e.dst = match.dl_src e.type = e.IP_TYPE ipp = pkt.ipv4() ipp.protocol = ipp.ICMP_PROTOCOL ipp.srcip = match.nw_dst #FIXME: Ridiculous ipp.dstip = match.nw_src icmp = pkt.icmp() icmp.type = pkt.ICMP.TYPE_DEST_UNREACH icmp.code = pkt.ICMP.CODE_UNREACH_HOST orig_ip = event.parsed.find('ipv4') d = orig_ip.pack() d = d[:orig_ip.hl * 4 + 8] import struct d = struct.pack("!HH", 0, 0) + d #FIXME: MTU icmp.payload = d ipp.payload = icmp e.payload = ipp msg = of.ofp_packet_out() msg.actions.append(of.ofp_action_output(port=event.port)) msg.data = e.pack() self.connection.send(msg) return self._install_path(p, match, event.ofp.buffer_id) log.debug("Installing path for %s -> %s %04x (%i hops)", match.dl_src, match.dl_dst, match.dl_type, len(p))
def create_icmp_packet(self, src_ip, dst_ip, icmp_reply_type, icmp_payload): icmp_reply = pkt.icmp() icmp_reply.type = icmp_reply_type icmp_reply.payload = icmp_payload ip_p = pkt.ipv4() ip_p.srcip = dst_ip ip_p.dstip = src_ip ip_p.protocol = pkt.ipv4.ICMP_PROTOCOL ip_p.payload = icmp_reply return ip_p
def install_path (self, dst_sw, last_port, match, event): """ From l2_multi - tries to install a path between switch & some destination """ p = _get_path(self, dst_sw, event.port, last_port) if p is None: log.warning("Can't get from %s to %s", match.dl_src, match.dl_dst) import pox.lib.packet as pkt if (match.dl_type == pkt.ethernet.IP_TYPE and event.parsed.find('ipv4')): log.debug("Dest unreachable (%s -> %s)", match.dl_src, match.dl_dst) from pox.lib.addresses import EthAddr e = pkt.ethernet() e.src = EthAddr(dpid_to_str(self.dpid)) e.dst = match.dl_src e.type = e.IP_TYPE ipp = pkt.ipv4() ipp.protocol = ipp.ICMP_PROTOCOL ipp.srcip = match.nw_dst ipp.dstip = match.nw_src icmp = pkt.icmp() icmp.type = pkt.ICMP.TYPE_DEST_UNREACH icmp.code = pkt.ICMP.CODE_UNREACH_HOST orig_ip = event.parsed.find('ipv4') d = orig_ip.pack() d = d[:orig_ip.hl * 4 + 8] import struct d = struct.pack("!HH", 0,0) + d icmp.payload = d ipp.payload = icmp e.payload = ipp msg = of.ofp_packet_out() msg.actions.append(of.ofp_action_output(port = event.port)) msg.data = e.pack() self.connection.send(msg) return log.debug("Installing path for %s -> %s %04x (%i hops)", match.dl_src, match.dl_dst, match.dl_type, len(p)) # Install the path self._install_path(p, match, event.ofp) # Install the reverse path (assumed to work) p = [(sw,out_port,in_port) for sw,in_port,out_port in p] self._install_path(p, match.flip())
def import_rules(self): file = open("firewall_rules.txt", "r") for line in file: if len(line) != 1: #this is to get rid of the empty lines words = [] words = line.split() if words[0] == "permit" or words[0] == "deny": #make sure it is not a comment ruleObject = Rules() ruleObject.line = line for idx in range(len(words)): if words[idx] == "permit": ruleObject.action = "permit" if words[idx] == "deny": ruleObject.action = "deny" if words[idx] == "ip": ruleObject.type = type(pktlib.ipv4()) if words[idx] == "tcp": ruleObject.type = type(pktlib.tcp()) if words[idx] == "udp": ruleObject.type = type(pktlib.udp()) if words[idx] == "icmp": ruleObject.type = type(pktlib.icmp()) if words[idx] == "src": if words[idx+1] == "any": ruleObject.src = 1 else: ruleObject.src = words[idx+1] if "/" in ruleObject.src: #meaning its a net addr-we need the netmask temp = parse_cidr(ruleObject.src) srcnetMask = cidr_to_netmask(temp[1]) ruleObject.netMask = srcnetMask if words[idx] == "srcport": if words[idx+1] == "any": ruleObject.srcport = 1 else: ruleObject.srcport = words[idx+1] if words[idx] == "dst": if words[idx+1] == "any": ruleObject.dst = 1 else: ruleObject.dst = words[idx+1] if words[idx] == "dstport": if words[idx+1] == "any": ruleObject.dstport = 1 else: ruleObject.dstport = words[idx+1] if words[idx] == "ratelimit": ruleObject.ratelimit = words[idx+1] self.rules.append(ruleObject)
def ping(eth="00:18:02:6e:ce:55", ip="192.168.0.1"): e = pkt.ethernet() e.src = p.addresses["ethernet"]["addr"] or "02:00:00:11:22:33" e.dst = EthAddr(eth) e.type = e.IP_TYPE ipp = pkt.ipv4() ipp.protocol = ipp.ICMP_PROTOCOL ipp.srcip = p.addresses["AF_INET"]["addr"] ipp.dstip = IPAddr(ip) icmp = pkt.icmp() icmp.type = pkt.ICMP.TYPE_ECHO_REQUEST icmp.payload = "PingPing" * 6 ipp.payload = icmp e.payload = ipp p.inject(e)
def ping(eth='00:18:02:6e:ce:55', ip='192.168.0.1'): e = pkt.ethernet() e.src = p.addresses['ethernet']['addr'] or '02:00:00:11:22:33' e.dst = EthAddr(eth) e.type = e.IP_TYPE ipp = pkt.ipv4() ipp.protocol = ipp.ICMP_PROTOCOL ipp.srcip = p.addresses['AF_INET']['addr'] ipp.dstip = IPAddr(ip) icmp = pkt.icmp() icmp.type = pkt.ICMP.TYPE_ECHO_REQUEST icmp.payload = "PingPing" * 6 ipp.payload = icmp e.payload = ipp p.inject(e)
def ping (eth='00:18:02:6e:ce:55', ip='192.168.0.1'): e = pkt.ethernet() e.src = p.addresses['ethernet']['addr'] e.dst = EthAddr(eth) e.type = e.IP_TYPE ipp = pkt.ipv4() ipp.protocol = ipp.ICMP_PROTOCOL ipp.srcip = p.addresses['AF_INET']['addr'] ipp.dstip = IPAddr(ip) icmp = pkt.icmp() icmp.type = pkt.ICMP.TYPE_ECHO_REQUEST icmp.payload = "PingPing" * 6 ipp.payload = icmp e.payload = ipp p.inject(e)
def flowlet_to_packet(flowlet): if hasattr(flowlet, "origpkt"): return getattr(flowlet, "origpkt") ident = flowlet.ident.key etherhdr = pktlib.ethernet() etherhdr.src = EthAddr(flowlet.srcmac) etherhdr.dst = EthAddr(flowlet.dstmac) etherhdr.type = pktlib.ethernet.IP_TYPE ipv4 = pktlib.ipv4() ipv4.srcip = IPAddr(ident.srcip) ipv4.dstip = IPAddr(ident.dstip) ipv4.protocol = ident.ipproto ipv4.tos = flowlet.iptos iplen = flowlet.bytes / flowlet.pkts ipv4.iplen = iplen payloadlen = 0 etherhdr.payload = ipv4 if ident.ipproto == IPPROTO_ICMP: layer4 = pktlib.icmp() layer4.type = ident.dport >> 8 layer4.code = ident.dport & 0x00FF payloadlen = max(iplen - 28, 0) elif ident.ipproto == IPPROTO_UDP: layer4 = pktlib.udp() layer4.srcport = ident.sport layer4.dstport = ident.dport elif ident.ipproto == IPPROTO_TCP: layer4 = pktlib.tcp() layer4.srcport = ident.sport layer4.dstport = ident.dport layer4.flags = flowlet.tcpflags layer4.off = 5 payloadlen = max(iplen - 40, 0) layer4.tcplen = payloadlen layer4.payload = spack("{}x".format(payloadlen)) else: raise UnhandledPoxPacketFlowletTranslation( "Can't translate IP protocol {} from flowlet to POX packet".format(fident.ipproto) ) ipv4.payload = layer4 etherhdr.origflet = flowlet return etherhdr
def install_path(self, dst_sw, last_port, match, event): # dst_sw is switch instance # print 'install_path', self,dst_sw p = _get_path(self,dst_sw,last_port) if p is None: log.warning("Can't get from %s to %s", match.dl_src, match.dl_dst) import pox.lib.packet as pkt if (match.dl_type == pkt.ethernet.IP_TYPE and event.parsed.find('ipv4')): # It's IP -- let's send a destination unreachable log.debug("Dest unreachable (%s -> %s)", match.dl_src, match.dl_dst) from pox.lib.addresses import EthAddr e = pkt.ethernet() e.src = EthAddr(dpidToStr(self.dpid)) #FIXME: Hmm... e.dst = match.dl_src e.type = e.IP_TYPE ipp = pkt.ipv4() ipp.protocol = ipp.ICMP_PROTOCOL ipp.srcip = match.nw_dst #FIXME: Ridiculous ipp.dstip = match.nw_src icmp = pkt.icmp() icmp.type = pkt.ICMP.TYPE_DEST_UNREACH icmp.code = pkt.ICMP.CODE_UNREACH_HOST orig_ip = event.parsed.find('ipv4') d = orig_ip.pack() d = d[:orig_ip.hl * 4 + 8] import struct d = struct.pack("!HH", 0,0) + d #FIXME: MTU icmp.payload = d ipp.payload = icmp e.payload = ipp msg_ = of.ofp_packet_out() msg_.actions.append(of.ofp_action_output(port = event.port)) msg_.data = e.pack() self.connection.send(msg_) return log.debug('will install this path:%s' %p) self._install_path(p,match,event.ofp.buffer_id) log.debug("Installing path for %s -> %s %04x (%i hops)", match.dl_src,match.dl_dst,match.dl_type,len(p))
def make_ICMP(self, TYPE, pkt, dev): icmppkt = pktlib.icmp() if(TYPE=='PING'): #if ping type print 'ping reply' icmppkt.type = pktlib.TYPE_ECHO_REPLY ping = pktlib.echo() ping.id = pkt.payload.payload.id ping.seq = pkt.payload.payload.seq ping.payload = pkt.payload.payload.payload #It's like christmas, so much unwrapping icmppkt.payload = ping else: #if error message if(TYPE=='TIMEEXCEED'): print 'time exceed' icmppkt.type = pktlib.TYPE_TIME_EXCEED else: icmppkt.type = pktlib.TYPE_DEST_UNREACH if(TYPE=='UNREACH_NET'): #if table lookup failed print 'net unreach' icmppkt.code = pktlib.CODE_UNREACH_NET elif(TYPE=='UNREACH_HOST'): #if sent 5 arps and no reply from host print 'host unreach' icmppkt.code = pktlib.CODE_UNREACH_HOST elif(TYPE=='UNREACH_PORT'): #sent to us, but not an ICMP PING print 'port unreach' icmppkt.code = pktlib.CODE_UNREACH_PORT else: print 'wtf? if it wasnt one of these errors, something REALLY went wrong' icmppkt.payload = pktlib.unreach() icmppkt.payload.payload = pkt.dump()[:28] #wrap up in IP then ethernet ipreply = pktlib.ipv4() ipreply.protocol = ipreply.ICMP_PROTOCOL ipreply.dstip = pkt.srcip ipreply.ttl = 65 #make this bigger, because it gets decremented when we send it ipreply.payload = icmppkt ipreply.srcip = self.net.interface_by_name(dev).ipaddr match = self.FT_lookup(ipreply, dev) intf = self.net.interface_by_name(match[3]) prefix = match[1] nexthop = match[2] if nexthop not in self.macaddrs: self.queue.append([match, floor(time()), ipreply, 0]) self.send_arp_request(match) else: self.send_packet(match, ipreply) '''
def flowlet_to_packet(flowlet): '''Translate an fs flowlet to a POX packet''' if hasattr(flowlet, "origpkt"): return getattr(flowlet, "origpkt") ident = flowlet.ident.key etherhdr = pktlib.ethernet() etherhdr.src = EthAddr(flowlet.srcmac) etherhdr.dst = EthAddr(flowlet.dstmac) etherhdr.type = pktlib.ethernet.IP_TYPE ipv4 = pktlib.ipv4() ipv4.srcip = IPAddr(ident.srcip) ipv4.dstip = IPAddr(ident.dstip) ipv4.protocol = ident.ipproto ipv4.tos = flowlet.iptos iplen = flowlet.bytes / flowlet.pkts ipv4.iplen = iplen payloadlen = 0 etherhdr.payload = ipv4 if ident.ipproto == IPPROTO_ICMP: layer4 = pktlib.icmp() layer4.type = ident.dport >> 8 layer4.code = ident.dport & 0x00FF payloadlen = max(iplen-28,0) elif ident.ipproto == IPPROTO_UDP: layer4 = pktlib.udp() layer4.srcport = ident.sport layer4.dstport = ident.dport elif ident.ipproto == IPPROTO_TCP: layer4 = pktlib.tcp() layer4.srcport = ident.sport layer4.dstport = ident.dport layer4.flags = flowlet.tcpflags layer4.off = 5 payloadlen = max(iplen-40,0) layer4.tcplen = payloadlen layer4.payload = spack('{}x'.format(payloadlen)) else: raise UnhandledPoxPacketFlowletTranslation("Can't translate IP protocol {} from flowlet to POX packet".format(fident.ipproto)) ipv4.payload = layer4 etherhdr.origflet = flowlet return etherhdr
def icmp_ping(self, src_interface, dst_interface, payload_content=None): ''' Return an ICMP ping packet; if an interface is none, random addresses are used ''' e = ethernet() e.src = self._choose_eth_addr(src_interface) e.dst = self._choose_eth_addr(dst_interface) e.type = ethernet.IP_TYPE i = ipv4() i.protocol = ipv4.ICMP_PROTOCOL i.srcip = self._choose_ip_addr(src_interface) i.dstip = self._choose_ip_addr(dst_interface) ping = icmp() ping.type = random.choice([TYPE_ECHO_REQUEST, TYPE_ECHO_REPLY]) if payload_content == "" or payload_content is None: payload_content = "Ping" * 12 ping.payload = payload_content i.payload = ping e.payload = i return e
def sendICPMMessage(event,type,code): src_ip,my_mac = get_MAC_IP(event.connection.dpid,event.port) p=event.parsed.find("ipv4") packet = event.parsed #print p #print packet icmp = pkt.icmp() icmp.type = type icmp.code = code data = p.pack() data = data[:p.hl * 4 + 8] data = struct.pack("!HH", 0,0) + data icmp.payload = data # Make the IP packet around it ipp = pkt.ipv4() ipp.protocol = ipp.ICMP_PROTOCOL ipp.srcip = IPAddr(src_ip) ipp.dstip = p.srcip # Ethernet around that... e = pkt.ethernet() e.src = EthAddr(my_mac) e.dst = packet.src e.type = e.IP_TYPE # Hook them up... ipp.payload = icmp e.payload = ipp #print e #print e.payload # Send it back to the input port event.connection.send(of.ofp_packet_out( data=e.pack(), action=of.ofp_action_output(port=event.port))) log.debug("%s ICMP %s", ipp.dstip, ipp.srcip)
def icmp_ping(self, switch_impl, in_port): # randomly choose an in_port. if len(switch_impl.ports) == 0: raise RuntimeError("No Ports Registered on switch_impl! %s" % str(switch_impl)) # TODO: e = ethernet() # TODO: need a better way to create random MAC addresses e.src = EthAddr(struct.pack("Q",self.random.randint(1,0xFF))[:6]) e.dst = in_port.hw_addr e.type = ethernet.IP_TYPE ipp = ipv4() ipp.protocol = ipv4.ICMP_PROTOCOL ipp.srcip = IPAddr(self.random.randint(0,0xFFFFFFFF)) ipp.dstip = IPAddr(self.random.randint(0,0xFFFFFFFF)) ping = icmp() ping.type = TYPE_ECHO_REQUEST ping.payload = "PingPing" * 6 ipp.payload = ping e.payload = ipp return e
def _do_health_check(self): now = get_time_in_ms() health_changed = False LoadBalancingSwitch.logger.debug('Performing health check.') for host in self._host_pool: if host.is_healthy and now - host.last_check_reply >= LoadBalancingSwitch.MAX_HEALTH_CHECK_AGE: # Haven't gotten a health check response in one health check interval. Mark as unhealthy. host.is_healthy = False LoadBalancingSwitch.logger.warn('Host {} did not respond to health check, marking unhealthy.'.format(host.ip_address)) health_changed = True host_mac = self._arp_table.lookup(host.ip_address) if host_mac is None: # Host is not in ARP table; send an ARP health check. host_port = of.OFPP_FLOOD arp_probe = arp( opcode = arp.REQUEST, hwsrc = self._vip_mac, hwdst = SpecialMacs.ARP_REQUEST_TARGET, protosrc = self._vip, protodst = host.ip_address) ether_wrapper = ethernet(type = ethernet.ARP_TYPE, src = self._vip_mac, dst = SpecialMacs.ARP_REQUEST_TARGET, payload = arp_probe) else: # Send ICMP ECHO REQUEST health check. host_port = self._get_port_for_mac(host_mac) if host_port is None: # If the host is in the ARP table, we should know which port to send to, but if not we can fall back to flooding. host_port = of.OFPP_FLOOD # Health check pings contain the timestamp for the health check, so we can ignore responses from stale checks. icmp_echo_req = icmp(type = TYPE_ECHO_REQUEST, payload = struct.pack('!IQ', self._health_check_seq, now)) ipv4_wrapper = ipv4(protocol = ipv4.ICMP_PROTOCOL, srcip = self._vip, dstip = host.ip_address, payload = icmp_echo_req) ether_wrapper = ethernet(type = ethernet.IP_TYPE, src = self._vip_mac, dst = host_mac, payload = ipv4_wrapper) self._send_packet(ether_wrapper, host_port) if health_changed: self._report_healthy_hosts() # Schedule the next health check. Timer(LoadBalancingSwitch.DEFAULT_HEALTH_CHECK_INTERVAL, self._do_health_check)
def icmp_ping(self, interface): # randomly choose an in_port. e = ethernet() e.src = interface.hw_addr # TODO(cs): need a better way to create random MAC addresses # TODO(cs): allow the user to specify a non-random dst address e.dst = EthAddr(struct.pack("Q",self.random.randint(1,0xFF))[:6]) e.type = ethernet.IP_TYPE ipp = ipv4() ipp.protocol = ipv4.ICMP_PROTOCOL if hasattr(interface, 'ips'): ipp.srcip = self.random.choice(interface.ips) else: ipp.srcip = IPAddr(self.random.randint(0,0xFFFFFFFF)) ipp.dstip = IPAddr(self.random.randint(0,0xFFFFFFFF)) ping = icmp() ping.type = TYPE_ECHO_REQUEST ping.payload = "PingPing" * 6 ipp.payload = ping e.payload = ipp return e
def networkMatch(self, ruleObject, pkt): if (ruleObject.type == type(pkt)): if ruleObject.netMask != None: pktsrc = IPAddr(ruleObject.netMask.toUnsigned() & pkt.srcip.toUnsigned()) else: pktsrc = pkt.srcip #checks if it is a network address or not if "/" in str(ruleObject.src): templist = [] templist = ruleObject.src.split("/") rulesrc = IPAddr(templist[0]) else: rulesrc = ruleObject.src if "/" in str(ruleObject.dst): templist = [] templist = ruleObject.dst.split("/") ruledst = IPAddr(templist[0]) else: ruledst = ruleObject.dst #first type: ipv4/icmp if type(pkt) == type(pktlib.ipv4()) or type(pkt) == type(pktlib.icmp()): conditional1 = (ruleObject.type == type(pkt)), (rulesrc == pktsrc or rulesrc == 1), (ruledst == pkt.dstip or ruledst == 1) if ruleObject.ratelimit != None: conditional1a = (ruleObject.type == type(pkt)), (rulesrc == pktsrc or rulesrc == 1), (ruledst == pkt.dstip or ruledst == 1), (ruleObject.bucket >= len(pkt.pack())) return all(conditional1a) else: return all(conditional1) #second type: udp/tcp if type(pkt) == type(pktlib.tcp()) or type(pkt) == type(pktlib.udp()): conditional2 = (ruleObject.type == type(pkt)), (rulesrc == pktsrc or rulesrc == 1), ((ruledst == 1) or (ruledst == pkt.dstip)), (int(ruleObject.srcport) == int(pkt.payload.srcport) or int(ruleObject.srcport) == 1), (int(ruleObject.dstport) == int(pkt.payload.dstport) or int(ruleObject.dstport) == 1) if ruleObject.ratelimit != None: conditional2a = (ruleObject.type == type(pkt)), (rulesrc == pktsrc or rulesrc == 1), (ruledst == pkt.dstip or ruledst ==1), (int(ruleObject.srcport) == int(pkt.payload.srcport) or int(ruleObject.srcport) == 1), (int(ruleObject.dstport) == int(pkt.payload.dstport) or int(ruleObject.dstport) == 1), (ruleObject.bucket >= len(pkt.pack())) return all(conditional2a) else: return all(conditional2)
def icmp_forward(self, payload, icmp_type, ip_src, ip_dst, mac_src, mac_dst, out_port): global icmpf, ip_pakt, eth icmpf = icmp() icmpf.type = icmp_type icmpf.payload = payload ip_pakt = ipv4() ip_pakt.protocol = ip_pakt.ICMP_PROTOCOL ip_pakt.srcip = ip_src ip_pakt.dstip = ip_dst #log.debug('Forward ICMP %s from src_IP %s and dst_IP %s' %(icmp_type, ip_pakt.srcip, ip_pakt.dstip)) eth = ethernet() eth.src = mac_src eth.dst = mac_dst eth.type = eth.IP_TYPE ip_pakt.payload = icmpf eth.payload = ip_pakt self.resend_packet(eth, out_port)