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