Skip to content

Commit

Permalink
Update zero checksum support
Browse files Browse the repository at this point in the history
  • Loading branch information
tuexen committed Jul 23, 2023
1 parent ac559d2 commit 95ac813
Show file tree
Hide file tree
Showing 10 changed files with 130 additions and 44 deletions.
9 changes: 8 additions & 1 deletion usrsctplib/netinet/sctp_header.h
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,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
Expand Down Expand Up @@ -539,6 +539,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
Expand Down
11 changes: 8 additions & 3 deletions usrsctplib/netinet/sctp_input.c
Original file line number Diff line number Diff line change
Expand Up @@ -2083,7 +2083,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);
Expand Down Expand Up @@ -2320,7 +2320,7 @@ sctp_process_cookie_new(struct mbuf *m, int iphlen, int offset,
#endif
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);

Expand Down Expand Up @@ -5861,9 +5861,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);
}
}
Expand Down
95 changes: 75 additions & 20 deletions usrsctplib/netinet/sctp_output.c
Original file line number Diff line number Diff line change
Expand Up @@ -5070,6 +5070,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;
Expand Down Expand Up @@ -5170,11 +5171,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;
}

Expand Down Expand Up @@ -5493,7 +5495,7 @@ sctp_arethere_unrecognized_parameters(struct mbuf *in_initpkt,
break;
case SCTP_HAS_NAT_SUPPORT:
*nat_friendly = 1;
/* fall through */
/* FALLTHROUGH */
case SCTP_PRSCTP_SUPPORTED:
if (padded_size != sizeof(struct sctp_paramhdr)) {
SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error prsctp/nat support %d\n", plen);
Expand Down Expand Up @@ -5962,6 +5964,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;
Expand Down Expand Up @@ -6350,9 +6353,9 @@ sctp_send_initiate_ack(struct sctp_inpcb *inp, struct sctp_tcb *stcb,
}
}
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 *);
Expand Down Expand Up @@ -6477,12 +6480,17 @@ sctp_send_initiate_ack(struct sctp_inpcb *inp, struct sctp_tcb *stcb,
}

/* 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;
}

Expand Down Expand Up @@ -9018,7 +9026,14 @@ sctp_med_chunk_output(struct sctp_inpcb *inp,
* 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;
Expand Down Expand Up @@ -9352,8 +9367,15 @@ sctp_med_chunk_output(struct sctp_inpcb *inp,
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);
Expand Down Expand Up @@ -10096,7 +10118,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;
Expand Down Expand Up @@ -10387,6 +10416,14 @@ sctp_chunk_retransmission(struct sctp_inpcb *inp,
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,
Expand All @@ -10397,7 +10434,7 @@ sctp_chunk_retransmission(struct sctp_inpcb *inp,
#if defined(__FreeBSD__) && !defined(__Userspace__)
0, 0,
#endif
asoc->zero_checksum == 2,
use_zero_crc,
so_locked))) {
/* error, we could not output */
SCTPDBG(SCTP_DEBUG_OUTPUT3, "Gak send error %d\n", error);
Expand Down Expand Up @@ -11493,6 +11530,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;

#if defined(__APPLE__) && !defined(__Userspace__)
if (so_locked) {
Expand All @@ -11514,6 +11552,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) {
Expand Down Expand Up @@ -11578,7 +11624,7 @@ sctp_send_abort_tcb(struct sctp_tcb *stcb, struct mbuf *operr, int so_locked)
#if defined(__FreeBSD__) && !defined(__Userspace__)
0, 0,
#endif
stcb->asoc.zero_checksum == 2,
use_zero_crc,
so_locked))) {
SCTPDBG(SCTP_DEBUG_OUTPUT3, "Gak send error %d\n", error);
if (error == ENOBUFS) {
Expand All @@ -11602,6 +11648,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) {
Expand All @@ -11615,6 +11662,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;
Expand All @@ -11629,7 +11684,7 @@ sctp_send_shutdown_complete(struct sctp_tcb *stcb,
#if defined(__FreeBSD__) && !defined(__Userspace__)
0, 0,
#endif
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) {
Expand Down
24 changes: 17 additions & 7 deletions usrsctplib/netinet/sctp_pcb.c
Original file line number Diff line number Diff line change
Expand Up @@ -2797,7 +2797,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;

#if defined(__FreeBSD__) && !defined(__Userspace__)
inp->fibnum = so->so_fibnum;
Expand Down Expand Up @@ -7412,12 +7412,22 @@ 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 */
Expand Down
2 changes: 1 addition & 1 deletion usrsctplib/netinet/sctp_pcb.h
Original file line number Diff line number Diff line change
Expand Up @@ -496,7 +496,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
Expand Down
9 changes: 3 additions & 6 deletions usrsctplib/netinet/sctp_structs.h
Original file line number Diff line number Diff line change
Expand Up @@ -1234,12 +1234,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;
Expand Down
4 changes: 4 additions & 0 deletions usrsctplib/netinet/sctp_uio.h
Original file line number Diff line number Diff line change
Expand Up @@ -810,6 +810,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 */
Expand Down
12 changes: 7 additions & 5 deletions usrsctplib/netinet/sctp_usrreq.c
Original file line number Diff line number Diff line change
Expand Up @@ -7800,13 +7800,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;
}

Expand Down
3 changes: 2 additions & 1 deletion usrsctplib/netinet/sctputil.c
Original file line number Diff line number Diff line change
Expand Up @@ -1178,7 +1178,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;
Expand Down
5 changes: 5 additions & 0 deletions usrsctplib/usrsctp.h
Original file line number Diff line number Diff line change
Expand Up @@ -560,6 +560,7 @@ struct sctp_event_subscribe {
#define SCTP_NRSACK_SUPPORTED 0x00000030
#define SCTP_PKTDROP_SUPPORTED 0x00000031
#define SCTP_MAX_CWND 0x00000032
#define SCTP_ACCEPT_ZERO_CHECKSUM 0x00000033

#define SCTP_ENABLE_STREAM_RESET 0x00000900 /* struct sctp_assoc_value */

Expand Down Expand Up @@ -767,6 +768,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


/*
* Main SCTP chunk types
Expand Down

0 comments on commit 95ac813

Please sign in to comment.