def test_distribute_receive_packet_query(self): """ pkt_eth = packet.get_protocols(ethernet.ethernet) pkt_ipv6 = packet.get_protocols(ipv6.ipv6) pkt_icmpv6_list = packet.get_protocols(icmpv6.icmpv6) for pkt_icmpv6 in pkt_icmpv6_list: for pkt_icmpv6 in pkt_icmpv6_list: # MLDv2 Query if pkt_icmpv6.type_ == icmpv6.MLD_LISTENER_QUERY: self.logger.debug("MLDv2 Query : %s", str(pkt_icmpv6.data)) self.send_reply() # MLDv2 Report if pkt_icmpv6.type_ == icmpv6.MLDV2_LISTENER_REPORT: self.logger.debug("MLDv2 Report : %s", str(pkt_icmpv6.data)) self.send_multicast_info(pkt_icmpv6) """ addressinfo = ["00:11:22:33:44:55", "66:55:44:33:22:11", "1111::2222", "9999::8888"] mld = icmpv6.mldv2_query(address="1234::6789", srcs=["9876::4321"]) packet = self.mld_proc.create_packet(addressinfo, mld) self.mocker.StubOutWithMock(self.mld_proc, "send_reply") self.mld_proc.send_reply().AndReturn(0) self.mocker.ReplayAll() self.mld_proc.distribute_receive_packet(packet) self.mocker.UnsetStubs() self.mocker.VerifyAll()
def createPacket(self, src, dst, srcip, dstip): # create send packet # ether - vlan - ipv6 - icmpv6 ( - mldv2 ) sendpkt = packet.Packet() sendpkt.add_protocol(ethernet.ethernet( ethertype=ether.ETH_TYPE_8021Q, dst=dst, src=src)) sendpkt.add_protocol(vlan.vlan( pcp=0, cfi=0, vid=100, ethertype=ether.ETH_TYPE_IPV6)) sendpkt.add_protocol(ipv6.ipv6( src=srcip, dst=dstip, nxt=inet.IPPROTO_ICMPV6)) sendpkt.add_protocol(icmpv6.icmpv6( type_=icmpv6.ICMPV6_MEMBERSHIP_QUERY, data=icmpv6.mldv2_query(address='::'))) ''' sendpkt.add_protocol(icmpv6.icmpv6( type_=icmpv6.MLDV2_LISTENER_REPORT, data=icmpv6.mldv2_report( record_num=2, records=[ icmpv6.mldv2_report_group(type_=1, address='::'), icmpv6.mldv2_report_group(type_=2, address='::')]))) ''' sendpkt.serialize() return sendpkt
def createPacket(self, src, dst, srcip, dstip): # create send packet sendpkt = packet.Packet() sendpkt.add_protocol(ethernet.ethernet(ethertype=ether.ETH_TYPE_8021Q, dst=dst, src=src)) sendpkt.add_protocol(vlan.vlan(pcp=0, cfi=0, vid=100, ethertype=ether.ETH_TYPE_IPV6)) sendpkt.add_protocol(ipv6.ipv6(src=srcip, dst=dstip, nxt=inet.IPPROTO_ICMPV6)) sendpkt.add_protocol(icmpv6.icmpv6(type_=icmpv6.ICMPV6_MEMBERSHIP_QUERY, data=icmpv6.mldv2_query(address='ff38::1'))) sendpkt.serialize() return sendpkt
def create_mldquery(self, mc_address, mc_serv_ip): self.logger.debug("") query = icmpv6.mldv2_query( address=str(mc_address), srcs=[str(mc_serv_ip)] if mc_serv_ip else None, maxresp=self.QUERY_MAX_RESPONSE, qrv=self.QUERY_QRV, qqic=self.QQIC) self.logger.debug("created query : %s", str(query)) return query
def test_create_mldquery(self): """ self.logger.debug("") return icmpv6.mldv2_query(address=mc_addr, srcs=ip_addr_list, maxresp=10000, qqic=15) """ mc_addr = "::" ip_addr_list = [] expect = icmpv6.mldv2_query(address=mc_addr, srcs=ip_addr_list, maxresp=10000, qqic=15) actual = self.mld_proc.create_mldquery(mc_addr, ip_addr_list) eq_(expect.address, actual.address) eq_(expect.srcs, actual.srcs) eq_(expect.maxresp, actual.maxresp) eq_(expect.qqic, actual.qqic)
def test_create_packet(self): addressinfo = ["00:11:22:33:44:55", "66:55:44:33:22:11", "1111::2222", "9999::8888"] eth = ethernet.ethernet( ethertype=ether.ETH_TYPE_IPV6, src=addressinfo[0], dst=addressinfo[1]) ext_headers = [ipv6.hop_opts(nxt=inet.IPPROTO_ICMPV6, data=[ipv6.option(type_=5, len_=2, data="\x00\x00"), ipv6.option(type_=1, len_=0)])] ip6 = ipv6.ipv6(src=addressinfo[2], dst=addressinfo[3], hop_limit=1, nxt=inet.IPPROTO_HOPOPTS, ext_hdrs=ext_headers) mld = icmpv6.mldv2_query(address="1234::6789", srcs=["9876::4321"]) icmp6 = icmpv6_extend( type_=icmpv6.MLD_LISTENER_QUERY, data=mld) expect = eth / ip6 / icmp6 expect.serialize() actual = self.mld_proc.create_packet(addressinfo, mld) exp_eth = expect.get_protocols(ethernet.ethernet) exp_ip6 = expect.get_protocols(ipv6.ipv6) exp_extend = exp_ip6[0] exp_icmp6 = expect.get_protocols(icmpv6.icmpv6) exp_mld = exp_icmp6[0].data act_eth = actual.get_protocols(ethernet.ethernet) act_ip6 = actual.get_protocols(ipv6.ipv6) act_extend = act_ip6[0].ext_hdrs act_icmp6 = actual.get_protocols(icmpv6.icmpv6) act_mld = act_icmp6[0].data # TODO 確認方法 eq_(expect.data, actual.data) """
def create_mldquery(self, mc_addr, ip_addr_list): self.logger.debug("") return icmpv6.mldv2_query(address=mc_addr, srcs=ip_addr_list, maxresp=10000, qqic=15)
''' $ sudo ip link add veth0 type veth peer name veth1 $ sudo ip netns add ns0 $ sudo ip link set dev veth0 netns ns0 $ sudo ip netns exec ns0 bash # python send.py ''' import socket from ryu.lib.packet import packet, ethernet, vlan, ipv6, icmpv6 from ryu.ofproto import ether, inet sock = socket.socket(socket.AF_PACKET, socket.SOCK_RAW) sock.bind(('veth0', 0)) pkt = packet.Packet() pkt.add_protocol(ethernet.ethernet(ethertype=ether.ETH_TYPE_8021Q)) pkt.add_protocol(vlan.vlan(vid=10, ethertype=ether.ETH_TYPE_IPV6)) pkt.add_protocol(ipv6.ipv6(nxt=inet.IPPROTO_ICMPV6)) pkt.add_protocol(icmpv6.icmpv6( type_=icmpv6.MLD_LISTENER_QUERY, data=icmpv6.mldv2_query())) pkt.serialize() sock.send(pkt.data)
def _packet_in_handler(self, ev): self.packet_in_cnt += 1 self.packet_in_cnt_s += 1 msg = ev.msg datapath = msg.datapath ofproto = datapath.ofproto parser = datapath.ofproto_parser in_port = msg.match['in_port'] pkt = packet.Packet(msg.data) # get_protocols(ethernet) pkt_eth = pkt.get_protocols(ethernet.ethernet)[0] self.logger.debug('ethernet= %s ', str(pkt_eth)) dst = pkt_eth.dst src = pkt_eth.src pkt_ipv6 = None pkt_icmpv6 = None if 'ipv6' in self.PROTPCOL: # get_protocols(pkt_ipv6) pkt_ipv6 = pkt.get_protocols(ipv6.ipv6) if 0 < len(pkt_ipv6): self.logger.debug('ipv6= %s', str(pkt_ipv6)) # get_protocols(pkt_icmpv6) pkt_icmpv6 = pkt.get_protocols(icmpv6.icmpv6) if 0 < len(pkt_icmpv6): self.logger.debug('icmpv6= %s icmpv6.ND_NEIGHBOR_SOLICIT = %s' , str(pkt_icmpv6), icmpv6.ND_NEIGHBOR_SOLICIT) # if not pkt_icmpv6[0].type_ in [icmpv6.MLDV2_LISTENER_REPORT, icmpv6.ICMPV6_MEMBERSHIP_QUERY]: # print "icmpv6.type is " + str(pkt_icmpv6[0].type_) # return dpid = datapath self.mac_to_port.setdefault(dpid, {}) self.logger.debug('packet in %s %s %s %s %s', dpid, src, dst, in_port, str(self.packet_in_cnt)) # 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 self.logger.debug('in_port = %s, out_port = %s, OFPP_FLOOD = %s', str(in_port), str(out_port), str(ofproto.OFPP_FLOOD)) if out_port != ofproto.OFPP_FLOOD: if 'eth' in self.PROTPCOL: # match match = parser.OFPMatch(in_port=in_port, eth_dst=dst ) #miss match match = parser.OFPMatch(in_port=in_port, eth_type=0, eth_dst=dst ) elif 'ipv6' in self.PROTPCOL: match = parser.OFPMatch(in_port=in_port, eth_type=ether.ETH_TYPE_IPV6, ip_proto=inet.IPPROTO_ICMPV6, ipv6_dst=dst) self.add_flow(datapath, 1, match, actions) data = None if msg.buffer_id == ofproto.OFP_NO_BUFFER: data = msg.data # create send packet sendpkt = packet.Packet() sendpkt.add_protocol(ethernet.ethernet(ethertype=ether.ETH_TYPE_8021Q, dst=dst, src=src)) sendpkt.add_protocol(vlan.vlan(pcp=0, cfi=0, vid=100, ethertype=ether.ETH_TYPE_IPV6)) sendpkt.add_protocol(ipv6.ipv6(src=srcip, dst=dstip, nxt=inet.IPPROTO_ICMPV6)) sendpkt.add_protocol(icmpv6.icmpv6(type_=icmpv6.ICMPV6_MEMBERSHIP_QUERY, data=icmpv6.mldv2_query(address='::'))) sendpkt.serialize() self.logger.debug("packet-out %s" % (sendpkt,)) print "\n" data = sendpkt.data IPPROTO_ICMP = socket.getprotobyname('icmp') sock = socket.socket(socket.AF_INET6, socket.SOCK_RAW, IPPROTO_ICMP) while sendpkt.data: sent_bytes = sock.sendto(sendpkt.data, ('ff38::1', 0)) sendpkt.data = sendpkt.data[sent_bytes:] """ # out = parser.OFPPacketOut(datapath=datapath, buffer_id=msg.buffer_id, in_port=in_port, actions=actions, data=data) #out = parser.OFPPacketOut(datapath=datapath, buffer_id=0xffffffff, in_port=in_port, actions=actions, data=data) self.logger.debug("out %s %s %s %s" % (datapath, self.buffer_id, in_port, actions)) sendmsg = ofproto_v1_3_parser._set_msg_type(ofproto_v1_3.OFPT_PACKET_OUT); sendmsg.__init__(datapath) sendmsg.datapath = datapath sendmsg.buffer_id = 0xffffffff sendmsg.in_port = in_port sendmsg.actions_len = 0 sendmsg.actions = actions self.logger.debug("sendmsg %s %s %s %s" % (sendmsg.datapath, sendmsg.buffer_id, sendmsg.in_port, sendmsg.actions)) actions_len = 0 offset = ofproto_v1_3.OFP_PACKET_OUT_SIZE for a in actions: a.serialize(self.buf, offset) offset += a.len actions_len += a.len if sendpkt.data is not None: assert sendmsg.buffer_id == ofproto_v1_3.OFP_NO_BUFFER self.buf += sendpkt.data self.logger.debug("self.buf %s " % (self.buf)) sendout = msg_pack_into(ofproto_v1_3.OFP_PACKET_OUT_PACK_STR, self.buf, ofproto_v1_3.OFP_HEADER_SIZE, sendmsg.buffer_id, in_port, a.len) self.logger.debug("self.buf %s " % (self.buf)) IPPROTO_ICMP = socket.getprotobyname('icmp') sock = socket.socket(socket.AF_INET6, socket.SOCK_RAW, IPPROTO_ICMP) # sock.send(sendout) # while self.buf: # sent_bytes = sock.sendto(self.buf, ('ff38::1', 0)) # self.buf = self.buf[sent_bytes:] while self.buf: sent_bytes = sock.sendto(self.buf, ('ff38::1', 0)) self.buf = self.buf[sent_bytes:] # create_connection # sock.send() #datapath.send_packet_out(self, sendmsg.buffer_id, sendmsg.in_port, sendmsg.actions) """ #datapath.send_packet_out(buffer_id=0xffffffff, in_port=in_port, actions=actions, data=data) # datapath.send_msg(sendout) # datapath.send_msg(out) """ # create send packet sendpkt = packet.Packet() sendpkt.add_protocol(ethernet.ethernet(ethertype=ether.ETH_TYPE_8021Q, dst=dst, src=src)) sendpkt.add_protocol(vlan.vlan(pcp=0, cfi=0, vid=100, ethertype=ether.ETH_TYPE_IPV6)) sendpkt.add_protocol(ipv6.ipv6(src=pkt_ipv6[0].src, dst=pkt_ipv6[0].dst, nxt=inet.IPPROTO_ICMPV6)) sendpkt.add_protocol(icmpv6.icmpv6(type_=icmpv6.ICMPV6_MEMBERSHIP_QUERY, data=icmpv6.mldv2_query(address='::'))) sendpkt.serialize() newmsg = ofproto_v1_3_parser._set_msg_type(ofproto.OFPT_PACKET_OUT); self.logger.debug("newmsg %s " % (newmsg)) newmsg.__init__(datapath) newmsg.buffer_id = msg.buffer_id newmsg.in_port = in_port newmsg.actions_len = 0 newmsg.actions = actions self.buf print "\n" actions_len = 0 offset = ofproto.OFP_PACKET_OUT_SIZE for a in actions: a.serialize(self.buf, offset) offset += a.len actions_len += a.len self.logger.debug("DATA %s " % (sendpkt.data)) if sendpkt.data is not None: # assert msg.buffer_id == 0xffffffff self.buf += sendpkt.data msg_pack_into(ofproto.OFP_PACKET_OUT_PACK_STR, self.buf, ofproto.OFP_HEADER_SIZE, self.buffer_id, self.in_port, self.actions_len) if data is not None: newmsg.buf += sendpkt.data self.logger.debug("self.buf %s " % (newmsg.buf)) self.logger.debug("ofproto.OFP_PACKET_OUT_PACK_STR %s " % (ofproto.OFP_PACKET_OUT_PACK_STR)) self.logger.debug("ofproto.OFP_HEADER_SIZE %s " % (ofproto.OFP_HEADER_SIZE)) self.logger.debug("msg.buffer_id %s " % (newmsg.buffer_id)) self.logger.debug("in_port %s " % (in_port)) self.logger.debug("a.len %s " % (a.len)) out = msg_pack_into(ofproto.OFP_PACKET_OUT_PACK_STR, newmsg.buf, ofproto.OFP_HEADER_SIZE, newmsg.buffer_id, in_port, a.len) self.logger.debug("out %s " % (out)) # data = sendpkt.data # assert isinstance(msg, parser.MsgBase) IPPROTO_ICMP = socket.getprotobyname('icmp') sock = socket.socket(socket.AF_INET6, socket.SOCK_RAW, IPPROTO_ICMP) # msg.buffer_id == 0xffffffff # out = parser.OFPPacketOut(datapath=datapath, buffer_id=msg.buffer_id, # in_port=in_port, actions=actions, data=data) # msg(datapath, version, msg_type, msg_len, xid, buf): # parser.OFPMatch # out = parser.OFPPacketOut(datapath=datapath, buffer_id=msg.buffer_id, # in_port=in_port, actions=actions, data=data) # out.serialize() sock.send(out) # if sendpkt.xid is None: # parser.set # self.set_xid(sendpkt) # sendpkt.serialize() # LOG.debug('send_msg %s', msg) # self.send(msg.buf) # if self.data is not None: # assert self.buffer_id == 0xffffffff # self.buf += data """ # sock.sendto(sendpkt,dst) # datapath.send_msg(parserdata) """ self.buffer_id = ofproto.OFP_NO_BUFFER self.in_port = in_port self.actions = actions self.actions_len = 16 self.data = data print('self.buffer_id=' + str(self.buffer_id) + ' self.in_port=' + str(self.in_port) + ' self.actions=' + str(self.actions) + ' self.actions_len=' + str(self.actions_len) + ' self.data=' + str(self.data)) offset = ofproto.OFP_PACKET_OUT_SIZE print('offset=' + str(offset)) if self.data is not None: assert self.buffer_id == 0xffffffff self.buf += data print ("send %s " % data) parserdata = parser.msg_pack_into(ofproto_v1_3.OFP_PACKET_OUT_PACK_STR, self.buf, ofproto.OFP_HEADER_SIZE, self.buffer_id, self.in_port, self.actions_len) print ("send %s " % self.buf) print ("send %s " % parserdata) datapath.send_msg(parserdata) """ # sock.send(sendpkt) """ print('New switch is joined: %s' % datapath.id) flow = { 'match': { 'dl_type': ether.ETH_TYPE_IPV6, 'nw_proto': inet.IPPROTO_ICMPV6, }, 'actions': [ { 'type': 'OUTPUT', 'port': ofproto_v1_3.OFPP_NORMAL, }, ] } print('New flow: %s' % flow) self.actions_len = 0 offset = ofproto.OFP_PACKET_OUT_SIZE for a in self.actions: a.serialize(self.buf, offset) offset += a.len self.actions_len += a.len if self.data is not None: assert self.buffer_id == 0xffffffff self.buf += self.data msg_pack_into(ofproto.OFP_PACKET_OUT_PACK_STR, self.buf, ofproto.OFP_HEADER_SIZE, self.buffer_id, self.in_port, self.actions_len) # parser # parser(cls, datapath, version, msg_type, msg_len, xid, buf): parser.OFPPacketOut #out = parser.OFPPacketOut(datapath=datapath, buffer_id=msg.buffer_id, in_port=in_port, actions=actions, data=data) # out = parser.OFPPacketOut(datapath=datapath, buffer_id=0xffffffff, in_port=in_port, actions=actions, data=data) # MsgBase.set_headers() # buf = 'version: %s msg_type %s xid %s ' % ('0x1', '0x6', '0xd35262b') # buf = 'version: 0x%x msg_type 0x%x xid 0x%x ' % (self.OFP_VERSIONS, 0x6, 8) # print (str(buf)) aaa = parser.set_headers(self, '0x1', '0x6', '0xd35262b') # MsgBase(self, datapath) print aaa # datapath.ofproto_parser # ofproto_parser # MsgBase.set_buf(buf) # MsgBase.set_buf() # def parser(cls, datapath, version, msg_type, msg_len, xid, buf): # msg_ = cls(datapath) # msg_.set_headers(version, msg_type, msg_len, xid) # msg_.set_buf(buf) # return msg_ """ """ self.socket = socket self.socket.setsockopt(IPPROTO_TCP, TCP_NODELAY, 1) self.address = address self.is_active = True # The limit is arbitrary. We need to limit queue size to # prevent it from eating memory up self.send_q = hub.Queue(16) self.xid = random.randint(0, self.ofproto.MAX_XID) self.id = None # datapath_id is unknown yet self.ports = None self.flow_format = ofproto_v1_0.NXFF_OPENFLOW10 self.ofp_brick = ryu.base.app_manager.lookup_service_brick('ofp_event') self.set_state(handler.HANDSHAKE_DISPATCHER) """ #datapath.send_msg(out) """
def _packet_in_handler(self, ev): self.packet_in_cnt += 1 self.packet_in_cnt_s += 1 msg = ev.msg datapath = msg.datapath ofproto = datapath.ofproto parser = datapath.ofproto_parser in_port = msg.match['in_port'] pkt = packet.Packet(msg.data) # get_protocols(ethernet) pkt_eth = pkt.get_protocols(ethernet.ethernet)[0] self.logger.debug('ethernet= %s ', str(pkt_eth)) dst = pkt_eth.dst src = pkt_eth.src pkt_ipv6 = None pkt_icmpv6 = None if 'ipv6' in self.PROTPCOL: # get_protocols(pkt_ipv6) pkt_ipv6 = pkt.get_protocols(ipv6.ipv6) if 0 < len(pkt_ipv6): self.logger.debug('ipv6= %s', str(pkt_ipv6)) # get_protocols(pkt_icmpv6) pkt_icmpv6 = pkt.get_protocols(icmpv6.icmpv6) if 0 < len(pkt_icmpv6): self.logger.debug('icmpv6= %s icmpv6.ND_NEIGHBOR_SOLICIT = %s' , str(pkt_icmpv6), icmpv6.ND_NEIGHBOR_SOLICIT) if not pkt_icmpv6[0].type_ in [icmpv6.MLDV2_LISTENER_REPORT, icmpv6.ICMPV6_MEMBERSHIP_QUERY]: print "icmpv6.type is " + pkt_icmpv6[0].type return dpid = datapath self.mac_to_port.setdefault(dpid, {}) self.logger.debug('packet in %s %s %s %s %s', dpid, src, dst, in_port, str(self.packet_in_cnt)) # 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 self.logger.debug('in_port = %s, out_port = %s, OFPP_FLOOD = %s', str(in_port), str(out_port), str(ofproto.OFPP_FLOOD)) if out_port != ofproto.OFPP_FLOOD: if 'eth' in self.PROTPCOL: # match match = parser.OFPMatch(in_port=in_port, eth_dst=dst ) #miss match match = parser.OFPMatch(in_port=in_port, eth_type=0, eth_dst=dst ) elif 'ipv6' in self.PROTPCOL: match = parser.OFPMatch(in_port=in_port, eth_type=ether.ETH_TYPE_IPV6, ip_proto=inet.IPPROTO_ICMPV6, ipv6_dst=dst) self.add_flow(datapath, 1, match, actions) data = None if msg.buffer_id == ofproto.OFP_NO_BUFFER: data = msg.data # create send packet sendpkt = packet.Packet() sendpkt.add_protocol(ethernet.ethernet(ethertype=ether.ETH_TYPE_8021Q, dst=dst, src=src)) sendpkt.add_protocol(vlan.vlan(pcp=0, cfi=0, vid=100, ethertype=ether.ETH_TYPE_IPV6)) sendpkt.add_protocol(ipv6.ipv6(src=pkt_ipv6[0].src, dst=pkt_ipv6[0].dst, nxt=inet.IPPROTO_ICMPV6)) sendpkt.add_protocol(icmpv6.icmpv6(type_=icmpv6.ICMPV6_MEMBERSHIP_QUERY, data=icmpv6.mldv2_query(address='::'))) sendpkt.serialize() self.logger.debug("******** create packet :\n %s" % (sendpkt,)) print "\n" data = sendpkt.data #out = parser.OFPPacketOut(datapath=datapath, buffer_id=msg.buffer_id, in_port=in_port, actions=actions, data=data) out = parser.OFPPacketOut(datapath=datapath, buffer_id=0xffffffff, in_port=in_port, actions=actions, data=data) self.logger.debug("******** send :\n datapath : %s,\n in_port : %s,\n" \ % (str(datapath), str(in_port))) datapath.send_msg(out) """