]>
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
[] _U_
=
24 "@(#) $Header: /tcpdump/master/tcpdump/print-ip.c,v 1.159 2007-09-14 01:29:28 guy Exp $ (LBL)";
31 #include <tcpdump-stdinc.h>
37 #include "addrtoname.h"
38 #include "interface.h"
39 #include "extract.h" /* must come after interface.h */
44 struct tok ip_option_values
[] = {
47 { IPOPT_TS
, "timestamp" },
48 { IPOPT_SECURITY
, "security" },
50 { IPOPT_SSRR
, "SSRR" },
51 { IPOPT_LSRR
, "LSRR" },
53 { IPOPT_RFC1393
, "traceroute" },
58 * print the recorded route in an IP RR, LSRR or SSRR option.
61 ip_printroute(register const u_char
*cp
, u_int length
)
67 printf(" [bad length %u]", length
);
71 printf(" [bad length %u]", length
);
73 if (ptr
< 3 || ((ptr
+ 1) & 3) || ptr
> length
+ 1)
74 printf(" [bad ptr %u]", cp
[2]);
76 for (len
= 3; len
< length
; len
+= 4) {
77 printf(" %s", ipaddr_string(&cp
[len
]));
84 * If source-routing is present and valid, return the final destination.
85 * Otherwise, return IP destination.
87 * This is used for UDP and TCP pseudo-header in the checksum
91 ip_finddst(const struct ip
*ip
)
98 cp
= (const u_char
*)(ip
+ 1);
99 length
= (IP_HL(ip
) << 2) - sizeof(struct ip
);
101 for (; length
> 0; cp
+= len
, length
-= len
) {
108 else if (tt
== IPOPT_NOP
)
123 memcpy(&retval
, cp
+ len
- 4, 4);
128 memcpy(&retval
, &ip
->ip_dst
.s_addr
, sizeof(u_int32_t
));
133 ip_printts(register const u_char
*cp
, u_int length
)
141 printf("[bad length %u]", length
);
145 hoplen
= ((cp
[3]&0xF) != IPOPT_TS_TSONLY
) ? 8 : 4;
146 if ((length
- 4) & (hoplen
-1))
147 printf("[bad length %u]", length
);
150 if (ptr
< 4 || ((ptr
- 4) & (hoplen
-1)) || ptr
> length
+ 1)
151 printf("[bad ptr %u]", cp
[2]);
153 case IPOPT_TS_TSONLY
:
156 case IPOPT_TS_TSANDADDR
:
160 * prespecified should really be 3, but some ones might send 2
161 * instead, and the IPOPT_TS_PRESPEC constant can apparently
162 * have both values, so we have to hard-code it here.
166 printf("PRESPEC2.0");
168 case 3: /* IPOPT_TS_PRESPEC */
172 printf("[bad ts type %d]", cp
[3]&0xF);
177 for (len
= 4; len
< length
; len
+= hoplen
) {
180 printf("%s%d@%s", type
, EXTRACT_32BITS(&cp
[len
+hoplen
-4]),
181 hoplen
!=8 ? "" : ipaddr_string(&cp
[len
]));
186 printf("%s", ptr
== len
? " ^ " : "");
189 printf(" [%d hops not recorded]} ", cp
[3]>>4);
198 ip_optprint(register const u_char
*cp
, u_int length
)
200 register u_int option_len
;
201 const char *sep
= "";
203 for (; length
> 0; cp
+= option_len
, length
-= option_len
) {
213 tok2str(ip_option_values
,"unknown %u",option_code
));
215 if (option_code
== IPOPT_NOP
||
216 option_code
== IPOPT_EOL
)
222 if (option_len
< 2) {
223 printf(" [bad length %u]", option_len
);
228 if (option_len
> length
) {
229 printf(" [bad length %u]", option_len
);
233 TCHECK2(*cp
, option_len
);
235 switch (option_code
) {
240 ip_printts(cp
, option_len
);
243 case IPOPT_RR
: /* fall through */
246 ip_printroute(cp
, option_len
);
250 if (option_len
< 4) {
251 printf(" [bad length %u]", option_len
);
255 if (EXTRACT_16BITS(&cp
[2]) != 0)
256 printf(" value %u", EXTRACT_16BITS(&cp
[2]));
259 case IPOPT_NOP
: /* nothing to print - fall through */
271 #define IP_RES 0x8000
273 static struct tok ip_frag_values
[] = {
276 { IP_RES
, "rsvd" }, /* The RFC3514 evil ;-) bit */
280 struct ip_print_demux_state
{
289 ip_print_demux(netdissect_options
*ndo
,
290 struct ip_print_demux_state
*ipds
)
292 struct protoent
*proto
;
293 struct cksum_vec vec
[1];
299 ipds
->nh
= *ipds
->cp
;
300 ipds
->advance
= ah_print(ipds
->cp
);
301 if (ipds
->advance
<= 0)
303 ipds
->cp
+= ipds
->advance
;
304 ipds
->len
-= ipds
->advance
;
310 ipds
->advance
= esp_print(ndo
, ipds
->cp
, ipds
->len
,
311 (const u_char
*)ipds
->ip
,
313 if (ipds
->advance
<= 0)
315 ipds
->cp
+= ipds
->advance
;
316 ipds
->len
-= ipds
->advance
+ padlen
;
317 ipds
->nh
= enh
& 0xff;
324 ipds
->advance
= ipcomp_print(ipds
->cp
, &enh
);
325 if (ipds
->advance
<= 0)
327 ipds
->cp
+= ipds
->advance
;
328 ipds
->len
-= ipds
->advance
;
329 ipds
->nh
= enh
& 0xff;
334 sctp_print(ipds
->cp
, (const u_char
*)ipds
->ip
, ipds
->len
);
338 dccp_print(ipds
->cp
, (const u_char
*)ipds
->ip
, ipds
->len
);
342 /* pass on the MF bit plus the offset to detect fragments */
343 tcp_print(ipds
->cp
, ipds
->len
, (const u_char
*)ipds
->ip
,
344 ipds
->off
& (IP_MF
|IP_OFFMASK
));
348 /* pass on the MF bit plus the offset to detect fragments */
349 udp_print(ipds
->cp
, ipds
->len
, (const u_char
*)ipds
->ip
,
350 ipds
->off
& (IP_MF
|IP_OFFMASK
));
354 /* pass on the MF bit plus the offset to detect fragments */
355 icmp_print(ipds
->cp
, ipds
->len
, (const u_char
*)ipds
->ip
,
356 ipds
->off
& (IP_MF
|IP_OFFMASK
));
361 * XXX - the current IANA protocol number assignments
362 * page lists 9 as "any private interior gateway
363 * (used by Cisco for their IGRP)" and 88 as
364 * "EIGRP" from Cisco.
366 * Recent BSD <netinet/in.h> headers define
367 * IP_PROTO_PIGP as 9 and IP_PROTO_IGRP as 88.
368 * We define IP_PROTO_PIGP as 9 and
369 * IP_PROTO_EIGRP as 88; those names better
370 * match was the current protocol number
373 igrp_print(ipds
->cp
, ipds
->len
, (const u_char
*)ipds
->ip
);
377 eigrp_print(ipds
->cp
, ipds
->len
);
381 ND_PRINT((ndo
, " nd %d", ipds
->len
));
385 egp_print(ipds
->cp
, ipds
->len
);
389 ospf_print(ipds
->cp
, ipds
->len
, (const u_char
*)ipds
->ip
);
393 igmp_print(ipds
->cp
, ipds
->len
);
397 /* DVMRP multicast tunnel (ip-in-ip encapsulation) */
398 ip_print(ndo
, ipds
->cp
, ipds
->len
);
400 ND_PRINT((ndo
, " (ipip-proto-4)"));
407 /* ip6-in-ip encapsulation */
408 ip6_print(ndo
, ipds
->cp
, ipds
->len
);
413 rsvp_print(ipds
->cp
, ipds
->len
);
418 gre_print(ipds
->cp
, ipds
->len
);
422 mobile_print(ipds
->cp
, ipds
->len
);
426 vec
[0].ptr
= ipds
->cp
;
427 vec
[0].len
= ipds
->len
;
428 pim_print(ipds
->cp
, ipds
->len
, in_cksum(vec
, 1));
432 vrrp_print(ipds
->cp
, ipds
->len
, ipds
->ip
->ip_ttl
);
436 pgm_print(ipds
->cp
, ipds
->len
, (const u_char
*)ipds
->ip
);
440 if ((proto
= getprotobynumber(ipds
->nh
)) != NULL
)
441 ND_PRINT((ndo
, " %s", proto
->p_name
));
443 ND_PRINT((ndo
, " ip-proto-%d", ipds
->nh
));
444 ND_PRINT((ndo
, " %d", ipds
->len
));
450 ip_print_inner(netdissect_options
*ndo
,
452 u_int length
, u_int nh
,
455 struct ip_print_demux_state ipd
;
457 ipd
.ip
= (const struct ip
*)bp2
;
464 ip_print_demux(ndo
, &ipd
);
469 * print an IP datagram.
472 ip_print(netdissect_options
*ndo
,
476 struct ip_print_demux_state ipd
;
477 struct ip_print_demux_state
*ipds
=&ipd
;
480 struct cksum_vec vec
[1];
481 u_int16_t sum
, ip_sum
;
482 struct protoent
*proto
;
484 ipds
->ip
= (const struct ip
*)bp
;
485 if (IP_V(ipds
->ip
) != 4) { /* print version if != 4 */
486 printf("IP%u ", IP_V(ipds
->ip
));
487 if (IP_V(ipds
->ip
) == 6)
488 printf(", wrong link-layer encapsulation");
493 if ((u_char
*)(ipds
->ip
+ 1) > ndo
->ndo_snapend
) {
497 if (length
< sizeof (struct ip
)) {
498 (void)printf("truncated-ip %u", length
);
501 hlen
= IP_HL(ipds
->ip
) * 4;
502 if (hlen
< sizeof (struct ip
)) {
503 (void)printf("bad-hlen %u", hlen
);
507 ipds
->len
= EXTRACT_16BITS(&ipds
->ip
->ip_len
);
508 if (length
< ipds
->len
)
509 (void)printf("truncated-ip - %u bytes missing! ",
511 if (ipds
->len
< hlen
) {
514 (void)printf("bad-len %u", ipds
->len
);
518 /* we guess that it is a TSO send */
522 (void)printf("bad-len %u", ipds
->len
);
524 #endif /* GUESS_TSO */
528 * Cut off the snapshot length to the end of the IP payload.
530 ipend
= bp
+ ipds
->len
;
531 if (ipend
< ndo
->ndo_snapend
)
532 ndo
->ndo_snapend
= ipend
;
536 ipds
->off
= EXTRACT_16BITS(&ipds
->ip
->ip_off
);
539 (void)printf("(tos 0x%x", (int)ipds
->ip
->ip_tos
);
541 if (ipds
->ip
->ip_tos
& 0x03) {
542 switch (ipds
->ip
->ip_tos
& 0x03) {
544 (void)printf(",ECT(1)");
547 (void)printf(",ECT(0)");
554 if (ipds
->ip
->ip_ttl
>= 1)
555 (void)printf(", ttl %u", ipds
->ip
->ip_ttl
);
558 * for the firewall guys, print id, offset.
559 * On all but the last stick a "+" in the flags portion.
560 * For unfragmented datagrams, note the don't fragment flag.
563 (void)printf(", id %u, offset %u, flags [%s], proto %s (%u)",
564 EXTRACT_16BITS(&ipds
->ip
->ip_id
),
565 (ipds
->off
& 0x1fff) * 8,
566 bittok2str(ip_frag_values
, "none", ipds
->off
&0xe000),
567 tok2str(ipproto_values
,"unknown",ipds
->ip
->ip_p
),
570 (void)printf(", length %u", EXTRACT_16BITS(&ipds
->ip
->ip_len
));
572 if ((hlen
- sizeof(struct ip
)) > 0) {
573 printf(", options (");
574 ip_optprint((u_char
*)(ipds
->ip
+ 1), hlen
- sizeof(struct ip
));
578 if (!Kflag
&& (u_char
*)ipds
->ip
+ hlen
<= ndo
->ndo_snapend
) {
579 vec
[0].ptr
= (const u_int8_t
*)(void *)ipds
->ip
;
581 sum
= in_cksum(vec
, 1);
583 ip_sum
= EXTRACT_16BITS(&ipds
->ip
->ip_sum
);
584 (void)printf(", bad cksum %x (->%x)!", ip_sum
,
585 in_cksum_shouldbe(ip_sum
, sum
));
593 * If this is fragment zero, hand it to the next higher
596 if ((ipds
->off
& 0x1fff) == 0) {
597 ipds
->cp
= (const u_char
*)ipds
->ip
+ hlen
;
598 ipds
->nh
= ipds
->ip
->ip_p
;
600 if (ipds
->nh
!= IPPROTO_TCP
&& ipds
->nh
!= IPPROTO_UDP
&&
601 ipds
->nh
!= IPPROTO_SCTP
&& ipds
->nh
!= IPPROTO_DCCP
) {
602 (void)printf("%s > %s: ",
603 ipaddr_string(&ipds
->ip
->ip_src
),
604 ipaddr_string(&ipds
->ip
->ip_dst
));
606 ip_print_demux(ndo
, ipds
);
608 /* Ultra quiet now means that all this stuff should be suppressed */
609 if (qflag
> 1) return;
612 * if this isn't the first frag, we're missing the
613 * next level protocol header. print the ip addr
616 if (ipds
->off
& 0x1fff) {
617 (void)printf("%s > %s:", ipaddr_string(&ipds
->ip
->ip_src
),
618 ipaddr_string(&ipds
->ip
->ip_dst
));
619 if ((proto
= getprotobynumber(ipds
->ip
->ip_p
)) != NULL
)
620 (void)printf(" %s", proto
->p_name
);
622 (void)printf(" ip-proto-%d", ipds
->ip
->ip_p
);
628 ipN_print(register const u_char
*bp
, register u_int length
)
632 ip
= (struct ip
*)bp
;
634 (void)printf("truncated-ip %d", length
);
637 memcpy (&hdr
, (char *)ip
, 4);
638 switch (IP_V(&hdr
)) {
640 ip_print (gndo
, bp
, length
);
644 ip6_print (gndo
, bp
, length
);
648 (void)printf("unknown ip %d", IP_V(&hdr
));
655 * c-style: whitesmith