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