Hi,

Can we rename the the function in_pcbhashlookup() to in_pcblookup()?
Then we have in_pcblookup() and in_pcblookup_listen() as public PCB
interface.  Using a hash table is only an implementation detail.

For internal use I would like to introduce in_pcbhash_insert() and
in_pcbhash_lookup() to avoid code duplication.

Routing domain is unsigned, change the type to u_int.

If the diff is too large for review, I can split these parts.

ok?

bluhm

Index: net/pf.c
===================================================================
RCS file: /data/mirror/openbsd/cvs/src/sys/net/pf.c,v
retrieving revision 1.1137
diff -u -p -r1.1137 pf.c
--- net/pf.c    8 Aug 2022 12:06:30 -0000       1.1137
+++ net/pf.c    19 Aug 2022 16:22:47 -0000
@@ -3348,7 +3348,7 @@ pf_socket_lookup(struct pf_pdesc *pd)
                 * Fails when rtable is changed while evaluating the ruleset
                 * The socket looked up will not match the one hit in the end.
                 */
-               inp = in_pcbhashlookup(tb, saddr->v4, sport, daddr->v4, dport,
+               inp = in_pcblookup(tb, saddr->v4, sport, daddr->v4, dport,
                    pd->rdomain);
                if (inp == NULL) {
                        inp = in_pcblookup_listen(tb, daddr->v4, dport,
@@ -3359,7 +3359,7 @@ pf_socket_lookup(struct pf_pdesc *pd)
                break;
 #ifdef INET6
        case AF_INET6:
-               inp = in6_pcbhashlookup(tb, &saddr->v6, sport, &daddr->v6,
+               inp = in6_pcblookup(tb, &saddr->v6, sport, &daddr->v6,
                    dport, pd->rdomain);
                if (inp == NULL) {
                        inp = in6_pcblookup_listen(tb, &daddr->v6, dport,
Index: netinet/in_pcb.c
===================================================================
RCS file: /data/mirror/openbsd/cvs/src/sys/netinet/in_pcb.c,v
retrieving revision 1.270
diff -u -p -r1.270 in_pcb.c
--- netinet/in_pcb.c    8 Aug 2022 12:06:30 -0000       1.270
+++ netinet/in_pcb.c    19 Aug 2022 20:41:23 -0000
@@ -120,14 +120,16 @@ struct baddynamicports baddynamicports;
 struct baddynamicports rootonlyports;
 struct pool inpcb_pool;
 
-void   in_pcbrehash_locked(struct inpcb *);
+void   in_pcbhash_insert(struct inpcb *);
+struct inpcb *in_pcbhash_lookup(struct inpcbtable *, u_int,
+    const struct in_addr *, u_short, const struct in_addr *, u_short);
 int    in_pcbresize(struct inpcbtable *, int);
 
 #define        INPCBHASH_LOADFACTOR(_x)        (((_x) * 3) / 4)
 
-struct inpcbhead *in_pcbhash(struct inpcbtable *, int,
+struct inpcbhead *in_pcbhash(struct inpcbtable *, u_int,
     const struct in_addr *, u_short, const struct in_addr *, u_short);
-struct inpcbhead *in_pcblhash(struct inpcbtable *, int, u_short);
+struct inpcbhead *in_pcblhash(struct inpcbtable *, u_int, u_short);
 
 /*
  * in_pcb is used for inet and inet6.  in6_pcb only contains special
@@ -141,12 +143,12 @@ in_init(void)
 }
 
 struct inpcbhead *
-in_pcbhash(struct inpcbtable *table, int rdom,
+in_pcbhash(struct inpcbtable *table, u_int rdomain,
     const struct in_addr *faddr, u_short fport,
     const struct in_addr *laddr, u_short lport)
 {
        SIPHASH_CTX ctx;
-       u_int32_t nrdom = htonl(rdom);
+       u_int32_t nrdom = htonl(rdomain);
 
        SipHash24_Init(&ctx, &table->inpt_key);
        SipHash24_Update(&ctx, &nrdom, sizeof(nrdom));
@@ -159,10 +161,10 @@ in_pcbhash(struct inpcbtable *table, int
 }
 
 struct inpcbhead *
-in_pcblhash(struct inpcbtable *table, int rdom, u_short lport)
+in_pcblhash(struct inpcbtable *table, u_int rdomain, u_short lport)
 {
        SIPHASH_CTX ctx;
-       u_int32_t nrdom = htonl(rdom);
+       u_int32_t nrdom = htonl(rdomain);
 
        SipHash24_Init(&ctx, &table->inpt_lkey);
        SipHash24_Update(&ctx, &nrdom, sizeof(nrdom));
@@ -226,9 +228,6 @@ int
 in_pcballoc(struct socket *so, struct inpcbtable *table)
 {
        struct inpcb *inp;
-       struct inpcbhead *head;
-
-       NET_ASSERT_LOCKED();
 
        inp = pool_get(&inpcb_pool, PR_NOWAIT|PR_ZERO);
        if (inp == NULL)
@@ -257,19 +256,7 @@ in_pcballoc(struct socket *so, struct in
        if (table->inpt_count++ > INPCBHASH_LOADFACTOR(table->inpt_size))
                (void)in_pcbresize(table, table->inpt_size * 2);
        TAILQ_INSERT_HEAD(&table->inpt_queue, inp, inp_queue);
-       head = in_pcblhash(table, inp->inp_rtableid, inp->inp_lport);
-       LIST_INSERT_HEAD(head, inp, inp_lhash);
-#ifdef INET6
-       if (sotopf(so) == PF_INET6)
-               head = in6_pcbhash(table, rtable_l2(inp->inp_rtableid),
-                   &inp->inp_faddr6, inp->inp_fport,
-                   &inp->inp_laddr6, inp->inp_lport);
-       else
-#endif /* INET6 */
-               head = in_pcbhash(table, rtable_l2(inp->inp_rtableid),
-                   &inp->inp_faddr, inp->inp_fport,
-                   &inp->inp_laddr, inp->inp_lport);
-       LIST_INSERT_HEAD(head, inp, inp_hash);
+       in_pcbhash_insert(inp);
        mtx_leave(&table->inpt_mtx);
 
        so->so_pcb = inp;
@@ -511,7 +498,7 @@ in_pcbconnect(struct inpcb *inp, struct 
        if (error)
                return (error);
 
-       t = in_pcbhashlookup(inp->inp_table, sin->sin_addr, sin->sin_port,
+       t = in_pcblookup(inp->inp_table, sin->sin_addr, sin->sin_port,
            ina, inp->inp_lport, inp->inp_rtableid);
        if (t != NULL) {
                in_pcbunref(t);
@@ -525,7 +512,7 @@ in_pcbconnect(struct inpcb *inp, struct 
                        error = in_pcbbind(inp, NULL, curproc);
                        if (error)
                                return (error);
-                       t = in_pcbhashlookup(inp->inp_table, sin->sin_addr,
+                       t = in_pcblookup(inp->inp_table, sin->sin_addr,
                            sin->sin_port, ina, inp->inp_lport,
                            inp->inp_rtableid);
                        if (t != NULL) {
@@ -580,8 +567,6 @@ in_pcbdetach(struct inpcb *inp)
        struct socket *so = inp->inp_socket;
        struct inpcbtable *table = inp->inp_table;
 
-       NET_ASSERT_LOCKED();
-
        so->so_pcb = NULL;
        /*
         * As long as the NET_LOCK() is the default lock for Internet
@@ -1026,12 +1011,14 @@ in_pcbrehash(struct inpcb *inp)
        struct inpcbtable *table = inp->inp_table;
 
        mtx_enter(&table->inpt_mtx);
-       in_pcbrehash_locked(inp);
+       LIST_REMOVE(inp, inp_lhash);
+       LIST_REMOVE(inp, inp_hash);
+       in_pcbhash_insert(inp);
        mtx_leave(&table->inpt_mtx);
 }
 
 void
-in_pcbrehash_locked(struct inpcb *inp)
+in_pcbhash_insert(struct inpcb *inp)
 {
        struct inpcbtable *table = inp->inp_table;
        struct inpcbhead *head;
@@ -1039,10 +1026,8 @@ in_pcbrehash_locked(struct inpcb *inp)
        NET_ASSERT_LOCKED();
        MUTEX_ASSERT_LOCKED(&table->inpt_mtx);
 
-       LIST_REMOVE(inp, inp_lhash);
        head = in_pcblhash(table, inp->inp_rtableid, inp->inp_lport);
        LIST_INSERT_HEAD(head, inp, inp_lhash);
-       LIST_REMOVE(inp, inp_hash);
 #ifdef INET6
        if (inp->inp_flags & INP_IPV6)
                head = in6_pcbhash(table, rtable_l2(inp->inp_rtableid),
@@ -1056,6 +1041,44 @@ in_pcbrehash_locked(struct inpcb *inp)
        LIST_INSERT_HEAD(head, inp, inp_hash);
 }
 
+struct inpcb *
+in_pcbhash_lookup(struct inpcbtable *table, u_int rdomain,
+    const struct in_addr *faddr, u_short fport,
+    const struct in_addr *laddr, u_short lport)
+{
+       struct inpcbhead *head;
+       struct inpcb *inp;
+
+       NET_ASSERT_LOCKED();
+       MUTEX_ASSERT_LOCKED(&table->inpt_mtx);
+
+       head = in_pcbhash(table, rdomain, faddr, fport, laddr, lport);
+       LIST_FOREACH(inp, head, inp_hash) {
+#ifdef INET6
+               if (ISSET(inp->inp_flags, INP_IPV6))
+                       continue;
+#endif
+               if (inp->inp_fport == fport && inp->inp_lport == lport &&
+                   inp->inp_faddr.s_addr == faddr->s_addr &&
+                   inp->inp_laddr.s_addr == laddr->s_addr &&
+                   rtable_l2(inp->inp_rtableid) == rdomain) {
+                       break;
+               }
+       }
+       if (inp != NULL) {
+               /*
+                * Move this PCB to the head of hash chain so that
+                * repeated accesses are quicker.  This is analogous to
+                * the historic single-entry PCB cache.
+                */
+               if (inp != LIST_FIRST(head)) {
+                       LIST_REMOVE(inp, inp_hash);
+                       LIST_INSERT_HEAD(head, inp, inp_hash);
+               }
+       }
+       return (inp);
+}
+
 int
 in_pcbresize(struct inpcbtable *table, int hashsize)
 {
@@ -1087,7 +1110,9 @@ in_pcbresize(struct inpcbtable *table, i
        arc4random_buf(&table->inpt_lkey, sizeof(table->inpt_lkey));
 
        TAILQ_FOREACH(inp, &table->inpt_queue, inp_queue) {
-               in_pcbrehash_locked(inp);
+               LIST_REMOVE(inp, inp_lhash);
+               LIST_REMOVE(inp, inp_hash);
+               in_pcbhash_insert(inp);
        }
        hashfree(ohashtbl, osize, M_PCB);
        hashfree(olhashtbl, osize, M_PCB);
@@ -1100,7 +1125,7 @@ int       in_pcbnotifymiss = 0;
 #endif
 
 /*
- * The in(6)_pcbhashlookup functions are used to locate connected sockets
+ * The in(6)_pcblookup functions are used to locate connected sockets
  * quickly:
  *     faddr.fport <-> laddr.lport
  * No wildcard matching is done so that listening sockets are not found.
@@ -1109,38 +1134,15 @@ int     in_pcbnotifymiss = 0;
  * After those two lookups no other are necessary.
  */
 struct inpcb *
-in_pcbhashlookup(struct inpcbtable *table, struct in_addr faddr,
-    u_int fport_arg, struct in_addr laddr, u_int lport_arg, u_int rtable)
+in_pcblookup(struct inpcbtable *table, struct in_addr faddr,
+    u_int fport, struct in_addr laddr, u_int lport, u_int rtable)
 {
-       struct inpcbhead *head;
        struct inpcb *inp;
-       u_int16_t fport = fport_arg, lport = lport_arg;
        u_int rdomain;
 
        rdomain = rtable_l2(rtable);
        mtx_enter(&table->inpt_mtx);
-       head = in_pcbhash(table, rdomain, &faddr, fport, &laddr, lport);
-       LIST_FOREACH(inp, head, inp_hash) {
-#ifdef INET6
-               if (inp->inp_flags & INP_IPV6)
-                       continue;       /*XXX*/
-#endif
-               if (inp->inp_faddr.s_addr == faddr.s_addr &&
-                   inp->inp_fport == fport && inp->inp_lport == lport &&
-                   inp->inp_laddr.s_addr == laddr.s_addr &&
-                   rtable_l2(inp->inp_rtableid) == rdomain) {
-                       /*
-                        * Move this PCB to the head of hash chain so that
-                        * repeated accesses are quicker.  This is analogous to
-                        * the historic single-entry PCB cache.
-                        */
-                       if (inp != LIST_FIRST(head)) {
-                               LIST_REMOVE(inp, inp_hash);
-                               LIST_INSERT_HEAD(head, inp, inp_hash);
-                       }
-                       break;
-               }
-       }
+       inp = in_pcbhash_lookup(table, rdomain, &faddr, fport, &laddr, lport);
        in_pcbref(inp);
        mtx_leave(&table->inpt_mtx);
 #ifdef DIAGNOSTIC
@@ -1164,7 +1166,6 @@ struct inpcb *
 in_pcblookup_listen(struct inpcbtable *table, struct in_addr laddr,
     u_int lport_arg, struct mbuf *m, u_int rtable)
 {
-       struct inpcbhead *head;
        const struct in_addr *key1, *key2;
        struct inpcb *inp;
        u_int16_t lport = lport_arg;
@@ -1205,41 +1206,10 @@ in_pcblookup_listen(struct inpcbtable *t
 
        rdomain = rtable_l2(rtable);
        mtx_enter(&table->inpt_mtx);
-       head = in_pcbhash(table, rdomain, &zeroin_addr, 0, key1, lport);
-       LIST_FOREACH(inp, head, inp_hash) {
-#ifdef INET6
-               if (inp->inp_flags & INP_IPV6)
-                       continue;       /*XXX*/
-#endif
-               if (inp->inp_lport == lport && inp->inp_fport == 0 &&
-                   inp->inp_laddr.s_addr == key1->s_addr &&
-                   inp->inp_faddr.s_addr == INADDR_ANY &&
-                   rtable_l2(inp->inp_rtableid) == rdomain)
-                       break;
-       }
+       inp = in_pcbhash_lookup(table, rdomain, &zeroin_addr, 0, key1, lport);
        if (inp == NULL && key1->s_addr != key2->s_addr) {
-               head = in_pcbhash(table, rdomain,
+               inp = in_pcbhash_lookup(table, rdomain,
                    &zeroin_addr, 0, key2, lport);
-               LIST_FOREACH(inp, head, inp_hash) {
-#ifdef INET6
-                       if (inp->inp_flags & INP_IPV6)
-                               continue;       /*XXX*/
-#endif
-                       if (inp->inp_lport == lport && inp->inp_fport == 0 &&
-                           inp->inp_laddr.s_addr == key2->s_addr &&
-                           inp->inp_faddr.s_addr == INADDR_ANY &&
-                           rtable_l2(inp->inp_rtableid) == rdomain)
-                               break;
-               }
-       }
-       /*
-        * Move this PCB to the head of hash chain so that
-        * repeated accesses are quicker.  This is analogous to
-        * the historic single-entry PCB cache.
-        */
-       if (inp != NULL && inp != LIST_FIRST(head)) {
-               LIST_REMOVE(inp, inp_hash);
-               LIST_INSERT_HEAD(head, inp, inp_hash);
        }
        in_pcbref(inp);
        mtx_leave(&table->inpt_mtx);
Index: netinet/in_pcb.h
===================================================================
RCS file: /data/mirror/openbsd/cvs/src/sys/netinet/in_pcb.h,v
retrieving revision 1.129
diff -u -p -r1.129 in_pcb.h
--- netinet/in_pcb.h    15 May 2022 09:12:20 -0000      1.129
+++ netinet/in_pcb.h    19 Aug 2022 18:24:10 -0000
@@ -283,17 +283,17 @@ struct inpcb *
 void    in_pcbunref(struct inpcb *);
 void    in_pcbdisconnect(struct inpcb *);
 struct inpcb *
-        in_pcbhashlookup(struct inpcbtable *, struct in_addr,
+        in_pcblookup(struct inpcbtable *, struct in_addr,
                               u_int, struct in_addr, u_int, u_int);
 struct inpcb *
         in_pcblookup_listen(struct inpcbtable *, struct in_addr, u_int,
            struct mbuf *, u_int);
 #ifdef INET6
 struct inpcbhead *
-        in6_pcbhash(struct inpcbtable *, int, const struct in6_addr *,
+        in6_pcbhash(struct inpcbtable *, u_int, const struct in6_addr *,
            u_short, const struct in6_addr *, u_short);
 struct inpcb *
-        in6_pcbhashlookup(struct inpcbtable *, const struct in6_addr *,
+        in6_pcblookup(struct inpcbtable *, const struct in6_addr *,
                               u_int, const struct in6_addr *, u_int, u_int);
 struct inpcb *
         in6_pcblookup_listen(struct inpcbtable *, struct in6_addr *, u_int,
Index: netinet/tcp_input.c
===================================================================
RCS file: /data/mirror/openbsd/cvs/src/sys/netinet/tcp_input.c,v
retrieving revision 1.377
diff -u -p -r1.377 tcp_input.c
--- netinet/tcp_input.c 11 Aug 2022 09:13:21 -0000      1.377
+++ netinet/tcp_input.c 19 Aug 2022 16:22:58 -0000
@@ -531,13 +531,13 @@ findpcb:
                switch (af) {
 #ifdef INET6
                case AF_INET6:
-                       inp = in6_pcbhashlookup(&tcbtable, &ip6->ip6_src,
+                       inp = in6_pcblookup(&tcbtable, &ip6->ip6_src,
                            th->th_sport, &ip6->ip6_dst, th->th_dport,
                            m->m_pkthdr.ph_rtableid);
                        break;
 #endif
                case AF_INET:
-                       inp = in_pcbhashlookup(&tcbtable, ip->ip_src,
+                       inp = in_pcblookup(&tcbtable, ip->ip_src,
                            th->th_sport, ip->ip_dst, th->th_dport,
                            m->m_pkthdr.ph_rtableid);
                        break;
Index: netinet/tcp_subr.c
===================================================================
RCS file: /data/mirror/openbsd/cvs/src/sys/netinet/tcp_subr.c,v
retrieving revision 1.185
diff -u -p -r1.185 tcp_subr.c
--- netinet/tcp_subr.c  8 Aug 2022 12:06:30 -0000       1.185
+++ netinet/tcp_subr.c  19 Aug 2022 16:23:06 -0000
@@ -661,7 +661,7 @@ tcp6_ctlinput(int cmd, struct sockaddr *
                 * corresponding to the address in the ICMPv6 message
                 * payload.
                 */
-               inp = in6_pcbhashlookup(&tcbtable, &sa6->sin6_addr,
+               inp = in6_pcblookup(&tcbtable, &sa6->sin6_addr,
                    th.th_dport, &sa6_src->sin6_addr, th.th_sport, rdomain);
                if (cmd == PRC_MSGSIZE) {
                        /*
@@ -733,7 +733,7 @@ tcp_ctlinput(int cmd, struct sockaddr *s
                 */
                th = (struct tcphdr *)((caddr_t)ip + (ip->ip_hl << 2));
                seq = ntohl(th->th_seq);
-               inp = in_pcbhashlookup(&tcbtable,
+               inp = in_pcblookup(&tcbtable,
                    ip->ip_dst, th->th_dport, ip->ip_src, th->th_sport,
                    rdomain);
                if (inp && (tp = intotcpcb(inp)) &&
@@ -798,7 +798,7 @@ tcp_ctlinput(int cmd, struct sockaddr *s
 
        if (ip) {
                th = (struct tcphdr *)((caddr_t)ip + (ip->ip_hl << 2));
-               inp = in_pcbhashlookup(&tcbtable,
+               inp = in_pcblookup(&tcbtable,
                    ip->ip_dst, th->th_dport, ip->ip_src, th->th_sport,
                    rdomain);
                if (inp) {
Index: netinet/tcp_usrreq.c
===================================================================
RCS file: /data/mirror/openbsd/cvs/src/sys/netinet/tcp_usrreq.c,v
retrieving revision 1.188
diff -u -p -r1.188 tcp_usrreq.c
--- netinet/tcp_usrreq.c        15 Aug 2022 14:44:18 -0000      1.188
+++ netinet/tcp_usrreq.c        19 Aug 2022 16:23:10 -0000
@@ -915,12 +915,12 @@ tcp_ident(void *oldp, size_t *oldlenp, v
        switch (tir.faddr.ss_family) {
 #ifdef INET6
        case AF_INET6:
-               inp = in6_pcbhashlookup(&tcbtable, &f6,
+               inp = in6_pcblookup(&tcbtable, &f6,
                    fin6->sin6_port, &l6, lin6->sin6_port, tir.rdomain);
                break;
 #endif
        case AF_INET:
-               inp = in_pcbhashlookup(&tcbtable, fin->sin_addr,
+               inp = in_pcblookup(&tcbtable, fin->sin_addr,
                    fin->sin_port, lin->sin_addr, lin->sin_port, tir.rdomain);
                break;
        default:
Index: netinet/udp_usrreq.c
===================================================================
RCS file: /data/mirror/openbsd/cvs/src/sys/netinet/udp_usrreq.c,v
retrieving revision 1.282
diff -u -p -r1.282 udp_usrreq.c
--- netinet/udp_usrreq.c        15 Aug 2022 09:11:39 -0000      1.282
+++ netinet/udp_usrreq.c        19 Aug 2022 16:23:20 -0000
@@ -478,12 +478,12 @@ udp_input(struct mbuf **mp, int *offp, i
        if (inp == NULL) {
 #ifdef INET6
                if (ip6)
-                       inp = in6_pcbhashlookup(&udbtable, &ip6->ip6_src,
+                       inp = in6_pcblookup(&udbtable, &ip6->ip6_src,
                            uh->uh_sport, &ip6->ip6_dst, uh->uh_dport,
                            m->m_pkthdr.ph_rtableid);
                else
 #endif /* INET6 */
-               inp = in_pcbhashlookup(&udbtable, ip->ip_src, uh->uh_sport,
+               inp = in_pcblookup(&udbtable, ip->ip_src, uh->uh_sport,
                    ip->ip_dst, uh->uh_dport, m->m_pkthdr.ph_rtableid);
        }
        if (inp == NULL) {
@@ -774,7 +774,7 @@ udp6_ctlinput(int cmd, struct sockaddr *
                         * corresponding to the address in the ICMPv6 message
                         * payload.
                         */
-                       inp = in6_pcbhashlookup(&udbtable, &sa6.sin6_addr,
+                       inp = in6_pcblookup(&udbtable, &sa6.sin6_addr,
                            uh.uh_dport, &sa6_src.sin6_addr, uh.uh_sport,
                            rdomain);
 #if 0
@@ -860,7 +860,7 @@ udp_ctlinput(int cmd, struct sockaddr *s
                        return;
                }
 #endif
-               inp = in_pcbhashlookup(&udbtable,
+               inp = in_pcblookup(&udbtable,
                    ip->ip_dst, uhp->uh_dport, ip->ip_src, uhp->uh_sport,
                    rdomain);
                if (inp && inp->inp_socket != NULL)
Index: netinet6/in6_pcb.c
===================================================================
RCS file: /data/mirror/openbsd/cvs/src/sys/netinet6/in6_pcb.c,v
retrieving revision 1.119
diff -u -p -r1.119 in6_pcb.c
--- netinet6/in6_pcb.c  8 Aug 2022 12:06:31 -0000       1.119
+++ netinet6/in6_pcb.c  19 Aug 2022 20:16:23 -0000
@@ -126,13 +126,16 @@
 
 const struct in6_addr zeroin6_addr;
 
+struct inpcb *in6_pcbhash_lookup(struct inpcbtable *, u_int,
+    const struct in6_addr *, u_short, const struct in6_addr *, u_short);
+
 struct inpcbhead *
-in6_pcbhash(struct inpcbtable *table, int rdom,
+in6_pcbhash(struct inpcbtable *table, u_int rdomain,
     const struct in6_addr *faddr, u_short fport,
     const struct in6_addr *laddr, u_short lport)
 {
        SIPHASH_CTX ctx;
-       u_int32_t nrdom = htonl(rdom);
+       u_int32_t nrdom = htonl(rdomain);
 
        SipHash24_Init(&ctx, &table->inpt_key);
        SipHash24_Update(&ctx, &nrdom, sizeof(nrdom));
@@ -280,7 +283,7 @@ in6_pcbconnect(struct inpcb *inp, struct
 
        inp->inp_ipv6.ip6_hlim = (u_int8_t)in6_selecthlim(inp);
 
-       t = in6_pcbhashlookup(inp->inp_table, &sin6->sin6_addr, sin6->sin6_port,
+       t = in6_pcblookup(inp->inp_table, &sin6->sin6_addr, sin6->sin6_port,
            IN6_IS_ADDR_UNSPECIFIED(&inp->inp_laddr6) ? in6a : &inp->inp_laddr6,
            inp->inp_lport, inp->inp_rtableid);
        if (t != NULL) {
@@ -295,7 +298,7 @@ in6_pcbconnect(struct inpcb *inp, struct
                        error = in_pcbbind(inp, NULL, curproc);
                        if (error)
                                return (error);
-                       t = in6_pcbhashlookup(inp->inp_table, &sin6->sin6_addr,
+                       t = in6_pcblookup(inp->inp_table, &sin6->sin6_addr,
                            sin6->sin6_port, in6a, inp->inp_lport,
                            inp->inp_rtableid);
                        if (t != NULL) {
@@ -516,37 +519,51 @@ in6_pcbnotify(struct inpcbtable *table, 
 }
 
 struct inpcb *
-in6_pcbhashlookup(struct inpcbtable *table, const struct in6_addr *faddr,
-    u_int fport_arg, const struct in6_addr *laddr, u_int lport_arg,
-    u_int rtable)
+in6_pcbhash_lookup(struct inpcbtable *table, u_int rdomain,
+    const struct in6_addr *faddr, u_short fport,
+    const struct in6_addr *laddr, u_short lport)
 {
        struct inpcbhead *head;
        struct inpcb *inp;
-       u_int16_t fport = fport_arg, lport = lport_arg;
-       u_int rdomain;
 
-       rdomain = rtable_l2(rtable);
-       mtx_enter(&table->inpt_mtx);
+       NET_ASSERT_LOCKED();
+       MUTEX_ASSERT_LOCKED(&table->inpt_mtx);
+
        head = in6_pcbhash(table, rdomain, faddr, fport, laddr, lport);
        LIST_FOREACH(inp, head, inp_hash) {
-               if (!(inp->inp_flags & INP_IPV6))
+               if (!ISSET(inp->inp_flags, INP_IPV6))
                        continue;
-               if (IN6_ARE_ADDR_EQUAL(&inp->inp_faddr6, faddr) &&
-                   inp->inp_fport == fport && inp->inp_lport == lport &&
+               if (inp->inp_fport == fport && inp->inp_lport == lport &&
+                   IN6_ARE_ADDR_EQUAL(&inp->inp_faddr6, faddr) &&
                    IN6_ARE_ADDR_EQUAL(&inp->inp_laddr6, laddr) &&
                    rtable_l2(inp->inp_rtableid) == rdomain) {
-                       /*
-                        * Move this PCB to the head of hash chain so that
-                        * repeated accesses are quicker.  This is analogous to
-                        * the historic single-entry PCB cache.
-                        */
-                       if (inp != LIST_FIRST(head)) {
-                               LIST_REMOVE(inp, inp_hash);
-                               LIST_INSERT_HEAD(head, inp, inp_hash);
-                       }
                        break;
                }
        }
+       if (inp != NULL) {
+               /*
+                * Move this PCB to the head of hash chain so that
+                * repeated accesses are quicker.  This is analogous to
+                * the historic single-entry PCB cache.
+                */
+               if (inp != LIST_FIRST(head)) {
+                       LIST_REMOVE(inp, inp_hash);
+                       LIST_INSERT_HEAD(head, inp, inp_hash);
+               }
+       }
+       return (inp);
+}
+
+struct inpcb *
+in6_pcblookup(struct inpcbtable *table, const struct in6_addr *faddr,
+    u_int fport, const struct in6_addr *laddr, u_int lport, u_int rtable)
+{
+       struct inpcb *inp;
+       u_int rdomain;
+
+       rdomain = rtable_l2(rtable);
+       mtx_enter(&table->inpt_mtx);
+       inp = in6_pcbhash_lookup(table, rdomain, faddr, fport, laddr, lport);
        in_pcbref(inp);
        mtx_leave(&table->inpt_mtx);
 #ifdef DIAGNOSTIC
@@ -560,12 +577,10 @@ in6_pcbhashlookup(struct inpcbtable *tab
 
 struct inpcb *
 in6_pcblookup_listen(struct inpcbtable *table, struct in6_addr *laddr,
-    u_int lport_arg, struct mbuf *m, u_int rtable)
+    u_int lport, struct mbuf *m, u_int rtable)
 {
-       struct inpcbhead *head;
        const struct in6_addr *key1, *key2;
        struct inpcb *inp;
-       u_int16_t lport = lport_arg;
        u_int rdomain;
 
        key1 = laddr;
@@ -600,37 +615,10 @@ in6_pcblookup_listen(struct inpcbtable *
 
        rdomain = rtable_l2(rtable);
        mtx_enter(&table->inpt_mtx);
-       head = in6_pcbhash(table, rdomain, &zeroin6_addr, 0, key1, lport);
-       LIST_FOREACH(inp, head, inp_hash) {
-               if (!(inp->inp_flags & INP_IPV6))
-                       continue;
-               if (inp->inp_lport == lport && inp->inp_fport == 0 &&
-                   IN6_ARE_ADDR_EQUAL(&inp->inp_laddr6, key1) &&
-                   IN6_IS_ADDR_UNSPECIFIED(&inp->inp_faddr6) &&
-                   rtable_l2(inp->inp_rtableid) == rdomain)
-                       break;
-       }
+       inp = in6_pcbhash_lookup(table, rdomain, &zeroin6_addr, 0, key1, lport);
        if (inp == NULL && ! IN6_ARE_ADDR_EQUAL(key1, key2)) {
-               head = in6_pcbhash(table, rdomain,
+               inp = in6_pcbhash_lookup(table, rdomain,
                    &zeroin6_addr, 0, key2, lport);
-               LIST_FOREACH(inp, head, inp_hash) {
-                       if (!(inp->inp_flags & INP_IPV6))
-                               continue;
-                       if (inp->inp_lport == lport && inp->inp_fport == 0 &&
-                           IN6_ARE_ADDR_EQUAL(&inp->inp_laddr6, key2) &&
-                           IN6_IS_ADDR_UNSPECIFIED(&inp->inp_faddr6) &&
-                           rtable_l2(inp->inp_rtableid) == rdomain)
-                               break;
-               }
-       }
-       /*
-        * Move this PCB to the head of hash chain so that
-        * repeated accesses are quicker.  This is analogous to
-        * the historic single-entry PCB cache.
-        */
-       if (inp != NULL && inp != LIST_FIRST(head)) {
-               LIST_REMOVE(inp, inp_hash);
-               LIST_INSERT_HEAD(head, inp, inp_hash);
        }
        in_pcbref(inp);
        mtx_leave(&table->inpt_mtx);
Index: netinet6/raw_ip6.c
===================================================================
RCS file: /data/mirror/openbsd/cvs/src/sys/netinet6/raw_ip6.c,v
retrieving revision 1.150
diff -u -p -r1.150 raw_ip6.c
--- netinet6/raw_ip6.c  15 Aug 2022 09:11:39 -0000      1.150
+++ netinet6/raw_ip6.c  19 Aug 2022 16:23:25 -0000
@@ -319,7 +319,7 @@ rip6_ctlinput(int cmd, struct sockaddr *
                 * XXX chase extension headers, or pass final nxt value
                 * from icmp6_notify_error()
                 */
-               in6p = in6_pcbhashlookup(&rawin6pcbtable, &sa6->sin6_addr, 0,
+               in6p = in6_pcblookup(&rawin6pcbtable, &sa6->sin6_addr, 0,
                    &sa6_src->sin6_addr, 0, rdomain);
 
                if (in6p && in6p->inp_ipv6.ip6_nxt &&

Reply via email to