On 05/06/18(Tue) 13:30, Martin Pieuchot wrote:
> Diff below rename some fields in pfkey & routing sockets to help the
> reader understand where they are coming from.  The goal is to lock
> per-socket fields.
> 
> I'll prefix the rest of the fields in a different diff to ease reviews.

Here's the second part:

diff --git sys/net/pfkeyv2.c sys/net/pfkeyv2.c
index 4d2ec841b52..10386773b13 100644
--- sys/net/pfkeyv2.c
+++ sys/net/pfkeyv2.c
@@ -140,11 +140,11 @@ struct keycb {
 #define kcb_proto      kcb_rcb.rcb_proto
 
        SRPL_ENTRY(keycb)       kcb_list;
-       struct refcnt           refcnt;
-       int                     flags;
-       uint32_t                pid;
-       uint32_t                registration; /* Increase if SATYPE_MAX > 31 */
-       unsigned int            rdomain;
+       struct refcnt           kcb_refcnt;
+       int                     kcb_flags;
+       uint32_t                kcb_pid;
+       uint32_t                kcb_registration; /* Inc. if SATYPE_MAX > 31 */
+       unsigned int            kcb_rdomain;
 };
 #define sotokeycb(so) ((struct keycb *)(so)->so_pcb)
 
@@ -225,7 +225,7 @@ keycb_ref(void *null, void *v)
 {
        struct keycb *kp = v;
 
-       refcnt_take(&kp->refcnt);
+       refcnt_take(&kp->kcb_refcnt);
 }
 
 void
@@ -233,7 +233,7 @@ keycb_unref(void *null, void *v)
 {
        struct keycb *kp = v;
 
-       refcnt_rele_wake(&kp->refcnt);
+       refcnt_rele_wake(&kp->kcb_refcnt);
 }
 
 void
@@ -260,7 +260,7 @@ pfkeyv2_attach(struct socket *so, int proto)
 
        kp = malloc(sizeof(struct keycb), M_PCB, M_WAITOK | M_ZERO);
        so->so_pcb = kp;
-       refcnt_init(&kp->refcnt);
+       refcnt_init(&kp->kcb_refcnt);
 
        error = soreserve(so, RAWSNDQ, RAWRCVQ);
        if (error) {
@@ -276,8 +276,8 @@ pfkeyv2_attach(struct socket *so, int proto)
        soisconnected(so);
 
        kp->kcb_faddr = &pfkey_addr;
-       kp->pid = curproc->p_p->ps_pid;
-       kp->rdomain = rtable_l2(curproc->p_p->ps_rtableid);
+       kp->kcb_pid = curproc->p_p->ps_pid;
+       kp->kcb_rdomain = rtable_l2(curproc->p_p->ps_rtableid);
 
        rw_enter(&pfkey_cb.kcb_lk, RW_WRITE);
        SRPL_INSERT_HEAD_LOCKED(&pfkey_cb.kcb_rc, &pfkey_cb.kcb, kp, kcb_list);
@@ -300,13 +300,13 @@ pfkeyv2_detach(struct socket *so)
        if (kp == NULL)
                return ENOTCONN;
 
-       if (kp->flags &
+       if (kp->kcb_flags &
            (PFKEYV2_SOCKETFLAGS_REGISTERED|PFKEYV2_SOCKETFLAGS_PROMISC)) {
                mtx_enter(&pfkeyv2_mtx);
-               if (kp->flags & PFKEYV2_SOCKETFLAGS_REGISTERED)
+               if (kp->kcb_flags & PFKEYV2_SOCKETFLAGS_REGISTERED)
                        nregistered--;
 
-               if (kp->flags & PFKEYV2_SOCKETFLAGS_PROMISC)
+               if (kp->kcb_flags & PFKEYV2_SOCKETFLAGS_PROMISC)
                        npromisc--;
                mtx_leave(&pfkeyv2_mtx);
        }
@@ -317,7 +317,7 @@ pfkeyv2_detach(struct socket *so)
        rw_exit(&pfkey_cb.kcb_lk);
 
        /* wait for all references to drop */
-       refcnt_finalize(&kp->refcnt, "pfkeyrefs");
+       refcnt_finalize(&kp->kcb_refcnt, "pfkeyrefs");
 
        so->so_pcb = NULL;
        KASSERT((so->so_state & SS_NOFDREF) == 0);
@@ -469,9 +469,9 @@ pfkeyv2_sendmessage(void **headers, int mode, struct socket 
*so,
                 * original destination.
                 */
                SRPL_FOREACH(s, &sr, &pfkey_cb.kcb, kcb_list) {
-                       if ((s->flags & PFKEYV2_SOCKETFLAGS_PROMISC) &&
+                       if ((s->kcb_flags & PFKEYV2_SOCKETFLAGS_PROMISC) &&
                            (s->kcb_socket != so) &&
-                           (s->rdomain == rdomain))
+                           (s->kcb_rdomain == rdomain))
                                pfkey_sendup(s, packet, 1);
                }
                SRPL_LEAVE(&sr);
@@ -484,13 +484,13 @@ pfkeyv2_sendmessage(void **headers, int mode, struct 
socket *so,
                 * the specified satype (e.g., all IPSEC-ESP negotiators)
                 */
                SRPL_FOREACH(s, &sr, &pfkey_cb.kcb, kcb_list) {
-                       if ((s->flags & PFKEYV2_SOCKETFLAGS_REGISTERED) &&
-                           (s->rdomain == rdomain)) {
+                       if ((s->kcb_flags & PFKEYV2_SOCKETFLAGS_REGISTERED) &&
+                           (s->kcb_rdomain == rdomain)) {
                                if (!satype)    /* Just send to everyone 
registered */
                                        pfkey_sendup(s, packet, 1);
                                else {
                                        /* Check for specified satype */
-                                       if ((1 << satype) & s->registration)
+                                       if ((1 << satype) & s->kcb_registration)
                                                pfkey_sendup(s, packet, 1);
                                }
                        }
@@ -515,9 +515,9 @@ pfkeyv2_sendmessage(void **headers, int mode, struct socket 
*so,
 
                /* Send to all registered promiscuous listeners */
                SRPL_FOREACH(s, &sr, &pfkey_cb.kcb, kcb_list) {
-                       if ((s->flags & PFKEYV2_SOCKETFLAGS_PROMISC) &&
-                           !(s->flags & PFKEYV2_SOCKETFLAGS_REGISTERED) &&
-                           (s->rdomain == rdomain))
+                       if ((s->kcb_flags & PFKEYV2_SOCKETFLAGS_PROMISC) &&
+                           !(s->kcb_flags & PFKEYV2_SOCKETFLAGS_REGISTERED) &&
+                           (s->kcb_rdomain == rdomain))
                                pfkey_sendup(s, packet, 1);
                }
                SRPL_LEAVE(&sr);
@@ -527,7 +527,7 @@ pfkeyv2_sendmessage(void **headers, int mode, struct socket 
*so,
        case PFKEYV2_SENDMESSAGE_BROADCAST:
                /* Send message to all sockets */
                SRPL_FOREACH(s, &sr, &pfkey_cb.kcb, kcb_list) {
-                       if (s->rdomain == rdomain)
+                       if (s->kcb_rdomain == rdomain)
                                pfkey_sendup(s, packet, 1);
                }
                SRPL_LEAVE(&sr);
@@ -1026,7 +1026,7 @@ pfkeyv2_send(struct socket *so, void *message, int len)
                goto ret;
        }
 
-       rdomain = kp->rdomain;
+       rdomain = kp->kcb_rdomain;
 
        /* If we have any promiscuous listeners, send them a copy of the 
message */
        if (promisc) {
@@ -1056,8 +1056,8 @@ pfkeyv2_send(struct socket *so, void *message, int len)
 
                /* Send to all promiscuous listeners */
                SRPL_FOREACH(bkp, &sr, &pfkey_cb.kcb, kcb_list) {
-                       if ((bkp->flags & PFKEYV2_SOCKETFLAGS_PROMISC) &&
-                           (bkp->rdomain == rdomain))
+                       if ((bkp->kcb_flags & PFKEYV2_SOCKETFLAGS_PROMISC) &&
+                           (bkp->kcb_rdomain == rdomain))
                                pfkey_sendup(bkp, packet, 1);
                }
                SRPL_LEAVE(&sr);
@@ -1448,8 +1448,8 @@ pfkeyv2_send(struct socket *so, void *message, int len)
                break;
 
        case SADB_REGISTER:
-               if (!(kp->flags & PFKEYV2_SOCKETFLAGS_REGISTERED)) {
-                       kp->flags |= PFKEYV2_SOCKETFLAGS_REGISTERED;
+               if (!(kp->kcb_flags & PFKEYV2_SOCKETFLAGS_REGISTERED)) {
+                       kp->kcb_flags |= PFKEYV2_SOCKETFLAGS_REGISTERED;
                        mtx_enter(&pfkeyv2_mtx);
                        nregistered++;
                        mtx_leave(&pfkeyv2_mtx);
@@ -1481,7 +1481,8 @@ pfkeyv2_send(struct socket *so, void *message, int len)
                }
 
                /* Keep track what this socket has registered for */
-               kp->registration |= (1 << ((struct sadb_msg 
*)message)->sadb_msg_satype);
+               kp->kcb_registration |=
+                   (1 << ((struct sadb_msg *)message)->sadb_msg_satype);
 
                ssup = (struct sadb_supported *) freeme;
                ssup->sadb_supported_len = i / sizeof(uint64_t);
@@ -1831,9 +1832,9 @@ pfkeyv2_send(struct socket *so, void *message, int len)
 
                        SRPL_FOREACH(bkp, &sr, &pfkey_cb.kcb, kcb_list) {
                                if ((bkp != kp) &&
-                                   (bkp->rdomain == rdomain) &&
+                                   (bkp->kcb_rdomain == rdomain) &&
                                    (!smsg->sadb_msg_seq ||
-                                   (smsg->sadb_msg_seq == kp->pid)))
+                                   (smsg->sadb_msg_seq == kp->kcb_pid)))
                                        pfkey_sendup(bkp, packet, 1);
                        }
                        SRPL_LEAVE(&sr);
@@ -1845,19 +1846,19 @@ pfkeyv2_send(struct socket *so, void *message, int len)
                                goto ret;
                        }
 
-                       i = (kp->flags &
+                       i = (kp->kcb_flags &
                            PFKEYV2_SOCKETFLAGS_PROMISC) ? 1 : 0;
                        j = smsg->sadb_msg_satype ? 1 : 0;
 
                        if (i ^ j) {
                                if (j) {
-                                       kp->flags |=
+                                       kp->kcb_flags |=
                                            PFKEYV2_SOCKETFLAGS_PROMISC;
                                        mtx_enter(&pfkeyv2_mtx);
                                        npromisc++;
                                        mtx_leave(&pfkeyv2_mtx);
                                } else {
-                                       kp->flags &=
+                                       kp->kcb_flags &=
                                            ~PFKEYV2_SOCKETFLAGS_PROMISC;
                                        mtx_enter(&pfkeyv2_mtx);
                                        npromisc--;
diff --git sys/net/rtsock.c sys/net/rtsock.c
index 0c46854b092..afb74663708 100644
--- sys/net/rtsock.c
+++ sys/net/rtsock.c
@@ -142,12 +142,12 @@ struct routecb {
 #define rop_proto      rop_rcb.rcb_proto
 
        SRPL_ENTRY(routecb)     rop_list;
-       struct refcnt           refcnt;
-       struct timeout          timeout;
-       unsigned int            msgfilter;
-       unsigned int            flags;
-       u_int                   rtableid;
-       u_char                  priority;
+       struct refcnt           rop_refcnt;
+       struct timeout          rop_timeout;
+       unsigned int            rop_msgfilter;
+       unsigned int            rop_flags;
+       u_int                   rop_rtableid;
+       u_char                  rop_priority;
 };
 #define        sotoroutecb(so) ((struct routecb *)(so)->so_pcb)
 
@@ -184,7 +184,7 @@ rcb_ref(void *null, void *v)
 {
        struct routecb *rop = v;
 
-       refcnt_take(&rop->refcnt);
+       refcnt_take(&rop->rop_refcnt);
 }
 
 void
@@ -192,7 +192,7 @@ rcb_unref(void *null, void *v)
 {
        struct routecb *rop = v;
 
-       refcnt_rele_wake(&rop->refcnt);
+       refcnt_rele_wake(&rop->rop_refcnt);
 }
 
 int
@@ -216,10 +216,10 @@ route_usrreq(struct socket *so, int req, struct mbuf *m, 
struct mbuf *nam,
                 * If we are in a FLUSH state, check if the buffer is
                 * empty so that we can clear the flag.
                 */
-               if (((rop->flags & ROUTECB_FLAG_FLUSH) != 0) &&
+               if (((rop->rop_flags & ROUTECB_FLAG_FLUSH) != 0) &&
                    ((sbspace(rop->rop_socket, &rop->rop_socket->so_rcv) ==
                    rop->rop_socket->so_rcv.sb_hiwat)))
-                       rop->flags &= ~ROUTECB_FLAG_FLUSH;
+                       rop->rop_flags &= ~ROUTECB_FLAG_FLUSH;
                break;
 
        default:
@@ -243,8 +243,8 @@ route_attach(struct socket *so, int proto)
        rop = malloc(sizeof(struct routecb), M_PCB, M_WAITOK|M_ZERO);
        so->so_pcb = rop;
        /* Init the timeout structure */
-       timeout_set(&rop->timeout, rtm_senddesync, rop);
-       refcnt_init(&rop->refcnt);
+       timeout_set(&rop->rop_timeout, rtm_senddesync, rop);
+       refcnt_init(&rop->rop_refcnt);
 
        if (curproc == NULL)
                error = EACCES;
@@ -259,7 +259,7 @@ route_attach(struct socket *so, int proto)
        rop->rop_proto.sp_family = so->so_proto->pr_domain->dom_family;
        rop->rop_proto.sp_protocol = proto;
 
-       rop->rtableid = curproc->p_p->ps_rtableid;
+       rop->rop_rtableid = curproc->p_p->ps_rtableid;
 
        soisconnected(so);
        so->so_options |= SO_USELOOPBACK;
@@ -287,7 +287,7 @@ route_detach(struct socket *so)
 
        rw_enter(&route_cb.rcb_lk, RW_WRITE);
 
-       timeout_del(&rop->timeout);
+       timeout_del(&rop->rop_timeout);
        route_cb.any_count--;
 
        SRPL_REMOVE_LOCKED(&route_cb.rcb_rc, &route_cb.rcb,
@@ -295,7 +295,7 @@ route_detach(struct socket *so)
        rw_exit(&route_cb.rcb_lk);
 
        /* wait for all references to drop */
-       refcnt_finalize(&rop->refcnt, "rtsockrefs");
+       refcnt_finalize(&rop->rop_refcnt, "rtsockrefs");
 
        so->so_pcb = NULL;
        KASSERT((so->so_state & SS_NOFDREF) == 0);
@@ -322,7 +322,7 @@ route_ctloutput(int op, struct socket *so, int level, int 
optname,
                        if (m == NULL || m->m_len != sizeof(unsigned int))
                                error = EINVAL;
                        else
-                               rop->msgfilter = *mtod(m, unsigned int *);
+                               rop->rop_msgfilter = *mtod(m, unsigned int *);
                        break;
                case ROUTE_TABLEFILTER:
                        if (m == NULL || m->m_len != sizeof(unsigned int)) {
@@ -333,7 +333,7 @@ route_ctloutput(int op, struct socket *so, int level, int 
optname,
                        if (tid != RTABLE_ANY && !rtable_exists(tid))
                                error = ENOENT;
                        else
-                               rop->rtableid = tid;
+                               rop->rop_rtableid = tid;
                        break;
                case ROUTE_PRIOFILTER:
                        if (m == NULL || m->m_len != sizeof(unsigned int)) {
@@ -344,7 +344,7 @@ route_ctloutput(int op, struct socket *so, int level, int 
optname,
                        if (prio > RTP_MAX)
                                error = EINVAL;
                        else
-                               rop->priority = prio;
+                               rop->rop_priority = prio;
                        break;
                default:
                        error = ENOPROTOOPT;
@@ -355,15 +355,15 @@ route_ctloutput(int op, struct socket *so, int level, int 
optname,
                switch (optname) {
                case ROUTE_MSGFILTER:
                        m->m_len = sizeof(unsigned int);
-                       *mtod(m, unsigned int *) = rop->msgfilter;
+                       *mtod(m, unsigned int *) = rop->rop_msgfilter;
                        break;
                case ROUTE_TABLEFILTER:
                        m->m_len = sizeof(unsigned int);
-                       *mtod(m, unsigned int *) = rop->rtableid;
+                       *mtod(m, unsigned int *) = rop->rop_rtableid;
                        break;
                case ROUTE_PRIOFILTER:
                        m->m_len = sizeof(unsigned int);
-                       *mtod(m, unsigned int *) = rop->priority;
+                       *mtod(m, unsigned int *) = rop->rop_priority;
                        break;
                default:
                        error = ENOPROTOOPT;
@@ -382,7 +382,7 @@ rtm_senddesync(void *data)
        rop = (struct routecb *)data;
 
        /* If we are in a DESYNC state, try to send a RTM_DESYNC packet */
-       if ((rop->flags & ROUTECB_FLAG_DESYNC) == 0)
+       if ((rop->rop_flags & ROUTECB_FLAG_DESYNC) == 0)
                return;
 
        /*
@@ -394,14 +394,14 @@ rtm_senddesync(void *data)
                struct socket *so = rop->rop_socket;
                if (sbappendaddr(so, &so->so_rcv, &route_src,
                    desync_mbuf, NULL) != 0) {
-                       rop->flags &= ~ROUTECB_FLAG_DESYNC;
+                       rop->rop_flags &= ~ROUTECB_FLAG_DESYNC;
                        sorwakeup(rop->rop_socket);
                        return;
                }
                m_freem(desync_mbuf);
        }
        /* Re-add timeout to try sending msg again */
-       timeout_add(&rop->timeout, ROUTE_DESYNC_RESEND_TIMEOUT);
+       timeout_add(&rop->rop_timeout, ROUTE_DESYNC_RESEND_TIMEOUT);
 }
 
 void
@@ -443,10 +443,11 @@ route_input(struct mbuf *m0, struct socket *so, 
sa_family_t sa_family)
                /* filter messages that the process does not want */
                rtm = mtod(m, struct rt_msghdr *);
                /* but RTM_DESYNC can't be filtered */
-               if (rtm->rtm_type != RTM_DESYNC && rop->msgfilter != 0 &&
-                   !(rop->msgfilter & (1 << rtm->rtm_type)))
+               if (rtm->rtm_type != RTM_DESYNC && rop->rop_msgfilter != 0 &&
+                   !(rop->rop_msgfilter & (1 << rtm->rtm_type)))
                        continue;
-               if (rop->priority != 0 && rop->priority < rtm->rtm_priority)
+               if (rop->rop_priority != 0 &&
+                   rop->rop_priority < rtm->rtm_priority)
                        continue;
                switch (rtm->rtm_type) {
                case RTM_IFANNOUNCE:
@@ -458,14 +459,14 @@ route_input(struct mbuf *m0, struct socket *so, 
sa_family_t sa_family)
                case RTM_DELADDR:
                case RTM_IFINFO:
                        /* check against rdomain id */
-                       if (rop->rtableid != RTABLE_ANY &&
-                           rtable_l2(rop->rtableid) != rtm->rtm_tableid)
+                       if (rop->rop_rtableid != RTABLE_ANY &&
+                           rtable_l2(rop->rop_rtableid) != rtm->rtm_tableid)
                                continue;
                        break;
                default:
                        /* check against rtable id */
-                       if (rop->rtableid != RTABLE_ANY &&
-                           rop->rtableid != rtm->rtm_tableid)
+                       if (rop->rop_rtableid != RTABLE_ANY &&
+                           rop->rop_rtableid != rtm->rtm_tableid)
                                continue;
                        break;
                }
@@ -474,22 +475,22 @@ route_input(struct mbuf *m0, struct socket *so, 
sa_family_t sa_family)
                 * Check to see if the flush flag is set. If so, don't queue
                 * any more messages until the flag is cleared.
                 */
-               if ((rop->flags & ROUTECB_FLAG_FLUSH) != 0)
+               if ((rop->rop_flags & ROUTECB_FLAG_FLUSH) != 0)
                        continue;
 
                if (last) {
                        rtm_sendup(last, m, 1);
-                       refcnt_rele_wake(&sotoroutecb(last)->refcnt);
+                       refcnt_rele_wake(&sotoroutecb(last)->rop_refcnt);
                }
                /* keep a reference for last */
-               refcnt_take(&rop->refcnt);
+               refcnt_take(&rop->rop_refcnt);
                last = rop->rop_socket;
        }
        SRPL_LEAVE(&sr);
 
        if (last) {
                rtm_sendup(last, m, 0);
-               refcnt_rele_wake(&sotoroutecb(last)->refcnt);
+               refcnt_rele_wake(&sotoroutecb(last)->rop_refcnt);
        } else
                m_freem(m);
 }
@@ -510,7 +511,7 @@ rtm_sendup(struct socket *so, struct mbuf *m0, int more)
        if (sbspace(so, &so->so_rcv) < (2 * MSIZE) ||
            sbappendaddr(so, &so->so_rcv, &route_src, m, NULL) == 0) {
                /* Flag socket as desync'ed and flush required */
-               rop->flags |= ROUTECB_FLAG_DESYNC | ROUTECB_FLAG_FLUSH;
+               rop->rop_flags |= ROUTECB_FLAG_DESYNC | ROUTECB_FLAG_FLUSH;
                rtm_senddesync(rop);
                m_freem(m);
                return (ENOBUFS);

Reply via email to