]>
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.73 1999-11-21 03:47:35 assar 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 */
204 if (in_cksum((const u_short
*)bp
, len
, 0))
205 printf(" bad igmp cksum %x!", EXTRACT_16BITS(&bp
[2]));
209 fputs("[|igmp]", stdout
);
213 * print the recorded route in an IP RR, LSRR or SSRR option.
216 ip_printroute(const char *type
, register const u_char
*cp
, u_int length
)
218 register u_int ptr
= cp
[2] - 1;
221 printf(" %s{", type
);
222 if ((length
+ 1) & 3)
223 printf(" [bad length %d]", length
);
224 if (ptr
< 3 || ((ptr
+ 1) & 3) || ptr
> length
+ 1)
225 printf(" [bad ptr %d]", cp
[2]);
228 for (len
= 3; len
< length
; len
+= 4) {
231 printf("%s%s", type
, ipaddr_string(&cp
[len
]));
234 printf("%s}", ptr
== len
? "#" : "");
238 ip_printts(register const u_char
*cp
, u_int length
)
240 register u_int ptr
= cp
[2] - 1;
246 hoplen
= ((cp
[3]&0xF) != IPOPT_TS_TSONLY
) ? 8 : 4;
247 if ((length
- 4) & (hoplen
-1))
248 printf("[bad length %d]", length
);
249 if (ptr
< 4 || ((ptr
- 4) & (hoplen
-1)) || ptr
> length
+ 1)
250 printf("[bad ptr %d]", cp
[2]);
252 case IPOPT_TS_TSONLY
:
255 case IPOPT_TS_TSANDADDR
:
259 printf("PRESPEC2.0");
261 case IPOPT_TS_PRESPEC
:
265 printf("[bad ts type %d]", cp
[3]&0xF);
270 for (len
= 4; len
< length
; len
+= hoplen
) {
273 printf("%s%d@%s", type
, ntohl(*(u_int32_t
*)&cp
[len
+hoplen
-4]),
274 hoplen
!=8 ? "" : ipaddr_string(&cp
[len
]));
279 printf("%s", ptr
== len
? " ^ " : "");
282 printf(" [%d hops not recorded]} ", cp
[3]>>4);
291 ip_optprint(register const u_char
*cp
, u_int length
)
295 for (; length
> 0; cp
+= len
, length
-= len
) {
298 len
= (tt
== IPOPT_NOP
|| tt
== IPOPT_EOL
) ? 1 : cp
[1];
300 printf("[|ip op len %d]", len
);
303 if (&cp
[1] >= snapend
|| cp
+ len
> snapend
) {
312 printf("-%d", length
- 1);
323 #ifndef IPOPT_SECURITY
324 #define IPOPT_SECURITY 130
325 #endif /* IPOPT_SECURITY */
327 printf(" SECURITY{%d}", len
);
331 ip_printroute("RR", cp
, len
);
335 ip_printroute("SSRR", cp
, len
);
339 ip_printroute("LSRR", cp
, len
);
343 #define IPOPT_RA 148 /* router alert */
349 else if (cp
[2] || cp
[3])
350 printf("%d.%d", cp
[2], cp
[3]);
354 printf(" IPOPT-%d{%d}", cp
[0], len
);
361 * compute an IP header checksum.
362 * don't modifiy the packet.
365 in_cksum(const u_short
*addr
, register int len
, u_short csum
)
368 const u_short
*w
= addr
;
373 * Our algorithm is simple, using a 32 bit accumulator (sum),
374 * we add sequential 16 bit words to it, and at the end, fold
375 * back all the carry bits from the top 16 bits into the lower
383 sum
+= htons(*(u_char
*)w
<<8);
386 * add back carry outs from top 16 bits to low 16 bits
388 sum
= (sum
>> 16) + (sum
& 0xffff); /* add hi 16 to low 16 */
389 sum
+= (sum
>> 16); /* add carry */
390 answer
= ~sum
; /* truncate to 16 bits */
395 * print an IP datagram.
398 ip_print(register const u_char
*bp
, register u_int length
)
400 register const struct ip
*ip
;
401 register u_int hlen
, len
, len0
, off
;
402 register const u_char
*cp
;
406 ip
= (const struct ip
*)bp
;
409 * If the IP header is not aligned, copy into abuf.
410 * This will never happen with BPF. It does happen raw packet
414 static u_char
*abuf
= NULL
;
415 static int didwarn
= 0;
418 abuf
= (u_char
*)malloc(snaplen
);
420 error("ip_print: malloc");
422 memcpy((char *)abuf
, (char *)ip
, min(length
, snaplen
));
423 snapend
+= abuf
- (u_char
*)ip
;
425 ip
= (struct ip
*)abuf
;
426 /* We really want libpcap to give us aligned packets */
428 warning("compensating for unaligned libpcap packets");
433 if ((u_char
*)(ip
+ 1) > snapend
) {
437 if (length
< sizeof (struct ip
)) {
438 (void)printf("truncated-ip %d", length
);
441 hlen
= ip
->ip_hl
* 4;
443 len
= ntohs(ip
->ip_len
);
445 (void)printf("truncated-ip - %d bytes missing!",
451 * If this is fragment zero, hand it to the next higher
454 off
= ntohs(ip
->ip_off
);
455 if ((off
& 0x1fff) == 0) {
456 cp
= (const u_char
*)ip
+ hlen
;
459 if (nh
!= IPPROTO_TCP
&& nh
!= IPPROTO_UDP
) {
460 (void)printf("%s > %s: ", ipaddr_string(&ip
->ip_src
),
461 ipaddr_string(&ip
->ip_dst
));
467 #define IPPROTO_AH 51
471 advance
= ah_print(cp
, (const u_char
*)ip
);
477 #define IPPROTO_ESP 50
482 advance
= esp_print(cp
, (const u_char
*)ip
, &enh
);
491 #ifndef IPPROTO_IPCOMP
492 #define IPPROTO_IPCOMP 108
497 advance
= ipcomp_print(cp
, (const u_char
*)ip
, &enh
);
507 tcp_print(cp
, len
, (const u_char
*)ip
);
511 udp_print(cp
, len
, (const u_char
*)ip
);
515 icmp_print(cp
, len
, (const u_char
*)ip
);
519 #define IPPROTO_IGRP 9
522 igrp_print(cp
, len
, (const u_char
*)ip
);
527 (void)printf("%s > %s:", ipaddr_string(&ip
->ip_src
),
528 ipaddr_string(&ip
->ip_dst
));
530 (void)printf(" nd %d", len
);
534 egp_print(cp
, len
, (const u_char
*)ip
);
538 #define IPPROTO_OSPF 89
541 ospf_print(cp
, len
, (const u_char
*)ip
);
545 #define IPPROTO_IGMP 2
548 igmp_print(cp
, len
, (const u_char
*)ip
);
552 /* DVMRP multicast tunnel (ip-in-ip encapsulation) */
555 (void)printf("%s > %s: ",
556 ipaddr_string(&ip
->ip_src
),
557 ipaddr_string(&ip
->ip_dst
));
567 #ifndef IP6PROTO_ENCAP
568 #define IP6PROTO_ENCAP 41
571 /* ip6-in-ip encapsulation */
574 (void)printf("%s > %s: ",
575 ipaddr_string(&ip
->ip_src
),
576 ipaddr_string(&ip
->ip_dst
));
588 #define IPPROTO_GRE 47
592 (void)printf("gre %s > %s: ",
593 ipaddr_string(&ip
->ip_src
),
594 ipaddr_string(&ip
->ip_dst
));
598 printf(" (gre encap)");
603 #ifndef IPPROTO_MOBILE
604 #define IPPROTO_MOBILE 55
608 (void)printf("mobile %s > %s: ",
609 ipaddr_string(&ip
->ip_src
),
610 ipaddr_string(&ip
->ip_dst
));
611 mobile_print(cp
, len
);
613 printf(" (mobile encap)");
619 #define IPPROTO_PIM 103
627 (void)printf("%s > %s:", ipaddr_string(&ip
->ip_src
),
628 ipaddr_string(&ip
->ip_dst
));
630 (void)printf(" ip-proto-%d %d", nh
, len
);
636 * for fragmented datagrams, print id:size@offset. On all
637 * but the last stick a "+". For unfragmented datagrams, note
638 * the don't fragment flag.
640 len
= len0
; /* get the original length */
643 * if this isn't the first frag, we're missing the
644 * next level protocol header. print the ip addr.
647 (void)printf("%s > %s:", ipaddr_string(&ip
->ip_src
),
648 ipaddr_string(&ip
->ip_dst
));
655 (void)printf(" (frag %d:%u@%d%s)", ntohs(ip
->ip_id
), len
,
657 (off
& IP_MF
)? "+" : "");
659 } else if (off
& IP_DF
)
660 (void)printf(" (DF)");
663 (void)printf(" [tos 0x%x", (int)ip
->ip_tos
);
665 if (ip
->ip_tos
&0x02) {
666 (void)printf(",ECT");
674 (void)printf(" [ttl %d]", (int)ip
->ip_ttl
);
681 if (ip
->ip_ttl
> 1) {
682 (void)printf("%sttl %d", sep
, (int)ip
->ip_ttl
);
685 if ((off
& 0x3fff) == 0) {
686 (void)printf("%sid %d", sep
, (int)ntohs(ip
->ip_id
));
689 if ((u_char
*)ip
+ hlen
<= snapend
) {
690 sum
= in_cksum((const u_short
*)ip
, hlen
, 0);
692 (void)printf("%sbad cksum %x!", sep
,
697 if ((hlen
-= sizeof(struct ip
)) > 0) {
698 (void)printf("%soptlen=%d", sep
, hlen
);
699 ip_optprint((u_char
*)(ip
+ 1), hlen
);