suricata
defrag.h
Go to the documentation of this file.
1 /* Copyright (C) 2007-2013 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 /**
19  * \file
20  *
21  * \author Endace Technology Limited, Jason Ish <jason.ish@endace.com>
22  */
23 
24 #ifndef SURICATA_DEFRAG_H
25 #define SURICATA_DEFRAG_H
26 
27 #include "tree.h"
28 #include "threads.h"
29 #include "util-pool.h"
30 #include "threadvars.h"
31 #include "decode.h"
32 
33 /**
34  * A context for an instance of a fragmentation re-assembler, in case
35  * we ever need more than one.
36  */
37 typedef struct DefragContext_ {
38  Pool *frag_pool; /**< Pool of fragments. */
40 
41  uint32_t timeout; /**< Default timeout. */
43 
44 /**
45  * Storage for an individual fragment.
46  */
47 typedef struct Frag_ {
48  uint16_t offset; /**< The offset of this fragment, already
49  * multiplied by 8. */
50 
51  uint8_t hlen; /**< The length of this fragments IP header. */
52 
53  uint8_t more_frags:4; /**< More frags? */
54  uint8_t skip:4; /**< Skip this fragment during re-assembly. */
55 
56  uint32_t len; /**< The length of this fragment. */
57 
58  uint16_t frag_hdr_offset; /**< Offset in the packet where the frag
59  * header starts. */
60 
61  uint16_t data_offset; /**< Offset to the packet data. */
62  uint16_t data_len; /**< Length of data. */
63 
64  uint16_t ltrim; /**< Number of leading bytes to trim when
65  * re-assembling the packet. */
66 
67  uint8_t *pkt; /**< The actual packet. */
68 
69 #ifdef DEBUG
70  uint64_t pcap_cnt; /**< pcap_cnt of original packet */
71 #endif
72 
74 } Frag;
75 
76 int DefragRbFragCompare(struct Frag_ *a, struct Frag_ *b);
77 
78 RB_HEAD(IP_FRAGMENTS, Frag_);
80 
81 /**
82  * A defragmentation tracker. Used to track fragments that make up a
83  * single packet.
84  */
85 typedef struct DefragTracker_ {
86  SCMutex lock; /**< Mutex for locking list operations on
87  * this tracker. */
88 
89  uint16_t vlan_id[VLAN_MAX_LAYERS]; /**< VLAN ID tracker applies to. */
90  uint16_t ip_hdr_offset; /**< Offset in the packet where the IP
91  * header starts. */
92 
93  uint32_t id; /**< IP ID for this tracker. 32 bits for IPv6, 16
94  * for IPv4. */
95 
96  uint8_t proto; /**< IP protocol for this tracker. */
97 
98  uint8_t policy; /**< Reassembly policy this tracker will use. */
99 
100  uint8_t af; /**< Address family for this tracker, AF_INET or
101  * AF_INET6. */
102 
103  uint8_t seen_last; /**< Has this tracker seen the last fragment? */
104 
105  uint8_t remove; /**< remove */
106 
107  Address src_addr; /**< Source address for this tracker. */
108  Address dst_addr; /**< Destination address for this tracker. */
109 
110  int datalink; /**< datalink for reassembled packet, set by first fragment */
111  SCTime_t timeout; /**< When this tracker will timeout. */
112  uint32_t host_timeout; /**< Host timeout, statically assigned from the yaml */
113 
114  /** use cnt, reference counter */
115  SC_ATOMIC_DECLARE(unsigned int, use_cnt);
116 
117  struct IP_FRAGMENTS fragment_tree;
118 
119  /** hash pointer, protected by hash row mutex/spin */
121 
122  /** stack pointer, protected by tracker-queue mutex/spin */
125 
126 void DefragInit(void);
127 void DefragDestroy(void);
128 
129 uint8_t DefragGetOsPolicy(Packet *);
132 void DefragRegisterTests(void);
133 
134 #endif /* SURICATA_DEFRAG_H */
Frag_
Definition: defrag.h:47
DefragGetOsPolicy
uint8_t DefragGetOsPolicy(Packet *)
Get the defrag policy based on the destination address of the packet.
Definition: defrag.c:962
Defrag
Packet * Defrag(ThreadVars *, DecodeThreadVars *, Packet *)
Entry point for IPv4 and IPv6 fragments.
Definition: defrag.c:1040
DefragTracker_::hnext
struct DefragTracker_ * hnext
Definition: defrag.h:120
RB_PROTOTYPE
RB_PROTOTYPE(IP_FRAGMENTS, Frag_, rb, DefragRbFragCompare)
threads.h
DefragTracker_::ip_hdr_offset
uint16_t ip_hdr_offset
Definition: defrag.h:90
DefragTrackerFreeFrags
void DefragTrackerFreeFrags(DefragTracker *)
Free all frags associated with a tracker.
Definition: defrag.c:118
Frag_::data_len
uint16_t data_len
Definition: defrag.h:62
DefragContext_::frag_pool
Pool * frag_pool
Definition: defrag.h:38
DefragTracker_::host_timeout
uint32_t host_timeout
Definition: defrag.h:112
Address_
Definition: decode.h:113
Pool_
Definition: util-pool.h:43
DefragTracker_::SC_ATOMIC_DECLARE
SC_ATOMIC_DECLARE(unsigned int, use_cnt)
DefragTracker_::lock
SCMutex lock
Definition: defrag.h:86
DefragContext_::frag_pool_lock
SCMutex frag_pool_lock
Definition: defrag.h:39
DefragTracker_::remove
uint8_t remove
Definition: defrag.h:105
decode.h
DefragRegisterTests
void DefragRegisterTests(void)
Definition: defrag.c:3259
DefragTracker_::seen_last
uint8_t seen_last
Definition: defrag.h:103
DefragTracker_::policy
uint8_t policy
Definition: defrag.h:98
DefragTracker_
Definition: defrag.h:85
DefragTracker_::vlan_id
uint16_t vlan_id[VLAN_MAX_LAYERS]
Definition: defrag.h:89
ThreadVars_
Per thread variable structure.
Definition: threadvars.h:57
DefragContext_::timeout
uint32_t timeout
Definition: defrag.h:41
DefragTracker_::dst_addr
Address dst_addr
Definition: defrag.h:108
Packet_
Definition: decode.h:516
DefragTracker_::id
uint32_t id
Definition: defrag.h:93
DefragTracker_::src_addr
Address src_addr
Definition: defrag.h:107
Frag_::skip
uint8_t skip
Definition: defrag.h:54
SCTime_t
Definition: util-time.h:40
Frag_::pkt
uint8_t * pkt
Definition: defrag.h:67
DefragInit
void DefragInit(void)
Definition: defrag.c:1115
Frag_::data_offset
uint16_t data_offset
Definition: defrag.h:61
DefragTracker_::proto
uint8_t proto
Definition: defrag.h:96
DefragContext_
Definition: defrag.h:37
DefragContext
struct DefragContext_ DefragContext
Frag_::ltrim
uint16_t ltrim
Definition: defrag.h:64
Frag_::RB_ENTRY
RB_ENTRY(Frag_) rb
tree.h
DefragTracker_::fragment_tree
struct IP_FRAGMENTS fragment_tree
Definition: defrag.h:117
Frag_::len
uint32_t len
Definition: defrag.h:56
DefragTracker_::timeout
SCTime_t timeout
Definition: defrag.h:111
VLAN_MAX_LAYERS
#define VLAN_MAX_LAYERS
Definition: decode-vlan.h:51
Frag_::frag_hdr_offset
uint16_t frag_hdr_offset
Definition: defrag.h:58
threadvars.h
DecodeThreadVars_
Structure to hold thread specific data for all decode modules.
Definition: decode.h:995
Frag_::more_frags
uint8_t more_frags
Definition: defrag.h:53
DefragTracker_::af
uint8_t af
Definition: defrag.h:100
DefragTracker_::lnext
struct DefragTracker_ * lnext
Definition: defrag.h:123
Frag
struct Frag_ Frag
DefragTracker_::datalink
int datalink
Definition: defrag.h:110
Frag_::hlen
uint8_t hlen
Definition: defrag.h:51
DefragRbFragCompare
int DefragRbFragCompare(struct Frag_ *a, struct Frag_ *b)
Definition: defrag.c:532
DefragTracker
struct DefragTracker_ DefragTracker
util-pool.h
SCMutex
#define SCMutex
Definition: threads-debug.h:114
Frag_::offset
uint16_t offset
Definition: defrag.h:48
DefragDestroy
void DefragDestroy(void)
Definition: defrag.c:1135
RB_HEAD
RB_HEAD(IP_FRAGMENTS, Frag_)