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. static 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. kfree(ctx->rx.rec_seq);
  242. kfree(ctx->rx.iv);
  243. tls_sw_free_resources_rx(sk);
  244. }
  245. #ifdef CONFIG_TLS_DEVICE
  246. if (ctx->rx_conf == TLS_HW)
  247. tls_device_offload_cleanup_rx(sk);
  248. if (ctx->tx_conf != TLS_HW && ctx->rx_conf != TLS_HW) {
  249. #else
  250. {
  251. #endif
  252. tls_ctx_free(ctx);
  253. ctx = NULL;
  254. }
  255. skip_tx_cleanup:
  256. release_sock(sk);
  257. sk_proto_close(sk, timeout);
  258. /* free ctx for TLS_HW_RECORD, used by tcp_set_state
  259. * for sk->sk_prot->unhash [tls_hw_unhash]
  260. */
  261. if (free_ctx)
  262. tls_ctx_free(ctx);
  263. }
  264. static int do_tls_getsockopt_tx(struct sock *sk, char __user *optval,
  265. int __user *optlen)
  266. {
  267. int rc = 0;
  268. struct tls_context *ctx = tls_get_ctx(sk);
  269. struct tls_crypto_info *crypto_info;
  270. int len;
  271. if (get_user(len, optlen))
  272. return -EFAULT;
  273. if (!optval || (len < sizeof(*crypto_info))) {
  274. rc = -EINVAL;
  275. goto out;
  276. }
  277. if (!ctx) {
  278. rc = -EBUSY;
  279. goto out;
  280. }
  281. /* get user crypto info */
  282. crypto_info = &ctx->crypto_send.info;
  283. if (!TLS_CRYPTO_INFO_READY(crypto_info)) {
  284. rc = -EBUSY;
  285. goto out;
  286. }
  287. if (len == sizeof(*crypto_info)) {
  288. if (copy_to_user(optval, crypto_info, sizeof(*crypto_info)))
  289. rc = -EFAULT;
  290. goto out;
  291. }
  292. switch (crypto_info->cipher_type) {
  293. case TLS_CIPHER_AES_GCM_128: {
  294. struct tls12_crypto_info_aes_gcm_128 *
  295. crypto_info_aes_gcm_128 =
  296. container_of(crypto_info,
  297. struct tls12_crypto_info_aes_gcm_128,
  298. info);
  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,
  305. ctx->tx.iv + TLS_CIPHER_AES_GCM_128_SALT_SIZE,
  306. TLS_CIPHER_AES_GCM_128_IV_SIZE);
  307. memcpy(crypto_info_aes_gcm_128->rec_seq, ctx->tx.rec_seq,
  308. TLS_CIPHER_AES_GCM_128_REC_SEQ_SIZE);
  309. release_sock(sk);
  310. if (copy_to_user(optval,
  311. crypto_info_aes_gcm_128,
  312. sizeof(*crypto_info_aes_gcm_128)))
  313. rc = -EFAULT;
  314. break;
  315. }
  316. default:
  317. rc = -EINVAL;
  318. }
  319. out:
  320. return rc;
  321. }
  322. static int do_tls_getsockopt(struct sock *sk, int optname,
  323. char __user *optval, int __user *optlen)
  324. {
  325. int rc = 0;
  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. static int tls_getsockopt(struct sock *sk, int level, int optname,
  337. char __user *optval, int __user *optlen)
  338. {
  339. struct tls_context *ctx = tls_get_ctx(sk);
  340. if (level != SOL_TLS)
  341. return ctx->getsockopt(sk, level, optname, optval, optlen);
  342. return do_tls_getsockopt(sk, optname, optval, optlen);
  343. }
  344. static int do_tls_setsockopt_conf(struct sock *sk, char __user *optval,
  345. unsigned int optlen, int tx)
  346. {
  347. struct tls_crypto_info *crypto_info;
  348. struct tls_context *ctx = tls_get_ctx(sk);
  349. int rc = 0;
  350. int conf;
  351. if (!optval || (optlen < sizeof(*crypto_info))) {
  352. rc = -EINVAL;
  353. goto out;
  354. }
  355. if (tx)
  356. crypto_info = &ctx->crypto_send.info;
  357. else
  358. crypto_info = &ctx->crypto_recv.info;
  359. /* Currently we don't support set crypto info more than one time */
  360. if (TLS_CRYPTO_INFO_READY(crypto_info)) {
  361. rc = -EBUSY;
  362. goto out;
  363. }
  364. rc = copy_from_user(crypto_info, optval, sizeof(*crypto_info));
  365. if (rc) {
  366. rc = -EFAULT;
  367. goto err_crypto_info;
  368. }
  369. /* check version */
  370. if (crypto_info->version != TLS_1_2_VERSION) {
  371. rc = -ENOTSUPP;
  372. goto err_crypto_info;
  373. }
  374. switch (crypto_info->cipher_type) {
  375. case TLS_CIPHER_AES_GCM_128: {
  376. if (optlen != sizeof(struct tls12_crypto_info_aes_gcm_128)) {
  377. rc = -EINVAL;
  378. goto err_crypto_info;
  379. }
  380. rc = copy_from_user(crypto_info + 1, optval + sizeof(*crypto_info),
  381. optlen - sizeof(*crypto_info));
  382. if (rc) {
  383. rc = -EFAULT;
  384. goto err_crypto_info;
  385. }
  386. break;
  387. }
  388. default:
  389. rc = -EINVAL;
  390. goto err_crypto_info;
  391. }
  392. if (tx) {
  393. #ifdef CONFIG_TLS_DEVICE
  394. rc = tls_set_device_offload(sk, ctx);
  395. conf = TLS_HW;
  396. if (rc) {
  397. #else
  398. {
  399. #endif
  400. rc = tls_set_sw_offload(sk, ctx, 1);
  401. conf = TLS_SW;
  402. }
  403. } else {
  404. #ifdef CONFIG_TLS_DEVICE
  405. rc = tls_set_device_offload_rx(sk, ctx);
  406. conf = TLS_HW;
  407. if (rc) {
  408. #else
  409. {
  410. #endif
  411. rc = tls_set_sw_offload(sk, ctx, 0);
  412. conf = TLS_SW;
  413. }
  414. }
  415. if (rc)
  416. goto err_crypto_info;
  417. if (tx)
  418. ctx->tx_conf = conf;
  419. else
  420. ctx->rx_conf = conf;
  421. update_sk_prot(sk, ctx);
  422. if (tx) {
  423. ctx->sk_write_space = sk->sk_write_space;
  424. sk->sk_write_space = tls_write_space;
  425. } else {
  426. sk->sk_socket->ops = &tls_sw_proto_ops;
  427. }
  428. goto out;
  429. err_crypto_info:
  430. memzero_explicit(crypto_info, sizeof(union tls_crypto_context));
  431. out:
  432. return rc;
  433. }
  434. static int do_tls_setsockopt(struct sock *sk, int optname,
  435. char __user *optval, unsigned int optlen)
  436. {
  437. int rc = 0;
  438. switch (optname) {
  439. case TLS_TX:
  440. case TLS_RX:
  441. lock_sock(sk);
  442. rc = do_tls_setsockopt_conf(sk, optval, optlen,
  443. optname == TLS_TX);
  444. release_sock(sk);
  445. break;
  446. default:
  447. rc = -ENOPROTOOPT;
  448. break;
  449. }
  450. return rc;
  451. }
  452. static int tls_setsockopt(struct sock *sk, int level, int optname,
  453. char __user *optval, unsigned int optlen)
  454. {
  455. struct tls_context *ctx = tls_get_ctx(sk);
  456. if (level != SOL_TLS)
  457. return ctx->setsockopt(sk, level, optname, optval, optlen);
  458. return do_tls_setsockopt(sk, optname, optval, optlen);
  459. }
  460. static struct tls_context *create_ctx(struct sock *sk)
  461. {
  462. struct inet_connection_sock *icsk = inet_csk(sk);
  463. struct tls_context *ctx;
  464. ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
  465. if (!ctx)
  466. return NULL;
  467. icsk->icsk_ulp_data = ctx;
  468. return ctx;
  469. }
  470. static int tls_hw_prot(struct sock *sk)
  471. {
  472. struct tls_context *ctx;
  473. struct tls_device *dev;
  474. int rc = 0;
  475. mutex_lock(&device_mutex);
  476. list_for_each_entry(dev, &device_list, dev_list) {
  477. if (dev->feature && dev->feature(dev)) {
  478. ctx = create_ctx(sk);
  479. if (!ctx)
  480. goto out;
  481. ctx->hash = sk->sk_prot->hash;
  482. ctx->unhash = sk->sk_prot->unhash;
  483. ctx->sk_proto_close = sk->sk_prot->close;
  484. ctx->rx_conf = TLS_HW_RECORD;
  485. ctx->tx_conf = TLS_HW_RECORD;
  486. update_sk_prot(sk, ctx);
  487. rc = 1;
  488. break;
  489. }
  490. }
  491. out:
  492. mutex_unlock(&device_mutex);
  493. return rc;
  494. }
  495. static void tls_hw_unhash(struct sock *sk)
  496. {
  497. struct tls_context *ctx = tls_get_ctx(sk);
  498. struct tls_device *dev;
  499. mutex_lock(&device_mutex);
  500. list_for_each_entry(dev, &device_list, dev_list) {
  501. if (dev->unhash)
  502. dev->unhash(dev, sk);
  503. }
  504. mutex_unlock(&device_mutex);
  505. ctx->unhash(sk);
  506. }
  507. static int tls_hw_hash(struct sock *sk)
  508. {
  509. struct tls_context *ctx = tls_get_ctx(sk);
  510. struct tls_device *dev;
  511. int err;
  512. err = ctx->hash(sk);
  513. mutex_lock(&device_mutex);
  514. list_for_each_entry(dev, &device_list, dev_list) {
  515. if (dev->hash)
  516. err |= dev->hash(dev, sk);
  517. }
  518. mutex_unlock(&device_mutex);
  519. if (err)
  520. tls_hw_unhash(sk);
  521. return err;
  522. }
  523. static void build_protos(struct proto prot[TLS_NUM_CONFIG][TLS_NUM_CONFIG],
  524. struct proto *base)
  525. {
  526. prot[TLS_BASE][TLS_BASE] = *base;
  527. prot[TLS_BASE][TLS_BASE].setsockopt = tls_setsockopt;
  528. prot[TLS_BASE][TLS_BASE].getsockopt = tls_getsockopt;
  529. prot[TLS_BASE][TLS_BASE].close = tls_sk_proto_close;
  530. prot[TLS_SW][TLS_BASE] = prot[TLS_BASE][TLS_BASE];
  531. prot[TLS_SW][TLS_BASE].sendmsg = tls_sw_sendmsg;
  532. prot[TLS_SW][TLS_BASE].sendpage = tls_sw_sendpage;
  533. prot[TLS_BASE][TLS_SW] = prot[TLS_BASE][TLS_BASE];
  534. prot[TLS_BASE][TLS_SW].recvmsg = tls_sw_recvmsg;
  535. prot[TLS_BASE][TLS_SW].close = tls_sk_proto_close;
  536. prot[TLS_SW][TLS_SW] = prot[TLS_SW][TLS_BASE];
  537. prot[TLS_SW][TLS_SW].recvmsg = tls_sw_recvmsg;
  538. prot[TLS_SW][TLS_SW].close = tls_sk_proto_close;
  539. #ifdef CONFIG_TLS_DEVICE
  540. prot[TLS_HW][TLS_BASE] = prot[TLS_BASE][TLS_BASE];
  541. prot[TLS_HW][TLS_BASE].sendmsg = tls_device_sendmsg;
  542. prot[TLS_HW][TLS_BASE].sendpage = tls_device_sendpage;
  543. prot[TLS_HW][TLS_SW] = prot[TLS_BASE][TLS_SW];
  544. prot[TLS_HW][TLS_SW].sendmsg = tls_device_sendmsg;
  545. prot[TLS_HW][TLS_SW].sendpage = tls_device_sendpage;
  546. prot[TLS_BASE][TLS_HW] = prot[TLS_BASE][TLS_SW];
  547. prot[TLS_SW][TLS_HW] = prot[TLS_SW][TLS_SW];
  548. prot[TLS_HW][TLS_HW] = prot[TLS_HW][TLS_SW];
  549. #endif
  550. prot[TLS_HW_RECORD][TLS_HW_RECORD] = *base;
  551. prot[TLS_HW_RECORD][TLS_HW_RECORD].hash = tls_hw_hash;
  552. prot[TLS_HW_RECORD][TLS_HW_RECORD].unhash = tls_hw_unhash;
  553. prot[TLS_HW_RECORD][TLS_HW_RECORD].close = tls_sk_proto_close;
  554. }
  555. static int tls_init(struct sock *sk)
  556. {
  557. int ip_ver = sk->sk_family == AF_INET6 ? TLSV6 : TLSV4;
  558. struct tls_context *ctx;
  559. int rc = 0;
  560. if (tls_hw_prot(sk))
  561. goto out;
  562. /* The TLS ulp is currently supported only for TCP sockets
  563. * in ESTABLISHED state.
  564. * Supporting sockets in LISTEN state will require us
  565. * to modify the accept implementation to clone rather then
  566. * share the ulp context.
  567. */
  568. if (sk->sk_state != TCP_ESTABLISHED)
  569. return -ENOTSUPP;
  570. /* allocate tls context */
  571. ctx = create_ctx(sk);
  572. if (!ctx) {
  573. rc = -ENOMEM;
  574. goto out;
  575. }
  576. ctx->setsockopt = sk->sk_prot->setsockopt;
  577. ctx->getsockopt = sk->sk_prot->getsockopt;
  578. ctx->sk_proto_close = sk->sk_prot->close;
  579. /* Build IPv6 TLS whenever the address of tcpv6 _prot changes */
  580. if (ip_ver == TLSV6 &&
  581. unlikely(sk->sk_prot != smp_load_acquire(&saved_tcpv6_prot))) {
  582. mutex_lock(&tcpv6_prot_mutex);
  583. if (likely(sk->sk_prot != saved_tcpv6_prot)) {
  584. build_protos(tls_prots[TLSV6], sk->sk_prot);
  585. smp_store_release(&saved_tcpv6_prot, sk->sk_prot);
  586. }
  587. mutex_unlock(&tcpv6_prot_mutex);
  588. }
  589. ctx->tx_conf = TLS_BASE;
  590. ctx->rx_conf = TLS_BASE;
  591. update_sk_prot(sk, ctx);
  592. out:
  593. return rc;
  594. }
  595. void tls_register_device(struct tls_device *device)
  596. {
  597. mutex_lock(&device_mutex);
  598. list_add_tail(&device->dev_list, &device_list);
  599. mutex_unlock(&device_mutex);
  600. }
  601. EXPORT_SYMBOL(tls_register_device);
  602. void tls_unregister_device(struct tls_device *device)
  603. {
  604. mutex_lock(&device_mutex);
  605. list_del(&device->dev_list);
  606. mutex_unlock(&device_mutex);
  607. }
  608. EXPORT_SYMBOL(tls_unregister_device);
  609. static struct tcp_ulp_ops tcp_tls_ulp_ops __read_mostly = {
  610. .name = "tls",
  611. .uid = TCP_ULP_TLS,
  612. .user_visible = true,
  613. .owner = THIS_MODULE,
  614. .init = tls_init,
  615. };
  616. static int __init tls_register(void)
  617. {
  618. build_protos(tls_prots[TLSV4], &tcp_prot);
  619. tls_sw_proto_ops = inet_stream_ops;
  620. tls_sw_proto_ops.poll = tls_sw_poll;
  621. tls_sw_proto_ops.splice_read = tls_sw_splice_read;
  622. #ifdef CONFIG_TLS_DEVICE
  623. tls_device_init();
  624. #endif
  625. tcp_register_ulp(&tcp_tls_ulp_ops);
  626. return 0;
  627. }
  628. static void __exit tls_unregister(void)
  629. {
  630. tcp_unregister_ulp(&tcp_tls_ulp_ops);
  631. #ifdef CONFIG_TLS_DEVICE
  632. tls_device_cleanup();
  633. #endif
  634. }
  635. module_init(tls_register);
  636. module_exit(tls_unregister);