]>
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.88 2000-09-29 04:58:40 guy 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 #define IN_CLASSD(i) (((int32_t)(i) & 0xf0000000) == 0xe0000000)
57 /* (following from ipmulti/mrouted/prune.h) */
60 * The packet format for a traceroute request.
63 u_int tr_src
; /* traceroute source */
64 u_int tr_dst
; /* traceroute destination */
65 u_int tr_raddr
; /* traceroute response address */
66 u_int tr_rttlqid
; /* response ttl and qid */
69 #define TR_GETTTL(x) (int)(((x) >> 24) & 0xff)
70 #define TR_GETQID(x) ((x) & 0x00ffffff)
73 * Traceroute response format. A traceroute response has a tr_query at the
74 * beginning, followed by one tr_resp for each hop taken.
77 u_int tr_qarr
; /* query arrival time */
78 u_int tr_inaddr
; /* incoming interface address */
79 u_int tr_outaddr
; /* outgoing interface address */
80 u_int tr_rmtaddr
; /* parent address in source tree */
81 u_int tr_vifin
; /* input packet count on interface */
82 u_int tr_vifout
; /* output packet count on interface */
83 u_int tr_pktcnt
; /* total incoming packets for src-grp */
84 u_char tr_rproto
; /* routing proto deployed on router */
85 u_char tr_fttl
; /* ttl required to forward on outvif */
86 u_char tr_smask
; /* subnet mask for src addr */
87 u_char tr_rflags
; /* forwarding error codes */
90 /* defs within mtrace */
94 /* fields for tr_rflags (forwarding error codes) */
102 #define TR_NO_SPACE 0x81
103 #define TR_OLD_ROUTER 0x82
105 /* fields for tr_rproto (routing protocol) */
106 #define TR_PROTO_DVMRP 1
107 #define TR_PROTO_MOSPF 2
108 #define TR_PROTO_PIM 3
109 #define TR_PROTO_CBT 4
111 static void print_mtrace(register const u_char
*bp
, register u_int len
)
113 register struct tr_query
*tr
= (struct tr_query
*)(bp
+ 8);
115 printf("mtrace %lu: %s to %s reply-to %s",
116 (u_long
)TR_GETQID(ntohl(tr
->tr_rttlqid
)),
117 ipaddr_string(&tr
->tr_src
), ipaddr_string(&tr
->tr_dst
),
118 ipaddr_string(&tr
->tr_raddr
));
119 if (IN_CLASSD(ntohl(tr
->tr_raddr
)))
120 printf(" with-ttl %d", TR_GETTTL(ntohl(tr
->tr_rttlqid
)));
123 static void print_mresp(register const u_char
*bp
, register u_int len
)
125 register struct tr_query
*tr
= (struct tr_query
*)(bp
+ 8);
127 printf("mresp %lu: %s to %s reply-to %s",
128 (u_long
)TR_GETQID(ntohl(tr
->tr_rttlqid
)),
129 ipaddr_string(&tr
->tr_src
), ipaddr_string(&tr
->tr_dst
),
130 ipaddr_string(&tr
->tr_raddr
));
131 if (IN_CLASSD(ntohl(tr
->tr_raddr
)))
132 printf(" with-ttl %d", TR_GETTTL(ntohl(tr
->tr_rttlqid
)));
136 igmp_print(register const u_char
*bp
, register u_int len
,
137 register const u_char
*bp2
)
139 register const struct ip
*ip
;
141 ip
= (const struct ip
*)bp2
;
142 (void)printf("%s > %s: ",
143 ipaddr_string(&ip
->ip_src
),
144 ipaddr_string(&ip
->ip_dst
));
147 (void)printf("igmp");
154 (void)printf("igmp query");
155 if (EXTRACT_32BITS(&bp
[4]))
156 (void)printf(" [gaddr %s]", ipaddr_string(&bp
[4]));
158 (void)printf(" [len %d]", len
);
161 (void)printf("igmp v1 report %s", ipaddr_string(&bp
[4]));
163 (void)printf(" [len %d]", len
);
166 (void)printf("igmp v2 report %s", ipaddr_string(&bp
[4]));
169 (void)printf("igmp leave %s", ipaddr_string(&bp
[4]));
172 (void)printf("igmp dvmrp");
174 (void)printf(" [len %d]", len
);
176 dvmrp_print(bp
, len
);
179 (void)printf("igmp pimv1");
180 pimv1_print(bp
, len
);
183 print_mresp(bp
, len
);
186 print_mtrace(bp
, len
);
189 (void)printf("igmp-%d", bp
[0]);
193 if (vflag
&& TTEST2(bp
[0], len
)) {
194 /* Check the IGMP checksum */
195 if (in_cksum((const u_short
*)bp
, len
, 0))
196 printf(" bad igmp cksum %x!", EXTRACT_16BITS(&bp
[2]));
200 fputs("[|igmp]", stdout
);
204 * print the recorded route in an IP RR, LSRR or SSRR option.
207 ip_printroute(const char *type
, register const u_char
*cp
, u_int length
)
209 register u_int ptr
= cp
[2] - 1;
212 printf(" %s{", type
);
213 if ((length
+ 1) & 3)
214 printf(" [bad length %d]", length
);
215 if (ptr
< 3 || ((ptr
+ 1) & 3) || ptr
> length
+ 1)
216 printf(" [bad ptr %d]", cp
[2]);
219 for (len
= 3; len
< length
; len
+= 4) {
222 printf("%s%s", type
, ipaddr_string(&cp
[len
]));
225 printf("%s}", ptr
== len
? "#" : "");
229 ip_printts(register const u_char
*cp
, u_int length
)
231 register u_int ptr
= cp
[2] - 1;
232 register u_int len
= 0;
237 hoplen
= ((cp
[3]&0xF) != IPOPT_TS_TSONLY
) ? 8 : 4;
238 if ((length
- 4) & (hoplen
-1))
239 printf("[bad length %d]", length
);
240 if (ptr
< 4 || ((ptr
- 4) & (hoplen
-1)) || ptr
> length
+ 1)
241 printf("[bad ptr %d]", cp
[2]);
243 case IPOPT_TS_TSONLY
:
246 case IPOPT_TS_TSANDADDR
:
250 * prespecified should really be 3, but some ones might send 2
251 * instead, and the IPOPT_TS_PRESPEC constant can apparently
252 * have both values, so we have to hard-code it here.
256 printf("PRESPEC2.0");
258 case 3: /* IPOPT_TS_PRESPEC */
262 printf("[bad ts type %d]", cp
[3]&0xF);
267 for (len
= 4; len
< length
; len
+= hoplen
) {
270 printf("%s%d@%s", type
, EXTRACT_32BITS(&cp
[len
+hoplen
-4]),
271 hoplen
!=8 ? "" : ipaddr_string(&cp
[len
]));
276 printf("%s", ptr
== len
? " ^ " : "");
279 printf(" [%d hops not recorded]} ", cp
[3]>>4);
288 ip_optprint(register const u_char
*cp
, u_int length
)
292 for (; length
> 0; cp
+= len
, length
-= len
) {
295 if (tt
== IPOPT_NOP
|| tt
== IPOPT_EOL
)
298 if (&cp
[1] >= snapend
) {
305 printf("[|ip op len %d]", len
);
308 if (&cp
[1] >= snapend
|| cp
+ len
> snapend
) {
317 printf("-%d", length
- 1);
328 #ifndef IPOPT_SECURITY
329 #define IPOPT_SECURITY 130
330 #endif /* IPOPT_SECURITY */
332 printf(" SECURITY{%d}", len
);
336 ip_printroute("RR", cp
, len
);
340 ip_printroute("SSRR", cp
, len
);
344 ip_printroute("LSRR", cp
, len
);
348 #define IPOPT_RA 148 /* router alert */
354 else if (cp
[2] || cp
[3])
355 printf("%d.%d", cp
[2], cp
[3]);
359 printf(" IPOPT-%d{%d}", cp
[0], len
);
366 * compute an IP header checksum.
367 * don't modifiy the packet.
370 in_cksum(const u_short
*addr
, register int len
, u_short csum
)
373 const u_short
*w
= addr
;
378 * Our algorithm is simple, using a 32 bit accumulator (sum),
379 * we add sequential 16 bit words to it, and at the end, fold
380 * back all the carry bits from the top 16 bits into the lower
388 sum
+= htons(*(u_char
*)w
<<8);
391 * add back carry outs from top 16 bits to low 16 bits
393 sum
= (sum
>> 16) + (sum
& 0xffff); /* add hi 16 to low 16 */
394 sum
+= (sum
>> 16); /* add carry */
395 answer
= ~sum
; /* truncate to 16 bits */
400 * print an IP datagram.
403 ip_print(register const u_char
*bp
, register u_int length
)
405 register const struct ip
*ip
;
406 register u_int hlen
, len
, len0
, off
;
407 register const u_char
*cp
;
411 ip
= (const struct ip
*)bp
;
414 * If the IP header is not aligned, copy into abuf.
415 * This will never happen with BPF. It does happen raw packet
419 static u_char
*abuf
= NULL
;
420 static int didwarn
= 0;
423 abuf
= (u_char
*)malloc(snaplen
);
425 error("ip_print: malloc");
427 memcpy((char *)abuf
, (char *)ip
, min(length
, snaplen
));
428 snapend
+= abuf
- (u_char
*)ip
;
430 ip
= (struct ip
*)abuf
;
431 /* We really want libpcap to give us aligned packets */
433 warning("compensating for unaligned libpcap packets");
438 if ((u_char
*)(ip
+ 1) > snapend
) {
442 if (length
< sizeof (struct ip
)) {
443 (void)printf("truncated-ip %d", length
);
446 hlen
= ip
->ip_hl
* 4;
447 if (hlen
< sizeof (struct ip
)) {
448 (void)printf("bad-hlen %d", hlen
);
452 len
= ntohs(ip
->ip_len
);
454 (void)printf("truncated-ip - %d bytes missing!",
460 * If this is fragment zero, hand it to the next higher
463 off
= ntohs(ip
->ip_off
);
464 if ((off
& 0x1fff) == 0) {
465 cp
= (const u_char
*)ip
+ hlen
;
468 if (nh
!= IPPROTO_TCP
&& nh
!= IPPROTO_UDP
) {
469 (void)printf("%s > %s: ", ipaddr_string(&ip
->ip_src
),
470 ipaddr_string(&ip
->ip_dst
));
476 #define IPPROTO_AH 51
480 advance
= ah_print(cp
, (const u_char
*)ip
);
486 #define IPPROTO_ESP 50
491 advance
= esp_print(cp
, (const u_char
*)ip
, &enh
);
500 #ifndef IPPROTO_IPCOMP
501 #define IPPROTO_IPCOMP 108
506 advance
= ipcomp_print(cp
, (const u_char
*)ip
, &enh
);
516 tcp_print(cp
, len
, (const u_char
*)ip
);
520 udp_print(cp
, len
, (const u_char
*)ip
);
524 icmp_print(cp
, len
, (const u_char
*)ip
);
528 #define IPPROTO_IGRP 9
531 igrp_print(cp
, len
, (const u_char
*)ip
);
536 (void)printf("%s > %s:", ipaddr_string(&ip
->ip_src
),
537 ipaddr_string(&ip
->ip_dst
));
539 (void)printf(" nd %d", len
);
543 egp_print(cp
, len
, (const u_char
*)ip
);
547 #define IPPROTO_OSPF 89
550 ospf_print(cp
, len
, (const u_char
*)ip
);
554 #define IPPROTO_IGMP 2
557 igmp_print(cp
, len
, (const u_char
*)ip
);
561 /* DVMRP multicast tunnel (ip-in-ip encapsulation) */
564 (void)printf("%s > %s: ",
565 ipaddr_string(&ip
->ip_src
),
566 ipaddr_string(&ip
->ip_dst
));
576 #ifndef IP6PROTO_ENCAP
577 #define IP6PROTO_ENCAP 41
580 /* ip6-in-ip encapsulation */
583 (void)printf("%s > %s: ",
584 ipaddr_string(&ip
->ip_src
),
585 ipaddr_string(&ip
->ip_dst
));
597 #define IPPROTO_GRE 47
601 (void)printf("gre %s > %s: ",
602 ipaddr_string(&ip
->ip_src
),
603 ipaddr_string(&ip
->ip_dst
));
607 printf(" (gre encap)");
612 #ifndef IPPROTO_MOBILE
613 #define IPPROTO_MOBILE 55
617 (void)printf("mobile %s > %s: ",
618 ipaddr_string(&ip
->ip_src
),
619 ipaddr_string(&ip
->ip_dst
));
620 mobile_print(cp
, len
);
622 printf(" (mobile encap)");
628 #define IPPROTO_PIM 103
635 #define IPPROTO_VRRP 112
638 vrrp_print(cp
, len
, ip
->ip_ttl
);
643 (void)printf("%s > %s:", ipaddr_string(&ip
->ip_src
),
644 ipaddr_string(&ip
->ip_dst
));
646 (void)printf(" ip-proto-%d %d", nh
, len
);
651 /* Ultra quiet now means that all this stuff should be suppressed */
653 if (qflag
> 1) return;
657 * for fragmented datagrams, print id:size@offset. On all
658 * but the last stick a "+". For unfragmented datagrams, note
659 * the don't fragment flag.
661 len
= len0
; /* get the original length */
664 * if this isn't the first frag, we're missing the
665 * next level protocol header. print the ip addr.
668 (void)printf("%s > %s:", ipaddr_string(&ip
->ip_src
),
669 ipaddr_string(&ip
->ip_dst
));
676 (void)printf(" (frag %d:%u@%d%s)", ntohs(ip
->ip_id
), len
,
678 (off
& IP_MF
)? "+" : "");
680 } else if (off
& IP_DF
)
681 (void)printf(" (DF)");
684 (void)printf(" [tos 0x%x", (int)ip
->ip_tos
);
686 if (ip
->ip_tos
&0x02) {
687 (void)printf(",ECT");
695 (void)printf(" [ttl %d]", (int)ip
->ip_ttl
);
702 if (ip
->ip_ttl
> 1) {
703 (void)printf("%sttl %d", sep
, (int)ip
->ip_ttl
);
706 if ((off
& 0x3fff) == 0) {
707 (void)printf("%sid %d", sep
, (int)ntohs(ip
->ip_id
));
710 (void)printf("%slen %d", sep
, (int)ntohs(ip
->ip_len
));
712 if ((u_char
*)ip
+ hlen
<= snapend
) {
713 sum
= in_cksum((const u_short
*)ip
, hlen
, 0);
715 (void)printf("%sbad cksum %x!", sep
,
720 if ((hlen
-= sizeof(struct ip
)) > 0) {
721 (void)printf("%soptlen=%d", sep
, hlen
);
722 ip_optprint((u_char
*)(ip
+ 1), hlen
);
729 ipN_print(register const u_char
*bp
, register u_int length
)
733 ip
= (struct ip
*)bp
;
735 (void)printf("truncated-ip %d", length
);
738 memcpy (&hdr
, (char *)ip
, 4);
741 ip_print (bp
, length
);
745 ip6_print (bp
, length
);
749 (void)printf("unknown ip %d", hdr
.ip_v
);