Re: move PRU_DETACH out of pr_usrreq

2017-11-02 Thread Martin Pieuchot
On 02/11/17(Thu) 09:06, Florian Obser wrote:
> this moves PRU_DETACH out of pr_usrreq into per proto pr_detach
> functions, like what claudio did to pr_attach.
> 
> Intentionally mostly mechanical. There might be some cleanup here and
> there in the functions themselves.

I like it, comments inline.

> diff --git kern/uipc_socket.c kern/uipc_socket.c
> index 615da029414..df08096a9ee 100644
> --- kern/uipc_socket.c
> +++ kern/uipc_socket.c
> @@ -266,8 +266,13 @@ soclose(struct socket *so)
>   }
>  drop:
>   if (so->so_pcb) {
> - int error2 = (*so->so_proto->pr_usrreq)(so, PRU_DETACH, NULL,
> - NULL, NULL, curproc);
> + int error2;
> +
> + if (so->so_proto->pr_detach == NULL)
> + panic("soclose pr_detach == NULL: so %p, so_type %d",
> + so, so->so_type);

Since we're now enforcing the existence of pr_detach, a KASSERT() is
enough.

> +
> + error2 = (*so->so_proto->pr_detach)(so);
>   if (error == 0)
>   error = error2;
>   }
> diff --git kern/uipc_usrreq.c kern/uipc_usrreq.c
> index 07e032ca0a3..35871b7cf54 100644
> --- kern/uipc_usrreq.c
> +++ kern/uipc_usrreq.c
> @@ -126,10 +126,6 @@ uipc_usrreq(struct socket *so, int req, struct mbuf *m, 
> struct mbuf *nam,
>  
>   switch (req) {
>  
> - case PRU_DETACH:
> - unp_detach(unp);
> - break;
> -
>   case PRU_BIND:
>   error = unp_bind(unp, nam, p);
>   break;
> @@ -378,6 +374,21 @@ uipc_attach(struct socket *so, int proto)
>   return (0);
>  }
>  
> +int
> +uipc_detach(struct socket *so)
> +{
> + struct unpcb *unp = sotounpcb(so);
> +
> + if (unp == NULL)
> + return (EINVAL);
> +
> + NET_ASSERT_UNLOCKED();
> +
> + unp_detach(unp);
> +
> + return (0);
> +}
> +
>  void
>  unp_detach(struct unpcb *unp)
>  {
> diff --git net/pfkeyv2.c net/pfkeyv2.c
> index ac593e4d5f1..f52ec9d3692 100644
> --- net/pfkeyv2.c
> +++ net/pfkeyv2.c
> @@ -159,7 +159,7 @@ static int npromisc = 0;
>  void pfkey_init(void);
>  
>  int pfkeyv2_attach(struct socket *, int);
> -int pfkeyv2_detach(struct socket *, struct proc *);
> +int pfkeyv2_detach(struct socket *);
>  int pfkeyv2_usrreq(struct socket *, int, struct mbuf *, struct mbuf *,
>  struct mbuf *, struct proc *);
>  int pfkeyv2_output(struct mbuf *, struct socket *, struct sockaddr *,
> @@ -192,6 +192,7 @@ static struct protosw pfkeysw[] = {
>.pr_output= pfkeyv2_output,
>.pr_usrreq= pfkeyv2_usrreq,
>.pr_attach= pfkeyv2_attach,
> +  .pr_detach= pfkeyv2_detach,
>.pr_sysctl= pfkeyv2_sysctl,
>  }
>  };
> @@ -255,7 +256,7 @@ pfkeyv2_attach(struct socket *so, int proto)
>   * Close a PF_KEYv2 socket.
>   */
>  int
> -pfkeyv2_detach(struct socket *so, struct proc *p)
> +pfkeyv2_detach(struct socket *so)
>  {
>   struct keycb *kp;
>  
> @@ -276,7 +277,7 @@ pfkeyv2_detach(struct socket *so, struct proc *p)
>   mtx_leave(_mtx);
>   }
>  
> - raw_detach(>rcb);
> + raw_do_detach(>rcb);

Claudio suggested merging the content of the raw functions in pfkey and
rtsock.  It doesn't make much sense to keep a function for calling
sofree().  So I'd get rid of raw_detach() and inline it here.

A next step could be to get rid of net/raw_cb.c

>   return (0);
>  }
>  
> @@ -284,12 +285,7 @@ int
>  pfkeyv2_usrreq(struct socket *so, int req, struct mbuf *mbuf,
>  struct mbuf *nam, struct mbuf *control, struct proc *p)
>  {
> - switch (req) {
> - case PRU_DETACH:
> - return (pfkeyv2_detach(so, p));
> - default:
> - return (raw_usrreq(so, req, mbuf, nam, control, p));
> - }
> + return (raw_usrreq(so, req, mbuf, nam, control, p));
>  }
>  
>  int
> diff --git net/raw_cb.c net/raw_cb.c
> index e77da50b039..55c3fd21d2f 100644
> --- net/raw_cb.c
> +++ net/raw_cb.c
> @@ -76,12 +76,27 @@ raw_attach(struct socket *so, int proto)
>   return (0);
>  }
>  
> +int
> +raw_detach(struct socket *so)
> +{
> + struct rawcb *rp = sotorawcb(so);
> +
> + soassertlocked(so);
> +
> + if (rp == NULL)
> + return (EINVAL);
> +
> + raw_do_detach(rp);
> +
> + return (0);
> +}
> +
>  /*
>   * Detach the raw connection block and discard
>   * socket resources.
>   */
>  void
> -raw_detach(struct rawcb *rp)
> +raw_do_detach(struct rawcb *rp)
>  {
>   struct socket *so = rp->rcb_socket;
>  
> @@ -97,5 +112,5 @@ void
>  raw_disconnect(struct rawcb *rp)
>  {
>   if (rp->rcb_socket->so_state & SS_NOFDREF)
> - raw_detach(rp);
> + raw_do_detach(rp);
>  }
> diff --git net/raw_cb.h net/raw_cb.h
> index 00c3d30f98a..a3e964d807f 100644
> --- net/raw_cb.h
> +++ net/raw_cb.h
> @@ -56,7 +56,8 @@ struct rawcb {
>  
>  #define  sotorawcb(so)   ((struct rawcb *)(so)->so_pcb)
>  int   raw_attach(struct socket *, int);
> -void  raw_detach(struct rawcb *);
> +int   

move PRU_DETACH out of pr_usrreq

2017-11-02 Thread Florian Obser
this moves PRU_DETACH out of pr_usrreq into per proto pr_detach
functions, like what claudio did to pr_attach.

Intentionally mostly mechanical. There might be some cleanup here and
there in the functions themselves.

OK?

diff --git kern/uipc_proto.c kern/uipc_proto.c
index 1e86120f374..8797f0d6322 100644
--- kern/uipc_proto.c
+++ kern/uipc_proto.c
@@ -56,6 +56,7 @@ struct protosw unixsw[] = {
   .pr_flags= PR_CONNREQUIRED|PR_WANTRCVD|PR_RIGHTS,
   .pr_usrreq   = uipc_usrreq,
   .pr_attach   = uipc_attach,
+  .pr_detach   = uipc_detach,
 },
 {
   .pr_type = SOCK_SEQPACKET,
@@ -64,6 +65,7 @@ struct protosw unixsw[] = {
   .pr_flags= PR_ATOMIC|PR_CONNREQUIRED|PR_WANTRCVD|PR_RIGHTS,
   .pr_usrreq   = uipc_usrreq,
   .pr_attach   = uipc_attach,
+  .pr_detach   = uipc_detach,
 },
 {
   .pr_type = SOCK_DGRAM,
@@ -72,6 +74,7 @@ struct protosw unixsw[] = {
   .pr_flags= PR_ATOMIC|PR_ADDR|PR_RIGHTS,
   .pr_usrreq   = uipc_usrreq,
   .pr_attach   = uipc_attach,
+  .pr_detach   = uipc_detach,
 }
 };
 
diff --git kern/uipc_socket.c kern/uipc_socket.c
index 615da029414..df08096a9ee 100644
--- kern/uipc_socket.c
+++ kern/uipc_socket.c
@@ -266,8 +266,13 @@ soclose(struct socket *so)
}
 drop:
if (so->so_pcb) {
-   int error2 = (*so->so_proto->pr_usrreq)(so, PRU_DETACH, NULL,
-   NULL, NULL, curproc);
+   int error2;
+
+   if (so->so_proto->pr_detach == NULL)
+   panic("soclose pr_detach == NULL: so %p, so_type %d",
+   so, so->so_type);
+
+   error2 = (*so->so_proto->pr_detach)(so);
if (error == 0)
error = error2;
}
diff --git kern/uipc_usrreq.c kern/uipc_usrreq.c
index 07e032ca0a3..35871b7cf54 100644
--- kern/uipc_usrreq.c
+++ kern/uipc_usrreq.c
@@ -126,10 +126,6 @@ uipc_usrreq(struct socket *so, int req, struct mbuf *m, 
struct mbuf *nam,
 
switch (req) {
 
-   case PRU_DETACH:
-   unp_detach(unp);
-   break;
-
case PRU_BIND:
error = unp_bind(unp, nam, p);
break;
@@ -378,6 +374,21 @@ uipc_attach(struct socket *so, int proto)
return (0);
 }
 
+int
+uipc_detach(struct socket *so)
+{
+   struct unpcb *unp = sotounpcb(so);
+
+   if (unp == NULL)
+   return (EINVAL);
+
+   NET_ASSERT_UNLOCKED();
+
+   unp_detach(unp);
+
+   return (0);
+}
+
 void
 unp_detach(struct unpcb *unp)
 {
diff --git net/pfkeyv2.c net/pfkeyv2.c
index ac593e4d5f1..f52ec9d3692 100644
--- net/pfkeyv2.c
+++ net/pfkeyv2.c
@@ -159,7 +159,7 @@ static int npromisc = 0;
 void pfkey_init(void);
 
 int pfkeyv2_attach(struct socket *, int);
-int pfkeyv2_detach(struct socket *, struct proc *);
+int pfkeyv2_detach(struct socket *);
 int pfkeyv2_usrreq(struct socket *, int, struct mbuf *, struct mbuf *,
 struct mbuf *, struct proc *);
 int pfkeyv2_output(struct mbuf *, struct socket *, struct sockaddr *,
@@ -192,6 +192,7 @@ static struct protosw pfkeysw[] = {
   .pr_output= pfkeyv2_output,
   .pr_usrreq= pfkeyv2_usrreq,
   .pr_attach= pfkeyv2_attach,
+  .pr_detach= pfkeyv2_detach,
   .pr_sysctl= pfkeyv2_sysctl,
 }
 };
@@ -255,7 +256,7 @@ pfkeyv2_attach(struct socket *so, int proto)
  * Close a PF_KEYv2 socket.
  */
 int
-pfkeyv2_detach(struct socket *so, struct proc *p)
+pfkeyv2_detach(struct socket *so)
 {
struct keycb *kp;
 
@@ -276,7 +277,7 @@ pfkeyv2_detach(struct socket *so, struct proc *p)
mtx_leave(_mtx);
}
 
-   raw_detach(>rcb);
+   raw_do_detach(>rcb);
return (0);
 }
 
@@ -284,12 +285,7 @@ int
 pfkeyv2_usrreq(struct socket *so, int req, struct mbuf *mbuf,
 struct mbuf *nam, struct mbuf *control, struct proc *p)
 {
-   switch (req) {
-   case PRU_DETACH:
-   return (pfkeyv2_detach(so, p));
-   default:
-   return (raw_usrreq(so, req, mbuf, nam, control, p));
-   }
+   return (raw_usrreq(so, req, mbuf, nam, control, p));
 }
 
 int
diff --git net/raw_cb.c net/raw_cb.c
index e77da50b039..55c3fd21d2f 100644
--- net/raw_cb.c
+++ net/raw_cb.c
@@ -76,12 +76,27 @@ raw_attach(struct socket *so, int proto)
return (0);
 }
 
+int
+raw_detach(struct socket *so)
+{
+   struct rawcb *rp = sotorawcb(so);
+
+   soassertlocked(so);
+
+   if (rp == NULL)
+   return (EINVAL);
+
+   raw_do_detach(rp);
+
+   return (0);
+}
+
 /*
  * Detach the raw connection block and discard
  * socket resources.
  */
 void
-raw_detach(struct rawcb *rp)
+raw_do_detach(struct rawcb *rp)
 {
struct socket *so = rp->rcb_socket;
 
@@ -97,5 +112,5 @@ void
 raw_disconnect(struct rawcb *rp)
 {
if (rp->rcb_socket->so_state & SS_NOFDREF)
-   raw_detach(rp);
+   raw_do_detach(rp);
 }
diff --git net/raw_cb.h net/raw_cb.h
index 00c3d30f98a..a3e964d807f 100644
--- net/raw_cb.h
+++ net/raw_cb.h
@@ -56,7 +56,8