]>
git.saurik.com Git - apple/libinfo.git/blob - gen.subproj/ip6opt.c
14ad8ae1ff3edb4c2f52fe73c6f3b65f2fdb777f
2 * Copyright (c) 2018 Apple Inc. All rights reserved.
4 /* $KAME: ip6opt.c,v 1.13 2003/06/06 10:08:20 suz Exp $ */
7 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of the project nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * $FreeBSD: src/lib/libc/net/ip6opt.c,v 1.8.10.2.2.1 2010/06/14 02:09:06 kensmith Exp $
37 #include "libinfo_common.h"
40 * These routines support RFC 3542
41 * __APPLE_USE_RFC_3542 selects the appropriate API in <netinet6/in6.h>
43 #define __APPLE_USE_RFC_3542
45 #include <sys/cdefs.h>
47 #include <sys/param.h>
48 #include <sys/types.h>
49 #include <sys/socket.h>
51 #include <netinet/in.h>
52 #include <netinet/ip6.h>
57 static int ip6optlen(u_int8_t
*opt
, u_int8_t
*lim
);
58 static void inet6_insert_padopt(u_char
*p
, int len
);
61 * This function returns the number of bytes required to hold an option
62 * when it is stored as ancillary data, including the cmsghdr structure
63 * at the beginning, and any padding at the end (to make its size a
64 * multiple of 8 bytes). The argument is the size of the structure
65 * defining the option, which must include any pad bytes at the
66 * beginning (the value y in the alignment term "xn + y"), the type
67 * byte, the length byte, and the option data.
71 inet6_option_space(int nbytes
)
73 nbytes
+= 2; /* we need space for nxt-hdr and length fields */
74 return(CMSG_SPACE((nbytes
+ 7) & ~7));
78 * This function is called once per ancillary data object that will
79 * contain either Hop-by-Hop or Destination options. It returns 0 on
80 * success or -1 on an error.
84 inet6_option_init(void *bp
, struct cmsghdr
**cmsgp
, int type
)
86 struct cmsghdr
*ch
= (struct cmsghdr
*)bp
;
88 /* argument validation */
89 if (type
!= IPV6_HOPOPTS
&& type
!= IPV6_DSTOPTS
)
92 ch
->cmsg_level
= IPPROTO_IPV6
;
94 ch
->cmsg_len
= CMSG_LEN(0);
101 * This function appends a Hop-by-Hop option or a Destination option
102 * into an ancillary data object that has been initialized by
103 * inet6_option_init(). This function returns 0 if it succeeds or -1 on
105 * multx is the value x in the alignment term "xn + y" described
106 * earlier. It must have a value of 1, 2, 4, or 8.
107 * plusy is the value y in the alignment term "xn + y" described
108 * earlier. It must have a value between 0 and 7, inclusive.
112 inet6_option_append(struct cmsghdr
*cmsg
, const u_int8_t
*typep
, int multx
,
115 int padlen
, optlen
, off
;
116 u_char
*bp
= (u_char
*)cmsg
+ cmsg
->cmsg_len
;
117 struct ip6_ext
*eh
= (struct ip6_ext
*)CMSG_DATA(cmsg
);
119 /* argument validation */
120 if (multx
!= 1 && multx
!= 2 && multx
!= 4 && multx
!= 8)
122 if (plusy
< 0 || plusy
> 7)
126 * If this is the first option, allocate space for the
127 * first 2 bytes(for next header and length fields) of
130 if (bp
== (u_char
*)eh
) {
135 /* calculate pad length before the option. */
136 off
= bp
- (u_char
*)eh
;
137 padlen
= (((off
% multx
) + (multx
- 1)) & ~(multx
- 1)) -
140 padlen
%= multx
; /* keep the pad as short as possible */
142 inet6_insert_padopt(bp
, padlen
);
143 cmsg
->cmsg_len
+= padlen
;
146 /* copy the option */
147 if (typep
[0] == IP6OPT_PAD1
)
150 optlen
= typep
[1] + 2;
151 memcpy(bp
, typep
, optlen
);
153 cmsg
->cmsg_len
+= optlen
;
155 /* calculate pad length after the option and insert the padding */
156 off
= bp
- (u_char
*)eh
;
157 padlen
= ((off
+ 7) & ~7) - off
;
158 inet6_insert_padopt(bp
, padlen
);
160 cmsg
->cmsg_len
+= padlen
;
162 /* update the length field of the ip6 option header */
163 eh
->ip6e_len
= ((bp
- (u_char
*)eh
) >> 3) - 1;
169 * This function appends a Hop-by-Hop option or a Destination option
170 * into an ancillary data object that has been initialized by
171 * inet6_option_init(). This function returns a pointer to the 8-bit
172 * option type field that starts the option on success, or NULL on an
174 * The difference between this function and inet6_option_append() is
175 * that the latter copies the contents of a previously built option into
176 * the ancillary data object while the current function returns a
177 * pointer to the space in the data object where the option's TLV must
178 * then be built by the caller.
183 inet6_option_alloc(struct cmsghdr
*cmsg
, int datalen
, int multx
, int plusy
)
186 u_int8_t
*bp
= (u_char
*)cmsg
+ cmsg
->cmsg_len
;
188 struct ip6_ext
*eh
= (struct ip6_ext
*)CMSG_DATA(cmsg
);
190 /* argument validation */
191 if (multx
!= 1 && multx
!= 2 && multx
!= 4 && multx
!= 8)
193 if (plusy
< 0 || plusy
> 7)
197 * If this is the first option, allocate space for the
198 * first 2 bytes(for next header and length fields) of
201 if (bp
== (u_char
*)eh
) {
206 /* calculate pad length before the option. */
207 off
= bp
- (u_char
*)eh
;
208 padlen
= (((off
% multx
) + (multx
- 1)) & ~(multx
- 1)) -
211 padlen
%= multx
; /* keep the pad as short as possible */
213 inet6_insert_padopt(bp
, padlen
);
214 cmsg
->cmsg_len
+= padlen
;
217 /* keep space to store specified length of data */
220 cmsg
->cmsg_len
+= datalen
;
222 /* calculate pad length after the option and insert the padding */
223 off
= bp
- (u_char
*)eh
;
224 padlen
= ((off
+ 7) & ~7) - off
;
225 inet6_insert_padopt(bp
, padlen
);
227 cmsg
->cmsg_len
+= padlen
;
229 /* update the length field of the ip6 option header */
230 eh
->ip6e_len
= ((bp
- (u_char
*)eh
) >> 3) - 1;
236 * This function processes the next Hop-by-Hop option or Destination
237 * option in an ancillary data object. If another option remains to be
238 * processed, the return value of the function is 0 and *tptrp points to
239 * the 8-bit option type field (which is followed by the 8-bit option
240 * data length, followed by the option data). If no more options remain
241 * to be processed, the return value is -1 and *tptrp is NULL. If an
242 * error occurs, the return value is -1 and *tptrp is not NULL.
247 inet6_option_next(const struct cmsghdr
*cmsg
, u_int8_t
**tptrp
)
249 struct ip6_ext
*ip6e
;
253 if (cmsg
->cmsg_level
!= IPPROTO_IPV6
||
254 (cmsg
->cmsg_type
!= IPV6_HOPOPTS
&&
255 cmsg
->cmsg_type
!= IPV6_DSTOPTS
))
258 /* message length validation */
259 if (cmsg
->cmsg_len
< CMSG_SPACE(sizeof(struct ip6_ext
)))
261 ip6e
= (struct ip6_ext
*)CMSG_DATA(cmsg
);
262 hdrlen
= (ip6e
->ip6e_len
+ 1) << 3;
263 if (cmsg
->cmsg_len
< CMSG_SPACE(hdrlen
))
267 * If the caller does not specify the starting point,
268 * simply return the 1st option.
269 * Otherwise, search the option list for the next option.
271 lim
= (u_int8_t
*)ip6e
+ hdrlen
;
273 *tptrp
= (u_int8_t
*)(ip6e
+ 1);
275 if ((optlen
= ip6optlen(*tptrp
, lim
)) == 0)
278 *tptrp
= *tptrp
+ optlen
;
280 if (*tptrp
>= lim
) { /* there is no option */
285 * Finally, checks if the next option is safely stored in the
288 if (ip6optlen(*tptrp
, lim
) == 0)
295 * This function is similar to the inet6_option_next() function,
296 * except this function lets the caller specify the option type to be
297 * searched for, instead of always returning the next option in the
298 * ancillary data object.
299 * Note: RFC 2292 says the type of tptrp is u_int8_t *, but we think
300 * it's a typo. The variable should be type of u_int8_t **.
304 inet6_option_find(const struct cmsghdr
*cmsg
, u_int8_t
**tptrp
, int type
)
306 struct ip6_ext
*ip6e
;
308 u_int8_t
*optp
, *lim
;
310 if (cmsg
->cmsg_level
!= IPPROTO_IPV6
||
311 (cmsg
->cmsg_type
!= IPV6_HOPOPTS
&&
312 cmsg
->cmsg_type
!= IPV6_DSTOPTS
))
315 /* message length validation */
316 if (cmsg
->cmsg_len
< CMSG_SPACE(sizeof(struct ip6_ext
)))
318 ip6e
= (struct ip6_ext
*)CMSG_DATA(cmsg
);
319 hdrlen
= (ip6e
->ip6e_len
+ 1) << 3;
320 if (cmsg
->cmsg_len
< CMSG_SPACE(hdrlen
))
324 * If the caller does not specify the starting point,
325 * search from the beginning of the option list.
326 * Otherwise, search from *the next option* of the specified point.
328 lim
= (u_int8_t
*)ip6e
+ hdrlen
;
330 *tptrp
= (u_int8_t
*)(ip6e
+ 1);
332 if ((optlen
= ip6optlen(*tptrp
, lim
)) == 0)
335 *tptrp
= *tptrp
+ optlen
;
337 for (optp
= *tptrp
; optp
< lim
; optp
+= optlen
) {
342 if ((optlen
= ip6optlen(optp
, lim
)) == 0)
352 * Calculate the length of a given IPv6 option. Also checks
353 * if the option is safely stored in user's buffer according to the
354 * calculated length and the limitation of the buffer.
357 ip6optlen(u_int8_t
*opt
, u_int8_t
*lim
)
361 if (*opt
== IP6OPT_PAD1
)
364 /* is there enough space to store type and len? */
367 optlen
= *(opt
+ 1) + 2;
369 if (opt
+ optlen
<= lim
)
376 inet6_insert_padopt(u_char
*p
, int len
)
387 memset(&p
[2], 0, len
- 2);
393 * The following functions are defined in RFC3542, which is a successor
399 inet6_opt_init(void *extbuf
, socklen_t extlen
)
401 struct ip6_ext
*ext
= (struct ip6_ext
*)extbuf
;
403 if (extlen
== 0 || (extlen
% 8)) {
408 ext
->ip6e_len
= (extlen
>> 3) - 1;
411 return(2); /* sizeof the next and the length fields */
416 inet6_opt_append(void *extbuf
, socklen_t extlen
, int offset
, u_int8_t type
, socklen_t len
, u_int8_t align
, void **databufp
)
418 int currentlen
= offset
, padlen
= 0;
421 * The option type must have a value from 2 to 255, inclusive.
422 * (0 and 1 are reserved for the Pad1 and PadN options, respectively.)
428 * The option data length must have a value between 0 and 255,
429 * inclusive, and is the length of the option data that follows.
436 * The align parameter must have a value of 1, 2, 4, or 8.
437 * The align value can not exceed the value of len.
439 if (align
!= 1 && align
!= 2 && align
!= 4 && align
!= 8)
444 /* Calculate the padding length. */
445 currentlen
+= 2 + len
; /* 2 means "type + len" */
446 if (currentlen
% align
)
447 padlen
= align
- (currentlen
% align
);
449 /* The option must fit in the extension header buffer. */
450 currentlen
+= padlen
;
451 if (extlen
&& /* XXX: right? */
456 u_int8_t
*optp
= (u_int8_t
*)extbuf
+ offset
;
459 /* insert a Pad1 option */
463 else if (padlen
> 0) {
464 /* insert a PadN option for alignment */
465 *optp
++ = IP6OPT_PADN
;
466 *optp
++ = padlen
- 2;
467 memset(optp
, 0, padlen
- 2);
468 optp
+= (padlen
- 2);
482 inet6_opt_finish(void *extbuf
, socklen_t extlen
, int offset
)
484 int updatelen
= offset
> 0 ? (1 + ((offset
- 1) | 7)) : 0;;
488 int padlen
= updatelen
- offset
;
490 if (updatelen
> extlen
)
493 padp
= (u_int8_t
*)extbuf
+ offset
;
496 else if (padlen
> 0) {
497 *padp
++ = IP6OPT_PADN
;
498 *padp
++ = (padlen
- 2);
499 memset(padp
, 0, padlen
- 2);
508 inet6_opt_set_val(void *databuf
, int offset
, void *val
, socklen_t vallen
)
510 memcpy((u_int8_t
*)databuf
+ offset
, val
, vallen
);
511 return(offset
+ vallen
);
516 inet6_opt_next(void *extbuf
, socklen_t extlen
, int offset
, u_int8_t
*typep
, socklen_t
*lenp
, void **databufp
)
518 u_int8_t
*optp
, *lim
;
521 /* Validate extlen. XXX: is the variable really necessary?? */
522 if (extlen
== 0 || (extlen
% 8))
524 lim
= (u_int8_t
*)extbuf
+ extlen
;
527 * If this is the first time this function called for this options
528 * header, simply return the 1st option.
529 * Otherwise, search the option list for the next option.
532 optp
= (u_int8_t
*)((struct ip6_hbh
*)extbuf
+ 1);
535 optp
= (u_int8_t
*)extbuf
+ offset
;
537 /* Find the next option skipping any padding options. */
544 if ((optlen
= ip6optlen(optp
, lim
)) == 0)
549 if ((optlen
= ip6optlen(optp
, lim
)) == 0)
553 *databufp
= optp
+ 2;
554 return(optp
+ optlen
- (u_int8_t
*)extbuf
);
559 *databufp
= NULL
; /* for safety */
565 inet6_opt_find(void *extbuf
, socklen_t extlen
, int offset
, u_int8_t type
, socklen_t
*lenp
, void **databufp
)
567 u_int8_t
*optp
, *lim
;
570 /* Validate extlen. XXX: is the variable really necessary?? */
571 if (extlen
== 0 || (extlen
% 8))
573 lim
= (u_int8_t
*)extbuf
+ extlen
;
576 * If this is the first time this function called for this options
577 * header, simply return the 1st option.
578 * Otherwise, search the option list for the next option.
581 optp
= (u_int8_t
*)((struct ip6_hbh
*)extbuf
+ 1);
584 optp
= (u_int8_t
*)extbuf
+ offset
;
586 /* Find the specified option */
588 if ((optlen
= ip6optlen(optp
, lim
)) == 0)
591 if (*optp
== type
) { /* found */
593 *databufp
= optp
+ 2;
594 return(optp
+ optlen
- (u_int8_t
*)extbuf
);
601 *databufp
= NULL
; /* for safety */
607 inet6_opt_get_val(void *databuf
, int offset
, void *val
, socklen_t vallen
)
609 /* we can't assume alignment here */
610 memcpy(val
, (u_int8_t
*)databuf
+ offset
, vallen
);
612 return(offset
+ vallen
);