/
main.c
256 lines (216 loc) · 6.85 KB
/
main.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
//---------------------------------------------------------------------------------------------
//
// Copyright (c) 2017 - 2019 , fmad engineering llc
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// 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
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
//
// Fast PCAP de-encapsulation tool.
//
// Automatically de-encapsulate PCAPs with some basic filtering
//
//
//---------------------------------------------------------------------------------------------
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdarg.h>
#include <string.h>
#include <signal.h>
#include <pthread.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <sys/shm.h>
#include <sys/ioctl.h>
//#include <linux/sched.h>
#include "fTypes.h"
#include "fNetwork.h"
#include "decap.h"
//-------------------------------------------------------------------------------------------------
double TSC2Nano = 0;
//-------------------------------------------------------------------------------------------------
static void Help(void)
{
fprintf(stderr, "pcap_decap \n");
fprintf(stderr, "\n");
fprintf(stderr, "Command works entirely based linux input / ouput pipes.\n");
fprintf(stderr, "For example:\n");
fprintf(stderr, "$ cat erspan.pcap | pcap_decap > output.pcap\n");
fprintf(stderr, "\n");
fprintf(stderr, "Options:\n");
fprintf(stderr, "-v : verbose output\n");
fprintf(stderr, "-vv : dump every packet\n");
fprintf(stderr, "\n");
fprintf(stderr, "Protocols:\n");
fprintf(stderr, "--ixia : Ixia X40 Stream. replace FCS with a TS\n");
fprintf(stderr, "--arista7130 : Arista 7130 footer mode (Metamako)\n");
fprintf(stderr, "--arista7130-double : Arista 7130 footer double tagged\n");
fprintf(stderr, "--arista7150-insert : Arista 7150 insert FCS with a TS\n");
fprintf(stderr, "--arista7150-replace : Arista 7150 replace FCS with a TS\n");
fprintf(stderr, "--arista7280-mac48 : Arista 7280 48bit source MAC overwrite\n");
fprintf(stderr, "--arista7280-eth64 : Arista 7280 64bit Ethernet Header\n");
fprintf(stderr, "--cisco3550 : Cisco 3550 (Exablaze)\n");
fprintf(stderr, "\n");
}
//-------------------------------------------------------------------------------------------------
// prints number comma seperated
u8* PrettyNumber(u64 num)
{
// nasty ... but dont have t care about collecting
static u8 out[128][128];
static u32 out_pos = 0;
u8* Buffer = out[out_pos++];
out_pos = out_pos & 127;
u8 Value[128];
sprintf(Value, "%lli", num);
memset(Buffer, 0x20, 128);
int pos = 0;
int dec = 0;
for (int i=strlen(Value)-1; i >= 0; i--)
{
Buffer[23 - pos] = Value[i];
pos++;
if ((i != 0) && ((dec % 3) == 2))
{
Buffer[23 - pos] = ',';
pos++;
}
dec++;
}
Buffer[24] = 0;
return Buffer;
}
//-------------------------------------------------------------------------------------------------
void trace(char* Message, ...)
{
va_list arglist;
va_start(arglist, Message);
char buf[16*1024];
vsprintf(buf, Message, arglist);
fprintf(stderr, "%s", buf);
fflush(stderr);
}
//-------------------------------------------------------------------------------------------------
int main(int argc, char* argv[])
{
fprintf(stderr, "PCAP De-encapsuation : FMADIO 10G 40G 100G Packet Capture : http://www.fmad.io\n");
for (int i=1; i < argc; i++)
{
if (strcmp(argv[i], "--help") == 0)
{
Help();
return 0;
}
}
FILE* InFile = stdin;
FILE* OutFile = stdout;
// write output pcap header
PCAPHeader_t Header;
Header.Magic = PCAPHEADER_MAGIC_NANO;
Header.Major = PCAPHEADER_MAJOR;
Header.Minor = PCAPHEADER_MINOR;
Header.TimeZone = 0;
Header.SigFlag = 0;
Header.SnapLen = 65535;
Header.Link = PCAPHEADER_LINK_ETHERNET;
if (fwrite(&Header, sizeof(Header), 1, OutFile) != 1)
{
fprintf(stderr, "Failed to write header to output\n");
return 0;
}
// read the header
PCAPHeader_t InputHeader;
if (fread(&InputHeader, sizeof(InputHeader), 1, InFile) != 1)
{
fprintf(stderr, "Input pcap failed to read\n");
return 0;
}
// what kind of pcap
u64 TimeScale = 0;
if (InputHeader.Magic == PCAPHEADER_MAGIC_NANO)
{
fprintf(stderr, "Found PCAP Nano\n");
TimeScale = 1;
}
if (InputHeader.Magic == PCAPHEADER_MAGIC_USEC)
{
fprintf(stderr, "Found PCAP USec\n");
TimeScale = 1000;
}
if (TimeScale == 0)
{
fprintf(stderr, "Invalid input PCAP magic. Found: %08x Expect %08x\n",
InputHeader.Magic, PCAPHEADER_MAGIC_NANO);
return 0;
}
u64 TotalBytes = 0;
u64 TotalPacket = 0;
u64 T0 = rdtsc();
u8* PktInput = malloc(32*1024);
u8* PktOutput = malloc(32*1024);
memset(PktInput, 0, 16*1024);
PCAPPacket_t HeaderInput;
PCAPPacket_t HeaderOutput;
// init protocol stats
fDecap_t* D = fDecap_Open(argc, argv);
while (true)
{
// read pcap header
if (fread(&HeaderInput, sizeof(HeaderInput), 1, InFile) != 1)
{
break;
}
if (fread(PktInput, HeaderInput.LengthCapture, 1, InFile) != 1)
{
break;
}
//fprintf(stderr, "size: %i\n", HeaderInput.LengthCapture);
// PCAP timestamp
u64 TS = (u64)HeaderInput.Sec * 1000000000ULL + (u64)HeaderInput.NSec * TimeScale;
fEther_t* Ether = (fEther_t*)PktInput;
// assume payload has no de-encapsulation
u8* Payload = PktInput + sizeof(fEther_t);
u32 PayloadLength = HeaderInput.LengthCapture - sizeof(fEther_t);
u32 MetaPort = 0;
u64 MetaTS = TS; // default assume pcap TS
u32 MetaFCS = 0;
u32 EtherProto = fDecap_Packet( D,
TS,
&Ether,
&Payload,
&PayloadLength,
&MetaPort,
&MetaTS,
&MetaFCS
);
// update capture length based on stripped packet
HeaderOutput.LengthCapture = PayloadLength + sizeof(fEther_t);
HeaderOutput.LengthWire = HeaderInput.LengthWire;
// re-write timestamp, decoder may have updated
HeaderOutput.Sec = MetaTS / 1000000000ULL;
HeaderOutput.NSec = MetaTS % 1000000000ULL;
fwrite(&HeaderOutput, sizeof(HeaderOutput), 1, OutFile);
// write ether header
Ether->Proto = swap16(EtherProto);
fwrite(Ether, sizeof(fEther_t), 1, OutFile);
// write payload
fwrite(Payload, HeaderOutput.LengthCapture - sizeof(fEther_t), 1, OutFile);
}
// print protocol stats
fDecap_Close(D);
return 0;
}