-
Notifications
You must be signed in to change notification settings - Fork 85
/
Copy pathsflowtool.c
5007 lines (4491 loc) · 167 KB
/
sflowtool.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) 2002-2011 InMon Corp. Licensed under the terms of the InMon sFlow licence: */
/* http://www.inmon.com/technology/sflowlicense.txt */
#if defined(__cplusplus)
extern "C" {
#endif
#ifdef WIN32
#include "config_windows.h"
#else
#include "config.h"
#endif
#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <sys/types.h>
#include <fcntl.h>
#include <time.h>
#include <setjmp.h>
#include <ctype.h>
#ifdef WIN32
#else
#include <stdint.h>
#include <unistd.h>
#include <netdb.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <sys/poll.h>
#include <netinet/in_systm.h>
#include <netinet/in.h>
#include <inttypes.h>
#include <arpa/inet.h>
#include <netdb.h>
#endif
#include "sflow.h" /* sFlow v5 */
#include "sflowtool.h" /* sFlow v2/4 */
/* If the platform is Linux, enable the source-spoofing feature too. */
#ifdef linux
#define SPOOFSOURCE 1
#endif
/*
#ifdef DARWIN
#include <architecture/byte_order.h>
#define bswap_16(x) NXSwapShort(x)
#define bswap_32(x) NXSwapInt(x)
#else
#include <byteswap.h>
#endif
*/
/* just do it in a portable way... */
static uint32_t MyByteSwap32(uint32_t n) {
return (((n & 0x000000FF)<<24) +
((n & 0x0000FF00)<<8) +
((n & 0x00FF0000)>>8) +
((n & 0xFF000000)>>24));
}
static uint16_t MyByteSwap16(uint16_t n) {
return ((n >> 8) | (n << 8));
}
#ifndef PRIu64
# ifdef WIN32
# define PRIu64 "I64u"
# else
# define PRIu64 "llu"
# endif
#endif
#define YES 1
#define NO 0
/* define my own IP header struct - to ease portability */
struct myiphdr
{
uint8_t version_and_headerLen;
uint8_t tos;
uint16_t tot_len;
uint16_t id;
uint16_t frag_off;
uint8_t ttl;
uint8_t protocol;
uint16_t check;
uint32_t saddr;
uint32_t daddr;
};
/* ip6 header if no option headers */
struct myip6hdr {
uint8_t version_and_priority;
uint8_t label1;
uint8_t label2;
uint8_t label3;
uint16_t payloadLength;
uint8_t nextHeader;
uint8_t ttl;
struct in6_addr saddr;
struct in6_addr daddr;
};
/* same for tcp */
struct mytcphdr
{
uint16_t th_sport; /* source port */
uint16_t th_dport; /* destination port */
uint32_t th_seq; /* sequence number */
uint32_t th_ack; /* acknowledgement number */
uint8_t th_off_and_unused;
uint8_t th_flags;
uint16_t th_win; /* window */
uint16_t th_sum; /* checksum */
uint16_t th_urp; /* urgent pointer */
};
/* and UDP */
struct myudphdr {
uint16_t uh_sport; /* source port */
uint16_t uh_dport; /* destination port */
uint16_t uh_ulen; /* udp length */
uint16_t uh_sum; /* udp checksum */
};
/* and ICMP */
struct myicmphdr
{
uint8_t type; /* message type */
uint8_t code; /* type sub-code */
/* ignore the rest */
};
#ifdef SPOOFSOURCE
#define SPOOFSOURCE_SENDPACKET_SIZE 2000
struct mySendPacket {
struct myiphdr ip;
struct myudphdr udp;
uint8_t data[SPOOFSOURCE_SENDPACKET_SIZE];
};
#endif
/* tcpdump file format */
struct pcap_file_header {
uint32_t magic;
uint16_t version_major;
uint16_t version_minor;
uint32_t thiszone; /* gmt to local correction */
uint32_t sigfigs; /* accuracy of timestamps */
uint32_t snaplen; /* max length saved portion of each pkt */
uint32_t linktype; /* data link type (DLT_*) */
};
struct pcap_pkthdr {
uint32_t ts_sec; /* time stamp - used to be struct timeval, but time_t can be 64 bits now */
uint32_t ts_usec;
uint32_t caplen; /* length of portion present */
uint32_t len; /* length this packet (off wire) */
/* some systems expect to see more information here. For example,
* on some versions of RedHat Linux, there are three extra fields:
* int index;
* unsigned short protocol;
* unsigned char pkt_type;
* To pad the header with zeros, use the tcpdumpHdrPad option.
*/
};
typedef struct _SFForwardingTarget {
struct _SFForwardingTarget *nxt;
struct sockaddr_in addr;
int sock;
} SFForwardingTarget;
typedef struct _SFForwardingTarget6 {
struct _SFForwardingTarget6 *nxt;
struct sockaddr_in6 addr;
int sock;
} SFForwardingTarget6;
typedef enum { SFLFMT_FULL=0, SFLFMT_PCAP, SFLFMT_LINE, SFLFMT_NETFLOW, SFLFMT_FWD, SFLFMT_CLF, SFLFMT_SCRIPT } EnumSFLFormat;
typedef struct _SFConfig {
/* sflow(R) options */
uint16_t sFlowInputPort;
/* netflow(TM) options */
uint16_t netFlowOutputPort;
struct in_addr netFlowOutputIP;
int netFlowOutputSocket;
uint16_t netFlowPeerAS;
int disableNetFlowScale;
/* tcpdump options */
char *readPcapFileName;
FILE *readPcapFile;
struct pcap_file_header readPcapHdr;
char *writePcapFile;
EnumSFLFormat outputFormat;
uint32_t tcpdumpHdrPad;
uint8_t zeroPad[100];
int pcapSwap;
#ifdef SPOOFSOURCE
int spoofSource;
uint16_t ipid;
struct mySendPacket sendPkt;
uint32_t packetLen;
#endif
SFForwardingTarget *forwardingTargets;
SFForwardingTarget6 *forwardingTargets6;
/* vlan filtering */
int gotVlanFilter;
#define FILTER_MAX_VLAN 4096
uint8_t vlanFilter[FILTER_MAX_VLAN + 1];
/* content stripping */
int removeContent;
/* options to restrict IP socket / bind */
int listen4;
int listen6;
int listenControlled;
/* general options */
int keepGoing;
} SFConfig;
/* make the options structure global to the program */
static SFConfig sfConfig;
/* define a separate global we can use to construct the common-log-file format */
typedef struct _SFCommonLogFormat {
#define SFLFMT_CLF_MAX_LINE 2000
int valid;
char client[64];
char http_log[SFLFMT_CLF_MAX_LINE];
} SFCommonLogFormat;
static SFCommonLogFormat sfCLF;
static const char *SFHTTP_method_names[] = { "-", "OPTIONS", "GET", "HEAD", "POST", "PUT", "DELETE", "TRACE", "CONNECT" };
typedef struct _SFSample {
SFLAddress sourceIP;
SFLAddress agent_addr;
uint32_t agentSubId;
/* the raw pdu */
uint8_t *rawSample;
uint32_t rawSampleLen;
uint8_t *endp;
time_t pcapTimestamp;
time_t readTimestamp;
/* decode cursor */
uint32_t *datap;
uint32_t datagramVersion;
uint32_t sampleType;
uint32_t elementType;
uint32_t ds_class;
uint32_t ds_index;
/* generic interface counter sample */
SFLIf_counters ifCounters;
/* sample stream info */
uint32_t sysUpTime;
uint32_t sequenceNo;
uint32_t sampledPacketSize;
uint32_t samplesGenerated;
uint32_t meanSkipCount;
uint32_t samplePool;
uint32_t dropEvents;
/* the sampled header */
uint32_t packet_data_tag;
uint32_t headerProtocol;
uint8_t *header;
uint32_t headerLen;
uint32_t stripped;
/* header decode */
int gotIPV4;
int gotIPV4Struct;
int offsetToIPV4;
int gotIPV6;
int gotIPV6Struct;
int offsetToIPV6;
int offsetToPayload;
SFLAddress ipsrc;
SFLAddress ipdst;
uint32_t dcd_ipProtocol;
uint32_t dcd_ipTos;
uint32_t dcd_ipTTL;
uint32_t dcd_sport;
uint32_t dcd_dport;
uint32_t dcd_tcpFlags;
uint32_t ip_fragmentOffset;
uint32_t udp_pduLen;
/* ports */
uint32_t inputPortFormat;
uint32_t outputPortFormat;
uint32_t inputPort;
uint32_t outputPort;
/* ethernet */
uint32_t eth_type;
uint32_t eth_len;
uint8_t eth_src[8];
uint8_t eth_dst[8];
/* vlan */
uint32_t in_vlan;
uint32_t in_priority;
uint32_t internalPriority;
uint32_t out_vlan;
uint32_t out_priority;
int vlanFilterReject;
/* extended data fields */
uint32_t num_extended;
uint32_t extended_data_tag;
#define SASAMPLE_EXTENDED_DATA_SWITCH 1
#define SASAMPLE_EXTENDED_DATA_ROUTER 4
#define SASAMPLE_EXTENDED_DATA_GATEWAY 8
#define SASAMPLE_EXTENDED_DATA_USER 16
#define SASAMPLE_EXTENDED_DATA_URL 32
#define SASAMPLE_EXTENDED_DATA_MPLS 64
#define SASAMPLE_EXTENDED_DATA_NAT 128
#define SASAMPLE_EXTENDED_DATA_MPLS_TUNNEL 256
#define SASAMPLE_EXTENDED_DATA_MPLS_VC 512
#define SASAMPLE_EXTENDED_DATA_MPLS_FTN 1024
#define SASAMPLE_EXTENDED_DATA_MPLS_LDP_FEC 2048
#define SASAMPLE_EXTENDED_DATA_VLAN_TUNNEL 4096
#define SASAMPLE_EXTENDED_DATA_NAT_PORT 8192
/* IP forwarding info */
SFLAddress nextHop;
uint32_t srcMask;
uint32_t dstMask;
/* BGP info */
SFLAddress bgp_nextHop;
uint32_t my_as;
uint32_t src_as;
uint32_t src_peer_as;
uint32_t dst_as_path_len;
uint32_t *dst_as_path;
/* note: version 4 dst as path segments just get printed, not stored here, however
* the dst_peer and dst_as are filled in, since those are used for netflow encoding
*/
uint32_t dst_peer_as;
uint32_t dst_as;
uint32_t communities_len;
uint32_t *communities;
uint32_t localpref;
/* user id */
#define SA_MAX_EXTENDED_USER_LEN 200
uint32_t src_user_charset;
uint32_t src_user_len;
char src_user[SA_MAX_EXTENDED_USER_LEN+1];
uint32_t dst_user_charset;
uint32_t dst_user_len;
char dst_user[SA_MAX_EXTENDED_USER_LEN+1];
/* url */
#define SA_MAX_EXTENDED_URL_LEN 200
#define SA_MAX_EXTENDED_HOST_LEN 200
uint32_t url_direction;
uint32_t url_len;
char url[SA_MAX_EXTENDED_URL_LEN+1];
uint32_t host_len;
char host[SA_MAX_EXTENDED_HOST_LEN+1];
/* mpls */
SFLAddress mpls_nextHop;
/* nat */
SFLAddress nat_src;
SFLAddress nat_dst;
/* counter blocks */
uint32_t statsSamplingInterval;
uint32_t counterBlockVersion;
/* exception handler context */
jmp_buf env;
#define ERROUT stderr
#ifdef DEBUG
# define SFABORT(s, r) abort()
# undef ERROUT
# define ERROUT stdout
#else
# define SFABORT(s, r) longjmp((s)->env, (r))
#endif
#define SF_ABORT_EOS 1
#define SF_ABORT_DECODE_ERROR 2
#define SF_ABORT_LENGTH_ERROR 3
} SFSample;
/* Cisco netflow version 5 record format */
typedef struct _NFFlow5 {
uint32_t srcIP;
uint32_t dstIP;
uint32_t nextHop;
uint16_t if_in;
uint16_t if_out;
uint32_t frames;
uint32_t bytes;
uint32_t firstTime;
uint32_t lastTime;
uint16_t srcPort;
uint16_t dstPort;
uint8_t pad1;
uint8_t tcpFlags;
uint8_t ipProto;
uint8_t ipTos;
uint16_t srcAS;
uint16_t dstAS;
uint8_t srcMask; /* No. bits */
uint8_t dstMask; /* No. bits */
uint16_t pad2;
} NFFlow5;
typedef struct _NFFlowHdr5 {
uint16_t version;
uint16_t count;
uint32_t sysUpTime;
uint32_t unixSeconds;
uint32_t unixNanoSeconds;
uint32_t flowSequence;
uint8_t engineType;
uint8_t engineId;
uint16_t sampling_interval;
} NFFlowHdr5;
typedef struct _NFFlowPkt5 {
NFFlowHdr5 hdr;
NFFlow5 flow; /* normally an array, but here we always send just 1 at a time */
} NFFlowPkt5;
static void readFlowSample_header(SFSample *sample);
static void readFlowSample(SFSample *sample, int expanded);
static char *printTag(uint32_t tag, char *buf);
static char *printAddress(SFLAddress *address, char *buf);
/*_________________---------------------------__________________
_________________ heap allocation __________________
-----------------___________________________------------------
*/
void *my_calloc(size_t bytes) {
void *mem = calloc(1, bytes);
if(mem == NULL) {
fprintf(ERROUT, "calloc(%"PRIu64") failed: %s\n", (uint64_t)bytes, strerror(errno));
exit(-1);
}
return mem;
}
void my_free(void *ptr) {
if(ptr) {
free(ptr);
}
}
/*_________________---------------------------__________________
_________________ sf_log __________________
-----------------___________________________------------------
*/
void sf_log(SFSample *sample, char *fmt, ...)
{
/* don't print anything if we are exporting tcpdump format or tabular format instead */
if(sfConfig.outputFormat == SFLFMT_SCRIPT) {
/* scripts like to have all the context on every line */
char agentIP[51], tag1[51], tag2[51], nowstr[200];
time_t now = sample->pcapTimestamp ?: sample->readTimestamp;
strftime(nowstr, 200, "%d/%b/%Y:%H:%M:%S", localtime(&now));
printf("%s %s %u %u %u:%u %s %s ",
nowstr,
printAddress(&sample->agent_addr, agentIP),
sample->agentSubId,
sample->sequenceNo,
sample->ds_class,
sample->ds_index,
printTag(sample->sampleType, tag1),
printTag(sample->elementType, tag2));
}
if(sfConfig.outputFormat == SFLFMT_FULL
|| sfConfig.outputFormat == SFLFMT_SCRIPT) {
va_list args;
va_start(args, fmt);
if(vprintf(fmt, args) < 0) {
exit(-40);
}
}
}
/*_________________---------------------------__________________
_________________ printHex __________________
-----------------___________________________------------------
*/
static uint8_t bin2hex(int nib) { return (nib < 10) ? ('0' + nib) : ('A' - 10 + nib); }
int printHex(const uint8_t *a, int len, char *buf, int bufLen, int marker, int bytesPerOutputLine)
{
int b = 0, i = 0;
for(; i < len; i++) {
uint8_t byte;
if(b > (bufLen - 10)) break;
if(marker > 0 && i == marker) {
buf[b++] = '<';
buf[b++] = '*';
buf[b++] = '>';
buf[b++] = '-';
}
byte = a[i];
buf[b++] = bin2hex(byte >> 4);
buf[b++] = bin2hex(byte & 0x0f);
if(i > 0 && (i % bytesPerOutputLine) == 0) buf[b++] = '\n';
else {
/* separate the bytes with a dash */
if (i < (len - 1)) buf[b++] = '-';
}
}
buf[b] = '\0';
return b;
}
/*_________________---------------------------__________________
_________________ printUUID __________________
-----------------___________________________------------------
*/
int printUUID(const uint8_t *a, char *buf, int bufLen)
{
int i, b = 0;
b += printHex(a, 4, buf, bufLen, 0, 100);
buf[b++] = '-';
b += printHex(a + 4, 2, buf + b, bufLen - b, 0, 100);
buf[b++] = '-';
b += printHex(a + 6, 2, buf + b, bufLen - b, 0, 100);
buf[b++] = '-';
b += printHex(a + 8, 2, buf + b, bufLen - b, 0, 100);
buf[b++] = '-';
b += printHex(a + 10, 6, buf + b, bufLen - b, 0, 100);
/* should really be lowercase hex - fix that here */
for(i = 0; i < b; i++) buf[i] = tolower(buf[i]);
/* add NUL termination */
buf[b] = '\0';
return b;
}
/*_________________---------------------------__________________
_________________ URLEncode __________________
-----------------___________________________------------------
*/
char *URLEncode(char *in, char *out, int outlen)
{
register char c, *r = in, *w = out;
int maxlen = (strlen(in) * 3) + 1;
if(outlen < maxlen) return "URLEncode: not enough space";
while ((c = *r++)) {
if(isalnum(c)) *w++ = c;
else if(isspace(c)) *w++ = '+';
else {
*w++ = '%';
*w++ = bin2hex(c >> 4);
*w++ = bin2hex(c & 0x0f);
}
}
*w++ = '\0';
return out;
}
/*_________________---------------------------__________________
_________________ IP_to_a __________________
-----------------___________________________------------------
*/
char *IP_to_a(uint32_t ipaddr, char *buf)
{
uint8_t *ip = (uint8_t *)&ipaddr;
/* should really be: snprintf(buf, buflen,...) but snprintf() is not always available */
sprintf(buf, "%u.%u.%u.%u", ip[0], ip[1], ip[2], ip[3]);
return buf;
}
static char *printAddress(SFLAddress *address, char *buf) {
switch(address->type) {
case SFLADDRESSTYPE_IP_V4:
IP_to_a(address->address.ip_v4.addr, buf);
break;
case SFLADDRESSTYPE_IP_V6:
{
uint8_t *b = address->address.ip_v6.addr;
/* should really be: snprintf(buf, buflen,...) but snprintf() is not always available */
sprintf(buf, "%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x",
b[0],b[1],b[2],b[3],b[4],b[5],b[6],b[7],b[8],b[9],b[10],b[11],b[12],b[13],b[14],b[15]);
}
break;
default:
sprintf(buf, "-");
}
return buf;
}
/*_________________---------------------------__________________
_________________ sampleFilterOK __________________
-----------------___________________________------------------
*/
int sampleFilterOK(SFSample *sample)
{
/* the vlan filter will only reject a sample if both in_vlan and out_vlan are rejected. If the
vlan was not reported in an SFLExtended_Switch struct, but was only picked up from the 802.1q header
then the out_vlan will be 0, so to be sure you are rejecting vlan 1, you may need to reject both
vlan 0 and vlan 1. */
return(sfConfig.gotVlanFilter == NO
|| sfConfig.vlanFilter[sample->in_vlan]
|| sfConfig.vlanFilter[sample->out_vlan]);
}
/*_________________---------------------------__________________
_________________ writeFlowLine __________________
-----------------___________________________------------------
*/
static void writeFlowLine(SFSample *sample)
{
char agentIP[51], srcIP[51], dstIP[51];
/* source */
if(printf("FLOW,%s,%d,%d,",
printAddress(&sample->agent_addr, agentIP),
sample->inputPort,
sample->outputPort) < 0) {
exit(-41);
}
/* layer 2 */
if(printf("%02x%02x%02x%02x%02x%02x,%02x%02x%02x%02x%02x%02x,0x%04x,%d,%d",
sample->eth_src[0],
sample->eth_src[1],
sample->eth_src[2],
sample->eth_src[3],
sample->eth_src[4],
sample->eth_src[5],
sample->eth_dst[0],
sample->eth_dst[1],
sample->eth_dst[2],
sample->eth_dst[3],
sample->eth_dst[4],
sample->eth_dst[5],
sample->eth_type,
sample->in_vlan,
sample->out_vlan) < 0) {
exit(-42);
}
/* layer 3/4 */
if(printf(",%s,%s,%d,0x%02x,%d,%d,%d,0x%02x",
printAddress(&sample->ipsrc, srcIP),
printAddress(&sample->ipdst, dstIP),
sample->dcd_ipProtocol,
sample->dcd_ipTos,
sample->dcd_ipTTL,
sample->dcd_sport,
sample->dcd_dport,
sample->dcd_tcpFlags) < 0) {
exit(-43);
}
/* bytes */
if(printf(",%d,%d,%d\n",
sample->sampledPacketSize,
sample->sampledPacketSize - sample->stripped - sample->offsetToIPV4,
sample->meanSkipCount) < 0) {
exit(-44);
}
}
/*_________________---------------------------__________________
_________________ writeCountersLine __________________
-----------------___________________________------------------
*/
static void writeCountersLine(SFSample *sample)
{
/* source */
char agentIP[51];
if(printf("CNTR,%s,", printAddress(&sample->agent_addr, agentIP)) < 0) {
exit(-45);
}
if(printf("%u,%u,%"PRIu64",%u,%u,%"PRIu64",%u,%u,%u,%u,%u,%u,%"PRIu64",%u,%u,%u,%u,%u,%u\n",
sample->ifCounters.ifIndex,
sample->ifCounters.ifType,
sample->ifCounters.ifSpeed,
sample->ifCounters.ifDirection,
sample->ifCounters.ifStatus,
sample->ifCounters.ifInOctets,
sample->ifCounters.ifInUcastPkts,
sample->ifCounters.ifInMulticastPkts,
sample->ifCounters.ifInBroadcastPkts,
sample->ifCounters.ifInDiscards,
sample->ifCounters.ifInErrors,
sample->ifCounters.ifInUnknownProtos,
sample->ifCounters.ifOutOctets,
sample->ifCounters.ifOutUcastPkts,
sample->ifCounters.ifOutMulticastPkts,
sample->ifCounters.ifOutBroadcastPkts,
sample->ifCounters.ifOutDiscards,
sample->ifCounters.ifOutErrors,
sample->ifCounters.ifPromiscuousMode) < 0) {
exit(-46);
}
}
/*_________________---------------------------__________________
_________________ receiveError __________________
-----------------___________________________------------------
*/
static void receiveError(SFSample *sample, char *errm, int hexdump)
{
char ipbuf[51];
char scratch[6000];
char *msg = "";
char *hex = "";
uint32_t markOffset = (uint8_t *)sample->datap - sample->rawSample;
if(errm) msg = errm;
if(hexdump) {
printHex(sample->rawSample, sample->rawSampleLen, scratch, 6000, markOffset, 16);
hex = scratch;
}
fprintf(ERROUT, "%s (source IP = %s) %s\n",
msg,
printAddress(&sample->sourceIP, ipbuf),
hex);
SFABORT(sample, SF_ABORT_DECODE_ERROR);
}
/*_________________---------------------------__________________
_________________ lengthCheck __________________
-----------------___________________________------------------
*/
static void lengthCheck(SFSample *sample, char *description, uint8_t *start, int len) {
uint32_t actualLen = (uint8_t *)sample->datap - start;
uint32_t adjustedLen = ((len + 3) >> 2) << 2;
if(actualLen != adjustedLen) {
fprintf(ERROUT, "%s length error (expected %d, found %d)\n", description, len, actualLen);
SFABORT(sample, SF_ABORT_LENGTH_ERROR);
}
}
/*_________________---------------------------__________________
_________________ decodeLinkLayer __________________
-----------------___________________________------------------
store the offset to the start of the ipv4 header in the sequence_number field
or -1 if not found. Decode the 802.1d if it's there.
*/
#define NFT_ETHHDR_SIZ 14
#define NFT_8022_SIZ 3
#define NFT_MAX_8023_LEN 1500
#define NFT_MIN_SIZ (NFT_ETHHDR_SIZ + sizeof(struct myiphdr))
static void decodeLinkLayer(SFSample *sample)
{
uint8_t *start = sample->header;
uint8_t *end = start + sample->headerLen;
uint8_t *ptr = start;
uint16_t type_len;
/* assume not found */
sample->gotIPV4 = NO;
sample->gotIPV6 = NO;
if((end - ptr) < NFT_ETHHDR_SIZ) return; /* not enough for an Ethernet header */
sf_log(sample,"dstMAC %02x%02x%02x%02x%02x%02x\n", ptr[0], ptr[1], ptr[2], ptr[3], ptr[4], ptr[5]);
memcpy(sample->eth_dst, ptr, 6);
ptr += 6;
sf_log(sample,"srcMAC %02x%02x%02x%02x%02x%02x\n", ptr[0], ptr[1], ptr[2], ptr[3], ptr[4], ptr[5]);
memcpy(sample->eth_src, ptr, 6);
ptr += 6;
type_len = (ptr[0] << 8) + ptr[1];
ptr += 2;
if(type_len == 0x8100) {
if((end - ptr) < 4) return; /* not enough for an 802.1Q header */
/* VLAN - next two bytes */
uint32_t vlanData = (ptr[0] << 8) + ptr[1];
uint32_t vlan = vlanData & 0x0fff;
uint32_t priority = vlanData >> 13;
ptr += 2;
/* _____________________________________ */
/* | pri | c | vlan-id | */
/* ------------------------------------- */
/* [priority = 3bits] [Canonical Format Flag = 1bit] [vlan-id = 12 bits] */
sf_log(sample,"decodedVLAN %u\n", vlan);
sf_log(sample,"decodedPriority %u\n", priority);
sample->in_vlan = vlan;
/* now get the type_len again (next two bytes) */
type_len = (ptr[0] << 8) + ptr[1];
ptr += 2;
}
/* now we're just looking for IP */
if((end - start) < sizeof(struct myiphdr)) return; /* not enough for an IPv4 header (or IPX, or SNAP) */
/* peek for IPX */
if(type_len == 0x0200 || type_len == 0x0201 || type_len == 0x0600) {
#define IPX_HDR_LEN 30
#define IPX_MAX_DATA 546
int ipxChecksum = (ptr[0] == 0xff && ptr[1] == 0xff);
int ipxLen = (ptr[2] << 8) + ptr[3];
if(ipxChecksum &&
ipxLen >= IPX_HDR_LEN &&
ipxLen <= (IPX_HDR_LEN + IPX_MAX_DATA))
/* we don't do anything with IPX here */
return;
}
if(type_len <= NFT_MAX_8023_LEN) {
/* assume 802.3+802.2 header */
/* check for SNAP */
if(ptr[0] == 0xAA &&
ptr[1] == 0xAA &&
ptr[2] == 0x03) {
ptr += 3;
if(ptr[0] != 0 ||
ptr[1] != 0 ||
ptr[2] != 0) {
sf_log(sample,"VSNAP_OUI %02X-%02X-%02X\n", ptr[0], ptr[1], ptr[2]);
return; /* no further decode for vendor-specific protocol */
}
ptr += 3;
/* OUI == 00-00-00 means the next two bytes are the ethernet type (RFC 2895) */
type_len = (ptr[0] << 8) + ptr[1];
ptr += 2;
}
else {
if (ptr[0] == 0x06 &&
ptr[1] == 0x06 &&
(ptr[2] & 0x01)) {
/* IP over 8022 */
ptr += 3;
/* force the type_len to be IP so we can inline the IP decode below */
type_len = 0x0800;
}
else return;
}
}
/* assume type_len is an ethernet-type now */
sample->eth_type = type_len;
if(type_len == 0x0800) {
/* IPV4 - check again that we have enough header bytes */
if((end - ptr) < sizeof(struct myiphdr)) return;
/* look at first byte of header.... */
/* ___________________________ */
/* | version | hdrlen | */
/* --------------------------- */
if((*ptr >> 4) != 4) return; /* not version 4 */
if((*ptr & 15) < 5) return; /* not IP (hdr len must be 5 quads or more) */
/* survived all the tests - store the offset to the start of the ip header */
sample->gotIPV4 = YES;
sample->offsetToIPV4 = (ptr - start);
}
if(type_len == 0x86DD) {
/* IPV6 */
/* look at first byte of header.... */
if((*ptr >> 4) != 6) return; /* not version 6 */
/* survived all the tests - store the offset to the start of the ip6 header */
sample->gotIPV6 = YES;
sample->offsetToIPV6 = (ptr - start);
}
}
/*_________________---------------------------__________________
_________________ decode80211MAC __________________
-----------------___________________________------------------
store the offset to the start of the ipv4 header in the sequence_number field
or -1 if not found.
*/
#define WIFI_MIN_HDR_SIZ 24
static void decode80211MAC(SFSample *sample)
{
uint8_t *start = sample->header;
uint8_t *end = start + sample->headerLen;
uint8_t *ptr = start;
/* assume not found */
sample->gotIPV4 = NO;
sample->gotIPV6 = NO;
if(sample->headerLen < WIFI_MIN_HDR_SIZ) return; /* not enough for an 80211 MAC header */
uint32_t fc = (ptr[1] << 8) + ptr[0]; /* [b7..b0][b15..b8] */
uint32_t protocolVersion = fc & 3;
uint32_t control = (fc >> 2) & 3;
uint32_t subType = (fc >> 4) & 15;
uint32_t toDS = (fc >> 8) & 1;
uint32_t fromDS = (fc >> 9) & 1;
uint32_t moreFrag = (fc >> 10) & 1;
uint32_t retry = (fc >> 11) & 1;
uint32_t pwrMgt = (fc >> 12) & 1;
uint32_t moreData = (fc >> 13) & 1;
uint32_t encrypted = (fc >> 14) & 1;
uint32_t order = fc >> 15;
ptr += 2;
uint32_t duration_id = (ptr[1] << 8) + ptr[0]; /* not in network byte order either? */
ptr += 2;
switch(control) {
case 0: /* mgmt */
case 1: /* ctrl */
case 3: /* rsvd */
break;
case 2: /* data */
{
uint8_t *macAddr1 = ptr;
ptr += 6;
uint8_t *macAddr2 = ptr;
ptr += 6;
uint8_t *macAddr3 = ptr;
ptr += 6;
uint32_t sequence = (ptr[0] << 8) + ptr[1];
ptr += 2;
/* ToDS FromDS Addr1 Addr2 Addr3 Addr4
0 0 DA SA BSSID N/A (ad-hoc)
0 1 DA BSSID SA N/A
1 0 BSSID SA DA N/A
1 1 RA TA DA SA (wireless bridge) */
uint8_t *rxMAC = macAddr1;
uint8_t *txMAC = macAddr2;
uint8_t *srcMAC = NULL;
uint8_t *dstMAC = NULL;
if(toDS) {
dstMAC = macAddr3;
if(fromDS) {
srcMAC = ptr; /* macAddr4. 1,1 => (wireless bridge) */
ptr += 6;
}
else srcMAC = macAddr2; /* 1,0 */
}
else {
dstMAC = macAddr1;
if(fromDS) srcMAC = macAddr3; /* 0,1 */
else srcMAC = macAddr2; /* 0,0 */
}
if(srcMAC) {
sf_log(sample,"srcMAC %02x%02x%02x%02x%02x%02x\n", srcMAC[0], srcMAC[1], srcMAC[2], srcMAC[3], srcMAC[4], srcMAC[5]);
memcpy(sample->eth_src, srcMAC, 6);
}
if(dstMAC) {
sf_log(sample,"dstMAC %02x%02x%02x%02x%02x%02x\n", dstMAC[0], dstMAC[1], dstMAC[2], dstMAC[3], dstMAC[4], dstMAC[5]);
memcpy(sample->eth_dst, srcMAC, 6);
}
if(txMAC) sf_log(sample,"txMAC %02x%02x%02x%02x%02x%02x\n", txMAC[0], txMAC[1], txMAC[2], txMAC[3], txMAC[4], txMAC[5]);
if(rxMAC) sf_log(sample,"rxMAC %02x%02x%02x%02x%02x%02x\n", rxMAC[0], rxMAC[1], rxMAC[2], rxMAC[3], rxMAC[4], rxMAC[5]);
}
}
}
/*_________________---------------------------__________________
_________________ decodeIPLayer4 __________________
-----------------___________________________------------------
*/