tls_main.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167
  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 <linux/inet_diag.h>
  41. #include <net/snmp.h>
  42. #include <net/tls.h>
  43. #include <net/tls_toe.h>
  44. #include "tls.h"
  45. MODULE_AUTHOR("Mellanox Technologies");
  46. MODULE_DESCRIPTION("Transport Layer Security Support");
  47. MODULE_LICENSE("Dual BSD/GPL");
  48. MODULE_ALIAS_TCP_ULP("tls");
  49. enum {
  50. TLSV4,
  51. TLSV6,
  52. TLS_NUM_PROTS,
  53. };
  54. #define CHECK_CIPHER_DESC(cipher,ci) \
  55. static_assert(cipher ## _IV_SIZE <= TLS_MAX_IV_SIZE); \
  56. static_assert(cipher ## _SALT_SIZE <= TLS_MAX_SALT_SIZE); \
  57. static_assert(cipher ## _REC_SEQ_SIZE <= TLS_MAX_REC_SEQ_SIZE); \
  58. static_assert(cipher ## _TAG_SIZE == TLS_TAG_SIZE); \
  59. static_assert(sizeof_field(struct ci, iv) == cipher ## _IV_SIZE); \
  60. static_assert(sizeof_field(struct ci, key) == cipher ## _KEY_SIZE); \
  61. static_assert(sizeof_field(struct ci, salt) == cipher ## _SALT_SIZE); \
  62. static_assert(sizeof_field(struct ci, rec_seq) == cipher ## _REC_SEQ_SIZE);
  63. #define __CIPHER_DESC(ci) \
  64. .iv_offset = offsetof(struct ci, iv), \
  65. .key_offset = offsetof(struct ci, key), \
  66. .salt_offset = offsetof(struct ci, salt), \
  67. .rec_seq_offset = offsetof(struct ci, rec_seq), \
  68. .crypto_info = sizeof(struct ci)
  69. #define CIPHER_DESC(cipher,ci,algname,_offloadable) [cipher - TLS_CIPHER_MIN] = { \
  70. .nonce = cipher ## _IV_SIZE, \
  71. .iv = cipher ## _IV_SIZE, \
  72. .key = cipher ## _KEY_SIZE, \
  73. .salt = cipher ## _SALT_SIZE, \
  74. .tag = cipher ## _TAG_SIZE, \
  75. .rec_seq = cipher ## _REC_SEQ_SIZE, \
  76. .cipher_name = algname, \
  77. .offloadable = _offloadable, \
  78. __CIPHER_DESC(ci), \
  79. }
  80. #define CIPHER_DESC_NONCE0(cipher,ci,algname,_offloadable) [cipher - TLS_CIPHER_MIN] = { \
  81. .nonce = 0, \
  82. .iv = cipher ## _IV_SIZE, \
  83. .key = cipher ## _KEY_SIZE, \
  84. .salt = cipher ## _SALT_SIZE, \
  85. .tag = cipher ## _TAG_SIZE, \
  86. .rec_seq = cipher ## _REC_SEQ_SIZE, \
  87. .cipher_name = algname, \
  88. .offloadable = _offloadable, \
  89. __CIPHER_DESC(ci), \
  90. }
  91. const struct tls_cipher_desc tls_cipher_desc[TLS_CIPHER_MAX + 1 - TLS_CIPHER_MIN] = {
  92. CIPHER_DESC(TLS_CIPHER_AES_GCM_128, tls12_crypto_info_aes_gcm_128, "gcm(aes)", true),
  93. CIPHER_DESC(TLS_CIPHER_AES_GCM_256, tls12_crypto_info_aes_gcm_256, "gcm(aes)", true),
  94. CIPHER_DESC(TLS_CIPHER_AES_CCM_128, tls12_crypto_info_aes_ccm_128, "ccm(aes)", false),
  95. CIPHER_DESC_NONCE0(TLS_CIPHER_CHACHA20_POLY1305, tls12_crypto_info_chacha20_poly1305, "rfc7539(chacha20,poly1305)", false),
  96. CIPHER_DESC(TLS_CIPHER_SM4_GCM, tls12_crypto_info_sm4_gcm, "gcm(sm4)", false),
  97. CIPHER_DESC(TLS_CIPHER_SM4_CCM, tls12_crypto_info_sm4_ccm, "ccm(sm4)", false),
  98. CIPHER_DESC(TLS_CIPHER_ARIA_GCM_128, tls12_crypto_info_aria_gcm_128, "gcm(aria)", false),
  99. CIPHER_DESC(TLS_CIPHER_ARIA_GCM_256, tls12_crypto_info_aria_gcm_256, "gcm(aria)", false),
  100. };
  101. CHECK_CIPHER_DESC(TLS_CIPHER_AES_GCM_128, tls12_crypto_info_aes_gcm_128);
  102. CHECK_CIPHER_DESC(TLS_CIPHER_AES_GCM_256, tls12_crypto_info_aes_gcm_256);
  103. CHECK_CIPHER_DESC(TLS_CIPHER_AES_CCM_128, tls12_crypto_info_aes_ccm_128);
  104. CHECK_CIPHER_DESC(TLS_CIPHER_CHACHA20_POLY1305, tls12_crypto_info_chacha20_poly1305);
  105. CHECK_CIPHER_DESC(TLS_CIPHER_SM4_GCM, tls12_crypto_info_sm4_gcm);
  106. CHECK_CIPHER_DESC(TLS_CIPHER_SM4_CCM, tls12_crypto_info_sm4_ccm);
  107. CHECK_CIPHER_DESC(TLS_CIPHER_ARIA_GCM_128, tls12_crypto_info_aria_gcm_128);
  108. CHECK_CIPHER_DESC(TLS_CIPHER_ARIA_GCM_256, tls12_crypto_info_aria_gcm_256);
  109. static const struct proto *saved_tcpv6_prot;
  110. static DEFINE_MUTEX(tcpv6_prot_mutex);
  111. static const struct proto *saved_tcpv4_prot;
  112. static DEFINE_MUTEX(tcpv4_prot_mutex);
  113. static struct proto tls_prots[TLS_NUM_PROTS][TLS_NUM_CONFIG][TLS_NUM_CONFIG];
  114. static struct proto_ops tls_proto_ops[TLS_NUM_PROTS][TLS_NUM_CONFIG][TLS_NUM_CONFIG];
  115. static void build_protos(struct proto prot[TLS_NUM_CONFIG][TLS_NUM_CONFIG],
  116. const struct proto *base);
  117. void update_sk_prot(struct sock *sk, struct tls_context *ctx)
  118. {
  119. int ip_ver = sk->sk_family == AF_INET6 ? TLSV6 : TLSV4;
  120. WRITE_ONCE(sk->sk_prot,
  121. &tls_prots[ip_ver][ctx->tx_conf][ctx->rx_conf]);
  122. WRITE_ONCE(sk->sk_socket->ops,
  123. &tls_proto_ops[ip_ver][ctx->tx_conf][ctx->rx_conf]);
  124. }
  125. int wait_on_pending_writer(struct sock *sk, long *timeo)
  126. {
  127. DEFINE_WAIT_FUNC(wait, woken_wake_function);
  128. int ret, rc = 0;
  129. add_wait_queue(sk_sleep(sk), &wait);
  130. while (1) {
  131. if (!*timeo) {
  132. rc = -EAGAIN;
  133. break;
  134. }
  135. if (signal_pending(current)) {
  136. rc = sock_intr_errno(*timeo);
  137. break;
  138. }
  139. ret = sk_wait_event(sk, timeo,
  140. !READ_ONCE(sk->sk_write_pending), &wait);
  141. if (ret) {
  142. if (ret < 0)
  143. rc = ret;
  144. break;
  145. }
  146. }
  147. remove_wait_queue(sk_sleep(sk), &wait);
  148. return rc;
  149. }
  150. int tls_push_sg(struct sock *sk,
  151. struct tls_context *ctx,
  152. struct scatterlist *sg,
  153. u16 first_offset,
  154. int flags)
  155. {
  156. struct bio_vec bvec;
  157. struct msghdr msg = {
  158. .msg_flags = MSG_SPLICE_PAGES | flags,
  159. };
  160. int ret = 0;
  161. struct page *p;
  162. size_t size;
  163. int offset = first_offset;
  164. size = sg->length - offset;
  165. offset += sg->offset;
  166. ctx->splicing_pages = true;
  167. while (1) {
  168. /* is sending application-limited? */
  169. tcp_rate_check_app_limited(sk);
  170. p = sg_page(sg);
  171. retry:
  172. bvec_set_page(&bvec, p, size, offset);
  173. iov_iter_bvec(&msg.msg_iter, ITER_SOURCE, &bvec, 1, size);
  174. ret = tcp_sendmsg_locked(sk, &msg, size);
  175. if (ret != size) {
  176. if (ret > 0) {
  177. offset += ret;
  178. size -= ret;
  179. goto retry;
  180. }
  181. offset -= sg->offset;
  182. ctx->partially_sent_offset = offset;
  183. ctx->partially_sent_record = (void *)sg;
  184. ctx->splicing_pages = false;
  185. return ret;
  186. }
  187. put_page(p);
  188. sk_mem_uncharge(sk, sg->length);
  189. sg = sg_next(sg);
  190. if (!sg)
  191. break;
  192. offset = sg->offset;
  193. size = sg->length;
  194. }
  195. ctx->splicing_pages = false;
  196. return 0;
  197. }
  198. static int tls_handle_open_record(struct sock *sk, int flags)
  199. {
  200. struct tls_context *ctx = tls_get_ctx(sk);
  201. if (tls_is_pending_open_record(ctx))
  202. return ctx->push_pending_record(sk, flags);
  203. return 0;
  204. }
  205. int tls_process_cmsg(struct sock *sk, struct msghdr *msg,
  206. unsigned char *record_type)
  207. {
  208. struct cmsghdr *cmsg;
  209. int rc = -EINVAL;
  210. for_each_cmsghdr(cmsg, msg) {
  211. if (!CMSG_OK(msg, cmsg))
  212. return -EINVAL;
  213. if (cmsg->cmsg_level != SOL_TLS)
  214. continue;
  215. switch (cmsg->cmsg_type) {
  216. case TLS_SET_RECORD_TYPE:
  217. if (cmsg->cmsg_len < CMSG_LEN(sizeof(*record_type)))
  218. return -EINVAL;
  219. if (msg->msg_flags & MSG_MORE)
  220. return -EINVAL;
  221. *record_type = *(unsigned char *)CMSG_DATA(cmsg);
  222. rc = tls_handle_open_record(sk, msg->msg_flags);
  223. break;
  224. default:
  225. return -EINVAL;
  226. }
  227. }
  228. return rc;
  229. }
  230. int tls_push_partial_record(struct sock *sk, struct tls_context *ctx,
  231. int flags)
  232. {
  233. struct scatterlist *sg;
  234. u16 offset;
  235. sg = ctx->partially_sent_record;
  236. offset = ctx->partially_sent_offset;
  237. ctx->partially_sent_record = NULL;
  238. return tls_push_sg(sk, ctx, sg, offset, flags);
  239. }
  240. void tls_free_partial_record(struct sock *sk, struct tls_context *ctx)
  241. {
  242. struct scatterlist *sg;
  243. for (sg = ctx->partially_sent_record; sg; sg = sg_next(sg)) {
  244. put_page(sg_page(sg));
  245. sk_mem_uncharge(sk, sg->length);
  246. }
  247. ctx->partially_sent_record = NULL;
  248. }
  249. static void tls_write_space(struct sock *sk)
  250. {
  251. struct tls_context *ctx = tls_get_ctx(sk);
  252. /* If splicing_pages call lower protocol write space handler
  253. * to ensure we wake up any waiting operations there. For example
  254. * if splicing pages where to call sk_wait_event.
  255. */
  256. if (ctx->splicing_pages) {
  257. ctx->sk_write_space(sk);
  258. return;
  259. }
  260. #ifdef CONFIG_TLS_DEVICE
  261. if (ctx->tx_conf == TLS_HW)
  262. tls_device_write_space(sk, ctx);
  263. else
  264. #endif
  265. tls_sw_write_space(sk, ctx);
  266. ctx->sk_write_space(sk);
  267. }
  268. /**
  269. * tls_ctx_free() - free TLS ULP context
  270. * @sk: socket to with @ctx is attached
  271. * @ctx: TLS context structure
  272. *
  273. * Free TLS context. If @sk is %NULL caller guarantees that the socket
  274. * to which @ctx was attached has no outstanding references.
  275. */
  276. void tls_ctx_free(struct sock *sk, struct tls_context *ctx)
  277. {
  278. if (!ctx)
  279. return;
  280. memzero_explicit(&ctx->crypto_send, sizeof(ctx->crypto_send));
  281. memzero_explicit(&ctx->crypto_recv, sizeof(ctx->crypto_recv));
  282. mutex_destroy(&ctx->tx_lock);
  283. if (sk)
  284. kfree_rcu(ctx, rcu);
  285. else
  286. kfree(ctx);
  287. }
  288. static void tls_sk_proto_cleanup(struct sock *sk,
  289. struct tls_context *ctx, long timeo)
  290. {
  291. if (unlikely(sk->sk_write_pending) &&
  292. !wait_on_pending_writer(sk, &timeo))
  293. tls_handle_open_record(sk, 0);
  294. /* We need these for tls_sw_fallback handling of other packets */
  295. if (ctx->tx_conf == TLS_SW) {
  296. tls_sw_release_resources_tx(sk);
  297. TLS_DEC_STATS(sock_net(sk), LINUX_MIB_TLSCURRTXSW);
  298. } else if (ctx->tx_conf == TLS_HW) {
  299. tls_device_free_resources_tx(sk);
  300. TLS_DEC_STATS(sock_net(sk), LINUX_MIB_TLSCURRTXDEVICE);
  301. }
  302. if (ctx->rx_conf == TLS_SW) {
  303. tls_sw_release_resources_rx(sk);
  304. TLS_DEC_STATS(sock_net(sk), LINUX_MIB_TLSCURRRXSW);
  305. } else if (ctx->rx_conf == TLS_HW) {
  306. tls_device_offload_cleanup_rx(sk);
  307. TLS_DEC_STATS(sock_net(sk), LINUX_MIB_TLSCURRRXDEVICE);
  308. }
  309. }
  310. static void tls_sk_proto_close(struct sock *sk, long timeout)
  311. {
  312. struct inet_connection_sock *icsk = inet_csk(sk);
  313. struct tls_context *ctx = tls_get_ctx(sk);
  314. long timeo = sock_sndtimeo(sk, 0);
  315. bool free_ctx;
  316. if (ctx->tx_conf == TLS_SW)
  317. tls_sw_cancel_work_tx(ctx);
  318. lock_sock(sk);
  319. free_ctx = ctx->tx_conf != TLS_HW && ctx->rx_conf != TLS_HW;
  320. if (ctx->tx_conf != TLS_BASE || ctx->rx_conf != TLS_BASE)
  321. tls_sk_proto_cleanup(sk, ctx, timeo);
  322. write_lock_bh(&sk->sk_callback_lock);
  323. if (free_ctx)
  324. rcu_assign_pointer(icsk->icsk_ulp_data, NULL);
  325. WRITE_ONCE(sk->sk_prot, ctx->sk_proto);
  326. if (sk->sk_write_space == tls_write_space)
  327. sk->sk_write_space = ctx->sk_write_space;
  328. write_unlock_bh(&sk->sk_callback_lock);
  329. release_sock(sk);
  330. if (ctx->tx_conf == TLS_SW)
  331. tls_sw_free_ctx_tx(ctx);
  332. if (ctx->rx_conf == TLS_SW || ctx->rx_conf == TLS_HW)
  333. tls_sw_strparser_done(ctx);
  334. if (ctx->rx_conf == TLS_SW)
  335. tls_sw_free_ctx_rx(ctx);
  336. ctx->sk_proto->close(sk, timeout);
  337. if (free_ctx)
  338. tls_ctx_free(sk, ctx);
  339. }
  340. static __poll_t tls_sk_poll(struct file *file, struct socket *sock,
  341. struct poll_table_struct *wait)
  342. {
  343. struct tls_sw_context_rx *ctx;
  344. struct tls_context *tls_ctx;
  345. struct sock *sk = sock->sk;
  346. struct sk_psock *psock;
  347. __poll_t mask = 0;
  348. u8 shutdown;
  349. int state;
  350. mask = tcp_poll(file, sock, wait);
  351. state = inet_sk_state_load(sk);
  352. shutdown = READ_ONCE(sk->sk_shutdown);
  353. if (unlikely(state != TCP_ESTABLISHED || shutdown & RCV_SHUTDOWN))
  354. return mask;
  355. tls_ctx = tls_get_ctx(sk);
  356. ctx = tls_sw_ctx_rx(tls_ctx);
  357. psock = sk_psock_get(sk);
  358. if (skb_queue_empty_lockless(&ctx->rx_list) &&
  359. !tls_strp_msg_ready(ctx) &&
  360. sk_psock_queue_empty(psock))
  361. mask &= ~(EPOLLIN | EPOLLRDNORM);
  362. if (psock)
  363. sk_psock_put(sk, psock);
  364. return mask;
  365. }
  366. static int do_tls_getsockopt_conf(struct sock *sk, char __user *optval,
  367. int __user *optlen, int tx)
  368. {
  369. int rc = 0;
  370. const struct tls_cipher_desc *cipher_desc;
  371. struct tls_context *ctx = tls_get_ctx(sk);
  372. struct tls_crypto_info *crypto_info;
  373. struct cipher_context *cctx;
  374. int len;
  375. if (get_user(len, optlen))
  376. return -EFAULT;
  377. if (!optval || (len < sizeof(*crypto_info))) {
  378. rc = -EINVAL;
  379. goto out;
  380. }
  381. if (!ctx) {
  382. rc = -EBUSY;
  383. goto out;
  384. }
  385. /* get user crypto info */
  386. if (tx) {
  387. crypto_info = &ctx->crypto_send.info;
  388. cctx = &ctx->tx;
  389. } else {
  390. crypto_info = &ctx->crypto_recv.info;
  391. cctx = &ctx->rx;
  392. }
  393. if (!TLS_CRYPTO_INFO_READY(crypto_info)) {
  394. rc = -EBUSY;
  395. goto out;
  396. }
  397. if (len == sizeof(*crypto_info)) {
  398. if (copy_to_user(optval, crypto_info, sizeof(*crypto_info)))
  399. rc = -EFAULT;
  400. goto out;
  401. }
  402. cipher_desc = get_cipher_desc(crypto_info->cipher_type);
  403. if (!cipher_desc || len != cipher_desc->crypto_info) {
  404. rc = -EINVAL;
  405. goto out;
  406. }
  407. memcpy(crypto_info_iv(crypto_info, cipher_desc),
  408. cctx->iv + cipher_desc->salt, cipher_desc->iv);
  409. memcpy(crypto_info_rec_seq(crypto_info, cipher_desc),
  410. cctx->rec_seq, cipher_desc->rec_seq);
  411. if (copy_to_user(optval, crypto_info, cipher_desc->crypto_info))
  412. rc = -EFAULT;
  413. out:
  414. return rc;
  415. }
  416. static int do_tls_getsockopt_tx_zc(struct sock *sk, char __user *optval,
  417. int __user *optlen)
  418. {
  419. struct tls_context *ctx = tls_get_ctx(sk);
  420. unsigned int value;
  421. int len;
  422. if (get_user(len, optlen))
  423. return -EFAULT;
  424. if (len != sizeof(value))
  425. return -EINVAL;
  426. value = ctx->zerocopy_sendfile;
  427. if (copy_to_user(optval, &value, sizeof(value)))
  428. return -EFAULT;
  429. return 0;
  430. }
  431. static int do_tls_getsockopt_no_pad(struct sock *sk, char __user *optval,
  432. int __user *optlen)
  433. {
  434. struct tls_context *ctx = tls_get_ctx(sk);
  435. int value, len;
  436. if (ctx->prot_info.version != TLS_1_3_VERSION)
  437. return -EINVAL;
  438. if (get_user(len, optlen))
  439. return -EFAULT;
  440. if (len < sizeof(value))
  441. return -EINVAL;
  442. value = -EINVAL;
  443. if (ctx->rx_conf == TLS_SW || ctx->rx_conf == TLS_HW)
  444. value = ctx->rx_no_pad;
  445. if (value < 0)
  446. return value;
  447. if (put_user(sizeof(value), optlen))
  448. return -EFAULT;
  449. if (copy_to_user(optval, &value, sizeof(value)))
  450. return -EFAULT;
  451. return 0;
  452. }
  453. static int do_tls_getsockopt(struct sock *sk, int optname,
  454. char __user *optval, int __user *optlen)
  455. {
  456. int rc = 0;
  457. lock_sock(sk);
  458. switch (optname) {
  459. case TLS_TX:
  460. case TLS_RX:
  461. rc = do_tls_getsockopt_conf(sk, optval, optlen,
  462. optname == TLS_TX);
  463. break;
  464. case TLS_TX_ZEROCOPY_RO:
  465. rc = do_tls_getsockopt_tx_zc(sk, optval, optlen);
  466. break;
  467. case TLS_RX_EXPECT_NO_PAD:
  468. rc = do_tls_getsockopt_no_pad(sk, optval, optlen);
  469. break;
  470. default:
  471. rc = -ENOPROTOOPT;
  472. break;
  473. }
  474. release_sock(sk);
  475. return rc;
  476. }
  477. static int tls_getsockopt(struct sock *sk, int level, int optname,
  478. char __user *optval, int __user *optlen)
  479. {
  480. struct tls_context *ctx = tls_get_ctx(sk);
  481. if (level != SOL_TLS)
  482. return ctx->sk_proto->getsockopt(sk, level,
  483. optname, optval, optlen);
  484. return do_tls_getsockopt(sk, optname, optval, optlen);
  485. }
  486. static int validate_crypto_info(const struct tls_crypto_info *crypto_info,
  487. const struct tls_crypto_info *alt_crypto_info)
  488. {
  489. if (crypto_info->version != TLS_1_2_VERSION &&
  490. crypto_info->version != TLS_1_3_VERSION)
  491. return -EINVAL;
  492. switch (crypto_info->cipher_type) {
  493. case TLS_CIPHER_ARIA_GCM_128:
  494. case TLS_CIPHER_ARIA_GCM_256:
  495. if (crypto_info->version != TLS_1_2_VERSION)
  496. return -EINVAL;
  497. break;
  498. }
  499. /* Ensure that TLS version and ciphers are same in both directions */
  500. if (TLS_CRYPTO_INFO_READY(alt_crypto_info)) {
  501. if (alt_crypto_info->version != crypto_info->version ||
  502. alt_crypto_info->cipher_type != crypto_info->cipher_type)
  503. return -EINVAL;
  504. }
  505. return 0;
  506. }
  507. static int do_tls_setsockopt_conf(struct sock *sk, sockptr_t optval,
  508. unsigned int optlen, int tx)
  509. {
  510. struct tls_crypto_info *crypto_info;
  511. struct tls_crypto_info *alt_crypto_info;
  512. struct tls_context *ctx = tls_get_ctx(sk);
  513. const struct tls_cipher_desc *cipher_desc;
  514. union tls_crypto_context *crypto_ctx;
  515. int rc = 0;
  516. int conf;
  517. if (sockptr_is_null(optval) || (optlen < sizeof(*crypto_info)))
  518. return -EINVAL;
  519. if (tx) {
  520. crypto_ctx = &ctx->crypto_send;
  521. alt_crypto_info = &ctx->crypto_recv.info;
  522. } else {
  523. crypto_ctx = &ctx->crypto_recv;
  524. alt_crypto_info = &ctx->crypto_send.info;
  525. }
  526. crypto_info = &crypto_ctx->info;
  527. /* Currently we don't support set crypto info more than one time */
  528. if (TLS_CRYPTO_INFO_READY(crypto_info))
  529. return -EBUSY;
  530. rc = copy_from_sockptr(crypto_info, optval, sizeof(*crypto_info));
  531. if (rc) {
  532. rc = -EFAULT;
  533. goto err_crypto_info;
  534. }
  535. rc = validate_crypto_info(crypto_info, alt_crypto_info);
  536. if (rc)
  537. goto err_crypto_info;
  538. cipher_desc = get_cipher_desc(crypto_info->cipher_type);
  539. if (!cipher_desc) {
  540. rc = -EINVAL;
  541. goto err_crypto_info;
  542. }
  543. if (optlen != cipher_desc->crypto_info) {
  544. rc = -EINVAL;
  545. goto err_crypto_info;
  546. }
  547. rc = copy_from_sockptr_offset(crypto_info + 1, optval,
  548. sizeof(*crypto_info),
  549. optlen - sizeof(*crypto_info));
  550. if (rc) {
  551. rc = -EFAULT;
  552. goto err_crypto_info;
  553. }
  554. if (tx) {
  555. rc = tls_set_device_offload(sk);
  556. conf = TLS_HW;
  557. if (!rc) {
  558. TLS_INC_STATS(sock_net(sk), LINUX_MIB_TLSTXDEVICE);
  559. TLS_INC_STATS(sock_net(sk), LINUX_MIB_TLSCURRTXDEVICE);
  560. } else {
  561. rc = tls_set_sw_offload(sk, 1);
  562. if (rc)
  563. goto err_crypto_info;
  564. TLS_INC_STATS(sock_net(sk), LINUX_MIB_TLSTXSW);
  565. TLS_INC_STATS(sock_net(sk), LINUX_MIB_TLSCURRTXSW);
  566. conf = TLS_SW;
  567. }
  568. } else {
  569. rc = tls_set_device_offload_rx(sk, ctx);
  570. conf = TLS_HW;
  571. if (!rc) {
  572. TLS_INC_STATS(sock_net(sk), LINUX_MIB_TLSRXDEVICE);
  573. TLS_INC_STATS(sock_net(sk), LINUX_MIB_TLSCURRRXDEVICE);
  574. } else {
  575. rc = tls_set_sw_offload(sk, 0);
  576. if (rc)
  577. goto err_crypto_info;
  578. TLS_INC_STATS(sock_net(sk), LINUX_MIB_TLSRXSW);
  579. TLS_INC_STATS(sock_net(sk), LINUX_MIB_TLSCURRRXSW);
  580. conf = TLS_SW;
  581. }
  582. tls_sw_strparser_arm(sk, ctx);
  583. }
  584. if (tx)
  585. ctx->tx_conf = conf;
  586. else
  587. ctx->rx_conf = conf;
  588. update_sk_prot(sk, ctx);
  589. if (tx) {
  590. ctx->sk_write_space = sk->sk_write_space;
  591. sk->sk_write_space = tls_write_space;
  592. } else {
  593. struct tls_sw_context_rx *rx_ctx = tls_sw_ctx_rx(ctx);
  594. tls_strp_check_rcv(&rx_ctx->strp);
  595. }
  596. return 0;
  597. err_crypto_info:
  598. memzero_explicit(crypto_ctx, sizeof(*crypto_ctx));
  599. return rc;
  600. }
  601. static int do_tls_setsockopt_tx_zc(struct sock *sk, sockptr_t optval,
  602. unsigned int optlen)
  603. {
  604. struct tls_context *ctx = tls_get_ctx(sk);
  605. unsigned int value;
  606. if (sockptr_is_null(optval) || optlen != sizeof(value))
  607. return -EINVAL;
  608. if (copy_from_sockptr(&value, optval, sizeof(value)))
  609. return -EFAULT;
  610. if (value > 1)
  611. return -EINVAL;
  612. ctx->zerocopy_sendfile = value;
  613. return 0;
  614. }
  615. static int do_tls_setsockopt_no_pad(struct sock *sk, sockptr_t optval,
  616. unsigned int optlen)
  617. {
  618. struct tls_context *ctx = tls_get_ctx(sk);
  619. u32 val;
  620. int rc;
  621. if (ctx->prot_info.version != TLS_1_3_VERSION ||
  622. sockptr_is_null(optval) || optlen < sizeof(val))
  623. return -EINVAL;
  624. rc = copy_from_sockptr(&val, optval, sizeof(val));
  625. if (rc)
  626. return -EFAULT;
  627. if (val > 1)
  628. return -EINVAL;
  629. rc = check_zeroed_sockptr(optval, sizeof(val), optlen - sizeof(val));
  630. if (rc < 1)
  631. return rc == 0 ? -EINVAL : rc;
  632. lock_sock(sk);
  633. rc = -EINVAL;
  634. if (ctx->rx_conf == TLS_SW || ctx->rx_conf == TLS_HW) {
  635. ctx->rx_no_pad = val;
  636. tls_update_rx_zc_capable(ctx);
  637. rc = 0;
  638. }
  639. release_sock(sk);
  640. return rc;
  641. }
  642. static int do_tls_setsockopt(struct sock *sk, int optname, sockptr_t optval,
  643. unsigned int optlen)
  644. {
  645. int rc = 0;
  646. switch (optname) {
  647. case TLS_TX:
  648. case TLS_RX:
  649. lock_sock(sk);
  650. rc = do_tls_setsockopt_conf(sk, optval, optlen,
  651. optname == TLS_TX);
  652. release_sock(sk);
  653. break;
  654. case TLS_TX_ZEROCOPY_RO:
  655. lock_sock(sk);
  656. rc = do_tls_setsockopt_tx_zc(sk, optval, optlen);
  657. release_sock(sk);
  658. break;
  659. case TLS_RX_EXPECT_NO_PAD:
  660. rc = do_tls_setsockopt_no_pad(sk, optval, optlen);
  661. break;
  662. default:
  663. rc = -ENOPROTOOPT;
  664. break;
  665. }
  666. return rc;
  667. }
  668. static int tls_setsockopt(struct sock *sk, int level, int optname,
  669. sockptr_t optval, unsigned int optlen)
  670. {
  671. struct tls_context *ctx = tls_get_ctx(sk);
  672. if (level != SOL_TLS)
  673. return ctx->sk_proto->setsockopt(sk, level, optname, optval,
  674. optlen);
  675. return do_tls_setsockopt(sk, optname, optval, optlen);
  676. }
  677. static int tls_disconnect(struct sock *sk, int flags)
  678. {
  679. return -EOPNOTSUPP;
  680. }
  681. struct tls_context *tls_ctx_create(struct sock *sk)
  682. {
  683. struct inet_connection_sock *icsk = inet_csk(sk);
  684. struct tls_context *ctx;
  685. ctx = kzalloc(sizeof(*ctx), GFP_ATOMIC);
  686. if (!ctx)
  687. return NULL;
  688. mutex_init(&ctx->tx_lock);
  689. ctx->sk_proto = READ_ONCE(sk->sk_prot);
  690. ctx->sk = sk;
  691. /* Release semantic of rcu_assign_pointer() ensures that
  692. * ctx->sk_proto is visible before changing sk->sk_prot in
  693. * update_sk_prot(), and prevents reading uninitialized value in
  694. * tls_{getsockopt, setsockopt}. Note that we do not need a
  695. * read barrier in tls_{getsockopt,setsockopt} as there is an
  696. * address dependency between sk->sk_proto->{getsockopt,setsockopt}
  697. * and ctx->sk_proto.
  698. */
  699. rcu_assign_pointer(icsk->icsk_ulp_data, ctx);
  700. return ctx;
  701. }
  702. static void build_proto_ops(struct proto_ops ops[TLS_NUM_CONFIG][TLS_NUM_CONFIG],
  703. const struct proto_ops *base)
  704. {
  705. ops[TLS_BASE][TLS_BASE] = *base;
  706. ops[TLS_SW ][TLS_BASE] = ops[TLS_BASE][TLS_BASE];
  707. ops[TLS_SW ][TLS_BASE].splice_eof = tls_sw_splice_eof;
  708. ops[TLS_BASE][TLS_SW ] = ops[TLS_BASE][TLS_BASE];
  709. ops[TLS_BASE][TLS_SW ].splice_read = tls_sw_splice_read;
  710. ops[TLS_BASE][TLS_SW ].poll = tls_sk_poll;
  711. ops[TLS_BASE][TLS_SW ].read_sock = tls_sw_read_sock;
  712. ops[TLS_SW ][TLS_SW ] = ops[TLS_SW ][TLS_BASE];
  713. ops[TLS_SW ][TLS_SW ].splice_read = tls_sw_splice_read;
  714. ops[TLS_SW ][TLS_SW ].poll = tls_sk_poll;
  715. ops[TLS_SW ][TLS_SW ].read_sock = tls_sw_read_sock;
  716. #ifdef CONFIG_TLS_DEVICE
  717. ops[TLS_HW ][TLS_BASE] = ops[TLS_BASE][TLS_BASE];
  718. ops[TLS_HW ][TLS_SW ] = ops[TLS_BASE][TLS_SW ];
  719. ops[TLS_BASE][TLS_HW ] = ops[TLS_BASE][TLS_SW ];
  720. ops[TLS_SW ][TLS_HW ] = ops[TLS_SW ][TLS_SW ];
  721. ops[TLS_HW ][TLS_HW ] = ops[TLS_HW ][TLS_SW ];
  722. #endif
  723. #ifdef CONFIG_TLS_TOE
  724. ops[TLS_HW_RECORD][TLS_HW_RECORD] = *base;
  725. #endif
  726. }
  727. static void tls_build_proto(struct sock *sk)
  728. {
  729. int ip_ver = sk->sk_family == AF_INET6 ? TLSV6 : TLSV4;
  730. struct proto *prot = READ_ONCE(sk->sk_prot);
  731. /* Build IPv6 TLS whenever the address of tcpv6 _prot changes */
  732. if (ip_ver == TLSV6 &&
  733. unlikely(prot != smp_load_acquire(&saved_tcpv6_prot))) {
  734. mutex_lock(&tcpv6_prot_mutex);
  735. if (likely(prot != saved_tcpv6_prot)) {
  736. build_protos(tls_prots[TLSV6], prot);
  737. build_proto_ops(tls_proto_ops[TLSV6],
  738. sk->sk_socket->ops);
  739. smp_store_release(&saved_tcpv6_prot, prot);
  740. }
  741. mutex_unlock(&tcpv6_prot_mutex);
  742. }
  743. if (ip_ver == TLSV4 &&
  744. unlikely(prot != smp_load_acquire(&saved_tcpv4_prot))) {
  745. mutex_lock(&tcpv4_prot_mutex);
  746. if (likely(prot != saved_tcpv4_prot)) {
  747. build_protos(tls_prots[TLSV4], prot);
  748. build_proto_ops(tls_proto_ops[TLSV4],
  749. sk->sk_socket->ops);
  750. smp_store_release(&saved_tcpv4_prot, prot);
  751. }
  752. mutex_unlock(&tcpv4_prot_mutex);
  753. }
  754. }
  755. static void build_protos(struct proto prot[TLS_NUM_CONFIG][TLS_NUM_CONFIG],
  756. const struct proto *base)
  757. {
  758. prot[TLS_BASE][TLS_BASE] = *base;
  759. prot[TLS_BASE][TLS_BASE].setsockopt = tls_setsockopt;
  760. prot[TLS_BASE][TLS_BASE].getsockopt = tls_getsockopt;
  761. prot[TLS_BASE][TLS_BASE].disconnect = tls_disconnect;
  762. prot[TLS_BASE][TLS_BASE].close = tls_sk_proto_close;
  763. prot[TLS_SW][TLS_BASE] = prot[TLS_BASE][TLS_BASE];
  764. prot[TLS_SW][TLS_BASE].sendmsg = tls_sw_sendmsg;
  765. prot[TLS_SW][TLS_BASE].splice_eof = tls_sw_splice_eof;
  766. prot[TLS_BASE][TLS_SW] = prot[TLS_BASE][TLS_BASE];
  767. prot[TLS_BASE][TLS_SW].recvmsg = tls_sw_recvmsg;
  768. prot[TLS_BASE][TLS_SW].sock_is_readable = tls_sw_sock_is_readable;
  769. prot[TLS_BASE][TLS_SW].close = tls_sk_proto_close;
  770. prot[TLS_SW][TLS_SW] = prot[TLS_SW][TLS_BASE];
  771. prot[TLS_SW][TLS_SW].recvmsg = tls_sw_recvmsg;
  772. prot[TLS_SW][TLS_SW].sock_is_readable = tls_sw_sock_is_readable;
  773. prot[TLS_SW][TLS_SW].close = tls_sk_proto_close;
  774. #ifdef CONFIG_TLS_DEVICE
  775. prot[TLS_HW][TLS_BASE] = prot[TLS_BASE][TLS_BASE];
  776. prot[TLS_HW][TLS_BASE].sendmsg = tls_device_sendmsg;
  777. prot[TLS_HW][TLS_BASE].splice_eof = tls_device_splice_eof;
  778. prot[TLS_HW][TLS_SW] = prot[TLS_BASE][TLS_SW];
  779. prot[TLS_HW][TLS_SW].sendmsg = tls_device_sendmsg;
  780. prot[TLS_HW][TLS_SW].splice_eof = tls_device_splice_eof;
  781. prot[TLS_BASE][TLS_HW] = prot[TLS_BASE][TLS_SW];
  782. prot[TLS_SW][TLS_HW] = prot[TLS_SW][TLS_SW];
  783. prot[TLS_HW][TLS_HW] = prot[TLS_HW][TLS_SW];
  784. #endif
  785. #ifdef CONFIG_TLS_TOE
  786. prot[TLS_HW_RECORD][TLS_HW_RECORD] = *base;
  787. prot[TLS_HW_RECORD][TLS_HW_RECORD].hash = tls_toe_hash;
  788. prot[TLS_HW_RECORD][TLS_HW_RECORD].unhash = tls_toe_unhash;
  789. #endif
  790. }
  791. static int tls_init(struct sock *sk)
  792. {
  793. struct tls_context *ctx;
  794. int rc = 0;
  795. tls_build_proto(sk);
  796. #ifdef CONFIG_TLS_TOE
  797. if (tls_toe_bypass(sk))
  798. return 0;
  799. #endif
  800. /* The TLS ulp is currently supported only for TCP sockets
  801. * in ESTABLISHED state.
  802. * Supporting sockets in LISTEN state will require us
  803. * to modify the accept implementation to clone rather then
  804. * share the ulp context.
  805. */
  806. if (sk->sk_state != TCP_ESTABLISHED)
  807. return -ENOTCONN;
  808. /* allocate tls context */
  809. write_lock_bh(&sk->sk_callback_lock);
  810. ctx = tls_ctx_create(sk);
  811. if (!ctx) {
  812. rc = -ENOMEM;
  813. goto out;
  814. }
  815. ctx->tx_conf = TLS_BASE;
  816. ctx->rx_conf = TLS_BASE;
  817. update_sk_prot(sk, ctx);
  818. out:
  819. write_unlock_bh(&sk->sk_callback_lock);
  820. return rc;
  821. }
  822. static void tls_update(struct sock *sk, struct proto *p,
  823. void (*write_space)(struct sock *sk))
  824. {
  825. struct tls_context *ctx;
  826. WARN_ON_ONCE(sk->sk_prot == p);
  827. ctx = tls_get_ctx(sk);
  828. if (likely(ctx)) {
  829. ctx->sk_write_space = write_space;
  830. ctx->sk_proto = p;
  831. } else {
  832. /* Pairs with lockless read in sk_clone_lock(). */
  833. WRITE_ONCE(sk->sk_prot, p);
  834. sk->sk_write_space = write_space;
  835. }
  836. }
  837. static u16 tls_user_config(struct tls_context *ctx, bool tx)
  838. {
  839. u16 config = tx ? ctx->tx_conf : ctx->rx_conf;
  840. switch (config) {
  841. case TLS_BASE:
  842. return TLS_CONF_BASE;
  843. case TLS_SW:
  844. return TLS_CONF_SW;
  845. case TLS_HW:
  846. return TLS_CONF_HW;
  847. case TLS_HW_RECORD:
  848. return TLS_CONF_HW_RECORD;
  849. }
  850. return 0;
  851. }
  852. static int tls_get_info(struct sock *sk, struct sk_buff *skb)
  853. {
  854. u16 version, cipher_type;
  855. struct tls_context *ctx;
  856. struct nlattr *start;
  857. int err;
  858. start = nla_nest_start_noflag(skb, INET_ULP_INFO_TLS);
  859. if (!start)
  860. return -EMSGSIZE;
  861. rcu_read_lock();
  862. ctx = rcu_dereference(inet_csk(sk)->icsk_ulp_data);
  863. if (!ctx) {
  864. err = 0;
  865. goto nla_failure;
  866. }
  867. version = ctx->prot_info.version;
  868. if (version) {
  869. err = nla_put_u16(skb, TLS_INFO_VERSION, version);
  870. if (err)
  871. goto nla_failure;
  872. }
  873. cipher_type = ctx->prot_info.cipher_type;
  874. if (cipher_type) {
  875. err = nla_put_u16(skb, TLS_INFO_CIPHER, cipher_type);
  876. if (err)
  877. goto nla_failure;
  878. }
  879. err = nla_put_u16(skb, TLS_INFO_TXCONF, tls_user_config(ctx, true));
  880. if (err)
  881. goto nla_failure;
  882. err = nla_put_u16(skb, TLS_INFO_RXCONF, tls_user_config(ctx, false));
  883. if (err)
  884. goto nla_failure;
  885. if (ctx->tx_conf == TLS_HW && ctx->zerocopy_sendfile) {
  886. err = nla_put_flag(skb, TLS_INFO_ZC_RO_TX);
  887. if (err)
  888. goto nla_failure;
  889. }
  890. if (ctx->rx_no_pad) {
  891. err = nla_put_flag(skb, TLS_INFO_RX_NO_PAD);
  892. if (err)
  893. goto nla_failure;
  894. }
  895. rcu_read_unlock();
  896. nla_nest_end(skb, start);
  897. return 0;
  898. nla_failure:
  899. rcu_read_unlock();
  900. nla_nest_cancel(skb, start);
  901. return err;
  902. }
  903. static size_t tls_get_info_size(const struct sock *sk)
  904. {
  905. size_t size = 0;
  906. size += nla_total_size(0) + /* INET_ULP_INFO_TLS */
  907. nla_total_size(sizeof(u16)) + /* TLS_INFO_VERSION */
  908. nla_total_size(sizeof(u16)) + /* TLS_INFO_CIPHER */
  909. nla_total_size(sizeof(u16)) + /* TLS_INFO_RXCONF */
  910. nla_total_size(sizeof(u16)) + /* TLS_INFO_TXCONF */
  911. nla_total_size(0) + /* TLS_INFO_ZC_RO_TX */
  912. nla_total_size(0) + /* TLS_INFO_RX_NO_PAD */
  913. 0;
  914. return size;
  915. }
  916. static int __net_init tls_init_net(struct net *net)
  917. {
  918. int err;
  919. net->mib.tls_statistics = alloc_percpu(struct linux_tls_mib);
  920. if (!net->mib.tls_statistics)
  921. return -ENOMEM;
  922. err = tls_proc_init(net);
  923. if (err)
  924. goto err_free_stats;
  925. return 0;
  926. err_free_stats:
  927. free_percpu(net->mib.tls_statistics);
  928. return err;
  929. }
  930. static void __net_exit tls_exit_net(struct net *net)
  931. {
  932. tls_proc_fini(net);
  933. free_percpu(net->mib.tls_statistics);
  934. }
  935. static struct pernet_operations tls_proc_ops = {
  936. .init = tls_init_net,
  937. .exit = tls_exit_net,
  938. };
  939. static struct tcp_ulp_ops tcp_tls_ulp_ops __read_mostly = {
  940. .name = "tls",
  941. .owner = THIS_MODULE,
  942. .init = tls_init,
  943. .update = tls_update,
  944. .get_info = tls_get_info,
  945. .get_info_size = tls_get_info_size,
  946. };
  947. static int __init tls_register(void)
  948. {
  949. int err;
  950. err = register_pernet_subsys(&tls_proc_ops);
  951. if (err)
  952. return err;
  953. err = tls_strp_dev_init();
  954. if (err)
  955. goto err_pernet;
  956. err = tls_device_init();
  957. if (err)
  958. goto err_strp;
  959. tcp_register_ulp(&tcp_tls_ulp_ops);
  960. return 0;
  961. err_strp:
  962. tls_strp_dev_exit();
  963. err_pernet:
  964. unregister_pernet_subsys(&tls_proc_ops);
  965. return err;
  966. }
  967. static void __exit tls_unregister(void)
  968. {
  969. tcp_unregister_ulp(&tcp_tls_ulp_ops);
  970. tls_strp_dev_exit();
  971. tls_device_cleanup();
  972. unregister_pernet_subsys(&tls_proc_ops);
  973. }
  974. module_init(tls_register);
  975. module_exit(tls_unregister);