]>
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.69 1999-10-17 22:18:01 mcr 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/udp.h>
35 #include <netinet/tcp.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 #ifdef WORDS_BIGENDIAN
69 u_int ttl
: 8; /* traceroute response ttl */
70 u_int qid
: 24; /* traceroute query id */
74 u_int qid
: 24; /* traceroute query id */
75 u_int ttl
: 8; /* traceroute response ttl */
84 * Traceroute response format. A traceroute response has a tr_query at the
85 * beginning, followed by one tr_resp for each hop taken.
88 u_int tr_qarr
; /* query arrival time */
89 u_int tr_inaddr
; /* incoming interface address */
90 u_int tr_outaddr
; /* outgoing interface address */
91 u_int tr_rmtaddr
; /* parent address in source tree */
92 u_int tr_vifin
; /* input packet count on interface */
93 u_int tr_vifout
; /* output packet count on interface */
94 u_int tr_pktcnt
; /* total incoming packets for src-grp */
95 u_char tr_rproto
; /* routing proto deployed on router */
96 u_char tr_fttl
; /* ttl required to forward on outvif */
97 u_char tr_smask
; /* subnet mask for src addr */
98 u_char tr_rflags
; /* forwarding error codes */
101 /* defs within mtrace */
105 /* fields for tr_rflags (forwarding error codes) */
107 #define TR_WRONG_IF 1
113 #define TR_NO_SPACE 0x81
114 #define TR_OLD_ROUTER 0x82
116 /* fields for tr_rproto (routing protocol) */
117 #define TR_PROTO_DVMRP 1
118 #define TR_PROTO_MOSPF 2
119 #define TR_PROTO_PIM 3
120 #define TR_PROTO_CBT 4
122 static void print_mtrace(register const u_char
*bp
, register u_int len
)
124 register struct tr_query
*tr
= (struct tr_query
*)(bp
+ 8);
126 printf("mtrace %d: %s to %s reply-to %s", tr
->tr_qid
,
127 ipaddr_string(&tr
->tr_src
), ipaddr_string(&tr
->tr_dst
),
128 ipaddr_string(&tr
->tr_raddr
));
129 if (IN_CLASSD(ntohl(tr
->tr_raddr
)))
130 printf(" with-ttl %d", tr
->tr_rttl
);
133 static void print_mresp(register const u_char
*bp
, register u_int len
)
135 register struct tr_query
*tr
= (struct tr_query
*)(bp
+ 8);
137 printf("mresp %d: %s to %s reply-to %s", tr
->tr_qid
,
138 ipaddr_string(&tr
->tr_src
), ipaddr_string(&tr
->tr_dst
),
139 ipaddr_string(&tr
->tr_raddr
));
140 if (IN_CLASSD(ntohl(tr
->tr_raddr
)))
141 printf(" with-ttl %d", tr
->tr_rttl
);
145 igmp_print(register const u_char
*bp
, register u_int len
,
146 register const u_char
*bp2
)
148 register const struct ip
*ip
;
150 ip
= (const struct ip
*)bp2
;
151 (void)printf("%s > %s: ",
152 ipaddr_string(&ip
->ip_src
),
153 ipaddr_string(&ip
->ip_dst
));
158 (void)printf("igmp query");
160 (void)printf(" [gaddr %s]", ipaddr_string(&bp
[4]));
162 (void)printf(" [len %d]", len
);
165 (void)printf("igmp report %s", ipaddr_string(&bp
[4]));
167 (void)printf(" [len %d]", len
);
170 (void)printf("igmp nreport %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 pim");
187 print_mresp(bp
, len
);
190 print_mtrace(bp
, len
);
193 (void)printf("igmp-%d", bp
[0] & 0xf);
196 if ((bp
[0] >> 4) != 1)
197 (void)printf(" [v%d]", bp
[0] >> 4);
201 /* Check the IGMP checksum */
204 const u_short
*sp
= (u_short
*)bp
;
206 for (count
= len
/ 2; --count
>= 0; )
209 sum
+= ntohs(*(u_char
*) sp
<< 8);
211 sum
= (sum
& 0xffff) + (sum
>> 16);
214 printf(" bad igmp cksum %x!", EXTRACT_16BITS(&bp
[2]));
218 fputs("[|igmp]", stdout
);
222 * print the recorded route in an IP RR, LSRR or SSRR option.
225 ip_printroute(const char *type
, register const u_char
*cp
, u_int length
)
227 register u_int ptr
= cp
[2] - 1;
230 printf(" %s{", type
);
231 if ((length
+ 1) & 3)
232 printf(" [bad length %d]", length
);
233 if (ptr
< 3 || ((ptr
+ 1) & 3) || ptr
> length
+ 1)
234 printf(" [bad ptr %d]", cp
[2]);
237 for (len
= 3; len
< length
; len
+= 4) {
240 printf("%s%s", type
, ipaddr_string(&cp
[len
]));
243 printf("%s}", ptr
== len
? "#" : "");
250 ip_optprint(register const u_char
*cp
, u_int length
)
254 for (; length
> 0; cp
+= len
, length
-= len
) {
257 len
= (tt
== IPOPT_NOP
|| tt
== IPOPT_EOL
) ? 1 : cp
[1];
259 printf("[|ip op len %d]", len
);
262 if (&cp
[1] >= snapend
|| cp
+ len
> snapend
) {
271 printf("-%d", length
- 1);
279 printf(" TS{%d}", len
);
283 printf(" SECURITY{%d}", len
);
287 printf(" RR{%d}=", len
);
288 ip_printroute("RR", cp
, len
);
292 ip_printroute("SSRR", cp
, len
);
296 ip_printroute("LSRR", cp
, len
);
300 printf(" IPOPT-%d{%d}", cp
[0], len
);
307 * compute an IP header checksum.
308 * don't modifiy the packet.
311 in_cksum(const struct ip
*ip
)
313 register const u_short
*sp
= (u_short
*)ip
;
314 register u_int32_t sum
= 0;
318 * No need for endian conversions.
320 for (count
= ip
->ip_hl
* 2; --count
>= 0; )
323 sum
= (sum
& 0xffff) + (sum
>> 16);
330 * print an IP datagram.
333 ip_print(register const u_char
*bp
, register u_int length
)
335 register const struct ip
*ip
;
336 register u_int hlen
, len
, off
;
337 register const u_char
*cp
;
339 ip
= (const struct ip
*)bp
;
342 * If the IP header is not aligned, copy into abuf.
343 * This will never happen with BPF. It does happen raw packet
347 static u_char
*abuf
= NULL
;
348 static int didwarn
= 0;
351 abuf
= (u_char
*)malloc(snaplen
);
353 error("ip_print: malloc");
355 memcpy((char *)abuf
, (char *)ip
, min(length
, snaplen
));
356 snapend
+= abuf
- (u_char
*)ip
;
358 ip
= (struct ip
*)abuf
;
359 /* We really want libpcap to give us aligned packets */
361 warning("compensating for unaligned libpcap packets");
366 if ((u_char
*)(ip
+ 1) > snapend
) {
370 if (length
< sizeof (struct ip
)) {
371 (void)printf("truncated-ip %d", length
);
374 hlen
= ip
->ip_hl
* 4;
376 len
= ntohs(ip
->ip_len
);
378 (void)printf("truncated-ip - %d bytes missing!",
383 * If this is fragment zero, hand it to the next higher
386 off
= ntohs(ip
->ip_off
);
387 if ((off
& 0x1fff) == 0) {
388 cp
= (const u_char
*)ip
+ hlen
;
392 tcp_print(cp
, len
, (const u_char
*)ip
);
396 udp_print(cp
, len
, (const u_char
*)ip
);
400 icmp_print(cp
, (const u_char
*)ip
);
404 #define IPPROTO_IGRP 9
407 igrp_print(cp
, len
, (const u_char
*)ip
);
411 (void)printf("%s > %s:", ipaddr_string(&ip
->ip_src
),
412 ipaddr_string(&ip
->ip_dst
));
413 (void)printf(" nd %d", len
);
417 egp_print(cp
, len
, (const u_char
*)ip
);
421 #define IPPROTO_OSPF 89
424 ospf_print(cp
, len
, (const u_char
*)ip
);
428 #define IPPROTO_IGMP 2
431 igmp_print(cp
, len
, (const u_char
*)ip
);
435 /* DVMRP multicast tunnel (ip-in-ip encapsulation) */
437 (void)printf("%s > %s: ",
438 ipaddr_string(&ip
->ip_src
),
439 ipaddr_string(&ip
->ip_dst
));
448 #define IPPROTO_GRE 47
452 (void)printf("gre %s > %s: ",
453 ipaddr_string(&ip
->ip_src
),
454 ipaddr_string(&ip
->ip_dst
));
458 printf(" (gre encap)");
464 (void)printf("%s > %s:", ipaddr_string(&ip
->ip_src
),
465 ipaddr_string(&ip
->ip_dst
));
466 (void)printf(" ip-proto-%d %d", ip
->ip_p
, len
);
471 * for fragmented datagrams, print id:size@offset. On all
472 * but the last stick a "+". For unfragmented datagrams, note
473 * the don't fragment flag.
477 * if this isn't the first frag, we're missing the
478 * next level protocol header. print the ip addr.
481 (void)printf("%s > %s:", ipaddr_string(&ip
->ip_src
),
482 ipaddr_string(&ip
->ip_dst
));
483 (void)printf(" (frag %d:%d@%d%s)", ntohs(ip
->ip_id
), len
,
485 (off
& IP_MF
)? "+" : "");
486 } else if (off
& IP_DF
)
487 (void)printf(" (DF)");
490 (void)printf(" [tos 0x%x]", (int)ip
->ip_tos
);
492 (void)printf(" [ttl %d]", (int)ip
->ip_ttl
);
499 if (ip
->ip_ttl
> 1) {
500 (void)printf("%sttl %d", sep
, (int)ip
->ip_ttl
);
503 if ((off
& 0x3fff) == 0) {
504 (void)printf("%sid %d", sep
, (int)ntohs(ip
->ip_id
));
507 if ((u_char
*)ip
+ hlen
<= snapend
) {
510 (void)printf("%sbad cksum %x!", sep
,
515 if ((hlen
-= sizeof(struct ip
)) > 0) {
516 (void)printf("%soptlen=%d", sep
, hlen
);
517 ip_optprint((u_char
*)(ip
+ 1), hlen
);