]>
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.66 1999-10-07 23:47:11 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/ip_var.h>
35 #include <netinet/udp.h>
36 #include <netinet/udp_var.h>
37 #include <netinet/tcp.h>
38 #include <netinet/tcpip.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 #ifdef WORDS_BIGENDIAN
72 u_int ttl
: 8; /* traceroute response ttl */
73 u_int qid
: 24; /* traceroute query id */
77 u_int qid
: 24; /* traceroute query id */
78 u_int ttl
: 8; /* traceroute response ttl */
87 * Traceroute response format. A traceroute response has a tr_query at the
88 * beginning, followed by one tr_resp for each hop taken.
91 u_int tr_qarr
; /* query arrival time */
92 u_int tr_inaddr
; /* incoming interface address */
93 u_int tr_outaddr
; /* outgoing interface address */
94 u_int tr_rmtaddr
; /* parent address in source tree */
95 u_int tr_vifin
; /* input packet count on interface */
96 u_int tr_vifout
; /* output packet count on interface */
97 u_int tr_pktcnt
; /* total incoming packets for src-grp */
98 u_char tr_rproto
; /* routing proto deployed on router */
99 u_char tr_fttl
; /* ttl required to forward on outvif */
100 u_char tr_smask
; /* subnet mask for src addr */
101 u_char tr_rflags
; /* forwarding error codes */
104 /* defs within mtrace */
108 /* fields for tr_rflags (forwarding error codes) */
110 #define TR_WRONG_IF 1
116 #define TR_NO_SPACE 0x81
117 #define TR_OLD_ROUTER 0x82
119 /* fields for tr_rproto (routing protocol) */
120 #define TR_PROTO_DVMRP 1
121 #define TR_PROTO_MOSPF 2
122 #define TR_PROTO_PIM 3
123 #define TR_PROTO_CBT 4
125 static void print_mtrace(register const u_char
*bp
, register u_int len
)
127 register struct tr_query
*tr
= (struct tr_query
*)(bp
+ 8);
129 printf("mtrace %d: %s to %s reply-to %s", tr
->tr_qid
,
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
->tr_rttl
);
136 static void print_mresp(register const u_char
*bp
, register u_int len
)
138 register struct tr_query
*tr
= (struct tr_query
*)(bp
+ 8);
140 printf("mresp %d: %s to %s reply-to %s", tr
->tr_qid
,
141 ipaddr_string(&tr
->tr_src
), ipaddr_string(&tr
->tr_dst
),
142 ipaddr_string(&tr
->tr_raddr
));
143 if (IN_CLASSD(ntohl(tr
->tr_raddr
)))
144 printf(" with-ttl %d", tr
->tr_rttl
);
148 igmp_print(register const u_char
*bp
, register u_int len
,
149 register const u_char
*bp2
)
151 register const struct ip
*ip
;
153 ip
= (const struct ip
*)bp2
;
154 (void)printf("%s > %s: ",
155 ipaddr_string(&ip
->ip_src
),
156 ipaddr_string(&ip
->ip_dst
));
161 (void)printf("igmp query");
163 (void)printf(" [gaddr %s]", ipaddr_string(&bp
[4]));
165 (void)printf(" [len %d]", len
);
168 (void)printf("igmp report %s", ipaddr_string(&bp
[4]));
170 (void)printf(" [len %d]", len
);
173 (void)printf("igmp nreport %s", ipaddr_string(&bp
[4]));
176 (void)printf("igmp leave %s", ipaddr_string(&bp
[4]));
179 (void)printf("igmp dvmrp");
181 (void)printf(" [len %d]", len
);
183 dvmrp_print(bp
, len
);
186 (void)printf("igmp pim");
190 print_mresp(bp
, len
);
193 print_mtrace(bp
, len
);
196 (void)printf("igmp-%d", bp
[0] & 0xf);
199 if ((bp
[0] >> 4) != 1)
200 (void)printf(" [v%d]", bp
[0] >> 4);
204 /* Check the IGMP checksum */
207 const u_short
*sp
= (u_short
*)bp
;
209 for (count
= len
/ 2; --count
>= 0; )
212 sum
+= ntohs(*(u_char
*) sp
<< 8);
214 sum
= (sum
& 0xffff) + (sum
>> 16);
217 printf(" bad igmp cksum %x!", EXTRACT_16BITS(&bp
[2]));
221 fputs("[|igmp]", stdout
);
225 * print the recorded route in an IP RR, LSRR or SSRR option.
228 ip_printroute(const char *type
, register const u_char
*cp
, u_int length
)
230 register u_int ptr
= cp
[2] - 1;
233 printf(" %s{", type
);
234 if ((length
+ 1) & 3)
235 printf(" [bad length %d]", length
);
236 if (ptr
< 3 || ((ptr
+ 1) & 3) || ptr
> length
+ 1)
237 printf(" [bad ptr %d]", cp
[2]);
240 for (len
= 3; len
< length
; len
+= 4) {
243 printf("%s%s", type
, ipaddr_string(&cp
[len
]));
246 printf("%s}", ptr
== len
? "#" : "");
253 ip_optprint(register const u_char
*cp
, u_int length
)
257 for (; length
> 0; cp
+= len
, length
-= len
) {
260 len
= (tt
== IPOPT_NOP
|| tt
== IPOPT_EOL
) ? 1 : cp
[1];
262 printf("[|ip op len %d]", len
);
265 if (&cp
[1] >= snapend
|| cp
+ len
> snapend
) {
274 printf("-%d", length
- 1);
282 printf(" TS{%d}", len
);
286 printf(" SECURITY{%d}", len
);
290 printf(" RR{%d}=", len
);
291 ip_printroute("RR", cp
, len
);
295 ip_printroute("SSRR", cp
, len
);
299 ip_printroute("LSRR", cp
, len
);
303 printf(" IPOPT-%d{%d}", cp
[0], len
);
310 * compute an IP header checksum.
311 * don't modifiy the packet.
314 in_cksum(const struct ip
*ip
)
316 register const u_short
*sp
= (u_short
*)ip
;
317 register u_int32_t sum
= 0;
321 * No need for endian conversions.
323 for (count
= ip
->ip_hl
* 2; --count
>= 0; )
326 sum
= (sum
& 0xffff) + (sum
>> 16);
333 * print an IP datagram.
336 ip_print(register const u_char
*bp
, register u_int length
)
338 register const struct ip
*ip
;
339 register u_int hlen
, len
, off
;
340 register const u_char
*cp
;
342 ip
= (const struct ip
*)bp
;
345 * If the IP header is not aligned, copy into abuf.
346 * This will never happen with BPF. It does happen raw packet
350 static u_char
*abuf
= NULL
;
351 static int didwarn
= 0;
354 abuf
= (u_char
*)malloc(snaplen
);
356 error("ip_print: malloc");
358 memcpy((char *)abuf
, (char *)ip
, min(length
, snaplen
));
359 snapend
+= abuf
- (u_char
*)ip
;
361 ip
= (struct ip
*)abuf
;
362 /* We really want libpcap to give us aligned packets */
364 warning("compensating for unaligned libpcap packets");
369 if ((u_char
*)(ip
+ 1) > snapend
) {
373 if (length
< sizeof (struct ip
)) {
374 (void)printf("truncated-ip %d", length
);
377 hlen
= ip
->ip_hl
* 4;
379 len
= ntohs(ip
->ip_len
);
381 (void)printf("truncated-ip - %d bytes missing!",
386 * If this is fragment zero, hand it to the next higher
389 off
= ntohs(ip
->ip_off
);
390 if ((off
& 0x1fff) == 0) {
391 cp
= (const u_char
*)ip
+ hlen
;
395 tcp_print(cp
, len
, (const u_char
*)ip
);
399 udp_print(cp
, len
, (const u_char
*)ip
);
403 icmp_print(cp
, (const u_char
*)ip
);
407 #define IPPROTO_IGRP 9
410 igrp_print(cp
, len
, (const u_char
*)ip
);
414 (void)printf("%s > %s:", ipaddr_string(&ip
->ip_src
),
415 ipaddr_string(&ip
->ip_dst
));
416 (void)printf(" nd %d", len
);
420 egp_print(cp
, len
, (const u_char
*)ip
);
424 #define IPPROTO_OSPF 89
427 ospf_print(cp
, len
, (const u_char
*)ip
);
431 #define IPPROTO_IGMP 2
434 igmp_print(cp
, len
, (const u_char
*)ip
);
438 /* DVMRP multicast tunnel (ip-in-ip encapsulation) */
440 (void)printf("%s > %s: ",
441 ipaddr_string(&ip
->ip_src
),
442 ipaddr_string(&ip
->ip_dst
));
451 #define IPPROTO_GRE 47
455 (void)printf("gre %s > %s: ",
456 ipaddr_string(&ip
->ip_src
),
457 ipaddr_string(&ip
->ip_dst
));
461 printf(" (gre encap)");
467 (void)printf("%s > %s:", ipaddr_string(&ip
->ip_src
),
468 ipaddr_string(&ip
->ip_dst
));
469 (void)printf(" ip-proto-%d %d", ip
->ip_p
, len
);
474 * for fragmented datagrams, print id:size@offset. On all
475 * but the last stick a "+". For unfragmented datagrams, note
476 * the don't fragment flag.
480 * if this isn't the first frag, we're missing the
481 * next level protocol header. print the ip addr.
484 (void)printf("%s > %s:", ipaddr_string(&ip
->ip_src
),
485 ipaddr_string(&ip
->ip_dst
));
486 (void)printf(" (frag %d:%d@%d%s)", ntohs(ip
->ip_id
), len
,
488 (off
& IP_MF
)? "+" : "");
489 } else if (off
& IP_DF
)
490 (void)printf(" (DF)");
493 (void)printf(" [tos 0x%x]", (int)ip
->ip_tos
);
495 (void)printf(" [ttl %d]", (int)ip
->ip_ttl
);
502 if (ip
->ip_ttl
> 1) {
503 (void)printf("%sttl %d", sep
, (int)ip
->ip_ttl
);
506 if ((off
& 0x3fff) == 0) {
507 (void)printf("%sid %d", sep
, (int)ntohs(ip
->ip_id
));
510 if ((u_char
*)ip
+ hlen
<= snapend
) {
513 (void)printf("%sbad cksum %x!", sep
,
518 if ((hlen
-= sizeof(struct ip
)) > 0) {
519 (void)printf("%soptlen=%d", sep
, hlen
);
520 ip_optprint((u_char
*)(ip
+ 1), hlen
);