2020-05-14 17:11:47 -04:00
|
|
|
import ipaddress
|
2020-05-18 05:24:43 -04:00
|
|
|
import logging
|
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
|
|
|
|
|
|
|
|
2020-05-18 05:24:43 -04:00
|
|
|
# TODO: more logging
|
|
|
|
logger = logging.getLogger()
|
|
|
|
|
|
|
|
|
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
|
2020-05-18 06:31:05 -04:00
|
|
|
self.ra_dhcp = ra_dhcp
|
|
|
|
for a in ('dns', 'tag', 'other', 'dhcp'):
|
|
|
|
k = 'ra_{0}'.format(a)
|
|
|
|
v = getattr(self, k)
|
|
|
|
logger.debug('{0}: {1}'.format(k, v))
|
2020-05-18 04:59:00 -04:00
|
|
|
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() != ''])
|
2020-05-18 06:31:05 -04:00
|
|
|
logger.debug('ra_domains: {0}'.format(', '.join(self.ra_domains)))
|
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()
|
2020-05-18 05:24:43 -04:00
|
|
|
logger.debug('Looking for iface {0}'.format(self.iface))
|
2020-05-14 17:11:47 -04:00
|
|
|
self.iface_idx = ipr.link_lookup(ifname = self.iface)[0]
|
2020-05-18 05:24:43 -04:00
|
|
|
logger.debug('Found iface {0} at idx {1}'.format(self.iface, self.iface_idx))
|
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']
|
2020-05-18 05:33:57 -04:00
|
|
|
logger.debug('Link-Local address for {0}: {1}'.format(self.iface, addrs))
|
2020-05-18 04:59:00 -04:00
|
|
|
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
|
|
|
|
self.iface_blocks = self.alloc_block.extract_subnet(self.prefix, count = 1)
|
2020-05-18 06:32:35 -04:00
|
|
|
logger.debug('Allocation blocks for {0}: {1}'.format(self.iface, ','.join([str(i) for i in self.iface_blocks])))
|
2020-05-14 17:11:47 -04:00
|
|
|
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-18 05:48:40 -04:00
|
|
|
self.ra = 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
|
2020-05-18 06:21:48 -04:00
|
|
|
tag = None
|
|
|
|
_ra_xml = _assign_xml.find('ra')
|
|
|
|
if _ra_xml is not None and self.ra_provider:
|
|
|
|
tag = _ra_xml.attrib.get('tag', None)
|
2020-05-18 06:47:33 -04:00
|
|
|
_dns_xml = _ra_xml.find('dns')
|
|
|
|
_dhcp_xml = _ra_xml.find('dhcpv6')
|
2020-05-18 06:21:48 -04:00
|
|
|
if _dns_xml is not None:
|
|
|
|
do_dns = utils.xml2bool(_dns_xml.text.strip())
|
|
|
|
domains = [i.strip() for i in _dns_xml.attrib.get('domains', '').split() if i.strip() != '']
|
|
|
|
if _dhcp_xml is not None:
|
|
|
|
do_dhcp = utils.xml2bool(_dhcp_xml.text.strip())
|
|
|
|
ra_other = utils.xml2bool(_dhcp_xml.attrib.get('advOther', 'false').strip())
|
2020-05-18 04:59:00 -04:00
|
|
|
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)
|