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