]> The Tcpdump Group git mirrors - tcpdump/blob - print-icmp.c
b04032d503b833e70c1343c7c106d057400c26de
[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.49 2000-09-23 08:03:35 guy 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 struct mbuf;
36 struct rtentry;
37 #include <net/if.h>
38
39 #include <netinet/in.h>
40 #include <netinet/in_systm.h>
41 #include <netinet/ip.h>
42 #include <netinet/ip_icmp.h>
43 #include <netinet/ip_var.h>
44 #include <netinet/udp.h>
45 #include <netinet/udp_var.h>
46 #include <netinet/tcp.h>
47
48 #include <stdio.h>
49 #include <string.h>
50
51 #include "interface.h"
52 #include "addrtoname.h"
53 #include "extract.h" /* must come after interface.h */
54
55 /* rfc1700 */
56 #ifndef ICMP_UNREACH_NET_UNKNOWN
57 #define ICMP_UNREACH_NET_UNKNOWN 6 /* destination net unknown */
58 #endif
59 #ifndef ICMP_UNREACH_HOST_UNKNOWN
60 #define ICMP_UNREACH_HOST_UNKNOWN 7 /* destination host unknown */
61 #endif
62 #ifndef ICMP_UNREACH_ISOLATED
63 #define ICMP_UNREACH_ISOLATED 8 /* source host isolated */
64 #endif
65 #ifndef ICMP_UNREACH_NET_PROHIB
66 #define ICMP_UNREACH_NET_PROHIB 9 /* admin prohibited net */
67 #endif
68 #ifndef ICMP_UNREACH_HOST_PROHIB
69 #define ICMP_UNREACH_HOST_PROHIB 10 /* admin prohibited host */
70 #endif
71 #ifndef ICMP_UNREACH_TOSNET
72 #define ICMP_UNREACH_TOSNET 11 /* tos prohibited net */
73 #endif
74 #ifndef ICMP_UNREACH_TOSHOST
75 #define ICMP_UNREACH_TOSHOST 12 /* tos prohibited host */
76 #endif
77
78 /* rfc1716 */
79 #ifndef ICMP_UNREACH_FILTER_PROHIB
80 #define ICMP_UNREACH_FILTER_PROHIB 13 /* admin prohibited filter */
81 #endif
82 #ifndef ICMP_UNREACH_HOST_PRECEDENCE
83 #define ICMP_UNREACH_HOST_PRECEDENCE 14 /* host precedence violation */
84 #endif
85 #ifndef ICMP_UNREACH_PRECEDENCE_CUTOFF
86 #define ICMP_UNREACH_PRECEDENCE_CUTOFF 15 /* precedence cutoff */
87 #endif
88
89 /* rfc1256 */
90 #ifndef ICMP_ROUTERADVERT
91 #define ICMP_ROUTERADVERT 9 /* router advertisement */
92 #endif
93 #ifndef ICMP_ROUTERSOLICIT
94 #define ICMP_ROUTERSOLICIT 10 /* router solicitation */
95 #endif
96
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" },
108 { 0, NULL }
109 };
110
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" },
135 { 0, NULL }
136 };
137
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" },
144 { 0, NULL }
145 };
146
147 /* rfc1191 */
148 struct mtu_discovery {
149 short unused;
150 short nexthopmtu;
151 };
152
153 /* rfc1256 */
154 struct ih_rdiscovery {
155 u_char ird_addrnum;
156 u_char ird_addrsiz;
157 u_short ird_lifetime;
158 };
159
160 struct id_rdiscovery {
161 u_int32_t ird_addr;
162 u_int32_t ird_pref;
163 };
164
165 void
166 icmp_print(register const u_char *bp, u_int plen, register const u_char *bp2)
167 {
168 register char *cp;
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;
175 char buf[256];
176
177 dp = (struct icmp *)bp;
178 ip = (struct ip *)bp2;
179 str = buf;
180
181 #if 0
182 (void)printf("%s > %s: ",
183 ipaddr_string(&ip->ip_src),
184 ipaddr_string(&ip->ip_dst));
185 #endif
186
187 TCHECK(dp->icmp_code);
188 switch (dp->icmp_type) {
189
190 case ICMP_UNREACH:
191 TCHECK(dp->icmp_ip.ip_dst);
192 switch (dp->icmp_code) {
193
194 case ICMP_UNREACH_PROTOCOL:
195 TCHECK(dp->icmp_ip.ip_p);
196 (void)snprintf(buf, sizeof(buf),
197 "%s protocol %d unreachable",
198 ipaddr_string(&dp->icmp_ip.ip_dst),
199 dp->icmp_ip.ip_p);
200 break;
201
202 case ICMP_UNREACH_PORT:
203 TCHECK(dp->icmp_ip.ip_p);
204 oip = &dp->icmp_ip;
205 hlen = oip->ip_hl * 4;
206 ouh = (struct udphdr *)(((u_char *)oip) + hlen);
207 dport = ntohs(ouh->uh_dport);
208 switch (oip->ip_p) {
209
210 case IPPROTO_TCP:
211 (void)snprintf(buf, sizeof(buf),
212 "%s tcp port %s unreachable",
213 ipaddr_string(&oip->ip_dst),
214 tcpport_string(dport));
215 break;
216
217 case IPPROTO_UDP:
218 (void)snprintf(buf, sizeof(buf),
219 "%s udp port %s unreachable",
220 ipaddr_string(&oip->ip_dst),
221 udpport_string(dport));
222 break;
223
224 default:
225 (void)snprintf(buf, sizeof(buf),
226 "%s protocol %d port %d unreachable",
227 ipaddr_string(&oip->ip_dst),
228 oip->ip_p, dport);
229 break;
230 }
231 break;
232
233 case ICMP_UNREACH_NEEDFRAG:
234 {
235 register const struct mtu_discovery *mp;
236 mp = (struct mtu_discovery *)&dp->icmp_void;
237 mtu = EXTRACT_16BITS(&mp->nexthopmtu);
238 if (mtu) {
239 (void)snprintf(buf, sizeof(buf),
240 "%s unreachable - need to frag (mtu %d)",
241 ipaddr_string(&dp->icmp_ip.ip_dst), mtu);
242 } else {
243 (void)snprintf(buf, sizeof(buf),
244 "%s unreachable - need to frag",
245 ipaddr_string(&dp->icmp_ip.ip_dst));
246 }
247 }
248 break;
249
250 default:
251 fmt = tok2str(unreach2str, "#%d %%s unreachable",
252 dp->icmp_code);
253 (void)snprintf(buf, sizeof(buf), fmt,
254 ipaddr_string(&dp->icmp_ip.ip_dst));
255 break;
256 }
257 break;
258
259 case ICMP_REDIRECT:
260 TCHECK(dp->icmp_ip.ip_dst);
261 fmt = tok2str(type2str, "redirect-#%d %%s to net %%s",
262 dp->icmp_code);
263 (void)snprintf(buf, sizeof(buf), fmt,
264 ipaddr_string(&dp->icmp_ip.ip_dst),
265 ipaddr_string(&dp->icmp_gwaddr));
266 break;
267
268 case ICMP_ROUTERADVERT:
269 {
270 register const struct ih_rdiscovery *ihp;
271 register const struct id_rdiscovery *idp;
272 u_int lifetime, num, size;
273
274 (void)snprintf(buf, sizeof(buf), "router advertisement");
275 cp = buf + strlen(buf);
276
277 ihp = (struct ih_rdiscovery *)&dp->icmp_void;
278 TCHECK(*ihp);
279 (void)strncpy(cp, " lifetime ", sizeof(buf) - (cp - buf));
280 cp = buf + strlen(buf);
281 lifetime = EXTRACT_16BITS(&ihp->ird_lifetime);
282 if (lifetime < 60) {
283 (void)snprintf(cp, sizeof(buf) - (cp - buf), "%u",
284 lifetime);
285 } else if (lifetime < 60 * 60) {
286 (void)snprintf(cp, sizeof(buf) - (cp - buf), "%u:%02u",
287 lifetime / 60, lifetime % 60);
288 } else {
289 (void)snprintf(cp, sizeof(buf) - (cp - buf),
290 "%u:%02u:%02u",
291 lifetime / 3600,
292 (lifetime % 3600) / 60,
293 lifetime % 60);
294 }
295 cp = buf + strlen(buf);
296
297 num = ihp->ird_addrnum;
298 (void)snprintf(cp, sizeof(buf) - (cp - buf), " %d:", num);
299 cp = buf + strlen(buf);
300
301 size = ihp->ird_addrsiz;
302 if (size != 2) {
303 (void)snprintf(cp, sizeof(buf) - (cp - buf),
304 " [size %d]", size);
305 break;
306 }
307 idp = (struct id_rdiscovery *)&dp->icmp_data;
308 while (num-- > 0) {
309 TCHECK(*idp);
310 (void)snprintf(cp, sizeof(buf) - (cp - buf), " {%s %u}",
311 ipaddr_string(&idp->ird_addr),
312 EXTRACT_32BITS(&idp->ird_pref));
313 cp = buf + strlen(buf);
314 ++idp;
315 }
316 }
317 break;
318
319 case ICMP_TIMXCEED:
320 TCHECK(dp->icmp_ip.ip_dst);
321 switch (dp->icmp_code) {
322
323 case ICMP_TIMXCEED_INTRANS:
324 str = "time exceeded in-transit";
325 break;
326
327 case ICMP_TIMXCEED_REASS:
328 str = "ip reassembly time exceeded";
329 break;
330
331 default:
332 (void)snprintf(buf, sizeof(buf), "time exceeded-#%d",
333 dp->icmp_code);
334 break;
335 }
336 break;
337
338 case ICMP_PARAMPROB:
339 if (dp->icmp_code)
340 (void)snprintf(buf, sizeof(buf),
341 "parameter problem - code %d", dp->icmp_code);
342 else {
343 TCHECK(dp->icmp_pptr);
344 (void)snprintf(buf, sizeof(buf),
345 "parameter problem - octet %d", dp->icmp_pptr);
346 }
347 break;
348
349 case ICMP_MASKREPLY:
350 TCHECK(dp->icmp_mask);
351 (void)snprintf(buf, sizeof(buf), "address mask is 0x%08x",
352 (unsigned)ntohl(dp->icmp_mask));
353 break;
354
355 case ICMP_TSTAMP:
356 TCHECK(dp->icmp_seq);
357 (void)sprintf(buf, "time stamp query id %u seq %u",
358 (unsigned)ntohs(dp->icmp_id),
359 (unsigned)ntohs(dp->icmp_seq));
360 break;
361
362 case ICMP_TSTAMPREPLY:
363 TCHECK(dp->icmp_ttime);
364 (void)sprintf(buf, "time stamp reply id %u seq %u : org 0x%lx recv 0x%lx xmit 0x%lx",
365 (unsigned)ntohs(dp->icmp_id),
366 (unsigned)ntohs(dp->icmp_seq),
367 (unsigned long)ntohl(dp->icmp_otime),
368 (unsigned long)ntohl(dp->icmp_rtime),
369 (unsigned long)ntohl(dp->icmp_ttime));
370 break;
371
372 default:
373 str = tok2str(icmp2str, "type-#%d", dp->icmp_type);
374 break;
375 }
376 (void)printf("icmp: %s", str);
377 if (vflag) {
378 if (TTEST2(*bp, plen)) {
379 if (in_cksum((u_short*)dp, plen, 0))
380 printf(" (wrong icmp csum)");
381 }
382 }
383 if (vflag > 1 && !ICMP_INFOTYPE(dp->icmp_type)) {
384 bp += 8;
385 (void)printf(" for ");
386 ip = (struct ip *)bp;
387 snaplen = snapend - bp;
388 ip_print(bp, ntohs(ip->ip_len));
389 }
390 return;
391 trunc:
392 fputs("[|icmp]", stdout);
393 }