Commit 251ae411 authored by Yohann D'Anello's avatar Yohann D'Anello

Rework on routing configuration

Signed-off-by: Yohann D'Anello's avatarYohann D'ANELLO <ynerant@crans.org>
parent 266ec5b4
......@@ -366,6 +366,10 @@ def main():
ip('addr', my_ip + '/%s' % len(subnet),
'dev', config.main_interface)
ip('rule', 'from', 'all', 'lookup', '34072')
ip('route', 'table', '34072', 'local', my_ip, 'dev', config.main_interface)
subprocess.check_call(('ip', '-6', 'route', 'del', 'table', 'local', my_ip))
ip('rule', 'from', my_subnet, 'to', my_subnet, 'iif', config.main_interface, 'lookup', '34071')
if_rt = ['ip', '-6', 'route', 'del',
'fe80::/64', 'dev', config.main_interface]
if config.main_interface == 'lo':
......
......@@ -70,7 +70,6 @@ class Connection(object):
self.address_list = address_list
self.iface = iface
self._prefix = prefix
self._monitoring_address = None
def __iter__(self):
if not hasattr(self, '_remote_ip_set'):
......@@ -100,22 +99,6 @@ class Connection(object):
self._retry += 1
def connected(self, serial):
# Generate random IP address in the prefix
# FIXME Run DAD
# TODO Check if there are enough IP addresses. If not, we have to find a rule.
prefix = self.tunnel_manager.ctl.network + self.tunnel_manager._prefix
suffix = bin(random.randint(2, 2 ** (128 - len(prefix))))[2:]
self._monitoring_address = utils.ipFromBin(prefix, suffix)
# Add IP address on Re6stnet interface
subprocess.check_call(('ip', '-6', 'address', 'add', self._monitoring_address, 'dev', self.iface))
subprocess.check_call(('ip', '-6', 'route', 'del', self._monitoring_address))
# Add IP rule to indicate to search main route first instead of local route
# FIXME Get main interface instead of hardcoding 'lo'
subprocess.check_call(('ip', '-6', 'rule', 'add', 'from', utils.ipFromBin(prefix, '1'),
'to', self._monitoring_address, 'iif', 'lo', 'priority', '0'))
subprocess.check_call(('ip', '-6', 'rule', 'del', 'from', 'all', 'lookup', 'local', 'priority', '0'))
subprocess.check_call(('ip', '-6', 'rule', 'add', 'from', 'all', 'lookup', 'local', 'priority', '1'))
cache = self.tunnel_manager.cache
if serial in cache.crl:
self.tunnel_manager._kill(self._prefix)
......@@ -129,11 +112,6 @@ class Connection(object):
cache.connecting(self._prefix, 0)
def close(self):
if self._monitoring_address:
# Remove address and reset IP rule
subprocess.check_call(('ip', '-6', 'address', 'del', self._monitoring_address, 'dev', self.iface))
subprocess.check_call(('ip', '-6', 'rule', 'del', 'to', self._monitoring_address, 'priority', '0'))
try:
self.process.stop()
except AttributeError:
......@@ -228,6 +206,7 @@ class BaseTunnelManager(object):
self.cache = cache
self._connecting = set()
self._connection_dict = {}
self._neighbour_monitoring_addresses = {}
self._served = defaultdict(dict)
self._version = cache.version
self._conf_country = conf_country
......@@ -692,25 +671,54 @@ class BaseTunnelManager(object):
"""
Refresh routes that are used for link monitoring.
"""
my_address = utils.ipFromBin(self._network + self._prefix, '1')
# Cleanup old routes
for connection in self._connection_dict.values():
if connection._monitoring_address:
subprocess.check_call(('ip', '-6', 'route', 'del', connection._monitoring_address,
'dev', connection.iface))
for prefix in list(self._neighbour_monitoring_addresses.keys()):
if prefix not in self.ctl.neighbours:
address = self._neighbour_monitoring_addresses[prefix]
# FIXME Replace lo by main inteface name
subprocess.check_call(('ip', '-6', 'address', 'del', address, 'dev', 'lo'))
subprocess.check_call(('ip', '-6', 'route', 'del', my_address,
'from', address, 'dev', 'lo', 'table', '34071'))
# Babel is not initialized yet.
if not hasattr(self.ctl, 'neighbours'):
return
# Get nexthop for each prefix, and draw a route for monitoring addresses
for prefix in self.ctl.neighbours.keys():
if prefix is None or prefix not in self._connection_dict:
if prefix is None:
continue
neighbour = self.ctl.neighbours[prefix][0]
nexthop = neighbour.address
nexthop = utils.ipFromBin("".join(bin(ord(c))[2:].zfill(8) for c in nexthop))
connection = self._connection_dict[prefix]
address = connection._monitoring_address
# Find interface name from interface id
ifindex = neighbour.ifindex
output = subprocess.check_output(('ip', 'link'))
for line in output.split('\n'):
if line.startswith(str(ifindex) + ':'):
iface = line.split(' ')[1][:-1]
break
else:
logging.error("Unknown interface index: " + str(ifindex))
continue
# Assign new IP address to this link is not existing
if prefix not in self._neighbour_monitoring_addresses:
p = self.ctl.network + self._prefix
s = bin(random.randint(2, 2 ** (128 - len(p))))[2:]
self._neighbour_monitoring_addresses[prefix] = utils.ipFromBin(p, s)
address = self._neighbour_monitoring_addresses[prefix]
# Add route in kernel
subprocess.check_call(('ip', '-6', 'route', 'add', address, 'via', nexthop, 'dev', connection.iface))
# FIXME Replace lo by main inteface name
subprocess.check_call(('ip', '-6', 'address', 'add', address, 'dev', 'lo'))
subprocess.check_call(('ip', '-6', 'route', 'del', address, 'dev', 'lo', 'table', 'main'))
subprocess.check_call(('ip', '-6', 'route', 'add', my_address, 'from', address,
'via', nexthop, 'dev', iface, 'src', address, 'table', '34071'))
def _updateCountry(self, address):
def update():
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment