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