-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathph_icmp.cc
541 lines (383 loc) · 16.2 KB
/
ph_icmp.cc
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
/*
* Copyright (c) 2001 - 2003
* Sebastian Majewski. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by Sebastian Majewski
* and his contributors.
* 4. Neither the name of Sebastian Majewski nor the names of his contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include <netz.h>
#include "netz/cksum.h"
#include "support.h"
#include "ph_packet.h"
#include "ip_proto.h"
c_string print_icmp_info(u_int, u_int);
c_string print_ip_info(c_ip_header, byte*);
c_string icmp_packet_handler(c_packet_info packet_info)
{
c_string output_string;
c_icmp_header header(packet_info.packet);
output_string.add((char*)"ICMP\t");
output_string.add((char*)"TYPE %u CODE %u '",
header.get_type(), header.get_code());
output_string += print_icmp_info(header.get_type(), header.get_code());
output_string.add((char*)"'\n");
output_string.add((char*)"\t(LEN %u) CKSUM %u ",
packet_info.packet_len, header.get_cksum());
if(!cksum(packet_info.packet, packet_info.packet_len))
{
output_string.add((char*)"(OK)");
}
else
{
output_string.add((char*)"(BAD)");
}
output_string.add((char*)" ");
switch(header.get_type())
{
case ICMP_ECHOREPLY: {
c_icmp_message_echoreply message(header);
output_string.add((char*)"ID %u SQN %u\n",
message.get_id(),
message.get_seqnumber());
break;
}
case ICMP_UNREACH: {
c_icmp_message_unreach message(header);
output_string += print_ip_info(message.get_ipheader(),
message.get_ipdata());
output_string.add((char*)"\n");
break;
}
case ICMP_SOURCEQUENCH: {
c_icmp_message_sourcequench message(header);
output_string += print_ip_info(message.get_ipheader(),
message.get_ipdata());
output_string.add((char*)"\n");
break;
}
case ICMP_REDIRECT: {
c_icmp_message_redirect message(header);
string gateway_string[16];
output_string.add((char*)"GATE %s",
conv_ip_str(gateway_string, message.get_gateway()));
output_string += print_ip_info(message.get_ipheader(),
message.get_ipdata());
output_string.add((char*)"\n");
break;
}
case ICMP_ECHOREQUEST: {
c_icmp_message_echorequest message(header);
output_string.add((char*)"ID %u SQN %u\n",
message.get_id(),
message.get_seqnumber());
break;
}
case ICMP_TIMEXCEED: {
c_icmp_message_timexceed message(header);
output_string += print_ip_info(message.get_ipheader(),
message.get_ipdata());
output_string.add((char*)"\n");
break;
}
case ICMP_PARAMPROB: {
c_icmp_message_paramprob message(header);
output_string.add((char*)"PTR %u",
message.get_pointer());
output_string += print_ip_info(message.get_ipheader(),
message.get_ipdata());
output_string.add((char*)"\n");
break;
}
case ICMP_ROUTERADVERT: {
c_icmp_message_routeradvert message(header);
output_string.add((char*)"NUM %u SIZE %u LTIME %u",
message.get_addrnumber(),
message.get_addrentrysize(),
message.get_lifetime());
string router_string[16];
for(int i = 0; i < message.get_addrnumber(); i++)
{
output_string.add((char*)"\n \tROUTER %s PLEVEL %u",
conv_ip_str(router_string, message.get_address(i)),
message.get_plevel(i));
}
output_string.add((char*)"\n");
break;
}
case ICMP_TSREQUEST: {
c_icmp_message_tsrequest message(header);
output_string.add((char*)"ID %u SQN %u",
message.get_id(),
message.get_seqnumber());
output_string.add((char*)"\n \tORG %u REC %u TRS %u",
message.get_originate(),
message.get_receive(),
message.get_transmit());
output_string.add((char*)"\n");
break;
}
case ICMP_INFOREQUEST: {
c_icmp_message_inforequest message(header);
output_string.add((char*)"ID %u SQN %u\n",
message.get_id(),
message.get_seqnumber());
break;
}
case ICMP_INFOREPLY: {
c_icmp_message_inforeply message(header);
output_string.add((char*)"ID %u SQN %u\n",
message.get_id(),
message.get_seqnumber());
break;
}
case ICMP_MASKREQUEST: {
c_icmp_message_maskrequest message(header);
output_string.add((char*)"ID %u SQN %u",
message.get_id(),
message.get_seqnumber());
string mask_string[16];
output_string.add((char*)"\n \tMASK %s\n",
conv_ip_str(mask_string, message.get_mask()));
break;
}
case ICMP_MASKREPLY: {
c_icmp_message_maskreply message(header);
output_string.add((char*)"ID %u SQN %u",
message.get_id(),
message.get_seqnumber());
string mask_string[16];
output_string.add((char*)"\n \tMASK %s\n",
conv_ip_str(mask_string, message.get_mask()));
break;
}
case ICMP_TRACEROUTE: {
c_icmp_message_traceroute message(header);
output_string.add((char*)"ID %u",
message.get_id());
output_string.add((char*)"\n \tOUT %u RET %u SPD %u MTU %u\n",
message.get_outhopcount(),
message.get_rethopcount(),
message.get_outlinkspeed(),
message.get_outlinkmtu());
break;
}
case ICMP_SECURITY: {
c_icmp_message_security message(header);
output_string.add((char*)"PTR %u",
message.get_pointer());
output_string += print_ip_info(message.get_ipheader(),
message.get_ipdata());
output_string.add((char*)"\n");
break;
}
default:
output_string.add((char*)"\n");
}
output_string += debug(packet_info);
output_string += print_line();
return output_string;
}
c_string print_icmp_info(u_int type, u_int code)
{
switch(type)
{
case ICMP_ECHOREPLY:
return c_string((char*)"Echo Reply");
case ICMP_UNREACH:
switch(code)
{
case ICMP_UNREACH_NET:
return c_string((char*)"Net Unreachable");
case ICMP_UNREACH_HOST:
return c_string((char*)"Host Unreachable");
case ICMP_UNREACH_PROTOCOL:
return c_string((char*)"Protocol Unreachable");
case ICMP_UNREACH_PORT:
return c_string((char*)"Port Unreachable");
case ICMP_UNREACH_NEEDFRAG:
return c_string((char*)"Fragmentation Needed and Don't Fragment was Set");
case ICMP_UNREACH_SRCFAIL:
return c_string((char*)"Source Route Failed");
case ICMP_UNREACH_NETUNK:
return c_string((char*)"Destination Network Unknown");
case ICMP_UNREACH_HOSTUNK:
return c_string((char*)"Destination Host Unknown");
case ICMP_UNREACH_ISOLATED:
return c_string((char*)"Source Host Isolated");
case ICMP_UNREACH_NETPROHIB:
return c_string((char*)"Comm. with Dest. Net. is Adm. Prohib.");
case ICMP_UNREACH_TOSNET:
return c_string((char*)"Comm. with Dest. Host is Adm. Prohib.");
case ICMP_UNREACH_TOSHOST:
return c_string((char*)"Destination Network Unreachable for ToS");
case ICMP_UNREACH_FILTERPROHIB:
return c_string((char*)"Destination Host Unreachable for ToS");
}
case ICMP_SOURCEQUENCH:
return c_string((char*)"Source Quench");
case ICMP_REDIRECT:
switch(code)
{
case ICMP_REDIRECT_NET:
return c_string((char*)"Redirect Datagram for the Network (or subnet)");
case ICMP_REDIRECT_HOST:
return c_string((char*)"Redirect Datagram for the Host");
case ICMP_REDIRECT_TOSNET:
return c_string((char*)"Redirect Datagram for the Type of Service and Network");
case ICMP_REDIRECT_TOSHOST:
return c_string((char*)"Redirect Datagram for the ToS and Host");
}
case ICMP_ALTHOSTADDR:
return c_string((char*)"Alternate Address for Host");
case ICMP_ECHOREQUEST:
return c_string((char*)"Echo");
case ICMP_ROUTERADVERT:
return c_string((char*)"Router Advertisement");
case ICMP_ROUTERSOLICIT:
return c_string((char*)"Router Solicitation");
case ICMP_TIMEXCEED:
switch(code)
{
case ICMP_TIMEXCEED_INTRANS:
return c_string((char*)"Time to Live exceeded in Transit");
case ICMP_TIMEXCEED_REASS:
return c_string((char*)"Fragment Reassembly Time Exceeded");
}
case ICMP_PARAMPROB:
switch(code)
{
case ICMP_PARAMPROB_ERRATPTR:
return c_string((char*)"Pointer Indicates the Error");
case ICMP_PARAMPROB_OPTABSENT:
return c_string((char*)"Missing a Required Option");
case ICMP_PARAMPROB_LENGTH:
return c_string((char*)"Bad Length");
}
case ICMP_TSREQUEST:
return c_string((char*)"Timestamp Request");
case ICMP_TSREPLY:
return c_string((char*)"Timestamp Reply");
case ICMP_INFOREQUEST:
return c_string((char*)"Information Request");
case ICMP_INFOREPLY:
return c_string((char*)"Information Reply");
case ICMP_MASKREQUEST:
return c_string((char*)"Address Mask Request");
case ICMP_MASKREPLY:
return c_string((char*)"Address Mask Reply");
case ICMP_RESERVED19:
return c_string((char*)"Reserved-19 (for Security)");
case ICMP_RESERVED20:
return c_string((char*)"Reserved-20 (for Robustness Experiment)");
case ICMP_RESERVED21:
return c_string((char*)"Reserved-21 (for Robustness Experiment)");
case ICMP_RESERVED22:
return c_string((char*)"Reserved-22 (for Robustness Experiment)");
case ICMP_RESERVED23:
return c_string((char*)"Reserved-23 (for Robustness Experiment)");
case ICMP_RESERVED24:
return c_string((char*)"Reserved-24 (for Robustness Experiment)");
case ICMP_RESERVED25:
return c_string((char*)"Reserved-25 (for Robustness Experiment)");
case ICMP_RESERVED26:
return c_string((char*)"Reserved-26 (for Robustness Experiment)");
case ICMP_RESERVED27:
return c_string((char*)"Reserved-27 (for Robustness Experiment)");
case ICMP_RESERVED28:
return c_string((char*)"Reserved-28 (for Robustness Experiment)");
case ICMP_RESERVED29:
return c_string((char*)"Reserved-29 (for Robustness Experiment)");
case ICMP_TRACEROUTE:
return c_string((char*)"Traceroute");
case ICMP_CONVERR:
return c_string((char*)"Datagram Conversion Error");
case ICMP_MOBHRED:
return c_string((char*)"Mobile Host Redirect");
case ICMP_WHEREAREYOU:
return c_string((char*)"IPv6 Where-Are-You");
case ICMP_IAMHERE:
return c_string((char*)"IPv6 I-Am-Here");
case ICMP_MOBREGREQ:
return c_string((char*)"Mobile Registration Request");
case ICMP_MOBREGREP:
return c_string((char*)"Mobile Registration Reply");
case ICMP_SKIP:
return c_string((char*)"Skip");
case ICMP_SECURITY:
switch(code)
{
case ICMP_SECURITY_BADSPI:
return c_string((char*)"Security - Bad SPI");
case ICMP_SECURITY_AUTHENFAIL:
return c_string((char*)"Security - Authenticaton Failed");
case ICMP_SECURITY_DECOMPFAIL:
return c_string((char*)"Security - Decompression Failed");
case ICMP_SECURITY_DECRYPFAIL:
return c_string((char*)"Security - Decryption Failed");
case ICMP_SECURITY_NEEDAUTHEN:
return c_string((char*)"Security - Need Authentication");
case ICMP_SECURITY_NEEDAUTHOR:
return c_string((char*)"Security - Need Authorization");
}
default:
return c_string((char*)"Unknown");
}
}
c_string print_ip_info(c_ip_header header, byte* data)
{
c_string output_string;
string src_string[16];
string dst_string[16];
output_string.add((char*)"\n\nIP HDR\tSRC %s DST %s VER %u CKSUM %u ",
conv_ip_str(src_string, header.get_src()),
conv_ip_str(dst_string, header.get_dst()),
header.get_ver(),
header.get_cksum());
output_string.add((char*)"ID %u\n\tHLEN %u LEN %u TTL %u ",
header.get_id(),
header.get_hlen(),
header.get_len(),
header.get_ttl());
output_string.add_hex((char*)"TOS 0x%02X ",
header.get_tos());
output_string.add_hex((char*)"FRAG 0x%02X ",
header.get_frag());
output_string.add((char*)"PROTO 0x%02X (",
header.get_proto());
output_string += print_ip_proto(header.get_proto());
output_string.add((char*)")\n\n");
output_string.add_hex((char*)"IP DTA\t0x%02X ", data[0]);
output_string.add_hex((char*)"0x%02X ", data[1]);
output_string.add_hex((char*)"0x%02X ", data[2]);
output_string.add_hex((char*)"0x%02X ", data[3]);
output_string.add_hex((char*)"0x%02X ", data[4]);
output_string.add_hex((char*)"0x%02X ", data[5]);
output_string.add_hex((char*)"0x%02X ", data[6]);
output_string.add_hex((char*)"0x%02X", data[7]);
return output_string;
}