git: 52640d617449 - main - sctp: update zero checksum support

From: Michael Tuexen <tuexen_at_FreeBSD.org>
Date: Sun, 23 Jul 2023 13:43:03 UTC
The branch main has been updated by tuexen:

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

commit 52640d617449287731b6e70ce107d1ac8309c3ee
Author:     Michael Tuexen <tuexen@FreeBSD.org>
AuthorDate: 2023-07-23 04:41:32 +0000
Commit:     Michael Tuexen <tuexen@FreeBSD.org>
CommitDate: 2023-07-23 04:41:32 +0000

    sctp: update zero checksum support
    
    Implement support for the error detection method identifier.
    MFC after:      2 weeks
---
 sys/netinet/sctp_header.h  |  9 ++++-
 sys/netinet/sctp_input.c   | 11 ++++--
 sys/netinet/sctp_output.c  | 93 ++++++++++++++++++++++++++++++++++++----------
 sys/netinet/sctp_pcb.c     | 25 +++++++++----
 sys/netinet/sctp_pcb.h     |  2 +-
 sys/netinet/sctp_structs.h |  8 ++--
 sys/netinet/sctp_uio.h     |  4 ++
 sys/netinet/sctp_usrreq.c  | 12 +++---
 sys/netinet/sctputil.c     |  3 +-
 9 files changed, 125 insertions(+), 42 deletions(-)

diff --git a/sys/netinet/sctp_header.h b/sys/netinet/sctp_header.h
index 93dda17ae9b1..d077c4573bf2 100644
--- a/sys/netinet/sctp_header.h
+++ b/sys/netinet/sctp_header.h
@@ -207,7 +207,7 @@ struct sctp_state_cookie {	/* this is our definition... */
 
 	uint8_t ipv4_scope;	/* IPv4 private addr scope */
 	uint8_t loopback_scope;	/* loopback scope information */
-	uint8_t zero_checksum;	/* copy of the inp value */
+	uint8_t rcv_edmid;	/* copy of the inp value */
 	uint8_t reserved[SCTP_RESERVE_SPACE];	/* Align to 64 bits */
 	/*
 	 * at the end is tacked on the INIT chunk and the INIT-ACK chunk
@@ -520,6 +520,13 @@ struct sctp_auth_chunk {
 	uint8_t hmac[];
 }               SCTP_PACKED;
 
+/* Zero checksum support draft-ietf-tsvwg-sctp-zero-checksum */
+
+struct sctp_zero_checksum_acceptable {
+	struct sctp_paramhdr ph;
+	uint32_t edmid;
+}                             SCTP_PACKED;
+
 /*
  * we pre-reserve enough room for a ECNE or CWR AND a SACK with no missing
  * pieces. If ENCE is missing we could have a couple of blocks. This way we
diff --git a/sys/netinet/sctp_input.c b/sys/netinet/sctp_input.c
index 5a7ed6ffde90..c585e31c8296 100644
--- a/sys/netinet/sctp_input.c
+++ b/sys/netinet/sctp_input.c
@@ -1883,7 +1883,7 @@ sctp_process_cookie_existing(struct mbuf *m, int iphlen, int offset,
 			}
 			SCTP_ZONE_FREE(SCTP_BASE_INFO(ipi_zone_asconf_ack), aack);
 		}
-		asoc->zero_checksum = cookie->zero_checksum;
+		asoc->rcv_edmid = cookie->rcv_edmid;
 
 		/* process the INIT-ACK info (my info) */
 		asoc->my_vtag = ntohl(initack_cp->init.initiate_tag);
@@ -2080,7 +2080,7 @@ sctp_process_cookie_new(struct mbuf *m, int iphlen, int offset,
 		    SCTP_FROM_SCTP_INPUT + SCTP_LOC_18);
 		return (NULL);
 	}
-	asoc->zero_checksum = cookie->zero_checksum;
+	asoc->rcv_edmid = cookie->rcv_edmid;
 	/* process the INIT-ACK info (my info) */
 	asoc->my_rwnd = ntohl(initack_cp->init.a_rwnd);
 
@@ -5381,9 +5381,14 @@ sctp_common_input_processing(struct mbuf **mm, int iphlen, int offset, int lengt
 			stcb = sctp_findassociation_addr(m, offset, src, dst,
 			    sh, ch, &inp, &net, vrf_id);
 			stcb_looked_up = true;
-			if ((stcb == NULL) || (stcb->asoc.zero_checksum == 0)) {
+			if (stcb == NULL) {
+				goto validate_cksum;
+			}
+			if (stcb->asoc.rcv_edmid == SCTP_EDMID_NONE) {
 				goto validate_cksum;
 			}
+			KASSERT(stcb->asoc.rcv_edmid == SCTP_EDMID_LOWER_LAYER_DTLS,
+			    ("Unexpected EDMID %u", stcb->asoc.rcv_edmid));
 			SCTP_STAT_INCR(sctps_recvzerocrc);
 		}
 	}
diff --git a/sys/netinet/sctp_output.c b/sys/netinet/sctp_output.c
index 5b4fbb28e089..a146ad401b30 100644
--- a/sys/netinet/sctp_output.c
+++ b/sys/netinet/sctp_output.c
@@ -4627,6 +4627,7 @@ sctp_send_initiate(struct sctp_inpcb *inp, struct sctp_tcb *stcb, int so_locked)
 	struct sctp_init_chunk *init;
 	struct sctp_supported_addr_param *sup_addr;
 	struct sctp_adaptation_layer_indication *ali;
+	struct sctp_zero_checksum_acceptable *zero_chksum;
 	struct sctp_supported_chunk_types_param *pr_supported;
 	struct sctp_paramhdr *ph;
 	int cnt_inits_to = 0;
@@ -4720,11 +4721,12 @@ sctp_send_initiate(struct sctp_inpcb *inp, struct sctp_tcb *stcb, int so_locked)
 	}
 
 	/* Zero checksum acceptable parameter */
-	if (stcb->asoc.zero_checksum > 0) {
-		parameter_len = (uint16_t)sizeof(struct sctp_paramhdr);
-		ph = (struct sctp_paramhdr *)(mtod(m, caddr_t)+chunk_len);
-		ph->param_type = htons(SCTP_ZERO_CHECKSUM_ACCEPTABLE);
-		ph->param_length = htons(parameter_len);
+	if (stcb->asoc.rcv_edmid != SCTP_EDMID_NONE) {
+		parameter_len = (uint16_t)sizeof(struct sctp_zero_checksum_acceptable);
+		zero_chksum = (struct sctp_zero_checksum_acceptable *)(mtod(m, caddr_t)+chunk_len);
+		zero_chksum->ph.param_type = htons(SCTP_ZERO_CHECKSUM_ACCEPTABLE);
+		zero_chksum->ph.param_length = htons(parameter_len);
+		zero_chksum->edmid = htonl(stcb->asoc.rcv_edmid);
 		chunk_len += parameter_len;
 	}
 
@@ -5483,6 +5485,7 @@ sctp_send_initiate_ack(struct sctp_inpcb *inp, struct sctp_tcb *stcb,
 	struct mbuf *m, *m_tmp, *m_last, *m_cookie, *op_err;
 	struct sctp_init_ack_chunk *initack;
 	struct sctp_adaptation_layer_indication *ali;
+	struct sctp_zero_checksum_acceptable *zero_chksum;
 	struct sctp_supported_chunk_types_param *pr_supported;
 	struct sctp_paramhdr *ph;
 	union sctp_sockstore *over_addr;
@@ -5805,9 +5808,9 @@ do_a_abort:
 		}
 	}
 	if (asoc != NULL) {
-		stc.zero_checksum = asoc->zero_checksum > 0 ? 1 : 0;
+		stc.rcv_edmid = asoc->rcv_edmid;
 	} else {
-		stc.zero_checksum = inp->zero_checksum;
+		stc.rcv_edmid = inp->rcv_edmid;
 	}
 	/* Now lets put the SCTP header in place */
 	initack = mtod(m, struct sctp_init_ack_chunk *);
@@ -5933,12 +5936,17 @@ do_a_abort:
 	}
 
 	/* Zero checksum acceptable parameter */
-	if (((asoc != NULL) && (asoc->zero_checksum > 0)) ||
-	    ((asoc == NULL) && (inp->zero_checksum == 1))) {
-		parameter_len = (uint16_t)sizeof(struct sctp_paramhdr);
-		ph = (struct sctp_paramhdr *)(mtod(m, caddr_t)+chunk_len);
-		ph->param_type = htons(SCTP_ZERO_CHECKSUM_ACCEPTABLE);
-		ph->param_length = htons(parameter_len);
+	if (((asoc != NULL) && (asoc->rcv_edmid != SCTP_EDMID_NONE)) ||
+	    ((asoc == NULL) && (inp->rcv_edmid != SCTP_EDMID_NONE))) {
+		parameter_len = (uint16_t)sizeof(struct sctp_zero_checksum_acceptable);
+		zero_chksum = (struct sctp_zero_checksum_acceptable *)(mtod(m, caddr_t)+chunk_len);
+		zero_chksum->ph.param_type = htons(SCTP_ZERO_CHECKSUM_ACCEPTABLE);
+		zero_chksum->ph.param_length = htons(parameter_len);
+		if (asoc != NULL) {
+			zero_chksum->edmid = htonl(asoc->rcv_edmid);
+		} else {
+			zero_chksum->edmid = htonl(inp->rcv_edmid);
+		}
 		chunk_len += parameter_len;
 	}
 
@@ -8426,7 +8434,14 @@ again_one_more_time:
 					 * flight size since this little guy
 					 * is a control only packet.
 					 */
-					use_zero_crc = asoc->zero_checksum == 2;
+					switch (asoc->snd_edmid) {
+					case SCTP_EDMID_LOWER_LAYER_DTLS:
+						use_zero_crc = true;
+						break;
+					default:
+						use_zero_crc = false;
+						break;
+					}
 					if (asconf) {
 						sctp_timer_start(SCTP_TIMER_TYPE_ASCONF, inp, stcb, net);
 						use_zero_crc = false;
@@ -8758,8 +8773,15 @@ again_one_more_time:
 no_data_fill:
 		/* Is there something to send for this destination? */
 		if (outchain) {
+			switch (asoc->snd_edmid) {
+			case SCTP_EDMID_LOWER_LAYER_DTLS:
+				use_zero_crc = true;
+				break;
+			default:
+				use_zero_crc = false;
+				break;
+			}
 			/* We may need to start a control timer or two */
-			use_zero_crc = asoc->zero_checksum == 2;
 			if (asconf) {
 				sctp_timer_start(SCTP_TIMER_TYPE_ASCONF, inp,
 				    stcb, net);
@@ -9495,7 +9517,14 @@ sctp_chunk_retransmission(struct sctp_inpcb *inp,
 	/* do we have control chunks to retransmit? */
 	if (m != NULL) {
 		/* Start a timer no matter if we succeed or fail */
-		use_zero_crc = asoc->zero_checksum == 2;
+		switch (asoc->snd_edmid) {
+		case SCTP_EDMID_LOWER_LAYER_DTLS:
+			use_zero_crc = true;
+			break;
+		default:
+			use_zero_crc = false;
+			break;
+		}
 		if (chk->rec.chunk_id.id == SCTP_COOKIE_ECHO) {
 			sctp_timer_start(SCTP_TIMER_TYPE_COOKIE, inp, stcb, chk->whoTo);
 			use_zero_crc = false;
@@ -9782,6 +9811,14 @@ one_chunk_around:
 				sctp_timer_start(SCTP_TIMER_TYPE_SEND, inp, stcb, net);
 				tmr_started = 1;
 			}
+			switch (asoc->snd_edmid) {
+			case SCTP_EDMID_LOWER_LAYER_DTLS:
+				use_zero_crc = true;
+				break;
+			default:
+				use_zero_crc = false;
+				break;
+			}
 			/* Now lets send it, if there is anything to send :> */
 			if ((error = sctp_lowlevel_chunk_output(inp, stcb, net,
 			    (struct sockaddr *)&net->ro._l_addr, m,
@@ -9790,7 +9827,7 @@ one_chunk_around:
 			    inp->sctp_lport, stcb->rport, htonl(stcb->asoc.peer_vtag),
 			    net->port, NULL,
 			    0, 0,
-			    asoc->zero_checksum == 2,
+			    use_zero_crc,
 			    so_locked))) {
 				/* error, we could not output */
 				SCTPDBG(SCTP_DEBUG_OUTPUT3, "Gak send error %d\n", error);
@@ -10875,6 +10912,7 @@ sctp_send_abort_tcb(struct sctp_tcb *stcb, struct mbuf *operr, int so_locked)
 	uint32_t auth_offset = 0;
 	int error;
 	uint16_t cause_len, chunk_len, padding_len;
+	bool use_zero_crc;
 
 	SCTP_TCB_LOCK_ASSERT(stcb);
 	/*-
@@ -10889,6 +10927,14 @@ sctp_send_abort_tcb(struct sctp_tcb *stcb, struct mbuf *operr, int so_locked)
 	} else {
 		m_out = NULL;
 	}
+	switch (stcb->asoc.snd_edmid) {
+	case SCTP_EDMID_LOWER_LAYER_DTLS:
+		use_zero_crc = true;
+		break;
+	default:
+		use_zero_crc = false;
+		break;
+	}
 	m_abort = sctp_get_mbuf_for_msg(sizeof(struct sctp_abort_chunk), 0, M_NOWAIT, 1, MT_HEADER);
 	if (m_abort == NULL) {
 		if (m_out) {
@@ -10951,7 +10997,7 @@ sctp_send_abort_tcb(struct sctp_tcb *stcb, struct mbuf *operr, int so_locked)
 	    stcb->sctp_ep->sctp_lport, stcb->rport, htonl(vtag),
 	    stcb->asoc.primary_destination->port, NULL,
 	    0, 0,
-	    stcb->asoc.zero_checksum == 2,
+	    use_zero_crc,
 	    so_locked))) {
 		SCTPDBG(SCTP_DEBUG_OUTPUT3, "Gak send error %d\n", error);
 		if (error == ENOBUFS) {
@@ -10975,6 +11021,7 @@ sctp_send_shutdown_complete(struct sctp_tcb *stcb,
 	uint32_t vtag;
 	int error;
 	uint8_t flags;
+	bool use_zero_crc;
 
 	m_shutdown_comp = sctp_get_mbuf_for_msg(sizeof(struct sctp_chunkhdr), 0, M_NOWAIT, 1, MT_HEADER);
 	if (m_shutdown_comp == NULL) {
@@ -10988,6 +11035,14 @@ sctp_send_shutdown_complete(struct sctp_tcb *stcb,
 		flags = 0;
 		vtag = stcb->asoc.peer_vtag;
 	}
+	switch (stcb->asoc.snd_edmid) {
+	case SCTP_EDMID_LOWER_LAYER_DTLS:
+		use_zero_crc = true;
+		break;
+	default:
+		use_zero_crc = false;
+		break;
+	}
 	shutdown_complete = mtod(m_shutdown_comp, struct sctp_shutdown_complete_chunk *);
 	shutdown_complete->ch.chunk_type = SCTP_SHUTDOWN_COMPLETE;
 	shutdown_complete->ch.chunk_flags = flags;
@@ -11000,7 +11055,7 @@ sctp_send_shutdown_complete(struct sctp_tcb *stcb,
 	    htonl(vtag),
 	    net->port, NULL,
 	    0, 0,
-	    stcb->asoc.zero_checksum == 2,
+	    use_zero_crc,
 	    SCTP_SO_NOT_LOCKED))) {
 		SCTPDBG(SCTP_DEBUG_OUTPUT3, "Gak send error %d\n", error);
 		if (error == ENOBUFS) {
diff --git a/sys/netinet/sctp_pcb.c b/sys/netinet/sctp_pcb.c
index 4e97dc4e7ad3..9df2f82bdaa6 100644
--- a/sys/netinet/sctp_pcb.c
+++ b/sys/netinet/sctp_pcb.c
@@ -2433,7 +2433,7 @@ sctp_inpcb_alloc(struct socket *so, uint32_t vrf_id)
 	inp->nrsack_supported = (uint8_t)SCTP_BASE_SYSCTL(sctp_nrsack_enable);
 	inp->pktdrop_supported = (uint8_t)SCTP_BASE_SYSCTL(sctp_pktdrop_enable);
 	inp->idata_supported = 0;
-	inp->zero_checksum = 0;
+	inp->rcv_edmid = SCTP_EDMID_NONE;
 
 	inp->fibnum = so->so_fibnum;
 	/* init the small hash table we use to track asocid <-> tcb */
@@ -6403,12 +6403,23 @@ sctp_load_addresses_from_init(struct sctp_tcb *stcb, struct mbuf *m,
 			/* Peer supports pr-sctp */
 			peer_supports_prsctp = 1;
 		} else if (ptype == SCTP_ZERO_CHECKSUM_ACCEPTABLE) {
-			/*
-			 * Only send zero checksums if the upper layer has
-			 * also enabled the support for this.
-			 */
-			if (stcb->asoc.zero_checksum == 1) {
-				stcb->asoc.zero_checksum = 2;
+			struct sctp_zero_checksum_acceptable zero_chksum,
+			                             *zero_chksum_p;
+
+			phdr = sctp_get_next_param(m, offset,
+			    (struct sctp_paramhdr *)&zero_chksum,
+			    sizeof(struct sctp_zero_checksum_acceptable));
+			if (phdr != NULL) {
+				/*
+				 * Only send zero checksums if the upper
+				 * layer has enabled the support for the
+				 * same method as allowed by the peer.
+				 */
+				zero_chksum_p = (struct sctp_zero_checksum_acceptable *)phdr;
+				if ((ntohl(zero_chksum_p->edmid) != SCTP_EDMID_NONE) &&
+				    (ntohl(zero_chksum_p->edmid) == stcb->asoc.rcv_edmid)) {
+					stcb->asoc.snd_edmid = stcb->asoc.rcv_edmid;
+				}
 			}
 		} else if (ptype == SCTP_SUPPORTED_CHUNK_EXT) {
 			/* A supported extension chunk */
diff --git a/sys/netinet/sctp_pcb.h b/sys/netinet/sctp_pcb.h
index 39d8b4e7013e..7b94a40bcfa4 100644
--- a/sys/netinet/sctp_pcb.h
+++ b/sys/netinet/sctp_pcb.h
@@ -409,7 +409,7 @@ struct sctp_inpcb {
 	uint8_t reconfig_supported;
 	uint8_t nrsack_supported;
 	uint8_t pktdrop_supported;
-	uint8_t zero_checksum;
+	uint8_t rcv_edmid;
 	struct sctp_nonpad_sndrcvinfo def_send;
 	/*-
 	 * These three are here for the sosend_dgram
diff --git a/sys/netinet/sctp_structs.h b/sys/netinet/sctp_structs.h
index 504e9ad79fe3..ceec7687f7ee 100644
--- a/sys/netinet/sctp_structs.h
+++ b/sys/netinet/sctp_structs.h
@@ -1185,11 +1185,9 @@ struct sctp_association {
 	uint8_t pktdrop_supported;
 	uint8_t idata_supported;
 
-	/*
-	 * Zero checksum supported information: 0: disabled 1: enabled for
-	 * rcv 2: enabled for snd/rcv
-	 */
-	uint8_t zero_checksum;
+	/* Zero checksum supported information */
+	uint8_t rcv_edmid;
+	uint8_t snd_edmid;
 
 	/* Did the peer make the stream config (add out) request */
 	uint8_t peer_req_out;
diff --git a/sys/netinet/sctp_uio.h b/sys/netinet/sctp_uio.h
index e833b0ab33ae..d25df39dd981 100644
--- a/sys/netinet/sctp_uio.h
+++ b/sys/netinet/sctp_uio.h
@@ -790,6 +790,10 @@ struct sctp_get_nonce_values {
 	uint32_t gn_local_tag;
 };
 
+/* Values for SCTP_ACCEPT_ZERO_CHECKSUM */
+#define SCTP_EDMID_NONE             0
+#define SCTP_EDMID_LOWER_LAYER_DTLS 1
+
 /* Debugging logs */
 struct sctp_str_log {
 	void *stcb;		/* FIXME: LP64 issue */
diff --git a/sys/netinet/sctp_usrreq.c b/sys/netinet/sctp_usrreq.c
index 917d301a85dc..d603ef5efcd6 100644
--- a/sys/netinet/sctp_usrreq.c
+++ b/sys/netinet/sctp_usrreq.c
@@ -6839,13 +6839,15 @@ sctp_setopt(struct socket *so, int optname, void *optval, size_t optsize,
 			uint32_t *value;
 
 			SCTP_CHECK_AND_CAST(value, optval, uint32_t, optsize);
-			SCTP_INP_WLOCK(inp);
-			if (*value != 0) {
-				inp->zero_checksum = 1;
+			if ((*value == SCTP_EDMID_NONE) ||
+			    (*value == SCTP_EDMID_LOWER_LAYER_DTLS)) {
+				SCTP_INP_WLOCK(inp);
+				inp->rcv_edmid = *value;
+				SCTP_INP_WUNLOCK(inp);
 			} else {
-				inp->zero_checksum = 0;
+				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_USRREQ, EINVAL);
+				error = EINVAL;
 			}
-			SCTP_INP_WUNLOCK(inp);
 			break;
 		}
 
diff --git a/sys/netinet/sctputil.c b/sys/netinet/sctputil.c
index 8bb0f7446b68..00a8953336de 100644
--- a/sys/netinet/sctputil.c
+++ b/sys/netinet/sctputil.c
@@ -1149,7 +1149,8 @@ sctp_init_asoc(struct sctp_inpcb *inp, struct sctp_tcb *stcb,
 	asoc->nrsack_supported = inp->nrsack_supported;
 	asoc->pktdrop_supported = inp->pktdrop_supported;
 	asoc->idata_supported = inp->idata_supported;
-	asoc->zero_checksum = inp->zero_checksum;
+	asoc->rcv_edmid = inp->rcv_edmid;
+	asoc->snd_edmid = SCTP_EDMID_NONE;
 	asoc->sctp_cmt_pf = (uint8_t)0;
 	asoc->sctp_frag_point = inp->sctp_frag_point;
 	asoc->sctp_features = inp->sctp_features;