Home | History | Annotate | Line # | Download | only in net
rthdr.c revision 1.3
      1 /*	$NetBSD: rthdr.c,v 1.3 1999/09/16 11:45:19 lukem Exp $	*/
      2 
      3 /*
      4  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. Neither the name of the project nor the names of its contributors
     16  *    may be used to endorse or promote products derived from this software
     17  *    without specific prior written permission.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
     20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
     23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29  * SUCH DAMAGE.
     30  */
     31 
     32 #include <sys/param.h>
     33 #include <sys/types.h>
     34 #include <sys/socket.h>
     35 
     36 #include <netinet/in.h>
     37 #include <netinet/ip6.h>
     38 
     39 #include <assert.h>
     40 #include <string.h>
     41 #include <stdio.h>
     42 
     43 size_t
     44 inet6_rthdr_space(type, seg)
     45     int type, seg;
     46 {
     47     switch(type) {
     48      case IPV6_RTHDR_TYPE_0:
     49 	 if (seg < 1 || seg > 23)
     50 	     return(0);
     51 	 return(CMSG_SPACE(sizeof(struct in6_addr) * (seg - 1)
     52 			   + sizeof(struct ip6_rthdr0)));
     53      default:
     54 #ifdef DEBUG
     55 	 fprintf(stderr, "inet6_rthdr_space: unknown type(%d)\n", type);
     56 #endif
     57 	 return(0);
     58     }
     59 }
     60 
     61 struct cmsghdr *
     62 inet6_rthdr_init(bp, type)
     63     void *bp;
     64     int type;
     65 {
     66     register struct cmsghdr *ch;
     67     register struct ip6_rthdr *rthdr;
     68 
     69     _DIAGASSERT(bp != NULL);
     70 #ifdef _DIAGNOSTIC
     71     if (bp == NULL)
     72 	return(NULL);
     73 #endif
     74 
     75     ch = (struct cmsghdr *)bp;
     76     rthdr = (struct ip6_rthdr *)(ch + 1);
     77 
     78     ch->cmsg_level = IPPROTO_IPV6;
     79     ch->cmsg_type = IPV6_RTHDR;
     80 
     81     switch(type) {
     82      case IPV6_RTHDR_TYPE_0:
     83 	 ch->cmsg_len = CMSG_LEN(sizeof(struct ip6_rthdr0) - sizeof(struct in6_addr));
     84 	 bzero(rthdr, sizeof(struct ip6_rthdr0));
     85 	 rthdr->ip6r_type = IPV6_RTHDR_TYPE_0;
     86 	 return(ch);
     87      default:
     88 #ifdef DEBUG
     89 	 fprintf(stderr, "inet6_rthdr_init: unknown type(%d)\n", type);
     90 #endif
     91 	 return(NULL);
     92     }
     93 }
     94 
     95 int
     96 inet6_rthdr_add(cmsg, addr, flags)
     97     struct cmsghdr *cmsg;
     98     const struct in6_addr *addr;
     99     u_int flags;
    100 {
    101     register struct ip6_rthdr *rthdr;
    102 
    103     _DIAGASSERT(cmsg != NULL);
    104     _DIAGASSERT(addr != NULL);
    105 #ifdef _DIAGNOSTIC
    106     if (cmsg == NULL || addr == NULL)
    107 	return (-1);
    108 #endif
    109 
    110     rthdr = (struct ip6_rthdr *)(cmsg + 1);
    111 
    112     switch(rthdr->ip6r_type) {
    113      case IPV6_RTHDR_TYPE_0:
    114      {
    115 	 struct ip6_rthdr0 *rt0 = (struct ip6_rthdr0 *)rthdr;
    116 	 if (flags != IPV6_RTHDR_LOOSE && flags != IPV6_RTHDR_STRICT) {
    117 #ifdef DEBUG
    118 	     fprintf(stderr, "inet6_rthdr_add: unsupported flag(%d)\n", flags);
    119 #endif
    120 	     return(-1);
    121 	 }
    122 	 if (rt0->ip6r0_segleft == 23) {
    123 #ifdef DEBUG
    124 	     fprintf(stderr, "inet6_rthdr_add: segment overflow\n");
    125 #endif
    126 	     return(-1);
    127 	 }
    128 	 if (flags == IPV6_RTHDR_STRICT) {
    129 	     int c, b;
    130 	     c = rt0->ip6r0_segleft / 8;
    131 	     b = rt0->ip6r0_segleft % 8;
    132 	     rt0->ip6r0_slmap[c] |= (1 << (7 - b));
    133 	 }
    134 	 rt0->ip6r0_segleft++;
    135 	 bcopy(addr, (caddr_t)rt0 + ((rt0->ip6r0_len + 1) << 3),
    136 	       sizeof(struct in6_addr));
    137 	 rt0->ip6r0_len += sizeof(struct in6_addr) >> 3;
    138 	 cmsg->cmsg_len = CMSG_LEN((rt0->ip6r0_len + 1) << 3);
    139 	 break;
    140      }
    141      default:
    142 #ifdef DEBUG
    143 	 fprintf(stderr, "inet6_rthdr_add: unknown type(%d)\n",
    144 		 rthdr->ip6r_type);
    145 #endif
    146 	 return(-1);
    147     }
    148 
    149     return(0);
    150 }
    151 
    152 int
    153 inet6_rthdr_lasthop(cmsg, flags)
    154     struct cmsghdr *cmsg;
    155     unsigned int flags;
    156 {
    157     register struct ip6_rthdr *rthdr;
    158 
    159     _DIAGASSERT(cmsg != NULL);
    160 #ifdef _DIAGNOSTIC
    161     if (cmsg == NULL)
    162 	return (-1);
    163 #endif
    164 
    165     rthdr = (struct ip6_rthdr *)(cmsg + 1);
    166 
    167     switch(rthdr->ip6r_type) {
    168      case IPV6_RTHDR_TYPE_0:
    169      {
    170 	 struct ip6_rthdr0 *rt0 = (struct ip6_rthdr0 *)rthdr;
    171 	 if (flags != IPV6_RTHDR_LOOSE && flags != IPV6_RTHDR_STRICT) {
    172 #ifdef DEBUG
    173 	     fprintf(stderr, "inet6_rthdr_lasthop: unsupported flag(%d)\n", flags);
    174 #endif
    175 	     return(-1);
    176 	 }
    177 	 if (rt0->ip6r0_segleft > 23) {
    178 #ifdef DEBUG
    179 	     fprintf(stderr, "inet6_rthdr_add: segment overflow\n");
    180 #endif
    181 	     return(-1);
    182 	 }
    183 	 if (flags == IPV6_RTHDR_STRICT) {
    184 	     int c, b;
    185 	     c = rt0->ip6r0_segleft / 8;
    186 	     b = rt0->ip6r0_segleft % 8;
    187 	     rt0->ip6r0_slmap[c] |= (1 << (7 - b));
    188 	 }
    189 	 break;
    190      }
    191      default:
    192 #ifdef DEBUG
    193 	 fprintf(stderr, "inet6_rthdr_lasthop: unknown type(%d)\n",
    194 		 rthdr->ip6r_type);
    195 #endif
    196 	 return(-1);
    197     }
    198 
    199     return(0);
    200 }
    201 
    202 #if 0
    203 int
    204 inet6_rthdr_reverse(in, out)
    205     const struct cmsghdr *in;
    206     struct cmsghdr *out;
    207 {
    208 #ifdef DEBUG
    209     fprintf(stderr, "inet6_rthdr_reverse: not implemented yet\n");
    210 #endif
    211     return -1;
    212 }
    213 #endif
    214 
    215 int
    216 inet6_rthdr_segments(cmsg)
    217     const struct cmsghdr *cmsg;
    218 {
    219     register struct ip6_rthdr *rthdr;
    220 
    221     _DIAGASSERT(cmsg != NULL);
    222 #ifdef _DIAGNOSTIC
    223     if (cmsg == NULL)
    224 	return (-1);
    225 #endif
    226 
    227     rthdr = (struct ip6_rthdr *)(cmsg + 1);
    228 
    229     switch(rthdr->ip6r_type) {
    230     case IPV6_RTHDR_TYPE_0:
    231       {
    232 	struct ip6_rthdr0 *rt0 = (struct ip6_rthdr0 *)rthdr;
    233 
    234 	if (rt0->ip6r0_len % 2 || 46 < rt0->ip6r0_len) {
    235 #ifdef DEBUG
    236 	    fprintf(stderr, "inet6_rthdr_segments: invalid size(%d)\n",
    237 		rt0->ip6r0_len);
    238 #endif
    239 	    return -1;
    240 	}
    241 
    242 	return (rt0->ip6r0_len * 8) / sizeof(struct in6_addr);
    243       }
    244 
    245     default:
    246 #ifdef DEBUG
    247 	fprintf(stderr, "inet6_rthdr_segments: unknown type(%d)\n",
    248 	    rthdr->ip6r_type);
    249 #endif
    250 	return -1;
    251     }
    252 }
    253 
    254 struct in6_addr *
    255 inet6_rthdr_getaddr(cmsg, index)
    256     struct cmsghdr *cmsg;
    257     int index;
    258 {
    259     register struct ip6_rthdr *rthdr;
    260 
    261     _DIAGASSERT(cmsg != NULL);
    262 #ifdef _DIAGNOSTIC
    263     if (cmsg == NULL)
    264 	return (NULL);
    265 #endif
    266 
    267     rthdr = (struct ip6_rthdr *)(cmsg + 1);
    268 
    269     switch(rthdr->ip6r_type) {
    270     case IPV6_RTHDR_TYPE_0:
    271       {
    272 	struct ip6_rthdr0 *rt0 = (struct ip6_rthdr0 *)rthdr;
    273 	int naddr;
    274 
    275 	if (rt0->ip6r0_len % 2 || 46 < rt0->ip6r0_len) {
    276 #ifdef DEBUG
    277 	    fprintf(stderr, "inet6_rthdr_getaddr: invalid size(%d)\n",
    278 		rt0->ip6r0_len);
    279 #endif
    280 	    return NULL;
    281 	}
    282 	naddr = (rt0->ip6r0_len * 8) / sizeof(struct in6_addr);
    283 	if (index <= 0 || naddr < index) {
    284 #ifdef DEBUG
    285 	    fprintf(stderr, "inet6_rthdr_getaddr: invalid index(%d)\n", index);
    286 #endif
    287 	    return NULL;
    288 	}
    289 	return &rt0->ip6r0_addr[index - 1];
    290       }
    291 
    292     default:
    293 #ifdef DEBUG
    294 	fprintf(stderr, "inet6_rthdr_getaddr: unknown type(%d)\n",
    295 	    rthdr->ip6r_type);
    296 #endif
    297 	return NULL;
    298     }
    299 }
    300 
    301 int
    302 inet6_rthdr_getflags(cmsg, index)
    303     const struct cmsghdr *cmsg;
    304     int index;
    305 {
    306     register struct ip6_rthdr *rthdr;
    307 
    308     _DIAGASSERT(cmsg != NULL);
    309 #ifdef _DIAGNOSTIC
    310     if (cmsg == NULL)
    311 	return (-1);
    312 #endif
    313 
    314     rthdr = (struct ip6_rthdr *)(cmsg + 1);
    315 
    316     switch(rthdr->ip6r_type) {
    317     case IPV6_RTHDR_TYPE_0:
    318       {
    319 	struct ip6_rthdr0 *rt0 = (struct ip6_rthdr0 *)rthdr;
    320 	int naddr;
    321 
    322 	if (rt0->ip6r0_len % 2 || 46 < rt0->ip6r0_len) {
    323 #ifdef DEBUG
    324 	    fprintf(stderr, "inet6_rthdr_getflags: invalid size(%d)\n",
    325 		rt0->ip6r0_len);
    326 #endif
    327 	    return -1;
    328 	}
    329 	naddr = (rt0->ip6r0_len * 8) / sizeof(struct in6_addr);
    330 	if (index < 0 || naddr < index) {
    331 #ifdef DEBUG
    332 	    fprintf(stderr, "inet6_rthdr_getflags: invalid index(%d)\n", index);
    333 #endif
    334 	    return -1;
    335 	}
    336 	if (rt0->ip6r0_slmap[index / 8] & (0x80 >> (index % 8)))
    337 	    return IPV6_RTHDR_STRICT;
    338 	else
    339 	    return IPV6_RTHDR_LOOSE;
    340       }
    341 
    342     default:
    343 #ifdef DEBUG
    344 	fprintf(stderr, "inet6_rthdr_getflags: unknown type(%d)\n",
    345 	    rthdr->ip6r_type);
    346 #endif
    347 	return -1;
    348     }
    349 }
    350