Skip to content

Commit 6094ac0

Browse files
committed
Linux - check if auxdata is available
1 parent 24d0ad8 commit 6094ac0

File tree

2 files changed

+39
-16
lines changed

2 files changed

+39
-16
lines changed

scapy/arch/linux.py

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -461,12 +461,18 @@ def __init__(self, iface=None, type=ETH_P_ALL, promisc=None, filter=None,
461461
)
462462
if not six.PY2:
463463
# Receive Auxiliary Data (VLAN tags)
464-
self.ins.setsockopt(SOL_PACKET, PACKET_AUXDATA, 1)
465-
self.ins.setsockopt(
466-
socket.SOL_SOCKET,
467-
SO_TIMESTAMPNS,
468-
1
469-
)
464+
try:
465+
self.ins.setsockopt(SOL_PACKET, PACKET_AUXDATA, 1)
466+
self.ins.setsockopt(
467+
socket.SOL_SOCKET,
468+
SO_TIMESTAMPNS,
469+
1
470+
)
471+
self.auxdata_available = True
472+
except OSError:
473+
# Note: Auxiliary Data is only supported since
474+
# Linux 2.6.21
475+
warning("Your Linux Kernel does not support Auxiliary Data!")
470476
if isinstance(self, L2ListenSocket):
471477
self.outs = None
472478
else:

scapy/supersocket.py

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ class SuperSocket(six.with_metaclass(_SuperSocket_metaclass)):
6161
closed = 0
6262
nonblocking_socket = False
6363
read_allowed_exceptions = ()
64+
auxdata_available = False
6465

6566
def __init__(self, family=socket.AF_INET, type=socket.SOCK_STREAM, proto=0): # noqa: E501
6667
self.ins = socket.socket(family, type, proto)
@@ -85,16 +86,26 @@ def _recv_raw(self, sock, x):
8586
"""Internal function to receive a Packet,
8687
and process ancillary data.
8788
"""
88-
flags_len = socket.CMSG_LEN(4096)
8989
timestamp = None
90+
if not self.auxdata_available:
91+
pkt, _, _, sa_ll = sock.recvmsg(x)
92+
return pkt, sa_ll, timestamp
93+
flags_len = socket.CMSG_LEN(4096)
9094
pkt, ancdata, flags, sa_ll = sock.recvmsg(x, flags_len)
9195
if not pkt:
92-
return pkt, sa_ll
96+
return pkt, sa_ll, timestamp
9397
for cmsg_lvl, cmsg_type, cmsg_data in ancdata:
9498
# Check available ancillary data
9599
if (cmsg_lvl == SOL_PACKET and cmsg_type == PACKET_AUXDATA):
96100
# Parse AUXDATA
97-
auxdata = tpacket_auxdata.from_buffer_copy(cmsg_data)
101+
try:
102+
auxdata = tpacket_auxdata.from_buffer_copy(cmsg_data)
103+
except ValueError:
104+
# Note: according to Python documentation, recvmsg()
105+
# can return a truncated message. A ValueError
106+
# exception likely indicates that Auxiliary
107+
# Data is not supported by the Linux kernel.
108+
return pkt, sa_ll, timestamp
98109
if auxdata.tp_vlan_tci != 0 or \
99110
auxdata.tp_status & TP_STATUS_VLAN_VALID:
100111
# Insert VLAN tag
@@ -213,13 +224,19 @@ def __init__(self, type=ETH_P_IP, filter=None, iface=None, promisc=None, nofilte
213224
if iface is not None:
214225
self.ins.bind((iface, type))
215226
if not six.PY2:
216-
# Receive Auxiliary Data (VLAN tags)
217-
self.ins.setsockopt(SOL_PACKET, PACKET_AUXDATA, 1)
218-
self.ins.setsockopt(
219-
socket.SOL_SOCKET,
220-
SO_TIMESTAMPNS,
221-
1
222-
)
227+
try:
228+
# Receive Auxiliary Data (VLAN tags)
229+
self.ins.setsockopt(SOL_PACKET, PACKET_AUXDATA, 1)
230+
self.ins.setsockopt(
231+
socket.SOL_SOCKET,
232+
SO_TIMESTAMPNS,
233+
1
234+
)
235+
self.auxdata_available = True
236+
except OSError:
237+
# Note: Auxiliary Data is only supported since
238+
# Linux 2.6.21
239+
warning("Your Linux Kernel does not support Auxiliary Data!")
223240

224241
def recv(self, x=MTU):
225242
pkt, sa_ll, ts = self._recv_raw(self.ins, x)

0 commit comments

Comments
 (0)