]> The Tcpdump Group git mirrors - tcpdump/blob - print-icmp.c
s/sprintf/snprintf/.
[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.44 2000-01-17 06:24:25 itojun 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)snprintf(buf, sizeof(buf),
200 "%s protocol %d unreachable",
201 ipaddr_string(&dp->icmp_ip.ip_dst),
202 dp->icmp_ip.ip_p);
203 break;
204
205 case ICMP_UNREACH_PORT:
206 TCHECK(dp->icmp_ip.ip_p);
207 oip = &dp->icmp_ip;
208 hlen = oip->ip_hl * 4;
209 ouh = (struct udphdr *)(((u_char *)oip) + hlen);
210 dport = ntohs(ouh->uh_dport);
211 switch (oip->ip_p) {
212
213 case IPPROTO_TCP:
214 (void)snprintf(buf, sizeof(buf),
215 "%s tcp port %s unreachable",
216 ipaddr_string(&oip->ip_dst),
217 tcpport_string(dport));
218 break;
219
220 case IPPROTO_UDP:
221 (void)snprintf(buf, sizeof(buf),
222 "%s udp port %s unreachable",
223 ipaddr_string(&oip->ip_dst),
224 udpport_string(dport));
225 break;
226
227 default:
228 (void)snprintf(buf, sizeof(buf),
229 "%s protocol %d port %d unreachable",
230 ipaddr_string(&oip->ip_dst),
231 oip->ip_p, dport);
232 break;
233 }
234 break;
235
236 case ICMP_UNREACH_NEEDFRAG:
237 {
238 register const struct mtu_discovery *mp;
239
240 mp = (struct mtu_discovery *)&dp->icmp_void;
241 mtu = EXTRACT_16BITS(&mp->nexthopmtu);
242 if (mtu)
243 (void)snprintf(buf, sizeof(buf),
244 "%s unreachable - need to frag (mtu %d)",
245 ipaddr_string(&dp->icmp_ip.ip_dst), mtu);
246 else
247 (void)snprintf(buf, sizeof(buf),
248 "%s unreachable - need to frag",
249 ipaddr_string(&dp->icmp_ip.ip_dst));
250 }
251 break;
252
253 default:
254 fmt = tok2str(unreach2str, "#%d %%s unreachable",
255 dp->icmp_code);
256 (void)snprintf(buf, sizeof(buf), fmt,
257 ipaddr_string(&dp->icmp_ip.ip_dst));
258 break;
259 }
260 break;
261
262 case ICMP_REDIRECT:
263 TCHECK(dp->icmp_ip.ip_dst);
264 fmt = tok2str(type2str, "redirect-#%d %%s to net %%s",
265 dp->icmp_code);
266 (void)snprintf(buf, sizeof(buf), fmt,
267 ipaddr_string(&dp->icmp_ip.ip_dst),
268 ipaddr_string(&dp->icmp_gwaddr));
269 break;
270
271 case ICMP_ROUTERADVERT:
272 {
273 register const struct ih_rdiscovery *ihp;
274 register const struct id_rdiscovery *idp;
275 u_int lifetime, num, size;
276
277 (void)strcpy(buf, "router advertisement");
278 cp = buf + strlen(buf);
279
280 ihp = (struct ih_rdiscovery *)&dp->icmp_void;
281 TCHECK(*ihp);
282 (void)strncpy(cp, " lifetime ", sizeof(buf) - (cp - buf));
283 cp = buf + strlen(buf);
284 lifetime = EXTRACT_16BITS(&ihp->ird_lifetime);
285 if (lifetime < 60)
286 (void)snprintf(cp, sizeof(buf) - (cp - buf), "%u",
287 lifetime);
288 else if (lifetime < 60 * 60)
289 (void)snprintf(cp, sizeof(buf) - (cp - buf), "%u:%02u",
290 lifetime / 60, lifetime % 60);
291 else
292 (void)snprintf(cp, sizeof(buf) - (cp - buf),
293 "%u:%02u:%02u",
294 lifetime / 3600,
295 (lifetime % 3600) / 60,
296 lifetime % 60);
297 cp = buf + strlen(buf);
298
299 num = ihp->ird_addrnum;
300 (void)snprintf(cp, sizeof(buf) - (cp - buf), " %d:", num);
301 cp = buf + strlen(buf);
302
303 size = ihp->ird_addrsiz;
304 if (size != 2) {
305 (void)snprintf(cp, sizeof(buf) - (cp - buf),
306 " [size %d]", size);
307 break;
308 }
309 idp = (struct id_rdiscovery *)&dp->icmp_data;
310 while (num-- > 0) {
311 TCHECK(*idp);
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);
316 }
317 }
318 break;
319
320 case ICMP_TIMXCEED:
321 TCHECK(dp->icmp_ip.ip_dst);
322 switch (dp->icmp_code) {
323
324 case ICMP_TIMXCEED_INTRANS:
325 str = "time exceeded in-transit";
326 break;
327
328 case ICMP_TIMXCEED_REASS:
329 str = "ip reassembly time exceeded";
330 break;
331
332 default:
333 (void)snprintf(buf, sizeof(buf), "time exceeded-#%d",
334 dp->icmp_code);
335 break;
336 }
337 break;
338
339 case ICMP_PARAMPROB:
340 if (dp->icmp_code)
341 (void)snprintf(buf, sizeof(buf),
342 "parameter problem - code %d", dp->icmp_code);
343 else {
344 TCHECK(dp->icmp_pptr);
345 (void)snprintf(buf, sizeof(buf),
346 "parameter problem - octet %d", dp->icmp_pptr);
347 }
348 break;
349
350 case ICMP_MASKREPLY:
351 TCHECK(dp->icmp_mask);
352 (void)snprintf(buf, sizeof(buf), "address mask is 0x%08x",
353 (u_int32_t)ntohl(dp->icmp_mask));
354 break;
355
356 default:
357 str = tok2str(icmp2str, "type-#%d", dp->icmp_type);
358 break;
359 }
360 (void)printf("icmp: %s", str);
361 if (vflag) {
362 if (TTEST2(*bp, plen)) {
363 if (in_cksum((u_short*)dp, plen, 0))
364 printf(" (wrong icmp csum)");
365 }
366 }
367 if (vflag > 1 && !ICMP_INFOTYPE(dp->icmp_type)) {
368 bp += 8;
369 (void)printf(" for ");
370 ip = (struct ip *)bp;
371 snaplen = snapend - bp;
372 ip_print(bp, ntohs(ip->ip_len));
373 }
374 return;
375 trunc:
376 fputs("[|icmp]", stdout);
377 }