]>
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 */
26 #include "netdissect-stdinc.h"
28 #include "netdissect.h"
29 #include "addrtoname.h"
36 static const struct tok ip_option_values
[] = {
39 { IPOPT_TS
, "timestamp" },
40 { IPOPT_SECURITY
, "security" },
42 { IPOPT_SSRR
, "SSRR" },
43 { IPOPT_LSRR
, "LSRR" },
45 { IPOPT_RFC1393
, "traceroute" },
50 * print the recorded route in an IP RR, LSRR or SSRR option.
53 ip_printroute(netdissect_options
*ndo
,
54 const u_char
*cp
, u_int length
)
60 ND_PRINT(" [bad length %u]", length
);
64 ND_PRINT(" [bad length %u]", length
);
65 ptr
= GET_U_1(cp
+ 2) - 1;
66 if (ptr
< 3 || ((ptr
+ 1) & 3) || ptr
> length
+ 1)
67 ND_PRINT(" [bad ptr %u]", GET_U_1(cp
+ 2));
69 for (len
= 3; len
< length
; len
+= 4) {
70 ND_TCHECK_4(cp
+ len
); /* Needed to print the IP addresses */
71 ND_PRINT(" %s", GET_IPADDR_STRING(cp
+ len
));
82 * If source-routing is present and valid, return the final destination.
83 * Otherwise, return IP destination.
85 * This is used for UDP and TCP pseudo-header in the checksum
89 ip_finddst(netdissect_options
*ndo
,
96 cp
= (const u_char
*)(ip
+ 1);
97 length
= IP_HL(ip
) * 4;
98 if (length
< sizeof(struct ip
))
100 length
-= sizeof(struct ip
);
102 for (; length
!= 0; cp
+= len
, length
-= len
) {
108 else if (tt
== IPOPT_NOP
)
111 len
= GET_U_1(cp
+ 1);
117 ND_TCHECK_LEN(cp
, len
);
124 return (GET_IPV4_TO_NETWORK_ORDER(cp
+ len
- 4));
128 return (GET_IPV4_TO_NETWORK_ORDER(ip
->ip_dst
));
132 * Compute a V4-style checksum by building a pseudoheader.
135 nextproto4_cksum(netdissect_options
*ndo
,
136 const struct ip
*ip
, const uint8_t *data
,
137 u_int len
, u_int covlen
, uint8_t next_proto
)
146 struct cksum_vec vec
[2];
148 /* pseudo-header.. */
149 ph
.len
= htons((uint16_t)len
);
151 ph
.proto
= next_proto
;
152 ph
.src
= GET_IPV4_TO_NETWORK_ORDER(ip
->ip_src
);
154 ph
.dst
= GET_IPV4_TO_NETWORK_ORDER(ip
->ip_dst
);
156 ph
.dst
= ip_finddst(ndo
, ip
);
158 vec
[0].ptr
= (const uint8_t *)(void *)&ph
;
159 vec
[0].len
= sizeof(ph
);
162 return (in_cksum(vec
, 2));
166 ip_printts(netdissect_options
*ndo
,
167 const u_char
*cp
, u_int length
)
175 ND_PRINT("[bad length %u]", length
);
179 hoplen
= ((GET_U_1(cp
+ 3) & 0xF) != IPOPT_TS_TSONLY
) ? 8 : 4;
180 if ((length
- 4) & (hoplen
-1))
181 ND_PRINT("[bad length %u]", length
);
182 ptr
= GET_U_1(cp
+ 2) - 1;
184 if (ptr
< 4 || ((ptr
- 4) & (hoplen
-1)) || ptr
> length
+ 1)
185 ND_PRINT("[bad ptr %u]", GET_U_1(cp
+ 2));
186 switch (GET_U_1(cp
+ 3)&0xF) {
187 case IPOPT_TS_TSONLY
:
190 case IPOPT_TS_TSANDADDR
:
193 case IPOPT_TS_PRESPEC
:
197 ND_PRINT("[bad ts type %u]", GET_U_1(cp
+ 3)&0xF);
202 for (len
= 4; len
< length
; len
+= hoplen
) {
205 ND_TCHECK_LEN(cp
+ len
, hoplen
);
206 ND_PRINT("%s%u@%s", type
, GET_BE_U_4(cp
+ len
+ hoplen
- 4),
207 hoplen
!=8 ? "" : GET_IPADDR_STRING(cp
+ len
));
212 ND_PRINT("%s", ptr
== len
? " ^ " : "");
214 if (GET_U_1(cp
+ 3) >> 4)
215 ND_PRINT(" [%u hops not recorded]} ", GET_U_1(cp
+ 3)>>4);
226 If truncated return -1, else 0.
229 ip_optprint(netdissect_options
*ndo
,
230 const u_char
*cp
, u_int length
)
233 const char *sep
= "";
235 for (; length
> 0; cp
+= option_len
, length
-= option_len
) {
241 option_code
= GET_U_1(cp
);
244 tok2str(ip_option_values
,"unknown %u",option_code
));
246 if (option_code
== IPOPT_NOP
||
247 option_code
== IPOPT_EOL
)
251 option_len
= GET_U_1(cp
+ 1);
252 if (option_len
< 2) {
253 ND_PRINT(" [bad length %u]", option_len
);
258 if (option_len
> length
) {
259 ND_PRINT(" [bad length %u]", option_len
);
263 ND_TCHECK_LEN(cp
, option_len
);
265 switch (option_code
) {
270 if (ip_printts(ndo
, cp
, option_len
) == -1)
274 case IPOPT_RR
: /* fall through */
277 if (ip_printroute(ndo
, cp
, option_len
) == -1)
282 if (option_len
< 4) {
283 ND_PRINT(" [bad length %u]", option_len
);
287 if (GET_BE_U_2(cp
+ 2) != 0)
288 ND_PRINT(" value %u", GET_BE_U_2(cp
+ 2));
291 case IPOPT_NOP
: /* nothing to print - fall through */
303 #define IP_RES 0x8000
305 static const struct tok ip_frag_values
[] = {
308 { IP_RES
, "rsvd" }, /* The RFC3514 evil ;-) bit */
314 * print an IP datagram.
317 ip_print(netdissect_options
*ndo
,
325 struct cksum_vec vec
[1];
326 uint8_t ip_tos
, ip_ttl
, ip_proto
;
327 uint16_t sum
, ip_sum
;
330 int presumed_tso
= 0;
332 ndo
->ndo_protocol
= "ip";
333 ip
= (const struct ip
*)bp
;
335 if (!ndo
->ndo_eflag
) {
336 nd_print_protocol_caps(ndo
);
340 ND_ICHECK_ZU(length
, <, sizeof (struct ip
));
341 ND_ICHECKMSG_U("version", IP_V(ip
), !=, 4);
343 hlen
= IP_HL(ip
) * 4;
344 ND_ICHECKMSG_ZU("header length", hlen
, <, sizeof (struct ip
));
346 len
= GET_BE_U_2(ip
->ip_len
);
348 ND_PRINT("[total length %u > length %u]", len
, length
);
349 nd_print_invalid(ndo
);
353 /* we guess that it is a TSO send */
357 ND_ICHECKMSG_U("total length", len
, <, hlen
);
361 * Cut off the snapshot length to the end of the IP payload
362 * or the end of the data in which it's contained, whichever
365 if (!nd_push_snaplen(ndo
, bp
, ND_MIN(length
, len
))) {
366 (*ndo
->ndo_error
)(ndo
, S_ERR_ND_MEM_ALLOC
,
367 "%s: can't push snaplen on buffer stack", __func__
);
372 off
= GET_BE_U_2(ip
->ip_off
);
374 ip_proto
= GET_U_1(ip
->ip_p
);
376 if (ndo
->ndo_vflag
) {
377 ip_tos
= GET_U_1(ip
->ip_tos
);
378 ND_PRINT("(tos 0x%x", ip_tos
);
380 switch (ip_tos
& 0x03) {
398 ip_ttl
= GET_U_1(ip
->ip_ttl
);
400 ND_PRINT(", ttl %u", ip_ttl
);
403 * for the firewall guys, print id, offset.
404 * On all but the last stick a "+" in the flags portion.
405 * For unfragmented datagrams, note the don't fragment flag.
407 ND_PRINT(", id %u, offset %u, flags [%s], proto %s (%u)",
408 GET_BE_U_2(ip
->ip_id
),
409 (off
& IP_OFFMASK
) * 8,
410 bittok2str(ip_frag_values
, "none", off
& (IP_RES
|IP_DF
|IP_MF
)),
411 tok2str(ipproto_values
, "unknown", ip_proto
),
415 ND_PRINT(", length %u [was 0, presumed TSO]", length
);
417 ND_PRINT(", length %u", GET_BE_U_2(ip
->ip_len
));
419 if ((hlen
> sizeof(struct ip
))) {
420 ND_PRINT(", options (");
421 if (ip_optprint(ndo
, (const u_char
*)(ip
+ 1),
422 hlen
- sizeof(struct ip
)) == -1) {
423 ND_PRINT(" [truncated-option]");
429 if (!ndo
->ndo_Kflag
&& ND_TTEST_LEN((const u_char
*)ip
, hlen
)) {
430 vec
[0].ptr
= (const uint8_t *)(const void *)ip
;
432 sum
= in_cksum(vec
, 1);
434 ip_sum
= GET_BE_U_2(ip
->ip_sum
);
435 ND_PRINT(", bad cksum %x (->%x)!", ip_sum
,
436 in_cksum_shouldbe(ip_sum
, sum
));
442 ND_PRINT("%s > %s: ",
443 GET_IPADDR_STRING(ip
->ip_src
),
444 GET_IPADDR_STRING(ip
->ip_dst
));
446 nd_pop_packet_info(ndo
);
452 * If this is fragment zero, hand it to the next higher
453 * level protocol. Let them know whether there are more
456 if ((off
& IP_OFFMASK
) == 0) {
457 uint8_t nh
= GET_U_1(ip
->ip_p
);
459 if (nh
!= IPPROTO_TCP
&& nh
!= IPPROTO_UDP
&&
460 nh
!= IPPROTO_SCTP
&& nh
!= IPPROTO_DCCP
) {
461 ND_PRINT("%s > %s: ",
462 GET_IPADDR_STRING(ip
->ip_src
),
463 GET_IPADDR_STRING(ip
->ip_dst
));
466 * Do a bounds check before calling ip_demux_print().
467 * At least the header data is required.
469 if (!ND_TTEST_LEN((const u_char
*)ip
, hlen
)) {
470 ND_PRINT(" [remaining caplen(%u) < header length(%u)]",
471 ND_BYTES_AVAILABLE_AFTER((const u_char
*)ip
),
473 nd_trunc_longjmp(ndo
);
475 ip_demux_print(ndo
, (const u_char
*)ip
+ hlen
, len
, 4,
476 off
& IP_MF
, GET_U_1(ip
->ip_ttl
), nh
, bp
);
479 * Ultra quiet now means that all this stuff should be
482 if (ndo
->ndo_qflag
> 1) {
483 nd_pop_packet_info(ndo
);
488 * This isn't the first frag, so we're missing the
489 * next level protocol header. print the ip addr
492 ND_PRINT("%s > %s:", GET_IPADDR_STRING(ip
->ip_src
),
493 GET_IPADDR_STRING(ip
->ip_dst
));
494 if (!ndo
->ndo_nflag
&& (p_name
= netdb_protoname(ip_proto
)) != NULL
)
495 ND_PRINT(" %s", p_name
);
497 ND_PRINT(" ip-proto-%u", ip_proto
);
499 nd_pop_packet_info(ndo
);
507 nd_print_invalid(ndo
);
511 ipN_print(netdissect_options
*ndo
, const u_char
*bp
, u_int length
)
513 ndo
->ndo_protocol
= "ipn";
515 ND_PRINT("truncated-ip %u", length
);
519 switch (GET_U_1(bp
) & 0xF0) {
521 ip_print(ndo
, bp
, length
);
524 ip6_print(ndo
, bp
, length
);
527 ND_PRINT("unknown ip %u", (GET_U_1(bp
) & 0xF0) >> 4);