]>
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.93 2001-01-28 08:18:27 itojun Exp $ (LBL)";
31 #include <sys/param.h>
33 #include <sys/socket.h>
35 #include <netinet/in.h>
42 #include "addrtoname.h"
43 #include "interface.h"
44 #include "extract.h" /* must come after interface.h */
54 * print the recorded route in an IP RR, LSRR or SSRR option.
57 ip_printroute(const char *type
, register const u_char
*cp
, u_int length
)
59 register u_int ptr
= cp
[2] - 1;
64 printf(" [bad length %d]", length
);
65 if (ptr
< 3 || ((ptr
+ 1) & 3) || ptr
> length
+ 1)
66 printf(" [bad ptr %d]", cp
[2]);
69 for (len
= 3; len
< length
; len
+= 4) {
72 printf("%s%s", type
, ipaddr_string(&cp
[len
]));
75 printf("%s}", ptr
== len
? "#" : "");
79 ip_printts(register const u_char
*cp
, u_int length
)
81 register u_int ptr
= cp
[2] - 1;
82 register u_int len
= 0;
87 hoplen
= ((cp
[3]&0xF) != IPOPT_TS_TSONLY
) ? 8 : 4;
88 if ((length
- 4) & (hoplen
-1))
89 printf("[bad length %d]", length
);
90 if (ptr
< 4 || ((ptr
- 4) & (hoplen
-1)) || ptr
> length
+ 1)
91 printf("[bad ptr %d]", cp
[2]);
96 case IPOPT_TS_TSANDADDR
:
100 * prespecified should really be 3, but some ones might send 2
101 * instead, and the IPOPT_TS_PRESPEC constant can apparently
102 * have both values, so we have to hard-code it here.
106 printf("PRESPEC2.0");
108 case 3: /* IPOPT_TS_PRESPEC */
112 printf("[bad ts type %d]", cp
[3]&0xF);
117 for (len
= 4; len
< length
; len
+= hoplen
) {
120 printf("%s%d@%s", type
, EXTRACT_32BITS(&cp
[len
+hoplen
-4]),
121 hoplen
!=8 ? "" : ipaddr_string(&cp
[len
]));
126 printf("%s", ptr
== len
? " ^ " : "");
129 printf(" [%d hops not recorded]} ", cp
[3]>>4);
138 ip_optprint(register const u_char
*cp
, u_int length
)
142 for (; length
> 0; cp
+= len
, length
-= len
) {
145 if (tt
== IPOPT_NOP
|| tt
== IPOPT_EOL
)
148 if (&cp
[1] >= snapend
) {
155 printf("[|ip op len %d]", len
);
158 if (&cp
[1] >= snapend
|| cp
+ len
> snapend
) {
167 printf("-%d", length
- 1);
178 #ifndef IPOPT_SECURITY
179 #define IPOPT_SECURITY 130
180 #endif /* IPOPT_SECURITY */
182 printf(" SECURITY{%d}", len
);
186 ip_printroute("RR", cp
, len
);
190 ip_printroute("SSRR", cp
, len
);
194 ip_printroute("LSRR", cp
, len
);
198 #define IPOPT_RA 148 /* router alert */
204 else if (cp
[2] || cp
[3])
205 printf("%d.%d", cp
[2], cp
[3]);
209 printf(" IPOPT-%d{%d}", cp
[0], len
);
216 * compute an IP header checksum.
217 * don't modifiy the packet.
220 in_cksum(const u_short
*addr
, register int len
, u_short csum
)
223 const u_short
*w
= addr
;
228 * Our algorithm is simple, using a 32 bit accumulator (sum),
229 * we add sequential 16 bit words to it, and at the end, fold
230 * back all the carry bits from the top 16 bits into the lower
238 sum
+= htons(*(u_char
*)w
<<8);
241 * add back carry outs from top 16 bits to low 16 bits
243 sum
= (sum
>> 16) + (sum
& 0xffff); /* add hi 16 to low 16 */
244 sum
+= (sum
>> 16); /* add carry */
245 answer
= ~sum
; /* truncate to 16 bits */
250 * print an IP datagram.
253 ip_print(register const u_char
*bp
, register u_int length
)
255 register const struct ip
*ip
;
256 register u_int hlen
, len
, len0
, off
;
257 register const u_char
*cp
;
261 ip
= (const struct ip
*)bp
;
264 * If the IP header is not aligned, copy into abuf.
265 * This will never happen with BPF. It does happen raw packet
269 static u_char
*abuf
= NULL
;
270 static int didwarn
= 0;
273 abuf
= (u_char
*)malloc(snaplen
);
275 error("ip_print: malloc");
277 memcpy((char *)abuf
, (char *)ip
, min(length
, snaplen
));
278 snapend
+= abuf
- (u_char
*)ip
;
280 ip
= (struct ip
*)abuf
;
281 /* We really want libpcap to give us aligned packets */
283 warning("compensating for unaligned libpcap packets");
288 if ((u_char
*)(ip
+ 1) > snapend
) {
292 if (length
< sizeof (struct ip
)) {
293 (void)printf("truncated-ip %d", length
);
296 hlen
= IP_HL(ip
) * 4;
297 if (hlen
< sizeof (struct ip
)) {
298 (void)printf("bad-hlen %d", hlen
);
302 len
= ntohs(ip
->ip_len
);
304 (void)printf("truncated-ip - %d bytes missing!",
310 * If this is fragment zero, hand it to the next higher
313 off
= ntohs(ip
->ip_off
);
314 if ((off
& 0x1fff) == 0) {
315 cp
= (const u_char
*)ip
+ hlen
;
318 if (nh
!= IPPROTO_TCP
&& nh
!= IPPROTO_UDP
) {
319 (void)printf("%s > %s: ", ipaddr_string(&ip
->ip_src
),
320 ipaddr_string(&ip
->ip_dst
));
326 #define IPPROTO_AH 51
330 advance
= ah_print(cp
, (const u_char
*)ip
);
336 #define IPPROTO_ESP 50
341 advance
= esp_print(cp
, (const u_char
*)ip
, &enh
);
350 #ifndef IPPROTO_IPCOMP
351 #define IPPROTO_IPCOMP 108
356 advance
= ipcomp_print(cp
, (const u_char
*)ip
, &enh
);
366 tcp_print(cp
, len
, (const u_char
*)ip
, (off
&~ 0x6000));
370 udp_print(cp
, len
, (const u_char
*)ip
, (off
&~ 0x6000));
374 icmp_print(cp
, len
, (const u_char
*)ip
);
378 #define IPPROTO_IGRP 9
381 igrp_print(cp
, len
, (const u_char
*)ip
);
386 (void)printf("%s > %s:", ipaddr_string(&ip
->ip_src
),
387 ipaddr_string(&ip
->ip_dst
));
389 (void)printf(" nd %d", len
);
393 egp_print(cp
, len
, (const u_char
*)ip
);
397 #define IPPROTO_OSPF 89
400 ospf_print(cp
, len
, (const u_char
*)ip
);
404 #define IPPROTO_IGMP 2
407 igmp_print(cp
, len
, (const u_char
*)ip
);
411 /* DVMRP multicast tunnel (ip-in-ip encapsulation) */
414 (void)printf("%s > %s: ",
415 ipaddr_string(&ip
->ip_src
),
416 ipaddr_string(&ip
->ip_dst
));
426 #ifndef IP6PROTO_ENCAP
427 #define IP6PROTO_ENCAP 41
430 /* ip6-in-ip encapsulation */
433 (void)printf("%s > %s: ",
434 ipaddr_string(&ip
->ip_src
),
435 ipaddr_string(&ip
->ip_dst
));
447 #define IPPROTO_GRE 47
451 (void)printf("gre %s > %s: ",
452 ipaddr_string(&ip
->ip_src
),
453 ipaddr_string(&ip
->ip_dst
));
457 printf(" (gre encap)");
462 #ifndef IPPROTO_MOBILE
463 #define IPPROTO_MOBILE 55
467 (void)printf("mobile %s > %s: ",
468 ipaddr_string(&ip
->ip_src
),
469 ipaddr_string(&ip
->ip_dst
));
470 mobile_print(cp
, len
);
472 printf(" (mobile encap)");
478 #define IPPROTO_PIM 103
485 #define IPPROTO_VRRP 112
489 (void)printf("vrrp %s > %s: ",
490 ipaddr_string(&ip
->ip_src
),
491 ipaddr_string(&ip
->ip_dst
));
492 vrrp_print(cp
, len
, ip
->ip_ttl
);
497 (void)printf("%s > %s:", ipaddr_string(&ip
->ip_src
),
498 ipaddr_string(&ip
->ip_dst
));
500 (void)printf(" ip-proto-%d %d", nh
, len
);
505 /* Ultra quiet now means that all this stuff should be suppressed */
507 if (qflag
> 1) return;
511 * for fragmented datagrams, print id:size@offset. On all
512 * but the last stick a "+". For unfragmented datagrams, note
513 * the don't fragment flag.
515 len
= len0
; /* get the original length */
518 * if this isn't the first frag, we're missing the
519 * next level protocol header. print the ip addr.
522 (void)printf("%s > %s:", ipaddr_string(&ip
->ip_src
),
523 ipaddr_string(&ip
->ip_dst
));
530 (void)printf(" (frag %d:%u@%d%s)", ntohs(ip
->ip_id
), len
,
532 (off
& IP_MF
)? "+" : "");
534 } else if (off
& IP_DF
)
535 (void)printf(" (DF)");
538 (void)printf(" [tos 0x%x", (int)ip
->ip_tos
);
540 if (ip
->ip_tos
&0x02) {
541 (void)printf(",ECT");
549 (void)printf(" [ttl %d]", (int)ip
->ip_ttl
);
556 if (ip
->ip_ttl
> 1) {
557 (void)printf("%sttl %d", sep
, (int)ip
->ip_ttl
);
560 if ((off
& 0x3fff) == 0) {
561 (void)printf("%sid %d", sep
, (int)ntohs(ip
->ip_id
));
564 (void)printf("%slen %d", sep
, (int)ntohs(ip
->ip_len
));
566 if ((u_char
*)ip
+ hlen
<= snapend
) {
567 sum
= in_cksum((const u_short
*)ip
, hlen
, 0);
569 (void)printf("%sbad cksum %x!", sep
,
574 if ((hlen
-= sizeof(struct ip
)) > 0) {
575 (void)printf("%soptlen=%d", sep
, hlen
);
576 ip_optprint((u_char
*)(ip
+ 1), hlen
);
583 ipN_print(register const u_char
*bp
, register u_int length
)
587 ip
= (struct ip
*)bp
;
589 (void)printf("truncated-ip %d", length
);
592 memcpy (&hdr
, (char *)ip
, 4);
593 switch (IP_V(&hdr
)) {
595 ip_print (bp
, length
);
599 ip6_print (bp
, length
);
603 (void)printf("unknown ip %d", IP_V(&hdr
));