]> The Tcpdump Group git mirrors - tcpdump/blob - print-ip.c
Go with Wireshark's Internet checksum routine.
[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 #ifndef lint
23 static const char rcsid[] _U_ =
24 "@(#) $Header: /tcpdump/master/tcpdump/print-ip.c,v 1.159 2007-09-14 01:29:28 guy Exp $ (LBL)";
25 #endif
26
27 #ifdef HAVE_CONFIG_H
28 #include "config.h"
29 #endif
30
31 #include <tcpdump-stdinc.h>
32
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <string.h>
36
37 #include "addrtoname.h"
38 #include "interface.h"
39 #include "extract.h" /* must come after interface.h */
40
41 #include "ip.h"
42 #include "ipproto.h"
43
44 struct tok ip_option_values[] = {
45 { IPOPT_EOL, "EOL" },
46 { IPOPT_NOP, "NOP" },
47 { IPOPT_TS, "timestamp" },
48 { IPOPT_SECURITY, "security" },
49 { IPOPT_RR, "RR" },
50 { IPOPT_SSRR, "SSRR" },
51 { IPOPT_LSRR, "LSRR" },
52 { IPOPT_RA, "RA" },
53 { IPOPT_RFC1393, "traceroute" },
54 { 0, NULL }
55 };
56
57 /*
58 * print the recorded route in an IP RR, LSRR or SSRR option.
59 */
60 static void
61 ip_printroute(register const u_char *cp, u_int length)
62 {
63 register u_int ptr;
64 register u_int len;
65
66 if (length < 3) {
67 printf(" [bad length %u]", length);
68 return;
69 }
70 if ((length + 1) & 3)
71 printf(" [bad length %u]", length);
72 ptr = cp[2] - 1;
73 if (ptr < 3 || ((ptr + 1) & 3) || ptr > length + 1)
74 printf(" [bad ptr %u]", cp[2]);
75
76 for (len = 3; len < length; len += 4) {
77 printf(" %s", ipaddr_string(&cp[len]));
78 if (ptr > len)
79 printf(",");
80 }
81 }
82
83 /*
84 * If source-routing is present and valid, return the final destination.
85 * Otherwise, return IP destination.
86 *
87 * This is used for UDP and TCP pseudo-header in the checksum
88 * calculation.
89 */
90 u_int32_t
91 ip_finddst(const struct ip *ip)
92 {
93 int length;
94 int len;
95 const u_char *cp;
96 u_int32_t retval;
97
98 cp = (const u_char *)(ip + 1);
99 length = (IP_HL(ip) << 2) - sizeof(struct ip);
100
101 for (; length > 0; cp += len, length -= len) {
102 int tt;
103
104 TCHECK(*cp);
105 tt = *cp;
106 if (tt == IPOPT_EOL)
107 break;
108 else if (tt == IPOPT_NOP)
109 len = 1;
110 else {
111 TCHECK(cp[1]);
112 len = cp[1];
113 if (len < 2)
114 break;
115 }
116 TCHECK2(*cp, len);
117 switch (tt) {
118
119 case IPOPT_SSRR:
120 case IPOPT_LSRR:
121 if (len < 7)
122 break;
123 memcpy(&retval, cp + len - 4, 4);
124 return retval;
125 }
126 }
127 trunc:
128 memcpy(&retval, &ip->ip_dst.s_addr, sizeof(u_int32_t));
129 return retval;
130 }
131
132 static void
133 ip_printts(register const u_char *cp, u_int length)
134 {
135 register u_int ptr;
136 register u_int len;
137 int hoplen;
138 const char *type;
139
140 if (length < 4) {
141 printf("[bad length %u]", length);
142 return;
143 }
144 printf(" TS{");
145 hoplen = ((cp[3]&0xF) != IPOPT_TS_TSONLY) ? 8 : 4;
146 if ((length - 4) & (hoplen-1))
147 printf("[bad length %u]", length);
148 ptr = cp[2] - 1;
149 len = 0;
150 if (ptr < 4 || ((ptr - 4) & (hoplen-1)) || ptr > length + 1)
151 printf("[bad ptr %u]", cp[2]);
152 switch (cp[3]&0xF) {
153 case IPOPT_TS_TSONLY:
154 printf("TSONLY");
155 break;
156 case IPOPT_TS_TSANDADDR:
157 printf("TS+ADDR");
158 break;
159 /*
160 * prespecified should really be 3, but some ones might send 2
161 * instead, and the IPOPT_TS_PRESPEC constant can apparently
162 * have both values, so we have to hard-code it here.
163 */
164
165 case 2:
166 printf("PRESPEC2.0");
167 break;
168 case 3: /* IPOPT_TS_PRESPEC */
169 printf("PRESPEC");
170 break;
171 default:
172 printf("[bad ts type %d]", cp[3]&0xF);
173 goto done;
174 }
175
176 type = " ";
177 for (len = 4; len < length; len += hoplen) {
178 if (ptr == len)
179 type = " ^ ";
180 printf("%s%d@%s", type, EXTRACT_32BITS(&cp[len+hoplen-4]),
181 hoplen!=8 ? "" : ipaddr_string(&cp[len]));
182 type = " ";
183 }
184
185 done:
186 printf("%s", ptr == len ? " ^ " : "");
187
188 if (cp[3]>>4)
189 printf(" [%d hops not recorded]} ", cp[3]>>4);
190 else
191 printf("}");
192 }
193
194 /*
195 * print IP options.
196 */
197 static void
198 ip_optprint(register const u_char *cp, u_int length)
199 {
200 register u_int option_len;
201 const char *sep = "";
202
203 for (; length > 0; cp += option_len, length -= option_len) {
204 u_int option_code;
205
206 printf("%s", sep);
207 sep = ",";
208
209 TCHECK(*cp);
210 option_code = *cp;
211
212 printf("%s",
213 tok2str(ip_option_values,"unknown %u",option_code));
214
215 if (option_code == IPOPT_NOP ||
216 option_code == IPOPT_EOL)
217 option_len = 1;
218
219 else {
220 TCHECK(cp[1]);
221 option_len = cp[1];
222 if (option_len < 2) {
223 printf(" [bad length %u]", option_len);
224 return;
225 }
226 }
227
228 if (option_len > length) {
229 printf(" [bad length %u]", option_len);
230 return;
231 }
232
233 TCHECK2(*cp, option_len);
234
235 switch (option_code) {
236 case IPOPT_EOL:
237 return;
238
239 case IPOPT_TS:
240 ip_printts(cp, option_len);
241 break;
242
243 case IPOPT_RR: /* fall through */
244 case IPOPT_SSRR:
245 case IPOPT_LSRR:
246 ip_printroute(cp, option_len);
247 break;
248
249 case IPOPT_RA:
250 if (option_len < 4) {
251 printf(" [bad length %u]", option_len);
252 break;
253 }
254 TCHECK(cp[3]);
255 if (EXTRACT_16BITS(&cp[2]) != 0)
256 printf(" value %u", EXTRACT_16BITS(&cp[2]));
257 break;
258
259 case IPOPT_NOP: /* nothing to print - fall through */
260 case IPOPT_SECURITY:
261 default:
262 break;
263 }
264 }
265 return;
266
267 trunc:
268 printf("[|ip]");
269 }
270
271 #define IP_RES 0x8000
272
273 static struct tok ip_frag_values[] = {
274 { IP_MF, "+" },
275 { IP_DF, "DF" },
276 { IP_RES, "rsvd" }, /* The RFC3514 evil ;-) bit */
277 { 0, NULL }
278 };
279
280 struct ip_print_demux_state {
281 const struct ip *ip;
282 const u_char *cp;
283 u_int len, off;
284 u_char nh;
285 int advance;
286 };
287
288 static void
289 ip_print_demux(netdissect_options *ndo,
290 struct ip_print_demux_state *ipds)
291 {
292 struct protoent *proto;
293 struct cksum_vec vec[1];
294
295 again:
296 switch (ipds->nh) {
297
298 case IPPROTO_AH:
299 ipds->nh = *ipds->cp;
300 ipds->advance = ah_print(ipds->cp);
301 if (ipds->advance <= 0)
302 break;
303 ipds->cp += ipds->advance;
304 ipds->len -= ipds->advance;
305 goto again;
306
307 case IPPROTO_ESP:
308 {
309 int enh, padlen;
310 ipds->advance = esp_print(ndo, ipds->cp, ipds->len,
311 (const u_char *)ipds->ip,
312 &enh, &padlen);
313 if (ipds->advance <= 0)
314 break;
315 ipds->cp += ipds->advance;
316 ipds->len -= ipds->advance + padlen;
317 ipds->nh = enh & 0xff;
318 goto again;
319 }
320
321 case IPPROTO_IPCOMP:
322 {
323 int enh;
324 ipds->advance = ipcomp_print(ipds->cp, &enh);
325 if (ipds->advance <= 0)
326 break;
327 ipds->cp += ipds->advance;
328 ipds->len -= ipds->advance;
329 ipds->nh = enh & 0xff;
330 goto again;
331 }
332
333 case IPPROTO_SCTP:
334 sctp_print(ipds->cp, (const u_char *)ipds->ip, ipds->len);
335 break;
336
337 case IPPROTO_DCCP:
338 dccp_print(ipds->cp, (const u_char *)ipds->ip, ipds->len);
339 break;
340
341 case IPPROTO_TCP:
342 /* pass on the MF bit plus the offset to detect fragments */
343 tcp_print(ipds->cp, ipds->len, (const u_char *)ipds->ip,
344 ipds->off & (IP_MF|IP_OFFMASK));
345 break;
346
347 case IPPROTO_UDP:
348 /* pass on the MF bit plus the offset to detect fragments */
349 udp_print(ipds->cp, ipds->len, (const u_char *)ipds->ip,
350 ipds->off & (IP_MF|IP_OFFMASK));
351 break;
352
353 case IPPROTO_ICMP:
354 /* pass on the MF bit plus the offset to detect fragments */
355 icmp_print(ipds->cp, ipds->len, (const u_char *)ipds->ip,
356 ipds->off & (IP_MF|IP_OFFMASK));
357 break;
358
359 case IPPROTO_PIGP:
360 /*
361 * XXX - the current IANA protocol number assignments
362 * page lists 9 as "any private interior gateway
363 * (used by Cisco for their IGRP)" and 88 as
364 * "EIGRP" from Cisco.
365 *
366 * Recent BSD <netinet/in.h> headers define
367 * IP_PROTO_PIGP as 9 and IP_PROTO_IGRP as 88.
368 * We define IP_PROTO_PIGP as 9 and
369 * IP_PROTO_EIGRP as 88; those names better
370 * match was the current protocol number
371 * assignments say.
372 */
373 igrp_print(ipds->cp, ipds->len, (const u_char *)ipds->ip);
374 break;
375
376 case IPPROTO_EIGRP:
377 eigrp_print(ipds->cp, ipds->len);
378 break;
379
380 case IPPROTO_ND:
381 ND_PRINT((ndo, " nd %d", ipds->len));
382 break;
383
384 case IPPROTO_EGP:
385 egp_print(ipds->cp, ipds->len);
386 break;
387
388 case IPPROTO_OSPF:
389 ospf_print(ipds->cp, ipds->len, (const u_char *)ipds->ip);
390 break;
391
392 case IPPROTO_IGMP:
393 igmp_print(ipds->cp, ipds->len);
394 break;
395
396 case IPPROTO_IPV4:
397 /* DVMRP multicast tunnel (ip-in-ip encapsulation) */
398 ip_print(ndo, ipds->cp, ipds->len);
399 if (! vflag) {
400 ND_PRINT((ndo, " (ipip-proto-4)"));
401 return;
402 }
403 break;
404
405 #ifdef INET6
406 case IPPROTO_IPV6:
407 /* ip6-in-ip encapsulation */
408 ip6_print(ndo, ipds->cp, ipds->len);
409 break;
410 #endif /*INET6*/
411
412 case IPPROTO_RSVP:
413 rsvp_print(ipds->cp, ipds->len);
414 break;
415
416 case IPPROTO_GRE:
417 /* do it */
418 gre_print(ipds->cp, ipds->len);
419 break;
420
421 case IPPROTO_MOBILE:
422 mobile_print(ipds->cp, ipds->len);
423 break;
424
425 case IPPROTO_PIM:
426 vec[0].ptr = ipds->cp;
427 vec[0].len = ipds->len;
428 pim_print(ipds->cp, ipds->len, in_cksum(vec, 1));
429 break;
430
431 case IPPROTO_VRRP:
432 vrrp_print(ipds->cp, ipds->len, ipds->ip->ip_ttl);
433 break;
434
435 case IPPROTO_PGM:
436 pgm_print(ipds->cp, ipds->len, (const u_char *)ipds->ip);
437 break;
438
439 default:
440 if ((proto = getprotobynumber(ipds->nh)) != NULL)
441 ND_PRINT((ndo, " %s", proto->p_name));
442 else
443 ND_PRINT((ndo, " ip-proto-%d", ipds->nh));
444 ND_PRINT((ndo, " %d", ipds->len));
445 break;
446 }
447 }
448
449 void
450 ip_print_inner(netdissect_options *ndo,
451 const u_char *bp,
452 u_int length, u_int nh,
453 const u_char *bp2)
454 {
455 struct ip_print_demux_state ipd;
456
457 ipd.ip = (const struct ip *)bp2;
458 ipd.cp = bp;
459 ipd.len = length;
460 ipd.off = 0;
461 ipd.nh = nh;
462 ipd.advance = 0;
463
464 ip_print_demux(ndo, &ipd);
465 }
466
467
468 /*
469 * print an IP datagram.
470 */
471 void
472 ip_print(netdissect_options *ndo,
473 const u_char *bp,
474 u_int length)
475 {
476 struct ip_print_demux_state ipd;
477 struct ip_print_demux_state *ipds=&ipd;
478 const u_char *ipend;
479 u_int hlen;
480 struct cksum_vec vec[1];
481 u_int16_t sum, ip_sum;
482 struct protoent *proto;
483
484 ipds->ip = (const struct ip *)bp;
485 if (IP_V(ipds->ip) != 4) { /* print version if != 4 */
486 printf("IP%u ", IP_V(ipds->ip));
487 if (IP_V(ipds->ip) == 6)
488 printf(", wrong link-layer encapsulation");
489 }
490 else if (!eflag)
491 printf("IP ");
492
493 if ((u_char *)(ipds->ip + 1) > ndo->ndo_snapend) {
494 printf("[|ip]");
495 return;
496 }
497 if (length < sizeof (struct ip)) {
498 (void)printf("truncated-ip %u", length);
499 return;
500 }
501 hlen = IP_HL(ipds->ip) * 4;
502 if (hlen < sizeof (struct ip)) {
503 (void)printf("bad-hlen %u", hlen);
504 return;
505 }
506
507 ipds->len = EXTRACT_16BITS(&ipds->ip->ip_len);
508 if (length < ipds->len)
509 (void)printf("truncated-ip - %u bytes missing! ",
510 ipds->len - length);
511 if (ipds->len < hlen) {
512 #ifdef GUESS_TSO
513 if (ipds->len) {
514 (void)printf("bad-len %u", ipds->len);
515 return;
516 }
517 else {
518 /* we guess that it is a TSO send */
519 ipds->len = length;
520 }
521 #else
522 (void)printf("bad-len %u", ipds->len);
523 return;
524 #endif /* GUESS_TSO */
525 }
526
527 /*
528 * Cut off the snapshot length to the end of the IP payload.
529 */
530 ipend = bp + ipds->len;
531 if (ipend < ndo->ndo_snapend)
532 ndo->ndo_snapend = ipend;
533
534 ipds->len -= hlen;
535
536 ipds->off = EXTRACT_16BITS(&ipds->ip->ip_off);
537
538 if (vflag) {
539 (void)printf("(tos 0x%x", (int)ipds->ip->ip_tos);
540 /* ECN bits */
541 if (ipds->ip->ip_tos & 0x03) {
542 switch (ipds->ip->ip_tos & 0x03) {
543 case 1:
544 (void)printf(",ECT(1)");
545 break;
546 case 2:
547 (void)printf(",ECT(0)");
548 break;
549 case 3:
550 (void)printf(",CE");
551 }
552 }
553
554 if (ipds->ip->ip_ttl >= 1)
555 (void)printf(", ttl %u", ipds->ip->ip_ttl);
556
557 /*
558 * for the firewall guys, print id, offset.
559 * On all but the last stick a "+" in the flags portion.
560 * For unfragmented datagrams, note the don't fragment flag.
561 */
562
563 (void)printf(", id %u, offset %u, flags [%s], proto %s (%u)",
564 EXTRACT_16BITS(&ipds->ip->ip_id),
565 (ipds->off & 0x1fff) * 8,
566 bittok2str(ip_frag_values, "none", ipds->off&0xe000),
567 tok2str(ipproto_values,"unknown",ipds->ip->ip_p),
568 ipds->ip->ip_p);
569
570 (void)printf(", length %u", EXTRACT_16BITS(&ipds->ip->ip_len));
571
572 if ((hlen - sizeof(struct ip)) > 0) {
573 printf(", options (");
574 ip_optprint((u_char *)(ipds->ip + 1), hlen - sizeof(struct ip));
575 printf(")");
576 }
577
578 if (!Kflag && (u_char *)ipds->ip + hlen <= ndo->ndo_snapend) {
579 vec[0].ptr = (const u_int8_t *)(void *)ipds->ip;
580 vec[0].len = hlen;
581 sum = in_cksum(vec, 1);
582 if (sum != 0) {
583 ip_sum = EXTRACT_16BITS(&ipds->ip->ip_sum);
584 (void)printf(", bad cksum %x (->%x)!", ip_sum,
585 in_cksum_shouldbe(ip_sum, sum));
586 }
587 }
588
589 printf(")\n ");
590 }
591
592 /*
593 * If this is fragment zero, hand it to the next higher
594 * level protocol.
595 */
596 if ((ipds->off & 0x1fff) == 0) {
597 ipds->cp = (const u_char *)ipds->ip + hlen;
598 ipds->nh = ipds->ip->ip_p;
599
600 if (ipds->nh != IPPROTO_TCP && ipds->nh != IPPROTO_UDP &&
601 ipds->nh != IPPROTO_SCTP && ipds->nh != IPPROTO_DCCP) {
602 (void)printf("%s > %s: ",
603 ipaddr_string(&ipds->ip->ip_src),
604 ipaddr_string(&ipds->ip->ip_dst));
605 }
606 ip_print_demux(ndo, ipds);
607 } else {
608 /* Ultra quiet now means that all this stuff should be suppressed */
609 if (qflag > 1) return;
610
611 /*
612 * if this isn't the first frag, we're missing the
613 * next level protocol header. print the ip addr
614 * and the protocol.
615 */
616 if (ipds->off & 0x1fff) {
617 (void)printf("%s > %s:", ipaddr_string(&ipds->ip->ip_src),
618 ipaddr_string(&ipds->ip->ip_dst));
619 if ((proto = getprotobynumber(ipds->ip->ip_p)) != NULL)
620 (void)printf(" %s", proto->p_name);
621 else
622 (void)printf(" ip-proto-%d", ipds->ip->ip_p);
623 }
624 }
625 }
626
627 void
628 ipN_print(register const u_char *bp, register u_int length)
629 {
630 struct ip *ip, hdr;
631
632 ip = (struct ip *)bp;
633 if (length < 4) {
634 (void)printf("truncated-ip %d", length);
635 return;
636 }
637 memcpy (&hdr, (char *)ip, 4);
638 switch (IP_V(&hdr)) {
639 case 4:
640 ip_print (gndo, bp, length);
641 return;
642 #ifdef INET6
643 case 6:
644 ip6_print (gndo, bp, length);
645 return;
646 #endif
647 default:
648 (void)printf("unknown ip %d", IP_V(&hdr));
649 return;
650 }
651 }
652
653 /*
654 * Local Variables:
655 * c-style: whitesmith
656 * c-basic-offset: 8
657 * End:
658 */
659
660