]> git.saurik.com Git - apple/network_cmds.git/blob - tcpdump.tproj/print-icmp.c
network_cmds-76.tar.gz
[apple/network_cmds.git] / tcpdump.tproj / print-icmp.c
1 /*
2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
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
12 * this file.
13 *
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
20 * under the License."
21 *
22 * @APPLE_LICENSE_HEADER_END@
23 */
24 /*
25 * Copyright (c) 1988, 1989, 1990, 1991, 1993, 1994, 1995, 1996
26 * The Regents of the University of California. All rights reserved.
27 *
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
39 * written permission.
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.
43 */
44
45 #ifndef lint
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)";
48 #endif
49
50 #include <sys/param.h>
51 #include <sys/time.h>
52 #include <sys/socket.h>
53
54 #if __STDC__
55 struct mbuf;
56 struct rtentry;
57 #endif
58 #include <net/if.h>
59
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>
70
71 #include <stdio.h>
72 #include <string.h>
73
74 #include "interface.h"
75 #include "addrtoname.h"
76 #include "extract.h" /* must come after interface.h */
77
78 /* rfc1700 */
79 #ifndef ICMP_UNREACH_NET_UNKNOWN
80 #define ICMP_UNREACH_NET_UNKNOWN 6 /* destination net unknown */
81 #endif
82 #ifndef ICMP_UNREACH_HOST_UNKNOWN
83 #define ICMP_UNREACH_HOST_UNKNOWN 7 /* destination host unknown */
84 #endif
85 #ifndef ICMP_UNREACH_ISOLATED
86 #define ICMP_UNREACH_ISOLATED 8 /* source host isolated */
87 #endif
88 #ifndef ICMP_UNREACH_NET_PROHIB
89 #define ICMP_UNREACH_NET_PROHIB 9 /* admin prohibited net */
90 #endif
91 #ifndef ICMP_UNREACH_HOST_PROHIB
92 #define ICMP_UNREACH_HOST_PROHIB 10 /* admin prohibited host */
93 #endif
94 #ifndef ICMP_UNREACH_TOSNET
95 #define ICMP_UNREACH_TOSNET 11 /* tos prohibited net */
96 #endif
97 #ifndef ICMP_UNREACH_TOSHOST
98 #define ICMP_UNREACH_TOSHOST 12 /* tos prohibited host */
99 #endif
100
101 /* rfc1716 */
102 #ifndef ICMP_UNREACH_FILTER_PROHIB
103 #define ICMP_UNREACH_FILTER_PROHIB 13 /* admin prohibited filter */
104 #endif
105 #ifndef ICMP_UNREACH_HOST_PRECEDENCE
106 #define ICMP_UNREACH_HOST_PRECEDENCE 14 /* host precedence violation */
107 #endif
108 #ifndef ICMP_UNREACH_PRECEDENCE_CUTOFF
109 #define ICMP_UNREACH_PRECEDENCE_CUTOFF 15 /* precedence cutoff */
110 #endif
111
112 /* rfc1256 */
113 #ifndef ICMP_ROUTERADVERT
114 #define ICMP_ROUTERADVERT 9 /* router advertisement */
115 #endif
116 #ifndef ICMP_ROUTERSOLICIT
117 #define ICMP_ROUTERSOLICIT 10 /* router solicitation */
118 #endif
119
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" },
131 { 0, NULL }
132 };
133
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" },
158 { 0, NULL }
159 };
160
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" },
167 { 0, NULL }
168 };
169
170 /* rfc1191 */
171 struct mtu_discovery {
172 short unused;
173 short nexthopmtu;
174 };
175
176 /* rfc1256 */
177 struct ih_rdiscovery {
178 u_char ird_addrnum;
179 u_char ird_addrsiz;
180 u_short ird_lifetime;
181 };
182
183 struct id_rdiscovery {
184 u_int32_t ird_addr;
185 u_int32_t ird_pref;
186 };
187
188 void
189 icmp_print(register const u_char *bp, register const u_char *bp2)
190 {
191 register char *cp;
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;
198 char buf[256];
199
200 dp = (struct icmp *)bp;
201 ip = (struct ip *)bp2;
202 str = buf;
203
204 (void)printf("%s > %s: ",
205 ipaddr_string(&ip->ip_src),
206 ipaddr_string(&ip->ip_dst));
207
208 TCHECK(dp->icmp_code);
209 switch (dp->icmp_type) {
210
211 case ICMP_UNREACH:
212 TCHECK(dp->icmp_ip.ip_dst);
213 switch (dp->icmp_code) {
214
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),
219 dp->icmp_ip.ip_p);
220 break;
221
222 case ICMP_UNREACH_PORT:
223 TCHECK(dp->icmp_ip.ip_p);
224 oip = &dp->icmp_ip;
225 hlen = oip->ip_hl * 4;
226 ouh = (struct udphdr *)(((u_char *)oip) + hlen);
227 dport = ntohs(ouh->uh_dport);
228 switch (oip->ip_p) {
229
230 case IPPROTO_TCP:
231 (void)sprintf(buf,
232 "%s tcp port %s unreachable",
233 ipaddr_string(&oip->ip_dst),
234 tcpport_string(dport));
235 break;
236
237 case IPPROTO_UDP:
238 (void)sprintf(buf,
239 "%s udp port %s unreachable",
240 ipaddr_string(&oip->ip_dst),
241 udpport_string(dport));
242 break;
243
244 default:
245 (void)sprintf(buf,
246 "%s protocol %d port %d unreachable",
247 ipaddr_string(&oip->ip_dst),
248 oip->ip_p, dport);
249 break;
250 }
251 break;
252
253 case ICMP_UNREACH_NEEDFRAG:
254 {
255 register const struct mtu_discovery *mp;
256
257 mp = (struct mtu_discovery *)&dp->icmp_void;
258 mtu = EXTRACT_16BITS(&mp->nexthopmtu);
259 if (mtu)
260 (void)sprintf(buf,
261 "%s unreachable - need to frag (mtu %d)",
262 ipaddr_string(&dp->icmp_ip.ip_dst), mtu);
263 else
264 (void)sprintf(buf,
265 "%s unreachable - need to frag",
266 ipaddr_string(&dp->icmp_ip.ip_dst));
267 }
268 break;
269
270 default:
271 fmt = tok2str(unreach2str, "#%d %%s unreachable",
272 dp->icmp_code);
273 (void)sprintf(buf, fmt,
274 ipaddr_string(&dp->icmp_ip.ip_dst));
275 break;
276 }
277 break;
278
279 case ICMP_REDIRECT:
280 TCHECK(dp->icmp_ip.ip_dst);
281 fmt = tok2str(type2str, "redirect-#%d %%s to net %%s",
282 dp->icmp_code);
283 (void)sprintf(buf, fmt,
284 ipaddr_string(&dp->icmp_ip.ip_dst),
285 ipaddr_string(&dp->icmp_gwaddr));
286 break;
287
288 case ICMP_ROUTERADVERT:
289 {
290 register const struct ih_rdiscovery *ihp;
291 register const struct id_rdiscovery *idp;
292 u_int lifetime, num, size;
293
294 (void)strcpy(buf, "router advertisement");
295 cp = buf + strlen(buf);
296
297 ihp = (struct ih_rdiscovery *)&dp->icmp_void;
298 TCHECK(*ihp);
299 (void)strcpy(cp, " lifetime ");
300 cp = buf + strlen(buf);
301 lifetime = EXTRACT_16BITS(&ihp->ird_lifetime);
302 if (lifetime < 60)
303 (void)sprintf(cp, "%u", lifetime);
304 else if (lifetime < 60 * 60)
305 (void)sprintf(cp, "%u:%02u",
306 lifetime / 60, lifetime % 60);
307 else
308 (void)sprintf(cp, "%u:%02u:%02u",
309 lifetime / 3600,
310 (lifetime % 3600) / 60,
311 lifetime % 60);
312 cp = buf + strlen(buf);
313
314 num = ihp->ird_addrnum;
315 (void)sprintf(cp, " %d:", num);
316 cp = buf + strlen(buf);
317
318 size = ihp->ird_addrsiz;
319 if (size != 2) {
320 (void)sprintf(cp, " [size %d]", size);
321 break;
322 }
323 idp = (struct id_rdiscovery *)&dp->icmp_data;
324 while (num-- > 0) {
325 TCHECK(*idp);
326 (void)sprintf(cp, " {%s %u}",
327 ipaddr_string(&idp->ird_addr),
328 EXTRACT_32BITS(&idp->ird_pref));
329 cp = buf + strlen(buf);
330 }
331 }
332 break;
333
334 case ICMP_TIMXCEED:
335 TCHECK(dp->icmp_ip.ip_dst);
336 switch (dp->icmp_code) {
337
338 case ICMP_TIMXCEED_INTRANS:
339 str = "time exceeded in-transit";
340 break;
341
342 case ICMP_TIMXCEED_REASS:
343 str = "ip reassembly time exceeded";
344 break;
345
346 default:
347 (void)sprintf(buf, "time exceeded-#%d", dp->icmp_code);
348 break;
349 }
350 break;
351
352 case ICMP_PARAMPROB:
353 if (dp->icmp_code)
354 (void)sprintf(buf, "parameter problem - code %d",
355 dp->icmp_code);
356 else {
357 TCHECK(dp->icmp_pptr);
358 (void)sprintf(buf, "parameter problem - octet %d",
359 dp->icmp_pptr);
360 }
361 break;
362
363 case ICMP_MASKREPLY:
364 TCHECK(dp->icmp_mask);
365 (void)sprintf(buf, "address mask is 0x%08x",
366 (u_int32_t)ntohl(dp->icmp_mask));
367 break;
368
369 default:
370 str = tok2str(icmp2str, "type-#%d", dp->icmp_type);
371 break;
372 }
373 (void)printf("icmp: %s", str);
374 return;
375 trunc:
376 fputs("[|icmp]", stdout);
377 }