tls_sw.c 69 KB

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