]>
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"
30 #include "netdissect.h"
31 #include "addrtoname.h"
38 static const struct tok ip_option_values
[] = {
41 { IPOPT_TS
, "timestamp" },
42 { IPOPT_SECURITY
, "security" },
44 { IPOPT_SSRR
, "SSRR" },
45 { IPOPT_LSRR
, "LSRR" },
47 { IPOPT_RFC1393
, "traceroute" },
52 * print the recorded route in an IP RR, LSRR or SSRR option.
55 ip_printroute(netdissect_options
*ndo
,
56 const u_char
*cp
, u_int length
)
62 ND_PRINT(" [bad length %u]", length
);
66 ND_PRINT(" [bad length %u]", length
);
67 ptr
= GET_U_1(cp
+ 2) - 1;
68 if (ptr
< 3 || ((ptr
+ 1) & 3) || ptr
> length
+ 1)
69 ND_PRINT(" [bad ptr %u]", GET_U_1(cp
+ 2));
71 for (len
= 3; len
< length
; len
+= 4) {
72 ND_TCHECK_4(cp
+ len
); /* Needed to print the IP addresses */
73 ND_PRINT(" %s", GET_IPADDR_STRING(cp
+ len
));
84 * If source-routing is present and valid, return the final destination.
85 * Otherwise, return IP destination.
87 * This is used for UDP and TCP pseudo-header in the checksum
91 ip_finddst(netdissect_options
*ndo
,
98 cp
= (const u_char
*)(ip
+ 1);
99 length
= IP_HL(ip
) * 4;
100 if (length
< sizeof(struct ip
))
102 length
-= sizeof(struct ip
);
104 for (; length
!= 0; cp
+= len
, length
-= len
) {
110 else if (tt
== IPOPT_NOP
)
113 len
= GET_U_1(cp
+ 1);
119 ND_TCHECK_LEN(cp
, len
);
126 return (GET_IPV4_TO_NETWORK_ORDER(cp
+ len
- 4));
130 return (GET_IPV4_TO_NETWORK_ORDER(ip
->ip_dst
));
134 * Compute a V4-style checksum by building a pseudoheader.
137 nextproto4_cksum(netdissect_options
*ndo
,
138 const struct ip
*ip
, const uint8_t *data
,
139 u_int len
, u_int covlen
, uint8_t next_proto
)
148 struct cksum_vec vec
[2];
150 /* pseudo-header.. */
151 ph
.len
= htons((uint16_t)len
);
153 ph
.proto
= next_proto
;
154 ph
.src
= GET_IPV4_TO_NETWORK_ORDER(ip
->ip_src
);
156 ph
.dst
= GET_IPV4_TO_NETWORK_ORDER(ip
->ip_dst
);
158 ph
.dst
= ip_finddst(ndo
, ip
);
160 vec
[0].ptr
= (const uint8_t *)(void *)&ph
;
161 vec
[0].len
= sizeof(ph
);
164 return (in_cksum(vec
, 2));
168 ip_printts(netdissect_options
*ndo
,
169 const u_char
*cp
, u_int length
)
177 ND_PRINT("[bad length %u]", length
);
181 hoplen
= ((GET_U_1(cp
+ 3) & 0xF) != IPOPT_TS_TSONLY
) ? 8 : 4;
182 if ((length
- 4) & (hoplen
-1))
183 ND_PRINT("[bad length %u]", length
);
184 ptr
= GET_U_1(cp
+ 2) - 1;
186 if (ptr
< 4 || ((ptr
- 4) & (hoplen
-1)) || ptr
> length
+ 1)
187 ND_PRINT("[bad ptr %u]", GET_U_1(cp
+ 2));
188 switch (GET_U_1(cp
+ 3)&0xF) {
189 case IPOPT_TS_TSONLY
:
192 case IPOPT_TS_TSANDADDR
:
195 case IPOPT_TS_PRESPEC
:
199 ND_PRINT("[bad ts type %u]", GET_U_1(cp
+ 3)&0xF);
204 for (len
= 4; len
< length
; len
+= hoplen
) {
207 ND_TCHECK_LEN(cp
+ len
, hoplen
);
208 ND_PRINT("%s%u@%s", type
, GET_BE_U_4(cp
+ len
+ hoplen
- 4),
209 hoplen
!=8 ? "" : GET_IPADDR_STRING(cp
+ len
));
214 ND_PRINT("%s", ptr
== len
? " ^ " : "");
216 if (GET_U_1(cp
+ 3) >> 4)
217 ND_PRINT(" [%u hops not recorded]} ", GET_U_1(cp
+ 3)>>4);
228 If truncated return -1, else 0.
231 ip_optprint(netdissect_options
*ndo
,
232 const u_char
*cp
, u_int length
)
235 const char *sep
= "";
237 for (; length
> 0; cp
+= option_len
, length
-= option_len
) {
243 option_code
= GET_U_1(cp
);
246 tok2str(ip_option_values
,"unknown %u",option_code
));
248 if (option_code
== IPOPT_NOP
||
249 option_code
== IPOPT_EOL
)
253 option_len
= GET_U_1(cp
+ 1);
254 if (option_len
< 2) {
255 ND_PRINT(" [bad length %u]", option_len
);
260 if (option_len
> length
) {
261 ND_PRINT(" [bad length %u]", option_len
);
265 ND_TCHECK_LEN(cp
, option_len
);
267 switch (option_code
) {
272 if (ip_printts(ndo
, cp
, option_len
) == -1)
276 case IPOPT_RR
: /* fall through */
279 if (ip_printroute(ndo
, cp
, option_len
) == -1)
284 if (option_len
< 4) {
285 ND_PRINT(" [bad length %u]", option_len
);
289 if (GET_BE_U_2(cp
+ 2) != 0)
290 ND_PRINT(" value %u", GET_BE_U_2(cp
+ 2));
293 case IPOPT_NOP
: /* nothing to print - fall through */
305 #define IP_RES 0x8000
307 static const struct tok ip_frag_values
[] = {
310 { IP_RES
, "rsvd" }, /* The RFC3514 evil ;-) bit */
316 * print an IP datagram.
319 ip_print(netdissect_options
*ndo
,
327 struct cksum_vec vec
[1];
328 uint8_t ip_tos
, ip_ttl
, ip_proto
;
329 uint16_t sum
, ip_sum
;
333 ndo
->ndo_protocol
= "ip";
334 ip
= (const struct ip
*)bp
;
335 if (IP_V(ip
) != 4) { /* print version and fail if != 4 */
337 ND_PRINT("IP6, wrong link-layer encapsulation");
339 ND_PRINT("IP%u", IP_V(ip
));
340 nd_print_invalid(ndo
);
347 if (length
< sizeof (struct ip
)) {
348 ND_PRINT("truncated-ip %u", length
);
351 hlen
= IP_HL(ip
) * 4;
352 if (hlen
< sizeof (struct ip
)) {
353 ND_PRINT("bad-hlen %u", hlen
);
357 len
= GET_BE_U_2(ip
->ip_len
);
359 ND_PRINT("truncated-ip - %u bytes missing! ",
364 ND_PRINT("bad-len %u", len
);
368 /* we guess that it is a TSO send */
372 ND_PRINT("bad-len %u", len
);
374 #endif /* GUESS_TSO */
378 * Cut off the snapshot length to the end of the IP payload.
380 nd_push_snapend(ndo
, bp
+ len
);
384 off
= GET_BE_U_2(ip
->ip_off
);
386 ip_proto
= GET_U_1(ip
->ip_p
);
388 if (ndo
->ndo_vflag
) {
389 ip_tos
= GET_U_1(ip
->ip_tos
);
390 ND_PRINT("(tos 0x%x", ip_tos
);
392 switch (ip_tos
& 0x03) {
410 ip_ttl
= GET_U_1(ip
->ip_ttl
);
412 ND_PRINT(", ttl %u", ip_ttl
);
415 * for the firewall guys, print id, offset.
416 * On all but the last stick a "+" in the flags portion.
417 * For unfragmented datagrams, note the don't fragment flag.
419 ND_PRINT(", id %u, offset %u, flags [%s], proto %s (%u)",
420 GET_BE_U_2(ip
->ip_id
),
421 (off
& IP_OFFMASK
) * 8,
422 bittok2str(ip_frag_values
, "none", off
& (IP_RES
|IP_DF
|IP_MF
)),
423 tok2str(ipproto_values
, "unknown", ip_proto
),
426 ND_PRINT(", length %u", GET_BE_U_2(ip
->ip_len
));
428 if ((hlen
- sizeof(struct ip
)) > 0) {
429 ND_PRINT(", options (");
430 if (ip_optprint(ndo
, (const u_char
*)(ip
+ 1),
431 hlen
- sizeof(struct ip
)) == -1) {
432 ND_PRINT(" [truncated-option]");
438 if (!ndo
->ndo_Kflag
&& (const u_char
*)ip
+ hlen
<= ndo
->ndo_snapend
) {
439 vec
[0].ptr
= (const uint8_t *)(const void *)ip
;
441 sum
= in_cksum(vec
, 1);
443 ip_sum
= GET_BE_U_2(ip
->ip_sum
);
444 ND_PRINT(", bad cksum %x (->%x)!", ip_sum
,
445 in_cksum_shouldbe(ip_sum
, sum
));
451 ND_PRINT("%s > %s: ",
452 GET_IPADDR_STRING(ip
->ip_src
),
453 GET_IPADDR_STRING(ip
->ip_dst
));
455 nd_pop_packet_info(ndo
);
461 * If this is fragment zero, hand it to the next higher
462 * level protocol. Let them know whether there are more
465 if ((off
& IP_OFFMASK
) == 0) {
466 uint8_t nh
= GET_U_1(ip
->ip_p
);
468 if (nh
!= IPPROTO_TCP
&& nh
!= IPPROTO_UDP
&&
469 nh
!= IPPROTO_SCTP
&& nh
!= IPPROTO_DCCP
) {
470 ND_PRINT("%s > %s: ",
471 GET_IPADDR_STRING(ip
->ip_src
),
472 GET_IPADDR_STRING(ip
->ip_dst
));
475 * Do a bounds check before calling ip_demux_print().
476 * At least the header data is required.
478 if (!ND_TTEST_LEN((const u_char
*)ip
, hlen
)) {
479 ND_PRINT(" [remaining caplen(%u) < header length(%u)]",
480 ND_BYTES_AVAILABLE_AFTER((const u_char
*)ip
),
482 nd_trunc_longjmp(ndo
);
484 ip_demux_print(ndo
, (const u_char
*)ip
+ hlen
, len
, 4,
485 off
& IP_MF
, GET_U_1(ip
->ip_ttl
), nh
, bp
);
488 * Ultra quiet now means that all this stuff should be
491 if (ndo
->ndo_qflag
> 1) {
492 nd_pop_packet_info(ndo
);
497 * This isn't the first frag, so we're missing the
498 * next level protocol header. print the ip addr
501 ND_PRINT("%s > %s:", GET_IPADDR_STRING(ip
->ip_src
),
502 GET_IPADDR_STRING(ip
->ip_dst
));
503 if (!ndo
->ndo_nflag
&& (p_name
= netdb_protoname(ip_proto
)) != NULL
)
504 ND_PRINT(" %s", p_name
);
506 ND_PRINT(" ip-proto-%u", ip_proto
);
508 nd_pop_packet_info(ndo
);
516 ipN_print(netdissect_options
*ndo
, const u_char
*bp
, u_int length
)
518 ndo
->ndo_protocol
= "ipn";
520 ND_PRINT("truncated-ip %u", length
);
524 switch (GET_U_1(bp
) & 0xF0) {
526 ip_print(ndo
, bp
, length
);
529 ip6_print(ndo
, bp
, length
);
532 ND_PRINT("unknown ip %u", (GET_U_1(bp
) & 0xF0) >> 4);