2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2018-2019 Chelsio Communications, Inc.
6 * Written by: John Baldwin <jhb@FreeBSD.org>
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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.
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
31 #include "opt_inet6.h"
32 #include "opt_kern_tls.h"
34 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD$");
37 #include <sys/param.h>
40 #include <sys/sglist.h>
41 #include <sys/socket.h>
42 #include <sys/socketvar.h>
43 #include <sys/sockbuf.h>
44 #include <netinet/in.h>
45 #include <netinet/in_pcb.h>
46 #include <netinet/ip.h>
47 #include <netinet/ip6.h>
48 #include <netinet/tcp_var.h>
49 #include <opencrypto/cryptodev.h>
50 #include <opencrypto/xform.h>
52 #include "common/common.h"
53 #include "common/t4_regs.h"
54 #include "common/t4_regs_values.h"
55 #include "common/t4_tcb.h"
58 #include "t4_mp_ring.h"
59 #include "crypto/t4_crypto.h"
61 #if defined(INET) || defined(INET6)
65 #define GCM_TAG_SIZE 16
66 #define TLS_HEADER_LENGTH 5
68 #define TLS_KEY_CONTEXT_SZ roundup2(sizeof(struct tls_keyctx), 32)
80 __u8 reneg_to_write_rx;
85 __be32 len16; /* command length */
86 __be32 dlen; /* data length in 32-byte units */
94 struct tx_keyctx_hdr {
97 __be16 dualck_to_txvalid;
108 #define S_TLS_KEYCTX_TX_WR_TXOPAD_PRESENT 11
109 #define M_TLS_KEYCTX_TX_WR_TXOPAD_PRESENT 0x1
110 #define V_TLS_KEYCTX_TX_WR_TXOPAD_PRESENT(x) \
111 ((x) << S_TLS_KEYCTX_TX_WR_TXOPAD_PRESENT)
112 #define G_TLS_KEYCTX_TX_WR_TXOPAD_PRESENT(x) \
113 (((x) >> S_TLS_KEYCTX_TX_WR_TXOPAD_PRESENT) & \
114 M_TLS_KEYCTX_TX_WR_TXOPAD_PRESENT)
115 #define F_TLS_KEYCTX_TX_WR_TXOPAD_PRESENT \
116 V_TLS_KEYCTX_TX_WR_TXOPAD_PRESENT(1U)
118 #define S_TLS_KEYCTX_TX_WR_SALT_PRESENT 10
119 #define M_TLS_KEYCTX_TX_WR_SALT_PRESENT 0x1
120 #define V_TLS_KEYCTX_TX_WR_SALT_PRESENT(x) \
121 ((x) << S_TLS_KEYCTX_TX_WR_SALT_PRESENT)
122 #define G_TLS_KEYCTX_TX_WR_SALT_PRESENT(x) \
123 (((x) >> S_TLS_KEYCTX_TX_WR_SALT_PRESENT) & \
124 M_TLS_KEYCTX_TX_WR_SALT_PRESENT)
125 #define F_TLS_KEYCTX_TX_WR_SALT_PRESENT \
126 V_TLS_KEYCTX_TX_WR_SALT_PRESENT(1U)
128 #define S_TLS_KEYCTX_TX_WR_TXCK_SIZE 6
129 #define M_TLS_KEYCTX_TX_WR_TXCK_SIZE 0xf
130 #define V_TLS_KEYCTX_TX_WR_TXCK_SIZE(x) \
131 ((x) << S_TLS_KEYCTX_TX_WR_TXCK_SIZE)
132 #define G_TLS_KEYCTX_TX_WR_TXCK_SIZE(x) \
133 (((x) >> S_TLS_KEYCTX_TX_WR_TXCK_SIZE) & \
134 M_TLS_KEYCTX_TX_WR_TXCK_SIZE)
136 #define S_TLS_KEYCTX_TX_WR_TXMK_SIZE 2
137 #define M_TLS_KEYCTX_TX_WR_TXMK_SIZE 0xf
138 #define V_TLS_KEYCTX_TX_WR_TXMK_SIZE(x) \
139 ((x) << S_TLS_KEYCTX_TX_WR_TXMK_SIZE)
140 #define G_TLS_KEYCTX_TX_WR_TXMK_SIZE(x) \
141 (((x) >> S_TLS_KEYCTX_TX_WR_TXMK_SIZE) & \
142 M_TLS_KEYCTX_TX_WR_TXMK_SIZE)
144 #define S_TLS_KEYCTX_TX_WR_TXVALID 0
145 #define M_TLS_KEYCTX_TX_WR_TXVALID 0x1
146 #define V_TLS_KEYCTX_TX_WR_TXVALID(x) \
147 ((x) << S_TLS_KEYCTX_TX_WR_TXVALID)
148 #define G_TLS_KEYCTX_TX_WR_TXVALID(x) \
149 (((x) >> S_TLS_KEYCTX_TX_WR_TXVALID) & M_TLS_KEYCTX_TX_WR_TXVALID)
150 #define F_TLS_KEYCTX_TX_WR_TXVALID V_TLS_KEYCTX_TX_WR_TXVALID(1U)
152 /* Key Context Programming Operation type */
153 #define KEY_WRITE_RX 0x1
154 #define KEY_WRITE_TX 0x2
155 #define KEY_DELETE_RX 0x4
156 #define KEY_DELETE_TX 0x8
159 struct cxgbe_snd_tag com;
160 struct vi_info *vi; /* virtual interface */
162 struct l2t_entry *l2te; /* L2 table entry used by this connection */
163 int tid; /* Connection identifier */
167 bool using_timestamps;
168 unsigned char enc_mode;
170 struct tls_scmd scmd0;
171 struct tls_scmd scmd0_short;
173 unsigned int tx_key_info_size;
181 /* Only used outside of setup and teardown when using inline keys. */
182 struct tls_keyctx keyctx;
184 /* Fields only used during setup and teardown. */
185 struct inpcb *inp; /* backpointer to host stack's PCB */
187 struct sge_wrq *ctrlq;
188 struct clip_entry *ce; /* CLIP table entry used by this tid */
190 unsigned char auth_mode;
191 unsigned char hmac_ctrl;
192 unsigned char mac_first;
193 unsigned char iv_size;
195 unsigned int frag_size;
196 unsigned int cipher_secret_size;
202 static int ktls_setup_keys(struct tlspcb *tlsp,
203 const struct ktls_session *tls, struct sge_txq *txq);
205 static inline struct tlspcb *
206 mst_to_tls(struct m_snd_tag *t)
208 return ((struct tlspcb *)mst_to_cst(t));
211 /* XXX: There are similar versions of these two in tom/t4_tls.c. */
213 get_new_keyid(struct tlspcb *tlsp)
217 if (vmem_alloc(tlsp->sc->key_map, TLS_KEY_CONTEXT_SZ,
218 M_NOWAIT | M_FIRSTFIT, &addr) != 0)
225 free_keyid(struct tlspcb *tlsp, int keyid)
228 CTR3(KTR_CXGBE, "%s: tid %d key addr %#x", __func__, tlsp->tid, keyid);
229 vmem_free(tlsp->sc->key_map, keyid, TLS_KEY_CONTEXT_SZ);
232 static struct tlspcb *
233 alloc_tlspcb(struct ifnet *ifp, struct vi_info *vi, int flags)
235 struct port_info *pi = vi->pi;
236 struct adapter *sc = pi->adapter;
239 tlsp = malloc(sizeof(*tlsp), M_CXGBE, M_ZERO | flags);
243 cxgbe_snd_tag_init(&tlsp->com, ifp, IF_SND_TAG_TYPE_TLS);
246 tlsp->ctrlq = &sc->sge.ctrlq[pi->port_id];
248 tlsp->tx_key_addr = -1;
254 init_ktls_key_params(struct tlspcb *tlsp, const struct ktls_session *tls)
258 if (tls->params.tls_vminor == TLS_MINOR_VER_ONE)
259 tlsp->proto_ver = SCMD_PROTO_VERSION_TLS_1_1;
261 tlsp->proto_ver = SCMD_PROTO_VERSION_TLS_1_2;
262 tlsp->cipher_secret_size = tls->params.cipher_key_len;
263 tlsp->tx_key_info_size = sizeof(struct tx_keyctx_hdr) +
264 tlsp->cipher_secret_size;
265 if (tls->params.cipher_algorithm == CRYPTO_AES_NIST_GCM_16) {
266 tlsp->auth_mode = SCMD_AUTH_MODE_GHASH;
267 tlsp->enc_mode = SCMD_CIPH_MODE_AES_GCM;
270 tlsp->hmac_ctrl = SCMD_HMAC_CTRL_NOP;
271 tlsp->tx_key_info_size += GMAC_BLOCK_LEN;
273 switch (tls->params.auth_algorithm) {
274 case CRYPTO_SHA1_HMAC:
275 mac_key_size = roundup2(SHA1_HASH_LEN, 16);
276 tlsp->auth_mode = SCMD_AUTH_MODE_SHA1;
278 case CRYPTO_SHA2_256_HMAC:
279 mac_key_size = SHA2_256_HASH_LEN;
280 tlsp->auth_mode = SCMD_AUTH_MODE_SHA256;
282 case CRYPTO_SHA2_384_HMAC:
283 mac_key_size = SHA2_512_HASH_LEN;
284 tlsp->auth_mode = SCMD_AUTH_MODE_SHA512_384;
287 tlsp->enc_mode = SCMD_CIPH_MODE_AES_CBC;
288 tlsp->iv_size = 8; /* for CBC, iv is 16B, unit of 2B */
290 tlsp->hmac_ctrl = SCMD_HMAC_CTRL_NO_TRUNC;
291 tlsp->tx_key_info_size += mac_key_size * 2;
294 tlsp->frag_size = tls->params.max_frame_len;
298 ktls_act_open_cpl_size(bool isipv6)
302 return (sizeof(struct cpl_t6_act_open_req6));
304 return (sizeof(struct cpl_t6_act_open_req));
308 mk_ktls_act_open_req(struct adapter *sc, struct vi_info *vi, struct inpcb *inp,
309 struct tlspcb *tlsp, int atid, void *dst)
311 struct tcpcb *tp = intotcpcb(inp);
312 struct cpl_t6_act_open_req *cpl6;
313 struct cpl_act_open_req *cpl;
318 cpl = (struct cpl_act_open_req *)cpl6;
320 qid_atid = V_TID_QID(sc->sge.fwq.abs_id) | V_TID_TID(atid) |
321 V_TID_COOKIE(CPL_COOKIE_KERN_TLS);
322 OPCODE_TID(cpl) = htobe32(MK_OPCODE_TID(CPL_ACT_OPEN_REQ,
324 inp_4tuple_get(inp, &cpl->local_ip, &cpl->local_port,
325 &cpl->peer_ip, &cpl->peer_port);
327 options = F_TCAM_BYPASS | V_ULP_MODE(ULP_MODE_NONE);
328 options |= V_SMAC_SEL(vi->smt_idx) | V_TX_CHAN(vi->pi->tx_chan);
329 options |= F_NON_OFFLOAD;
330 cpl->opt0 = htobe64(options);
332 options = V_TX_QUEUE(sc->params.tp.tx_modq[vi->pi->tx_chan]);
333 if (tp->t_flags & TF_REQ_TSTMP)
334 options |= F_TSTAMPS_EN;
335 cpl->opt2 = htobe32(options);
339 mk_ktls_act_open_req6(struct adapter *sc, struct vi_info *vi,
340 struct inpcb *inp, struct tlspcb *tlsp, int atid, void *dst)
342 struct tcpcb *tp = intotcpcb(inp);
343 struct cpl_t6_act_open_req6 *cpl6;
344 struct cpl_act_open_req6 *cpl;
349 cpl = (struct cpl_act_open_req6 *)cpl6;
351 qid_atid = V_TID_QID(sc->sge.fwq.abs_id) | V_TID_TID(atid) |
352 V_TID_COOKIE(CPL_COOKIE_KERN_TLS);
353 OPCODE_TID(cpl) = htobe32(MK_OPCODE_TID(CPL_ACT_OPEN_REQ6,
355 cpl->local_port = inp->inp_lport;
356 cpl->local_ip_hi = *(uint64_t *)&inp->in6p_laddr.s6_addr[0];
357 cpl->local_ip_lo = *(uint64_t *)&inp->in6p_laddr.s6_addr[8];
358 cpl->peer_port = inp->inp_fport;
359 cpl->peer_ip_hi = *(uint64_t *)&inp->in6p_faddr.s6_addr[0];
360 cpl->peer_ip_lo = *(uint64_t *)&inp->in6p_faddr.s6_addr[8];
362 options = F_TCAM_BYPASS | V_ULP_MODE(ULP_MODE_NONE);
363 options |= V_SMAC_SEL(vi->smt_idx) | V_TX_CHAN(vi->pi->tx_chan);
364 options |= F_NON_OFFLOAD;
365 cpl->opt0 = htobe64(options);
367 options = V_TX_QUEUE(sc->params.tp.tx_modq[vi->pi->tx_chan]);
368 if (tp->t_flags & TF_REQ_TSTMP)
369 options |= F_TSTAMPS_EN;
370 cpl->opt2 = htobe32(options);
374 send_ktls_act_open_req(struct adapter *sc, struct vi_info *vi,
375 struct inpcb *inp, struct tlspcb *tlsp, int atid)
380 isipv6 = (inp->inp_vflag & INP_IPV6) != 0;
382 tlsp->ce = t4_hold_lip(sc, &inp->in6p_laddr, NULL);
383 if (tlsp->ce == NULL)
387 wr = alloc_wrqe(ktls_act_open_cpl_size(isipv6), tlsp->ctrlq);
389 CTR2(KTR_CXGBE, "%s: atid %d failed to alloc WR", __func__,
395 mk_ktls_act_open_req6(sc, vi, inp, tlsp, atid, wrtod(wr));
397 mk_ktls_act_open_req(sc, vi, inp, tlsp, atid, wrtod(wr));
399 tlsp->open_pending = true;
405 ktls_act_open_rpl(struct sge_iq *iq, const struct rss_header *rss,
408 struct adapter *sc = iq->adapter;
409 const struct cpl_act_open_rpl *cpl = (const void *)(rss + 1);
410 u_int atid = G_TID_TID(G_AOPEN_ATID(be32toh(cpl->atid_status)));
411 u_int status = G_AOPEN_STATUS(be32toh(cpl->atid_status));
412 struct tlspcb *tlsp = lookup_atid(sc, atid);
413 struct inpcb *inp = tlsp->inp;
415 CTR3(KTR_CXGBE, "%s: atid %d status %d", __func__, atid, status);
418 tlsp->tid = GET_TID(cpl);
421 tlsp->open_pending = false;
427 /* SET_TCB_FIELD sent as a ULP command looks like this */
428 #define LEN__SET_TCB_FIELD_ULP (sizeof(struct ulp_txpkt) + \
429 sizeof(struct ulptx_idata) + sizeof(struct cpl_set_tcb_field_core))
431 _Static_assert((LEN__SET_TCB_FIELD_ULP + sizeof(struct ulptx_idata)) % 16 == 0,
432 "CPL_SET_TCB_FIELD ULP command not 16-byte aligned");
435 write_set_tcb_field_ulp(struct tlspcb *tlsp, void *dst, struct sge_txq *txq,
436 uint16_t word, uint64_t mask, uint64_t val)
438 struct ulp_txpkt *txpkt;
439 struct ulptx_idata *idata;
440 struct cpl_set_tcb_field_core *cpl;
444 txpkt->cmd_dest = htobe32(V_ULPTX_CMD(ULP_TX_PKT) |
445 V_ULP_TXPKT_DATAMODIFY(0) |
446 V_ULP_TXPKT_CHANNELID(tlsp->vi->pi->port_id) | V_ULP_TXPKT_DEST(0) |
447 V_ULP_TXPKT_FID(txq->eq.cntxt_id) | V_ULP_TXPKT_RO(1));
448 txpkt->len = htobe32(howmany(LEN__SET_TCB_FIELD_ULP, 16));
450 /* ULPTX_IDATA sub-command */
451 idata = (struct ulptx_idata *)(txpkt + 1);
452 idata->cmd_more = htobe32(V_ULPTX_CMD(ULP_TX_SC_IMM));
453 idata->len = htobe32(sizeof(*cpl));
455 /* CPL_SET_TCB_FIELD */
456 cpl = (struct cpl_set_tcb_field_core *)(idata + 1);
457 OPCODE_TID(cpl) = htobe32(MK_OPCODE_TID(CPL_SET_TCB_FIELD, tlsp->tid));
458 cpl->reply_ctrl = htobe16(F_NO_REPLY);
459 cpl->word_cookie = htobe16(V_WORD(word));
460 cpl->mask = htobe64(mask);
461 cpl->val = htobe64(val);
464 idata = (struct ulptx_idata *)(cpl + 1);
465 idata->cmd_more = htobe32(V_ULPTX_CMD(ULP_TX_SC_NOOP));
466 idata->len = htobe32(0);
470 ktls_set_tcb_fields(struct tlspcb *tlsp, struct tcpcb *tp, struct sge_txq *txq)
472 struct fw_ulptx_wr *wr;
478 len = sizeof(*wr) + 3 * roundup2(LEN__SET_TCB_FIELD_ULP, 16);
479 if (tp->t_flags & TF_REQ_TSTMP)
480 len += roundup2(LEN__SET_TCB_FIELD_ULP, 16);
481 m = alloc_wr_mbuf(len, M_NOWAIT);
483 CTR2(KTR_CXGBE, "%s: tid %d failed to alloc WR mbuf", __func__,
487 m->m_pkthdr.snd_tag = m_snd_tag_ref(&tlsp->com.com);
488 m->m_pkthdr.csum_flags |= CSUM_SND_TAG;
491 wr = mtod(m, void *);
492 wr->op_to_compl = htobe32(V_FW_WR_OP(FW_ULPTX_WR));
493 wr->flowid_len16 = htobe32(F_FW_ULPTX_WR_DATA |
494 V_FW_WR_LEN16(len / 16));
496 dst = (char *)(wr + 1);
498 /* Clear TF_NON_OFFLOAD and set TF_CORE_BYPASS */
499 write_set_tcb_field_ulp(tlsp, dst, txq, W_TCB_T_FLAGS,
500 V_TCB_T_FLAGS(V_TF_CORE_BYPASS(1) | V_TF_NON_OFFLOAD(1)),
501 V_TCB_T_FLAGS(V_TF_CORE_BYPASS(1)));
502 dst += roundup2(LEN__SET_TCB_FIELD_ULP, 16);
504 /* Clear the SND_UNA_RAW, SND_NXT_RAW, and SND_MAX_RAW offsets. */
505 write_set_tcb_field_ulp(tlsp, dst, txq, W_TCB_SND_UNA_RAW,
506 V_TCB_SND_NXT_RAW(M_TCB_SND_NXT_RAW) |
507 V_TCB_SND_UNA_RAW(M_TCB_SND_UNA_RAW),
508 V_TCB_SND_NXT_RAW(0) | V_TCB_SND_UNA_RAW(0));
509 dst += roundup2(LEN__SET_TCB_FIELD_ULP, 16);
511 write_set_tcb_field_ulp(tlsp, dst, txq, W_TCB_SND_MAX_RAW,
512 V_TCB_SND_MAX_RAW(M_TCB_SND_MAX_RAW), V_TCB_SND_MAX_RAW(0));
513 dst += roundup2(LEN__SET_TCB_FIELD_ULP, 16);
515 if (tp->t_flags & TF_REQ_TSTMP) {
516 write_set_tcb_field_ulp(tlsp, dst, txq, W_TCB_TIMESTAMP_OFFSET,
517 V_TCB_TIMESTAMP_OFFSET(M_TCB_TIMESTAMP_OFFSET),
518 V_TCB_TIMESTAMP_OFFSET(tp->ts_offset >> 28));
519 dst += roundup2(LEN__SET_TCB_FIELD_ULP, 16);
522 KASSERT(dst - (char *)wr == len, ("%s: length mismatch", __func__));
525 error = mp_ring_enqueue(txq->r, items, 1, 1);
532 cxgbe_tls_tag_alloc(struct ifnet *ifp, union if_snd_tag_alloc_params *params,
533 struct m_snd_tag **pt)
535 const struct ktls_session *tls;
542 int atid, error, keyid;
544 tls = params->tls.tls;
546 /* Only TLS 1.1 and TLS 1.2 are currently supported. */
547 if (tls->params.tls_vmajor != TLS_MAJOR_VER_ONE ||
548 tls->params.tls_vminor < TLS_MINOR_VER_ONE ||
549 tls->params.tls_vminor > TLS_MINOR_VER_TWO)
550 return (EPROTONOSUPPORT);
552 /* Sanity check values in *tls. */
553 switch (tls->params.cipher_algorithm) {
555 /* XXX: Explicitly ignore any provided IV. */
556 switch (tls->params.cipher_key_len) {
564 switch (tls->params.auth_algorithm) {
565 case CRYPTO_SHA1_HMAC:
566 case CRYPTO_SHA2_256_HMAC:
567 case CRYPTO_SHA2_384_HMAC:
570 return (EPROTONOSUPPORT);
573 case CRYPTO_AES_NIST_GCM_16:
574 if (tls->params.iv_len != SALT_SIZE)
576 switch (tls->params.cipher_key_len) {
586 return (EPROTONOSUPPORT);
592 tlsp = alloc_tlspcb(ifp, vi, M_WAITOK);
594 atid = alloc_atid(sc, tlsp);
600 if (sc->tlst.inline_keys)
603 keyid = get_new_keyid(tlsp);
605 CTR2(KTR_CXGBE, "%s: atid %d using immediate key ctx", __func__,
607 tlsp->inline_key = true;
609 tlsp->tx_key_addr = keyid;
610 CTR3(KTR_CXGBE, "%s: atid %d allocated TX key addr %#x",
612 atid, tlsp->tx_key_addr);
615 inp = params->tls.inp;
617 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
625 if (tp->t_flags & TF_REQ_TSTMP) {
626 tlsp->using_timestamps = true;
627 if ((tp->ts_offset & 0xfffffff) != 0) {
633 tlsp->using_timestamps = false;
635 error = send_ktls_act_open_req(sc, vi, inp, tlsp, atid);
641 /* Wait for reply to active open. */
642 CTR2(KTR_CXGBE, "%s: atid %d sent CPL_ACT_OPEN_REQ", __func__,
644 while (tlsp->open_pending) {
646 * XXX: PCATCH? We would then have to discard the PCB
647 * when the completion CPL arrived.
649 error = rw_sleep(tlsp, &inp->inp_lock, 0, "t6tlsop", 0);
659 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
665 txq = &sc->sge.txq[vi->first_txq];
666 if (inp->inp_flowtype != M_HASHTYPE_NONE)
667 txq += ((inp->inp_flowid % (vi->ntxq - vi->rsrv_noflowq)) +
671 error = ktls_set_tcb_fields(tlsp, tp, txq);
676 init_ktls_key_params(tlsp, tls);
678 error = ktls_setup_keys(tlsp, tls, txq);
682 /* The SCMD fields used when encrypting a full TLS record. */
683 tlsp->scmd0.seqno_numivs = htobe32(V_SCMD_SEQ_NO_CTRL(3) |
684 V_SCMD_PROTO_VERSION(tlsp->proto_ver) |
685 V_SCMD_ENC_DEC_CTRL(SCMD_ENCDECCTRL_ENCRYPT) |
686 V_SCMD_CIPH_AUTH_SEQ_CTRL((tlsp->mac_first == 0)) |
687 V_SCMD_CIPH_MODE(tlsp->enc_mode) |
688 V_SCMD_AUTH_MODE(tlsp->auth_mode) |
689 V_SCMD_HMAC_CTRL(tlsp->hmac_ctrl) |
690 V_SCMD_IV_SIZE(tlsp->iv_size) | V_SCMD_NUM_IVS(1));
692 tlsp->scmd0.ivgen_hdrlen = V_SCMD_IV_GEN_CTRL(0) |
693 V_SCMD_TLS_FRAG_ENABLE(0);
694 if (tlsp->inline_key)
695 tlsp->scmd0.ivgen_hdrlen |= V_SCMD_KEY_CTX_INLINE(1);
696 tlsp->scmd0.ivgen_hdrlen = htobe32(tlsp->scmd0.ivgen_hdrlen);
699 * The SCMD fields used when encrypting a partial TLS record
700 * (no trailer and possibly a truncated payload).
702 tlsp->scmd0_short.seqno_numivs = V_SCMD_SEQ_NO_CTRL(0) |
703 V_SCMD_PROTO_VERSION(SCMD_PROTO_VERSION_GENERIC) |
704 V_SCMD_ENC_DEC_CTRL(SCMD_ENCDECCTRL_ENCRYPT) |
705 V_SCMD_CIPH_AUTH_SEQ_CTRL((tlsp->mac_first == 0)) |
706 V_SCMD_AUTH_MODE(SCMD_AUTH_MODE_NOP) |
707 V_SCMD_HMAC_CTRL(SCMD_HMAC_CTRL_NOP) |
708 V_SCMD_IV_SIZE(AES_BLOCK_LEN / 2) | V_SCMD_NUM_IVS(0);
709 if (tlsp->enc_mode == SCMD_CIPH_MODE_AES_GCM)
710 tlsp->scmd0_short.seqno_numivs |=
711 V_SCMD_CIPH_MODE(SCMD_CIPH_MODE_AES_CTR);
713 tlsp->scmd0_short.seqno_numivs |=
714 V_SCMD_CIPH_MODE(tlsp->enc_mode);
715 tlsp->scmd0_short.seqno_numivs =
716 htobe32(tlsp->scmd0_short.seqno_numivs);
718 tlsp->scmd0_short.ivgen_hdrlen = V_SCMD_IV_GEN_CTRL(0) |
719 V_SCMD_TLS_FRAG_ENABLE(0) |
721 if (tlsp->inline_key)
722 tlsp->scmd0_short.ivgen_hdrlen |= V_SCMD_KEY_CTX_INLINE(1);
725 if (tlsp->enc_mode == SCMD_CIPH_MODE_AES_GCM)
730 *pt = &tlsp->com.com;
736 m_snd_tag_rele(&tlsp->com.com);
741 ktls_setup_keys(struct tlspcb *tlsp, const struct ktls_session *tls,
744 struct auth_hash *axf;
745 int error, keyid, kwrlen, kctxlen, len;
746 struct tls_key_req *kwr;
747 struct tls_keyctx *kctx;
748 void *items[1], *key;
749 struct tx_keyctx_hdr *khdr;
750 unsigned int ck_size, mk_size, partial_digest_len;
754 * Store the salt and keys in the key context. For
755 * connections with an inline key, this key context is passed
756 * as immediate data in each work request. For connections
757 * storing the key in DDR, a work request is used to store a
758 * copy of the key context in DDR.
760 kctx = &tlsp->keyctx;
763 switch (tlsp->cipher_secret_size) {
765 ck_size = CHCR_KEYCTX_CIPHER_KEY_SIZE_128;
768 ck_size = CHCR_KEYCTX_CIPHER_KEY_SIZE_192;
771 ck_size = CHCR_KEYCTX_CIPHER_KEY_SIZE_256;
774 panic("bad key size");
777 partial_digest_len = 0;
778 if (tlsp->enc_mode == SCMD_CIPH_MODE_AES_GCM)
779 mk_size = CHCR_KEYCTX_MAC_KEY_SIZE_512;
781 switch (tlsp->auth_mode) {
782 case SCMD_AUTH_MODE_SHA1:
783 axf = &auth_hash_hmac_sha1;
784 mk_size = CHCR_KEYCTX_MAC_KEY_SIZE_160;
785 partial_digest_len = SHA1_HASH_LEN;
787 case SCMD_AUTH_MODE_SHA256:
788 axf = &auth_hash_hmac_sha2_256;
789 mk_size = CHCR_KEYCTX_MAC_KEY_SIZE_256;
790 partial_digest_len = SHA2_256_HASH_LEN;
792 case SCMD_AUTH_MODE_SHA512_384:
793 axf = &auth_hash_hmac_sha2_384;
794 mk_size = CHCR_KEYCTX_MAC_KEY_SIZE_512;
795 partial_digest_len = SHA2_512_HASH_LEN;
798 panic("bad auth mode");
802 khdr->ctxlen = (tlsp->tx_key_info_size >> 4);
803 khdr->dualck_to_txvalid = V_TLS_KEYCTX_TX_WR_SALT_PRESENT(1) |
804 V_TLS_KEYCTX_TX_WR_TXCK_SIZE(ck_size) |
805 V_TLS_KEYCTX_TX_WR_TXMK_SIZE(mk_size) |
806 V_TLS_KEYCTX_TX_WR_TXVALID(1);
807 if (tlsp->enc_mode != SCMD_CIPH_MODE_AES_GCM)
808 khdr->dualck_to_txvalid |= V_TLS_KEYCTX_TX_WR_TXOPAD_PRESENT(1);
809 khdr->dualck_to_txvalid = htobe16(khdr->dualck_to_txvalid);
810 key = kctx->keys.edkey;
811 memcpy(key, tls->params.cipher_key, tls->params.cipher_key_len);
812 if (tlsp->enc_mode == SCMD_CIPH_MODE_AES_GCM) {
813 memcpy(khdr->txsalt, tls->params.iv, SALT_SIZE);
814 t4_init_gmac_hash(tls->params.cipher_key,
815 tls->params.cipher_key_len,
816 (char *)key + tls->params.cipher_key_len);
818 t4_init_hmac_digest(axf, partial_digest_len,
819 tls->params.auth_key, tls->params.auth_key_len,
820 (char *)key + tls->params.cipher_key_len);
823 if (tlsp->inline_key)
826 keyid = tlsp->tx_key_addr;
828 /* Populate key work request. */
829 kwrlen = sizeof(*kwr);
830 kctxlen = roundup2(sizeof(*kctx), 32);
831 len = kwrlen + kctxlen;
833 m = alloc_wr_mbuf(len, M_NOWAIT);
835 CTR2(KTR_CXGBE, "%s: tid %d failed to alloc WR mbuf", __func__,
839 m->m_pkthdr.snd_tag = m_snd_tag_ref(&tlsp->com.com);
840 m->m_pkthdr.csum_flags |= CSUM_SND_TAG;
841 kwr = mtod(m, void *);
844 kwr->wr_hi = htobe32(V_FW_WR_OP(FW_ULPTX_WR) |
846 kwr->wr_mid = htobe32(V_FW_WR_LEN16(DIV_ROUND_UP(len, 16)));
847 kwr->protocol = tlsp->proto_ver;
848 kwr->mfs = htons(tlsp->frag_size);
849 kwr->reneg_to_write_rx = KEY_WRITE_TX;
852 kwr->cmd = htobe32(V_ULPTX_CMD(ULP_TX_MEM_WRITE) |
853 V_T5_ULP_MEMIO_ORDER(1) | V_T5_ULP_MEMIO_IMM(1));
854 kwr->dlen = htobe32(V_ULP_MEMIO_DATA_LEN(kctxlen >> 5));
855 kwr->len16 = htobe32((tlsp->tid << 8) |
856 DIV_ROUND_UP(len - sizeof(struct work_request_hdr), 16));
857 kwr->kaddr = htobe32(V_ULP_MEMIO_ADDR(keyid >> 5));
860 kwr->sc_more = htobe32(V_ULPTX_CMD(ULP_TX_SC_IMM));
861 kwr->sc_len = htobe32(kctxlen);
863 kctx = (struct tls_keyctx *)(kwr + 1);
864 memcpy(kctx, &tlsp->keyctx, sizeof(*kctx));
867 * Place the key work request in the transmit queue. It
868 * should be sent to the NIC before any TLS packets using this
872 error = mp_ring_enqueue(txq->r, items, 1, 1);
876 CTR2(KTR_CXGBE, "%s: tid %d sent key WR", __func__, tlsp->tid);
881 ktls_base_wr_size(struct tlspcb *tlsp)
885 wr_len = sizeof(struct fw_ulptx_wr); // 16
886 wr_len += sizeof(struct ulp_txpkt); // 8
887 wr_len += sizeof(struct ulptx_idata); // 8
888 wr_len += sizeof(struct cpl_tx_sec_pdu);// 32
889 if (tlsp->inline_key)
890 wr_len += tlsp->tx_key_info_size;
892 wr_len += sizeof(struct ulptx_sc_memrd);// 8
893 wr_len += sizeof(struct ulptx_idata); // 8
895 wr_len += sizeof(struct cpl_tx_data); // 16
899 /* How many bytes of TCP payload to send for a given TLS record. */
901 ktls_tcp_payload_length(struct tlspcb *tlsp, struct mbuf *m_tls)
903 struct tls_record_layer *hdr;
906 M_ASSERTEXTPG(m_tls);
907 hdr = (void *)m_tls->m_epg_hdr;
908 plen = ntohs(hdr->tls_length);
911 * What range of the TLS record is the mbuf requesting to be
914 mlen = mtod(m_tls, vm_offset_t) + m_tls->m_len;
916 /* Always send complete records. */
917 if (mlen == TLS_HEADER_LENGTH + plen)
921 * If the host stack has asked to send part of the trailer,
922 * trim the length to avoid sending any of the trailer. There
923 * is no way to send a partial trailer currently.
925 if (mlen > TLS_HEADER_LENGTH + plen - m_tls->m_epg_trllen)
926 mlen = TLS_HEADER_LENGTH + plen - m_tls->m_epg_trllen;
930 * For AES-CBC adjust the ciphertext length for the block
933 if (tlsp->enc_mode == SCMD_CIPH_MODE_AES_CBC &&
934 mlen > TLS_HEADER_LENGTH) {
935 mlen = TLS_HEADER_LENGTH + rounddown(mlen - TLS_HEADER_LENGTH,
939 #ifdef VERBOSE_TRACES
940 CTR4(KTR_CXGBE, "%s: tid %d short TLS record (%u vs %u)",
941 __func__, tlsp->tid, mlen, TLS_HEADER_LENGTH + plen);
947 * For a "short" TLS record, determine the offset into the TLS record
948 * payload to send. This offset does not include the TLS header, but
949 * a non-zero offset implies that a header will not be sent.
952 ktls_payload_offset(struct tlspcb *tlsp, struct mbuf *m_tls)
954 struct tls_record_layer *hdr;
960 M_ASSERTEXTPG(m_tls);
961 hdr = (void *)m_tls->m_epg_hdr;
962 plen = ntohs(hdr->tls_length);
964 mlen = mtod(m_tls, vm_offset_t) + m_tls->m_len;
965 MPASS(mlen < TLS_HEADER_LENGTH + plen);
967 if (mtod(m_tls, vm_offset_t) <= m_tls->m_epg_hdrlen)
969 if (tlsp->enc_mode == SCMD_CIPH_MODE_AES_GCM) {
971 * Always send something. This function is only called
972 * if we aren't sending the tag at all, but if the
973 * request starts in the tag then we are in an odd
974 * state where would effectively send nothing. Cap
975 * the offset at the last byte of the record payload
976 * to send the last cipher block.
978 offset = min(mtod(m_tls, vm_offset_t) - m_tls->m_epg_hdrlen,
979 (plen - TLS_HEADER_LENGTH - m_tls->m_epg_trllen) - 1);
980 return (rounddown(offset, AES_BLOCK_LEN));
986 ktls_sgl_size(u_int nsegs)
990 /* First segment is part of ulptx_sgl. */
993 wr_len = sizeof(struct ulptx_sgl);
994 wr_len += 8 * ((3 * nsegs) / 2 + (nsegs & 1));
999 ktls_wr_len(struct tlspcb *tlsp, struct mbuf *m, struct mbuf *m_tls,
1002 struct tls_record_layer *hdr;
1003 u_int imm_len, offset, plen, wr_len, tlen;
1005 M_ASSERTEXTPG(m_tls);
1008 * Determine the size of the TLS record payload to send
1009 * excluding header and trailer.
1011 tlen = ktls_tcp_payload_length(tlsp, m_tls);
1012 if (tlen <= m_tls->m_epg_hdrlen) {
1014 * For requests that only want to send the TLS header,
1015 * send a tunnelled packet as immediate data.
1017 wr_len = sizeof(struct fw_eth_tx_pkt_wr) +
1018 sizeof(struct cpl_tx_pkt_core) +
1019 roundup2(m->m_len + m_tls->m_len, 16);
1020 if (wr_len > SGE_MAX_WR_LEN) {
1022 "%s: tid %d TLS header-only packet too long (len %d)",
1023 __func__, tlsp->tid, m->m_len + m_tls->m_len);
1026 /* This should always be the last TLS record in a chain. */
1027 MPASS(m_tls->m_next == NULL);
1030 * XXX: Set a bogus 'nsegs' value to avoid tripping an
1031 * assertion in mbuf_nsegs() in t4_sge.c.
1037 hdr = (void *)m_tls->m_epg_hdr;
1038 plen = TLS_HEADER_LENGTH + ntohs(hdr->tls_length) - m_tls->m_epg_trllen;
1041 offset = ktls_payload_offset(tlsp, m_tls);
1045 /* Calculate the size of the work request. */
1046 wr_len = ktls_base_wr_size(tlsp);
1049 * Full records and short records with an offset of 0 include
1050 * the TLS header as immediate data. Short records include a
1051 * raw AES IV as immediate data.
1055 imm_len += m_tls->m_epg_hdrlen;
1057 imm_len += AES_BLOCK_LEN;
1058 wr_len += roundup2(imm_len, 16);
1060 /* TLS record payload via DSGL. */
1061 *nsegsp = sglist_count_mbuf_epg(m_tls, m_tls->m_epg_hdrlen + offset,
1062 plen - (m_tls->m_epg_hdrlen + offset));
1063 wr_len += ktls_sgl_size(*nsegsp);
1065 wr_len = roundup2(wr_len, 16);
1070 * See if we have any TCP options requiring a dedicated options-only
1074 ktls_has_tcp_options(struct tcphdr *tcp)
1077 int cnt, opt, optlen;
1079 cp = (u_char *)(tcp + 1);
1080 cnt = tcp->th_off * 4 - sizeof(struct tcphdr);
1081 for (; cnt > 0; cnt -= optlen, cp += optlen) {
1083 if (opt == TCPOPT_EOL)
1085 if (opt == TCPOPT_NOP)
1091 if (optlen < 2 || optlen > cnt)
1096 case TCPOPT_TIMESTAMP:
1106 * Find the TCP timestamp option.
1109 ktls_find_tcp_timestamps(struct tcphdr *tcp)
1112 int cnt, opt, optlen;
1114 cp = (u_char *)(tcp + 1);
1115 cnt = tcp->th_off * 4 - sizeof(struct tcphdr);
1116 for (; cnt > 0; cnt -= optlen, cp += optlen) {
1118 if (opt == TCPOPT_EOL)
1120 if (opt == TCPOPT_NOP)
1126 if (optlen < 2 || optlen > cnt)
1129 if (opt == TCPOPT_TIMESTAMP && optlen == TCPOLEN_TIMESTAMP)
1136 t6_ktls_parse_pkt(struct mbuf *m, int *nsegsp, int *len16p)
1138 struct tlspcb *tlsp;
1139 struct ether_header *eh;
1141 struct ip6_hdr *ip6;
1145 u_int wr_len, tot_len;
1148 * Locate headers in initial mbuf.
1150 * XXX: This assumes all of the headers are in the initial mbuf.
1151 * Could perhaps use m_advance() like parse_pkt() if that turns
1152 * out to not be true.
1155 MPASS(m->m_pkthdr.snd_tag != NULL);
1156 tlsp = mst_to_tls(m->m_pkthdr.snd_tag);
1158 if (m->m_len <= sizeof(*eh) + sizeof(*ip)) {
1159 CTR2(KTR_CXGBE, "%s: tid %d header mbuf too short", __func__,
1163 eh = mtod(m, struct ether_header *);
1164 if (ntohs(eh->ether_type) != ETHERTYPE_IP &&
1165 ntohs(eh->ether_type) != ETHERTYPE_IPV6) {
1166 CTR2(KTR_CXGBE, "%s: tid %d mbuf not ETHERTYPE_IP{,V6}",
1167 __func__, tlsp->tid);
1170 m->m_pkthdr.l2hlen = sizeof(*eh);
1172 /* XXX: Reject unsupported IP options? */
1173 if (ntohs(eh->ether_type) == ETHERTYPE_IP) {
1174 ip = (struct ip *)(eh + 1);
1175 if (ip->ip_p != IPPROTO_TCP) {
1176 CTR2(KTR_CXGBE, "%s: tid %d mbuf not IPPROTO_TCP",
1177 __func__, tlsp->tid);
1180 m->m_pkthdr.l3hlen = ip->ip_hl * 4;
1182 ip6 = (struct ip6_hdr *)(eh + 1);
1183 if (ip6->ip6_nxt != IPPROTO_TCP) {
1184 CTR3(KTR_CXGBE, "%s: tid %d mbuf not IPPROTO_TCP (%u)",
1185 __func__, tlsp->tid, ip6->ip6_nxt);
1188 m->m_pkthdr.l3hlen = sizeof(struct ip6_hdr);
1190 if (m->m_len < m->m_pkthdr.l2hlen + m->m_pkthdr.l3hlen +
1192 CTR2(KTR_CXGBE, "%s: tid %d header mbuf too short (2)",
1193 __func__, tlsp->tid);
1196 tcp = (struct tcphdr *)((char *)(eh + 1) + m->m_pkthdr.l3hlen);
1197 m->m_pkthdr.l4hlen = tcp->th_off * 4;
1199 /* Bail if there is TCP payload before the TLS record. */
1200 if (m->m_len != m->m_pkthdr.l2hlen + m->m_pkthdr.l3hlen +
1201 m->m_pkthdr.l4hlen) {
1203 "%s: tid %d header mbuf bad length (%d + %d + %d != %d)",
1204 __func__, tlsp->tid, m->m_pkthdr.l2hlen,
1205 m->m_pkthdr.l3hlen, m->m_pkthdr.l4hlen, m->m_len);
1209 /* Assume all headers are in 'm' for now. */
1210 MPASS(m->m_next != NULL);
1211 MPASS(m->m_next->m_flags & M_EXTPG);
1216 * Each of the remaining mbufs in the chain should reference a
1220 for (m_tls = m->m_next; m_tls != NULL; m_tls = m_tls->m_next) {
1221 MPASS(m_tls->m_flags & M_EXTPG);
1223 wr_len = ktls_wr_len(tlsp, m, m_tls, &nsegs);
1224 #ifdef VERBOSE_TRACES
1225 CTR4(KTR_CXGBE, "%s: tid %d wr_len %d nsegs %d", __func__,
1226 tlsp->tid, wr_len, nsegs);
1228 if (wr_len > SGE_MAX_WR_LEN || nsegs > TX_SGL_SEGS)
1230 tot_len += roundup2(wr_len, EQ_ESIZE);
1233 * Store 'nsegs' for the first TLS record in the
1234 * header mbuf's metadata.
1240 MPASS(tot_len != 0);
1243 * See if we have any TCP options or a FIN requiring a
1246 if ((tcp->th_flags & TH_FIN) != 0 || ktls_has_tcp_options(tcp)) {
1247 wr_len = sizeof(struct fw_eth_tx_pkt_wr) +
1248 sizeof(struct cpl_tx_pkt_core) + roundup2(m->m_len, 16);
1249 if (wr_len > SGE_MAX_WR_LEN) {
1251 "%s: tid %d options-only packet too long (len %d)",
1252 __func__, tlsp->tid, m->m_len);
1255 tot_len += roundup2(wr_len, EQ_ESIZE);
1258 /* Include room for a TP work request to program an L2T entry. */
1259 tot_len += EQ_ESIZE;
1262 * Include room for a ULPTX work request including up to 5
1263 * CPL_SET_TCB_FIELD commands before the first TLS work
1266 wr_len = sizeof(struct fw_ulptx_wr) +
1267 5 * roundup2(LEN__SET_TCB_FIELD_ULP, 16);
1270 * If timestamps are present, reserve 1 more command for
1271 * setting the echoed timestamp.
1273 if (tlsp->using_timestamps)
1274 wr_len += roundup2(LEN__SET_TCB_FIELD_ULP, 16);
1276 tot_len += roundup2(wr_len, EQ_ESIZE);
1278 *len16p = tot_len / 16;
1279 #ifdef VERBOSE_TRACES
1280 CTR4(KTR_CXGBE, "%s: tid %d len16 %d nsegs %d", __func__,
1281 tlsp->tid, *len16p, *nsegsp);
1287 * If the SGL ends on an address that is not 16 byte aligned, this function will
1288 * add a 0 filled flit at the end.
1291 write_gl_to_buf(struct sglist *gl, caddr_t to)
1293 struct sglist_seg *seg;
1295 struct ulptx_sgl *usgl;
1296 int i, nflits, nsegs;
1298 KASSERT(((uintptr_t)to & 0xf) == 0,
1299 ("%s: SGL must start at a 16 byte boundary: %p", __func__, to));
1301 nsegs = gl->sg_nseg;
1304 nflits = (3 * (nsegs - 1)) / 2 + ((nsegs - 1) & 1) + 2;
1305 flitp = (__be64 *)to;
1306 seg = &gl->sg_segs[0];
1307 usgl = (void *)flitp;
1309 usgl->cmd_nsge = htobe32(V_ULPTX_CMD(ULP_TX_SC_DSGL) |
1310 V_ULPTX_NSGE(nsegs));
1311 usgl->len0 = htobe32(seg->ss_len);
1312 usgl->addr0 = htobe64(seg->ss_paddr);
1315 for (i = 0; i < nsegs - 1; i++, seg++) {
1316 usgl->sge[i / 2].len[i & 1] = htobe32(seg->ss_len);
1317 usgl->sge[i / 2].addr[i & 1] = htobe64(seg->ss_paddr);
1320 usgl->sge[i / 2].len[1] = htobe32(0);
1324 MPASS(((uintptr_t)flitp) & 0xf);
1328 MPASS((((uintptr_t)flitp) & 0xf) == 0);
1332 copy_to_txd(struct sge_eq *eq, caddr_t from, caddr_t *to, int len)
1335 MPASS((uintptr_t)(*to) >= (uintptr_t)&eq->desc[0]);
1336 MPASS((uintptr_t)(*to) < (uintptr_t)&eq->desc[eq->sidx]);
1338 if (__predict_true((uintptr_t)(*to) + len <=
1339 (uintptr_t)&eq->desc[eq->sidx])) {
1340 bcopy(from, *to, len);
1342 if ((uintptr_t)(*to) == (uintptr_t)&eq->desc[eq->sidx])
1343 (*to) = (caddr_t)eq->desc;
1345 int portion = (uintptr_t)&eq->desc[eq->sidx] - (uintptr_t)(*to);
1347 bcopy(from, *to, portion);
1349 portion = len - portion; /* remaining */
1350 bcopy(from, (void *)eq->desc, portion);
1351 (*to) = (caddr_t)eq->desc + portion;
1356 ktls_write_tcp_options(struct sge_txq *txq, void *dst, struct mbuf *m,
1357 u_int available, u_int pidx)
1359 struct tx_sdesc *txsd;
1360 struct fw_eth_tx_pkt_wr *wr;
1361 struct cpl_tx_pkt_core *cpl;
1364 int len16, ndesc, pktlen;
1365 struct ether_header *eh;
1366 struct ip *ip, newip;
1367 struct ip6_hdr *ip6, newip6;
1368 struct tcphdr *tcp, newtcp;
1371 TXQ_LOCK_ASSERT_OWNED(txq);
1376 ctrl = sizeof(struct cpl_tx_pkt_core) + pktlen;
1377 len16 = howmany(sizeof(struct fw_eth_tx_pkt_wr) + ctrl, 16);
1378 ndesc = tx_len16_to_desc(len16);
1379 MPASS(ndesc <= available);
1381 /* Firmware work request header */
1382 wr->op_immdlen = htobe32(V_FW_WR_OP(FW_ETH_TX_PKT_WR) |
1383 V_FW_ETH_TX_PKT_WR_IMMDLEN(ctrl));
1385 ctrl = V_FW_WR_LEN16(len16);
1386 wr->equiq_to_len16 = htobe32(ctrl);
1389 cpl = (void *)(wr + 1);
1392 cpl->ctrl0 = txq->cpl_ctrl0;
1394 cpl->len = htobe16(pktlen);
1396 out = (void *)(cpl + 1);
1398 /* Copy over Ethernet header. */
1399 eh = mtod(m, struct ether_header *);
1400 copy_to_txd(&txq->eq, (caddr_t)eh, &out, m->m_pkthdr.l2hlen);
1402 /* Fixup length in IP header and copy out. */
1403 if (ntohs(eh->ether_type) == ETHERTYPE_IP) {
1404 ip = (void *)((char *)eh + m->m_pkthdr.l2hlen);
1406 newip.ip_len = htons(pktlen - m->m_pkthdr.l2hlen);
1407 copy_to_txd(&txq->eq, (caddr_t)&newip, &out, sizeof(newip));
1408 if (m->m_pkthdr.l3hlen > sizeof(*ip))
1409 copy_to_txd(&txq->eq, (caddr_t)(ip + 1), &out,
1410 m->m_pkthdr.l3hlen - sizeof(*ip));
1411 ctrl1 = V_TXPKT_CSUM_TYPE(TX_CSUM_TCPIP) |
1412 V_T6_TXPKT_ETHHDR_LEN(m->m_pkthdr.l2hlen - ETHER_HDR_LEN) |
1413 V_TXPKT_IPHDR_LEN(m->m_pkthdr.l3hlen);
1415 ip6 = (void *)((char *)eh + m->m_pkthdr.l2hlen);
1417 newip6.ip6_plen = htons(pktlen - m->m_pkthdr.l2hlen);
1418 copy_to_txd(&txq->eq, (caddr_t)&newip6, &out, sizeof(newip6));
1419 MPASS(m->m_pkthdr.l3hlen == sizeof(*ip6));
1420 ctrl1 = V_TXPKT_CSUM_TYPE(TX_CSUM_TCPIP6) |
1421 V_T6_TXPKT_ETHHDR_LEN(m->m_pkthdr.l2hlen - ETHER_HDR_LEN) |
1422 V_TXPKT_IPHDR_LEN(m->m_pkthdr.l3hlen);
1424 cpl->ctrl1 = htobe64(ctrl1);
1427 /* Clear PUSH and FIN in the TCP header if present. */
1428 tcp = (void *)((char *)eh + m->m_pkthdr.l2hlen + m->m_pkthdr.l3hlen);
1430 newtcp.th_flags &= ~(TH_PUSH | TH_FIN);
1431 copy_to_txd(&txq->eq, (caddr_t)&newtcp, &out, sizeof(newtcp));
1433 /* Copy rest of packet. */
1434 copy_to_txd(&txq->eq, (caddr_t)(tcp + 1), &out, pktlen -
1435 (m->m_pkthdr.l2hlen + m->m_pkthdr.l3hlen + sizeof(*tcp)));
1440 txq->kern_tls_options++;
1442 txsd = &txq->sdesc[pidx];
1444 txsd->desc_used = ndesc;
1450 ktls_write_tunnel_packet(struct sge_txq *txq, void *dst, struct mbuf *m,
1451 struct mbuf *m_tls, u_int available, tcp_seq tcp_seqno, u_int pidx)
1453 struct tx_sdesc *txsd;
1454 struct fw_eth_tx_pkt_wr *wr;
1455 struct cpl_tx_pkt_core *cpl;
1458 int len16, ndesc, pktlen;
1459 struct ether_header *eh;
1460 struct ip *ip, newip;
1461 struct ip6_hdr *ip6, newip6;
1462 struct tcphdr *tcp, newtcp;
1465 TXQ_LOCK_ASSERT_OWNED(txq);
1468 /* Locate the template TLS header. */
1469 M_ASSERTEXTPG(m_tls);
1471 /* This should always be the last TLS record in a chain. */
1472 MPASS(m_tls->m_next == NULL);
1475 pktlen = m->m_len + m_tls->m_len;
1476 ctrl = sizeof(struct cpl_tx_pkt_core) + pktlen;
1477 len16 = howmany(sizeof(struct fw_eth_tx_pkt_wr) + ctrl, 16);
1478 ndesc = tx_len16_to_desc(len16);
1479 MPASS(ndesc <= available);
1481 /* Firmware work request header */
1482 wr->op_immdlen = htobe32(V_FW_WR_OP(FW_ETH_TX_PKT_WR) |
1483 V_FW_ETH_TX_PKT_WR_IMMDLEN(ctrl));
1485 ctrl = V_FW_WR_LEN16(len16);
1486 wr->equiq_to_len16 = htobe32(ctrl);
1489 cpl = (void *)(wr + 1);
1492 cpl->ctrl0 = txq->cpl_ctrl0;
1494 cpl->len = htobe16(pktlen);
1496 out = (void *)(cpl + 1);
1498 /* Copy over Ethernet header. */
1499 eh = mtod(m, struct ether_header *);
1500 copy_to_txd(&txq->eq, (caddr_t)eh, &out, m->m_pkthdr.l2hlen);
1502 /* Fixup length in IP header and copy out. */
1503 if (ntohs(eh->ether_type) == ETHERTYPE_IP) {
1504 ip = (void *)((char *)eh + m->m_pkthdr.l2hlen);
1506 newip.ip_len = htons(pktlen - m->m_pkthdr.l2hlen);
1507 copy_to_txd(&txq->eq, (caddr_t)&newip, &out, sizeof(newip));
1508 if (m->m_pkthdr.l3hlen > sizeof(*ip))
1509 copy_to_txd(&txq->eq, (caddr_t)(ip + 1), &out,
1510 m->m_pkthdr.l3hlen - sizeof(*ip));
1511 ctrl1 = V_TXPKT_CSUM_TYPE(TX_CSUM_TCPIP) |
1512 V_T6_TXPKT_ETHHDR_LEN(m->m_pkthdr.l2hlen - ETHER_HDR_LEN) |
1513 V_TXPKT_IPHDR_LEN(m->m_pkthdr.l3hlen);
1515 ip6 = (void *)((char *)eh + m->m_pkthdr.l2hlen);
1517 newip6.ip6_plen = htons(pktlen - m->m_pkthdr.l2hlen);
1518 copy_to_txd(&txq->eq, (caddr_t)&newip6, &out, sizeof(newip6));
1519 MPASS(m->m_pkthdr.l3hlen == sizeof(*ip6));
1520 ctrl1 = V_TXPKT_CSUM_TYPE(TX_CSUM_TCPIP6) |
1521 V_T6_TXPKT_ETHHDR_LEN(m->m_pkthdr.l2hlen - ETHER_HDR_LEN) |
1522 V_TXPKT_IPHDR_LEN(m->m_pkthdr.l3hlen);
1524 cpl->ctrl1 = htobe64(ctrl1);
1527 /* Set sequence number in TCP header. */
1528 tcp = (void *)((char *)eh + m->m_pkthdr.l2hlen + m->m_pkthdr.l3hlen);
1530 newtcp.th_seq = htonl(tcp_seqno + mtod(m_tls, vm_offset_t));
1531 copy_to_txd(&txq->eq, (caddr_t)&newtcp, &out, sizeof(newtcp));
1533 /* Copy rest of TCP header. */
1534 copy_to_txd(&txq->eq, (caddr_t)(tcp + 1), &out, m->m_len -
1535 (m->m_pkthdr.l2hlen + m->m_pkthdr.l3hlen + sizeof(*tcp)));
1537 /* Copy the subset of the TLS header requested. */
1538 copy_to_txd(&txq->eq, (char *)m_tls->m_epg_hdr +
1539 mtod(m_tls, vm_offset_t), &out, m_tls->m_len);
1544 txq->kern_tls_header++;
1546 txsd = &txq->sdesc[pidx];
1548 txsd->desc_used = ndesc;
1553 _Static_assert(sizeof(struct cpl_set_tcb_field) <= EQ_ESIZE,
1554 "CPL_SET_TCB_FIELD must be smaller than a single TX descriptor");
1555 _Static_assert(W_TCB_SND_UNA_RAW == W_TCB_SND_NXT_RAW,
1556 "SND_NXT_RAW and SND_UNA_RAW are in different words");
1559 ktls_write_tls_wr(struct tlspcb *tlsp, struct sge_txq *txq,
1560 void *dst, struct mbuf *m, struct tcphdr *tcp, struct mbuf *m_tls,
1561 u_int nsegs, u_int available, tcp_seq tcp_seqno, uint32_t *tsopt,
1562 u_int pidx, bool set_l2t_idx)
1564 struct sge_eq *eq = &txq->eq;
1565 struct tx_sdesc *txsd;
1566 struct fw_ulptx_wr *wr;
1567 struct ulp_txpkt *txpkt;
1568 struct ulptx_sc_memrd *memrd;
1569 struct ulptx_idata *idata;
1570 struct cpl_tx_sec_pdu *sec_pdu;
1571 struct cpl_tx_data *tx_data;
1572 struct tls_record_layer *hdr;
1574 u_int aad_start, aad_stop;
1575 u_int auth_start, auth_stop, auth_insert;
1576 u_int cipher_start, cipher_stop, iv_offset;
1577 u_int imm_len, mss, ndesc, offset, plen, tlen, twr_len, wr_len;
1578 u_int fields, tx_max_offset, tx_max;
1579 bool first_wr, last_wr, using_scratch;
1582 MPASS(tlsp->txq == txq);
1584 first_wr = (tlsp->prev_seq == 0 && tlsp->prev_ack == 0 &&
1585 tlsp->prev_win == 0);
1588 * Use the per-txq scratch pad if near the end of the ring to
1589 * simplify handling of wrap-around. This uses a simple but
1590 * not quite perfect test of using the scratch buffer if we
1591 * can't fit a maximal work request in without wrapping.
1593 using_scratch = (eq->sidx - pidx < SGE_MAX_WR_LEN / EQ_ESIZE);
1595 /* Locate the TLS header. */
1596 M_ASSERTEXTPG(m_tls);
1597 hdr = (void *)m_tls->m_epg_hdr;
1598 plen = TLS_HEADER_LENGTH + ntohs(hdr->tls_length) - m_tls->m_epg_trllen;
1600 /* Determine how much of the TLS record to send. */
1601 tlen = ktls_tcp_payload_length(tlsp, m_tls);
1602 if (tlen <= m_tls->m_epg_hdrlen) {
1604 * For requests that only want to send the TLS header,
1605 * send a tunnelled packet as immediate data.
1607 #ifdef VERBOSE_TRACES
1608 CTR3(KTR_CXGBE, "%s: tid %d header-only TLS record %u",
1609 __func__, tlsp->tid, (u_int)m_tls->m_epg_seqno);
1611 return (ktls_write_tunnel_packet(txq, dst, m, m_tls, available,
1616 offset = ktls_payload_offset(tlsp, m_tls);
1617 #ifdef VERBOSE_TRACES
1618 CTR4(KTR_CXGBE, "%s: tid %d short TLS record %u with offset %u",
1619 __func__, tlsp->tid, (u_int)m_tls->m_epg_seqno, offset);
1621 if (m_tls->m_next == NULL && (tcp->th_flags & TH_FIN) != 0) {
1622 txq->kern_tls_fin_short++;
1624 panic("%s: FIN on short TLS record", __func__);
1631 * This is the last work request for a given TLS mbuf chain if
1632 * it is the last mbuf in the chain and FIN is not set. If
1633 * FIN is set, then ktls_write_tcp_fin() will write out the
1634 * last work request.
1636 last_wr = m_tls->m_next == NULL && (tcp->th_flags & TH_FIN) == 0;
1639 * The host stack may ask us to not send part of the start of
1640 * a TLS record. (For example, the stack might have
1641 * previously sent a "short" TLS record and might later send
1642 * down an mbuf that requests to send the remainder of the TLS
1643 * record.) The crypto engine must process a TLS record from
1644 * the beginning if computing a GCM tag or HMAC, so we always
1645 * send the TLS record from the beginning as input to the
1646 * crypto engine and via CPL_TX_DATA to TP. However, TP will
1647 * drop individual packets after they have been chopped up
1648 * into MSS-sized chunks if the entire sequence range of those
1649 * packets is less than SND_UNA. SND_UNA is computed as
1650 * TX_MAX - SND_UNA_RAW. Thus, use the offset stored in
1651 * m_data to set TX_MAX to the first byte in the TCP sequence
1652 * space the host actually wants us to send and set
1655 * If the host sends us back to back requests that span the
1656 * trailer of a single TLS record (first request ends "in" the
1657 * trailer and second request starts at the next byte but
1658 * still "in" the trailer), the initial bytes of the trailer
1659 * that the first request drops will not be retransmitted. If
1660 * the host uses the same requests when retransmitting the
1661 * connection will hang. To handle this, always transmit the
1662 * full trailer for a request that begins "in" the trailer
1663 * (the second request in the example above). This should
1664 * also help to avoid retransmits for the common case.
1666 * A similar condition exists when using CBC for back to back
1667 * requests that span a single AES block. The first request
1668 * will be truncated to end at the end of the previous AES
1669 * block. To handle this, always begin transmission at the
1670 * start of the current AES block.
1672 tx_max_offset = mtod(m_tls, vm_offset_t);
1673 if (tx_max_offset > TLS_HEADER_LENGTH + ntohs(hdr->tls_length) -
1674 m_tls->m_epg_trllen) {
1675 /* Always send the full trailer. */
1676 tx_max_offset = TLS_HEADER_LENGTH + ntohs(hdr->tls_length) -
1677 m_tls->m_epg_trllen;
1679 if (tlsp->enc_mode == SCMD_CIPH_MODE_AES_CBC &&
1680 tx_max_offset > TLS_HEADER_LENGTH) {
1681 /* Always send all of the first AES block. */
1682 tx_max_offset = TLS_HEADER_LENGTH +
1683 rounddown(tx_max_offset - TLS_HEADER_LENGTH,
1686 tx_max = tcp_seqno + tx_max_offset;
1689 * Update TCB fields. Reserve space for the FW_ULPTX_WR header
1690 * but don't populate it until we know how many field updates
1694 wr = (void *)txq->ss;
1697 out = (void *)(wr + 1);
1701 ("trying to set L2T_IX for subsequent TLS WR"));
1702 #ifdef VERBOSE_TRACES
1703 CTR3(KTR_CXGBE, "%s: tid %d set L2T_IX to %d", __func__,
1704 tlsp->tid, tlsp->l2te->idx);
1706 write_set_tcb_field_ulp(tlsp, out, txq, W_TCB_L2T_IX,
1707 V_TCB_L2T_IX(M_TCB_L2T_IX), V_TCB_L2T_IX(tlsp->l2te->idx));
1708 out += roundup2(LEN__SET_TCB_FIELD_ULP, 16);
1711 if (tsopt != NULL && tlsp->prev_tsecr != ntohl(tsopt[1])) {
1713 ("trying to set T_RTSEQ_RECENT for subsequent TLS WR"));
1714 #ifdef VERBOSE_TRACES
1715 CTR2(KTR_CXGBE, "%s: tid %d wrote updated T_RTSEQ_RECENT",
1716 __func__, tlsp->tid);
1718 write_set_tcb_field_ulp(tlsp, out, txq, W_TCB_T_RTSEQ_RECENT,
1719 V_TCB_T_RTSEQ_RECENT(M_TCB_T_RTSEQ_RECENT),
1720 V_TCB_T_RTSEQ_RECENT(ntohl(tsopt[1])));
1721 out += roundup2(LEN__SET_TCB_FIELD_ULP, 16);
1724 tlsp->prev_tsecr = ntohl(tsopt[1]);
1727 if (first_wr || tlsp->prev_seq != tx_max) {
1729 ("trying to set TX_MAX for subsequent TLS WR"));
1730 #ifdef VERBOSE_TRACES
1732 "%s: tid %d setting TX_MAX to %u (tcp_seqno %u)",
1733 __func__, tlsp->tid, tx_max, tcp_seqno);
1735 write_set_tcb_field_ulp(tlsp, out, txq, W_TCB_TX_MAX,
1736 V_TCB_TX_MAX(M_TCB_TX_MAX), V_TCB_TX_MAX(tx_max));
1737 out += roundup2(LEN__SET_TCB_FIELD_ULP, 16);
1742 * If there is data to drop at the beginning of this TLS
1743 * record or if this is a retransmit,
1744 * reset SND_UNA_RAW to 0 so that SND_UNA == TX_MAX.
1746 if (tlsp->prev_seq != tx_max || mtod(m_tls, vm_offset_t) != 0) {
1748 ("trying to clear SND_UNA_RAW for subsequent TLS WR"));
1749 #ifdef VERBOSE_TRACES
1750 CTR2(KTR_CXGBE, "%s: tid %d clearing SND_UNA_RAW", __func__,
1753 write_set_tcb_field_ulp(tlsp, out, txq, W_TCB_SND_UNA_RAW,
1754 V_TCB_SND_UNA_RAW(M_TCB_SND_UNA_RAW),
1755 V_TCB_SND_UNA_RAW(0));
1756 out += roundup2(LEN__SET_TCB_FIELD_ULP, 16);
1761 * Store the expected sequence number of the next byte after
1764 tlsp->prev_seq = tcp_seqno + tlen;
1766 if (first_wr || tlsp->prev_ack != ntohl(tcp->th_ack)) {
1768 ("trying to set RCV_NXT for subsequent TLS WR"));
1769 write_set_tcb_field_ulp(tlsp, out, txq, W_TCB_RCV_NXT,
1770 V_TCB_RCV_NXT(M_TCB_RCV_NXT),
1771 V_TCB_RCV_NXT(ntohl(tcp->th_ack)));
1772 out += roundup2(LEN__SET_TCB_FIELD_ULP, 16);
1775 tlsp->prev_ack = ntohl(tcp->th_ack);
1778 if (first_wr || tlsp->prev_win != ntohs(tcp->th_win)) {
1780 ("trying to set RCV_WND for subsequent TLS WR"));
1781 write_set_tcb_field_ulp(tlsp, out, txq, W_TCB_RCV_WND,
1782 V_TCB_RCV_WND(M_TCB_RCV_WND),
1783 V_TCB_RCV_WND(ntohs(tcp->th_win)));
1784 out += roundup2(LEN__SET_TCB_FIELD_ULP, 16);
1787 tlsp->prev_win = ntohs(tcp->th_win);
1790 /* Recalculate 'nsegs' if cached value is not available. */
1792 nsegs = sglist_count_mbuf_epg(m_tls, m_tls->m_epg_hdrlen +
1793 offset, plen - (m_tls->m_epg_hdrlen + offset));
1795 /* Calculate the size of the TLS work request. */
1796 twr_len = ktls_base_wr_size(tlsp);
1800 imm_len += m_tls->m_epg_hdrlen;
1802 imm_len += AES_BLOCK_LEN;
1803 twr_len += roundup2(imm_len, 16);
1804 twr_len += ktls_sgl_size(nsegs);
1807 * If any field updates were required, determine if they can
1808 * be included in the TLS work request. If not, use the
1809 * FW_ULPTX_WR work request header at 'wr' as a dedicated work
1810 * request for the field updates and start a new work request
1811 * for the TLS work request afterward.
1814 wr_len = fields * roundup2(LEN__SET_TCB_FIELD_ULP, 16);
1815 if (twr_len + wr_len <= SGE_MAX_WR_LEN &&
1816 tlsp->sc->tlst.combo_wrs) {
1818 txpkt = (void *)out;
1820 wr_len += sizeof(*wr);
1821 wr->op_to_compl = htobe32(V_FW_WR_OP(FW_ULPTX_WR));
1822 wr->flowid_len16 = htobe32(F_FW_ULPTX_WR_DATA |
1823 V_FW_WR_LEN16(wr_len / 16));
1827 * If we were using scratch space, copy the
1828 * field updates work request to the ring.
1830 if (using_scratch) {
1832 copy_to_txd(eq, txq->ss, &out, wr_len);
1835 ndesc = howmany(wr_len, EQ_ESIZE);
1836 MPASS(ndesc <= available);
1839 txsd = &txq->sdesc[pidx];
1841 txsd->desc_used = ndesc;
1842 IDXINCR(pidx, ndesc, eq->sidx);
1843 dst = &eq->desc[pidx];
1846 * Determine if we should use scratch space
1847 * for the TLS work request based on the
1848 * available space after advancing pidx for
1849 * the field updates work request.
1852 using_scratch = (eq->sidx - pidx <
1853 howmany(wr_len, EQ_ESIZE));
1855 wr = (void *)txq->ss;
1858 txpkt = (void *)(wr + 1);
1862 txpkt = (void *)out;
1865 wr_len = roundup2(wr_len, 16);
1866 MPASS(ndesc + howmany(wr_len, EQ_ESIZE) <= available);
1869 wr->op_to_compl = htobe32(V_FW_WR_OP(FW_ULPTX_WR));
1870 wr->flowid_len16 = htobe32(F_FW_ULPTX_WR_DATA |
1871 V_FW_WR_LEN16(wr_len / 16));
1875 txpkt->cmd_dest = htobe32(V_ULPTX_CMD(ULP_TX_PKT) |
1876 V_ULP_TXPKT_DATAMODIFY(0) |
1877 V_ULP_TXPKT_CHANNELID(tlsp->vi->pi->port_id) | V_ULP_TXPKT_DEST(0) |
1878 V_ULP_TXPKT_FID(txq->eq.cntxt_id) | V_ULP_TXPKT_RO(1));
1879 txpkt->len = htobe32(howmany(twr_len - sizeof(*wr), 16));
1881 /* ULPTX_IDATA sub-command */
1882 idata = (void *)(txpkt + 1);
1883 idata->cmd_more = htobe32(V_ULPTX_CMD(ULP_TX_SC_IMM) |
1884 V_ULP_TX_SC_MORE(1));
1885 idata->len = sizeof(struct cpl_tx_sec_pdu);
1888 * The key context, CPL_TX_DATA, and immediate data are part
1889 * of this ULPTX_IDATA when using an inline key. When reading
1890 * the key from memory, the CPL_TX_DATA and immediate data are
1891 * part of a separate ULPTX_IDATA.
1893 if (tlsp->inline_key)
1894 idata->len += tlsp->tx_key_info_size +
1895 sizeof(struct cpl_tx_data) + imm_len;
1896 idata->len = htobe32(idata->len);
1898 /* CPL_TX_SEC_PDU */
1899 sec_pdu = (void *)(idata + 1);
1902 * For short records, AAD is counted as header data in SCMD0,
1903 * the IV is next followed by a cipher region for the payload.
1912 cipher_start = AES_BLOCK_LEN + 1;
1915 sec_pdu->pldlen = htobe32(16 + plen -
1916 (m_tls->m_epg_hdrlen + offset));
1918 /* These two flits are actually a CPL_TLS_TX_SCMD_FMT. */
1919 sec_pdu->seqno_numivs = tlsp->scmd0_short.seqno_numivs;
1920 sec_pdu->ivgen_hdrlen = htobe32(
1921 tlsp->scmd0_short.ivgen_hdrlen |
1922 V_SCMD_HDR_LEN(offset == 0 ? m_tls->m_epg_hdrlen : 0));
1924 txq->kern_tls_short++;
1927 * AAD is TLS header. IV is after AAD. The cipher region
1928 * starts after the IV. See comments in ccr_authenc() and
1929 * ccr_gmac() in t4_crypto.c regarding cipher and auth
1930 * start/stop values.
1933 aad_stop = TLS_HEADER_LENGTH;
1934 iv_offset = TLS_HEADER_LENGTH + 1;
1935 cipher_start = m_tls->m_epg_hdrlen + 1;
1936 if (tlsp->enc_mode == SCMD_CIPH_MODE_AES_GCM) {
1938 auth_start = cipher_start;
1943 auth_start = cipher_start;
1948 sec_pdu->pldlen = htobe32(plen);
1950 /* These two flits are actually a CPL_TLS_TX_SCMD_FMT. */
1951 sec_pdu->seqno_numivs = tlsp->scmd0.seqno_numivs;
1952 sec_pdu->ivgen_hdrlen = tlsp->scmd0.ivgen_hdrlen;
1954 if (mtod(m_tls, vm_offset_t) == 0)
1955 txq->kern_tls_full++;
1957 txq->kern_tls_partial++;
1959 sec_pdu->op_ivinsrtofst = htobe32(
1960 V_CPL_TX_SEC_PDU_OPCODE(CPL_TX_SEC_PDU) |
1961 V_CPL_TX_SEC_PDU_CPLLEN(2) | V_CPL_TX_SEC_PDU_PLACEHOLDER(0) |
1962 V_CPL_TX_SEC_PDU_IVINSRTOFST(iv_offset));
1963 sec_pdu->aadstart_cipherstop_hi = htobe32(
1964 V_CPL_TX_SEC_PDU_AADSTART(aad_start) |
1965 V_CPL_TX_SEC_PDU_AADSTOP(aad_stop) |
1966 V_CPL_TX_SEC_PDU_CIPHERSTART(cipher_start) |
1967 V_CPL_TX_SEC_PDU_CIPHERSTOP_HI(cipher_stop >> 4));
1968 sec_pdu->cipherstop_lo_authinsert = htobe32(
1969 V_CPL_TX_SEC_PDU_CIPHERSTOP_LO(cipher_stop & 0xf) |
1970 V_CPL_TX_SEC_PDU_AUTHSTART(auth_start) |
1971 V_CPL_TX_SEC_PDU_AUTHSTOP(auth_stop) |
1972 V_CPL_TX_SEC_PDU_AUTHINSERT(auth_insert));
1974 sec_pdu->scmd1 = htobe64(m_tls->m_epg_seqno);
1977 out = (void *)(sec_pdu + 1);
1978 if (tlsp->inline_key) {
1979 memcpy(out, &tlsp->keyctx, tlsp->tx_key_info_size);
1980 out += tlsp->tx_key_info_size;
1982 /* ULPTX_SC_MEMRD to read key context. */
1983 memrd = (void *)out;
1984 memrd->cmd_to_len = htobe32(V_ULPTX_CMD(ULP_TX_SC_MEMRD) |
1985 V_ULP_TX_SC_MORE(1) |
1986 V_ULPTX_LEN16(tlsp->tx_key_info_size >> 4));
1987 memrd->addr = htobe32(tlsp->tx_key_addr >> 5);
1989 /* ULPTX_IDATA for CPL_TX_DATA and TLS header. */
1990 idata = (void *)(memrd + 1);
1991 idata->cmd_more = htobe32(V_ULPTX_CMD(ULP_TX_SC_IMM) |
1992 V_ULP_TX_SC_MORE(1));
1993 idata->len = htobe32(sizeof(struct cpl_tx_data) + imm_len);
1995 out = (void *)(idata + 1);
1999 tx_data = (void *)out;
2000 OPCODE_TID(tx_data) = htonl(MK_OPCODE_TID(CPL_TX_DATA, tlsp->tid));
2001 if (m->m_pkthdr.csum_flags & CSUM_TSO) {
2002 mss = m->m_pkthdr.tso_segsz;
2003 tlsp->prev_mss = mss;
2004 } else if (tlsp->prev_mss != 0)
2005 mss = tlsp->prev_mss;
2007 mss = tlsp->vi->ifp->if_mtu -
2008 (m->m_pkthdr.l3hlen + m->m_pkthdr.l4hlen);
2010 tx_data->len = htobe32(V_TX_DATA_MSS(mss) | V_TX_LENGTH(tlen));
2011 tx_data->rsvd = htobe32(tcp_seqno);
2013 tx_data->len = htobe32(V_TX_DATA_MSS(mss) |
2014 V_TX_LENGTH(tlen - (m_tls->m_epg_hdrlen + offset)));
2015 tx_data->rsvd = htobe32(tcp_seqno + m_tls->m_epg_hdrlen + offset);
2017 tx_data->flags = htobe32(F_TX_BYPASS);
2018 if (last_wr && tcp->th_flags & TH_PUSH)
2019 tx_data->flags |= htobe32(F_TX_PUSH | F_TX_SHOVE);
2021 /* Populate the TLS header */
2022 out = (void *)(tx_data + 1);
2024 memcpy(out, m_tls->m_epg_hdr, m_tls->m_epg_hdrlen);
2025 out += m_tls->m_epg_hdrlen;
2028 /* AES IV for a short record. */
2031 if (tlsp->enc_mode == SCMD_CIPH_MODE_AES_GCM) {
2032 memcpy(iv, tlsp->keyctx.txhdr.txsalt, SALT_SIZE);
2033 memcpy(iv + 4, hdr + 1, 8);
2034 *(uint32_t *)(iv + 12) = htobe32(2 +
2035 offset / AES_BLOCK_LEN);
2037 memcpy(iv, hdr + 1, AES_BLOCK_LEN);
2038 out += AES_BLOCK_LEN;
2041 if (imm_len % 16 != 0) {
2042 /* Zero pad to an 8-byte boundary. */
2043 memset(out, 0, 8 - (imm_len % 8));
2044 out += 8 - (imm_len % 8);
2047 * Insert a ULP_TX_SC_NOOP if needed so the SGL is
2050 if (imm_len % 16 <= 8) {
2051 idata = (void *)out;
2052 idata->cmd_more = htobe32(V_ULPTX_CMD(ULP_TX_SC_NOOP));
2053 idata->len = htobe32(0);
2054 out = (void *)(idata + 1);
2058 /* SGL for record payload */
2059 sglist_reset(txq->gl);
2060 if (sglist_append_mbuf_epg(txq->gl, m_tls, m_tls->m_epg_hdrlen + offset,
2061 plen - (m_tls->m_epg_hdrlen + offset)) != 0) {
2063 panic("%s: failed to append sglist", __func__);
2066 write_gl_to_buf(txq->gl, out);
2068 if (using_scratch) {
2070 copy_to_txd(eq, txq->ss, &out, wr_len);
2073 ndesc += howmany(wr_len, EQ_ESIZE);
2074 MPASS(ndesc <= available);
2076 txq->kern_tls_records++;
2077 txq->kern_tls_octets += tlen - mtod(m_tls, vm_offset_t);
2078 if (mtod(m_tls, vm_offset_t) != 0) {
2080 txq->kern_tls_waste += mtod(m_tls, vm_offset_t);
2082 txq->kern_tls_waste += mtod(m_tls, vm_offset_t) -
2083 (m_tls->m_epg_hdrlen + offset);
2086 txsd = &txq->sdesc[pidx];
2091 txsd->desc_used = howmany(wr_len, EQ_ESIZE);
2097 ktls_write_tcp_fin(struct sge_txq *txq, void *dst, struct mbuf *m,
2098 u_int available, tcp_seq tcp_seqno, u_int pidx)
2100 struct tx_sdesc *txsd;
2101 struct fw_eth_tx_pkt_wr *wr;
2102 struct cpl_tx_pkt_core *cpl;
2105 int len16, ndesc, pktlen;
2106 struct ether_header *eh;
2107 struct ip *ip, newip;
2108 struct ip6_hdr *ip6, newip6;
2109 struct tcphdr *tcp, newtcp;
2112 TXQ_LOCK_ASSERT_OWNED(txq);
2117 ctrl = sizeof(struct cpl_tx_pkt_core) + pktlen;
2118 len16 = howmany(sizeof(struct fw_eth_tx_pkt_wr) + ctrl, 16);
2119 ndesc = tx_len16_to_desc(len16);
2120 MPASS(ndesc <= available);
2122 /* Firmware work request header */
2123 wr->op_immdlen = htobe32(V_FW_WR_OP(FW_ETH_TX_PKT_WR) |
2124 V_FW_ETH_TX_PKT_WR_IMMDLEN(ctrl));
2126 ctrl = V_FW_WR_LEN16(len16);
2127 wr->equiq_to_len16 = htobe32(ctrl);
2130 cpl = (void *)(wr + 1);
2133 cpl->ctrl0 = txq->cpl_ctrl0;
2135 cpl->len = htobe16(pktlen);
2137 out = (void *)(cpl + 1);
2139 /* Copy over Ethernet header. */
2140 eh = mtod(m, struct ether_header *);
2141 copy_to_txd(&txq->eq, (caddr_t)eh, &out, m->m_pkthdr.l2hlen);
2143 /* Fixup length in IP header and copy out. */
2144 if (ntohs(eh->ether_type) == ETHERTYPE_IP) {
2145 ip = (void *)((char *)eh + m->m_pkthdr.l2hlen);
2147 newip.ip_len = htons(pktlen - m->m_pkthdr.l2hlen);
2148 copy_to_txd(&txq->eq, (caddr_t)&newip, &out, sizeof(newip));
2149 if (m->m_pkthdr.l3hlen > sizeof(*ip))
2150 copy_to_txd(&txq->eq, (caddr_t)(ip + 1), &out,
2151 m->m_pkthdr.l3hlen - sizeof(*ip));
2152 ctrl1 = V_TXPKT_CSUM_TYPE(TX_CSUM_TCPIP) |
2153 V_T6_TXPKT_ETHHDR_LEN(m->m_pkthdr.l2hlen - ETHER_HDR_LEN) |
2154 V_TXPKT_IPHDR_LEN(m->m_pkthdr.l3hlen);
2156 ip6 = (void *)((char *)eh + m->m_pkthdr.l2hlen);
2158 newip6.ip6_plen = htons(pktlen - m->m_pkthdr.l2hlen);
2159 copy_to_txd(&txq->eq, (caddr_t)&newip6, &out, sizeof(newip6));
2160 MPASS(m->m_pkthdr.l3hlen == sizeof(*ip6));
2161 ctrl1 = V_TXPKT_CSUM_TYPE(TX_CSUM_TCPIP6) |
2162 V_T6_TXPKT_ETHHDR_LEN(m->m_pkthdr.l2hlen - ETHER_HDR_LEN) |
2163 V_TXPKT_IPHDR_LEN(m->m_pkthdr.l3hlen);
2165 cpl->ctrl1 = htobe64(ctrl1);
2168 /* Set sequence number in TCP header. */
2169 tcp = (void *)((char *)eh + m->m_pkthdr.l2hlen + m->m_pkthdr.l3hlen);
2171 newtcp.th_seq = htonl(tcp_seqno);
2172 copy_to_txd(&txq->eq, (caddr_t)&newtcp, &out, sizeof(newtcp));
2174 /* Copy rest of packet. */
2175 copy_to_txd(&txq->eq, (caddr_t)(tcp + 1), &out, m->m_len -
2176 (m->m_pkthdr.l2hlen + m->m_pkthdr.l3hlen + sizeof(*tcp)));
2181 txq->kern_tls_fin++;
2183 txsd = &txq->sdesc[pidx];
2185 txsd->desc_used = ndesc;
2191 t6_ktls_write_wr(struct sge_txq *txq, void *dst, struct mbuf *m, u_int nsegs,
2194 struct sge_eq *eq = &txq->eq;
2195 struct tx_sdesc *txsd;
2196 struct tlspcb *tlsp;
2199 struct ether_header *eh;
2201 u_int ndesc, pidx, totdesc;
2203 bool has_fin, set_l2t_idx;
2207 MPASS(m->m_pkthdr.snd_tag != NULL);
2208 tlsp = mst_to_tls(m->m_pkthdr.snd_tag);
2211 eh = mtod(m, struct ether_header *);
2212 tcp = (struct tcphdr *)((char *)eh + m->m_pkthdr.l2hlen +
2213 m->m_pkthdr.l3hlen);
2215 has_fin = (tcp->th_flags & TH_FIN) != 0;
2218 * If this TLS record has a FIN, then we will send any
2219 * requested options as part of the FIN packet.
2221 if (!has_fin && ktls_has_tcp_options(tcp)) {
2222 ndesc = ktls_write_tcp_options(txq, dst, m, available, pidx);
2224 IDXINCR(pidx, ndesc, eq->sidx);
2225 dst = &eq->desc[pidx];
2226 #ifdef VERBOSE_TRACES
2227 CTR2(KTR_CXGBE, "%s: tid %d wrote TCP options packet", __func__,
2233 * Allocate a new L2T entry if necessary. This may write out
2234 * a work request to the txq.
2236 if (m->m_flags & M_VLANTAG)
2237 vlan_tag = m->m_pkthdr.ether_vtag;
2240 set_l2t_idx = false;
2241 if (tlsp->l2te == NULL || tlsp->l2te->vlan != vlan_tag ||
2242 memcmp(tlsp->l2te->dmac, eh->ether_dhost, ETHER_ADDR_LEN) != 0) {
2245 t4_l2t_release(tlsp->l2te);
2246 tlsp->l2te = t4_l2t_alloc_tls(tlsp->sc, txq, dst, &ndesc,
2247 vlan_tag, tlsp->vi->pi->lport, eh->ether_dhost);
2248 if (tlsp->l2te == NULL)
2249 CXGBE_UNIMPLEMENTED("failed to allocate TLS L2TE");
2251 MPASS(ndesc <= available - totdesc);
2254 txsd = &txq->sdesc[pidx];
2256 txsd->desc_used = ndesc;
2258 IDXINCR(pidx, ndesc, eq->sidx);
2259 dst = &eq->desc[pidx];
2264 * Iterate over each TLS record constructing a work request
2267 for (m_tls = m->m_next; m_tls != NULL; m_tls = m_tls->m_next) {
2268 MPASS(m_tls->m_flags & M_EXTPG);
2271 * Determine the initial TCP sequence number for this
2275 if (m_tls == m->m_next) {
2276 tcp_seqno = ntohl(tcp->th_seq) -
2277 mtod(m_tls, vm_offset_t);
2278 if (tlsp->using_timestamps)
2279 tsopt = ktls_find_tcp_timestamps(tcp);
2281 MPASS(mtod(m_tls, vm_offset_t) == 0);
2282 tcp_seqno = tlsp->prev_seq;
2285 ndesc = ktls_write_tls_wr(tlsp, txq, dst, m, tcp, m_tls,
2286 nsegs, available - totdesc, tcp_seqno, tsopt, pidx,
2289 IDXINCR(pidx, ndesc, eq->sidx);
2290 dst = &eq->desc[pidx];
2293 * The value of nsegs from the header mbuf's metadata
2294 * is only valid for the first TLS record.
2298 /* Only need to set the L2T index once. */
2299 set_l2t_idx = false;
2304 * If the TCP header for this chain has FIN sent, then
2305 * explicitly send a packet that has FIN set. This
2306 * will also have PUSH set if requested. This assumes
2307 * we sent at least one TLS record work request and
2308 * uses the TCP sequence number after that reqeust as
2309 * the sequence number for the FIN packet.
2311 ndesc = ktls_write_tcp_fin(txq, dst, m, available,
2312 tlsp->prev_seq, pidx);
2316 MPASS(totdesc <= available);
2321 cxgbe_tls_tag_free(struct m_snd_tag *mst)
2324 struct tlspcb *tlsp;
2326 tlsp = mst_to_tls(mst);
2329 CTR2(KTR_CXGBE, "%s: tid %d", __func__, tlsp->tid);
2332 t4_l2t_release(tlsp->l2te);
2334 release_tid(sc, tlsp->tid, tlsp->ctrlq);
2336 t4_release_lip(sc, tlsp->ce);
2337 if (tlsp->tx_key_addr >= 0)
2338 free_keyid(tlsp, tlsp->tx_key_addr);
2340 zfree(tlsp, M_CXGBE);
2344 t6_ktls_modload(void)
2347 t4_register_shared_cpl_handler(CPL_ACT_OPEN_RPL, ktls_act_open_rpl,
2348 CPL_COOKIE_KERN_TLS);
2352 t6_ktls_modunload(void)
2355 t4_register_shared_cpl_handler(CPL_ACT_OPEN_RPL, NULL,
2356 CPL_COOKIE_KERN_TLS);
2362 cxgbe_tls_tag_alloc(struct ifnet *ifp, union if_snd_tag_alloc_params *params,
2363 struct m_snd_tag **pt)
2369 t6_ktls_parse_pkt(struct mbuf *m, int *nsegsp, int *len16p)
2375 t6_ktls_write_wr(struct sge_txq *txq, void *dst, struct mbuf *m, u_int nsegs,
2378 panic("can't happen");
2382 cxgbe_tls_tag_free(struct m_snd_tag *mst)
2384 panic("can't happen");
2388 t6_ktls_modload(void)
2393 t6_ktls_modunload(void)