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