Skip to content

Commit cd01ec1

Browse files
authored
Rewrite arch/linux (#4352)
* Rewrite arch/linux: interfaces/routes loading This rewrites much of the arch/linux code, in order to use a RTNETLINK socket instead of reading /proc/net/XXX. Among those: - the read_routes(6) functions - the linux interfaces provider - arch/linux util functions This adds support for multiple IPv4 addresses to interfaces, among with a generally much better handling of routes. fixes #4201 * Apply guedou suggestions * Restrain routes to RT_TABLE_LOCAL and RT_TABLE_MAIN
1 parent 8bb5ba3 commit cd01ec1

File tree

11 files changed

+1059
-377
lines changed

11 files changed

+1059
-377
lines changed

.config/mypy/mypy_check.py

Lines changed: 34 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -63,57 +63,56 @@
6363
"--ignore-missing-imports",
6464
# config
6565
"--follow-imports=skip", # Remove eventually
66-
"--config-file=" + os.path.abspath(
67-
os.path.join(
68-
localdir,
69-
"mypy.ini"
70-
)
71-
),
66+
"--config-file=" + os.path.abspath(os.path.join(localdir, "mypy.ini")),
7267
"--show-traceback",
73-
] + ([
74-
"--platform=" + PLATFORM
75-
] if PLATFORM else [])
68+
] + (["--platform=" + PLATFORM] if PLATFORM else [])
7669

7770
if PLATFORM.startswith("linux"):
78-
ARGS.extend([
79-
"--always-true=LINUX",
80-
"--always-false=OPENBSD",
81-
"--always-false=FREEBSD",
82-
"--always-false=NETBSD",
83-
"--always-false=DARWIN",
84-
"--always-false=WINDOWS",
85-
"--always-false=BSD",
86-
])
71+
ARGS.extend(
72+
[
73+
"--always-true=LINUX",
74+
"--always-false=OPENBSD",
75+
"--always-false=FREEBSD",
76+
"--always-false=NETBSD",
77+
"--always-false=DARWIN",
78+
"--always-false=WINDOWS",
79+
"--always-false=BSD",
80+
]
81+
)
8782
FILES = [x for x in FILES if not x.startswith("scapy/arch/windows")]
8883
elif PLATFORM.startswith("win32"):
89-
ARGS.extend([
90-
"--always-false=LINUX",
91-
"--always-false=OPENBSD",
92-
"--always-false=FREEBSD",
93-
"--always-false=NETBSD",
94-
"--always-false=DARWIN",
95-
"--always-true=WINDOWS",
96-
"--always-false=WINDOWS_XP",
97-
"--always-false=BSD",
98-
])
84+
ARGS.extend(
85+
[
86+
"--always-false=LINUX",
87+
"--always-false=OPENBSD",
88+
"--always-false=FREEBSD",
89+
"--always-false=NETBSD",
90+
"--always-false=DARWIN",
91+
"--always-true=WINDOWS",
92+
"--always-false=WINDOWS_XP",
93+
"--always-false=BSD",
94+
]
95+
)
9996
FILES = [
100-
x for x in FILES if (
101-
x not in {
97+
x
98+
for x in FILES
99+
if (
100+
x
101+
not in {
102102
# Disabled on Windows
103-
"scapy/arch/linux.py",
104103
"scapy/arch/unix.py",
105104
"scapy/arch/solaris.py",
106105
"scapy/contrib/cansocket_native.py",
107106
"scapy/contrib/isotp/isotp_native_socket.py",
108107
}
109-
) and not x.startswith("scapy/arch/bpf")
108+
)
109+
and not x.startswith("scapy/arch/bpf")
110+
and not x.startswith("scapy/arch/linux")
110111
]
111112
else:
112113
raise ValueError("Unknown platform")
113114

114115
# Run mypy over the files
115-
ARGS += [
116-
os.path.abspath(f) for f in FILES
117-
]
116+
ARGS += [os.path.abspath(f) for f in FILES]
118117

119118
mypy_main(args=ARGS)

.config/mypy/mypy_enabled.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ scapy/arch/bpf/core.py
1616
scapy/arch/bpf/supersocket.py
1717
scapy/arch/common.py
1818
scapy/arch/libpcap.py
19-
scapy/arch/linux.py
19+
scapy/arch/linux/__init__.py
20+
scapy/arch/linux/rtnetlink.py
2021
scapy/arch/solaris.py
2122
scapy/arch/unix.py
2223
scapy/arch/windows/__init__.py

scapy/arch/__init__.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,6 @@ def get_if_raw_addr6(iff):
125125
# Next step is to import following architecture specific functions:
126126
# def attach_filter(s, filter, iface)
127127
# def get_if(iff,cmd)
128-
# def get_if_index(iff)
129128
# def get_if_raw_addr(iff)
130129
# def get_if_raw_hwaddr(iff)
131130
# def in6_getifaddr()

scapy/arch/common.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,15 @@
77
Functions common to different architectures
88
"""
99

10+
import socket
1011
import ctypes
12+
1113
from scapy.config import conf
1214
from scapy.data import MTU, ARPHDR_ETHER, ARPHRD_TO_DLT
1315
from scapy.error import Scapy_Exception
14-
from scapy.interfaces import network_name
16+
from scapy.interfaces import network_name, resolve_iface, NetworkInterface
1517
from scapy.libs.structures import bpf_program
18+
from scapy.pton_ntop import inet_pton
1619
from scapy.utils import decode_locale_str
1720

1821
# Type imports
@@ -33,7 +36,6 @@
3336
"RUNNING",
3437
"NOARP",
3538
"PROMISC",
36-
"NOTRAILERS",
3739
"ALLMULTI",
3840
"MASTER",
3941
"SLAVE",
@@ -47,6 +49,15 @@
4749
]
4850

4951

52+
def get_if_raw_addr(iff):
53+
# type: (Union[NetworkInterface, str]) -> bytes
54+
"""Return the raw IPv4 address of interface"""
55+
iff = resolve_iface(iff)
56+
if not iff.ip:
57+
return b"\x00" * 4
58+
return inet_pton(socket.AF_INET, iff.ip)
59+
60+
5061
# BPF HANDLERS
5162

5263

0 commit comments

Comments
 (0)