All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH net-next v2] Crypto/chtls: add/delete TLS header in driver
@ 2020-03-19  4:41 Rohit Maheshwari
  2020-03-19 18:18 ` Jakub Kicinski
  2020-03-23 20:05 ` David Miller
  0 siblings, 2 replies; 3+ messages in thread
From: Rohit Maheshwari @ 2020-03-19  4:41 UTC (permalink / raw)
  To: herbert, davem, netdev, kuba
  Cc: borisp, secdev, Rohit Maheshwari, Vinay Kumar Yadav

Kernel TLS forms TLS header in kernel during encryption and removes
while decryption before giving packet back to user application. The
similar logic is introduced in chtls code as well.

v1->v2:
- tls_proccess_cmsg() uses tls_handle_open_record() which is not required
  in TOE-TLS. Don't mix TOE with other TLS types.

Signed-off-by: Vinay Kumar Yadav <vinay.yadav@chelsio.com>
Signed-off-by: Rohit Maheshwari <rohitm@chelsio.com>
---
 drivers/crypto/chelsio/chtls/chtls_io.c | 73 ++++++++++++++++++++-----
 1 file changed, 59 insertions(+), 14 deletions(-)

diff --git a/drivers/crypto/chelsio/chtls/chtls_io.c b/drivers/crypto/chelsio/chtls/chtls_io.c
index 5cf9b021220b..e1651adb9d06 100644
--- a/drivers/crypto/chelsio/chtls/chtls_io.c
+++ b/drivers/crypto/chelsio/chtls/chtls_io.c
@@ -902,14 +902,6 @@ static int chtls_skb_copy_to_page_nocache(struct sock *sk,
 	return 0;
 }
 
-/* Read TLS header to find content type and data length */
-static int tls_header_read(struct tls_hdr *thdr, struct iov_iter *from)
-{
-	if (copy_from_iter(thdr, sizeof(*thdr), from) != sizeof(*thdr))
-		return -EFAULT;
-	return (__force int)cpu_to_be16(thdr->length);
-}
-
 static int csk_mem_free(struct chtls_dev *cdev, struct sock *sk)
 {
 	return (cdev->max_host_sndbuf - sk->sk_wmem_queued);
@@ -981,6 +973,37 @@ static int csk_wait_memory(struct chtls_dev *cdev,
 	goto do_rm_wq;
 }
 
+static int chtls_proccess_cmsg(struct sock *sk, struct msghdr *msg,
+			       unsigned char *record_type)
+{
+	struct cmsghdr *cmsg;
+	int rc = -EINVAL;
+
+	for_each_cmsghdr(cmsg, msg) {
+		if (!CMSG_OK(msg, cmsg))
+			return -EINVAL;
+		if (cmsg->cmsg_level != SOL_TLS)
+			continue;
+
+		switch (cmsg->cmsg_type) {
+		case TLS_SET_RECORD_TYPE:
+			if (cmsg->cmsg_len < CMSG_LEN(sizeof(*record_type)))
+				return -EINVAL;
+
+			if (msg->msg_flags & MSG_MORE)
+				return -EINVAL;
+
+			*record_type = *(unsigned char *)CMSG_DATA(cmsg);
+			rc = 0;
+			break;
+		default:
+			return -EINVAL;
+		}
+	}
+
+	return rc;
+}
+
 int chtls_sendmsg(struct sock *sk, struct msghdr *msg, size_t size)
 {
 	struct chtls_sock *csk = rcu_dereference_sk_user_data(sk);
@@ -1022,15 +1045,21 @@ int chtls_sendmsg(struct sock *sk, struct msghdr *msg, size_t size)
 			goto wait_for_sndbuf;
 
 		if (is_tls_tx(csk) && !csk->tlshws.txleft) {
-			struct tls_hdr hdr;
+			unsigned char record_type = TLS_RECORD_TYPE_DATA;
 
-			recordsz = tls_header_read(&hdr, &msg->msg_iter);
-			size -= TLS_HEADER_LENGTH;
-			copied += TLS_HEADER_LENGTH;
+			if (unlikely(msg->msg_controllen)) {
+				err = chtls_proccess_cmsg(sk, msg,
+							  &record_type);
+				if (err)
+					goto out_err;
+			}
+
+			recordsz = size;
 			csk->tlshws.txleft = recordsz;
-			csk->tlshws.type = hdr.type;
+			csk->tlshws.type = record_type;
+
 			if (skb)
-				ULP_SKB_CB(skb)->ulp.tls.type = hdr.type;
+				ULP_SKB_CB(skb)->ulp.tls.type = record_type;
 		}
 
 		if (!skb || (ULP_SKB_CB(skb)->flags & ULPCB_FLAG_NO_APPEND) ||
@@ -1521,6 +1550,22 @@ static int chtls_pt_recvmsg(struct sock *sk, struct msghdr *msg, size_t len,
 				}
 			}
 		}
+		/* Set record type if not already done. For a non-data record,
+		 * do not proceed if record type could not be copied.
+		 */
+		if (ULP_SKB_CB(skb)->flags & ULPCB_FLAG_TLS_HDR) {
+			struct tls_hdr *thdr = (struct tls_hdr *)skb->data;
+			int cerr = 0;
+
+			cerr = put_cmsg(msg, SOL_TLS, TLS_GET_RECORD_TYPE,
+					sizeof(thdr->type), &thdr->type);
+
+			if (cerr && thdr->type != TLS_RECORD_TYPE_DATA)
+				return -EIO;
+			/*  don't send tls header, skip copy */
+			goto skip_copy;
+		}
+
 		if (skb_copy_datagram_msg(skb, offset, msg, avail)) {
 			if (!copied) {
 				copied = -EFAULT;
-- 
2.18.1


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH net-next v2] Crypto/chtls: add/delete TLS header in driver
  2020-03-19  4:41 [PATCH net-next v2] Crypto/chtls: add/delete TLS header in driver Rohit Maheshwari
@ 2020-03-19 18:18 ` Jakub Kicinski
  2020-03-23 20:05 ` David Miller
  1 sibling, 0 replies; 3+ messages in thread
From: Jakub Kicinski @ 2020-03-19 18:18 UTC (permalink / raw)
  To: Rohit Maheshwari
  Cc: herbert, davem, netdev, borisp, secdev, Vinay Kumar Yadav

On Thu, 19 Mar 2020 10:11:21 +0530 Rohit Maheshwari wrote:
> Kernel TLS forms TLS header in kernel during encryption and removes
> while decryption before giving packet back to user application. The
> similar logic is introduced in chtls code as well.
> 
> v1->v2:
> - tls_proccess_cmsg() uses tls_handle_open_record() which is not required
>   in TOE-TLS. Don't mix TOE with other TLS types.
> 
> Signed-off-by: Vinay Kumar Yadav <vinay.yadav@chelsio.com>
> Signed-off-by: Rohit Maheshwari <rohitm@chelsio.com>

Acked-by: Jakub Kicinski <kuba@kernel.org>

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH net-next v2] Crypto/chtls: add/delete TLS header in driver
  2020-03-19  4:41 [PATCH net-next v2] Crypto/chtls: add/delete TLS header in driver Rohit Maheshwari
  2020-03-19 18:18 ` Jakub Kicinski
@ 2020-03-23 20:05 ` David Miller
  1 sibling, 0 replies; 3+ messages in thread
From: David Miller @ 2020-03-23 20:05 UTC (permalink / raw)
  To: rohitm; +Cc: herbert, netdev, kuba, borisp, secdev, vinay.yadav

From: Rohit Maheshwari <rohitm@chelsio.com>
Date: Thu, 19 Mar 2020 10:11:21 +0530

> Kernel TLS forms TLS header in kernel during encryption and removes
> while decryption before giving packet back to user application. The
> similar logic is introduced in chtls code as well.
> 
> v1->v2:
> - tls_proccess_cmsg() uses tls_handle_open_record() which is not required
>   in TOE-TLS. Don't mix TOE with other TLS types.
> 
> Signed-off-by: Vinay Kumar Yadav <vinay.yadav@chelsio.com>
> Signed-off-by: Rohit Maheshwari <rohitm@chelsio.com>

Applied, thank you.

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2020-03-23 20:05 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-03-19  4:41 [PATCH net-next v2] Crypto/chtls: add/delete TLS header in driver Rohit Maheshwari
2020-03-19 18:18 ` Jakub Kicinski
2020-03-23 20:05 ` David Miller

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.