]>
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
));
72 if (ptr
< 3 || ((ptr
+ 1) & 3) || ptr
> length
+ 1)
73 ND_PRINT((ndo
, " [bad ptr %u]", cp
[2]));
75 for (len
= 3; len
< length
; len
+= 4) {
76 ND_TCHECK2(cp
[len
], 4);
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
) {
113 else if (tt
== IPOPT_NOP
)
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{"));
185 hoplen
= ((cp
[3]&0xF) != IPOPT_TS_TSONLY
) ? 8 : 4;
186 if ((length
- 4) & (hoplen
-1))
187 ND_PRINT((ndo
, "[bad length %u]", length
));
191 if (ptr
< 4 || ((ptr
- 4) & (hoplen
-1)) || ptr
> length
+ 1)
192 ND_PRINT((ndo
, "[bad ptr %u]", cp
[2]));
195 case IPOPT_TS_TSONLY
:
196 ND_PRINT((ndo
, "TSONLY"));
198 case IPOPT_TS_TSANDADDR
:
199 ND_PRINT((ndo
, "TS+ADDR"));
202 * prespecified should really be 3, but some ones might send 2
203 * instead, and the IPOPT_TS_PRESPEC constant can apparently
204 * have both values, so we have to hard-code it here.
208 ND_PRINT((ndo
, "PRESPEC2.0"));
210 case 3: /* IPOPT_TS_PRESPEC */
211 ND_PRINT((ndo
, "PRESPEC"));
214 ND_PRINT((ndo
, "[bad ts type %d]", cp
[3]&0xF));
219 for (len
= 4; len
< length
; len
+= hoplen
) {
222 ND_TCHECK2(cp
[len
], hoplen
);
223 ND_PRINT((ndo
, "%s%d@%s", type
, EXTRACT_32BITS(&cp
[len
+hoplen
-4]),
224 hoplen
!=8 ? "" : ipaddr_string(ndo
, &cp
[len
])));
229 ND_PRINT((ndo
, "%s", ptr
== len
? " ^ " : ""));
232 ND_PRINT((ndo
, " [%d hops not recorded]} ", cp
[3]>>4));
234 ND_PRINT((ndo
, "}"));
245 ip_optprint(netdissect_options
*ndo
,
246 register const u_char
*cp
, u_int length
)
248 register u_int option_len
;
249 const char *sep
= "";
251 for (; length
> 0; cp
+= option_len
, length
-= option_len
) {
254 ND_PRINT((ndo
, "%s", sep
));
261 tok2str(ip_option_values
,"unknown %u",option_code
)));
263 if (option_code
== IPOPT_NOP
||
264 option_code
== IPOPT_EOL
)
270 if (option_len
< 2) {
271 ND_PRINT((ndo
, " [bad length %u]", option_len
));
276 if (option_len
> length
) {
277 ND_PRINT((ndo
, " [bad length %u]", option_len
));
281 ND_TCHECK2(*cp
, option_len
);
283 switch (option_code
) {
288 if (ip_printts(ndo
, cp
, option_len
) == -1)
292 case IPOPT_RR
: /* fall through */
295 if (ip_printroute(ndo
, cp
, option_len
) == -1)
300 if (option_len
< 4) {
301 ND_PRINT((ndo
, " [bad length %u]", option_len
));
305 if (EXTRACT_16BITS(&cp
[2]) != 0)
306 ND_PRINT((ndo
, " value %u", EXTRACT_16BITS(&cp
[2])));
309 case IPOPT_NOP
: /* nothing to print - fall through */
318 ND_PRINT((ndo
, "%s", tstr
));
321 #define IP_RES 0x8000
323 static const struct tok ip_frag_values
[] = {
326 { IP_RES
, "rsvd" }, /* The RFC3514 evil ;-) bit */
330 struct ip_print_demux_state
{
339 ip_print_demux(netdissect_options
*ndo
,
340 struct ip_print_demux_state
*ipds
)
348 if (!ND_TTEST(*ipds
->cp
)) {
349 ND_PRINT((ndo
, "[|AH]"));
352 ipds
->nh
= *ipds
->cp
;
353 ipds
->advance
= ah_print(ndo
, ipds
->cp
);
354 if (ipds
->advance
<= 0)
356 ipds
->cp
+= ipds
->advance
;
357 ipds
->len
-= ipds
->advance
;
363 ipds
->advance
= esp_print(ndo
, ipds
->cp
, ipds
->len
,
364 (const u_char
*)ipds
->ip
,
366 if (ipds
->advance
<= 0)
368 ipds
->cp
+= ipds
->advance
;
369 ipds
->len
-= ipds
->advance
+ padlen
;
370 ipds
->nh
= enh
& 0xff;
376 ipcomp_print(ndo
, ipds
->cp
);
378 * Either this has decompressed the payload and
379 * printed it, in which case there's nothing more
380 * to do, or it hasn't, in which case there's
381 * nothing more to do.
387 sctp_print(ndo
, ipds
->cp
, (const u_char
*)ipds
->ip
, ipds
->len
);
391 dccp_print(ndo
, ipds
->cp
, (const u_char
*)ipds
->ip
, ipds
->len
);
395 /* pass on the MF bit plus the offset to detect fragments */
396 tcp_print(ndo
, ipds
->cp
, ipds
->len
, (const u_char
*)ipds
->ip
,
397 ipds
->off
& (IP_MF
|IP_OFFMASK
));
401 /* pass on the MF bit plus the offset to detect fragments */
402 udp_print(ndo
, ipds
->cp
, ipds
->len
, (const u_char
*)ipds
->ip
,
403 ipds
->off
& (IP_MF
|IP_OFFMASK
));
407 /* pass on the MF bit plus the offset to detect fragments */
408 icmp_print(ndo
, ipds
->cp
, ipds
->len
, (const u_char
*)ipds
->ip
,
409 ipds
->off
& (IP_MF
|IP_OFFMASK
));
414 * XXX - the current IANA protocol number assignments
415 * page lists 9 as "any private interior gateway
416 * (used by Cisco for their IGRP)" and 88 as
417 * "EIGRP" from Cisco.
419 * Recent BSD <netinet/in.h> headers define
420 * IP_PROTO_PIGP as 9 and IP_PROTO_IGRP as 88.
421 * We define IP_PROTO_PIGP as 9 and
422 * IP_PROTO_EIGRP as 88; those names better
423 * match was the current protocol number
426 igrp_print(ndo
, ipds
->cp
, ipds
->len
);
430 eigrp_print(ndo
, ipds
->cp
, ipds
->len
);
434 ND_PRINT((ndo
, " nd %d", ipds
->len
));
438 egp_print(ndo
, ipds
->cp
, ipds
->len
);
442 ospf_print(ndo
, ipds
->cp
, ipds
->len
, (const u_char
*)ipds
->ip
);
446 igmp_print(ndo
, ipds
->cp
, ipds
->len
);
450 /* DVMRP multicast tunnel (ip-in-ip encapsulation) */
451 ip_print(ndo
, ipds
->cp
, ipds
->len
);
452 if (! ndo
->ndo_vflag
) {
453 ND_PRINT((ndo
, " (ipip-proto-4)"));
459 /* ip6-in-ip encapsulation */
460 ip6_print(ndo
, ipds
->cp
, ipds
->len
);
464 rsvp_print(ndo
, ipds
->cp
, ipds
->len
);
469 gre_print(ndo
, ipds
->cp
, ipds
->len
);
473 mobile_print(ndo
, ipds
->cp
, ipds
->len
);
477 pim_print(ndo
, ipds
->cp
, ipds
->len
, (const u_char
*)ipds
->ip
);
481 if (ndo
->ndo_packettype
== PT_CARP
) {
483 ND_PRINT((ndo
, "carp %s > %s: ",
484 ipaddr_string(ndo
, &ipds
->ip
->ip_src
),
485 ipaddr_string(ndo
, &ipds
->ip
->ip_dst
)));
486 carp_print(ndo
, ipds
->cp
, ipds
->len
, ipds
->ip
->ip_ttl
);
489 ND_PRINT((ndo
, "vrrp %s > %s: ",
490 ipaddr_string(ndo
, &ipds
->ip
->ip_src
),
491 ipaddr_string(ndo
, &ipds
->ip
->ip_dst
)));
492 vrrp_print(ndo
, ipds
->cp
, ipds
->len
,
493 (const u_char
*)ipds
->ip
, ipds
->ip
->ip_ttl
);
498 pgm_print(ndo
, ipds
->cp
, ipds
->len
, (const u_char
*)ipds
->ip
);
502 if (ndo
->ndo_nflag
==0 && (p_name
= netdb_protoname(ipds
->nh
)) != NULL
)
503 ND_PRINT((ndo
, " %s", p_name
));
505 ND_PRINT((ndo
, " ip-proto-%d", ipds
->nh
));
506 ND_PRINT((ndo
, " %d", ipds
->len
));
512 ip_print_inner(netdissect_options
*ndo
,
514 u_int length
, u_int nh
,
517 struct ip_print_demux_state ipd
;
519 ipd
.ip
= (const struct ip
*)bp2
;
526 ip_print_demux(ndo
, &ipd
);
531 * print an IP datagram.
534 ip_print(netdissect_options
*ndo
,
538 struct ip_print_demux_state ipd
;
539 struct ip_print_demux_state
*ipds
=&ipd
;
542 struct cksum_vec vec
[1];
543 uint16_t sum
, ip_sum
;
546 ipds
->ip
= (const struct ip
*)bp
;
547 ND_TCHECK(ipds
->ip
->ip_vhl
);
548 if (IP_V(ipds
->ip
) != 4) { /* print version and fail if != 4 */
549 if (IP_V(ipds
->ip
) == 6)
550 ND_PRINT((ndo
, "IP6, wrong link-layer encapsulation "));
552 ND_PRINT((ndo
, "IP%u ", IP_V(ipds
->ip
)));
556 ND_PRINT((ndo
, "IP "));
558 ND_TCHECK(*ipds
->ip
);
559 if (length
< sizeof (struct ip
)) {
560 ND_PRINT((ndo
, "truncated-ip %u", length
));
563 hlen
= IP_HL(ipds
->ip
) * 4;
564 if (hlen
< sizeof (struct ip
)) {
565 ND_PRINT((ndo
, "bad-hlen %u", hlen
));
569 ipds
->len
= EXTRACT_16BITS(&ipds
->ip
->ip_len
);
570 if (length
< ipds
->len
)
571 ND_PRINT((ndo
, "truncated-ip - %u bytes missing! ",
572 ipds
->len
- length
));
573 if (ipds
->len
< hlen
) {
576 ND_PRINT((ndo
, "bad-len %u", ipds
->len
));
580 /* we guess that it is a TSO send */
584 ND_PRINT((ndo
, "bad-len %u", ipds
->len
));
586 #endif /* GUESS_TSO */
590 * Cut off the snapshot length to the end of the IP payload.
592 ipend
= bp
+ ipds
->len
;
593 if (ipend
< ndo
->ndo_snapend
)
594 ndo
->ndo_snapend
= ipend
;
598 ipds
->off
= EXTRACT_16BITS(&ipds
->ip
->ip_off
);
600 if (ndo
->ndo_vflag
) {
601 ND_PRINT((ndo
, "(tos 0x%x", (int)ipds
->ip
->ip_tos
));
603 switch (ipds
->ip
->ip_tos
& 0x03) {
609 ND_PRINT((ndo
, ",ECT(1)"));
613 ND_PRINT((ndo
, ",ECT(0)"));
617 ND_PRINT((ndo
, ",CE"));
621 if (ipds
->ip
->ip_ttl
>= 1)
622 ND_PRINT((ndo
, ", ttl %u", ipds
->ip
->ip_ttl
));
625 * for the firewall guys, print id, offset.
626 * On all but the last stick a "+" in the flags portion.
627 * For unfragmented datagrams, note the don't fragment flag.
630 ND_PRINT((ndo
, ", id %u, offset %u, flags [%s], proto %s (%u)",
631 EXTRACT_16BITS(&ipds
->ip
->ip_id
),
632 (ipds
->off
& 0x1fff) * 8,
633 bittok2str(ip_frag_values
, "none", ipds
->off
&0xe000),
634 tok2str(ipproto_values
,"unknown",ipds
->ip
->ip_p
),
637 ND_PRINT((ndo
, ", length %u", EXTRACT_16BITS(&ipds
->ip
->ip_len
)));
639 if ((hlen
- sizeof(struct ip
)) > 0) {
640 ND_PRINT((ndo
, ", options ("));
641 ip_optprint(ndo
, (const u_char
*)(ipds
->ip
+ 1), hlen
- sizeof(struct ip
));
642 ND_PRINT((ndo
, ")"));
645 if (!ndo
->ndo_Kflag
&& (const u_char
*)ipds
->ip
+ hlen
<= ndo
->ndo_snapend
) {
646 vec
[0].ptr
= (const uint8_t *)(const void *)ipds
->ip
;
648 sum
= in_cksum(vec
, 1);
650 ip_sum
= EXTRACT_16BITS(&ipds
->ip
->ip_sum
);
651 ND_PRINT((ndo
, ", bad cksum %x (->%x)!", ip_sum
,
652 in_cksum_shouldbe(ip_sum
, sum
)));
656 ND_PRINT((ndo
, ")\n "));
660 * If this is fragment zero, hand it to the next higher
663 if ((ipds
->off
& 0x1fff) == 0) {
664 ipds
->cp
= (const u_char
*)ipds
->ip
+ hlen
;
665 ipds
->nh
= ipds
->ip
->ip_p
;
667 if (ipds
->nh
!= IPPROTO_TCP
&& ipds
->nh
!= IPPROTO_UDP
&&
668 ipds
->nh
!= IPPROTO_SCTP
&& ipds
->nh
!= IPPROTO_DCCP
) {
669 ND_PRINT((ndo
, "%s > %s: ",
670 ipaddr_string(ndo
, &ipds
->ip
->ip_src
),
671 ipaddr_string(ndo
, &ipds
->ip
->ip_dst
)));
673 ip_print_demux(ndo
, ipds
);
676 * Ultra quiet now means that all this stuff should be
679 if (ndo
->ndo_qflag
> 1)
683 * This isn't the first frag, so we're missing the
684 * next level protocol header. print the ip addr
687 ND_PRINT((ndo
, "%s > %s:", ipaddr_string(ndo
, &ipds
->ip
->ip_src
),
688 ipaddr_string(ndo
, &ipds
->ip
->ip_dst
)));
689 if (!ndo
->ndo_nflag
&& (p_name
= netdb_protoname(ipds
->ip
->ip_p
)) != NULL
)
690 ND_PRINT((ndo
, " %s", p_name
));
692 ND_PRINT((ndo
, " ip-proto-%d", ipds
->ip
->ip_p
));
697 ND_PRINT((ndo
, "%s", tstr
));
702 ipN_print(netdissect_options
*ndo
, register const u_char
*bp
, register u_int length
)
705 ND_PRINT((ndo
, "truncated-ip %d", length
));
710 switch (*bp
& 0xF0) {
712 ip_print (ndo
, bp
, length
);
715 ip6_print (ndo
, bp
, length
);
718 ND_PRINT((ndo
, "unknown ip %d", (*bp
& 0xF0) >> 4));
724 ND_PRINT((ndo
, "%s", tstr
));
730 * c-style: whitesmith