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