def build_packet_tcp_22(): """ Build an SSH-like packet for use in tests. """ #ethernet(dst='08:00:27:1e:98:88',ethertype=2048,src='00:00:00:00:00:02') #ipv4(csum=25158,dst='192.168.57.40',flags=2,header_length=5, # identification=58864,offset=0,option=None,proto=6,src='192.168.56.12', # tos=0,total_length=60,ttl=64,version=4) #tcp(ack=0,bits=2,csum=60347,dst_port=22,offset=10, # option='\x02\x04\x05\xb4\x04\x02\x08\n\x00\x08\x16\x0f\x00\x00\x00\x00\ # x01\x03\x03\x06' # ,seq=533918719,src_port=52656,urgent=0,window_size=29200) e = ethernet.ethernet(dst='00:00:00:00:00:02', src='00:00:00:00:00:01', ethertype=2048) i = ipv4.ipv4(version=4, header_length=5, tos=0, total_length=0, identification=0, flags=0, offset=0, ttl=255, proto=0, csum=0, src='10.0.0.1', dst='10.0.0.2', option=None) t = tcp.tcp(src_port=52656, dst_port=22, seq=533918719, ack=0, offset=10, bits=2, window_size=29200, csum=0, urgent=0, option=None) p = packet.Packet() p.add_protocol(e) p.add_protocol(i) p.add_protocol(t) p.serialize() print repr(p.data) # the on-wire packet return p
def match_to_packet(self, match): pkt = packet.Packet() l2 = ethernet.ethernet( dst=match.setdefault('eth_dst', "00:00:00:00:00:02"), src=match.setdefault('eth_src', "00:00:00:00:00:01"), ethertype=match.setdefault('eth_type', 0x800) ) pkt.add_protocol(l2) if 'vlan_vid' in match: pkt.get_protocol(ethernet.ethernet).ethertype=0x8100 vl = vlan.vlan( pcp=0, cfi=0, vid=match['vlan_vid'], ethertype=match['eth_type'] ) pkt.add_protocol(vl) l3 = ipv4.ipv4( src=match.setdefault('ipv4_src', "192.168.1.1"), dst=match.setdefault('ipv4_dst', "192.168.1.2") ) pkt.add_protocol(l3) l4 = tcp.tcp( src_port=match.setdefault('tcp_src', 12345), dst_port=match.setdefault('tcp_dst', 80) ) pkt.add_protocol(l4) pkt.serialize() return pkt
def handle_ip(self, datapath, in_port, pkt): ofproto = datapath.ofproto parser = datapath.ofproto_parser ipv4_pkt = pkt.get_protocol(ipv4.ipv4) # parse out the IPv4 pkt if datapath.id == 1 and ipv4_pkt.proto == inet.IPPROTO_TCP: tcp_pkt = pkt.get_protocol(tcp.tcp) # parser out the TCP pkt eth_pkt = pkt.get_protocol(ethernet.ethernet) tcp_hd = tcp.tcp(ack=tcp_pkt.seq + 1, src_port=tcp_pkt.dst_port, dst_port=tcp_pkt.src_port, bits=20) ip_hd = ipv4.ipv4(dst=ipv4_pkt.src, src=ipv4_pkt.dst, proto=ipv4_pkt.proto) ether_hd = ethernet.ethernet(ethertype=ether.ETH_TYPE_IP, dst=eth_pkt.src, src=eth_pkt.dst) tcp_rst_ack = packet.Packet() tcp_rst_ack.add_protocol(ether_hd) tcp_rst_ack.add_protocol(ip_hd) tcp_rst_ack.add_protocol(tcp_hd) tcp_rst_ack.serialize() actions = [parser.OFPActionOutput(in_port)] out = parser.OFPPacketOut(datapath, ofproto.OFP_NO_BUFFER, ofproto.OFPP_CONTROLLER, actions, tcp_rst_ack.data) datapath.send_msg(out)
def test_serialize(self): offset = 5 csum = 0 src_ip = '192.168.10.1' dst_ip = '192.168.100.1' prev = ipv4(4, 5, 0, 0, 0, 0, 0, 64, inet.IPPROTO_TCP, 0, src_ip, dst_ip) t = tcp(self.src_port, self.dst_port, self.seq, self.ack, offset, self.bits, self.window_size, csum, self.urgent) buf = t.serialize(bytearray(), prev) res = struct.unpack(tcp._PACK_STR, str(buf)) eq_(res[0], self.src_port) eq_(res[1], self.dst_port) eq_(res[2], self.seq) eq_(res[3], self.ack) eq_(res[4], offset << 4) eq_(res[5], self.bits) eq_(res[6], self.window_size) eq_(res[8], self.urgent) # checksum ph = struct.pack('!4s4sBBH', addrconv.ipv4.text_to_bin(src_ip), addrconv.ipv4.text_to_bin(dst_ip), 0, 6, offset * 4) d = ph + buf + bytearray() s = packet_utils.checksum(d) eq_(0, s)
def frodeTest(self, msg, datapath, ofp, pkt): # Time the execution self.starttime = time.time() allSwitches = self.getAllDatapaths() # Loop through all switches for sw in allSwitches: print( "===============================================================================" ) print("Testing switch: ", sw.dp.id) #if sw.dp.id is 1: # remove catch rule from self, if any self.removeCatchRuleByID(sw.dp.id) # Install catch rules on neighbours allNeighbours = self.getNeighborsByID(sw.dp.id) for neigh in allNeighbours: # id self.addCatchRuleByID(int(neigh, 16)) # Scrape and sort flowtable flowtable = gather.getMatchData(sw.dp.id) flowtable = sorted(flowtable, key=generator.sortKey, reverse=True) # Loop through flow table entries for entry in flowtable: # Generate packet from match field and rules above pkt = generator.packetFromMatch(entry, flowtable) self.generateTime = time.time() # add packet to list entry["packet"] = {"ip": pkt.get_protocol(ipv4.ipv4())} if entry["packet"]["ip"].proto is 6: entry["packet"]["tcp"] = pkt.get_protocol(tcp.tcp()) elif entry["packet"]["ip"].proto is 17: entry["packet"]["udp"] = pkt.get_protocol(udp.udp()) # is total overlap? if pkt == -1: # log and move on entry["totalOverlap"] = True self.totalOverlap += 1 # log some info about # the entry & packet break # ? # is drop rule? if (len(entry["actions"]) is 0 or re.search( 'CLEAR_ACTIONS', entry["actions"][0]) is not None): # get match and send packet self.checkDropRule(entry, pkt, sw) # is unicast else: # get match and send packet self.checkUnicastRule(entry, pkt, sw) self.packetGenTime = self.generateTime - self.starttime print("PACKET GEN TIME: ", format(self.packetGenTime, '.5f'))
def test_serialize(self): offset = 5 csum = 0 src_ip = '192.168.10.1' dst_ip = '192.168.100.1' prev = ipv4(4, 5, 0, 0, 0, 0, 0, 64, inet.IPPROTO_TCP, 0, src_ip, dst_ip) t = tcp.tcp(self.src_port, self.dst_port, self.seq, self.ack, offset, self.bits, self.window_size, csum, self.urgent) buf = t.serialize(bytearray(), prev) res = struct.unpack(tcp.tcp._PACK_STR, six.binary_type(buf)) eq_(res[0], self.src_port) eq_(res[1], self.dst_port) eq_(res[2], self.seq) eq_(res[3], self.ack) eq_(res[4], offset << 4) eq_(res[5], self.bits) eq_(res[6], self.window_size) eq_(res[8], self.urgent) # test __len__ # offset indicates the number of 32 bit (= 4 bytes) # words in the TCP Header. # So, we compare len(tcp) with offset * 4, here. eq_(offset * 4, len(t)) # checksum ph = struct.pack('!4s4sBBH', addrconv.ipv4.text_to_bin(src_ip), addrconv.ipv4.text_to_bin(dst_ip), 0, 6, offset * 4) d = ph + buf s = packet_utils.checksum(d) eq_(0, s)
def _flow_stats_reply_handler(self, ev): body = ev.msg.body # # self.logger.info('datapath ' # 'in-port eth-dst ' # 'out-port packets bytes') # self.logger.info('---------------- ' # '-------- ----------------- ' # '-------- -------- --------') for stat in sorted([flow for flow in body if flow.priority == 1], key=lambda flow: (flow.match['in_port'], flow.match['eth_dst'])): datapath = ev.msg.datapath ofproto = datapath.ofproto parser = datapath.ofproto_parser dpid = datapath.id self._host.setdefault(dpid, []) # machine learning check pkt = tcp.tcp(ev.msg.body) cnt = 0 if(pkt.has_flags(tcp.TCP_SYN)): cnt +=1 # 发送丢包指令 if(cnt > 5): self._drop_packets(datapath=datapath) self._host[dpid].append(stat.match['in_port']) self.logger.info('[+]%x: %x is abnormal\n',dpid,stat.match['in_port']) else: pass
def handle_ip(self, datapath, in_port, pkt): ofproto = datapath.ofproto parser = datapath.ofproto_parser ipv4_pkt = pkt.get_protocol(ipv4.ipv4) # parse out the IPv4 pkt if datapath.id == 1 and ipv4_pkt.proto == inet.IPPROTO_TCP: tcp_pkt = pkt.get_protocol(tcp.tcp) # parser out the TCP pkt eth_pkt = pkt.get_protocol(ethernet.ethernet) #if (tcp_pkt.bits) % 4 == 2: tcp_hd = tcp.tcp(ack=tcp_pkt.seq+1, src_port = tcp_pkt.dst_port, dst_port = tcp_pkt.src_port, bits=20) ip_hd = ipv4.ipv4(dst= ipv4_pkt.src, src= ipv4_pkt.dst, proto=ipv4_pkt.proto) ether_hd = ethernet.ethernet(ethertype = ether.ETH_TYPE_IP, dst = eth_pkt.src, src = eth_pkt.dst) tcp_rst_ack = packet.Packet() tcp_rst_ack.add_protocol(ether_hd) tcp_rst_ack.add_protocol(ip_hd) tcp_rst_ack.add_protocol(tcp_hd) tcp_rst_ack.serialize() # generate the TCP packet with the RST flag set to 1 # packet generation is similar to ARP, # but need to generate ethernet->ip->tcp and serialize it # send the Packet Out mst to back to the host who is initilaizing the ARP actions = [parser.OFPActionOutput(in_port)]; out = parser.OFPPacketOut(datapath, ofproto.OFP_NO_BUFFER, ofproto.OFPP_CONTROLLER, actions, tcp_rst_ack.data) datapath.send_msg(out)
def handle_ip(self, datapath, in_port, pkt): ofproto = datapath.ofproto parser = datapath.ofproto_parser eth_pkt = pkt.get_protocol(ethernet.ethernet) ipv4_pkt = pkt.get_protocol(ipv4.ipv4) tcp_pkt = pkt.get_protocol(tcp.tcp) tcp_response = packet.Packet() tcp_response.add_protocol( ethernet.ethernet(dst=eth_pkt.src, src=eth_pkt.dst, ethertype=eth_pkt.ethertype)) tcp_response.add_protocol( ipv4.ipv4(dst=ipv4_pkt.src, src=ipv4_pkt.dst, proto=ipv4_pkt.proto)) tcp_response.add_protocol( tcp.tcp(dst_port=tcp_pkt.src_port, src_port=tcp_pkt.dst_port, bits=tcp.TCP_RST)) tcp_response.serialize() actions = [parser.OFPActionOutput(in_port)] out = parser.OFPPacketOut(datapath, ofproto.OFP_NO_BUFFER, ofproto.OFPP_CONTROLLER, actions, tcp_response.data) datapath.send_msg(out) """if datapath.id == 1 and ipv4_pkt.proto == inet.IPPROTO_TCP:
def reply_tcp_pke(self, pkt): pkt_eth = pkt.get_protocol(ethernet.ethernet) pkt_ipv4 = pkt.get_protocol(ipv4.ipv4) pkt_tcp = pkt.get_protocol(tcp.tcp) pkt = packet.Packet() # Add ethernet protocol with ether type IP protocol and mac addresses pkt.add_protocol(ethernet.ethernet(ethertype=0x0800, dst=pkt_eth.dst, src=pkt_eth.src)) # Add ipv4 protocol with IP addresses and TCP protocol which is 6 pkt.add_protocol(ipv4.ipv4(dst=pkt_ipv4.dst, src=pkt_ipv4.src, proto=6)) # Add tcp protocol with port numbers and sequence number pkt.add_protocol(tcp.tcp(src_port=pkt_tcp.dst_port, dst_port=pkt_tcp.src_port, seq=pkt_tcp.seq + 1, ack=pkt_tcp.ack, offset=pkt_tcp.offset, bits=pkt_tcp.bits, window_size=pkt_tcp.window_size, csum=pkt_tcp.csum, urgent=pkt_tcp.urgent, option=pkt_tcp.option)) return pkt
def test_serialize_option(self): # prepare test data offset = 0 csum = 0 option = [ tcp.TCPOptionMaximumSegmentSize(max_seg_size=1460), tcp.TCPOptionSACKPermitted(), tcp.TCPOptionTimestamps(ts_val=287454020, ts_ecr=1432778632), tcp.TCPOptionNoOperation(), tcp.TCPOptionWindowScale(shift_cnt=9), ] option_buf = (b'\x02\x04\x05\xb4' b'\x04\x02' b'\x08\x0a\x11\x22\x33\x44\x55\x66\x77\x88' b'\x01' b'\x03\x03\x09') prev = ipv4(4, 5, 0, 0, 0, 0, 0, 64, inet.IPPROTO_TCP, 0, '192.168.10.1', '192.168.100.1') # test serializer t = tcp.tcp(self.src_port, self.dst_port, self.seq, self.ack, offset, self.bits, self.window_size, csum, self.urgent, option) buf = t.serialize(bytearray(), prev) r_option_buf = buf[tcp.tcp._MIN_LEN:tcp.tcp._MIN_LEN + len(option_buf)] eq_(option_buf, r_option_buf) # test parser (r_tcp, _, _) = tcp.tcp.parser(buf) eq_(str(option), str(r_tcp.option))
def __init__(self, *args, **kwargs): super(DPIController, self).__init__(*args, **kwargs) self.tbl_num = self._service_manager.get_table_num(self.APP_NAME) self.next_table = self._service_manager.get_next_table_num( self.APP_NAME) self.setup_type = kwargs['config']['setup_type'] self._datapath = None self._dpi_enabled = kwargs['config']['dpi']['enabled'] self._mon_port = kwargs['config']['dpi']['mon_port'] self._mon_port_number = kwargs['config']['dpi']['mon_port_number'] self._idle_timeout = kwargs['config']['dpi']['idle_timeout'] self._bridge_name = kwargs['config']['bridge_name'] self._app_set_tbl_num = self._service_manager.INTERNAL_APP_SET_TABLE_NUM self._imsi_set_tbl_num = \ self._service_manager.INTERNAL_IMSI_SET_TABLE_NUM if self._dpi_enabled: self._create_monitor_port() tcp_pkt = packet.Packet() tcp_pkt.add_protocol( ethernet.ethernet(ethertype=ether_types.ETH_TYPE_IP)) tcp_pkt.add_protocol(ipv4.ipv4(proto=6)) tcp_pkt.add_protocol(tcp.tcp()) tcp_pkt.serialize() self.tcp_pkt_data = tcp_pkt.data udp_pkt = packet.Packet() udp_pkt.add_protocol( ethernet.ethernet(ethertype=ether_types.ETH_TYPE_IP)) udp_pkt.add_protocol(ipv4.ipv4(proto=17)) udp_pkt.add_protocol(udp.udp()) udp_pkt.serialize() self.udp_pkt_data = udp_pkt.data
def build_new_packet(switch): src_eth = switch['src_eth'] dst_eth = switch['dst_eth'] src_ip = switch['src_ip'] dst_ip = switch['dst_ip'] ip_id = switch['ip_id'] seq = switch['seq'] ack = switch['ack'] src_port = switch['src_port'] dst_port = switch['dst_port'] #eth proto eth_pkt = ethernet.ethernet(src_eth,dst_eth,ethertype=ether_types.ETH_TYPE_IP) #ip proto ip_pkt = ipv4.ipv4(version=4, header_length=5, tos=0, total_length=0, identification=ip_id+1, flags=2, offset=0, ttl=64, proto=in_proto.IPPROTO_TCP, csum=0, src=dst_ip, dst=src_ip) #tcp proto #psh ack bit bits = 0b011000 tcp_pkt = tcp.tcp(src_port=dst_port, dst_port=src_port, seq=ack, ack=seq, offset=0, bits=bits, window_size=2048, csum=0, urgent=0, option=None) p = packet.Packet() p.add_protocol(eth_pkt) p.add_protocol(ip_pkt) p.add_protocol(tcp_pkt) return p
def generate_trace_pkt(entries, color, r_id): ''' Receives the REST/PUT to generate a PacketOut data needs to be serialized template_trace.json is an example ''' trace = {} switch = {} eth = {} ip = {} tp = {} # TODO Validate for dl_vlan. If empty, return error. dpid, in_port = 0, 65532 dl_src, dl_dst = "ca:fe:ca:fe:00:00", "ca:fe:ca:fe:ca:fe" dl_vlan, dl_type = 100, 2048 nw_src, nw_dst, nw_tos = '127.0.0.1', '127.0.0.1', 0 tp_src, tp_dst = 1, 1 try: trace = entries['trace'] switch = trace['switch'] eth = trace['eth'] ip = trace['ip'] tp = trace['tp'] except: pass if len(switch) > 0: dpid, in_port = prepare_switch(switch, dpid, in_port) if len(eth) > 0: dl_src, dl_dst, dl_vlan, dl_type = prepare_ethernet(eth, dl_src, dl_dst, dl_vlan, dl_type) if len(ip) > 0: nw_src, nw_dst, nw_tos = prepare_ip(ip, nw_src, nw_dst, nw_tos) if len(tp) > 0: tp_src, tp_dst = prepare_tp(tp, tp_src, tp_dst) pkt = packet.Packet() eth_pkt = ethernet.ethernet(dst=dl_dst, src=dl_src, ethertype=33024) vlan_pkt = vlan.vlan(vid=dl_vlan, ethertype=dl_type, pcp=int(color, 2)) pkt.add_protocol(eth_pkt) pkt.add_protocol(vlan_pkt) if dl_type == 2048: ip_pkt = ipv4.ipv4(dst=str(nw_dst), src=str(nw_src), tos=nw_tos, proto=6) pkt.add_protocol(ip_pkt) tp_pkt = tcp.tcp(dst_port=tp_dst, src_port=tp_src) pkt.add_protocol(tp_pkt) data = str(r_id) # this will be the ID pkt.add_protocol(data) pkt.serialize() return in_port, pkt
def test_serialize_option(self): # prepare test data offset = 0 csum = 0 option = [ tcp.TCPOptionMaximumSegmentSize(max_seg_size=1460), tcp.TCPOptionSACKPermitted(), tcp.TCPOptionTimestamps(ts_val=287454020, ts_ecr=1432778632), tcp.TCPOptionNoOperation(), tcp.TCPOptionWindowScale(shift_cnt=9), ] option_buf = b"\x02\x04\x05\xb4" b"\x04\x02" b"\x08\x0a\x11\x22\x33\x44\x55\x66\x77\x88" b"\x01" b"\x03\x03\x09" prev = ipv4(4, 5, 0, 0, 0, 0, 0, 64, inet.IPPROTO_TCP, 0, "192.168.10.1", "192.168.100.1") # test serializer t = tcp.tcp( self.src_port, self.dst_port, self.seq, self.ack, offset, self.bits, self.window_size, csum, self.urgent, option, ) buf = t.serialize(bytearray(), prev) r_option_buf = buf[tcp.tcp._MIN_LEN : tcp.tcp._MIN_LEN + len(option_buf)] eq_(option_buf, r_option_buf) # test parser (r_tcp, _, _) = tcp.tcp.parser(buf) eq_(str(option), str(r_tcp.option))
def handle_ip(self, datapath, in_port, pkt): ofproto = datapath.ofproto parser = datapath.ofproto_parser ipv4_pkt = pkt.get_protocol(ipv4.ipv4) # parse out the IPv4 pkt if (datapath.id == 2 or datapath.id == 4) and ipv4_pkt.proto == inet.IPPROTO_TCP: tcp_pkt = ipv4_pkt.get_protocol(tcp.tcp) # parser out the TCP pkt ### generate the TCP packet with the RST flag set to 1 ### packet generation is similar to ARP, ### but you need to generate ethernet->ip->tcp and serialize it eth_pkt = ipv4_dst.get_protocol(ethernet, ethernet) tcp_hd = tcp.tcp(ack=tcp_pkt.seq + 1, src_port=tcp_pkt.dst_port, dst_port=tcp_pkt.src_port, bits=20) ip_hd = ipv4.ipv4(dst=ipv4_pkt.src, src=ipv4_pkt.dst, proto=ipv4_pkt.proto) ether_hd = ethernet.ethernet(ethertype=ether.ETH_TYPE_IP, dst=eth_pkt.src, src=eth_pkt.dst) tcp_rst_ack = packet.Packet() tcp_rst_ack.add_protocol(ether_hd) tcp_rst_ack.add_protocol(ip_hd) tcp_rst_ack.add_protocol(tcp_hd) tcp_rst_ack.serialize() # send the Packet Out mst to back to the host who is initilaizing the ARP actions = [parser.OFPActionOutput(in_port)] out = parser.OFPPacketOut(datapath, ofproto.OFP_NO_BUFFER, ofproto.OFPP_CONTROLLER, actions, tcp_rst_ack.data) datapath.send_msg(out)
def generateFINpkt(self): ipv4_p = self.payload_pkt.get_protocol(ipv4.ipv4) tcp_P = self.payload_pkt.get_protocol(tcp.tcp) eth_p = self.payload_pkt.get_protocol(ethernet.ethernet) e = ethernet.ethernet(dst=eth_p.dst, src=eth_p.src) #ip = ipv4.ipv4(4, 5, ipv4_p.tos, 0, 0, 0, 0, 255, 6, 0, src=ipv4_p.dst, dst=ipv4_p.src, option=None) #ip = ipv4.ipv4(4, 5, ipv4_p.tos, 0, ipv4_p.identification, ipv4_p.flags, 0, ipv4_p.ttl, ipv4_p.proto, 0, src=ipv4_p.src, dst=ipv4_p.dst, option=None) ip = ipv4.ipv4(4, 5, ipv4_p.tos, 0, ipv4_p.identification, ipv4_p.flags, 0, ipv4_p.ttl, ipv4_p.proto, 0, src=ipv4_p.dst, dst=ipv4_p.src, option=None) bits = 1 | 1 << 4 #tcpd = tcp.tcp(tcp_P.src_port, tcp_P.dst_port, tcp_P.seq, tcp_P.ack, 0, bits, tcp_P.window_size, 0, tcp_P.urgent, tcp_P.option) tcpd = tcp.tcp(tcp_P.dst_port, tcp_P.src_port, tcp_P.seq, tcp_P.ack, 0, bits, tcp_P.window_size, 0, tcp_P.urgent, tcp_P.option) fin_pkt = packet.Packet() fin_pkt.add_protocol(e) fin_pkt.add_protocol(ip) fin_pkt.add_protocol(tcpd) fin_pkt.serialize() print "FIN packet is generated." return fin_pkt
def assemble_tcp(self, content, vlanid, dst_ip, dst_mac, src_ip, src_mac, dport, sport=61300): offset = ethernet.ethernet._MIN_LEN if vlanid != 0: ether_proto = ether.ETH_TYPE_8021Q vlan_ether = ether.ETH_TYPE_IP v = vlan.vlan(0, 0, vlanid, vlan_ether) offset += vlan.vlan._MIN_LEN else: ether_proto = ether.ETH_TYPE_IP pkt = packet.Packet() if vlanid != 0: pkt.add_protocol(v) # Add ethernet protocol with ether type IP protocol and mac addresses pkt.add_protocol(ethernet.ethernet(ethertype=ether_proto, dst=dst_mac, src=src_mac)) # Add ipv4 protocol with IP addresses and TCP protocol which is 6 pkt.add_protocol(ipv4.ipv4(dst=dst_ip, src=src_ip, proto=6)) # Add tcp protocol with port numbers and sequence number pkt.add_protocol(tcp.tcp(src_port=sport, dst_port=dport)) pkt.add_protocol(bytearray(content)) return pkt
def _build_pkt(self, fields, ops): pkt_out = packet.Packet() pkt_ipv4 = pkt_out.get_protocol(ipv4.ipv4) pkt_icmp = pkt_out.get_protocol(icmp.icmp) def addIPv4(pkt_out, fields): pkt_out.add_protocol( ipv4.ipv4(dst=fields['dstip'], src=fields['srcip'], proto=fields['proto'])) return pkt_out pkt_out.add_protocol( ethernet.ethernet(ethertype=fields['ethtype'], dst=fields['dstmac'], src=fields['srcmac'])) # Add if ARP if 'arp' in fields['ptype']: pkt_out.add_protocol( arp.arp(opcode=arp.ARP_REPLY, src_mac=fields['srcmac'], src_ip=fields['srcip'], dst_mac=fields['dstmac'], dst_ip=fields['dstip'])) # Add if IPv4 if 'ipv4' in fields['ptype']: pkt_out = addIPv4(pkt_out, fields) # Add if ICMP if 'icmp' in fields['ptype']: pkt_out = addIPv4(pkt_out, fields) pkt_out.add_protocol( icmp.icmp(type_=icmp.ICMP_ECHO_REPLY, code=icmp.ICMP_ECHO_REPLY_CODE, csum=0, data=None)) # Add if UDP if 'udp' in fields['ptype']: pkt_out = addIPv4(pkt_out, fields) pkt_out.add_protocol( udp.udp(dst_port=fields['dstport'], bits=fields['bits'], option=fields['opt'], src_port=fields['srcport'])) # Add if TCP if 'tcp' in fields['ptype']: pkt_out = addIPv4(pkt_out, fields) pkt_out.add_protocol( tcp.tcp(dst_port=fields['dstport'], bits=fields['bits'], option=fields['opt'], src_port=fields['srcport'])) #Add covert channel information if fields['com'] != None: pkt_out.add_protocol(fields['com']) #Send crafted packet self._send_packet(fields['dp'], ops['newport'], pkt_out)
def tcp_packet_gen(): pkt = packet.Packet() pkt.protocols.append( ethernet.ethernet("ff:ff:ff:ff:ff:ff", "ff:ff:ff:ff:ff:ff", IPV4)) pkt.protocols.append(ipv4.ipv4(proto=TCP_PROTO)) pkt.protocols.append(tcp.tcp(0, 0, 0, 0, 0, 0, 0, 0, 0)) return pkt
def switch_features_handler(self, ev): datapath = ev.msg.datapath ofproto = datapath.ofproto parser = datapath.ofproto_parser # install table-miss flow entry # # We specify NO BUFFER to max_len of the output action due to # OVS bug. At this moment, if we specify a lesser number, e.g., # 128, OVS will send Packet-In with invalid buffer_id and # truncated packet data. In that case, we cannot output packets # correctly. The bug has been fixed in OVS v2.1.0. match = parser.OFPMatch() actions = [ parser.OFPActionOutput(ofproto.OFPP_CONTROLLER, ofproto.OFPCML_NO_BUFFER) ] self.add_flow(datapath, 0, match, actions) if int(datapath.id) == 2: match = parser.OFPMatch(eth_type=0x0800, ipv4_src='0.0.0.1', ipv4_dst='0.0.0.2') actions = [ parser.OFPActionSetField(ipv4_src='0.0.0.0'), parser.OFPActionSetField(ipv4_dst='0.0.0.0'), parser.OFPActionOutput(ofproto.OFPP_CONTROLLER, ofproto.OFPCML_NO_BUFFER) ] self.add_flow(datapath, 1, match, actions) if int(datapath.id) == 1: self.dp_add = datapath pkt = packet.Packet() pkt.add_protocol( ethernet.ethernet(ethertype=0x0800, dst='75:43:21:13:15:17', src='75:43:12:31:51:71')) pkt.add_protocol( ipv4.ipv4(dst='0.0.0.2', src='0.0.0.1', ttl=254, proto=6)) pkt.add_protocol(tcp.tcp(src_port=1234, dst_port=5001, seq=1234566)) pkt.add_protocol('j' * 1380) pkt.serialize() actions = [parser.OFPActionOutput(ofproto.OFPP_TABLE)] match = parser.OFPMatch(eth_type=0x0800, ipv4_src='0.0.0.1', ipv4_dst='0.0.0.2') actions = [parser.OFPActionOutput(2), parser.OFPActionOutput(2)] self.add_flow(datapath, 1, match, actions) #self.start_time[int(datapath.id)-1] = datetime.datetime.now().microsecond datapath.send_packet_out(buffer_id=ofproto.OFP_NO_BUFFER, in_port=ofproto.OFPP_CONTROLLER, actions=actions, data=pkt.data)
def test_default_args(self): prev = ipv4(proto=inet.IPPROTO_TCP) t = tcp.tcp() buf = t.serialize(bytearray(), prev) res = struct.unpack(tcp.tcp._PACK_STR, buf) eq_(res[0], 1) eq_(res[1], 1) eq_(res[2], 0) eq_(res[3], 0) eq_(res[4], 5 << 4) eq_(res[5], 0) eq_(res[6], 0) eq_(res[8], 0) # with option, without offset t = tcp.tcp(option=[tcp.TCPOptionMaximumSegmentSize(1460)]) buf = t.serialize(bytearray(), prev) res = struct.unpack(tcp.tcp._PACK_STR + "4s", buf) eq_(res[0], 1) eq_(res[1], 1) eq_(res[2], 0) eq_(res[3], 0) eq_(res[4], 6 << 4) eq_(res[5], 0) eq_(res[6], 0) eq_(res[8], 0) eq_(res[9], b"\x02\x04\x05\xb4") # with option, with long offset t = tcp.tcp(offset=7, option=[tcp.TCPOptionWindowScale(shift_cnt=9)]) buf = t.serialize(bytearray(), prev) res = struct.unpack(tcp.tcp._PACK_STR + "8s", buf) eq_(res[0], 1) eq_(res[1], 1) eq_(res[2], 0) eq_(res[3], 0) eq_(res[4], 7 << 4) eq_(res[5], 0) eq_(res[6], 0) eq_(res[8], 0) eq_(res[9], b"\x03\x03\x09\x00\x00\x00\x00\x00")
def test_default_args(self): prev = ipv4(proto=inet.IPPROTO_TCP) t = tcp() buf = t.serialize(bytearray(), prev) res = struct.unpack(tcp._PACK_STR, buf) eq_(res[0], 1) eq_(res[1], 1) eq_(res[2], 0) eq_(res[3], 0) eq_(res[4], 5 << 4) eq_(res[5], 0) eq_(res[6], 0) eq_(res[8], 0) # with option, without offset t = tcp(option='\x01\x02\x03') buf = t.serialize(bytearray(), prev) res = struct.unpack(tcp._PACK_STR + '4s', buf) eq_(res[0], 1) eq_(res[1], 1) eq_(res[2], 0) eq_(res[3], 0) eq_(res[4], 6 << 4) eq_(res[5], 0) eq_(res[6], 0) eq_(res[8], 0) eq_(res[9], '\x01\x02\x03\x00') # with option, with long offset t = tcp(offset=7, option='\x01\x02\x03') buf = t.serialize(bytearray(), prev) res = struct.unpack(tcp._PACK_STR + '8s', buf) eq_(res[0], 1) eq_(res[1], 1) eq_(res[2], 0) eq_(res[3], 0) eq_(res[4], 7 << 4) eq_(res[5], 0) eq_(res[6], 0) eq_(res[8], 0) eq_(res[9], '\x01\x02\x03\x00\x00\x00\x00\x00')
def test_default_args(self): prev = ipv4(proto=inet.IPPROTO_TCP) t = tcp.tcp() buf = t.serialize(bytearray(), prev) res = struct.unpack(tcp.tcp._PACK_STR, buf) eq_(res[0], 1) eq_(res[1], 1) eq_(res[2], 0) eq_(res[3], 0) eq_(res[4], 5 << 4) eq_(res[5], 0) eq_(res[6], 0) eq_(res[8], 0) # with option, without offset t = tcp.tcp(option=[tcp.TCPOptionMaximumSegmentSize(1460)]) buf = t.serialize(bytearray(), prev) res = struct.unpack(tcp.tcp._PACK_STR + '4s', buf) eq_(res[0], 1) eq_(res[1], 1) eq_(res[2], 0) eq_(res[3], 0) eq_(res[4], 6 << 4) eq_(res[5], 0) eq_(res[6], 0) eq_(res[8], 0) eq_(res[9], b'\x02\x04\x05\xb4') # with option, with long offset t = tcp.tcp(offset=7, option=[tcp.TCPOptionWindowScale(shift_cnt=9)]) buf = t.serialize(bytearray(), prev) res = struct.unpack(tcp.tcp._PACK_STR + '8s', buf) eq_(res[0], 1) eq_(res[1], 1) eq_(res[2], 0) eq_(res[3], 0) eq_(res[4], 7 << 4) eq_(res[5], 0) eq_(res[6], 0) eq_(res[8], 0) eq_(res[9], b'\x03\x03\x09\x00\x00\x00\x00\x00')
def handle_ip(self, dpath, in_port, pkt): ofproto = dpath.ofproto parser = dpath.ofproto_parser ipv4_pkt = pkt.get_protocol(ipv4.ipv4) # parse out the IPv4 pkt tcp_pkt = pkt.get_protocol(tcp.tcp) # parser out the TCP pkt eth_pkt = pkt.get_protocol(ethernet.ethernet) ip_src = ipv4_pkt.src ip_dst = ipv4_pkt.dst ip_proto = ipv4_pkt.proto dst_port = tcp_pkt.dst_port if ip_src == "10.0.0.2" and ip_dst == "10.0.0.4" and ip_proto == inet.IPPROTO_TCP and dst_port == 80: tcp_hd = tcp.tcp(ack=tcp_pkt.seq + 1, src_port=tcp_pkt.dst_port, dst_port=tcp_pkt.src_port, bits=20) ip_hd = ipv4.ipv4(dst=ipv4_pkt.src, src=ipv4_pkt.dst, proto=ipv4_pkt.proto) ether_hd = ethernet.ethernet(ethertype=ether.ETH_TYPE_IP, dst=eth_pkt.src, src=eth_pkt.dst) tcp_rst_ack = packet.Packet() tcp_rst_ack.add_protocol(ether_hd) tcp_rst_ack.add_protocol(ip_hd) tcp_rst_ack.add_protocol(tcp_hd) self.packet_output(dpath,in_port,tcp_rst_ack) elif ip_src == "10.0.0.4" and ip_dst == "10.0.0.2" and ip_proto == inet.IPPROTO_TCP and dst_port == 80: tcp_hd = tcp.tcp(ack=tcp_pkt.seq + 1, src_port=tcp_pkt.dst_port, dst_port=tcp_pkt.src_port, bits=20) ip_hd = ipv4.ipv4(dst=ipv4_pkt.src, src=ipv4_pkt.dst, proto=ipv4_pkt.proto) ether_hd = ethernet.ethernet(ethertype=ether.ETH_TYPE_IP, dst=eth_pkt.src, src=eth_pkt.dst) tcp_rst_ack = packet.Packet() tcp_rst_ack.add_protocol(ether_hd) tcp_rst_ack.add_protocol(ip_hd) tcp_rst_ack.add_protocol(tcp_hd) self.packet_output(dpath,in_port,tcp_rst_ack) else: return
def _flow_stats_reply_handler(self, ev): body = ev.msg.body self.count = 0 self.byte = [] self.byte_count_constant = 1000 # for example self.packet_count_constant = 500 # for example for stat in sorted([flow for flow in body if flow.priority == 1], key=lambda flow: (flow.match['in_port'], flow.match['eth_dst'])): self.cache = stat self.byte_count = stat.byte_count self.packet_count = stat.packet_count if (self.byte_count > self.byte_count_constant and self.packet_count > self.packet_count_constant): self.destination = stat.match['eth_dst'] self.in_port = stat.match['in_port'] self.destination_id = ev.msg.datapath.id self.logger.info( "______________________________Ethernet dst victim_________________________________________________" ) self.logger.info(ds) self.logger.info( "______________________________Ethernet dst Attacker_______________________________________________" ) print('DPID :', self.destination_id) print('IN_PORT :', self.in_port) self.logger.info( "__________________________________________ Byte count so far______________________________________" ) byte.append(stat.byte_count) self.logger.info(byte) self.logger.info( "_________________________________________No of bytes in each host_________________________________" ) self.logger.info(byte[-1]) b1 = byte[-1] self.logger.info(byte[-2]) b2 = byte[-2] self.logger.info( "_________________________________________________N value__________________________________________" ) N = b1 + b2 self.logger.info(N) self.logger.info( "_____________________________________________Entropy Ratio________________________________________" ) self.bytes_in_each_port = [b1, b2] self.entropy = scipy.stats.entropy(self.bytes_in_each_port) self.logger.info(self.entropy) pkt = tcp.tcp(bits=(tcp.TCP_SYN & tcp.TCP_ACK)) self.logger.info(pkt.has_flags(tcp.TCP_SYN, tcp.TCP_ACK)) if (pkt != "True"): self.count = self.count + 1 self.logger.info(count)
def _get_tp_pkt(self, proto=IPPROTO_TCP, src_port=34567, dst_port=80): pkt = packet.Packet() pkt.add_protocol(ethernet.ethernet()) pkt.add_protocol(ipv4.ipv4(proto=proto)) if proto == IPPROTO_TCP: pkt.add_protocol(tcp.tcp(src_port=src_port, dst_port=dst_port)) elif proto == IPPROTO_UDP: pkt.add_protocol(udp.udp(src_port=src_port, dst_port=dst_port)) return pkt
def _handle_tcp_reset(self,datapath, dst, src, pkt_ethernet, pkt_ipv4, in_port, src_ip, dst_ip): new_pkt = packet.Packet() new_pkt.add_protocol(ethernet.ethernet(ethertype=pkt_ethernet.ethertype, dst=dst, src=src)) new_pkt.add_protocol(ipv4.ipv4(dst=dst_ip, src=src_ip, proto=pkt_ipv4.proto)) new_pkt.add_protocol(tcp.tcp(src_port=80, dst_port=1, rst=1, seq=0, ack=0, offset=0, bits=4, window_size=0, csum=0, urgent=0, option=None)) self._send_packet(datapath, in_port, new_pkt)
def sampleTcpPayload(): e = ethernet.ethernet( dst=ETHERNET_DATA["dst_mac"], src=ETHERNET_DATA["src_mac"], ethertype=TCP_DATA["ethertype"] ) pip = ipv4.ipv4(src=TCP_DATA["src_ip"], dst=TCP_DATA["dst_ip"], proto=TCP_DATA["proto"]) ptcp = tcp.tcp(src_port=TCP_DATA["src_port"], dst_port=TCP_DATA["dst_port"]) p = packet.Packet() p.add_protocol(e) p.add_protocol(pip) p.add_protocol(ptcp) p.serialize() return NotBuffered(binascii.a2b_base64(binascii.b2a_base64(p.data)))
def handle_ip(self, datapath, in_port, pkt): ofproto = datapath.ofproto parser = datapath.ofproto_parser ipv4_pkt = pkt.get_protocol(ipv4.ipv4) # parse out the IPv4 pkt if datapath.id == 3 and ipv4_pkt.proto == inet.IPPROTO_TCP: tcp_pkt = pkt.get_protocol(tcp.tcp) # parser out the TCP pkt ### generate the TCP packet with the RST flag set to 1 ### packet generation is similar to ARP, ### but you need to generate ethernet->ip->tcp and serialize it ether_frame = ethernet.ethernet(dst=self.arp_table[ipv4_pkt.src], src=self.arp_table[ipv4_pkt.dst], ethertype=ether.ETH_TYPE_IP) ip_packet = ipv4.ipv4(version=4, header_length=5, tos=0, total_length=0, identification=0, flags=0, offset=0, ttl=255, proto=0x06, csum=0, src=ipv4_pkt.dst, dst=ipv4_pkt.src, option=None) tcp_segment = tcp.tcp(src_port=tcp_pkt.dst_port, dst_port=tcp_pkt.src_port, seq=0, ack=(tcp_pkt.seq + 1), offset=0, bits=0x004, window_size=0, csum=0, urgent=0, option=None) tcp_rst_ack = packet.Packet() tcp_rst_ack.add_protocol(ether_frame) tcp_rst_ack.add_protocol(ip_packet) tcp_rst_ack.add_protocol(tcp_segment) tcp_rst_ack.serialize() # send the Packet Out mst to back to the host who is initilaizing the ARP actions = [parser.OFPActionOutput(in_port)] out = parser.OFPPacketOut(datapath, ofproto.OFP_NO_BUFFER, ofproto.OFPP_CONTROLLER, actions, tcp_rst_ack.data) datapath.send_msg(out)
def generate_testpacket(ip, tcp_src): p = packet.Packet() p.add_protocol(ethernet.ethernet(ethertype=0x800)) #p.add_protocol(ethernet.ethernet(ethertype=0x8847)) #p.add_protocol(mpls.mpls(label=tcp_src)) #p.add_protocol(vlan.vlan(vid=vid+1501, ethertype=0x800)) p.add_protocol(ipv4.ipv4(src=ip, proto=inet.IPPROTO_TCP)) p.add_protocol(tcp.tcp(src_port=tcp_src)) p.serialize() #pkt = packet.Packet(p.data) #for ele in pkt.protocols: # print ele return p
def _build_pkt(self, fields, ops): pkt_out = packet.Packet() pkt_ipv4 = pkt_out.get_protocol(ipv4.ipv4) pkt_icmp = pkt_out.get_protocol(icmp.icmp) def addIPv4(pkt_out, fields): pkt_out.add_protocol(ipv4.ipv4(dst=fields['dstip'], src=fields['srcip'], proto=fields['proto'])) return pkt_out pkt_out.add_protocol(ethernet.ethernet(ethertype=fields['ethtype'], dst=fields['dstmac'], src=fields['srcmac'])) # Add if ARP if 'arp' in fields['ptype']: pkt_out.add_protocol(arp.arp(opcode=arp.ARP_REPLY, src_mac=fields['srcmac'], src_ip=fields['srcip'], dst_mac=fields['dstmac'], dst_ip=fields['dstip'])) # Add if IPv4 if 'ipv4' in fields['ptype']: pkt_out = addIPv4(pkt_out,fields) # Add if ICMP if 'icmp' in fields['ptype']: pkt_out = addIPv4(pkt_out,fields) pkt_out.add_protocol(icmp.icmp(type_=icmp.ICMP_ECHO_REPLY, code=icmp.ICMP_ECHO_REPLY_CODE, csum=0, data=None)) # Add if UDP if 'udp' in fields['ptype']: pkt_out = addIPv4(pkt_out,fields) pkt_out.add_protocol(udp.udp(dst_port=fields['dstport'], bits=fields['bits'],option=fields['opt'], src_port=fields['srcport'])) # Add if TCP if 'tcp' in fields['ptype']: pkt_out = addIPv4(pkt_out,fields) pkt_out.add_protocol(tcp.tcp(dst_port=fields['dstport'], bits=fields['bits'],option=fields['opt'], src_port=fields['srcport'])) #Add covert channel information if fields['com'] != None: pkt_out.add_protocol(fields['com']) #Send crafted packet self._send_packet(fields['dp'], ops['newport'], pkt_out)
def _remove_tcp_options(self, pkt): eth = pkt.get_protocols( ethernet.ethernet)[0] # type: ethernet.ethernet ip = pkt.get_protocols(ipv4.ipv4)[0] # type: ipv4.ipv4 ptcp = pkt.get_protocols(tcp.tcp)[0] # type: tcp.tcp if (ptcp.has_flags(tcp.TCP_SYN)): new_ip = ipv4.ipv4(version=ip.version, header_length=5, tos=ip.tos, total_length=0, identification=ip.identification, flags=ip.flags, offset=ip.offset, ttl=ip.ttl, proto=ip.proto, csum=0, src=ip.src, dst=ip.dst, option=ip.option) # Remove TCP Timestamp and SACK permitted Option as it prevents the handover from working new_options = [] for option in ptcp.option: # type: tcp.TCPOption if not option.kind in [ tcp.TCP_OPTION_KIND_TIMESTAMPS, tcp.TCP_OPTION_KIND_SACK_PERMITTED ]: new_options.append(option) new_ptcp = tcp.tcp(src_port=ptcp.src_port, dst_port=ptcp.dst_port, seq=ptcp.seq, ack=ptcp.ack, offset=0, bits=ptcp.bits, window_size=ptcp.window_size, csum=0, urgent=ptcp.urgent, option=new_options) new_pkt = packet.Packet() new_pkt.add_protocol(eth) new_pkt.add_protocol(new_ip) new_pkt.add_protocol(new_ptcp) new_pkt.serialize() return new_pkt else: return pkt
def sendpkt(self, dpid, pkt, port=None): srcip = pkt['srcip'] dstip = pkt['dstip'] srcport = pkt['srcport'] dstport = pkt['dstport'] pkt = packet.Packet() pkt.add_protocol(ethernet.ethernet(ethertype=2048)) # pkt.add_protocol(ethernet.ethernet(ethertype=2048,src='11:22:33:44:55:66',dst='11:22:33:44:55:66')) # proto=6 tcp; proto=17 udp pkt.add_protocol(ipv4.ipv4(proto=6, src=srcip, dst=dstip)) #pkt.add_protocol(udp.udp(src_port=srcport,dst_port=dstport)) pkt.add_protocol(tcp.tcp(src_port=srcport, dst_port=dstport)) datapath = get_switch(self, dpid)[0].dp self.pkt_out(datapath, pkt, port)
def generateSYNACKtoSYN(self): ipv4_p = self.syn_pkt.get_protocol(ipv4.ipv4) tcp_P = self.syn_pkt.get_protocol(tcp.tcp) eth_p = self.syn_pkt.get_protocol(ethernet.ethernet) e = ethernet.ethernet(dst=eth_p.src, src=eth_p.dst) #The application allow to generate TCP/IP stack fingerprint by setting packet header information: IP ID, SYN seq number, etc. #In this version we simply use the inbound packet's header information ip = ipv4.ipv4(4, 5, ipv4_p.tos, ipv4_p.total_length, ipv4_p.identification, ipv4_p.flags, 0, ipv4_p.ttl, 6, 0, src=ipv4_p.dst, dst=ipv4_p.src, option=None) bits = 0 | 1 << 1 | 1 << 4 # SYN and ACK set #In this version we simply generate random SYN seq number random.seed(1) self.controller_syn_seq = random.randint(4000000000, 4100000000) seq = self.controller_syn_seq tcpd = tcp.tcp(tcp_P.dst_port, tcp_P.src_port, seq, tcp_P.seq + 1, 0, bits, 65535, 0, 0, tcp_P.option) print "SYN_ACK option=", tcp_P.option #for p in self.syn_pkt: # if p.protocol_name == 'ethernet': # self.client_src_mac = p.src # e = ethernet.ethernet(dst=p.src, src=p.dst) # if p.protocol_name == 'ipv4': # ip = ipv4.ipv4(4, 5, p.tos, 0, 0, 0, 0, 255, 6, 0, src=p.dst, dst=p.src, option=None) # if p.protocol_name == 'tcp': # bits = 0 | 1 << 1 | 1 << 4 # SYN and ACK set # self.controller_syn_seq = 1 #random.randint(1, 100) # select random seq #TODO need a function to select it according to different OS # seq = self.controller_syn_seq #print "src_port=", p.src_port, "dst_port=", p.dst_port, "seq=", p.seq, "ack=", p.ack, "bits=", p.bits, "win_size=", p.window_size, "urg=", p.urgent, "option=", p.option # tcpd = tcp.tcp(p.dst_port, p.src_port, seq, p.seq+1, 0, bits, 65535, 0, 0, None) p = packet.Packet() p.add_protocol(e) p.add_protocol(ip) p.add_protocol(tcpd) p.serialize() print "SYN_ACK is generated." return p
def tcp(self, imsi='001010000000013', src_mac='00:00:00:00:00:00', src_ip='192.168.70.2', src_port=80, dst_mac='ff:ff:ff:ff:ff:ff', dst_ip='192.168.70.3', dst_port=80, bits=tcp.TCP_SYN, seq=0, ack=0): """ Send a TCP packet through the magma switch and display which tabled caused a drop (-1 if the packet wasn't dropped by any table) """ pkt = ethernet.ethernet(src=src_mac, dst=dst_mac) / \ ipv4.ipv4(ttl=55, proto=6, src=src_ip, dst=dst_ip) / \ tcp.tcp(src_port=src_port, dst_port=dst_port, bits=bits, seq=seq, ack=ack) pkt.serialize() self.raw(data=pkt.data, imsi=imsi)
def receiveprobe(self, msg): dp = msg.datapath #print msg.data in_port = msg.match['in_port'] dpid = dp.id ofproto = dp.ofproto parser = dp.ofproto_parser pkt = packet.Packet(data=msg.data) pkt_ethernet = pkt.get_protocol(ethernet.ethernet) pkt_mpls = pkt.get_protocol(mpls.mpls) pkt_ipv4 = pkt.get_protocol(ipv4.ipv4) pkt_tcp = pkt.get_protocol(tcp.tcp) #print pkt_mpls if pkt_mpls.label == self.colortag[dpid]: return dstip = pkt_ipv4.dst #print dstip dstdp = self.get_host_location(dstip) if dpid == dstdp[0]: self.logger.info("current hop is : %s", dp.id) print "tracing is over" return #print dpid,in_port self.logger.info("current hop is : %s", dp.id) probe = packet.Packet() probe.add_protocol( ethernet.ethernet(dst=pkt_ethernet.dst, src=pkt_ethernet.src, ethertype=0x8847)) probe.add_protocol(mpls.mpls(label=self.colortag[dpid])) probe.add_protocol( ipv4.ipv4(proto=6, src=pkt_ipv4.src, dst=pkt_ipv4.dst)) probe.add_protocol( tcp.tcp(src_port=pkt_tcp.src_port, dst_port=pkt_tcp.dst_port)) #print "this dp's color is: ",self.colortag[dpid] #print probe probe.serialize() data = probe.data #self.pcap_pen.write_pkt(data) #print "re-send" #print 'current dpid: ',dp.id actions = [parser.OFPActionOutput(ofproto.OFPP_TABLE)] out = parser.OFPPacketOut(datapath=dp, buffer_id=ofproto.OFP_NO_BUFFER, in_port=in_port, actions=actions, data=data) dp.send_msg(out)
def switch_features_handler(self, ev): datapath = ev.msg.datapath ofproto = datapath.ofproto parser = datapath.ofproto_parser # install table-miss flow entry # # We specify NO BUFFER to max_len of the output action due to # OVS bug. At this moment, if we specify a lesser number, e.g., # 128, OVS will send Packet-In with invalid buffer_id and # truncated packet data. In that case, we cannot output packets # correctly. The bug has been fixed in OVS v2.1.0. match = parser.OFPMatch() actions = [parser.OFPActionOutput(ofproto.OFPP_CONTROLLER, ofproto.OFPCML_NO_BUFFER)] self.add_flow(datapath, 0, match, actions) if int(datapath.id) == 2: match = parser.OFPMatch(eth_type=0x0800,ipv4_src='0.0.0.1', ipv4_dst='0.0.0.2') actions = [parser.OFPActionSetField(ipv4_src='0.0.0.0'), parser.OFPActionSetField(ipv4_dst='0.0.0.0'), parser.OFPActionOutput(ofproto.OFPP_CONTROLLER, ofproto.OFPCML_NO_BUFFER)] self.add_flow(datapath, 1, match, actions) if int(datapath.id) == 1 : self.dp_add = datapath pkt = packet.Packet() pkt.add_protocol(ethernet.ethernet(ethertype=0x0800, dst='75:43:21:13:15:17', src='75:43:12:31:51:71')) pkt.add_protocol(ipv4.ipv4(dst='0.0.0.2', src='0.0.0.1', ttl=254,proto=6)) pkt.add_protocol(tcp.tcp(src_port=1234, dst_port=5001, seq=1234566)) pkt.add_protocol('j'*1380) pkt.serialize() actions = [parser.OFPActionOutput(ofproto.OFPP_TABLE)] match = parser.OFPMatch(eth_type=0x0800,ipv4_src='0.0.0.1', ipv4_dst='0.0.0.2') actions = [parser.OFPActionOutput(2), parser.OFPActionOutput(2)] self.add_flow(datapath, 1, match, actions) #self.start_time[int(datapath.id)-1] = datetime.datetime.now().microsecond datapath.send_packet_out(buffer_id=ofproto.OFP_NO_BUFFER, in_port=ofproto.OFPP_CONTROLLER, actions=actions, data=pkt.data)
def test_serialize_option(self): offset = 6 csum = 0 option = '\x01\x02' src_ip = '192.168.10.1' dst_ip = '192.168.100.1' prev = ipv4(4, 5, 0, 0, 0, 0, 0, 64, inet.IPPROTO_TCP, 0, src_ip, dst_ip) t = tcp(self.src_port, self.dst_port, self.seq, self.ack, offset, self.bits, self.window_size, csum, self.urgent, option) buf = t.serialize(bytearray(), prev) r_option = buf[tcp._MIN_LEN:tcp._MIN_LEN + len(option)] eq_(option, r_option)
def sampleTcpPayload(): e = ethernet.ethernet(dst=ETHERNET_DATA["dst_mac"], src=ETHERNET_DATA["src_mac"], ethertype=TCP_DATA["ethertype"]) pip = ipv4.ipv4(src=TCP_DATA["src_ip"], dst=TCP_DATA["dst_ip"], proto=TCP_DATA["proto"]) ptcp = tcp.tcp(src_port=TCP_DATA["src_port"], dst_port=TCP_DATA["dst_port"]) p = packet.Packet() p.add_protocol(e) p.add_protocol(pip) p.add_protocol(ptcp) p.serialize() return NotBuffered(binascii.a2b_base64(binascii.b2a_base64(p.data)))
def build_syn_packet(): e = ethernet.ethernet(dst='00:1e:68:bb:b5:2e', src='00:23:54:6c:29:14', ethertype=ether.ETH_TYPE_IP) i = ipv4.ipv4(dst='140.114.71.177', src='192.168.11.23', proto=inet.IPPROTO_TCP) t = tcp.tcp(src_port=5656, dst_port=9292, bits=tcp.TCP_SYN) p = packet.Packet() p.add_protocol(e) p.add_protocol(i) p.add_protocol(t) p.serialize() return p
def _tracing(self): hub.sleep(15) srcip = '10.0.0.1' dstip = '10.0.0.10' srcport = 30000 dstport = 5001 #data = 'helloworld' srcdp = self.get_host_location(srcip) dstdp = self.get_host_location(dstip) dstmac = self.access_table[dstdp] srcmac = self.access_table[srcdp] #print dstmac,srcmac ofproto = self.datapaths[srcdp[0]].ofproto parser = self.datapaths[srcdp[0]].ofproto_parser count = 5 """ while count: pkt = packet.Packet() pkt.add_protocol(ethernet.ethernet(ethertype=ether.ETH_TYPE_IP,dst=dstmac[1],src=srcmac[1])) pkt.add_protocol(ipv4.ipv4(dst=dstip,src=srcip,proto=6)) pkt.add_protocol(tcp.tcp(src_port=srcport,dst_port=dstport)) pkt.serialize() data = pkt.data actions = [parser.OFPActionOutput(ofproto.OFPP_TABLE)] out = parser.OFPPacketOut(datapath=self.datapaths[srcdp[0]],buffer_id=ofproto.OFP_NO_BUFFER,in_port=srcdp[1],actions=actions,data=data) self.datapaths[srcdp[1]].send_msg(out) count = count - 1 """ pkt = packet.Packet() pkt.add_protocol( ethernet.ethernet(dst=dstmac[1], src=srcmac[1], ethertype=0x8847)) pkt.add_protocol(mpls.mpls(label=self.colortag[self.datapaths[1].id])) pkt.add_protocol(ipv4.ipv4(proto=6, src=srcip, dst=dstip)) pkt.add_protocol(tcp.tcp(src_port=srcport, dst_port=dstport)) #print srcdp[1] pkt.serialize() data = pkt.data actions = [parser.OFPActionOutput(ofproto.OFPP_TABLE)] out = parser.OFPPacketOut(datapath=self.datapaths[srcdp[0]], buffer_id=ofproto.OFP_NO_BUFFER, in_port=srcdp[1], actions=actions, data=data) self.datapaths[1].send_msg(out) #print "done" self.logger.info("current hop is : %s", self.datapaths[1].id)
def build_packet(self): print "## BUILD PACKET" e = ethernet.ethernet(dst='ff:ff:ff:ff:ff:ff', src='08:60:6e:7f:74:e7', ethertype=ether.ETH_TYPE_ARP) a = arp.arp(hwtype=1, proto=0x0800, hlen=6, plen=4, opcode=2, src_mac='08:60:6e:7f:74:e7', src_ip='192.0.2.1', dst_mac='00:00:00:00:00:00', dst_ip='192.0.2.2') p = packet.Packet() p.add_protocol(e) p.add_protocol(a) p.serialize() print repr(p.data) # the on-wire packet # pkt = tcp.tcp(bits=(tcp.TCP_SYN | tcp.TCP_ACK)) pkt = tcp.tcp(bits=(tcp.TCP_SYN | tcp.TCP_ACK)) if pkt.has_flags(tcp.TCP_SYN, tcp.TCP_ACK): print "BUILD pkt has tcp SYN/ACK flags" # repr(pkt.)
def switch_features_handler(self, ev): datapath = ev.msg.datapath ofproto = datapath.ofproto parser = datapath.ofproto_parser # install table-miss flow entry # # We specify NO BUFFER to max_len of the output action due to # OVS bug. At this moment, if we specify a lesser number, e.g., # 128, OVS will send Packet-In with invalid buffer_id and # truncated packet data. In that case, we cannot output packets # correctly. The bug has been fixed in OVS v2.1.0. match = parser.OFPMatch() actions = [parser.OFPActionOutput(ofproto.OFPP_CONTROLLER, ofproto.OFPCML_NO_BUFFER)] self.add_flow(datapath, 0, match, actions) pkt = packet.Packet() pkt.add_protocol(ethernet.ethernet(ethertype=0x0800, dst='70:56:81:12:34:56', src='70:56:81:65:43:21')) pkt.add_protocol(ipv4.ipv4(dst='192.168.8.70', src='192.168.8.50', proto=6)) pkt.add_protocol(tcp.tcp(src_port=5566, dst_port=8080, seq=1234566)) pkt.serialize() actions = [parser.OFPActionOutput(ofproto.OFPP_CONTROLLER)] #out = parser.OFPPacketOut(datapath=datapath, buffer_id=ofproto.OFP_NO_BUFFER, # in_port=1, actions=actions, data=data) # ??? pkt.data has self.start_time[int(datapath.id)-1] = datetime.datetime.now().microsecond #print self.start_time[int(datapath.id)-1] #datapath.send_msg(out) datapath.send_packet_out(buffer_id=ofproto.OFP_NO_BUFFER, in_port=1, actions=actions, data=pkt.data)
def test_serialize(self): offset = 5 csum = 0 src_ip = "192.168.10.1" dst_ip = "192.168.100.1" prev = ipv4(4, 5, 0, 0, 0, 0, 0, 64, inet.IPPROTO_TCP, 0, src_ip, dst_ip) t = tcp.tcp( self.src_port, self.dst_port, self.seq, self.ack, offset, self.bits, self.window_size, csum, self.urgent ) buf = t.serialize(bytearray(), prev) res = struct.unpack(tcp.tcp._PACK_STR, six.binary_type(buf)) eq_(res[0], self.src_port) eq_(res[1], self.dst_port) eq_(res[2], self.seq) eq_(res[3], self.ack) eq_(res[4], offset << 4) eq_(res[5], self.bits) eq_(res[6], self.window_size) eq_(res[8], self.urgent) # test __len__ # offset indicates the number of 32 bit (= 4 bytes) # words in the TCP Header. # So, we compare len(tcp) with offset * 4, here. eq_(offset * 4, len(t)) # checksum ph = struct.pack( "!4s4sBBH", addrconv.ipv4.text_to_bin(src_ip), addrconv.ipv4.text_to_bin(dst_ip), 0, 6, offset * 4 ) d = ph + buf s = packet_utils.checksum(d) eq_(0, s)
def _packet_in_handler(self, ev): # If you hit this you might want to increase # the "miss_send_length" of your switch if ev.msg.msg_len < ev.msg.total_len: self.logger.debug("packet truncated: only %s of %s bytes", ev.msg.msg_len, ev.msg.total_len) msg = ev.msg datapath = msg.datapath ofproto = datapath.ofproto parser = datapath.ofproto_parser in_port = msg.match['in_port'] pkt = packet.Packet(msg.data) eth = pkt.get_protocols(ethernet.ethernet)[0] dst = eth.dst src = eth.src nw = pkt.get_protocol(ipv4.ipv4) tp = pkt.get_protocol(tcp.tcp) if nw is not None and nw.src == '192.168.8.50': print ("####") self.end_time[int(datapath.id)-1] = datetime.datetime.now().microsecond #print int(datapath.id) #print self.start_time[int(datapath.id)-1] #print self.end_time[int(datapath.id)-1] #print datetime.datetime.now().microsecond if int(datapath.id) == 1: pkt = packet.Packet() pkt.add_protocol(ethernet.ethernet(ethertype=0x0800, dst='70:56:81:12:34:56', src='70:56:81:65:43:21')) pkt.add_protocol(ipv4.ipv4(dst='192.168.8.70', src='192.168.8.51', proto=6)) pkt.add_protocol(tcp.tcp(src_port=5566, dst_port=8080, seq=1234566)) pkt.serialize() actions = [parser.OFPActionOutput(2)] self.start_time[2] = datetime.datetime.now().microsecond datapath.send_packet_out(buffer_id=ofproto.OFP_NO_BUFFER, in_port=1, actions=actions, data=pkt.data) elif nw is not None and nw.src == '192.168.8.51': print ("####") self.end_time[2] = datetime.datetime.now().microsecond print ("switch1---->switch2") #print self.end_time[2]-self.start_time[2] print self.start_time[0] print self.end_time[0] print self.start_time[1] print self.end_time[1] print self.start_time[2] print self.end_time[2] print self.end_time[2]-self.start_time[2]-(self.end_time[0]-self.start_time[0]+self.end_time[1]-self.start_time[1])/2 else: dpid = datapath.id self.mac_to_port.setdefault(dpid, {}) #self.logger.info("packet in %s %s %s %s", dpid, src, dst, in_port) # learn a mac address to avoid FLOOD next time. self.mac_to_port[dpid][src] = in_port if dst in self.mac_to_port[dpid]: out_port = self.mac_to_port[dpid][dst] else: out_port = ofproto.OFPP_FLOOD actions = [parser.OFPActionOutput(out_port)] # install a flow to avoid packet_in next time if out_port != ofproto.OFPP_FLOOD: match = parser.OFPMatch(in_port=in_port, eth_dst=dst) # verify if we have a valid buffer_id, if yes avoid to send both # flow_mod & packet_out if msg.buffer_id != ofproto.OFP_NO_BUFFER: self.add_flow(datapath, 1, match, actions, msg.buffer_id) return else: self.add_flow(datapath, 1, match, actions) data = None if msg.buffer_id == ofproto.OFP_NO_BUFFER: data = msg.data out = parser.OFPPacketOut(datapath=datapath, buffer_id=msg.buffer_id, in_port=in_port, actions=actions, data=data) datapath.send_msg(out)
def build_tcp_packet(pkt,ctl,add_seq=0): #ethernet proto eth = pkt.get_protocols(ethernet.ethernet)[0] #ip proto ip = pkt.get_protocols(ipv4.ipv4)[0] dst_ip = ip.src src_ip = ip.dst identification = random.randint(1, 5000) #tcp proto mytcp = pkt.get_protocols(tcp.tcp)[0] myseq = mytcp.ack myack = mytcp.seq option = mytcp.option #Syn bit if mytcp.bits & 0b000010 : bits = 0b010010 myseq = random.getrandbits(32) myack += 1 #mem_add_tcp_seq(str(myseq),str(myack+1)) '''for i in range(0,len(option)): if option[i].kind == tcp.TCP_OPTION_KIND_TIMESTAMPS: my_ts_ecr = option[i].ts_val my_tc_val = (int(time.time()) & 0xffffffff) option[i] = tcp.TCPOptionTimestamps(ts_val=my_tc_val, ts_ecr=my_ts_ecr)''' #Fin bit if mytcp.bits & 0b000001 : bits = 0b010001 myack += 1 '''for i in range(0,len(option)): if option[i].kind == tcp.TCP_OPTION_KIND_TIMESTAMPS: my_ts_ecr = option[i].ts_val my_tc_val = (int(time.time()) & 0xffffffff) option[i] = tcp.TCPOptionTimestamps(ts_val=my_tc_val, ts_ecr=my_ts_ecr)''' #Psh bit if mytcp.bits & 0b001000 : bits = 0b010000 myack += len(pkt.protocols[-1]) '''for i in range(0,len(option)): if option[i].kind == tcp.TCP_OPTION_KIND_TIMESTAMPS: my_ts_ecr = option[i].ts_val my_tc_val = (int(time.time()) & 0xffffffff) option[i] = tcp.TCPOptionTimestamps(ts_val=my_tc_val, ts_ecr=my_ts_ecr)''' #forged header if ctl: #forged openflow header #ethernet eth_pkt = eth #ip proto ip_pkt = ipv4.ipv4(version=4, header_length=5, tos=0, total_length=0, identification=ip.identification+1, flags=2, offset=0, ttl=64, proto=in_proto.IPPROTO_TCP, csum=0, src=ip.src, dst=ip.dst) #tcp proto #psh ack bit bits = 0b011000 tcp_pkt = tcp.tcp(src_port=mytcp.src_port, dst_port=mytcp.dst_port, seq=mytcp.seq+add_seq, ack=mytcp.ack, offset=0, bits=bits, window_size=2048, csum=0, urgent=0, option=None) else: #forged normal header #ethernet proto dst_mac = eth.src src_mac = eth.dst eth_pkt = ethernet.ethernet(dst_mac,src_mac,ethertype=ether_types.ETH_TYPE_IP) #ip proto ip_pkt = ipv4.ipv4(version=4, header_length=5, tos=0, total_length=0, identification=identification, flags=2, offset=0, ttl=64, proto=in_proto.IPPROTO_TCP, csum=0, src=src_ip, dst=dst_ip) #tcp proto tcp_pkt = tcp.tcp(src_port=mytcp.dst_port, dst_port=mytcp.src_port, seq=myseq, ack=myack, offset=0, bits=bits, window_size=2048, csum=0, urgent=0, option=None) p = packet.Packet() p.add_protocol(eth_pkt) p.add_protocol(ip_pkt) p.add_protocol(tcp_pkt) return p
def _packet_in_handler(self, ev): # If you hit this you might want to increase # the "miss_send_length" of your switch #if ev.msg.msg_len < ev.msg.total_len: # self.logger.debug("packet truncated: only %s of %s bytes", # ev.msg.msg_len, ev.msg.total_len) msg = ev.msg datapath = msg.datapath ofproto = datapath.ofproto parser = datapath.ofproto_parser in_port = msg.match['in_port'] pkt = packet.Packet(msg.data) eth = pkt.get_protocols(ethernet.ethernet)[0] dst = eth.dst src = eth.src nw = pkt.get_protocol(ipv4.ipv4) tp = pkt.get_protocol(tcp.tcp) if nw is not None and dst == '75:43:21:13:15:17': print nw.src print nw.dst self.re_time[self.count] = struct.unpack("I",socket.inet_aton(str(nw.src)))[0] self.re_time_u[self.count] = struct.unpack("I",socket.inet_aton(str(nw.dst)))[0] self.count = self.count + 1 if self.count == 2 : self.count = 0 result = self.re_time[1] - self.re_time[0] result = result * 1000000 + self.re_time_u[1] - self.re_time_u[0] print 11472.0/result #file_object = open('file.txt', 'a') #file_object.write(str(11472.0/result)) #file_object.write("\n") if result : self.data.append(11472.0/result) self.cc =self.cc + 1 if self.cc == 100: for i in range(20): x = max(self.data) y = min(self.data) self.data.remove(x) self.data.remove(y) print sum(self.data)/60 print sum(self.data) file_object = open('file.txt', 'a') file_object.write(str(sum(self.data)/60)) file_object.write("\n") else: pkt = packet.Packet() pkt.add_protocol(ethernet.ethernet(ethertype=0x0800, dst='75:43:21:13:15:17', src='75:43:12:31:51:71')) pkt.add_protocol(ipv4.ipv4(dst='0.0.0.2', src='0.0.0.1', ttl=254,proto=6)) pkt.add_protocol(tcp.tcp(src_port=1234, dst_port=5001, seq=1234566)) pkt.add_protocol('j'*1380) pkt.serialize() actions = [parser.OFPActionOutput(ofproto.OFPP_TABLE)] match = parser.OFPMatch(eth_type=0x0800,ipv4_src='0.0.0.1', ipv4_dst='0.0.0.2') actions = [parser.OFPActionOutput(2), parser.OFPActionOutput(2)] self.dp_add.send_packet_out(buffer_id=ofproto.OFP_NO_BUFFER, in_port=ofproto.OFPP_CONTROLLER, actions=actions, data=pkt.data) else: dpid = datapath.id self.mac_to_port.setdefault(dpid, {}) #self.logger.info("packet in %s %s %s %s", dpid, src, dst, in_port) # learn a mac address to avoid FLOOD next time. self.mac_to_port[dpid][src] = in_port if dst in self.mac_to_port[dpid]: out_port = self.mac_to_port[dpid][dst] else: out_port = ofproto.OFPP_FLOOD actions = [parser.OFPActionOutput(out_port)] # install a flow to avoid packet_in next time if out_port != ofproto.OFPP_FLOOD: match = parser.OFPMatch(in_port=in_port, eth_dst=dst) # verify if we have a valid buffer_id, if yes avoid to send both # flow_mod & packet_out if msg.buffer_id != ofproto.OFP_NO_BUFFER: self.add_flow(datapath, 1, match, actions, msg.buffer_id) return else: self.add_flow(datapath, 1, match, actions) data = None if msg.buffer_id == ofproto.OFP_NO_BUFFER: data = msg.data out = parser.OFPPacketOut(datapath=datapath, buffer_id=msg.buffer_id, in_port=in_port, actions=actions, data=data) datapath.send_msg(out)
def _build_pkt(self, fields, ops): pkt_out = packet.Packet() pkt_ipv4 = pkt_out.get_protocol(ipv4.ipv4) pkt_icmp = pkt_out.get_protocol(icmp.icmp) def addIPv4(pkt_out, fields): pkt_out.add_protocol(ipv4.ipv4(dst=fields['dstip'], version = 4, header_length = 5, tos = 0, total_length = 0, identification = fields['id'], flags=0x02, ttl = 63, proto = fields['proto'], csum = 0, option = None, src=fields['srcip'])) return pkt_out def addARP(pkt_out,fields): pkt_out.add_protocol(arp.arp(opcode=arp.ARP_REPLY, src_mac=fields['srcmac'], src_ip=fields['srcip'], dst_mac=fields['dstmac'], dst_ip=fields['dstip'])) return pkt_out pkt_out.add_protocol(ethernet.ethernet(ethertype=fields['ethtype'], dst=fields['dstmac'], src=fields['srcmac'])) # Add if ARP if 'arp' in fields['ptype']: pkt_out.add_protocol(arp.arp(opcode=arp.ARP_REPLY, src_mac=fields['srcmac'], src_ip=fields['srcip'], dst_mac=fields['dstmac'], dst_ip=fields['dstip'])) # Add if IPv4 if 'ipv4' in fields['ptype']: pkt_out = addIPv4(pkt_out,fields) # Add if ICMP if 'icmp' in fields['ptype']: pkt_out = addIPv4(pkt_out,fields) pkt_out.add_protocol(icmp.icmp(type_=icmp.ICMP_ECHO_REPLY, code=icmp.ICMP_ECHO_REPLY_CODE, csum=0, data=None)) # Add if UDP if 'udp' in fields['ptype']: #pkt_out = addARP(pkt_out,fields) pkt_out = addIPv4(pkt_out,fields) pkt_out.add_protocol(udp.udp(dst_port=fields['dstport'], csum = 0, total_length = 0, src_port=fields['srcport'])) # bits=fields['bits'],option=fields['opt'], ## ## # Add if TCP if 'tcp' in fields['ptype']: pkt_out = addIPv4(pkt_out,fields) pkt_out.add_protocol(tcp.tcp(dst_port=fields['dstport'], bits=fields['bits'],option=fields['opt'], src_port=fields['srcport'])) #Add covert channel information if fields['com'] != None: pkt_out.add_protocol(fields['com']) #Send crafted packet print "Packet out: \n" print pkt_out self._send_packet(fields['dp'], ops['newport'], pkt_out)
def _packet_in_handler(self, ev): msg = ev.msg datapath = msg.datapath ofproto = datapath.ofproto parser = datapath.ofproto_parser in_port = msg.match['in_port'] port = in_port pkt = packet.Packet(msg.data) eth = pkt.get_protocols(ethernet.ethernet)[0] dst = eth.dst src = eth.src actionport = 0 dpid = datapath.id pkt_arp = pkt.get_protocol(arp.arp) pkt_udp = pkt.get_protocol(udp.udp) pkt_icmp = pkt.get_protocol(icmp.icmp) pkt_tcp = pkt.get_protocol(tcp.tcp) pkt_ethernet = pkt.get_protocol(ethernet.ethernet) pkt_ipv4 = pkt.get_protocol(ipv4.ipv4) if pkt_tcp or pkt_icmp: #print pkt_ipv4 src_ip = pkt_ipv4.src dst_ip = pkt_ipv4.dst if dpid == 3: if dst == '00:00:00:00:00:02' or dst == '00:00:00:00:00:04': action = [parser.OFPActionOutput(3)] actionport = 3 match1 = parser.OFPMatch(eth_dst='00:00:00:00:00:02',eth_type=0x0800,ip_proto=6) match1a = parser.OFPMatch(eth_dst='00:00:00:00:00:04',eth_type=0x0800,ip_proto=6) self.logger.info("This line gets executed") self.add_flow(datapath, 1, match1, action) self.add_flow(datapath, 1, match1a, action) match1 = parser.OFPMatch(eth_dst='00:00:00:00:00:02',eth_type=0x0800,ip_proto=1) match1a = parser.OFPMatch(eth_dst='00:00:00:00:00:04',eth_type=0x0800,ip_proto=1) self.logger.info("This line gets executed") self.add_flow(datapath, 1, match1, action) self.add_flow(datapath, 1, match1a, action) elif (dst == '00:00:00:00:00:03' and src == '00:00:00:00:00:01' and pkt_tcp) or (dst == '00:00:00:00:00:01' and src == '00:00:00:00:00:03' and pkt_tcp): #self._handle_tcp_reset(self,datapath, dst, src, pkt_ethernet, pkt_ipv4, in_port, src_ip, dst_ip) new_pkt = packet.Packet() new_pkt.add_protocol(ethernet.ethernet(ethertype=pkt_ethernet.ethertype, dst=src, src=dst)) new_pkt.add_protocol(ipv4.ipv4(dst=src_ip, src=dst_ip, proto=pkt_ipv4.proto)) new_pkt.add_protocol(tcp.tcp(src_port=80,dst_port=1, seq=0, ack=0, offset=0, bits=20, window_size=0, csum=0, urgent=0, option=None)) self._send_packet(datapath, in_port, new_pkt) #self.send_set_config(self, datapath) ofp = datapath.ofproto ofp_parser = datapath.ofproto_parser req = ofp_parser.OFPSetConfig(datapath, ofp.OFPC_FRAG_DROP, 256) datapath.send_msg(req) return elif dst == '00:00:00:00:00:01': action = [parser.OFPActionOutput(1)] actionport = 1 match2 = parser.OFPMatch(eth_dst='00:00:00:00:00:01',eth_type=0x0800,ip_proto=6) self.add_flow(datapath, 1, match2, action) match2 = parser.OFPMatch(eth_dst='00:00:00:00:00:01',eth_type=0x0800,ip_proto=1) self.add_flow(datapath, 1, match2, action) elif dst == '00:00:00:00:00:03': action = [parser.OFPActionOutput(2)] actionport = 2 match3 = parser.OFPMatch(eth_dst='00:00:00:00:00:03',eth_type=0x0800,ip_proto=6) self.add_flow(datapath, 1, match3, action) match3 = parser.OFPMatch(eth_dst='00:00:00:00:00:03',eth_type=0x0800,ip_proto=1) self.add_flow(datapath, 1, match3, action) else: print '' elif dpid == 4: if dst == '00:00:00:00:00:02': action = [parser.OFPActionOutput(1)] actionport = 1 match1 = parser.OFPMatch(eth_dst='00:00:00:00:00:02',eth_type=0x0800,ip_proto=6) self.add_flow(datapath, 1, match1, action) match1 = parser.OFPMatch(eth_dst='00:00:00:00:00:02',eth_type=0x0800,ip_proto=1) self.add_flow(datapath, 1, match1, action) elif dst == '00:00:00:00:00:04': action = [parser.OFPActionOutput(2)] actionport = 2 match3 = parser.OFPMatch(eth_dst='00:00:00:00:00:04',eth_type=0x0800,ip_proto=6) self.add_flow(datapath, 1, match3, action) match3 = parser.OFPMatch(eth_dst='00:00:00:00:00:04',eth_type=0x0800,ip_proto=1) self.add_flow(datapath, 1, match3, action) elif dst == '00:00:00:00:00:01' or dst == '00:00:00:00:00:03': action = [parser.OFPActionOutput(3)] actionport = 3 match2 = parser.OFPMatch(eth_dst='00:00:00:00:00:01',eth_type=0x0800,ip_proto=6) match2a = parser.OFPMatch(eth_dst='00:00:00:00:00:03',eth_type=0x0800,ip_proto=6) self.add_flow(datapath, 1, match2, action) self.add_flow(datapath, 1, match2a, action) match2 = parser.OFPMatch(eth_dst='00:00:00:00:00:01',eth_type=0x0800,ip_proto=1) match2a = parser.OFPMatch(eth_dst='00:00:00:00:00:03',eth_type=0x0800,ip_proto=1) self.add_flow(datapath, 1, match2, action) self.add_flow(datapath, 1, match2a, action) else: print '' else: print '' #actionport = self._handle_tcp(datapath, dst, dpid, parser) if pkt_udp: if dpid == 3: if (dst == '00:00:00:00:00:02' and src == '00:00:00:00:00:03'): actions = {} out = parser.OFPPacketOut(datapath=datapath, buffer_id=msg.buffer_id, in_port=in_port, actions=actions, data=None) datapath.send_msg(out) match = parser.OFPMatch(eth_dst='00:00:00:00:00:02', eth_src='00:00:00:00:00:03',eth_type=0x0800,ip_proto=17) self.add_flow(datapath,3,match,actions) return elif dst == '00:00:00:00:00:02' or dst == '00:00:00:00:00:04': action = [parser.OFPActionOutput(4)] actionport = 4 match1 = parser.OFPMatch(eth_dst='00:00:00:00:00:02',eth_type=0x0800,ip_proto=17) match1a = parser.OFPMatch(eth_dst='00:00:00:00:00:04',eth_type=0x0800,ip_proto=17) self.add_flow(datapath, 1, match1, action) self.add_flow(datapath, 1, match1a, action) elif dst == '00:00:00:00:00:01': action = [parser.OFPActionOutput(1)] actionport = 1 match2 = parser.OFPMatch(eth_dst='00:00:00:00:00:01',eth_type=0x0800,ip_proto=17) self.add_flow(datapath, 1, match2, action) elif dst == '00:00:00:00:00:03': action = [parser.OFPActionOutput(2)] actionport = 2 match3 = parser.OFPMatch(eth_dst='00:00:00:00:00:03',eth_type=0x0800,ip_proto=17) self.add_flow(datapath, 1, match3, action) else: print '' elif dpid == 4: if (dst == '00:00:00:00:00:03' and src == '00:00:00:00:00:02'): actions = {} out = parser.OFPPacketOut(datapath=datapath, buffer_id=msg.buffer_id, in_port=in_port, actions=actions, data=None) datapath.send_msg(out) match = parser.OFPMatch(eth_dst='00:00:00:00:00:03', eth_src='00:00:00:00:00:02',eth_type=0x0800,ip_proto=17) self.add_flow(datapath,3,match,actions) return elif dst == '00:00:00:00:00:02': action = [parser.OFPActionOutput(1)] actionport = 1 match1 = parser.OFPMatch(eth_dst='00:00:00:00:00:02',eth_type=0x0800,ip_proto=17) self.add_flow(datapath, 1, match1, action) elif dst == '00:00:00:00:00:04': action = [parser.OFPActionOutput(2)] actionport = 2 match3 = parser.OFPMatch(eth_dst='00:00:00:00:00:04',eth_type=0x0800,ip_proto=17) self.add_flow(datapath, 1, match3, action) elif dst == '00:00:00:00:00:01' or dst == '00:00:00:00:00:03': action = [parser.OFPActionOutput(4)] actionport = 4 match2 = parser.OFPMatch(eth_dst='00:00:00:00:00:01',eth_type=0x0800,ip_proto=17) match2a = parser.OFPMatch(eth_dst='00:00:00:00:00:03',eth_type=0x0800,ip_proto=17) self.add_flow(datapath, 1, match2, action) self.add_flow(datapath, 1, match2a, action) else: print '' elif dpid == 5: if in_port == 1: action1 = [parser.OFPActionOutput(2)] actionport = 2 match1 = parser.OFPMatch(in_port = in_port) self.add_flow(datapath, 1, match1, action1) elif in_port == 2: action2 = [parser.OFPActionOutput(1)] actionport = 1 match2 = parser.OFPMatch(in_port = in_port) self.add_flow(datapath, 1, match2, action2) else: print '' #self._handle_udp(datapath, eth.dst, dpid, parser) if pkt_arp: if self.arp_host.has_key(pkt_arp.dst_ip): self._handle_arp(datapath, in_port, pkt_ethernet, pkt_arp) return #self.mac_to_port.setdefault(dpid, {}) self.logger.info("packet in %s %s %s %s %s", dpid, src, dst, in_port, actionport) actions = [parser.OFPActionOutput(actionport)] data = None if msg.buffer_id == ofproto.OFP_NO_BUFFER: data = msg.data out = parser.OFPPacketOut(datapath=datapath, buffer_id=msg.buffer_id, in_port=in_port, actions=actions, data=data) datapath.send_msg(out)