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