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_id = 0;
78  p->livedev_dst_id = 0;
79 }
80 
82 {
83  FlowDeReference(&p->flow);
86 }
87 
88 /**
89  * \brief Recycle a packet structure for reuse.
90  */
92 {
93 /* clear the address structure by setting all fields to 0 */
94 #define CLEAR_ADDR(a) \
95  do { \
96  (a)->family = 0; \
97  (a)->addr_data32[0] = 0; \
98  (a)->addr_data32[1] = 0; \
99  (a)->addr_data32[2] = 0; \
100  (a)->addr_data32[3] = 0; \
101  } while (0)
102 
103  CLEAR_ADDR(&p->src);
104  CLEAR_ADDR(&p->dst);
105  p->sp = 0;
106  p->dp = 0;
107  p->proto = 0;
108  p->recursion_level = 0;
110  p->app_update_direction = 0;
111  p->sig_mask = 0;
112  p->pkt_hooks = 0;
113  const uint32_t pflags = p->flags;
114  p->flags = 0;
115  p->flowflags = 0;
116  p->pkt_src = 0;
117  p->vlan_id[0] = 0;
118  p->vlan_id[1] = 0;
119  p->vlan_idx = 0;
120  p->ttype = PacketTunnelNone;
121  SCTIME_INIT(p->ts);
122  p->datalink = 0;
123  p->drop_reason = 0;
124 #define PACKET_RESET_ACTION(p) (p)->action = 0
126  if (p->pktvar != NULL) {
127  PktVarFree(p->pktvar);
128  p->pktvar = NULL;
129  }
130  PacketClearL2(p);
131  PacketClearL3(p);
132  PacketClearL4(p);
133  p->payload = NULL;
134  p->payload_len = 0;
135  p->BypassPacketsFlow = NULL;
136 #define RESET_PKT_LEN(p) ((p)->pktlen = 0)
137  RESET_PKT_LEN(p);
138  p->alerts.discarded = 0;
139  p->alerts.suppressed = 0;
140  p->alerts.drop.action = 0;
141  if (p->alerts.cnt > 0) {
142  if (pflags & PKT_ALERT_CTX_USED)
144  p->alerts.cnt = 0;
145  }
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_id = 0;
155  p->livedev_dst_id = 0;
157  p->tenant_id = 0;
158  p->nb_decoded_layers = 0;
159 }
160 
162 {
164  PacketReinit(p);
165 }
166 
167 /**
168  * \brief Cleanup a packet so that we can free it. No memset needed..
169  */
171 {
173  if (p->pktvar != NULL) {
174  PktVarFree(p->pktvar);
175  }
181 }
182 
184 {
186 }
187 
188 inline void SCPacketSetLiveDevice(Packet *p, LiveDevice *device)
189 {
190  p->livedev_id = LiveDeviceGetId(device);
191 }
192 
193 inline void SCPacketSetDatalink(Packet *p, int datalink)
194 {
195  p->datalink = datalink;
196 }
197 
199 {
200  p->ts = ts;
201 }
202 
203 inline void SCPacketSetSource(Packet *p, enum PktSrcEnum source)
204 {
205  p->pkt_src = (uint8_t)source;
206 }
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:527
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:628
SCPacketSetLiveDevice
void SCPacketSetLiveDevice(Packet *p, LiveDevice *device)
Set a packets live device.
Definition: packet.c:188
Packet_::livedev_dst_id
uint16_t livedev_dst_id
Definition: decode.h:624
Packet_::payload
uint8_t * payload
Definition: decode.h:609
PacketAlerts_::cnt
uint16_t cnt
Definition: decode.h:288
action-globals.h
Packet_::flags
uint32_t flags
Definition: decode.h:551
PacketRecycle
void PacketRecycle(Packet *p)
Definition: packet.c:161
Packet_::action
uint8_t action
Definition: decode.h:613
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:533
RESET_PKT_LEN
#define RESET_PKT_LEN(p)
Packet_::tunnel_verdicted
bool tunnel_verdicted
Definition: decode.h:652
LiveDevice_
Definition: util-device-private.h:32
PacketReleaseRefs
void PacketReleaseRefs(Packet *p)
Definition: packet.c:81
PacketAlerts_::alerts
PacketAlert * alerts
Definition: decode.h:291
rust.h
Packet_::sig_mask
SignatureMask sig_mask
Definition: decode.h:542
PacketAlerts_::drop
PacketAlert drop
Definition: decode.h:294
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:629
Packet_::flowflags
uint8_t flowflags
Definition: decode.h:536
Packet_::BypassPacketsFlow
int(* BypassPacketsFlow)(struct Packet_ *)
Definition: decode.h:598
PacketDropReason
PacketDropReason
Definition: decode.h:381
CLEAR_ADDR
#define CLEAR_ADDR(a)
Packet_::payload_len
uint16_t payload_len
Definition: decode.h:610
Packet_::alerts
PacketAlerts alerts
Definition: decode.h:626
Packet_::tunnel_tpr_cnt
uint16_t tunnel_tpr_cnt
Definition: decode.h:664
Packet_::app_layer_events
AppLayerDecoderEvents * app_layer_events
Definition: decode.h:634
Packet_::events
PacketEngineEvents events
Definition: decode.h:632
PacketAlertFree
void PacketAlertFree(PacketAlert *pa_array)
Definition: decode.c:169
SCPacketSetSource
void SCPacketSetSource(Packet *p, enum PktSrcEnum source)
Set packet source.
Definition: packet.c:203
PacketAlert_::action
uint8_t action
Definition: decode.h:251
Packet_::datalink
int datalink
Definition: decode.h:641
PacketReinit
void PacketReinit(Packet *p)
Recycle a packet structure for reuse.
Definition: packet.c:91
Packet_::pktvar
PktVar * pktvar
Definition: decode.h:601
Packet_::tunnel_lock
SCSpinlock tunnel_lock
Definition: decode.h:685
Packet_::ts
SCTime_t ts
Definition: decode.h:559
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:382
Packet_::prev
struct Packet_ * prev
Definition: decode.h:638
PacketDestructor
void PacketDestructor(Packet *p)
Cleanup a packet so that we can free it. No memset needed..
Definition: packet.c:170
Packet_::tunnel_rtv_cnt
uint16_t tunnel_rtv_cnt
Definition: decode.h:662
pkt-var.h
Packet_::sp
Port sp
Definition: decode.h:512
PacketAlerts_::discarded
uint16_t discarded
Definition: decode.h:289
Packet_::pkt_hooks
uint16_t pkt_hooks
Definition: decode.h:545
PACKET_RESET_ACTION
#define PACKET_RESET_ACTION(p)
util-profiling.h
PKT_ALERT_CTX_USED
#define PKT_ALERT_CTX_USED
Definition: decode.h:1284
ACTION_ALERT
#define ACTION_ALERT
Definition: action-globals.h:29
Packet_
Definition: decode.h:505
LiveDeviceGetId
uint16_t LiveDeviceGetId(const LiveDevice *dev)
Definition: util-device.c:452
Packet_::ttype
uint8_t ttype
Definition: decode.h:548
SCPacketSetTime
void SCPacketSetTime(Packet *p, SCTime_t ts)
Set the timestamp for a packet.
Definition: packet.c:198
SCTime_t
Definition: util-time.h:40
PacketTunnelNone
@ PacketTunnelNone
Definition: decode.h:407
Packet_::nb_decoded_layers
uint8_t nb_decoded_layers
Definition: decode.h:646
PACKET_FREE_EXTDATA
#define PACKET_FREE_EXTDATA(p)
Definition: decode.h:1094
Packet_::ReleasePacket
void(* ReleasePacket)(struct Packet_ *)
Definition: decode.h:595
Packet_::tenant_id
uint32_t tenant_id
Definition: decode.h:667
Packet_::flow
struct Flow_ * flow
Definition: decode.h:553
PKT_DROP_REASON_INNER_PACKET
@ PKT_DROP_REASON_INNER_PACKET
Definition: decode.h:398
packet.h
Packet_::livedev_id
uint16_t livedev_id
Definition: decode.h:622
Packet_::app_update_direction
uint8_t app_update_direction
Definition: decode.h:539
SCPacketSetReleasePacket
void SCPacketSetReleasePacket(Packet *p, void(*ReleasePacket)(Packet *p))
Set a packet release function.
Definition: packet.c:183
PacketGetAction
uint8_t PacketGetAction(const Packet *p)
Definition: packet.c:61
SCAppLayerDecoderEventsFreeEvents
void SCAppLayerDecoderEventsFreeEvents(AppLayerDecoderEvents **events)
Definition: app-layer-events.c:137
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:637
Packet_::root
struct Packet_ * root
Definition: decode.h:655
PacketAlerts_::suppressed
uint16_t suppressed
Definition: decode.h:290
Packet_::pkt_src
uint8_t pkt_src
Definition: decode.h:615
Packet_::recursion_level
uint8_t recursion_level
Definition: decode.h:530
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:649
Packet_::dst
Address dst
Definition: decode.h:510
SCPacketSetDatalink
void SCPacketSetDatalink(Packet *p, int datalink)
Set a packets data link type.
Definition: packet.c:193
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:532
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:520
DEBUG_VALIDATE_BUG_ON
#define DEBUG_VALIDATE_BUG_ON(exp)
Definition: util-validate.h:109
Packet_::src
Address src
Definition: decode.h:509
PacketEngineEvents_::cnt
uint8_t cnt
Definition: decode.h:308