xref: /freebsd/sys/netinet6/in6_pcb.c (revision cec50dea12481dc578c0805c887ab2097e1c06c5)
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  * 4. Neither the name of the University nor the names of its contributors
47  *    may be used to endorse or promote products derived from this software
48  *    without specific prior written permission.
49  *
50  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
51  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
52  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
53  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
54  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
55  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
56  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
57  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
58  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
59  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
60  * SUCH DAMAGE.
61  *
62  *	@(#)in_pcb.c	8.2 (Berkeley) 1/4/94
63  */
64 
65 #include "opt_inet.h"
66 #include "opt_inet6.h"
67 #include "opt_ipsec.h"
68 
69 #include <sys/param.h>
70 #include <sys/systm.h>
71 #include <sys/malloc.h>
72 #include <sys/mbuf.h>
73 #include <sys/domain.h>
74 #include <sys/protosw.h>
75 #include <sys/socket.h>
76 #include <sys/socketvar.h>
77 #include <sys/sockio.h>
78 #include <sys/errno.h>
79 #include <sys/time.h>
80 #include <sys/proc.h>
81 #include <sys/jail.h>
82 
83 #include <vm/uma.h>
84 
85 #include <net/if.h>
86 #include <net/if_types.h>
87 #include <net/route.h>
88 
89 #include <netinet/in.h>
90 #include <netinet/in_var.h>
91 #include <netinet/in_systm.h>
92 #include <netinet/tcp_var.h>
93 #include <netinet/ip6.h>
94 #include <netinet/ip_var.h>
95 #include <netinet6/ip6_var.h>
96 #include <netinet6/nd6.h>
97 #include <netinet/in_pcb.h>
98 #include <netinet6/in6_pcb.h>
99 
100 #ifdef IPSEC
101 #include <netinet6/ipsec.h>
102 #ifdef INET6
103 #include <netinet6/ipsec6.h>
104 #endif
105 #include <netinet6/ah.h>
106 #ifdef INET6
107 #include <netinet6/ah6.h>
108 #endif
109 #include <netkey/key.h>
110 #endif /* IPSEC */
111 
112 #ifdef FAST_IPSEC
113 #include <netipsec/ipsec.h>
114 #include <netipsec/ipsec6.h>
115 #include <netipsec/key.h>
116 #endif /* FAST_IPSEC */
117 
118 struct	in6_addr zeroin6_addr;
119 
120 int
121 in6_pcbbind(inp, nam, cred)
122 	register struct inpcb *inp;
123 	struct sockaddr *nam;
124 	struct ucred *cred;
125 {
126 	struct socket *so = inp->inp_socket;
127 	struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)NULL;
128 	struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
129 	u_short	lport = 0;
130 	int wild = 0, reuseport = (so->so_options & SO_REUSEPORT);
131 
132 	INP_INFO_WLOCK_ASSERT(pcbinfo);
133 	INP_LOCK_ASSERT(inp);
134 
135 	if (!in6_ifaddr) /* XXX broken! */
136 		return (EADDRNOTAVAIL);
137 	if (inp->inp_lport || !IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr))
138 		return (EINVAL);
139 	if ((so->so_options & (SO_REUSEADDR|SO_REUSEPORT)) == 0)
140 		wild = 1;
141 	if (nam) {
142 		sin6 = (struct sockaddr_in6 *)nam;
143 		if (nam->sa_len != sizeof(*sin6))
144 			return (EINVAL);
145 		/*
146 		 * family check.
147 		 */
148 		if (nam->sa_family != AF_INET6)
149 			return (EAFNOSUPPORT);
150 
151 		/* KAME hack: embed scopeid */
152 		if (in6_embedscope(&sin6->sin6_addr, sin6, inp, NULL) != 0)
153 			return EINVAL;
154 		/* this must be cleared for ifa_ifwithaddr() */
155 		sin6->sin6_scope_id = 0;
156 
157 		lport = sin6->sin6_port;
158 		if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) {
159 			/*
160 			 * Treat SO_REUSEADDR as SO_REUSEPORT for multicast;
161 			 * allow compepte duplication of binding if
162 			 * SO_REUSEPORT is set, or if SO_REUSEADDR is set
163 			 * and a multicast address is bound on both
164 			 * new and duplicated sockets.
165 			 */
166 			if (so->so_options & SO_REUSEADDR)
167 				reuseport = SO_REUSEADDR|SO_REUSEPORT;
168 		} else if (!IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
169 			struct ifaddr *ia = NULL;
170 
171 			sin6->sin6_port = 0;		/* yech... */
172 			if ((ia = ifa_ifwithaddr((struct sockaddr *)sin6)) == 0)
173 				return (EADDRNOTAVAIL);
174 
175 			/*
176 			 * XXX: bind to an anycast address might accidentally
177 			 * cause sending a packet with anycast source address.
178 			 * We should allow to bind to a deprecated address, since
179 			 * the application dares to use it.
180 			 */
181 			if (ia &&
182 			    ((struct in6_ifaddr *)ia)->ia6_flags &
183 			    (IN6_IFF_ANYCAST|IN6_IFF_NOTREADY|IN6_IFF_DETACHED)) {
184 				return (EADDRNOTAVAIL);
185 			}
186 		}
187 		if (lport) {
188 			struct inpcb *t;
189 
190 			/* GROSS */
191 			if (ntohs(lport) < IPV6PORT_RESERVED &&
192 			    suser_cred(cred, SUSER_ALLOWJAIL))
193 				return (EACCES);
194 			if (so->so_cred->cr_uid != 0 &&
195 			    !IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) {
196 				t = in6_pcblookup_local(pcbinfo,
197 				    &sin6->sin6_addr, lport,
198 				    INPLOOKUP_WILDCARD);
199 				if (t &&
200 				    ((t->inp_vflag & INP_TIMEWAIT) == 0) &&
201 				    (so->so_type != SOCK_STREAM ||
202 				     IN6_IS_ADDR_UNSPECIFIED(&t->in6p_faddr)) &&
203 				    (!IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr) ||
204 			    	     !IN6_IS_ADDR_UNSPECIFIED(&t->in6p_laddr) ||
205 				     (t->inp_socket->so_options & SO_REUSEPORT)
206 				      == 0) && (so->so_cred->cr_uid !=
207 				     t->inp_socket->so_cred->cr_uid))
208 					return (EADDRINUSE);
209 				if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0 &&
210 				    IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
211 					struct sockaddr_in sin;
212 
213 					in6_sin6_2_sin(&sin, sin6);
214 					t = in_pcblookup_local(pcbinfo,
215 						sin.sin_addr, lport,
216 						INPLOOKUP_WILDCARD);
217 					if (t &&
218 					    ((t->inp_vflag &
219 					      INP_TIMEWAIT) == 0) &&
220 					    (so->so_type != SOCK_STREAM ||
221 					     ntohl(t->inp_faddr.s_addr) ==
222 					      INADDR_ANY) &&
223 					    (so->so_cred->cr_uid !=
224 					     t->inp_socket->so_cred->cr_uid))
225 						return (EADDRINUSE);
226 				}
227 			}
228 			t = in6_pcblookup_local(pcbinfo, &sin6->sin6_addr,
229 						lport, wild);
230 			if (t && (reuseport & ((t->inp_vflag & INP_TIMEWAIT) ?
231 			    intotw(t)->tw_so_options :
232 			    t->inp_socket->so_options)) == 0)
233 				return (EADDRINUSE);
234 			if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0 &&
235 			    IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
236 				struct sockaddr_in sin;
237 
238 				in6_sin6_2_sin(&sin, sin6);
239 				t = in_pcblookup_local(pcbinfo, sin.sin_addr,
240 						       lport, wild);
241 				if (t && t->inp_vflag & INP_TIMEWAIT) {
242 					if ((reuseport &
243 					    intotw(t)->tw_so_options) == 0 &&
244 					    (ntohl(t->inp_laddr.s_addr) !=
245 					     INADDR_ANY || ((inp->inp_vflag &
246 					     INP_IPV6PROTO) ==
247 					     (t->inp_vflag & INP_IPV6PROTO))))
248 						return (EADDRINUSE);
249 				}
250 				else if (t &&
251 				    (reuseport & t->inp_socket->so_options)
252 				    == 0 && (ntohl(t->inp_laddr.s_addr) !=
253 				    INADDR_ANY || INP_SOCKAF(so) ==
254 				     INP_SOCKAF(t->inp_socket)))
255 					return (EADDRINUSE);
256 			}
257 		}
258 		inp->in6p_laddr = sin6->sin6_addr;
259 	}
260 	if (lport == 0) {
261 		int e;
262 		if ((e = in6_pcbsetport(&inp->in6p_laddr, inp, cred)) != 0)
263 			return (e);
264 	}
265 	else {
266 		inp->inp_lport = lport;
267 		if (in_pcbinshash(inp) != 0) {
268 			inp->in6p_laddr = in6addr_any;
269 			inp->inp_lport = 0;
270 			return (EAGAIN);
271 		}
272 	}
273 	return (0);
274 }
275 
276 /*
277  *   Transform old in6_pcbconnect() into an inner subroutine for new
278  *   in6_pcbconnect(): Do some validity-checking on the remote
279  *   address (in mbuf 'nam') and then determine local host address
280  *   (i.e., which interface) to use to access that remote host.
281  *
282  *   This preserves definition of in6_pcbconnect(), while supporting a
283  *   slightly different version for T/TCP.  (This is more than
284  *   a bit of a kludge, but cleaning up the internal interfaces would
285  *   have forced minor changes in every protocol).
286  */
287 
288 int
289 in6_pcbladdr(inp, nam, plocal_addr6)
290 	register struct inpcb *inp;
291 	struct sockaddr *nam;
292 	struct in6_addr **plocal_addr6;
293 {
294 	register struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)nam;
295 	struct ifnet *ifp = NULL;
296 	int error = 0;
297 
298 	if (nam->sa_len != sizeof (*sin6))
299 		return (EINVAL);
300 	if (sin6->sin6_family != AF_INET6)
301 		return (EAFNOSUPPORT);
302 	if (sin6->sin6_port == 0)
303 		return (EADDRNOTAVAIL);
304 
305 	INP_INFO_WLOCK_ASSERT(inp->inp_pcbinfo);
306 	INP_LOCK_ASSERT(inp);
307 
308 	/* KAME hack: embed scopeid */
309 	if (in6_embedscope(&sin6->sin6_addr, sin6, inp, &ifp) != 0)
310 		return EINVAL;
311 
312 	if (in6_ifaddr) {
313 		/*
314 		 * If the destination address is UNSPECIFIED addr,
315 		 * use the loopback addr, e.g ::1.
316 		 */
317 		if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr))
318 			sin6->sin6_addr = in6addr_loopback;
319 	}
320 	{
321 		/*
322 		 * XXX: in6_selectsrc might replace the bound local address
323 		 * with the address specified by setsockopt(IPV6_PKTINFO).
324 		 * Is it the intended behavior?
325 		 */
326 		*plocal_addr6 = in6_selectsrc(sin6, inp->in6p_outputopts,
327 					      inp->in6p_moptions, NULL,
328 					      &inp->in6p_laddr, &error);
329 		if (*plocal_addr6 == 0) {
330 			if (error == 0)
331 				error = EADDRNOTAVAIL;
332 			return (error);
333 		}
334 		/*
335 		 * Don't do pcblookup call here; return interface in
336 		 * plocal_addr6
337 		 * and exit to caller, that will do the lookup.
338 		 */
339 	}
340 	return (0);
341 }
342 
343 /*
344  * Outer subroutine:
345  * Connect from a socket to a specified address.
346  * Both address and port must be specified in argument sin.
347  * If don't have a local address for this socket yet,
348  * then pick one.
349  */
350 int
351 in6_pcbconnect(inp, nam, cred)
352 	register struct inpcb *inp;
353 	struct sockaddr *nam;
354 	struct ucred *cred;
355 {
356 	struct in6_addr *addr6;
357 	register struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)nam;
358 	int error;
359 
360 	INP_INFO_WLOCK_ASSERT(inp->inp_pcbinfo);
361 	INP_LOCK_ASSERT(inp);
362 
363 	/*
364 	 * Call inner routine, to assign local interface address.
365 	 * in6_pcbladdr() may automatically fill in sin6_scope_id.
366 	 */
367 	if ((error = in6_pcbladdr(inp, nam, &addr6)) != 0)
368 		return (error);
369 
370 	if (in6_pcblookup_hash(inp->inp_pcbinfo, &sin6->sin6_addr,
371 			       sin6->sin6_port,
372 			      IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)
373 			      ? addr6 : &inp->in6p_laddr,
374 			      inp->inp_lport, 0, NULL) != NULL) {
375 		return (EADDRINUSE);
376 	}
377 	if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)) {
378 		if (inp->inp_lport == 0) {
379 			error = in6_pcbbind(inp, (struct sockaddr *)0, cred);
380 			if (error)
381 				return (error);
382 		}
383 		inp->in6p_laddr = *addr6;
384 	}
385 	inp->in6p_faddr = sin6->sin6_addr;
386 	inp->inp_fport = sin6->sin6_port;
387 	/* update flowinfo - draft-itojun-ipv6-flowlabel-api-00 */
388 	inp->in6p_flowinfo &= ~IPV6_FLOWLABEL_MASK;
389 	if (inp->in6p_flags & IN6P_AUTOFLOWLABEL)
390 		inp->in6p_flowinfo |=
391 		    (htonl(ip6_randomflowlabel()) & IPV6_FLOWLABEL_MASK);
392 
393 	in_pcbrehash(inp);
394 #ifdef IPSEC
395 	if (inp->inp_socket->so_type == SOCK_STREAM)
396 		ipsec_pcbconn(inp->inp_sp);
397 #endif
398 	return (0);
399 }
400 
401 void
402 in6_pcbdisconnect(inp)
403 	struct inpcb *inp;
404 {
405 
406 	INP_INFO_WLOCK_ASSERT(inp->inp_pcbinfo);
407 	INP_LOCK_ASSERT(inp);
408 
409 	bzero((caddr_t)&inp->in6p_faddr, sizeof(inp->in6p_faddr));
410 	inp->inp_fport = 0;
411 	/* clear flowinfo - draft-itojun-ipv6-flowlabel-api-00 */
412 	inp->in6p_flowinfo &= ~IPV6_FLOWLABEL_MASK;
413 	in_pcbrehash(inp);
414 #ifdef IPSEC
415 	ipsec_pcbdisconn(inp->inp_sp);
416 #endif
417 	if (inp->inp_socket->so_state & SS_NOFDREF)
418 		in6_pcbdetach(inp);
419 }
420 
421 void
422 in6_pcbdetach(inp)
423 	struct inpcb *inp;
424 {
425 	struct socket *so = inp->inp_socket;
426 	struct inpcbinfo *ipi = inp->inp_pcbinfo;
427 
428 	INP_INFO_WLOCK_ASSERT(inp->inp_pcbinfo);
429 	INP_LOCK_ASSERT(inp);
430 
431 #if defined(IPSEC) || defined(FAST_IPSEC)
432 	if (inp->in6p_sp != NULL)
433 		ipsec6_delete_pcbpolicy(inp);
434 #endif /* IPSEC */
435 	inp->inp_gencnt = ++ipi->ipi_gencnt;
436 	in_pcbremlists(inp);
437 
438 	if (so) {
439 		SOCK_LOCK(so);
440 		so->so_pcb = NULL;
441 		sotryfree(so);
442 	}
443 
444  	ip6_freepcbopts(inp->in6p_outputopts);
445  	ip6_freemoptions(inp->in6p_moptions);
446 	/* Check and free IPv4 related resources in case of mapped addr */
447 	if (inp->inp_options)
448 		(void)m_free(inp->inp_options);
449 	ip_freemoptions(inp->inp_moptions);
450 	inp->inp_vflag = 0;
451 	INP_LOCK_DESTROY(inp);
452 	uma_zfree(ipi->ipi_zone, inp);
453 }
454 
455 struct sockaddr *
456 in6_sockaddr(port, addr_p)
457 	in_port_t port;
458 	struct in6_addr *addr_p;
459 {
460 	struct sockaddr_in6 *sin6;
461 
462 	MALLOC(sin6, struct sockaddr_in6 *, sizeof *sin6, M_SONAME, M_WAITOK);
463 	bzero(sin6, sizeof *sin6);
464 	sin6->sin6_family = AF_INET6;
465 	sin6->sin6_len = sizeof(*sin6);
466 	sin6->sin6_port = port;
467 	sin6->sin6_addr = *addr_p;
468 	if (IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr))
469 		sin6->sin6_scope_id = ntohs(sin6->sin6_addr.s6_addr16[1]);
470 	else
471 		sin6->sin6_scope_id = 0;	/*XXX*/
472 	if (IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr))
473 		sin6->sin6_addr.s6_addr16[1] = 0;
474 
475 	return (struct sockaddr *)sin6;
476 }
477 
478 struct sockaddr *
479 in6_v4mapsin6_sockaddr(port, addr_p)
480 	in_port_t port;
481 	struct in_addr *addr_p;
482 {
483 	struct sockaddr_in sin;
484 	struct sockaddr_in6 *sin6_p;
485 
486 	bzero(&sin, sizeof sin);
487 	sin.sin_family = AF_INET;
488 	sin.sin_len = sizeof(sin);
489 	sin.sin_port = port;
490 	sin.sin_addr = *addr_p;
491 
492 	MALLOC(sin6_p, struct sockaddr_in6 *, sizeof *sin6_p, M_SONAME,
493 		M_WAITOK);
494 	in6_sin_2_v4mapsin6(&sin, sin6_p);
495 
496 	return (struct sockaddr *)sin6_p;
497 }
498 
499 /*
500  * The calling convention of in6_setsockaddr() and in6_setpeeraddr() was
501  * modified to match the pru_sockaddr() and pru_peeraddr() entry points
502  * in struct pr_usrreqs, so that protocols can just reference then directly
503  * without the need for a wrapper function.  The socket must have a valid
504  * (i.e., non-nil) PCB, but it should be impossible to get an invalid one
505  * except through a kernel programming error, so it is acceptable to panic
506  * (or in this case trap) if the PCB is invalid.  (Actually, we don't trap
507  * because there actually /is/ a programming error somewhere... XXX)
508  */
509 int
510 in6_setsockaddr(so, nam)
511 	struct socket *so;
512 	struct sockaddr **nam;
513 {
514 	int s;
515 	register struct inpcb *inp;
516 	struct in6_addr addr;
517 	in_port_t port;
518 
519 	s = splnet();
520 	inp = sotoinpcb(so);
521 	if (!inp) {
522 		splx(s);
523 		return EINVAL;
524 	}
525 	port = inp->inp_lport;
526 	addr = inp->in6p_laddr;
527 	splx(s);
528 
529 	*nam = in6_sockaddr(port, &addr);
530 	return 0;
531 }
532 
533 int
534 in6_setpeeraddr(so, nam)
535 	struct socket *so;
536 	struct sockaddr **nam;
537 {
538 	int s;
539 	struct inpcb *inp;
540 	struct in6_addr addr;
541 	in_port_t port;
542 
543 	s = splnet();
544 	inp = sotoinpcb(so);
545 	if (!inp) {
546 		splx(s);
547 		return EINVAL;
548 	}
549 	port = inp->inp_fport;
550 	addr = inp->in6p_faddr;
551 	splx(s);
552 
553 	*nam = in6_sockaddr(port, &addr);
554 	return 0;
555 }
556 
557 int
558 in6_mapped_sockaddr(struct socket *so, struct sockaddr **nam)
559 {
560 	struct	inpcb *inp = sotoinpcb(so);
561 	int	error;
562 
563 	if (inp == NULL)
564 		return EINVAL;
565 	if ((inp->inp_vflag & (INP_IPV4 | INP_IPV6)) == INP_IPV4) {
566 		error = in_setsockaddr(so, nam, &tcbinfo);
567 		if (error == 0)
568 			in6_sin_2_v4mapsin6_in_sock(nam);
569 	} else {
570 		/* scope issues will be handled in in6_setsockaddr(). */
571 		error = in6_setsockaddr(so, nam);
572 	}
573 
574 	return error;
575 }
576 
577 int
578 in6_mapped_peeraddr(struct socket *so, struct sockaddr **nam)
579 {
580 	struct	inpcb *inp = sotoinpcb(so);
581 	int	error;
582 
583 	if (inp == NULL)
584 		return EINVAL;
585 	if ((inp->inp_vflag & (INP_IPV4 | INP_IPV6)) == INP_IPV4) {
586 		error = in_setpeeraddr(so, nam, &tcbinfo);
587 		if (error == 0)
588 			in6_sin_2_v4mapsin6_in_sock(nam);
589 	} else
590 	/* scope issues will be handled in in6_setpeeraddr(). */
591 	error = in6_setpeeraddr(so, nam);
592 
593 	return error;
594 }
595 
596 /*
597  * Pass some notification to all connections of a protocol
598  * associated with address dst.  The local address and/or port numbers
599  * may be specified to limit the search.  The "usual action" will be
600  * taken, depending on the ctlinput cmd.  The caller must filter any
601  * cmds that are uninteresting (e.g., no error in the map).
602  * Call the protocol specific routine (if any) to report
603  * any errors for each matching socket.
604  *
605  * Must be called at splnet.
606  */
607 void
608 in6_pcbnotify(pcbinfo, dst, fport_arg, src, lport_arg, cmd, cmdarg, notify)
609 	struct inpcbinfo *pcbinfo;
610 	struct sockaddr *dst;
611 	const struct sockaddr *src;
612 	u_int fport_arg, lport_arg;
613 	int cmd;
614 	void *cmdarg;
615 	struct inpcb *(*notify) __P((struct inpcb *, int));
616 {
617 	struct inpcbhead *head;
618 	struct inpcb *inp, *ninp;
619 	struct sockaddr_in6 sa6_src, *sa6_dst;
620 	u_short	fport = fport_arg, lport = lport_arg;
621 	u_int32_t flowinfo;
622 	int errno, s;
623 
624 	if ((unsigned)cmd >= PRC_NCMDS || dst->sa_family != AF_INET6)
625 		return;
626 
627 	sa6_dst = (struct sockaddr_in6 *)dst;
628 	if (IN6_IS_ADDR_UNSPECIFIED(&sa6_dst->sin6_addr))
629 		return;
630 
631 	/*
632 	 * note that src can be NULL when we get notify by local fragmentation.
633 	 */
634 	sa6_src = (src == NULL) ? sa6_any : *(const struct sockaddr_in6 *)src;
635 	flowinfo = sa6_src.sin6_flowinfo;
636 
637 	/*
638 	 * Redirects go to all references to the destination,
639 	 * and use in6_rtchange to invalidate the route cache.
640 	 * Dead host indications: also use in6_rtchange to invalidate
641 	 * the cache, and deliver the error to all the sockets.
642 	 * Otherwise, if we have knowledge of the local port and address,
643 	 * deliver only to that socket.
644 	 */
645 	if (PRC_IS_REDIRECT(cmd) || cmd == PRC_HOSTDEAD) {
646 		fport = 0;
647 		lport = 0;
648 		bzero((caddr_t)&sa6_src.sin6_addr, sizeof(sa6_src.sin6_addr));
649 
650 		if (cmd != PRC_HOSTDEAD)
651 			notify = in6_rtchange;
652 	}
653 	errno = inet6ctlerrmap[cmd];
654 	s = splnet();
655 	head = pcbinfo->listhead;
656 	INP_INFO_WLOCK(pcbinfo);
657  	for (inp = LIST_FIRST(head); inp != NULL; inp = ninp) {
658 		INP_LOCK(inp);
659  		ninp = LIST_NEXT(inp, inp_list);
660 
661  		if ((inp->inp_vflag & INP_IPV6) == 0) {
662 			INP_UNLOCK(inp);
663 			continue;
664 		}
665 
666 		/*
667 		 * If the error designates a new path MTU for a destination
668 		 * and the application (associated with this socket) wanted to
669 		 * know the value, notify. Note that we notify for all
670 		 * disconnected sockets if the corresponding application
671 		 * wanted. This is because some UDP applications keep sending
672 		 * sockets disconnected.
673 		 * XXX: should we avoid to notify the value to TCP sockets?
674 		 */
675 		if (cmd == PRC_MSGSIZE && (inp->inp_flags & IN6P_MTU) != 0 &&
676 		    (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr) ||
677 		     IN6_ARE_ADDR_EQUAL(&inp->in6p_faddr, &sa6_dst->sin6_addr))) {
678 			ip6_notify_pmtu(inp, (struct sockaddr_in6 *)dst,
679 					(u_int32_t *)cmdarg);
680 		}
681 
682 		/*
683 		 * Detect if we should notify the error. If no source and
684 		 * destination ports are specifed, but non-zero flowinfo and
685 		 * local address match, notify the error. This is the case
686 		 * when the error is delivered with an encrypted buffer
687 		 * by ESP. Otherwise, just compare addresses and ports
688 		 * as usual.
689 		 */
690 		if (lport == 0 && fport == 0 && flowinfo &&
691 		    inp->inp_socket != NULL &&
692 		    flowinfo == (inp->in6p_flowinfo & IPV6_FLOWLABEL_MASK) &&
693 		    IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, &sa6_src.sin6_addr))
694 			goto do_notify;
695 		else if (!IN6_ARE_ADDR_EQUAL(&inp->in6p_faddr,
696 					     &sa6_dst->sin6_addr) ||
697 			 inp->inp_socket == 0 ||
698 			 (lport && inp->inp_lport != lport) ||
699 			 (!IN6_IS_ADDR_UNSPECIFIED(&sa6_src.sin6_addr) &&
700 			  !IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr,
701 					      &sa6_src.sin6_addr)) ||
702 			 (fport && inp->inp_fport != fport)) {
703 			INP_UNLOCK(inp);
704 			continue;
705 		}
706 
707 	  do_notify:
708 		if (notify) {
709 			if ((*notify)(inp, errno))
710 				INP_UNLOCK(inp);
711 		} else
712 			INP_UNLOCK(inp);
713 	}
714 	INP_INFO_WUNLOCK(pcbinfo);
715 	splx(s);
716 }
717 
718 /*
719  * Lookup a PCB based on the local address and port.
720  */
721 struct inpcb *
722 in6_pcblookup_local(pcbinfo, laddr, lport_arg, wild_okay)
723 	struct inpcbinfo *pcbinfo;
724 	struct in6_addr *laddr;
725 	u_int lport_arg;
726 	int wild_okay;
727 {
728 	register struct inpcb *inp;
729 	int matchwild = 3, wildcard;
730 	u_short lport = lport_arg;
731 
732 	if (!wild_okay) {
733 		struct inpcbhead *head;
734 		/*
735 		 * Look for an unconnected (wildcard foreign addr) PCB that
736 		 * matches the local address and port we're looking for.
737 		 */
738 		head = &pcbinfo->hashbase[INP_PCBHASH(INADDR_ANY, lport, 0,
739 						      pcbinfo->hashmask)];
740 		LIST_FOREACH(inp, head, inp_hash) {
741 			if ((inp->inp_vflag & INP_IPV6) == 0)
742 				continue;
743 			if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr) &&
744 			    IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, laddr) &&
745 			    inp->inp_lport == lport) {
746 				/*
747 				 * Found.
748 				 */
749 				return (inp);
750 			}
751 		}
752 		/*
753 		 * Not found.
754 		 */
755 		return (NULL);
756 	} else {
757 		struct inpcbporthead *porthash;
758 		struct inpcbport *phd;
759 		struct inpcb *match = NULL;
760 		/*
761 		 * Best fit PCB lookup.
762 		 *
763 		 * First see if this local port is in use by looking on the
764 		 * port hash list.
765 		 */
766 		porthash = &pcbinfo->porthashbase[INP_PCBPORTHASH(lport,
767 		    pcbinfo->porthashmask)];
768 		LIST_FOREACH(phd, porthash, phd_hash) {
769 			if (phd->phd_port == lport)
770 				break;
771 		}
772 		if (phd != NULL) {
773 			/*
774 			 * Port is in use by one or more PCBs. Look for best
775 			 * fit.
776 			 */
777 			LIST_FOREACH(inp, &phd->phd_pcblist, inp_portlist) {
778 				wildcard = 0;
779 				if ((inp->inp_vflag & INP_IPV6) == 0)
780 					continue;
781 				if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr))
782 					wildcard++;
783 				if (!IN6_IS_ADDR_UNSPECIFIED(
784 					&inp->in6p_laddr)) {
785 					if (IN6_IS_ADDR_UNSPECIFIED(laddr))
786 						wildcard++;
787 					else if (!IN6_ARE_ADDR_EQUAL(
788 						&inp->in6p_laddr, laddr))
789 						continue;
790 				} else {
791 					if (!IN6_IS_ADDR_UNSPECIFIED(laddr))
792 						wildcard++;
793 				}
794 				if (wildcard < matchwild) {
795 					match = inp;
796 					matchwild = wildcard;
797 					if (matchwild == 0) {
798 						break;
799 					}
800 				}
801 			}
802 		}
803 		return (match);
804 	}
805 }
806 
807 void
808 in6_pcbpurgeif0(head, ifp)
809 	struct in6pcb *head;
810 	struct ifnet *ifp;
811 {
812 	struct in6pcb *in6p;
813 	struct ip6_moptions *im6o;
814 	struct in6_multi_mship *imm, *nimm;
815 
816 	for (in6p = head; in6p != NULL; in6p = LIST_NEXT(in6p, inp_list)) {
817 		im6o = in6p->in6p_moptions;
818 		if ((in6p->inp_vflag & INP_IPV6) &&
819 		    im6o) {
820 			/*
821 			 * Unselect the outgoing interface if it is being
822 			 * detached.
823 			 */
824 			if (im6o->im6o_multicast_ifp == ifp)
825 				im6o->im6o_multicast_ifp = NULL;
826 
827 			/*
828 			 * Drop multicast group membership if we joined
829 			 * through the interface being detached.
830 			 * XXX controversial - is it really legal for kernel
831 			 * to force this?
832 			 */
833 			for (imm = im6o->im6o_memberships.lh_first;
834 			     imm != NULL; imm = nimm) {
835 				nimm = imm->i6mm_chain.le_next;
836 				if (imm->i6mm_maddr->in6m_ifp == ifp) {
837 					LIST_REMOVE(imm, i6mm_chain);
838 					in6_delmulti(imm->i6mm_maddr);
839 					free(imm, M_IPMADDR);
840 				}
841 			}
842 		}
843 	}
844 }
845 
846 /*
847  * Check for alternatives when higher level complains
848  * about service problems.  For now, invalidate cached
849  * routing information.  If the route was created dynamically
850  * (by a redirect), time to try a default gateway again.
851  */
852 void
853 in6_losing(in6p)
854 	struct inpcb *in6p;
855 {
856 	/*
857 	 * We don't store route pointers in the routing table anymore
858 	 */
859 	return;
860 }
861 
862 /*
863  * After a routing change, flush old routing
864  * and allocate a (hopefully) better one.
865  */
866 struct inpcb *
867 in6_rtchange(inp, errno)
868 	struct inpcb *inp;
869 	int errno;
870 {
871 	/*
872 	 * We don't store route pointers in the routing table anymore
873 	 */
874 	return inp;
875 }
876 
877 /*
878  * Lookup PCB in hash list.
879  */
880 struct inpcb *
881 in6_pcblookup_hash(pcbinfo, faddr, fport_arg, laddr, lport_arg, wildcard, ifp)
882 	struct inpcbinfo *pcbinfo;
883 	struct in6_addr *faddr, *laddr;
884 	u_int fport_arg, lport_arg;
885 	int wildcard;
886 	struct ifnet *ifp;
887 {
888 	struct inpcbhead *head;
889 	register struct inpcb *inp;
890 	u_short fport = fport_arg, lport = lport_arg;
891 	int faith;
892 
893 	if (faithprefix_p != NULL)
894 		faith = (*faithprefix_p)(laddr);
895 	else
896 		faith = 0;
897 
898 	/*
899 	 * First look for an exact match.
900 	 */
901 	head = &pcbinfo->hashbase[INP_PCBHASH(faddr->s6_addr32[3] /* XXX */,
902 					      lport, fport,
903 					      pcbinfo->hashmask)];
904 	LIST_FOREACH(inp, head, inp_hash) {
905 		if ((inp->inp_vflag & INP_IPV6) == 0)
906 			continue;
907 		if (IN6_ARE_ADDR_EQUAL(&inp->in6p_faddr, faddr) &&
908 		    IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, laddr) &&
909 		    inp->inp_fport == fport &&
910 		    inp->inp_lport == lport) {
911 			/*
912 			 * Found.
913 			 */
914 			return (inp);
915 		}
916 	}
917 	if (wildcard) {
918 		struct inpcb *local_wild = NULL;
919 
920 		head = &pcbinfo->hashbase[INP_PCBHASH(INADDR_ANY, lport, 0,
921 						      pcbinfo->hashmask)];
922 		LIST_FOREACH(inp, head, inp_hash) {
923 			if ((inp->inp_vflag & INP_IPV6) == 0)
924 				continue;
925 			if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr) &&
926 			    inp->inp_lport == lport) {
927 				if (faith && (inp->inp_flags & INP_FAITH) == 0)
928 					continue;
929 				if (IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr,
930 						       laddr))
931 					return (inp);
932 				else if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr))
933 					local_wild = inp;
934 			}
935 		}
936 		return (local_wild);
937 	}
938 
939 	/*
940 	 * Not found.
941 	 */
942 	return (NULL);
943 }
944 
945 void
946 init_sin6(struct sockaddr_in6 *sin6, struct mbuf *m)
947 {
948 	struct ip6_hdr *ip;
949 
950 	ip = mtod(m, struct ip6_hdr *);
951 	bzero(sin6, sizeof(*sin6));
952 	sin6->sin6_len = sizeof(*sin6);
953 	sin6->sin6_family = AF_INET6;
954 	sin6->sin6_addr = ip->ip6_src;
955 	if (IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr))
956 		sin6->sin6_addr.s6_addr16[1] = 0;
957 	sin6->sin6_scope_id =
958 		(m->m_pkthdr.rcvif && IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr))
959 		? m->m_pkthdr.rcvif->if_index : 0;
960 
961 	return;
962 }
963