suricata
app-layer-frames.c
Go to the documentation of this file.
1 /* Copyright (C) 2007-2024 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 Victor Julien <victor@inliniac.net>
22  *
23  */
24 
25 #include "suricata-common.h"
26 #include "util-print.h"
27 
28 #include "flow.h"
29 #include "stream-tcp.h"
30 #include "rust.h"
31 #include "app-layer-frames.h"
32 #include "app-layer-parser.h"
33 
34 struct FrameConfig {
35  SC_ATOMIC_DECLARE(uint64_t, types);
36 };
37 /* This array should be allocated to contain g_alproto_max protocols. */
38 static struct FrameConfig *frame_config;
39 
40 void FrameConfigInit(void)
41 {
42  frame_config = SCCalloc(g_alproto_max, sizeof(struct FrameConfig));
43  if (unlikely(frame_config == NULL)) {
44  FatalError("Unable to alloc frame_config.");
45  }
46  for (AppProto p = 0; p < g_alproto_max; p++) {
47  SC_ATOMIC_INIT(frame_config[p].types);
48  }
49 }
50 
52 {
53  SCFree(frame_config);
54 }
55 
57 {
58  const uint64_t bits = UINT64_MAX;
59  for (AppProto p = 0; p < g_alproto_max; p++) {
60  struct FrameConfig *fc = &frame_config[p];
61  SC_ATOMIC_OR(fc->types, bits);
62  }
63 }
64 
65 void FrameConfigEnable(const AppProto p, const uint8_t type)
66 {
67  const uint64_t bits = BIT_U64(type);
68  struct FrameConfig *fc = &frame_config[p];
69  SC_ATOMIC_OR(fc->types, bits);
70 }
71 
72 static inline bool FrameConfigTypeIsEnabled(const AppProto p, const uint8_t type)
73 {
74  struct FrameConfig *fc = &frame_config[p];
75  const uint64_t bits = BIT_U64(type);
76  const bool enabled = (SC_ATOMIC_GET(fc->types) & bits) != 0;
77  return enabled;
78 }
79 
80 #ifdef DEBUG
81 static void FrameDebug(const char *prefix, const Frames *frames, const Frame *frame)
82 {
83  const char *type_name = "unknown";
84  if (frame->type == FRAME_STREAM_TYPE) {
85  type_name = "stream";
86  } else if (frames != NULL) {
87  type_name = AppLayerParserGetFrameNameById(frames->ipproto, frames->alproto, frame->type);
88  }
89  SCLogDebug("[%s] %p: frame:%p type:%u/%s id:%" PRIi64 " flags:%02x offset:%" PRIu64
90  ", len:%" PRIi64 ", inspect_progress:%" PRIu64 ", events:%u %u/%u/%u/%u",
91  prefix, frames, frame, frame->type, type_name, frame->id, frame->flags, frame->offset,
92  frame->len, frame->inspect_progress, frame->event_cnt, frame->events[0],
93  frame->events[1], frame->events[2], frame->events[3]);
94 }
95 #else
96 #define FrameDebug(prefix, frames, frame)
97 #endif
98 
99 /**
100  * \note "open" means a frame that has no length set (len == -1)
101  * \todo perhaps we can search backwards */
102 Frame *FrameGetLastOpenByType(Frames *frames, const uint8_t frame_type)
103 {
104  Frame *candidate = NULL;
105 
106  SCLogDebug(
107  "frames %p cnt %u, looking for last of type %" PRIu8, frames, frames->cnt, frame_type);
108  for (uint16_t i = 0; i < frames->cnt; i++) {
109  if (i < FRAMES_STATIC_CNT) {
110  Frame *frame = &frames->sframes[i];
111  FrameDebug("get_by_id(static)", frames, frame);
112  if (frame->type == frame_type && frame->len == -1)
113  candidate = frame;
114  } else {
115  const uint16_t o = i - FRAMES_STATIC_CNT;
116  Frame *frame = &frames->dframes[o];
117  FrameDebug("get_by_id(dynamic)", frames, frame);
118  if (frame->type == frame_type && frame->len == -1)
119  candidate = frame;
120  }
121  }
122  return candidate;
123 }
124 
125 Frame *FrameGetById(Frames *frames, const int64_t id)
126 {
127  SCLogDebug("frames %p cnt %u, looking for %" PRIi64, frames, frames->cnt, id);
128  for (uint16_t i = 0; i < frames->cnt; i++) {
129  if (i < FRAMES_STATIC_CNT) {
130  Frame *frame = &frames->sframes[i];
131  FrameDebug("get_by_id(static)", frames, frame);
132  if (frame->id == id)
133  return frame;
134  } else {
135  const uint16_t o = i - FRAMES_STATIC_CNT;
136  Frame *frame = &frames->dframes[o];
137  FrameDebug("get_by_id(dynamic)", frames, frame);
138  if (frame->id == id)
139  return frame;
140  }
141  }
142  return NULL;
143 }
144 
145 Frame *FrameGetByIndex(Frames *frames, const uint32_t idx)
146 {
147  if (idx >= frames->cnt)
148  return NULL;
149 
150  if (idx < FRAMES_STATIC_CNT) {
151  Frame *frame = &frames->sframes[idx];
152  FrameDebug("get_by_idx(s)", frames, frame);
153  return frame;
154  } else {
155  const uint32_t o = idx - FRAMES_STATIC_CNT;
156  Frame *frame = &frames->dframes[o];
157  FrameDebug("get_by_idx(d)", frames, frame);
158  return frame;
159  }
160 }
161 
162 static Frame *FrameNew(Frames *frames, uint64_t offset, int64_t len)
163 {
164  DEBUG_VALIDATE_BUG_ON(frames == NULL);
165 
166  if (frames->cnt < FRAMES_STATIC_CNT) {
167  Frame *frame = &frames->sframes[frames->cnt];
168  frames->sframes[frames->cnt].offset = offset;
169  frames->sframes[frames->cnt].len = len;
170  frames->sframes[frames->cnt].id = ++frames->base_id;
171  frames->cnt++;
172  return frame;
173  } else if (frames->dframes == NULL) {
174  DEBUG_VALIDATE_BUG_ON(frames->dyn_size != 0);
176 
177  frames->dframes = SCCalloc(8, sizeof(Frame));
178  if (frames->dframes == NULL) {
179  return NULL;
180  }
181  frames->cnt++;
183 
184  frames->dyn_size = 8;
185  frames->dframes[0].offset = offset;
186  frames->dframes[0].len = len;
187  frames->dframes[0].id = ++frames->base_id;
188  return &frames->dframes[0];
189  } else {
191 
192  /* need to handle dynamic storage of frames now */
193  const uint16_t dyn_cnt = frames->cnt - FRAMES_STATIC_CNT;
194  if (dyn_cnt < frames->dyn_size) {
195  DEBUG_VALIDATE_BUG_ON(frames->dframes == NULL);
196 
197  // fall through
198  } else {
199  if (frames->dyn_size == 256) {
200  SCLogDebug("limit reached! 256 dynamic frames already");
201  // limit reached
202  // TODO figure out if this should lead to an event of sorts
203  return NULL;
204  }
205 
206  /* realloc time */
207  uint16_t new_dyn_size = frames->dyn_size * 2;
208  uint32_t new_alloc_size = new_dyn_size * sizeof(Frame);
209 
210  void *ptr = SCRealloc(frames->dframes, new_alloc_size);
211  if (ptr == NULL) {
212  return NULL;
213  }
214 
215  memset((uint8_t *)ptr + (frames->dyn_size * sizeof(Frame)), 0x00,
216  (frames->dyn_size * sizeof(Frame)));
217  frames->dframes = ptr;
218  frames->dyn_size = new_dyn_size;
219  }
220 
221  frames->cnt++;
222  frames->dframes[dyn_cnt].offset = offset;
223  frames->dframes[dyn_cnt].len = len;
224  frames->dframes[dyn_cnt].id = ++frames->base_id;
225  return &frames->dframes[dyn_cnt];
226  }
227 }
228 
229 static void FrameClean(Frame *frame)
230 {
231  memset(frame, 0, sizeof(*frame));
232 }
233 
234 static void FrameCopy(Frame *dst, Frame *src)
235 {
236  memcpy(dst, src, sizeof(*dst));
237 }
238 
239 #ifdef DEBUG
240 static void AppLayerFrameDumpForFrames(const char *prefix, const Frames *frames)
241 {
242  SCLogDebug("prefix: %s", prefix);
243  for (uint16_t i = 0; i < frames->cnt; i++) {
244  if (i < FRAMES_STATIC_CNT) {
245  const Frame *frame = &frames->sframes[i];
246  FrameDebug(prefix, frames, frame);
247  } else {
248  const uint16_t o = i - FRAMES_STATIC_CNT;
249  const Frame *frame = &frames->dframes[o];
250  FrameDebug(prefix, frames, frame);
251  }
252  }
253  SCLogDebug("prefix: %s", prefix);
254 }
255 #endif
256 
257 static inline uint64_t FrameLeftEdge(const TcpStream *stream, const Frame *frame)
258 {
259  const int64_t app_progress = STREAM_APP_PROGRESS(stream);
260 
261  const int64_t frame_offset = frame->offset;
262  const int64_t frame_data = app_progress - frame_offset;
263 
264  SCLogDebug("frame_offset %" PRIi64 ", frame_data %" PRIi64 ", frame->len %" PRIi64,
265  frame_offset, frame_data, frame->len);
266  DEBUG_VALIDATE_BUG_ON(frame_offset > app_progress);
267 
268  /* length unknown, make sure to have at least 2500 */
269  if (frame->len < 0) {
270  if (frame_data <= 2500) {
271  SCLogDebug("got <= 2500 bytes (%" PRIu64 "), returning offset %" PRIu64, frame_data,
272  frame_offset);
273  return frame_offset;
274  } else {
275  SCLogDebug("got > 2500 bytes (%" PRIu64 "), returning offset %" PRIu64, frame_data,
276  (frame_offset + (frame_data - 2500)));
277  return frame_offset + (frame_data - 2500);
278  }
279 
280  /* length specified */
281  } else {
282  /* have all data for the frame, we can skip it */
283  if (frame->len <= frame_data) {
284  uint64_t x = frame_offset + frame_data;
285  SCLogDebug("x %" PRIu64, x);
286  return x;
287  /*
288 
289  [ stream <frame_data> ]
290  [ frame .......]
291 
292  */
293  } else if (frame_data < 2500) {
294  uint64_t x = frame_offset;
295  SCLogDebug("x %" PRIu64, x);
296  return x;
297  } else {
298  uint64_t x = frame_offset + (frame_data - 2500);
299  SCLogDebug("x %" PRIu64, x);
300  return x;
301  }
302  }
303 }
304 
305 /** Stream buffer slides forward, we need to update and age out
306  * frame offsets/frames. Aging out means we move existing frames
307  * into the slots we'd free up.
308  *
309  * Start:
310  *
311  * [ stream ]
312  * [ frame ...........]
313  * offset: 2
314  * len: 19
315  *
316  * Slide:
317  * [ stream ]
318  * [ frame .... .]
319  * offset: 2
320  * len: 19
321  *
322  * Slide:
323  * [ stream ]
324  * [ frame ........... ]
325  * offset: 2
326  * len: 19
327  */
328 static int FrameSlide(const char *ds, Frames *frames, const TcpStream *stream, const uint32_t slide)
329 {
330  SCLogDebug("start: left edge %" PRIu64 ", left_edge_rel %u, stream base %" PRIu64
331  ", next %" PRIu64,
332  (uint64_t)frames->left_edge_rel + STREAM_BASE_OFFSET(stream), frames->left_edge_rel,
333  STREAM_BASE_OFFSET(stream), STREAM_BASE_OFFSET(stream) + slide);
334  DEBUG_VALIDATE_BUG_ON(frames == NULL);
335  SCLogDebug("%s frames %p: sliding %u bytes", ds, frames, slide);
336  uint64_t le = STREAM_APP_PROGRESS(stream);
337  const uint64_t next_base = STREAM_BASE_OFFSET(stream) + slide;
338 #if defined(DEBUG) || defined(DEBUG_VALIDATION)
339  const uint16_t start = frames->cnt;
340  uint16_t removed = 0;
341 #endif
342  uint16_t x = 0;
343  for (uint16_t i = 0; i < frames->cnt; i++) {
344  if (i < FRAMES_STATIC_CNT) {
345  Frame *frame = &frames->sframes[i];
346  FrameDebug("slide(s)", frames, frame);
347  if (frame->len >= 0 && frame->offset + frame->len <= next_base) {
348  // remove by not incrementing 'x'
349  SCLogDebug("removing %p id %" PRIi64, frame, frame->id);
350  FrameClean(frame);
351 #if defined(DEBUG) || defined(DEBUG_VALIDATION)
352  removed++;
353 #endif
354  } else {
355  Frame *nframe = &frames->sframes[x];
356  FrameCopy(nframe, frame);
357  if (frame != nframe) {
358  FrameClean(frame);
359  }
360  le = MIN(le, FrameLeftEdge(stream, nframe));
361  x++;
362  }
363  } else {
364  const uint16_t o = i - FRAMES_STATIC_CNT;
365  Frame *frame = &frames->dframes[o];
366  FrameDebug("slide(d)", frames, frame);
367  if (frame->len >= 0 && frame->offset + frame->len <= next_base) {
368  // remove by not incrementing 'x'
369  SCLogDebug("removing %p id %" PRIi64, frame, frame->id);
370  FrameClean(frame);
371 #if defined(DEBUG) || defined(DEBUG_VALIDATION)
372  removed++;
373 #endif
374  } else {
375  Frame *nframe;
376  if (x >= FRAMES_STATIC_CNT) {
377  nframe = &frames->dframes[x - FRAMES_STATIC_CNT];
378  } else {
379  nframe = &frames->sframes[x];
380  }
381  FrameCopy(nframe, frame);
382  if (frame != nframe) {
383  FrameClean(frame);
384  }
385  le = MIN(le, FrameLeftEdge(stream, nframe));
386  x++;
387  }
388  }
389  }
390  frames->cnt = x;
391  uint64_t o = STREAM_BASE_OFFSET(stream) + slide;
392  DEBUG_VALIDATE_BUG_ON(o > le);
393  DEBUG_VALIDATE_BUG_ON(le - o > UINT32_MAX);
394  frames->left_edge_rel = (uint32_t)(le - o);
395 
396 #ifdef DEBUG
397  SCLogDebug("end: left edge %" PRIu64 ", left_edge_rel %u, stream base %" PRIu64
398  " (+slide), cnt %u, removed %u, start %u",
399  (uint64_t)frames->left_edge_rel + STREAM_BASE_OFFSET(stream) + slide,
400  frames->left_edge_rel, STREAM_BASE_OFFSET(stream) + slide, frames->cnt, removed, start);
401  char pf[32] = "";
402  snprintf(pf, sizeof(pf), "%s:post_slide", ds);
403  AppLayerFrameDumpForFrames(pf, frames);
404 #endif
405  DEBUG_VALIDATE_BUG_ON(x != start - removed);
406  return 0;
407 }
408 
409 void AppLayerFramesSlide(Flow *f, const uint32_t slide, const uint8_t direction)
410 {
411  FramesContainer *frames_container = AppLayerFramesGetContainer(f);
412  if (frames_container == NULL)
413  return;
414  Frames *frames;
415  TcpSession *ssn = f->protoctx;
416  TcpStream *stream;
417  if (direction == STREAM_TOSERVER) {
418  stream = &ssn->client;
419  frames = &frames_container->toserver;
420  FrameSlide("toserver", frames, stream, slide);
421  } else {
422  stream = &ssn->server;
423  frames = &frames_container->toclient;
424  FrameSlide("toclient", frames, stream, slide);
425  }
426 }
427 
428 static void FrameFreeSingleFrame(Frames *frames, Frame *r)
429 {
430  FrameDebug("free", frames, r);
431  FrameClean(r);
432 }
433 
434 static void FramesClear(Frames *frames)
435 {
436  DEBUG_VALIDATE_BUG_ON(frames == NULL);
437 
438  SCLogDebug("frames %u", frames->cnt);
439  for (uint16_t i = 0; i < frames->cnt; i++) {
440  if (i < FRAMES_STATIC_CNT) {
441  Frame *r = &frames->sframes[i];
442  SCLogDebug("removing frame %p", r);
443  FrameFreeSingleFrame(frames, r);
444  } else {
445  const uint16_t o = i - FRAMES_STATIC_CNT;
446  Frame *r = &frames->dframes[o];
447  SCLogDebug("removing frame %p", r);
448  FrameFreeSingleFrame(frames, r);
449  }
450  }
451  frames->cnt = 0;
452 }
453 
454 void FramesFree(Frames *frames)
455 {
456  DEBUG_VALIDATE_BUG_ON(frames == NULL);
457  FramesClear(frames);
458  SCFree(frames->dframes);
459  frames->dframes = NULL;
460 }
461 
462 /** \brief create new frame using a pointer to start of the frame
463  */
465  const uint8_t *frame_start, const int64_t len, int dir, uint8_t frame_type)
466 {
467  SCLogDebug("frame_start:%p stream_slice->input:%p stream_slice->offset:%" PRIu64, frame_start,
468  stream_slice->input, stream_slice->offset);
469 
470  if (!(FrameConfigTypeIsEnabled(f->alproto, frame_type)))
471  return NULL;
472 
473  /* workarounds for many (unit|fuzz)tests not handling TCP data properly */
474 #if defined(UNITTESTS) || defined(FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION)
475  if (f->proto == IPPROTO_TCP && f->protoctx == NULL)
476  return NULL;
477  if (frame_start < stream_slice->input ||
478  frame_start > stream_slice->input + stream_slice->input_len)
479  return NULL;
480 #endif
481  DEBUG_VALIDATE_BUG_ON(frame_start < stream_slice->input);
482  DEBUG_VALIDATE_BUG_ON(stream_slice->input == NULL);
483  DEBUG_VALIDATE_BUG_ON(f->proto == IPPROTO_TCP && f->protoctx == NULL);
484 
485  ptrdiff_t ptr_offset = frame_start - stream_slice->input;
486 #ifdef DEBUG
487  uint64_t offset = ptr_offset + stream_slice->offset;
488  SCLogDebug("flow %p direction %s frame %p starting at %" PRIu64 " len %" PRIi64
489  " (offset %" PRIu64 ")",
490  f, dir == 0 ? "toserver" : "toclient", frame_start, offset, len, stream_slice->offset);
491 #endif
492  DEBUG_VALIDATE_BUG_ON(f->alparser == NULL);
493 
494  FramesContainer *frames_container = AppLayerFramesSetupContainer(f);
495  if (frames_container == NULL)
496  return NULL;
497 
498  Frames *frames;
499  if (dir == 0) {
500  frames = &frames_container->toserver;
501  } else {
502  frames = &frames_container->toclient;
503  }
504 
505  uint64_t abs_frame_offset = stream_slice->offset + ptr_offset;
506 
507  Frame *r = FrameNew(frames, abs_frame_offset, len);
508  if (r != NULL) {
509  r->type = frame_type;
510  FrameDebug("new_by_ptr", frames, r);
511  }
512  return r;
513 }
514 
515 static Frame *AppLayerFrameUdp(
516  Flow *f, const uint32_t frame_start_rel, const int64_t len, int dir, uint8_t frame_type)
517 {
518  DEBUG_VALIDATE_BUG_ON(f->proto != IPPROTO_UDP);
519 
520  if (!(FrameConfigTypeIsEnabled(f->alproto, frame_type)))
521  return NULL;
522 
523  FramesContainer *frames_container = AppLayerFramesSetupContainer(f);
524  if (frames_container == NULL)
525  return NULL;
526 
527  Frames *frames;
528  if (dir == 0) {
529  frames = &frames_container->toserver;
530  } else {
531  frames = &frames_container->toclient;
532  }
533 
534  Frame *r = FrameNew(frames, frame_start_rel, len);
535  if (r != NULL) {
536  r->type = frame_type;
537  }
538  return r;
539 }
540 
541 /** \brief create new frame using a relative offset from the start of the stream slice
542  */
543 Frame *SCAppLayerFrameNewByRelativeOffset(Flow *f, const void *ss, const uint32_t frame_start_rel,
544  const int64_t len, int dir, uint8_t frame_type)
545 {
546  // need to hide StreamSlice argument
547  // as we cannot bindgen a C function with an argument whose type
548  // is defined in rust (at least before a suricata_core crate)
549  const StreamSlice *stream_slice = (const StreamSlice *)ss;
550  if (!(FrameConfigTypeIsEnabled(f->alproto, frame_type)))
551  return NULL;
552 
553  /* workarounds for many (unit|fuzz)tests not handling TCP data properly */
554 #if defined(UNITTESTS) || defined(FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION)
555  if (f->proto == IPPROTO_TCP && f->protoctx == NULL)
556  return NULL;
557  if (stream_slice->input == NULL)
558  return NULL;
559 #else
560  DEBUG_VALIDATE_BUG_ON(stream_slice->input == NULL);
561 #endif
562  DEBUG_VALIDATE_BUG_ON(f->proto == IPPROTO_TCP && f->protoctx == NULL);
563  DEBUG_VALIDATE_BUG_ON(f->alparser == NULL);
564 
565  if (f->proto == IPPROTO_UDP) {
566  return AppLayerFrameUdp(f, frame_start_rel, len, dir, frame_type);
567  }
568 
569  FramesContainer *frames_container = AppLayerFramesSetupContainer(f);
570  if (frames_container == NULL)
571  return NULL;
572 
573  Frames *frames;
574  if (dir == 0) {
575  frames = &frames_container->toserver;
576  } else {
577  frames = &frames_container->toclient;
578  }
579 
580  const uint64_t frame_abs_offset = (uint64_t)frame_start_rel + stream_slice->offset;
581 #ifdef DEBUG_VALIDATION
582  const TcpSession *ssn = f->protoctx;
583  const TcpStream *stream = dir == 0 ? &ssn->client : &ssn->server;
584  BUG_ON(stream_slice->offset != STREAM_APP_PROGRESS(stream));
585  BUG_ON(frame_abs_offset > STREAM_APP_PROGRESS(stream) + stream_slice->input_len);
586 #endif
587  Frame *r = FrameNew(frames, frame_abs_offset, len);
588  if (r != NULL) {
589  r->type = frame_type;
590  }
591  return r;
592 }
593 
595 {
596 #ifdef DEBUG
597  if (f->proto == IPPROTO_TCP && f->protoctx && f->alparser) {
598  FramesContainer *frames_container = AppLayerFramesGetContainer(f);
599  if (frames_container != NULL) {
600  AppLayerFrameDumpForFrames("toserver::dump", &frames_container->toserver);
601  AppLayerFrameDumpForFrames("toclient::dump", &frames_container->toclient);
602  }
603  }
604 #endif
605 }
606 
607 /** \brief create new frame using the absolute offset from the start of the stream
608  */
610  const uint64_t frame_start, const int64_t len, int dir, uint8_t frame_type)
611 {
612  if (!(FrameConfigTypeIsEnabled(f->alproto, frame_type)))
613  return NULL;
614 
615  /* workarounds for many (unit|fuzz)tests not handling TCP data properly */
616 #if defined(UNITTESTS) || defined(FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION)
617  if (f->proto == IPPROTO_TCP && f->protoctx == NULL)
618  return NULL;
619  if (stream_slice->input == NULL)
620  return NULL;
621 #else
622  DEBUG_VALIDATE_BUG_ON(stream_slice->input == NULL);
623 #endif
624  DEBUG_VALIDATE_BUG_ON(f->proto == IPPROTO_TCP && f->protoctx == NULL);
625  DEBUG_VALIDATE_BUG_ON(f->alparser == NULL);
626  DEBUG_VALIDATE_BUG_ON(frame_start < stream_slice->offset);
627  DEBUG_VALIDATE_BUG_ON(frame_start - stream_slice->offset >= (uint64_t)INT_MAX);
628 
629  FramesContainer *frames_container = AppLayerFramesSetupContainer(f);
630  if (frames_container == NULL)
631  return NULL;
632 
633  Frames *frames;
634  if (dir == 0) {
635  frames = &frames_container->toserver;
636  } else {
637  frames = &frames_container->toclient;
638  }
639 
640  SCLogDebug("flow %p direction %s frame type %u offset %" PRIu64 " len %" PRIi64
641  " (slice offset %" PRIu64 ")",
642  f, dir == 0 ? "toserver" : "toclient", frame_type, frame_start, len,
643  stream_slice->offset);
644  Frame *r = FrameNew(frames, frame_start, len);
645  if (r != NULL) {
646  r->type = frame_type;
647  }
648  return r;
649 }
650 
651 void AppLayerFrameAddEvent(Frame *r, uint8_t e)
652 {
653  if (r != NULL) {
654  if (r->event_cnt < 4) { // TODO
655  r->events[r->event_cnt++] = e;
656  }
657  FrameDebug("add_event", NULL, r);
658  }
659 }
660 
661 void SCAppLayerFrameAddEventById(const Flow *f, const int dir, const FrameId id, uint8_t e)
662 {
663  Frame *frame = AppLayerFrameGetById(f, dir, id);
664  AppLayerFrameAddEvent(frame, e);
665 }
666 
667 void AppLayerFrameSetLength(Frame *frame, int64_t len)
668 {
669  if (frame != NULL) {
670  frame->len = len;
671  FrameDebug("set_length", NULL, frame);
672  }
673 }
674 
675 void SCAppLayerFrameSetLengthById(const Flow *f, const int dir, const FrameId id, int64_t len)
676 {
677  Frame *frame = AppLayerFrameGetById(f, dir, id);
678  AppLayerFrameSetLength(frame, len);
679 }
680 
681 void AppLayerFrameSetTxId(Frame *r, uint64_t tx_id)
682 {
683  if (r != NULL) {
685  r->tx_id = tx_id;
686  FrameDebug("set_txid", NULL, r);
687  }
688 }
689 
690 void SCAppLayerFrameSetTxIdById(const Flow *f, const int dir, const FrameId id, uint64_t tx_id)
691 {
692  Frame *frame = AppLayerFrameGetById(f, dir, id);
693  AppLayerFrameSetTxId(frame, tx_id);
694 }
695 
696 Frame *AppLayerFrameGetById(const Flow *f, const int dir, const FrameId frame_id)
697 {
698  FramesContainer *frames_container = AppLayerFramesGetContainer(f);
699  SCLogDebug("get frame_id %" PRIi64 " direction %u/%s frames_container %p", frame_id, dir,
700  dir == 0 ? "toserver" : "toclient", frames_container);
701  if (frames_container == NULL)
702  return NULL;
703 
704  Frames *frames;
705  if (dir == 0) {
706  frames = &frames_container->toserver;
707  } else {
708  frames = &frames_container->toclient;
709  }
710  SCLogDebug("frames %p", frames);
711  return FrameGetById(frames, frame_id);
712 }
713 
714 Frame *AppLayerFrameGetLastOpenByType(Flow *f, const int dir, const uint8_t frame_type)
715 {
716  if (!(FrameConfigTypeIsEnabled(f->alproto, frame_type)))
717  return NULL;
718 
719  FramesContainer *frames_container = AppLayerFramesGetContainer(f);
720  SCLogDebug("get frame_type %" PRIu8 " direction %u/%s frames_container %p", frame_type, dir,
721  dir == 0 ? "toserver" : "toclient", frames_container);
722  if (frames_container == NULL)
723  return NULL;
724 
725  Frames *frames;
726  if (dir == 0) {
727  frames = &frames_container->toserver;
728  } else {
729  frames = &frames_container->toclient;
730  }
731  SCLogDebug("frames %p", frames);
732  return FrameGetLastOpenByType(frames, frame_type);
733 }
734 
735 static inline bool FrameIsDone(const Frame *frame, const uint64_t abs_right_edge)
736 {
737  /* frame with negative length means we don't know the size yet. */
738  if (frame->len < 0)
739  return false;
740 
741  const int64_t frame_abs_offset = frame->offset;
742  const int64_t frame_right_edge = frame_abs_offset + frame->len;
743  if ((uint64_t)frame_right_edge <= abs_right_edge) {
744  SCLogDebug("frame %p id %" PRIi64 " is done", frame, frame->id);
745  return true;
746  }
747  return false;
748 }
749 
750 static void FramePrune(Frames *frames, const TcpStream *stream, const bool eof)
751 {
752 #ifdef DEBUG_VALIDATION
753  const uint64_t frames_le_start = (uint64_t)frames->left_edge_rel + STREAM_BASE_OFFSET(stream);
754 #endif
755  SCLogDebug("start: left edge %" PRIu64 ", left_edge_rel %u, stream base %" PRIu64,
756  (uint64_t)frames->left_edge_rel + STREAM_BASE_OFFSET(stream), frames->left_edge_rel,
757  STREAM_BASE_OFFSET(stream));
758  const uint64_t acked = StreamTcpGetUsable(stream, eof);
759  uint64_t le = STREAM_APP_PROGRESS(stream);
760 
761 #if defined(DEBUG) || defined(DEBUG_VALIDATION)
762  const uint16_t start = frames->cnt;
763  uint16_t removed = 0;
764 #endif
765  uint16_t x = 0;
766  for (uint16_t i = 0; i < frames->cnt; i++) {
767  if (i < FRAMES_STATIC_CNT) {
768  Frame *frame = &frames->sframes[i];
769  FrameDebug("prune(s)", frames, frame);
770  if (eof || FrameIsDone(frame, acked)) {
771  // remove by not incrementing 'x'
772  SCLogDebug("removing %p id %" PRIi64, frame, frame->id);
773  FrameDebug("remove(s)", frames, frame);
774  FrameClean(frame);
775 #if defined(DEBUG) || defined(DEBUG_VALIDATION)
776  removed++;
777 #endif
778  } else {
779  const uint64_t fle = FrameLeftEdge(stream, frame);
780  le = MIN(le, fle);
781  SCLogDebug("le %" PRIu64 ", frame fle %" PRIu64, le, fle);
782  Frame *nframe = &frames->sframes[x];
783  FrameCopy(nframe, frame);
784  if (frame != nframe) {
785  FrameClean(frame);
786  }
787  x++;
788  }
789  } else {
790  const uint16_t o = i - FRAMES_STATIC_CNT;
791  Frame *frame = &frames->dframes[o];
792  FrameDebug("prune(d)", frames, frame);
793  if (eof || FrameIsDone(frame, acked)) {
794  // remove by not incrementing 'x'
795  SCLogDebug("removing %p id %" PRIi64, frame, frame->id);
796  FrameDebug("remove(d)", frames, frame);
797  FrameClean(frame);
798 #if defined(DEBUG) || defined(DEBUG_VALIDATION)
799  removed++;
800 #endif
801  } else {
802  const uint64_t fle = FrameLeftEdge(stream, frame);
803  le = MIN(le, fle);
804  SCLogDebug("le %" PRIu64 ", frame fle %" PRIu64, le, fle);
805  Frame *nframe;
806  if (x >= FRAMES_STATIC_CNT) {
807  nframe = &frames->dframes[x - FRAMES_STATIC_CNT];
808  } else {
809  nframe = &frames->sframes[x];
810  }
811  FrameCopy(nframe, frame);
812  if (frame != nframe) {
813  FrameClean(frame);
814  }
815  x++;
816  }
817  }
818  }
819  frames->cnt = x;
821  DEBUG_VALIDATE_BUG_ON(le - STREAM_BASE_OFFSET(stream) > UINT32_MAX);
822  frames->left_edge_rel = (uint32_t)(le - STREAM_BASE_OFFSET(stream));
823 #ifdef DEBUG
824  SCLogDebug("end: left edge %" PRIu64 ", left_edge_rel %u, stream base %" PRIu64
825  ", cnt %u, removed %u, start %u",
826  (uint64_t)frames->left_edge_rel + STREAM_BASE_OFFSET(stream), frames->left_edge_rel,
827  STREAM_BASE_OFFSET(stream), frames->cnt, removed, start);
828  AppLayerFrameDumpForFrames("post_slide", frames);
829 #endif
830  if (frames->cnt > 0) { // if we removed all this can fail
831  DEBUG_VALIDATE_BUG_ON(frames_le_start > le);
832  }
833  DEBUG_VALIDATE_BUG_ON(x != start - removed);
834 }
835 
836 void FramesPrune(Flow *f, Packet *p)
837 {
838  if (f->proto == IPPROTO_TCP && f->protoctx == NULL)
839  return;
840  FramesContainer *frames_container = AppLayerFramesGetContainer(f);
841  if (frames_container == NULL)
842  return;
843 
844  Frames *frames;
845 
846  if (p->proto == IPPROTO_UDP) {
847  SCLogDebug("clearing all UDP frames");
848  if (PKT_IS_TOSERVER(p)) {
849  frames = &frames_container->toserver;
850  } else {
851  frames = &frames_container->toclient;
852  }
853  FramesClear(frames);
854  return;
855  }
856 
857  TcpSession *ssn = f->protoctx;
858 
861  return;
862  }
863 
864  TcpStream *stream;
865  if (PKT_IS_TOSERVER(p)) {
866  stream = &ssn->client;
867  frames = &frames_container->toserver;
868  } else {
869  stream = &ssn->server;
870  frames = &frames_container->toclient;
871  }
872 
873  const bool eof = ssn->state == TCP_CLOSED || PKT_IS_PSEUDOPKT(p);
874  SCLogDebug("eof %s", eof ? "TRUE" : "false");
875  FramePrune(frames, stream, eof);
876 }
Packet_::proto
uint8_t proto
Definition: decode.h:523
TcpStream_
Definition: stream-tcp-private.h:106
Frame::inspect_progress
uint64_t inspect_progress
Definition: app-layer-frames.h:53
len
uint8_t len
Definition: app-layer-dnp3.h:2
SCAppLayerFrameAddEventById
void SCAppLayerFrameAddEventById(const Flow *f, const int dir, const FrameId id, uint8_t e)
Definition: app-layer-frames.c:661
Frame::tx_id
uint64_t tx_id
Definition: app-layer-frames.h:52
offset
uint64_t offset
Definition: util-streaming-buffer.h:0
FrameConfig::SC_ATOMIC_DECLARE
SC_ATOMIC_DECLARE(uint64_t, types)
SC_ATOMIC_INIT
#define SC_ATOMIC_INIT(name)
wrapper for initializing an atomic variable.
Definition: util-atomic.h:314
FramesFree
void FramesFree(Frames *frames)
Definition: app-layer-frames.c:454
AppLayerFrameAddEvent
void AppLayerFrameAddEvent(Frame *r, uint8_t e)
Definition: app-layer-frames.c:651
PKT_IS_PSEUDOPKT
#define PKT_IS_PSEUDOPKT(p)
return 1 if the packet is a pseudo packet
Definition: decode.h:1323
stream-tcp.h
StreamSlice
struct StreamSlice StreamSlice
Definition: app-layer-parser.h:38
unlikely
#define unlikely(expr)
Definition: util-optimize.h:35
AppLayerFramesGetContainer
FramesContainer * AppLayerFramesGetContainer(const Flow *f)
Definition: app-layer-parser.c:184
SCLogDebug
#define SCLogDebug(...)
Definition: util-debug.h:279
Frame::events
uint8_t events[4]
Definition: app-layer-frames.h:48
Flow_::proto
uint8_t proto
Definition: flow.h:370
AppProto
uint16_t AppProto
Definition: app-layer-protos.h:86
FramesContainer::toserver
Frames toserver
Definition: app-layer-frames.h:72
AppLayerFramesSetupContainer
FramesContainer * AppLayerFramesSetupContainer(Flow *f)
Definition: app-layer-parser.c:191
Frame::offset
uint64_t offset
Definition: app-layer-frames.h:49
Frame
Definition: app-layer-frames.h:43
Flow_
Flow data structure.
Definition: flow.h:348
FrameConfig
Definition: app-layer-frames.c:34
SCAppLayerFrameSetLengthById
void SCAppLayerFrameSetLengthById(const Flow *f, const int dir, const FrameId id, int64_t len)
Definition: app-layer-frames.c:675
Frames::cnt
uint16_t cnt
Definition: app-layer-frames.h:59
AppLayerFrameSetTxId
void AppLayerFrameSetTxId(Frame *r, uint64_t tx_id)
Definition: app-layer-frames.c:681
Frame::id
int64_t id
Definition: app-layer-frames.h:51
FrameGetByIndex
Frame * FrameGetByIndex(Frames *frames, const uint32_t idx)
Definition: app-layer-frames.c:145
rust.h
MIN
#define MIN(x, y)
Definition: suricata-common.h:408
Frames
Definition: app-layer-frames.h:58
FramesContainer
Definition: app-layer-frames.h:71
Frames::left_edge_rel
uint32_t left_edge_rel
Definition: app-layer-frames.h:61
AppLayerFrameNewByAbsoluteOffset
Frame * AppLayerFrameNewByAbsoluteOffset(Flow *f, const StreamSlice *stream_slice, const uint64_t frame_start, const int64_t len, int dir, uint8_t frame_type)
create new frame using the absolute offset from the start of the stream
Definition: app-layer-frames.c:609
AppLayerFrameGetLastOpenByType
Frame * AppLayerFrameGetLastOpenByType(Flow *f, const int dir, const uint8_t frame_type)
Definition: app-layer-frames.c:714
Flow_::protoctx
void * protoctx
Definition: flow.h:433
StreamTcpGetUsable
uint64_t StreamTcpGetUsable(const TcpStream *stream, const bool eof)
Definition: stream-tcp-reassemble.c:426
Frames::dframes
Frame * dframes
Definition: app-layer-frames.h:64
TcpSession_::flags
uint32_t flags
Definition: stream-tcp-private.h:294
Flow_::alparser
AppLayerParserState * alparser
Definition: flow.h:470
FramesContainer::toclient
Frames toclient
Definition: app-layer-frames.h:73
PKT_IS_TOSERVER
#define PKT_IS_TOSERVER(p)
Definition: decode.h:238
AppLayerParserGetFrameNameById
const char * AppLayerParserGetFrameNameById(uint8_t ipproto, AppProto alproto, const uint8_t id)
Definition: app-layer-parser.c:1622
g_alproto_max
AppProto g_alproto_max
Definition: app-layer-protos.c:30
AppLayerFramesFreeContainer
void AppLayerFramesFreeContainer(Flow *f)
Definition: app-layer-parser.c:176
FrameGetById
Frame * FrameGetById(Frames *frames, const int64_t id)
Definition: app-layer-frames.c:125
FRAME_FLAG_TX_ID_SET
#define FRAME_FLAG_TX_ID_SET
Definition: app-layer-frames.h:36
STREAM_BASE_OFFSET
#define STREAM_BASE_OFFSET(stream)
Definition: stream-tcp-private.h:144
util-print.h
SCAppLayerFrameNewByRelativeOffset
Frame * SCAppLayerFrameNewByRelativeOffset(Flow *f, const void *ss, const uint32_t frame_start_rel, const int64_t len, int dir, uint8_t frame_type)
create new frame using a relative offset from the start of the stream slice
Definition: app-layer-frames.c:543
AppLayerFrameGetById
Frame * AppLayerFrameGetById(const Flow *f, const int dir, const FrameId frame_id)
Definition: app-layer-frames.c:696
TcpSession_::state
uint8_t state
Definition: stream-tcp-private.h:285
FrameId
int64_t FrameId
Definition: app-layer-frames.h:32
AppLayerFramesSlide
void AppLayerFramesSlide(Flow *f, const uint32_t slide, const uint8_t direction)
Definition: app-layer-frames.c:409
app-layer-parser.h
BUG_ON
#define BUG_ON(x)
Definition: suricata-common.h:317
FRAMES_STATIC_CNT
#define FRAMES_STATIC_CNT
Definition: app-layer-frames.h:56
Packet_
Definition: decode.h:501
type
uint16_t type
Definition: decode-vlan.c:106
Frame::len
int64_t len
Definition: app-layer-frames.h:50
Frame
struct Frame Frame
BIT_U64
#define BIT_U64(n)
Definition: suricata-common.h:418
SCAppLayerFrameSetTxIdById
void SCAppLayerFrameSetTxIdById(const Flow *f, const int dir, const FrameId id, uint64_t tx_id)
Definition: app-layer-frames.c:690
Frame::flags
uint8_t flags
Definition: app-layer-frames.h:45
SCRealloc
#define SCRealloc(ptr, sz)
Definition: util-mem.h:50
TCP_CLOSED
@ TCP_CLOSED
Definition: stream-tcp-private.h:162
app-layer-frames.h
Frame::type
uint8_t type
Definition: app-layer-frames.h:44
suricata-common.h
Frame::event_cnt
uint8_t event_cnt
Definition: app-layer-frames.h:46
FatalError
#define FatalError(...)
Definition: util-debug.h:514
Frames::sframes
Frame sframes[FRAMES_STATIC_CNT]
Definition: app-layer-frames.h:63
AppLayerFrameNewByPointer
Frame * AppLayerFrameNewByPointer(Flow *f, const StreamSlice *stream_slice, const uint8_t *frame_start, const int64_t len, int dir, uint8_t frame_type)
create new frame using a pointer to start of the frame
Definition: app-layer-frames.c:464
FrameConfigEnableAll
void FrameConfigEnableAll(void)
Definition: app-layer-frames.c:56
TcpSession_::client
TcpStream client
Definition: stream-tcp-private.h:297
FrameConfigInit
void FrameConfigInit(void)
Definition: app-layer-frames.c:40
FRAME_STREAM_TYPE
#define FRAME_STREAM_TYPE
Definition: app-layer-frames.h:30
TcpSession_::server
TcpStream server
Definition: stream-tcp-private.h:296
FramesPrune
void FramesPrune(Flow *f, Packet *p)
Definition: app-layer-frames.c:836
SCFree
#define SCFree(p)
Definition: util-mem.h:61
FrameGetLastOpenByType
Frame * FrameGetLastOpenByType(Frames *frames, const uint8_t frame_type)
Definition: app-layer-frames.c:102
Frames::base_id
uint64_t base_id
Definition: app-layer-frames.h:62
src
uint16_t src
Definition: app-layer-dnp3.h:5
FrameConfigEnable
void FrameConfigEnable(const AppProto p, const uint8_t type)
Definition: app-layer-frames.c:65
AppLayerFrameDump
void AppLayerFrameDump(Flow *f)
Definition: app-layer-frames.c:594
STREAMTCP_FLAG_APP_LAYER_DISABLED
#define STREAMTCP_FLAG_APP_LAYER_DISABLED
Definition: stream-tcp-private.h:201
STREAM_APP_PROGRESS
#define STREAM_APP_PROGRESS(stream)
Definition: stream-tcp-private.h:145
dst
uint16_t dst
Definition: app-layer-dnp3.h:4
SC_ATOMIC_GET
#define SC_ATOMIC_GET(name)
Get the value from the atomic variable.
Definition: util-atomic.h:375
TcpSession_
Definition: stream-tcp-private.h:283
FrameConfigDeInit
void FrameConfigDeInit(void)
Definition: app-layer-frames.c:51
flow.h
Flow_::alproto
AppProto alproto
application level protocol
Definition: flow.h:442
SCCalloc
#define SCCalloc(nm, sz)
Definition: util-mem.h:53
DEBUG_VALIDATE_BUG_ON
#define DEBUG_VALIDATE_BUG_ON(exp)
Definition: util-validate.h:102
Frames::dyn_size
uint16_t dyn_size
Definition: app-layer-frames.h:60
AppLayerFrameSetLength
void AppLayerFrameSetLength(Frame *frame, int64_t len)
Definition: app-layer-frames.c:667
SC_ATOMIC_OR
#define SC_ATOMIC_OR(name, val)
Bitwise OR a value to our atomic variable.
Definition: util-atomic.h:350
FrameDebug
#define FrameDebug(prefix, frames, frame)
Definition: app-layer-frames.c:96