suricata
packet.c
Go to the documentation of this file.
1/* Copyright (C) 2007-2022 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 "pkt-var.h"
20#include "flow.h"
21#include "host.h"
22#include "util-profiling.h"
23#include "util-validate.h"
24#include "action-globals.h"
25#include "app-layer-events.h"
26
27/** \brief issue drop action
28 *
29 * Set drop (+reject) flags in both current and root packet.
30 *
31 * \param action action bit flags. Must be limited to ACTION_DROP_REJECT|ACTION_ALERT
32 */
33void PacketDrop(Packet *p, const uint8_t action, enum PacketDropReason r)
34{
36
38 p->drop_reason = (uint8_t)r;
39
40 if (p->root) {
41 p->root->action |= action;
44 }
45 }
46 p->action |= action;
47}
48
49bool PacketCheckAction(const Packet *p, const uint8_t a)
50{
51 if (likely(p->root == NULL)) {
52 return (p->action & a) != 0;
53 } else {
54 /* check against both */
55 const uint8_t actions = p->action | p->root->action;
56 return (actions & a) != 0;
57 }
58}
59
60/**
61 * \brief Initialize a packet structure for use.
62 */
64{
67 p->livedev = NULL;
68}
69
71{
72 FlowDeReference(&p->flow);
75}
76
77/**
78 * \brief Recycle a packet structure for reuse.
79 */
81{
82/* clear the address structure by setting all fields to 0 */
83#define CLEAR_ADDR(a) \
84 do { \
85 (a)->family = 0; \
86 (a)->addr_data32[0] = 0; \
87 (a)->addr_data32[1] = 0; \
88 (a)->addr_data32[2] = 0; \
89 (a)->addr_data32[3] = 0; \
90 } while (0)
91
92 CLEAR_ADDR(&p->src);
93 CLEAR_ADDR(&p->dst);
94 p->sp = 0;
95 p->dp = 0;
96 p->proto = 0;
97 p->recursion_level = 0;
100 p->sig_mask = 0;
101 p->pkt_hooks = 0;
102 const uint32_t pflags = p->flags;
103 p->flags = 0;
104 p->flowflags = 0;
105 p->pkt_src = 0;
106 p->vlan_id[0] = 0;
107 p->vlan_id[1] = 0;
108 p->vlan_idx = 0;
110 SCTIME_INIT(p->ts);
111 p->datalink = 0;
112 p->drop_reason = 0;
113#define PACKET_RESET_ACTION(p) (p)->action = 0
115 if (p->pktvar != NULL) {
116 PktVarFree(p->pktvar);
117 p->pktvar = NULL;
118 }
119 PacketClearL2(p);
120 PacketClearL3(p);
121 PacketClearL4(p);
122 p->payload = NULL;
123 p->payload_len = 0;
124 p->BypassPacketsFlow = NULL;
125#define RESET_PKT_LEN(p) ((p)->pktlen = 0)
126 RESET_PKT_LEN(p);
127 p->alerts.discarded = 0;
128 p->alerts.suppressed = 0;
129 p->alerts.drop.action = 0;
130 if (p->alerts.cnt > 0) {
131 if (pflags & PKT_ALERT_CTX_USED)
133 p->alerts.cnt = 0;
134 }
135 p->pcap_cnt = 0;
136 p->tunnel_rtv_cnt = 0;
137 p->tunnel_tpr_cnt = 0;
138 p->events.cnt = 0;
140 p->next = NULL;
141 p->prev = NULL;
142 p->tunnel_verdicted = false;
143 p->root = NULL;
144 p->livedev = NULL;
146 p->tenant_id = 0;
147 p->nb_decoded_layers = 0;
148}
149
151{
153 PacketReinit(p);
154}
155
156/**
157 * \brief Cleanup a packet so that we can free it. No memset needed..
158 */
171
172inline void SCPacketSetReleasePacket(Packet *p, void (*ReleasePacket)(Packet *p))
173{
174 p->ReleasePacket = ReleasePacket;
175}
176
178{
179 p->livedev = device;
180}
181
182inline void SCPacketSetDatalink(Packet *p, int datalink)
183{
184 p->datalink = datalink;
185}
186
188{
189 p->ts = ts;
190}
191
192inline void SCPacketSetSource(Packet *p, enum PktSrcEnum source)
193{
194 p->pkt_src = (uint8_t)source;
195}
#define ACTION_DROP_REJECT
#define ACTION_ALERT
void AppLayerDecoderEventsFreeEvents(AppLayerDecoderEvents **events)
void AppLayerDecoderEventsResetEvents(AppLayerDecoderEvents *events)
PacketDropReason
Definition decode.h:380
@ PKT_DROP_REASON_INNER_PACKET
Definition decode.h:397
@ PKT_DROP_REASON_NOT_SET
Definition decode.h:381
#define PACKET_FREE_EXTDATA(p)
Definition decode.h:1068
#define PKT_ALERT_CTX_USED
Definition decode.h:1255
@ PacketTunnelNone
Definition decode.h:406
PktSrcEnum
Definition decode.h:51
PacketAlert * PacketAlertCreate(void)
Initialize PacketAlerts with dynamic alerts array size.
Definition decode.c:140
void PacketAlertRecycle(PacketAlert *pa_array, uint16_t cnt)
Definition decode.c:148
void PacketAlertFree(PacketAlert *pa_array)
Definition decode.c:165
#define HostDeReference(src_h_ptr)
Definition host.h:124
void PacketReleaseRefs(Packet *p)
Definition packet.c:70
#define PACKET_RESET_ACTION(p)
#define RESET_PKT_LEN(p)
void SCPacketSetLiveDevice(Packet *p, LiveDevice *device)
Set a packets live device.
Definition packet.c:177
void SCPacketSetTime(Packet *p, SCTime_t ts)
Set the timestamp for a packet.
Definition packet.c:187
#define CLEAR_ADDR(a)
void SCPacketSetReleasePacket(Packet *p, void(*ReleasePacket)(Packet *p))
Set a packet release function.
Definition packet.c:172
void PacketReinit(Packet *p)
Recycle a packet structure for reuse.
Definition packet.c:80
void PacketDrop(Packet *p, const uint8_t action, enum PacketDropReason r)
issue drop action
Definition packet.c:33
void SCPacketSetSource(Packet *p, enum PktSrcEnum source)
Set packet source.
Definition packet.c:192
void SCPacketSetDatalink(Packet *p, int datalink)
Set a packets data link type.
Definition packet.c:182
void PacketDestructor(Packet *p)
Cleanup a packet so that we can free it. No memset needed..
Definition packet.c:159
void PacketInit(Packet *p)
Initialize a packet structure for use.
Definition packet.c:63
bool PacketCheckAction(const Packet *p, const uint8_t a)
Definition packet.c:49
void PacketRecycle(Packet *p)
Definition packet.c:150
void PktVarFree(PktVar *pv)
Definition pkt-var.c:111
uint64_t ts
uint8_t action
Definition decode.h:250
uint16_t suppressed
Definition decode.h:289
uint16_t discarded
Definition decode.h:288
uint16_t cnt
Definition decode.h:287
PacketAlert * alerts
Definition decode.h:290
PacketAlert drop
Definition decode.h:293
uint32_t tenant_id
Definition decode.h:665
uint8_t flowflags
Definition decode.h:532
uint64_t pcap_cnt
Definition decode.h:626
SCTime_t ts
Definition decode.h:555
uint8_t action
Definition decode.h:609
uint8_t pkt_src
Definition decode.h:611
Address src
Definition decode.h:505
Port sp
Definition decode.h:508
uint8_t nb_decoded_layers
Definition decode.h:644
SignatureMask sig_mask
Definition decode.h:538
uint8_t drop_reason
Definition decode.h:647
SCSpinlock tunnel_lock
Definition decode.h:683
uint8_t app_update_direction
Definition decode.h:535
uint16_t tunnel_rtv_cnt
Definition decode.h:660
uint8_t recursion_level
Definition decode.h:526
PktVar * pktvar
Definition decode.h:597
PacketAlerts alerts
Definition decode.h:620
enum PacketTunnelType ttype
Definition decode.h:553
struct Flow_ * flow
Definition decode.h:546
AppLayerDecoderEvents * app_layer_events
Definition decode.h:632
struct Packet_::@39 persistent
PacketEngineEvents events
Definition decode.h:630
struct Host_ * host_dst
Definition decode.h:623
uint16_t vlan_id[VLAN_MAX_LAYERS]
Definition decode.h:528
uint8_t * payload
Definition decode.h:605
int datalink
Definition decode.h:639
bool tunnel_verdicted
Definition decode.h:650
struct Packet_ * root
Definition decode.h:653
uint16_t pkt_hooks
Definition decode.h:541
uint16_t payload_len
Definition decode.h:606
struct Packet_ * next
Definition decode.h:635
struct LiveDevice_ * livedev
Definition decode.h:618
void(* ReleasePacket)(struct Packet_ *)
Definition decode.h:591
uint32_t flags
Definition decode.h:544
uint16_t tunnel_tpr_cnt
Definition decode.h:662
uint8_t vlan_idx
Definition decode.h:529
Address dst
Definition decode.h:506
struct Packet_ * prev
Definition decode.h:636
struct Host_ * host_src
Definition decode.h:622
uint8_t proto
Definition decode.h:523
int(* BypassPacketsFlow)(struct Packet_ *)
Definition decode.h:594
Port dp
Definition decode.h:516
#define SCSpinInit
#define SCSpinDestroy
#define likely(expr)
#define PACKET_PROFILING_RESET(p)
#define SCTIME_INIT(t)
Definition util-time.h:45
#define DEBUG_VALIDATE_BUG_ON(exp)