Deleted Added
full compact
in6_pcb.c (189848) in6_pcb.c (191672)
1/*-
2 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of the project nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 *
29 * $KAME: in6_pcb.c,v 1.31 2001/05/21 05:45:10 jinmei Exp $
30 */
31
32/*-
33 * Copyright (c) 1982, 1986, 1991, 1993
34 * The Regents of the University of California. All rights reserved.
35 *
36 * Redistribution and use in source and binary forms, with or without
37 * modification, are permitted provided that the following conditions
38 * are met:
39 * 1. Redistributions of source code must retain the above copyright
40 * notice, this list of conditions and the following disclaimer.
41 * 2. Redistributions in binary form must reproduce the above copyright
42 * notice, this list of conditions and the following disclaimer in the
43 * documentation and/or other materials provided with the distribution.
44 * 4. Neither the name of the University nor the names of its contributors
45 * may be used to endorse or promote products derived from this software
46 * without specific prior written permission.
47 *
48 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
49 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
50 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
51 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
52 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
53 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
54 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
55 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
56 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
57 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
58 * SUCH DAMAGE.
59 *
60 * @(#)in_pcb.c 8.2 (Berkeley) 1/4/94
61 */
62
63#include <sys/cdefs.h>
1/*-
2 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of the project nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 *
29 * $KAME: in6_pcb.c,v 1.31 2001/05/21 05:45:10 jinmei Exp $
30 */
31
32/*-
33 * Copyright (c) 1982, 1986, 1991, 1993
34 * The Regents of the University of California. All rights reserved.
35 *
36 * Redistribution and use in source and binary forms, with or without
37 * modification, are permitted provided that the following conditions
38 * are met:
39 * 1. Redistributions of source code must retain the above copyright
40 * notice, this list of conditions and the following disclaimer.
41 * 2. Redistributions in binary form must reproduce the above copyright
42 * notice, this list of conditions and the following disclaimer in the
43 * documentation and/or other materials provided with the distribution.
44 * 4. Neither the name of the University nor the names of its contributors
45 * may be used to endorse or promote products derived from this software
46 * without specific prior written permission.
47 *
48 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
49 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
50 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
51 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
52 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
53 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
54 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
55 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
56 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
57 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
58 * SUCH DAMAGE.
59 *
60 * @(#)in_pcb.c 8.2 (Berkeley) 1/4/94
61 */
62
63#include <sys/cdefs.h>
64__FBSDID("$FreeBSD: head/sys/netinet6/in6_pcb.c 189848 2009-03-15 09:58:31Z rwatson $");
64__FBSDID("$FreeBSD: head/sys/netinet6/in6_pcb.c 191672 2009-04-29 19:19:13Z bms $");
65
66#include "opt_inet.h"
67#include "opt_inet6.h"
68#include "opt_ipsec.h"
69#include "opt_mac.h"
70
71#include <sys/param.h>
72#include <sys/systm.h>
73#include <sys/malloc.h>
74#include <sys/mbuf.h>
75#include <sys/domain.h>
76#include <sys/protosw.h>
77#include <sys/socket.h>
78#include <sys/socketvar.h>
79#include <sys/sockio.h>
80#include <sys/errno.h>
81#include <sys/time.h>
82#include <sys/priv.h>
83#include <sys/proc.h>
84#include <sys/jail.h>
85#include <sys/vimage.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/tcp_var.h>
97#include <netinet/ip6.h>
98#include <netinet/ip_var.h>
99#include <netinet/vinet.h>
100
101#include <netinet6/ip6_var.h>
102#include <netinet6/nd6.h>
103#include <netinet/in_pcb.h>
104#include <netinet6/in6_pcb.h>
105#include <netinet6/scope6_var.h>
106#include <netinet6/vinet6.h>
107
108#include <security/mac/mac_framework.h>
109
110struct in6_addr zeroin6_addr;
111
112int
113in6_pcbbind(register struct inpcb *inp, struct sockaddr *nam,
114 struct ucred *cred)
115{
116 INIT_VNET_INET6(inp->inp_vnet);
117 INIT_VNET_INET(inp->inp_vnet);
118 struct socket *so = inp->inp_socket;
119 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)NULL;
120 struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
121 u_short lport = 0;
122 int error, wild = 0, reuseport = (so->so_options & SO_REUSEPORT);
123
124 INP_INFO_WLOCK_ASSERT(pcbinfo);
125 INP_WLOCK_ASSERT(inp);
126
127 if (!V_in6_ifaddr) /* XXX broken! */
128 return (EADDRNOTAVAIL);
129 if (inp->inp_lport || !IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr))
130 return (EINVAL);
131 if ((so->so_options & (SO_REUSEADDR|SO_REUSEPORT)) == 0)
132 wild = INPLOOKUP_WILDCARD;
133 if (nam == NULL) {
134 if ((error = prison_local_ip6(cred, &inp->in6p_laddr,
135 ((inp->inp_flags & IN6P_IPV6_V6ONLY) != 0))) != 0)
136 return (error);
137 } else {
138 sin6 = (struct sockaddr_in6 *)nam;
139 if (nam->sa_len != sizeof(*sin6))
140 return (EINVAL);
141 /*
142 * family check.
143 */
144 if (nam->sa_family != AF_INET6)
145 return (EAFNOSUPPORT);
146
147 if ((error = sa6_embedscope(sin6, V_ip6_use_defzone)) != 0)
148 return(error);
149
150 if ((error = prison_local_ip6(cred, &sin6->sin6_addr,
151 ((inp->inp_flags & IN6P_IPV6_V6ONLY) != 0))) != 0)
152 return (error);
153
154 lport = sin6->sin6_port;
155 if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) {
156 /*
157 * Treat SO_REUSEADDR as SO_REUSEPORT for multicast;
158 * allow compepte duplication of binding if
159 * SO_REUSEPORT is set, or if SO_REUSEADDR is set
160 * and a multicast address is bound on both
161 * new and duplicated sockets.
162 */
163 if (so->so_options & SO_REUSEADDR)
164 reuseport = SO_REUSEADDR|SO_REUSEPORT;
165 } else if (!IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
166 struct ifaddr *ia = NULL;
167
168 sin6->sin6_port = 0; /* yech... */
169 if ((ia = ifa_ifwithaddr((struct sockaddr *)sin6)) == 0)
170 return (EADDRNOTAVAIL);
171
172 /*
173 * XXX: bind to an anycast address might accidentally
174 * cause sending a packet with anycast source address.
175 * We should allow to bind to a deprecated address, since
176 * the application dares to use it.
177 */
178 if (ia &&
179 ((struct in6_ifaddr *)ia)->ia6_flags &
180 (IN6_IFF_ANYCAST|IN6_IFF_NOTREADY|IN6_IFF_DETACHED)) {
181 return (EADDRNOTAVAIL);
182 }
183 }
184 if (lport) {
185 struct inpcb *t;
186
187 /* GROSS */
188 if (ntohs(lport) <= V_ipport_reservedhigh &&
189 ntohs(lport) >= V_ipport_reservedlow &&
190 priv_check_cred(cred, PRIV_NETINET_RESERVEDPORT,
191 0))
192 return (EACCES);
193 if (!IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr) &&
194 priv_check_cred(inp->inp_cred,
195 PRIV_NETINET_REUSEPORT, 0) != 0) {
196 t = in6_pcblookup_local(pcbinfo,
197 &sin6->sin6_addr, lport,
198 INPLOOKUP_WILDCARD, cred);
199 if (t &&
200 ((t->inp_flags & INP_TIMEWAIT) == 0) &&
201 (so->so_type != SOCK_STREAM ||
202 IN6_IS_ADDR_UNSPECIFIED(&t->in6p_faddr)) &&
203 (!IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr) ||
204 !IN6_IS_ADDR_UNSPECIFIED(&t->in6p_laddr) ||
205 (t->inp_socket->so_options & SO_REUSEPORT)
206 == 0) && (inp->inp_cred->cr_uid !=
207 t->inp_cred->cr_uid))
208 return (EADDRINUSE);
209 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0 &&
210 IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
211 struct sockaddr_in sin;
212
213 in6_sin6_2_sin(&sin, sin6);
214 t = in_pcblookup_local(pcbinfo,
215 sin.sin_addr, lport,
216 INPLOOKUP_WILDCARD, cred);
217 if (t &&
218 ((t->inp_flags &
219 INP_TIMEWAIT) == 0) &&
220 (so->so_type != SOCK_STREAM ||
221 ntohl(t->inp_faddr.s_addr) ==
222 INADDR_ANY) &&
223 (inp->inp_cred->cr_uid !=
224 t->inp_cred->cr_uid))
225 return (EADDRINUSE);
226 }
227 }
228 t = in6_pcblookup_local(pcbinfo, &sin6->sin6_addr,
229 lport, wild, cred);
230 if (t && (reuseport & ((t->inp_flags & INP_TIMEWAIT) ?
231 intotw(t)->tw_so_options :
232 t->inp_socket->so_options)) == 0)
233 return (EADDRINUSE);
234 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0 &&
235 IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
236 struct sockaddr_in sin;
237
238 in6_sin6_2_sin(&sin, sin6);
239 t = in_pcblookup_local(pcbinfo, sin.sin_addr,
240 lport, wild, cred);
241 if (t && t->inp_flags & INP_TIMEWAIT) {
242 if ((reuseport &
243 intotw(t)->tw_so_options) == 0 &&
244 (ntohl(t->inp_laddr.s_addr) !=
245 INADDR_ANY || ((inp->inp_vflag &
246 INP_IPV6PROTO) ==
247 (t->inp_vflag & INP_IPV6PROTO))))
248 return (EADDRINUSE);
249 }
250 else if (t &&
251 (reuseport & t->inp_socket->so_options)
252 == 0 && (ntohl(t->inp_laddr.s_addr) !=
253 INADDR_ANY || INP_SOCKAF(so) ==
254 INP_SOCKAF(t->inp_socket)))
255 return (EADDRINUSE);
256 }
257 }
258 inp->in6p_laddr = sin6->sin6_addr;
259 }
260 if (lport == 0) {
261 if ((error = in6_pcbsetport(&inp->in6p_laddr, inp, cred)) != 0)
262 return (error);
263 } else {
264 inp->inp_lport = lport;
265 if (in_pcbinshash(inp) != 0) {
266 inp->in6p_laddr = in6addr_any;
267 inp->inp_lport = 0;
268 return (EAGAIN);
269 }
270 }
271 return (0);
272}
273
274/*
275 * Transform old in6_pcbconnect() into an inner subroutine for new
276 * in6_pcbconnect(): Do some validity-checking on the remote
277 * address (in mbuf 'nam') and then determine local host address
278 * (i.e., which interface) to use to access that remote host.
279 *
280 * This preserves definition of in6_pcbconnect(), while supporting a
281 * slightly different version for T/TCP. (This is more than
282 * a bit of a kludge, but cleaning up the internal interfaces would
283 * have forced minor changes in every protocol).
284 */
285int
286in6_pcbladdr(register struct inpcb *inp, struct sockaddr *nam,
287 struct in6_addr **plocal_addr6)
288{
289 INIT_VNET_INET6(inp->inp_vnet);
290 register struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)nam;
291 int error = 0;
292 struct ifnet *ifp = NULL;
293 int scope_ambiguous = 0;
294
295 INP_INFO_WLOCK_ASSERT(inp->inp_pcbinfo);
296 INP_WLOCK_ASSERT(inp);
297
298 if (nam->sa_len != sizeof (*sin6))
299 return (EINVAL);
300 if (sin6->sin6_family != AF_INET6)
301 return (EAFNOSUPPORT);
302 if (sin6->sin6_port == 0)
303 return (EADDRNOTAVAIL);
304
305 if (sin6->sin6_scope_id == 0 && !V_ip6_use_defzone)
306 scope_ambiguous = 1;
307 if ((error = sa6_embedscope(sin6, V_ip6_use_defzone)) != 0)
308 return(error);
309
310 if (V_in6_ifaddr) {
311 /*
312 * If the destination address is UNSPECIFIED addr,
313 * use the loopback addr, e.g ::1.
314 */
315 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr))
316 sin6->sin6_addr = in6addr_loopback;
317 }
318 if ((error = prison_remote_ip6(inp->inp_cred, &sin6->sin6_addr)) != 0)
319 return (error);
320
321 /*
322 * XXX: in6_selectsrc might replace the bound local address
323 * with the address specified by setsockopt(IPV6_PKTINFO).
324 * Is it the intended behavior?
325 */
326 *plocal_addr6 = in6_selectsrc(sin6, inp->in6p_outputopts,
327 inp, NULL,
328 inp->inp_cred,
329 &ifp, &error);
330 if (ifp && scope_ambiguous &&
331 (error = in6_setscope(&sin6->sin6_addr, ifp, NULL)) != 0) {
332 return(error);
333 }
334
335 if (*plocal_addr6 == NULL) {
336 if (error == 0)
337 error = EADDRNOTAVAIL;
338 return (error);
339 }
340 /*
341 * Don't do pcblookup call here; return interface in
342 * plocal_addr6
343 * and exit to caller, that will do the lookup.
344 */
345
346 return (0);
347}
348
349/*
350 * Outer subroutine:
351 * Connect from a socket to a specified address.
352 * Both address and port must be specified in argument sin.
353 * If don't have a local address for this socket yet,
354 * then pick one.
355 */
356int
357in6_pcbconnect(register struct inpcb *inp, struct sockaddr *nam,
358 struct ucred *cred)
359{
360 struct in6_addr *addr6;
361 register struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)nam;
362 int error;
363
364 INP_INFO_WLOCK_ASSERT(inp->inp_pcbinfo);
365 INP_WLOCK_ASSERT(inp);
366
367 /*
368 * Call inner routine, to assign local interface address.
369 * in6_pcbladdr() may automatically fill in sin6_scope_id.
370 */
371 if ((error = in6_pcbladdr(inp, nam, &addr6)) != 0)
372 return (error);
373
374 if (in6_pcblookup_hash(inp->inp_pcbinfo, &sin6->sin6_addr,
375 sin6->sin6_port,
376 IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)
377 ? addr6 : &inp->in6p_laddr,
378 inp->inp_lport, 0, NULL) != NULL) {
379 return (EADDRINUSE);
380 }
381 if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)) {
382 if (inp->inp_lport == 0) {
383 error = in6_pcbbind(inp, (struct sockaddr *)0, cred);
384 if (error)
385 return (error);
386 }
387 inp->in6p_laddr = *addr6;
388 }
389 inp->in6p_faddr = sin6->sin6_addr;
390 inp->inp_fport = sin6->sin6_port;
391 /* update flowinfo - draft-itojun-ipv6-flowlabel-api-00 */
392 inp->inp_flow &= ~IPV6_FLOWLABEL_MASK;
393 if (inp->inp_flags & IN6P_AUTOFLOWLABEL)
394 inp->inp_flow |=
395 (htonl(ip6_randomflowlabel()) & IPV6_FLOWLABEL_MASK);
396
397 in_pcbrehash(inp);
398
399 return (0);
400}
401
402void
403in6_pcbdisconnect(struct inpcb *inp)
404{
405
406 INP_INFO_WLOCK_ASSERT(inp->inp_pcbinfo);
407 INP_WLOCK_ASSERT(inp);
408
409 bzero((caddr_t)&inp->in6p_faddr, sizeof(inp->in6p_faddr));
410 inp->inp_fport = 0;
411 /* clear flowinfo - draft-itojun-ipv6-flowlabel-api-00 */
412 inp->inp_flow &= ~IPV6_FLOWLABEL_MASK;
413 in_pcbrehash(inp);
414}
415
416struct sockaddr *
417in6_sockaddr(in_port_t port, struct in6_addr *addr_p)
418{
419 struct sockaddr_in6 *sin6;
420
421 sin6 = malloc(sizeof *sin6, M_SONAME, M_WAITOK);
422 bzero(sin6, sizeof *sin6);
423 sin6->sin6_family = AF_INET6;
424 sin6->sin6_len = sizeof(*sin6);
425 sin6->sin6_port = port;
426 sin6->sin6_addr = *addr_p;
427 (void)sa6_recoverscope(sin6); /* XXX: should catch errors */
428
429 return (struct sockaddr *)sin6;
430}
431
432struct sockaddr *
433in6_v4mapsin6_sockaddr(in_port_t port, struct in_addr *addr_p)
434{
435 struct sockaddr_in sin;
436 struct sockaddr_in6 *sin6_p;
437
438 bzero(&sin, sizeof sin);
439 sin.sin_family = AF_INET;
440 sin.sin_len = sizeof(sin);
441 sin.sin_port = port;
442 sin.sin_addr = *addr_p;
443
444 sin6_p = malloc(sizeof *sin6_p, M_SONAME,
445 M_WAITOK);
446 in6_sin_2_v4mapsin6(&sin, sin6_p);
447
448 return (struct sockaddr *)sin6_p;
449}
450
451int
452in6_getsockaddr(struct socket *so, struct sockaddr **nam)
453{
454 register struct inpcb *inp;
455 struct in6_addr addr;
456 in_port_t port;
457
458 inp = sotoinpcb(so);
459 KASSERT(inp != NULL, ("in6_getsockaddr: inp == NULL"));
460
461 INP_RLOCK(inp);
462 port = inp->inp_lport;
463 addr = inp->in6p_laddr;
464 INP_RUNLOCK(inp);
465
466 *nam = in6_sockaddr(port, &addr);
467 return 0;
468}
469
470int
471in6_getpeeraddr(struct socket *so, struct sockaddr **nam)
472{
473 struct inpcb *inp;
474 struct in6_addr addr;
475 in_port_t port;
476
477 inp = sotoinpcb(so);
478 KASSERT(inp != NULL, ("in6_getpeeraddr: inp == NULL"));
479
480 INP_RLOCK(inp);
481 port = inp->inp_fport;
482 addr = inp->in6p_faddr;
483 INP_RUNLOCK(inp);
484
485 *nam = in6_sockaddr(port, &addr);
486 return 0;
487}
488
489int
490in6_mapped_sockaddr(struct socket *so, struct sockaddr **nam)
491{
492 struct inpcb *inp;
493 int error;
494
495 inp = sotoinpcb(so);
496 KASSERT(inp != NULL, ("in6_mapped_sockaddr: inp == NULL"));
497
498 if ((inp->inp_vflag & (INP_IPV4 | INP_IPV6)) == INP_IPV4) {
499 error = in_getsockaddr(so, nam);
500 if (error == 0)
501 in6_sin_2_v4mapsin6_in_sock(nam);
502 } else {
503 /* scope issues will be handled in in6_getsockaddr(). */
504 error = in6_getsockaddr(so, nam);
505 }
506
507 return error;
508}
509
510int
511in6_mapped_peeraddr(struct socket *so, struct sockaddr **nam)
512{
513 struct inpcb *inp;
514 int error;
515
516 inp = sotoinpcb(so);
517 KASSERT(inp != NULL, ("in6_mapped_peeraddr: inp == NULL"));
518
519 if ((inp->inp_vflag & (INP_IPV4 | INP_IPV6)) == INP_IPV4) {
520 error = in_getpeeraddr(so, nam);
521 if (error == 0)
522 in6_sin_2_v4mapsin6_in_sock(nam);
523 } else
524 /* scope issues will be handled in in6_getpeeraddr(). */
525 error = in6_getpeeraddr(so, nam);
526
527 return error;
528}
529
530/*
531 * Pass some notification to all connections of a protocol
532 * associated with address dst. The local address and/or port numbers
533 * may be specified to limit the search. The "usual action" will be
534 * taken, depending on the ctlinput cmd. The caller must filter any
535 * cmds that are uninteresting (e.g., no error in the map).
536 * Call the protocol specific routine (if any) to report
537 * any errors for each matching socket.
538 */
539void
540in6_pcbnotify(struct inpcbinfo *pcbinfo, struct sockaddr *dst,
541 u_int fport_arg, const struct sockaddr *src, u_int lport_arg,
542 int cmd, void *cmdarg,
543 struct inpcb *(*notify)(struct inpcb *, int))
544{
545 struct inpcb *inp, *inp_temp;
546 struct sockaddr_in6 sa6_src, *sa6_dst;
547 u_short fport = fport_arg, lport = lport_arg;
548 u_int32_t flowinfo;
549 int errno;
550
551 if ((unsigned)cmd >= PRC_NCMDS || dst->sa_family != AF_INET6)
552 return;
553
554 sa6_dst = (struct sockaddr_in6 *)dst;
555 if (IN6_IS_ADDR_UNSPECIFIED(&sa6_dst->sin6_addr))
556 return;
557
558 /*
559 * note that src can be NULL when we get notify by local fragmentation.
560 */
561 sa6_src = (src == NULL) ? sa6_any : *(const struct sockaddr_in6 *)src;
562 flowinfo = sa6_src.sin6_flowinfo;
563
564 /*
565 * Redirects go to all references to the destination,
566 * and use in6_rtchange to invalidate the route cache.
567 * Dead host indications: also use in6_rtchange to invalidate
568 * the cache, and deliver the error to all the sockets.
569 * Otherwise, if we have knowledge of the local port and address,
570 * deliver only to that socket.
571 */
572 if (PRC_IS_REDIRECT(cmd) || cmd == PRC_HOSTDEAD) {
573 fport = 0;
574 lport = 0;
575 bzero((caddr_t)&sa6_src.sin6_addr, sizeof(sa6_src.sin6_addr));
576
577 if (cmd != PRC_HOSTDEAD)
578 notify = in6_rtchange;
579 }
580 errno = inet6ctlerrmap[cmd];
581 INP_INFO_WLOCK(pcbinfo);
582 LIST_FOREACH_SAFE(inp, pcbinfo->ipi_listhead, inp_list, inp_temp) {
583 INP_WLOCK(inp);
584 if ((inp->inp_vflag & INP_IPV6) == 0) {
585 INP_WUNLOCK(inp);
586 continue;
587 }
588
589 /*
590 * If the error designates a new path MTU for a destination
591 * and the application (associated with this socket) wanted to
592 * know the value, notify. Note that we notify for all
593 * disconnected sockets if the corresponding application
594 * wanted. This is because some UDP applications keep sending
595 * sockets disconnected.
596 * XXX: should we avoid to notify the value to TCP sockets?
597 */
598 if (cmd == PRC_MSGSIZE && (inp->inp_flags & IN6P_MTU) != 0 &&
599 (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr) ||
600 IN6_ARE_ADDR_EQUAL(&inp->in6p_faddr, &sa6_dst->sin6_addr))) {
601 ip6_notify_pmtu(inp, (struct sockaddr_in6 *)dst,
602 (u_int32_t *)cmdarg);
603 }
604
605 /*
606 * Detect if we should notify the error. If no source and
607 * destination ports are specifed, but non-zero flowinfo and
608 * local address match, notify the error. This is the case
609 * when the error is delivered with an encrypted buffer
610 * by ESP. Otherwise, just compare addresses and ports
611 * as usual.
612 */
613 if (lport == 0 && fport == 0 && flowinfo &&
614 inp->inp_socket != NULL &&
615 flowinfo == (inp->inp_flow & IPV6_FLOWLABEL_MASK) &&
616 IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, &sa6_src.sin6_addr))
617 goto do_notify;
618 else if (!IN6_ARE_ADDR_EQUAL(&inp->in6p_faddr,
619 &sa6_dst->sin6_addr) ||
620 inp->inp_socket == 0 ||
621 (lport && inp->inp_lport != lport) ||
622 (!IN6_IS_ADDR_UNSPECIFIED(&sa6_src.sin6_addr) &&
623 !IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr,
624 &sa6_src.sin6_addr)) ||
625 (fport && inp->inp_fport != fport)) {
626 INP_WUNLOCK(inp);
627 continue;
628 }
629
630 do_notify:
631 if (notify) {
632 if ((*notify)(inp, errno))
633 INP_WUNLOCK(inp);
634 } else
635 INP_WUNLOCK(inp);
636 }
637 INP_INFO_WUNLOCK(pcbinfo);
638}
639
640/*
641 * Lookup a PCB based on the local address and port.
642 */
643struct inpcb *
644in6_pcblookup_local(struct inpcbinfo *pcbinfo, struct in6_addr *laddr,
645 u_short lport, int wild_okay, struct ucred *cred)
646{
647 register struct inpcb *inp;
648 int matchwild = 3, wildcard;
649
650 INP_INFO_WLOCK_ASSERT(pcbinfo);
651
652 if (!wild_okay) {
653 struct inpcbhead *head;
654 /*
655 * Look for an unconnected (wildcard foreign addr) PCB that
656 * matches the local address and port we're looking for.
657 */
658 head = &pcbinfo->ipi_hashbase[INP_PCBHASH(INADDR_ANY, lport,
659 0, pcbinfo->ipi_hashmask)];
660 LIST_FOREACH(inp, head, inp_hash) {
661 /* XXX inp locking */
662 if ((inp->inp_vflag & INP_IPV6) == 0)
663 continue;
664 if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr) &&
665 IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, laddr) &&
666 inp->inp_lport == lport) {
667 /* Found. */
668 if (cred == NULL ||
669 inp->inp_cred->cr_prison == cred->cr_prison)
670 return (inp);
671 }
672 }
673 /*
674 * Not found.
675 */
676 return (NULL);
677 } else {
678 struct inpcbporthead *porthash;
679 struct inpcbport *phd;
680 struct inpcb *match = NULL;
681 /*
682 * Best fit PCB lookup.
683 *
684 * First see if this local port is in use by looking on the
685 * port hash list.
686 */
687 porthash = &pcbinfo->ipi_porthashbase[INP_PCBPORTHASH(lport,
688 pcbinfo->ipi_porthashmask)];
689 LIST_FOREACH(phd, porthash, phd_hash) {
690 if (phd->phd_port == lport)
691 break;
692 }
693 if (phd != NULL) {
694 /*
695 * Port is in use by one or more PCBs. Look for best
696 * fit.
697 */
698 LIST_FOREACH(inp, &phd->phd_pcblist, inp_portlist) {
699 wildcard = 0;
700 if (cred != NULL &&
701 inp->inp_cred->cr_prison != cred->cr_prison)
702 continue;
703 /* XXX inp locking */
704 if ((inp->inp_vflag & INP_IPV6) == 0)
705 continue;
706 if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr))
707 wildcard++;
708 if (!IN6_IS_ADDR_UNSPECIFIED(
709 &inp->in6p_laddr)) {
710 if (IN6_IS_ADDR_UNSPECIFIED(laddr))
711 wildcard++;
712 else if (!IN6_ARE_ADDR_EQUAL(
713 &inp->in6p_laddr, laddr))
714 continue;
715 } else {
716 if (!IN6_IS_ADDR_UNSPECIFIED(laddr))
717 wildcard++;
718 }
719 if (wildcard < matchwild) {
720 match = inp;
721 matchwild = wildcard;
722 if (matchwild == 0)
723 break;
724 }
725 }
726 }
727 return (match);
728 }
729}
730
731void
732in6_pcbpurgeif0(struct inpcbinfo *pcbinfo, struct ifnet *ifp)
733{
734 struct inpcb *in6p;
735 struct ip6_moptions *im6o;
65
66#include "opt_inet.h"
67#include "opt_inet6.h"
68#include "opt_ipsec.h"
69#include "opt_mac.h"
70
71#include <sys/param.h>
72#include <sys/systm.h>
73#include <sys/malloc.h>
74#include <sys/mbuf.h>
75#include <sys/domain.h>
76#include <sys/protosw.h>
77#include <sys/socket.h>
78#include <sys/socketvar.h>
79#include <sys/sockio.h>
80#include <sys/errno.h>
81#include <sys/time.h>
82#include <sys/priv.h>
83#include <sys/proc.h>
84#include <sys/jail.h>
85#include <sys/vimage.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/tcp_var.h>
97#include <netinet/ip6.h>
98#include <netinet/ip_var.h>
99#include <netinet/vinet.h>
100
101#include <netinet6/ip6_var.h>
102#include <netinet6/nd6.h>
103#include <netinet/in_pcb.h>
104#include <netinet6/in6_pcb.h>
105#include <netinet6/scope6_var.h>
106#include <netinet6/vinet6.h>
107
108#include <security/mac/mac_framework.h>
109
110struct in6_addr zeroin6_addr;
111
112int
113in6_pcbbind(register struct inpcb *inp, struct sockaddr *nam,
114 struct ucred *cred)
115{
116 INIT_VNET_INET6(inp->inp_vnet);
117 INIT_VNET_INET(inp->inp_vnet);
118 struct socket *so = inp->inp_socket;
119 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)NULL;
120 struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
121 u_short lport = 0;
122 int error, wild = 0, reuseport = (so->so_options & SO_REUSEPORT);
123
124 INP_INFO_WLOCK_ASSERT(pcbinfo);
125 INP_WLOCK_ASSERT(inp);
126
127 if (!V_in6_ifaddr) /* XXX broken! */
128 return (EADDRNOTAVAIL);
129 if (inp->inp_lport || !IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr))
130 return (EINVAL);
131 if ((so->so_options & (SO_REUSEADDR|SO_REUSEPORT)) == 0)
132 wild = INPLOOKUP_WILDCARD;
133 if (nam == NULL) {
134 if ((error = prison_local_ip6(cred, &inp->in6p_laddr,
135 ((inp->inp_flags & IN6P_IPV6_V6ONLY) != 0))) != 0)
136 return (error);
137 } else {
138 sin6 = (struct sockaddr_in6 *)nam;
139 if (nam->sa_len != sizeof(*sin6))
140 return (EINVAL);
141 /*
142 * family check.
143 */
144 if (nam->sa_family != AF_INET6)
145 return (EAFNOSUPPORT);
146
147 if ((error = sa6_embedscope(sin6, V_ip6_use_defzone)) != 0)
148 return(error);
149
150 if ((error = prison_local_ip6(cred, &sin6->sin6_addr,
151 ((inp->inp_flags & IN6P_IPV6_V6ONLY) != 0))) != 0)
152 return (error);
153
154 lport = sin6->sin6_port;
155 if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) {
156 /*
157 * Treat SO_REUSEADDR as SO_REUSEPORT for multicast;
158 * allow compepte duplication of binding if
159 * SO_REUSEPORT is set, or if SO_REUSEADDR is set
160 * and a multicast address is bound on both
161 * new and duplicated sockets.
162 */
163 if (so->so_options & SO_REUSEADDR)
164 reuseport = SO_REUSEADDR|SO_REUSEPORT;
165 } else if (!IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
166 struct ifaddr *ia = NULL;
167
168 sin6->sin6_port = 0; /* yech... */
169 if ((ia = ifa_ifwithaddr((struct sockaddr *)sin6)) == 0)
170 return (EADDRNOTAVAIL);
171
172 /*
173 * XXX: bind to an anycast address might accidentally
174 * cause sending a packet with anycast source address.
175 * We should allow to bind to a deprecated address, since
176 * the application dares to use it.
177 */
178 if (ia &&
179 ((struct in6_ifaddr *)ia)->ia6_flags &
180 (IN6_IFF_ANYCAST|IN6_IFF_NOTREADY|IN6_IFF_DETACHED)) {
181 return (EADDRNOTAVAIL);
182 }
183 }
184 if (lport) {
185 struct inpcb *t;
186
187 /* GROSS */
188 if (ntohs(lport) <= V_ipport_reservedhigh &&
189 ntohs(lport) >= V_ipport_reservedlow &&
190 priv_check_cred(cred, PRIV_NETINET_RESERVEDPORT,
191 0))
192 return (EACCES);
193 if (!IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr) &&
194 priv_check_cred(inp->inp_cred,
195 PRIV_NETINET_REUSEPORT, 0) != 0) {
196 t = in6_pcblookup_local(pcbinfo,
197 &sin6->sin6_addr, lport,
198 INPLOOKUP_WILDCARD, cred);
199 if (t &&
200 ((t->inp_flags & INP_TIMEWAIT) == 0) &&
201 (so->so_type != SOCK_STREAM ||
202 IN6_IS_ADDR_UNSPECIFIED(&t->in6p_faddr)) &&
203 (!IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr) ||
204 !IN6_IS_ADDR_UNSPECIFIED(&t->in6p_laddr) ||
205 (t->inp_socket->so_options & SO_REUSEPORT)
206 == 0) && (inp->inp_cred->cr_uid !=
207 t->inp_cred->cr_uid))
208 return (EADDRINUSE);
209 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0 &&
210 IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
211 struct sockaddr_in sin;
212
213 in6_sin6_2_sin(&sin, sin6);
214 t = in_pcblookup_local(pcbinfo,
215 sin.sin_addr, lport,
216 INPLOOKUP_WILDCARD, cred);
217 if (t &&
218 ((t->inp_flags &
219 INP_TIMEWAIT) == 0) &&
220 (so->so_type != SOCK_STREAM ||
221 ntohl(t->inp_faddr.s_addr) ==
222 INADDR_ANY) &&
223 (inp->inp_cred->cr_uid !=
224 t->inp_cred->cr_uid))
225 return (EADDRINUSE);
226 }
227 }
228 t = in6_pcblookup_local(pcbinfo, &sin6->sin6_addr,
229 lport, wild, cred);
230 if (t && (reuseport & ((t->inp_flags & INP_TIMEWAIT) ?
231 intotw(t)->tw_so_options :
232 t->inp_socket->so_options)) == 0)
233 return (EADDRINUSE);
234 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0 &&
235 IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
236 struct sockaddr_in sin;
237
238 in6_sin6_2_sin(&sin, sin6);
239 t = in_pcblookup_local(pcbinfo, sin.sin_addr,
240 lport, wild, cred);
241 if (t && t->inp_flags & INP_TIMEWAIT) {
242 if ((reuseport &
243 intotw(t)->tw_so_options) == 0 &&
244 (ntohl(t->inp_laddr.s_addr) !=
245 INADDR_ANY || ((inp->inp_vflag &
246 INP_IPV6PROTO) ==
247 (t->inp_vflag & INP_IPV6PROTO))))
248 return (EADDRINUSE);
249 }
250 else if (t &&
251 (reuseport & t->inp_socket->so_options)
252 == 0 && (ntohl(t->inp_laddr.s_addr) !=
253 INADDR_ANY || INP_SOCKAF(so) ==
254 INP_SOCKAF(t->inp_socket)))
255 return (EADDRINUSE);
256 }
257 }
258 inp->in6p_laddr = sin6->sin6_addr;
259 }
260 if (lport == 0) {
261 if ((error = in6_pcbsetport(&inp->in6p_laddr, inp, cred)) != 0)
262 return (error);
263 } else {
264 inp->inp_lport = lport;
265 if (in_pcbinshash(inp) != 0) {
266 inp->in6p_laddr = in6addr_any;
267 inp->inp_lport = 0;
268 return (EAGAIN);
269 }
270 }
271 return (0);
272}
273
274/*
275 * Transform old in6_pcbconnect() into an inner subroutine for new
276 * in6_pcbconnect(): Do some validity-checking on the remote
277 * address (in mbuf 'nam') and then determine local host address
278 * (i.e., which interface) to use to access that remote host.
279 *
280 * This preserves definition of in6_pcbconnect(), while supporting a
281 * slightly different version for T/TCP. (This is more than
282 * a bit of a kludge, but cleaning up the internal interfaces would
283 * have forced minor changes in every protocol).
284 */
285int
286in6_pcbladdr(register struct inpcb *inp, struct sockaddr *nam,
287 struct in6_addr **plocal_addr6)
288{
289 INIT_VNET_INET6(inp->inp_vnet);
290 register struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)nam;
291 int error = 0;
292 struct ifnet *ifp = NULL;
293 int scope_ambiguous = 0;
294
295 INP_INFO_WLOCK_ASSERT(inp->inp_pcbinfo);
296 INP_WLOCK_ASSERT(inp);
297
298 if (nam->sa_len != sizeof (*sin6))
299 return (EINVAL);
300 if (sin6->sin6_family != AF_INET6)
301 return (EAFNOSUPPORT);
302 if (sin6->sin6_port == 0)
303 return (EADDRNOTAVAIL);
304
305 if (sin6->sin6_scope_id == 0 && !V_ip6_use_defzone)
306 scope_ambiguous = 1;
307 if ((error = sa6_embedscope(sin6, V_ip6_use_defzone)) != 0)
308 return(error);
309
310 if (V_in6_ifaddr) {
311 /*
312 * If the destination address is UNSPECIFIED addr,
313 * use the loopback addr, e.g ::1.
314 */
315 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr))
316 sin6->sin6_addr = in6addr_loopback;
317 }
318 if ((error = prison_remote_ip6(inp->inp_cred, &sin6->sin6_addr)) != 0)
319 return (error);
320
321 /*
322 * XXX: in6_selectsrc might replace the bound local address
323 * with the address specified by setsockopt(IPV6_PKTINFO).
324 * Is it the intended behavior?
325 */
326 *plocal_addr6 = in6_selectsrc(sin6, inp->in6p_outputopts,
327 inp, NULL,
328 inp->inp_cred,
329 &ifp, &error);
330 if (ifp && scope_ambiguous &&
331 (error = in6_setscope(&sin6->sin6_addr, ifp, NULL)) != 0) {
332 return(error);
333 }
334
335 if (*plocal_addr6 == NULL) {
336 if (error == 0)
337 error = EADDRNOTAVAIL;
338 return (error);
339 }
340 /*
341 * Don't do pcblookup call here; return interface in
342 * plocal_addr6
343 * and exit to caller, that will do the lookup.
344 */
345
346 return (0);
347}
348
349/*
350 * Outer subroutine:
351 * Connect from a socket to a specified address.
352 * Both address and port must be specified in argument sin.
353 * If don't have a local address for this socket yet,
354 * then pick one.
355 */
356int
357in6_pcbconnect(register struct inpcb *inp, struct sockaddr *nam,
358 struct ucred *cred)
359{
360 struct in6_addr *addr6;
361 register struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)nam;
362 int error;
363
364 INP_INFO_WLOCK_ASSERT(inp->inp_pcbinfo);
365 INP_WLOCK_ASSERT(inp);
366
367 /*
368 * Call inner routine, to assign local interface address.
369 * in6_pcbladdr() may automatically fill in sin6_scope_id.
370 */
371 if ((error = in6_pcbladdr(inp, nam, &addr6)) != 0)
372 return (error);
373
374 if (in6_pcblookup_hash(inp->inp_pcbinfo, &sin6->sin6_addr,
375 sin6->sin6_port,
376 IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)
377 ? addr6 : &inp->in6p_laddr,
378 inp->inp_lport, 0, NULL) != NULL) {
379 return (EADDRINUSE);
380 }
381 if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)) {
382 if (inp->inp_lport == 0) {
383 error = in6_pcbbind(inp, (struct sockaddr *)0, cred);
384 if (error)
385 return (error);
386 }
387 inp->in6p_laddr = *addr6;
388 }
389 inp->in6p_faddr = sin6->sin6_addr;
390 inp->inp_fport = sin6->sin6_port;
391 /* update flowinfo - draft-itojun-ipv6-flowlabel-api-00 */
392 inp->inp_flow &= ~IPV6_FLOWLABEL_MASK;
393 if (inp->inp_flags & IN6P_AUTOFLOWLABEL)
394 inp->inp_flow |=
395 (htonl(ip6_randomflowlabel()) & IPV6_FLOWLABEL_MASK);
396
397 in_pcbrehash(inp);
398
399 return (0);
400}
401
402void
403in6_pcbdisconnect(struct inpcb *inp)
404{
405
406 INP_INFO_WLOCK_ASSERT(inp->inp_pcbinfo);
407 INP_WLOCK_ASSERT(inp);
408
409 bzero((caddr_t)&inp->in6p_faddr, sizeof(inp->in6p_faddr));
410 inp->inp_fport = 0;
411 /* clear flowinfo - draft-itojun-ipv6-flowlabel-api-00 */
412 inp->inp_flow &= ~IPV6_FLOWLABEL_MASK;
413 in_pcbrehash(inp);
414}
415
416struct sockaddr *
417in6_sockaddr(in_port_t port, struct in6_addr *addr_p)
418{
419 struct sockaddr_in6 *sin6;
420
421 sin6 = malloc(sizeof *sin6, M_SONAME, M_WAITOK);
422 bzero(sin6, sizeof *sin6);
423 sin6->sin6_family = AF_INET6;
424 sin6->sin6_len = sizeof(*sin6);
425 sin6->sin6_port = port;
426 sin6->sin6_addr = *addr_p;
427 (void)sa6_recoverscope(sin6); /* XXX: should catch errors */
428
429 return (struct sockaddr *)sin6;
430}
431
432struct sockaddr *
433in6_v4mapsin6_sockaddr(in_port_t port, struct in_addr *addr_p)
434{
435 struct sockaddr_in sin;
436 struct sockaddr_in6 *sin6_p;
437
438 bzero(&sin, sizeof sin);
439 sin.sin_family = AF_INET;
440 sin.sin_len = sizeof(sin);
441 sin.sin_port = port;
442 sin.sin_addr = *addr_p;
443
444 sin6_p = malloc(sizeof *sin6_p, M_SONAME,
445 M_WAITOK);
446 in6_sin_2_v4mapsin6(&sin, sin6_p);
447
448 return (struct sockaddr *)sin6_p;
449}
450
451int
452in6_getsockaddr(struct socket *so, struct sockaddr **nam)
453{
454 register struct inpcb *inp;
455 struct in6_addr addr;
456 in_port_t port;
457
458 inp = sotoinpcb(so);
459 KASSERT(inp != NULL, ("in6_getsockaddr: inp == NULL"));
460
461 INP_RLOCK(inp);
462 port = inp->inp_lport;
463 addr = inp->in6p_laddr;
464 INP_RUNLOCK(inp);
465
466 *nam = in6_sockaddr(port, &addr);
467 return 0;
468}
469
470int
471in6_getpeeraddr(struct socket *so, struct sockaddr **nam)
472{
473 struct inpcb *inp;
474 struct in6_addr addr;
475 in_port_t port;
476
477 inp = sotoinpcb(so);
478 KASSERT(inp != NULL, ("in6_getpeeraddr: inp == NULL"));
479
480 INP_RLOCK(inp);
481 port = inp->inp_fport;
482 addr = inp->in6p_faddr;
483 INP_RUNLOCK(inp);
484
485 *nam = in6_sockaddr(port, &addr);
486 return 0;
487}
488
489int
490in6_mapped_sockaddr(struct socket *so, struct sockaddr **nam)
491{
492 struct inpcb *inp;
493 int error;
494
495 inp = sotoinpcb(so);
496 KASSERT(inp != NULL, ("in6_mapped_sockaddr: inp == NULL"));
497
498 if ((inp->inp_vflag & (INP_IPV4 | INP_IPV6)) == INP_IPV4) {
499 error = in_getsockaddr(so, nam);
500 if (error == 0)
501 in6_sin_2_v4mapsin6_in_sock(nam);
502 } else {
503 /* scope issues will be handled in in6_getsockaddr(). */
504 error = in6_getsockaddr(so, nam);
505 }
506
507 return error;
508}
509
510int
511in6_mapped_peeraddr(struct socket *so, struct sockaddr **nam)
512{
513 struct inpcb *inp;
514 int error;
515
516 inp = sotoinpcb(so);
517 KASSERT(inp != NULL, ("in6_mapped_peeraddr: inp == NULL"));
518
519 if ((inp->inp_vflag & (INP_IPV4 | INP_IPV6)) == INP_IPV4) {
520 error = in_getpeeraddr(so, nam);
521 if (error == 0)
522 in6_sin_2_v4mapsin6_in_sock(nam);
523 } else
524 /* scope issues will be handled in in6_getpeeraddr(). */
525 error = in6_getpeeraddr(so, nam);
526
527 return error;
528}
529
530/*
531 * Pass some notification to all connections of a protocol
532 * associated with address dst. The local address and/or port numbers
533 * may be specified to limit the search. The "usual action" will be
534 * taken, depending on the ctlinput cmd. The caller must filter any
535 * cmds that are uninteresting (e.g., no error in the map).
536 * Call the protocol specific routine (if any) to report
537 * any errors for each matching socket.
538 */
539void
540in6_pcbnotify(struct inpcbinfo *pcbinfo, struct sockaddr *dst,
541 u_int fport_arg, const struct sockaddr *src, u_int lport_arg,
542 int cmd, void *cmdarg,
543 struct inpcb *(*notify)(struct inpcb *, int))
544{
545 struct inpcb *inp, *inp_temp;
546 struct sockaddr_in6 sa6_src, *sa6_dst;
547 u_short fport = fport_arg, lport = lport_arg;
548 u_int32_t flowinfo;
549 int errno;
550
551 if ((unsigned)cmd >= PRC_NCMDS || dst->sa_family != AF_INET6)
552 return;
553
554 sa6_dst = (struct sockaddr_in6 *)dst;
555 if (IN6_IS_ADDR_UNSPECIFIED(&sa6_dst->sin6_addr))
556 return;
557
558 /*
559 * note that src can be NULL when we get notify by local fragmentation.
560 */
561 sa6_src = (src == NULL) ? sa6_any : *(const struct sockaddr_in6 *)src;
562 flowinfo = sa6_src.sin6_flowinfo;
563
564 /*
565 * Redirects go to all references to the destination,
566 * and use in6_rtchange to invalidate the route cache.
567 * Dead host indications: also use in6_rtchange to invalidate
568 * the cache, and deliver the error to all the sockets.
569 * Otherwise, if we have knowledge of the local port and address,
570 * deliver only to that socket.
571 */
572 if (PRC_IS_REDIRECT(cmd) || cmd == PRC_HOSTDEAD) {
573 fport = 0;
574 lport = 0;
575 bzero((caddr_t)&sa6_src.sin6_addr, sizeof(sa6_src.sin6_addr));
576
577 if (cmd != PRC_HOSTDEAD)
578 notify = in6_rtchange;
579 }
580 errno = inet6ctlerrmap[cmd];
581 INP_INFO_WLOCK(pcbinfo);
582 LIST_FOREACH_SAFE(inp, pcbinfo->ipi_listhead, inp_list, inp_temp) {
583 INP_WLOCK(inp);
584 if ((inp->inp_vflag & INP_IPV6) == 0) {
585 INP_WUNLOCK(inp);
586 continue;
587 }
588
589 /*
590 * If the error designates a new path MTU for a destination
591 * and the application (associated with this socket) wanted to
592 * know the value, notify. Note that we notify for all
593 * disconnected sockets if the corresponding application
594 * wanted. This is because some UDP applications keep sending
595 * sockets disconnected.
596 * XXX: should we avoid to notify the value to TCP sockets?
597 */
598 if (cmd == PRC_MSGSIZE && (inp->inp_flags & IN6P_MTU) != 0 &&
599 (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr) ||
600 IN6_ARE_ADDR_EQUAL(&inp->in6p_faddr, &sa6_dst->sin6_addr))) {
601 ip6_notify_pmtu(inp, (struct sockaddr_in6 *)dst,
602 (u_int32_t *)cmdarg);
603 }
604
605 /*
606 * Detect if we should notify the error. If no source and
607 * destination ports are specifed, but non-zero flowinfo and
608 * local address match, notify the error. This is the case
609 * when the error is delivered with an encrypted buffer
610 * by ESP. Otherwise, just compare addresses and ports
611 * as usual.
612 */
613 if (lport == 0 && fport == 0 && flowinfo &&
614 inp->inp_socket != NULL &&
615 flowinfo == (inp->inp_flow & IPV6_FLOWLABEL_MASK) &&
616 IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, &sa6_src.sin6_addr))
617 goto do_notify;
618 else if (!IN6_ARE_ADDR_EQUAL(&inp->in6p_faddr,
619 &sa6_dst->sin6_addr) ||
620 inp->inp_socket == 0 ||
621 (lport && inp->inp_lport != lport) ||
622 (!IN6_IS_ADDR_UNSPECIFIED(&sa6_src.sin6_addr) &&
623 !IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr,
624 &sa6_src.sin6_addr)) ||
625 (fport && inp->inp_fport != fport)) {
626 INP_WUNLOCK(inp);
627 continue;
628 }
629
630 do_notify:
631 if (notify) {
632 if ((*notify)(inp, errno))
633 INP_WUNLOCK(inp);
634 } else
635 INP_WUNLOCK(inp);
636 }
637 INP_INFO_WUNLOCK(pcbinfo);
638}
639
640/*
641 * Lookup a PCB based on the local address and port.
642 */
643struct inpcb *
644in6_pcblookup_local(struct inpcbinfo *pcbinfo, struct in6_addr *laddr,
645 u_short lport, int wild_okay, struct ucred *cred)
646{
647 register struct inpcb *inp;
648 int matchwild = 3, wildcard;
649
650 INP_INFO_WLOCK_ASSERT(pcbinfo);
651
652 if (!wild_okay) {
653 struct inpcbhead *head;
654 /*
655 * Look for an unconnected (wildcard foreign addr) PCB that
656 * matches the local address and port we're looking for.
657 */
658 head = &pcbinfo->ipi_hashbase[INP_PCBHASH(INADDR_ANY, lport,
659 0, pcbinfo->ipi_hashmask)];
660 LIST_FOREACH(inp, head, inp_hash) {
661 /* XXX inp locking */
662 if ((inp->inp_vflag & INP_IPV6) == 0)
663 continue;
664 if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr) &&
665 IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, laddr) &&
666 inp->inp_lport == lport) {
667 /* Found. */
668 if (cred == NULL ||
669 inp->inp_cred->cr_prison == cred->cr_prison)
670 return (inp);
671 }
672 }
673 /*
674 * Not found.
675 */
676 return (NULL);
677 } else {
678 struct inpcbporthead *porthash;
679 struct inpcbport *phd;
680 struct inpcb *match = NULL;
681 /*
682 * Best fit PCB lookup.
683 *
684 * First see if this local port is in use by looking on the
685 * port hash list.
686 */
687 porthash = &pcbinfo->ipi_porthashbase[INP_PCBPORTHASH(lport,
688 pcbinfo->ipi_porthashmask)];
689 LIST_FOREACH(phd, porthash, phd_hash) {
690 if (phd->phd_port == lport)
691 break;
692 }
693 if (phd != NULL) {
694 /*
695 * Port is in use by one or more PCBs. Look for best
696 * fit.
697 */
698 LIST_FOREACH(inp, &phd->phd_pcblist, inp_portlist) {
699 wildcard = 0;
700 if (cred != NULL &&
701 inp->inp_cred->cr_prison != cred->cr_prison)
702 continue;
703 /* XXX inp locking */
704 if ((inp->inp_vflag & INP_IPV6) == 0)
705 continue;
706 if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr))
707 wildcard++;
708 if (!IN6_IS_ADDR_UNSPECIFIED(
709 &inp->in6p_laddr)) {
710 if (IN6_IS_ADDR_UNSPECIFIED(laddr))
711 wildcard++;
712 else if (!IN6_ARE_ADDR_EQUAL(
713 &inp->in6p_laddr, laddr))
714 continue;
715 } else {
716 if (!IN6_IS_ADDR_UNSPECIFIED(laddr))
717 wildcard++;
718 }
719 if (wildcard < matchwild) {
720 match = inp;
721 matchwild = wildcard;
722 if (matchwild == 0)
723 break;
724 }
725 }
726 }
727 return (match);
728 }
729}
730
731void
732in6_pcbpurgeif0(struct inpcbinfo *pcbinfo, struct ifnet *ifp)
733{
734 struct inpcb *in6p;
735 struct ip6_moptions *im6o;
736 struct in6_multi_mship *imm, *nimm;
736 int i, gap;
737
738 INP_INFO_RLOCK(pcbinfo);
739 LIST_FOREACH(in6p, pcbinfo->ipi_listhead, inp_list) {
740 INP_WLOCK(in6p);
741 im6o = in6p->in6p_moptions;
737
738 INP_INFO_RLOCK(pcbinfo);
739 LIST_FOREACH(in6p, pcbinfo->ipi_listhead, inp_list) {
740 INP_WLOCK(in6p);
741 im6o = in6p->in6p_moptions;
742 if ((in6p->inp_vflag & INP_IPV6) &&
743 im6o) {
742 if ((in6p->inp_vflag & INP_IPV6) && im6o != NULL) {
744 /*
743 /*
745 * Unselect the outgoing interface if it is being
746 * detached.
744 * Unselect the outgoing ifp for multicast if it
745 * is being detached.
747 */
748 if (im6o->im6o_multicast_ifp == ifp)
749 im6o->im6o_multicast_ifp = NULL;
746 */
747 if (im6o->im6o_multicast_ifp == ifp)
748 im6o->im6o_multicast_ifp = NULL;
750
751 /*
752 * Drop multicast group membership if we joined
753 * through the interface being detached.
749 /*
750 * Drop multicast group membership if we joined
751 * through the interface being detached.
754 * XXX controversial - is it really legal for kernel
755 * to force this?
756 */
752 */
757 for (imm = im6o->im6o_memberships.lh_first;
758 imm != NULL; imm = nimm) {
759 nimm = imm->i6mm_chain.le_next;
760 if (imm->i6mm_maddr->in6m_ifp == ifp) {
761 LIST_REMOVE(imm, i6mm_chain);
762 in6_delmulti(imm->i6mm_maddr);
763 free(imm, M_IP6MADDR);
753 gap = 0;
754 for (i = 0; i < im6o->im6o_num_memberships; i++) {
755 if (im6o->im6o_membership[i]->in6m_ifp ==
756 ifp) {
757 in6_mc_leave(im6o->im6o_membership[i],
758 NULL);
759 gap++;
760 } else if (gap != 0) {
761 im6o->im6o_membership[i - gap] =
762 im6o->im6o_membership[i];
764 }
765 }
763 }
764 }
765 im6o->im6o_num_memberships -= gap;
766 }
767 INP_WUNLOCK(in6p);
768 }
769 INP_INFO_RUNLOCK(pcbinfo);
770}
771
772/*
773 * Check for alternatives when higher level complains
774 * about service problems. For now, invalidate cached
775 * routing information. If the route was created dynamically
776 * (by a redirect), time to try a default gateway again.
777 */
778void
779in6_losing(struct inpcb *in6p)
780{
781
782 /*
783 * We don't store route pointers in the routing table anymore
784 */
785 return;
786}
787
788/*
789 * After a routing change, flush old routing
790 * and allocate a (hopefully) better one.
791 */
792struct inpcb *
793in6_rtchange(struct inpcb *inp, int errno)
794{
795 /*
796 * We don't store route pointers in the routing table anymore
797 */
798 return inp;
799}
800
801/*
802 * Lookup PCB in hash list.
803 */
804struct inpcb *
805in6_pcblookup_hash(struct inpcbinfo *pcbinfo, struct in6_addr *faddr,
806 u_int fport_arg, struct in6_addr *laddr, u_int lport_arg, int wildcard,
807 struct ifnet *ifp)
808{
809 struct inpcbhead *head;
810 struct inpcb *inp, *tmpinp;
811 u_short fport = fport_arg, lport = lport_arg;
812 int faith;
813
814 INP_INFO_LOCK_ASSERT(pcbinfo);
815
816 if (faithprefix_p != NULL)
817 faith = (*faithprefix_p)(laddr);
818 else
819 faith = 0;
820
821 /*
822 * First look for an exact match.
823 */
824 tmpinp = NULL;
825 head = &pcbinfo->ipi_hashbase[
826 INP_PCBHASH(faddr->s6_addr32[3] /* XXX */, lport, fport,
827 pcbinfo->ipi_hashmask)];
828 LIST_FOREACH(inp, head, inp_hash) {
829 /* XXX inp locking */
830 if ((inp->inp_vflag & INP_IPV6) == 0)
831 continue;
832 if (IN6_ARE_ADDR_EQUAL(&inp->in6p_faddr, faddr) &&
833 IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, laddr) &&
834 inp->inp_fport == fport &&
835 inp->inp_lport == lport) {
836 /*
837 * XXX We should be able to directly return
838 * the inp here, without any checks.
839 * Well unless both bound with SO_REUSEPORT?
840 */
841 if (jailed(inp->inp_cred))
842 return (inp);
843 if (tmpinp == NULL)
844 tmpinp = inp;
845 }
846 }
847 if (tmpinp != NULL)
848 return (tmpinp);
849
850 /*
851 * Then look for a wildcard match, if requested.
852 */
853 if (wildcard == INPLOOKUP_WILDCARD) {
854 struct inpcb *local_wild = NULL, *local_exact = NULL;
855 struct inpcb *jail_wild = NULL;
856 int injail;
857
858 /*
859 * Order of socket selection - we always prefer jails.
860 * 1. jailed, non-wild.
861 * 2. jailed, wild.
862 * 3. non-jailed, non-wild.
863 * 4. non-jailed, wild.
864 */
865 head = &pcbinfo->ipi_hashbase[INP_PCBHASH(INADDR_ANY, lport,
866 0, pcbinfo->ipi_hashmask)];
867 LIST_FOREACH(inp, head, inp_hash) {
868 /* XXX inp locking */
869 if ((inp->inp_vflag & INP_IPV6) == 0)
870 continue;
871
872 if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr) ||
873 inp->inp_lport != lport) {
874 continue;
875 }
876
877 /* XXX inp locking */
878 if (faith && (inp->inp_flags & INP_FAITH) == 0)
879 continue;
880
881 injail = jailed(inp->inp_cred);
882 if (injail) {
883 if (prison_check_ip6(inp->inp_cred,
884 laddr) != 0)
885 continue;
886 } else {
887 if (local_exact != NULL)
888 continue;
889 }
890
891 if (IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, laddr)) {
892 if (injail)
893 return (inp);
894 else
895 local_exact = inp;
896 } else if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)) {
897 if (injail)
898 jail_wild = inp;
899 else
900 local_wild = inp;
901 }
902 } /* LIST_FOREACH */
903
904 if (jail_wild != NULL)
905 return (jail_wild);
906 if (local_exact != NULL)
907 return (local_exact);
908 if (local_wild != NULL)
909 return (local_wild);
910 } /* if (wildcard == INPLOOKUP_WILDCARD) */
911
912 /*
913 * Not found.
914 */
915 return (NULL);
916}
917
918void
919init_sin6(struct sockaddr_in6 *sin6, struct mbuf *m)
920{
921 struct ip6_hdr *ip;
922
923 ip = mtod(m, struct ip6_hdr *);
924 bzero(sin6, sizeof(*sin6));
925 sin6->sin6_len = sizeof(*sin6);
926 sin6->sin6_family = AF_INET6;
927 sin6->sin6_addr = ip->ip6_src;
928
929 (void)sa6_recoverscope(sin6); /* XXX: should catch errors... */
930
931 return;
932}
766 }
767 INP_WUNLOCK(in6p);
768 }
769 INP_INFO_RUNLOCK(pcbinfo);
770}
771
772/*
773 * Check for alternatives when higher level complains
774 * about service problems. For now, invalidate cached
775 * routing information. If the route was created dynamically
776 * (by a redirect), time to try a default gateway again.
777 */
778void
779in6_losing(struct inpcb *in6p)
780{
781
782 /*
783 * We don't store route pointers in the routing table anymore
784 */
785 return;
786}
787
788/*
789 * After a routing change, flush old routing
790 * and allocate a (hopefully) better one.
791 */
792struct inpcb *
793in6_rtchange(struct inpcb *inp, int errno)
794{
795 /*
796 * We don't store route pointers in the routing table anymore
797 */
798 return inp;
799}
800
801/*
802 * Lookup PCB in hash list.
803 */
804struct inpcb *
805in6_pcblookup_hash(struct inpcbinfo *pcbinfo, struct in6_addr *faddr,
806 u_int fport_arg, struct in6_addr *laddr, u_int lport_arg, int wildcard,
807 struct ifnet *ifp)
808{
809 struct inpcbhead *head;
810 struct inpcb *inp, *tmpinp;
811 u_short fport = fport_arg, lport = lport_arg;
812 int faith;
813
814 INP_INFO_LOCK_ASSERT(pcbinfo);
815
816 if (faithprefix_p != NULL)
817 faith = (*faithprefix_p)(laddr);
818 else
819 faith = 0;
820
821 /*
822 * First look for an exact match.
823 */
824 tmpinp = NULL;
825 head = &pcbinfo->ipi_hashbase[
826 INP_PCBHASH(faddr->s6_addr32[3] /* XXX */, lport, fport,
827 pcbinfo->ipi_hashmask)];
828 LIST_FOREACH(inp, head, inp_hash) {
829 /* XXX inp locking */
830 if ((inp->inp_vflag & INP_IPV6) == 0)
831 continue;
832 if (IN6_ARE_ADDR_EQUAL(&inp->in6p_faddr, faddr) &&
833 IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, laddr) &&
834 inp->inp_fport == fport &&
835 inp->inp_lport == lport) {
836 /*
837 * XXX We should be able to directly return
838 * the inp here, without any checks.
839 * Well unless both bound with SO_REUSEPORT?
840 */
841 if (jailed(inp->inp_cred))
842 return (inp);
843 if (tmpinp == NULL)
844 tmpinp = inp;
845 }
846 }
847 if (tmpinp != NULL)
848 return (tmpinp);
849
850 /*
851 * Then look for a wildcard match, if requested.
852 */
853 if (wildcard == INPLOOKUP_WILDCARD) {
854 struct inpcb *local_wild = NULL, *local_exact = NULL;
855 struct inpcb *jail_wild = NULL;
856 int injail;
857
858 /*
859 * Order of socket selection - we always prefer jails.
860 * 1. jailed, non-wild.
861 * 2. jailed, wild.
862 * 3. non-jailed, non-wild.
863 * 4. non-jailed, wild.
864 */
865 head = &pcbinfo->ipi_hashbase[INP_PCBHASH(INADDR_ANY, lport,
866 0, pcbinfo->ipi_hashmask)];
867 LIST_FOREACH(inp, head, inp_hash) {
868 /* XXX inp locking */
869 if ((inp->inp_vflag & INP_IPV6) == 0)
870 continue;
871
872 if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr) ||
873 inp->inp_lport != lport) {
874 continue;
875 }
876
877 /* XXX inp locking */
878 if (faith && (inp->inp_flags & INP_FAITH) == 0)
879 continue;
880
881 injail = jailed(inp->inp_cred);
882 if (injail) {
883 if (prison_check_ip6(inp->inp_cred,
884 laddr) != 0)
885 continue;
886 } else {
887 if (local_exact != NULL)
888 continue;
889 }
890
891 if (IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, laddr)) {
892 if (injail)
893 return (inp);
894 else
895 local_exact = inp;
896 } else if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)) {
897 if (injail)
898 jail_wild = inp;
899 else
900 local_wild = inp;
901 }
902 } /* LIST_FOREACH */
903
904 if (jail_wild != NULL)
905 return (jail_wild);
906 if (local_exact != NULL)
907 return (local_exact);
908 if (local_wild != NULL)
909 return (local_wild);
910 } /* if (wildcard == INPLOOKUP_WILDCARD) */
911
912 /*
913 * Not found.
914 */
915 return (NULL);
916}
917
918void
919init_sin6(struct sockaddr_in6 *sin6, struct mbuf *m)
920{
921 struct ip6_hdr *ip;
922
923 ip = mtod(m, struct ip6_hdr *);
924 bzero(sin6, sizeof(*sin6));
925 sin6->sin6_len = sizeof(*sin6);
926 sin6->sin6_family = AF_INET6;
927 sin6->sin6_addr = ip->ip6_src;
928
929 (void)sa6_recoverscope(sin6); /* XXX: should catch errors... */
930
931 return;
932}