diff options
author | shin <shin@FreeBSD.org> | 1999-12-16 18:32:01 +0000 |
---|---|---|
committer | shin <shin@FreeBSD.org> | 1999-12-16 18:32:01 +0000 |
commit | 985fda16e7b9a2ea19c0ce2acad334d015c8017f (patch) | |
tree | b11f1d7e07abdcc92794d21279c092496ec5f413 /lib | |
parent | 6db98885a563ab8ab2853b6991b22cdb71acb7e3 (diff) | |
download | FreeBSD-src-985fda16e7b9a2ea19c0ce2acad334d015c8017f.zip FreeBSD-src-985fda16e7b9a2ea19c0ce2acad334d015c8017f.tar.gz |
KAME 4th patch
IPv6 specific library functions addition.
(getnameinfo(), getaddrinfo(), and IPv6 transport support is not yet)
Reviewed by: freebsd-arch, cvs-committers
Obtained from: KAME project
Diffstat (limited to 'lib')
-rw-r--r-- | lib/libc/net/Makefile.inc | 12 | ||||
-rw-r--r-- | lib/libc/net/if_indextoname.3 | 142 | ||||
-rw-r--r-- | lib/libc/net/ifname.c | 212 | ||||
-rw-r--r-- | lib/libc/net/ip6opt.c | 386 | ||||
-rw-r--r-- | lib/libc/net/rthdr.c | 298 | ||||
-rw-r--r-- | lib/libc/net/vars.c | 42 |
6 files changed, 1088 insertions, 4 deletions
diff --git a/lib/libc/net/Makefile.inc b/lib/libc/net/Makefile.inc index 8b4db7c..5b7dc85 100644 --- a/lib/libc/net/Makefile.inc +++ b/lib/libc/net/Makefile.inc @@ -8,14 +8,16 @@ SRCS+= addr2ascii.c ascii2addr.c base64.c ether_addr.c \ gethostbydns.c gethostbyht.c gethostbynis.c gethostnamadr.c \ getnetbydns.c getnetbyht.c getnetbynis.c getnetnamadr.c \ getproto.c getprotoent.c getprotoname.c getservbyname.c \ - getservbyport.c getservent.c herror.c inet_addr.c inet_lnaof.c \ + getservbyport.c getservent.c herror.c inet_addr.c ifname.c \ + inet_lnaof.c \ inet_makeaddr.c inet_net_ntop.c inet_net_pton.c inet_neta.c \ inet_netof.c inet_network.c inet_ntoa.c inet_ntop.c \ - inet_pton.c linkaddr.c map_v4v6.c ns_addr.c ns_name.c ns_netint.c \ + inet_pton.c ip6opt.c linkaddr.c map_v4v6.c ns_addr.c \ + ns_name.c ns_netint.c \ ns_ntoa.c ns_parse.c ns_print.c ns_ttl.c nsap_addr.c \ rcmd.c recv.c res_comp.c res_data.c res_debug.c \ res_init.c res_mkquery.c res_mkupdate.c res_query.c res_send.c \ - res_update.c send.c + res_update.c rthdr.c send.c vars.c # not supported: iso_addr.c # machine-dependent net sources @@ -23,7 +25,7 @@ SRCS+= addr2ascii.c ascii2addr.c base64.c ether_addr.c \ .if ${LIB} == "c" MAN3+= addr2ascii.3 byteorder.3 ethers.3 gethostbyname.3 \ - getnetent.3 getprotoent.3 getservent.3 \ + getnetent.3 getprotoent.3 getservent.3 if_indextoname.3 \ inet.3 linkaddr.3 rcmd.3 resolver.3 # not installed: iso_addr.3 ns.3 @@ -46,6 +48,8 @@ MLINKS+=inet.3 addr.3 inet.3 inet_addr.3 inet.3 inet_aton.3 \ inet.3 inet_lnaof.3 inet.3 inet_makeaddr.3 inet.3 inet_netof.3 \ inet.3 inet_network.3 inet.3 inet_ntoa.3 inet.3 network.3 \ inet.3 ntoa.3 +MLINKS+=if_indextoname.3 if_nametoindex.3 if_indextoname.3 if_nameindex.3 \ + if_indextoname.3 if_freenameindex.3 MLINKS+=linkaddr.3 link_addr.3 linkaddr.3 link_ntoa.3 #MLINKS+=ns.3 ns_addr.3 ns.3 ns_ntoa.3 MLINKS+=rcmd.3 iruserok.3 rcmd.3 rresvport.3 rcmd.3 ruserok.3 diff --git a/lib/libc/net/if_indextoname.3 b/lib/libc/net/if_indextoname.3 new file mode 100644 index 0000000..186ddba --- /dev/null +++ b/lib/libc/net/if_indextoname.3 @@ -0,0 +1,142 @@ +.\" Copyright (c) 1983, 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: @(#)rcmd.3 8.1 (Berkeley) 6/4/93 +.\" $FreeBSD$ +.\" +.Dd May 21, 1998 +.Dt IF_NAMETOINDEX 3 +.Os KAME +.Sh NAME +.Nm if_nametoindex , +.Nm if_indextoname , +.Nm if_nameindex , +.Nm if_freenameindex +.Nd convert interface index to name, and vice versa +.Sh SYNOPSIS +.Fd #include <net/if.h> +.Ft "unsigned int" +.Fn if_nametoindex "const char *ifname" +.Ft "char *" +.Fn if_indextoname "unsigned int ifindex" "char *ifname" +.Ft "struct if_nameindex *" +.Fn if_nameindex "void" +.Ft "void" +.Fn if_freenameindex "struct if_nameindex *ptr" +.Sh DESCRIPTION +The functions map interface index to readable interface name +.Po +such as +.Li ``lo0'' +.Pc +, and vice versa. +.Pp +.Fn if_nametoindex +converts readable interface name to interface index +.Pp positive integer value . +If the specified interface does not exist, 0 will be returned. +.Pp +.Fn if_indextoname +converts interface index to readable interface name. +The +.Fa ifname +argument must point to a buffer of at least +.Dv IF_NAMESIZE +bytes into which the interface name corresponding to the specified index is +returned. +.Po +.Dv IF_NAMESIZE +is also defined in +.Li <net/if.h> +and its value includes a terminating null byte at the end of the +interface name. +.Pc +This pointer is also the return value of the function. +If there is no interface corresponding to the specified index, +.Dv NULL +is returned. +.Pp +.Fn if_nameindex +returns an array of +.Fa if_nameindex +structures. +.Fa if_nametoindex +is also defined in +.Li <net/if.h> , +and is as follows: +.Bd -literal -offset +struct if_nameindex { + unsigned int if_index; /* 1, 2, ... */ + char *if_name; /* null terminated name: "le0", ... */ +}; +.Ed +.Pp +The end of the array of structures is indicated by a structure with +an +.Fa if_index +of 0 and an +.Fa if_name +of +.Dv NULL . +The function returns a +.Dv NULL +pointer upon an error. +The memory used for this array of structures along with the interface +names pointed to by the +.Fa if_name +members is obtained dynamically. +This memory is freed by the +.Fn if_freenameindex +function. +.Pp +.Fn if_freenameindex +takes a pointer that was returned by +.Fn if_nameindex +as argument +.Pq Fa ptr , +and it reclaims the region allocated. +.Sh DIAGNOSTICS +.Fn if_nametoindex +returns 0 on error, positive integer on success. +.Fn if_indextoname +and +.Fn if_nameindex +return +.Dv NULL +on errors. +.Sh SEE ALSO +R. Gilligan, S. Thomson, J. Bound, and W. Stevens, +``Basic Socket Interface Extensions for IPv6,'' RFC2553, March 1999. +.Sh HISTORY +The implementation first appeared in WIDE Hydrangea IPv6 protocol stack kit. +.Sh STANDARDS +These functions are defined in ``Basic Socket Interface Extensions for IPv6'' +.Pq RFC2533 . diff --git a/lib/libc/net/ifname.c b/lib/libc/net/ifname.c new file mode 100644 index 0000000..3d3916a --- /dev/null +++ b/lib/libc/net/ifname.c @@ -0,0 +1,212 @@ +/* + * 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. + * + * $FreeBSD$ + */ +/* + * TODO: + * - prototype defs into arpa/inet.h, not net/if.h (bsd-api-new-02) + */ + +#include <sys/param.h> +#include <sys/socket.h> +#include <sys/sysctl.h> +#include <net/if.h> +#include <net/route.h> +#include <net/if_dl.h> + +#include <errno.h> +#include <stdlib.h> +#include <string.h> + +#define ROUNDUP(a) \ + ((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long)) +#define ADVANCE(x, n) (x += ROUNDUP((n)->sa_len)) + +unsigned int +if_nametoindex(ifname) + const char *ifname; +{ + struct if_nameindex *iff = if_nameindex(), *ifx; + int ret; + + if (iff == NULL) return 0; + ifx = iff; + while (ifx->if_name != NULL) { + if (strcmp(ifx->if_name, ifname) == 0) { + ret = ifx->if_index; + if_freenameindex(iff); + return ret; + } + ifx++; + } + if_freenameindex(iff); + errno = ENXIO; + return 0; +} + +char * +if_indextoname(ifindex, ifname) + unsigned int ifindex; + char *ifname; /* at least IF_NAMESIZE */ +{ + struct if_nameindex *iff = if_nameindex(), *ifx; + char *cp, *dp; + + if (iff == NULL) return NULL; + ifx = iff; + while (ifx->if_index != 0) { + if (ifx->if_index == ifindex) { + cp = ifname; + dp = ifx->if_name; + while ((*cp++ = *dp++)) ; + if_freenameindex(iff); + return (ifname); + } + ifx++; + } + if_freenameindex(iff); + errno = ENXIO; + return NULL; +} + +struct if_nameindex * +if_nameindex() +{ + size_t needed; + int mib[6], i, ifn = 0, off = 0, hlen; + char *buf = NULL, *lim, *next, *cp, *ifbuf = NULL; + struct rt_msghdr *rtm; + struct if_msghdr *ifm; + struct sockaddr *sa; + struct sockaddr_dl *sdl; + struct if_nameindex *ret = NULL; + static int ifxs = 64; /* initial upper limit */ + struct _ifx { + int if_index; + int if_off; + } *ifx = NULL; + + mib[0] = CTL_NET; + mib[1] = PF_ROUTE; + mib[2] = 0; /* protocol */ + mib[3] = 0; /* wildcard address family */ + mib[4] = NET_RT_IFLIST; + mib[5] = 0; /* no flags */ + if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0) + return NULL; + if ((buf = malloc(needed)) == NULL) { + errno = ENOMEM; + goto end; + } + /* XXX: we may have allocated too much than necessary */ + if ((ifbuf = malloc(needed)) == NULL) { + errno = ENOMEM; + goto end; + } + if ((ifx = (struct _ifx *)malloc(sizeof(*ifx) * ifxs)) == NULL) { + errno = ENOMEM; + goto end; + } + if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0) { + /* sysctl has set errno */ + goto end; + } + lim = buf + needed; + for (next = buf; next < lim; next += rtm->rtm_msglen) { + rtm = (struct rt_msghdr *)next; + if (rtm->rtm_version != RTM_VERSION) { + errno = EPROTONOSUPPORT; + goto end; + } + switch (rtm->rtm_type) { + case RTM_IFINFO: + ifm = (struct if_msghdr *)rtm; + ifx[ifn].if_index = ifm->ifm_index; + ifx[ifn].if_off = off; + cp = (char *)(ifm + 1); + for (i = 1; i; i <<= 1) { + if (i & ifm->ifm_addrs) { + sa = (struct sockaddr *)cp; + if (i == RTA_IFP && + sa->sa_family == AF_LINK) { + sdl = (struct sockaddr_dl *)sa; + memcpy(ifbuf + off, + sdl->sdl_data, + sdl->sdl_nlen); + off += sdl->sdl_nlen; + *(ifbuf + off) = '\0'; + off++; + } + ADVANCE(cp, sa); + } + } + if (++ifn == ifxs) { + /* we need more memory */ + struct _ifx *newifx; + + ifxs *= 2; + if ((newifx = (struct _ifx *)malloc(sizeof(*newifx) * ifxs)) == NULL) { + errno = ENOMEM; + goto end; + } + + /* copy and free old data */ + memcpy(newifx, ifx, (sizeof(*ifx) * ifxs) / 2); + free(ifx); + ifx = newifx; + } + } + } + hlen = sizeof(struct if_nameindex) * (ifn + 1); + if ((cp = (char *)malloc(hlen + off)) == NULL) { + errno = ENOMEM; + goto end; + } + bcopy(ifbuf, cp + hlen, off); + ret = (struct if_nameindex *)cp; + for (i = 0; i < ifn; i++) { + ret[i].if_index = ifx[i].if_index; + ret[i].if_name = cp + hlen + ifx[i].if_off; + } + ret[ifn].if_index = 0; + ret[ifn].if_name = NULL; + + end: + if (buf) free(buf); + if (ifbuf) free(ifbuf); + if (ifx) free(ifx); + + return ret; +} + +void if_freenameindex(ptr) + struct if_nameindex *ptr; +{ + free(ptr); +} diff --git a/lib/libc/net/ip6opt.c b/lib/libc/net/ip6opt.c new file mode 100644 index 0000000..f9291b9 --- /dev/null +++ b/lib/libc/net/ip6opt.c @@ -0,0 +1,386 @@ +/* + * 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. + * + * $FreeBSD$ + */ + +#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; + /* 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; + /* 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/lib/libc/net/rthdr.c b/lib/libc/net/rthdr.c new file mode 100644 index 0000000..ce2a33c --- /dev/null +++ b/lib/libc/net/rthdr.c @@ -0,0 +1,298 @@ +/* + * 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. + * + * $FreeBSD$ + */ + +#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/lib/libc/net/vars.c b/lib/libc/net/vars.c new file mode 100644 index 0000000..bf6b57a --- /dev/null +++ b/lib/libc/net/vars.c @@ -0,0 +1,42 @@ +/* + * 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. + * + * $FreeBSD$ + */ + +#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; + |