tls_device.c 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438
  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 <crypto/aead.h>
  32. #include <linux/highmem.h>
  33. #include <linux/module.h>
  34. #include <linux/netdevice.h>
  35. #include <net/dst.h>
  36. #include <net/inet_connection_sock.h>
  37. #include <net/tcp.h>
  38. #include <net/tls.h>
  39. #include <linux/skbuff_ref.h>
  40. #include "tls.h"
  41. #include "trace.h"
  42. /* device_offload_lock is used to synchronize tls_dev_add
  43. * against NETDEV_DOWN notifications.
  44. */
  45. static DECLARE_RWSEM(device_offload_lock);
  46. static struct workqueue_struct *destruct_wq __read_mostly;
  47. static LIST_HEAD(tls_device_list);
  48. static LIST_HEAD(tls_device_down_list);
  49. static DEFINE_SPINLOCK(tls_device_lock);
  50. static struct page *dummy_page;
  51. static void tls_device_free_ctx(struct tls_context *ctx)
  52. {
  53. if (ctx->tx_conf == TLS_HW)
  54. kfree(tls_offload_ctx_tx(ctx));
  55. if (ctx->rx_conf == TLS_HW)
  56. kfree(tls_offload_ctx_rx(ctx));
  57. tls_ctx_free(NULL, ctx);
  58. }
  59. static void tls_device_tx_del_task(struct work_struct *work)
  60. {
  61. struct tls_offload_context_tx *offload_ctx =
  62. container_of(work, struct tls_offload_context_tx, destruct_work);
  63. struct tls_context *ctx = offload_ctx->ctx;
  64. struct net_device *netdev;
  65. /* Safe, because this is the destroy flow, refcount is 0, so
  66. * tls_device_down can't store this field in parallel.
  67. */
  68. netdev = rcu_dereference_protected(ctx->netdev,
  69. !refcount_read(&ctx->refcount));
  70. netdev->tlsdev_ops->tls_dev_del(netdev, ctx, TLS_OFFLOAD_CTX_DIR_TX);
  71. dev_put(netdev);
  72. ctx->netdev = NULL;
  73. tls_device_free_ctx(ctx);
  74. }
  75. static void tls_device_queue_ctx_destruction(struct tls_context *ctx)
  76. {
  77. struct net_device *netdev;
  78. unsigned long flags;
  79. bool async_cleanup;
  80. spin_lock_irqsave(&tls_device_lock, flags);
  81. if (unlikely(!refcount_dec_and_test(&ctx->refcount))) {
  82. spin_unlock_irqrestore(&tls_device_lock, flags);
  83. return;
  84. }
  85. list_del(&ctx->list); /* Remove from tls_device_list / tls_device_down_list */
  86. /* Safe, because this is the destroy flow, refcount is 0, so
  87. * tls_device_down can't store this field in parallel.
  88. */
  89. netdev = rcu_dereference_protected(ctx->netdev,
  90. !refcount_read(&ctx->refcount));
  91. async_cleanup = netdev && ctx->tx_conf == TLS_HW;
  92. if (async_cleanup) {
  93. struct tls_offload_context_tx *offload_ctx = tls_offload_ctx_tx(ctx);
  94. /* queue_work inside the spinlock
  95. * to make sure tls_device_down waits for that work.
  96. */
  97. queue_work(destruct_wq, &offload_ctx->destruct_work);
  98. }
  99. spin_unlock_irqrestore(&tls_device_lock, flags);
  100. if (!async_cleanup)
  101. tls_device_free_ctx(ctx);
  102. }
  103. /* We assume that the socket is already connected */
  104. static struct net_device *get_netdev_for_sock(struct sock *sk)
  105. {
  106. struct dst_entry *dst = sk_dst_get(sk);
  107. struct net_device *netdev = NULL;
  108. if (likely(dst)) {
  109. netdev = netdev_sk_get_lowest_dev(dst->dev, sk);
  110. dev_hold(netdev);
  111. }
  112. dst_release(dst);
  113. return netdev;
  114. }
  115. static void destroy_record(struct tls_record_info *record)
  116. {
  117. int i;
  118. for (i = 0; i < record->num_frags; i++)
  119. __skb_frag_unref(&record->frags[i], false);
  120. kfree(record);
  121. }
  122. static void delete_all_records(struct tls_offload_context_tx *offload_ctx)
  123. {
  124. struct tls_record_info *info, *temp;
  125. list_for_each_entry_safe(info, temp, &offload_ctx->records_list, list) {
  126. list_del(&info->list);
  127. destroy_record(info);
  128. }
  129. offload_ctx->retransmit_hint = NULL;
  130. }
  131. static void tls_icsk_clean_acked(struct sock *sk, u32 acked_seq)
  132. {
  133. struct tls_context *tls_ctx = tls_get_ctx(sk);
  134. struct tls_record_info *info, *temp;
  135. struct tls_offload_context_tx *ctx;
  136. u64 deleted_records = 0;
  137. unsigned long flags;
  138. if (!tls_ctx)
  139. return;
  140. ctx = tls_offload_ctx_tx(tls_ctx);
  141. spin_lock_irqsave(&ctx->lock, flags);
  142. info = ctx->retransmit_hint;
  143. if (info && !before(acked_seq, info->end_seq))
  144. ctx->retransmit_hint = NULL;
  145. list_for_each_entry_safe(info, temp, &ctx->records_list, list) {
  146. if (before(acked_seq, info->end_seq))
  147. break;
  148. list_del(&info->list);
  149. destroy_record(info);
  150. deleted_records++;
  151. }
  152. ctx->unacked_record_sn += deleted_records;
  153. spin_unlock_irqrestore(&ctx->lock, flags);
  154. }
  155. /* At this point, there should be no references on this
  156. * socket and no in-flight SKBs associated with this
  157. * socket, so it is safe to free all the resources.
  158. */
  159. void tls_device_sk_destruct(struct sock *sk)
  160. {
  161. struct tls_context *tls_ctx = tls_get_ctx(sk);
  162. struct tls_offload_context_tx *ctx = tls_offload_ctx_tx(tls_ctx);
  163. tls_ctx->sk_destruct(sk);
  164. if (tls_ctx->tx_conf == TLS_HW) {
  165. if (ctx->open_record)
  166. destroy_record(ctx->open_record);
  167. delete_all_records(ctx);
  168. crypto_free_aead(ctx->aead_send);
  169. clean_acked_data_disable(inet_csk(sk));
  170. }
  171. tls_device_queue_ctx_destruction(tls_ctx);
  172. }
  173. EXPORT_SYMBOL_GPL(tls_device_sk_destruct);
  174. void tls_device_free_resources_tx(struct sock *sk)
  175. {
  176. struct tls_context *tls_ctx = tls_get_ctx(sk);
  177. tls_free_partial_record(sk, tls_ctx);
  178. }
  179. void tls_offload_tx_resync_request(struct sock *sk, u32 got_seq, u32 exp_seq)
  180. {
  181. struct tls_context *tls_ctx = tls_get_ctx(sk);
  182. trace_tls_device_tx_resync_req(sk, got_seq, exp_seq);
  183. WARN_ON(test_and_set_bit(TLS_TX_SYNC_SCHED, &tls_ctx->flags));
  184. }
  185. EXPORT_SYMBOL_GPL(tls_offload_tx_resync_request);
  186. static void tls_device_resync_tx(struct sock *sk, struct tls_context *tls_ctx,
  187. u32 seq)
  188. {
  189. struct net_device *netdev;
  190. int err = 0;
  191. u8 *rcd_sn;
  192. tcp_write_collapse_fence(sk);
  193. rcd_sn = tls_ctx->tx.rec_seq;
  194. trace_tls_device_tx_resync_send(sk, seq, rcd_sn);
  195. down_read(&device_offload_lock);
  196. netdev = rcu_dereference_protected(tls_ctx->netdev,
  197. lockdep_is_held(&device_offload_lock));
  198. if (netdev)
  199. err = netdev->tlsdev_ops->tls_dev_resync(netdev, sk, seq,
  200. rcd_sn,
  201. TLS_OFFLOAD_CTX_DIR_TX);
  202. up_read(&device_offload_lock);
  203. if (err)
  204. return;
  205. clear_bit_unlock(TLS_TX_SYNC_SCHED, &tls_ctx->flags);
  206. }
  207. static void tls_append_frag(struct tls_record_info *record,
  208. struct page_frag *pfrag,
  209. int size)
  210. {
  211. skb_frag_t *frag;
  212. frag = &record->frags[record->num_frags - 1];
  213. if (skb_frag_page(frag) == pfrag->page &&
  214. skb_frag_off(frag) + skb_frag_size(frag) == pfrag->offset) {
  215. skb_frag_size_add(frag, size);
  216. } else {
  217. ++frag;
  218. skb_frag_fill_page_desc(frag, pfrag->page, pfrag->offset,
  219. size);
  220. ++record->num_frags;
  221. get_page(pfrag->page);
  222. }
  223. pfrag->offset += size;
  224. record->len += size;
  225. }
  226. static int tls_push_record(struct sock *sk,
  227. struct tls_context *ctx,
  228. struct tls_offload_context_tx *offload_ctx,
  229. struct tls_record_info *record,
  230. int flags)
  231. {
  232. struct tls_prot_info *prot = &ctx->prot_info;
  233. struct tcp_sock *tp = tcp_sk(sk);
  234. skb_frag_t *frag;
  235. int i;
  236. record->end_seq = tp->write_seq + record->len;
  237. list_add_tail_rcu(&record->list, &offload_ctx->records_list);
  238. offload_ctx->open_record = NULL;
  239. if (test_bit(TLS_TX_SYNC_SCHED, &ctx->flags))
  240. tls_device_resync_tx(sk, ctx, tp->write_seq);
  241. tls_advance_record_sn(sk, prot, &ctx->tx);
  242. for (i = 0; i < record->num_frags; i++) {
  243. frag = &record->frags[i];
  244. sg_unmark_end(&offload_ctx->sg_tx_data[i]);
  245. sg_set_page(&offload_ctx->sg_tx_data[i], skb_frag_page(frag),
  246. skb_frag_size(frag), skb_frag_off(frag));
  247. sk_mem_charge(sk, skb_frag_size(frag));
  248. get_page(skb_frag_page(frag));
  249. }
  250. sg_mark_end(&offload_ctx->sg_tx_data[record->num_frags - 1]);
  251. /* all ready, send */
  252. return tls_push_sg(sk, ctx, offload_ctx->sg_tx_data, 0, flags);
  253. }
  254. static void tls_device_record_close(struct sock *sk,
  255. struct tls_context *ctx,
  256. struct tls_record_info *record,
  257. struct page_frag *pfrag,
  258. unsigned char record_type)
  259. {
  260. struct tls_prot_info *prot = &ctx->prot_info;
  261. struct page_frag dummy_tag_frag;
  262. /* append tag
  263. * device will fill in the tag, we just need to append a placeholder
  264. * use socket memory to improve coalescing (re-using a single buffer
  265. * increases frag count)
  266. * if we can't allocate memory now use the dummy page
  267. */
  268. if (unlikely(pfrag->size - pfrag->offset < prot->tag_size) &&
  269. !skb_page_frag_refill(prot->tag_size, pfrag, sk->sk_allocation)) {
  270. dummy_tag_frag.page = dummy_page;
  271. dummy_tag_frag.offset = 0;
  272. pfrag = &dummy_tag_frag;
  273. }
  274. tls_append_frag(record, pfrag, prot->tag_size);
  275. /* fill prepend */
  276. tls_fill_prepend(ctx, skb_frag_address(&record->frags[0]),
  277. record->len - prot->overhead_size,
  278. record_type);
  279. }
  280. static int tls_create_new_record(struct tls_offload_context_tx *offload_ctx,
  281. struct page_frag *pfrag,
  282. size_t prepend_size)
  283. {
  284. struct tls_record_info *record;
  285. skb_frag_t *frag;
  286. record = kmalloc(sizeof(*record), GFP_KERNEL);
  287. if (!record)
  288. return -ENOMEM;
  289. frag = &record->frags[0];
  290. skb_frag_fill_page_desc(frag, pfrag->page, pfrag->offset,
  291. prepend_size);
  292. get_page(pfrag->page);
  293. pfrag->offset += prepend_size;
  294. record->num_frags = 1;
  295. record->len = prepend_size;
  296. offload_ctx->open_record = record;
  297. return 0;
  298. }
  299. static int tls_do_allocation(struct sock *sk,
  300. struct tls_offload_context_tx *offload_ctx,
  301. struct page_frag *pfrag,
  302. size_t prepend_size)
  303. {
  304. int ret;
  305. if (!offload_ctx->open_record) {
  306. if (unlikely(!skb_page_frag_refill(prepend_size, pfrag,
  307. sk->sk_allocation))) {
  308. READ_ONCE(sk->sk_prot)->enter_memory_pressure(sk);
  309. sk_stream_moderate_sndbuf(sk);
  310. return -ENOMEM;
  311. }
  312. ret = tls_create_new_record(offload_ctx, pfrag, prepend_size);
  313. if (ret)
  314. return ret;
  315. if (pfrag->size > pfrag->offset)
  316. return 0;
  317. }
  318. if (!sk_page_frag_refill(sk, pfrag))
  319. return -ENOMEM;
  320. return 0;
  321. }
  322. static int tls_device_copy_data(void *addr, size_t bytes, struct iov_iter *i)
  323. {
  324. size_t pre_copy, nocache;
  325. pre_copy = ~((unsigned long)addr - 1) & (SMP_CACHE_BYTES - 1);
  326. if (pre_copy) {
  327. pre_copy = min(pre_copy, bytes);
  328. if (copy_from_iter(addr, pre_copy, i) != pre_copy)
  329. return -EFAULT;
  330. bytes -= pre_copy;
  331. addr += pre_copy;
  332. }
  333. nocache = round_down(bytes, SMP_CACHE_BYTES);
  334. if (copy_from_iter_nocache(addr, nocache, i) != nocache)
  335. return -EFAULT;
  336. bytes -= nocache;
  337. addr += nocache;
  338. if (bytes && copy_from_iter(addr, bytes, i) != bytes)
  339. return -EFAULT;
  340. return 0;
  341. }
  342. static int tls_push_data(struct sock *sk,
  343. struct iov_iter *iter,
  344. size_t size, int flags,
  345. unsigned char record_type)
  346. {
  347. struct tls_context *tls_ctx = tls_get_ctx(sk);
  348. struct tls_prot_info *prot = &tls_ctx->prot_info;
  349. struct tls_offload_context_tx *ctx = tls_offload_ctx_tx(tls_ctx);
  350. struct tls_record_info *record;
  351. int tls_push_record_flags;
  352. struct page_frag *pfrag;
  353. size_t orig_size = size;
  354. u32 max_open_record_len;
  355. bool more = false;
  356. bool done = false;
  357. int copy, rc = 0;
  358. long timeo;
  359. if (flags &
  360. ~(MSG_MORE | MSG_DONTWAIT | MSG_NOSIGNAL |
  361. MSG_SPLICE_PAGES | MSG_EOR))
  362. return -EOPNOTSUPP;
  363. if ((flags & (MSG_MORE | MSG_EOR)) == (MSG_MORE | MSG_EOR))
  364. return -EINVAL;
  365. if (unlikely(sk->sk_err))
  366. return -sk->sk_err;
  367. flags |= MSG_SENDPAGE_DECRYPTED;
  368. tls_push_record_flags = flags | MSG_MORE;
  369. timeo = sock_sndtimeo(sk, flags & MSG_DONTWAIT);
  370. if (tls_is_partially_sent_record(tls_ctx)) {
  371. rc = tls_push_partial_record(sk, tls_ctx, flags);
  372. if (rc < 0)
  373. return rc;
  374. }
  375. pfrag = sk_page_frag(sk);
  376. /* TLS_HEADER_SIZE is not counted as part of the TLS record, and
  377. * we need to leave room for an authentication tag.
  378. */
  379. max_open_record_len = TLS_MAX_PAYLOAD_SIZE +
  380. prot->prepend_size;
  381. do {
  382. rc = tls_do_allocation(sk, ctx, pfrag, prot->prepend_size);
  383. if (unlikely(rc)) {
  384. rc = sk_stream_wait_memory(sk, &timeo);
  385. if (!rc)
  386. continue;
  387. record = ctx->open_record;
  388. if (!record)
  389. break;
  390. handle_error:
  391. if (record_type != TLS_RECORD_TYPE_DATA) {
  392. /* avoid sending partial
  393. * record with type !=
  394. * application_data
  395. */
  396. size = orig_size;
  397. destroy_record(record);
  398. ctx->open_record = NULL;
  399. } else if (record->len > prot->prepend_size) {
  400. goto last_record;
  401. }
  402. break;
  403. }
  404. record = ctx->open_record;
  405. copy = min_t(size_t, size, max_open_record_len - record->len);
  406. if (copy && (flags & MSG_SPLICE_PAGES)) {
  407. struct page_frag zc_pfrag;
  408. struct page **pages = &zc_pfrag.page;
  409. size_t off;
  410. rc = iov_iter_extract_pages(iter, &pages,
  411. copy, 1, 0, &off);
  412. if (rc <= 0) {
  413. if (rc == 0)
  414. rc = -EIO;
  415. goto handle_error;
  416. }
  417. copy = rc;
  418. if (WARN_ON_ONCE(!sendpage_ok(zc_pfrag.page))) {
  419. iov_iter_revert(iter, copy);
  420. rc = -EIO;
  421. goto handle_error;
  422. }
  423. zc_pfrag.offset = off;
  424. zc_pfrag.size = copy;
  425. tls_append_frag(record, &zc_pfrag, copy);
  426. } else if (copy) {
  427. copy = min_t(size_t, copy, pfrag->size - pfrag->offset);
  428. rc = tls_device_copy_data(page_address(pfrag->page) +
  429. pfrag->offset, copy,
  430. iter);
  431. if (rc)
  432. goto handle_error;
  433. tls_append_frag(record, pfrag, copy);
  434. }
  435. size -= copy;
  436. if (!size) {
  437. last_record:
  438. tls_push_record_flags = flags;
  439. if (flags & MSG_MORE) {
  440. more = true;
  441. break;
  442. }
  443. done = true;
  444. }
  445. if (done || record->len >= max_open_record_len ||
  446. (record->num_frags >= MAX_SKB_FRAGS - 1)) {
  447. tls_device_record_close(sk, tls_ctx, record,
  448. pfrag, record_type);
  449. rc = tls_push_record(sk,
  450. tls_ctx,
  451. ctx,
  452. record,
  453. tls_push_record_flags);
  454. if (rc < 0)
  455. break;
  456. }
  457. } while (!done);
  458. tls_ctx->pending_open_record_frags = more;
  459. if (orig_size - size > 0)
  460. rc = orig_size - size;
  461. return rc;
  462. }
  463. int tls_device_sendmsg(struct sock *sk, struct msghdr *msg, size_t size)
  464. {
  465. unsigned char record_type = TLS_RECORD_TYPE_DATA;
  466. struct tls_context *tls_ctx = tls_get_ctx(sk);
  467. int rc;
  468. if (!tls_ctx->zerocopy_sendfile)
  469. msg->msg_flags &= ~MSG_SPLICE_PAGES;
  470. mutex_lock(&tls_ctx->tx_lock);
  471. lock_sock(sk);
  472. if (unlikely(msg->msg_controllen)) {
  473. rc = tls_process_cmsg(sk, msg, &record_type);
  474. if (rc)
  475. goto out;
  476. }
  477. rc = tls_push_data(sk, &msg->msg_iter, size, msg->msg_flags,
  478. record_type);
  479. out:
  480. release_sock(sk);
  481. mutex_unlock(&tls_ctx->tx_lock);
  482. return rc;
  483. }
  484. void tls_device_splice_eof(struct socket *sock)
  485. {
  486. struct sock *sk = sock->sk;
  487. struct tls_context *tls_ctx = tls_get_ctx(sk);
  488. struct iov_iter iter = {};
  489. if (!tls_is_partially_sent_record(tls_ctx))
  490. return;
  491. mutex_lock(&tls_ctx->tx_lock);
  492. lock_sock(sk);
  493. if (tls_is_partially_sent_record(tls_ctx)) {
  494. iov_iter_bvec(&iter, ITER_SOURCE, NULL, 0, 0);
  495. tls_push_data(sk, &iter, 0, 0, TLS_RECORD_TYPE_DATA);
  496. }
  497. release_sock(sk);
  498. mutex_unlock(&tls_ctx->tx_lock);
  499. }
  500. struct tls_record_info *tls_get_record(struct tls_offload_context_tx *context,
  501. u32 seq, u64 *p_record_sn)
  502. {
  503. u64 record_sn = context->hint_record_sn;
  504. struct tls_record_info *info, *last;
  505. info = context->retransmit_hint;
  506. if (!info ||
  507. before(seq, info->end_seq - info->len)) {
  508. /* if retransmit_hint is irrelevant start
  509. * from the beginning of the list
  510. */
  511. info = list_first_entry_or_null(&context->records_list,
  512. struct tls_record_info, list);
  513. if (!info)
  514. return NULL;
  515. /* send the start_marker record if seq number is before the
  516. * tls offload start marker sequence number. This record is
  517. * required to handle TCP packets which are before TLS offload
  518. * started.
  519. * And if it's not start marker, look if this seq number
  520. * belongs to the list.
  521. */
  522. if (likely(!tls_record_is_start_marker(info))) {
  523. /* we have the first record, get the last record to see
  524. * if this seq number belongs to the list.
  525. */
  526. last = list_last_entry(&context->records_list,
  527. struct tls_record_info, list);
  528. if (!between(seq, tls_record_start_seq(info),
  529. last->end_seq))
  530. return NULL;
  531. }
  532. record_sn = context->unacked_record_sn;
  533. }
  534. /* We just need the _rcu for the READ_ONCE() */
  535. rcu_read_lock();
  536. list_for_each_entry_from_rcu(info, &context->records_list, list) {
  537. if (before(seq, info->end_seq)) {
  538. if (!context->retransmit_hint ||
  539. after(info->end_seq,
  540. context->retransmit_hint->end_seq)) {
  541. context->hint_record_sn = record_sn;
  542. context->retransmit_hint = info;
  543. }
  544. *p_record_sn = record_sn;
  545. goto exit_rcu_unlock;
  546. }
  547. record_sn++;
  548. }
  549. info = NULL;
  550. exit_rcu_unlock:
  551. rcu_read_unlock();
  552. return info;
  553. }
  554. EXPORT_SYMBOL(tls_get_record);
  555. static int tls_device_push_pending_record(struct sock *sk, int flags)
  556. {
  557. struct iov_iter iter;
  558. iov_iter_kvec(&iter, ITER_SOURCE, NULL, 0, 0);
  559. return tls_push_data(sk, &iter, 0, flags, TLS_RECORD_TYPE_DATA);
  560. }
  561. void tls_device_write_space(struct sock *sk, struct tls_context *ctx)
  562. {
  563. if (tls_is_partially_sent_record(ctx)) {
  564. gfp_t sk_allocation = sk->sk_allocation;
  565. WARN_ON_ONCE(sk->sk_write_pending);
  566. sk->sk_allocation = GFP_ATOMIC;
  567. tls_push_partial_record(sk, ctx,
  568. MSG_DONTWAIT | MSG_NOSIGNAL |
  569. MSG_SENDPAGE_DECRYPTED);
  570. sk->sk_allocation = sk_allocation;
  571. }
  572. }
  573. static void tls_device_resync_rx(struct tls_context *tls_ctx,
  574. struct sock *sk, u32 seq, u8 *rcd_sn)
  575. {
  576. struct tls_offload_context_rx *rx_ctx = tls_offload_ctx_rx(tls_ctx);
  577. struct net_device *netdev;
  578. trace_tls_device_rx_resync_send(sk, seq, rcd_sn, rx_ctx->resync_type);
  579. rcu_read_lock();
  580. netdev = rcu_dereference(tls_ctx->netdev);
  581. if (netdev)
  582. netdev->tlsdev_ops->tls_dev_resync(netdev, sk, seq, rcd_sn,
  583. TLS_OFFLOAD_CTX_DIR_RX);
  584. rcu_read_unlock();
  585. TLS_INC_STATS(sock_net(sk), LINUX_MIB_TLSRXDEVICERESYNC);
  586. }
  587. static bool
  588. tls_device_rx_resync_async(struct tls_offload_resync_async *resync_async,
  589. s64 resync_req, u32 *seq, u16 *rcd_delta)
  590. {
  591. u32 is_async = resync_req & RESYNC_REQ_ASYNC;
  592. u32 req_seq = resync_req >> 32;
  593. u32 req_end = req_seq + ((resync_req >> 16) & 0xffff);
  594. u16 i;
  595. *rcd_delta = 0;
  596. if (is_async) {
  597. /* shouldn't get to wraparound:
  598. * too long in async stage, something bad happened
  599. */
  600. if (WARN_ON_ONCE(resync_async->rcd_delta == USHRT_MAX))
  601. return false;
  602. /* asynchronous stage: log all headers seq such that
  603. * req_seq <= seq <= end_seq, and wait for real resync request
  604. */
  605. if (before(*seq, req_seq))
  606. return false;
  607. if (!after(*seq, req_end) &&
  608. resync_async->loglen < TLS_DEVICE_RESYNC_ASYNC_LOGMAX)
  609. resync_async->log[resync_async->loglen++] = *seq;
  610. resync_async->rcd_delta++;
  611. return false;
  612. }
  613. /* synchronous stage: check against the logged entries and
  614. * proceed to check the next entries if no match was found
  615. */
  616. for (i = 0; i < resync_async->loglen; i++)
  617. if (req_seq == resync_async->log[i] &&
  618. atomic64_try_cmpxchg(&resync_async->req, &resync_req, 0)) {
  619. *rcd_delta = resync_async->rcd_delta - i;
  620. *seq = req_seq;
  621. resync_async->loglen = 0;
  622. resync_async->rcd_delta = 0;
  623. return true;
  624. }
  625. resync_async->loglen = 0;
  626. resync_async->rcd_delta = 0;
  627. if (req_seq == *seq &&
  628. atomic64_try_cmpxchg(&resync_async->req,
  629. &resync_req, 0))
  630. return true;
  631. return false;
  632. }
  633. void tls_device_rx_resync_new_rec(struct sock *sk, u32 rcd_len, u32 seq)
  634. {
  635. struct tls_context *tls_ctx = tls_get_ctx(sk);
  636. struct tls_offload_context_rx *rx_ctx;
  637. u8 rcd_sn[TLS_MAX_REC_SEQ_SIZE];
  638. u32 sock_data, is_req_pending;
  639. struct tls_prot_info *prot;
  640. s64 resync_req;
  641. u16 rcd_delta;
  642. u32 req_seq;
  643. if (tls_ctx->rx_conf != TLS_HW)
  644. return;
  645. if (unlikely(test_bit(TLS_RX_DEV_DEGRADED, &tls_ctx->flags)))
  646. return;
  647. prot = &tls_ctx->prot_info;
  648. rx_ctx = tls_offload_ctx_rx(tls_ctx);
  649. memcpy(rcd_sn, tls_ctx->rx.rec_seq, prot->rec_seq_size);
  650. switch (rx_ctx->resync_type) {
  651. case TLS_OFFLOAD_SYNC_TYPE_DRIVER_REQ:
  652. resync_req = atomic64_read(&rx_ctx->resync_req);
  653. req_seq = resync_req >> 32;
  654. seq += TLS_HEADER_SIZE - 1;
  655. is_req_pending = resync_req;
  656. if (likely(!is_req_pending) || req_seq != seq ||
  657. !atomic64_try_cmpxchg(&rx_ctx->resync_req, &resync_req, 0))
  658. return;
  659. break;
  660. case TLS_OFFLOAD_SYNC_TYPE_CORE_NEXT_HINT:
  661. if (likely(!rx_ctx->resync_nh_do_now))
  662. return;
  663. /* head of next rec is already in, note that the sock_inq will
  664. * include the currently parsed message when called from parser
  665. */
  666. sock_data = tcp_inq(sk);
  667. if (sock_data > rcd_len) {
  668. trace_tls_device_rx_resync_nh_delay(sk, sock_data,
  669. rcd_len);
  670. return;
  671. }
  672. rx_ctx->resync_nh_do_now = 0;
  673. seq += rcd_len;
  674. tls_bigint_increment(rcd_sn, prot->rec_seq_size);
  675. break;
  676. case TLS_OFFLOAD_SYNC_TYPE_DRIVER_REQ_ASYNC:
  677. resync_req = atomic64_read(&rx_ctx->resync_async->req);
  678. is_req_pending = resync_req;
  679. if (likely(!is_req_pending))
  680. return;
  681. if (!tls_device_rx_resync_async(rx_ctx->resync_async,
  682. resync_req, &seq, &rcd_delta))
  683. return;
  684. tls_bigint_subtract(rcd_sn, rcd_delta);
  685. break;
  686. }
  687. tls_device_resync_rx(tls_ctx, sk, seq, rcd_sn);
  688. }
  689. static void tls_device_core_ctrl_rx_resync(struct tls_context *tls_ctx,
  690. struct tls_offload_context_rx *ctx,
  691. struct sock *sk, struct sk_buff *skb)
  692. {
  693. struct strp_msg *rxm;
  694. /* device will request resyncs by itself based on stream scan */
  695. if (ctx->resync_type != TLS_OFFLOAD_SYNC_TYPE_CORE_NEXT_HINT)
  696. return;
  697. /* already scheduled */
  698. if (ctx->resync_nh_do_now)
  699. return;
  700. /* seen decrypted fragments since last fully-failed record */
  701. if (ctx->resync_nh_reset) {
  702. ctx->resync_nh_reset = 0;
  703. ctx->resync_nh.decrypted_failed = 1;
  704. ctx->resync_nh.decrypted_tgt = TLS_DEVICE_RESYNC_NH_START_IVAL;
  705. return;
  706. }
  707. if (++ctx->resync_nh.decrypted_failed <= ctx->resync_nh.decrypted_tgt)
  708. return;
  709. /* doing resync, bump the next target in case it fails */
  710. if (ctx->resync_nh.decrypted_tgt < TLS_DEVICE_RESYNC_NH_MAX_IVAL)
  711. ctx->resync_nh.decrypted_tgt *= 2;
  712. else
  713. ctx->resync_nh.decrypted_tgt += TLS_DEVICE_RESYNC_NH_MAX_IVAL;
  714. rxm = strp_msg(skb);
  715. /* head of next rec is already in, parser will sync for us */
  716. if (tcp_inq(sk) > rxm->full_len) {
  717. trace_tls_device_rx_resync_nh_schedule(sk);
  718. ctx->resync_nh_do_now = 1;
  719. } else {
  720. struct tls_prot_info *prot = &tls_ctx->prot_info;
  721. u8 rcd_sn[TLS_MAX_REC_SEQ_SIZE];
  722. memcpy(rcd_sn, tls_ctx->rx.rec_seq, prot->rec_seq_size);
  723. tls_bigint_increment(rcd_sn, prot->rec_seq_size);
  724. tls_device_resync_rx(tls_ctx, sk, tcp_sk(sk)->copied_seq,
  725. rcd_sn);
  726. }
  727. }
  728. static int
  729. tls_device_reencrypt(struct sock *sk, struct tls_context *tls_ctx)
  730. {
  731. struct tls_sw_context_rx *sw_ctx = tls_sw_ctx_rx(tls_ctx);
  732. const struct tls_cipher_desc *cipher_desc;
  733. int err, offset, copy, data_len, pos;
  734. struct sk_buff *skb, *skb_iter;
  735. struct scatterlist sg[1];
  736. struct strp_msg *rxm;
  737. char *orig_buf, *buf;
  738. cipher_desc = get_cipher_desc(tls_ctx->crypto_recv.info.cipher_type);
  739. DEBUG_NET_WARN_ON_ONCE(!cipher_desc || !cipher_desc->offloadable);
  740. rxm = strp_msg(tls_strp_msg(sw_ctx));
  741. orig_buf = kmalloc(rxm->full_len + TLS_HEADER_SIZE + cipher_desc->iv,
  742. sk->sk_allocation);
  743. if (!orig_buf)
  744. return -ENOMEM;
  745. buf = orig_buf;
  746. err = tls_strp_msg_cow(sw_ctx);
  747. if (unlikely(err))
  748. goto free_buf;
  749. skb = tls_strp_msg(sw_ctx);
  750. rxm = strp_msg(skb);
  751. offset = rxm->offset;
  752. sg_init_table(sg, 1);
  753. sg_set_buf(&sg[0], buf,
  754. rxm->full_len + TLS_HEADER_SIZE + cipher_desc->iv);
  755. err = skb_copy_bits(skb, offset, buf, TLS_HEADER_SIZE + cipher_desc->iv);
  756. if (err)
  757. goto free_buf;
  758. /* We are interested only in the decrypted data not the auth */
  759. err = decrypt_skb(sk, sg);
  760. if (err != -EBADMSG)
  761. goto free_buf;
  762. else
  763. err = 0;
  764. data_len = rxm->full_len - cipher_desc->tag;
  765. if (skb_pagelen(skb) > offset) {
  766. copy = min_t(int, skb_pagelen(skb) - offset, data_len);
  767. if (skb->decrypted) {
  768. err = skb_store_bits(skb, offset, buf, copy);
  769. if (err)
  770. goto free_buf;
  771. }
  772. offset += copy;
  773. buf += copy;
  774. }
  775. pos = skb_pagelen(skb);
  776. skb_walk_frags(skb, skb_iter) {
  777. int frag_pos;
  778. /* Practically all frags must belong to msg if reencrypt
  779. * is needed with current strparser and coalescing logic,
  780. * but strparser may "get optimized", so let's be safe.
  781. */
  782. if (pos + skb_iter->len <= offset)
  783. goto done_with_frag;
  784. if (pos >= data_len + rxm->offset)
  785. break;
  786. frag_pos = offset - pos;
  787. copy = min_t(int, skb_iter->len - frag_pos,
  788. data_len + rxm->offset - offset);
  789. if (skb_iter->decrypted) {
  790. err = skb_store_bits(skb_iter, frag_pos, buf, copy);
  791. if (err)
  792. goto free_buf;
  793. }
  794. offset += copy;
  795. buf += copy;
  796. done_with_frag:
  797. pos += skb_iter->len;
  798. }
  799. free_buf:
  800. kfree(orig_buf);
  801. return err;
  802. }
  803. int tls_device_decrypted(struct sock *sk, struct tls_context *tls_ctx)
  804. {
  805. struct tls_offload_context_rx *ctx = tls_offload_ctx_rx(tls_ctx);
  806. struct tls_sw_context_rx *sw_ctx = tls_sw_ctx_rx(tls_ctx);
  807. struct sk_buff *skb = tls_strp_msg(sw_ctx);
  808. struct strp_msg *rxm = strp_msg(skb);
  809. int is_decrypted, is_encrypted;
  810. if (!tls_strp_msg_mixed_decrypted(sw_ctx)) {
  811. is_decrypted = skb->decrypted;
  812. is_encrypted = !is_decrypted;
  813. } else {
  814. is_decrypted = 0;
  815. is_encrypted = 0;
  816. }
  817. trace_tls_device_decrypted(sk, tcp_sk(sk)->copied_seq - rxm->full_len,
  818. tls_ctx->rx.rec_seq, rxm->full_len,
  819. is_encrypted, is_decrypted);
  820. if (unlikely(test_bit(TLS_RX_DEV_DEGRADED, &tls_ctx->flags))) {
  821. if (likely(is_encrypted || is_decrypted))
  822. return is_decrypted;
  823. /* After tls_device_down disables the offload, the next SKB will
  824. * likely have initial fragments decrypted, and final ones not
  825. * decrypted. We need to reencrypt that single SKB.
  826. */
  827. return tls_device_reencrypt(sk, tls_ctx);
  828. }
  829. /* Return immediately if the record is either entirely plaintext or
  830. * entirely ciphertext. Otherwise handle reencrypt partially decrypted
  831. * record.
  832. */
  833. if (is_decrypted) {
  834. ctx->resync_nh_reset = 1;
  835. return is_decrypted;
  836. }
  837. if (is_encrypted) {
  838. tls_device_core_ctrl_rx_resync(tls_ctx, ctx, sk, skb);
  839. return 0;
  840. }
  841. ctx->resync_nh_reset = 1;
  842. return tls_device_reencrypt(sk, tls_ctx);
  843. }
  844. static void tls_device_attach(struct tls_context *ctx, struct sock *sk,
  845. struct net_device *netdev)
  846. {
  847. if (sk->sk_destruct != tls_device_sk_destruct) {
  848. refcount_set(&ctx->refcount, 1);
  849. dev_hold(netdev);
  850. RCU_INIT_POINTER(ctx->netdev, netdev);
  851. spin_lock_irq(&tls_device_lock);
  852. list_add_tail(&ctx->list, &tls_device_list);
  853. spin_unlock_irq(&tls_device_lock);
  854. ctx->sk_destruct = sk->sk_destruct;
  855. smp_store_release(&sk->sk_destruct, tls_device_sk_destruct);
  856. }
  857. }
  858. static struct tls_offload_context_tx *alloc_offload_ctx_tx(struct tls_context *ctx)
  859. {
  860. struct tls_offload_context_tx *offload_ctx;
  861. __be64 rcd_sn;
  862. offload_ctx = kzalloc(sizeof(*offload_ctx), GFP_KERNEL);
  863. if (!offload_ctx)
  864. return NULL;
  865. INIT_WORK(&offload_ctx->destruct_work, tls_device_tx_del_task);
  866. INIT_LIST_HEAD(&offload_ctx->records_list);
  867. spin_lock_init(&offload_ctx->lock);
  868. sg_init_table(offload_ctx->sg_tx_data,
  869. ARRAY_SIZE(offload_ctx->sg_tx_data));
  870. /* start at rec_seq - 1 to account for the start marker record */
  871. memcpy(&rcd_sn, ctx->tx.rec_seq, sizeof(rcd_sn));
  872. offload_ctx->unacked_record_sn = be64_to_cpu(rcd_sn) - 1;
  873. offload_ctx->ctx = ctx;
  874. return offload_ctx;
  875. }
  876. int tls_set_device_offload(struct sock *sk)
  877. {
  878. struct tls_record_info *start_marker_record;
  879. struct tls_offload_context_tx *offload_ctx;
  880. const struct tls_cipher_desc *cipher_desc;
  881. struct tls_crypto_info *crypto_info;
  882. struct tls_prot_info *prot;
  883. struct net_device *netdev;
  884. struct tls_context *ctx;
  885. char *iv, *rec_seq;
  886. int rc;
  887. ctx = tls_get_ctx(sk);
  888. prot = &ctx->prot_info;
  889. if (ctx->priv_ctx_tx)
  890. return -EEXIST;
  891. netdev = get_netdev_for_sock(sk);
  892. if (!netdev) {
  893. pr_err_ratelimited("%s: netdev not found\n", __func__);
  894. return -EINVAL;
  895. }
  896. if (!(netdev->features & NETIF_F_HW_TLS_TX)) {
  897. rc = -EOPNOTSUPP;
  898. goto release_netdev;
  899. }
  900. crypto_info = &ctx->crypto_send.info;
  901. if (crypto_info->version != TLS_1_2_VERSION) {
  902. rc = -EOPNOTSUPP;
  903. goto release_netdev;
  904. }
  905. cipher_desc = get_cipher_desc(crypto_info->cipher_type);
  906. if (!cipher_desc || !cipher_desc->offloadable) {
  907. rc = -EINVAL;
  908. goto release_netdev;
  909. }
  910. rc = init_prot_info(prot, crypto_info, cipher_desc);
  911. if (rc)
  912. goto release_netdev;
  913. iv = crypto_info_iv(crypto_info, cipher_desc);
  914. rec_seq = crypto_info_rec_seq(crypto_info, cipher_desc);
  915. memcpy(ctx->tx.iv + cipher_desc->salt, iv, cipher_desc->iv);
  916. memcpy(ctx->tx.rec_seq, rec_seq, cipher_desc->rec_seq);
  917. start_marker_record = kmalloc(sizeof(*start_marker_record), GFP_KERNEL);
  918. if (!start_marker_record) {
  919. rc = -ENOMEM;
  920. goto release_netdev;
  921. }
  922. offload_ctx = alloc_offload_ctx_tx(ctx);
  923. if (!offload_ctx) {
  924. rc = -ENOMEM;
  925. goto free_marker_record;
  926. }
  927. rc = tls_sw_fallback_init(sk, offload_ctx, crypto_info);
  928. if (rc)
  929. goto free_offload_ctx;
  930. start_marker_record->end_seq = tcp_sk(sk)->write_seq;
  931. start_marker_record->len = 0;
  932. start_marker_record->num_frags = 0;
  933. list_add_tail(&start_marker_record->list, &offload_ctx->records_list);
  934. clean_acked_data_enable(inet_csk(sk), &tls_icsk_clean_acked);
  935. ctx->push_pending_record = tls_device_push_pending_record;
  936. /* TLS offload is greatly simplified if we don't send
  937. * SKBs where only part of the payload needs to be encrypted.
  938. * So mark the last skb in the write queue as end of record.
  939. */
  940. tcp_write_collapse_fence(sk);
  941. /* Avoid offloading if the device is down
  942. * We don't want to offload new flows after
  943. * the NETDEV_DOWN event
  944. *
  945. * device_offload_lock is taken in tls_devices's NETDEV_DOWN
  946. * handler thus protecting from the device going down before
  947. * ctx was added to tls_device_list.
  948. */
  949. down_read(&device_offload_lock);
  950. if (!(netdev->flags & IFF_UP)) {
  951. rc = -EINVAL;
  952. goto release_lock;
  953. }
  954. ctx->priv_ctx_tx = offload_ctx;
  955. rc = netdev->tlsdev_ops->tls_dev_add(netdev, sk, TLS_OFFLOAD_CTX_DIR_TX,
  956. &ctx->crypto_send.info,
  957. tcp_sk(sk)->write_seq);
  958. trace_tls_device_offload_set(sk, TLS_OFFLOAD_CTX_DIR_TX,
  959. tcp_sk(sk)->write_seq, rec_seq, rc);
  960. if (rc)
  961. goto release_lock;
  962. tls_device_attach(ctx, sk, netdev);
  963. up_read(&device_offload_lock);
  964. /* following this assignment tls_is_skb_tx_device_offloaded
  965. * will return true and the context might be accessed
  966. * by the netdev's xmit function.
  967. */
  968. smp_store_release(&sk->sk_validate_xmit_skb, tls_validate_xmit_skb);
  969. dev_put(netdev);
  970. return 0;
  971. release_lock:
  972. up_read(&device_offload_lock);
  973. clean_acked_data_disable(inet_csk(sk));
  974. crypto_free_aead(offload_ctx->aead_send);
  975. free_offload_ctx:
  976. kfree(offload_ctx);
  977. ctx->priv_ctx_tx = NULL;
  978. free_marker_record:
  979. kfree(start_marker_record);
  980. release_netdev:
  981. dev_put(netdev);
  982. return rc;
  983. }
  984. int tls_set_device_offload_rx(struct sock *sk, struct tls_context *ctx)
  985. {
  986. struct tls12_crypto_info_aes_gcm_128 *info;
  987. struct tls_offload_context_rx *context;
  988. struct net_device *netdev;
  989. int rc = 0;
  990. if (ctx->crypto_recv.info.version != TLS_1_2_VERSION)
  991. return -EOPNOTSUPP;
  992. netdev = get_netdev_for_sock(sk);
  993. if (!netdev) {
  994. pr_err_ratelimited("%s: netdev not found\n", __func__);
  995. return -EINVAL;
  996. }
  997. if (!(netdev->features & NETIF_F_HW_TLS_RX)) {
  998. rc = -EOPNOTSUPP;
  999. goto release_netdev;
  1000. }
  1001. /* Avoid offloading if the device is down
  1002. * We don't want to offload new flows after
  1003. * the NETDEV_DOWN event
  1004. *
  1005. * device_offload_lock is taken in tls_devices's NETDEV_DOWN
  1006. * handler thus protecting from the device going down before
  1007. * ctx was added to tls_device_list.
  1008. */
  1009. down_read(&device_offload_lock);
  1010. if (!(netdev->flags & IFF_UP)) {
  1011. rc = -EINVAL;
  1012. goto release_lock;
  1013. }
  1014. context = kzalloc(sizeof(*context), GFP_KERNEL);
  1015. if (!context) {
  1016. rc = -ENOMEM;
  1017. goto release_lock;
  1018. }
  1019. context->resync_nh_reset = 1;
  1020. ctx->priv_ctx_rx = context;
  1021. rc = tls_set_sw_offload(sk, 0);
  1022. if (rc)
  1023. goto release_ctx;
  1024. rc = netdev->tlsdev_ops->tls_dev_add(netdev, sk, TLS_OFFLOAD_CTX_DIR_RX,
  1025. &ctx->crypto_recv.info,
  1026. tcp_sk(sk)->copied_seq);
  1027. info = (void *)&ctx->crypto_recv.info;
  1028. trace_tls_device_offload_set(sk, TLS_OFFLOAD_CTX_DIR_RX,
  1029. tcp_sk(sk)->copied_seq, info->rec_seq, rc);
  1030. if (rc)
  1031. goto free_sw_resources;
  1032. tls_device_attach(ctx, sk, netdev);
  1033. up_read(&device_offload_lock);
  1034. dev_put(netdev);
  1035. return 0;
  1036. free_sw_resources:
  1037. up_read(&device_offload_lock);
  1038. tls_sw_free_resources_rx(sk);
  1039. down_read(&device_offload_lock);
  1040. release_ctx:
  1041. ctx->priv_ctx_rx = NULL;
  1042. release_lock:
  1043. up_read(&device_offload_lock);
  1044. release_netdev:
  1045. dev_put(netdev);
  1046. return rc;
  1047. }
  1048. void tls_device_offload_cleanup_rx(struct sock *sk)
  1049. {
  1050. struct tls_context *tls_ctx = tls_get_ctx(sk);
  1051. struct net_device *netdev;
  1052. down_read(&device_offload_lock);
  1053. netdev = rcu_dereference_protected(tls_ctx->netdev,
  1054. lockdep_is_held(&device_offload_lock));
  1055. if (!netdev)
  1056. goto out;
  1057. netdev->tlsdev_ops->tls_dev_del(netdev, tls_ctx,
  1058. TLS_OFFLOAD_CTX_DIR_RX);
  1059. if (tls_ctx->tx_conf != TLS_HW) {
  1060. dev_put(netdev);
  1061. rcu_assign_pointer(tls_ctx->netdev, NULL);
  1062. } else {
  1063. set_bit(TLS_RX_DEV_CLOSED, &tls_ctx->flags);
  1064. }
  1065. out:
  1066. up_read(&device_offload_lock);
  1067. tls_sw_release_resources_rx(sk);
  1068. }
  1069. static int tls_device_down(struct net_device *netdev)
  1070. {
  1071. struct tls_context *ctx, *tmp;
  1072. unsigned long flags;
  1073. LIST_HEAD(list);
  1074. /* Request a write lock to block new offload attempts */
  1075. down_write(&device_offload_lock);
  1076. spin_lock_irqsave(&tls_device_lock, flags);
  1077. list_for_each_entry_safe(ctx, tmp, &tls_device_list, list) {
  1078. struct net_device *ctx_netdev =
  1079. rcu_dereference_protected(ctx->netdev,
  1080. lockdep_is_held(&device_offload_lock));
  1081. if (ctx_netdev != netdev ||
  1082. !refcount_inc_not_zero(&ctx->refcount))
  1083. continue;
  1084. list_move(&ctx->list, &list);
  1085. }
  1086. spin_unlock_irqrestore(&tls_device_lock, flags);
  1087. list_for_each_entry_safe(ctx, tmp, &list, list) {
  1088. /* Stop offloaded TX and switch to the fallback.
  1089. * tls_is_skb_tx_device_offloaded will return false.
  1090. */
  1091. WRITE_ONCE(ctx->sk->sk_validate_xmit_skb, tls_validate_xmit_skb_sw);
  1092. /* Stop the RX and TX resync.
  1093. * tls_dev_resync must not be called after tls_dev_del.
  1094. */
  1095. rcu_assign_pointer(ctx->netdev, NULL);
  1096. /* Start skipping the RX resync logic completely. */
  1097. set_bit(TLS_RX_DEV_DEGRADED, &ctx->flags);
  1098. /* Sync with inflight packets. After this point:
  1099. * TX: no non-encrypted packets will be passed to the driver.
  1100. * RX: resync requests from the driver will be ignored.
  1101. */
  1102. synchronize_net();
  1103. /* Release the offload context on the driver side. */
  1104. if (ctx->tx_conf == TLS_HW)
  1105. netdev->tlsdev_ops->tls_dev_del(netdev, ctx,
  1106. TLS_OFFLOAD_CTX_DIR_TX);
  1107. if (ctx->rx_conf == TLS_HW &&
  1108. !test_bit(TLS_RX_DEV_CLOSED, &ctx->flags))
  1109. netdev->tlsdev_ops->tls_dev_del(netdev, ctx,
  1110. TLS_OFFLOAD_CTX_DIR_RX);
  1111. dev_put(netdev);
  1112. /* Move the context to a separate list for two reasons:
  1113. * 1. When the context is deallocated, list_del is called.
  1114. * 2. It's no longer an offloaded context, so we don't want to
  1115. * run offload-specific code on this context.
  1116. */
  1117. spin_lock_irqsave(&tls_device_lock, flags);
  1118. list_move_tail(&ctx->list, &tls_device_down_list);
  1119. spin_unlock_irqrestore(&tls_device_lock, flags);
  1120. /* Device contexts for RX and TX will be freed in on sk_destruct
  1121. * by tls_device_free_ctx. rx_conf and tx_conf stay in TLS_HW.
  1122. * Now release the ref taken above.
  1123. */
  1124. if (refcount_dec_and_test(&ctx->refcount)) {
  1125. /* sk_destruct ran after tls_device_down took a ref, and
  1126. * it returned early. Complete the destruction here.
  1127. */
  1128. list_del(&ctx->list);
  1129. tls_device_free_ctx(ctx);
  1130. }
  1131. }
  1132. up_write(&device_offload_lock);
  1133. flush_workqueue(destruct_wq);
  1134. return NOTIFY_DONE;
  1135. }
  1136. static int tls_dev_event(struct notifier_block *this, unsigned long event,
  1137. void *ptr)
  1138. {
  1139. struct net_device *dev = netdev_notifier_info_to_dev(ptr);
  1140. if (!dev->tlsdev_ops &&
  1141. !(dev->features & (NETIF_F_HW_TLS_RX | NETIF_F_HW_TLS_TX)))
  1142. return NOTIFY_DONE;
  1143. switch (event) {
  1144. case NETDEV_REGISTER:
  1145. case NETDEV_FEAT_CHANGE:
  1146. if (netif_is_bond_master(dev))
  1147. return NOTIFY_DONE;
  1148. if ((dev->features & NETIF_F_HW_TLS_RX) &&
  1149. !dev->tlsdev_ops->tls_dev_resync)
  1150. return NOTIFY_BAD;
  1151. if (dev->tlsdev_ops &&
  1152. dev->tlsdev_ops->tls_dev_add &&
  1153. dev->tlsdev_ops->tls_dev_del)
  1154. return NOTIFY_DONE;
  1155. else
  1156. return NOTIFY_BAD;
  1157. case NETDEV_DOWN:
  1158. return tls_device_down(dev);
  1159. }
  1160. return NOTIFY_DONE;
  1161. }
  1162. static struct notifier_block tls_dev_notifier = {
  1163. .notifier_call = tls_dev_event,
  1164. };
  1165. int __init tls_device_init(void)
  1166. {
  1167. int err;
  1168. dummy_page = alloc_page(GFP_KERNEL);
  1169. if (!dummy_page)
  1170. return -ENOMEM;
  1171. destruct_wq = alloc_workqueue("ktls_device_destruct", 0, 0);
  1172. if (!destruct_wq) {
  1173. err = -ENOMEM;
  1174. goto err_free_dummy;
  1175. }
  1176. err = register_netdevice_notifier(&tls_dev_notifier);
  1177. if (err)
  1178. goto err_destroy_wq;
  1179. return 0;
  1180. err_destroy_wq:
  1181. destroy_workqueue(destruct_wq);
  1182. err_free_dummy:
  1183. put_page(dummy_page);
  1184. return err;
  1185. }
  1186. void __exit tls_device_cleanup(void)
  1187. {
  1188. unregister_netdevice_notifier(&tls_dev_notifier);
  1189. destroy_workqueue(destruct_wq);
  1190. clean_acked_data_flush();
  1191. put_page(dummy_page);
  1192. }