tls_device_fallback.c 12 KB

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