From 4e33952cfed470971b26120bf2844b19653363e0 Mon Sep 17 00:00:00 2001
From: itojun <>
Date: Sat, 11 Dec 1999 08:09:11 +0000
Subject: add inet6_option_* and inet6_rthdr_*. increase shlib minor.

---
 src/lib/libc/net/Makefile.inc         |  20 +-
 src/lib/libc/net/inet6_option_space.3 | 452 ++++++++++++++++++++++++++++++++++
 src/lib/libc/net/inet6_rthdr_space.3  | 322 ++++++++++++++++++++++++
 src/lib/libc/net/ip6opt.c             | 388 +++++++++++++++++++++++++++++
 src/lib/libc/net/rthdr.c              | 298 ++++++++++++++++++++++
 src/lib/libc/net/vars6.c              |  42 ++++
 6 files changed, 1520 insertions(+), 2 deletions(-)
 create mode 100644 src/lib/libc/net/inet6_option_space.3
 create mode 100644 src/lib/libc/net/inet6_rthdr_space.3
 create mode 100644 src/lib/libc/net/ip6opt.c
 create mode 100644 src/lib/libc/net/rthdr.c
 create mode 100644 src/lib/libc/net/vars6.c

(limited to 'src')

diff --git a/src/lib/libc/net/Makefile.inc b/src/lib/libc/net/Makefile.inc
index 95f937a320..8b708aae9a 100644
--- a/src/lib/libc/net/Makefile.inc
+++ b/src/lib/libc/net/Makefile.inc
@@ -1,4 +1,4 @@
-#	$OpenBSD: Makefile.inc,v 1.25 1999/11/17 05:22:41 millert Exp $
+#	$OpenBSD: Makefile.inc,v 1.26 1999/12/11 08:09:11 itojun Exp $
 
 # net sources
 .PATH: ${LIBCSRCDIR}/arch/${MACHINE_ARCH}/net ${LIBCSRCDIR}/net
@@ -19,6 +19,9 @@ SRCS+=	base64.c freeaddrinfo.c gai_strerror.c getaddrinfo.c gethostnamadr.c \
 	res_init.c res_mkquery.c res_query.c res_random.c res_send.c send.c \
 	sethostent.c ethers.c rcmdsh.c
 
+# IPv6
+SRCS+=	ip6opt.c rthdr.c vars6.c
+
 # machine-dependent net sources
 # m-d Makefile.inc must include sources for:
 #	htonl() htons() ntohl() ntohs()
@@ -28,7 +31,8 @@ SRCS+=	base64.c freeaddrinfo.c gai_strerror.c getaddrinfo.c gethostnamadr.c \
 MAN+=	byteorder.3 ethers.3 getaddrinfo.3 gethostbyname.3 getnameinfo.3 \
 	getnetent.3 getprotoent.3 getservent.3 inet.3 if_indextoname.3 \
 	inet_net.3 iso_addr.3 link_addr.3 ns.3 ipx.3 \
-	rcmd.3 rcmdsh.3 resolver.3 net_addrcmp.3
+	rcmd.3 rcmdsh.3 resolver.3 net_addrcmp.3 \
+	inet6_option_space.3 inet6_rthdr_space.3
 
 
 MLINKS+=byteorder.3 htonl.3 byteorder.3 htons.3 byteorder.3 ntohl.3 \
@@ -64,3 +68,15 @@ MLINKS+=rcmd.3 iruserok.3 rcmd.3 rresvport.3 rcmd.3 ruserok.3
 MLINKS+=resolver.3 dn_comp.3 resolver.3 dn_expand.3 resolver.3 res_init.3 \
 	resolver.3 res_mkquery.3 resolver.3 res_send.3 resolver.3 res_query.3 \
 	resolver.3 res_search.3
+MLINKS+=inet6_option_space.3 inet6_option_init.3 \
+	inet6_option_space.3 inet6_option_append.3 \
+	inet6_option_space.3 inet6_option_alloc.3 \
+	inet6_option_space.3 inet6_option_next.3 \
+	inet6_option_space.3 inet6_option_find.3
+MLINKS+=inet6_rthdr_space.3 inet6_rthdr_init.3 \
+	inet6_rthdr_space.3 inet6_rthdr_add.3 \
+	inet6_rthdr_space.3 inet6_rthdr_lasthop.3 \
+	inet6_rthdr_space.3 inet6_rthdr_reverse.3 \
+	inet6_rthdr_space.3 inet6_rthdr_segments.3 \
+	inet6_rthdr_space.3 inet6_rthdr_getaddr.3 \
+	inet6_rthdr_space.3 inet6_rthdr_getflags.3
diff --git a/src/lib/libc/net/inet6_option_space.3 b/src/lib/libc/net/inet6_option_space.3
new file mode 100644
index 0000000000..2427b71216
--- /dev/null
+++ b/src/lib/libc/net/inet6_option_space.3
@@ -0,0 +1,452 @@
+.\"	$OpenBSD: inet6_option_space.3,v 1.1 1999/12/11 08:09:11 itojun Exp $
+.\"
+.\" Copyright (c) 1983, 1987, 1991, 1993
+.\"	The Regents of the University of California.  All rights reserved.
+.\"
+.\" Redistribution and use in source and binary forms, with or without
+.\" modification, are permitted provided that the following conditions
+.\" are met:
+.\" 1. Redistributions of source code must retain the above copyright
+.\"    notice, this list of conditions and the following disclaimer.
+.\" 2. Redistributions in binary form must reproduce the above copyright
+.\"    notice, this list of conditions and the following disclaimer in the
+.\"    documentation and/or other materials provided with the distribution.
+.\" 3. All advertising materials mentioning features or use of this software
+.\"    must display the following acknowledgement:
+.\"	This product includes software developed by the University of
+.\"	California, Berkeley and its contributors.
+.\" 4. Neither the name of the University nor the names of its contributors
+.\"    may be used to endorse or promote products derived from this software
+.\"    without specific prior written permission.
+.\"
+.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
+.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
+.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+.\" SUCH DAMAGE.
+.\"
+.\"     From: @(#)gethostbyname.3	8.4 (Berkeley) 5/25/95
+.\"     KAME Id: inet6_option_space.3,v 1.2 1999/12/10 04:46:16 itojun Exp
+.\"
+.Dd December 10, 1999
+.Dt INET6_OPTION_SPACE 3
+.Os
+.\"
+.Sh NAME
+.Nm inet6_option_space ,
+.Nm inet6_option_init ,
+.Nm inet6_option_append ,
+.Nm inet6_option_alloc ,
+.Nm inet6_option_next ,
+.Nm inet6_option_find
+.Nd IPv6 Hop-by-Hop and Destination Options manipulation
+.\"
+.Sh SYNOPSIS
+.Fd #include <netinet/in.h>
+.Ft "int"
+.Fn inet6_options_space "int nbytes"
+.Ft "int"
+.Fn inet6_option_init "void *bp" "struct cmsghdr **cmsgp" "int type"
+.Ft "int"
+.Fn inet6_option_append "struct cmsghdr *cmsg" "const u_int8_t *typep" "int multx" "int plusy"
+.Ft "u_int8_t *"
+.Fn inet6_option_alloc "struct cmsghdr *cmsg" "int datalen" "int multx" "int plusy";
+.Ft "int"
+.Fn inet6_option_next "const struct cmsghdr *cmsg" "u_int8_t **tptrp"
+.Ft "int"
+.Fn inet6_option_find "const struct cmsghdr *cmsg" "u_int8_t **tptrp" "int type"
+.\"
+.Sh DESCRIPTION
+.\"
+Building and parsing the Hop-by-Hop and Destination options is
+complicated due to alignment constranints, padding and
+ancillary data manipulation.
+RFC2292 defines a set of functions to help the application.
+The function prototypes for
+these functions are all in the
+.Aq Li netinet/in.h
+header.
+.\"
+.Ss inet6_option_space
+.Fn inet6_option_space
+returns the number of bytes required to hold an option when it is stored as
+ancillary data, including the
+.Li cmsghdr
+structure at the beginning,
+and any padding at the end
+.Po
+to make its size a multiple of 8 bytes
+.Pc .
+The argument is the size of the structure defining the option,
+which must include any pad bytes at the beginning
+.Po
+the value
+.Li y
+in the alignment term
+.Dq Li xn + y
+.Pc ,
+the type byte, the length byte, and the option data.
+.Pp
+Note: If multiple options are stored in a single ancillary data
+object, which is the recommended technique, this function
+overestimates the amount of space required by the size of
+.Li N-1
+.Li cmsghdr
+structures,
+where
+.Li N
+is the number of options to be stored in the object.
+This is of little consequence, since it is assumed that most
+Hop-by-Hop option headers and Destination option headers carry only
+one option
+.Pq appendix B of [RFC-2460] .
+.\"
+.Ss inet6_option_init
+.Fn inet6_option_init
+is called once per ancillary data object that will
+contain either Hop-by-Hop or Destination options.
+It returns
+.Li 0
+on success or
+.Li -1
+on an error.
+.Pp
+.Fa bp
+is a pointer to previously allocated space that will contain the
+ancillary data object.
+It must be large enough to contain all the
+individual options to be added by later calls to
+.Fn inet6_option_append
+and
+.Fn inet6_option_alloc .
+.Pp
+.Fa cmsgp
+is a pointer to a pointer to a
+.Li cmsghdr
+structure.
+.Fa *cmsgp
+is initialized by this function to point to the
+.Li cmsghdr
+structure constructed by this function in the buffer pointed to by
+.Fa bp .
+.Pp
+.Fa type
+is either
+.Dv IPV6_HOPOPTS
+or
+.Dv IPV6_DSTOPTS .
+This
+.Fa type
+is stored in the
+.Li cmsg_type
+member of the
+.Li cmsghdr
+structure pointed to by
+.Fa *cmsgp .
+.\"
+.Ss inet6_option_append
+This function appends a Hop-by-Hop option or a Destination option
+into an ancillary data object that has been initialized by
+.Fn inet6_option_init .
+This function returns
+.Li 0
+if it succeeds or
+.Li -1
+on an error.
+.Pp
+.Fa cmsg
+is a pointer to the
+.Li cmsghdr
+structure that must have been
+initialized by
+.Fn inet6_option_init .
+.Pp
+.Fa typep
+is a pointer to the 8-bit option type.
+It is assumed that this
+field is immediately followed by the 8-bit option data length field,
+which is then followed immediately by the option data.
+The caller
+initializes these three fields
+.Pq the type-length-value, or TLV
+before calling this function.
+.Pp
+The option type must have a value from
+.Li 2
+to
+.Li 255 , inclusive.
+.Po
+.Li 0
+and
+.Li 1
+are reserved for the
+.Li Pad1
+and
+.Li PadN
+options, respectively.
+.Pc
+.Pp
+The option data length must have a value between
+.Li 0
+and
+.Li 255 ,
+inclusive, and is the length of the option data that follows.
+.Pp
+.Fa multx
+is the value
+.Li x
+in the alignment term
+.Dq Li xn + y .
+It must have a value of
+.Li 1 ,
+.Li 2 ,
+.Li 4 ,
+or
+.Li 8 .
+.Pp
+.Fa plusy
+is the value
+.Li y
+in the alignment term
+.Dq Li xn + y .
+It must have a value between
+.Li 0
+and
+.Li 7 ,
+inclusive.
+.\"
+.Ss inet6_option_alloc
+This function appends a Hop-by-Hop option or a Destination option
+into an ancillary data object that has been initialized by
+.Fn inet6_option_init .
+This function returns a pointer to the 8-bit
+option type field that starts the option on success, or
+.Dv NULL
+on an error.
+.Pp
+The difference between this function and
+.Fn inet6_option_append
+is that the latter copies the contents of a previously built option into
+the ancillary data object while the current function returns a
+pointer to the space in the data object where the option's TLV must
+then be built by the caller.
+.Pp
+.Fa cmsg
+is a pointer to the
+.Li cmsghdr
+structure that must have been
+initialized by
+.Fn inet6_option_init .
+.Pp
+.Fa datalen
+is the value of the option data length byte for this option.
+This value is required as an argument to allow the function to
+determine if padding must be appended at the end of the option.
+.Po
+The
+.Fn inet6_option_append
+function does not need a data length argument
+since the option data length must already be stored by the caller.
+.Pc
+.Pp
+.Fa multx
+is the value
+.Li x
+in the alignment term
+.Dq Li xn + y .
+It must have a value of
+.Li 1 ,
+.Li 2 ,
+.Li 4 ,
+or
+.Li 8 .
+.Pp
+.Fa plusy
+is the value
+.Li y
+in the alignment term
+.Dq Li xn + y .
+It must have a value between
+.Li 0
+and
+.Li 7 ,
+inclusive.
+.\"
+.Ss inet6_option_next
+This function processes the next Hop-by-Hop option or Destination
+option in an ancillary data object.
+If another option remains to be
+processed, the return value of the function is
+.Li 0
+and
+.Fa *tptrp
+points to
+the 8-bit option type field
+.Po
+which is followed by the 8-bit option
+data length, followed by the option data
+.Pc .
+If no more options remain
+to be processed, the return value is
+.Li -1
+and
+.Fa *tptrp
+is
+.Dv NULL .
+If an error occurs, the return value is
+.Li -1
+and
+.Fa *tptrp
+is not
+.Dv NULL .
+.Pp
+.Fa cmsg
+is a pointer to
+.Li cmsghdr
+structure of which
+.Li cmsg_level
+equals
+.Dv IPPROTO_IPV6
+and
+.Li cmsg_type
+equals either
+.Dv IPV6_HOPOPTS
+or
+.Dv IPV6_DSTOPTS .
+.Pp
+.Fa tptrp
+is a pointer to a pointer to an 8-bit byte and
+.Fa *tptrp
+is used
+by the function to remember its place in the ancillary data object
+each time the function is called.
+The first time this function is
+called for a given ancillary data object,
+.Fa *tptrp
+must be set to
+.Dv NULL .
+.Pp 
+Each time this function returns success,
+.Fa *tptrp
+points to the 8-bit
+option type field for the next option to be processed.
+.\"
+.Ss inet6_option_find
+This function is similar to the previously described
+.Fn inet6_option_next
+function, except this function lets the caller
+specify the option type to be searched for, instead of always
+returning the next option in the ancillary data object.
+.Fa cmsg
+is a
+pointer to
+.Li cmsghdr
+structure of which
+.Li cmsg_level
+equals
+.Dv IPPROTO_IPV6
+and
+.Li cmsg_type
+equals either
+.Dv IPV6_HOPOPTS
+or
+.Dv IPV6_DSTOPTS .
+.Pp
+.Fa tptrp
+is a pointer to a pointer to an 8-bit byte and
+.Fa *tptrp
+is used
+by the function to remember its place in the ancillary data object
+each time the function is called.
+The first time this function is
+called for a given ancillary data object,
+.Fa *tptrp
+must be set to
+.Dv NULL .
+.Pa
+This function starts searching for an option of the specified type
+beginning after the value of
+.Fa *tptrp .
+If an option of the specified
+type is located, this function returns
+.Li 0
+and
+.Fa *tptrp
+points to the 8-
+bit option type field for the option of the specified type.
+If an
+option of the specified type is not located, the return value is
+.Li -1
+and
+.Fa *tptrp
+is
+.Dv NULL .
+If an error occurs, the return value is
+.Li -1
+and
+.Fa *tptrp
+is not
+.Dv NULL .
+.\"
+.Sh DIAGNOSTICS
+.Fn inet6_option_init
+and
+.Fn inet6_option_append
+return
+.Li 0
+on success or
+.Li -1
+on an error.
+.Pp
+.Fn inet6_option_alloc
+returns
+.Dv NULL on an error.
+.Pp
+On errors,
+.Fn inet6_option_next
+and
+.Fn inet6_option_find
+return
+.Li -1
+setting
+.Fa *tptrp
+to non 
+.Dv NULL
+value.
+.\"
+.Sh EXAMPLES
+RFC2292 gives comprehensive examples in chapter 6.
+.\"
+.Sh SEE ALSO
+.Rs
+.%A W. Stevens
+.%A M. Thomas
+.%T "Advanced Sockets API for IPv6"
+.%N RFC2292
+.%D February 1998
+.Re
+.Rs
+.%A S. Deering
+.%A R. Hinden
+.%T "Internet Protocol, Version 6 (IPv6) Specification"
+.%N RFC2460
+.%D December 1998
+.Re
+.\"
+.Sh HISTORY
+The implementation first appeared in KAME advanced networking kit.
+.\"
+.Sh STANDARDS
+The functions are
+are documented in
+.Dq Advanced Sockets API for IPv6
+.Pq RFC2292 .
+.\"
+.Sh BUGS
+The text was shamelessly copied from RFC2292.
diff --git a/src/lib/libc/net/inet6_rthdr_space.3 b/src/lib/libc/net/inet6_rthdr_space.3
new file mode 100644
index 0000000000..887d866106
--- /dev/null
+++ b/src/lib/libc/net/inet6_rthdr_space.3
@@ -0,0 +1,322 @@
+.\"	$OpenBSD: inet6_rthdr_space.3,v 1.1 1999/12/11 08:09:11 itojun Exp $
+.\"
+.\" Copyright (c) 1983, 1987, 1991, 1993
+.\"	The Regents of the University of California.  All rights reserved.
+.\"
+.\" Redistribution and use in source and binary forms, with or without
+.\" modification, are permitted provided that the following conditions
+.\" are met:
+.\" 1. Redistributions of source code must retain the above copyright
+.\"    notice, this list of conditions and the following disclaimer.
+.\" 2. Redistributions in binary form must reproduce the above copyright
+.\"    notice, this list of conditions and the following disclaimer in the
+.\"    documentation and/or other materials provided with the distribution.
+.\" 3. All advertising materials mentioning features or use of this software
+.\"    must display the following acknowledgement:
+.\"	This product includes software developed by the University of
+.\"	California, Berkeley and its contributors.
+.\" 4. Neither the name of the University nor the names of its contributors
+.\"    may be used to endorse or promote products derived from this software
+.\"    without specific prior written permission.
+.\"
+.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
+.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
+.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+.\" SUCH DAMAGE.
+.\"
+.\"     From: @(#)gethostbyname.3	8.4 (Berkeley) 5/25/95
+.\"     KAME Id: inet6_rthdr_space.3,v 1.1 1999/12/10 04:48:55 itojun Exp
+.\"
+.Dd December 10, 1999
+.Dt INET6_OPTION_SPACE 3
+.Os
+.\"
+.Sh NAME
+.Nm inet6_rthdr_space ,
+.Nm inet6_rthdr_init ,
+.Nm inet6_rthdr_add ,
+.Nm inet6_rthdr_lasthop ,
+.Nm inet6_rthdr_reverse ,
+.Nm inet6_rthdr_segments ,
+.Nm inet6_rthdr_getaddr ,
+.Nm inet6_rthdr_getflags
+.Nd IPv6 Routing Header Options manipulation
+.\"
+.Sh SYNOPSIS
+.Fd #include <netinet/in.h>
+.Ft size_t
+.Fn inet6_rthdr_space "int type" "int segments"
+.Ft "struct cmsghdr *"
+.Fn inet6_rthdr_init "void *bp" "int type"
+.Ft int
+.Fn inet6_rthdr_add "struct cmsghdr *cmsg" "const struct in6_addr *addr" "unsigned int flags"
+.Ft int
+.Fn inet6_rthdr_lasthop "struct cmsghdr *cmsg" "unsigned int flags"
+.Ft int
+.Fn inet6_rthdr_reverse "const struct cmsghdr *in" "struct cmsghdr *out"
+.Ft int
+.Fn inet6_rthdr_segments "const struct cmsghdr *cmsg"
+.Ft "struct in6_addr"
+.Fn inet6_rthdr_getaddr "struct cmsghdr *cmsg" "int index"
+.Ft int
+.Fn inet6_rthdr_getflags "const struct cmsghdr *cmsg" "int index"
+.\"
+.Sh DESCRIPTION
+RFC2292 IPv6 advanced API defines eight
+functions that the application calls to build and examine a Routing
+header.  Four functions build a Routing header:
+.Bl -hang
+.It Fn inet6_rthdr_space
+return #bytes required for ancillary data
+.It Fn inet6_rthdr_init
+initialize ancillary data for Routing header
+.It Fn inet6_rthdr_add
+add IPv6 address & flags to Routing header
+.It Fn inet6_rthdr_lasthop
+specify the flags for the final hop
+.El
+.Pp
+Four functions deal with a returned Routing header:
+.Bl -hang
+.It Fn inet6_rthdr_reverse
+reverse a Routing header
+.It Fn inet6_rthdr_segments
+return #segments in a Routing header
+.It Fn inet6_rthdr_getaddr
+fetch one address from a Routing header
+.It Fn inet6_rthdr_getflags
+fetch one flag from a Routing header
+.El
+.Pp
+The function prototypes for these functions are all in the
+.Aq Li netinet/in.h
+header.
+.\"
+.Ss inet6_rthdr_space
+This function returns the number of bytes required to hold a Routing
+header of the specified
+.Fa type
+containing the specified number of
+.Fa segments
+.Pq addresses .
+For an IPv6 Type 0 Routing header, the number
+of segments must be between 1 and 23, inclusive.  The return value
+includes the size of the cmsghdr structure that precedes the Routing
+header, and any required padding.
+.Pp
+If the return value is 0, then either the type of the Routing header
+is not supported by this implementation or the number of segments is
+invalid for this type of Routing header.
+.Pp
+Note: This function returns the size but does not allocate the space
+required for the ancillary data.
+This allows an application to
+allocate a larger buffer, if other ancillary data objects are
+desired, since all the ancillary data objects must be specified to
+.Xr sendmsg 2
+as a single
+.Li msg_control
+buffer.
+.\"
+.Ss inet6_rthdr_init
+This function initializes the buffer pointed to by
+.Fa bp
+to contain a
+.Li cmsghdr
+structure followed by a Routing header of the specified
+.Fa type .
+The
+.Li cmsg_len
+member of the
+.Li cmsghdr
+structure is initialized to the
+size of the structure plus the amount of space required by the
+Routing header.
+The
+.Li cmsg_level
+and
+.Li cmsg_type
+members are also initialized as required.
+.Pp
+The caller must allocate the buffer and its size can be determined by
+calling
+.Fn inet6_rthdr_space .
+.Pp
+Upon success the return value is the pointer to the
+.Li cmsghdr
+structure, and this is then used as the first argument to the next
+two functions.
+Upon an error the return value is
+.Dv NULL .
+.\"
+.Ss inet6_rthdr_add
+This function adds the address pointed to by
+.Fa addr
+to the end of the
+Routing header being constructed and sets the type of this hop to the
+value of
+.Fa flags .
+For an IPv6 Type 0 Routing header,
+.Fa flags
+must be
+either
+.Dv IPV6_RTHDR_LOOSE
+or
+.Dv IPV6_RTHDR_STRICT .
+.Pp
+If successful, the
+.Li cmsg_len
+member of the
+.Li cmsghdr
+structure is
+updated to account for the new address in the Routing header and the
+return value of the function is 0.
+Upon an error the return value of
+the function is -1.
+.\"
+.Ss inet6_rthdr_lasthop
+This function specifies the Strict/Loose flag for the final hop of a
+Routing header.
+For an IPv6 Type 0 Routing header,
+.Fa flags
+must be either
+.Dv IPV6_RTHDR_LOOSE
+or
+.Dv IPV6_RTHDR_STRICT .
+.Pp
+The return value of the function is 0 upon success, or -1 upon an error.
+.Pp
+Notice that a Routing header specifying
+.Li N
+intermediate nodes requires
+.Li N+1
+Strict/Loose flags.
+This requires
+.Li N
+calls to
+.Fn inet6_rthdr_add
+followed by one call to
+.Fn inet6_rthdr_lasthop .
+.\"
+.Ss inet6_rthdr_reverse
+This function takes a Routing header that was received as ancillary
+data
+.Po
+pointed to by the first argument,
+.Fa in
+.Pc
+and writes a new Routing
+header that sends datagrams along the reverse of that route.
+Both
+arguments are allowed to point to the same buffer
+.Pq that is, the reversal can occur in place .
+.Pp
+The return value of the function is 0 on success, or -1 upon an
+error.
+.\"
+.Ss inet6_rthdr_segments
+This function returns the number of segments
+.Pq addresses
+contained in
+the Routing header described by
+.Fa cmsg .
+On success the return value is
+between 1 and 23, inclusive.
+The return value of the function is -1 upon an error.
+.\"
+.Ss inet6_rthdr_getaddr
+This function returns a pointer to the IPv6 address specified by
+.Fa index
+.Po
+which must have a value between 1 and the value returned by
+.Fn inet6_rthdr_segments
+.Pc
+in the Routing header described by
+.Fa cmsg .
+An
+application should first call
+.Fn inet6_rthdr_segments
+to obtain the number of segments in the Routing header.
+.Pp
+Upon an error the return value of the function is
+.Dv NULL .
+.\"
+.Ss inet6_rthdr_getflags
+This function returns the flags value specified by
+.Fa index
+.Po
+which must
+have a value between 0 and the value returned by
+.Fn inet6_rthdr_segments
+.Pc
+in the Routing header described by
+.Fa cmsg .
+For an IPv6 Type 0 Routing header the return value will be either
+.Dv IPV6_RTHDR_LOOSE
+or
+.Dv IPV6_RTHDR_STRICT .
+.Pp
+Upon an error the return value of the function is -1.
+.Pp
+Note: Addresses are indexed starting at 1, and flags starting at 0,
+to maintain consistency with the terminology and figures in RFC2460.
+.\"
+.Sh DIAGNOSTICS
+.Fn inet6_rthdr_space
+returns 0 on errors.
+.Pp
+.Fn inet6_rthdr_add ,
+.Fn inet6_rthdr_lasthop
+and
+.Fn inet6_rthdr_reverse
+return 0 on success, and returns -1 on error.
+.Pp
+.Fn inet6_rthdr_init
+and
+.Fn inet6_rthdr_getaddr
+return
+.Dv NULL
+on error.
+.Pp
+.Fn inet6_rthdr_segments
+and
+.Fn inet6_rthdr_getflags
+return -1 on error.
+.\"
+.Sh EXAMPLES
+RFC2292 gives comprehensive examples in chapter 8.
+.\"
+.Sh SEE ALSO
+.Rs
+.%A W. Stevens
+.%A M. Thomas
+.%T "Advanced Sockets API for IPv6"
+.%N RFC2292
+.%D February 1998
+.Re
+.Rs
+.%A S. Deering
+.%A R. Hinden
+.%T "Internet Protocol, Version 6 (IPv6) Specification"
+.%N RFC2460
+.%D December 1998
+.Re
+.\"
+.Sh HISTORY
+The implementation first appeared in KAME advanced networking kit.
+.\"
+.Sh STANDARDS
+The functions are
+are documented in
+.Dq Advanced Sockets API for IPv6
+.Pq RFC2292 .
+.\"
+.Sh BUGS
+The text was shamelessly copied from RFC2292.
diff --git a/src/lib/libc/net/ip6opt.c b/src/lib/libc/net/ip6opt.c
new file mode 100644
index 0000000000..cbd49f0c22
--- /dev/null
+++ b/src/lib/libc/net/ip6opt.c
@@ -0,0 +1,388 @@
+/*	$OpenBSD: ip6opt.c,v 1.1 1999/12/11 08:09:11 itojun Exp $	*/
+
+/*
+ * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
+ * All rights reserved.
+ * 
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ * 3. Neither the name of the project nor the names of its contributors
+ *    may be used to endorse or promote products derived from this software
+ *    without specific prior written permission.
+ * 
+ * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#include <sys/param.h>
+#include <sys/types.h>
+#include <sys/socket.h>
+
+#include <netinet/in.h>
+#include <netinet/ip6.h>
+
+#include <string.h>
+#include <stdio.h>
+
+static int ip6optlen(u_int8_t *opt, u_int8_t *lim);
+static void inet6_insert_padopt(u_char *p, int len);
+
+/*
+ * This function returns the number of bytes required to hold an option
+ * when it is stored as ancillary data, including the cmsghdr structure
+ * at the beginning, and any padding at the end (to make its size a
+ * multiple of 8 bytes).  The argument is the size of the structure
+ * defining the option, which must include any pad bytes at the
+ * beginning (the value y in the alignment term "xn + y"), the type
+ * byte, the length byte, and the option data.
+ */
+int
+inet6_option_space(nbytes)
+	int nbytes;
+{
+	nbytes += 2;	/* we need space for nxt-hdr and length fields */
+	return(CMSG_SPACE((nbytes + 7) & ~7));
+}
+
+/*
+ * This function is called once per ancillary data object that will
+ * contain either Hop-by-Hop or Destination options.  It returns 0 on
+ * success or -1 on an error.
+ */
+int
+inet6_option_init(bp, cmsgp, type)
+	void *bp;
+	struct cmsghdr **cmsgp;
+	int type;
+{
+	register struct cmsghdr *ch = (struct cmsghdr *)bp;
+
+	/* argument validation */
+	if (type != IPV6_HOPOPTS && type != IPV6_DSTOPTS)
+		return(-1);
+	
+	ch->cmsg_level = IPPROTO_IPV6;
+	ch->cmsg_type = type;
+	ch->cmsg_len = CMSG_LEN(0);
+
+	*cmsgp = ch;
+	return(0);
+}
+
+/*
+ * This function appends a Hop-by-Hop option or a Destination option
+ * into an ancillary data object that has been initialized by
+ * inet6_option_init().  This function returns 0 if it succeeds or -1 on
+ * an error.
+ * multx is the value x in the alignment term "xn + y" described
+ * earlier.  It must have a value of 1, 2, 4, or 8.
+ * plusy is the value y in the alignment term "xn + y" described
+ * earlier.  It must have a value between 0 and 7, inclusive.
+ */
+int
+inet6_option_append(cmsg, typep, multx, plusy)
+	struct cmsghdr *cmsg;
+	const u_int8_t *typep;
+	int multx;
+	int plusy;
+{
+	int padlen, optlen, off;
+	register u_char *bp = (u_char *)cmsg + cmsg->cmsg_len;
+	struct ip6_ext *eh = (struct ip6_ext *)CMSG_DATA(cmsg);
+
+	/* argument validation */
+	if (multx != 1 && multx != 2 && multx != 4 && multx != 8)
+		return(-1);
+	if (plusy < 0 || plusy > 7)
+		return(-1);
+	if (typep[0] > 255)
+		return(-1);
+
+	/*
+	 * If this is the first option, allocate space for the
+	 * first 2 bytes(for next header and length fields) of
+	 * the option header.
+	 */
+	if (bp == (u_char *)eh) {
+		bp += 2;
+		cmsg->cmsg_len += 2;
+	}
+
+	/* calculate pad length before the option. */
+	off = bp - (u_char *)eh;
+	padlen = (((off % multx) + (multx - 1)) & ~(multx - 1)) -
+		(off % multx);
+	padlen += plusy;
+	padlen %= multx;	/* keep the pad as short as possible */
+	/* insert padding */
+	inet6_insert_padopt(bp, padlen);
+	cmsg->cmsg_len += padlen;
+	bp += padlen;
+
+	/* copy the option */
+	if (typep[0] == IP6OPT_PAD1)
+		optlen = 1;
+	else
+		optlen = typep[1] + 2;
+	memcpy(bp, typep, optlen);
+	bp += optlen;
+	cmsg->cmsg_len += optlen;
+
+	/* calculate pad length after the option and insert the padding */
+	off = bp - (u_char *)eh;
+	padlen = ((off + 7) & ~7) - off;
+	inet6_insert_padopt(bp, padlen);
+	bp += padlen;
+	cmsg->cmsg_len += padlen;
+
+	/* update the length field of the ip6 option header */
+	eh->ip6e_len = ((bp - (u_char *)eh) >> 3) - 1;
+
+	return(0);
+}
+
+/*
+ * This function appends a Hop-by-Hop option or a Destination option
+ * into an ancillary data object that has been initialized by
+ * inet6_option_init().  This function returns a pointer to the 8-bit
+ * option type field that starts the option on success, or NULL on an
+ * error.
+ * The difference between this function and inet6_option_append() is
+ * that the latter copies the contents of a previously built option into
+ * the ancillary data object while the current function returns a
+ * pointer to the space in the data object where the option's TLV must
+ * then be built by the caller.
+ * 
+ */
+u_int8_t *
+inet6_option_alloc(cmsg, datalen, multx, plusy)
+	struct cmsghdr *cmsg;
+	int datalen;
+	int multx;
+	int plusy;
+{
+	int padlen, off;
+	register u_int8_t *bp = (u_char *)cmsg + cmsg->cmsg_len;
+	u_int8_t *retval;
+	struct ip6_ext *eh = (struct ip6_ext *)CMSG_DATA(cmsg);
+
+	/* argument validation */
+	if (multx != 1 && multx != 2 && multx != 4 && multx != 8)
+		return(NULL);
+	if (plusy < 0 || plusy > 7)
+		return(NULL);
+
+	/*
+	 * If this is the first option, allocate space for the
+	 * first 2 bytes(for next header and length fields) of
+	 * the option header.
+	 */
+	if (bp == (u_char *)eh) {
+		bp += 2;
+		cmsg->cmsg_len += 2;
+	}
+
+	/* calculate pad length before the option. */
+	off = bp - (u_char *)eh;
+	padlen = (((off % multx) + (multx - 1)) & ~(multx - 1)) -
+		(off % multx);
+	padlen += plusy;
+	padlen %= multx;	/* keep the pad as short as possible */
+	/* insert padding */
+	inet6_insert_padopt(bp, padlen);
+	cmsg->cmsg_len += padlen;
+	bp += padlen;
+
+	/* keep space to store specified length of data */
+	retval = bp;
+	bp += datalen;
+	cmsg->cmsg_len += datalen;
+
+	/* calculate pad length after the option and insert the padding */
+	off = bp - (u_char *)eh;
+	padlen = ((off + 7) & ~7) - off;
+	inet6_insert_padopt(bp, padlen);
+	bp += padlen;
+	cmsg->cmsg_len += padlen;
+
+	/* update the length field of the ip6 option header */
+	eh->ip6e_len = ((bp - (u_char *)eh) >> 3) - 1;
+
+	return(retval);
+}
+
+/*
+ * This function processes the next Hop-by-Hop option or Destination
+ * option in an ancillary data object.  If another option remains to be
+ * processed, the return value of the function is 0 and *tptrp points to
+ * the 8-bit option type field (which is followed by the 8-bit option
+ * data length, followed by the option data).  If no more options remain
+ * to be processed, the return value is -1 and *tptrp is NULL.  If an
+ * error occurs, the return value is -1 and *tptrp is not NULL.
+ * (RFC 2292, 6.3.5)
+ */
+int
+inet6_option_next(cmsg, tptrp)
+	const struct cmsghdr *cmsg;
+	u_int8_t **tptrp;
+{
+	struct ip6_ext *ip6e;
+	int hdrlen, optlen;
+	u_int8_t *lim;
+
+	if (cmsg->cmsg_level != IPPROTO_IPV6 ||
+	    (cmsg->cmsg_type != IPV6_HOPOPTS &&
+	     cmsg->cmsg_type != IPV6_DSTOPTS))
+		return(-1);
+
+	/* message length validation */
+	if (cmsg->cmsg_len < CMSG_SPACE(sizeof(struct ip6_ext)))
+		return(-1);
+	ip6e = (struct ip6_ext *)CMSG_DATA(cmsg);
+	hdrlen = (ip6e->ip6e_len + 1) << 3;
+	if (cmsg->cmsg_len < CMSG_SPACE(hdrlen))
+		return(-1);
+
+	/*
+	 * If the caller does not specify the starting point,
+	 * simply return the 1st option.
+	 * Otherwise, search the option list for the next option.
+	 */
+	lim = (u_int8_t *)ip6e + hdrlen;
+	if (*tptrp == NULL)
+		*tptrp = (u_int8_t *)(ip6e + 1);
+	else {
+		if ((optlen = ip6optlen(*tptrp, lim)) == 0)
+			return(-1);
+
+		*tptrp = *tptrp + optlen;
+	}
+	if (*tptrp >= lim) {	/* there is no option */
+		*tptrp = NULL;
+		return(-1);
+	}
+	/*
+	 * Finally, checks if the next option is safely stored in the
+	 * cmsg data.
+	 */
+	if (ip6optlen(*tptrp, lim) == 0)
+		return(-1);
+	else
+		return(0);
+}
+
+/*
+ * This function is similar to the inet6_option_next() function,
+ * except this function lets the caller specify the option type to be
+ * searched for, instead of always returning the next option in the
+ * ancillary data object.
+ * Note: RFC 2292 says the type of tptrp is u_int8_t *, but we think
+ *       it's a typo. The variable should be type of u_int8_t **.
+ */
+int
+inet6_option_find(cmsg, tptrp, type)
+	const struct cmsghdr *cmsg;
+	u_int8_t **tptrp;
+	int type;
+{
+	struct ip6_ext *ip6e;
+	int hdrlen, optlen;
+	u_int8_t *optp, *lim;
+
+	if (cmsg->cmsg_level != IPPROTO_IPV6 ||
+	    (cmsg->cmsg_type != IPV6_HOPOPTS &&
+	     cmsg->cmsg_type != IPV6_DSTOPTS))
+		return(-1);
+
+	/* message length validation */
+	if (cmsg->cmsg_len < CMSG_SPACE(sizeof(struct ip6_ext)))
+		return(-1);
+	ip6e = (struct ip6_ext *)CMSG_DATA(cmsg);
+	hdrlen = (ip6e->ip6e_len + 1) << 3;
+	if (cmsg->cmsg_len < CMSG_SPACE(hdrlen))
+		return(-1);	
+
+	/*
+	 * If the caller does not specify the starting point,
+	 * search from the beginning of the option list.
+	 * Otherwise, search from *the next option* of the specified point.
+	 */
+	lim = (u_int8_t *)ip6e + hdrlen;
+	if (*tptrp == NULL)
+		*tptrp = (u_int8_t *)(ip6e + 1);
+	else {
+		if ((optlen = ip6optlen(*tptrp, lim)) == 0)
+			return(-1);
+
+		*tptrp = *tptrp + optlen;
+	}
+	for (optp = *tptrp; optp < lim; optp += optlen) {
+		if (*optp == type) {
+			*tptrp = optp;
+			return(0);
+		}
+		if ((optlen = ip6optlen(optp, lim)) == 0)
+			return(-1);
+	}
+
+	/* search failed */
+	*tptrp = NULL;
+	return(-1);
+}
+
+/*
+ * Calculate the length of a given IPv6 option. Also checks
+ * if the option is safely stored in user's buffer according to the
+ * calculated length and the limitation of the buffer.
+ */
+static int
+ip6optlen(opt, lim)
+	u_int8_t *opt, *lim;
+{
+	int optlen;
+
+	if (*opt == IP6OPT_PAD1)
+		optlen = 1;
+	else {
+		/* is there enough space to store type and len? */
+		if (opt + 2 > lim)
+			return(0);
+		optlen = *(opt + 1) + 2;
+	}
+	if (opt + optlen <= lim)
+		return(optlen);
+
+	return(0);
+}
+
+static void
+inet6_insert_padopt(u_char *p, int len)
+{
+	switch(len) {
+	 case 0:
+		 return;
+	 case 1:
+		 p[0] = IP6OPT_PAD1;
+		 return;
+	 default:
+		 p[0] = IP6OPT_PADN;
+		 p[1] = len - 2; 
+		 memset(&p[2], 0, len - 2);
+		 return;
+	}
+}
diff --git a/src/lib/libc/net/rthdr.c b/src/lib/libc/net/rthdr.c
new file mode 100644
index 0000000000..2dff59668e
--- /dev/null
+++ b/src/lib/libc/net/rthdr.c
@@ -0,0 +1,298 @@
+/*	$OpenBSD: rthdr.c,v 1.1 1999/12/11 08:09:11 itojun Exp $	*/
+
+/*
+ * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
+ * All rights reserved.
+ * 
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ * 3. Neither the name of the project nor the names of its contributors
+ *    may be used to endorse or promote products derived from this software
+ *    without specific prior written permission.
+ * 
+ * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#include <sys/param.h>
+#include <sys/types.h>
+#include <sys/socket.h>
+
+#include <netinet/in.h>
+#include <netinet/ip6.h>
+
+#include <string.h>
+#include <stdio.h>
+
+size_t
+inet6_rthdr_space(type, seg)
+    int type, seg;
+{
+    switch(type) {
+     case IPV6_RTHDR_TYPE_0:
+	 if (seg < 1 || seg > 23)
+	     return(0);
+	 return(CMSG_SPACE(sizeof(struct in6_addr) * (seg - 1)
+			   + sizeof(struct ip6_rthdr0)));
+     default:
+#ifdef DEBUG
+	 fprintf(stderr, "inet6_rthdr_space: unknown type(%d)\n", type);
+#endif 
+	 return(0);
+    }
+}
+
+struct cmsghdr *
+inet6_rthdr_init(bp, type)
+    void *bp;
+    int type;
+{
+    register struct cmsghdr *ch = (struct cmsghdr *)bp;
+    register struct ip6_rthdr *rthdr = (struct ip6_rthdr *)(ch + 1);
+
+    ch->cmsg_level = IPPROTO_IPV6;
+    ch->cmsg_type = IPV6_RTHDR;
+
+    switch(type) {
+     case IPV6_RTHDR_TYPE_0:
+	 ch->cmsg_len = CMSG_LEN(sizeof(struct ip6_rthdr0) - sizeof(struct in6_addr));
+	 bzero(rthdr, sizeof(struct ip6_rthdr0));
+	 rthdr->ip6r_type = IPV6_RTHDR_TYPE_0;
+	 return(ch);
+     default:
+#ifdef DEBUG
+	 fprintf(stderr, "inet6_rthdr_init: unknown type(%d)\n", type);
+#endif 
+	 return(NULL);
+    }
+}
+
+int
+inet6_rthdr_add(cmsg, addr, flags)
+    struct cmsghdr *cmsg;
+    const struct in6_addr *addr;
+    u_int flags;
+{
+    register struct ip6_rthdr *rthdr = (struct ip6_rthdr *)(cmsg + 1);
+
+    switch(rthdr->ip6r_type) {
+     case IPV6_RTHDR_TYPE_0:
+     {
+	 struct ip6_rthdr0 *rt0 = (struct ip6_rthdr0 *)rthdr;
+	 if (flags != IPV6_RTHDR_LOOSE && flags != IPV6_RTHDR_STRICT) {
+#ifdef DEBUG
+	     fprintf(stderr, "inet6_rthdr_add: unsupported flag(%d)\n", flags);
+#endif 
+	     return(-1);
+	 }
+	 if (rt0->ip6r0_segleft == 23) {
+#ifdef DEBUG
+	     fprintf(stderr, "inet6_rthdr_add: segment overflow\n");
+#endif 
+	     return(-1);
+	 }
+	 if (flags == IPV6_RTHDR_STRICT) {
+	     int c, b;
+	     c = rt0->ip6r0_segleft / 8;
+	     b = rt0->ip6r0_segleft % 8;
+	     rt0->ip6r0_slmap[c] |= (1 << (7 - b));
+	 }
+	 rt0->ip6r0_segleft++;
+	 bcopy(addr, (caddr_t)rt0 + ((rt0->ip6r0_len + 1) << 3),
+	       sizeof(struct in6_addr));
+	 rt0->ip6r0_len += sizeof(struct in6_addr) >> 3;
+	 cmsg->cmsg_len = CMSG_LEN((rt0->ip6r0_len + 1) << 3);
+	 break;
+     }
+     default:
+#ifdef DEBUG
+	 fprintf(stderr, "inet6_rthdr_add: unknown type(%d)\n",
+		 rthdr->ip6r_type);
+#endif 
+	 return(-1);
+    }
+
+    return(0);
+}
+
+int
+inet6_rthdr_lasthop(cmsg, flags)
+    struct cmsghdr *cmsg;
+    unsigned int flags;
+{
+    register struct ip6_rthdr *rthdr = (struct ip6_rthdr *)(cmsg + 1);
+
+    switch(rthdr->ip6r_type) {
+     case IPV6_RTHDR_TYPE_0:
+     {
+	 struct ip6_rthdr0 *rt0 = (struct ip6_rthdr0 *)rthdr;
+	 if (flags != IPV6_RTHDR_LOOSE && flags != IPV6_RTHDR_STRICT) {
+#ifdef DEBUG
+	     fprintf(stderr, "inet6_rthdr_lasthop: unsupported flag(%d)\n", flags);
+#endif 
+	     return(-1);
+	 }
+	 if (rt0->ip6r0_segleft > 23) {
+#ifdef DEBUG
+	     fprintf(stderr, "inet6_rthdr_add: segment overflow\n");
+#endif 
+	     return(-1);
+	 }
+	 if (flags == IPV6_RTHDR_STRICT) {
+	     int c, b;
+	     c = rt0->ip6r0_segleft / 8;
+	     b = rt0->ip6r0_segleft % 8;
+	     rt0->ip6r0_slmap[c] |= (1 << (7 - b));
+	 }
+	 break;
+     }
+     default:
+#ifdef DEBUG
+	 fprintf(stderr, "inet6_rthdr_lasthop: unknown type(%d)\n",
+		 rthdr->ip6r_type);
+#endif 
+	 return(-1);
+    }
+
+    return(0);
+}
+
+#if 0
+int
+inet6_rthdr_reverse(in, out)
+    const struct cmsghdr *in;
+    struct cmsghdr *out;
+{
+#ifdef DEBUG
+    fprintf(stderr, "inet6_rthdr_reverse: not implemented yet\n");
+#endif 
+    return -1;
+}
+#endif
+
+int
+inet6_rthdr_segments(cmsg)
+    const struct cmsghdr *cmsg;
+{
+    register struct ip6_rthdr *rthdr = (struct ip6_rthdr *)(cmsg + 1);
+
+    switch(rthdr->ip6r_type) {
+    case IPV6_RTHDR_TYPE_0:
+      {
+	struct ip6_rthdr0 *rt0 = (struct ip6_rthdr0 *)rthdr;
+
+	if (rt0->ip6r0_len % 2 || 46 < rt0->ip6r0_len) {
+#ifdef DEBUG
+	    fprintf(stderr, "inet6_rthdr_segments: invalid size(%d)\n",
+		rt0->ip6r0_len);
+#endif 
+	    return -1;
+	}
+
+	return (rt0->ip6r0_len * 8) / sizeof(struct in6_addr);
+      }
+
+    default:
+#ifdef DEBUG
+	fprintf(stderr, "inet6_rthdr_segments: unknown type(%d)\n",
+	    rthdr->ip6r_type);
+#endif 
+	return -1;
+    }
+}
+
+struct in6_addr *
+inet6_rthdr_getaddr(cmsg, index)
+    struct cmsghdr *cmsg;
+    int index;
+{
+    register struct ip6_rthdr *rthdr = (struct ip6_rthdr *)(cmsg + 1);
+
+    switch(rthdr->ip6r_type) {
+    case IPV6_RTHDR_TYPE_0:
+      {
+	struct ip6_rthdr0 *rt0 = (struct ip6_rthdr0 *)rthdr;
+	int naddr;
+
+	if (rt0->ip6r0_len % 2 || 46 < rt0->ip6r0_len) {
+#ifdef DEBUG
+	    fprintf(stderr, "inet6_rthdr_getaddr: invalid size(%d)\n",
+		rt0->ip6r0_len);
+#endif 
+	    return NULL;
+	}
+	naddr = (rt0->ip6r0_len * 8) / sizeof(struct in6_addr);
+	if (index <= 0 || naddr < index) {
+#ifdef DEBUG
+	    fprintf(stderr, "inet6_rthdr_getaddr: invalid index(%d)\n", index);
+#endif 
+	    return NULL;
+	}
+	return &rt0->ip6r0_addr[index - 1];
+      }
+
+    default:
+#ifdef DEBUG
+	fprintf(stderr, "inet6_rthdr_getaddr: unknown type(%d)\n",
+	    rthdr->ip6r_type);
+#endif 
+	return NULL;
+    }
+}
+
+int
+inet6_rthdr_getflags(cmsg, index)
+    const struct cmsghdr *cmsg;
+    int index;
+{
+    register struct ip6_rthdr *rthdr = (struct ip6_rthdr *)(cmsg + 1);
+
+    switch(rthdr->ip6r_type) {
+    case IPV6_RTHDR_TYPE_0:
+      {
+	struct ip6_rthdr0 *rt0 = (struct ip6_rthdr0 *)rthdr;
+	int naddr;
+
+	if (rt0->ip6r0_len % 2 || 46 < rt0->ip6r0_len) {
+#ifdef DEBUG
+	    fprintf(stderr, "inet6_rthdr_getflags: invalid size(%d)\n",
+		rt0->ip6r0_len);
+#endif 
+	    return -1;
+	}
+	naddr = (rt0->ip6r0_len * 8) / sizeof(struct in6_addr);
+	if (index < 0 || naddr < index) {
+#ifdef DEBUG
+	    fprintf(stderr, "inet6_rthdr_getflags: invalid index(%d)\n", index);
+#endif 
+	    return -1;
+	}
+	if (rt0->ip6r0_slmap[index / 8] & (0x80 >> (index % 8)))
+	    return IPV6_RTHDR_STRICT;
+	else
+	    return IPV6_RTHDR_LOOSE;
+      }
+
+    default:
+#ifdef DEBUG
+	fprintf(stderr, "inet6_rthdr_getflags: unknown type(%d)\n",
+	    rthdr->ip6r_type);
+#endif 
+	return -1;
+    }
+}
diff --git a/src/lib/libc/net/vars6.c b/src/lib/libc/net/vars6.c
new file mode 100644
index 0000000000..6d0f069ef6
--- /dev/null
+++ b/src/lib/libc/net/vars6.c
@@ -0,0 +1,42 @@
+/*	$OpenBSD: vars6.c,v 1.1 1999/12/11 08:09:11 itojun Exp $	*/
+
+/*
+ * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
+ * All rights reserved.
+ * 
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ * 3. Neither the name of the project nor the names of its contributors
+ *    may be used to endorse or promote products derived from this software
+ *    without specific prior written permission.
+ * 
+ * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#include <sys/types.h>
+#include <netinet/in.h>
+
+/*
+ * Definitions of some costant IPv6 addresses.
+ */
+const struct in6_addr in6addr_any = IN6ADDR_ANY_INIT;
+const struct in6_addr in6addr_loopback = IN6ADDR_LOOPBACK_INIT;
+const struct in6_addr in6addr_nodelocal_allnodes = IN6ADDR_NODELOCAL_ALLNODES_INIT;
+const struct in6_addr in6addr_linklocal_allnodes = IN6ADDR_LINKLOCAL_ALLNODES_INIT;
+
-- 
cgit v1.2.3-55-g6feb