]>
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.
22 /* \summary: IP printer */
28 #include "netdissect-stdinc.h"
32 #include "netdissect.h"
33 #include "addrtoname.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(netdissect_options
*ndo
,
59 const u_char
*cp
, u_int length
)
65 ND_PRINT(" [bad length %u]", length
);
69 ND_PRINT(" [bad length %u]", length
);
71 ptr
= EXTRACT_U_1(cp
+ 2) - 1;
72 if (ptr
< 3 || ((ptr
+ 1) & 3) || ptr
> length
+ 1)
73 ND_PRINT(" [bad ptr %u]", EXTRACT_U_1(cp
+ 2));
75 for (len
= 3; len
< length
; len
+= 4) {
76 ND_TCHECK_4(cp
+ len
);
77 ND_PRINT(" %s", ipaddr_string(ndo
, cp
+ len
));
88 * If source-routing is present and valid, return the final destination.
89 * Otherwise, return IP destination.
91 * This is used for UDP and TCP pseudo-header in the checksum
95 ip_finddst(netdissect_options
*ndo
,
102 cp
= (const u_char
*)(ip
+ 1);
103 length
= (IP_HL(ip
) << 2);
104 if (length
< sizeof(struct ip
))
106 length
-= sizeof(struct ip
);
108 for (; length
!= 0; cp
+= len
, length
-= len
) {
112 tt
= EXTRACT_U_1(cp
);
115 else if (tt
== IPOPT_NOP
)
119 len
= EXTRACT_U_1(cp
+ 1);
125 ND_TCHECK_LEN(cp
, len
);
132 return (EXTRACT_IPV4_TO_NETWORK_ORDER(cp
+ len
- 4));
136 return (EXTRACT_IPV4_TO_NETWORK_ORDER(ip
->ip_dst
));
140 * Compute a V4-style checksum by building a pseudoheader.
143 nextproto4_cksum(netdissect_options
*ndo
,
144 const struct ip
*ip
, const uint8_t *data
,
145 u_int len
, u_int covlen
, u_int next_proto
)
154 struct cksum_vec vec
[2];
156 /* pseudo-header.. */
157 ph
.len
= htons((uint16_t)len
);
159 ph
.proto
= next_proto
;
160 ph
.src
= EXTRACT_IPV4_TO_NETWORK_ORDER(ip
->ip_src
);
162 ph
.dst
= EXTRACT_IPV4_TO_NETWORK_ORDER(ip
->ip_dst
);
164 ph
.dst
= ip_finddst(ndo
, ip
);
166 vec
[0].ptr
= (const uint8_t *)(void *)&ph
;
167 vec
[0].len
= sizeof(ph
);
170 return (in_cksum(vec
, 2));
174 ip_printts(netdissect_options
*ndo
,
175 const u_char
*cp
, u_int length
)
183 ND_PRINT("[bad length %u]", length
);
188 hoplen
= ((EXTRACT_U_1(cp
+ 3) & 0xF) != IPOPT_TS_TSONLY
) ? 8 : 4;
189 if ((length
- 4) & (hoplen
-1))
190 ND_PRINT("[bad length %u]", length
);
192 ptr
= EXTRACT_U_1(cp
+ 2) - 1;
194 if (ptr
< 4 || ((ptr
- 4) & (hoplen
-1)) || ptr
> length
+ 1)
195 ND_PRINT("[bad ptr %u]", EXTRACT_U_1(cp
+ 2));
197 switch (EXTRACT_U_1(cp
+ 3)&0xF) {
198 case IPOPT_TS_TSONLY
:
201 case IPOPT_TS_TSANDADDR
:
205 * prespecified should really be 3, but some ones might send 2
206 * instead, and the IPOPT_TS_PRESPEC constant can apparently
207 * have both values, so we have to hard-code it here.
211 ND_PRINT("PRESPEC2.0");
213 case 3: /* IPOPT_TS_PRESPEC */
217 ND_PRINT("[bad ts type %u]", EXTRACT_U_1(cp
+ 3)&0xF);
222 for (len
= 4; len
< length
; len
+= hoplen
) {
225 ND_TCHECK_LEN(cp
+ len
, hoplen
);
226 ND_PRINT("%s%u@%s", type
, EXTRACT_BE_U_4(cp
+ len
+ hoplen
- 4),
227 hoplen
!=8 ? "" : ipaddr_string(ndo
, cp
+ len
));
232 ND_PRINT("%s", ptr
== len
? " ^ " : "");
234 if (EXTRACT_U_1(cp
+ 3) >> 4)
235 ND_PRINT(" [%u hops not recorded]} ", EXTRACT_U_1(cp
+ 3)>>4);
248 ip_optprint(netdissect_options
*ndo
,
249 const u_char
*cp
, u_int length
)
252 const char *sep
= "";
254 for (; length
> 0; cp
+= option_len
, length
-= option_len
) {
261 option_code
= EXTRACT_U_1(cp
);
264 tok2str(ip_option_values
,"unknown %u",option_code
));
266 if (option_code
== IPOPT_NOP
||
267 option_code
== IPOPT_EOL
)
272 option_len
= EXTRACT_U_1(cp
+ 1);
273 if (option_len
< 2) {
274 ND_PRINT(" [bad length %u]", option_len
);
279 if (option_len
> length
) {
280 ND_PRINT(" [bad length %u]", option_len
);
284 ND_TCHECK_LEN(cp
, option_len
);
286 switch (option_code
) {
291 if (ip_printts(ndo
, cp
, option_len
) == -1)
295 case IPOPT_RR
: /* fall through */
298 if (ip_printroute(ndo
, cp
, option_len
) == -1)
303 if (option_len
< 4) {
304 ND_PRINT(" [bad length %u]", option_len
);
308 if (EXTRACT_BE_U_2(cp
+ 2) != 0)
309 ND_PRINT(" value %u", EXTRACT_BE_U_2(cp
+ 2));
312 case IPOPT_NOP
: /* nothing to print - fall through */
321 ND_PRINT("%s", tstr
);
324 #define IP_RES 0x8000
326 static const struct tok ip_frag_values
[] = {
329 { IP_RES
, "rsvd" }, /* The RFC3514 evil ;-) bit */
333 struct ip_print_demux_state
{
342 ip_print_demux(netdissect_options
*ndo
,
343 struct ip_print_demux_state
*ipds
)
351 if (!ND_TTEST_1(ipds
->cp
)) {
355 ipds
->nh
= EXTRACT_U_1(ipds
->cp
);
356 ipds
->advance
= ah_print(ndo
, ipds
->cp
);
357 if (ipds
->advance
<= 0)
359 ipds
->cp
+= ipds
->advance
;
360 ipds
->len
-= ipds
->advance
;
366 ipds
->advance
= esp_print(ndo
, ipds
->cp
, ipds
->len
,
367 (const u_char
*)ipds
->ip
,
369 if (ipds
->advance
<= 0)
371 ipds
->cp
+= ipds
->advance
;
372 ipds
->len
-= ipds
->advance
+ padlen
;
373 ipds
->nh
= enh
& 0xff;
379 ipcomp_print(ndo
, ipds
->cp
);
381 * Either this has decompressed the payload and
382 * printed it, in which case there's nothing more
383 * to do, or it hasn't, in which case there's
384 * nothing more to do.
390 sctp_print(ndo
, ipds
->cp
, (const u_char
*)ipds
->ip
, ipds
->len
);
394 dccp_print(ndo
, ipds
->cp
, (const u_char
*)ipds
->ip
, ipds
->len
);
398 /* pass on the MF bit plus the offset to detect fragments */
399 tcp_print(ndo
, ipds
->cp
, ipds
->len
, (const u_char
*)ipds
->ip
,
400 ipds
->off
& (IP_MF
|IP_OFFMASK
));
404 /* pass on the MF bit plus the offset to detect fragments */
405 udp_print(ndo
, ipds
->cp
, ipds
->len
, (const u_char
*)ipds
->ip
,
406 ipds
->off
& (IP_MF
|IP_OFFMASK
));
410 /* pass on the MF bit plus the offset to detect fragments */
411 icmp_print(ndo
, ipds
->cp
, ipds
->len
, (const u_char
*)ipds
->ip
,
412 ipds
->off
& (IP_MF
|IP_OFFMASK
));
417 * XXX - the current IANA protocol number assignments
418 * page lists 9 as "any private interior gateway
419 * (used by Cisco for their IGRP)" and 88 as
420 * "EIGRP" from Cisco.
422 * Recent BSD <netinet/in.h> headers define
423 * IP_PROTO_PIGP as 9 and IP_PROTO_IGRP as 88.
424 * We define IP_PROTO_PIGP as 9 and
425 * IP_PROTO_EIGRP as 88; those names better
426 * match was the current protocol number
429 igrp_print(ndo
, ipds
->cp
, ipds
->len
);
433 eigrp_print(ndo
, ipds
->cp
, ipds
->len
);
437 ND_PRINT(" nd %u", ipds
->len
);
441 egp_print(ndo
, ipds
->cp
, ipds
->len
);
445 ospf_print(ndo
, ipds
->cp
, ipds
->len
, (const u_char
*)ipds
->ip
);
449 igmp_print(ndo
, ipds
->cp
, ipds
->len
);
453 /* DVMRP multicast tunnel (ip-in-ip encapsulation) */
454 ip_print(ndo
, ipds
->cp
, ipds
->len
);
455 if (! ndo
->ndo_vflag
) {
456 ND_PRINT(" (ipip-proto-4)");
462 /* ip6-in-ip encapsulation */
463 ip6_print(ndo
, ipds
->cp
, ipds
->len
);
467 rsvp_print(ndo
, ipds
->cp
, ipds
->len
);
472 gre_print(ndo
, ipds
->cp
, ipds
->len
);
476 mobile_print(ndo
, ipds
->cp
, ipds
->len
);
480 pim_print(ndo
, ipds
->cp
, ipds
->len
, (const u_char
*)ipds
->ip
);
484 if (ndo
->ndo_packettype
== PT_CARP
) {
486 ND_PRINT("carp %s > %s: ",
487 ipaddr_string(ndo
, ipds
->ip
->ip_src
),
488 ipaddr_string(ndo
, ipds
->ip
->ip_dst
));
489 carp_print(ndo
, ipds
->cp
, ipds
->len
,
490 EXTRACT_U_1(ipds
->ip
->ip_ttl
));
493 ND_PRINT("vrrp %s > %s: ",
494 ipaddr_string(ndo
, ipds
->ip
->ip_src
),
495 ipaddr_string(ndo
, ipds
->ip
->ip_dst
));
496 vrrp_print(ndo
, ipds
->cp
, ipds
->len
,
497 (const u_char
*)ipds
->ip
,
498 EXTRACT_U_1(ipds
->ip
->ip_ttl
));
503 pgm_print(ndo
, ipds
->cp
, ipds
->len
, (const u_char
*)ipds
->ip
);
507 if (ndo
->ndo_nflag
==0 && (p_name
= netdb_protoname(ipds
->nh
)) != NULL
)
508 ND_PRINT(" %s", p_name
);
510 ND_PRINT(" ip-proto-%u", ipds
->nh
);
511 ND_PRINT(" %u", ipds
->len
);
517 ip_print_inner(netdissect_options
*ndo
,
519 u_int length
, u_int nh
,
522 struct ip_print_demux_state ipd
;
524 ipd
.ip
= (const struct ip
*)bp2
;
531 ip_print_demux(ndo
, &ipd
);
536 * print an IP datagram.
539 ip_print(netdissect_options
*ndo
,
543 struct ip_print_demux_state ipd
;
544 struct ip_print_demux_state
*ipds
=&ipd
;
547 struct cksum_vec vec
[1];
548 uint8_t ip_tos
, ip_ttl
, ip_proto
;
549 uint16_t sum
, ip_sum
;
552 ipds
->ip
= (const struct ip
*)bp
;
553 ND_TCHECK_1(ipds
->ip
->ip_vhl
);
554 if (IP_V(ipds
->ip
) != 4) { /* print version and fail if != 4 */
555 if (IP_V(ipds
->ip
) == 6)
556 ND_PRINT("IP6, wrong link-layer encapsulation");
558 ND_PRINT("IP%u", IP_V(ipds
->ip
));
564 ND_TCHECK_SIZE(ipds
->ip
);
565 if (length
< sizeof (struct ip
)) {
566 ND_PRINT("truncated-ip %u", length
);
569 hlen
= IP_HL(ipds
->ip
) * 4;
570 if (hlen
< sizeof (struct ip
)) {
571 ND_PRINT("bad-hlen %u", hlen
);
575 ipds
->len
= EXTRACT_BE_U_2(ipds
->ip
->ip_len
);
576 if (length
< ipds
->len
)
577 ND_PRINT("truncated-ip - %u bytes missing! ",
579 if (ipds
->len
< hlen
) {
582 ND_PRINT("bad-len %u", ipds
->len
);
586 /* we guess that it is a TSO send */
590 ND_PRINT("bad-len %u", ipds
->len
);
592 #endif /* GUESS_TSO */
596 * Cut off the snapshot length to the end of the IP payload.
598 ipend
= bp
+ ipds
->len
;
599 if (ipend
< ndo
->ndo_snapend
)
600 ndo
->ndo_snapend
= ipend
;
604 ipds
->off
= EXTRACT_BE_U_2(ipds
->ip
->ip_off
);
606 ip_proto
= EXTRACT_U_1(ipds
->ip
->ip_p
);
608 if (ndo
->ndo_vflag
) {
609 ip_tos
= EXTRACT_U_1(ipds
->ip
->ip_tos
);
610 ND_PRINT("(tos 0x%x", ip_tos
);
612 switch (ip_tos
& 0x03) {
630 ip_ttl
= EXTRACT_U_1(ipds
->ip
->ip_ttl
);
632 ND_PRINT(", ttl %u", ip_ttl
);
635 * for the firewall guys, print id, offset.
636 * On all but the last stick a "+" in the flags portion.
637 * For unfragmented datagrams, note the don't fragment flag.
639 ND_PRINT(", id %u, offset %u, flags [%s], proto %s (%u)",
640 EXTRACT_BE_U_2(ipds
->ip
->ip_id
),
641 (ipds
->off
& 0x1fff) * 8,
642 bittok2str(ip_frag_values
, "none", ipds
->off
&0xe000),
643 tok2str(ipproto_values
, "unknown", ip_proto
),
646 ND_PRINT(", length %u", EXTRACT_BE_U_2(ipds
->ip
->ip_len
));
648 if ((hlen
- sizeof(struct ip
)) > 0) {
649 ND_PRINT(", options (");
650 ip_optprint(ndo
, (const u_char
*)(ipds
->ip
+ 1), hlen
- sizeof(struct ip
));
654 if (!ndo
->ndo_Kflag
&& (const u_char
*)ipds
->ip
+ hlen
<= ndo
->ndo_snapend
) {
655 vec
[0].ptr
= (const uint8_t *)(const void *)ipds
->ip
;
657 sum
= in_cksum(vec
, 1);
659 ip_sum
= EXTRACT_BE_U_2(ipds
->ip
->ip_sum
);
660 ND_PRINT(", bad cksum %x (->%x)!", ip_sum
,
661 in_cksum_shouldbe(ip_sum
, sum
));
669 * If this is fragment zero, hand it to the next higher
672 if ((ipds
->off
& 0x1fff) == 0) {
673 ipds
->cp
= (const u_char
*)ipds
->ip
+ hlen
;
674 ipds
->nh
= EXTRACT_U_1(ipds
->ip
->ip_p
);
676 if (ipds
->nh
!= IPPROTO_TCP
&& ipds
->nh
!= IPPROTO_UDP
&&
677 ipds
->nh
!= IPPROTO_SCTP
&& ipds
->nh
!= IPPROTO_DCCP
) {
678 ND_PRINT("%s > %s: ",
679 ipaddr_string(ndo
, ipds
->ip
->ip_src
),
680 ipaddr_string(ndo
, ipds
->ip
->ip_dst
));
682 ip_print_demux(ndo
, ipds
);
685 * Ultra quiet now means that all this stuff should be
688 if (ndo
->ndo_qflag
> 1)
692 * This isn't the first frag, so we're missing the
693 * next level protocol header. print the ip addr
696 ND_PRINT("%s > %s:", ipaddr_string(ndo
, ipds
->ip
->ip_src
),
697 ipaddr_string(ndo
, ipds
->ip
->ip_dst
));
698 if (!ndo
->ndo_nflag
&& (p_name
= netdb_protoname(ip_proto
)) != NULL
)
699 ND_PRINT(" %s", p_name
);
701 ND_PRINT(" ip-proto-%u", ip_proto
);
706 ND_PRINT("%s", tstr
);
711 ipN_print(netdissect_options
*ndo
, const u_char
*bp
, u_int length
)
714 ND_PRINT("truncated-ip %u", length
);
719 switch (EXTRACT_U_1(bp
) & 0xF0) {
721 ip_print (ndo
, bp
, length
);
724 ip6_print (ndo
, bp
, length
);
727 ND_PRINT("unknown ip %u", (EXTRACT_U_1(bp
) & 0xF0) >> 4);
733 ND_PRINT("%s", tstr
);
739 * c-style: whitesmith