]> git.saurik.com Git - apple/libinfo.git/blob - gen.subproj/ip6opt.c
Libinfo-129.tar.gz
[apple/libinfo.git] / gen.subproj / ip6opt.c
1 /*
2 * Copyright (C) 1995, 1996, 1997, and 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 * $FreeBSD: src/lib/libc/net/ip6opt.c,v 1.1 1999/12/16 18:32:01 shin Exp $
30 */
31
32 #include <sys/param.h>
33 #include <sys/types.h>
34 #include <sys/socket.h>
35
36 #include <netinet/in.h>
37 #include <netinet/ip6.h>
38
39 #include <string.h>
40 #include <stdio.h>
41
42 static int ip6optlen(u_int8_t *opt, u_int8_t *lim);
43 static void inet6_insert_padopt(u_char *p, int len);
44
45 /*
46 * This function returns the number of bytes required to hold an option
47 * when it is stored as ancillary data, including the cmsghdr structure
48 * at the beginning, and any padding at the end (to make its size a
49 * multiple of 8 bytes). The argument is the size of the structure
50 * defining the option, which must include any pad bytes at the
51 * beginning (the value y in the alignment term "xn + y"), the type
52 * byte, the length byte, and the option data.
53 */
54 int
55 inet6_option_space(nbytes)
56 int nbytes;
57 {
58 nbytes += 2; /* we need space for nxt-hdr and length fields */
59 return(CMSG_SPACE((nbytes + 7) & ~7));
60 }
61
62 /*
63 * This function is called once per ancillary data object that will
64 * contain either Hop-by-Hop or Destination options. It returns 0 on
65 * success or -1 on an error.
66 */
67 int
68 inet6_option_init(bp, cmsgp, type)
69 void *bp;
70 struct cmsghdr **cmsgp;
71 int type;
72 {
73 register struct cmsghdr *ch = (struct cmsghdr *)bp;
74
75 /* argument validation */
76 if (type != IPV6_HOPOPTS && type != IPV6_DSTOPTS)
77 return(-1);
78
79 ch->cmsg_level = IPPROTO_IPV6;
80 ch->cmsg_type = type;
81 ch->cmsg_len = CMSG_LEN(0);
82
83 *cmsgp = ch;
84 return(0);
85 }
86
87 /*
88 * This function appends a Hop-by-Hop option or a Destination option
89 * into an ancillary data object that has been initialized by
90 * inet6_option_init(). This function returns 0 if it succeeds or -1 on
91 * an error.
92 * multx is the value x in the alignment term "xn + y" described
93 * earlier. It must have a value of 1, 2, 4, or 8.
94 * plusy is the value y in the alignment term "xn + y" described
95 * earlier. It must have a value between 0 and 7, inclusive.
96 */
97 int
98 inet6_option_append(cmsg, typep, multx, plusy)
99 struct cmsghdr *cmsg;
100 const u_int8_t *typep;
101 int multx;
102 int plusy;
103 {
104 int padlen, optlen, off;
105 register u_char *bp = (u_char *)cmsg + cmsg->cmsg_len;
106 struct ip6_ext *eh = (struct ip6_ext *)CMSG_DATA(cmsg);
107
108 /* argument validation */
109 if (multx != 1 && multx != 2 && multx != 4 && multx != 8)
110 return(-1);
111 if (plusy < 0 || plusy > 7)
112 return(-1);
113 if (typep[0] > 255)
114 return(-1);
115
116 /*
117 * If this is the first option, allocate space for the
118 * first 2 bytes(for next header and length fields) of
119 * the option header.
120 */
121 if (bp == (u_char *)eh) {
122 bp += 2;
123 cmsg->cmsg_len += 2;
124 }
125
126 /* calculate pad length before the option. */
127 off = bp - (u_char *)eh;
128 padlen = (((off % multx) + (multx - 1)) & ~(multx - 1)) -
129 (off % multx);
130 padlen += plusy;
131 /* insert padding */
132 inet6_insert_padopt(bp, padlen);
133 cmsg->cmsg_len += padlen;
134 bp += padlen;
135
136 /* copy the option */
137 if (typep[0] == IP6OPT_PAD1)
138 optlen = 1;
139 else
140 optlen = typep[1] + 2;
141 memcpy(bp, typep, optlen);
142 bp += optlen;
143 cmsg->cmsg_len += optlen;
144
145 /* calculate pad length after the option and insert the padding */
146 off = bp - (u_char *)eh;
147 padlen = ((off + 7) & ~7) - off;
148 inet6_insert_padopt(bp, padlen);
149 bp += padlen;
150 cmsg->cmsg_len += padlen;
151
152 /* update the length field of the ip6 option header */
153 eh->ip6e_len = ((bp - (u_char *)eh) >> 3) - 1;
154
155 return(0);
156 }
157
158 /*
159 * This function appends a Hop-by-Hop option or a Destination option
160 * into an ancillary data object that has been initialized by
161 * inet6_option_init(). This function returns a pointer to the 8-bit
162 * option type field that starts the option on success, or NULL on an
163 * error.
164 * The difference between this function and inet6_option_append() is
165 * that the latter copies the contents of a previously built option into
166 * the ancillary data object while the current function returns a
167 * pointer to the space in the data object where the option's TLV must
168 * then be built by the caller.
169 *
170 */
171 u_int8_t *
172 inet6_option_alloc(cmsg, datalen, multx, plusy)
173 struct cmsghdr *cmsg;
174 int datalen;
175 int multx;
176 int plusy;
177 {
178 int padlen, off;
179 register u_int8_t *bp = (u_char *)cmsg + cmsg->cmsg_len;
180 u_int8_t *retval;
181 struct ip6_ext *eh = (struct ip6_ext *)CMSG_DATA(cmsg);
182
183 /* argument validation */
184 if (multx != 1 && multx != 2 && multx != 4 && multx != 8)
185 return(NULL);
186 if (plusy < 0 || plusy > 7)
187 return(NULL);
188
189 /*
190 * If this is the first option, allocate space for the
191 * first 2 bytes(for next header and length fields) of
192 * the option header.
193 */
194 if (bp == (u_char *)eh) {
195 bp += 2;
196 cmsg->cmsg_len += 2;
197 }
198
199 /* calculate pad length before the option. */
200 off = bp - (u_char *)eh;
201 padlen = (((off % multx) + (multx - 1)) & ~(multx - 1)) -
202 (off % multx);
203 padlen += plusy;
204 /* insert padding */
205 inet6_insert_padopt(bp, padlen);
206 cmsg->cmsg_len += padlen;
207 bp += padlen;
208
209 /* keep space to store specified length of data */
210 retval = bp;
211 bp += datalen;
212 cmsg->cmsg_len += datalen;
213
214 /* calculate pad length after the option and insert the padding */
215 off = bp - (u_char *)eh;
216 padlen = ((off + 7) & ~7) - off;
217 inet6_insert_padopt(bp, padlen);
218 bp += padlen;
219 cmsg->cmsg_len += padlen;
220
221 /* update the length field of the ip6 option header */
222 eh->ip6e_len = ((bp - (u_char *)eh) >> 3) - 1;
223
224 return(retval);
225 }
226
227 /*
228 * This function processes the next Hop-by-Hop option or Destination
229 * option in an ancillary data object. If another option remains to be
230 * processed, the return value of the function is 0 and *tptrp points to
231 * the 8-bit option type field (which is followed by the 8-bit option
232 * data length, followed by the option data). If no more options remain
233 * to be processed, the return value is -1 and *tptrp is NULL. If an
234 * error occurs, the return value is -1 and *tptrp is not NULL.
235 * (RFC 2292, 6.3.5)
236 */
237 int
238 inet6_option_next(cmsg, tptrp)
239 const struct cmsghdr *cmsg;
240 u_int8_t **tptrp;
241 {
242 struct ip6_ext *ip6e;
243 int hdrlen, optlen;
244 u_int8_t *lim;
245
246 if (cmsg->cmsg_level != IPPROTO_IPV6 ||
247 (cmsg->cmsg_type != IPV6_HOPOPTS &&
248 cmsg->cmsg_type != IPV6_DSTOPTS))
249 return(-1);
250
251 /* message length validation */
252 if (cmsg->cmsg_len < CMSG_SPACE(sizeof(struct ip6_ext)))
253 return(-1);
254 ip6e = (struct ip6_ext *)CMSG_DATA(cmsg);
255 hdrlen = (ip6e->ip6e_len + 1) << 3;
256 if (cmsg->cmsg_len < CMSG_SPACE(hdrlen))
257 return(-1);
258
259 /*
260 * If the caller does not specify the starting point,
261 * simply return the 1st option.
262 * Otherwise, search the option list for the next option.
263 */
264 lim = (u_int8_t *)ip6e + hdrlen;
265 if (*tptrp == NULL)
266 *tptrp = (u_int8_t *)(ip6e + 1);
267 else {
268 if ((optlen = ip6optlen(*tptrp, lim)) == 0)
269 return(-1);
270
271 *tptrp = *tptrp + optlen;
272 }
273 if (*tptrp >= lim) { /* there is no option */
274 *tptrp = NULL;
275 return(-1);
276 }
277 /*
278 * Finally, checks if the next option is safely stored in the
279 * cmsg data.
280 */
281 if (ip6optlen(*tptrp, lim) == 0)
282 return(-1);
283 else
284 return(0);
285 }
286
287 /*
288 * This function is similar to the inet6_option_next() function,
289 * except this function lets the caller specify the option type to be
290 * searched for, instead of always returning the next option in the
291 * ancillary data object.
292 * Note: RFC 2292 says the type of tptrp is u_int8_t *, but we think
293 * it's a typo. The variable should be type of u_int8_t **.
294 */
295 int
296 inet6_option_find(cmsg, tptrp, type)
297 const struct cmsghdr *cmsg;
298 u_int8_t **tptrp;
299 int type;
300 {
301 struct ip6_ext *ip6e;
302 int hdrlen, optlen;
303 u_int8_t *optp, *lim;
304
305 if (cmsg->cmsg_level != IPPROTO_IPV6 ||
306 (cmsg->cmsg_type != IPV6_HOPOPTS &&
307 cmsg->cmsg_type != IPV6_DSTOPTS))
308 return(-1);
309
310 /* message length validation */
311 if (cmsg->cmsg_len < CMSG_SPACE(sizeof(struct ip6_ext)))
312 return(-1);
313 ip6e = (struct ip6_ext *)CMSG_DATA(cmsg);
314 hdrlen = (ip6e->ip6e_len + 1) << 3;
315 if (cmsg->cmsg_len < CMSG_SPACE(hdrlen))
316 return(-1);
317
318 /*
319 * If the caller does not specify the starting point,
320 * search from the beginning of the option list.
321 * Otherwise, search from *the next option* of the specified point.
322 */
323 lim = (u_int8_t *)ip6e + hdrlen;
324 if (*tptrp == NULL)
325 *tptrp = (u_int8_t *)(ip6e + 1);
326 else {
327 if ((optlen = ip6optlen(*tptrp, lim)) == 0)
328 return(-1);
329
330 *tptrp = *tptrp + optlen;
331 }
332 for (optp = *tptrp; optp < lim; optp += optlen) {
333 if (*optp == type) {
334 *tptrp = optp;
335 return(0);
336 }
337 if ((optlen = ip6optlen(optp, lim)) == 0)
338 return(-1);
339 }
340
341 /* search failed */
342 *tptrp = NULL;
343 return(-1);
344 }
345
346 /*
347 * Calculate the length of a given IPv6 option. Also checks
348 * if the option is safely stored in user's buffer according to the
349 * calculated length and the limitation of the buffer.
350 */
351 static int
352 ip6optlen(opt, lim)
353 u_int8_t *opt, *lim;
354 {
355 int optlen;
356
357 if (*opt == IP6OPT_PAD1)
358 optlen = 1;
359 else {
360 /* is there enough space to store type and len? */
361 if (opt + 2 > lim)
362 return(0);
363 optlen = *(opt + 1) + 2;
364 }
365 if (opt + optlen <= lim)
366 return(optlen);
367
368 return(0);
369 }
370
371 static void
372 inet6_insert_padopt(u_char *p, int len)
373 {
374 switch(len) {
375 case 0:
376 return;
377 case 1:
378 p[0] = IP6OPT_PAD1;
379 return;
380 default:
381 p[0] = IP6OPT_PADN;
382 p[1] = len - 2;
383 memset(&p[2], 0, len - 2);
384 return;
385 }
386 }