This commit sets new users to see the DragonFly-tips fortunes instead
[dragonfly.git] / contrib / tcpdump-3.8.3 / print-ip.c
blob0cc559892e1c529f756925baf9099d2cc33d3b8a
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.
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.
22 #ifndef lint
23 static const char rcsid[] _U_ =
24 "@(#) $Header: /tcpdump/master/tcpdump/print-ip.c,v 1.128.2.6 2004/03/24 09:01:39 guy Exp $ (LBL)";
25 #endif
27 #ifdef HAVE_CONFIG_H
28 #include "config.h"
29 #endif
31 #include <tcpdump-stdinc.h>
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <string.h>
37 #include "addrtoname.h"
38 #include "interface.h"
39 #include "extract.h" /* must come after interface.h */
41 #include "ip.h"
42 #include "ipproto.h"
45 * print the recorded route in an IP RR, LSRR or SSRR option.
47 static void
48 ip_printroute(const char *type, register const u_char *cp, u_int length)
50 register u_int ptr;
51 register u_int len;
53 if (length < 3) {
54 printf(" [bad length %u]", length);
55 return;
57 printf(" %s{", type);
58 if ((length + 1) & 3)
59 printf(" [bad length %u]", length);
60 ptr = cp[2] - 1;
61 if (ptr < 3 || ((ptr + 1) & 3) || ptr > length + 1)
62 printf(" [bad ptr %u]", cp[2]);
64 type = "";
65 for (len = 3; len < length; len += 4) {
66 if (ptr == len)
67 type = "#";
68 printf("%s%s", type, ipaddr_string(&cp[len]));
69 type = " ";
71 printf("%s}", ptr == len? "#" : "");
75 * If source-routing is present, return the final destination.
76 * Otherwise, return IP destination.
78 * This is used for UDP and TCP pseudo-header in the checksum
79 * calculation.
81 u_int32_t
82 ip_finddst(const struct ip *ip)
84 int length;
85 int len;
86 const u_char *cp;
87 u_int32_t retval;
89 cp = (const u_char *)(ip + 1);
90 length = (IP_HL(ip) << 2) - sizeof(struct ip);
92 for (; length > 0; cp += len, length -= len) {
93 int tt;
95 TCHECK(*cp);
96 tt = *cp;
97 if (tt == IPOPT_NOP || tt == IPOPT_EOL)
98 len = 1;
99 else {
100 TCHECK(cp[1]);
101 len = cp[1];
103 if (len < 2) {
104 return 0;
106 TCHECK2(*cp, len);
107 switch (tt) {
109 case IPOPT_SSRR:
110 case IPOPT_LSRR:
111 if (len < 7)
112 return 0;
113 memcpy(&retval, cp + len - 4, 4);
114 return retval;
117 return ip->ip_dst.s_addr;
119 trunc:
120 return 0;
123 static void
124 ip_printts(register const u_char *cp, u_int length)
126 register u_int ptr;
127 register u_int len;
128 int hoplen;
129 const char *type;
131 if (length < 4) {
132 printf("[bad length %d]", length);
133 return;
135 printf(" TS{");
136 hoplen = ((cp[3]&0xF) != IPOPT_TS_TSONLY) ? 8 : 4;
137 if ((length - 4) & (hoplen-1))
138 printf("[bad length %d]", length);
139 ptr = cp[2] - 1;
140 len = 0;
141 if (ptr < 4 || ((ptr - 4) & (hoplen-1)) || ptr > length + 1)
142 printf("[bad ptr %d]", cp[2]);
143 switch (cp[3]&0xF) {
144 case IPOPT_TS_TSONLY:
145 printf("TSONLY");
146 break;
147 case IPOPT_TS_TSANDADDR:
148 printf("TS+ADDR");
149 break;
151 * prespecified should really be 3, but some ones might send 2
152 * instead, and the IPOPT_TS_PRESPEC constant can apparently
153 * have both values, so we have to hard-code it here.
156 case 2:
157 printf("PRESPEC2.0");
158 break;
159 case 3: /* IPOPT_TS_PRESPEC */
160 printf("PRESPEC");
161 break;
162 default:
163 printf("[bad ts type %d]", cp[3]&0xF);
164 goto done;
167 type = " ";
168 for (len = 4; len < length; len += hoplen) {
169 if (ptr == len)
170 type = " ^ ";
171 printf("%s%d@%s", type, EXTRACT_32BITS(&cp[len+hoplen-4]),
172 hoplen!=8 ? "" : ipaddr_string(&cp[len]));
173 type = " ";
176 done:
177 printf("%s", ptr == len ? " ^ " : "");
179 if (cp[3]>>4)
180 printf(" [%d hops not recorded]} ", cp[3]>>4);
181 else
182 printf("}");
186 * print IP options.
188 static void
189 ip_optprint(register const u_char *cp, u_int length)
191 register u_int len;
193 for (; length > 0; cp += len, length -= len) {
194 int tt;
196 TCHECK(*cp);
197 tt = *cp;
198 if (tt == IPOPT_NOP || tt == IPOPT_EOL)
199 len = 1;
200 else {
201 TCHECK(cp[1]);
202 len = cp[1];
203 if (len < 2) {
204 printf("[|ip op len %d]", len);
205 return;
207 TCHECK2(*cp, len);
209 switch (tt) {
211 case IPOPT_EOL:
212 printf(" EOL");
213 if (length > 1)
214 printf("-%d", length - 1);
215 return;
217 case IPOPT_NOP:
218 printf(" NOP");
219 break;
221 case IPOPT_TS:
222 ip_printts(cp, len);
223 break;
225 #ifndef IPOPT_SECURITY
226 #define IPOPT_SECURITY 130
227 #endif /* IPOPT_SECURITY */
228 case IPOPT_SECURITY:
229 printf(" SECURITY{%d}", len);
230 break;
232 case IPOPT_RR:
233 ip_printroute("RR", cp, len);
234 break;
236 case IPOPT_SSRR:
237 ip_printroute("SSRR", cp, len);
238 break;
240 case IPOPT_LSRR:
241 ip_printroute("LSRR", cp, len);
242 break;
244 #ifndef IPOPT_RA
245 #define IPOPT_RA 148 /* router alert */
246 #endif
247 case IPOPT_RA:
248 printf(" RA");
249 if (len != 4)
250 printf("{%d}", len);
251 else {
252 TCHECK(cp[3]);
253 if (cp[2] || cp[3])
254 printf("%d.%d", cp[2], cp[3]);
256 break;
258 default:
259 printf(" IPOPT-%d{%d}", cp[0], len);
260 break;
263 return;
265 trunc:
266 printf("[|ip]");
270 * compute an IP header checksum.
271 * don't modifiy the packet.
273 u_short
274 in_cksum(const u_short *addr, register u_int len, int csum)
276 int nleft = len;
277 const u_short *w = addr;
278 u_short answer;
279 int sum = csum;
282 * Our algorithm is simple, using a 32 bit accumulator (sum),
283 * we add sequential 16 bit words to it, and at the end, fold
284 * back all the carry bits from the top 16 bits into the lower
285 * 16 bits.
287 while (nleft > 1) {
288 sum += *w++;
289 nleft -= 2;
291 if (nleft == 1)
292 sum += htons(*(u_char *)w<<8);
295 * add back carry outs from top 16 bits to low 16 bits
297 sum = (sum >> 16) + (sum & 0xffff); /* add hi 16 to low 16 */
298 sum += (sum >> 16); /* add carry */
299 answer = ~sum; /* truncate to 16 bits */
300 return (answer);
304 * Given the host-byte-order value of the checksum field in a packet
305 * header, and the network-byte-order computed checksum of the data
306 * that the checksum covers (including the checksum itself), compute
307 * what the checksum field *should* have been.
309 u_int16_t
310 in_cksum_shouldbe(u_int16_t sum, u_int16_t computed_sum)
312 u_int32_t shouldbe;
315 * The value that should have gone into the checksum field
316 * is the negative of the value gotten by summing up everything
317 * *but* the checksum field.
319 * We can compute that by subtracting the value of the checksum
320 * field from the sum of all the data in the packet, and then
321 * computing the negative of that value.
323 * "sum" is the value of the checksum field, and "computed_sum"
324 * is the negative of the sum of all the data in the packets,
325 * so that's -(-computed_sum - sum), or (sum + computed_sum).
327 * All the arithmetic in question is one's complement, so the
328 * addition must include an end-around carry; we do this by
329 * doing the arithmetic in 32 bits (with no sign-extension),
330 * and then adding the upper 16 bits of the sum, which contain
331 * the carry, to the lower 16 bits of the sum, and then do it
332 * again in case *that* sum produced a carry.
334 * As RFC 1071 notes, the checksum can be computed without
335 * byte-swapping the 16-bit words; summing 16-bit words
336 * on a big-endian machine gives a big-endian checksum, which
337 * can be directly stuffed into the big-endian checksum fields
338 * in protocol headers, and summing words on a little-endian
339 * machine gives a little-endian checksum, which must be
340 * byte-swapped before being stuffed into a big-endian checksum
341 * field.
343 * "computed_sum" is a network-byte-order value, so we must put
344 * it in host byte order before subtracting it from the
345 * host-byte-order value from the header; the adjusted checksum
346 * will be in host byte order, which is what we'll return.
348 shouldbe = sum;
349 shouldbe += ntohs(computed_sum);
350 shouldbe = (shouldbe & 0xFFFF) + (shouldbe >> 16);
351 shouldbe = (shouldbe & 0xFFFF) + (shouldbe >> 16);
352 return shouldbe;
355 #ifndef IP_MF
356 #define IP_MF 0x2000
357 #endif /* IP_MF */
358 #ifndef IP_DF
359 #define IP_DF 0x4000
360 #endif /* IP_DF */
361 #define IP_RES 0x8000
363 static struct tok ip_frag_values[] = {
364 { IP_MF, "+" },
365 { IP_DF, "DF" },
366 { IP_RES, "rsvd" }, /* The RFC3514 evil ;-) bit */
367 { 0, NULL }
371 * print an IP datagram.
373 void
374 ip_print(register const u_char *bp, register u_int length)
376 register const struct ip *ip;
377 register u_int hlen, len, len0, off;
378 const u_char *ipend;
379 register const u_char *cp;
380 u_char nh;
381 int advance;
382 struct protoent *proto;
383 u_int16_t sum, ip_sum;
385 ip = (const struct ip *)bp;
386 if (IP_V(ip) != 4) { /* print version if != 4 */
387 printf("IP%u ", IP_V(ip));
388 if (IP_V(ip) == 6)
389 printf(", wrong link-layer encapsulation");
391 else
392 printf("IP ");
394 if ((u_char *)(ip + 1) > snapend) {
395 printf("[|ip]");
396 return;
398 if (length < sizeof (struct ip)) {
399 (void)printf("truncated-ip %d", length);
400 return;
402 hlen = IP_HL(ip) * 4;
403 if (hlen < sizeof (struct ip)) {
404 (void)printf("bad-hlen %u", hlen);
405 return;
408 len = EXTRACT_16BITS(&ip->ip_len);
409 if (length < len)
410 (void)printf("truncated-ip - %u bytes missing! ",
411 len - length);
412 if (len < hlen) {
413 (void)printf("bad-len %u", len);
414 return;
418 * Cut off the snapshot length to the end of the IP payload.
420 ipend = bp + len;
421 if (ipend < snapend)
422 snapend = ipend;
424 len -= hlen;
425 len0 = len;
427 off = EXTRACT_16BITS(&ip->ip_off);
429 if (vflag) {
430 (void)printf("(tos 0x%x", (int)ip->ip_tos);
431 /* ECN bits */
432 if (ip->ip_tos & 0x03) {
433 switch (ip->ip_tos & 0x03) {
434 case 1:
435 (void)printf(",ECT(1)");
436 break;
437 case 2:
438 (void)printf(",ECT(0)");
439 break;
440 case 3:
441 (void)printf(",CE");
445 if (ip->ip_ttl >= 1)
446 (void)printf(", ttl %3u", ip->ip_ttl);
449 * for the firewall guys, print id, offset.
450 * On all but the last stick a "+" in the flags portion.
451 * For unfragmented datagrams, note the don't fragment flag.
454 (void)printf(", id %u, offset %u, flags [%s]",
455 EXTRACT_16BITS(&ip->ip_id),
456 (off & 0x1fff) * 8,
457 bittok2str(ip_frag_values, "none", off & 0xe000 ));
459 (void)printf(", length: %u", EXTRACT_16BITS(&ip->ip_len));
461 if ((hlen - sizeof(struct ip)) > 0) {
462 (void)printf(", optlength: %u (", hlen - (u_int)sizeof(struct ip));
463 ip_optprint((u_char *)(ip + 1), hlen - sizeof(struct ip));
464 printf(" )");
467 if ((u_char *)ip + hlen <= snapend) {
468 sum = in_cksum((const u_short *)ip, hlen, 0);
469 if (sum != 0) {
470 ip_sum = EXTRACT_16BITS(&ip->ip_sum);
471 (void)printf(", bad cksum %x (->%x)!", ip_sum,
472 in_cksum_shouldbe(ip_sum, sum));
476 printf(") ");
480 * If this is fragment zero, hand it to the next higher
481 * level protocol.
483 if ((off & 0x1fff) == 0) {
484 cp = (const u_char *)ip + hlen;
485 nh = ip->ip_p;
487 if (nh != IPPROTO_TCP && nh != IPPROTO_UDP &&
488 nh != IPPROTO_SCTP) {
489 (void)printf("%s > %s: ", ipaddr_string(&ip->ip_src),
490 ipaddr_string(&ip->ip_dst));
492 again:
493 switch (nh) {
495 case IPPROTO_AH:
496 nh = *cp;
497 advance = ah_print(cp);
498 if (advance <= 0)
499 break;
500 cp += advance;
501 len -= advance;
502 goto again;
504 case IPPROTO_ESP:
506 int enh, padlen;
507 advance = esp_print(cp, (const u_char *)ip, &enh, &padlen);
508 if (advance <= 0)
509 break;
510 cp += advance;
511 len -= advance + padlen;
512 nh = enh & 0xff;
513 goto again;
516 case IPPROTO_IPCOMP:
518 int enh;
519 advance = ipcomp_print(cp, &enh);
520 if (advance <= 0)
521 break;
522 cp += advance;
523 len -= advance;
524 nh = enh & 0xff;
525 goto again;
528 case IPPROTO_SCTP:
529 sctp_print(cp, (const u_char *)ip, len);
530 break;
532 case IPPROTO_TCP:
533 tcp_print(cp, len, (const u_char *)ip, (off &~ 0x6000));
534 break;
536 case IPPROTO_UDP:
537 udp_print(cp, len, (const u_char *)ip, (off &~ 0x6000));
538 break;
540 case IPPROTO_ICMP:
541 /* pass on the MF bit plus the offset to detect fragments */
542 icmp_print(cp, len, (const u_char *)ip, (off & 0x3fff));
543 break;
545 case IPPROTO_IGRP:
546 igrp_print(cp, len, (const u_char *)ip);
547 break;
549 case IPPROTO_ND:
550 (void)printf(" nd %d", len);
551 break;
553 case IPPROTO_EGP:
554 egp_print(cp);
555 break;
557 case IPPROTO_OSPF:
558 ospf_print(cp, len, (const u_char *)ip);
559 break;
561 case IPPROTO_IGMP:
562 igmp_print(cp, len);
563 break;
565 case IPPROTO_IPV4:
566 /* DVMRP multicast tunnel (ip-in-ip encapsulation) */
567 ip_print(cp, len);
568 if (! vflag) {
569 printf(" (ipip-proto-4)");
570 return;
572 break;
574 #ifdef INET6
575 case IPPROTO_IPV6:
576 /* ip6-in-ip encapsulation */
577 ip6_print(cp, len);
578 break;
579 #endif /*INET6*/
581 case IPPROTO_RSVP:
582 rsvp_print(cp, len);
583 break;
585 case IPPROTO_GRE:
586 /* do it */
587 gre_print(cp, len);
588 break;
590 case IPPROTO_MOBILE:
591 mobile_print(cp, len);
592 break;
594 case IPPROTO_PIM:
595 pim_print(cp, len);
596 break;
598 case IPPROTO_VRRP:
599 vrrp_print(cp, len, ip->ip_ttl);
600 break;
602 default:
603 if ((proto = getprotobynumber(nh)) != NULL)
604 (void)printf(" %s", proto->p_name);
605 else
606 (void)printf(" ip-proto-%d", nh);
607 printf(" %d", len);
608 break;
610 } else {
611 /* Ultra quiet now means that all this stuff should be suppressed */
612 if (qflag > 1) return;
615 * if this isn't the first frag, we're missing the
616 * next level protocol header. print the ip addr
617 * and the protocol.
619 if (off & 0x1fff) {
620 (void)printf("%s > %s:", ipaddr_string(&ip->ip_src),
621 ipaddr_string(&ip->ip_dst));
622 if ((proto = getprotobynumber(ip->ip_p)) != NULL)
623 (void)printf(" %s", proto->p_name);
624 else
625 (void)printf(" ip-proto-%d", ip->ip_p);
630 void
631 ipN_print(register const u_char *bp, register u_int length)
633 struct ip *ip, hdr;
635 ip = (struct ip *)bp;
636 if (length < 4) {
637 (void)printf("truncated-ip %d", length);
638 return;
640 memcpy (&hdr, (char *)ip, 4);
641 switch (IP_V(&hdr)) {
642 case 4:
643 ip_print (bp, length);
644 return;
645 #ifdef INET6
646 case 6:
647 ip6_print (bp, length);
648 return;
649 #endif
650 default:
651 (void)printf("unknown ip %d", IP_V(&hdr));
652 return;