From 425690a9e2bef2e5b58fb34d342ee36a3ab8a107 Mon Sep 17 00:00:00 2001 From: Guillaume Valadon Date: Sat, 16 Mar 2024 11:45:12 +0100 Subject: [PATCH] Use IPv6 default route when no IPv4 is available --- doc/scapy/usage.rst | 3 +++ scapy/interfaces.py | 16 ++++++++++++++++ test/regression.uts | 13 +++++++++++++ 3 files changed, 32 insertions(+) diff --git a/doc/scapy/usage.rst b/doc/scapy/usage.rst index 67b3f02a05c..cabd87a5e29 100644 --- a/doc/scapy/usage.rst +++ b/doc/scapy/usage.rst @@ -226,6 +226,9 @@ make\_table() displays a table according to a lambda function Sending packets --------------- +.. note:: + Scapy automatically detects the network interface to be used by default, and stores this result in ``conf.iface``. Packets built by Scapy uses this variable to set relevant fields such as Ethernet source addresses. When sending packets, with functions such as ``send()``, Scapy will use the network interface stored in ``conf.iface``. This behavior can be changed using the ``iface=`` argument. With IPv6 and link-local addresses, it is mandatory to setup both ``conf.iface`` and ``iface=`` the same value to get the desired result, as Scapy cannot find which interface to use for link-local communications. + .. index:: single: Sending packets, send diff --git a/scapy/interfaces.py b/scapy/interfaces.py index f40f529ea8b..38ca1e0b6b1 100644 --- a/scapy/interfaces.py +++ b/scapy/interfaces.py @@ -372,6 +372,8 @@ def get_if_list(): def get_working_if(): # type: () -> NetworkInterface """Return an interface that works""" + + # IPv4 # return the interface associated with the route with smallest # mask (route by default if it exists) routes = conf.route.routes[:] @@ -383,6 +385,20 @@ def get_working_if(): iface = resolve_iface(ifname) # type: ignore if iface.is_valid(): return iface + + # IPv6 + routes_ipv6 = conf.route6.routes + default_routes_ipv6 = [r for r in routes_ipv6 if r[0] == "::"] + if default_routes_ipv6: + # Sort the default routes using the priority (at index -1) + tmp_routes = sorted(default_routes_ipv6, key=lambda r: r[-1]) + + # Return the interface (at index 3) of the highest priority default + ifname = tmp_routes[-1][3] + iface = resolve_iface(ifname) + if iface.is_valid(): + return iface + # There is no hope left return resolve_iface(conf.loopback_name) diff --git a/test/regression.uts b/test/regression.uts index 5b5737a570e..0c8a7893edb 100644 --- a/test/regression.uts +++ b/test/regression.uts @@ -334,6 +334,19 @@ assert "cuz you know the way to go" + conf.iface # right + _test_get_working_if() +# Test IPv6 default route interface selection +ni = NetworkInterface(InterfaceProvider(), {"name": "scapy0", "ips": ["::1"], "mac": "aa:aa:aa:aa:aa:aa"}) + +import mock +@mock.patch("scapy.interfaces.conf.ifaces", {}) +@mock.patch("scapy.interfaces.conf.route.routes", []) +@mock.patch("scapy.interfaces.conf.route6.routes", [("::", 0, "", ni, [""], 0)]) +def _test_get_working_if_v6(): + + assert get_working_if() == ni + +_test_get_working_if_v6() + = Test conf.ifaces conf.iface