xref: /openbmc/linux/net/tls/tls_main.c (revision 08700dab)
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/tls.h>
45 #include <net/tls_toe.h>
46 
47 MODULE_AUTHOR("Mellanox Technologies");
48 MODULE_DESCRIPTION("Transport Layer Security Support");
49 MODULE_LICENSE("Dual BSD/GPL");
50 MODULE_ALIAS_TCP_ULP("tls");
51 
52 enum {
53 	TLSV4,
54 	TLSV6,
55 	TLS_NUM_PROTS,
56 };
57 
58 static struct proto *saved_tcpv6_prot;
59 static DEFINE_MUTEX(tcpv6_prot_mutex);
60 static struct proto *saved_tcpv4_prot;
61 static DEFINE_MUTEX(tcpv4_prot_mutex);
62 static struct proto tls_prots[TLS_NUM_PROTS][TLS_NUM_CONFIG][TLS_NUM_CONFIG];
63 static struct proto_ops tls_sw_proto_ops;
64 static void build_protos(struct proto prot[TLS_NUM_CONFIG][TLS_NUM_CONFIG],
65 			 struct proto *base);
66 
67 void update_sk_prot(struct sock *sk, struct tls_context *ctx)
68 {
69 	int ip_ver = sk->sk_family == AF_INET6 ? TLSV6 : TLSV4;
70 
71 	sk->sk_prot = &tls_prots[ip_ver][ctx->tx_conf][ctx->rx_conf];
72 }
73 
74 int wait_on_pending_writer(struct sock *sk, long *timeo)
75 {
76 	int rc = 0;
77 	DEFINE_WAIT_FUNC(wait, woken_wake_function);
78 
79 	add_wait_queue(sk_sleep(sk), &wait);
80 	while (1) {
81 		if (!*timeo) {
82 			rc = -EAGAIN;
83 			break;
84 		}
85 
86 		if (signal_pending(current)) {
87 			rc = sock_intr_errno(*timeo);
88 			break;
89 		}
90 
91 		if (sk_wait_event(sk, timeo, !sk->sk_write_pending, &wait))
92 			break;
93 	}
94 	remove_wait_queue(sk_sleep(sk), &wait);
95 	return rc;
96 }
97 
98 int tls_push_sg(struct sock *sk,
99 		struct tls_context *ctx,
100 		struct scatterlist *sg,
101 		u16 first_offset,
102 		int flags)
103 {
104 	int sendpage_flags = flags | MSG_SENDPAGE_NOTLAST;
105 	int ret = 0;
106 	struct page *p;
107 	size_t size;
108 	int offset = first_offset;
109 
110 	size = sg->length - offset;
111 	offset += sg->offset;
112 
113 	ctx->in_tcp_sendpages = true;
114 	while (1) {
115 		if (sg_is_last(sg))
116 			sendpage_flags = flags;
117 
118 		/* is sending application-limited? */
119 		tcp_rate_check_app_limited(sk);
120 		p = sg_page(sg);
121 retry:
122 		ret = do_tcp_sendpages(sk, p, offset, size, sendpage_flags);
123 
124 		if (ret != size) {
125 			if (ret > 0) {
126 				offset += ret;
127 				size -= ret;
128 				goto retry;
129 			}
130 
131 			offset -= sg->offset;
132 			ctx->partially_sent_offset = offset;
133 			ctx->partially_sent_record = (void *)sg;
134 			ctx->in_tcp_sendpages = false;
135 			return ret;
136 		}
137 
138 		put_page(p);
139 		sk_mem_uncharge(sk, sg->length);
140 		sg = sg_next(sg);
141 		if (!sg)
142 			break;
143 
144 		offset = sg->offset;
145 		size = sg->length;
146 	}
147 
148 	ctx->in_tcp_sendpages = false;
149 
150 	return 0;
151 }
152 
153 static int tls_handle_open_record(struct sock *sk, int flags)
154 {
155 	struct tls_context *ctx = tls_get_ctx(sk);
156 
157 	if (tls_is_pending_open_record(ctx))
158 		return ctx->push_pending_record(sk, flags);
159 
160 	return 0;
161 }
162 
163 int tls_proccess_cmsg(struct sock *sk, struct msghdr *msg,
164 		      unsigned char *record_type)
165 {
166 	struct cmsghdr *cmsg;
167 	int rc = -EINVAL;
168 
169 	for_each_cmsghdr(cmsg, msg) {
170 		if (!CMSG_OK(msg, cmsg))
171 			return -EINVAL;
172 		if (cmsg->cmsg_level != SOL_TLS)
173 			continue;
174 
175 		switch (cmsg->cmsg_type) {
176 		case TLS_SET_RECORD_TYPE:
177 			if (cmsg->cmsg_len < CMSG_LEN(sizeof(*record_type)))
178 				return -EINVAL;
179 
180 			if (msg->msg_flags & MSG_MORE)
181 				return -EINVAL;
182 
183 			rc = tls_handle_open_record(sk, msg->msg_flags);
184 			if (rc)
185 				return rc;
186 
187 			*record_type = *(unsigned char *)CMSG_DATA(cmsg);
188 			rc = 0;
189 			break;
190 		default:
191 			return -EINVAL;
192 		}
193 	}
194 
195 	return rc;
196 }
197 
198 int tls_push_partial_record(struct sock *sk, struct tls_context *ctx,
199 			    int flags)
200 {
201 	struct scatterlist *sg;
202 	u16 offset;
203 
204 	sg = ctx->partially_sent_record;
205 	offset = ctx->partially_sent_offset;
206 
207 	ctx->partially_sent_record = NULL;
208 	return tls_push_sg(sk, ctx, sg, offset, flags);
209 }
210 
211 bool tls_free_partial_record(struct sock *sk, struct tls_context *ctx)
212 {
213 	struct scatterlist *sg;
214 
215 	sg = ctx->partially_sent_record;
216 	if (!sg)
217 		return false;
218 
219 	while (1) {
220 		put_page(sg_page(sg));
221 		sk_mem_uncharge(sk, sg->length);
222 
223 		if (sg_is_last(sg))
224 			break;
225 		sg++;
226 	}
227 	ctx->partially_sent_record = NULL;
228 	return true;
229 }
230 
231 static void tls_write_space(struct sock *sk)
232 {
233 	struct tls_context *ctx = tls_get_ctx(sk);
234 
235 	/* If in_tcp_sendpages call lower protocol write space handler
236 	 * to ensure we wake up any waiting operations there. For example
237 	 * if do_tcp_sendpages where to call sk_wait_event.
238 	 */
239 	if (ctx->in_tcp_sendpages) {
240 		ctx->sk_write_space(sk);
241 		return;
242 	}
243 
244 #ifdef CONFIG_TLS_DEVICE
245 	if (ctx->tx_conf == TLS_HW)
246 		tls_device_write_space(sk, ctx);
247 	else
248 #endif
249 		tls_sw_write_space(sk, ctx);
250 
251 	ctx->sk_write_space(sk);
252 }
253 
254 /**
255  * tls_ctx_free() - free TLS ULP context
256  * @sk:  socket to with @ctx is attached
257  * @ctx: TLS context structure
258  *
259  * Free TLS context. If @sk is %NULL caller guarantees that the socket
260  * to which @ctx was attached has no outstanding references.
261  */
262 void tls_ctx_free(struct sock *sk, struct tls_context *ctx)
263 {
264 	if (!ctx)
265 		return;
266 
267 	memzero_explicit(&ctx->crypto_send, sizeof(ctx->crypto_send));
268 	memzero_explicit(&ctx->crypto_recv, sizeof(ctx->crypto_recv));
269 
270 	if (sk)
271 		kfree_rcu(ctx, rcu);
272 	else
273 		kfree(ctx);
274 }
275 
276 static void tls_sk_proto_cleanup(struct sock *sk,
277 				 struct tls_context *ctx, long timeo)
278 {
279 	if (unlikely(sk->sk_write_pending) &&
280 	    !wait_on_pending_writer(sk, &timeo))
281 		tls_handle_open_record(sk, 0);
282 
283 	/* We need these for tls_sw_fallback handling of other packets */
284 	if (ctx->tx_conf == TLS_SW) {
285 		kfree(ctx->tx.rec_seq);
286 		kfree(ctx->tx.iv);
287 		tls_sw_release_resources_tx(sk);
288 	} else if (ctx->tx_conf == TLS_HW) {
289 		tls_device_free_resources_tx(sk);
290 	}
291 
292 	if (ctx->rx_conf == TLS_SW)
293 		tls_sw_release_resources_rx(sk);
294 	else if (ctx->rx_conf == TLS_HW)
295 		tls_device_offload_cleanup_rx(sk);
296 }
297 
298 static void tls_sk_proto_close(struct sock *sk, long timeout)
299 {
300 	struct inet_connection_sock *icsk = inet_csk(sk);
301 	struct tls_context *ctx = tls_get_ctx(sk);
302 	long timeo = sock_sndtimeo(sk, 0);
303 	bool free_ctx;
304 
305 	if (ctx->tx_conf == TLS_SW)
306 		tls_sw_cancel_work_tx(ctx);
307 
308 	lock_sock(sk);
309 	free_ctx = ctx->tx_conf != TLS_HW && ctx->rx_conf != TLS_HW;
310 
311 	if (ctx->tx_conf != TLS_BASE || ctx->rx_conf != TLS_BASE)
312 		tls_sk_proto_cleanup(sk, ctx, timeo);
313 
314 	write_lock_bh(&sk->sk_callback_lock);
315 	if (free_ctx)
316 		rcu_assign_pointer(icsk->icsk_ulp_data, NULL);
317 	sk->sk_prot = ctx->sk_proto;
318 	if (sk->sk_write_space == tls_write_space)
319 		sk->sk_write_space = ctx->sk_write_space;
320 	write_unlock_bh(&sk->sk_callback_lock);
321 	release_sock(sk);
322 	if (ctx->tx_conf == TLS_SW)
323 		tls_sw_free_ctx_tx(ctx);
324 	if (ctx->rx_conf == TLS_SW || ctx->rx_conf == TLS_HW)
325 		tls_sw_strparser_done(ctx);
326 	if (ctx->rx_conf == TLS_SW)
327 		tls_sw_free_ctx_rx(ctx);
328 	ctx->sk_proto->close(sk, timeout);
329 
330 	if (free_ctx)
331 		tls_ctx_free(sk, ctx);
332 }
333 
334 static int do_tls_getsockopt_tx(struct sock *sk, char __user *optval,
335 				int __user *optlen)
336 {
337 	int rc = 0;
338 	struct tls_context *ctx = tls_get_ctx(sk);
339 	struct tls_crypto_info *crypto_info;
340 	int len;
341 
342 	if (get_user(len, optlen))
343 		return -EFAULT;
344 
345 	if (!optval || (len < sizeof(*crypto_info))) {
346 		rc = -EINVAL;
347 		goto out;
348 	}
349 
350 	if (!ctx) {
351 		rc = -EBUSY;
352 		goto out;
353 	}
354 
355 	/* get user crypto info */
356 	crypto_info = &ctx->crypto_send.info;
357 
358 	if (!TLS_CRYPTO_INFO_READY(crypto_info)) {
359 		rc = -EBUSY;
360 		goto out;
361 	}
362 
363 	if (len == sizeof(*crypto_info)) {
364 		if (copy_to_user(optval, crypto_info, sizeof(*crypto_info)))
365 			rc = -EFAULT;
366 		goto out;
367 	}
368 
369 	switch (crypto_info->cipher_type) {
370 	case TLS_CIPHER_AES_GCM_128: {
371 		struct tls12_crypto_info_aes_gcm_128 *
372 		  crypto_info_aes_gcm_128 =
373 		  container_of(crypto_info,
374 			       struct tls12_crypto_info_aes_gcm_128,
375 			       info);
376 
377 		if (len != sizeof(*crypto_info_aes_gcm_128)) {
378 			rc = -EINVAL;
379 			goto out;
380 		}
381 		lock_sock(sk);
382 		memcpy(crypto_info_aes_gcm_128->iv,
383 		       ctx->tx.iv + TLS_CIPHER_AES_GCM_128_SALT_SIZE,
384 		       TLS_CIPHER_AES_GCM_128_IV_SIZE);
385 		memcpy(crypto_info_aes_gcm_128->rec_seq, ctx->tx.rec_seq,
386 		       TLS_CIPHER_AES_GCM_128_REC_SEQ_SIZE);
387 		release_sock(sk);
388 		if (copy_to_user(optval,
389 				 crypto_info_aes_gcm_128,
390 				 sizeof(*crypto_info_aes_gcm_128)))
391 			rc = -EFAULT;
392 		break;
393 	}
394 	case TLS_CIPHER_AES_GCM_256: {
395 		struct tls12_crypto_info_aes_gcm_256 *
396 		  crypto_info_aes_gcm_256 =
397 		  container_of(crypto_info,
398 			       struct tls12_crypto_info_aes_gcm_256,
399 			       info);
400 
401 		if (len != sizeof(*crypto_info_aes_gcm_256)) {
402 			rc = -EINVAL;
403 			goto out;
404 		}
405 		lock_sock(sk);
406 		memcpy(crypto_info_aes_gcm_256->iv,
407 		       ctx->tx.iv + TLS_CIPHER_AES_GCM_256_SALT_SIZE,
408 		       TLS_CIPHER_AES_GCM_256_IV_SIZE);
409 		memcpy(crypto_info_aes_gcm_256->rec_seq, ctx->tx.rec_seq,
410 		       TLS_CIPHER_AES_GCM_256_REC_SEQ_SIZE);
411 		release_sock(sk);
412 		if (copy_to_user(optval,
413 				 crypto_info_aes_gcm_256,
414 				 sizeof(*crypto_info_aes_gcm_256)))
415 			rc = -EFAULT;
416 		break;
417 	}
418 	default:
419 		rc = -EINVAL;
420 	}
421 
422 out:
423 	return rc;
424 }
425 
426 static int do_tls_getsockopt(struct sock *sk, int optname,
427 			     char __user *optval, int __user *optlen)
428 {
429 	int rc = 0;
430 
431 	switch (optname) {
432 	case TLS_TX:
433 		rc = do_tls_getsockopt_tx(sk, optval, optlen);
434 		break;
435 	default:
436 		rc = -ENOPROTOOPT;
437 		break;
438 	}
439 	return rc;
440 }
441 
442 static int tls_getsockopt(struct sock *sk, int level, int optname,
443 			  char __user *optval, int __user *optlen)
444 {
445 	struct tls_context *ctx = tls_get_ctx(sk);
446 
447 	if (level != SOL_TLS)
448 		return ctx->sk_proto->getsockopt(sk, level,
449 						 optname, optval, optlen);
450 
451 	return do_tls_getsockopt(sk, optname, optval, optlen);
452 }
453 
454 static int do_tls_setsockopt_conf(struct sock *sk, char __user *optval,
455 				  unsigned int optlen, int tx)
456 {
457 	struct tls_crypto_info *crypto_info;
458 	struct tls_crypto_info *alt_crypto_info;
459 	struct tls_context *ctx = tls_get_ctx(sk);
460 	size_t optsize;
461 	int rc = 0;
462 	int conf;
463 
464 	if (!optval || (optlen < sizeof(*crypto_info))) {
465 		rc = -EINVAL;
466 		goto out;
467 	}
468 
469 	if (tx) {
470 		crypto_info = &ctx->crypto_send.info;
471 		alt_crypto_info = &ctx->crypto_recv.info;
472 	} else {
473 		crypto_info = &ctx->crypto_recv.info;
474 		alt_crypto_info = &ctx->crypto_send.info;
475 	}
476 
477 	/* Currently we don't support set crypto info more than one time */
478 	if (TLS_CRYPTO_INFO_READY(crypto_info)) {
479 		rc = -EBUSY;
480 		goto out;
481 	}
482 
483 	rc = copy_from_user(crypto_info, optval, sizeof(*crypto_info));
484 	if (rc) {
485 		rc = -EFAULT;
486 		goto err_crypto_info;
487 	}
488 
489 	/* check version */
490 	if (crypto_info->version != TLS_1_2_VERSION &&
491 	    crypto_info->version != TLS_1_3_VERSION) {
492 		rc = -ENOTSUPP;
493 		goto err_crypto_info;
494 	}
495 
496 	/* Ensure that TLS version and ciphers are same in both directions */
497 	if (TLS_CRYPTO_INFO_READY(alt_crypto_info)) {
498 		if (alt_crypto_info->version != crypto_info->version ||
499 		    alt_crypto_info->cipher_type != crypto_info->cipher_type) {
500 			rc = -EINVAL;
501 			goto err_crypto_info;
502 		}
503 	}
504 
505 	switch (crypto_info->cipher_type) {
506 	case TLS_CIPHER_AES_GCM_128:
507 		optsize = sizeof(struct tls12_crypto_info_aes_gcm_128);
508 		break;
509 	case TLS_CIPHER_AES_GCM_256: {
510 		optsize = sizeof(struct tls12_crypto_info_aes_gcm_256);
511 		break;
512 	}
513 	case TLS_CIPHER_AES_CCM_128:
514 		optsize = sizeof(struct tls12_crypto_info_aes_ccm_128);
515 		break;
516 	default:
517 		rc = -EINVAL;
518 		goto err_crypto_info;
519 	}
520 
521 	if (optlen != optsize) {
522 		rc = -EINVAL;
523 		goto err_crypto_info;
524 	}
525 
526 	rc = copy_from_user(crypto_info + 1, optval + sizeof(*crypto_info),
527 			    optlen - sizeof(*crypto_info));
528 	if (rc) {
529 		rc = -EFAULT;
530 		goto err_crypto_info;
531 	}
532 
533 	if (tx) {
534 		rc = tls_set_device_offload(sk, ctx);
535 		conf = TLS_HW;
536 		if (rc) {
537 			rc = tls_set_sw_offload(sk, ctx, 1);
538 			if (rc)
539 				goto err_crypto_info;
540 			conf = TLS_SW;
541 		}
542 	} else {
543 		rc = tls_set_device_offload_rx(sk, ctx);
544 		conf = TLS_HW;
545 		if (rc) {
546 			rc = tls_set_sw_offload(sk, ctx, 0);
547 			if (rc)
548 				goto err_crypto_info;
549 			conf = TLS_SW;
550 		}
551 		tls_sw_strparser_arm(sk, ctx);
552 	}
553 
554 	if (tx)
555 		ctx->tx_conf = conf;
556 	else
557 		ctx->rx_conf = conf;
558 	update_sk_prot(sk, ctx);
559 	if (tx) {
560 		ctx->sk_write_space = sk->sk_write_space;
561 		sk->sk_write_space = tls_write_space;
562 	} else {
563 		sk->sk_socket->ops = &tls_sw_proto_ops;
564 	}
565 	goto out;
566 
567 err_crypto_info:
568 	memzero_explicit(crypto_info, sizeof(union tls_crypto_context));
569 out:
570 	return rc;
571 }
572 
573 static int do_tls_setsockopt(struct sock *sk, int optname,
574 			     char __user *optval, unsigned int optlen)
575 {
576 	int rc = 0;
577 
578 	switch (optname) {
579 	case TLS_TX:
580 	case TLS_RX:
581 		lock_sock(sk);
582 		rc = do_tls_setsockopt_conf(sk, optval, optlen,
583 					    optname == TLS_TX);
584 		release_sock(sk);
585 		break;
586 	default:
587 		rc = -ENOPROTOOPT;
588 		break;
589 	}
590 	return rc;
591 }
592 
593 static int tls_setsockopt(struct sock *sk, int level, int optname,
594 			  char __user *optval, unsigned int optlen)
595 {
596 	struct tls_context *ctx = tls_get_ctx(sk);
597 
598 	if (level != SOL_TLS)
599 		return ctx->sk_proto->setsockopt(sk, level, optname, optval,
600 						 optlen);
601 
602 	return do_tls_setsockopt(sk, optname, optval, optlen);
603 }
604 
605 struct tls_context *tls_ctx_create(struct sock *sk)
606 {
607 	struct inet_connection_sock *icsk = inet_csk(sk);
608 	struct tls_context *ctx;
609 
610 	ctx = kzalloc(sizeof(*ctx), GFP_ATOMIC);
611 	if (!ctx)
612 		return NULL;
613 
614 	rcu_assign_pointer(icsk->icsk_ulp_data, ctx);
615 	ctx->sk_proto = sk->sk_prot;
616 	return ctx;
617 }
618 
619 static void tls_build_proto(struct sock *sk)
620 {
621 	int ip_ver = sk->sk_family == AF_INET6 ? TLSV6 : TLSV4;
622 
623 	/* Build IPv6 TLS whenever the address of tcpv6 _prot changes */
624 	if (ip_ver == TLSV6 &&
625 	    unlikely(sk->sk_prot != smp_load_acquire(&saved_tcpv6_prot))) {
626 		mutex_lock(&tcpv6_prot_mutex);
627 		if (likely(sk->sk_prot != saved_tcpv6_prot)) {
628 			build_protos(tls_prots[TLSV6], sk->sk_prot);
629 			smp_store_release(&saved_tcpv6_prot, sk->sk_prot);
630 		}
631 		mutex_unlock(&tcpv6_prot_mutex);
632 	}
633 
634 	if (ip_ver == TLSV4 &&
635 	    unlikely(sk->sk_prot != smp_load_acquire(&saved_tcpv4_prot))) {
636 		mutex_lock(&tcpv4_prot_mutex);
637 		if (likely(sk->sk_prot != saved_tcpv4_prot)) {
638 			build_protos(tls_prots[TLSV4], sk->sk_prot);
639 			smp_store_release(&saved_tcpv4_prot, sk->sk_prot);
640 		}
641 		mutex_unlock(&tcpv4_prot_mutex);
642 	}
643 }
644 
645 static void build_protos(struct proto prot[TLS_NUM_CONFIG][TLS_NUM_CONFIG],
646 			 struct proto *base)
647 {
648 	prot[TLS_BASE][TLS_BASE] = *base;
649 	prot[TLS_BASE][TLS_BASE].setsockopt	= tls_setsockopt;
650 	prot[TLS_BASE][TLS_BASE].getsockopt	= tls_getsockopt;
651 	prot[TLS_BASE][TLS_BASE].close		= tls_sk_proto_close;
652 
653 	prot[TLS_SW][TLS_BASE] = prot[TLS_BASE][TLS_BASE];
654 	prot[TLS_SW][TLS_BASE].sendmsg		= tls_sw_sendmsg;
655 	prot[TLS_SW][TLS_BASE].sendpage		= tls_sw_sendpage;
656 
657 	prot[TLS_BASE][TLS_SW] = prot[TLS_BASE][TLS_BASE];
658 	prot[TLS_BASE][TLS_SW].recvmsg		  = tls_sw_recvmsg;
659 	prot[TLS_BASE][TLS_SW].stream_memory_read = tls_sw_stream_read;
660 	prot[TLS_BASE][TLS_SW].close		  = tls_sk_proto_close;
661 
662 	prot[TLS_SW][TLS_SW] = prot[TLS_SW][TLS_BASE];
663 	prot[TLS_SW][TLS_SW].recvmsg		= tls_sw_recvmsg;
664 	prot[TLS_SW][TLS_SW].stream_memory_read	= tls_sw_stream_read;
665 	prot[TLS_SW][TLS_SW].close		= tls_sk_proto_close;
666 
667 #ifdef CONFIG_TLS_DEVICE
668 	prot[TLS_HW][TLS_BASE] = prot[TLS_BASE][TLS_BASE];
669 	prot[TLS_HW][TLS_BASE].sendmsg		= tls_device_sendmsg;
670 	prot[TLS_HW][TLS_BASE].sendpage		= tls_device_sendpage;
671 
672 	prot[TLS_HW][TLS_SW] = prot[TLS_BASE][TLS_SW];
673 	prot[TLS_HW][TLS_SW].sendmsg		= tls_device_sendmsg;
674 	prot[TLS_HW][TLS_SW].sendpage		= tls_device_sendpage;
675 
676 	prot[TLS_BASE][TLS_HW] = prot[TLS_BASE][TLS_SW];
677 
678 	prot[TLS_SW][TLS_HW] = prot[TLS_SW][TLS_SW];
679 
680 	prot[TLS_HW][TLS_HW] = prot[TLS_HW][TLS_SW];
681 #endif
682 
683 	prot[TLS_HW_RECORD][TLS_HW_RECORD] = *base;
684 	prot[TLS_HW_RECORD][TLS_HW_RECORD].hash		= tls_hw_hash;
685 	prot[TLS_HW_RECORD][TLS_HW_RECORD].unhash	= tls_hw_unhash;
686 }
687 
688 static int tls_init(struct sock *sk)
689 {
690 	struct tls_context *ctx;
691 	int rc = 0;
692 
693 	tls_build_proto(sk);
694 
695 	if (tls_hw_prot(sk))
696 		return 0;
697 
698 	/* The TLS ulp is currently supported only for TCP sockets
699 	 * in ESTABLISHED state.
700 	 * Supporting sockets in LISTEN state will require us
701 	 * to modify the accept implementation to clone rather then
702 	 * share the ulp context.
703 	 */
704 	if (sk->sk_state != TCP_ESTABLISHED)
705 		return -ENOTSUPP;
706 
707 	/* allocate tls context */
708 	write_lock_bh(&sk->sk_callback_lock);
709 	ctx = tls_ctx_create(sk);
710 	if (!ctx) {
711 		rc = -ENOMEM;
712 		goto out;
713 	}
714 
715 	ctx->tx_conf = TLS_BASE;
716 	ctx->rx_conf = TLS_BASE;
717 	update_sk_prot(sk, ctx);
718 out:
719 	write_unlock_bh(&sk->sk_callback_lock);
720 	return rc;
721 }
722 
723 static void tls_update(struct sock *sk, struct proto *p)
724 {
725 	struct tls_context *ctx;
726 
727 	ctx = tls_get_ctx(sk);
728 	if (likely(ctx))
729 		ctx->sk_proto = p;
730 	else
731 		sk->sk_prot = p;
732 }
733 
734 static int tls_get_info(const struct sock *sk, struct sk_buff *skb)
735 {
736 	u16 version, cipher_type;
737 	struct tls_context *ctx;
738 	struct nlattr *start;
739 	int err;
740 
741 	start = nla_nest_start_noflag(skb, INET_ULP_INFO_TLS);
742 	if (!start)
743 		return -EMSGSIZE;
744 
745 	rcu_read_lock();
746 	ctx = rcu_dereference(inet_csk(sk)->icsk_ulp_data);
747 	if (!ctx) {
748 		err = 0;
749 		goto nla_failure;
750 	}
751 	version = ctx->prot_info.version;
752 	if (version) {
753 		err = nla_put_u16(skb, TLS_INFO_VERSION, version);
754 		if (err)
755 			goto nla_failure;
756 	}
757 	cipher_type = ctx->prot_info.cipher_type;
758 	if (cipher_type) {
759 		err = nla_put_u16(skb, TLS_INFO_CIPHER, cipher_type);
760 		if (err)
761 			goto nla_failure;
762 	}
763 	err = nla_put_u16(skb, TLS_INFO_TXCONF, tls_user_config(ctx, true));
764 	if (err)
765 		goto nla_failure;
766 
767 	err = nla_put_u16(skb, TLS_INFO_RXCONF, tls_user_config(ctx, false));
768 	if (err)
769 		goto nla_failure;
770 
771 	rcu_read_unlock();
772 	nla_nest_end(skb, start);
773 	return 0;
774 
775 nla_failure:
776 	rcu_read_unlock();
777 	nla_nest_cancel(skb, start);
778 	return err;
779 }
780 
781 static size_t tls_get_info_size(const struct sock *sk)
782 {
783 	size_t size = 0;
784 
785 	size += nla_total_size(0) +		/* INET_ULP_INFO_TLS */
786 		nla_total_size(sizeof(u16)) +	/* TLS_INFO_VERSION */
787 		nla_total_size(sizeof(u16)) +	/* TLS_INFO_CIPHER */
788 		nla_total_size(sizeof(u16)) +	/* TLS_INFO_RXCONF */
789 		nla_total_size(sizeof(u16)) +	/* TLS_INFO_TXCONF */
790 		0;
791 
792 	return size;
793 }
794 
795 static struct tcp_ulp_ops tcp_tls_ulp_ops __read_mostly = {
796 	.name			= "tls",
797 	.owner			= THIS_MODULE,
798 	.init			= tls_init,
799 	.update			= tls_update,
800 	.get_info		= tls_get_info,
801 	.get_info_size		= tls_get_info_size,
802 };
803 
804 static int __init tls_register(void)
805 {
806 	tls_sw_proto_ops = inet_stream_ops;
807 	tls_sw_proto_ops.splice_read = tls_sw_splice_read;
808 
809 	tls_device_init();
810 	tcp_register_ulp(&tcp_tls_ulp_ops);
811 
812 	return 0;
813 }
814 
815 static void __exit tls_unregister(void)
816 {
817 	tcp_unregister_ulp(&tcp_tls_ulp_ops);
818 	tls_device_cleanup();
819 }
820 
821 module_init(tls_register);
822 module_exit(tls_unregister);
823