Home | History | Annotate | Line # | Download | only in net
rthdr.c revision 1.17
      1  1.17     lukem /*	$NetBSD: rthdr.c,v 1.17 2009/02/05 23:22:39 lukem 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.17     lukem __RCSID("$NetBSD: rthdr.c,v 1.17 2009/02/05 23:22:39 lukem 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.16    rpaulo __weak_alias(inet6_rth_space, _inet6_rth_space)
     58  1.16    rpaulo __weak_alias(inet6_rth_init, _inet6_rth_init)
     59  1.16    rpaulo __weak_alias(inet6_rth_add, _inet6_rth_add)
     60  1.16    rpaulo __weak_alias(inet6_rth_reverse, _inet6_rth_reverse)
     61  1.16    rpaulo __weak_alias(inet6_rth_segments, _inet6_rth_segments)
     62  1.16    rpaulo __weak_alias(inet6_rth_getaddr, _inet6_rth_getaddr)
     63   1.8    itojun #endif
     64   1.1    itojun 
     65  1.16    rpaulo /*
     66  1.16    rpaulo  * RFC2292 API
     67  1.16    rpaulo  */
     68  1.16    rpaulo 
     69   1.1    itojun size_t
     70   1.1    itojun inet6_rthdr_space(type, seg)
     71  1.13    itojun 	int type, seg;
     72   1.1    itojun {
     73  1.13    itojun 	switch (type) {
     74  1.13    itojun 	case IPV6_RTHDR_TYPE_0:
     75  1.13    itojun 		if (seg < 1 || seg > 23)
     76  1.13    itojun 			return (0);
     77  1.14    itojun 		return (CMSG_SPACE(sizeof(struct in6_addr) * seg +
     78  1.13    itojun 		    sizeof(struct ip6_rthdr0)));
     79  1.13    itojun 	default:
     80  1.13    itojun 		return (0);
     81  1.13    itojun 	}
     82   1.1    itojun }
     83   1.1    itojun 
     84   1.1    itojun struct cmsghdr *
     85   1.1    itojun inet6_rthdr_init(bp, type)
     86  1.13    itojun 	void *bp;
     87  1.13    itojun 	int type;
     88   1.1    itojun {
     89  1.13    itojun 	struct cmsghdr *ch;
     90  1.13    itojun 	struct ip6_rthdr *rthdr;
     91   1.3     lukem 
     92  1.13    itojun 	_DIAGASSERT(bp != NULL);
     93   1.3     lukem 
     94  1.13    itojun 	ch = (struct cmsghdr *)bp;
     95  1.13    itojun 	rthdr = (struct ip6_rthdr *)(void *)CMSG_DATA(ch);
     96   1.1    itojun 
     97  1.13    itojun 	ch->cmsg_level = IPPROTO_IPV6;
     98  1.13    itojun 	ch->cmsg_type = IPV6_RTHDR;
     99  1.13    itojun 
    100  1.13    itojun 	switch (type) {
    101  1.13    itojun 	case IPV6_RTHDR_TYPE_0:
    102  1.16    rpaulo #ifdef COMPAT_RFC2292
    103  1.16    rpaulo 		ch->cmsg_len = CMSG_LEN(sizeof(struct ip6_rthdr0) -
    104  1.16    rpaulo 		    sizeof(struct in6_addr));
    105  1.16    rpaulo #else
    106  1.14    itojun 		ch->cmsg_len = CMSG_LEN(sizeof(struct ip6_rthdr0));
    107  1.16    rpaulo #endif
    108  1.13    itojun 		(void)memset(rthdr, 0, sizeof(struct ip6_rthdr0));
    109  1.13    itojun 		rthdr->ip6r_type = IPV6_RTHDR_TYPE_0;
    110  1.13    itojun 		return (ch);
    111  1.13    itojun 	default:
    112  1.13    itojun 		return (NULL);
    113  1.13    itojun 	}
    114   1.1    itojun }
    115   1.1    itojun 
    116   1.1    itojun int
    117   1.1    itojun inet6_rthdr_add(cmsg, addr, flags)
    118  1.13    itojun 	struct cmsghdr *cmsg;
    119  1.13    itojun 	const struct in6_addr *addr;
    120  1.13    itojun 	u_int flags;
    121   1.1    itojun {
    122  1.13    itojun 	struct ip6_rthdr *rthdr;
    123   1.3     lukem 
    124  1.13    itojun 	_DIAGASSERT(cmsg != NULL);
    125  1.13    itojun 	_DIAGASSERT(addr != NULL);
    126   1.3     lukem 
    127  1.13    itojun 	rthdr = (struct ip6_rthdr *)(void *)CMSG_DATA(cmsg);
    128   1.1    itojun 
    129  1.13    itojun 	switch (rthdr->ip6r_type) {
    130  1.13    itojun 	case IPV6_RTHDR_TYPE_0:
    131  1.13    itojun 	{
    132  1.13    itojun 		struct ip6_rthdr0 *rt0 = (struct ip6_rthdr0 *)(void *)rthdr;
    133  1.16    rpaulo 		if (flags != IPV6_RTHDR_LOOSE && flags != IPV6_RTHDR_STRICT)
    134  1.13    itojun 			return (-1);
    135  1.13    itojun 		if (rt0->ip6r0_segleft == 23)
    136  1.13    itojun 			return (-1);
    137  1.16    rpaulo 		if (flags != IPV6_RTHDR_LOOSE)
    138  1.16    rpaulo 			return (-1);
    139  1.13    itojun 		rt0->ip6r0_segleft++;
    140  1.13    itojun 		(void)memcpy(((caddr_t)(void *)rt0) +
    141  1.13    itojun 		    ((rt0->ip6r0_len + 1) << 3), addr, sizeof(struct in6_addr));
    142  1.13    itojun 		rt0->ip6r0_len += sizeof(struct in6_addr) >> 3;
    143  1.13    itojun 		cmsg->cmsg_len = CMSG_LEN((rt0->ip6r0_len + 1) << 3);
    144  1.13    itojun 		break;
    145  1.13    itojun 	}
    146  1.13    itojun 	default:
    147  1.13    itojun 		return (-1);
    148  1.13    itojun 	}
    149   1.1    itojun 
    150  1.13    itojun 	return (0);
    151   1.1    itojun }
    152   1.1    itojun 
    153   1.1    itojun int
    154   1.1    itojun inet6_rthdr_lasthop(cmsg, flags)
    155  1.13    itojun 	struct cmsghdr *cmsg;
    156  1.13    itojun 	unsigned int flags;
    157   1.1    itojun {
    158  1.13    itojun 	struct ip6_rthdr *rthdr;
    159   1.3     lukem 
    160  1.13    itojun 	_DIAGASSERT(cmsg != NULL);
    161   1.3     lukem 
    162  1.13    itojun 	rthdr = (struct ip6_rthdr *)(void *)CMSG_DATA(cmsg);
    163   1.1    itojun 
    164  1.13    itojun 	switch (rthdr->ip6r_type) {
    165  1.13    itojun 	case IPV6_RTHDR_TYPE_0:
    166  1.13    itojun 	{
    167  1.13    itojun 		struct ip6_rthdr0 *rt0 = (struct ip6_rthdr0 *)(void *)rthdr;
    168  1.16    rpaulo 		if (rt0->ip6r0_segleft > 23)
    169  1.16    rpaulo 			return (-1);
    170  1.14    itojun 		if (flags != IPV6_RTHDR_LOOSE)
    171  1.13    itojun 			return (-1);
    172  1.13    itojun 		break;
    173  1.13    itojun 	}
    174  1.13    itojun 	default:
    175  1.13    itojun 		return (-1);
    176  1.13    itojun 	}
    177   1.1    itojun 
    178  1.13    itojun 	return (0);
    179   1.1    itojun }
    180   1.1    itojun 
    181   1.1    itojun #if 0
    182   1.1    itojun int
    183   1.1    itojun inet6_rthdr_reverse(in, out)
    184  1.13    itojun 	const struct cmsghdr *in;
    185  1.13    itojun 	struct cmsghdr *out;
    186   1.1    itojun {
    187  1.13    itojun 
    188  1.13    itojun 	return (-1);
    189   1.1    itojun }
    190   1.1    itojun #endif
    191   1.1    itojun 
    192   1.1    itojun int
    193   1.1    itojun inet6_rthdr_segments(cmsg)
    194  1.13    itojun 	const struct cmsghdr *cmsg;
    195   1.1    itojun {
    196  1.13    itojun 	const struct ip6_rthdr *rthdr;
    197  1.13    itojun 
    198  1.13    itojun 	_DIAGASSERT(cmsg != NULL);
    199  1.13    itojun 
    200  1.15  christos 	rthdr = __UNCONST(CCMSG_DATA(cmsg));
    201   1.3     lukem 
    202  1.13    itojun 	switch (rthdr->ip6r_type) {
    203  1.13    itojun 	case IPV6_RTHDR_TYPE_0:
    204  1.13    itojun 	{
    205  1.13    itojun 		const struct ip6_rthdr0 *rt0 =
    206  1.14    itojun 		    (const struct ip6_rthdr0 *)(const void *)rthdr;
    207   1.3     lukem 
    208  1.13    itojun 		if (rt0->ip6r0_len % 2 || 46 < rt0->ip6r0_len)
    209  1.13    itojun 			return (-1);
    210   1.1    itojun 
    211  1.13    itojun 		return (rt0->ip6r0_len * 8) / sizeof(struct in6_addr);
    212  1.13    itojun 	}
    213  1.13    itojun 
    214  1.13    itojun 	default:
    215  1.13    itojun 		return (-1);
    216  1.13    itojun 	}
    217   1.1    itojun }
    218   1.1    itojun 
    219   1.1    itojun struct in6_addr *
    220  1.11     lukem inet6_rthdr_getaddr(cmsg, idx)
    221  1.13    itojun 	struct cmsghdr *cmsg;
    222  1.13    itojun 	int idx;
    223   1.1    itojun {
    224  1.13    itojun 	struct ip6_rthdr *rthdr;
    225   1.3     lukem 
    226  1.13    itojun 	_DIAGASSERT(cmsg != NULL);
    227   1.3     lukem 
    228  1.13    itojun 	rthdr = (struct ip6_rthdr *)(void *)CMSG_DATA(cmsg);
    229  1.13    itojun 
    230  1.13    itojun 	switch (rthdr->ip6r_type) {
    231  1.13    itojun 	case IPV6_RTHDR_TYPE_0:
    232  1.13    itojun 	{
    233  1.13    itojun 		struct ip6_rthdr0 *rt0 = (struct ip6_rthdr0 *)(void *)rthdr;
    234  1.13    itojun 		int naddr;
    235  1.13    itojun 
    236  1.13    itojun 		if (rt0->ip6r0_len % 2 || 46 < rt0->ip6r0_len)
    237  1.13    itojun 			return NULL;
    238  1.13    itojun 		naddr = (rt0->ip6r0_len * 8) / sizeof(struct in6_addr);
    239  1.13    itojun 		if (idx <= 0 || naddr < idx)
    240  1.13    itojun 			return NULL;
    241  1.16    rpaulo #ifdef COMPAT_RFC2292
    242  1.16    rpaulo 		return ((struct in6_addr *)(void *)(rt0 + 1)) + idx - 1;
    243  1.16    rpaulo #else
    244  1.14    itojun 		return ((struct in6_addr *)(void *)(rt0 + 1)) + idx;
    245  1.16    rpaulo #endif
    246  1.13    itojun 	}
    247   1.1    itojun 
    248  1.13    itojun 	default:
    249  1.13    itojun 		return NULL;
    250  1.13    itojun 	}
    251   1.1    itojun }
    252   1.1    itojun 
    253   1.1    itojun int
    254  1.11     lukem inet6_rthdr_getflags(cmsg, idx)
    255  1.13    itojun 	const struct cmsghdr *cmsg;
    256  1.13    itojun 	int idx;
    257   1.1    itojun {
    258  1.13    itojun 	const struct ip6_rthdr *rthdr;
    259  1.13    itojun 
    260  1.13    itojun 	_DIAGASSERT(cmsg != NULL);
    261   1.3     lukem 
    262  1.15  christos 	rthdr = __UNCONST(CCMSG_DATA(cmsg));
    263   1.3     lukem 
    264  1.13    itojun 	switch (rthdr->ip6r_type) {
    265  1.13    itojun 	case IPV6_RTHDR_TYPE_0:
    266  1.13    itojun 	{
    267  1.13    itojun 		const struct ip6_rthdr0 *rt0 = (const struct ip6_rthdr0 *)
    268  1.13    itojun 		(const void *)rthdr;
    269  1.13    itojun 		int naddr;
    270  1.13    itojun 
    271  1.13    itojun 		if (rt0->ip6r0_len % 2 || 46 < rt0->ip6r0_len)
    272  1.13    itojun 			return (-1);
    273  1.13    itojun 		naddr = (rt0->ip6r0_len * 8) / sizeof(struct in6_addr);
    274  1.13    itojun 		if (idx < 0 || naddr < idx)
    275  1.13    itojun 			return (-1);
    276  1.14    itojun 		return IPV6_RTHDR_LOOSE;
    277  1.13    itojun 	}
    278   1.1    itojun 
    279  1.13    itojun 	default:
    280  1.13    itojun 		return (-1);
    281  1.13    itojun 	}
    282   1.1    itojun }
    283  1.16    rpaulo 
    284  1.16    rpaulo /*
    285  1.16    rpaulo  * RFC3542 (2292bis) API
    286  1.16    rpaulo  */
    287  1.16    rpaulo 
    288  1.16    rpaulo socklen_t
    289  1.16    rpaulo inet6_rth_space(int type, int segments)
    290  1.16    rpaulo {
    291  1.16    rpaulo 	switch (type) {
    292  1.16    rpaulo 	case IPV6_RTHDR_TYPE_0:
    293  1.16    rpaulo 		return (((segments * 2) + 1) << 3);
    294  1.16    rpaulo 	default:
    295  1.16    rpaulo 		return (0);	/* type not suppported */
    296  1.16    rpaulo 	}
    297  1.16    rpaulo }
    298  1.16    rpaulo 
    299  1.16    rpaulo void *
    300  1.16    rpaulo inet6_rth_init(void *bp, socklen_t bp_len, int type, int segments)
    301  1.16    rpaulo {
    302  1.16    rpaulo 	struct ip6_rthdr *rth;
    303  1.16    rpaulo 	struct ip6_rthdr0 *rth0;
    304  1.16    rpaulo 
    305  1.16    rpaulo 	_DIAGASSERT(bp != NULL);
    306  1.16    rpaulo 
    307  1.16    rpaulo 	rth = (struct ip6_rthdr *)bp;
    308  1.16    rpaulo 
    309  1.16    rpaulo 	switch (type) {
    310  1.16    rpaulo 	case IPV6_RTHDR_TYPE_0:
    311  1.16    rpaulo 		/* length validation */
    312  1.16    rpaulo 		if (bp_len < inet6_rth_space(IPV6_RTHDR_TYPE_0, segments))
    313  1.16    rpaulo 			return (NULL);
    314  1.16    rpaulo 
    315  1.16    rpaulo 		memset(bp, 0, bp_len);
    316  1.16    rpaulo 		rth0 = (struct ip6_rthdr0 *)(void *)rth;
    317  1.16    rpaulo 		rth0->ip6r0_len = segments * 2;
    318  1.16    rpaulo 		rth0->ip6r0_type = IPV6_RTHDR_TYPE_0;
    319  1.16    rpaulo 		rth0->ip6r0_segleft = 0;
    320  1.16    rpaulo 		rth0->ip6r0_reserved = 0;
    321  1.16    rpaulo 		break;
    322  1.16    rpaulo 	default:
    323  1.16    rpaulo 		return (NULL);	/* type not supported */
    324  1.16    rpaulo 	}
    325  1.16    rpaulo 
    326  1.16    rpaulo 	return (bp);
    327  1.16    rpaulo }
    328  1.16    rpaulo 
    329  1.16    rpaulo int
    330  1.16    rpaulo inet6_rth_add(void *bp, const struct in6_addr *addr)
    331  1.16    rpaulo {
    332  1.16    rpaulo 	struct ip6_rthdr *rth;
    333  1.16    rpaulo 	struct ip6_rthdr0 *rth0;
    334  1.16    rpaulo 	struct in6_addr *nextaddr;
    335  1.16    rpaulo 
    336  1.16    rpaulo 	_DIAGASSERT(bp != NULL);
    337  1.16    rpaulo 
    338  1.16    rpaulo 	rth = (struct ip6_rthdr *)bp;
    339  1.16    rpaulo 
    340  1.16    rpaulo 	switch (rth->ip6r_type) {
    341  1.16    rpaulo 	case IPV6_RTHDR_TYPE_0:
    342  1.16    rpaulo 		rth0 = (struct ip6_rthdr0 *)(void *)rth;
    343  1.16    rpaulo 		nextaddr = (struct in6_addr *)(void *)(rth0 + 1)
    344  1.16    rpaulo 		    + rth0->ip6r0_segleft;
    345  1.16    rpaulo 		*nextaddr = *addr;
    346  1.16    rpaulo 		rth0->ip6r0_segleft++;
    347  1.16    rpaulo 		break;
    348  1.16    rpaulo 	default:
    349  1.16    rpaulo 		return (-1);	/* type not supported */
    350  1.16    rpaulo 	}
    351  1.16    rpaulo 
    352  1.16    rpaulo 	return (0);
    353  1.16    rpaulo }
    354  1.16    rpaulo 
    355  1.16    rpaulo int
    356  1.16    rpaulo inet6_rth_reverse(const void *in, void *out)
    357  1.16    rpaulo {
    358  1.16    rpaulo 	const struct ip6_rthdr *rth_in;
    359  1.16    rpaulo 	const struct ip6_rthdr0 *rth0_in;
    360  1.16    rpaulo 	struct ip6_rthdr0 *rth0_out;
    361  1.16    rpaulo 	int i, segments;
    362  1.16    rpaulo 
    363  1.16    rpaulo 	_DIAGASSERT(in != NULL);
    364  1.16    rpaulo 	_DIAGASSERT(out != NULL);
    365  1.16    rpaulo 
    366  1.16    rpaulo 	rth_in = (const struct ip6_rthdr *)in;
    367  1.16    rpaulo 
    368  1.16    rpaulo 	switch (rth_in->ip6r_type) {
    369  1.16    rpaulo 	case IPV6_RTHDR_TYPE_0:
    370  1.16    rpaulo 		rth0_in = (const struct ip6_rthdr0 *)in;
    371  1.16    rpaulo 		rth0_out = (struct ip6_rthdr0 *)out;
    372  1.16    rpaulo 
    373  1.16    rpaulo 		/* parameter validation XXX too paranoid? */
    374  1.16    rpaulo 		if (rth0_in->ip6r0_len % 2)
    375  1.16    rpaulo 			return (-1);
    376  1.16    rpaulo 		segments = rth0_in->ip6r0_len / 2;
    377  1.16    rpaulo 
    378  1.16    rpaulo 		/* we can't use memcpy here, since in and out may overlap */
    379  1.16    rpaulo 		memmove((void *)rth0_out, (const void *)rth0_in,
    380  1.16    rpaulo 			(unsigned int)(((rth0_in->ip6r0_len) + 1) << 3));
    381  1.16    rpaulo 		rth0_out->ip6r0_segleft = segments;
    382  1.16    rpaulo 
    383  1.16    rpaulo 		/* reverse the addresses */
    384  1.16    rpaulo 		for (i = 0; i < segments / 2; i++) {
    385  1.16    rpaulo 			struct in6_addr addr_tmp, *addr1, *addr2;
    386  1.16    rpaulo 
    387  1.16    rpaulo 			addr1 = (struct in6_addr *)(void *)(rth0_out + 1) + i;
    388  1.16    rpaulo 			addr2 = (struct in6_addr *)(void *)(rth0_out + 1) +
    389  1.16    rpaulo 				(segments - i - 1);
    390  1.16    rpaulo 			addr_tmp = *addr1;
    391  1.16    rpaulo 			*addr1 = *addr2;
    392  1.16    rpaulo 			*addr2 = addr_tmp;
    393  1.16    rpaulo 		}
    394  1.16    rpaulo 
    395  1.16    rpaulo 		break;
    396  1.16    rpaulo 	default:
    397  1.16    rpaulo 		return (-1);	/* type not supported */
    398  1.16    rpaulo 	}
    399  1.16    rpaulo 
    400  1.16    rpaulo 	return (0);
    401  1.16    rpaulo }
    402  1.16    rpaulo 
    403  1.16    rpaulo int
    404  1.16    rpaulo inet6_rth_segments(const void *bp)
    405  1.16    rpaulo {
    406  1.16    rpaulo 	const struct ip6_rthdr *rh;
    407  1.16    rpaulo 	const struct ip6_rthdr0 *rh0;
    408  1.16    rpaulo 	unsigned int addrs;
    409  1.16    rpaulo 
    410  1.16    rpaulo 	_DIAGASSERT(bp != NULL);
    411  1.16    rpaulo 
    412  1.16    rpaulo 	rh = (const struct ip6_rthdr *)bp;
    413  1.16    rpaulo 
    414  1.16    rpaulo 	switch (rh->ip6r_type) {
    415  1.16    rpaulo 	case IPV6_RTHDR_TYPE_0:
    416  1.16    rpaulo 		rh0 = (const struct ip6_rthdr0 *)bp;
    417  1.16    rpaulo 
    418  1.16    rpaulo 		/*
    419  1.16    rpaulo 		 * Validation for a type-0 routing header.
    420  1.16    rpaulo 		 * Is this too strict?
    421  1.16    rpaulo 		 */
    422  1.16    rpaulo 		if ((rh0->ip6r0_len % 2) != 0 ||
    423  1.16    rpaulo 		    (addrs = (rh0->ip6r0_len / 2)) < rh0->ip6r0_segleft)
    424  1.16    rpaulo 			return (-1);
    425  1.16    rpaulo 
    426  1.16    rpaulo 		return (addrs);
    427  1.16    rpaulo 	default:
    428  1.16    rpaulo 		return (-1);	/* unknown type */
    429  1.16    rpaulo 	}
    430  1.16    rpaulo }
    431  1.16    rpaulo 
    432  1.16    rpaulo struct in6_addr *
    433  1.16    rpaulo inet6_rth_getaddr(const void *bp, int idx)
    434  1.16    rpaulo {
    435  1.16    rpaulo 	const struct ip6_rthdr *rh;
    436  1.16    rpaulo 	const struct ip6_rthdr0 *rh0;
    437  1.16    rpaulo 	unsigned int addrs;
    438  1.16    rpaulo 
    439  1.16    rpaulo 	_DIAGASSERT(bp != NULL);
    440  1.16    rpaulo 
    441  1.16    rpaulo 	rh = (const struct ip6_rthdr *)bp;
    442  1.16    rpaulo 
    443  1.16    rpaulo 	switch (rh->ip6r_type) {
    444  1.16    rpaulo 	case IPV6_RTHDR_TYPE_0:
    445  1.16    rpaulo 		 rh0 = (const struct ip6_rthdr0 *)bp;
    446  1.16    rpaulo 
    447  1.16    rpaulo 		/*
    448  1.16    rpaulo 		 * Validation for a type-0 routing header.
    449  1.16    rpaulo 		 * Is this too strict?
    450  1.16    rpaulo 		 */
    451  1.16    rpaulo 		if ((rh0->ip6r0_len % 2) != 0 ||
    452  1.16    rpaulo 		    (addrs = (rh0->ip6r0_len / 2)) < rh0->ip6r0_segleft)
    453  1.16    rpaulo 			return (NULL);
    454  1.16    rpaulo 
    455  1.17     lukem 		if (idx < 0 || addrs <= (unsigned int)idx)
    456  1.16    rpaulo 			return (NULL);
    457  1.16    rpaulo 
    458  1.16    rpaulo 		return (((struct in6_addr *)(void *)__UNCONST(rh0 + 1)) + idx);
    459  1.16    rpaulo 	default:
    460  1.16    rpaulo 		return (NULL);	/* unknown type */
    461  1.16    rpaulo 	}
    462  1.16    rpaulo }
    463