routerbox/utils/he_ipv6/tunnel.py
2020-05-18 06:32:35 -04:00

239 lines
7.5 KiB
Python

import ipaddress
import logging
import socket
##
import netaddr
from pyroute2 import IPRoute
##
from . import ra
from . import utils
# TODO: more logging
logger = logging.getLogger()
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):
def __init__(self,
assign_xml,
ra_dns = False,
ra_dhcp = False,
ra_other = False,
ra_tag = None,
ra_domains = None):
self.xml = assign_xml
self.ra_dns = ra_dns
self.ra_tag = ra_tag
self.ra_other = ra_other
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))
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() != ''])
logger.debug('ra_domains: {0}'.format(', '.join(self.ra_domains)))
self.iface = None
self.iface_ll = None
self.iface_idx = None
self.iface_blocks = []
self.dhcp6_ranges = []
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()
logger.debug('Looking for iface {0}'.format(self.iface))
self.iface_idx = ipr.link_lookup(ifname = self.iface)[0]
logger.debug('Found iface {0} at idx {1}'.format(self.iface, self.iface_idx))
# Link-Local address
ll = ipr.get_addr(index = self.iface_idx,
family = socket.AF_INET6,
scope = 253)[0]['attrs']
addrs = dict(ll)['IFA_ADDRESS']
logger.debug('Link-Local address for {0}: {1}'.format(self.iface, addrs))
if isinstance(addrs, (list, tuple)):
addr = addrs[0]
else:
addr = addrs
self.iface_ll = addr
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)
logger.debug('Allocation blocks for {0}: {1}'.format(self.iface, ','.join([str(i) for i in self.iface_blocks])))
for i in self.iface_blocks:
# 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))
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):
def __init__(self, tun_xml, he_tunnel, creds):
self.xml = tun_xml
self.creds = creds
self.he = he_tunnel
self.update_key = self.he.creds.password
self.id = None
self.client = None
self.server = None
self.endpoint = None
self.ra = None
self.ra_provider = None
self.allocations = {} # This is a dict of {}[alloc.id] = Allocation obj (as provided by HE)
self.assignments = [] # This is a list of Assignment objs
self.parse()
def _allocations(self):
self.allocations = self.he.allocations
return(None)
def _assignments(self):
_assigns_xml = self.xml.find('assignments')
self.ra_provider = _assigns_xml.attrib.get('raProvider')
for _assign_xml in _assigns_xml.findall('assign'):
do_dns = False
domains = []
do_dhcp = False
ra_other = False
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)
_dns_xml = _assign_xml.find('dns')
_dhcp_xml = _assign_xml.find('dhcpv6')
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())
assign = Assignment(_assign_xml,
ra_dns = do_dns,
ra_dhcp = do_dhcp,
ra_other = ra_other,
ra_tag = tag,
ra_domains = domains)
assign.alloc = self.allocations[assign.alloc_id]
assign.parse_alloc()
self.assignments.append(assign)
return(None)
def _client(self):
self.client = self.he.client
return(None)
def _creds(self):
self.creds_id = self.xml.attrib['creds'].strip()
return(None)
def _endpoint(self):
self.endpoint = self.he.endpoint
return(None)
def _id(self):
self.id = int(self.xml.attrib['id'].strip())
return(None)
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)
return(None)
def _server(self):
self.server = self.he.server
return(None)
def parse(self):
self._id()
self._creds()
self._client()
self._server()
self._endpoint()
self._allocations()
self._assignments()
self._ra()
return(None)