Commit d7d7b425 authored by Julien Muchembled's avatar Julien Muchembled

Do not fail on unexpected 'route_up' notifications from OpenVPN clients

This fixes the following error:

  TypeError: unsupported operand type(s) for -: 'NoneType' and 'int'
  Traceback (most recent call last):
    File "/usr/sbin/re6stnet", line 438, in main
      tunnel_manager.handleTunnelEvent(read_pipe.readline())
    File "/usr/lib/python2.7/dist-packages/re6st/tunnel.py", line 389, in handleTunnelEvent
      m(*args)
    File "/usr/lib/python2.7/dist-packages/re6st/tunnel.py", line 412, in _ovpn_route_up
      self._connection_dict[prefix].connected()
    File "/usr/lib/python2.7/dist-packages/re6st/tunnel.py", line 76, in connected
      i = self._retry - 1

What happened is probably that a route_up notification was received just before
killing/recreating the connection for the same node, and then process twice
the same OpenVPN notification: in this case, the first was for a previous
connection and should have been ignored.
parent 2c3d66bb
#!/usr/bin/python -S #!/usr/bin/python -S
import os, sys import os, sys
if os.environ['script_type'] == 'up': script_type = os.environ['script_type']
if script_type == 'up':
# OpenVPN unsets PATH before calling hooks # OpenVPN unsets PATH before calling hooks
# which is equivalent to set /bin:/usr/bin # which is equivalent to set /bin:/usr/bin
os.environ['PATH'] = '/bin:/sbin:/usr/bin:/usr/sbin' os.environ['PATH'] = '/bin:/sbin:/usr/bin:/usr/sbin'
...@@ -9,4 +10,6 @@ if os.environ['script_type'] == 'up': ...@@ -9,4 +10,6 @@ if os.environ['script_type'] == 'up':
'mtu', os.environ['tun_mtu']) 'mtu', os.environ['tun_mtu'])
# Write into pipe external ip address received # Write into pipe external ip address received
os.write(int(sys.argv[1]), '%(script_type)s %(common_name)s %(OPENVPN_external_ip)s\n' % os.environ) import time
os.write(int(sys.argv[1]), "%s %s %s %s\n" % (script_type,
os.environ['common_name'], time.time(), os.environ['OPENVPN_external_ip']))
...@@ -40,6 +40,7 @@ class MultiGatewayManager(dict): ...@@ -40,6 +40,7 @@ class MultiGatewayManager(dict):
class Connection(object): class Connection(object):
_retry = routes = 0 _retry = routes = 0
time = float('inf')
def __init__(self, tunnel_manager, address_list, iface, prefix): def __init__(self, tunnel_manager, address_list, iface, prefix):
self.tunnel_manager = tunnel_manager self.tunnel_manager = tunnel_manager
...@@ -60,6 +61,7 @@ class Connection(object): ...@@ -60,6 +61,7 @@ class Connection(object):
def open(self): def open(self):
tm = self.tunnel_manager tm = self.tunnel_manager
self.time = time.time()
self.process = plib.client( self.process = plib.client(
self.iface, (self.address_list[self._retry],), tm.encrypt, self.iface, (self.address_list[self._retry],), tm.encrypt,
'--tls-remote', '%u/%u' % (int(self._prefix, 2), len(self._prefix)), '--tls-remote', '%u/%u' % (int(self._prefix, 2), len(self._prefix)),
...@@ -406,12 +408,17 @@ class TunnelManager(object): ...@@ -406,12 +408,17 @@ class TunnelManager(object):
if self._gateway_manager is not None: if self._gateway_manager is not None:
self._gateway_manager.remove(trusted_ip) self._gateway_manager.remove(trusted_ip)
def _ovpn_route_up(self, common_name, ip): def _ovpn_route_up(self, common_name, time, ip):
prefix = utils.binFromSubnet(common_name) prefix = utils.binFromSubnet(common_name)
c = self._connection_dict.get(prefix)
if c and c.time < float(time):
try: try:
self._connection_dict[prefix].connected() c.connected()
except KeyError: except (KeyError, TypeError), e:
pass logging.error("%s (route_up %s)", e, common_name)
else:
logging.info("ignore route_up notification for %s %r",
common_name, tuple(self._connection_dict))
if self._ip_changed: if self._ip_changed:
family, address = self._ip_changed(ip) family, address = self._ip_changed(ip)
if address: if address:
......
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