tls_main.c 17 KB

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