tls_sw.c 71 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812
  1. /*
  2. * Copyright (c) 2016-2017, Mellanox Technologies. All rights reserved.
  3. * Copyright (c) 2016-2017, Dave Watson <[email protected]>. All rights reserved.
  4. * Copyright (c) 2016-2017, Lance Chao <[email protected]>. All rights reserved.
  5. * Copyright (c) 2016, Fridolin Pokorny <[email protected]>. All rights reserved.
  6. * Copyright (c) 2016, Nikos Mavrogiannopoulos <[email protected]>. All rights reserved.
  7. * Copyright (c) 2018, Covalent IO, Inc. http://covalent.io
  8. *
  9. * This software is available to you under a choice of one of two
  10. * licenses. You may choose to be licensed under the terms of the GNU
  11. * General Public License (GPL) Version 2, available from the file
  12. * COPYING in the main directory of this source tree, or the
  13. * OpenIB.org BSD license below:
  14. *
  15. * Redistribution and use in source and binary forms, with or
  16. * without modification, are permitted provided that the following
  17. * conditions are met:
  18. *
  19. * - Redistributions of source code must retain the above
  20. * copyright notice, this list of conditions and the following
  21. * disclaimer.
  22. *
  23. * - Redistributions in binary form must reproduce the above
  24. * copyright notice, this list of conditions and the following
  25. * disclaimer in the documentation and/or other materials
  26. * provided with the distribution.
  27. *
  28. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  29. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  30. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  31. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  32. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  33. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  34. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  35. * SOFTWARE.
  36. */
  37. #include <linux/bug.h>
  38. #include <linux/sched/signal.h>
  39. #include <linux/module.h>
  40. #include <linux/kernel.h>
  41. #include <linux/splice.h>
  42. #include <crypto/aead.h>
  43. #include <net/strparser.h>
  44. #include <net/tls.h>
  45. #include "tls.h"
  46. struct tls_decrypt_arg {
  47. struct_group(inargs,
  48. bool zc;
  49. bool async;
  50. u8 tail;
  51. );
  52. struct sk_buff *skb;
  53. };
  54. struct tls_decrypt_ctx {
  55. struct sock *sk;
  56. u8 iv[MAX_IV_SIZE];
  57. u8 aad[TLS_MAX_AAD_SIZE];
  58. u8 tail;
  59. bool free_sgout;
  60. struct scatterlist sg[];
  61. };
  62. noinline void tls_err_abort(struct sock *sk, int err)
  63. {
  64. WARN_ON_ONCE(err >= 0);
  65. /* sk->sk_err should contain a positive error code. */
  66. WRITE_ONCE(sk->sk_err, -err);
  67. /* Paired with smp_rmb() in tcp_poll() */
  68. smp_wmb();
  69. sk_error_report(sk);
  70. }
  71. static int __skb_nsg(struct sk_buff *skb, int offset, int len,
  72. unsigned int recursion_level)
  73. {
  74. int start = skb_headlen(skb);
  75. int i, chunk = start - offset;
  76. struct sk_buff *frag_iter;
  77. int elt = 0;
  78. if (unlikely(recursion_level >= 24))
  79. return -EMSGSIZE;
  80. if (chunk > 0) {
  81. if (chunk > len)
  82. chunk = len;
  83. elt++;
  84. len -= chunk;
  85. if (len == 0)
  86. return elt;
  87. offset += chunk;
  88. }
  89. for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
  90. int end;
  91. WARN_ON(start > offset + len);
  92. end = start + skb_frag_size(&skb_shinfo(skb)->frags[i]);
  93. chunk = end - offset;
  94. if (chunk > 0) {
  95. if (chunk > len)
  96. chunk = len;
  97. elt++;
  98. len -= chunk;
  99. if (len == 0)
  100. return elt;
  101. offset += chunk;
  102. }
  103. start = end;
  104. }
  105. if (unlikely(skb_has_frag_list(skb))) {
  106. skb_walk_frags(skb, frag_iter) {
  107. int end, ret;
  108. WARN_ON(start > offset + len);
  109. end = start + frag_iter->len;
  110. chunk = end - offset;
  111. if (chunk > 0) {
  112. if (chunk > len)
  113. chunk = len;
  114. ret = __skb_nsg(frag_iter, offset - start, chunk,
  115. recursion_level + 1);
  116. if (unlikely(ret < 0))
  117. return ret;
  118. elt += ret;
  119. len -= chunk;
  120. if (len == 0)
  121. return elt;
  122. offset += chunk;
  123. }
  124. start = end;
  125. }
  126. }
  127. BUG_ON(len);
  128. return elt;
  129. }
  130. /* Return the number of scatterlist elements required to completely map the
  131. * skb, or -EMSGSIZE if the recursion depth is exceeded.
  132. */
  133. static int skb_nsg(struct sk_buff *skb, int offset, int len)
  134. {
  135. return __skb_nsg(skb, offset, len, 0);
  136. }
  137. static int tls_padding_length(struct tls_prot_info *prot, struct sk_buff *skb,
  138. struct tls_decrypt_arg *darg)
  139. {
  140. struct strp_msg *rxm = strp_msg(skb);
  141. struct tls_msg *tlm = tls_msg(skb);
  142. int sub = 0;
  143. /* Determine zero-padding length */
  144. if (prot->version == TLS_1_3_VERSION) {
  145. int offset = rxm->full_len - TLS_TAG_SIZE - 1;
  146. char content_type = darg->zc ? darg->tail : 0;
  147. int err;
  148. while (content_type == 0) {
  149. if (offset < prot->prepend_size)
  150. return -EBADMSG;
  151. err = skb_copy_bits(skb, rxm->offset + offset,
  152. &content_type, 1);
  153. if (err)
  154. return err;
  155. if (content_type)
  156. break;
  157. sub++;
  158. offset--;
  159. }
  160. tlm->control = content_type;
  161. }
  162. return sub;
  163. }
  164. static void tls_decrypt_done(crypto_completion_data_t *data, int err)
  165. {
  166. struct aead_request *aead_req = crypto_get_completion_data(data);
  167. struct crypto_aead *aead = crypto_aead_reqtfm(aead_req);
  168. struct scatterlist *sgout = aead_req->dst;
  169. struct tls_sw_context_rx *ctx;
  170. struct tls_decrypt_ctx *dctx;
  171. struct tls_context *tls_ctx;
  172. struct scatterlist *sg;
  173. unsigned int pages;
  174. struct sock *sk;
  175. int aead_size;
  176. aead_size = sizeof(*aead_req) + crypto_aead_reqsize(aead);
  177. aead_size = ALIGN(aead_size, __alignof__(*dctx));
  178. dctx = (void *)((u8 *)aead_req + aead_size);
  179. sk = dctx->sk;
  180. tls_ctx = tls_get_ctx(sk);
  181. ctx = tls_sw_ctx_rx(tls_ctx);
  182. /* Propagate if there was an err */
  183. if (err) {
  184. if (err == -EBADMSG)
  185. TLS_INC_STATS(sock_net(sk), LINUX_MIB_TLSDECRYPTERROR);
  186. ctx->async_wait.err = err;
  187. tls_err_abort(sk, err);
  188. }
  189. /* Free the destination pages if skb was not decrypted inplace */
  190. if (dctx->free_sgout) {
  191. /* Skip the first S/G entry as it points to AAD */
  192. for_each_sg(sg_next(sgout), sg, UINT_MAX, pages) {
  193. if (!sg)
  194. break;
  195. put_page(sg_page(sg));
  196. }
  197. }
  198. kfree(aead_req);
  199. spin_lock_bh(&ctx->decrypt_compl_lock);
  200. if (!atomic_dec_return(&ctx->decrypt_pending))
  201. complete(&ctx->async_wait.completion);
  202. spin_unlock_bh(&ctx->decrypt_compl_lock);
  203. }
  204. static int tls_do_decryption(struct sock *sk,
  205. struct scatterlist *sgin,
  206. struct scatterlist *sgout,
  207. char *iv_recv,
  208. size_t data_len,
  209. struct aead_request *aead_req,
  210. struct tls_decrypt_arg *darg)
  211. {
  212. struct tls_context *tls_ctx = tls_get_ctx(sk);
  213. struct tls_prot_info *prot = &tls_ctx->prot_info;
  214. struct tls_sw_context_rx *ctx = tls_sw_ctx_rx(tls_ctx);
  215. int ret;
  216. aead_request_set_tfm(aead_req, ctx->aead_recv);
  217. aead_request_set_ad(aead_req, prot->aad_size);
  218. aead_request_set_crypt(aead_req, sgin, sgout,
  219. data_len + prot->tag_size,
  220. (u8 *)iv_recv);
  221. if (darg->async) {
  222. aead_request_set_callback(aead_req,
  223. CRYPTO_TFM_REQ_MAY_BACKLOG,
  224. tls_decrypt_done, aead_req);
  225. atomic_inc(&ctx->decrypt_pending);
  226. } else {
  227. aead_request_set_callback(aead_req,
  228. CRYPTO_TFM_REQ_MAY_BACKLOG,
  229. crypto_req_done, &ctx->async_wait);
  230. }
  231. ret = crypto_aead_decrypt(aead_req);
  232. if (ret == -EINPROGRESS) {
  233. if (darg->async)
  234. return 0;
  235. ret = crypto_wait_req(ret, &ctx->async_wait);
  236. }
  237. darg->async = false;
  238. return ret;
  239. }
  240. static void tls_trim_both_msgs(struct sock *sk, int target_size)
  241. {
  242. struct tls_context *tls_ctx = tls_get_ctx(sk);
  243. struct tls_prot_info *prot = &tls_ctx->prot_info;
  244. struct tls_sw_context_tx *ctx = tls_sw_ctx_tx(tls_ctx);
  245. struct tls_rec *rec = ctx->open_rec;
  246. sk_msg_trim(sk, &rec->msg_plaintext, target_size);
  247. if (target_size > 0)
  248. target_size += prot->overhead_size;
  249. sk_msg_trim(sk, &rec->msg_encrypted, target_size);
  250. }
  251. static int tls_alloc_encrypted_msg(struct sock *sk, int len)
  252. {
  253. struct tls_context *tls_ctx = tls_get_ctx(sk);
  254. struct tls_sw_context_tx *ctx = tls_sw_ctx_tx(tls_ctx);
  255. struct tls_rec *rec = ctx->open_rec;
  256. struct sk_msg *msg_en = &rec->msg_encrypted;
  257. return sk_msg_alloc(sk, msg_en, len, 0);
  258. }
  259. static int tls_clone_plaintext_msg(struct sock *sk, int required)
  260. {
  261. struct tls_context *tls_ctx = tls_get_ctx(sk);
  262. struct tls_prot_info *prot = &tls_ctx->prot_info;
  263. struct tls_sw_context_tx *ctx = tls_sw_ctx_tx(tls_ctx);
  264. struct tls_rec *rec = ctx->open_rec;
  265. struct sk_msg *msg_pl = &rec->msg_plaintext;
  266. struct sk_msg *msg_en = &rec->msg_encrypted;
  267. int skip, len;
  268. /* We add page references worth len bytes from encrypted sg
  269. * at the end of plaintext sg. It is guaranteed that msg_en
  270. * has enough required room (ensured by caller).
  271. */
  272. len = required - msg_pl->sg.size;
  273. /* Skip initial bytes in msg_en's data to be able to use
  274. * same offset of both plain and encrypted data.
  275. */
  276. skip = prot->prepend_size + msg_pl->sg.size;
  277. return sk_msg_clone(sk, msg_pl, msg_en, skip, len);
  278. }
  279. static struct tls_rec *tls_get_rec(struct sock *sk)
  280. {
  281. struct tls_context *tls_ctx = tls_get_ctx(sk);
  282. struct tls_prot_info *prot = &tls_ctx->prot_info;
  283. struct tls_sw_context_tx *ctx = tls_sw_ctx_tx(tls_ctx);
  284. struct sk_msg *msg_pl, *msg_en;
  285. struct tls_rec *rec;
  286. int mem_size;
  287. mem_size = sizeof(struct tls_rec) + crypto_aead_reqsize(ctx->aead_send);
  288. rec = kzalloc(mem_size, sk->sk_allocation);
  289. if (!rec)
  290. return NULL;
  291. msg_pl = &rec->msg_plaintext;
  292. msg_en = &rec->msg_encrypted;
  293. sk_msg_init(msg_pl);
  294. sk_msg_init(msg_en);
  295. sg_init_table(rec->sg_aead_in, 2);
  296. sg_set_buf(&rec->sg_aead_in[0], rec->aad_space, prot->aad_size);
  297. sg_unmark_end(&rec->sg_aead_in[1]);
  298. sg_init_table(rec->sg_aead_out, 2);
  299. sg_set_buf(&rec->sg_aead_out[0], rec->aad_space, prot->aad_size);
  300. sg_unmark_end(&rec->sg_aead_out[1]);
  301. rec->sk = sk;
  302. return rec;
  303. }
  304. static void tls_free_rec(struct sock *sk, struct tls_rec *rec)
  305. {
  306. sk_msg_free(sk, &rec->msg_encrypted);
  307. sk_msg_free(sk, &rec->msg_plaintext);
  308. kfree(rec);
  309. }
  310. static void tls_free_open_rec(struct sock *sk)
  311. {
  312. struct tls_context *tls_ctx = tls_get_ctx(sk);
  313. struct tls_sw_context_tx *ctx = tls_sw_ctx_tx(tls_ctx);
  314. struct tls_rec *rec = ctx->open_rec;
  315. if (rec) {
  316. tls_free_rec(sk, rec);
  317. ctx->open_rec = NULL;
  318. }
  319. }
  320. int tls_tx_records(struct sock *sk, int flags)
  321. {
  322. struct tls_context *tls_ctx = tls_get_ctx(sk);
  323. struct tls_sw_context_tx *ctx = tls_sw_ctx_tx(tls_ctx);
  324. struct tls_rec *rec, *tmp;
  325. struct sk_msg *msg_en;
  326. int tx_flags, rc = 0;
  327. if (tls_is_partially_sent_record(tls_ctx)) {
  328. rec = list_first_entry(&ctx->tx_list,
  329. struct tls_rec, list);
  330. if (flags == -1)
  331. tx_flags = rec->tx_flags;
  332. else
  333. tx_flags = flags;
  334. rc = tls_push_partial_record(sk, tls_ctx, tx_flags);
  335. if (rc)
  336. goto tx_err;
  337. /* Full record has been transmitted.
  338. * Remove the head of tx_list
  339. */
  340. list_del(&rec->list);
  341. sk_msg_free(sk, &rec->msg_plaintext);
  342. kfree(rec);
  343. }
  344. /* Tx all ready records */
  345. list_for_each_entry_safe(rec, tmp, &ctx->tx_list, list) {
  346. if (READ_ONCE(rec->tx_ready)) {
  347. if (flags == -1)
  348. tx_flags = rec->tx_flags;
  349. else
  350. tx_flags = flags;
  351. msg_en = &rec->msg_encrypted;
  352. rc = tls_push_sg(sk, tls_ctx,
  353. &msg_en->sg.data[msg_en->sg.curr],
  354. 0, tx_flags);
  355. if (rc)
  356. goto tx_err;
  357. list_del(&rec->list);
  358. sk_msg_free(sk, &rec->msg_plaintext);
  359. kfree(rec);
  360. } else {
  361. break;
  362. }
  363. }
  364. tx_err:
  365. if (rc < 0 && rc != -EAGAIN)
  366. tls_err_abort(sk, -EBADMSG);
  367. return rc;
  368. }
  369. static void tls_encrypt_done(crypto_completion_data_t *data, int err)
  370. {
  371. struct aead_request *aead_req = crypto_get_completion_data(data);
  372. struct tls_sw_context_tx *ctx;
  373. struct tls_context *tls_ctx;
  374. struct tls_prot_info *prot;
  375. struct scatterlist *sge;
  376. struct sk_msg *msg_en;
  377. struct tls_rec *rec;
  378. bool ready = false;
  379. struct sock *sk;
  380. int pending;
  381. rec = container_of(aead_req, struct tls_rec, aead_req);
  382. msg_en = &rec->msg_encrypted;
  383. sk = rec->sk;
  384. tls_ctx = tls_get_ctx(sk);
  385. prot = &tls_ctx->prot_info;
  386. ctx = tls_sw_ctx_tx(tls_ctx);
  387. sge = sk_msg_elem(msg_en, msg_en->sg.curr);
  388. sge->offset -= prot->prepend_size;
  389. sge->length += prot->prepend_size;
  390. /* Check if error is previously set on socket */
  391. if (err || sk->sk_err) {
  392. rec = NULL;
  393. /* If err is already set on socket, return the same code */
  394. if (sk->sk_err) {
  395. ctx->async_wait.err = -sk->sk_err;
  396. } else {
  397. ctx->async_wait.err = err;
  398. tls_err_abort(sk, err);
  399. }
  400. }
  401. if (rec) {
  402. struct tls_rec *first_rec;
  403. /* Mark the record as ready for transmission */
  404. smp_store_mb(rec->tx_ready, true);
  405. /* If received record is at head of tx_list, schedule tx */
  406. first_rec = list_first_entry(&ctx->tx_list,
  407. struct tls_rec, list);
  408. if (rec == first_rec)
  409. ready = true;
  410. }
  411. spin_lock_bh(&ctx->encrypt_compl_lock);
  412. pending = atomic_dec_return(&ctx->encrypt_pending);
  413. if (!pending && ctx->async_notify)
  414. complete(&ctx->async_wait.completion);
  415. spin_unlock_bh(&ctx->encrypt_compl_lock);
  416. if (!ready)
  417. return;
  418. /* Schedule the transmission */
  419. if (!test_and_set_bit(BIT_TX_SCHEDULED, &ctx->tx_bitmask))
  420. schedule_delayed_work(&ctx->tx_work.work, 1);
  421. }
  422. static int tls_do_encryption(struct sock *sk,
  423. struct tls_context *tls_ctx,
  424. struct tls_sw_context_tx *ctx,
  425. struct aead_request *aead_req,
  426. size_t data_len, u32 start)
  427. {
  428. struct tls_prot_info *prot = &tls_ctx->prot_info;
  429. struct tls_rec *rec = ctx->open_rec;
  430. struct sk_msg *msg_en = &rec->msg_encrypted;
  431. struct scatterlist *sge = sk_msg_elem(msg_en, start);
  432. int rc, iv_offset = 0;
  433. /* For CCM based ciphers, first byte of IV is a constant */
  434. switch (prot->cipher_type) {
  435. case TLS_CIPHER_AES_CCM_128:
  436. rec->iv_data[0] = TLS_AES_CCM_IV_B0_BYTE;
  437. iv_offset = 1;
  438. break;
  439. case TLS_CIPHER_SM4_CCM:
  440. rec->iv_data[0] = TLS_SM4_CCM_IV_B0_BYTE;
  441. iv_offset = 1;
  442. break;
  443. }
  444. memcpy(&rec->iv_data[iv_offset], tls_ctx->tx.iv,
  445. prot->iv_size + prot->salt_size);
  446. tls_xor_iv_with_seq(prot, rec->iv_data + iv_offset,
  447. tls_ctx->tx.rec_seq);
  448. sge->offset += prot->prepend_size;
  449. sge->length -= prot->prepend_size;
  450. msg_en->sg.curr = start;
  451. aead_request_set_tfm(aead_req, ctx->aead_send);
  452. aead_request_set_ad(aead_req, prot->aad_size);
  453. aead_request_set_crypt(aead_req, rec->sg_aead_in,
  454. rec->sg_aead_out,
  455. data_len, rec->iv_data);
  456. aead_request_set_callback(aead_req, CRYPTO_TFM_REQ_MAY_BACKLOG,
  457. tls_encrypt_done, aead_req);
  458. /* Add the record in tx_list */
  459. list_add_tail((struct list_head *)&rec->list, &ctx->tx_list);
  460. atomic_inc(&ctx->encrypt_pending);
  461. rc = crypto_aead_encrypt(aead_req);
  462. if (!rc || rc != -EINPROGRESS) {
  463. atomic_dec(&ctx->encrypt_pending);
  464. sge->offset -= prot->prepend_size;
  465. sge->length += prot->prepend_size;
  466. }
  467. if (!rc) {
  468. WRITE_ONCE(rec->tx_ready, true);
  469. } else if (rc != -EINPROGRESS) {
  470. list_del(&rec->list);
  471. return rc;
  472. }
  473. /* Unhook the record from context if encryption is not failure */
  474. ctx->open_rec = NULL;
  475. tls_advance_record_sn(sk, prot, &tls_ctx->tx);
  476. return rc;
  477. }
  478. static int tls_split_open_record(struct sock *sk, struct tls_rec *from,
  479. struct tls_rec **to, struct sk_msg *msg_opl,
  480. struct sk_msg *msg_oen, u32 split_point,
  481. u32 tx_overhead_size, u32 *orig_end)
  482. {
  483. u32 i, j, bytes = 0, apply = msg_opl->apply_bytes;
  484. struct scatterlist *sge, *osge, *nsge;
  485. u32 orig_size = msg_opl->sg.size;
  486. struct scatterlist tmp = { };
  487. struct sk_msg *msg_npl;
  488. struct tls_rec *new;
  489. int ret;
  490. new = tls_get_rec(sk);
  491. if (!new)
  492. return -ENOMEM;
  493. ret = sk_msg_alloc(sk, &new->msg_encrypted, msg_opl->sg.size +
  494. tx_overhead_size, 0);
  495. if (ret < 0) {
  496. tls_free_rec(sk, new);
  497. return ret;
  498. }
  499. *orig_end = msg_opl->sg.end;
  500. i = msg_opl->sg.start;
  501. sge = sk_msg_elem(msg_opl, i);
  502. while (apply && sge->length) {
  503. if (sge->length > apply) {
  504. u32 len = sge->length - apply;
  505. get_page(sg_page(sge));
  506. sg_set_page(&tmp, sg_page(sge), len,
  507. sge->offset + apply);
  508. sge->length = apply;
  509. bytes += apply;
  510. apply = 0;
  511. } else {
  512. apply -= sge->length;
  513. bytes += sge->length;
  514. }
  515. sk_msg_iter_var_next(i);
  516. if (i == msg_opl->sg.end)
  517. break;
  518. sge = sk_msg_elem(msg_opl, i);
  519. }
  520. msg_opl->sg.end = i;
  521. msg_opl->sg.curr = i;
  522. msg_opl->sg.copybreak = 0;
  523. msg_opl->apply_bytes = 0;
  524. msg_opl->sg.size = bytes;
  525. msg_npl = &new->msg_plaintext;
  526. msg_npl->apply_bytes = apply;
  527. msg_npl->sg.size = orig_size - bytes;
  528. j = msg_npl->sg.start;
  529. nsge = sk_msg_elem(msg_npl, j);
  530. if (tmp.length) {
  531. memcpy(nsge, &tmp, sizeof(*nsge));
  532. sk_msg_iter_var_next(j);
  533. nsge = sk_msg_elem(msg_npl, j);
  534. }
  535. osge = sk_msg_elem(msg_opl, i);
  536. while (osge->length) {
  537. memcpy(nsge, osge, sizeof(*nsge));
  538. sg_unmark_end(nsge);
  539. sk_msg_iter_var_next(i);
  540. sk_msg_iter_var_next(j);
  541. if (i == *orig_end)
  542. break;
  543. osge = sk_msg_elem(msg_opl, i);
  544. nsge = sk_msg_elem(msg_npl, j);
  545. }
  546. msg_npl->sg.end = j;
  547. msg_npl->sg.curr = j;
  548. msg_npl->sg.copybreak = 0;
  549. *to = new;
  550. return 0;
  551. }
  552. static void tls_merge_open_record(struct sock *sk, struct tls_rec *to,
  553. struct tls_rec *from, u32 orig_end)
  554. {
  555. struct sk_msg *msg_npl = &from->msg_plaintext;
  556. struct sk_msg *msg_opl = &to->msg_plaintext;
  557. struct scatterlist *osge, *nsge;
  558. u32 i, j;
  559. i = msg_opl->sg.end;
  560. sk_msg_iter_var_prev(i);
  561. j = msg_npl->sg.start;
  562. osge = sk_msg_elem(msg_opl, i);
  563. nsge = sk_msg_elem(msg_npl, j);
  564. if (sg_page(osge) == sg_page(nsge) &&
  565. osge->offset + osge->length == nsge->offset) {
  566. osge->length += nsge->length;
  567. put_page(sg_page(nsge));
  568. }
  569. msg_opl->sg.end = orig_end;
  570. msg_opl->sg.curr = orig_end;
  571. msg_opl->sg.copybreak = 0;
  572. msg_opl->apply_bytes = msg_opl->sg.size + msg_npl->sg.size;
  573. msg_opl->sg.size += msg_npl->sg.size;
  574. sk_msg_free(sk, &to->msg_encrypted);
  575. sk_msg_xfer_full(&to->msg_encrypted, &from->msg_encrypted);
  576. kfree(from);
  577. }
  578. static int tls_push_record(struct sock *sk, int flags,
  579. unsigned char record_type)
  580. {
  581. struct tls_context *tls_ctx = tls_get_ctx(sk);
  582. struct tls_prot_info *prot = &tls_ctx->prot_info;
  583. struct tls_sw_context_tx *ctx = tls_sw_ctx_tx(tls_ctx);
  584. struct tls_rec *rec = ctx->open_rec, *tmp = NULL;
  585. u32 i, split_point, orig_end;
  586. struct sk_msg *msg_pl, *msg_en;
  587. struct aead_request *req;
  588. bool split;
  589. int rc;
  590. if (!rec)
  591. return 0;
  592. msg_pl = &rec->msg_plaintext;
  593. msg_en = &rec->msg_encrypted;
  594. split_point = msg_pl->apply_bytes;
  595. split = split_point && split_point < msg_pl->sg.size;
  596. if (unlikely((!split &&
  597. msg_pl->sg.size +
  598. prot->overhead_size > msg_en->sg.size) ||
  599. (split &&
  600. split_point +
  601. prot->overhead_size > msg_en->sg.size))) {
  602. split = true;
  603. split_point = msg_en->sg.size;
  604. }
  605. if (split) {
  606. rc = tls_split_open_record(sk, rec, &tmp, msg_pl, msg_en,
  607. split_point, prot->overhead_size,
  608. &orig_end);
  609. if (rc < 0)
  610. return rc;
  611. /* This can happen if above tls_split_open_record allocates
  612. * a single large encryption buffer instead of two smaller
  613. * ones. In this case adjust pointers and continue without
  614. * split.
  615. */
  616. if (!msg_pl->sg.size) {
  617. tls_merge_open_record(sk, rec, tmp, orig_end);
  618. msg_pl = &rec->msg_plaintext;
  619. msg_en = &rec->msg_encrypted;
  620. split = false;
  621. }
  622. sk_msg_trim(sk, msg_en, msg_pl->sg.size +
  623. prot->overhead_size);
  624. }
  625. rec->tx_flags = flags;
  626. req = &rec->aead_req;
  627. i = msg_pl->sg.end;
  628. sk_msg_iter_var_prev(i);
  629. rec->content_type = record_type;
  630. if (prot->version == TLS_1_3_VERSION) {
  631. /* Add content type to end of message. No padding added */
  632. sg_set_buf(&rec->sg_content_type, &rec->content_type, 1);
  633. sg_mark_end(&rec->sg_content_type);
  634. sg_chain(msg_pl->sg.data, msg_pl->sg.end + 1,
  635. &rec->sg_content_type);
  636. } else {
  637. sg_mark_end(sk_msg_elem(msg_pl, i));
  638. }
  639. if (msg_pl->sg.end < msg_pl->sg.start) {
  640. sg_chain(&msg_pl->sg.data[msg_pl->sg.start],
  641. MAX_SKB_FRAGS - msg_pl->sg.start + 1,
  642. msg_pl->sg.data);
  643. }
  644. i = msg_pl->sg.start;
  645. sg_chain(rec->sg_aead_in, 2, &msg_pl->sg.data[i]);
  646. i = msg_en->sg.end;
  647. sk_msg_iter_var_prev(i);
  648. sg_mark_end(sk_msg_elem(msg_en, i));
  649. i = msg_en->sg.start;
  650. sg_chain(rec->sg_aead_out, 2, &msg_en->sg.data[i]);
  651. tls_make_aad(rec->aad_space, msg_pl->sg.size + prot->tail_size,
  652. tls_ctx->tx.rec_seq, record_type, prot);
  653. tls_fill_prepend(tls_ctx,
  654. page_address(sg_page(&msg_en->sg.data[i])) +
  655. msg_en->sg.data[i].offset,
  656. msg_pl->sg.size + prot->tail_size,
  657. record_type);
  658. tls_ctx->pending_open_record_frags = false;
  659. rc = tls_do_encryption(sk, tls_ctx, ctx, req,
  660. msg_pl->sg.size + prot->tail_size, i);
  661. if (rc < 0) {
  662. if (rc != -EINPROGRESS) {
  663. tls_err_abort(sk, -EBADMSG);
  664. if (split) {
  665. tls_ctx->pending_open_record_frags = true;
  666. tls_merge_open_record(sk, rec, tmp, orig_end);
  667. }
  668. }
  669. ctx->async_capable = 1;
  670. return rc;
  671. } else if (split) {
  672. msg_pl = &tmp->msg_plaintext;
  673. msg_en = &tmp->msg_encrypted;
  674. sk_msg_trim(sk, msg_en, msg_pl->sg.size + prot->overhead_size);
  675. tls_ctx->pending_open_record_frags = true;
  676. ctx->open_rec = tmp;
  677. }
  678. return tls_tx_records(sk, flags);
  679. }
  680. static int bpf_exec_tx_verdict(struct sk_msg *msg, struct sock *sk,
  681. bool full_record, u8 record_type,
  682. ssize_t *copied, int flags)
  683. {
  684. struct tls_context *tls_ctx = tls_get_ctx(sk);
  685. struct tls_sw_context_tx *ctx = tls_sw_ctx_tx(tls_ctx);
  686. struct sk_msg msg_redir = { };
  687. struct sk_psock *psock;
  688. struct sock *sk_redir;
  689. struct tls_rec *rec;
  690. bool enospc, policy, redir_ingress;
  691. int err = 0, send;
  692. u32 delta = 0;
  693. policy = !(flags & MSG_SENDPAGE_NOPOLICY);
  694. psock = sk_psock_get(sk);
  695. if (!psock || !policy) {
  696. err = tls_push_record(sk, flags, record_type);
  697. if (err && err != -EINPROGRESS && sk->sk_err == EBADMSG) {
  698. *copied -= sk_msg_free(sk, msg);
  699. tls_free_open_rec(sk);
  700. err = -sk->sk_err;
  701. }
  702. if (psock)
  703. sk_psock_put(sk, psock);
  704. return err;
  705. }
  706. more_data:
  707. enospc = sk_msg_full(msg);
  708. if (psock->eval == __SK_NONE) {
  709. delta = msg->sg.size;
  710. psock->eval = sk_psock_msg_verdict(sk, psock, msg);
  711. delta -= msg->sg.size;
  712. }
  713. if (msg->cork_bytes && msg->cork_bytes > msg->sg.size &&
  714. !enospc && !full_record) {
  715. err = -ENOSPC;
  716. goto out_err;
  717. }
  718. msg->cork_bytes = 0;
  719. send = msg->sg.size;
  720. if (msg->apply_bytes && msg->apply_bytes < send)
  721. send = msg->apply_bytes;
  722. switch (psock->eval) {
  723. case __SK_PASS:
  724. err = tls_push_record(sk, flags, record_type);
  725. if (err && err != -EINPROGRESS && sk->sk_err == EBADMSG) {
  726. *copied -= sk_msg_free(sk, msg);
  727. tls_free_open_rec(sk);
  728. err = -sk->sk_err;
  729. goto out_err;
  730. }
  731. break;
  732. case __SK_REDIRECT:
  733. redir_ingress = psock->redir_ingress;
  734. sk_redir = psock->sk_redir;
  735. memcpy(&msg_redir, msg, sizeof(*msg));
  736. if (msg->apply_bytes < send)
  737. msg->apply_bytes = 0;
  738. else
  739. msg->apply_bytes -= send;
  740. sk_msg_return_zero(sk, msg, send);
  741. msg->sg.size -= send;
  742. release_sock(sk);
  743. err = tcp_bpf_sendmsg_redir(sk_redir, redir_ingress,
  744. &msg_redir, send, flags);
  745. lock_sock(sk);
  746. if (err < 0) {
  747. *copied -= sk_msg_free_nocharge(sk, &msg_redir);
  748. msg->sg.size = 0;
  749. }
  750. if (msg->sg.size == 0)
  751. tls_free_open_rec(sk);
  752. break;
  753. case __SK_DROP:
  754. default:
  755. sk_msg_free_partial(sk, msg, send);
  756. if (msg->apply_bytes < send)
  757. msg->apply_bytes = 0;
  758. else
  759. msg->apply_bytes -= send;
  760. if (msg->sg.size == 0)
  761. tls_free_open_rec(sk);
  762. *copied -= (send + delta);
  763. err = -EACCES;
  764. }
  765. if (likely(!err)) {
  766. bool reset_eval = !ctx->open_rec;
  767. rec = ctx->open_rec;
  768. if (rec) {
  769. msg = &rec->msg_plaintext;
  770. if (!msg->apply_bytes)
  771. reset_eval = true;
  772. }
  773. if (reset_eval) {
  774. psock->eval = __SK_NONE;
  775. if (psock->sk_redir) {
  776. sock_put(psock->sk_redir);
  777. psock->sk_redir = NULL;
  778. }
  779. }
  780. if (rec)
  781. goto more_data;
  782. }
  783. out_err:
  784. sk_psock_put(sk, psock);
  785. return err;
  786. }
  787. static int tls_sw_push_pending_record(struct sock *sk, int flags)
  788. {
  789. struct tls_context *tls_ctx = tls_get_ctx(sk);
  790. struct tls_sw_context_tx *ctx = tls_sw_ctx_tx(tls_ctx);
  791. struct tls_rec *rec = ctx->open_rec;
  792. struct sk_msg *msg_pl;
  793. size_t copied;
  794. if (!rec)
  795. return 0;
  796. msg_pl = &rec->msg_plaintext;
  797. copied = msg_pl->sg.size;
  798. if (!copied)
  799. return 0;
  800. return bpf_exec_tx_verdict(msg_pl, sk, true, TLS_RECORD_TYPE_DATA,
  801. &copied, flags);
  802. }
  803. int tls_sw_sendmsg(struct sock *sk, struct msghdr *msg, size_t size)
  804. {
  805. long timeo = sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT);
  806. struct tls_context *tls_ctx = tls_get_ctx(sk);
  807. struct tls_prot_info *prot = &tls_ctx->prot_info;
  808. struct tls_sw_context_tx *ctx = tls_sw_ctx_tx(tls_ctx);
  809. bool async_capable = ctx->async_capable;
  810. unsigned char record_type = TLS_RECORD_TYPE_DATA;
  811. bool is_kvec = iov_iter_is_kvec(&msg->msg_iter);
  812. bool eor = !(msg->msg_flags & MSG_MORE);
  813. size_t try_to_copy;
  814. ssize_t copied = 0;
  815. struct sk_msg *msg_pl, *msg_en;
  816. struct tls_rec *rec;
  817. int required_size;
  818. int num_async = 0;
  819. bool full_record;
  820. int record_room;
  821. int num_zc = 0;
  822. int orig_size;
  823. int ret = 0;
  824. int pending;
  825. if (msg->msg_flags & ~(MSG_MORE | MSG_DONTWAIT | MSG_NOSIGNAL |
  826. MSG_CMSG_COMPAT))
  827. return -EOPNOTSUPP;
  828. ret = mutex_lock_interruptible(&tls_ctx->tx_lock);
  829. if (ret)
  830. return ret;
  831. lock_sock(sk);
  832. if (unlikely(msg->msg_controllen)) {
  833. ret = tls_process_cmsg(sk, msg, &record_type);
  834. if (ret) {
  835. if (ret == -EINPROGRESS)
  836. num_async++;
  837. else if (ret != -EAGAIN)
  838. goto send_end;
  839. }
  840. }
  841. while (msg_data_left(msg)) {
  842. if (sk->sk_err) {
  843. ret = -sk->sk_err;
  844. goto send_end;
  845. }
  846. if (ctx->open_rec)
  847. rec = ctx->open_rec;
  848. else
  849. rec = ctx->open_rec = tls_get_rec(sk);
  850. if (!rec) {
  851. ret = -ENOMEM;
  852. goto send_end;
  853. }
  854. msg_pl = &rec->msg_plaintext;
  855. msg_en = &rec->msg_encrypted;
  856. orig_size = msg_pl->sg.size;
  857. full_record = false;
  858. try_to_copy = msg_data_left(msg);
  859. record_room = TLS_MAX_PAYLOAD_SIZE - msg_pl->sg.size;
  860. if (try_to_copy >= record_room) {
  861. try_to_copy = record_room;
  862. full_record = true;
  863. }
  864. required_size = msg_pl->sg.size + try_to_copy +
  865. prot->overhead_size;
  866. if (!sk_stream_memory_free(sk))
  867. goto wait_for_sndbuf;
  868. alloc_encrypted:
  869. ret = tls_alloc_encrypted_msg(sk, required_size);
  870. if (ret) {
  871. if (ret != -ENOSPC)
  872. goto wait_for_memory;
  873. /* Adjust try_to_copy according to the amount that was
  874. * actually allocated. The difference is due
  875. * to max sg elements limit
  876. */
  877. try_to_copy -= required_size - msg_en->sg.size;
  878. full_record = true;
  879. }
  880. if (!is_kvec && (full_record || eor) && !async_capable) {
  881. u32 first = msg_pl->sg.end;
  882. ret = sk_msg_zerocopy_from_iter(sk, &msg->msg_iter,
  883. msg_pl, try_to_copy);
  884. if (ret)
  885. goto fallback_to_reg_send;
  886. num_zc++;
  887. copied += try_to_copy;
  888. sk_msg_sg_copy_set(msg_pl, first);
  889. ret = bpf_exec_tx_verdict(msg_pl, sk, full_record,
  890. record_type, &copied,
  891. msg->msg_flags);
  892. if (ret) {
  893. if (ret == -EINPROGRESS)
  894. num_async++;
  895. else if (ret == -ENOMEM)
  896. goto wait_for_memory;
  897. else if (ctx->open_rec && ret == -ENOSPC)
  898. goto rollback_iter;
  899. else if (ret != -EAGAIN)
  900. goto send_end;
  901. }
  902. continue;
  903. rollback_iter:
  904. copied -= try_to_copy;
  905. sk_msg_sg_copy_clear(msg_pl, first);
  906. iov_iter_revert(&msg->msg_iter,
  907. msg_pl->sg.size - orig_size);
  908. fallback_to_reg_send:
  909. sk_msg_trim(sk, msg_pl, orig_size);
  910. }
  911. required_size = msg_pl->sg.size + try_to_copy;
  912. ret = tls_clone_plaintext_msg(sk, required_size);
  913. if (ret) {
  914. if (ret != -ENOSPC)
  915. goto send_end;
  916. /* Adjust try_to_copy according to the amount that was
  917. * actually allocated. The difference is due
  918. * to max sg elements limit
  919. */
  920. try_to_copy -= required_size - msg_pl->sg.size;
  921. full_record = true;
  922. sk_msg_trim(sk, msg_en,
  923. msg_pl->sg.size + prot->overhead_size);
  924. }
  925. if (try_to_copy) {
  926. ret = sk_msg_memcopy_from_iter(sk, &msg->msg_iter,
  927. msg_pl, try_to_copy);
  928. if (ret < 0)
  929. goto trim_sgl;
  930. }
  931. /* Open records defined only if successfully copied, otherwise
  932. * we would trim the sg but not reset the open record frags.
  933. */
  934. tls_ctx->pending_open_record_frags = true;
  935. copied += try_to_copy;
  936. if (full_record || eor) {
  937. ret = bpf_exec_tx_verdict(msg_pl, sk, full_record,
  938. record_type, &copied,
  939. msg->msg_flags);
  940. if (ret) {
  941. if (ret == -EINPROGRESS)
  942. num_async++;
  943. else if (ret == -ENOMEM)
  944. goto wait_for_memory;
  945. else if (ret != -EAGAIN) {
  946. if (ret == -ENOSPC)
  947. ret = 0;
  948. goto send_end;
  949. }
  950. }
  951. }
  952. continue;
  953. wait_for_sndbuf:
  954. set_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
  955. wait_for_memory:
  956. ret = sk_stream_wait_memory(sk, &timeo);
  957. if (ret) {
  958. trim_sgl:
  959. if (ctx->open_rec)
  960. tls_trim_both_msgs(sk, orig_size);
  961. goto send_end;
  962. }
  963. if (ctx->open_rec && msg_en->sg.size < required_size)
  964. goto alloc_encrypted;
  965. }
  966. if (!num_async) {
  967. goto send_end;
  968. } else if (num_zc) {
  969. /* Wait for pending encryptions to get completed */
  970. spin_lock_bh(&ctx->encrypt_compl_lock);
  971. ctx->async_notify = true;
  972. pending = atomic_read(&ctx->encrypt_pending);
  973. spin_unlock_bh(&ctx->encrypt_compl_lock);
  974. if (pending)
  975. crypto_wait_req(-EINPROGRESS, &ctx->async_wait);
  976. else
  977. reinit_completion(&ctx->async_wait.completion);
  978. /* There can be no concurrent accesses, since we have no
  979. * pending encrypt operations
  980. */
  981. WRITE_ONCE(ctx->async_notify, false);
  982. if (ctx->async_wait.err) {
  983. ret = ctx->async_wait.err;
  984. copied = 0;
  985. }
  986. }
  987. /* Transmit if any encryptions have completed */
  988. if (test_and_clear_bit(BIT_TX_SCHEDULED, &ctx->tx_bitmask)) {
  989. cancel_delayed_work(&ctx->tx_work.work);
  990. tls_tx_records(sk, msg->msg_flags);
  991. }
  992. send_end:
  993. ret = sk_stream_error(sk, msg->msg_flags, ret);
  994. release_sock(sk);
  995. mutex_unlock(&tls_ctx->tx_lock);
  996. return copied > 0 ? copied : ret;
  997. }
  998. static int tls_sw_do_sendpage(struct sock *sk, struct page *page,
  999. int offset, size_t size, int flags)
  1000. {
  1001. long timeo = sock_sndtimeo(sk, flags & MSG_DONTWAIT);
  1002. struct tls_context *tls_ctx = tls_get_ctx(sk);
  1003. struct tls_sw_context_tx *ctx = tls_sw_ctx_tx(tls_ctx);
  1004. struct tls_prot_info *prot = &tls_ctx->prot_info;
  1005. unsigned char record_type = TLS_RECORD_TYPE_DATA;
  1006. struct sk_msg *msg_pl;
  1007. struct tls_rec *rec;
  1008. int num_async = 0;
  1009. ssize_t copied = 0;
  1010. bool full_record;
  1011. int record_room;
  1012. int ret = 0;
  1013. bool eor;
  1014. eor = !(flags & MSG_SENDPAGE_NOTLAST);
  1015. sk_clear_bit(SOCKWQ_ASYNC_NOSPACE, sk);
  1016. /* Call the sk_stream functions to manage the sndbuf mem. */
  1017. while (size > 0) {
  1018. size_t copy, required_size;
  1019. if (sk->sk_err) {
  1020. ret = -sk->sk_err;
  1021. goto sendpage_end;
  1022. }
  1023. if (ctx->open_rec)
  1024. rec = ctx->open_rec;
  1025. else
  1026. rec = ctx->open_rec = tls_get_rec(sk);
  1027. if (!rec) {
  1028. ret = -ENOMEM;
  1029. goto sendpage_end;
  1030. }
  1031. msg_pl = &rec->msg_plaintext;
  1032. full_record = false;
  1033. record_room = TLS_MAX_PAYLOAD_SIZE - msg_pl->sg.size;
  1034. copy = size;
  1035. if (copy >= record_room) {
  1036. copy = record_room;
  1037. full_record = true;
  1038. }
  1039. required_size = msg_pl->sg.size + copy + prot->overhead_size;
  1040. if (!sk_stream_memory_free(sk))
  1041. goto wait_for_sndbuf;
  1042. alloc_payload:
  1043. ret = tls_alloc_encrypted_msg(sk, required_size);
  1044. if (ret) {
  1045. if (ret != -ENOSPC)
  1046. goto wait_for_memory;
  1047. /* Adjust copy according to the amount that was
  1048. * actually allocated. The difference is due
  1049. * to max sg elements limit
  1050. */
  1051. copy -= required_size - msg_pl->sg.size;
  1052. full_record = true;
  1053. }
  1054. sk_msg_page_add(msg_pl, page, copy, offset);
  1055. msg_pl->sg.copybreak = 0;
  1056. msg_pl->sg.curr = msg_pl->sg.end;
  1057. sk_mem_charge(sk, copy);
  1058. offset += copy;
  1059. size -= copy;
  1060. copied += copy;
  1061. tls_ctx->pending_open_record_frags = true;
  1062. if (full_record || eor || sk_msg_full(msg_pl)) {
  1063. ret = bpf_exec_tx_verdict(msg_pl, sk, full_record,
  1064. record_type, &copied, flags);
  1065. if (ret) {
  1066. if (ret == -EINPROGRESS)
  1067. num_async++;
  1068. else if (ret == -ENOMEM)
  1069. goto wait_for_memory;
  1070. else if (ret != -EAGAIN) {
  1071. if (ret == -ENOSPC)
  1072. ret = 0;
  1073. goto sendpage_end;
  1074. }
  1075. }
  1076. }
  1077. continue;
  1078. wait_for_sndbuf:
  1079. set_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
  1080. wait_for_memory:
  1081. ret = sk_stream_wait_memory(sk, &timeo);
  1082. if (ret) {
  1083. if (ctx->open_rec)
  1084. tls_trim_both_msgs(sk, msg_pl->sg.size);
  1085. goto sendpage_end;
  1086. }
  1087. if (ctx->open_rec)
  1088. goto alloc_payload;
  1089. }
  1090. if (num_async) {
  1091. /* Transmit if any encryptions have completed */
  1092. if (test_and_clear_bit(BIT_TX_SCHEDULED, &ctx->tx_bitmask)) {
  1093. cancel_delayed_work(&ctx->tx_work.work);
  1094. tls_tx_records(sk, flags);
  1095. }
  1096. }
  1097. sendpage_end:
  1098. ret = sk_stream_error(sk, flags, ret);
  1099. return copied > 0 ? copied : ret;
  1100. }
  1101. int tls_sw_sendpage_locked(struct sock *sk, struct page *page,
  1102. int offset, size_t size, int flags)
  1103. {
  1104. if (flags & ~(MSG_MORE | MSG_DONTWAIT | MSG_NOSIGNAL |
  1105. MSG_SENDPAGE_NOTLAST | MSG_SENDPAGE_NOPOLICY |
  1106. MSG_NO_SHARED_FRAGS))
  1107. return -EOPNOTSUPP;
  1108. return tls_sw_do_sendpage(sk, page, offset, size, flags);
  1109. }
  1110. int tls_sw_sendpage(struct sock *sk, struct page *page,
  1111. int offset, size_t size, int flags)
  1112. {
  1113. struct tls_context *tls_ctx = tls_get_ctx(sk);
  1114. int ret;
  1115. if (flags & ~(MSG_MORE | MSG_DONTWAIT | MSG_NOSIGNAL |
  1116. MSG_SENDPAGE_NOTLAST | MSG_SENDPAGE_NOPOLICY))
  1117. return -EOPNOTSUPP;
  1118. ret = mutex_lock_interruptible(&tls_ctx->tx_lock);
  1119. if (ret)
  1120. return ret;
  1121. lock_sock(sk);
  1122. ret = tls_sw_do_sendpage(sk, page, offset, size, flags);
  1123. release_sock(sk);
  1124. mutex_unlock(&tls_ctx->tx_lock);
  1125. return ret;
  1126. }
  1127. static int
  1128. tls_rx_rec_wait(struct sock *sk, struct sk_psock *psock, bool nonblock,
  1129. bool released)
  1130. {
  1131. struct tls_context *tls_ctx = tls_get_ctx(sk);
  1132. struct tls_sw_context_rx *ctx = tls_sw_ctx_rx(tls_ctx);
  1133. DEFINE_WAIT_FUNC(wait, woken_wake_function);
  1134. long timeo;
  1135. timeo = sock_rcvtimeo(sk, nonblock);
  1136. while (!tls_strp_msg_ready(ctx)) {
  1137. if (!sk_psock_queue_empty(psock))
  1138. return 0;
  1139. if (sk->sk_err)
  1140. return sock_error(sk);
  1141. if (!skb_queue_empty(&sk->sk_receive_queue)) {
  1142. tls_strp_check_rcv(&ctx->strp);
  1143. if (tls_strp_msg_ready(ctx))
  1144. break;
  1145. }
  1146. if (sk->sk_shutdown & RCV_SHUTDOWN)
  1147. return 0;
  1148. if (sock_flag(sk, SOCK_DONE))
  1149. return 0;
  1150. if (!timeo)
  1151. return -EAGAIN;
  1152. released = true;
  1153. add_wait_queue(sk_sleep(sk), &wait);
  1154. sk_set_bit(SOCKWQ_ASYNC_WAITDATA, sk);
  1155. sk_wait_event(sk, &timeo,
  1156. tls_strp_msg_ready(ctx) ||
  1157. !sk_psock_queue_empty(psock),
  1158. &wait);
  1159. sk_clear_bit(SOCKWQ_ASYNC_WAITDATA, sk);
  1160. remove_wait_queue(sk_sleep(sk), &wait);
  1161. /* Handle signals */
  1162. if (signal_pending(current))
  1163. return sock_intr_errno(timeo);
  1164. }
  1165. tls_strp_msg_load(&ctx->strp, released);
  1166. return 1;
  1167. }
  1168. static int tls_setup_from_iter(struct iov_iter *from,
  1169. int length, int *pages_used,
  1170. struct scatterlist *to,
  1171. int to_max_pages)
  1172. {
  1173. int rc = 0, i = 0, num_elem = *pages_used, maxpages;
  1174. struct page *pages[MAX_SKB_FRAGS];
  1175. unsigned int size = 0;
  1176. ssize_t copied, use;
  1177. size_t offset;
  1178. while (length > 0) {
  1179. i = 0;
  1180. maxpages = to_max_pages - num_elem;
  1181. if (maxpages == 0) {
  1182. rc = -EFAULT;
  1183. goto out;
  1184. }
  1185. copied = iov_iter_get_pages2(from, pages,
  1186. length,
  1187. maxpages, &offset);
  1188. if (copied <= 0) {
  1189. rc = -EFAULT;
  1190. goto out;
  1191. }
  1192. length -= copied;
  1193. size += copied;
  1194. while (copied) {
  1195. use = min_t(int, copied, PAGE_SIZE - offset);
  1196. sg_set_page(&to[num_elem],
  1197. pages[i], use, offset);
  1198. sg_unmark_end(&to[num_elem]);
  1199. /* We do not uncharge memory from this API */
  1200. offset = 0;
  1201. copied -= use;
  1202. i++;
  1203. num_elem++;
  1204. }
  1205. }
  1206. /* Mark the end in the last sg entry if newly added */
  1207. if (num_elem > *pages_used)
  1208. sg_mark_end(&to[num_elem - 1]);
  1209. out:
  1210. if (rc)
  1211. iov_iter_revert(from, size);
  1212. *pages_used = num_elem;
  1213. return rc;
  1214. }
  1215. static struct sk_buff *
  1216. tls_alloc_clrtxt_skb(struct sock *sk, struct sk_buff *skb,
  1217. unsigned int full_len)
  1218. {
  1219. struct strp_msg *clr_rxm;
  1220. struct sk_buff *clr_skb;
  1221. int err;
  1222. clr_skb = alloc_skb_with_frags(0, full_len, TLS_PAGE_ORDER,
  1223. &err, sk->sk_allocation);
  1224. if (!clr_skb)
  1225. return NULL;
  1226. skb_copy_header(clr_skb, skb);
  1227. clr_skb->len = full_len;
  1228. clr_skb->data_len = full_len;
  1229. clr_rxm = strp_msg(clr_skb);
  1230. clr_rxm->offset = 0;
  1231. return clr_skb;
  1232. }
  1233. /* Decrypt handlers
  1234. *
  1235. * tls_decrypt_sw() and tls_decrypt_device() are decrypt handlers.
  1236. * They must transform the darg in/out argument are as follows:
  1237. * | Input | Output
  1238. * -------------------------------------------------------------------
  1239. * zc | Zero-copy decrypt allowed | Zero-copy performed
  1240. * async | Async decrypt allowed | Async crypto used / in progress
  1241. * skb | * | Output skb
  1242. *
  1243. * If ZC decryption was performed darg.skb will point to the input skb.
  1244. */
  1245. /* This function decrypts the input skb into either out_iov or in out_sg
  1246. * or in skb buffers itself. The input parameter 'darg->zc' indicates if
  1247. * zero-copy mode needs to be tried or not. With zero-copy mode, either
  1248. * out_iov or out_sg must be non-NULL. In case both out_iov and out_sg are
  1249. * NULL, then the decryption happens inside skb buffers itself, i.e.
  1250. * zero-copy gets disabled and 'darg->zc' is updated.
  1251. */
  1252. static int tls_decrypt_sg(struct sock *sk, struct iov_iter *out_iov,
  1253. struct scatterlist *out_sg,
  1254. struct tls_decrypt_arg *darg)
  1255. {
  1256. struct tls_context *tls_ctx = tls_get_ctx(sk);
  1257. struct tls_sw_context_rx *ctx = tls_sw_ctx_rx(tls_ctx);
  1258. struct tls_prot_info *prot = &tls_ctx->prot_info;
  1259. int n_sgin, n_sgout, aead_size, err, pages = 0;
  1260. struct sk_buff *skb = tls_strp_msg(ctx);
  1261. const struct strp_msg *rxm = strp_msg(skb);
  1262. const struct tls_msg *tlm = tls_msg(skb);
  1263. struct aead_request *aead_req;
  1264. struct scatterlist *sgin = NULL;
  1265. struct scatterlist *sgout = NULL;
  1266. const int data_len = rxm->full_len - prot->overhead_size;
  1267. int tail_pages = !!prot->tail_size;
  1268. struct tls_decrypt_ctx *dctx;
  1269. struct sk_buff *clear_skb;
  1270. int iv_offset = 0;
  1271. u8 *mem;
  1272. n_sgin = skb_nsg(skb, rxm->offset + prot->prepend_size,
  1273. rxm->full_len - prot->prepend_size);
  1274. if (n_sgin < 1)
  1275. return n_sgin ?: -EBADMSG;
  1276. if (darg->zc && (out_iov || out_sg)) {
  1277. clear_skb = NULL;
  1278. if (out_iov)
  1279. n_sgout = 1 + tail_pages +
  1280. iov_iter_npages_cap(out_iov, INT_MAX, data_len);
  1281. else
  1282. n_sgout = sg_nents(out_sg);
  1283. } else {
  1284. darg->zc = false;
  1285. clear_skb = tls_alloc_clrtxt_skb(sk, skb, rxm->full_len);
  1286. if (!clear_skb)
  1287. return -ENOMEM;
  1288. n_sgout = 1 + skb_shinfo(clear_skb)->nr_frags;
  1289. }
  1290. /* Increment to accommodate AAD */
  1291. n_sgin = n_sgin + 1;
  1292. /* Allocate a single block of memory which contains
  1293. * aead_req || tls_decrypt_ctx.
  1294. * Both structs are variable length.
  1295. */
  1296. aead_size = sizeof(*aead_req) + crypto_aead_reqsize(ctx->aead_recv);
  1297. aead_size = ALIGN(aead_size, __alignof__(*dctx));
  1298. mem = kmalloc(aead_size + struct_size(dctx, sg, size_add(n_sgin, n_sgout)),
  1299. sk->sk_allocation);
  1300. if (!mem) {
  1301. err = -ENOMEM;
  1302. goto exit_free_skb;
  1303. }
  1304. /* Segment the allocated memory */
  1305. aead_req = (struct aead_request *)mem;
  1306. dctx = (struct tls_decrypt_ctx *)(mem + aead_size);
  1307. dctx->sk = sk;
  1308. sgin = &dctx->sg[0];
  1309. sgout = &dctx->sg[n_sgin];
  1310. /* For CCM based ciphers, first byte of nonce+iv is a constant */
  1311. switch (prot->cipher_type) {
  1312. case TLS_CIPHER_AES_CCM_128:
  1313. dctx->iv[0] = TLS_AES_CCM_IV_B0_BYTE;
  1314. iv_offset = 1;
  1315. break;
  1316. case TLS_CIPHER_SM4_CCM:
  1317. dctx->iv[0] = TLS_SM4_CCM_IV_B0_BYTE;
  1318. iv_offset = 1;
  1319. break;
  1320. }
  1321. /* Prepare IV */
  1322. if (prot->version == TLS_1_3_VERSION ||
  1323. prot->cipher_type == TLS_CIPHER_CHACHA20_POLY1305) {
  1324. memcpy(&dctx->iv[iv_offset], tls_ctx->rx.iv,
  1325. prot->iv_size + prot->salt_size);
  1326. } else {
  1327. err = skb_copy_bits(skb, rxm->offset + TLS_HEADER_SIZE,
  1328. &dctx->iv[iv_offset] + prot->salt_size,
  1329. prot->iv_size);
  1330. if (err < 0)
  1331. goto exit_free;
  1332. memcpy(&dctx->iv[iv_offset], tls_ctx->rx.iv, prot->salt_size);
  1333. }
  1334. tls_xor_iv_with_seq(prot, &dctx->iv[iv_offset], tls_ctx->rx.rec_seq);
  1335. /* Prepare AAD */
  1336. tls_make_aad(dctx->aad, rxm->full_len - prot->overhead_size +
  1337. prot->tail_size,
  1338. tls_ctx->rx.rec_seq, tlm->control, prot);
  1339. /* Prepare sgin */
  1340. sg_init_table(sgin, n_sgin);
  1341. sg_set_buf(&sgin[0], dctx->aad, prot->aad_size);
  1342. err = skb_to_sgvec(skb, &sgin[1],
  1343. rxm->offset + prot->prepend_size,
  1344. rxm->full_len - prot->prepend_size);
  1345. if (err < 0)
  1346. goto exit_free;
  1347. if (clear_skb) {
  1348. sg_init_table(sgout, n_sgout);
  1349. sg_set_buf(&sgout[0], dctx->aad, prot->aad_size);
  1350. err = skb_to_sgvec(clear_skb, &sgout[1], prot->prepend_size,
  1351. data_len + prot->tail_size);
  1352. if (err < 0)
  1353. goto exit_free;
  1354. } else if (out_iov) {
  1355. sg_init_table(sgout, n_sgout);
  1356. sg_set_buf(&sgout[0], dctx->aad, prot->aad_size);
  1357. err = tls_setup_from_iter(out_iov, data_len, &pages, &sgout[1],
  1358. (n_sgout - 1 - tail_pages));
  1359. if (err < 0)
  1360. goto exit_free_pages;
  1361. if (prot->tail_size) {
  1362. sg_unmark_end(&sgout[pages]);
  1363. sg_set_buf(&sgout[pages + 1], &dctx->tail,
  1364. prot->tail_size);
  1365. sg_mark_end(&sgout[pages + 1]);
  1366. }
  1367. } else if (out_sg) {
  1368. memcpy(sgout, out_sg, n_sgout * sizeof(*sgout));
  1369. }
  1370. dctx->free_sgout = !!pages;
  1371. /* Prepare and submit AEAD request */
  1372. err = tls_do_decryption(sk, sgin, sgout, dctx->iv,
  1373. data_len + prot->tail_size, aead_req, darg);
  1374. if (err)
  1375. goto exit_free_pages;
  1376. darg->skb = clear_skb ?: tls_strp_msg(ctx);
  1377. clear_skb = NULL;
  1378. if (unlikely(darg->async)) {
  1379. err = tls_strp_msg_hold(&ctx->strp, &ctx->async_hold);
  1380. if (err)
  1381. __skb_queue_tail(&ctx->async_hold, darg->skb);
  1382. return err;
  1383. }
  1384. if (prot->tail_size)
  1385. darg->tail = dctx->tail;
  1386. exit_free_pages:
  1387. /* Release the pages in case iov was mapped to pages */
  1388. for (; pages > 0; pages--)
  1389. put_page(sg_page(&sgout[pages]));
  1390. exit_free:
  1391. kfree(mem);
  1392. exit_free_skb:
  1393. consume_skb(clear_skb);
  1394. return err;
  1395. }
  1396. static int
  1397. tls_decrypt_sw(struct sock *sk, struct tls_context *tls_ctx,
  1398. struct msghdr *msg, struct tls_decrypt_arg *darg)
  1399. {
  1400. struct tls_sw_context_rx *ctx = tls_sw_ctx_rx(tls_ctx);
  1401. struct tls_prot_info *prot = &tls_ctx->prot_info;
  1402. struct strp_msg *rxm;
  1403. int pad, err;
  1404. err = tls_decrypt_sg(sk, &msg->msg_iter, NULL, darg);
  1405. if (err < 0) {
  1406. if (err == -EBADMSG)
  1407. TLS_INC_STATS(sock_net(sk), LINUX_MIB_TLSDECRYPTERROR);
  1408. return err;
  1409. }
  1410. /* keep going even for ->async, the code below is TLS 1.3 */
  1411. /* If opportunistic TLS 1.3 ZC failed retry without ZC */
  1412. if (unlikely(darg->zc && prot->version == TLS_1_3_VERSION &&
  1413. darg->tail != TLS_RECORD_TYPE_DATA)) {
  1414. darg->zc = false;
  1415. if (!darg->tail)
  1416. TLS_INC_STATS(sock_net(sk), LINUX_MIB_TLSRXNOPADVIOL);
  1417. TLS_INC_STATS(sock_net(sk), LINUX_MIB_TLSDECRYPTRETRY);
  1418. return tls_decrypt_sw(sk, tls_ctx, msg, darg);
  1419. }
  1420. pad = tls_padding_length(prot, darg->skb, darg);
  1421. if (pad < 0) {
  1422. if (darg->skb != tls_strp_msg(ctx))
  1423. consume_skb(darg->skb);
  1424. return pad;
  1425. }
  1426. rxm = strp_msg(darg->skb);
  1427. rxm->full_len -= pad;
  1428. return 0;
  1429. }
  1430. static int
  1431. tls_decrypt_device(struct sock *sk, struct msghdr *msg,
  1432. struct tls_context *tls_ctx, struct tls_decrypt_arg *darg)
  1433. {
  1434. struct tls_sw_context_rx *ctx = tls_sw_ctx_rx(tls_ctx);
  1435. struct tls_prot_info *prot = &tls_ctx->prot_info;
  1436. struct strp_msg *rxm;
  1437. int pad, err;
  1438. if (tls_ctx->rx_conf != TLS_HW)
  1439. return 0;
  1440. err = tls_device_decrypted(sk, tls_ctx);
  1441. if (err <= 0)
  1442. return err;
  1443. pad = tls_padding_length(prot, tls_strp_msg(ctx), darg);
  1444. if (pad < 0)
  1445. return pad;
  1446. darg->async = false;
  1447. darg->skb = tls_strp_msg(ctx);
  1448. /* ->zc downgrade check, in case TLS 1.3 gets here */
  1449. darg->zc &= !(prot->version == TLS_1_3_VERSION &&
  1450. tls_msg(darg->skb)->control != TLS_RECORD_TYPE_DATA);
  1451. rxm = strp_msg(darg->skb);
  1452. rxm->full_len -= pad;
  1453. if (!darg->zc) {
  1454. /* Non-ZC case needs a real skb */
  1455. darg->skb = tls_strp_msg_detach(ctx);
  1456. if (!darg->skb)
  1457. return -ENOMEM;
  1458. } else {
  1459. unsigned int off, len;
  1460. /* In ZC case nobody cares about the output skb.
  1461. * Just copy the data here. Note the skb is not fully trimmed.
  1462. */
  1463. off = rxm->offset + prot->prepend_size;
  1464. len = rxm->full_len - prot->overhead_size;
  1465. err = skb_copy_datagram_msg(darg->skb, off, msg, len);
  1466. if (err)
  1467. return err;
  1468. }
  1469. return 1;
  1470. }
  1471. static int tls_rx_one_record(struct sock *sk, struct msghdr *msg,
  1472. struct tls_decrypt_arg *darg)
  1473. {
  1474. struct tls_context *tls_ctx = tls_get_ctx(sk);
  1475. struct tls_prot_info *prot = &tls_ctx->prot_info;
  1476. struct strp_msg *rxm;
  1477. int err;
  1478. err = tls_decrypt_device(sk, msg, tls_ctx, darg);
  1479. if (!err)
  1480. err = tls_decrypt_sw(sk, tls_ctx, msg, darg);
  1481. if (err < 0)
  1482. return err;
  1483. rxm = strp_msg(darg->skb);
  1484. rxm->offset += prot->prepend_size;
  1485. rxm->full_len -= prot->overhead_size;
  1486. tls_advance_record_sn(sk, prot, &tls_ctx->rx);
  1487. return 0;
  1488. }
  1489. int decrypt_skb(struct sock *sk, struct scatterlist *sgout)
  1490. {
  1491. struct tls_decrypt_arg darg = { .zc = true, };
  1492. return tls_decrypt_sg(sk, NULL, sgout, &darg);
  1493. }
  1494. static int tls_record_content_type(struct msghdr *msg, struct tls_msg *tlm,
  1495. u8 *control)
  1496. {
  1497. int err;
  1498. if (!*control) {
  1499. *control = tlm->control;
  1500. if (!*control)
  1501. return -EBADMSG;
  1502. err = put_cmsg(msg, SOL_TLS, TLS_GET_RECORD_TYPE,
  1503. sizeof(*control), control);
  1504. if (*control != TLS_RECORD_TYPE_DATA) {
  1505. if (err || msg->msg_flags & MSG_CTRUNC)
  1506. return -EIO;
  1507. }
  1508. } else if (*control != tlm->control) {
  1509. return 0;
  1510. }
  1511. return 1;
  1512. }
  1513. static void tls_rx_rec_done(struct tls_sw_context_rx *ctx)
  1514. {
  1515. tls_strp_msg_done(&ctx->strp);
  1516. }
  1517. /* This function traverses the rx_list in tls receive context to copies the
  1518. * decrypted records into the buffer provided by caller zero copy is not
  1519. * true. Further, the records are removed from the rx_list if it is not a peek
  1520. * case and the record has been consumed completely.
  1521. */
  1522. static int process_rx_list(struct tls_sw_context_rx *ctx,
  1523. struct msghdr *msg,
  1524. u8 *control,
  1525. size_t skip,
  1526. size_t len,
  1527. bool is_peek)
  1528. {
  1529. struct sk_buff *skb = skb_peek(&ctx->rx_list);
  1530. struct tls_msg *tlm;
  1531. ssize_t copied = 0;
  1532. int err;
  1533. while (skip && skb) {
  1534. struct strp_msg *rxm = strp_msg(skb);
  1535. tlm = tls_msg(skb);
  1536. err = tls_record_content_type(msg, tlm, control);
  1537. if (err <= 0)
  1538. goto out;
  1539. if (skip < rxm->full_len)
  1540. break;
  1541. skip = skip - rxm->full_len;
  1542. skb = skb_peek_next(skb, &ctx->rx_list);
  1543. }
  1544. while (len && skb) {
  1545. struct sk_buff *next_skb;
  1546. struct strp_msg *rxm = strp_msg(skb);
  1547. int chunk = min_t(unsigned int, rxm->full_len - skip, len);
  1548. tlm = tls_msg(skb);
  1549. err = tls_record_content_type(msg, tlm, control);
  1550. if (err <= 0)
  1551. goto out;
  1552. err = skb_copy_datagram_msg(skb, rxm->offset + skip,
  1553. msg, chunk);
  1554. if (err < 0)
  1555. goto out;
  1556. len = len - chunk;
  1557. copied = copied + chunk;
  1558. /* Consume the data from record if it is non-peek case*/
  1559. if (!is_peek) {
  1560. rxm->offset = rxm->offset + chunk;
  1561. rxm->full_len = rxm->full_len - chunk;
  1562. /* Return if there is unconsumed data in the record */
  1563. if (rxm->full_len - skip)
  1564. break;
  1565. }
  1566. /* The remaining skip-bytes must lie in 1st record in rx_list.
  1567. * So from the 2nd record, 'skip' should be 0.
  1568. */
  1569. skip = 0;
  1570. if (msg)
  1571. msg->msg_flags |= MSG_EOR;
  1572. next_skb = skb_peek_next(skb, &ctx->rx_list);
  1573. if (!is_peek) {
  1574. __skb_unlink(skb, &ctx->rx_list);
  1575. consume_skb(skb);
  1576. }
  1577. skb = next_skb;
  1578. }
  1579. err = 0;
  1580. out:
  1581. return copied ? : err;
  1582. }
  1583. static bool
  1584. tls_read_flush_backlog(struct sock *sk, struct tls_prot_info *prot,
  1585. size_t len_left, size_t decrypted, ssize_t done,
  1586. size_t *flushed_at)
  1587. {
  1588. size_t max_rec;
  1589. if (len_left <= decrypted)
  1590. return false;
  1591. max_rec = prot->overhead_size - prot->tail_size + TLS_MAX_PAYLOAD_SIZE;
  1592. if (done - *flushed_at < SZ_128K && tcp_inq(sk) > max_rec)
  1593. return false;
  1594. *flushed_at = done;
  1595. return sk_flush_backlog(sk);
  1596. }
  1597. static int tls_rx_reader_acquire(struct sock *sk, struct tls_sw_context_rx *ctx,
  1598. bool nonblock)
  1599. {
  1600. long timeo;
  1601. timeo = sock_rcvtimeo(sk, nonblock);
  1602. while (unlikely(ctx->reader_present)) {
  1603. DEFINE_WAIT_FUNC(wait, woken_wake_function);
  1604. ctx->reader_contended = 1;
  1605. add_wait_queue(&ctx->wq, &wait);
  1606. sk_wait_event(sk, &timeo,
  1607. !READ_ONCE(ctx->reader_present), &wait);
  1608. remove_wait_queue(&ctx->wq, &wait);
  1609. if (timeo <= 0)
  1610. return -EAGAIN;
  1611. if (signal_pending(current))
  1612. return sock_intr_errno(timeo);
  1613. }
  1614. WRITE_ONCE(ctx->reader_present, 1);
  1615. return 0;
  1616. }
  1617. static int tls_rx_reader_lock(struct sock *sk, struct tls_sw_context_rx *ctx,
  1618. bool nonblock)
  1619. {
  1620. int err;
  1621. lock_sock(sk);
  1622. err = tls_rx_reader_acquire(sk, ctx, nonblock);
  1623. if (err)
  1624. release_sock(sk);
  1625. return err;
  1626. }
  1627. static void tls_rx_reader_release(struct sock *sk, struct tls_sw_context_rx *ctx)
  1628. {
  1629. if (unlikely(ctx->reader_contended)) {
  1630. if (wq_has_sleeper(&ctx->wq))
  1631. wake_up(&ctx->wq);
  1632. else
  1633. ctx->reader_contended = 0;
  1634. WARN_ON_ONCE(!ctx->reader_present);
  1635. }
  1636. WRITE_ONCE(ctx->reader_present, 0);
  1637. }
  1638. static void tls_rx_reader_unlock(struct sock *sk, struct tls_sw_context_rx *ctx)
  1639. {
  1640. tls_rx_reader_release(sk, ctx);
  1641. release_sock(sk);
  1642. }
  1643. int tls_sw_recvmsg(struct sock *sk,
  1644. struct msghdr *msg,
  1645. size_t len,
  1646. int flags,
  1647. int *addr_len)
  1648. {
  1649. struct tls_context *tls_ctx = tls_get_ctx(sk);
  1650. struct tls_sw_context_rx *ctx = tls_sw_ctx_rx(tls_ctx);
  1651. struct tls_prot_info *prot = &tls_ctx->prot_info;
  1652. ssize_t decrypted = 0, async_copy_bytes = 0;
  1653. struct sk_psock *psock;
  1654. unsigned char control = 0;
  1655. size_t flushed_at = 0;
  1656. struct strp_msg *rxm;
  1657. struct tls_msg *tlm;
  1658. ssize_t copied = 0;
  1659. bool async = false;
  1660. int target, err;
  1661. bool is_kvec = iov_iter_is_kvec(&msg->msg_iter);
  1662. bool is_peek = flags & MSG_PEEK;
  1663. bool released = true;
  1664. bool bpf_strp_enabled;
  1665. bool zc_capable;
  1666. if (unlikely(flags & MSG_ERRQUEUE))
  1667. return sock_recv_errqueue(sk, msg, len, SOL_IP, IP_RECVERR);
  1668. psock = sk_psock_get(sk);
  1669. err = tls_rx_reader_lock(sk, ctx, flags & MSG_DONTWAIT);
  1670. if (err < 0)
  1671. return err;
  1672. bpf_strp_enabled = sk_psock_strp_enabled(psock);
  1673. /* If crypto failed the connection is broken */
  1674. err = ctx->async_wait.err;
  1675. if (err)
  1676. goto end;
  1677. /* Process pending decrypted records. It must be non-zero-copy */
  1678. err = process_rx_list(ctx, msg, &control, 0, len, is_peek);
  1679. if (err < 0)
  1680. goto end;
  1681. copied = err;
  1682. if (len <= copied)
  1683. goto end;
  1684. target = sock_rcvlowat(sk, flags & MSG_WAITALL, len);
  1685. len = len - copied;
  1686. zc_capable = !bpf_strp_enabled && !is_kvec && !is_peek &&
  1687. ctx->zc_capable;
  1688. decrypted = 0;
  1689. while (len && (decrypted + copied < target || tls_strp_msg_ready(ctx))) {
  1690. struct tls_decrypt_arg darg;
  1691. int to_decrypt, chunk;
  1692. err = tls_rx_rec_wait(sk, psock, flags & MSG_DONTWAIT,
  1693. released);
  1694. if (err <= 0) {
  1695. if (psock) {
  1696. chunk = sk_msg_recvmsg(sk, psock, msg, len,
  1697. flags);
  1698. if (chunk > 0) {
  1699. decrypted += chunk;
  1700. len -= chunk;
  1701. continue;
  1702. }
  1703. }
  1704. goto recv_end;
  1705. }
  1706. memset(&darg.inargs, 0, sizeof(darg.inargs));
  1707. rxm = strp_msg(tls_strp_msg(ctx));
  1708. tlm = tls_msg(tls_strp_msg(ctx));
  1709. to_decrypt = rxm->full_len - prot->overhead_size;
  1710. if (zc_capable && to_decrypt <= len &&
  1711. tlm->control == TLS_RECORD_TYPE_DATA)
  1712. darg.zc = true;
  1713. /* Do not use async mode if record is non-data */
  1714. if (tlm->control == TLS_RECORD_TYPE_DATA && !bpf_strp_enabled)
  1715. darg.async = ctx->async_capable;
  1716. else
  1717. darg.async = false;
  1718. err = tls_rx_one_record(sk, msg, &darg);
  1719. if (err < 0) {
  1720. tls_err_abort(sk, -EBADMSG);
  1721. goto recv_end;
  1722. }
  1723. async |= darg.async;
  1724. /* If the type of records being processed is not known yet,
  1725. * set it to record type just dequeued. If it is already known,
  1726. * but does not match the record type just dequeued, go to end.
  1727. * We always get record type here since for tls1.2, record type
  1728. * is known just after record is dequeued from stream parser.
  1729. * For tls1.3, we disable async.
  1730. */
  1731. err = tls_record_content_type(msg, tls_msg(darg.skb), &control);
  1732. if (err <= 0) {
  1733. DEBUG_NET_WARN_ON_ONCE(darg.zc);
  1734. tls_rx_rec_done(ctx);
  1735. put_on_rx_list_err:
  1736. __skb_queue_tail(&ctx->rx_list, darg.skb);
  1737. goto recv_end;
  1738. }
  1739. /* periodically flush backlog, and feed strparser */
  1740. released = tls_read_flush_backlog(sk, prot, len, to_decrypt,
  1741. decrypted + copied,
  1742. &flushed_at);
  1743. /* TLS 1.3 may have updated the length by more than overhead */
  1744. rxm = strp_msg(darg.skb);
  1745. chunk = rxm->full_len;
  1746. tls_rx_rec_done(ctx);
  1747. if (!darg.zc) {
  1748. bool partially_consumed = chunk > len;
  1749. struct sk_buff *skb = darg.skb;
  1750. DEBUG_NET_WARN_ON_ONCE(darg.skb == ctx->strp.anchor);
  1751. if (async) {
  1752. /* TLS 1.2-only, to_decrypt must be text len */
  1753. chunk = min_t(int, to_decrypt, len);
  1754. async_copy_bytes += chunk;
  1755. put_on_rx_list:
  1756. decrypted += chunk;
  1757. len -= chunk;
  1758. __skb_queue_tail(&ctx->rx_list, skb);
  1759. continue;
  1760. }
  1761. if (bpf_strp_enabled) {
  1762. released = true;
  1763. err = sk_psock_tls_strp_read(psock, skb);
  1764. if (err != __SK_PASS) {
  1765. rxm->offset = rxm->offset + rxm->full_len;
  1766. rxm->full_len = 0;
  1767. if (err == __SK_DROP)
  1768. consume_skb(skb);
  1769. continue;
  1770. }
  1771. }
  1772. if (partially_consumed)
  1773. chunk = len;
  1774. err = skb_copy_datagram_msg(skb, rxm->offset,
  1775. msg, chunk);
  1776. if (err < 0)
  1777. goto put_on_rx_list_err;
  1778. if (is_peek)
  1779. goto put_on_rx_list;
  1780. if (partially_consumed) {
  1781. rxm->offset += chunk;
  1782. rxm->full_len -= chunk;
  1783. goto put_on_rx_list;
  1784. }
  1785. consume_skb(skb);
  1786. }
  1787. decrypted += chunk;
  1788. len -= chunk;
  1789. /* Return full control message to userspace before trying
  1790. * to parse another message type
  1791. */
  1792. msg->msg_flags |= MSG_EOR;
  1793. if (control != TLS_RECORD_TYPE_DATA)
  1794. break;
  1795. }
  1796. recv_end:
  1797. if (async) {
  1798. int ret, pending;
  1799. /* Wait for all previously submitted records to be decrypted */
  1800. spin_lock_bh(&ctx->decrypt_compl_lock);
  1801. reinit_completion(&ctx->async_wait.completion);
  1802. pending = atomic_read(&ctx->decrypt_pending);
  1803. spin_unlock_bh(&ctx->decrypt_compl_lock);
  1804. ret = 0;
  1805. if (pending)
  1806. ret = crypto_wait_req(-EINPROGRESS, &ctx->async_wait);
  1807. __skb_queue_purge(&ctx->async_hold);
  1808. if (ret) {
  1809. if (err >= 0 || err == -EINPROGRESS)
  1810. err = ret;
  1811. decrypted = 0;
  1812. goto end;
  1813. }
  1814. /* Drain records from the rx_list & copy if required */
  1815. if (is_peek || is_kvec)
  1816. err = process_rx_list(ctx, msg, &control, copied,
  1817. decrypted, is_peek);
  1818. else
  1819. err = process_rx_list(ctx, msg, &control, 0,
  1820. async_copy_bytes, is_peek);
  1821. decrypted += max(err, 0);
  1822. }
  1823. copied += decrypted;
  1824. end:
  1825. tls_rx_reader_unlock(sk, ctx);
  1826. if (psock)
  1827. sk_psock_put(sk, psock);
  1828. return copied ? : err;
  1829. }
  1830. ssize_t tls_sw_splice_read(struct socket *sock, loff_t *ppos,
  1831. struct pipe_inode_info *pipe,
  1832. size_t len, unsigned int flags)
  1833. {
  1834. struct tls_context *tls_ctx = tls_get_ctx(sock->sk);
  1835. struct tls_sw_context_rx *ctx = tls_sw_ctx_rx(tls_ctx);
  1836. struct strp_msg *rxm = NULL;
  1837. struct sock *sk = sock->sk;
  1838. struct tls_msg *tlm;
  1839. struct sk_buff *skb;
  1840. ssize_t copied = 0;
  1841. int chunk;
  1842. int err;
  1843. err = tls_rx_reader_lock(sk, ctx, flags & SPLICE_F_NONBLOCK);
  1844. if (err < 0)
  1845. return err;
  1846. if (!skb_queue_empty(&ctx->rx_list)) {
  1847. skb = __skb_dequeue(&ctx->rx_list);
  1848. } else {
  1849. struct tls_decrypt_arg darg;
  1850. err = tls_rx_rec_wait(sk, NULL, flags & SPLICE_F_NONBLOCK,
  1851. true);
  1852. if (err <= 0)
  1853. goto splice_read_end;
  1854. memset(&darg.inargs, 0, sizeof(darg.inargs));
  1855. err = tls_rx_one_record(sk, NULL, &darg);
  1856. if (err < 0) {
  1857. tls_err_abort(sk, -EBADMSG);
  1858. goto splice_read_end;
  1859. }
  1860. tls_rx_rec_done(ctx);
  1861. skb = darg.skb;
  1862. }
  1863. rxm = strp_msg(skb);
  1864. tlm = tls_msg(skb);
  1865. /* splice does not support reading control messages */
  1866. if (tlm->control != TLS_RECORD_TYPE_DATA) {
  1867. err = -EINVAL;
  1868. goto splice_requeue;
  1869. }
  1870. chunk = min_t(unsigned int, rxm->full_len, len);
  1871. copied = skb_splice_bits(skb, sk, rxm->offset, pipe, chunk, flags);
  1872. if (copied < 0)
  1873. goto splice_requeue;
  1874. if (chunk < rxm->full_len) {
  1875. rxm->offset += len;
  1876. rxm->full_len -= len;
  1877. goto splice_requeue;
  1878. }
  1879. consume_skb(skb);
  1880. splice_read_end:
  1881. tls_rx_reader_unlock(sk, ctx);
  1882. return copied ? : err;
  1883. splice_requeue:
  1884. __skb_queue_head(&ctx->rx_list, skb);
  1885. goto splice_read_end;
  1886. }
  1887. bool tls_sw_sock_is_readable(struct sock *sk)
  1888. {
  1889. struct tls_context *tls_ctx = tls_get_ctx(sk);
  1890. struct tls_sw_context_rx *ctx = tls_sw_ctx_rx(tls_ctx);
  1891. bool ingress_empty = true;
  1892. struct sk_psock *psock;
  1893. rcu_read_lock();
  1894. psock = sk_psock(sk);
  1895. if (psock)
  1896. ingress_empty = list_empty(&psock->ingress_msg);
  1897. rcu_read_unlock();
  1898. return !ingress_empty || tls_strp_msg_ready(ctx) ||
  1899. !skb_queue_empty(&ctx->rx_list);
  1900. }
  1901. int tls_rx_msg_size(struct tls_strparser *strp, struct sk_buff *skb)
  1902. {
  1903. struct tls_context *tls_ctx = tls_get_ctx(strp->sk);
  1904. struct tls_prot_info *prot = &tls_ctx->prot_info;
  1905. char header[TLS_HEADER_SIZE + MAX_IV_SIZE];
  1906. size_t cipher_overhead;
  1907. size_t data_len = 0;
  1908. int ret;
  1909. /* Verify that we have a full TLS header, or wait for more data */
  1910. if (strp->stm.offset + prot->prepend_size > skb->len)
  1911. return 0;
  1912. /* Sanity-check size of on-stack buffer. */
  1913. if (WARN_ON(prot->prepend_size > sizeof(header))) {
  1914. ret = -EINVAL;
  1915. goto read_failure;
  1916. }
  1917. /* Linearize header to local buffer */
  1918. ret = skb_copy_bits(skb, strp->stm.offset, header, prot->prepend_size);
  1919. if (ret < 0)
  1920. goto read_failure;
  1921. strp->mark = header[0];
  1922. data_len = ((header[4] & 0xFF) | (header[3] << 8));
  1923. cipher_overhead = prot->tag_size;
  1924. if (prot->version != TLS_1_3_VERSION &&
  1925. prot->cipher_type != TLS_CIPHER_CHACHA20_POLY1305)
  1926. cipher_overhead += prot->iv_size;
  1927. if (data_len > TLS_MAX_PAYLOAD_SIZE + cipher_overhead +
  1928. prot->tail_size) {
  1929. ret = -EMSGSIZE;
  1930. goto read_failure;
  1931. }
  1932. if (data_len < cipher_overhead) {
  1933. ret = -EBADMSG;
  1934. goto read_failure;
  1935. }
  1936. /* Note that both TLS1.3 and TLS1.2 use TLS_1_2 version here */
  1937. if (header[1] != TLS_1_2_VERSION_MINOR ||
  1938. header[2] != TLS_1_2_VERSION_MAJOR) {
  1939. ret = -EINVAL;
  1940. goto read_failure;
  1941. }
  1942. tls_device_rx_resync_new_rec(strp->sk, data_len + TLS_HEADER_SIZE,
  1943. TCP_SKB_CB(skb)->seq + strp->stm.offset);
  1944. return data_len + TLS_HEADER_SIZE;
  1945. read_failure:
  1946. tls_err_abort(strp->sk, ret);
  1947. return ret;
  1948. }
  1949. void tls_rx_msg_ready(struct tls_strparser *strp)
  1950. {
  1951. struct tls_sw_context_rx *ctx;
  1952. ctx = container_of(strp, struct tls_sw_context_rx, strp);
  1953. ctx->saved_data_ready(strp->sk);
  1954. }
  1955. static void tls_data_ready(struct sock *sk)
  1956. {
  1957. struct tls_context *tls_ctx = tls_get_ctx(sk);
  1958. struct tls_sw_context_rx *ctx = tls_sw_ctx_rx(tls_ctx);
  1959. struct sk_psock *psock;
  1960. gfp_t alloc_save;
  1961. alloc_save = sk->sk_allocation;
  1962. sk->sk_allocation = GFP_ATOMIC;
  1963. tls_strp_data_ready(&ctx->strp);
  1964. sk->sk_allocation = alloc_save;
  1965. psock = sk_psock_get(sk);
  1966. if (psock) {
  1967. if (!list_empty(&psock->ingress_msg))
  1968. ctx->saved_data_ready(sk);
  1969. sk_psock_put(sk, psock);
  1970. }
  1971. }
  1972. void tls_sw_cancel_work_tx(struct tls_context *tls_ctx)
  1973. {
  1974. struct tls_sw_context_tx *ctx = tls_sw_ctx_tx(tls_ctx);
  1975. set_bit(BIT_TX_CLOSING, &ctx->tx_bitmask);
  1976. set_bit(BIT_TX_SCHEDULED, &ctx->tx_bitmask);
  1977. cancel_delayed_work_sync(&ctx->tx_work.work);
  1978. }
  1979. void tls_sw_release_resources_tx(struct sock *sk)
  1980. {
  1981. struct tls_context *tls_ctx = tls_get_ctx(sk);
  1982. struct tls_sw_context_tx *ctx = tls_sw_ctx_tx(tls_ctx);
  1983. struct tls_rec *rec, *tmp;
  1984. int pending;
  1985. /* Wait for any pending async encryptions to complete */
  1986. spin_lock_bh(&ctx->encrypt_compl_lock);
  1987. ctx->async_notify = true;
  1988. pending = atomic_read(&ctx->encrypt_pending);
  1989. spin_unlock_bh(&ctx->encrypt_compl_lock);
  1990. if (pending)
  1991. crypto_wait_req(-EINPROGRESS, &ctx->async_wait);
  1992. tls_tx_records(sk, -1);
  1993. /* Free up un-sent records in tx_list. First, free
  1994. * the partially sent record if any at head of tx_list.
  1995. */
  1996. if (tls_ctx->partially_sent_record) {
  1997. tls_free_partial_record(sk, tls_ctx);
  1998. rec = list_first_entry(&ctx->tx_list,
  1999. struct tls_rec, list);
  2000. list_del(&rec->list);
  2001. sk_msg_free(sk, &rec->msg_plaintext);
  2002. kfree(rec);
  2003. }
  2004. list_for_each_entry_safe(rec, tmp, &ctx->tx_list, list) {
  2005. list_del(&rec->list);
  2006. sk_msg_free(sk, &rec->msg_encrypted);
  2007. sk_msg_free(sk, &rec->msg_plaintext);
  2008. kfree(rec);
  2009. }
  2010. crypto_free_aead(ctx->aead_send);
  2011. tls_free_open_rec(sk);
  2012. }
  2013. void tls_sw_free_ctx_tx(struct tls_context *tls_ctx)
  2014. {
  2015. struct tls_sw_context_tx *ctx = tls_sw_ctx_tx(tls_ctx);
  2016. kfree(ctx);
  2017. }
  2018. void tls_sw_release_resources_rx(struct sock *sk)
  2019. {
  2020. struct tls_context *tls_ctx = tls_get_ctx(sk);
  2021. struct tls_sw_context_rx *ctx = tls_sw_ctx_rx(tls_ctx);
  2022. kfree(tls_ctx->rx.rec_seq);
  2023. kfree(tls_ctx->rx.iv);
  2024. if (ctx->aead_recv) {
  2025. __skb_queue_purge(&ctx->rx_list);
  2026. crypto_free_aead(ctx->aead_recv);
  2027. tls_strp_stop(&ctx->strp);
  2028. /* If tls_sw_strparser_arm() was not called (cleanup paths)
  2029. * we still want to tls_strp_stop(), but sk->sk_data_ready was
  2030. * never swapped.
  2031. */
  2032. if (ctx->saved_data_ready) {
  2033. write_lock_bh(&sk->sk_callback_lock);
  2034. sk->sk_data_ready = ctx->saved_data_ready;
  2035. write_unlock_bh(&sk->sk_callback_lock);
  2036. }
  2037. }
  2038. }
  2039. void tls_sw_strparser_done(struct tls_context *tls_ctx)
  2040. {
  2041. struct tls_sw_context_rx *ctx = tls_sw_ctx_rx(tls_ctx);
  2042. tls_strp_done(&ctx->strp);
  2043. }
  2044. void tls_sw_free_ctx_rx(struct tls_context *tls_ctx)
  2045. {
  2046. struct tls_sw_context_rx *ctx = tls_sw_ctx_rx(tls_ctx);
  2047. kfree(ctx);
  2048. }
  2049. void tls_sw_free_resources_rx(struct sock *sk)
  2050. {
  2051. struct tls_context *tls_ctx = tls_get_ctx(sk);
  2052. tls_sw_release_resources_rx(sk);
  2053. tls_sw_free_ctx_rx(tls_ctx);
  2054. }
  2055. /* The work handler to transmitt the encrypted records in tx_list */
  2056. static void tx_work_handler(struct work_struct *work)
  2057. {
  2058. struct delayed_work *delayed_work = to_delayed_work(work);
  2059. struct tx_work *tx_work = container_of(delayed_work,
  2060. struct tx_work, work);
  2061. struct sock *sk = tx_work->sk;
  2062. struct tls_context *tls_ctx = tls_get_ctx(sk);
  2063. struct tls_sw_context_tx *ctx;
  2064. if (unlikely(!tls_ctx))
  2065. return;
  2066. ctx = tls_sw_ctx_tx(tls_ctx);
  2067. if (test_bit(BIT_TX_CLOSING, &ctx->tx_bitmask))
  2068. return;
  2069. if (!test_and_clear_bit(BIT_TX_SCHEDULED, &ctx->tx_bitmask))
  2070. return;
  2071. if (mutex_trylock(&tls_ctx->tx_lock)) {
  2072. lock_sock(sk);
  2073. tls_tx_records(sk, -1);
  2074. release_sock(sk);
  2075. mutex_unlock(&tls_ctx->tx_lock);
  2076. } else if (!test_and_set_bit(BIT_TX_SCHEDULED, &ctx->tx_bitmask)) {
  2077. /* Someone is holding the tx_lock, they will likely run Tx
  2078. * and cancel the work on their way out of the lock section.
  2079. * Schedule a long delay just in case.
  2080. */
  2081. schedule_delayed_work(&ctx->tx_work.work, msecs_to_jiffies(10));
  2082. }
  2083. }
  2084. static bool tls_is_tx_ready(struct tls_sw_context_tx *ctx)
  2085. {
  2086. struct tls_rec *rec;
  2087. rec = list_first_entry_or_null(&ctx->tx_list, struct tls_rec, list);
  2088. if (!rec)
  2089. return false;
  2090. return READ_ONCE(rec->tx_ready);
  2091. }
  2092. void tls_sw_write_space(struct sock *sk, struct tls_context *ctx)
  2093. {
  2094. struct tls_sw_context_tx *tx_ctx = tls_sw_ctx_tx(ctx);
  2095. /* Schedule the transmission if tx list is ready */
  2096. if (tls_is_tx_ready(tx_ctx) &&
  2097. !test_and_set_bit(BIT_TX_SCHEDULED, &tx_ctx->tx_bitmask))
  2098. schedule_delayed_work(&tx_ctx->tx_work.work, 0);
  2099. }
  2100. void tls_sw_strparser_arm(struct sock *sk, struct tls_context *tls_ctx)
  2101. {
  2102. struct tls_sw_context_rx *rx_ctx = tls_sw_ctx_rx(tls_ctx);
  2103. write_lock_bh(&sk->sk_callback_lock);
  2104. rx_ctx->saved_data_ready = sk->sk_data_ready;
  2105. sk->sk_data_ready = tls_data_ready;
  2106. write_unlock_bh(&sk->sk_callback_lock);
  2107. }
  2108. void tls_update_rx_zc_capable(struct tls_context *tls_ctx)
  2109. {
  2110. struct tls_sw_context_rx *rx_ctx = tls_sw_ctx_rx(tls_ctx);
  2111. rx_ctx->zc_capable = tls_ctx->rx_no_pad ||
  2112. tls_ctx->prot_info.version != TLS_1_3_VERSION;
  2113. }
  2114. int tls_set_sw_offload(struct sock *sk, struct tls_context *ctx, int tx)
  2115. {
  2116. struct tls_context *tls_ctx = tls_get_ctx(sk);
  2117. struct tls_prot_info *prot = &tls_ctx->prot_info;
  2118. struct tls_crypto_info *crypto_info;
  2119. struct tls_sw_context_tx *sw_ctx_tx = NULL;
  2120. struct tls_sw_context_rx *sw_ctx_rx = NULL;
  2121. struct cipher_context *cctx;
  2122. struct crypto_aead **aead;
  2123. u16 nonce_size, tag_size, iv_size, rec_seq_size, salt_size;
  2124. struct crypto_tfm *tfm;
  2125. char *iv, *rec_seq, *key, *salt, *cipher_name;
  2126. size_t keysize;
  2127. int rc = 0;
  2128. if (!ctx) {
  2129. rc = -EINVAL;
  2130. goto out;
  2131. }
  2132. if (tx) {
  2133. if (!ctx->priv_ctx_tx) {
  2134. sw_ctx_tx = kzalloc(sizeof(*sw_ctx_tx), GFP_KERNEL);
  2135. if (!sw_ctx_tx) {
  2136. rc = -ENOMEM;
  2137. goto out;
  2138. }
  2139. ctx->priv_ctx_tx = sw_ctx_tx;
  2140. } else {
  2141. sw_ctx_tx =
  2142. (struct tls_sw_context_tx *)ctx->priv_ctx_tx;
  2143. }
  2144. } else {
  2145. if (!ctx->priv_ctx_rx) {
  2146. sw_ctx_rx = kzalloc(sizeof(*sw_ctx_rx), GFP_KERNEL);
  2147. if (!sw_ctx_rx) {
  2148. rc = -ENOMEM;
  2149. goto out;
  2150. }
  2151. ctx->priv_ctx_rx = sw_ctx_rx;
  2152. } else {
  2153. sw_ctx_rx =
  2154. (struct tls_sw_context_rx *)ctx->priv_ctx_rx;
  2155. }
  2156. }
  2157. if (tx) {
  2158. crypto_init_wait(&sw_ctx_tx->async_wait);
  2159. spin_lock_init(&sw_ctx_tx->encrypt_compl_lock);
  2160. crypto_info = &ctx->crypto_send.info;
  2161. cctx = &ctx->tx;
  2162. aead = &sw_ctx_tx->aead_send;
  2163. INIT_LIST_HEAD(&sw_ctx_tx->tx_list);
  2164. INIT_DELAYED_WORK(&sw_ctx_tx->tx_work.work, tx_work_handler);
  2165. sw_ctx_tx->tx_work.sk = sk;
  2166. } else {
  2167. crypto_init_wait(&sw_ctx_rx->async_wait);
  2168. spin_lock_init(&sw_ctx_rx->decrypt_compl_lock);
  2169. init_waitqueue_head(&sw_ctx_rx->wq);
  2170. crypto_info = &ctx->crypto_recv.info;
  2171. cctx = &ctx->rx;
  2172. skb_queue_head_init(&sw_ctx_rx->rx_list);
  2173. skb_queue_head_init(&sw_ctx_rx->async_hold);
  2174. aead = &sw_ctx_rx->aead_recv;
  2175. }
  2176. switch (crypto_info->cipher_type) {
  2177. case TLS_CIPHER_AES_GCM_128: {
  2178. struct tls12_crypto_info_aes_gcm_128 *gcm_128_info;
  2179. gcm_128_info = (void *)crypto_info;
  2180. nonce_size = TLS_CIPHER_AES_GCM_128_IV_SIZE;
  2181. tag_size = TLS_CIPHER_AES_GCM_128_TAG_SIZE;
  2182. iv_size = TLS_CIPHER_AES_GCM_128_IV_SIZE;
  2183. iv = gcm_128_info->iv;
  2184. rec_seq_size = TLS_CIPHER_AES_GCM_128_REC_SEQ_SIZE;
  2185. rec_seq = gcm_128_info->rec_seq;
  2186. keysize = TLS_CIPHER_AES_GCM_128_KEY_SIZE;
  2187. key = gcm_128_info->key;
  2188. salt = gcm_128_info->salt;
  2189. salt_size = TLS_CIPHER_AES_GCM_128_SALT_SIZE;
  2190. cipher_name = "gcm(aes)";
  2191. break;
  2192. }
  2193. case TLS_CIPHER_AES_GCM_256: {
  2194. struct tls12_crypto_info_aes_gcm_256 *gcm_256_info;
  2195. gcm_256_info = (void *)crypto_info;
  2196. nonce_size = TLS_CIPHER_AES_GCM_256_IV_SIZE;
  2197. tag_size = TLS_CIPHER_AES_GCM_256_TAG_SIZE;
  2198. iv_size = TLS_CIPHER_AES_GCM_256_IV_SIZE;
  2199. iv = gcm_256_info->iv;
  2200. rec_seq_size = TLS_CIPHER_AES_GCM_256_REC_SEQ_SIZE;
  2201. rec_seq = gcm_256_info->rec_seq;
  2202. keysize = TLS_CIPHER_AES_GCM_256_KEY_SIZE;
  2203. key = gcm_256_info->key;
  2204. salt = gcm_256_info->salt;
  2205. salt_size = TLS_CIPHER_AES_GCM_256_SALT_SIZE;
  2206. cipher_name = "gcm(aes)";
  2207. break;
  2208. }
  2209. case TLS_CIPHER_AES_CCM_128: {
  2210. struct tls12_crypto_info_aes_ccm_128 *ccm_128_info;
  2211. ccm_128_info = (void *)crypto_info;
  2212. nonce_size = TLS_CIPHER_AES_CCM_128_IV_SIZE;
  2213. tag_size = TLS_CIPHER_AES_CCM_128_TAG_SIZE;
  2214. iv_size = TLS_CIPHER_AES_CCM_128_IV_SIZE;
  2215. iv = ccm_128_info->iv;
  2216. rec_seq_size = TLS_CIPHER_AES_CCM_128_REC_SEQ_SIZE;
  2217. rec_seq = ccm_128_info->rec_seq;
  2218. keysize = TLS_CIPHER_AES_CCM_128_KEY_SIZE;
  2219. key = ccm_128_info->key;
  2220. salt = ccm_128_info->salt;
  2221. salt_size = TLS_CIPHER_AES_CCM_128_SALT_SIZE;
  2222. cipher_name = "ccm(aes)";
  2223. break;
  2224. }
  2225. case TLS_CIPHER_CHACHA20_POLY1305: {
  2226. struct tls12_crypto_info_chacha20_poly1305 *chacha20_poly1305_info;
  2227. chacha20_poly1305_info = (void *)crypto_info;
  2228. nonce_size = 0;
  2229. tag_size = TLS_CIPHER_CHACHA20_POLY1305_TAG_SIZE;
  2230. iv_size = TLS_CIPHER_CHACHA20_POLY1305_IV_SIZE;
  2231. iv = chacha20_poly1305_info->iv;
  2232. rec_seq_size = TLS_CIPHER_CHACHA20_POLY1305_REC_SEQ_SIZE;
  2233. rec_seq = chacha20_poly1305_info->rec_seq;
  2234. keysize = TLS_CIPHER_CHACHA20_POLY1305_KEY_SIZE;
  2235. key = chacha20_poly1305_info->key;
  2236. salt = chacha20_poly1305_info->salt;
  2237. salt_size = TLS_CIPHER_CHACHA20_POLY1305_SALT_SIZE;
  2238. cipher_name = "rfc7539(chacha20,poly1305)";
  2239. break;
  2240. }
  2241. case TLS_CIPHER_SM4_GCM: {
  2242. struct tls12_crypto_info_sm4_gcm *sm4_gcm_info;
  2243. sm4_gcm_info = (void *)crypto_info;
  2244. nonce_size = TLS_CIPHER_SM4_GCM_IV_SIZE;
  2245. tag_size = TLS_CIPHER_SM4_GCM_TAG_SIZE;
  2246. iv_size = TLS_CIPHER_SM4_GCM_IV_SIZE;
  2247. iv = sm4_gcm_info->iv;
  2248. rec_seq_size = TLS_CIPHER_SM4_GCM_REC_SEQ_SIZE;
  2249. rec_seq = sm4_gcm_info->rec_seq;
  2250. keysize = TLS_CIPHER_SM4_GCM_KEY_SIZE;
  2251. key = sm4_gcm_info->key;
  2252. salt = sm4_gcm_info->salt;
  2253. salt_size = TLS_CIPHER_SM4_GCM_SALT_SIZE;
  2254. cipher_name = "gcm(sm4)";
  2255. break;
  2256. }
  2257. case TLS_CIPHER_SM4_CCM: {
  2258. struct tls12_crypto_info_sm4_ccm *sm4_ccm_info;
  2259. sm4_ccm_info = (void *)crypto_info;
  2260. nonce_size = TLS_CIPHER_SM4_CCM_IV_SIZE;
  2261. tag_size = TLS_CIPHER_SM4_CCM_TAG_SIZE;
  2262. iv_size = TLS_CIPHER_SM4_CCM_IV_SIZE;
  2263. iv = sm4_ccm_info->iv;
  2264. rec_seq_size = TLS_CIPHER_SM4_CCM_REC_SEQ_SIZE;
  2265. rec_seq = sm4_ccm_info->rec_seq;
  2266. keysize = TLS_CIPHER_SM4_CCM_KEY_SIZE;
  2267. key = sm4_ccm_info->key;
  2268. salt = sm4_ccm_info->salt;
  2269. salt_size = TLS_CIPHER_SM4_CCM_SALT_SIZE;
  2270. cipher_name = "ccm(sm4)";
  2271. break;
  2272. }
  2273. case TLS_CIPHER_ARIA_GCM_128: {
  2274. struct tls12_crypto_info_aria_gcm_128 *aria_gcm_128_info;
  2275. aria_gcm_128_info = (void *)crypto_info;
  2276. nonce_size = TLS_CIPHER_ARIA_GCM_128_IV_SIZE;
  2277. tag_size = TLS_CIPHER_ARIA_GCM_128_TAG_SIZE;
  2278. iv_size = TLS_CIPHER_ARIA_GCM_128_IV_SIZE;
  2279. iv = aria_gcm_128_info->iv;
  2280. rec_seq_size = TLS_CIPHER_ARIA_GCM_128_REC_SEQ_SIZE;
  2281. rec_seq = aria_gcm_128_info->rec_seq;
  2282. keysize = TLS_CIPHER_ARIA_GCM_128_KEY_SIZE;
  2283. key = aria_gcm_128_info->key;
  2284. salt = aria_gcm_128_info->salt;
  2285. salt_size = TLS_CIPHER_ARIA_GCM_128_SALT_SIZE;
  2286. cipher_name = "gcm(aria)";
  2287. break;
  2288. }
  2289. case TLS_CIPHER_ARIA_GCM_256: {
  2290. struct tls12_crypto_info_aria_gcm_256 *gcm_256_info;
  2291. gcm_256_info = (void *)crypto_info;
  2292. nonce_size = TLS_CIPHER_ARIA_GCM_256_IV_SIZE;
  2293. tag_size = TLS_CIPHER_ARIA_GCM_256_TAG_SIZE;
  2294. iv_size = TLS_CIPHER_ARIA_GCM_256_IV_SIZE;
  2295. iv = gcm_256_info->iv;
  2296. rec_seq_size = TLS_CIPHER_ARIA_GCM_256_REC_SEQ_SIZE;
  2297. rec_seq = gcm_256_info->rec_seq;
  2298. keysize = TLS_CIPHER_ARIA_GCM_256_KEY_SIZE;
  2299. key = gcm_256_info->key;
  2300. salt = gcm_256_info->salt;
  2301. salt_size = TLS_CIPHER_ARIA_GCM_256_SALT_SIZE;
  2302. cipher_name = "gcm(aria)";
  2303. break;
  2304. }
  2305. default:
  2306. rc = -EINVAL;
  2307. goto free_priv;
  2308. }
  2309. if (crypto_info->version == TLS_1_3_VERSION) {
  2310. nonce_size = 0;
  2311. prot->aad_size = TLS_HEADER_SIZE;
  2312. prot->tail_size = 1;
  2313. } else {
  2314. prot->aad_size = TLS_AAD_SPACE_SIZE;
  2315. prot->tail_size = 0;
  2316. }
  2317. /* Sanity-check the sizes for stack allocations. */
  2318. if (iv_size > MAX_IV_SIZE || nonce_size > MAX_IV_SIZE ||
  2319. rec_seq_size > TLS_MAX_REC_SEQ_SIZE || tag_size != TLS_TAG_SIZE ||
  2320. prot->aad_size > TLS_MAX_AAD_SIZE) {
  2321. rc = -EINVAL;
  2322. goto free_priv;
  2323. }
  2324. prot->version = crypto_info->version;
  2325. prot->cipher_type = crypto_info->cipher_type;
  2326. prot->prepend_size = TLS_HEADER_SIZE + nonce_size;
  2327. prot->tag_size = tag_size;
  2328. prot->overhead_size = prot->prepend_size +
  2329. prot->tag_size + prot->tail_size;
  2330. prot->iv_size = iv_size;
  2331. prot->salt_size = salt_size;
  2332. cctx->iv = kmalloc(iv_size + salt_size, GFP_KERNEL);
  2333. if (!cctx->iv) {
  2334. rc = -ENOMEM;
  2335. goto free_priv;
  2336. }
  2337. /* Note: 128 & 256 bit salt are the same size */
  2338. prot->rec_seq_size = rec_seq_size;
  2339. memcpy(cctx->iv, salt, salt_size);
  2340. memcpy(cctx->iv + salt_size, iv, iv_size);
  2341. cctx->rec_seq = kmemdup(rec_seq, rec_seq_size, GFP_KERNEL);
  2342. if (!cctx->rec_seq) {
  2343. rc = -ENOMEM;
  2344. goto free_iv;
  2345. }
  2346. if (!*aead) {
  2347. *aead = crypto_alloc_aead(cipher_name, 0, 0);
  2348. if (IS_ERR(*aead)) {
  2349. rc = PTR_ERR(*aead);
  2350. *aead = NULL;
  2351. goto free_rec_seq;
  2352. }
  2353. }
  2354. ctx->push_pending_record = tls_sw_push_pending_record;
  2355. rc = crypto_aead_setkey(*aead, key, keysize);
  2356. if (rc)
  2357. goto free_aead;
  2358. rc = crypto_aead_setauthsize(*aead, prot->tag_size);
  2359. if (rc)
  2360. goto free_aead;
  2361. if (sw_ctx_rx) {
  2362. tfm = crypto_aead_tfm(sw_ctx_rx->aead_recv);
  2363. tls_update_rx_zc_capable(ctx);
  2364. sw_ctx_rx->async_capable =
  2365. crypto_info->version != TLS_1_3_VERSION &&
  2366. !!(tfm->__crt_alg->cra_flags & CRYPTO_ALG_ASYNC);
  2367. rc = tls_strp_init(&sw_ctx_rx->strp, sk);
  2368. if (rc)
  2369. goto free_aead;
  2370. }
  2371. goto out;
  2372. free_aead:
  2373. crypto_free_aead(*aead);
  2374. *aead = NULL;
  2375. free_rec_seq:
  2376. kfree(cctx->rec_seq);
  2377. cctx->rec_seq = NULL;
  2378. free_iv:
  2379. kfree(cctx->iv);
  2380. cctx->iv = NULL;
  2381. free_priv:
  2382. if (tx) {
  2383. kfree(ctx->priv_ctx_tx);
  2384. ctx->priv_ctx_tx = NULL;
  2385. } else {
  2386. kfree(ctx->priv_ctx_rx);
  2387. ctx->priv_ctx_rx = NULL;
  2388. }
  2389. out:
  2390. return rc;
  2391. }