]> The Tcpdump Group git mirrors - tcpdump/blob - print-ip6.c
verify/warn icmpv6 checksum. from jinmei@kame
[tcpdump] / 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[] =
24 "@(#) $Header: /tcpdump/master/tcpdump/print-ip6.c,v 1.29 2003-05-02 08:13:55 itojun 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
45 /*
46 * print an IP6 datagram.
47 */
48 void
49 ip6_print(register const u_char *bp, register u_int length)
50 {
51 register const struct ip6_hdr *ip6;
52 register int advance;
53 register u_int len;
54 register const u_char *cp;
55 int nh;
56 int fragmented = 0;
57 u_int flow;
58
59 ip6 = (const struct ip6_hdr *)bp;
60
61 TCHECK(*ip6);
62 if (length < sizeof (struct ip6_hdr)) {
63 (void)printf("truncated-ip6 %d", length);
64 return;
65 }
66 advance = sizeof(struct ip6_hdr);
67
68 len = EXTRACT_16BITS(&ip6->ip6_plen);
69 if (length < len + advance)
70 (void)printf("truncated-ip6 - %d bytes missing!",
71 len + advance - length);
72
73 cp = (const u_char *)ip6;
74 nh = ip6->ip6_nxt;
75 while (cp < snapend) {
76 cp += advance;
77
78 if (cp == (const u_char *)(ip6 + 1)
79 && nh != IPPROTO_TCP && nh != IPPROTO_UDP) {
80 (void)printf("%s > %s: ", ip6addr_string(&ip6->ip6_src),
81 ip6addr_string(&ip6->ip6_dst));
82 }
83
84 switch (nh) {
85 case IPPROTO_HOPOPTS:
86 advance = hbhopt_print(cp);
87 nh = *cp;
88 break;
89 case IPPROTO_DSTOPTS:
90 advance = dstopt_print(cp);
91 nh = *cp;
92 break;
93 case IPPROTO_FRAGMENT:
94 advance = frag6_print(cp, (const u_char *)ip6);
95 if (snapend <= cp + advance)
96 goto end;
97 nh = *cp;
98 fragmented = 1;
99 break;
100 #ifndef IPPROTO_MOBILITY
101 #define IPPROTO_MOBILITY 62
102 #endif
103 case IPPROTO_MOBILITY:
104 /*
105 * XXX - we don't use "advance"; is this
106 * header always a final header?
107 */
108 advance = mobility_print(cp, (const u_char *)ip6);
109 nh = *cp;
110 goto end;
111 case IPPROTO_ROUTING:
112 advance = rt6_print(cp, (const u_char *)ip6);
113 nh = *cp;
114 break;
115 case IPPROTO_TCP:
116 tcp_print(cp, len + sizeof(struct ip6_hdr) - (cp - bp),
117 (const u_char *)ip6, fragmented);
118 goto end;
119 case IPPROTO_UDP:
120 udp_print(cp, len + sizeof(struct ip6_hdr) - (cp - bp),
121 (const u_char *)ip6, fragmented);
122 goto end;
123 case IPPROTO_ICMPV6:
124 icmp6_print(cp, (const u_char *)ip6, fragmented);
125 goto end;
126 case IPPROTO_AH:
127 advance = ah_print(cp);
128 nh = *cp;
129 break;
130 case IPPROTO_ESP:
131 {
132 int enh, padlen;
133 advance = esp_print(cp, (const u_char *)ip6, &enh, &padlen);
134 if (enh < 0)
135 goto end;
136 nh = enh & 0xff;
137 len -= padlen;
138 break;
139 }
140 #ifndef IPPROTO_IPCOMP
141 #define IPPROTO_IPCOMP 108
142 #endif
143 case IPPROTO_IPCOMP:
144 {
145 int enh;
146 advance = ipcomp_print(cp, &enh);
147 if (enh < 0)
148 goto end;
149 nh = enh & 0xff;
150 break;
151 }
152
153 #ifndef IPPROTO_PIM
154 #define IPPROTO_PIM 103
155 #endif
156 case IPPROTO_PIM:
157 pim_print(cp, len);
158 goto end;
159 #ifndef IPPROTO_OSPF
160 #define IPPROTO_OSPF 89
161 #endif
162 case IPPROTO_OSPF:
163 ospf6_print(cp, len);
164 goto end;
165 case IPPROTO_IPV6:
166 ip6_print(cp, len);
167 goto end;
168 #ifndef IPPROTO_IPV4
169 #define IPPROTO_IPV4 4
170 #endif
171 case IPPROTO_IPV4:
172 ip_print(cp, len);
173 goto end;
174 case IPPROTO_NONE:
175 (void)printf("no next header");
176 goto end;
177
178 default:
179 (void)printf("ip-proto-%d %d", ip6->ip6_nxt, len);
180 goto end;
181 }
182 }
183
184 end:
185
186 flow = EXTRACT_32BITS(&ip6->ip6_flow);
187 #if 0
188 /* rfc1883 */
189 if (flow & 0x0f000000)
190 (void)printf(" [pri 0x%x]", (flow & 0x0f000000) >> 24);
191 if (flow & 0x00ffffff)
192 (void)printf(" [flowlabel 0x%x]", flow & 0x00ffffff);
193 #else
194 /* RFC 2460 */
195 if (flow & 0x0ff00000)
196 (void)printf(" [class 0x%x]", (flow & 0x0ff00000) >> 20);
197 if (flow & 0x000fffff)
198 (void)printf(" [flowlabel 0x%x]", flow & 0x000fffff);
199 #endif
200
201 if (ip6->ip6_hlim <= 1)
202 (void)printf(" [hlim %d]", (int)ip6->ip6_hlim);
203
204 if (vflag) {
205 printf(" (");
206 (void)printf("len %d", len);
207 if (ip6->ip6_hlim > 1)
208 (void)printf(", hlim %d", (int)ip6->ip6_hlim);
209 printf(")");
210 }
211 return;
212 trunc:
213 (void)printf("[|ip6]");
214 }
215
216 #endif /* INET6 */