tls_main.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523
  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 <net/tls.h>
  40. MODULE_AUTHOR("Mellanox Technologies");
  41. MODULE_DESCRIPTION("Transport Layer Security Support");
  42. MODULE_LICENSE("Dual BSD/GPL");
  43. enum {
  44. TLS_BASE_TX,
  45. TLS_SW_TX,
  46. TLS_NUM_CONFIG,
  47. };
  48. static struct proto tls_prots[TLS_NUM_CONFIG];
  49. static inline void update_sk_prot(struct sock *sk, struct tls_context *ctx)
  50. {
  51. sk->sk_prot = &tls_prots[ctx->tx_conf];
  52. }
  53. int wait_on_pending_writer(struct sock *sk, long *timeo)
  54. {
  55. int rc = 0;
  56. DEFINE_WAIT_FUNC(wait, woken_wake_function);
  57. add_wait_queue(sk_sleep(sk), &wait);
  58. while (1) {
  59. if (!*timeo) {
  60. rc = -EAGAIN;
  61. break;
  62. }
  63. if (signal_pending(current)) {
  64. rc = sock_intr_errno(*timeo);
  65. break;
  66. }
  67. if (sk_wait_event(sk, timeo, !sk->sk_write_pending, &wait))
  68. break;
  69. }
  70. remove_wait_queue(sk_sleep(sk), &wait);
  71. return rc;
  72. }
  73. int tls_push_sg(struct sock *sk,
  74. struct tls_context *ctx,
  75. struct scatterlist *sg,
  76. u16 first_offset,
  77. int flags)
  78. {
  79. int sendpage_flags = flags | MSG_SENDPAGE_NOTLAST;
  80. int ret = 0;
  81. struct page *p;
  82. size_t size;
  83. int offset = first_offset;
  84. size = sg->length - offset;
  85. offset += sg->offset;
  86. while (1) {
  87. if (sg_is_last(sg))
  88. sendpage_flags = flags;
  89. /* is sending application-limited? */
  90. tcp_rate_check_app_limited(sk);
  91. p = sg_page(sg);
  92. retry:
  93. ret = do_tcp_sendpages(sk, p, offset, size, sendpage_flags);
  94. if (ret != size) {
  95. if (ret > 0) {
  96. offset += ret;
  97. size -= ret;
  98. goto retry;
  99. }
  100. offset -= sg->offset;
  101. ctx->partially_sent_offset = offset;
  102. ctx->partially_sent_record = (void *)sg;
  103. return ret;
  104. }
  105. put_page(p);
  106. sk_mem_uncharge(sk, sg->length);
  107. sg = sg_next(sg);
  108. if (!sg)
  109. break;
  110. offset = sg->offset;
  111. size = sg->length;
  112. }
  113. clear_bit(TLS_PENDING_CLOSED_RECORD, &ctx->flags);
  114. return 0;
  115. }
  116. static int tls_handle_open_record(struct sock *sk, int flags)
  117. {
  118. struct tls_context *ctx = tls_get_ctx(sk);
  119. if (tls_is_pending_open_record(ctx))
  120. return ctx->push_pending_record(sk, flags);
  121. return 0;
  122. }
  123. int tls_proccess_cmsg(struct sock *sk, struct msghdr *msg,
  124. unsigned char *record_type)
  125. {
  126. struct cmsghdr *cmsg;
  127. int rc = -EINVAL;
  128. for_each_cmsghdr(cmsg, msg) {
  129. if (!CMSG_OK(msg, cmsg))
  130. return -EINVAL;
  131. if (cmsg->cmsg_level != SOL_TLS)
  132. continue;
  133. switch (cmsg->cmsg_type) {
  134. case TLS_SET_RECORD_TYPE:
  135. if (cmsg->cmsg_len < CMSG_LEN(sizeof(*record_type)))
  136. return -EINVAL;
  137. if (msg->msg_flags & MSG_MORE)
  138. return -EINVAL;
  139. rc = tls_handle_open_record(sk, msg->msg_flags);
  140. if (rc)
  141. return rc;
  142. *record_type = *(unsigned char *)CMSG_DATA(cmsg);
  143. rc = 0;
  144. break;
  145. default:
  146. return -EINVAL;
  147. }
  148. }
  149. return rc;
  150. }
  151. int tls_push_pending_closed_record(struct sock *sk, struct tls_context *ctx,
  152. int flags, long *timeo)
  153. {
  154. struct scatterlist *sg;
  155. u16 offset;
  156. if (!tls_is_partially_sent_record(ctx))
  157. return ctx->push_pending_record(sk, flags);
  158. sg = ctx->partially_sent_record;
  159. offset = ctx->partially_sent_offset;
  160. ctx->partially_sent_record = NULL;
  161. return tls_push_sg(sk, ctx, sg, offset, flags);
  162. }
  163. static void tls_write_space(struct sock *sk)
  164. {
  165. struct tls_context *ctx = tls_get_ctx(sk);
  166. if (!sk->sk_write_pending && tls_is_pending_closed_record(ctx)) {
  167. gfp_t sk_allocation = sk->sk_allocation;
  168. int rc;
  169. long timeo = 0;
  170. sk->sk_allocation = GFP_ATOMIC;
  171. rc = tls_push_pending_closed_record(sk, ctx,
  172. MSG_DONTWAIT |
  173. MSG_NOSIGNAL,
  174. &timeo);
  175. sk->sk_allocation = sk_allocation;
  176. if (rc < 0)
  177. return;
  178. }
  179. ctx->sk_write_space(sk);
  180. }
  181. static void tls_sk_proto_close(struct sock *sk, long timeout)
  182. {
  183. struct tls_context *ctx = tls_get_ctx(sk);
  184. long timeo = sock_sndtimeo(sk, 0);
  185. void (*sk_proto_close)(struct sock *sk, long timeout);
  186. lock_sock(sk);
  187. sk_proto_close = ctx->sk_proto_close;
  188. if (ctx->tx_conf == TLS_BASE_TX) {
  189. kfree(ctx);
  190. goto skip_tx_cleanup;
  191. }
  192. if (!tls_complete_pending_work(sk, ctx, 0, &timeo))
  193. tls_handle_open_record(sk, 0);
  194. if (ctx->partially_sent_record) {
  195. struct scatterlist *sg = ctx->partially_sent_record;
  196. while (1) {
  197. put_page(sg_page(sg));
  198. sk_mem_uncharge(sk, sg->length);
  199. if (sg_is_last(sg))
  200. break;
  201. sg++;
  202. }
  203. }
  204. kfree(ctx->rec_seq);
  205. kfree(ctx->iv);
  206. if (ctx->tx_conf == TLS_SW_TX)
  207. tls_sw_free_tx_resources(sk);
  208. skip_tx_cleanup:
  209. release_sock(sk);
  210. sk_proto_close(sk, timeout);
  211. }
  212. static int do_tls_getsockopt_tx(struct sock *sk, char __user *optval,
  213. int __user *optlen)
  214. {
  215. int rc = 0;
  216. struct tls_context *ctx = tls_get_ctx(sk);
  217. struct tls_crypto_info *crypto_info;
  218. int len;
  219. if (get_user(len, optlen))
  220. return -EFAULT;
  221. if (!optval || (len < sizeof(*crypto_info))) {
  222. rc = -EINVAL;
  223. goto out;
  224. }
  225. if (!ctx) {
  226. rc = -EBUSY;
  227. goto out;
  228. }
  229. /* get user crypto info */
  230. crypto_info = &ctx->crypto_send;
  231. if (!TLS_CRYPTO_INFO_READY(crypto_info)) {
  232. rc = -EBUSY;
  233. goto out;
  234. }
  235. if (len == sizeof(*crypto_info)) {
  236. if (copy_to_user(optval, crypto_info, sizeof(*crypto_info)))
  237. rc = -EFAULT;
  238. goto out;
  239. }
  240. switch (crypto_info->cipher_type) {
  241. case TLS_CIPHER_AES_GCM_128: {
  242. struct tls12_crypto_info_aes_gcm_128 *
  243. crypto_info_aes_gcm_128 =
  244. container_of(crypto_info,
  245. struct tls12_crypto_info_aes_gcm_128,
  246. info);
  247. if (len != sizeof(*crypto_info_aes_gcm_128)) {
  248. rc = -EINVAL;
  249. goto out;
  250. }
  251. lock_sock(sk);
  252. memcpy(crypto_info_aes_gcm_128->iv,
  253. ctx->iv + TLS_CIPHER_AES_GCM_128_SALT_SIZE,
  254. TLS_CIPHER_AES_GCM_128_IV_SIZE);
  255. memcpy(crypto_info_aes_gcm_128->rec_seq, ctx->rec_seq,
  256. TLS_CIPHER_AES_GCM_128_REC_SEQ_SIZE);
  257. release_sock(sk);
  258. if (copy_to_user(optval,
  259. crypto_info_aes_gcm_128,
  260. sizeof(*crypto_info_aes_gcm_128)))
  261. rc = -EFAULT;
  262. break;
  263. }
  264. default:
  265. rc = -EINVAL;
  266. }
  267. out:
  268. return rc;
  269. }
  270. static int do_tls_getsockopt(struct sock *sk, int optname,
  271. char __user *optval, int __user *optlen)
  272. {
  273. int rc = 0;
  274. switch (optname) {
  275. case TLS_TX:
  276. rc = do_tls_getsockopt_tx(sk, optval, optlen);
  277. break;
  278. default:
  279. rc = -ENOPROTOOPT;
  280. break;
  281. }
  282. return rc;
  283. }
  284. static int tls_getsockopt(struct sock *sk, int level, int optname,
  285. char __user *optval, int __user *optlen)
  286. {
  287. struct tls_context *ctx = tls_get_ctx(sk);
  288. if (level != SOL_TLS)
  289. return ctx->getsockopt(sk, level, optname, optval, optlen);
  290. return do_tls_getsockopt(sk, optname, optval, optlen);
  291. }
  292. static int do_tls_setsockopt_tx(struct sock *sk, char __user *optval,
  293. unsigned int optlen)
  294. {
  295. struct tls_crypto_info *crypto_info;
  296. struct tls_context *ctx = tls_get_ctx(sk);
  297. int rc = 0;
  298. int tx_conf;
  299. if (!optval || (optlen < sizeof(*crypto_info))) {
  300. rc = -EINVAL;
  301. goto out;
  302. }
  303. crypto_info = &ctx->crypto_send;
  304. /* Currently we don't support set crypto info more than one time */
  305. if (TLS_CRYPTO_INFO_READY(crypto_info)) {
  306. rc = -EBUSY;
  307. goto out;
  308. }
  309. rc = copy_from_user(crypto_info, optval, sizeof(*crypto_info));
  310. if (rc) {
  311. rc = -EFAULT;
  312. goto err_crypto_info;
  313. }
  314. /* check version */
  315. if (crypto_info->version != TLS_1_2_VERSION) {
  316. rc = -ENOTSUPP;
  317. goto err_crypto_info;
  318. }
  319. switch (crypto_info->cipher_type) {
  320. case TLS_CIPHER_AES_GCM_128: {
  321. if (optlen != sizeof(struct tls12_crypto_info_aes_gcm_128)) {
  322. rc = -EINVAL;
  323. goto err_crypto_info;
  324. }
  325. rc = copy_from_user(crypto_info + 1, optval + sizeof(*crypto_info),
  326. optlen - sizeof(*crypto_info));
  327. if (rc) {
  328. rc = -EFAULT;
  329. goto err_crypto_info;
  330. }
  331. break;
  332. }
  333. default:
  334. rc = -EINVAL;
  335. goto err_crypto_info;
  336. }
  337. /* currently SW is default, we will have ethtool in future */
  338. rc = tls_set_sw_offload(sk, ctx);
  339. tx_conf = TLS_SW_TX;
  340. if (rc)
  341. goto err_crypto_info;
  342. ctx->tx_conf = tx_conf;
  343. update_sk_prot(sk, ctx);
  344. ctx->sk_write_space = sk->sk_write_space;
  345. sk->sk_write_space = tls_write_space;
  346. goto out;
  347. err_crypto_info:
  348. memset(crypto_info, 0, sizeof(*crypto_info));
  349. out:
  350. return rc;
  351. }
  352. static int do_tls_setsockopt(struct sock *sk, int optname,
  353. char __user *optval, unsigned int optlen)
  354. {
  355. int rc = 0;
  356. switch (optname) {
  357. case TLS_TX:
  358. lock_sock(sk);
  359. rc = do_tls_setsockopt_tx(sk, optval, optlen);
  360. release_sock(sk);
  361. break;
  362. default:
  363. rc = -ENOPROTOOPT;
  364. break;
  365. }
  366. return rc;
  367. }
  368. static int tls_setsockopt(struct sock *sk, int level, int optname,
  369. char __user *optval, unsigned int optlen)
  370. {
  371. struct tls_context *ctx = tls_get_ctx(sk);
  372. if (level != SOL_TLS)
  373. return ctx->setsockopt(sk, level, optname, optval, optlen);
  374. return do_tls_setsockopt(sk, optname, optval, optlen);
  375. }
  376. static int tls_init(struct sock *sk)
  377. {
  378. struct inet_connection_sock *icsk = inet_csk(sk);
  379. struct tls_context *ctx;
  380. int rc = 0;
  381. /* The TLS ulp is currently supported only for TCP sockets
  382. * in ESTABLISHED state.
  383. * Supporting sockets in LISTEN state will require us
  384. * to modify the accept implementation to clone rather then
  385. * share the ulp context.
  386. */
  387. if (sk->sk_state != TCP_ESTABLISHED)
  388. return -ENOTSUPP;
  389. /* allocate tls context */
  390. ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
  391. if (!ctx) {
  392. rc = -ENOMEM;
  393. goto out;
  394. }
  395. icsk->icsk_ulp_data = ctx;
  396. ctx->setsockopt = sk->sk_prot->setsockopt;
  397. ctx->getsockopt = sk->sk_prot->getsockopt;
  398. ctx->sk_proto_close = sk->sk_prot->close;
  399. ctx->tx_conf = TLS_BASE_TX;
  400. update_sk_prot(sk, ctx);
  401. out:
  402. return rc;
  403. }
  404. static struct tcp_ulp_ops tcp_tls_ulp_ops __read_mostly = {
  405. .name = "tls",
  406. .uid = TCP_ULP_TLS,
  407. .user_visible = true,
  408. .owner = THIS_MODULE,
  409. .init = tls_init,
  410. };
  411. static void build_protos(struct proto *prot, struct proto *base)
  412. {
  413. prot[TLS_BASE_TX] = *base;
  414. prot[TLS_BASE_TX].setsockopt = tls_setsockopt;
  415. prot[TLS_BASE_TX].getsockopt = tls_getsockopt;
  416. prot[TLS_BASE_TX].close = tls_sk_proto_close;
  417. prot[TLS_SW_TX] = prot[TLS_BASE_TX];
  418. prot[TLS_SW_TX].sendmsg = tls_sw_sendmsg;
  419. prot[TLS_SW_TX].sendpage = tls_sw_sendpage;
  420. }
  421. static int __init tls_register(void)
  422. {
  423. build_protos(tls_prots, &tcp_prot);
  424. tcp_register_ulp(&tcp_tls_ulp_ops);
  425. return 0;
  426. }
  427. static void __exit tls_unregister(void)
  428. {
  429. tcp_unregister_ulp(&tcp_tls_ulp_ops);
  430. }
  431. module_init(tls_register);
  432. module_exit(tls_unregister);