Home | History | Annotate | Line # | Download | only in net
ip6opt.c revision 1.3
      1 /*	$NetBSD: ip6opt.c,v 1.3 1999/09/16 11:45:15 lukem 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 <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 <assert.h>
     40 #include <string.h>
     41 #include <stdio.h>
     42 
     43 static int ip6optlen(u_int8_t *opt, u_int8_t *lim);
     44 static void inet6_insert_padopt(u_char *p, int len);
     45 
     46 /*
     47  * This function returns the number of bytes required to hold an option
     48  * when it is stored as ancillary data, including the cmsghdr structure
     49  * at the beginning, and any padding at the end (to make its size a
     50  * multiple of 8 bytes).  The argument is the size of the structure
     51  * defining the option, which must include any pad bytes at the
     52  * beginning (the value y in the alignment term "xn + y"), the type
     53  * byte, the length byte, and the option data.
     54  */
     55 int
     56 inet6_option_space(nbytes)
     57 	int nbytes;
     58 {
     59 	nbytes += 2;	/* we need space for nxt-hdr and length fields */
     60 	return(CMSG_SPACE((nbytes + 7) & ~7));
     61 }
     62 
     63 /*
     64  * This function is called once per ancillary data object that will
     65  * contain either Hop-by-Hop or Destination options.  It returns 0 on
     66  * success or -1 on an error.
     67  */
     68 int
     69 inet6_option_init(bp, cmsgp, type)
     70 	void *bp;
     71 	struct cmsghdr **cmsgp;
     72 	int type;
     73 {
     74 	register struct cmsghdr *ch;
     75 
     76 	_DIAGASSERT(bp != NULL);
     77 	_DIAGASSERT(cmsgp != NULL);
     78 #ifdef _DIAGNOSTIC
     79 	if (bp == NULL || cmsgp == NULL)
     80 		return (-1);
     81 #endif
     82 
     83 	ch = (struct cmsghdr *)bp;
     84 
     85 	/* argument validation */
     86 	if (type != IPV6_HOPOPTS && type != IPV6_DSTOPTS)
     87 		return(-1);
     88 
     89 	ch->cmsg_level = IPPROTO_IPV6;
     90 	ch->cmsg_type = type;
     91 	ch->cmsg_len = CMSG_LEN(0);
     92 
     93 	*cmsgp = ch;
     94 	return(0);
     95 }
     96 
     97 /*
     98  * This function appends a Hop-by-Hop option or a Destination option
     99  * into an ancillary data object that has been initialized by
    100  * inet6_option_init().  This function returns 0 if it succeeds or -1 on
    101  * an error.
    102  * multx is the value x in the alignment term "xn + y" described
    103  * earlier.  It must have a value of 1, 2, 4, or 8.
    104  * plusy is the value y in the alignment term "xn + y" described
    105  * earlier.  It must have a value between 0 and 7, inclusive.
    106  */
    107 int
    108 inet6_option_append(cmsg, typep, multx, plusy)
    109 	struct cmsghdr *cmsg;
    110 	const u_int8_t *typep;
    111 	int multx;
    112 	int plusy;
    113 {
    114 	int padlen, optlen, off;
    115 	register u_char *bp;
    116 	struct ip6_ext *eh;
    117 
    118 	_DIAGASSERT(cmsg != NULL);
    119 	_DIAGASSERT(typep != NULL);
    120 #ifdef _DIAGNOSTIC
    121 	if (cmsg == NULL || typep == NULL)
    122 		return (-1);
    123 #endif
    124 
    125 	bp = (u_char *)cmsg + cmsg->cmsg_len;
    126 	eh = (struct ip6_ext *)CMSG_DATA(cmsg);
    127 
    128 	/* argument validation */
    129 	if (multx != 1 && multx != 2 && multx != 4 && multx != 8)
    130 		return(-1);
    131 	if (plusy < 0 || plusy > 7)
    132 		return(-1);
    133 	if (typep[0] > 255)
    134 		return(-1);
    135 
    136 	/*
    137 	 * If this is the first option, allocate space for the
    138 	 * first 2 bytes(for next header and length fields) of
    139 	 * the option header.
    140 	 */
    141 	if (bp == (u_char *)eh) {
    142 		bp += 2;
    143 		cmsg->cmsg_len += 2;
    144 	}
    145 
    146 	/* calculate pad length before the option. */
    147 	off = bp - (u_char *)eh;
    148 	padlen = (((off % multx) + (multx - 1)) & ~(multx - 1)) -
    149 		(off % multx);
    150 	padlen += plusy;
    151 	/* insert padding */
    152 	inet6_insert_padopt(bp, padlen);
    153 	cmsg->cmsg_len += padlen;
    154 	bp += padlen;
    155 
    156 	/* copy the option */
    157 	if (typep[0] == IP6OPT_PAD1)
    158 		optlen = 1;
    159 	else
    160 		optlen = typep[1] + 2;
    161 	memcpy(bp, typep, optlen);
    162 	bp += optlen;
    163 	cmsg->cmsg_len += optlen;
    164 
    165 	/* calculate pad length after the option and insert the padding */
    166 	off = bp - (u_char *)eh;
    167 	padlen = ((off + 7) & ~7) - off;
    168 	inet6_insert_padopt(bp, padlen);
    169 	bp += padlen;
    170 	cmsg->cmsg_len += padlen;
    171 
    172 	/* update the length field of the ip6 option header */
    173 	eh->ip6e_len = ((bp - (u_char *)eh) >> 3) - 1;
    174 
    175 	return(0);
    176 }
    177 
    178 /*
    179  * This function appends a Hop-by-Hop option or a Destination option
    180  * into an ancillary data object that has been initialized by
    181  * inet6_option_init().  This function returns a pointer to the 8-bit
    182  * option type field that starts the option on success, or NULL on an
    183  * error.
    184  * The difference between this function and inet6_option_append() is
    185  * that the latter copies the contents of a previously built option into
    186  * the ancillary data object while the current function returns a
    187  * pointer to the space in the data object where the option's TLV must
    188  * then be built by the caller.
    189  *
    190  */
    191 u_int8_t *
    192 inet6_option_alloc(cmsg, datalen, multx, plusy)
    193 	struct cmsghdr *cmsg;
    194 	int datalen;
    195 	int multx;
    196 	int plusy;
    197 {
    198 	int padlen, off;
    199 	register u_int8_t *bp;
    200 	u_int8_t *retval;
    201 	struct ip6_ext *eh;
    202 
    203 	_DIAGASSERT(cmsg != NULL);
    204 #ifdef _DIAGNOSTIC
    205 	if (cmsg == NULL)
    206 		return(NULL);
    207 #endif
    208 
    209 	bp = (u_char *)cmsg + cmsg->cmsg_len;
    210 	eh = (struct ip6_ext *)CMSG_DATA(cmsg);
    211 
    212 	/* argument validation */
    213 	if (multx != 1 && multx != 2 && multx != 4 && multx != 8)
    214 		return(NULL);
    215 	if (plusy < 0 || plusy > 7)
    216 		return(NULL);
    217 
    218 	/*
    219 	 * If this is the first option, allocate space for the
    220 	 * first 2 bytes(for next header and length fields) of
    221 	 * the option header.
    222 	 */
    223 	if (bp == (u_char *)eh) {
    224 		bp += 2;
    225 		cmsg->cmsg_len += 2;
    226 	}
    227 
    228 	/* calculate pad length before the option. */
    229 	off = bp - (u_char *)eh;
    230 	padlen = (((off % multx) + (multx - 1)) & ~(multx - 1)) -
    231 		(off % multx);
    232 	padlen += plusy;
    233 	/* insert padding */
    234 	inet6_insert_padopt(bp, padlen);
    235 	cmsg->cmsg_len += padlen;
    236 	bp += padlen;
    237 
    238 	/* keep space to store specified length of data */
    239 	retval = bp;
    240 	bp += datalen;
    241 	cmsg->cmsg_len += datalen;
    242 
    243 	/* calculate pad length after the option and insert the padding */
    244 	off = bp - (u_char *)eh;
    245 	padlen = ((off + 7) & ~7) - off;
    246 	inet6_insert_padopt(bp, padlen);
    247 	bp += padlen;
    248 	cmsg->cmsg_len += padlen;
    249 
    250 	/* update the length field of the ip6 option header */
    251 	eh->ip6e_len = ((bp - (u_char *)eh) >> 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 #ifdef _DIAGNOSTIC
    278 	if (cmsg == NULL || tptrp == NULL) {
    279 		/* XXX: can't set tptrp to non NULL for this error...  */
    280 		return (-1);
    281 	}
    282 #endif
    283 
    284 	if (cmsg->cmsg_level != IPPROTO_IPV6 ||
    285 	    (cmsg->cmsg_type != IPV6_HOPOPTS &&
    286 	     cmsg->cmsg_type != IPV6_DSTOPTS))
    287 		return(-1);
    288 
    289 	/* message length validation */
    290 	if (cmsg->cmsg_len < CMSG_SPACE(sizeof(struct ip6_ext)))
    291 		return(-1);
    292 	ip6e = (struct ip6_ext *)CMSG_DATA(cmsg);
    293 	hdrlen = (ip6e->ip6e_len + 1) << 3;
    294 	if (cmsg->cmsg_len < CMSG_SPACE(hdrlen))
    295 		return(-1);
    296 
    297 	/*
    298 	 * If the caller does not specify the starting point,
    299 	 * simply return the 1st option.
    300 	 * Otherwise, search the option list for the next option.
    301 	 */
    302 	lim = (u_int8_t *)ip6e + hdrlen;
    303 	if (*tptrp == NULL)
    304 		*tptrp = (u_int8_t *)(ip6e + 1);
    305 	else {
    306 		if ((optlen = ip6optlen(*tptrp, lim)) == 0)
    307 			return(-1);
    308 
    309 		*tptrp = *tptrp + optlen;
    310 	}
    311 	if (*tptrp >= lim) {	/* there is no option */
    312 		*tptrp = NULL;
    313 		return(-1);
    314 	}
    315 	/*
    316 	 * Finally, checks if the next option is safely stored in the
    317 	 * cmsg data.
    318 	 */
    319 	if (ip6optlen(*tptrp, lim) == 0)
    320 		return(-1);
    321 	else
    322 		return(0);
    323 }
    324 
    325 /*
    326  * This function is similar to the inet6_option_next() function,
    327  * except this function lets the caller specify the option type to be
    328  * searched for, instead of always returning the next option in the
    329  * ancillary data object.
    330  * Note: RFC 2292 says the type of tptrp is u_int8_t *, but we think
    331  *       it's a typo. The variable should be type of u_int8_t **.
    332  */
    333 int
    334 inet6_option_find(cmsg, tptrp, type)
    335 	const struct cmsghdr *cmsg;
    336 	u_int8_t **tptrp;
    337 	int type;
    338 {
    339 	struct ip6_ext *ip6e;
    340 	int hdrlen, optlen;
    341 	u_int8_t *optp, *lim;
    342 
    343 	_DIAGASSERT(cmsg != NULL);
    344 	_DIAGASSERT(tptrp != NULL);
    345 #ifdef _DIAGNOSTIC
    346 	if (cmsg == NULL || tptrp == NULL) {
    347 		/* XXX: can't set tptrp to non NULL for this error...  */
    348 		return (-1);
    349 	}
    350 #endif
    351 
    352 	if (cmsg->cmsg_level != IPPROTO_IPV6 ||
    353 	    (cmsg->cmsg_type != IPV6_HOPOPTS &&
    354 	     cmsg->cmsg_type != IPV6_DSTOPTS))
    355 		return(-1);
    356 
    357 	/* message length validation */
    358 	if (cmsg->cmsg_len < CMSG_SPACE(sizeof(struct ip6_ext)))
    359 		return(-1);
    360 	ip6e = (struct ip6_ext *)CMSG_DATA(cmsg);
    361 	hdrlen = (ip6e->ip6e_len + 1) << 3;
    362 	if (cmsg->cmsg_len < CMSG_SPACE(hdrlen))
    363 		return(-1);
    364 
    365 	/*
    366 	 * If the caller does not specify the starting point,
    367 	 * search from the beginning of the option list.
    368 	 * Otherwise, search from *the next option* of the specified point.
    369 	 */
    370 	lim = (u_int8_t *)ip6e + hdrlen;
    371 	if (*tptrp == NULL)
    372 		*tptrp = (u_int8_t *)(ip6e + 1);
    373 	else {
    374 		if ((optlen = ip6optlen(*tptrp, lim)) == 0)
    375 			return(-1);
    376 
    377 		*tptrp = *tptrp + optlen;
    378 	}
    379 	for (optp = *tptrp; optp < lim; optp += optlen) {
    380 		if (*optp == type) {
    381 			*tptrp = optp;
    382 			return(0);
    383 		}
    384 		if ((optlen = ip6optlen(optp, lim)) == 0)
    385 			return(-1);
    386 	}
    387 
    388 	/* search failed */
    389 	*tptrp = NULL;
    390 	return(-1);
    391 }
    392 
    393 /*
    394  * Calculate the length of a given IPv6 option. Also checks
    395  * if the option is safely stored in user's buffer according to the
    396  * calculated length and the limitation of the buffer.
    397  */
    398 static int
    399 ip6optlen(opt, lim)
    400 	u_int8_t *opt, *lim;
    401 {
    402 	int optlen;
    403 
    404 	_DIAGASSERT(opt != NULL);
    405 	_DIAGASSERT(lim != NULL);
    406 
    407 	if (*opt == IP6OPT_PAD1)
    408 		optlen = 1;
    409 	else {
    410 		/* is there enough space to store type and len? */
    411 		if (opt + 2 > lim)
    412 			return(0);
    413 		optlen = *(opt + 1) + 2;
    414 	}
    415 	if (opt + optlen <= lim)
    416 		return(optlen);
    417 
    418 	return(0);
    419 }
    420 
    421 static void
    422 inet6_insert_padopt(u_char *p, int len)
    423 {
    424 
    425 	_DIAGASSERT(p != NULL);
    426 
    427 	switch(len) {
    428 	 case 0:
    429 		 return;
    430 	 case 1:
    431 		 p[0] = IP6OPT_PAD1;
    432 		 return;
    433 	 default:
    434 		 p[0] = IP6OPT_PADN;
    435 		 p[1] = len - 2;
    436 		 memset(&p[2], 0, len - 2);
    437 		 return;
    438 	}
    439 }
    440