Skip to content

Commit f655438

Browse files
committed
Fix interface selection for IPv6-only hosts
Instead of considering the full IPv4 routing table, we only look for IPv4 default route and IPv6 routes shorter or equal /8 (for instance `2000::/3`). Since the `conf.route6` is not ready yet when we first run, we reload `conf.ifaces` after loading `route6.py`. This fixes #4304 Signed-off-by: Ondřej Caletka <[email protected]>
1 parent 8cea357 commit f655438

File tree

2 files changed

+16
-5
lines changed

2 files changed

+16
-5
lines changed

scapy/interfaces.py

+14-5
Original file line numberDiff line numberDiff line change
@@ -372,11 +372,20 @@ def get_if_list():
372372
def get_working_if():
373373
# type: () -> NetworkInterface
374374
"""Return an interface that works"""
375-
# return the interface associated with the route with smallest
376-
# mask (route by default if it exists)
377-
routes = conf.route.routes[:]
378-
routes.sort(key=lambda x: x[1])
379-
ifaces = (x[3] for x in routes)
375+
# return the interface associated with the default route
376+
# IPv4 default route is preferred, then IPv6 route
377+
# shorter or equal than /8 or any interface as a fallback
378+
default_routes_v4 = (x for x in conf.route.routes if x[1] == 0)
379+
if conf.route6:
380+
default_routes_v6 = (x for x in conf.route6.routes if x[1] <= 8)
381+
default_routes_v6 = sorted(default_routes_v6, key=lambda x: x[1])
382+
else:
383+
default_routes_v6 = []
384+
385+
ifaces = (x[3] for x in itertools.chain(
386+
default_routes_v4,
387+
default_routes_v6)
388+
)
380389
# First check the routing ifaces from best to worse,
381390
# then check all the available ifaces as backup.
382391
for ifname in itertools.chain(ifaces, conf.ifaces.values()):

scapy/route6.py

+2
Original file line numberDiff line numberDiff line change
@@ -360,3 +360,5 @@ def route(self, dst="", dev=None, verbose=conf.verb):
360360

361361

362362
conf.route6 = Route6()
363+
# IPv6 routing table can influence default interface selection
364+
conf.ifaces.reload()

0 commit comments

Comments
 (0)