in6_pcb.c revision 132794
1/*	$FreeBSD: head/sys/netinet6/in6_pcb.c 132794 2004-07-28 13:03:07Z yar $	*/
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
119struct	in6_addr zeroin6_addr;
120
121int
122in6_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	INP_INFO_WLOCK_ASSERT(pcbinfo);
134	INP_LOCK_ASSERT(inp);
135
136	if (!in6_ifaddr) /* XXX broken! */
137		return (EADDRNOTAVAIL);
138	if (inp->inp_lport || !IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr))
139		return (EINVAL);
140	if ((so->so_options & (SO_REUSEADDR|SO_REUSEPORT)) == 0)
141		wild = 1;
142	if (nam) {
143		sin6 = (struct sockaddr_in6 *)nam;
144		if (nam->sa_len != sizeof(*sin6))
145			return (EINVAL);
146		/*
147		 * family check.
148		 */
149		if (nam->sa_family != AF_INET6)
150			return (EAFNOSUPPORT);
151
152		/* KAME hack: embed scopeid */
153		if (in6_embedscope(&sin6->sin6_addr, sin6, inp, NULL) != 0)
154			return EINVAL;
155		/* this must be cleared for ifa_ifwithaddr() */
156		sin6->sin6_scope_id = 0;
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) < IPV6PORT_RESERVED &&
193			    suser_cred(cred, SUSER_ALLOWJAIL))
194				return (EACCES);
195			if (so->so_cred->cr_uid != 0 &&
196			    !IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) {
197				t = in6_pcblookup_local(pcbinfo,
198				    &sin6->sin6_addr, lport,
199				    INPLOOKUP_WILDCARD);
200				if (t &&
201				    ((t->inp_vflag & INP_TIMEWAIT) == 0) &&
202				    (so->so_type != SOCK_STREAM ||
203				     IN6_IS_ADDR_UNSPECIFIED(&t->in6p_faddr)) &&
204				    (!IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr) ||
205			    	     !IN6_IS_ADDR_UNSPECIFIED(&t->in6p_laddr) ||
206				     (t->inp_socket->so_options & SO_REUSEPORT)
207				      == 0) && (so->so_cred->cr_uid !=
208				     t->inp_socket->so_cred->cr_uid))
209					return (EADDRINUSE);
210				if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0 &&
211				    IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
212					struct sockaddr_in sin;
213
214					in6_sin6_2_sin(&sin, sin6);
215					t = in_pcblookup_local(pcbinfo,
216						sin.sin_addr, lport,
217						INPLOOKUP_WILDCARD);
218					if (t &&
219					    ((t->inp_vflag &
220					      INP_TIMEWAIT) == 0) &&
221					    (so->so_type != SOCK_STREAM ||
222					     ntohl(t->inp_faddr.s_addr) ==
223					      INADDR_ANY) &&
224					    (so->so_cred->cr_uid !=
225					     t->inp_socket->so_cred->cr_uid))
226						return (EADDRINUSE);
227				}
228			}
229			t = in6_pcblookup_local(pcbinfo, &sin6->sin6_addr,
230						lport, wild);
231			if (t && (reuseport & ((t->inp_vflag & INP_TIMEWAIT) ?
232			    intotw(t)->tw_so_options :
233			    t->inp_socket->so_options)) == 0)
234				return (EADDRINUSE);
235			if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0 &&
236			    IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
237				struct sockaddr_in sin;
238
239				in6_sin6_2_sin(&sin, sin6);
240				t = in_pcblookup_local(pcbinfo, sin.sin_addr,
241						       lport, wild);
242				if (t && t->inp_vflag & INP_TIMEWAIT) {
243					if ((reuseport &
244					    intotw(t)->tw_so_options) == 0 &&
245					    (ntohl(t->inp_laddr.s_addr) !=
246					     INADDR_ANY || ((inp->inp_vflag &
247					     INP_IPV6PROTO) ==
248					     (t->inp_vflag & INP_IPV6PROTO))))
249						return (EADDRINUSE);
250				}
251				else if (t &&
252				    (reuseport & t->inp_socket->so_options)
253				    == 0 && (ntohl(t->inp_laddr.s_addr) !=
254				    INADDR_ANY || INP_SOCKAF(so) ==
255				     INP_SOCKAF(t->inp_socket)))
256					return (EADDRINUSE);
257			}
258		}
259		inp->in6p_laddr = sin6->sin6_addr;
260	}
261	if (lport == 0) {
262		int e;
263		if ((e = in6_pcbsetport(&inp->in6p_laddr, inp, cred)) != 0)
264			return (e);
265	}
266	else {
267		inp->inp_lport = lport;
268		if (in_pcbinshash(inp) != 0) {
269			inp->in6p_laddr = in6addr_any;
270			inp->inp_lport = 0;
271			return (EAGAIN);
272		}
273	}
274	return (0);
275}
276
277/*
278 *   Transform old in6_pcbconnect() into an inner subroutine for new
279 *   in6_pcbconnect(): Do some validity-checking on the remote
280 *   address (in mbuf 'nam') and then determine local host address
281 *   (i.e., which interface) to use to access that remote host.
282 *
283 *   This preserves definition of in6_pcbconnect(), while supporting a
284 *   slightly different version for T/TCP.  (This is more than
285 *   a bit of a kludge, but cleaning up the internal interfaces would
286 *   have forced minor changes in every protocol).
287 */
288
289int
290in6_pcbladdr(inp, nam, plocal_addr6)
291	register struct inpcb *inp;
292	struct sockaddr *nam;
293	struct in6_addr **plocal_addr6;
294{
295	register struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)nam;
296	struct ifnet *ifp = NULL;
297	int error = 0;
298
299	if (nam->sa_len != sizeof (*sin6))
300		return (EINVAL);
301	if (sin6->sin6_family != AF_INET6)
302		return (EAFNOSUPPORT);
303	if (sin6->sin6_port == 0)
304		return (EADDRNOTAVAIL);
305
306	INP_INFO_WLOCK_ASSERT(inp->inp_pcbinfo);
307	INP_LOCK_ASSERT(inp);
308
309	/* KAME hack: embed scopeid */
310	if (in6_embedscope(&sin6->sin6_addr, sin6, inp, &ifp) != 0)
311		return EINVAL;
312
313	if (in6_ifaddr) {
314		/*
315		 * If the destination address is UNSPECIFIED addr,
316		 * use the loopback addr, e.g ::1.
317		 */
318		if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr))
319			sin6->sin6_addr = in6addr_loopback;
320	}
321	{
322		/*
323		 * XXX: in6_selectsrc might replace the bound local address
324		 * with the address specified by setsockopt(IPV6_PKTINFO).
325		 * Is it the intended behavior?
326		 */
327		*plocal_addr6 = in6_selectsrc(sin6, inp->in6p_outputopts,
328					      inp->in6p_moptions, NULL,
329					      &inp->in6p_laddr, &error);
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(inp, nam, cred)
353	register struct inpcb *inp;
354	struct sockaddr *nam;
355	struct ucred *cred;
356{
357	struct in6_addr *addr6;
358	register struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)nam;
359	int error;
360
361	INP_INFO_WLOCK_ASSERT(inp->inp_pcbinfo);
362	INP_LOCK_ASSERT(inp);
363
364	/*
365	 * Call inner routine, to assign local interface address.
366	 * in6_pcbladdr() may automatically fill in sin6_scope_id.
367	 */
368	if ((error = in6_pcbladdr(inp, nam, &addr6)) != 0)
369		return (error);
370
371	if (in6_pcblookup_hash(inp->inp_pcbinfo, &sin6->sin6_addr,
372			       sin6->sin6_port,
373			      IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)
374			      ? addr6 : &inp->in6p_laddr,
375			      inp->inp_lport, 0, NULL) != NULL) {
376		return (EADDRINUSE);
377	}
378	if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)) {
379		if (inp->inp_lport == 0) {
380			error = in6_pcbbind(inp, (struct sockaddr *)0, cred);
381			if (error)
382				return (error);
383		}
384		inp->in6p_laddr = *addr6;
385	}
386	inp->in6p_faddr = sin6->sin6_addr;
387	inp->inp_fport = sin6->sin6_port;
388	/* update flowinfo - draft-itojun-ipv6-flowlabel-api-00 */
389	inp->in6p_flowinfo &= ~IPV6_FLOWLABEL_MASK;
390	if (inp->in6p_flags & IN6P_AUTOFLOWLABEL)
391		inp->in6p_flowinfo |=
392#ifdef RANDOM_IP_ID
393		    (htonl(ip6_randomflowlabel()) & IPV6_FLOWLABEL_MASK);
394#else
395		    (htonl(ip6_flow_seq++) & IPV6_FLOWLABEL_MASK);
396#endif
397
398	in_pcbrehash(inp);
399#ifdef IPSEC
400	if (inp->inp_socket->so_type == SOCK_STREAM)
401		ipsec_pcbconn(inp->inp_sp);
402#endif
403	return (0);
404}
405
406void
407in6_pcbdisconnect(inp)
408	struct inpcb *inp;
409{
410
411	INP_INFO_WLOCK_ASSERT(inp->inp_pcbinfo);
412	INP_LOCK_ASSERT(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
426void
427in6_pcbdetach(inp)
428	struct inpcb *inp;
429{
430	struct socket *so = inp->inp_socket;
431	struct inpcbinfo *ipi = inp->inp_pcbinfo;
432
433	INP_INFO_WLOCK_ASSERT(inp->inp_pcbinfo);
434	INP_LOCK_ASSERT(inp);
435
436#if defined(IPSEC) || defined(FAST_IPSEC)
437	if (inp->in6p_sp != NULL)
438		ipsec6_delete_pcbpolicy(inp);
439#endif /* IPSEC */
440	inp->inp_gencnt = ++ipi->ipi_gencnt;
441	in_pcbremlists(inp);
442
443	if (so) {
444		SOCK_LOCK(so);
445		so->so_pcb = NULL;
446		sotryfree(so);
447	}
448
449 	ip6_freepcbopts(inp->in6p_outputopts);
450 	ip6_freemoptions(inp->in6p_moptions);
451	/* Check and free IPv4 related resources in case of mapped addr */
452	if (inp->inp_options)
453		(void)m_free(inp->inp_options);
454	ip_freemoptions(inp->inp_moptions);
455	inp->inp_vflag = 0;
456	INP_LOCK_DESTROY(inp);
457	uma_zfree(ipi->ipi_zone, inp);
458}
459
460struct sockaddr *
461in6_sockaddr(port, addr_p)
462	in_port_t port;
463	struct in6_addr *addr_p;
464{
465	struct sockaddr_in6 *sin6;
466
467	MALLOC(sin6, struct sockaddr_in6 *, sizeof *sin6, M_SONAME, M_WAITOK);
468	bzero(sin6, sizeof *sin6);
469	sin6->sin6_family = AF_INET6;
470	sin6->sin6_len = sizeof(*sin6);
471	sin6->sin6_port = port;
472	sin6->sin6_addr = *addr_p;
473	if (IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr))
474		sin6->sin6_scope_id = ntohs(sin6->sin6_addr.s6_addr16[1]);
475	else
476		sin6->sin6_scope_id = 0;	/*XXX*/
477	if (IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr))
478		sin6->sin6_addr.s6_addr16[1] = 0;
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	int s;
520	register struct inpcb *inp;
521	struct in6_addr addr;
522	in_port_t port;
523
524	s = splnet();
525	inp = sotoinpcb(so);
526	if (!inp) {
527		splx(s);
528		return EINVAL;
529	}
530	port = inp->inp_lport;
531	addr = inp->in6p_laddr;
532	splx(s);
533
534	*nam = in6_sockaddr(port, &addr);
535	return 0;
536}
537
538int
539in6_setpeeraddr(so, nam)
540	struct socket *so;
541	struct sockaddr **nam;
542{
543	int s;
544	struct inpcb *inp;
545	struct in6_addr addr;
546	in_port_t port;
547
548	s = splnet();
549	inp = sotoinpcb(so);
550	if (!inp) {
551		splx(s);
552		return EINVAL;
553	}
554	port = inp->inp_fport;
555	addr = inp->in6p_faddr;
556	splx(s);
557
558	*nam = in6_sockaddr(port, &addr);
559	return 0;
560}
561
562int
563in6_mapped_sockaddr(struct socket *so, struct sockaddr **nam)
564{
565	struct	inpcb *inp = sotoinpcb(so);
566	int	error;
567
568	if (inp == NULL)
569		return EINVAL;
570	if ((inp->inp_vflag & (INP_IPV4 | INP_IPV6)) == INP_IPV4) {
571		error = in_setsockaddr(so, nam, &tcbinfo);
572		if (error == 0)
573			in6_sin_2_v4mapsin6_in_sock(nam);
574	} else {
575		/* scope issues will be handled in in6_setsockaddr(). */
576		error = in6_setsockaddr(so, nam);
577	}
578
579	return error;
580}
581
582int
583in6_mapped_peeraddr(struct socket *so, struct sockaddr **nam)
584{
585	struct	inpcb *inp = sotoinpcb(so);
586	int	error;
587
588	if (inp == NULL)
589		return EINVAL;
590	if ((inp->inp_vflag & (INP_IPV4 | INP_IPV6)) == INP_IPV4) {
591		error = in_setpeeraddr(so, nam, &tcbinfo);
592		if (error == 0)
593			in6_sin_2_v4mapsin6_in_sock(nam);
594	} else
595	/* scope issues will be handled in in6_setpeeraddr(). */
596	error = in6_setpeeraddr(so, nam);
597
598	return error;
599}
600
601/*
602 * Pass some notification to all connections of a protocol
603 * associated with address dst.  The local address and/or port numbers
604 * may be specified to limit the search.  The "usual action" will be
605 * taken, depending on the ctlinput cmd.  The caller must filter any
606 * cmds that are uninteresting (e.g., no error in the map).
607 * Call the protocol specific routine (if any) to report
608 * any errors for each matching socket.
609 *
610 * Must be called at splnet.
611 */
612void
613in6_pcbnotify(head, dst, fport_arg, src, lport_arg, cmd, cmdarg, notify)
614	struct inpcbhead *head;
615	struct sockaddr *dst;
616	const struct sockaddr *src;
617	u_int fport_arg, lport_arg;
618	int cmd;
619	void *cmdarg;
620	struct inpcb *(*notify) __P((struct inpcb *, int));
621{
622	struct inpcb *inp, *ninp;
623	struct sockaddr_in6 sa6_src, *sa6_dst;
624	u_short	fport = fport_arg, lport = lport_arg;
625	u_int32_t flowinfo;
626	int errno, s;
627
628	if ((unsigned)cmd >= PRC_NCMDS || dst->sa_family != AF_INET6)
629		return;
630
631	sa6_dst = (struct sockaddr_in6 *)dst;
632	if (IN6_IS_ADDR_UNSPECIFIED(&sa6_dst->sin6_addr))
633		return;
634
635	/*
636	 * note that src can be NULL when we get notify by local fragmentation.
637	 */
638	sa6_src = (src == NULL) ? sa6_any : *(const struct sockaddr_in6 *)src;
639	flowinfo = sa6_src.sin6_flowinfo;
640
641	/*
642	 * Redirects go to all references to the destination,
643	 * and use in6_rtchange to invalidate the route cache.
644	 * Dead host indications: also use in6_rtchange to invalidate
645	 * the cache, and deliver the error to all the sockets.
646	 * Otherwise, if we have knowledge of the local port and address,
647	 * deliver only to that socket.
648	 */
649	if (PRC_IS_REDIRECT(cmd) || cmd == PRC_HOSTDEAD) {
650		fport = 0;
651		lport = 0;
652		bzero((caddr_t)&sa6_src.sin6_addr, sizeof(sa6_src.sin6_addr));
653
654		if (cmd != PRC_HOSTDEAD)
655			notify = in6_rtchange;
656	}
657	errno = inet6ctlerrmap[cmd];
658	s = splnet();
659 	for (inp = LIST_FIRST(head); inp != NULL; inp = ninp) {
660 		ninp = LIST_NEXT(inp, inp_list);
661
662 		if ((inp->inp_vflag & INP_IPV6) == 0)
663			continue;
664
665		/*
666		 * If the error designates a new path MTU for a destination
667		 * and the application (associated with this socket) wanted to
668		 * know the value, notify. Note that we notify for all
669		 * disconnected sockets if the corresponding application
670		 * wanted. This is because some UDP applications keep sending
671		 * sockets disconnected.
672		 * XXX: should we avoid to notify the value to TCP sockets?
673		 */
674		if (cmd == PRC_MSGSIZE && (inp->inp_flags & IN6P_MTU) != 0 &&
675		    (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr) ||
676		     IN6_ARE_ADDR_EQUAL(&inp->in6p_faddr, &sa6_dst->sin6_addr))) {
677			ip6_notify_pmtu(inp, (struct sockaddr_in6 *)dst,
678					(u_int32_t *)cmdarg);
679		}
680
681		/*
682		 * Detect if we should notify the error. If no source and
683		 * destination ports are specifed, but non-zero flowinfo and
684		 * local address match, notify the error. This is the case
685		 * when the error is delivered with an encrypted buffer
686		 * by ESP. Otherwise, just compare addresses and ports
687		 * as usual.
688		 */
689		if (lport == 0 && fport == 0 && flowinfo &&
690		    inp->inp_socket != NULL &&
691		    flowinfo == (inp->in6p_flowinfo & IPV6_FLOWLABEL_MASK) &&
692		    IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, &sa6_src.sin6_addr))
693			goto do_notify;
694		else if (!IN6_ARE_ADDR_EQUAL(&inp->in6p_faddr,
695					     &sa6_dst->sin6_addr) ||
696			 inp->inp_socket == 0 ||
697			 (lport && inp->inp_lport != lport) ||
698			 (!IN6_IS_ADDR_UNSPECIFIED(&sa6_src.sin6_addr) &&
699			  !IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr,
700					      &sa6_src.sin6_addr)) ||
701			 (fport && inp->inp_fport != fport))
702			continue;
703
704	  do_notify:
705		if (notify)
706			(*notify)(inp, errno);
707	}
708	splx(s);
709}
710
711/*
712 * Lookup a PCB based on the local address and port.
713 */
714struct inpcb *
715in6_pcblookup_local(pcbinfo, laddr, lport_arg, wild_okay)
716	struct inpcbinfo *pcbinfo;
717	struct in6_addr *laddr;
718	u_int lport_arg;
719	int wild_okay;
720{
721	register struct inpcb *inp;
722	int matchwild = 3, wildcard;
723	u_short lport = lport_arg;
724
725	if (!wild_okay) {
726		struct inpcbhead *head;
727		/*
728		 * Look for an unconnected (wildcard foreign addr) PCB that
729		 * matches the local address and port we're looking for.
730		 */
731		head = &pcbinfo->hashbase[INP_PCBHASH(INADDR_ANY, lport, 0,
732						      pcbinfo->hashmask)];
733		LIST_FOREACH(inp, head, inp_hash) {
734			if ((inp->inp_vflag & INP_IPV6) == 0)
735				continue;
736			if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr) &&
737			    IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, laddr) &&
738			    inp->inp_lport == lport) {
739				/*
740				 * Found.
741				 */
742				return (inp);
743			}
744		}
745		/*
746		 * Not found.
747		 */
748		return (NULL);
749	} else {
750		struct inpcbporthead *porthash;
751		struct inpcbport *phd;
752		struct inpcb *match = NULL;
753		/*
754		 * Best fit PCB lookup.
755		 *
756		 * First see if this local port is in use by looking on the
757		 * port hash list.
758		 */
759		porthash = &pcbinfo->porthashbase[INP_PCBPORTHASH(lport,
760		    pcbinfo->porthashmask)];
761		LIST_FOREACH(phd, porthash, phd_hash) {
762			if (phd->phd_port == lport)
763				break;
764		}
765		if (phd != NULL) {
766			/*
767			 * Port is in use by one or more PCBs. Look for best
768			 * fit.
769			 */
770			LIST_FOREACH(inp, &phd->phd_pcblist, inp_portlist) {
771				wildcard = 0;
772				if ((inp->inp_vflag & INP_IPV6) == 0)
773					continue;
774				if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr))
775					wildcard++;
776				if (!IN6_IS_ADDR_UNSPECIFIED(
777					&inp->in6p_laddr)) {
778					if (IN6_IS_ADDR_UNSPECIFIED(laddr))
779						wildcard++;
780					else if (!IN6_ARE_ADDR_EQUAL(
781						&inp->in6p_laddr, laddr))
782						continue;
783				} else {
784					if (!IN6_IS_ADDR_UNSPECIFIED(laddr))
785						wildcard++;
786				}
787				if (wildcard < matchwild) {
788					match = inp;
789					matchwild = wildcard;
790					if (matchwild == 0) {
791						break;
792					}
793				}
794			}
795		}
796		return (match);
797	}
798}
799
800void
801in6_pcbpurgeif0(head, ifp)
802	struct in6pcb *head;
803	struct ifnet *ifp;
804{
805	struct in6pcb *in6p;
806	struct ip6_moptions *im6o;
807	struct in6_multi_mship *imm, *nimm;
808
809	for (in6p = head; in6p != NULL; in6p = LIST_NEXT(in6p, inp_list)) {
810		im6o = in6p->in6p_moptions;
811		if ((in6p->inp_vflag & INP_IPV6) &&
812		    im6o) {
813			/*
814			 * Unselect the outgoing interface if it is being
815			 * detached.
816			 */
817			if (im6o->im6o_multicast_ifp == ifp)
818				im6o->im6o_multicast_ifp = NULL;
819
820			/*
821			 * Drop multicast group membership if we joined
822			 * through the interface being detached.
823			 * XXX controversial - is it really legal for kernel
824			 * to force this?
825			 */
826			for (imm = im6o->im6o_memberships.lh_first;
827			     imm != NULL; imm = nimm) {
828				nimm = imm->i6mm_chain.le_next;
829				if (imm->i6mm_maddr->in6m_ifp == ifp) {
830					LIST_REMOVE(imm, i6mm_chain);
831					in6_delmulti(imm->i6mm_maddr);
832					free(imm, M_IPMADDR);
833				}
834			}
835		}
836	}
837}
838
839/*
840 * Check for alternatives when higher level complains
841 * about service problems.  For now, invalidate cached
842 * routing information.  If the route was created dynamically
843 * (by a redirect), time to try a default gateway again.
844 */
845void
846in6_losing(in6p)
847	struct inpcb *in6p;
848{
849	/*
850	 * We don't store route pointers in the routing table anymore
851	 */
852	return;
853}
854
855/*
856 * After a routing change, flush old routing
857 * and allocate a (hopefully) better one.
858 */
859struct inpcb *
860in6_rtchange(inp, errno)
861	struct inpcb *inp;
862	int errno;
863{
864	/*
865	 * We don't store route pointers in the routing table anymore
866	 */
867	return inp;
868}
869
870/*
871 * Lookup PCB in hash list.
872 */
873struct inpcb *
874in6_pcblookup_hash(pcbinfo, faddr, fport_arg, laddr, lport_arg, wildcard, ifp)
875	struct inpcbinfo *pcbinfo;
876	struct in6_addr *faddr, *laddr;
877	u_int fport_arg, lport_arg;
878	int wildcard;
879	struct ifnet *ifp;
880{
881	struct inpcbhead *head;
882	register struct inpcb *inp;
883	u_short fport = fport_arg, lport = lport_arg;
884	int faith;
885
886	if (faithprefix_p != NULL)
887		faith = (*faithprefix_p)(laddr);
888	else
889		faith = 0;
890
891	/*
892	 * First look for an exact match.
893	 */
894	head = &pcbinfo->hashbase[INP_PCBHASH(faddr->s6_addr32[3] /* XXX */,
895					      lport, fport,
896					      pcbinfo->hashmask)];
897	LIST_FOREACH(inp, head, inp_hash) {
898		if ((inp->inp_vflag & INP_IPV6) == 0)
899			continue;
900		if (IN6_ARE_ADDR_EQUAL(&inp->in6p_faddr, faddr) &&
901		    IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, laddr) &&
902		    inp->inp_fport == fport &&
903		    inp->inp_lport == lport) {
904			/*
905			 * Found.
906			 */
907			return (inp);
908		}
909	}
910	if (wildcard) {
911		struct inpcb *local_wild = NULL;
912
913		head = &pcbinfo->hashbase[INP_PCBHASH(INADDR_ANY, lport, 0,
914						      pcbinfo->hashmask)];
915		LIST_FOREACH(inp, head, inp_hash) {
916			if ((inp->inp_vflag & INP_IPV6) == 0)
917				continue;
918			if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr) &&
919			    inp->inp_lport == lport) {
920				if (faith && (inp->inp_flags & INP_FAITH) == 0)
921					continue;
922				if (IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr,
923						       laddr))
924					return (inp);
925				else if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr))
926					local_wild = inp;
927			}
928		}
929		return (local_wild);
930	}
931
932	/*
933	 * Not found.
934	 */
935	return (NULL);
936}
937
938void
939init_sin6(struct sockaddr_in6 *sin6, struct mbuf *m)
940{
941	struct ip6_hdr *ip;
942
943	ip = mtod(m, struct ip6_hdr *);
944	bzero(sin6, sizeof(*sin6));
945	sin6->sin6_len = sizeof(*sin6);
946	sin6->sin6_family = AF_INET6;
947	sin6->sin6_addr = ip->ip6_src;
948	if (IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr))
949		sin6->sin6_addr.s6_addr16[1] = 0;
950	sin6->sin6_scope_id =
951		(m->m_pkthdr.rcvif && IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr))
952		? m->m_pkthdr.rcvif->if_index : 0;
953
954	return;
955}
956