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