]>
git.saurik.com Git - apple/libinfo.git/blob - gen.subproj/ip6opt.c
2 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
29 * $FreeBSD: src/lib/libc/net/ip6opt.c,v 1.1 1999/12/16 18:32:01 shin Exp $
32 #include <sys/param.h>
33 #include <sys/types.h>
34 #include <sys/socket.h>
36 #include <netinet/in.h>
37 #include <netinet/ip6.h>
42 static int ip6optlen(u_int8_t
*opt
, u_int8_t
*lim
);
43 static void inet6_insert_padopt(u_char
*p
, int len
);
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.
55 inet6_option_space(nbytes
)
58 nbytes
+= 2; /* we need space for nxt-hdr and length fields */
59 return(CMSG_SPACE((nbytes
+ 7) & ~7));
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.
68 inet6_option_init(bp
, cmsgp
, type
)
70 struct cmsghdr
**cmsgp
;
73 register struct cmsghdr
*ch
= (struct cmsghdr
*)bp
;
75 /* argument validation */
76 if (type
!= IPV6_HOPOPTS
&& type
!= IPV6_DSTOPTS
)
79 ch
->cmsg_level
= IPPROTO_IPV6
;
81 ch
->cmsg_len
= CMSG_LEN(0);
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
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.
98 inet6_option_append(cmsg
, typep
, multx
, plusy
)
100 const u_int8_t
*typep
;
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
);
108 /* argument validation */
109 if (multx
!= 1 && multx
!= 2 && multx
!= 4 && multx
!= 8)
111 if (plusy
< 0 || plusy
> 7)
115 * If this is the first option, allocate space for the
116 * first 2 bytes(for next header and length fields) of
119 if (bp
== (u_char
*)eh
) {
124 /* calculate pad length before the option. */
125 off
= bp
- (u_char
*)eh
;
126 padlen
= (((off
% multx
) + (multx
- 1)) & ~(multx
- 1)) -
130 inet6_insert_padopt(bp
, padlen
);
131 cmsg
->cmsg_len
+= padlen
;
134 /* copy the option */
135 if (typep
[0] == IP6OPT_PAD1
)
138 optlen
= typep
[1] + 2;
139 memcpy(bp
, typep
, optlen
);
141 cmsg
->cmsg_len
+= optlen
;
143 /* calculate pad length after the option and insert the padding */
144 off
= bp
- (u_char
*)eh
;
145 padlen
= ((off
+ 7) & ~7) - off
;
146 inet6_insert_padopt(bp
, padlen
);
148 cmsg
->cmsg_len
+= padlen
;
150 /* update the length field of the ip6 option header */
151 eh
->ip6e_len
= ((bp
- (u_char
*)eh
) >> 3) - 1;
157 * This function appends a Hop-by-Hop option or a Destination option
158 * into an ancillary data object that has been initialized by
159 * inet6_option_init(). This function returns a pointer to the 8-bit
160 * option type field that starts the option on success, or NULL on an
162 * The difference between this function and inet6_option_append() is
163 * that the latter copies the contents of a previously built option into
164 * the ancillary data object while the current function returns a
165 * pointer to the space in the data object where the option's TLV must
166 * then be built by the caller.
170 inet6_option_alloc(cmsg
, datalen
, multx
, plusy
)
171 struct cmsghdr
*cmsg
;
177 register u_int8_t
*bp
= (u_char
*)cmsg
+ cmsg
->cmsg_len
;
179 struct ip6_ext
*eh
= (struct ip6_ext
*)CMSG_DATA(cmsg
);
181 /* argument validation */
182 if (multx
!= 1 && multx
!= 2 && multx
!= 4 && multx
!= 8)
184 if (plusy
< 0 || plusy
> 7)
188 * If this is the first option, allocate space for the
189 * first 2 bytes(for next header and length fields) of
192 if (bp
== (u_char
*)eh
) {
197 /* calculate pad length before the option. */
198 off
= bp
- (u_char
*)eh
;
199 padlen
= (((off
% multx
) + (multx
- 1)) & ~(multx
- 1)) -
203 inet6_insert_padopt(bp
, padlen
);
204 cmsg
->cmsg_len
+= padlen
;
207 /* keep space to store specified length of data */
210 cmsg
->cmsg_len
+= datalen
;
212 /* calculate pad length after the option and insert the padding */
213 off
= bp
- (u_char
*)eh
;
214 padlen
= ((off
+ 7) & ~7) - off
;
215 inet6_insert_padopt(bp
, padlen
);
217 cmsg
->cmsg_len
+= padlen
;
219 /* update the length field of the ip6 option header */
220 eh
->ip6e_len
= ((bp
- (u_char
*)eh
) >> 3) - 1;
226 * This function processes the next Hop-by-Hop option or Destination
227 * option in an ancillary data object. If another option remains to be
228 * processed, the return value of the function is 0 and *tptrp points to
229 * the 8-bit option type field (which is followed by the 8-bit option
230 * data length, followed by the option data). If no more options remain
231 * to be processed, the return value is -1 and *tptrp is NULL. If an
232 * error occurs, the return value is -1 and *tptrp is not NULL.
236 inet6_option_next(cmsg
, tptrp
)
237 const struct cmsghdr
*cmsg
;
240 struct ip6_ext
*ip6e
;
244 if (cmsg
->cmsg_level
!= IPPROTO_IPV6
||
245 (cmsg
->cmsg_type
!= IPV6_HOPOPTS
&&
246 cmsg
->cmsg_type
!= IPV6_DSTOPTS
))
249 /* message length validation */
250 if (cmsg
->cmsg_len
< CMSG_SPACE(sizeof(struct ip6_ext
)))
252 ip6e
= (struct ip6_ext
*)CMSG_DATA(cmsg
);
253 hdrlen
= (ip6e
->ip6e_len
+ 1) << 3;
254 if (cmsg
->cmsg_len
< CMSG_SPACE(hdrlen
))
258 * If the caller does not specify the starting point,
259 * simply return the 1st option.
260 * Otherwise, search the option list for the next option.
262 lim
= (u_int8_t
*)ip6e
+ hdrlen
;
264 *tptrp
= (u_int8_t
*)(ip6e
+ 1);
266 if ((optlen
= ip6optlen(*tptrp
, lim
)) == 0)
269 *tptrp
= *tptrp
+ optlen
;
271 if (*tptrp
>= lim
) { /* there is no option */
276 * Finally, checks if the next option is safely stored in the
279 if (ip6optlen(*tptrp
, lim
) == 0)
286 * This function is similar to the inet6_option_next() function,
287 * except this function lets the caller specify the option type to be
288 * searched for, instead of always returning the next option in the
289 * ancillary data object.
290 * Note: RFC 2292 says the type of tptrp is u_int8_t *, but we think
291 * it's a typo. The variable should be type of u_int8_t **.
294 inet6_option_find(cmsg
, tptrp
, type
)
295 const struct cmsghdr
*cmsg
;
299 struct ip6_ext
*ip6e
;
301 u_int8_t
*optp
, *lim
;
303 if (cmsg
->cmsg_level
!= IPPROTO_IPV6
||
304 (cmsg
->cmsg_type
!= IPV6_HOPOPTS
&&
305 cmsg
->cmsg_type
!= IPV6_DSTOPTS
))
308 /* message length validation */
309 if (cmsg
->cmsg_len
< CMSG_SPACE(sizeof(struct ip6_ext
)))
311 ip6e
= (struct ip6_ext
*)CMSG_DATA(cmsg
);
312 hdrlen
= (ip6e
->ip6e_len
+ 1) << 3;
313 if (cmsg
->cmsg_len
< CMSG_SPACE(hdrlen
))
317 * If the caller does not specify the starting point,
318 * search from the beginning of the option list.
319 * Otherwise, search from *the next option* of the specified point.
321 lim
= (u_int8_t
*)ip6e
+ hdrlen
;
323 *tptrp
= (u_int8_t
*)(ip6e
+ 1);
325 if ((optlen
= ip6optlen(*tptrp
, lim
)) == 0)
328 *tptrp
= *tptrp
+ optlen
;
330 for (optp
= *tptrp
; optp
< lim
; optp
+= optlen
) {
335 if ((optlen
= ip6optlen(optp
, lim
)) == 0)
345 * Calculate the length of a given IPv6 option. Also checks
346 * if the option is safely stored in user's buffer according to the
347 * calculated length and the limitation of the buffer.
355 if (*opt
== IP6OPT_PAD1
)
358 /* is there enough space to store type and len? */
361 optlen
= *(opt
+ 1) + 2;
363 if (opt
+ optlen
<= lim
)
370 inet6_insert_padopt(u_char
*p
, int len
)
381 memset(&p
[2], 0, len
- 2);