Home | History | Annotate | Line # | Download | only in netinet6
in6_pcb.c revision 1.10
      1  1.10   itojun /*	$NetBSD: in6_pcb.c,v 1.10 1999/12/13 15:17:22 itojun Exp $	*/
      2   1.5  thorpej 
      3   1.2   itojun /*
      4   1.2   itojun  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
      5   1.2   itojun  * All rights reserved.
      6   1.2   itojun  *
      7   1.2   itojun  * Redistribution and use in source and binary forms, with or without
      8   1.2   itojun  * modification, are permitted provided that the following conditions
      9   1.2   itojun  * are met:
     10   1.2   itojun  * 1. Redistributions of source code must retain the above copyright
     11   1.2   itojun  *    notice, this list of conditions and the following disclaimer.
     12   1.2   itojun  * 2. Redistributions in binary form must reproduce the above copyright
     13   1.2   itojun  *    notice, this list of conditions and the following disclaimer in the
     14   1.2   itojun  *    documentation and/or other materials provided with the distribution.
     15   1.2   itojun  * 3. Neither the name of the project nor the names of its contributors
     16   1.2   itojun  *    may be used to endorse or promote products derived from this software
     17   1.2   itojun  *    without specific prior written permission.
     18   1.2   itojun  *
     19   1.2   itojun  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
     20   1.2   itojun  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21   1.2   itojun  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22   1.2   itojun  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
     23   1.2   itojun  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24   1.2   itojun  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25   1.2   itojun  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26   1.2   itojun  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27   1.2   itojun  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28   1.2   itojun  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29   1.2   itojun  * SUCH DAMAGE.
     30   1.2   itojun  */
     31   1.2   itojun 
     32   1.2   itojun /*
     33   1.2   itojun  * Copyright (c) 1982, 1986, 1991, 1993
     34   1.2   itojun  *	The Regents of the University of California.  All rights reserved.
     35   1.2   itojun  *
     36   1.2   itojun  * Redistribution and use in source and binary forms, with or without
     37   1.2   itojun  * modification, are permitted provided that the following conditions
     38   1.2   itojun  * are met:
     39   1.2   itojun  * 1. Redistributions of source code must retain the above copyright
     40   1.2   itojun  *    notice, this list of conditions and the following disclaimer.
     41   1.2   itojun  * 2. Redistributions in binary form must reproduce the above copyright
     42   1.2   itojun  *    notice, this list of conditions and the following disclaimer in the
     43   1.2   itojun  *    documentation and/or other materials provided with the distribution.
     44   1.2   itojun  * 3. All advertising materials mentioning features or use of this software
     45   1.2   itojun  *    must display the following acknowledgement:
     46   1.2   itojun  *	This product includes software developed by the University of
     47   1.2   itojun  *	California, Berkeley and its contributors.
     48   1.2   itojun  * 4. Neither the name of the University nor the names of its contributors
     49   1.2   itojun  *    may be used to endorse or promote products derived from this software
     50   1.2   itojun  *    without specific prior written permission.
     51   1.2   itojun  *
     52   1.2   itojun  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     53   1.2   itojun  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     54   1.2   itojun  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     55   1.2   itojun  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     56   1.2   itojun  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     57   1.2   itojun  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     58   1.2   itojun  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     59   1.2   itojun  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     60   1.2   itojun  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     61   1.2   itojun  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     62   1.2   itojun  * SUCH DAMAGE.
     63   1.2   itojun  *
     64   1.2   itojun  *	@(#)in_pcb.c	8.2 (Berkeley) 1/4/94
     65   1.2   itojun  */
     66   1.7  thorpej 
     67   1.9   itojun #ifdef __NetBSD__	/*XXX*/
     68   1.7  thorpej #include "opt_ipsec.h"
     69   1.9   itojun #endif
     70   1.2   itojun 
     71   1.2   itojun #include <sys/param.h>
     72   1.2   itojun #include <sys/systm.h>
     73   1.2   itojun #include <sys/malloc.h>
     74   1.2   itojun #include <sys/mbuf.h>
     75   1.2   itojun #include <sys/protosw.h>
     76   1.2   itojun #include <sys/socket.h>
     77   1.2   itojun #include <sys/socketvar.h>
     78   1.2   itojun #include <sys/ioctl.h>
     79   1.2   itojun #include <sys/errno.h>
     80   1.2   itojun #include <sys/time.h>
     81   1.2   itojun #include <sys/proc.h>
     82   1.2   itojun 
     83   1.2   itojun #include <net/if.h>
     84   1.2   itojun #include <net/route.h>
     85   1.2   itojun 
     86   1.2   itojun #include <netinet/in.h>
     87   1.2   itojun #include <netinet/in_var.h>
     88   1.2   itojun #include <netinet/in_systm.h>
     89   1.2   itojun #include <netinet/ip.h>
     90   1.2   itojun #include <netinet/in_pcb.h>
     91   1.2   itojun #include <netinet6/ip6.h>
     92   1.2   itojun #include <netinet6/in6_pcb.h>
     93   1.2   itojun #include <netinet6/ip6_var.h>
     94   1.2   itojun #include <netinet6/nd6.h>
     95   1.2   itojun 
     96  1.10   itojun #ifndef __bsdi__
     97   1.2   itojun #include "loop.h"
     98  1.10   itojun #endif
     99   1.2   itojun #ifdef __NetBSD__
    100   1.2   itojun extern struct ifnet loif[NLOOP];
    101   1.2   itojun #endif
    102   1.8   itojun #include "faith.h"
    103   1.2   itojun 
    104   1.2   itojun #ifdef IPSEC
    105   1.2   itojun #include <netinet6/ipsec.h>
    106   1.2   itojun #include <netkey/key.h>
    107   1.2   itojun #include <netkey/key_debug.h>
    108   1.2   itojun #endif /* IPSEC */
    109   1.2   itojun 
    110   1.2   itojun struct in6_addr zeroin6_addr;
    111   1.2   itojun 
    112   1.2   itojun int
    113   1.2   itojun in6_pcballoc(so, head)
    114   1.2   itojun 	struct socket *so;
    115   1.2   itojun 	struct in6pcb *head;
    116   1.2   itojun {
    117   1.2   itojun 	struct in6pcb *in6p;
    118   1.2   itojun 
    119   1.2   itojun 	MALLOC(in6p, struct in6pcb *, sizeof(*in6p), M_PCB, M_NOWAIT);
    120   1.2   itojun 	if (in6p == NULL)
    121   1.2   itojun 		return(ENOBUFS);
    122   1.2   itojun 	bzero((caddr_t)in6p, sizeof(*in6p));
    123   1.2   itojun 	in6p->in6p_head = head;
    124   1.2   itojun 	in6p->in6p_socket = so;
    125   1.2   itojun 	in6p->in6p_hops = -1;	/* use kernel default */
    126   1.2   itojun 	in6p->in6p_icmp6filt = NULL;
    127   1.3   itojun #if 0
    128   1.2   itojun 	insque(in6p, head);
    129   1.3   itojun #else
    130   1.3   itojun 	in6p->in6p_next = head->in6p_next;
    131   1.3   itojun 	head->in6p_next = in6p;
    132   1.3   itojun 	in6p->in6p_prev = head;
    133   1.3   itojun 	in6p->in6p_next->in6p_prev = in6p;
    134   1.3   itojun #endif
    135   1.2   itojun 	so->so_pcb = (caddr_t)in6p;
    136   1.2   itojun 	return(0);
    137   1.2   itojun }
    138   1.2   itojun 
    139   1.2   itojun int
    140   1.2   itojun in6_pcbbind(in6p, nam)
    141   1.2   itojun 	register struct in6pcb *in6p;
    142   1.2   itojun 	struct mbuf *nam;
    143   1.2   itojun {
    144   1.2   itojun 	struct socket *so = in6p->in6p_socket;
    145   1.2   itojun 	struct in6pcb *head = in6p->in6p_head;
    146   1.2   itojun 	struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)NULL;
    147   1.2   itojun 	struct proc *p = curproc;		/* XXX */
    148   1.2   itojun 	u_short	lport = 0;
    149   1.2   itojun 	int wild = 0, reuseport = (so->so_options & SO_REUSEPORT);
    150   1.2   itojun 	int error;
    151   1.2   itojun 
    152  1.10   itojun 	if (in6p->in6p_lport || !IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr))
    153   1.2   itojun 		return(EINVAL);
    154   1.2   itojun 	if ((so->so_options & (SO_REUSEADDR|SO_REUSEPORT)) == 0 &&
    155   1.2   itojun 	   ((so->so_proto->pr_flags & PR_CONNREQUIRED) == 0 ||
    156   1.2   itojun 	    (so->so_options & SO_ACCEPTCONN) == 0))
    157   1.2   itojun 		wild = IN6PLOOKUP_WILDCARD;
    158   1.2   itojun 	if (nam) {
    159   1.2   itojun 		sin6 = mtod(nam, struct sockaddr_in6 *);
    160   1.2   itojun 		if (nam->m_len != sizeof(*sin6))
    161   1.2   itojun 			return(EINVAL);
    162   1.2   itojun 		/*
    163   1.2   itojun 		 * We should check the family, but old programs
    164   1.2   itojun 		 * incorrectly fail to intialize it.
    165   1.2   itojun 		 */
    166   1.2   itojun 		if (sin6->sin6_family != AF_INET6)
    167   1.2   itojun 			return(EAFNOSUPPORT);
    168   1.2   itojun 
    169   1.2   itojun 		/*
    170   1.2   itojun 		 * If the scope of the destination is link-local, embed the
    171   1.2   itojun 		 * interface index in the address.
    172   1.2   itojun 		 */
    173   1.2   itojun 		if (IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr)) {
    174   1.2   itojun 			/* XXX boundary check is assumed to be already done. */
    175   1.2   itojun 			/* XXX sin6_scope_id is weaker than advanced-api. */
    176   1.2   itojun 			struct in6_pktinfo *pi;
    177   1.2   itojun 			if (in6p->in6p_outputopts &&
    178   1.2   itojun 			    (pi = in6p->in6p_outputopts->ip6po_pktinfo) &&
    179   1.2   itojun 			    pi->ipi6_ifindex) {
    180   1.2   itojun 				sin6->sin6_addr.s6_addr16[1]
    181   1.2   itojun 					= htons(pi->ipi6_ifindex);
    182   1.2   itojun 			} else if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)
    183   1.2   itojun 				&& in6p->in6p_moptions
    184   1.2   itojun 				&& in6p->in6p_moptions->im6o_multicast_ifp) {
    185   1.2   itojun 				sin6->sin6_addr.s6_addr16[1] =
    186   1.2   itojun 					htons(in6p->in6p_moptions->im6o_multicast_ifp->if_index);
    187   1.2   itojun 			} else if (sin6->sin6_scope_id) {
    188   1.2   itojun 				/* boundary check */
    189   1.2   itojun 				if (sin6->sin6_scope_id < 0
    190   1.2   itojun 				 || if_index < sin6->sin6_scope_id) {
    191   1.2   itojun 					return ENXIO;  /* XXX EINVAL? */
    192   1.2   itojun 				}
    193   1.2   itojun 				sin6->sin6_addr.s6_addr16[1]
    194   1.2   itojun 					= htons(sin6->sin6_scope_id & 0xffff);/*XXX*/
    195   1.2   itojun 				/* this must be cleared for ifa_ifwithaddr() */
    196   1.2   itojun 				sin6->sin6_scope_id = 0;
    197   1.2   itojun 			}
    198   1.2   itojun 		}
    199   1.2   itojun 
    200   1.2   itojun 		lport = sin6->sin6_port;
    201   1.2   itojun 		if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) {
    202   1.2   itojun 			/*
    203   1.2   itojun 			 * Treat SO_REUSEADDR as SO_REUSEPORT for multicast;
    204   1.2   itojun 			 * allow compepte duplication of binding if
    205   1.2   itojun 			 * SO_REUSEPORT is set, or if SO_REUSEADDR is set
    206   1.2   itojun 			 * and a multicast address is bound on both
    207   1.2   itojun 			 * new and duplicated sockets.
    208   1.2   itojun 			 */
    209   1.2   itojun 			if (so->so_options & SO_REUSEADDR)
    210   1.2   itojun 				reuseport = SO_REUSEADDR|SO_REUSEPORT;
    211   1.2   itojun 		} else if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
    212   1.2   itojun 			struct sockaddr_in sin;
    213   1.2   itojun 
    214   1.2   itojun 			bzero(&sin, sizeof(sin));
    215   1.2   itojun 			sin.sin_len = sizeof(sin);
    216   1.2   itojun 			sin.sin_family = AF_INET;
    217   1.2   itojun 			bcopy(&sin6->sin6_addr.s6_addr32[3], &sin.sin_addr,
    218   1.2   itojun 				sizeof(sin.sin_addr));
    219   1.2   itojun 			if (ifa_ifwithaddr((struct sockaddr *)&sin) == 0)
    220   1.2   itojun 				return EADDRNOTAVAIL;
    221  1.10   itojun 		} else if (!IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
    222   1.2   itojun 			struct ifaddr *ia = NULL;
    223   1.2   itojun 
    224   1.2   itojun 			sin6->sin6_port = 0;		/* yech... */
    225  1.10   itojun #if defined(NFAITH) && NFAITH > 0
    226  1.10   itojun 			if ((in6p->in6p_flags & IN6P_FAITH) == 0
    227  1.10   itojun 			 && (ia = ifa_ifwithaddr((struct sockaddr *)sin6)) == 0)
    228  1.10   itojun #else
    229   1.2   itojun 			if ((ia = ifa_ifwithaddr((struct sockaddr *)sin6)) == 0)
    230  1.10   itojun #endif
    231   1.2   itojun 				return(EADDRNOTAVAIL);
    232   1.2   itojun 
    233   1.2   itojun 			/*
    234   1.2   itojun 			 * XXX: bind to an anycast address might accidentally
    235   1.2   itojun 			 * cause sending a packet with anycast source address.
    236   1.2   itojun 			 */
    237   1.2   itojun 			if (ia &&
    238   1.2   itojun 			    ((struct in6_ifaddr *)ia)->ia6_flags &
    239   1.2   itojun 			    (IN6_IFF_ANYCAST|IN6_IFF_NOTREADY|
    240   1.2   itojun 			     IN6_IFF_DETACHED|IN6_IFF_DEPRECATED)) {
    241   1.2   itojun 				return(EADDRNOTAVAIL);
    242   1.2   itojun 			}
    243   1.2   itojun 		}
    244   1.2   itojun 		if (lport) {
    245   1.2   itojun 			/* GROSS */
    246   1.2   itojun 			if (ntohs(lport) < IPV6PORT_RESERVED &&
    247   1.2   itojun 			   (error = suser(p->p_ucred, &p->p_acflag)))
    248   1.2   itojun 				return(EACCES);
    249   1.2   itojun 
    250   1.2   itojun 			if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
    251   1.2   itojun 				/* should check this but we can't ... */
    252   1.2   itojun #if 0
    253   1.2   itojun 				struct inpcb *t;
    254   1.2   itojun 
    255   1.2   itojun 				t = in_pcblookup_bind(&tcbtable,
    256   1.2   itojun 					(struct in_addr *)&sin6->sin6_addr.s6_addr32[3],
    257   1.2   itojun 					lport);
    258   1.2   itojun 				if (t && (reuseport & t->inp_socket->so_options) == 0)
    259   1.2   itojun 					return EADDRINUSE;
    260   1.2   itojun #endif
    261   1.2   itojun 			} else {
    262   1.2   itojun 				struct in6pcb *t;
    263   1.2   itojun 
    264   1.2   itojun 				t = in6_pcblookup(head, &zeroin6_addr, 0,
    265   1.2   itojun 						  &sin6->sin6_addr, lport, wild);
    266   1.2   itojun 				if (t && (reuseport & t->in6p_socket->so_options) == 0)
    267   1.2   itojun 					return(EADDRINUSE);
    268   1.2   itojun 			}
    269   1.2   itojun 		}
    270   1.2   itojun 		in6p->in6p_laddr = sin6->sin6_addr;
    271   1.2   itojun 	}
    272  1.10   itojun 
    273   1.2   itojun 	if (lport == 0) {
    274  1.10   itojun 		int e;
    275  1.10   itojun 		if ((e = in6_pcbsetport(&in6p->in6p_laddr, in6p)) != 0)
    276  1.10   itojun 			return(e);
    277  1.10   itojun 	}
    278  1.10   itojun 	else
    279  1.10   itojun 		in6p->in6p_lport = lport;
    280  1.10   itojun 
    281  1.10   itojun 	in6p->in6p_flowinfo = sin6 ? sin6->sin6_flowinfo : 0;	/*XXX*/
    282  1.10   itojun 	return(0);
    283  1.10   itojun }
    284  1.10   itojun 
    285  1.10   itojun /*
    286  1.10   itojun  * Find an empty port and set it to the specified PCB.
    287  1.10   itojun  * XXX IN6P_LOWPORT
    288  1.10   itojun  */
    289  1.10   itojun int
    290  1.10   itojun in6_pcbsetport(laddr, in6p)
    291  1.10   itojun 	struct in6_addr *laddr;
    292  1.10   itojun 	struct in6pcb *in6p;
    293  1.10   itojun {
    294  1.10   itojun 	struct socket *so = in6p->in6p_socket;
    295  1.10   itojun 	struct in6pcb *head = in6p->in6p_head;
    296  1.10   itojun 	u_short last_port, lport = 0;
    297  1.10   itojun 	int wild = 0;
    298  1.10   itojun 	void *t;
    299   1.2   itojun 
    300  1.10   itojun 	/* XXX: this is redundant when called from in6_pcbbind */
    301  1.10   itojun 	if ((so->so_options & (SO_REUSEADDR|SO_REUSEPORT)) == 0 &&
    302  1.10   itojun 	   ((so->so_proto->pr_flags & PR_CONNREQUIRED) == 0 ||
    303  1.10   itojun 	    (so->so_options & SO_ACCEPTCONN) == 0))
    304  1.10   itojun 		wild = IN6PLOOKUP_WILDCARD;
    305   1.2   itojun 
    306  1.10   itojun 	/* value out of range */
    307  1.10   itojun 	if (head->in6p_lport < IPV6PORT_ANONMIN)
    308  1.10   itojun 		head->in6p_lport = IPV6PORT_ANONMIN;
    309  1.10   itojun 	else if (head->in6p_lport > IPV6PORT_ANONMAX)
    310  1.10   itojun 		head->in6p_lport = IPV6PORT_ANONMIN;
    311  1.10   itojun 	last_port = head->in6p_lport;
    312  1.10   itojun 	goto startover;	/*to randomize*/
    313  1.10   itojun 	for (;;) {
    314  1.10   itojun 		lport = htons(head->in6p_lport);
    315  1.10   itojun 		if (IN6_IS_ADDR_V4MAPPED(laddr)) {
    316   1.2   itojun #if 0
    317  1.10   itojun 			t = in_pcblookup_bind(&tcbtable,
    318  1.10   itojun 					      (struct in_addr *)&in6p->in6p_laddr.s6_addr32[3],
    319  1.10   itojun 					      lport);
    320   1.2   itojun #else
    321  1.10   itojun 			t = NULL;
    322   1.2   itojun #endif
    323  1.10   itojun 		} else {
    324  1.10   itojun 			t = in6_pcblookup(head, &zeroin6_addr, 0, laddr,
    325  1.10   itojun 					  lport, wild);
    326   1.2   itojun 		}
    327  1.10   itojun 		if (t == 0)
    328  1.10   itojun 			break;
    329  1.10   itojun 	  startover:
    330  1.10   itojun 		if (head->in6p_lport >= IPV6PORT_ANONMAX)
    331  1.10   itojun 			head->in6p_lport = IPV6PORT_ANONMIN;
    332  1.10   itojun 		else
    333  1.10   itojun 			head->in6p_lport++;
    334  1.10   itojun 		if (head->in6p_lport == last_port)
    335  1.10   itojun 			return (EADDRINUSE);
    336   1.2   itojun 	}
    337  1.10   itojun 
    338   1.2   itojun 	in6p->in6p_lport = lport;
    339  1.10   itojun 	return(0);		/* success */
    340   1.2   itojun }
    341   1.2   itojun 
    342   1.2   itojun /*
    343   1.2   itojun  * Connect from a socket to a specified address.
    344   1.2   itojun  * Both address and port must be specified in argument sin6.
    345   1.2   itojun  * If don't have a local address for this socket yet,
    346   1.2   itojun  * then pick one.
    347   1.2   itojun  */
    348   1.2   itojun int
    349   1.2   itojun in6_pcbconnect(in6p, nam)
    350   1.2   itojun 	struct in6pcb *in6p;
    351   1.2   itojun 	struct mbuf *nam;
    352   1.2   itojun {
    353   1.2   itojun 	struct in6_addr *in6a = NULL;
    354   1.2   itojun 	struct sockaddr_in6 *sin6 = mtod(nam, struct sockaddr_in6 *);
    355   1.2   itojun 	struct in6_pktinfo *pi;
    356   1.2   itojun 	struct ifnet *ifp = NULL;	/* outgoing interface */
    357   1.2   itojun 	int error = 0;
    358   1.2   itojun 	struct in6_addr mapped;
    359   1.2   itojun 
    360   1.2   itojun 	(void)&in6a;				/* XXX fool gcc */
    361   1.2   itojun 
    362   1.2   itojun 	if (nam->m_len != sizeof(*sin6))
    363   1.2   itojun 		return(EINVAL);
    364   1.2   itojun 	if (sin6->sin6_family != AF_INET6)
    365   1.2   itojun 		return(EAFNOSUPPORT);
    366   1.2   itojun 	if (sin6->sin6_port == 0)
    367   1.2   itojun 		return(EADDRNOTAVAIL);
    368   1.2   itojun 
    369   1.2   itojun 	/* sanity check for mapped address case */
    370   1.2   itojun 	if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
    371   1.2   itojun 		if (IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr))
    372   1.2   itojun 			in6p->in6p_laddr.s6_addr16[5] = htons(0xffff);
    373   1.2   itojun 		if (!IN6_IS_ADDR_V4MAPPED(&in6p->in6p_laddr))
    374   1.2   itojun 			return EINVAL;
    375   1.2   itojun 	} else {
    376   1.2   itojun 		if (IN6_IS_ADDR_V4MAPPED(&in6p->in6p_laddr))
    377   1.2   itojun 			return EINVAL;
    378   1.2   itojun 	}
    379   1.2   itojun 
    380   1.2   itojun 	/*
    381   1.2   itojun 	 * If the scope of the destination is link-local, embed the interface
    382   1.2   itojun 	 * index in the address.
    383   1.2   itojun 	 */
    384   1.2   itojun 	if (IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr)) {
    385   1.2   itojun 		/* XXX boundary check is assumed to be already done. */
    386   1.2   itojun 		/* XXX sin6_scope_id is weaker than advanced-api. */
    387   1.2   itojun 		if (in6p->in6p_outputopts &&
    388   1.2   itojun 		    (pi = in6p->in6p_outputopts->ip6po_pktinfo) &&
    389   1.2   itojun 		    pi->ipi6_ifindex) {
    390   1.2   itojun 			sin6->sin6_addr.s6_addr16[1] = htons(pi->ipi6_ifindex);
    391   1.2   itojun 			ifp = ifindex2ifnet[pi->ipi6_ifindex];
    392   1.2   itojun 		}
    393   1.2   itojun 		else if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr) &&
    394   1.2   itojun 			 in6p->in6p_moptions &&
    395   1.2   itojun 			 in6p->in6p_moptions->im6o_multicast_ifp) {
    396   1.2   itojun 			sin6->sin6_addr.s6_addr16[1] =
    397   1.2   itojun 				htons(in6p->in6p_moptions->im6o_multicast_ifp->if_index);
    398   1.2   itojun 			ifp = ifindex2ifnet[in6p->in6p_moptions->im6o_multicast_ifp->if_index];
    399   1.2   itojun 		} else if (sin6->sin6_scope_id) {
    400   1.2   itojun 			/* boundary check */
    401   1.2   itojun 			if (sin6->sin6_scope_id < 0
    402   1.2   itojun 			 || if_index < sin6->sin6_scope_id) {
    403   1.2   itojun 				return ENXIO;  /* XXX EINVAL? */
    404   1.2   itojun 			}
    405   1.2   itojun 			sin6->sin6_addr.s6_addr16[1]
    406   1.2   itojun 				= htons(sin6->sin6_scope_id & 0xffff);/*XXX*/
    407   1.2   itojun 			ifp = ifindex2ifnet[sin6->sin6_scope_id];
    408   1.2   itojun 		}
    409   1.2   itojun 	}
    410   1.2   itojun 
    411   1.2   itojun 	/* Source address selection. */
    412   1.2   itojun 	if (IN6_IS_ADDR_V4MAPPED(&in6p->in6p_laddr)
    413   1.2   itojun 	 && in6p->in6p_laddr.s6_addr32[3] == 0) {
    414   1.2   itojun 		struct sockaddr_in sin, *sinp;
    415   1.2   itojun 
    416   1.2   itojun 		bzero(&sin, sizeof(sin));
    417   1.2   itojun 		sin.sin_len = sizeof(sin);
    418   1.2   itojun 		sin.sin_family = AF_INET;
    419   1.2   itojun 		bcopy(&sin6->sin6_addr.s6_addr32[3], &sin.sin_addr,
    420   1.2   itojun 			sizeof(sin.sin_addr));
    421   1.2   itojun 		sinp = in_selectsrc(&sin, (struct route *)&in6p->in6p_route,
    422   1.2   itojun 			in6p->in6p_socket->so_options, NULL, &error);
    423   1.2   itojun 		if (sinp == 0) {
    424   1.2   itojun 			if (error == 0)
    425   1.2   itojun 				error = EADDRNOTAVAIL;
    426   1.2   itojun 			return(error);
    427   1.2   itojun 		}
    428   1.2   itojun 		bzero(&mapped, sizeof(mapped));
    429   1.2   itojun 		mapped.s6_addr16[5] = htons(0xffff);
    430   1.2   itojun 		bcopy(&sinp->sin_addr, &mapped.s6_addr32[3], sizeof(sinp->sin_addr));
    431   1.2   itojun 		in6a = &mapped;
    432  1.10   itojun 	} else {
    433  1.10   itojun 		/*
    434  1.10   itojun 		 * XXX: in6_selectsrc might replace the bound local address
    435  1.10   itojun 		 * with the address specified by setsockopt(IPV6_PKTINFO).
    436  1.10   itojun 		 * Is it the intended behavior?
    437  1.10   itojun 		 */
    438   1.2   itojun 		in6a = in6_selectsrc(sin6, in6p->in6p_outputopts,
    439  1.10   itojun 				     in6p->in6p_moptions,
    440  1.10   itojun 				     &in6p->in6p_route,
    441  1.10   itojun 				     &in6p->in6p_laddr, &error);
    442   1.2   itojun 		if (in6a == 0) {
    443   1.2   itojun 			if (error == 0)
    444   1.2   itojun 				error = EADDRNOTAVAIL;
    445   1.2   itojun 			return(error);
    446   1.2   itojun 		}
    447   1.2   itojun 	}
    448   1.2   itojun 	if (in6p->in6p_route.ro_rt)
    449   1.2   itojun 		ifp = in6p->in6p_route.ro_rt->rt_ifp;
    450   1.2   itojun 
    451  1.10   itojun 	in6p->in6p_ip6.ip6_hlim = (u_int8_t)in6_selecthlim(in6p, ifp);
    452   1.2   itojun 
    453   1.2   itojun 	if (in6_pcblookup(in6p->in6p_head,
    454   1.2   itojun 			 &sin6->sin6_addr,
    455   1.2   itojun 			 sin6->sin6_port,
    456  1.10   itojun 			 IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr) ?
    457   1.2   itojun 			  in6a : &in6p->in6p_laddr,
    458   1.2   itojun 			 in6p->in6p_lport,
    459   1.2   itojun 			 0))
    460   1.2   itojun 		return(EADDRINUSE);
    461  1.10   itojun 	if (IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr)
    462   1.2   itojun 	 || (IN6_IS_ADDR_V4MAPPED(&in6p->in6p_laddr)
    463   1.2   itojun 	  && in6p->in6p_laddr.s6_addr32[3] == 0)) {
    464   1.2   itojun 		if (in6p->in6p_lport == 0)
    465   1.2   itojun 			(void)in6_pcbbind(in6p, (struct mbuf *)0);
    466   1.2   itojun 		in6p->in6p_laddr = *in6a;
    467   1.2   itojun 	}
    468   1.2   itojun 	in6p->in6p_faddr = sin6->sin6_addr;
    469   1.2   itojun 	in6p->in6p_fport = sin6->sin6_port;
    470   1.2   itojun 	/*
    471   1.2   itojun 	 * xxx kazu flowlabel is necessary for connect?
    472   1.2   itojun 	 * but if this line is missing, the garbage value remains.
    473   1.2   itojun 	 */
    474   1.2   itojun 	in6p->in6p_flowinfo = sin6->sin6_flowinfo;
    475   1.2   itojun 	return(0);
    476   1.2   itojun }
    477   1.2   itojun 
    478   1.2   itojun /*
    479   1.2   itojun  * Return an IPv6 address, which is the most appropriate for given
    480   1.2   itojun  * destination and user specified options.
    481   1.2   itojun  * If necessary, this function lookups the routing table and return
    482   1.2   itojun  * an entry to the caller for later use.
    483   1.2   itojun  */
    484   1.2   itojun struct in6_addr *
    485  1.10   itojun in6_selectsrc(dstsock, opts, mopts, ro, laddr, errorp)
    486   1.2   itojun 	struct sockaddr_in6 *dstsock;
    487   1.2   itojun 	struct ip6_pktopts *opts;
    488   1.2   itojun 	struct ip6_moptions *mopts;
    489   1.2   itojun 	struct route_in6 *ro;
    490  1.10   itojun 	struct in6_addr *laddr;
    491   1.2   itojun 	int *errorp;
    492   1.2   itojun {
    493   1.2   itojun 	struct in6_addr *dst;
    494   1.2   itojun 	struct in6_ifaddr *ia6 = 0;
    495  1.10   itojun 	struct in6_pktinfo *pi = NULL;
    496   1.2   itojun 
    497   1.2   itojun 	dst = &dstsock->sin6_addr;
    498   1.2   itojun 	*errorp = 0;
    499   1.2   itojun 
    500   1.2   itojun 	/*
    501   1.2   itojun 	 * If the source address is explicitly specified by the caller,
    502   1.2   itojun 	 * use it.
    503  1.10   itojun 	 */
    504  1.10   itojun 	if (opts && (pi = opts->ip6po_pktinfo) &&
    505  1.10   itojun 	    !IN6_IS_ADDR_UNSPECIFIED(&pi->ipi6_addr))
    506  1.10   itojun 		return(&pi->ipi6_addr);
    507  1.10   itojun 
    508  1.10   itojun 	/*
    509  1.10   itojun 	 * If the source address is not specified but the socket(if any)
    510  1.10   itojun 	 * is already bound, use the bound address.
    511  1.10   itojun 	 */
    512  1.10   itojun 	if (laddr && !IN6_IS_ADDR_UNSPECIFIED(laddr))
    513  1.10   itojun 		return(laddr);
    514  1.10   itojun 
    515  1.10   itojun 	/*
    516   1.2   itojun 	 * If the caller doesn't specify the source address but
    517   1.2   itojun 	 * the outgoing interface, use an address associated with
    518   1.2   itojun 	 * the interface.
    519   1.2   itojun 	 */
    520  1.10   itojun 	if (pi && pi->ipi6_ifindex) {
    521  1.10   itojun 		/* XXX boundary check is assumed to be already done. */
    522  1.10   itojun 		ia6 = in6_ifawithscope(ifindex2ifnet[pi->ipi6_ifindex],
    523  1.10   itojun 				       dst);
    524  1.10   itojun 		if (ia6 == 0) {
    525  1.10   itojun 			*errorp = EADDRNOTAVAIL;
    526  1.10   itojun 			return(0);
    527  1.10   itojun 		}
    528  1.10   itojun 		return(&satosin6(&ia6->ia_addr)->sin6_addr);
    529  1.10   itojun 	}
    530  1.10   itojun 
    531  1.10   itojun 	/*
    532  1.10   itojun 	 * If the destination address is a link-local unicast address or
    533  1.10   itojun 	 * a multicast address, and if the outgoing interface is specified
    534  1.10   itojun 	 * by the sin6_scope_id filed, use an address associated with the
    535  1.10   itojun 	 * interface.
    536  1.10   itojun 	 * XXX: We're now trying to define more specific semantics of
    537  1.10   itojun 	 *      sin6_scope_id field, so this part will be rewritten in
    538  1.10   itojun 	 *      the near future.
    539  1.10   itojun 	 */
    540  1.10   itojun 	if ((IN6_IS_ADDR_LINKLOCAL(dst) || IN6_IS_ADDR_MULTICAST(dst)) &&
    541  1.10   itojun 	    dstsock->sin6_scope_id) {
    542  1.10   itojun 		/*
    543  1.10   itojun 		 * I'm not sure if boundary check for scope_id is done
    544  1.10   itojun 		 * somewhere...
    545  1.10   itojun 		 */
    546  1.10   itojun 		if (dstsock->sin6_scope_id < 0 ||
    547  1.10   itojun 		    if_index < dstsock->sin6_scope_id) {
    548  1.10   itojun 			*errorp = ENXIO; /* XXX: better error? */
    549  1.10   itojun 			return(0);
    550  1.10   itojun 		}
    551  1.10   itojun 		ia6 = in6_ifawithscope(ifindex2ifnet[dstsock->sin6_scope_id],
    552  1.10   itojun 				       dst);
    553  1.10   itojun 		if (ia6 == 0) {
    554  1.10   itojun 			*errorp = EADDRNOTAVAIL;
    555  1.10   itojun 			return(0);
    556   1.2   itojun 		}
    557  1.10   itojun 		return(&satosin6(&ia6->ia_addr)->sin6_addr);
    558   1.2   itojun 	}
    559   1.2   itojun 
    560   1.2   itojun 	/*
    561   1.2   itojun 	 * If the destination address is a multicast address and
    562   1.2   itojun 	 * the outgoing interface for the address is specified
    563   1.2   itojun 	 * by the caller, use an address associated with the interface.
    564   1.2   itojun 	 * There is a sanity check here; if the destination has node-local
    565   1.2   itojun 	 * scope, the outgoing interfacde should be a loopback address.
    566   1.2   itojun 	 * Even if the outgoing interface is not specified, we also
    567   1.2   itojun 	 * choose a loopback interface as the outgoing interface.
    568   1.2   itojun 	 */
    569   1.2   itojun 	if (IN6_IS_ADDR_MULTICAST(dst)) {
    570   1.2   itojun 		struct ifnet *ifp = mopts ? mopts->im6o_multicast_ifp : NULL;
    571   1.2   itojun #ifdef __bsdi__
    572   1.2   itojun 		extern struct ifnet loif;
    573   1.2   itojun #endif
    574   1.2   itojun 
    575   1.2   itojun 		if (ifp == NULL && IN6_IS_ADDR_MC_NODELOCAL(dst)) {
    576   1.2   itojun #ifdef __bsdi__
    577   1.2   itojun 			ifp = &loif;
    578   1.2   itojun #else
    579   1.2   itojun 			ifp = &loif[0];
    580   1.2   itojun #endif
    581   1.2   itojun 		}
    582   1.2   itojun 
    583   1.2   itojun 		if (ifp) {
    584   1.2   itojun 			ia6 = in6_ifawithscope(ifp, dst);
    585   1.2   itojun 			if (ia6 == 0) {
    586   1.2   itojun 				*errorp = EADDRNOTAVAIL;
    587   1.2   itojun 				return(0);
    588   1.2   itojun 			}
    589   1.2   itojun 			return(&satosin6(&ia6->ia_addr)->sin6_addr);
    590   1.2   itojun 		}
    591   1.2   itojun 	}
    592   1.2   itojun 
    593   1.2   itojun 	/*
    594   1.2   itojun 	 * If the next hop address for the packet is specified
    595   1.2   itojun 	 * by caller, use an address associated with the route
    596   1.2   itojun 	 * to the next hop.
    597   1.2   itojun 	 */
    598   1.2   itojun 	{
    599   1.2   itojun 		struct sockaddr_in6 *sin6_next;
    600   1.2   itojun 		struct rtentry *rt;
    601   1.2   itojun 
    602   1.2   itojun 		if (opts && opts->ip6po_nexthop) {
    603   1.2   itojun 			sin6_next = satosin6(opts->ip6po_nexthop);
    604   1.2   itojun 			rt = nd6_lookup(&sin6_next->sin6_addr, 1, NULL);
    605   1.2   itojun 			if (rt) {
    606   1.2   itojun 				ia6 = in6_ifawithscope(rt->rt_ifp, dst);
    607   1.2   itojun 				if (ia6 == 0)
    608   1.2   itojun 					ia6 = ifatoia6(rt->rt_ifa);
    609   1.2   itojun 			}
    610   1.2   itojun 			if (ia6 == 0) {
    611   1.2   itojun 				*errorp = EADDRNOTAVAIL;
    612   1.2   itojun 				return(0);
    613   1.2   itojun 			}
    614   1.2   itojun 			return(&satosin6(&ia6->ia_addr)->sin6_addr);
    615   1.2   itojun 		}
    616   1.2   itojun 	}
    617   1.2   itojun 
    618   1.2   itojun 	/*
    619   1.2   itojun 	 * If route is known or can be allocated now,
    620   1.2   itojun 	 * our src addr is taken from the i/f, else punt.
    621   1.2   itojun 	 */
    622   1.2   itojun 	if (ro) {
    623   1.2   itojun 		if (ro->ro_rt &&
    624   1.2   itojun 		    !IN6_ARE_ADDR_EQUAL(&satosin6(&ro->ro_dst)->sin6_addr, dst)) {
    625   1.2   itojun 			RTFREE(ro->ro_rt);
    626   1.2   itojun 			ro->ro_rt = (struct rtentry *)0;
    627   1.2   itojun 		}
    628   1.2   itojun 		if (ro->ro_rt == (struct rtentry *)0 ||
    629   1.2   itojun 		    ro->ro_rt->rt_ifp == (struct ifnet *)0) {
    630   1.2   itojun 			/* No route yet, so try to acquire one */
    631   1.2   itojun 			bzero(&ro->ro_dst, sizeof(struct sockaddr_in6));
    632   1.2   itojun 			ro->ro_dst.sin6_family = AF_INET6;
    633   1.2   itojun 			ro->ro_dst.sin6_len = sizeof(struct sockaddr_in6);
    634   1.2   itojun 			ro->ro_dst.sin6_addr = *dst;
    635   1.2   itojun 			if (IN6_IS_ADDR_MULTICAST(dst)) {
    636   1.2   itojun #ifdef __FreeBSD__
    637   1.2   itojun 				ro->ro_rt = rtalloc1(&((struct route *)ro)
    638   1.2   itojun 						     ->ro_dst, 0, 0UL);
    639   1.2   itojun #endif /*__FreeBSD__*/
    640   1.2   itojun #if defined(__bsdi__) || defined(__NetBSD__)
    641   1.2   itojun 				ro->ro_rt = rtalloc1(&((struct route *)ro)
    642   1.2   itojun 						     ->ro_dst, 0);
    643   1.2   itojun #endif /*__bsdi__*/
    644  1.10   itojun 			} else
    645   1.2   itojun 				rtalloc((struct route *)ro);
    646   1.2   itojun 		}
    647   1.2   itojun 
    648   1.2   itojun 		/*
    649   1.2   itojun 		 * in_pcbconnect() checks out IFF_LOOPBACK to skip using
    650   1.2   itojun 		 * the address. But we don't know why it does so.
    651   1.2   itojun 		 * It is necessary to ensure the scope even for lo0
    652   1.2   itojun 		 * so doesn't check out IFF_LOOPBACK.
    653   1.2   itojun 		 */
    654   1.2   itojun 
    655   1.2   itojun 		if (ro->ro_rt) {
    656   1.2   itojun 			ia6 = in6_ifawithscope(ro->ro_rt->rt_ifa->ifa_ifp, dst);
    657   1.2   itojun 			if (ia6 == 0) /* xxx scope error ?*/
    658   1.2   itojun 				ia6 = ifatoia6(ro->ro_rt->rt_ifa);
    659   1.2   itojun 		}
    660   1.2   itojun #if 0
    661   1.2   itojun 		/*
    662   1.2   itojun 		 * xxx The followings are necessary? (kazu)
    663   1.2   itojun 		 * I don't think so.
    664   1.2   itojun 		 * It's for SO_DONTROUTE option in IPv4.(jinmei)
    665   1.2   itojun 		 */
    666   1.2   itojun 		if (ia6 == 0) {
    667   1.2   itojun 			struct sockaddr_in6 sin6 = {sizeof(sin6), AF_INET6, 0};
    668   1.2   itojun 
    669   1.2   itojun 			sin6->sin6_addr = *dst;
    670   1.2   itojun 
    671   1.2   itojun 			ia6 = ifatoia6(ifa_ifwithdstaddr(sin6tosa(&sin6)));
    672   1.2   itojun 			if (ia6 == 0)
    673   1.2   itojun 				ia6 = ifatoia6(ifa_ifwithnet(sin6tosa(&sin6)));
    674   1.2   itojun 			if (ia6 == 0)
    675   1.2   itojun 				return(0);
    676   1.2   itojun 			return(&satosin6(&ia6->ia_addr)->sin6_addr);
    677   1.2   itojun 		}
    678   1.2   itojun #endif /* 0 */
    679   1.2   itojun 		if (ia6 == 0) {
    680   1.2   itojun 			*errorp = EHOSTUNREACH;	/* no route */
    681   1.2   itojun 			return(0);
    682   1.2   itojun 		}
    683   1.2   itojun 		return(&satosin6(&ia6->ia_addr)->sin6_addr);
    684   1.2   itojun 	}
    685   1.2   itojun 
    686   1.2   itojun 	*errorp = EADDRNOTAVAIL;
    687   1.2   itojun 	return(0);
    688   1.2   itojun }
    689   1.2   itojun 
    690  1.10   itojun /*
    691  1.10   itojun  * Default hop limit selection. The precedence is as follows:
    692  1.10   itojun  * 1. Hoplimit valued specified via ioctl.
    693  1.10   itojun  * 2. (If the outgoing interface is detected) the current
    694  1.10   itojun  *     hop limit of the interface specified by router advertisement.
    695  1.10   itojun  * 3. The system default hoplimit.
    696  1.10   itojun */
    697  1.10   itojun int
    698  1.10   itojun in6_selecthlim(in6p, ifp)
    699  1.10   itojun 	struct in6pcb *in6p;
    700  1.10   itojun 	struct ifnet *ifp;
    701  1.10   itojun {
    702  1.10   itojun 	if (in6p && in6p->in6p_hops >= 0)
    703  1.10   itojun 		return(in6p->in6p_hops);
    704  1.10   itojun 	else if (ifp)
    705  1.10   itojun 		return(nd_ifinfo[ifp->if_index].chlim);
    706  1.10   itojun 	else
    707  1.10   itojun 		return(ip6_defhlim);
    708  1.10   itojun }
    709  1.10   itojun 
    710   1.2   itojun void
    711   1.2   itojun in6_pcbdisconnect(in6p)
    712   1.2   itojun 	struct in6pcb *in6p;
    713   1.2   itojun {
    714   1.2   itojun 	bzero((caddr_t)&in6p->in6p_faddr, sizeof(in6p->in6p_faddr));
    715   1.2   itojun 	in6p->in6p_fport = 0;
    716   1.2   itojun 	if (in6p->in6p_socket->so_state & SS_NOFDREF)
    717   1.2   itojun 		in6_pcbdetach(in6p);
    718   1.2   itojun }
    719   1.2   itojun 
    720   1.2   itojun void
    721   1.2   itojun in6_pcbdetach(in6p)
    722   1.2   itojun 	struct in6pcb *in6p;
    723   1.2   itojun {
    724   1.2   itojun 	struct socket *so = in6p->in6p_socket;
    725   1.2   itojun 
    726   1.2   itojun #ifdef IPSEC
    727   1.2   itojun 	if (sotoin6pcb(so) != 0)
    728   1.2   itojun 		key_freeso(so);
    729   1.2   itojun 	ipsec6_delete_pcbpolicy(in6p);
    730   1.2   itojun #endif /* IPSEC */
    731   1.2   itojun 	sotoin6pcb(so) = 0;
    732   1.2   itojun 	sofree(so);
    733   1.2   itojun 	if (in6p->in6p_options)
    734   1.2   itojun 		m_freem(in6p->in6p_options);
    735   1.2   itojun 	if (in6p->in6p_outputopts) {
    736   1.2   itojun 		if (in6p->in6p_outputopts->ip6po_rthdr &&
    737   1.2   itojun 		    in6p->in6p_outputopts->ip6po_route.ro_rt)
    738   1.2   itojun 			RTFREE(in6p->in6p_outputopts->ip6po_route.ro_rt);
    739   1.2   itojun 		if (in6p->in6p_outputopts->ip6po_m)
    740   1.2   itojun 			(void)m_free(in6p->in6p_outputopts->ip6po_m);
    741   1.2   itojun 		free(in6p->in6p_outputopts, M_IP6OPT);
    742   1.2   itojun 	}
    743   1.2   itojun 	if (in6p->in6p_route.ro_rt)
    744   1.2   itojun 		rtfree(in6p->in6p_route.ro_rt);
    745   1.2   itojun 	ip6_freemoptions(in6p->in6p_moptions);
    746   1.3   itojun #if 0
    747   1.2   itojun 	remque(in6p);
    748   1.3   itojun #else
    749   1.3   itojun 	in6p->in6p_next->in6p_prev = in6p->in6p_prev;
    750   1.3   itojun 	in6p->in6p_prev->in6p_next = in6p->in6p_next;
    751   1.3   itojun 	in6p->in6p_prev = NULL;
    752   1.3   itojun #endif
    753   1.2   itojun 	FREE(in6p, M_PCB);
    754   1.2   itojun }
    755   1.2   itojun 
    756   1.2   itojun void
    757   1.2   itojun in6_setsockaddr(in6p, nam)
    758   1.2   itojun 	struct in6pcb *in6p;
    759   1.2   itojun 	struct mbuf *nam;
    760   1.2   itojun {
    761   1.2   itojun 	struct sockaddr_in6 *sin6;
    762   1.2   itojun 
    763   1.2   itojun 	nam->m_len = sizeof(*sin6);
    764   1.2   itojun 	sin6 = mtod(nam, struct sockaddr_in6 *);
    765   1.2   itojun 	bzero((caddr_t)sin6, sizeof(*sin6));
    766   1.2   itojun 	sin6->sin6_family = AF_INET6;
    767   1.2   itojun 	sin6->sin6_len = sizeof(struct sockaddr_in6);
    768   1.2   itojun 	sin6->sin6_port = in6p->in6p_lport;
    769   1.2   itojun 	sin6->sin6_addr = in6p->in6p_laddr;
    770   1.2   itojun 	if (IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr))
    771   1.2   itojun 		sin6->sin6_scope_id = ntohs(sin6->sin6_addr.s6_addr16[1]);
    772   1.2   itojun 	else
    773   1.2   itojun 		sin6->sin6_scope_id = 0;	/*XXX*/
    774   1.2   itojun 	if (IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr))
    775   1.2   itojun 		sin6->sin6_addr.s6_addr16[1] = 0;
    776   1.2   itojun }
    777   1.2   itojun 
    778   1.2   itojun void
    779   1.2   itojun in6_setpeeraddr(in6p, nam)
    780   1.2   itojun 	struct in6pcb *in6p;
    781   1.2   itojun 	struct mbuf *nam;
    782   1.2   itojun {
    783   1.2   itojun 	struct sockaddr_in6 *sin6;
    784   1.2   itojun 
    785   1.2   itojun 	nam->m_len = sizeof(*sin6);
    786   1.2   itojun 	sin6 = mtod(nam, struct sockaddr_in6 *);
    787   1.2   itojun 	bzero((caddr_t)sin6, sizeof(*sin6));
    788   1.2   itojun 	sin6->sin6_family = AF_INET6;
    789   1.2   itojun 	sin6->sin6_len = sizeof(struct sockaddr_in6);
    790   1.2   itojun 	sin6->sin6_port = in6p->in6p_fport;
    791   1.2   itojun 	sin6->sin6_addr = in6p->in6p_faddr;
    792   1.2   itojun 	if (IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr))
    793   1.2   itojun 		sin6->sin6_scope_id = ntohs(sin6->sin6_addr.s6_addr16[1]);
    794   1.2   itojun 	else
    795   1.2   itojun 		sin6->sin6_scope_id = 0;	/*XXX*/
    796   1.2   itojun 	if (IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr))
    797   1.2   itojun 		sin6->sin6_addr.s6_addr16[1] = 0;
    798   1.2   itojun }
    799   1.2   itojun 
    800   1.2   itojun /*
    801   1.2   itojun  * Pass some notification to all connections of a protocol
    802   1.2   itojun  * associated with address dst.  The local address and/or port numbers
    803   1.2   itojun  * may be specified to limit the search.  The "usual action" will be
    804   1.2   itojun  * taken, depending on the ctlinput cmd.  The caller must filter any
    805   1.2   itojun  * cmds that are uninteresting (e.g., no error in the map).
    806   1.2   itojun  * Call the protocol specific routine (if any) to report
    807   1.2   itojun  * any errors for each matching socket.
    808   1.2   itojun  *
    809   1.6   itojun  * Must be called at splsoftnet.
    810   1.2   itojun  */
    811   1.2   itojun int
    812   1.2   itojun in6_pcbnotify(head, dst, fport_arg, laddr6, lport_arg, cmd, notify)
    813   1.2   itojun 	struct in6pcb *head;
    814   1.2   itojun 	struct sockaddr *dst;
    815   1.2   itojun 	u_int fport_arg, lport_arg;
    816   1.2   itojun 	struct in6_addr *laddr6;
    817   1.2   itojun 	int cmd;
    818   1.2   itojun 	void (*notify) __P((struct in6pcb *, int));
    819   1.2   itojun {
    820   1.2   itojun 	struct in6pcb *in6p, *oin6p;
    821   1.2   itojun 	struct in6_addr faddr6;
    822   1.2   itojun 	u_short	fport = fport_arg, lport = lport_arg;
    823   1.2   itojun 	int errno;
    824   1.2   itojun 	int nmatch = 0;
    825   1.2   itojun 
    826   1.2   itojun 	if ((unsigned)cmd > PRC_NCMDS || dst->sa_family != AF_INET6)
    827   1.2   itojun 		return 0;
    828   1.2   itojun 	faddr6 = ((struct sockaddr_in6 *)dst)->sin6_addr;
    829  1.10   itojun 	if (IN6_IS_ADDR_UNSPECIFIED(&faddr6))
    830   1.2   itojun 		return 0;
    831   1.2   itojun 
    832   1.2   itojun 	/*
    833   1.2   itojun 	 * Redirects go to all references to the destination,
    834   1.2   itojun 	 * and use in_rtchange to invalidate the route cache.
    835   1.2   itojun 	 * Dead host indications: notify all references to the destination.
    836   1.2   itojun 	 * Otherwise, if we have knowledge of the local port and address,
    837   1.2   itojun 	 * deliver only to that socket.
    838   1.2   itojun 	 */
    839   1.2   itojun 	if (PRC_IS_REDIRECT(cmd) || cmd == PRC_HOSTDEAD) {
    840   1.2   itojun 		fport = 0;
    841   1.2   itojun 		lport = 0;
    842   1.2   itojun 		bzero((caddr_t)laddr6, sizeof(*laddr6));
    843   1.2   itojun 		if (cmd != PRC_HOSTDEAD)
    844   1.2   itojun 			notify = in6_rtchange;
    845   1.2   itojun 	}
    846   1.2   itojun 	if (notify == NULL)
    847   1.2   itojun 		return 0;
    848   1.2   itojun 	errno = inet6ctlerrmap[cmd];
    849   1.2   itojun 	for (in6p = head->in6p_next; in6p != head;) {
    850   1.2   itojun 		if (!IN6_ARE_ADDR_EQUAL(&in6p->in6p_faddr,&faddr6) ||
    851   1.2   itojun 		   in6p->in6p_socket == 0 ||
    852   1.2   itojun 		   (lport && in6p->in6p_lport != lport) ||
    853  1.10   itojun 		   (!IN6_IS_ADDR_UNSPECIFIED(laddr6) &&
    854   1.2   itojun 		    !IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr, laddr6)) ||
    855   1.2   itojun 		   (fport && in6p->in6p_fport != fport)) {
    856   1.2   itojun 			in6p = in6p->in6p_next;
    857   1.2   itojun 			continue;
    858   1.2   itojun 		}
    859   1.2   itojun 		oin6p = in6p;
    860   1.2   itojun 		in6p = in6p->in6p_next;
    861   1.2   itojun 		(*notify)(oin6p, errno);
    862   1.2   itojun 		nmatch++;
    863   1.2   itojun 	}
    864   1.2   itojun 	return nmatch;
    865   1.2   itojun }
    866   1.2   itojun 
    867   1.2   itojun /*
    868   1.2   itojun  * Check for alternatives when higher level complains
    869   1.2   itojun  * about service problems.  For now, invalidate cached
    870   1.2   itojun  * routing information.  If the route was created dynamically
    871   1.2   itojun  * (by a redirect), time to try a default gateway again.
    872   1.2   itojun  */
    873   1.2   itojun void
    874   1.2   itojun in6_losing(in6p)
    875   1.2   itojun 	struct in6pcb *in6p;
    876   1.2   itojun {
    877   1.2   itojun 	struct rtentry *rt;
    878   1.2   itojun 	struct rt_addrinfo info;
    879   1.2   itojun 
    880   1.2   itojun 	if ((rt = in6p->in6p_route.ro_rt) != NULL) {
    881   1.2   itojun 		in6p->in6p_route.ro_rt = 0;
    882   1.2   itojun 		bzero((caddr_t)&info, sizeof(info));
    883   1.2   itojun 		info.rti_info[RTAX_DST] =
    884   1.2   itojun 			(struct sockaddr *)&in6p->in6p_route.ro_dst;
    885   1.2   itojun 		info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
    886   1.2   itojun 		info.rti_info[RTAX_NETMASK] = rt_mask(rt);
    887   1.2   itojun 		rt_missmsg(RTM_LOSING, &info, rt->rt_flags, 0);
    888   1.2   itojun 		if (rt->rt_flags & RTF_DYNAMIC)
    889   1.2   itojun 			(void)rtrequest(RTM_DELETE, rt_key(rt),
    890   1.2   itojun 					rt->rt_gateway, rt_mask(rt), rt->rt_flags,
    891   1.2   itojun 					(struct rtentry **)0);
    892   1.2   itojun 		else
    893   1.2   itojun 		/*
    894   1.2   itojun 		 * A new route can be allocated
    895   1.2   itojun 		 * the next time output is attempted.
    896   1.2   itojun 		 */
    897   1.2   itojun 			rtfree(rt);
    898   1.2   itojun 	}
    899   1.2   itojun }
    900   1.2   itojun 
    901   1.2   itojun /*
    902   1.2   itojun  * After a routing change, flush old routing
    903   1.2   itojun  * and allocate a (hopefully) better one.
    904   1.2   itojun  */
    905   1.2   itojun void
    906   1.2   itojun in6_rtchange(in6p, errno)
    907   1.2   itojun 	struct in6pcb *in6p;
    908   1.2   itojun 	int errno;
    909   1.2   itojun {
    910   1.2   itojun 	if (in6p->in6p_route.ro_rt) {
    911   1.2   itojun 		rtfree(in6p->in6p_route.ro_rt);
    912   1.2   itojun 		in6p->in6p_route.ro_rt = 0;
    913   1.2   itojun 		/*
    914   1.2   itojun 		 * A new route can be allocated the next time
    915   1.2   itojun 		 * output is attempted.
    916   1.2   itojun 		 */
    917   1.2   itojun 	}
    918   1.2   itojun }
    919   1.2   itojun 
    920   1.2   itojun struct in6pcb *
    921   1.2   itojun in6_pcblookup(head, faddr6, fport_arg, laddr6, lport_arg, flags)
    922   1.2   itojun 	struct in6pcb *head;
    923   1.2   itojun 	struct in6_addr *faddr6, *laddr6;
    924   1.2   itojun 	u_int fport_arg, lport_arg;
    925   1.2   itojun 	int flags;
    926   1.2   itojun {
    927   1.2   itojun 	struct in6pcb *in6p, *match = 0;
    928   1.2   itojun 	int matchwild = 3, wildcard;
    929   1.2   itojun 	u_short	fport = fport_arg, lport = lport_arg;
    930   1.2   itojun 
    931   1.2   itojun 	for (in6p = head->in6p_next; in6p != head; in6p = in6p->in6p_next) {
    932   1.2   itojun 		if (in6p->in6p_lport != lport)
    933   1.2   itojun 			continue;
    934   1.2   itojun 		wildcard = 0;
    935  1.10   itojun 		if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr)) {
    936  1.10   itojun 			if (IN6_IS_ADDR_UNSPECIFIED(laddr6))
    937   1.2   itojun 				wildcard++;
    938   1.2   itojun 			else if (!IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr, laddr6))
    939   1.2   itojun 				continue;
    940   1.2   itojun 		} else {
    941  1.10   itojun 			if (!IN6_IS_ADDR_UNSPECIFIED(laddr6))
    942   1.2   itojun 				wildcard++;
    943   1.2   itojun 		}
    944  1.10   itojun 		if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr)) {
    945  1.10   itojun 			if (IN6_IS_ADDR_UNSPECIFIED(faddr6))
    946   1.2   itojun 				wildcard++;
    947   1.2   itojun 			else if (!IN6_ARE_ADDR_EQUAL(&in6p->in6p_faddr, faddr6)
    948   1.2   itojun 			      || in6p->in6p_fport != fport)
    949   1.2   itojun 				continue;
    950   1.2   itojun 		} else {
    951  1.10   itojun 			if (!IN6_IS_ADDR_UNSPECIFIED(faddr6))
    952   1.2   itojun 				wildcard++;
    953   1.2   itojun 		}
    954   1.2   itojun 		if (wildcard && (flags & IN6PLOOKUP_WILDCARD) == 0)
    955   1.2   itojun 			continue;
    956   1.2   itojun 		if (wildcard < matchwild) {
    957   1.2   itojun 			match = in6p;
    958   1.2   itojun 			matchwild = wildcard;
    959   1.2   itojun 			if (matchwild == 0)
    960   1.2   itojun 				break;
    961   1.2   itojun 		}
    962   1.2   itojun 	}
    963   1.2   itojun 	return(match);
    964   1.2   itojun }
    965   1.2   itojun 
    966   1.2   itojun #ifndef TCP6
    967   1.2   itojun struct rtentry *
    968   1.2   itojun in6_pcbrtentry(in6p)
    969   1.2   itojun 	struct in6pcb *in6p;
    970   1.2   itojun {
    971   1.2   itojun 	struct route_in6 *ro;
    972   1.2   itojun 
    973   1.2   itojun 	ro = &in6p->in6p_route;
    974   1.2   itojun 
    975   1.2   itojun 	if (ro->ro_rt == NULL) {
    976   1.2   itojun 		/*
    977   1.2   itojun 		 * No route yet, so try to acquire one.
    978   1.2   itojun 		 */
    979   1.2   itojun 		if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr)) {
    980   1.2   itojun 			bzero(&ro->ro_dst, sizeof(ro->ro_dst));
    981   1.2   itojun 			ro->ro_dst.sin6_family = AF_INET6;
    982   1.2   itojun 			ro->ro_dst.sin6_len = sizeof(struct sockaddr_in6);
    983   1.2   itojun 			satosin6(&ro->ro_dst)->sin6_addr = in6p->in6p_faddr;
    984   1.2   itojun 			rtalloc((struct route *)ro);
    985   1.2   itojun 		}
    986   1.2   itojun 	}
    987   1.2   itojun 	return (ro->ro_rt);
    988   1.2   itojun }
    989   1.2   itojun 
    990   1.2   itojun struct in6pcb *
    991   1.8   itojun in6_pcblookup_connect(head, faddr6, fport_arg, laddr6, lport_arg, faith)
    992   1.2   itojun 	struct in6pcb *head;
    993   1.2   itojun 	struct in6_addr *faddr6, *laddr6;
    994   1.2   itojun 	u_int fport_arg, lport_arg;
    995   1.8   itojun 	int faith;
    996   1.2   itojun {
    997   1.2   itojun 	struct in6pcb *in6p;
    998   1.2   itojun 	u_short	fport = fport_arg, lport = lport_arg;
    999   1.2   itojun 
   1000   1.2   itojun 	for (in6p = head->in6p_next; in6p != head; in6p = in6p->in6p_next) {
   1001   1.8   itojun #if defined(NFAITH) && NFAITH > 0
   1002   1.8   itojun 		if (faith && (in6p->in6p_flags & IN6P_FAITH) == 0)
   1003   1.8   itojun 			continue;
   1004   1.8   itojun #endif
   1005   1.2   itojun 		/* find exact match on both source and dest */
   1006   1.2   itojun 		if (in6p->in6p_fport != fport)
   1007   1.2   itojun 			continue;
   1008   1.2   itojun 		if (in6p->in6p_lport != lport)
   1009   1.2   itojun 			continue;
   1010   1.2   itojun 		if (IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr))
   1011   1.2   itojun 			continue;
   1012   1.2   itojun 		if (!IN6_ARE_ADDR_EQUAL(&in6p->in6p_faddr, faddr6))
   1013   1.2   itojun 			continue;
   1014   1.2   itojun 		if (IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr))
   1015   1.2   itojun 			continue;
   1016   1.2   itojun 		if (!IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr, laddr6))
   1017   1.2   itojun 			continue;
   1018   1.2   itojun 		return in6p;
   1019   1.2   itojun 	}
   1020   1.2   itojun 	return NULL;
   1021   1.2   itojun }
   1022   1.2   itojun 
   1023   1.2   itojun struct in6pcb *
   1024   1.8   itojun in6_pcblookup_bind(head, laddr6, lport_arg, faith)
   1025   1.2   itojun 	struct in6pcb *head;
   1026   1.2   itojun 	struct in6_addr *laddr6;
   1027   1.2   itojun 	u_int lport_arg;
   1028   1.8   itojun 	int faith;
   1029   1.2   itojun {
   1030   1.2   itojun 	struct in6pcb *in6p, *match;
   1031   1.2   itojun 	u_short	lport = lport_arg;
   1032   1.2   itojun 
   1033   1.2   itojun 	match = NULL;
   1034   1.2   itojun 	for (in6p = head->in6p_next; in6p != head; in6p = in6p->in6p_next) {
   1035   1.2   itojun 		/*
   1036   1.2   itojun 	 	 * find destination match.  exact match is preferred
   1037   1.2   itojun 		 * against wildcard match.
   1038   1.2   itojun 		 */
   1039   1.8   itojun #if defined(NFAITH) && NFAITH > 0
   1040   1.8   itojun 		if (faith && (in6p->in6p_flags & IN6P_FAITH) == 0)
   1041   1.8   itojun 			continue;
   1042   1.8   itojun #endif
   1043   1.2   itojun 		if (in6p->in6p_fport != 0)
   1044   1.2   itojun 			continue;
   1045   1.2   itojun 		if (in6p->in6p_lport != lport)
   1046   1.2   itojun 			continue;
   1047   1.2   itojun 		if (IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr))
   1048   1.2   itojun 			match = in6p;
   1049   1.2   itojun 		else if (IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr, laddr6))
   1050   1.2   itojun 			return in6p;
   1051   1.2   itojun 	}
   1052   1.2   itojun 	return match;
   1053   1.2   itojun }
   1054   1.2   itojun #endif
   1055