]>
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 #define NETDISSECT_REWORKED
27 #include <tcpdump-stdinc.h>
31 #include "interface.h"
32 #include "addrtoname.h"
33 #include "extract.h" /* must come after interface.h */
38 static const char tstr
[] = "[|ip]";
40 static const struct tok ip_option_values
[] = {
43 { IPOPT_TS
, "timestamp" },
44 { IPOPT_SECURITY
, "security" },
46 { IPOPT_SSRR
, "SSRR" },
47 { IPOPT_LSRR
, "LSRR" },
49 { IPOPT_RFC1393
, "traceroute" },
54 * print the recorded route in an IP RR, LSRR or SSRR option.
57 ip_printroute(netdissect_options
*ndo
,
58 register const u_char
*cp
, u_int length
)
64 ND_PRINT((ndo
, " [bad length %u]", length
));
68 ND_PRINT((ndo
, " [bad length %u]", length
));
70 if (ptr
< 3 || ((ptr
+ 1) & 3) || ptr
> length
+ 1)
71 ND_PRINT((ndo
, " [bad ptr %u]", cp
[2]));
73 for (len
= 3; len
< length
; len
+= 4) {
74 ND_PRINT((ndo
, " %s", ipaddr_string(ndo
, &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(netdissect_options
*ndo
,
96 cp
= (const u_char
*)(ip
+ 1);
97 length
= (IP_HL(ip
) << 2) - sizeof(struct ip
);
99 for (; length
> 0; cp
+= len
, length
-= len
) {
106 else if (tt
== IPOPT_NOP
)
114 ND_TCHECK2(*cp
, len
);
121 UNALIGNED_MEMCPY(&retval
, cp
+ len
- 4, 4);
126 UNALIGNED_MEMCPY(&retval
, &ip
->ip_dst
.s_addr
, sizeof(uint32_t));
131 * Compute a V4-style checksum by building a pseudoheader.
134 nextproto4_cksum(netdissect_options
*ndo
,
135 const struct ip
*ip
, const uint8_t *data
,
136 u_int len
, u_int covlen
, u_int next_proto
)
145 struct cksum_vec vec
[2];
147 /* pseudo-header.. */
148 ph
.len
= htons((uint16_t)len
);
150 ph
.proto
= next_proto
;
151 UNALIGNED_MEMCPY(&ph
.src
, &ip
->ip_src
.s_addr
, sizeof(uint32_t));
153 UNALIGNED_MEMCPY(&ph
.dst
, &ip
->ip_dst
.s_addr
, sizeof(uint32_t));
155 ph
.dst
= ip_finddst(ndo
, ip
);
157 vec
[0].ptr
= (const uint8_t *)(void *)&ph
;
158 vec
[0].len
= sizeof(ph
);
161 return (in_cksum(vec
, 2));
165 ip_printts(netdissect_options
*ndo
,
166 register const u_char
*cp
, u_int length
)
174 ND_PRINT((ndo
, "[bad length %u]", length
));
177 ND_PRINT((ndo
, " TS{"));
178 hoplen
= ((cp
[3]&0xF) != IPOPT_TS_TSONLY
) ? 8 : 4;
179 if ((length
- 4) & (hoplen
-1))
180 ND_PRINT((ndo
, "[bad length %u]", length
));
183 if (ptr
< 4 || ((ptr
- 4) & (hoplen
-1)) || ptr
> length
+ 1)
184 ND_PRINT((ndo
, "[bad ptr %u]", cp
[2]));
186 case IPOPT_TS_TSONLY
:
187 ND_PRINT((ndo
, "TSONLY"));
189 case IPOPT_TS_TSANDADDR
:
190 ND_PRINT((ndo
, "TS+ADDR"));
193 * prespecified should really be 3, but some ones might send 2
194 * instead, and the IPOPT_TS_PRESPEC constant can apparently
195 * have both values, so we have to hard-code it here.
199 ND_PRINT((ndo
, "PRESPEC2.0"));
201 case 3: /* IPOPT_TS_PRESPEC */
202 ND_PRINT((ndo
, "PRESPEC"));
205 ND_PRINT((ndo
, "[bad ts type %d]", cp
[3]&0xF));
210 for (len
= 4; len
< length
; len
+= hoplen
) {
213 ND_PRINT((ndo
, "%s%d@%s", type
, EXTRACT_32BITS(&cp
[len
+hoplen
-4]),
214 hoplen
!=8 ? "" : ipaddr_string(ndo
, &cp
[len
])));
219 ND_PRINT((ndo
, "%s", ptr
== len
? " ^ " : ""));
222 ND_PRINT((ndo
, " [%d hops not recorded]} ", cp
[3]>>4));
224 ND_PRINT((ndo
, "}"));
231 ip_optprint(netdissect_options
*ndo
,
232 register const u_char
*cp
, u_int length
)
234 register u_int option_len
;
235 const char *sep
= "";
237 for (; length
> 0; cp
+= option_len
, length
-= option_len
) {
240 ND_PRINT((ndo
, "%s", sep
));
247 tok2str(ip_option_values
,"unknown %u",option_code
)));
249 if (option_code
== IPOPT_NOP
||
250 option_code
== IPOPT_EOL
)
256 if (option_len
< 2) {
257 ND_PRINT((ndo
, " [bad length %u]", option_len
));
262 if (option_len
> length
) {
263 ND_PRINT((ndo
, " [bad length %u]", option_len
));
267 ND_TCHECK2(*cp
, option_len
);
269 switch (option_code
) {
274 ip_printts(ndo
, cp
, option_len
);
277 case IPOPT_RR
: /* fall through */
280 ip_printroute(ndo
, cp
, option_len
);
284 if (option_len
< 4) {
285 ND_PRINT((ndo
, " [bad length %u]", option_len
));
289 if (EXTRACT_16BITS(&cp
[2]) != 0)
290 ND_PRINT((ndo
, " value %u", EXTRACT_16BITS(&cp
[2])));
293 case IPOPT_NOP
: /* nothing to print - fall through */
302 ND_PRINT((ndo
, "%s", tstr
));
305 #define IP_RES 0x8000
307 static const struct tok ip_frag_values
[] = {
310 { IP_RES
, "rsvd" }, /* The RFC3514 evil ;-) bit */
314 struct ip_print_demux_state
{
323 ip_print_demux(netdissect_options
*ndo
,
324 struct ip_print_demux_state
*ipds
)
326 struct protoent
*proto
;
327 struct cksum_vec vec
[1];
333 ipds
->nh
= *ipds
->cp
;
334 ipds
->advance
= ah_print(ndo
, ipds
->cp
);
335 if (ipds
->advance
<= 0)
337 ipds
->cp
+= ipds
->advance
;
338 ipds
->len
-= ipds
->advance
;
344 ipds
->advance
= esp_print(ndo
, ipds
->cp
, ipds
->len
,
345 (const u_char
*)ipds
->ip
,
347 if (ipds
->advance
<= 0)
349 ipds
->cp
+= ipds
->advance
;
350 ipds
->len
-= ipds
->advance
+ padlen
;
351 ipds
->nh
= enh
& 0xff;
358 ipds
->advance
= ipcomp_print(ndo
, ipds
->cp
, &enh
);
359 if (ipds
->advance
<= 0)
361 ipds
->cp
+= ipds
->advance
;
362 ipds
->len
-= ipds
->advance
;
363 ipds
->nh
= enh
& 0xff;
368 sctp_print(ndo
, ipds
->cp
, (const u_char
*)ipds
->ip
, ipds
->len
);
372 dccp_print(ndo
, ipds
->cp
, (const u_char
*)ipds
->ip
, ipds
->len
);
376 /* pass on the MF bit plus the offset to detect fragments */
377 tcp_print(ndo
, ipds
->cp
, ipds
->len
, (const u_char
*)ipds
->ip
,
378 ipds
->off
& (IP_MF
|IP_OFFMASK
));
382 /* pass on the MF bit plus the offset to detect fragments */
383 udp_print(ndo
, ipds
->cp
, ipds
->len
, (const u_char
*)ipds
->ip
,
384 ipds
->off
& (IP_MF
|IP_OFFMASK
));
388 /* pass on the MF bit plus the offset to detect fragments */
389 icmp_print(ndo
, ipds
->cp
, ipds
->len
, (const u_char
*)ipds
->ip
,
390 ipds
->off
& (IP_MF
|IP_OFFMASK
));
395 * XXX - the current IANA protocol number assignments
396 * page lists 9 as "any private interior gateway
397 * (used by Cisco for their IGRP)" and 88 as
398 * "EIGRP" from Cisco.
400 * Recent BSD <netinet/in.h> headers define
401 * IP_PROTO_PIGP as 9 and IP_PROTO_IGRP as 88.
402 * We define IP_PROTO_PIGP as 9 and
403 * IP_PROTO_EIGRP as 88; those names better
404 * match was the current protocol number
407 igrp_print(ndo
, ipds
->cp
, ipds
->len
);
411 eigrp_print(ndo
, ipds
->cp
, ipds
->len
);
415 ND_PRINT((ndo
, " nd %d", ipds
->len
));
419 egp_print(ndo
, ipds
->cp
, ipds
->len
);
423 ospf_print(ndo
, ipds
->cp
, ipds
->len
, (const u_char
*)ipds
->ip
);
427 igmp_print(ndo
, ipds
->cp
, ipds
->len
);
431 /* DVMRP multicast tunnel (ip-in-ip encapsulation) */
432 ip_print(ndo
, ipds
->cp
, ipds
->len
);
433 if (! ndo
->ndo_vflag
) {
434 ND_PRINT((ndo
, " (ipip-proto-4)"));
440 /* ip6-in-ip encapsulation */
441 ip6_print(ndo
, ipds
->cp
, ipds
->len
);
445 rsvp_print(ndo
, ipds
->cp
, ipds
->len
);
450 gre_print(ndo
, ipds
->cp
, ipds
->len
);
454 mobile_print(ndo
, ipds
->cp
, ipds
->len
);
458 vec
[0].ptr
= ipds
->cp
;
459 vec
[0].len
= ipds
->len
;
460 pim_print(ndo
, ipds
->cp
, ipds
->len
, in_cksum(vec
, 1));
464 if (ndo
->ndo_packettype
== PT_CARP
) {
466 ND_PRINT((ndo
, "carp %s > %s: ",
467 ipaddr_string(ndo
, &ipds
->ip
->ip_src
),
468 ipaddr_string(ndo
, &ipds
->ip
->ip_dst
)));
469 carp_print(ndo
, ipds
->cp
, ipds
->len
, ipds
->ip
->ip_ttl
);
472 ND_PRINT((ndo
, "vrrp %s > %s: ",
473 ipaddr_string(ndo
, &ipds
->ip
->ip_src
),
474 ipaddr_string(ndo
, &ipds
->ip
->ip_dst
)));
475 vrrp_print(ndo
, ipds
->cp
, ipds
->len
,
476 (const u_char
*)ipds
->ip
, ipds
->ip
->ip_ttl
);
481 pgm_print(ndo
, ipds
->cp
, ipds
->len
, (const u_char
*)ipds
->ip
);
485 if (ndo
->ndo_nflag
==0 && (proto
= getprotobynumber(ipds
->nh
)) != NULL
)
486 ND_PRINT((ndo
, " %s", proto
->p_name
));
488 ND_PRINT((ndo
, " ip-proto-%d", ipds
->nh
));
489 ND_PRINT((ndo
, " %d", ipds
->len
));
495 ip_print_inner(netdissect_options
*ndo
,
497 u_int length
, u_int nh
,
500 struct ip_print_demux_state ipd
;
502 ipd
.ip
= (const struct ip
*)bp2
;
509 ip_print_demux(ndo
, &ipd
);
514 * print an IP datagram.
517 ip_print(netdissect_options
*ndo
,
521 struct ip_print_demux_state ipd
;
522 struct ip_print_demux_state
*ipds
=&ipd
;
525 struct cksum_vec vec
[1];
526 uint16_t sum
, ip_sum
;
527 struct protoent
*proto
;
529 ipds
->ip
= (const struct ip
*)bp
;
530 ND_TCHECK(ipds
->ip
->ip_vhl
);
531 if (IP_V(ipds
->ip
) != 4) { /* print version if != 4 */
532 if (IP_V(ipds
->ip
) == 6)
533 ND_PRINT((ndo
, "IP6, wrong link-layer encapsulation "));
535 ND_PRINT((ndo
, "IP%u ", IP_V(ipds
->ip
)));
537 else if (!ndo
->ndo_eflag
)
538 ND_PRINT((ndo
, "IP "));
540 ND_TCHECK(*ipds
->ip
);
541 if (length
< sizeof (struct ip
)) {
542 ND_PRINT((ndo
, "truncated-ip %u", length
));
545 hlen
= IP_HL(ipds
->ip
) * 4;
546 if (hlen
< sizeof (struct ip
)) {
547 ND_PRINT((ndo
, "bad-hlen %u", hlen
));
551 ipds
->len
= EXTRACT_16BITS(&ipds
->ip
->ip_len
);
552 if (length
< ipds
->len
)
553 ND_PRINT((ndo
, "truncated-ip - %u bytes missing! ",
554 ipds
->len
- length
));
555 if (ipds
->len
< hlen
) {
558 ND_PRINT((ndo
, "bad-len %u", ipds
->len
));
562 /* we guess that it is a TSO send */
566 ND_PRINT((ndo
, "bad-len %u", ipds
->len
));
568 #endif /* GUESS_TSO */
572 * Cut off the snapshot length to the end of the IP payload.
574 ipend
= bp
+ ipds
->len
;
575 if (ipend
< ndo
->ndo_snapend
)
576 ndo
->ndo_snapend
= ipend
;
580 ipds
->off
= EXTRACT_16BITS(&ipds
->ip
->ip_off
);
582 if (ndo
->ndo_vflag
) {
583 ND_PRINT((ndo
, "(tos 0x%x", (int)ipds
->ip
->ip_tos
));
585 if (ipds
->ip
->ip_tos
& 0x03) {
586 switch (ipds
->ip
->ip_tos
& 0x03) {
588 ND_PRINT((ndo
, ",ECT(1)"));
591 ND_PRINT((ndo
, ",ECT(0)"));
594 ND_PRINT((ndo
, ",CE"));
598 if (ipds
->ip
->ip_ttl
>= 1)
599 ND_PRINT((ndo
, ", ttl %u", ipds
->ip
->ip_ttl
));
602 * for the firewall guys, print id, offset.
603 * On all but the last stick a "+" in the flags portion.
604 * For unfragmented datagrams, note the don't fragment flag.
607 ND_PRINT((ndo
, ", id %u, offset %u, flags [%s], proto %s (%u)",
608 EXTRACT_16BITS(&ipds
->ip
->ip_id
),
609 (ipds
->off
& 0x1fff) * 8,
610 bittok2str(ip_frag_values
, "none", ipds
->off
&0xe000),
611 tok2str(ipproto_values
,"unknown",ipds
->ip
->ip_p
),
614 ND_PRINT((ndo
, ", length %u", EXTRACT_16BITS(&ipds
->ip
->ip_len
)));
616 if ((hlen
- sizeof(struct ip
)) > 0) {
617 ND_PRINT((ndo
, ", options ("));
618 ip_optprint(ndo
, (u_char
*)(ipds
->ip
+ 1), hlen
- sizeof(struct ip
));
619 ND_PRINT((ndo
, ")"));
622 if (!ndo
->ndo_Kflag
&& (u_char
*)ipds
->ip
+ hlen
<= ndo
->ndo_snapend
) {
623 vec
[0].ptr
= (const uint8_t *)(void *)ipds
->ip
;
625 sum
= in_cksum(vec
, 1);
627 ip_sum
= EXTRACT_16BITS(&ipds
->ip
->ip_sum
);
628 ND_PRINT((ndo
, ", bad cksum %x (->%x)!", ip_sum
,
629 in_cksum_shouldbe(ip_sum
, sum
)));
633 ND_PRINT((ndo
, ")\n "));
637 * If this is fragment zero, hand it to the next higher
640 if ((ipds
->off
& 0x1fff) == 0) {
641 ipds
->cp
= (const u_char
*)ipds
->ip
+ hlen
;
642 ipds
->nh
= ipds
->ip
->ip_p
;
644 if (ipds
->nh
!= IPPROTO_TCP
&& ipds
->nh
!= IPPROTO_UDP
&&
645 ipds
->nh
!= IPPROTO_SCTP
&& ipds
->nh
!= IPPROTO_DCCP
) {
646 ND_PRINT((ndo
, "%s > %s: ",
647 ipaddr_string(ndo
, &ipds
->ip
->ip_src
),
648 ipaddr_string(ndo
, &ipds
->ip
->ip_dst
)));
650 ip_print_demux(ndo
, ipds
);
652 /* Ultra quiet now means that all this stuff should be suppressed */
653 if (ndo
->ndo_qflag
> 1) return;
656 * if this isn't the first frag, we're missing the
657 * next level protocol header. print the ip addr
660 if (ipds
->off
& 0x1fff) {
661 ND_PRINT((ndo
, "%s > %s:", ipaddr_string(ndo
, &ipds
->ip
->ip_src
),
662 ipaddr_string(ndo
, &ipds
->ip
->ip_dst
)));
663 if (!ndo
->ndo_nflag
&& (proto
= getprotobynumber(ipds
->ip
->ip_p
)) != NULL
)
664 ND_PRINT((ndo
, " %s", proto
->p_name
));
666 ND_PRINT((ndo
, " ip-proto-%d", ipds
->ip
->ip_p
));
672 ND_PRINT((ndo
, "%s", tstr
));
677 ipN_print(netdissect_options
*ndo
, register const u_char
*bp
, register u_int length
)
682 ND_PRINT((ndo
, "truncated-ip %d", length
));
685 memcpy (&hdr
, bp
, 4);
686 switch (IP_V(&hdr
)) {
688 ip_print (ndo
, bp
, length
);
691 ip6_print (ndo
, bp
, length
);
694 ND_PRINT((ndo
, "unknown ip %d", IP_V(&hdr
)));
701 * c-style: whitesmith