tls_device.c 37 KB

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