2 * Copyright (c) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997
3 * The Regents of the University of California. All rights reserved.
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
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.
23 static const char rcsid[] _U_ =
24 "@(#) $Header: /tcpdump/master/tcpdump/print-ip.c,v 1.128.2.6 2004/03/24 09:01:39 guy Exp $ (LBL)";
31 #include <tcpdump-stdinc.h>
37 #include "addrtoname.h"
38 #include "interface.h"
39 #include "extract.h" /* must come after interface.h */
45 * print the recorded route in an IP RR, LSRR or SSRR option.
48 ip_printroute(const char *type, register const u_char *cp, u_int length)
54 printf(" [bad length %u]", length);
59 printf(" [bad length %u]", length);
61 if (ptr < 3 || ((ptr + 1) & 3) || ptr > length + 1)
62 printf(" [bad ptr %u]", cp[2]);
65 for (len = 3; len < length; len += 4) {
68 printf("%s%s", type, ipaddr_string(&cp[len]));
71 printf("%s}", ptr == len? "#" : "");
75 * If source-routing is present, return the final destination.
76 * Otherwise, return IP destination.
78 * This is used for UDP and TCP pseudo-header in the checksum
82 ip_finddst(const struct ip *ip)
89 cp = (const u_char *)(ip + 1);
90 length = (IP_HL(ip) << 2) - sizeof(struct ip);
92 for (; length > 0; cp += len, length -= len) {
97 if (tt == IPOPT_NOP || tt == IPOPT_EOL)
113 memcpy(&retval, cp + len - 4, 4);
117 return ip->ip_dst.s_addr;
124 ip_printts(register const u_char *cp, u_int length)
132 printf("[bad length %d]", length);
136 hoplen = ((cp[3]&0xF) != IPOPT_TS_TSONLY) ? 8 : 4;
137 if ((length - 4) & (hoplen-1))
138 printf("[bad length %d]", length);
141 if (ptr < 4 || ((ptr - 4) & (hoplen-1)) || ptr > length + 1)
142 printf("[bad ptr %d]", cp[2]);
144 case IPOPT_TS_TSONLY:
147 case IPOPT_TS_TSANDADDR:
151 * prespecified should really be 3, but some ones might send 2
152 * instead, and the IPOPT_TS_PRESPEC constant can apparently
153 * have both values, so we have to hard-code it here.
157 printf("PRESPEC2.0");
159 case 3: /* IPOPT_TS_PRESPEC */
163 printf("[bad ts type %d]", cp[3]&0xF);
168 for (len = 4; len < length; len += hoplen) {
171 printf("%s%d@%s", type, EXTRACT_32BITS(&cp[len+hoplen-4]),
172 hoplen!=8 ? "" : ipaddr_string(&cp[len]));
177 printf("%s", ptr == len ? " ^ " : "");
180 printf(" [%d hops not recorded]} ", cp[3]>>4);
189 ip_optprint(register const u_char *cp, u_int length)
193 for (; length > 0; cp += len, length -= len) {
198 if (tt == IPOPT_NOP || tt == IPOPT_EOL)
204 printf("[|ip op len %d]", len);
214 printf("-%d", length - 1);
225 #ifndef IPOPT_SECURITY
226 #define IPOPT_SECURITY 130
227 #endif /* IPOPT_SECURITY */
229 printf(" SECURITY{%d}", len);
233 ip_printroute("RR", cp, len);
237 ip_printroute("SSRR", cp, len);
241 ip_printroute("LSRR", cp, len);
245 #define IPOPT_RA 148 /* router alert */
254 printf("%d.%d", cp[2], cp[3]);
259 printf(" IPOPT-%d{%d}", cp[0], len);
270 * compute an IP header checksum.
271 * don't modifiy the packet.
274 in_cksum(const u_short *addr, register u_int len, int csum)
277 const u_short *w = addr;
282 * Our algorithm is simple, using a 32 bit accumulator (sum),
283 * we add sequential 16 bit words to it, and at the end, fold
284 * back all the carry bits from the top 16 bits into the lower
292 sum += htons(*(u_char *)w<<8);
295 * add back carry outs from top 16 bits to low 16 bits
297 sum = (sum >> 16) + (sum & 0xffff); /* add hi 16 to low 16 */
298 sum += (sum >> 16); /* add carry */
299 answer = ~sum; /* truncate to 16 bits */
304 * Given the host-byte-order value of the checksum field in a packet
305 * header, and the network-byte-order computed checksum of the data
306 * that the checksum covers (including the checksum itself), compute
307 * what the checksum field *should* have been.
310 in_cksum_shouldbe(u_int16_t sum, u_int16_t computed_sum)
315 * The value that should have gone into the checksum field
316 * is the negative of the value gotten by summing up everything
317 * *but* the checksum field.
319 * We can compute that by subtracting the value of the checksum
320 * field from the sum of all the data in the packet, and then
321 * computing the negative of that value.
323 * "sum" is the value of the checksum field, and "computed_sum"
324 * is the negative of the sum of all the data in the packets,
325 * so that's -(-computed_sum - sum), or (sum + computed_sum).
327 * All the arithmetic in question is one's complement, so the
328 * addition must include an end-around carry; we do this by
329 * doing the arithmetic in 32 bits (with no sign-extension),
330 * and then adding the upper 16 bits of the sum, which contain
331 * the carry, to the lower 16 bits of the sum, and then do it
332 * again in case *that* sum produced a carry.
334 * As RFC 1071 notes, the checksum can be computed without
335 * byte-swapping the 16-bit words; summing 16-bit words
336 * on a big-endian machine gives a big-endian checksum, which
337 * can be directly stuffed into the big-endian checksum fields
338 * in protocol headers, and summing words on a little-endian
339 * machine gives a little-endian checksum, which must be
340 * byte-swapped before being stuffed into a big-endian checksum
343 * "computed_sum" is a network-byte-order value, so we must put
344 * it in host byte order before subtracting it from the
345 * host-byte-order value from the header; the adjusted checksum
346 * will be in host byte order, which is what we'll return.
349 shouldbe += ntohs(computed_sum);
350 shouldbe = (shouldbe & 0xFFFF) + (shouldbe >> 16);
351 shouldbe = (shouldbe & 0xFFFF) + (shouldbe >> 16);
361 #define IP_RES 0x8000
363 static struct tok ip_frag_values[] = {
366 { IP_RES, "rsvd" }, /* The RFC3514 evil ;-) bit */
371 * print an IP datagram.
374 ip_print(register const u_char *bp, register u_int length)
376 register const struct ip *ip;
377 register u_int hlen, len, len0, off;
379 register const u_char *cp;
382 struct protoent *proto;
383 u_int16_t sum, ip_sum;
385 ip = (const struct ip *)bp;
386 if (IP_V(ip) != 4) { /* print version if != 4 */
387 printf("IP%u ", IP_V(ip));
389 printf(", wrong link-layer encapsulation");
394 if ((u_char *)(ip + 1) > snapend) {
398 if (length < sizeof (struct ip)) {
399 (void)printf("truncated-ip %d", length);
402 hlen = IP_HL(ip) * 4;
403 if (hlen < sizeof (struct ip)) {
404 (void)printf("bad-hlen %u", hlen);
408 len = EXTRACT_16BITS(&ip->ip_len);
410 (void)printf("truncated-ip - %u bytes missing! ",
413 (void)printf("bad-len %u", len);
418 * Cut off the snapshot length to the end of the IP payload.
427 off = EXTRACT_16BITS(&ip->ip_off);
430 (void)printf("(tos 0x%x", (int)ip->ip_tos);
432 if (ip->ip_tos & 0x03) {
433 switch (ip->ip_tos & 0x03) {
435 (void)printf(",ECT(1)");
438 (void)printf(",ECT(0)");
446 (void)printf(", ttl %3u", ip->ip_ttl);
449 * for the firewall guys, print id, offset.
450 * On all but the last stick a "+" in the flags portion.
451 * For unfragmented datagrams, note the don't fragment flag.
454 (void)printf(", id %u, offset %u, flags [%s]",
455 EXTRACT_16BITS(&ip->ip_id),
457 bittok2str(ip_frag_values, "none", off & 0xe000 ));
459 (void)printf(", length: %u", EXTRACT_16BITS(&ip->ip_len));
461 if ((hlen - sizeof(struct ip)) > 0) {
462 (void)printf(", optlength: %u (", hlen - (u_int)sizeof(struct ip));
463 ip_optprint((u_char *)(ip + 1), hlen - sizeof(struct ip));
467 if ((u_char *)ip + hlen <= snapend) {
468 sum = in_cksum((const u_short *)ip, hlen, 0);
470 ip_sum = EXTRACT_16BITS(&ip->ip_sum);
471 (void)printf(", bad cksum %x (->%x)!", ip_sum,
472 in_cksum_shouldbe(ip_sum, sum));
480 * If this is fragment zero, hand it to the next higher
483 if ((off & 0x1fff) == 0) {
484 cp = (const u_char *)ip + hlen;
487 if (nh != IPPROTO_TCP && nh != IPPROTO_UDP &&
488 nh != IPPROTO_SCTP) {
489 (void)printf("%s > %s: ", ipaddr_string(&ip->ip_src),
490 ipaddr_string(&ip->ip_dst));
497 advance = ah_print(cp);
507 advance = esp_print(cp, (const u_char *)ip, &enh, &padlen);
511 len -= advance + padlen;
519 advance = ipcomp_print(cp, &enh);
529 sctp_print(cp, (const u_char *)ip, len);
533 tcp_print(cp, len, (const u_char *)ip, (off &~ 0x6000));
537 udp_print(cp, len, (const u_char *)ip, (off &~ 0x6000));
541 /* pass on the MF bit plus the offset to detect fragments */
542 icmp_print(cp, len, (const u_char *)ip, (off & 0x3fff));
546 igrp_print(cp, len, (const u_char *)ip);
550 (void)printf(" nd %d", len);
558 ospf_print(cp, len, (const u_char *)ip);
566 /* DVMRP multicast tunnel (ip-in-ip encapsulation) */
569 printf(" (ipip-proto-4)");
576 /* ip6-in-ip encapsulation */
591 mobile_print(cp, len);
599 vrrp_print(cp, len, ip->ip_ttl);
603 if ((proto = getprotobynumber(nh)) != NULL)
604 (void)printf(" %s", proto->p_name);
606 (void)printf(" ip-proto-%d", nh);
611 /* Ultra quiet now means that all this stuff should be suppressed */
612 if (qflag > 1) return;
615 * if this isn't the first frag, we're missing the
616 * next level protocol header. print the ip addr
620 (void)printf("%s > %s:", ipaddr_string(&ip->ip_src),
621 ipaddr_string(&ip->ip_dst));
622 if ((proto = getprotobynumber(ip->ip_p)) != NULL)
623 (void)printf(" %s", proto->p_name);
625 (void)printf(" ip-proto-%d", ip->ip_p);
631 ipN_print(register const u_char *bp, register u_int length)
635 ip = (struct ip *)bp;
637 (void)printf("truncated-ip %d", length);
640 memcpy (&hdr, (char *)ip, 4);
641 switch (IP_V(&hdr)) {
643 ip_print (bp, length);
647 ip6_print (bp, length);
651 (void)printf("unknown ip %d", IP_V(&hdr));