1*0a6a1f1dSLionel Sambuc /* $NetBSD: ip6opt.c,v 1.15 2014/02/07 02:36:06 christos Exp $ */
22fe8fb19SBen Gras
32fe8fb19SBen Gras /*
42fe8fb19SBen Gras * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
52fe8fb19SBen Gras * All rights reserved.
62fe8fb19SBen Gras *
72fe8fb19SBen Gras * Redistribution and use in source and binary forms, with or without
82fe8fb19SBen Gras * modification, are permitted provided that the following conditions
92fe8fb19SBen Gras * are met:
102fe8fb19SBen Gras * 1. Redistributions of source code must retain the above copyright
112fe8fb19SBen Gras * notice, this list of conditions and the following disclaimer.
122fe8fb19SBen Gras * 2. Redistributions in binary form must reproduce the above copyright
132fe8fb19SBen Gras * notice, this list of conditions and the following disclaimer in the
142fe8fb19SBen Gras * documentation and/or other materials provided with the distribution.
152fe8fb19SBen Gras * 3. Neither the name of the project nor the names of its contributors
162fe8fb19SBen Gras * may be used to endorse or promote products derived from this software
172fe8fb19SBen Gras * without specific prior written permission.
182fe8fb19SBen Gras *
192fe8fb19SBen Gras * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
202fe8fb19SBen Gras * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
212fe8fb19SBen Gras * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
222fe8fb19SBen Gras * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
232fe8fb19SBen Gras * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
242fe8fb19SBen Gras * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
252fe8fb19SBen Gras * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
262fe8fb19SBen Gras * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
272fe8fb19SBen Gras * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
282fe8fb19SBen Gras * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
292fe8fb19SBen Gras * SUCH DAMAGE.
302fe8fb19SBen Gras */
312fe8fb19SBen Gras
322fe8fb19SBen Gras #include <sys/cdefs.h>
332fe8fb19SBen Gras #if defined(LIBC_SCCS) && !defined(lint)
34*0a6a1f1dSLionel Sambuc __RCSID("$NetBSD: ip6opt.c,v 1.15 2014/02/07 02:36:06 christos Exp $");
352fe8fb19SBen Gras #endif /* LIBC_SCCS and not lint */
362fe8fb19SBen Gras
372fe8fb19SBen Gras #include "namespace.h"
382fe8fb19SBen Gras #include <sys/param.h>
392fe8fb19SBen Gras #include <sys/types.h>
402fe8fb19SBen Gras #include <sys/socket.h>
412fe8fb19SBen Gras
422fe8fb19SBen Gras #include <netinet/in.h>
432fe8fb19SBen Gras #include <netinet/ip6.h>
442fe8fb19SBen Gras
452fe8fb19SBen Gras #include <assert.h>
46f14fb602SLionel Sambuc #include <stddef.h>
472fe8fb19SBen Gras #include <string.h>
482fe8fb19SBen Gras #include <stdio.h>
492fe8fb19SBen Gras
502fe8fb19SBen Gras #ifdef __weak_alias
512fe8fb19SBen Gras __weak_alias(inet6_option_alloc,_inet6_option_alloc)
522fe8fb19SBen Gras __weak_alias(inet6_option_append,_inet6_option_append)
532fe8fb19SBen Gras __weak_alias(inet6_option_find,_inet6_option_find)
542fe8fb19SBen Gras __weak_alias(inet6_option_init,_inet6_option_init)
552fe8fb19SBen Gras __weak_alias(inet6_option_next,_inet6_option_next)
562fe8fb19SBen Gras __weak_alias(inet6_option_space,_inet6_option_space)
572fe8fb19SBen Gras __weak_alias(inet6_opt_init, _inet6_opt_init)
582fe8fb19SBen Gras __weak_alias(inet6_opt_append, _inet6_opt_append)
592fe8fb19SBen Gras __weak_alias(inet6_opt_finish, _inet6_opt_finish)
602fe8fb19SBen Gras __weak_alias(inet6_opt_set_val, _inet6_opt_set_val)
612fe8fb19SBen Gras __weak_alias(inet6_opt_next, _inet6_opt_next)
622fe8fb19SBen Gras __weak_alias(inet6_opt_find, _inet6_opt_find)
632fe8fb19SBen Gras __weak_alias(inet6_opt_get_val, _inet6_opt_get_val)
642fe8fb19SBen Gras #endif
652fe8fb19SBen Gras
66f14fb602SLionel Sambuc static int ip6optlen(uint8_t *opt, uint8_t *lim);
67f14fb602SLionel Sambuc static void inet6_insert_padopt(uint8_t *p, size_t len);
682fe8fb19SBen Gras
692fe8fb19SBen Gras /*
702fe8fb19SBen Gras * This function returns the number of bytes required to hold an option
712fe8fb19SBen Gras * when it is stored as ancillary data, including the cmsghdr structure
722fe8fb19SBen Gras * at the beginning, and any padding at the end (to make its size a
732fe8fb19SBen Gras * multiple of 8 bytes). The argument is the size of the structure
742fe8fb19SBen Gras * defining the option, which must include any pad bytes at the
752fe8fb19SBen Gras * beginning (the value y in the alignment term "xn + y"), the type
762fe8fb19SBen Gras * byte, the length byte, and the option data.
772fe8fb19SBen Gras */
782fe8fb19SBen Gras int
inet6_option_space(int nbytes)79f14fb602SLionel Sambuc inet6_option_space(int nbytes)
802fe8fb19SBen Gras {
81f14fb602SLionel Sambuc size_t sp;
822fe8fb19SBen Gras nbytes += 2; /* we need space for nxt-hdr and length fields */
83f14fb602SLionel Sambuc sp = CMSG_SPACE((nbytes + 7) & ~7);
84f14fb602SLionel Sambuc _DIAGASSERT(__type_fit(int, sp));
85f14fb602SLionel Sambuc return (int)sp;
862fe8fb19SBen Gras }
872fe8fb19SBen Gras
882fe8fb19SBen Gras /*
892fe8fb19SBen Gras * This function is called once per ancillary data object that will
902fe8fb19SBen Gras * contain either Hop-by-Hop or Destination options. It returns 0 on
912fe8fb19SBen Gras * success or -1 on an error.
922fe8fb19SBen Gras */
932fe8fb19SBen Gras int
inet6_option_init(void * bp,struct cmsghdr ** cmsgp,int type)94f14fb602SLionel Sambuc inet6_option_init(void *bp, struct cmsghdr **cmsgp, int type)
952fe8fb19SBen Gras {
962fe8fb19SBen Gras register struct cmsghdr *ch;
972fe8fb19SBen Gras
982fe8fb19SBen Gras _DIAGASSERT(bp != NULL);
992fe8fb19SBen Gras _DIAGASSERT(cmsgp != NULL);
1002fe8fb19SBen Gras
1012fe8fb19SBen Gras ch = (struct cmsghdr *)bp;
1022fe8fb19SBen Gras
1032fe8fb19SBen Gras /* argument validation */
1042fe8fb19SBen Gras if (type != IPV6_HOPOPTS && type != IPV6_DSTOPTS)
1052fe8fb19SBen Gras return(-1);
1062fe8fb19SBen Gras
1072fe8fb19SBen Gras ch->cmsg_level = IPPROTO_IPV6;
1082fe8fb19SBen Gras ch->cmsg_type = type;
1092fe8fb19SBen Gras ch->cmsg_len = CMSG_LEN(0);
1102fe8fb19SBen Gras
1112fe8fb19SBen Gras *cmsgp = ch;
1122fe8fb19SBen Gras return(0);
1132fe8fb19SBen Gras }
1142fe8fb19SBen Gras
1152fe8fb19SBen Gras /*
1162fe8fb19SBen Gras * This function appends a Hop-by-Hop option or a Destination option
1172fe8fb19SBen Gras * into an ancillary data object that has been initialized by
1182fe8fb19SBen Gras * inet6_option_init(). This function returns 0 if it succeeds or -1 on
1192fe8fb19SBen Gras * an error.
1202fe8fb19SBen Gras * multx is the value x in the alignment term "xn + y" described
1212fe8fb19SBen Gras * earlier. It must have a value of 1, 2, 4, or 8.
1222fe8fb19SBen Gras * plusy is the value y in the alignment term "xn + y" described
1232fe8fb19SBen Gras * earlier. It must have a value between 0 and 7, inclusive.
1242fe8fb19SBen Gras */
1252fe8fb19SBen Gras int
inet6_option_append(struct cmsghdr * cmsg,const uint8_t * typep,int multx,int plusy)126f14fb602SLionel Sambuc inet6_option_append(struct cmsghdr *cmsg, const uint8_t *typep, int multx,
127f14fb602SLionel Sambuc int plusy)
1282fe8fb19SBen Gras {
1292fe8fb19SBen Gras size_t padlen, optlen, off;
130f14fb602SLionel Sambuc register uint8_t *bp;
1312fe8fb19SBen Gras struct ip6_ext *eh;
1322fe8fb19SBen Gras
1332fe8fb19SBen Gras _DIAGASSERT(cmsg != NULL);
1342fe8fb19SBen Gras _DIAGASSERT(typep != NULL);
1352fe8fb19SBen Gras
136f14fb602SLionel Sambuc bp = (uint8_t *)(void *)cmsg + cmsg->cmsg_len;
1372fe8fb19SBen Gras eh = (struct ip6_ext *)(void *)CMSG_DATA(cmsg);
1382fe8fb19SBen Gras
1392fe8fb19SBen Gras /* argument validation */
1402fe8fb19SBen Gras if (multx != 1 && multx != 2 && multx != 4 && multx != 8)
1412fe8fb19SBen Gras return(-1);
1422fe8fb19SBen Gras if (plusy < 0 || plusy > 7)
1432fe8fb19SBen Gras return(-1);
1442fe8fb19SBen Gras
1452fe8fb19SBen Gras /*
1462fe8fb19SBen Gras * If this is the first option, allocate space for the
1472fe8fb19SBen Gras * first 2 bytes(for next header and length fields) of
1482fe8fb19SBen Gras * the option header.
1492fe8fb19SBen Gras */
150f14fb602SLionel Sambuc if (bp == (uint8_t *)(void *)eh) {
1512fe8fb19SBen Gras bp += 2;
1522fe8fb19SBen Gras cmsg->cmsg_len += 2;
1532fe8fb19SBen Gras }
1542fe8fb19SBen Gras
1552fe8fb19SBen Gras /* calculate pad length before the option. */
156f14fb602SLionel Sambuc off = bp - (uint8_t *)(void *)eh;
1572fe8fb19SBen Gras padlen = (((off % multx) + (multx - 1)) & ~(multx - 1)) -
1582fe8fb19SBen Gras (off % multx);
1592fe8fb19SBen Gras padlen += plusy;
1602fe8fb19SBen Gras padlen %= multx; /* keep the pad as short as possible */
1612fe8fb19SBen Gras /* insert padding */
1622fe8fb19SBen Gras inet6_insert_padopt(bp, padlen);
163f14fb602SLionel Sambuc _DIAGASSERT(__type_fit(socklen_t, padlen + cmsg->cmsg_len));
164f14fb602SLionel Sambuc cmsg->cmsg_len += (socklen_t)padlen;
1652fe8fb19SBen Gras bp += padlen;
1662fe8fb19SBen Gras
1672fe8fb19SBen Gras /* copy the option */
1682fe8fb19SBen Gras if (typep[0] == IP6OPT_PAD1)
1692fe8fb19SBen Gras optlen = 1;
1702fe8fb19SBen Gras else
1712fe8fb19SBen Gras optlen = typep[1] + 2;
1722fe8fb19SBen Gras memcpy(bp, typep, (size_t)optlen);
1732fe8fb19SBen Gras bp += optlen;
174f14fb602SLionel Sambuc _DIAGASSERT(__type_fit(socklen_t, optlen + cmsg->cmsg_len));
175f14fb602SLionel Sambuc cmsg->cmsg_len += (socklen_t)optlen;
1762fe8fb19SBen Gras
1772fe8fb19SBen Gras /* calculate pad length after the option and insert the padding */
178f14fb602SLionel Sambuc off = bp - (uint8_t *)(void *)eh;
1792fe8fb19SBen Gras padlen = ((off + 7) & ~7) - off;
1802fe8fb19SBen Gras inet6_insert_padopt(bp, padlen);
1812fe8fb19SBen Gras bp += padlen;
182f14fb602SLionel Sambuc _DIAGASSERT(__type_fit(socklen_t, padlen + cmsg->cmsg_len));
183f14fb602SLionel Sambuc cmsg->cmsg_len += (socklen_t)padlen;
1842fe8fb19SBen Gras
1852fe8fb19SBen Gras /* update the length field of the ip6 option header */
186f14fb602SLionel Sambuc off = bp - (uint8_t *)(void *)eh;
187f14fb602SLionel Sambuc _DIAGASSERT(__type_fit(uint8_t, (off >> 3) - 1));
188f14fb602SLionel Sambuc eh->ip6e_len = (uint8_t)((off >> 3) - 1);
1892fe8fb19SBen Gras
1902fe8fb19SBen Gras return(0);
1912fe8fb19SBen Gras }
1922fe8fb19SBen Gras
1932fe8fb19SBen Gras /*
1942fe8fb19SBen Gras * This function appends a Hop-by-Hop option or a Destination option
1952fe8fb19SBen Gras * into an ancillary data object that has been initialized by
1962fe8fb19SBen Gras * inet6_option_init(). This function returns a pointer to the 8-bit
1972fe8fb19SBen Gras * option type field that starts the option on success, or NULL on an
1982fe8fb19SBen Gras * error.
1992fe8fb19SBen Gras * The difference between this function and inet6_option_append() is
2002fe8fb19SBen Gras * that the latter copies the contents of a previously built option into
2012fe8fb19SBen Gras * the ancillary data object while the current function returns a
2022fe8fb19SBen Gras * pointer to the space in the data object where the option's TLV must
2032fe8fb19SBen Gras * then be built by the caller.
2042fe8fb19SBen Gras *
2052fe8fb19SBen Gras */
206f14fb602SLionel Sambuc uint8_t *
inet6_option_alloc(struct cmsghdr * cmsg,int datalen,int multx,int plusy)207f14fb602SLionel Sambuc inet6_option_alloc(struct cmsghdr *cmsg, int datalen, int multx, int plusy)
2082fe8fb19SBen Gras {
2092fe8fb19SBen Gras size_t padlen, off;
210f14fb602SLionel Sambuc register uint8_t *bp;
211f14fb602SLionel Sambuc uint8_t *retval;
2122fe8fb19SBen Gras struct ip6_ext *eh;
2132fe8fb19SBen Gras
2142fe8fb19SBen Gras _DIAGASSERT(cmsg != NULL);
2152fe8fb19SBen Gras
216f14fb602SLionel Sambuc bp = (uint8_t *)(void *)cmsg + cmsg->cmsg_len;
2172fe8fb19SBen Gras eh = (struct ip6_ext *)(void *)CMSG_DATA(cmsg);
2182fe8fb19SBen Gras
2192fe8fb19SBen Gras /* argument validation */
2202fe8fb19SBen Gras if (multx != 1 && multx != 2 && multx != 4 && multx != 8)
2212fe8fb19SBen Gras return(NULL);
2222fe8fb19SBen Gras if (plusy < 0 || plusy > 7)
2232fe8fb19SBen Gras return(NULL);
2242fe8fb19SBen Gras
2252fe8fb19SBen Gras /*
2262fe8fb19SBen Gras * If this is the first option, allocate space for the
2272fe8fb19SBen Gras * first 2 bytes(for next header and length fields) of
2282fe8fb19SBen Gras * the option header.
2292fe8fb19SBen Gras */
230f14fb602SLionel Sambuc if (bp == (uint8_t *)(void *)eh) {
2312fe8fb19SBen Gras bp += 2;
2322fe8fb19SBen Gras cmsg->cmsg_len += 2;
2332fe8fb19SBen Gras }
2342fe8fb19SBen Gras
2352fe8fb19SBen Gras /* calculate pad length before the option. */
236f14fb602SLionel Sambuc off = bp - (uint8_t *)(void *)eh;
2372fe8fb19SBen Gras padlen = (((off % multx) + (multx - 1)) & ~(multx - 1)) -
2382fe8fb19SBen Gras (off % multx);
2392fe8fb19SBen Gras padlen += plusy;
2402fe8fb19SBen Gras padlen %= multx; /* keep the pad as short as possible */
2412fe8fb19SBen Gras /* insert padding */
2422fe8fb19SBen Gras inet6_insert_padopt(bp, padlen);
243f14fb602SLionel Sambuc cmsg->cmsg_len += (socklen_t)padlen;
2442fe8fb19SBen Gras bp += padlen;
2452fe8fb19SBen Gras
2462fe8fb19SBen Gras /* keep space to store specified length of data */
2472fe8fb19SBen Gras retval = bp;
2482fe8fb19SBen Gras bp += datalen;
2492fe8fb19SBen Gras cmsg->cmsg_len += datalen;
2502fe8fb19SBen Gras
2512fe8fb19SBen Gras /* calculate pad length after the option and insert the padding */
252f14fb602SLionel Sambuc off = bp - (uint8_t *)(void *)eh;
2532fe8fb19SBen Gras padlen = ((off + 7) & ~7) - off;
2542fe8fb19SBen Gras inet6_insert_padopt(bp, padlen);
2552fe8fb19SBen Gras bp += padlen;
256f14fb602SLionel Sambuc _DIAGASSERT(__type_fit(socklen_t, padlen + cmsg->cmsg_len));
257f14fb602SLionel Sambuc cmsg->cmsg_len += (socklen_t)padlen;
2582fe8fb19SBen Gras
2592fe8fb19SBen Gras /* update the length field of the ip6 option header */
260f14fb602SLionel Sambuc off = bp - (uint8_t *)(void *)eh;
261f14fb602SLionel Sambuc _DIAGASSERT(__type_fit(uint8_t, (off >> 3) - 1));
262f14fb602SLionel Sambuc eh->ip6e_len = (uint8_t)((off >> 3) - 1);
2632fe8fb19SBen Gras
2642fe8fb19SBen Gras return(retval);
2652fe8fb19SBen Gras }
2662fe8fb19SBen Gras
2672fe8fb19SBen Gras /*
2682fe8fb19SBen Gras * This function processes the next Hop-by-Hop option or Destination
2692fe8fb19SBen Gras * option in an ancillary data object. If another option remains to be
2702fe8fb19SBen Gras * processed, the return value of the function is 0 and *tptrp points to
2712fe8fb19SBen Gras * the 8-bit option type field (which is followed by the 8-bit option
2722fe8fb19SBen Gras * data length, followed by the option data). If no more options remain
2732fe8fb19SBen Gras * to be processed, the return value is -1 and *tptrp is NULL. If an
2742fe8fb19SBen Gras * error occurs, the return value is -1 and *tptrp is not NULL.
2752fe8fb19SBen Gras * (RFC 2292, 6.3.5)
2762fe8fb19SBen Gras */
2772fe8fb19SBen Gras int
inet6_option_next(const struct cmsghdr * cmsg,uint8_t ** tptrp)278f14fb602SLionel Sambuc inet6_option_next(const struct cmsghdr *cmsg, uint8_t **tptrp)
2792fe8fb19SBen Gras {
2802fe8fb19SBen Gras struct ip6_ext *ip6e;
2812fe8fb19SBen Gras int hdrlen, optlen;
282f14fb602SLionel Sambuc uint8_t *lim;
2832fe8fb19SBen Gras
2842fe8fb19SBen Gras _DIAGASSERT(cmsg != NULL);
2852fe8fb19SBen Gras _DIAGASSERT(tptrp != NULL);
2862fe8fb19SBen Gras
2872fe8fb19SBen Gras if (cmsg->cmsg_level != IPPROTO_IPV6 ||
2882fe8fb19SBen Gras (cmsg->cmsg_type != IPV6_HOPOPTS &&
2892fe8fb19SBen Gras cmsg->cmsg_type != IPV6_DSTOPTS))
2902fe8fb19SBen Gras return(-1);
2912fe8fb19SBen Gras
2922fe8fb19SBen Gras /* message length validation */
2932fe8fb19SBen Gras if (cmsg->cmsg_len < CMSG_SPACE(sizeof(struct ip6_ext)))
2942fe8fb19SBen Gras return(-1);
2952fe8fb19SBen Gras ip6e = __UNCONST(CCMSG_DATA(cmsg));
2962fe8fb19SBen Gras hdrlen = (ip6e->ip6e_len + 1) << 3;
2972fe8fb19SBen Gras if (cmsg->cmsg_len < CMSG_SPACE(hdrlen))
2982fe8fb19SBen Gras return(-1);
2992fe8fb19SBen Gras
3002fe8fb19SBen Gras /*
3012fe8fb19SBen Gras * If the caller does not specify the starting point,
3022fe8fb19SBen Gras * simply return the 1st option.
3032fe8fb19SBen Gras * Otherwise, search the option list for the next option.
3042fe8fb19SBen Gras */
305f14fb602SLionel Sambuc lim = (uint8_t *)(void *)ip6e + hdrlen;
3062fe8fb19SBen Gras if (*tptrp == NULL)
307f14fb602SLionel Sambuc *tptrp = (uint8_t *)(void *)(ip6e + 1);
3082fe8fb19SBen Gras else {
3092fe8fb19SBen Gras if ((optlen = ip6optlen(*tptrp, lim)) == 0)
3102fe8fb19SBen Gras return(-1);
3112fe8fb19SBen Gras
3122fe8fb19SBen Gras *tptrp = *tptrp + optlen;
3132fe8fb19SBen Gras }
3142fe8fb19SBen Gras if (*tptrp >= lim) { /* there is no option */
3152fe8fb19SBen Gras *tptrp = NULL;
3162fe8fb19SBen Gras return(-1);
3172fe8fb19SBen Gras }
3182fe8fb19SBen Gras /*
3192fe8fb19SBen Gras * Finally, checks if the next option is safely stored in the
3202fe8fb19SBen Gras * cmsg data.
3212fe8fb19SBen Gras */
3222fe8fb19SBen Gras if (ip6optlen(*tptrp, lim) == 0)
3232fe8fb19SBen Gras return(-1);
3242fe8fb19SBen Gras else
3252fe8fb19SBen Gras return(0);
3262fe8fb19SBen Gras }
3272fe8fb19SBen Gras
3282fe8fb19SBen Gras /*
3292fe8fb19SBen Gras * This function is similar to the inet6_option_next() function,
3302fe8fb19SBen Gras * except this function lets the caller specify the option type to be
3312fe8fb19SBen Gras * searched for, instead of always returning the next option in the
3322fe8fb19SBen Gras * ancillary data object.
333f14fb602SLionel Sambuc * Note: RFC 2292 says the type of tptrp is uint8_t *, but we think
334f14fb602SLionel Sambuc * it's a typo. The variable should be type of uint8_t **.
3352fe8fb19SBen Gras */
3362fe8fb19SBen Gras int
inet6_option_find(const struct cmsghdr * cmsg,uint8_t ** tptrp,int type)337f14fb602SLionel Sambuc inet6_option_find(const struct cmsghdr *cmsg, uint8_t **tptrp, int type)
3382fe8fb19SBen Gras {
3392fe8fb19SBen Gras struct ip6_ext *ip6e;
3402fe8fb19SBen Gras int hdrlen, optlen;
341f14fb602SLionel Sambuc uint8_t *optp, *lim;
3422fe8fb19SBen Gras
3432fe8fb19SBen Gras _DIAGASSERT(cmsg != NULL);
3442fe8fb19SBen Gras _DIAGASSERT(tptrp != NULL);
3452fe8fb19SBen Gras
3462fe8fb19SBen Gras if (cmsg->cmsg_level != IPPROTO_IPV6 ||
3472fe8fb19SBen Gras (cmsg->cmsg_type != IPV6_HOPOPTS &&
3482fe8fb19SBen Gras cmsg->cmsg_type != IPV6_DSTOPTS))
3492fe8fb19SBen Gras return(-1);
3502fe8fb19SBen Gras
3512fe8fb19SBen Gras /* message length validation */
3522fe8fb19SBen Gras if (cmsg->cmsg_len < CMSG_SPACE(sizeof(struct ip6_ext)))
3532fe8fb19SBen Gras return(-1);
3542fe8fb19SBen Gras ip6e = __UNCONST(CCMSG_DATA(cmsg));
3552fe8fb19SBen Gras hdrlen = (ip6e->ip6e_len + 1) << 3;
3562fe8fb19SBen Gras if (cmsg->cmsg_len < CMSG_SPACE(hdrlen))
3572fe8fb19SBen Gras return(-1);
3582fe8fb19SBen Gras
3592fe8fb19SBen Gras /*
3602fe8fb19SBen Gras * If the caller does not specify the starting point,
3612fe8fb19SBen Gras * search from the beginning of the option list.
3622fe8fb19SBen Gras * Otherwise, search from *the next option* of the specified point.
3632fe8fb19SBen Gras */
364f14fb602SLionel Sambuc lim = (uint8_t *)(void *)ip6e + hdrlen;
3652fe8fb19SBen Gras if (*tptrp == NULL)
366f14fb602SLionel Sambuc *tptrp = (uint8_t *)(void *)(ip6e + 1);
3672fe8fb19SBen Gras else {
3682fe8fb19SBen Gras if ((optlen = ip6optlen(*tptrp, lim)) == 0)
3692fe8fb19SBen Gras return(-1);
3702fe8fb19SBen Gras
3712fe8fb19SBen Gras *tptrp = *tptrp + optlen;
3722fe8fb19SBen Gras }
3732fe8fb19SBen Gras for (optp = *tptrp; optp < lim; optp += optlen) {
3742fe8fb19SBen Gras if (*optp == type) {
3752fe8fb19SBen Gras *tptrp = optp;
3762fe8fb19SBen Gras return(0);
3772fe8fb19SBen Gras }
3782fe8fb19SBen Gras if ((optlen = ip6optlen(optp, lim)) == 0)
3792fe8fb19SBen Gras return(-1);
3802fe8fb19SBen Gras }
3812fe8fb19SBen Gras
3822fe8fb19SBen Gras /* search failed */
3832fe8fb19SBen Gras *tptrp = NULL;
3842fe8fb19SBen Gras return(-1);
3852fe8fb19SBen Gras }
3862fe8fb19SBen Gras
3872fe8fb19SBen Gras /*
3882fe8fb19SBen Gras * Calculate the length of a given IPv6 option. Also checks
3892fe8fb19SBen Gras * if the option is safely stored in user's buffer according to the
3902fe8fb19SBen Gras * calculated length and the limitation of the buffer.
3912fe8fb19SBen Gras */
3922fe8fb19SBen Gras static int
ip6optlen(uint8_t * opt,uint8_t * lim)393f14fb602SLionel Sambuc ip6optlen(uint8_t *opt, uint8_t *lim)
3942fe8fb19SBen Gras {
3952fe8fb19SBen Gras int optlen;
3962fe8fb19SBen Gras
3972fe8fb19SBen Gras _DIAGASSERT(opt != NULL);
3982fe8fb19SBen Gras _DIAGASSERT(lim != NULL);
3992fe8fb19SBen Gras
4002fe8fb19SBen Gras if (*opt == IP6OPT_PAD1)
4012fe8fb19SBen Gras optlen = 1;
4022fe8fb19SBen Gras else {
4032fe8fb19SBen Gras /* is there enough space to store type and len? */
4042fe8fb19SBen Gras if (opt + 2 > lim)
4052fe8fb19SBen Gras return(0);
4062fe8fb19SBen Gras optlen = *(opt + 1) + 2;
4072fe8fb19SBen Gras }
4082fe8fb19SBen Gras if (opt + optlen <= lim)
4092fe8fb19SBen Gras return(optlen);
4102fe8fb19SBen Gras
4112fe8fb19SBen Gras return(0);
4122fe8fb19SBen Gras }
4132fe8fb19SBen Gras
4142fe8fb19SBen Gras static void
inet6_insert_padopt(uint8_t * p,size_t len)415f14fb602SLionel Sambuc inet6_insert_padopt(uint8_t *p, size_t len)
4162fe8fb19SBen Gras {
4172fe8fb19SBen Gras
4182fe8fb19SBen Gras _DIAGASSERT(p != NULL);
4192fe8fb19SBen Gras
4202fe8fb19SBen Gras switch(len) {
4212fe8fb19SBen Gras case 0:
4222fe8fb19SBen Gras return;
4232fe8fb19SBen Gras case 1:
4242fe8fb19SBen Gras p[0] = IP6OPT_PAD1;
4252fe8fb19SBen Gras return;
4262fe8fb19SBen Gras default:
4272fe8fb19SBen Gras p[0] = IP6OPT_PADN;
428f14fb602SLionel Sambuc _DIAGASSERT(__type_fit(uint8_t, len - 2));
429f14fb602SLionel Sambuc p[1] = (uint8_t)(len - 2);
4302fe8fb19SBen Gras memset(&p[2], 0, len - 2);
4312fe8fb19SBen Gras return;
4322fe8fb19SBen Gras }
4332fe8fb19SBen Gras }
4342fe8fb19SBen Gras
4352fe8fb19SBen Gras /*
4362fe8fb19SBen Gras * The following functions are defined in RFC3542, which is a successor
4372fe8fb19SBen Gras * of RFC2292.
4382fe8fb19SBen Gras */
4392fe8fb19SBen Gras
4402fe8fb19SBen Gras int
inet6_opt_init(void * extbuf,socklen_t extlen)4412fe8fb19SBen Gras inet6_opt_init(void *extbuf, socklen_t extlen)
4422fe8fb19SBen Gras {
4432fe8fb19SBen Gras struct ip6_ext *ext = (struct ip6_ext *)extbuf;
4442fe8fb19SBen Gras
4452fe8fb19SBen Gras if (ext) {
446*0a6a1f1dSLionel Sambuc if (extlen == 0 || (extlen % 8))
4472fe8fb19SBen Gras return (-1);
4482fe8fb19SBen Gras ext->ip6e_len = (extlen >> 3) - 1;
4492fe8fb19SBen Gras }
4502fe8fb19SBen Gras
4512fe8fb19SBen Gras return (2); /* sizeof the next and the length fields */
4522fe8fb19SBen Gras }
4532fe8fb19SBen Gras
4542fe8fb19SBen Gras int
inet6_opt_append(void * extbuf,socklen_t extlen,int offset,uint8_t type,socklen_t len,uint8_t align,void ** databufp)455f14fb602SLionel Sambuc inet6_opt_append(void *extbuf, socklen_t extlen, int offset, uint8_t type,
456f14fb602SLionel Sambuc socklen_t len, uint8_t align, void **databufp)
4572fe8fb19SBen Gras {
4582fe8fb19SBen Gras int currentlen = offset;
4592fe8fb19SBen Gras size_t padlen = 0;
4602fe8fb19SBen Gras
4612fe8fb19SBen Gras /*
4622fe8fb19SBen Gras * The option type must have a value from 2 to 255, inclusive.
4632fe8fb19SBen Gras * (0 and 1 are reserved for the Pad1 and PadN options, respectively.)
4642fe8fb19SBen Gras */
4652fe8fb19SBen Gras if (type < 2)
4662fe8fb19SBen Gras return (-1);
4672fe8fb19SBen Gras
4682fe8fb19SBen Gras /*
4692fe8fb19SBen Gras * The option data length must have a value between 0 and 255,
4702fe8fb19SBen Gras * inclusive, and is the length of the option data that follows.
4712fe8fb19SBen Gras */
4722fe8fb19SBen Gras if (len > 255)
4732fe8fb19SBen Gras return (-1);
4742fe8fb19SBen Gras
4752fe8fb19SBen Gras /*
4762fe8fb19SBen Gras * The align parameter must have a value of 1, 2, 4, or 8.
4772fe8fb19SBen Gras * The align value can not exceed the value of len.
4782fe8fb19SBen Gras */
4792fe8fb19SBen Gras if (align != 1 && align != 2 && align != 4 && align != 8)
4802fe8fb19SBen Gras return (-1);
4812fe8fb19SBen Gras if (align > len)
4822fe8fb19SBen Gras return (-1);
4832fe8fb19SBen Gras
4842fe8fb19SBen Gras /* Calculate the padding length. */
4852fe8fb19SBen Gras currentlen += 2 + len; /* 2 means "type + len" */
4862fe8fb19SBen Gras if (currentlen % align)
4872fe8fb19SBen Gras padlen = align - (currentlen % align);
4882fe8fb19SBen Gras
4892fe8fb19SBen Gras /* The option must fit in the extension header buffer. */
490f14fb602SLionel Sambuc _DIAGASSERT(__type_fit(int, currentlen + padlen));
491f14fb602SLionel Sambuc currentlen += (int)padlen;
4922fe8fb19SBen Gras if (extlen && /* XXX: right? */
4932fe8fb19SBen Gras (socklen_t)currentlen > extlen)
4942fe8fb19SBen Gras return (-1);
4952fe8fb19SBen Gras
4962fe8fb19SBen Gras if (extbuf) {
497f14fb602SLionel Sambuc uint8_t *optp = (uint8_t *)extbuf + offset;
4982fe8fb19SBen Gras
4992fe8fb19SBen Gras if (padlen == 1) {
5002fe8fb19SBen Gras /* insert a Pad1 option */
5012fe8fb19SBen Gras *optp = IP6OPT_PAD1;
5022fe8fb19SBen Gras optp++;
5032fe8fb19SBen Gras } else if (padlen > 0) {
5042fe8fb19SBen Gras /* insert a PadN option for alignment */
5052fe8fb19SBen Gras *optp++ = IP6OPT_PADN;
506f14fb602SLionel Sambuc _DIAGASSERT(__type_fit(uint8_t, padlen - 2));
507f14fb602SLionel Sambuc *optp++ = (uint8_t)(padlen - 2);
5082fe8fb19SBen Gras memset(optp, 0, padlen - 2);
5092fe8fb19SBen Gras optp += (padlen - 2);
5102fe8fb19SBen Gras }
5112fe8fb19SBen Gras
5122fe8fb19SBen Gras *optp++ = type;
5132fe8fb19SBen Gras *optp++ = len;
5142fe8fb19SBen Gras
5152fe8fb19SBen Gras *databufp = optp;
5162fe8fb19SBen Gras }
5172fe8fb19SBen Gras
5182fe8fb19SBen Gras return (currentlen);
5192fe8fb19SBen Gras }
5202fe8fb19SBen Gras
5212fe8fb19SBen Gras int
inet6_opt_finish(void * extbuf,socklen_t extlen,int offset)5222fe8fb19SBen Gras inet6_opt_finish(void *extbuf, socklen_t extlen, int offset)
5232fe8fb19SBen Gras {
5242fe8fb19SBen Gras int updatelen = offset > 0 ? (1 + ((offset - 1) | 7)) : 0;
5252fe8fb19SBen Gras
5262fe8fb19SBen Gras if (extbuf) {
527f14fb602SLionel Sambuc uint8_t *padp;
5282fe8fb19SBen Gras size_t padlen = updatelen - offset;
5292fe8fb19SBen Gras
530f14fb602SLionel Sambuc if ((socklen_t)updatelen > extlen || padlen >= 256 + 2)
5312fe8fb19SBen Gras return (-1);
5322fe8fb19SBen Gras
533f14fb602SLionel Sambuc padp = (uint8_t *)extbuf + offset;
5342fe8fb19SBen Gras if (padlen == 1)
5352fe8fb19SBen Gras *padp = IP6OPT_PAD1;
5362fe8fb19SBen Gras else if (padlen > 0) {
5372fe8fb19SBen Gras *padp++ = IP6OPT_PADN;
538f14fb602SLionel Sambuc *padp++ = (uint8_t)(padlen - 2);
5392fe8fb19SBen Gras memset(padp, 0, padlen - 2);
5402fe8fb19SBen Gras }
5412fe8fb19SBen Gras }
5422fe8fb19SBen Gras
5432fe8fb19SBen Gras return (updatelen);
5442fe8fb19SBen Gras }
5452fe8fb19SBen Gras
5462fe8fb19SBen Gras int
inet6_opt_set_val(void * databuf,int offset,void * val,socklen_t vallen)5472fe8fb19SBen Gras inet6_opt_set_val(void *databuf, int offset, void *val, socklen_t vallen)
5482fe8fb19SBen Gras {
5492fe8fb19SBen Gras
550f14fb602SLionel Sambuc memcpy((uint8_t *)databuf + offset, val, vallen);
5512fe8fb19SBen Gras return (offset + vallen);
5522fe8fb19SBen Gras }
5532fe8fb19SBen Gras
5542fe8fb19SBen Gras int
inet6_opt_next(void * extbuf,socklen_t extlen,int offset,uint8_t * typep,socklen_t * lenp,void ** databufp)555f14fb602SLionel Sambuc inet6_opt_next(void *extbuf, socklen_t extlen, int offset, uint8_t *typep,
5562fe8fb19SBen Gras socklen_t *lenp, void **databufp)
5572fe8fb19SBen Gras {
558f14fb602SLionel Sambuc uint8_t *optp, *lim;
5592fe8fb19SBen Gras int optlen;
5602fe8fb19SBen Gras
5612fe8fb19SBen Gras /* Validate extlen. XXX: is the variable really necessary?? */
5622fe8fb19SBen Gras if (extlen == 0 || (extlen % 8))
5632fe8fb19SBen Gras return (-1);
564f14fb602SLionel Sambuc lim = (uint8_t *)extbuf + extlen;
5652fe8fb19SBen Gras
5662fe8fb19SBen Gras /*
5672fe8fb19SBen Gras * If this is the first time this function called for this options
5682fe8fb19SBen Gras * header, simply return the 1st option.
5692fe8fb19SBen Gras * Otherwise, search the option list for the next option.
5702fe8fb19SBen Gras */
5712fe8fb19SBen Gras if (offset == 0)
572f14fb602SLionel Sambuc optp = (uint8_t *)(void *)((struct ip6_hbh *)extbuf + 1);
5732fe8fb19SBen Gras else
574f14fb602SLionel Sambuc optp = (uint8_t *)extbuf + offset;
5752fe8fb19SBen Gras
5762fe8fb19SBen Gras /* Find the next option skipping any padding options. */
5772fe8fb19SBen Gras while (optp < lim) {
578f14fb602SLionel Sambuc ptrdiff_t rv;
5792fe8fb19SBen Gras switch(*optp) {
5802fe8fb19SBen Gras case IP6OPT_PAD1:
5812fe8fb19SBen Gras optp++;
5822fe8fb19SBen Gras break;
5832fe8fb19SBen Gras case IP6OPT_PADN:
5842fe8fb19SBen Gras if ((optlen = ip6optlen(optp, lim)) == 0)
5852fe8fb19SBen Gras goto optend;
5862fe8fb19SBen Gras optp += optlen;
5872fe8fb19SBen Gras break;
5882fe8fb19SBen Gras default: /* found */
5892fe8fb19SBen Gras if ((optlen = ip6optlen(optp, lim)) == 0)
5902fe8fb19SBen Gras goto optend;
5912fe8fb19SBen Gras *typep = *optp;
5922fe8fb19SBen Gras *lenp = optlen - 2;
5932fe8fb19SBen Gras *databufp = optp + 2;
594f14fb602SLionel Sambuc rv = optp + optlen - (uint8_t *)extbuf;
595f14fb602SLionel Sambuc _DIAGASSERT(__type_fit(int, rv));
596f14fb602SLionel Sambuc return (int)rv;
5972fe8fb19SBen Gras }
5982fe8fb19SBen Gras }
5992fe8fb19SBen Gras
6002fe8fb19SBen Gras optend:
6012fe8fb19SBen Gras *databufp = NULL; /* for safety */
6022fe8fb19SBen Gras return (-1);
6032fe8fb19SBen Gras }
6042fe8fb19SBen Gras
6052fe8fb19SBen Gras int
inet6_opt_find(void * extbuf,socklen_t extlen,int offset,uint8_t type,socklen_t * lenp,void ** databufp)606f14fb602SLionel Sambuc inet6_opt_find(void *extbuf, socklen_t extlen, int offset, uint8_t type,
6072fe8fb19SBen Gras socklen_t *lenp, void **databufp)
6082fe8fb19SBen Gras {
609f14fb602SLionel Sambuc uint8_t *optp, *lim;
6102fe8fb19SBen Gras int optlen;
6112fe8fb19SBen Gras
6122fe8fb19SBen Gras /* Validate extlen. XXX: is the variable really necessary?? */
6132fe8fb19SBen Gras if (extlen == 0 || (extlen % 8))
6142fe8fb19SBen Gras return (-1);
615f14fb602SLionel Sambuc lim = (uint8_t *)extbuf + extlen;
6162fe8fb19SBen Gras
6172fe8fb19SBen Gras /*
6182fe8fb19SBen Gras * If this is the first time this function called for this options
6192fe8fb19SBen Gras * header, simply return the 1st option.
6202fe8fb19SBen Gras * Otherwise, search the option list for the next option.
6212fe8fb19SBen Gras */
6222fe8fb19SBen Gras if (offset == 0)
623f14fb602SLionel Sambuc optp = (uint8_t *)(void *)((struct ip6_hbh *)extbuf + 1);
6242fe8fb19SBen Gras else
625f14fb602SLionel Sambuc optp = (uint8_t *)extbuf + offset;
6262fe8fb19SBen Gras
6272fe8fb19SBen Gras /* Find the specified option */
6282fe8fb19SBen Gras while (optp < lim) {
6292fe8fb19SBen Gras if ((optlen = ip6optlen(optp, lim)) == 0)
6302fe8fb19SBen Gras goto optend;
6312fe8fb19SBen Gras
6322fe8fb19SBen Gras if (*optp == type) { /* found */
633f14fb602SLionel Sambuc ptrdiff_t td;
6342fe8fb19SBen Gras *lenp = optlen - 2;
6352fe8fb19SBen Gras *databufp = optp + 2;
636f14fb602SLionel Sambuc td = optp + optlen - (uint8_t *)extbuf;
637f14fb602SLionel Sambuc _DIAGASSERT(__type_fit(int, td));
638f14fb602SLionel Sambuc return (int)td;
6392fe8fb19SBen Gras }
6402fe8fb19SBen Gras
6412fe8fb19SBen Gras optp += optlen;
6422fe8fb19SBen Gras }
6432fe8fb19SBen Gras
6442fe8fb19SBen Gras optend:
6452fe8fb19SBen Gras *databufp = NULL; /* for safety */
6462fe8fb19SBen Gras return (-1);
6472fe8fb19SBen Gras }
6482fe8fb19SBen Gras
6492fe8fb19SBen Gras int
inet6_opt_get_val(void * databuf,int offset,void * val,socklen_t vallen)6502fe8fb19SBen Gras inet6_opt_get_val(void *databuf, int offset, void *val, socklen_t vallen)
6512fe8fb19SBen Gras {
6522fe8fb19SBen Gras
6532fe8fb19SBen Gras /* we can't assume alignment here */
654f14fb602SLionel Sambuc memcpy(val, (uint8_t *)databuf + offset, vallen);
6552fe8fb19SBen Gras
6562fe8fb19SBen Gras return (offset + vallen);
6572fe8fb19SBen Gras }
658