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