git: c93db4abf454 - main - udp: call UDP methods from UDP over IPv6 directly

From: Gleb Smirnoff <glebius_at_FreeBSD.org>
Date: Tue, 16 Aug 2022 19:40:53 UTC
The branch main has been updated by glebius:

URL: https://cgit.FreeBSD.org/src/commit/?id=c93db4abf4545d1bc8cb2b67eacd17f6d1ee6b74

commit c93db4abf4545d1bc8cb2b67eacd17f6d1ee6b74
Author:     Gleb Smirnoff <glebius@FreeBSD.org>
AuthorDate: 2022-08-16 19:40:36 +0000
Commit:     Gleb Smirnoff <glebius@FreeBSD.org>
CommitDate: 2022-08-16 19:40:36 +0000

    udp: call UDP methods from UDP over IPv6 directly
    
    Both UDP and UDP Lite use same methods on sockets.  Both UDP over IPv4
    and over IPv6 use same methods.  Don't pretend that methods can switch
    and remove this unneeded complexity.
    
    Reviewed by:            melifaro
    Differential revision:  https://reviews.freebsd.org/D36154
---
 sys/netinet/udp_usrreq.c   |  9 ++++++---
 sys/netinet6/udp6_usrreq.c | 37 +++++++++++--------------------------
 2 files changed, 17 insertions(+), 29 deletions(-)

diff --git a/sys/netinet/udp_usrreq.c b/sys/netinet/udp_usrreq.c
index 86093806dccc..bff82b9718e1 100644
--- a/sys/netinet/udp_usrreq.c
+++ b/sys/netinet/udp_usrreq.c
@@ -1490,7 +1490,8 @@ release:
 	return (error);
 }
 
-static void
+pr_abort_t udp_abort;			/* shared with udp6_usrreq.c */
+void
 udp_abort(struct socket *so)
 {
 	struct inpcb *inp;
@@ -1683,7 +1684,8 @@ udp_detach(struct socket *so)
 	udp_discardcb(up);
 }
 
-static int
+pr_disconnect_t udp_disconnect;		/* shared with udp6_usrreq.c */
+int
 udp_disconnect(struct socket *so)
 {
 	struct inpcb *inp;
@@ -1708,7 +1710,8 @@ udp_disconnect(struct socket *so)
 	return (0);
 }
 
-static int
+pr_send_t udp_send;			/* shared with udp6_usrreq.c */
+int
 udp_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
     struct mbuf *control, struct thread *td)
 {
diff --git a/sys/netinet6/udp6_usrreq.c b/sys/netinet6/udp6_usrreq.c
index a7bdfce97707..6a3ac2abd90b 100644
--- a/sys/netinet6/udp6_usrreq.c
+++ b/sys/netinet6/udp6_usrreq.c
@@ -131,12 +131,18 @@ VNET_DEFINE(int, zero_checksum_port) = 0;
 SYSCTL_INT(_net_inet6_udp6, OID_AUTO, rfc6935_port, CTLFLAG_VNET | CTLFLAG_RW,
     &VNET_NAME(zero_checksum_port), 0,
     "Zero UDP checksum allowed for traffic to/from this port.");
+
+
+/* netinet/udp_usrreqs.c */
+pr_abort_t	udp_abort;
+pr_disconnect_t	udp_disconnect;
+pr_send_t	udp_send;
+
 /*
  * UDP protocol implementation.
  * Per RFC 768, August, 1980.
  */
 
-extern struct protosw	inetsw[];
 static void		udp6_detach(struct socket *so);
 
 static int
@@ -777,8 +783,6 @@ udp6_output(struct socket *so, int flags_arg, struct mbuf *m,
 			hasv4addr = IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)
 			    ? 1 : 0;
 		if (hasv4addr) {
-			struct pr_usrreqs *pru;
-
 			/*
 			 * XXXRW: We release UDP-layer locks before calling
 			 * udp_send() in order to avoid recursion.  However,
@@ -790,9 +794,8 @@ udp6_output(struct socket *so, int flags_arg, struct mbuf *m,
 			INP_UNLOCK(inp);
 			if (sin6)
 				in6_sin6_2_sin_in_sock((struct sockaddr *)sin6);
-			pru = inetsw[ip_protox[nxt]].pr_usrreqs;
 			/* addr will just be freed in sendit(). */
-			return ((*pru->pru_send)(so, flags_arg | PRUS_IPV6, m,
+			return (udp_send(so, flags_arg | PRUS_IPV6, m,
 			    (struct sockaddr *)sin6, control, td));
 		}
 	} else
@@ -1003,14 +1006,8 @@ udp6_abort(struct socket *so)
 	INP_WLOCK(inp);
 #ifdef INET
 	if (inp->inp_vflag & INP_IPV4) {
-		struct pr_usrreqs *pru;
-		uint8_t nxt;
-
-		nxt = (inp->inp_socket->so_proto->pr_protocol == IPPROTO_UDP) ?
-		    IPPROTO_UDP : IPPROTO_UDPLITE;
 		INP_WUNLOCK(inp);
-		pru = inetsw[ip_protox[nxt]].pr_usrreqs;
-		(*pru->pru_abort)(so);
+		udp_abort(so);
 		return;
 	}
 #endif
@@ -1131,14 +1128,8 @@ udp6_close(struct socket *so)
 	INP_WLOCK(inp);
 #ifdef INET
 	if (inp->inp_vflag & INP_IPV4) {
-		struct pr_usrreqs *pru;
-		uint8_t nxt;
-
-		nxt = (inp->inp_socket->so_proto->pr_protocol == IPPROTO_UDP) ?
-		    IPPROTO_UDP : IPPROTO_UDPLITE;
 		INP_WUNLOCK(inp);
-		pru = inetsw[ip_protox[nxt]].pr_usrreqs;
-		(*pru->pru_disconnect)(so);
+		(void)udp_disconnect(so);
 		return;
 	}
 #endif
@@ -1283,14 +1274,8 @@ udp6_disconnect(struct socket *so)
 	INP_WLOCK(inp);
 #ifdef INET
 	if (inp->inp_vflag & INP_IPV4) {
-		struct pr_usrreqs *pru;
-		uint8_t nxt;
-
-		nxt = (inp->inp_socket->so_proto->pr_protocol == IPPROTO_UDP) ?
-		    IPPROTO_UDP : IPPROTO_UDPLITE;
 		INP_WUNLOCK(inp);
-		pru = inetsw[ip_protox[nxt]].pr_usrreqs;
-		(void)(*pru->pru_disconnect)(so);
+		(void)udp_disconnect(so);
 		return (0);
 	}
 #endif