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