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