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"
69 #include <sys/param.h>
70 #include <sys/systm.h>
71 #include <sys/malloc.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>
87 #include <net/if_types.h>
88 #include <net/route.h>
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>
103 #include <netinet6/ipsec.h>
105 #include <netinet6/ipsec6.h>
107 #include <netinet6/ah.h>
109 #include <netinet6/ah6.h>
111 #include <netkey/key.h>
115 #include <netipsec/ipsec.h>
116 #include <netipsec/ipsec6.h>
117 #include <netipsec/key.h>
118 #endif /* FAST_IPSEC */
120 struct in6_addr zeroin6_addr;
123 in6_pcbbind(inp, nam, cred)
124 register struct inpcb *inp;
125 struct sockaddr *nam;
128 struct socket *so = inp->inp_socket;
129 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)NULL;
130 struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
132 int wild = 0, reuseport = (so->so_options & SO_REUSEPORT);
134 INP_INFO_WLOCK_ASSERT(pcbinfo);
135 INP_LOCK_ASSERT(inp);
137 if (!in6_ifaddr) /* XXX broken! */
138 return (EADDRNOTAVAIL);
139 if (inp->inp_lport || !IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr))
141 if ((so->so_options & (SO_REUSEADDR|SO_REUSEPORT)) == 0)
142 wild = INPLOOKUP_WILDCARD;
146 sin6 = (struct sockaddr_in6 *)nam;
147 if (nam->sa_len != sizeof(*sin6))
152 if (nam->sa_family != AF_INET6)
153 return (EAFNOSUPPORT);
155 if ((error = sa6_embedscope(sin6, ip6_use_defzone)) != 0)
158 lport = sin6->sin6_port;
159 if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) {
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.
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;
172 sin6->sin6_port = 0; /* yech... */
173 if ((ia = ifa_ifwithaddr((struct sockaddr *)sin6)) == 0)
174 return (EADDRNOTAVAIL);
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.
183 ((struct in6_ifaddr *)ia)->ia6_flags &
184 (IN6_IFF_ANYCAST|IN6_IFF_NOTREADY|IN6_IFF_DETACHED)) {
185 return (EADDRNOTAVAIL);
192 if (ntohs(lport) <= ipport_reservedhigh &&
193 ntohs(lport) >= ipport_reservedlow &&
194 priv_check_cred(cred, PRIV_NETINET_RESERVEDPORT,
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,
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))
213 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0 &&
214 IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
215 struct sockaddr_in sin;
217 in6_sin6_2_sin(&sin, sin6);
218 t = in_pcblookup_local(pcbinfo,
223 INP_TIMEWAIT) == 0) &&
224 (so->so_type != SOCK_STREAM ||
225 ntohl(t->inp_faddr.s_addr) ==
227 (so->so_cred->cr_uid !=
228 t->inp_socket->so_cred->cr_uid))
232 t = in6_pcblookup_local(pcbinfo, &sin6->sin6_addr,
234 if (t && (reuseport & ((t->inp_vflag & INP_TIMEWAIT) ?
235 intotw(t)->tw_so_options :
236 t->inp_socket->so_options)) == 0)
238 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0 &&
239 IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
240 struct sockaddr_in sin;
242 in6_sin6_2_sin(&sin, sin6);
243 t = in_pcblookup_local(pcbinfo, sin.sin_addr,
245 if (t && t->inp_vflag & INP_TIMEWAIT) {
247 intotw(t)->tw_so_options) == 0 &&
248 (ntohl(t->inp_laddr.s_addr) !=
249 INADDR_ANY || ((inp->inp_vflag &
251 (t->inp_vflag & INP_IPV6PROTO))))
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)))
262 inp->in6p_laddr = sin6->sin6_addr;
266 if ((e = in6_pcbsetport(&inp->in6p_laddr, inp, cred)) != 0)
270 inp->inp_lport = lport;
271 if (in_pcbinshash(inp) != 0) {
272 inp->in6p_laddr = in6addr_any;
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.
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).
293 in6_pcbladdr(inp, nam, plocal_addr6)
294 register struct inpcb *inp;
295 struct sockaddr *nam;
296 struct in6_addr **plocal_addr6;
298 register struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)nam;
300 struct ifnet *ifp = NULL;
301 int scope_ambiguous = 0;
303 INP_INFO_WLOCK_ASSERT(inp->inp_pcbinfo);
304 INP_LOCK_ASSERT(inp);
306 if (nam->sa_len != sizeof (*sin6))
308 if (sin6->sin6_family != AF_INET6)
309 return (EAFNOSUPPORT);
310 if (sin6->sin6_port == 0)
311 return (EADDRNOTAVAIL);
313 if (sin6->sin6_scope_id == 0 && !ip6_use_defzone)
315 if ((error = sa6_embedscope(sin6, ip6_use_defzone)) != 0)
320 * If the destination address is UNSPECIFIED addr,
321 * use the loopback addr, e.g ::1.
323 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr))
324 sin6->sin6_addr = in6addr_loopback;
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?
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) {
340 if (*plocal_addr6 == 0) {
342 error = EADDRNOTAVAIL;
346 * Don't do pcblookup call here; return interface in
348 * and exit to caller, that will do the lookup.
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,
362 in6_pcbconnect(inp, nam, cred)
363 register struct inpcb *inp;
364 struct sockaddr *nam;
367 struct in6_addr *addr6;
368 register struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)nam;
371 INP_INFO_WLOCK_ASSERT(inp->inp_pcbinfo);
372 INP_LOCK_ASSERT(inp);
375 * Call inner routine, to assign local interface address.
376 * in6_pcbladdr() may automatically fill in sin6_scope_id.
378 if ((error = in6_pcbladdr(inp, nam, &addr6)) != 0)
381 if (in6_pcblookup_hash(inp->inp_pcbinfo, &sin6->sin6_addr,
383 IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)
384 ? addr6 : &inp->in6p_laddr,
385 inp->inp_lport, 0, NULL) != NULL) {
388 if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)) {
389 if (inp->inp_lport == 0) {
390 error = in6_pcbbind(inp, (struct sockaddr *)0, cred);
394 inp->in6p_laddr = *addr6;
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);
406 if (inp->inp_socket->so_type == SOCK_STREAM)
407 ipsec_pcbconn(inp->inp_sp);
413 in6_pcbdisconnect(inp)
417 INP_INFO_WLOCK_ASSERT(inp->inp_pcbinfo);
418 INP_LOCK_ASSERT(inp);
420 bzero((caddr_t)&inp->in6p_faddr, sizeof(inp->in6p_faddr));
422 /* clear flowinfo - draft-itojun-ipv6-flowlabel-api-00 */
423 inp->in6p_flowinfo &= ~IPV6_FLOWLABEL_MASK;
426 ipsec_pcbdisconn(inp->inp_sp);
431 in6_pcbdetach(struct inpcb *inp)
434 KASSERT(inp->inp_socket != NULL, ("in6_pcbdetach: inp_socket == NULL"));
435 inp->inp_socket->so_pcb = NULL;
436 inp->inp_socket = NULL;
440 in6_pcbfree(struct inpcb *inp)
442 struct inpcbinfo *ipi = inp->inp_pcbinfo;
444 KASSERT(inp->inp_socket == NULL, ("in6_pcbfree: inp_socket != NULL"));
445 INP_INFO_WLOCK_ASSERT(inp->inp_pcbinfo);
446 INP_LOCK_ASSERT(inp);
448 #if defined(IPSEC) || defined(FAST_IPSEC)
449 if (inp->in6p_sp != NULL)
450 ipsec6_delete_pcbpolicy(inp);
452 inp->inp_gencnt = ++ipi->ipi_gencnt;
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);
463 uma_zfree(ipi->ipi_zone, inp);
467 in6_sockaddr(port, addr_p)
469 struct in6_addr *addr_p;
471 struct sockaddr_in6 *sin6;
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 */
481 return (struct sockaddr *)sin6;
485 in6_v4mapsin6_sockaddr(port, addr_p)
487 struct in_addr *addr_p;
489 struct sockaddr_in sin;
490 struct sockaddr_in6 *sin6_p;
492 bzero(&sin, sizeof sin);
493 sin.sin_family = AF_INET;
494 sin.sin_len = sizeof(sin);
496 sin.sin_addr = *addr_p;
498 MALLOC(sin6_p, struct sockaddr_in6 *, sizeof *sin6_p, M_SONAME,
500 in6_sin_2_v4mapsin6(&sin, sin6_p);
502 return (struct sockaddr *)sin6_p;
506 in6_getsockaddr(so, nam)
508 struct sockaddr **nam;
510 register struct inpcb *inp;
511 struct in6_addr addr;
515 KASSERT(inp != NULL, ("in6_getsockaddr: inp == NULL"));
518 port = inp->inp_lport;
519 addr = inp->in6p_laddr;
522 *nam = in6_sockaddr(port, &addr);
527 in6_getpeeraddr(so, nam)
529 struct sockaddr **nam;
532 struct in6_addr addr;
536 KASSERT(inp != NULL, ("in6_getpeeraddr: inp == NULL"));
539 port = inp->inp_fport;
540 addr = inp->in6p_faddr;
543 *nam = in6_sockaddr(port, &addr);
548 in6_mapped_sockaddr(struct socket *so, struct sockaddr **nam)
554 KASSERT(inp != NULL, ("in6_mapped_sockaddr: inp == NULL"));
556 if ((inp->inp_vflag & (INP_IPV4 | INP_IPV6)) == INP_IPV4) {
557 error = in_getsockaddr(so, nam);
559 in6_sin_2_v4mapsin6_in_sock(nam);
561 /* scope issues will be handled in in6_getsockaddr(). */
562 error = in6_getsockaddr(so, nam);
569 in6_mapped_peeraddr(struct socket *so, struct sockaddr **nam)
575 KASSERT(inp != NULL, ("in6_mapped_peeraddr: inp == NULL"));
577 if ((inp->inp_vflag & (INP_IPV4 | INP_IPV6)) == INP_IPV4) {
578 error = in_getpeeraddr(so, nam);
580 in6_sin_2_v4mapsin6_in_sock(nam);
582 /* scope issues will be handled in in6_getpeeraddr(). */
583 error = in6_getpeeraddr(so, nam);
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.
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;
605 struct inpcb *(*notify) __P((struct inpcb *, int));
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;
614 if ((unsigned)cmd >= PRC_NCMDS || dst->sa_family != AF_INET6)
617 sa6_dst = (struct sockaddr_in6 *)dst;
618 if (IN6_IS_ADDR_UNSPECIFIED(&sa6_dst->sin6_addr))
622 * note that src can be NULL when we get notify by local fragmentation.
624 sa6_src = (src == NULL) ? sa6_any : *(const struct sockaddr_in6 *)src;
625 flowinfo = sa6_src.sin6_flowinfo;
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.
635 if (PRC_IS_REDIRECT(cmd) || cmd == PRC_HOSTDEAD) {
638 bzero((caddr_t)&sa6_src.sin6_addr, sizeof(sa6_src.sin6_addr));
640 if (cmd != PRC_HOSTDEAD)
641 notify = in6_rtchange;
643 errno = inet6ctlerrmap[cmd];
644 head = pcbinfo->ipi_listhead;
645 INP_INFO_WLOCK(pcbinfo);
646 for (inp = LIST_FIRST(head); inp != NULL; inp = ninp) {
648 ninp = LIST_NEXT(inp, inp_list);
650 if ((inp->inp_vflag & INP_IPV6) == 0) {
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?
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);
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
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))
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)) {
698 if ((*notify)(inp, errno))
703 INP_INFO_WUNLOCK(pcbinfo);
707 * Lookup a PCB based on the local address and port.
710 in6_pcblookup_local(pcbinfo, laddr, lport_arg, wild_okay)
711 struct inpcbinfo *pcbinfo;
712 struct in6_addr *laddr;
716 register struct inpcb *inp;
717 int matchwild = 3, wildcard;
718 u_short lport = lport_arg;
720 INP_INFO_WLOCK_ASSERT(pcbinfo);
723 struct inpcbhead *head;
725 * Look for an unconnected (wildcard foreign addr) PCB that
726 * matches the local address and port we're looking for.
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)
733 if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr) &&
734 IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, laddr) &&
735 inp->inp_lport == lport) {
747 struct inpcbporthead *porthash;
748 struct inpcbport *phd;
749 struct inpcb *match = NULL;
751 * Best fit PCB lookup.
753 * First see if this local port is in use by looking on the
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)
764 * Port is in use by one or more PCBs. Look for best
767 LIST_FOREACH(inp, &phd->phd_pcblist, inp_portlist) {
769 if ((inp->inp_vflag & INP_IPV6) == 0)
771 if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr))
773 if (!IN6_IS_ADDR_UNSPECIFIED(
775 if (IN6_IS_ADDR_UNSPECIFIED(laddr))
777 else if (!IN6_ARE_ADDR_EQUAL(
778 &inp->in6p_laddr, laddr))
781 if (!IN6_IS_ADDR_UNSPECIFIED(laddr))
784 if (wildcard < matchwild) {
786 matchwild = wildcard;
787 if (matchwild == 0) {
798 in6_pcbpurgeif0(pcbinfo, ifp)
799 struct inpcbinfo *pcbinfo;
803 struct ip6_moptions *im6o;
804 struct in6_multi_mship *imm, *nimm;
806 INP_INFO_RLOCK(pcbinfo);
807 LIST_FOREACH(in6p, pcbinfo->ipi_listhead, inp_list) {
809 im6o = in6p->in6p_moptions;
810 if ((in6p->inp_vflag & INP_IPV6) &&
813 * Unselect the outgoing interface if it is being
816 if (im6o->im6o_multicast_ifp == ifp)
817 im6o->im6o_multicast_ifp = NULL;
820 * Drop multicast group membership if we joined
821 * through the interface being detached.
822 * XXX controversial - is it really legal for kernel
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);
837 INP_INFO_RUNLOCK(pcbinfo);
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.
851 * We don't store route pointers in the routing table anymore
857 * After a routing change, flush old routing
858 * and allocate a (hopefully) better one.
861 in6_rtchange(inp, errno)
866 * We don't store route pointers in the routing table anymore
872 * Lookup PCB in hash list.
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;
882 struct inpcbhead *head;
883 register struct inpcb *inp;
884 u_short fport = fport_arg, lport = lport_arg;
887 INP_INFO_RLOCK_ASSERT(pcbinfo);
889 if (faithprefix_p != NULL)
890 faith = (*faithprefix_p)(laddr);
895 * First look for an exact match.
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)
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) {
914 struct inpcb *local_wild = NULL;
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)
921 if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr) &&
922 inp->inp_lport == lport) {
923 if (faith && (inp->inp_flags & INP_FAITH) == 0)
925 if (IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr,
928 else if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr))
942 init_sin6(struct sockaddr_in6 *sin6, struct mbuf *m)
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;
952 (void)sa6_recoverscope(sin6); /* XXX: should catch errors... */