]>
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.144 2004-09-27 21:13:09 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
, len0
, 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! ",
402 (void)printf("bad-len %u", len
);
407 * Cut off the snapshot length to the end of the IP payload.
416 off
= EXTRACT_16BITS(&ip
->ip_off
);
419 (void)printf("(tos 0x%x", (int)ip
->ip_tos
);
421 if (ip
->ip_tos
& 0x03) {
422 switch (ip
->ip_tos
& 0x03) {
424 (void)printf(",ECT(1)");
427 (void)printf(",ECT(0)");
435 (void)printf(", ttl %3u", ip
->ip_ttl
);
438 * for the firewall guys, print id, offset.
439 * On all but the last stick a "+" in the flags portion.
440 * For unfragmented datagrams, note the don't fragment flag.
443 (void)printf(", id %u, offset %u, flags [%s], proto: %s (%u)",
444 EXTRACT_16BITS(&ip
->ip_id
),
446 bittok2str(ip_frag_values
, "none", off
& 0xe000 ),
447 tok2str(ipproto_values
,"unknown",ip
->ip_p
),
450 (void)printf(", length: %u", EXTRACT_16BITS(&ip
->ip_len
));
452 if ((hlen
- sizeof(struct ip
)) > 0) {
453 printf(", options ( ");
454 ip_optprint((u_char
*)(ip
+ 1), hlen
- sizeof(struct ip
));
458 if ((u_char
*)ip
+ hlen
<= snapend
) {
459 sum
= in_cksum((const u_short
*)ip
, hlen
, 0);
461 ip_sum
= EXTRACT_16BITS(&ip
->ip_sum
);
462 (void)printf(", bad cksum %x (->%x)!", ip_sum
,
463 in_cksum_shouldbe(ip_sum
, sum
));
471 * If this is fragment zero, hand it to the next higher
474 if ((off
& 0x1fff) == 0) {
475 cp
= (const u_char
*)ip
+ hlen
;
478 if (nh
!= IPPROTO_TCP
&& nh
!= IPPROTO_UDP
&&
479 nh
!= IPPROTO_SCTP
) {
480 (void)printf("%s > %s: ", ipaddr_string(&ip
->ip_src
),
481 ipaddr_string(&ip
->ip_dst
));
488 advance
= ah_print(cp
);
498 advance
= esp_print(gndo
, cp
, len
, (const u_char
*)ip
, &enh
, &padlen
);
502 len
-= advance
+ padlen
;
510 advance
= ipcomp_print(cp
, &enh
);
520 sctp_print(cp
, (const u_char
*)ip
, len
);
524 tcp_print(cp
, len
, (const u_char
*)ip
, (off
&~ 0x6000));
528 udp_print(cp
, len
, (const u_char
*)ip
, (off
&~ 0x6000));
532 /* pass on the MF bit plus the offset to detect fragments */
533 icmp_print(cp
, len
, (const u_char
*)ip
, (off
& 0x3fff));
538 * XXX - the current IANA protocol number assignments
539 * page lists 9 as "any private interior gateway
540 * (used by Cisco for their IGRP)" and 88 as
541 * "EIGRP" from Cisco.
543 * Recent BSD <netinet/in.h> headers define
544 * IP_PROTO_PIGP as 9 and IP_PROTO_IGRP as 88.
545 * We define IP_PROTO_PIGP as 9 and
546 * IP_PROTO_EIGRP as 88; those names better
547 * match was the current protocol number
550 igrp_print(cp
, len
, (const u_char
*)ip
);
554 eigrp_print(cp
, len
);
558 (void)printf(" nd %d", len
);
566 ospf_print(cp
, len
, (const u_char
*)ip
);
574 /* DVMRP multicast tunnel (ip-in-ip encapsulation) */
577 printf(" (ipip-proto-4)");
584 /* ip6-in-ip encapsulation */
599 mobile_print(cp
, len
);
607 vrrp_print(cp
, len
, ip
->ip_ttl
);
611 if ((proto
= getprotobynumber(nh
)) != NULL
)
612 (void)printf(" %s", proto
->p_name
);
614 (void)printf(" ip-proto-%d", nh
);
619 /* Ultra quiet now means that all this stuff should be suppressed */
620 if (qflag
> 1) return;
623 * if this isn't the first frag, we're missing the
624 * next level protocol header. print the ip addr
628 (void)printf("%s > %s:", ipaddr_string(&ip
->ip_src
),
629 ipaddr_string(&ip
->ip_dst
));
630 if ((proto
= getprotobynumber(ip
->ip_p
)) != NULL
)
631 (void)printf(" %s", proto
->p_name
);
633 (void)printf(" ip-proto-%d", ip
->ip_p
);
639 ipN_print(register const u_char
*bp
, register u_int length
)
643 ip
= (struct ip
*)bp
;
645 (void)printf("truncated-ip %d", length
);
648 memcpy (&hdr
, (char *)ip
, 4);
649 switch (IP_V(&hdr
)) {
651 ip_print (bp
, length
);
655 ip6_print (bp
, length
);
659 (void)printf("unknown ip %d", IP_V(&hdr
));