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.
25 static const char rcsid[] _U_ =
26 "@(#) $Header: /tcpdump/master/tcpdump/print-ip.c,v 1.159 2007-09-14 01:29:28 guy Exp $ (LBL)";
33 #include <tcpdump-stdinc.h>
39 #include "addrtoname.h"
40 #include "interface.h"
41 #include "extract.h" /* must come after interface.h */
46 struct tok ip_option_values[] = {
49 { IPOPT_TS, "timestamp" },
50 { IPOPT_SECURITY, "security" },
52 { IPOPT_SSRR, "SSRR" },
53 { IPOPT_LSRR, "LSRR" },
55 { IPOPT_RFC1393, "traceroute" },
60 * print the recorded route in an IP RR, LSRR or SSRR option.
63 ip_printroute(register const u_char *cp, u_int length)
69 printf(" [bad length %u]", length);
73 printf(" [bad length %u]", length);
75 if (ptr < 3 || ((ptr + 1) & 3) || ptr > length + 1)
76 printf(" [bad ptr %u]", cp[2]);
78 for (len = 3; len < length; len += 4) {
79 printf(" %s", ipaddr_string(&cp[len]));
86 * If source-routing is present and valid, return the final destination.
87 * Otherwise, return IP destination.
89 * This is used for UDP and TCP pseudo-header in the checksum
93 ip_finddst(const struct ip *ip)
100 cp = (const u_char *)(ip + 1);
101 length = (IP_HL(ip) << 2) - sizeof(struct ip);
103 for (; length > 0; cp += len, length -= len) {
110 else if (tt == IPOPT_NOP)
125 memcpy(&retval, cp + len - 4, 4);
130 memcpy(&retval, &ip->ip_dst.s_addr, sizeof(u_int32_t));
135 ip_printts(register const u_char *cp, u_int length)
143 printf("[bad length %u]", length);
147 hoplen = ((cp[3]&0xF) != IPOPT_TS_TSONLY) ? 8 : 4;
148 if ((length - 4) & (hoplen-1))
149 printf("[bad length %u]", length);
152 if (ptr < 4 || ((ptr - 4) & (hoplen-1)) || ptr > length + 1)
153 printf("[bad ptr %u]", cp[2]);
155 case IPOPT_TS_TSONLY:
158 case IPOPT_TS_TSANDADDR:
162 * prespecified should really be 3, but some ones might send 2
163 * instead, and the IPOPT_TS_PRESPEC constant can apparently
164 * have both values, so we have to hard-code it here.
168 printf("PRESPEC2.0");
170 case 3: /* IPOPT_TS_PRESPEC */
174 printf("[bad ts type %d]", cp[3]&0xF);
179 for (len = 4; len < length; len += hoplen) {
182 printf("%s%d@%s", type, EXTRACT_32BITS(&cp[len+hoplen-4]),
183 hoplen!=8 ? "" : ipaddr_string(&cp[len]));
188 printf("%s", ptr == len ? " ^ " : "");
191 printf(" [%d hops not recorded]} ", cp[3]>>4);
200 ip_optprint(register const u_char *cp, u_int length)
202 register u_int option_len;
203 const char *sep = "";
205 for (; length > 0; cp += option_len, length -= option_len) {
215 tok2str(ip_option_values,"unknown %u",option_code));
217 if (option_code == IPOPT_NOP ||
218 option_code == IPOPT_EOL)
224 if (option_len < 2) {
225 printf(" [bad length %u]", option_len);
230 if (option_len > length) {
231 printf(" [bad length %u]", option_len);
235 TCHECK2(*cp, option_len);
237 switch (option_code) {
242 ip_printts(cp, option_len);
245 case IPOPT_RR: /* fall through */
248 ip_printroute(cp, option_len);
252 if (option_len < 4) {
253 printf(" [bad length %u]", option_len);
257 if (EXTRACT_16BITS(&cp[2]) != 0)
258 printf(" value %u", EXTRACT_16BITS(&cp[2]));
261 case IPOPT_NOP: /* nothing to print - fall through */
274 * compute an IP header checksum.
275 * don't modifiy the packet.
278 in_cksum(const u_short *addr, register u_int len, int csum)
281 const u_short *w = addr;
286 * Our algorithm is simple, using a 32 bit accumulator (sum),
287 * we add sequential 16 bit words to it, and at the end, fold
288 * back all the carry bits from the top 16 bits into the lower
296 sum += htons(*(u_char *)w<<8);
299 * add back carry outs from top 16 bits to low 16 bits
301 sum = (sum >> 16) + (sum & 0xffff); /* add hi 16 to low 16 */
302 sum += (sum >> 16); /* add carry */
303 answer = ~sum; /* truncate to 16 bits */
308 * Given the host-byte-order value of the checksum field in a packet
309 * header, and the network-byte-order computed checksum of the data
310 * that the checksum covers (including the checksum itself), compute
311 * what the checksum field *should* have been.
314 in_cksum_shouldbe(u_int16_t sum, u_int16_t computed_sum)
319 * The value that should have gone into the checksum field
320 * is the negative of the value gotten by summing up everything
321 * *but* the checksum field.
323 * We can compute that by subtracting the value of the checksum
324 * field from the sum of all the data in the packet, and then
325 * computing the negative of that value.
327 * "sum" is the value of the checksum field, and "computed_sum"
328 * is the negative of the sum of all the data in the packets,
329 * so that's -(-computed_sum - sum), or (sum + computed_sum).
331 * All the arithmetic in question is one's complement, so the
332 * addition must include an end-around carry; we do this by
333 * doing the arithmetic in 32 bits (with no sign-extension),
334 * and then adding the upper 16 bits of the sum, which contain
335 * the carry, to the lower 16 bits of the sum, and then do it
336 * again in case *that* sum produced a carry.
338 * As RFC 1071 notes, the checksum can be computed without
339 * byte-swapping the 16-bit words; summing 16-bit words
340 * on a big-endian machine gives a big-endian checksum, which
341 * can be directly stuffed into the big-endian checksum fields
342 * in protocol headers, and summing words on a little-endian
343 * machine gives a little-endian checksum, which must be
344 * byte-swapped before being stuffed into a big-endian checksum
347 * "computed_sum" is a network-byte-order value, so we must put
348 * it in host byte order before subtracting it from the
349 * host-byte-order value from the header; the adjusted checksum
350 * will be in host byte order, which is what we'll return.
353 shouldbe += ntohs(computed_sum);
354 shouldbe = (shouldbe & 0xFFFF) + (shouldbe >> 16);
355 shouldbe = (shouldbe & 0xFFFF) + (shouldbe >> 16);
359 #define IP_RES 0x8000
361 static struct tok ip_frag_values[] = {
364 { IP_RES, "rsvd" }, /* The RFC3514 evil ;-) bit */
368 struct ip_print_demux_state {
377 ip_print_demux(netdissect_options *ndo,
378 struct ip_print_demux_state *ipds)
380 struct protoent *proto;
386 ipds->nh = *ipds->cp;
387 ipds->advance = ah_print(ipds->cp);
388 if (ipds->advance <= 0)
390 ipds->cp += ipds->advance;
391 ipds->len -= ipds->advance;
397 ipds->advance = esp_print(ndo, ipds->cp, ipds->len,
398 (const u_char *)ipds->ip,
400 if (ipds->advance <= 0)
402 ipds->cp += ipds->advance;
403 ipds->len -= ipds->advance + padlen;
404 ipds->nh = enh & 0xff;
411 ipds->advance = ipcomp_print(ipds->cp, &enh);
412 if (ipds->advance <= 0)
414 ipds->cp += ipds->advance;
415 ipds->len -= ipds->advance;
416 ipds->nh = enh & 0xff;
421 sctp_print(ipds->cp, (const u_char *)ipds->ip, ipds->len);
425 dccp_print(ipds->cp, (const u_char *)ipds->ip, ipds->len);
429 /* pass on the MF bit plus the offset to detect fragments */
430 tcp_print(ipds->cp, ipds->len, (const u_char *)ipds->ip,
431 ipds->off & (IP_MF|IP_OFFMASK));
435 /* pass on the MF bit plus the offset to detect fragments */
436 udp_print(ipds->cp, ipds->len, (const u_char *)ipds->ip,
437 ipds->off & (IP_MF|IP_OFFMASK));
441 /* pass on the MF bit plus the offset to detect fragments */
442 icmp_print(ipds->cp, ipds->len, (const u_char *)ipds->ip,
443 ipds->off & (IP_MF|IP_OFFMASK));
448 * XXX - the current IANA protocol number assignments
449 * page lists 9 as "any private interior gateway
450 * (used by Cisco for their IGRP)" and 88 as
451 * "EIGRP" from Cisco.
453 * Recent BSD <netinet/in.h> headers define
454 * IP_PROTO_PIGP as 9 and IP_PROTO_IGRP as 88.
455 * We define IP_PROTO_PIGP as 9 and
456 * IP_PROTO_EIGRP as 88; those names better
457 * match was the current protocol number
460 igrp_print(ipds->cp, ipds->len, (const u_char *)ipds->ip);
464 eigrp_print(ipds->cp, ipds->len);
468 ND_PRINT((ndo, " nd %d", ipds->len));
472 egp_print(ipds->cp, ipds->len);
476 ospf_print(ipds->cp, ipds->len, (const u_char *)ipds->ip);
480 igmp_print(ipds->cp, ipds->len);
484 /* DVMRP multicast tunnel (ip-in-ip encapsulation) */
485 ip_print(gndo, ipds->cp, ipds->len);
487 ND_PRINT((ndo, " (ipip-proto-4)"));
494 /* ip6-in-ip encapsulation */
495 ip6_print(ipds->cp, ipds->len);
500 rsvp_print(ipds->cp, ipds->len);
505 gre_print(ipds->cp, ipds->len);
509 mobile_print(ipds->cp, ipds->len);
513 pim_print(ipds->cp, ipds->len,
514 in_cksum((const u_short*)ipds->cp, ipds->len, 0));
518 vrrp_print(ipds->cp, ipds->len, ipds->ip->ip_ttl);
522 pgm_print(ipds->cp, ipds->len, (const u_char *)ipds->ip);
526 if ((proto = getprotobynumber(ipds->nh)) != NULL)
527 ND_PRINT((ndo, " %s", proto->p_name));
529 ND_PRINT((ndo, " ip-proto-%d", ipds->nh));
530 ND_PRINT((ndo, " %d", ipds->len));
536 ip_print_inner(netdissect_options *ndo,
538 u_int length, u_int nh,
541 struct ip_print_demux_state ipd;
543 ipd.ip = (const struct ip *)bp2;
550 ip_print_demux(ndo, &ipd);
555 * print an IP datagram.
558 ip_print(netdissect_options *ndo,
562 struct ip_print_demux_state ipd;
563 struct ip_print_demux_state *ipds=&ipd;
566 u_int16_t sum, ip_sum;
567 struct protoent *proto;
569 ipds->ip = (const struct ip *)bp;
570 if (IP_V(ipds->ip) != 4) { /* print version if != 4 */
571 printf("IP%u ", IP_V(ipds->ip));
572 if (IP_V(ipds->ip) == 6)
573 printf(", wrong link-layer encapsulation");
578 if ((u_char *)(ipds->ip + 1) > snapend) {
582 if (length < sizeof (struct ip)) {
583 (void)printf("truncated-ip %u", length);
586 hlen = IP_HL(ipds->ip) * 4;
587 if (hlen < sizeof (struct ip)) {
588 (void)printf("bad-hlen %u", hlen);
592 ipds->len = EXTRACT_16BITS(&ipds->ip->ip_len);
593 if (length < ipds->len)
594 (void)printf("truncated-ip - %u bytes missing! ",
596 if (ipds->len < hlen) {
599 (void)printf("bad-len %u", ipds->len);
603 /* we guess that it is a TSO send */
607 (void)printf("bad-len %u", ipds->len);
609 #endif /* GUESS_TSO */
613 * Cut off the snapshot length to the end of the IP payload.
615 ipend = bp + ipds->len;
621 ipds->off = EXTRACT_16BITS(&ipds->ip->ip_off);
624 (void)printf("(tos 0x%x", (int)ipds->ip->ip_tos);
626 if (ipds->ip->ip_tos & 0x03) {
627 switch (ipds->ip->ip_tos & 0x03) {
629 (void)printf(",ECT(1)");
632 (void)printf(",ECT(0)");
639 if (ipds->ip->ip_ttl >= 1)
640 (void)printf(", ttl %u", ipds->ip->ip_ttl);
643 * for the firewall guys, print id, offset.
644 * On all but the last stick a "+" in the flags portion.
645 * For unfragmented datagrams, note the don't fragment flag.
648 (void)printf(", id %u, offset %u, flags [%s], proto %s (%u)",
649 EXTRACT_16BITS(&ipds->ip->ip_id),
650 (ipds->off & 0x1fff) * 8,
651 bittok2str(ip_frag_values, "none", ipds->off&0xe000),
652 tok2str(ipproto_values,"unknown",ipds->ip->ip_p),
655 (void)printf(", length %u", EXTRACT_16BITS(&ipds->ip->ip_len));
657 if ((hlen - sizeof(struct ip)) > 0) {
658 printf(", options (");
659 ip_optprint((u_char *)(ipds->ip + 1), hlen - sizeof(struct ip));
663 if (!Kflag && (u_char *)ipds->ip + hlen <= snapend) {
664 sum = in_cksum((const u_short *)ipds->ip, hlen, 0);
666 ip_sum = EXTRACT_16BITS(&ipds->ip->ip_sum);
667 (void)printf(", bad cksum %x (->%x)!", ip_sum,
668 in_cksum_shouldbe(ip_sum, sum));
676 * If this is fragment zero, hand it to the next higher
679 if ((ipds->off & 0x1fff) == 0) {
680 ipds->cp = (const u_char *)ipds->ip + hlen;
681 ipds->nh = ipds->ip->ip_p;
683 if (ipds->nh != IPPROTO_TCP && ipds->nh != IPPROTO_UDP &&
684 ipds->nh != IPPROTO_SCTP && ipds->nh != IPPROTO_DCCP) {
685 (void)printf("%s > %s: ",
686 ipaddr_string(&ipds->ip->ip_src),
687 ipaddr_string(&ipds->ip->ip_dst));
689 ip_print_demux(ndo, ipds);
691 /* Ultra quiet now means that all this stuff should be suppressed */
692 if (qflag > 1) return;
695 * if this isn't the first frag, we're missing the
696 * next level protocol header. print the ip addr
699 if (ipds->off & 0x1fff) {
700 (void)printf("%s > %s:", ipaddr_string(&ipds->ip->ip_src),
701 ipaddr_string(&ipds->ip->ip_dst));
702 if ((proto = getprotobynumber(ipds->ip->ip_p)) != NULL)
703 (void)printf(" %s", proto->p_name);
705 (void)printf(" ip-proto-%d", ipds->ip->ip_p);
711 ipN_print(register const u_char *bp, register u_int length)
715 ip = (struct ip *)bp;
717 (void)printf("truncated-ip %d", length);
720 memcpy (&hdr, (char *)ip, 4);
721 switch (IP_V(&hdr)) {
723 ip_print (gndo, bp, length);
727 ip6_print (bp, length);
731 (void)printf("unknown ip %d", IP_V(&hdr));
738 * c-style: whitesmith