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.38 1999-10-07 23:47:11 mcr 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>
46 #include <netinet/tcpip.h>
51 #include "interface.h"
52 #include "addrtoname.h"
53 #include "extract.h" /* must come after interface.h */
56 #ifndef ICMP_UNREACH_NET_UNKNOWN
57 #define ICMP_UNREACH_NET_UNKNOWN 6 /* destination net unknown */
59 #ifndef ICMP_UNREACH_HOST_UNKNOWN
60 #define ICMP_UNREACH_HOST_UNKNOWN 7 /* destination host unknown */
62 #ifndef ICMP_UNREACH_ISOLATED
63 #define ICMP_UNREACH_ISOLATED 8 /* source host isolated */
65 #ifndef ICMP_UNREACH_NET_PROHIB
66 #define ICMP_UNREACH_NET_PROHIB 9 /* admin prohibited net */
68 #ifndef ICMP_UNREACH_HOST_PROHIB
69 #define ICMP_UNREACH_HOST_PROHIB 10 /* admin prohibited host */
71 #ifndef ICMP_UNREACH_TOSNET
72 #define ICMP_UNREACH_TOSNET 11 /* tos prohibited net */
74 #ifndef ICMP_UNREACH_TOSHOST
75 #define ICMP_UNREACH_TOSHOST 12 /* tos prohibited host */
79 #ifndef ICMP_UNREACH_FILTER_PROHIB
80 #define ICMP_UNREACH_FILTER_PROHIB 13 /* admin prohibited filter */
82 #ifndef ICMP_UNREACH_HOST_PRECEDENCE
83 #define ICMP_UNREACH_HOST_PRECEDENCE 14 /* host precedence violation */
85 #ifndef ICMP_UNREACH_PRECEDENCE_CUTOFF
86 #define ICMP_UNREACH_PRECEDENCE_CUTOFF 15 /* precedence cutoff */
90 #ifndef ICMP_ROUTERADVERT
91 #define ICMP_ROUTERADVERT 9 /* router advertisement */
93 #ifndef ICMP_ROUTERSOLICIT
94 #define ICMP_ROUTERSOLICIT 10 /* router solicitation */
97 /* Most of the icmp types */
98 static struct tok icmp2str
[] = {
99 { ICMP_ECHOREPLY
, "echo reply" },
100 { ICMP_SOURCEQUENCH
, "source quench" },
101 { ICMP_ECHO
, "echo request" },
102 { ICMP_ROUTERSOLICIT
, "router solicitation" },
103 { ICMP_TSTAMP
, "time stamp request" },
104 { ICMP_TSTAMPREPLY
, "time stamp reply" },
105 { ICMP_IREQ
, "information request" },
106 { ICMP_IREQREPLY
, "information reply" },
107 { ICMP_MASKREQ
, "address mask request" },
111 /* Formats for most of the ICMP_UNREACH codes */
112 static struct tok unreach2str
[] = {
113 { ICMP_UNREACH_NET
, "net %s unreachable" },
114 { ICMP_UNREACH_HOST
, "host %s unreachable" },
115 { ICMP_UNREACH_SRCFAIL
,
116 "%s unreachable - source route failed" },
117 { ICMP_UNREACH_NET_UNKNOWN
, "net %s unreachable - unknown" },
118 { ICMP_UNREACH_HOST_UNKNOWN
, "host %s unreachable - unknown" },
119 { ICMP_UNREACH_ISOLATED
,
120 "%s unreachable - source host isolated" },
121 { ICMP_UNREACH_NET_PROHIB
,
122 "net %s unreachable - admin prohibited" },
123 { ICMP_UNREACH_HOST_PROHIB
,
124 "host %s unreachable - admin prohibited" },
125 { ICMP_UNREACH_TOSNET
,
126 "net %s unreachable - tos prohibited" },
127 { ICMP_UNREACH_TOSHOST
,
128 "host %s unreachable - tos prohibited" },
129 { ICMP_UNREACH_FILTER_PROHIB
,
130 "host %s unreachable - admin prohibited filter" },
131 { ICMP_UNREACH_HOST_PRECEDENCE
,
132 "host %s unreachable - host precedence violation" },
133 { ICMP_UNREACH_PRECEDENCE_CUTOFF
,
134 "host %s unreachable - precedence cutoff" },
138 /* Formats for the ICMP_REDIRECT codes */
139 static struct tok type2str
[] = {
140 { ICMP_REDIRECT_NET
, "redirect %s to net %s" },
141 { ICMP_REDIRECT_HOST
, "redirect %s to host %s" },
142 { ICMP_REDIRECT_TOSNET
, "redirect-tos %s to net %s" },
143 { ICMP_REDIRECT_TOSHOST
, "redirect-tos %s to net %s" },
148 struct mtu_discovery
{
154 struct ih_rdiscovery
{
157 u_short ird_lifetime
;
160 struct id_rdiscovery
{
166 icmp_print(register const u_char
*bp
, register const u_char
*bp2
)
169 register const struct icmp
*dp
;
170 register const struct ip
*ip
;
171 register const char *str
, *fmt
;
172 register const struct ip
*oip
;
173 register const struct udphdr
*ouh
;
174 register u_int hlen
, dport
, mtu
;
177 dp
= (struct icmp
*)bp
;
178 ip
= (struct ip
*)bp2
;
181 (void)printf("%s > %s: ",
182 ipaddr_string(&ip
->ip_src
),
183 ipaddr_string(&ip
->ip_dst
));
185 TCHECK(dp
->icmp_code
);
186 switch (dp
->icmp_type
) {
189 TCHECK(dp
->icmp_ip
.ip_dst
);
190 switch (dp
->icmp_code
) {
192 case ICMP_UNREACH_PROTOCOL
:
193 TCHECK(dp
->icmp_ip
.ip_p
);
194 (void)sprintf(buf
, "%s protocol %d unreachable",
195 ipaddr_string(&dp
->icmp_ip
.ip_dst
),
199 case ICMP_UNREACH_PORT
:
200 TCHECK(dp
->icmp_ip
.ip_p
);
202 hlen
= oip
->ip_hl
* 4;
203 ouh
= (struct udphdr
*)(((u_char
*)oip
) + hlen
);
204 dport
= ntohs(ouh
->uh_dport
);
209 "%s tcp port %s unreachable",
210 ipaddr_string(&oip
->ip_dst
),
211 tcpport_string(dport
));
216 "%s udp port %s unreachable",
217 ipaddr_string(&oip
->ip_dst
),
218 udpport_string(dport
));
223 "%s protocol %d port %d unreachable",
224 ipaddr_string(&oip
->ip_dst
),
230 case ICMP_UNREACH_NEEDFRAG
:
232 register const struct mtu_discovery
*mp
;
234 mp
= (struct mtu_discovery
*)&dp
->icmp_void
;
235 mtu
= EXTRACT_16BITS(&mp
->nexthopmtu
);
238 "%s unreachable - need to frag (mtu %d)",
239 ipaddr_string(&dp
->icmp_ip
.ip_dst
), mtu
);
242 "%s unreachable - need to frag",
243 ipaddr_string(&dp
->icmp_ip
.ip_dst
));
248 fmt
= tok2str(unreach2str
, "#%d %%s unreachable",
250 (void)sprintf(buf
, fmt
,
251 ipaddr_string(&dp
->icmp_ip
.ip_dst
));
257 TCHECK(dp
->icmp_ip
.ip_dst
);
258 fmt
= tok2str(type2str
, "redirect-#%d %%s to net %%s",
260 (void)sprintf(buf
, fmt
,
261 ipaddr_string(&dp
->icmp_ip
.ip_dst
),
262 ipaddr_string(&dp
->icmp_gwaddr
));
265 case ICMP_ROUTERADVERT
:
267 register const struct ih_rdiscovery
*ihp
;
268 register const struct id_rdiscovery
*idp
;
269 u_int lifetime
, num
, size
;
271 (void)strcpy(buf
, "router advertisement");
272 cp
= buf
+ strlen(buf
);
274 ihp
= (struct ih_rdiscovery
*)&dp
->icmp_void
;
276 (void)strcpy(cp
, " lifetime ");
277 cp
= buf
+ strlen(buf
);
278 lifetime
= EXTRACT_16BITS(&ihp
->ird_lifetime
);
280 (void)sprintf(cp
, "%u", lifetime
);
281 else if (lifetime
< 60 * 60)
282 (void)sprintf(cp
, "%u:%02u",
283 lifetime
/ 60, lifetime
% 60);
285 (void)sprintf(cp
, "%u:%02u:%02u",
287 (lifetime
% 3600) / 60,
289 cp
= buf
+ strlen(buf
);
291 num
= ihp
->ird_addrnum
;
292 (void)sprintf(cp
, " %d:", num
);
293 cp
= buf
+ strlen(buf
);
295 size
= ihp
->ird_addrsiz
;
297 (void)sprintf(cp
, " [size %d]", size
);
300 idp
= (struct id_rdiscovery
*)&dp
->icmp_data
;
303 (void)sprintf(cp
, " {%s %u}",
304 ipaddr_string(&idp
->ird_addr
),
305 EXTRACT_32BITS(&idp
->ird_pref
));
306 cp
= buf
+ strlen(buf
);
312 TCHECK(dp
->icmp_ip
.ip_dst
);
313 switch (dp
->icmp_code
) {
315 case ICMP_TIMXCEED_INTRANS
:
316 str
= "time exceeded in-transit";
319 case ICMP_TIMXCEED_REASS
:
320 str
= "ip reassembly time exceeded";
324 (void)sprintf(buf
, "time exceeded-#%d", dp
->icmp_code
);
331 (void)sprintf(buf
, "parameter problem - code %d",
334 TCHECK(dp
->icmp_pptr
);
335 (void)sprintf(buf
, "parameter problem - octet %d",
341 TCHECK(dp
->icmp_mask
);
342 (void)sprintf(buf
, "address mask is 0x%08x",
343 (u_int32_t
)ntohl(dp
->icmp_mask
));
347 str
= tok2str(icmp2str
, "type-#%d", dp
->icmp_type
);
350 (void)printf("icmp: %s", str
);
353 fputs("[|icmp]", stdout
);