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