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