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