]>
The Tcpdump Group git mirrors - tcpdump/blob - print-ip.c
0ab20d3b15b1cfc09ea97878169d913bb3b92794
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.110 2002-07-28 04:14:21 fenner Exp $ (LBL)";
31 #include <sys/param.h>
33 #include <sys/socket.h>
35 #include <netinet/in.h>
43 #include "addrtoname.h"
44 #include "interface.h"
45 #include "extract.h" /* must come after interface.h */
55 * print the recorded route in an IP RR, LSRR or SSRR option.
58 ip_printroute(const char *type
, register const u_char
*cp
, u_int length
)
60 register u_int ptr
= cp
[2] - 1;
65 printf(" [bad length %d]", length
);
66 if (ptr
< 3 || ((ptr
+ 1) & 3) || ptr
> length
+ 1)
67 printf(" [bad ptr %d]", cp
[2]);
70 for (len
= 3; len
< length
; len
+= 4) {
73 printf("%s%s", type
, ipaddr_string(&cp
[len
]));
76 printf("%s}", ptr
== len
? "#" : "");
80 * If source-routing is present, return the final destination.
81 * Otherwise, return IP destination.
83 * This is used for UDP and TCP pseudo-header in the checksum
87 ip_finddst(const struct ip
*ip
)
94 cp
= (const u_char
*)(ip
+ 1);
95 length
= (IP_HL(ip
) << 2) - sizeof(struct ip
);
97 for (; length
> 0; cp
+= len
, length
-= len
) {
100 if (tt
== IPOPT_NOP
|| tt
== IPOPT_EOL
)
103 if (&cp
[1] >= snapend
) {
111 if (&cp
[1] >= snapend
|| cp
+ len
> snapend
) {
118 memcpy(&retval
, cp
+ len
- 4, 4);
122 return ip
->ip_dst
.s_addr
;
126 ip_printts(register const u_char
*cp
, u_int length
)
128 register u_int ptr
= cp
[2] - 1;
129 register u_int len
= 0;
134 hoplen
= ((cp
[3]&0xF) != IPOPT_TS_TSONLY
) ? 8 : 4;
135 if ((length
- 4) & (hoplen
-1))
136 printf("[bad length %d]", length
);
137 if (ptr
< 4 || ((ptr
- 4) & (hoplen
-1)) || ptr
> length
+ 1)
138 printf("[bad ptr %d]", cp
[2]);
140 case IPOPT_TS_TSONLY
:
143 case IPOPT_TS_TSANDADDR
:
147 * prespecified should really be 3, but some ones might send 2
148 * instead, and the IPOPT_TS_PRESPEC constant can apparently
149 * have both values, so we have to hard-code it here.
153 printf("PRESPEC2.0");
155 case 3: /* IPOPT_TS_PRESPEC */
159 printf("[bad ts type %d]", cp
[3]&0xF);
164 for (len
= 4; len
< length
; len
+= hoplen
) {
167 printf("%s%d@%s", type
, EXTRACT_32BITS(&cp
[len
+hoplen
-4]),
168 hoplen
!=8 ? "" : ipaddr_string(&cp
[len
]));
173 printf("%s", ptr
== len
? " ^ " : "");
176 printf(" [%d hops not recorded]} ", cp
[3]>>4);
185 ip_optprint(register const u_char
*cp
, u_int length
)
189 for (; length
> 0; cp
+= len
, length
-= len
) {
192 if (tt
== IPOPT_NOP
|| tt
== IPOPT_EOL
)
195 if (&cp
[1] >= snapend
) {
202 printf("[|ip op len %d]", len
);
205 if (&cp
[1] >= snapend
|| cp
+ len
> snapend
) {
214 printf("-%d", length
- 1);
225 #ifndef IPOPT_SECURITY
226 #define IPOPT_SECURITY 130
227 #endif /* IPOPT_SECURITY */
229 printf(" SECURITY{%d}", len
);
233 ip_printroute("RR", cp
, len
);
237 ip_printroute("SSRR", cp
, len
);
241 ip_printroute("LSRR", cp
, len
);
245 #define IPOPT_RA 148 /* router alert */
251 else if (cp
[2] || cp
[3])
252 printf("%d.%d", cp
[2], cp
[3]);
256 printf(" IPOPT-%d{%d}", cp
[0], len
);
263 * compute an IP header checksum.
264 * don't modifiy the packet.
267 in_cksum(const u_short
*addr
, register u_int len
, int csum
)
270 const u_short
*w
= addr
;
275 * Our algorithm is simple, using a 32 bit accumulator (sum),
276 * we add sequential 16 bit words to it, and at the end, fold
277 * back all the carry bits from the top 16 bits into the lower
285 sum
+= htons(*(u_char
*)w
<<8);
288 * add back carry outs from top 16 bits to low 16 bits
290 sum
= (sum
>> 16) + (sum
& 0xffff); /* add hi 16 to low 16 */
291 sum
+= (sum
>> 16); /* add carry */
292 answer
= ~sum
; /* truncate to 16 bits */
297 * Given the host-byte-order value of the checksum field in a packet
298 * header, and the network-byte-order computed checksum of the data
299 * that the checksum covers (including the checksum itself), compute
300 * what the checksum field *should* have been.
303 in_cksum_shouldbe(u_int16_t sum
, u_int16_t computed_sum
)
308 * The value that should have gone into the checksum field
309 * is the negative of the value gotten by summing up everything
310 * *but* the checksum field.
312 * We can compute that by subtracting the value of the checksum
313 * field from the sum of all the data in the packet, and then
314 * computing the negative of that value.
316 * "sum" is the value of the checksum field, and "computed_sum"
317 * is the negative of the sum of all the data in the packets,
318 * so that's -(-computed_sum - sum), or (sum + computed_sum).
320 * All the arithmetic in question is one's complement, so the
321 * addition must include an end-around carry; we do this by
322 * doing the arithmetic in 32 bits (with no sign-extension),
323 * and then adding the upper 16 bits of the sum, which contain
324 * the carry, to the lower 16 bits of the sum, and then do it
325 * again in case *that* sum produced a carry.
327 * As RFC 1071 notes, the checksum can be computed without
328 * byte-swapping the 16-bit words; summing 16-bit words
329 * on a big-endian machine gives a big-endian checksum, which
330 * can be directly stuffed into the big-endian checksum fields
331 * in protocol headers, and summing words on a little-endian
332 * machine gives a little-endian checksum, which must be
333 * byte-swapped before being stuffed into a big-endian checksum
336 * "computed_sum" is a network-byte-order value, so we must put
337 * it in host byte order before subtracting it from the
338 * host-byte-order value from the header; the adjusted checksum
339 * will be in host byte order, which is what we'll return.
342 shouldbe
+= ntohs(computed_sum
);
343 shouldbe
= (shouldbe
& 0xFFFF) + (shouldbe
>> 16);
344 shouldbe
= (shouldbe
& 0xFFFF) + (shouldbe
>> 16);
349 * print an IP datagram.
352 ip_print(register const u_char
*bp
, register u_int length
)
354 register const struct ip
*ip
;
355 register u_int hlen
, len
, len0
, off
;
356 register const u_char
*cp
;
359 struct protoent
*proto
;
361 ip
= (const struct ip
*)bp
;
364 * If the IP header is not aligned, copy into abuf.
367 static u_char
*abuf
= NULL
;
368 static int didwarn
= 0;
371 abuf
= (u_char
*)malloc(snaplen
);
373 error("ip_print: malloc");
375 memcpy((char *)abuf
, (char *)ip
, min(length
, snaplen
));
376 snapend
+= abuf
- (u_char
*)ip
;
378 ip
= (struct ip
*)abuf
;
379 /* We really want libpcap to give us aligned packets */
381 warning("compensating for unaligned libpcap packets");
386 if ((u_char
*)(ip
+ 1) > snapend
) {
390 if (length
< sizeof (struct ip
)) {
391 (void)printf("truncated-ip %d", length
);
394 hlen
= IP_HL(ip
) * 4;
395 if (hlen
< sizeof (struct ip
)) {
396 (void)printf("bad-hlen %d", hlen
);
400 len
= ntohs(ip
->ip_len
);
402 (void)printf("truncated-ip - %d bytes missing! ",
409 off
= ntohs(ip
->ip_off
);
412 (void)printf("(tos 0x%x", (int)ip
->ip_tos
);
414 if (ip
->ip_tos
& 0x03) {
415 switch (ip
->ip_tos
& 0x03) {
417 (void)printf(",ECT(1)");
420 (void)printf(",ECT(0)");
428 (void)printf(", ttl %d", (int)ip
->ip_ttl
);
430 if ((off
& 0x3fff) == 0)
431 (void)printf(", id %d", (int)ntohs(ip
->ip_id
));
432 (void)printf(", len %d) ", (int)ntohs(ip
->ip_len
));
436 * If this is fragment zero, hand it to the next higher
439 if ((off
& 0x1fff) == 0) {
440 cp
= (const u_char
*)ip
+ hlen
;
444 #define IPPROTO_SCTP 132
446 if (nh
!= IPPROTO_TCP
&& nh
!= IPPROTO_UDP
&&
447 nh
!= IPPROTO_SCTP
) {
448 (void)printf("%s > %s: ", ipaddr_string(&ip
->ip_src
),
449 ipaddr_string(&ip
->ip_dst
));
455 #define IPPROTO_AH 51
459 advance
= ah_print(cp
, (const u_char
*)ip
);
465 #define IPPROTO_ESP 50
470 advance
= esp_print(cp
, (const u_char
*)ip
, &enh
, &padlen
);
472 len
-= advance
+ padlen
;
479 #ifndef IPPROTO_IPCOMP
480 #define IPPROTO_IPCOMP 108
485 advance
= ipcomp_print(cp
, (const u_char
*)ip
, &enh
);
495 sctp_print(cp
, (const u_char
*)ip
, len
);
499 tcp_print(cp
, len
, (const u_char
*)ip
, (off
&~ 0x6000));
503 udp_print(cp
, len
, (const u_char
*)ip
, (off
&~ 0x6000));
507 icmp_print(cp
, len
, (const u_char
*)ip
);
511 #define IPPROTO_IGRP 9
514 igrp_print(cp
, len
, (const u_char
*)ip
);
518 (void)printf(" nd %d", len
);
522 egp_print(cp
, len
, (const u_char
*)ip
);
526 #define IPPROTO_OSPF 89
529 ospf_print(cp
, len
, (const u_char
*)ip
);
533 #define IPPROTO_IGMP 2
540 /* DVMRP multicast tunnel (ip-in-ip encapsulation) */
543 printf(" (ipip-proto-4)");
549 #ifndef IP6PROTO_ENCAP
550 #define IP6PROTO_ENCAP 41
553 /* ip6-in-ip encapsulation */
560 #define IPPROTO_GRE 47
567 #ifndef IPPROTO_MOBILE
568 #define IPPROTO_MOBILE 55
571 mobile_print(cp
, len
);
575 #define IPPROTO_PIM 103
582 #define IPPROTO_VRRP 112
585 vrrp_print(cp
, len
, ip
->ip_ttl
);
589 if ((proto
= getprotobynumber(nh
)) != NULL
)
590 (void)printf(" %s", proto
->p_name
);
592 (void)printf(" ip-proto-%d", nh
);
598 /* Ultra quiet now means that all this stuff should be suppressed */
600 if (qflag
> 1) return;
604 * for fragmented datagrams, print id:size@offset. On all
605 * but the last stick a "+". For unfragmented datagrams, note
606 * the don't fragment flag.
608 len
= len0
; /* get the original length */
611 * if this isn't the first frag, we're missing the
612 * next level protocol header. print the ip addr
616 (void)printf("%s > %s:", ipaddr_string(&ip
->ip_src
),
617 ipaddr_string(&ip
->ip_dst
));
618 if ((proto
= getprotobynumber(ip
->ip_p
)) != NULL
)
619 (void)printf(" %s", proto
->p_name
);
621 (void)printf(" ip-proto-%d", ip
->ip_p
);
629 (void)printf(" (frag %d:%u@%d%s)", ntohs(ip
->ip_id
), len
,
631 (off
& IP_MF
)? "+" : "");
633 } else if (off
& IP_DF
)
634 (void)printf(" (DF)");
637 u_int16_t sum
, ip_sum
;
640 if ((u_char
*)ip
+ hlen
<= snapend
) {
641 sum
= in_cksum((const u_short
*)ip
, hlen
, 0);
643 ip_sum
= ntohs(ip
->ip_sum
);
644 (void)printf("%sbad cksum %x (->%x)!", sep
,
646 in_cksum_shouldbe(ip_sum
, sum
));
650 if ((hlen
-= sizeof(struct ip
)) > 0) {
651 (void)printf("%soptlen=%d", sep
, hlen
);
652 ip_optprint((u_char
*)(ip
+ 1), hlen
);
659 ipN_print(register const u_char
*bp
, register u_int length
)
663 ip
= (struct ip
*)bp
;
665 (void)printf("truncated-ip %d", length
);
668 memcpy (&hdr
, (char *)ip
, 4);
669 switch (IP_V(&hdr
)) {
671 ip_print (bp
, length
);
675 ip6_print (bp
, length
);
679 (void)printf("unknown ip %d", IP_V(&hdr
));