]> The Tcpdump Group git mirrors - tcpdump/blob - print-ip.c
Revert partially the commit 21b1273
[tcpdump] / print-ip.c
1 /*
2 * Copyright (c) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997
3 * The Regents of the University of California. All rights reserved.
4 *
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
16 * written permission.
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.
20 */
21
22 /* \summary: IP printer */
23
24 #ifdef HAVE_CONFIG_H
25 #include <config.h>
26 #endif
27
28 #include "netdissect-stdinc.h"
29
30 #include <string.h>
31
32 #include "netdissect.h"
33 #include "addrtoname.h"
34 #include "extract.h"
35
36 #include "ip.h"
37 #include "ipproto.h"
38
39
40 static const struct tok ip_option_values[] = {
41 { IPOPT_EOL, "EOL" },
42 { IPOPT_NOP, "NOP" },
43 { IPOPT_TS, "timestamp" },
44 { IPOPT_SECURITY, "security" },
45 { IPOPT_RR, "RR" },
46 { IPOPT_SSRR, "SSRR" },
47 { IPOPT_LSRR, "LSRR" },
48 { IPOPT_RA, "RA" },
49 { IPOPT_RFC1393, "traceroute" },
50 { 0, NULL }
51 };
52
53 /*
54 * print the recorded route in an IP RR, LSRR or SSRR option.
55 */
56 static int
57 ip_printroute(netdissect_options *ndo,
58 const u_char *cp, u_int length)
59 {
60 u_int ptr;
61 u_int len;
62
63 if (length < 3) {
64 ND_PRINT(" [bad length %u]", length);
65 return (0);
66 }
67 if ((length + 1) & 3)
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));
72
73 for (len = 3; len < length; len += 4) {
74 ND_PRINT(" %s", GET_IPADDR_STRING(cp + len));
75 if (ptr > len)
76 ND_PRINT(",");
77 }
78 return (0);
79 }
80
81 /*
82 * If source-routing is present and valid, return the final destination.
83 * Otherwise, return IP destination.
84 *
85 * This is used for UDP and TCP pseudo-header in the checksum
86 * calculation.
87 */
88 static uint32_t
89 ip_finddst(netdissect_options *ndo,
90 const struct ip *ip)
91 {
92 u_int length;
93 u_int len;
94 const u_char *cp;
95
96 cp = (const u_char *)(ip + 1);
97 length = IP_HL(ip) * 4;
98 if (length < sizeof(struct ip))
99 goto trunc;
100 length -= sizeof(struct ip);
101
102 for (; length != 0; cp += len, length -= len) {
103 int tt;
104
105 tt = GET_U_1(cp);
106 if (tt == IPOPT_EOL)
107 break;
108 else if (tt == IPOPT_NOP)
109 len = 1;
110 else {
111 len = GET_U_1(cp + 1);
112 if (len < 2)
113 break;
114 }
115 if (length < len)
116 goto trunc;
117 ND_TCHECK_LEN(cp, len);
118 switch (tt) {
119
120 case IPOPT_SSRR:
121 case IPOPT_LSRR:
122 if (len < 7)
123 break;
124 return (GET_IPV4_TO_NETWORK_ORDER(cp + len - 4));
125 }
126 }
127 trunc:
128 return (GET_IPV4_TO_NETWORK_ORDER(ip->ip_dst));
129 }
130
131 /*
132 * Compute a V4-style checksum by building a pseudoheader.
133 */
134 uint16_t
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)
138 {
139 struct phdr {
140 uint32_t src;
141 uint32_t dst;
142 uint8_t mbz;
143 uint8_t proto;
144 uint16_t len;
145 } ph;
146 struct cksum_vec vec[2];
147
148 /* pseudo-header.. */
149 ph.len = htons((uint16_t)len);
150 ph.mbz = 0;
151 ph.proto = next_proto;
152 ph.src = GET_IPV4_TO_NETWORK_ORDER(ip->ip_src);
153 if (IP_HL(ip) == 5)
154 ph.dst = GET_IPV4_TO_NETWORK_ORDER(ip->ip_dst);
155 else
156 ph.dst = ip_finddst(ndo, ip);
157
158 vec[0].ptr = (const uint8_t *)(void *)&ph;
159 vec[0].len = sizeof(ph);
160 vec[1].ptr = data;
161 vec[1].len = covlen;
162 return (in_cksum(vec, 2));
163 }
164
165 static int
166 ip_printts(netdissect_options *ndo,
167 const u_char *cp, u_int length)
168 {
169 u_int ptr;
170 u_int len;
171 u_int hoplen;
172 const char *type;
173
174 if (length < 4) {
175 ND_PRINT("[bad length %u]", length);
176 return (0);
177 }
178 ND_PRINT(" TS{");
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;
183 len = 0;
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:
188 ND_PRINT("TSONLY");
189 break;
190 case IPOPT_TS_TSANDADDR:
191 ND_PRINT("TS+ADDR");
192 break;
193 /*
194 * prespecified should really be 3, but some ones might send 2
195 * instead, and the IPOPT_TS_PRESPEC constant can apparently
196 * have both values, so we have to hard-code it here.
197 */
198
199 case 2:
200 ND_PRINT("PRESPEC2.0");
201 break;
202 case 3: /* IPOPT_TS_PRESPEC */
203 ND_PRINT("PRESPEC");
204 break;
205 default:
206 ND_PRINT("[bad ts type %u]", GET_U_1(cp + 3)&0xF);
207 goto done;
208 }
209
210 type = " ";
211 for (len = 4; len < length; len += hoplen) {
212 if (ptr == len)
213 type = " ^ ";
214 ND_TCHECK_LEN(cp + len, hoplen);
215 ND_PRINT("%s%u@%s", type, GET_BE_U_4(cp + len + hoplen - 4),
216 hoplen!=8 ? "" : GET_IPADDR_STRING(cp + len));
217 type = " ";
218 }
219
220 done:
221 ND_PRINT("%s", ptr == len ? " ^ " : "");
222
223 if (GET_U_1(cp + 3) >> 4)
224 ND_PRINT(" [%u hops not recorded]} ", GET_U_1(cp + 3)>>4);
225 else
226 ND_PRINT("}");
227 return (0);
228
229 trunc:
230 return (-1);
231 }
232
233 /*
234 * print IP options.
235 If truncated return -1, else 0.
236 */
237 static int
238 ip_optprint(netdissect_options *ndo,
239 const u_char *cp, u_int length)
240 {
241 u_int option_len;
242 const char *sep = "";
243
244 for (; length > 0; cp += option_len, length -= option_len) {
245 u_int option_code;
246
247 ND_PRINT("%s", sep);
248 sep = ",";
249
250 option_code = GET_U_1(cp);
251
252 ND_PRINT("%s",
253 tok2str(ip_option_values,"unknown %u",option_code));
254
255 if (option_code == IPOPT_NOP ||
256 option_code == IPOPT_EOL)
257 option_len = 1;
258
259 else {
260 option_len = GET_U_1(cp + 1);
261 if (option_len < 2) {
262 ND_PRINT(" [bad length %u]", option_len);
263 return 0;
264 }
265 }
266
267 if (option_len > length) {
268 ND_PRINT(" [bad length %u]", option_len);
269 return 0;
270 }
271
272 ND_TCHECK_LEN(cp, option_len);
273
274 switch (option_code) {
275 case IPOPT_EOL:
276 return 0;
277
278 case IPOPT_TS:
279 if (ip_printts(ndo, cp, option_len) == -1)
280 goto trunc;
281 break;
282
283 case IPOPT_RR: /* fall through */
284 case IPOPT_SSRR:
285 case IPOPT_LSRR:
286 if (ip_printroute(ndo, cp, option_len) == -1)
287 goto trunc;
288 break;
289
290 case IPOPT_RA:
291 if (option_len < 4) {
292 ND_PRINT(" [bad length %u]", option_len);
293 break;
294 }
295 ND_TCHECK_1(cp + 3);
296 if (GET_BE_U_2(cp + 2) != 0)
297 ND_PRINT(" value %u", GET_BE_U_2(cp + 2));
298 break;
299
300 case IPOPT_NOP: /* nothing to print - fall through */
301 case IPOPT_SECURITY:
302 default:
303 break;
304 }
305 }
306 return 0;
307
308 trunc:
309 return -1;
310 }
311
312 #define IP_RES 0x8000
313
314 static const struct tok ip_frag_values[] = {
315 { IP_MF, "+" },
316 { IP_DF, "DF" },
317 { IP_RES, "rsvd" }, /* The RFC3514 evil ;-) bit */
318 { 0, NULL }
319 };
320
321
322 /*
323 * print an IP datagram.
324 */
325 void
326 ip_print(netdissect_options *ndo,
327 const u_char *bp,
328 u_int length)
329 {
330 const struct ip *ip;
331 u_int off;
332 u_int hlen;
333 u_int len;
334 struct cksum_vec vec[1];
335 uint8_t ip_tos, ip_ttl, ip_proto;
336 uint16_t sum, ip_sum;
337 const char *p_name;
338 int truncated = 0;
339
340 ndo->ndo_protocol = "ip";
341 ip = (const struct ip *)bp;
342 if (IP_V(ip) != 4) { /* print version and fail if != 4 */
343 if (IP_V(ip) == 6)
344 ND_PRINT("IP6, wrong link-layer encapsulation");
345 else
346 ND_PRINT("IP%u", IP_V(ip));
347 nd_print_invalid(ndo);
348 return;
349 }
350 if (!ndo->ndo_eflag)
351 ND_PRINT("IP ");
352
353 ND_TCHECK_SIZE(ip);
354 if (length < sizeof (struct ip)) {
355 ND_PRINT("truncated-ip %u", length);
356 return;
357 }
358 hlen = IP_HL(ip) * 4;
359 if (hlen < sizeof (struct ip)) {
360 ND_PRINT("bad-hlen %u", hlen);
361 return;
362 }
363
364 len = GET_BE_U_2(ip->ip_len);
365 if (length < len)
366 ND_PRINT("truncated-ip - %u bytes missing! ",
367 len - length);
368 if (len < hlen) {
369 #ifdef GUESS_TSO
370 if (len) {
371 ND_PRINT("bad-len %u", len);
372 return;
373 }
374 else {
375 /* we guess that it is a TSO send */
376 len = length;
377 }
378 #else
379 ND_PRINT("bad-len %u", len);
380 return;
381 #endif /* GUESS_TSO */
382 }
383
384 /*
385 * Cut off the snapshot length to the end of the IP payload.
386 */
387 nd_push_snapend(ndo, bp + len);
388
389 len -= hlen;
390
391 off = GET_BE_U_2(ip->ip_off);
392
393 ip_proto = GET_U_1(ip->ip_p);
394
395 if (ndo->ndo_vflag) {
396 ip_tos = GET_U_1(ip->ip_tos);
397 ND_PRINT("(tos 0x%x", ip_tos);
398 /* ECN bits */
399 switch (ip_tos & 0x03) {
400
401 case 0:
402 break;
403
404 case 1:
405 ND_PRINT(",ECT(1)");
406 break;
407
408 case 2:
409 ND_PRINT(",ECT(0)");
410 break;
411
412 case 3:
413 ND_PRINT(",CE");
414 break;
415 }
416
417 ip_ttl = GET_U_1(ip->ip_ttl);
418 if (ip_ttl >= 1)
419 ND_PRINT(", ttl %u", ip_ttl);
420
421 /*
422 * for the firewall guys, print id, offset.
423 * On all but the last stick a "+" in the flags portion.
424 * For unfragmented datagrams, note the don't fragment flag.
425 */
426 ND_PRINT(", id %u, offset %u, flags [%s], proto %s (%u)",
427 GET_BE_U_2(ip->ip_id),
428 (off & IP_OFFMASK) * 8,
429 bittok2str(ip_frag_values, "none", off & (IP_RES|IP_DF|IP_MF)),
430 tok2str(ipproto_values, "unknown", ip_proto),
431 ip_proto);
432
433 ND_PRINT(", length %u", GET_BE_U_2(ip->ip_len));
434
435 if ((hlen - sizeof(struct ip)) > 0) {
436 ND_PRINT(", options (");
437 if (ip_optprint(ndo, (const u_char *)(ip + 1),
438 hlen - sizeof(struct ip)) == -1) {
439 ND_PRINT(" [truncated-option]");
440 truncated = 1;
441 }
442 ND_PRINT(")");
443 }
444
445 if (!ndo->ndo_Kflag && (const u_char *)ip + hlen <= ndo->ndo_snapend) {
446 vec[0].ptr = (const uint8_t *)(const void *)ip;
447 vec[0].len = hlen;
448 sum = in_cksum(vec, 1);
449 if (sum != 0) {
450 ip_sum = GET_BE_U_2(ip->ip_sum);
451 ND_PRINT(", bad cksum %x (->%x)!", ip_sum,
452 in_cksum_shouldbe(ip_sum, sum));
453 }
454 }
455
456 ND_PRINT(")\n ");
457 if (truncated) {
458 ND_PRINT("%s > %s: ",
459 GET_IPADDR_STRING(ip->ip_src),
460 GET_IPADDR_STRING(ip->ip_dst));
461 nd_print_trunc(ndo);
462 nd_pop_packet_info(ndo);
463 return;
464 }
465 }
466
467 /*
468 * If this is fragment zero, hand it to the next higher
469 * level protocol. Let them know whether there are more
470 * fragments.
471 */
472 if ((off & IP_OFFMASK) == 0) {
473 uint8_t nh = GET_U_1(ip->ip_p);
474
475 if (nh != IPPROTO_TCP && nh != IPPROTO_UDP &&
476 nh != IPPROTO_SCTP && nh != IPPROTO_DCCP) {
477 ND_PRINT("%s > %s: ",
478 GET_IPADDR_STRING(ip->ip_src),
479 GET_IPADDR_STRING(ip->ip_dst));
480 }
481 ip_demux_print(ndo, (const u_char *)ip + hlen, len, 4,
482 off & IP_MF, GET_U_1(ip->ip_ttl), nh, bp);
483 } else {
484 /*
485 * Ultra quiet now means that all this stuff should be
486 * suppressed.
487 */
488 if (ndo->ndo_qflag > 1) {
489 nd_pop_packet_info(ndo);
490 return;
491 }
492
493 /*
494 * This isn't the first frag, so we're missing the
495 * next level protocol header. print the ip addr
496 * and the protocol.
497 */
498 ND_PRINT("%s > %s:", GET_IPADDR_STRING(ip->ip_src),
499 GET_IPADDR_STRING(ip->ip_dst));
500 if (!ndo->ndo_nflag && (p_name = netdb_protoname(ip_proto)) != NULL)
501 ND_PRINT(" %s", p_name);
502 else
503 ND_PRINT(" ip-proto-%u", ip_proto);
504 }
505 nd_pop_packet_info(ndo);
506 return;
507
508 trunc:
509 nd_print_trunc(ndo);
510 return;
511 }
512
513 void
514 ipN_print(netdissect_options *ndo, const u_char *bp, u_int length)
515 {
516 ndo->ndo_protocol = "ipn";
517 if (length < 1) {
518 ND_PRINT("truncated-ip %u", length);
519 return;
520 }
521
522 switch (GET_U_1(bp) & 0xF0) {
523 case 0x40:
524 ip_print(ndo, bp, length);
525 break;
526 case 0x60:
527 ip6_print(ndo, bp, length);
528 break;
529 default:
530 ND_PRINT("unknown ip %u", (GET_U_1(bp) & 0xF0) >> 4);
531 break;
532 }
533 }