tls_device.c 38 KB

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