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