suricata
defrag-hash.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 "suricata-common.h"
19 #include "conf.h"
20 #include "defrag-hash.h"
21 #include "defrag-stack.h"
22 #include "defrag-config.h"
23 #include "defrag-timeout.h"
24 #include "util-random.h"
25 #include "util-byte.h"
26 #include "util-misc.h"
27 #include "util-hash-lookup3.h"
28 #include "util-validate.h"
29 
30 /** defrag tracker hash table */
33 SC_ATOMIC_DECLARE(uint64_t,defrag_memuse);
34 SC_ATOMIC_DECLARE(unsigned int,defragtracker_counter);
35 SC_ATOMIC_DECLARE(unsigned int,defragtracker_prune_idx);
36 
37 static DefragTracker *DefragTrackerGetUsedDefragTracker(
39 
40 /** queue with spare tracker */
41 static DefragTrackerStack defragtracker_spare_q;
42 
43 /**
44  * \brief Update memcap value
45  *
46  * \param size new memcap value
47  */
48 int DefragTrackerSetMemcap(uint64_t size)
49 {
50  if ((uint64_t)SC_ATOMIC_GET(defrag_memuse) < size) {
51  SC_ATOMIC_SET(defrag_config.memcap, size);
52  return 1;
53  }
54 
55  return 0;
56 }
57 
58 /**
59  * \brief Return memcap value
60  *
61  * \retval memcap value
62  */
63 uint64_t DefragTrackerGetMemcap(void)
64 {
65  uint64_t memcapcopy = SC_ATOMIC_GET(defrag_config.memcap);
66  return memcapcopy;
67 }
68 
69 /**
70  * \brief Return memuse value
71  *
72  * \retval memuse value
73  */
74 uint64_t DefragTrackerGetMemuse(void)
75 {
76  uint64_t memusecopy = (uint64_t)SC_ATOMIC_GET(defrag_memuse);
77  return memusecopy;
78 }
79 
81 {
83 }
84 
86 {
87  DefragTrackerEnqueue(&defragtracker_spare_q, h);
88  (void) SC_ATOMIC_SUB(defragtracker_counter, 1);
89 }
90 
91 static DefragTracker *DefragTrackerAlloc(void)
92 {
93  if (!(DEFRAG_CHECK_MEMCAP(sizeof(DefragTracker)))) {
94  return NULL;
95  }
96 
97  (void) SC_ATOMIC_ADD(defrag_memuse, sizeof(DefragTracker));
98 
99  DefragTracker *dt = SCCalloc(1, sizeof(DefragTracker));
100  if (unlikely(dt == NULL)) {
101  (void)SC_ATOMIC_SUB(defrag_memuse, sizeof(DefragTracker));
102  return NULL;
103  }
104  SCMutexInit(&dt->lock, NULL);
105  SC_ATOMIC_INIT(dt->use_cnt);
106  return dt;
107 }
108 
109 static void DefragTrackerFree(DefragTracker *dt)
110 {
111  if (dt != NULL) {
113 
114  SCMutexDestroy(&dt->lock);
115  SCFree(dt);
116  (void) SC_ATOMIC_SUB(defrag_memuse, sizeof(DefragTracker));
117  }
118 }
119 
120 #define DefragTrackerIncrUsecnt(dt) \
121  SC_ATOMIC_ADD((dt)->use_cnt, 1)
122 #define DefragTrackerDecrUsecnt(dt) \
123  SC_ATOMIC_SUB((dt)->use_cnt, 1)
124 
125 static void DefragTrackerInit(DefragTracker *dt, Packet *p)
126 {
127  /* copy address */
128  COPY_ADDRESS(&p->src, &dt->src_addr);
129  COPY_ADDRESS(&p->dst, &dt->dst_addr);
130 
131  if (PacketIsIPv4(p)) {
132  const IPV4Hdr *ip4h = PacketGetIPv4(p);
133  dt->id = IPV4_GET_RAW_IPID(ip4h);
134  dt->af = AF_INET;
135  } else {
136  DEBUG_VALIDATE_BUG_ON(!PacketIsIPv6(p));
137  dt->id = IPV6_EXTHDR_GET_FH_ID(p);
138  dt->af = AF_INET6;
139  }
140  dt->proto = PacketGetIPProto(p);
141  memcpy(&dt->vlan_id[0], &p->vlan_id[0], sizeof(dt->vlan_id));
142  dt->tunnel_id = p->tunnel_id;
143  dt->policy = DefragGetOsPolicy(p);
145  dt->remove = 0;
146  dt->seen_last = 0;
147 
148  (void) DefragTrackerIncrUsecnt(dt);
149 }
150 
152 {
153  (void) DefragTrackerDecrUsecnt(t);
154  SCMutexUnlock(&t->lock);
155 }
156 
158 {
160 }
161 
162 #define DEFRAG_DEFAULT_HASHSIZE 4096
163 #define DEFRAG_DEFAULT_MEMCAP 16777216
164 #define DEFRAG_DEFAULT_PREALLOC 1000
165 
166 /** \brief initialize the configuration
167  * \warning Not thread safe */
168 void DefragInitConfig(bool quiet)
169 {
170  SCLogDebug("initializing defrag engine...");
171 
172  memset(&defrag_config, 0, sizeof(defrag_config));
173  SC_ATOMIC_INIT(defragtracker_counter);
174  SC_ATOMIC_INIT(defrag_memuse);
175  SC_ATOMIC_INIT(defragtracker_prune_idx);
177  DefragTrackerStackInit(&defragtracker_spare_q);
178 
179  /* set defaults */
180  defrag_config.hash_rand = (uint32_t)RandomGet();
184  defrag_config.memcap_policy = ExceptionPolicyParse("defrag.memcap-policy", false);
185 
186  /* Check if we have memcap and hash_size defined at config */
187  const char *conf_val;
188  uint32_t configval = 0;
189 
190  uint64_t defrag_memcap;
191  /** set config values for memcap, prealloc and hash_size */
192  if ((SCConfGetNonNull("defrag.memcap", &conf_val)) == 1) {
193  if (ParseSizeStringU64(conf_val, &defrag_memcap) < 0) {
194  SCLogError("Error parsing defrag.memcap "
195  "from conf file - %s. Killing engine",
196  conf_val);
197  exit(EXIT_FAILURE);
198  } else {
199  SC_ATOMIC_SET(defrag_config.memcap, defrag_memcap);
200  }
201  }
202  if ((SCConfGetNonNull("defrag.hash-size", &conf_val)) == 1) {
203  if (StringParseUint32(&configval, 10, strlen(conf_val),
204  conf_val) > 0) {
205  defrag_config.hash_size = configval;
206  } else {
207  WarnInvalidConfEntry("defrag.hash-size", "%"PRIu32, defrag_config.hash_size);
208  }
209  }
210 
211  if ((SCConfGetNonNull("defrag.trackers", &conf_val)) == 1) {
212  if (StringParseUint32(&configval, 10, strlen(conf_val),
213  conf_val) > 0) {
214  defrag_config.prealloc = configval;
215  } else {
216  WarnInvalidConfEntry("defrag.trackers", "%"PRIu32, defrag_config.prealloc);
217  }
218  }
219  SCLogDebug("DefragTracker config from suricata.yaml: memcap: %"PRIu64", hash-size: "
220  "%"PRIu32", prealloc: %"PRIu32, SC_ATOMIC_GET(defrag_config.memcap),
222 
223  /* alloc hash memory */
224  uint64_t hash_size = defrag_config.hash_size * sizeof(DefragTrackerHashRow);
225  if (!(DEFRAG_CHECK_MEMCAP(hash_size))) {
226  SCLogError("allocating defrag hash failed: "
227  "max defrag memcap is smaller than projected hash size. "
228  "Memcap: %" PRIu64 ", Hash table size %" PRIu64 ". Calculate "
229  "total hash size by multiplying \"defrag.hash-size\" with %" PRIuMAX ", "
230  "which is the hash bucket size.",
231  SC_ATOMIC_GET(defrag_config.memcap), hash_size,
232  (uintmax_t)sizeof(DefragTrackerHashRow));
233  exit(EXIT_FAILURE);
234  }
236  if (unlikely(defragtracker_hash == NULL)) {
237  FatalError("Fatal error encountered in DefragTrackerInitConfig. Exiting...");
238  }
240 
241  uint32_t i = 0;
242  for (i = 0; i < defrag_config.hash_size; i++) {
244  }
245  (void) SC_ATOMIC_ADD(defrag_memuse, (defrag_config.hash_size * sizeof(DefragTrackerHashRow)));
246 
247  if (!quiet) {
248  SCLogConfig("allocated %"PRIu64" bytes of memory for the defrag hash... "
249  "%" PRIu32 " buckets of size %" PRIuMAX "",
250  SC_ATOMIC_GET(defrag_memuse), defrag_config.hash_size,
251  (uintmax_t)sizeof(DefragTrackerHashRow));
252  }
253 
254  if ((SCConfGetNonNull("defrag.prealloc", &conf_val)) == 1) {
255  if (SCConfValIsTrue(conf_val)) {
256  /* pre allocate defrag trackers */
257  for (i = 0; i < defrag_config.prealloc; i++) {
258  if (!(DEFRAG_CHECK_MEMCAP(sizeof(DefragTracker)))) {
259  SCLogError("preallocating defrag trackers failed: "
260  "max defrag memcap reached. Memcap %" PRIu64 ", "
261  "Memuse %" PRIu64 ".",
263  ((uint64_t)SC_ATOMIC_GET(defrag_memuse) +
264  (uint64_t)sizeof(DefragTracker)));
265  exit(EXIT_FAILURE);
266  }
267 
268  DefragTracker *h = DefragTrackerAlloc();
269  if (h == NULL) {
270  SCLogError("preallocating defrag failed: %s", strerror(errno));
271  exit(EXIT_FAILURE);
272  }
273  DefragTrackerEnqueue(&defragtracker_spare_q,h);
274  }
275  if (!quiet) {
276  SCLogConfig("preallocated %" PRIu32 " defrag trackers of size %" PRIuMAX "",
277  DefragTrackerStackSize(&defragtracker_spare_q),
278  (uintmax_t)sizeof(DefragTracker));
279  }
280  }
281  }
282 
283  if (!quiet) {
284  SCLogConfig("defrag memory usage: %"PRIu64" bytes, maximum: %"PRIu64,
285  SC_ATOMIC_GET(defrag_memuse), SC_ATOMIC_GET(defrag_config.memcap));
286  }
287 }
288 
289 /** \brief shutdown the flow engine
290  * \warning Not thread safe */
292 {
293  DefragTracker *dt;
294 
295  /* free spare queue */
296  while((dt = DefragTrackerDequeue(&defragtracker_spare_q))) {
297  BUG_ON(SC_ATOMIC_GET(dt->use_cnt) > 0);
298  DefragTrackerFree(dt);
299  }
300 
301  /* clear and free the hash */
302  if (defragtracker_hash != NULL) {
303  for (uint32_t u = 0; u < defrag_config.hash_size; u++) {
304  dt = defragtracker_hash[u].head;
305  while (dt) {
306  DefragTracker *n = dt->hnext;
308  DefragTrackerFree(dt);
309  dt = n;
310  }
311 
313  }
315  defragtracker_hash = NULL;
316  }
317  (void) SC_ATOMIC_SUB(defrag_memuse, defrag_config.hash_size * sizeof(DefragTrackerHashRow));
318  DefragTrackerStackDestroy(&defragtracker_spare_q);
319 }
320 
321 /** \brief compare two raw ipv6 addrs
322  *
323  * \note we don't care about the real ipv6 ip's, this is just
324  * to consistently fill the DefragHashKey6 struct, without all
325  * the SCNtohl calls.
326  *
327  * \warning do not use elsewhere unless you know what you're doing.
328  * detect-engine-address-ipv6.c's AddressIPv6GtU32 is likely
329  * what you are looking for.
330  */
331 static inline int DefragHashRawAddressIPv6GtU32(const uint32_t *a, const uint32_t *b)
332 {
333  for (int i = 0; i < 4; i++) {
334  if (a[i] > b[i])
335  return 1;
336  if (a[i] < b[i])
337  break;
338  }
339 
340  return 0;
341 }
342 
343 typedef struct DefragHashKey4_ {
344  union {
345  struct {
346  uint32_t src, dst;
347  uint32_t id;
349  uint16_t tunnel_id;
350  };
351  uint32_t u32[5];
352  };
354 
355 typedef struct DefragHashKey6_ {
356  union {
357  struct {
358  uint32_t src[4], dst[4];
359  uint32_t id;
361  uint16_t tunnel_id;
362  };
363  uint32_t u32[11];
364  };
366 
367 /* calculate the hash key for this packet
368  *
369  * we're using:
370  * hash_rand -- set at init time
371  * source address
372  * destination address
373  * id
374  * vlan_id
375  */
376 static inline uint32_t DefragHashGetKey(Packet *p)
377 {
378  uint32_t key;
379 
380  if (PacketIsIPv4(p)) {
381  const IPV4Hdr *ip4h = PacketGetIPv4(p);
382  DefragHashKey4 dhk = { 0 };
383  if (p->src.addr_data32[0] > p->dst.addr_data32[0]) {
384  dhk.src = p->src.addr_data32[0];
385  dhk.dst = p->dst.addr_data32[0];
386  } else {
387  dhk.src = p->dst.addr_data32[0];
388  dhk.dst = p->src.addr_data32[0];
389  }
390  dhk.id = (uint32_t)IPV4_GET_RAW_IPID(ip4h);
391  memcpy(&dhk.vlan_id[0], &p->vlan_id[0], sizeof(dhk.vlan_id));
392  dhk.tunnel_id = p->tunnel_id;
393 
394  uint32_t hash =
395  hashword(dhk.u32, sizeof(dhk.u32) / sizeof(uint32_t), defrag_config.hash_rand);
396  key = hash % defrag_config.hash_size;
397  } else if (PacketIsIPv6(p)) {
398  DefragHashKey6 dhk = { 0 };
399  if (DefragHashRawAddressIPv6GtU32(p->src.addr_data32, p->dst.addr_data32)) {
400  dhk.src[0] = p->src.addr_data32[0];
401  dhk.src[1] = p->src.addr_data32[1];
402  dhk.src[2] = p->src.addr_data32[2];
403  dhk.src[3] = p->src.addr_data32[3];
404  dhk.dst[0] = p->dst.addr_data32[0];
405  dhk.dst[1] = p->dst.addr_data32[1];
406  dhk.dst[2] = p->dst.addr_data32[2];
407  dhk.dst[3] = p->dst.addr_data32[3];
408  } else {
409  dhk.src[0] = p->dst.addr_data32[0];
410  dhk.src[1] = p->dst.addr_data32[1];
411  dhk.src[2] = p->dst.addr_data32[2];
412  dhk.src[3] = p->dst.addr_data32[3];
413  dhk.dst[0] = p->src.addr_data32[0];
414  dhk.dst[1] = p->src.addr_data32[1];
415  dhk.dst[2] = p->src.addr_data32[2];
416  dhk.dst[3] = p->src.addr_data32[3];
417  }
418  dhk.id = IPV6_EXTHDR_GET_FH_ID(p);
419  memcpy(&dhk.vlan_id[0], &p->vlan_id[0], sizeof(dhk.vlan_id));
420  dhk.tunnel_id = p->tunnel_id;
421 
422  uint32_t hash =
423  hashword(dhk.u32, sizeof(dhk.u32) / sizeof(uint32_t), defrag_config.hash_rand);
424  key = hash % defrag_config.hash_size;
425  } else {
426  key = 0;
427  }
428  return key;
429 }
430 
431 /* Since two or more trackers can have the same hash key, we need to compare
432  * the tracker with the current tracker key. */
433 #define CMP_DEFRAGTRACKER(d1, d2, id) \
434  (((CMP_ADDR(&(d1)->src_addr, &(d2)->src) && CMP_ADDR(&(d1)->dst_addr, &(d2)->dst)) || \
435  (CMP_ADDR(&(d1)->src_addr, &(d2)->dst) && CMP_ADDR(&(d1)->dst_addr, &(d2)->src))) && \
436  (d1)->proto == PacketGetIPProto(d2) && (d1)->id == (id) && \
437  (d1)->vlan_id[0] == (d2)->vlan_id[0] && (d1)->vlan_id[1] == (d2)->vlan_id[1] && \
438  (d1)->vlan_id[2] == (d2)->vlan_id[2] && (d1)->tunnel_id == (d2)->tunnel_id)
439 
440 static inline int DefragTrackerCompare(DefragTracker *t, Packet *p)
441 {
442  uint32_t id;
443  if (PacketIsIPv4(p)) {
444  if (t->af != AF_INET)
445  return 0;
446  const IPV4Hdr *ip4h = PacketGetIPv4(p);
447  id = (uint32_t)IPV4_GET_RAW_IPID(ip4h);
448  } else {
449  if (t->af != AF_INET6)
450  return 0;
451  id = IPV6_EXTHDR_GET_FH_ID(p);
452  }
453 
454  return CMP_DEFRAGTRACKER(t, p, id);
455 }
456 
457 static void DefragExceptionPolicyStatsIncr(
459 {
461  if (likely(id.id > 0)) {
462  StatsCounterIncr(&tv->stats, id);
463  }
464 }
465 
466 /**
467  * \brief Get a new defrag tracker
468  *
469  * Get a new defrag tracker. We're checking memcap first and will try to make room
470  * if the memcap is reached.
471  *
472  * \retval dt *LOCKED* tracker on success, NULL on error.
473  */
474 static DefragTracker *DefragTrackerGetNew(ThreadVars *tv, DecodeThreadVars *dtv, Packet *p)
475 {
476 #ifdef QA_SIMULATION
477  if (g_eps_defrag_memcap != UINT64_MAX && g_eps_defrag_memcap == PcapPacketCntGet(p)) {
478  SCLogNotice("simulating memcap hit for packet %" PRIu64, PcapPacketCntGet(p));
480  DefragExceptionPolicyStatsIncr(tv, dtv, defrag_config.memcap_policy);
481  return NULL;
482  }
483 #endif
484 
485  DefragTracker *dt = NULL;
486 
487  /* get a tracker from the spare queue */
488  dt = DefragTrackerDequeue(&defragtracker_spare_q);
489  if (dt == NULL) {
490  /* If we reached the max memcap, we get a used tracker */
491  if (!(DEFRAG_CHECK_MEMCAP(sizeof(DefragTracker)))) {
492  dt = DefragTrackerGetUsedDefragTracker(tv, dtv);
493  if (dt == NULL) {
495  DefragExceptionPolicyStatsIncr(tv, dtv, defrag_config.memcap_policy);
496  return NULL;
497  }
498 
499  /* freed a tracker, but it's unlocked */
500  } else {
501  /* now see if we can alloc a new tracker */
502  dt = DefragTrackerAlloc();
503  if (dt == NULL) {
505  DefragExceptionPolicyStatsIncr(tv, dtv, defrag_config.memcap_policy);
506  return NULL;
507  }
508 
509  /* tracker is initialized but *unlocked* */
510  }
511  } else {
512  /* tracker has been recycled before it went into the spare queue */
513 
514  /* tracker is initialized (recycled) but *unlocked* */
515  }
516 
517  (void) SC_ATOMIC_ADD(defragtracker_counter, 1);
518  SCMutexLock(&dt->lock);
519  return dt;
520 }
521 
522 /* DefragGetTrackerFromHash
523  *
524  * Hash retrieval function for trackers. Looks up the hash bucket containing the
525  * tracker pointer. Then compares the packet with the found tracker to see if it is
526  * the tracker we need. If it isn't, walk the list until the right tracker is found.
527  *
528  * returns a *LOCKED* tracker or NULL
529  */
531 {
532  DefragTracker *dt = NULL;
533 
534  /* get the key to our bucket */
535  uint32_t key = DefragHashGetKey(p);
536  /* get our hash bucket and lock it */
538  DRLOCK_LOCK(hb);
539 
540  /* see if the bucket already has a tracker */
541  if (hb->head == NULL) {
542  dt = DefragTrackerGetNew(tv, dtv, p);
543  if (dt == NULL) {
544  DRLOCK_UNLOCK(hb);
545  return NULL;
546  }
547 
548  /* tracker is locked */
549  hb->head = dt;
550 
551  /* got one, now lock, initialize and return */
552  DefragTrackerInit(dt,p);
553 
554  DRLOCK_UNLOCK(hb);
555  return dt;
556  }
557 
558  /* ok, we have a tracker in the bucket. Let's find out if it is our tracker */
559  DefragTracker *prev_dt = NULL;
560  dt = hb->head;
561 
562  do {
563  DefragTracker *next_dt = NULL;
564 
565  SCMutexLock(&dt->lock);
566  if (DefragTrackerTimedOut(dt, p->ts)) {
567  next_dt = dt->hnext;
568  dt->hnext = NULL;
569  if (prev_dt) {
570  prev_dt->hnext = next_dt;
571  } else {
572  hb->head = next_dt;
573  }
575  SCMutexUnlock(&dt->lock);
576 
579  goto tracker_removed;
580  } else if (!dt->remove && DefragTrackerCompare(dt, p)) {
581  /* found our tracker, keep locked & return */
582  (void)DefragTrackerIncrUsecnt(dt);
583  DRLOCK_UNLOCK(hb);
584  return dt;
585  }
586  SCMutexUnlock(&dt->lock);
587  /* unless we removed 'dt', prev_dt needs to point to
588  * current 'dt' when adding a new tracker below. */
589  prev_dt = dt;
590  next_dt = dt->hnext;
591 
592  tracker_removed:
593  if (next_dt == NULL) {
594  dt = DefragTrackerGetNew(tv, dtv, p);
595  if (dt == NULL) {
596  DRLOCK_UNLOCK(hb);
597  return NULL;
598  }
599  dt->hnext = hb->head;
600  hb->head = dt;
601 
602  /* tracker is locked */
603 
604  /* initialize and return */
605  DefragTrackerInit(dt, p);
606 
607  DRLOCK_UNLOCK(hb);
608  return dt;
609  }
610 
611  dt = next_dt;
612  } while (dt != NULL);
613 
614  /* should be unreachable */
615  BUG_ON(1);
616  return NULL;
617 }
618 
619 /** \brief look up a tracker in the hash
620  *
621  * \param a address to look up
622  *
623  * \retval h *LOCKED* tracker or NULL
624  */
626 {
627  DefragTracker *dt = NULL;
628 
629  /* get the key to our bucket */
630  uint32_t key = DefragHashGetKey(p);
631  /* get our hash bucket and lock it */
633  DRLOCK_LOCK(hb);
634 
635  /* see if the bucket already has a tracker */
636  if (hb->head == NULL) {
637  DRLOCK_UNLOCK(hb);
638  return dt;
639  }
640 
641  /* ok, we have a tracker in the bucket. Let's find out if it is our tracker */
642  dt = hb->head;
643 
644  do {
645  if (!dt->remove && DefragTrackerCompare(dt, p)) {
646  /* found our tracker, lock & return */
647  SCMutexLock(&dt->lock);
648  (void)DefragTrackerIncrUsecnt(dt);
649  DRLOCK_UNLOCK(hb);
650  return dt;
651 
652  } else if (dt->hnext == NULL) {
653  DRLOCK_UNLOCK(hb);
654  return NULL;
655  }
656 
657  dt = dt->hnext;
658  } while (dt != NULL);
659 
660  /* should be unreachable */
661  BUG_ON(1);
662  return NULL;
663 }
664 
665 /** \internal
666  * \brief Get a tracker from the hash directly.
667  *
668  * Called in conditions where the spare queue is empty and memcap is reached.
669  *
670  * Walks the hash until a tracker can be freed. "defragtracker_prune_idx" atomic int makes
671  * sure we don't start at the top each time since that would clear the top of
672  * the hash leading to longer and longer search times under high pressure (observed).
673  *
674  * \retval dt tracker or NULL
675  */
676 static DefragTracker *DefragTrackerGetUsedDefragTracker(ThreadVars *tv, const DecodeThreadVars *dtv)
677 {
678  uint32_t idx = SC_ATOMIC_GET(defragtracker_prune_idx) % defrag_config.hash_size;
679  uint32_t cnt = defrag_config.hash_size;
680 
681  while (cnt--) {
682  if (++idx >= defrag_config.hash_size)
683  idx = 0;
684 
686 
687  if (DRLOCK_TRYLOCK(hb) != 0)
688  continue;
689 
690  DefragTracker *dt = hb->head;
691  if (dt == NULL) {
692  DRLOCK_UNLOCK(hb);
693  continue;
694  }
695 
696  if (SCMutexTrylock(&dt->lock) != 0) {
697  DRLOCK_UNLOCK(hb);
698  continue;
699  }
700 
701  /** never prune a tracker that is used by a packets
702  * we are currently processing in one of the threads */
703  if (SC_ATOMIC_GET(dt->use_cnt) > 0) {
704  DRLOCK_UNLOCK(hb);
705  SCMutexUnlock(&dt->lock);
706  continue;
707  }
708 
709  /* only count "forced" reuse */
710  bool incr_reuse_cnt = !dt->remove;
711 
712  /* remove from the hash */
713  hb->head = dt->hnext;
714 
715  dt->hnext = NULL;
716  DRLOCK_UNLOCK(hb);
717 
719 
720  SCMutexUnlock(&dt->lock);
721 
722  if (incr_reuse_cnt) {
724  } else {
726  }
727 
728  (void) SC_ATOMIC_ADD(defragtracker_prune_idx, (defrag_config.hash_size - cnt));
729  return dt;
730  }
731 
732  return NULL;
733 }
PKT_DROP_REASON_DEFRAG_MEMCAP
@ PKT_DROP_REASON_DEFRAG_MEMCAP
Definition: decode.h:406
util-byte.h
IPV4_GET_RAW_IPID
#define IPV4_GET_RAW_IPID(ip4h)
Definition: decode-ipv4.h:99
DefragTrackerFreeFrags
void DefragTrackerFreeFrags(DefragTracker *tracker)
Free all frags associated with a tracker.
Definition: defrag.c:118
DefragLookupTrackerFromHash
DefragTracker * DefragLookupTrackerFromHash(Packet *p)
look up a tracker in the hash
Definition: defrag-hash.c:625
ExceptionPolicyApply
void ExceptionPolicyApply(Packet *p, enum ExceptionPolicy policy, enum PacketDropReason drop_reason)
Definition: util-exception-policy.c:138
DefragHashKey4
struct DefragHashKey4_ DefragHashKey4
SCConfValIsTrue
int SCConfValIsTrue(const char *val)
Check if a value is true.
Definition: conf.c:577
IPV6_EXTHDR_GET_FH_ID
#define IPV6_EXTHDR_GET_FH_ID(p)
Definition: decode-ipv6.h:103
DefragHashKey4_::src
uint32_t src
Definition: defrag-hash.c:346
hashword
uint32_t hashword(const uint32_t *k, size_t length, uint32_t initval)
Definition: util-hash-lookup3.c:172
DefragTracker_::hnext
struct DefragTracker_ * hnext
Definition: defrag.h:122
defragtracker_hash
DefragTrackerHashRow * defragtracker_hash
Definition: defrag-hash.c:31
SC_ATOMIC_INIT
#define SC_ATOMIC_INIT(name)
wrapper for initializing an atomic variable.
Definition: util-atomic.h:314
DefragTrackerIncrUsecnt
#define DefragTrackerIncrUsecnt(dt)
Definition: defrag-hash.c:120
DefragHashKey6_::u32
uint32_t u32[11]
Definition: defrag-hash.c:363
unlikely
#define unlikely(expr)
Definition: util-optimize.h:35
SC_ATOMIC_SET
#define SC_ATOMIC_SET(name, val)
Set the value for the atomic variable.
Definition: util-atomic.h:386
DefragTrackerHashRow_
Definition: defrag-hash.h:60
PcapPacketCntGet
uint64_t PcapPacketCntGet(const Packet *p)
Definition: decode.c:1244
SCLogDebug
#define SCLogDebug(...)
Definition: util-debug.h:282
ParseSizeStringU64
int ParseSizeStringU64(const char *size, uint64_t *res)
Definition: util-misc.c:190
DefragHashKey6_::tunnel_id
uint16_t tunnel_id
Definition: defrag-hash.c:361
DefragTrackerStackSize
uint32_t DefragTrackerStackSize(DefragTrackerStack *q)
return stack size
Definition: defrag-stack.c:105
DefragHashKey4_::vlan_id
uint16_t vlan_id[VLAN_MAX_LAYERS]
Definition: defrag-hash.c:348
DefragHashKey4_::id
uint32_t id
Definition: defrag-hash.c:347
SC_ATOMIC_ADD
#define SC_ATOMIC_ADD(name, val)
add a value to our atomic variable
Definition: util-atomic.h:332
DefragTrackerHashRow
struct DefragTrackerHashRow_ DefragTrackerHashRow
DefragTrackerDequeue
DefragTracker * DefragTrackerDequeue(DefragTrackerStack *q)
remove a tracker from the queue
Definition: defrag-stack.c:81
DefragTrackerClearMemory
void DefragTrackerClearMemory(DefragTracker *dt)
Definition: defrag-hash.c:157
SCMutexLock
#define SCMutexLock(mut)
Definition: threads-debug.h:117
DefragTracker_::host_timeout
uint32_t host_timeout
Definition: defrag.h:114
defrag-timeout.h
p
Packet * p
Definition: fuzz_dataset.c:30
RandomGet
long int RandomGet(void)
Definition: util-random.c:130
StatsCounterId
Definition: counters.h:30
DefragTrackerGetMemuse
uint64_t DefragTrackerGetMemuse(void)
Return memuse value.
Definition: defrag-hash.c:74
defrag-config.h
DefragPolicyGetHostTimeout
int DefragPolicyGetHostTimeout(Packet *p)
Definition: defrag-config.c:95
ExceptionPolicyParse
enum ExceptionPolicy ExceptionPolicyParse(const char *option, bool support_flow)
Definition: util-exception-policy.c:312
DefragTrackerMoveToSpare
void DefragTrackerMoveToSpare(DefragTracker *h)
Definition: defrag-hash.c:85
DecodeThreadVars_::counter_defrag_tracker_hard_reuse
StatsCounterId counter_defrag_tracker_hard_reuse
Definition: decode.h:1085
DefragTracker_::lock
SCMutex lock
Definition: defrag.h:86
DefragConfig_::hash_size
uint32_t hash_size
Definition: defrag-hash.h:71
DefragGetOsPolicy
uint8_t DefragGetOsPolicy(Packet *p)
Get the defrag policy based on the destination address of the packet.
Definition: defrag.c:989
DefragConfig_::prealloc
uint32_t prealloc
Definition: defrag-hash.h:72
DefragHashKey6_::src
uint32_t src[4]
Definition: defrag-hash.c:358
DefragTracker_::tunnel_id
uint16_t tunnel_id
Definition: defrag.h:107
DefragTrackerHashRow_::head
DefragTracker * head
Definition: defrag-hash.h:62
DefragTracker_::remove
uint8_t remove
Definition: defrag.h:105
defrag_config
DefragConfig defrag_config
Definition: defrag-hash.c:32
DefragHashKey6_::vlan_id
uint16_t vlan_id[VLAN_MAX_LAYERS]
Definition: defrag-hash.c:360
Packet_::ts
SCTime_t ts
Definition: decode.h:590
DefragTracker_::seen_last
uint8_t seen_last
Definition: defrag.h:103
DefragTracker_::policy
uint8_t policy
Definition: defrag.h:98
SCMutexUnlock
#define SCMutexUnlock(mut)
Definition: threads-debug.h:120
DefragTracker_
Definition: defrag.h:85
DEFRAG_CHECK_MEMCAP
#define DEFRAG_CHECK_MEMCAP(size)
check if a memory alloc would fit in the memcap
Definition: defrag-hash.h:83
DefragTracker_::vlan_id
uint16_t vlan_id[VLAN_MAX_LAYERS]
Definition: defrag.h:89
SCConfGetNonNull
int SCConfGetNonNull(const char *name, const char **vptr)
Retrieve the non-null value of a configuration node.
Definition: conf.c:380
ThreadVars_
Per thread variable structure.
Definition: threadvars.h:57
Packet_::tunnel_id
uint16_t tunnel_id
Definition: decode.h:558
StatsCounterIncr
void StatsCounterIncr(StatsThreadContext *stats, StatsCounterId id)
Increments the local counter.
Definition: counters.c:163
DefragTrackerTimedOut
int DefragTrackerTimedOut(DefragTracker *dt, SCTime_t ts)
Definition: defrag-timeout.c:39
DEFRAG_DEFAULT_HASHSIZE
#define DEFRAG_DEFAULT_HASHSIZE
Definition: defrag-hash.c:162
StringParseUint32
int StringParseUint32(uint32_t *res, int base, size_t len, const char *str)
Definition: util-byte.c:268
DefragHashKey6_::dst
uint32_t dst[4]
Definition: defrag-hash.c:358
DefragTrackerGetMemcap
uint64_t DefragTrackerGetMemcap(void)
Return memcap value.
Definition: defrag-hash.c:63
DefragTracker_::dst_addr
Address dst_addr
Definition: defrag.h:110
DRLOCK_LOCK
#define DRLOCK_LOCK(fb)
Definition: defrag-hash.h:53
BUG_ON
#define BUG_ON(x)
Definition: suricata-common.h:322
SC_ATOMIC_SUB
#define SC_ATOMIC_SUB(name, val)
sub a value from our atomic variable
Definition: util-atomic.h:341
DefragHashKey6
struct DefragHashKey6_ DefragHashKey6
DecodeThreadVars_::counter_defrag_tracker_timeout
StatsCounterId counter_defrag_tracker_timeout
Definition: decode.h:1086
Packet_
Definition: decode.h:528
DefragTracker_::id
uint32_t id
Definition: defrag.h:93
DefragTracker_::src_addr
Address src_addr
Definition: defrag.h:109
DRLOCK_UNLOCK
#define DRLOCK_UNLOCK(fb)
Definition: defrag-hash.h:55
conf.h
DefragInitConfig
void DefragInitConfig(bool quiet)
initialize the configuration
Definition: defrag-hash.c:168
DEFRAG_DEFAULT_MEMCAP
#define DEFRAG_DEFAULT_MEMCAP
Definition: defrag-hash.c:163
DefragHashKey4_::u32
uint32_t u32[5]
Definition: defrag-hash.c:351
DefragTrackerRelease
void DefragTrackerRelease(DefragTracker *t)
Definition: defrag-hash.c:151
DefragTracker_::proto
uint8_t proto
Definition: defrag.h:96
SCMutexInit
#define SCMutexInit(mut, mutattrs)
Definition: threads-debug.h:116
WarnInvalidConfEntry
#define WarnInvalidConfEntry(param_name, format, value)
Generic API that can be used by all to log an invalid conf entry.
Definition: util-misc.h:35
DefragConfig_::memcap_policy
enum ExceptionPolicy memcap_policy
Definition: defrag-hash.h:73
dtv
DecodeThreadVars * dtv
Definition: fuzz_decodepcapfile.c:35
DecodeThreadVars_::counter_defrag_memcap_eps
ExceptionPolicyCounters counter_defrag_memcap_eps
Definition: decode.h:1087
DefragTrackerStack_
Definition: defrag-stack.h:41
IPV4Hdr_
Definition: decode-ipv4.h:72
DRLOCK_DESTROY
#define DRLOCK_DESTROY(fb)
Definition: defrag-hash.h:52
cnt
uint32_t cnt
Definition: tmqh-packetpool.h:7
DefragTrackerStackDestroy
void DefragTrackerStackDestroy(DefragTrackerStack *q)
Destroy a tracker queue.
Definition: defrag-stack.c:46
suricata-common.h
defrag-stack.h
DefragHashKey4_
Definition: defrag-hash.c:343
DefragHashKey6_
Definition: defrag-hash.c:355
VLAN_MAX_LAYERS
#define VLAN_MAX_LAYERS
Definition: decode-vlan.h:51
DefragConfig_
Definition: defrag-hash.h:68
CMP_DEFRAGTRACKER
#define CMP_DEFRAGTRACKER(d1, d2, id)
Definition: defrag-hash.c:433
FatalError
#define FatalError(...)
Definition: util-debug.h:517
util-hash-lookup3.h
DRLOCK_TRYLOCK
#define DRLOCK_TRYLOCK(fb)
Definition: defrag-hash.h:54
tv
ThreadVars * tv
Definition: fuzz_decodepcapfile.c:34
DefragTrackerSetMemcap
int DefragTrackerSetMemcap(uint64_t size)
Update memcap value.
Definition: defrag-hash.c:48
util-validate.h
DefragConfig_::hash_rand
uint32_t hash_rand
Definition: defrag-hash.h:70
SCLogConfig
struct SCLogConfig_ SCLogConfig
Holds the config state used by the logging api.
SCLogError
#define SCLogError(...)
Macro used to log ERROR messages.
Definition: util-debug.h:274
DefragHashKey4_::tunnel_id
uint16_t tunnel_id
Definition: defrag-hash.c:349
SCFree
#define SCFree(p)
Definition: util-mem.h:61
DecodeThreadVars_
Structure to hold thread specific data for all decode modules.
Definition: decode.h:1015
DRLOCK_INIT
#define DRLOCK_INIT(fb)
Definition: defrag-hash.h:51
DefragTracker_::af
uint8_t af
Definition: defrag.h:100
util-random.h
DEFRAG_DEFAULT_PREALLOC
#define DEFRAG_DEFAULT_PREALLOC
Definition: defrag-hash.c:164
DefragTrackerDecrUsecnt
#define DefragTrackerDecrUsecnt(dt)
Definition: defrag-hash.c:122
defrag-hash.h
Packet_::dst
Address dst
Definition: decode.h:533
DefragHashKey6_::id
uint32_t id
Definition: defrag-hash.c:359
Packet_::vlan_id
uint16_t vlan_id[VLAN_MAX_LAYERS]
Definition: decode.h:560
likely
#define likely(expr)
Definition: util-optimize.h:32
DefragGetTrackerFromHash
DefragTracker * DefragGetTrackerFromHash(ThreadVars *tv, DecodeThreadVars *dtv, Packet *p)
Definition: defrag-hash.c:530
DefragHashShutdown
void DefragHashShutdown(void)
shutdown the flow engine
Definition: defrag-hash.c:291
DecodeThreadVars_::counter_defrag_tracker_soft_reuse
StatsCounterId counter_defrag_tracker_soft_reuse
Definition: decode.h:1084
SC_ATOMIC_GET
#define SC_ATOMIC_GET(name)
Get the value from the atomic variable.
Definition: util-atomic.h:375
util-misc.h
COPY_ADDRESS
#define COPY_ADDRESS(a, b)
Definition: decode.h:145
SC_ATOMIC_DECLARE
SC_ATOMIC_DECLARE(uint64_t, defrag_memuse)
SCLogNotice
#define SCLogNotice(...)
Macro used to log NOTICE messages.
Definition: util-debug.h:250
ExceptionPolicy
ExceptionPolicy
Definition: util-exception-policy-types.h:26
SCCalloc
#define SCCalloc(nm, sz)
Definition: util-mem.h:53
ThreadVars_::stats
StatsThreadContext stats
Definition: threadvars.h:120
SCMutexDestroy
#define SCMutexDestroy
Definition: threads-debug.h:121
DefragTrackerStackInit
DefragTrackerStack * DefragTrackerStackInit(DefragTrackerStack *q)
Definition: defrag-stack.c:32
DefragTrackerEnqueue
void DefragTrackerEnqueue(DefragTrackerStack *q, DefragTracker *dt)
add a tracker to a queue
Definition: defrag-stack.c:57
DefragHashKey4_::dst
uint32_t dst
Definition: defrag-hash.c:346
DefragGetMemcapExceptionPolicy
enum ExceptionPolicy DefragGetMemcapExceptionPolicy(void)
Definition: defrag-hash.c:80
ExceptionPolicyCounters_::eps_id
StatsCounterId eps_id[EXCEPTION_POLICY_MAX]
Definition: util-exception-policy-types.h:56
DEBUG_VALIDATE_BUG_ON
#define DEBUG_VALIDATE_BUG_ON(exp)
Definition: util-validate.h:109
Packet_::src
Address src
Definition: decode.h:532
SCMutexTrylock
#define SCMutexTrylock(mut)
Definition: threads-debug.h:118