]>
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.71 1999-10-30 05:30:20 itojun Exp $ (LBL)";
27 #include <sys/param.h>
29 #include <sys/socket.h>
31 #include <netinet/in.h>
32 #include <netinet/in_systm.h>
33 #include <netinet/ip.h>
34 #include <netinet/ip_var.h>
35 #include <netinet/udp.h>
36 #include <netinet/udp_var.h>
37 #include <netinet/tcp.h>
47 #include "addrtoname.h"
48 #include "interface.h"
49 #include "extract.h" /* must come after interface.h */
57 #define IN_CLASSD(i) (((int32_t)(i) & 0xf0000000) == 0xe0000000)
60 /* (following from ipmulti/mrouted/prune.h) */
63 * The packet format for a traceroute request.
66 u_int tr_src
; /* traceroute source */
67 u_int tr_dst
; /* traceroute destination */
68 u_int tr_raddr
; /* traceroute response address */
69 #if defined(WORDS_BIGENDIAN) || (defined(BYTE_ORDER) && (BYTE_ORDER == BIG_ENDIAN))
71 u_int ttl
: 8; /* traceroute response ttl */
72 u_int qid
: 24; /* traceroute query id */
76 u_int qid
: 24; /* traceroute query id */
77 u_int ttl
: 8; /* traceroute response ttl */
86 * Traceroute response format. A traceroute response has a tr_query at the
87 * beginning, followed by one tr_resp for each hop taken.
90 u_int tr_qarr
; /* query arrival time */
91 u_int tr_inaddr
; /* incoming interface address */
92 u_int tr_outaddr
; /* outgoing interface address */
93 u_int tr_rmtaddr
; /* parent address in source tree */
94 u_int tr_vifin
; /* input packet count on interface */
95 u_int tr_vifout
; /* output packet count on interface */
96 u_int tr_pktcnt
; /* total incoming packets for src-grp */
97 u_char tr_rproto
; /* routing proto deployed on router */
98 u_char tr_fttl
; /* ttl required to forward on outvif */
99 u_char tr_smask
; /* subnet mask for src addr */
100 u_char tr_rflags
; /* forwarding error codes */
103 /* defs within mtrace */
107 /* fields for tr_rflags (forwarding error codes) */
109 #define TR_WRONG_IF 1
115 #define TR_NO_SPACE 0x81
116 #define TR_OLD_ROUTER 0x82
118 /* fields for tr_rproto (routing protocol) */
119 #define TR_PROTO_DVMRP 1
120 #define TR_PROTO_MOSPF 2
121 #define TR_PROTO_PIM 3
122 #define TR_PROTO_CBT 4
124 static void print_mtrace(register const u_char
*bp
, register u_int len
)
126 register struct tr_query
*tr
= (struct tr_query
*)(bp
+ 8);
128 printf("mtrace %d: %s to %s reply-to %s", tr
->tr_qid
,
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
->tr_rttl
);
135 static void print_mresp(register const u_char
*bp
, register u_int len
)
137 register struct tr_query
*tr
= (struct tr_query
*)(bp
+ 8);
139 printf("mresp %d: %s to %s reply-to %s", tr
->tr_qid
,
140 ipaddr_string(&tr
->tr_src
), ipaddr_string(&tr
->tr_dst
),
141 ipaddr_string(&tr
->tr_raddr
));
142 if (IN_CLASSD(ntohl(tr
->tr_raddr
)))
143 printf(" with-ttl %d", tr
->tr_rttl
);
147 igmp_print(register const u_char
*bp
, register u_int len
,
148 register const u_char
*bp2
)
150 register const struct ip
*ip
;
152 ip
= (const struct ip
*)bp2
;
153 (void)printf("%s > %s: ",
154 ipaddr_string(&ip
->ip_src
),
155 ipaddr_string(&ip
->ip_dst
));
160 (void)printf("igmp query");
162 (void)printf(" [gaddr %s]", ipaddr_string(&bp
[4]));
164 (void)printf(" [len %d]", len
);
167 (void)printf("igmp report %s", ipaddr_string(&bp
[4]));
169 (void)printf(" [len %d]", len
);
172 (void)printf("igmp nreport %s", ipaddr_string(&bp
[4]));
175 (void)printf("igmp leave %s", ipaddr_string(&bp
[4]));
178 (void)printf("igmp dvmrp");
180 (void)printf(" [len %d]", len
);
182 dvmrp_print(bp
, len
);
185 (void)printf("igmp pim");
186 igmp_pim_print(bp
, len
);
189 print_mresp(bp
, len
);
192 print_mtrace(bp
, len
);
195 (void)printf("igmp-%d", bp
[0] & 0xf);
198 if ((bp
[0] >> 4) != 1)
199 (void)printf(" [v%d]", bp
[0] >> 4);
203 /* Check the IGMP checksum */
206 const u_short
*sp
= (u_short
*)bp
;
208 for (count
= len
/ 2; --count
>= 0; )
211 sum
+= ntohs(*(u_char
*) sp
<< 8);
213 sum
= (sum
& 0xffff) + (sum
>> 16);
216 printf(" bad igmp cksum %x!", EXTRACT_16BITS(&bp
[2]));
220 fputs("[|igmp]", stdout
);
224 * print the recorded route in an IP RR, LSRR or SSRR option.
227 ip_printroute(const char *type
, register const u_char
*cp
, u_int length
)
229 register u_int ptr
= cp
[2] - 1;
232 printf(" %s{", type
);
233 if ((length
+ 1) & 3)
234 printf(" [bad length %d]", length
);
235 if (ptr
< 3 || ((ptr
+ 1) & 3) || ptr
> length
+ 1)
236 printf(" [bad ptr %d]", cp
[2]);
239 for (len
= 3; len
< length
; len
+= 4) {
242 printf("%s%s", type
, ipaddr_string(&cp
[len
]));
245 printf("%s}", ptr
== len
? "#" : "");
252 ip_optprint(register const u_char
*cp
, u_int length
)
256 for (; length
> 0; cp
+= len
, length
-= len
) {
259 len
= (tt
== IPOPT_NOP
|| tt
== IPOPT_EOL
) ? 1 : cp
[1];
261 printf("[|ip op len %d]", len
);
264 if (&cp
[1] >= snapend
|| cp
+ len
> snapend
) {
273 printf("-%d", length
- 1);
281 printf(" TS{%d}", len
);
285 printf(" SECURITY{%d}", len
);
289 printf(" RR{%d}=", len
);
290 ip_printroute("RR", cp
, len
);
294 ip_printroute("SSRR", cp
, len
);
298 ip_printroute("LSRR", cp
, len
);
302 printf(" IPOPT-%d{%d}", cp
[0], len
);
309 * compute an IP header checksum.
310 * don't modifiy the packet.
313 in_cksum(const struct ip
*ip
)
315 register const u_short
*sp
= (u_short
*)ip
;
316 register u_int32_t sum
= 0;
320 * No need for endian conversions.
322 for (count
= ip
->ip_hl
* 2; --count
>= 0; )
325 sum
= (sum
& 0xffff) + (sum
>> 16);
332 * print an IP datagram.
335 ip_print(register const u_char
*bp
, register u_int length
)
337 register const struct ip
*ip
;
338 register u_int hlen
, len
, len0
, off
;
339 register const u_char
*cp
;
343 ip
= (const struct ip
*)bp
;
346 * If the IP header is not aligned, copy into abuf.
347 * This will never happen with BPF. It does happen raw packet
351 static u_char
*abuf
= NULL
;
352 static int didwarn
= 0;
355 abuf
= (u_char
*)malloc(snaplen
);
357 error("ip_print: malloc");
359 memcpy((char *)abuf
, (char *)ip
, min(length
, snaplen
));
360 snapend
+= abuf
- (u_char
*)ip
;
362 ip
= (struct ip
*)abuf
;
363 /* We really want libpcap to give us aligned packets */
365 warning("compensating for unaligned libpcap packets");
370 if ((u_char
*)(ip
+ 1) > snapend
) {
374 if (length
< sizeof (struct ip
)) {
375 (void)printf("truncated-ip %d", length
);
378 hlen
= ip
->ip_hl
* 4;
380 len
= ntohs(ip
->ip_len
);
382 (void)printf("truncated-ip - %d bytes missing!",
388 * If this is fragment zero, hand it to the next higher
391 off
= ntohs(ip
->ip_off
);
392 if ((off
& 0x1fff) == 0) {
393 cp
= (const u_char
*)ip
+ hlen
;
396 if (nh
!= IPPROTO_TCP
&& nh
!= IPPROTO_UDP
) {
397 (void)printf("%s > %s: ", ipaddr_string(&ip
->ip_src
),
398 ipaddr_string(&ip
->ip_dst
));
405 advance
= ah_print(cp
, (const u_char
*)ip
);
413 advance
= esp_print(cp
, (const u_char
*)ip
, &enh
);
422 #ifndef IPPROTO_IPCOMP
423 #define IPPROTO_IPCOMP 108
428 advance
= ipcomp_print(cp
, (const u_char
*)ip
, &enh
);
438 tcp_print(cp
, len
, (const u_char
*)ip
);
442 udp_print(cp
, len
, (const u_char
*)ip
);
446 icmp_print(cp
, (const u_char
*)ip
);
450 #define IPPROTO_IGRP 9
453 igrp_print(cp
, len
, (const u_char
*)ip
);
458 (void)printf("%s > %s:", ipaddr_string(&ip
->ip_src
),
459 ipaddr_string(&ip
->ip_dst
));
461 (void)printf(" nd %d", len
);
465 egp_print(cp
, len
, (const u_char
*)ip
);
469 #define IPPROTO_OSPF 89
472 ospf_print(cp
, len
, (const u_char
*)ip
);
476 #define IPPROTO_IGMP 2
479 igmp_print(cp
, len
, (const u_char
*)ip
);
483 /* DVMRP multicast tunnel (ip-in-ip encapsulation) */
486 (void)printf("%s > %s: ",
487 ipaddr_string(&ip
->ip_src
),
488 ipaddr_string(&ip
->ip_dst
));
498 #ifndef IP6PROTO_ENCAP
499 #define IP6PROTO_ENCAP 41
502 /* ip6-in-ip encapsulation */
505 (void)printf("%s > %s: ",
506 ipaddr_string(&ip
->ip_src
),
507 ipaddr_string(&ip
->ip_dst
));
519 #define IPPROTO_GRE 47
523 (void)printf("gre %s > %s: ",
524 ipaddr_string(&ip
->ip_src
),
525 ipaddr_string(&ip
->ip_dst
));
529 printf(" (gre encap)");
534 #ifndef IPPROTO_MOBILE
535 #define IPPROTO_MOBILE 55
539 (void)printf("mobile %s > %s: ",
540 ipaddr_string(&ip
->ip_src
),
541 ipaddr_string(&ip
->ip_dst
));
542 mobile_print(cp
, len
);
544 printf(" (mobile encap)");
550 #define IPPROTO_PIM 103
558 (void)printf("%s > %s:", ipaddr_string(&ip
->ip_src
),
559 ipaddr_string(&ip
->ip_dst
));
561 (void)printf(" ip-proto-%d %d", nh
, len
);
567 * for fragmented datagrams, print id:size@offset. On all
568 * but the last stick a "+". For unfragmented datagrams, note
569 * the don't fragment flag.
571 len
= len0
; /* get the original length */
574 * if this isn't the first frag, we're missing the
575 * next level protocol header. print the ip addr.
578 (void)printf("%s > %s:", ipaddr_string(&ip
->ip_src
),
579 ipaddr_string(&ip
->ip_dst
));
580 (void)printf(" (frag %d:%u@%d%s)", ntohs(ip
->ip_id
), len
,
582 (off
& IP_MF
)? "+" : "");
583 } else if (off
& IP_DF
)
584 (void)printf(" (DF)");
587 (void)printf(" [tos 0x%x]", (int)ip
->ip_tos
);
589 (void)printf(" [ttl %d]", (int)ip
->ip_ttl
);
596 if (ip
->ip_ttl
> 1) {
597 (void)printf("%sttl %d", sep
, (int)ip
->ip_ttl
);
600 if ((off
& 0x3fff) == 0) {
601 (void)printf("%sid %d", sep
, (int)ntohs(ip
->ip_id
));
604 if ((u_char
*)ip
+ hlen
<= snapend
) {
607 (void)printf("%sbad cksum %x!", sep
,
612 if ((hlen
-= sizeof(struct ip
)) > 0) {
613 (void)printf("%soptlen=%d", sep
, hlen
);
614 ip_optprint((u_char
*)(ip
+ 1), hlen
);