1 /* 2 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project. 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 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. 16 * 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 27 * SUCH DAMAGE. 28 * 29 * $FreeBSD: src/lib/libc/net/ip6opt.c,v 1.1 1999/12/16 18:32:01 shin Exp $ 30 * $DragonFly: src/lib/libc/net/ip6opt.c,v 1.4 2005/11/13 02:04:47 swildner Exp $ 31 */ 32 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 <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(int nbytes) 57 { 58 nbytes += 2; /* we need space for nxt-hdr and length fields */ 59 return(CMSG_SPACE((nbytes + 7) & ~7)); 60 } 61 62 /* 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. 66 */ 67 int 68 inet6_option_init(void *bp, struct cmsghdr **cmsgp, int type) 69 { 70 struct cmsghdr *ch = (struct cmsghdr *)bp; 71 72 /* argument validation */ 73 if (type != IPV6_HOPOPTS && type != IPV6_DSTOPTS) 74 return(-1); 75 76 ch->cmsg_level = IPPROTO_IPV6; 77 ch->cmsg_type = type; 78 ch->cmsg_len = CMSG_LEN(0); 79 80 *cmsgp = ch; 81 return(0); 82 } 83 84 /* 85 * This function appends a Hop-by-Hop option or a Destination option 86 * into an ancillary data object that has been initialized by 87 * inet6_option_init(). This function returns 0 if it succeeds or -1 on 88 * an error. 89 * multx is the value x in the alignment term "xn + y" described 90 * earlier. It must have a value of 1, 2, 4, or 8. 91 * plusy is the value y in the alignment term "xn + y" described 92 * earlier. It must have a value between 0 and 7, inclusive. 93 */ 94 int 95 inet6_option_append(struct cmsghdr *cmsg, const u_int8_t *typep, int multx, 96 int plusy) 97 { 98 int padlen, optlen, off; 99 u_char *bp = (u_char *)cmsg + cmsg->cmsg_len; 100 struct ip6_ext *eh = (struct ip6_ext *)CMSG_DATA(cmsg); 101 102 /* argument validation */ 103 if (multx != 1 && multx != 2 && multx != 4 && multx != 8) 104 return(-1); 105 if (plusy < 0 || plusy > 7) 106 return(-1); 107 if (typep[0] > 255) 108 return(-1); 109 110 /* 111 * If this is the first option, allocate space for the 112 * first 2 bytes(for next header and length fields) of 113 * the option header. 114 */ 115 if (bp == (u_char *)eh) { 116 bp += 2; 117 cmsg->cmsg_len += 2; 118 } 119 120 /* calculate pad length before the option. */ 121 off = bp - (u_char *)eh; 122 padlen = (((off % multx) + (multx - 1)) & ~(multx - 1)) - 123 (off % multx); 124 padlen += plusy; 125 /* insert padding */ 126 inet6_insert_padopt(bp, padlen); 127 cmsg->cmsg_len += padlen; 128 bp += padlen; 129 130 /* copy the option */ 131 if (typep[0] == IP6OPT_PAD1) 132 optlen = 1; 133 else 134 optlen = typep[1] + 2; 135 memcpy(bp, typep, optlen); 136 bp += optlen; 137 cmsg->cmsg_len += optlen; 138 139 /* calculate pad length after the option and insert the padding */ 140 off = bp - (u_char *)eh; 141 padlen = ((off + 7) & ~7) - off; 142 inet6_insert_padopt(bp, padlen); 143 bp += padlen; 144 cmsg->cmsg_len += padlen; 145 146 /* update the length field of the ip6 option header */ 147 eh->ip6e_len = ((bp - (u_char *)eh) >> 3) - 1; 148 149 return(0); 150 } 151 152 /* 153 * This function appends a Hop-by-Hop option or a Destination option 154 * into an ancillary data object that has been initialized by 155 * inet6_option_init(). This function returns a pointer to the 8-bit 156 * option type field that starts the option on success, or NULL on an 157 * error. 158 * The difference between this function and inet6_option_append() is 159 * that the latter copies the contents of a previously built option into 160 * the ancillary data object while the current function returns a 161 * pointer to the space in the data object where the option's TLV must 162 * then be built by the caller. 163 * 164 */ 165 u_int8_t * 166 inet6_option_alloc(struct cmsghdr *cmsg, int datalen, int multx, int plusy) 167 { 168 int padlen, off; 169 u_int8_t *bp = (u_char *)cmsg + cmsg->cmsg_len; 170 u_int8_t *retval; 171 struct ip6_ext *eh = (struct ip6_ext *)CMSG_DATA(cmsg); 172 173 /* argument validation */ 174 if (multx != 1 && multx != 2 && multx != 4 && multx != 8) 175 return(NULL); 176 if (plusy < 0 || plusy > 7) 177 return(NULL); 178 179 /* 180 * If this is the first option, allocate space for the 181 * first 2 bytes(for next header and length fields) of 182 * the option header. 183 */ 184 if (bp == (u_char *)eh) { 185 bp += 2; 186 cmsg->cmsg_len += 2; 187 } 188 189 /* calculate pad length before the option. */ 190 off = bp - (u_char *)eh; 191 padlen = (((off % multx) + (multx - 1)) & ~(multx - 1)) - 192 (off % multx); 193 padlen += plusy; 194 /* insert padding */ 195 inet6_insert_padopt(bp, padlen); 196 cmsg->cmsg_len += padlen; 197 bp += padlen; 198 199 /* keep space to store specified length of data */ 200 retval = bp; 201 bp += datalen; 202 cmsg->cmsg_len += datalen; 203 204 /* calculate pad length after the option and insert the padding */ 205 off = bp - (u_char *)eh; 206 padlen = ((off + 7) & ~7) - off; 207 inet6_insert_padopt(bp, padlen); 208 bp += padlen; 209 cmsg->cmsg_len += padlen; 210 211 /* update the length field of the ip6 option header */ 212 eh->ip6e_len = ((bp - (u_char *)eh) >> 3) - 1; 213 214 return(retval); 215 } 216 217 /* 218 * This function processes the next Hop-by-Hop option or Destination 219 * option in an ancillary data object. If another option remains to be 220 * processed, the return value of the function is 0 and *tptrp points to 221 * the 8-bit option type field (which is followed by the 8-bit option 222 * data length, followed by the option data). If no more options remain 223 * to be processed, the return value is -1 and *tptrp is NULL. If an 224 * error occurs, the return value is -1 and *tptrp is not NULL. 225 * (RFC 2292, 6.3.5) 226 */ 227 int 228 inet6_option_next(const struct cmsghdr *cmsg, u_int8_t **tptrp) 229 { 230 struct ip6_ext *ip6e; 231 int hdrlen, optlen; 232 u_int8_t *lim; 233 234 if (cmsg->cmsg_level != IPPROTO_IPV6 || 235 (cmsg->cmsg_type != IPV6_HOPOPTS && 236 cmsg->cmsg_type != IPV6_DSTOPTS)) 237 return(-1); 238 239 /* message length validation */ 240 if (cmsg->cmsg_len < CMSG_SPACE(sizeof(struct ip6_ext))) 241 return(-1); 242 ip6e = (struct ip6_ext *)CMSG_DATA(cmsg); 243 hdrlen = (ip6e->ip6e_len + 1) << 3; 244 if (cmsg->cmsg_len < CMSG_SPACE(hdrlen)) 245 return(-1); 246 247 /* 248 * If the caller does not specify the starting point, 249 * simply return the 1st option. 250 * Otherwise, search the option list for the next option. 251 */ 252 lim = (u_int8_t *)ip6e + hdrlen; 253 if (*tptrp == NULL) 254 *tptrp = (u_int8_t *)(ip6e + 1); 255 else { 256 if ((optlen = ip6optlen(*tptrp, lim)) == 0) 257 return(-1); 258 259 *tptrp = *tptrp + optlen; 260 } 261 if (*tptrp >= lim) { /* there is no option */ 262 *tptrp = NULL; 263 return(-1); 264 } 265 /* 266 * Finally, checks if the next option is safely stored in the 267 * cmsg data. 268 */ 269 if (ip6optlen(*tptrp, lim) == 0) 270 return(-1); 271 else 272 return(0); 273 } 274 275 /* 276 * This function is similar to the inet6_option_next() function, 277 * except this function lets the caller specify the option type to be 278 * searched for, instead of always returning the next option in the 279 * ancillary data object. 280 * Note: RFC 2292 says the type of tptrp is u_int8_t *, but we think 281 * it's a typo. The variable should be type of u_int8_t **. 282 */ 283 int 284 inet6_option_find(const struct cmsghdr *cmsg, u_int8_t **tptrp, int type) 285 { 286 struct ip6_ext *ip6e; 287 int hdrlen, optlen; 288 u_int8_t *optp, *lim; 289 290 if (cmsg->cmsg_level != IPPROTO_IPV6 || 291 (cmsg->cmsg_type != IPV6_HOPOPTS && 292 cmsg->cmsg_type != IPV6_DSTOPTS)) 293 return(-1); 294 295 /* message length validation */ 296 if (cmsg->cmsg_len < CMSG_SPACE(sizeof(struct ip6_ext))) 297 return(-1); 298 ip6e = (struct ip6_ext *)CMSG_DATA(cmsg); 299 hdrlen = (ip6e->ip6e_len + 1) << 3; 300 if (cmsg->cmsg_len < CMSG_SPACE(hdrlen)) 301 return(-1); 302 303 /* 304 * If the caller does not specify the starting point, 305 * search from the beginning of the option list. 306 * Otherwise, search from *the next option* of the specified point. 307 */ 308 lim = (u_int8_t *)ip6e + hdrlen; 309 if (*tptrp == NULL) 310 *tptrp = (u_int8_t *)(ip6e + 1); 311 else { 312 if ((optlen = ip6optlen(*tptrp, lim)) == 0) 313 return(-1); 314 315 *tptrp = *tptrp + optlen; 316 } 317 for (optp = *tptrp; optp < lim; optp += optlen) { 318 if (*optp == type) { 319 *tptrp = optp; 320 return(0); 321 } 322 if ((optlen = ip6optlen(optp, lim)) == 0) 323 return(-1); 324 } 325 326 /* search failed */ 327 *tptrp = NULL; 328 return(-1); 329 } 330 331 /* 332 * Calculate the length of a given IPv6 option. Also checks 333 * if the option is safely stored in user's buffer according to the 334 * calculated length and the limitation of the buffer. 335 */ 336 static int 337 ip6optlen(u_int8_t *opt, u_int8_t *lim) 338 { 339 int optlen; 340 341 if (*opt == IP6OPT_PAD1) 342 optlen = 1; 343 else { 344 /* is there enough space to store type and len? */ 345 if (opt + 2 > lim) 346 return(0); 347 optlen = *(opt + 1) + 2; 348 } 349 if (opt + optlen <= lim) 350 return(optlen); 351 352 return(0); 353 } 354 355 static void 356 inet6_insert_padopt(u_char *p, int len) 357 { 358 switch(len) { 359 case 0: 360 return; 361 case 1: 362 p[0] = IP6OPT_PAD1; 363 return; 364 default: 365 p[0] = IP6OPT_PADN; 366 p[1] = len - 2; 367 memset(&p[2], 0, len - 2); 368 return; 369 } 370 } 371