tls_main.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759
  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. #include <linux/module.h>
  34. #include <net/tcp.h>
  35. #include <net/inet_common.h>
  36. #include <linux/highmem.h>
  37. #include <linux/netdevice.h>
  38. #include <linux/sched/signal.h>
  39. #include <linux/inetdevice.h>
  40. #include <net/tls.h>
  41. MODULE_AUTHOR("Mellanox Technologies");
  42. MODULE_DESCRIPTION("Transport Layer Security Support");
  43. MODULE_LICENSE("Dual BSD/GPL");
  44. MODULE_ALIAS_TCP_ULP("tls");
  45. enum {
  46. TLSV4,
  47. TLSV6,
  48. TLS_NUM_PROTS,
  49. };
  50. static struct proto *saved_tcpv6_prot;
  51. static DEFINE_MUTEX(tcpv6_prot_mutex);
  52. static LIST_HEAD(device_list);
  53. static DEFINE_MUTEX(device_mutex);
  54. static struct proto tls_prots[TLS_NUM_PROTS][TLS_NUM_CONFIG][TLS_NUM_CONFIG];
  55. static struct proto_ops tls_sw_proto_ops;
  56. static void update_sk_prot(struct sock *sk, struct tls_context *ctx)
  57. {
  58. int ip_ver = sk->sk_family == AF_INET6 ? TLSV6 : TLSV4;
  59. sk->sk_prot = &tls_prots[ip_ver][ctx->tx_conf][ctx->rx_conf];
  60. }
  61. int wait_on_pending_writer(struct sock *sk, long *timeo)
  62. {
  63. int rc = 0;
  64. DEFINE_WAIT_FUNC(wait, woken_wake_function);
  65. add_wait_queue(sk_sleep(sk), &wait);
  66. while (1) {
  67. if (!*timeo) {
  68. rc = -EAGAIN;
  69. break;
  70. }
  71. if (signal_pending(current)) {
  72. rc = sock_intr_errno(*timeo);
  73. break;
  74. }
  75. if (sk_wait_event(sk, timeo, !sk->sk_write_pending, &wait))
  76. break;
  77. }
  78. remove_wait_queue(sk_sleep(sk), &wait);
  79. return rc;
  80. }
  81. int tls_push_sg(struct sock *sk,
  82. struct tls_context *ctx,
  83. struct scatterlist *sg,
  84. u16 first_offset,
  85. int flags)
  86. {
  87. int sendpage_flags = flags | MSG_SENDPAGE_NOTLAST;
  88. int ret = 0;
  89. struct page *p;
  90. size_t size;
  91. int offset = first_offset;
  92. size = sg->length - offset;
  93. offset += sg->offset;
  94. ctx->in_tcp_sendpages = true;
  95. while (1) {
  96. if (sg_is_last(sg))
  97. sendpage_flags = flags;
  98. /* is sending application-limited? */
  99. tcp_rate_check_app_limited(sk);
  100. p = sg_page(sg);
  101. retry:
  102. ret = do_tcp_sendpages(sk, p, offset, size, sendpage_flags);
  103. if (ret != size) {
  104. if (ret > 0) {
  105. offset += ret;
  106. size -= ret;
  107. goto retry;
  108. }
  109. offset -= sg->offset;
  110. ctx->partially_sent_offset = offset;
  111. ctx->partially_sent_record = (void *)sg;
  112. ctx->in_tcp_sendpages = false;
  113. return ret;
  114. }
  115. put_page(p);
  116. sk_mem_uncharge(sk, sg->length);
  117. sg = sg_next(sg);
  118. if (!sg)
  119. break;
  120. offset = sg->offset;
  121. size = sg->length;
  122. }
  123. clear_bit(TLS_PENDING_CLOSED_RECORD, &ctx->flags);
  124. ctx->in_tcp_sendpages = false;
  125. ctx->sk_write_space(sk);
  126. return 0;
  127. }
  128. static int tls_handle_open_record(struct sock *sk, int flags)
  129. {
  130. struct tls_context *ctx = tls_get_ctx(sk);
  131. if (tls_is_pending_open_record(ctx))
  132. return ctx->push_pending_record(sk, flags);
  133. return 0;
  134. }
  135. int tls_proccess_cmsg(struct sock *sk, struct msghdr *msg,
  136. unsigned char *record_type)
  137. {
  138. struct cmsghdr *cmsg;
  139. int rc = -EINVAL;
  140. for_each_cmsghdr(cmsg, msg) {
  141. if (!CMSG_OK(msg, cmsg))
  142. return -EINVAL;
  143. if (cmsg->cmsg_level != SOL_TLS)
  144. continue;
  145. switch (cmsg->cmsg_type) {
  146. case TLS_SET_RECORD_TYPE:
  147. if (cmsg->cmsg_len < CMSG_LEN(sizeof(*record_type)))
  148. return -EINVAL;
  149. if (msg->msg_flags & MSG_MORE)
  150. return -EINVAL;
  151. rc = tls_handle_open_record(sk, msg->msg_flags);
  152. if (rc)
  153. return rc;
  154. *record_type = *(unsigned char *)CMSG_DATA(cmsg);
  155. rc = 0;
  156. break;
  157. default:
  158. return -EINVAL;
  159. }
  160. }
  161. return rc;
  162. }
  163. int tls_push_pending_closed_record(struct sock *sk, struct tls_context *ctx,
  164. int flags, long *timeo)
  165. {
  166. struct scatterlist *sg;
  167. u16 offset;
  168. if (!tls_is_partially_sent_record(ctx))
  169. return ctx->push_pending_record(sk, flags);
  170. sg = ctx->partially_sent_record;
  171. offset = ctx->partially_sent_offset;
  172. ctx->partially_sent_record = NULL;
  173. return tls_push_sg(sk, ctx, sg, offset, flags);
  174. }
  175. static void tls_write_space(struct sock *sk)
  176. {
  177. struct tls_context *ctx = tls_get_ctx(sk);
  178. /* If in_tcp_sendpages call lower protocol write space handler
  179. * to ensure we wake up any waiting operations there. For example
  180. * if do_tcp_sendpages where to call sk_wait_event.
  181. */
  182. if (ctx->in_tcp_sendpages) {
  183. ctx->sk_write_space(sk);
  184. return;
  185. }
  186. if (!sk->sk_write_pending && tls_is_pending_closed_record(ctx)) {
  187. gfp_t sk_allocation = sk->sk_allocation;
  188. int rc;
  189. long timeo = 0;
  190. sk->sk_allocation = GFP_ATOMIC;
  191. rc = tls_push_pending_closed_record(sk, ctx,
  192. MSG_DONTWAIT |
  193. MSG_NOSIGNAL,
  194. &timeo);
  195. sk->sk_allocation = sk_allocation;
  196. if (rc < 0)
  197. return;
  198. }
  199. ctx->sk_write_space(sk);
  200. }
  201. void tls_ctx_free(struct tls_context *ctx)
  202. {
  203. if (!ctx)
  204. return;
  205. memzero_explicit(&ctx->crypto_send, sizeof(ctx->crypto_send));
  206. memzero_explicit(&ctx->crypto_recv, sizeof(ctx->crypto_recv));
  207. kfree(ctx);
  208. }
  209. static void tls_sk_proto_close(struct sock *sk, long timeout)
  210. {
  211. struct tls_context *ctx = tls_get_ctx(sk);
  212. long timeo = sock_sndtimeo(sk, 0);
  213. void (*sk_proto_close)(struct sock *sk, long timeout);
  214. bool free_ctx = false;
  215. lock_sock(sk);
  216. sk_proto_close = ctx->sk_proto_close;
  217. if ((ctx->tx_conf == TLS_HW_RECORD && ctx->rx_conf == TLS_HW_RECORD) ||
  218. (ctx->tx_conf == TLS_BASE && ctx->rx_conf == TLS_BASE)) {
  219. free_ctx = true;
  220. goto skip_tx_cleanup;
  221. }
  222. if (!tls_complete_pending_work(sk, ctx, 0, &timeo))
  223. tls_handle_open_record(sk, 0);
  224. if (ctx->partially_sent_record) {
  225. struct scatterlist *sg = ctx->partially_sent_record;
  226. while (1) {
  227. put_page(sg_page(sg));
  228. sk_mem_uncharge(sk, sg->length);
  229. if (sg_is_last(sg))
  230. break;
  231. sg++;
  232. }
  233. }
  234. /* We need these for tls_sw_fallback handling of other packets */
  235. if (ctx->tx_conf == TLS_SW) {
  236. kfree(ctx->tx.rec_seq);
  237. kfree(ctx->tx.iv);
  238. tls_sw_free_resources_tx(sk);
  239. }
  240. if (ctx->rx_conf == TLS_SW)
  241. tls_sw_free_resources_rx(sk);
  242. #ifdef CONFIG_TLS_DEVICE
  243. if (ctx->rx_conf == TLS_HW)
  244. tls_device_offload_cleanup_rx(sk);
  245. if (ctx->tx_conf != TLS_HW && ctx->rx_conf != TLS_HW) {
  246. #else
  247. {
  248. #endif
  249. if (sk->sk_write_space == tls_write_space)
  250. sk->sk_write_space = ctx->sk_write_space;
  251. tls_ctx_free(ctx);
  252. ctx = NULL;
  253. }
  254. skip_tx_cleanup:
  255. release_sock(sk);
  256. sk_proto_close(sk, timeout);
  257. /* free ctx for TLS_HW_RECORD, used by tcp_set_state
  258. * for sk->sk_prot->unhash [tls_hw_unhash]
  259. */
  260. if (free_ctx)
  261. tls_ctx_free(ctx);
  262. }
  263. static int do_tls_getsockopt_tx(struct sock *sk, char __user *optval,
  264. int __user *optlen)
  265. {
  266. int rc = 0;
  267. struct tls_context *ctx = tls_get_ctx(sk);
  268. struct tls_crypto_info *crypto_info;
  269. int len;
  270. if (get_user(len, optlen))
  271. return -EFAULT;
  272. if (!optval || (len < sizeof(*crypto_info))) {
  273. rc = -EINVAL;
  274. goto out;
  275. }
  276. if (!ctx) {
  277. rc = -EBUSY;
  278. goto out;
  279. }
  280. /* get user crypto info */
  281. crypto_info = &ctx->crypto_send.info;
  282. if (!TLS_CRYPTO_INFO_READY(crypto_info)) {
  283. rc = -EBUSY;
  284. goto out;
  285. }
  286. if (len == sizeof(*crypto_info)) {
  287. if (copy_to_user(optval, crypto_info, sizeof(*crypto_info)))
  288. rc = -EFAULT;
  289. goto out;
  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. if (len != sizeof(*crypto_info_aes_gcm_128)) {
  299. rc = -EINVAL;
  300. goto out;
  301. }
  302. lock_sock(sk);
  303. memcpy(crypto_info_aes_gcm_128->iv,
  304. ctx->tx.iv + TLS_CIPHER_AES_GCM_128_SALT_SIZE,
  305. TLS_CIPHER_AES_GCM_128_IV_SIZE);
  306. memcpy(crypto_info_aes_gcm_128->rec_seq, ctx->tx.rec_seq,
  307. TLS_CIPHER_AES_GCM_128_REC_SEQ_SIZE);
  308. release_sock(sk);
  309. if (copy_to_user(optval,
  310. crypto_info_aes_gcm_128,
  311. sizeof(*crypto_info_aes_gcm_128)))
  312. rc = -EFAULT;
  313. break;
  314. }
  315. default:
  316. rc = -EINVAL;
  317. }
  318. out:
  319. return rc;
  320. }
  321. static int do_tls_getsockopt(struct sock *sk, int optname,
  322. char __user *optval, int __user *optlen)
  323. {
  324. int rc = 0;
  325. switch (optname) {
  326. case TLS_TX:
  327. rc = do_tls_getsockopt_tx(sk, optval, optlen);
  328. break;
  329. default:
  330. rc = -ENOPROTOOPT;
  331. break;
  332. }
  333. return rc;
  334. }
  335. static int tls_getsockopt(struct sock *sk, int level, int optname,
  336. char __user *optval, int __user *optlen)
  337. {
  338. struct tls_context *ctx = tls_get_ctx(sk);
  339. if (level != SOL_TLS)
  340. return ctx->getsockopt(sk, level, optname, optval, optlen);
  341. return do_tls_getsockopt(sk, optname, optval, optlen);
  342. }
  343. static int do_tls_setsockopt_conf(struct sock *sk, char __user *optval,
  344. unsigned int optlen, int tx)
  345. {
  346. struct tls_crypto_info *crypto_info;
  347. struct tls_context *ctx = tls_get_ctx(sk);
  348. int rc = 0;
  349. int conf;
  350. if (!optval || (optlen < sizeof(*crypto_info))) {
  351. rc = -EINVAL;
  352. goto out;
  353. }
  354. if (tx)
  355. crypto_info = &ctx->crypto_send.info;
  356. else
  357. crypto_info = &ctx->crypto_recv.info;
  358. /* Currently we don't support set crypto info more than one time */
  359. if (TLS_CRYPTO_INFO_READY(crypto_info)) {
  360. rc = -EBUSY;
  361. goto out;
  362. }
  363. rc = copy_from_user(crypto_info, optval, sizeof(*crypto_info));
  364. if (rc) {
  365. rc = -EFAULT;
  366. goto err_crypto_info;
  367. }
  368. /* check version */
  369. if (crypto_info->version != TLS_1_2_VERSION) {
  370. rc = -ENOTSUPP;
  371. goto err_crypto_info;
  372. }
  373. switch (crypto_info->cipher_type) {
  374. case TLS_CIPHER_AES_GCM_128: {
  375. if (optlen != sizeof(struct tls12_crypto_info_aes_gcm_128)) {
  376. rc = -EINVAL;
  377. goto err_crypto_info;
  378. }
  379. rc = copy_from_user(crypto_info + 1, optval + sizeof(*crypto_info),
  380. optlen - sizeof(*crypto_info));
  381. if (rc) {
  382. rc = -EFAULT;
  383. goto err_crypto_info;
  384. }
  385. break;
  386. }
  387. default:
  388. rc = -EINVAL;
  389. goto err_crypto_info;
  390. }
  391. if (tx) {
  392. #ifdef CONFIG_TLS_DEVICE
  393. rc = tls_set_device_offload(sk, ctx);
  394. conf = TLS_HW;
  395. if (rc) {
  396. #else
  397. {
  398. #endif
  399. rc = tls_set_sw_offload(sk, ctx, 1);
  400. conf = TLS_SW;
  401. }
  402. } else {
  403. #ifdef CONFIG_TLS_DEVICE
  404. rc = tls_set_device_offload_rx(sk, ctx);
  405. conf = TLS_HW;
  406. if (rc) {
  407. #else
  408. {
  409. #endif
  410. rc = tls_set_sw_offload(sk, ctx, 0);
  411. conf = TLS_SW;
  412. }
  413. }
  414. if (rc)
  415. goto err_crypto_info;
  416. if (tx)
  417. ctx->tx_conf = conf;
  418. else
  419. ctx->rx_conf = conf;
  420. update_sk_prot(sk, ctx);
  421. if (tx) {
  422. ctx->sk_write_space = sk->sk_write_space;
  423. sk->sk_write_space = tls_write_space;
  424. } else {
  425. sk->sk_socket->ops = &tls_sw_proto_ops;
  426. }
  427. goto out;
  428. err_crypto_info:
  429. memzero_explicit(crypto_info, sizeof(union tls_crypto_context));
  430. out:
  431. return rc;
  432. }
  433. static int do_tls_setsockopt(struct sock *sk, int optname,
  434. char __user *optval, unsigned int optlen)
  435. {
  436. int rc = 0;
  437. switch (optname) {
  438. case TLS_TX:
  439. case TLS_RX:
  440. lock_sock(sk);
  441. rc = do_tls_setsockopt_conf(sk, optval, optlen,
  442. optname == TLS_TX);
  443. release_sock(sk);
  444. break;
  445. default:
  446. rc = -ENOPROTOOPT;
  447. break;
  448. }
  449. return rc;
  450. }
  451. static int tls_setsockopt(struct sock *sk, int level, int optname,
  452. char __user *optval, unsigned int optlen)
  453. {
  454. struct tls_context *ctx = tls_get_ctx(sk);
  455. if (level != SOL_TLS)
  456. return ctx->setsockopt(sk, level, optname, optval, optlen);
  457. return do_tls_setsockopt(sk, optname, optval, optlen);
  458. }
  459. static struct tls_context *create_ctx(struct sock *sk)
  460. {
  461. struct inet_connection_sock *icsk = inet_csk(sk);
  462. struct tls_context *ctx;
  463. ctx = kzalloc(sizeof(*ctx), GFP_ATOMIC);
  464. if (!ctx)
  465. return NULL;
  466. icsk->icsk_ulp_data = ctx;
  467. ctx->setsockopt = sk->sk_prot->setsockopt;
  468. ctx->getsockopt = sk->sk_prot->getsockopt;
  469. ctx->sk_proto_close = sk->sk_prot->close;
  470. return ctx;
  471. }
  472. static int tls_hw_prot(struct sock *sk)
  473. {
  474. struct tls_context *ctx;
  475. struct tls_device *dev;
  476. int rc = 0;
  477. mutex_lock(&device_mutex);
  478. list_for_each_entry(dev, &device_list, dev_list) {
  479. if (dev->feature && dev->feature(dev)) {
  480. ctx = create_ctx(sk);
  481. if (!ctx)
  482. goto out;
  483. ctx->hash = sk->sk_prot->hash;
  484. ctx->unhash = sk->sk_prot->unhash;
  485. ctx->sk_proto_close = sk->sk_prot->close;
  486. ctx->rx_conf = TLS_HW_RECORD;
  487. ctx->tx_conf = TLS_HW_RECORD;
  488. update_sk_prot(sk, ctx);
  489. rc = 1;
  490. break;
  491. }
  492. }
  493. out:
  494. mutex_unlock(&device_mutex);
  495. return rc;
  496. }
  497. static void tls_hw_unhash(struct sock *sk)
  498. {
  499. struct tls_context *ctx = tls_get_ctx(sk);
  500. struct tls_device *dev;
  501. mutex_lock(&device_mutex);
  502. list_for_each_entry(dev, &device_list, dev_list) {
  503. if (dev->unhash)
  504. dev->unhash(dev, sk);
  505. }
  506. mutex_unlock(&device_mutex);
  507. ctx->unhash(sk);
  508. }
  509. static int tls_hw_hash(struct sock *sk)
  510. {
  511. struct tls_context *ctx = tls_get_ctx(sk);
  512. struct tls_device *dev;
  513. int err;
  514. err = ctx->hash(sk);
  515. mutex_lock(&device_mutex);
  516. list_for_each_entry(dev, &device_list, dev_list) {
  517. if (dev->hash)
  518. err |= dev->hash(dev, sk);
  519. }
  520. mutex_unlock(&device_mutex);
  521. if (err)
  522. tls_hw_unhash(sk);
  523. return err;
  524. }
  525. static void build_protos(struct proto prot[TLS_NUM_CONFIG][TLS_NUM_CONFIG],
  526. struct proto *base)
  527. {
  528. prot[TLS_BASE][TLS_BASE] = *base;
  529. prot[TLS_BASE][TLS_BASE].setsockopt = tls_setsockopt;
  530. prot[TLS_BASE][TLS_BASE].getsockopt = tls_getsockopt;
  531. prot[TLS_BASE][TLS_BASE].close = tls_sk_proto_close;
  532. prot[TLS_SW][TLS_BASE] = prot[TLS_BASE][TLS_BASE];
  533. prot[TLS_SW][TLS_BASE].sendmsg = tls_sw_sendmsg;
  534. prot[TLS_SW][TLS_BASE].sendpage = tls_sw_sendpage;
  535. prot[TLS_BASE][TLS_SW] = prot[TLS_BASE][TLS_BASE];
  536. prot[TLS_BASE][TLS_SW].recvmsg = tls_sw_recvmsg;
  537. prot[TLS_BASE][TLS_SW].close = tls_sk_proto_close;
  538. prot[TLS_SW][TLS_SW] = prot[TLS_SW][TLS_BASE];
  539. prot[TLS_SW][TLS_SW].recvmsg = tls_sw_recvmsg;
  540. prot[TLS_SW][TLS_SW].close = tls_sk_proto_close;
  541. #ifdef CONFIG_TLS_DEVICE
  542. prot[TLS_HW][TLS_BASE] = prot[TLS_BASE][TLS_BASE];
  543. prot[TLS_HW][TLS_BASE].sendmsg = tls_device_sendmsg;
  544. prot[TLS_HW][TLS_BASE].sendpage = tls_device_sendpage;
  545. prot[TLS_HW][TLS_SW] = prot[TLS_BASE][TLS_SW];
  546. prot[TLS_HW][TLS_SW].sendmsg = tls_device_sendmsg;
  547. prot[TLS_HW][TLS_SW].sendpage = tls_device_sendpage;
  548. prot[TLS_BASE][TLS_HW] = prot[TLS_BASE][TLS_SW];
  549. prot[TLS_SW][TLS_HW] = prot[TLS_SW][TLS_SW];
  550. prot[TLS_HW][TLS_HW] = prot[TLS_HW][TLS_SW];
  551. #endif
  552. prot[TLS_HW_RECORD][TLS_HW_RECORD] = *base;
  553. prot[TLS_HW_RECORD][TLS_HW_RECORD].hash = tls_hw_hash;
  554. prot[TLS_HW_RECORD][TLS_HW_RECORD].unhash = tls_hw_unhash;
  555. prot[TLS_HW_RECORD][TLS_HW_RECORD].close = tls_sk_proto_close;
  556. }
  557. static int tls_init(struct sock *sk)
  558. {
  559. int ip_ver = sk->sk_family == AF_INET6 ? TLSV6 : TLSV4;
  560. struct tls_context *ctx;
  561. int rc = 0;
  562. if (tls_hw_prot(sk))
  563. goto out;
  564. /* The TLS ulp is currently supported only for TCP sockets
  565. * in ESTABLISHED state.
  566. * Supporting sockets in LISTEN state will require us
  567. * to modify the accept implementation to clone rather then
  568. * share the ulp context.
  569. */
  570. if (sk->sk_state != TCP_ESTABLISHED)
  571. return -ENOTSUPP;
  572. /* allocate tls context */
  573. ctx = create_ctx(sk);
  574. if (!ctx) {
  575. rc = -ENOMEM;
  576. goto out;
  577. }
  578. /* Build IPv6 TLS whenever the address of tcpv6 _prot changes */
  579. if (ip_ver == TLSV6 &&
  580. unlikely(sk->sk_prot != smp_load_acquire(&saved_tcpv6_prot))) {
  581. mutex_lock(&tcpv6_prot_mutex);
  582. if (likely(sk->sk_prot != saved_tcpv6_prot)) {
  583. build_protos(tls_prots[TLSV6], sk->sk_prot);
  584. smp_store_release(&saved_tcpv6_prot, sk->sk_prot);
  585. }
  586. mutex_unlock(&tcpv6_prot_mutex);
  587. }
  588. ctx->tx_conf = TLS_BASE;
  589. ctx->rx_conf = TLS_BASE;
  590. update_sk_prot(sk, ctx);
  591. out:
  592. return rc;
  593. }
  594. void tls_register_device(struct tls_device *device)
  595. {
  596. mutex_lock(&device_mutex);
  597. list_add_tail(&device->dev_list, &device_list);
  598. mutex_unlock(&device_mutex);
  599. }
  600. EXPORT_SYMBOL(tls_register_device);
  601. void tls_unregister_device(struct tls_device *device)
  602. {
  603. mutex_lock(&device_mutex);
  604. list_del(&device->dev_list);
  605. mutex_unlock(&device_mutex);
  606. }
  607. EXPORT_SYMBOL(tls_unregister_device);
  608. static struct tcp_ulp_ops tcp_tls_ulp_ops __read_mostly = {
  609. .name = "tls",
  610. .uid = TCP_ULP_TLS,
  611. .user_visible = true,
  612. .owner = THIS_MODULE,
  613. .init = tls_init,
  614. };
  615. static int __init tls_register(void)
  616. {
  617. build_protos(tls_prots[TLSV4], &tcp_prot);
  618. tls_sw_proto_ops = inet_stream_ops;
  619. tls_sw_proto_ops.poll = tls_sw_poll;
  620. tls_sw_proto_ops.splice_read = tls_sw_splice_read;
  621. #ifdef CONFIG_TLS_DEVICE
  622. tls_device_init();
  623. #endif
  624. tcp_register_ulp(&tcp_tls_ulp_ops);
  625. return 0;
  626. }
  627. static void __exit tls_unregister(void)
  628. {
  629. tcp_unregister_ulp(&tcp_tls_ulp_ops);
  630. #ifdef CONFIG_TLS_DEVICE
  631. tls_device_cleanup();
  632. #endif
  633. }
  634. module_init(tls_register);
  635. module_exit(tls_unregister);