Esempio n. 1
0
    def subnetentry(x):
        """
        Generate a subnet declaration block given an IPv4 prefix string
        for inclusion in the dhcpd3 config file.
        """
        addr = x.split("/")[0]
        if ipaddress.is_ipv6_address(addr):
            return ""
        else:
            addr = x.split("/")[0]
            net = Ipv4Prefix(x)
            # divide the address space in half
            rangelow = net.addr(net.num_addr() / 2)
            rangehigh = net.max_addr()
            return """
subnet %s netmask %s {
  pool {
    range %s %s;
    default-lease-time 600;
    option routers %s;
  }
}
""" % (
                net.prefix_str(),
                net.netmask_str(),
                rangelow,
                rangehigh,
                addr,
            )
Esempio n. 2
0
def get_address_type(address):
    addr, _slash, _prefixlen = address.partition("/")
    if ipaddress.is_ipv4_address(addr):
        address_type = "IPv4"
    elif ipaddress.is_ipv6_address(addr):
        address_type = "IPv6"
    else:
        raise NotImplementedError
    return address_type
Esempio n. 3
0
    def generateFrrConf(cls, node):
        """
        Returns configuration file text. Other services that depend on zebra
        will have generatefrrifcconfig() and generatefrrconfig()
        hooks that are invoked here.
        """
        # we could verify here that filename == frr.conf
        cfg = ""
        for ifc in node.netifs():
            cfg += "interface %s\n" % ifc.name
            # include control interfaces in addressing but not routing daemons
            if hasattr(ifc, "control") and ifc.control is True:
                cfg += "  "
                cfg += "\n  ".join(map(cls.addrstr, ifc.addrlist))
                cfg += "\n"
                continue
            cfgv4 = ""
            cfgv6 = ""
            want_ipv4 = False
            want_ipv6 = False
            for s in node.services:
                if cls.name not in s.dependencies:
                    continue
                ifccfg = s.generatefrrifcconfig(node, ifc)
                if s.ipv4_routing:
                    want_ipv4 = True
                if s.ipv6_routing:
                    want_ipv6 = True
                    cfgv6 += ifccfg
                else:
                    cfgv4 += ifccfg

            if want_ipv4:
                ipv4list = filter(
                    lambda x: ipaddress.is_ipv4_address(x.split("/")[0]),
                    ifc.addrlist)
                cfg += "  "
                cfg += "\n  ".join(map(cls.addrstr, ipv4list))
                cfg += "\n"
                cfg += cfgv4
            if want_ipv6:
                ipv6list = filter(
                    lambda x: ipaddress.is_ipv6_address(x.split("/")[0]),
                    ifc.addrlist)
                cfg += "  "
                cfg += "\n  ".join(map(cls.addrstr, ipv6list))
                cfg += "\n"
                cfg += cfgv6
            cfg += "!\n"

        for s in node.services:
            if cls.name not in s.dependencies:
                continue
            cfg += s.generatefrrconfig(node)
        return cfg
Esempio n. 4
0
 def subnetentry(x):
     """
     Generate a subnet declaration block given an IPv6 prefix string
     for inclusion in the RADVD config file.
     """
     addr = x.split("/")[0]
     if ipaddress.is_ipv6_address(addr):
         net = Ipv6Prefix(x)
         return str(net)
     else:
         return ""
Esempio n. 5
0
 def addrstr(x):
     """
     helper for mapping IP addresses to zebra config statements
     """
     addr = x.split("/")[0]
     if ipaddress.is_ipv4_address(addr):
         return "ip address %s" % x
     elif ipaddress.is_ipv6_address(addr):
         return "ipv6 address %s" % x
     else:
         raise ValueError("invalid address: %s", x)
Esempio n. 6
0
 def addrstr(x):
     addr = x.split("/")[0]
     if ipaddress.is_ipv6_address(addr):
         net = Ipv6Prefix(x)
     else:
         net = Ipv4Prefix(x)
     if net.max_addr() == net.min_addr():
         return ""
     else:
         if os.uname()[0] == "Linux":
             rtcmd = "ip route add default via"
         else:
             raise Exception("unknown platform")
         return "%s %s" % (rtcmd, net.min_addr())
Esempio n. 7
0
 def routestr(x):
     addr = x.split("/")[0]
     if ipaddress.is_ipv6_address(addr):
         net = Ipv6Prefix(x)
         dst = "3ffe:4::/64"
     else:
         net = Ipv4Prefix(x)
         dst = "10.9.8.0/24"
     if net.max_addr() == net.min_addr():
         return ""
     else:
         if os.uname()[0] == "Linux":
             rtcmd = "#/sbin/ip route add %s via" % dst
         else:
             raise Exception("unknown platform")
         return "%s %s" % (rtcmd, net.min_addr())
Esempio n. 8
0
File: sdn.py Progetto: tinchoa/core
    def generate_config(cls, node, filename):
        # Check whether the node is running zebra
        has_zebra = 0
        for s in node.services:
            if s.name == "zebra":
                has_zebra = 1

        cfg = "#!/bin/sh\n"
        cfg += "# auto-generated by OvsService (OvsService.py)\n"
        cfg += "/etc/init.d/openvswitch-switch start < /dev/null\n"
        cfg += "ovs-vsctl add-br ovsbr0 -- set Bridge ovsbr0 fail-mode=secure\n"
        cfg += "ifconfig ovsbr0 up\n"

        for ifc in node.netifs():
            if hasattr(ifc, "control") and ifc.control is True:
                continue
            ifnumstr = re.findall(r"\d+", ifc.name)
            ifnum = ifnumstr[0]

            # create virtual interfaces
            cfg += "ip link add rtr%s type veth peer name sw%s\n" % (ifnum, ifnum)
            cfg += "ifconfig rtr%s up\n" % ifnum
            cfg += "ifconfig sw%s up\n" % ifnum

            # remove ip address of eths because quagga/zebra will assign same IPs to rtr interfaces
            # or assign them manually to rtr interfaces if zebra is not running
            for ifcaddr in ifc.addrlist:
                addr = ifcaddr.split("/")[0]
                if ipaddress.is_ipv4_address(addr):
                    cfg += "ip addr del %s dev %s\n" % (ifcaddr, ifc.name)
                    if has_zebra == 0:
                        cfg += "ip addr add %s dev rtr%s\n" % (ifcaddr, ifnum)
                elif ipaddress.is_ipv6_address(addr):
                    cfg += "ip -6 addr del %s dev %s\n" % (ifcaddr, ifc.name)
                    if has_zebra == 0:
                        cfg += "ip -6 addr add %s dev rtr%s\n" % (ifcaddr, ifnum)
                else:
                    raise ValueError("invalid address: %s" % ifcaddr)

            # add interfaces to bridge
            cfg += "ovs-vsctl add-port ovsbr0 eth%s\n" % ifnum
            cfg += "ovs-vsctl add-port ovsbr0 sw%s\n" % ifnum

        # Add rule for default controller if there is one local (even if the controller is not local, it finds it)
        cfg += "ovs-vsctl set-controller ovsbr0 tcp:127.0.0.1:6633\n"

        # Setup default flows
        portnum = 1
        for ifc in node.netifs():
            if hasattr(ifc, "control") and ifc.control is True:
                continue
            cfg += (
                "ovs-ofctl add-flow ovsbr0 priority=1000,in_port=%d,action=output:%d\n"
                % (portnum, portnum + 1)
            )
            cfg += (
                "ovs-ofctl add-flow ovsbr0 priority=1000,in_port=%d,action=output:%d\n"
                % (portnum + 1, portnum)
            )
            portnum += 2

        return cfg