xref: /freebsd/sys/netinet6/in6_pcb.c (revision 7660b554bc59a07be0431c17e0e33815818baa69)
1 /*	$FreeBSD$	*/
2 /*	$KAME: in6_pcb.c,v 1.31 2001/05/21 05:45:10 jinmei Exp $	*/
3 
4 /*
5  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. Neither the name of the project nor the names of its contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  *
32  */
33 
34 /*
35  * Copyright (c) 1982, 1986, 1991, 1993
36  *	The Regents of the University of California.  All rights reserved.
37  *
38  * Redistribution and use in source and binary forms, with or without
39  * modification, are permitted provided that the following conditions
40  * are met:
41  * 1. Redistributions of source code must retain the above copyright
42  *    notice, this list of conditions and the following disclaimer.
43  * 2. Redistributions in binary form must reproduce the above copyright
44  *    notice, this list of conditions and the following disclaimer in the
45  *    documentation and/or other materials provided with the distribution.
46  * 3. All advertising materials mentioning features or use of this software
47  *    must display the following acknowledgement:
48  *	This product includes software developed by the University of
49  *	California, Berkeley and its contributors.
50  * 4. Neither the name of the University nor the names of its contributors
51  *    may be used to endorse or promote products derived from this software
52  *    without specific prior written permission.
53  *
54  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
55  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
56  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
57  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
58  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
59  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
60  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
61  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
62  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
63  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
64  * SUCH DAMAGE.
65  *
66  *	@(#)in_pcb.c	8.2 (Berkeley) 1/4/94
67  */
68 
69 #include "opt_inet.h"
70 #include "opt_inet6.h"
71 #include "opt_ipsec.h"
72 
73 #include <sys/param.h>
74 #include <sys/systm.h>
75 #include <sys/malloc.h>
76 #include <sys/mbuf.h>
77 #include <sys/domain.h>
78 #include <sys/protosw.h>
79 #include <sys/socket.h>
80 #include <sys/socketvar.h>
81 #include <sys/sockio.h>
82 #include <sys/errno.h>
83 #include <sys/time.h>
84 #include <sys/proc.h>
85 #include <sys/jail.h>
86 
87 #include <vm/uma.h>
88 
89 #include <net/if.h>
90 #include <net/if_types.h>
91 #include <net/route.h>
92 
93 #include <netinet/in.h>
94 #include <netinet/in_var.h>
95 #include <netinet/in_systm.h>
96 #include <netinet/tcp_var.h>
97 #include <netinet/ip6.h>
98 #include <netinet/ip_var.h>
99 #include <netinet6/ip6_var.h>
100 #include <netinet6/nd6.h>
101 #include <netinet/in_pcb.h>
102 #include <netinet6/in6_pcb.h>
103 
104 #ifdef IPSEC
105 #include <netinet6/ipsec.h>
106 #ifdef INET6
107 #include <netinet6/ipsec6.h>
108 #endif
109 #include <netinet6/ah.h>
110 #ifdef INET6
111 #include <netinet6/ah6.h>
112 #endif
113 #include <netkey/key.h>
114 #endif /* IPSEC */
115 
116 #ifdef FAST_IPSEC
117 #include <netipsec/ipsec.h>
118 #include <netipsec/ipsec6.h>
119 #include <netipsec/key.h>
120 #define	IPSEC
121 #endif /* FAST_IPSEC */
122 
123 struct	in6_addr zeroin6_addr;
124 
125 int
126 in6_pcbbind(inp, nam, td)
127 	register struct inpcb *inp;
128 	struct sockaddr *nam;
129 	struct thread *td;
130 {
131 	struct socket *so = inp->inp_socket;
132 	struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)NULL;
133 	struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
134 	u_short	lport = 0;
135 	int wild = 0, reuseport = (so->so_options & SO_REUSEPORT);
136 
137 	if (!in6_ifaddr) /* XXX broken! */
138 		return (EADDRNOTAVAIL);
139 	if (inp->inp_lport || !IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr))
140 		return(EINVAL);
141 	if ((so->so_options & (SO_REUSEADDR|SO_REUSEPORT)) == 0)
142 		wild = 1;
143 	if (nam) {
144 		sin6 = (struct sockaddr_in6 *)nam;
145 		if (nam->sa_len != sizeof(*sin6))
146 			return(EINVAL);
147 		/*
148 		 * family check.
149 		 */
150 		if (nam->sa_family != AF_INET6)
151 			return(EAFNOSUPPORT);
152 
153 		/* KAME hack: embed scopeid */
154 		if (in6_embedscope(&sin6->sin6_addr, sin6, inp, NULL) != 0)
155 			return EINVAL;
156 		/* this must be cleared for ifa_ifwithaddr() */
157 		sin6->sin6_scope_id = 0;
158 
159 		lport = sin6->sin6_port;
160 		if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) {
161 			/*
162 			 * Treat SO_REUSEADDR as SO_REUSEPORT for multicast;
163 			 * allow compepte duplication of binding if
164 			 * SO_REUSEPORT is set, or if SO_REUSEADDR is set
165 			 * and a multicast address is bound on both
166 			 * new and duplicated sockets.
167 			 */
168 			if (so->so_options & SO_REUSEADDR)
169 				reuseport = SO_REUSEADDR|SO_REUSEPORT;
170 		} else if (!IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
171 			struct ifaddr *ia = NULL;
172 
173 			sin6->sin6_port = 0;		/* yech... */
174 			if ((ia = ifa_ifwithaddr((struct sockaddr *)sin6)) == 0)
175 				return(EADDRNOTAVAIL);
176 
177 			/*
178 			 * XXX: bind to an anycast address might accidentally
179 			 * cause sending a packet with anycast source address.
180 			 * We should allow to bind to a deprecated address, since
181 			 * the application dare to use it.
182 			 */
183 			if (ia &&
184 			    ((struct in6_ifaddr *)ia)->ia6_flags &
185 			    (IN6_IFF_ANYCAST|IN6_IFF_NOTREADY|IN6_IFF_DETACHED)) {
186 				return(EADDRNOTAVAIL);
187 			}
188 		}
189 		if (lport) {
190 			struct inpcb *t;
191 
192 			/* GROSS */
193 			if (ntohs(lport) < IPV6PORT_RESERVED && td &&
194 			    suser_cred(td->td_ucred, PRISON_ROOT))
195 				return(EACCES);
196 			if (so->so_cred->cr_uid != 0 &&
197 			    !IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) {
198 				t = in6_pcblookup_local(pcbinfo,
199 				    &sin6->sin6_addr, lport,
200 				    INPLOOKUP_WILDCARD);
201 				if (t && (t->inp_vflag & INP_TIMEWAIT)) {
202 					if ((!IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr) ||
203 					    !IN6_IS_ADDR_UNSPECIFIED(&t->in6p_laddr) ||
204 					    !(intotw(t)->tw_so_options & SO_REUSEPORT))
205 					    && so->so_cred->cr_uid !=
206 					    intotw(t)->tw_cred->cr_uid)
207 						return (EADDRINUSE);
208 				} else if (t &&
209 				    (!IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr) ||
210 			    	     !IN6_IS_ADDR_UNSPECIFIED(&t->in6p_laddr) ||
211 				     (t->inp_socket->so_options & SO_REUSEPORT)
212 				      == 0) && (so->so_cred->cr_uid !=
213 				     t->inp_socket->so_cred->cr_uid))
214 					return (EADDRINUSE);
215 				if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0 &&
216 				    IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
217 					struct sockaddr_in sin;
218 
219 					in6_sin6_2_sin(&sin, sin6);
220 					t = in_pcblookup_local(pcbinfo,
221 						sin.sin_addr, lport,
222 						INPLOOKUP_WILDCARD);
223 					if (t && (t->inp_vflag & INP_TIMEWAIT)) {
224 						if (so->so_cred->cr_uid !=
225 						    intotw(t)->tw_cred->cr_uid &&
226 						    (ntohl(t->inp_laddr.s_addr) !=
227 						     INADDR_ANY ||
228 						     ((inp->inp_vflag &
229 						       INP_IPV6PROTO) ==
230 						      (t->inp_vflag &
231 						       INP_IPV6PROTO))))
232 					    return (EADDRINUSE);
233 					} else if (t &&
234 					    (so->so_cred->cr_uid !=
235 					     t->inp_socket->so_cred->cr_uid) &&
236 					    (ntohl(t->inp_laddr.s_addr) !=
237 					     INADDR_ANY ||
238 					     INP_SOCKAF(so) ==
239 					     INP_SOCKAF(t->inp_socket)))
240 						return (EADDRINUSE);
241 				}
242 			}
243 			t = in6_pcblookup_local(pcbinfo, &sin6->sin6_addr,
244 						lport, wild);
245 			if (t && (reuseport & ((t->inp_vflag & INP_TIMEWAIT) ?
246 			    intotw(t)->tw_so_options :
247 			    t->inp_socket->so_options)) == 0)
248 				return(EADDRINUSE);
249 			if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0 &&
250 			    IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
251 				struct sockaddr_in sin;
252 
253 				in6_sin6_2_sin(&sin, sin6);
254 				t = in_pcblookup_local(pcbinfo, sin.sin_addr,
255 						       lport, wild);
256 				if (t && t->inp_vflag & INP_TIMEWAIT) {
257 					if ((reuseport &
258 					    intotw(t)->tw_so_options) == 0 &&
259 					    (ntohl(t->inp_laddr.s_addr) !=
260 					     INADDR_ANY || ((inp->inp_vflag &
261 					     INP_IPV6PROTO) ==
262 					     (t->inp_vflag & INP_IPV6PROTO))))
263 						return (EADDRINUSE);
264 				}
265 				else if (t &&
266 				    (reuseport & t->inp_socket->so_options)
267 				    == 0 && (ntohl(t->inp_laddr.s_addr) !=
268 				    INADDR_ANY || INP_SOCKAF(so) ==
269 				     INP_SOCKAF(t->inp_socket)))
270 					return (EADDRINUSE);
271 			}
272 		}
273 		inp->in6p_laddr = sin6->sin6_addr;
274 	}
275 	if (lport == 0) {
276 		int e;
277 		if ((e = in6_pcbsetport(&inp->in6p_laddr, inp, td)) != 0)
278 			return(e);
279 	}
280 	else {
281 		inp->inp_lport = lport;
282 		if (in_pcbinshash(inp) != 0) {
283 			inp->in6p_laddr = in6addr_any;
284 			inp->inp_lport = 0;
285 			return (EAGAIN);
286 		}
287 	}
288 	return(0);
289 }
290 
291 /*
292  *   Transform old in6_pcbconnect() into an inner subroutine for new
293  *   in6_pcbconnect(): Do some validity-checking on the remote
294  *   address (in mbuf 'nam') and then determine local host address
295  *   (i.e., which interface) to use to access that remote host.
296  *
297  *   This preserves definition of in6_pcbconnect(), while supporting a
298  *   slightly different version for T/TCP.  (This is more than
299  *   a bit of a kludge, but cleaning up the internal interfaces would
300  *   have forced minor changes in every protocol).
301  */
302 
303 int
304 in6_pcbladdr(inp, nam, plocal_addr6)
305 	register struct inpcb *inp;
306 	struct sockaddr *nam;
307 	struct in6_addr **plocal_addr6;
308 {
309 	register struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)nam;
310 	struct ifnet *ifp = NULL;
311 	int error = 0;
312 
313 	if (nam->sa_len != sizeof (*sin6))
314 		return (EINVAL);
315 	if (sin6->sin6_family != AF_INET6)
316 		return (EAFNOSUPPORT);
317 	if (sin6->sin6_port == 0)
318 		return (EADDRNOTAVAIL);
319 
320 	/* KAME hack: embed scopeid */
321 	if (in6_embedscope(&sin6->sin6_addr, sin6, inp, &ifp) != 0)
322 		return EINVAL;
323 
324 	if (in6_ifaddr) {
325 		/*
326 		 * If the destination address is UNSPECIFIED addr,
327 		 * use the loopback addr, e.g ::1.
328 		 */
329 		if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr))
330 			sin6->sin6_addr = in6addr_loopback;
331 	}
332 	{
333 		/*
334 		 * XXX: in6_selectsrc might replace the bound local address
335 		 * with the address specified by setsockopt(IPV6_PKTINFO).
336 		 * Is it the intended behavior?
337 		 */
338 		*plocal_addr6 = in6_selectsrc(sin6, inp->in6p_outputopts,
339 					      inp->in6p_moptions,
340 					      &inp->in6p_route,
341 					      &inp->in6p_laddr, &error);
342 		if (*plocal_addr6 == 0) {
343 			if (error == 0)
344 				error = EADDRNOTAVAIL;
345 			return(error);
346 		}
347 		/*
348 		 * Don't do pcblookup call here; return interface in
349 		 * plocal_addr6
350 		 * and exit to caller, that will do the lookup.
351 		 */
352 	}
353 
354 	if (inp->in6p_route.ro_rt)
355 		ifp = inp->in6p_route.ro_rt->rt_ifp;
356 
357 	return(0);
358 }
359 
360 /*
361  * Outer subroutine:
362  * Connect from a socket to a specified address.
363  * Both address and port must be specified in argument sin.
364  * If don't have a local address for this socket yet,
365  * then pick one.
366  */
367 int
368 in6_pcbconnect(inp, nam, td)
369 	register struct inpcb *inp;
370 	struct sockaddr *nam;
371 	struct thread *td;
372 {
373 	struct in6_addr *addr6;
374 	register struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)nam;
375 	int error;
376 
377 	/*
378 	 * Call inner routine, to assign local interface address.
379 	 * in6_pcbladdr() may automatically fill in sin6_scope_id.
380 	 */
381 	if ((error = in6_pcbladdr(inp, nam, &addr6)) != 0)
382 		return(error);
383 
384 	if (in6_pcblookup_hash(inp->inp_pcbinfo, &sin6->sin6_addr,
385 			       sin6->sin6_port,
386 			      IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)
387 			      ? addr6 : &inp->in6p_laddr,
388 			      inp->inp_lport, 0, NULL) != NULL) {
389 		return (EADDRINUSE);
390 	}
391 	if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)) {
392 		if (inp->inp_lport == 0) {
393 			error = in6_pcbbind(inp, (struct sockaddr *)0, td);
394 			if (error)
395 				return (error);
396 		}
397 		inp->in6p_laddr = *addr6;
398 	}
399 	inp->in6p_faddr = sin6->sin6_addr;
400 	inp->inp_fport = sin6->sin6_port;
401 	/* update flowinfo - draft-itojun-ipv6-flowlabel-api-00 */
402 	inp->in6p_flowinfo &= ~IPV6_FLOWLABEL_MASK;
403 	if (inp->in6p_flags & IN6P_AUTOFLOWLABEL)
404 		inp->in6p_flowinfo |=
405 		    (htonl(ip6_flow_seq++) & IPV6_FLOWLABEL_MASK);
406 
407 	in_pcbrehash(inp);
408 	return (0);
409 }
410 
411 #if 0
412 /*
413  * Return an IPv6 address, which is the most appropriate for given
414  * destination and user specified options.
415  * If necessary, this function lookups the routing table and return
416  * an entry to the caller for later use.
417  */
418 struct in6_addr *
419 in6_selectsrc(dstsock, opts, mopts, ro, laddr, errorp)
420 	struct sockaddr_in6 *dstsock;
421 	struct ip6_pktopts *opts;
422 	struct ip6_moptions *mopts;
423 	struct route_in6 *ro;
424 	struct in6_addr *laddr;
425 	int *errorp;
426 {
427 	struct in6_addr *dst;
428 	struct in6_ifaddr *ia6 = 0;
429 	struct in6_pktinfo *pi = NULL;
430 
431 	dst = &dstsock->sin6_addr;
432 	*errorp = 0;
433 
434 	/*
435 	 * If the source address is explicitly specified by the caller,
436 	 * use it.
437 	 */
438 	if (opts && (pi = opts->ip6po_pktinfo) &&
439 	    !IN6_IS_ADDR_UNSPECIFIED(&pi->ipi6_addr))
440 		return(&pi->ipi6_addr);
441 
442 	/*
443 	 * If the source address is not specified but the socket(if any)
444 	 * is already bound, use the bound address.
445 	 */
446 	if (laddr && !IN6_IS_ADDR_UNSPECIFIED(laddr))
447 		return(laddr);
448 
449 	/*
450 	 * If the caller doesn't specify the source address but
451 	 * the outgoing interface, use an address associated with
452 	 * the interface.
453 	 */
454 	if (pi && pi->ipi6_ifindex) {
455 		/* XXX boundary check is assumed to be already done. */
456 		ia6 = in6_ifawithscope(ifnet_byindex(pi->ipi6_ifindex), dst);
457 		if (ia6 == 0) {
458 			*errorp = EADDRNOTAVAIL;
459 			return(0);
460 		}
461 		return(&satosin6(&ia6->ia_addr)->sin6_addr);
462 	}
463 
464 	/*
465 	 * If the destination address is a link-local unicast address or
466 	 * a multicast address, and if the outgoing interface is specified
467 	 * by the sin6_scope_id filed, use an address associated with the
468 	 * interface.
469 	 * XXX: We're now trying to define more specific semantics of
470 	 *      sin6_scope_id field, so this part will be rewritten in
471 	 *      the near future.
472 	 */
473 	if ((IN6_IS_ADDR_LINKLOCAL(dst) || IN6_IS_ADDR_MULTICAST(dst)) &&
474 	    dstsock->sin6_scope_id) {
475 		/*
476 		 * I'm not sure if boundary check for scope_id is done
477 		 * somewhere...
478 		 */
479 		if (dstsock->sin6_scope_id < 0 ||
480 		    if_index < dstsock->sin6_scope_id) {
481 			*errorp = ENXIO; /* XXX: better error? */
482 			return(0);
483 		}
484 		ia6 = in6_ifawithscope(ifnet_byindex(dstsock->sin6_scope_id),
485 				       dst);
486 		if (ia6 == 0) {
487 			*errorp = EADDRNOTAVAIL;
488 			return(0);
489 		}
490 		return(&satosin6(&ia6->ia_addr)->sin6_addr);
491 	}
492 
493 	/*
494 	 * If the destination address is a multicast address and
495 	 * the outgoing interface for the address is specified
496 	 * by the caller, use an address associated with the interface.
497 	 * There is a sanity check here; if the destination has node-local
498 	 * scope, the outgoing interfacde should be a loopback address.
499 	 * Even if the outgoing interface is not specified, we also
500 	 * choose a loopback interface as the outgoing interface.
501 	 */
502 	if (IN6_IS_ADDR_MULTICAST(dst)) {
503 		struct ifnet *ifp = mopts ? mopts->im6o_multicast_ifp : NULL;
504 
505 		if (ifp == NULL && IN6_IS_ADDR_MC_NODELOCAL(dst)) {
506 			ifp = &loif[0];
507 		}
508 
509 		if (ifp) {
510 			ia6 = in6_ifawithscope(ifp, dst);
511 			if (ia6 == 0) {
512 				*errorp = EADDRNOTAVAIL;
513 				return(0);
514 			}
515 			return(&ia6->ia_addr.sin6_addr);
516 		}
517 	}
518 
519 	/*
520 	 * If the next hop address for the packet is specified
521 	 * by caller, use an address associated with the route
522 	 * to the next hop.
523 	 */
524 	{
525 		struct sockaddr_in6 *sin6_next;
526 		struct rtentry *rt;
527 
528 		if (opts && opts->ip6po_nexthop) {
529 			sin6_next = satosin6(opts->ip6po_nexthop);
530 			rt = nd6_lookup(&sin6_next->sin6_addr, 1, NULL);
531 			if (rt) {
532 				ia6 = in6_ifawithscope(rt->rt_ifp, dst);
533 				if (ia6 == 0)
534 					ia6 = ifatoia6(rt->rt_ifa);
535 			}
536 			if (ia6 == 0) {
537 				*errorp = EADDRNOTAVAIL;
538 				return(0);
539 			}
540 			return(&satosin6(&ia6->ia_addr)->sin6_addr);
541 		}
542 	}
543 
544 	/*
545 	 * If route is known or can be allocated now,
546 	 * our src addr is taken from the i/f, else punt.
547 	 */
548 	if (ro) {
549 		if (ro->ro_rt &&
550 		    !IN6_ARE_ADDR_EQUAL(&satosin6(&ro->ro_dst)->sin6_addr, dst)) {
551 			RTFREE(ro->ro_rt);
552 			ro->ro_rt = (struct rtentry *)0;
553 		}
554 		if (ro->ro_rt == (struct rtentry *)0 ||
555 		    ro->ro_rt->rt_ifp == (struct ifnet *)0) {
556 			struct sockaddr_in6 *dst6;
557 
558 			/* No route yet, so try to acquire one */
559 			bzero(&ro->ro_dst, sizeof(struct sockaddr_in6));
560 			dst6 = (struct sockaddr_in6 *)&ro->ro_dst;
561 			dst6->sin6_family = AF_INET6;
562 			dst6->sin6_len = sizeof(struct sockaddr_in6);
563 			dst6->sin6_addr = *dst;
564 			if (IN6_IS_ADDR_MULTICAST(dst)) {
565 				ro->ro_rt = rtalloc1(&((struct route *)ro)
566 						     ->ro_dst, 0, 0UL);
567 			} else {
568 				rtalloc((struct route *)ro);
569 			}
570 		}
571 
572 		/*
573 		 * in_pcbconnect() checks out IFF_LOOPBACK to skip using
574 		 * the address. But we don't know why it does so.
575 		 * It is necessary to ensure the scope even for lo0
576 		 * so doesn't check out IFF_LOOPBACK.
577 		 */
578 
579 		if (ro->ro_rt) {
580 			ia6 = in6_ifawithscope(ro->ro_rt->rt_ifa->ifa_ifp, dst);
581 			if (ia6 == 0) /* xxx scope error ?*/
582 				ia6 = ifatoia6(ro->ro_rt->rt_ifa);
583 		}
584 		if (ia6 == 0) {
585 			*errorp = EHOSTUNREACH;	/* no route */
586 			return(0);
587 		}
588 		return(&satosin6(&ia6->ia_addr)->sin6_addr);
589 	}
590 
591 	*errorp = EADDRNOTAVAIL;
592 	return(0);
593 }
594 
595 /*
596  * Default hop limit selection. The precedence is as follows:
597  * 1. Hoplimit valued specified via ioctl.
598  * 2. (If the outgoing interface is detected) the current
599  *     hop limit of the interface specified by router advertisement.
600  * 3. The system default hoplimit.
601 */
602 int
603 in6_selecthlim(in6p, ifp)
604 	struct in6pcb *in6p;
605 	struct ifnet *ifp;
606 {
607 	if (in6p && in6p->in6p_hops >= 0)
608 		return(in6p->in6p_hops);
609 	else if (ifp)
610 		return(nd_ifinfo[ifp->if_index].chlim);
611 	else
612 		return(ip6_defhlim);
613 }
614 #endif
615 
616 void
617 in6_pcbdisconnect(inp)
618 	struct inpcb *inp;
619 {
620 	bzero((caddr_t)&inp->in6p_faddr, sizeof(inp->in6p_faddr));
621 	inp->inp_fport = 0;
622 	/* clear flowinfo - draft-itojun-ipv6-flowlabel-api-00 */
623 	inp->in6p_flowinfo &= ~IPV6_FLOWLABEL_MASK;
624 	in_pcbrehash(inp);
625 	if (inp->inp_socket->so_state & SS_NOFDREF)
626 		in6_pcbdetach(inp);
627 }
628 
629 void
630 in6_pcbdetach(inp)
631 	struct inpcb *inp;
632 {
633 	struct socket *so = inp->inp_socket;
634 	struct inpcbinfo *ipi = inp->inp_pcbinfo;
635 
636 #ifdef IPSEC
637 	if (inp->in6p_sp != NULL)
638 		ipsec6_delete_pcbpolicy(inp);
639 #endif /* IPSEC */
640 	inp->inp_gencnt = ++ipi->ipi_gencnt;
641 	in_pcbremlists(inp);
642 	if (so) {
643 		so->so_pcb = NULL;
644 		sotryfree(so);
645 	}
646 	if (inp->in6p_options)
647 		m_freem(inp->in6p_options);
648  	ip6_freepcbopts(inp->in6p_outputopts);
649  	ip6_freemoptions(inp->in6p_moptions);
650 	if (inp->in6p_route.ro_rt)
651 		rtfree(inp->in6p_route.ro_rt);
652 	/* Check and free IPv4 related resources in case of mapped addr */
653 	if (inp->inp_options)
654 		(void)m_free(inp->inp_options);
655 	ip_freemoptions(inp->inp_moptions);
656 	inp->inp_vflag = 0;
657 	INP_LOCK_DESTROY(inp);
658 	uma_zfree(ipi->ipi_zone, inp);
659 }
660 
661 struct sockaddr *
662 in6_sockaddr(port, addr_p)
663 	in_port_t port;
664 	struct in6_addr *addr_p;
665 {
666 	struct sockaddr_in6 *sin6;
667 
668 	MALLOC(sin6, struct sockaddr_in6 *, sizeof *sin6, M_SONAME, M_WAITOK);
669 	bzero(sin6, sizeof *sin6);
670 	sin6->sin6_family = AF_INET6;
671 	sin6->sin6_len = sizeof(*sin6);
672 	sin6->sin6_port = port;
673 	sin6->sin6_addr = *addr_p;
674 	if (IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr))
675 		sin6->sin6_scope_id = ntohs(sin6->sin6_addr.s6_addr16[1]);
676 	else
677 		sin6->sin6_scope_id = 0;	/*XXX*/
678 	if (IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr))
679 		sin6->sin6_addr.s6_addr16[1] = 0;
680 
681 	return (struct sockaddr *)sin6;
682 }
683 
684 struct sockaddr *
685 in6_v4mapsin6_sockaddr(port, addr_p)
686 	in_port_t port;
687 	struct in_addr *addr_p;
688 {
689 	struct sockaddr_in sin;
690 	struct sockaddr_in6 *sin6_p;
691 
692 	bzero(&sin, sizeof sin);
693 	sin.sin_family = AF_INET;
694 	sin.sin_len = sizeof(sin);
695 	sin.sin_port = port;
696 	sin.sin_addr = *addr_p;
697 
698 	MALLOC(sin6_p, struct sockaddr_in6 *, sizeof *sin6_p, M_SONAME,
699 		M_WAITOK);
700 	in6_sin_2_v4mapsin6(&sin, sin6_p);
701 
702 	return (struct sockaddr *)sin6_p;
703 }
704 
705 /*
706  * The calling convention of in6_setsockaddr() and in6_setpeeraddr() was
707  * modified to match the pru_sockaddr() and pru_peeraddr() entry points
708  * in struct pr_usrreqs, so that protocols can just reference then directly
709  * without the need for a wrapper function.  The socket must have a valid
710  * (i.e., non-nil) PCB, but it should be impossible to get an invalid one
711  * except through a kernel programming error, so it is acceptable to panic
712  * (or in this case trap) if the PCB is invalid.  (Actually, we don't trap
713  * because there actually /is/ a programming error somewhere... XXX)
714  */
715 int
716 in6_setsockaddr(so, nam)
717 	struct socket *so;
718 	struct sockaddr **nam;
719 {
720 	int s;
721 	register struct inpcb *inp;
722 	struct in6_addr addr;
723 	in_port_t port;
724 
725 	s = splnet();
726 	inp = sotoinpcb(so);
727 	if (!inp) {
728 		splx(s);
729 		return EINVAL;
730 	}
731 	port = inp->inp_lport;
732 	addr = inp->in6p_laddr;
733 	splx(s);
734 
735 	*nam = in6_sockaddr(port, &addr);
736 	return 0;
737 }
738 
739 int
740 in6_setpeeraddr(so, nam)
741 	struct socket *so;
742 	struct sockaddr **nam;
743 {
744 	int s;
745 	struct inpcb *inp;
746 	struct in6_addr addr;
747 	in_port_t port;
748 
749 	s = splnet();
750 	inp = sotoinpcb(so);
751 	if (!inp) {
752 		splx(s);
753 		return EINVAL;
754 	}
755 	port = inp->inp_fport;
756 	addr = inp->in6p_faddr;
757 	splx(s);
758 
759 	*nam = in6_sockaddr(port, &addr);
760 	return 0;
761 }
762 
763 int
764 in6_mapped_sockaddr(struct socket *so, struct sockaddr **nam)
765 {
766 	struct	inpcb *inp = sotoinpcb(so);
767 	int	error;
768 
769 	if (inp == NULL)
770 		return EINVAL;
771 	if (inp->inp_vflag & INP_IPV4) {
772 		error = in_setsockaddr(so, nam, &tcbinfo);
773 		if (error == 0)
774 			in6_sin_2_v4mapsin6_in_sock(nam);
775 	} else
776 	/* scope issues will be handled in in6_setsockaddr(). */
777 	error = in6_setsockaddr(so, nam);
778 
779 	return error;
780 }
781 
782 int
783 in6_mapped_peeraddr(struct socket *so, struct sockaddr **nam)
784 {
785 	struct	inpcb *inp = sotoinpcb(so);
786 	int	error;
787 
788 	if (inp == NULL)
789 		return EINVAL;
790 	if (inp->inp_vflag & INP_IPV4) {
791 		error = in_setpeeraddr(so, nam, &tcbinfo);
792 		if (error == 0)
793 			in6_sin_2_v4mapsin6_in_sock(nam);
794 	} else
795 	/* scope issues will be handled in in6_setpeeraddr(). */
796 	error = in6_setpeeraddr(so, nam);
797 
798 	return error;
799 }
800 
801 /*
802  * Pass some notification to all connections of a protocol
803  * associated with address dst.  The local address and/or port numbers
804  * may be specified to limit the search.  The "usual action" will be
805  * taken, depending on the ctlinput cmd.  The caller must filter any
806  * cmds that are uninteresting (e.g., no error in the map).
807  * Call the protocol specific routine (if any) to report
808  * any errors for each matching socket.
809  *
810  * Must be called at splnet.
811  */
812 void
813 in6_pcbnotify(head, dst, fport_arg, src, lport_arg, cmd, notify)
814 	struct inpcbhead *head;
815 	struct sockaddr *dst;
816 	const struct sockaddr *src;
817 	u_int fport_arg, lport_arg;
818 	int cmd;
819 	struct inpcb *(*notify) __P((struct inpcb *, int));
820 {
821 	struct inpcb *inp, *ninp;
822 	struct sockaddr_in6 sa6_src, *sa6_dst;
823 	u_short	fport = fport_arg, lport = lport_arg;
824 	u_int32_t flowinfo;
825 	int errno, s;
826 
827 	if ((unsigned)cmd >= PRC_NCMDS || dst->sa_family != AF_INET6)
828 		return;
829 
830 	sa6_dst = (struct sockaddr_in6 *)dst;
831 	if (IN6_IS_ADDR_UNSPECIFIED(&sa6_dst->sin6_addr))
832 		return;
833 
834 	/*
835 	 * note that src can be NULL when we get notify by local fragmentation.
836 	 */
837 	sa6_src = (src == NULL) ? sa6_any : *(const struct sockaddr_in6 *)src;
838 	flowinfo = sa6_src.sin6_flowinfo;
839 
840 	/*
841 	 * Redirects go to all references to the destination,
842 	 * and use in6_rtchange to invalidate the route cache.
843 	 * Dead host indications: also use in6_rtchange to invalidate
844 	 * the cache, and deliver the error to all the sockets.
845 	 * Otherwise, if we have knowledge of the local port and address,
846 	 * deliver only to that socket.
847 	 */
848 	if (PRC_IS_REDIRECT(cmd) || cmd == PRC_HOSTDEAD) {
849 		fport = 0;
850 		lport = 0;
851 		bzero((caddr_t)&sa6_src.sin6_addr, sizeof(sa6_src.sin6_addr));
852 
853 		if (cmd != PRC_HOSTDEAD)
854 			notify = in6_rtchange;
855 	}
856 	errno = inet6ctlerrmap[cmd];
857 	s = splnet();
858  	for (inp = LIST_FIRST(head); inp != NULL; inp = ninp) {
859  		ninp = LIST_NEXT(inp, inp_list);
860 
861  		if ((inp->inp_vflag & INP_IPV6) == 0)
862 			continue;
863 
864 		/*
865 		 * Detect if we should notify the error. If no source and
866 		 * destination ports are specifed, but non-zero flowinfo and
867 		 * local address match, notify the error. This is the case
868 		 * when the error is delivered with an encrypted buffer
869 		 * by ESP. Otherwise, just compare addresses and ports
870 		 * as usual.
871 		 */
872 		if (lport == 0 && fport == 0 && flowinfo &&
873 		    inp->inp_socket != NULL &&
874 		    flowinfo == (inp->in6p_flowinfo & IPV6_FLOWLABEL_MASK) &&
875 		    IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, &sa6_src.sin6_addr))
876 			goto do_notify;
877 		else if (!IN6_ARE_ADDR_EQUAL(&inp->in6p_faddr,
878 					     &sa6_dst->sin6_addr) ||
879 			 inp->inp_socket == 0 ||
880 			 (lport && inp->inp_lport != lport) ||
881 			 (!IN6_IS_ADDR_UNSPECIFIED(&sa6_src.sin6_addr) &&
882 			  !IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr,
883 					      &sa6_src.sin6_addr)) ||
884 			 (fport && inp->inp_fport != fport))
885 			continue;
886 
887 	  do_notify:
888 		if (notify)
889 			(*notify)(inp, errno);
890 	}
891 	splx(s);
892 }
893 
894 /*
895  * Lookup a PCB based on the local address and port.
896  */
897 struct inpcb *
898 in6_pcblookup_local(pcbinfo, laddr, lport_arg, wild_okay)
899 	struct inpcbinfo *pcbinfo;
900 	struct in6_addr *laddr;
901 	u_int lport_arg;
902 	int wild_okay;
903 {
904 	register struct inpcb *inp;
905 	int matchwild = 3, wildcard;
906 	u_short lport = lport_arg;
907 
908 	if (!wild_okay) {
909 		struct inpcbhead *head;
910 		/*
911 		 * Look for an unconnected (wildcard foreign addr) PCB that
912 		 * matches the local address and port we're looking for.
913 		 */
914 		head = &pcbinfo->hashbase[INP_PCBHASH(INADDR_ANY, lport, 0,
915 						      pcbinfo->hashmask)];
916 		LIST_FOREACH(inp, head, inp_hash) {
917 			if ((inp->inp_vflag & INP_IPV6) == 0)
918 				continue;
919 			if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr) &&
920 			    IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, laddr) &&
921 			    inp->inp_lport == lport) {
922 				/*
923 				 * Found.
924 				 */
925 				return (inp);
926 			}
927 		}
928 		/*
929 		 * Not found.
930 		 */
931 		return (NULL);
932 	} else {
933 		struct inpcbporthead *porthash;
934 		struct inpcbport *phd;
935 		struct inpcb *match = NULL;
936 		/*
937 		 * Best fit PCB lookup.
938 		 *
939 		 * First see if this local port is in use by looking on the
940 		 * port hash list.
941 		 */
942 		porthash = &pcbinfo->porthashbase[INP_PCBPORTHASH(lport,
943 		    pcbinfo->porthashmask)];
944 		LIST_FOREACH(phd, porthash, phd_hash) {
945 			if (phd->phd_port == lport)
946 				break;
947 		}
948 		if (phd != NULL) {
949 			/*
950 			 * Port is in use by one or more PCBs. Look for best
951 			 * fit.
952 			 */
953 			LIST_FOREACH(inp, &phd->phd_pcblist, inp_portlist) {
954 				wildcard = 0;
955 				if ((inp->inp_vflag & INP_IPV6) == 0)
956 					continue;
957 				if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr))
958 					wildcard++;
959 				if (!IN6_IS_ADDR_UNSPECIFIED(
960 					&inp->in6p_laddr)) {
961 					if (IN6_IS_ADDR_UNSPECIFIED(laddr))
962 						wildcard++;
963 					else if (!IN6_ARE_ADDR_EQUAL(
964 						&inp->in6p_laddr, laddr))
965 						continue;
966 				} else {
967 					if (!IN6_IS_ADDR_UNSPECIFIED(laddr))
968 						wildcard++;
969 				}
970 				if (wildcard < matchwild) {
971 					match = inp;
972 					matchwild = wildcard;
973 					if (matchwild == 0) {
974 						break;
975 					}
976 				}
977 			}
978 		}
979 		return (match);
980 	}
981 }
982 
983 void
984 in6_pcbpurgeif0(head, ifp)
985 	struct in6pcb *head;
986 	struct ifnet *ifp;
987 {
988 	struct in6pcb *in6p;
989 	struct ip6_moptions *im6o;
990 	struct in6_multi_mship *imm, *nimm;
991 
992 	for (in6p = head; in6p != NULL; in6p = LIST_NEXT(in6p, inp_list)) {
993 		im6o = in6p->in6p_moptions;
994 		if ((in6p->inp_vflag & INP_IPV6) &&
995 		    im6o) {
996 			/*
997 			 * Unselect the outgoing interface if it is being
998 			 * detached.
999 			 */
1000 			if (im6o->im6o_multicast_ifp == ifp)
1001 				im6o->im6o_multicast_ifp = NULL;
1002 
1003 			/*
1004 			 * Drop multicast group membership if we joined
1005 			 * through the interface being detached.
1006 			 * XXX controversial - is it really legal for kernel
1007 			 * to force this?
1008 			 */
1009 			for (imm = im6o->im6o_memberships.lh_first;
1010 			     imm != NULL; imm = nimm) {
1011 				nimm = imm->i6mm_chain.le_next;
1012 				if (imm->i6mm_maddr->in6m_ifp == ifp) {
1013 					LIST_REMOVE(imm, i6mm_chain);
1014 					in6_delmulti(imm->i6mm_maddr);
1015 					free(imm, M_IPMADDR);
1016 				}
1017 			}
1018 		}
1019 	}
1020 }
1021 
1022 /*
1023  * Check for alternatives when higher level complains
1024  * about service problems.  For now, invalidate cached
1025  * routing information.  If the route was created dynamically
1026  * (by a redirect), time to try a default gateway again.
1027  */
1028 void
1029 in6_losing(in6p)
1030 	struct inpcb *in6p;
1031 {
1032 	struct rtentry *rt;
1033 	struct rt_addrinfo info;
1034 
1035 	if ((rt = in6p->in6p_route.ro_rt) != NULL) {
1036 		bzero((caddr_t)&info, sizeof(info));
1037 		info.rti_flags = rt->rt_flags;
1038 		info.rti_info[RTAX_DST] = rt_key(rt);
1039 		info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
1040 		info.rti_info[RTAX_NETMASK] = rt_mask(rt);
1041 		rt_missmsg(RTM_LOSING, &info, rt->rt_flags, 0);
1042 		if (rt->rt_flags & RTF_DYNAMIC)
1043 			(void)rtrequest1(RTM_DELETE, &info, NULL);
1044 		in6p->in6p_route.ro_rt = NULL;
1045 		rtfree(rt);
1046 		/*
1047 		 * A new route can be allocated
1048 		 * the next time output is attempted.
1049 		 */
1050 	}
1051 }
1052 
1053 /*
1054  * After a routing change, flush old routing
1055  * and allocate a (hopefully) better one.
1056  */
1057 struct inpcb *
1058 in6_rtchange(inp, errno)
1059 	struct inpcb *inp;
1060 	int errno;
1061 {
1062 	if (inp->in6p_route.ro_rt) {
1063 		rtfree(inp->in6p_route.ro_rt);
1064 		inp->in6p_route.ro_rt = 0;
1065 		/*
1066 		 * A new route can be allocated the next time
1067 		 * output is attempted.
1068 		 */
1069 	}
1070 	return inp;
1071 }
1072 
1073 /*
1074  * Lookup PCB in hash list.
1075  */
1076 struct inpcb *
1077 in6_pcblookup_hash(pcbinfo, faddr, fport_arg, laddr, lport_arg, wildcard, ifp)
1078 	struct inpcbinfo *pcbinfo;
1079 	struct in6_addr *faddr, *laddr;
1080 	u_int fport_arg, lport_arg;
1081 	int wildcard;
1082 	struct ifnet *ifp;
1083 {
1084 	struct inpcbhead *head;
1085 	register struct inpcb *inp;
1086 	u_short fport = fport_arg, lport = lport_arg;
1087 	int faith;
1088 
1089 	if (faithprefix_p != NULL)
1090 		faith = (*faithprefix_p)(laddr);
1091 	else
1092 		faith = 0;
1093 
1094 	/*
1095 	 * First look for an exact match.
1096 	 */
1097 	head = &pcbinfo->hashbase[INP_PCBHASH(faddr->s6_addr32[3] /* XXX */,
1098 					      lport, fport,
1099 					      pcbinfo->hashmask)];
1100 	LIST_FOREACH(inp, head, inp_hash) {
1101 		if ((inp->inp_vflag & INP_IPV6) == 0)
1102 			continue;
1103 		if (IN6_ARE_ADDR_EQUAL(&inp->in6p_faddr, faddr) &&
1104 		    IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, laddr) &&
1105 		    inp->inp_fport == fport &&
1106 		    inp->inp_lport == lport) {
1107 			/*
1108 			 * Found.
1109 			 */
1110 			return (inp);
1111 		}
1112 	}
1113 	if (wildcard) {
1114 		struct inpcb *local_wild = NULL;
1115 
1116 		head = &pcbinfo->hashbase[INP_PCBHASH(INADDR_ANY, lport, 0,
1117 						      pcbinfo->hashmask)];
1118 		LIST_FOREACH(inp, head, inp_hash) {
1119 			if ((inp->inp_vflag & INP_IPV6) == 0)
1120 				continue;
1121 			if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr) &&
1122 			    inp->inp_lport == lport) {
1123 				if (faith && (inp->inp_flags & INP_FAITH) == 0)
1124 					continue;
1125 				if (IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr,
1126 						       laddr))
1127 					return (inp);
1128 				else if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr))
1129 					local_wild = inp;
1130 			}
1131 		}
1132 		return (local_wild);
1133 	}
1134 
1135 	/*
1136 	 * Not found.
1137 	 */
1138 	return (NULL);
1139 }
1140 
1141 void
1142 init_sin6(struct sockaddr_in6 *sin6, struct mbuf *m)
1143 {
1144 	struct ip6_hdr *ip;
1145 
1146 	ip = mtod(m, struct ip6_hdr *);
1147 	bzero(sin6, sizeof(*sin6));
1148 	sin6->sin6_len = sizeof(*sin6);
1149 	sin6->sin6_family = AF_INET6;
1150 	sin6->sin6_addr = ip->ip6_src;
1151 	if (IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr))
1152 		sin6->sin6_addr.s6_addr16[1] = 0;
1153 	sin6->sin6_scope_id =
1154 		(m->m_pkthdr.rcvif && IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr))
1155 		? m->m_pkthdr.rcvif->if_index : 0;
1156 
1157 	return;
1158 }
1159