]>
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.120 2003-02-05 02:30:39 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 */
49 * print the recorded route in an IP RR, LSRR or SSRR option.
52 ip_printroute(const char *type
, register const u_char
*cp
, u_int length
)
54 register u_int ptr
= cp
[2] - 1;
59 printf(" [bad length %d]", length
);
60 if (ptr
< 3 || ((ptr
+ 1) & 3) || ptr
> length
+ 1)
61 printf(" [bad ptr %d]", cp
[2]);
64 for (len
= 3; len
< length
; len
+= 4) {
67 printf("%s%s", type
, ipaddr_string(&cp
[len
]));
70 printf("%s}", ptr
== len
? "#" : "");
74 * If source-routing is present, return the final destination.
75 * Otherwise, return IP destination.
77 * This is used for UDP and TCP pseudo-header in the checksum
81 ip_finddst(const struct ip
*ip
)
88 cp
= (const u_char
*)(ip
+ 1);
89 length
= (IP_HL(ip
) << 2) - sizeof(struct ip
);
91 for (; length
> 0; cp
+= len
, length
-= len
) {
94 if (tt
== IPOPT_NOP
|| tt
== IPOPT_EOL
)
97 if (&cp
[1] >= snapend
) {
105 if (&cp
[1] >= snapend
|| cp
+ len
> snapend
) {
112 memcpy(&retval
, cp
+ len
- 4, 4);
116 return ip
->ip_dst
.s_addr
;
120 ip_printts(register const u_char
*cp
, u_int length
)
122 register u_int ptr
= cp
[2] - 1;
123 register u_int len
= 0;
128 hoplen
= ((cp
[3]&0xF) != IPOPT_TS_TSONLY
) ? 8 : 4;
129 if ((length
- 4) & (hoplen
-1))
130 printf("[bad length %d]", length
);
131 if (ptr
< 4 || ((ptr
- 4) & (hoplen
-1)) || ptr
> length
+ 1)
132 printf("[bad ptr %d]", cp
[2]);
134 case IPOPT_TS_TSONLY
:
137 case IPOPT_TS_TSANDADDR
:
141 * prespecified should really be 3, but some ones might send 2
142 * instead, and the IPOPT_TS_PRESPEC constant can apparently
143 * have both values, so we have to hard-code it here.
147 printf("PRESPEC2.0");
149 case 3: /* IPOPT_TS_PRESPEC */
153 printf("[bad ts type %d]", cp
[3]&0xF);
158 for (len
= 4; len
< length
; len
+= hoplen
) {
161 printf("%s%d@%s", type
, EXTRACT_32BITS(&cp
[len
+hoplen
-4]),
162 hoplen
!=8 ? "" : ipaddr_string(&cp
[len
]));
167 printf("%s", ptr
== len
? " ^ " : "");
170 printf(" [%d hops not recorded]} ", cp
[3]>>4);
179 ip_optprint(register const u_char
*cp
, u_int length
)
183 for (; length
> 0; cp
+= len
, length
-= len
) {
186 if (tt
== IPOPT_NOP
|| tt
== IPOPT_EOL
)
189 if (&cp
[1] >= snapend
) {
196 printf("[|ip op len %d]", len
);
199 if (&cp
[1] >= snapend
|| cp
+ len
> snapend
) {
208 printf("-%d", length
- 1);
219 #ifndef IPOPT_SECURITY
220 #define IPOPT_SECURITY 130
221 #endif /* IPOPT_SECURITY */
223 printf(" SECURITY{%d}", len
);
227 ip_printroute("RR", cp
, len
);
231 ip_printroute("SSRR", cp
, len
);
235 ip_printroute("LSRR", cp
, len
);
239 #define IPOPT_RA 148 /* router alert */
245 else if (cp
[2] || cp
[3])
246 printf("%d.%d", cp
[2], cp
[3]);
250 printf(" IPOPT-%d{%d}", cp
[0], len
);
257 * compute an IP header checksum.
258 * don't modifiy the packet.
261 in_cksum(const u_short
*addr
, register u_int len
, int csum
)
264 const u_short
*w
= addr
;
269 * Our algorithm is simple, using a 32 bit accumulator (sum),
270 * we add sequential 16 bit words to it, and at the end, fold
271 * back all the carry bits from the top 16 bits into the lower
279 sum
+= htons(*(u_char
*)w
<<8);
282 * add back carry outs from top 16 bits to low 16 bits
284 sum
= (sum
>> 16) + (sum
& 0xffff); /* add hi 16 to low 16 */
285 sum
+= (sum
>> 16); /* add carry */
286 answer
= ~sum
; /* truncate to 16 bits */
291 * Given the host-byte-order value of the checksum field in a packet
292 * header, and the network-byte-order computed checksum of the data
293 * that the checksum covers (including the checksum itself), compute
294 * what the checksum field *should* have been.
297 in_cksum_shouldbe(u_int16_t sum
, u_int16_t computed_sum
)
302 * The value that should have gone into the checksum field
303 * is the negative of the value gotten by summing up everything
304 * *but* the checksum field.
306 * We can compute that by subtracting the value of the checksum
307 * field from the sum of all the data in the packet, and then
308 * computing the negative of that value.
310 * "sum" is the value of the checksum field, and "computed_sum"
311 * is the negative of the sum of all the data in the packets,
312 * so that's -(-computed_sum - sum), or (sum + computed_sum).
314 * All the arithmetic in question is one's complement, so the
315 * addition must include an end-around carry; we do this by
316 * doing the arithmetic in 32 bits (with no sign-extension),
317 * and then adding the upper 16 bits of the sum, which contain
318 * the carry, to the lower 16 bits of the sum, and then do it
319 * again in case *that* sum produced a carry.
321 * As RFC 1071 notes, the checksum can be computed without
322 * byte-swapping the 16-bit words; summing 16-bit words
323 * on a big-endian machine gives a big-endian checksum, which
324 * can be directly stuffed into the big-endian checksum fields
325 * in protocol headers, and summing words on a little-endian
326 * machine gives a little-endian checksum, which must be
327 * byte-swapped before being stuffed into a big-endian checksum
330 * "computed_sum" is a network-byte-order value, so we must put
331 * it in host byte order before subtracting it from the
332 * host-byte-order value from the header; the adjusted checksum
333 * will be in host byte order, which is what we'll return.
336 shouldbe
+= ntohs(computed_sum
);
337 shouldbe
= (shouldbe
& 0xFFFF) + (shouldbe
>> 16);
338 shouldbe
= (shouldbe
& 0xFFFF) + (shouldbe
>> 16);
343 * print an IP datagram.
346 ip_print(register const u_char
*bp
, register u_int length
)
348 register const struct ip
*ip
;
349 register u_int hlen
, len
, len0
, off
;
350 register const u_char
*cp
;
353 struct protoent
*proto
;
355 ip
= (const struct ip
*)bp
;
356 if ((u_char
*)(ip
+ 1) > snapend
) {
360 if (length
< sizeof (struct ip
)) {
361 (void)printf("truncated-ip %d", length
);
364 hlen
= IP_HL(ip
) * 4;
365 if (hlen
< sizeof (struct ip
)) {
366 (void)printf("bad-hlen %d", hlen
);
370 len
= EXTRACT_16BITS(&ip
->ip_len
);
372 (void)printf("truncated-ip - %d bytes missing! ",
377 off
= EXTRACT_16BITS(&ip
->ip_off
);
380 (void)printf("(tos 0x%x", (int)ip
->ip_tos
);
382 if (ip
->ip_tos
& 0x03) {
383 switch (ip
->ip_tos
& 0x03) {
385 (void)printf(",ECT(1)");
388 (void)printf(",ECT(0)");
396 (void)printf(", ttl %u", ip
->ip_ttl
);
398 if ((off
& 0x3fff) != 0)
399 (void)printf(", id %u", EXTRACT_16BITS(&ip
->ip_id
));
401 (void)printf(", length: %u", EXTRACT_16BITS(&ip
->ip_len
));
403 if ((hlen
- sizeof(struct ip
)) > 0) {
404 (void)printf(", optlength: %u (", hlen
- (u_int
)sizeof(struct ip
));
405 ip_optprint((u_char
*)(ip
+ 1), hlen
- sizeof(struct ip
));
412 * If this is fragment zero, hand it to the next higher
415 if ((off
& 0x1fff) == 0) {
416 cp
= (const u_char
*)ip
+ hlen
;
420 #define IPPROTO_SCTP 132
422 if (nh
!= IPPROTO_TCP
&& nh
!= IPPROTO_UDP
&&
423 nh
!= IPPROTO_SCTP
) {
424 (void)printf("%s > %s: ", ipaddr_string(&ip
->ip_src
),
425 ipaddr_string(&ip
->ip_dst
));
431 #define IPPROTO_AH 51
435 advance
= ah_print(cp
);
441 #define IPPROTO_ESP 50
446 advance
= esp_print(cp
, (const u_char
*)ip
, &enh
, &padlen
);
448 len
-= advance
+ padlen
;
455 #ifndef IPPROTO_IPCOMP
456 #define IPPROTO_IPCOMP 108
461 advance
= ipcomp_print(cp
, &enh
);
471 sctp_print(cp
, (const u_char
*)ip
, len
);
475 tcp_print(cp
, len
, (const u_char
*)ip
, (off
&~ 0x6000));
479 udp_print(cp
, len
, (const u_char
*)ip
, (off
&~ 0x6000));
483 icmp_print(cp
, len
, (const u_char
*)ip
);
487 #define IPPROTO_IGRP 9
490 igrp_print(cp
, len
, (const u_char
*)ip
);
494 (void)printf(" nd %d", len
);
502 #define IPPROTO_OSPF 89
505 ospf_print(cp
, len
, (const u_char
*)ip
);
509 #define IPPROTO_IGMP 2
516 /* DVMRP multicast tunnel (ip-in-ip encapsulation) */
519 printf(" (ipip-proto-4)");
525 #ifndef IP6PROTO_ENCAP
526 #define IP6PROTO_ENCAP 41
529 /* ip6-in-ip encapsulation */
535 #define IPPROTO_RSVP 46
542 #define IPPROTO_GRE 47
549 #ifndef IPPROTO_MOBILE
550 #define IPPROTO_MOBILE 55
553 mobile_print(cp
, len
);
557 #define IPPROTO_PIM 103
564 #define IPPROTO_VRRP 112
567 vrrp_print(cp
, len
, ip
->ip_ttl
);
571 if ((proto
= getprotobynumber(nh
)) != NULL
)
572 (void)printf(" %s", proto
->p_name
);
574 (void)printf(" ip-proto-%d", nh
);
580 /* Ultra quiet now means that all this stuff should be suppressed */
582 if (qflag
> 1) return;
586 * for fragmented datagrams, print id:size@offset. On all
587 * but the last stick a "+". For unfragmented datagrams, note
588 * the don't fragment flag.
590 len
= len0
; /* get the original length */
593 * if this isn't the first frag, we're missing the
594 * next level protocol header. print the ip addr
598 (void)printf("%s > %s:", ipaddr_string(&ip
->ip_src
),
599 ipaddr_string(&ip
->ip_dst
));
600 if ((proto
= getprotobynumber(ip
->ip_p
)) != NULL
)
601 (void)printf(" %s", proto
->p_name
);
603 (void)printf(" ip-proto-%d", ip
->ip_p
);
611 (void)printf(" (frag %u:%u@%u%s)", EXTRACT_16BITS(&ip
->ip_id
), len
,
613 (off
& IP_MF
)? "+" : "");
615 } else if (off
& IP_DF
)
616 (void)printf(" (DF)");
619 u_int16_t sum
, ip_sum
;
620 const char *sep
= "";
622 if ((u_char
*)ip
+ hlen
<= snapend
) {
623 sum
= in_cksum((const u_short
*)ip
, hlen
, 0);
625 ip_sum
= EXTRACT_16BITS(&ip
->ip_sum
);
626 (void)printf("%sbad cksum %x (->%x)!", sep
,
628 in_cksum_shouldbe(ip_sum
, sum
));
636 ipN_print(register const u_char
*bp
, register u_int length
)
640 ip
= (struct ip
*)bp
;
642 (void)printf("truncated-ip %d", length
);
645 memcpy (&hdr
, (char *)ip
, 4);
646 switch (IP_V(&hdr
)) {
648 ip_print (bp
, length
);
652 ip6_print (bp
, length
);
656 (void)printf("unknown ip %d", IP_V(&hdr
));