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