From: mvs Date: Sat, 20 Aug 2022 23:48:57 +0000 (+0000) Subject: Move PRU_BIND request to (*pru_bind)() handler. X-Git-Url: http://artulab.com/gitweb/?a=commitdiff_plain;h=121fc5cf6810c87c35842f48bf7231b89876684a;p=openbsd Move PRU_BIND request to (*pru_bind)() handler. For the protocols which don't support request, leave handler NULL. Do the NULL check within corresponding pru_() wrapper and return EOPNOTSUPP in such case. This will be done for all upcoming user request handlers. ok bluhm@ guenther@ --- diff --git a/sys/kern/uipc_usrreq.c b/sys/kern/uipc_usrreq.c index 55a2fc64bac..880e83391d7 100644 --- a/sys/kern/uipc_usrreq.c +++ b/sys/kern/uipc_usrreq.c @@ -1,4 +1,4 @@ -/* $OpenBSD: uipc_usrreq.c,v 1.168 2022/08/15 09:11:38 mvs Exp $ */ +/* $OpenBSD: uipc_usrreq.c,v 1.169 2022/08/20 23:48:57 mvs Exp $ */ /* $NetBSD: uipc_usrreq.c,v 1.18 1996/02/09 19:00:50 christos Exp $ */ /* @@ -130,6 +130,7 @@ const struct pr_usrreqs uipc_usrreqs = { .pru_usrreq = uipc_usrreq, .pru_attach = uipc_attach, .pru_detach = uipc_detach, + .pru_bind = uipc_bind, }; void @@ -222,10 +223,6 @@ uipc_usrreq(struct socket *so, int req, struct mbuf *m, struct mbuf *nam, switch (req) { - case PRU_BIND: - error = unp_bind(unp, nam, p); - break; - case PRU_LISTEN: if (unp->unp_vnode == NULL) error = EINVAL; @@ -537,6 +534,14 @@ uipc_detach(struct socket *so) return (0); } +int +uipc_bind(struct socket *so, struct mbuf *nam, struct proc *p) +{ + struct unpcb *unp = sotounpcb(so); + + return unp_bind(unp, nam, p); +} + int uipc_sysctl(int *name, u_int namelen, void *oldp, size_t *oldlenp, void *newp, size_t newlen) diff --git a/sys/net/pfkeyv2.c b/sys/net/pfkeyv2.c index ab2571e0acb..00e12d2041c 100644 --- a/sys/net/pfkeyv2.c +++ b/sys/net/pfkeyv2.c @@ -1,4 +1,4 @@ -/* $OpenBSD: pfkeyv2.c,v 1.235 2022/08/15 09:11:38 mvs Exp $ */ +/* $OpenBSD: pfkeyv2.c,v 1.236 2022/08/20 23:48:57 mvs Exp $ */ /* * @(#)COPYRIGHT 1.1 (NRL) 17 January 1995 @@ -358,7 +358,6 @@ pfkeyv2_usrreq(struct socket *so, int req, struct mbuf *m, switch (req) { /* no connect, bind, accept. Socket is connected from the start */ case PRU_CONNECT: - case PRU_BIND: case PRU_CONNECT2: case PRU_LISTEN: case PRU_ACCEPT: diff --git a/sys/net/rtsock.c b/sys/net/rtsock.c index abc76b7d069..647b1e8326a 100644 --- a/sys/net/rtsock.c +++ b/sys/net/rtsock.c @@ -1,4 +1,4 @@ -/* $OpenBSD: rtsock.c,v 1.335 2022/08/15 09:11:38 mvs Exp $ */ +/* $OpenBSD: rtsock.c,v 1.336 2022/08/20 23:48:58 mvs Exp $ */ /* $NetBSD: rtsock.c,v 1.18 1996/03/29 00:32:10 cgd Exp $ */ /* @@ -234,7 +234,6 @@ route_usrreq(struct socket *so, int req, struct mbuf *m, struct mbuf *nam, switch (req) { /* no connect, bind, accept. Socket is connected from the start */ case PRU_CONNECT: - case PRU_BIND: case PRU_CONNECT2: case PRU_LISTEN: case PRU_ACCEPT: diff --git a/sys/netinet/ip_divert.c b/sys/netinet/ip_divert.c index f5825f554c3..b843fc96bf4 100644 --- a/sys/netinet/ip_divert.c +++ b/sys/netinet/ip_divert.c @@ -1,4 +1,4 @@ -/* $OpenBSD: ip_divert.c,v 1.69 2022/08/15 09:11:39 mvs Exp $ */ +/* $OpenBSD: ip_divert.c,v 1.70 2022/08/20 23:48:58 mvs Exp $ */ /* * Copyright (c) 2009 Michele Marchetto @@ -66,6 +66,7 @@ const struct pr_usrreqs divert_usrreqs = { .pru_usrreq = divert_usrreq, .pru_attach = divert_attach, .pru_detach = divert_detach, + .pru_bind = divert_bind, }; int divbhashsize = DIVERTHASHSIZE; @@ -274,10 +275,6 @@ divert_usrreq(struct socket *so, int req, struct mbuf *m, struct mbuf *addr, } switch (req) { - case PRU_BIND: - error = in_pcbbind(inp, addr, p); - break; - case PRU_SHUTDOWN: socantsendmore(so); break; @@ -364,6 +361,15 @@ divert_detach(struct socket *so) return (0); } +int +divert_bind(struct socket *so, struct mbuf *addr, struct proc *p) +{ + struct inpcb *inp = sotoinpcb(so); + + soassertlocked(so); + return in_pcbbind(inp, addr, p); +} + int divert_sysctl_divstat(void *oldp, size_t *oldlenp, void *newp) { diff --git a/sys/netinet/ip_divert.h b/sys/netinet/ip_divert.h index f76dd4d1e23..09b7de4b54e 100644 --- a/sys/netinet/ip_divert.h +++ b/sys/netinet/ip_divert.h @@ -1,4 +1,4 @@ -/* $OpenBSD: ip_divert.h,v 1.16 2022/08/15 09:11:39 mvs Exp $ */ +/* $OpenBSD: ip_divert.h,v 1.17 2022/08/20 23:48:58 mvs Exp $ */ /* * Copyright (c) 2009 Michele Marchetto @@ -74,5 +74,6 @@ int divert_usrreq(struct socket *, int, struct mbuf *, struct mbuf *, struct mbuf *, struct proc *); int divert_attach(struct socket *, int); int divert_detach(struct socket *); +int divert_bind(struct socket *, struct mbuf *, struct proc *); #endif /* _KERNEL */ #endif /* _IP_DIVERT_H_ */ diff --git a/sys/netinet/ip_gre.c b/sys/netinet/ip_gre.c index ea73003f68d..78dcb701ca9 100644 --- a/sys/netinet/ip_gre.c +++ b/sys/netinet/ip_gre.c @@ -1,4 +1,4 @@ -/* $OpenBSD: ip_gre.c,v 1.75 2022/08/15 09:11:39 mvs Exp $ */ +/* $OpenBSD: ip_gre.c,v 1.76 2022/08/20 23:48:58 mvs Exp $ */ /* $NetBSD: ip_gre.c,v 1.9 1999/10/25 19:18:11 drochner Exp $ */ /* @@ -65,6 +65,7 @@ const struct pr_usrreqs gre_usrreqs = { .pru_usrreq = gre_usrreq, .pru_attach = rip_attach, .pru_detach = rip_detach, + .pru_bind = rip_bind, }; int diff --git a/sys/netinet/ip_var.h b/sys/netinet/ip_var.h index 68adea349d0..01e6d8a764a 100644 --- a/sys/netinet/ip_var.h +++ b/sys/netinet/ip_var.h @@ -1,4 +1,4 @@ -/* $OpenBSD: ip_var.h,v 1.97 2022/08/15 09:11:39 mvs Exp $ */ +/* $OpenBSD: ip_var.h,v 1.98 2022/08/20 23:48:58 mvs Exp $ */ /* $NetBSD: ip_var.h,v 1.16 1996/02/13 23:43:20 christos Exp $ */ /* @@ -260,6 +260,7 @@ int rip_usrreq(struct socket *, int, struct mbuf *, struct mbuf *, struct mbuf *, struct proc *); int rip_attach(struct socket *, int); int rip_detach(struct socket *); +int rip_bind(struct socket *so, struct mbuf *, struct proc *); #ifdef MROUTING extern struct socket *ip_mrouter[]; /* multicast routing daemon */ #endif diff --git a/sys/netinet/raw_ip.c b/sys/netinet/raw_ip.c index 8758f3fa466..e444363025a 100644 --- a/sys/netinet/raw_ip.c +++ b/sys/netinet/raw_ip.c @@ -1,4 +1,4 @@ -/* $OpenBSD: raw_ip.c,v 1.130 2022/08/15 09:11:39 mvs Exp $ */ +/* $OpenBSD: raw_ip.c,v 1.131 2022/08/20 23:48:58 mvs Exp $ */ /* $NetBSD: raw_ip.c,v 1.25 1996/02/18 18:58:33 christos Exp $ */ /* @@ -107,6 +107,7 @@ const struct pr_usrreqs rip_usrreqs = { .pru_usrreq = rip_usrreq, .pru_attach = rip_attach, .pru_detach = rip_detach, + .pru_bind = rip_bind, }; /* @@ -485,23 +486,6 @@ rip_usrreq(struct socket *so, int req, struct mbuf *m, struct mbuf *nam, in_pcbdetach(inp); break; - case PRU_BIND: - { - struct sockaddr_in *addr; - - if ((error = in_nam2sin(nam, &addr))) - break; - if (!((so->so_options & SO_BINDANY) || - addr->sin_addr.s_addr == INADDR_ANY || - addr->sin_addr.s_addr == INADDR_BROADCAST || - in_broadcast(addr->sin_addr, inp->inp_rtableid) || - ifa_ifwithaddr(sintosa(addr), inp->inp_rtableid))) { - error = EADDRNOTAVAIL; - break; - } - inp->inp_laddr = addr->sin_addr; - break; - } case PRU_CONNECT: { struct sockaddr_in *addr; @@ -637,3 +621,27 @@ rip_detach(struct socket *so) return (0); } + +int +rip_bind(struct socket *so, struct mbuf *nam, struct proc *p) +{ + struct inpcb *inp = sotoinpcb(so); + struct sockaddr_in *addr; + int error; + + soassertlocked(so); + + if ((error = in_nam2sin(nam, &addr))) + return (error); + + if (!((so->so_options & SO_BINDANY) || + addr->sin_addr.s_addr == INADDR_ANY || + addr->sin_addr.s_addr == INADDR_BROADCAST || + in_broadcast(addr->sin_addr, inp->inp_rtableid) || + ifa_ifwithaddr(sintosa(addr), inp->inp_rtableid))) + return (EADDRNOTAVAIL); + + inp->inp_laddr = addr->sin_addr; + + return (0); +} diff --git a/sys/netinet/tcp_usrreq.c b/sys/netinet/tcp_usrreq.c index fa3da17513d..88aa0d26ed7 100644 --- a/sys/netinet/tcp_usrreq.c +++ b/sys/netinet/tcp_usrreq.c @@ -1,4 +1,4 @@ -/* $OpenBSD: tcp_usrreq.c,v 1.188 2022/08/15 14:44:18 mvs Exp $ */ +/* $OpenBSD: tcp_usrreq.c,v 1.189 2022/08/20 23:48:58 mvs Exp $ */ /* $NetBSD: tcp_usrreq.c,v 1.20 1996/02/13 23:44:16 christos Exp $ */ /* @@ -115,6 +115,7 @@ const struct pr_usrreqs tcp_usrreqs = { .pru_usrreq = tcp_usrreq, .pru_attach = tcp_attach, .pru_detach = tcp_detach, + .pru_bind = tcp_bind, }; static int pr_slowhz = PR_SLOWHZ; @@ -211,13 +212,6 @@ tcp_usrreq(struct socket *so, int req, struct mbuf *m, struct mbuf *nam, switch (req) { - /* - * Give the socket an address. - */ - case PRU_BIND: - error = in_pcbbind(inp, nam, p); - break; - /* * Prepare to accept connections. */ @@ -780,6 +774,32 @@ tcp_detach(struct socket *so) return (error); } +/* + * Give the socket an address. + */ +int +tcp_bind(struct socket *so, struct mbuf *nam, struct proc *p) +{ + struct inpcb *inp; + struct tcpcb *tp; + int error; + short ostate; + + soassertlocked(so); + + if ((error = tcp_sogetpcb(so, &inp, &tp))) + return (error); + + if (so->so_options & SO_DEBUG) + ostate = tp->t_state; + + error = in_pcbbind(inp, nam, p); + + if (so->so_options & SO_DEBUG) + tcp_trace(TA_USER, ostate, tp, tp, NULL, PRU_BIND, 0); + return (error); +} + /* * Initiate (or continue) disconnect. * If embryonic state, just send reset (once). diff --git a/sys/netinet/tcp_var.h b/sys/netinet/tcp_var.h index 97213c6e0c7..7ffa048b459 100644 --- a/sys/netinet/tcp_var.h +++ b/sys/netinet/tcp_var.h @@ -1,4 +1,4 @@ -/* $OpenBSD: tcp_var.h,v 1.141 2022/08/15 09:11:39 mvs Exp $ */ +/* $OpenBSD: tcp_var.h,v 1.142 2022/08/20 23:48:58 mvs Exp $ */ /* $NetBSD: tcp_var.h,v 1.17 1996/02/13 23:44:24 christos Exp $ */ /* @@ -714,6 +714,7 @@ int tcp_usrreq(struct socket *, int, struct mbuf *, struct mbuf *, struct mbuf *, struct proc *); int tcp_attach(struct socket *, int); int tcp_detach(struct socket *); +int tcp_bind(struct socket *, struct mbuf *, struct proc *); void tcp_xmit_timer(struct tcpcb *, int); void tcpdropoldhalfopen(struct tcpcb *, u_int16_t); void tcp_sack_option(struct tcpcb *,struct tcphdr *,u_char *,int); diff --git a/sys/netinet/udp_usrreq.c b/sys/netinet/udp_usrreq.c index 0f7dcac974a..06a1252ae72 100644 --- a/sys/netinet/udp_usrreq.c +++ b/sys/netinet/udp_usrreq.c @@ -1,4 +1,4 @@ -/* $OpenBSD: udp_usrreq.c,v 1.282 2022/08/15 09:11:39 mvs Exp $ */ +/* $OpenBSD: udp_usrreq.c,v 1.283 2022/08/20 23:48:58 mvs Exp $ */ /* $NetBSD: udp_usrreq.c,v 1.28 1996/03/16 23:54:03 christos Exp $ */ /* @@ -126,6 +126,7 @@ const struct pr_usrreqs udp_usrreqs = { .pru_usrreq = udp_usrreq, .pru_attach = udp_attach, .pru_detach = udp_detach, + .pru_bind = udp_bind, }; const struct sysctl_bounded_args udpctl_vars[] = { @@ -1074,10 +1075,6 @@ udp_usrreq(struct socket *so, int req, struct mbuf *m, struct mbuf *addr, */ switch (req) { - case PRU_BIND: - error = in_pcbbind(inp, addr, p); - break; - case PRU_LISTEN: error = EOPNOTSUPP; break; @@ -1275,6 +1272,15 @@ udp_detach(struct socket *so) return (0); } +int +udp_bind(struct socket *so, struct mbuf *addr, struct proc *p) +{ + struct inpcb *inp = sotoinpcb(so); + + soassertlocked(so); + return in_pcbbind(inp, addr, p); +} + /* * Sysctl for udp variables. */ diff --git a/sys/netinet/udp_var.h b/sys/netinet/udp_var.h index 939068eb3bd..c8ef191313d 100644 --- a/sys/netinet/udp_var.h +++ b/sys/netinet/udp_var.h @@ -1,4 +1,4 @@ -/* $OpenBSD: udp_var.h,v 1.38 2022/08/15 09:11:39 mvs Exp $ */ +/* $OpenBSD: udp_var.h,v 1.39 2022/08/20 23:48:58 mvs Exp $ */ /* $NetBSD: udp_var.h,v 1.12 1996/02/13 23:44:41 christos Exp $ */ /* @@ -143,5 +143,6 @@ int udp_usrreq(struct socket *, int, struct mbuf *, struct mbuf *, struct mbuf *, struct proc *); int udp_attach(struct socket *, int); int udp_detach(struct socket *); +int udp_bind(struct socket *, struct mbuf *, struct proc *); #endif /* _KERNEL */ #endif /* _NETINET_UDP_VAR_H_ */ diff --git a/sys/netinet6/ip6_divert.c b/sys/netinet6/ip6_divert.c index 6a02695e1ff..bf7d4436bfe 100644 --- a/sys/netinet6/ip6_divert.c +++ b/sys/netinet6/ip6_divert.c @@ -1,4 +1,4 @@ -/* $OpenBSD: ip6_divert.c,v 1.68 2022/08/15 09:11:39 mvs Exp $ */ +/* $OpenBSD: ip6_divert.c,v 1.69 2022/08/20 23:48:58 mvs Exp $ */ /* * Copyright (c) 2009 Michele Marchetto @@ -67,6 +67,7 @@ const struct pr_usrreqs divert6_usrreqs = { .pru_usrreq = divert6_usrreq, .pru_attach = divert6_attach, .pru_detach = divert6_detach, + .pru_bind = divert6_bind, }; int divb6hashsize = DIVERTHASHSIZE; @@ -280,10 +281,6 @@ divert6_usrreq(struct socket *so, int req, struct mbuf *m, struct mbuf *addr, } switch (req) { - case PRU_BIND: - error = in_pcbbind(inp, addr, p); - break; - case PRU_SHUTDOWN: socantsendmore(so); break; @@ -371,6 +368,15 @@ divert6_detach(struct socket *so) return (0); } +int +divert6_bind(struct socket *so, struct mbuf *addr, struct proc *p) +{ + struct inpcb *inp = sotoinpcb(so); + + soassertlocked(so); + return in_pcbbind(inp, addr, p); +} + int divert6_sysctl_div6stat(void *oldp, size_t *oldlenp, void *newp) { diff --git a/sys/netinet6/ip6_divert.h b/sys/netinet6/ip6_divert.h index cc8ea696813..a17549c6ef0 100644 --- a/sys/netinet6/ip6_divert.h +++ b/sys/netinet6/ip6_divert.h @@ -1,4 +1,4 @@ -/* $OpenBSD: ip6_divert.h,v 1.14 2022/08/15 09:11:39 mvs Exp $ */ +/* $OpenBSD: ip6_divert.h,v 1.15 2022/08/20 23:48:58 mvs Exp $ */ /* * Copyright (c) 2009 Michele Marchetto @@ -74,6 +74,7 @@ int divert6_usrreq(struct socket *, int, struct mbuf *, struct mbuf *, struct mbuf *, struct proc *); int divert6_attach(struct socket *, int); int divert6_detach(struct socket *); +int divert6_bind(struct socket *, struct mbuf *, struct proc *); #endif /* _KERNEL */ #endif /* _IP6_DIVERT_H_ */ diff --git a/sys/netinet6/ip6_var.h b/sys/netinet6/ip6_var.h index 0fc60efe413..eb45f1d6408 100644 --- a/sys/netinet6/ip6_var.h +++ b/sys/netinet6/ip6_var.h @@ -1,4 +1,4 @@ -/* $OpenBSD: ip6_var.h,v 1.95 2022/08/15 09:11:39 mvs Exp $ */ +/* $OpenBSD: ip6_var.h,v 1.96 2022/08/20 23:48:58 mvs Exp $ */ /* $KAME: ip6_var.h,v 1.33 2000/06/11 14:59:20 jinmei Exp $ */ /* @@ -355,6 +355,7 @@ int rip6_usrreq(struct socket *, int, struct mbuf *, struct mbuf *, struct mbuf *, struct proc *); int rip6_attach(struct socket *, int); int rip6_detach(struct socket *); +int rip6_bind(struct socket *, struct mbuf *, struct proc *); int rip6_sysctl(int *, u_int, void *, size_t *, void *, size_t); int dest6_input(struct mbuf **, int *, int, int); diff --git a/sys/netinet6/raw_ip6.c b/sys/netinet6/raw_ip6.c index 93752793884..cf311001e2a 100644 --- a/sys/netinet6/raw_ip6.c +++ b/sys/netinet6/raw_ip6.c @@ -1,4 +1,4 @@ -/* $OpenBSD: raw_ip6.c,v 1.150 2022/08/15 09:11:39 mvs Exp $ */ +/* $OpenBSD: raw_ip6.c,v 1.151 2022/08/20 23:48:58 mvs Exp $ */ /* $KAME: raw_ip6.c,v 1.69 2001/03/04 15:55:44 itojun Exp $ */ /* @@ -109,6 +109,7 @@ const struct pr_usrreqs rip6_usrreqs = { .pru_usrreq = rip6_usrreq, .pru_attach = rip6_attach, .pru_detach = rip6_detach, + .pru_bind = rip6_bind, }; /* @@ -604,25 +605,6 @@ rip6_usrreq(struct socket *so, int req, struct mbuf *m, struct mbuf *nam, in_pcbdetach(in6p); break; - case PRU_BIND: - { - struct sockaddr_in6 *addr; - - if ((error = in6_nam2sin6(nam, &addr))) - break; - /* - * Make sure to not enter in_pcblookup_local(), local ports - * are non-sensical for raw sockets. - */ - addr->sin6_port = 0; - - if ((error = in6_pcbaddrisavail(in6p, addr, 0, p))) - break; - - in6p->inp_laddr6 = addr->sin6_addr; - break; - } - case PRU_CONNECT: { struct sockaddr_in6 *addr; @@ -775,6 +757,31 @@ rip6_detach(struct socket *so) return (0); } +int +rip6_bind(struct socket *so, struct mbuf *nam, struct proc *p) +{ + struct inpcb *in6p = sotoinpcb(so); + struct sockaddr_in6 *addr; + int error; + + soassertlocked(so); + + if ((error = in6_nam2sin6(nam, &addr))) + return (error); + + /* + * Make sure to not enter in_pcblookup_local(), local ports + * are non-sensical for raw sockets. + */ + addr->sin6_port = 0; + + if ((error = in6_pcbaddrisavail(in6p, addr, 0, p))) + return (error); + + in6p->inp_laddr6 = addr->sin6_addr; + return (0); +} + int rip6_sysctl_rip6stat(void *oldp, size_t *oldplen, void *newp) { diff --git a/sys/sys/protosw.h b/sys/sys/protosw.h index 9fb80269c26..560699bfea8 100644 --- a/sys/sys/protosw.h +++ b/sys/sys/protosw.h @@ -1,4 +1,4 @@ -/* $OpenBSD: protosw.h,v 1.37 2022/08/15 09:11:39 mvs Exp $ */ +/* $OpenBSD: protosw.h,v 1.38 2022/08/20 23:48:58 mvs Exp $ */ /* $NetBSD: protosw.h,v 1.10 1996/04/09 20:55:32 cgd Exp $ */ /*- @@ -66,6 +66,7 @@ struct pr_usrreqs { int (*pru_attach)(struct socket *, int); int (*pru_detach)(struct socket *); + int (*pru_bind)(struct socket *, struct mbuf *, struct proc *); }; struct protosw { @@ -264,8 +265,9 @@ pru_detach(struct socket *so) static inline int pru_bind(struct socket *so, struct mbuf *nam, struct proc *p) { - return (*so->so_proto->pr_usrreqs->pru_usrreq)(so, - PRU_BIND, NULL, nam, NULL, p); + if (so->so_proto->pr_usrreqs->pru_bind) + return (*so->so_proto->pr_usrreqs->pru_bind)(so, nam, p); + return (EOPNOTSUPP); } static inline int diff --git a/sys/sys/unpcb.h b/sys/sys/unpcb.h index f16d236da19..92bd53acf90 100644 --- a/sys/sys/unpcb.h +++ b/sys/sys/unpcb.h @@ -1,4 +1,4 @@ -/* $OpenBSD: unpcb.h,v 1.27 2022/08/15 09:11:39 mvs Exp $ */ +/* $OpenBSD: unpcb.h,v 1.28 2022/08/20 23:48:58 mvs Exp $ */ /* $NetBSD: unpcb.h,v 1.6 1994/06/29 06:46:08 cgd Exp $ */ /* @@ -113,6 +113,7 @@ int uipc_usrreq(struct socket *, int , struct mbuf *, struct mbuf *, struct mbuf *, struct proc *); int uipc_attach(struct socket *, int); int uipc_detach(struct socket *); +int uipc_bind(struct socket *, struct mbuf *, struct proc *); void unp_init(void); int unp_bind(struct unpcb *, struct mbuf *, struct proc *);