print-ip.c (56896) | print-ip.c (75118) |
---|---|
1/* 2 * Copyright (c) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997 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 * | 1/* 2 * Copyright (c) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997 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-ip.c 56896 2000-01-30 01:05:24Z fenner $ | 21 * $FreeBSD: head/contrib/tcpdump/print-ip.c 75118 2001-04-03 07:50:46Z fenner $ |
22 */ 23 24#ifndef lint 25static const char rcsid[] = | 22 */ 23 24#ifndef lint 25static const char rcsid[] = |
26 "@(#) $Header: /tcpdump/master/tcpdump/print-ip.c,v 1.79 1999/12/22 06:27:21 itojun Exp $ (LBL)"; | 26 "@(#) $Header: /tcpdump/master/tcpdump/print-ip.c,v 1.92 2001/01/02 23:00:01 guy Exp $ (LBL)"; |
27#endif 28 29#ifdef HAVE_CONFIG_H 30#include "config.h" 31#endif 32 33#include <sys/param.h> 34#include <sys/time.h> 35#include <sys/socket.h> 36 37#include <netinet/in.h> | 27#endif 28 29#ifdef HAVE_CONFIG_H 30#include "config.h" 31#endif 32 33#include <sys/param.h> 34#include <sys/time.h> 35#include <sys/socket.h> 36 37#include <netinet/in.h> |
38#include <netinet/in_systm.h> 39#include <netinet/ip.h> 40#include <netinet/ip_var.h> 41#include <netinet/udp.h> 42#include <netinet/udp_var.h> 43#include <netinet/tcp.h> | |
44 | 38 |
45#ifdef HAVE_MALLOC_H 46#include <malloc.h> 47#endif | |
48#include <stdio.h> 49#include <stdlib.h> 50#include <string.h> 51#include <unistd.h> 52 53#include "addrtoname.h" 54#include "interface.h" 55#include "extract.h" /* must come after interface.h */ 56 | 39#include <stdio.h> 40#include <stdlib.h> 41#include <string.h> 42#include <unistd.h> 43 44#include "addrtoname.h" 45#include "interface.h" 46#include "extract.h" /* must come after interface.h */ 47 |
48#include "ip.h" 49 |
|
57/* Compatibility */ 58#ifndef IPPROTO_ND 59#define IPPROTO_ND 77 60#endif 61 | 50/* Compatibility */ 51#ifndef IPPROTO_ND 52#define IPPROTO_ND 77 53#endif 54 |
62#ifndef IN_CLASSD 63#define IN_CLASSD(i) (((int32_t)(i) & 0xf0000000) == 0xe0000000) 64#endif 65 66/* (following from ipmulti/mrouted/prune.h) */ 67 | |
68/* | 55/* |
69 * The packet format for a traceroute request. 70 */ 71struct tr_query { 72 u_int tr_src; /* traceroute source */ 73 u_int tr_dst; /* traceroute destination */ 74 u_int tr_raddr; /* traceroute response address */ 75 u_int tr_rttlqid; /* response ttl and qid */ 76}; 77 78#define TR_GETTTL(x) (int)(((x) >> 24) & 0xff) 79#define TR_GETQID(x) ((x) & 0x00ffffff) 80 81/* 82 * Traceroute response format. A traceroute response has a tr_query at the 83 * beginning, followed by one tr_resp for each hop taken. 84 */ 85struct tr_resp { 86 u_int tr_qarr; /* query arrival time */ 87 u_int tr_inaddr; /* incoming interface address */ 88 u_int tr_outaddr; /* outgoing interface address */ 89 u_int tr_rmtaddr; /* parent address in source tree */ 90 u_int tr_vifin; /* input packet count on interface */ 91 u_int tr_vifout; /* output packet count on interface */ 92 u_int tr_pktcnt; /* total incoming packets for src-grp */ 93 u_char tr_rproto; /* routing proto deployed on router */ 94 u_char tr_fttl; /* ttl required to forward on outvif */ 95 u_char tr_smask; /* subnet mask for src addr */ 96 u_char tr_rflags; /* forwarding error codes */ 97}; 98 99/* defs within mtrace */ 100#define TR_QUERY 1 101#define TR_RESP 2 102 103/* fields for tr_rflags (forwarding error codes) */ 104#define TR_NO_ERR 0 105#define TR_WRONG_IF 1 106#define TR_PRUNED 2 107#define TR_OPRUNED 3 108#define TR_SCOPED 4 109#define TR_NO_RTE 5 110#define TR_NO_FWD 7 111#define TR_NO_SPACE 0x81 112#define TR_OLD_ROUTER 0x82 113 114/* fields for tr_rproto (routing protocol) */ 115#define TR_PROTO_DVMRP 1 116#define TR_PROTO_MOSPF 2 117#define TR_PROTO_PIM 3 118#define TR_PROTO_CBT 4 119 120static void print_mtrace(register const u_char *bp, register u_int len) 121{ 122 register struct tr_query *tr = (struct tr_query *)(bp + 8); 123 124 printf("mtrace %lu: %s to %s reply-to %s", 125 (u_long)TR_GETQID(ntohl(tr->tr_rttlqid)), 126 ipaddr_string(&tr->tr_src), ipaddr_string(&tr->tr_dst), 127 ipaddr_string(&tr->tr_raddr)); 128 if (IN_CLASSD(ntohl(tr->tr_raddr))) 129 printf(" with-ttl %d", TR_GETTTL(ntohl(tr->tr_rttlqid))); 130} 131 132static void print_mresp(register const u_char *bp, register u_int len) 133{ 134 register struct tr_query *tr = (struct tr_query *)(bp + 8); 135 136 printf("mresp %lu: %s to %s reply-to %s", 137 (u_long)TR_GETQID(ntohl(tr->tr_rttlqid)), 138 ipaddr_string(&tr->tr_src), ipaddr_string(&tr->tr_dst), 139 ipaddr_string(&tr->tr_raddr)); 140 if (IN_CLASSD(ntohl(tr->tr_raddr))) 141 printf(" with-ttl %d", TR_GETTTL(ntohl(tr->tr_rttlqid))); 142} 143 144static void 145igmp_print(register const u_char *bp, register u_int len, 146 register const u_char *bp2) 147{ 148 register const struct ip *ip; 149 150 ip = (const struct ip *)bp2; 151 (void)printf("%s > %s: ", 152 ipaddr_string(&ip->ip_src), 153 ipaddr_string(&ip->ip_dst)); 154 155 if (qflag) { 156 (void)printf("igmp"); 157 return; 158 } 159 160 TCHECK2(bp[0], 8); 161 switch (bp[0]) { 162 case 0x11: 163 (void)printf("igmp %s query", bp[1] ? "v2" : "v1"); 164 if (bp[1] && bp[1] != 100) 165 (void)printf(" [intvl %d]", bp[1]); 166 (void)printf("igmp query"); 167 if (EXTRACT_32BITS(&bp[4])) 168 (void)printf(" [gaddr %s]", ipaddr_string(&bp[4])); 169 if (len != 8) 170 (void)printf(" [len %d]", len); 171 break; 172 case 0x12: 173 case 0x16: 174 (void)printf("igmp %s report %s", 175 (bp[0] & 0x0f) == 6 ? "v2" : "v1", 176 ipaddr_string(&bp[4])); 177 if (len != 8) 178 (void)printf(" [len %d]", len); 179 if (bp[1]) 180 (void)printf(" [b1=0x%x]", bp[1]); 181 break; 182 case 0x17: 183 (void)printf("igmp leave %s", ipaddr_string(&bp[4])); 184 if (len != 8) 185 (void)printf(" [len %d]", len); 186 if (bp[1]) 187 (void)printf(" [b1=0x%x]", bp[1]); 188 break; 189 case 0x13: 190 (void)printf("igmp dvmrp"); 191 if (len < 8) 192 (void)printf(" [len %d]", len); 193 else 194 dvmrp_print(bp, len); 195 break; 196 case 0x14: 197 (void)printf("igmp pimv1"); 198 pimv1_print(bp, len); 199 break; 200 case 0x1e: 201 print_mresp(bp, len); 202 break; 203 case 0x1f: 204 print_mtrace(bp, len); 205 break; 206 default: 207 (void)printf("igmp-%d", bp[0] & 0xf); 208 if (bp[1]) 209 (void)printf(" [b1=0x%02x]", bp[1]); 210 break; 211 } 212 213 if (vflag && TTEST2(bp[0], len)) { 214 /* Check the IGMP checksum */ 215 if (in_cksum((const u_short*)bp, len, 0)) 216 printf(" bad igmp cksum %x!", EXTRACT_16BITS(&bp[2])); 217 } 218 return; 219trunc: 220 fputs("[|igmp]", stdout); 221} 222 223/* | |
224 * print the recorded route in an IP RR, LSRR or SSRR option. 225 */ 226static void 227ip_printroute(const char *type, register const u_char *cp, u_int length) 228{ 229 register u_int ptr = cp[2] - 1; 230 register u_int len; 231 --- 75 unchanged lines hidden (view full) --- 307static void 308ip_optprint(register const u_char *cp, u_int length) 309{ 310 register u_int len; 311 312 for (; length > 0; cp += len, length -= len) { 313 int tt = *cp; 314 | 56 * print the recorded route in an IP RR, LSRR or SSRR option. 57 */ 58static void 59ip_printroute(const char *type, register const u_char *cp, u_int length) 60{ 61 register u_int ptr = cp[2] - 1; 62 register u_int len; 63 --- 75 unchanged lines hidden (view full) --- 139static void 140ip_optprint(register const u_char *cp, u_int length) 141{ 142 register u_int len; 143 144 for (; length > 0; cp += len, length -= len) { 145 int tt = *cp; 146 |
315 len = (tt == IPOPT_NOP || tt == IPOPT_EOL) ? 1 : cp[1]; | 147 if (tt == IPOPT_NOP || tt == IPOPT_EOL) 148 len = 1; 149 else { 150 if (&cp[1] >= snapend) { 151 printf("[|ip]"); 152 return; 153 } 154 len = cp[1]; 155 } |
316 if (len <= 0) { 317 printf("[|ip op len %d]", len); 318 return; 319 } 320 if (&cp[1] >= snapend || cp + len > snapend) { 321 printf("[|ip]"); 322 return; 323 } --- 126 unchanged lines hidden (view full) --- 450 if ((u_char *)(ip + 1) > snapend) { 451 printf("[|ip]"); 452 return; 453 } 454 if (length < sizeof (struct ip)) { 455 (void)printf("truncated-ip %d", length); 456 return; 457 } | 156 if (len <= 0) { 157 printf("[|ip op len %d]", len); 158 return; 159 } 160 if (&cp[1] >= snapend || cp + len > snapend) { 161 printf("[|ip]"); 162 return; 163 } --- 126 unchanged lines hidden (view full) --- 290 if ((u_char *)(ip + 1) > snapend) { 291 printf("[|ip]"); 292 return; 293 } 294 if (length < sizeof (struct ip)) { 295 (void)printf("truncated-ip %d", length); 296 return; 297 } |
458 hlen = ip->ip_hl * 4; | 298 hlen = IP_HL(ip) * 4; 299 if (hlen < sizeof (struct ip)) { 300 (void)printf("bad-hlen %d", hlen); 301 return; 302 } |
459 460 len = ntohs(ip->ip_len); 461 if (length < len) 462 (void)printf("truncated-ip - %d bytes missing!", 463 len - length); 464 len -= hlen; 465 len0 = len; 466 --- 49 unchanged lines hidden (view full) --- 516 len -= advance; 517 if (enh < 0) 518 break; 519 nh = enh & 0xff; 520 goto again; 521 } 522 523 case IPPROTO_TCP: | 303 304 len = ntohs(ip->ip_len); 305 if (length < len) 306 (void)printf("truncated-ip - %d bytes missing!", 307 len - length); 308 len -= hlen; 309 len0 = len; 310 --- 49 unchanged lines hidden (view full) --- 360 len -= advance; 361 if (enh < 0) 362 break; 363 nh = enh & 0xff; 364 goto again; 365 } 366 367 case IPPROTO_TCP: |
524 tcp_print(cp, len, (const u_char *)ip); | 368 tcp_print(cp, len, (const u_char *)ip, (off &~ 0x6000)); |
525 break; 526 527 case IPPROTO_UDP: | 369 break; 370 371 case IPPROTO_UDP: |
528 udp_print(cp, len, (const u_char *)ip); | 372 udp_print(cp, len, (const u_char *)ip, (off &~ 0x6000)); |
529 break; 530 531 case IPPROTO_ICMP: 532 icmp_print(cp, len, (const u_char *)ip); 533 break; 534 535#ifndef IPPROTO_IGRP 536#define IPPROTO_IGRP 9 --- 97 unchanged lines hidden (view full) --- 634 635#ifndef IPPROTO_PIM 636#define IPPROTO_PIM 103 637#endif 638 case IPPROTO_PIM: 639 pim_print(cp, len); 640 break; 641 | 373 break; 374 375 case IPPROTO_ICMP: 376 icmp_print(cp, len, (const u_char *)ip); 377 break; 378 379#ifndef IPPROTO_IGRP 380#define IPPROTO_IGRP 9 --- 97 unchanged lines hidden (view full) --- 478 479#ifndef IPPROTO_PIM 480#define IPPROTO_PIM 103 481#endif 482 case IPPROTO_PIM: 483 pim_print(cp, len); 484 break; 485 |
486#ifndef IPPROTO_VRRP 487#define IPPROTO_VRRP 112 488#endif 489 case IPPROTO_VRRP: 490 if (vflag) 491 (void)printf("vrrp %s > %s: ", 492 ipaddr_string(&ip->ip_src), 493 ipaddr_string(&ip->ip_dst)); 494 vrrp_print(cp, len, ip->ip_ttl); 495 break; 496 |
|
642 default: 643#if 0 644 (void)printf("%s > %s:", ipaddr_string(&ip->ip_src), 645 ipaddr_string(&ip->ip_dst)); 646#endif 647 (void)printf(" ip-proto-%d %d", nh, len); 648 break; 649 } --- 53 unchanged lines hidden (view full) --- 703 if (ip->ip_ttl > 1) { 704 (void)printf("%sttl %d", sep, (int)ip->ip_ttl); 705 sep = ", "; 706 } 707 if ((off & 0x3fff) == 0) { 708 (void)printf("%sid %d", sep, (int)ntohs(ip->ip_id)); 709 sep = ", "; 710 } | 497 default: 498#if 0 499 (void)printf("%s > %s:", ipaddr_string(&ip->ip_src), 500 ipaddr_string(&ip->ip_dst)); 501#endif 502 (void)printf(" ip-proto-%d %d", nh, len); 503 break; 504 } --- 53 unchanged lines hidden (view full) --- 558 if (ip->ip_ttl > 1) { 559 (void)printf("%sttl %d", sep, (int)ip->ip_ttl); 560 sep = ", "; 561 } 562 if ((off & 0x3fff) == 0) { 563 (void)printf("%sid %d", sep, (int)ntohs(ip->ip_id)); 564 sep = ", "; 565 } |
566 (void)printf("%slen %d", sep, (int)ntohs(ip->ip_len)); 567 sep = ", "; |
|
711 if ((u_char *)ip + hlen <= snapend) { 712 sum = in_cksum((const u_short *)ip, hlen, 0); 713 if (sum != 0) { 714 (void)printf("%sbad cksum %x!", sep, 715 ntohs(ip->ip_sum)); 716 sep = ", "; 717 } 718 } 719 if ((hlen -= sizeof(struct ip)) > 0) { 720 (void)printf("%soptlen=%d", sep, hlen); 721 ip_optprint((u_char *)(ip + 1), hlen); 722 } 723 printf(")"); 724 } 725} | 568 if ((u_char *)ip + hlen <= snapend) { 569 sum = in_cksum((const u_short *)ip, hlen, 0); 570 if (sum != 0) { 571 (void)printf("%sbad cksum %x!", sep, 572 ntohs(ip->ip_sum)); 573 sep = ", "; 574 } 575 } 576 if ((hlen -= sizeof(struct ip)) > 0) { 577 (void)printf("%soptlen=%d", sep, hlen); 578 ip_optprint((u_char *)(ip + 1), hlen); 579 } 580 printf(")"); 581 } 582} |
583 584void 585ipN_print(register const u_char *bp, register u_int length) 586{ 587 struct ip *ip, hdr; 588 589 ip = (struct ip *)bp; 590 if (length < 4) { 591 (void)printf("truncated-ip %d", length); 592 return; 593 } 594 memcpy (&hdr, (char *)ip, 4); 595 switch (IP_V(&hdr)) { 596 case 4: 597 ip_print (bp, length); 598 return; 599#ifdef INET6 600 case 6: 601 ip6_print (bp, length); 602 return; 603#endif 604 default: 605 (void)printf("unknown ip %d", IP_V(&hdr)); 606 return; 607 } 608} |
|