diff --git a/net-dhcp/network.py b/net-dhcp/network.py index ddaa794..9cabc83 100644 --- a/net-dhcp/network.py +++ b/net-dhcp/network.py @@ -331,32 +331,35 @@ class ContainerDHCPManager: self._thread.start() def _on_event(self, dhcp, event_type, _event): - if event_type != udhcpc.EventType.RENEW or not dhcp.gateway: - return + if event_type == udhcpc.EventType.DECONFIG: + logger.info('[dhcp container] DECONFIG Event %s', _event) + logger.info('[dhcp container] Flushing IP addresses') + subprocess.check_call(['nsenter', f'-n{dhcp.netns}', '--', '/sbin/ip', 'address', 'flush', 'dev', + str(dhcp.iface['ifname'])], timeout=1, stdin=subprocess.DEVNULL, stdout=subprocess.DEVNULL, + stderr=subprocess.DEVNULL) + elif event_type == udhcpc.EventType.RENEW or event_type == udhcpc.EventType.BOUND: + if event_type == udhcpc.EventType.RENEW: + logger.info('[dhcp container] RENEW Event %s', _event) + elif event_type == udhcpc.EventType.BOUND: + logger.info('[dhcp container] BOUND Event %s', _event) - logger.info('[dhcp container] Replacing gateway with %s', dhcp.gateway) - subprocess.check_call(['nsenter', f'-n{dhcp.netns}', '--', '/sbin/ip', 'route', 'replace', 'default', 'via', - str(dhcp.gateway)], timeout=1, stdin=subprocess.DEVNULL, stdout=subprocess.DEVNULL, - stderr=subprocess.DEVNULL) + logger.info('[dhcp container] Flushing IP addresses') + subprocess.check_call(['nsenter', f'-n{dhcp.netns}', '--', '/sbin/ip', 'address', 'flush', 'dev', + str(dhcp.iface['ifname'])], timeout=1, stdin=subprocess.DEVNULL, stdout=subprocess.DEVNULL, + stderr=subprocess.DEVNULL) - # TODO: Adding default route with NDB seems to be broken (because of the dst syntax?) - #for route in ndb.routes: - # if route['type'] != rtypes['RTN_UNICAST'] or \ - # route['oif'] != dhcp.iface['index'] or \ - # (route['family'] == socket.AF_INET6 and not self.ipv6) or \ - # route['dst'] not in ('', '/0'): - # continue + logger.info('[dhcp container] Adding IP addresses %s', dhcp.ip) + subprocess.check_call(['nsenter', f'-n{dhcp.netns}', '--', '/sbin/ip', 'address', 'add', str(dhcp.ip), 'dev', + str(dhcp.iface['ifname'])], timeout=1, stdin=subprocess.DEVNULL, stdout=subprocess.DEVNULL, + stderr=subprocess.DEVNULL) - # # Needed because Route.remove() doesn't like a blank destination - # logger.info('Removing default route via %s', route['gateway']) - # route['dst'] = '::' if route['family'] == socket.AF_INET6 else '0.0.0.0' - # (route - # .remove() - # .commit()) - - #logger.info('Adding default route via %s', dhcp.gateway) - #(ndb.routes.add({'oif': dhcp.iface['index'], 'gateway': dhcp.gateway}) - # .commit()) + if dhcp.gateway: + logger.info('[dhcp container] Replacing gateway with %s', dhcp.gateway) + subprocess.check_call(['nsenter', f'-n{dhcp.netns}', '--', '/sbin/ip', 'route', 'replace', 'default', 'via', + str(dhcp.gateway)], timeout=1, stdin=subprocess.DEVNULL, stdout=subprocess.DEVNULL, + stderr=subprocess.DEVNULL) + else: + logger.info('[dhcp container] Unhandled Event %s: %s', event_type,_event) def run(self): try: