tls_main.c 17 KB

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