]> The Tcpdump Group git mirrors - tcpdump/blob - print-ip6opts.c
updated version
[tcpdump] / print-ip6opts.c
1 /*
2 * Copyright (C) 1998 WIDE Project.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of the project nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 */
29
30 #ifdef HAVE_CONFIG_H
31 #include "config.h"
32 #endif
33
34 #include <netdissect-stdinc.h>
35
36 #include "ip6.h"
37
38 #include "netdissect.h"
39 #include "addrtoname.h"
40 #include "extract.h"
41
42 static void
43 ip6_sopt_print(netdissect_options *ndo, const u_char *bp, int len)
44 {
45 int i;
46 int optlen;
47
48 for (i = 0; i < len; i += optlen) {
49 if (bp[i] == IP6OPT_PAD1)
50 optlen = 1;
51 else {
52 if (i + 1 < len)
53 optlen = bp[i + 1] + 2;
54 else
55 goto trunc;
56 }
57 if (i + optlen > len)
58 goto trunc;
59
60 switch (bp[i]) {
61 case IP6OPT_PAD1:
62 ND_PRINT((ndo, ", pad1"));
63 break;
64 case IP6OPT_PADN:
65 if (len - i < IP6OPT_MINLEN) {
66 ND_PRINT((ndo, ", padn: trunc"));
67 goto trunc;
68 }
69 ND_PRINT((ndo, ", padn"));
70 break;
71 default:
72 if (len - i < IP6OPT_MINLEN) {
73 ND_PRINT((ndo, ", sopt_type %d: trunc)", bp[i]));
74 goto trunc;
75 }
76 ND_PRINT((ndo, ", sopt_type 0x%02x: len=%d", bp[i], bp[i + 1]));
77 break;
78 }
79 }
80 return;
81
82 trunc:
83 ND_PRINT((ndo, "[trunc] "));
84 }
85
86 static void
87 ip6_opt_print(netdissect_options *ndo, const u_char *bp, int len)
88 {
89 int i;
90 int optlen = 0;
91
92 if (len == 0)
93 return;
94 for (i = 0; i < len; i += optlen) {
95 if (bp[i] == IP6OPT_PAD1)
96 optlen = 1;
97 else {
98 if (i + 1 < len)
99 optlen = bp[i + 1] + 2;
100 else
101 goto trunc;
102 }
103 if (i + optlen > len)
104 goto trunc;
105
106 switch (bp[i]) {
107 case IP6OPT_PAD1:
108 ND_PRINT((ndo, "(pad1)"));
109 break;
110 case IP6OPT_PADN:
111 if (len - i < IP6OPT_MINLEN) {
112 ND_PRINT((ndo, "(padn: trunc)"));
113 goto trunc;
114 }
115 ND_PRINT((ndo, "(padn)"));
116 break;
117 case IP6OPT_ROUTER_ALERT:
118 if (len - i < IP6OPT_RTALERT_LEN) {
119 ND_PRINT((ndo, "(rtalert: trunc)"));
120 goto trunc;
121 }
122 if (bp[i + 1] != IP6OPT_RTALERT_LEN - 2) {
123 ND_PRINT((ndo, "(rtalert: invalid len %d)", bp[i + 1]));
124 goto trunc;
125 }
126 ND_PRINT((ndo, "(rtalert: 0x%04x) ", EXTRACT_16BITS(&bp[i + 2])));
127 break;
128 case IP6OPT_JUMBO:
129 if (len - i < IP6OPT_JUMBO_LEN) {
130 ND_PRINT((ndo, "(jumbo: trunc)"));
131 goto trunc;
132 }
133 if (bp[i + 1] != IP6OPT_JUMBO_LEN - 2) {
134 ND_PRINT((ndo, "(jumbo: invalid len %d)", bp[i + 1]));
135 goto trunc;
136 }
137 ND_PRINT((ndo, "(jumbo: %u) ", EXTRACT_32BITS(&bp[i + 2])));
138 break;
139 case IP6OPT_HOME_ADDRESS:
140 if (len - i < IP6OPT_HOMEADDR_MINLEN) {
141 ND_PRINT((ndo, "(homeaddr: trunc)"));
142 goto trunc;
143 }
144 if (bp[i + 1] < IP6OPT_HOMEADDR_MINLEN - 2) {
145 ND_PRINT((ndo, "(homeaddr: invalid len %d)", bp[i + 1]));
146 goto trunc;
147 }
148 ND_PRINT((ndo, "(homeaddr: %s", ip6addr_string(ndo, &bp[i + 2])));
149 if (bp[i + 1] > IP6OPT_HOMEADDR_MINLEN - 2) {
150 ip6_sopt_print(ndo, &bp[i + IP6OPT_HOMEADDR_MINLEN],
151 (optlen - IP6OPT_HOMEADDR_MINLEN));
152 }
153 ND_PRINT((ndo, ")"));
154 break;
155 default:
156 if (len - i < IP6OPT_MINLEN) {
157 ND_PRINT((ndo, "(type %d: trunc)", bp[i]));
158 goto trunc;
159 }
160 ND_PRINT((ndo, "(opt_type 0x%02x: len=%d)", bp[i], bp[i + 1]));
161 break;
162 }
163 }
164 ND_PRINT((ndo, " "));
165 return;
166
167 trunc:
168 ND_PRINT((ndo, "[trunc] "));
169 }
170
171 int
172 hbhopt_print(netdissect_options *ndo, register const u_char *bp)
173 {
174 const struct ip6_hbh *dp = (const struct ip6_hbh *)bp;
175 int hbhlen = 0;
176
177 ND_TCHECK(dp->ip6h_len);
178 hbhlen = (int)((dp->ip6h_len + 1) << 3);
179 ND_TCHECK2(*dp, hbhlen);
180 ND_PRINT((ndo, "HBH "));
181 if (ndo->ndo_vflag)
182 ip6_opt_print(ndo, (const u_char *)dp + sizeof(*dp), hbhlen - sizeof(*dp));
183
184 return(hbhlen);
185
186 trunc:
187 ND_PRINT((ndo, "[|HBH]"));
188 return(-1);
189 }
190
191 int
192 dstopt_print(netdissect_options *ndo, register const u_char *bp)
193 {
194 const struct ip6_dest *dp = (const struct ip6_dest *)bp;
195 int dstoptlen = 0;
196
197 ND_TCHECK(dp->ip6d_len);
198 dstoptlen = (int)((dp->ip6d_len + 1) << 3);
199 ND_TCHECK2(*dp, dstoptlen);
200 ND_PRINT((ndo, "DSTOPT "));
201 if (ndo->ndo_vflag) {
202 ip6_opt_print(ndo, (const u_char *)dp + sizeof(*dp),
203 dstoptlen - sizeof(*dp));
204 }
205
206 return(dstoptlen);
207
208 trunc:
209 ND_PRINT((ndo, "[|DSTOPT]"));
210 return(-1);
211 }