From 69690c9b3a032b5bbba1b3bc6c1144f5d52ee702 Mon Sep 17 00:00:00 2001 From: mvs Date: Sat, 13 Aug 2022 21:01:46 +0000 Subject: [PATCH] Introduce the pru_*() wrappers for corresponding (*pr_usrreq)() calls. This is helpful for the following (*pr_usrreq)() split to multiple handlers. But right now this makes code more readable. Also add '#ifndef _SYS_SOCKETVAR_H_' to sys/socketvar.h. This prevents the collisions when both sys/protosw.h and sys/socketvar.h are included together. Both 'socket' and 'protosw' structures are required to be defined before pru_*() wrappers, so we need to include sys/socketvar.h to sys/protosw.h. ok bluhm@ --- sys/kern/sys_socket.c | 8 +-- sys/kern/uipc_socket.c | 63 +++++++----------- sys/kern/uipc_socket2.c | 4 +- sys/kern/uipc_syscalls.c | 6 +- sys/net/if.c | 6 +- sys/nfs/nfs_socket.c | 8 +-- sys/sys/protosw.h | 135 ++++++++++++++++++++++++++++++++++++++- sys/sys/socketvar.h | 7 +- 8 files changed, 175 insertions(+), 62 deletions(-) diff --git a/sys/kern/sys_socket.c b/sys/kern/sys_socket.c index 836485795e8..6064d8c5c93 100644 --- a/sys/kern/sys_socket.c +++ b/sys/kern/sys_socket.c @@ -1,4 +1,4 @@ -/* $OpenBSD: sys_socket.c,v 1.51 2022/06/20 01:39:44 visa Exp $ */ +/* $OpenBSD: sys_socket.c,v 1.52 2022/08/13 21:01:46 mvs Exp $ */ /* $NetBSD: sys_socket.c,v 1.13 1995/08/12 23:59:09 mycroft Exp $ */ /* @@ -138,8 +138,7 @@ soo_ioctl(struct file *fp, u_long cmd, caddr_t data, struct proc *p) if (IOCGROUP(cmd) == 'r') return (EOPNOTSUPP); KERNEL_LOCK(); - error = ((*so->so_proto->pr_usrreq)(so, PRU_CONTROL, - (struct mbuf *)cmd, (struct mbuf *)data, NULL, p)); + error = pru_control(so, cmd, data, NULL, p); KERNEL_UNLOCK(); break; } @@ -161,8 +160,7 @@ soo_stat(struct file *fp, struct stat *ub, struct proc *p) ub->st_mode |= S_IWUSR | S_IWGRP | S_IWOTH; ub->st_uid = so->so_euid; ub->st_gid = so->so_egid; - (void) ((*so->so_proto->pr_usrreq)(so, PRU_SENSE, - (struct mbuf *)ub, NULL, NULL, p)); + (void)pru_sense(so, ub); sounlock(so); return (0); } diff --git a/sys/kern/uipc_socket.c b/sys/kern/uipc_socket.c index e2eb7ea0892..c8d10b5c531 100644 --- a/sys/kern/uipc_socket.c +++ b/sys/kern/uipc_socket.c @@ -1,4 +1,4 @@ -/* $OpenBSD: uipc_socket.c,v 1.280 2022/07/25 07:28:22 visa Exp $ */ +/* $OpenBSD: uipc_socket.c,v 1.281 2022/08/13 21:01:46 mvs Exp $ */ /* $NetBSD: uipc_socket.c,v 1.21 1996/02/04 02:17:52 christos Exp $ */ /* @@ -195,7 +195,7 @@ socreate(int dom, struct socket **aso, int type, int proto) so->so_rcv.sb_timeo_nsecs = INFSLP; solock(so); - error = (*prp->pr_attach)(so, proto); + error = pru_attach(so, proto); if (error) { so->so_state |= SS_NOFDREF; /* sofree() calls sounlock(). */ @@ -210,12 +210,8 @@ socreate(int dom, struct socket **aso, int type, int proto) int sobind(struct socket *so, struct mbuf *nam, struct proc *p) { - int error; - soassertlocked(so); - - error = (*so->so_proto->pr_usrreq)(so, PRU_BIND, NULL, nam, NULL, p); - return (error); + return pru_bind(so, nam, p); } int @@ -231,8 +227,7 @@ solisten(struct socket *so, int backlog) if (isspliced(so) || issplicedback(so)) return (EOPNOTSUPP); #endif /* SOCKET_SPLICE */ - error = (*so->so_proto->pr_usrreq)(so, PRU_LISTEN, NULL, NULL, NULL, - curproc); + error = pru_listen(so); if (error) return (error); if (TAILQ_FIRST(&so->so_q) == NULL) @@ -392,8 +387,7 @@ soclose(struct socket *so, int flags) drop: if (so->so_pcb) { int error2; - KASSERT(so->so_proto->pr_detach); - error2 = (*so->so_proto->pr_detach)(so); + error2 = pru_detach(so); if (error == 0) error = error2; } @@ -444,8 +438,7 @@ soabort(struct socket *so) { soassertlocked(so); - return (*so->so_proto->pr_usrreq)(so, PRU_ABORT, NULL, NULL, NULL, - curproc); + return pru_abort(so); } int @@ -460,8 +453,7 @@ soaccept(struct socket *so, struct mbuf *nam) so->so_state &= ~SS_NOFDREF; if ((so->so_state & SS_ISDISCONNECTED) == 0 || (so->so_proto->pr_flags & PR_ABRTACPTDIS) == 0) - error = (*so->so_proto->pr_usrreq)(so, PRU_ACCEPT, NULL, - nam, NULL, curproc); + error = pru_accept(so, nam); else error = ECONNABORTED; return (error); @@ -487,8 +479,7 @@ soconnect(struct socket *so, struct mbuf *nam) (error = sodisconnect(so)))) error = EISCONN; else - error = (*so->so_proto->pr_usrreq)(so, PRU_CONNECT, - NULL, nam, NULL, curproc); + error = pru_connect(so, nam); return (error); } @@ -502,8 +493,7 @@ soconnect2(struct socket *so1, struct socket *so2) else solock(so1); - error = (*so1->so_proto->pr_usrreq)(so1, PRU_CONNECT2, NULL, - (struct mbuf *)so2, NULL, curproc); + error = pru_connect2(so1, so2); if (persocket) sounlock(so2); @@ -522,8 +512,7 @@ sodisconnect(struct socket *so) return (ENOTCONN); if (so->so_state & SS_ISDISCONNECTING) return (EALREADY); - error = (*so->so_proto->pr_usrreq)(so, PRU_DISCONNECT, NULL, NULL, - NULL, curproc); + error = pru_disconnect(so); return (error); } @@ -654,9 +643,10 @@ restart: so->so_state &= ~SS_ISSENDING; if (top && so->so_options & SO_ZEROIZE) top->m_flags |= M_ZEROIZE; - error = (*so->so_proto->pr_usrreq)(so, - (flags & MSG_OOB) ? PRU_SENDOOB : PRU_SEND, - top, addr, control, curproc); + if (flags & MSG_OOB) + error = pru_sendoob(so, top, addr, control); + else + error = pru_send(so, top, addr, control); clen = 0; control = NULL; top = NULL; @@ -819,8 +809,7 @@ soreceive(struct socket *so, struct mbuf **paddr, struct uio *uio, if (flags & MSG_OOB) { m = m_get(M_WAIT, MT_DATA); solock(so); - error = (*pr->pr_usrreq)(so, PRU_RCVOOB, m, - (struct mbuf *)(long)(flags & MSG_PEEK), NULL, curproc); + error = pru_rcvoob(so, m, flags & MSG_PEEK); sounlock(so); if (error) goto bad; @@ -1170,8 +1159,7 @@ dontblock: SBLASTRECORDCHK(&so->so_rcv, "soreceive 4"); SBLASTMBUFCHK(&so->so_rcv, "soreceive 4"); if (pr->pr_flags & PR_WANTRCVD && so->so_pcb) - (*pr->pr_usrreq)(so, PRU_RCVD, NULL, - (struct mbuf *)(long)flags, NULL, curproc); + pru_rcvd(so, flags); } if (orig_resid == uio->uio_resid && orig_resid && (flags & MSG_EOR) == 0 && (so->so_state & SS_CANTRCVMORE) == 0) { @@ -1193,7 +1181,6 @@ release: int soshutdown(struct socket *so, int how) { - const struct protosw *pr = so->so_proto; int error = 0; solock(so); @@ -1205,8 +1192,7 @@ soshutdown(struct socket *so, int how) sorflush(so); /* FALLTHROUGH */ case SHUT_WR: - error = (*pr->pr_usrreq)(so, PRU_SHUTDOWN, NULL, NULL, NULL, - curproc); + error = pru_shutdown(so); break; default: error = EINVAL; @@ -1538,8 +1524,7 @@ somove(struct socket *so, int wait) if (m == NULL) { sbdroprecord(so, &so->so_rcv); if (so->so_proto->pr_flags & PR_WANTRCVD && so->so_pcb) - (so->so_proto->pr_usrreq)(so, PRU_RCVD, NULL, - NULL, NULL, NULL); + pru_rcvd(so, 0); goto nextpkt; } @@ -1645,8 +1630,7 @@ somove(struct socket *so, int wait) /* Send window update to source peer as receive buffer has changed. */ if (so->so_proto->pr_flags & PR_WANTRCVD && so->so_pcb) - (so->so_proto->pr_usrreq)(so, PRU_RCVD, NULL, - NULL, NULL, NULL); + pru_rcvd(so, 0); /* Receive buffer did shrink by len bytes, adjust oob. */ state = so->so_state; @@ -1674,8 +1658,7 @@ somove(struct socket *so, int wait) } else if (oobmark) { o = m_split(m, oobmark, wait); if (o) { - error = (*sosp->so_proto->pr_usrreq)(sosp, - PRU_SEND, m, NULL, NULL, NULL); + error = pru_send(sosp, m, NULL, NULL); if (error) { if (sosp->so_state & SS_CANTSENDMORE) error = EPIPE; @@ -1692,8 +1675,7 @@ somove(struct socket *so, int wait) if (o) { o->m_len = 1; *mtod(o, caddr_t) = *mtod(m, caddr_t); - error = (*sosp->so_proto->pr_usrreq)(sosp, PRU_SENDOOB, - o, NULL, NULL, NULL); + error = pru_sendoob(sosp, o, NULL, NULL); if (error) { if (sosp->so_state & SS_CANTSENDMORE) error = EPIPE; @@ -1714,8 +1696,7 @@ somove(struct socket *so, int wait) /* Append all remaining data to drain socket. */ if (so->so_rcv.sb_cc == 0 || maxreached) sosp->so_state &= ~SS_ISSENDING; - error = (*sosp->so_proto->pr_usrreq)(sosp, PRU_SEND, m, NULL, NULL, - NULL); + error = pru_send(sosp, m, NULL, NULL); if (error) { if (sosp->so_state & SS_CANTSENDMORE) error = EPIPE; diff --git a/sys/kern/uipc_socket2.c b/sys/kern/uipc_socket2.c index a70dccfe96d..21e3a965038 100644 --- a/sys/kern/uipc_socket2.c +++ b/sys/kern/uipc_socket2.c @@ -1,4 +1,4 @@ -/* $OpenBSD: uipc_socket2.c,v 1.126 2022/07/25 07:28:22 visa Exp $ */ +/* $OpenBSD: uipc_socket2.c,v 1.127 2022/08/13 21:01:46 mvs Exp $ */ /* $NetBSD: uipc_socket2.c,v 1.11 1996/02/04 02:17:55 christos Exp $ */ /* @@ -238,7 +238,7 @@ sonewconn(struct socket *head, int connstatus) sounlock(head); } - error = (*so->so_proto->pr_attach)(so, 0); + error = pru_attach(so, 0); if (persocket) { sounlock(so); diff --git a/sys/kern/uipc_syscalls.c b/sys/kern/uipc_syscalls.c index 617026e0193..6b72be16c58 100644 --- a/sys/kern/uipc_syscalls.c +++ b/sys/kern/uipc_syscalls.c @@ -1,4 +1,4 @@ -/* $OpenBSD: uipc_syscalls.c,v 1.199 2022/07/18 04:42:37 deraadt Exp $ */ +/* $OpenBSD: uipc_syscalls.c,v 1.200 2022/08/13 21:01:46 mvs Exp $ */ /* $NetBSD: uipc_syscalls.c,v 1.19 1996/02/09 19:00:48 christos Exp $ */ /* @@ -1100,7 +1100,7 @@ sys_getsockname(struct proc *p, void *v, register_t *retval) } m = m_getclr(M_WAIT, MT_SONAME); solock(so); - error = (*so->so_proto->pr_usrreq)(so, PRU_SOCKADDR, NULL, m, NULL, p); + error = pru_sockaddr(so, m); sounlock(so); if (error) goto bad; @@ -1147,7 +1147,7 @@ sys_getpeername(struct proc *p, void *v, register_t *retval) goto bad; m = m_getclr(M_WAIT, MT_SONAME); solock(so); - error = (*so->so_proto->pr_usrreq)(so, PRU_PEERADDR, NULL, m, NULL, p); + error = pru_peeraddr(so, m); sounlock(so); if (error) goto bad; diff --git a/sys/net/if.c b/sys/net/if.c index cdb43e83d15..c7045347c12 100644 --- a/sys/net/if.c +++ b/sys/net/if.c @@ -1,4 +1,4 @@ -/* $OpenBSD: if.c,v 1.662 2022/08/06 15:57:58 bluhm Exp $ */ +/* $OpenBSD: if.c,v 1.663 2022/08/13 21:01:46 mvs Exp $ */ /* $NetBSD: if.c,v 1.35 1996/05/07 05:26:04 thorpej Exp $ */ /* @@ -2360,9 +2360,7 @@ forceup: break; /* FALLTHROUGH */ default: - error = ((*so->so_proto->pr_usrreq)(so, PRU_CONTROL, - (struct mbuf *) cmd, (struct mbuf *) data, - (struct mbuf *) ifp, p)); + error = pru_control(so, cmd, data, ifp, p); if (error != EOPNOTSUPP) break; switch (cmd) { diff --git a/sys/nfs/nfs_socket.c b/sys/nfs/nfs_socket.c index 44f99d60a8f..5488a501040 100644 --- a/sys/nfs/nfs_socket.c +++ b/sys/nfs/nfs_socket.c @@ -1,4 +1,4 @@ -/* $OpenBSD: nfs_socket.c,v 1.142 2022/06/06 14:45:41 claudio Exp $ */ +/* $OpenBSD: nfs_socket.c,v 1.143 2022/08/13 21:01:46 mvs Exp $ */ /* $NetBSD: nfs_socket.c,v 1.27 1996/04/15 20:20:00 thorpej Exp $ */ /* @@ -1177,11 +1177,9 @@ nfs_timer(void *arg) nmp->nm_sent < nmp->nm_cwnd) && (m = m_copym(rep->r_mreq, 0, M_COPYALL, M_DONTWAIT))){ if ((nmp->nm_flag & NFSMNT_NOCONN) == 0) - error = (*so->so_proto->pr_usrreq)(so, PRU_SEND, - m, NULL, NULL, curproc); + error = pru_send(so, m, NULL, NULL); else - error = (*so->so_proto->pr_usrreq)(so, PRU_SEND, - m, nmp->nm_nam, NULL, curproc); + error = pru_send(so, m, nmp->nm_nam, NULL); if (error) { if (NFSIGNORE_SOERROR(nmp->nm_soflags, error)) so->so_error = 0; diff --git a/sys/sys/protosw.h b/sys/sys/protosw.h index a494fe3edd7..af12315caaf 100644 --- a/sys/sys/protosw.h +++ b/sys/sys/protosw.h @@ -1,4 +1,4 @@ -/* $OpenBSD: protosw.h,v 1.35 2022/02/25 23:51:04 guenther Exp $ */ +/* $OpenBSD: protosw.h,v 1.36 2022/08/13 21:01:46 mvs Exp $ */ /* $NetBSD: protosw.h,v 1.10 1996/04/09 20:55:32 cgd Exp $ */ /*- @@ -227,6 +227,11 @@ char *prcorequests[] = { #endif #ifdef _KERNEL + +#include +#include + +struct ifnet; struct sockaddr; const struct protosw *pffindproto(int, int, int); const struct protosw *pffindtype(int, int); @@ -240,4 +245,132 @@ extern u_char ip6_protox[]; extern const struct protosw inet6sw[]; #endif /* INET6 */ +static inline int +pru_attach(struct socket *so, int proto) +{ + return (*so->so_proto->pr_attach)(so, proto); +} + +static inline int +pru_detach(struct socket *so) +{ + return (*so->so_proto->pr_detach)(so); +} + +static inline int +pru_bind(struct socket *so, struct mbuf *nam, struct proc *p) +{ + return (*so->so_proto->pr_usrreq)(so, + PRU_BIND, NULL, nam, NULL, p); +} + +static inline int +pru_listen(struct socket *so) +{ + return (*so->so_proto->pr_usrreq)(so, + PRU_LISTEN, NULL, NULL, NULL, curproc); +} + +static inline int +pru_connect(struct socket *so, struct mbuf *nam) +{ + return (*so->so_proto->pr_usrreq)(so, + PRU_CONNECT, NULL, nam, NULL, curproc); +} + +static inline int +pru_accept(struct socket *so, struct mbuf *nam) +{ + return (*so->so_proto->pr_usrreq)(so, + PRU_ACCEPT, NULL, nam, NULL, curproc); +} + +static inline int +pru_disconnect(struct socket *so) +{ + return (*so->so_proto->pr_usrreq)(so, + PRU_DISCONNECT, NULL, NULL, NULL, curproc); +} + +static inline int +pru_shutdown(struct socket *so) +{ + return (*so->so_proto->pr_usrreq)(so, + PRU_SHUTDOWN, NULL, NULL, NULL, curproc); +} + +static inline int +pru_rcvd(struct socket *so, int flags) +{ + return (*so->so_proto->pr_usrreq)(so, + PRU_RCVD, NULL, (struct mbuf *)(long)flags, NULL, curproc); +} + +static inline int +pru_send(struct socket *so, struct mbuf *top, struct mbuf *addr, + struct mbuf *control) +{ + return (*so->so_proto->pr_usrreq)(so, + PRU_SEND, top, addr, control, curproc); +} + +static inline int +pru_abort(struct socket *so) +{ + return (*so->so_proto->pr_usrreq)(so, + PRU_ABORT, NULL, NULL, NULL, curproc); +} + +static inline int +pru_control(struct socket *so, u_long cmd, caddr_t data, + struct ifnet *ifp, struct proc *p) +{ + return (*so->so_proto->pr_usrreq)(so, + PRU_CONTROL, (struct mbuf *)cmd, (struct mbuf *)data, + (struct mbuf *)ifp, p); +} + +static inline int +pru_sense(struct socket *so, struct stat *ub) +{ + return (*so->so_proto->pr_usrreq)(so, + PRU_SENSE, (struct mbuf *)ub, NULL, NULL, curproc); +} + +static inline int +pru_rcvoob(struct socket *so, struct mbuf *m, int flags) +{ + return (*so->so_proto->pr_usrreq)(so, + PRU_RCVOOB, m, (struct mbuf *)(long)flags, NULL, curproc); +} + +static inline int +pru_sendoob(struct socket *so, struct mbuf *top, struct mbuf *addr, + struct mbuf *control) +{ + return (*so->so_proto->pr_usrreq)(so, + PRU_SENDOOB, top, addr, control, curproc); +} + +static inline int +pru_sockaddr(struct socket *so, struct mbuf *addr) +{ + return (*so->so_proto->pr_usrreq)(so, + PRU_SOCKADDR, NULL, addr, NULL, curproc); +} + +static inline int +pru_peeraddr(struct socket *so, struct mbuf *addr) +{ + return (*so->so_proto->pr_usrreq)(so, + PRU_PEERADDR, NULL, addr, NULL, curproc); +} + +static inline int +pru_connect2(struct socket *so1, struct socket *so2) +{ + return (*so1->so_proto->pr_usrreq)(so1, + PRU_CONNECT2, NULL, (struct mbuf *)so2, NULL, curproc); +} + #endif diff --git a/sys/sys/socketvar.h b/sys/sys/socketvar.h index 6a3b7d4ad9e..fa96d7cc0b9 100644 --- a/sys/sys/socketvar.h +++ b/sys/sys/socketvar.h @@ -1,4 +1,4 @@ -/* $OpenBSD: socketvar.h,v 1.106 2022/07/15 17:20:24 deraadt Exp $ */ +/* $OpenBSD: socketvar.h,v 1.107 2022/08/13 21:01:46 mvs Exp $ */ /* $NetBSD: socketvar.h,v 1.18 1996/02/09 18:25:38 christos Exp $ */ /*- @@ -32,6 +32,9 @@ * @(#)socketvar.h 8.1 (Berkeley) 6/2/93 */ +#ifndef _SYS_SOCKETVAR_H_ +#define _SYS_SOCKETVAR_H_ + #include /* for struct selinfo */ #include #include /* for struct sigio_ref */ @@ -370,3 +373,5 @@ void sbcheck(struct socket *, struct sockbuf *); #endif /* SOCKBUF_DEBUG */ #endif /* _KERNEL */ + +#endif /* _SYS_SOCKETVAR_H_ */ -- 2.20.1