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.41 1999-11-21 03:46:38 assar Exp $ (LBL)";
27 #include <sys/param.h>
29 #include <sys/socket.h>
37 #include <netinet/in.h>
38 #include <netinet/if_ether.h>
39 #include <netinet/in_systm.h>
40 #include <netinet/ip.h>
41 #include <netinet/ip_icmp.h>
42 #include <netinet/ip_var.h>
43 #include <netinet/udp.h>
44 #include <netinet/udp_var.h>
45 #include <netinet/tcp.h>
50 #include "interface.h"
51 #include "addrtoname.h"
52 #include "extract.h" /* must come after interface.h */
55 #ifndef ICMP_UNREACH_NET_UNKNOWN
56 #define ICMP_UNREACH_NET_UNKNOWN 6 /* destination net unknown */
58 #ifndef ICMP_UNREACH_HOST_UNKNOWN
59 #define ICMP_UNREACH_HOST_UNKNOWN 7 /* destination host unknown */
61 #ifndef ICMP_UNREACH_ISOLATED
62 #define ICMP_UNREACH_ISOLATED 8 /* source host isolated */
64 #ifndef ICMP_UNREACH_NET_PROHIB
65 #define ICMP_UNREACH_NET_PROHIB 9 /* admin prohibited net */
67 #ifndef ICMP_UNREACH_HOST_PROHIB
68 #define ICMP_UNREACH_HOST_PROHIB 10 /* admin prohibited host */
70 #ifndef ICMP_UNREACH_TOSNET
71 #define ICMP_UNREACH_TOSNET 11 /* tos prohibited net */
73 #ifndef ICMP_UNREACH_TOSHOST
74 #define ICMP_UNREACH_TOSHOST 12 /* tos prohibited host */
78 #ifndef ICMP_UNREACH_FILTER_PROHIB
79 #define ICMP_UNREACH_FILTER_PROHIB 13 /* admin prohibited filter */
81 #ifndef ICMP_UNREACH_HOST_PRECEDENCE
82 #define ICMP_UNREACH_HOST_PRECEDENCE 14 /* host precedence violation */
84 #ifndef ICMP_UNREACH_PRECEDENCE_CUTOFF
85 #define ICMP_UNREACH_PRECEDENCE_CUTOFF 15 /* precedence cutoff */
89 #ifndef ICMP_ROUTERADVERT
90 #define ICMP_ROUTERADVERT 9 /* router advertisement */
92 #ifndef ICMP_ROUTERSOLICIT
93 #define ICMP_ROUTERSOLICIT 10 /* router solicitation */
96 /* Most of the icmp types */
97 static struct tok icmp2str
[] = {
98 { ICMP_ECHOREPLY
, "echo reply" },
99 { ICMP_SOURCEQUENCH
, "source quench" },
100 { ICMP_ECHO
, "echo request" },
101 { ICMP_ROUTERSOLICIT
, "router solicitation" },
102 { ICMP_TSTAMP
, "time stamp request" },
103 { ICMP_TSTAMPREPLY
, "time stamp reply" },
104 { ICMP_IREQ
, "information request" },
105 { ICMP_IREQREPLY
, "information reply" },
106 { ICMP_MASKREQ
, "address mask request" },
110 /* Formats for most of the ICMP_UNREACH codes */
111 static struct tok unreach2str
[] = {
112 { ICMP_UNREACH_NET
, "net %s unreachable" },
113 { ICMP_UNREACH_HOST
, "host %s unreachable" },
114 { ICMP_UNREACH_SRCFAIL
,
115 "%s unreachable - source route failed" },
116 { ICMP_UNREACH_NET_UNKNOWN
, "net %s unreachable - unknown" },
117 { ICMP_UNREACH_HOST_UNKNOWN
, "host %s unreachable - unknown" },
118 { ICMP_UNREACH_ISOLATED
,
119 "%s unreachable - source host isolated" },
120 { ICMP_UNREACH_NET_PROHIB
,
121 "net %s unreachable - admin prohibited" },
122 { ICMP_UNREACH_HOST_PROHIB
,
123 "host %s unreachable - admin prohibited" },
124 { ICMP_UNREACH_TOSNET
,
125 "net %s unreachable - tos prohibited" },
126 { ICMP_UNREACH_TOSHOST
,
127 "host %s unreachable - tos prohibited" },
128 { ICMP_UNREACH_FILTER_PROHIB
,
129 "host %s unreachable - admin prohibited filter" },
130 { ICMP_UNREACH_HOST_PRECEDENCE
,
131 "host %s unreachable - host precedence violation" },
132 { ICMP_UNREACH_PRECEDENCE_CUTOFF
,
133 "host %s unreachable - precedence cutoff" },
137 /* Formats for the ICMP_REDIRECT codes */
138 static struct tok type2str
[] = {
139 { ICMP_REDIRECT_NET
, "redirect %s to net %s" },
140 { ICMP_REDIRECT_HOST
, "redirect %s to host %s" },
141 { ICMP_REDIRECT_TOSNET
, "redirect-tos %s to net %s" },
142 { ICMP_REDIRECT_TOSHOST
, "redirect-tos %s to net %s" },
147 struct mtu_discovery
{
153 struct ih_rdiscovery
{
156 u_short ird_lifetime
;
159 struct id_rdiscovery
{
165 icmp_print(register const u_char
*bp
, u_int plen
, register const u_char
*bp2
)
168 register const struct icmp
*dp
;
169 register const struct ip
*ip
;
170 register const char *str
, *fmt
;
171 register const struct ip
*oip
;
172 register const struct udphdr
*ouh
;
173 register u_int hlen
, dport
, mtu
;
176 dp
= (struct icmp
*)bp
;
177 ip
= (struct ip
*)bp2
;
181 (void)printf("%s > %s: ",
182 ipaddr_string(&ip
->ip_src
),
183 ipaddr_string(&ip
->ip_dst
));
186 TCHECK(dp
->icmp_code
);
187 switch (dp
->icmp_type
) {
190 TCHECK(dp
->icmp_ip
.ip_dst
);
191 switch (dp
->icmp_code
) {
193 case ICMP_UNREACH_PROTOCOL
:
194 TCHECK(dp
->icmp_ip
.ip_p
);
195 (void)sprintf(buf
, "%s protocol %d unreachable",
196 ipaddr_string(&dp
->icmp_ip
.ip_dst
),
200 case ICMP_UNREACH_PORT
:
201 TCHECK(dp
->icmp_ip
.ip_p
);
203 hlen
= oip
->ip_hl
* 4;
204 ouh
= (struct udphdr
*)(((u_char
*)oip
) + hlen
);
205 dport
= ntohs(ouh
->uh_dport
);
210 "%s tcp port %s unreachable",
211 ipaddr_string(&oip
->ip_dst
),
212 tcpport_string(dport
));
217 "%s udp port %s unreachable",
218 ipaddr_string(&oip
->ip_dst
),
219 udpport_string(dport
));
224 "%s protocol %d port %d unreachable",
225 ipaddr_string(&oip
->ip_dst
),
231 case ICMP_UNREACH_NEEDFRAG
:
233 register const struct mtu_discovery
*mp
;
235 mp
= (struct mtu_discovery
*)&dp
->icmp_void
;
236 mtu
= EXTRACT_16BITS(&mp
->nexthopmtu
);
239 "%s unreachable - need to frag (mtu %d)",
240 ipaddr_string(&dp
->icmp_ip
.ip_dst
), mtu
);
243 "%s unreachable - need to frag",
244 ipaddr_string(&dp
->icmp_ip
.ip_dst
));
249 fmt
= tok2str(unreach2str
, "#%d %%s unreachable",
251 (void)sprintf(buf
, fmt
,
252 ipaddr_string(&dp
->icmp_ip
.ip_dst
));
258 TCHECK(dp
->icmp_ip
.ip_dst
);
259 fmt
= tok2str(type2str
, "redirect-#%d %%s to net %%s",
261 (void)sprintf(buf
, fmt
,
262 ipaddr_string(&dp
->icmp_ip
.ip_dst
),
263 ipaddr_string(&dp
->icmp_gwaddr
));
266 case ICMP_ROUTERADVERT
:
268 register const struct ih_rdiscovery
*ihp
;
269 register const struct id_rdiscovery
*idp
;
270 u_int lifetime
, num
, size
;
272 (void)strcpy(buf
, "router advertisement");
273 cp
= buf
+ strlen(buf
);
275 ihp
= (struct ih_rdiscovery
*)&dp
->icmp_void
;
277 (void)strcpy(cp
, " lifetime ");
278 cp
= buf
+ strlen(buf
);
279 lifetime
= EXTRACT_16BITS(&ihp
->ird_lifetime
);
281 (void)sprintf(cp
, "%u", lifetime
);
282 else if (lifetime
< 60 * 60)
283 (void)sprintf(cp
, "%u:%02u",
284 lifetime
/ 60, lifetime
% 60);
286 (void)sprintf(cp
, "%u:%02u:%02u",
288 (lifetime
% 3600) / 60,
290 cp
= buf
+ strlen(buf
);
292 num
= ihp
->ird_addrnum
;
293 (void)sprintf(cp
, " %d:", num
);
294 cp
= buf
+ strlen(buf
);
296 size
= ihp
->ird_addrsiz
;
298 (void)sprintf(cp
, " [size %d]", size
);
301 idp
= (struct id_rdiscovery
*)&dp
->icmp_data
;
304 (void)sprintf(cp
, " {%s %u}",
305 ipaddr_string(&idp
->ird_addr
),
306 EXTRACT_32BITS(&idp
->ird_pref
));
307 cp
= buf
+ strlen(buf
);
313 TCHECK(dp
->icmp_ip
.ip_dst
);
314 switch (dp
->icmp_code
) {
316 case ICMP_TIMXCEED_INTRANS
:
317 str
= "time exceeded in-transit";
320 case ICMP_TIMXCEED_REASS
:
321 str
= "ip reassembly time exceeded";
325 (void)sprintf(buf
, "time exceeded-#%d", dp
->icmp_code
);
332 (void)sprintf(buf
, "parameter problem - code %d",
335 TCHECK(dp
->icmp_pptr
);
336 (void)sprintf(buf
, "parameter problem - octet %d",
342 TCHECK(dp
->icmp_mask
);
343 (void)sprintf(buf
, "address mask is 0x%08x",
344 (u_int32_t
)ntohl(dp
->icmp_mask
));
348 str
= tok2str(icmp2str
, "type-#%d", dp
->icmp_type
);
351 (void)printf("icmp: %s", str
);
353 if (((u_char
*)bp
) + plen
<= snapend
) {
354 if (in_cksum((u_short
*)dp
, plen
, 0))
355 printf(" (wrong icmp csum)");
360 fputs("[|icmp]", stdout
);