Commit Graph

293 Commits (c0aa60c573f72a3fddf9d73b7cdca1bd469098dd)

Author SHA1 Message Date
Jason Ish 9d5158594f util-device: break into public and private definitions
util-device.h exposes some details that are particularly problematic
for C++, even when wrapped in 'extern "C"'. To address this, break the
header into public and private parts. The public part exposes
LiveDevice as an opaque data structure, while the private header has
the actual definition.

The idea is that only Suricata C source files should include the
private header, it should not be re-included in any other header
file. And this is the header library users should use, however we
don't enforce it with tecnical means, a library user could still
include the private header, but the clue there is in the name.
7 months ago
Jason Ish 51f7b5924d af-packet: remove build conditional for tpacket-v3
All kernels on supported distrubtions should now support tpacket-v3,
so only enable af-packet if v2 and v3 are available.
8 months ago
Victor Julien 749ffbd06a af-packet: use actual snaplen in bpf
Avoids setting a 0 snaplen in BPF, leading to an error.

Fixes: b8b6ed550a ("af-packet: delay setting default-packet-size for af-packet")

Ticket: #7618.
8 months ago
Jason Ish b8b6ed550a af-packet: delay setting default-packet-size for af-packet
AF_PACKET needs more information about its configuration before we can
set the default packet size, so on startup, leave unset in suricata.c
if in AF_PACKET mode.

If defrag is enabled, use a default packet size of 9k for tpacket-v2.
This can still lead to truncation events, then the user can increase
their 'default-packet-size'.

Tpacket-v3 does not need an increased packet size as it will handle
any size of packet that is smaller than the configured block size
which now has a default of 128k.

9k for the snap is somewhat arbitrary but is large enough for the
common 9000 jumbo frame plus some extra headers including tpacket
headers.

Ticket: #7458
8 months ago
Jason Ish d78f2c9a4e af-packet: add event for packets truncated by af-packet
Ticket: #7458
8 months ago
Jason Ish 5871c6458c af-packet: make tpacket-v2 block size configurable
With the change of the default tpacket-v2 block size from 32k to 128k,
allow it to be configurable for users who may want to make it larger,
or revert it back to the pre 7.0.9 default of 32k.

Ticket: #7458
8 months ago
Jeff Lucovsky 7d5c170dd2 gen/bool: Clarify bool checks
Remove comparisons with true or false with C bools.
9 months ago
Eric Leblond 22a71e7317 af-packet: code cleaning
clang-tidy did detect the -1 return value was not compatible with
TmEcode enum.
9 months ago
Victor Julien 923ad6af77 af-packet: speed up thread sync during startup
Threads are initialized sequentially to allow for a predictable mapping
of threads and queues. Not all parts of the start up need to be done
sequentially. The setting up of the rings can be very expensive, taking
of a couple of hundred milliseconds. The ring setup doesn't need to be
done sequentially though.

This patch releases the thread early, after bind but before the ring
setups.

Ticket: #7272.
12 months ago
Victor Julien 96a0ffadde packetpool: allow larger max-pending-packets
Original limit was due to a specific data structure.
1 year ago
Jeff Lucovsky 834cd6fbdb af-packet: Remove unused preprocessor define
Remove unused preprocessor value; exposed by compilation warning
1 year ago
Philippe Antoine ce9bfba76a ci: fix and test with Wunused-macros
Ticket: 6937
2 years ago
Victor Julien 8698610b4a af-packet: fix eBPF/XDP compilation 2 years ago
Victor Julien 54362d44db decode/ethernet: move ethh into L2 section
L2 section similar to L3 and L4 sections.

Ticket: #6938.
2 years ago
Victor Julien 5c3036bea8 decode/udp: add and use PacketIsUDP inline func
Replace PKT_IS_UDP macro with PacketIsUDP inline function.

For better readability and type checking.

Ticket: #5517.
2 years ago
Victor Julien cca912e9b3 decode/tcp: add and use PacketIsTCP inline func
To prepare future changes to the Packet header pointers.

Ticket: #5517.
2 years ago
Victor Julien eb3ca643c1 decode: reduce PKT_IS_IPV4/PKT_IS_IPV6 use
Replace it with inline functions. Adds inline functions to
wrap PKT_IS_IPV4/PKT_IS_IPV6.

This is in preparation of removing the macro's, and cleaning up the
header pointers.

Ticket: #5517.
2 years ago
Victor Julien 6066c4d6e7 decode/tunnel: improve tunnel handling
Give each packet explicit tunnel type `ttype`: none, root, child.

Assigning happens when a (tunnel) packet is set up and is thread
safe.
2 years ago
Jeff Lucovsky 193e0ea1a9 memory/alloc: Use SCCalloc instead of malloc/memset 2 years ago
Shivani Bhardwaj 2fa0fac289 af-packet: fetch mtu info once
With the current layout and fn calls, it was seen that once in the
beginning after the MTU was found and displayed to the user, when the
threads spawned, each thread displayed MTU info as a part of
AFPPeersListAdd fn. This happened in AF_PACKET IPS mode and led to
excessive MTU logs.
Save this info in the LiveDevice struct and avoid calling the unneeded
fns later on.

Bug 5831
2 years ago
Shivani Bhardwaj 572f8a3da6 util/ioctl: use LiveDevice to retrieve name
The fn GetIfaceMaxPacketSize now uses LiveDevice object as a param
instead of a string. This was done to keep the logic of checking for the
device to this function itself instead of having callers first determine
whether the device exists or not.
This also falls in line with the changes made to avoid excessive MTU
logs in the following commit.

Related to redmine ticket 5831.
2 years ago
Jeff Lucovsky 0d2268ddfc decode/vlan: Decode upto 3 layers of VLAN
Issue: 2816

This commit increase the number of VLAN layers supported by Suricata
from 2 to 3. 3-layers are dubbed "Q-in-Q-in-Q".

Note that 3 layers are not compliant with any existing standard but are
often seen in larger deployments.
2 years ago
Victor Julien 8dc58a6e9d capture: spelling 3 years ago
Jason Ish b5fbdc3e5f capture: use uint16_t for max_pending_packets
Use a fixed type of max_pending_packets instead of intmax_t which can
differ based on the platform/standard library.

Should also prevent lints about possible arithmetic overflow.
3 years ago
Arne Welzel 51aef3c230 af-packet: Ignore outgoing packets on loopback interfaces
When reading a loopback interface, packets are received twice: Once as
outgoing packets and once as incoming packets.

Libpcap ignores outgoing packets. With current versions of Suricata, sniffing
a single http://localhost:80 request over lo using the af-packet source
minimally shows two syn packets, two synacks and twice as many packets in
the stats entries than you'd expect when running tcpdump or Wireshark.
3 years ago
Victor Julien 0265c13550 src: fix extern max_pending_packets type 3 years ago
Jeff Lucovsky 9fbe683642 time: Rework SCTime_t into a struct
Issue: 5718

This commit changes SCTime_t to a struct with members setup as
bitfields.
3 years ago
Jeff Lucovsky 31793aface time: Replace struct timeval with scalar value
Issue: 5718

This commit switches the majority of time handling to a new type --
SCTime_t -- which is a 64 bit container for time:
- 44 bits -- seconds
- 20 bits -- useconds
3 years ago
Victor Julien ebd8728219 src: fix strict-prototype warnings
Tested on Fedora 37 with clang 15.

app-layer.c:1055:27: error: a function declaration without a prototype is deprecated in all versions of C [-Werror,-Wstrict-prototypes]
void AppLayerSetupCounters()
                          ^
                           void
app-layer.c:1176:29: error: a function declaration without a prototype is deprecated in all versions of C [-Werror,-Wstrict-prototypes]
void AppLayerDeSetupCounters()
                            ^
                             void
2 errors generated.
3 years ago
Victor Julien 8b846bdcff af-packet: output cleanups and unification
Make all messages use the same format:

<iface>: <message>
3 years ago
Victor Julien b31ffde6f4 output: remove error codes from output 3 years ago
Victor Julien 39f5c7f56a error: use SC_EINVAL for invalid input 3 years ago
Victor Julien e042cd785e error: use SC_ENOMEM for alloc errors 3 years ago
Victor Julien ef881c942b af-packet: output cleanups 3 years ago
Philippe Antoine ad713246a9 src: remove double includes
Keep the unconditional include to be sure it works

git grep '#include "' src/*.c | sort | uniq -c | awk '$1 > 1'
3 years ago
Philippe Antoine 62352ad030 src: fix remaining cppclean warnings 3 years ago
Philippe Antoine e85f3916e3 src: fix integer warnings
and adds defrag debug validations
3 years ago
Richard McConnell 13beba141c source: add THV_RUNNING flag to notify of running state
Each module (thread) updates its status to indicate running.
Main thread awaits for all threads to be in a running state
before continuing the initialisation process

Implements feature 5384
(https://redmine.openinfosecfoundation.org/issues/5384)
3 years ago
Victor Julien cd2a5ec84f packet: move action functions to packet files 3 years ago
Victor Julien 6c200c7793 detect: issue drop to root packet in all cases
Update DROP action handling in tunnel packets. DROP/REJECT action is set
to outer (root) and inner packet.

Check action flags both against outer (root) and inner packet.

Remove PACKET_SET_ACTION macro. Replace with RESET for the one reset usecase.
The reason to remove is to make the logic easier to understand.

Reduce scope of RESET macros.

Rename PacketTestAction to PacketCheckAction except in unittests. Keep
PacketTestAction as a wrapper around PacketCheckAction. This makes it
easier to trace the action handling in the real code.

Fix rate_filter setting actions directly.

General code cleanups.

Bug: #5571.
3 years ago
Lukas Sismis 8845c07a90 bypass: af-packet: fix memory leak - reassign of EBPFBypassData
AF-Packet bypass function in some situations allocates EBPF bypass data
for an already bypassed flow and assigns it to the flow without any checks

Issue: #5368
3 years ago
Eric Leblond 1c2fba57f8 suricata: introduce global linktype
As Suricata is not supporting pcap-ng we have to stick with one single
datalink type for the capture if ever we want to do pcap logging.
Assuming this, this patch introduces a function to set the link
type globally. This will be used with pcap conditional logging
to get the logging of TCP segments with the correct link type.
4 years ago
Victor Julien fedced209d af-packet/v2: use proper type for ring
cppcheck:

src/source-af-packet.c:1762:19: warning: Size of pointer 'v2' used instead of size of its data. This is likely to lead to a buffer overflow. You probably intend to write 'sizeof(*v2)'. [pointerSize]
        ptv->ring.v2 = SCMalloc(ptv->req.v2.tp_frame_nr * sizeof (union thdr *));
                  ^
src/source-af-packet.c:1767:26: warning: Size of pointer 'v2' used instead of size of its data. This is likely to lead to a buffer overflow. You probably intend to write 'sizeof(*v2)'. [pointerSize]
        memset(ptv->ring.v2, 0, ptv->req.v2.tp_frame_nr * sizeof (union thdr *));
                         ^

scan-build:

CC       source-af-packet.o
source-af-packet.c:1762:24: warning: Result of 'malloc' is converted to a pointer of type 'char', which is incompatible with sizeof operand type 'union thdr *' [unix.MallocSizeof]
        ptv->ring.v2 = SCMalloc(ptv->req.v2.tp_frame_nr * sizeof (union thdr *));
                       ^~~~~~~~                           ~~~~~~~~~~~~~~~~~~~~~
./util-mem.h:35:18: note: expanded from macro 'SCMalloc'
                 ^~~~~~
1 warning generated.

Bug: #5291.
4 years ago
Victor Julien 39bf623fdd af-packet: add send error counter 4 years ago
Victor Julien 8a5b945c7b af-packet: only ref mpeer if needed in tpacket v2
We only use it in autofp mode, for reference counting purposes.

Removes 2 atomic operations per packet in the more common workers
runmode.
4 years ago
Victor Julien e9c6ad19b3 af-packet: optimize packet setup
Don't set fields we don't use in V3.
4 years ago
Victor Julien cad0ff9ebb af-packet: add counters on how poll() works
Use `capture.afpacket.*` counter name space.
4 years ago
Victor Julien c7ad3f8d30 af-packet: don't check ifstate per send call in IPS
Instead just accept that the socket state leads to `sendto` errors.
So print at most one error per socket.
4 years ago
Victor Julien dab036727f af-packet: simplify AFPWritePacket
Since return code was ignored by all callers, we can just turn it into a
void function and slightly simplify the logic.
4 years ago
Victor Julien 3f79f452ad af-packet: use BUG_ON for 'impossible' condition 4 years ago