tls_main.c 11 KB

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