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