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