forked from nmap/nmap
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathPacketParser.cc
More file actions
1857 lines (1673 loc) · 73.7 KB
/
PacketParser.cc
File metadata and controls
1857 lines (1673 loc) · 73.7 KB
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
/***************************************************************************
* PacketParser.cc -- The PacketParser Class offers methods to parse *
* received network packets. Its main purpose is to facilitate the *
* conversion of raw sequences of bytes into chains of objects of the *
* PacketElement family. *
* *
***********************IMPORTANT NMAP LICENSE TERMS************************
* *
* The Nmap Security Scanner is (C) 1996-2016 Insecure.Com LLC. Nmap is *
* also a registered trademark of Insecure.Com LLC. This program is free *
* software; you may redistribute and/or modify it under the terms of the *
* GNU General Public License as published by the Free Software *
* Foundation; Version 2 ("GPL"), BUT ONLY WITH ALL OF THE CLARIFICATIONS *
* AND EXCEPTIONS DESCRIBED HEREIN. This guarantees your right to use, *
* modify, and redistribute this software under certain conditions. If *
* you wish to embed Nmap technology into proprietary software, we sell *
* alternative licenses (contact sales@nmap.com). Dozens of software *
* vendors already license Nmap technology such as host discovery, port *
* scanning, OS detection, version detection, and the Nmap Scripting *
* Engine. *
* *
* Note that the GPL places important restrictions on "derivative works", *
* yet it does not provide a detailed definition of that term. To avoid *
* misunderstandings, we interpret that term as broadly as copyright law *
* allows. For example, we consider an application to constitute a *
* derivative work for the purpose of this license if it does any of the *
* following with any software or content covered by this license *
* ("Covered Software"): *
* *
* o Integrates source code from Covered Software. *
* *
* o Reads or includes copyrighted data files, such as Nmap's nmap-os-db *
* or nmap-service-probes. *
* *
* o Is designed specifically to execute Covered Software and parse the *
* results (as opposed to typical shell or execution-menu apps, which will *
* execute anything you tell them to). *
* *
* o Includes Covered Software in a proprietary executable installer. The *
* installers produced by InstallShield are an example of this. Including *
* Nmap with other software in compressed or archival form does not *
* trigger this provision, provided appropriate open source decompression *
* or de-archiving software is widely available for no charge. For the *
* purposes of this license, an installer is considered to include Covered *
* Software even if it actually retrieves a copy of Covered Software from *
* another source during runtime (such as by downloading it from the *
* Internet). *
* *
* o Links (statically or dynamically) to a library which does any of the *
* above. *
* *
* o Executes a helper program, module, or script to do any of the above. *
* *
* This list is not exclusive, but is meant to clarify our interpretation *
* of derived works with some common examples. Other people may interpret *
* the plain GPL differently, so we consider this a special exception to *
* the GPL that we apply to Covered Software. Works which meet any of *
* these conditions must conform to all of the terms of this license, *
* particularly including the GPL Section 3 requirements of providing *
* source code and allowing free redistribution of the work as a whole. *
* *
* As another special exception to the GPL terms, Insecure.Com LLC grants *
* permission to link the code of this program with any version of the *
* OpenSSL library which is distributed under a license identical to that *
* listed in the included docs/licenses/OpenSSL.txt file, and distribute *
* linked combinations including the two. *
* *
* Any redistribution of Covered Software, including any derived works, *
* must obey and carry forward all of the terms of this license, including *
* obeying all GPL rules and restrictions. For example, source code of *
* the whole work must be provided and free redistribution must be *
* allowed. All GPL references to "this License", are to be treated as *
* including the terms and conditions of this license text as well. *
* *
* Because this license imposes special exceptions to the GPL, Covered *
* Work may not be combined (even as part of a larger work) with plain GPL *
* software. The terms, conditions, and exceptions of this license must *
* be included as well. This license is incompatible with some other open *
* source licenses as well. In some cases we can relicense portions of *
* Nmap or grant special permissions to use it in other open source *
* software. Please contact fyodor@nmap.org with any such requests. *
* Similarly, we don't incorporate incompatible open source software into *
* Covered Software without special permission from the copyright holders. *
* *
* If you have any questions about the licensing restrictions on using *
* Nmap in other works, are happy to help. As mentioned above, we also *
* offer alternative license to integrate Nmap into proprietary *
* applications and appliances. These contracts have been sold to dozens *
* of software vendors, and generally include a perpetual license as well *
* as providing for priority support and updates. They also fund the *
* continued development of Nmap. Please email sales@nmap.com for further *
* information. *
* *
* If you have received a written license agreement or contract for *
* Covered Software stating terms other than these, you may choose to use *
* and redistribute Covered Software under those terms instead of these. *
* *
* Source is provided to this software because we believe users have a *
* right to know exactly what a program is going to do before they run it. *
* This also allows you to audit the software for security holes. *
* *
* Source code also allows you to port Nmap to new platforms, fix bugs, *
* and add new features. You are highly encouraged to send your changes *
* to the dev@nmap.org mailing list for possible incorporation into the *
* main distribution. By sending these changes to Fyodor or one of the *
* Insecure.Org development mailing lists, or checking them into the Nmap *
* source code repository, it is understood (unless you specify otherwise) *
* that you are offering the Nmap Project (Insecure.Com LLC) the *
* unlimited, non-exclusive right to reuse, modify, and relicense the *
* code. Nmap will always be available Open Source, but this is important *
* because the inability to relicense code has caused devastating problems *
* for other Free Software projects (such as KDE and NASM). We also *
* occasionally relicense the code to third parties as discussed above. *
* If you wish to specify special license conditions of your *
* contributions, just say so when you send them. *
* *
* 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 Nmap *
* license file for more details (it's in a COPYING file included with *
* Nmap, and also available from https://svn.nmap.org/nmap/COPYING) *
* *
***************************************************************************/
/* This code was originally part of the Nping tool. */
#include "PacketParser.h"
#include <assert.h>
#define PKTPARSERDEBUG false
PacketParser::PacketParser() {
this->reset();
} /* End of PacketParser constructor */
PacketParser::~PacketParser() {
} /* End of PacketParser destructor */
/** Sets every attribute to its default value- */
void PacketParser::reset() {
} /* End of PacketParser destructor */
const char *PacketParser::header_type2string(int val){
header_type_string_t header_types[]={
{HEADER_TYPE_IPv6_HOPOPT, "IPv6 Hop-by-Hop"},
{HEADER_TYPE_ICMPv4,"ICMPv4"},
{HEADER_TYPE_IGMP,"IGMP"},
{HEADER_TYPE_IPv4,"IPv4"},
{HEADER_TYPE_TCP,"TCP"},
{HEADER_TYPE_EGP,"EGP"},
{HEADER_TYPE_UDP,"UDP"},
{HEADER_TYPE_IPv6,"IPv6"},
{HEADER_TYPE_IPv6_ROUTE,"IPv6-Route"},
{HEADER_TYPE_IPv6_FRAG,"IPv6-Frag"},
{HEADER_TYPE_GRE,"GRE"},
{HEADER_TYPE_ESP,"ESP"},
{HEADER_TYPE_AH,"AH"},
{HEADER_TYPE_ICMPv6,"ICMPv6"},
{HEADER_TYPE_IPv6_NONXT,"IPv6-NoNxt"},
{HEADER_TYPE_IPv6_OPTS,"IPv6-Opts"},
{HEADER_TYPE_EIGRP,"EIGRP"},
{HEADER_TYPE_ETHERNET,"Ethernet"},
{HEADER_TYPE_L2TP,"L2TP"},
{HEADER_TYPE_SCTP,"SCTP"},
{HEADER_TYPE_IPv6_MOBILE,"Mobility Header"},
{HEADER_TYPE_MPLS_IN_IP,"MPLS-in-IP"},
{HEADER_TYPE_ARP,"ARP"},
{HEADER_TYPE_RAW_DATA,"Raw Data"},
{0,NULL}
};
int i=0;
for(i=0; header_types[i].str!=NULL; i++ ){
if((int)header_types[i].type==val)
return header_types[i].str;
}
return NULL;
} /* End of header_type2string() */
#define MAX_HEADERS_IN_PACKET 32
pkt_type_t *PacketParser::parse_packet(const u8 *pkt, size_t pktlen, bool eth_included){
if(PKTPARSERDEBUG)printf("%s(%p, %lu)\n", __func__, pkt, (long unsigned)pktlen);
static pkt_type_t this_packet[MAX_HEADERS_IN_PACKET+1]; /* Packet structure array */
u8 current_header=0; /* Current array position of "this_packet" */
const u8 *curr_pkt=pkt; /* Pointer to current part of the packet */
size_t curr_pktlen=pktlen; /* Remaining packet length */
int ethlen=0, arplen=0; /* Aux length variables: link layer */
int iplen=0,ip6len=0; /* Aux length variables: network layer */
int tcplen=0,udplen=0,icmplen=0; /* Aux length variables: transport layer */
int exthdrlen=0; /* Aux length variables: extension headers */
int next_layer=0; /* Next header type to process */
int expected=0; /* Next protocol expected */
bool finished=false; /* Loop breaking flag */
bool unknown_hdr=false; /* Indicates unknown header found */
IPv4Header ip4;
IPv6Header ip6;
TCPHeader tcp;
UDPHeader udp;
ICMPv4Header icmp4;
ICMPv6Header icmp6;
EthernetHeader eth;
DestOptsHeader ext_dopts;
FragmentHeader ext_frag;
HopByHopHeader ext_hopt;
RoutingHeader ext_routing;
ARPHeader arp;
memset(this_packet, 0, sizeof(this_packet));
/* Decide which layer we have to start from */
if( eth_included ){
next_layer=LINK_LAYER;
expected=HEADER_TYPE_ETHERNET;
}else{
next_layer=NETWORK_LAYER;
}
/* Header processing loop */
while(!finished && curr_pktlen>0 && current_header<MAX_HEADERS_IN_PACKET){
/* Ethernet and ARP headers ***********************************************/
if(next_layer==LINK_LAYER ){
if(PKTPARSERDEBUG)puts("Next Layer=Link");
if(expected==HEADER_TYPE_ETHERNET){
if(PKTPARSERDEBUG)puts("Expected Layer=Ethernet");
if(eth.storeRecvData(curr_pkt, curr_pktlen)==OP_FAILURE){
unknown_hdr=true;
break;
}
if( (ethlen=eth.validate())==OP_FAILURE){
unknown_hdr=true;
break;
}
/* Determine next header type */
switch( eth.getEtherType() ){
case ETHTYPE_IPV4:
expected=HEADER_TYPE_IPv4;
next_layer=NETWORK_LAYER;
break;
case ETHTYPE_IPV6:
expected=HEADER_TYPE_IPv6;
next_layer=NETWORK_LAYER;
break;
case ETHTYPE_ARP:
next_layer=LINK_LAYER;
expected=HEADER_TYPE_ARP;
break;
default:
next_layer=APPLICATION_LAYER;
expected=HEADER_TYPE_RAW_DATA;
break;
}
this_packet[current_header].length=ethlen;
this_packet[current_header++].type=HEADER_TYPE_ETHERNET;
eth.reset();
curr_pkt+=ethlen;
curr_pktlen-=ethlen;
}else if(expected==HEADER_TYPE_ARP){
if(PKTPARSERDEBUG)puts("Expected Layer=ARP");
if(arp.storeRecvData(curr_pkt, curr_pktlen)==OP_FAILURE){
unknown_hdr=true;
break;
}
if( (arplen=arp.validate())==OP_FAILURE){
unknown_hdr=true;
break;
}
this_packet[current_header].length=arplen;
this_packet[current_header++].type=HEADER_TYPE_ARP;
arp.reset();
curr_pkt+=arplen;
curr_pktlen-=arplen;
if(curr_pktlen>0){
next_layer=APPLICATION_LAYER;
expected=HEADER_TYPE_RAW_DATA;
}else{
finished=true;
}
}else{
assert(finished==true);
}
/* IPv4 and IPv6 headers **************************************************/
}else if(next_layer==NETWORK_LAYER){
if(PKTPARSERDEBUG)puts("Next Layer=Network");
/* Determine IP version */
if (ip4.storeRecvData(curr_pkt, curr_pktlen)==OP_FAILURE){
unknown_hdr=true;
break;
}
/* IP version 4 ---------------------------------*/
if(ip4.getVersion()==4){
if( (iplen=ip4.validate())==OP_FAILURE){
unknown_hdr=true;
break;
}
/* Determine next header type */
switch(ip4.getNextProto()){
case HEADER_TYPE_ICMPv4:
next_layer=TRANSPORT_LAYER;
expected=HEADER_TYPE_ICMPv4;
break;
case HEADER_TYPE_IPv4: /* IP in IP */
next_layer=NETWORK_LAYER;
expected=HEADER_TYPE_IPv4;
break;
case HEADER_TYPE_TCP:
next_layer=TRANSPORT_LAYER;
expected=HEADER_TYPE_TCP;
break;
case HEADER_TYPE_UDP:
next_layer=TRANSPORT_LAYER;
expected=HEADER_TYPE_UDP;
break;
case HEADER_TYPE_IPv6: /* IPv6 in IPv4 */
next_layer=NETWORK_LAYER;
expected=HEADER_TYPE_IPv6;
break;
default:
next_layer=APPLICATION_LAYER;
expected=HEADER_TYPE_RAW_DATA;
break;
}
this_packet[current_header].length=iplen;
this_packet[current_header++].type=HEADER_TYPE_IPv4;
ip4.reset();
curr_pkt+=iplen;
curr_pktlen-=iplen;
/* IP version 6 ---------------------------------*/
}else if(ip4.getVersion()==6){
ip4.reset();
if (ip6.storeRecvData(curr_pkt, curr_pktlen)==OP_FAILURE){
unknown_hdr=true;
break;
}
if( (ip6len=ip6.validate())==OP_FAILURE ){
unknown_hdr=true;
break;
}
switch( ip6.getNextHeader() ){
case HEADER_TYPE_ICMPv6:
next_layer=TRANSPORT_LAYER;
expected=HEADER_TYPE_ICMPv6;
break;
case HEADER_TYPE_IPv4: /* IPv4 in IPv6 */
next_layer=NETWORK_LAYER;
expected=HEADER_TYPE_IPv4;
break;
case HEADER_TYPE_TCP:
next_layer=TRANSPORT_LAYER;
expected=HEADER_TYPE_TCP;
break;
case HEADER_TYPE_UDP:
next_layer=TRANSPORT_LAYER;
expected=HEADER_TYPE_UDP;
break;
case HEADER_TYPE_IPv6: /* IPv6 in IPv6 */
next_layer=NETWORK_LAYER;
expected=HEADER_TYPE_IPv6;
break;
case HEADER_TYPE_IPv6_HOPOPT:
next_layer=EXTHEADERS_LAYER;
expected=HEADER_TYPE_IPv6_HOPOPT;
break;
case HEADER_TYPE_IPv6_OPTS:
next_layer=EXTHEADERS_LAYER;
expected=HEADER_TYPE_IPv6_OPTS;
break;
case HEADER_TYPE_IPv6_ROUTE:
next_layer=EXTHEADERS_LAYER;
expected=HEADER_TYPE_IPv6_ROUTE;
break;
case HEADER_TYPE_IPv6_FRAG:
next_layer=EXTHEADERS_LAYER;
expected=HEADER_TYPE_IPv6_FRAG;
break;
default:
next_layer=APPLICATION_LAYER;
expected=HEADER_TYPE_RAW_DATA;
break;
}
this_packet[current_header].length=ip6len;
this_packet[current_header++].type=HEADER_TYPE_IPv6;
ip6.reset();
curr_pkt+=ip6len;
curr_pktlen-=ip6len;
/* Bogus IP version -----------------------------*/
}else{
/* Wrong IP version, treat as raw data. */
next_layer=APPLICATION_LAYER;
expected=HEADER_TYPE_RAW_DATA;
}
/* TCP, UDP, ICMPv4 and ICMPv6 headers ************************************/
}else if(next_layer==TRANSPORT_LAYER){
if(PKTPARSERDEBUG)puts("Next Layer=Transport");
if(expected==HEADER_TYPE_TCP){
if(PKTPARSERDEBUG)puts("Expected Layer=TCP");
if(tcp.storeRecvData(curr_pkt, curr_pktlen)==OP_FAILURE ){
unknown_hdr=true;
break;
}
if( (tcplen=tcp.validate())==OP_FAILURE){
unknown_hdr=true;
break;
}
expected=HEADER_TYPE_RAW_DATA;
this_packet[current_header].length=tcplen;
this_packet[current_header++].type=HEADER_TYPE_TCP;
tcp.reset();
curr_pkt+=tcplen;
curr_pktlen-=tcplen;
next_layer=APPLICATION_LAYER;
}else if(expected==HEADER_TYPE_UDP){
if(PKTPARSERDEBUG)puts("Expected Layer=UDP");
if(udp.storeRecvData(curr_pkt, curr_pktlen)==OP_FAILURE ){
unknown_hdr=true;
break;
}
if( (udplen=udp.validate())==OP_FAILURE){
unknown_hdr=true;
break;
}
expected=HEADER_TYPE_RAW_DATA;
this_packet[current_header].length=udplen;
this_packet[current_header++].type=HEADER_TYPE_UDP;
udp.reset();
curr_pkt+=udplen;
curr_pktlen-=udplen;
next_layer=APPLICATION_LAYER;
}else if(expected==HEADER_TYPE_ICMPv4){
if(PKTPARSERDEBUG)puts("Expected Layer=ICMPv4");
if(icmp4.storeRecvData(curr_pkt, curr_pktlen)==OP_FAILURE ){
unknown_hdr=true;
break;
}
if( (icmplen=icmp4.validate())==OP_FAILURE){
unknown_hdr=true;
break;
}
switch( icmp4.getType() ){
/* Types that include an IPv4 packet as payload */
case ICMP_UNREACH:
case ICMP_TIMXCEED:
case ICMP_PARAMPROB:
case ICMP_SOURCEQUENCH:
case ICMP_REDIRECT:
next_layer=NETWORK_LAYER;
expected=HEADER_TYPE_IPv4;
break;
/* ICMP types that include misc payloads (or no payload) */
default:
expected=HEADER_TYPE_RAW_DATA;
next_layer=APPLICATION_LAYER;
break;
}
this_packet[current_header].length=icmplen;
this_packet[current_header++].type=HEADER_TYPE_ICMPv4;
icmp4.reset();
curr_pkt+=icmplen;
curr_pktlen-=icmplen;
}else if(expected==HEADER_TYPE_ICMPv6){
if(PKTPARSERDEBUG)puts("Expected Layer=ICMPv6");
if(icmp6.storeRecvData(curr_pkt, curr_pktlen)==OP_FAILURE){
unknown_hdr=true;
break;
}
if( (icmplen=icmp6.validate())==OP_FAILURE){
unknown_hdr=true;
break;
}
switch( icmp6.getType() ){
/* Types that include an IPv6 packet as payload */
case ICMPv6_UNREACH:
case ICMPv6_PKTTOOBIG:
case ICMPv6_TIMXCEED:
case ICMPv6_PARAMPROB:
next_layer=NETWORK_LAYER;
expected=HEADER_TYPE_IPv6;
break;
/* ICMPv6 types that include misc payloads (or no payload) */
default:
expected=HEADER_TYPE_RAW_DATA;
next_layer=APPLICATION_LAYER;
break;
}
this_packet[current_header].length=icmplen;
this_packet[current_header++].type=HEADER_TYPE_ICMPv6;
icmp6.reset();
curr_pkt+=icmplen;
curr_pktlen-=icmplen;
}else{
/* Wrong application layer protocol, treat as raw data. */
next_layer=APPLICATION_LAYER;
expected=HEADER_TYPE_RAW_DATA;
}
/* IPv6 Extension Headers */
}else if(next_layer==EXTHEADERS_LAYER){
if(PKTPARSERDEBUG)puts("Next Layer=ExtHdr");
u8 ext_next=0;
/* Hop-by-Hop Options */
if(expected==HEADER_TYPE_IPv6_HOPOPT){
if(PKTPARSERDEBUG)puts("Expected=Hopt");
if(ext_hopt.storeRecvData(curr_pkt, curr_pktlen)==OP_FAILURE ){
unknown_hdr=true;
break;
}
if( (exthdrlen=ext_hopt.validate())==OP_FAILURE){
unknown_hdr=true;
break;
}
ext_next=ext_hopt.getNextHeader();
ext_hopt.reset();
/* Routing Header */
}else if(expected==HEADER_TYPE_IPv6_ROUTE){
if(PKTPARSERDEBUG)puts("Expected=Route");
if(ext_routing.storeRecvData(curr_pkt, curr_pktlen)==OP_FAILURE ){
unknown_hdr=true;
break;
}
if( (exthdrlen=ext_routing.validate())==OP_FAILURE){
unknown_hdr=true;
break;
}
ext_next=ext_routing.getNextHeader();
ext_routing.reset();
/* Fragmentation Header */
}else if(expected==HEADER_TYPE_IPv6_FRAG){
if(PKTPARSERDEBUG)puts("Expected=Frag");
if(ext_frag.storeRecvData(curr_pkt, curr_pktlen)==OP_FAILURE ){
unknown_hdr=true;
break;
}
if( (exthdrlen=ext_frag.validate())==OP_FAILURE){
unknown_hdr=true;
break;
}
ext_next=ext_frag.getNextHeader();
ext_frag.reset();
/* Destination Options Header */
}else if(expected==HEADER_TYPE_IPv6_OPTS){
if(PKTPARSERDEBUG)puts("Expected=Dopts");
if(ext_dopts.storeRecvData(curr_pkt, curr_pktlen)==OP_FAILURE ){
unknown_hdr=true;
break;
}
if( (exthdrlen=ext_dopts.validate())==OP_FAILURE){
unknown_hdr=true;
break;
}
ext_next=ext_dopts.getNextHeader();
ext_dopts.reset();
}else{
/* Should never happen. */
unknown_hdr=true;
break;
}
/* Update the info for this header */
this_packet[current_header].length=exthdrlen;
this_packet[current_header++].type=expected;
curr_pkt+=exthdrlen;
curr_pktlen-=exthdrlen;
/* Lets's see what comes next */
switch(ext_next){
case HEADER_TYPE_ICMPv6:
next_layer=TRANSPORT_LAYER;
expected=HEADER_TYPE_ICMPv6;
break;
case HEADER_TYPE_IPv4: /* IPv4 in IPv6 */
next_layer=NETWORK_LAYER;
expected=HEADER_TYPE_IPv4;
break;
case HEADER_TYPE_TCP:
next_layer=TRANSPORT_LAYER;
expected=HEADER_TYPE_TCP;
break;
case HEADER_TYPE_UDP:
next_layer=TRANSPORT_LAYER;
expected=HEADER_TYPE_UDP;
break;
case HEADER_TYPE_IPv6: /* IPv6 in IPv6 */
next_layer=NETWORK_LAYER;
expected=HEADER_TYPE_IPv6;
break;
case HEADER_TYPE_IPv6_HOPOPT:
next_layer=EXTHEADERS_LAYER;
expected=HEADER_TYPE_IPv6_HOPOPT;
break;
case HEADER_TYPE_IPv6_OPTS:
next_layer=EXTHEADERS_LAYER;
expected=HEADER_TYPE_IPv6_OPTS;
break;
case HEADER_TYPE_IPv6_ROUTE:
next_layer=EXTHEADERS_LAYER;
expected=HEADER_TYPE_IPv6_ROUTE;
break;
case HEADER_TYPE_IPv6_FRAG:
next_layer=EXTHEADERS_LAYER;
expected=HEADER_TYPE_IPv6_FRAG;
break;
default:
next_layer=APPLICATION_LAYER;
expected=HEADER_TYPE_RAW_DATA;
break;
}
/* Miscellaneous payloads *************************************************/
}else{ // next_layer==APPLICATION_LAYER
if(PKTPARSERDEBUG)puts("Next Layer=Application");
if(curr_pktlen>0){
/* If we get here it is possible that the packet is ARP but
* we have no access to the original Ethernet header. We
* determine if this header is ARP by checking its size
* and checking for some common values. */
if(arp.storeRecvData(curr_pkt, curr_pktlen)!=OP_FAILURE){
if( (arplen=arp.validate())!=OP_FAILURE){
if(arp.getHardwareType()==HDR_ETH10MB){
if(arp.getProtocolType()==0x0800){
if(arp.getHwAddrLen()==ETH_ADDRESS_LEN){
if(arp.getProtoAddrLen()==IPv4_ADDRESS_LEN){
this_packet[current_header].length=arplen;
this_packet[current_header++].type=HEADER_TYPE_ARP;
arp.reset();
curr_pkt+=arplen;
curr_pktlen-=arplen;
if(curr_pktlen>0){
next_layer=APPLICATION_LAYER;
expected=HEADER_TYPE_RAW_DATA;
}else{
finished=true;
}
}
}
}
}
}
}
//if(expected==HEADER_TYPE_DNS){
//}else if(expected==HEADER_TYPE_HTTP){
//}... ETC
this_packet[current_header].length=curr_pktlen;
this_packet[current_header++].type=HEADER_TYPE_RAW_DATA;
curr_pktlen=0;
}
finished=true;
}
} /* End of header processing loop */
/* If we couldn't validate some header, treat that header and any remaining
* data, as raw application data. */
if (unknown_hdr==true){
if(curr_pktlen>0){
if(PKTPARSERDEBUG)puts("Unknown layer found. Treating it as raw data.");
this_packet[current_header].length=curr_pktlen;
this_packet[current_header++].type=HEADER_TYPE_RAW_DATA;
}
}
return this_packet;
} /* End of parse_received_packet() */
/* TODO: remove */
int PacketParser::dummy_print_packet_type(const u8 *pkt, size_t pktlen, bool eth_included){
pkt_type_t *packetheaders=PacketParser::parse_packet(pkt, pktlen, eth_included);
for(int i=0; packetheaders[i].length!=0; i++){
printf("%s:", header_type2string(packetheaders[i].type));
}
printf("\n");
return OP_SUCCESS;
} /* End of dummy_print_packet_type() */
int PacketParser::dummy_print_packet(const u8 *pkt, size_t pktlen, bool eth_included){
PacketElement *me=NULL, *aux=NULL;
if( (me=split(pkt, pktlen, eth_included))==NULL )
return OP_FAILURE;
else{
me->print(stdout, PRINT_DETAIL_HIGH);
printf("\n");
}
/* Free the structs */
while(me!=NULL){
aux=me->getNextElement();
delete me;
me=aux;
}
return OP_SUCCESS;
} /* End of dummy_print_packet() */
/** For a given packet, this method determines where the application layer data
* begins. It returs a positive offset if any application data was found, zero
* if the packet did not contain application data and a negative integer in
* case of error. */
int PacketParser::payload_offset(const u8 *pkt, size_t pktlen, bool link_included){
PacketElement *me=NULL, *aux=NULL;
size_t offset=pktlen; /* Initially, point to the end of the packet. */
/* Safe checks*/
if(pkt==NULL || pktlen<=0)
return -1;
dummy_print_packet_type(pkt, pktlen, link_included);
/* Split the packet into separate protocol headers */
if( (me=split(pkt, pktlen, link_included))==NULL )
return -2;
else{
aux=me;
}
/* Find if there is application data and where it begins */
while(me!=NULL){
/* When we find application data, we compute the offset by substacting the
length of the application data from the packet's total length */
if(me->protocol_id()==HEADER_TYPE_RAW_DATA){
offset=pktlen-me->getLen();
break;
me=me->getNextElement();
}else{
me=me->getNextElement();
}
}
/* Free the structs */
me=aux;
while(me!=NULL){
aux=me->getNextElement();
delete me;
me=aux;
}
/* Return 0 if we didn't find any application data */
if(offset==pktlen){
return 0;
}else{
return offset;
}
} /* End of payload_offset() */
PacketElement *PacketParser::split(const u8 *pkt, size_t pktlen){
return split(pkt, pktlen, false);
} /* End of split() */
PacketElement *PacketParser::split(const u8 *pkt, size_t pktlen, bool eth_included){
pkt_type_t *packetheaders=NULL;
const u8 *curr_pkt=pkt;
PacketElement *first=NULL;
PacketElement *last=NULL;
IPv4Header *ip4=NULL;
IPv6Header *ip6=NULL;
DestOptsHeader *ext_dopts=NULL;
FragmentHeader *ext_frag=NULL;
HopByHopHeader *ext_hopt=NULL;
RoutingHeader *ext_routing=NULL;
TCPHeader *tcp=NULL;
UDPHeader *udp=NULL;
ICMPv4Header *icmp4=NULL;
ICMPv6Header *icmp6=NULL;
EthernetHeader *eth=NULL;
ARPHeader *arp=NULL;
RawData *raw=NULL;
/* Analyze the packet. This returns a list of header types and lengths */
if((packetheaders=PacketParser::parse_packet(pkt, pktlen, eth_included))==NULL)
return NULL;
/* Store each header in its own PacketHeader object type */
for(int i=0; packetheaders[i].length!=0; i++){
switch(packetheaders[i].type){
case HEADER_TYPE_ETHERNET:
eth=new EthernetHeader();
eth->storeRecvData(curr_pkt, packetheaders[i].length);
if(first==NULL){
first=eth;
}else{
last->setNextElement(eth);
}
last=eth;
break;
case HEADER_TYPE_ARP:
arp=new ARPHeader();
arp->storeRecvData(curr_pkt, packetheaders[i].length);
if(first==NULL){
first=arp;
}else{
last->setNextElement(arp);
}
last=arp;
break;
case HEADER_TYPE_IPv4:
ip4=new IPv4Header();
ip4->storeRecvData(curr_pkt, packetheaders[i].length);
if(first==NULL){
first=ip4;
}else{
last->setNextElement(ip4);
}
last=ip4;
break;
case HEADER_TYPE_IPv6:
ip6=new IPv6Header();
ip6->storeRecvData(curr_pkt, packetheaders[i].length);
if(first==NULL){
first=ip6;
}else{
last->setNextElement(ip6);
}
last=ip6;
break;
case HEADER_TYPE_TCP:
tcp=new TCPHeader();
tcp->storeRecvData(curr_pkt, packetheaders[i].length);
if(first==NULL){
first=tcp;
}else{
last->setNextElement(tcp);
}
last=tcp;
break;
case HEADER_TYPE_UDP:
udp=new UDPHeader();
udp->storeRecvData(curr_pkt, packetheaders[i].length);
if(first==NULL){
first=udp;
}else{
last->setNextElement(udp);
}
last=udp;
break;
case HEADER_TYPE_ICMPv4:
icmp4=new ICMPv4Header();
icmp4->storeRecvData(curr_pkt, packetheaders[i].length);
if(first==NULL){
first=icmp4;
}else{
last->setNextElement(icmp4);
}
last=icmp4;
break;
case HEADER_TYPE_ICMPv6:
icmp6=new ICMPv6Header();
icmp6->storeRecvData(curr_pkt, packetheaders[i].length);
if(first==NULL){
first=icmp6;
}else{
last->setNextElement(icmp6);
}
last=icmp6;
break;
case HEADER_TYPE_IPv6_HOPOPT:
ext_hopt=new HopByHopHeader();
ext_hopt->storeRecvData(curr_pkt, packetheaders[i].length);
if(first==NULL){
first=ext_hopt;
}else{
last->setNextElement(ext_hopt);
}
last=ext_hopt;
break;
case HEADER_TYPE_IPv6_ROUTE:
ext_routing=new RoutingHeader();
ext_routing->storeRecvData(curr_pkt, packetheaders[i].length);
if(first==NULL){
first=ext_routing;
}else{
last->setNextElement(ext_routing);
}
last=ext_routing;
break;
case HEADER_TYPE_IPv6_FRAG:
ext_frag=new FragmentHeader();
ext_frag->storeRecvData(curr_pkt, packetheaders[i].length);
if(first==NULL){
first=ext_frag;
}else{
last->setNextElement(ext_frag);
}
last=ext_frag;
break;
case HEADER_TYPE_IPv6_OPTS:
ext_dopts=new DestOptsHeader();
ext_dopts->storeRecvData(curr_pkt, packetheaders[i].length);
if(first==NULL){
first=ext_dopts;
}else{
last->setNextElement(ext_dopts);
}
last=ext_dopts;
break;
case HEADER_TYPE_RAW_DATA:
default:
raw=new RawData();
raw->storeRecvData(curr_pkt, packetheaders[i].length);
if(first==NULL){
first=raw;
}else{
last->setNextElement(raw);
}
last=raw;
break;
}
curr_pkt+=packetheaders[i].length;
}
return first;
} /* End of split() */
/* This method frees a chain of PacketElement objects. Note that objects in
* the chain are freed by calling "delete" on them, so only those instances
* that have been obtained through a call to "new" should be passed to this
* method. Chains returned by PacketParser::split() are safe to use with this.*/
int PacketParser::freePacketChain(PacketElement *first){
PacketElement *curr=first;
PacketElement *next=NULL;
while(curr!=NULL){
next=curr->getNextElement();
delete curr;
curr=next;
}
return OP_SUCCESS;
} /* End of freePacketChain() */
/* This method is for debugging purposes only. It tests the packet parser and
* the PacketElement class family. Basically it checks that the supplied
* chain of PacketElements can be serialized and de-serialized correctly.
* Returns NULL on success or an error string in case of failure. */
const char *PacketParser::test_packet_parser(PacketElement *test_pkt){
const char *errmsg=NULL;
PacketElement *parsed_pkt=NULL;
PacketElement *orig_pkt=NULL;
PacketElement *new_pkt=NULL;
u8 *mypktbuff2=NULL;
u8 *mypktbuff=NULL;
if(test_pkt==NULL){
errmsg="NULL pointer supplied";
goto end;
}
/* Generate a serialized version of the packet */
mypktbuff=(u8 *)safe_malloc(test_pkt->getLen());
test_pkt->dumpToBinaryBuffer(mypktbuff, test_pkt->getLen());
/* Generate a chain of PacketElement objects from the serialized version. */
parsed_pkt=PacketParser::split(mypktbuff, test_pkt->getLen());
if(parsed_pkt==NULL){
errmsg="PacketParser::split() returned NULL";
goto end;
}
if(parsed_pkt->getLen()!=test_pkt->getLen()){
errmsg="Packets have different lengths";
goto end;
}
/* Generate a serialized version of the new chain */
mypktbuff2=(u8 *)safe_malloc(parsed_pkt->getLen());
parsed_pkt->dumpToBinaryBuffer(mypktbuff2, parsed_pkt->getLen());
/* Make sure both packets produce the exact same binary buffer */
if(memcmp(mypktbuff, mypktbuff2, parsed_pkt->getLen())!=0){
errmsg="The two packets do not result in the same binary buffer";
goto end;
}
/* Now let's check that both chains have the same number and type of
* PacketElements. */
orig_pkt=test_pkt;
new_pkt=parsed_pkt;
while(orig_pkt!=NULL && new_pkt!=NULL){