def valve_packetout(out_port, data): return parser.OFPPacketOut( datapath=None, buffer_id=ofp.OFP_NO_BUFFER, in_port=ofp.OFPP_CONTROLLER, actions=[parser.OFPActionOutput(out_port, 0)], data=data)
def send_pkt(self, datapath, data, port=ofproto.OFPP_FLOOD): actions = [parser.OFPActionOutput(port)] out = parser.OFPPacketOut(datapath=datapath, actions=actions, in_port=datapath.ofproto.OFPP_CONTROLLER, data=data, buffer_id=ofproto.OFP_NO_BUFFER) datapath.send_msg(out)
def send_pkt(self, dp, data, port=ofproto.OFPP_FLOOD): """ Convenience method that instructs a switch to forward a packet from the controller. """ out = parser.OFPPacketOut(datapath=dp, actions=[parser.OFPActionOutput(port)], in_port=dp.ofproto.OFPP_CONTROLLER, data=data, buffer_id=ofproto.OFP_NO_BUFFER) dp.send_msg(out)
def _send_packet(self, datapath, port, pkt): ofproto = datapath.ofproto parser = datapath.ofproto_parser pkt.serialize() self.logger.info("packet-out %s" % (pkt, )) data = pkt.data actions = [parser.OFPActionOutput(port=port)] out = parser.OFPPacketOut(datapath=datapath, buffer_id=ofproto.OFP_NO_BUFFER, in_port=ofproto.OFPP_CONTROLLER, actions=actions, data=data) datapath.send_msg(out)
def packetout(port_num, data): """Return OpenFlow action to packet out to dataplane from controller. Args: port_num (int): port to output to. data (str): raw packet to output. Returns: ryu.ofproto.ofproto_v1_3_parser.OFPActionOutput: packet out action. """ return parser.OFPPacketOut(datapath=None, buffer_id=ofp.OFP_NO_BUFFER, in_port=ofp.OFPP_CONTROLLER, actions=[output_port(port_num)], data=data)
def send_packet_out(self, switch, out_port=of13.OFPP_FLOOD, data=None, buffer_id=of13.OFP_NO_BUFFER, actions=[], in_port=0): msg = parser13.OFPPacketOut(datapath=switch, data=data, buffer_id=buffer_id, actions=actions, in_port=in_port) switch.send_msg(msg)
def packetouts(port_nums, data): """Return OpenFlow action to multiply packet out to dataplane from controller. Args: port_num (list): ints, ports to output to. data (str): raw packet to output. Returns: ryu.ofproto.ofproto_v1_3_parser.OFPActionOutput: packet out action. """ random.shuffle(port_nums) return parser.OFPPacketOut( datapath=None, buffer_id=ofp.OFP_NO_BUFFER, in_port=ofp.OFPP_CONTROLLER, actions=[output_port(port_num) for port_num in port_nums], data=data)
def packet_in_handler(self, event): if event.msg.match['in_port'] != FAKEPORT: return pkt = packet.Packet(event.msg.data) eth_protocol = pkt.get_protocol(ethernet.ethernet) vlan_protocol = pkt.get_protocol(vlan.vlan) ipv6_protocol = pkt.get_protocol(ipv6.ipv6) icmpv6_protocol = pkt.get_protocol(icmpv6.icmpv6) if not (eth_protocol and vlan_protocol and ipv6_protocol and icmpv6_protocol): return if icmpv6_protocol.type_ != icmpv6.ND_NEIGHBOR_SOLICIT: return if int(ipaddress.ip_address(ipv6_protocol.src)) == 0: return src_ip = ipaddress.ip_address(icmpv6_protocol.data.dst) if src_ip.is_reserved: return eth_dst = eth_protocol.src dst_ip = ipv6_protocol.src eth_src = FAKECLIENTMAC vid = vlan_protocol.vid reply = packet.Packet() for protocol in (ethernet.ethernet(eth_dst, eth_src, ether.ETH_TYPE_8021Q), vlan.vlan(vid=vid, ethertype=ether.ETH_TYPE_IPV6), ipv6.ipv6(src=src_ip, dst=dst_ip, nxt=socket.IPPROTO_ICMPV6, hop_limit=255), icmpv6.icmpv6( type_=icmpv6.ND_NEIGHBOR_ADVERT, data=icmpv6.nd_neighbor( dst=src_ip, option=icmpv6.nd_option_tla(hw_src=eth_src), res=7))): reply.add_protocol(protocol) reply.serialize() out = parser.OFPPacketOut(datapath=event.msg.datapath, buffer_id=ofp.OFP_NO_BUFFER, in_port=ofp.OFPP_CONTROLLER, actions=[parser.OFPActionOutput(FAKEPORT)], data=reply.data) self.send_mods(event.msg.datapath, [out])
def _send_packet_out(self, obj): packet_out = obj.get("OFPPacketOut") datapath = self.dpstore.get(packet_out.get("datapath_id")) datapath = datapath.get("dp_obj") actions = self._parse_action(packet_out.get('actions')) buffer_id = packet_out.get("buffer_id") in_port = int(packet_out.get("in_port")) data = None #if buffer_id == ofproto_v1_3.OFP_NO_BUFFER: data = packet_out.get("data").decode("hex") out = ofproto_v1_3_parser.OFPPacketOut( datapath=datapath, buffer_id=ofproto_v1_3.OFP_NO_BUFFER, in_port=in_port, actions=actions, data=data) datapath.send_msg(out)
def packet_in(self, event): msg = event.msg path = msg.datapath inpt = msg.match['in_port'] pckt = packet.Packet(msg.data) eth = pckt.get_protocols(ethernet.ethernet)[0] arpp = pckt.get_protocols(arp.arp)[0] acts = [parser.OFPActionOutput(inpt)] mtch = parser.OFPMatch(in_port=inpt, ipv4_dst=arpp.src_ip, ip_proto=0, eth_type=ether_types.ETH_TYPE_IP) self.send_flow(path, 1, mtch, acts) dest = eth.dst sorc = eth.src dpid = path.id self.mac_tab.setdefault(dpid, {}) self.mac_tab[dpid][sorc] = inpt outp = self.get_port(dest, dpid) acts = [parser.OFPActionOutput(outp)] if outp != proto.OFPP_FLOOD: mtch = parser.OFPMatch(in_port=outp, eth_dst=dest) self.send_flow(path, 1, mtch, acts) data = None if msg.buffer_id == proto.OFP_NO_BUFFER: data = msg.data out = parser.OFPPacketOut(datapath=path, buffer_id=msg.buffer_id, in_port=inpt, actions=acts, data=data) path.send_msg(out)
def packet_in_handler(self, ev): msg_in = ev.msg pkt = packet.Packet(msg_in.data) eth = pkt.get_protocols(ethernet.ethernet)[0] datapath = msg_in.datapath ofp = datapath.ofproto ofp_parser = datapath.ofproto_parser dpid = datapath.id self.mac_to_port.setdefault(dpid, {}) in_port = msg_in.match['in_port'] self.mac_to_port[dpid][eth.src] = in_port msg_out = ofp_parser.OFPPacketOut( datapath=datapath, buffer_id=msg_in.buffer_id, in_port=in_port, data=msg_in.data, ) if eth.dst in self.mac_to_port[dpid]: msg_out.actions = [ ofp_parser.OFPActionOutput(self.mac_to_port[dpid][eth.dst]), ] match = ofp_parser.OFPMatch(in_port=in_port, eth_dst=eth.dst, eth_src=eth.src) self.add_flow(datapath, msg_in.buffer_id, 3, 1, match, msg_out.actions) if msg_in.buffer_id == ofp.OFP_NO_BUFFER: datapath.send_msg(msg_out)
def foo(self): #buffer_id = 4294967295 #s = socket.socket() # Create a socket object s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) #host = socket.gethostname() # Get local machine name port = 5001 # Reserve a port for your service. s.bind(('', port)) # Bind to the port s.listen(1000) # Now wait for client connection. while True: c, addr = s.accept() # Establish connection with client. print 'Got connection from', addr #rdata =json.loads(c.recv(2048)) rdata = pickle.loads(c.recv(1024)) if not rdata: break dpid = rdata[0] src = rdata[1] dst = rdata[2] in_port = rdata[3] #MacAddress ip_src = rdata[4] buffer_id = rdata[5] msg_data = rdata[6] datapath = self.Data_Path[dpid] pkt = packet.Packet(msg_data) pkt_tcp = pkt.get_protocol(tcp.tcp) if len(rdata) < 8: self.mac_to_port[dpid][src] = in_port self.Hostinfo.append((dpid, src, ip_src, in_port)) #pass # to Install an entry elif rdata[7] == True: self.Diffie_Hellman(src) self.newlocation.append((dpid, src, ip_src, in_port)) time.sleep(1) else: print rdata[7] print "BLOCK %s %s %s %s" % (dpid, src, dst, in_port) actions = [] match = parser.OFPMatch(in_port=in_port) self.add_flow(datapath, 5, match, actions, table_id=1) return Private_key = 0 if src in self.Key.keys(): if self.Key[src][1] != 0: self.Key[src][ 3] = self.Key[src][1]**self.Key[src][2] % 21841 Keyfile = open('Key.txt', 'w') Keyfile.write(str(self.Key)) Keyfile.close 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: if Private_key: match = parser.OFPMatch(in_port=in_port, eth_dst=dst, eth_src=src, eth_type=0x8847, mpls_label=Private_key) else: match = parser.OFPMatch(in_port=in_port, eth_dst=dst, eth_src=src) # verify if we have a valid buffer_id, if yes avoid to send both # flow_mod & packet_out if buffer_id != ofproto.OFP_NO_BUFFER: self.add_flow(datapath, 1, match, actions, buffer_id) return else: self.add_flow(datapath, 1, match, actions) data = None if buffer_id == ofproto.OFP_NO_BUFFER: data = msg_data out = parser.OFPPacketOut(datapath=datapath, buffer_id=buffer_id, in_port=in_port, actions=actions, data=data) datapath.send_msg(out)
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] pkt_tcp = pkt.get_protocol(tcp.tcp) ip = pkt.get_protocol(ipv4.ipv4) if eth.ethertype == ether_types.ETH_TYPE_LLDP: # ignore lldp packet return dst = eth.dst src = eth.src dpid = datapath.id self.mac_to_port.setdefault(dpid, {}) # learn a mac address to avoid FLOOD next time. Private_key = 0 table_id = 1 #l = [257,260] last_item = 4 # we have to assign the correct user ports 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)] priority = 1 if ip and dpid in self.Edgeswitch and in_port in range( 2, last_item + 1): if (dpid, src, ip.src, in_port) in self.Hostinfo: if src in self.Key.keys(): file = open("/home/abdullah/ryu/Key.txt", "r") contents = file.read() dictionary = ast.literal_eval(contents) file.close() self.Key = dictionary if self.Key[src][1] != 0: # I have to waite the user answer t = self.Key[src] lst = list(t) lst[3] = lst[1]**lst[2] % 21841 #private key t = tuple(lst) self.Key[src] = t Private_key = lst[3] else: return print ip.src, "is an authenticated user and its location", dpid, in_port else: return if msg.table_id == 0 and pkt_tcp.bits == 2: if Private_key != 0: match = parser.OFPMatch(in_port=in_port, eth_src=src, eth_type=0x8847, mpls_label=Private_key) actions = [ parser.OFPActionPopMpls(), parser.OFPActionOutput(out_port) ] inst = [ parser.OFPInstructionActions( ofproto.OFPIT_APPLY_ACTIONS, actions) ] mod = parser.OFPFlowMod(datapath=datapath, table_id=0, priority=10, match=match, instructions=inst) datapath.send_msg(mod) self.Flowcounter[dpid] += 1 actions = [] match = parser.OFPMatch(in_port=in_port, eth_type=0x0800, ip_proto=6, tcp_flags=2) self.add_flow(datapath, 5, match, actions, table_id=0) return else: match = parser.OFPMatch(in_port=in_port, eth_type=0x0800, ip_proto=6, tcp_flags=2) actions = [parser.OFPActionOutput(out_port)] inst = [ parser.OFPInstructionActions( ofproto.OFPIT_APPLY_ACTIONS, actions) ] mod = parser.OFPFlowMod(datapath=datapath, table_id=0, priority=10, match=match, instructions=inst) datapath.send_msg(mod) self.Flowcounter[dpid] += 1 print pkt return elif dpid in self.Edgeswitch: self.logger.info("packet in %s %s %s %s", dpid, src, dst, in_port) table_id = 1 else: self.logger.info("packet in %s %s %s %s", dpid, src, dst, in_port) table_id = 0 # install a flow to avoid packet_in next time if out_port != ofproto.OFPP_FLOOD: if Private_key != 0 and dpid in self.Edgeswitch: match = parser.OFPMatch(in_port=in_port, eth_src=src) actions = [] self.add_flow(datapath, 5, match, actions, table_id=1) match = parser.OFPMatch(in_port=in_port, eth_dst=dst, eth_src=src, eth_type=0x8847, mpls_label=Private_key) priority = 10 table_id = 1 actions = [ parser.OFPActionPopMpls(), parser.OFPActionOutput(out_port) ] else: match = parser.OFPMatch(in_port=in_port, eth_dst=dst, eth_src=src) # verify if we have a valid buffer_id, if yes avoid to send both # flow_mod & pa enumerate(cket_out if msg.buffer_id != ofproto.OFP_NO_BUFFER: self.add_flow(datapath, priority, match, actions, msg.buffer_id, table_id=table_id) return else: self.add_flow(datapath, priority, match, actions, table_id=table_id) else: pass 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)