xref: /freebsd/sys/dev/cxgbe/tom/t4_tls.c (revision 105fd928b0b5b35ab529e5f6914788dc49582901)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2017-2018 Chelsio Communications, Inc.
5  * All rights reserved.
6  * Written by: John Baldwin <jhb@FreeBSD.org>
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  */
29 
30 #include "opt_inet.h"
31 #include "opt_kern_tls.h"
32 
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
35 
36 #ifdef KERN_TLS
37 #include <sys/param.h>
38 #include <sys/ktr.h>
39 #include <sys/ktls.h>
40 #include <sys/sglist.h>
41 #include <sys/socket.h>
42 #include <sys/socketvar.h>
43 #include <sys/systm.h>
44 #include <netinet/in.h>
45 #include <netinet/in_pcb.h>
46 #include <netinet/tcp_var.h>
47 #include <netinet/toecore.h>
48 #include <opencrypto/cryptodev.h>
49 #include <opencrypto/xform.h>
50 
51 #ifdef TCP_OFFLOAD
52 #include "common/common.h"
53 #include "common/t4_tcb.h"
54 #include "crypto/t4_crypto.h"
55 #include "tom/t4_tom_l2t.h"
56 #include "tom/t4_tom.h"
57 
58 /*
59  * The TCP sequence number of a CPL_TLS_DATA mbuf is saved here while
60  * the mbuf is in the ulp_pdu_reclaimq.
61  */
62 #define	tls_tcp_seq	PH_loc.thirtytwo[0]
63 
64 static void
65 t4_set_tls_tcb_field(struct toepcb *toep, uint16_t word, uint64_t mask,
66     uint64_t val)
67 {
68 	struct adapter *sc = td_adapter(toep->td);
69 
70 	t4_set_tcb_field(sc, &toep->ofld_txq->wrq, toep, word, mask, val, 0, 0);
71 }
72 
73 /* TLS and DTLS common routines */
74 bool
75 can_tls_offload(struct adapter *sc)
76 {
77 
78 	return (sc->tt.tls && sc->cryptocaps & FW_CAPS_CONFIG_TLSKEYS);
79 }
80 
81 int
82 tls_tx_key(struct toepcb *toep)
83 {
84 	struct tls_ofld_info *tls_ofld = &toep->tls;
85 
86 	return (tls_ofld->tx_key_addr >= 0);
87 }
88 
89 /* Set TLS Key-Id in TCB */
90 static void
91 t4_set_tls_keyid(struct toepcb *toep, unsigned int key_id)
92 {
93 
94 	t4_set_tls_tcb_field(toep, W_TCB_RX_TLS_KEY_TAG,
95 			 V_TCB_RX_TLS_KEY_TAG(M_TCB_RX_TLS_BUF_TAG),
96 			 V_TCB_RX_TLS_KEY_TAG(key_id));
97 }
98 
99 /* Clear TF_RX_QUIESCE to re-enable receive. */
100 static void
101 t4_clear_rx_quiesce(struct toepcb *toep)
102 {
103 
104 	t4_set_tls_tcb_field(toep, W_TCB_T_FLAGS, V_TF_RX_QUIESCE(1), 0);
105 }
106 
107 static void
108 tls_clr_ofld_mode(struct toepcb *toep)
109 {
110 
111 	tls_stop_handshake_timer(toep);
112 
113 	KASSERT(toep->tls.rx_key_addr == -1,
114 	    ("%s: tid %d has RX key", __func__, toep->tid));
115 
116 	/* Switch to plain TOE mode. */
117 	t4_set_tls_tcb_field(toep, W_TCB_ULP_RAW,
118 	    V_TCB_ULP_RAW(V_TF_TLS_ENABLE(1)),
119 	    V_TCB_ULP_RAW(V_TF_TLS_ENABLE(0)));
120 	t4_set_tls_tcb_field(toep, W_TCB_ULP_TYPE,
121 	    V_TCB_ULP_TYPE(M_TCB_ULP_TYPE), V_TCB_ULP_TYPE(ULP_MODE_NONE));
122 	t4_clear_rx_quiesce(toep);
123 
124 	toep->flags &= ~(TPF_FORCE_CREDITS | TPF_TLS_ESTABLISHED);
125 	toep->params.ulp_mode = ULP_MODE_NONE;
126 }
127 
128 /* TLS/DTLS content type  for CPL SFO */
129 static inline unsigned char
130 tls_content_type(unsigned char content_type)
131 {
132 	/*
133 	 * XXX: Shouldn't this map CONTENT_TYPE_APP_DATA to DATA and
134 	 * default to "CUSTOM" for all other types including
135 	 * heartbeat?
136 	 */
137 	switch (content_type) {
138 	case CONTENT_TYPE_CCS:
139 		return CPL_TX_TLS_SFO_TYPE_CCS;
140 	case CONTENT_TYPE_ALERT:
141 		return CPL_TX_TLS_SFO_TYPE_ALERT;
142 	case CONTENT_TYPE_HANDSHAKE:
143 		return CPL_TX_TLS_SFO_TYPE_HANDSHAKE;
144 	case CONTENT_TYPE_HEARTBEAT:
145 		return CPL_TX_TLS_SFO_TYPE_HEARTBEAT;
146 	}
147 	return CPL_TX_TLS_SFO_TYPE_DATA;
148 }
149 
150 /* TLS Key memory management */
151 static void
152 clear_tls_keyid(struct toepcb *toep)
153 {
154 	struct tls_ofld_info *tls_ofld = &toep->tls;
155 	struct adapter *sc = td_adapter(toep->td);
156 
157 	if (tls_ofld->rx_key_addr >= 0) {
158 		t4_free_tls_keyid(sc, tls_ofld->rx_key_addr);
159 		tls_ofld->rx_key_addr = -1;
160 	}
161 	if (tls_ofld->tx_key_addr >= 0) {
162 		t4_free_tls_keyid(sc, tls_ofld->tx_key_addr);
163 		tls_ofld->tx_key_addr = -1;
164 	}
165 }
166 
167 static int
168 get_tp_plen_max(struct ktls_session *tls)
169 {
170 	int plen = ((min(3*4096, TP_TX_PG_SZ))/1448) * 1448;
171 
172 	return (tls->params.max_frame_len <= 8192 ? plen : FC_TP_PLEN_MAX);
173 }
174 
175 /* Send request to get the key-id */
176 static int
177 tls_program_key_id(struct toepcb *toep, struct ktls_session *tls,
178     int direction)
179 {
180 	struct tls_ofld_info *tls_ofld = &toep->tls;
181 	struct adapter *sc = td_adapter(toep->td);
182 	struct ofld_tx_sdesc *txsd;
183 	int keyid;
184 	struct wrqe *wr;
185 	struct tls_key_req *kwr;
186 	struct tls_keyctx *kctx;
187 
188 #ifdef INVARIANTS
189 	int kwrlen, kctxlen, len;
190 
191 	kwrlen = sizeof(*kwr);
192 	kctxlen = roundup2(sizeof(*kctx), 32);
193 	len = roundup2(kwrlen + kctxlen, 16);
194 	MPASS(TLS_KEY_WR_SZ == len);
195 #endif
196 	if (toep->txsd_avail == 0)
197 		return (EAGAIN);
198 
199 	if ((keyid = t4_alloc_tls_keyid(sc)) < 0) {
200 		return (ENOSPC);
201 	}
202 
203 	wr = alloc_wrqe(TLS_KEY_WR_SZ, &toep->ofld_txq->wrq);
204 	if (wr == NULL) {
205 		t4_free_tls_keyid(sc, keyid);
206 		return (ENOMEM);
207 	}
208 	kwr = wrtod(wr);
209 	memset(kwr, 0, TLS_KEY_WR_SZ);
210 
211 	t4_write_tlskey_wr(tls, direction, toep->tid, F_FW_WR_COMPL, keyid,
212 	    kwr);
213 	kctx = (struct tls_keyctx *)(kwr + 1);
214 	if (direction == KTLS_TX)
215 		tls_ofld->tx_key_addr = keyid;
216 	else
217 		tls_ofld->rx_key_addr = keyid;
218 	t4_tls_key_ctx(tls, direction, kctx);
219 
220 	txsd = &toep->txsd[toep->txsd_pidx];
221 	txsd->tx_credits = DIV_ROUND_UP(TLS_KEY_WR_SZ, 16);
222 	txsd->plen = 0;
223 	toep->tx_credits -= txsd->tx_credits;
224 	if (__predict_false(++toep->txsd_pidx == toep->txsd_total))
225 		toep->txsd_pidx = 0;
226 	toep->txsd_avail--;
227 
228 	t4_wrq_tx(sc, wr);
229 
230 	return (0);
231 }
232 
233 /*
234  * In some cases a client connection can hang without sending the
235  * ServerHelloDone message from the NIC to the host.  Send a dummy
236  * RX_DATA_ACK with RX_MODULATE to unstick the connection.
237  */
238 static void
239 tls_send_handshake_ack(void *arg)
240 {
241 	struct toepcb *toep = arg;
242 	struct tls_ofld_info *tls_ofld = &toep->tls;
243 	struct adapter *sc = td_adapter(toep->td);
244 
245 	/* Bail without rescheduling if the connection has closed. */
246 	if ((toep->flags & (TPF_FIN_SENT | TPF_ABORT_SHUTDOWN)) != 0)
247 		return;
248 
249 	/*
250 	 * If this connection has timed out without receiving more
251 	 * data, downgrade to plain TOE mode and don't re-arm the
252 	 * timer.
253 	 */
254 	if (sc->tt.tls_rx_timeout != 0) {
255 		struct inpcb *inp;
256 		struct tcpcb *tp;
257 
258 		inp = toep->inp;
259 		tp = intotcpcb(inp);
260 		if ((ticks - tp->t_rcvtime) >= sc->tt.tls_rx_timeout) {
261 			CTR2(KTR_CXGBE, "%s: tid %d clr_ofld_mode", __func__,
262 			    toep->tid);
263 			tls_clr_ofld_mode(toep);
264 			return;
265 		}
266 	}
267 
268 	/*
269 	 * XXX: Does not have the t4_get_tcb() checks to refine the
270 	 * workaround.
271 	 */
272 	callout_schedule(&tls_ofld->handshake_timer, TLS_SRV_HELLO_RD_TM * hz);
273 
274 	CTR2(KTR_CXGBE, "%s: tid %d sending RX_DATA_ACK", __func__, toep->tid);
275 	send_rx_modulate(sc, toep);
276 }
277 
278 static void
279 tls_start_handshake_timer(struct toepcb *toep)
280 {
281 	struct tls_ofld_info *tls_ofld = &toep->tls;
282 
283 	INP_WLOCK_ASSERT(toep->inp);
284 	callout_reset(&tls_ofld->handshake_timer, TLS_SRV_HELLO_BKOFF_TM * hz,
285 	    tls_send_handshake_ack, toep);
286 }
287 
288 void
289 tls_stop_handshake_timer(struct toepcb *toep)
290 {
291 	struct tls_ofld_info *tls_ofld = &toep->tls;
292 
293 	INP_WLOCK_ASSERT(toep->inp);
294 	callout_stop(&tls_ofld->handshake_timer);
295 }
296 
297 int
298 tls_alloc_ktls(struct toepcb *toep, struct ktls_session *tls, int direction)
299 {
300 	struct adapter *sc = td_adapter(toep->td);
301 	int error, explicit_iv_size, key_offset, mac_first;
302 
303 	if (!can_tls_offload(td_adapter(toep->td)))
304 		return (EINVAL);
305 	switch (ulp_mode(toep)) {
306 	case ULP_MODE_TLS:
307 		break;
308 	case ULP_MODE_NONE:
309 	case ULP_MODE_TCPDDP:
310 		if (direction != KTLS_TX)
311 			return (EINVAL);
312 		break;
313 	default:
314 		return (EINVAL);
315 	}
316 
317 	switch (tls->params.cipher_algorithm) {
318 	case CRYPTO_AES_CBC:
319 		/* XXX: Explicitly ignore any provided IV. */
320 		switch (tls->params.cipher_key_len) {
321 		case 128 / 8:
322 		case 192 / 8:
323 		case 256 / 8:
324 			break;
325 		default:
326 			error = EINVAL;
327 			goto clr_ofld;
328 		}
329 		switch (tls->params.auth_algorithm) {
330 		case CRYPTO_SHA1_HMAC:
331 		case CRYPTO_SHA2_256_HMAC:
332 		case CRYPTO_SHA2_384_HMAC:
333 			break;
334 		default:
335 			error = EPROTONOSUPPORT;
336 			goto clr_ofld;
337 		}
338 		explicit_iv_size = AES_BLOCK_LEN;
339 		mac_first = 1;
340 		break;
341 	case CRYPTO_AES_NIST_GCM_16:
342 		if (tls->params.iv_len != SALT_SIZE) {
343 			error = EINVAL;
344 			goto clr_ofld;
345 		}
346 		switch (tls->params.cipher_key_len) {
347 		case 128 / 8:
348 		case 192 / 8:
349 		case 256 / 8:
350 			break;
351 		default:
352 			error = EINVAL;
353 			goto clr_ofld;
354 		}
355 		explicit_iv_size = 8;
356 		mac_first = 0;
357 		break;
358 	default:
359 		error = EPROTONOSUPPORT;
360 		goto clr_ofld;
361 	}
362 
363 	/* Only TLS 1.1 and TLS 1.2 are currently supported. */
364 	if (tls->params.tls_vmajor != TLS_MAJOR_VER_ONE ||
365 	    tls->params.tls_vminor < TLS_MINOR_VER_ONE ||
366 	    tls->params.tls_vminor > TLS_MINOR_VER_TWO) {
367 		error = EPROTONOSUPPORT;
368 		goto clr_ofld;
369 	}
370 
371 	/* Bail if we already have a key. */
372 	if (direction == KTLS_TX) {
373 		if (toep->tls.tx_key_addr != -1)
374 			return (EOPNOTSUPP);
375 	} else {
376 		if (toep->tls.rx_key_addr != -1)
377 			return (EOPNOTSUPP);
378 	}
379 
380 	error = tls_program_key_id(toep, tls, direction);
381 	if (error) {
382 		if (direction == KTLS_RX)
383 			goto clr_ofld;
384 		return (error);
385 	}
386 
387 	if (direction == KTLS_TX) {
388 		toep->tls.scmd0.seqno_numivs =
389 			(V_SCMD_SEQ_NO_CTRL(3) |
390 			 V_SCMD_PROTO_VERSION(t4_tls_proto_ver(tls)) |
391 			 V_SCMD_ENC_DEC_CTRL(SCMD_ENCDECCTRL_ENCRYPT) |
392 			 V_SCMD_CIPH_AUTH_SEQ_CTRL((mac_first == 0)) |
393 			 V_SCMD_CIPH_MODE(t4_tls_cipher_mode(tls)) |
394 			 V_SCMD_AUTH_MODE(t4_tls_auth_mode(tls)) |
395 			 V_SCMD_HMAC_CTRL(t4_tls_hmac_ctrl(tls)) |
396 			 V_SCMD_IV_SIZE(explicit_iv_size / 2));
397 
398 		toep->tls.scmd0.ivgen_hdrlen =
399 			(V_SCMD_IV_GEN_CTRL(1) |
400 			 V_SCMD_KEY_CTX_INLINE(0) |
401 			 V_SCMD_TLS_FRAG_ENABLE(1));
402 
403 		if (tls->params.cipher_algorithm == CRYPTO_AES_NIST_GCM_16)
404 			toep->tls.iv_len = 8;
405 		else
406 			toep->tls.iv_len = AES_BLOCK_LEN;
407 
408 		toep->tls.frag_size = tls->params.max_frame_len;
409 		toep->tls.fcplenmax = get_tp_plen_max(tls);
410 		toep->tls.expn_per_ulp = tls->params.tls_hlen +
411 		    tls->params.tls_tlen;
412 		toep->tls.pdus_per_ulp = 1;
413 		toep->tls.adjusted_plen = toep->tls.expn_per_ulp +
414 		    tls->params.max_frame_len;
415 		toep->tls.tx_key_info_size = t4_tls_key_info_size(tls);
416 	} else {
417 		/* Stop timer on handshake completion */
418 		tls_stop_handshake_timer(toep);
419 
420 		toep->flags &= ~TPF_FORCE_CREDITS;
421 		toep->flags |= TPF_TLS_RECEIVE;
422 		toep->tls.rx_version = tls->params.tls_vmajor << 8 |
423 		    tls->params.tls_vminor;
424 
425 		/*
426 		 * RX key tags are an index into the key portion of MA
427 		 * memory stored as an offset from the base address in
428 		 * units of 64 bytes.
429 		 */
430 		key_offset = toep->tls.rx_key_addr - sc->vres.key.start;
431 		t4_set_tls_keyid(toep, key_offset / 64);
432 		t4_set_tls_tcb_field(toep, W_TCB_ULP_RAW,
433 				 V_TCB_ULP_RAW(M_TCB_ULP_RAW),
434 				 V_TCB_ULP_RAW((V_TF_TLS_KEY_SIZE(3) |
435 						V_TF_TLS_CONTROL(1) |
436 						V_TF_TLS_ACTIVE(1) |
437 						V_TF_TLS_ENABLE(1))));
438 		t4_set_tls_tcb_field(toep, W_TCB_TLS_SEQ,
439 				 V_TCB_TLS_SEQ(M_TCB_TLS_SEQ),
440 				 V_TCB_TLS_SEQ(0));
441 		t4_clear_rx_quiesce(toep);
442 	}
443 
444 	return (0);
445 
446 clr_ofld:
447 	if (ulp_mode(toep) == ULP_MODE_TLS) {
448 		CTR2(KTR_CXGBE, "%s: tid %d clr_ofld_mode", __func__,
449 		    toep->tid);
450 		tls_clr_ofld_mode(toep);
451 	}
452 	return (error);
453 }
454 
455 void
456 tls_init_toep(struct toepcb *toep)
457 {
458 	struct tls_ofld_info *tls_ofld = &toep->tls;
459 
460 	tls_ofld->rx_key_addr = -1;
461 	tls_ofld->tx_key_addr = -1;
462 }
463 
464 void
465 tls_establish(struct toepcb *toep)
466 {
467 
468 	/*
469 	 * Enable PDU extraction.
470 	 *
471 	 * XXX: Supposedly this should be done by the firmware when
472 	 * the ULP_MODE FLOWC parameter is set in send_flowc_wr(), but
473 	 * in practice this seems to be required.
474 	 */
475 	CTR2(KTR_CXGBE, "%s: tid %d setting TLS_ENABLE", __func__, toep->tid);
476 	t4_set_tls_tcb_field(toep, W_TCB_ULP_RAW, V_TCB_ULP_RAW(M_TCB_ULP_RAW),
477 	    V_TCB_ULP_RAW(V_TF_TLS_ENABLE(1)));
478 
479 	toep->flags |= TPF_FORCE_CREDITS | TPF_TLS_ESTABLISHED;
480 
481 	callout_init_rw(&toep->tls.handshake_timer, &toep->inp->inp_lock, 0);
482 	tls_start_handshake_timer(toep);
483 }
484 
485 void
486 tls_detach(struct toepcb *toep)
487 {
488 
489 	if (toep->flags & TPF_TLS_ESTABLISHED) {
490 		tls_stop_handshake_timer(toep);
491 		toep->flags &= ~TPF_TLS_ESTABLISHED;
492 	}
493 }
494 
495 void
496 tls_uninit_toep(struct toepcb *toep)
497 {
498 
499 	MPASS((toep->flags & TPF_TLS_ESTABLISHED) == 0);
500 	clear_tls_keyid(toep);
501 }
502 
503 #define MAX_OFLD_TX_CREDITS (SGE_MAX_WR_LEN / 16)
504 #define	MIN_OFLD_TLSTX_CREDITS(toep)					\
505 	(howmany(sizeof(struct fw_tlstx_data_wr) + 			\
506 	    sizeof(struct cpl_tx_tls_sfo) + sizeof(struct ulptx_idata) + \
507 	    sizeof(struct ulptx_sc_memrd) +				\
508 	    AES_BLOCK_LEN + 1, 16))
509 
510 static void
511 write_tlstx_wr(struct fw_tlstx_data_wr *txwr, struct toepcb *toep,
512     unsigned int immdlen, unsigned int plen, unsigned int expn,
513     unsigned int pdus, uint8_t credits, int shove, int imm_ivs)
514 {
515 	struct tls_ofld_info *tls_ofld = &toep->tls;
516 	unsigned int len = plen + expn;
517 
518 	txwr->op_to_immdlen = htobe32(V_WR_OP(FW_TLSTX_DATA_WR) |
519 	    V_FW_TLSTX_DATA_WR_COMPL(1) |
520 	    V_FW_TLSTX_DATA_WR_IMMDLEN(immdlen));
521 	txwr->flowid_len16 = htobe32(V_FW_TLSTX_DATA_WR_FLOWID(toep->tid) |
522 	    V_FW_TLSTX_DATA_WR_LEN16(credits));
523 	txwr->plen = htobe32(len);
524 	txwr->lsodisable_to_flags = htobe32(V_TX_ULP_MODE(ULP_MODE_TLS) |
525 	    V_TX_URG(0) | /* F_T6_TX_FORCE | */ V_TX_SHOVE(shove));
526 	txwr->ctxloc_to_exp = htobe32(V_FW_TLSTX_DATA_WR_NUMIVS(pdus) |
527 	    V_FW_TLSTX_DATA_WR_EXP(expn) |
528 	    V_FW_TLSTX_DATA_WR_CTXLOC(TLS_SFO_WR_CONTEXTLOC_DDR) |
529 	    V_FW_TLSTX_DATA_WR_IVDSGL(!imm_ivs) |
530 	    V_FW_TLSTX_DATA_WR_KEYSIZE(tls_ofld->tx_key_info_size >> 4));
531 	txwr->mfs = htobe16(tls_ofld->frag_size);
532 	txwr->adjustedplen_pkd = htobe16(
533 	    V_FW_TLSTX_DATA_WR_ADJUSTEDPLEN(tls_ofld->adjusted_plen));
534 	txwr->expinplenmax_pkd = htobe16(
535 	    V_FW_TLSTX_DATA_WR_EXPINPLENMAX(tls_ofld->expn_per_ulp));
536 	txwr->pdusinplenmax_pkd =
537 	    V_FW_TLSTX_DATA_WR_PDUSINPLENMAX(tls_ofld->pdus_per_ulp);
538 }
539 
540 static void
541 write_tlstx_cpl(struct cpl_tx_tls_sfo *cpl, struct toepcb *toep,
542     struct tls_hdr *tls_hdr, unsigned int plen, unsigned int pdus)
543 {
544 	struct tls_ofld_info *tls_ofld = &toep->tls;
545 	int data_type, seglen;
546 
547 	if (plen < tls_ofld->frag_size)
548 		seglen = plen;
549 	else
550 		seglen = tls_ofld->frag_size;
551 	data_type = tls_content_type(tls_hdr->type);
552 	cpl->op_to_seg_len = htobe32(V_CPL_TX_TLS_SFO_OPCODE(CPL_TX_TLS_SFO) |
553 	    V_CPL_TX_TLS_SFO_DATA_TYPE(data_type) |
554 	    V_CPL_TX_TLS_SFO_CPL_LEN(2) | V_CPL_TX_TLS_SFO_SEG_LEN(seglen));
555 	cpl->pld_len = htobe32(plen);
556 	if (data_type == CPL_TX_TLS_SFO_TYPE_HEARTBEAT)
557 		cpl->type_protover = htobe32(
558 		    V_CPL_TX_TLS_SFO_TYPE(tls_hdr->type));
559 	cpl->seqno_numivs = htobe32(tls_ofld->scmd0.seqno_numivs |
560 	    V_SCMD_NUM_IVS(pdus));
561 	cpl->ivgen_hdrlen = htobe32(tls_ofld->scmd0.ivgen_hdrlen);
562 	cpl->scmd1 = htobe64(tls_ofld->tx_seq_no);
563 	tls_ofld->tx_seq_no += pdus;
564 }
565 
566 static int
567 count_ext_pgs_segs(struct mbuf *m)
568 {
569 	vm_paddr_t nextpa;
570 	u_int i, nsegs;
571 
572 	MPASS(m->m_epg_npgs > 0);
573 	nsegs = 1;
574 	nextpa = m->m_epg_pa[0] + PAGE_SIZE;
575 	for (i = 1; i < m->m_epg_npgs; i++) {
576 		if (nextpa != m->m_epg_pa[i])
577 			nsegs++;
578 		nextpa = m->m_epg_pa[i] + PAGE_SIZE;
579 	}
580 	return (nsegs);
581 }
582 
583 static void
584 write_ktlstx_sgl(void *dst, struct mbuf *m, int nsegs)
585 {
586 	struct ulptx_sgl *usgl = dst;
587 	vm_paddr_t pa;
588 	uint32_t len;
589 	int i, j;
590 
591 	KASSERT(nsegs > 0, ("%s: nsegs 0", __func__));
592 
593 	usgl->cmd_nsge = htobe32(V_ULPTX_CMD(ULP_TX_SC_DSGL) |
594 	    V_ULPTX_NSGE(nsegs));
595 
596 	/* Figure out the first S/G length. */
597 	pa = m->m_epg_pa[0] + m->m_epg_1st_off;
598 	usgl->addr0 = htobe64(pa);
599 	len = m_epg_pagelen(m, 0, m->m_epg_1st_off);
600 	pa += len;
601 	for (i = 1; i < m->m_epg_npgs; i++) {
602 		if (m->m_epg_pa[i] != pa)
603 			break;
604 		len += m_epg_pagelen(m, i, 0);
605 		pa += m_epg_pagelen(m, i, 0);
606 	}
607 	usgl->len0 = htobe32(len);
608 #ifdef INVARIANTS
609 	nsegs--;
610 #endif
611 
612 	j = -1;
613 	for (; i < m->m_epg_npgs; i++) {
614 		if (j == -1 || m->m_epg_pa[i] != pa) {
615 			if (j >= 0)
616 				usgl->sge[j / 2].len[j & 1] = htobe32(len);
617 			j++;
618 #ifdef INVARIANTS
619 			nsegs--;
620 #endif
621 			pa = m->m_epg_pa[i];
622 			usgl->sge[j / 2].addr[j & 1] = htobe64(pa);
623 			len = m_epg_pagelen(m, i, 0);
624 			pa += len;
625 		} else {
626 			len += m_epg_pagelen(m, i, 0);
627 			pa += m_epg_pagelen(m, i, 0);
628 		}
629 	}
630 	if (j >= 0) {
631 		usgl->sge[j / 2].len[j & 1] = htobe32(len);
632 
633 		if ((j & 1) == 0)
634 			usgl->sge[j / 2].len[1] = htobe32(0);
635 	}
636 	KASSERT(nsegs == 0, ("%s: nsegs %d, m %p", __func__, nsegs, m));
637 }
638 
639 /*
640  * Similar to t4_push_frames() but handles sockets that contain TLS
641  * record mbufs.
642  */
643 void
644 t4_push_ktls(struct adapter *sc, struct toepcb *toep, int drop)
645 {
646 	struct tls_hdr *thdr;
647 	struct fw_tlstx_data_wr *txwr;
648 	struct cpl_tx_tls_sfo *cpl;
649 	struct ulptx_idata *idata;
650 	struct ulptx_sc_memrd *memrd;
651 	struct wrqe *wr;
652 	struct mbuf *m;
653 	u_int nsegs, credits, wr_len;
654 	u_int expn_size;
655 	struct inpcb *inp = toep->inp;
656 	struct tcpcb *tp = intotcpcb(inp);
657 	struct socket *so = inp->inp_socket;
658 	struct sockbuf *sb = &so->so_snd;
659 	int tls_size, tx_credits, shove, sowwakeup;
660 	struct ofld_tx_sdesc *txsd;
661 	char *buf;
662 
663 	INP_WLOCK_ASSERT(inp);
664 	KASSERT(toep->flags & TPF_FLOWC_WR_SENT,
665 	    ("%s: flowc_wr not sent for tid %u.", __func__, toep->tid));
666 
667 	KASSERT(ulp_mode(toep) == ULP_MODE_NONE ||
668 	    ulp_mode(toep) == ULP_MODE_TCPDDP || ulp_mode(toep) == ULP_MODE_TLS,
669 	    ("%s: ulp_mode %u for toep %p", __func__, ulp_mode(toep), toep));
670 	KASSERT(tls_tx_key(toep),
671 	    ("%s: TX key not set for toep %p", __func__, toep));
672 
673 #ifdef VERBOSE_TRACES
674 	CTR4(KTR_CXGBE, "%s: tid %d toep flags %#x tp flags %#x drop %d",
675 	    __func__, toep->tid, toep->flags, tp->t_flags);
676 #endif
677 	if (__predict_false(toep->flags & TPF_ABORT_SHUTDOWN))
678 		return;
679 
680 #ifdef RATELIMIT
681 	if (__predict_false(inp->inp_flags2 & INP_RATE_LIMIT_CHANGED) &&
682 	    (update_tx_rate_limit(sc, toep, so->so_max_pacing_rate) == 0)) {
683 		inp->inp_flags2 &= ~INP_RATE_LIMIT_CHANGED;
684 	}
685 #endif
686 
687 	/*
688 	 * This function doesn't resume by itself.  Someone else must clear the
689 	 * flag and call this function.
690 	 */
691 	if (__predict_false(toep->flags & TPF_TX_SUSPENDED)) {
692 		KASSERT(drop == 0,
693 		    ("%s: drop (%d) != 0 but tx is suspended", __func__, drop));
694 		return;
695 	}
696 
697 	txsd = &toep->txsd[toep->txsd_pidx];
698 	for (;;) {
699 		tx_credits = min(toep->tx_credits, MAX_OFLD_TX_CREDITS);
700 
701 		SOCKBUF_LOCK(sb);
702 		sowwakeup = drop;
703 		if (drop) {
704 			sbdrop_locked(sb, drop);
705 			drop = 0;
706 		}
707 
708 		m = sb->sb_sndptr != NULL ? sb->sb_sndptr->m_next : sb->sb_mb;
709 
710 		/*
711 		 * Send a FIN if requested, but only if there's no
712 		 * more data to send.
713 		 */
714 		if (m == NULL && toep->flags & TPF_SEND_FIN) {
715 			if (sowwakeup)
716 				sowwakeup_locked(so);
717 			else
718 				SOCKBUF_UNLOCK(sb);
719 			SOCKBUF_UNLOCK_ASSERT(sb);
720 			t4_close_conn(sc, toep);
721 			return;
722 		}
723 
724 		/*
725 		 * If there is no ready data to send, wait until more
726 		 * data arrives.
727 		 */
728 		if (m == NULL || (m->m_flags & M_NOTAVAIL) != 0) {
729 			if (sowwakeup)
730 				sowwakeup_locked(so);
731 			else
732 				SOCKBUF_UNLOCK(sb);
733 			SOCKBUF_UNLOCK_ASSERT(sb);
734 #ifdef VERBOSE_TRACES
735 			CTR2(KTR_CXGBE, "%s: tid %d no ready data to send",
736 			    __func__, toep->tid);
737 #endif
738 			return;
739 		}
740 
741 		KASSERT(m->m_flags & M_EXTPG, ("%s: mbuf %p is not NOMAP",
742 		    __func__, m));
743 		KASSERT(m->m_epg_tls != NULL,
744 		    ("%s: mbuf %p doesn't have TLS session", __func__, m));
745 
746 		/* Calculate WR length. */
747 		wr_len = sizeof(struct fw_tlstx_data_wr) +
748 		    sizeof(struct cpl_tx_tls_sfo) +
749 		    sizeof(struct ulptx_idata) + sizeof(struct ulptx_sc_memrd);
750 
751 		/* Explicit IVs for AES-CBC and AES-GCM are <= 16. */
752 		MPASS(toep->tls.iv_len <= AES_BLOCK_LEN);
753 		wr_len += AES_BLOCK_LEN;
754 
755 		/* Account for SGL in work request length. */
756 		nsegs = count_ext_pgs_segs(m);
757 		wr_len += sizeof(struct ulptx_sgl) +
758 		    ((3 * (nsegs - 1)) / 2 + ((nsegs - 1) & 1)) * 8;
759 
760 		/* Not enough credits for this work request. */
761 		if (howmany(wr_len, 16) > tx_credits) {
762 			if (sowwakeup)
763 				sowwakeup_locked(so);
764 			else
765 				SOCKBUF_UNLOCK(sb);
766 			SOCKBUF_UNLOCK_ASSERT(sb);
767 #ifdef VERBOSE_TRACES
768 			CTR5(KTR_CXGBE,
769 	    "%s: tid %d mbuf %p requires %d credits, but only %d available",
770 			    __func__, toep->tid, m, howmany(wr_len, 16),
771 			    tx_credits);
772 #endif
773 			toep->flags |= TPF_TX_SUSPENDED;
774 			return;
775 		}
776 
777 		/* Shove if there is no additional data pending. */
778 		shove = ((m->m_next == NULL ||
779 		    (m->m_next->m_flags & M_NOTAVAIL) != 0)) &&
780 		    (tp->t_flags & TF_MORETOCOME) == 0;
781 
782 		if (sb->sb_flags & SB_AUTOSIZE &&
783 		    V_tcp_do_autosndbuf &&
784 		    sb->sb_hiwat < V_tcp_autosndbuf_max &&
785 		    sbused(sb) >= sb->sb_hiwat * 7 / 8) {
786 			int newsize = min(sb->sb_hiwat + V_tcp_autosndbuf_inc,
787 			    V_tcp_autosndbuf_max);
788 
789 			if (!sbreserve_locked(sb, newsize, so, NULL))
790 				sb->sb_flags &= ~SB_AUTOSIZE;
791 			else
792 				sowwakeup = 1;	/* room available */
793 		}
794 		if (sowwakeup)
795 			sowwakeup_locked(so);
796 		else
797 			SOCKBUF_UNLOCK(sb);
798 		SOCKBUF_UNLOCK_ASSERT(sb);
799 
800 		if (__predict_false(toep->flags & TPF_FIN_SENT))
801 			panic("%s: excess tx.", __func__);
802 
803 		wr = alloc_wrqe(roundup2(wr_len, 16), &toep->ofld_txq->wrq);
804 		if (wr == NULL) {
805 			/* XXX: how will we recover from this? */
806 			toep->flags |= TPF_TX_SUSPENDED;
807 			return;
808 		}
809 
810 		thdr = (struct tls_hdr *)&m->m_epg_hdr;
811 #ifdef VERBOSE_TRACES
812 		CTR5(KTR_CXGBE, "%s: tid %d TLS record %ju type %d len %#x",
813 		    __func__, toep->tid, m->m_epg_seqno, thdr->type,
814 		    m->m_len);
815 #endif
816 		txwr = wrtod(wr);
817 		cpl = (struct cpl_tx_tls_sfo *)(txwr + 1);
818 		memset(txwr, 0, roundup2(wr_len, 16));
819 		credits = howmany(wr_len, 16);
820 		expn_size = m->m_epg_hdrlen +
821 		    m->m_epg_trllen;
822 		tls_size = m->m_len - expn_size;
823 		write_tlstx_wr(txwr, toep, 0,
824 		    tls_size, expn_size, 1, credits, shove, 1);
825 		toep->tls.tx_seq_no = m->m_epg_seqno;
826 		write_tlstx_cpl(cpl, toep, thdr, tls_size, 1);
827 
828 		idata = (struct ulptx_idata *)(cpl + 1);
829 		idata->cmd_more = htobe32(V_ULPTX_CMD(ULP_TX_SC_NOOP));
830 		idata->len = htobe32(0);
831 		memrd = (struct ulptx_sc_memrd *)(idata + 1);
832 		memrd->cmd_to_len = htobe32(V_ULPTX_CMD(ULP_TX_SC_MEMRD) |
833 		    V_ULP_TX_SC_MORE(1) |
834 		    V_ULPTX_LEN16(toep->tls.tx_key_info_size >> 4));
835 		memrd->addr = htobe32(toep->tls.tx_key_addr >> 5);
836 
837 		/* Copy IV. */
838 		buf = (char *)(memrd + 1);
839 		memcpy(buf, thdr + 1, toep->tls.iv_len);
840 		buf += AES_BLOCK_LEN;
841 
842 		write_ktlstx_sgl(buf, m, nsegs);
843 
844 		KASSERT(toep->tx_credits >= credits,
845 			("%s: not enough credits", __func__));
846 
847 		toep->tx_credits -= credits;
848 
849 		tp->snd_nxt += m->m_len;
850 		tp->snd_max += m->m_len;
851 
852 		SOCKBUF_LOCK(sb);
853 		sb->sb_sndptr = m;
854 		SOCKBUF_UNLOCK(sb);
855 
856 		toep->flags |= TPF_TX_DATA_SENT;
857 		if (toep->tx_credits < MIN_OFLD_TLSTX_CREDITS(toep))
858 			toep->flags |= TPF_TX_SUSPENDED;
859 
860 		KASSERT(toep->txsd_avail > 0, ("%s: no txsd", __func__));
861 		txsd->plen = m->m_len;
862 		txsd->tx_credits = credits;
863 		txsd++;
864 		if (__predict_false(++toep->txsd_pidx == toep->txsd_total)) {
865 			toep->txsd_pidx = 0;
866 			txsd = &toep->txsd[0];
867 		}
868 		toep->txsd_avail--;
869 
870 		counter_u64_add(toep->ofld_txq->tx_toe_tls_records, 1);
871 		counter_u64_add(toep->ofld_txq->tx_toe_tls_octets, m->m_len);
872 
873 		t4_l2t_send(sc, wr, toep->l2te);
874 	}
875 }
876 
877 /*
878  * For TLS data we place received mbufs received via CPL_TLS_DATA into
879  * an mbufq in the TLS offload state.  When CPL_RX_TLS_CMP is
880  * received, the completed PDUs are placed into the socket receive
881  * buffer.
882  *
883  * The TLS code reuses the ulp_pdu_reclaimq to hold the pending mbufs.
884  */
885 static int
886 do_tls_data(struct sge_iq *iq, const struct rss_header *rss, struct mbuf *m)
887 {
888 	struct adapter *sc = iq->adapter;
889 	const struct cpl_tls_data *cpl = mtod(m, const void *);
890 	unsigned int tid = GET_TID(cpl);
891 	struct toepcb *toep = lookup_tid(sc, tid);
892 	struct inpcb *inp = toep->inp;
893 	struct tcpcb *tp;
894 	int len;
895 
896 	/* XXX: Should this match do_rx_data instead? */
897 	KASSERT(!(toep->flags & TPF_SYNQE),
898 	    ("%s: toep %p claims to be a synq entry", __func__, toep));
899 
900 	KASSERT(toep->tid == tid, ("%s: toep tid/atid mismatch", __func__));
901 
902 	/* strip off CPL header */
903 	m_adj(m, sizeof(*cpl));
904 	len = m->m_pkthdr.len;
905 
906 	toep->ofld_rxq->rx_toe_tls_octets += len;
907 
908 	KASSERT(len == G_CPL_TLS_DATA_LENGTH(be32toh(cpl->length_pkd)),
909 	    ("%s: payload length mismatch", __func__));
910 
911 	INP_WLOCK(inp);
912 	if (inp->inp_flags & (INP_DROPPED | INP_TIMEWAIT)) {
913 		CTR4(KTR_CXGBE, "%s: tid %u, rx (%d bytes), inp_flags 0x%x",
914 		    __func__, tid, len, inp->inp_flags);
915 		INP_WUNLOCK(inp);
916 		m_freem(m);
917 		return (0);
918 	}
919 
920 	/* Save TCP sequence number. */
921 	m->m_pkthdr.tls_tcp_seq = be32toh(cpl->seq);
922 
923 	if (mbufq_enqueue(&toep->ulp_pdu_reclaimq, m)) {
924 #ifdef INVARIANTS
925 		panic("Failed to queue TLS data packet");
926 #else
927 		printf("%s: Failed to queue TLS data packet\n", __func__);
928 		INP_WUNLOCK(inp);
929 		m_freem(m);
930 		return (0);
931 #endif
932 	}
933 
934 	tp = intotcpcb(inp);
935 	tp->t_rcvtime = ticks;
936 
937 #ifdef VERBOSE_TRACES
938 	CTR4(KTR_CXGBE, "%s: tid %u len %d seq %u", __func__, tid, len,
939 	    be32toh(cpl->seq));
940 #endif
941 
942 	INP_WUNLOCK(inp);
943 	return (0);
944 }
945 
946 static int
947 do_rx_tls_cmp(struct sge_iq *iq, const struct rss_header *rss, struct mbuf *m)
948 {
949 	struct adapter *sc = iq->adapter;
950 	const struct cpl_rx_tls_cmp *cpl = mtod(m, const void *);
951 	struct tlsrx_hdr_pkt *tls_hdr_pkt;
952 	unsigned int tid = GET_TID(cpl);
953 	struct toepcb *toep = lookup_tid(sc, tid);
954 	struct inpcb *inp = toep->inp;
955 	struct tcpcb *tp;
956 	struct socket *so;
957 	struct sockbuf *sb;
958 	struct mbuf *tls_data;
959 	struct tls_get_record *tgr;
960 	struct mbuf *control;
961 	int len, pdu_length, rx_credits;
962 
963 	KASSERT(toep->tid == tid, ("%s: toep tid/atid mismatch", __func__));
964 	KASSERT(!(toep->flags & TPF_SYNQE),
965 	    ("%s: toep %p claims to be a synq entry", __func__, toep));
966 
967 	/* strip off CPL header */
968 	m_adj(m, sizeof(*cpl));
969 	len = m->m_pkthdr.len;
970 
971 	toep->ofld_rxq->rx_toe_tls_records++;
972 
973 	KASSERT(len == G_CPL_RX_TLS_CMP_LENGTH(be32toh(cpl->pdulength_length)),
974 	    ("%s: payload length mismatch", __func__));
975 
976 	INP_WLOCK(inp);
977 	if (inp->inp_flags & (INP_DROPPED | INP_TIMEWAIT)) {
978 		CTR4(KTR_CXGBE, "%s: tid %u, rx (%d bytes), inp_flags 0x%x",
979 		    __func__, tid, len, inp->inp_flags);
980 		INP_WUNLOCK(inp);
981 		m_freem(m);
982 		return (0);
983 	}
984 
985 	pdu_length = G_CPL_RX_TLS_CMP_PDULENGTH(be32toh(cpl->pdulength_length));
986 
987 	so = inp_inpcbtosocket(inp);
988 	tp = intotcpcb(inp);
989 
990 #ifdef VERBOSE_TRACES
991 	CTR6(KTR_CXGBE, "%s: tid %u PDU len %d len %d seq %u, rcv_nxt %u",
992 	    __func__, tid, pdu_length, len, be32toh(cpl->seq), tp->rcv_nxt);
993 #endif
994 
995 	tp->rcv_nxt += pdu_length;
996 	KASSERT(tp->rcv_wnd >= pdu_length,
997 	    ("%s: negative window size", __func__));
998 	tp->rcv_wnd -= pdu_length;
999 
1000 	/* XXX: Not sure what to do about urgent data. */
1001 
1002 	/*
1003 	 * The payload of this CPL is the TLS header followed by
1004 	 * additional fields.
1005 	 */
1006 	KASSERT(m->m_len >= sizeof(*tls_hdr_pkt),
1007 	    ("%s: payload too small", __func__));
1008 	tls_hdr_pkt = mtod(m, void *);
1009 
1010 	tls_data = mbufq_dequeue(&toep->ulp_pdu_reclaimq);
1011 	if (tls_data != NULL) {
1012 		KASSERT(be32toh(cpl->seq) == tls_data->m_pkthdr.tls_tcp_seq,
1013 		    ("%s: sequence mismatch", __func__));
1014 	}
1015 
1016 	/* Report decryption errors as EBADMSG. */
1017 	if ((tls_hdr_pkt->res_to_mac_error & M_TLSRX_HDR_PKT_ERROR) != 0) {
1018 		m_freem(m);
1019 		m_freem(tls_data);
1020 
1021 		CURVNET_SET(toep->vnet);
1022 		so->so_error = EBADMSG;
1023 		sorwakeup(so);
1024 
1025 		INP_WUNLOCK(inp);
1026 		CURVNET_RESTORE();
1027 
1028 		return (0);
1029 	}
1030 
1031 	/* Allocate the control message mbuf. */
1032 	control = sbcreatecontrol(NULL, sizeof(*tgr), TLS_GET_RECORD,
1033 	    IPPROTO_TCP);
1034 	if (control == NULL) {
1035 		m_freem(m);
1036 		m_freem(tls_data);
1037 
1038 		CURVNET_SET(toep->vnet);
1039 		so->so_error = ENOBUFS;
1040 		sorwakeup(so);
1041 
1042 		INP_WUNLOCK(inp);
1043 		CURVNET_RESTORE();
1044 
1045 		return (0);
1046 	}
1047 
1048 	tgr = (struct tls_get_record *)
1049 	    CMSG_DATA(mtod(control, struct cmsghdr *));
1050 	tgr->tls_type = tls_hdr_pkt->type;
1051 	tgr->tls_vmajor = be16toh(tls_hdr_pkt->version) >> 8;
1052 	tgr->tls_vminor = be16toh(tls_hdr_pkt->version) & 0xff;
1053 
1054 	m_freem(m);
1055 
1056 	if (tls_data != NULL) {
1057 		m_last(tls_data)->m_flags |= M_EOR;
1058 		tgr->tls_length = htobe16(tls_data->m_pkthdr.len);
1059 	} else
1060 		tgr->tls_length = 0;
1061 	m = tls_data;
1062 
1063 	sb = &so->so_rcv;
1064 	SOCKBUF_LOCK(sb);
1065 
1066 	if (__predict_false(sb->sb_state & SBS_CANTRCVMORE)) {
1067 		struct epoch_tracker et;
1068 
1069 		CTR3(KTR_CXGBE, "%s: tid %u, excess rx (%d bytes)",
1070 		    __func__, tid, pdu_length);
1071 		m_freem(m);
1072 		m_freem(control);
1073 		SOCKBUF_UNLOCK(sb);
1074 		INP_WUNLOCK(inp);
1075 
1076 		CURVNET_SET(toep->vnet);
1077 		NET_EPOCH_ENTER(et);
1078 		INP_WLOCK(inp);
1079 		tp = tcp_drop(tp, ECONNRESET);
1080 		if (tp)
1081 			INP_WUNLOCK(inp);
1082 		NET_EPOCH_EXIT(et);
1083 		CURVNET_RESTORE();
1084 
1085 		return (0);
1086 	}
1087 
1088 	/*
1089 	 * Not all of the bytes on the wire are included in the socket buffer
1090 	 * (e.g. the MAC of the TLS record).  However, those bytes are included
1091 	 * in the TCP sequence space.
1092 	 */
1093 
1094 	/* receive buffer autosize */
1095 	MPASS(toep->vnet == so->so_vnet);
1096 	CURVNET_SET(toep->vnet);
1097 	if (sb->sb_flags & SB_AUTOSIZE &&
1098 	    V_tcp_do_autorcvbuf &&
1099 	    sb->sb_hiwat < V_tcp_autorcvbuf_max &&
1100 	    m->m_pkthdr.len > (sbspace(sb) / 8 * 7)) {
1101 		unsigned int hiwat = sb->sb_hiwat;
1102 		unsigned int newsize = min(hiwat + sc->tt.autorcvbuf_inc,
1103 		    V_tcp_autorcvbuf_max);
1104 
1105 		if (!sbreserve_locked(sb, newsize, so, NULL))
1106 			sb->sb_flags &= ~SB_AUTOSIZE;
1107 	}
1108 
1109 	sbappendcontrol_locked(sb, m, control, 0);
1110 	rx_credits = sbspace(sb) > tp->rcv_wnd ? sbspace(sb) - tp->rcv_wnd : 0;
1111 #ifdef VERBOSE_TRACES
1112 	CTR4(KTR_CXGBE, "%s: tid %u rx_credits %u rcv_wnd %u",
1113 	    __func__, tid, rx_credits, tp->rcv_wnd);
1114 #endif
1115 	if (rx_credits > 0 && sbused(sb) + tp->rcv_wnd < sb->sb_lowat) {
1116 		rx_credits = send_rx_credits(sc, toep, rx_credits);
1117 		tp->rcv_wnd += rx_credits;
1118 		tp->rcv_adv += rx_credits;
1119 	}
1120 
1121 	sorwakeup_locked(so);
1122 	SOCKBUF_UNLOCK_ASSERT(sb);
1123 
1124 	INP_WUNLOCK(inp);
1125 	CURVNET_RESTORE();
1126 	return (0);
1127 }
1128 
1129 void
1130 do_rx_data_tls(const struct cpl_rx_data *cpl, struct toepcb *toep,
1131     struct mbuf *m)
1132 {
1133 	struct inpcb *inp = toep->inp;
1134 	struct tls_ofld_info *tls_ofld = &toep->tls;
1135 	struct tls_hdr *hdr;
1136 	struct tcpcb *tp;
1137 	struct socket *so;
1138 	struct sockbuf *sb;
1139 	int len, rx_credits;
1140 
1141 	len = m->m_pkthdr.len;
1142 
1143 	INP_WLOCK_ASSERT(inp);
1144 
1145 	so = inp_inpcbtosocket(inp);
1146 	tp = intotcpcb(inp);
1147 	sb = &so->so_rcv;
1148 	SOCKBUF_LOCK(sb);
1149 	CURVNET_SET(toep->vnet);
1150 
1151 	tp->rcv_nxt += len;
1152 	KASSERT(tp->rcv_wnd >= len, ("%s: negative window size", __func__));
1153 	tp->rcv_wnd -= len;
1154 
1155 	/* Do we have a full TLS header? */
1156 	if (len < sizeof(*hdr)) {
1157 		CTR3(KTR_CXGBE, "%s: tid %u len %d: too short for a TLS header",
1158 		    __func__, toep->tid, len);
1159 		so->so_error = EMSGSIZE;
1160 		goto out;
1161 	}
1162 	hdr = mtod(m, struct tls_hdr *);
1163 
1164 	/* Is the header valid? */
1165 	if (be16toh(hdr->version) != tls_ofld->rx_version) {
1166 		CTR3(KTR_CXGBE, "%s: tid %u invalid version %04x",
1167 		    __func__, toep->tid, be16toh(hdr->version));
1168 		so->so_error = EINVAL;
1169 		goto out;
1170 	}
1171 	if (be16toh(hdr->length) < sizeof(*hdr)) {
1172 		CTR3(KTR_CXGBE, "%s: tid %u invalid length %u",
1173 		    __func__, toep->tid, be16toh(hdr->length));
1174 		so->so_error = EBADMSG;
1175 		goto out;
1176 	}
1177 
1178 	/* Did we get a truncated record? */
1179 	if (len < be16toh(hdr->length)) {
1180 		CTR4(KTR_CXGBE, "%s: tid %u truncated TLS record (%d vs %u)",
1181 		    __func__, toep->tid, len, be16toh(hdr->length));
1182 
1183 		so->so_error = EMSGSIZE;
1184 		goto out;
1185 	}
1186 
1187 	/* Is the header type unknown? */
1188 	switch (hdr->type) {
1189 	case CONTENT_TYPE_CCS:
1190 	case CONTENT_TYPE_ALERT:
1191 	case CONTENT_TYPE_APP_DATA:
1192 	case CONTENT_TYPE_HANDSHAKE:
1193 		break;
1194 	default:
1195 		CTR3(KTR_CXGBE, "%s: tid %u invalid TLS record type %u",
1196 		    __func__, toep->tid, hdr->type);
1197 		so->so_error = EBADMSG;
1198 		goto out;
1199 	}
1200 
1201 	/*
1202 	 * Just punt.  Although this could fall back to software
1203 	 * decryption, this case should never really happen.
1204 	 */
1205 	CTR4(KTR_CXGBE, "%s: tid %u dropping TLS record type %u, length %u",
1206 	    __func__, toep->tid, hdr->type, be16toh(hdr->length));
1207 	so->so_error = EBADMSG;
1208 
1209 out:
1210 	/*
1211 	 * This connection is going to die anyway, so probably don't
1212 	 * need to bother with returning credits.
1213 	 */
1214 	rx_credits = sbspace(sb) > tp->rcv_wnd ? sbspace(sb) - tp->rcv_wnd : 0;
1215 #ifdef VERBOSE_TRACES
1216 	CTR4(KTR_CXGBE, "%s: tid %u rx_credits %u rcv_wnd %u",
1217 	    __func__, toep->tid, rx_credits, tp->rcv_wnd);
1218 #endif
1219 	if (rx_credits > 0 && sbused(sb) + tp->rcv_wnd < sb->sb_lowat) {
1220 		rx_credits = send_rx_credits(toep->vi->adapter, toep,
1221 		    rx_credits);
1222 		tp->rcv_wnd += rx_credits;
1223 		tp->rcv_adv += rx_credits;
1224 	}
1225 
1226 	sorwakeup_locked(so);
1227 	SOCKBUF_UNLOCK_ASSERT(sb);
1228 
1229 	INP_WUNLOCK(inp);
1230 	CURVNET_RESTORE();
1231 
1232 	m_freem(m);
1233 }
1234 
1235 void
1236 t4_tls_mod_load(void)
1237 {
1238 
1239 	t4_register_cpl_handler(CPL_TLS_DATA, do_tls_data);
1240 	t4_register_cpl_handler(CPL_RX_TLS_CMP, do_rx_tls_cmp);
1241 }
1242 
1243 void
1244 t4_tls_mod_unload(void)
1245 {
1246 
1247 	t4_register_cpl_handler(CPL_TLS_DATA, NULL);
1248 	t4_register_cpl_handler(CPL_RX_TLS_CMP, NULL);
1249 }
1250 #endif	/* TCP_OFFLOAD */
1251 #endif	/* KERN_TLS */
1252