Home | History | Annotate | Line # | Download | only in net
ip6opt.c revision 1.6
      1 /*	$NetBSD: ip6opt.c,v 1.6 2000/04/24 09:27:31 itojun Exp $	*/
      2 
      3 /*
      4  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. Neither the name of the project nor the names of its contributors
     16  *    may be used to endorse or promote products derived from this software
     17  *    without specific prior written permission.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
     20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
     23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29  * SUCH DAMAGE.
     30  */
     31 
     32 #include "namespace.h"
     33 #include <sys/param.h>
     34 #include <sys/types.h>
     35 #include <sys/socket.h>
     36 
     37 #include <netinet/in.h>
     38 #include <netinet/ip6.h>
     39 
     40 #include <assert.h>
     41 #include <string.h>
     42 #include <stdio.h>
     43 
     44 #ifdef __weak_alias
     45 __weak_alias(inet6_option_alloc,_inet6_option_alloc)
     46 __weak_alias(inet6_option_append,_inet6_option_append)
     47 __weak_alias(inet6_option_find,_inet6_option_find)
     48 __weak_alias(inet6_option_init,_inet6_option_init)
     49 __weak_alias(inet6_option_next,_inet6_option_next)
     50 __weak_alias(inet6_option_space,_inet6_option_space)
     51 #endif
     52 
     53 static int ip6optlen(u_int8_t *opt, u_int8_t *lim);
     54 static void inet6_insert_padopt(u_char *p, size_t len);
     55 
     56 /*
     57  * This function returns the number of bytes required to hold an option
     58  * when it is stored as ancillary data, including the cmsghdr structure
     59  * at the beginning, and any padding at the end (to make its size a
     60  * multiple of 8 bytes).  The argument is the size of the structure
     61  * defining the option, which must include any pad bytes at the
     62  * beginning (the value y in the alignment term "xn + y"), the type
     63  * byte, the length byte, and the option data.
     64  */
     65 int
     66 inet6_option_space(nbytes)
     67 	int nbytes;
     68 {
     69 	nbytes += 2;	/* we need space for nxt-hdr and length fields */
     70 	return(CMSG_SPACE((nbytes + 7) & ~7));
     71 }
     72 
     73 /*
     74  * This function is called once per ancillary data object that will
     75  * contain either Hop-by-Hop or Destination options.  It returns 0 on
     76  * success or -1 on an error.
     77  */
     78 int
     79 inet6_option_init(bp, cmsgp, type)
     80 	void *bp;
     81 	struct cmsghdr **cmsgp;
     82 	int type;
     83 {
     84 	register struct cmsghdr *ch;
     85 
     86 	_DIAGASSERT(bp != NULL);
     87 	_DIAGASSERT(cmsgp != NULL);
     88 
     89 	ch = (struct cmsghdr *)bp;
     90 
     91 	/* argument validation */
     92 	if (type != IPV6_HOPOPTS && type != IPV6_DSTOPTS)
     93 		return(-1);
     94 
     95 	ch->cmsg_level = IPPROTO_IPV6;
     96 	ch->cmsg_type = type;
     97 	ch->cmsg_len = CMSG_LEN(0);
     98 
     99 	*cmsgp = ch;
    100 	return(0);
    101 }
    102 
    103 /*
    104  * This function appends a Hop-by-Hop option or a Destination option
    105  * into an ancillary data object that has been initialized by
    106  * inet6_option_init().  This function returns 0 if it succeeds or -1 on
    107  * an error.
    108  * multx is the value x in the alignment term "xn + y" described
    109  * earlier.  It must have a value of 1, 2, 4, or 8.
    110  * plusy is the value y in the alignment term "xn + y" described
    111  * earlier.  It must have a value between 0 and 7, inclusive.
    112  */
    113 int
    114 inet6_option_append(cmsg, typep, multx, plusy)
    115 	struct cmsghdr *cmsg;
    116 	const u_int8_t *typep;
    117 	int multx;
    118 	int plusy;
    119 {
    120 	size_t padlen, optlen, off;
    121 	register u_char *bp;
    122 	struct ip6_ext *eh;
    123 
    124 	_DIAGASSERT(cmsg != NULL);
    125 	_DIAGASSERT(typep != NULL);
    126 
    127 	bp = (u_char *)cmsg + cmsg->cmsg_len;
    128 	eh = (struct ip6_ext *)CMSG_DATA(cmsg);
    129 
    130 	/* argument validation */
    131 	if (multx != 1 && multx != 2 && multx != 4 && multx != 8)
    132 		return(-1);
    133 	if (plusy < 0 || plusy > 7)
    134 		return(-1);
    135 	if (typep[0] > 255)
    136 		return(-1);
    137 
    138 	/*
    139 	 * If this is the first option, allocate space for the
    140 	 * first 2 bytes(for next header and length fields) of
    141 	 * the option header.
    142 	 */
    143 	if (bp == (u_char *)eh) {
    144 		bp += 2;
    145 		cmsg->cmsg_len += 2;
    146 	}
    147 
    148 	/* calculate pad length before the option. */
    149 	off = bp - (u_char *)eh;
    150 	padlen = (((off % multx) + (multx - 1)) & ~(multx - 1)) -
    151 		(off % multx);
    152 	padlen += plusy;
    153 	/* insert padding */
    154 	inet6_insert_padopt(bp, padlen);
    155 	cmsg->cmsg_len += padlen;
    156 	bp += padlen;
    157 
    158 	/* copy the option */
    159 	if (typep[0] == IP6OPT_PAD1)
    160 		optlen = 1;
    161 	else
    162 		optlen = typep[1] + 2;
    163 	memcpy(bp, typep, (size_t)optlen);
    164 	bp += optlen;
    165 	cmsg->cmsg_len += optlen;
    166 
    167 	/* calculate pad length after the option and insert the padding */
    168 	off = bp - (u_char *)eh;
    169 	padlen = ((off + 7) & ~7) - off;
    170 	inet6_insert_padopt(bp, padlen);
    171 	bp += padlen;
    172 	cmsg->cmsg_len += padlen;
    173 
    174 	/* update the length field of the ip6 option header */
    175 	off = bp - (u_char *)eh;
    176 	eh->ip6e_len = (off >> 3) - 1;
    177 
    178 	return(0);
    179 }
    180 
    181 /*
    182  * This function appends a Hop-by-Hop option or a Destination option
    183  * into an ancillary data object that has been initialized by
    184  * inet6_option_init().  This function returns a pointer to the 8-bit
    185  * option type field that starts the option on success, or NULL on an
    186  * error.
    187  * The difference between this function and inet6_option_append() is
    188  * that the latter copies the contents of a previously built option into
    189  * the ancillary data object while the current function returns a
    190  * pointer to the space in the data object where the option's TLV must
    191  * then be built by the caller.
    192  *
    193  */
    194 u_int8_t *
    195 inet6_option_alloc(cmsg, datalen, multx, plusy)
    196 	struct cmsghdr *cmsg;
    197 	int datalen;
    198 	int multx;
    199 	int plusy;
    200 {
    201 	size_t padlen, off;
    202 	register u_int8_t *bp;
    203 	u_int8_t *retval;
    204 	struct ip6_ext *eh;
    205 
    206 	_DIAGASSERT(cmsg != NULL);
    207 
    208 	bp = (u_char *)cmsg + cmsg->cmsg_len;
    209 	eh = (struct ip6_ext *)CMSG_DATA(cmsg);
    210 
    211 	/* argument validation */
    212 	if (multx != 1 && multx != 2 && multx != 4 && multx != 8)
    213 		return(NULL);
    214 	if (plusy < 0 || plusy > 7)
    215 		return(NULL);
    216 
    217 	/*
    218 	 * If this is the first option, allocate space for the
    219 	 * first 2 bytes(for next header and length fields) of
    220 	 * the option header.
    221 	 */
    222 	if (bp == (u_char *)eh) {
    223 		bp += 2;
    224 		cmsg->cmsg_len += 2;
    225 	}
    226 
    227 	/* calculate pad length before the option. */
    228 	off = bp - (u_char *)eh;
    229 	padlen = (((off % multx) + (multx - 1)) & ~(multx - 1)) -
    230 		(off % multx);
    231 	padlen += plusy;
    232 	/* insert padding */
    233 	inet6_insert_padopt(bp, padlen);
    234 	cmsg->cmsg_len += padlen;
    235 	bp += padlen;
    236 
    237 	/* keep space to store specified length of data */
    238 	retval = bp;
    239 	bp += datalen;
    240 	cmsg->cmsg_len += datalen;
    241 
    242 	/* calculate pad length after the option and insert the padding */
    243 	off = bp - (u_char *)eh;
    244 	padlen = ((off + 7) & ~7) - off;
    245 	inet6_insert_padopt(bp, padlen);
    246 	bp += padlen;
    247 	cmsg->cmsg_len += padlen;
    248 
    249 	/* update the length field of the ip6 option header */
    250 	off = bp - (u_char *)eh;
    251 	eh->ip6e_len = (off >> 3) - 1;
    252 
    253 	return(retval);
    254 }
    255 
    256 /*
    257  * This function processes the next Hop-by-Hop option or Destination
    258  * option in an ancillary data object.  If another option remains to be
    259  * processed, the return value of the function is 0 and *tptrp points to
    260  * the 8-bit option type field (which is followed by the 8-bit option
    261  * data length, followed by the option data).  If no more options remain
    262  * to be processed, the return value is -1 and *tptrp is NULL.  If an
    263  * error occurs, the return value is -1 and *tptrp is not NULL.
    264  * (RFC 2292, 6.3.5)
    265  */
    266 int
    267 inet6_option_next(cmsg, tptrp)
    268 	const struct cmsghdr *cmsg;
    269 	u_int8_t **tptrp;
    270 {
    271 	struct ip6_ext *ip6e;
    272 	int hdrlen, optlen;
    273 	u_int8_t *lim;
    274 
    275 	_DIAGASSERT(cmsg != NULL);
    276 	_DIAGASSERT(tptrp != NULL);
    277 
    278 	if (cmsg->cmsg_level != IPPROTO_IPV6 ||
    279 	    (cmsg->cmsg_type != IPV6_HOPOPTS &&
    280 	     cmsg->cmsg_type != IPV6_DSTOPTS))
    281 		return(-1);
    282 
    283 	/* message length validation */
    284 	if (cmsg->cmsg_len < CMSG_SPACE(sizeof(struct ip6_ext)))
    285 		return(-1);
    286 	ip6e = (struct ip6_ext *)CMSG_DATA(cmsg);
    287 	hdrlen = (ip6e->ip6e_len + 1) << 3;
    288 	if (cmsg->cmsg_len < CMSG_SPACE(hdrlen))
    289 		return(-1);
    290 
    291 	/*
    292 	 * If the caller does not specify the starting point,
    293 	 * simply return the 1st option.
    294 	 * Otherwise, search the option list for the next option.
    295 	 */
    296 	lim = (u_int8_t *)ip6e + hdrlen;
    297 	if (*tptrp == NULL)
    298 		*tptrp = (u_int8_t *)(ip6e + 1);
    299 	else {
    300 		if ((optlen = ip6optlen(*tptrp, lim)) == 0)
    301 			return(-1);
    302 
    303 		*tptrp = *tptrp + optlen;
    304 	}
    305 	if (*tptrp >= lim) {	/* there is no option */
    306 		*tptrp = NULL;
    307 		return(-1);
    308 	}
    309 	/*
    310 	 * Finally, checks if the next option is safely stored in the
    311 	 * cmsg data.
    312 	 */
    313 	if (ip6optlen(*tptrp, lim) == 0)
    314 		return(-1);
    315 	else
    316 		return(0);
    317 }
    318 
    319 /*
    320  * This function is similar to the inet6_option_next() function,
    321  * except this function lets the caller specify the option type to be
    322  * searched for, instead of always returning the next option in the
    323  * ancillary data object.
    324  * Note: RFC 2292 says the type of tptrp is u_int8_t *, but we think
    325  *       it's a typo. The variable should be type of u_int8_t **.
    326  */
    327 int
    328 inet6_option_find(cmsg, tptrp, type)
    329 	const struct cmsghdr *cmsg;
    330 	u_int8_t **tptrp;
    331 	int type;
    332 {
    333 	struct ip6_ext *ip6e;
    334 	int hdrlen, optlen;
    335 	u_int8_t *optp, *lim;
    336 
    337 	_DIAGASSERT(cmsg != NULL);
    338 	_DIAGASSERT(tptrp != NULL);
    339 
    340 	if (cmsg->cmsg_level != IPPROTO_IPV6 ||
    341 	    (cmsg->cmsg_type != IPV6_HOPOPTS &&
    342 	     cmsg->cmsg_type != IPV6_DSTOPTS))
    343 		return(-1);
    344 
    345 	/* message length validation */
    346 	if (cmsg->cmsg_len < CMSG_SPACE(sizeof(struct ip6_ext)))
    347 		return(-1);
    348 	ip6e = (struct ip6_ext *)CMSG_DATA(cmsg);
    349 	hdrlen = (ip6e->ip6e_len + 1) << 3;
    350 	if (cmsg->cmsg_len < CMSG_SPACE(hdrlen))
    351 		return(-1);
    352 
    353 	/*
    354 	 * If the caller does not specify the starting point,
    355 	 * search from the beginning of the option list.
    356 	 * Otherwise, search from *the next option* of the specified point.
    357 	 */
    358 	lim = (u_int8_t *)ip6e + hdrlen;
    359 	if (*tptrp == NULL)
    360 		*tptrp = (u_int8_t *)(ip6e + 1);
    361 	else {
    362 		if ((optlen = ip6optlen(*tptrp, lim)) == 0)
    363 			return(-1);
    364 
    365 		*tptrp = *tptrp + optlen;
    366 	}
    367 	for (optp = *tptrp; optp < lim; optp += optlen) {
    368 		if (*optp == type) {
    369 			*tptrp = optp;
    370 			return(0);
    371 		}
    372 		if ((optlen = ip6optlen(optp, lim)) == 0)
    373 			return(-1);
    374 	}
    375 
    376 	/* search failed */
    377 	*tptrp = NULL;
    378 	return(-1);
    379 }
    380 
    381 /*
    382  * Calculate the length of a given IPv6 option. Also checks
    383  * if the option is safely stored in user's buffer according to the
    384  * calculated length and the limitation of the buffer.
    385  */
    386 static int
    387 ip6optlen(opt, lim)
    388 	u_int8_t *opt, *lim;
    389 {
    390 	int optlen;
    391 
    392 	_DIAGASSERT(opt != NULL);
    393 	_DIAGASSERT(lim != NULL);
    394 
    395 	if (*opt == IP6OPT_PAD1)
    396 		optlen = 1;
    397 	else {
    398 		/* is there enough space to store type and len? */
    399 		if (opt + 2 > lim)
    400 			return(0);
    401 		optlen = *(opt + 1) + 2;
    402 	}
    403 	if (opt + optlen <= lim)
    404 		return(optlen);
    405 
    406 	return(0);
    407 }
    408 
    409 static void
    410 inet6_insert_padopt(u_char *p, size_t len)
    411 {
    412 
    413 	_DIAGASSERT(p != NULL);
    414 
    415 	switch(len) {
    416 	 case 0:
    417 		 return;
    418 	 case 1:
    419 		 p[0] = IP6OPT_PAD1;
    420 		 return;
    421 	 default:
    422 		 p[0] = IP6OPT_PADN;
    423 		 p[1] = len - 2;
    424 		 memset(&p[2], 0, len - 2);
    425 		 return;
    426 	}
    427 }
    428