Ejemplo n.º 1
0
Archivo: nat.py Proyecto: yapkke/yapc
 def __route_check(self, o):
     """Check route
     
     @param o route check object (dictionary)
     """
     gw = self.get_if_route(mif=o["mif"])
     if gw == None:
         o["tried"] += 1
         if o["tried"] < MAX_RETRY:
             rc = yapc.priv_callback(self, o)
             self.server.post_event(rc, 1)
     else:
         no = self.switch.if_name2dpid_port_mac(o["if"])[1]
         mc.set(nat.get_gw_key(no), gw)
         output.info("Gateway of " + o["if"] + " is " + gw, self.__class__.__name__)
         # Check for route
         self.check_default_route()
         # Register ip range
         ipv4addr = self.ifmgr.ipv4_addr_n_mask(o["mif"])
         if ipv4addr == None:
             return
         ipr = (
             pu.ip_string2val(ipv4addr["addr"]),
             pu.ip_string2val(ipv4addr["netmask"]),
             pu.hex_str2array(self.ifmgr.ethernet_addr(o["mif"])),
         )
         mc.set(nat.get_ip_range_key(no), ipr)
         output.info(
             o["if"] + "(" + str(no) + ") has IP address %x and netmask %x" % (ipr[0], ipr[1]),
             self.__class__.__name__,
         )
         # Call for ARP
         rc = yapc.priv_callback(self, {"type": "arp", "tried": 0, "ip": gw, "if": o["mif"]})
         self.server.post_event(rc, 0)
Ejemplo n.º 2
0
Archivo: nat.py Proyecto: yapkke/yapc
    def setup(self, interfaces, inner_addr=LOCAL_IP, gw=LOCAL_GW, gw_mac=None):
        """Add interfaces
        
        @param interfaces list of interfaces
        @param inner_addr IP to give COIN's client side interface
        @param gw gateway to use for COIN's interface
        @param gw_mac gateway mac address
        """
        # Set up interfaces
        self.loif = self.add_loif("local")
        self.add_interfaces(interfaces)

        # Get IP addresses on the interfaces
        self.ifmgr.set_ipv4_addr(self.loif.client_intf, inner_addr)
        mc.set(
            nat.SW_INNER_PORT_ADDR,
            (pu.ip_string2val(inner_addr), pu.hex_str2array(self.ifmgr.ethernet_addr(self.loif.client_intf))),
        )
        for i in range(0, len(interfaces)):
            self.ifmgr.up(interfaces[i])

        # Setup route
        self.ifmgr.add_route("default", gw=gw, iface=self.loif.client_intf)
        if gw_mac == None:
            gw_mac = self.ifmgr.ethernet_addr(self.loif.switch_intf)
        self.ifmgr.set_ip_mac(gw, gw_mac)

        self.check_default_route()
Ejemplo n.º 3
0
Archivo: nat.py Proyecto: yapkke/yapc
    def _process_inbound(self, pktin, intfs, iport, lointf):
        """Event handler for inbound packet

        @param pktin packet in event to handle
        @param intfs dictionary of interfaces (with ip range)
        @param iport port no of local interface
        @param lointf local interface address (ip, mac)
        @return false
        """
        try:
            ipr = intfs[pktin.match.in_port]
        except KeyError:
            output.vdbg("IP packet received on unknown/uninitialized interface", self.__class__.__name__)
            return False

        if ipr[2] != pktin.match.dl_dst:
            return False

        flow = flows.exact_entry(pktin.match)
        flow.add_nw_rewrite(False, lointf[0])
        flow.add_dl_rewrite(False, lointf[1])
        flow.add_output(iport)
        if pktin.pktin.buffer_id != flow.buffer_id:
            flow.set_buffer(pktin.pktin.buffer_id)
            self.get_conn().send(flow.get_flow_mod(pyof.OFPFC_ADD, cookie=self.cookie).pack())
            self.cookie += 1
        else:
            self.get_conn().send(flow.get_flow_mod(pyof.OFPFC_ADD, cookie=self.cookie).pack())
            self.cookie += 1
            ofpkt.nw_rewrite(pktin.dpkt, False, lointf[0])
            ofpkt.dl_rewrite(pktin.dpkt, False, lointf[1])
            self.get_conn().send(flow.get_packet_out(pyof.OFPFC_ADD).pack() + pktin.dpkt.pack())

        gw = mc.get(nat.get_gw_key(flow.match.in_port))
        gwmac = mc.get(nat.get_gw_mac_key(gw))
        if gwmac == None:
            return False

        if (ipr[0] & ipr[1]) != (pktin.match.nw_src & ipr[1]):
            # Global address
            rflow = flow.reverse(iport)
            rflow.match.nw_src = lointf[0]
            rflow.match.dl_src = lointf[1]
            rflow.add_nw_rewrite(True, ipr[0])
            rflow.add_dl_rewrite(True, ipr[2])
            rflow.add_dl_rewrite(False, pu.hex_str2array(gwmac))
            rflow.add_output(flow.match.in_port)
            if (rflow.match.wildcards & pyof.OFPFW_DL_TYPE) != 0:
                output.dbg(rflow.match.show(), self.__class__.__name__)
            self.get_conn().send(rflow.get_flow_mod(pyof.OFPFC_ADD, cookie=self.cookie).pack())
            self.cookie += 1

        return False
Ejemplo n.º 4
0
Archivo: nat.py Proyecto: yapkke/yapc
 def set_ifconfig(self, msg):
     """Does manual ifconfig
     """
     intf = msg["name"]
     ip = msg["ipaddr"]
     mask = msg["netmask"]
     gw = msg["gwaddr"]
     gwmac = msg["gwmac"]
     no = self.switch.if_name2dpid_port_mac(intf)[1]
     # Register gateway
     mc.set(nat.get_gw_key(no), gw)
     output.info("Gateway of " + intf + " is " + gw, self.__class__.__name__)
     # Register ip range
     ipr = (pu.ip_string2val(ip), pu.ip_string2val(mask), pu.hex_str2array(self.ifmgr.ethernet_addr(intf)))
     mc.set(nat.get_ip_range_key(no), ipr)
     output.info(
         intf + "(" + str(no) + ") has IP address %x and netmask %x" % (ipr[0], ipr[1]), self.__class__.__name__
     )
     # Regsiter mac address
     mc.set(nat.get_gw_mac_key(gw), gwmac)
     output.info("ARP of " + gw + " is " + str(gwmac), self.__class__.__name__)
Ejemplo n.º 5
0
Archivo: nat.py Proyecto: yapkke/yapc
    def _process_outbound(self, pktin, intfs, iport, lointf):
        """Event handler for outbound IP packet

        @param pktin packet in event to handle
        @param intfs dictionary of interfaces (with ip range)
        @param iport port no of local interface
        @param lointf local interface address (ip, mac)
        @return false if processed else true
        """
        flow = flows.exact_entry(pktin.match)

        for portno, ipr in intfs.items():
            if (ipr[0] & ipr[1]) == (pktin.match.nw_dst & ipr[1]):
                # Local address
                flow.set_buffer(pktin.pktin.buffer_id)
                flow.add_nw_rewrite(True, ipr[0])
                flow.add_dl_rewrite(True, ipr[2])
                flow.add_output(portno)
                if flow.buffer_id != pktin.pktin.buffer_id:
                    flow.set_buffer(pktin.pktin.buffer_id)
                    self.get_conn().send(flow.get_flow_mod(pyof.OFPFC_ADD).pack())
                else:
                    self.get_conn().send(flow.get_flow_mod(pyof.OFPFC_ADD).pack())
                    ofpkt.nw_rewrite(pktin.dpkt, True, ipr[0])
                    ofpkt.dl_rewrite(pktin.dpkt, True, ipr[2])
                    self.get_conn().send(flow.get_packet_out(pyof.OFPFC_ADD).pack() + pktin.dpkt.pack())
                return False

        # Global address
        cportl = []
        if (
            (pktin.match.nw_proto == 17)
            and (pktin.match.tp_dst == 53)
            and (self.coin.config["dns_select_interface"] != None)
        ):
            output.dbg("DNS packet", self.__class__.__name__)
            tcport = self.dns_select_intf(intfs)
            if tcport != None:
                cportl.append(tcport)
        else:
            output.dbg("non-DNS packet", self.__class__.__name__)
            tcport = self.select_intf(intfs)
            if tcport != None:
                cportl.append(tcport)

        for cport in cportl:
            gw = mc.get(nat.get_gw_key(cport))
            gwmac = mc.get(nat.get_gw_mac_key(gw))
            ipr = intfs[cport]
            if (gw == None) or (gwmac == None):
                output.warn("Packet ignored since gateway for interface not found!", self.__class__.__name__)
                return False

            # Outbound
            flow.add_nw_rewrite(True, ipr[0])
            flow.add_dl_rewrite(True, ipr[2])
            flow.add_dl_rewrite(False, pu.hex_str2array(gwmac))
            flow.add_output(cport)
            if pktin.pktin.buffer_id != flow.buffer_id:
                flow.set_buffer(pktin.pktin.buffer_id)
                self.get_conn().send(flow.get_flow_mod(pyof.OFPFC_ADD, cookie=self.cookie).pack())
                self.cookie += 1
            else:
                self.get_conn().send(flow.get_flow_mod(pyof.OFPFC_ADD, cookie=self.cookie).pack())
                self.cookie += 1
                ofpkt.nw_rewrite(pktin.dpkt, True, ipr[0])
                ofpkt.dl_rewrite(pktin.dpkt, True, ipr[2])
                ofpkt.dl_rewrite(pktin.dpkt, False, pu.hex_str2array(gwmac))
                self.get_conn().send(flow.get_packet_out(pyof.OFPFC_ADD).pack() + pktin.dpkt.pack())

            # Inbound
            rflow = flow.reverse(cport)
            rflow.match.nw_dst = ipr[0]
            rflow.match.dl_dst = ipr[2]
            rflow.add_nw_rewrite(False, lointf[0])
            rflow.add_dl_rewrite(False, lointf[1])
            rflow.add_output(iport)
            self.get_conn().send(rflow.get_flow_mod(pyof.OFPFC_ADD, cookie=self.cookie).pack())
            self.cookie += 1

            return False

        return True