2020-05-14 17:11:47 -04:00
|
|
|
import ipaddress
|
2020-05-18 04:59:00 -04:00
|
|
|
import socket
|
2020-05-14 17:11:47 -04:00
|
|
|
##
|
|
|
|
import netaddr
|
|
|
|
from pyroute2 import IPRoute
|
|
|
|
##
|
2020-05-15 18:01:03 -04:00
|
|
|
from . import ra
|
2020-05-18 04:59:00 -04:00
|
|
|
from . import utils
|
2020-05-14 17:11:47 -04:00
|
|
|
|
|
|
|
|
|
|
|
class IP(object):
|
|
|
|
type = None
|
|
|
|
version = None
|
|
|
|
_ip = ipaddress.ip_address
|
|
|
|
_net = ipaddress.ip_network
|
|
|
|
_net_ip = netaddr.IPAddress
|
|
|
|
_net_net = netaddr.IPNetwork
|
|
|
|
|
|
|
|
def __init__(self, ip, prefix, *args, **kwargs):
|
|
|
|
self.str = ip
|
|
|
|
self.prefix = int(prefix)
|
|
|
|
self.net_ip = self._net_ip(self.str)
|
|
|
|
self.net_net = self._net_net('{0}/{1}'.format(self.str, self.prefix))
|
|
|
|
|
|
|
|
def _ext_init(self):
|
|
|
|
self.ip = self._ip(self.str)
|
|
|
|
self.net = self._net('{0}/{1}'.format(self.str, self.prefix), strict = False)
|
|
|
|
return(None)
|
|
|
|
|
|
|
|
|
|
|
|
class IP4(IP):
|
|
|
|
type = 'IPv4'
|
|
|
|
version = 4
|
|
|
|
_ip = ipaddress.IPv4Address
|
|
|
|
_net = ipaddress.IPv4Network
|
|
|
|
|
|
|
|
def __init__(self, ip, prefix, *args, **kwargs):
|
|
|
|
super().__init__(ip, prefix, *args, **kwargs)
|
|
|
|
self._ext_init()
|
|
|
|
|
|
|
|
|
|
|
|
class IP6(IP):
|
|
|
|
type = 'IPv6'
|
|
|
|
version = 6
|
|
|
|
_ip = ipaddress.IPv6Address
|
|
|
|
_net = ipaddress.IPv6Network
|
|
|
|
|
|
|
|
def __init__(self, ip, prefix, *args, **kwargs):
|
|
|
|
super().__init__(ip, prefix, *args, **kwargs)
|
|
|
|
self._ext_init()
|
|
|
|
self.alloc_block = netaddr.SubnetSplitter(self.net_net)
|
|
|
|
|
|
|
|
|
|
|
|
class Assignment(object):
|
2020-05-18 04:59:00 -04:00
|
|
|
def __init__(self,
|
|
|
|
assign_xml,
|
|
|
|
ra_dns = False,
|
|
|
|
ra_dhcp = False,
|
|
|
|
ra_other = False,
|
|
|
|
ra_tag = None,
|
|
|
|
ra_domains = None):
|
2020-05-14 17:11:47 -04:00
|
|
|
self.xml = assign_xml
|
2020-05-18 04:59:00 -04:00
|
|
|
self.ra_dns = ra_dns
|
|
|
|
self.ra_tag = ra_tag
|
|
|
|
self.ra_other = ra_other
|
|
|
|
self.ra_domains = set()
|
|
|
|
if isinstance(ra_domains, list):
|
|
|
|
self.ra_domains.update(ra_domains)
|
|
|
|
elif isinstance(ra_domains, str):
|
|
|
|
self.ra_domains.update([i.lower().strip() for i in ra_domains if i.strip() != ''])
|
|
|
|
self.ra_dhcp = ra_dhcp
|
2020-05-14 17:11:47 -04:00
|
|
|
self.iface = None
|
2020-05-18 04:59:00 -04:00
|
|
|
self.iface_ll = None
|
2020-05-14 17:11:47 -04:00
|
|
|
self.iface_idx = None
|
|
|
|
self.iface_blocks = []
|
2020-05-18 04:59:00 -04:00
|
|
|
self.dhcp6_ranges = []
|
2020-05-14 17:11:47 -04:00
|
|
|
self.alloc = None # This must be set externally to a mapped Allocation instance
|
|
|
|
self.alloc_id = None
|
|
|
|
self.prefix = None
|
|
|
|
self.alloc_block = None
|
|
|
|
self.parse()
|
|
|
|
|
|
|
|
def _alloc(self):
|
|
|
|
self.alloc_id = int(self.xml.attrib['alloc'].strip())
|
|
|
|
return(None)
|
|
|
|
|
|
|
|
def _iface(self):
|
|
|
|
_iface_txt = self.xml.attrib['iface'].strip()
|
|
|
|
self.iface = _iface_txt.strip()
|
|
|
|
ipr = IPRoute()
|
|
|
|
self.iface_idx = ipr.link_lookup(ifname = self.iface)[0]
|
2020-05-18 04:59:00 -04:00
|
|
|
# Link-Local address
|
|
|
|
ll = ipr.get_addr(index = self.iface_idx,
|
|
|
|
family = socket.AF_INET6,
|
|
|
|
scope = 253)[0]['attrs']
|
|
|
|
addrs = dict(ll)['IFA_ADDRESS']
|
|
|
|
if isinstance(addrs, (list, tuple)):
|
|
|
|
addr = addrs[0]
|
|
|
|
else:
|
|
|
|
addr = addrs
|
|
|
|
self.iface_ll = addr
|
2020-05-14 17:11:47 -04:00
|
|
|
ipr.close()
|
|
|
|
return(None)
|
|
|
|
|
|
|
|
def _prefix(self):
|
|
|
|
self.prefix = int(self.xml.attrib.get('prefix', 64).strip())
|
|
|
|
return(None)
|
|
|
|
|
|
|
|
def parse(self):
|
|
|
|
self._iface()
|
|
|
|
self._alloc()
|
|
|
|
self._prefix()
|
|
|
|
return(None)
|
|
|
|
|
|
|
|
def parse_alloc(self):
|
|
|
|
self.alloc_block = self.alloc.ip.alloc_block
|
2020-05-15 03:14:41 -04:00
|
|
|
# NOT AN IP6 OBJECT!
|
2020-05-14 17:11:47 -04:00
|
|
|
self.iface_blocks = self.alloc_block.extract_subnet(self.prefix, count = 1)
|
|
|
|
for i in self.iface_blocks:
|
2020-05-18 04:59:00 -04:00
|
|
|
# DHCPv6 range.
|
|
|
|
_base = str(i.ip).rstrip(':')
|
|
|
|
start = '{0}:dead:beef:cafe:0'.format(_base)
|
|
|
|
stop = '{0}:dead:beef:cafe:ffff'.format(_base)
|
|
|
|
self.dhcp6_ranges.append((start, stop))
|
2020-05-14 17:11:47 -04:00
|
|
|
return(None)
|
|
|
|
|
|
|
|
|
|
|
|
class Allocation(object):
|
|
|
|
def __init__(self, alloc_net):
|
|
|
|
_ip, _prefix = alloc_net.split('/')
|
|
|
|
self.id = int(_prefix.strip())
|
|
|
|
self.prefix = self.id
|
|
|
|
self.ip = IP6(_ip.strip(), self.prefix)
|
|
|
|
|
|
|
|
|
|
|
|
class Tunnel(object):
|
2020-05-14 23:46:03 -04:00
|
|
|
def __init__(self, tun_xml, he_tunnel, creds):
|
2020-05-14 17:11:47 -04:00
|
|
|
self.xml = tun_xml
|
2020-05-14 23:46:03 -04:00
|
|
|
self.creds = creds
|
|
|
|
self.he = he_tunnel
|
|
|
|
self.update_key = self.he.creds.password
|
2020-05-14 17:11:47 -04:00
|
|
|
self.id = None
|
|
|
|
self.client = None
|
|
|
|
self.server = None
|
2020-05-15 01:45:52 -04:00
|
|
|
self.endpoint = None
|
2020-05-15 18:01:03 -04:00
|
|
|
self.ra_provider = None
|
2020-05-18 04:59:00 -04:00
|
|
|
self.allocations = {} # This is a dict of {}[alloc.id] = Allocation obj (as provided by HE)
|
2020-05-14 17:11:47 -04:00
|
|
|
self.assignments = [] # This is a list of Assignment objs
|
|
|
|
self.parse()
|
|
|
|
|
|
|
|
def _allocations(self):
|
2020-05-14 23:46:03 -04:00
|
|
|
self.allocations = self.he.allocations
|
2020-05-14 17:11:47 -04:00
|
|
|
return(None)
|
|
|
|
|
|
|
|
def _assignments(self):
|
|
|
|
_assigns_xml = self.xml.find('assignments')
|
2020-05-18 04:59:00 -04:00
|
|
|
self.ra_provider = _assigns_xml.attrib.get('raProvider')
|
2020-05-14 17:11:47 -04:00
|
|
|
for _assign_xml in _assigns_xml.findall('assign'):
|
2020-05-18 04:59:00 -04:00
|
|
|
do_dns = False
|
|
|
|
domains = []
|
|
|
|
do_dhcp = False
|
|
|
|
ra_other = False
|
|
|
|
tag = _assign_xml.attrib.get('tag', None)
|
|
|
|
dns = _assign_xml.find('dns')
|
|
|
|
if dns and self.ra_provider:
|
|
|
|
do_dns = utils.xml2bool(dns.text.strip())
|
|
|
|
domains = [i.strip() for i in dns.attrib.get('domains', '').split() if i.strip() != '']
|
|
|
|
dhcp = _assign_xml.find('dhcpv6')
|
|
|
|
if dhcp and self.ra_provider:
|
|
|
|
do_dhcp = utils.xml2bool(dhcp.text.strip())
|
|
|
|
ra_other = utils.xml2bool(dhcp.attrib.get('advOther', 'false').strip())
|
|
|
|
assign = Assignment(_assign_xml,
|
|
|
|
ra_dns = do_dns,
|
|
|
|
ra_dhcp = do_dhcp,
|
|
|
|
ra_other = ra_other,
|
|
|
|
ra_tag = tag,
|
|
|
|
ra_domains = domains)
|
2020-05-14 17:11:47 -04:00
|
|
|
assign.alloc = self.allocations[assign.alloc_id]
|
|
|
|
assign.parse_alloc()
|
|
|
|
self.assignments.append(assign)
|
|
|
|
return(None)
|
|
|
|
|
|
|
|
def _client(self):
|
2020-05-15 01:45:52 -04:00
|
|
|
self.client = self.he.client
|
2020-05-14 17:11:47 -04:00
|
|
|
return(None)
|
|
|
|
|
|
|
|
def _creds(self):
|
|
|
|
self.creds_id = self.xml.attrib['creds'].strip()
|
|
|
|
return(None)
|
|
|
|
|
2020-05-15 01:45:52 -04:00
|
|
|
def _endpoint(self):
|
|
|
|
self.endpoint = self.he.endpoint
|
|
|
|
return(None)
|
|
|
|
|
2020-05-14 17:11:47 -04:00
|
|
|
def _id(self):
|
|
|
|
self.id = int(self.xml.attrib['id'].strip())
|
|
|
|
return(None)
|
|
|
|
|
2020-05-18 04:59:00 -04:00
|
|
|
def _ra(self):
|
|
|
|
# TODO: support conf path override via config XML?
|
|
|
|
if self.ra_provider.strip().lower() == 'dnsmasq':
|
|
|
|
self.ra = ra.DNSMasq()
|
|
|
|
elif self.ra_provider.strip().lower() == 'radvd':
|
|
|
|
self.ra = ra.RADVD()
|
|
|
|
self.ra.conf.generate(self.assignments)
|
2020-05-14 17:11:47 -04:00
|
|
|
return(None)
|
|
|
|
|
|
|
|
def _server(self):
|
2020-05-15 01:45:52 -04:00
|
|
|
self.server = self.he.server
|
2020-05-14 17:11:47 -04:00
|
|
|
return(None)
|
|
|
|
|
|
|
|
def parse(self):
|
|
|
|
self._id()
|
|
|
|
self._creds()
|
|
|
|
self._client()
|
|
|
|
self._server()
|
2020-05-15 02:48:55 -04:00
|
|
|
self._endpoint()
|
2020-05-14 17:11:47 -04:00
|
|
|
self._allocations()
|
|
|
|
self._assignments()
|
2020-05-18 04:59:00 -04:00
|
|
|
self._ra()
|
2020-05-14 17:11:47 -04:00
|
|
|
return(None)
|