]>
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.109 2002-07-21 20:48:26 guy 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 ip_printts(register const u_char
*cp
, u_int length
)
82 register u_int ptr
= cp
[2] - 1;
83 register u_int len
= 0;
88 hoplen
= ((cp
[3]&0xF) != IPOPT_TS_TSONLY
) ? 8 : 4;
89 if ((length
- 4) & (hoplen
-1))
90 printf("[bad length %d]", length
);
91 if (ptr
< 4 || ((ptr
- 4) & (hoplen
-1)) || ptr
> length
+ 1)
92 printf("[bad ptr %d]", cp
[2]);
97 case IPOPT_TS_TSANDADDR
:
101 * prespecified should really be 3, but some ones might send 2
102 * instead, and the IPOPT_TS_PRESPEC constant can apparently
103 * have both values, so we have to hard-code it here.
107 printf("PRESPEC2.0");
109 case 3: /* IPOPT_TS_PRESPEC */
113 printf("[bad ts type %d]", cp
[3]&0xF);
118 for (len
= 4; len
< length
; len
+= hoplen
) {
121 printf("%s%d@%s", type
, EXTRACT_32BITS(&cp
[len
+hoplen
-4]),
122 hoplen
!=8 ? "" : ipaddr_string(&cp
[len
]));
127 printf("%s", ptr
== len
? " ^ " : "");
130 printf(" [%d hops not recorded]} ", cp
[3]>>4);
139 ip_optprint(register const u_char
*cp
, u_int length
)
143 for (; length
> 0; cp
+= len
, length
-= len
) {
146 if (tt
== IPOPT_NOP
|| tt
== IPOPT_EOL
)
149 if (&cp
[1] >= snapend
) {
156 printf("[|ip op len %d]", len
);
159 if (&cp
[1] >= snapend
|| cp
+ len
> snapend
) {
168 printf("-%d", length
- 1);
179 #ifndef IPOPT_SECURITY
180 #define IPOPT_SECURITY 130
181 #endif /* IPOPT_SECURITY */
183 printf(" SECURITY{%d}", len
);
187 ip_printroute("RR", cp
, len
);
191 ip_printroute("SSRR", cp
, len
);
195 ip_printroute("LSRR", cp
, len
);
199 #define IPOPT_RA 148 /* router alert */
205 else if (cp
[2] || cp
[3])
206 printf("%d.%d", cp
[2], cp
[3]);
210 printf(" IPOPT-%d{%d}", cp
[0], len
);
217 * compute an IP header checksum.
218 * don't modifiy the packet.
221 in_cksum(const u_short
*addr
, register u_int len
, int csum
)
224 const u_short
*w
= addr
;
229 * Our algorithm is simple, using a 32 bit accumulator (sum),
230 * we add sequential 16 bit words to it, and at the end, fold
231 * back all the carry bits from the top 16 bits into the lower
239 sum
+= htons(*(u_char
*)w
<<8);
242 * add back carry outs from top 16 bits to low 16 bits
244 sum
= (sum
>> 16) + (sum
& 0xffff); /* add hi 16 to low 16 */
245 sum
+= (sum
>> 16); /* add carry */
246 answer
= ~sum
; /* truncate to 16 bits */
251 * Given the host-byte-order value of the checksum field in a packet
252 * header, and the network-byte-order computed checksum of the data
253 * that the checksum covers (including the checksum itself), compute
254 * what the checksum field *should* have been.
257 in_cksum_shouldbe(u_int16_t sum
, u_int16_t computed_sum
)
262 * The value that should have gone into the checksum field
263 * is the negative of the value gotten by summing up everything
264 * *but* the checksum field.
266 * We can compute that by subtracting the value of the checksum
267 * field from the sum of all the data in the packet, and then
268 * computing the negative of that value.
270 * "sum" is the value of the checksum field, and "computed_sum"
271 * is the negative of the sum of all the data in the packets,
272 * so that's -(-computed_sum - sum), or (sum + computed_sum).
274 * All the arithmetic in question is one's complement, so the
275 * addition must include an end-around carry; we do this by
276 * doing the arithmetic in 32 bits (with no sign-extension),
277 * and then adding the upper 16 bits of the sum, which contain
278 * the carry, to the lower 16 bits of the sum, and then do it
279 * again in case *that* sum produced a carry.
281 * As RFC 1071 notes, the checksum can be computed without
282 * byte-swapping the 16-bit words; summing 16-bit words
283 * on a big-endian machine gives a big-endian checksum, which
284 * can be directly stuffed into the big-endian checksum fields
285 * in protocol headers, and summing words on a little-endian
286 * machine gives a little-endian checksum, which must be
287 * byte-swapped before being stuffed into a big-endian checksum
290 * "computed_sum" is a network-byte-order value, so we must put
291 * it in host byte order before subtracting it from the
292 * host-byte-order value from the header; the adjusted checksum
293 * will be in host byte order, which is what we'll return.
296 shouldbe
+= ntohs(computed_sum
);
297 shouldbe
= (shouldbe
& 0xFFFF) + (shouldbe
>> 16);
298 shouldbe
= (shouldbe
& 0xFFFF) + (shouldbe
>> 16);
303 * print an IP datagram.
306 ip_print(register const u_char
*bp
, register u_int length
)
308 register const struct ip
*ip
;
309 register u_int hlen
, len
, len0
, off
;
310 register const u_char
*cp
;
313 struct protoent
*proto
;
315 ip
= (const struct ip
*)bp
;
318 * If the IP header is not aligned, copy into abuf.
321 static u_char
*abuf
= NULL
;
322 static int didwarn
= 0;
325 abuf
= (u_char
*)malloc(snaplen
);
327 error("ip_print: malloc");
329 memcpy((char *)abuf
, (char *)ip
, min(length
, snaplen
));
330 snapend
+= abuf
- (u_char
*)ip
;
332 ip
= (struct ip
*)abuf
;
333 /* We really want libpcap to give us aligned packets */
335 warning("compensating for unaligned libpcap packets");
340 if ((u_char
*)(ip
+ 1) > snapend
) {
344 if (length
< sizeof (struct ip
)) {
345 (void)printf("truncated-ip %d", length
);
348 hlen
= IP_HL(ip
) * 4;
349 if (hlen
< sizeof (struct ip
)) {
350 (void)printf("bad-hlen %d", hlen
);
354 len
= ntohs(ip
->ip_len
);
356 (void)printf("truncated-ip - %d bytes missing! ",
363 off
= ntohs(ip
->ip_off
);
366 (void)printf("(tos 0x%x", (int)ip
->ip_tos
);
368 if (ip
->ip_tos
& 0x03) {
369 switch (ip
->ip_tos
& 0x03) {
371 (void)printf(",ECT(1)");
374 (void)printf(",ECT(0)");
382 (void)printf(", ttl %d", (int)ip
->ip_ttl
);
384 if ((off
& 0x3fff) == 0)
385 (void)printf(", id %d", (int)ntohs(ip
->ip_id
));
386 (void)printf(", len %d) ", (int)ntohs(ip
->ip_len
));
390 * If this is fragment zero, hand it to the next higher
393 if ((off
& 0x1fff) == 0) {
394 cp
= (const u_char
*)ip
+ hlen
;
398 #define IPPROTO_SCTP 132
400 if (nh
!= IPPROTO_TCP
&& nh
!= IPPROTO_UDP
&&
401 nh
!= IPPROTO_SCTP
) {
402 (void)printf("%s > %s: ", ipaddr_string(&ip
->ip_src
),
403 ipaddr_string(&ip
->ip_dst
));
409 #define IPPROTO_AH 51
413 advance
= ah_print(cp
, (const u_char
*)ip
);
419 #define IPPROTO_ESP 50
424 advance
= esp_print(cp
, (const u_char
*)ip
, &enh
, &padlen
);
426 len
-= advance
+ padlen
;
433 #ifndef IPPROTO_IPCOMP
434 #define IPPROTO_IPCOMP 108
439 advance
= ipcomp_print(cp
, (const u_char
*)ip
, &enh
);
449 sctp_print(cp
, (const u_char
*)ip
, len
);
453 tcp_print(cp
, len
, (const u_char
*)ip
, (off
&~ 0x6000));
457 udp_print(cp
, len
, (const u_char
*)ip
, (off
&~ 0x6000));
461 icmp_print(cp
, len
, (const u_char
*)ip
);
465 #define IPPROTO_IGRP 9
468 igrp_print(cp
, len
, (const u_char
*)ip
);
472 (void)printf(" nd %d", len
);
476 egp_print(cp
, len
, (const u_char
*)ip
);
480 #define IPPROTO_OSPF 89
483 ospf_print(cp
, len
, (const u_char
*)ip
);
487 #define IPPROTO_IGMP 2
494 /* DVMRP multicast tunnel (ip-in-ip encapsulation) */
497 printf(" (ipip-proto-4)");
503 #ifndef IP6PROTO_ENCAP
504 #define IP6PROTO_ENCAP 41
507 /* ip6-in-ip encapsulation */
514 #define IPPROTO_GRE 47
521 #ifndef IPPROTO_MOBILE
522 #define IPPROTO_MOBILE 55
525 mobile_print(cp
, len
);
529 #define IPPROTO_PIM 103
536 #define IPPROTO_VRRP 112
539 vrrp_print(cp
, len
, ip
->ip_ttl
);
543 if ((proto
= getprotobynumber(nh
)) != NULL
)
544 (void)printf(" %s", proto
->p_name
);
546 (void)printf(" ip-proto-%d", nh
);
552 /* Ultra quiet now means that all this stuff should be suppressed */
554 if (qflag
> 1) return;
558 * for fragmented datagrams, print id:size@offset. On all
559 * but the last stick a "+". For unfragmented datagrams, note
560 * the don't fragment flag.
562 len
= len0
; /* get the original length */
565 * if this isn't the first frag, we're missing the
566 * next level protocol header. print the ip addr
570 (void)printf("%s > %s:", ipaddr_string(&ip
->ip_src
),
571 ipaddr_string(&ip
->ip_dst
));
572 if ((proto
= getprotobynumber(ip
->ip_p
)) != NULL
)
573 (void)printf(" %s", proto
->p_name
);
575 (void)printf(" ip-proto-%d", ip
->ip_p
);
583 (void)printf(" (frag %d:%u@%d%s)", ntohs(ip
->ip_id
), len
,
585 (off
& IP_MF
)? "+" : "");
587 } else if (off
& IP_DF
)
588 (void)printf(" (DF)");
591 u_int16_t sum
, ip_sum
;
594 if ((u_char
*)ip
+ hlen
<= snapend
) {
595 sum
= in_cksum((const u_short
*)ip
, hlen
, 0);
597 ip_sum
= ntohs(ip
->ip_sum
);
598 (void)printf("%sbad cksum %x (->%x)!", sep
,
600 in_cksum_shouldbe(ip_sum
, sum
));
604 if ((hlen
-= sizeof(struct ip
)) > 0) {
605 (void)printf("%soptlen=%d", sep
, hlen
);
606 ip_optprint((u_char
*)(ip
+ 1), hlen
);
613 ipN_print(register const u_char
*bp
, register u_int length
)
617 ip
= (struct ip
*)bp
;
619 (void)printf("truncated-ip %d", length
);
622 memcpy (&hdr
, (char *)ip
, 4);
623 switch (IP_V(&hdr
)) {
625 ip_print (bp
, length
);
629 ip6_print (bp
, length
);
633 (void)printf("unknown ip %d", IP_V(&hdr
));