]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/netinet6/in6_pcb.c
Prepare for future integration between CAM and newbus. xpt_bus_register
[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/priv.h>
81 #include <sys/proc.h>
82 #include <sys/jail.h>
83
84 #include <vm/uma.h>
85
86 #include <net/if.h>
87 #include <net/if_types.h>
88 #include <net/route.h>
89
90 #include <netinet/in.h>
91 #include <netinet/in_var.h>
92 #include <netinet/in_systm.h>
93 #include <netinet/tcp_var.h>
94 #include <netinet/ip6.h>
95 #include <netinet/ip_var.h>
96 #include <netinet6/ip6_var.h>
97 #include <netinet6/nd6.h>
98 #include <netinet/in_pcb.h>
99 #include <netinet6/in6_pcb.h>
100 #include <netinet6/scope6_var.h>
101
102 #ifdef IPSEC
103 #include <netinet6/ipsec.h>
104 #ifdef INET6
105 #include <netinet6/ipsec6.h>
106 #endif
107 #include <netinet6/ah.h>
108 #ifdef INET6
109 #include <netinet6/ah6.h>
110 #endif
111 #include <netkey/key.h>
112 #endif /* IPSEC */
113
114 #ifdef FAST_IPSEC
115 #include <netipsec/ipsec.h>
116 #include <netipsec/ipsec6.h>
117 #include <netipsec/key.h>
118 #endif /* FAST_IPSEC */
119
120 struct  in6_addr zeroin6_addr;
121
122 int
123 in6_pcbbind(inp, nam, cred)
124         register struct inpcb *inp;
125         struct sockaddr *nam;
126         struct ucred *cred;
127 {
128         struct socket *so = inp->inp_socket;
129         struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)NULL;
130         struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
131         u_short lport = 0;
132         int wild = 0, reuseport = (so->so_options & SO_REUSEPORT);
133
134         INP_INFO_WLOCK_ASSERT(pcbinfo);
135         INP_LOCK_ASSERT(inp);
136
137         if (!in6_ifaddr) /* XXX broken! */
138                 return (EADDRNOTAVAIL);
139         if (inp->inp_lport || !IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr))
140                 return (EINVAL);
141         if ((so->so_options & (SO_REUSEADDR|SO_REUSEPORT)) == 0)
142                 wild = INPLOOKUP_WILDCARD;
143         if (nam) {
144                 int error;
145
146                 sin6 = (struct sockaddr_in6 *)nam;
147                 if (nam->sa_len != sizeof(*sin6))
148                         return (EINVAL);
149                 /*
150                  * family check.
151                  */
152                 if (nam->sa_family != AF_INET6)
153                         return (EAFNOSUPPORT);
154
155                 if ((error = sa6_embedscope(sin6, ip6_use_defzone)) != 0)
156                         return(error);
157
158                 lport = sin6->sin6_port;
159                 if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) {
160                         /*
161                          * Treat SO_REUSEADDR as SO_REUSEPORT for multicast;
162                          * allow compepte duplication of binding if
163                          * SO_REUSEPORT is set, or if SO_REUSEADDR is set
164                          * and a multicast address is bound on both
165                          * new and duplicated sockets.
166                          */
167                         if (so->so_options & SO_REUSEADDR)
168                                 reuseport = SO_REUSEADDR|SO_REUSEPORT;
169                 } else if (!IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
170                         struct ifaddr *ia = NULL;
171
172                         sin6->sin6_port = 0;            /* yech... */
173                         if ((ia = ifa_ifwithaddr((struct sockaddr *)sin6)) == 0)
174                                 return (EADDRNOTAVAIL);
175
176                         /*
177                          * XXX: bind to an anycast address might accidentally
178                          * cause sending a packet with anycast source address.
179                          * We should allow to bind to a deprecated address, since
180                          * the application dares to use it.
181                          */
182                         if (ia &&
183                             ((struct in6_ifaddr *)ia)->ia6_flags &
184                             (IN6_IFF_ANYCAST|IN6_IFF_NOTREADY|IN6_IFF_DETACHED)) {
185                                 return (EADDRNOTAVAIL);
186                         }
187                 }
188                 if (lport) {
189                         struct inpcb *t;
190
191                         /* GROSS */
192                         if (ntohs(lport) <= ipport_reservedhigh &&
193                             ntohs(lport) >= ipport_reservedlow &&
194                             priv_check_cred(cred, PRIV_NETINET_RESERVEDPORT,
195                             0))
196                                 return (EACCES);
197                         if (!IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr) &&
198                             priv_check_cred(so->so_cred,
199                             PRIV_NETINET_REUSEPORT, 0) != 0) {
200                                 t = in6_pcblookup_local(pcbinfo,
201                                     &sin6->sin6_addr, lport,
202                                     INPLOOKUP_WILDCARD);
203                                 if (t &&
204                                     ((t->inp_vflag & INP_TIMEWAIT) == 0) &&
205                                     (so->so_type != SOCK_STREAM ||
206                                      IN6_IS_ADDR_UNSPECIFIED(&t->in6p_faddr)) &&
207                                     (!IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr) ||
208                                      !IN6_IS_ADDR_UNSPECIFIED(&t->in6p_laddr) ||
209                                      (t->inp_socket->so_options & SO_REUSEPORT) 
210                                       == 0) && (so->so_cred->cr_uid !=
211                                      t->inp_socket->so_cred->cr_uid))
212                                         return (EADDRINUSE);
213                                 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0 &&
214                                     IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
215                                         struct sockaddr_in sin;
216
217                                         in6_sin6_2_sin(&sin, sin6);
218                                         t = in_pcblookup_local(pcbinfo,
219                                                 sin.sin_addr, lport,
220                                                 INPLOOKUP_WILDCARD);
221                                         if (t &&
222                                             ((t->inp_vflag &
223                                               INP_TIMEWAIT) == 0) &&
224                                             (so->so_type != SOCK_STREAM ||
225                                              ntohl(t->inp_faddr.s_addr) ==
226                                               INADDR_ANY) &&
227                                             (so->so_cred->cr_uid !=
228                                              t->inp_socket->so_cred->cr_uid))
229                                                 return (EADDRINUSE);
230                                 }
231                         }
232                         t = in6_pcblookup_local(pcbinfo, &sin6->sin6_addr,
233                                                 lport, wild);
234                         if (t && (reuseport & ((t->inp_vflag & INP_TIMEWAIT) ?
235                             intotw(t)->tw_so_options : 
236                             t->inp_socket->so_options)) == 0)
237                                 return (EADDRINUSE);
238                         if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0 &&
239                             IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
240                                 struct sockaddr_in sin;
241
242                                 in6_sin6_2_sin(&sin, sin6);
243                                 t = in_pcblookup_local(pcbinfo, sin.sin_addr,
244                                                        lport, wild);
245                                 if (t && t->inp_vflag & INP_TIMEWAIT) {
246                                         if ((reuseport & 
247                                             intotw(t)->tw_so_options) == 0 &&
248                                             (ntohl(t->inp_laddr.s_addr) !=
249                                              INADDR_ANY || ((inp->inp_vflag & 
250                                              INP_IPV6PROTO) == 
251                                              (t->inp_vflag & INP_IPV6PROTO))))
252                                                 return (EADDRINUSE);
253                                 }
254                                 else if (t && 
255                                     (reuseport & t->inp_socket->so_options) 
256                                     == 0 && (ntohl(t->inp_laddr.s_addr) != 
257                                     INADDR_ANY || INP_SOCKAF(so) ==
258                                      INP_SOCKAF(t->inp_socket)))
259                                         return (EADDRINUSE);
260                         }
261                 }
262                 inp->in6p_laddr = sin6->sin6_addr;
263         }
264         if (lport == 0) {
265                 int e;
266                 if ((e = in6_pcbsetport(&inp->in6p_laddr, inp, cred)) != 0)
267                         return (e);
268         }
269         else {
270                 inp->inp_lport = lport;
271                 if (in_pcbinshash(inp) != 0) {
272                         inp->in6p_laddr = in6addr_any;
273                         inp->inp_lport = 0;
274                         return (EAGAIN);
275                 }
276         }
277         return (0);
278 }
279
280 /*
281  *   Transform old in6_pcbconnect() into an inner subroutine for new
282  *   in6_pcbconnect(): Do some validity-checking on the remote
283  *   address (in mbuf 'nam') and then determine local host address
284  *   (i.e., which interface) to use to access that remote host.
285  *
286  *   This preserves definition of in6_pcbconnect(), while supporting a
287  *   slightly different version for T/TCP.  (This is more than
288  *   a bit of a kludge, but cleaning up the internal interfaces would
289  *   have forced minor changes in every protocol).
290  */
291
292 int
293 in6_pcbladdr(inp, nam, plocal_addr6)
294         register struct inpcb *inp;
295         struct sockaddr *nam;
296         struct in6_addr **plocal_addr6;
297 {
298         register struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)nam;
299         int error = 0;
300         struct ifnet *ifp = NULL;
301         int scope_ambiguous = 0;
302
303         INP_INFO_WLOCK_ASSERT(inp->inp_pcbinfo);
304         INP_LOCK_ASSERT(inp);
305
306         if (nam->sa_len != sizeof (*sin6))
307                 return (EINVAL);
308         if (sin6->sin6_family != AF_INET6)
309                 return (EAFNOSUPPORT);
310         if (sin6->sin6_port == 0)
311                 return (EADDRNOTAVAIL);
312
313         if (sin6->sin6_scope_id == 0 && !ip6_use_defzone)
314                 scope_ambiguous = 1;
315         if ((error = sa6_embedscope(sin6, ip6_use_defzone)) != 0)
316                 return(error);
317
318         if (in6_ifaddr) {
319                 /*
320                  * If the destination address is UNSPECIFIED addr,
321                  * use the loopback addr, e.g ::1.
322                  */
323                 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr))
324                         sin6->sin6_addr = in6addr_loopback;
325         }
326
327         /*
328          * XXX: in6_selectsrc might replace the bound local address
329          * with the address specified by setsockopt(IPV6_PKTINFO).
330          * Is it the intended behavior?
331          */
332         *plocal_addr6 = in6_selectsrc(sin6, inp->in6p_outputopts,
333                                       inp->in6p_moptions, NULL,
334                                       &inp->in6p_laddr, &ifp, &error);
335         if (ifp && scope_ambiguous &&
336             (error = in6_setscope(&sin6->sin6_addr, ifp, NULL)) != 0) {
337                 return(error);
338         }
339
340         if (*plocal_addr6 == 0) {
341                 if (error == 0)
342                         error = EADDRNOTAVAIL;
343                 return (error);
344         }
345         /*
346          * Don't do pcblookup call here; return interface in
347          * plocal_addr6
348          * and exit to caller, that will do the lookup.
349          */
350
351         return (0);
352 }
353
354 /*
355  * Outer subroutine:
356  * Connect from a socket to a specified address.
357  * Both address and port must be specified in argument sin.
358  * If don't have a local address for this socket yet,
359  * then pick one.
360  */
361 int
362 in6_pcbconnect(inp, nam, cred)
363         register struct inpcb *inp;
364         struct sockaddr *nam;
365         struct ucred *cred;
366 {
367         struct in6_addr *addr6;
368         register struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)nam;
369         int error;
370
371         INP_INFO_WLOCK_ASSERT(inp->inp_pcbinfo);
372         INP_LOCK_ASSERT(inp);
373
374         /*
375          * Call inner routine, to assign local interface address.
376          * in6_pcbladdr() may automatically fill in sin6_scope_id.
377          */
378         if ((error = in6_pcbladdr(inp, nam, &addr6)) != 0)
379                 return (error);
380
381         if (in6_pcblookup_hash(inp->inp_pcbinfo, &sin6->sin6_addr,
382                                sin6->sin6_port,
383                               IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)
384                               ? addr6 : &inp->in6p_laddr,
385                               inp->inp_lport, 0, NULL) != NULL) {
386                 return (EADDRINUSE);
387         }
388         if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)) {
389                 if (inp->inp_lport == 0) {
390                         error = in6_pcbbind(inp, (struct sockaddr *)0, cred);
391                         if (error)
392                                 return (error);
393                 }
394                 inp->in6p_laddr = *addr6;
395         }
396         inp->in6p_faddr = sin6->sin6_addr;
397         inp->inp_fport = sin6->sin6_port;
398         /* update flowinfo - draft-itojun-ipv6-flowlabel-api-00 */
399         inp->in6p_flowinfo &= ~IPV6_FLOWLABEL_MASK;
400         if (inp->in6p_flags & IN6P_AUTOFLOWLABEL)
401                 inp->in6p_flowinfo |=
402                     (htonl(ip6_randomflowlabel()) & IPV6_FLOWLABEL_MASK);
403
404         in_pcbrehash(inp);
405 #ifdef IPSEC
406         if (inp->inp_socket->so_type == SOCK_STREAM)
407                 ipsec_pcbconn(inp->inp_sp);
408 #endif
409         return (0);
410 }
411
412 void
413 in6_pcbdisconnect(inp)
414         struct inpcb *inp;
415 {
416
417         INP_INFO_WLOCK_ASSERT(inp->inp_pcbinfo);
418         INP_LOCK_ASSERT(inp);
419
420         bzero((caddr_t)&inp->in6p_faddr, sizeof(inp->in6p_faddr));
421         inp->inp_fport = 0;
422         /* clear flowinfo - draft-itojun-ipv6-flowlabel-api-00 */
423         inp->in6p_flowinfo &= ~IPV6_FLOWLABEL_MASK;
424         in_pcbrehash(inp);
425 #ifdef IPSEC
426         ipsec_pcbdisconn(inp->inp_sp);
427 #endif
428 }
429
430 void
431 in6_pcbdetach(struct inpcb *inp)
432 {
433
434         KASSERT(inp->inp_socket != NULL, ("in6_pcbdetach: inp_socket == NULL"));
435         inp->inp_socket->so_pcb = NULL;
436         inp->inp_socket = NULL;
437 }
438
439 void
440 in6_pcbfree(struct inpcb *inp)
441 {
442         struct inpcbinfo *ipi = inp->inp_pcbinfo;
443
444         KASSERT(inp->inp_socket == NULL, ("in6_pcbfree: inp_socket != NULL"));
445         INP_INFO_WLOCK_ASSERT(inp->inp_pcbinfo);
446         INP_LOCK_ASSERT(inp);
447
448 #if defined(IPSEC) || defined(FAST_IPSEC)
449         if (inp->in6p_sp != NULL)
450                 ipsec6_delete_pcbpolicy(inp);
451 #endif /* IPSEC */
452         inp->inp_gencnt = ++ipi->ipi_gencnt;
453         in_pcbremlists(inp);
454         ip6_freepcbopts(inp->in6p_outputopts);
455         ip6_freemoptions(inp->in6p_moptions);
456         /* Check and free IPv4 related resources in case of mapped addr */
457         if (inp->inp_options)
458                 (void)m_free(inp->inp_options);
459         if (inp->inp_moptions != NULL)
460                 inp_freemoptions(inp->inp_moptions);
461         inp->inp_vflag = 0;
462         INP_UNLOCK(inp);
463         uma_zfree(ipi->ipi_zone, inp);
464 }
465
466 struct sockaddr *
467 in6_sockaddr(port, addr_p)
468         in_port_t port;
469         struct in6_addr *addr_p;
470 {
471         struct sockaddr_in6 *sin6;
472
473         MALLOC(sin6, struct sockaddr_in6 *, sizeof *sin6, M_SONAME, M_WAITOK);
474         bzero(sin6, sizeof *sin6);
475         sin6->sin6_family = AF_INET6;
476         sin6->sin6_len = sizeof(*sin6);
477         sin6->sin6_port = port;
478         sin6->sin6_addr = *addr_p;
479         (void)sa6_recoverscope(sin6); /* XXX: should catch errors */
480
481         return (struct sockaddr *)sin6;
482 }
483
484 struct sockaddr *
485 in6_v4mapsin6_sockaddr(port, addr_p)
486         in_port_t port;
487         struct in_addr *addr_p;
488 {
489         struct sockaddr_in sin;
490         struct sockaddr_in6 *sin6_p;
491
492         bzero(&sin, sizeof sin);
493         sin.sin_family = AF_INET;
494         sin.sin_len = sizeof(sin);
495         sin.sin_port = port;
496         sin.sin_addr = *addr_p;
497
498         MALLOC(sin6_p, struct sockaddr_in6 *, sizeof *sin6_p, M_SONAME,
499                 M_WAITOK);
500         in6_sin_2_v4mapsin6(&sin, sin6_p);
501
502         return (struct sockaddr *)sin6_p;
503 }
504
505 int
506 in6_getsockaddr(so, nam)
507         struct socket *so;
508         struct sockaddr **nam;
509 {
510         register struct inpcb *inp;
511         struct in6_addr addr;
512         in_port_t port;
513
514         inp = sotoinpcb(so);
515         KASSERT(inp != NULL, ("in6_getsockaddr: inp == NULL"));
516
517         INP_LOCK(inp);
518         port = inp->inp_lport;
519         addr = inp->in6p_laddr;
520         INP_UNLOCK(inp);
521
522         *nam = in6_sockaddr(port, &addr);
523         return 0;
524 }
525
526 int
527 in6_getpeeraddr(so, nam)
528         struct socket *so;
529         struct sockaddr **nam;
530 {
531         struct inpcb *inp;
532         struct in6_addr addr;
533         in_port_t port;
534
535         inp = sotoinpcb(so);
536         KASSERT(inp != NULL, ("in6_getpeeraddr: inp == NULL"));
537
538         INP_LOCK(inp);
539         port = inp->inp_fport;
540         addr = inp->in6p_faddr;
541         INP_UNLOCK(inp);
542
543         *nam = in6_sockaddr(port, &addr);
544         return 0;
545 }
546
547 int
548 in6_mapped_sockaddr(struct socket *so, struct sockaddr **nam)
549 {
550         struct  inpcb *inp;
551         int     error;
552
553         inp = sotoinpcb(so);
554         KASSERT(inp != NULL, ("in6_mapped_sockaddr: inp == NULL"));
555
556         if ((inp->inp_vflag & (INP_IPV4 | INP_IPV6)) == INP_IPV4) {
557                 error = in_getsockaddr(so, nam);
558                 if (error == 0)
559                         in6_sin_2_v4mapsin6_in_sock(nam);
560         } else {
561                 /* scope issues will be handled in in6_getsockaddr(). */
562                 error = in6_getsockaddr(so, nam);
563         }
564
565         return error;
566 }
567
568 int
569 in6_mapped_peeraddr(struct socket *so, struct sockaddr **nam)
570 {
571         struct  inpcb *inp;
572         int     error;
573
574         inp = sotoinpcb(so);
575         KASSERT(inp != NULL, ("in6_mapped_peeraddr: inp == NULL"));
576
577         if ((inp->inp_vflag & (INP_IPV4 | INP_IPV6)) == INP_IPV4) {
578                 error = in_getpeeraddr(so, nam);
579                 if (error == 0)
580                         in6_sin_2_v4mapsin6_in_sock(nam);
581         } else
582         /* scope issues will be handled in in6_getpeeraddr(). */
583         error = in6_getpeeraddr(so, nam);
584
585         return error;
586 }
587
588 /*
589  * Pass some notification to all connections of a protocol
590  * associated with address dst.  The local address and/or port numbers
591  * may be specified to limit the search.  The "usual action" will be
592  * taken, depending on the ctlinput cmd.  The caller must filter any
593  * cmds that are uninteresting (e.g., no error in the map).
594  * Call the protocol specific routine (if any) to report
595  * any errors for each matching socket.
596  */
597 void
598 in6_pcbnotify(pcbinfo, dst, fport_arg, src, lport_arg, cmd, cmdarg, notify)
599         struct inpcbinfo *pcbinfo;
600         struct sockaddr *dst;
601         const struct sockaddr *src;
602         u_int fport_arg, lport_arg;
603         int cmd;
604         void *cmdarg;
605         struct inpcb *(*notify) __P((struct inpcb *, int));
606 {
607         struct inpcbhead *head;
608         struct inpcb *inp, *ninp;
609         struct sockaddr_in6 sa6_src, *sa6_dst;
610         u_short fport = fport_arg, lport = lport_arg;
611         u_int32_t flowinfo;
612         int errno;
613
614         if ((unsigned)cmd >= PRC_NCMDS || dst->sa_family != AF_INET6)
615                 return;
616
617         sa6_dst = (struct sockaddr_in6 *)dst;
618         if (IN6_IS_ADDR_UNSPECIFIED(&sa6_dst->sin6_addr))
619                 return;
620
621         /*
622          * note that src can be NULL when we get notify by local fragmentation.
623          */
624         sa6_src = (src == NULL) ? sa6_any : *(const struct sockaddr_in6 *)src;
625         flowinfo = sa6_src.sin6_flowinfo;
626
627         /*
628          * Redirects go to all references to the destination,
629          * and use in6_rtchange to invalidate the route cache.
630          * Dead host indications: also use in6_rtchange to invalidate
631          * the cache, and deliver the error to all the sockets.
632          * Otherwise, if we have knowledge of the local port and address,
633          * deliver only to that socket.
634          */
635         if (PRC_IS_REDIRECT(cmd) || cmd == PRC_HOSTDEAD) {
636                 fport = 0;
637                 lport = 0;
638                 bzero((caddr_t)&sa6_src.sin6_addr, sizeof(sa6_src.sin6_addr));
639
640                 if (cmd != PRC_HOSTDEAD)
641                         notify = in6_rtchange;
642         }
643         errno = inet6ctlerrmap[cmd];
644         head = pcbinfo->ipi_listhead;
645         INP_INFO_WLOCK(pcbinfo);
646         for (inp = LIST_FIRST(head); inp != NULL; inp = ninp) {
647                 INP_LOCK(inp);
648                 ninp = LIST_NEXT(inp, inp_list);
649
650                 if ((inp->inp_vflag & INP_IPV6) == 0) {
651                         INP_UNLOCK(inp);
652                         continue;
653                 }
654
655                 /*
656                  * If the error designates a new path MTU for a destination
657                  * and the application (associated with this socket) wanted to
658                  * know the value, notify. Note that we notify for all
659                  * disconnected sockets if the corresponding application
660                  * wanted. This is because some UDP applications keep sending
661                  * sockets disconnected.
662                  * XXX: should we avoid to notify the value to TCP sockets?
663                  */
664                 if (cmd == PRC_MSGSIZE && (inp->inp_flags & IN6P_MTU) != 0 &&
665                     (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr) ||
666                      IN6_ARE_ADDR_EQUAL(&inp->in6p_faddr, &sa6_dst->sin6_addr))) {
667                         ip6_notify_pmtu(inp, (struct sockaddr_in6 *)dst,
668                                         (u_int32_t *)cmdarg);
669                 }
670
671                 /*
672                  * Detect if we should notify the error. If no source and
673                  * destination ports are specifed, but non-zero flowinfo and
674                  * local address match, notify the error. This is the case
675                  * when the error is delivered with an encrypted buffer
676                  * by ESP. Otherwise, just compare addresses and ports
677                  * as usual.
678                  */
679                 if (lport == 0 && fport == 0 && flowinfo &&
680                     inp->inp_socket != NULL &&
681                     flowinfo == (inp->in6p_flowinfo & IPV6_FLOWLABEL_MASK) &&
682                     IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, &sa6_src.sin6_addr))
683                         goto do_notify;
684                 else if (!IN6_ARE_ADDR_EQUAL(&inp->in6p_faddr,
685                                              &sa6_dst->sin6_addr) ||
686                          inp->inp_socket == 0 ||
687                          (lport && inp->inp_lport != lport) ||
688                          (!IN6_IS_ADDR_UNSPECIFIED(&sa6_src.sin6_addr) &&
689                           !IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr,
690                                               &sa6_src.sin6_addr)) ||
691                          (fport && inp->inp_fport != fport)) {
692                         INP_UNLOCK(inp);
693                         continue;
694                 }
695
696           do_notify:
697                 if (notify) {
698                         if ((*notify)(inp, errno))
699                                 INP_UNLOCK(inp);
700                 } else
701                         INP_UNLOCK(inp);
702         }
703         INP_INFO_WUNLOCK(pcbinfo);
704 }
705
706 /*
707  * Lookup a PCB based on the local address and port.
708  */
709 struct inpcb *
710 in6_pcblookup_local(pcbinfo, laddr, lport_arg, wild_okay)
711         struct inpcbinfo *pcbinfo;
712         struct in6_addr *laddr;
713         u_int lport_arg;
714         int wild_okay;
715 {
716         register struct inpcb *inp;
717         int matchwild = 3, wildcard;
718         u_short lport = lport_arg;
719
720         INP_INFO_WLOCK_ASSERT(pcbinfo);
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->ipi_hashbase[INP_PCBHASH(INADDR_ANY, lport,
729                     0, pcbinfo->ipi_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->ipi_porthashbase[INP_PCBPORTHASH(lport,
757                     pcbinfo->ipi_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(pcbinfo, ifp)
799         struct inpcbinfo *pcbinfo;
800         struct ifnet *ifp;
801 {
802         struct in6pcb *in6p;
803         struct ip6_moptions *im6o;
804         struct in6_multi_mship *imm, *nimm;
805
806         INP_INFO_RLOCK(pcbinfo);
807         LIST_FOREACH(in6p, pcbinfo->ipi_listhead, inp_list) {
808                 INP_LOCK(in6p);
809                 im6o = in6p->in6p_moptions;
810                 if ((in6p->inp_vflag & INP_IPV6) &&
811                     im6o) {
812                         /*
813                          * Unselect the outgoing interface if it is being
814                          * detached.
815                          */
816                         if (im6o->im6o_multicast_ifp == ifp)
817                                 im6o->im6o_multicast_ifp = NULL;
818
819                         /*
820                          * Drop multicast group membership if we joined
821                          * through the interface being detached.
822                          * XXX controversial - is it really legal for kernel
823                          * to force this?
824                          */
825                         for (imm = im6o->im6o_memberships.lh_first;
826                              imm != NULL; imm = nimm) {
827                                 nimm = imm->i6mm_chain.le_next;
828                                 if (imm->i6mm_maddr->in6m_ifp == ifp) {
829                                         LIST_REMOVE(imm, i6mm_chain);
830                                         in6_delmulti(imm->i6mm_maddr);
831                                         free(imm, M_IP6MADDR);
832                                 }
833                         }
834                 }
835                 INP_UNLOCK(in6p);
836         }
837         INP_INFO_RUNLOCK(pcbinfo);
838 }
839
840 /*
841  * Check for alternatives when higher level complains
842  * about service problems.  For now, invalidate cached
843  * routing information.  If the route was created dynamically
844  * (by a redirect), time to try a default gateway again.
845  */
846 void
847 in6_losing(in6p)
848         struct inpcb *in6p;
849 {
850         /*
851          * We don't store route pointers in the routing table anymore
852          */
853         return;
854 }
855
856 /*
857  * After a routing change, flush old routing
858  * and allocate a (hopefully) better one.
859  */
860 struct inpcb *
861 in6_rtchange(inp, errno)
862         struct inpcb *inp;
863         int errno;
864 {
865         /*
866          * We don't store route pointers in the routing table anymore
867          */
868         return inp;
869 }
870
871 /*
872  * Lookup PCB in hash list.
873  */
874 struct inpcb *
875 in6_pcblookup_hash(pcbinfo, faddr, fport_arg, laddr, lport_arg, wildcard, ifp)
876         struct inpcbinfo *pcbinfo;
877         struct in6_addr *faddr, *laddr;
878         u_int fport_arg, lport_arg;
879         int wildcard;
880         struct ifnet *ifp;
881 {
882         struct inpcbhead *head;
883         register struct inpcb *inp;
884         u_short fport = fport_arg, lport = lport_arg;
885         int faith;
886
887         INP_INFO_RLOCK_ASSERT(pcbinfo);
888
889         if (faithprefix_p != NULL)
890                 faith = (*faithprefix_p)(laddr);
891         else
892                 faith = 0;
893
894         /*
895          * First look for an exact match.
896          */
897         head = &pcbinfo->ipi_hashbase[
898             INP_PCBHASH(faddr->s6_addr32[3] /* XXX */, lport, fport,
899             pcbinfo->ipi_hashmask)];
900         LIST_FOREACH(inp, head, inp_hash) {
901                 if ((inp->inp_vflag & INP_IPV6) == 0)
902                         continue;
903                 if (IN6_ARE_ADDR_EQUAL(&inp->in6p_faddr, faddr) &&
904                     IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, laddr) &&
905                     inp->inp_fport == fport &&
906                     inp->inp_lport == lport) {
907                         /*
908                          * Found.
909                          */
910                         return (inp);
911                 }
912         }
913         if (wildcard) {
914                 struct inpcb *local_wild = NULL;
915
916                 head = &pcbinfo->ipi_hashbase[INP_PCBHASH(INADDR_ANY, lport,
917                     0, pcbinfo->ipi_hashmask)];
918                 LIST_FOREACH(inp, head, inp_hash) {
919                         if ((inp->inp_vflag & INP_IPV6) == 0)
920                                 continue;
921                         if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr) &&
922                             inp->inp_lport == lport) {
923                                 if (faith && (inp->inp_flags & INP_FAITH) == 0)
924                                         continue;
925                                 if (IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr,
926                                                        laddr))
927                                         return (inp);
928                                 else if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr))
929                                         local_wild = inp;
930                         }
931                 }
932                 return (local_wild);
933         }
934
935         /*
936          * Not found.
937          */
938         return (NULL);
939 }
940
941 void
942 init_sin6(struct sockaddr_in6 *sin6, struct mbuf *m)
943 {
944         struct ip6_hdr *ip;
945
946         ip = mtod(m, struct ip6_hdr *);
947         bzero(sin6, sizeof(*sin6));
948         sin6->sin6_len = sizeof(*sin6);
949         sin6->sin6_family = AF_INET6;
950         sin6->sin6_addr = ip->ip6_src;
951
952         (void)sa6_recoverscope(sin6); /* XXX: should catch errors... */
953
954         return;
955 }