Skip to contentSkip to conversation

crypto: AF_ALG - consolidate common code in recvmsg

PATCH New Stephan Müller 1 participant 1 message Aug 1, 2017 Crypto API

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 crypto: AF_ALG - consolidate common code in recvmsg

The recvmsg handler has the following common code that is consolidated:

* af_alg_alloc_areq: allocation of the request data structure for the cipher operation

* af_alg_get_rsgl: creation of the RX SGL anchored in the request data structure

The request data structure is extended by the field last_rsgl which points to the last RX SGL list entry. This shall help recvmsg implementation to chain the RX SGL to other SG(L)s if needed. It is currently used by algif_aead which chains the tag SGL to the RX SGL during decryption.

---
 crypto/af_alg.c         | 96 +++++++++++++++++++++++++++++++++++++++++++++++++
 crypto/algif_aead.c     | 70 ++++++------------------------------
 crypto/algif_skcipher.c | 65 +++++----------------------------
 include/crypto/if_alg.h |  7 ++++
 4 files changed, 121 insertions(+), 117 deletions(-)
crypto/af_alg.c +96 -0
index ae0e93103c76..d6936c0e08d9 100644
@@ -1104,6 +1104,102 @@ unsigned int af_alg_poll(struct file *file, struct socket *sock,
 }
 EXPORT_SYMBOL_GPL(af_alg_poll);
 
+/**
+ * af_alg_alloc_areq - allocate struct af_alg_async_req
+ *
+ * @sk socket of connection to user space
+ * @areqlen size of struct af_alg_async_req + crypto_*_reqsize
+ * @return allocated data structure or ERR_PTR upon error
+ */
+struct af_alg_async_req *af_alg_alloc_areq(struct sock *sk,
+					   unsigned int areqlen)
+{
+	struct af_alg_async_req *areq = sock_kmalloc(sk, areqlen, GFP_KERNEL);
+
+	if (unlikely(!areq))
+		return ERR_PTR(-ENOMEM);
+
+	areq->areqlen = areqlen;
+	areq->sk = sk;
+	areq->last_rsgl = NULL;
+	INIT_LIST_HEAD(&areq->rsgl_list);
+	areq->tsgl = NULL;
+	areq->tsgl_entries = 0;
+
+	return areq;
+}
+EXPORT_SYMBOL_GPL(af_alg_alloc_areq);
+
+/**
+ * af_alg_get_rsgl - create the RX SGL for the output data from the crypto
+ *		     operation
+ *
+ * @sk socket of connection to user space
+ * @msg user space message
+ * @flags flags used to invoke recvmsg with
+ * @areq instance of the cryptographic request that will hold the RX SGL
+ * @maxsize maximum number of bytes to be pulled from user space
+ * @outlen number of bytes in the RX SGL
+ * @return 0 on success, < 0 upon error
+ */
+int af_alg_get_rsgl(struct sock *sk, struct msghdr *msg, int flags,
+		    struct af_alg_async_req *areq, size_t maxsize,
+		    size_t *outlen)
+{
+	struct alg_sock *ask = alg_sk(sk);
+	struct af_alg_ctx *ctx = ask->private;
+	size_t len = 0;
+
+	while (maxsize > len && msg_data_left(msg)) {
+		struct af_alg_rsgl *rsgl;
+		size_t seglen;
+		int err;
+
+		/* limit the amount of readable buffers */
+		if (!af_alg_readable(sk))
+			break;
+
+		if (!ctx->used) {
+			err = af_alg_wait_for_data(sk, flags);
+			if (err)
+				return err;
+		}
+
+		seglen = min_t(size_t, (maxsize - len),
+			       msg_data_left(msg));
+
+		if (list_empty(&areq->rsgl_list)) {
+			rsgl = &areq->first_rsgl;
+		} else {
+			rsgl = sock_kmalloc(sk, sizeof(*rsgl), GFP_KERNEL);
+			if (unlikely(!rsgl))
+				return -ENOMEM;
+		}
+
+		rsgl->sgl.npages = 0;
+		list_add_tail(&rsgl->list, &areq->rsgl_list);
+
+		/* make one iovec available as scatterlist */
+		err = af_alg_make_sg(&rsgl->sgl, &msg->msg_iter, seglen);
+		if (err < 0)
+			return err;
+
+		/* chain the new scatterlist with previous one */
+		if (areq->last_rsgl)
+			af_alg_link_sg(&areq->last_rsgl->sgl, &rsgl->sgl);
+
+		areq->last_rsgl = rsgl;
+		len += err;
+		ctx->rcvused += err;
+		rsgl->sg_num_bytes = err;
+		iov_iter_advance(&msg->msg_iter, err);
+	}
+
+	*outlen = len;
+	return 0;
+}
+EXPORT_SYMBOL_GPL(af_alg_get_rsgl);
+
 static int __init af_alg_init(void)
 {
 	int err = proto_register(&alg_proto, 0);
crypto/algif_aead.c +10 -60

98 lines not on this page · raw message

crypto/algif_skcipher.c +8 -57

84 lines not on this page · raw message

include/crypto/if_alg.h +7 -0
index 7bac3fee6061..75ec9c662268 100644
@@ -93,6 +93,7 @@ struct af_alg_rsgl {
  * @iocb:		IOCB for AIO operations
  * @sk:			Socket the request is associated with
  * @first_rsgl:		First RX SG
+ * @last_rsgl:		Pointer to last RX SG
  * @rsgl_list:		Track RX SGs
  * @tsgl:		Private, per request TX SGL of buffers to process
  * @tsgl_entries:	Number of entries in priv. TX SGL
@@ -105,6 +106,7 @@ struct af_alg_async_req {
 	struct sock *sk;
 
 	struct af_alg_rsgl first_rsgl;
+	struct af_alg_rsgl *last_rsgl;
 	struct list_head rsgl_list;
 
 	struct scatterlist *tsgl;
@@ -256,5 +258,10 @@ ssize_t af_alg_sendpage(struct socket *sock, struct page *page,
 void af_alg_async_cb(struct crypto_async_request *_req, int err);
 unsigned int af_alg_poll(struct file *file, struct socket *sock,
 			 poll_table *wait);
+struct af_alg_async_req *af_alg_alloc_areq(struct sock *sk,
+					   unsigned int areqlen);
+int af_alg_get_rsgl(struct sock *sk, struct msghdr *msg, int flags,
+		    struct af_alg_async_req *areq, size_t maxsize,
+		    size_t *outlen);
 
 #endif	/* _CRYPTO_IF_ALG_H */
Signature · 1 line
2.13.3