forked from OISF/suricata
-
Notifications
You must be signed in to change notification settings - Fork 0
/
decode-ipv4.c
1678 lines (1515 loc) · 53.1 KB
/
decode-ipv4.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* Copyright (C) 2007-2013 Open Information Security Foundation
*
* You can copy, redistribute or modify this Program under the terms of
* the GNU General Public License version 2 as published by the Free
* Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* version 2 along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301, USA.
*/
/**
* \ingroup decode
*
* @{
*/
/**
* \file
*
* \author Victor Julien <[email protected]>
* \author Brian Rectanus <[email protected]>
*
* Decode IPv4
*/
#include "suricata-common.h"
#include "packet-queue.h"
#include "decode.h"
#include "decode-ipv4.h"
#include "decode-events.h"
#include "defrag.h"
#include "pkt-var.h"
#include "host.h"
#include "util-unittest.h"
#include "util-debug.h"
#include "util-optimize.h"
#include "util-print.h"
#include "util-profiling.h"
/* Generic validation
*
* [--type--][--len---]
*
* \todo This function needs removed in favor of specific validation.
*
* See: RFC 791
*/
static int IPV4OptValidateGeneric(Packet *p, const IPV4Opt *o)
{
switch (o->type) {
/* See: RFC 4782 */
case IPV4_OPT_QS:
if (o->len < IPV4_OPT_QS_MIN) {
ENGINE_SET_INVALID_EVENT(p, IPV4_OPT_INVALID_LEN);
return -1;
}
break;
/* See: RFC 1108 */
case IPV4_OPT_SEC:
if (o->len != IPV4_OPT_SEC_LEN) {
ENGINE_SET_INVALID_EVENT(p, IPV4_OPT_INVALID_LEN);
return -1;
}
break;
case IPV4_OPT_SID:
if (o->len != IPV4_OPT_SID_LEN) {
ENGINE_SET_INVALID_EVENT(p, IPV4_OPT_INVALID_LEN);
return -1;
}
break;
/* See: RFC 2113 */
case IPV4_OPT_RTRALT:
if (o->len != IPV4_OPT_RTRALT_LEN) {
ENGINE_SET_INVALID_EVENT(p, IPV4_OPT_INVALID_LEN);
return -1;
}
break;
default:
/* Should never get here unless there is a coding error */
ENGINE_SET_INVALID_EVENT(p, IPV4_OPT_UNKNOWN);
return -1;
}
return 0;
}
/* Validate route type options
*
* [--type--][--len---][--ptr---][address1]...[addressN]
*
* See: RFC 791
*/
static int IPV4OptValidateRoute(Packet *p, const IPV4Opt *o)
{
uint8_t ptr;
/* Check length */
if (unlikely(o->len < IPV4_OPT_ROUTE_MIN)) {
ENGINE_SET_INVALID_EVENT(p, IPV4_OPT_INVALID_LEN);
return -1;
}
/* Data is required */
if (unlikely(o->data == NULL)) {
ENGINE_SET_INVALID_EVENT(p, IPV4_OPT_MALFORMED);
return -1;
}
ptr = *o->data;
/* Address pointer is 1 based and points at least after type+len+ptr,
* must be a incremented by 4 bytes (address size) and cannot extend
* past option length.
*/
if (unlikely((ptr < 4) || (ptr % 4) || (ptr > o->len + 1))) {
ENGINE_SET_INVALID_EVENT(p, IPV4_OPT_MALFORMED);
return -1;
}
return 0;
}
/* Validate timestamp type options
*
* [--type--][--len---][--ptr---][ovfl][flag][rec1----...]...[recN----...]
* NOTE: rec could be 4 (ts only) or 8 (ip+ts) bytes in length.
*
* See: RFC 781
*/
static int IPV4OptValidateTimestamp(Packet *p, const IPV4Opt *o)
{
uint8_t ptr;
uint8_t flag;
uint8_t rec_size;
/* Check length */
if (unlikely(o->len < IPV4_OPT_TS_MIN)) {
ENGINE_SET_INVALID_EVENT(p, IPV4_OPT_INVALID_LEN);
return -1;
}
/* Data is required */
if (unlikely(o->data == NULL)) {
ENGINE_SET_INVALID_EVENT(p, IPV4_OPT_MALFORMED);
return -1;
}
ptr = *o->data;
/* We need the flag to determine what is in the option payload */
if (unlikely(ptr < 5)) {
ENGINE_SET_INVALID_EVENT(p, IPV4_OPT_MALFORMED);
return -1;
}
flag = *(o->data + 3) & 0x00ff;
/* A flag of 1|3 means we have both the ip+ts in each record */
rec_size = ((flag == 1) || (flag == 3)) ? 8 : 4;
/* Address pointer is 1 based and points at least after
* type+len+ptr+ovfl+flag, must be incremented by by the rec_size
* and cannot extend past option length.
*/
if (unlikely(((ptr - 5) % rec_size) || (ptr > o->len + 1))) {
ENGINE_SET_INVALID_EVENT(p, IPV4_OPT_MALFORMED);
return -1;
}
return 0;
}
/* Validate CIPSO option
*
* [--type--][--len---][--doi---][tags--...]
*
* See: draft-ietf-cipso-ipsecurity-01.txt
* See: FIPS 188 (tags 6 & 7)
*/
static int IPV4OptValidateCIPSO(Packet *p, const IPV4Opt *o)
{
// uint32_t doi;
uint8_t *tag;
uint16_t len;
/* Check length */
if (unlikely(o->len < IPV4_OPT_CIPSO_MIN)) {
ENGINE_SET_INVALID_EVENT(p, IPV4_OPT_INVALID_LEN);
return -1;
}
/* Data is required */
if (unlikely(o->data == NULL)) {
ENGINE_SET_INVALID_EVENT(p, IPV4_OPT_MALFORMED);
return -1;
}
// doi = *o->data;
tag = o->data + 4;
len = o->len - 1 - 1 - 4; /* Length of tags after header */
#if 0
/* Domain of Interest (DOI) of 0 is reserved and thus invalid */
/** \todo Aparently a DOI of zero is fine in practice - verify. */
if (doi == 0) {
ENGINE_SET_EVENT(p,IPV4_OPT_MALFORMED);
return -1;
}
#endif
/* NOTE: We know len has passed min tests prior to this call */
/* Check that tags are formatted correctly
* [-ttype--][--tlen--][-tagdata-...]
*/
while (len) {
uint8_t ttype;
uint8_t tlen;
/* Tag header must fit within option length */
if (unlikely(len < 2)) {
//printf("CIPSO tag header too large %" PRIu16 " < 2\n", len);
ENGINE_SET_INVALID_EVENT(p, IPV4_OPT_MALFORMED);
return -1;
}
/* Tag header is type+len */
ttype = *(tag++);
tlen = *(tag++);
/* Tag length must fit within the option length */
if (unlikely(tlen > len)) {
//printf("CIPSO tag len too large %" PRIu8 " > %" PRIu16 "\n", tlen, len);
ENGINE_SET_INVALID_EVENT(p, IPV4_OPT_MALFORMED);
return -1;
}
switch(ttype) {
case 1:
case 2:
case 5:
case 6:
case 7:
/* Tag is at least 4 and at most the remainder of option len */
if (unlikely((tlen < 4) || (tlen > len))) {
//printf("CIPSO tag %" PRIu8 " bad tlen=%" PRIu8 " len=%" PRIu8 "\n", ttype, tlen, len);
ENGINE_SET_INVALID_EVENT(p, IPV4_OPT_MALFORMED);
return -1;
}
/* The alignment octet is always 0 except tag
* type 7, which has no such field.
*/
if (unlikely((ttype != 7) && (*tag != 0))) {
//printf("CIPSO tag %" PRIu8 " ao=%" PRIu8 "\n", ttype, tlen);
ENGINE_SET_INVALID_EVENT(p, IPV4_OPT_MALFORMED);
return -1;
}
/* Skip the rest of the tag payload */
tag += tlen - 2;
len -= tlen;
continue;
case 0:
/* Tag type 0 is reserved and thus invalid */
/** \todo Wireshark marks this a padding, but spec says reserved. */
ENGINE_SET_INVALID_EVENT(p,IPV4_OPT_MALFORMED);
return -1;
default:
//printf("CIPSO tag %" PRIu8 " unknown tag\n", ttype);
ENGINE_SET_INVALID_EVENT(p, IPV4_OPT_MALFORMED);
/** \todo May not want to return error here on unknown tag type (at least not for 3|4) */
return -1;
}
}
return 0;
}
typedef struct IPV4Options_ {
IPV4Opt o_rr;
IPV4Opt o_qs;
IPV4Opt o_ts;
IPV4Opt o_sec;
IPV4Opt o_lsrr;
IPV4Opt o_cipso;
IPV4Opt o_sid;
IPV4Opt o_ssrr;
IPV4Opt o_rtralt;
} IPV4Options;
/**
* Decode/Validate IPv4 Options.
*/
static int DecodeIPV4Options(Packet *p, uint8_t *pkt, uint16_t len, IPV4Options *opts)
{
uint16_t plen = len;
#ifdef DEBUG
if (SCLogDebugEnabled()) {
uint16_t i;
char buf[256] = "";
int offset = 0;
for (i = 0; i < len; i++) {
offset += snprintf(buf + offset, (sizeof(buf) - offset), "%02" PRIx8 " ", pkt[i]);
}
SCLogDebug("IPV4OPTS: { %s}", buf);
}
#endif
/* Options length must be padded to 8byte boundary */
if (plen % 8) {
ENGINE_SET_EVENT(p,IPV4_OPT_PAD_REQUIRED);
/* Warn - we can keep going */
}
while (plen)
{
p->ip4vars.opt_cnt++;
/* single byte options */
if (*pkt == IPV4_OPT_EOL) {
/** \todo What if more data exist after EOL (possible covert channel or data leakage)? */
SCLogDebug("IPV4OPT %" PRIu16 " len 1 @ %" PRIu16 "/%" PRIu16 "",
*pkt, (len - plen), (len - 1));
p->ip4vars.opts_set |= IPV4_OPT_FLAG_EOL;
break;
} else if (*pkt == IPV4_OPT_NOP) {
SCLogDebug("IPV4OPT %" PRIu16 " len 1 @ %" PRIu16 "/%" PRIu16 "",
*pkt, (len - plen), (len - 1));
pkt++;
plen--;
p->ip4vars.opts_set |= IPV4_OPT_FLAG_NOP;
/* multibyte options */
} else {
if (unlikely(plen < 2)) {
/** \todo What if padding is non-zero (possible covert channel or data leakage)? */
/** \todo Spec seems to indicate EOL required if there is padding */
ENGINE_SET_EVENT(p,IPV4_OPT_EOL_REQUIRED);
break;
}
/* Option length is too big for packet */
if (unlikely(*(pkt+1) > plen)) {
ENGINE_SET_INVALID_EVENT(p, IPV4_OPT_INVALID_LEN);
return -1;
}
IPV4Opt opt = {*pkt, *(pkt+1), plen > 2 ? (pkt + 2) : NULL };
/* we already know that the total options len is valid,
* so here the len of the specific option must be bad.
* Also check for invalid lengths 0 and 1. */
if (unlikely(opt.len > plen || opt.len < 2)) {
ENGINE_SET_INVALID_EVENT(p, IPV4_OPT_INVALID_LEN);
return -1;
}
/* we are parsing the most commonly used opts to prevent
* us from having to walk the opts list for these all the
* time. */
/** \todo Figure out which IP options are more common and list them first */
switch (opt.type) {
case IPV4_OPT_TS:
if (opts->o_ts.type != 0) {
ENGINE_SET_EVENT(p,IPV4_OPT_DUPLICATE);
/* Warn - we can keep going */
break;
} else if (IPV4OptValidateTimestamp(p, &opt)) {
return -1;
}
opts->o_ts = opt;
p->ip4vars.opts_set |= IPV4_OPT_FLAG_TS;
break;
case IPV4_OPT_RR:
if (opts->o_rr.type != 0) {
ENGINE_SET_EVENT(p,IPV4_OPT_DUPLICATE);
/* Warn - we can keep going */
break;
} else if (IPV4OptValidateRoute(p, &opt) != 0) {
return -1;
}
opts->o_rr = opt;
p->ip4vars.opts_set |= IPV4_OPT_FLAG_RR;
break;
case IPV4_OPT_QS:
if (opts->o_qs.type != 0) {
ENGINE_SET_EVENT(p,IPV4_OPT_DUPLICATE);
/* Warn - we can keep going */
break;
} else if (IPV4OptValidateGeneric(p, &opt)) {
return -1;
}
opts->o_qs = opt;
p->ip4vars.opts_set |= IPV4_OPT_FLAG_QS;
break;
case IPV4_OPT_SEC:
if (opts->o_sec.type != 0) {
ENGINE_SET_EVENT(p,IPV4_OPT_DUPLICATE);
/* Warn - we can keep going */
break;
} else if (IPV4OptValidateGeneric(p, &opt)) {
return -1;
}
opts->o_sec = opt;
p->ip4vars.opts_set |= IPV4_OPT_FLAG_SEC;
break;
case IPV4_OPT_LSRR:
if (opts->o_lsrr.type != 0) {
ENGINE_SET_EVENT(p,IPV4_OPT_DUPLICATE);
/* Warn - we can keep going */
break;
} else if (IPV4OptValidateRoute(p, &opt) != 0) {
return -1;
}
opts->o_lsrr = opt;
p->ip4vars.opts_set |= IPV4_OPT_FLAG_LSRR;
break;
case IPV4_OPT_CIPSO:
if (opts->o_cipso.type != 0) {
ENGINE_SET_EVENT(p,IPV4_OPT_DUPLICATE);
/* Warn - we can keep going */
break;
} else if (IPV4OptValidateCIPSO(p, &opt) != 0) {
return -1;
}
opts->o_cipso = opt;
p->ip4vars.opts_set |= IPV4_OPT_FLAG_CIPSO;
break;
case IPV4_OPT_SID:
if (opts->o_sid.type != 0) {
ENGINE_SET_EVENT(p,IPV4_OPT_DUPLICATE);
/* Warn - we can keep going */
break;
} else if (IPV4OptValidateGeneric(p, &opt)) {
return -1;
}
opts->o_sid = opt;
p->ip4vars.opts_set |= IPV4_OPT_FLAG_SID;
break;
case IPV4_OPT_SSRR:
if (opts->o_ssrr.type != 0) {
ENGINE_SET_EVENT(p,IPV4_OPT_DUPLICATE);
/* Warn - we can keep going */
break;
} else if (IPV4OptValidateRoute(p, &opt) != 0) {
return -1;
}
opts->o_ssrr = opt;
p->ip4vars.opts_set |= IPV4_OPT_FLAG_SSRR;
break;
case IPV4_OPT_RTRALT:
if (opts->o_rtralt.type != 0) {
ENGINE_SET_EVENT(p,IPV4_OPT_DUPLICATE);
/* Warn - we can keep going */
break;
} else if (IPV4OptValidateGeneric(p, &opt)) {
return -1;
}
opts->o_rtralt = opt;
p->ip4vars.opts_set |= IPV4_OPT_FLAG_RTRALT;
break;
default:
SCLogDebug("IPV4OPT <unknown> (%" PRIu8 ") len %" PRIu8,
opt.type, opt.len);
ENGINE_SET_EVENT(p,IPV4_OPT_INVALID);
/* Warn - we can keep going */
break;
}
pkt += opt.len;
plen -= opt.len;
}
}
return 0;
}
static int DecodeIPV4Packet(Packet *p, uint8_t *pkt, uint16_t len)
{
if (unlikely(len < IPV4_HEADER_LEN)) {
ENGINE_SET_INVALID_EVENT(p, IPV4_PKT_TOO_SMALL);
return -1;
}
if (unlikely(IP_GET_RAW_VER(pkt) != 4)) {
SCLogDebug("wrong ip version %" PRIu8 "",IP_GET_RAW_VER(pkt));
ENGINE_SET_INVALID_EVENT(p, IPV4_WRONG_IP_VER);
return -1;
}
p->ip4h = (IPV4Hdr *)pkt;
if (unlikely(IPV4_GET_HLEN(p) < IPV4_HEADER_LEN)) {
ENGINE_SET_INVALID_EVENT(p, IPV4_HLEN_TOO_SMALL);
return -1;
}
if (unlikely(IPV4_GET_IPLEN(p) < IPV4_GET_HLEN(p))) {
ENGINE_SET_INVALID_EVENT(p, IPV4_IPLEN_SMALLER_THAN_HLEN);
return -1;
}
if (unlikely(len < IPV4_GET_IPLEN(p))) {
ENGINE_SET_INVALID_EVENT(p, IPV4_TRUNC_PKT);
return -1;
}
/* set the address struct */
SET_IPV4_SRC_ADDR(p,&p->src);
SET_IPV4_DST_ADDR(p,&p->dst);
/* save the options len */
uint8_t ip_opt_len = IPV4_GET_HLEN(p) - IPV4_HEADER_LEN;
if (ip_opt_len > 0) {
IPV4Options opts;
memset(&opts, 0x00, sizeof(opts));
DecodeIPV4Options(p, pkt + IPV4_HEADER_LEN, ip_opt_len, &opts);
}
return 0;
}
int DecodeIPV4(ThreadVars *tv, DecodeThreadVars *dtv, Packet *p, uint8_t *pkt, uint16_t len, PacketQueue *pq)
{
StatsIncr(tv, dtv->counter_ipv4);
SCLogDebug("pkt %p len %"PRIu16"", pkt, len);
/* do the actual decoding */
if (unlikely(DecodeIPV4Packet (p, pkt, len) < 0)) {
SCLogDebug("decoding IPv4 packet failed");
p->ip4h = NULL;
return TM_ECODE_FAILED;
}
p->proto = IPV4_GET_IPPROTO(p);
/* If a fragment, pass off for re-assembly. */
if (unlikely(IPV4_GET_IPOFFSET(p) > 0 || IPV4_GET_MF(p) == 1)) {
Packet *rp = Defrag(tv, dtv, p, pq);
if (rp != NULL) {
PacketEnqueue(pq, rp);
}
p->flags |= PKT_IS_FRAGMENT;
return TM_ECODE_OK;
}
/* do hdr test, process hdr rules */
#ifdef DEBUG
if (SCLogDebugEnabled()) { /* only convert the addresses if debug is really enabled */
/* debug print */
char s[16], d[16];
PrintInet(AF_INET, (const void *)GET_IPV4_SRC_ADDR_PTR(p), s, sizeof(s));
PrintInet(AF_INET, (const void *)GET_IPV4_DST_ADDR_PTR(p), d, sizeof(d));
SCLogDebug("IPV4 %s->%s PROTO: %" PRIu32 " OFFSET: %" PRIu32 " RF: %" PRIu32 " DF: %" PRIu32 " MF: %" PRIu32 " ID: %" PRIu32 "", s,d,
IPV4_GET_IPPROTO(p), IPV4_GET_IPOFFSET(p), IPV4_GET_RF(p),
IPV4_GET_DF(p), IPV4_GET_MF(p), IPV4_GET_IPID(p));
}
#endif /* DEBUG */
/* check what next decoder to invoke */
switch (IPV4_GET_IPPROTO(p)) {
case IPPROTO_TCP:
DecodeTCP(tv, dtv, p, pkt + IPV4_GET_HLEN(p),
IPV4_GET_IPLEN(p) - IPV4_GET_HLEN(p), pq);
break;
case IPPROTO_UDP:
DecodeUDP(tv, dtv, p, pkt + IPV4_GET_HLEN(p),
IPV4_GET_IPLEN(p) - IPV4_GET_HLEN(p), pq);
break;
case IPPROTO_ICMP:
DecodeICMPV4(tv, dtv, p, pkt + IPV4_GET_HLEN(p),
IPV4_GET_IPLEN(p) - IPV4_GET_HLEN(p), pq);
break;
case IPPROTO_GRE:
DecodeGRE(tv, dtv, p, pkt + IPV4_GET_HLEN(p),
IPV4_GET_IPLEN(p) - IPV4_GET_HLEN(p), pq);
break;
case IPPROTO_SCTP:
DecodeSCTP(tv, dtv, p, pkt + IPV4_GET_HLEN(p),
IPV4_GET_IPLEN(p) - IPV4_GET_HLEN(p), pq);
break;
case IPPROTO_IPV6:
{
if (pq != NULL) {
/* spawn off tunnel packet */
Packet *tp = PacketTunnelPktSetup(tv, dtv, p, pkt + IPV4_GET_HLEN(p),
IPV4_GET_IPLEN(p) - IPV4_GET_HLEN(p),
DECODE_TUNNEL_IPV6, pq);
if (tp != NULL) {
PKT_SET_SRC(tp, PKT_SRC_DECODER_IPV4);
PacketEnqueue(pq,tp);
}
}
break;
}
case IPPROTO_IP:
/* check PPP VJ uncompressed packets and decode tcp dummy */
if(p->ppph != NULL && ntohs(p->ppph->protocol) == PPP_VJ_UCOMP) {
DecodeTCP(tv, dtv, p, pkt + IPV4_GET_HLEN(p),
IPV4_GET_IPLEN(p) - IPV4_GET_HLEN(p), pq);
}
break;
case IPPROTO_ICMPV6:
ENGINE_SET_INVALID_EVENT(p, IPV4_WITH_ICMPV6);
break;
}
return TM_ECODE_OK;
}
/* UNITTESTS */
#ifdef UNITTESTS
/** \test IPV4 with no options. */
int DecodeIPV4OptionsNONETest01(void)
{
uint8_t raw_opts[] = { };
Packet *p = PacketGetFromAlloc();
FAIL_IF(unlikely(p == NULL));
IPV4Options opts;
memset(&opts, 0x00, sizeof(opts));
int rc = DecodeIPV4Options(p, raw_opts, sizeof(raw_opts), &opts);
FAIL_IF(rc != 0);
SCFree(p);
PASS;
}
/** \test IPV4 with EOL option. */
int DecodeIPV4OptionsEOLTest01(void)
{
uint8_t raw_opts[] = {
IPV4_OPT_EOL, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
};
Packet *p = PacketGetFromAlloc();
FAIL_IF(unlikely(p == NULL));
IPV4Options opts;
memset(&opts, 0x00, sizeof(opts));
int rc = DecodeIPV4Options(p, raw_opts, sizeof(raw_opts), &opts);
FAIL_IF (rc != 0);
SCFree(p);
PASS;
}
/** \test IPV4 with NOP option. */
int DecodeIPV4OptionsNOPTest01(void)
{
uint8_t raw_opts[] = {
IPV4_OPT_NOP, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
};
Packet *p = PacketGetFromAlloc();
FAIL_IF(unlikely(p == NULL));
IPV4Options opts;
memset(&opts, 0x00, sizeof(opts));
int rc = DecodeIPV4Options(p, raw_opts, sizeof(raw_opts), &opts);
FAIL_IF (rc != 0);
SCFree(p);
PASS;
}
/** \test IPV4 with RR option. */
int DecodeIPV4OptionsRRTest01(void)
{
uint8_t raw_opts[] = {
IPV4_OPT_RR, 0x27, 0x08, 0xc0, 0xa8, 0x2a, 0x64, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
};
Packet *p = PacketGetFromAlloc();
FAIL_IF(unlikely(p == NULL));
IPV4Options opts;
memset(&opts, 0x00, sizeof(opts));
int rc = DecodeIPV4Options(p, raw_opts, sizeof(raw_opts), &opts);
FAIL_IF(rc != 0);
FAIL_IF(opts.o_rr.type != IPV4_OPT_RR);
SCFree(p);
PASS;
}
/** \test IPV4 with RR option (len too large). */
int DecodeIPV4OptionsRRTest02(void)
{
uint8_t raw_opts[] = {
IPV4_OPT_RR, 0xff, 0x08, 0xc0, 0xa8, 0x2a, 0x64, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
};
Packet *p = PacketGetFromAlloc();
FAIL_IF(unlikely(p == NULL));
IPV4Options opts;
memset(&opts, 0x00, sizeof(opts));
int rc = DecodeIPV4Options(p, raw_opts, sizeof(raw_opts), &opts);
FAIL_IF(rc == 0);
FAIL_IF(opts.o_rr.type != 0);
SCFree(p);
PASS;
}
/** \test IPV4 with RR option (ptr too large). */
int DecodeIPV4OptionsRRTest03(void)
{
uint8_t raw_opts[] = {
IPV4_OPT_RR, 0x27, 0xff, 0xc0, 0xa8, 0x2a, 0x64, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
};
Packet *p = PacketGetFromAlloc();
FAIL_IF(unlikely(p == NULL));
IPV4Options opts;
memset(&opts, 0x00, sizeof(opts));
int rc = DecodeIPV4Options(p, raw_opts, sizeof(raw_opts), &opts);
FAIL_IF(rc == 0);
FAIL_IF(opts.o_rr.type != 0);
SCFree(p);
PASS;
}
/** \test IPV4 with RR option (ptr not in 4 byte increment). */
int DecodeIPV4OptionsRRTest04(void)
{
uint8_t raw_opts[] = {
IPV4_OPT_RR, 0x27, 0x05, 0xc0, 0xa8, 0x2a, 0x64, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
};
Packet *p = PacketGetFromAlloc();
FAIL_IF(unlikely(p == NULL));
IPV4Options opts;
memset(&opts, 0x00, sizeof(opts));
int rc = DecodeIPV4Options(p, raw_opts, sizeof(raw_opts), &opts);
FAIL_IF(rc == 0);
FAIL_IF(opts.o_rr.type != 0);
SCFree(p);
PASS;
}
/** \test IPV4 with QS option. */
int DecodeIPV4OptionsQSTest01(void)
{
uint8_t raw_opts[] = {
IPV4_OPT_QS, 0x08, 0x0d, 0x00, 0xbe, 0xef, 0x00, 0x00
};
Packet *p = PacketGetFromAlloc();
FAIL_IF(unlikely(p == NULL));
IPV4Options opts;
memset(&opts, 0x00, sizeof(opts));
int rc = DecodeIPV4Options(p, raw_opts, sizeof(raw_opts), &opts);
FAIL_IF(rc != 0);
FAIL_IF(opts.o_qs.type != IPV4_OPT_QS);
SCFree(p);
PASS;
}
/** \test IPV4 with QS option (len too small) */
int DecodeIPV4OptionsQSTest02(void)
{
uint8_t raw_opts[] = {
IPV4_OPT_QS, 0x07, 0x0d, 0x00, 0xbe, 0xef, 0x00, 0x00
};
Packet *p = PacketGetFromAlloc();
FAIL_IF(unlikely(p == NULL));
IPV4Options opts;
memset(&opts, 0x00, sizeof(opts));
int rc = DecodeIPV4Options(p, raw_opts, sizeof(raw_opts), &opts);
FAIL_IF(rc == 0);
FAIL_IF(opts.o_qs.type != 0);
SCFree(p);
PASS;
}
/** \test IPV4 with TS option. */
int DecodeIPV4OptionsTSTest01(void)
{
uint8_t raw_opts[] = {
IPV4_OPT_TS, 0x24, 0x0d, 0x01, 0x0a, 0x0a, 0x0a, 0x69,
0x04, 0xce, 0x0d, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
};
Packet *p = PacketGetFromAlloc();
FAIL_IF(unlikely(p == NULL));
IPV4Options opts;
memset(&opts, 0x00, sizeof(opts));
int rc = DecodeIPV4Options(p, raw_opts, sizeof(raw_opts), &opts);
FAIL_IF(rc != 0);
FAIL_IF(opts.o_ts.type != IPV4_OPT_TS);
SCFree(p);
PASS;
}
/** \test IPV4 with TS option (ptr too small). */
int DecodeIPV4OptionsTSTest02(void)
{
uint8_t raw_opts[] = {
IPV4_OPT_TS, 0x24, 0x04, 0x01, 0x0a, 0x0a, 0x0a, 0x69,
0x04, 0xce, 0x0d, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
};
Packet *p = PacketGetFromAlloc();
FAIL_IF(unlikely(p == NULL));
IPV4Options opts;
memset(&opts, 0x00, sizeof(opts));
int rc = DecodeIPV4Options(p, raw_opts, sizeof(raw_opts), &opts);
FAIL_IF(rc == 0);
FAIL_IF(opts.o_ts.type != 0);
SCFree(p);
PASS;
}
/** \test IPV4 with TS option (ptr too large). */
int DecodeIPV4OptionsTSTest03(void)
{
uint8_t raw_opts[] = {
IPV4_OPT_TS, 0x24, 0xff, 0x01, 0x0a, 0x0a, 0x0a, 0x69,
0x04, 0xce, 0x0d, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
};
Packet *p = PacketGetFromAlloc();
FAIL_IF(unlikely(p == NULL));
IPV4Options opts;
memset(&opts, 0x00, sizeof(opts));
int rc = DecodeIPV4Options(p, raw_opts, sizeof(raw_opts), &opts);
FAIL_IF(rc == 0);
FAIL_IF(opts.o_ts.type != 0);
SCFree(p);
PASS;
}
/** \test IPV4 with TS option (ptr not valid). */
int DecodeIPV4OptionsTSTest04(void)
{
uint8_t raw_opts[] = {
IPV4_OPT_TS, 0x24, 0x0a, 0x01, 0x0a, 0x0a, 0x0a, 0x69,
0x04, 0xce, 0x0d, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
};
Packet *p = PacketGetFromAlloc();
FAIL_IF(unlikely(p == NULL));
IPV4Options opts;
memset(&opts, 0x00, sizeof(opts));
int rc = DecodeIPV4Options(p, raw_opts, sizeof(raw_opts), &opts);
FAIL_IF(rc == 0);
FAIL_IF(opts.o_ts.type != 0);
SCFree(p);
PASS;
}
/** \test IPV4 with SEC option. */
int DecodeIPV4OptionsSECTest01(void)
{
uint8_t raw_opts[] = {
IPV4_OPT_SEC, 0x0b, 0xf1, 0x35, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
};
Packet *p = PacketGetFromAlloc();
FAIL_IF(unlikely(p == NULL));
IPV4Options opts;
memset(&opts, 0x00, sizeof(opts));
int rc = DecodeIPV4Options(p, raw_opts, sizeof(raw_opts), &opts);
FAIL_IF(rc != 0);
FAIL_IF(opts.o_sec.type != IPV4_OPT_SEC);
SCFree(p);
PASS;
}
/** \test IPV4 with SEC option (invalid length). */
int DecodeIPV4OptionsSECTest02(void)
{
uint8_t raw_opts[] = {
IPV4_OPT_SEC, 0x0a, 0xf1, 0x35, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
};
Packet *p = PacketGetFromAlloc();
FAIL_IF(unlikely(p == NULL));
IPV4Options opts;
memset(&opts, 0x00, sizeof(opts));
int rc = DecodeIPV4Options(p, raw_opts, sizeof(raw_opts), &opts);
FAIL_IF(rc == 0);
FAIL_IF(opts.o_sec.type != 0);
SCFree(p);
PASS;
}
/** \test IPV4 with LSRR option. */
int DecodeIPV4OptionsLSRRTest01(void)
{
uint8_t raw_opts[] = {
IPV4_OPT_LSRR, 0x27, 0x08, 0xc0, 0xa8, 0x2a, 0x64, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
};
Packet *p = PacketGetFromAlloc();
FAIL_IF(unlikely(p == NULL));
IPV4Options opts;
memset(&opts, 0x00, sizeof(opts));
int rc = DecodeIPV4Options(p, raw_opts, sizeof(raw_opts), &opts);
FAIL_IF(rc != 0);
FAIL_IF(opts.o_lsrr.type != IPV4_OPT_LSRR);
SCFree(p);
PASS;
}
/** \test IPV4 with LSRR option (len too large). */
int DecodeIPV4OptionsLSRRTest02(void)
{
uint8_t raw_opts[] = {
IPV4_OPT_LSRR, 0xff, 0x08, 0xc0, 0xa8, 0x2a, 0x64, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
};
Packet *p = PacketGetFromAlloc();
FAIL_IF(unlikely(p == NULL));
IPV4Options opts;
memset(&opts, 0x00, sizeof(opts));
int rc = DecodeIPV4Options(p, raw_opts, sizeof(raw_opts), &opts);
FAIL_IF(rc == 0);
FAIL_IF(opts.o_lsrr.type != 0);
SCFree(p);
PASS;
}
/** \test IPV4 with LSRR option (ptr too large). */
int DecodeIPV4OptionsLSRRTest03(void)
{
uint8_t raw_opts[] = {
IPV4_OPT_LSRR, 0x27, 0xff, 0xc0, 0xa8, 0x2a, 0x64, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
};
Packet *p = PacketGetFromAlloc();
FAIL_IF(unlikely(p == NULL));
IPV4Options opts;
memset(&opts, 0x00, sizeof(opts));
int rc = DecodeIPV4Options(p, raw_opts, sizeof(raw_opts), &opts);
FAIL_IF(rc == 0);
FAIL_IF(opts.o_lsrr.type != 0);
SCFree(p);
PASS;
}
/** \test IPV4 with LSRR option (ptr not in 4 byte increment). */
int DecodeIPV4OptionsLSRRTest04(void)
{
uint8_t raw_opts[] = {
IPV4_OPT_LSRR, 0x27, 0x05, 0xc0, 0xa8, 0x2a, 0x64, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
};
Packet *p = PacketGetFromAlloc();
FAIL_IF(unlikely(p == NULL));