]>
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 <netdissect-stdinc.h>
30 #include "netdissect.h"
31 #include "addrtoname.h"
37 static const char tstr
[] = "[|ip]";
39 static const struct tok ip_option_values
[] = {
42 { IPOPT_TS
, "timestamp" },
43 { IPOPT_SECURITY
, "security" },
45 { IPOPT_SSRR
, "SSRR" },
46 { IPOPT_LSRR
, "LSRR" },
48 { IPOPT_RFC1393
, "traceroute" },
53 * print the recorded route in an IP RR, LSRR or SSRR option.
56 ip_printroute(netdissect_options
*ndo
,
57 register const u_char
*cp
, u_int length
)
63 ND_PRINT((ndo
, " [bad length %u]", length
));
67 ND_PRINT((ndo
, " [bad length %u]", length
));
69 if (ptr
< 3 || ((ptr
+ 1) & 3) || ptr
> length
+ 1)
70 ND_PRINT((ndo
, " [bad ptr %u]", cp
[2]));
72 for (len
= 3; len
< length
; len
+= 4) {
73 ND_PRINT((ndo
, " %s", ipaddr_string(ndo
, &cp
[len
])));
80 * If source-routing is present and valid, return the final destination.
81 * Otherwise, return IP destination.
83 * This is used for UDP and TCP pseudo-header in the checksum
87 ip_finddst(netdissect_options
*ndo
,
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
)
113 ND_TCHECK2(*cp
, len
);
120 UNALIGNED_MEMCPY(&retval
, cp
+ len
- 4, 4);
125 UNALIGNED_MEMCPY(&retval
, &ip
->ip_dst
, sizeof(uint32_t));
130 * Compute a V4-style checksum by building a pseudoheader.
133 nextproto4_cksum(netdissect_options
*ndo
,
134 const struct ip
*ip
, const uint8_t *data
,
135 u_int len
, u_int covlen
, u_int next_proto
)
144 struct cksum_vec vec
[2];
146 /* pseudo-header.. */
147 ph
.len
= htons((uint16_t)len
);
149 ph
.proto
= next_proto
;
150 UNALIGNED_MEMCPY(&ph
.src
, &ip
->ip_src
, sizeof(uint32_t));
152 UNALIGNED_MEMCPY(&ph
.dst
, &ip
->ip_dst
, sizeof(uint32_t));
154 ph
.dst
= ip_finddst(ndo
, ip
);
156 vec
[0].ptr
= (const uint8_t *)(void *)&ph
;
157 vec
[0].len
= sizeof(ph
);
160 return (in_cksum(vec
, 2));
164 ip_printts(netdissect_options
*ndo
,
165 register const u_char
*cp
, u_int length
)
173 ND_PRINT((ndo
, "[bad length %u]", length
));
176 ND_PRINT((ndo
, " TS{"));
177 hoplen
= ((cp
[3]&0xF) != IPOPT_TS_TSONLY
) ? 8 : 4;
178 if ((length
- 4) & (hoplen
-1))
179 ND_PRINT((ndo
, "[bad length %u]", length
));
182 if (ptr
< 4 || ((ptr
- 4) & (hoplen
-1)) || ptr
> length
+ 1)
183 ND_PRINT((ndo
, "[bad ptr %u]", cp
[2]));
185 case IPOPT_TS_TSONLY
:
186 ND_PRINT((ndo
, "TSONLY"));
188 case IPOPT_TS_TSANDADDR
:
189 ND_PRINT((ndo
, "TS+ADDR"));
192 * prespecified should really be 3, but some ones might send 2
193 * instead, and the IPOPT_TS_PRESPEC constant can apparently
194 * have both values, so we have to hard-code it here.
198 ND_PRINT((ndo
, "PRESPEC2.0"));
200 case 3: /* IPOPT_TS_PRESPEC */
201 ND_PRINT((ndo
, "PRESPEC"));
204 ND_PRINT((ndo
, "[bad ts type %d]", cp
[3]&0xF));
209 for (len
= 4; len
< length
; len
+= hoplen
) {
212 ND_PRINT((ndo
, "%s%d@%s", type
, EXTRACT_32BITS(&cp
[len
+hoplen
-4]),
213 hoplen
!=8 ? "" : ipaddr_string(ndo
, &cp
[len
])));
218 ND_PRINT((ndo
, "%s", ptr
== len
? " ^ " : ""));
221 ND_PRINT((ndo
, " [%d hops not recorded]} ", cp
[3]>>4));
223 ND_PRINT((ndo
, "}"));
230 ip_optprint(netdissect_options
*ndo
,
231 register const u_char
*cp
, u_int length
)
233 register u_int option_len
;
234 const char *sep
= "";
236 for (; length
> 0; cp
+= option_len
, length
-= option_len
) {
239 ND_PRINT((ndo
, "%s", sep
));
246 tok2str(ip_option_values
,"unknown %u",option_code
)));
248 if (option_code
== IPOPT_NOP
||
249 option_code
== IPOPT_EOL
)
255 if (option_len
< 2) {
256 ND_PRINT((ndo
, " [bad length %u]", option_len
));
261 if (option_len
> length
) {
262 ND_PRINT((ndo
, " [bad length %u]", option_len
));
266 ND_TCHECK2(*cp
, option_len
);
268 switch (option_code
) {
273 ip_printts(ndo
, cp
, option_len
);
276 case IPOPT_RR
: /* fall through */
279 ip_printroute(ndo
, cp
, option_len
);
283 if (option_len
< 4) {
284 ND_PRINT((ndo
, " [bad length %u]", option_len
));
288 if (EXTRACT_16BITS(&cp
[2]) != 0)
289 ND_PRINT((ndo
, " value %u", EXTRACT_16BITS(&cp
[2])));
292 case IPOPT_NOP
: /* nothing to print - fall through */
301 ND_PRINT((ndo
, "%s", tstr
));
304 #define IP_RES 0x8000
306 static const struct tok ip_frag_values
[] = {
309 { IP_RES
, "rsvd" }, /* The RFC3514 evil ;-) bit */
313 struct ip_print_demux_state
{
322 ip_print_demux(netdissect_options
*ndo
,
323 struct ip_print_demux_state
*ipds
)
325 struct protoent
*proto
;
331 ipds
->nh
= *ipds
->cp
;
332 ipds
->advance
= ah_print(ndo
, ipds
->cp
);
333 if (ipds
->advance
<= 0)
335 ipds
->cp
+= ipds
->advance
;
336 ipds
->len
-= ipds
->advance
;
342 ipds
->advance
= esp_print(ndo
, ipds
->cp
, ipds
->len
,
343 (const u_char
*)ipds
->ip
,
345 if (ipds
->advance
<= 0)
347 ipds
->cp
+= ipds
->advance
;
348 ipds
->len
-= ipds
->advance
+ padlen
;
349 ipds
->nh
= enh
& 0xff;
356 ipds
->advance
= ipcomp_print(ndo
, ipds
->cp
, &enh
);
357 if (ipds
->advance
<= 0)
359 ipds
->cp
+= ipds
->advance
;
360 ipds
->len
-= ipds
->advance
;
361 ipds
->nh
= enh
& 0xff;
366 sctp_print(ndo
, ipds
->cp
, (const u_char
*)ipds
->ip
, ipds
->len
);
370 dccp_print(ndo
, ipds
->cp
, (const u_char
*)ipds
->ip
, ipds
->len
);
374 /* pass on the MF bit plus the offset to detect fragments */
375 tcp_print(ndo
, ipds
->cp
, ipds
->len
, (const u_char
*)ipds
->ip
,
376 ipds
->off
& (IP_MF
|IP_OFFMASK
));
380 /* pass on the MF bit plus the offset to detect fragments */
381 udp_print(ndo
, ipds
->cp
, ipds
->len
, (const u_char
*)ipds
->ip
,
382 ipds
->off
& (IP_MF
|IP_OFFMASK
));
386 /* pass on the MF bit plus the offset to detect fragments */
387 icmp_print(ndo
, ipds
->cp
, ipds
->len
, (const u_char
*)ipds
->ip
,
388 ipds
->off
& (IP_MF
|IP_OFFMASK
));
393 * XXX - the current IANA protocol number assignments
394 * page lists 9 as "any private interior gateway
395 * (used by Cisco for their IGRP)" and 88 as
396 * "EIGRP" from Cisco.
398 * Recent BSD <netinet/in.h> headers define
399 * IP_PROTO_PIGP as 9 and IP_PROTO_IGRP as 88.
400 * We define IP_PROTO_PIGP as 9 and
401 * IP_PROTO_EIGRP as 88; those names better
402 * match was the current protocol number
405 igrp_print(ndo
, ipds
->cp
, ipds
->len
);
409 eigrp_print(ndo
, ipds
->cp
, ipds
->len
);
413 ND_PRINT((ndo
, " nd %d", ipds
->len
));
417 egp_print(ndo
, ipds
->cp
, ipds
->len
);
421 ospf_print(ndo
, ipds
->cp
, ipds
->len
, (const u_char
*)ipds
->ip
);
425 igmp_print(ndo
, ipds
->cp
, ipds
->len
);
429 /* DVMRP multicast tunnel (ip-in-ip encapsulation) */
430 ip_print(ndo
, ipds
->cp
, ipds
->len
);
431 if (! ndo
->ndo_vflag
) {
432 ND_PRINT((ndo
, " (ipip-proto-4)"));
438 /* ip6-in-ip encapsulation */
439 ip6_print(ndo
, ipds
->cp
, ipds
->len
);
443 rsvp_print(ndo
, ipds
->cp
, ipds
->len
);
448 gre_print(ndo
, ipds
->cp
, ipds
->len
);
452 mobile_print(ndo
, ipds
->cp
, ipds
->len
);
456 pim_print(ndo
, ipds
->cp
, ipds
->len
, (const u_char
*)ipds
->ip
);
460 if (ndo
->ndo_packettype
== PT_CARP
) {
462 ND_PRINT((ndo
, "carp %s > %s: ",
463 ipaddr_string(ndo
, &ipds
->ip
->ip_src
),
464 ipaddr_string(ndo
, &ipds
->ip
->ip_dst
)));
465 carp_print(ndo
, ipds
->cp
, ipds
->len
, ipds
->ip
->ip_ttl
);
468 ND_PRINT((ndo
, "vrrp %s > %s: ",
469 ipaddr_string(ndo
, &ipds
->ip
->ip_src
),
470 ipaddr_string(ndo
, &ipds
->ip
->ip_dst
)));
471 vrrp_print(ndo
, ipds
->cp
, ipds
->len
,
472 (const u_char
*)ipds
->ip
, ipds
->ip
->ip_ttl
);
477 pgm_print(ndo
, ipds
->cp
, ipds
->len
, (const u_char
*)ipds
->ip
);
481 if (ndo
->ndo_nflag
==0 && (proto
= getprotobynumber(ipds
->nh
)) != NULL
)
482 ND_PRINT((ndo
, " %s", proto
->p_name
));
484 ND_PRINT((ndo
, " ip-proto-%d", ipds
->nh
));
485 ND_PRINT((ndo
, " %d", ipds
->len
));
491 ip_print_inner(netdissect_options
*ndo
,
493 u_int length
, u_int nh
,
496 struct ip_print_demux_state ipd
;
498 ipd
.ip
= (const struct ip
*)bp2
;
505 ip_print_demux(ndo
, &ipd
);
510 * print an IP datagram.
513 ip_print(netdissect_options
*ndo
,
517 struct ip_print_demux_state ipd
;
518 struct ip_print_demux_state
*ipds
=&ipd
;
521 struct cksum_vec vec
[1];
522 uint16_t sum
, ip_sum
;
523 struct protoent
*proto
;
525 ipds
->ip
= (const struct ip
*)bp
;
526 ND_TCHECK(ipds
->ip
->ip_vhl
);
527 if (IP_V(ipds
->ip
) != 4) { /* print version if != 4 */
528 if (IP_V(ipds
->ip
) == 6)
529 ND_PRINT((ndo
, "IP6, wrong link-layer encapsulation "));
531 ND_PRINT((ndo
, "IP%u ", IP_V(ipds
->ip
)));
533 else if (!ndo
->ndo_eflag
)
534 ND_PRINT((ndo
, "IP "));
536 ND_TCHECK(*ipds
->ip
);
537 if (length
< sizeof (struct ip
)) {
538 ND_PRINT((ndo
, "truncated-ip %u", length
));
541 hlen
= IP_HL(ipds
->ip
) * 4;
542 if (hlen
< sizeof (struct ip
)) {
543 ND_PRINT((ndo
, "bad-hlen %u", hlen
));
547 ipds
->len
= EXTRACT_16BITS(&ipds
->ip
->ip_len
);
548 if (length
< ipds
->len
)
549 ND_PRINT((ndo
, "truncated-ip - %u bytes missing! ",
550 ipds
->len
- length
));
551 if (ipds
->len
< hlen
) {
554 ND_PRINT((ndo
, "bad-len %u", ipds
->len
));
558 /* we guess that it is a TSO send */
562 ND_PRINT((ndo
, "bad-len %u", ipds
->len
));
564 #endif /* GUESS_TSO */
568 * Cut off the snapshot length to the end of the IP payload.
570 ipend
= bp
+ ipds
->len
;
571 if (ipend
< ndo
->ndo_snapend
)
572 ndo
->ndo_snapend
= ipend
;
576 ipds
->off
= EXTRACT_16BITS(&ipds
->ip
->ip_off
);
578 if (ndo
->ndo_vflag
) {
579 ND_PRINT((ndo
, "(tos 0x%x", (int)ipds
->ip
->ip_tos
));
581 if (ipds
->ip
->ip_tos
& 0x03) {
582 switch (ipds
->ip
->ip_tos
& 0x03) {
584 ND_PRINT((ndo
, ",ECT(1)"));
587 ND_PRINT((ndo
, ",ECT(0)"));
590 ND_PRINT((ndo
, ",CE"));
594 if (ipds
->ip
->ip_ttl
>= 1)
595 ND_PRINT((ndo
, ", ttl %u", ipds
->ip
->ip_ttl
));
598 * for the firewall guys, print id, offset.
599 * On all but the last stick a "+" in the flags portion.
600 * For unfragmented datagrams, note the don't fragment flag.
603 ND_PRINT((ndo
, ", id %u, offset %u, flags [%s], proto %s (%u)",
604 EXTRACT_16BITS(&ipds
->ip
->ip_id
),
605 (ipds
->off
& 0x1fff) * 8,
606 bittok2str(ip_frag_values
, "none", ipds
->off
&0xe000),
607 tok2str(ipproto_values
,"unknown",ipds
->ip
->ip_p
),
610 ND_PRINT((ndo
, ", length %u", EXTRACT_16BITS(&ipds
->ip
->ip_len
)));
612 if ((hlen
- sizeof(struct ip
)) > 0) {
613 ND_PRINT((ndo
, ", options ("));
614 ip_optprint(ndo
, (const u_char
*)(ipds
->ip
+ 1), hlen
- sizeof(struct ip
));
615 ND_PRINT((ndo
, ")"));
618 if (!ndo
->ndo_Kflag
&& (const u_char
*)ipds
->ip
+ hlen
<= ndo
->ndo_snapend
) {
619 vec
[0].ptr
= (const uint8_t *)(const void *)ipds
->ip
;
621 sum
= in_cksum(vec
, 1);
623 ip_sum
= EXTRACT_16BITS(&ipds
->ip
->ip_sum
);
624 ND_PRINT((ndo
, ", bad cksum %x (->%x)!", ip_sum
,
625 in_cksum_shouldbe(ip_sum
, sum
)));
629 ND_PRINT((ndo
, ")\n "));
633 * If this is fragment zero, hand it to the next higher
636 if ((ipds
->off
& 0x1fff) == 0) {
637 ipds
->cp
= (const u_char
*)ipds
->ip
+ hlen
;
638 ipds
->nh
= ipds
->ip
->ip_p
;
640 if (ipds
->nh
!= IPPROTO_TCP
&& ipds
->nh
!= IPPROTO_UDP
&&
641 ipds
->nh
!= IPPROTO_SCTP
&& ipds
->nh
!= IPPROTO_DCCP
) {
642 ND_PRINT((ndo
, "%s > %s: ",
643 ipaddr_string(ndo
, &ipds
->ip
->ip_src
),
644 ipaddr_string(ndo
, &ipds
->ip
->ip_dst
)));
646 ip_print_demux(ndo
, ipds
);
648 /* Ultra quiet now means that all this stuff should be suppressed */
649 if (ndo
->ndo_qflag
> 1) return;
652 * if this isn't the first frag, we're missing the
653 * next level protocol header. print the ip addr
656 if (ipds
->off
& 0x1fff) {
657 ND_PRINT((ndo
, "%s > %s:", ipaddr_string(ndo
, &ipds
->ip
->ip_src
),
658 ipaddr_string(ndo
, &ipds
->ip
->ip_dst
)));
659 if (!ndo
->ndo_nflag
&& (proto
= getprotobynumber(ipds
->ip
->ip_p
)) != NULL
)
660 ND_PRINT((ndo
, " %s", proto
->p_name
));
662 ND_PRINT((ndo
, " ip-proto-%d", ipds
->ip
->ip_p
));
668 ND_PRINT((ndo
, "%s", tstr
));
673 ipN_print(netdissect_options
*ndo
, register const u_char
*bp
, register u_int length
)
678 ND_PRINT((ndo
, "truncated-ip %d", length
));
681 memcpy (&hdr
, bp
, 4);
682 switch (IP_V(&hdr
)) {
684 ip_print (ndo
, bp
, length
);
687 ip6_print (ndo
, bp
, length
);
690 ND_PRINT((ndo
, "unknown ip %d", IP_V(&hdr
)));
697 * c-style: whitesmith