Home | History | Annotate | Line # | Download | only in net
rthdr.c revision 1.1
      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/param.h>
     31 #include <sys/types.h>
     32 #include <sys/socket.h>
     33 
     34 #include <netinet/in.h>
     35 #include <netinet/ip6.h>
     36 
     37 #include <string.h>
     38 #include <stdio.h>
     39 
     40 size_t
     41 inet6_rthdr_space(type, seg)
     42     int type, seg;
     43 {
     44     switch(type) {
     45      case IPV6_RTHDR_TYPE_0:
     46 	 if (seg < 1 || seg > 23)
     47 	     return(0);
     48 	 return(CMSG_SPACE(sizeof(struct in6_addr) * (seg - 1)
     49 			   + sizeof(struct ip6_rthdr0)));
     50      default:
     51 #ifdef DEBUG
     52 	 fprintf(stderr, "inet6_rthdr_space: unknown type(%d)\n", type);
     53 #endif
     54 	 return(0);
     55     }
     56 }
     57 
     58 struct cmsghdr *
     59 inet6_rthdr_init(bp, type)
     60     void *bp;
     61     int type;
     62 {
     63     register struct cmsghdr *ch = (struct cmsghdr *)bp;
     64     register struct ip6_rthdr *rthdr = (struct ip6_rthdr *)(ch + 1);
     65 
     66     ch->cmsg_level = IPPROTO_IPV6;
     67     ch->cmsg_type = IPV6_RTHDR;
     68 
     69     switch(type) {
     70      case IPV6_RTHDR_TYPE_0:
     71 	 ch->cmsg_len = CMSG_LEN(sizeof(struct ip6_rthdr0) - sizeof(struct in6_addr));
     72 	 bzero(rthdr, sizeof(struct ip6_rthdr0));
     73 	 rthdr->ip6r_type = IPV6_RTHDR_TYPE_0;
     74 	 return(ch);
     75      default:
     76 #ifdef DEBUG
     77 	 fprintf(stderr, "inet6_rthdr_init: unknown type(%d)\n", type);
     78 #endif
     79 	 return(NULL);
     80     }
     81 }
     82 
     83 int
     84 inet6_rthdr_add(cmsg, addr, flags)
     85     struct cmsghdr *cmsg;
     86     const struct in6_addr *addr;
     87     u_int flags;
     88 {
     89     register struct ip6_rthdr *rthdr = (struct ip6_rthdr *)(cmsg + 1);
     90 
     91     switch(rthdr->ip6r_type) {
     92      case IPV6_RTHDR_TYPE_0:
     93      {
     94 	 struct ip6_rthdr0 *rt0 = (struct ip6_rthdr0 *)rthdr;
     95 	 if (flags != IPV6_RTHDR_LOOSE && flags != IPV6_RTHDR_STRICT) {
     96 #ifdef DEBUG
     97 	     fprintf(stderr, "inet6_rthdr_add: unsupported flag(%d)\n", flags);
     98 #endif
     99 	     return(-1);
    100 	 }
    101 	 if (rt0->ip6r0_segleft == 23) {
    102 #ifdef DEBUG
    103 	     fprintf(stderr, "inet6_rthdr_add: segment overflow\n");
    104 #endif
    105 	     return(-1);
    106 	 }
    107 	 if (flags == IPV6_RTHDR_STRICT) {
    108 	     int c, b;
    109 	     c = rt0->ip6r0_segleft / 8;
    110 	     b = rt0->ip6r0_segleft % 8;
    111 	     rt0->ip6r0_slmap[c] |= (1 << (7 - b));
    112 	 }
    113 	 rt0->ip6r0_segleft++;
    114 	 bcopy(addr, (caddr_t)rt0 + ((rt0->ip6r0_len + 1) << 3),
    115 	       sizeof(struct in6_addr));
    116 	 rt0->ip6r0_len += sizeof(struct in6_addr) >> 3;
    117 	 cmsg->cmsg_len = CMSG_LEN((rt0->ip6r0_len + 1) << 3);
    118 	 break;
    119      }
    120      default:
    121 #ifdef DEBUG
    122 	 fprintf(stderr, "inet6_rthdr_add: unknown type(%d)\n",
    123 		 rthdr->ip6r_type);
    124 #endif
    125 	 return(-1);
    126     }
    127 
    128     return(0);
    129 }
    130 
    131 int
    132 inet6_rthdr_lasthop(cmsg, flags)
    133     struct cmsghdr *cmsg;
    134     unsigned int flags;
    135 {
    136     register struct ip6_rthdr *rthdr = (struct ip6_rthdr *)(cmsg + 1);
    137 
    138     switch(rthdr->ip6r_type) {
    139      case IPV6_RTHDR_TYPE_0:
    140      {
    141 	 struct ip6_rthdr0 *rt0 = (struct ip6_rthdr0 *)rthdr;
    142 	 if (flags != IPV6_RTHDR_LOOSE && flags != IPV6_RTHDR_STRICT) {
    143 #ifdef DEBUG
    144 	     fprintf(stderr, "inet6_rthdr_lasthop: unsupported flag(%d)\n", flags);
    145 #endif
    146 	     return(-1);
    147 	 }
    148 	 if (rt0->ip6r0_segleft > 23) {
    149 #ifdef DEBUG
    150 	     fprintf(stderr, "inet6_rthdr_add: segment overflow\n");
    151 #endif
    152 	     return(-1);
    153 	 }
    154 	 if (flags == IPV6_RTHDR_STRICT) {
    155 	     int c, b;
    156 	     c = rt0->ip6r0_segleft / 8;
    157 	     b = rt0->ip6r0_segleft % 8;
    158 	     rt0->ip6r0_slmap[c] |= (1 << (7 - b));
    159 	 }
    160 	 break;
    161      }
    162      default:
    163 #ifdef DEBUG
    164 	 fprintf(stderr, "inet6_rthdr_lasthop: unknown type(%d)\n",
    165 		 rthdr->ip6r_type);
    166 #endif
    167 	 return(-1);
    168     }
    169 
    170     return(0);
    171 }
    172 
    173 #if 0
    174 int
    175 inet6_rthdr_reverse(in, out)
    176     const struct cmsghdr *in;
    177     struct cmsghdr *out;
    178 {
    179 #ifdef DEBUG
    180     fprintf(stderr, "inet6_rthdr_reverse: not implemented yet\n");
    181 #endif
    182     return -1;
    183 }
    184 #endif
    185 
    186 int
    187 inet6_rthdr_segments(cmsg)
    188     const struct cmsghdr *cmsg;
    189 {
    190     register struct ip6_rthdr *rthdr = (struct ip6_rthdr *)(cmsg + 1);
    191 
    192     switch(rthdr->ip6r_type) {
    193     case IPV6_RTHDR_TYPE_0:
    194       {
    195 	struct ip6_rthdr0 *rt0 = (struct ip6_rthdr0 *)rthdr;
    196 
    197 	if (rt0->ip6r0_len % 2 || 46 < rt0->ip6r0_len) {
    198 #ifdef DEBUG
    199 	    fprintf(stderr, "inet6_rthdr_segments: invalid size(%d)\n",
    200 		rt0->ip6r0_len);
    201 #endif
    202 	    return -1;
    203 	}
    204 
    205 	return (rt0->ip6r0_len * 8) / sizeof(struct in6_addr);
    206       }
    207 
    208     default:
    209 #ifdef DEBUG
    210 	fprintf(stderr, "inet6_rthdr_segments: unknown type(%d)\n",
    211 	    rthdr->ip6r_type);
    212 #endif
    213 	return -1;
    214     }
    215 }
    216 
    217 struct in6_addr *
    218 inet6_rthdr_getaddr(cmsg, index)
    219     struct cmsghdr *cmsg;
    220     int index;
    221 {
    222     register struct ip6_rthdr *rthdr = (struct ip6_rthdr *)(cmsg + 1);
    223 
    224     switch(rthdr->ip6r_type) {
    225     case IPV6_RTHDR_TYPE_0:
    226       {
    227 	struct ip6_rthdr0 *rt0 = (struct ip6_rthdr0 *)rthdr;
    228 	int naddr;
    229 
    230 	if (rt0->ip6r0_len % 2 || 46 < rt0->ip6r0_len) {
    231 #ifdef DEBUG
    232 	    fprintf(stderr, "inet6_rthdr_getaddr: invalid size(%d)\n",
    233 		rt0->ip6r0_len);
    234 #endif
    235 	    return NULL;
    236 	}
    237 	naddr = (rt0->ip6r0_len * 8) / sizeof(struct in6_addr);
    238 	if (index <= 0 || naddr < index) {
    239 #ifdef DEBUG
    240 	    fprintf(stderr, "inet6_rthdr_getaddr: invalid index(%d)\n", index);
    241 #endif
    242 	    return NULL;
    243 	}
    244 	return &rt0->ip6r0_addr[index - 1];
    245       }
    246 
    247     default:
    248 #ifdef DEBUG
    249 	fprintf(stderr, "inet6_rthdr_getaddr: unknown type(%d)\n",
    250 	    rthdr->ip6r_type);
    251 #endif
    252 	return NULL;
    253     }
    254 }
    255 
    256 int
    257 inet6_rthdr_getflags(cmsg, index)
    258     const struct cmsghdr *cmsg;
    259     int index;
    260 {
    261     register struct ip6_rthdr *rthdr = (struct ip6_rthdr *)(cmsg + 1);
    262 
    263     switch(rthdr->ip6r_type) {
    264     case IPV6_RTHDR_TYPE_0:
    265       {
    266 	struct ip6_rthdr0 *rt0 = (struct ip6_rthdr0 *)rthdr;
    267 	int naddr;
    268 
    269 	if (rt0->ip6r0_len % 2 || 46 < rt0->ip6r0_len) {
    270 #ifdef DEBUG
    271 	    fprintf(stderr, "inet6_rthdr_getflags: invalid size(%d)\n",
    272 		rt0->ip6r0_len);
    273 #endif
    274 	    return -1;
    275 	}
    276 	naddr = (rt0->ip6r0_len * 8) / sizeof(struct in6_addr);
    277 	if (index < 0 || naddr < index) {
    278 #ifdef DEBUG
    279 	    fprintf(stderr, "inet6_rthdr_getflags: invalid index(%d)\n", index);
    280 #endif
    281 	    return -1;
    282 	}
    283 	if (rt0->ip6r0_slmap[index / 8] & (0x80 >> (index % 8)))
    284 	    return IPV6_RTHDR_STRICT;
    285 	else
    286 	    return IPV6_RTHDR_LOOSE;
    287       }
    288 
    289     default:
    290 #ifdef DEBUG
    291 	fprintf(stderr, "inet6_rthdr_getflags: unknown type(%d)\n",
    292 	    rthdr->ip6r_type);
    293 #endif
    294 	return -1;
    295     }
    296 }
    297