]>
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
[] =
24 "@(#) $Header: /tcpdump/master/tcpdump/print-ip.c,v 1.127 2003-06-07 11:57:53 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
)
50 register u_int ptr
= cp
[2] - 1;
55 printf(" [bad length %d]", length
);
56 if (ptr
< 3 || ((ptr
+ 1) & 3) || ptr
> length
+ 1)
57 printf(" [bad ptr %d]", cp
[2]);
60 for (len
= 3; len
< length
; len
+= 4) {
63 printf("%s%s", type
, ipaddr_string(&cp
[len
]));
66 printf("%s}", ptr
== len
? "#" : "");
70 * If source-routing is present, return the final destination.
71 * Otherwise, return IP destination.
73 * This is used for UDP and TCP pseudo-header in the checksum
77 ip_finddst(const struct ip
*ip
)
84 cp
= (const u_char
*)(ip
+ 1);
85 length
= (IP_HL(ip
) << 2) - sizeof(struct ip
);
87 for (; length
> 0; cp
+= len
, length
-= len
) {
90 if (tt
== IPOPT_NOP
|| tt
== IPOPT_EOL
)
93 if (&cp
[1] >= snapend
) {
101 if (&cp
[1] >= snapend
|| cp
+ len
> snapend
) {
108 memcpy(&retval
, cp
+ len
- 4, 4);
112 return ip
->ip_dst
.s_addr
;
116 ip_printts(register const u_char
*cp
, u_int length
)
118 register u_int ptr
= cp
[2] - 1;
119 register u_int len
= 0;
124 hoplen
= ((cp
[3]&0xF) != IPOPT_TS_TSONLY
) ? 8 : 4;
125 if ((length
- 4) & (hoplen
-1))
126 printf("[bad length %d]", length
);
127 if (ptr
< 4 || ((ptr
- 4) & (hoplen
-1)) || ptr
> length
+ 1)
128 printf("[bad ptr %d]", cp
[2]);
130 case IPOPT_TS_TSONLY
:
133 case IPOPT_TS_TSANDADDR
:
137 * prespecified should really be 3, but some ones might send 2
138 * instead, and the IPOPT_TS_PRESPEC constant can apparently
139 * have both values, so we have to hard-code it here.
143 printf("PRESPEC2.0");
145 case 3: /* IPOPT_TS_PRESPEC */
149 printf("[bad ts type %d]", cp
[3]&0xF);
154 for (len
= 4; len
< length
; len
+= hoplen
) {
157 printf("%s%d@%s", type
, EXTRACT_32BITS(&cp
[len
+hoplen
-4]),
158 hoplen
!=8 ? "" : ipaddr_string(&cp
[len
]));
163 printf("%s", ptr
== len
? " ^ " : "");
166 printf(" [%d hops not recorded]} ", cp
[3]>>4);
175 ip_optprint(register const u_char
*cp
, u_int length
)
179 for (; length
> 0; cp
+= len
, length
-= len
) {
182 if (tt
== IPOPT_NOP
|| tt
== IPOPT_EOL
)
185 if (&cp
[1] >= snapend
) {
192 printf("[|ip op len %d]", len
);
195 if (&cp
[1] >= snapend
|| cp
+ len
> snapend
) {
204 printf("-%d", length
- 1);
215 #ifndef IPOPT_SECURITY
216 #define IPOPT_SECURITY 130
217 #endif /* IPOPT_SECURITY */
219 printf(" SECURITY{%d}", len
);
223 ip_printroute("RR", cp
, len
);
227 ip_printroute("SSRR", cp
, len
);
231 ip_printroute("LSRR", cp
, len
);
235 #define IPOPT_RA 148 /* router alert */
241 else if (cp
[2] || cp
[3])
242 printf("%d.%d", cp
[2], cp
[3]);
246 printf(" IPOPT-%d{%d}", cp
[0], len
);
253 * compute an IP header checksum.
254 * don't modifiy the packet.
257 in_cksum(const u_short
*addr
, register u_int len
, int csum
)
260 const u_short
*w
= addr
;
265 * Our algorithm is simple, using a 32 bit accumulator (sum),
266 * we add sequential 16 bit words to it, and at the end, fold
267 * back all the carry bits from the top 16 bits into the lower
275 sum
+= htons(*(u_char
*)w
<<8);
278 * add back carry outs from top 16 bits to low 16 bits
280 sum
= (sum
>> 16) + (sum
& 0xffff); /* add hi 16 to low 16 */
281 sum
+= (sum
>> 16); /* add carry */
282 answer
= ~sum
; /* truncate to 16 bits */
287 * Given the host-byte-order value of the checksum field in a packet
288 * header, and the network-byte-order computed checksum of the data
289 * that the checksum covers (including the checksum itself), compute
290 * what the checksum field *should* have been.
293 in_cksum_shouldbe(u_int16_t sum
, u_int16_t computed_sum
)
298 * The value that should have gone into the checksum field
299 * is the negative of the value gotten by summing up everything
300 * *but* the checksum field.
302 * We can compute that by subtracting the value of the checksum
303 * field from the sum of all the data in the packet, and then
304 * computing the negative of that value.
306 * "sum" is the value of the checksum field, and "computed_sum"
307 * is the negative of the sum of all the data in the packets,
308 * so that's -(-computed_sum - sum), or (sum + computed_sum).
310 * All the arithmetic in question is one's complement, so the
311 * addition must include an end-around carry; we do this by
312 * doing the arithmetic in 32 bits (with no sign-extension),
313 * and then adding the upper 16 bits of the sum, which contain
314 * the carry, to the lower 16 bits of the sum, and then do it
315 * again in case *that* sum produced a carry.
317 * As RFC 1071 notes, the checksum can be computed without
318 * byte-swapping the 16-bit words; summing 16-bit words
319 * on a big-endian machine gives a big-endian checksum, which
320 * can be directly stuffed into the big-endian checksum fields
321 * in protocol headers, and summing words on a little-endian
322 * machine gives a little-endian checksum, which must be
323 * byte-swapped before being stuffed into a big-endian checksum
326 * "computed_sum" is a network-byte-order value, so we must put
327 * it in host byte order before subtracting it from the
328 * host-byte-order value from the header; the adjusted checksum
329 * will be in host byte order, which is what we'll return.
332 shouldbe
+= ntohs(computed_sum
);
333 shouldbe
= (shouldbe
& 0xFFFF) + (shouldbe
>> 16);
334 shouldbe
= (shouldbe
& 0xFFFF) + (shouldbe
>> 16);
344 #define IP_RES 0x8000
346 static struct tok ip_frag_values
[] = {
349 { IP_RES
, "rsvd" }, /* The RFC3514 evil ;-) bit */
354 * print an IP datagram.
357 ip_print(register const u_char
*bp
, register u_int length
)
359 register const struct ip
*ip
;
360 register u_int hlen
, len
, len0
, off
;
361 register const u_char
*cp
;
364 struct protoent
*proto
;
365 u_int16_t sum
, ip_sum
;
366 const char *sep
= "";
368 ip
= (const struct ip
*)bp
;
369 if (IP_V(ip
) != 4) { /* print version if != 4 */
370 printf("IP%u ", IP_V(ip
));
372 printf(", wrong link-layer encapsulation");
377 if ((u_char
*)(ip
+ 1) > snapend
) {
381 if (length
< sizeof (struct ip
)) {
382 (void)printf("truncated-ip %d", length
);
385 hlen
= IP_HL(ip
) * 4;
386 if (hlen
< sizeof (struct ip
)) {
387 (void)printf("bad-hlen %d", hlen
);
391 len
= EXTRACT_16BITS(&ip
->ip_len
);
393 (void)printf("truncated-ip - %d bytes missing! ",
398 off
= EXTRACT_16BITS(&ip
->ip_off
);
401 (void)printf("(tos 0x%x", (int)ip
->ip_tos
);
403 if (ip
->ip_tos
& 0x03) {
404 switch (ip
->ip_tos
& 0x03) {
406 (void)printf(",ECT(1)");
409 (void)printf(",ECT(0)");
417 (void)printf(", ttl %3u", ip
->ip_ttl
);
420 * for the firewall guys, print id, offset.
421 * On all but the last stick a "+" in the flags portion.
422 * For unfragmented datagrams, note the don't fragment flag.
425 (void)printf(", id %u, offset %u, flags [%s]",
426 EXTRACT_16BITS(&ip
->ip_id
),
428 bittok2str(ip_frag_values
, "none", off
& 0xe000 ));
430 (void)printf(", length: %u", EXTRACT_16BITS(&ip
->ip_len
));
432 if ((hlen
- sizeof(struct ip
)) > 0) {
433 (void)printf(", optlength: %u (", hlen
- (u_int
)sizeof(struct ip
));
434 ip_optprint((u_char
*)(ip
+ 1), hlen
- sizeof(struct ip
));
438 if ((u_char
*)ip
+ hlen
<= snapend
) {
439 sum
= in_cksum((const u_short
*)ip
, hlen
, 0);
441 ip_sum
= EXTRACT_16BITS(&ip
->ip_sum
);
442 (void)printf("%sbad cksum %x (->%x)!", sep
,
444 in_cksum_shouldbe(ip_sum
, sum
));
453 * If this is fragment zero, hand it to the next higher
456 if ((off
& 0x1fff) == 0) {
457 cp
= (const u_char
*)ip
+ hlen
;
460 if (nh
!= IPPROTO_TCP
&& nh
!= IPPROTO_UDP
&&
461 nh
!= IPPROTO_SCTP
) {
462 (void)printf("%s > %s: ", ipaddr_string(&ip
->ip_src
),
463 ipaddr_string(&ip
->ip_dst
));
470 advance
= ah_print(cp
);
478 advance
= esp_print(cp
, (const u_char
*)ip
, &enh
, &padlen
);
480 len
-= advance
+ padlen
;
490 advance
= ipcomp_print(cp
, &enh
);
500 sctp_print(cp
, (const u_char
*)ip
, len
);
504 tcp_print(cp
, len
, (const u_char
*)ip
, (off
&~ 0x6000));
508 udp_print(cp
, len
, (const u_char
*)ip
, (off
&~ 0x6000));
512 /* pass on the MF bit plus the offset to detect fragments */
513 icmp_print(cp
, len
, (const u_char
*)ip
, (off
& 0x3fff));
517 igrp_print(cp
, len
, (const u_char
*)ip
);
521 (void)printf(" nd %d", len
);
529 ospf_print(cp
, len
, (const u_char
*)ip
);
537 /* DVMRP multicast tunnel (ip-in-ip encapsulation) */
540 printf(" (ipip-proto-4)");
547 /* ip6-in-ip encapsulation */
562 mobile_print(cp
, len
);
570 vrrp_print(cp
, len
, ip
->ip_ttl
);
574 if ((proto
= getprotobynumber(nh
)) != NULL
)
575 (void)printf(" %s", proto
->p_name
);
577 (void)printf(" ip-proto-%d", nh
);
582 /* Ultra quiet now means that all this stuff should be suppressed */
583 if (qflag
> 1) return;
586 * if this isn't the first frag, we're missing the
587 * next level protocol header. print the ip addr
591 (void)printf("%s > %s:", ipaddr_string(&ip
->ip_src
),
592 ipaddr_string(&ip
->ip_dst
));
593 if ((proto
= getprotobynumber(ip
->ip_p
)) != NULL
)
594 (void)printf(" %s", proto
->p_name
);
596 (void)printf(" ip-proto-%d", ip
->ip_p
);
602 ipN_print(register const u_char
*bp
, register u_int length
)
606 ip
= (struct ip
*)bp
;
608 (void)printf("truncated-ip %d", length
);
611 memcpy (&hdr
, (char *)ip
, 4);
612 switch (IP_V(&hdr
)) {
614 ip_print (bp
, length
);
618 ip6_print (bp
, length
);
622 (void)printf("unknown ip %d", IP_V(&hdr
));