]> The Tcpdump Group git mirrors - tcpdump/blob - print-icmp.c
Get rid of unneeded includes of <net/if.h>.
[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.52 2000-09-28 06:43:00 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
38 #include <netinet/in.h>
39 #include <netinet/in_systm.h>
40
41 #include <stdio.h>
42 #include <string.h>
43
44 #include "interface.h"
45 #include "addrtoname.h"
46 #include "extract.h" /* must come after interface.h */
47
48 #include "ip.h"
49 #include "udp.h"
50
51 /*
52 * Interface Control Message Protocol Definitions.
53 * Per RFC 792, September 1981.
54 */
55
56 /*
57 * Structure of an icmp header.
58 */
59 struct icmp {
60 u_char icmp_type; /* type of message, see below */
61 u_char icmp_code; /* type sub code */
62 u_short icmp_cksum; /* ones complement cksum of struct */
63 union {
64 u_char ih_pptr; /* ICMP_PARAMPROB */
65 struct in_addr ih_gwaddr; /* ICMP_REDIRECT */
66 struct ih_idseq {
67 n_short icd_id;
68 n_short icd_seq;
69 } ih_idseq;
70 int ih_void;
71
72 /* ICMP_UNREACH_NEEDFRAG -- Path MTU Discovery (RFC1191) */
73 struct ih_pmtu {
74 n_short ipm_void;
75 n_short ipm_nextmtu;
76 } ih_pmtu;
77 } icmp_hun;
78 #define icmp_pptr icmp_hun.ih_pptr
79 #define icmp_gwaddr icmp_hun.ih_gwaddr
80 #define icmp_id icmp_hun.ih_idseq.icd_id
81 #define icmp_seq icmp_hun.ih_idseq.icd_seq
82 #define icmp_void icmp_hun.ih_void
83 #define icmp_pmvoid icmp_hun.ih_pmtu.ipm_void
84 #define icmp_nextmtu icmp_hun.ih_pmtu.ipm_nextmtu
85 union {
86 struct id_ts {
87 n_time its_otime;
88 n_time its_rtime;
89 n_time its_ttime;
90 } id_ts;
91 struct id_ip {
92 struct ip idi_ip;
93 /* options and then 64 bits of data */
94 } id_ip;
95 u_int id_mask;
96 char id_data[1];
97 } icmp_dun;
98 #define icmp_otime icmp_dun.id_ts.its_otime
99 #define icmp_rtime icmp_dun.id_ts.its_rtime
100 #define icmp_ttime icmp_dun.id_ts.its_ttime
101 #define icmp_ip icmp_dun.id_ip.idi_ip
102 #define icmp_mask icmp_dun.id_mask
103 #define icmp_data icmp_dun.id_data
104 };
105
106 /*
107 * Lower bounds on packet lengths for various types.
108 * For the error advice packets must first insure that the
109 * packet is large enought to contain the returned ip header.
110 * Only then can we do the check to see if 64 bits of packet
111 * data have been returned, since we need to check the returned
112 * ip header length.
113 */
114 #define ICMP_MINLEN 8 /* abs minimum */
115 #define ICMP_TSLEN (8 + 3 * sizeof (n_time)) /* timestamp */
116 #define ICMP_MASKLEN 12 /* address mask */
117 #define ICMP_ADVLENMIN (8 + sizeof (struct ip) + 8) /* min */
118 #define ICMP_ADVLEN(p) (8 + ((p)->icmp_ip.ip_hl << 2) + 8)
119 /* N.B.: must separately check that ip_hl >= 5 */
120
121 /*
122 * Definition of type and code field values.
123 */
124 #define ICMP_ECHOREPLY 0 /* echo reply */
125 #define ICMP_UNREACH 3 /* dest unreachable, codes: */
126 #define ICMP_UNREACH_NET 0 /* bad net */
127 #define ICMP_UNREACH_HOST 1 /* bad host */
128 #define ICMP_UNREACH_PROTOCOL 2 /* bad protocol */
129 #define ICMP_UNREACH_PORT 3 /* bad port */
130 #define ICMP_UNREACH_NEEDFRAG 4 /* IP_DF caused drop */
131 #define ICMP_UNREACH_SRCFAIL 5 /* src route failed */
132 #define ICMP_UNREACH_NET_UNKNOWN 6 /* unknown net */
133 #define ICMP_UNREACH_HOST_UNKNOWN 7 /* unknown host */
134 #define ICMP_UNREACH_ISOLATED 8 /* src host isolated */
135 #define ICMP_UNREACH_NET_PROHIB 9 /* prohibited access */
136 #define ICMP_UNREACH_HOST_PROHIB 10 /* ditto */
137 #define ICMP_UNREACH_TOSNET 11 /* bad tos for net */
138 #define ICMP_UNREACH_TOSHOST 12 /* bad tos for host */
139 #define ICMP_SOURCEQUENCH 4 /* packet lost, slow down */
140 #define ICMP_REDIRECT 5 /* shorter route, codes: */
141 #define ICMP_REDIRECT_NET 0 /* for network */
142 #define ICMP_REDIRECT_HOST 1 /* for host */
143 #define ICMP_REDIRECT_TOSNET 2 /* for tos and net */
144 #define ICMP_REDIRECT_TOSHOST 3 /* for tos and host */
145 #define ICMP_ECHO 8 /* echo service */
146 #define ICMP_ROUTERADVERT 9 /* router advertisement */
147 #define ICMP_ROUTERSOLICIT 10 /* router solicitation */
148 #define ICMP_TIMXCEED 11 /* time exceeded, code: */
149 #define ICMP_TIMXCEED_INTRANS 0 /* ttl==0 in transit */
150 #define ICMP_TIMXCEED_REASS 1 /* ttl==0 in reass */
151 #define ICMP_PARAMPROB 12 /* ip header bad */
152 #define ICMP_PARAMPROB_OPTABSENT 1 /* req. opt. absent */
153 #define ICMP_TSTAMP 13 /* timestamp request */
154 #define ICMP_TSTAMPREPLY 14 /* timestamp reply */
155 #define ICMP_IREQ 15 /* information request */
156 #define ICMP_IREQREPLY 16 /* information reply */
157 #define ICMP_MASKREQ 17 /* address mask request */
158 #define ICMP_MASKREPLY 18 /* address mask reply */
159
160 #define ICMP_MAXTYPE 18
161
162 #define ICMP_INFOTYPE(type) \
163 ((type) == ICMP_ECHOREPLY || (type) == ICMP_ECHO || \
164 (type) == ICMP_ROUTERADVERT || (type) == ICMP_ROUTERSOLICIT || \
165 (type) == ICMP_TSTAMP || (type) == ICMP_TSTAMPREPLY || \
166 (type) == ICMP_IREQ || (type) == ICMP_IREQREPLY || \
167 (type) == ICMP_MASKREQ || (type) == ICMP_MASKREPLY)
168 /* rfc1700 */
169 #ifndef ICMP_UNREACH_NET_UNKNOWN
170 #define ICMP_UNREACH_NET_UNKNOWN 6 /* destination net unknown */
171 #endif
172 #ifndef ICMP_UNREACH_HOST_UNKNOWN
173 #define ICMP_UNREACH_HOST_UNKNOWN 7 /* destination host unknown */
174 #endif
175 #ifndef ICMP_UNREACH_ISOLATED
176 #define ICMP_UNREACH_ISOLATED 8 /* source host isolated */
177 #endif
178 #ifndef ICMP_UNREACH_NET_PROHIB
179 #define ICMP_UNREACH_NET_PROHIB 9 /* admin prohibited net */
180 #endif
181 #ifndef ICMP_UNREACH_HOST_PROHIB
182 #define ICMP_UNREACH_HOST_PROHIB 10 /* admin prohibited host */
183 #endif
184 #ifndef ICMP_UNREACH_TOSNET
185 #define ICMP_UNREACH_TOSNET 11 /* tos prohibited net */
186 #endif
187 #ifndef ICMP_UNREACH_TOSHOST
188 #define ICMP_UNREACH_TOSHOST 12 /* tos prohibited host */
189 #endif
190
191 /* rfc1716 */
192 #ifndef ICMP_UNREACH_FILTER_PROHIB
193 #define ICMP_UNREACH_FILTER_PROHIB 13 /* admin prohibited filter */
194 #endif
195 #ifndef ICMP_UNREACH_HOST_PRECEDENCE
196 #define ICMP_UNREACH_HOST_PRECEDENCE 14 /* host precedence violation */
197 #endif
198 #ifndef ICMP_UNREACH_PRECEDENCE_CUTOFF
199 #define ICMP_UNREACH_PRECEDENCE_CUTOFF 15 /* precedence cutoff */
200 #endif
201
202 /* Most of the icmp types */
203 static struct tok icmp2str[] = {
204 { ICMP_ECHOREPLY, "echo reply" },
205 { ICMP_SOURCEQUENCH, "source quench" },
206 { ICMP_ECHO, "echo request" },
207 { ICMP_ROUTERSOLICIT, "router solicitation" },
208 { ICMP_TSTAMP, "time stamp request" },
209 { ICMP_TSTAMPREPLY, "time stamp reply" },
210 { ICMP_IREQ, "information request" },
211 { ICMP_IREQREPLY, "information reply" },
212 { ICMP_MASKREQ, "address mask request" },
213 { 0, NULL }
214 };
215
216 /* Formats for most of the ICMP_UNREACH codes */
217 static struct tok unreach2str[] = {
218 { ICMP_UNREACH_NET, "net %s unreachable" },
219 { ICMP_UNREACH_HOST, "host %s unreachable" },
220 { ICMP_UNREACH_SRCFAIL,
221 "%s unreachable - source route failed" },
222 { ICMP_UNREACH_NET_UNKNOWN, "net %s unreachable - unknown" },
223 { ICMP_UNREACH_HOST_UNKNOWN, "host %s unreachable - unknown" },
224 { ICMP_UNREACH_ISOLATED,
225 "%s unreachable - source host isolated" },
226 { ICMP_UNREACH_NET_PROHIB,
227 "net %s unreachable - admin prohibited" },
228 { ICMP_UNREACH_HOST_PROHIB,
229 "host %s unreachable - admin prohibited" },
230 { ICMP_UNREACH_TOSNET,
231 "net %s unreachable - tos prohibited" },
232 { ICMP_UNREACH_TOSHOST,
233 "host %s unreachable - tos prohibited" },
234 { ICMP_UNREACH_FILTER_PROHIB,
235 "host %s unreachable - admin prohibited filter" },
236 { ICMP_UNREACH_HOST_PRECEDENCE,
237 "host %s unreachable - host precedence violation" },
238 { ICMP_UNREACH_PRECEDENCE_CUTOFF,
239 "host %s unreachable - precedence cutoff" },
240 { 0, NULL }
241 };
242
243 /* Formats for the ICMP_REDIRECT codes */
244 static struct tok type2str[] = {
245 { ICMP_REDIRECT_NET, "redirect %s to net %s" },
246 { ICMP_REDIRECT_HOST, "redirect %s to host %s" },
247 { ICMP_REDIRECT_TOSNET, "redirect-tos %s to net %s" },
248 { ICMP_REDIRECT_TOSHOST, "redirect-tos %s to net %s" },
249 { 0, NULL }
250 };
251
252 /* rfc1191 */
253 struct mtu_discovery {
254 short unused;
255 short nexthopmtu;
256 };
257
258 /* rfc1256 */
259 struct ih_rdiscovery {
260 u_char ird_addrnum;
261 u_char ird_addrsiz;
262 u_short ird_lifetime;
263 };
264
265 struct id_rdiscovery {
266 u_int32_t ird_addr;
267 u_int32_t ird_pref;
268 };
269
270 void
271 icmp_print(register const u_char *bp, u_int plen, register const u_char *bp2)
272 {
273 register char *cp;
274 register const struct icmp *dp;
275 register const struct ip *ip;
276 register const char *str, *fmt;
277 register const struct ip *oip;
278 register const struct udphdr *ouh;
279 register u_int hlen, dport, mtu;
280 char buf[256];
281
282 dp = (struct icmp *)bp;
283 ip = (struct ip *)bp2;
284 str = buf;
285
286 #if 0
287 (void)printf("%s > %s: ",
288 ipaddr_string(&ip->ip_src),
289 ipaddr_string(&ip->ip_dst));
290 #endif
291
292 TCHECK(dp->icmp_code);
293 switch (dp->icmp_type) {
294
295 case ICMP_UNREACH:
296 TCHECK(dp->icmp_ip.ip_dst);
297 switch (dp->icmp_code) {
298
299 case ICMP_UNREACH_PROTOCOL:
300 TCHECK(dp->icmp_ip.ip_p);
301 (void)snprintf(buf, sizeof(buf),
302 "%s protocol %d unreachable",
303 ipaddr_string(&dp->icmp_ip.ip_dst),
304 dp->icmp_ip.ip_p);
305 break;
306
307 case ICMP_UNREACH_PORT:
308 TCHECK(dp->icmp_ip.ip_p);
309 oip = &dp->icmp_ip;
310 hlen = oip->ip_hl * 4;
311 ouh = (struct udphdr *)(((u_char *)oip) + hlen);
312 dport = ntohs(ouh->uh_dport);
313 switch (oip->ip_p) {
314
315 case IPPROTO_TCP:
316 (void)snprintf(buf, sizeof(buf),
317 "%s tcp port %s unreachable",
318 ipaddr_string(&oip->ip_dst),
319 tcpport_string(dport));
320 break;
321
322 case IPPROTO_UDP:
323 (void)snprintf(buf, sizeof(buf),
324 "%s udp port %s unreachable",
325 ipaddr_string(&oip->ip_dst),
326 udpport_string(dport));
327 break;
328
329 default:
330 (void)snprintf(buf, sizeof(buf),
331 "%s protocol %d port %d unreachable",
332 ipaddr_string(&oip->ip_dst),
333 oip->ip_p, dport);
334 break;
335 }
336 break;
337
338 case ICMP_UNREACH_NEEDFRAG:
339 {
340 register const struct mtu_discovery *mp;
341 mp = (struct mtu_discovery *)&dp->icmp_void;
342 mtu = EXTRACT_16BITS(&mp->nexthopmtu);
343 if (mtu) {
344 (void)snprintf(buf, sizeof(buf),
345 "%s unreachable - need to frag (mtu %d)",
346 ipaddr_string(&dp->icmp_ip.ip_dst), mtu);
347 } else {
348 (void)snprintf(buf, sizeof(buf),
349 "%s unreachable - need to frag",
350 ipaddr_string(&dp->icmp_ip.ip_dst));
351 }
352 }
353 break;
354
355 default:
356 fmt = tok2str(unreach2str, "#%d %%s unreachable",
357 dp->icmp_code);
358 (void)snprintf(buf, sizeof(buf), fmt,
359 ipaddr_string(&dp->icmp_ip.ip_dst));
360 break;
361 }
362 break;
363
364 case ICMP_REDIRECT:
365 TCHECK(dp->icmp_ip.ip_dst);
366 fmt = tok2str(type2str, "redirect-#%d %%s to net %%s",
367 dp->icmp_code);
368 (void)snprintf(buf, sizeof(buf), fmt,
369 ipaddr_string(&dp->icmp_ip.ip_dst),
370 ipaddr_string(&dp->icmp_gwaddr));
371 break;
372
373 case ICMP_ROUTERADVERT:
374 {
375 register const struct ih_rdiscovery *ihp;
376 register const struct id_rdiscovery *idp;
377 u_int lifetime, num, size;
378
379 (void)snprintf(buf, sizeof(buf), "router advertisement");
380 cp = buf + strlen(buf);
381
382 ihp = (struct ih_rdiscovery *)&dp->icmp_void;
383 TCHECK(*ihp);
384 (void)strncpy(cp, " lifetime ", sizeof(buf) - (cp - buf));
385 cp = buf + strlen(buf);
386 lifetime = EXTRACT_16BITS(&ihp->ird_lifetime);
387 if (lifetime < 60) {
388 (void)snprintf(cp, sizeof(buf) - (cp - buf), "%u",
389 lifetime);
390 } else if (lifetime < 60 * 60) {
391 (void)snprintf(cp, sizeof(buf) - (cp - buf), "%u:%02u",
392 lifetime / 60, lifetime % 60);
393 } else {
394 (void)snprintf(cp, sizeof(buf) - (cp - buf),
395 "%u:%02u:%02u",
396 lifetime / 3600,
397 (lifetime % 3600) / 60,
398 lifetime % 60);
399 }
400 cp = buf + strlen(buf);
401
402 num = ihp->ird_addrnum;
403 (void)snprintf(cp, sizeof(buf) - (cp - buf), " %d:", num);
404 cp = buf + strlen(buf);
405
406 size = ihp->ird_addrsiz;
407 if (size != 2) {
408 (void)snprintf(cp, sizeof(buf) - (cp - buf),
409 " [size %d]", size);
410 break;
411 }
412 idp = (struct id_rdiscovery *)&dp->icmp_data;
413 while (num-- > 0) {
414 TCHECK(*idp);
415 (void)snprintf(cp, sizeof(buf) - (cp - buf), " {%s %u}",
416 ipaddr_string(&idp->ird_addr),
417 EXTRACT_32BITS(&idp->ird_pref));
418 cp = buf + strlen(buf);
419 ++idp;
420 }
421 }
422 break;
423
424 case ICMP_TIMXCEED:
425 TCHECK(dp->icmp_ip.ip_dst);
426 switch (dp->icmp_code) {
427
428 case ICMP_TIMXCEED_INTRANS:
429 str = "time exceeded in-transit";
430 break;
431
432 case ICMP_TIMXCEED_REASS:
433 str = "ip reassembly time exceeded";
434 break;
435
436 default:
437 (void)snprintf(buf, sizeof(buf), "time exceeded-#%d",
438 dp->icmp_code);
439 break;
440 }
441 break;
442
443 case ICMP_PARAMPROB:
444 if (dp->icmp_code)
445 (void)snprintf(buf, sizeof(buf),
446 "parameter problem - code %d", dp->icmp_code);
447 else {
448 TCHECK(dp->icmp_pptr);
449 (void)snprintf(buf, sizeof(buf),
450 "parameter problem - octet %d", dp->icmp_pptr);
451 }
452 break;
453
454 case ICMP_MASKREPLY:
455 TCHECK(dp->icmp_mask);
456 (void)snprintf(buf, sizeof(buf), "address mask is 0x%08x",
457 (unsigned)ntohl(dp->icmp_mask));
458 break;
459
460 case ICMP_TSTAMP:
461 TCHECK(dp->icmp_seq);
462 (void)sprintf(buf, "time stamp query id %u seq %u",
463 (unsigned)ntohs(dp->icmp_id),
464 (unsigned)ntohs(dp->icmp_seq));
465 break;
466
467 case ICMP_TSTAMPREPLY:
468 TCHECK(dp->icmp_ttime);
469 (void)sprintf(buf, "time stamp reply id %u seq %u : org 0x%lx recv 0x%lx xmit 0x%lx",
470 (unsigned)ntohs(dp->icmp_id),
471 (unsigned)ntohs(dp->icmp_seq),
472 (unsigned long)ntohl(dp->icmp_otime),
473 (unsigned long)ntohl(dp->icmp_rtime),
474 (unsigned long)ntohl(dp->icmp_ttime));
475 break;
476
477 default:
478 str = tok2str(icmp2str, "type-#%d", dp->icmp_type);
479 break;
480 }
481 (void)printf("icmp: %s", str);
482 if (vflag) {
483 if (TTEST2(*bp, plen)) {
484 if (in_cksum((u_short*)dp, plen, 0))
485 printf(" (wrong icmp csum)");
486 }
487 }
488 if (vflag > 1 && !ICMP_INFOTYPE(dp->icmp_type)) {
489 bp += 8;
490 (void)printf(" for ");
491 ip = (struct ip *)bp;
492 snaplen = snapend - bp;
493 ip_print(bp, ntohs(ip->ip_len));
494 }
495 return;
496 trunc:
497 fputs("[|icmp]", stdout);
498 }