]>
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 register const u_char
*cp
, u_int length
)
65 ND_PRINT((ndo
, " [bad length %u]", length
));
69 ND_PRINT((ndo
, " [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((ndo
, " [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((ndo
, " %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
,
103 cp
= (const u_char
*)(ip
+ 1);
104 length
= (IP_HL(ip
) << 2) - sizeof(struct ip
);
106 for (; length
> 0; cp
+= len
, length
-= len
) {
110 tt
= EXTRACT_U_1(cp
);
113 else if (tt
== IPOPT_NOP
)
117 len
= EXTRACT_U_1(cp
+ 1);
121 ND_TCHECK2(*cp
, len
);
128 UNALIGNED_MEMCPY(&retval
, cp
+ len
- 4, 4);
133 UNALIGNED_MEMCPY(&retval
, &ip
->ip_dst
, sizeof(uint32_t));
138 * Compute a V4-style checksum by building a pseudoheader.
141 nextproto4_cksum(netdissect_options
*ndo
,
142 const struct ip
*ip
, const uint8_t *data
,
143 u_int len
, u_int covlen
, u_int next_proto
)
152 struct cksum_vec vec
[2];
154 /* pseudo-header.. */
155 ph
.len
= htons((uint16_t)len
);
157 ph
.proto
= next_proto
;
158 UNALIGNED_MEMCPY(&ph
.src
, &ip
->ip_src
, sizeof(uint32_t));
160 UNALIGNED_MEMCPY(&ph
.dst
, &ip
->ip_dst
, sizeof(uint32_t));
162 ph
.dst
= ip_finddst(ndo
, ip
);
164 vec
[0].ptr
= (const uint8_t *)(void *)&ph
;
165 vec
[0].len
= sizeof(ph
);
168 return (in_cksum(vec
, 2));
172 ip_printts(netdissect_options
*ndo
,
173 register const u_char
*cp
, u_int length
)
181 ND_PRINT((ndo
, "[bad length %u]", length
));
184 ND_PRINT((ndo
, " TS{"));
186 hoplen
= ((EXTRACT_U_1(cp
+ 3) & 0xF) != IPOPT_TS_TSONLY
) ? 8 : 4;
187 if ((length
- 4) & (hoplen
-1))
188 ND_PRINT((ndo
, "[bad length %u]", length
));
190 ptr
= EXTRACT_U_1(cp
+ 2) - 1;
192 if (ptr
< 4 || ((ptr
- 4) & (hoplen
-1)) || ptr
> length
+ 1)
193 ND_PRINT((ndo
, "[bad ptr %u]", EXTRACT_U_1(cp
+ 2)));
195 switch (EXTRACT_U_1(cp
+ 3)&0xF) {
196 case IPOPT_TS_TSONLY
:
197 ND_PRINT((ndo
, "TSONLY"));
199 case IPOPT_TS_TSANDADDR
:
200 ND_PRINT((ndo
, "TS+ADDR"));
203 * prespecified should really be 3, but some ones might send 2
204 * instead, and the IPOPT_TS_PRESPEC constant can apparently
205 * have both values, so we have to hard-code it here.
209 ND_PRINT((ndo
, "PRESPEC2.0"));
211 case 3: /* IPOPT_TS_PRESPEC */
212 ND_PRINT((ndo
, "PRESPEC"));
215 ND_PRINT((ndo
, "[bad ts type %d]", EXTRACT_U_1(cp
+ 3)&0xF));
220 for (len
= 4; len
< length
; len
+= hoplen
) {
223 ND_TCHECK2(cp
[len
], hoplen
);
224 ND_PRINT((ndo
, "%s%d@%s", type
, EXTRACT_BE_U_4(cp
+ len
+ hoplen
- 4),
225 hoplen
!=8 ? "" : ipaddr_string(ndo
, cp
+ len
)));
230 ND_PRINT((ndo
, "%s", ptr
== len
? " ^ " : ""));
232 if (EXTRACT_U_1(cp
+ 3) >> 4)
233 ND_PRINT((ndo
, " [%d hops not recorded]} ", EXTRACT_U_1(cp
+ 3)>>4));
235 ND_PRINT((ndo
, "}"));
246 ip_optprint(netdissect_options
*ndo
,
247 register const u_char
*cp
, u_int length
)
249 register u_int option_len
;
250 const char *sep
= "";
252 for (; length
> 0; cp
+= option_len
, length
-= option_len
) {
255 ND_PRINT((ndo
, "%s", sep
));
259 option_code
= EXTRACT_U_1(cp
);
262 tok2str(ip_option_values
,"unknown %u",option_code
)));
264 if (option_code
== IPOPT_NOP
||
265 option_code
== IPOPT_EOL
)
270 option_len
= EXTRACT_U_1(cp
+ 1);
271 if (option_len
< 2) {
272 ND_PRINT((ndo
, " [bad length %u]", option_len
));
277 if (option_len
> length
) {
278 ND_PRINT((ndo
, " [bad length %u]", option_len
));
282 ND_TCHECK2(*cp
, option_len
);
284 switch (option_code
) {
289 if (ip_printts(ndo
, cp
, option_len
) == -1)
293 case IPOPT_RR
: /* fall through */
296 if (ip_printroute(ndo
, cp
, option_len
) == -1)
301 if (option_len
< 4) {
302 ND_PRINT((ndo
, " [bad length %u]", option_len
));
306 if (EXTRACT_BE_U_2(cp
+ 2) != 0)
307 ND_PRINT((ndo
, " value %u", EXTRACT_BE_U_2(cp
+ 2)));
310 case IPOPT_NOP
: /* nothing to print - fall through */
319 ND_PRINT((ndo
, "%s", tstr
));
322 #define IP_RES 0x8000
324 static const struct tok ip_frag_values
[] = {
327 { IP_RES
, "rsvd" }, /* The RFC3514 evil ;-) bit */
331 struct ip_print_demux_state
{
340 ip_print_demux(netdissect_options
*ndo
,
341 struct ip_print_demux_state
*ipds
)
349 if (!ND_TTEST(*ipds
->cp
)) {
350 ND_PRINT((ndo
, "[|AH]"));
353 ipds
->nh
= EXTRACT_U_1(ipds
->cp
);
354 ipds
->advance
= ah_print(ndo
, ipds
->cp
);
355 if (ipds
->advance
<= 0)
357 ipds
->cp
+= ipds
->advance
;
358 ipds
->len
-= ipds
->advance
;
364 ipds
->advance
= esp_print(ndo
, ipds
->cp
, ipds
->len
,
365 (const u_char
*)ipds
->ip
,
367 if (ipds
->advance
<= 0)
369 ipds
->cp
+= ipds
->advance
;
370 ipds
->len
-= ipds
->advance
+ padlen
;
371 ipds
->nh
= enh
& 0xff;
377 ipcomp_print(ndo
, ipds
->cp
);
379 * Either this has decompressed the payload and
380 * printed it, in which case there's nothing more
381 * to do, or it hasn't, in which case there's
382 * nothing more to do.
388 sctp_print(ndo
, ipds
->cp
, (const u_char
*)ipds
->ip
, ipds
->len
);
392 dccp_print(ndo
, ipds
->cp
, (const u_char
*)ipds
->ip
, ipds
->len
);
396 /* pass on the MF bit plus the offset to detect fragments */
397 tcp_print(ndo
, ipds
->cp
, ipds
->len
, (const u_char
*)ipds
->ip
,
398 ipds
->off
& (IP_MF
|IP_OFFMASK
));
402 /* pass on the MF bit plus the offset to detect fragments */
403 udp_print(ndo
, ipds
->cp
, ipds
->len
, (const u_char
*)ipds
->ip
,
404 ipds
->off
& (IP_MF
|IP_OFFMASK
));
408 /* pass on the MF bit plus the offset to detect fragments */
409 icmp_print(ndo
, ipds
->cp
, ipds
->len
, (const u_char
*)ipds
->ip
,
410 ipds
->off
& (IP_MF
|IP_OFFMASK
));
415 * XXX - the current IANA protocol number assignments
416 * page lists 9 as "any private interior gateway
417 * (used by Cisco for their IGRP)" and 88 as
418 * "EIGRP" from Cisco.
420 * Recent BSD <netinet/in.h> headers define
421 * IP_PROTO_PIGP as 9 and IP_PROTO_IGRP as 88.
422 * We define IP_PROTO_PIGP as 9 and
423 * IP_PROTO_EIGRP as 88; those names better
424 * match was the current protocol number
427 igrp_print(ndo
, ipds
->cp
, ipds
->len
);
431 eigrp_print(ndo
, ipds
->cp
, ipds
->len
);
435 ND_PRINT((ndo
, " nd %d", ipds
->len
));
439 egp_print(ndo
, ipds
->cp
, ipds
->len
);
443 ospf_print(ndo
, ipds
->cp
, ipds
->len
, (const u_char
*)ipds
->ip
);
447 igmp_print(ndo
, ipds
->cp
, ipds
->len
);
451 /* DVMRP multicast tunnel (ip-in-ip encapsulation) */
452 ip_print(ndo
, ipds
->cp
, ipds
->len
);
453 if (! ndo
->ndo_vflag
) {
454 ND_PRINT((ndo
, " (ipip-proto-4)"));
460 /* ip6-in-ip encapsulation */
461 ip6_print(ndo
, ipds
->cp
, ipds
->len
);
465 rsvp_print(ndo
, ipds
->cp
, ipds
->len
);
470 gre_print(ndo
, ipds
->cp
, ipds
->len
);
474 mobile_print(ndo
, ipds
->cp
, ipds
->len
);
478 pim_print(ndo
, ipds
->cp
, ipds
->len
, (const u_char
*)ipds
->ip
);
482 if (ndo
->ndo_packettype
== PT_CARP
) {
484 ND_PRINT((ndo
, "carp %s > %s: ",
485 ipaddr_string(ndo
, &ipds
->ip
->ip_src
),
486 ipaddr_string(ndo
, &ipds
->ip
->ip_dst
)));
487 carp_print(ndo
, ipds
->cp
, ipds
->len
,
488 EXTRACT_U_1(ipds
->ip
->ip_ttl
));
491 ND_PRINT((ndo
, "vrrp %s > %s: ",
492 ipaddr_string(ndo
, &ipds
->ip
->ip_src
),
493 ipaddr_string(ndo
, &ipds
->ip
->ip_dst
)));
494 vrrp_print(ndo
, ipds
->cp
, ipds
->len
,
495 (const u_char
*)ipds
->ip
,
496 EXTRACT_U_1(ipds
->ip
->ip_ttl
));
501 pgm_print(ndo
, ipds
->cp
, ipds
->len
, (const u_char
*)ipds
->ip
);
505 if (ndo
->ndo_nflag
==0 && (p_name
= netdb_protoname(ipds
->nh
)) != NULL
)
506 ND_PRINT((ndo
, " %s", p_name
));
508 ND_PRINT((ndo
, " ip-proto-%d", ipds
->nh
));
509 ND_PRINT((ndo
, " %d", ipds
->len
));
515 ip_print_inner(netdissect_options
*ndo
,
517 u_int length
, u_int nh
,
520 struct ip_print_demux_state ipd
;
522 ipd
.ip
= (const struct ip
*)bp2
;
529 ip_print_demux(ndo
, &ipd
);
534 * print an IP datagram.
537 ip_print(netdissect_options
*ndo
,
541 struct ip_print_demux_state ipd
;
542 struct ip_print_demux_state
*ipds
=&ipd
;
545 struct cksum_vec vec
[1];
546 uint8_t ip_tos
, ip_ttl
, ip_proto
;
547 uint16_t sum
, ip_sum
;
550 ipds
->ip
= (const struct ip
*)bp
;
551 ND_TCHECK(ipds
->ip
->ip_vhl
);
552 if (IP_V(ipds
->ip
) != 4) { /* print version and fail if != 4 */
553 if (IP_V(ipds
->ip
) == 6)
554 ND_PRINT((ndo
, "IP6, wrong link-layer encapsulation "));
556 ND_PRINT((ndo
, "IP%u ", IP_V(ipds
->ip
)));
560 ND_PRINT((ndo
, "IP "));
562 ND_TCHECK(*ipds
->ip
);
563 if (length
< sizeof (struct ip
)) {
564 ND_PRINT((ndo
, "truncated-ip %u", length
));
567 hlen
= IP_HL(ipds
->ip
) * 4;
568 if (hlen
< sizeof (struct ip
)) {
569 ND_PRINT((ndo
, "bad-hlen %u", hlen
));
573 ipds
->len
= EXTRACT_BE_U_2(&ipds
->ip
->ip_len
);
574 if (length
< ipds
->len
)
575 ND_PRINT((ndo
, "truncated-ip - %u bytes missing! ",
576 ipds
->len
- length
));
577 if (ipds
->len
< hlen
) {
580 ND_PRINT((ndo
, "bad-len %u", ipds
->len
));
584 /* we guess that it is a TSO send */
588 ND_PRINT((ndo
, "bad-len %u", ipds
->len
));
590 #endif /* GUESS_TSO */
594 * Cut off the snapshot length to the end of the IP payload.
596 ipend
= bp
+ ipds
->len
;
597 if (ipend
< ndo
->ndo_snapend
)
598 ndo
->ndo_snapend
= ipend
;
602 ipds
->off
= EXTRACT_BE_U_2(&ipds
->ip
->ip_off
);
604 ip_proto
= EXTRACT_U_1(ipds
->ip
->ip_p
);
606 if (ndo
->ndo_vflag
) {
607 ip_tos
= EXTRACT_U_1(ipds
->ip
->ip_tos
);
608 ND_PRINT((ndo
, "(tos 0x%x", ip_tos
));
610 switch (ip_tos
& 0x03) {
616 ND_PRINT((ndo
, ",ECT(1)"));
620 ND_PRINT((ndo
, ",ECT(0)"));
624 ND_PRINT((ndo
, ",CE"));
628 ip_ttl
= EXTRACT_U_1(ipds
->ip
->ip_ttl
);
630 ND_PRINT((ndo
, ", ttl %u", ip_ttl
));
633 * for the firewall guys, print id, offset.
634 * On all but the last stick a "+" in the flags portion.
635 * For unfragmented datagrams, note the don't fragment flag.
637 ND_PRINT((ndo
, ", id %u, offset %u, flags [%s], proto %s (%u)",
638 EXTRACT_BE_U_2(&ipds
->ip
->ip_id
),
639 (ipds
->off
& 0x1fff) * 8,
640 bittok2str(ip_frag_values
, "none", ipds
->off
&0xe000),
641 tok2str(ipproto_values
, "unknown", ip_proto
),
644 ND_PRINT((ndo
, ", length %u", EXTRACT_BE_U_2(ipds
->ip
->ip_len
)));
646 if ((hlen
- sizeof(struct ip
)) > 0) {
647 ND_PRINT((ndo
, ", options ("));
648 ip_optprint(ndo
, (const u_char
*)(ipds
->ip
+ 1), hlen
- sizeof(struct ip
));
649 ND_PRINT((ndo
, ")"));
652 if (!ndo
->ndo_Kflag
&& (const u_char
*)ipds
->ip
+ hlen
<= ndo
->ndo_snapend
) {
653 vec
[0].ptr
= (const uint8_t *)(const void *)ipds
->ip
;
655 sum
= in_cksum(vec
, 1);
657 ip_sum
= EXTRACT_BE_U_2(ipds
->ip
->ip_sum
);
658 ND_PRINT((ndo
, ", bad cksum %x (->%x)!", ip_sum
,
659 in_cksum_shouldbe(ip_sum
, sum
)));
663 ND_PRINT((ndo
, ")\n "));
667 * If this is fragment zero, hand it to the next higher
670 if ((ipds
->off
& 0x1fff) == 0) {
671 ipds
->cp
= (const u_char
*)ipds
->ip
+ hlen
;
672 ipds
->nh
= EXTRACT_U_1(ipds
->ip
->ip_p
);
674 if (ipds
->nh
!= IPPROTO_TCP
&& ipds
->nh
!= IPPROTO_UDP
&&
675 ipds
->nh
!= IPPROTO_SCTP
&& ipds
->nh
!= IPPROTO_DCCP
) {
676 ND_PRINT((ndo
, "%s > %s: ",
677 ipaddr_string(ndo
, &ipds
->ip
->ip_src
),
678 ipaddr_string(ndo
, &ipds
->ip
->ip_dst
)));
680 ip_print_demux(ndo
, ipds
);
683 * Ultra quiet now means that all this stuff should be
686 if (ndo
->ndo_qflag
> 1)
690 * This isn't the first frag, so we're missing the
691 * next level protocol header. print the ip addr
694 ND_PRINT((ndo
, "%s > %s:", ipaddr_string(ndo
, &ipds
->ip
->ip_src
),
695 ipaddr_string(ndo
, &ipds
->ip
->ip_dst
)));
696 if (!ndo
->ndo_nflag
&& (p_name
= netdb_protoname(ip_proto
)) != NULL
)
697 ND_PRINT((ndo
, " %s", p_name
));
699 ND_PRINT((ndo
, " ip-proto-%u", ip_proto
));
704 ND_PRINT((ndo
, "%s", tstr
));
709 ipN_print(netdissect_options
*ndo
, register const u_char
*bp
, register u_int length
)
712 ND_PRINT((ndo
, "truncated-ip %d", length
));
717 switch (EXTRACT_U_1(bp
) & 0xF0) {
719 ip_print (ndo
, bp
, length
);
722 ip6_print (ndo
, bp
, length
);
725 ND_PRINT((ndo
, "unknown ip %u", (EXTRACT_U_1(bp
) & 0xF0) >> 4));
731 ND_PRINT((ndo
, "%s", tstr
));
737 * c-style: whitesmith