tls_device_fallback.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493
  1. /* Copyright (c) 2018, Mellanox Technologies All rights reserved.
  2. *
  3. * This software is available to you under a choice of one of two
  4. * licenses. You may choose to be licensed under the terms of the GNU
  5. * General Public License (GPL) Version 2, available from the file
  6. * COPYING in the main directory of this source tree, or the
  7. * OpenIB.org BSD license below:
  8. *
  9. * Redistribution and use in source and binary forms, with or
  10. * without modification, are permitted provided that the following
  11. * conditions are met:
  12. *
  13. * - Redistributions of source code must retain the above
  14. * copyright notice, this list of conditions and the following
  15. * disclaimer.
  16. *
  17. * - Redistributions in binary form must reproduce the above
  18. * copyright notice, this list of conditions and the following
  19. * disclaimer in the documentation and/or other materials
  20. * provided with the distribution.
  21. *
  22. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  23. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  24. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  25. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  26. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  27. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  28. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  29. * SOFTWARE.
  30. */
  31. #include <net/tls.h>
  32. #include <crypto/aead.h>
  33. #include <crypto/scatterwalk.h>
  34. #include <net/ip6_checksum.h>
  35. #include <linux/skbuff_ref.h>
  36. #include "tls.h"
  37. static void chain_to_walk(struct scatterlist *sg, struct scatter_walk *walk)
  38. {
  39. struct scatterlist *src = walk->sg;
  40. int diff = walk->offset - src->offset;
  41. sg_set_page(sg, sg_page(src),
  42. src->length - diff, walk->offset);
  43. scatterwalk_crypto_chain(sg, sg_next(src), 2);
  44. }
  45. static int tls_enc_record(struct aead_request *aead_req,
  46. struct crypto_aead *aead, char *aad,
  47. char *iv, __be64 rcd_sn,
  48. struct scatter_walk *in,
  49. struct scatter_walk *out, int *in_len,
  50. struct tls_prot_info *prot)
  51. {
  52. unsigned char buf[TLS_HEADER_SIZE + TLS_MAX_IV_SIZE];
  53. const struct tls_cipher_desc *cipher_desc;
  54. struct scatterlist sg_in[3];
  55. struct scatterlist sg_out[3];
  56. unsigned int buf_size;
  57. u16 len;
  58. int rc;
  59. cipher_desc = get_cipher_desc(prot->cipher_type);
  60. DEBUG_NET_WARN_ON_ONCE(!cipher_desc || !cipher_desc->offloadable);
  61. buf_size = TLS_HEADER_SIZE + cipher_desc->iv;
  62. len = min_t(int, *in_len, buf_size);
  63. scatterwalk_copychunks(buf, in, len, 0);
  64. scatterwalk_copychunks(buf, out, len, 1);
  65. *in_len -= len;
  66. if (!*in_len)
  67. return 0;
  68. scatterwalk_pagedone(in, 0, 1);
  69. scatterwalk_pagedone(out, 1, 1);
  70. len = buf[4] | (buf[3] << 8);
  71. len -= cipher_desc->iv;
  72. tls_make_aad(aad, len - cipher_desc->tag, (char *)&rcd_sn, buf[0], prot);
  73. memcpy(iv + cipher_desc->salt, buf + TLS_HEADER_SIZE, cipher_desc->iv);
  74. sg_init_table(sg_in, ARRAY_SIZE(sg_in));
  75. sg_init_table(sg_out, ARRAY_SIZE(sg_out));
  76. sg_set_buf(sg_in, aad, TLS_AAD_SPACE_SIZE);
  77. sg_set_buf(sg_out, aad, TLS_AAD_SPACE_SIZE);
  78. chain_to_walk(sg_in + 1, in);
  79. chain_to_walk(sg_out + 1, out);
  80. *in_len -= len;
  81. if (*in_len < 0) {
  82. *in_len += cipher_desc->tag;
  83. /* the input buffer doesn't contain the entire record.
  84. * trim len accordingly. The resulting authentication tag
  85. * will contain garbage, but we don't care, so we won't
  86. * include any of it in the output skb
  87. * Note that we assume the output buffer length
  88. * is larger then input buffer length + tag size
  89. */
  90. if (*in_len < 0)
  91. len += *in_len;
  92. *in_len = 0;
  93. }
  94. if (*in_len) {
  95. scatterwalk_copychunks(NULL, in, len, 2);
  96. scatterwalk_pagedone(in, 0, 1);
  97. scatterwalk_copychunks(NULL, out, len, 2);
  98. scatterwalk_pagedone(out, 1, 1);
  99. }
  100. len -= cipher_desc->tag;
  101. aead_request_set_crypt(aead_req, sg_in, sg_out, len, iv);
  102. rc = crypto_aead_encrypt(aead_req);
  103. return rc;
  104. }
  105. static void tls_init_aead_request(struct aead_request *aead_req,
  106. struct crypto_aead *aead)
  107. {
  108. aead_request_set_tfm(aead_req, aead);
  109. aead_request_set_ad(aead_req, TLS_AAD_SPACE_SIZE);
  110. }
  111. static struct aead_request *tls_alloc_aead_request(struct crypto_aead *aead,
  112. gfp_t flags)
  113. {
  114. unsigned int req_size = sizeof(struct aead_request) +
  115. crypto_aead_reqsize(aead);
  116. struct aead_request *aead_req;
  117. aead_req = kzalloc(req_size, flags);
  118. if (aead_req)
  119. tls_init_aead_request(aead_req, aead);
  120. return aead_req;
  121. }
  122. static int tls_enc_records(struct aead_request *aead_req,
  123. struct crypto_aead *aead, struct scatterlist *sg_in,
  124. struct scatterlist *sg_out, char *aad, char *iv,
  125. u64 rcd_sn, int len, struct tls_prot_info *prot)
  126. {
  127. struct scatter_walk out, in;
  128. int rc;
  129. scatterwalk_start(&in, sg_in);
  130. scatterwalk_start(&out, sg_out);
  131. do {
  132. rc = tls_enc_record(aead_req, aead, aad, iv,
  133. cpu_to_be64(rcd_sn), &in, &out, &len, prot);
  134. rcd_sn++;
  135. } while (rc == 0 && len);
  136. scatterwalk_done(&in, 0, 0);
  137. scatterwalk_done(&out, 1, 0);
  138. return rc;
  139. }
  140. /* Can't use icsk->icsk_af_ops->send_check here because the ip addresses
  141. * might have been changed by NAT.
  142. */
  143. static void update_chksum(struct sk_buff *skb, int headln)
  144. {
  145. struct tcphdr *th = tcp_hdr(skb);
  146. int datalen = skb->len - headln;
  147. const struct ipv6hdr *ipv6h;
  148. const struct iphdr *iph;
  149. /* We only changed the payload so if we are using partial we don't
  150. * need to update anything.
  151. */
  152. if (likely(skb->ip_summed == CHECKSUM_PARTIAL))
  153. return;
  154. skb->ip_summed = CHECKSUM_PARTIAL;
  155. skb->csum_start = skb_transport_header(skb) - skb->head;
  156. skb->csum_offset = offsetof(struct tcphdr, check);
  157. if (skb->sk->sk_family == AF_INET6) {
  158. ipv6h = ipv6_hdr(skb);
  159. th->check = ~csum_ipv6_magic(&ipv6h->saddr, &ipv6h->daddr,
  160. datalen, IPPROTO_TCP, 0);
  161. } else {
  162. iph = ip_hdr(skb);
  163. th->check = ~csum_tcpudp_magic(iph->saddr, iph->daddr, datalen,
  164. IPPROTO_TCP, 0);
  165. }
  166. }
  167. static void complete_skb(struct sk_buff *nskb, struct sk_buff *skb, int headln)
  168. {
  169. struct sock *sk = skb->sk;
  170. int delta;
  171. skb_copy_header(nskb, skb);
  172. skb_put(nskb, skb->len);
  173. memcpy(nskb->data, skb->data, headln);
  174. nskb->destructor = skb->destructor;
  175. nskb->sk = sk;
  176. skb->destructor = NULL;
  177. skb->sk = NULL;
  178. update_chksum(nskb, headln);
  179. /* sock_efree means skb must gone through skb_orphan_partial() */
  180. if (nskb->destructor == sock_efree)
  181. return;
  182. delta = nskb->truesize - skb->truesize;
  183. if (likely(delta < 0))
  184. WARN_ON_ONCE(refcount_sub_and_test(-delta, &sk->sk_wmem_alloc));
  185. else if (delta)
  186. refcount_add(delta, &sk->sk_wmem_alloc);
  187. }
  188. /* This function may be called after the user socket is already
  189. * closed so make sure we don't use anything freed during
  190. * tls_sk_proto_close here
  191. */
  192. static int fill_sg_in(struct scatterlist *sg_in,
  193. struct sk_buff *skb,
  194. struct tls_offload_context_tx *ctx,
  195. u64 *rcd_sn,
  196. s32 *sync_size,
  197. int *resync_sgs)
  198. {
  199. int tcp_payload_offset = skb_tcp_all_headers(skb);
  200. int payload_len = skb->len - tcp_payload_offset;
  201. u32 tcp_seq = ntohl(tcp_hdr(skb)->seq);
  202. struct tls_record_info *record;
  203. unsigned long flags;
  204. int remaining;
  205. int i;
  206. spin_lock_irqsave(&ctx->lock, flags);
  207. record = tls_get_record(ctx, tcp_seq, rcd_sn);
  208. if (!record) {
  209. spin_unlock_irqrestore(&ctx->lock, flags);
  210. return -EINVAL;
  211. }
  212. *sync_size = tcp_seq - tls_record_start_seq(record);
  213. if (*sync_size < 0) {
  214. int is_start_marker = tls_record_is_start_marker(record);
  215. spin_unlock_irqrestore(&ctx->lock, flags);
  216. /* This should only occur if the relevant record was
  217. * already acked. In that case it should be ok
  218. * to drop the packet and avoid retransmission.
  219. *
  220. * There is a corner case where the packet contains
  221. * both an acked and a non-acked record.
  222. * We currently don't handle that case and rely
  223. * on TCP to retransmit a packet that doesn't contain
  224. * already acked payload.
  225. */
  226. if (!is_start_marker)
  227. *sync_size = 0;
  228. return -EINVAL;
  229. }
  230. remaining = *sync_size;
  231. for (i = 0; remaining > 0; i++) {
  232. skb_frag_t *frag = &record->frags[i];
  233. __skb_frag_ref(frag);
  234. sg_set_page(sg_in + i, skb_frag_page(frag),
  235. skb_frag_size(frag), skb_frag_off(frag));
  236. remaining -= skb_frag_size(frag);
  237. if (remaining < 0)
  238. sg_in[i].length += remaining;
  239. }
  240. *resync_sgs = i;
  241. spin_unlock_irqrestore(&ctx->lock, flags);
  242. if (skb_to_sgvec(skb, &sg_in[i], tcp_payload_offset, payload_len) < 0)
  243. return -EINVAL;
  244. return 0;
  245. }
  246. static void fill_sg_out(struct scatterlist sg_out[3], void *buf,
  247. struct tls_context *tls_ctx,
  248. struct sk_buff *nskb,
  249. int tcp_payload_offset,
  250. int payload_len,
  251. int sync_size,
  252. void *dummy_buf)
  253. {
  254. const struct tls_cipher_desc *cipher_desc =
  255. get_cipher_desc(tls_ctx->crypto_send.info.cipher_type);
  256. sg_set_buf(&sg_out[0], dummy_buf, sync_size);
  257. sg_set_buf(&sg_out[1], nskb->data + tcp_payload_offset, payload_len);
  258. /* Add room for authentication tag produced by crypto */
  259. dummy_buf += sync_size;
  260. sg_set_buf(&sg_out[2], dummy_buf, cipher_desc->tag);
  261. }
  262. static struct sk_buff *tls_enc_skb(struct tls_context *tls_ctx,
  263. struct scatterlist sg_out[3],
  264. struct scatterlist *sg_in,
  265. struct sk_buff *skb,
  266. s32 sync_size, u64 rcd_sn)
  267. {
  268. struct tls_offload_context_tx *ctx = tls_offload_ctx_tx(tls_ctx);
  269. int tcp_payload_offset = skb_tcp_all_headers(skb);
  270. int payload_len = skb->len - tcp_payload_offset;
  271. const struct tls_cipher_desc *cipher_desc;
  272. void *buf, *iv, *aad, *dummy_buf, *salt;
  273. struct aead_request *aead_req;
  274. struct sk_buff *nskb = NULL;
  275. int buf_len;
  276. aead_req = tls_alloc_aead_request(ctx->aead_send, GFP_ATOMIC);
  277. if (!aead_req)
  278. return NULL;
  279. cipher_desc = get_cipher_desc(tls_ctx->crypto_send.info.cipher_type);
  280. DEBUG_NET_WARN_ON_ONCE(!cipher_desc || !cipher_desc->offloadable);
  281. buf_len = cipher_desc->salt + cipher_desc->iv + TLS_AAD_SPACE_SIZE +
  282. sync_size + cipher_desc->tag;
  283. buf = kmalloc(buf_len, GFP_ATOMIC);
  284. if (!buf)
  285. goto free_req;
  286. iv = buf;
  287. salt = crypto_info_salt(&tls_ctx->crypto_send.info, cipher_desc);
  288. memcpy(iv, salt, cipher_desc->salt);
  289. aad = buf + cipher_desc->salt + cipher_desc->iv;
  290. dummy_buf = aad + TLS_AAD_SPACE_SIZE;
  291. nskb = alloc_skb(skb_headroom(skb) + skb->len, GFP_ATOMIC);
  292. if (!nskb)
  293. goto free_buf;
  294. skb_reserve(nskb, skb_headroom(skb));
  295. fill_sg_out(sg_out, buf, tls_ctx, nskb, tcp_payload_offset,
  296. payload_len, sync_size, dummy_buf);
  297. if (tls_enc_records(aead_req, ctx->aead_send, sg_in, sg_out, aad, iv,
  298. rcd_sn, sync_size + payload_len,
  299. &tls_ctx->prot_info) < 0)
  300. goto free_nskb;
  301. complete_skb(nskb, skb, tcp_payload_offset);
  302. /* validate_xmit_skb_list assumes that if the skb wasn't segmented
  303. * nskb->prev will point to the skb itself
  304. */
  305. nskb->prev = nskb;
  306. free_buf:
  307. kfree(buf);
  308. free_req:
  309. kfree(aead_req);
  310. return nskb;
  311. free_nskb:
  312. kfree_skb(nskb);
  313. nskb = NULL;
  314. goto free_buf;
  315. }
  316. static struct sk_buff *tls_sw_fallback(struct sock *sk, struct sk_buff *skb)
  317. {
  318. int tcp_payload_offset = skb_tcp_all_headers(skb);
  319. struct tls_context *tls_ctx = tls_get_ctx(sk);
  320. struct tls_offload_context_tx *ctx = tls_offload_ctx_tx(tls_ctx);
  321. int payload_len = skb->len - tcp_payload_offset;
  322. struct scatterlist *sg_in, sg_out[3];
  323. struct sk_buff *nskb = NULL;
  324. int sg_in_max_elements;
  325. int resync_sgs = 0;
  326. s32 sync_size = 0;
  327. u64 rcd_sn;
  328. /* worst case is:
  329. * MAX_SKB_FRAGS in tls_record_info
  330. * MAX_SKB_FRAGS + 1 in SKB head and frags.
  331. */
  332. sg_in_max_elements = 2 * MAX_SKB_FRAGS + 1;
  333. if (!payload_len)
  334. return skb;
  335. sg_in = kmalloc_array(sg_in_max_elements, sizeof(*sg_in), GFP_ATOMIC);
  336. if (!sg_in)
  337. goto free_orig;
  338. sg_init_table(sg_in, sg_in_max_elements);
  339. sg_init_table(sg_out, ARRAY_SIZE(sg_out));
  340. if (fill_sg_in(sg_in, skb, ctx, &rcd_sn, &sync_size, &resync_sgs)) {
  341. /* bypass packets before kernel TLS socket option was set */
  342. if (sync_size < 0 && payload_len <= -sync_size)
  343. nskb = skb_get(skb);
  344. goto put_sg;
  345. }
  346. nskb = tls_enc_skb(tls_ctx, sg_out, sg_in, skb, sync_size, rcd_sn);
  347. put_sg:
  348. while (resync_sgs)
  349. put_page(sg_page(&sg_in[--resync_sgs]));
  350. kfree(sg_in);
  351. free_orig:
  352. if (nskb)
  353. consume_skb(skb);
  354. else
  355. kfree_skb(skb);
  356. return nskb;
  357. }
  358. struct sk_buff *tls_validate_xmit_skb(struct sock *sk,
  359. struct net_device *dev,
  360. struct sk_buff *skb)
  361. {
  362. if (dev == rcu_dereference_bh(tls_get_ctx(sk)->netdev) ||
  363. netif_is_bond_master(dev))
  364. return skb;
  365. return tls_sw_fallback(sk, skb);
  366. }
  367. EXPORT_SYMBOL_GPL(tls_validate_xmit_skb);
  368. struct sk_buff *tls_validate_xmit_skb_sw(struct sock *sk,
  369. struct net_device *dev,
  370. struct sk_buff *skb)
  371. {
  372. return tls_sw_fallback(sk, skb);
  373. }
  374. struct sk_buff *tls_encrypt_skb(struct sk_buff *skb)
  375. {
  376. return tls_sw_fallback(skb->sk, skb);
  377. }
  378. EXPORT_SYMBOL_GPL(tls_encrypt_skb);
  379. int tls_sw_fallback_init(struct sock *sk,
  380. struct tls_offload_context_tx *offload_ctx,
  381. struct tls_crypto_info *crypto_info)
  382. {
  383. const struct tls_cipher_desc *cipher_desc;
  384. int rc;
  385. cipher_desc = get_cipher_desc(crypto_info->cipher_type);
  386. if (!cipher_desc || !cipher_desc->offloadable)
  387. return -EINVAL;
  388. offload_ctx->aead_send =
  389. crypto_alloc_aead(cipher_desc->cipher_name, 0, CRYPTO_ALG_ASYNC);
  390. if (IS_ERR(offload_ctx->aead_send)) {
  391. rc = PTR_ERR(offload_ctx->aead_send);
  392. pr_err_ratelimited("crypto_alloc_aead failed rc=%d\n", rc);
  393. offload_ctx->aead_send = NULL;
  394. goto err_out;
  395. }
  396. rc = crypto_aead_setkey(offload_ctx->aead_send,
  397. crypto_info_key(crypto_info, cipher_desc),
  398. cipher_desc->key);
  399. if (rc)
  400. goto free_aead;
  401. rc = crypto_aead_setauthsize(offload_ctx->aead_send, cipher_desc->tag);
  402. if (rc)
  403. goto free_aead;
  404. return 0;
  405. free_aead:
  406. crypto_free_aead(offload_ctx->aead_send);
  407. err_out:
  408. return rc;
  409. }