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.149.2.9 2007/09/14 01:30:02 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 */
44 struct tok ip_option_values[] = {
47 { IPOPT_TS, "timestamp" },
48 { IPOPT_SECURITY, "security" },
50 { IPOPT_SSRR, "SSRR" },
51 { IPOPT_LSRR, "LSRR" },
53 { IPOPT_RFC1393, "traceroute" },
58 * print the recorded route in an IP RR, LSRR or SSRR option.
61 ip_printroute(register const u_char *cp, u_int length)
67 printf(" [bad length %u]", length);
71 printf(" [bad length %u]", length);
73 if (ptr < 3 || ((ptr + 1) & 3) || ptr > length + 1)
74 printf(" [bad ptr %u]", cp[2]);
76 for (len = 3; len < length; len += 4) {
77 printf(" %s", ipaddr_string(&cp[len]));
84 * If source-routing is present and valid, return the final destination.
85 * Otherwise, return IP destination.
87 * This is used for UDP and TCP pseudo-header in the checksum
91 ip_finddst(const struct ip *ip)
98 cp = (const u_char *)(ip + 1);
99 length = (IP_HL(ip) << 2) - sizeof(struct ip);
101 for (; length > 0; cp += len, length -= len) {
108 else if (tt == IPOPT_NOP)
123 memcpy(&retval, cp + len - 4, 4);
128 memcpy(&retval, &ip->ip_dst.s_addr, sizeof(u_int32_t));
133 ip_printts(register const u_char *cp, u_int length)
141 printf("[bad length %u]", length);
145 hoplen = ((cp[3]&0xF) != IPOPT_TS_TSONLY) ? 8 : 4;
146 if ((length - 4) & (hoplen-1))
147 printf("[bad length %u]", length);
150 if (ptr < 4 || ((ptr - 4) & (hoplen-1)) || ptr > length + 1)
151 printf("[bad ptr %u]", cp[2]);
153 case IPOPT_TS_TSONLY:
156 case IPOPT_TS_TSANDADDR:
160 * prespecified should really be 3, but some ones might send 2
161 * instead, and the IPOPT_TS_PRESPEC constant can apparently
162 * have both values, so we have to hard-code it here.
166 printf("PRESPEC2.0");
168 case 3: /* IPOPT_TS_PRESPEC */
172 printf("[bad ts type %d]", cp[3]&0xF);
177 for (len = 4; len < length; len += hoplen) {
180 printf("%s%d@%s", type, EXTRACT_32BITS(&cp[len+hoplen-4]),
181 hoplen!=8 ? "" : ipaddr_string(&cp[len]));
186 printf("%s", ptr == len ? " ^ " : "");
189 printf(" [%d hops not recorded]} ", cp[3]>>4);
198 ip_optprint(register const u_char *cp, u_int length)
200 register u_int option_len;
201 const char *sep = "";
203 for (; length > 0; cp += option_len, length -= option_len) {
213 tok2str(ip_option_values,"unknown %u",option_code));
215 if (option_code == IPOPT_NOP ||
216 option_code == IPOPT_EOL)
222 if (option_len < 2) {
223 printf(" [bad length %u]", option_len);
228 if (option_len > length) {
229 printf(" [bad length %u]", option_len);
233 TCHECK2(*cp, option_len);
235 switch (option_code) {
240 ip_printts(cp, option_len);
243 case IPOPT_RR: /* fall through */
246 ip_printroute(cp, option_len);
250 if (option_len < 4) {
251 printf(" [bad length %u]", option_len);
255 if (EXTRACT_16BITS(&cp[2]) != 0)
256 printf(" value %u", EXTRACT_16BITS(&cp[2]));
259 case IPOPT_NOP: /* nothing to print - fall through */
272 * compute an IP header checksum.
273 * don't modifiy the packet.
276 in_cksum(const u_short *addr, register u_int len, int csum)
279 const u_short *w = addr;
284 * Our algorithm is simple, using a 32 bit accumulator (sum),
285 * we add sequential 16 bit words to it, and at the end, fold
286 * back all the carry bits from the top 16 bits into the lower
294 sum += htons(*(u_char *)w<<8);
297 * add back carry outs from top 16 bits to low 16 bits
299 sum = (sum >> 16) + (sum & 0xffff); /* add hi 16 to low 16 */
300 sum += (sum >> 16); /* add carry */
301 answer = ~sum; /* truncate to 16 bits */
306 * Given the host-byte-order value of the checksum field in a packet
307 * header, and the network-byte-order computed checksum of the data
308 * that the checksum covers (including the checksum itself), compute
309 * what the checksum field *should* have been.
312 in_cksum_shouldbe(u_int16_t sum, u_int16_t computed_sum)
317 * The value that should have gone into the checksum field
318 * is the negative of the value gotten by summing up everything
319 * *but* the checksum field.
321 * We can compute that by subtracting the value of the checksum
322 * field from the sum of all the data in the packet, and then
323 * computing the negative of that value.
325 * "sum" is the value of the checksum field, and "computed_sum"
326 * is the negative of the sum of all the data in the packets,
327 * so that's -(-computed_sum - sum), or (sum + computed_sum).
329 * All the arithmetic in question is one's complement, so the
330 * addition must include an end-around carry; we do this by
331 * doing the arithmetic in 32 bits (with no sign-extension),
332 * and then adding the upper 16 bits of the sum, which contain
333 * the carry, to the lower 16 bits of the sum, and then do it
334 * again in case *that* sum produced a carry.
336 * As RFC 1071 notes, the checksum can be computed without
337 * byte-swapping the 16-bit words; summing 16-bit words
338 * on a big-endian machine gives a big-endian checksum, which
339 * can be directly stuffed into the big-endian checksum fields
340 * in protocol headers, and summing words on a little-endian
341 * machine gives a little-endian checksum, which must be
342 * byte-swapped before being stuffed into a big-endian checksum
345 * "computed_sum" is a network-byte-order value, so we must put
346 * it in host byte order before subtracting it from the
347 * host-byte-order value from the header; the adjusted checksum
348 * will be in host byte order, which is what we'll return.
351 shouldbe += ntohs(computed_sum);
352 shouldbe = (shouldbe & 0xFFFF) + (shouldbe >> 16);
353 shouldbe = (shouldbe & 0xFFFF) + (shouldbe >> 16);
357 #define IP_RES 0x8000
359 static struct tok ip_frag_values[] = {
362 { IP_RES, "rsvd" }, /* The RFC3514 evil ;-) bit */
366 struct ip_print_demux_state {
375 ip_print_demux(netdissect_options *ndo,
376 struct ip_print_demux_state *ipds)
378 struct protoent *proto;
384 ipds->nh = *ipds->cp;
385 ipds->advance = ah_print(ipds->cp);
386 if (ipds->advance <= 0)
388 ipds->cp += ipds->advance;
389 ipds->len -= ipds->advance;
395 ipds->advance = esp_print(ndo, ipds->cp, ipds->len,
396 (const u_char *)ipds->ip,
398 if (ipds->advance <= 0)
400 ipds->cp += ipds->advance;
401 ipds->len -= ipds->advance + padlen;
402 ipds->nh = enh & 0xff;
409 ipds->advance = ipcomp_print(ipds->cp, &enh);
410 if (ipds->advance <= 0)
412 ipds->cp += ipds->advance;
413 ipds->len -= ipds->advance;
414 ipds->nh = enh & 0xff;
419 sctp_print(ipds->cp, (const u_char *)ipds->ip, ipds->len);
423 dccp_print(ipds->cp, (const u_char *)ipds->ip, ipds->len);
427 /* pass on the MF bit plus the offset to detect fragments */
428 tcp_print(ipds->cp, ipds->len, (const u_char *)ipds->ip,
429 ipds->off & (IP_MF|IP_OFFMASK));
433 /* pass on the MF bit plus the offset to detect fragments */
434 udp_print(ipds->cp, ipds->len, (const u_char *)ipds->ip,
435 ipds->off & (IP_MF|IP_OFFMASK));
439 /* pass on the MF bit plus the offset to detect fragments */
440 icmp_print(ipds->cp, ipds->len, (const u_char *)ipds->ip,
441 ipds->off & (IP_MF|IP_OFFMASK));
446 * XXX - the current IANA protocol number assignments
447 * page lists 9 as "any private interior gateway
448 * (used by Cisco for their IGRP)" and 88 as
449 * "EIGRP" from Cisco.
451 * Recent BSD <netinet/in.h> headers define
452 * IP_PROTO_PIGP as 9 and IP_PROTO_IGRP as 88.
453 * We define IP_PROTO_PIGP as 9 and
454 * IP_PROTO_EIGRP as 88; those names better
455 * match was the current protocol number
458 igrp_print(ipds->cp, ipds->len, (const u_char *)ipds->ip);
462 eigrp_print(ipds->cp, ipds->len);
466 ND_PRINT((ndo, " nd %d", ipds->len));
470 egp_print(ipds->cp, ipds->len);
474 ospf_print(ipds->cp, ipds->len, (const u_char *)ipds->ip);
478 igmp_print(ipds->cp, ipds->len);
482 /* DVMRP multicast tunnel (ip-in-ip encapsulation) */
483 ip_print(gndo, ipds->cp, ipds->len);
485 ND_PRINT((ndo, " (ipip-proto-4)"));
492 /* ip6-in-ip encapsulation */
493 ip6_print(ipds->cp, ipds->len);
498 rsvp_print(ipds->cp, ipds->len);
503 gre_print(ipds->cp, ipds->len);
507 mobile_print(ipds->cp, ipds->len);
511 pim_print(ipds->cp, ipds->len);
515 vrrp_print(ipds->cp, ipds->len, ipds->ip->ip_ttl);
519 pgm_print(ipds->cp, ipds->len, (const u_char *)ipds->ip);
523 if ((proto = getprotobynumber(ipds->nh)) != NULL)
524 ND_PRINT((ndo, " %s", proto->p_name));
526 ND_PRINT((ndo, " ip-proto-%d", ipds->nh));
527 ND_PRINT((ndo, " %d", ipds->len));
533 ip_print_inner(netdissect_options *ndo,
535 u_int length, u_int nh,
538 struct ip_print_demux_state ipd;
540 ipd.ip = (const struct ip *)bp2;
547 ip_print_demux(ndo, &ipd);
552 * print an IP datagram.
555 ip_print(netdissect_options *ndo,
559 struct ip_print_demux_state ipd;
560 struct ip_print_demux_state *ipds=&ipd;
563 u_int16_t sum, ip_sum;
564 struct protoent *proto;
566 ipds->ip = (const struct ip *)bp;
567 if (IP_V(ipds->ip) != 4) { /* print version if != 4 */
568 printf("IP%u ", IP_V(ipds->ip));
569 if (IP_V(ipds->ip) == 6)
570 printf(", wrong link-layer encapsulation");
575 if ((u_char *)(ipds->ip + 1) > snapend) {
579 if (length < sizeof (struct ip)) {
580 (void)printf("truncated-ip %u", length);
583 hlen = IP_HL(ipds->ip) * 4;
584 if (hlen < sizeof (struct ip)) {
585 (void)printf("bad-hlen %u", hlen);
589 ipds->len = EXTRACT_16BITS(&ipds->ip->ip_len);
590 if (length < ipds->len)
591 (void)printf("truncated-ip - %u bytes missing! ",
593 if (ipds->len < hlen) {
596 (void)printf("bad-len %u", ipds->len);
600 /* we guess that it is a TSO send */
604 (void)printf("bad-len %u", ipds->len);
606 #endif /* GUESS_TSO */
610 * Cut off the snapshot length to the end of the IP payload.
612 ipend = bp + ipds->len;
618 ipds->off = EXTRACT_16BITS(&ipds->ip->ip_off);
621 (void)printf("(tos 0x%x", (int)ipds->ip->ip_tos);
623 if (ipds->ip->ip_tos & 0x03) {
624 switch (ipds->ip->ip_tos & 0x03) {
626 (void)printf(",ECT(1)");
629 (void)printf(",ECT(0)");
636 if (ipds->ip->ip_ttl >= 1)
637 (void)printf(", ttl %u", ipds->ip->ip_ttl);
640 * for the firewall guys, print id, offset.
641 * On all but the last stick a "+" in the flags portion.
642 * For unfragmented datagrams, note the don't fragment flag.
645 (void)printf(", id %u, offset %u, flags [%s], proto %s (%u)",
646 EXTRACT_16BITS(&ipds->ip->ip_id),
647 (ipds->off & 0x1fff) * 8,
648 bittok2str(ip_frag_values, "none", ipds->off&0xe000),
649 tok2str(ipproto_values,"unknown",ipds->ip->ip_p),
652 (void)printf(", length %u", EXTRACT_16BITS(&ipds->ip->ip_len));
654 if ((hlen - sizeof(struct ip)) > 0) {
655 printf(", options (");
656 ip_optprint((u_char *)(ipds->ip + 1), hlen - sizeof(struct ip));
660 if ((u_char *)ipds->ip + hlen <= snapend) {
661 sum = in_cksum((const u_short *)ipds->ip, hlen, 0);
663 ip_sum = EXTRACT_16BITS(&ipds->ip->ip_sum);
664 (void)printf(", bad cksum %x (->%x)!", ip_sum,
665 in_cksum_shouldbe(ip_sum, sum));
673 * If this is fragment zero, hand it to the next higher
676 if ((ipds->off & 0x1fff) == 0) {
677 ipds->cp = (const u_char *)ipds->ip + hlen;
678 ipds->nh = ipds->ip->ip_p;
680 if (ipds->nh != IPPROTO_TCP && ipds->nh != IPPROTO_UDP &&
681 ipds->nh != IPPROTO_SCTP && ipds->nh != IPPROTO_DCCP) {
682 (void)printf("%s > %s: ",
683 ipaddr_string(&ipds->ip->ip_src),
684 ipaddr_string(&ipds->ip->ip_dst));
686 ip_print_demux(ndo, ipds);
688 /* Ultra quiet now means that all this stuff should be suppressed */
689 if (qflag > 1) return;
692 * if this isn't the first frag, we're missing the
693 * next level protocol header. print the ip addr
696 if (ipds->off & 0x1fff) {
697 (void)printf("%s > %s:", ipaddr_string(&ipds->ip->ip_src),
698 ipaddr_string(&ipds->ip->ip_dst));
699 if ((proto = getprotobynumber(ipds->ip->ip_p)) != NULL)
700 (void)printf(" %s", proto->p_name);
702 (void)printf(" ip-proto-%d", ipds->ip->ip_p);
708 ipN_print(register const u_char *bp, register u_int length)
712 ip = (struct ip *)bp;
714 (void)printf("truncated-ip %d", length);
717 memcpy (&hdr, (char *)ip, 4);
718 switch (IP_V(&hdr)) {
720 ip_print (gndo, bp, length);
724 ip6_print (bp, length);
728 (void)printf("unknown ip %d", IP_V(&hdr));
735 * c-style: whitesmith