]>
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.
26 #include <tcpdump-stdinc.h>
32 #include "addrtoname.h"
33 #include "interface.h"
34 #include "extract.h" /* must come after interface.h */
39 static const char tstr
[] = "[|ip]";
41 static const struct tok ip_option_values
[] = {
44 { IPOPT_TS
, "timestamp" },
45 { IPOPT_SECURITY
, "security" },
47 { IPOPT_SSRR
, "SSRR" },
48 { IPOPT_LSRR
, "LSRR" },
50 { IPOPT_RFC1393
, "traceroute" },
55 * print the recorded route in an IP RR, LSRR or SSRR option.
58 ip_printroute(register const u_char
*cp
, u_int length
)
64 printf(" [bad length %u]", length
);
68 printf(" [bad length %u]", length
);
70 if (ptr
< 3 || ((ptr
+ 1) & 3) || ptr
> length
+ 1)
71 printf(" [bad ptr %u]", cp
[2]);
73 for (len
= 3; len
< length
; len
+= 4) {
74 printf(" %s", ipaddr_string(&cp
[len
]));
81 * If source-routing is present and valid, return the final destination.
82 * Otherwise, return IP destination.
84 * This is used for UDP and TCP pseudo-header in the checksum
88 ip_finddst(const struct ip
*ip
)
95 cp
= (const u_char
*)(ip
+ 1);
96 length
= (IP_HL(ip
) << 2) - sizeof(struct ip
);
98 for (; length
> 0; cp
+= len
, length
-= len
) {
105 else if (tt
== IPOPT_NOP
)
120 UNALIGNED_MEMCPY(&retval
, cp
+ len
- 4, 4);
125 UNALIGNED_MEMCPY(&retval
, &ip
->ip_dst
.s_addr
, sizeof(u_int32_t
));
130 * Compute a V4-style checksum by building a pseudoheader.
133 nextproto4_cksum(const struct ip
*ip
, const u_int8_t
*data
,
134 u_int len
, u_int covlen
, u_int next_proto
)
143 struct cksum_vec vec
[2];
145 /* pseudo-header.. */
146 ph
.len
= htons((u_int16_t
)len
);
148 ph
.proto
= next_proto
;
149 UNALIGNED_MEMCPY(&ph
.src
, &ip
->ip_src
.s_addr
, sizeof(u_int32_t
));
151 UNALIGNED_MEMCPY(&ph
.dst
, &ip
->ip_dst
.s_addr
, sizeof(u_int32_t
));
153 ph
.dst
= ip_finddst(ip
);
155 vec
[0].ptr
= (const u_int8_t
*)(void *)&ph
;
156 vec
[0].len
= sizeof(ph
);
159 return (in_cksum(vec
, 2));
163 ip_printts(register const u_char
*cp
, u_int length
)
171 printf("[bad length %u]", length
);
175 hoplen
= ((cp
[3]&0xF) != IPOPT_TS_TSONLY
) ? 8 : 4;
176 if ((length
- 4) & (hoplen
-1))
177 printf("[bad length %u]", length
);
180 if (ptr
< 4 || ((ptr
- 4) & (hoplen
-1)) || ptr
> length
+ 1)
181 printf("[bad ptr %u]", cp
[2]);
183 case IPOPT_TS_TSONLY
:
186 case IPOPT_TS_TSANDADDR
:
190 * prespecified should really be 3, but some ones might send 2
191 * instead, and the IPOPT_TS_PRESPEC constant can apparently
192 * have both values, so we have to hard-code it here.
196 printf("PRESPEC2.0");
198 case 3: /* IPOPT_TS_PRESPEC */
202 printf("[bad ts type %d]", cp
[3]&0xF);
207 for (len
= 4; len
< length
; len
+= hoplen
) {
210 printf("%s%d@%s", type
, EXTRACT_32BITS(&cp
[len
+hoplen
-4]),
211 hoplen
!=8 ? "" : ipaddr_string(&cp
[len
]));
216 printf("%s", ptr
== len
? " ^ " : "");
219 printf(" [%d hops not recorded]} ", cp
[3]>>4);
228 ip_optprint(register const u_char
*cp
, u_int length
)
230 register u_int option_len
;
231 const char *sep
= "";
233 for (; length
> 0; cp
+= option_len
, length
-= option_len
) {
243 tok2str(ip_option_values
,"unknown %u",option_code
));
245 if (option_code
== IPOPT_NOP
||
246 option_code
== IPOPT_EOL
)
252 if (option_len
< 2) {
253 printf(" [bad length %u]", option_len
);
258 if (option_len
> length
) {
259 printf(" [bad length %u]", option_len
);
263 TCHECK2(*cp
, option_len
);
265 switch (option_code
) {
270 ip_printts(cp
, option_len
);
273 case IPOPT_RR
: /* fall through */
276 ip_printroute(cp
, option_len
);
280 if (option_len
< 4) {
281 printf(" [bad length %u]", option_len
);
285 if (EXTRACT_16BITS(&cp
[2]) != 0)
286 printf(" value %u", EXTRACT_16BITS(&cp
[2]));
289 case IPOPT_NOP
: /* nothing to print - fall through */
301 #define IP_RES 0x8000
303 static const struct tok ip_frag_values
[] = {
306 { IP_RES
, "rsvd" }, /* The RFC3514 evil ;-) bit */
310 struct ip_print_demux_state
{
319 ip_print_demux(netdissect_options
*ndo
,
320 struct ip_print_demux_state
*ipds
)
322 struct protoent
*proto
;
323 struct cksum_vec vec
[1];
329 ipds
->nh
= *ipds
->cp
;
330 ipds
->advance
= ah_print(gndo
, ipds
->cp
);
331 if (ipds
->advance
<= 0)
333 ipds
->cp
+= ipds
->advance
;
334 ipds
->len
-= ipds
->advance
;
340 ipds
->advance
= esp_print(ndo
, ipds
->cp
, ipds
->len
,
341 (const u_char
*)ipds
->ip
,
343 if (ipds
->advance
<= 0)
345 ipds
->cp
+= ipds
->advance
;
346 ipds
->len
-= ipds
->advance
+ padlen
;
347 ipds
->nh
= enh
& 0xff;
354 ipds
->advance
= ipcomp_print(ipds
->cp
, &enh
);
355 if (ipds
->advance
<= 0)
357 ipds
->cp
+= ipds
->advance
;
358 ipds
->len
-= ipds
->advance
;
359 ipds
->nh
= enh
& 0xff;
364 sctp_print(ipds
->cp
, (const u_char
*)ipds
->ip
, ipds
->len
);
368 dccp_print(ipds
->cp
, (const u_char
*)ipds
->ip
, ipds
->len
);
372 /* pass on the MF bit plus the offset to detect fragments */
373 tcp_print(ipds
->cp
, ipds
->len
, (const u_char
*)ipds
->ip
,
374 ipds
->off
& (IP_MF
|IP_OFFMASK
));
378 /* pass on the MF bit plus the offset to detect fragments */
379 udp_print(ipds
->cp
, ipds
->len
, (const u_char
*)ipds
->ip
,
380 ipds
->off
& (IP_MF
|IP_OFFMASK
));
384 /* pass on the MF bit plus the offset to detect fragments */
385 icmp_print(ipds
->cp
, ipds
->len
, (const u_char
*)ipds
->ip
,
386 ipds
->off
& (IP_MF
|IP_OFFMASK
));
391 * XXX - the current IANA protocol number assignments
392 * page lists 9 as "any private interior gateway
393 * (used by Cisco for their IGRP)" and 88 as
394 * "EIGRP" from Cisco.
396 * Recent BSD <netinet/in.h> headers define
397 * IP_PROTO_PIGP as 9 and IP_PROTO_IGRP as 88.
398 * We define IP_PROTO_PIGP as 9 and
399 * IP_PROTO_EIGRP as 88; those names better
400 * match was the current protocol number
403 igrp_print(ipds
->cp
, ipds
->len
, (const u_char
*)ipds
->ip
);
407 eigrp_print(ipds
->cp
, ipds
->len
);
411 ND_PRINT((ndo
, " nd %d", ipds
->len
));
415 egp_print(ipds
->cp
, ipds
->len
);
419 ospf_print(ipds
->cp
, ipds
->len
, (const u_char
*)ipds
->ip
);
423 igmp_print(ipds
->cp
, ipds
->len
);
427 /* DVMRP multicast tunnel (ip-in-ip encapsulation) */
428 ip_print(ndo
, ipds
->cp
, ipds
->len
);
430 ND_PRINT((ndo
, " (ipip-proto-4)"));
437 /* ip6-in-ip encapsulation */
438 ip6_print(ndo
, ipds
->cp
, ipds
->len
);
443 rsvp_print(ipds
->cp
, ipds
->len
);
448 gre_print(ipds
->cp
, ipds
->len
);
452 mobile_print(ipds
->cp
, ipds
->len
);
456 vec
[0].ptr
= ipds
->cp
;
457 vec
[0].len
= ipds
->len
;
458 pim_print(ipds
->cp
, ipds
->len
, in_cksum(vec
, 1));
462 if (packettype
== PT_CARP
) {
464 (void)printf("carp %s > %s: ",
465 ipaddr_string(&ipds
->ip
->ip_src
),
466 ipaddr_string(&ipds
->ip
->ip_dst
));
467 carp_print(ndo
, ipds
->cp
, ipds
->len
, ipds
->ip
->ip_ttl
);
470 (void)printf("vrrp %s > %s: ",
471 ipaddr_string(&ipds
->ip
->ip_src
),
472 ipaddr_string(&ipds
->ip
->ip_dst
));
473 vrrp_print(ipds
->cp
, ipds
->len
, ipds
->ip
->ip_ttl
);
478 pgm_print(ipds
->cp
, ipds
->len
, (const u_char
*)ipds
->ip
);
482 if (ndo
->ndo_nflag
==0 && (proto
= getprotobynumber(ipds
->nh
)) != NULL
)
483 ND_PRINT((ndo
, " %s", proto
->p_name
));
485 ND_PRINT((ndo
, " ip-proto-%d", ipds
->nh
));
486 ND_PRINT((ndo
, " %d", ipds
->len
));
492 ip_print_inner(netdissect_options
*ndo
,
494 u_int length
, u_int nh
,
497 struct ip_print_demux_state ipd
;
499 ipd
.ip
= (const struct ip
*)bp2
;
506 ip_print_demux(ndo
, &ipd
);
511 * print an IP datagram.
514 ip_print(netdissect_options
*ndo
,
518 struct ip_print_demux_state ipd
;
519 struct ip_print_demux_state
*ipds
=&ipd
;
522 struct cksum_vec vec
[1];
523 u_int16_t sum
, ip_sum
;
524 struct protoent
*proto
;
526 ipds
->ip
= (const struct ip
*)bp
;
527 if (IP_V(ipds
->ip
) != 4) { /* print version if != 4 */
528 printf("IP%u ", IP_V(ipds
->ip
));
529 if (IP_V(ipds
->ip
) == 6)
530 printf(", wrong link-layer encapsulation");
535 if ((u_char
*)(ipds
->ip
+ 1) > ndo
->ndo_snapend
) {
539 if (length
< sizeof (struct ip
)) {
540 (void)printf("truncated-ip %u", length
);
543 hlen
= IP_HL(ipds
->ip
) * 4;
544 if (hlen
< sizeof (struct ip
)) {
545 (void)printf("bad-hlen %u", hlen
);
549 ipds
->len
= EXTRACT_16BITS(&ipds
->ip
->ip_len
);
550 if (length
< ipds
->len
)
551 (void)printf("truncated-ip - %u bytes missing! ",
553 if (ipds
->len
< hlen
) {
556 (void)printf("bad-len %u", ipds
->len
);
560 /* we guess that it is a TSO send */
564 (void)printf("bad-len %u", ipds
->len
);
566 #endif /* GUESS_TSO */
570 * Cut off the snapshot length to the end of the IP payload.
572 ipend
= bp
+ ipds
->len
;
573 if (ipend
< ndo
->ndo_snapend
)
574 ndo
->ndo_snapend
= ipend
;
578 ipds
->off
= EXTRACT_16BITS(&ipds
->ip
->ip_off
);
581 (void)printf("(tos 0x%x", (int)ipds
->ip
->ip_tos
);
583 if (ipds
->ip
->ip_tos
& 0x03) {
584 switch (ipds
->ip
->ip_tos
& 0x03) {
586 (void)printf(",ECT(1)");
589 (void)printf(",ECT(0)");
596 if (ipds
->ip
->ip_ttl
>= 1)
597 (void)printf(", ttl %u", ipds
->ip
->ip_ttl
);
600 * for the firewall guys, print id, offset.
601 * On all but the last stick a "+" in the flags portion.
602 * For unfragmented datagrams, note the don't fragment flag.
605 (void)printf(", id %u, offset %u, flags [%s], proto %s (%u)",
606 EXTRACT_16BITS(&ipds
->ip
->ip_id
),
607 (ipds
->off
& 0x1fff) * 8,
608 bittok2str(ip_frag_values
, "none", ipds
->off
&0xe000),
609 tok2str(ipproto_values
,"unknown",ipds
->ip
->ip_p
),
612 (void)printf(", length %u", EXTRACT_16BITS(&ipds
->ip
->ip_len
));
614 if ((hlen
- sizeof(struct ip
)) > 0) {
615 printf(", options (");
616 ip_optprint((u_char
*)(ipds
->ip
+ 1), hlen
- sizeof(struct ip
));
620 if (!Kflag
&& (u_char
*)ipds
->ip
+ hlen
<= ndo
->ndo_snapend
) {
621 vec
[0].ptr
= (const u_int8_t
*)(void *)ipds
->ip
;
623 sum
= in_cksum(vec
, 1);
625 ip_sum
= EXTRACT_16BITS(&ipds
->ip
->ip_sum
);
626 (void)printf(", bad cksum %x (->%x)!", ip_sum
,
627 in_cksum_shouldbe(ip_sum
, sum
));
635 * If this is fragment zero, hand it to the next higher
638 if ((ipds
->off
& 0x1fff) == 0) {
639 ipds
->cp
= (const u_char
*)ipds
->ip
+ hlen
;
640 ipds
->nh
= ipds
->ip
->ip_p
;
642 if (ipds
->nh
!= IPPROTO_TCP
&& ipds
->nh
!= IPPROTO_UDP
&&
643 ipds
->nh
!= IPPROTO_SCTP
&& ipds
->nh
!= IPPROTO_DCCP
) {
644 (void)printf("%s > %s: ",
645 ipaddr_string(&ipds
->ip
->ip_src
),
646 ipaddr_string(&ipds
->ip
->ip_dst
));
648 ip_print_demux(ndo
, ipds
);
650 /* Ultra quiet now means that all this stuff should be suppressed */
651 if (qflag
> 1) return;
654 * if this isn't the first frag, we're missing the
655 * next level protocol header. print the ip addr
658 if (ipds
->off
& 0x1fff) {
659 (void)printf("%s > %s:", ipaddr_string(&ipds
->ip
->ip_src
),
660 ipaddr_string(&ipds
->ip
->ip_dst
));
661 if (!ndo
->ndo_nflag
&& (proto
= getprotobynumber(ipds
->ip
->ip_p
)) != NULL
)
662 (void)printf(" %s", proto
->p_name
);
664 (void)printf(" ip-proto-%d", ipds
->ip
->ip_p
);
670 ipN_print(register const u_char
*bp
, register u_int length
)
675 (void)printf("truncated-ip %d", length
);
678 memcpy (&hdr
, bp
, 4);
679 switch (IP_V(&hdr
)) {
681 ip_print (gndo
, bp
, length
);
685 ip6_print (gndo
, bp
, length
);
689 (void)printf("unknown ip %d", IP_V(&hdr
));
696 * c-style: whitesmith