xref: /openbmc/linux/net/tls/tls_main.c (revision 9134211f)
1 /*
2  * Copyright (c) 2016-2017, Mellanox Technologies. All rights reserved.
3  * Copyright (c) 2016-2017, Dave Watson <davejwatson@fb.com>. All rights reserved.
4  *
5  * This software is available to you under a choice of one of two
6  * licenses.  You may choose to be licensed under the terms of the GNU
7  * General Public License (GPL) Version 2, available from the file
8  * COPYING in the main directory of this source tree, or the
9  * OpenIB.org BSD license below:
10  *
11  *     Redistribution and use in source and binary forms, with or
12  *     without modification, are permitted provided that the following
13  *     conditions are met:
14  *
15  *      - Redistributions of source code must retain the above
16  *        copyright notice, this list of conditions and the following
17  *        disclaimer.
18  *
19  *      - Redistributions in binary form must reproduce the above
20  *        copyright notice, this list of conditions and the following
21  *        disclaimer in the documentation and/or other materials
22  *        provided with the distribution.
23  *
24  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
28  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
29  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
30  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
31  * SOFTWARE.
32  */
33 
34 #include <linux/module.h>
35 
36 #include <net/tcp.h>
37 #include <net/inet_common.h>
38 #include <linux/highmem.h>
39 #include <linux/netdevice.h>
40 #include <linux/sched/signal.h>
41 #include <linux/inetdevice.h>
42 #include <linux/inet_diag.h>
43 
44 #include <net/snmp.h>
45 #include <net/tls.h>
46 #include <net/tls_toe.h>
47 
48 #include "tls.h"
49 
50 MODULE_AUTHOR("Mellanox Technologies");
51 MODULE_DESCRIPTION("Transport Layer Security Support");
52 MODULE_LICENSE("Dual BSD/GPL");
53 MODULE_ALIAS_TCP_ULP("tls");
54 
55 enum {
56 	TLSV4,
57 	TLSV6,
58 	TLS_NUM_PROTS,
59 };
60 
61 #define CIPHER_SIZE_DESC(cipher) [cipher] = { \
62 	.iv = cipher ## _IV_SIZE, \
63 	.key = cipher ## _KEY_SIZE, \
64 	.salt = cipher ## _SALT_SIZE, \
65 	.tag = cipher ## _TAG_SIZE, \
66 	.rec_seq = cipher ## _REC_SEQ_SIZE, \
67 }
68 
69 const struct tls_cipher_size_desc tls_cipher_size_desc[] = {
70 	CIPHER_SIZE_DESC(TLS_CIPHER_AES_GCM_128),
71 	CIPHER_SIZE_DESC(TLS_CIPHER_AES_GCM_256),
72 	CIPHER_SIZE_DESC(TLS_CIPHER_AES_CCM_128),
73 	CIPHER_SIZE_DESC(TLS_CIPHER_CHACHA20_POLY1305),
74 	CIPHER_SIZE_DESC(TLS_CIPHER_SM4_GCM),
75 	CIPHER_SIZE_DESC(TLS_CIPHER_SM4_CCM),
76 };
77 
78 static const struct proto *saved_tcpv6_prot;
79 static DEFINE_MUTEX(tcpv6_prot_mutex);
80 static const struct proto *saved_tcpv4_prot;
81 static DEFINE_MUTEX(tcpv4_prot_mutex);
82 static struct proto tls_prots[TLS_NUM_PROTS][TLS_NUM_CONFIG][TLS_NUM_CONFIG];
83 static struct proto_ops tls_proto_ops[TLS_NUM_PROTS][TLS_NUM_CONFIG][TLS_NUM_CONFIG];
84 static void build_protos(struct proto prot[TLS_NUM_CONFIG][TLS_NUM_CONFIG],
85 			 const struct proto *base);
86 
87 void update_sk_prot(struct sock *sk, struct tls_context *ctx)
88 {
89 	int ip_ver = sk->sk_family == AF_INET6 ? TLSV6 : TLSV4;
90 
91 	WRITE_ONCE(sk->sk_prot,
92 		   &tls_prots[ip_ver][ctx->tx_conf][ctx->rx_conf]);
93 	WRITE_ONCE(sk->sk_socket->ops,
94 		   &tls_proto_ops[ip_ver][ctx->tx_conf][ctx->rx_conf]);
95 }
96 
97 int wait_on_pending_writer(struct sock *sk, long *timeo)
98 {
99 	int rc = 0;
100 	DEFINE_WAIT_FUNC(wait, woken_wake_function);
101 
102 	add_wait_queue(sk_sleep(sk), &wait);
103 	while (1) {
104 		if (!*timeo) {
105 			rc = -EAGAIN;
106 			break;
107 		}
108 
109 		if (signal_pending(current)) {
110 			rc = sock_intr_errno(*timeo);
111 			break;
112 		}
113 
114 		if (sk_wait_event(sk, timeo,
115 				  !READ_ONCE(sk->sk_write_pending), &wait))
116 			break;
117 	}
118 	remove_wait_queue(sk_sleep(sk), &wait);
119 	return rc;
120 }
121 
122 int tls_push_sg(struct sock *sk,
123 		struct tls_context *ctx,
124 		struct scatterlist *sg,
125 		u16 first_offset,
126 		int flags)
127 {
128 	struct bio_vec bvec;
129 	struct msghdr msg = {
130 		.msg_flags = MSG_SPLICE_PAGES | flags,
131 	};
132 	int ret = 0;
133 	struct page *p;
134 	size_t size;
135 	int offset = first_offset;
136 
137 	size = sg->length - offset;
138 	offset += sg->offset;
139 
140 	ctx->splicing_pages = true;
141 	while (1) {
142 		if (sg_is_last(sg))
143 			msg.msg_flags = flags;
144 
145 		/* is sending application-limited? */
146 		tcp_rate_check_app_limited(sk);
147 		p = sg_page(sg);
148 retry:
149 		bvec_set_page(&bvec, p, size, offset);
150 		iov_iter_bvec(&msg.msg_iter, ITER_SOURCE, &bvec, 1, size);
151 
152 		ret = tcp_sendmsg_locked(sk, &msg, size);
153 
154 		if (ret != size) {
155 			if (ret > 0) {
156 				offset += ret;
157 				size -= ret;
158 				goto retry;
159 			}
160 
161 			offset -= sg->offset;
162 			ctx->partially_sent_offset = offset;
163 			ctx->partially_sent_record = (void *)sg;
164 			ctx->splicing_pages = false;
165 			return ret;
166 		}
167 
168 		put_page(p);
169 		sk_mem_uncharge(sk, sg->length);
170 		sg = sg_next(sg);
171 		if (!sg)
172 			break;
173 
174 		offset = sg->offset;
175 		size = sg->length;
176 	}
177 
178 	ctx->splicing_pages = false;
179 
180 	return 0;
181 }
182 
183 static int tls_handle_open_record(struct sock *sk, int flags)
184 {
185 	struct tls_context *ctx = tls_get_ctx(sk);
186 
187 	if (tls_is_pending_open_record(ctx))
188 		return ctx->push_pending_record(sk, flags);
189 
190 	return 0;
191 }
192 
193 int tls_process_cmsg(struct sock *sk, struct msghdr *msg,
194 		     unsigned char *record_type)
195 {
196 	struct cmsghdr *cmsg;
197 	int rc = -EINVAL;
198 
199 	for_each_cmsghdr(cmsg, msg) {
200 		if (!CMSG_OK(msg, cmsg))
201 			return -EINVAL;
202 		if (cmsg->cmsg_level != SOL_TLS)
203 			continue;
204 
205 		switch (cmsg->cmsg_type) {
206 		case TLS_SET_RECORD_TYPE:
207 			if (cmsg->cmsg_len < CMSG_LEN(sizeof(*record_type)))
208 				return -EINVAL;
209 
210 			if (msg->msg_flags & MSG_MORE)
211 				return -EINVAL;
212 
213 			rc = tls_handle_open_record(sk, msg->msg_flags);
214 			if (rc)
215 				return rc;
216 
217 			*record_type = *(unsigned char *)CMSG_DATA(cmsg);
218 			rc = 0;
219 			break;
220 		default:
221 			return -EINVAL;
222 		}
223 	}
224 
225 	return rc;
226 }
227 
228 int tls_push_partial_record(struct sock *sk, struct tls_context *ctx,
229 			    int flags)
230 {
231 	struct scatterlist *sg;
232 	u16 offset;
233 
234 	sg = ctx->partially_sent_record;
235 	offset = ctx->partially_sent_offset;
236 
237 	ctx->partially_sent_record = NULL;
238 	return tls_push_sg(sk, ctx, sg, offset, flags);
239 }
240 
241 void tls_free_partial_record(struct sock *sk, struct tls_context *ctx)
242 {
243 	struct scatterlist *sg;
244 
245 	for (sg = ctx->partially_sent_record; sg; sg = sg_next(sg)) {
246 		put_page(sg_page(sg));
247 		sk_mem_uncharge(sk, sg->length);
248 	}
249 	ctx->partially_sent_record = NULL;
250 }
251 
252 static void tls_write_space(struct sock *sk)
253 {
254 	struct tls_context *ctx = tls_get_ctx(sk);
255 
256 	/* If splicing_pages call lower protocol write space handler
257 	 * to ensure we wake up any waiting operations there. For example
258 	 * if splicing pages where to call sk_wait_event.
259 	 */
260 	if (ctx->splicing_pages) {
261 		ctx->sk_write_space(sk);
262 		return;
263 	}
264 
265 #ifdef CONFIG_TLS_DEVICE
266 	if (ctx->tx_conf == TLS_HW)
267 		tls_device_write_space(sk, ctx);
268 	else
269 #endif
270 		tls_sw_write_space(sk, ctx);
271 
272 	ctx->sk_write_space(sk);
273 }
274 
275 /**
276  * tls_ctx_free() - free TLS ULP context
277  * @sk:  socket to with @ctx is attached
278  * @ctx: TLS context structure
279  *
280  * Free TLS context. If @sk is %NULL caller guarantees that the socket
281  * to which @ctx was attached has no outstanding references.
282  */
283 void tls_ctx_free(struct sock *sk, struct tls_context *ctx)
284 {
285 	if (!ctx)
286 		return;
287 
288 	memzero_explicit(&ctx->crypto_send, sizeof(ctx->crypto_send));
289 	memzero_explicit(&ctx->crypto_recv, sizeof(ctx->crypto_recv));
290 	mutex_destroy(&ctx->tx_lock);
291 
292 	if (sk)
293 		kfree_rcu(ctx, rcu);
294 	else
295 		kfree(ctx);
296 }
297 
298 static void tls_sk_proto_cleanup(struct sock *sk,
299 				 struct tls_context *ctx, long timeo)
300 {
301 	if (unlikely(sk->sk_write_pending) &&
302 	    !wait_on_pending_writer(sk, &timeo))
303 		tls_handle_open_record(sk, 0);
304 
305 	/* We need these for tls_sw_fallback handling of other packets */
306 	if (ctx->tx_conf == TLS_SW) {
307 		kfree(ctx->tx.rec_seq);
308 		kfree(ctx->tx.iv);
309 		tls_sw_release_resources_tx(sk);
310 		TLS_DEC_STATS(sock_net(sk), LINUX_MIB_TLSCURRTXSW);
311 	} else if (ctx->tx_conf == TLS_HW) {
312 		tls_device_free_resources_tx(sk);
313 		TLS_DEC_STATS(sock_net(sk), LINUX_MIB_TLSCURRTXDEVICE);
314 	}
315 
316 	if (ctx->rx_conf == TLS_SW) {
317 		tls_sw_release_resources_rx(sk);
318 		TLS_DEC_STATS(sock_net(sk), LINUX_MIB_TLSCURRRXSW);
319 	} else if (ctx->rx_conf == TLS_HW) {
320 		tls_device_offload_cleanup_rx(sk);
321 		TLS_DEC_STATS(sock_net(sk), LINUX_MIB_TLSCURRRXDEVICE);
322 	}
323 }
324 
325 static void tls_sk_proto_close(struct sock *sk, long timeout)
326 {
327 	struct inet_connection_sock *icsk = inet_csk(sk);
328 	struct tls_context *ctx = tls_get_ctx(sk);
329 	long timeo = sock_sndtimeo(sk, 0);
330 	bool free_ctx;
331 
332 	if (ctx->tx_conf == TLS_SW)
333 		tls_sw_cancel_work_tx(ctx);
334 
335 	lock_sock(sk);
336 	free_ctx = ctx->tx_conf != TLS_HW && ctx->rx_conf != TLS_HW;
337 
338 	if (ctx->tx_conf != TLS_BASE || ctx->rx_conf != TLS_BASE)
339 		tls_sk_proto_cleanup(sk, ctx, timeo);
340 
341 	write_lock_bh(&sk->sk_callback_lock);
342 	if (free_ctx)
343 		rcu_assign_pointer(icsk->icsk_ulp_data, NULL);
344 	WRITE_ONCE(sk->sk_prot, ctx->sk_proto);
345 	if (sk->sk_write_space == tls_write_space)
346 		sk->sk_write_space = ctx->sk_write_space;
347 	write_unlock_bh(&sk->sk_callback_lock);
348 	release_sock(sk);
349 	if (ctx->tx_conf == TLS_SW)
350 		tls_sw_free_ctx_tx(ctx);
351 	if (ctx->rx_conf == TLS_SW || ctx->rx_conf == TLS_HW)
352 		tls_sw_strparser_done(ctx);
353 	if (ctx->rx_conf == TLS_SW)
354 		tls_sw_free_ctx_rx(ctx);
355 	ctx->sk_proto->close(sk, timeout);
356 
357 	if (free_ctx)
358 		tls_ctx_free(sk, ctx);
359 }
360 
361 static __poll_t tls_sk_poll(struct file *file, struct socket *sock,
362 			    struct poll_table_struct *wait)
363 {
364 	struct tls_sw_context_rx *ctx;
365 	struct tls_context *tls_ctx;
366 	struct sock *sk = sock->sk;
367 	struct sk_psock *psock;
368 	__poll_t mask = 0;
369 	u8 shutdown;
370 	int state;
371 
372 	mask = tcp_poll(file, sock, wait);
373 
374 	state = inet_sk_state_load(sk);
375 	shutdown = READ_ONCE(sk->sk_shutdown);
376 	if (unlikely(state != TCP_ESTABLISHED || shutdown & RCV_SHUTDOWN))
377 		return mask;
378 
379 	tls_ctx = tls_get_ctx(sk);
380 	ctx = tls_sw_ctx_rx(tls_ctx);
381 	psock = sk_psock_get(sk);
382 
383 	if (skb_queue_empty_lockless(&ctx->rx_list) &&
384 	    !tls_strp_msg_ready(ctx) &&
385 	    sk_psock_queue_empty(psock))
386 		mask &= ~(EPOLLIN | EPOLLRDNORM);
387 
388 	if (psock)
389 		sk_psock_put(sk, psock);
390 
391 	return mask;
392 }
393 
394 static int do_tls_getsockopt_conf(struct sock *sk, char __user *optval,
395 				  int __user *optlen, int tx)
396 {
397 	int rc = 0;
398 	struct tls_context *ctx = tls_get_ctx(sk);
399 	struct tls_crypto_info *crypto_info;
400 	struct cipher_context *cctx;
401 	int len;
402 
403 	if (get_user(len, optlen))
404 		return -EFAULT;
405 
406 	if (!optval || (len < sizeof(*crypto_info))) {
407 		rc = -EINVAL;
408 		goto out;
409 	}
410 
411 	if (!ctx) {
412 		rc = -EBUSY;
413 		goto out;
414 	}
415 
416 	/* get user crypto info */
417 	if (tx) {
418 		crypto_info = &ctx->crypto_send.info;
419 		cctx = &ctx->tx;
420 	} else {
421 		crypto_info = &ctx->crypto_recv.info;
422 		cctx = &ctx->rx;
423 	}
424 
425 	if (!TLS_CRYPTO_INFO_READY(crypto_info)) {
426 		rc = -EBUSY;
427 		goto out;
428 	}
429 
430 	if (len == sizeof(*crypto_info)) {
431 		if (copy_to_user(optval, crypto_info, sizeof(*crypto_info)))
432 			rc = -EFAULT;
433 		goto out;
434 	}
435 
436 	switch (crypto_info->cipher_type) {
437 	case TLS_CIPHER_AES_GCM_128: {
438 		struct tls12_crypto_info_aes_gcm_128 *
439 		  crypto_info_aes_gcm_128 =
440 		  container_of(crypto_info,
441 			       struct tls12_crypto_info_aes_gcm_128,
442 			       info);
443 
444 		if (len != sizeof(*crypto_info_aes_gcm_128)) {
445 			rc = -EINVAL;
446 			goto out;
447 		}
448 		memcpy(crypto_info_aes_gcm_128->iv,
449 		       cctx->iv + TLS_CIPHER_AES_GCM_128_SALT_SIZE,
450 		       TLS_CIPHER_AES_GCM_128_IV_SIZE);
451 		memcpy(crypto_info_aes_gcm_128->rec_seq, cctx->rec_seq,
452 		       TLS_CIPHER_AES_GCM_128_REC_SEQ_SIZE);
453 		if (copy_to_user(optval,
454 				 crypto_info_aes_gcm_128,
455 				 sizeof(*crypto_info_aes_gcm_128)))
456 			rc = -EFAULT;
457 		break;
458 	}
459 	case TLS_CIPHER_AES_GCM_256: {
460 		struct tls12_crypto_info_aes_gcm_256 *
461 		  crypto_info_aes_gcm_256 =
462 		  container_of(crypto_info,
463 			       struct tls12_crypto_info_aes_gcm_256,
464 			       info);
465 
466 		if (len != sizeof(*crypto_info_aes_gcm_256)) {
467 			rc = -EINVAL;
468 			goto out;
469 		}
470 		memcpy(crypto_info_aes_gcm_256->iv,
471 		       cctx->iv + TLS_CIPHER_AES_GCM_256_SALT_SIZE,
472 		       TLS_CIPHER_AES_GCM_256_IV_SIZE);
473 		memcpy(crypto_info_aes_gcm_256->rec_seq, cctx->rec_seq,
474 		       TLS_CIPHER_AES_GCM_256_REC_SEQ_SIZE);
475 		if (copy_to_user(optval,
476 				 crypto_info_aes_gcm_256,
477 				 sizeof(*crypto_info_aes_gcm_256)))
478 			rc = -EFAULT;
479 		break;
480 	}
481 	case TLS_CIPHER_AES_CCM_128: {
482 		struct tls12_crypto_info_aes_ccm_128 *aes_ccm_128 =
483 			container_of(crypto_info,
484 				struct tls12_crypto_info_aes_ccm_128, info);
485 
486 		if (len != sizeof(*aes_ccm_128)) {
487 			rc = -EINVAL;
488 			goto out;
489 		}
490 		memcpy(aes_ccm_128->iv,
491 		       cctx->iv + TLS_CIPHER_AES_CCM_128_SALT_SIZE,
492 		       TLS_CIPHER_AES_CCM_128_IV_SIZE);
493 		memcpy(aes_ccm_128->rec_seq, cctx->rec_seq,
494 		       TLS_CIPHER_AES_CCM_128_REC_SEQ_SIZE);
495 		if (copy_to_user(optval, aes_ccm_128, sizeof(*aes_ccm_128)))
496 			rc = -EFAULT;
497 		break;
498 	}
499 	case TLS_CIPHER_CHACHA20_POLY1305: {
500 		struct tls12_crypto_info_chacha20_poly1305 *chacha20_poly1305 =
501 			container_of(crypto_info,
502 				struct tls12_crypto_info_chacha20_poly1305,
503 				info);
504 
505 		if (len != sizeof(*chacha20_poly1305)) {
506 			rc = -EINVAL;
507 			goto out;
508 		}
509 		memcpy(chacha20_poly1305->iv,
510 		       cctx->iv + TLS_CIPHER_CHACHA20_POLY1305_SALT_SIZE,
511 		       TLS_CIPHER_CHACHA20_POLY1305_IV_SIZE);
512 		memcpy(chacha20_poly1305->rec_seq, cctx->rec_seq,
513 		       TLS_CIPHER_CHACHA20_POLY1305_REC_SEQ_SIZE);
514 		if (copy_to_user(optval, chacha20_poly1305,
515 				sizeof(*chacha20_poly1305)))
516 			rc = -EFAULT;
517 		break;
518 	}
519 	case TLS_CIPHER_SM4_GCM: {
520 		struct tls12_crypto_info_sm4_gcm *sm4_gcm_info =
521 			container_of(crypto_info,
522 				struct tls12_crypto_info_sm4_gcm, info);
523 
524 		if (len != sizeof(*sm4_gcm_info)) {
525 			rc = -EINVAL;
526 			goto out;
527 		}
528 		memcpy(sm4_gcm_info->iv,
529 		       cctx->iv + TLS_CIPHER_SM4_GCM_SALT_SIZE,
530 		       TLS_CIPHER_SM4_GCM_IV_SIZE);
531 		memcpy(sm4_gcm_info->rec_seq, cctx->rec_seq,
532 		       TLS_CIPHER_SM4_GCM_REC_SEQ_SIZE);
533 		if (copy_to_user(optval, sm4_gcm_info, sizeof(*sm4_gcm_info)))
534 			rc = -EFAULT;
535 		break;
536 	}
537 	case TLS_CIPHER_SM4_CCM: {
538 		struct tls12_crypto_info_sm4_ccm *sm4_ccm_info =
539 			container_of(crypto_info,
540 				struct tls12_crypto_info_sm4_ccm, info);
541 
542 		if (len != sizeof(*sm4_ccm_info)) {
543 			rc = -EINVAL;
544 			goto out;
545 		}
546 		memcpy(sm4_ccm_info->iv,
547 		       cctx->iv + TLS_CIPHER_SM4_CCM_SALT_SIZE,
548 		       TLS_CIPHER_SM4_CCM_IV_SIZE);
549 		memcpy(sm4_ccm_info->rec_seq, cctx->rec_seq,
550 		       TLS_CIPHER_SM4_CCM_REC_SEQ_SIZE);
551 		if (copy_to_user(optval, sm4_ccm_info, sizeof(*sm4_ccm_info)))
552 			rc = -EFAULT;
553 		break;
554 	}
555 	case TLS_CIPHER_ARIA_GCM_128: {
556 		struct tls12_crypto_info_aria_gcm_128 *
557 		  crypto_info_aria_gcm_128 =
558 		  container_of(crypto_info,
559 			       struct tls12_crypto_info_aria_gcm_128,
560 			       info);
561 
562 		if (len != sizeof(*crypto_info_aria_gcm_128)) {
563 			rc = -EINVAL;
564 			goto out;
565 		}
566 		memcpy(crypto_info_aria_gcm_128->iv,
567 		       cctx->iv + TLS_CIPHER_ARIA_GCM_128_SALT_SIZE,
568 		       TLS_CIPHER_ARIA_GCM_128_IV_SIZE);
569 		memcpy(crypto_info_aria_gcm_128->rec_seq, cctx->rec_seq,
570 		       TLS_CIPHER_ARIA_GCM_128_REC_SEQ_SIZE);
571 		if (copy_to_user(optval,
572 				 crypto_info_aria_gcm_128,
573 				 sizeof(*crypto_info_aria_gcm_128)))
574 			rc = -EFAULT;
575 		break;
576 	}
577 	case TLS_CIPHER_ARIA_GCM_256: {
578 		struct tls12_crypto_info_aria_gcm_256 *
579 		  crypto_info_aria_gcm_256 =
580 		  container_of(crypto_info,
581 			       struct tls12_crypto_info_aria_gcm_256,
582 			       info);
583 
584 		if (len != sizeof(*crypto_info_aria_gcm_256)) {
585 			rc = -EINVAL;
586 			goto out;
587 		}
588 		memcpy(crypto_info_aria_gcm_256->iv,
589 		       cctx->iv + TLS_CIPHER_ARIA_GCM_256_SALT_SIZE,
590 		       TLS_CIPHER_ARIA_GCM_256_IV_SIZE);
591 		memcpy(crypto_info_aria_gcm_256->rec_seq, cctx->rec_seq,
592 		       TLS_CIPHER_ARIA_GCM_256_REC_SEQ_SIZE);
593 		if (copy_to_user(optval,
594 				 crypto_info_aria_gcm_256,
595 				 sizeof(*crypto_info_aria_gcm_256)))
596 			rc = -EFAULT;
597 		break;
598 	}
599 	default:
600 		rc = -EINVAL;
601 	}
602 
603 out:
604 	return rc;
605 }
606 
607 static int do_tls_getsockopt_tx_zc(struct sock *sk, char __user *optval,
608 				   int __user *optlen)
609 {
610 	struct tls_context *ctx = tls_get_ctx(sk);
611 	unsigned int value;
612 	int len;
613 
614 	if (get_user(len, optlen))
615 		return -EFAULT;
616 
617 	if (len != sizeof(value))
618 		return -EINVAL;
619 
620 	value = ctx->zerocopy_sendfile;
621 	if (copy_to_user(optval, &value, sizeof(value)))
622 		return -EFAULT;
623 
624 	return 0;
625 }
626 
627 static int do_tls_getsockopt_no_pad(struct sock *sk, char __user *optval,
628 				    int __user *optlen)
629 {
630 	struct tls_context *ctx = tls_get_ctx(sk);
631 	int value, len;
632 
633 	if (ctx->prot_info.version != TLS_1_3_VERSION)
634 		return -EINVAL;
635 
636 	if (get_user(len, optlen))
637 		return -EFAULT;
638 	if (len < sizeof(value))
639 		return -EINVAL;
640 
641 	value = -EINVAL;
642 	if (ctx->rx_conf == TLS_SW || ctx->rx_conf == TLS_HW)
643 		value = ctx->rx_no_pad;
644 	if (value < 0)
645 		return value;
646 
647 	if (put_user(sizeof(value), optlen))
648 		return -EFAULT;
649 	if (copy_to_user(optval, &value, sizeof(value)))
650 		return -EFAULT;
651 
652 	return 0;
653 }
654 
655 static int do_tls_getsockopt(struct sock *sk, int optname,
656 			     char __user *optval, int __user *optlen)
657 {
658 	int rc = 0;
659 
660 	lock_sock(sk);
661 
662 	switch (optname) {
663 	case TLS_TX:
664 	case TLS_RX:
665 		rc = do_tls_getsockopt_conf(sk, optval, optlen,
666 					    optname == TLS_TX);
667 		break;
668 	case TLS_TX_ZEROCOPY_RO:
669 		rc = do_tls_getsockopt_tx_zc(sk, optval, optlen);
670 		break;
671 	case TLS_RX_EXPECT_NO_PAD:
672 		rc = do_tls_getsockopt_no_pad(sk, optval, optlen);
673 		break;
674 	default:
675 		rc = -ENOPROTOOPT;
676 		break;
677 	}
678 
679 	release_sock(sk);
680 
681 	return rc;
682 }
683 
684 static int tls_getsockopt(struct sock *sk, int level, int optname,
685 			  char __user *optval, int __user *optlen)
686 {
687 	struct tls_context *ctx = tls_get_ctx(sk);
688 
689 	if (level != SOL_TLS)
690 		return ctx->sk_proto->getsockopt(sk, level,
691 						 optname, optval, optlen);
692 
693 	return do_tls_getsockopt(sk, optname, optval, optlen);
694 }
695 
696 static int do_tls_setsockopt_conf(struct sock *sk, sockptr_t optval,
697 				  unsigned int optlen, int tx)
698 {
699 	struct tls_crypto_info *crypto_info;
700 	struct tls_crypto_info *alt_crypto_info;
701 	struct tls_context *ctx = tls_get_ctx(sk);
702 	size_t optsize;
703 	int rc = 0;
704 	int conf;
705 
706 	if (sockptr_is_null(optval) || (optlen < sizeof(*crypto_info)))
707 		return -EINVAL;
708 
709 	if (tx) {
710 		crypto_info = &ctx->crypto_send.info;
711 		alt_crypto_info = &ctx->crypto_recv.info;
712 	} else {
713 		crypto_info = &ctx->crypto_recv.info;
714 		alt_crypto_info = &ctx->crypto_send.info;
715 	}
716 
717 	/* Currently we don't support set crypto info more than one time */
718 	if (TLS_CRYPTO_INFO_READY(crypto_info))
719 		return -EBUSY;
720 
721 	rc = copy_from_sockptr(crypto_info, optval, sizeof(*crypto_info));
722 	if (rc) {
723 		rc = -EFAULT;
724 		goto err_crypto_info;
725 	}
726 
727 	/* check version */
728 	if (crypto_info->version != TLS_1_2_VERSION &&
729 	    crypto_info->version != TLS_1_3_VERSION) {
730 		rc = -EINVAL;
731 		goto err_crypto_info;
732 	}
733 
734 	/* Ensure that TLS version and ciphers are same in both directions */
735 	if (TLS_CRYPTO_INFO_READY(alt_crypto_info)) {
736 		if (alt_crypto_info->version != crypto_info->version ||
737 		    alt_crypto_info->cipher_type != crypto_info->cipher_type) {
738 			rc = -EINVAL;
739 			goto err_crypto_info;
740 		}
741 	}
742 
743 	switch (crypto_info->cipher_type) {
744 	case TLS_CIPHER_AES_GCM_128:
745 		optsize = sizeof(struct tls12_crypto_info_aes_gcm_128);
746 		break;
747 	case TLS_CIPHER_AES_GCM_256: {
748 		optsize = sizeof(struct tls12_crypto_info_aes_gcm_256);
749 		break;
750 	}
751 	case TLS_CIPHER_AES_CCM_128:
752 		optsize = sizeof(struct tls12_crypto_info_aes_ccm_128);
753 		break;
754 	case TLS_CIPHER_CHACHA20_POLY1305:
755 		optsize = sizeof(struct tls12_crypto_info_chacha20_poly1305);
756 		break;
757 	case TLS_CIPHER_SM4_GCM:
758 		optsize = sizeof(struct tls12_crypto_info_sm4_gcm);
759 		break;
760 	case TLS_CIPHER_SM4_CCM:
761 		optsize = sizeof(struct tls12_crypto_info_sm4_ccm);
762 		break;
763 	case TLS_CIPHER_ARIA_GCM_128:
764 		if (crypto_info->version != TLS_1_2_VERSION) {
765 			rc = -EINVAL;
766 			goto err_crypto_info;
767 		}
768 		optsize = sizeof(struct tls12_crypto_info_aria_gcm_128);
769 		break;
770 	case TLS_CIPHER_ARIA_GCM_256:
771 		if (crypto_info->version != TLS_1_2_VERSION) {
772 			rc = -EINVAL;
773 			goto err_crypto_info;
774 		}
775 		optsize = sizeof(struct tls12_crypto_info_aria_gcm_256);
776 		break;
777 	default:
778 		rc = -EINVAL;
779 		goto err_crypto_info;
780 	}
781 
782 	if (optlen != optsize) {
783 		rc = -EINVAL;
784 		goto err_crypto_info;
785 	}
786 
787 	rc = copy_from_sockptr_offset(crypto_info + 1, optval,
788 				      sizeof(*crypto_info),
789 				      optlen - sizeof(*crypto_info));
790 	if (rc) {
791 		rc = -EFAULT;
792 		goto err_crypto_info;
793 	}
794 
795 	if (tx) {
796 		rc = tls_set_device_offload(sk, ctx);
797 		conf = TLS_HW;
798 		if (!rc) {
799 			TLS_INC_STATS(sock_net(sk), LINUX_MIB_TLSTXDEVICE);
800 			TLS_INC_STATS(sock_net(sk), LINUX_MIB_TLSCURRTXDEVICE);
801 		} else {
802 			rc = tls_set_sw_offload(sk, ctx, 1);
803 			if (rc)
804 				goto err_crypto_info;
805 			TLS_INC_STATS(sock_net(sk), LINUX_MIB_TLSTXSW);
806 			TLS_INC_STATS(sock_net(sk), LINUX_MIB_TLSCURRTXSW);
807 			conf = TLS_SW;
808 		}
809 	} else {
810 		rc = tls_set_device_offload_rx(sk, ctx);
811 		conf = TLS_HW;
812 		if (!rc) {
813 			TLS_INC_STATS(sock_net(sk), LINUX_MIB_TLSRXDEVICE);
814 			TLS_INC_STATS(sock_net(sk), LINUX_MIB_TLSCURRRXDEVICE);
815 		} else {
816 			rc = tls_set_sw_offload(sk, ctx, 0);
817 			if (rc)
818 				goto err_crypto_info;
819 			TLS_INC_STATS(sock_net(sk), LINUX_MIB_TLSRXSW);
820 			TLS_INC_STATS(sock_net(sk), LINUX_MIB_TLSCURRRXSW);
821 			conf = TLS_SW;
822 		}
823 		tls_sw_strparser_arm(sk, ctx);
824 	}
825 
826 	if (tx)
827 		ctx->tx_conf = conf;
828 	else
829 		ctx->rx_conf = conf;
830 	update_sk_prot(sk, ctx);
831 	if (tx) {
832 		ctx->sk_write_space = sk->sk_write_space;
833 		sk->sk_write_space = tls_write_space;
834 	} else {
835 		struct tls_sw_context_rx *rx_ctx = tls_sw_ctx_rx(ctx);
836 
837 		tls_strp_check_rcv(&rx_ctx->strp);
838 	}
839 	return 0;
840 
841 err_crypto_info:
842 	memzero_explicit(crypto_info, sizeof(union tls_crypto_context));
843 	return rc;
844 }
845 
846 static int do_tls_setsockopt_tx_zc(struct sock *sk, sockptr_t optval,
847 				   unsigned int optlen)
848 {
849 	struct tls_context *ctx = tls_get_ctx(sk);
850 	unsigned int value;
851 
852 	if (sockptr_is_null(optval) || optlen != sizeof(value))
853 		return -EINVAL;
854 
855 	if (copy_from_sockptr(&value, optval, sizeof(value)))
856 		return -EFAULT;
857 
858 	if (value > 1)
859 		return -EINVAL;
860 
861 	ctx->zerocopy_sendfile = value;
862 
863 	return 0;
864 }
865 
866 static int do_tls_setsockopt_no_pad(struct sock *sk, sockptr_t optval,
867 				    unsigned int optlen)
868 {
869 	struct tls_context *ctx = tls_get_ctx(sk);
870 	u32 val;
871 	int rc;
872 
873 	if (ctx->prot_info.version != TLS_1_3_VERSION ||
874 	    sockptr_is_null(optval) || optlen < sizeof(val))
875 		return -EINVAL;
876 
877 	rc = copy_from_sockptr(&val, optval, sizeof(val));
878 	if (rc)
879 		return -EFAULT;
880 	if (val > 1)
881 		return -EINVAL;
882 	rc = check_zeroed_sockptr(optval, sizeof(val), optlen - sizeof(val));
883 	if (rc < 1)
884 		return rc == 0 ? -EINVAL : rc;
885 
886 	lock_sock(sk);
887 	rc = -EINVAL;
888 	if (ctx->rx_conf == TLS_SW || ctx->rx_conf == TLS_HW) {
889 		ctx->rx_no_pad = val;
890 		tls_update_rx_zc_capable(ctx);
891 		rc = 0;
892 	}
893 	release_sock(sk);
894 
895 	return rc;
896 }
897 
898 static int do_tls_setsockopt(struct sock *sk, int optname, sockptr_t optval,
899 			     unsigned int optlen)
900 {
901 	int rc = 0;
902 
903 	switch (optname) {
904 	case TLS_TX:
905 	case TLS_RX:
906 		lock_sock(sk);
907 		rc = do_tls_setsockopt_conf(sk, optval, optlen,
908 					    optname == TLS_TX);
909 		release_sock(sk);
910 		break;
911 	case TLS_TX_ZEROCOPY_RO:
912 		lock_sock(sk);
913 		rc = do_tls_setsockopt_tx_zc(sk, optval, optlen);
914 		release_sock(sk);
915 		break;
916 	case TLS_RX_EXPECT_NO_PAD:
917 		rc = do_tls_setsockopt_no_pad(sk, optval, optlen);
918 		break;
919 	default:
920 		rc = -ENOPROTOOPT;
921 		break;
922 	}
923 	return rc;
924 }
925 
926 static int tls_setsockopt(struct sock *sk, int level, int optname,
927 			  sockptr_t optval, unsigned int optlen)
928 {
929 	struct tls_context *ctx = tls_get_ctx(sk);
930 
931 	if (level != SOL_TLS)
932 		return ctx->sk_proto->setsockopt(sk, level, optname, optval,
933 						 optlen);
934 
935 	return do_tls_setsockopt(sk, optname, optval, optlen);
936 }
937 
938 struct tls_context *tls_ctx_create(struct sock *sk)
939 {
940 	struct inet_connection_sock *icsk = inet_csk(sk);
941 	struct tls_context *ctx;
942 
943 	ctx = kzalloc(sizeof(*ctx), GFP_ATOMIC);
944 	if (!ctx)
945 		return NULL;
946 
947 	mutex_init(&ctx->tx_lock);
948 	rcu_assign_pointer(icsk->icsk_ulp_data, ctx);
949 	ctx->sk_proto = READ_ONCE(sk->sk_prot);
950 	ctx->sk = sk;
951 	return ctx;
952 }
953 
954 static void build_proto_ops(struct proto_ops ops[TLS_NUM_CONFIG][TLS_NUM_CONFIG],
955 			    const struct proto_ops *base)
956 {
957 	ops[TLS_BASE][TLS_BASE] = *base;
958 
959 	ops[TLS_SW  ][TLS_BASE] = ops[TLS_BASE][TLS_BASE];
960 	ops[TLS_SW  ][TLS_BASE].splice_eof	= tls_sw_splice_eof;
961 
962 	ops[TLS_BASE][TLS_SW  ] = ops[TLS_BASE][TLS_BASE];
963 	ops[TLS_BASE][TLS_SW  ].splice_read	= tls_sw_splice_read;
964 	ops[TLS_BASE][TLS_SW  ].poll		= tls_sk_poll;
965 
966 	ops[TLS_SW  ][TLS_SW  ] = ops[TLS_SW  ][TLS_BASE];
967 	ops[TLS_SW  ][TLS_SW  ].splice_read	= tls_sw_splice_read;
968 	ops[TLS_SW  ][TLS_SW  ].poll		= tls_sk_poll;
969 
970 #ifdef CONFIG_TLS_DEVICE
971 	ops[TLS_HW  ][TLS_BASE] = ops[TLS_BASE][TLS_BASE];
972 
973 	ops[TLS_HW  ][TLS_SW  ] = ops[TLS_BASE][TLS_SW  ];
974 
975 	ops[TLS_BASE][TLS_HW  ] = ops[TLS_BASE][TLS_SW  ];
976 
977 	ops[TLS_SW  ][TLS_HW  ] = ops[TLS_SW  ][TLS_SW  ];
978 
979 	ops[TLS_HW  ][TLS_HW  ] = ops[TLS_HW  ][TLS_SW  ];
980 #endif
981 #ifdef CONFIG_TLS_TOE
982 	ops[TLS_HW_RECORD][TLS_HW_RECORD] = *base;
983 #endif
984 }
985 
986 static void tls_build_proto(struct sock *sk)
987 {
988 	int ip_ver = sk->sk_family == AF_INET6 ? TLSV6 : TLSV4;
989 	struct proto *prot = READ_ONCE(sk->sk_prot);
990 
991 	/* Build IPv6 TLS whenever the address of tcpv6 _prot changes */
992 	if (ip_ver == TLSV6 &&
993 	    unlikely(prot != smp_load_acquire(&saved_tcpv6_prot))) {
994 		mutex_lock(&tcpv6_prot_mutex);
995 		if (likely(prot != saved_tcpv6_prot)) {
996 			build_protos(tls_prots[TLSV6], prot);
997 			build_proto_ops(tls_proto_ops[TLSV6],
998 					sk->sk_socket->ops);
999 			smp_store_release(&saved_tcpv6_prot, prot);
1000 		}
1001 		mutex_unlock(&tcpv6_prot_mutex);
1002 	}
1003 
1004 	if (ip_ver == TLSV4 &&
1005 	    unlikely(prot != smp_load_acquire(&saved_tcpv4_prot))) {
1006 		mutex_lock(&tcpv4_prot_mutex);
1007 		if (likely(prot != saved_tcpv4_prot)) {
1008 			build_protos(tls_prots[TLSV4], prot);
1009 			build_proto_ops(tls_proto_ops[TLSV4],
1010 					sk->sk_socket->ops);
1011 			smp_store_release(&saved_tcpv4_prot, prot);
1012 		}
1013 		mutex_unlock(&tcpv4_prot_mutex);
1014 	}
1015 }
1016 
1017 static void build_protos(struct proto prot[TLS_NUM_CONFIG][TLS_NUM_CONFIG],
1018 			 const struct proto *base)
1019 {
1020 	prot[TLS_BASE][TLS_BASE] = *base;
1021 	prot[TLS_BASE][TLS_BASE].setsockopt	= tls_setsockopt;
1022 	prot[TLS_BASE][TLS_BASE].getsockopt	= tls_getsockopt;
1023 	prot[TLS_BASE][TLS_BASE].close		= tls_sk_proto_close;
1024 
1025 	prot[TLS_SW][TLS_BASE] = prot[TLS_BASE][TLS_BASE];
1026 	prot[TLS_SW][TLS_BASE].sendmsg		= tls_sw_sendmsg;
1027 	prot[TLS_SW][TLS_BASE].splice_eof	= tls_sw_splice_eof;
1028 
1029 	prot[TLS_BASE][TLS_SW] = prot[TLS_BASE][TLS_BASE];
1030 	prot[TLS_BASE][TLS_SW].recvmsg		  = tls_sw_recvmsg;
1031 	prot[TLS_BASE][TLS_SW].sock_is_readable   = tls_sw_sock_is_readable;
1032 	prot[TLS_BASE][TLS_SW].close		  = tls_sk_proto_close;
1033 
1034 	prot[TLS_SW][TLS_SW] = prot[TLS_SW][TLS_BASE];
1035 	prot[TLS_SW][TLS_SW].recvmsg		= tls_sw_recvmsg;
1036 	prot[TLS_SW][TLS_SW].sock_is_readable   = tls_sw_sock_is_readable;
1037 	prot[TLS_SW][TLS_SW].close		= tls_sk_proto_close;
1038 
1039 #ifdef CONFIG_TLS_DEVICE
1040 	prot[TLS_HW][TLS_BASE] = prot[TLS_BASE][TLS_BASE];
1041 	prot[TLS_HW][TLS_BASE].sendmsg		= tls_device_sendmsg;
1042 	prot[TLS_HW][TLS_BASE].splice_eof	= tls_device_splice_eof;
1043 
1044 	prot[TLS_HW][TLS_SW] = prot[TLS_BASE][TLS_SW];
1045 	prot[TLS_HW][TLS_SW].sendmsg		= tls_device_sendmsg;
1046 	prot[TLS_HW][TLS_SW].splice_eof		= tls_device_splice_eof;
1047 
1048 	prot[TLS_BASE][TLS_HW] = prot[TLS_BASE][TLS_SW];
1049 
1050 	prot[TLS_SW][TLS_HW] = prot[TLS_SW][TLS_SW];
1051 
1052 	prot[TLS_HW][TLS_HW] = prot[TLS_HW][TLS_SW];
1053 #endif
1054 #ifdef CONFIG_TLS_TOE
1055 	prot[TLS_HW_RECORD][TLS_HW_RECORD] = *base;
1056 	prot[TLS_HW_RECORD][TLS_HW_RECORD].hash		= tls_toe_hash;
1057 	prot[TLS_HW_RECORD][TLS_HW_RECORD].unhash	= tls_toe_unhash;
1058 #endif
1059 }
1060 
1061 static int tls_init(struct sock *sk)
1062 {
1063 	struct tls_context *ctx;
1064 	int rc = 0;
1065 
1066 	tls_build_proto(sk);
1067 
1068 #ifdef CONFIG_TLS_TOE
1069 	if (tls_toe_bypass(sk))
1070 		return 0;
1071 #endif
1072 
1073 	/* The TLS ulp is currently supported only for TCP sockets
1074 	 * in ESTABLISHED state.
1075 	 * Supporting sockets in LISTEN state will require us
1076 	 * to modify the accept implementation to clone rather then
1077 	 * share the ulp context.
1078 	 */
1079 	if (sk->sk_state != TCP_ESTABLISHED)
1080 		return -ENOTCONN;
1081 
1082 	/* allocate tls context */
1083 	write_lock_bh(&sk->sk_callback_lock);
1084 	ctx = tls_ctx_create(sk);
1085 	if (!ctx) {
1086 		rc = -ENOMEM;
1087 		goto out;
1088 	}
1089 
1090 	ctx->tx_conf = TLS_BASE;
1091 	ctx->rx_conf = TLS_BASE;
1092 	update_sk_prot(sk, ctx);
1093 out:
1094 	write_unlock_bh(&sk->sk_callback_lock);
1095 	return rc;
1096 }
1097 
1098 static void tls_update(struct sock *sk, struct proto *p,
1099 		       void (*write_space)(struct sock *sk))
1100 {
1101 	struct tls_context *ctx;
1102 
1103 	WARN_ON_ONCE(sk->sk_prot == p);
1104 
1105 	ctx = tls_get_ctx(sk);
1106 	if (likely(ctx)) {
1107 		ctx->sk_write_space = write_space;
1108 		ctx->sk_proto = p;
1109 	} else {
1110 		/* Pairs with lockless read in sk_clone_lock(). */
1111 		WRITE_ONCE(sk->sk_prot, p);
1112 		sk->sk_write_space = write_space;
1113 	}
1114 }
1115 
1116 static u16 tls_user_config(struct tls_context *ctx, bool tx)
1117 {
1118 	u16 config = tx ? ctx->tx_conf : ctx->rx_conf;
1119 
1120 	switch (config) {
1121 	case TLS_BASE:
1122 		return TLS_CONF_BASE;
1123 	case TLS_SW:
1124 		return TLS_CONF_SW;
1125 	case TLS_HW:
1126 		return TLS_CONF_HW;
1127 	case TLS_HW_RECORD:
1128 		return TLS_CONF_HW_RECORD;
1129 	}
1130 	return 0;
1131 }
1132 
1133 static int tls_get_info(const struct sock *sk, struct sk_buff *skb)
1134 {
1135 	u16 version, cipher_type;
1136 	struct tls_context *ctx;
1137 	struct nlattr *start;
1138 	int err;
1139 
1140 	start = nla_nest_start_noflag(skb, INET_ULP_INFO_TLS);
1141 	if (!start)
1142 		return -EMSGSIZE;
1143 
1144 	rcu_read_lock();
1145 	ctx = rcu_dereference(inet_csk(sk)->icsk_ulp_data);
1146 	if (!ctx) {
1147 		err = 0;
1148 		goto nla_failure;
1149 	}
1150 	version = ctx->prot_info.version;
1151 	if (version) {
1152 		err = nla_put_u16(skb, TLS_INFO_VERSION, version);
1153 		if (err)
1154 			goto nla_failure;
1155 	}
1156 	cipher_type = ctx->prot_info.cipher_type;
1157 	if (cipher_type) {
1158 		err = nla_put_u16(skb, TLS_INFO_CIPHER, cipher_type);
1159 		if (err)
1160 			goto nla_failure;
1161 	}
1162 	err = nla_put_u16(skb, TLS_INFO_TXCONF, tls_user_config(ctx, true));
1163 	if (err)
1164 		goto nla_failure;
1165 
1166 	err = nla_put_u16(skb, TLS_INFO_RXCONF, tls_user_config(ctx, false));
1167 	if (err)
1168 		goto nla_failure;
1169 
1170 	if (ctx->tx_conf == TLS_HW && ctx->zerocopy_sendfile) {
1171 		err = nla_put_flag(skb, TLS_INFO_ZC_RO_TX);
1172 		if (err)
1173 			goto nla_failure;
1174 	}
1175 	if (ctx->rx_no_pad) {
1176 		err = nla_put_flag(skb, TLS_INFO_RX_NO_PAD);
1177 		if (err)
1178 			goto nla_failure;
1179 	}
1180 
1181 	rcu_read_unlock();
1182 	nla_nest_end(skb, start);
1183 	return 0;
1184 
1185 nla_failure:
1186 	rcu_read_unlock();
1187 	nla_nest_cancel(skb, start);
1188 	return err;
1189 }
1190 
1191 static size_t tls_get_info_size(const struct sock *sk)
1192 {
1193 	size_t size = 0;
1194 
1195 	size += nla_total_size(0) +		/* INET_ULP_INFO_TLS */
1196 		nla_total_size(sizeof(u16)) +	/* TLS_INFO_VERSION */
1197 		nla_total_size(sizeof(u16)) +	/* TLS_INFO_CIPHER */
1198 		nla_total_size(sizeof(u16)) +	/* TLS_INFO_RXCONF */
1199 		nla_total_size(sizeof(u16)) +	/* TLS_INFO_TXCONF */
1200 		nla_total_size(0) +		/* TLS_INFO_ZC_RO_TX */
1201 		nla_total_size(0) +		/* TLS_INFO_RX_NO_PAD */
1202 		0;
1203 
1204 	return size;
1205 }
1206 
1207 static int __net_init tls_init_net(struct net *net)
1208 {
1209 	int err;
1210 
1211 	net->mib.tls_statistics = alloc_percpu(struct linux_tls_mib);
1212 	if (!net->mib.tls_statistics)
1213 		return -ENOMEM;
1214 
1215 	err = tls_proc_init(net);
1216 	if (err)
1217 		goto err_free_stats;
1218 
1219 	return 0;
1220 err_free_stats:
1221 	free_percpu(net->mib.tls_statistics);
1222 	return err;
1223 }
1224 
1225 static void __net_exit tls_exit_net(struct net *net)
1226 {
1227 	tls_proc_fini(net);
1228 	free_percpu(net->mib.tls_statistics);
1229 }
1230 
1231 static struct pernet_operations tls_proc_ops = {
1232 	.init = tls_init_net,
1233 	.exit = tls_exit_net,
1234 };
1235 
1236 static struct tcp_ulp_ops tcp_tls_ulp_ops __read_mostly = {
1237 	.name			= "tls",
1238 	.owner			= THIS_MODULE,
1239 	.init			= tls_init,
1240 	.update			= tls_update,
1241 	.get_info		= tls_get_info,
1242 	.get_info_size		= tls_get_info_size,
1243 };
1244 
1245 static int __init tls_register(void)
1246 {
1247 	int err;
1248 
1249 	err = register_pernet_subsys(&tls_proc_ops);
1250 	if (err)
1251 		return err;
1252 
1253 	err = tls_strp_dev_init();
1254 	if (err)
1255 		goto err_pernet;
1256 
1257 	err = tls_device_init();
1258 	if (err)
1259 		goto err_strp;
1260 
1261 	tcp_register_ulp(&tcp_tls_ulp_ops);
1262 
1263 	return 0;
1264 err_strp:
1265 	tls_strp_dev_exit();
1266 err_pernet:
1267 	unregister_pernet_subsys(&tls_proc_ops);
1268 	return err;
1269 }
1270 
1271 static void __exit tls_unregister(void)
1272 {
1273 	tcp_unregister_ulp(&tcp_tls_ulp_ops);
1274 	tls_strp_dev_exit();
1275 	tls_device_cleanup();
1276 	unregister_pernet_subsys(&tls_proc_ops);
1277 }
1278 
1279 module_init(tls_register);
1280 module_exit(tls_unregister);
1281