]>
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.80 2000-01-25 01:03:23 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 u_int tr_rttlqid
; /* response ttl and qid */
76 #define TR_GETTTL(x) (int)(((x) >> 24) & 0xff)
77 #define TR_GETQID(x) ((x) & 0x00ffffff)
80 * Traceroute response format. A traceroute response has a tr_query at the
81 * beginning, followed by one tr_resp for each hop taken.
84 u_int tr_qarr
; /* query arrival time */
85 u_int tr_inaddr
; /* incoming interface address */
86 u_int tr_outaddr
; /* outgoing interface address */
87 u_int tr_rmtaddr
; /* parent address in source tree */
88 u_int tr_vifin
; /* input packet count on interface */
89 u_int tr_vifout
; /* output packet count on interface */
90 u_int tr_pktcnt
; /* total incoming packets for src-grp */
91 u_char tr_rproto
; /* routing proto deployed on router */
92 u_char tr_fttl
; /* ttl required to forward on outvif */
93 u_char tr_smask
; /* subnet mask for src addr */
94 u_char tr_rflags
; /* forwarding error codes */
97 /* defs within mtrace */
101 /* fields for tr_rflags (forwarding error codes) */
103 #define TR_WRONG_IF 1
109 #define TR_NO_SPACE 0x81
110 #define TR_OLD_ROUTER 0x82
112 /* fields for tr_rproto (routing protocol) */
113 #define TR_PROTO_DVMRP 1
114 #define TR_PROTO_MOSPF 2
115 #define TR_PROTO_PIM 3
116 #define TR_PROTO_CBT 4
118 static void print_mtrace(register const u_char
*bp
, register u_int len
)
120 register struct tr_query
*tr
= (struct tr_query
*)(bp
+ 8);
122 printf("mtrace %lu: %s to %s reply-to %s",
123 (u_long
)TR_GETQID(ntohl(tr
->tr_rttlqid
)),
124 ipaddr_string(&tr
->tr_src
), ipaddr_string(&tr
->tr_dst
),
125 ipaddr_string(&tr
->tr_raddr
));
126 if (IN_CLASSD(ntohl(tr
->tr_raddr
)))
127 printf(" with-ttl %d", TR_GETTTL(ntohl(tr
->tr_rttlqid
)));
130 static void print_mresp(register const u_char
*bp
, register u_int len
)
132 register struct tr_query
*tr
= (struct tr_query
*)(bp
+ 8);
134 printf("mresp %lu: %s to %s reply-to %s",
135 (u_long
)TR_GETQID(ntohl(tr
->tr_rttlqid
)),
136 ipaddr_string(&tr
->tr_src
), ipaddr_string(&tr
->tr_dst
),
137 ipaddr_string(&tr
->tr_raddr
));
138 if (IN_CLASSD(ntohl(tr
->tr_raddr
)))
139 printf(" with-ttl %d", TR_GETTTL(ntohl(tr
->tr_rttlqid
)));
143 igmp_print(register const u_char
*bp
, register u_int len
,
144 register const u_char
*bp2
)
146 register const struct ip
*ip
;
148 ip
= (const struct ip
*)bp2
;
149 (void)printf("%s > %s: ",
150 ipaddr_string(&ip
->ip_src
),
151 ipaddr_string(&ip
->ip_dst
));
154 (void)printf("igmp");
161 (void)printf("igmp query");
162 if (EXTRACT_32BITS(&bp
[4]))
163 (void)printf(" [gaddr %s]", ipaddr_string(&bp
[4]));
165 (void)printf(" [len %d]", len
);
168 (void)printf("igmp v1 report %s", ipaddr_string(&bp
[4]));
170 (void)printf(" [len %d]", len
);
173 (void)printf("igmp v2 report %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 pimv1");
187 pimv1_print(bp
, len
);
190 print_mresp(bp
, len
);
193 print_mtrace(bp
, len
);
196 (void)printf("igmp-%d", bp
[0]);
200 if (vflag
&& TTEST2(bp
[0], len
)) {
201 /* Check the IGMP checksum */
202 if (in_cksum((const u_short
*)bp
, len
, 0))
203 printf(" bad igmp cksum %x!", EXTRACT_16BITS(&bp
[2]));
207 fputs("[|igmp]", stdout
);
211 * print the recorded route in an IP RR, LSRR or SSRR option.
214 ip_printroute(const char *type
, register const u_char
*cp
, u_int length
)
216 register u_int ptr
= cp
[2] - 1;
219 printf(" %s{", type
);
220 if ((length
+ 1) & 3)
221 printf(" [bad length %d]", length
);
222 if (ptr
< 3 || ((ptr
+ 1) & 3) || ptr
> length
+ 1)
223 printf(" [bad ptr %d]", cp
[2]);
226 for (len
= 3; len
< length
; len
+= 4) {
229 printf("%s%s", type
, ipaddr_string(&cp
[len
]));
232 printf("%s}", ptr
== len
? "#" : "");
236 ip_printts(register const u_char
*cp
, u_int length
)
238 register u_int ptr
= cp
[2] - 1;
239 register u_int len
= 0;
244 hoplen
= ((cp
[3]&0xF) != IPOPT_TS_TSONLY
) ? 8 : 4;
245 if ((length
- 4) & (hoplen
-1))
246 printf("[bad length %d]", length
);
247 if (ptr
< 4 || ((ptr
- 4) & (hoplen
-1)) || ptr
> length
+ 1)
248 printf("[bad ptr %d]", cp
[2]);
250 case IPOPT_TS_TSONLY
:
253 case IPOPT_TS_TSANDADDR
:
257 * prespecified should really be 3, but some ones might send 2
258 * instead, and the IPOPT_TS_PRESPEC constant can apparently
259 * have both values, so we have to hard-code it here.
263 printf("PRESPEC2.0");
265 case 3: /* IPOPT_TS_PRESPEC */
269 printf("[bad ts type %d]", cp
[3]&0xF);
274 for (len
= 4; len
< length
; len
+= hoplen
) {
277 printf("%s%d@%s", type
, EXTRACT_32BITS(&cp
[len
+hoplen
-4]),
278 hoplen
!=8 ? "" : ipaddr_string(&cp
[len
]));
283 printf("%s", ptr
== len
? " ^ " : "");
286 printf(" [%d hops not recorded]} ", cp
[3]>>4);
295 ip_optprint(register const u_char
*cp
, u_int length
)
299 for (; length
> 0; cp
+= len
, length
-= len
) {
302 len
= (tt
== IPOPT_NOP
|| tt
== IPOPT_EOL
) ? 1 : cp
[1];
304 printf("[|ip op len %d]", len
);
307 if (&cp
[1] >= snapend
|| cp
+ len
> snapend
) {
316 printf("-%d", length
- 1);
327 #ifndef IPOPT_SECURITY
328 #define IPOPT_SECURITY 130
329 #endif /* IPOPT_SECURITY */
331 printf(" SECURITY{%d}", len
);
335 ip_printroute("RR", cp
, len
);
339 ip_printroute("SSRR", cp
, len
);
343 ip_printroute("LSRR", cp
, len
);
347 #define IPOPT_RA 148 /* router alert */
353 else if (cp
[2] || cp
[3])
354 printf("%d.%d", cp
[2], cp
[3]);
358 printf(" IPOPT-%d{%d}", cp
[0], len
);
365 * compute an IP header checksum.
366 * don't modifiy the packet.
369 in_cksum(const u_short
*addr
, register int len
, u_short csum
)
372 const u_short
*w
= addr
;
377 * Our algorithm is simple, using a 32 bit accumulator (sum),
378 * we add sequential 16 bit words to it, and at the end, fold
379 * back all the carry bits from the top 16 bits into the lower
387 sum
+= htons(*(u_char
*)w
<<8);
390 * add back carry outs from top 16 bits to low 16 bits
392 sum
= (sum
>> 16) + (sum
& 0xffff); /* add hi 16 to low 16 */
393 sum
+= (sum
>> 16); /* add carry */
394 answer
= ~sum
; /* truncate to 16 bits */
399 * print an IP datagram.
402 ip_print(register const u_char
*bp
, register u_int length
)
404 register const struct ip
*ip
;
405 register u_int hlen
, len
, len0
, off
;
406 register const u_char
*cp
;
410 ip
= (const struct ip
*)bp
;
413 * If the IP header is not aligned, copy into abuf.
414 * This will never happen with BPF. It does happen raw packet
418 static u_char
*abuf
= NULL
;
419 static int didwarn
= 0;
422 abuf
= (u_char
*)malloc(snaplen
);
424 error("ip_print: malloc");
426 memcpy((char *)abuf
, (char *)ip
, min(length
, snaplen
));
427 snapend
+= abuf
- (u_char
*)ip
;
429 ip
= (struct ip
*)abuf
;
430 /* We really want libpcap to give us aligned packets */
432 warning("compensating for unaligned libpcap packets");
437 if ((u_char
*)(ip
+ 1) > snapend
) {
441 if (length
< sizeof (struct ip
)) {
442 (void)printf("truncated-ip %d", length
);
445 hlen
= ip
->ip_hl
* 4;
446 if (hlen
< sizeof (struct ip
)) {
447 (void)printf("bad-hlen %d", hlen
);
451 len
= ntohs(ip
->ip_len
);
453 (void)printf("truncated-ip - %d bytes missing!",
459 * If this is fragment zero, hand it to the next higher
462 off
= ntohs(ip
->ip_off
);
463 if ((off
& 0x1fff) == 0) {
464 cp
= (const u_char
*)ip
+ hlen
;
467 if (nh
!= IPPROTO_TCP
&& nh
!= IPPROTO_UDP
) {
468 (void)printf("%s > %s: ", ipaddr_string(&ip
->ip_src
),
469 ipaddr_string(&ip
->ip_dst
));
475 #define IPPROTO_AH 51
479 advance
= ah_print(cp
, (const u_char
*)ip
);
485 #define IPPROTO_ESP 50
490 advance
= esp_print(cp
, (const u_char
*)ip
, &enh
);
499 #ifndef IPPROTO_IPCOMP
500 #define IPPROTO_IPCOMP 108
505 advance
= ipcomp_print(cp
, (const u_char
*)ip
, &enh
);
515 tcp_print(cp
, len
, (const u_char
*)ip
);
519 udp_print(cp
, len
, (const u_char
*)ip
);
523 icmp_print(cp
, len
, (const u_char
*)ip
);
527 #define IPPROTO_IGRP 9
530 igrp_print(cp
, len
, (const u_char
*)ip
);
535 (void)printf("%s > %s:", ipaddr_string(&ip
->ip_src
),
536 ipaddr_string(&ip
->ip_dst
));
538 (void)printf(" nd %d", len
);
542 egp_print(cp
, len
, (const u_char
*)ip
);
546 #define IPPROTO_OSPF 89
549 ospf_print(cp
, len
, (const u_char
*)ip
);
553 #define IPPROTO_IGMP 2
556 igmp_print(cp
, len
, (const u_char
*)ip
);
560 /* DVMRP multicast tunnel (ip-in-ip encapsulation) */
563 (void)printf("%s > %s: ",
564 ipaddr_string(&ip
->ip_src
),
565 ipaddr_string(&ip
->ip_dst
));
575 #ifndef IP6PROTO_ENCAP
576 #define IP6PROTO_ENCAP 41
579 /* ip6-in-ip encapsulation */
582 (void)printf("%s > %s: ",
583 ipaddr_string(&ip
->ip_src
),
584 ipaddr_string(&ip
->ip_dst
));
596 #define IPPROTO_GRE 47
600 (void)printf("gre %s > %s: ",
601 ipaddr_string(&ip
->ip_src
),
602 ipaddr_string(&ip
->ip_dst
));
606 printf(" (gre encap)");
611 #ifndef IPPROTO_MOBILE
612 #define IPPROTO_MOBILE 55
616 (void)printf("mobile %s > %s: ",
617 ipaddr_string(&ip
->ip_src
),
618 ipaddr_string(&ip
->ip_dst
));
619 mobile_print(cp
, len
);
621 printf(" (mobile encap)");
627 #define IPPROTO_PIM 103
635 (void)printf("%s > %s:", ipaddr_string(&ip
->ip_src
),
636 ipaddr_string(&ip
->ip_dst
));
638 (void)printf(" ip-proto-%d %d", nh
, len
);
643 /* Ultra quiet now means that all this stuff should be suppressed */
645 if (qflag
> 1) return;
649 * for fragmented datagrams, print id:size@offset. On all
650 * but the last stick a "+". For unfragmented datagrams, note
651 * the don't fragment flag.
653 len
= len0
; /* get the original length */
656 * if this isn't the first frag, we're missing the
657 * next level protocol header. print the ip addr.
660 (void)printf("%s > %s:", ipaddr_string(&ip
->ip_src
),
661 ipaddr_string(&ip
->ip_dst
));
668 (void)printf(" (frag %d:%u@%d%s)", ntohs(ip
->ip_id
), len
,
670 (off
& IP_MF
)? "+" : "");
672 } else if (off
& IP_DF
)
673 (void)printf(" (DF)");
676 (void)printf(" [tos 0x%x", (int)ip
->ip_tos
);
678 if (ip
->ip_tos
&0x02) {
679 (void)printf(",ECT");
687 (void)printf(" [ttl %d]", (int)ip
->ip_ttl
);
694 if (ip
->ip_ttl
> 1) {
695 (void)printf("%sttl %d", sep
, (int)ip
->ip_ttl
);
698 if ((off
& 0x3fff) == 0) {
699 (void)printf("%sid %d", sep
, (int)ntohs(ip
->ip_id
));
702 if ((u_char
*)ip
+ hlen
<= snapend
) {
703 sum
= in_cksum((const u_short
*)ip
, hlen
, 0);
705 (void)printf("%sbad cksum %x!", sep
,
710 if ((hlen
-= sizeof(struct ip
)) > 0) {
711 (void)printf("%soptlen=%d", sep
, hlen
);
712 ip_optprint((u_char
*)(ip
+ 1), hlen
);