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