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 --- 4 unchanged lines hidden (view full) --- 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 * $FreeBSD: head/contrib/tcpdump/print-icmp.c 127675 2004-03-31 14:57:24Z bms $ |
22 */ 23 24#ifndef lint |
25static const char rcsid[] _U_ = 26 "@(#) $Header: /tcpdump/master/tcpdump/print-icmp.c,v 1.73.2.3 2004/03/24 00:56:34 guy Exp $ (LBL)"; |
27#endif 28 29#ifdef HAVE_CONFIG_H 30#include "config.h" 31#endif 32 |
33#include <tcpdump-stdinc.h> |
34 |
35#include <stdio.h> 36#include <string.h> |
37 38#include "interface.h" 39#include "addrtoname.h" 40#include "extract.h" /* must come after interface.h */ 41 42#include "ip.h" 43#include "udp.h" |
44#include "ipproto.h" |
45 46/* 47 * Interface Control Message Protocol Definitions. 48 * Per RFC 792, September 1981. 49 */ 50 51/* 52 * Structure of an icmp header. --- 8 unchanged lines hidden (view full) --- 61 struct ih_idseq { 62 u_int16_t icd_id; 63 u_int16_t icd_seq; 64 } ih_idseq; 65 u_int32_t ih_void; 66 67 /* ICMP_UNREACH_NEEDFRAG -- Path MTU Discovery (RFC1191) */ 68 struct ih_pmtu { |
69 u_int16_t ipm_void; |
70 u_int16_t ipm_nextmtu; 71 } ih_pmtu; 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 --- 180 unchanged lines hidden (view full) --- 258}; 259 260struct id_rdiscovery { 261 u_int32_t ird_addr; 262 u_int32_t ird_pref; 263}; 264 265void |
266icmp_print(const u_char *bp, u_int plen, const u_char *bp2, int fragmented) |
267{ 268 char *cp; 269 const struct icmp *dp; 270 const struct ip *ip; 271 const char *str, *fmt; 272 const struct ip *oip; 273 const struct udphdr *ouh; 274 u_int hlen, dport, mtu; 275 char buf[MAXHOSTNAMELEN + 100]; 276 277 dp = (struct icmp *)bp; 278 ip = (struct ip *)bp2; 279 str = buf; 280 281 TCHECK(dp->icmp_code); 282 switch (dp->icmp_type) { 283 |
284 case ICMP_ECHO: 285 case ICMP_ECHOREPLY: 286 TCHECK(dp->icmp_seq); 287 (void)snprintf(buf, sizeof(buf), "echo %s seq %u", 288 dp->icmp_type == ICMP_ECHO ? 289 "request" : "reply", 290 EXTRACT_16BITS(&dp->icmp_seq)); 291 break; 292 |
293 case ICMP_UNREACH: 294 TCHECK(dp->icmp_ip.ip_dst); 295 switch (dp->icmp_code) { 296 297 case ICMP_UNREACH_PROTOCOL: 298 TCHECK(dp->icmp_ip.ip_p); 299 (void)snprintf(buf, sizeof(buf), 300 "%s protocol %d unreachable", 301 ipaddr_string(&dp->icmp_ip.ip_dst), 302 dp->icmp_ip.ip_p); 303 break; 304 305 case ICMP_UNREACH_PORT: 306 TCHECK(dp->icmp_ip.ip_p); 307 oip = &dp->icmp_ip; 308 hlen = IP_HL(oip) * 4; 309 ouh = (struct udphdr *)(((u_char *)oip) + hlen); |
310 TCHECK(ouh->uh_dport); 311 dport = EXTRACT_16BITS(&ouh->uh_dport); |
312 switch (oip->ip_p) { 313 314 case IPPROTO_TCP: 315 (void)snprintf(buf, sizeof(buf), 316 "%s tcp port %s unreachable", 317 ipaddr_string(&oip->ip_dst), 318 tcpport_string(dport)); 319 break; --- 128 unchanged lines hidden (view full) --- 448 (void)snprintf(buf, sizeof(buf), 449 "parameter problem - octet %d", dp->icmp_pptr); 450 } 451 break; 452 453 case ICMP_MASKREPLY: 454 TCHECK(dp->icmp_mask); 455 (void)snprintf(buf, sizeof(buf), "address mask is 0x%08x", |
456 EXTRACT_32BITS(&dp->icmp_mask)); |
457 break; 458 459 case ICMP_TSTAMP: 460 TCHECK(dp->icmp_seq); 461 (void)snprintf(buf, sizeof(buf), 462 "time stamp query id %u seq %u", |
463 EXTRACT_16BITS(&dp->icmp_id), 464 EXTRACT_16BITS(&dp->icmp_seq)); |
465 break; 466 467 case ICMP_TSTAMPREPLY: 468 TCHECK(dp->icmp_ttime); 469 (void)snprintf(buf, sizeof(buf), |
470 "time stamp reply id %u seq %u : org 0x%x recv 0x%x xmit 0x%x", 471 EXTRACT_16BITS(&dp->icmp_id), 472 EXTRACT_16BITS(&dp->icmp_seq), 473 EXTRACT_32BITS(&dp->icmp_otime), 474 EXTRACT_32BITS(&dp->icmp_rtime), 475 EXTRACT_32BITS(&dp->icmp_ttime)); |
476 break; 477 478 default: 479 str = tok2str(icmp2str, "type-#%d", dp->icmp_type); 480 break; 481 } |
482 (void)printf("icmp %d: %s", plen, str); 483 if (vflag && !fragmented) { /* don't attempt checksumming if this is a frag */ 484 u_int16_t sum, icmp_sum; |
485 if (TTEST2(*bp, plen)) { |
486 sum = in_cksum((u_short*)dp, plen, 0); 487 if (sum != 0) { 488 icmp_sum = EXTRACT_16BITS(&dp->icmp_cksum); 489 (void)printf(" (wrong icmp cksum %x (->%x)!)", 490 icmp_sum, 491 in_cksum_shouldbe(icmp_sum, sum)); 492 } |
493 } 494 } |
495 if (vflag > 1 && !ICMP_INFOTYPE(dp->icmp_type)) { 496 bp += 8; 497 (void)printf(" for "); 498 ip = (struct ip *)bp; 499 snaplen = snapend - bp; 500 ip_print(bp, EXTRACT_16BITS(&ip->ip_len)); 501 } |
502 return; 503trunc: 504 fputs("[|icmp]", stdout); 505} |