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