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