File: icmp.c

package info (click to toggle)
ipgrab 0.9.9-1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k, lenny, sarge
  • size: 1,712 kB
  • ctags: 2,059
  • sloc: ansic: 11,983; sh: 878; makefile: 52
file content (457 lines) | stat: -rw-r--r-- 11,009 bytes parent folder | download
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
/**************************************************************************** 
** File: icmp.c
**
** Author: Mike Borella
**
** Comments: Dump ICMP information
**
** $Id: icmp.c,v 1.17 2002/01/03 00:04:01 mborella Exp $
**
** 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 Library 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
**
*****************************************************************************/

#include "icmp.h"
#include "ip.h"

#define HOLDER_SIZE 64

/*
 * Format of ICMP echo request and reply
 */

typedef struct icmp_echo
{
  u_int16_t id;
  u_int16_t seqno;
} icmp_echo_t;

/*
 * Format of ICMP mask request and reply
 */

typedef struct icmp_mask
{
  u_int16_t id;
  u_int16_t seqno;
  u_int32_t mask;
} icmp_mask_t;

/*
 * Format of ICMP router advertisement
 */

typedef struct icmp_routeradvert
{
  u_int8_t  addresses;
  u_int8_t  address_size;
  u_int16_t lifetime;
} icmp_routeradvert_t;

/* 
 * Contains the descriptions of ICMP types 
 */

strmap_t icmp_type_map[] = 
{
  { ICMP_TYPE_ECHOREPLY ,      "echo reply" },
  { ICMP_TYPE_DESTUNREACHABLE, "destination unreachable" },
  { ICMP_TYPE_SOURCEQUENCH,    "source quench" },
  { ICMP_TYPE_REDIRECT,        "redirect" },
  { ICMP_TYPE_ECHOREQUEST,     "echo request" },
  { ICMP_TYPE_ROUTERADVERT,    "router advertisement" },
  { ICMP_TYPE_ROUTERSOLICIT,   "router solicitation" },
  { ICMP_TYPE_TIMEEXCEEDED,    "time exceeded" },
  { ICMP_TYPE_PARAMPROBLEM,    "parameter problem" },
  { ICMP_TYPE_TIMESTAMP,       "timestamp" },
  { ICMP_TYPE_TIMESTAMPREPLY,  "timestamp reply" },
  { ICMP_TYPE_INFOREQUEST,     "information request" },
  { ICMP_TYPE_INFOREPLY,       "information reply" },
  { ICMP_TYPE_MASKREQUEST,     "mask request" },
  { ICMP_TYPE_MASKREPLY,       "mask reply" },
  { ICMP_TYPE_TRACEROUTE,      "traceroute" },
  { ICMP_TYPE_CONVERSIONERROR, "datagram conversion error" },
  { 0, "" }
};

/* 
 * Contains the descriptions of ICMP destination unreachable messages
 */

strmap_t icmp_du_map[] = 
{
  { ICMP_DU_NET,               "network unreachable" },
  { ICMP_DU_HOST,              "host unreachable" },
  { ICMP_DU_PROTOCOL,          "protocol unreachable" },
  { ICMP_DU_PORT,              "port unreachable"},
  { ICMP_DU_FRAG,              "fragmentation needed"},
  { ICMP_DU_SRCRTEFAIL,        "source route failed"},
  { ICMP_DU_NETUNKNOWN,        "network unknown"},
  { ICMP_DU_HOSTUNKNOWN,       "host unknown"},
  { ICMP_DU_SRCISOLATED,       "source isolated"},
  { ICMP_DU_NETADMIN,          "network admin prohibited"},
  { ICMP_DU_HOSTADMIN,         "host admin prohibited"},
  { ICMP_DU_NETTOS,            "network unreachable for TOS"},
  { ICMP_DU_HOSTTOS,           "host unreachable for TOS"},
  { ICMP_DU_ADMIN,             "admin prohibited"},
  { ICMP_DU_HOSTPRECVIOL,      "host precedence violation"},
  { ICMP_DU_PRECCUTOFF,        "precendence cutoff"},
  { 0, "" }
};

/* 
 * Contains the descriptions of ICMP destination unreachable messages
 */

strmap_t icmp_timeexceeded_map[] = 
{
  { ICMP_TIMEEXCEEDED_TTL,               "TTL equals 0" },
  { ICMP_TIMEEXCEEDED_REASSEMBLY,        "TTL expired in reassembly"}
};

extern struct arg_t * my_args;

/*----------------------------------------------------------------------------
**
** dump_icmp_echo()
**
** Parse ICMP echo request and echo reply fields.  Note that we don't have to 
** check for the -n option because if it is set, we shouldn't ever get here.
**
**----------------------------------------------------------------------------
*/

void dump_icmp_echo(packet_t *pkt)
{
  icmp_echo_t echo;

  /*
   * Get the Echo Request/Reply fields
   */

  if (get_packet_bytes((u_int8_t *) &echo, pkt, 4) == 0)
    return;

  /*
   * Dump the ID and sequence #
   */

  if (my_args->m)
    {
      display_minimal((u_int8_t *) &echo.seqno, 2, DISP_DEC);
      display_minimal_string(" ");
    }
  else
    {
      display("Identifier", (u_int8_t *) &echo.id, 2, DISP_DEC);
      display("Sequence number", (u_int8_t *) &echo.seqno, 2, DISP_DEC);
    }
}

/*----------------------------------------------------------------------------
**
** dump_icmp_mask()
**
** Parse ICMP mask request and reply fields
**
**----------------------------------------------------------------------------
*/

void dump_icmp_mask(packet_t *pkt)
{
  icmp_mask_t mask;

  /*
   * Get the Mask Request/Reply fields
   */

  if (get_packet_bytes((u_int8_t *) &mask, pkt, 8) == 0)
    return;

  /*
   * Dump the mask
   */

  if (my_args->m)
    {
      display_minimal_ipv4((u_int8_t *) &mask.mask);
    }
  else
    {
      display_ipv4("Mask", (u_int8_t *) &mask.mask);
    }

}

/*----------------------------------------------------------------------------
**
** dump_icmp_routeradvert()
**
** Parse ICMP router advertisement
**
**----------------------------------------------------------------------------
*/

void dump_icmp_routeradvert(packet_t *pkt)
{
  icmp_routeradvert_t ra;
  u_int32_t addr;
  u_int32_t preference;
  int i;

  /*
   * Get the basic fields
   */

  if (get_packet_bytes((u_int8_t *) &ra, pkt, sizeof(ra)) == 0)
    return;

  /* Conversions */
  ra.lifetime = ntohs(ra.lifetime);

  /*
   * Dump the basic fields
   */

  if (my_args->m)
    {

    }
  else
    {
      display("Addresses", (u_int8_t *) &ra.addresses, 1, DISP_DEC);
      display("Address size", (u_int8_t *) &ra.address_size, 1, DISP_DEC);
      display("Lifetime", (u_int8_t *) &ra.lifetime, 2, DISP_DEC);
    }

  /* 
   * Loop through addresses
   */

  for (i=0; i < ra.addresses; i++)
    {
      if (get_packet_bytes((u_int8_t *) &addr, pkt, 4) == 0)
	return;
      if (get_packet_bytes((u_int8_t *) &preference, pkt, 4) == 0)
	return;

      /* Conversions */
      preference = ntohl(preference);

      if (my_args->m)
	{
	  display_minimal_ipv4((u_int8_t *) &addr);
	  display_minimal_string("(");
	  display_minimal((u_int8_t *) &preference, 4, DISP_DEC);
	  display_minimal_string(") ");
	}
      else
	{
	  display_ipv4("Address", (u_int8_t *) &addr);
	  display("Preference", (u_int8_t *) &preference, 4, DISP_DEC);
	}
    }
}

/*----------------------------------------------------------------------------
**
** dump_icmp()
**
** Parse ICMP header and dump fields
**
**----------------------------------------------------------------------------
*/

void dump_icmp(packet_t *pkt)
{
  icmp_header_t icmp;
  char holder[HOLDER_SIZE];

  /* Set the layer */
  set_layer(LAYER_NETWORK);

  /*
   * Stats accounting
   */

  stats_update(STATS_ICMP);

  /*
   * Get the header
   */

  if (get_packet_bytes((u_int8_t *) &icmp, pkt, 4) == 0)
    return;

  if (my_args->m)
    {
      /* In minimal mode lets just dump the type and code */
      display_minimal_string("| ICMP ");
      display_minimal_string(map2str(icmp_type_map, icmp.type));
      display_minimal_string(" ");
      switch(icmp.type)
	{
	case ICMP_TYPE_DESTUNREACHABLE:
	  display_minimal_string(map2str(icmp_du_map, icmp.code));
	  break;
	  
	case ICMP_TYPE_TIMEEXCEEDED:
	  display_minimal_string(map2str(icmp_timeexceeded_map, icmp.code));
	  break;
	    
	case ICMP_TYPE_ECHOREPLY:
	case ICMP_TYPE_ECHOREQUEST:
	  dump_icmp_echo(pkt);
	  break;

	case ICMP_TYPE_MASKREQUEST:
	case ICMP_TYPE_MASKREPLY:
	  dump_icmp_mask(pkt);
	  break; 

	case ICMP_TYPE_ROUTERADVERT:
	  dump_icmp_routeradvert(pkt);
	  break;

	case ICMP_TYPE_ROUTERSOLICIT:
	  return;

	case ICMP_TYPE_SOURCEQUENCH:
	case ICMP_TYPE_REDIRECT:
	case ICMP_TYPE_PARAMPROBLEM:
	case ICMP_TYPE_TIMESTAMP:
	case ICMP_TYPE_TIMESTAMPREPLY:
	case ICMP_TYPE_INFOREQUEST:
	case ICMP_TYPE_INFOREPLY:
	case ICMP_TYPE_TRACEROUTE:
	case ICMP_TYPE_CONVERSIONERROR:
	  break;

	default:
	  break;
	}
    }
  else
    {
      /* announcement */
      display_header_banner("ICMP Header");
      
      /* Dump the type */
      snprintf(holder, HOLDER_SIZE, "%d (%s)", icmp.type, 
	      map2str(icmp_type_map, icmp.type));
      display("Type", (u_int8_t *) holder, strlen(holder), DISP_STRING);
      
      /* 
       * Based on the type, dump the code, if the type has codes
       */
      
      switch(icmp.type)
	{
	case ICMP_TYPE_DESTUNREACHABLE:
	  snprintf(holder, HOLDER_SIZE, "%d (%s)", icmp.code, 
		  map2str(icmp_du_map, icmp.code));
	  display("Code", (u_int8_t *) &holder, strlen(holder), DISP_STRING);
	  break;
	  
	case ICMP_TYPE_TIMEEXCEEDED:
	  snprintf(holder, HOLDER_SIZE, "%d (%s)", icmp.code, 
		  map2str(icmp_timeexceeded_map, icmp.code));
	  display("Code", (u_int8_t *) &holder, strlen(holder), DISP_STRING);
	  break;
	  
	default:
	  display("Code", (u_int8_t *) &icmp.code, 1, DISP_DEC);
	  break;
	}
      
      icmp.checksum = ntohs(icmp.checksum);
      display("Checksum", (u_int8_t *) &icmp.checksum, 2, DISP_DEC);
      
      /* 
       * Call an ICMP-type-specific function 
       */
      
      switch (icmp.type) 
	{
	case ICMP_TYPE_ECHOREPLY:
	case ICMP_TYPE_ECHOREQUEST:
	  dump_icmp_echo(pkt);
	  
	  /* dump the hex buffer */
	  hexbuffer_flush();
	  
	  return; /* bail so that we don't continue reading */
	  
	case ICMP_TYPE_DESTUNREACHABLE:
	  /* Get the unused bytes */
	  if (get_packet_bytes((u_int8_t *) &holder, pkt, 4) == 0)
	    return;
	  break;
	  
	case ICMP_TYPE_ROUTERSOLICIT:
	  /* Get the unused bytes */
	  if (get_packet_bytes((u_int8_t *) &holder, pkt, 4) == 0)
	    return;
	  /* dump the hex buffer */
	  hexbuffer_flush();
	  return;
	  
	case ICMP_TYPE_ROUTERADVERT:
	  dump_icmp_routeradvert(pkt);
	  /* dump the hex buffer */
	  hexbuffer_flush();
	  return;
	  
	case ICMP_TYPE_SOURCEQUENCH:
	case ICMP_TYPE_REDIRECT:
	case ICMP_TYPE_PARAMPROBLEM:
	case ICMP_TYPE_TIMESTAMP:
	case ICMP_TYPE_TIMESTAMPREPLY:
	case ICMP_TYPE_INFOREQUEST:
	case ICMP_TYPE_INFOREPLY:
	case ICMP_TYPE_TRACEROUTE:
	case ICMP_TYPE_CONVERSIONERROR:
	  break;
	  
	case ICMP_TYPE_TIMEEXCEEDED:
	  /* Get the unused bytes */
	  if (get_packet_bytes((u_int8_t *) &holder, pkt, 4) == 0)
	    return;
	  break;
	  
	case ICMP_TYPE_MASKREQUEST:
	case ICMP_TYPE_MASKREPLY:
	  dump_icmp_mask(pkt);
	  break;
	  
	default:
	  break;
	  
	}
      
      /* dump the hex buffer */
      hexbuffer_flush();
      
      /*
       * In most ICMP packets there will be an IP header after the ICMP hdr.
       * If it seems to be there, we can parse it out
       */
      
      if (get_packet_apparentbytesleft(pkt) >= 20)
	dump_ip(pkt);
    }
  
}