Fix warning:
[dragonfly.git] / contrib / tcpdump-3.8.3 / print-ip6.c
1 /*
2  * Copyright (c) 1988, 1989, 1990, 1991, 1992, 1993, 1994
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-ip6.c,v 1.32.2.8 2003/11/24 20:31:22 guy Exp $";
25 #endif
26
27 #ifdef HAVE_CONFIG_H
28 #include "config.h"
29 #endif
30
31 #ifdef INET6
32
33 #include <tcpdump-stdinc.h>
34
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <string.h>
38
39 #include "interface.h"
40 #include "addrtoname.h"
41 #include "extract.h"
42
43 #include "ip6.h"
44 #include "ipproto.h"
45
46 /*
47  * print an IP6 datagram.
48  */
49 void
50 ip6_print(register const u_char *bp, register u_int length)
51 {
52         register const struct ip6_hdr *ip6;
53         register int advance;
54         u_int len;
55         const u_char *ipend;
56         register const u_char *cp;
57         register u_int payload_len;
58         int nh;
59         int fragmented = 0;
60         u_int flow;
61
62         ip6 = (const struct ip6_hdr *)bp;
63
64         TCHECK(*ip6);
65         if (length < sizeof (struct ip6_hdr)) {
66                 (void)printf("truncated-ip6 %d", length);
67                 return;
68         }
69
70         payload_len = EXTRACT_16BITS(&ip6->ip6_plen);
71         len = payload_len + sizeof(struct ip6_hdr);
72         if (length < len)
73                 (void)printf("truncated-ip6 - %d bytes missing!",
74                         len - length);
75
76         /*
77          * Cut off the snapshot length to the end of the IP payload.
78          */
79         ipend = bp + len;
80         if (ipend < snapend)
81                 snapend = ipend;
82
83         cp = (const u_char *)ip6;
84         advance = sizeof(struct ip6_hdr);
85         nh = ip6->ip6_nxt;
86         while (cp < snapend && advance > 0) {
87                 cp += advance;
88                 len -= advance;
89
90                 if (cp == (const u_char *)(ip6 + 1) &&
91                     nh != IPPROTO_TCP && nh != IPPROTO_UDP &&
92                     nh != IPPROTO_SCTP) {
93                         (void)printf("%s > %s: ", ip6addr_string(&ip6->ip6_src),
94                                      ip6addr_string(&ip6->ip6_dst));
95                 }
96
97                 switch (nh) {
98                 case IPPROTO_HOPOPTS:
99                         advance = hbhopt_print(cp);
100                         nh = *cp;
101                         break;
102                 case IPPROTO_DSTOPTS:
103                         advance = dstopt_print(cp);
104                         nh = *cp;
105                         break;
106                 case IPPROTO_FRAGMENT:
107                         advance = frag6_print(cp, (const u_char *)ip6);
108                         if (snapend <= cp + advance)
109                                 goto end;
110                         nh = *cp;
111                         fragmented = 1;
112                         break;
113
114                 case IPPROTO_MOBILITY_OLD:
115                 case IPPROTO_MOBILITY:
116                         /*
117                          * XXX - we don't use "advance"; the current
118                          * "Mobility Support in IPv6" draft
119                          * (draft-ietf-mobileip-ipv6-24) says that
120                          * the next header field in a mobility header
121                          * should be IPPROTO_NONE, but speaks of
122                          * the possiblity of a future extension in
123                          * which payload can be piggybacked atop a
124                          * mobility header.
125                          */
126                         advance = mobility_print(cp, (const u_char *)ip6);
127                         nh = *cp;
128                         goto end;
129                 case IPPROTO_ROUTING:
130                         advance = rt6_print(cp, (const u_char *)ip6);
131                         nh = *cp;
132                         break;
133                 case IPPROTO_SCTP:
134                         sctp_print(cp, (const u_char *)ip6, len);
135                         goto end;
136                 case IPPROTO_TCP:
137                         tcp_print(cp, len, (const u_char *)ip6, fragmented);
138                         goto end;
139                 case IPPROTO_UDP:
140                         udp_print(cp, len, (const u_char *)ip6, fragmented);
141                         goto end;
142                 case IPPROTO_ICMPV6:
143                         icmp6_print(cp, len, (const u_char *)ip6, fragmented);
144                         goto end;
145                 case IPPROTO_AH:
146                         advance = ah_print(cp);
147                         nh = *cp;
148                         break;
149                 case IPPROTO_ESP:
150                     {
151                         int enh, padlen;
152                         advance = esp_print(cp, (const u_char *)ip6, &enh, &padlen);
153                         nh = enh & 0xff;
154                         len -= padlen;
155                         break;
156                     }
157                 case IPPROTO_IPCOMP:
158                     {
159                         int enh;
160                         advance = ipcomp_print(cp, &enh);
161                         nh = enh & 0xff;
162                         break;
163                     }
164
165                 case IPPROTO_PIM:
166                         pim_print(cp, len);
167                         goto end;
168                 case IPPROTO_OSPF:
169                         ospf6_print(cp, len);
170                         goto end;
171
172                 case IPPROTO_IPV6:
173                         ip6_print(cp, len);
174                         goto end;
175
176                 case IPPROTO_IPV4:
177                         ip_print(cp, len);
178                         goto end;
179
180                 case IPPROTO_NONE:
181                         (void)printf("no next header");
182                         goto end;
183
184                 default:
185                         (void)printf("ip-proto-%d %d", ip6->ip6_nxt, len);
186                         goto end;
187                 }
188         }
189
190  end:
191
192         flow = EXTRACT_32BITS(&ip6->ip6_flow);
193 #if 0
194         /* rfc1883 */
195         if (flow & 0x0f000000)
196                 (void)printf(" [pri 0x%x]", (flow & 0x0f000000) >> 24);
197         if (flow & 0x00ffffff)
198                 (void)printf(" [flowlabel 0x%x]", flow & 0x00ffffff);
199 #else
200         /* RFC 2460 */
201         if (flow & 0x0ff00000)
202                 (void)printf(" [class 0x%x]", (flow & 0x0ff00000) >> 20);
203         if (flow & 0x000fffff)
204                 (void)printf(" [flowlabel 0x%x]", flow & 0x000fffff);
205 #endif
206
207         if (ip6->ip6_hlim <= 1)
208                 (void)printf(" [hlim %u]", ip6->ip6_hlim);
209
210         if (vflag) {
211                 printf(" (");
212                 (void)printf("len %u", payload_len);
213                 if (ip6->ip6_hlim > 1)
214                         (void)printf(", hlim %d", (int)ip6->ip6_hlim);
215                 printf(")");
216         }
217         return;
218 trunc:
219         (void)printf("[|ip6]");
220 }
221
222 #endif /* INET6 */