Merge from vendor branch BSDTAR:
[dragonfly.git] / contrib / tcpdump-3.8.3 / 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.128.2.6 2004/03/24 09:01:39 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 /*
45  * print the recorded route in an IP RR, LSRR or SSRR option.
46  */
47 static void
48 ip_printroute(const char *type, register const u_char *cp, u_int length)
49 {
50         register u_int ptr;
51         register u_int len;
52
53         if (length < 3) {
54                 printf(" [bad length %u]", length);
55                 return;
56         }
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]);
63
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 = " ";
70         }
71         printf("%s}", ptr == len? "#" : "");
72 }
73
74 /*
75  * If source-routing is present, return the final destination.
76  * Otherwise, return IP destination.
77  *
78  * This is used for UDP and TCP pseudo-header in the checksum
79  * calculation.
80  */
81 u_int32_t
82 ip_finddst(const struct ip *ip)
83 {
84         int length;
85         int len;
86         const u_char *cp;
87         u_int32_t retval;
88
89         cp = (const u_char *)(ip + 1);
90         length = (IP_HL(ip) << 2) - sizeof(struct ip);
91
92         for (; length > 0; cp += len, length -= len) {
93                 int tt;
94
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];
102                 }
103                 if (len < 2) {
104                         return 0;
105                 }
106                 TCHECK2(*cp, len);
107                 switch (tt) {
108
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;
115                 }
116         }
117         return ip->ip_dst.s_addr;
118
119 trunc:
120         return 0;
121 }
122
123 static void
124 ip_printts(register const u_char *cp, u_int length)
125 {
126         register u_int ptr;
127         register u_int len;
128         int hoplen;
129         const char *type;
130
131         if (length < 4) {
132                 printf("[bad length %d]", length);
133                 return;
134         }
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;
150         /*
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.
154          */
155
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;
165         }
166
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 = " ";
174         }
175
176 done:
177         printf("%s", ptr == len ? " ^ " : "");
178
179         if (cp[3]>>4)
180                 printf(" [%d hops not recorded]} ", cp[3]>>4);
181         else
182                 printf("}");
183 }
184
185 /*
186  * print IP options.
187  */
188 static void
189 ip_optprint(register const u_char *cp, u_int length)
190 {
191         register u_int len;
192
193         for (; length > 0; cp += len, length -= len) {
194                 int tt;
195
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;
206                         }
207                         TCHECK2(*cp, len);
208                 }
209                 switch (tt) {
210
211                 case IPOPT_EOL:
212                         printf(" EOL");
213                         if (length > 1)
214                                 printf("-%d", length - 1);
215                         return;
216
217                 case IPOPT_NOP:
218                         printf(" NOP");
219                         break;
220
221                 case IPOPT_TS:
222                         ip_printts(cp, len);
223                         break;
224
225 #ifndef IPOPT_SECURITY
226 #define IPOPT_SECURITY 130
227 #endif /* IPOPT_SECURITY */
228                 case IPOPT_SECURITY:
229                         printf(" SECURITY{%d}", len);
230                         break;
231
232                 case IPOPT_RR:
233                         ip_printroute("RR", cp, len);
234                         break;
235
236                 case IPOPT_SSRR:
237                         ip_printroute("SSRR", cp, len);
238                         break;
239
240                 case IPOPT_LSRR:
241                         ip_printroute("LSRR", cp, len);
242                         break;
243
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]);
255                         }
256                         break;
257
258                 default:
259                         printf(" IPOPT-%d{%d}", cp[0], len);
260                         break;
261                 }
262         }
263         return;
264
265 trunc:
266         printf("[|ip]");
267 }
268
269 /*
270  * compute an IP header checksum.
271  * don't modifiy the packet.
272  */
273 u_short
274 in_cksum(const u_short *addr, register u_int len, int csum)
275 {
276         int nleft = len;
277         const u_short *w = addr;
278         u_short answer;
279         int sum = csum;
280
281         /*
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.
286          */
287         while (nleft > 1)  {
288                 sum += *w++;
289                 nleft -= 2;
290         }
291         if (nleft == 1)
292                 sum += htons(*(u_char *)w<<8);
293
294         /*
295          * add back carry outs from top 16 bits to low 16 bits
296          */
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);
301 }
302
303 /*
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.
308  */
309 u_int16_t
310 in_cksum_shouldbe(u_int16_t sum, u_int16_t computed_sum)
311 {
312         u_int32_t shouldbe;
313
314         /*
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.
318          *
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.
322          *
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).
326          *
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.
333          *
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.
342          *
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.
347          */
348         shouldbe = sum;
349         shouldbe += ntohs(computed_sum);
350         shouldbe = (shouldbe & 0xFFFF) + (shouldbe >> 16);
351         shouldbe = (shouldbe & 0xFFFF) + (shouldbe >> 16);
352         return shouldbe;
353 }
354
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
362
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 }
368 };
369
370 /*
371  * print an IP datagram.
372  */
373 void
374 ip_print(register const u_char *bp, register u_int length)
375 {
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;
384
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");
390         }
391         else
392             printf("IP ");
393
394         if ((u_char *)(ip + 1) > snapend) {
395                 printf("[|ip]");
396                 return;
397         }
398         if (length < sizeof (struct ip)) {
399                 (void)printf("truncated-ip %d", length);
400                 return;
401         }
402         hlen = IP_HL(ip) * 4;
403         if (hlen < sizeof (struct ip)) {
404                 (void)printf("bad-hlen %u", hlen);
405                 return;
406         }
407
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;
415         }
416
417         /*
418          * Cut off the snapshot length to the end of the IP payload.
419          */
420         ipend = bp + len;
421         if (ipend < snapend)
422                 snapend = ipend;
423
424         len -= hlen;
425         len0 = len;
426
427         off = EXTRACT_16BITS(&ip->ip_off);
428
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");
442                 }
443             }
444
445             if (ip->ip_ttl >= 1)
446                 (void)printf(", ttl %3u", ip->ip_ttl);    
447
448             /*
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.
452              */
453
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 ));
458
459             (void)printf(", length: %u", EXTRACT_16BITS(&ip->ip_len));
460
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(" )");
465             }
466
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));
473                 }
474             }
475
476             printf(") ");
477         }
478
479         /*
480          * If this is fragment zero, hand it to the next higher
481          * level protocol.
482          */
483         if ((off & 0x1fff) == 0) {
484                 cp = (const u_char *)ip + hlen;
485                 nh = ip->ip_p;
486
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));
491                 }
492 again:
493                 switch (nh) {
494
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;
503
504                 case IPPROTO_ESP:
505                     {
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;
514                     }
515
516                 case IPPROTO_IPCOMP:
517                     {
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;
526                     }
527
528                 case IPPROTO_SCTP:
529                         sctp_print(cp, (const u_char *)ip, len);
530                         break;
531
532                 case IPPROTO_TCP:
533                         tcp_print(cp, len, (const u_char *)ip, (off &~ 0x6000));
534                         break;
535
536                 case IPPROTO_UDP:
537                         udp_print(cp, len, (const u_char *)ip, (off &~ 0x6000));
538                         break;
539
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;
544
545                 case IPPROTO_IGRP:
546                         igrp_print(cp, len, (const u_char *)ip);
547                         break;
548
549                 case IPPROTO_ND:
550                         (void)printf(" nd %d", len);
551                         break;
552
553                 case IPPROTO_EGP:
554                         egp_print(cp);
555                         break;
556
557                 case IPPROTO_OSPF:
558                         ospf_print(cp, len, (const u_char *)ip);
559                         break;
560
561                 case IPPROTO_IGMP:
562                         igmp_print(cp, len);
563                         break;
564
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;
571                         }
572                         break;
573
574 #ifdef INET6
575                 case IPPROTO_IPV6:
576                         /* ip6-in-ip encapsulation */
577                         ip6_print(cp, len);
578                         break;
579 #endif /*INET6*/
580
581                 case IPPROTO_RSVP:
582                         rsvp_print(cp, len);
583                         break;
584
585                 case IPPROTO_GRE:
586                         /* do it */
587                         gre_print(cp, len);
588                         break;
589
590                 case IPPROTO_MOBILE:
591                         mobile_print(cp, len);
592                         break;
593
594                 case IPPROTO_PIM:
595                         pim_print(cp, len);
596                         break;
597
598                 case IPPROTO_VRRP:
599                         vrrp_print(cp, len, ip->ip_ttl);
600                         break;
601
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;
609                 }
610         } else {
611             /* Ultra quiet now means that all this stuff should be suppressed */
612             if (qflag > 1) return;
613
614             /*
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.
618              */
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);
626             } 
627         }
628 }
629
630 void
631 ipN_print(register const u_char *bp, register u_int length)
632 {
633         struct ip *ip, hdr;
634
635         ip = (struct ip *)bp;
636         if (length < 4) {
637                 (void)printf("truncated-ip %d", length);
638                 return;
639         }
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;
653         }
654 }
655
656
657