]>
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.101 2001-10-27 07:42:46 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 * print an IP datagram.
254 ip_print(register const u_char
*bp
, register u_int length
)
256 register const struct ip
*ip
;
257 register u_int hlen
, len
, len0
, off
;
258 register const u_char
*cp
;
261 struct protoent
*proto
;
263 ip
= (const struct ip
*)bp
;
266 * If the IP header is not aligned, copy into abuf.
267 * This will never happen with BPF. It does happen raw packet
271 static u_char
*abuf
= NULL
;
272 static int didwarn
= 0;
275 abuf
= (u_char
*)malloc(snaplen
);
277 error("ip_print: malloc");
279 memcpy((char *)abuf
, (char *)ip
, min(length
, snaplen
));
280 snapend
+= abuf
- (u_char
*)ip
;
282 ip
= (struct ip
*)abuf
;
283 /* We really want libpcap to give us aligned packets */
285 warning("compensating for unaligned libpcap packets");
290 if ((u_char
*)(ip
+ 1) > snapend
) {
294 if (length
< sizeof (struct ip
)) {
295 (void)printf("truncated-ip %d", length
);
298 hlen
= IP_HL(ip
) * 4;
299 if (hlen
< sizeof (struct ip
)) {
300 (void)printf("bad-hlen %d", hlen
);
304 len
= ntohs(ip
->ip_len
);
306 (void)printf("truncated-ip - %d bytes missing! ",
312 * If this is fragment zero, hand it to the next higher
315 off
= ntohs(ip
->ip_off
);
316 if ((off
& 0x1fff) == 0) {
317 cp
= (const u_char
*)ip
+ hlen
;
321 #define IPPROTO_SCTP 132
323 if (nh
!= IPPROTO_TCP
&& nh
!= IPPROTO_UDP
&&
324 nh
!= IPPROTO_SCTP
) {
325 (void)printf("%s > %s: ", ipaddr_string(&ip
->ip_src
),
326 ipaddr_string(&ip
->ip_dst
));
332 #define IPPROTO_AH 51
336 advance
= ah_print(cp
, (const u_char
*)ip
);
342 #define IPPROTO_ESP 50
347 advance
= esp_print(cp
, (const u_char
*)ip
, &enh
, &padlen
);
349 len
-= advance
+ padlen
;
356 #ifndef IPPROTO_IPCOMP
357 #define IPPROTO_IPCOMP 108
362 advance
= ipcomp_print(cp
, (const u_char
*)ip
, &enh
);
372 sctp_print(cp
, (const u_char
*)ip
, len
);
376 tcp_print(cp
, len
, (const u_char
*)ip
, (off
&~ 0x6000));
380 udp_print(cp
, len
, (const u_char
*)ip
, (off
&~ 0x6000));
384 icmp_print(cp
, len
, (const u_char
*)ip
);
388 #define IPPROTO_IGRP 9
391 igrp_print(cp
, len
, (const u_char
*)ip
);
395 (void)printf(" nd %d", len
);
399 egp_print(cp
, len
, (const u_char
*)ip
);
403 #define IPPROTO_OSPF 89
406 ospf_print(cp
, len
, (const u_char
*)ip
);
410 #define IPPROTO_IGMP 2
417 /* DVMRP multicast tunnel (ip-in-ip encapsulation) */
420 printf(" (ipip-proto-4)");
426 #ifndef IP6PROTO_ENCAP
427 #define IP6PROTO_ENCAP 41
430 /* ip6-in-ip encapsulation */
437 #define IPPROTO_GRE 47
444 #ifndef IPPROTO_MOBILE
445 #define IPPROTO_MOBILE 55
448 mobile_print(cp
, len
);
452 #define IPPROTO_PIM 103
459 #define IPPROTO_VRRP 112
462 vrrp_print(cp
, len
, ip
->ip_ttl
);
466 if ((proto
= getprotobynumber(nh
)) != NULL
)
467 (void)printf(" %s", proto
->p_name
);
469 (void)printf(" ip-proto-%d", nh
);
475 /* Ultra quiet now means that all this stuff should be suppressed */
477 if (qflag
> 1) return;
481 * for fragmented datagrams, print id:size@offset. On all
482 * but the last stick a "+". For unfragmented datagrams, note
483 * the don't fragment flag.
485 len
= len0
; /* get the original length */
488 * if this isn't the first frag, we're missing the
489 * next level protocol header. print the ip addr
493 (void)printf("%s > %s:", ipaddr_string(&ip
->ip_src
),
494 ipaddr_string(&ip
->ip_dst
));
495 if ((proto
= getprotobynumber(ip
->ip_p
)) != NULL
)
496 (void)printf(" %s", proto
->p_name
);
498 (void)printf(" ip-proto-%d", ip
->ip_p
);
506 (void)printf(" (frag %d:%u@%d%s)", ntohs(ip
->ip_id
), len
,
508 (off
& IP_MF
)? "+" : "");
510 } else if (off
& IP_DF
)
511 (void)printf(" (DF)");
514 (void)printf(" [tos 0x%x", (int)ip
->ip_tos
);
516 if (ip
->ip_tos
& 0x03) {
517 switch (ip
->ip_tos
& 0x03) {
519 (void)printf(",ECT(1)");
522 (void)printf(",ECT(0)");
532 (void)printf(" [ttl %d]", (int)ip
->ip_ttl
);
539 if (ip
->ip_ttl
> 1) {
540 (void)printf("%sttl %d", sep
, (int)ip
->ip_ttl
);
543 if ((off
& 0x3fff) == 0) {
544 (void)printf("%sid %d", sep
, (int)ntohs(ip
->ip_id
));
547 (void)printf("%slen %d", sep
, (int)ntohs(ip
->ip_len
));
549 if ((u_char
*)ip
+ hlen
<= snapend
) {
550 sum
= in_cksum((const u_short
*)ip
, hlen
, 0);
552 (void)printf("%sbad cksum %x!", sep
,
557 if ((hlen
-= sizeof(struct ip
)) > 0) {
558 (void)printf("%soptlen=%d", sep
, hlen
);
559 ip_optprint((u_char
*)(ip
+ 1), hlen
);
566 ipN_print(register const u_char
*bp
, register u_int length
)
570 ip
= (struct ip
*)bp
;
572 (void)printf("truncated-ip %d", length
);
575 memcpy (&hdr
, (char *)ip
, 4);
576 switch (IP_V(&hdr
)) {
578 ip_print (bp
, length
);
582 ip6_print (bp
, length
);
586 (void)printf("unknown ip %d", IP_V(&hdr
));