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