]> The Tcpdump Group git mirrors - tcpdump/blob - print-icmp.c
22f03baf815503c873d5e760ce0020baef2d6607
[tcpdump] / print-icmp.c
1 /*
2 * Copyright (c) 1988, 1989, 1990, 1991, 1993, 1994, 1995, 1996
3 * The Regents of the University of California. All rights reserved.
4 *
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
16 * written permission.
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.
20 */
21
22 #ifndef lint
23 static const char rcsid[] =
24 "@(#) $Header: /tcpdump/master/tcpdump/print-icmp.c,v 1.42 1999-11-21 09:36:52 fenner Exp $ (LBL)";
25 #endif
26
27 #ifdef HAVE_CONFIG_H
28 #include "config.h"
29 #endif
30
31 #include <sys/param.h>
32 #include <sys/time.h>
33 #include <sys/socket.h>
34
35 #if __STDC__
36 struct mbuf;
37 struct rtentry;
38 #endif
39 #include <net/if.h>
40
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>
50
51 #include <stdio.h>
52 #include <string.h>
53
54 #include "interface.h"
55 #include "addrtoname.h"
56 #include "extract.h" /* must come after interface.h */
57
58 /* rfc1700 */
59 #ifndef ICMP_UNREACH_NET_UNKNOWN
60 #define ICMP_UNREACH_NET_UNKNOWN 6 /* destination net unknown */
61 #endif
62 #ifndef ICMP_UNREACH_HOST_UNKNOWN
63 #define ICMP_UNREACH_HOST_UNKNOWN 7 /* destination host unknown */
64 #endif
65 #ifndef ICMP_UNREACH_ISOLATED
66 #define ICMP_UNREACH_ISOLATED 8 /* source host isolated */
67 #endif
68 #ifndef ICMP_UNREACH_NET_PROHIB
69 #define ICMP_UNREACH_NET_PROHIB 9 /* admin prohibited net */
70 #endif
71 #ifndef ICMP_UNREACH_HOST_PROHIB
72 #define ICMP_UNREACH_HOST_PROHIB 10 /* admin prohibited host */
73 #endif
74 #ifndef ICMP_UNREACH_TOSNET
75 #define ICMP_UNREACH_TOSNET 11 /* tos prohibited net */
76 #endif
77 #ifndef ICMP_UNREACH_TOSHOST
78 #define ICMP_UNREACH_TOSHOST 12 /* tos prohibited host */
79 #endif
80
81 /* rfc1716 */
82 #ifndef ICMP_UNREACH_FILTER_PROHIB
83 #define ICMP_UNREACH_FILTER_PROHIB 13 /* admin prohibited filter */
84 #endif
85 #ifndef ICMP_UNREACH_HOST_PRECEDENCE
86 #define ICMP_UNREACH_HOST_PRECEDENCE 14 /* host precedence violation */
87 #endif
88 #ifndef ICMP_UNREACH_PRECEDENCE_CUTOFF
89 #define ICMP_UNREACH_PRECEDENCE_CUTOFF 15 /* precedence cutoff */
90 #endif
91
92 /* rfc1256 */
93 #ifndef ICMP_ROUTERADVERT
94 #define ICMP_ROUTERADVERT 9 /* router advertisement */
95 #endif
96 #ifndef ICMP_ROUTERSOLICIT
97 #define ICMP_ROUTERSOLICIT 10 /* router solicitation */
98 #endif
99
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" },
111 { 0, NULL }
112 };
113
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" },
138 { 0, NULL }
139 };
140
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" },
147 { 0, NULL }
148 };
149
150 /* rfc1191 */
151 struct mtu_discovery {
152 short unused;
153 short nexthopmtu;
154 };
155
156 /* rfc1256 */
157 struct ih_rdiscovery {
158 u_char ird_addrnum;
159 u_char ird_addrsiz;
160 u_short ird_lifetime;
161 };
162
163 struct id_rdiscovery {
164 u_int32_t ird_addr;
165 u_int32_t ird_pref;
166 };
167
168 void
169 icmp_print(register const u_char *bp, u_int plen, register const u_char *bp2)
170 {
171 register char *cp;
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;
178 char buf[256];
179
180 dp = (struct icmp *)bp;
181 ip = (struct ip *)bp2;
182 str = buf;
183
184 #if 0
185 (void)printf("%s > %s: ",
186 ipaddr_string(&ip->ip_src),
187 ipaddr_string(&ip->ip_dst));
188 #endif
189
190 TCHECK(dp->icmp_code);
191 switch (dp->icmp_type) {
192
193 case ICMP_UNREACH:
194 TCHECK(dp->icmp_ip.ip_dst);
195 switch (dp->icmp_code) {
196
197 case ICMP_UNREACH_PROTOCOL:
198 TCHECK(dp->icmp_ip.ip_p);
199 (void)sprintf(buf, "%s protocol %d unreachable",
200 ipaddr_string(&dp->icmp_ip.ip_dst),
201 dp->icmp_ip.ip_p);
202 break;
203
204 case ICMP_UNREACH_PORT:
205 TCHECK(dp->icmp_ip.ip_p);
206 oip = &dp->icmp_ip;
207 hlen = oip->ip_hl * 4;
208 ouh = (struct udphdr *)(((u_char *)oip) + hlen);
209 dport = ntohs(ouh->uh_dport);
210 switch (oip->ip_p) {
211
212 case IPPROTO_TCP:
213 (void)sprintf(buf,
214 "%s tcp port %s unreachable",
215 ipaddr_string(&oip->ip_dst),
216 tcpport_string(dport));
217 break;
218
219 case IPPROTO_UDP:
220 (void)sprintf(buf,
221 "%s udp port %s unreachable",
222 ipaddr_string(&oip->ip_dst),
223 udpport_string(dport));
224 break;
225
226 default:
227 (void)sprintf(buf,
228 "%s protocol %d port %d unreachable",
229 ipaddr_string(&oip->ip_dst),
230 oip->ip_p, dport);
231 break;
232 }
233 break;
234
235 case ICMP_UNREACH_NEEDFRAG:
236 {
237 register const struct mtu_discovery *mp;
238
239 mp = (struct mtu_discovery *)&dp->icmp_void;
240 mtu = EXTRACT_16BITS(&mp->nexthopmtu);
241 if (mtu)
242 (void)sprintf(buf,
243 "%s unreachable - need to frag (mtu %d)",
244 ipaddr_string(&dp->icmp_ip.ip_dst), mtu);
245 else
246 (void)sprintf(buf,
247 "%s unreachable - need to frag",
248 ipaddr_string(&dp->icmp_ip.ip_dst));
249 }
250 break;
251
252 default:
253 fmt = tok2str(unreach2str, "#%d %%s unreachable",
254 dp->icmp_code);
255 (void)sprintf(buf, fmt,
256 ipaddr_string(&dp->icmp_ip.ip_dst));
257 break;
258 }
259 break;
260
261 case ICMP_REDIRECT:
262 TCHECK(dp->icmp_ip.ip_dst);
263 fmt = tok2str(type2str, "redirect-#%d %%s to net %%s",
264 dp->icmp_code);
265 (void)sprintf(buf, fmt,
266 ipaddr_string(&dp->icmp_ip.ip_dst),
267 ipaddr_string(&dp->icmp_gwaddr));
268 break;
269
270 case ICMP_ROUTERADVERT:
271 {
272 register const struct ih_rdiscovery *ihp;
273 register const struct id_rdiscovery *idp;
274 u_int lifetime, num, size;
275
276 (void)strcpy(buf, "router advertisement");
277 cp = buf + strlen(buf);
278
279 ihp = (struct ih_rdiscovery *)&dp->icmp_void;
280 TCHECK(*ihp);
281 (void)strcpy(cp, " lifetime ");
282 cp = buf + strlen(buf);
283 lifetime = EXTRACT_16BITS(&ihp->ird_lifetime);
284 if (lifetime < 60)
285 (void)sprintf(cp, "%u", lifetime);
286 else if (lifetime < 60 * 60)
287 (void)sprintf(cp, "%u:%02u",
288 lifetime / 60, lifetime % 60);
289 else
290 (void)sprintf(cp, "%u:%02u:%02u",
291 lifetime / 3600,
292 (lifetime % 3600) / 60,
293 lifetime % 60);
294 cp = buf + strlen(buf);
295
296 num = ihp->ird_addrnum;
297 (void)sprintf(cp, " %d:", num);
298 cp = buf + strlen(buf);
299
300 size = ihp->ird_addrsiz;
301 if (size != 2) {
302 (void)sprintf(cp, " [size %d]", size);
303 break;
304 }
305 idp = (struct id_rdiscovery *)&dp->icmp_data;
306 while (num-- > 0) {
307 TCHECK(*idp);
308 (void)sprintf(cp, " {%s %u}",
309 ipaddr_string(&idp->ird_addr),
310 EXTRACT_32BITS(&idp->ird_pref));
311 cp = buf + strlen(buf);
312 }
313 }
314 break;
315
316 case ICMP_TIMXCEED:
317 TCHECK(dp->icmp_ip.ip_dst);
318 switch (dp->icmp_code) {
319
320 case ICMP_TIMXCEED_INTRANS:
321 str = "time exceeded in-transit";
322 break;
323
324 case ICMP_TIMXCEED_REASS:
325 str = "ip reassembly time exceeded";
326 break;
327
328 default:
329 (void)sprintf(buf, "time exceeded-#%d", dp->icmp_code);
330 break;
331 }
332 break;
333
334 case ICMP_PARAMPROB:
335 if (dp->icmp_code)
336 (void)sprintf(buf, "parameter problem - code %d",
337 dp->icmp_code);
338 else {
339 TCHECK(dp->icmp_pptr);
340 (void)sprintf(buf, "parameter problem - octet %d",
341 dp->icmp_pptr);
342 }
343 break;
344
345 case ICMP_MASKREPLY:
346 TCHECK(dp->icmp_mask);
347 (void)sprintf(buf, "address mask is 0x%08x",
348 (u_int32_t)ntohl(dp->icmp_mask));
349 break;
350
351 default:
352 str = tok2str(icmp2str, "type-#%d", dp->icmp_type);
353 break;
354 }
355 (void)printf("icmp: %s", str);
356 if (vflag) {
357 if (((u_char*)bp) + plen <= snapend) {
358 if (in_cksum((u_short*)dp, plen, 0))
359 printf(" (wrong icmp csum)");
360 }
361 }
362 return;
363 trunc:
364 fputs("[|icmp]", stdout);
365 }