]>
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"
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 const u_char
*cp
, u_int length
)
64 ND_PRINT(" [bad length %u]", length
);
68 ND_PRINT(" [bad length %u]", length
);
69 ptr
= GET_U_1(cp
+ 2) - 1;
70 if (ptr
< 3 || ((ptr
+ 1) & 3) || ptr
> length
+ 1)
71 ND_PRINT(" [bad ptr %u]", GET_U_1(cp
+ 2));
73 for (len
= 3; len
< length
; len
+= 4) {
74 ND_TCHECK_4(cp
+ len
); /* Needed to print the IP addresses */
75 ND_PRINT(" %s", GET_IPADDR_STRING(cp
+ len
));
86 * If source-routing is present and valid, return the final destination.
87 * Otherwise, return IP destination.
89 * This is used for UDP and TCP pseudo-header in the checksum
93 ip_finddst(netdissect_options
*ndo
,
100 cp
= (const u_char
*)(ip
+ 1);
101 length
= IP_HL(ip
) * 4;
102 if (length
< sizeof(struct ip
))
104 length
-= sizeof(struct ip
);
106 for (; length
!= 0; cp
+= len
, length
-= len
) {
112 else if (tt
== IPOPT_NOP
)
115 len
= GET_U_1(cp
+ 1);
121 ND_TCHECK_LEN(cp
, len
);
128 return (GET_IPV4_TO_NETWORK_ORDER(cp
+ len
- 4));
132 return (GET_IPV4_TO_NETWORK_ORDER(ip
->ip_dst
));
136 * Compute a V4-style checksum by building a pseudoheader.
139 nextproto4_cksum(netdissect_options
*ndo
,
140 const struct ip
*ip
, const uint8_t *data
,
141 u_int len
, u_int covlen
, uint8_t next_proto
)
150 struct cksum_vec vec
[2];
152 /* pseudo-header.. */
153 ph
.len
= htons((uint16_t)len
);
155 ph
.proto
= next_proto
;
156 ph
.src
= GET_IPV4_TO_NETWORK_ORDER(ip
->ip_src
);
158 ph
.dst
= GET_IPV4_TO_NETWORK_ORDER(ip
->ip_dst
);
160 ph
.dst
= ip_finddst(ndo
, ip
);
162 vec
[0].ptr
= (const uint8_t *)(void *)&ph
;
163 vec
[0].len
= sizeof(ph
);
166 return (in_cksum(vec
, 2));
170 ip_printts(netdissect_options
*ndo
,
171 const u_char
*cp
, u_int length
)
179 ND_PRINT("[bad length %u]", length
);
183 hoplen
= ((GET_U_1(cp
+ 3) & 0xF) != IPOPT_TS_TSONLY
) ? 8 : 4;
184 if ((length
- 4) & (hoplen
-1))
185 ND_PRINT("[bad length %u]", length
);
186 ptr
= GET_U_1(cp
+ 2) - 1;
188 if (ptr
< 4 || ((ptr
- 4) & (hoplen
-1)) || ptr
> length
+ 1)
189 ND_PRINT("[bad ptr %u]", GET_U_1(cp
+ 2));
190 switch (GET_U_1(cp
+ 3)&0xF) {
191 case IPOPT_TS_TSONLY
:
194 case IPOPT_TS_TSANDADDR
:
197 case IPOPT_TS_PRESPEC
:
201 ND_PRINT("[bad ts type %u]", GET_U_1(cp
+ 3)&0xF);
206 for (len
= 4; len
< length
; len
+= hoplen
) {
209 ND_TCHECK_LEN(cp
+ len
, hoplen
);
210 ND_PRINT("%s%u@%s", type
, GET_BE_U_4(cp
+ len
+ hoplen
- 4),
211 hoplen
!=8 ? "" : GET_IPADDR_STRING(cp
+ len
));
216 ND_PRINT("%s", ptr
== len
? " ^ " : "");
218 if (GET_U_1(cp
+ 3) >> 4)
219 ND_PRINT(" [%u hops not recorded]} ", GET_U_1(cp
+ 3)>>4);
230 If truncated return -1, else 0.
233 ip_optprint(netdissect_options
*ndo
,
234 const u_char
*cp
, u_int length
)
237 const char *sep
= "";
239 for (; length
> 0; cp
+= option_len
, length
-= option_len
) {
245 option_code
= GET_U_1(cp
);
248 tok2str(ip_option_values
,"unknown %u",option_code
));
250 if (option_code
== IPOPT_NOP
||
251 option_code
== IPOPT_EOL
)
255 option_len
= GET_U_1(cp
+ 1);
256 if (option_len
< 2) {
257 ND_PRINT(" [bad length %u]", option_len
);
262 if (option_len
> length
) {
263 ND_PRINT(" [bad length %u]", option_len
);
267 ND_TCHECK_LEN(cp
, option_len
);
269 switch (option_code
) {
274 if (ip_printts(ndo
, cp
, option_len
) == -1)
278 case IPOPT_RR
: /* fall through */
281 if (ip_printroute(ndo
, cp
, option_len
) == -1)
286 if (option_len
< 4) {
287 ND_PRINT(" [bad length %u]", option_len
);
291 if (GET_BE_U_2(cp
+ 2) != 0)
292 ND_PRINT(" value %u", GET_BE_U_2(cp
+ 2));
295 case IPOPT_NOP
: /* nothing to print - fall through */
307 #define IP_RES 0x8000
309 static const struct tok ip_frag_values
[] = {
312 { IP_RES
, "rsvd" }, /* The RFC3514 evil ;-) bit */
318 * print an IP datagram.
321 ip_print(netdissect_options
*ndo
,
329 struct cksum_vec vec
[1];
330 uint8_t ip_tos
, ip_ttl
, ip_proto
;
331 uint16_t sum
, ip_sum
;
335 ndo
->ndo_protocol
= "ip";
336 ip
= (const struct ip
*)bp
;
337 if (IP_V(ip
) != 4) { /* print version and fail if != 4 */
339 ND_PRINT("IP6, wrong link-layer encapsulation");
341 ND_PRINT("IP%u", IP_V(ip
));
342 nd_print_invalid(ndo
);
349 if (length
< sizeof (struct ip
)) {
350 ND_PRINT("truncated-ip %u", length
);
353 hlen
= IP_HL(ip
) * 4;
354 if (hlen
< sizeof (struct ip
)) {
355 ND_PRINT("bad-hlen %u", hlen
);
359 len
= GET_BE_U_2(ip
->ip_len
);
361 ND_PRINT("truncated-ip - %u bytes missing! ",
366 ND_PRINT("bad-len %u", len
);
370 /* we guess that it is a TSO send */
374 ND_PRINT("bad-len %u", len
);
376 #endif /* GUESS_TSO */
380 * Cut off the snapshot length to the end of the IP payload.
382 nd_push_snapend(ndo
, bp
+ len
);
386 off
= GET_BE_U_2(ip
->ip_off
);
388 ip_proto
= GET_U_1(ip
->ip_p
);
390 if (ndo
->ndo_vflag
) {
391 ip_tos
= GET_U_1(ip
->ip_tos
);
392 ND_PRINT("(tos 0x%x", ip_tos
);
394 switch (ip_tos
& 0x03) {
412 ip_ttl
= GET_U_1(ip
->ip_ttl
);
414 ND_PRINT(", ttl %u", ip_ttl
);
417 * for the firewall guys, print id, offset.
418 * On all but the last stick a "+" in the flags portion.
419 * For unfragmented datagrams, note the don't fragment flag.
421 ND_PRINT(", id %u, offset %u, flags [%s], proto %s (%u)",
422 GET_BE_U_2(ip
->ip_id
),
423 (off
& IP_OFFMASK
) * 8,
424 bittok2str(ip_frag_values
, "none", off
& (IP_RES
|IP_DF
|IP_MF
)),
425 tok2str(ipproto_values
, "unknown", ip_proto
),
428 ND_PRINT(", length %u", GET_BE_U_2(ip
->ip_len
));
430 if ((hlen
- sizeof(struct ip
)) > 0) {
431 ND_PRINT(", options (");
432 if (ip_optprint(ndo
, (const u_char
*)(ip
+ 1),
433 hlen
- sizeof(struct ip
)) == -1) {
434 ND_PRINT(" [truncated-option]");
440 if (!ndo
->ndo_Kflag
&& (const u_char
*)ip
+ hlen
<= ndo
->ndo_snapend
) {
441 vec
[0].ptr
= (const uint8_t *)(const void *)ip
;
443 sum
= in_cksum(vec
, 1);
445 ip_sum
= GET_BE_U_2(ip
->ip_sum
);
446 ND_PRINT(", bad cksum %x (->%x)!", ip_sum
,
447 in_cksum_shouldbe(ip_sum
, sum
));
453 ND_PRINT("%s > %s: ",
454 GET_IPADDR_STRING(ip
->ip_src
),
455 GET_IPADDR_STRING(ip
->ip_dst
));
457 nd_pop_packet_info(ndo
);
463 * If this is fragment zero, hand it to the next higher
464 * level protocol. Let them know whether there are more
467 if ((off
& IP_OFFMASK
) == 0) {
468 uint8_t nh
= GET_U_1(ip
->ip_p
);
470 if (nh
!= IPPROTO_TCP
&& nh
!= IPPROTO_UDP
&&
471 nh
!= IPPROTO_SCTP
&& nh
!= IPPROTO_DCCP
) {
472 ND_PRINT("%s > %s: ",
473 GET_IPADDR_STRING(ip
->ip_src
),
474 GET_IPADDR_STRING(ip
->ip_dst
));
476 ip_demux_print(ndo
, (const u_char
*)ip
+ hlen
, len
, 4,
477 off
& IP_MF
, GET_U_1(ip
->ip_ttl
), nh
, bp
);
480 * Ultra quiet now means that all this stuff should be
483 if (ndo
->ndo_qflag
> 1) {
484 nd_pop_packet_info(ndo
);
489 * This isn't the first frag, so we're missing the
490 * next level protocol header. print the ip addr
493 ND_PRINT("%s > %s:", GET_IPADDR_STRING(ip
->ip_src
),
494 GET_IPADDR_STRING(ip
->ip_dst
));
495 if (!ndo
->ndo_nflag
&& (p_name
= netdb_protoname(ip_proto
)) != NULL
)
496 ND_PRINT(" %s", p_name
);
498 ND_PRINT(" ip-proto-%u", ip_proto
);
500 nd_pop_packet_info(ndo
);
509 ipN_print(netdissect_options
*ndo
, const u_char
*bp
, u_int length
)
511 ndo
->ndo_protocol
= "ipn";
513 ND_PRINT("truncated-ip %u", length
);
517 switch (GET_U_1(bp
) & 0xF0) {
519 ip_print(ndo
, bp
, length
);
522 ip6_print(ndo
, bp
, length
);
525 ND_PRINT("unknown ip %u", (GET_U_1(bp
) & 0xF0) >> 4);