1 /*-
2 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
3 * Copyright (c) 2010-2011 Juniper Networks, Inc.
4 * All rights reserved.
5 *
6 * Portions of this software were developed by Robert N. M. Watson under
7 * contract to Juniper Networks, Inc.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the project nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 *
33 * $KAME: in6_pcb.c,v 1.31 2001/05/21 05:45:10 jinmei Exp $
34 */
35
36 /*-
37 * Copyright (c) 1982, 1986, 1991, 1993
38 * The Regents of the University of California. All rights reserved.
39 *
40 * Redistribution and use in source and binary forms, with or without
41 * modification, are permitted provided that the following conditions
42 * are met:
43 * 1. Redistributions of source code must retain the above copyright
44 * notice, this list of conditions and the following disclaimer.
45 * 2. Redistributions in binary form must reproduce the above copyright
46 * notice, this list of conditions and the following disclaimer in the
47 * documentation and/or other materials provided with the distribution.
48 * 4. Neither the name of the University nor the names of its contributors
49 * may be used to endorse or promote products derived from this software
50 * without specific prior written permission.
51 *
52 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
53 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
54 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
55 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
56 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
57 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
58 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
59 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
60 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
61 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
62 * SUCH DAMAGE.
63 *
64 * @(#)in_pcb.c 8.2 (Berkeley) 1/4/94
65 */
66
67 #include <sys/cdefs.h>
68 __FBSDID("$FreeBSD: releng/9.0/sys/netinet6/in6_pcb.c 227451 2011-11-11 15:24:17Z trociny $");
69
70 #include "opt_inet.h"
71 #include "opt_inet6.h"
72 #include "opt_ipsec.h"
73 #include "opt_pcbgroup.h"
74
75 #include <sys/param.h>
76 #include <sys/systm.h>
77 #include <sys/malloc.h>
78 #include <sys/mbuf.h>
79 #include <sys/domain.h>
80 #include <sys/protosw.h>
81 #include <sys/socket.h>
82 #include <sys/socketvar.h>
83 #include <sys/sockio.h>
84 #include <sys/errno.h>
85 #include <sys/time.h>
86 #include <sys/priv.h>
87 #include <sys/proc.h>
88 #include <sys/jail.h>
89
90 #include <vm/uma.h>
91
92 #include <net/if.h>
93 #include <net/if_types.h>
94 #include <net/route.h>
95
96 #include <netinet/in.h>
97 #include <netinet/in_var.h>
98 #include <netinet/in_systm.h>
99 #include <netinet/tcp_var.h>
100 #include <netinet/ip6.h>
101 #include <netinet/ip_var.h>
102
103 #include <netinet6/ip6_var.h>
104 #include <netinet6/nd6.h>
105 #include <netinet/in_pcb.h>
106 #include <netinet6/in6_pcb.h>
107 #include <netinet6/scope6_var.h>
108
109 struct in6_addr zeroin6_addr;
110
111 int
112 in6_pcbbind(register struct inpcb *inp, struct sockaddr *nam,
113 struct ucred *cred)
114 {
115 struct socket *so = inp->inp_socket;
116 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)NULL;
117 struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
118 u_short lport = 0;
119 int error, lookupflags = 0;
120 int reuseport = (so->so_options & SO_REUSEPORT);
121
122 INP_WLOCK_ASSERT(inp);
123 INP_HASH_WLOCK_ASSERT(pcbinfo);
124
125 if (TAILQ_EMPTY(&V_in6_ifaddrhead)) /* XXX broken! */
126 return (EADDRNOTAVAIL);
127 if (inp->inp_lport || !IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr))
128 return (EINVAL);
129 if ((so->so_options & (SO_REUSEADDR|SO_REUSEPORT)) == 0)
130 lookupflags = INPLOOKUP_WILDCARD;
131 if (nam == NULL) {
132 if ((error = prison_local_ip6(cred, &inp->in6p_laddr,
133 ((inp->inp_flags & IN6P_IPV6_V6ONLY) != 0))) != 0)
134 return (error);
135 } else {
136 sin6 = (struct sockaddr_in6 *)nam;
137 if (nam->sa_len != sizeof(*sin6))
138 return (EINVAL);
139 /*
140 * family check.
141 */
142 if (nam->sa_family != AF_INET6)
143 return (EAFNOSUPPORT);
144
145 if ((error = sa6_embedscope(sin6, V_ip6_use_defzone)) != 0)
146 return(error);
147
148 if ((error = prison_local_ip6(cred, &sin6->sin6_addr,
149 ((inp->inp_flags & IN6P_IPV6_V6ONLY) != 0))) != 0)
150 return (error);
151
152 lport = sin6->sin6_port;
153 if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) {
154 /*
155 * Treat SO_REUSEADDR as SO_REUSEPORT for multicast;
156 * allow compepte duplication of binding if
157 * SO_REUSEPORT is set, or if SO_REUSEADDR is set
158 * and a multicast address is bound on both
159 * new and duplicated sockets.
160 */
161 if (so->so_options & SO_REUSEADDR)
162 reuseport = SO_REUSEADDR|SO_REUSEPORT;
163 } else if (!IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
164 struct ifaddr *ifa;
165
166 sin6->sin6_port = 0; /* yech... */
167 if ((ifa = ifa_ifwithaddr((struct sockaddr *)sin6)) ==
168 NULL &&
169 (inp->inp_flags & INP_BINDANY) == 0) {
170 return (EADDRNOTAVAIL);
171 }
172
173 /*
174 * XXX: bind to an anycast address might accidentally
175 * cause sending a packet with anycast source address.
176 * We should allow to bind to a deprecated address, since
177 * the application dares to use it.
178 */
179 if (ifa != NULL &&
180 ((struct in6_ifaddr *)ifa)->ia6_flags &
181 (IN6_IFF_ANYCAST|IN6_IFF_NOTREADY|IN6_IFF_DETACHED)) {
182 ifa_free(ifa);
183 return (EADDRNOTAVAIL);
184 }
185 if (ifa != NULL)
186 ifa_free(ifa);
187 }
188 if (lport) {
189 struct inpcb *t;
190 struct tcptw *tw;
191
192 /* GROSS */
193 if (ntohs(lport) <= V_ipport_reservedhigh &&
194 ntohs(lport) >= V_ipport_reservedlow &&
195 priv_check_cred(cred, PRIV_NETINET_RESERVEDPORT,
196 0))
197 return (EACCES);
198 if (!IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr) &&
199 priv_check_cred(inp->inp_cred,
200 PRIV_NETINET_REUSEPORT, 0) != 0) {
201 t = in6_pcblookup_local(pcbinfo,
202 &sin6->sin6_addr, lport,
203 INPLOOKUP_WILDCARD, cred);
204 if (t &&
205 ((t->inp_flags & INP_TIMEWAIT) == 0) &&
206 (so->so_type != SOCK_STREAM ||
207 IN6_IS_ADDR_UNSPECIFIED(&t->in6p_faddr)) &&
208 (!IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr) ||
209 !IN6_IS_ADDR_UNSPECIFIED(&t->in6p_laddr) ||
210 (t->inp_flags2 & INP_REUSEPORT) == 0) &&
211 (inp->inp_cred->cr_uid !=
212 t->inp_cred->cr_uid))
213 return (EADDRINUSE);
214 #ifdef INET
215 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0 &&
216 IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
217 struct sockaddr_in sin;
218
219 in6_sin6_2_sin(&sin, sin6);
220 t = in_pcblookup_local(pcbinfo,
221 sin.sin_addr, lport,
222 INPLOOKUP_WILDCARD, cred);
223 if (t &&
224 ((t->inp_flags &
225 INP_TIMEWAIT) == 0) &&
226 (so->so_type != SOCK_STREAM ||
227 ntohl(t->inp_faddr.s_addr) ==
228 INADDR_ANY) &&
229 (inp->inp_cred->cr_uid !=
230 t->inp_cred->cr_uid))
231 return (EADDRINUSE);
232 }
233 #endif
234 }
235 t = in6_pcblookup_local(pcbinfo, &sin6->sin6_addr,
236 lport, lookupflags, cred);
237 if (t && (t->inp_flags & INP_TIMEWAIT)) {
238 /*
239 * XXXRW: If an incpb has had its timewait
240 * state recycled, we treat the address as
241 * being in use (for now). This is better
242 * than a panic, but not desirable.
243 */
244 tw = intotw(t);
245 if (tw == NULL ||
246 (reuseport & tw->tw_so_options) == 0)
247 return (EADDRINUSE);
248 } else if (t && (reuseport & t->inp_socket->so_options)
249 == 0) {
250 return (EADDRINUSE);
251 }
252 #ifdef INET
253 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0 &&
254 IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
255 struct sockaddr_in sin;
256
257 in6_sin6_2_sin(&sin, sin6);
258 t = in_pcblookup_local(pcbinfo, sin.sin_addr,
259 lport, lookupflags, cred);
260 if (t && t->inp_flags & INP_TIMEWAIT) {
261 tw = intotw(t);
262 if (tw == NULL)
263 return (EADDRINUSE);
264 if ((reuseport & tw->tw_so_options) == 0
265 && (ntohl(t->inp_laddr.s_addr) !=
266 INADDR_ANY || ((inp->inp_vflag &
267 INP_IPV6PROTO) ==
268 (t->inp_vflag & INP_IPV6PROTO))))
269 return (EADDRINUSE);
270 } else if (t && (reuseport == 0 ||
271 (t->inp_flags2 & INP_REUSEPORT) == 0) &&
272 (ntohl(t->inp_laddr.s_addr) != INADDR_ANY ||
273 (t->inp_vflag & INP_IPV6PROTO) != 0))
274 return (EADDRINUSE);
275 }
276 #endif
277 }
278 inp->in6p_laddr = sin6->sin6_addr;
279 }
280 if (lport == 0) {
281 if ((error = in6_pcbsetport(&inp->in6p_laddr, inp, cred)) != 0) {
282 /* Undo an address bind that may have occurred. */
283 inp->in6p_laddr = in6addr_any;
284 return (error);
285 }
286 } else {
287 inp->inp_lport = lport;
288 if (in_pcbinshash(inp) != 0) {
289 inp->in6p_laddr = in6addr_any;
290 inp->inp_lport = 0;
291 return (EAGAIN);
292 }
293 }
294 return (0);
295 }
296
297 /*
298 * Transform old in6_pcbconnect() into an inner subroutine for new
299 * in6_pcbconnect(): Do some validity-checking on the remote
300 * address (in mbuf 'nam') and then determine local host address
301 * (i.e., which interface) to use to access that remote host.
302 *
303 * This preserves definition of in6_pcbconnect(), while supporting a
304 * slightly different version for T/TCP. (This is more than
305 * a bit of a kludge, but cleaning up the internal interfaces would
306 * have forced minor changes in every protocol).
307 */
308 int
309 in6_pcbladdr(register struct inpcb *inp, struct sockaddr *nam,
310 struct in6_addr *plocal_addr6)
311 {
312 register struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)nam;
313 int error = 0;
314 struct ifnet *ifp = NULL;
315 int scope_ambiguous = 0;
316 struct in6_addr in6a;
317
318 INP_WLOCK_ASSERT(inp);
319 INP_HASH_WLOCK_ASSERT(inp->inp_pcbinfo); /* XXXRW: why? */
320
321 if (nam->sa_len != sizeof (*sin6))
322 return (EINVAL);
323 if (sin6->sin6_family != AF_INET6)
324 return (EAFNOSUPPORT);
325 if (sin6->sin6_port == 0)
326 return (EADDRNOTAVAIL);
327
328 if (sin6->sin6_scope_id == 0 && !V_ip6_use_defzone)
329 scope_ambiguous = 1;
330 if ((error = sa6_embedscope(sin6, V_ip6_use_defzone)) != 0)
331 return(error);
332
333 if (!TAILQ_EMPTY(&V_in6_ifaddrhead)) {
334 /*
335 * If the destination address is UNSPECIFIED addr,
336 * use the loopback addr, e.g ::1.
337 */
338 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr))
339 sin6->sin6_addr = in6addr_loopback;
340 }
341 if ((error = prison_remote_ip6(inp->inp_cred, &sin6->sin6_addr)) != 0)
342 return (error);
343
344 error = in6_selectsrc(sin6, inp->in6p_outputopts,
345 inp, NULL, inp->inp_cred, &ifp, &in6a);
346 if (error)
347 return (error);
348
349 if (ifp && scope_ambiguous &&
350 (error = in6_setscope(&sin6->sin6_addr, ifp, NULL)) != 0) {
351 return(error);
352 }
353
354 /*
355 * Do not update this earlier, in case we return with an error.
356 *
357 * XXX: this in6_selectsrc result might replace the bound local
358 * address with the address specified by setsockopt(IPV6_PKTINFO).
359 * Is it the intended behavior?
360 */
361 *plocal_addr6 = in6a;
362
363 /*
364 * Don't do pcblookup call here; return interface in
365 * plocal_addr6
366 * and exit to caller, that will do the lookup.
367 */
368
369 return (0);
370 }
371
372 /*
373 * Outer subroutine:
374 * Connect from a socket to a specified address.
375 * Both address and port must be specified in argument sin.
376 * If don't have a local address for this socket yet,
377 * then pick one.
378 */
379 int
380 in6_pcbconnect_mbuf(register struct inpcb *inp, struct sockaddr *nam,
381 struct ucred *cred, struct mbuf *m)
382 {
383 struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
384 register struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)nam;
385 struct in6_addr addr6;
386 int error;
387
388 INP_WLOCK_ASSERT(inp);
389 INP_HASH_WLOCK_ASSERT(pcbinfo);
390
391 /*
392 * Call inner routine, to assign local interface address.
393 * in6_pcbladdr() may automatically fill in sin6_scope_id.
394 */
395 if ((error = in6_pcbladdr(inp, nam, &addr6)) != 0)
396 return (error);
397
398 if (in6_pcblookup_hash_locked(pcbinfo, &sin6->sin6_addr,
399 sin6->sin6_port,
400 IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)
401 ? &addr6 : &inp->in6p_laddr,
402 inp->inp_lport, 0, NULL) != NULL) {
403 return (EADDRINUSE);
404 }
405 if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)) {
406 if (inp->inp_lport == 0) {
407 error = in6_pcbbind(inp, (struct sockaddr *)0, cred);
408 if (error)
409 return (error);
410 }
411 inp->in6p_laddr = addr6;
412 }
413 inp->in6p_faddr = sin6->sin6_addr;
414 inp->inp_fport = sin6->sin6_port;
415 /* update flowinfo - draft-itojun-ipv6-flowlabel-api-00 */
416 inp->inp_flow &= ~IPV6_FLOWLABEL_MASK;
417 if (inp->inp_flags & IN6P_AUTOFLOWLABEL)
418 inp->inp_flow |=
419 (htonl(ip6_randomflowlabel()) & IPV6_FLOWLABEL_MASK);
420
421 in_pcbrehash_mbuf(inp, m);
422
423 return (0);
424 }
425
426 int
427 in6_pcbconnect(struct inpcb *inp, struct sockaddr *nam, struct ucred *cred)
428 {
429
430 return (in6_pcbconnect_mbuf(inp, nam, cred, NULL));
431 }
432
433 void
434 in6_pcbdisconnect(struct inpcb *inp)
435 {
436
437 INP_WLOCK_ASSERT(inp);
438 INP_HASH_WLOCK_ASSERT(inp->inp_pcbinfo);
439
440 bzero((caddr_t)&inp->in6p_faddr, sizeof(inp->in6p_faddr));
441 inp->inp_fport = 0;
442 /* clear flowinfo - draft-itojun-ipv6-flowlabel-api-00 */
443 inp->inp_flow &= ~IPV6_FLOWLABEL_MASK;
444 in_pcbrehash(inp);
445 }
446
447 struct sockaddr *
448 in6_sockaddr(in_port_t port, struct in6_addr *addr_p)
449 {
450 struct sockaddr_in6 *sin6;
451
452 sin6 = malloc(sizeof *sin6, M_SONAME, M_WAITOK);
453 bzero(sin6, sizeof *sin6);
454 sin6->sin6_family = AF_INET6;
455 sin6->sin6_len = sizeof(*sin6);
456 sin6->sin6_port = port;
457 sin6->sin6_addr = *addr_p;
458 (void)sa6_recoverscope(sin6); /* XXX: should catch errors */
459
460 return (struct sockaddr *)sin6;
461 }
462
463 struct sockaddr *
464 in6_v4mapsin6_sockaddr(in_port_t port, struct in_addr *addr_p)
465 {
466 struct sockaddr_in sin;
467 struct sockaddr_in6 *sin6_p;
468
469 bzero(&sin, sizeof sin);
470 sin.sin_family = AF_INET;
471 sin.sin_len = sizeof(sin);
472 sin.sin_port = port;
473 sin.sin_addr = *addr_p;
474
475 sin6_p = malloc(sizeof *sin6_p, M_SONAME,
476 M_WAITOK);
477 in6_sin_2_v4mapsin6(&sin, sin6_p);
478
479 return (struct sockaddr *)sin6_p;
480 }
481
482 int
483 in6_getsockaddr(struct socket *so, struct sockaddr **nam)
484 {
485 register struct inpcb *inp;
486 struct in6_addr addr;
487 in_port_t port;
488
489 inp = sotoinpcb(so);
490 KASSERT(inp != NULL, ("in6_getsockaddr: inp == NULL"));
491
492 INP_RLOCK(inp);
493 port = inp->inp_lport;
494 addr = inp->in6p_laddr;
495 INP_RUNLOCK(inp);
496
497 *nam = in6_sockaddr(port, &addr);
498 return 0;
499 }
500
501 int
502 in6_getpeeraddr(struct socket *so, struct sockaddr **nam)
503 {
504 struct inpcb *inp;
505 struct in6_addr addr;
506 in_port_t port;
507
508 inp = sotoinpcb(so);
509 KASSERT(inp != NULL, ("in6_getpeeraddr: inp == NULL"));
510
511 INP_RLOCK(inp);
512 port = inp->inp_fport;
513 addr = inp->in6p_faddr;
514 INP_RUNLOCK(inp);
515
516 *nam = in6_sockaddr(port, &addr);
517 return 0;
518 }
519
520 int
521 in6_mapped_sockaddr(struct socket *so, struct sockaddr **nam)
522 {
523 struct inpcb *inp;
524 int error;
525
526 inp = sotoinpcb(so);
527 KASSERT(inp != NULL, ("in6_mapped_sockaddr: inp == NULL"));
528
529 #ifdef INET
530 if ((inp->inp_vflag & (INP_IPV4 | INP_IPV6)) == INP_IPV4) {
531 error = in_getsockaddr(so, nam);
532 if (error == 0)
533 in6_sin_2_v4mapsin6_in_sock(nam);
534 } else
535 #endif
536 {
537 /* scope issues will be handled in in6_getsockaddr(). */
538 error = in6_getsockaddr(so, nam);
539 }
540
541 return error;
542 }
543
544 int
545 in6_mapped_peeraddr(struct socket *so, struct sockaddr **nam)
546 {
547 struct inpcb *inp;
548 int error;
549
550 inp = sotoinpcb(so);
551 KASSERT(inp != NULL, ("in6_mapped_peeraddr: inp == NULL"));
552
553 #ifdef INET
554 if ((inp->inp_vflag & (INP_IPV4 | INP_IPV6)) == INP_IPV4) {
555 error = in_getpeeraddr(so, nam);
556 if (error == 0)
557 in6_sin_2_v4mapsin6_in_sock(nam);
558 } else
559 #endif
560 /* scope issues will be handled in in6_getpeeraddr(). */
561 error = in6_getpeeraddr(so, nam);
562
563 return error;
564 }
565
566 /*
567 * Pass some notification to all connections of a protocol
568 * associated with address dst. The local address and/or port numbers
569 * may be specified to limit the search. The "usual action" will be
570 * taken, depending on the ctlinput cmd. The caller must filter any
571 * cmds that are uninteresting (e.g., no error in the map).
572 * Call the protocol specific routine (if any) to report
573 * any errors for each matching socket.
574 */
575 void
576 in6_pcbnotify(struct inpcbinfo *pcbinfo, struct sockaddr *dst,
577 u_int fport_arg, const struct sockaddr *src, u_int lport_arg,
578 int cmd, void *cmdarg,
579 struct inpcb *(*notify)(struct inpcb *, int))
580 {
581 struct inpcb *inp, *inp_temp;
582 struct sockaddr_in6 sa6_src, *sa6_dst;
583 u_short fport = fport_arg, lport = lport_arg;
584 u_int32_t flowinfo;
585 int errno;
586
587 if ((unsigned)cmd >= PRC_NCMDS || dst->sa_family != AF_INET6)
588 return;
589
590 sa6_dst = (struct sockaddr_in6 *)dst;
591 if (IN6_IS_ADDR_UNSPECIFIED(&sa6_dst->sin6_addr))
592 return;
593
594 /*
595 * note that src can be NULL when we get notify by local fragmentation.
596 */
597 sa6_src = (src == NULL) ? sa6_any : *(const struct sockaddr_in6 *)src;
598 flowinfo = sa6_src.sin6_flowinfo;
599
600 /*
601 * Redirects go to all references to the destination,
602 * and use in6_rtchange to invalidate the route cache.
603 * Dead host indications: also use in6_rtchange to invalidate
604 * the cache, and deliver the error to all the sockets.
605 * Otherwise, if we have knowledge of the local port and address,
606 * deliver only to that socket.
607 */
608 if (PRC_IS_REDIRECT(cmd) || cmd == PRC_HOSTDEAD) {
609 fport = 0;
610 lport = 0;
611 bzero((caddr_t)&sa6_src.sin6_addr, sizeof(sa6_src.sin6_addr));
612
613 if (cmd != PRC_HOSTDEAD)
614 notify = in6_rtchange;
615 }
616 errno = inet6ctlerrmap[cmd];
617 INP_INFO_WLOCK(pcbinfo);
618 LIST_FOREACH_SAFE(inp, pcbinfo->ipi_listhead, inp_list, inp_temp) {
619 INP_WLOCK(inp);
620 if ((inp->inp_vflag & INP_IPV6) == 0) {
621 INP_WUNLOCK(inp);
622 continue;
623 }
624
625 /*
626 * If the error designates a new path MTU for a destination
627 * and the application (associated with this socket) wanted to
628 * know the value, notify. Note that we notify for all
629 * disconnected sockets if the corresponding application
630 * wanted. This is because some UDP applications keep sending
631 * sockets disconnected.
632 * XXX: should we avoid to notify the value to TCP sockets?
633 */
634 if (cmd == PRC_MSGSIZE && (inp->inp_flags & IN6P_MTU) != 0 &&
635 (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr) ||
636 IN6_ARE_ADDR_EQUAL(&inp->in6p_faddr, &sa6_dst->sin6_addr))) {
637 ip6_notify_pmtu(inp, (struct sockaddr_in6 *)dst,
638 (u_int32_t *)cmdarg);
639 }
640
641 /*
642 * Detect if we should notify the error. If no source and
643 * destination ports are specifed, but non-zero flowinfo and
644 * local address match, notify the error. This is the case
645 * when the error is delivered with an encrypted buffer
646 * by ESP. Otherwise, just compare addresses and ports
647 * as usual.
648 */
649 if (lport == 0 && fport == 0 && flowinfo &&
650 inp->inp_socket != NULL &&
651 flowinfo == (inp->inp_flow & IPV6_FLOWLABEL_MASK) &&
652 IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, &sa6_src.sin6_addr))
653 goto do_notify;
654 else if (!IN6_ARE_ADDR_EQUAL(&inp->in6p_faddr,
655 &sa6_dst->sin6_addr) ||
656 inp->inp_socket == 0 ||
657 (lport && inp->inp_lport != lport) ||
658 (!IN6_IS_ADDR_UNSPECIFIED(&sa6_src.sin6_addr) &&
659 !IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr,
660 &sa6_src.sin6_addr)) ||
661 (fport && inp->inp_fport != fport)) {
662 INP_WUNLOCK(inp);
663 continue;
664 }
665
666 do_notify:
667 if (notify) {
668 if ((*notify)(inp, errno))
669 INP_WUNLOCK(inp);
670 } else
671 INP_WUNLOCK(inp);
672 }
673 INP_INFO_WUNLOCK(pcbinfo);
674 }
675
676 /*
677 * Lookup a PCB based on the local address and port. Caller must hold the
678 * hash lock. No inpcb locks or references are acquired.
679 */
680 struct inpcb *
681 in6_pcblookup_local(struct inpcbinfo *pcbinfo, struct in6_addr *laddr,
682 u_short lport, int lookupflags, struct ucred *cred)
683 {
684 register struct inpcb *inp;
685 int matchwild = 3, wildcard;
686
687 KASSERT((lookupflags & ~(INPLOOKUP_WILDCARD)) == 0,
688 ("%s: invalid lookup flags %d", __func__, lookupflags));
689
690 INP_HASH_WLOCK_ASSERT(pcbinfo);
691
692 if ((lookupflags & INPLOOKUP_WILDCARD) == 0) {
693 struct inpcbhead *head;
694 /*
695 * Look for an unconnected (wildcard foreign addr) PCB that
696 * matches the local address and port we're looking for.
697 */
698 head = &pcbinfo->ipi_hashbase[INP_PCBHASH(INADDR_ANY, lport,
699 0, pcbinfo->ipi_hashmask)];
700 LIST_FOREACH(inp, head, inp_hash) {
701 /* XXX inp locking */
702 if ((inp->inp_vflag & INP_IPV6) == 0)
703 continue;
704 if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr) &&
705 IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, laddr) &&
706 inp->inp_lport == lport) {
707 /* Found. */
708 if (cred == NULL ||
709 prison_equal_ip6(cred->cr_prison,
710 inp->inp_cred->cr_prison))
711 return (inp);
712 }
713 }
714 /*
715 * Not found.
716 */
717 return (NULL);
718 } else {
719 struct inpcbporthead *porthash;
720 struct inpcbport *phd;
721 struct inpcb *match = NULL;
722 /*
723 * Best fit PCB lookup.
724 *
725 * First see if this local port is in use by looking on the
726 * port hash list.
727 */
728 porthash = &pcbinfo->ipi_porthashbase[INP_PCBPORTHASH(lport,
729 pcbinfo->ipi_porthashmask)];
730 LIST_FOREACH(phd, porthash, phd_hash) {
731 if (phd->phd_port == lport)
732 break;
733 }
734 if (phd != NULL) {
735 /*
736 * Port is in use by one or more PCBs. Look for best
737 * fit.
738 */
739 LIST_FOREACH(inp, &phd->phd_pcblist, inp_portlist) {
740 wildcard = 0;
741 if (cred != NULL &&
742 !prison_equal_ip6(cred->cr_prison,
743 inp->inp_cred->cr_prison))
744 continue;
745 /* XXX inp locking */
746 if ((inp->inp_vflag & INP_IPV6) == 0)
747 continue;
748 if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr))
749 wildcard++;
750 if (!IN6_IS_ADDR_UNSPECIFIED(
751 &inp->in6p_laddr)) {
752 if (IN6_IS_ADDR_UNSPECIFIED(laddr))
753 wildcard++;
754 else if (!IN6_ARE_ADDR_EQUAL(
755 &inp->in6p_laddr, laddr))
756 continue;
757 } else {
758 if (!IN6_IS_ADDR_UNSPECIFIED(laddr))
759 wildcard++;
760 }
761 if (wildcard < matchwild) {
762 match = inp;
763 matchwild = wildcard;
764 if (matchwild == 0)
765 break;
766 }
767 }
768 }
769 return (match);
770 }
771 }
772
773 void
774 in6_pcbpurgeif0(struct inpcbinfo *pcbinfo, struct ifnet *ifp)
775 {
776 struct inpcb *in6p;
777 struct ip6_moptions *im6o;
778 int i, gap;
779
780 INP_INFO_RLOCK(pcbinfo);
781 LIST_FOREACH(in6p, pcbinfo->ipi_listhead, inp_list) {
782 INP_WLOCK(in6p);
783 im6o = in6p->in6p_moptions;
784 if ((in6p->inp_vflag & INP_IPV6) && im6o != NULL) {
785 /*
786 * Unselect the outgoing ifp for multicast if it
787 * is being detached.
788 */
789 if (im6o->im6o_multicast_ifp == ifp)
790 im6o->im6o_multicast_ifp = NULL;
791 /*
792 * Drop multicast group membership if we joined
793 * through the interface being detached.
794 */
795 gap = 0;
796 for (i = 0; i < im6o->im6o_num_memberships; i++) {
797 if (im6o->im6o_membership[i]->in6m_ifp ==
798 ifp) {
799 in6_mc_leave(im6o->im6o_membership[i],
800 NULL);
801 gap++;
802 } else if (gap != 0) {
803 im6o->im6o_membership[i - gap] =
804 im6o->im6o_membership[i];
805 }
806 }
807 im6o->im6o_num_memberships -= gap;
808 }
809 INP_WUNLOCK(in6p);
810 }
811 INP_INFO_RUNLOCK(pcbinfo);
812 }
813
814 /*
815 * Check for alternatives when higher level complains
816 * about service problems. For now, invalidate cached
817 * routing information. If the route was created dynamically
818 * (by a redirect), time to try a default gateway again.
819 */
820 void
821 in6_losing(struct inpcb *in6p)
822 {
823
824 /*
825 * We don't store route pointers in the routing table anymore
826 */
827 return;
828 }
829
830 /*
831 * After a routing change, flush old routing
832 * and allocate a (hopefully) better one.
833 */
834 struct inpcb *
835 in6_rtchange(struct inpcb *inp, int errno)
836 {
837 /*
838 * We don't store route pointers in the routing table anymore
839 */
840 return inp;
841 }
842
843 #ifdef PCBGROUP
844 /*
845 * Lookup PCB in hash list, using pcbgroup tables.
846 */
847 static struct inpcb *
848 in6_pcblookup_group(struct inpcbinfo *pcbinfo, struct inpcbgroup *pcbgroup,
849 struct in6_addr *faddr, u_int fport_arg, struct in6_addr *laddr,
850 u_int lport_arg, int lookupflags, struct ifnet *ifp)
851 {
852 struct inpcbhead *head;
853 struct inpcb *inp, *tmpinp;
854 u_short fport = fport_arg, lport = lport_arg;
855 int faith;
856
857 if (faithprefix_p != NULL)
858 faith = (*faithprefix_p)(laddr);
859 else
860 faith = 0;
861
862 /*
863 * First look for an exact match.
864 */
865 tmpinp = NULL;
866 INP_GROUP_LOCK(pcbgroup);
867 head = &pcbgroup->ipg_hashbase[
868 INP_PCBHASH(faddr->s6_addr32[3] /* XXX */, lport, fport,
869 pcbgroup->ipg_hashmask)];
870 LIST_FOREACH(inp, head, inp_pcbgrouphash) {
871 /* XXX inp locking */
872 if ((inp->inp_vflag & INP_IPV6) == 0)
873 continue;
874 if (IN6_ARE_ADDR_EQUAL(&inp->in6p_faddr, faddr) &&
875 IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, laddr) &&
876 inp->inp_fport == fport &&
877 inp->inp_lport == lport) {
878 /*
879 * XXX We should be able to directly return
880 * the inp here, without any checks.
881 * Well unless both bound with SO_REUSEPORT?
882 */
883 if (prison_flag(inp->inp_cred, PR_IP6))
884 goto found;
885 if (tmpinp == NULL)
886 tmpinp = inp;
887 }
888 }
889 if (tmpinp != NULL) {
890 inp = tmpinp;
891 goto found;
892 }
893
894 /*
895 * Then look for a wildcard match, if requested.
896 */
897 if ((lookupflags & INPLOOKUP_WILDCARD) != 0) {
898 struct inpcb *local_wild = NULL, *local_exact = NULL;
899 struct inpcb *jail_wild = NULL;
900 int injail;
901
902 /*
903 * Order of socket selection - we always prefer jails.
904 * 1. jailed, non-wild.
905 * 2. jailed, wild.
906 * 3. non-jailed, non-wild.
907 * 4. non-jailed, wild.
908 */
909 head = &pcbinfo->ipi_wildbase[INP_PCBHASH(INADDR_ANY, lport,
910 0, pcbinfo->ipi_wildmask)];
911 LIST_FOREACH(inp, head, inp_pcbgroup_wild) {
912 /* XXX inp locking */
913 if ((inp->inp_vflag & INP_IPV6) == 0)
914 continue;
915
916 if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr) ||
917 inp->inp_lport != lport) {
918 continue;
919 }
920
921 /* XXX inp locking */
922 if (faith && (inp->inp_flags & INP_FAITH) == 0)
923 continue;
924
925 injail = prison_flag(inp->inp_cred, PR_IP6);
926 if (injail) {
927 if (prison_check_ip6(inp->inp_cred,
928 laddr) != 0)
929 continue;
930 } else {
931 if (local_exact != NULL)
932 continue;
933 }
934
935 if (IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, laddr)) {
936 if (injail)
937 goto found;
938 else
939 local_exact = inp;
940 } else if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)) {
941 if (injail)
942 jail_wild = inp;
943 else
944 local_wild = inp;
945 }
946 } /* LIST_FOREACH */
947
948 inp = jail_wild;
949 if (inp == NULL)
950 inp = jail_wild;
951 if (inp == NULL)
952 inp = local_exact;
953 if (inp == NULL)
954 inp = local_wild;
955 if (inp != NULL)
956 goto found;
957 } /* if ((lookupflags & INPLOOKUP_WILDCARD) != 0) */
958 INP_GROUP_UNLOCK(pcbgroup);
959 return (NULL);
960
961 found:
962 in_pcbref(inp);
963 INP_GROUP_UNLOCK(pcbgroup);
964 if (lookupflags & INPLOOKUP_WLOCKPCB) {
965 INP_WLOCK(inp);
966 if (in_pcbrele_wlocked(inp))
967 return (NULL);
968 } else if (lookupflags & INPLOOKUP_RLOCKPCB) {
969 INP_RLOCK(inp);
970 if (in_pcbrele_rlocked(inp))
971 return (NULL);
972 } else
973 panic("%s: locking buf", __func__);
974 return (inp);
975 }
976 #endif /* PCBGROUP */
977
978 /*
979 * Lookup PCB in hash list.
980 */
981 struct inpcb *
982 in6_pcblookup_hash_locked(struct inpcbinfo *pcbinfo, struct in6_addr *faddr,
983 u_int fport_arg, struct in6_addr *laddr, u_int lport_arg,
984 int lookupflags, struct ifnet *ifp)
985 {
986 struct inpcbhead *head;
987 struct inpcb *inp, *tmpinp;
988 u_short fport = fport_arg, lport = lport_arg;
989 int faith;
990
991 KASSERT((lookupflags & ~(INPLOOKUP_WILDCARD)) == 0,
992 ("%s: invalid lookup flags %d", __func__, lookupflags));
993
994 INP_HASH_LOCK_ASSERT(pcbinfo);
995
996 if (faithprefix_p != NULL)
997 faith = (*faithprefix_p)(laddr);
998 else
999 faith = 0;
1000
1001 /*
1002 * First look for an exact match.
1003 */
1004 tmpinp = NULL;
1005 head = &pcbinfo->ipi_hashbase[
1006 INP_PCBHASH(faddr->s6_addr32[3] /* XXX */, lport, fport,
1007 pcbinfo->ipi_hashmask)];
1008 LIST_FOREACH(inp, head, inp_hash) {
1009 /* XXX inp locking */
1010 if ((inp->inp_vflag & INP_IPV6) == 0)
1011 continue;
1012 if (IN6_ARE_ADDR_EQUAL(&inp->in6p_faddr, faddr) &&
1013 IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, laddr) &&
1014 inp->inp_fport == fport &&
1015 inp->inp_lport == lport) {
1016 /*
1017 * XXX We should be able to directly return
1018 * the inp here, without any checks.
1019 * Well unless both bound with SO_REUSEPORT?
1020 */
1021 if (prison_flag(inp->inp_cred, PR_IP6))
1022 return (inp);
1023 if (tmpinp == NULL)
1024 tmpinp = inp;
1025 }
1026 }
1027 if (tmpinp != NULL)
1028 return (tmpinp);
1029
1030 /*
1031 * Then look for a wildcard match, if requested.
1032 */
1033 if ((lookupflags & INPLOOKUP_WILDCARD) != 0) {
1034 struct inpcb *local_wild = NULL, *local_exact = NULL;
1035 struct inpcb *jail_wild = NULL;
1036 int injail;
1037
1038 /*
1039 * Order of socket selection - we always prefer jails.
1040 * 1. jailed, non-wild.
1041 * 2. jailed, wild.
1042 * 3. non-jailed, non-wild.
1043 * 4. non-jailed, wild.
1044 */
1045 head = &pcbinfo->ipi_hashbase[INP_PCBHASH(INADDR_ANY, lport,
1046 0, pcbinfo->ipi_hashmask)];
1047 LIST_FOREACH(inp, head, inp_hash) {
1048 /* XXX inp locking */
1049 if ((inp->inp_vflag & INP_IPV6) == 0)
1050 continue;
1051
1052 if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr) ||
1053 inp->inp_lport != lport) {
1054 continue;
1055 }
1056
1057 /* XXX inp locking */
1058 if (faith && (inp->inp_flags & INP_FAITH) == 0)
1059 continue;
1060
1061 injail = prison_flag(inp->inp_cred, PR_IP6);
1062 if (injail) {
1063 if (prison_check_ip6(inp->inp_cred,
1064 laddr) != 0)
1065 continue;
1066 } else {
1067 if (local_exact != NULL)
1068 continue;
1069 }
1070
1071 if (IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, laddr)) {
1072 if (injail)
1073 return (inp);
1074 else
1075 local_exact = inp;
1076 } else if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)) {
1077 if (injail)
1078 jail_wild = inp;
1079 else
1080 local_wild = inp;
1081 }
1082 } /* LIST_FOREACH */
1083
1084 if (jail_wild != NULL)
1085 return (jail_wild);
1086 if (local_exact != NULL)
1087 return (local_exact);
1088 if (local_wild != NULL)
1089 return (local_wild);
1090 } /* if ((lookupflags & INPLOOKUP_WILDCARD) != 0) */
1091
1092 /*
1093 * Not found.
1094 */
1095 return (NULL);
1096 }
1097
1098 /*
1099 * Lookup PCB in hash list, using pcbinfo tables. This variation locks the
1100 * hash list lock, and will return the inpcb locked (i.e., requires
1101 * INPLOOKUP_LOCKPCB).
1102 */
1103 static struct inpcb *
1104 in6_pcblookup_hash(struct inpcbinfo *pcbinfo, struct in6_addr *faddr,
1105 u_int fport, struct in6_addr *laddr, u_int lport, int lookupflags,
1106 struct ifnet *ifp)
1107 {
1108 struct inpcb *inp;
1109
1110 INP_HASH_RLOCK(pcbinfo);
1111 inp = in6_pcblookup_hash_locked(pcbinfo, faddr, fport, laddr, lport,
1112 (lookupflags & ~(INPLOOKUP_RLOCKPCB | INPLOOKUP_WLOCKPCB)), ifp);
1113 if (inp != NULL) {
1114 in_pcbref(inp);
1115 INP_HASH_RUNLOCK(pcbinfo);
1116 if (lookupflags & INPLOOKUP_WLOCKPCB) {
1117 INP_WLOCK(inp);
1118 if (in_pcbrele_wlocked(inp))
1119 return (NULL);
1120 } else if (lookupflags & INPLOOKUP_RLOCKPCB) {
1121 INP_RLOCK(inp);
1122 if (in_pcbrele_rlocked(inp))
1123 return (NULL);
1124 } else
1125 panic("%s: locking bug", __func__);
1126 } else
1127 INP_HASH_RUNLOCK(pcbinfo);
1128 return (inp);
1129 }
1130
1131 /*
1132 * Public inpcb lookup routines, accepting a 4-tuple, and optionally, an mbuf
1133 * from which a pre-calculated hash value may be extracted.
1134 *
1135 * Possibly more of this logic should be in in6_pcbgroup.c.
1136 */
1137 struct inpcb *
1138 in6_pcblookup(struct inpcbinfo *pcbinfo, struct in6_addr *faddr, u_int fport,
1139 struct in6_addr *laddr, u_int lport, int lookupflags, struct ifnet *ifp)
1140 {
1141 #if defined(PCBGROUP)
1142 struct inpcbgroup *pcbgroup;
1143 #endif
1144
1145 KASSERT((lookupflags & ~INPLOOKUP_MASK) == 0,
1146 ("%s: invalid lookup flags %d", __func__, lookupflags));
1147 KASSERT((lookupflags & (INPLOOKUP_RLOCKPCB | INPLOOKUP_WLOCKPCB)) != 0,
1148 ("%s: LOCKPCB not set", __func__));
1149
1150 #if defined(PCBGROUP)
1151 if (in_pcbgroup_enabled(pcbinfo)) {
1152 pcbgroup = in6_pcbgroup_bytuple(pcbinfo, laddr, lport, faddr,
1153 fport);
1154 return (in6_pcblookup_group(pcbinfo, pcbgroup, faddr, fport,
1155 laddr, lport, lookupflags, ifp));
1156 }
1157 #endif
1158 return (in6_pcblookup_hash(pcbinfo, faddr, fport, laddr, lport,
1159 lookupflags, ifp));
1160 }
1161
1162 struct inpcb *
1163 in6_pcblookup_mbuf(struct inpcbinfo *pcbinfo, struct in6_addr *faddr,
1164 u_int fport, struct in6_addr *laddr, u_int lport, int lookupflags,
1165 struct ifnet *ifp, struct mbuf *m)
1166 {
1167 #ifdef PCBGROUP
1168 struct inpcbgroup *pcbgroup;
1169 #endif
1170
1171 KASSERT((lookupflags & ~INPLOOKUP_MASK) == 0,
1172 ("%s: invalid lookup flags %d", __func__, lookupflags));
1173 KASSERT((lookupflags & (INPLOOKUP_RLOCKPCB | INPLOOKUP_WLOCKPCB)) != 0,
1174 ("%s: LOCKPCB not set", __func__));
1175
1176 #ifdef PCBGROUP
1177 if (in_pcbgroup_enabled(pcbinfo)) {
1178 pcbgroup = in6_pcbgroup_byhash(pcbinfo, M_HASHTYPE_GET(m),
1179 m->m_pkthdr.flowid);
1180 if (pcbgroup != NULL)
1181 return (in6_pcblookup_group(pcbinfo, pcbgroup, faddr,
1182 fport, laddr, lport, lookupflags, ifp));
1183 pcbgroup = in6_pcbgroup_bytuple(pcbinfo, laddr, lport, faddr,
1184 fport);
1185 return (in6_pcblookup_group(pcbinfo, pcbgroup, faddr, fport,
1186 laddr, lport, lookupflags, ifp));
1187 }
1188 #endif
1189 return (in6_pcblookup_hash(pcbinfo, faddr, fport, laddr, lport,
1190 lookupflags, ifp));
1191 }
1192
1193 void
1194 init_sin6(struct sockaddr_in6 *sin6, struct mbuf *m)
1195 {
1196 struct ip6_hdr *ip;
1197
1198 ip = mtod(m, struct ip6_hdr *);
1199 bzero(sin6, sizeof(*sin6));
1200 sin6->sin6_len = sizeof(*sin6);
1201 sin6->sin6_family = AF_INET6;
1202 sin6->sin6_addr = ip->ip6_src;
1203
1204 (void)sa6_recoverscope(sin6); /* XXX: should catch errors... */
1205
1206 return;
1207 }
Cache object: 9a256ac96723b6647325077057d7e2ef
|