tls_device_fallback.c 13 KB

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