]>
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.76 1999-11-22 07:25:27 fenner 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>
51 #include "addrtoname.h"
52 #include "interface.h"
53 #include "extract.h" /* must come after interface.h */
61 #define IN_CLASSD(i) (((int32_t)(i) & 0xf0000000) == 0xe0000000)
64 /* (following from ipmulti/mrouted/prune.h) */
67 * The packet format for a traceroute request.
70 u_int tr_src
; /* traceroute source */
71 u_int tr_dst
; /* traceroute destination */
72 u_int tr_raddr
; /* traceroute response address */
73 #if defined(WORDS_BIGENDIAN) || (defined(BYTE_ORDER) && (BYTE_ORDER == BIG_ENDIAN))
75 u_int ttl
: 8; /* traceroute response ttl */
76 u_int qid
: 24; /* traceroute query id */
80 u_int qid
: 24; /* traceroute query id */
81 u_int ttl
: 8; /* traceroute response ttl */
90 * Traceroute response format. A traceroute response has a tr_query at the
91 * beginning, followed by one tr_resp for each hop taken.
94 u_int tr_qarr
; /* query arrival time */
95 u_int tr_inaddr
; /* incoming interface address */
96 u_int tr_outaddr
; /* outgoing interface address */
97 u_int tr_rmtaddr
; /* parent address in source tree */
98 u_int tr_vifin
; /* input packet count on interface */
99 u_int tr_vifout
; /* output packet count on interface */
100 u_int tr_pktcnt
; /* total incoming packets for src-grp */
101 u_char tr_rproto
; /* routing proto deployed on router */
102 u_char tr_fttl
; /* ttl required to forward on outvif */
103 u_char tr_smask
; /* subnet mask for src addr */
104 u_char tr_rflags
; /* forwarding error codes */
107 /* defs within mtrace */
111 /* fields for tr_rflags (forwarding error codes) */
113 #define TR_WRONG_IF 1
119 #define TR_NO_SPACE 0x81
120 #define TR_OLD_ROUTER 0x82
122 /* fields for tr_rproto (routing protocol) */
123 #define TR_PROTO_DVMRP 1
124 #define TR_PROTO_MOSPF 2
125 #define TR_PROTO_PIM 3
126 #define TR_PROTO_CBT 4
128 static void print_mtrace(register const u_char
*bp
, register u_int len
)
130 register struct tr_query
*tr
= (struct tr_query
*)(bp
+ 8);
132 printf("mtrace %d: %s to %s reply-to %s", tr
->tr_qid
,
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
->tr_rttl
);
139 static void print_mresp(register const u_char
*bp
, register u_int len
)
141 register struct tr_query
*tr
= (struct tr_query
*)(bp
+ 8);
143 printf("mresp %d: %s to %s reply-to %s", tr
->tr_qid
,
144 ipaddr_string(&tr
->tr_src
), ipaddr_string(&tr
->tr_dst
),
145 ipaddr_string(&tr
->tr_raddr
));
146 if (IN_CLASSD(ntohl(tr
->tr_raddr
)))
147 printf(" with-ttl %d", tr
->tr_rttl
);
151 igmp_print(register const u_char
*bp
, register u_int len
,
152 register const u_char
*bp2
)
154 register const struct ip
*ip
;
156 ip
= (const struct ip
*)bp2
;
157 (void)printf("%s > %s: ",
158 ipaddr_string(&ip
->ip_src
),
159 ipaddr_string(&ip
->ip_dst
));
164 (void)printf("igmp query");
166 (void)printf(" [gaddr %s]", ipaddr_string(&bp
[4]));
168 (void)printf(" [len %d]", len
);
171 (void)printf("igmp report %s", ipaddr_string(&bp
[4]));
173 (void)printf(" [len %d]", len
);
176 (void)printf("igmp nreport %s", ipaddr_string(&bp
[4]));
179 (void)printf("igmp leave %s", ipaddr_string(&bp
[4]));
182 (void)printf("igmp dvmrp");
184 (void)printf(" [len %d]", len
);
186 dvmrp_print(bp
, len
);
189 (void)printf("igmp pim");
190 pimv1_print(bp
, len
);
193 print_mresp(bp
, len
);
196 print_mtrace(bp
, len
);
199 (void)printf("igmp-%d", bp
[0] & 0xf);
202 if ((bp
[0] >> 4) != 1)
203 (void)printf(" [v%d]", bp
[0] >> 4);
207 /* Check the IGMP checksum */
208 if (in_cksum((const u_short
*)bp
, len
, 0))
209 printf(" bad igmp cksum %x!", EXTRACT_16BITS(&bp
[2]));
213 fputs("[|igmp]", stdout
);
217 * print the recorded route in an IP RR, LSRR or SSRR option.
220 ip_printroute(const char *type
, register const u_char
*cp
, u_int length
)
222 register u_int ptr
= cp
[2] - 1;
225 printf(" %s{", type
);
226 if ((length
+ 1) & 3)
227 printf(" [bad length %d]", length
);
228 if (ptr
< 3 || ((ptr
+ 1) & 3) || ptr
> length
+ 1)
229 printf(" [bad ptr %d]", cp
[2]);
232 for (len
= 3; len
< length
; len
+= 4) {
235 printf("%s%s", type
, ipaddr_string(&cp
[len
]));
238 printf("%s}", ptr
== len
? "#" : "");
242 ip_printts(register const u_char
*cp
, u_int length
)
244 register u_int ptr
= cp
[2] - 1;
250 hoplen
= ((cp
[3]&0xF) != IPOPT_TS_TSONLY
) ? 8 : 4;
251 if ((length
- 4) & (hoplen
-1))
252 printf("[bad length %d]", length
);
253 if (ptr
< 4 || ((ptr
- 4) & (hoplen
-1)) || ptr
> length
+ 1)
254 printf("[bad ptr %d]", cp
[2]);
256 case IPOPT_TS_TSONLY
:
259 case IPOPT_TS_TSANDADDR
:
263 * prespecified should really be 3, but some ones might send 2
264 * instead, and the IPOPT_TS_PRESPEC constant can apparently
265 * have both values, so we have to hard-code it here.
269 printf("PRESPEC2.0");
271 case 3: /* IPOPT_TS_PRESPEC */
275 printf("[bad ts type %d]", cp
[3]&0xF);
280 for (len
= 4; len
< length
; len
+= hoplen
) {
283 printf("%s%d@%s", type
, ntohl(*(u_int32_t
*)&cp
[len
+hoplen
-4]),
284 hoplen
!=8 ? "" : ipaddr_string(&cp
[len
]));
289 printf("%s", ptr
== len
? " ^ " : "");
292 printf(" [%d hops not recorded]} ", cp
[3]>>4);
301 ip_optprint(register const u_char
*cp
, u_int length
)
305 for (; length
> 0; cp
+= len
, length
-= len
) {
308 len
= (tt
== IPOPT_NOP
|| tt
== IPOPT_EOL
) ? 1 : cp
[1];
310 printf("[|ip op len %d]", len
);
313 if (&cp
[1] >= snapend
|| cp
+ len
> snapend
) {
322 printf("-%d", length
- 1);
333 #ifndef IPOPT_SECURITY
334 #define IPOPT_SECURITY 130
335 #endif /* IPOPT_SECURITY */
337 printf(" SECURITY{%d}", len
);
341 ip_printroute("RR", cp
, len
);
345 ip_printroute("SSRR", cp
, len
);
349 ip_printroute("LSRR", cp
, len
);
353 #define IPOPT_RA 148 /* router alert */
359 else if (cp
[2] || cp
[3])
360 printf("%d.%d", cp
[2], cp
[3]);
364 printf(" IPOPT-%d{%d}", cp
[0], len
);
371 * compute an IP header checksum.
372 * don't modifiy the packet.
375 in_cksum(const u_short
*addr
, register int len
, u_short csum
)
378 const u_short
*w
= addr
;
383 * Our algorithm is simple, using a 32 bit accumulator (sum),
384 * we add sequential 16 bit words to it, and at the end, fold
385 * back all the carry bits from the top 16 bits into the lower
393 sum
+= htons(*(u_char
*)w
<<8);
396 * add back carry outs from top 16 bits to low 16 bits
398 sum
= (sum
>> 16) + (sum
& 0xffff); /* add hi 16 to low 16 */
399 sum
+= (sum
>> 16); /* add carry */
400 answer
= ~sum
; /* truncate to 16 bits */
405 * print an IP datagram.
408 ip_print(register const u_char
*bp
, register u_int length
)
410 register const struct ip
*ip
;
411 register u_int hlen
, len
, len0
, off
;
412 register const u_char
*cp
;
416 ip
= (const struct ip
*)bp
;
419 * If the IP header is not aligned, copy into abuf.
420 * This will never happen with BPF. It does happen raw packet
424 static u_char
*abuf
= NULL
;
425 static int didwarn
= 0;
428 abuf
= (u_char
*)malloc(snaplen
);
430 error("ip_print: malloc");
432 memcpy((char *)abuf
, (char *)ip
, min(length
, snaplen
));
433 snapend
+= abuf
- (u_char
*)ip
;
435 ip
= (struct ip
*)abuf
;
436 /* We really want libpcap to give us aligned packets */
438 warning("compensating for unaligned libpcap packets");
443 if ((u_char
*)(ip
+ 1) > snapend
) {
447 if (length
< sizeof (struct ip
)) {
448 (void)printf("truncated-ip %d", length
);
451 hlen
= ip
->ip_hl
* 4;
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
637 (void)printf("%s > %s:", ipaddr_string(&ip
->ip_src
),
638 ipaddr_string(&ip
->ip_dst
));
640 (void)printf(" ip-proto-%d %d", nh
, len
);
645 /* Ultra quiet now means that all this stuff should be suppressed */
647 if (qflag
> 1) return;
651 * for fragmented datagrams, print id:size@offset. On all
652 * but the last stick a "+". For unfragmented datagrams, note
653 * the don't fragment flag.
655 len
= len0
; /* get the original length */
658 * if this isn't the first frag, we're missing the
659 * next level protocol header. print the ip addr.
662 (void)printf("%s > %s:", ipaddr_string(&ip
->ip_src
),
663 ipaddr_string(&ip
->ip_dst
));
670 (void)printf(" (frag %d:%u@%d%s)", ntohs(ip
->ip_id
), len
,
672 (off
& IP_MF
)? "+" : "");
674 } else if (off
& IP_DF
)
675 (void)printf(" (DF)");
678 (void)printf(" [tos 0x%x", (int)ip
->ip_tos
);
680 if (ip
->ip_tos
&0x02) {
681 (void)printf(",ECT");
689 (void)printf(" [ttl %d]", (int)ip
->ip_ttl
);
696 if (ip
->ip_ttl
> 1) {
697 (void)printf("%sttl %d", sep
, (int)ip
->ip_ttl
);
700 if ((off
& 0x3fff) == 0) {
701 (void)printf("%sid %d", sep
, (int)ntohs(ip
->ip_id
));
704 if ((u_char
*)ip
+ hlen
<= snapend
) {
705 sum
= in_cksum((const u_short
*)ip
, hlen
, 0);
707 (void)printf("%sbad cksum %x!", sep
,
712 if ((hlen
-= sizeof(struct ip
)) > 0) {
713 (void)printf("%soptlen=%d", sep
, hlen
);
714 ip_optprint((u_char
*)(ip
+ 1), hlen
);