]> The Tcpdump Group git mirrors - tcpdump/blob - print-ip.c
warn if v6 was found using ip_print()
[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[] =
24 "@(#) $Header: /tcpdump/master/tcpdump/print-ip.c,v 1.126 2003-05-25 16:40:48 hannes 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
43 /* Compatibility */
44 #ifndef IPPROTO_ND
45 #define IPPROTO_ND 77
46 #endif
47
48 /*
49 * print the recorded route in an IP RR, LSRR or SSRR option.
50 */
51 static void
52 ip_printroute(const char *type, register const u_char *cp, u_int length)
53 {
54 register u_int ptr = cp[2] - 1;
55 register u_int len;
56
57 printf(" %s{", type);
58 if ((length + 1) & 3)
59 printf(" [bad length %d]", length);
60 if (ptr < 3 || ((ptr + 1) & 3) || ptr > length + 1)
61 printf(" [bad ptr %d]", cp[2]);
62
63 type = "";
64 for (len = 3; len < length; len += 4) {
65 if (ptr == len)
66 type = "#";
67 printf("%s%s", type, ipaddr_string(&cp[len]));
68 type = " ";
69 }
70 printf("%s}", ptr == len? "#" : "");
71 }
72
73 /*
74 * If source-routing is present, return the final destination.
75 * Otherwise, return IP destination.
76 *
77 * This is used for UDP and TCP pseudo-header in the checksum
78 * calculation.
79 */
80 u_int32_t
81 ip_finddst(const struct ip *ip)
82 {
83 int length;
84 int len;
85 const u_char *cp;
86 u_int32_t retval;
87
88 cp = (const u_char *)(ip + 1);
89 length = (IP_HL(ip) << 2) - sizeof(struct ip);
90
91 for (; length > 0; cp += len, length -= len) {
92 int tt = *cp;
93
94 if (tt == IPOPT_NOP || tt == IPOPT_EOL)
95 len = 1;
96 else {
97 if (&cp[1] >= snapend) {
98 return 0;
99 }
100 len = cp[1];
101 }
102 if (len <= 0) {
103 return 0;
104 }
105 if (&cp[1] >= snapend || cp + len > snapend) {
106 return 0;
107 }
108 switch (tt) {
109
110 case IPOPT_SSRR:
111 case IPOPT_LSRR:
112 memcpy(&retval, cp + len - 4, 4);
113 return retval;
114 }
115 }
116 return ip->ip_dst.s_addr;
117 }
118
119 static void
120 ip_printts(register const u_char *cp, u_int length)
121 {
122 register u_int ptr = cp[2] - 1;
123 register u_int len = 0;
124 int hoplen;
125 const char *type;
126
127 printf(" TS{");
128 hoplen = ((cp[3]&0xF) != IPOPT_TS_TSONLY) ? 8 : 4;
129 if ((length - 4) & (hoplen-1))
130 printf("[bad length %d]", length);
131 if (ptr < 4 || ((ptr - 4) & (hoplen-1)) || ptr > length + 1)
132 printf("[bad ptr %d]", cp[2]);
133 switch (cp[3]&0xF) {
134 case IPOPT_TS_TSONLY:
135 printf("TSONLY");
136 break;
137 case IPOPT_TS_TSANDADDR:
138 printf("TS+ADDR");
139 break;
140 /*
141 * prespecified should really be 3, but some ones might send 2
142 * instead, and the IPOPT_TS_PRESPEC constant can apparently
143 * have both values, so we have to hard-code it here.
144 */
145
146 case 2:
147 printf("PRESPEC2.0");
148 break;
149 case 3: /* IPOPT_TS_PRESPEC */
150 printf("PRESPEC");
151 break;
152 default:
153 printf("[bad ts type %d]", cp[3]&0xF);
154 goto done;
155 }
156
157 type = " ";
158 for (len = 4; len < length; len += hoplen) {
159 if (ptr == len)
160 type = " ^ ";
161 printf("%s%d@%s", type, EXTRACT_32BITS(&cp[len+hoplen-4]),
162 hoplen!=8 ? "" : ipaddr_string(&cp[len]));
163 type = " ";
164 }
165
166 done:
167 printf("%s", ptr == len ? " ^ " : "");
168
169 if (cp[3]>>4)
170 printf(" [%d hops not recorded]} ", cp[3]>>4);
171 else
172 printf("}");
173 }
174
175 /*
176 * print IP options.
177 */
178 static void
179 ip_optprint(register const u_char *cp, u_int length)
180 {
181 register u_int len;
182
183 for (; length > 0; cp += len, length -= len) {
184 int tt = *cp;
185
186 if (tt == IPOPT_NOP || tt == IPOPT_EOL)
187 len = 1;
188 else {
189 if (&cp[1] >= snapend) {
190 printf("[|ip]");
191 return;
192 }
193 len = cp[1];
194 }
195 if (len <= 0) {
196 printf("[|ip op len %d]", len);
197 return;
198 }
199 if (&cp[1] >= snapend || cp + len > snapend) {
200 printf("[|ip]");
201 return;
202 }
203 switch (tt) {
204
205 case IPOPT_EOL:
206 printf(" EOL");
207 if (length > 1)
208 printf("-%d", length - 1);
209 return;
210
211 case IPOPT_NOP:
212 printf(" NOP");
213 break;
214
215 case IPOPT_TS:
216 ip_printts(cp, len);
217 break;
218
219 #ifndef IPOPT_SECURITY
220 #define IPOPT_SECURITY 130
221 #endif /* IPOPT_SECURITY */
222 case IPOPT_SECURITY:
223 printf(" SECURITY{%d}", len);
224 break;
225
226 case IPOPT_RR:
227 ip_printroute("RR", cp, len);
228 break;
229
230 case IPOPT_SSRR:
231 ip_printroute("SSRR", cp, len);
232 break;
233
234 case IPOPT_LSRR:
235 ip_printroute("LSRR", cp, len);
236 break;
237
238 #ifndef IPOPT_RA
239 #define IPOPT_RA 148 /* router alert */
240 #endif
241 case IPOPT_RA:
242 printf(" RA");
243 if (len != 4)
244 printf("{%d}", len);
245 else if (cp[2] || cp[3])
246 printf("%d.%d", cp[2], cp[3]);
247 break;
248
249 default:
250 printf(" IPOPT-%d{%d}", cp[0], len);
251 break;
252 }
253 }
254 }
255
256 /*
257 * compute an IP header checksum.
258 * don't modifiy the packet.
259 */
260 u_short
261 in_cksum(const u_short *addr, register u_int len, int csum)
262 {
263 int nleft = len;
264 const u_short *w = addr;
265 u_short answer;
266 int sum = csum;
267
268 /*
269 * Our algorithm is simple, using a 32 bit accumulator (sum),
270 * we add sequential 16 bit words to it, and at the end, fold
271 * back all the carry bits from the top 16 bits into the lower
272 * 16 bits.
273 */
274 while (nleft > 1) {
275 sum += *w++;
276 nleft -= 2;
277 }
278 if (nleft == 1)
279 sum += htons(*(u_char *)w<<8);
280
281 /*
282 * add back carry outs from top 16 bits to low 16 bits
283 */
284 sum = (sum >> 16) + (sum & 0xffff); /* add hi 16 to low 16 */
285 sum += (sum >> 16); /* add carry */
286 answer = ~sum; /* truncate to 16 bits */
287 return (answer);
288 }
289
290 /*
291 * Given the host-byte-order value of the checksum field in a packet
292 * header, and the network-byte-order computed checksum of the data
293 * that the checksum covers (including the checksum itself), compute
294 * what the checksum field *should* have been.
295 */
296 u_int16_t
297 in_cksum_shouldbe(u_int16_t sum, u_int16_t computed_sum)
298 {
299 u_int32_t shouldbe;
300
301 /*
302 * The value that should have gone into the checksum field
303 * is the negative of the value gotten by summing up everything
304 * *but* the checksum field.
305 *
306 * We can compute that by subtracting the value of the checksum
307 * field from the sum of all the data in the packet, and then
308 * computing the negative of that value.
309 *
310 * "sum" is the value of the checksum field, and "computed_sum"
311 * is the negative of the sum of all the data in the packets,
312 * so that's -(-computed_sum - sum), or (sum + computed_sum).
313 *
314 * All the arithmetic in question is one's complement, so the
315 * addition must include an end-around carry; we do this by
316 * doing the arithmetic in 32 bits (with no sign-extension),
317 * and then adding the upper 16 bits of the sum, which contain
318 * the carry, to the lower 16 bits of the sum, and then do it
319 * again in case *that* sum produced a carry.
320 *
321 * As RFC 1071 notes, the checksum can be computed without
322 * byte-swapping the 16-bit words; summing 16-bit words
323 * on a big-endian machine gives a big-endian checksum, which
324 * can be directly stuffed into the big-endian checksum fields
325 * in protocol headers, and summing words on a little-endian
326 * machine gives a little-endian checksum, which must be
327 * byte-swapped before being stuffed into a big-endian checksum
328 * field.
329 *
330 * "computed_sum" is a network-byte-order value, so we must put
331 * it in host byte order before subtracting it from the
332 * host-byte-order value from the header; the adjusted checksum
333 * will be in host byte order, which is what we'll return.
334 */
335 shouldbe = sum;
336 shouldbe += ntohs(computed_sum);
337 shouldbe = (shouldbe & 0xFFFF) + (shouldbe >> 16);
338 shouldbe = (shouldbe & 0xFFFF) + (shouldbe >> 16);
339 return shouldbe;
340 }
341
342 #ifndef IP_MF
343 #define IP_MF 0x2000
344 #endif /* IP_MF */
345 #ifndef IP_DF
346 #define IP_DF 0x4000
347 #endif /* IP_DF */
348 #define IP_RES 0x8000
349
350 static struct tok ip_frag_values[] = {
351 { IP_MF, "+" },
352 { IP_DF, "DF" },
353 { IP_RES, "rsvd" }, /* The RFC3514 evil ;-) bit */
354 { 0, NULL }
355 };
356
357 /*
358 * print an IP datagram.
359 */
360 void
361 ip_print(register const u_char *bp, register u_int length)
362 {
363 register const struct ip *ip;
364 register u_int hlen, len, len0, off;
365 register const u_char *cp;
366 u_char nh;
367 int advance;
368 struct protoent *proto;
369 u_int16_t sum, ip_sum;
370 const char *sep = "";
371
372 ip = (const struct ip *)bp;
373 if (IP_V(ip) != 4) { /* print version if != 4 */
374 printf("IP%u ", IP_V(ip));
375 if (IP_V(ip) == 6)
376 printf(", wrong link-layer encapsulation");
377 }
378 else
379 printf("IP ");
380
381 if ((u_char *)(ip + 1) > snapend) {
382 printf("[|ip]");
383 return;
384 }
385 if (length < sizeof (struct ip)) {
386 (void)printf("truncated-ip %d", length);
387 return;
388 }
389 hlen = IP_HL(ip) * 4;
390 if (hlen < sizeof (struct ip)) {
391 (void)printf("bad-hlen %d", hlen);
392 return;
393 }
394
395 len = EXTRACT_16BITS(&ip->ip_len);
396 if (length < len)
397 (void)printf("truncated-ip - %d bytes missing! ",
398 len - length);
399 len -= hlen;
400 len0 = len;
401
402 off = EXTRACT_16BITS(&ip->ip_off);
403
404 if (vflag) {
405 (void)printf("(tos 0x%x", (int)ip->ip_tos);
406 /* ECN bits */
407 if (ip->ip_tos & 0x03) {
408 switch (ip->ip_tos & 0x03) {
409 case 1:
410 (void)printf(",ECT(1)");
411 break;
412 case 2:
413 (void)printf(",ECT(0)");
414 break;
415 case 3:
416 (void)printf(",CE");
417 }
418 }
419
420 if (ip->ip_ttl >= 1)
421 (void)printf(", ttl %3u", ip->ip_ttl);
422
423 /*
424 * for the firewall guys, print id, offset.
425 * On all but the last stick a "+" in the flags portion.
426 * For unfragmented datagrams, note the don't fragment flag.
427 */
428
429 (void)printf(", id %u, offset %u, flags [%s]",
430 EXTRACT_16BITS(&ip->ip_id),
431 (off & 0x1fff) * 8,
432 bittok2str(ip_frag_values, "none", off & 0xe000 ));
433
434 (void)printf(", length: %u", EXTRACT_16BITS(&ip->ip_len));
435
436 if ((hlen - sizeof(struct ip)) > 0) {
437 (void)printf(", optlength: %u (", hlen - (u_int)sizeof(struct ip));
438 ip_optprint((u_char *)(ip + 1), hlen - sizeof(struct ip));
439 printf(" )");
440 }
441
442 if ((u_char *)ip + hlen <= snapend) {
443 sum = in_cksum((const u_short *)ip, hlen, 0);
444 if (sum != 0) {
445 ip_sum = EXTRACT_16BITS(&ip->ip_sum);
446 (void)printf("%sbad cksum %x (->%x)!", sep,
447 ip_sum,
448 in_cksum_shouldbe(ip_sum, sum));
449 sep = ", ";
450 }
451 }
452
453 printf(") ");
454 }
455
456 /*
457 * If this is fragment zero, hand it to the next higher
458 * level protocol.
459 */
460 if ((off & 0x1fff) == 0) {
461 cp = (const u_char *)ip + hlen;
462 nh = ip->ip_p;
463
464 #ifndef IPPROTO_SCTP
465 #define IPPROTO_SCTP 132
466 #endif
467 if (nh != IPPROTO_TCP && nh != IPPROTO_UDP &&
468 nh != IPPROTO_SCTP) {
469 (void)printf("%s > %s: ", ipaddr_string(&ip->ip_src),
470 ipaddr_string(&ip->ip_dst));
471 }
472 again:
473 switch (nh) {
474
475 #ifndef IPPROTO_AH
476 #define IPPROTO_AH 51
477 #endif
478 case IPPROTO_AH:
479 nh = *cp;
480 advance = ah_print(cp);
481 cp += advance;
482 len -= advance;
483 goto again;
484
485 #ifndef IPPROTO_ESP
486 #define IPPROTO_ESP 50
487 #endif
488 case IPPROTO_ESP:
489 {
490 int enh, padlen;
491 advance = esp_print(cp, (const u_char *)ip, &enh, &padlen);
492 cp += advance;
493 len -= advance + padlen;
494 if (enh < 0)
495 break;
496 nh = enh & 0xff;
497 goto again;
498 }
499
500 #ifndef IPPROTO_IPCOMP
501 #define IPPROTO_IPCOMP 108
502 #endif
503 case IPPROTO_IPCOMP:
504 {
505 int enh;
506 advance = ipcomp_print(cp, &enh);
507 cp += advance;
508 len -= advance;
509 if (enh < 0)
510 break;
511 nh = enh & 0xff;
512 goto again;
513 }
514
515 case IPPROTO_SCTP:
516 sctp_print(cp, (const u_char *)ip, len);
517 break;
518
519 case IPPROTO_TCP:
520 tcp_print(cp, len, (const u_char *)ip, (off &~ 0x6000));
521 break;
522
523 case IPPROTO_UDP:
524 udp_print(cp, len, (const u_char *)ip, (off &~ 0x6000));
525 break;
526
527 case IPPROTO_ICMP:
528 /* pass on the MF bit plus the offset to detect fragments */
529 icmp_print(cp, len, (const u_char *)ip, (off & 0x3fff));
530 break;
531
532 #ifndef IPPROTO_IGRP
533 #define IPPROTO_IGRP 9
534 #endif
535 case IPPROTO_IGRP:
536 igrp_print(cp, len, (const u_char *)ip);
537 break;
538
539 case IPPROTO_ND:
540 (void)printf(" nd %d", len);
541 break;
542
543 case IPPROTO_EGP:
544 egp_print(cp);
545 break;
546
547 #ifndef IPPROTO_OSPF
548 #define IPPROTO_OSPF 89
549 #endif
550 case IPPROTO_OSPF:
551 ospf_print(cp, len, (const u_char *)ip);
552 break;
553
554 #ifndef IPPROTO_IGMP
555 #define IPPROTO_IGMP 2
556 #endif
557 case IPPROTO_IGMP:
558 igmp_print(cp, len);
559 break;
560
561 case 4:
562 /* DVMRP multicast tunnel (ip-in-ip encapsulation) */
563 ip_print(cp, len);
564 if (! vflag) {
565 printf(" (ipip-proto-4)");
566 return;
567 }
568 break;
569
570 #ifdef INET6
571 #ifndef IP6PROTO_ENCAP
572 #define IP6PROTO_ENCAP 41
573 #endif
574 case IP6PROTO_ENCAP:
575 /* ip6-in-ip encapsulation */
576 ip6_print(cp, len);
577 break;
578 #endif /*INET6*/
579
580 #ifndef IPPROTO_RSVP
581 #define IPPROTO_RSVP 46
582 #endif
583 case IPPROTO_RSVP:
584 rsvp_print(cp, len);
585 break;
586
587 #ifndef IPPROTO_GRE
588 #define IPPROTO_GRE 47
589 #endif
590 case IPPROTO_GRE:
591 /* do it */
592 gre_print(cp, len);
593 break;
594
595 #ifndef IPPROTO_MOBILE
596 #define IPPROTO_MOBILE 55
597 #endif
598 case IPPROTO_MOBILE:
599 mobile_print(cp, len);
600 break;
601
602 #ifndef IPPROTO_PIM
603 #define IPPROTO_PIM 103
604 #endif
605 case IPPROTO_PIM:
606 pim_print(cp, len);
607 break;
608
609 #ifndef IPPROTO_VRRP
610 #define IPPROTO_VRRP 112
611 #endif
612 case IPPROTO_VRRP:
613 vrrp_print(cp, len, ip->ip_ttl);
614 break;
615
616 default:
617 if ((proto = getprotobynumber(nh)) != NULL)
618 (void)printf(" %s", proto->p_name);
619 else
620 (void)printf(" ip-proto-%d", nh);
621 printf(" %d", len);
622 break;
623 }
624 } else {
625 /* Ultra quiet now means that all this stuff should be suppressed */
626 if (qflag > 1) return;
627
628 /*
629 * if this isn't the first frag, we're missing the
630 * next level protocol header. print the ip addr
631 * and the protocol.
632 */
633 if (off & 0x1fff) {
634 (void)printf("%s > %s:", ipaddr_string(&ip->ip_src),
635 ipaddr_string(&ip->ip_dst));
636 if ((proto = getprotobynumber(ip->ip_p)) != NULL)
637 (void)printf(" %s", proto->p_name);
638 else
639 (void)printf(" ip-proto-%d", ip->ip_p);
640 }
641 }
642 }
643
644 void
645 ipN_print(register const u_char *bp, register u_int length)
646 {
647 struct ip *ip, hdr;
648
649 ip = (struct ip *)bp;
650 if (length < 4) {
651 (void)printf("truncated-ip %d", length);
652 return;
653 }
654 memcpy (&hdr, (char *)ip, 4);
655 switch (IP_V(&hdr)) {
656 case 4:
657 ip_print (bp, length);
658 return;
659 #ifdef INET6
660 case 6:
661 ip6_print (bp, length);
662 return;
663 #endif
664 default:
665 (void)printf("unknown ip %d", IP_V(&hdr));
666 return;
667 }
668 }
669
670
671