in6pcb: in6_pcblookup_local() must have been protected by port token
[dragonfly.git] / sys / netinet6 / in6_pcb.c
CommitLineData
984263bc
MD
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 $ */
bde3511a 3
984263bc
MD
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.
dc71b7ab 46 * 3. Neither the name of the University nor the names of its contributors
984263bc
MD
47 * may be used to endorse or promote products derived from this software
48 * without specific prior written permission.
49 *
50 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
51 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
52 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
53 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
54 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
55 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
56 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
57 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
58 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
59 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
60 * SUCH DAMAGE.
61 *
62 * @(#)in_pcb.c 8.2 (Berkeley) 1/4/94
63 */
64
65#include "opt_inet.h"
66#include "opt_inet6.h"
67#include "opt_ipsec.h"
68
69#include <sys/param.h>
70#include <sys/systm.h>
71#include <sys/malloc.h>
72#include <sys/mbuf.h>
73#include <sys/domain.h>
74#include <sys/protosw.h>
75#include <sys/socket.h>
76#include <sys/socketvar.h>
77#include <sys/sockio.h>
78#include <sys/errno.h>
79#include <sys/time.h>
80#include <sys/proc.h>
895c1f85 81#include <sys/priv.h>
984263bc 82#include <sys/jail.h>
002c1265 83
abf782b3 84#include <sys/thread2.h>
002c1265 85#include <sys/msgport2.h>
984263bc
MD
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
d2438d69 112#include <netproto/key/key.h>
984263bc
MD
113#endif /* IPSEC */
114
115#ifdef FAST_IPSEC
bf844ffa
JH
116#include <netproto/ipsec/ipsec.h>
117#include <netproto/ipsec/ipsec6.h>
118#include <netproto/ipsec/key.h>
984263bc
MD
119#define IPSEC
120#endif /* FAST_IPSEC */
121
122struct in6_addr zeroin6_addr;
123
124int
dadab5e9 125in6_pcbbind(struct inpcb *inp, struct sockaddr *nam, struct thread *td)
984263bc
MD
126{
127 struct socket *so = inp->inp_socket;
2038fb68 128 struct sockaddr_in6 *sin6 = NULL;
3e4150ef 129 struct sockaddr_in6 jsin6;
984263bc 130 struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
3e4150ef
VBD
131 struct proc *p = td->td_proc;
132 struct ucred *cred = NULL;
984263bc
MD
133 u_short lport = 0;
134 int wild = 0, reuseport = (so->so_options & SO_REUSEPORT);
8d976fd7 135 int error;
984263bc
MD
136
137 if (!in6_ifaddr) /* XXX broken! */
138 return (EADDRNOTAVAIL);
139 if (inp->inp_lport || !IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr))
bde3511a 140 return (EINVAL);
984263bc
MD
141 if ((so->so_options & (SO_REUSEADDR|SO_REUSEPORT)) == 0)
142 wild = 1;
3e4150ef
VBD
143 if (p)
144 cred = p->p_ucred;
8d976fd7
SZ
145
146 /*
147 * This has to be atomic. If the porthash is shared across multiple
148 * protocol threads (aka tcp) then the token will be non-NULL.
149 */
150 if (pcbinfo->porttoken)
151 lwkt_gettoken(pcbinfo->porttoken);
152
984263bc
MD
153 if (nam) {
154 sin6 = (struct sockaddr_in6 *)nam;
8d976fd7
SZ
155 if (nam->sa_len != sizeof(*sin6)) {
156 error = EINVAL;
157 goto done;
158 }
984263bc
MD
159 /*
160 * family check.
161 */
8d976fd7
SZ
162 if (nam->sa_family != AF_INET6) {
163 error = EAFNOSUPPORT;
164 goto done;
165 }
984263bc 166
8d976fd7
SZ
167 if (!prison_replace_wildcards(td, nam)) {
168 error = EINVAL;
169 goto done;
170 }
3e4150ef 171
984263bc 172 /* KAME hack: embed scopeid */
8d976fd7
SZ
173 if (in6_embedscope(&sin6->sin6_addr, sin6, inp, NULL) != 0) {
174 error = EINVAL;
175 goto done;
176 }
984263bc
MD
177 /* this must be cleared for ifa_ifwithaddr() */
178 sin6->sin6_scope_id = 0;
179
180 lport = sin6->sin6_port;
181 if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) {
182 /*
183 * Treat SO_REUSEADDR as SO_REUSEPORT for multicast;
184 * allow compepte duplication of binding if
185 * SO_REUSEPORT is set, or if SO_REUSEADDR is set
186 * and a multicast address is bound on both
187 * new and duplicated sockets.
188 */
189 if (so->so_options & SO_REUSEADDR)
190 reuseport = SO_REUSEADDR|SO_REUSEPORT;
191 } else if (!IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
192 struct ifaddr *ia = NULL;
193
194 sin6->sin6_port = 0; /* yech... */
3e4150ef
VBD
195 if (!prison_replace_wildcards(td, (struct sockaddr *)sin6)) {
196 sin6->sin6_addr = kin6addr_any;
8d976fd7
SZ
197 error = EINVAL;
198 goto done;
199 }
200 if ((ia = ifa_ifwithaddr((struct sockaddr *)sin6)) == NULL) {
201 error = EADDRNOTAVAIL;
202 goto done;
3e4150ef 203 }
984263bc
MD
204
205 /*
206 * XXX: bind to an anycast address might accidentally
207 * cause sending a packet with anycast source address.
208 * We should allow to bind to a deprecated address, since
209 * the application dare to use it.
210 */
211 if (ia &&
212 ((struct in6_ifaddr *)ia)->ia6_flags &
213 (IN6_IFF_ANYCAST|IN6_IFF_NOTREADY|IN6_IFF_DETACHED)) {
8d976fd7
SZ
214 error = EADDRNOTAVAIL;
215 goto done;
984263bc
MD
216 }
217 }
218 if (lport) {
219 struct inpcb *t;
220
221 /* GROSS */
3e4150ef 222 if (ntohs(lport) < IPV6PORT_RESERVED && cred &&
8d976fd7
SZ
223 priv_check_cred(cred, PRIV_NETINET_RESERVEDPORT, 0)) {
224 error = EACCES;
225 goto done;
226 }
984263bc
MD
227 if (so->so_cred->cr_uid != 0 &&
228 !IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) {
229 t = in6_pcblookup_local(pcbinfo,
230 &sin6->sin6_addr, lport,
3e4150ef 231 INPLOOKUP_WILDCARD, cred);
984263bc
MD
232 if (t &&
233 (!IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr) ||
234 !IN6_IS_ADDR_UNSPECIFIED(&t->in6p_laddr) ||
235 (t->inp_socket->so_options &
236 SO_REUSEPORT) == 0) &&
237 (so->so_cred->cr_uid !=
8d976fd7
SZ
238 t->inp_socket->so_cred->cr_uid)) {
239 error = EADDRINUSE;
240 goto done;
241 }
984263bc
MD
242 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0 &&
243 IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
244 struct sockaddr_in sin;
245
246 in6_sin6_2_sin(&sin, sin6);
247 t = in_pcblookup_local(pcbinfo,
248 sin.sin_addr, lport,
3e4150ef 249 INPLOOKUP_WILDCARD, cred);
984263bc
MD
250 if (t &&
251 (so->so_cred->cr_uid !=
252 t->inp_socket->so_cred->cr_uid) &&
253 (ntohl(t->inp_laddr.s_addr) !=
254 INADDR_ANY ||
255 INP_SOCKAF(so) ==
8d976fd7
SZ
256 INP_SOCKAF(t->inp_socket))) {
257 error = EADDRINUSE;
258 goto done;
259 }
984263bc
MD
260 }
261 }
3e4150ef 262 if (cred && cred->cr_prison &&
8d976fd7
SZ
263 !prison_replace_wildcards(td, nam)) {
264 error = EADDRNOTAVAIL;
265 goto done;
266 }
984263bc 267 t = in6_pcblookup_local(pcbinfo, &sin6->sin6_addr,
3e4150ef 268 lport, wild, cred);
8d976fd7
SZ
269 if (t && (reuseport & t->inp_socket->so_options) == 0) {
270 error = EADDRINUSE;
271 goto done;
272 }
984263bc
MD
273 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0 &&
274 IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
275 struct sockaddr_in sin;
276
277 in6_sin6_2_sin(&sin, sin6);
3e4150ef
VBD
278 t = in_pcblookup_local(pcbinfo,
279 sin.sin_addr, lport,
280 wild, cred);
984263bc
MD
281 if (t &&
282 (reuseport & t->inp_socket->so_options)
283 == 0 &&
284 (ntohl(t->inp_laddr.s_addr)
285 != INADDR_ANY ||
286 INP_SOCKAF(so) ==
8d976fd7
SZ
287 INP_SOCKAF(t->inp_socket))) {
288 error = EADDRINUSE;
289 goto done;
290 }
984263bc
MD
291 }
292 }
293 inp->in6p_laddr = sin6->sin6_addr;
294 }
295 if (lport == 0) {
296 int e;
3e4150ef
VBD
297
298 jsin6.sin6_addr = inp->in6p_laddr;
299 jsin6.sin6_family = AF_INET6;
300 if (!prison_replace_wildcards(td, (struct sockaddr*)&jsin6)) {
301 inp->in6p_laddr = kin6addr_any;
302 inp->inp_lport = 0;
8d976fd7
SZ
303 error = EINVAL;
304 goto done;
3e4150ef
VBD
305 }
306
8d976fd7
SZ
307 if ((e = in6_pcbsetport(&inp->in6p_laddr, inp, td)) != 0) {
308 error = e;
309 goto done;
310 }
984263bc
MD
311 }
312 else {
313 inp->inp_lport = lport;
13d8907a 314 if (in_pcbinsporthash(inp) != 0) {
84204577 315 inp->in6p_laddr = kin6addr_any;
984263bc 316 inp->inp_lport = 0;
8d976fd7
SZ
317 error = EAGAIN;
318 goto done;
984263bc
MD
319 }
320 }
8d976fd7
SZ
321 error = 0;
322done:
323 if (pcbinfo->porttoken)
324 lwkt_reltoken(pcbinfo->porttoken);
325 return error;
984263bc
MD
326}
327
328/*
329 * Transform old in6_pcbconnect() into an inner subroutine for new
330 * in6_pcbconnect(): Do some validity-checking on the remote
331 * address (in mbuf 'nam') and then determine local host address
332 * (i.e., which interface) to use to access that remote host.
333 *
334 * This preserves definition of in6_pcbconnect(), while supporting a
335 * slightly different version for T/TCP. (This is more than
336 * a bit of a kludge, but cleaning up the internal interfaces would
337 * have forced minor changes in every protocol).
338 */
339
340int
dadab5e9 341in6_pcbladdr(struct inpcb *inp, struct sockaddr *nam,
3e4150ef 342 struct in6_addr **plocal_addr6, struct thread *td)
984263bc 343{
dadab5e9 344 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)nam;
984263bc
MD
345 struct ifnet *ifp = NULL;
346 int error = 0;
347
348 if (nam->sa_len != sizeof (*sin6))
349 return (EINVAL);
350 if (sin6->sin6_family != AF_INET6)
351 return (EAFNOSUPPORT);
352 if (sin6->sin6_port == 0)
353 return (EADDRNOTAVAIL);
354
355 /* KAME hack: embed scopeid */
356 if (in6_embedscope(&sin6->sin6_addr, sin6, inp, &ifp) != 0)
357 return EINVAL;
358
359 if (in6_ifaddr) {
360 /*
361 * If the destination address is UNSPECIFIED addr,
362 * use the loopback addr, e.g ::1.
363 */
364 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr))
84204577 365 sin6->sin6_addr = kin6addr_loopback;
984263bc
MD
366 }
367 {
368 /*
369 * XXX: in6_selectsrc might replace the bound local address
370 * with the address specified by setsockopt(IPV6_PKTINFO).
371 * Is it the intended behavior?
372 */
373 *plocal_addr6 = in6_selectsrc(sin6, inp->in6p_outputopts,
374 inp->in6p_moptions,
375 &inp->in6p_route,
3e4150ef 376 &inp->in6p_laddr, &error, td);
4090d6ff 377 if (*plocal_addr6 == NULL) {
984263bc
MD
378 if (error == 0)
379 error = EADDRNOTAVAIL;
bde3511a 380 return (error);
984263bc
MD
381 }
382 /*
383 * Don't do pcblookup call here; return interface in
384 * plocal_addr6
385 * and exit to caller, that will do the lookup.
386 */
387 }
388
389 if (inp->in6p_route.ro_rt)
390 ifp = inp->in6p_route.ro_rt->rt_ifp;
391
bde3511a 392 return (0);
984263bc
MD
393}
394
395/*
396 * Outer subroutine:
397 * Connect from a socket to a specified address.
398 * Both address and port must be specified in argument sin.
399 * If don't have a local address for this socket yet,
400 * then pick one.
401 */
402int
dadab5e9 403in6_pcbconnect(struct inpcb *inp, struct sockaddr *nam, struct thread *td)
984263bc
MD
404{
405 struct in6_addr *addr6;
34ee9825 406 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)nam;
984263bc
MD
407 int error;
408
409 /*
410 * Call inner routine, to assign local interface address.
411 * in6_pcbladdr() may automatically fill in sin6_scope_id.
412 */
3e4150ef 413 if ((error = in6_pcbladdr(inp, nam, &addr6, td)) != 0)
bde3511a 414 return (error);
984263bc 415
7fe56515 416 if (in6_pcblookup_hash(inp->inp_cpcbinfo, &sin6->sin6_addr,
984263bc
MD
417 sin6->sin6_port,
418 IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)
419 ? addr6 : &inp->in6p_laddr,
420 inp->inp_lport, 0, NULL) != NULL) {
421 return (EADDRINUSE);
422 }
423 if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)) {
424 if (inp->inp_lport == 0) {
60233e58 425 error = in6_pcbbind(inp, NULL, td);
984263bc
MD
426 if (error)
427 return (error);
428 }
429 inp->in6p_laddr = *addr6;
430 }
431 inp->in6p_faddr = sin6->sin6_addr;
432 inp->inp_fport = sin6->sin6_port;
433 /* update flowinfo - draft-itojun-ipv6-flowlabel-api-00 */
434 inp->in6p_flowinfo &= ~IPV6_FLOWLABEL_MASK;
435 if (inp->in6p_flags & IN6P_AUTOFLOWLABEL)
436 inp->in6p_flowinfo |=
437 (htonl(ip6_flow_seq++) & IPV6_FLOWLABEL_MASK);
438
2322f6ca 439 in_pcbinsconnhash(inp);
984263bc
MD
440 return (0);
441}
442
443#if 0
444/*
445 * Return an IPv6 address, which is the most appropriate for given
446 * destination and user specified options.
447 * If necessary, this function lookups the routing table and return
448 * an entry to the caller for later use.
449 */
450struct in6_addr *
122ebd49
CP
451in6_selectsrc(struct sockaddr_in6 *dstsock, struct ip6_pktopts *opts,
452 struct ip6_moptions *mopts, struct route_in6 *ro,
3e4150ef 453 struct in6_addr *laddr, int *errorp, struct thread *td)
984263bc 454{
3e4150ef
VBD
455 struct sockaddr_in6 jsin6;
456 struct ucred *cred = NULL;
984263bc
MD
457 struct in6_addr *dst;
458 struct in6_ifaddr *ia6 = 0;
459 struct in6_pktinfo *pi = NULL;
3e4150ef 460 int jailed = 0;
984263bc 461
3e4150ef
VBD
462 if (td && td->td_proc && td->td_proc->p_ucred)
463 cred = td->td_proc->p_ucred;
464 if (cred && cred->cr_prison)
465 jailed = 1;
466 jsin6.sin6_family = AF_INET6;
984263bc
MD
467 dst = &dstsock->sin6_addr;
468 *errorp = 0;
469
470 /*
471 * If the source address is explicitly specified by the caller,
472 * use it.
473 */
474 if (opts && (pi = opts->ip6po_pktinfo) &&
3e4150ef
VBD
475 !IN6_IS_ADDR_UNSPECIFIED(&pi->ipi6_addr)) {
476 jsin6.sin6_addr = pi->ipi6_addr;
477 if (jailed && !jailed_ip(cred->cr_prison,
478 (struct sockaddr *)&jsin6)) {
479 return(0);
480 } else {
481 return (&pi->ipi6_addr);
482 }
483 }
984263bc
MD
484
485 /*
486 * If the source address is not specified but the socket(if any)
487 * is already bound, use the bound address.
488 */
3e4150ef
VBD
489 if (laddr && !IN6_IS_ADDR_UNSPECIFIED(laddr)) {
490 jsin6.sin6_addr = *laddr;
491 if (jailed && !jailed_ip(cred->cr_prison,
492 (struct sockaddr *)&jsin6)) {
493 return(0);
494 } else {
495 return (laddr);
496 }
497 }
984263bc
MD
498
499 /*
500 * If the caller doesn't specify the source address but
501 * the outgoing interface, use an address associated with
502 * the interface.
503 */
504 if (pi && pi->ipi6_ifindex) {
505 /* XXX boundary check is assumed to be already done. */
506 ia6 = in6_ifawithscope(ifindex2ifnet[pi->ipi6_ifindex],
cfd79120 507 dst, cred);
3e4150ef
VBD
508
509 if (ia6 && jailed) {
510 jsin6.sin6_addr = (&ia6->ia_addr)->sin6_addr;
511 if (!jailed_ip(cred->cr_prison,
512 (struct sockaddr *)&jsin6))
513 ia6 = 0;
514 }
515
984263bc
MD
516 if (ia6 == 0) {
517 *errorp = EADDRNOTAVAIL;
bde3511a 518 return (0);
984263bc 519 }
bde3511a 520 return (&satosin6(&ia6->ia_addr)->sin6_addr);
984263bc
MD
521 }
522
523 /*
524 * If the destination address is a link-local unicast address or
525 * a multicast address, and if the outgoing interface is specified
526 * by the sin6_scope_id filed, use an address associated with the
527 * interface.
528 * XXX: We're now trying to define more specific semantics of
529 * sin6_scope_id field, so this part will be rewritten in
530 * the near future.
531 */
532 if ((IN6_IS_ADDR_LINKLOCAL(dst) || IN6_IS_ADDR_MULTICAST(dst)) &&
533 dstsock->sin6_scope_id) {
534 /*
535 * I'm not sure if boundary check for scope_id is done
536 * somewhere...
537 */
538 if (dstsock->sin6_scope_id < 0 ||
539 if_index < dstsock->sin6_scope_id) {
540 *errorp = ENXIO; /* XXX: better error? */
bde3511a 541 return (0);
984263bc
MD
542 }
543 ia6 = in6_ifawithscope(ifindex2ifnet[dstsock->sin6_scope_id],
cfd79120 544 dst, cred);
3e4150ef
VBD
545
546 if (ia6 && jailed) {
547 jsin6.sin6_addr = (&ia6->ia_addr)->sin6_addr;
548 if (!jailed_ip(cred->cr_prison,
549 (struct sockaddr *)&jsin6))
550 ia6 = 0;
551 }
552
984263bc
MD
553 if (ia6 == 0) {
554 *errorp = EADDRNOTAVAIL;
bde3511a 555 return (0);
984263bc 556 }
bde3511a 557 return (&satosin6(&ia6->ia_addr)->sin6_addr);
984263bc
MD
558 }
559
560 /*
561 * If the destination address is a multicast address and
562 * the outgoing interface for the address is specified
563 * by the caller, use an address associated with the interface.
564 * There is a sanity check here; if the destination has node-local
565 * scope, the outgoing interfacde should be a loopback address.
566 * Even if the outgoing interface is not specified, we also
567 * choose a loopback interface as the outgoing interface.
568 */
3e4150ef 569 if (!jailed && IN6_IS_ADDR_MULTICAST(dst)) {
984263bc
MD
570 struct ifnet *ifp = mopts ? mopts->im6o_multicast_ifp : NULL;
571
572 if (ifp == NULL && IN6_IS_ADDR_MC_NODELOCAL(dst)) {
573 ifp = &loif[0];
574 }
575
576 if (ifp) {
cfd79120 577 ia6 = in6_ifawithscope(ifp, dst, cred);
984263bc
MD
578 if (ia6 == 0) {
579 *errorp = EADDRNOTAVAIL;
bde3511a 580 return (0);
984263bc 581 }
bde3511a 582 return (&ia6->ia_addr.sin6_addr);
984263bc
MD
583 }
584 }
585
586 /*
587 * If the next hop address for the packet is specified
588 * by caller, use an address associated with the route
589 * to the next hop.
590 */
591 {
592 struct sockaddr_in6 *sin6_next;
593 struct rtentry *rt;
594
595 if (opts && opts->ip6po_nexthop) {
596 sin6_next = satosin6(opts->ip6po_nexthop);
597 rt = nd6_lookup(&sin6_next->sin6_addr, 1, NULL);
598 if (rt) {
cfd79120 599 ia6 = in6_ifawithscope(rt->rt_ifp, dst, cred);
984263bc
MD
600 if (ia6 == 0)
601 ia6 = ifatoia6(rt->rt_ifa);
602 }
3e4150ef
VBD
603 if (ia6 && jailed) {
604 jsin6.sin6_addr = (&ia6->ia_addr)->sin6_addr;
605 if (!jailed_ip(cred->cr_prison,
606 (struct sockaddr *)&jsin6))
607 ia6 = 0;
608 }
609
984263bc
MD
610 if (ia6 == 0) {
611 *errorp = EADDRNOTAVAIL;
bde3511a 612 return (0);
984263bc 613 }
bde3511a 614 return (&satosin6(&ia6->ia_addr)->sin6_addr);
984263bc
MD
615 }
616 }
617
618 /*
619 * If route is known or can be allocated now,
620 * our src addr is taken from the i/f, else punt.
621 */
622 if (ro) {
623 if (ro->ro_rt &&
624 !IN6_ARE_ADDR_EQUAL(&satosin6(&ro->ro_dst)->sin6_addr, dst)) {
625 RTFREE(ro->ro_rt);
f23061d4 626 ro->ro_rt = NULL;
984263bc 627 }
f23061d4 628 if (ro->ro_rt == NULL || ro->ro_rt->rt_ifp == NULL) {
984263bc
MD
629 struct sockaddr_in6 *dst6;
630
631 /* No route yet, so try to acquire one */
632 bzero(&ro->ro_dst, sizeof(struct sockaddr_in6));
f23061d4 633 dst6 = &ro->ro_dst;
984263bc
MD
634 dst6->sin6_family = AF_INET6;
635 dst6->sin6_len = sizeof(struct sockaddr_in6);
636 dst6->sin6_addr = *dst;
3e4150ef 637 if (!jailed && IN6_IS_ADDR_MULTICAST(dst)) {
f3ed2586
JH
638 ro->ro_rt =
639 rtpurelookup((struct sockaddr *)&ro->ro_dst);
984263bc
MD
640 } else {
641 rtalloc((struct route *)ro);
642 }
643 }
644
645 /*
646 * in_pcbconnect() checks out IFF_LOOPBACK to skip using
647 * the address. But we don't know why it does so.
648 * It is necessary to ensure the scope even for lo0
649 * so doesn't check out IFF_LOOPBACK.
650 */
651
652 if (ro->ro_rt) {
cfd79120 653 ia6 = in6_ifawithscope(ro->ro_rt->rt_ifa->ifa_ifp, dst, cred);
3e4150ef
VBD
654 if (ia6 && jailed) {
655 jsin6.sin6_addr = (&ia6->ia_addr)->sin6_addr;
656 if (!jailed_ip(cred->cr_prison,
657 (struct sockaddr *)&jsin6))
658 ia6 = 0;
659 }
660
984263bc
MD
661 if (ia6 == 0) /* xxx scope error ?*/
662 ia6 = ifatoia6(ro->ro_rt->rt_ifa);
3e4150ef
VBD
663
664 if (ia6 && jailed) {
665 jsin6.sin6_addr = (&ia6->ia_addr)->sin6_addr;
666 if (!jailed_ip(cred->cr_prison,
667 (struct sockaddr *)&jsin6))
668 ia6 = 0;
669 }
984263bc
MD
670 }
671 if (ia6 == 0) {
672 *errorp = EHOSTUNREACH; /* no route */
bde3511a 673 return (0);
984263bc 674 }
bde3511a 675 return (&satosin6(&ia6->ia_addr)->sin6_addr);
984263bc
MD
676 }
677
678 *errorp = EADDRNOTAVAIL;
bde3511a 679 return (0);
984263bc
MD
680}
681
682/*
683 * Default hop limit selection. The precedence is as follows:
684 * 1. Hoplimit valued specified via ioctl.
685 * 2. (If the outgoing interface is detected) the current
686 * hop limit of the interface specified by router advertisement.
687 * 3. The system default hoplimit.
688*/
689int
122ebd49 690in6_selecthlim(struct in6pcb *in6p, struct ifnet *ifp)
984263bc
MD
691{
692 if (in6p && in6p->in6p_hops >= 0)
bde3511a 693 return (in6p->in6p_hops);
984263bc 694 else if (ifp)
bde3511a 695 return (ND_IFINFO(ifp)->chlim);
984263bc 696 else
bde3511a 697 return (ip6_defhlim);
984263bc
MD
698}
699#endif
700
701void
122ebd49 702in6_pcbdisconnect(struct inpcb *inp)
984263bc
MD
703{
704 bzero((caddr_t)&inp->in6p_faddr, sizeof(inp->in6p_faddr));
705 inp->inp_fport = 0;
706 /* clear flowinfo - draft-itojun-ipv6-flowlabel-api-00 */
707 inp->in6p_flowinfo &= ~IPV6_FLOWLABEL_MASK;
13d8907a 708 in_pcbremconnhash(inp);
984263bc
MD
709 if (inp->inp_socket->so_state & SS_NOFDREF)
710 in6_pcbdetach(inp);
711}
712
713void
122ebd49 714in6_pcbdetach(struct inpcb *inp)
984263bc
MD
715{
716 struct socket *so = inp->inp_socket;
717 struct inpcbinfo *ipi = inp->inp_pcbinfo;
718
719#ifdef IPSEC
720 if (inp->in6p_sp != NULL)
721 ipsec6_delete_pcbpolicy(inp);
722#endif /* IPSEC */
723 inp->inp_gencnt = ++ipi->ipi_gencnt;
724 in_pcbremlists(inp);
acbbccb2 725 so->so_pcb = NULL;
0ce0603e 726 KKASSERT((so->so_state & SS_ASSERTINPROG) == 0);
6cef7136 727 sofree(so); /* remove pcb ref */
984263bc
MD
728
729 if (inp->in6p_options)
730 m_freem(inp->in6p_options);
8f706258
SW
731 ip6_freepcbopts(inp->in6p_outputopts);
732 ip6_freemoptions(inp->in6p_moptions);
984263bc
MD
733 if (inp->in6p_route.ro_rt)
734 rtfree(inp->in6p_route.ro_rt);
735 /* Check and free IPv4 related resources in case of mapped addr */
736 if (inp->inp_options)
3e0c844d 737 m_free(inp->inp_options);
984263bc
MD
738 ip_freemoptions(inp->inp_moptions);
739
740 inp->inp_vflag = 0;
9f42c129 741 kfree(inp, M_PCB);
984263bc
MD
742}
743
744/*
745 * The calling convention of in6_setsockaddr() and in6_setpeeraddr() was
746 * modified to match the pru_sockaddr() and pru_peeraddr() entry points
747 * in struct pr_usrreqs, so that protocols can just reference then directly
748 * without the need for a wrapper function. The socket must have a valid
749 * (i.e., non-nil) PCB, but it should be impossible to get an invalid one
750 * except through a kernel programming error, so it is acceptable to panic
751 * (or in this case trap) if the PCB is invalid. (Actually, we don't trap
752 * because there actually /is/ a programming error somewhere... XXX)
753 */
002c1265
MD
754void
755in6_setsockaddr_dispatch(netmsg_t msg)
756{
757 int error;
758
759 error = in6_setsockaddr(msg->sockaddr.base.nm_so, msg->sockaddr.nm_nam);
760 lwkt_replymsg(&msg->sockaddr.base.lmsg, error);
761}
762
984263bc 763int
122ebd49 764in6_setsockaddr(struct socket *so, struct sockaddr **nam)
984263bc 765{
34ee9825
RG
766 struct inpcb *inp;
767 struct sockaddr_in6 *sin6;
984263bc
MD
768
769 /*
770 * Do the malloc first in case it blocks.
771 */
884717e1 772 sin6 = kmalloc(sizeof *sin6, M_SONAME, M_WAITOK | M_ZERO);
984263bc
MD
773 sin6->sin6_family = AF_INET6;
774 sin6->sin6_len = sizeof(*sin6);
775
abf782b3 776 crit_enter();
ed894f8c 777 inp = so->so_pcb;
984263bc 778 if (!inp) {
abf782b3 779 crit_exit();
efda3bd0 780 kfree(sin6, M_SONAME);
984263bc
MD
781 return EINVAL;
782 }
783 sin6->sin6_port = inp->inp_lport;
784 sin6->sin6_addr = inp->in6p_laddr;
abf782b3 785 crit_exit();
984263bc
MD
786 if (IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr))
787 sin6->sin6_scope_id = ntohs(sin6->sin6_addr.s6_addr16[1]);
788 else
789 sin6->sin6_scope_id = 0; /*XXX*/
790 if (IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr))
791 sin6->sin6_addr.s6_addr16[1] = 0;
792
793 *nam = (struct sockaddr *)sin6;
794 return 0;
795}
796
002c1265
MD
797void
798in6_setpeeraddr_dispatch(netmsg_t msg)
799{
800 int error;
801
802 error = in6_setpeeraddr(msg->peeraddr.base.nm_so, msg->peeraddr.nm_nam);
803 lwkt_replymsg(&msg->peeraddr.base.lmsg, error);
804}
805
984263bc 806int
122ebd49 807in6_setpeeraddr(struct socket *so, struct sockaddr **nam)
984263bc 808{
984263bc 809 struct inpcb *inp;
34ee9825 810 struct sockaddr_in6 *sin6;
984263bc
MD
811
812 /*
813 * Do the malloc first in case it blocks.
814 */
884717e1 815 sin6 = kmalloc(sizeof(*sin6), M_SONAME, M_WAITOK | M_ZERO);
984263bc
MD
816 sin6->sin6_family = AF_INET6;
817 sin6->sin6_len = sizeof(struct sockaddr_in6);
818
abf782b3 819 crit_enter();
ed894f8c 820 inp = so->so_pcb;
984263bc 821 if (!inp) {
abf782b3 822 crit_exit();
efda3bd0 823 kfree(sin6, M_SONAME);
984263bc
MD
824 return EINVAL;
825 }
826 sin6->sin6_port = inp->inp_fport;
827 sin6->sin6_addr = inp->in6p_faddr;
abf782b3 828 crit_exit();
984263bc
MD
829 if (IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr))
830 sin6->sin6_scope_id = ntohs(sin6->sin6_addr.s6_addr16[1]);
831 else
832 sin6->sin6_scope_id = 0; /*XXX*/
833 if (IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr))
834 sin6->sin6_addr.s6_addr16[1] = 0;
835
836 *nam = (struct sockaddr *)sin6;
837 return 0;
838}
839
002c1265
MD
840void
841in6_mapped_sockaddr_dispatch(netmsg_t msg)
842{
843 int error;
844
845 error = in6_mapped_sockaddr(msg->sockaddr.base.nm_so,
846 msg->sockaddr.nm_nam);
847 lwkt_replymsg(&msg->sockaddr.base.lmsg, error);
848}
849
984263bc
MD
850int
851in6_mapped_sockaddr(struct socket *so, struct sockaddr **nam)
852{
ed894f8c 853 struct inpcb *inp = so->so_pcb;
984263bc
MD
854 int error;
855
856 if (inp == NULL)
857 return EINVAL;
858 if (inp->inp_vflag & INP_IPV4) {
859 error = in_setsockaddr(so, nam);
860 if (error == 0)
861 in6_sin_2_v4mapsin6_in_sock(nam);
862 } else
863 /* scope issues will be handled in in6_setsockaddr(). */
864 error = in6_setsockaddr(so, nam);
865
866 return error;
867}
868
869int
870in6_mapped_peeraddr(struct socket *so, struct sockaddr **nam)
871{
ed894f8c 872 struct inpcb *inp = so->so_pcb;
984263bc
MD
873 int error;
874
875 if (inp == NULL)
876 return EINVAL;
877 if (inp->inp_vflag & INP_IPV4) {
878 error = in_setpeeraddr(so, nam);
879 if (error == 0)
880 in6_sin_2_v4mapsin6_in_sock(nam);
881 } else
882 /* scope issues will be handled in in6_setpeeraddr(). */
883 error = in6_setpeeraddr(so, nam);
884
885 return error;
886}
887
002c1265
MD
888void
889in6_mapped_peeraddr_dispatch(netmsg_t msg)
890{
891 int error;
892
893 error = in6_mapped_peeraddr(msg->base.nm_so, msg->peeraddr.nm_nam);
894 lwkt_replymsg(&msg->base.lmsg, error);
895}
896
984263bc
MD
897/*
898 * Pass some notification to all connections of a protocol
899 * associated with address dst. The local address and/or port numbers
900 * may be specified to limit the search. The "usual action" will be
901 * taken, depending on the ctlinput cmd. The caller must filter any
902 * cmds that are uninteresting (e.g., no error in the map).
903 * Call the protocol specific routine (if any) to report
904 * any errors for each matching socket.
905 *
abf782b3 906 * Must be called under crit_enter().
984263bc
MD
907 */
908void
7d448528
JH
909in6_pcbnotify(struct inpcbhead *head, struct sockaddr *dst, in_port_t fport,
910 const struct sockaddr *src, in_port_t lport, int cmd, int arg,
122ebd49 911 void (*notify) (struct inpcb *, int))
984263bc
MD
912{
913 struct inpcb *inp, *ninp;
914 struct sockaddr_in6 sa6_src, *sa6_dst;
984263bc 915 u_int32_t flowinfo;
984263bc 916
06072076 917 if ((unsigned)cmd >= PRC_NCMDS || dst->sa_family != AF_INET6)
984263bc
MD
918 return;
919
920 sa6_dst = (struct sockaddr_in6 *)dst;
921 if (IN6_IS_ADDR_UNSPECIFIED(&sa6_dst->sin6_addr))
922 return;
923
924 /*
925 * note that src can be NULL when we get notify by local fragmentation.
926 */
19231402 927 sa6_src = (src == NULL) ? sa6_any : *(const struct sockaddr_in6 *)src;
984263bc
MD
928 flowinfo = sa6_src.sin6_flowinfo;
929
930 /*
931 * Redirects go to all references to the destination,
932 * and use in6_rtchange to invalidate the route cache.
933 * Dead host indications: also use in6_rtchange to invalidate
934 * the cache, and deliver the error to all the sockets.
935 * Otherwise, if we have knowledge of the local port and address,
936 * deliver only to that socket.
937 */
938 if (PRC_IS_REDIRECT(cmd) || cmd == PRC_HOSTDEAD) {
939 fport = 0;
940 lport = 0;
941 bzero((caddr_t)&sa6_src.sin6_addr, sizeof(sa6_src.sin6_addr));
942
943 if (cmd != PRC_HOSTDEAD)
944 notify = in6_rtchange;
945 }
7d448528
JH
946 if (cmd != PRC_MSGSIZE)
947 arg = inet6ctlerrmap[cmd];
abf782b3 948 crit_enter();
8f706258
SW
949 for (inp = LIST_FIRST(head); inp != NULL; inp = ninp) {
950 ninp = LIST_NEXT(inp, inp_list);
984263bc 951
0854930e
MD
952 if (inp->inp_flags & INP_PLACEMARKER)
953 continue;
954
8f706258 955 if ((inp->inp_vflag & INP_IPV6) == 0)
984263bc 956 continue;
5aa41e7c
HT
957 /*
958 * If the error designates a new path MTU for a destination
959 * and the application (associated with this socket) wanted to
960 * know the value, notify. Note that we notify for all
961 * disconnected sockets if the corresponding application
962 * wanted. This is because some UDP applications keep sending
963 * sockets disconnected.
964 * XXX: should we avoid to notify the value to TCP sockets?
965 */
966 if (cmd == PRC_MSGSIZE && (inp->inp_flags & IN6P_MTU) != 0 &&
967 (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr) ||
968 IN6_ARE_ADDR_EQUAL(&inp->in6p_faddr, &sa6_dst->sin6_addr))) {
fff76945 969 ip6_notify_pmtu(inp, (struct sockaddr_in6 *)dst, &arg);
5aa41e7c 970 }
984263bc
MD
971
972 /*
973 * Detect if we should notify the error. If no source and
974 * destination ports are specifed, but non-zero flowinfo and
975 * local address match, notify the error. This is the case
976 * when the error is delivered with an encrypted buffer
977 * by ESP. Otherwise, just compare addresses and ports
978 * as usual.
979 */
980 if (lport == 0 && fport == 0 && flowinfo &&
981 inp->inp_socket != NULL &&
982 flowinfo == (inp->in6p_flowinfo & IPV6_FLOWLABEL_MASK) &&
983 IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, &sa6_src.sin6_addr))
984 goto do_notify;
985 else if (!IN6_ARE_ADDR_EQUAL(&inp->in6p_faddr,
986 &sa6_dst->sin6_addr) ||
987 inp->inp_socket == 0 ||
988 (lport && inp->inp_lport != lport) ||
989 (!IN6_IS_ADDR_UNSPECIFIED(&sa6_src.sin6_addr) &&
990 !IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr,
991 &sa6_src.sin6_addr)) ||
992 (fport && inp->inp_fport != fport))
993 continue;
994
7d448528 995do_notify:
984263bc 996 if (notify)
7d448528 997 (*notify)(inp, arg);
984263bc 998 }
abf782b3 999 crit_exit();
984263bc
MD
1000}
1001
1002/*
1003 * Lookup a PCB based on the local address and port.
1004 */
1005struct inpcb *
122ebd49 1006in6_pcblookup_local(struct inpcbinfo *pcbinfo, struct in6_addr *laddr,
3e4150ef 1007 u_int lport_arg, int wild_okay, struct ucred *cred)
984263bc 1008{
34ee9825 1009 struct inpcb *inp;
984263bc
MD
1010 int matchwild = 3, wildcard;
1011 u_short lport = lport_arg;
13d8907a
JH
1012 struct inpcbporthead *porthash;
1013 struct inpcbport *phd;
1014 struct inpcb *match = NULL;
984263bc 1015
0ce0603e 1016 /*
2f792162
SZ
1017 * If the porthashbase is shared across several cpus, it must
1018 * have been locked.
0ce0603e
MD
1019 */
1020 if (pcbinfo->porttoken)
2f792162 1021 ASSERT_LWKT_TOKEN_HELD(pcbinfo->porttoken);
0ce0603e 1022
13d8907a
JH
1023 /*
1024 * Best fit PCB lookup.
1025 *
1026 * First see if this local port is in use by looking on the
1027 * port hash list.
1028 */
0ce0603e
MD
1029 porthash = &pcbinfo->porthashbase[
1030 INP_PCBPORTHASH(lport, pcbinfo->porthashmask)];
13d8907a
JH
1031 LIST_FOREACH(phd, porthash, phd_hash) {
1032 if (phd->phd_port == lport)
1033 break;
1034 }
0ce0603e 1035
13d8907a 1036 if (phd != NULL) {
984263bc 1037 /*
13d8907a
JH
1038 * Port is in use by one or more PCBs. Look for best
1039 * fit.
984263bc 1040 */
13d8907a
JH
1041 LIST_FOREACH(inp, &phd->phd_pcblist, inp_portlist) {
1042 wildcard = 0;
984263bc
MD
1043 if ((inp->inp_vflag & INP_IPV6) == 0)
1044 continue;
13d8907a
JH
1045 if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr))
1046 wildcard++;
1047 if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)) {
1048 if (IN6_IS_ADDR_UNSPECIFIED(laddr))
1049 wildcard++;
1050 else if (!IN6_ARE_ADDR_EQUAL(
1051 &inp->in6p_laddr, laddr))
984263bc 1052 continue;
13d8907a
JH
1053 } else {
1054 if (!IN6_IS_ADDR_UNSPECIFIED(laddr))
984263bc 1055 wildcard++;
13d8907a
JH
1056 }
1057 if (wildcard && !wild_okay)
1058 continue;
3e4150ef
VBD
1059 if (wildcard < matchwild &&
1060 (cred == NULL ||
1061 cred->cr_prison ==
1062 inp->inp_socket->so_cred->cr_prison)) {
13d8907a 1063 match = inp;
3e4150ef 1064 matchwild = wildcard;
13d8907a
JH
1065 if (wildcard == 0)
1066 break;
1067 else
984263bc 1068 matchwild = wildcard;
984263bc
MD
1069 }
1070 }
984263bc 1071 }
13d8907a 1072 return (match);
984263bc
MD
1073}
1074
1075void
122ebd49 1076in6_pcbpurgeif0(struct in6pcb *head, struct ifnet *ifp)
984263bc
MD
1077{
1078 struct in6pcb *in6p;
1079 struct ip6_moptions *im6o;
1080 struct in6_multi_mship *imm, *nimm;
1081
1082 for (in6p = head; in6p != NULL; in6p = LIST_NEXT(in6p, inp_list)) {
0854930e
MD
1083 if (in6p->in6p_flags & INP_PLACEMARKER)
1084 continue;
984263bc
MD
1085 im6o = in6p->in6p_moptions;
1086 if ((in6p->inp_vflag & INP_IPV6) &&
1087 im6o) {
1088 /*
1089 * Unselect the outgoing interface if it is being
1090 * detached.
1091 */
1092 if (im6o->im6o_multicast_ifp == ifp)
1093 im6o->im6o_multicast_ifp = NULL;
1094
1095 /*
1096 * Drop multicast group membership if we joined
1097 * through the interface being detached.
1098 * XXX controversial - is it really legal for kernel
1099 * to force this?
1100 */
1101 for (imm = im6o->im6o_memberships.lh_first;
1102 imm != NULL; imm = nimm) {
1103 nimm = imm->i6mm_chain.le_next;
1104 if (imm->i6mm_maddr->in6m_ifp == ifp) {
1105 LIST_REMOVE(imm, i6mm_chain);
1106 in6_delmulti(imm->i6mm_maddr);
efda3bd0 1107 kfree(imm, M_IPMADDR);
984263bc
MD
1108 }
1109 }
1110 }
1111 }
1112}
1113
1114/*
1115 * Check for alternatives when higher level complains
1116 * about service problems. For now, invalidate cached
1117 * routing information. If the route was created dynamically
1118 * (by a redirect), time to try a default gateway again.
1119 */
1120void
122ebd49 1121in6_losing(struct inpcb *in6p)
984263bc
MD
1122{
1123 struct rtentry *rt;
1124 struct rt_addrinfo info;
1125
1126 if ((rt = in6p->in6p_route.ro_rt) != NULL) {
1127 bzero((caddr_t)&info, sizeof(info));
1128 info.rti_flags = rt->rt_flags;
1129 info.rti_info[RTAX_DST] = rt_key(rt);
1130 info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
1131 info.rti_info[RTAX_NETMASK] = rt_mask(rt);
1132 rt_missmsg(RTM_LOSING, &info, rt->rt_flags, 0);
e779d13d
SZ
1133 if (rt->rt_flags & RTF_DYNAMIC) {
1134 rtrequest(RTM_DELETE, rt_key(rt), rt->rt_gateway,
1135 rt_mask(rt), rt->rt_flags, NULL);
1136 }
984263bc
MD
1137 in6p->in6p_route.ro_rt = NULL;
1138 rtfree(rt);
1139 /*
1140 * A new route can be allocated
1141 * the next time output is attempted.
1142 */
1143 }
1144}
1145
1146/*
1147 * After a routing change, flush old routing
1148 * and allocate a (hopefully) better one.
1149 */
1150void
71f385dc 1151in6_rtchange(struct inpcb *inp, int error)
984263bc
MD
1152{
1153 if (inp->in6p_route.ro_rt) {
1154 rtfree(inp->in6p_route.ro_rt);
1155 inp->in6p_route.ro_rt = 0;
1156 /*
1157 * A new route can be allocated the next time
1158 * output is attempted.
1159 */
1160 }
1161}
1162
1163/*
1164 * Lookup PCB in hash list.
1165 */
1166struct inpcb *
122ebd49
CP
1167in6_pcblookup_hash(struct inpcbinfo *pcbinfo, struct in6_addr *faddr,
1168 u_int fport_arg, struct in6_addr *laddr, u_int lport_arg,
1169 int wildcard, struct ifnet *ifp)
984263bc
MD
1170{
1171 struct inpcbhead *head;
34ee9825 1172 struct inpcb *inp;
3e4150ef 1173 struct inpcb *jinp = NULL;
984263bc
MD
1174 u_short fport = fport_arg, lport = lport_arg;
1175 int faith;
1176
1177 if (faithprefix_p != NULL)
1178 faith = (*faithprefix_p)(laddr);
1179 else
1180 faith = 0;
1181
1182 /*
1183 * First look for an exact match.
1184 */
13d8907a
JH
1185 head = &pcbinfo->hashbase[INP_PCBCONNHASH(faddr->s6_addr32[3] /* XXX */,
1186 fport,
1187 laddr->s6_addr32[3], /* XXX JH */
1188 lport,
984263bc
MD
1189 pcbinfo->hashmask)];
1190 LIST_FOREACH(inp, head, inp_hash) {
1191 if ((inp->inp_vflag & INP_IPV6) == 0)
1192 continue;
1193 if (IN6_ARE_ADDR_EQUAL(&inp->in6p_faddr, faddr) &&
1194 IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, laddr) &&
1195 inp->inp_fport == fport &&
1196 inp->inp_lport == lport) {
1197 /*
1198 * Found.
1199 */
3e4150ef
VBD
1200 if (inp->inp_socket == NULL ||
1201 inp->inp_socket->so_cred->cr_prison == NULL) {
1202 return (inp);
1203 } else {
1204 if (jinp == NULL)
1205 jinp = inp;
1206 }
984263bc
MD
1207 }
1208 }
3e4150ef
VBD
1209 if (jinp != NULL)
1210 return(jinp);
984263bc 1211 if (wildcard) {
847ec08e
JH
1212 struct inpcontainerhead *chead;
1213 struct inpcontainer *ic;
984263bc 1214 struct inpcb *local_wild = NULL;
3e4150ef
VBD
1215 struct inpcb *jinp_wild = NULL;
1216 struct sockaddr_in6 jsin6;
1217 struct ucred *cred;
984263bc 1218
3e4150ef
VBD
1219 /*
1220 * Order of socket selection:
1221 * 1. non-jailed, non-wild.
1222 * 2. non-jailed, wild.
1223 * 3. jailed, non-wild.
1224 * 4. jailed, wild.
1225 */
1226 jsin6.sin6_family = AF_INET6;
847ec08e 1227 chead = &pcbinfo->wildcardhashbase[INP_PCBWILDCARDHASH(lport,
2322f6ca 1228 pcbinfo->wildcardhashmask)];
847ec08e
JH
1229 LIST_FOREACH(ic, chead, ic_list) {
1230 inp = ic->ic_inp;
1231
1232 if (!(inp->inp_vflag & INP_IPV6))
984263bc 1233 continue;
3e4150ef
VBD
1234 if (inp->inp_socket != NULL)
1235 cred = inp->inp_socket->so_cred;
1236 else
1237 cred = NULL;
1238
1239 if (cred != NULL && jailed(cred)) {
1240 if (jinp != NULL) {
1241 continue;
1242 } else {
1243 jsin6.sin6_addr = *laddr;
1244 if (!jailed_ip(cred->cr_prison,
1245 (struct sockaddr *)&jsin6))
1246 continue;
1247 }
1248 }
984263bc
MD
1249 if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr) &&
1250 inp->inp_lport == lport) {
1251 if (faith && (inp->inp_flags & INP_FAITH) == 0)
1252 continue;
1253 if (IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr,
3e4150ef
VBD
1254 laddr)) {
1255 if (cred != NULL && jailed(cred))
1256 jinp = inp;
1257 else
1258 return (inp);
1259 } else if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)) {
1260 if (cred != NULL && jailed(cred))
1261 jinp_wild = inp;
1262 else
1263 local_wild = inp;
1264 }
984263bc
MD
1265 }
1266 }
3e4150ef
VBD
1267 if (local_wild != NULL)
1268 return (local_wild);
1269 if (jinp != NULL)
1270 return (jinp);
1271 return (jinp_wild);
984263bc
MD
1272 }
1273
1274 /*
1275 * Not found.
1276 */
1277 return (NULL);
1278}
1279
1280void
1281init_sin6(struct sockaddr_in6 *sin6, struct mbuf *m)
1282{
1283 struct ip6_hdr *ip;
1284
1285 ip = mtod(m, struct ip6_hdr *);
1286 bzero(sin6, sizeof(*sin6));
1287 sin6->sin6_len = sizeof(*sin6);
1288 sin6->sin6_family = AF_INET6;
1289 sin6->sin6_addr = ip->ip6_src;
1290 if (IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr))
1291 sin6->sin6_addr.s6_addr16[1] = 0;
1292 sin6->sin6_scope_id =
1293 (m->m_pkthdr.rcvif && IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr))
1294 ? m->m_pkthdr.rcvif->if_index : 0;
1295
1296 return;
1297}
68dc4251
SZ
1298
1299static void
1300in6_savefaddr(struct socket *so, const struct sockaddr *faddr)
1301{
1302 struct sockaddr_in6 *sin6;
1303
1304 KASSERT(faddr->sa_family == AF_INET6,
ed20d0e3 1305 ("not AF_INET6 faddr %d", faddr->sa_family));
68dc4251
SZ
1306
1307 sin6 = kmalloc(sizeof(*sin6), M_SONAME, M_WAITOK | M_ZERO);
1308 sin6->sin6_family = AF_INET6;
1309 sin6->sin6_len = sizeof(*sin6);
1310
1311 sin6->sin6_port = ((const struct sockaddr_in6 *)faddr)->sin6_port;
1312 sin6->sin6_addr = ((const struct sockaddr_in6 *)faddr)->sin6_addr;
1313
1314 if (IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr))
1315 sin6->sin6_scope_id = ntohs(sin6->sin6_addr.s6_addr16[1]);
1316 else
1317 sin6->sin6_scope_id = 0; /*XXX*/
1318 if (IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr))
1319 sin6->sin6_addr.s6_addr16[1] = 0;
1320
1321 so->so_faddr = (struct sockaddr *)sin6;
1322}
1323
1324void
1325in6_mapped_savefaddr(struct socket *so, const struct sockaddr *faddr)
1326{
1327 if (faddr->sa_family == AF_INET) {
1328 in_savefaddr(so, faddr);
1329 in6_sin_2_v4mapsin6_in_sock(&so->so_faddr);
1330 } else {
1331 in6_savefaddr(so, faddr);
1332 }
1333}