-
Notifications
You must be signed in to change notification settings - Fork 158
/
Copy pathdumppkt.c
386 lines (341 loc) · 11.6 KB
/
dumppkt.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
#include "project.h"
#include "lwip\ip_frag.h"
#include "lwip\udp.h"
#include "lwip\tcp_impl.h"
KSPIN_LOCK DumpLock = 0;
void
__tcp_debug_print_flags(u8_t flags)
{
DbgPrint("Flags(%.2X) ",flags);
if (flags & TCP_FIN) {
DbgPrint("FIN ");
}
if (flags & TCP_SYN) {
DbgPrint("SYN ");
}
if (flags & TCP_RST) {
DbgPrint("RST ");
}
if (flags & TCP_PSH) {
DbgPrint("PSH ");
}
if (flags & TCP_ACK) {
DbgPrint("ACK ");
}
if (flags & TCP_URG) {
DbgPrint("URG ");
}
if (flags & TCP_ECE) {
DbgPrint("ECE ");
}
if (flags & TCP_CWR) {
DbgPrint("CWR ");
}
DbgPrint("\n");
}
void DumpBin( PUCHAR Buffer, ULONG Length )
{
unsigned int k, m;
Length = min(Length,40);
for ( k = 0; k < Length; k+=20)
{
for ( m = k; m < k+20 && m < Length; ++m ){
DbgPrint("%.2X ", Buffer[m]);
}
for(;m < k + 20;++m){
DbgPrint (" ");
}
for ( m = k; m < k+20 && m < Length; ++m ){
if ( isprint( Buffer[m] ))
DbgPrint ("%c", Buffer[m]);
else
DbgPrint (".");
}
DbgPrint ("\n");
}
}
void _DumpNdisBuffer( struct eth_hdr *pEthHeader, PVOID Buffer, ULONG Size )
{
DbgPrint("=================================\n");
DbgPrint("Packet dump size=%lu:\n",Size);
DbgPrint("Ethernet header:\n");
DbgPrint("+-------------------------------+\n");
if ( pEthHeader->type == PP_HTONS(ETHTYPE_ARP))
{
DbgPrint("ETHERNET: ARP\n");
}
else if(pEthHeader->type == PP_HTONS(ETHTYPE_IP))
{
DbgPrint("ETHERNET: IP\n");
}
else if(pEthHeader->type == PP_HTONS(ETHTYPE_PPPOE) )
{
DbgPrint("ETHERNET: PPPOE\n");
}else{
DbgPrint("ETHERNET: %04X\n",pEthHeader->type);
}
DbgPrint("s_addr=%.2X-%.2X-%.2X-%.2X-%.2X-%.2X\nd_addr=%.2X-%.2X-%.2X-%.2X-%.2X-%.2X\n",
pEthHeader->src.addr[0],pEthHeader->src.addr[1],pEthHeader->src.addr[2],
pEthHeader->src.addr[3],pEthHeader->src.addr[4],pEthHeader->src.addr[5],
pEthHeader->dest.addr[0],pEthHeader->dest.addr[1],pEthHeader->dest.addr[2],
pEthHeader->dest.addr[3],pEthHeader->dest.addr[4],pEthHeader->dest.addr[5]
);
if ( pEthHeader->type == PP_HTONS(ETHTYPE_ARP))
{
struct etharp_hdr *pEthrApr = (struct etharp_hdr*)Buffer;
DbgPrint("ARP header:\n");
DbgPrint("+-------------------------------+\n");
if ( Size < SIZEOF_ETHARP_HDR ){
DbgPrint("Size < SIZEOF_ETHARP_PACKET\n");
DbgPrint("=================================\n");
return;
}
if ( pEthrApr->hwtype == PP_HTONS(1) ){
DbgPrint(" ARP.hwtype = HWTYPE_ETHERNET\n");
if ( pEthrApr->proto == PP_HTONS(ETHTYPE_IP) ){
DbgPrint(" ARP.proto = IP\n");
}else{
DbgPrint(" ARP.proto = %04X\n",pEthrApr->proto);
}
if ( pEthrApr->opcode == PP_HTONS(ARP_REPLY) ){
DbgPrint(" ARP.opcode = ARP_REPLY\n");
}else if ( pEthrApr->opcode == PP_HTONS(ARP_REQUEST) ){
DbgPrint(" ARP.opcode = ARP_REQUEST\n");
} else if ( pEthrApr->opcode == PP_HTONS(3) ){
DbgPrint(" ARP.opcode = ARP_RARP_REQUEST\n");
}else if ( pEthrApr->opcode == PP_HTONS(4) ){
DbgPrint(" ARP.opcode = ARP_RARP_REPLY\n");
}else {
DbgPrint(" ARP.opcode = %04X\n",pEthrApr->opcode);
}
DbgPrint("+-------------------------------+\n");
DbgPrint("shwaddr=%.2X-%.2X-%.2X-%.2X-%.2X-%.2X\ndhwaddr=%.2X-%.2X-%.2X-%.2X-%.2X-%.2X\n",
pEthrApr->shwaddr.addr[0],pEthrApr->shwaddr.addr[1],pEthrApr->shwaddr.addr[2],
pEthrApr->shwaddr.addr[3],pEthrApr->shwaddr.addr[4],pEthrApr->shwaddr.addr[5],
pEthrApr->dhwaddr.addr[0],pEthrApr->dhwaddr.addr[1],pEthrApr->dhwaddr.addr[2],
pEthrApr->dhwaddr.addr[3],pEthrApr->dhwaddr.addr[4],pEthrApr->dhwaddr.addr[5]
);
DbgPrint("+-------------------------------+\n");
DbgPrint("| %3"U16_F" | %3"U16_F" | %3"U16_F" | %3"U16_F" | (src)\n",
ip4_addr1_16(&pEthrApr->sipaddr),
ip4_addr2_16(&pEthrApr->sipaddr),
ip4_addr3_16(&pEthrApr->sipaddr),
ip4_addr4_16(&pEthrApr->sipaddr));
DbgPrint("+-------------------------------+\n");
DbgPrint("| %3"U16_F" | %3"U16_F" | %3"U16_F" | %3"U16_F" | (dest)\n",
ip4_addr1_16(&pEthrApr->dipaddr),
ip4_addr2_16(&pEthrApr->dipaddr),
ip4_addr3_16(&pEthrApr->dipaddr),
ip4_addr4_16(&pEthrApr->dipaddr));
DbgPrint("+-------------------------------+\n");
}else{
DbgPrint(" ARP.hwtype = %04X\n",pEthHeader->type);
}
}
else if ( pEthHeader->type == PP_HTONS(ETHTYPE_IP))
{
struct ip_hdr *iphdr = (struct ip_hdr*)Buffer;
USHORT wFragOffset;
UCHAR byRF,byDF,byMF;
ULONG ip_len,hlen = 0;
ip_addr_t src_ip;
ip_addr_t dst_ip;
DbgPrint("IP header:\n");
if ( Size < IP_HEADER_LEN ){
DbgPrint("Size < SIZEOF_ETH_HDR + IP_HEADER_LEN\n");
DbgPrint("=================================\n");
return;
}
ip_addr_copy(dst_ip, iphdr->dest);
ip_addr_copy(src_ip, iphdr->src);
// set the IP datagram length */
ip_len = ntohs( IPH_LEN(iphdr) );
// set the IP header length
hlen = IPH_HL(iphdr) << 2;
/* check for fragmented packets */
wFragOffset = ntohs(IPH_OFFSET(iphdr));
/*
* get the values of the reserved, more
* fragments and don't fragment flags
*/
byMF = (unsigned char)((wFragOffset & 0x2000) >> 13);
/* mask off the high bits in the fragment offset field */
wFragOffset &= 0x1FFF;
if ( wFragOffset || byMF ){
DbgPrint("ip fragmented, id=%04X\n",IPH_ID(iphdr));
DbgPrint("=================================\n");
return;
}
DbgPrint("+-------------------------------+\n");
DbgPrint("ip_hlen=%d\n",hlen);
DbgPrint("ip_len=%d\n",ip_len);
DbgPrint("+-------------------------------+\n");
switch (IPH_PROTO(iphdr))
{
case IP_PROTO_UDP:
DbgPrint("ip_proto=UDP\n");
break;
case IP_PROTO_TCP:
DbgPrint("ip_proto=TCP\n");
break;
case IP_PROTO_ICMP:
DbgPrint("ip_proto=ICMP\n");
break;
default:
DbgPrint("ip_proto=%02X\n",IPH_PROTO(iphdr));
break;
}
DbgPrint("+-------------------------------+\n");
DbgPrint("| %3"U16_F" | %3"U16_F" | %3"U16_F" | %3"U16_F" | (src)\n",
ip4_addr1_16(&iphdr->src),
ip4_addr2_16(&iphdr->src),
ip4_addr3_16(&iphdr->src),
ip4_addr4_16(&iphdr->src));
DbgPrint("+-------------------------------+\n");
DbgPrint("| %3"U16_F" | %3"U16_F" | %3"U16_F" | %3"U16_F" | (dest)\n",
ip4_addr1_16(&iphdr->dest),
ip4_addr2_16(&iphdr->dest),
ip4_addr3_16(&iphdr->dest),
ip4_addr4_16(&iphdr->dest));
if ( Size < SIZEOF_ETH_HDR + hlen ){
DbgPrint("Size < SIZEOF_ETH_HDR + hlen\n");
DbgPrint("=================================\n");
return;
}
if ( IPH_PROTO(iphdr) == IP_PROTO_TCP )
{
struct tcp_hdr *tcphdr = (struct tcp_hdr*)((PCHAR)Buffer+hlen);
if ( Size < SIZEOF_ETH_HDR + hlen +sizeof(struct tcp_hdr) ){
DbgPrint("Size < SIZEOF_ETH_HDR + hlen + sizeof(struct tcp_hdr)\n");
DbgPrint("=================================\n");
return;
}
DbgPrint("TCP header:\n");
DbgPrint("+-------------------------------+\n");
DbgPrint("| %5"U16_F" | %5"U16_F" | (src port, dest port)\n",
ntohs(tcphdr->src), ntohs(tcphdr->dest));
DbgPrint("+-------------------------------+\n");
DbgPrint("| %010"U32_F" | (seq no)\n",
ntohl(tcphdr->seqno));
DbgPrint("+-------------------------------+\n");
DbgPrint("| %010"U32_F" | (ack no)\n",
ntohl(tcphdr->ackno));
DbgPrint("+-------------------------------+\n");
__tcp_debug_print_flags(TCPH_FLAGS(tcphdr));
DbgPrint("+-------------------------------+\n");
DbgPrint("| 0x%04"X16_F" | %5"U16_F" | (chksum, urgp)\n",
ntohs(tcphdr->chksum), ntohs(tcphdr->urgp));
DbgPrint("+-------------------------------+\n");
DumpBin( (PUCHAR)tcphdr+sizeof(struct tcp_hdr), Size -(SIZEOF_ETH_HDR + hlen +sizeof(struct tcp_hdr)) );
}
else if ( IPH_PROTO(iphdr) == IP_PROTO_UDP )
{
struct udp_hdr *udphdr = (struct udp_hdr*)((PCHAR)Buffer+hlen);
if ( Size < SIZEOF_ETH_HDR + hlen +sizeof(struct udp_hdr) ){
DbgPrint("Size < SIZEOF_ETH_HDR + hlen + sizeof(struct udp_hdr)\n");
DbgPrint("=================================\n");
return;
}
DbgPrint("UDP header:\n");
DbgPrint("+-------------------------------+\n");
DbgPrint("| %5"U16_F" | %5"U16_F" | (src port, dest port)\n",
ntohs(udphdr->src), ntohs(udphdr->dest));
DbgPrint("+-------------------------------+\n");
DbgPrint("| %5"U16_F" | 0x%04"X16_F" | (len, chksum)\n",
ntohs(udphdr->len), ntohs(udphdr->chksum));
DbgPrint("+-------------------------------+\n");
}
else if ( IPH_PROTO(iphdr) == IP_PROTO_ICMP )
{
struct icmp_echo_hdr *icmphdr = (struct icmp_echo_hdr*)((PCHAR)Buffer+hlen);
if ( Size < SIZEOF_ETH_HDR + hlen + 2 ){
DbgPrint("Size < SIZEOF_ETH_HDR + hlen + 2\n");
DbgPrint("=================================\n");
return;
}
DbgPrint("ICMP header:\n");
DbgPrint("+-------------------------------+\n");
switch(ICMPH_TYPE(icmphdr))
{
case ICMP_ER:// 0 /* echo reply */
DbgPrint("type=ICMP_ER\n");
break;
case ICMP_DUR:// 3 /* destination unreachable */
DbgPrint("type=ICMP_DUR\n");
break;
case ICMP_SQ:// 4 /* source quench */
DbgPrint("type=ICMP_SQ\n");
break;
case ICMP_RD:// 5 /* redirect */
DbgPrint("type=ICMP_RD\n");
break;
case ICMP_ECHO:// 8 /* echo */
DbgPrint("type=ICMP_ECHO\n");
break;
case ICMP_TE:// 11 /* time exceeded */
DbgPrint("type=ICMP_TE\n");
break;
case ICMP_PP:// 12 /* parameter problem */
DbgPrint("type=ICMP_PP\n");
break;
case ICMP_TS:// 13 /* timestamp */
DbgPrint("type=ICMP_TS\n");
break;
case ICMP_TSR:// 14 /* timestamp reply */
DbgPrint("type=ICMP_TSR\n");
break;
case ICMP_IRQ:// 15 /* information request */
DbgPrint("type=ICMP_IRQ\n");
break;
case ICMP_IR:// 16 /* information reply */
DbgPrint("type=ICMP_IR\n");
break;
default:
DbgPrint("type=%.2X\n",ICMPH_TYPE(icmphdr));
break;
}
DbgPrint("+-------------------------------+\n");
}
}
DbgPrint("=================================\n");
}
void DumpNdisBuffer( PCHAR Direction, struct eth_hdr *pEthHeader, PVOID Buffer, ULONG Size )
{
KIRQL OldIrql;
KeAcquireSpinLock(&DumpLock,&OldIrql);
DbgPrint("%s /////////////////////////////////////////////////////\n",Direction);
DbgPrint("Buffer %p\n",pEthHeader);
_DumpNdisBuffer( pEthHeader, Buffer, Size );
DbgPrint("END DUMP /////////////////////////////////////////////////////\n",Direction);
KeReleaseSpinLock(&DumpLock,OldIrql);
}
void DumpNdisPacket( PCHAR Direction, PNDIS_PACKET Packet )
{
UINT nBufferCount, TotalPacketLength, uReadyLen,FirstBufferLength;
BOOLEAN fbResult = FALSE;
PNDIS_BUFFER FirstBuffer;
PVOID FirstBufferVA;
NdisGetFirstBufferFromPacket(
Packet,
&FirstBuffer,
&FirstBufferVA,
&FirstBufferLength,
&TotalPacketLength
);
if ( FirstBufferLength < SIZEOF_ETH_HDR ){
return;
}
//TEST
if ( FirstBufferLength < SIZEOF_ETH_HDR + IP_HEADER_LEN + TCP_HEADER_LEN )
return;
if ( FirstBufferVA ){
KIRQL OldIrql;
struct eth_hdr *pEthHeader = (struct eth_hdr *)FirstBufferVA;
KeAcquireSpinLock(&DumpLock,&OldIrql);
DbgPrint("%s /////////////////////////////////////////////////////\n",Direction);
DbgPrint("Packet %p\n",Packet);
_DumpNdisBuffer( pEthHeader, (PCHAR)FirstBufferVA + SIZEOF_ETH_HDR, FirstBufferLength - SIZEOF_ETH_HDR );
DbgPrint("END DUMP /////////////////////////////////////////////////////\n",Direction);
KeReleaseSpinLock(&DumpLock,OldIrql);
}
}