grrr...fix reverse chronological order
[dragonfly.git] / lib / libcr / net / ip6opt.c
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/libcr/net/Attic/ip6opt.c,v 1.3 2004/10/25 19:38:25 drhodus 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(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         if (typep[0] > 255)
115                 return(-1);
116
117         /*
118          * If this is the first option, allocate space for the
119          * first 2 bytes(for next header and length fields) of
120          * the option header.
121          */
122         if (bp == (u_char *)eh) {
123                 bp += 2;
124                 cmsg->cmsg_len += 2;
125         }
126
127         /* calculate pad length before the option. */
128         off = bp - (u_char *)eh;
129         padlen = (((off % multx) + (multx - 1)) & ~(multx - 1)) -
130                 (off % multx);
131         padlen += plusy;
132         /* insert padding */
133         inet6_insert_padopt(bp, padlen);
134         cmsg->cmsg_len += padlen;
135         bp += padlen;
136
137         /* copy the option */
138         if (typep[0] == IP6OPT_PAD1)
139                 optlen = 1;
140         else
141                 optlen = typep[1] + 2;
142         memcpy(bp, typep, optlen);
143         bp += optlen;
144         cmsg->cmsg_len += optlen;
145
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);
150         bp += padlen;
151         cmsg->cmsg_len += padlen;
152
153         /* update the length field of the ip6 option header */
154         eh->ip6e_len = ((bp - (u_char *)eh) >> 3) - 1;
155
156         return(0);
157 }
158
159 /*
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
164  * error.
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.
170  * 
171  */
172 u_int8_t *
173 inet6_option_alloc(cmsg, datalen, multx, plusy)
174         struct cmsghdr *cmsg;
175         int datalen;
176         int multx;
177         int plusy;
178 {
179         int padlen, off;
180         u_int8_t *bp = (u_char *)cmsg + cmsg->cmsg_len;
181         u_int8_t *retval;
182         struct ip6_ext *eh = (struct ip6_ext *)CMSG_DATA(cmsg);
183
184         /* argument validation */
185         if (multx != 1 && multx != 2 && multx != 4 && multx != 8)
186                 return(NULL);
187         if (plusy < 0 || plusy > 7)
188                 return(NULL);
189
190         /*
191          * If this is the first option, allocate space for the
192          * first 2 bytes(for next header and length fields) of
193          * the option header.
194          */
195         if (bp == (u_char *)eh) {
196                 bp += 2;
197                 cmsg->cmsg_len += 2;
198         }
199
200         /* calculate pad length before the option. */
201         off = bp - (u_char *)eh;
202         padlen = (((off % multx) + (multx - 1)) & ~(multx - 1)) -
203                 (off % multx);
204         padlen += plusy;
205         /* insert padding */
206         inet6_insert_padopt(bp, padlen);
207         cmsg->cmsg_len += padlen;
208         bp += padlen;
209
210         /* keep space to store specified length of data */
211         retval = bp;
212         bp += datalen;
213         cmsg->cmsg_len += datalen;
214
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);
219         bp += padlen;
220         cmsg->cmsg_len += padlen;
221
222         /* update the length field of the ip6 option header */
223         eh->ip6e_len = ((bp - (u_char *)eh) >> 3) - 1;
224
225         return(retval);
226 }
227
228 /*
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.
236  * (RFC 2292, 6.3.5)
237  */
238 int
239 inet6_option_next(cmsg, tptrp)
240         const struct cmsghdr *cmsg;
241         u_int8_t **tptrp;
242 {
243         struct ip6_ext *ip6e;
244         int hdrlen, optlen;
245         u_int8_t *lim;
246
247         if (cmsg->cmsg_level != IPPROTO_IPV6 ||
248             (cmsg->cmsg_type != IPV6_HOPOPTS &&
249              cmsg->cmsg_type != IPV6_DSTOPTS))
250                 return(-1);
251
252         /* message length validation */
253         if (cmsg->cmsg_len < CMSG_SPACE(sizeof(struct ip6_ext)))
254                 return(-1);
255         ip6e = (struct ip6_ext *)CMSG_DATA(cmsg);
256         hdrlen = (ip6e->ip6e_len + 1) << 3;
257         if (cmsg->cmsg_len < CMSG_SPACE(hdrlen))
258                 return(-1);
259
260         /*
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.
264          */
265         lim = (u_int8_t *)ip6e + hdrlen;
266         if (*tptrp == NULL)
267                 *tptrp = (u_int8_t *)(ip6e + 1);
268         else {
269                 if ((optlen = ip6optlen(*tptrp, lim)) == 0)
270                         return(-1);
271
272                 *tptrp = *tptrp + optlen;
273         }
274         if (*tptrp >= lim) {    /* there is no option */
275                 *tptrp = NULL;
276                 return(-1);
277         }
278         /*
279          * Finally, checks if the next option is safely stored in the
280          * cmsg data.
281          */
282         if (ip6optlen(*tptrp, lim) == 0)
283                 return(-1);
284         else
285                 return(0);
286 }
287
288 /*
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 **.
295  */
296 int
297 inet6_option_find(cmsg, tptrp, type)
298         const struct cmsghdr *cmsg;
299         u_int8_t **tptrp;
300         int type;
301 {
302         struct ip6_ext *ip6e;
303         int hdrlen, optlen;
304         u_int8_t *optp, *lim;
305
306         if (cmsg->cmsg_level != IPPROTO_IPV6 ||
307             (cmsg->cmsg_type != IPV6_HOPOPTS &&
308              cmsg->cmsg_type != IPV6_DSTOPTS))
309                 return(-1);
310
311         /* message length validation */
312         if (cmsg->cmsg_len < CMSG_SPACE(sizeof(struct ip6_ext)))
313                 return(-1);
314         ip6e = (struct ip6_ext *)CMSG_DATA(cmsg);
315         hdrlen = (ip6e->ip6e_len + 1) << 3;
316         if (cmsg->cmsg_len < CMSG_SPACE(hdrlen))
317                 return(-1);     
318
319         /*
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.
323          */
324         lim = (u_int8_t *)ip6e + hdrlen;
325         if (*tptrp == NULL)
326                 *tptrp = (u_int8_t *)(ip6e + 1);
327         else {
328                 if ((optlen = ip6optlen(*tptrp, lim)) == 0)
329                         return(-1);
330
331                 *tptrp = *tptrp + optlen;
332         }
333         for (optp = *tptrp; optp < lim; optp += optlen) {
334                 if (*optp == type) {
335                         *tptrp = optp;
336                         return(0);
337                 }
338                 if ((optlen = ip6optlen(optp, lim)) == 0)
339                         return(-1);
340         }
341
342         /* search failed */
343         *tptrp = NULL;
344         return(-1);
345 }
346
347 /*
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.
351  */
352 static int
353 ip6optlen(opt, lim)
354         u_int8_t *opt, *lim;
355 {
356         int optlen;
357
358         if (*opt == IP6OPT_PAD1)
359                 optlen = 1;
360         else {
361                 /* is there enough space to store type and len? */
362                 if (opt + 2 > lim)
363                         return(0);
364                 optlen = *(opt + 1) + 2;
365         }
366         if (opt + optlen <= lim)
367                 return(optlen);
368
369         return(0);
370 }
371
372 static void
373 inet6_insert_padopt(u_char *p, int len)
374 {
375         switch(len) {
376          case 0:
377                  return;
378          case 1:
379                  p[0] = IP6OPT_PAD1;
380                  return;
381          default:
382                  p[0] = IP6OPT_PADN;
383                  p[1] = len - 2; 
384                  memset(&p[2], 0, len - 2);
385                  return;
386         }
387 }