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