Fix markup. Fix backslashes to surive roff.
[netbsd-mini2440.git] / dist / tcpdump / print-icmp.c
blob4168f997c2e6c8ec9276639b843b7f656d953781
1 /* $NetBSD$ */
3 /*
4 * Copyright (c) 1988, 1989, 1990, 1991, 1993, 1994, 1995, 1996
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that: (1) source code distributions
9 * retain the above copyright notice and this paragraph in its entirety, (2)
10 * distributions including binary code include the above copyright notice and
11 * this paragraph in its entirety in the documentation or other materials
12 * provided with the distribution, and (3) all advertising materials mentioning
13 * features or use of this software display the following acknowledgement:
14 * ``This product includes software developed by the University of California,
15 * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
16 * the University nor the names of its contributors may be used to endorse
17 * or promote products derived from this software without specific prior
18 * written permission.
19 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
20 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
21 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
24 #include <sys/cdefs.h>
25 #ifndef lint
26 #if 0
27 static const char rcsid[] _U_ =
28 "@(#) Header: /tcpdump/master/tcpdump/print-icmp.c,v 1.81.2.4 2006/12/12 14:33:20 hannes Exp (LBL)";
29 #else
30 __RCSID("$NetBSD: tcpdump2rcsid.ex,v 1.1 2001/06/25 20:09:58 itojun Exp $");
31 #endif
32 #endif
34 #ifdef HAVE_CONFIG_H
35 #include "config.h"
36 #endif
38 #include <tcpdump-stdinc.h>
40 #include <stdio.h>
41 #include <string.h>
43 #include "interface.h"
44 #include "addrtoname.h"
45 #include "extract.h" /* must come after interface.h */
47 #include "ip.h"
48 #include "udp.h"
49 #include "ipproto.h"
50 #include "mpls.h"
53 * Interface Control Message Protocol Definitions.
54 * Per RFC 792, September 1981.
58 * Structure of an icmp header.
60 struct icmp {
61 u_int8_t icmp_type; /* type of message, see below */
62 u_int8_t icmp_code; /* type sub code */
63 u_int16_t icmp_cksum; /* ones complement cksum of struct */
64 union {
65 u_int8_t ih_pptr; /* ICMP_PARAMPROB */
66 struct in_addr ih_gwaddr; /* ICMP_REDIRECT */
67 struct ih_idseq {
68 u_int16_t icd_id;
69 u_int16_t icd_seq;
70 } ih_idseq;
71 u_int32_t ih_void;
72 } icmp_hun;
73 #define icmp_pptr icmp_hun.ih_pptr
74 #define icmp_gwaddr icmp_hun.ih_gwaddr
75 #define icmp_id icmp_hun.ih_idseq.icd_id
76 #define icmp_seq icmp_hun.ih_idseq.icd_seq
77 #define icmp_void icmp_hun.ih_void
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
99 #define ICMP_MPLS_EXT_EXTRACT_VERSION(x) (((x)&0xf0)>>4)
100 #define ICMP_MPLS_EXT_VERSION 2
103 * Lower bounds on packet lengths for various types.
104 * For the error advice packets must first insure that the
105 * packet is large enought to contain the returned ip header.
106 * Only then can we do the check to see if 64 bits of packet
107 * data have been returned, since we need to check the returned
108 * ip header length.
110 #define ICMP_MINLEN 8 /* abs minimum */
111 #define ICMP_EXTD_MINLEN (156 - sizeof (struct ip)) /* draft-bonica-internet-icmp-08 */
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 */
119 * Definition of type and code field values.
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 */
157 #define ICMP_MAXTYPE 18
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 #define ICMP_MPLS_EXT_TYPE(type) \
166 ((type) == ICMP_UNREACH || \
167 (type) == ICMP_TIMXCEED || \
168 (type) == ICMP_PARAMPROB)
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
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
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 }
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 }
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 host %s" },
250 { 0, NULL }
253 /* rfc1191 */
254 struct mtu_discovery {
255 u_int16_t unused;
256 u_int16_t nexthopmtu;
259 /* rfc1256 */
260 struct ih_rdiscovery {
261 u_int8_t ird_addrnum;
262 u_int8_t ird_addrsiz;
263 u_int16_t ird_lifetime;
266 struct id_rdiscovery {
267 u_int32_t ird_addr;
268 u_int32_t ird_pref;
272 * draft-bonica-internet-icmp-08
274 * The Destination Unreachable, Time Exceeded
275 * and Parameter Problem messages are slighly changed as per
276 * the above draft. A new Length field gets added to give
277 * the caller an idea about the length of the piggypacked
278 * IP packet before the MPLS extension header starts.
280 * The Length field represents length of the padded "original datagram"
281 * field measured in 32-bit words.
283 * 0 1 2 3
284 * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
285 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
286 * | Type | Code | Checksum |
287 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
288 * | unused | Length | unused |
289 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
290 * | Internet Header + leading octets of original datagram |
291 * | |
292 * | // |
293 * | |
294 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
297 struct icmp_ext_t {
298 u_int8_t icmp_type;
299 u_int8_t icmp_code;
300 u_int8_t icmp_checksum[2];
301 u_int8_t icmp_reserved;
302 u_int8_t icmp_length;
303 u_int8_t icmp_reserved2[2];
304 u_int8_t icmp_ext_legacy_header[128]; /* extension header starts 128 bytes after ICMP header */
305 u_int8_t icmp_ext_version_res[2];
306 u_int8_t icmp_ext_checksum[2];
307 u_int8_t icmp_ext_data[1];
310 struct icmp_mpls_ext_common_header_t {
313 struct icmp_mpls_ext_object_header_t {
314 u_int8_t length[2];
315 u_int8_t class_num;
316 u_int8_t ctype;
319 static const struct tok icmp_mpls_ext_obj_values[] = {
320 { 1, "MPLS Stack Entry" },
321 { 2, "Extended Payload" },
322 { 0, NULL}
325 /* prototypes */
326 const char *icmp_tstamp_print(u_int);
328 /* print the milliseconds since midnight UTC */
329 const char *
330 icmp_tstamp_print(u_int tstamp) {
331 u_int msec,sec,min,hrs;
333 static char buf[64];
335 msec = tstamp % 1000;
336 sec = tstamp / 1000;
337 min = sec / 60; sec -= min * 60;
338 hrs = min / 60; min -= hrs * 60;
339 snprintf(buf, sizeof(buf), "%02u:%02u:%02u.%03u",hrs,min,sec,msec);
340 return buf;
343 void
344 icmp_print(const u_char *bp, u_int plen, const u_char *bp2, int fragmented)
346 char *cp;
347 const struct icmp *dp;
348 const struct icmp_ext_t *ext_dp;
349 const struct ip *ip;
350 const char *str, *fmt;
351 const struct ip *oip;
352 const struct udphdr *ouh;
353 const u_int8_t *obj_tptr;
354 u_int32_t raw_label;
355 const u_char *snapend_save;
356 const struct icmp_mpls_ext_object_header_t *icmp_mpls_ext_object_header;
357 u_int hlen, dport, mtu, obj_tlen, obj_class_num, obj_ctype;
358 char buf[MAXHOSTNAMELEN + 100];
360 dp = (struct icmp *)bp;
361 ext_dp = (struct icmp_ext_t *)bp;
362 ip = (struct ip *)bp2;
363 str = buf;
365 TCHECK(dp->icmp_code);
366 switch (dp->icmp_type) {
368 case ICMP_ECHO:
369 case ICMP_ECHOREPLY:
370 TCHECK(dp->icmp_seq);
371 (void)snprintf(buf, sizeof(buf), "echo %s, id %u, seq %u",
372 dp->icmp_type == ICMP_ECHO ?
373 "request" : "reply",
374 EXTRACT_16BITS(&dp->icmp_id),
375 EXTRACT_16BITS(&dp->icmp_seq));
376 break;
378 case ICMP_UNREACH:
379 TCHECK(dp->icmp_ip.ip_dst);
380 switch (dp->icmp_code) {
382 case ICMP_UNREACH_PROTOCOL:
383 TCHECK(dp->icmp_ip.ip_p);
384 (void)snprintf(buf, sizeof(buf),
385 "%s protocol %d unreachable",
386 ipaddr_string(&dp->icmp_ip.ip_dst),
387 dp->icmp_ip.ip_p);
388 break;
390 case ICMP_UNREACH_PORT:
391 TCHECK(dp->icmp_ip.ip_p);
392 oip = &dp->icmp_ip;
393 hlen = IP_HL(oip) * 4;
394 ouh = (struct udphdr *)(((u_char *)oip) + hlen);
395 TCHECK(ouh->uh_dport);
396 dport = EXTRACT_16BITS(&ouh->uh_dport);
397 switch (oip->ip_p) {
399 case IPPROTO_TCP:
400 (void)snprintf(buf, sizeof(buf),
401 "%s tcp port %s unreachable",
402 ipaddr_string(&oip->ip_dst),
403 tcpport_string(dport));
404 break;
406 case IPPROTO_UDP:
407 (void)snprintf(buf, sizeof(buf),
408 "%s udp port %s unreachable",
409 ipaddr_string(&oip->ip_dst),
410 udpport_string(dport));
411 break;
413 default:
414 (void)snprintf(buf, sizeof(buf),
415 "%s protocol %d port %d unreachable",
416 ipaddr_string(&oip->ip_dst),
417 oip->ip_p, dport);
418 break;
420 break;
422 case ICMP_UNREACH_NEEDFRAG:
424 register const struct mtu_discovery *mp;
425 mp = (struct mtu_discovery *)&dp->icmp_void;
426 mtu = EXTRACT_16BITS(&mp->nexthopmtu);
427 if (mtu) {
428 (void)snprintf(buf, sizeof(buf),
429 "%s unreachable - need to frag (mtu %d)",
430 ipaddr_string(&dp->icmp_ip.ip_dst), mtu);
431 } else {
432 (void)snprintf(buf, sizeof(buf),
433 "%s unreachable - need to frag",
434 ipaddr_string(&dp->icmp_ip.ip_dst));
437 break;
439 default:
440 fmt = tok2str(unreach2str, "#%d %%s unreachable",
441 dp->icmp_code);
442 (void)snprintf(buf, sizeof(buf), fmt,
443 ipaddr_string(&dp->icmp_ip.ip_dst));
444 break;
446 break;
448 case ICMP_REDIRECT:
449 TCHECK(dp->icmp_ip.ip_dst);
450 fmt = tok2str(type2str, "redirect-#%d %%s to net %%s",
451 dp->icmp_code);
452 (void)snprintf(buf, sizeof(buf), fmt,
453 ipaddr_string(&dp->icmp_ip.ip_dst),
454 ipaddr_string(&dp->icmp_gwaddr));
455 break;
457 case ICMP_ROUTERADVERT:
459 register const struct ih_rdiscovery *ihp;
460 register const struct id_rdiscovery *idp;
461 u_int lifetime, num, size;
463 (void)snprintf(buf, sizeof(buf), "router advertisement");
464 cp = buf + strlen(buf);
466 ihp = (struct ih_rdiscovery *)&dp->icmp_void;
467 TCHECK(*ihp);
468 (void)strncpy(cp, " lifetime ", sizeof(buf) - (cp - buf));
469 cp = buf + strlen(buf);
470 lifetime = EXTRACT_16BITS(&ihp->ird_lifetime);
471 if (lifetime < 60) {
472 (void)snprintf(cp, sizeof(buf) - (cp - buf), "%u",
473 lifetime);
474 } else if (lifetime < 60 * 60) {
475 (void)snprintf(cp, sizeof(buf) - (cp - buf), "%u:%02u",
476 lifetime / 60, lifetime % 60);
477 } else {
478 (void)snprintf(cp, sizeof(buf) - (cp - buf),
479 "%u:%02u:%02u",
480 lifetime / 3600,
481 (lifetime % 3600) / 60,
482 lifetime % 60);
484 cp = buf + strlen(buf);
486 num = ihp->ird_addrnum;
487 (void)snprintf(cp, sizeof(buf) - (cp - buf), " %d:", num);
488 cp = buf + strlen(buf);
490 size = ihp->ird_addrsiz;
491 if (size != 2) {
492 (void)snprintf(cp, sizeof(buf) - (cp - buf),
493 " [size %d]", size);
494 break;
496 idp = (struct id_rdiscovery *)&dp->icmp_data;
497 while (num-- > 0) {
498 TCHECK(*idp);
499 (void)snprintf(cp, sizeof(buf) - (cp - buf), " {%s %u}",
500 ipaddr_string(&idp->ird_addr),
501 EXTRACT_32BITS(&idp->ird_pref));
502 cp = buf + strlen(buf);
503 ++idp;
506 break;
508 case ICMP_TIMXCEED:
509 TCHECK(dp->icmp_ip.ip_dst);
510 switch (dp->icmp_code) {
512 case ICMP_TIMXCEED_INTRANS:
513 str = "time exceeded in-transit";
514 break;
516 case ICMP_TIMXCEED_REASS:
517 str = "ip reassembly time exceeded";
518 break;
520 default:
521 (void)snprintf(buf, sizeof(buf), "time exceeded-#%d",
522 dp->icmp_code);
523 break;
525 break;
527 case ICMP_PARAMPROB:
528 if (dp->icmp_code)
529 (void)snprintf(buf, sizeof(buf),
530 "parameter problem - code %d", dp->icmp_code);
531 else {
532 TCHECK(dp->icmp_pptr);
533 (void)snprintf(buf, sizeof(buf),
534 "parameter problem - octet %d", dp->icmp_pptr);
536 break;
538 case ICMP_MASKREPLY:
539 TCHECK(dp->icmp_mask);
540 (void)snprintf(buf, sizeof(buf), "address mask is 0x%08x",
541 EXTRACT_32BITS(&dp->icmp_mask));
542 break;
544 case ICMP_TSTAMP:
545 TCHECK(dp->icmp_seq);
546 (void)snprintf(buf, sizeof(buf),
547 "time stamp query id %u seq %u",
548 EXTRACT_16BITS(&dp->icmp_id),
549 EXTRACT_16BITS(&dp->icmp_seq));
550 break;
552 case ICMP_TSTAMPREPLY:
553 TCHECK(dp->icmp_ttime);
554 (void)snprintf(buf, sizeof(buf),
555 "time stamp reply id %u seq %u: org %s",
556 EXTRACT_16BITS(&dp->icmp_id),
557 EXTRACT_16BITS(&dp->icmp_seq),
558 icmp_tstamp_print(EXTRACT_32BITS(&dp->icmp_otime)));
560 (void)snprintf(buf+strlen(buf),sizeof(buf)-strlen(buf),", recv %s",
561 icmp_tstamp_print(EXTRACT_32BITS(&dp->icmp_rtime)));
562 (void)snprintf(buf+strlen(buf),sizeof(buf)-strlen(buf),", xmit %s",
563 icmp_tstamp_print(EXTRACT_32BITS(&dp->icmp_ttime)));
564 break;
566 default:
567 str = tok2str(icmp2str, "type-#%d", dp->icmp_type);
568 break;
570 (void)printf("ICMP %s, length %u", str, plen);
571 if (vflag && !fragmented) { /* don't attempt checksumming if this is a frag */
572 u_int16_t sum, icmp_sum;
573 if (TTEST2(*bp, plen)) {
574 sum = in_cksum((u_short*)dp, plen, 0);
575 if (sum != 0) {
576 icmp_sum = EXTRACT_16BITS(&dp->icmp_cksum);
577 (void)printf(" (wrong icmp cksum %x (->%x)!)",
578 icmp_sum,
579 in_cksum_shouldbe(icmp_sum, sum));
585 * print the remnants of the IP packet.
586 * save the snaplength as this may get overidden in the IP printer.
588 if (vflag >= 1 && !ICMP_INFOTYPE(dp->icmp_type)) {
589 bp += 8;
590 (void)printf("\n\t");
591 ip = (struct ip *)bp;
592 snaplen = snapend - bp;
593 snapend_save = snapend;
594 ip_print(gndo, bp, EXTRACT_16BITS(&ip->ip_len));
595 snapend = snapend_save;
599 * Attempt to decode the MPLS extensions only for some ICMP types.
601 if (vflag >= 1 && plen > ICMP_EXTD_MINLEN && ICMP_MPLS_EXT_TYPE(dp->icmp_type)) {
603 TCHECK(*ext_dp);
606 * Check first if the mpls extension header shows a non-zero length.
607 * If the length field is not set then silently verify the checksum
608 * to check if an extension header is present. This is expedient,
609 * however not all implementations set the length field proper.
611 if (!ext_dp->icmp_length &&
612 in_cksum((const u_short *)&ext_dp->icmp_ext_version_res,
613 plen - ICMP_EXTD_MINLEN, 0)) {
614 return;
617 printf("\n\tMPLS extension v%u",
618 ICMP_MPLS_EXT_EXTRACT_VERSION(*(ext_dp->icmp_ext_version_res)));
621 * Sanity checking of the header.
623 if (ICMP_MPLS_EXT_EXTRACT_VERSION(*(ext_dp->icmp_ext_version_res)) !=
624 ICMP_MPLS_EXT_VERSION) {
625 printf(" packet not supported");
626 return;
629 hlen = plen - ICMP_EXTD_MINLEN;
630 printf(", checksum 0x%04x (%scorrect), length %u",
631 EXTRACT_16BITS(ext_dp->icmp_ext_checksum),
632 in_cksum((const u_short *)&ext_dp->icmp_ext_version_res,
633 plen - ICMP_EXTD_MINLEN, 0) ? "in" : "",
634 hlen);
636 hlen -= 4; /* subtract common header size */
637 obj_tptr = (u_int8_t *)ext_dp->icmp_ext_data;
639 while (hlen > sizeof(struct icmp_mpls_ext_object_header_t)) {
641 icmp_mpls_ext_object_header = (struct icmp_mpls_ext_object_header_t *)obj_tptr;
642 TCHECK(*icmp_mpls_ext_object_header);
643 obj_tlen = EXTRACT_16BITS(icmp_mpls_ext_object_header->length);
644 obj_class_num = icmp_mpls_ext_object_header->class_num;
645 obj_ctype = icmp_mpls_ext_object_header->ctype;
646 obj_tptr += sizeof(struct icmp_mpls_ext_object_header_t);
648 printf("\n\t %s Object (%u), Class-Type: %u, length %u",
649 tok2str(icmp_mpls_ext_obj_values,"unknown",obj_class_num),
650 obj_class_num,
651 obj_ctype,
652 obj_tlen);
654 hlen-=sizeof(struct icmp_mpls_ext_object_header_t); /* length field includes tlv header */
656 /* infinite loop protection */
657 if ((obj_class_num == 0) ||
658 (obj_tlen < sizeof(struct icmp_mpls_ext_object_header_t))) {
659 return;
661 obj_tlen-=sizeof(struct icmp_mpls_ext_object_header_t);
663 switch (obj_class_num) {
664 case 1:
665 switch(obj_ctype) {
666 case 1:
667 TCHECK2(*obj_tptr, 4);
668 raw_label = EXTRACT_32BITS(obj_tptr);
669 printf("\n\t label %u, exp %u", MPLS_LABEL(raw_label), MPLS_EXP(raw_label));
670 if (MPLS_STACK(raw_label))
671 printf(", [S]");
672 printf(", ttl %u", MPLS_TTL(raw_label));
673 break;
674 default:
675 print_unknown_data(obj_tptr, "\n\t ", obj_tlen);
677 break;
680 * FIXME those are the defined objects that lack a decoder
681 * you are welcome to contribute code ;-)
683 case 2:
684 default:
685 print_unknown_data(obj_tptr, "\n\t ", obj_tlen);
686 break;
688 if (hlen < obj_tlen)
689 break;
690 hlen -= obj_tlen;
691 obj_tptr += obj_tlen;
695 return;
696 trunc:
697 fputs("[|icmp]", stdout);