]>
The Tcpdump Group git mirrors - tcpdump/blob - print-ip.c
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.147 2005-01-21 08:02:06 hannes 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" },
57 * print the recorded route in an IP RR, LSRR or SSRR option.
60 ip_printroute(register const u_char
*cp
, u_int length
)
66 printf(" [bad length %u]", length
);
70 printf(" [bad length %u]", length
);
72 if (ptr
< 3 || ((ptr
+ 1) & 3) || ptr
> length
+ 1)
73 printf(" [bad ptr %u]", cp
[2]);
75 for (len
= 3; len
< length
; len
+= 4) {
76 printf("%s", ipaddr_string(&cp
[len
]));
83 * If source-routing is present and valid, return the final destination.
84 * Otherwise, return IP destination.
86 * This is used for UDP and TCP pseudo-header in the checksum
90 ip_finddst(const struct ip
*ip
)
97 cp
= (const u_char
*)(ip
+ 1);
98 length
= (IP_HL(ip
) << 2) - sizeof(struct ip
);
100 for (; length
> 0; cp
+= len
, length
-= len
) {
107 else if (tt
== IPOPT_NOP
)
122 memcpy(&retval
, cp
+ len
- 4, 4);
127 memcpy(&retval
, &ip
->ip_dst
.s_addr
, sizeof(u_int32_t
));
132 ip_printts(register const u_char
*cp
, u_int length
)
140 printf("[bad length %d]", length
);
144 hoplen
= ((cp
[3]&0xF) != IPOPT_TS_TSONLY
) ? 8 : 4;
145 if ((length
- 4) & (hoplen
-1))
146 printf("[bad length %d]", length
);
149 if (ptr
< 4 || ((ptr
- 4) & (hoplen
-1)) || ptr
> length
+ 1)
150 printf("[bad ptr %d]", cp
[2]);
152 case IPOPT_TS_TSONLY
:
155 case IPOPT_TS_TSANDADDR
:
159 * prespecified should really be 3, but some ones might send 2
160 * instead, and the IPOPT_TS_PRESPEC constant can apparently
161 * have both values, so we have to hard-code it here.
165 printf("PRESPEC2.0");
167 case 3: /* IPOPT_TS_PRESPEC */
171 printf("[bad ts type %d]", cp
[3]&0xF);
176 for (len
= 4; len
< length
; len
+= hoplen
) {
179 printf("%s%d@%s", type
, EXTRACT_32BITS(&cp
[len
+hoplen
-4]),
180 hoplen
!=8 ? "" : ipaddr_string(&cp
[len
]));
185 printf("%s", ptr
== len
? " ^ " : "");
188 printf(" [%d hops not recorded]} ", cp
[3]>>4);
197 ip_optprint(register const u_char
*cp
, u_int length
)
199 register u_int option_len
;
201 for (; length
> 0; cp
+= option_len
, length
-= option_len
) {
207 if (option_code
== IPOPT_NOP
||
208 option_code
== IPOPT_EOL
)
216 printf("%s (%u) len %u",
217 tok2str(ip_option_values
,"unknown",option_code
),
224 TCHECK2(*cp
, option_len
);
226 switch (option_code
) {
231 ip_printts(cp
, option_len
);
234 case IPOPT_RR
: /* fall through */
237 ip_printroute( cp
, option_len
);
242 if (EXTRACT_16BITS(&cp
[2]) != 0)
243 printf("value %u", EXTRACT_16BITS(&cp
[2]));
246 case IPOPT_NOP
: /* nothing to print - fall through */
259 * compute an IP header checksum.
260 * don't modifiy the packet.
263 in_cksum(const u_short
*addr
, register u_int len
, int csum
)
266 const u_short
*w
= addr
;
271 * Our algorithm is simple, using a 32 bit accumulator (sum),
272 * we add sequential 16 bit words to it, and at the end, fold
273 * back all the carry bits from the top 16 bits into the lower
281 sum
+= htons(*(u_char
*)w
<<8);
284 * add back carry outs from top 16 bits to low 16 bits
286 sum
= (sum
>> 16) + (sum
& 0xffff); /* add hi 16 to low 16 */
287 sum
+= (sum
>> 16); /* add carry */
288 answer
= ~sum
; /* truncate to 16 bits */
293 * Given the host-byte-order value of the checksum field in a packet
294 * header, and the network-byte-order computed checksum of the data
295 * that the checksum covers (including the checksum itself), compute
296 * what the checksum field *should* have been.
299 in_cksum_shouldbe(u_int16_t sum
, u_int16_t computed_sum
)
304 * The value that should have gone into the checksum field
305 * is the negative of the value gotten by summing up everything
306 * *but* the checksum field.
308 * We can compute that by subtracting the value of the checksum
309 * field from the sum of all the data in the packet, and then
310 * computing the negative of that value.
312 * "sum" is the value of the checksum field, and "computed_sum"
313 * is the negative of the sum of all the data in the packets,
314 * so that's -(-computed_sum - sum), or (sum + computed_sum).
316 * All the arithmetic in question is one's complement, so the
317 * addition must include an end-around carry; we do this by
318 * doing the arithmetic in 32 bits (with no sign-extension),
319 * and then adding the upper 16 bits of the sum, which contain
320 * the carry, to the lower 16 bits of the sum, and then do it
321 * again in case *that* sum produced a carry.
323 * As RFC 1071 notes, the checksum can be computed without
324 * byte-swapping the 16-bit words; summing 16-bit words
325 * on a big-endian machine gives a big-endian checksum, which
326 * can be directly stuffed into the big-endian checksum fields
327 * in protocol headers, and summing words on a little-endian
328 * machine gives a little-endian checksum, which must be
329 * byte-swapped before being stuffed into a big-endian checksum
332 * "computed_sum" is a network-byte-order value, so we must put
333 * it in host byte order before subtracting it from the
334 * host-byte-order value from the header; the adjusted checksum
335 * will be in host byte order, which is what we'll return.
338 shouldbe
+= ntohs(computed_sum
);
339 shouldbe
= (shouldbe
& 0xFFFF) + (shouldbe
>> 16);
340 shouldbe
= (shouldbe
& 0xFFFF) + (shouldbe
>> 16);
350 #define IP_RES 0x8000
352 static struct tok ip_frag_values
[] = {
355 { IP_RES
, "rsvd" }, /* The RFC3514 evil ;-) bit */
360 * print an IP datagram.
363 ip_print(register const u_char
*bp
, register u_int length
)
365 register const struct ip
*ip
;
366 register u_int hlen
, len
, off
;
368 register const u_char
*cp
;
371 struct protoent
*proto
;
372 u_int16_t sum
, ip_sum
;
374 ip
= (const struct ip
*)bp
;
375 if (IP_V(ip
) != 4) { /* print version if != 4 */
376 printf("IP%u ", IP_V(ip
));
378 printf(", wrong link-layer encapsulation");
383 if ((u_char
*)(ip
+ 1) > snapend
) {
387 if (length
< sizeof (struct ip
)) {
388 (void)printf("truncated-ip %u", length
);
391 hlen
= IP_HL(ip
) * 4;
392 if (hlen
< sizeof (struct ip
)) {
393 (void)printf("bad-hlen %u", hlen
);
397 len
= EXTRACT_16BITS(&ip
->ip_len
);
399 (void)printf("truncated-ip - %u bytes missing! ",
404 (void)printf("bad-len %u", len
);
408 /* we guess that it is a TSO send */
412 (void)printf("bad-len %u", len
);
414 #endif /* GUESS_TSO */
418 * Cut off the snapshot length to the end of the IP payload.
426 off
= EXTRACT_16BITS(&ip
->ip_off
);
429 (void)printf("(tos 0x%x", (int)ip
->ip_tos
);
431 if (ip
->ip_tos
& 0x03) {
432 switch (ip
->ip_tos
& 0x03) {
434 (void)printf(",ECT(1)");
437 (void)printf(",ECT(0)");
445 (void)printf(", ttl %3u", ip
->ip_ttl
);
448 * for the firewall guys, print id, offset.
449 * On all but the last stick a "+" in the flags portion.
450 * For unfragmented datagrams, note the don't fragment flag.
453 (void)printf(", id %u, offset %u, flags [%s], proto: %s (%u)",
454 EXTRACT_16BITS(&ip
->ip_id
),
456 bittok2str(ip_frag_values
, "none", off
& 0xe000 ),
457 tok2str(ipproto_values
,"unknown",ip
->ip_p
),
460 (void)printf(", length: %u", EXTRACT_16BITS(&ip
->ip_len
));
462 if ((hlen
- sizeof(struct ip
)) > 0) {
463 printf(", options ( ");
464 ip_optprint((u_char
*)(ip
+ 1), hlen
- sizeof(struct ip
));
468 if ((u_char
*)ip
+ hlen
<= snapend
) {
469 sum
= in_cksum((const u_short
*)ip
, hlen
, 0);
471 ip_sum
= EXTRACT_16BITS(&ip
->ip_sum
);
472 (void)printf(", bad cksum %x (->%x)!", ip_sum
,
473 in_cksum_shouldbe(ip_sum
, sum
));
481 * If this is fragment zero, hand it to the next higher
484 if ((off
& 0x1fff) == 0) {
485 cp
= (const u_char
*)ip
+ hlen
;
488 if (nh
!= IPPROTO_TCP
&& nh
!= IPPROTO_UDP
&&
489 nh
!= IPPROTO_SCTP
) {
490 (void)printf("%s > %s: ", ipaddr_string(&ip
->ip_src
),
491 ipaddr_string(&ip
->ip_dst
));
498 advance
= ah_print(cp
);
508 advance
= esp_print(gndo
, cp
, len
, (const u_char
*)ip
, &enh
, &padlen
);
512 len
-= advance
+ padlen
;
520 advance
= ipcomp_print(cp
, &enh
);
530 sctp_print(cp
, (const u_char
*)ip
, len
);
534 tcp_print(cp
, len
, (const u_char
*)ip
, (off
&~ 0x6000));
538 udp_print(cp
, len
, (const u_char
*)ip
, (off
&~ 0x6000));
542 /* pass on the MF bit plus the offset to detect fragments */
543 icmp_print(cp
, len
, (const u_char
*)ip
, (off
& 0x3fff));
548 * XXX - the current IANA protocol number assignments
549 * page lists 9 as "any private interior gateway
550 * (used by Cisco for their IGRP)" and 88 as
551 * "EIGRP" from Cisco.
553 * Recent BSD <netinet/in.h> headers define
554 * IP_PROTO_PIGP as 9 and IP_PROTO_IGRP as 88.
555 * We define IP_PROTO_PIGP as 9 and
556 * IP_PROTO_EIGRP as 88; those names better
557 * match was the current protocol number
560 igrp_print(cp
, len
, (const u_char
*)ip
);
564 eigrp_print(cp
, len
);
568 (void)printf(" nd %d", len
);
576 ospf_print(cp
, len
, (const u_char
*)ip
);
584 /* DVMRP multicast tunnel (ip-in-ip encapsulation) */
587 printf(" (ipip-proto-4)");
594 /* ip6-in-ip encapsulation */
609 mobile_print(cp
, len
);
617 vrrp_print(cp
, len
, ip
->ip_ttl
);
621 if ((proto
= getprotobynumber(nh
)) != NULL
)
622 (void)printf(" %s", proto
->p_name
);
624 (void)printf(" ip-proto-%d", nh
);
629 /* Ultra quiet now means that all this stuff should be suppressed */
630 if (qflag
> 1) return;
633 * if this isn't the first frag, we're missing the
634 * next level protocol header. print the ip addr
638 (void)printf("%s > %s:", ipaddr_string(&ip
->ip_src
),
639 ipaddr_string(&ip
->ip_dst
));
640 if ((proto
= getprotobynumber(ip
->ip_p
)) != NULL
)
641 (void)printf(" %s", proto
->p_name
);
643 (void)printf(" ip-proto-%d", ip
->ip_p
);
649 ipN_print(register const u_char
*bp
, register u_int length
)
653 ip
= (struct ip
*)bp
;
655 (void)printf("truncated-ip %d", length
);
658 memcpy (&hdr
, (char *)ip
, 4);
659 switch (IP_V(&hdr
)) {
661 ip_print (bp
, length
);
665 ip6_print (bp
, length
);
669 (void)printf("unknown ip %d", IP_V(&hdr
));