2 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
29 * $FreeBSD: src/lib/libc/net/ip6opt.c,v 1.1 1999/12/16 18:32:01 shin Exp $
30 * $DragonFly: src/lib/libcr/net/Attic/ip6opt.c,v 1.3 2004/10/25 19:38:25 drhodus Exp $
33 #include <sys/param.h>
34 #include <sys/types.h>
35 #include <sys/socket.h>
37 #include <netinet/in.h>
38 #include <netinet/ip6.h>
43 static int ip6optlen(u_int8_t *opt, u_int8_t *lim);
44 static void inet6_insert_padopt(u_char *p, int len);
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.
56 inet6_option_space(nbytes)
59 nbytes += 2; /* we need space for nxt-hdr and length fields */
60 return(CMSG_SPACE((nbytes + 7) & ~7));
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.
69 inet6_option_init(bp, cmsgp, type)
71 struct cmsghdr **cmsgp;
74 struct cmsghdr *ch = (struct cmsghdr *)bp;
76 /* argument validation */
77 if (type != IPV6_HOPOPTS && type != IPV6_DSTOPTS)
80 ch->cmsg_level = IPPROTO_IPV6;
82 ch->cmsg_len = CMSG_LEN(0);
89 * This function appends a Hop-by-Hop option or a Destination option
90 * into an ancillary data object that has been initialized by
91 * inet6_option_init(). This function returns 0 if it succeeds or -1 on
93 * multx is the value x in the alignment term "xn + y" described
94 * earlier. It must have a value of 1, 2, 4, or 8.
95 * plusy is the value y in the alignment term "xn + y" described
96 * earlier. It must have a value between 0 and 7, inclusive.
99 inet6_option_append(cmsg, typep, multx, plusy)
100 struct cmsghdr *cmsg;
101 const u_int8_t *typep;
105 int padlen, optlen, off;
106 u_char *bp = (u_char *)cmsg + cmsg->cmsg_len;
107 struct ip6_ext *eh = (struct ip6_ext *)CMSG_DATA(cmsg);
109 /* argument validation */
110 if (multx != 1 && multx != 2 && multx != 4 && multx != 8)
112 if (plusy < 0 || plusy > 7)
118 * If this is the first option, allocate space for the
119 * first 2 bytes(for next header and length fields) of
122 if (bp == (u_char *)eh) {
127 /* calculate pad length before the option. */
128 off = bp - (u_char *)eh;
129 padlen = (((off % multx) + (multx - 1)) & ~(multx - 1)) -
133 inet6_insert_padopt(bp, padlen);
134 cmsg->cmsg_len += padlen;
137 /* copy the option */
138 if (typep[0] == IP6OPT_PAD1)
141 optlen = typep[1] + 2;
142 memcpy(bp, typep, optlen);
144 cmsg->cmsg_len += optlen;
146 /* calculate pad length after the option and insert the padding */
147 off = bp - (u_char *)eh;
148 padlen = ((off + 7) & ~7) - off;
149 inet6_insert_padopt(bp, padlen);
151 cmsg->cmsg_len += padlen;
153 /* update the length field of the ip6 option header */
154 eh->ip6e_len = ((bp - (u_char *)eh) >> 3) - 1;
160 * This function appends a Hop-by-Hop option or a Destination option
161 * into an ancillary data object that has been initialized by
162 * inet6_option_init(). This function returns a pointer to the 8-bit
163 * option type field that starts the option on success, or NULL on an
165 * The difference between this function and inet6_option_append() is
166 * that the latter copies the contents of a previously built option into
167 * the ancillary data object while the current function returns a
168 * pointer to the space in the data object where the option's TLV must
169 * then be built by the caller.
173 inet6_option_alloc(cmsg, datalen, multx, plusy)
174 struct cmsghdr *cmsg;
180 u_int8_t *bp = (u_char *)cmsg + cmsg->cmsg_len;
182 struct ip6_ext *eh = (struct ip6_ext *)CMSG_DATA(cmsg);
184 /* argument validation */
185 if (multx != 1 && multx != 2 && multx != 4 && multx != 8)
187 if (plusy < 0 || plusy > 7)
191 * If this is the first option, allocate space for the
192 * first 2 bytes(for next header and length fields) of
195 if (bp == (u_char *)eh) {
200 /* calculate pad length before the option. */
201 off = bp - (u_char *)eh;
202 padlen = (((off % multx) + (multx - 1)) & ~(multx - 1)) -
206 inet6_insert_padopt(bp, padlen);
207 cmsg->cmsg_len += padlen;
210 /* keep space to store specified length of data */
213 cmsg->cmsg_len += datalen;
215 /* calculate pad length after the option and insert the padding */
216 off = bp - (u_char *)eh;
217 padlen = ((off + 7) & ~7) - off;
218 inet6_insert_padopt(bp, padlen);
220 cmsg->cmsg_len += padlen;
222 /* update the length field of the ip6 option header */
223 eh->ip6e_len = ((bp - (u_char *)eh) >> 3) - 1;
229 * This function processes the next Hop-by-Hop option or Destination
230 * option in an ancillary data object. If another option remains to be
231 * processed, the return value of the function is 0 and *tptrp points to
232 * the 8-bit option type field (which is followed by the 8-bit option
233 * data length, followed by the option data). If no more options remain
234 * to be processed, the return value is -1 and *tptrp is NULL. If an
235 * error occurs, the return value is -1 and *tptrp is not NULL.
239 inet6_option_next(cmsg, tptrp)
240 const struct cmsghdr *cmsg;
243 struct ip6_ext *ip6e;
247 if (cmsg->cmsg_level != IPPROTO_IPV6 ||
248 (cmsg->cmsg_type != IPV6_HOPOPTS &&
249 cmsg->cmsg_type != IPV6_DSTOPTS))
252 /* message length validation */
253 if (cmsg->cmsg_len < CMSG_SPACE(sizeof(struct ip6_ext)))
255 ip6e = (struct ip6_ext *)CMSG_DATA(cmsg);
256 hdrlen = (ip6e->ip6e_len + 1) << 3;
257 if (cmsg->cmsg_len < CMSG_SPACE(hdrlen))
261 * If the caller does not specify the starting point,
262 * simply return the 1st option.
263 * Otherwise, search the option list for the next option.
265 lim = (u_int8_t *)ip6e + hdrlen;
267 *tptrp = (u_int8_t *)(ip6e + 1);
269 if ((optlen = ip6optlen(*tptrp, lim)) == 0)
272 *tptrp = *tptrp + optlen;
274 if (*tptrp >= lim) { /* there is no option */
279 * Finally, checks if the next option is safely stored in the
282 if (ip6optlen(*tptrp, lim) == 0)
289 * This function is similar to the inet6_option_next() function,
290 * except this function lets the caller specify the option type to be
291 * searched for, instead of always returning the next option in the
292 * ancillary data object.
293 * Note: RFC 2292 says the type of tptrp is u_int8_t *, but we think
294 * it's a typo. The variable should be type of u_int8_t **.
297 inet6_option_find(cmsg, tptrp, type)
298 const struct cmsghdr *cmsg;
302 struct ip6_ext *ip6e;
304 u_int8_t *optp, *lim;
306 if (cmsg->cmsg_level != IPPROTO_IPV6 ||
307 (cmsg->cmsg_type != IPV6_HOPOPTS &&
308 cmsg->cmsg_type != IPV6_DSTOPTS))
311 /* message length validation */
312 if (cmsg->cmsg_len < CMSG_SPACE(sizeof(struct ip6_ext)))
314 ip6e = (struct ip6_ext *)CMSG_DATA(cmsg);
315 hdrlen = (ip6e->ip6e_len + 1) << 3;
316 if (cmsg->cmsg_len < CMSG_SPACE(hdrlen))
320 * If the caller does not specify the starting point,
321 * search from the beginning of the option list.
322 * Otherwise, search from *the next option* of the specified point.
324 lim = (u_int8_t *)ip6e + hdrlen;
326 *tptrp = (u_int8_t *)(ip6e + 1);
328 if ((optlen = ip6optlen(*tptrp, lim)) == 0)
331 *tptrp = *tptrp + optlen;
333 for (optp = *tptrp; optp < lim; optp += optlen) {
338 if ((optlen = ip6optlen(optp, lim)) == 0)
348 * Calculate the length of a given IPv6 option. Also checks
349 * if the option is safely stored in user's buffer according to the
350 * calculated length and the limitation of the buffer.
358 if (*opt == IP6OPT_PAD1)
361 /* is there enough space to store type and len? */
364 optlen = *(opt + 1) + 2;
366 if (opt + optlen <= lim)
373 inet6_insert_padopt(u_char *p, int len)
384 memset(&p[2], 0, len - 2);