suricata
packet.c
Go to the documentation of this file.
1 /* Copyright (C) 2007-2026 Open Information Security Foundation
2  *
3  * You can copy, redistribute or modify this Program under the terms of
4  * the GNU General Public License version 2 as published by the Free
5  * Software Foundation.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * version 2 along with this program; if not, write to the Free Software
14  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
15  * 02110-1301, USA.
16  */
17 
18 #include "packet.h"
19 #include "packet-bindgen.h"
20 #include "pkt-var.h"
21 #include "flow.h"
22 #include "host.h"
23 #include "util-profiling.h"
24 #include "util-validate.h"
25 #include "action-globals.h"
26 #include "rust.h"
27 #include "app-layer-events.h"
28 
29 /** \brief issue drop action
30  *
31  * Set drop (+reject) flags in both current and root packet.
32  *
33  * \param action action bit flags. Must be limited to ACTION_DROP_REJECT|ACTION_ALERT
34  */
35 void PacketDrop(Packet *p, const uint8_t action, enum PacketDropReason r)
36 {
38 
40  p->drop_reason = (uint8_t)r;
41 
42  if (p->root) {
43  p->root->action |= action;
46  }
47  }
48  p->action |= action;
49 }
50 
51 bool PacketCheckAction(const Packet *p, const uint8_t a)
52 {
53  if (likely(p->root == NULL)) {
54  return (p->action & a) != 0;
55  } else {
56  /* check against both */
57  const uint8_t actions = p->action | p->root->action;
58  return (actions & a) != 0;
59  }
60 }
61 
62 uint8_t PacketGetAction(const Packet *p)
63 {
64  if (likely(p->root == NULL)) {
65  return p->action;
66  } else {
67  return p->action | p->root->action;
68  }
69 }
70 
71 /**
72  * \brief Initialize a packet structure for use.
73  */
75 {
78  if (unlikely(p->alerts.alerts == NULL)) {
79  return false;
80  }
81  p->livedev_id = 0;
82  p->livedev_dst_id = 0;
83  return true;
84 }
85 
87 {
88  FlowDeReference(&p->flow);
91 }
92 
93 /**
94  * \brief Recycle a packet structure for reuse.
95  */
97 {
98 /* clear the address structure by setting all fields to 0 */
99 #define CLEAR_ADDR(a) \
100  do { \
101  (a)->family = 0; \
102  (a)->addr_data32[0] = 0; \
103  (a)->addr_data32[1] = 0; \
104  (a)->addr_data32[2] = 0; \
105  (a)->addr_data32[3] = 0; \
106  } while (0)
107 
108  CLEAR_ADDR(&p->src);
109  CLEAR_ADDR(&p->dst);
110  p->sp = 0;
111  p->dp = 0;
112  p->proto = 0;
113  p->recursion_level = 0;
115  p->app_update_direction = 0;
116  p->sig_mask = 0;
117  p->pkt_hooks = 0;
118  const uint32_t pflags = p->flags;
119  p->flags = 0;
120  p->flowflags = 0;
121  p->pkt_src = 0;
122  p->tunnel_id = 0;
123  for (uint8_t i = 0; i < VLAN_MAX_LAYERS; i++) {
124  p->vlan_id[i] = 0;
125  }
126  p->vlan_idx = 0;
128  SCTIME_INIT(p->ts);
129  p->datalink = 0;
130  p->drop_reason = 0;
131 #define PACKET_RESET_ACTION(p) (p)->action = 0
133  if (p->pktvar != NULL) {
134  PktVarFree(p->pktvar);
135  p->pktvar = NULL;
136  }
137  PacketClearL2(p);
138  PacketClearL3(p);
139  PacketClearL4(p);
140  p->payload = NULL;
141  p->payload_len = 0;
142  p->BypassPacketsFlow = NULL;
143 #define RESET_PKT_LEN(p) ((p)->pktlen = 0)
144  RESET_PKT_LEN(p);
145  p->alerts.discarded = 0;
147  p->alerts.suppressed = 0;
149  if (p->alerts.cnt > 0) {
150  if (pflags & PKT_ALERT_CTX_USED)
152  p->alerts.cnt = 0;
153  }
154  p->tunnel_rtv_cnt = 0;
155  p->tunnel_tpr_cnt = 0;
156  p->events.cnt = 0;
158  p->next = NULL;
159  p->prev = NULL;
160  p->tunnel_verdicted = false;
161  p->root = NULL;
163  p->livedev_id = 0;
164  p->livedev_dst_id = 0;
166  p->tenant_id = 0;
167  p->nb_decoded_layers = 0;
168 }
169 
171 {
173  PacketReinit(p);
174 }
175 
176 /**
177  * \brief Cleanup a packet so that we can free it. No memset needed..
178  */
180 {
182  if (p->pktvar != NULL) {
183  PktVarFree(p->pktvar);
184  }
191 }
192 
194 {
196 }
197 
198 inline void SCPacketSetLiveDevice(Packet *p, LiveDevice *device)
199 {
200  p->livedev_id = LiveDeviceGetId(device);
201 }
202 
204 {
205  p->datalink = datalink;
206 }
207 
209 {
210  p->ts = ts;
211 }
212 
213 inline void SCPacketSetSource(Packet *p, enum PktSrcEnum source)
214 {
215  p->pkt_src = (uint8_t)source;
216 }
217 
218 /**
219  * \brief Get IP proto regardless of IP version.
220  *
221  * Exported wrapper around the PacketGetIPProto inline for plugins and Rust.
222  */
223 uint8_t SCPacketGetIPProto(const Packet *p)
224 {
225  return PacketGetIPProto(p);
226 }
227 
228 /**
229  * \brief Test whether the packet is marked as flowing to the server.
230  *
231  * Requires a valid packet. Returns false when the direction flag is not set.
232  */
234 {
235  return PKT_IS_TOSERVER(p);
236 }
237 
238 /**
239  * \brief Test whether the packet is marked as flowing to the client.
240  *
241  * Requires a valid packet. Returns false when the direction flag is not set.
242  */
244 {
245  return PKT_IS_TOCLIENT(p);
246 }
247 
248 /**
249  * \brief Get the type of the packet's L3 layer.
250  *
251  * \retval PACKET_L3_UNKNOWN The packet is NULL or has no decoded L3 layer.
252  */
254 {
255  return p == NULL ? PACKET_L3_UNKNOWN : p->l3.type;
256 }
257 
258 /**
259  * \brief Get the L3 packet data, starting at the L3 header.
260  *
261  * The returned data is borrowed from p and is valid only while its packet
262  * buffer remains valid and unchanged. For IPv4 and IPv6 the length comes
263  * from the IP header and includes the IPv6 base header. For ARP it is the
264  * fixed ARP header length. Trailing padding is excluded. The decoder has
265  * already validated the length against the captured packet length.
266  *
267  * \param p Packet to inspect, or NULL.
268  * \param len Non-NULL output pointer, set to zero when no data is returned.
269  * \retval NULL The packet is NULL or has no decoded L3 layer.
270  */
271 const uint8_t *SCPacketGetL3(const Packet *p, uint32_t *len)
272 {
273  *len = 0;
274  if (p == NULL)
275  return NULL;
276 
277  switch (p->l3.type) {
278  case PACKET_L3_IPV4: {
279  const IPV4Hdr *ip4h = PacketGetIPv4(p);
280  *len = IPV4_GET_RAW_IPLEN(ip4h);
281  return (const uint8_t *)ip4h;
282  }
283  case PACKET_L3_IPV6: {
284  const IPV6Hdr *ip6h = PacketGetIPv6(p);
286  return (const uint8_t *)ip6h;
287  }
288  case PACKET_L3_ARP:
289  *len = ARP_HEADER_LEN;
290  return (const uint8_t *)PacketGetARP(p);
291  case PACKET_L3_UNKNOWN:
292  break;
293  }
294  return NULL;
295 }
296 
297 /**
298  * \brief Get the flow associated with the packet.
299  */
301 {
302  return p == NULL ? NULL : p->flow;
303 }
304 
305 /**
306  * \brief Get packet timestamp as individual values.
307  */
308 void SCPacketGetTimeAsParts(const Packet *p, uint64_t *secs, uint64_t *usecs)
309 {
310  *secs = (uint64_t)SCTIME_SECS(p->ts);
311  *usecs = (uint64_t)SCTIME_USECS(p->ts);
312 }
PKT_IS_TOCLIENT
#define PKT_IS_TOCLIENT(p)
Definition: decode.h:257
PacketCheckAction
bool PacketCheckAction(const Packet *p, const uint8_t a)
Definition: packet.c:51
HostDeReference
#define HostDeReference(src_h_ptr)
Definition: host.h:124
host.h
SCPacketIsToServer
bool SCPacketIsToServer(const Packet *p)
Test whether the packet is marked as flowing to the server.
Definition: packet.c:233
SCPacketGetL3
const uint8_t * SCPacketGetL3(const Packet *p, uint32_t *len)
Get the L3 packet data, starting at the L3 header.
Definition: packet.c:271
Packet_::proto
uint8_t proto
Definition: decode.h:550
len
uint8_t len
Definition: app-layer-dnp3.h:2
ts
uint64_t ts
Definition: source-erf-file.c:68
PacketAlerts_::firewall_discarded
uint16_t firewall_discarded
Definition: decode.h:308
SCSpinDestroy
#define SCSpinDestroy
Definition: threads-debug.h:240
IPV6_GET_RAW_PLEN
#define IPV6_GET_RAW_PLEN(ip6h)
Definition: decode-ipv6.h:66
packet-bindgen.h
unlikely
#define unlikely(expr)
Definition: util-optimize.h:35
DECODE_TUNNEL_UNSET
@ DECODE_TUNNEL_UNSET
Definition: decode.h:39
Packet_::host_src
struct Host_ * host_src
Definition: decode.h:659
SCPacketSetLiveDevice
void SCPacketSetLiveDevice(Packet *p, LiveDevice *device)
Set a packets live device.
Definition: packet.c:198
Packet_::livedev_dst_id
uint16_t livedev_dst_id
Definition: decode.h:655
Packet_::payload
uint8_t * payload
Definition: decode.h:640
PacketAlerts_::cnt
uint16_t cnt
Definition: decode.h:306
action-globals.h
Packet_::flags
uint32_t flags
Definition: decode.h:582
PacketRecycle
void PacketRecycle(Packet *p)
Definition: packet.c:170
Packet_::action
uint8_t action
Definition: decode.h:644
PacketAlertCreate
PacketAlert * PacketAlertCreate(void)
Initialize PacketAlerts with dynamic alerts array size.
Definition: decode.c:144
Packet_::vlan_idx
uint8_t vlan_idx
Definition: decode.h:561
Flow_
Flow data structure.
Definition: flow.h:360
RESET_PKT_LEN
#define RESET_PKT_LEN(p)
Packet_::tunnel_verdicted
bool tunnel_verdicted
Definition: decode.h:683
LiveDevice_
Definition: util-device-private.h:32
PacketReleaseRefs
void PacketReleaseRefs(Packet *p)
Definition: packet.c:86
PacketAlerts_::alerts
PacketAlert * alerts
Definition: decode.h:310
rust.h
Packet_::sig_mask
SignatureMask sig_mask
Definition: decode.h:570
Packet_::tproto
uint8_t tproto
Definition: decode.h:579
PacketAlerts_::drop
PacketAlert drop
Definition: decode.h:313
ACTION_DROP_REJECT
#define ACTION_DROP_REJECT
Definition: action-globals.h:40
p
Packet * p
Definition: fuzz_dataset.c:30
AppLayerDecoderEventsResetEvents
void AppLayerDecoderEventsResetEvents(AppLayerDecoderEvents *events)
Definition: app-layer-events.c:130
Packet_::host_dst
struct Host_ * host_dst
Definition: decode.h:660
Packet_::flowflags
uint8_t flowflags
Definition: decode.h:564
Packet_::BypassPacketsFlow
int(* BypassPacketsFlow)(struct Packet_ *)
Definition: decode.h:629
PacketDropReason
PacketDropReason
Definition: decode.h:402
CLEAR_ADDR
#define CLEAR_ADDR(a)
Packet_::payload_len
uint16_t payload_len
Definition: decode.h:641
Packet_::alerts
PacketAlerts alerts
Definition: decode.h:657
Packet_::tunnel_tpr_cnt
uint16_t tunnel_tpr_cnt
Definition: decode.h:695
Packet_::app_layer_events
AppLayerDecoderEvents * app_layer_events
Definition: decode.h:665
Packet_::events
PacketEngineEvents events
Definition: decode.h:663
PacketAlertFree
void PacketAlertFree(PacketAlert *pa_array)
Definition: decode.c:176
PacketL3Types
PacketL3Types
Definition: packet-bindgen.h:24
SCPacketSetSource
void SCPacketSetSource(Packet *p, enum PktSrcEnum source)
Set packet source.
Definition: packet.c:213
Packet_::datalink
int datalink
Definition: decode.h:672
PacketReinit
void PacketReinit(Packet *p)
Recycle a packet structure for reuse.
Definition: packet.c:96
Packet_::pktvar
PktVar * pktvar
Definition: decode.h:632
PKT_IS_TOSERVER
#define PKT_IS_TOSERVER(p)
Definition: decode.h:256
Packet_::tunnel_lock
SCSpinlock tunnel_lock
Definition: decode.h:716
Packet_::ts
SCTime_t ts
Definition: decode.h:590
SCTIME_INIT
#define SCTIME_INIT(t)
Definition: util-time.h:45
PktSrcEnum
PktSrcEnum
Definition: decode.h:68
PKT_DROP_REASON_NOT_SET
@ PKT_DROP_REASON_NOT_SET
Definition: decode.h:403
Packet_::prev
struct Packet_ * prev
Definition: decode.h:669
PacketDestructor
void PacketDestructor(Packet *p)
Cleanup a packet so that we can free it. No memset needed..
Definition: packet.c:179
Packet_::tunnel_rtv_cnt
uint16_t tunnel_rtv_cnt
Definition: decode.h:693
SCPacketGetIPProto
uint8_t SCPacketGetIPProto(const Packet *p)
Get IP proto regardless of IP version.
Definition: packet.c:223
pkt-var.h
Packet_::sp
Port sp
Definition: decode.h:535
Packet_::tunnel_id
uint16_t tunnel_id
Definition: decode.h:558
PacketAlerts_::discarded
uint16_t discarded
Definition: decode.h:307
Packet_::pkt_hooks
uint16_t pkt_hooks
Definition: decode.h:573
PACKET_L3_UNKNOWN
@ PACKET_L3_UNKNOWN
Definition: packet-bindgen.h:25
PACKET_RESET_ACTION
#define PACKET_RESET_ACTION(p)
util-profiling.h
PKT_ALERT_CTX_USED
#define PKT_ALERT_CTX_USED
Definition: decode.h:1309
IPV6Hdr_
Definition: decode-ipv6.h:32
ACTION_ALERT
#define ACTION_ALERT
Definition: action-globals.h:29
Packet_
Definition: decode.h:528
LiveDeviceGetId
uint16_t LiveDeviceGetId(const LiveDevice *dev)
Definition: util-device.c:452
Packet_::ttype
uint8_t ttype
Definition: decode.h:576
SCPacketSetTime
void SCPacketSetTime(Packet *p, SCTime_t ts)
Set the timestamp for a packet.
Definition: packet.c:208
SCTime_t
Definition: util-time.h:40
SCPacketGetTimeAsParts
void SCPacketGetTimeAsParts(const Packet *p, uint64_t *secs, uint64_t *usecs)
Get packet timestamp as individual values.
Definition: packet.c:308
PacketTunnelNone
@ PacketTunnelNone
Definition: decode.h:436
PACKET_L3_IPV4
@ PACKET_L3_IPV4
Definition: packet-bindgen.h:26
IPV4Hdr_
Definition: decode-ipv4.h:72
PACKET_L3_IPV6
@ PACKET_L3_IPV6
Definition: packet-bindgen.h:27
Packet_::nb_decoded_layers
uint8_t nb_decoded_layers
Definition: decode.h:677
SCPacketIsToClient
bool SCPacketIsToClient(const Packet *p)
Test whether the packet is marked as flowing to the client.
Definition: packet.c:243
PACKET_FREE_EXTDATA
#define PACKET_FREE_EXTDATA(p)
Definition: decode.h:1130
PacketL3::type
enum PacketL3Types type
Definition: decode.h:457
Packet_::ReleasePacket
void(* ReleasePacket)(struct Packet_ *)
Definition: decode.h:626
Packet_::tenant_id
uint32_t tenant_id
Definition: decode.h:698
Packet_::flow
struct Flow_ * flow
Definition: decode.h:584
PacketAlertDropReset
void PacketAlertDropReset(Packet *p)
Release the alert context the drop slot owns, then invalidate the slot. Not an alerts[] entry,...
Definition: decode.c:170
PKT_DROP_REASON_INNER_PACKET
@ PKT_DROP_REASON_INNER_PACKET
Definition: decode.h:420
packet.h
Packet_::livedev_id
uint16_t livedev_id
Definition: decode.h:653
SCPacketGetFlow
Flow * SCPacketGetFlow(const Packet *p)
Get the flow associated with the packet.
Definition: packet.c:300
SCTIME_SECS
#define SCTIME_SECS(t)
Definition: util-time.h:57
Packet_::app_update_direction
uint8_t app_update_direction
Definition: decode.h:567
SCPacketSetReleasePacket
void SCPacketSetReleasePacket(Packet *p, void(*ReleasePacket)(Packet *p))
Set a packet release function.
Definition: packet.c:193
PacketGetAction
uint8_t PacketGetAction(const Packet *p)
Definition: packet.c:62
VLAN_MAX_LAYERS
#define VLAN_MAX_LAYERS
Definition: decode-vlan.h:51
SCAppLayerDecoderEventsFreeEvents
void SCAppLayerDecoderEventsFreeEvents(AppLayerDecoderEvents **events)
Definition: app-layer-events.c:138
Packet_::persistent
struct Packet_::@38 persistent
app-layer-events.h
util-validate.h
PACKET_PROFILING_RESET
#define PACKET_PROFILING_RESET(p)
Definition: util-profiling.h:161
SCSpinInit
#define SCSpinInit
Definition: threads-debug.h:239
Packet_::next
struct Packet_ * next
Definition: decode.h:668
PACKET_L3_ARP
@ PACKET_L3_ARP
Definition: packet-bindgen.h:28
Packet_::l3
struct PacketL3 l3
Definition: decode.h:635
Packet_::root
struct Packet_ * root
Definition: decode.h:686
PacketAlerts_::suppressed
uint16_t suppressed
Definition: decode.h:309
Packet_::pkt_src
uint8_t pkt_src
Definition: decode.h:646
Packet_::recursion_level
uint8_t recursion_level
Definition: decode.h:553
PktVarFree
void PktVarFree(PktVar *pv)
Definition: pkt-var.c:111
PacketDrop
void PacketDrop(Packet *p, const uint8_t action, enum PacketDropReason r)
issue drop action
Definition: packet.c:35
Packet_::drop_reason
uint8_t drop_reason
Definition: decode.h:680
Packet_::dst
Address dst
Definition: decode.h:533
SCPacketSetDatalink
void SCPacketSetDatalink(Packet *p, int datalink)
Set a packets data link type.
Definition: packet.c:203
PacketAlertRecycle
void PacketAlertRecycle(PacketAlert *pa_array, uint16_t cnt)
Definition: decode.c:149
IPV6_HEADER_LEN
#define IPV6_HEADER_LEN
Definition: decode-ipv6.h:27
IPV4_GET_RAW_IPLEN
#define IPV4_GET_RAW_IPLEN(ip4h)
Definition: decode-ipv4.h:98
Packet_::vlan_id
uint16_t vlan_id[VLAN_MAX_LAYERS]
Definition: decode.h:560
likely
#define likely(expr)
Definition: util-optimize.h:32
ARP_HEADER_LEN
#define ARP_HEADER_LEN
Definition: decode-arp.h:27
PacketInit
bool PacketInit(Packet *p)
Initialize a packet structure for use.
Definition: packet.c:74
flow.h
Packet_::dp
Port dp
Definition: decode.h:543
DEBUG_VALIDATE_BUG_ON
#define DEBUG_VALIDATE_BUG_ON(exp)
Definition: util-validate.h:109
Packet_::src
Address src
Definition: decode.h:532
SCPacketGetL3Type
enum PacketL3Types SCPacketGetL3Type(const Packet *p)
Get the type of the packet's L3 layer.
Definition: packet.c:253
SCTIME_USECS
#define SCTIME_USECS(t)
Definition: util-time.h:56
PacketEngineEvents_::cnt
uint8_t cnt
Definition: decode.h:329