xref: /netbsd-src/sys/netinet6/in6_pcb.c (revision 08c81a9c2dc8c7300e893321eb65c0925d60871c)
1 /*	$NetBSD: in6_pcb.c,v 1.52 2002/09/11 02:46:45 itojun Exp $	*/
2 /*	$KAME: in6_pcb.c,v 1.84 2001/02/08 18:02:08 itojun 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  * Copyright (c) 1982, 1986, 1991, 1993
35  *	The Regents of the University of California.  All rights reserved.
36  *
37  * Redistribution and use in source and binary forms, with or without
38  * modification, are permitted provided that the following conditions
39  * are met:
40  * 1. Redistributions of source code must retain the above copyright
41  *    notice, this list of conditions and the following disclaimer.
42  * 2. Redistributions in binary form must reproduce the above copyright
43  *    notice, this list of conditions and the following disclaimer in the
44  *    documentation and/or other materials provided with the distribution.
45  * 3. All advertising materials mentioning features or use of this software
46  *    must display the following acknowledgement:
47  *	This product includes software developed by the University of
48  *	California, Berkeley and its contributors.
49  * 4. Neither the name of the University nor the names of its contributors
50  *    may be used to endorse or promote products derived from this software
51  *    without specific prior written permission.
52  *
53  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
54  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
55  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
56  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
57  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
58  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
59  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
60  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
61  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
62  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
63  * SUCH DAMAGE.
64  *
65  *	@(#)in_pcb.c	8.2 (Berkeley) 1/4/94
66  */
67 
68 #include <sys/cdefs.h>
69 __KERNEL_RCSID(0, "$NetBSD: in6_pcb.c,v 1.52 2002/09/11 02:46:45 itojun Exp $");
70 
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/protosw.h>
78 #include <sys/socket.h>
79 #include <sys/socketvar.h>
80 #include <sys/ioctl.h>
81 #include <sys/errno.h>
82 #include <sys/time.h>
83 #include <sys/proc.h>
84 
85 #include <net/if.h>
86 #include <net/route.h>
87 
88 #include <netinet/in.h>
89 #include <netinet/in_var.h>
90 #include <netinet/in_systm.h>
91 #include <netinet/ip.h>
92 #include <netinet/in_pcb.h>
93 #include <netinet/ip6.h>
94 #include <netinet6/ip6_var.h>
95 #include <netinet6/in6_pcb.h>
96 #include <netinet6/nd6.h>
97 
98 #include "loop.h"
99 extern struct ifnet loif[NLOOP];
100 #include "faith.h"
101 
102 #ifdef IPSEC
103 #include <netinet6/ipsec.h>
104 #include <netkey/key.h>
105 #endif /* IPSEC */
106 
107 struct in6_addr zeroin6_addr;
108 
109 int ip6_anonportmin = IPV6PORT_ANONMIN;
110 int ip6_anonportmax = IPV6PORT_ANONMAX;
111 int ip6_lowportmin  = IPV6PORT_RESERVEDMIN;
112 int ip6_lowportmax  = IPV6PORT_RESERVEDMAX;
113 
114 int
115 in6_pcballoc(so, head)
116 	struct socket *so;
117 	struct in6pcb *head;
118 {
119 	struct in6pcb *in6p;
120 	int s;
121 #ifdef IPSEC
122 	int error;
123 #endif
124 
125 	MALLOC(in6p, struct in6pcb *, sizeof(*in6p), M_PCB, M_NOWAIT);
126 	if (in6p == NULL)
127 		return (ENOBUFS);
128 	bzero((caddr_t)in6p, sizeof(*in6p));
129 	in6p->in6p_head = head;
130 	in6p->in6p_socket = so;
131 	in6p->in6p_hops = -1;	/* use kernel default */
132 	in6p->in6p_icmp6filt = NULL;
133 #ifdef IPSEC
134 	error = ipsec_init_pcbpolicy(so, &in6p->in6p_sp);
135 	if (error != 0) {
136 		FREE(in6p, M_PCB);
137 		return error;
138 	}
139 #endif /* IPSEC */
140 	s = splnet();
141 	in6p->in6p_next = head->in6p_next;
142 	head->in6p_next = in6p;
143 	in6p->in6p_prev = head;
144 	in6p->in6p_next->in6p_prev = in6p;
145 	splx(s);
146 	if (ip6_v6only)
147 		in6p->in6p_flags |= IN6P_IPV6_V6ONLY;
148 	so->so_pcb = (caddr_t)in6p;
149 	return (0);
150 }
151 
152 int
153 in6_pcbbind(in6p, nam, p)
154 	struct in6pcb *in6p;
155 	struct mbuf *nam;
156 	struct proc *p;
157 {
158 	struct socket *so = in6p->in6p_socket;
159 	struct in6pcb *head = in6p->in6p_head;
160 	struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)NULL;
161 	u_int16_t lport = 0;
162 	int wild = 0, reuseport = (so->so_options & SO_REUSEPORT);
163 
164 	if (in6p->in6p_lport || !IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr))
165 		return (EINVAL);
166 	if ((so->so_options & (SO_REUSEADDR|SO_REUSEPORT)) == 0 &&
167 	   ((so->so_proto->pr_flags & PR_CONNREQUIRED) == 0 ||
168 	    (so->so_options & SO_ACCEPTCONN) == 0))
169 		wild = IN6PLOOKUP_WILDCARD;
170 	if (nam) {
171 		sin6 = mtod(nam, struct sockaddr_in6 *);
172 		if (nam->m_len != sizeof(*sin6))
173 			return (EINVAL);
174 		/*
175 		 * We should check the family, but old programs
176 		 * incorrectly fail to intialize it.
177 		 */
178 		if (sin6->sin6_family != AF_INET6)
179 			return (EAFNOSUPPORT);
180 
181 		/*
182 		 * since we do not check port number duplicate with IPv4 space,
183 		 * we reject it at this moment.  If we leave it, we would
184 		 * mistakenly allow normal users to hijack tcp/udp ports from
185 		 * other users.
186 		 */
187 		if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr))
188 			return (EADDRNOTAVAIL);
189 
190 		/* KAME hack: embed scopeid */
191 		if (in6_embedscope(&sin6->sin6_addr, sin6, in6p, NULL) != 0)
192 			return EINVAL;
193 		/* this must be cleared for ifa_ifwithaddr() */
194 		sin6->sin6_scope_id = 0;
195 
196 		lport = sin6->sin6_port;
197 		if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) {
198 			/*
199 			 * Treat SO_REUSEADDR as SO_REUSEPORT for multicast;
200 			 * allow compepte duplication of binding if
201 			 * SO_REUSEPORT is set, or if SO_REUSEADDR is set
202 			 * and a multicast address is bound on both
203 			 * new and duplicated sockets.
204 			 */
205 			if (so->so_options & SO_REUSEADDR)
206 				reuseport = SO_REUSEADDR|SO_REUSEPORT;
207 		}
208 		else if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
209 			struct sockaddr_in sin;
210 
211 			bzero(&sin, sizeof(sin));
212 			sin.sin_len = sizeof(sin);
213 			sin.sin_family = AF_INET;
214 			bcopy(&sin6->sin6_addr.s6_addr32[3], &sin.sin_addr,
215 				sizeof(sin.sin_addr));
216 			if (ifa_ifwithaddr((struct sockaddr *)&sin) == 0)
217 				return EADDRNOTAVAIL;
218 		}
219 		else if (!IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
220 			struct ifaddr *ia = NULL;
221 
222 			sin6->sin6_port = 0;		/* yech... */
223 			if ((in6p->in6p_flags & IN6P_FAITH) == 0 &&
224 			    (ia = ifa_ifwithaddr((struct sockaddr *)sin6)) == 0)
225 				return (EADDRNOTAVAIL);
226 
227 			/*
228 			 * bind to an anycast address might accidentally
229 			 * cause sending a packet with an anycast source
230 			 * address, so we forbid it.
231 			 *
232 			 * We should allow to bind to a deprecated address,
233 			 * since the application dare to use it.
234 			 * But, can we assume that they are careful enough
235 			 * to check if the address is deprecated or not?
236 			 * Maybe, as a safeguard, we should have a setsockopt
237 			 * flag to control the bind(2) behavior against
238 			 * deprecated addresses (default: forbid bind(2)).
239 			 */
240 			if (ia &&
241 			    ((struct in6_ifaddr *)ia)->ia6_flags &
242 			    (IN6_IFF_ANYCAST|IN6_IFF_NOTREADY|IN6_IFF_DETACHED))
243 				return (EADDRNOTAVAIL);
244 		}
245 		if (lport) {
246 #ifndef IPNOPRIVPORTS
247 			int priv;
248 
249 			/*
250 			 * NOTE: all operating systems use suser() for
251 			 * privilege check!  do not rewrite it into SS_PRIV.
252 			 */
253 			priv = (p && !suser(p->p_ucred, &p->p_acflag)) ? 1 : 0;
254 			/* GROSS */
255 			if (ntohs(lport) < IPV6PORT_RESERVED && !priv)
256 				return (EACCES);
257 #endif
258 
259 			if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
260 				/* should check this but we can't ... */
261 #if 0
262 				struct inpcb *t;
263 
264 				t = in_pcblookup_bind(&tcbtable,
265 					(struct in_addr *)&sin6->sin6_addr.s6_addr32[3],
266 					lport);
267 				if (t && (reuseport & t->inp_socket->so_options) == 0)
268 					return EADDRINUSE;
269 #endif
270 			} else
271 			{
272 				struct in6pcb *t;
273 
274 				t = in6_pcblookup(head, &zeroin6_addr, 0,
275 						  &sin6->sin6_addr, lport, wild);
276 				if (t && (reuseport & t->in6p_socket->so_options) == 0)
277 					return (EADDRINUSE);
278 			}
279 		}
280 		in6p->in6p_laddr = sin6->sin6_addr;
281 	}
282 
283 	if (lport == 0) {
284 		int e;
285 		if ((e = in6_pcbsetport(&in6p->in6p_laddr, in6p, p)) != 0)
286 			return (e);
287 	}
288 	else
289 		in6p->in6p_lport = lport;
290 
291 	in6p->in6p_flowinfo = sin6 ? sin6->sin6_flowinfo : 0;	/*XXX*/
292 	return (0);
293 }
294 
295 /*
296  * Connect from a socket to a specified address.
297  * Both address and port must be specified in argument sin6.
298  * If don't have a local address for this socket yet,
299  * then pick one.
300  */
301 int
302 in6_pcbconnect(in6p, nam)
303 	struct in6pcb *in6p;
304 	struct mbuf *nam;
305 {
306 	struct in6_addr *in6a = NULL;
307 	struct sockaddr_in6 *sin6 = mtod(nam, struct sockaddr_in6 *);
308 	struct ifnet *ifp = NULL;	/* outgoing interface */
309 	int error = 0;
310 #ifdef INET
311 	struct in6_addr mapped;
312 #endif
313 	struct sockaddr_in6 tmp;
314 
315 	(void)&in6a;				/* XXX fool gcc */
316 
317 	if (nam->m_len != sizeof(*sin6))
318 		return (EINVAL);
319 	if (sin6->sin6_family != AF_INET6)
320 		return (EAFNOSUPPORT);
321 	if (sin6->sin6_port == 0)
322 		return (EADDRNOTAVAIL);
323 
324 	/* sanity check for mapped address case */
325 	if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
326 		if ((in6p->in6p_flags & IN6P_IPV6_V6ONLY) != 0)
327 			return EINVAL;
328 		if (IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr))
329 			in6p->in6p_laddr.s6_addr16[5] = htons(0xffff);
330 		if (!IN6_IS_ADDR_V4MAPPED(&in6p->in6p_laddr))
331 			return EINVAL;
332 	} else
333 	{
334 		if (IN6_IS_ADDR_V4MAPPED(&in6p->in6p_laddr))
335 			return EINVAL;
336 	}
337 
338 	/* protect *sin6 from overwrites */
339 	tmp = *sin6;
340 	sin6 = &tmp;
341 
342 	/* KAME hack: embed scopeid */
343 	if (in6_embedscope(&sin6->sin6_addr, sin6, in6p, &ifp) != 0)
344 		return EINVAL;
345 
346 	/* Source address selection. */
347 	if (IN6_IS_ADDR_V4MAPPED(&in6p->in6p_laddr)
348 	 && in6p->in6p_laddr.s6_addr32[3] == 0) {
349 #ifdef INET
350 		struct sockaddr_in sin, *sinp;
351 
352 		bzero(&sin, sizeof(sin));
353 		sin.sin_len = sizeof(sin);
354 		sin.sin_family = AF_INET;
355 		bcopy(&sin6->sin6_addr.s6_addr32[3], &sin.sin_addr,
356 			sizeof(sin.sin_addr));
357 		sinp = in_selectsrc(&sin, (struct route *)&in6p->in6p_route,
358 			in6p->in6p_socket->so_options, NULL, &error);
359 		if (sinp == 0) {
360 			if (error == 0)
361 				error = EADDRNOTAVAIL;
362 			return (error);
363 		}
364 		bzero(&mapped, sizeof(mapped));
365 		mapped.s6_addr16[5] = htons(0xffff);
366 		bcopy(&sinp->sin_addr, &mapped.s6_addr32[3], sizeof(sinp->sin_addr));
367 		in6a = &mapped;
368 #else
369 		return EADDRNOTAVAIL;
370 #endif
371 	} else
372 	{
373 		/*
374 		 * XXX: in6_selectsrc might replace the bound local address
375 		 * with the address specified by setsockopt(IPV6_PKTINFO).
376 		 * Is it the intended behavior?
377 		 */
378 		in6a = in6_selectsrc(sin6, in6p->in6p_outputopts,
379 				     in6p->in6p_moptions,
380 				     &in6p->in6p_route,
381 				     &in6p->in6p_laddr, &error);
382 		if (in6a == 0) {
383 			if (error == 0)
384 				error = EADDRNOTAVAIL;
385 			return (error);
386 		}
387 	}
388 	if (in6p->in6p_route.ro_rt)
389 		ifp = in6p->in6p_route.ro_rt->rt_ifp;
390 
391 	in6p->in6p_ip6.ip6_hlim = (u_int8_t)in6_selecthlim(in6p, ifp);
392 
393 	if (in6_pcblookup(in6p->in6p_head,
394 			 &sin6->sin6_addr,
395 			 sin6->sin6_port,
396 			 IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr) ?
397 			  in6a : &in6p->in6p_laddr,
398 			 in6p->in6p_lport,
399 			 0))
400 		return (EADDRINUSE);
401 	if (IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr)
402 	 || (IN6_IS_ADDR_V4MAPPED(&in6p->in6p_laddr)
403 	  && in6p->in6p_laddr.s6_addr32[3] == 0))
404 	{
405 		/*
406 		 * XXX in IPv4 mapped address case, we should grab fresh
407 		 * local port number by in_pcbbind, not in6_pcbbind.
408 		 * if we are in bad luck, we may assign conflicting port number
409 		 * between IPv4 and IPv6 unwillingly.
410 		 */
411 		if (in6p->in6p_lport == 0) {
412 			(void)in6_pcbbind(in6p, (struct mbuf *)0,
413 			    (struct proc *)0);
414 		}
415 		in6p->in6p_laddr = *in6a;
416 	}
417 	in6p->in6p_faddr = sin6->sin6_addr;
418 	in6p->in6p_fport = sin6->sin6_port;
419 	/*
420 	 * xxx kazu flowlabel is necessary for connect?
421 	 * but if this line is missing, the garbage value remains.
422 	 */
423 	in6p->in6p_flowinfo = sin6->sin6_flowinfo;
424 #ifdef IPSEC
425 	if (in6p->in6p_socket->so_type == SOCK_STREAM)
426 		ipsec_pcbconn(in6p->in6p_sp);
427 #endif
428 	return (0);
429 }
430 
431 void
432 in6_pcbdisconnect(in6p)
433 	struct in6pcb *in6p;
434 {
435 	bzero((caddr_t)&in6p->in6p_faddr, sizeof(in6p->in6p_faddr));
436 	in6p->in6p_fport = 0;
437 	if (in6p->in6p_socket->so_state & SS_NOFDREF)
438 		in6_pcbdetach(in6p);
439 #ifdef IPSEC
440 	ipsec_pcbdisconn(in6p->in6p_sp);
441 #endif
442 }
443 
444 void
445 in6_pcbdetach(in6p)
446 	struct in6pcb *in6p;
447 {
448 	struct socket *so = in6p->in6p_socket;
449 	int s;
450 
451 #ifdef IPSEC
452 	ipsec6_delete_pcbpolicy(in6p);
453 #endif /* IPSEC */
454 	sotoin6pcb(so) = 0;
455 	sofree(so);
456 	if (in6p->in6p_options)
457 		m_freem(in6p->in6p_options);
458 	if (in6p->in6p_outputopts) {
459 		if (in6p->in6p_outputopts->ip6po_rthdr &&
460 		    in6p->in6p_outputopts->ip6po_route.ro_rt)
461 			RTFREE(in6p->in6p_outputopts->ip6po_route.ro_rt);
462 		if (in6p->in6p_outputopts->ip6po_m)
463 			(void)m_free(in6p->in6p_outputopts->ip6po_m);
464 		free(in6p->in6p_outputopts, M_IP6OPT);
465 	}
466 	if (in6p->in6p_route.ro_rt)
467 		rtfree(in6p->in6p_route.ro_rt);
468 	ip6_freemoptions(in6p->in6p_moptions);
469 	s = splnet();
470 	in6p->in6p_next->in6p_prev = in6p->in6p_prev;
471 	in6p->in6p_prev->in6p_next = in6p->in6p_next;
472 	in6p->in6p_prev = NULL;
473 	splx(s);
474 	FREE(in6p, M_PCB);
475 }
476 
477 void
478 in6_setsockaddr(in6p, nam)
479 	struct in6pcb *in6p;
480 	struct mbuf *nam;
481 {
482 	struct sockaddr_in6 *sin6;
483 
484 	nam->m_len = sizeof(*sin6);
485 	sin6 = mtod(nam, struct sockaddr_in6 *);
486 	bzero((caddr_t)sin6, sizeof(*sin6));
487 	sin6->sin6_family = AF_INET6;
488 	sin6->sin6_len = sizeof(struct sockaddr_in6);
489 	sin6->sin6_port = in6p->in6p_lport;
490 	/* KAME hack: recover scopeid */
491 	(void)in6_recoverscope(sin6, &in6p->in6p_laddr, NULL);
492 }
493 
494 void
495 in6_setpeeraddr(in6p, nam)
496 	struct in6pcb *in6p;
497 	struct mbuf *nam;
498 {
499 	struct sockaddr_in6 *sin6;
500 
501 	nam->m_len = sizeof(*sin6);
502 	sin6 = mtod(nam, struct sockaddr_in6 *);
503 	bzero((caddr_t)sin6, sizeof(*sin6));
504 	sin6->sin6_family = AF_INET6;
505 	sin6->sin6_len = sizeof(struct sockaddr_in6);
506 	sin6->sin6_port = in6p->in6p_fport;
507 	/* KAME hack: recover scopeid */
508 	(void)in6_recoverscope(sin6, &in6p->in6p_faddr, NULL);
509 }
510 
511 /*
512  * Pass some notification to all connections of a protocol
513  * associated with address dst.  The local address and/or port numbers
514  * may be specified to limit the search.  The "usual action" will be
515  * taken, depending on the ctlinput cmd.  The caller must filter any
516  * cmds that are uninteresting (e.g., no error in the map).
517  * Call the protocol specific routine (if any) to report
518  * any errors for each matching socket.
519  *
520  * Must be called at splsoftnet.
521  *
522  * Note: src (4th arg) carries the flowlabel value on the original IPv6
523  * header, in sin6_flowinfo member.
524  */
525 int
526 in6_pcbnotify(head, dst, fport_arg, src, lport_arg, cmd, cmdarg, notify)
527 	struct in6pcb *head;
528 	struct sockaddr *dst, *src;
529 	u_int fport_arg, lport_arg;
530 	int cmd;
531 	void *cmdarg;
532 	void (*notify) __P((struct in6pcb *, int));
533 {
534 	struct in6pcb *in6p, *nin6p;
535 	struct sockaddr_in6 sa6_src, *sa6_dst;
536 	u_int16_t fport = fport_arg, lport = lport_arg;
537 	int errno;
538 	int nmatch = 0;
539 	u_int32_t flowinfo;
540 
541 	if ((unsigned)cmd > PRC_NCMDS || dst->sa_family != AF_INET6)
542 		return 0;
543 
544 	sa6_dst = (struct sockaddr_in6 *)dst;
545 	if (IN6_IS_ADDR_UNSPECIFIED(&sa6_dst->sin6_addr))
546 		return 0;
547 
548 	/*
549 	 * note that src can be NULL when we get notify by local fragmentation.
550 	 */
551 	sa6_src = (src == NULL) ? sa6_any : *(struct sockaddr_in6 *)src;
552 	flowinfo = sa6_src.sin6_flowinfo;
553 
554 	/*
555 	 * Redirects go to all references to the destination,
556 	 * and use in6_rtchange to invalidate the route cache.
557 	 * Dead host indications: also use in6_rtchange to invalidate
558 	 * the cache, and deliver the error to all the sockets.
559 	 * Otherwise, if we have knowledge of the local port and address,
560 	 * deliver only to that socket.
561 	 */
562 	if (PRC_IS_REDIRECT(cmd) || cmd == PRC_HOSTDEAD) {
563 		fport = 0;
564 		lport = 0;
565 		bzero((caddr_t)&sa6_src.sin6_addr, sizeof(sa6_src.sin6_addr));
566 
567 		if (cmd != PRC_HOSTDEAD)
568 			notify = in6_rtchange;
569 	}
570 
571 	errno = inet6ctlerrmap[cmd];
572 	for (in6p = head->in6p_next; in6p != head; in6p = nin6p) {
573 		nin6p = in6p->in6p_next;
574 
575 		/*
576 		 * Under the following condition, notify of redirects
577 		 * to the pcb, without making address matches against inpcb.
578 		 * - redirect notification is arrived.
579 		 * - the inpcb is unconnected.
580 		 * - the inpcb is caching !RTF_HOST routing entry.
581 		 * - the ICMPv6 notification is from the gateway cached in the
582 		 *   inpcb.  i.e. ICMPv6 notification is from nexthop gateway
583 		 *   the inpcb used very recently.
584 		 *
585 		 * This is to improve interaction between netbsd/openbsd
586 		 * redirect handling code, and inpcb route cache code.
587 		 * without the clause, !RTF_HOST routing entry (which carries
588 		 * gateway used by inpcb right before the ICMPv6 redirect)
589 		 * will be cached forever in unconnected inpcb.
590 		 *
591 		 * There still is a question regarding to what is TRT:
592 		 * - On bsdi/freebsd, RTF_HOST (cloned) routing entry will be
593 		 *   generated on packet output.  inpcb will always cache
594 		 *   RTF_HOST routing entry so there's no need for the clause
595 		 *   (ICMPv6 redirect will update RTF_HOST routing entry,
596 		 *   and inpcb is caching it already).
597 		 *   However, bsdi/freebsd are vulnerable to local DoS attacks
598 		 *   due to the cloned routing entries.
599 		 * - Specwise, "destination cache" is mentioned in RFC2461.
600 		 *   Jinmei says that it implies bsdi/freebsd behavior, itojun
601 		 *   is not really convinced.
602 		 * - Having hiwat/lowat on # of cloned host route (redirect/
603 		 *   pmtud) may be a good idea.  netbsd/openbsd has it.  see
604 		 *   icmp6_mtudisc_update().
605 		 */
606 		if ((PRC_IS_REDIRECT(cmd) || cmd == PRC_HOSTDEAD) &&
607 		    IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr) &&
608 		    in6p->in6p_route.ro_rt &&
609 		    !(in6p->in6p_route.ro_rt->rt_flags & RTF_HOST)) {
610 			struct sockaddr_in6 *dst6;
611 
612 			dst6 = (struct sockaddr_in6 *)&in6p->in6p_route.ro_dst;
613 			if (IN6_ARE_ADDR_EQUAL(&dst6->sin6_addr,
614 			    &sa6_dst->sin6_addr))
615 				goto do_notify;
616 		}
617 
618 		/*
619 		 * Detect if we should notify the error. If no source and
620 		 * destination ports are specified, but non-zero flowinfo and
621 		 * local address match, notify the error. This is the case
622 		 * when the error is delivered with an encrypted buffer
623 		 * by ESP. Otherwise, just compare addresses and ports
624 		 * as usual.
625 		 */
626 		if (lport == 0 && fport == 0 && flowinfo &&
627 		    in6p->in6p_socket != NULL &&
628 		    flowinfo == (in6p->in6p_flowinfo & IPV6_FLOWLABEL_MASK) &&
629 		    IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr, &sa6_src.sin6_addr))
630 			goto do_notify;
631 		else if (!IN6_ARE_ADDR_EQUAL(&in6p->in6p_faddr,
632 					     &sa6_dst->sin6_addr) ||
633 		    in6p->in6p_socket == 0 ||
634 		    (lport && in6p->in6p_lport != lport) ||
635 		    (!IN6_IS_ADDR_UNSPECIFIED(&sa6_src.sin6_addr) &&
636 		     !IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr,
637 					 &sa6_src.sin6_addr)) ||
638 		    (fport && in6p->in6p_fport != fport))
639 			continue;
640 
641 	  do_notify:
642 		if (notify)
643 			(*notify)(in6p, errno);
644 		nmatch++;
645 	}
646 	return nmatch;
647 }
648 
649 void
650 in6_pcbpurgeif0(head, ifp)
651 	struct in6pcb *head;
652 	struct ifnet *ifp;
653 {
654 	struct in6pcb *in6p, *nin6p;
655 	struct ip6_moptions *im6o;
656 	struct in6_multi_mship *imm, *nimm;
657 
658 	for (in6p = head->in6p_next; in6p != head; in6p = nin6p) {
659 		nin6p = in6p->in6p_next;
660 		im6o = in6p->in6p_moptions;
661 		if (im6o) {
662 			/*
663 			 * Unselect the outgoing interface if it is being
664 			 * detached.
665 			 */
666 			if (im6o->im6o_multicast_ifp == ifp)
667 				im6o->im6o_multicast_ifp = NULL;
668 
669 			/*
670 			 * Drop multicast group membership if we joined
671 			 * through the interface being detached.
672 			 * XXX controversial - is it really legal for kernel
673 			 * to force this?
674 			 */
675 			for (imm = im6o->im6o_memberships.lh_first;
676 			     imm != NULL; imm = nimm) {
677 				nimm = imm->i6mm_chain.le_next;
678 				if (imm->i6mm_maddr->in6m_ifp == ifp) {
679 					LIST_REMOVE(imm, i6mm_chain);
680 					in6_leavegroup(imm);
681 				}
682 			}
683 		}
684 	}
685 }
686 
687 void
688 in6_pcbpurgeif(head, ifp)
689 	struct in6pcb *head;
690 	struct ifnet *ifp;
691 {
692 	struct in6pcb *in6p, *nin6p;
693 
694 	for (in6p = head->in6p_next; in6p != head; in6p = nin6p) {
695 		nin6p = in6p->in6p_next;
696 		if (in6p->in6p_route.ro_rt != NULL &&
697 		    in6p->in6p_route.ro_rt->rt_ifp == ifp)
698 			in6_rtchange(in6p, 0);
699 	}
700 }
701 
702 /*
703  * Check for alternatives when higher level complains
704  * about service problems.  For now, invalidate cached
705  * routing information.  If the route was created dynamically
706  * (by a redirect), time to try a default gateway again.
707  */
708 void
709 in6_losing(in6p)
710 	struct in6pcb *in6p;
711 {
712 	struct rtentry *rt;
713 	struct rt_addrinfo info;
714 
715 	if ((rt = in6p->in6p_route.ro_rt) != NULL) {
716 		in6p->in6p_route.ro_rt = 0;
717 		bzero((caddr_t)&info, sizeof(info));
718 		info.rti_info[RTAX_DST] =
719 			(struct sockaddr *)&in6p->in6p_route.ro_dst;
720 		info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
721 		info.rti_info[RTAX_NETMASK] = rt_mask(rt);
722 		rt_missmsg(RTM_LOSING, &info, rt->rt_flags, 0);
723 		if (rt->rt_flags & RTF_DYNAMIC) {
724 			(void)rtrequest(RTM_DELETE, rt_key(rt),
725 					rt->rt_gateway, rt_mask(rt), rt->rt_flags,
726 					(struct rtentry **)0);
727 		} else {
728 			/*
729 			 * A new route can be allocated
730 			 * the next time output is attempted.
731 			 */
732 			rtfree(rt);
733 		}
734 	}
735 }
736 
737 /*
738  * After a routing change, flush old routing
739  * and allocate a (hopefully) better one.
740  */
741 void
742 in6_rtchange(in6p, errno)
743 	struct in6pcb *in6p;
744 	int errno;
745 {
746 	if (in6p->in6p_route.ro_rt) {
747 		rtfree(in6p->in6p_route.ro_rt);
748 		in6p->in6p_route.ro_rt = 0;
749 		/*
750 		 * A new route can be allocated the next time
751 		 * output is attempted.
752 		 */
753 	}
754 }
755 
756 struct in6pcb *
757 in6_pcblookup(head, faddr6, fport_arg, laddr6, lport_arg, flags)
758 	struct in6pcb *head;
759 	struct in6_addr *faddr6, *laddr6;
760 	u_int fport_arg, lport_arg;
761 	int flags;
762 {
763 	struct in6pcb *in6p, *match = 0;
764 	int matchwild = 3, wildcard;
765 	u_int16_t fport = fport_arg, lport = lport_arg;
766 
767 	for (in6p = head->in6p_next; in6p != head; in6p = in6p->in6p_next) {
768 		if (in6p->in6p_lport != lport)
769 			continue;
770 		wildcard = 0;
771 		if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr)) {
772 			if (IN6_IS_ADDR_UNSPECIFIED(laddr6))
773 				wildcard++;
774 			else if (!IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr, laddr6))
775 				continue;
776 		}
777 		else if (IN6_IS_ADDR_V4MAPPED(&in6p->in6p_laddr)
778 			&& in6p->in6p_laddr.s6_addr32[3] == 0) {
779 			if (!IN6_IS_ADDR_V4MAPPED(laddr6))
780 				continue;
781 			if (laddr6->s6_addr32[3] == 0)
782 				;
783 			else
784 				wildcard++;
785 		}
786 		else {
787 			if (IN6_IS_ADDR_V4MAPPED(laddr6)) {
788 				if (in6p->in6p_flags & IN6P_IPV6_V6ONLY)
789 					continue;
790 				else
791 					wildcard++;
792 			} else if (!IN6_IS_ADDR_UNSPECIFIED(laddr6))
793 				wildcard++;
794 		}
795 		if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr)) {
796 			if (IN6_IS_ADDR_UNSPECIFIED(faddr6))
797 				wildcard++;
798 			else if (!IN6_ARE_ADDR_EQUAL(&in6p->in6p_faddr, faddr6)
799 			      || in6p->in6p_fport != fport)
800 				continue;
801 		}
802 		else if (IN6_IS_ADDR_V4MAPPED(&in6p->in6p_faddr)
803 			&& in6p->in6p_faddr.s6_addr32[3] == 0) {
804 			if (!IN6_IS_ADDR_V4MAPPED(faddr6))
805 				continue;
806 			if (faddr6->s6_addr32[3] == 0)
807 				;
808 			else
809 				wildcard++;
810 		}
811 		else {
812 			if (IN6_IS_ADDR_V4MAPPED(faddr6)) {
813 				if (in6p->in6p_flags & IN6P_IPV6_V6ONLY)
814 					continue;
815 				else
816 					wildcard++;
817 			} else if (!IN6_IS_ADDR_UNSPECIFIED(faddr6))
818 				wildcard++;
819 		}
820 
821 		if (wildcard && (flags & IN6PLOOKUP_WILDCARD) == 0)
822 			continue;
823 		if (wildcard < matchwild) {
824 			match = in6p;
825 			matchwild = wildcard;
826 			if (matchwild == 0)
827 				break;
828 		}
829 	}
830 	return (match);
831 }
832 
833 struct rtentry *
834 in6_pcbrtentry(in6p)
835 	struct in6pcb *in6p;
836 {
837 	struct route_in6 *ro;
838 	struct sockaddr_in6 *dst6;
839 
840 	ro = &in6p->in6p_route;
841 	dst6 = (struct sockaddr_in6 *)&ro->ro_dst;
842 
843 	if (ro->ro_rt && ((ro->ro_rt->rt_flags & RTF_UP) == 0 ||
844 	    !IN6_ARE_ADDR_EQUAL(&dst6->sin6_addr, &in6p->in6p_faddr))) {
845 		RTFREE(ro->ro_rt);
846 		ro->ro_rt = (struct rtentry *)NULL;
847 	}
848 	if (ro->ro_rt == (struct rtentry *)NULL &&
849 	    !IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr)) {
850 		bzero(dst6, sizeof(*dst6));
851 		dst6->sin6_family = AF_INET6;
852 		dst6->sin6_len = sizeof(struct sockaddr_in6);
853 		dst6->sin6_addr = in6p->in6p_faddr;
854 		rtalloc((struct route *)ro);
855 	}
856 	return (ro->ro_rt);
857 }
858 
859 struct in6pcb *
860 in6_pcblookup_connect(head, faddr6, fport_arg, laddr6, lport_arg, faith)
861 	struct in6pcb *head;
862 	struct in6_addr *faddr6, *laddr6;
863 	u_int fport_arg, lport_arg;
864 	int faith;
865 {
866 	struct in6pcb *in6p;
867 	u_int16_t fport = fport_arg, lport = lport_arg;
868 
869 	for (in6p = head->in6p_next; in6p != head; in6p = in6p->in6p_next) {
870 		/* find exact match on both source and dest */
871 		if (in6p->in6p_fport != fport)
872 			continue;
873 		if (in6p->in6p_lport != lport)
874 			continue;
875 		if (IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr))
876 			continue;
877 		if (!IN6_ARE_ADDR_EQUAL(&in6p->in6p_faddr, faddr6))
878 			continue;
879 		if (IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr))
880 			continue;
881 		if (!IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr, laddr6))
882 			continue;
883 		if ((IN6_IS_ADDR_V4MAPPED(laddr6) ||
884 		     IN6_IS_ADDR_V4MAPPED(faddr6)) &&
885 		    (in6p->in6p_flags & IN6P_IPV6_V6ONLY))
886 			continue;
887 		return in6p;
888 	}
889 	return NULL;
890 }
891 
892 struct in6pcb *
893 in6_pcblookup_bind(head, laddr6, lport_arg, faith)
894 	struct in6pcb *head;
895 	struct in6_addr *laddr6;
896 	u_int lport_arg;
897 	int faith;
898 {
899 	struct in6pcb *in6p, *match;
900 	u_int16_t lport = lport_arg;
901 
902 	match = NULL;
903 	for (in6p = head->in6p_next; in6p != head; in6p = in6p->in6p_next) {
904 		/*
905 	 	 * find destination match.  exact match is preferred
906 		 * against wildcard match.
907 		 */
908 		if (faith && (in6p->in6p_flags & IN6P_FAITH) == 0)
909 			continue;
910 		if (in6p->in6p_fport != 0)
911 			continue;
912 		if (in6p->in6p_lport != lport)
913 			continue;
914 		if (IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr)) {
915 			if (IN6_IS_ADDR_V4MAPPED(laddr6)) {
916 				if ((in6p->in6p_flags & IN6P_IPV6_V6ONLY))
917 					continue;
918 				else
919 					match = in6p;
920 			} else
921 				match = in6p;
922 		}
923 		else if (IN6_IS_ADDR_V4MAPPED(&in6p->in6p_laddr) &&
924 			 !(in6p->in6p_flags & IN6P_IPV6_V6ONLY) &&
925 			 in6p->in6p_laddr.s6_addr32[3] == 0) {
926 			if (IN6_IS_ADDR_V4MAPPED(laddr6) &&
927 			    laddr6->s6_addr32[3] != 0)
928 				match = in6p;
929 		}
930 		else if (IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr, laddr6))
931 			return in6p;
932 	}
933 	return match;
934 }
935