Exemple #1
0
    def send_arp_request(self, datapath, outport_no, dst_ip):
        """
            pack and send ARP request for specific IP address.
        """
        src_mac_addr = str(
            self.switches[datapath.id].ports[outport_no].hw_addr)
        src_ip = str(self.switches[datapath.id].ports[outport_no].gateway.ipv4)
        dst_ip = str(dst_ip)
        p = packet.Packet()
        e = ethernet.ethernet(dst=mac.BROADCAST_STR,
                              src=src_mac_addr,
                              ethertype=ether.ETH_TYPE_ARP)
        p.add_protocol(e)
        a = arp.arp_ip(opcode=arp.ARP_REQUEST,
                       src_mac=src_mac_addr,
                       src_ip=src_ip,
                       dst_mac=mac.DONTCARE_STR,
                       dst_ip=dst_ip)
        p.add_protocol(a)
        p.serialize()

        datapath.send_packet_out(
            in_port=ofproto_v1_0.OFPP_NONE,
            actions=[datapath.ofproto_parser.OFPActionOutput(outport_no)],
            data=p.data)

        logger.info('ARP request sent: who has %s? tell %s (dpid=%s)', dst_ip,
                    src_ip, dpid_lib.dpid_to_str(datapath.id))
Exemple #2
0
 def send_arp_request(self, datapath, outport_no, dst_ip):
     """
       pack and send ARP request for specific IP address.
     """
     logger.info("outport:%s",outport_no)
     src_mac_addr = str(self.routers[datapath.id].ports[outport_no].mac_addr)
     src_ip = str(self.routers[datapath.id].ports[outport_no].ip_addr)
     dst_ip = str(dst_ip)
     p = packet.Packet()
     e = ethernet.ethernet(dst = mac.BROADCAST_STR,src = src_mac_addr, ethertype = ether.ETH_TYPE_ARP)
     
     a = arp.arp_ip(opcode = arp.ARP_REQUEST, src_mac = src_mac_addr,
           src_ip = src_ip, dst_mac = mac.DONTCARE_STR,
           dst_ip = dst_ip)
     p.add_protocol(e)
     p.add_protocol(a)
     p.serialize()
     actions = [datapath.ofproto_parser.OFPActionOutput(outport_no)]
     out = datapath.ofproto_parser.OFPPacketOut(datapath=datapath,
                             buffer_id=datapath.ofproto.OFP_NO_BUFFER,
                             in_port=datapath.ofproto.OFPP_CONTROLLER,
                             actions=actions,
                             data=p.data)  
     datapath.send_msg(out)
     logger.info('ARP request sent: who has %s? tell %s (dpid=%s)', dst_ip, src_ip, dpid_lib.dpid_to_str(datapath.id))
Exemple #3
0
    def runTest(self):
        app = App()

        # This is from coscin/coscin_app_ryu.  Simulates what you get in a PacketIn
        src_mac = "00:00:de:ad:be:ef"
        dst_mac = "ff:ff:ff:ff:ff:ff"
        src_ip = "192.168.56.100"
        dst_ip = "192.168.57.100"
        e = ethernet.ethernet(dst=dst_mac,
                              src=src_mac,
                              ethertype=ether.ETH_TYPE_ARP)
        pkt = arp.arp_ip(arp.ARP_REQUEST, src_mac, src_ip, "00:00:00:00:00:00",
                         dst_ip)
        p = packet.Packet()
        p.add_protocol(e)
        p.add_protocol(pkt)
        p.serialize()
        payload = NotBuffered(binascii.a2b_base64(binascii.b2a_base64(p.data)))

        p_arp = app.packet(payload, "arp")
        self.assertEqual(p_arp.src_ip, src_ip)

        p_eth = app.packet(payload, "ethernet")
        self.assertEqual(p_eth.src, src_mac)

        p_icmp = app.packet(payload, "icmp")
        self.assertEqual(p_icmp, None)
Exemple #4
0
    def _arp(self, dp, port_out, dst_ip):
        LOG.debug('Forwarder ' + str(dp.id) + ' ARP searching APN with ' +
                  str(dst_ip) + ' IP at port ' + str(port_out))
        ofp = dp.ofproto
        parser = dp.ofproto_parser
        pkt = packet.Packet()
        str_hex_dpid = str(hex(dp.id)).rstrip('L').lstrip('0x')
        #Ak je hex dpid kratsie ako 11 chceme ho zacat '02:'a doplnit nuly
        #do celkovej dlzky 12 aby nam z toho nevysla multicastova adresa
        if len(str_hex_dpid) < 11:
            src_mac = '02'
            for i in range(10 - len(str_hex_dpid)):
                src_mac += '0'
            src_mac += str_hex_dpid
        else:
            src_mac = dp.id

        eth = ethernet.ethernet('ff:ff:ff:ff:ff:ff', '00:13:8f:d4:2c:30',
                                ether.ETH_TYPE_ARP)
        arp_req = arp.arp_ip(1, '00:13:8f:d4:2c:30', DISCOVERY_ARP_IP,
                             '00:00:00:00:00:00', dst_ip)

        pkt = packet.Packet()
        pkt.add_protocol(eth)
        pkt.add_protocol(arp_req)
        pkt.serialize()
        actions = [parser.OFPActionOutput(port_out)]
        out = parser.OFPPacketOut(datapath=dp,
                                  buffer_id=ofp.OFP_NO_BUFFER,
                                  in_port=ofp.OFPP_CONTROLLER,
                                  actions=actions,
                                  data=pkt.data)
        dp.send_msg(out)
    def _arp(self, dp, port_out, dst_ip):
        LOG.debug('Forwarder '+str(dp.id)+' ARP searching APN with '+str(dst_ip)+' IP at port '+str(port_out))
        ofp = dp.ofproto
        parser = dp.ofproto_parser
        pkt = packet.Packet()
        str_hex_dpid = str(hex(dp.id)).rstrip('L').lstrip('0x')
        #Ak je hex dpid kratsie ako 11 chceme ho zacat '02:'a doplnit nuly
        #do celkovej dlzky 12 aby nam z toho nevysla multicastova adresa
        if len(str_hex_dpid) < 11:
            src_mac ='02'
            for i in range(10-len(str_hex_dpid)):
                src_mac += '0'
            src_mac += str_hex_dpid
        else:
            src_mac = dp.id
       
        eth = ethernet.ethernet('ff:ff:ff:ff:ff:ff', '00:13:8f:d4:2c:30', ether.ETH_TYPE_ARP)
        arp_req = arp.arp_ip(1, '00:13:8f:d4:2c:30', DISCOVERY_ARP_IP, '00:00:00:00:00:00', dst_ip)

        pkt = packet.Packet()
        pkt.add_protocol(eth)
        pkt.add_protocol(arp_req)
        pkt.serialize()
        actions=[parser.OFPActionOutput(port_out)]
        out=parser.OFPPacketOut(datapath=dp, buffer_id=ofp.OFP_NO_BUFFER, in_port=ofp.OFPP_CONTROLLER, actions=actions, data=pkt.data)
        dp.send_msg(out)
Exemple #6
0
def receiveARP(hosts,datapath,pkt,eth_pkt,in_port):
	arpPKT=pkt.get_protocol(arp)
	if(arpPKT.opcode==ARP_REQUEST or arpPKT.opcode==ARP_REV_REQUEST):
		dst=arpPKT.src_ip
		dstMAC=hosts[dst].mac
		src=arpPKT.dst_ip
		srcMAC=hosts[src].mac
		opcode=0
		if(arpPKT.opcode==ARP_REQUEST):
			opcode=ARP_REPLY
		else:
			opcode=ARP_REV_REPLY
		e=ethernet.ethernet(dstMAC,srcMAC,ether.ETH_TYPE_ARP)
		a=arp_ip(opcode,srcMAC,src,dstMAC,dst)
		p=packet.Packet()
		p.add_protocol(e)
		p.add_protocol(a)
		p.serialize()
		actions=[datapath.ofproto_parser.OFPActionOutput(in_port)]
		out=datapath.ofproto_parser.OFPPacketOut(datapath=datapath,\
			buffer_id=datapath.ofproto.OFP_NO_BUFFER,\
			in_port=datapath.ofproto.OFPP_CONTROLLER,\
			actions=actions,data=p.data)
		return out
	else:return None
Exemple #7
0
    def __arp_packet_factory(self, packet_type="query", switch_ip="", dst_mac="", dst_ip="") -> packet.Packet:
        '''
        A factory for ARP packet.
        It customize ARP query/response for different service types
        '''
        assert packet_type in ("query", "response"), "Not a valid packet type (query/response)"
        assert dst_ip,    "Both query and response require valid destination IP address"
        assert switch_ip, "Must specify the switch IP"

        if packet_type == "response":
            assert dst_mac, "a response packet should have a valid destination MAC address"

        if packet_type == "query":
            arp_body = arp.arp_ip(
                _ARP_QUERY, 
                self._switch_mac_address, switch_ip,
                '00:00:00:00:00:00',      dst_ip
            )
            ethernet_head = ethernet.ethernet(
                src=self._switch_mac_address,
                dst="ff:ff:ff:ff:ff:ff",
                ethertype=ether.ETH_TYPE_ARP
            )

        else:
            arp_body = arp.arp_ip(
                _ARP_RESPONSE,
                self._switch_mac_address, switch_ip,
                dst_mac,                  dst_ip
            )
            ethernet_head = ethernet.ethernet(
                src=self._switch_mac_address,
                dst=dst_mac,
                ethertype=ether.ETH_TYPE_ARP
            )

        raw_packet = packet.Packet()
        raw_packet.add_protocol(ethernet_head)
        raw_packet.add_protocol(arp_body)
        raw_packet.serialize()

        return raw_packet
 def send_arp_request(self, switch, src_ip, target_ip):
   # It's unclear what the source should be, since the switch has no mac or IP address.
   # It just hears all replies and picks out the interesting stuff.
   src_mac = self.BOGUS_MAC
   dst_mac = "ff:ff:ff:ff:ff:ff"
   e = ethernet.ethernet(dst=dst_mac, src=src_mac, ethertype=ether.ETH_TYPE_ARP)
   pkt = arp.arp_ip(arp.ARP_REQUEST, src_mac, src_ip, "00:00:00:00:00:00", target_ip)
   payload = self.arp_payload(e, pkt)
   logging.info("Sending Arp Request to "+target_ip)
   # None means the request will go out every available port
   self.flood(switch, None, payload)
Exemple #9
0
    def create_arp(self, src_mac, src_ip, dst_mac, dst_ip):
        eth = ethernet.ethernet(dst_mac, src_mac, ether_types.ETH_TYPE_ARP)
        resp = arp.arp_ip(2, src_mac, src_ip , dst_mac, dst_ip)

        print "Built ARP response->SrcMAC:", resp.src_mac, " SrcIP:", resp.src_ip, " DstMAC:", resp.dst_mac, " DstIP:", resp.dst_ip

        p = packet.Packet()
        p.add_protocol(eth)
        p.add_protocol(resp)
        p.serialize()

        return p
Exemple #10
0
    def _garp_packet(self, ip_address):
        # prepare garp packet
        src_mac = vrrp.vrrp_ipv4_src_mac_address(self.config.vrid)
        e = ethernet.ethernet(mac_lib.BROADCAST_STR, src_mac,
                              ether.ETH_TYPE_ARP)
        a = arp.arp_ip(arp.ARP_REQUEST, src_mac, ip_address,
                       mac_lib.DONTCARE_STR, ip_address)

        p = packet.Packet()
        p.add_protocol(e)
        utils.may_add_vlan(p, self.interface.vlan_id)
        p.add_protocol(a)
        p.serialize()
        return p
Exemple #11
0
def arp_answer_packet(src_mac, src_ip, dst_mac, dst_ip):
    pkt = packet.Packet()
    eth = ethernet.ethernet(dst=dst_mac,
                            src=src_mac,
                            ethertype=ether_types.ETH_TYPE_ARP)
    arpp = arp.arp_ip(opcode=arp.ARP_REPLY,
                      src_mac=src_mac,
                      src_ip=src_ip,
                      dst_mac=dst_mac,
                      dst_ip=dst_ip)
    pkt.add_protocol(eth)
    pkt.add_protocol(arpp)
    pkt.serialize()
    return pkt.data
    def _garp_packet(self, ip_address):
        # prepare garp packet
        src_mac = vrrp.vrrp_ipv4_src_mac_address(self.config.vrid)
        e = ethernet.ethernet(mac_lib.BROADCAST_STR, src_mac,
                              ether.ETH_TYPE_ARP)
        a = arp.arp_ip(arp.ARP_REQUEST, src_mac, ip_address,
                       mac_lib.DONTCARE_STR, ip_address)

        p = packet.Packet()
        p.add_protocol(e)
        utils.may_add_vlan(p, self.interface.vlan_id)
        p.add_protocol(a)
        p.serialize()
        return p
Exemple #13
0
    def arp_packet(opcode, src_mac, src_ip, dst_mac, dst_ip):
        """
        Generate ARP packet with ethernet encapsulated.
        """
        # Generate ethernet header first.
        pkt = packet.Packet()
        eth_pkt = ethernet.ethernet(dst_mac, src_mac, ETH_TYPE_ARP)
        pkt.add_protocol(eth_pkt)

        # Use IPv4 ARP wrapper from packet library directly.
        arp_pkt = arp.arp_ip(opcode, src_mac, src_ip, dst_mac, dst_ip)
        pkt.add_protocol(arp_pkt)

        pkt.serialize()
        return pkt.data
Exemple #14
0
    def arp_packet(opcode, src_mac, src_ip, dst_mac, dst_ip):
        """
        Generate ARP packet with ethernet encapsulated.
        """
        # Generate ethernet header first.
        pkt = packet.Packet()
        eth_pkt = ethernet.ethernet(dst_mac, src_mac, ETH_TYPE_ARP)
        pkt.add_protocol(eth_pkt)

        # Use IPv4 ARP wrapper from packet library directly.
        arp_pkt = arp.arp_ip(opcode, src_mac, src_ip, dst_mac, dst_ip)
        pkt.add_protocol(arp_pkt)

        pkt.serialize()
        return pkt.data
    def _arp_reply_packet(self, arp_req_sha, arp_req_spa, arp_req_tpa):
        if not (arp_req_tpa in self.config.ip_addresses
                or arp_req_tpa == self.config.primary_ip_address):
            return None

        src_mac = vrrp.vrrp_ipv4_src_mac_address(self.config.vrid)
        e = ethernet.ethernet(arp_req_sha, src_mac, ether.ETH_TYPE_ARP)
        a = arp.arp_ip(arp.ARP_REPLY, src_mac, arp_req_tpa, arp_req_sha,
                       arp_req_spa)

        p = packet.Packet()
        p.add_protocol(e)
        utils.may_add_vlan(p, self.interface.vlan_id)
        p.add_protocol(a)
        p.serialize()
        self._transmit(p.data)
Exemple #16
0
  def send_arp_reply(dp, port, src_mac, src_ip, target_mac, target_ip):
    parser = dp.ofproto_parser
    ofproto = dp.ofproto

    e = ethernet.ethernet(dst=src_mac, src=target_mac, ethertype=ether.ETH_TYPE_ARP)
    # Note for the reply we flip the src and target, as per ARP rules
    pkt = arp.arp_ip(arp.ARP_REPLY, target_mac, target_ip, src_mac, src_ip)
    p = packet.Packet()
    p.add_protocol(e)
    p.add_protocol(pkt)
    p.serialize()
    logging.info("Sending Arp Reply for "+src_ip)          

    out = parser.OFPPacketOut(datapath=dp, buffer_id=ofproto.OFP_NO_BUFFER, 
      in_port=port, actions=[ parser.OFPActionOutput(port) ], data=p.data)
    dp.send_msg(out)
Exemple #17
0
    def _arp_reply_packet(self, arp_req_sha, arp_req_spa, arp_req_tpa):
        if not (arp_req_tpa in self.config.ip_addresses or
                arp_req_tpa == self.config.primary_ip_address):
            return None

        src_mac = vrrp.vrrp_ipv4_src_mac_address(self.config.vrid)
        e = ethernet.ethernet(arp_req_sha, src_mac, ether.ETH_TYPE_ARP)
        a = arp.arp_ip(arp.ARP_REPLY, src_mac, arp_req_tpa,
                       arp_req_sha, arp_req_spa)

        p = packet.Packet()
        p.add_protocol(e)
        utils.may_add_vlan(p, self.interface.vlan_id)
        p.add_protocol(a)
        p.serialize()
        self._transmit(p.data)
    def _send_arp_request(self, datapath, outport_no, dst_ip):
        src_mac_addr = \
            self.dpid_to_switch[datapath.id].ports[outport_no].hw_addr
        src_ip = \
            self.dpid_to_switch[datapath.id].ports[outport_no].gateway.gw_ip
        p = packet.Packet()
        e = ethernet.ethernet(dst = mac.BROADCAST,
            src = src_mac_addr, ethertype = ether.ETH_TYPE_ARP)
        p.add_protocol(e)
        a = arp.arp_ip(opcode = arp.ARP_REQUEST, src_mac = src_mac_addr,
                src_ip = src_ip, dst_mac = mac.DONTCARE,
                dst_ip = dst_ip)
        p.add_protocol(a)
        p.serialize()

        datapath.send_packet_out(in_port = ofproto_v1_0.OFPP_NONE,
            actions = [datapath.ofproto_parser.OFPActionOutput(outport_no)],
            data = p.data)
Exemple #19
0
def _arp_send(dp, port_out, arp_code,  ip_sender, ip_target, eth_dst='ff:ff:ff:ff:ff:ff',eth_src=None,eth_target='00:00:00:00:00:00'):
    """
    Generates arp request and sends it out of 'port_out' of 'dp' forwarder

    Keyword arguments:
        dp -- Datapath
        port_out -- Port on forwarder (dp) used to spit out packet
        arp_code -- ARP OP code(1==reques, 2==reply)
        ip_sender -- Senders IP addres
        eth_dst  -- Ethernet destination address (in case of request, broadcast address is used)
        eth_src -- Ethernet source address. If none provided, it's generated from Datapath ID of sending forwarder (dp)
        eth_target -- Final recipients Ethernet address(in case of request, zeroed out address is used)
        ip_target --  Final recipients IP address
    """

    ofp = dp.ofproto
    parser = dp.ofproto_parser
    pkt = packet.Packet()

    ##If no src_mac was provided we generate one from Datapath ID of forwarder that recieved message
    ##If Datapath ID starts with zeros we cannot use it as legit MAC address
    ##Second hex digit must be 2 to indicate localy administered non-multicast address 
    if eth_src == None:
        str_hex_dpid = str(hex(dp.id)).rstrip('L').lstrip('0x')
        if len(str_hex_dpid) < 11:
            eth_src ='02'
            for i in range(10-len(str_hex_dpid)):
                eth_src += '0'
            eth_src += str_hex_dpid
        else:
            eth_src = dp.id
    
    eth = ethernet.ethernet(eth_dst, eth_src, ether.ETH_TYPE_ARP)
    arp_req = arp.arp_ip(arp_code, eth_src, ip_sender, eth_target, ip_target)

    pkt = packet.Packet()
    pkt.add_protocol(eth)
    pkt.add_protocol(arp_req)
    pkt.serialize()
    actions=[parser.OFPActionOutput(port_out)]
    out=parser.OFPPacketOut(datapath=dp, buffer_id=ofp.OFP_NO_BUFFER, in_port=ofp.OFPP_CONTROLLER, actions=actions, data=pkt.data)
    dp.send_msg(out)
Exemple #20
0
  def send_arp_request(dp, src_ip, target_ip):
    parser = dp.ofproto_parser
    ofproto = dp.ofproto

    # It's unclear what the source should be, since the switch has no mac or IP address.
    # It just hears all replies and picks out the interesting stuff.
    src_mac = OpenflowUtils.BOGUS_MAC
    dst_mac = "ff:ff:ff:ff:ff:ff"
    e = ethernet.ethernet(dst=dst_mac, src=src_mac, ethertype=ether.ETH_TYPE_ARP)
    pkt = arp.arp_ip(arp.ARP_REQUEST, src_mac, src_ip, "00:00:00:00:00:00", target_ip)
    p = packet.Packet()
    p.add_protocol(e)
    p.add_protocol(pkt)
    p.serialize()
    logging.info("Sending Arp Request to "+target_ip)          

    out = parser.OFPPacketOut(datapath=dp, in_port=2, buffer_id = ofproto.OFP_NO_BUFFER, 
      actions=[ parser.OFPActionOutput(ofproto.OFPP_ALL) ], 
      data = p.data
    )
    dp.send_msg(out)
Exemple #21
0
    def _send_arp_request(self, datapath, outport_no, dst_ip):
        src_mac_addr = \
            self.dpid_to_switch[datapath.id].ports[outport_no].hw_addr
        src_ip = \
            self.dpid_to_switch[datapath.id].ports[outport_no].gateway.gw_ip
        p = packet.Packet()
        e = ethernet.ethernet(dst=mac.BROADCAST,
                              src=src_mac_addr,
                              ethertype=ether.ETH_TYPE_ARP)
        p.add_protocol(e)
        a = arp.arp_ip(opcode=arp.ARP_REQUEST,
                       src_mac=src_mac_addr,
                       src_ip=src_ip,
                       dst_mac=mac.DONTCARE,
                       dst_ip=dst_ip)
        p.add_protocol(a)
        p.serialize()

        datapath.send_packet_out(
            in_port=ofproto_v1_0.OFPP_NONE,
            actions=[datapath.ofproto_parser.OFPActionOutput(outport_no)],
            data=p.data)
Exemple #22
0
    def send_arp_request(self, datapath, outport_no, dst_ip):
        """
            pack and send ARP request for specific IP address.
        """
        src_mac_addr = str(self.switches[datapath.id].ports[outport_no].hw_addr)
        src_ip = str(self.switches[datapath.id].ports[outport_no].gateway.ipv4)
        dst_ip = str(dst_ip)
        p = packet.Packet()
        e = ethernet.ethernet(dst = mac.BROADCAST_STR,
            src = src_mac_addr, ethertype = ether.ETH_TYPE_ARP)
        p.add_protocol(e)
        a = arp.arp_ip(opcode = arp.ARP_REQUEST, src_mac = src_mac_addr,
                src_ip = src_ip, dst_mac = mac.DONTCARE_STR,
                dst_ip = dst_ip)
        p.add_protocol(a)
        p.serialize()

        datapath.send_packet_out(in_port = ofproto_v1_0.OFPP_NONE,
            actions = [datapath.ofproto_parser.OFPActionOutput(outport_no)],
            data = p.data)

        logger.info('ARP request sent: who has %s? tell %s (dpid=%s)', dst_ip, src_ip, dpid_lib.dpid_to_str(datapath.id))
Exemple #23
0
    def runTest(self):
        app = App()

        # This is from coscin/coscin_app_ryu.  Simulates what you get in a PacketIn
        src_mac = "00:00:de:ad:be:ef"
        dst_mac = "ff:ff:ff:ff:ff:ff"
        src_ip = "192.168.56.100"
        dst_ip = "192.168.57.100"
        e = ethernet.ethernet(dst=dst_mac, src=src_mac, ethertype=ether.ETH_TYPE_ARP)
        pkt = arp.arp_ip(arp.ARP_REQUEST, src_mac, src_ip, "00:00:00:00:00:00", dst_ip)
        p = packet.Packet()
        p.add_protocol(e)
        p.add_protocol(pkt)
        p.serialize()
        payload = NotBuffered(binascii.a2b_base64(binascii.b2a_base64(p.data)))

        p_arp = app.packet(payload, "arp")
        self.assertEqual(p_arp.src_ip, src_ip)

        p_eth = app.packet(payload, "ethernet")
        self.assertEqual(p_eth.src, src_mac)

        p_icmp = app.packet(payload, "icmp")
        self.assertEqual(p_icmp, None)
Exemple #24
0
    def _packet_in(self, ev):
        dp = ev.msg.datapath
        ofp = dp.ofproto
        parser = dp.ofproto_parser
        match = ev.msg.match

        if (match['eth_type'] == 0x0800
                and match['ip_proto'] == inet.IPPROTO_UDP
                and match['udp_dst'] == VGSN_PORT
                and match['sndcp_first_segment'] == 1
                and match['sndcp_more_segments'] == 1):
            self._ping(dp,
                       match['in_port'],
                       SNDCP_FRAG_WARNING_SRC_IP,
                       match['ipv4_src'],
                       match['eth_dst'],
                       match['eth_src'],
                       icmp_type=3,
                       icmp_code=4)
            LOG.warning('WARNING: Device with IP: ' + match['ipv4_src'] +
                        ' sent fragmented sndcp packet')

        if match['eth_type'] == 0x0806 and match['arp_op'] == 1:
            LOG.debug("prisiel nam ARP request... ")
            str_hex_dpid = str(hex(dp.id)).rstrip('L').lstrip('0x')
            #Ak je hex dpid kratsie ako 11 chceme ho zacat '02:'a doplnit nuly
            #do celkovej dlzky 12 aby nam z toho nevysla multicastova adresa
            if len(str_hex_dpid) < 11:
                reply_mac = '02'
                for i in range(10 - len(str_hex_dpid)):
                    reply_mac += '0'
                reply_mac += str_hex_dpid
            else:
                reply_mac = dp.id

            LOG.debug('Odpovedam s src_mac: ' + reply_mac)
            eth = ethernet.ethernet(match['arp_sha'], reply_mac,
                                    ether.ETH_TYPE_ARP)
            arp_reply = arp.arp_ip(2, reply_mac, match['arp_tpa'],
                                   match['arp_sha'], match['arp_spa'])
            LOG.debug("  arp_reply=" + pprint.pformat(arp_reply))

            pkt = packet.Packet()
            pkt.add_protocol(eth)
            pkt.add_protocol(arp_reply)
            pkt.serialize()
            actions = [parser.OFPActionOutput(match['in_port'])]
            out = parser.OFPPacketOut(datapath=dp,
                                      buffer_id=ofp.OFP_NO_BUFFER,
                                      in_port=ofp.OFPP_CONTROLLER,
                                      actions=actions,
                                      data=pkt.data)
            dp.send_msg(out)

        if match['eth_type'] == 0x0806 and match['arp_op'] == 2 and match[
                'arp_tpa'] == DISCOVERY_ARP_IP:
            pkt = packet.Packet(array.array('B', ev.msg.data))
            arp_data = ''
            for p in pkt:
                if p.protocol_name == 'arp':
                    arp_data = p
            apn_ip = arp_data.src_ip
            apn_name = ''
            dp = ev.msg.datapath.id
            port = match['in_port']

            for apn in APN_POOL.keys():
                if APN_POOL.get(apn) == apn_ip:
                    apn_name = apn
                    break

            topo.add_link(dp, str(apn_name), port)
            topo.add_link(str(apn_name), dp, 0)
            topo.reload_topology()
            LOG.debug('APN ' + str(apn_name) + ' found at forwarder ' +
                      str(dp) + ' at port ' + str(port))

        if match['eth_type'] == 0x0800 and match[
                'ipv4_dst'] == DISCOVERY_IP_DST and match['ip_proto'] == 1:
            neighbourDPID = self._get_icmp_data(
                packet.Packet(array.array('B', ev.msg.data)), 'dpid')
            neighbourPort = self._get_icmp_data(
                packet.Packet(array.array('B', ev.msg.data)), 'port_out')
            #print('Som',ev.msg.datapath.id,
            #      ', dostal som spravu na porte: ',ev.msg.match['in_port'],
            #      ' od cisla:',neighbourDPID,
            #      ' ktory ma ma pripojeneho na porte',neighbourPort)
            topo.add_link(ev.msg.datapath.id, neighbourDPID,
                          ev.msg.match['in_port'])
            topo.add_link(neighbourDPID, ev.msg.datapath.id, neighbourPort)
            topo.reload_topology()
            LOG.debug('Topology changed: link between ' +
                      str(ev.msg.datapath.id) + ' and ' + str(neighbourDPID) +
                      ' is up.')
    def _packet_in(self, ev):
        dp = ev.msg.datapath
        ofp = dp.ofproto
        parser = dp.ofproto_parser
        match = ev.msg.match

        if (match['eth_type'] == 0x0800 and match['ip_proto'] == inet.IPPROTO_UDP 
            and match['udp_dst'] == VGSN_PORT and match['sndcp_first_segment'] == 1 
            and match['sndcp_more_segments'] == 1):
            self._ping(dp,match['in_port'],SNDCP_FRAG_WARNING_SRC_IP,match['ipv4_src'],match['eth_dst'],match['eth_src'],icmp_type=3,icmp_code=4)
            LOG.warning('WARNING: Device with IP: '+match['ipv4_src']+' sent fragmented sndcp packet')

        if match['eth_type'] == 0x0806 and match['arp_op'] == 1:
            LOG.debug("prisiel nam ARP request... ")
            str_hex_dpid = str(hex(dp.id)).rstrip('L').lstrip('0x')
            #Ak je hex dpid kratsie ako 11 chceme ho zacat '02:'a doplnit nuly 
            #do celkovej dlzky 12 aby nam z toho nevysla multicastova adresa
            if len(str_hex_dpid) < 11:
                reply_mac ='02'
                for i in range(10-len(str_hex_dpid)):
                    reply_mac += '0'
                reply_mac += str_hex_dpid
            else:
                reply_mac = dp.id

            LOG.debug('Odpovedam s src_mac: '+reply_mac)
            eth = ethernet.ethernet(match['arp_sha'], reply_mac, ether.ETH_TYPE_ARP)
            arp_reply = arp.arp_ip(2, reply_mac, match['arp_tpa'], match['arp_sha'], match['arp_spa'])
            LOG.debug("  arp_reply="+pprint.pformat(arp_reply))

            pkt = packet.Packet()
            pkt.add_protocol(eth)
            pkt.add_protocol(arp_reply)
            pkt.serialize()
            actions=[parser.OFPActionOutput(match['in_port'])]
            out=parser.OFPPacketOut(datapath=dp, buffer_id=ofp.OFP_NO_BUFFER, in_port=ofp.OFPP_CONTROLLER, actions=actions, data=pkt.data)
            dp.send_msg(out)

        if match['eth_type'] == 0x0806 and match['arp_op'] == 2 and match['arp_tpa'] == DISCOVERY_ARP_IP:
            pkt = packet.Packet(array.array('B', ev.msg.data))
            arp_data=''
            for p in pkt:
                if p.protocol_name == 'arp':
                    arp_data = p        
            apn_ip = arp_data.src_ip
            apn_name = ''
            dp = ev.msg.datapath.id
            port = match['in_port']

            for apn in APN_POOL.keys():
                if APN_POOL.get(apn) == apn_ip:
                    apn_name = apn
                    break

            topo.add_link(dp,str(apn_name),port)
            topo.add_link(str(apn_name),dp,0)
            topo.reload_topology()
            LOG.debug('APN '+str(apn_name)+' found at forwarder '+str(dp)+' at port '+str(port))
        
        if match['eth_type'] == 0x0800 and match['ipv4_dst'] == DISCOVERY_IP_DST and match['ip_proto'] == 1:
            neighbourDPID=self._get_icmp_data(packet.Packet(array.array('B', ev.msg.data)),'dpid')
            neighbourPort=self._get_icmp_data(packet.Packet(array.array('B', ev.msg.data)),'port_out')
            #print('Som',ev.msg.datapath.id,
            #      ', dostal som spravu na porte: ',ev.msg.match['in_port'], 
            #      ' od cisla:',neighbourDPID,
            #      ' ktory ma ma pripojeneho na porte',neighbourPort)
            topo.add_link(ev.msg.datapath.id, neighbourDPID, ev.msg.match['in_port'])
            topo.add_link(neighbourDPID, ev.msg.datapath.id, neighbourPort )
            topo.reload_topology()
            LOG.debug('Topology changed: link between '+str(ev.msg.datapath.id)+' and '+str(neighbourDPID)+' is up.')
Exemple #26
0
 def arp_reply(self, switch, port, src_mac, src_ip, target_mac, target_ip):
   e = ethernet.ethernet(dst=src_mac, src=target_mac, ethertype=ether.ETH_TYPE_ARP)
   # Note for the reply we flip the src and target, as per ARP rules
   pkt = arp.arp_ip(arp.ARP_REPLY, target_mac, target_ip, src_mac, src_ip)
   payload = self.main_app.arp_payload(e, pkt)
   self.main_app.pkt_out(self.nib.switch_to_dpid(switch), payload, [Output(Physical(port))])