]>
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.86 2000-09-23 08:26:34 guy Exp $ (LBL)";
31 #include <sys/param.h>
33 #include <sys/socket.h>
35 #include <netinet/in.h>
36 #include <netinet/in_systm.h>
37 #include <netinet/ip.h>
38 #include <netinet/ip_var.h>
45 #include "addrtoname.h"
46 #include "interface.h"
47 #include "extract.h" /* must come after interface.h */
55 #define IN_CLASSD(i) (((int32_t)(i) & 0xf0000000) == 0xe0000000)
58 /* (following from ipmulti/mrouted/prune.h) */
61 * The packet format for a traceroute request.
64 u_int tr_src
; /* traceroute source */
65 u_int tr_dst
; /* traceroute destination */
66 u_int tr_raddr
; /* traceroute response address */
67 u_int tr_rttlqid
; /* response ttl and qid */
70 #define TR_GETTTL(x) (int)(((x) >> 24) & 0xff)
71 #define TR_GETQID(x) ((x) & 0x00ffffff)
74 * Traceroute response format. A traceroute response has a tr_query at the
75 * beginning, followed by one tr_resp for each hop taken.
78 u_int tr_qarr
; /* query arrival time */
79 u_int tr_inaddr
; /* incoming interface address */
80 u_int tr_outaddr
; /* outgoing interface address */
81 u_int tr_rmtaddr
; /* parent address in source tree */
82 u_int tr_vifin
; /* input packet count on interface */
83 u_int tr_vifout
; /* output packet count on interface */
84 u_int tr_pktcnt
; /* total incoming packets for src-grp */
85 u_char tr_rproto
; /* routing proto deployed on router */
86 u_char tr_fttl
; /* ttl required to forward on outvif */
87 u_char tr_smask
; /* subnet mask for src addr */
88 u_char tr_rflags
; /* forwarding error codes */
91 /* defs within mtrace */
95 /* fields for tr_rflags (forwarding error codes) */
103 #define TR_NO_SPACE 0x81
104 #define TR_OLD_ROUTER 0x82
106 /* fields for tr_rproto (routing protocol) */
107 #define TR_PROTO_DVMRP 1
108 #define TR_PROTO_MOSPF 2
109 #define TR_PROTO_PIM 3
110 #define TR_PROTO_CBT 4
112 static void print_mtrace(register const u_char
*bp
, register u_int len
)
114 register struct tr_query
*tr
= (struct tr_query
*)(bp
+ 8);
116 printf("mtrace %lu: %s to %s reply-to %s",
117 (u_long
)TR_GETQID(ntohl(tr
->tr_rttlqid
)),
118 ipaddr_string(&tr
->tr_src
), ipaddr_string(&tr
->tr_dst
),
119 ipaddr_string(&tr
->tr_raddr
));
120 if (IN_CLASSD(ntohl(tr
->tr_raddr
)))
121 printf(" with-ttl %d", TR_GETTTL(ntohl(tr
->tr_rttlqid
)));
124 static void print_mresp(register const u_char
*bp
, register u_int len
)
126 register struct tr_query
*tr
= (struct tr_query
*)(bp
+ 8);
128 printf("mresp %lu: %s to %s reply-to %s",
129 (u_long
)TR_GETQID(ntohl(tr
->tr_rttlqid
)),
130 ipaddr_string(&tr
->tr_src
), ipaddr_string(&tr
->tr_dst
),
131 ipaddr_string(&tr
->tr_raddr
));
132 if (IN_CLASSD(ntohl(tr
->tr_raddr
)))
133 printf(" with-ttl %d", TR_GETTTL(ntohl(tr
->tr_rttlqid
)));
137 igmp_print(register const u_char
*bp
, register u_int len
,
138 register const u_char
*bp2
)
140 register const struct ip
*ip
;
142 ip
= (const struct ip
*)bp2
;
143 (void)printf("%s > %s: ",
144 ipaddr_string(&ip
->ip_src
),
145 ipaddr_string(&ip
->ip_dst
));
148 (void)printf("igmp");
155 (void)printf("igmp query");
156 if (EXTRACT_32BITS(&bp
[4]))
157 (void)printf(" [gaddr %s]", ipaddr_string(&bp
[4]));
159 (void)printf(" [len %d]", len
);
162 (void)printf("igmp v1 report %s", ipaddr_string(&bp
[4]));
164 (void)printf(" [len %d]", len
);
167 (void)printf("igmp v2 report %s", ipaddr_string(&bp
[4]));
170 (void)printf("igmp leave %s", ipaddr_string(&bp
[4]));
173 (void)printf("igmp dvmrp");
175 (void)printf(" [len %d]", len
);
177 dvmrp_print(bp
, len
);
180 (void)printf("igmp pimv1");
181 pimv1_print(bp
, len
);
184 print_mresp(bp
, len
);
187 print_mtrace(bp
, len
);
190 (void)printf("igmp-%d", bp
[0]);
194 if (vflag
&& TTEST2(bp
[0], len
)) {
195 /* Check the IGMP checksum */
196 if (in_cksum((const u_short
*)bp
, len
, 0))
197 printf(" bad igmp cksum %x!", EXTRACT_16BITS(&bp
[2]));
201 fputs("[|igmp]", stdout
);
205 * print the recorded route in an IP RR, LSRR or SSRR option.
208 ip_printroute(const char *type
, register const u_char
*cp
, u_int length
)
210 register u_int ptr
= cp
[2] - 1;
213 printf(" %s{", type
);
214 if ((length
+ 1) & 3)
215 printf(" [bad length %d]", length
);
216 if (ptr
< 3 || ((ptr
+ 1) & 3) || ptr
> length
+ 1)
217 printf(" [bad ptr %d]", cp
[2]);
220 for (len
= 3; len
< length
; len
+= 4) {
223 printf("%s%s", type
, ipaddr_string(&cp
[len
]));
226 printf("%s}", ptr
== len
? "#" : "");
230 ip_printts(register const u_char
*cp
, u_int length
)
232 register u_int ptr
= cp
[2] - 1;
233 register u_int len
= 0;
238 hoplen
= ((cp
[3]&0xF) != IPOPT_TS_TSONLY
) ? 8 : 4;
239 if ((length
- 4) & (hoplen
-1))
240 printf("[bad length %d]", length
);
241 if (ptr
< 4 || ((ptr
- 4) & (hoplen
-1)) || ptr
> length
+ 1)
242 printf("[bad ptr %d]", cp
[2]);
244 case IPOPT_TS_TSONLY
:
247 case IPOPT_TS_TSANDADDR
:
251 * prespecified should really be 3, but some ones might send 2
252 * instead, and the IPOPT_TS_PRESPEC constant can apparently
253 * have both values, so we have to hard-code it here.
257 printf("PRESPEC2.0");
259 case 3: /* IPOPT_TS_PRESPEC */
263 printf("[bad ts type %d]", cp
[3]&0xF);
268 for (len
= 4; len
< length
; len
+= hoplen
) {
271 printf("%s%d@%s", type
, EXTRACT_32BITS(&cp
[len
+hoplen
-4]),
272 hoplen
!=8 ? "" : ipaddr_string(&cp
[len
]));
277 printf("%s", ptr
== len
? " ^ " : "");
280 printf(" [%d hops not recorded]} ", cp
[3]>>4);
289 ip_optprint(register const u_char
*cp
, u_int length
)
293 for (; length
> 0; cp
+= len
, length
-= len
) {
296 if (tt
== IPOPT_NOP
|| tt
== IPOPT_EOL
)
299 if (&cp
[1] >= snapend
) {
306 printf("[|ip op len %d]", len
);
309 if (&cp
[1] >= snapend
|| cp
+ len
> snapend
) {
318 printf("-%d", length
- 1);
329 #ifndef IPOPT_SECURITY
330 #define IPOPT_SECURITY 130
331 #endif /* IPOPT_SECURITY */
333 printf(" SECURITY{%d}", len
);
337 ip_printroute("RR", cp
, len
);
341 ip_printroute("SSRR", cp
, len
);
345 ip_printroute("LSRR", cp
, len
);
349 #define IPOPT_RA 148 /* router alert */
355 else if (cp
[2] || cp
[3])
356 printf("%d.%d", cp
[2], cp
[3]);
360 printf(" IPOPT-%d{%d}", cp
[0], len
);
367 * compute an IP header checksum.
368 * don't modifiy the packet.
371 in_cksum(const u_short
*addr
, register int len
, u_short csum
)
374 const u_short
*w
= addr
;
379 * Our algorithm is simple, using a 32 bit accumulator (sum),
380 * we add sequential 16 bit words to it, and at the end, fold
381 * back all the carry bits from the top 16 bits into the lower
389 sum
+= htons(*(u_char
*)w
<<8);
392 * add back carry outs from top 16 bits to low 16 bits
394 sum
= (sum
>> 16) + (sum
& 0xffff); /* add hi 16 to low 16 */
395 sum
+= (sum
>> 16); /* add carry */
396 answer
= ~sum
; /* truncate to 16 bits */
401 * print an IP datagram.
404 ip_print(register const u_char
*bp
, register u_int length
)
406 register const struct ip
*ip
;
407 register u_int hlen
, len
, len0
, off
;
408 register const u_char
*cp
;
412 ip
= (const struct ip
*)bp
;
415 * If the IP header is not aligned, copy into abuf.
416 * This will never happen with BPF. It does happen raw packet
420 static u_char
*abuf
= NULL
;
421 static int didwarn
= 0;
424 abuf
= (u_char
*)malloc(snaplen
);
426 error("ip_print: malloc");
428 memcpy((char *)abuf
, (char *)ip
, min(length
, snaplen
));
429 snapend
+= abuf
- (u_char
*)ip
;
431 ip
= (struct ip
*)abuf
;
432 /* We really want libpcap to give us aligned packets */
434 warning("compensating for unaligned libpcap packets");
439 if ((u_char
*)(ip
+ 1) > snapend
) {
443 if (length
< sizeof (struct ip
)) {
444 (void)printf("truncated-ip %d", length
);
447 hlen
= ip
->ip_hl
* 4;
448 if (hlen
< sizeof (struct ip
)) {
449 (void)printf("bad-hlen %d", hlen
);
453 len
= ntohs(ip
->ip_len
);
455 (void)printf("truncated-ip - %d bytes missing!",
461 * If this is fragment zero, hand it to the next higher
464 off
= ntohs(ip
->ip_off
);
465 if ((off
& 0x1fff) == 0) {
466 cp
= (const u_char
*)ip
+ hlen
;
469 if (nh
!= IPPROTO_TCP
&& nh
!= IPPROTO_UDP
) {
470 (void)printf("%s > %s: ", ipaddr_string(&ip
->ip_src
),
471 ipaddr_string(&ip
->ip_dst
));
477 #define IPPROTO_AH 51
481 advance
= ah_print(cp
, (const u_char
*)ip
);
487 #define IPPROTO_ESP 50
492 advance
= esp_print(cp
, (const u_char
*)ip
, &enh
);
501 #ifndef IPPROTO_IPCOMP
502 #define IPPROTO_IPCOMP 108
507 advance
= ipcomp_print(cp
, (const u_char
*)ip
, &enh
);
517 tcp_print(cp
, len
, (const u_char
*)ip
);
521 udp_print(cp
, len
, (const u_char
*)ip
);
525 icmp_print(cp
, len
, (const u_char
*)ip
);
529 #define IPPROTO_IGRP 9
532 igrp_print(cp
, len
, (const u_char
*)ip
);
537 (void)printf("%s > %s:", ipaddr_string(&ip
->ip_src
),
538 ipaddr_string(&ip
->ip_dst
));
540 (void)printf(" nd %d", len
);
544 egp_print(cp
, len
, (const u_char
*)ip
);
548 #define IPPROTO_OSPF 89
551 ospf_print(cp
, len
, (const u_char
*)ip
);
555 #define IPPROTO_IGMP 2
558 igmp_print(cp
, len
, (const u_char
*)ip
);
562 /* DVMRP multicast tunnel (ip-in-ip encapsulation) */
565 (void)printf("%s > %s: ",
566 ipaddr_string(&ip
->ip_src
),
567 ipaddr_string(&ip
->ip_dst
));
577 #ifndef IP6PROTO_ENCAP
578 #define IP6PROTO_ENCAP 41
581 /* ip6-in-ip encapsulation */
584 (void)printf("%s > %s: ",
585 ipaddr_string(&ip
->ip_src
),
586 ipaddr_string(&ip
->ip_dst
));
598 #define IPPROTO_GRE 47
602 (void)printf("gre %s > %s: ",
603 ipaddr_string(&ip
->ip_src
),
604 ipaddr_string(&ip
->ip_dst
));
608 printf(" (gre encap)");
613 #ifndef IPPROTO_MOBILE
614 #define IPPROTO_MOBILE 55
618 (void)printf("mobile %s > %s: ",
619 ipaddr_string(&ip
->ip_src
),
620 ipaddr_string(&ip
->ip_dst
));
621 mobile_print(cp
, len
);
623 printf(" (mobile encap)");
629 #define IPPROTO_PIM 103
636 #define IPPROTO_VRRP 112
639 vrrp_print(cp
, len
, ip
->ip_ttl
);
644 (void)printf("%s > %s:", ipaddr_string(&ip
->ip_src
),
645 ipaddr_string(&ip
->ip_dst
));
647 (void)printf(" ip-proto-%d %d", nh
, len
);
652 /* Ultra quiet now means that all this stuff should be suppressed */
654 if (qflag
> 1) return;
658 * for fragmented datagrams, print id:size@offset. On all
659 * but the last stick a "+". For unfragmented datagrams, note
660 * the don't fragment flag.
662 len
= len0
; /* get the original length */
665 * if this isn't the first frag, we're missing the
666 * next level protocol header. print the ip addr.
669 (void)printf("%s > %s:", ipaddr_string(&ip
->ip_src
),
670 ipaddr_string(&ip
->ip_dst
));
677 (void)printf(" (frag %d:%u@%d%s)", ntohs(ip
->ip_id
), len
,
679 (off
& IP_MF
)? "+" : "");
681 } else if (off
& IP_DF
)
682 (void)printf(" (DF)");
685 (void)printf(" [tos 0x%x", (int)ip
->ip_tos
);
687 if (ip
->ip_tos
&0x02) {
688 (void)printf(",ECT");
696 (void)printf(" [ttl %d]", (int)ip
->ip_ttl
);
703 if (ip
->ip_ttl
> 1) {
704 (void)printf("%sttl %d", sep
, (int)ip
->ip_ttl
);
707 if ((off
& 0x3fff) == 0) {
708 (void)printf("%sid %d", sep
, (int)ntohs(ip
->ip_id
));
711 (void)printf("%slen %d", sep
, (int)ntohs(ip
->ip_len
));
713 if ((u_char
*)ip
+ hlen
<= snapend
) {
714 sum
= in_cksum((const u_short
*)ip
, hlen
, 0);
716 (void)printf("%sbad cksum %x!", sep
,
721 if ((hlen
-= sizeof(struct ip
)) > 0) {
722 (void)printf("%soptlen=%d", sep
, hlen
);
723 ip_optprint((u_char
*)(ip
+ 1), hlen
);
730 ipN_print(register const u_char
*bp
, register u_int length
)
734 ip
= (struct ip
*)bp
;
736 (void)printf("truncated-ip %d", length
);
739 memcpy (&hdr
, (char *)ip
, 4);
742 ip_print (bp
, length
);
746 ip6_print (bp
, length
);
750 (void)printf("unknown ip %d", hdr
.ip_v
);