tls_device_fallback.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513
  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 "tls.h"
  36. static void chain_to_walk(struct scatterlist *sg, struct scatter_walk *walk)
  37. {
  38. struct scatterlist *src = walk->sg;
  39. int diff = walk->offset - src->offset;
  40. sg_set_page(sg, sg_page(src),
  41. src->length - diff, walk->offset);
  42. scatterwalk_crypto_chain(sg, sg_next(src), 2);
  43. }
  44. static int tls_enc_record(struct aead_request *aead_req,
  45. struct crypto_aead *aead, char *aad,
  46. char *iv, __be64 rcd_sn,
  47. struct scatter_walk *in,
  48. struct scatter_walk *out, int *in_len,
  49. struct tls_prot_info *prot)
  50. {
  51. unsigned char buf[TLS_HEADER_SIZE + MAX_IV_SIZE];
  52. const struct tls_cipher_size_desc *cipher_sz;
  53. struct scatterlist sg_in[3];
  54. struct scatterlist sg_out[3];
  55. unsigned int buf_size;
  56. u16 len;
  57. int rc;
  58. switch (prot->cipher_type) {
  59. case TLS_CIPHER_AES_GCM_128:
  60. case TLS_CIPHER_AES_GCM_256:
  61. break;
  62. default:
  63. return -EINVAL;
  64. }
  65. cipher_sz = &tls_cipher_size_desc[prot->cipher_type];
  66. buf_size = TLS_HEADER_SIZE + cipher_sz->iv;
  67. len = min_t(int, *in_len, buf_size);
  68. scatterwalk_copychunks(buf, in, len, 0);
  69. scatterwalk_copychunks(buf, out, len, 1);
  70. *in_len -= len;
  71. if (!*in_len)
  72. return 0;
  73. scatterwalk_pagedone(in, 0, 1);
  74. scatterwalk_pagedone(out, 1, 1);
  75. len = buf[4] | (buf[3] << 8);
  76. len -= cipher_sz->iv;
  77. tls_make_aad(aad, len - cipher_sz->tag, (char *)&rcd_sn, buf[0], prot);
  78. memcpy(iv + cipher_sz->salt, buf + TLS_HEADER_SIZE, cipher_sz->iv);
  79. sg_init_table(sg_in, ARRAY_SIZE(sg_in));
  80. sg_init_table(sg_out, ARRAY_SIZE(sg_out));
  81. sg_set_buf(sg_in, aad, TLS_AAD_SPACE_SIZE);
  82. sg_set_buf(sg_out, aad, TLS_AAD_SPACE_SIZE);
  83. chain_to_walk(sg_in + 1, in);
  84. chain_to_walk(sg_out + 1, out);
  85. *in_len -= len;
  86. if (*in_len < 0) {
  87. *in_len += cipher_sz->tag;
  88. /* the input buffer doesn't contain the entire record.
  89. * trim len accordingly. The resulting authentication tag
  90. * will contain garbage, but we don't care, so we won't
  91. * include any of it in the output skb
  92. * Note that we assume the output buffer length
  93. * is larger then input buffer length + tag size
  94. */
  95. if (*in_len < 0)
  96. len += *in_len;
  97. *in_len = 0;
  98. }
  99. if (*in_len) {
  100. scatterwalk_copychunks(NULL, in, len, 2);
  101. scatterwalk_pagedone(in, 0, 1);
  102. scatterwalk_copychunks(NULL, out, len, 2);
  103. scatterwalk_pagedone(out, 1, 1);
  104. }
  105. len -= cipher_sz->tag;
  106. aead_request_set_crypt(aead_req, sg_in, sg_out, len, iv);
  107. rc = crypto_aead_encrypt(aead_req);
  108. return rc;
  109. }
  110. static void tls_init_aead_request(struct aead_request *aead_req,
  111. struct crypto_aead *aead)
  112. {
  113. aead_request_set_tfm(aead_req, aead);
  114. aead_request_set_ad(aead_req, TLS_AAD_SPACE_SIZE);
  115. }
  116. static struct aead_request *tls_alloc_aead_request(struct crypto_aead *aead,
  117. gfp_t flags)
  118. {
  119. unsigned int req_size = sizeof(struct aead_request) +
  120. crypto_aead_reqsize(aead);
  121. struct aead_request *aead_req;
  122. aead_req = kzalloc(req_size, flags);
  123. if (aead_req)
  124. tls_init_aead_request(aead_req, aead);
  125. return aead_req;
  126. }
  127. static int tls_enc_records(struct aead_request *aead_req,
  128. struct crypto_aead *aead, struct scatterlist *sg_in,
  129. struct scatterlist *sg_out, char *aad, char *iv,
  130. u64 rcd_sn, int len, struct tls_prot_info *prot)
  131. {
  132. struct scatter_walk out, in;
  133. int rc;
  134. scatterwalk_start(&in, sg_in);
  135. scatterwalk_start(&out, sg_out);
  136. do {
  137. rc = tls_enc_record(aead_req, aead, aad, iv,
  138. cpu_to_be64(rcd_sn), &in, &out, &len, prot);
  139. rcd_sn++;
  140. } while (rc == 0 && len);
  141. scatterwalk_done(&in, 0, 0);
  142. scatterwalk_done(&out, 1, 0);
  143. return rc;
  144. }
  145. /* Can't use icsk->icsk_af_ops->send_check here because the ip addresses
  146. * might have been changed by NAT.
  147. */
  148. static void update_chksum(struct sk_buff *skb, int headln)
  149. {
  150. struct tcphdr *th = tcp_hdr(skb);
  151. int datalen = skb->len - headln;
  152. const struct ipv6hdr *ipv6h;
  153. const struct iphdr *iph;
  154. /* We only changed the payload so if we are using partial we don't
  155. * need to update anything.
  156. */
  157. if (likely(skb->ip_summed == CHECKSUM_PARTIAL))
  158. return;
  159. skb->ip_summed = CHECKSUM_PARTIAL;
  160. skb->csum_start = skb_transport_header(skb) - skb->head;
  161. skb->csum_offset = offsetof(struct tcphdr, check);
  162. if (skb->sk->sk_family == AF_INET6) {
  163. ipv6h = ipv6_hdr(skb);
  164. th->check = ~csum_ipv6_magic(&ipv6h->saddr, &ipv6h->daddr,
  165. datalen, IPPROTO_TCP, 0);
  166. } else {
  167. iph = ip_hdr(skb);
  168. th->check = ~csum_tcpudp_magic(iph->saddr, iph->daddr, datalen,
  169. IPPROTO_TCP, 0);
  170. }
  171. }
  172. static void complete_skb(struct sk_buff *nskb, struct sk_buff *skb, int headln)
  173. {
  174. struct sock *sk = skb->sk;
  175. int delta;
  176. skb_copy_header(nskb, skb);
  177. skb_put(nskb, skb->len);
  178. memcpy(nskb->data, skb->data, headln);
  179. nskb->destructor = skb->destructor;
  180. nskb->sk = sk;
  181. skb->destructor = NULL;
  182. skb->sk = NULL;
  183. update_chksum(nskb, headln);
  184. /* sock_efree means skb must gone through skb_orphan_partial() */
  185. if (nskb->destructor == sock_efree)
  186. return;
  187. delta = nskb->truesize - skb->truesize;
  188. if (likely(delta < 0))
  189. WARN_ON_ONCE(refcount_sub_and_test(-delta, &sk->sk_wmem_alloc));
  190. else if (delta)
  191. refcount_add(delta, &sk->sk_wmem_alloc);
  192. }
  193. /* This function may be called after the user socket is already
  194. * closed so make sure we don't use anything freed during
  195. * tls_sk_proto_close here
  196. */
  197. static int fill_sg_in(struct scatterlist *sg_in,
  198. struct sk_buff *skb,
  199. struct tls_offload_context_tx *ctx,
  200. u64 *rcd_sn,
  201. s32 *sync_size,
  202. int *resync_sgs)
  203. {
  204. int tcp_payload_offset = skb_tcp_all_headers(skb);
  205. int payload_len = skb->len - tcp_payload_offset;
  206. u32 tcp_seq = ntohl(tcp_hdr(skb)->seq);
  207. struct tls_record_info *record;
  208. unsigned long flags;
  209. int remaining;
  210. int i;
  211. spin_lock_irqsave(&ctx->lock, flags);
  212. record = tls_get_record(ctx, tcp_seq, rcd_sn);
  213. if (!record) {
  214. spin_unlock_irqrestore(&ctx->lock, flags);
  215. return -EINVAL;
  216. }
  217. *sync_size = tcp_seq - tls_record_start_seq(record);
  218. if (*sync_size < 0) {
  219. int is_start_marker = tls_record_is_start_marker(record);
  220. spin_unlock_irqrestore(&ctx->lock, flags);
  221. /* This should only occur if the relevant record was
  222. * already acked. In that case it should be ok
  223. * to drop the packet and avoid retransmission.
  224. *
  225. * There is a corner case where the packet contains
  226. * both an acked and a non-acked record.
  227. * We currently don't handle that case and rely
  228. * on TCP to retranmit a packet that doesn't contain
  229. * already acked payload.
  230. */
  231. if (!is_start_marker)
  232. *sync_size = 0;
  233. return -EINVAL;
  234. }
  235. remaining = *sync_size;
  236. for (i = 0; remaining > 0; i++) {
  237. skb_frag_t *frag = &record->frags[i];
  238. __skb_frag_ref(frag);
  239. sg_set_page(sg_in + i, skb_frag_page(frag),
  240. skb_frag_size(frag), skb_frag_off(frag));
  241. remaining -= skb_frag_size(frag);
  242. if (remaining < 0)
  243. sg_in[i].length += remaining;
  244. }
  245. *resync_sgs = i;
  246. spin_unlock_irqrestore(&ctx->lock, flags);
  247. if (skb_to_sgvec(skb, &sg_in[i], tcp_payload_offset, payload_len) < 0)
  248. return -EINVAL;
  249. return 0;
  250. }
  251. static void fill_sg_out(struct scatterlist sg_out[3], void *buf,
  252. struct tls_context *tls_ctx,
  253. struct sk_buff *nskb,
  254. int tcp_payload_offset,
  255. int payload_len,
  256. int sync_size,
  257. void *dummy_buf)
  258. {
  259. const struct tls_cipher_size_desc *cipher_sz =
  260. &tls_cipher_size_desc[tls_ctx->crypto_send.info.cipher_type];
  261. sg_set_buf(&sg_out[0], dummy_buf, sync_size);
  262. sg_set_buf(&sg_out[1], nskb->data + tcp_payload_offset, payload_len);
  263. /* Add room for authentication tag produced by crypto */
  264. dummy_buf += sync_size;
  265. sg_set_buf(&sg_out[2], dummy_buf, cipher_sz->tag);
  266. }
  267. static struct sk_buff *tls_enc_skb(struct tls_context *tls_ctx,
  268. struct scatterlist sg_out[3],
  269. struct scatterlist *sg_in,
  270. struct sk_buff *skb,
  271. s32 sync_size, u64 rcd_sn)
  272. {
  273. struct tls_offload_context_tx *ctx = tls_offload_ctx_tx(tls_ctx);
  274. int tcp_payload_offset = skb_tcp_all_headers(skb);
  275. int payload_len = skb->len - tcp_payload_offset;
  276. const struct tls_cipher_size_desc *cipher_sz;
  277. void *buf, *iv, *aad, *dummy_buf, *salt;
  278. struct aead_request *aead_req;
  279. struct sk_buff *nskb = NULL;
  280. int buf_len;
  281. aead_req = tls_alloc_aead_request(ctx->aead_send, GFP_ATOMIC);
  282. if (!aead_req)
  283. return NULL;
  284. switch (tls_ctx->crypto_send.info.cipher_type) {
  285. case TLS_CIPHER_AES_GCM_128:
  286. salt = tls_ctx->crypto_send.aes_gcm_128.salt;
  287. break;
  288. case TLS_CIPHER_AES_GCM_256:
  289. salt = tls_ctx->crypto_send.aes_gcm_256.salt;
  290. break;
  291. default:
  292. goto free_req;
  293. }
  294. cipher_sz = &tls_cipher_size_desc[tls_ctx->crypto_send.info.cipher_type];
  295. buf_len = cipher_sz->salt + cipher_sz->iv + TLS_AAD_SPACE_SIZE +
  296. sync_size + cipher_sz->tag;
  297. buf = kmalloc(buf_len, GFP_ATOMIC);
  298. if (!buf)
  299. goto free_req;
  300. iv = buf;
  301. memcpy(iv, salt, cipher_sz->salt);
  302. aad = buf + cipher_sz->salt + cipher_sz->iv;
  303. dummy_buf = aad + TLS_AAD_SPACE_SIZE;
  304. nskb = alloc_skb(skb_headroom(skb) + skb->len, GFP_ATOMIC);
  305. if (!nskb)
  306. goto free_buf;
  307. skb_reserve(nskb, skb_headroom(skb));
  308. fill_sg_out(sg_out, buf, tls_ctx, nskb, tcp_payload_offset,
  309. payload_len, sync_size, dummy_buf);
  310. if (tls_enc_records(aead_req, ctx->aead_send, sg_in, sg_out, aad, iv,
  311. rcd_sn, sync_size + payload_len,
  312. &tls_ctx->prot_info) < 0)
  313. goto free_nskb;
  314. complete_skb(nskb, skb, tcp_payload_offset);
  315. /* validate_xmit_skb_list assumes that if the skb wasn't segmented
  316. * nskb->prev will point to the skb itself
  317. */
  318. nskb->prev = nskb;
  319. free_buf:
  320. kfree(buf);
  321. free_req:
  322. kfree(aead_req);
  323. return nskb;
  324. free_nskb:
  325. kfree_skb(nskb);
  326. nskb = NULL;
  327. goto free_buf;
  328. }
  329. static struct sk_buff *tls_sw_fallback(struct sock *sk, struct sk_buff *skb)
  330. {
  331. int tcp_payload_offset = skb_tcp_all_headers(skb);
  332. struct tls_context *tls_ctx = tls_get_ctx(sk);
  333. struct tls_offload_context_tx *ctx = tls_offload_ctx_tx(tls_ctx);
  334. int payload_len = skb->len - tcp_payload_offset;
  335. struct scatterlist *sg_in, sg_out[3];
  336. struct sk_buff *nskb = NULL;
  337. int sg_in_max_elements;
  338. int resync_sgs = 0;
  339. s32 sync_size = 0;
  340. u64 rcd_sn;
  341. /* worst case is:
  342. * MAX_SKB_FRAGS in tls_record_info
  343. * MAX_SKB_FRAGS + 1 in SKB head and frags.
  344. */
  345. sg_in_max_elements = 2 * MAX_SKB_FRAGS + 1;
  346. if (!payload_len)
  347. return skb;
  348. sg_in = kmalloc_array(sg_in_max_elements, sizeof(*sg_in), GFP_ATOMIC);
  349. if (!sg_in)
  350. goto free_orig;
  351. sg_init_table(sg_in, sg_in_max_elements);
  352. sg_init_table(sg_out, ARRAY_SIZE(sg_out));
  353. if (fill_sg_in(sg_in, skb, ctx, &rcd_sn, &sync_size, &resync_sgs)) {
  354. /* bypass packets before kernel TLS socket option was set */
  355. if (sync_size < 0 && payload_len <= -sync_size)
  356. nskb = skb_get(skb);
  357. goto put_sg;
  358. }
  359. nskb = tls_enc_skb(tls_ctx, sg_out, sg_in, skb, sync_size, rcd_sn);
  360. put_sg:
  361. while (resync_sgs)
  362. put_page(sg_page(&sg_in[--resync_sgs]));
  363. kfree(sg_in);
  364. free_orig:
  365. if (nskb)
  366. consume_skb(skb);
  367. else
  368. kfree_skb(skb);
  369. return nskb;
  370. }
  371. struct sk_buff *tls_validate_xmit_skb(struct sock *sk,
  372. struct net_device *dev,
  373. struct sk_buff *skb)
  374. {
  375. if (dev == rcu_dereference_bh(tls_get_ctx(sk)->netdev) ||
  376. netif_is_bond_master(dev))
  377. return skb;
  378. return tls_sw_fallback(sk, skb);
  379. }
  380. EXPORT_SYMBOL_GPL(tls_validate_xmit_skb);
  381. struct sk_buff *tls_validate_xmit_skb_sw(struct sock *sk,
  382. struct net_device *dev,
  383. struct sk_buff *skb)
  384. {
  385. return tls_sw_fallback(sk, skb);
  386. }
  387. struct sk_buff *tls_encrypt_skb(struct sk_buff *skb)
  388. {
  389. return tls_sw_fallback(skb->sk, skb);
  390. }
  391. EXPORT_SYMBOL_GPL(tls_encrypt_skb);
  392. int tls_sw_fallback_init(struct sock *sk,
  393. struct tls_offload_context_tx *offload_ctx,
  394. struct tls_crypto_info *crypto_info)
  395. {
  396. const struct tls_cipher_size_desc *cipher_sz;
  397. const u8 *key;
  398. int rc;
  399. offload_ctx->aead_send =
  400. crypto_alloc_aead("gcm(aes)", 0, CRYPTO_ALG_ASYNC);
  401. if (IS_ERR(offload_ctx->aead_send)) {
  402. rc = PTR_ERR(offload_ctx->aead_send);
  403. pr_err_ratelimited("crypto_alloc_aead failed rc=%d\n", rc);
  404. offload_ctx->aead_send = NULL;
  405. goto err_out;
  406. }
  407. switch (crypto_info->cipher_type) {
  408. case TLS_CIPHER_AES_GCM_128:
  409. key = ((struct tls12_crypto_info_aes_gcm_128 *)crypto_info)->key;
  410. break;
  411. case TLS_CIPHER_AES_GCM_256:
  412. key = ((struct tls12_crypto_info_aes_gcm_256 *)crypto_info)->key;
  413. break;
  414. default:
  415. rc = -EINVAL;
  416. goto free_aead;
  417. }
  418. cipher_sz = &tls_cipher_size_desc[crypto_info->cipher_type];
  419. rc = crypto_aead_setkey(offload_ctx->aead_send, key, cipher_sz->key);
  420. if (rc)
  421. goto free_aead;
  422. rc = crypto_aead_setauthsize(offload_ctx->aead_send, cipher_sz->tag);
  423. if (rc)
  424. goto free_aead;
  425. return 0;
  426. free_aead:
  427. crypto_free_aead(offload_ctx->aead_send);
  428. err_out:
  429. return rc;
  430. }