Remove IPsec and related code from the system.
[dragonfly.git] / sys / netinet6 / in6_pcb.c
1 /*      $FreeBSD: src/sys/netinet6/in6_pcb.c,v 1.10.2.9 2003/01/24 05:11:35 sam Exp $   */
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  * 3. 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
68 #include <sys/param.h>
69 #include <sys/systm.h>
70 #include <sys/malloc.h>
71 #include <sys/mbuf.h>
72 #include <sys/domain.h>
73 #include <sys/protosw.h>
74 #include <sys/socket.h>
75 #include <sys/socketvar.h>
76 #include <sys/sockio.h>
77 #include <sys/errno.h>
78 #include <sys/time.h>
79 #include <sys/proc.h>
80 #include <sys/priv.h>
81 #include <sys/jail.h>
82
83 #include <sys/thread2.h>
84 #include <sys/msgport2.h>
85
86 #include <vm/vm_zone.h>
87
88 #include <net/if.h>
89 #include <net/if_types.h>
90 #include <net/route.h>
91 #include <net/netisr2.h>
92
93 #include <netinet/in.h>
94 #include <netinet/in_var.h>
95 #include <netinet/in_systm.h>
96 #include <netinet/ip6.h>
97 #include <netinet/ip_var.h>
98 #include <netinet6/ip6_var.h>
99 #include <netinet6/nd6.h>
100 #include <netinet/in_pcb.h>
101 #include <netinet6/in6_pcb.h>
102
103 struct  in6_addr zeroin6_addr;
104
105 int
106 in6_pcbbind(struct inpcb *inp, struct sockaddr *nam, struct thread *td)
107 {
108         struct socket *so = inp->inp_socket;
109         struct sockaddr_in6 jsin6;
110         int error;
111
112         if (!in6_ifaddr) /* XXX broken! */
113                 return (EADDRNOTAVAIL);
114         if (inp->inp_lport || !IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr))
115                 return (EINVAL);
116
117         if (nam) {
118                 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)nam;
119                 struct inpcbinfo *pcbinfo;
120                 struct inpcbportinfo *portinfo;
121                 struct inpcbporthead *porthash;
122                 int wild = 0, reuseport = (so->so_options & SO_REUSEPORT);
123                 struct ucred *cred = NULL;
124                 struct inpcb *t;
125                 u_short lport, lport_ho;
126
127                 if ((so->so_options & (SO_REUSEADDR|SO_REUSEPORT)) == 0)
128                         wild = 1;
129                 if (td->td_proc != NULL)
130                         cred = td->td_proc->p_ucred;
131
132                 if (nam->sa_len != sizeof(*sin6))
133                         return (EINVAL);
134                 /*
135                  * family check.
136                  */
137                 if (nam->sa_family != AF_INET6)
138                         return (EAFNOSUPPORT);
139
140                 /* Reject v4-mapped address */
141                 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr))
142                         return (EADDRNOTAVAIL);
143
144                 if (!prison_replace_wildcards(td, nam))
145                         return (EINVAL);
146
147                 /* KAME hack: embed scopeid */
148                 if (in6_embedscope(&sin6->sin6_addr, sin6, inp, NULL) != 0)
149                         return (EINVAL);
150                 /* this must be cleared for ifa_ifwithaddr() */
151                 sin6->sin6_scope_id = 0;
152
153                 lport = sin6->sin6_port;
154                 if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) {
155                         /*
156                          * Treat SO_REUSEADDR as SO_REUSEPORT for multicast;
157                          * allow compepte duplication of binding if
158                          * SO_REUSEPORT is set, or if SO_REUSEADDR is set
159                          * and a multicast address is bound on both
160                          * new and duplicated sockets.
161                          */
162                         if (so->so_options & SO_REUSEADDR)
163                                 reuseport = SO_REUSEADDR|SO_REUSEPORT;
164                 } else if (!IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
165                         struct ifaddr *ia = NULL;
166
167                         sin6->sin6_port = 0;            /* yech... */
168                         if (!prison_replace_wildcards(td, (struct sockaddr *)sin6)) {
169                                 sin6->sin6_addr = kin6addr_any;
170                                 return (EINVAL);
171                         }
172                         if ((ia = ifa_ifwithaddr((struct sockaddr *)sin6)) == NULL)
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                 inp->in6p_laddr = sin6->sin6_addr;
188
189                 if (lport == 0)
190                         goto auto_select;
191                 lport_ho = ntohs(lport);
192
193                 /* GROSS */
194                 if (lport_ho < IPV6PORT_RESERVED && cred &&
195                     priv_check_cred(cred, PRIV_NETINET_RESERVEDPORT, 0)) {
196                         inp->in6p_laddr = kin6addr_any;
197                         return (EACCES);
198                 }
199
200                 /*
201                  * Locate the proper portinfo based on lport
202                  */
203                 pcbinfo = inp->inp_pcbinfo;
204                 portinfo =
205                     &pcbinfo->portinfo[lport_ho % pcbinfo->portinfo_cnt];
206                 KKASSERT((lport_ho % pcbinfo->portinfo_cnt) ==
207                     portinfo->offset);
208
209                 /*
210                  * This has to be atomic.  If the porthash is shared across
211                  * multiple protocol threads (aka tcp) then the token must
212                  * be held.
213                  */
214                 porthash = in_pcbporthash_head(portinfo, lport);
215                 GET_PORTHASH_TOKEN(porthash);
216
217                 if (so->so_cred->cr_uid != 0 &&
218                     !IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) {
219                         t = in6_pcblookup_local(porthash,
220                             &sin6->sin6_addr, lport, INPLOOKUP_WILDCARD, cred);
221                         if (t &&
222                             (so->so_cred->cr_uid !=
223                              t->inp_socket->so_cred->cr_uid)) {
224                                 inp->in6p_laddr = kin6addr_any;
225                                 error = EADDRINUSE;
226                                 goto done;
227                         }
228                 }
229                 if (cred && cred->cr_prison &&
230                     !prison_replace_wildcards(td, nam)) {
231                         inp->in6p_laddr = kin6addr_any;
232                         error = EADDRNOTAVAIL;
233                         goto done;
234                 }
235                 t = in6_pcblookup_local(porthash, &sin6->sin6_addr, lport,
236                     wild, cred);
237                 if (t && (reuseport & t->inp_socket->so_options) == 0) {
238                         inp->in6p_laddr = kin6addr_any;
239                         error = EADDRINUSE;
240                         goto done;
241                 }
242
243                 inp->inp_lport = lport;
244                 in_pcbinsporthash(porthash, inp);
245                 error = 0;
246 done:
247                 REL_PORTHASH_TOKEN(porthash);
248                 return (error);
249         } else {
250 auto_select:
251                 jsin6.sin6_addr = inp->in6p_laddr;
252                 jsin6.sin6_family = AF_INET6;
253                 if (!prison_replace_wildcards(td, (struct sockaddr*)&jsin6)) {
254                         inp->in6p_laddr = kin6addr_any;
255                         inp->inp_lport = 0;
256                         return (EINVAL);
257                 }
258
259                 return in6_pcbsetlport(&inp->in6p_laddr, inp, td);
260         }
261 }
262
263 /*
264  *   Transform old in6_pcbconnect() into an inner subroutine for new
265  *   in6_pcbconnect(): Do some validity-checking on the remote
266  *   address (in mbuf 'nam') and then determine local host address
267  *   (i.e., which interface) to use to access that remote host.
268  *
269  *   This preserves definition of in6_pcbconnect(), while supporting a
270  *   slightly different version for T/TCP.  (This is more than
271  *   a bit of a kludge, but cleaning up the internal interfaces would
272  *   have forced minor changes in every protocol).
273  */
274
275 int
276 in6_pcbladdr(struct inpcb *inp, struct sockaddr *nam,
277              struct in6_addr **plocal_addr6, struct thread *td)
278 {
279         struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)nam;
280         struct ifnet *ifp = NULL;
281         int error = 0;
282
283         if (nam->sa_len != sizeof (*sin6))
284                 return (EINVAL);
285         if (sin6->sin6_family != AF_INET6)
286                 return (EAFNOSUPPORT);
287         if (sin6->sin6_port == 0)
288                 return (EADDRNOTAVAIL);
289
290         /* KAME hack: embed scopeid */
291         if (in6_embedscope(&sin6->sin6_addr, sin6, inp, &ifp) != 0)
292                 return EINVAL;
293
294         if (in6_ifaddr) {
295                 /*
296                  * If the destination address is UNSPECIFIED addr,
297                  * use the loopback addr, e.g ::1.
298                  */
299                 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr))
300                         sin6->sin6_addr = kin6addr_loopback;
301         }
302         {
303                 /*
304                  * XXX: in6_selectsrc might replace the bound local address
305                  * with the address specified by setsockopt(IPV6_PKTINFO).
306                  * Is it the intended behavior?
307                  */
308                 *plocal_addr6 = in6_selectsrc(sin6, inp->in6p_outputopts,
309                                               inp->in6p_moptions,
310                                               &inp->in6p_route,
311                                               &inp->in6p_laddr, &error, td);
312                 if (*plocal_addr6 == NULL) {
313                         if (error == 0)
314                                 error = EADDRNOTAVAIL;
315                         return (error);
316                 }
317                 /*
318                  * Don't do pcblookup call here; return interface in
319                  * plocal_addr6
320                  * and exit to caller, that will do the lookup.
321                  */
322         }
323
324         if (inp->in6p_route.ro_rt)
325                 ifp = inp->in6p_route.ro_rt->rt_ifp;
326
327         return (0);
328 }
329
330 /*
331  * Outer subroutine:
332  * Connect from a socket to a specified address.
333  * Both address and port must be specified in argument sin.
334  * If don't have a local address for this socket yet,
335  * then pick one.
336  */
337 int
338 in6_pcbconnect(struct inpcb *inp, struct sockaddr *nam, struct thread *td)
339 {
340         struct in6_addr *addr6;
341         struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)nam;
342         int error;
343
344         /* Reject v4-mapped address */
345         if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr))
346                 return EADDRNOTAVAIL;
347
348         /*
349          * Call inner routine, to assign local interface address.
350          * in6_pcbladdr() may automatically fill in sin6_scope_id.
351          */
352         if ((error = in6_pcbladdr(inp, nam, &addr6, td)) != 0)
353                 return (error);
354
355         if (in6_pcblookup_hash(inp->inp_pcbinfo, &sin6->sin6_addr,
356                                sin6->sin6_port,
357                               IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)
358                               ? addr6 : &inp->in6p_laddr,
359                               inp->inp_lport, 0, NULL) != NULL) {
360                 return (EADDRINUSE);
361         }
362         if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)) {
363                 if (inp->inp_lport == 0) {
364                         error = in6_pcbbind(inp, NULL, td);
365                         if (error)
366                                 return (error);
367                 }
368                 inp->in6p_laddr = *addr6;
369         }
370         inp->in6p_faddr = sin6->sin6_addr;
371         inp->inp_fport = sin6->sin6_port;
372         /* update flowinfo - draft-itojun-ipv6-flowlabel-api-00 */
373         inp->in6p_flowinfo &= ~IPV6_FLOWLABEL_MASK;
374         if (inp->in6p_flags & IN6P_AUTOFLOWLABEL)
375                 inp->in6p_flowinfo |=
376                     (htonl(ip6_flow_seq++) & IPV6_FLOWLABEL_MASK);
377
378         in_pcbinsconnhash(inp);
379         return (0);
380 }
381
382 void
383 in6_pcbdisconnect(struct inpcb *inp)
384 {
385         bzero((caddr_t)&inp->in6p_faddr, sizeof(inp->in6p_faddr));
386         inp->inp_fport = 0;
387         /* clear flowinfo - draft-itojun-ipv6-flowlabel-api-00 */
388         inp->in6p_flowinfo &= ~IPV6_FLOWLABEL_MASK;
389         in_pcbremconnhash(inp);
390         if (inp->inp_socket->so_state & SS_NOFDREF)
391                 in6_pcbdetach(inp);
392 }
393
394 void
395 in6_pcbdetach(struct inpcb *inp)
396 {
397         struct socket *so = inp->inp_socket;
398         struct inpcbinfo *ipi = inp->inp_pcbinfo;
399
400         inp->inp_gencnt = ++ipi->ipi_gencnt;
401         in_pcbremlists(inp);
402         so->so_pcb = NULL;
403         KKASSERT((so->so_state & SS_ASSERTINPROG) == 0);
404         sofree(so);             /* remove pcb ref */
405
406         if (inp->in6p_options)
407                 m_freem(inp->in6p_options);
408         ip6_freepcbopts(inp->in6p_outputopts);
409         ip6_freemoptions(inp->in6p_moptions);
410         if (inp->in6p_route.ro_rt)
411                 rtfree(inp->in6p_route.ro_rt);
412         /* Check and free IPv4 related resources in case of mapped addr */
413         if (inp->inp_options)
414                 m_free(inp->inp_options);
415         ip_freemoptions(inp->inp_moptions);
416
417         kfree(inp, M_PCB);
418 }
419
420 /*
421  * The socket may have an invalid PCB, i.e. NULL.  For example, a TCP
422  * socket received RST.
423  */
424 static int
425 in6_setsockaddr(struct socket *so, struct sockaddr **nam)
426 {
427         struct inpcb *inp;
428         struct sockaddr_in6 *sin6;
429
430         KASSERT(curthread->td_type == TD_TYPE_NETISR, ("not in netisr"));
431         inp = so->so_pcb;
432         if (!inp)
433                 return EINVAL;
434
435         sin6 = kmalloc(sizeof *sin6, M_SONAME, M_WAITOK | M_ZERO);
436         sin6->sin6_family = AF_INET6;
437         sin6->sin6_len = sizeof(*sin6);
438         sin6->sin6_port = inp->inp_lport;
439         sin6->sin6_addr = inp->in6p_laddr;
440         if (IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr))
441                 sin6->sin6_scope_id = ntohs(sin6->sin6_addr.s6_addr16[1]);
442         else
443                 sin6->sin6_scope_id = 0;        /*XXX*/
444         if (IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr))
445                 sin6->sin6_addr.s6_addr16[1] = 0;
446
447         *nam = (struct sockaddr *)sin6;
448         return 0;
449 }
450
451 void
452 in6_setsockaddr_dispatch(netmsg_t msg)
453 {
454         int error;
455
456         error = in6_setsockaddr(msg->sockaddr.base.nm_so, msg->sockaddr.nm_nam);
457         lwkt_replymsg(&msg->sockaddr.base.lmsg, error);
458 }
459
460 void
461 in6_setpeeraddr_dispatch(netmsg_t msg)
462 {
463         int error;
464
465         error = in6_setpeeraddr(msg->peeraddr.base.nm_so, msg->peeraddr.nm_nam);
466         lwkt_replymsg(&msg->peeraddr.base.lmsg, error);
467 }
468
469 /*
470  * The socket may have an invalid PCB, i.e. NULL.  For example, a TCP
471  * socket received RST.
472  */
473 int
474 in6_setpeeraddr(struct socket *so, struct sockaddr **nam)
475 {
476         struct inpcb *inp;
477         struct sockaddr_in6 *sin6;
478
479         KASSERT(curthread->td_type == TD_TYPE_NETISR, ("not in netisr"));
480         inp = so->so_pcb;
481         if (!inp)
482                 return EINVAL;
483
484         sin6 = kmalloc(sizeof(*sin6), M_SONAME, M_WAITOK | M_ZERO);
485         sin6->sin6_family = AF_INET6;
486         sin6->sin6_len = sizeof(struct sockaddr_in6);
487         sin6->sin6_port = inp->inp_fport;
488         sin6->sin6_addr = inp->in6p_faddr;
489         if (IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr))
490                 sin6->sin6_scope_id = ntohs(sin6->sin6_addr.s6_addr16[1]);
491         else
492                 sin6->sin6_scope_id = 0;        /*XXX*/
493         if (IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr))
494                 sin6->sin6_addr.s6_addr16[1] = 0;
495
496         *nam = (struct sockaddr *)sin6;
497         return 0;
498 }
499
500 /*
501  * Pass some notification to all connections of a protocol
502  * associated with address dst.  The local address and/or port numbers
503  * may be specified to limit the search.  The "usual action" will be
504  * taken, depending on the ctlinput cmd.  The caller must filter any
505  * cmds that are uninteresting (e.g., no error in the map).
506  * Call the protocol specific routine (if any) to report
507  * any errors for each matching socket.
508  */
509 void
510 in6_pcbnotify(struct inpcbinfo *pcbinfo, struct sockaddr *dst, in_port_t fport,
511     const struct sockaddr *src, in_port_t lport, int cmd, int arg,
512     inp_notify_t notify)
513 {
514         struct inpcb *inp, *marker;
515         struct sockaddr_in6 sa6_src, *sa6_dst;
516         u_int32_t flowinfo;
517
518         if ((unsigned)cmd >= PRC_NCMDS || dst->sa_family != AF_INET6)
519                 return;
520
521         sa6_dst = (struct sockaddr_in6 *)dst;
522         if (IN6_IS_ADDR_UNSPECIFIED(&sa6_dst->sin6_addr))
523                 return;
524
525         /*
526          * note that src can be NULL when we get notify by local fragmentation.
527          */
528         sa6_src = (src == NULL) ? sa6_any : *(const struct sockaddr_in6 *)src;
529         flowinfo = sa6_src.sin6_flowinfo;
530
531         /*
532          * Redirects go to all references to the destination,
533          * and use in6_rtchange to invalidate the route cache.
534          * Dead host indications: also use in6_rtchange to invalidate
535          * the cache, and deliver the error to all the sockets.
536          * Otherwise, if we have knowledge of the local port and address,
537          * deliver only to that socket.
538          */
539         if (PRC_IS_REDIRECT(cmd) || cmd == PRC_HOSTDEAD) {
540                 fport = 0;
541                 lport = 0;
542                 bzero((caddr_t)&sa6_src.sin6_addr, sizeof(sa6_src.sin6_addr));
543
544                 if (cmd != PRC_HOSTDEAD)
545                         notify = in6_rtchange;
546         }
547         if (cmd != PRC_MSGSIZE)
548                 arg = inet6ctlerrmap[cmd];
549
550         marker = in_pcbmarker();
551
552         GET_PCBINFO_TOKEN(pcbinfo);
553
554         LIST_INSERT_HEAD(&pcbinfo->pcblisthead, marker, inp_list);
555         while ((inp = LIST_NEXT(marker, inp_list)) != NULL) {
556                 LIST_REMOVE(marker, inp_list);
557                 LIST_INSERT_AFTER(inp, marker, inp_list);
558
559                 if (inp->inp_flags & INP_PLACEMARKER)
560                         continue;
561
562                 if (!INP_ISIPV6(inp))
563                         continue;
564                 /*
565                  * If the error designates a new path MTU for a destination
566                  * and the application (associated with this socket) wanted to
567                  * know the value, notify. Note that we notify for all
568                  * disconnected sockets if the corresponding application
569                  * wanted. This is because some UDP applications keep sending
570                  * sockets disconnected.
571                  * XXX: should we avoid to notify the value to TCP sockets?
572                  */
573                 if (cmd == PRC_MSGSIZE && (inp->inp_flags & IN6P_MTU) != 0 &&
574                     (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr) ||
575                      IN6_ARE_ADDR_EQUAL(&inp->in6p_faddr, &sa6_dst->sin6_addr))) {
576                         ip6_notify_pmtu(inp, (struct sockaddr_in6 *)dst, &arg);
577                 }
578
579                 /*
580                  * Detect if we should notify the error. If no source and
581                  * destination ports are specifed, but non-zero flowinfo and
582                  * local address match, notify the error. This is the case
583                  * when the error is delivered with an encrypted buffer
584                  * by ESP. Otherwise, just compare addresses and ports
585                  * as usual.
586                  */
587                 if (lport == 0 && fport == 0 && flowinfo &&
588                     inp->inp_socket != NULL &&
589                     flowinfo == (inp->in6p_flowinfo & IPV6_FLOWLABEL_MASK) &&
590                     IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, &sa6_src.sin6_addr))
591                         goto do_notify;
592                 else if (!IN6_ARE_ADDR_EQUAL(&inp->in6p_faddr,
593                                              &sa6_dst->sin6_addr) ||
594                          inp->inp_socket == 0 ||
595                          (lport && inp->inp_lport != lport) ||
596                          (!IN6_IS_ADDR_UNSPECIFIED(&sa6_src.sin6_addr) &&
597                           !IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr,
598                                               &sa6_src.sin6_addr)) ||
599                          (fport && inp->inp_fport != fport))
600                         continue;
601
602 do_notify:
603                 if (notify)
604                         (*notify)(inp, arg);
605         }
606         LIST_REMOVE(marker, inp_list);
607
608         REL_PCBINFO_TOKEN(pcbinfo);
609 }
610
611 /*
612  * Lookup a PCB based on the local address and port.
613  */
614 struct inpcb *
615 in6_pcblookup_local(struct inpcbporthead *porthash,
616     const struct in6_addr *laddr, u_int lport_arg, int wild_okay,
617     struct ucred *cred)
618 {
619         struct inpcb *inp;
620         int matchwild = 3, wildcard;
621         u_short lport = lport_arg;
622         struct inpcbport *phd;
623         struct inpcb *match = NULL;
624
625         /*
626          * If the porthashbase is shared across several cpus, it must
627          * have been locked.
628          */
629         ASSERT_PORTHASH_TOKEN_HELD(porthash);
630
631         /*
632          * Best fit PCB lookup.
633          *
634          * First see if this local port is in use by looking on the
635          * port hash list.
636          */
637         LIST_FOREACH(phd, porthash, phd_hash) {
638                 if (phd->phd_port == lport)
639                         break;
640         }
641
642         if (phd != NULL) {
643                 /*
644                  * Port is in use by one or more PCBs. Look for best
645                  * fit.
646                  */
647                 LIST_FOREACH(inp, &phd->phd_pcblist, inp_portlist) {
648                         wildcard = 0;
649                         if (!INP_ISIPV6(inp))
650                                 continue;
651                         if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr))
652                                 wildcard++;
653                         if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)) {
654                                 if (IN6_IS_ADDR_UNSPECIFIED(laddr))
655                                         wildcard++;
656                                 else if (!IN6_ARE_ADDR_EQUAL(
657                                         &inp->in6p_laddr, laddr))
658                                         continue;
659                         } else {
660                                 if (!IN6_IS_ADDR_UNSPECIFIED(laddr))
661                                         wildcard++;
662                         }
663                         if (wildcard && !wild_okay)
664                                 continue;
665                         if (wildcard < matchwild &&
666                             (cred == NULL ||
667                              cred->cr_prison == 
668                                         inp->inp_socket->so_cred->cr_prison)) {
669                                 match = inp;
670                                 matchwild = wildcard;
671                                 if (wildcard == 0)
672                                         break;
673                                 else
674                                         matchwild = wildcard;
675                         }
676                 }
677         }
678         return (match);
679 }
680
681 void
682 in6_pcbpurgeif0(struct inpcbinfo *pcbinfo, struct ifnet *ifp)
683 {
684         struct in6pcb *in6p, *marker;
685         struct ip6_moptions *im6o;
686         struct in6_multi_mship *imm, *nimm;
687
688         /*
689          * We only need to make sure that we are in netisr0, where all
690          * multicast operation happen.  We could check inpcbinfo which
691          * does not belong to netisr0 by holding the inpcbinfo's token.
692          * In this case, the pcbinfo must be able to be shared, i.e.
693          * pcbinfo->infotoken is not NULL.
694          */
695         ASSERT_NETISR0;
696         KASSERT(pcbinfo->cpu == 0 || pcbinfo->infotoken != NULL,
697             ("pcbinfo could not be shared"));
698
699         /*
700          * Get a marker for the current netisr (netisr0).
701          *
702          * It is possible that the multicast address deletion blocks,
703          * which could cause temporary token releasing.  So we use
704          * inpcb marker here to get a coherent view of the inpcb list.
705          *
706          * While, on the other hand, moptions are only added and deleted
707          * in netisr0, so we would not see staled moption or miss moption
708          * even if the token was released due to the blocking multicast
709          * address deletion.
710          */
711         marker = in_pcbmarker();
712
713         GET_PCBINFO_TOKEN(pcbinfo);
714
715         LIST_INSERT_HEAD(&pcbinfo->pcblisthead, marker, inp_list);
716         while ((in6p = LIST_NEXT(marker, inp_list)) != NULL) {
717                 LIST_REMOVE(marker, inp_list);
718                 LIST_INSERT_AFTER(in6p, marker, inp_list);
719
720                 if (in6p->in6p_flags & INP_PLACEMARKER)
721                         continue;
722                 im6o = in6p->in6p_moptions;
723                 if (INP_ISIPV6(in6p) && im6o) {
724                         /*
725                          * Unselect the outgoing interface if it is being
726                          * detached.
727                          */
728                         if (im6o->im6o_multicast_ifp == ifp)
729                                 im6o->im6o_multicast_ifp = NULL;
730
731                         /*
732                          * Drop multicast group membership if we joined
733                          * through the interface being detached.
734                          * XXX controversial - is it really legal for kernel
735                          * to force this?
736                          */
737                         for (imm = im6o->im6o_memberships.lh_first;
738                              imm != NULL; imm = nimm) {
739                                 nimm = imm->i6mm_chain.le_next;
740                                 if (imm->i6mm_maddr->in6m_ifp == ifp) {
741                                         LIST_REMOVE(imm, i6mm_chain);
742                                         in6_delmulti(imm->i6mm_maddr);
743                                         kfree(imm, M_IPMADDR);
744                                 }
745                         }
746                 }
747         }
748         LIST_REMOVE(marker, inp_list);
749
750         REL_PCBINFO_TOKEN(pcbinfo);
751 }
752
753 /*
754  * Check for alternatives when higher level complains
755  * about service problems.  For now, invalidate cached
756  * routing information.  If the route was created dynamically
757  * (by a redirect), time to try a default gateway again.
758  */
759 void
760 in6_losing(struct inpcb *in6p)
761 {
762         struct rtentry *rt;
763         struct rt_addrinfo info;
764
765         if ((rt = in6p->in6p_route.ro_rt) != NULL) {
766                 bzero((caddr_t)&info, sizeof(info));
767                 info.rti_flags = rt->rt_flags;
768                 info.rti_info[RTAX_DST] = rt_key(rt);
769                 info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
770                 info.rti_info[RTAX_NETMASK] = rt_mask(rt);
771                 rt_missmsg(RTM_LOSING, &info, rt->rt_flags, 0);
772                 if (rt->rt_flags & RTF_DYNAMIC) {
773                         rtrequest(RTM_DELETE, rt_key(rt), rt->rt_gateway,
774                             rt_mask(rt), rt->rt_flags, NULL);
775                 }
776                 in6p->in6p_route.ro_rt = NULL;
777                 rtfree(rt);
778                 /*
779                  * A new route can be allocated
780                  * the next time output is attempted.
781                  */
782         }
783 }
784
785 /*
786  * After a routing change, flush old routing
787  * and allocate a (hopefully) better one.
788  */
789 void
790 in6_rtchange(struct inpcb *inp, int error)
791 {
792         if (inp->in6p_route.ro_rt) {
793                 rtfree(inp->in6p_route.ro_rt);
794                 inp->in6p_route.ro_rt = 0;
795                 /*
796                  * A new route can be allocated the next time
797                  * output is attempted.
798                  */
799         }
800 }
801
802 /*
803  * Lookup PCB in hash list.
804  */
805 struct inpcb *
806 in6_pcblookup_hash(struct inpcbinfo *pcbinfo, struct in6_addr *faddr,
807                    u_int fport_arg, struct in6_addr *laddr, u_int lport_arg,
808                    int wildcard, struct ifnet *ifp)
809 {
810         struct inpcbhead *head;
811         struct inpcb *inp;
812         struct inpcb *jinp = NULL;
813         u_short fport = fport_arg, lport = lport_arg;
814
815         /*
816          * First look for an exact match.
817          */
818         head = &pcbinfo->hashbase[INP_PCBCONNHASH(faddr->s6_addr32[3] /* XXX */,
819                                               fport,
820                                               laddr->s6_addr32[3], /* XXX JH */
821                                               lport,
822                                               pcbinfo->hashmask)];
823         LIST_FOREACH(inp, head, inp_hash) {
824                 if (!INP_ISIPV6(inp))
825                         continue;
826                 if (IN6_ARE_ADDR_EQUAL(&inp->in6p_faddr, faddr) &&
827                     IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, laddr) &&
828                     inp->inp_fport == fport &&
829                     inp->inp_lport == lport) {
830                         /*
831                          * Found.
832                          */
833                         if (inp->inp_socket == NULL ||
834                         inp->inp_socket->so_cred->cr_prison == NULL) {
835                                 return (inp);
836                         } else {
837                                 if  (jinp == NULL)
838                                         jinp = inp;
839                         }
840                 }
841         }
842         if (jinp != NULL)
843                 return(jinp);
844
845         if (wildcard) {
846                 struct inpcontainerhead *chead;
847                 struct inpcontainer *ic;
848                 struct inpcb *local_wild = NULL;
849                 struct inpcb *jinp_wild = NULL;
850                 struct sockaddr_in6 jsin6;
851                 struct ucred *cred;
852
853                 /*
854                  * Order of socket selection:
855                  * 1. non-jailed, non-wild.
856                  * 2. non-jailed, wild.
857                  * 3. jailed, non-wild.
858                  * 4. jailed, wild.
859                  */
860                 jsin6.sin6_family = AF_INET6;
861                 chead = &pcbinfo->wildcardhashbase[INP_PCBWILDCARDHASH(lport,
862                     pcbinfo->wildcardhashmask)];
863
864                 GET_PCBINFO_TOKEN(pcbinfo);
865                 LIST_FOREACH(ic, chead, ic_list) {
866                         inp = ic->ic_inp;
867                         if (inp->inp_flags & INP_PLACEMARKER)
868                                 continue;
869
870                         if (!INP_ISIPV6(inp))
871                                 continue;
872                         if (inp->inp_socket != NULL)
873                                 cred = inp->inp_socket->so_cred;
874                         else
875                                 cred = NULL;
876
877                         if (cred != NULL && jailed(cred)) {
878                                 if (jinp != NULL) {
879                                         continue;
880                                 } else {
881                                         jsin6.sin6_addr = *laddr;
882                                         if (!jailed_ip(cred->cr_prison,
883                                             (struct sockaddr *)&jsin6))
884                                                 continue;
885                                 }
886                         }
887                         if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr) &&
888                             inp->inp_lport == lport) {
889                                 if (IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr,
890                                                        laddr)) {
891                                         if (cred != NULL && jailed(cred)) {
892                                                 jinp = inp;
893                                         } else {
894                                                 REL_PCBINFO_TOKEN(pcbinfo);
895                                                 return (inp);
896                                         }
897                                 } else if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)) {
898                                         if (cred != NULL && jailed(cred))
899                                                 jinp_wild = inp;
900                                         else
901                                                 local_wild = inp;
902                                 }
903                         }
904                 }
905                 REL_PCBINFO_TOKEN(pcbinfo);
906
907                 if (local_wild != NULL)
908                         return (local_wild);
909                 if (jinp != NULL)
910                         return (jinp);
911                 return (jinp_wild);
912         }
913
914         /*
915          * Not found.
916          */
917         return (NULL);
918 }
919
920 void
921 init_sin6(struct sockaddr_in6 *sin6, struct mbuf *m)
922 {
923         struct ip6_hdr *ip;
924
925         ip = mtod(m, struct ip6_hdr *);
926         bzero(sin6, sizeof(*sin6));
927         sin6->sin6_len = sizeof(*sin6);
928         sin6->sin6_family = AF_INET6;
929         sin6->sin6_addr = ip->ip6_src;
930         if (IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr))
931                 sin6->sin6_addr.s6_addr16[1] = 0;
932         sin6->sin6_scope_id =
933                 (m->m_pkthdr.rcvif && IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr))
934                 ? m->m_pkthdr.rcvif->if_index : 0;
935
936         return;
937 }
938
939 void
940 in6_savefaddr(struct socket *so, const struct sockaddr *faddr)
941 {
942         struct sockaddr_in6 *sin6;
943
944         KASSERT(faddr->sa_family == AF_INET6,
945             ("not AF_INET6 faddr %d", faddr->sa_family));
946
947         sin6 = kmalloc(sizeof(*sin6), M_SONAME, M_WAITOK | M_ZERO);
948         sin6->sin6_family = AF_INET6;
949         sin6->sin6_len = sizeof(*sin6);
950
951         sin6->sin6_port = ((const struct sockaddr_in6 *)faddr)->sin6_port;
952         sin6->sin6_addr = ((const struct sockaddr_in6 *)faddr)->sin6_addr;
953
954         if (IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr))
955                 sin6->sin6_scope_id = ntohs(sin6->sin6_addr.s6_addr16[1]);
956         else
957                 sin6->sin6_scope_id = 0;        /*XXX*/
958         if (IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr))
959                 sin6->sin6_addr.s6_addr16[1] = 0;
960
961         so->so_faddr = (struct sockaddr *)sin6;
962 }