Skip to content
Merged
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
27 changes: 20 additions & 7 deletions scapy/arch/bpf/pfroute.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,14 @@
import socket
import struct

from scapy.consts import BIG_ENDIAN, BSD, NETBSD, OPENBSD, DARWIN
from scapy.consts import (
BIG_ENDIAN,
BSD,
DARWIN,
IS_64BITS,
NETBSD,
OPENBSD,
)
from scapy.config import conf
from scapy.error import log_runtime
from scapy.packet import (
Expand Down Expand Up @@ -418,7 +425,7 @@ class SockAddrsField(FieldListField):
holds_packets = 1

def __init__(self, name):
if DARWIN:
if not IS_64BITS or DARWIN:
align = 4
else:
align = 8
Expand Down Expand Up @@ -460,7 +467,8 @@ class if_data(Packet):
32 if BIG_ENDIAN else -32,
_IFCAP,
),
StrFixedLenField("ifi_lastchange", 0, length=16),
StrFixedLenField("ifi_lastchange", 0,
length=16 if IS_64BITS else 8),
]

def default_payload_class(self, payload: bytes) -> Type[Packet]:
Expand Down Expand Up @@ -489,7 +497,8 @@ class if_data(Packet):
Field("ifi_omcasts", 0, fmt="=Q"),
Field("ifi_iqdrops", 0, fmt="=Q"),
Field("ifi_noproto", 0, fmt="=Q"),
StrFixedLenField("ifi_lastchange", 0, length=16),
StrFixedLenField("ifi_lastchange", 0,
length=16 if IS_64BITS else 8),
]

def default_payload_class(self, payload: bytes) -> Type[Packet]:
Expand Down Expand Up @@ -563,7 +572,8 @@ class if_data(Packet):
Field("ifi_noproto", 0, fmt="=Q"),
Field("ifi_hwassist", 0, fmt="=Q"),
Field("tt", 0, fmt="=Q"),
StrFixedLenField("tv", 0, length=16),
StrFixedLenField("tv", 0,
length=16 if IS_64BITS else 8),
]

def default_payload_class(self, payload: bytes) -> Type[Packet]:
Expand Down Expand Up @@ -752,13 +762,15 @@ class rt_metrics(Packet):
Field("rmx_rtt", 0, fmt="=I"),
Field("rmx_rttvar", 0, fmt="=I"),
Field("rmx_pksent", 0, fmt="=I"),
StrFixedLenField("rmx_filler", b"", length=16),
StrFixedLenField("rmx_filler", 0,
length=16 if IS_64BITS else 8),
]

def default_payload_class(self, payload: bytes) -> Type[Packet]:
return conf.padding_layer

else:
# FreeBSD

class rt_metrics(Packet):
fields_desc = [
Expand All @@ -774,7 +786,8 @@ class rt_metrics(Packet):
Field("rmx_pksent", 0, fmt="=Q"),
Field("rmx_weight", 0, fmt="=Q"),
Field("rmx_nhidx", 0, fmt="=Q"),
StrFixedLenField("rmx_filler", b"", length=16),
StrFixedLenField("rmx_filler", 0,
length=16 if IS_64BITS else 8),
]

def default_payload_class(self, payload: bytes) -> Type[Packet]:
Expand Down
51 changes: 50 additions & 1 deletion test/regression.uts
Original file line number Diff line number Diff line change
Expand Up @@ -2964,7 +2964,7 @@ def mock_inet_ntop(af, data):


class BSDLoader:
def __init__(self, OPENBSD=False, FREEBSD=False, NETBSD=False, DARWIN=False, sysctldata=None, ifaces={}, AF_INET6=socket.AF_INET6):
def __init__(self, OPENBSD=False, FREEBSD=False, NETBSD=False, DARWIN=False, sysctldata=None, ifaces={}, AF_INET6=socket.AF_INET6, IS_64BITS=True):
self.sysctldata = sysctldata
self.ifaces = ifaces
socket.AF_LINK = 18
Expand All @@ -2976,6 +2976,7 @@ class BSDLoader:
# mock.patch('scapy.consts.FREEBSD', FREEBSD),
mock.patch('scapy.consts.NETBSD', NETBSD),
mock.patch('scapy.consts.DARWIN', DARWIN),
mock.patch('scapy.consts.IS_64BITS', IS_64BITS),
]
def __enter__(self):
# Apply patches that only occur when loading
Expand Down Expand Up @@ -3029,6 +3030,26 @@ assert routes == [
(2887241727, 4294967295, '172.23.192.138', 'hvn0', '172.23.192.138', 1)
]

= OpenBSD 7.6 GENERIC#531 i386 - read_routes()
~ mock_read_routes_bsd little_endian_only

import zlib
_PFROUTE_DATA = zlib.decompress(bytes.fromhex('789c7bc2c0ca92c0c0c8c0c0c0c160cec2c0e0ccc10006fa760c28c08089012b60c52e0c07024c98fc35120f1871c92b083132b031b331a4a41a3088c632a47262316f8dc4ab1330be12434a4672414e6a62716a0a2e371c00fb919901ec4720e989c38f584103612520373d40e3d73330a0f8f10392bc8210338304d03939f90638cd43d68fee7e6f1ab8bf9e80fbff53c5fd8c60f7bb02630d9bfbb126b1062483f0bb9f111fff3f1050e67e109705ec7e47c606aceec70588713f304fa0f111691ce27e440a22358f5c80bb9f1912fe2ccc58ddbf1c4a478aec4a4716c791f5d1dd0ff726d87d4008749cd0e702ecea51e3835cff40d3138b0256ff781370377eff20ec23c67d94f8e700ba7f181cb0fa673a45fe79ff1f958f9ebec877ff281805a360148c8251300a46c128183a0000223e6527'))

with BSDLoader(OPENBSD=True, sysctldata=_PFROUTE_DATA, AF_INET6=24, IS_64BITS=False) as pfroute:
routes = pfroute.read_routes()

assert routes == [
(0, 0, '172.24.224.1', 'de0', '172.24.234.200', 1),
(3758096384, 4026531840, '127.0.0.1', 'lo0', '127.0.0.1', 1),
(2130706432, 4278190080, '127.0.0.1', 'lo0', '127.0.0.1', 1),
(2130706433, 4294967295, '127.0.0.1', 'lo0', '127.0.0.1', 1),
(2887311360, 4294963200, '172.24.234.200', 'de0', '172.24.234.200', 1),
(2887311361, 4294967295, '0.0.0.0', 'de0', '172.24.234.200', 1),
(2887314120, 4294967295, '0.0.0.0', 'de0', '172.24.234.200', 1),
(2887315455, 4294967295, '172.24.234.200', 'de0', '172.24.234.200', 1)
]


= OpenBSD 7.5 amd64 - read_routes6()
~ mock_read_routes_bsd little_endian_only
Expand Down Expand Up @@ -3059,6 +3080,34 @@ assert routes == [
('ff02:3::', 32, 'fe80:3::1', 'lo0', ['fe80:3::1'], 1)
]

= OpenBSD 7.6 GENERIC#531 i386 - read_routes6()
~ mock_read_routes_bsd little_endian_only

import zlib

_PFROUTE_DATA = zlib.decompress(bytes.fromhex('789ced96310e824010450717136269456141ac2cedac2dacaced3d808947905b781d8ee21138814836226477083b0ae89aff0a9a1966e7e5872c394dc32329228a68533ef71169ae87803a49bb5b16b1b816346b4583aa21992b8acb954fe7b5786efef20db4ef8e96ba68faaeb80929d1ac4dc6e16ca96fe5dc8fef58f9d6397d37df617d93896caf86afd5e087ef45b497ffbe37d15eb56f6e35f8e16be7f4cff9de995e27dfcc6ef0c23793ed35bc6f75ff26ba3840beca3cdba5f6c9fdcbcd1d2bdf8219e7f6fdda0dfde41b6adfedf39e347d59fb949dcb9e5dfaaab65a57bee67b5ee4fbf6ff86dde045be93dfc81700000000000000000000009f7900a834b765'))


with BSDLoader(OPENBSD=True, sysctldata=_PFROUTE_DATA, AF_INET6=24, IS_64BITS=False) as pfroute:
routes = pfroute.read_routes6()

assert routes == [
('::', 96, '::1', 'lo0', ['::1'], 1),
('::1', 128, '::1', 'lo0', ['::1'], 1),
('::ffff:0.0.0.0', 96, '::1', 'lo0', ['::1'], 1),
('2002::', 24, '::1', 'lo0', ['::1'], 1),
('2002:7f00::', 24, '::1', 'lo0', ['::1'], 1),
('2002:e000::', 20, '::1', 'lo0', ['::1'], 1),
('2002:ff00::', 24, '::1', 'lo0', ['::1'], 1),
('fe80::', 10, '::1', 'lo0', ['::1'], 1),
('fec0::', 10, '::1', 'lo0', ['::1'], 1),
('fe80:3::1', 128, 'fe80:3::1', 'lo0', ['fe80:3::1'], 1),
('ff01::', 16, '::1', 'lo0', ['::1'], 1),
('ff01:3::', 32, 'fe80:3::1', 'lo0', ['fe80:3::1'], 1),
('ff02::', 16, '::1', 'lo0', ['::1'], 1),
('ff02:3::', 32, 'fe80:3::1', 'lo0', ['fe80:3::1'], 1)
]

= FreeBSD 14.1 amd64 - read_routes()
~ mock_read_routes_bsd little_endian_only

Expand Down
Loading