Merge from vendor branch LIBARCHIVE:
[dragonfly.git] / lib / libc / 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/libc/net/ip6opt.c,v 1.6 2007/05/29 10:58:11 hasso 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
108         /*
109          * If this is the first option, allocate space for the
110          * first 2 bytes(for next header and length fields) of
111          * the option header.
112          */
113         if (bp == (u_char *)eh) {
114                 bp += 2;
115                 cmsg->cmsg_len += 2;
116         }
117
118         /* calculate pad length before the option. */
119         off = bp - (u_char *)eh;
120         padlen = (((off % multx) + (multx - 1)) & ~(multx - 1)) -
121                 (off % multx);
122         padlen += plusy;
123         /* insert padding */
124         inet6_insert_padopt(bp, padlen);
125         cmsg->cmsg_len += padlen;
126         bp += padlen;
127
128         /* copy the option */
129         if (typep[0] == IP6OPT_PAD1)
130                 optlen = 1;
131         else
132                 optlen = typep[1] + 2;
133         memcpy(bp, typep, optlen);
134         bp += optlen;
135         cmsg->cmsg_len += optlen;
136
137         /* calculate pad length after the option and insert the padding */
138         off = bp - (u_char *)eh;
139         padlen = ((off + 7) & ~7) - off;
140         inet6_insert_padopt(bp, padlen);
141         bp += padlen;
142         cmsg->cmsg_len += padlen;
143
144         /* update the length field of the ip6 option header */
145         eh->ip6e_len = ((bp - (u_char *)eh) >> 3) - 1;
146
147         return(0);
148 }
149
150 /*
151  * This function appends a Hop-by-Hop option or a Destination option
152  * into an ancillary data object that has been initialized by
153  * inet6_option_init().  This function returns a pointer to the 8-bit
154  * option type field that starts the option on success, or NULL on an
155  * error.
156  * The difference between this function and inet6_option_append() is
157  * that the latter copies the contents of a previously built option into
158  * the ancillary data object while the current function returns a
159  * pointer to the space in the data object where the option's TLV must
160  * then be built by the caller.
161  * 
162  */
163 u_int8_t *
164 inet6_option_alloc(struct cmsghdr *cmsg, int datalen, int multx, int plusy)
165 {
166         int padlen, off;
167         u_int8_t *bp = (u_char *)cmsg + cmsg->cmsg_len;
168         u_int8_t *retval;
169         struct ip6_ext *eh = (struct ip6_ext *)CMSG_DATA(cmsg);
170
171         /* argument validation */
172         if (multx != 1 && multx != 2 && multx != 4 && multx != 8)
173                 return(NULL);
174         if (plusy < 0 || plusy > 7)
175                 return(NULL);
176
177         /*
178          * If this is the first option, allocate space for the
179          * first 2 bytes(for next header and length fields) of
180          * the option header.
181          */
182         if (bp == (u_char *)eh) {
183                 bp += 2;
184                 cmsg->cmsg_len += 2;
185         }
186
187         /* calculate pad length before the option. */
188         off = bp - (u_char *)eh;
189         padlen = (((off % multx) + (multx - 1)) & ~(multx - 1)) -
190                 (off % multx);
191         padlen += plusy;
192         /* insert padding */
193         inet6_insert_padopt(bp, padlen);
194         cmsg->cmsg_len += padlen;
195         bp += padlen;
196
197         /* keep space to store specified length of data */
198         retval = bp;
199         bp += datalen;
200         cmsg->cmsg_len += datalen;
201
202         /* calculate pad length after the option and insert the padding */
203         off = bp - (u_char *)eh;
204         padlen = ((off + 7) & ~7) - off;
205         inet6_insert_padopt(bp, padlen);
206         bp += padlen;
207         cmsg->cmsg_len += padlen;
208
209         /* update the length field of the ip6 option header */
210         eh->ip6e_len = ((bp - (u_char *)eh) >> 3) - 1;
211
212         return(retval);
213 }
214
215 /*
216  * This function processes the next Hop-by-Hop option or Destination
217  * option in an ancillary data object.  If another option remains to be
218  * processed, the return value of the function is 0 and *tptrp points to
219  * the 8-bit option type field (which is followed by the 8-bit option
220  * data length, followed by the option data).  If no more options remain
221  * to be processed, the return value is -1 and *tptrp is NULL.  If an
222  * error occurs, the return value is -1 and *tptrp is not NULL.
223  * (RFC 2292, 6.3.5)
224  */
225 int
226 inet6_option_next(const struct cmsghdr *cmsg, u_int8_t **tptrp)
227 {
228         struct ip6_ext *ip6e;
229         int hdrlen, optlen;
230         u_int8_t *lim;
231
232         if (cmsg->cmsg_level != IPPROTO_IPV6 ||
233             (cmsg->cmsg_type != IPV6_HOPOPTS &&
234              cmsg->cmsg_type != IPV6_DSTOPTS))
235                 return(-1);
236
237         /* message length validation */
238         if (cmsg->cmsg_len < CMSG_SPACE(sizeof(struct ip6_ext)))
239                 return(-1);
240         ip6e = (struct ip6_ext *)CMSG_DATA(cmsg);
241         hdrlen = (ip6e->ip6e_len + 1) << 3;
242         if (cmsg->cmsg_len < CMSG_SPACE(hdrlen))
243                 return(-1);
244
245         /*
246          * If the caller does not specify the starting point,
247          * simply return the 1st option.
248          * Otherwise, search the option list for the next option.
249          */
250         lim = (u_int8_t *)ip6e + hdrlen;
251         if (*tptrp == NULL)
252                 *tptrp = (u_int8_t *)(ip6e + 1);
253         else {
254                 if ((optlen = ip6optlen(*tptrp, lim)) == 0)
255                         return(-1);
256
257                 *tptrp = *tptrp + optlen;
258         }
259         if (*tptrp >= lim) {    /* there is no option */
260                 *tptrp = NULL;
261                 return(-1);
262         }
263         /*
264          * Finally, checks if the next option is safely stored in the
265          * cmsg data.
266          */
267         if (ip6optlen(*tptrp, lim) == 0)
268                 return(-1);
269         else
270                 return(0);
271 }
272
273 /*
274  * This function is similar to the inet6_option_next() function,
275  * except this function lets the caller specify the option type to be
276  * searched for, instead of always returning the next option in the
277  * ancillary data object.
278  * Note: RFC 2292 says the type of tptrp is u_int8_t *, but we think
279  *       it's a typo. The variable should be type of u_int8_t **.
280  */
281 int
282 inet6_option_find(const struct cmsghdr *cmsg, u_int8_t **tptrp, int type)
283 {
284         struct ip6_ext *ip6e;
285         int hdrlen, optlen;
286         u_int8_t *optp, *lim;
287
288         if (cmsg->cmsg_level != IPPROTO_IPV6 ||
289             (cmsg->cmsg_type != IPV6_HOPOPTS &&
290              cmsg->cmsg_type != IPV6_DSTOPTS))
291                 return(-1);
292
293         /* message length validation */
294         if (cmsg->cmsg_len < CMSG_SPACE(sizeof(struct ip6_ext)))
295                 return(-1);
296         ip6e = (struct ip6_ext *)CMSG_DATA(cmsg);
297         hdrlen = (ip6e->ip6e_len + 1) << 3;
298         if (cmsg->cmsg_len < CMSG_SPACE(hdrlen))
299                 return(-1);     
300
301         /*
302          * If the caller does not specify the starting point,
303          * search from the beginning of the option list.
304          * Otherwise, search from *the next option* of the specified point.
305          */
306         lim = (u_int8_t *)ip6e + hdrlen;
307         if (*tptrp == NULL)
308                 *tptrp = (u_int8_t *)(ip6e + 1);
309         else {
310                 if ((optlen = ip6optlen(*tptrp, lim)) == 0)
311                         return(-1);
312
313                 *tptrp = *tptrp + optlen;
314         }
315         for (optp = *tptrp; optp < lim; optp += optlen) {
316                 if (*optp == type) {
317                         *tptrp = optp;
318                         return(0);
319                 }
320                 if ((optlen = ip6optlen(optp, lim)) == 0)
321                         return(-1);
322         }
323
324         /* search failed */
325         *tptrp = NULL;
326         return(-1);
327 }
328
329 /*
330  * Calculate the length of a given IPv6 option. Also checks
331  * if the option is safely stored in user's buffer according to the
332  * calculated length and the limitation of the buffer.
333  */
334 static int
335 ip6optlen(u_int8_t *opt, u_int8_t *lim)
336 {
337         int optlen;
338
339         if (*opt == IP6OPT_PAD1)
340                 optlen = 1;
341         else {
342                 /* is there enough space to store type and len? */
343                 if (opt + 2 > lim)
344                         return(0);
345                 optlen = *(opt + 1) + 2;
346         }
347         if (opt + optlen <= lim)
348                 return(optlen);
349
350         return(0);
351 }
352
353 static void
354 inet6_insert_padopt(u_char *p, int len)
355 {
356         switch(len) {
357          case 0:
358                  return;
359          case 1:
360                  p[0] = IP6OPT_PAD1;
361                  return;
362          default:
363                  p[0] = IP6OPT_PADN;
364                  p[1] = len - 2; 
365                  memset(&p[2], 0, len - 2);
366                  return;
367         }
368 }
369
370 /*
371  * The following functions are defined in RFC3542, which is a successor
372  * of RFC2292.
373  */
374
375 int
376 inet6_opt_init(void *extbuf, socklen_t extlen)
377 {
378         struct ip6_ext *ext = (struct ip6_ext *)extbuf;
379
380         if (extlen < 0 || (extlen % 8))
381                 return (-1);
382
383         if (ext) {
384                 if (extlen == 0)
385                         return (-1);
386                 ext->ip6e_len = (extlen >> 3) - 1;
387         }
388
389         return (2);             /* sizeof the next and the length fields */
390 }
391
392 int
393 inet6_opt_append(void *extbuf, socklen_t extlen, int offset, u_int8_t type,
394                  socklen_t len, u_int8_t align, void **databufp)
395 {
396         int currentlen = offset, padlen = 0;
397
398         /*
399          * The option type must have a value from 2 to 255, inclusive.
400          * (0 and 1 are reserved for the Pad1 and PadN options, respectively.)
401          */
402 #if 0 /* always false */
403         if (type < 2 || type > 255)
404 #else
405         if (type < 2)
406 #endif
407                 return (-1);
408
409         /*
410          * The option data length must have a value between 0 and 255,
411          * inclusive, and is the length of the option data that follows.
412          */
413         if (len < 0 || len > 255)
414                 return (-1);
415
416         /*
417          * The align parameter must have a value of 1, 2, 4, or 8.
418          * The align value can not exceed the value of len.
419          */
420         if (align != 1 && align != 2 && align != 4 && align != 8)
421                 return (-1);
422         if (align > len)
423                 return (-1);
424
425         /* Calculate the padding length. */
426         currentlen += 2 + len;  /* 2 means "type + len" */
427         if (currentlen % align)
428                 padlen = align - (currentlen % align);
429
430         /* The option must fit in the extension header buffer. */
431         currentlen += padlen;
432         if (extlen &&           /* XXX: right? */
433             currentlen > extlen)
434                 return (-1);
435
436         if (extbuf) {
437                 u_int8_t *optp = (u_int8_t *)extbuf + offset;
438
439                 if (padlen == 1) {
440                         /* insert a Pad1 option */
441                         *optp = IP6OPT_PAD1;
442                         optp++;
443                 } else if (padlen > 0) {
444                         /* insert a PadN option for alignment */
445                         *optp++ = IP6OPT_PADN;
446                         *optp++ = padlen - 2;
447                         memset(optp, 0, padlen - 2);
448                         optp += (padlen - 2);
449                 }
450
451                 *optp++ = type;
452                 *optp++ = len;
453
454                 *databufp = optp;
455         }
456
457         return (currentlen);
458 }
459
460 int
461 inet6_opt_finish(void *extbuf, socklen_t extlen, int offset)
462 {
463         int updatelen = offset > 0 ? (1 + ((offset - 1) | 7)) : 0;;
464
465         if (extbuf) {
466                 u_int8_t *padp;
467                 int padlen = updatelen - offset;
468
469                 if (updatelen > extlen)
470                         return (-1);
471
472                 padp = (u_int8_t *)extbuf + offset;
473                 if (padlen == 1)
474                         *padp = IP6OPT_PAD1;
475                 else if (padlen > 0) {
476                         *padp++ = IP6OPT_PADN;
477                         *padp++ = (padlen - 2);
478                         memset(padp, 0, padlen - 2);
479                 }
480         }
481
482         return (updatelen);
483 }
484
485 int
486 inet6_opt_set_val(void *databuf, int offset, void *val, socklen_t vallen)
487 {
488
489         memcpy((u_int8_t *)databuf + offset, val, vallen);
490         return (offset + vallen);
491 }
492
493 int
494 inet6_opt_next(void *extbuf, socklen_t extlen, int offset, u_int8_t *typep,
495                socklen_t *lenp, void **databufp)
496 {
497         u_int8_t *optp, *lim;
498         int optlen;
499
500         /* Validate extlen. XXX: is the variable really necessary?? */
501         if (extlen == 0 || (extlen % 8))
502                 return (-1);
503         lim = (u_int8_t *)extbuf + extlen;
504
505         /*
506          * If this is the first time this function called for this options
507          * header, simply return the 1st option.
508          * Otherwise, search the option list for the next option.
509          */
510         if (offset == 0)
511                 optp = (u_int8_t *)((struct ip6_hbh *)extbuf + 1);
512         else
513                 optp = (u_int8_t *)extbuf + offset;
514
515         /* Find the next option skipping any padding options. */
516         while (optp < lim) {
517                 switch(*optp) {
518                 case IP6OPT_PAD1:
519                         optp++;
520                         break;
521                 case IP6OPT_PADN:
522                         if ((optlen = ip6optlen(optp, lim)) == 0)
523                                 goto optend;
524                         optp += optlen;
525                         break;
526                 default:        /* found */
527                         if ((optlen = ip6optlen(optp, lim)) == 0)
528                                 goto optend;
529                         *typep = *optp;
530                         *lenp = optlen - 2;
531                         *databufp = optp + 2;
532                         return (optp + optlen - (u_int8_t *)extbuf);
533                 }
534         }
535
536   optend:
537         *databufp = NULL; /* for safety */
538         return (-1);
539 }
540
541 int
542 inet6_opt_find(void *extbuf, socklen_t extlen, int offset, u_int8_t type,
543                socklen_t *lenp, void **databufp)
544 {
545         u_int8_t *optp, *lim;
546         int optlen;
547
548         /* Validate extlen. XXX: is the variable really necessary?? */
549         if (extlen == 0 || (extlen % 8))
550                 return (-1);
551         lim = (u_int8_t *)extbuf + extlen;
552
553         /*
554          * If this is the first time this function called for this options
555          * header, simply return the 1st option.
556          * Otherwise, search the option list for the next option.
557          */
558         if (offset == 0)
559                 optp = (u_int8_t *)((struct ip6_hbh *)extbuf + 1);
560         else
561                 optp = (u_int8_t *)extbuf + offset;
562
563         /* Find the specified option */
564         while (optp < lim) {
565                 if ((optlen = ip6optlen(optp, lim)) == 0)
566                         goto optend;
567
568                 if (*optp == type) { /* found */
569                         *lenp = optlen - 2;
570                         *databufp = optp + 2;
571                         return (optp + optlen - (u_int8_t *)extbuf);
572                 }
573
574                 optp += optlen;
575         }
576
577   optend:
578         *databufp = NULL; /* for safety */
579         return (-1);
580 }
581
582 int
583 inet6_opt_get_val(void *databuf, int offset, void *val, socklen_t vallen)
584 {
585
586         /* we can't assume alignment here */
587         memcpy(val, (u_int8_t *)databuf + offset, vallen);
588
589         return (offset + vallen);
590 }