in6_pcb.c revision 78064
1/*	$FreeBSD: head/sys/netinet6/in6_pcb.c 78064 2001-06-11 12:39:29Z ume $	*/
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 * 3. All advertising materials mentioning features or use of this software
47 *    must display the following acknowledgement:
48 *	This product includes software developed by the University of
49 *	California, Berkeley and its contributors.
50 * 4. Neither the name of the University nor the names of its contributors
51 *    may be used to endorse or promote products derived from this software
52 *    without specific prior written permission.
53 *
54 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
55 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
56 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
57 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
58 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
59 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
60 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
61 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
62 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
63 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
64 * SUCH DAMAGE.
65 *
66 *	@(#)in_pcb.c	8.2 (Berkeley) 1/4/94
67 */
68
69#include "opt_inet.h"
70#include "opt_inet6.h"
71#include "opt_ipsec.h"
72
73#include <sys/param.h>
74#include <sys/systm.h>
75#include <sys/malloc.h>
76#include <sys/mbuf.h>
77#include <sys/domain.h>
78#include <sys/protosw.h>
79#include <sys/socket.h>
80#include <sys/socketvar.h>
81#include <sys/sockio.h>
82#include <sys/errno.h>
83#include <sys/time.h>
84#include <sys/proc.h>
85#include <sys/jail.h>
86
87#include <vm/vm_zone.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/ip6.h>
97#include <netinet/ip_var.h>
98#include <netinet6/ip6_var.h>
99#include <netinet6/nd6.h>
100#include <netinet/in_pcb.h>
101#include <netinet6/in6_pcb.h>
102
103#include "faith.h"
104#if defined(NFAITH) && NFAITH > 0
105#include <net/if_faith.h>
106#endif
107
108#ifdef IPSEC
109#include <netinet6/ipsec.h>
110#ifdef INET6
111#include <netinet6/ipsec6.h>
112#endif
113#include <netinet6/ah.h>
114#ifdef INET6
115#include <netinet6/ah6.h>
116#endif
117#include <netkey/key.h>
118#endif /* IPSEC */
119
120struct	in6_addr zeroin6_addr;
121
122int
123in6_pcbbind(inp, nam, p)
124	register struct inpcb *inp;
125	struct sockaddr *nam;
126	struct proc *p;
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	if (!in6_ifaddr) /* XXX broken! */
135		return (EADDRNOTAVAIL);
136	if (inp->inp_lport || !IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr))
137		return(EINVAL);
138	if ((so->so_options & (SO_REUSEADDR|SO_REUSEPORT)) == 0)
139		wild = 1;
140	if (nam) {
141		sin6 = (struct sockaddr_in6 *)nam;
142		if (nam->sa_len != sizeof(*sin6))
143			return(EINVAL);
144		/*
145		 * family check.
146		 */
147		if (nam->sa_family != AF_INET6)
148			return(EAFNOSUPPORT);
149
150		/* KAME hack: embed scopeid */
151		if (in6_embedscope(&sin6->sin6_addr, sin6, inp, NULL) != 0)
152			return EINVAL;
153		/* this must be cleared for ifa_ifwithaddr() */
154		sin6->sin6_scope_id = 0;
155
156		lport = sin6->sin6_port;
157		if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) {
158			/*
159			 * Treat SO_REUSEADDR as SO_REUSEPORT for multicast;
160			 * allow compepte duplication of binding if
161			 * SO_REUSEPORT is set, or if SO_REUSEADDR is set
162			 * and a multicast address is bound on both
163			 * new and duplicated sockets.
164			 */
165			if (so->so_options & SO_REUSEADDR)
166				reuseport = SO_REUSEADDR|SO_REUSEPORT;
167		} else if (!IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
168			struct ifaddr *ia = NULL;
169
170			sin6->sin6_port = 0;		/* yech... */
171			if ((ia = ifa_ifwithaddr((struct sockaddr *)sin6)) == 0)
172				return(EADDRNOTAVAIL);
173
174			/*
175			 * XXX: bind to an anycast address might accidentally
176			 * cause sending a packet with anycast source address.
177			 * We should allow to bind to a deprecated address, since
178			 * the application dare to use it.
179			 */
180			if (ia &&
181			    ((struct in6_ifaddr *)ia)->ia6_flags &
182			    (IN6_IFF_ANYCAST|IN6_IFF_NOTREADY|IN6_IFF_DETACHED)) {
183				return(EADDRNOTAVAIL);
184			}
185		}
186		if (lport) {
187			struct inpcb *t;
188
189			/* GROSS */
190			if (ntohs(lport) < IPV6PORT_RESERVED && p &&
191			    suser_xxx(0, p, PRISON_ROOT))
192				return(EACCES);
193			if (so->so_cred->cr_uid != 0 &&
194			    !IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) {
195				t = in6_pcblookup_local(pcbinfo,
196				    &sin6->sin6_addr, lport,
197				    INPLOOKUP_WILDCARD);
198				if (t &&
199				    (!IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr) ||
200				     !IN6_IS_ADDR_UNSPECIFIED(&t->in6p_laddr) ||
201				     (t->inp_socket->so_options &
202				      SO_REUSEPORT) == 0) &&
203				    (so->so_cred->cr_uid !=
204				     t->inp_socket->so_cred->cr_uid))
205					return (EADDRINUSE);
206				if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0 &&
207				    IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
208					struct sockaddr_in sin;
209
210					in6_sin6_2_sin(&sin, sin6);
211					t = in_pcblookup_local(pcbinfo,
212						sin.sin_addr, lport,
213						INPLOOKUP_WILDCARD);
214					if (t &&
215					    (so->so_cred->cr_uid !=
216					     t->inp_socket->so_cred->cr_uid) &&
217					    (ntohl(t->inp_laddr.s_addr) !=
218					     INADDR_ANY ||
219					     INP_SOCKAF(so) ==
220					     INP_SOCKAF(t->inp_socket)))
221						return (EADDRINUSE);
222				}
223			}
224			t = in6_pcblookup_local(pcbinfo, &sin6->sin6_addr,
225						lport, wild);
226			if (t && (reuseport & t->inp_socket->so_options) == 0)
227				return(EADDRINUSE);
228			if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0 &&
229			    IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
230				struct sockaddr_in sin;
231
232				in6_sin6_2_sin(&sin, sin6);
233				t = in_pcblookup_local(pcbinfo, sin.sin_addr,
234						       lport, wild);
235				if (t &&
236				    (reuseport & t->inp_socket->so_options)
237				    == 0 &&
238				    (ntohl(t->inp_laddr.s_addr)
239				     != INADDR_ANY ||
240				     INP_SOCKAF(so) ==
241				     INP_SOCKAF(t->inp_socket)))
242					return (EADDRINUSE);
243			}
244		}
245		inp->in6p_laddr = sin6->sin6_addr;
246	}
247	if (lport == 0) {
248		int e;
249		if ((e = in6_pcbsetport(&inp->in6p_laddr, inp, p)) != 0)
250			return(e);
251	}
252	else {
253		inp->inp_lport = lport;
254		if (in_pcbinshash(inp) != 0) {
255			inp->in6p_laddr = in6addr_any;
256			inp->inp_lport = 0;
257			return (EAGAIN);
258		}
259	}
260	return(0);
261}
262
263/*
264 *   Transform old in6_pcbconnect() into an inner subroutine for new
265 *   in6_pcbconnect(): Do some validity-checking on the remote
266 *   address (in mbuf 'nam') and then determine local host address
267 *   (i.e., which interface) to use to access that remote host.
268 *
269 *   This preserves definition of in6_pcbconnect(), while supporting a
270 *   slightly different version for T/TCP.  (This is more than
271 *   a bit of a kludge, but cleaning up the internal interfaces would
272 *   have forced minor changes in every protocol).
273 */
274
275int
276in6_pcbladdr(inp, nam, plocal_addr6)
277	register struct inpcb *inp;
278	struct sockaddr *nam;
279	struct in6_addr **plocal_addr6;
280{
281	register struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)nam;
282	struct ifnet *ifp = NULL;
283	int error = 0;
284
285	if (nam->sa_len != sizeof (*sin6))
286		return (EINVAL);
287	if (sin6->sin6_family != AF_INET6)
288		return (EAFNOSUPPORT);
289	if (sin6->sin6_port == 0)
290		return (EADDRNOTAVAIL);
291
292	/* KAME hack: embed scopeid */
293	if (in6_embedscope(&sin6->sin6_addr, sin6, inp, &ifp) != 0)
294		return EINVAL;
295
296	if (in6_ifaddr) {
297		/*
298		 * If the destination address is UNSPECIFIED addr,
299		 * use the loopback addr, e.g ::1.
300		 */
301		if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr))
302			sin6->sin6_addr = in6addr_loopback;
303	}
304	{
305		/*
306		 * XXX: in6_selectsrc might replace the bound local address
307		 * with the address specified by setsockopt(IPV6_PKTINFO).
308		 * Is it the intended behavior?
309		 */
310		*plocal_addr6 = in6_selectsrc(sin6, inp->in6p_outputopts,
311					      inp->in6p_moptions,
312					      &inp->in6p_route,
313					      &inp->in6p_laddr, &error);
314		if (*plocal_addr6 == 0) {
315			if (error == 0)
316				error = EADDRNOTAVAIL;
317			return(error);
318		}
319		/*
320		 * Don't do pcblookup call here; return interface in
321		 * plocal_addr6
322		 * and exit to caller, that will do the lookup.
323		 */
324	}
325
326	if (inp->in6p_route.ro_rt)
327		ifp = inp->in6p_route.ro_rt->rt_ifp;
328
329	return(0);
330}
331
332/*
333 * Outer subroutine:
334 * Connect from a socket to a specified address.
335 * Both address and port must be specified in argument sin.
336 * If don't have a local address for this socket yet,
337 * then pick one.
338 */
339int
340in6_pcbconnect(inp, nam, p)
341	register struct inpcb *inp;
342	struct sockaddr *nam;
343	struct proc *p;
344{
345	struct in6_addr *addr6;
346	register struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)nam;
347	int error;
348
349	/*
350	 *   Call inner routine, to assign local interface address.
351	 */
352	if ((error = in6_pcbladdr(inp, nam, &addr6)) != 0)
353		return(error);
354
355	if (in6_pcblookup_hash(inp->inp_pcbinfo, &sin6->sin6_addr,
356			       sin6->sin6_port,
357			      IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)
358			      ? addr6 : &inp->in6p_laddr,
359			      inp->inp_lport, 0, NULL) != NULL) {
360		return (EADDRINUSE);
361	}
362	if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)) {
363		if (inp->inp_lport == 0) {
364			error = in6_pcbbind(inp, (struct sockaddr *)0, p);
365			if (error)
366				return (error);
367		}
368		inp->in6p_laddr = *addr6;
369	}
370	inp->in6p_faddr = sin6->sin6_addr;
371	inp->inp_fport = sin6->sin6_port;
372	/* update flowinfo - draft-itojun-ipv6-flowlabel-api-00 */
373	inp->in6p_flowinfo &= ~IPV6_FLOWLABEL_MASK;
374	if (inp->in6p_flags & IN6P_AUTOFLOWLABEL)
375		inp->in6p_flowinfo |=
376		    (htonl(ip6_flow_seq++) & IPV6_FLOWLABEL_MASK);
377
378	in_pcbrehash(inp);
379	return (0);
380}
381
382#if 0
383/*
384 * Return an IPv6 address, which is the most appropriate for given
385 * destination and user specified options.
386 * If necessary, this function lookups the routing table and return
387 * an entry to the caller for later use.
388 */
389struct in6_addr *
390in6_selectsrc(dstsock, opts, mopts, ro, laddr, errorp)
391	struct sockaddr_in6 *dstsock;
392	struct ip6_pktopts *opts;
393	struct ip6_moptions *mopts;
394	struct route_in6 *ro;
395	struct in6_addr *laddr;
396	int *errorp;
397{
398	struct in6_addr *dst;
399	struct in6_ifaddr *ia6 = 0;
400	struct in6_pktinfo *pi = NULL;
401
402	dst = &dstsock->sin6_addr;
403	*errorp = 0;
404
405	/*
406	 * If the source address is explicitly specified by the caller,
407	 * use it.
408	 */
409	if (opts && (pi = opts->ip6po_pktinfo) &&
410	    !IN6_IS_ADDR_UNSPECIFIED(&pi->ipi6_addr))
411		return(&pi->ipi6_addr);
412
413	/*
414	 * If the source address is not specified but the socket(if any)
415	 * is already bound, use the bound address.
416	 */
417	if (laddr && !IN6_IS_ADDR_UNSPECIFIED(laddr))
418		return(laddr);
419
420	/*
421	 * If the caller doesn't specify the source address but
422	 * the outgoing interface, use an address associated with
423	 * the interface.
424	 */
425	if (pi && pi->ipi6_ifindex) {
426		/* XXX boundary check is assumed to be already done. */
427		ia6 = in6_ifawithscope(ifindex2ifnet[pi->ipi6_ifindex],
428				       dst);
429		if (ia6 == 0) {
430			*errorp = EADDRNOTAVAIL;
431			return(0);
432		}
433		return(&satosin6(&ia6->ia_addr)->sin6_addr);
434	}
435
436	/*
437	 * If the destination address is a link-local unicast address or
438	 * a multicast address, and if the outgoing interface is specified
439	 * by the sin6_scope_id filed, use an address associated with the
440	 * interface.
441	 * XXX: We're now trying to define more specific semantics of
442	 *      sin6_scope_id field, so this part will be rewritten in
443	 *      the near future.
444	 */
445	if ((IN6_IS_ADDR_LINKLOCAL(dst) || IN6_IS_ADDR_MULTICAST(dst)) &&
446	    dstsock->sin6_scope_id) {
447		/*
448		 * I'm not sure if boundary check for scope_id is done
449		 * somewhere...
450		 */
451		if (dstsock->sin6_scope_id < 0 ||
452		    if_index < dstsock->sin6_scope_id) {
453			*errorp = ENXIO; /* XXX: better error? */
454			return(0);
455		}
456		ia6 = in6_ifawithscope(ifindex2ifnet[dstsock->sin6_scope_id],
457				       dst);
458		if (ia6 == 0) {
459			*errorp = EADDRNOTAVAIL;
460			return(0);
461		}
462		return(&satosin6(&ia6->ia_addr)->sin6_addr);
463	}
464
465	/*
466	 * If the destination address is a multicast address and
467	 * the outgoing interface for the address is specified
468	 * by the caller, use an address associated with the interface.
469	 * There is a sanity check here; if the destination has node-local
470	 * scope, the outgoing interfacde should be a loopback address.
471	 * Even if the outgoing interface is not specified, we also
472	 * choose a loopback interface as the outgoing interface.
473	 */
474	if (IN6_IS_ADDR_MULTICAST(dst)) {
475		struct ifnet *ifp = mopts ? mopts->im6o_multicast_ifp : NULL;
476
477		if (ifp == NULL && IN6_IS_ADDR_MC_NODELOCAL(dst)) {
478			ifp = &loif[0];
479		}
480
481		if (ifp) {
482			ia6 = in6_ifawithscope(ifp, dst);
483			if (ia6 == 0) {
484				*errorp = EADDRNOTAVAIL;
485				return(0);
486			}
487			return(&ia6->ia_addr.sin6_addr);
488		}
489	}
490
491	/*
492	 * If the next hop address for the packet is specified
493	 * by caller, use an address associated with the route
494	 * to the next hop.
495	 */
496	{
497		struct sockaddr_in6 *sin6_next;
498		struct rtentry *rt;
499
500		if (opts && opts->ip6po_nexthop) {
501			sin6_next = satosin6(opts->ip6po_nexthop);
502			rt = nd6_lookup(&sin6_next->sin6_addr, 1, NULL);
503			if (rt) {
504				ia6 = in6_ifawithscope(rt->rt_ifp, dst);
505				if (ia6 == 0)
506					ia6 = ifatoia6(rt->rt_ifa);
507			}
508			if (ia6 == 0) {
509				*errorp = EADDRNOTAVAIL;
510				return(0);
511			}
512			return(&satosin6(&ia6->ia_addr)->sin6_addr);
513		}
514	}
515
516	/*
517	 * If route is known or can be allocated now,
518	 * our src addr is taken from the i/f, else punt.
519	 */
520	if (ro) {
521		if (ro->ro_rt &&
522		    !IN6_ARE_ADDR_EQUAL(&satosin6(&ro->ro_dst)->sin6_addr, dst)) {
523			RTFREE(ro->ro_rt);
524			ro->ro_rt = (struct rtentry *)0;
525		}
526		if (ro->ro_rt == (struct rtentry *)0 ||
527		    ro->ro_rt->rt_ifp == (struct ifnet *)0) {
528			struct sockaddr_in6 *dst6;
529
530			/* No route yet, so try to acquire one */
531			bzero(&ro->ro_dst, sizeof(struct sockaddr_in6));
532			dst6 = (struct sockaddr_in6 *)&ro->ro_dst;
533			dst6->sin6_family = AF_INET6;
534			dst6->sin6_len = sizeof(struct sockaddr_in6);
535			dst6->sin6_addr = *dst;
536			if (IN6_IS_ADDR_MULTICAST(dst)) {
537				ro->ro_rt = rtalloc1(&((struct route *)ro)
538						     ->ro_dst, 0, 0UL);
539			} else {
540				rtalloc((struct route *)ro);
541			}
542		}
543
544		/*
545		 * in_pcbconnect() checks out IFF_LOOPBACK to skip using
546		 * the address. But we don't know why it does so.
547		 * It is necessary to ensure the scope even for lo0
548		 * so doesn't check out IFF_LOOPBACK.
549		 */
550
551		if (ro->ro_rt) {
552			ia6 = in6_ifawithscope(ro->ro_rt->rt_ifa->ifa_ifp, dst);
553			if (ia6 == 0) /* xxx scope error ?*/
554				ia6 = ifatoia6(ro->ro_rt->rt_ifa);
555		}
556		if (ia6 == 0) {
557			*errorp = EHOSTUNREACH;	/* no route */
558			return(0);
559		}
560		return(&satosin6(&ia6->ia_addr)->sin6_addr);
561	}
562
563	*errorp = EADDRNOTAVAIL;
564	return(0);
565}
566
567/*
568 * Default hop limit selection. The precedence is as follows:
569 * 1. Hoplimit valued specified via ioctl.
570 * 2. (If the outgoing interface is detected) the current
571 *     hop limit of the interface specified by router advertisement.
572 * 3. The system default hoplimit.
573*/
574int
575in6_selecthlim(in6p, ifp)
576	struct in6pcb *in6p;
577	struct ifnet *ifp;
578{
579	if (in6p && in6p->in6p_hops >= 0)
580		return(in6p->in6p_hops);
581	else if (ifp)
582		return(nd_ifinfo[ifp->if_index].chlim);
583	else
584		return(ip6_defhlim);
585}
586#endif
587
588void
589in6_pcbdisconnect(inp)
590	struct inpcb *inp;
591{
592	bzero((caddr_t)&inp->in6p_faddr, sizeof(inp->in6p_faddr));
593	inp->inp_fport = 0;
594	/* clear flowinfo - draft-itojun-ipv6-flowlabel-api-00 */
595	inp->in6p_flowinfo &= ~IPV6_FLOWLABEL_MASK;
596	in_pcbrehash(inp);
597	if (inp->inp_socket->so_state & SS_NOFDREF)
598		in6_pcbdetach(inp);
599}
600
601void
602in6_pcbdetach(inp)
603	struct inpcb *inp;
604{
605	struct socket *so = inp->inp_socket;
606	struct inpcbinfo *ipi = inp->inp_pcbinfo;
607
608#ifdef IPSEC
609	if (inp->in6p_sp != NULL)
610		ipsec6_delete_pcbpolicy(inp);
611#endif /* IPSEC */
612	inp->inp_gencnt = ++ipi->ipi_gencnt;
613	in_pcbremlists(inp);
614	sotoinpcb(so) = 0;
615	sofree(so);
616
617	if (inp->in6p_options)
618		m_freem(inp->in6p_options);
619 	ip6_freepcbopts(inp->in6p_outputopts);
620 	ip6_freemoptions(inp->in6p_moptions);
621	if (inp->in6p_route.ro_rt)
622		rtfree(inp->in6p_route.ro_rt);
623	/* Check and free IPv4 related resources in case of mapped addr */
624	if (inp->inp_options)
625		(void)m_free(inp->inp_options);
626	ip_freemoptions(inp->inp_moptions);
627
628	inp->inp_vflag = 0;
629	zfree(ipi->ipi_zone, inp);
630}
631
632/*
633 * The calling convention of in6_setsockaddr() and in6_setpeeraddr() was
634 * modified to match the pru_sockaddr() and pru_peeraddr() entry points
635 * in struct pr_usrreqs, so that protocols can just reference then directly
636 * without the need for a wrapper function.  The socket must have a valid
637 * (i.e., non-nil) PCB, but it should be impossible to get an invalid one
638 * except through a kernel programming error, so it is acceptable to panic
639 * (or in this case trap) if the PCB is invalid.  (Actually, we don't trap
640 * because there actually /is/ a programming error somewhere... XXX)
641 */
642int
643in6_setsockaddr(so, nam)
644	struct socket *so;
645	struct sockaddr **nam;
646{
647	int s;
648	register struct inpcb *inp;
649	register struct sockaddr_in6 *sin6;
650
651	/*
652	 * Do the malloc first in case it blocks.
653	 */
654	MALLOC(sin6, struct sockaddr_in6 *, sizeof *sin6, M_SONAME, M_WAITOK);
655	bzero(sin6, sizeof *sin6);
656	sin6->sin6_family = AF_INET6;
657	sin6->sin6_len = sizeof(*sin6);
658
659	s = splnet();
660	inp = sotoinpcb(so);
661	if (!inp) {
662		splx(s);
663		free(sin6, M_SONAME);
664		return EINVAL;
665	}
666	sin6->sin6_port = inp->inp_lport;
667	sin6->sin6_addr = inp->in6p_laddr;
668	splx(s);
669	if (IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr))
670		sin6->sin6_scope_id = ntohs(sin6->sin6_addr.s6_addr16[1]);
671	else
672		sin6->sin6_scope_id = 0;	/*XXX*/
673	if (IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr))
674		sin6->sin6_addr.s6_addr16[1] = 0;
675
676	*nam = (struct sockaddr *)sin6;
677	return 0;
678}
679
680int
681in6_setpeeraddr(so, nam)
682	struct socket *so;
683	struct sockaddr **nam;
684{
685	int s;
686	struct inpcb *inp;
687	register struct sockaddr_in6 *sin6;
688
689	/*
690	 * Do the malloc first in case it blocks.
691	 */
692	MALLOC(sin6, struct sockaddr_in6 *, sizeof(*sin6), M_SONAME, M_WAITOK);
693	bzero((caddr_t)sin6, sizeof (*sin6));
694	sin6->sin6_family = AF_INET6;
695	sin6->sin6_len = sizeof(struct sockaddr_in6);
696
697	s = splnet();
698	inp = sotoinpcb(so);
699	if (!inp) {
700		splx(s);
701		free(sin6, M_SONAME);
702		return EINVAL;
703	}
704	sin6->sin6_port = inp->inp_fport;
705	sin6->sin6_addr = inp->in6p_faddr;
706	splx(s);
707	if (IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr))
708		sin6->sin6_scope_id = ntohs(sin6->sin6_addr.s6_addr16[1]);
709	else
710		sin6->sin6_scope_id = 0;	/*XXX*/
711	if (IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr))
712		sin6->sin6_addr.s6_addr16[1] = 0;
713
714	*nam = (struct sockaddr *)sin6;
715	return 0;
716}
717
718int
719in6_mapped_sockaddr(struct socket *so, struct sockaddr **nam)
720{
721	struct	inpcb *inp = sotoinpcb(so);
722	int	error;
723
724	if (inp == NULL)
725		return EINVAL;
726	if (inp->inp_vflag & INP_IPV4) {
727		error = in_setsockaddr(so, nam);
728		if (error == 0)
729			in6_sin_2_v4mapsin6_in_sock(nam);
730	} else
731	error = in6_setsockaddr(so, nam);
732
733	return error;
734}
735
736int
737in6_mapped_peeraddr(struct socket *so, struct sockaddr **nam)
738{
739	struct	inpcb *inp = sotoinpcb(so);
740	int	error;
741
742	if (inp == NULL)
743		return EINVAL;
744	if (inp->inp_vflag & INP_IPV4) {
745		error = in_setpeeraddr(so, nam);
746		if (error == 0)
747			in6_sin_2_v4mapsin6_in_sock(nam);
748	} else
749	error = in6_setpeeraddr(so, nam);
750
751	return error;
752}
753
754/*
755 * Pass some notification to all connections of a protocol
756 * associated with address dst.  The local address and/or port numbers
757 * may be specified to limit the search.  The "usual action" will be
758 * taken, depending on the ctlinput cmd.  The caller must filter any
759 * cmds that are uninteresting (e.g., no error in the map).
760 * Call the protocol specific routine (if any) to report
761 * any errors for each matching socket.
762 *
763 * Must be called at splnet.
764 */
765void
766in6_pcbnotify(head, dst, fport_arg, src, lport_arg, cmd, notify)
767	struct inpcbhead *head;
768	struct sockaddr *dst, *src;
769	u_int fport_arg, lport_arg;
770	int cmd;
771	void (*notify) __P((struct inpcb *, int));
772{
773	struct inpcb *inp, *ninp;
774	struct sockaddr_in6 sa6_src, *sa6_dst;
775	u_short	fport = fport_arg, lport = lport_arg;
776	u_int32_t flowinfo;
777	int errno, s;
778
779	if ((unsigned)cmd > PRC_NCMDS || dst->sa_family != AF_INET6)
780		return;
781
782	sa6_dst = (struct sockaddr_in6 *)dst;
783	if (IN6_IS_ADDR_UNSPECIFIED(&sa6_dst->sin6_addr))
784		return;
785
786	/*
787	 * note that src can be NULL when we get notify by local fragmentation.
788	 */
789	sa6_src = (src == NULL) ? sa6_any : *(struct sockaddr_in6 *)src;
790	flowinfo = sa6_src.sin6_flowinfo;
791
792	/*
793	 * Redirects go to all references to the destination,
794	 * and use in6_rtchange to invalidate the route cache.
795	 * Dead host indications: also use in6_rtchange to invalidate
796	 * the cache, and deliver the error to all the sockets.
797	 * Otherwise, if we have knowledge of the local port and address,
798	 * deliver only to that socket.
799	 */
800	if (PRC_IS_REDIRECT(cmd) || cmd == PRC_HOSTDEAD) {
801		fport = 0;
802		lport = 0;
803		bzero((caddr_t)&sa6_src.sin6_addr, sizeof(sa6_src.sin6_addr));
804
805		if (cmd != PRC_HOSTDEAD)
806			notify = in6_rtchange;
807	}
808	errno = inet6ctlerrmap[cmd];
809	s = splnet();
810 	for (inp = LIST_FIRST(head); inp != NULL; inp = ninp) {
811 		ninp = LIST_NEXT(inp, inp_list);
812
813 		if ((inp->inp_vflag & INP_IPV6) == 0)
814			continue;
815
816		/*
817		 * Detect if we should notify the error. If no source and
818		 * destination ports are specifed, but non-zero flowinfo and
819		 * local address match, notify the error. This is the case
820		 * when the error is delivered with an encrypted buffer
821		 * by ESP. Otherwise, just compare addresses and ports
822		 * as usual.
823		 */
824		if (lport == 0 && fport == 0 && flowinfo &&
825		    inp->inp_socket != NULL &&
826		    flowinfo == (inp->in6p_flowinfo & IPV6_FLOWLABEL_MASK) &&
827		    IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, &sa6_src.sin6_addr))
828			goto do_notify;
829		else if (!IN6_ARE_ADDR_EQUAL(&inp->in6p_faddr,
830					     &sa6_dst->sin6_addr) ||
831			 inp->inp_socket == 0 ||
832			 (lport && inp->inp_lport != lport) ||
833			 (!IN6_IS_ADDR_UNSPECIFIED(&sa6_src.sin6_addr) &&
834			  !IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr,
835					      &sa6_src.sin6_addr)) ||
836			 (fport && inp->inp_fport != fport))
837			continue;
838
839	  do_notify:
840		if (notify)
841			(*notify)(inp, errno);
842	}
843	splx(s);
844}
845
846/*
847 * Lookup a PCB based on the local address and port.
848 */
849struct inpcb *
850in6_pcblookup_local(pcbinfo, laddr, lport_arg, wild_okay)
851	struct inpcbinfo *pcbinfo;
852	struct in6_addr *laddr;
853	u_int lport_arg;
854	int wild_okay;
855{
856	register struct inpcb *inp;
857	int matchwild = 3, wildcard;
858	u_short lport = lport_arg;
859
860	if (!wild_okay) {
861		struct inpcbhead *head;
862		/*
863		 * Look for an unconnected (wildcard foreign addr) PCB that
864		 * matches the local address and port we're looking for.
865		 */
866		head = &pcbinfo->hashbase[INP_PCBHASH(INADDR_ANY, lport, 0,
867						      pcbinfo->hashmask)];
868		LIST_FOREACH(inp, head, inp_hash) {
869			if ((inp->inp_vflag & INP_IPV6) == 0)
870				continue;
871			if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr) &&
872			    IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, laddr) &&
873			    inp->inp_lport == lport) {
874				/*
875				 * Found.
876				 */
877				return (inp);
878			}
879		}
880		/*
881		 * Not found.
882		 */
883		return (NULL);
884	} else {
885		struct inpcbporthead *porthash;
886		struct inpcbport *phd;
887		struct inpcb *match = NULL;
888		/*
889		 * Best fit PCB lookup.
890		 *
891		 * First see if this local port is in use by looking on the
892		 * port hash list.
893		 */
894		porthash = &pcbinfo->porthashbase[INP_PCBPORTHASH(lport,
895		    pcbinfo->porthashmask)];
896		LIST_FOREACH(phd, porthash, phd_hash) {
897			if (phd->phd_port == lport)
898				break;
899		}
900		if (phd != NULL) {
901			/*
902			 * Port is in use by one or more PCBs. Look for best
903			 * fit.
904			 */
905			LIST_FOREACH(inp, &phd->phd_pcblist, inp_portlist) {
906				wildcard = 0;
907				if ((inp->inp_vflag & INP_IPV6) == 0)
908					continue;
909				if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr))
910					wildcard++;
911				if (!IN6_IS_ADDR_UNSPECIFIED(
912					&inp->in6p_laddr)) {
913					if (IN6_IS_ADDR_UNSPECIFIED(laddr))
914						wildcard++;
915					else if (!IN6_ARE_ADDR_EQUAL(
916						&inp->in6p_laddr, laddr))
917						continue;
918				} else {
919					if (!IN6_IS_ADDR_UNSPECIFIED(laddr))
920						wildcard++;
921				}
922				if (wildcard < matchwild) {
923					match = inp;
924					matchwild = wildcard;
925					if (matchwild == 0) {
926						break;
927					}
928				}
929			}
930		}
931		return (match);
932	}
933}
934
935/*
936 * Check for alternatives when higher level complains
937 * about service problems.  For now, invalidate cached
938 * routing information.  If the route was created dynamically
939 * (by a redirect), time to try a default gateway again.
940 */
941void
942in6_losing(in6p)
943	struct inpcb *in6p;
944{
945	struct rtentry *rt;
946	struct rt_addrinfo info;
947
948	if ((rt = in6p->in6p_route.ro_rt) != NULL) {
949		in6p->in6p_route.ro_rt = 0;
950		bzero((caddr_t)&info, sizeof(info));
951		info.rti_info[RTAX_DST] =
952			(struct sockaddr *)&in6p->in6p_route.ro_dst;
953		info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
954		info.rti_info[RTAX_NETMASK] = rt_mask(rt);
955		rt_missmsg(RTM_LOSING, &info, rt->rt_flags, 0);
956		if (rt->rt_flags & RTF_DYNAMIC)
957			(void)rtrequest(RTM_DELETE, rt_key(rt),
958					rt->rt_gateway, rt_mask(rt), rt->rt_flags,
959					(struct rtentry **)0);
960		else
961		/*
962		 * A new route can be allocated
963		 * the next time output is attempted.
964		 */
965			rtfree(rt);
966	}
967}
968
969/*
970 * After a routing change, flush old routing
971 * and allocate a (hopefully) better one.
972 */
973void
974in6_rtchange(inp, errno)
975	struct inpcb *inp;
976	int errno;
977{
978	if (inp->in6p_route.ro_rt) {
979		rtfree(inp->in6p_route.ro_rt);
980		inp->in6p_route.ro_rt = 0;
981		/*
982		 * A new route can be allocated the next time
983		 * output is attempted.
984		 */
985	}
986}
987
988/*
989 * Lookup PCB in hash list.
990 */
991struct inpcb *
992in6_pcblookup_hash(pcbinfo, faddr, fport_arg, laddr, lport_arg, wildcard, ifp)
993	struct inpcbinfo *pcbinfo;
994	struct in6_addr *faddr, *laddr;
995	u_int fport_arg, lport_arg;
996	int wildcard;
997	struct ifnet *ifp;
998{
999	struct inpcbhead *head;
1000	register struct inpcb *inp;
1001	u_short fport = fport_arg, lport = lport_arg;
1002	int faith;
1003
1004#if defined(NFAITH) && NFAITH > 0
1005	faith = faithprefix(laddr);
1006#else
1007	faith = 0;
1008#endif
1009
1010	/*
1011	 * First look for an exact match.
1012	 */
1013	head = &pcbinfo->hashbase[INP_PCBHASH(faddr->s6_addr32[3] /* XXX */,
1014					      lport, fport,
1015					      pcbinfo->hashmask)];
1016	LIST_FOREACH(inp, head, inp_hash) {
1017		if ((inp->inp_vflag & INP_IPV6) == 0)
1018			continue;
1019		if (IN6_ARE_ADDR_EQUAL(&inp->in6p_faddr, faddr) &&
1020		    IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, laddr) &&
1021		    inp->inp_fport == fport &&
1022		    inp->inp_lport == lport) {
1023			/*
1024			 * Found.
1025			 */
1026			return (inp);
1027		}
1028	}
1029	if (wildcard) {
1030		struct inpcb *local_wild = NULL;
1031
1032		head = &pcbinfo->hashbase[INP_PCBHASH(INADDR_ANY, lport, 0,
1033						      pcbinfo->hashmask)];
1034		LIST_FOREACH(inp, head, inp_hash) {
1035			if ((inp->inp_vflag & INP_IPV6) == 0)
1036				continue;
1037			if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr) &&
1038			    inp->inp_lport == lport) {
1039				if (faith && (inp->inp_flags & INP_FAITH) == 0)
1040					continue;
1041				if (IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr,
1042						       laddr))
1043					return (inp);
1044				else if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr))
1045					local_wild = inp;
1046			}
1047		}
1048		return (local_wild);
1049	}
1050
1051	/*
1052	 * Not found.
1053	 */
1054	return (NULL);
1055}
1056
1057void
1058init_sin6(struct sockaddr_in6 *sin6, struct mbuf *m)
1059{
1060	struct ip6_hdr *ip;
1061
1062	ip = mtod(m, struct ip6_hdr *);
1063	bzero(sin6, sizeof(*sin6));
1064	sin6->sin6_len = sizeof(*sin6);
1065	sin6->sin6_family = AF_INET6;
1066	sin6->sin6_addr = ip->ip6_src;
1067	if (IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr))
1068		sin6->sin6_addr.s6_addr16[1] = 0;
1069	sin6->sin6_scope_id =
1070		(m->m_pkthdr.rcvif && IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr))
1071		? m->m_pkthdr.rcvif->if_index : 0;
1072
1073	return;
1074}
1075