blob: de6a1416bc41a14d32fcd57a88eba8eb7776fa75 [file] [log] [blame]
Dave Watson3c4d7552017-06-14 11:37:39 -07001/*
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
42#include <net/tls.h>
43
44MODULE_AUTHOR("Mellanox Technologies");
45MODULE_DESCRIPTION("Transport Layer Security Support");
46MODULE_LICENSE("Dual BSD/GPL");
47
Ilya Lesokhin6d882072017-11-13 10:22:45 +020048enum {
49 TLS_BASE_TX,
50 TLS_SW_TX,
51 TLS_NUM_CONFIG,
52};
53
54static struct proto tls_prots[TLS_NUM_CONFIG];
55
56static inline void update_sk_prot(struct sock *sk, struct tls_context *ctx)
57{
58 sk->sk_prot = &tls_prots[ctx->tx_conf];
59}
Dave Watson3c4d7552017-06-14 11:37:39 -070060
61int wait_on_pending_writer(struct sock *sk, long *timeo)
62{
63 int rc = 0;
64 DEFINE_WAIT_FUNC(wait, woken_wake_function);
65
66 add_wait_queue(sk_sleep(sk), &wait);
67 while (1) {
68 if (!*timeo) {
69 rc = -EAGAIN;
70 break;
71 }
72
73 if (signal_pending(current)) {
74 rc = sock_intr_errno(*timeo);
75 break;
76 }
77
78 if (sk_wait_event(sk, timeo, !sk->sk_write_pending, &wait))
79 break;
80 }
81 remove_wait_queue(sk_sleep(sk), &wait);
82 return rc;
83}
84
85int tls_push_sg(struct sock *sk,
86 struct tls_context *ctx,
87 struct scatterlist *sg,
88 u16 first_offset,
89 int flags)
90{
91 int sendpage_flags = flags | MSG_SENDPAGE_NOTLAST;
92 int ret = 0;
93 struct page *p;
94 size_t size;
95 int offset = first_offset;
96
97 size = sg->length - offset;
98 offset += sg->offset;
99
100 while (1) {
101 if (sg_is_last(sg))
102 sendpage_flags = flags;
103
104 /* is sending application-limited? */
105 tcp_rate_check_app_limited(sk);
106 p = sg_page(sg);
107retry:
108 ret = do_tcp_sendpages(sk, p, offset, size, sendpage_flags);
109
110 if (ret != size) {
111 if (ret > 0) {
112 offset += ret;
113 size -= ret;
114 goto retry;
115 }
116
117 offset -= sg->offset;
118 ctx->partially_sent_offset = offset;
119 ctx->partially_sent_record = (void *)sg;
120 return ret;
121 }
122
123 put_page(p);
124 sk_mem_uncharge(sk, sg->length);
125 sg = sg_next(sg);
126 if (!sg)
127 break;
128
129 offset = sg->offset;
130 size = sg->length;
131 }
132
133 clear_bit(TLS_PENDING_CLOSED_RECORD, &ctx->flags);
134
135 return 0;
136}
137
138static int tls_handle_open_record(struct sock *sk, int flags)
139{
140 struct tls_context *ctx = tls_get_ctx(sk);
141
142 if (tls_is_pending_open_record(ctx))
143 return ctx->push_pending_record(sk, flags);
144
145 return 0;
146}
147
148int tls_proccess_cmsg(struct sock *sk, struct msghdr *msg,
149 unsigned char *record_type)
150{
151 struct cmsghdr *cmsg;
152 int rc = -EINVAL;
153
154 for_each_cmsghdr(cmsg, msg) {
155 if (!CMSG_OK(msg, cmsg))
156 return -EINVAL;
157 if (cmsg->cmsg_level != SOL_TLS)
158 continue;
159
160 switch (cmsg->cmsg_type) {
161 case TLS_SET_RECORD_TYPE:
162 if (cmsg->cmsg_len < CMSG_LEN(sizeof(*record_type)))
163 return -EINVAL;
164
165 if (msg->msg_flags & MSG_MORE)
166 return -EINVAL;
167
168 rc = tls_handle_open_record(sk, msg->msg_flags);
169 if (rc)
170 return rc;
171
172 *record_type = *(unsigned char *)CMSG_DATA(cmsg);
173 rc = 0;
174 break;
175 default:
176 return -EINVAL;
177 }
178 }
179
180 return rc;
181}
182
183int tls_push_pending_closed_record(struct sock *sk, struct tls_context *ctx,
184 int flags, long *timeo)
185{
186 struct scatterlist *sg;
187 u16 offset;
188
189 if (!tls_is_partially_sent_record(ctx))
190 return ctx->push_pending_record(sk, flags);
191
192 sg = ctx->partially_sent_record;
193 offset = ctx->partially_sent_offset;
194
195 ctx->partially_sent_record = NULL;
196 return tls_push_sg(sk, ctx, sg, offset, flags);
197}
198
199static void tls_write_space(struct sock *sk)
200{
201 struct tls_context *ctx = tls_get_ctx(sk);
202
203 if (!sk->sk_write_pending && tls_is_pending_closed_record(ctx)) {
204 gfp_t sk_allocation = sk->sk_allocation;
205 int rc;
206 long timeo = 0;
207
208 sk->sk_allocation = GFP_ATOMIC;
209 rc = tls_push_pending_closed_record(sk, ctx,
210 MSG_DONTWAIT |
211 MSG_NOSIGNAL,
212 &timeo);
213 sk->sk_allocation = sk_allocation;
214
215 if (rc < 0)
216 return;
217 }
218
219 ctx->sk_write_space(sk);
220}
221
222static void tls_sk_proto_close(struct sock *sk, long timeout)
223{
224 struct tls_context *ctx = tls_get_ctx(sk);
225 long timeo = sock_sndtimeo(sk, 0);
226 void (*sk_proto_close)(struct sock *sk, long timeout);
227
228 lock_sock(sk);
229
230 if (!tls_complete_pending_work(sk, ctx, 0, &timeo))
231 tls_handle_open_record(sk, 0);
232
233 if (ctx->partially_sent_record) {
234 struct scatterlist *sg = ctx->partially_sent_record;
235
236 while (1) {
237 put_page(sg_page(sg));
238 sk_mem_uncharge(sk, sg->length);
239
240 if (sg_is_last(sg))
241 break;
242 sg++;
243 }
244 }
245 ctx->free_resources(sk);
246 kfree(ctx->rec_seq);
247 kfree(ctx->iv);
248
249 sk_proto_close = ctx->sk_proto_close;
250 kfree(ctx);
251
252 release_sock(sk);
253 sk_proto_close(sk, timeout);
254}
255
256static int do_tls_getsockopt_tx(struct sock *sk, char __user *optval,
257 int __user *optlen)
258{
259 int rc = 0;
260 struct tls_context *ctx = tls_get_ctx(sk);
261 struct tls_crypto_info *crypto_info;
262 int len;
263
264 if (get_user(len, optlen))
265 return -EFAULT;
266
267 if (!optval || (len < sizeof(*crypto_info))) {
268 rc = -EINVAL;
269 goto out;
270 }
271
272 if (!ctx) {
273 rc = -EBUSY;
274 goto out;
275 }
276
277 /* get user crypto info */
278 crypto_info = &ctx->crypto_send;
279
280 if (!TLS_CRYPTO_INFO_READY(crypto_info)) {
281 rc = -EBUSY;
282 goto out;
283 }
284
Matthias Rosenfelder5a3b8862017-07-06 00:56:36 -0400285 if (len == sizeof(*crypto_info)) {
Dan Carpenterac55cd62017-06-23 13:15:44 +0300286 if (copy_to_user(optval, crypto_info, sizeof(*crypto_info)))
287 rc = -EFAULT;
Dave Watson3c4d7552017-06-14 11:37:39 -0700288 goto out;
289 }
290
291 switch (crypto_info->cipher_type) {
292 case TLS_CIPHER_AES_GCM_128: {
293 struct tls12_crypto_info_aes_gcm_128 *
294 crypto_info_aes_gcm_128 =
295 container_of(crypto_info,
296 struct tls12_crypto_info_aes_gcm_128,
297 info);
298
299 if (len != sizeof(*crypto_info_aes_gcm_128)) {
300 rc = -EINVAL;
301 goto out;
302 }
303 lock_sock(sk);
304 memcpy(crypto_info_aes_gcm_128->iv, ctx->iv,
305 TLS_CIPHER_AES_GCM_128_IV_SIZE);
306 release_sock(sk);
Dan Carpenterac55cd62017-06-23 13:15:44 +0300307 if (copy_to_user(optval,
308 crypto_info_aes_gcm_128,
309 sizeof(*crypto_info_aes_gcm_128)))
310 rc = -EFAULT;
Dave Watson3c4d7552017-06-14 11:37:39 -0700311 break;
312 }
313 default:
314 rc = -EINVAL;
315 }
316
317out:
318 return rc;
319}
320
321static int do_tls_getsockopt(struct sock *sk, int optname,
322 char __user *optval, int __user *optlen)
323{
324 int rc = 0;
325
326 switch (optname) {
327 case TLS_TX:
328 rc = do_tls_getsockopt_tx(sk, optval, optlen);
329 break;
330 default:
331 rc = -ENOPROTOOPT;
332 break;
333 }
334 return rc;
335}
336
337static int tls_getsockopt(struct sock *sk, int level, int optname,
338 char __user *optval, int __user *optlen)
339{
340 struct tls_context *ctx = tls_get_ctx(sk);
341
342 if (level != SOL_TLS)
343 return ctx->getsockopt(sk, level, optname, optval, optlen);
344
345 return do_tls_getsockopt(sk, optname, optval, optlen);
346}
347
348static int do_tls_setsockopt_tx(struct sock *sk, char __user *optval,
349 unsigned int optlen)
350{
351 struct tls_crypto_info *crypto_info, tmp_crypto_info;
352 struct tls_context *ctx = tls_get_ctx(sk);
Dave Watson3c4d7552017-06-14 11:37:39 -0700353 int rc = 0;
Ilya Lesokhin6d882072017-11-13 10:22:45 +0200354 int tx_conf;
Dave Watson3c4d7552017-06-14 11:37:39 -0700355
356 if (!optval || (optlen < sizeof(*crypto_info))) {
357 rc = -EINVAL;
358 goto out;
359 }
360
361 rc = copy_from_user(&tmp_crypto_info, optval, sizeof(*crypto_info));
362 if (rc) {
363 rc = -EFAULT;
364 goto out;
365 }
366
367 /* check version */
368 if (tmp_crypto_info.version != TLS_1_2_VERSION) {
369 rc = -ENOTSUPP;
370 goto out;
371 }
372
373 /* get user crypto info */
374 crypto_info = &ctx->crypto_send;
375
376 /* Currently we don't support set crypto info more than one time */
377 if (TLS_CRYPTO_INFO_READY(crypto_info))
378 goto out;
379
380 switch (tmp_crypto_info.cipher_type) {
381 case TLS_CIPHER_AES_GCM_128: {
382 if (optlen != sizeof(struct tls12_crypto_info_aes_gcm_128)) {
383 rc = -EINVAL;
384 goto out;
385 }
386 rc = copy_from_user(
387 crypto_info,
388 optval,
389 sizeof(struct tls12_crypto_info_aes_gcm_128));
390
391 if (rc) {
392 rc = -EFAULT;
393 goto err_crypto_info;
394 }
395 break;
396 }
397 default:
398 rc = -EINVAL;
399 goto out;
400 }
401
402 ctx->sk_write_space = sk->sk_write_space;
403 sk->sk_write_space = tls_write_space;
404
405 ctx->sk_proto_close = sk->sk_prot->close;
406
407 /* currently SW is default, we will have ethtool in future */
408 rc = tls_set_sw_offload(sk, ctx);
Ilya Lesokhin6d882072017-11-13 10:22:45 +0200409 tx_conf = TLS_SW_TX;
Dave Watson3c4d7552017-06-14 11:37:39 -0700410 if (rc)
411 goto err_crypto_info;
412
Ilya Lesokhin6d882072017-11-13 10:22:45 +0200413 ctx->tx_conf = tx_conf;
414 update_sk_prot(sk, ctx);
Dave Watson3c4d7552017-06-14 11:37:39 -0700415 goto out;
416
417err_crypto_info:
418 memset(crypto_info, 0, sizeof(*crypto_info));
419out:
420 return rc;
421}
422
423static int do_tls_setsockopt(struct sock *sk, int optname,
424 char __user *optval, unsigned int optlen)
425{
426 int rc = 0;
427
428 switch (optname) {
429 case TLS_TX:
430 lock_sock(sk);
431 rc = do_tls_setsockopt_tx(sk, optval, optlen);
432 release_sock(sk);
433 break;
434 default:
435 rc = -ENOPROTOOPT;
436 break;
437 }
438 return rc;
439}
440
441static int tls_setsockopt(struct sock *sk, int level, int optname,
442 char __user *optval, unsigned int optlen)
443{
444 struct tls_context *ctx = tls_get_ctx(sk);
445
446 if (level != SOL_TLS)
447 return ctx->setsockopt(sk, level, optname, optval, optlen);
448
449 return do_tls_setsockopt(sk, optname, optval, optlen);
450}
451
452static int tls_init(struct sock *sk)
453{
454 struct inet_connection_sock *icsk = inet_csk(sk);
455 struct tls_context *ctx;
456 int rc = 0;
457
458 /* allocate tls context */
459 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
460 if (!ctx) {
461 rc = -ENOMEM;
462 goto out;
463 }
464 icsk->icsk_ulp_data = ctx;
465 ctx->setsockopt = sk->sk_prot->setsockopt;
466 ctx->getsockopt = sk->sk_prot->getsockopt;
Ilya Lesokhin6d882072017-11-13 10:22:45 +0200467
468 ctx->tx_conf = TLS_BASE_TX;
469 update_sk_prot(sk, ctx);
Dave Watson3c4d7552017-06-14 11:37:39 -0700470out:
471 return rc;
472}
473
474static struct tcp_ulp_ops tcp_tls_ulp_ops __read_mostly = {
475 .name = "tls",
476 .owner = THIS_MODULE,
477 .init = tls_init,
478};
479
Ilya Lesokhin6d882072017-11-13 10:22:45 +0200480static void build_protos(struct proto *prot, struct proto *base)
481{
482 prot[TLS_BASE_TX] = *base;
483 prot[TLS_BASE_TX].setsockopt = tls_setsockopt;
484 prot[TLS_BASE_TX].getsockopt = tls_getsockopt;
485
486 prot[TLS_SW_TX] = prot[TLS_BASE_TX];
487 prot[TLS_SW_TX].close = tls_sk_proto_close;
488 prot[TLS_SW_TX].sendmsg = tls_sw_sendmsg;
489 prot[TLS_SW_TX].sendpage = tls_sw_sendpage;
490}
491
Dave Watson3c4d7552017-06-14 11:37:39 -0700492static int __init tls_register(void)
493{
Ilya Lesokhin6d882072017-11-13 10:22:45 +0200494 build_protos(tls_prots, &tcp_prot);
Dave Watson3c4d7552017-06-14 11:37:39 -0700495
496 tcp_register_ulp(&tcp_tls_ulp_ops);
497
498 return 0;
499}
500
501static void __exit tls_unregister(void)
502{
503 tcp_unregister_ulp(&tcp_tls_ulp_ops);
504}
505
506module_init(tls_register);
507module_exit(tls_unregister);