Skip site navigation (1)Skip section navigation (2)
Date:      Mon, 24 Aug 2020 08:16:07 +0000 (UTC)
From:      Michael Tuexen <tuexen@FreeBSD.org>
To:        src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-stable@freebsd.org, svn-src-stable-12@freebsd.org
Subject:   svn commit: r364616 - stable/12/sys/netinet
Message-ID:  <202008240816.07O8G7Zu064747@repo.freebsd.org>

next in thread | raw e-mail | index | archive | help
Author: tuexen
Date: Mon Aug 24 08:16:07 2020
New Revision: 364616
URL: https://svnweb.freebsd.org/changeset/base/364616

Log:
  MFC r362448:
  Cleanup the adding and deleting of addresses via sctp_bindx().
  
  There is no need to use the association identifier, so remove it.
  While there, cleanup the code a bit.

Modified:
  stable/12/sys/netinet/sctp_usrreq.c
  stable/12/sys/netinet/sctputil.c
  stable/12/sys/netinet/sctputil.h
Directory Properties:
  stable/12/   (props changed)

Modified: stable/12/sys/netinet/sctp_usrreq.c
==============================================================================
--- stable/12/sys/netinet/sctp_usrreq.c	Mon Aug 24 08:14:41 2020	(r364615)
+++ stable/12/sys/netinet/sctp_usrreq.c	Mon Aug 24 08:16:07 2020	(r364616)
@@ -6013,9 +6013,7 @@ sctp_setopt(struct socket *so, int optname, void *optv
 				error = EAFNOSUPPORT;
 				break;
 			}
-			sctp_bindx_add_address(so, inp, addrs->addr,
-			    addrs->sget_assoc_id, vrf_id,
-			    &error, p);
+			sctp_bindx_add_address(so, inp, addrs->addr, vrf_id, &error, p);
 			break;
 		}
 	case SCTP_BINDX_REM_ADDR:
@@ -6059,9 +6057,7 @@ sctp_setopt(struct socket *so, int optname, void *optv
 				error = EAFNOSUPPORT;
 				break;
 			}
-			sctp_bindx_delete_address(inp, addrs->addr,
-			    addrs->sget_assoc_id, vrf_id,
-			    &error);
+			sctp_bindx_delete_address(inp, addrs->addr, vrf_id, &error);
 			break;
 		}
 	case SCTP_EVENT:

Modified: stable/12/sys/netinet/sctputil.c
==============================================================================
--- stable/12/sys/netinet/sctputil.c	Mon Aug 24 08:14:41 2020	(r364615)
+++ stable/12/sys/netinet/sctputil.c	Mon Aug 24 08:16:07 2020	(r364616)
@@ -6682,13 +6682,21 @@ sctp_connectx_helper_find(struct sctp_inpcb *inp, stru
  */
 void
 sctp_bindx_add_address(struct socket *so, struct sctp_inpcb *inp,
-    struct sockaddr *sa, sctp_assoc_t assoc_id,
-    uint32_t vrf_id, int *error, void *p)
+    struct sockaddr *sa, uint32_t vrf_id, int *error,
+    void *p)
 {
-	struct sockaddr *addr_touse;
 #if defined(INET) && defined(INET6)
 	struct sockaddr_in sin;
 #endif
+#ifdef INET6
+	struct sockaddr_in6 *sin6;
+#endif
+#ifdef INET
+	struct sockaddr_in *sinp;
+#endif
+	struct sockaddr *addr_to_use;
+	struct sctp_inpcb *lep;
+	uint16_t port;
 
 	/* see if we're bound all already! */
 	if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
@@ -6696,13 +6704,9 @@ sctp_bindx_add_address(struct socket *so, struct sctp_
 		*error = EINVAL;
 		return;
 	}
-	addr_touse = sa;
+	switch (sa->sa_family) {
 #ifdef INET6
-	if (sa->sa_family == AF_INET6) {
-#ifdef INET
-		struct sockaddr_in6 *sin6;
-
-#endif
+	case AF_INET6:
 		if (sa->sa_len != sizeof(struct sockaddr_in6)) {
 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
 			*error = EINVAL;
@@ -6714,8 +6718,9 @@ sctp_bindx_add_address(struct socket *so, struct sctp_
 			*error = EINVAL;
 			return;
 		}
+		sin6 = (struct sockaddr_in6 *)sa;
+		port = sin6->sin6_port;
 #ifdef INET
-		sin6 = (struct sockaddr_in6 *)addr_touse;
 		if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
 			if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) &&
 			    SCTP_IPV6_V6ONLY(inp)) {
@@ -6725,13 +6730,15 @@ sctp_bindx_add_address(struct socket *so, struct sctp_
 				return;
 			}
 			in6_sin6_2_sin(&sin, sin6);
-			addr_touse = (struct sockaddr *)&sin;
+			addr_to_use = (struct sockaddr *)&sin;
+		} else {
+			addr_to_use = sa;
 		}
 #endif
-	}
+		break;
 #endif
 #ifdef INET
-	if (sa->sa_family == AF_INET) {
+	case AF_INET:
 		if (sa->sa_len != sizeof(struct sockaddr_in)) {
 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
 			*error = EINVAL;
@@ -6744,8 +6751,16 @@ sctp_bindx_add_address(struct socket *so, struct sctp_
 			*error = EINVAL;
 			return;
 		}
-	}
+		sinp = (struct sockaddr_in *)sa;
+		port = sinp->sin_port;
+		addr_to_use = sa;
+		break;
 #endif
+	default:
+		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
+		*error = EINVAL;
+		return;
+	}
 	if (inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) {
 		if (p == NULL) {
 			/* Can't get proc for Net/Open BSD */
@@ -6753,55 +6768,25 @@ sctp_bindx_add_address(struct socket *so, struct sctp_
 			*error = EINVAL;
 			return;
 		}
-		*error = sctp_inpcb_bind(so, addr_touse, NULL, p);
+		*error = sctp_inpcb_bind(so, addr_to_use, NULL, p);
 		return;
 	}
-	/*
-	 * No locks required here since bind and mgmt_ep_sa all do their own
-	 * locking. If we do something for the FIX: below we may need to
-	 * lock in that case.
-	 */
-	if (assoc_id == 0) {
+	/* Validate the incoming port. */
+	if ((port != 0) && (port != inp->sctp_lport)) {
+		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
+		*error = EINVAL;
+		return;
+	}
+	lep = sctp_pcb_findep(addr_to_use, 1, 0, vrf_id);
+	if (lep == NULL) {
 		/* add the address */
-		struct sctp_inpcb *lep;
-		struct sockaddr_in *lsin = (struct sockaddr_in *)addr_touse;
-
-		/* validate the incoming port */
-		if ((lsin->sin_port != 0) &&
-		    (lsin->sin_port != inp->sctp_lport)) {
-			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
-			*error = EINVAL;
-			return;
-		} else {
-			/* user specified 0 port, set it to existing port */
-			lsin->sin_port = inp->sctp_lport;
-		}
-
-		lep = sctp_pcb_findep(addr_touse, 1, 0, vrf_id);
-		if (lep != NULL) {
-			/*
-			 * We must decrement the refcount since we have the
-			 * ep already and are binding. No remove going on
-			 * here.
-			 */
-			SCTP_INP_DECR_REF(lep);
-		}
-		if (lep == inp) {
-			/* already bound to it.. ok */
-			return;
-		} else if (lep == NULL) {
-			((struct sockaddr_in *)addr_touse)->sin_port = 0;
-			*error = sctp_addr_mgmt_ep_sa(inp, addr_touse,
-			    SCTP_ADD_IP_ADDRESS, vrf_id);
-		} else {
+		*error = sctp_addr_mgmt_ep_sa(inp, addr_to_use,
+		    SCTP_ADD_IP_ADDRESS, vrf_id);
+	} else {
+		if (lep != inp) {
 			*error = EADDRINUSE;
 		}
-		if (*error)
-			return;
-	} else {
-		/*
-		 * FIX: decide whether we allow assoc based bindx
-		 */
+		SCTP_INP_DECR_REF(lep);
 	}
 }
 
@@ -6811,11 +6796,11 @@ sctp_bindx_add_address(struct socket *so, struct sctp_
  */
 void
 sctp_bindx_delete_address(struct sctp_inpcb *inp,
-    struct sockaddr *sa, sctp_assoc_t assoc_id,
-    uint32_t vrf_id, int *error)
+    struct sockaddr *sa, uint32_t vrf_id, int *error)
 {
-	struct sockaddr *addr_touse;
+	struct sockaddr *addr_to_use;
 #if defined(INET) && defined(INET6)
+	struct sockaddr_in6 *sin6;
 	struct sockaddr_in sin;
 #endif
 
@@ -6825,13 +6810,9 @@ sctp_bindx_delete_address(struct sctp_inpcb *inp,
 		*error = EINVAL;
 		return;
 	}
-	addr_touse = sa;
+	switch (sa->sa_family) {
 #ifdef INET6
-	if (sa->sa_family == AF_INET6) {
-#ifdef INET
-		struct sockaddr_in6 *sin6;
-#endif
-
+	case AF_INET6:
 		if (sa->sa_len != sizeof(struct sockaddr_in6)) {
 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
 			*error = EINVAL;
@@ -6844,7 +6825,7 @@ sctp_bindx_delete_address(struct sctp_inpcb *inp,
 			return;
 		}
 #ifdef INET
-		sin6 = (struct sockaddr_in6 *)addr_touse;
+		sin6 = (struct sockaddr_in6 *)sa;
 		if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
 			if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) &&
 			    SCTP_IPV6_V6ONLY(inp)) {
@@ -6854,13 +6835,15 @@ sctp_bindx_delete_address(struct sctp_inpcb *inp,
 				return;
 			}
 			in6_sin6_2_sin(&sin, sin6);
-			addr_touse = (struct sockaddr *)&sin;
+			addr_to_use = (struct sockaddr *)&sin;
+		} else {
+			addr_to_use = sa;
 		}
 #endif
-	}
+		break;
 #endif
 #ifdef INET
-	if (sa->sa_family == AF_INET) {
+	case AF_INET:
 		if (sa->sa_len != sizeof(struct sockaddr_in)) {
 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
 			*error = EINVAL;
@@ -6873,22 +6856,17 @@ sctp_bindx_delete_address(struct sctp_inpcb *inp,
 			*error = EINVAL;
 			return;
 		}
-	}
+		addr_to_use = sa;
+		break;
 #endif
-	/*
-	 * No lock required mgmt_ep_sa does its own locking. If the FIX:
-	 * below is ever changed we may need to lock before calling
-	 * association level binding.
-	 */
-	if (assoc_id == 0) {
-		/* delete the address */
-		*error = sctp_addr_mgmt_ep_sa(inp, addr_touse,
-		    SCTP_DEL_IP_ADDRESS, vrf_id);
-	} else {
-		/*
-		 * FIX: decide whether we allow assoc based bindx
-		 */
+	default:
+		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
+		*error = EINVAL;
+		return;
 	}
+	/* No lock required mgmt_ep_sa does its own locking. */
+	*error = sctp_addr_mgmt_ep_sa(inp, addr_to_use, SCTP_DEL_IP_ADDRESS,
+	    vrf_id);
 }
 
 /*

Modified: stable/12/sys/netinet/sctputil.h
==============================================================================
--- stable/12/sys/netinet/sctputil.h	Mon Aug 24 08:14:41 2020	(r364615)
+++ stable/12/sys/netinet/sctputil.h	Mon Aug 24 08:16:07 2020	(r364616)
@@ -229,11 +229,10 @@ struct mbuf *sctp_generate_no_user_data_cause(uint32_t
 
 void
 sctp_bindx_add_address(struct socket *so, struct sctp_inpcb *inp,
-    struct sockaddr *sa, sctp_assoc_t assoc_id,
-    uint32_t vrf_id, int *error, void *p);
+    struct sockaddr *sa, uint32_t vrf_id, int *error,
+    void *p);
 void
-sctp_bindx_delete_address(struct sctp_inpcb *inp,
-    struct sockaddr *sa, sctp_assoc_t assoc_id,
+sctp_bindx_delete_address(struct sctp_inpcb *inp, struct sockaddr *sa,
     uint32_t vrf_id, int *error);
 
 int sctp_local_addr_count(struct sctp_tcb *stcb);



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?202008240816.07O8G7Zu064747>