2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * "Portions Copyright (c) 1999 Apple Computer, Inc. All Rights
7 * Reserved. This file contains Original Code and/or Modifications of
8 * Original Code as defined in and that are subject to the Apple Public
9 * Source License Version 1.0 (the 'License'). You may not use this file
10 * except in compliance with the License. Please obtain a copy of the
11 * License at http://www.apple.com/publicsource and read it before using
14 * The Original Code and all software distributed under the License are
15 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
16 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
17 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
19 * License for the specific language governing rights and limitations
22 * @APPLE_LICENSE_HEADER_END@
25 * Copyright (c) 1988, 1989, 1990, 1991, 1993, 1994, 1995, 1996
26 * The Regents of the University of California. All rights reserved.
28 * Redistribution and use in source and binary forms, with or without
29 * modification, are permitted provided that: (1) source code distributions
30 * retain the above copyright notice and this paragraph in its entirety, (2)
31 * distributions including binary code include the above copyright notice and
32 * this paragraph in its entirety in the documentation or other materials
33 * provided with the distribution, and (3) all advertising materials mentioning
34 * features or use of this software display the following acknowledgement:
35 * ``This product includes software developed by the University of California,
36 * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
37 * the University nor the names of its contributors may be used to endorse
38 * or promote products derived from this software without specific prior
40 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
41 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
42 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
46 static const char rcsid
[] =
47 "@(#) $Header: /cvs/Darwin/Commands/NeXT/network_cmds/tcpdump.tproj/print-icmp.c,v 1.1.1.1 1999/05/02 03:58:33 wsanchez Exp $ (LBL)";
50 #include <sys/param.h>
52 #include <sys/socket.h>
60 #include <netinet/in.h>
61 #include <netinet/if_ether.h>
62 #include <netinet/in_systm.h>
63 #include <netinet/ip.h>
64 #include <netinet/ip_icmp.h>
65 #include <netinet/ip_var.h>
66 #include <netinet/udp.h>
67 #include <netinet/udp_var.h>
68 #include <netinet/tcp.h>
69 #include <netinet/tcpip.h>
74 #include "interface.h"
75 #include "addrtoname.h"
76 #include "extract.h" /* must come after interface.h */
79 #ifndef ICMP_UNREACH_NET_UNKNOWN
80 #define ICMP_UNREACH_NET_UNKNOWN 6 /* destination net unknown */
82 #ifndef ICMP_UNREACH_HOST_UNKNOWN
83 #define ICMP_UNREACH_HOST_UNKNOWN 7 /* destination host unknown */
85 #ifndef ICMP_UNREACH_ISOLATED
86 #define ICMP_UNREACH_ISOLATED 8 /* source host isolated */
88 #ifndef ICMP_UNREACH_NET_PROHIB
89 #define ICMP_UNREACH_NET_PROHIB 9 /* admin prohibited net */
91 #ifndef ICMP_UNREACH_HOST_PROHIB
92 #define ICMP_UNREACH_HOST_PROHIB 10 /* admin prohibited host */
94 #ifndef ICMP_UNREACH_TOSNET
95 #define ICMP_UNREACH_TOSNET 11 /* tos prohibited net */
97 #ifndef ICMP_UNREACH_TOSHOST
98 #define ICMP_UNREACH_TOSHOST 12 /* tos prohibited host */
102 #ifndef ICMP_UNREACH_FILTER_PROHIB
103 #define ICMP_UNREACH_FILTER_PROHIB 13 /* admin prohibited filter */
105 #ifndef ICMP_UNREACH_HOST_PRECEDENCE
106 #define ICMP_UNREACH_HOST_PRECEDENCE 14 /* host precedence violation */
108 #ifndef ICMP_UNREACH_PRECEDENCE_CUTOFF
109 #define ICMP_UNREACH_PRECEDENCE_CUTOFF 15 /* precedence cutoff */
113 #ifndef ICMP_ROUTERADVERT
114 #define ICMP_ROUTERADVERT 9 /* router advertisement */
116 #ifndef ICMP_ROUTERSOLICIT
117 #define ICMP_ROUTERSOLICIT 10 /* router solicitation */
120 /* Most of the icmp types */
121 static struct tok icmp2str
[] = {
122 { ICMP_ECHOREPLY
, "echo reply" },
123 { ICMP_SOURCEQUENCH
, "source quench" },
124 { ICMP_ECHO
, "echo request" },
125 { ICMP_ROUTERSOLICIT
, "router solicitation" },
126 { ICMP_TSTAMP
, "time stamp request" },
127 { ICMP_TSTAMPREPLY
, "time stamp reply" },
128 { ICMP_IREQ
, "information request" },
129 { ICMP_IREQREPLY
, "information reply" },
130 { ICMP_MASKREQ
, "address mask request" },
134 /* Formats for most of the ICMP_UNREACH codes */
135 static struct tok unreach2str
[] = {
136 { ICMP_UNREACH_NET
, "net %s unreachable" },
137 { ICMP_UNREACH_HOST
, "host %s unreachable" },
138 { ICMP_UNREACH_SRCFAIL
,
139 "%s unreachable - source route failed" },
140 { ICMP_UNREACH_NET_UNKNOWN
, "net %s unreachable - unknown" },
141 { ICMP_UNREACH_HOST_UNKNOWN
, "host %s unreachable - unknown" },
142 { ICMP_UNREACH_ISOLATED
,
143 "%s unreachable - source host isolated" },
144 { ICMP_UNREACH_NET_PROHIB
,
145 "net %s unreachable - admin prohibited" },
146 { ICMP_UNREACH_HOST_PROHIB
,
147 "host %s unreachable - admin prohibited" },
148 { ICMP_UNREACH_TOSNET
,
149 "net %s unreachable - tos prohibited" },
150 { ICMP_UNREACH_TOSHOST
,
151 "host %s unreachable - tos prohibited" },
152 { ICMP_UNREACH_FILTER_PROHIB
,
153 "host %s unreachable - admin prohibited filter" },
154 { ICMP_UNREACH_HOST_PRECEDENCE
,
155 "host %s unreachable - host precedence violation" },
156 { ICMP_UNREACH_PRECEDENCE_CUTOFF
,
157 "host %s unreachable - precedence cutoff" },
161 /* Formats for the ICMP_REDIRECT codes */
162 static struct tok type2str
[] = {
163 { ICMP_REDIRECT_NET
, "redirect %s to net %s" },
164 { ICMP_REDIRECT_HOST
, "redirect %s to host %s" },
165 { ICMP_REDIRECT_TOSNET
, "redirect-tos %s to net %s" },
166 { ICMP_REDIRECT_TOSHOST
, "redirect-tos %s to net %s" },
171 struct mtu_discovery
{
177 struct ih_rdiscovery
{
180 u_short ird_lifetime
;
183 struct id_rdiscovery
{
189 icmp_print(register const u_char
*bp
, register const u_char
*bp2
)
192 register const struct icmp
*dp
;
193 register const struct ip
*ip
;
194 register const char *str
, *fmt
;
195 register const struct ip
*oip
;
196 register const struct udphdr
*ouh
;
197 register u_int hlen
, dport
, mtu
;
200 dp
= (struct icmp
*)bp
;
201 ip
= (struct ip
*)bp2
;
204 (void)printf("%s > %s: ",
205 ipaddr_string(&ip
->ip_src
),
206 ipaddr_string(&ip
->ip_dst
));
208 TCHECK(dp
->icmp_code
);
209 switch (dp
->icmp_type
) {
212 TCHECK(dp
->icmp_ip
.ip_dst
);
213 switch (dp
->icmp_code
) {
215 case ICMP_UNREACH_PROTOCOL
:
216 TCHECK(dp
->icmp_ip
.ip_p
);
217 (void)sprintf(buf
, "%s protocol %d unreachable",
218 ipaddr_string(&dp
->icmp_ip
.ip_dst
),
222 case ICMP_UNREACH_PORT
:
223 TCHECK(dp
->icmp_ip
.ip_p
);
225 hlen
= oip
->ip_hl
* 4;
226 ouh
= (struct udphdr
*)(((u_char
*)oip
) + hlen
);
227 dport
= ntohs(ouh
->uh_dport
);
232 "%s tcp port %s unreachable",
233 ipaddr_string(&oip
->ip_dst
),
234 tcpport_string(dport
));
239 "%s udp port %s unreachable",
240 ipaddr_string(&oip
->ip_dst
),
241 udpport_string(dport
));
246 "%s protocol %d port %d unreachable",
247 ipaddr_string(&oip
->ip_dst
),
253 case ICMP_UNREACH_NEEDFRAG
:
255 register const struct mtu_discovery
*mp
;
257 mp
= (struct mtu_discovery
*)&dp
->icmp_void
;
258 mtu
= EXTRACT_16BITS(&mp
->nexthopmtu
);
261 "%s unreachable - need to frag (mtu %d)",
262 ipaddr_string(&dp
->icmp_ip
.ip_dst
), mtu
);
265 "%s unreachable - need to frag",
266 ipaddr_string(&dp
->icmp_ip
.ip_dst
));
271 fmt
= tok2str(unreach2str
, "#%d %%s unreachable",
273 (void)sprintf(buf
, fmt
,
274 ipaddr_string(&dp
->icmp_ip
.ip_dst
));
280 TCHECK(dp
->icmp_ip
.ip_dst
);
281 fmt
= tok2str(type2str
, "redirect-#%d %%s to net %%s",
283 (void)sprintf(buf
, fmt
,
284 ipaddr_string(&dp
->icmp_ip
.ip_dst
),
285 ipaddr_string(&dp
->icmp_gwaddr
));
288 case ICMP_ROUTERADVERT
:
290 register const struct ih_rdiscovery
*ihp
;
291 register const struct id_rdiscovery
*idp
;
292 u_int lifetime
, num
, size
;
294 (void)strcpy(buf
, "router advertisement");
295 cp
= buf
+ strlen(buf
);
297 ihp
= (struct ih_rdiscovery
*)&dp
->icmp_void
;
299 (void)strcpy(cp
, " lifetime ");
300 cp
= buf
+ strlen(buf
);
301 lifetime
= EXTRACT_16BITS(&ihp
->ird_lifetime
);
303 (void)sprintf(cp
, "%u", lifetime
);
304 else if (lifetime
< 60 * 60)
305 (void)sprintf(cp
, "%u:%02u",
306 lifetime
/ 60, lifetime
% 60);
308 (void)sprintf(cp
, "%u:%02u:%02u",
310 (lifetime
% 3600) / 60,
312 cp
= buf
+ strlen(buf
);
314 num
= ihp
->ird_addrnum
;
315 (void)sprintf(cp
, " %d:", num
);
316 cp
= buf
+ strlen(buf
);
318 size
= ihp
->ird_addrsiz
;
320 (void)sprintf(cp
, " [size %d]", size
);
323 idp
= (struct id_rdiscovery
*)&dp
->icmp_data
;
326 (void)sprintf(cp
, " {%s %u}",
327 ipaddr_string(&idp
->ird_addr
),
328 EXTRACT_32BITS(&idp
->ird_pref
));
329 cp
= buf
+ strlen(buf
);
335 TCHECK(dp
->icmp_ip
.ip_dst
);
336 switch (dp
->icmp_code
) {
338 case ICMP_TIMXCEED_INTRANS
:
339 str
= "time exceeded in-transit";
342 case ICMP_TIMXCEED_REASS
:
343 str
= "ip reassembly time exceeded";
347 (void)sprintf(buf
, "time exceeded-#%d", dp
->icmp_code
);
354 (void)sprintf(buf
, "parameter problem - code %d",
357 TCHECK(dp
->icmp_pptr
);
358 (void)sprintf(buf
, "parameter problem - octet %d",
364 TCHECK(dp
->icmp_mask
);
365 (void)sprintf(buf
, "address mask is 0x%08x",
366 (u_int32_t
)ntohl(dp
->icmp_mask
));
370 str
= tok2str(icmp2str
, "type-#%d", dp
->icmp_type
);
373 (void)printf("icmp: %s", str
);
376 fputs("[|icmp]", stdout
);