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