2 /* $KAME: in6_pcb.c,v 1.31 2001/05/21 05:45:10 jinmei Exp $ */
5 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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.
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
35 * Copyright (c) 1982, 1986, 1991, 1993
36 * The Regents of the University of California. All rights reserved.
38 * Redistribution and use in source and binary forms, with or without
39 * modification, are permitted provided that the following conditions
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.
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
62 * @(#)in_pcb.c 8.2 (Berkeley) 1/4/94
66 #include "opt_inet6.h"
67 #include "opt_ipsec.h"
70 #include <sys/param.h>
71 #include <sys/systm.h>
73 #include <sys/malloc.h>
75 #include <sys/domain.h>
76 #include <sys/protosw.h>
77 #include <sys/socket.h>
78 #include <sys/socketvar.h>
79 #include <sys/sockio.h>
80 #include <sys/errno.h>
88 #include <net/if_types.h>
89 #include <net/route.h>
91 #include <netinet/in.h>
92 #include <netinet/in_var.h>
93 #include <netinet/in_systm.h>
94 #include <netinet/tcp_var.h>
95 #include <netinet/ip6.h>
96 #include <netinet/ip_var.h>
97 #include <netinet6/ip6_var.h>
98 #include <netinet6/nd6.h>
99 #include <netinet/in_pcb.h>
100 #include <netinet6/in6_pcb.h>
101 #include <netinet6/scope6_var.h>
104 #include <netinet6/ipsec.h>
106 #include <netinet6/ipsec6.h>
108 #include <netinet6/ah.h>
110 #include <netinet6/ah6.h>
112 #include <netkey/key.h>
116 #include <netipsec/ipsec.h>
117 #include <netipsec/ipsec6.h>
118 #include <netipsec/key.h>
119 #endif /* FAST_IPSEC */
121 struct in6_addr zeroin6_addr;
124 in6_pcbbind(inp, nam, cred)
125 register struct inpcb *inp;
126 struct sockaddr *nam;
129 struct socket *so = inp->inp_socket;
130 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)NULL;
131 struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
133 int wild = 0, reuseport = (so->so_options & SO_REUSEPORT);
135 INP_INFO_WLOCK_ASSERT(pcbinfo);
136 INP_LOCK_ASSERT(inp);
138 if (!in6_ifaddr) /* XXX broken! */
139 return (EADDRNOTAVAIL);
140 if (inp->inp_lport || !IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr))
142 if ((so->so_options & (SO_REUSEADDR|SO_REUSEPORT)) == 0)
147 sin6 = (struct sockaddr_in6 *)nam;
148 if (nam->sa_len != sizeof(*sin6))
153 if (nam->sa_family != AF_INET6)
154 return (EAFNOSUPPORT);
156 if ((error = sa6_embedscope(sin6, ip6_use_defzone)) != 0)
159 lport = sin6->sin6_port;
160 if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) {
162 * Treat SO_REUSEADDR as SO_REUSEPORT for multicast;
163 * allow compepte duplication of binding if
164 * SO_REUSEPORT is set, or if SO_REUSEADDR is set
165 * and a multicast address is bound on both
166 * new and duplicated sockets.
168 if (so->so_options & SO_REUSEADDR)
169 reuseport = SO_REUSEADDR|SO_REUSEPORT;
170 } else if (!IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
171 struct ifaddr *ia = NULL;
173 sin6->sin6_port = 0; /* yech... */
174 if ((ia = ifa_ifwithaddr((struct sockaddr *)sin6)) == 0)
175 return (EADDRNOTAVAIL);
178 * XXX: bind to an anycast address might accidentally
179 * cause sending a packet with anycast source address.
180 * We should allow to bind to a deprecated address, since
181 * the application dares to use it.
184 ((struct in6_ifaddr *)ia)->ia6_flags &
185 (IN6_IFF_ANYCAST|IN6_IFF_NOTREADY|IN6_IFF_DETACHED)) {
186 return (EADDRNOTAVAIL);
193 if (ntohs(lport) <= ipport_reservedhigh &&
194 ntohs(lport) >= ipport_reservedlow &&
195 suser_cred(cred, SUSER_ALLOWJAIL))
197 if (!IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr) &&
198 suser_cred(so->so_cred, SUSER_ALLOWJAIL) != 0) {
199 t = in6_pcblookup_local(pcbinfo,
200 &sin6->sin6_addr, lport,
203 ((t->inp_vflag & INP_TIMEWAIT) == 0) &&
204 (so->so_type != SOCK_STREAM ||
205 IN6_IS_ADDR_UNSPECIFIED(&t->in6p_faddr)) &&
206 (!IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr) ||
207 !IN6_IS_ADDR_UNSPECIFIED(&t->in6p_laddr) ||
208 (t->inp_socket->so_options & SO_REUSEPORT)
209 == 0) && (so->so_cred->cr_uid !=
210 t->inp_socket->so_cred->cr_uid))
212 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0 &&
213 IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
214 struct sockaddr_in sin;
216 in6_sin6_2_sin(&sin, sin6);
217 t = in_pcblookup_local(pcbinfo,
222 INP_TIMEWAIT) == 0) &&
223 (so->so_type != SOCK_STREAM ||
224 ntohl(t->inp_faddr.s_addr) ==
226 (so->so_cred->cr_uid !=
227 t->inp_socket->so_cred->cr_uid))
231 t = in6_pcblookup_local(pcbinfo, &sin6->sin6_addr,
233 if (t && (reuseport & ((t->inp_vflag & INP_TIMEWAIT) ?
234 intotw(t)->tw_so_options :
235 t->inp_socket->so_options)) == 0)
237 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0 &&
238 IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
239 struct sockaddr_in sin;
241 in6_sin6_2_sin(&sin, sin6);
242 t = in_pcblookup_local(pcbinfo, sin.sin_addr,
244 if (t && t->inp_vflag & INP_TIMEWAIT) {
246 intotw(t)->tw_so_options) == 0 &&
247 (ntohl(t->inp_laddr.s_addr) !=
248 INADDR_ANY || ((inp->inp_vflag &
250 (t->inp_vflag & INP_IPV6PROTO))))
254 (reuseport & t->inp_socket->so_options)
255 == 0 && (ntohl(t->inp_laddr.s_addr) !=
256 INADDR_ANY || INP_SOCKAF(so) ==
257 INP_SOCKAF(t->inp_socket)))
261 inp->in6p_laddr = sin6->sin6_addr;
265 if ((e = in6_pcbsetport(&inp->in6p_laddr, inp, cred)) != 0)
269 inp->inp_lport = lport;
270 if (in_pcbinshash(inp) != 0) {
271 inp->in6p_laddr = in6addr_any;
280 * Transform old in6_pcbconnect() into an inner subroutine for new
281 * in6_pcbconnect(): Do some validity-checking on the remote
282 * address (in mbuf 'nam') and then determine local host address
283 * (i.e., which interface) to use to access that remote host.
285 * This preserves definition of in6_pcbconnect(), while supporting a
286 * slightly different version for T/TCP. (This is more than
287 * a bit of a kludge, but cleaning up the internal interfaces would
288 * have forced minor changes in every protocol).
292 in6_pcbladdr(inp, nam, plocal_addr6)
293 register struct inpcb *inp;
294 struct sockaddr *nam;
295 struct in6_addr **plocal_addr6;
297 register struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)nam;
299 struct ifnet *ifp = NULL;
300 int scope_ambiguous = 0;
302 if (nam->sa_len != sizeof (*sin6))
304 if (sin6->sin6_family != AF_INET6)
305 return (EAFNOSUPPORT);
306 if (sin6->sin6_port == 0)
307 return (EADDRNOTAVAIL);
309 if (sin6->sin6_scope_id == 0 && !ip6_use_defzone)
311 if ((error = sa6_embedscope(sin6, ip6_use_defzone)) != 0)
314 INP_INFO_WLOCK_ASSERT(inp->inp_pcbinfo);
315 INP_LOCK_ASSERT(inp);
319 * If the destination address is UNSPECIFIED addr,
320 * use the loopback addr, e.g ::1.
322 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr))
323 sin6->sin6_addr = in6addr_loopback;
327 * XXX: in6_selectsrc might replace the bound local address
328 * with the address specified by setsockopt(IPV6_PKTINFO).
329 * Is it the intended behavior?
331 *plocal_addr6 = in6_selectsrc(sin6, inp->in6p_outputopts,
332 inp->in6p_moptions, NULL,
333 &inp->in6p_laddr, &ifp, &error);
334 if (ifp && scope_ambiguous &&
335 (error = in6_setscope(&sin6->sin6_addr, ifp, NULL)) != 0) {
339 if (*plocal_addr6 == 0) {
341 error = EADDRNOTAVAIL;
345 * Don't do pcblookup call here; return interface in
347 * and exit to caller, that will do the lookup.
355 * Connect from a socket to a specified address.
356 * Both address and port must be specified in argument sin.
357 * If don't have a local address for this socket yet,
361 in6_pcbconnect(inp, nam, cred)
362 register struct inpcb *inp;
363 struct sockaddr *nam;
366 struct in6_addr *addr6;
367 register struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)nam;
370 INP_INFO_WLOCK_ASSERT(inp->inp_pcbinfo);
371 INP_LOCK_ASSERT(inp);
374 * Call inner routine, to assign local interface address.
375 * in6_pcbladdr() may automatically fill in sin6_scope_id.
377 if ((error = in6_pcbladdr(inp, nam, &addr6)) != 0)
380 if (in6_pcblookup_hash(inp->inp_pcbinfo, &sin6->sin6_addr,
382 IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)
383 ? addr6 : &inp->in6p_laddr,
384 inp->inp_lport, 0, NULL) != NULL) {
387 if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)) {
388 if (inp->inp_lport == 0) {
389 error = in6_pcbbind(inp, (struct sockaddr *)0, cred);
393 inp->in6p_laddr = *addr6;
395 inp->in6p_faddr = sin6->sin6_addr;
396 inp->inp_fport = sin6->sin6_port;
397 /* update flowinfo - draft-itojun-ipv6-flowlabel-api-00 */
398 inp->in6p_flowinfo &= ~IPV6_FLOWLABEL_MASK;
399 if (inp->in6p_flags & IN6P_AUTOFLOWLABEL)
400 inp->in6p_flowinfo |=
401 (htonl(ip6_randomflowlabel()) & IPV6_FLOWLABEL_MASK);
405 if (inp->inp_socket->so_type == SOCK_STREAM)
406 ipsec_pcbconn(inp->inp_sp);
412 in6_pcbdisconnect(inp)
416 INP_INFO_WLOCK_ASSERT(inp->inp_pcbinfo);
417 INP_LOCK_ASSERT(inp);
419 bzero((caddr_t)&inp->in6p_faddr, sizeof(inp->in6p_faddr));
421 /* clear flowinfo - draft-itojun-ipv6-flowlabel-api-00 */
422 inp->in6p_flowinfo &= ~IPV6_FLOWLABEL_MASK;
425 ipsec_pcbdisconn(inp->inp_sp);
427 if (inp->inp_socket->so_state & SS_NOFDREF)
435 struct socket *so = inp->inp_socket;
436 struct inpcbinfo *ipi = inp->inp_pcbinfo;
438 INP_INFO_WLOCK_ASSERT(inp->inp_pcbinfo);
439 INP_LOCK_ASSERT(inp);
441 #if defined(IPSEC) || defined(FAST_IPSEC)
442 if (inp->in6p_sp != NULL)
443 ipsec6_delete_pcbpolicy(inp);
445 inp->inp_gencnt = ++ipi->ipi_gencnt;
455 ip6_freepcbopts(inp->in6p_outputopts);
456 ip6_freemoptions(inp->in6p_moptions);
457 /* Check and free IPv4 related resources in case of mapped addr */
458 if (inp->inp_options)
459 (void)m_free(inp->inp_options);
460 ip_freemoptions(inp->inp_moptions);
463 mac_destroy_inpcb(inp);
466 uma_zfree(ipi->ipi_zone, inp);
470 in6_sockaddr(port, addr_p)
472 struct in6_addr *addr_p;
474 struct sockaddr_in6 *sin6;
476 MALLOC(sin6, struct sockaddr_in6 *, sizeof *sin6, M_SONAME, M_WAITOK);
477 bzero(sin6, sizeof *sin6);
478 sin6->sin6_family = AF_INET6;
479 sin6->sin6_len = sizeof(*sin6);
480 sin6->sin6_port = port;
481 sin6->sin6_addr = *addr_p;
482 (void)sa6_recoverscope(sin6); /* XXX: should catch errors */
484 return (struct sockaddr *)sin6;
488 in6_v4mapsin6_sockaddr(port, addr_p)
490 struct in_addr *addr_p;
492 struct sockaddr_in sin;
493 struct sockaddr_in6 *sin6_p;
495 bzero(&sin, sizeof sin);
496 sin.sin_family = AF_INET;
497 sin.sin_len = sizeof(sin);
499 sin.sin_addr = *addr_p;
501 MALLOC(sin6_p, struct sockaddr_in6 *, sizeof *sin6_p, M_SONAME,
503 in6_sin_2_v4mapsin6(&sin, sin6_p);
505 return (struct sockaddr *)sin6_p;
509 * The calling convention of in6_setsockaddr() and in6_setpeeraddr() was
510 * modified to match the pru_sockaddr() and pru_peeraddr() entry points
511 * in struct pr_usrreqs, so that protocols can just reference then directly
512 * without the need for a wrapper function. The socket must have a valid
513 * (i.e., non-nil) PCB, but it should be impossible to get an invalid one
514 * except through a kernel programming error, so it is acceptable to panic
515 * (or in this case trap) if the PCB is invalid. (Actually, we don't trap
516 * because there actually /is/ a programming error somewhere... XXX)
519 in6_setsockaddr(so, nam)
521 struct sockaddr **nam;
524 register struct inpcb *inp;
525 struct in6_addr addr;
534 port = inp->inp_lport;
535 addr = inp->in6p_laddr;
538 *nam = in6_sockaddr(port, &addr);
543 in6_setpeeraddr(so, nam)
545 struct sockaddr **nam;
549 struct in6_addr addr;
558 port = inp->inp_fport;
559 addr = inp->in6p_faddr;
562 *nam = in6_sockaddr(port, &addr);
567 in6_mapped_sockaddr(struct socket *so, struct sockaddr **nam)
569 struct inpcb *inp = sotoinpcb(so);
574 if ((inp->inp_vflag & (INP_IPV4 | INP_IPV6)) == INP_IPV4) {
575 error = in_setsockaddr(so, nam, &tcbinfo);
577 in6_sin_2_v4mapsin6_in_sock(nam);
579 /* scope issues will be handled in in6_setsockaddr(). */
580 error = in6_setsockaddr(so, nam);
587 in6_mapped_peeraddr(struct socket *so, struct sockaddr **nam)
589 struct inpcb *inp = sotoinpcb(so);
594 if ((inp->inp_vflag & (INP_IPV4 | INP_IPV6)) == INP_IPV4) {
595 error = in_setpeeraddr(so, nam, &tcbinfo);
597 in6_sin_2_v4mapsin6_in_sock(nam);
599 /* scope issues will be handled in in6_setpeeraddr(). */
600 error = in6_setpeeraddr(so, nam);
606 * Pass some notification to all connections of a protocol
607 * associated with address dst. The local address and/or port numbers
608 * may be specified to limit the search. The "usual action" will be
609 * taken, depending on the ctlinput cmd. The caller must filter any
610 * cmds that are uninteresting (e.g., no error in the map).
611 * Call the protocol specific routine (if any) to report
612 * any errors for each matching socket.
614 * Must be called at splnet.
617 in6_pcbnotify(pcbinfo, dst, fport_arg, src, lport_arg, cmd, cmdarg, notify)
618 struct inpcbinfo *pcbinfo;
619 struct sockaddr *dst;
620 const struct sockaddr *src;
621 u_int fport_arg, lport_arg;
624 struct inpcb *(*notify) __P((struct inpcb *, int));
626 struct inpcbhead *head;
627 struct inpcb *inp, *ninp;
628 struct sockaddr_in6 sa6_src, *sa6_dst;
629 u_short fport = fport_arg, lport = lport_arg;
633 if ((unsigned)cmd >= PRC_NCMDS || dst->sa_family != AF_INET6)
636 sa6_dst = (struct sockaddr_in6 *)dst;
637 if (IN6_IS_ADDR_UNSPECIFIED(&sa6_dst->sin6_addr))
641 * note that src can be NULL when we get notify by local fragmentation.
643 sa6_src = (src == NULL) ? sa6_any : *(const struct sockaddr_in6 *)src;
644 flowinfo = sa6_src.sin6_flowinfo;
647 * Redirects go to all references to the destination,
648 * and use in6_rtchange to invalidate the route cache.
649 * Dead host indications: also use in6_rtchange to invalidate
650 * the cache, and deliver the error to all the sockets.
651 * Otherwise, if we have knowledge of the local port and address,
652 * deliver only to that socket.
654 if (PRC_IS_REDIRECT(cmd) || cmd == PRC_HOSTDEAD) {
657 bzero((caddr_t)&sa6_src.sin6_addr, sizeof(sa6_src.sin6_addr));
659 if (cmd != PRC_HOSTDEAD)
660 notify = in6_rtchange;
662 errno = inet6ctlerrmap[cmd];
664 head = pcbinfo->listhead;
665 INP_INFO_WLOCK(pcbinfo);
666 for (inp = LIST_FIRST(head); inp != NULL; inp = ninp) {
668 ninp = LIST_NEXT(inp, inp_list);
670 if ((inp->inp_vflag & INP_IPV6) == 0) {
676 * If the error designates a new path MTU for a destination
677 * and the application (associated with this socket) wanted to
678 * know the value, notify. Note that we notify for all
679 * disconnected sockets if the corresponding application
680 * wanted. This is because some UDP applications keep sending
681 * sockets disconnected.
682 * XXX: should we avoid to notify the value to TCP sockets?
684 if (cmd == PRC_MSGSIZE && (inp->inp_flags & IN6P_MTU) != 0 &&
685 (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr) ||
686 IN6_ARE_ADDR_EQUAL(&inp->in6p_faddr, &sa6_dst->sin6_addr))) {
687 ip6_notify_pmtu(inp, (struct sockaddr_in6 *)dst,
688 (u_int32_t *)cmdarg);
692 * Detect if we should notify the error. If no source and
693 * destination ports are specifed, but non-zero flowinfo and
694 * local address match, notify the error. This is the case
695 * when the error is delivered with an encrypted buffer
696 * by ESP. Otherwise, just compare addresses and ports
699 if (lport == 0 && fport == 0 && flowinfo &&
700 inp->inp_socket != NULL &&
701 flowinfo == (inp->in6p_flowinfo & IPV6_FLOWLABEL_MASK) &&
702 IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, &sa6_src.sin6_addr))
704 else if (!IN6_ARE_ADDR_EQUAL(&inp->in6p_faddr,
705 &sa6_dst->sin6_addr) ||
706 inp->inp_socket == 0 ||
707 (lport && inp->inp_lport != lport) ||
708 (!IN6_IS_ADDR_UNSPECIFIED(&sa6_src.sin6_addr) &&
709 !IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr,
710 &sa6_src.sin6_addr)) ||
711 (fport && inp->inp_fport != fport)) {
718 if ((*notify)(inp, errno))
723 INP_INFO_WUNLOCK(pcbinfo);
728 * Lookup a PCB based on the local address and port.
731 in6_pcblookup_local(pcbinfo, laddr, lport_arg, wild_okay)
732 struct inpcbinfo *pcbinfo;
733 struct in6_addr *laddr;
737 register struct inpcb *inp;
738 int matchwild = 3, wildcard;
739 u_short lport = lport_arg;
742 struct inpcbhead *head;
744 * Look for an unconnected (wildcard foreign addr) PCB that
745 * matches the local address and port we're looking for.
747 head = &pcbinfo->hashbase[INP_PCBHASH(INADDR_ANY, lport, 0,
749 LIST_FOREACH(inp, head, inp_hash) {
750 if ((inp->inp_vflag & INP_IPV6) == 0)
752 if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr) &&
753 IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, laddr) &&
754 inp->inp_lport == lport) {
766 struct inpcbporthead *porthash;
767 struct inpcbport *phd;
768 struct inpcb *match = NULL;
770 * Best fit PCB lookup.
772 * First see if this local port is in use by looking on the
775 porthash = &pcbinfo->porthashbase[INP_PCBPORTHASH(lport,
776 pcbinfo->porthashmask)];
777 LIST_FOREACH(phd, porthash, phd_hash) {
778 if (phd->phd_port == lport)
783 * Port is in use by one or more PCBs. Look for best
786 LIST_FOREACH(inp, &phd->phd_pcblist, inp_portlist) {
788 if ((inp->inp_vflag & INP_IPV6) == 0)
790 if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr))
792 if (!IN6_IS_ADDR_UNSPECIFIED(
794 if (IN6_IS_ADDR_UNSPECIFIED(laddr))
796 else if (!IN6_ARE_ADDR_EQUAL(
797 &inp->in6p_laddr, laddr))
800 if (!IN6_IS_ADDR_UNSPECIFIED(laddr))
803 if (wildcard < matchwild) {
805 matchwild = wildcard;
806 if (matchwild == 0) {
817 in6_pcbpurgeif0(head, ifp)
822 struct ip6_moptions *im6o;
823 struct in6_multi_mship *imm, *nimm;
825 for (in6p = head; in6p != NULL; in6p = LIST_NEXT(in6p, inp_list)) {
826 im6o = in6p->in6p_moptions;
827 if ((in6p->inp_vflag & INP_IPV6) &&
830 * Unselect the outgoing interface if it is being
833 if (im6o->im6o_multicast_ifp == ifp)
834 im6o->im6o_multicast_ifp = NULL;
837 * Drop multicast group membership if we joined
838 * through the interface being detached.
839 * XXX controversial - is it really legal for kernel
842 for (imm = im6o->im6o_memberships.lh_first;
843 imm != NULL; imm = nimm) {
844 nimm = imm->i6mm_chain.le_next;
845 if (imm->i6mm_maddr->in6m_ifp == ifp) {
846 LIST_REMOVE(imm, i6mm_chain);
847 in6_delmulti(imm->i6mm_maddr);
848 free(imm, M_IP6MADDR);
856 * Check for alternatives when higher level complains
857 * about service problems. For now, invalidate cached
858 * routing information. If the route was created dynamically
859 * (by a redirect), time to try a default gateway again.
866 * We don't store route pointers in the routing table anymore
872 * After a routing change, flush old routing
873 * and allocate a (hopefully) better one.
876 in6_rtchange(inp, errno)
881 * We don't store route pointers in the routing table anymore
887 * Lookup PCB in hash list.
890 in6_pcblookup_hash(pcbinfo, faddr, fport_arg, laddr, lport_arg, wildcard, ifp)
891 struct inpcbinfo *pcbinfo;
892 struct in6_addr *faddr, *laddr;
893 u_int fport_arg, lport_arg;
897 struct inpcbhead *head;
898 register struct inpcb *inp;
899 u_short fport = fport_arg, lport = lport_arg;
902 if (faithprefix_p != NULL)
903 faith = (*faithprefix_p)(laddr);
908 * First look for an exact match.
910 head = &pcbinfo->hashbase[INP_PCBHASH(faddr->s6_addr32[3] /* XXX */,
913 LIST_FOREACH(inp, head, inp_hash) {
914 if ((inp->inp_vflag & INP_IPV6) == 0)
916 if (IN6_ARE_ADDR_EQUAL(&inp->in6p_faddr, faddr) &&
917 IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, laddr) &&
918 inp->inp_fport == fport &&
919 inp->inp_lport == lport) {
927 struct inpcb *local_wild = NULL;
929 head = &pcbinfo->hashbase[INP_PCBHASH(INADDR_ANY, lport, 0,
931 LIST_FOREACH(inp, head, inp_hash) {
932 if ((inp->inp_vflag & INP_IPV6) == 0)
934 if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr) &&
935 inp->inp_lport == lport) {
936 if (faith && (inp->inp_flags & INP_FAITH) == 0)
938 if (IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr,
941 else if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr))
955 init_sin6(struct sockaddr_in6 *sin6, struct mbuf *m)
959 ip = mtod(m, struct ip6_hdr *);
960 bzero(sin6, sizeof(*sin6));
961 sin6->sin6_len = sizeof(*sin6);
962 sin6->sin6_family = AF_INET6;
963 sin6->sin6_addr = ip->ip6_src;
965 (void)sa6_recoverscope(sin6); /* XXX: should catch errors... */