2 * Copyright (c) 1988, 1989, 1990, 1991, 1993, 1994, 1995, 1996
3 * The Regents of the University of California. All rights reserved.
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
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.
23 static const char rcsid
[] =
24 "@(#) $Header: /tcpdump/master/tcpdump/print-icmp.c,v 1.64 2002-06-01 23:50:32 guy Exp $ (LBL)";
31 #include <sys/param.h>
33 #include <sys/socket.h>
35 #include <netinet/in.h>
39 #include <netdb.h> /* for MAXHOSTNAMELEN on some platforms */
41 #include "interface.h"
42 #include "addrtoname.h"
43 #include "extract.h" /* must come after interface.h */
49 * Interface Control Message Protocol Definitions.
50 * Per RFC 792, September 1981.
54 * Structure of an icmp header.
57 u_int8_t icmp_type
; /* type of message, see below */
58 u_int8_t icmp_code
; /* type sub code */
59 u_int16_t icmp_cksum
; /* ones complement cksum of struct */
61 u_int8_t ih_pptr
; /* ICMP_PARAMPROB */
62 struct in_addr ih_gwaddr
; /* ICMP_REDIRECT */
69 /* ICMP_UNREACH_NEEDFRAG -- Path MTU Discovery (RFC1191) */
72 u_int16_t ipm_nextmtu
;
75 #define icmp_pptr icmp_hun.ih_pptr
76 #define icmp_gwaddr icmp_hun.ih_gwaddr
77 #define icmp_id icmp_hun.ih_idseq.icd_id
78 #define icmp_seq icmp_hun.ih_idseq.icd_seq
79 #define icmp_void icmp_hun.ih_void
80 #define icmp_pmvoid icmp_hun.ih_pmtu.ipm_void
81 #define icmp_nextmtu icmp_hun.ih_pmtu.ipm_nextmtu
90 /* options and then 64 bits of data */
95 #define icmp_otime icmp_dun.id_ts.its_otime
96 #define icmp_rtime icmp_dun.id_ts.its_rtime
97 #define icmp_ttime icmp_dun.id_ts.its_ttime
98 #define icmp_ip icmp_dun.id_ip.idi_ip
99 #define icmp_mask icmp_dun.id_mask
100 #define icmp_data icmp_dun.id_data
104 * Lower bounds on packet lengths for various types.
105 * For the error advice packets must first insure that the
106 * packet is large enought to contain the returned ip header.
107 * Only then can we do the check to see if 64 bits of packet
108 * data have been returned, since we need to check the returned
111 #define ICMP_MINLEN 8 /* abs minimum */
112 #define ICMP_TSLEN (8 + 3 * sizeof (u_int32_t)) /* timestamp */
113 #define ICMP_MASKLEN 12 /* address mask */
114 #define ICMP_ADVLENMIN (8 + sizeof (struct ip) + 8) /* min */
115 #define ICMP_ADVLEN(p) (8 + (IP_HL(&(p)->icmp_ip) << 2) + 8)
116 /* N.B.: must separately check that ip_hl >= 5 */
119 * Definition of type and code field values.
121 #define ICMP_ECHOREPLY 0 /* echo reply */
122 #define ICMP_UNREACH 3 /* dest unreachable, codes: */
123 #define ICMP_UNREACH_NET 0 /* bad net */
124 #define ICMP_UNREACH_HOST 1 /* bad host */
125 #define ICMP_UNREACH_PROTOCOL 2 /* bad protocol */
126 #define ICMP_UNREACH_PORT 3 /* bad port */
127 #define ICMP_UNREACH_NEEDFRAG 4 /* IP_DF caused drop */
128 #define ICMP_UNREACH_SRCFAIL 5 /* src route failed */
129 #define ICMP_UNREACH_NET_UNKNOWN 6 /* unknown net */
130 #define ICMP_UNREACH_HOST_UNKNOWN 7 /* unknown host */
131 #define ICMP_UNREACH_ISOLATED 8 /* src host isolated */
132 #define ICMP_UNREACH_NET_PROHIB 9 /* prohibited access */
133 #define ICMP_UNREACH_HOST_PROHIB 10 /* ditto */
134 #define ICMP_UNREACH_TOSNET 11 /* bad tos for net */
135 #define ICMP_UNREACH_TOSHOST 12 /* bad tos for host */
136 #define ICMP_SOURCEQUENCH 4 /* packet lost, slow down */
137 #define ICMP_REDIRECT 5 /* shorter route, codes: */
138 #define ICMP_REDIRECT_NET 0 /* for network */
139 #define ICMP_REDIRECT_HOST 1 /* for host */
140 #define ICMP_REDIRECT_TOSNET 2 /* for tos and net */
141 #define ICMP_REDIRECT_TOSHOST 3 /* for tos and host */
142 #define ICMP_ECHO 8 /* echo service */
143 #define ICMP_ROUTERADVERT 9 /* router advertisement */
144 #define ICMP_ROUTERSOLICIT 10 /* router solicitation */
145 #define ICMP_TIMXCEED 11 /* time exceeded, code: */
146 #define ICMP_TIMXCEED_INTRANS 0 /* ttl==0 in transit */
147 #define ICMP_TIMXCEED_REASS 1 /* ttl==0 in reass */
148 #define ICMP_PARAMPROB 12 /* ip header bad */
149 #define ICMP_PARAMPROB_OPTABSENT 1 /* req. opt. absent */
150 #define ICMP_TSTAMP 13 /* timestamp request */
151 #define ICMP_TSTAMPREPLY 14 /* timestamp reply */
152 #define ICMP_IREQ 15 /* information request */
153 #define ICMP_IREQREPLY 16 /* information reply */
154 #define ICMP_MASKREQ 17 /* address mask request */
155 #define ICMP_MASKREPLY 18 /* address mask reply */
157 #define ICMP_MAXTYPE 18
159 #define ICMP_INFOTYPE(type) \
160 ((type) == ICMP_ECHOREPLY || (type) == ICMP_ECHO || \
161 (type) == ICMP_ROUTERADVERT || (type) == ICMP_ROUTERSOLICIT || \
162 (type) == ICMP_TSTAMP || (type) == ICMP_TSTAMPREPLY || \
163 (type) == ICMP_IREQ || (type) == ICMP_IREQREPLY || \
164 (type) == ICMP_MASKREQ || (type) == ICMP_MASKREPLY)
166 #ifndef ICMP_UNREACH_NET_UNKNOWN
167 #define ICMP_UNREACH_NET_UNKNOWN 6 /* destination net unknown */
169 #ifndef ICMP_UNREACH_HOST_UNKNOWN
170 #define ICMP_UNREACH_HOST_UNKNOWN 7 /* destination host unknown */
172 #ifndef ICMP_UNREACH_ISOLATED
173 #define ICMP_UNREACH_ISOLATED 8 /* source host isolated */
175 #ifndef ICMP_UNREACH_NET_PROHIB
176 #define ICMP_UNREACH_NET_PROHIB 9 /* admin prohibited net */
178 #ifndef ICMP_UNREACH_HOST_PROHIB
179 #define ICMP_UNREACH_HOST_PROHIB 10 /* admin prohibited host */
181 #ifndef ICMP_UNREACH_TOSNET
182 #define ICMP_UNREACH_TOSNET 11 /* tos prohibited net */
184 #ifndef ICMP_UNREACH_TOSHOST
185 #define ICMP_UNREACH_TOSHOST 12 /* tos prohibited host */
189 #ifndef ICMP_UNREACH_FILTER_PROHIB
190 #define ICMP_UNREACH_FILTER_PROHIB 13 /* admin prohibited filter */
192 #ifndef ICMP_UNREACH_HOST_PRECEDENCE
193 #define ICMP_UNREACH_HOST_PRECEDENCE 14 /* host precedence violation */
195 #ifndef ICMP_UNREACH_PRECEDENCE_CUTOFF
196 #define ICMP_UNREACH_PRECEDENCE_CUTOFF 15 /* precedence cutoff */
199 /* Most of the icmp types */
200 static struct tok icmp2str
[] = {
201 { ICMP_ECHOREPLY
, "echo reply" },
202 { ICMP_SOURCEQUENCH
, "source quench" },
203 { ICMP_ECHO
, "echo request" },
204 { ICMP_ROUTERSOLICIT
, "router solicitation" },
205 { ICMP_TSTAMP
, "time stamp request" },
206 { ICMP_TSTAMPREPLY
, "time stamp reply" },
207 { ICMP_IREQ
, "information request" },
208 { ICMP_IREQREPLY
, "information reply" },
209 { ICMP_MASKREQ
, "address mask request" },
213 /* Formats for most of the ICMP_UNREACH codes */
214 static struct tok unreach2str
[] = {
215 { ICMP_UNREACH_NET
, "net %s unreachable" },
216 { ICMP_UNREACH_HOST
, "host %s unreachable" },
217 { ICMP_UNREACH_SRCFAIL
,
218 "%s unreachable - source route failed" },
219 { ICMP_UNREACH_NET_UNKNOWN
, "net %s unreachable - unknown" },
220 { ICMP_UNREACH_HOST_UNKNOWN
, "host %s unreachable - unknown" },
221 { ICMP_UNREACH_ISOLATED
,
222 "%s unreachable - source host isolated" },
223 { ICMP_UNREACH_NET_PROHIB
,
224 "net %s unreachable - admin prohibited" },
225 { ICMP_UNREACH_HOST_PROHIB
,
226 "host %s unreachable - admin prohibited" },
227 { ICMP_UNREACH_TOSNET
,
228 "net %s unreachable - tos prohibited" },
229 { ICMP_UNREACH_TOSHOST
,
230 "host %s unreachable - tos prohibited" },
231 { ICMP_UNREACH_FILTER_PROHIB
,
232 "host %s unreachable - admin prohibited filter" },
233 { ICMP_UNREACH_HOST_PRECEDENCE
,
234 "host %s unreachable - host precedence violation" },
235 { ICMP_UNREACH_PRECEDENCE_CUTOFF
,
236 "host %s unreachable - precedence cutoff" },
240 /* Formats for the ICMP_REDIRECT codes */
241 static struct tok type2str
[] = {
242 { ICMP_REDIRECT_NET
, "redirect %s to net %s" },
243 { ICMP_REDIRECT_HOST
, "redirect %s to host %s" },
244 { ICMP_REDIRECT_TOSNET
, "redirect-tos %s to net %s" },
245 { ICMP_REDIRECT_TOSHOST
, "redirect-tos %s to host %s" },
250 struct mtu_discovery
{
252 u_int16_t nexthopmtu
;
256 struct ih_rdiscovery
{
257 u_int8_t ird_addrnum
;
258 u_int8_t ird_addrsiz
;
259 u_int16_t ird_lifetime
;
262 struct id_rdiscovery
{
268 icmp_print(const u_char
*bp
, u_int plen
, const u_char
*bp2
)
271 const struct icmp
*dp
;
273 const char *str
, *fmt
;
274 const struct ip
*oip
;
275 const struct udphdr
*ouh
;
276 u_int hlen
, dport
, mtu
;
277 char buf
[MAXHOSTNAMELEN
+ 100];
279 dp
= (struct icmp
*)bp
;
280 ip
= (struct ip
*)bp2
;
283 TCHECK(dp
->icmp_code
);
284 switch (dp
->icmp_type
) {
287 TCHECK(dp
->icmp_seq
);
288 (void)snprintf(buf
, sizeof(buf
), "echo request seq %u",
289 (unsigned)ntohs(dp
->icmp_seq
));
293 TCHECK(dp
->icmp_seq
);
294 (void)snprintf(buf
, sizeof(buf
), "echo reply seq %u",
295 (unsigned)ntohs(dp
->icmp_seq
));
299 TCHECK(dp
->icmp_ip
.ip_dst
);
300 switch (dp
->icmp_code
) {
302 case ICMP_UNREACH_PROTOCOL
:
303 TCHECK(dp
->icmp_ip
.ip_p
);
304 (void)snprintf(buf
, sizeof(buf
),
305 "%s protocol %d unreachable",
306 ipaddr_string(&dp
->icmp_ip
.ip_dst
),
310 case ICMP_UNREACH_PORT
:
311 TCHECK(dp
->icmp_ip
.ip_p
);
313 hlen
= IP_HL(oip
) * 4;
314 ouh
= (struct udphdr
*)(((u_char
*)oip
) + hlen
);
315 dport
= ntohs(ouh
->uh_dport
);
319 (void)snprintf(buf
, sizeof(buf
),
320 "%s tcp port %s unreachable",
321 ipaddr_string(&oip
->ip_dst
),
322 tcpport_string(dport
));
326 (void)snprintf(buf
, sizeof(buf
),
327 "%s udp port %s unreachable",
328 ipaddr_string(&oip
->ip_dst
),
329 udpport_string(dport
));
333 (void)snprintf(buf
, sizeof(buf
),
334 "%s protocol %d port %d unreachable",
335 ipaddr_string(&oip
->ip_dst
),
341 case ICMP_UNREACH_NEEDFRAG
:
343 register const struct mtu_discovery
*mp
;
344 mp
= (struct mtu_discovery
*)&dp
->icmp_void
;
345 mtu
= EXTRACT_16BITS(&mp
->nexthopmtu
);
347 (void)snprintf(buf
, sizeof(buf
),
348 "%s unreachable - need to frag (mtu %d)",
349 ipaddr_string(&dp
->icmp_ip
.ip_dst
), mtu
);
351 (void)snprintf(buf
, sizeof(buf
),
352 "%s unreachable - need to frag",
353 ipaddr_string(&dp
->icmp_ip
.ip_dst
));
359 fmt
= tok2str(unreach2str
, "#%d %%s unreachable",
361 (void)snprintf(buf
, sizeof(buf
), fmt
,
362 ipaddr_string(&dp
->icmp_ip
.ip_dst
));
368 TCHECK(dp
->icmp_ip
.ip_dst
);
369 fmt
= tok2str(type2str
, "redirect-#%d %%s to net %%s",
371 (void)snprintf(buf
, sizeof(buf
), fmt
,
372 ipaddr_string(&dp
->icmp_ip
.ip_dst
),
373 ipaddr_string(&dp
->icmp_gwaddr
));
376 case ICMP_ROUTERADVERT
:
378 register const struct ih_rdiscovery
*ihp
;
379 register const struct id_rdiscovery
*idp
;
380 u_int lifetime
, num
, size
;
382 (void)snprintf(buf
, sizeof(buf
), "router advertisement");
383 cp
= buf
+ strlen(buf
);
385 ihp
= (struct ih_rdiscovery
*)&dp
->icmp_void
;
387 (void)strncpy(cp
, " lifetime ", sizeof(buf
) - (cp
- buf
));
388 cp
= buf
+ strlen(buf
);
389 lifetime
= EXTRACT_16BITS(&ihp
->ird_lifetime
);
391 (void)snprintf(cp
, sizeof(buf
) - (cp
- buf
), "%u",
393 } else if (lifetime
< 60 * 60) {
394 (void)snprintf(cp
, sizeof(buf
) - (cp
- buf
), "%u:%02u",
395 lifetime
/ 60, lifetime
% 60);
397 (void)snprintf(cp
, sizeof(buf
) - (cp
- buf
),
400 (lifetime
% 3600) / 60,
403 cp
= buf
+ strlen(buf
);
405 num
= ihp
->ird_addrnum
;
406 (void)snprintf(cp
, sizeof(buf
) - (cp
- buf
), " %d:", num
);
407 cp
= buf
+ strlen(buf
);
409 size
= ihp
->ird_addrsiz
;
411 (void)snprintf(cp
, sizeof(buf
) - (cp
- buf
),
415 idp
= (struct id_rdiscovery
*)&dp
->icmp_data
;
418 (void)snprintf(cp
, sizeof(buf
) - (cp
- buf
), " {%s %u}",
419 ipaddr_string(&idp
->ird_addr
),
420 EXTRACT_32BITS(&idp
->ird_pref
));
421 cp
= buf
+ strlen(buf
);
428 TCHECK(dp
->icmp_ip
.ip_dst
);
429 switch (dp
->icmp_code
) {
431 case ICMP_TIMXCEED_INTRANS
:
432 str
= "time exceeded in-transit";
435 case ICMP_TIMXCEED_REASS
:
436 str
= "ip reassembly time exceeded";
440 (void)snprintf(buf
, sizeof(buf
), "time exceeded-#%d",
448 (void)snprintf(buf
, sizeof(buf
),
449 "parameter problem - code %d", dp
->icmp_code
);
451 TCHECK(dp
->icmp_pptr
);
452 (void)snprintf(buf
, sizeof(buf
),
453 "parameter problem - octet %d", dp
->icmp_pptr
);
458 TCHECK(dp
->icmp_mask
);
459 (void)snprintf(buf
, sizeof(buf
), "address mask is 0x%08x",
460 (unsigned)ntohl(dp
->icmp_mask
));
464 TCHECK(dp
->icmp_seq
);
465 (void)snprintf(buf
, sizeof(buf
),
466 "time stamp query id %u seq %u",
467 (unsigned)ntohs(dp
->icmp_id
),
468 (unsigned)ntohs(dp
->icmp_seq
));
471 case ICMP_TSTAMPREPLY
:
472 TCHECK(dp
->icmp_ttime
);
473 (void)snprintf(buf
, sizeof(buf
),
474 "time stamp reply id %u seq %u : org 0x%lx recv 0x%lx xmit 0x%lx",
475 (unsigned)ntohs(dp
->icmp_id
),
476 (unsigned)ntohs(dp
->icmp_seq
),
477 (unsigned long)ntohl(dp
->icmp_otime
),
478 (unsigned long)ntohl(dp
->icmp_rtime
),
479 (unsigned long)ntohl(dp
->icmp_ttime
));
483 str
= tok2str(icmp2str
, "type-#%d", dp
->icmp_type
);
486 (void)printf("icmp: %s", str
);
488 if (TTEST2(*bp
, plen
)) {
489 if (in_cksum((u_short
*)dp
, plen
, 0))
490 printf(" (wrong icmp csum)");
493 if (vflag
> 1 && !ICMP_INFOTYPE(dp
->icmp_type
)) {
495 (void)printf(" for ");
496 ip
= (struct ip
*)bp
;
497 snaplen
= snapend
- bp
;
498 ip_print(bp
, ntohs(ip
->ip_len
));
502 fputs("[|icmp]", stdout
);