]> The Tcpdump Group git mirrors - tcpdump/blob - print-icmp.c
don't pass on src & dst MAC adresses to the isoclns decoder as MAC adresses
[tcpdump] / print-icmp.c
1 /*
2 * Copyright (c) 1988, 1989, 1990, 1991, 1993, 1994, 1995, 1996
3 * The Regents of the University of California. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that: (1) source code distributions
7 * retain the above copyright notice and this paragraph in its entirety, (2)
8 * distributions including binary code include the above copyright notice and
9 * this paragraph in its entirety in the documentation or other materials
10 * provided with the distribution, and (3) all advertising materials mentioning
11 * features or use of this software display the following acknowledgement:
12 * ``This product includes software developed by the University of California,
13 * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
14 * the University nor the names of its contributors may be used to endorse
15 * or promote products derived from this software without specific prior
16 * written permission.
17 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
18 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
19 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
20 */
21
22 #ifndef lint
23 static const char rcsid[] =
24 "@(#) $Header: /tcpdump/master/tcpdump/print-icmp.c,v 1.72 2003-05-15 16:58:04 hannes Exp $ (LBL)";
25 #endif
26
27 #ifdef HAVE_CONFIG_H
28 #include "config.h"
29 #endif
30
31 #include <tcpdump-stdinc.h>
32
33 #include <stdio.h>
34 #include <string.h>
35
36 #include "interface.h"
37 #include "addrtoname.h"
38 #include "extract.h" /* must come after interface.h */
39
40 #include "ip.h"
41 #include "udp.h"
42
43 /*
44 * Interface Control Message Protocol Definitions.
45 * Per RFC 792, September 1981.
46 */
47
48 /*
49 * Structure of an icmp header.
50 */
51 struct icmp {
52 u_int8_t icmp_type; /* type of message, see below */
53 u_int8_t icmp_code; /* type sub code */
54 u_int16_t icmp_cksum; /* ones complement cksum of struct */
55 union {
56 u_int8_t ih_pptr; /* ICMP_PARAMPROB */
57 struct in_addr ih_gwaddr; /* ICMP_REDIRECT */
58 struct ih_idseq {
59 u_int16_t icd_id;
60 u_int16_t icd_seq;
61 } ih_idseq;
62 u_int32_t ih_void;
63
64 /* ICMP_UNREACH_NEEDFRAG -- Path MTU Discovery (RFC1191) */
65 struct ih_pmtu {
66 u_int16_t ipm_void;
67 u_int16_t ipm_nextmtu;
68 } ih_pmtu;
69 } icmp_hun;
70 #define icmp_pptr icmp_hun.ih_pptr
71 #define icmp_gwaddr icmp_hun.ih_gwaddr
72 #define icmp_id icmp_hun.ih_idseq.icd_id
73 #define icmp_seq icmp_hun.ih_idseq.icd_seq
74 #define icmp_void icmp_hun.ih_void
75 #define icmp_pmvoid icmp_hun.ih_pmtu.ipm_void
76 #define icmp_nextmtu icmp_hun.ih_pmtu.ipm_nextmtu
77 union {
78 struct id_ts {
79 u_int32_t its_otime;
80 u_int32_t its_rtime;
81 u_int32_t its_ttime;
82 } id_ts;
83 struct id_ip {
84 struct ip idi_ip;
85 /* options and then 64 bits of data */
86 } id_ip;
87 u_int32_t id_mask;
88 u_int8_t id_data[1];
89 } icmp_dun;
90 #define icmp_otime icmp_dun.id_ts.its_otime
91 #define icmp_rtime icmp_dun.id_ts.its_rtime
92 #define icmp_ttime icmp_dun.id_ts.its_ttime
93 #define icmp_ip icmp_dun.id_ip.idi_ip
94 #define icmp_mask icmp_dun.id_mask
95 #define icmp_data icmp_dun.id_data
96 };
97
98 /*
99 * Lower bounds on packet lengths for various types.
100 * For the error advice packets must first insure that the
101 * packet is large enought to contain the returned ip header.
102 * Only then can we do the check to see if 64 bits of packet
103 * data have been returned, since we need to check the returned
104 * ip header length.
105 */
106 #define ICMP_MINLEN 8 /* abs minimum */
107 #define ICMP_TSLEN (8 + 3 * sizeof (u_int32_t)) /* timestamp */
108 #define ICMP_MASKLEN 12 /* address mask */
109 #define ICMP_ADVLENMIN (8 + sizeof (struct ip) + 8) /* min */
110 #define ICMP_ADVLEN(p) (8 + (IP_HL(&(p)->icmp_ip) << 2) + 8)
111 /* N.B.: must separately check that ip_hl >= 5 */
112
113 /*
114 * Definition of type and code field values.
115 */
116 #define ICMP_ECHOREPLY 0 /* echo reply */
117 #define ICMP_UNREACH 3 /* dest unreachable, codes: */
118 #define ICMP_UNREACH_NET 0 /* bad net */
119 #define ICMP_UNREACH_HOST 1 /* bad host */
120 #define ICMP_UNREACH_PROTOCOL 2 /* bad protocol */
121 #define ICMP_UNREACH_PORT 3 /* bad port */
122 #define ICMP_UNREACH_NEEDFRAG 4 /* IP_DF caused drop */
123 #define ICMP_UNREACH_SRCFAIL 5 /* src route failed */
124 #define ICMP_UNREACH_NET_UNKNOWN 6 /* unknown net */
125 #define ICMP_UNREACH_HOST_UNKNOWN 7 /* unknown host */
126 #define ICMP_UNREACH_ISOLATED 8 /* src host isolated */
127 #define ICMP_UNREACH_NET_PROHIB 9 /* prohibited access */
128 #define ICMP_UNREACH_HOST_PROHIB 10 /* ditto */
129 #define ICMP_UNREACH_TOSNET 11 /* bad tos for net */
130 #define ICMP_UNREACH_TOSHOST 12 /* bad tos for host */
131 #define ICMP_SOURCEQUENCH 4 /* packet lost, slow down */
132 #define ICMP_REDIRECT 5 /* shorter route, codes: */
133 #define ICMP_REDIRECT_NET 0 /* for network */
134 #define ICMP_REDIRECT_HOST 1 /* for host */
135 #define ICMP_REDIRECT_TOSNET 2 /* for tos and net */
136 #define ICMP_REDIRECT_TOSHOST 3 /* for tos and host */
137 #define ICMP_ECHO 8 /* echo service */
138 #define ICMP_ROUTERADVERT 9 /* router advertisement */
139 #define ICMP_ROUTERSOLICIT 10 /* router solicitation */
140 #define ICMP_TIMXCEED 11 /* time exceeded, code: */
141 #define ICMP_TIMXCEED_INTRANS 0 /* ttl==0 in transit */
142 #define ICMP_TIMXCEED_REASS 1 /* ttl==0 in reass */
143 #define ICMP_PARAMPROB 12 /* ip header bad */
144 #define ICMP_PARAMPROB_OPTABSENT 1 /* req. opt. absent */
145 #define ICMP_TSTAMP 13 /* timestamp request */
146 #define ICMP_TSTAMPREPLY 14 /* timestamp reply */
147 #define ICMP_IREQ 15 /* information request */
148 #define ICMP_IREQREPLY 16 /* information reply */
149 #define ICMP_MASKREQ 17 /* address mask request */
150 #define ICMP_MASKREPLY 18 /* address mask reply */
151
152 #define ICMP_MAXTYPE 18
153
154 #define ICMP_INFOTYPE(type) \
155 ((type) == ICMP_ECHOREPLY || (type) == ICMP_ECHO || \
156 (type) == ICMP_ROUTERADVERT || (type) == ICMP_ROUTERSOLICIT || \
157 (type) == ICMP_TSTAMP || (type) == ICMP_TSTAMPREPLY || \
158 (type) == ICMP_IREQ || (type) == ICMP_IREQREPLY || \
159 (type) == ICMP_MASKREQ || (type) == ICMP_MASKREPLY)
160 /* rfc1700 */
161 #ifndef ICMP_UNREACH_NET_UNKNOWN
162 #define ICMP_UNREACH_NET_UNKNOWN 6 /* destination net unknown */
163 #endif
164 #ifndef ICMP_UNREACH_HOST_UNKNOWN
165 #define ICMP_UNREACH_HOST_UNKNOWN 7 /* destination host unknown */
166 #endif
167 #ifndef ICMP_UNREACH_ISOLATED
168 #define ICMP_UNREACH_ISOLATED 8 /* source host isolated */
169 #endif
170 #ifndef ICMP_UNREACH_NET_PROHIB
171 #define ICMP_UNREACH_NET_PROHIB 9 /* admin prohibited net */
172 #endif
173 #ifndef ICMP_UNREACH_HOST_PROHIB
174 #define ICMP_UNREACH_HOST_PROHIB 10 /* admin prohibited host */
175 #endif
176 #ifndef ICMP_UNREACH_TOSNET
177 #define ICMP_UNREACH_TOSNET 11 /* tos prohibited net */
178 #endif
179 #ifndef ICMP_UNREACH_TOSHOST
180 #define ICMP_UNREACH_TOSHOST 12 /* tos prohibited host */
181 #endif
182
183 /* rfc1716 */
184 #ifndef ICMP_UNREACH_FILTER_PROHIB
185 #define ICMP_UNREACH_FILTER_PROHIB 13 /* admin prohibited filter */
186 #endif
187 #ifndef ICMP_UNREACH_HOST_PRECEDENCE
188 #define ICMP_UNREACH_HOST_PRECEDENCE 14 /* host precedence violation */
189 #endif
190 #ifndef ICMP_UNREACH_PRECEDENCE_CUTOFF
191 #define ICMP_UNREACH_PRECEDENCE_CUTOFF 15 /* precedence cutoff */
192 #endif
193
194 /* Most of the icmp types */
195 static struct tok icmp2str[] = {
196 { ICMP_ECHOREPLY, "echo reply" },
197 { ICMP_SOURCEQUENCH, "source quench" },
198 { ICMP_ECHO, "echo request" },
199 { ICMP_ROUTERSOLICIT, "router solicitation" },
200 { ICMP_TSTAMP, "time stamp request" },
201 { ICMP_TSTAMPREPLY, "time stamp reply" },
202 { ICMP_IREQ, "information request" },
203 { ICMP_IREQREPLY, "information reply" },
204 { ICMP_MASKREQ, "address mask request" },
205 { 0, NULL }
206 };
207
208 /* Formats for most of the ICMP_UNREACH codes */
209 static struct tok unreach2str[] = {
210 { ICMP_UNREACH_NET, "net %s unreachable" },
211 { ICMP_UNREACH_HOST, "host %s unreachable" },
212 { ICMP_UNREACH_SRCFAIL,
213 "%s unreachable - source route failed" },
214 { ICMP_UNREACH_NET_UNKNOWN, "net %s unreachable - unknown" },
215 { ICMP_UNREACH_HOST_UNKNOWN, "host %s unreachable - unknown" },
216 { ICMP_UNREACH_ISOLATED,
217 "%s unreachable - source host isolated" },
218 { ICMP_UNREACH_NET_PROHIB,
219 "net %s unreachable - admin prohibited" },
220 { ICMP_UNREACH_HOST_PROHIB,
221 "host %s unreachable - admin prohibited" },
222 { ICMP_UNREACH_TOSNET,
223 "net %s unreachable - tos prohibited" },
224 { ICMP_UNREACH_TOSHOST,
225 "host %s unreachable - tos prohibited" },
226 { ICMP_UNREACH_FILTER_PROHIB,
227 "host %s unreachable - admin prohibited filter" },
228 { ICMP_UNREACH_HOST_PRECEDENCE,
229 "host %s unreachable - host precedence violation" },
230 { ICMP_UNREACH_PRECEDENCE_CUTOFF,
231 "host %s unreachable - precedence cutoff" },
232 { 0, NULL }
233 };
234
235 /* Formats for the ICMP_REDIRECT codes */
236 static struct tok type2str[] = {
237 { ICMP_REDIRECT_NET, "redirect %s to net %s" },
238 { ICMP_REDIRECT_HOST, "redirect %s to host %s" },
239 { ICMP_REDIRECT_TOSNET, "redirect-tos %s to net %s" },
240 { ICMP_REDIRECT_TOSHOST, "redirect-tos %s to host %s" },
241 { 0, NULL }
242 };
243
244 /* rfc1191 */
245 struct mtu_discovery {
246 u_int16_t unused;
247 u_int16_t nexthopmtu;
248 };
249
250 /* rfc1256 */
251 struct ih_rdiscovery {
252 u_int8_t ird_addrnum;
253 u_int8_t ird_addrsiz;
254 u_int16_t ird_lifetime;
255 };
256
257 struct id_rdiscovery {
258 u_int32_t ird_addr;
259 u_int32_t ird_pref;
260 };
261
262 void
263 icmp_print(const u_char *bp, u_int plen, const u_char *bp2, int fragmented)
264 {
265 char *cp;
266 const struct icmp *dp;
267 const struct ip *ip;
268 const char *str, *fmt;
269 const struct ip *oip;
270 const struct udphdr *ouh;
271 u_int hlen, dport, mtu;
272 char buf[MAXHOSTNAMELEN + 100];
273
274 dp = (struct icmp *)bp;
275 ip = (struct ip *)bp2;
276 str = buf;
277
278 TCHECK(dp->icmp_code);
279 switch (dp->icmp_type) {
280
281 case ICMP_ECHO:
282 case ICMP_ECHOREPLY:
283 TCHECK(dp->icmp_seq);
284 (void)snprintf(buf, sizeof(buf), "echo %s seq %u",
285 dp->icmp_type == ICMP_ECHO ?
286 "request" : "reply",
287 EXTRACT_16BITS(&dp->icmp_seq));
288 break;
289
290 case ICMP_UNREACH:
291 TCHECK(dp->icmp_ip.ip_dst);
292 switch (dp->icmp_code) {
293
294 case ICMP_UNREACH_PROTOCOL:
295 TCHECK(dp->icmp_ip.ip_p);
296 (void)snprintf(buf, sizeof(buf),
297 "%s protocol %d unreachable",
298 ipaddr_string(&dp->icmp_ip.ip_dst),
299 dp->icmp_ip.ip_p);
300 break;
301
302 case ICMP_UNREACH_PORT:
303 TCHECK(dp->icmp_ip.ip_p);
304 oip = &dp->icmp_ip;
305 hlen = IP_HL(oip) * 4;
306 ouh = (struct udphdr *)(((u_char *)oip) + hlen);
307 dport = EXTRACT_16BITS(&ouh->uh_dport);
308 switch (oip->ip_p) {
309
310 case IPPROTO_TCP:
311 (void)snprintf(buf, sizeof(buf),
312 "%s tcp port %s unreachable",
313 ipaddr_string(&oip->ip_dst),
314 tcpport_string(dport));
315 break;
316
317 case IPPROTO_UDP:
318 (void)snprintf(buf, sizeof(buf),
319 "%s udp port %s unreachable",
320 ipaddr_string(&oip->ip_dst),
321 udpport_string(dport));
322 break;
323
324 default:
325 (void)snprintf(buf, sizeof(buf),
326 "%s protocol %d port %d unreachable",
327 ipaddr_string(&oip->ip_dst),
328 oip->ip_p, dport);
329 break;
330 }
331 break;
332
333 case ICMP_UNREACH_NEEDFRAG:
334 {
335 register const struct mtu_discovery *mp;
336 mp = (struct mtu_discovery *)&dp->icmp_void;
337 mtu = EXTRACT_16BITS(&mp->nexthopmtu);
338 if (mtu) {
339 (void)snprintf(buf, sizeof(buf),
340 "%s unreachable - need to frag (mtu %d)",
341 ipaddr_string(&dp->icmp_ip.ip_dst), mtu);
342 } else {
343 (void)snprintf(buf, sizeof(buf),
344 "%s unreachable - need to frag",
345 ipaddr_string(&dp->icmp_ip.ip_dst));
346 }
347 }
348 break;
349
350 default:
351 fmt = tok2str(unreach2str, "#%d %%s unreachable",
352 dp->icmp_code);
353 (void)snprintf(buf, sizeof(buf), fmt,
354 ipaddr_string(&dp->icmp_ip.ip_dst));
355 break;
356 }
357 break;
358
359 case ICMP_REDIRECT:
360 TCHECK(dp->icmp_ip.ip_dst);
361 fmt = tok2str(type2str, "redirect-#%d %%s to net %%s",
362 dp->icmp_code);
363 (void)snprintf(buf, sizeof(buf), fmt,
364 ipaddr_string(&dp->icmp_ip.ip_dst),
365 ipaddr_string(&dp->icmp_gwaddr));
366 break;
367
368 case ICMP_ROUTERADVERT:
369 {
370 register const struct ih_rdiscovery *ihp;
371 register const struct id_rdiscovery *idp;
372 u_int lifetime, num, size;
373
374 (void)snprintf(buf, sizeof(buf), "router advertisement");
375 cp = buf + strlen(buf);
376
377 ihp = (struct ih_rdiscovery *)&dp->icmp_void;
378 TCHECK(*ihp);
379 (void)strncpy(cp, " lifetime ", sizeof(buf) - (cp - buf));
380 cp = buf + strlen(buf);
381 lifetime = EXTRACT_16BITS(&ihp->ird_lifetime);
382 if (lifetime < 60) {
383 (void)snprintf(cp, sizeof(buf) - (cp - buf), "%u",
384 lifetime);
385 } else if (lifetime < 60 * 60) {
386 (void)snprintf(cp, sizeof(buf) - (cp - buf), "%u:%02u",
387 lifetime / 60, lifetime % 60);
388 } else {
389 (void)snprintf(cp, sizeof(buf) - (cp - buf),
390 "%u:%02u:%02u",
391 lifetime / 3600,
392 (lifetime % 3600) / 60,
393 lifetime % 60);
394 }
395 cp = buf + strlen(buf);
396
397 num = ihp->ird_addrnum;
398 (void)snprintf(cp, sizeof(buf) - (cp - buf), " %d:", num);
399 cp = buf + strlen(buf);
400
401 size = ihp->ird_addrsiz;
402 if (size != 2) {
403 (void)snprintf(cp, sizeof(buf) - (cp - buf),
404 " [size %d]", size);
405 break;
406 }
407 idp = (struct id_rdiscovery *)&dp->icmp_data;
408 while (num-- > 0) {
409 TCHECK(*idp);
410 (void)snprintf(cp, sizeof(buf) - (cp - buf), " {%s %u}",
411 ipaddr_string(&idp->ird_addr),
412 EXTRACT_32BITS(&idp->ird_pref));
413 cp = buf + strlen(buf);
414 ++idp;
415 }
416 }
417 break;
418
419 case ICMP_TIMXCEED:
420 TCHECK(dp->icmp_ip.ip_dst);
421 switch (dp->icmp_code) {
422
423 case ICMP_TIMXCEED_INTRANS:
424 str = "time exceeded in-transit";
425 break;
426
427 case ICMP_TIMXCEED_REASS:
428 str = "ip reassembly time exceeded";
429 break;
430
431 default:
432 (void)snprintf(buf, sizeof(buf), "time exceeded-#%d",
433 dp->icmp_code);
434 break;
435 }
436 break;
437
438 case ICMP_PARAMPROB:
439 if (dp->icmp_code)
440 (void)snprintf(buf, sizeof(buf),
441 "parameter problem - code %d", dp->icmp_code);
442 else {
443 TCHECK(dp->icmp_pptr);
444 (void)snprintf(buf, sizeof(buf),
445 "parameter problem - octet %d", dp->icmp_pptr);
446 }
447 break;
448
449 case ICMP_MASKREPLY:
450 TCHECK(dp->icmp_mask);
451 (void)snprintf(buf, sizeof(buf), "address mask is 0x%08x",
452 EXTRACT_32BITS(&dp->icmp_mask));
453 break;
454
455 case ICMP_TSTAMP:
456 TCHECK(dp->icmp_seq);
457 (void)snprintf(buf, sizeof(buf),
458 "time stamp query id %u seq %u",
459 EXTRACT_16BITS(&dp->icmp_id),
460 EXTRACT_16BITS(&dp->icmp_seq));
461 break;
462
463 case ICMP_TSTAMPREPLY:
464 TCHECK(dp->icmp_ttime);
465 (void)snprintf(buf, sizeof(buf),
466 "time stamp reply id %u seq %u : org 0x%x recv 0x%x xmit 0x%x",
467 EXTRACT_16BITS(&dp->icmp_id),
468 EXTRACT_16BITS(&dp->icmp_seq),
469 EXTRACT_32BITS(&dp->icmp_otime),
470 EXTRACT_32BITS(&dp->icmp_rtime),
471 EXTRACT_32BITS(&dp->icmp_ttime));
472 break;
473
474 default:
475 str = tok2str(icmp2str, "type-#%d", dp->icmp_type);
476 break;
477 }
478 (void)printf("icmp %d: %s", plen, str);
479 if (vflag && !fragmented) { /* don't attempt checksumming if this is a frag */
480 u_int16_t sum, icmp_sum;
481 if (TTEST2(*bp, plen)) {
482 sum = in_cksum((u_short*)dp, plen, 0);
483 if (sum != 0) {
484 icmp_sum = EXTRACT_16BITS(&dp->icmp_cksum);
485 (void)printf(" (wrong icmp cksum %x (->%x)!)",
486 icmp_sum,
487 in_cksum_shouldbe(icmp_sum, sum));
488 }
489 }
490 }
491 if (vflag > 1 && !ICMP_INFOTYPE(dp->icmp_type)) {
492 bp += 8;
493 (void)printf(" for ");
494 ip = (struct ip *)bp;
495 snaplen = snapend - bp;
496 ip_print(bp, EXTRACT_16BITS(&ip->ip_len));
497 }
498 return;
499 trunc:
500 fputs("[|icmp]", stdout);
501 }