Initial commit
[profile/ivi/tcpdump.git] / 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.52 2007-09-21 07:05:33 hannes 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 "netdissect.h"
40 #include "interface.h"
41 #include "addrtoname.h"
42 #include "extract.h"
43
44 #include "ip6.h"
45 #include "ipproto.h"
46
47 /*
48  * Compute a V6-style checksum by building a pseudoheader.
49  */
50 int
51 nextproto6_cksum(const struct ip6_hdr *ip6, const u_short *data,
52                  u_int len, u_int next_proto)
53 {
54         size_t i;
55         u_int32_t sum = 0;
56         union ip6_pseudo_hdr phu;
57
58         /* pseudo-header */
59         memset(&phu, 0, sizeof(phu));
60         phu.ph.ph_src = ip6->ip6_src;
61         phu.ph.ph_dst = ip6->ip6_dst;
62         phu.ph.ph_len = htonl(len);
63         phu.ph.ph_nxt = next_proto;
64
65         for (i = 0; i < sizeof(phu.pa) / sizeof(phu.pa[0]); i++) {
66                 sum += phu.pa[i];
67         }
68
69         return in_cksum(data, len, sum);
70 }
71
72 /*
73  * print an IP6 datagram.
74  */
75 void
76 ip6_print(register const u_char *bp, register u_int length)
77 {
78         register const struct ip6_hdr *ip6;
79         register int advance;
80         u_int len;
81         const u_char *ipend;
82         register const u_char *cp;
83         register u_int payload_len;
84         int nh;
85         int fragmented = 0;
86         u_int flow;
87
88         ip6 = (const struct ip6_hdr *)bp;
89
90         TCHECK(*ip6);
91         if (length < sizeof (struct ip6_hdr)) {
92                 (void)printf("truncated-ip6 %u", length);
93                 return;
94         }
95
96         if (!eflag)
97             printf("IP6 ");
98
99         payload_len = EXTRACT_16BITS(&ip6->ip6_plen);
100         len = payload_len + sizeof(struct ip6_hdr);
101         if (length < len)
102                 (void)printf("truncated-ip6 - %u bytes missing!",
103                         len - length);
104
105         if (vflag) {
106             flow = EXTRACT_32BITS(&ip6->ip6_flow);
107             printf("(");
108 #if 0
109             /* rfc1883 */
110             if (flow & 0x0f000000)
111                 (void)printf("pri 0x%02x, ", (flow & 0x0f000000) >> 24);
112             if (flow & 0x00ffffff)
113                 (void)printf("flowlabel 0x%06x, ", flow & 0x00ffffff);
114 #else
115             /* RFC 2460 */
116             if (flow & 0x0ff00000)
117                 (void)printf("class 0x%02x, ", (flow & 0x0ff00000) >> 20);
118             if (flow & 0x000fffff)
119                 (void)printf("flowlabel 0x%05x, ", flow & 0x000fffff);
120 #endif
121
122             (void)printf("hlim %u, next-header %s (%u) payload length: %u) ",
123                          ip6->ip6_hlim,
124                          tok2str(ipproto_values,"unknown",ip6->ip6_nxt),
125                          ip6->ip6_nxt,
126                          payload_len);
127         }
128
129         /*
130          * Cut off the snapshot length to the end of the IP payload.
131          */
132         ipend = bp + len;
133         if (ipend < snapend)
134                 snapend = ipend;
135
136         cp = (const u_char *)ip6;
137         advance = sizeof(struct ip6_hdr);
138         nh = ip6->ip6_nxt;
139         while (cp < snapend && advance > 0) {
140                 cp += advance;
141                 len -= advance;
142
143                 if (cp == (const u_char *)(ip6 + 1) &&
144                     nh != IPPROTO_TCP && nh != IPPROTO_UDP &&
145                     nh != IPPROTO_DCCP && nh != IPPROTO_SCTP) {
146                         (void)printf("%s > %s: ", ip6addr_string(&ip6->ip6_src),
147                                      ip6addr_string(&ip6->ip6_dst));
148                 }
149
150                 switch (nh) {
151                 case IPPROTO_HOPOPTS:
152                         advance = hbhopt_print(cp);
153                         nh = *cp;
154                         break;
155                 case IPPROTO_DSTOPTS:
156                         advance = dstopt_print(cp);
157                         nh = *cp;
158                         break;
159                 case IPPROTO_FRAGMENT:
160                         advance = frag6_print(cp, (const u_char *)ip6);
161                         if (snapend <= cp + advance)
162                                 return;
163                         nh = *cp;
164                         fragmented = 1;
165                         break;
166
167                 case IPPROTO_MOBILITY_OLD:
168                 case IPPROTO_MOBILITY:
169                         /*
170                          * XXX - we don't use "advance"; the current
171                          * "Mobility Support in IPv6" draft
172                          * (draft-ietf-mobileip-ipv6-24) says that
173                          * the next header field in a mobility header
174                          * should be IPPROTO_NONE, but speaks of
175                          * the possiblity of a future extension in
176                          * which payload can be piggybacked atop a
177                          * mobility header.
178                          */
179                         advance = mobility_print(cp, (const u_char *)ip6);
180                         nh = *cp;
181                         return;
182                 case IPPROTO_ROUTING:
183                         advance = rt6_print(cp, (const u_char *)ip6);
184                         nh = *cp;
185                         break;
186                 case IPPROTO_SCTP:
187                         sctp_print(cp, (const u_char *)ip6, len);
188                         return;
189                 case IPPROTO_DCCP:
190                         dccp_print(cp, (const u_char *)ip6, len);
191                         return;
192                 case IPPROTO_TCP:
193                         tcp_print(cp, len, (const u_char *)ip6, fragmented);
194                         return;
195                 case IPPROTO_UDP:
196                         udp_print(cp, len, (const u_char *)ip6, fragmented);
197                         return;
198                 case IPPROTO_ICMPV6:
199                         icmp6_print(gndo, cp, len, (const u_char *)ip6, fragmented);
200                         return;
201                 case IPPROTO_AH:
202                         advance = ah_print(cp);
203                         nh = *cp;
204                         break;
205                 case IPPROTO_ESP:
206                     {
207                         int enh, padlen;
208                         advance = esp_print(gndo, cp, len, (const u_char *)ip6, &enh, &padlen);
209                         nh = enh & 0xff;
210                         len -= padlen;
211                         break;
212                     }
213                 case IPPROTO_IPCOMP:
214                     {
215                         int enh;
216                         advance = ipcomp_print(cp, &enh);
217                         nh = enh & 0xff;
218                         break;
219                     }
220
221                 case IPPROTO_PIM:
222                         pim_print(cp, len, nextproto6_cksum(ip6, (u_short *)cp, len,
223                                                             IPPROTO_PIM));
224                         return;
225
226                 case IPPROTO_OSPF:
227                         ospf6_print(cp, len);
228                         return;
229
230                 case IPPROTO_IPV6:
231                         ip6_print(cp, len);
232                         return;
233
234                 case IPPROTO_IPV4:
235                         ip_print(gndo, cp, len);
236                         return;
237
238                 case IPPROTO_PGM:
239                         pgm_print(cp, len, (const u_char *)ip6);
240                         return;
241
242                 case IPPROTO_GRE:
243                         gre_print(cp, len);
244                         return;
245
246                 case IPPROTO_RSVP:
247                         rsvp_print(cp, len);
248                         return;
249
250                 case IPPROTO_NONE:
251                         (void)printf("no next header");
252                         return;
253
254                 default:
255                         (void)printf("ip-proto-%d %d", nh, len);
256                         return;
257                 }
258         }
259
260         return;
261 trunc:
262         (void)printf("[|ip6]");
263 }
264
265 #endif /* INET6 */