]>
The Tcpdump Group git mirrors - tcpdump/blob - print-ip.c
5fb6cf1a817f1e4cc38d44a8701e88b72787ca5a
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.85 2000-07-29 06:06:27 assar 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>
39 #include <netinet/udp.h>
40 #include <netinet/udp_var.h>
41 #include <netinet/tcp.h>
48 #include "addrtoname.h"
49 #include "interface.h"
50 #include "extract.h" /* must come after interface.h */
58 #define IN_CLASSD(i) (((int32_t)(i) & 0xf0000000) == 0xe0000000)
61 /* (following from ipmulti/mrouted/prune.h) */
64 * The packet format for a traceroute request.
67 u_int tr_src
; /* traceroute source */
68 u_int tr_dst
; /* traceroute destination */
69 u_int tr_raddr
; /* traceroute response address */
70 u_int tr_rttlqid
; /* response ttl and qid */
73 #define TR_GETTTL(x) (int)(((x) >> 24) & 0xff)
74 #define TR_GETQID(x) ((x) & 0x00ffffff)
77 * Traceroute response format. A traceroute response has a tr_query at the
78 * beginning, followed by one tr_resp for each hop taken.
81 u_int tr_qarr
; /* query arrival time */
82 u_int tr_inaddr
; /* incoming interface address */
83 u_int tr_outaddr
; /* outgoing interface address */
84 u_int tr_rmtaddr
; /* parent address in source tree */
85 u_int tr_vifin
; /* input packet count on interface */
86 u_int tr_vifout
; /* output packet count on interface */
87 u_int tr_pktcnt
; /* total incoming packets for src-grp */
88 u_char tr_rproto
; /* routing proto deployed on router */
89 u_char tr_fttl
; /* ttl required to forward on outvif */
90 u_char tr_smask
; /* subnet mask for src addr */
91 u_char tr_rflags
; /* forwarding error codes */
94 /* defs within mtrace */
98 /* fields for tr_rflags (forwarding error codes) */
100 #define TR_WRONG_IF 1
106 #define TR_NO_SPACE 0x81
107 #define TR_OLD_ROUTER 0x82
109 /* fields for tr_rproto (routing protocol) */
110 #define TR_PROTO_DVMRP 1
111 #define TR_PROTO_MOSPF 2
112 #define TR_PROTO_PIM 3
113 #define TR_PROTO_CBT 4
115 static void print_mtrace(register const u_char
*bp
, register u_int len
)
117 register struct tr_query
*tr
= (struct tr_query
*)(bp
+ 8);
119 printf("mtrace %lu: %s to %s reply-to %s",
120 (u_long
)TR_GETQID(ntohl(tr
->tr_rttlqid
)),
121 ipaddr_string(&tr
->tr_src
), ipaddr_string(&tr
->tr_dst
),
122 ipaddr_string(&tr
->tr_raddr
));
123 if (IN_CLASSD(ntohl(tr
->tr_raddr
)))
124 printf(" with-ttl %d", TR_GETTTL(ntohl(tr
->tr_rttlqid
)));
127 static void print_mresp(register const u_char
*bp
, register u_int len
)
129 register struct tr_query
*tr
= (struct tr_query
*)(bp
+ 8);
131 printf("mresp %lu: %s to %s reply-to %s",
132 (u_long
)TR_GETQID(ntohl(tr
->tr_rttlqid
)),
133 ipaddr_string(&tr
->tr_src
), ipaddr_string(&tr
->tr_dst
),
134 ipaddr_string(&tr
->tr_raddr
));
135 if (IN_CLASSD(ntohl(tr
->tr_raddr
)))
136 printf(" with-ttl %d", TR_GETTTL(ntohl(tr
->tr_rttlqid
)));
140 igmp_print(register const u_char
*bp
, register u_int len
,
141 register const u_char
*bp2
)
143 register const struct ip
*ip
;
145 ip
= (const struct ip
*)bp2
;
146 (void)printf("%s > %s: ",
147 ipaddr_string(&ip
->ip_src
),
148 ipaddr_string(&ip
->ip_dst
));
151 (void)printf("igmp");
158 (void)printf("igmp query");
159 if (EXTRACT_32BITS(&bp
[4]))
160 (void)printf(" [gaddr %s]", ipaddr_string(&bp
[4]));
162 (void)printf(" [len %d]", len
);
165 (void)printf("igmp v1 report %s", ipaddr_string(&bp
[4]));
167 (void)printf(" [len %d]", len
);
170 (void)printf("igmp v2 report %s", ipaddr_string(&bp
[4]));
173 (void)printf("igmp leave %s", ipaddr_string(&bp
[4]));
176 (void)printf("igmp dvmrp");
178 (void)printf(" [len %d]", len
);
180 dvmrp_print(bp
, len
);
183 (void)printf("igmp pimv1");
184 pimv1_print(bp
, len
);
187 print_mresp(bp
, len
);
190 print_mtrace(bp
, len
);
193 (void)printf("igmp-%d", bp
[0]);
197 if (vflag
&& TTEST2(bp
[0], len
)) {
198 /* Check the IGMP checksum */
199 if (in_cksum((const u_short
*)bp
, len
, 0))
200 printf(" bad igmp cksum %x!", EXTRACT_16BITS(&bp
[2]));
204 fputs("[|igmp]", stdout
);
208 * print the recorded route in an IP RR, LSRR or SSRR option.
211 ip_printroute(const char *type
, register const u_char
*cp
, u_int length
)
213 register u_int ptr
= cp
[2] - 1;
216 printf(" %s{", type
);
217 if ((length
+ 1) & 3)
218 printf(" [bad length %d]", length
);
219 if (ptr
< 3 || ((ptr
+ 1) & 3) || ptr
> length
+ 1)
220 printf(" [bad ptr %d]", cp
[2]);
223 for (len
= 3; len
< length
; len
+= 4) {
226 printf("%s%s", type
, ipaddr_string(&cp
[len
]));
229 printf("%s}", ptr
== len
? "#" : "");
233 ip_printts(register const u_char
*cp
, u_int length
)
235 register u_int ptr
= cp
[2] - 1;
236 register u_int len
= 0;
241 hoplen
= ((cp
[3]&0xF) != IPOPT_TS_TSONLY
) ? 8 : 4;
242 if ((length
- 4) & (hoplen
-1))
243 printf("[bad length %d]", length
);
244 if (ptr
< 4 || ((ptr
- 4) & (hoplen
-1)) || ptr
> length
+ 1)
245 printf("[bad ptr %d]", cp
[2]);
247 case IPOPT_TS_TSONLY
:
250 case IPOPT_TS_TSANDADDR
:
254 * prespecified should really be 3, but some ones might send 2
255 * instead, and the IPOPT_TS_PRESPEC constant can apparently
256 * have both values, so we have to hard-code it here.
260 printf("PRESPEC2.0");
262 case 3: /* IPOPT_TS_PRESPEC */
266 printf("[bad ts type %d]", cp
[3]&0xF);
271 for (len
= 4; len
< length
; len
+= hoplen
) {
274 printf("%s%d@%s", type
, EXTRACT_32BITS(&cp
[len
+hoplen
-4]),
275 hoplen
!=8 ? "" : ipaddr_string(&cp
[len
]));
280 printf("%s", ptr
== len
? " ^ " : "");
283 printf(" [%d hops not recorded]} ", cp
[3]>>4);
292 ip_optprint(register const u_char
*cp
, u_int length
)
296 for (; length
> 0; cp
+= len
, length
-= len
) {
299 if (tt
== IPOPT_NOP
|| tt
== IPOPT_EOL
)
302 if (&cp
[1] >= snapend
) {
309 printf("[|ip op len %d]", len
);
312 if (&cp
[1] >= snapend
|| cp
+ len
> snapend
) {
321 printf("-%d", length
- 1);
332 #ifndef IPOPT_SECURITY
333 #define IPOPT_SECURITY 130
334 #endif /* IPOPT_SECURITY */
336 printf(" SECURITY{%d}", len
);
340 ip_printroute("RR", cp
, len
);
344 ip_printroute("SSRR", cp
, len
);
348 ip_printroute("LSRR", cp
, len
);
352 #define IPOPT_RA 148 /* router alert */
358 else if (cp
[2] || cp
[3])
359 printf("%d.%d", cp
[2], cp
[3]);
363 printf(" IPOPT-%d{%d}", cp
[0], len
);
370 * compute an IP header checksum.
371 * don't modifiy the packet.
374 in_cksum(const u_short
*addr
, register int len
, u_short csum
)
377 const u_short
*w
= addr
;
382 * Our algorithm is simple, using a 32 bit accumulator (sum),
383 * we add sequential 16 bit words to it, and at the end, fold
384 * back all the carry bits from the top 16 bits into the lower
392 sum
+= htons(*(u_char
*)w
<<8);
395 * add back carry outs from top 16 bits to low 16 bits
397 sum
= (sum
>> 16) + (sum
& 0xffff); /* add hi 16 to low 16 */
398 sum
+= (sum
>> 16); /* add carry */
399 answer
= ~sum
; /* truncate to 16 bits */
404 * print an IP datagram.
407 ip_print(register const u_char
*bp
, register u_int length
)
409 register const struct ip
*ip
;
410 register u_int hlen
, len
, len0
, off
;
411 register const u_char
*cp
;
415 ip
= (const struct ip
*)bp
;
418 * If the IP header is not aligned, copy into abuf.
419 * This will never happen with BPF. It does happen raw packet
423 static u_char
*abuf
= NULL
;
424 static int didwarn
= 0;
427 abuf
= (u_char
*)malloc(snaplen
);
429 error("ip_print: malloc");
431 memcpy((char *)abuf
, (char *)ip
, min(length
, snaplen
));
432 snapend
+= abuf
- (u_char
*)ip
;
434 ip
= (struct ip
*)abuf
;
435 /* We really want libpcap to give us aligned packets */
437 warning("compensating for unaligned libpcap packets");
442 if ((u_char
*)(ip
+ 1) > snapend
) {
446 if (length
< sizeof (struct ip
)) {
447 (void)printf("truncated-ip %d", length
);
450 hlen
= ip
->ip_hl
* 4;
451 if (hlen
< sizeof (struct ip
)) {
452 (void)printf("bad-hlen %d", hlen
);
456 len
= ntohs(ip
->ip_len
);
458 (void)printf("truncated-ip - %d bytes missing!",
464 * If this is fragment zero, hand it to the next higher
467 off
= ntohs(ip
->ip_off
);
468 if ((off
& 0x1fff) == 0) {
469 cp
= (const u_char
*)ip
+ hlen
;
472 if (nh
!= IPPROTO_TCP
&& nh
!= IPPROTO_UDP
) {
473 (void)printf("%s > %s: ", ipaddr_string(&ip
->ip_src
),
474 ipaddr_string(&ip
->ip_dst
));
480 #define IPPROTO_AH 51
484 advance
= ah_print(cp
, (const u_char
*)ip
);
490 #define IPPROTO_ESP 50
495 advance
= esp_print(cp
, (const u_char
*)ip
, &enh
);
504 #ifndef IPPROTO_IPCOMP
505 #define IPPROTO_IPCOMP 108
510 advance
= ipcomp_print(cp
, (const u_char
*)ip
, &enh
);
520 tcp_print(cp
, len
, (const u_char
*)ip
);
524 udp_print(cp
, len
, (const u_char
*)ip
);
528 icmp_print(cp
, len
, (const u_char
*)ip
);
532 #define IPPROTO_IGRP 9
535 igrp_print(cp
, len
, (const u_char
*)ip
);
540 (void)printf("%s > %s:", ipaddr_string(&ip
->ip_src
),
541 ipaddr_string(&ip
->ip_dst
));
543 (void)printf(" nd %d", len
);
547 egp_print(cp
, len
, (const u_char
*)ip
);
551 #define IPPROTO_OSPF 89
554 ospf_print(cp
, len
, (const u_char
*)ip
);
558 #define IPPROTO_IGMP 2
561 igmp_print(cp
, len
, (const u_char
*)ip
);
565 /* DVMRP multicast tunnel (ip-in-ip encapsulation) */
568 (void)printf("%s > %s: ",
569 ipaddr_string(&ip
->ip_src
),
570 ipaddr_string(&ip
->ip_dst
));
580 #ifndef IP6PROTO_ENCAP
581 #define IP6PROTO_ENCAP 41
584 /* ip6-in-ip encapsulation */
587 (void)printf("%s > %s: ",
588 ipaddr_string(&ip
->ip_src
),
589 ipaddr_string(&ip
->ip_dst
));
601 #define IPPROTO_GRE 47
605 (void)printf("gre %s > %s: ",
606 ipaddr_string(&ip
->ip_src
),
607 ipaddr_string(&ip
->ip_dst
));
611 printf(" (gre encap)");
616 #ifndef IPPROTO_MOBILE
617 #define IPPROTO_MOBILE 55
621 (void)printf("mobile %s > %s: ",
622 ipaddr_string(&ip
->ip_src
),
623 ipaddr_string(&ip
->ip_dst
));
624 mobile_print(cp
, len
);
626 printf(" (mobile encap)");
632 #define IPPROTO_PIM 103
639 #define IPPROTO_VRRP 112
642 vrrp_print(cp
, len
, ip
->ip_ttl
);
647 (void)printf("%s > %s:", ipaddr_string(&ip
->ip_src
),
648 ipaddr_string(&ip
->ip_dst
));
650 (void)printf(" ip-proto-%d %d", nh
, len
);
655 /* Ultra quiet now means that all this stuff should be suppressed */
657 if (qflag
> 1) return;
661 * for fragmented datagrams, print id:size@offset. On all
662 * but the last stick a "+". For unfragmented datagrams, note
663 * the don't fragment flag.
665 len
= len0
; /* get the original length */
668 * if this isn't the first frag, we're missing the
669 * next level protocol header. print the ip addr.
672 (void)printf("%s > %s:", ipaddr_string(&ip
->ip_src
),
673 ipaddr_string(&ip
->ip_dst
));
680 (void)printf(" (frag %d:%u@%d%s)", ntohs(ip
->ip_id
), len
,
682 (off
& IP_MF
)? "+" : "");
684 } else if (off
& IP_DF
)
685 (void)printf(" (DF)");
688 (void)printf(" [tos 0x%x", (int)ip
->ip_tos
);
690 if (ip
->ip_tos
&0x02) {
691 (void)printf(",ECT");
699 (void)printf(" [ttl %d]", (int)ip
->ip_ttl
);
706 if (ip
->ip_ttl
> 1) {
707 (void)printf("%sttl %d", sep
, (int)ip
->ip_ttl
);
710 if ((off
& 0x3fff) == 0) {
711 (void)printf("%sid %d", sep
, (int)ntohs(ip
->ip_id
));
714 (void)printf("%slen %d", sep
, (int)ntohs(ip
->ip_len
));
716 if ((u_char
*)ip
+ hlen
<= snapend
) {
717 sum
= in_cksum((const u_short
*)ip
, hlen
, 0);
719 (void)printf("%sbad cksum %x!", sep
,
724 if ((hlen
-= sizeof(struct ip
)) > 0) {
725 (void)printf("%soptlen=%d", sep
, hlen
);
726 ip_optprint((u_char
*)(ip
+ 1), hlen
);
733 ipN_print(register const u_char
*bp
, register u_int length
)
737 ip
= (struct ip
*)bp
;
739 (void)printf("truncated-ip %d", length
);
742 memcpy (&hdr
, (char *)ip
, 4);
745 ip_print (bp
, length
);
749 ip6_print (bp
, length
);
753 (void)printf("unknown ip %d", hdr
.ip_v
);