tls_device_fallback.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  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. delta = nskb->truesize - skb->truesize;
  174. if (likely(delta < 0))
  175. WARN_ON_ONCE(refcount_sub_and_test(-delta, &sk->sk_wmem_alloc));
  176. else if (delta)
  177. refcount_add(delta, &sk->sk_wmem_alloc);
  178. }
  179. /* This function may be called after the user socket is already
  180. * closed so make sure we don't use anything freed during
  181. * tls_sk_proto_close here
  182. */
  183. static int fill_sg_in(struct scatterlist *sg_in,
  184. struct sk_buff *skb,
  185. struct tls_offload_context_tx *ctx,
  186. u64 *rcd_sn,
  187. s32 *sync_size,
  188. int *resync_sgs)
  189. {
  190. int tcp_payload_offset = skb_transport_offset(skb) + tcp_hdrlen(skb);
  191. int payload_len = skb->len - tcp_payload_offset;
  192. u32 tcp_seq = ntohl(tcp_hdr(skb)->seq);
  193. struct tls_record_info *record;
  194. unsigned long flags;
  195. int remaining;
  196. int i;
  197. spin_lock_irqsave(&ctx->lock, flags);
  198. record = tls_get_record(ctx, tcp_seq, rcd_sn);
  199. if (!record) {
  200. spin_unlock_irqrestore(&ctx->lock, flags);
  201. WARN(1, "Record not found for seq %u\n", tcp_seq);
  202. return -EINVAL;
  203. }
  204. *sync_size = tcp_seq - tls_record_start_seq(record);
  205. if (*sync_size < 0) {
  206. int is_start_marker = tls_record_is_start_marker(record);
  207. spin_unlock_irqrestore(&ctx->lock, flags);
  208. /* This should only occur if the relevant record was
  209. * already acked. In that case it should be ok
  210. * to drop the packet and avoid retransmission.
  211. *
  212. * There is a corner case where the packet contains
  213. * both an acked and a non-acked record.
  214. * We currently don't handle that case and rely
  215. * on TCP to retranmit a packet that doesn't contain
  216. * already acked payload.
  217. */
  218. if (!is_start_marker)
  219. *sync_size = 0;
  220. return -EINVAL;
  221. }
  222. remaining = *sync_size;
  223. for (i = 0; remaining > 0; i++) {
  224. skb_frag_t *frag = &record->frags[i];
  225. __skb_frag_ref(frag);
  226. sg_set_page(sg_in + i, skb_frag_page(frag),
  227. skb_frag_size(frag), frag->page_offset);
  228. remaining -= skb_frag_size(frag);
  229. if (remaining < 0)
  230. sg_in[i].length += remaining;
  231. }
  232. *resync_sgs = i;
  233. spin_unlock_irqrestore(&ctx->lock, flags);
  234. if (skb_to_sgvec(skb, &sg_in[i], tcp_payload_offset, payload_len) < 0)
  235. return -EINVAL;
  236. return 0;
  237. }
  238. static void fill_sg_out(struct scatterlist sg_out[3], void *buf,
  239. struct tls_context *tls_ctx,
  240. struct sk_buff *nskb,
  241. int tcp_payload_offset,
  242. int payload_len,
  243. int sync_size,
  244. void *dummy_buf)
  245. {
  246. sg_set_buf(&sg_out[0], dummy_buf, sync_size);
  247. sg_set_buf(&sg_out[1], nskb->data + tcp_payload_offset, payload_len);
  248. /* Add room for authentication tag produced by crypto */
  249. dummy_buf += sync_size;
  250. sg_set_buf(&sg_out[2], dummy_buf, TLS_CIPHER_AES_GCM_128_TAG_SIZE);
  251. }
  252. static struct sk_buff *tls_enc_skb(struct tls_context *tls_ctx,
  253. struct scatterlist sg_out[3],
  254. struct scatterlist *sg_in,
  255. struct sk_buff *skb,
  256. s32 sync_size, u64 rcd_sn)
  257. {
  258. int tcp_payload_offset = skb_transport_offset(skb) + tcp_hdrlen(skb);
  259. struct tls_offload_context_tx *ctx = tls_offload_ctx_tx(tls_ctx);
  260. int payload_len = skb->len - tcp_payload_offset;
  261. void *buf, *iv, *aad, *dummy_buf;
  262. struct aead_request *aead_req;
  263. struct sk_buff *nskb = NULL;
  264. int buf_len;
  265. aead_req = tls_alloc_aead_request(ctx->aead_send, GFP_ATOMIC);
  266. if (!aead_req)
  267. return NULL;
  268. buf_len = TLS_CIPHER_AES_GCM_128_SALT_SIZE +
  269. TLS_CIPHER_AES_GCM_128_IV_SIZE +
  270. TLS_AAD_SPACE_SIZE +
  271. sync_size +
  272. TLS_CIPHER_AES_GCM_128_TAG_SIZE;
  273. buf = kmalloc(buf_len, GFP_ATOMIC);
  274. if (!buf)
  275. goto free_req;
  276. iv = buf;
  277. memcpy(iv, tls_ctx->crypto_send.aes_gcm_128.salt,
  278. TLS_CIPHER_AES_GCM_128_SALT_SIZE);
  279. aad = buf + TLS_CIPHER_AES_GCM_128_SALT_SIZE +
  280. TLS_CIPHER_AES_GCM_128_IV_SIZE;
  281. dummy_buf = aad + TLS_AAD_SPACE_SIZE;
  282. nskb = alloc_skb(skb_headroom(skb) + skb->len, GFP_ATOMIC);
  283. if (!nskb)
  284. goto free_buf;
  285. skb_reserve(nskb, skb_headroom(skb));
  286. fill_sg_out(sg_out, buf, tls_ctx, nskb, tcp_payload_offset,
  287. payload_len, sync_size, dummy_buf);
  288. if (tls_enc_records(aead_req, ctx->aead_send, sg_in, sg_out, aad, iv,
  289. rcd_sn, sync_size + payload_len) < 0)
  290. goto free_nskb;
  291. complete_skb(nskb, skb, tcp_payload_offset);
  292. /* validate_xmit_skb_list assumes that if the skb wasn't segmented
  293. * nskb->prev will point to the skb itself
  294. */
  295. nskb->prev = nskb;
  296. free_buf:
  297. kfree(buf);
  298. free_req:
  299. kfree(aead_req);
  300. return nskb;
  301. free_nskb:
  302. kfree_skb(nskb);
  303. nskb = NULL;
  304. goto free_buf;
  305. }
  306. static struct sk_buff *tls_sw_fallback(struct sock *sk, struct sk_buff *skb)
  307. {
  308. int tcp_payload_offset = skb_transport_offset(skb) + tcp_hdrlen(skb);
  309. struct tls_context *tls_ctx = tls_get_ctx(sk);
  310. struct tls_offload_context_tx *ctx = tls_offload_ctx_tx(tls_ctx);
  311. int payload_len = skb->len - tcp_payload_offset;
  312. struct scatterlist *sg_in, sg_out[3];
  313. struct sk_buff *nskb = NULL;
  314. int sg_in_max_elements;
  315. int resync_sgs = 0;
  316. s32 sync_size = 0;
  317. u64 rcd_sn;
  318. /* worst case is:
  319. * MAX_SKB_FRAGS in tls_record_info
  320. * MAX_SKB_FRAGS + 1 in SKB head and frags.
  321. */
  322. sg_in_max_elements = 2 * MAX_SKB_FRAGS + 1;
  323. if (!payload_len)
  324. return skb;
  325. sg_in = kmalloc_array(sg_in_max_elements, sizeof(*sg_in), GFP_ATOMIC);
  326. if (!sg_in)
  327. goto free_orig;
  328. sg_init_table(sg_in, sg_in_max_elements);
  329. sg_init_table(sg_out, ARRAY_SIZE(sg_out));
  330. if (fill_sg_in(sg_in, skb, ctx, &rcd_sn, &sync_size, &resync_sgs)) {
  331. /* bypass packets before kernel TLS socket option was set */
  332. if (sync_size < 0 && payload_len <= -sync_size)
  333. nskb = skb_get(skb);
  334. goto put_sg;
  335. }
  336. nskb = tls_enc_skb(tls_ctx, sg_out, sg_in, skb, sync_size, rcd_sn);
  337. put_sg:
  338. while (resync_sgs)
  339. put_page(sg_page(&sg_in[--resync_sgs]));
  340. kfree(sg_in);
  341. free_orig:
  342. kfree_skb(skb);
  343. return nskb;
  344. }
  345. struct sk_buff *tls_validate_xmit_skb(struct sock *sk,
  346. struct net_device *dev,
  347. struct sk_buff *skb)
  348. {
  349. if (dev == tls_get_ctx(sk)->netdev)
  350. return skb;
  351. return tls_sw_fallback(sk, skb);
  352. }
  353. EXPORT_SYMBOL_GPL(tls_validate_xmit_skb);
  354. int tls_sw_fallback_init(struct sock *sk,
  355. struct tls_offload_context_tx *offload_ctx,
  356. struct tls_crypto_info *crypto_info)
  357. {
  358. const u8 *key;
  359. int rc;
  360. offload_ctx->aead_send =
  361. crypto_alloc_aead("gcm(aes)", 0, CRYPTO_ALG_ASYNC);
  362. if (IS_ERR(offload_ctx->aead_send)) {
  363. rc = PTR_ERR(offload_ctx->aead_send);
  364. pr_err_ratelimited("crypto_alloc_aead failed rc=%d\n", rc);
  365. offload_ctx->aead_send = NULL;
  366. goto err_out;
  367. }
  368. key = ((struct tls12_crypto_info_aes_gcm_128 *)crypto_info)->key;
  369. rc = crypto_aead_setkey(offload_ctx->aead_send, key,
  370. TLS_CIPHER_AES_GCM_128_KEY_SIZE);
  371. if (rc)
  372. goto free_aead;
  373. rc = crypto_aead_setauthsize(offload_ctx->aead_send,
  374. TLS_CIPHER_AES_GCM_128_TAG_SIZE);
  375. if (rc)
  376. goto free_aead;
  377. return 0;
  378. free_aead:
  379. crypto_free_aead(offload_ctx->aead_send);
  380. err_out:
  381. return rc;
  382. }