diff --git a/utils/he_ipv6/tunnelbroker.py b/utils/he_ipv6/tunnelbroker.py index 4e80231..8a556d6 100644 --- a/utils/he_ipv6/tunnelbroker.py +++ b/utils/he_ipv6/tunnelbroker.py @@ -19,13 +19,15 @@ class TunnelBroker(object): logger.debug('Using config path: {0}'.format(self.conf_file)) self._conf = config.Config(self.conf_file) if tun_id: - self.cfg = self._conf.tunnels[int(tun_id)] + self.tun = self._conf.tunnels[int(tun_id)] else: tun_id = list(self._conf.tunnels.keys())[0] - self.cfg = self._conf.tunnels[tun_id] + self.tun = self._conf.tunnels[tun_id] + self.iface_name = 'he-{0}'.format(self.tun.id) self.wan = wan_ip - self.update = update + self.force_update = update self.my_ip = None + self.iface_idx = None def _get_my_ip(self): if self.wan: @@ -44,5 +46,81 @@ class TunnelBroker(object): logger.error('Could not determine default route. Does this machine have a single default route?') raise RuntimeError('Could not determine default IPv4 route') self.my_ip = config.IP4(_defrt[0]['attrs']['RTA_PREFSRC'], 32) + ipr.close() logger.debug('Set my_ip to {0}.'.format(self.my_ip.str)) return(None) + + def start(self): + if self.force_update: + logger.debug('IP update forced; updating.') + self._get_my_ip() + self.update() + ipr = IPRoute() + try: + ipr.link('add', + ifname = self.iface_name, + kind = 'sit', + sit_local = self.my_ip.str, + sit_remote = self.tun.server.str, + sit_ttl = 255) + logger.debug('Added link {0} successfully.'.format(self.iface_name)) + except Exception as e: + logger.error('Could not create link for link {0} ' + '(maybe it already exists?): {1}'.format(self.iface_name, e)) + ipr.close() + raise e + try: + self.iface_idx = ipr.link_lookup(ifname = self.iface_name)[0] + logger.debug('Found link {0} at index {1}.'.format(self.iface_name, self.iface_idx)) + except Exception as e: + logger.error('Could not set iface_idx for iface name {0}: {1}'.format(self.iface_name, e)) + ipr.close() + raise e + try: + ipr.addr('add', + index = self.iface_idx, + address = self.tun.client.str, + mask = self.tun.client.prefix, + family = socket.AF_INET6) + logger.debug('Added address {0} to link {1} with prefix {2}.'.format(self.tun.client.str, + self.iface_name, + self.tun.client.prefix)) + except Exception as e: + logger.error(('Could not add address {0} on link {1}: ' + '{2}').format(self.tun.client.str, self.iface_name, e)) + ipr.close() + raise e + try: + ipr.link('set', index = self.iface_idx, state = 'up', mtu = 1480) + logger.debug('Set link {0} status to UP.'.format(self.iface_name)) + except Exception as e: + logger.error(('Could not bring up link for iface name {0} at index {1}: ' + '{2}').format(self.iface_name, self.iface_idx, e)) + ipr.close() + raise e + for alloc in self.tun.allocations: + try: + ipr.addr('add', + index = alloc.iface_idx, + address = alloc.ip.str, + mask = alloc.ip.prefix, + family = socket.AF_INET6) + except Exception as e: + logger.error(('Could not add address {0} on link {1}: ' + '{2}').format(str(alloc.ip.str), alloc.iface_idx, e)) + ipr.close() + raise e + try: + # ipr.route('add', dst = 'default', oif = self.iface_idx, family = socket.AF_INET6) + ipr.route('add', dst = '::192.88.99.1', oif = self.iface_idx, family = socket.AF_INET6) + logger.debug('Added default route for link {0}.'.format(self.iface_name)) + except Exception as e: + logger.error(('Could not add default IPv6 route on link {0}: {1}').format(self.iface_name, e)) + ipr.close() + raise e + ipr.close() + return(None) + + def stop(self): + + def update(self):