Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix interface selection for IPv6-only hosts #4380

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 14 additions & 5 deletions scapy/interfaces.py
Original file line number Diff line number Diff line change
Expand Up @@ -372,11 +372,20 @@ def get_if_list():
def get_working_if():
# type: () -> NetworkInterface
"""Return an interface that works"""
# return the interface associated with the route with smallest
# mask (route by default if it exists)
routes = conf.route.routes[:]
routes.sort(key=lambda x: x[1])
ifaces = (x[3] for x in routes)
# return the interface associated with the default route
# IPv4 default route is preferred, then IPv6 route
# shorter or equal than /8 or any interface as a fallback
default_routes_v4 = (x for x in conf.route.routes if x[1] == 0)
if conf.route6:
default_routes_v6 = (x for x in conf.route6.routes if x[1] <= 8)
default_routes_v6 = sorted(default_routes_v6, key=lambda x: x[1])
else:
default_routes_v6 = []

ifaces = (x[3] for x in itertools.chain(
default_routes_v4,
default_routes_v6)
)
# First check the routing ifaces from best to worse,
# then check all the available ifaces as backup.
for ifname in itertools.chain(ifaces, conf.ifaces.values()):
Expand Down
2 changes: 2 additions & 0 deletions scapy/route6.py
Original file line number Diff line number Diff line change
Expand Up @@ -360,3 +360,5 @@ def route(self, dst="", dev=None, verbose=conf.verb):


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