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.44 2000-01-17 06:24:25 itojun Exp $ (LBL)";
31 #include <sys/param.h>
33 #include <sys/socket.h>
41 #include <netinet/in.h>
42 #include <netinet/if_ether.h>
43 #include <netinet/in_systm.h>
44 #include <netinet/ip.h>
45 #include <netinet/ip_icmp.h>
46 #include <netinet/ip_var.h>
47 #include <netinet/udp.h>
48 #include <netinet/udp_var.h>
49 #include <netinet/tcp.h>
54 #include "interface.h"
55 #include "addrtoname.h"
56 #include "extract.h" /* must come after interface.h */
59 #ifndef ICMP_UNREACH_NET_UNKNOWN
60 #define ICMP_UNREACH_NET_UNKNOWN 6 /* destination net unknown */
62 #ifndef ICMP_UNREACH_HOST_UNKNOWN
63 #define ICMP_UNREACH_HOST_UNKNOWN 7 /* destination host unknown */
65 #ifndef ICMP_UNREACH_ISOLATED
66 #define ICMP_UNREACH_ISOLATED 8 /* source host isolated */
68 #ifndef ICMP_UNREACH_NET_PROHIB
69 #define ICMP_UNREACH_NET_PROHIB 9 /* admin prohibited net */
71 #ifndef ICMP_UNREACH_HOST_PROHIB
72 #define ICMP_UNREACH_HOST_PROHIB 10 /* admin prohibited host */
74 #ifndef ICMP_UNREACH_TOSNET
75 #define ICMP_UNREACH_TOSNET 11 /* tos prohibited net */
77 #ifndef ICMP_UNREACH_TOSHOST
78 #define ICMP_UNREACH_TOSHOST 12 /* tos prohibited host */
82 #ifndef ICMP_UNREACH_FILTER_PROHIB
83 #define ICMP_UNREACH_FILTER_PROHIB 13 /* admin prohibited filter */
85 #ifndef ICMP_UNREACH_HOST_PRECEDENCE
86 #define ICMP_UNREACH_HOST_PRECEDENCE 14 /* host precedence violation */
88 #ifndef ICMP_UNREACH_PRECEDENCE_CUTOFF
89 #define ICMP_UNREACH_PRECEDENCE_CUTOFF 15 /* precedence cutoff */
93 #ifndef ICMP_ROUTERADVERT
94 #define ICMP_ROUTERADVERT 9 /* router advertisement */
96 #ifndef ICMP_ROUTERSOLICIT
97 #define ICMP_ROUTERSOLICIT 10 /* router solicitation */
100 /* Most of the icmp types */
101 static struct tok icmp2str
[] = {
102 { ICMP_ECHOREPLY
, "echo reply" },
103 { ICMP_SOURCEQUENCH
, "source quench" },
104 { ICMP_ECHO
, "echo request" },
105 { ICMP_ROUTERSOLICIT
, "router solicitation" },
106 { ICMP_TSTAMP
, "time stamp request" },
107 { ICMP_TSTAMPREPLY
, "time stamp reply" },
108 { ICMP_IREQ
, "information request" },
109 { ICMP_IREQREPLY
, "information reply" },
110 { ICMP_MASKREQ
, "address mask request" },
114 /* Formats for most of the ICMP_UNREACH codes */
115 static struct tok unreach2str
[] = {
116 { ICMP_UNREACH_NET
, "net %s unreachable" },
117 { ICMP_UNREACH_HOST
, "host %s unreachable" },
118 { ICMP_UNREACH_SRCFAIL
,
119 "%s unreachable - source route failed" },
120 { ICMP_UNREACH_NET_UNKNOWN
, "net %s unreachable - unknown" },
121 { ICMP_UNREACH_HOST_UNKNOWN
, "host %s unreachable - unknown" },
122 { ICMP_UNREACH_ISOLATED
,
123 "%s unreachable - source host isolated" },
124 { ICMP_UNREACH_NET_PROHIB
,
125 "net %s unreachable - admin prohibited" },
126 { ICMP_UNREACH_HOST_PROHIB
,
127 "host %s unreachable - admin prohibited" },
128 { ICMP_UNREACH_TOSNET
,
129 "net %s unreachable - tos prohibited" },
130 { ICMP_UNREACH_TOSHOST
,
131 "host %s unreachable - tos prohibited" },
132 { ICMP_UNREACH_FILTER_PROHIB
,
133 "host %s unreachable - admin prohibited filter" },
134 { ICMP_UNREACH_HOST_PRECEDENCE
,
135 "host %s unreachable - host precedence violation" },
136 { ICMP_UNREACH_PRECEDENCE_CUTOFF
,
137 "host %s unreachable - precedence cutoff" },
141 /* Formats for the ICMP_REDIRECT codes */
142 static struct tok type2str
[] = {
143 { ICMP_REDIRECT_NET
, "redirect %s to net %s" },
144 { ICMP_REDIRECT_HOST
, "redirect %s to host %s" },
145 { ICMP_REDIRECT_TOSNET
, "redirect-tos %s to net %s" },
146 { ICMP_REDIRECT_TOSHOST
, "redirect-tos %s to net %s" },
151 struct mtu_discovery
{
157 struct ih_rdiscovery
{
160 u_short ird_lifetime
;
163 struct id_rdiscovery
{
169 icmp_print(register const u_char
*bp
, u_int plen
, register const u_char
*bp2
)
172 register const struct icmp
*dp
;
173 register const struct ip
*ip
;
174 register const char *str
, *fmt
;
175 register const struct ip
*oip
;
176 register const struct udphdr
*ouh
;
177 register u_int hlen
, dport
, mtu
;
180 dp
= (struct icmp
*)bp
;
181 ip
= (struct ip
*)bp2
;
185 (void)printf("%s > %s: ",
186 ipaddr_string(&ip
->ip_src
),
187 ipaddr_string(&ip
->ip_dst
));
190 TCHECK(dp
->icmp_code
);
191 switch (dp
->icmp_type
) {
194 TCHECK(dp
->icmp_ip
.ip_dst
);
195 switch (dp
->icmp_code
) {
197 case ICMP_UNREACH_PROTOCOL
:
198 TCHECK(dp
->icmp_ip
.ip_p
);
199 (void)snprintf(buf
, sizeof(buf
),
200 "%s protocol %d unreachable",
201 ipaddr_string(&dp
->icmp_ip
.ip_dst
),
205 case ICMP_UNREACH_PORT
:
206 TCHECK(dp
->icmp_ip
.ip_p
);
208 hlen
= oip
->ip_hl
* 4;
209 ouh
= (struct udphdr
*)(((u_char
*)oip
) + hlen
);
210 dport
= ntohs(ouh
->uh_dport
);
214 (void)snprintf(buf
, sizeof(buf
),
215 "%s tcp port %s unreachable",
216 ipaddr_string(&oip
->ip_dst
),
217 tcpport_string(dport
));
221 (void)snprintf(buf
, sizeof(buf
),
222 "%s udp port %s unreachable",
223 ipaddr_string(&oip
->ip_dst
),
224 udpport_string(dport
));
228 (void)snprintf(buf
, sizeof(buf
),
229 "%s protocol %d port %d unreachable",
230 ipaddr_string(&oip
->ip_dst
),
236 case ICMP_UNREACH_NEEDFRAG
:
238 register const struct mtu_discovery
*mp
;
240 mp
= (struct mtu_discovery
*)&dp
->icmp_void
;
241 mtu
= EXTRACT_16BITS(&mp
->nexthopmtu
);
243 (void)snprintf(buf
, sizeof(buf
),
244 "%s unreachable - need to frag (mtu %d)",
245 ipaddr_string(&dp
->icmp_ip
.ip_dst
), mtu
);
247 (void)snprintf(buf
, sizeof(buf
),
248 "%s unreachable - need to frag",
249 ipaddr_string(&dp
->icmp_ip
.ip_dst
));
254 fmt
= tok2str(unreach2str
, "#%d %%s unreachable",
256 (void)snprintf(buf
, sizeof(buf
), fmt
,
257 ipaddr_string(&dp
->icmp_ip
.ip_dst
));
263 TCHECK(dp
->icmp_ip
.ip_dst
);
264 fmt
= tok2str(type2str
, "redirect-#%d %%s to net %%s",
266 (void)snprintf(buf
, sizeof(buf
), fmt
,
267 ipaddr_string(&dp
->icmp_ip
.ip_dst
),
268 ipaddr_string(&dp
->icmp_gwaddr
));
271 case ICMP_ROUTERADVERT
:
273 register const struct ih_rdiscovery
*ihp
;
274 register const struct id_rdiscovery
*idp
;
275 u_int lifetime
, num
, size
;
277 (void)strcpy(buf
, "router advertisement");
278 cp
= buf
+ strlen(buf
);
280 ihp
= (struct ih_rdiscovery
*)&dp
->icmp_void
;
282 (void)strncpy(cp
, " lifetime ", sizeof(buf
) - (cp
- buf
));
283 cp
= buf
+ strlen(buf
);
284 lifetime
= EXTRACT_16BITS(&ihp
->ird_lifetime
);
286 (void)snprintf(cp
, sizeof(buf
) - (cp
- buf
), "%u",
288 else if (lifetime
< 60 * 60)
289 (void)snprintf(cp
, sizeof(buf
) - (cp
- buf
), "%u:%02u",
290 lifetime
/ 60, lifetime
% 60);
292 (void)snprintf(cp
, sizeof(buf
) - (cp
- buf
),
295 (lifetime
% 3600) / 60,
297 cp
= buf
+ strlen(buf
);
299 num
= ihp
->ird_addrnum
;
300 (void)snprintf(cp
, sizeof(buf
) - (cp
- buf
), " %d:", num
);
301 cp
= buf
+ strlen(buf
);
303 size
= ihp
->ird_addrsiz
;
305 (void)snprintf(cp
, sizeof(buf
) - (cp
- buf
),
309 idp
= (struct id_rdiscovery
*)&dp
->icmp_data
;
312 (void)snprintf(cp
, sizeof(buf
) - (cp
- buf
), " {%s %u}",
313 ipaddr_string(&idp
->ird_addr
),
314 EXTRACT_32BITS(&idp
->ird_pref
));
315 cp
= buf
+ strlen(buf
);
321 TCHECK(dp
->icmp_ip
.ip_dst
);
322 switch (dp
->icmp_code
) {
324 case ICMP_TIMXCEED_INTRANS
:
325 str
= "time exceeded in-transit";
328 case ICMP_TIMXCEED_REASS
:
329 str
= "ip reassembly time exceeded";
333 (void)snprintf(buf
, sizeof(buf
), "time exceeded-#%d",
341 (void)snprintf(buf
, sizeof(buf
),
342 "parameter problem - code %d", dp
->icmp_code
);
344 TCHECK(dp
->icmp_pptr
);
345 (void)snprintf(buf
, sizeof(buf
),
346 "parameter problem - octet %d", dp
->icmp_pptr
);
351 TCHECK(dp
->icmp_mask
);
352 (void)snprintf(buf
, sizeof(buf
), "address mask is 0x%08x",
353 (u_int32_t
)ntohl(dp
->icmp_mask
));
357 str
= tok2str(icmp2str
, "type-#%d", dp
->icmp_type
);
360 (void)printf("icmp: %s", str
);
362 if (TTEST2(*bp
, plen
)) {
363 if (in_cksum((u_short
*)dp
, plen
, 0))
364 printf(" (wrong icmp csum)");
367 if (vflag
> 1 && !ICMP_INFOTYPE(dp
->icmp_type
)) {
369 (void)printf(" for ");
370 ip
= (struct ip
*)bp
;
371 snaplen
= snapend
- bp
;
372 ip_print(bp
, ntohs(ip
->ip_len
));
376 fputs("[|icmp]", stdout
);