tls_sw.c 70 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838
  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. if ((s32)delta > 0) {
  747. /* It indicates that we executed bpf_msg_pop_data(),
  748. * causing the plaintext data size to decrease.
  749. * Therefore the encrypted data size also needs to
  750. * correspondingly decrease. We only need to subtract
  751. * delta to calculate the new ciphertext length since
  752. * ktls does not support block encryption.
  753. */
  754. struct sk_msg *enc = &ctx->open_rec->msg_encrypted;
  755. sk_msg_trim(sk, enc, enc->sg.size - delta);
  756. }
  757. }
  758. if (msg->cork_bytes && msg->cork_bytes > msg->sg.size &&
  759. !enospc && !full_record) {
  760. err = -ENOSPC;
  761. goto out_err;
  762. }
  763. msg->cork_bytes = 0;
  764. send = msg->sg.size;
  765. if (msg->apply_bytes && msg->apply_bytes < send)
  766. send = msg->apply_bytes;
  767. switch (psock->eval) {
  768. case __SK_PASS:
  769. err = tls_push_record(sk, flags, record_type);
  770. if (err && err != -EINPROGRESS && sk->sk_err == EBADMSG) {
  771. *copied -= sk_msg_free(sk, msg);
  772. tls_free_open_rec(sk);
  773. err = -sk->sk_err;
  774. goto out_err;
  775. }
  776. break;
  777. case __SK_REDIRECT:
  778. redir_ingress = psock->redir_ingress;
  779. sk_redir = psock->sk_redir;
  780. memcpy(&msg_redir, msg, sizeof(*msg));
  781. if (msg->apply_bytes < send)
  782. msg->apply_bytes = 0;
  783. else
  784. msg->apply_bytes -= send;
  785. sk_msg_return_zero(sk, msg, send);
  786. msg->sg.size -= send;
  787. release_sock(sk);
  788. err = tcp_bpf_sendmsg_redir(sk_redir, redir_ingress,
  789. &msg_redir, send, flags);
  790. lock_sock(sk);
  791. if (err < 0) {
  792. /* Regardless of whether the data represented by
  793. * msg_redir is sent successfully, we have already
  794. * uncharged it via sk_msg_return_zero(). The
  795. * msg->sg.size represents the remaining unprocessed
  796. * data, which needs to be uncharged here.
  797. */
  798. sk_mem_uncharge(sk, msg->sg.size);
  799. *copied -= sk_msg_free_nocharge(sk, &msg_redir);
  800. msg->sg.size = 0;
  801. }
  802. if (msg->sg.size == 0)
  803. tls_free_open_rec(sk);
  804. break;
  805. case __SK_DROP:
  806. default:
  807. sk_msg_free_partial(sk, msg, send);
  808. if (msg->apply_bytes < send)
  809. msg->apply_bytes = 0;
  810. else
  811. msg->apply_bytes -= send;
  812. if (msg->sg.size == 0)
  813. tls_free_open_rec(sk);
  814. *copied -= (send + delta);
  815. err = -EACCES;
  816. }
  817. if (likely(!err)) {
  818. bool reset_eval = !ctx->open_rec;
  819. rec = ctx->open_rec;
  820. if (rec) {
  821. msg = &rec->msg_plaintext;
  822. if (!msg->apply_bytes)
  823. reset_eval = true;
  824. }
  825. if (reset_eval) {
  826. psock->eval = __SK_NONE;
  827. if (psock->sk_redir) {
  828. sock_put(psock->sk_redir);
  829. psock->sk_redir = NULL;
  830. }
  831. }
  832. if (rec)
  833. goto more_data;
  834. }
  835. out_err:
  836. sk_psock_put(sk, psock);
  837. return err;
  838. }
  839. static int tls_sw_push_pending_record(struct sock *sk, int flags)
  840. {
  841. struct tls_context *tls_ctx = tls_get_ctx(sk);
  842. struct tls_sw_context_tx *ctx = tls_sw_ctx_tx(tls_ctx);
  843. struct tls_rec *rec = ctx->open_rec;
  844. struct sk_msg *msg_pl;
  845. size_t copied;
  846. if (!rec)
  847. return 0;
  848. msg_pl = &rec->msg_plaintext;
  849. copied = msg_pl->sg.size;
  850. if (!copied)
  851. return 0;
  852. return bpf_exec_tx_verdict(msg_pl, sk, true, TLS_RECORD_TYPE_DATA,
  853. &copied, flags);
  854. }
  855. static int tls_sw_sendmsg_splice(struct sock *sk, struct msghdr *msg,
  856. struct sk_msg *msg_pl, size_t try_to_copy,
  857. ssize_t *copied)
  858. {
  859. struct page *page = NULL, **pages = &page;
  860. do {
  861. ssize_t part;
  862. size_t off;
  863. part = iov_iter_extract_pages(&msg->msg_iter, &pages,
  864. try_to_copy, 1, 0, &off);
  865. if (part <= 0)
  866. return part ?: -EIO;
  867. if (WARN_ON_ONCE(!sendpage_ok(page))) {
  868. iov_iter_revert(&msg->msg_iter, part);
  869. return -EIO;
  870. }
  871. sk_msg_page_add(msg_pl, page, part, off);
  872. msg_pl->sg.copybreak = 0;
  873. msg_pl->sg.curr = msg_pl->sg.end;
  874. sk_mem_charge(sk, part);
  875. *copied += part;
  876. try_to_copy -= part;
  877. } while (try_to_copy && !sk_msg_full(msg_pl));
  878. return 0;
  879. }
  880. static int tls_sw_sendmsg_locked(struct sock *sk, struct msghdr *msg,
  881. size_t size)
  882. {
  883. long timeo = sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT);
  884. struct tls_context *tls_ctx = tls_get_ctx(sk);
  885. struct tls_prot_info *prot = &tls_ctx->prot_info;
  886. struct tls_sw_context_tx *ctx = tls_sw_ctx_tx(tls_ctx);
  887. bool async_capable = ctx->async_capable;
  888. unsigned char record_type = TLS_RECORD_TYPE_DATA;
  889. bool is_kvec = iov_iter_is_kvec(&msg->msg_iter);
  890. bool eor = !(msg->msg_flags & MSG_MORE);
  891. size_t try_to_copy;
  892. ssize_t copied = 0;
  893. struct sk_msg *msg_pl, *msg_en;
  894. struct tls_rec *rec;
  895. int required_size;
  896. int num_async = 0;
  897. bool full_record;
  898. int record_room;
  899. int num_zc = 0;
  900. int orig_size;
  901. int ret = 0;
  902. if (!eor && (msg->msg_flags & MSG_EOR))
  903. return -EINVAL;
  904. if (unlikely(msg->msg_controllen)) {
  905. ret = tls_process_cmsg(sk, msg, &record_type);
  906. if (ret) {
  907. if (ret == -EINPROGRESS)
  908. num_async++;
  909. else if (ret != -EAGAIN)
  910. goto end;
  911. }
  912. }
  913. while (msg_data_left(msg)) {
  914. if (sk->sk_err) {
  915. ret = -sk->sk_err;
  916. goto send_end;
  917. }
  918. if (ctx->open_rec)
  919. rec = ctx->open_rec;
  920. else
  921. rec = ctx->open_rec = tls_get_rec(sk);
  922. if (!rec) {
  923. ret = -ENOMEM;
  924. goto send_end;
  925. }
  926. msg_pl = &rec->msg_plaintext;
  927. msg_en = &rec->msg_encrypted;
  928. orig_size = msg_pl->sg.size;
  929. full_record = false;
  930. try_to_copy = msg_data_left(msg);
  931. record_room = TLS_MAX_PAYLOAD_SIZE - msg_pl->sg.size;
  932. if (try_to_copy >= record_room) {
  933. try_to_copy = record_room;
  934. full_record = true;
  935. }
  936. required_size = msg_pl->sg.size + try_to_copy +
  937. prot->overhead_size;
  938. if (!sk_stream_memory_free(sk))
  939. goto wait_for_sndbuf;
  940. alloc_encrypted:
  941. ret = tls_alloc_encrypted_msg(sk, required_size);
  942. if (ret) {
  943. if (ret != -ENOSPC)
  944. goto wait_for_memory;
  945. /* Adjust try_to_copy according to the amount that was
  946. * actually allocated. The difference is due
  947. * to max sg elements limit
  948. */
  949. try_to_copy -= required_size - msg_en->sg.size;
  950. full_record = true;
  951. }
  952. if (try_to_copy && (msg->msg_flags & MSG_SPLICE_PAGES)) {
  953. ret = tls_sw_sendmsg_splice(sk, msg, msg_pl,
  954. try_to_copy, &copied);
  955. if (ret < 0)
  956. goto send_end;
  957. tls_ctx->pending_open_record_frags = true;
  958. if (sk_msg_full(msg_pl)) {
  959. full_record = true;
  960. sk_msg_trim(sk, msg_en,
  961. msg_pl->sg.size + prot->overhead_size);
  962. }
  963. if (full_record || eor)
  964. goto copied;
  965. continue;
  966. }
  967. if (!is_kvec && (full_record || eor) && !async_capable) {
  968. u32 first = msg_pl->sg.end;
  969. ret = sk_msg_zerocopy_from_iter(sk, &msg->msg_iter,
  970. msg_pl, try_to_copy);
  971. if (ret)
  972. goto fallback_to_reg_send;
  973. num_zc++;
  974. copied += try_to_copy;
  975. sk_msg_sg_copy_set(msg_pl, first);
  976. ret = bpf_exec_tx_verdict(msg_pl, sk, full_record,
  977. record_type, &copied,
  978. msg->msg_flags);
  979. if (ret) {
  980. if (ret == -EINPROGRESS)
  981. num_async++;
  982. else if (ret == -ENOMEM)
  983. goto wait_for_memory;
  984. else if (ctx->open_rec && ret == -ENOSPC) {
  985. if (msg_pl->cork_bytes) {
  986. ret = 0;
  987. goto send_end;
  988. }
  989. goto rollback_iter;
  990. } else if (ret != -EAGAIN)
  991. goto send_end;
  992. }
  993. /* Transmit if any encryptions have completed */
  994. if (test_and_clear_bit(BIT_TX_SCHEDULED, &ctx->tx_bitmask)) {
  995. cancel_delayed_work(&ctx->tx_work.work);
  996. tls_tx_records(sk, msg->msg_flags);
  997. }
  998. continue;
  999. rollback_iter:
  1000. copied -= try_to_copy;
  1001. sk_msg_sg_copy_clear(msg_pl, first);
  1002. iov_iter_revert(&msg->msg_iter,
  1003. msg_pl->sg.size - orig_size);
  1004. fallback_to_reg_send:
  1005. sk_msg_trim(sk, msg_pl, orig_size);
  1006. }
  1007. required_size = msg_pl->sg.size + try_to_copy;
  1008. ret = tls_clone_plaintext_msg(sk, required_size);
  1009. if (ret) {
  1010. if (ret != -ENOSPC)
  1011. goto send_end;
  1012. /* Adjust try_to_copy according to the amount that was
  1013. * actually allocated. The difference is due
  1014. * to max sg elements limit
  1015. */
  1016. try_to_copy -= required_size - msg_pl->sg.size;
  1017. full_record = true;
  1018. sk_msg_trim(sk, msg_en,
  1019. msg_pl->sg.size + prot->overhead_size);
  1020. }
  1021. if (try_to_copy) {
  1022. ret = sk_msg_memcopy_from_iter(sk, &msg->msg_iter,
  1023. msg_pl, try_to_copy);
  1024. if (ret < 0)
  1025. goto trim_sgl;
  1026. }
  1027. /* Open records defined only if successfully copied, otherwise
  1028. * we would trim the sg but not reset the open record frags.
  1029. */
  1030. tls_ctx->pending_open_record_frags = true;
  1031. copied += try_to_copy;
  1032. copied:
  1033. if (full_record || eor) {
  1034. ret = bpf_exec_tx_verdict(msg_pl, sk, full_record,
  1035. record_type, &copied,
  1036. msg->msg_flags);
  1037. if (ret) {
  1038. if (ret == -EINPROGRESS)
  1039. num_async++;
  1040. else if (ret == -ENOMEM)
  1041. goto wait_for_memory;
  1042. else if (ret != -EAGAIN) {
  1043. if (ret == -ENOSPC)
  1044. ret = 0;
  1045. goto send_end;
  1046. }
  1047. }
  1048. /* Transmit if any encryptions have completed */
  1049. if (test_and_clear_bit(BIT_TX_SCHEDULED, &ctx->tx_bitmask)) {
  1050. cancel_delayed_work(&ctx->tx_work.work);
  1051. tls_tx_records(sk, msg->msg_flags);
  1052. }
  1053. }
  1054. continue;
  1055. wait_for_sndbuf:
  1056. set_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
  1057. wait_for_memory:
  1058. ret = sk_stream_wait_memory(sk, &timeo);
  1059. if (ret) {
  1060. trim_sgl:
  1061. if (ctx->open_rec)
  1062. tls_trim_both_msgs(sk, orig_size);
  1063. goto send_end;
  1064. }
  1065. if (ctx->open_rec && msg_en->sg.size < required_size)
  1066. goto alloc_encrypted;
  1067. }
  1068. send_end:
  1069. if (!num_async) {
  1070. goto end;
  1071. } else if (num_zc || eor) {
  1072. int err;
  1073. /* Wait for pending encryptions to get completed */
  1074. err = tls_encrypt_async_wait(ctx);
  1075. if (err) {
  1076. ret = err;
  1077. copied = 0;
  1078. }
  1079. }
  1080. /* Transmit if any encryptions have completed */
  1081. if (test_and_clear_bit(BIT_TX_SCHEDULED, &ctx->tx_bitmask)) {
  1082. cancel_delayed_work(&ctx->tx_work.work);
  1083. tls_tx_records(sk, msg->msg_flags);
  1084. }
  1085. end:
  1086. ret = sk_stream_error(sk, msg->msg_flags, ret);
  1087. return copied > 0 ? copied : ret;
  1088. }
  1089. int tls_sw_sendmsg(struct sock *sk, struct msghdr *msg, size_t size)
  1090. {
  1091. struct tls_context *tls_ctx = tls_get_ctx(sk);
  1092. int ret;
  1093. if (msg->msg_flags & ~(MSG_MORE | MSG_DONTWAIT | MSG_NOSIGNAL |
  1094. MSG_CMSG_COMPAT | MSG_SPLICE_PAGES | MSG_EOR |
  1095. MSG_SENDPAGE_NOPOLICY))
  1096. return -EOPNOTSUPP;
  1097. ret = mutex_lock_interruptible(&tls_ctx->tx_lock);
  1098. if (ret)
  1099. return ret;
  1100. lock_sock(sk);
  1101. ret = tls_sw_sendmsg_locked(sk, msg, size);
  1102. release_sock(sk);
  1103. mutex_unlock(&tls_ctx->tx_lock);
  1104. return ret;
  1105. }
  1106. /*
  1107. * Handle unexpected EOF during splice without SPLICE_F_MORE set.
  1108. */
  1109. void tls_sw_splice_eof(struct socket *sock)
  1110. {
  1111. struct sock *sk = sock->sk;
  1112. struct tls_context *tls_ctx = tls_get_ctx(sk);
  1113. struct tls_sw_context_tx *ctx = tls_sw_ctx_tx(tls_ctx);
  1114. struct tls_rec *rec;
  1115. struct sk_msg *msg_pl;
  1116. ssize_t copied = 0;
  1117. bool retrying = false;
  1118. int ret = 0;
  1119. if (!ctx->open_rec)
  1120. return;
  1121. mutex_lock(&tls_ctx->tx_lock);
  1122. lock_sock(sk);
  1123. retry:
  1124. /* same checks as in tls_sw_push_pending_record() */
  1125. rec = ctx->open_rec;
  1126. if (!rec)
  1127. goto unlock;
  1128. msg_pl = &rec->msg_plaintext;
  1129. if (msg_pl->sg.size == 0)
  1130. goto unlock;
  1131. /* Check the BPF advisor and perform transmission. */
  1132. ret = bpf_exec_tx_verdict(msg_pl, sk, false, TLS_RECORD_TYPE_DATA,
  1133. &copied, 0);
  1134. switch (ret) {
  1135. case 0:
  1136. case -EAGAIN:
  1137. if (retrying)
  1138. goto unlock;
  1139. retrying = true;
  1140. goto retry;
  1141. case -EINPROGRESS:
  1142. break;
  1143. default:
  1144. goto unlock;
  1145. }
  1146. /* Wait for pending encryptions to get completed */
  1147. if (tls_encrypt_async_wait(ctx))
  1148. goto unlock;
  1149. /* Transmit if any encryptions have completed */
  1150. if (test_and_clear_bit(BIT_TX_SCHEDULED, &ctx->tx_bitmask)) {
  1151. cancel_delayed_work(&ctx->tx_work.work);
  1152. tls_tx_records(sk, 0);
  1153. }
  1154. unlock:
  1155. release_sock(sk);
  1156. mutex_unlock(&tls_ctx->tx_lock);
  1157. }
  1158. static int
  1159. tls_rx_rec_wait(struct sock *sk, struct sk_psock *psock, bool nonblock,
  1160. bool released)
  1161. {
  1162. struct tls_context *tls_ctx = tls_get_ctx(sk);
  1163. struct tls_sw_context_rx *ctx = tls_sw_ctx_rx(tls_ctx);
  1164. DEFINE_WAIT_FUNC(wait, woken_wake_function);
  1165. int ret = 0;
  1166. long timeo;
  1167. timeo = sock_rcvtimeo(sk, nonblock);
  1168. while (!tls_strp_msg_ready(ctx)) {
  1169. if (!sk_psock_queue_empty(psock))
  1170. return 0;
  1171. if (sk->sk_err)
  1172. return sock_error(sk);
  1173. if (ret < 0)
  1174. return ret;
  1175. if (!skb_queue_empty(&sk->sk_receive_queue)) {
  1176. tls_strp_check_rcv(&ctx->strp);
  1177. if (tls_strp_msg_ready(ctx))
  1178. break;
  1179. }
  1180. if (sk->sk_shutdown & RCV_SHUTDOWN)
  1181. return 0;
  1182. if (sock_flag(sk, SOCK_DONE))
  1183. return 0;
  1184. if (!timeo)
  1185. return -EAGAIN;
  1186. released = true;
  1187. add_wait_queue(sk_sleep(sk), &wait);
  1188. sk_set_bit(SOCKWQ_ASYNC_WAITDATA, sk);
  1189. ret = sk_wait_event(sk, &timeo,
  1190. tls_strp_msg_ready(ctx) ||
  1191. !sk_psock_queue_empty(psock),
  1192. &wait);
  1193. sk_clear_bit(SOCKWQ_ASYNC_WAITDATA, sk);
  1194. remove_wait_queue(sk_sleep(sk), &wait);
  1195. /* Handle signals */
  1196. if (signal_pending(current))
  1197. return sock_intr_errno(timeo);
  1198. }
  1199. if (unlikely(!tls_strp_msg_load(&ctx->strp, released)))
  1200. return tls_rx_rec_wait(sk, psock, nonblock, false);
  1201. return 1;
  1202. }
  1203. static int tls_setup_from_iter(struct iov_iter *from,
  1204. int length, int *pages_used,
  1205. struct scatterlist *to,
  1206. int to_max_pages)
  1207. {
  1208. int rc = 0, i = 0, num_elem = *pages_used, maxpages;
  1209. struct page *pages[MAX_SKB_FRAGS];
  1210. unsigned int size = 0;
  1211. ssize_t copied, use;
  1212. size_t offset;
  1213. while (length > 0) {
  1214. i = 0;
  1215. maxpages = to_max_pages - num_elem;
  1216. if (maxpages == 0) {
  1217. rc = -EFAULT;
  1218. goto out;
  1219. }
  1220. copied = iov_iter_get_pages2(from, pages,
  1221. length,
  1222. maxpages, &offset);
  1223. if (copied <= 0) {
  1224. rc = -EFAULT;
  1225. goto out;
  1226. }
  1227. length -= copied;
  1228. size += copied;
  1229. while (copied) {
  1230. use = min_t(int, copied, PAGE_SIZE - offset);
  1231. sg_set_page(&to[num_elem],
  1232. pages[i], use, offset);
  1233. sg_unmark_end(&to[num_elem]);
  1234. /* We do not uncharge memory from this API */
  1235. offset = 0;
  1236. copied -= use;
  1237. i++;
  1238. num_elem++;
  1239. }
  1240. }
  1241. /* Mark the end in the last sg entry if newly added */
  1242. if (num_elem > *pages_used)
  1243. sg_mark_end(&to[num_elem - 1]);
  1244. out:
  1245. if (rc)
  1246. iov_iter_revert(from, size);
  1247. *pages_used = num_elem;
  1248. return rc;
  1249. }
  1250. static struct sk_buff *
  1251. tls_alloc_clrtxt_skb(struct sock *sk, struct sk_buff *skb,
  1252. unsigned int full_len)
  1253. {
  1254. struct strp_msg *clr_rxm;
  1255. struct sk_buff *clr_skb;
  1256. int err;
  1257. clr_skb = alloc_skb_with_frags(0, full_len, TLS_PAGE_ORDER,
  1258. &err, sk->sk_allocation);
  1259. if (!clr_skb)
  1260. return NULL;
  1261. skb_copy_header(clr_skb, skb);
  1262. clr_skb->len = full_len;
  1263. clr_skb->data_len = full_len;
  1264. clr_rxm = strp_msg(clr_skb);
  1265. clr_rxm->offset = 0;
  1266. return clr_skb;
  1267. }
  1268. /* Decrypt handlers
  1269. *
  1270. * tls_decrypt_sw() and tls_decrypt_device() are decrypt handlers.
  1271. * They must transform the darg in/out argument are as follows:
  1272. * | Input | Output
  1273. * -------------------------------------------------------------------
  1274. * zc | Zero-copy decrypt allowed | Zero-copy performed
  1275. * async | Async decrypt allowed | Async crypto used / in progress
  1276. * skb | * | Output skb
  1277. *
  1278. * If ZC decryption was performed darg.skb will point to the input skb.
  1279. */
  1280. /* This function decrypts the input skb into either out_iov or in out_sg
  1281. * or in skb buffers itself. The input parameter 'darg->zc' indicates if
  1282. * zero-copy mode needs to be tried or not. With zero-copy mode, either
  1283. * out_iov or out_sg must be non-NULL. In case both out_iov and out_sg are
  1284. * NULL, then the decryption happens inside skb buffers itself, i.e.
  1285. * zero-copy gets disabled and 'darg->zc' is updated.
  1286. */
  1287. static int tls_decrypt_sg(struct sock *sk, struct iov_iter *out_iov,
  1288. struct scatterlist *out_sg,
  1289. struct tls_decrypt_arg *darg)
  1290. {
  1291. struct tls_context *tls_ctx = tls_get_ctx(sk);
  1292. struct tls_sw_context_rx *ctx = tls_sw_ctx_rx(tls_ctx);
  1293. struct tls_prot_info *prot = &tls_ctx->prot_info;
  1294. int n_sgin, n_sgout, aead_size, err, pages = 0;
  1295. struct sk_buff *skb = tls_strp_msg(ctx);
  1296. const struct strp_msg *rxm = strp_msg(skb);
  1297. const struct tls_msg *tlm = tls_msg(skb);
  1298. struct aead_request *aead_req;
  1299. struct scatterlist *sgin = NULL;
  1300. struct scatterlist *sgout = NULL;
  1301. const int data_len = rxm->full_len - prot->overhead_size;
  1302. int tail_pages = !!prot->tail_size;
  1303. struct tls_decrypt_ctx *dctx;
  1304. struct sk_buff *clear_skb;
  1305. int iv_offset = 0;
  1306. u8 *mem;
  1307. n_sgin = skb_nsg(skb, rxm->offset + prot->prepend_size,
  1308. rxm->full_len - prot->prepend_size);
  1309. if (n_sgin < 1)
  1310. return n_sgin ?: -EBADMSG;
  1311. if (darg->zc && (out_iov || out_sg)) {
  1312. clear_skb = NULL;
  1313. if (out_iov)
  1314. n_sgout = 1 + tail_pages +
  1315. iov_iter_npages_cap(out_iov, INT_MAX, data_len);
  1316. else
  1317. n_sgout = sg_nents(out_sg);
  1318. } else {
  1319. darg->zc = false;
  1320. clear_skb = tls_alloc_clrtxt_skb(sk, skb, rxm->full_len);
  1321. if (!clear_skb)
  1322. return -ENOMEM;
  1323. n_sgout = 1 + skb_shinfo(clear_skb)->nr_frags;
  1324. }
  1325. /* Increment to accommodate AAD */
  1326. n_sgin = n_sgin + 1;
  1327. /* Allocate a single block of memory which contains
  1328. * aead_req || tls_decrypt_ctx.
  1329. * Both structs are variable length.
  1330. */
  1331. aead_size = sizeof(*aead_req) + crypto_aead_reqsize(ctx->aead_recv);
  1332. aead_size = ALIGN(aead_size, __alignof__(*dctx));
  1333. mem = kmalloc(aead_size + struct_size(dctx, sg, size_add(n_sgin, n_sgout)),
  1334. sk->sk_allocation);
  1335. if (!mem) {
  1336. err = -ENOMEM;
  1337. goto exit_free_skb;
  1338. }
  1339. /* Segment the allocated memory */
  1340. aead_req = (struct aead_request *)mem;
  1341. dctx = (struct tls_decrypt_ctx *)(mem + aead_size);
  1342. dctx->sk = sk;
  1343. sgin = &dctx->sg[0];
  1344. sgout = &dctx->sg[n_sgin];
  1345. /* For CCM based ciphers, first byte of nonce+iv is a constant */
  1346. switch (prot->cipher_type) {
  1347. case TLS_CIPHER_AES_CCM_128:
  1348. dctx->iv[0] = TLS_AES_CCM_IV_B0_BYTE;
  1349. iv_offset = 1;
  1350. break;
  1351. case TLS_CIPHER_SM4_CCM:
  1352. dctx->iv[0] = TLS_SM4_CCM_IV_B0_BYTE;
  1353. iv_offset = 1;
  1354. break;
  1355. }
  1356. /* Prepare IV */
  1357. if (prot->version == TLS_1_3_VERSION ||
  1358. prot->cipher_type == TLS_CIPHER_CHACHA20_POLY1305) {
  1359. memcpy(&dctx->iv[iv_offset], tls_ctx->rx.iv,
  1360. prot->iv_size + prot->salt_size);
  1361. } else {
  1362. err = skb_copy_bits(skb, rxm->offset + TLS_HEADER_SIZE,
  1363. &dctx->iv[iv_offset] + prot->salt_size,
  1364. prot->iv_size);
  1365. if (err < 0)
  1366. goto exit_free;
  1367. memcpy(&dctx->iv[iv_offset], tls_ctx->rx.iv, prot->salt_size);
  1368. }
  1369. tls_xor_iv_with_seq(prot, &dctx->iv[iv_offset], tls_ctx->rx.rec_seq);
  1370. /* Prepare AAD */
  1371. tls_make_aad(dctx->aad, rxm->full_len - prot->overhead_size +
  1372. prot->tail_size,
  1373. tls_ctx->rx.rec_seq, tlm->control, prot);
  1374. /* Prepare sgin */
  1375. sg_init_table(sgin, n_sgin);
  1376. sg_set_buf(&sgin[0], dctx->aad, prot->aad_size);
  1377. err = skb_to_sgvec(skb, &sgin[1],
  1378. rxm->offset + prot->prepend_size,
  1379. rxm->full_len - prot->prepend_size);
  1380. if (err < 0)
  1381. goto exit_free;
  1382. if (clear_skb) {
  1383. sg_init_table(sgout, n_sgout);
  1384. sg_set_buf(&sgout[0], dctx->aad, prot->aad_size);
  1385. err = skb_to_sgvec(clear_skb, &sgout[1], prot->prepend_size,
  1386. data_len + prot->tail_size);
  1387. if (err < 0)
  1388. goto exit_free;
  1389. } else if (out_iov) {
  1390. sg_init_table(sgout, n_sgout);
  1391. sg_set_buf(&sgout[0], dctx->aad, prot->aad_size);
  1392. err = tls_setup_from_iter(out_iov, data_len, &pages, &sgout[1],
  1393. (n_sgout - 1 - tail_pages));
  1394. if (err < 0)
  1395. goto exit_free_pages;
  1396. if (prot->tail_size) {
  1397. sg_unmark_end(&sgout[pages]);
  1398. sg_set_buf(&sgout[pages + 1], &dctx->tail,
  1399. prot->tail_size);
  1400. sg_mark_end(&sgout[pages + 1]);
  1401. }
  1402. } else if (out_sg) {
  1403. memcpy(sgout, out_sg, n_sgout * sizeof(*sgout));
  1404. }
  1405. dctx->free_sgout = !!pages;
  1406. /* Prepare and submit AEAD request */
  1407. err = tls_do_decryption(sk, sgin, sgout, dctx->iv,
  1408. data_len + prot->tail_size, aead_req, darg);
  1409. if (err) {
  1410. if (darg->async_done)
  1411. goto exit_free_skb;
  1412. goto exit_free_pages;
  1413. }
  1414. darg->skb = clear_skb ?: tls_strp_msg(ctx);
  1415. clear_skb = NULL;
  1416. if (unlikely(darg->async)) {
  1417. err = tls_strp_msg_hold(&ctx->strp, &ctx->async_hold);
  1418. if (err) {
  1419. err = tls_decrypt_async_wait(ctx);
  1420. darg->async = false;
  1421. }
  1422. return err;
  1423. }
  1424. if (unlikely(darg->async_done))
  1425. return 0;
  1426. if (prot->tail_size)
  1427. darg->tail = dctx->tail;
  1428. exit_free_pages:
  1429. /* Release the pages in case iov was mapped to pages */
  1430. for (; pages > 0; pages--)
  1431. put_page(sg_page(&sgout[pages]));
  1432. exit_free:
  1433. kfree(mem);
  1434. exit_free_skb:
  1435. consume_skb(clear_skb);
  1436. return err;
  1437. }
  1438. static int
  1439. tls_decrypt_sw(struct sock *sk, struct tls_context *tls_ctx,
  1440. struct msghdr *msg, struct tls_decrypt_arg *darg)
  1441. {
  1442. struct tls_sw_context_rx *ctx = tls_sw_ctx_rx(tls_ctx);
  1443. struct tls_prot_info *prot = &tls_ctx->prot_info;
  1444. struct strp_msg *rxm;
  1445. int pad, err;
  1446. err = tls_decrypt_sg(sk, &msg->msg_iter, NULL, darg);
  1447. if (err < 0) {
  1448. if (err == -EBADMSG)
  1449. TLS_INC_STATS(sock_net(sk), LINUX_MIB_TLSDECRYPTERROR);
  1450. return err;
  1451. }
  1452. /* keep going even for ->async, the code below is TLS 1.3 */
  1453. /* If opportunistic TLS 1.3 ZC failed retry without ZC */
  1454. if (unlikely(darg->zc && prot->version == TLS_1_3_VERSION &&
  1455. darg->tail != TLS_RECORD_TYPE_DATA)) {
  1456. darg->zc = false;
  1457. if (!darg->tail)
  1458. TLS_INC_STATS(sock_net(sk), LINUX_MIB_TLSRXNOPADVIOL);
  1459. TLS_INC_STATS(sock_net(sk), LINUX_MIB_TLSDECRYPTRETRY);
  1460. return tls_decrypt_sw(sk, tls_ctx, msg, darg);
  1461. }
  1462. pad = tls_padding_length(prot, darg->skb, darg);
  1463. if (pad < 0) {
  1464. if (darg->skb != tls_strp_msg(ctx))
  1465. consume_skb(darg->skb);
  1466. return pad;
  1467. }
  1468. rxm = strp_msg(darg->skb);
  1469. rxm->full_len -= pad;
  1470. return 0;
  1471. }
  1472. static int
  1473. tls_decrypt_device(struct sock *sk, struct msghdr *msg,
  1474. struct tls_context *tls_ctx, struct tls_decrypt_arg *darg)
  1475. {
  1476. struct tls_sw_context_rx *ctx = tls_sw_ctx_rx(tls_ctx);
  1477. struct tls_prot_info *prot = &tls_ctx->prot_info;
  1478. struct strp_msg *rxm;
  1479. int pad, err;
  1480. if (tls_ctx->rx_conf != TLS_HW)
  1481. return 0;
  1482. err = tls_device_decrypted(sk, tls_ctx);
  1483. if (err <= 0)
  1484. return err;
  1485. pad = tls_padding_length(prot, tls_strp_msg(ctx), darg);
  1486. if (pad < 0)
  1487. return pad;
  1488. darg->async = false;
  1489. darg->skb = tls_strp_msg(ctx);
  1490. /* ->zc downgrade check, in case TLS 1.3 gets here */
  1491. darg->zc &= !(prot->version == TLS_1_3_VERSION &&
  1492. tls_msg(darg->skb)->control != TLS_RECORD_TYPE_DATA);
  1493. rxm = strp_msg(darg->skb);
  1494. rxm->full_len -= pad;
  1495. if (!darg->zc) {
  1496. /* Non-ZC case needs a real skb */
  1497. darg->skb = tls_strp_msg_detach(ctx);
  1498. if (!darg->skb)
  1499. return -ENOMEM;
  1500. } else {
  1501. unsigned int off, len;
  1502. /* In ZC case nobody cares about the output skb.
  1503. * Just copy the data here. Note the skb is not fully trimmed.
  1504. */
  1505. off = rxm->offset + prot->prepend_size;
  1506. len = rxm->full_len - prot->overhead_size;
  1507. err = skb_copy_datagram_msg(darg->skb, off, msg, len);
  1508. if (err)
  1509. return err;
  1510. }
  1511. return 1;
  1512. }
  1513. static int tls_rx_one_record(struct sock *sk, struct msghdr *msg,
  1514. struct tls_decrypt_arg *darg)
  1515. {
  1516. struct tls_context *tls_ctx = tls_get_ctx(sk);
  1517. struct tls_prot_info *prot = &tls_ctx->prot_info;
  1518. struct strp_msg *rxm;
  1519. int err;
  1520. err = tls_decrypt_device(sk, msg, tls_ctx, darg);
  1521. if (!err)
  1522. err = tls_decrypt_sw(sk, tls_ctx, msg, darg);
  1523. if (err < 0)
  1524. return err;
  1525. rxm = strp_msg(darg->skb);
  1526. rxm->offset += prot->prepend_size;
  1527. rxm->full_len -= prot->overhead_size;
  1528. tls_advance_record_sn(sk, prot, &tls_ctx->rx);
  1529. return 0;
  1530. }
  1531. int decrypt_skb(struct sock *sk, struct scatterlist *sgout)
  1532. {
  1533. struct tls_decrypt_arg darg = { .zc = true, };
  1534. return tls_decrypt_sg(sk, NULL, sgout, &darg);
  1535. }
  1536. /* All records returned from a recvmsg() call must have the same type.
  1537. * 0 is not a valid content type. Use it as "no type reported, yet".
  1538. */
  1539. static int tls_record_content_type(struct msghdr *msg, struct tls_msg *tlm,
  1540. u8 *control)
  1541. {
  1542. int err;
  1543. if (!*control) {
  1544. *control = tlm->control;
  1545. if (!*control)
  1546. return -EBADMSG;
  1547. err = put_cmsg(msg, SOL_TLS, TLS_GET_RECORD_TYPE,
  1548. sizeof(*control), control);
  1549. if (*control != TLS_RECORD_TYPE_DATA) {
  1550. if (err || msg->msg_flags & MSG_CTRUNC)
  1551. return -EIO;
  1552. }
  1553. } else if (*control != tlm->control) {
  1554. return 0;
  1555. }
  1556. return 1;
  1557. }
  1558. static void tls_rx_rec_done(struct tls_sw_context_rx *ctx)
  1559. {
  1560. tls_strp_msg_done(&ctx->strp);
  1561. }
  1562. /* This function traverses the rx_list in tls receive context to copies the
  1563. * decrypted records into the buffer provided by caller zero copy is not
  1564. * true. Further, the records are removed from the rx_list if it is not a peek
  1565. * case and the record has been consumed completely.
  1566. */
  1567. static int process_rx_list(struct tls_sw_context_rx *ctx,
  1568. struct msghdr *msg,
  1569. u8 *control,
  1570. size_t skip,
  1571. size_t len,
  1572. bool is_peek,
  1573. bool *more)
  1574. {
  1575. struct sk_buff *skb = skb_peek(&ctx->rx_list);
  1576. struct tls_msg *tlm;
  1577. ssize_t copied = 0;
  1578. int err;
  1579. while (skip && skb) {
  1580. struct strp_msg *rxm = strp_msg(skb);
  1581. tlm = tls_msg(skb);
  1582. err = tls_record_content_type(msg, tlm, control);
  1583. if (err <= 0)
  1584. goto more;
  1585. if (skip < rxm->full_len)
  1586. break;
  1587. skip = skip - rxm->full_len;
  1588. skb = skb_peek_next(skb, &ctx->rx_list);
  1589. }
  1590. while (len && skb) {
  1591. struct sk_buff *next_skb;
  1592. struct strp_msg *rxm = strp_msg(skb);
  1593. int chunk = min_t(unsigned int, rxm->full_len - skip, len);
  1594. tlm = tls_msg(skb);
  1595. err = tls_record_content_type(msg, tlm, control);
  1596. if (err <= 0)
  1597. goto more;
  1598. err = skb_copy_datagram_msg(skb, rxm->offset + skip,
  1599. msg, chunk);
  1600. if (err < 0)
  1601. goto more;
  1602. len = len - chunk;
  1603. copied = copied + chunk;
  1604. /* Consume the data from record if it is non-peek case*/
  1605. if (!is_peek) {
  1606. rxm->offset = rxm->offset + chunk;
  1607. rxm->full_len = rxm->full_len - chunk;
  1608. /* Return if there is unconsumed data in the record */
  1609. if (rxm->full_len - skip)
  1610. break;
  1611. }
  1612. /* The remaining skip-bytes must lie in 1st record in rx_list.
  1613. * So from the 2nd record, 'skip' should be 0.
  1614. */
  1615. skip = 0;
  1616. if (msg)
  1617. msg->msg_flags |= MSG_EOR;
  1618. next_skb = skb_peek_next(skb, &ctx->rx_list);
  1619. if (!is_peek) {
  1620. __skb_unlink(skb, &ctx->rx_list);
  1621. consume_skb(skb);
  1622. }
  1623. skb = next_skb;
  1624. }
  1625. err = 0;
  1626. out:
  1627. return copied ? : err;
  1628. more:
  1629. if (more)
  1630. *more = true;
  1631. goto out;
  1632. }
  1633. static bool
  1634. tls_read_flush_backlog(struct sock *sk, struct tls_prot_info *prot,
  1635. size_t len_left, size_t decrypted, ssize_t done,
  1636. size_t *flushed_at)
  1637. {
  1638. size_t max_rec;
  1639. if (len_left <= decrypted)
  1640. return false;
  1641. max_rec = prot->overhead_size - prot->tail_size + TLS_MAX_PAYLOAD_SIZE;
  1642. if (done - *flushed_at < SZ_128K && tcp_inq(sk) > max_rec)
  1643. return false;
  1644. *flushed_at = done;
  1645. return sk_flush_backlog(sk);
  1646. }
  1647. static int tls_rx_reader_acquire(struct sock *sk, struct tls_sw_context_rx *ctx,
  1648. bool nonblock)
  1649. {
  1650. long timeo;
  1651. int ret;
  1652. timeo = sock_rcvtimeo(sk, nonblock);
  1653. while (unlikely(ctx->reader_present)) {
  1654. DEFINE_WAIT_FUNC(wait, woken_wake_function);
  1655. ctx->reader_contended = 1;
  1656. add_wait_queue(&ctx->wq, &wait);
  1657. ret = sk_wait_event(sk, &timeo,
  1658. !READ_ONCE(ctx->reader_present), &wait);
  1659. remove_wait_queue(&ctx->wq, &wait);
  1660. if (timeo <= 0)
  1661. return -EAGAIN;
  1662. if (signal_pending(current))
  1663. return sock_intr_errno(timeo);
  1664. if (ret < 0)
  1665. return ret;
  1666. }
  1667. WRITE_ONCE(ctx->reader_present, 1);
  1668. return 0;
  1669. }
  1670. static int tls_rx_reader_lock(struct sock *sk, struct tls_sw_context_rx *ctx,
  1671. bool nonblock)
  1672. {
  1673. int err;
  1674. lock_sock(sk);
  1675. err = tls_rx_reader_acquire(sk, ctx, nonblock);
  1676. if (err)
  1677. release_sock(sk);
  1678. return err;
  1679. }
  1680. static void tls_rx_reader_release(struct sock *sk, struct tls_sw_context_rx *ctx)
  1681. {
  1682. if (unlikely(ctx->reader_contended)) {
  1683. if (wq_has_sleeper(&ctx->wq))
  1684. wake_up(&ctx->wq);
  1685. else
  1686. ctx->reader_contended = 0;
  1687. WARN_ON_ONCE(!ctx->reader_present);
  1688. }
  1689. WRITE_ONCE(ctx->reader_present, 0);
  1690. }
  1691. static void tls_rx_reader_unlock(struct sock *sk, struct tls_sw_context_rx *ctx)
  1692. {
  1693. tls_rx_reader_release(sk, ctx);
  1694. release_sock(sk);
  1695. }
  1696. int tls_sw_recvmsg(struct sock *sk,
  1697. struct msghdr *msg,
  1698. size_t len,
  1699. int flags,
  1700. int *addr_len)
  1701. {
  1702. struct tls_context *tls_ctx = tls_get_ctx(sk);
  1703. struct tls_sw_context_rx *ctx = tls_sw_ctx_rx(tls_ctx);
  1704. struct tls_prot_info *prot = &tls_ctx->prot_info;
  1705. ssize_t decrypted = 0, async_copy_bytes = 0;
  1706. struct sk_psock *psock;
  1707. unsigned char control = 0;
  1708. size_t flushed_at = 0;
  1709. struct strp_msg *rxm;
  1710. struct tls_msg *tlm;
  1711. ssize_t copied = 0;
  1712. ssize_t peeked = 0;
  1713. bool async = false;
  1714. int target, err;
  1715. bool is_kvec = iov_iter_is_kvec(&msg->msg_iter);
  1716. bool is_peek = flags & MSG_PEEK;
  1717. bool rx_more = false;
  1718. bool released = true;
  1719. bool bpf_strp_enabled;
  1720. bool zc_capable;
  1721. if (unlikely(flags & MSG_ERRQUEUE))
  1722. return sock_recv_errqueue(sk, msg, len, SOL_IP, IP_RECVERR);
  1723. err = tls_rx_reader_lock(sk, ctx, flags & MSG_DONTWAIT);
  1724. if (err < 0)
  1725. return err;
  1726. psock = sk_psock_get(sk);
  1727. bpf_strp_enabled = sk_psock_strp_enabled(psock);
  1728. /* If crypto failed the connection is broken */
  1729. err = ctx->async_wait.err;
  1730. if (err)
  1731. goto end;
  1732. /* Process pending decrypted records. It must be non-zero-copy */
  1733. err = process_rx_list(ctx, msg, &control, 0, len, is_peek, &rx_more);
  1734. if (err < 0)
  1735. goto end;
  1736. /* process_rx_list() will set @control if it processed any records */
  1737. copied = err;
  1738. if (len <= copied || rx_more ||
  1739. (control && control != TLS_RECORD_TYPE_DATA))
  1740. goto end;
  1741. target = sock_rcvlowat(sk, flags & MSG_WAITALL, len);
  1742. len = len - copied;
  1743. zc_capable = !bpf_strp_enabled && !is_kvec && !is_peek &&
  1744. ctx->zc_capable;
  1745. decrypted = 0;
  1746. while (len && (decrypted + copied < target || tls_strp_msg_ready(ctx))) {
  1747. struct tls_decrypt_arg darg;
  1748. int to_decrypt, chunk;
  1749. err = tls_rx_rec_wait(sk, psock, flags & MSG_DONTWAIT,
  1750. released);
  1751. if (err <= 0) {
  1752. if (psock) {
  1753. chunk = sk_msg_recvmsg(sk, psock, msg, len,
  1754. flags);
  1755. if (chunk > 0) {
  1756. decrypted += chunk;
  1757. len -= chunk;
  1758. continue;
  1759. }
  1760. }
  1761. goto recv_end;
  1762. }
  1763. memset(&darg.inargs, 0, sizeof(darg.inargs));
  1764. rxm = strp_msg(tls_strp_msg(ctx));
  1765. tlm = tls_msg(tls_strp_msg(ctx));
  1766. to_decrypt = rxm->full_len - prot->overhead_size;
  1767. if (zc_capable && to_decrypt <= len &&
  1768. tlm->control == TLS_RECORD_TYPE_DATA)
  1769. darg.zc = true;
  1770. /* Do not use async mode if record is non-data */
  1771. if (tlm->control == TLS_RECORD_TYPE_DATA && !bpf_strp_enabled)
  1772. darg.async = ctx->async_capable;
  1773. else
  1774. darg.async = false;
  1775. err = tls_rx_one_record(sk, msg, &darg);
  1776. if (err < 0) {
  1777. tls_err_abort(sk, -EBADMSG);
  1778. goto recv_end;
  1779. }
  1780. async |= darg.async;
  1781. /* If the type of records being processed is not known yet,
  1782. * set it to record type just dequeued. If it is already known,
  1783. * but does not match the record type just dequeued, go to end.
  1784. * We always get record type here since for tls1.2, record type
  1785. * is known just after record is dequeued from stream parser.
  1786. * For tls1.3, we disable async.
  1787. */
  1788. err = tls_record_content_type(msg, tls_msg(darg.skb), &control);
  1789. if (err <= 0) {
  1790. DEBUG_NET_WARN_ON_ONCE(darg.zc);
  1791. tls_rx_rec_done(ctx);
  1792. put_on_rx_list_err:
  1793. __skb_queue_tail(&ctx->rx_list, darg.skb);
  1794. goto recv_end;
  1795. }
  1796. /* periodically flush backlog, and feed strparser */
  1797. released = tls_read_flush_backlog(sk, prot, len, to_decrypt,
  1798. decrypted + copied,
  1799. &flushed_at);
  1800. /* TLS 1.3 may have updated the length by more than overhead */
  1801. rxm = strp_msg(darg.skb);
  1802. chunk = rxm->full_len;
  1803. tls_rx_rec_done(ctx);
  1804. if (!darg.zc) {
  1805. bool partially_consumed = chunk > len;
  1806. struct sk_buff *skb = darg.skb;
  1807. DEBUG_NET_WARN_ON_ONCE(darg.skb == ctx->strp.anchor);
  1808. if (async) {
  1809. /* TLS 1.2-only, to_decrypt must be text len */
  1810. chunk = min_t(int, to_decrypt, len);
  1811. async_copy_bytes += chunk;
  1812. put_on_rx_list:
  1813. decrypted += chunk;
  1814. len -= chunk;
  1815. __skb_queue_tail(&ctx->rx_list, skb);
  1816. if (unlikely(control != TLS_RECORD_TYPE_DATA))
  1817. break;
  1818. continue;
  1819. }
  1820. if (bpf_strp_enabled) {
  1821. released = true;
  1822. err = sk_psock_tls_strp_read(psock, skb);
  1823. if (err != __SK_PASS) {
  1824. rxm->offset = rxm->offset + rxm->full_len;
  1825. rxm->full_len = 0;
  1826. if (err == __SK_DROP)
  1827. consume_skb(skb);
  1828. continue;
  1829. }
  1830. }
  1831. if (partially_consumed)
  1832. chunk = len;
  1833. err = skb_copy_datagram_msg(skb, rxm->offset,
  1834. msg, chunk);
  1835. if (err < 0)
  1836. goto put_on_rx_list_err;
  1837. if (is_peek) {
  1838. peeked += chunk;
  1839. goto put_on_rx_list;
  1840. }
  1841. if (partially_consumed) {
  1842. rxm->offset += chunk;
  1843. rxm->full_len -= chunk;
  1844. goto put_on_rx_list;
  1845. }
  1846. consume_skb(skb);
  1847. }
  1848. decrypted += chunk;
  1849. len -= chunk;
  1850. /* Return full control message to userspace before trying
  1851. * to parse another message type
  1852. */
  1853. msg->msg_flags |= MSG_EOR;
  1854. if (control != TLS_RECORD_TYPE_DATA)
  1855. break;
  1856. }
  1857. recv_end:
  1858. if (async) {
  1859. int ret;
  1860. /* Wait for all previously submitted records to be decrypted */
  1861. ret = tls_decrypt_async_wait(ctx);
  1862. __skb_queue_purge(&ctx->async_hold);
  1863. if (ret) {
  1864. if (err >= 0 || err == -EINPROGRESS)
  1865. err = ret;
  1866. goto end;
  1867. }
  1868. /* Drain records from the rx_list & copy if required */
  1869. if (is_peek)
  1870. err = process_rx_list(ctx, msg, &control, copied + peeked,
  1871. decrypted - peeked, is_peek, NULL);
  1872. else
  1873. err = process_rx_list(ctx, msg, &control, 0,
  1874. async_copy_bytes, is_peek, NULL);
  1875. /* we could have copied less than we wanted, and possibly nothing */
  1876. decrypted += max(err, 0) - async_copy_bytes;
  1877. }
  1878. copied += decrypted;
  1879. end:
  1880. tls_rx_reader_unlock(sk, ctx);
  1881. if (psock)
  1882. sk_psock_put(sk, psock);
  1883. return copied ? : err;
  1884. }
  1885. ssize_t tls_sw_splice_read(struct socket *sock, loff_t *ppos,
  1886. struct pipe_inode_info *pipe,
  1887. size_t len, unsigned int flags)
  1888. {
  1889. struct tls_context *tls_ctx = tls_get_ctx(sock->sk);
  1890. struct tls_sw_context_rx *ctx = tls_sw_ctx_rx(tls_ctx);
  1891. struct strp_msg *rxm = NULL;
  1892. struct sock *sk = sock->sk;
  1893. struct tls_msg *tlm;
  1894. struct sk_buff *skb;
  1895. ssize_t copied = 0;
  1896. int chunk;
  1897. int err;
  1898. err = tls_rx_reader_lock(sk, ctx, flags & SPLICE_F_NONBLOCK);
  1899. if (err < 0)
  1900. return err;
  1901. if (!skb_queue_empty(&ctx->rx_list)) {
  1902. skb = __skb_dequeue(&ctx->rx_list);
  1903. } else {
  1904. struct tls_decrypt_arg darg;
  1905. err = tls_rx_rec_wait(sk, NULL, flags & SPLICE_F_NONBLOCK,
  1906. true);
  1907. if (err <= 0)
  1908. goto splice_read_end;
  1909. memset(&darg.inargs, 0, sizeof(darg.inargs));
  1910. err = tls_rx_one_record(sk, NULL, &darg);
  1911. if (err < 0) {
  1912. tls_err_abort(sk, -EBADMSG);
  1913. goto splice_read_end;
  1914. }
  1915. tls_rx_rec_done(ctx);
  1916. skb = darg.skb;
  1917. }
  1918. rxm = strp_msg(skb);
  1919. tlm = tls_msg(skb);
  1920. /* splice does not support reading control messages */
  1921. if (tlm->control != TLS_RECORD_TYPE_DATA) {
  1922. err = -EINVAL;
  1923. goto splice_requeue;
  1924. }
  1925. chunk = min_t(unsigned int, rxm->full_len, len);
  1926. copied = skb_splice_bits(skb, sk, rxm->offset, pipe, chunk, flags);
  1927. if (copied < 0)
  1928. goto splice_requeue;
  1929. if (chunk < rxm->full_len) {
  1930. rxm->offset += len;
  1931. rxm->full_len -= len;
  1932. goto splice_requeue;
  1933. }
  1934. consume_skb(skb);
  1935. splice_read_end:
  1936. tls_rx_reader_unlock(sk, ctx);
  1937. return copied ? : err;
  1938. splice_requeue:
  1939. __skb_queue_head(&ctx->rx_list, skb);
  1940. goto splice_read_end;
  1941. }
  1942. int tls_sw_read_sock(struct sock *sk, read_descriptor_t *desc,
  1943. sk_read_actor_t read_actor)
  1944. {
  1945. struct tls_context *tls_ctx = tls_get_ctx(sk);
  1946. struct tls_sw_context_rx *ctx = tls_sw_ctx_rx(tls_ctx);
  1947. struct tls_prot_info *prot = &tls_ctx->prot_info;
  1948. struct strp_msg *rxm = NULL;
  1949. struct sk_buff *skb = NULL;
  1950. struct sk_psock *psock;
  1951. size_t flushed_at = 0;
  1952. bool released = true;
  1953. struct tls_msg *tlm;
  1954. ssize_t copied = 0;
  1955. ssize_t decrypted;
  1956. int err, used;
  1957. psock = sk_psock_get(sk);
  1958. if (psock) {
  1959. sk_psock_put(sk, psock);
  1960. return -EINVAL;
  1961. }
  1962. err = tls_rx_reader_acquire(sk, ctx, true);
  1963. if (err < 0)
  1964. return err;
  1965. /* If crypto failed the connection is broken */
  1966. err = ctx->async_wait.err;
  1967. if (err)
  1968. goto read_sock_end;
  1969. decrypted = 0;
  1970. do {
  1971. if (!skb_queue_empty(&ctx->rx_list)) {
  1972. skb = __skb_dequeue(&ctx->rx_list);
  1973. rxm = strp_msg(skb);
  1974. tlm = tls_msg(skb);
  1975. } else {
  1976. struct tls_decrypt_arg darg;
  1977. err = tls_rx_rec_wait(sk, NULL, true, released);
  1978. if (err <= 0)
  1979. goto read_sock_end;
  1980. memset(&darg.inargs, 0, sizeof(darg.inargs));
  1981. err = tls_rx_one_record(sk, NULL, &darg);
  1982. if (err < 0) {
  1983. tls_err_abort(sk, -EBADMSG);
  1984. goto read_sock_end;
  1985. }
  1986. released = tls_read_flush_backlog(sk, prot, INT_MAX,
  1987. 0, decrypted,
  1988. &flushed_at);
  1989. skb = darg.skb;
  1990. rxm = strp_msg(skb);
  1991. tlm = tls_msg(skb);
  1992. decrypted += rxm->full_len;
  1993. tls_rx_rec_done(ctx);
  1994. }
  1995. /* read_sock does not support reading control messages */
  1996. if (tlm->control != TLS_RECORD_TYPE_DATA) {
  1997. err = -EINVAL;
  1998. goto read_sock_requeue;
  1999. }
  2000. used = read_actor(desc, skb, rxm->offset, rxm->full_len);
  2001. if (used <= 0) {
  2002. if (!copied)
  2003. err = used;
  2004. goto read_sock_requeue;
  2005. }
  2006. copied += used;
  2007. if (used < rxm->full_len) {
  2008. rxm->offset += used;
  2009. rxm->full_len -= used;
  2010. if (!desc->count)
  2011. goto read_sock_requeue;
  2012. } else {
  2013. consume_skb(skb);
  2014. if (!desc->count)
  2015. skb = NULL;
  2016. }
  2017. } while (skb);
  2018. read_sock_end:
  2019. tls_rx_reader_release(sk, ctx);
  2020. return copied ? : err;
  2021. read_sock_requeue:
  2022. __skb_queue_head(&ctx->rx_list, skb);
  2023. goto read_sock_end;
  2024. }
  2025. bool tls_sw_sock_is_readable(struct sock *sk)
  2026. {
  2027. struct tls_context *tls_ctx = tls_get_ctx(sk);
  2028. struct tls_sw_context_rx *ctx = tls_sw_ctx_rx(tls_ctx);
  2029. bool ingress_empty = true;
  2030. struct sk_psock *psock;
  2031. rcu_read_lock();
  2032. psock = sk_psock(sk);
  2033. if (psock)
  2034. ingress_empty = list_empty(&psock->ingress_msg);
  2035. rcu_read_unlock();
  2036. return !ingress_empty || tls_strp_msg_ready(ctx) ||
  2037. !skb_queue_empty(&ctx->rx_list);
  2038. }
  2039. int tls_rx_msg_size(struct tls_strparser *strp, struct sk_buff *skb)
  2040. {
  2041. struct tls_context *tls_ctx = tls_get_ctx(strp->sk);
  2042. struct tls_prot_info *prot = &tls_ctx->prot_info;
  2043. char header[TLS_HEADER_SIZE + TLS_MAX_IV_SIZE];
  2044. size_t cipher_overhead;
  2045. size_t data_len = 0;
  2046. int ret;
  2047. /* Verify that we have a full TLS header, or wait for more data */
  2048. if (strp->stm.offset + prot->prepend_size > skb->len)
  2049. return 0;
  2050. /* Sanity-check size of on-stack buffer. */
  2051. if (WARN_ON(prot->prepend_size > sizeof(header))) {
  2052. ret = -EINVAL;
  2053. goto read_failure;
  2054. }
  2055. /* Linearize header to local buffer */
  2056. ret = skb_copy_bits(skb, strp->stm.offset, header, prot->prepend_size);
  2057. if (ret < 0)
  2058. goto read_failure;
  2059. strp->mark = header[0];
  2060. data_len = ((header[4] & 0xFF) | (header[3] << 8));
  2061. cipher_overhead = prot->tag_size;
  2062. if (prot->version != TLS_1_3_VERSION &&
  2063. prot->cipher_type != TLS_CIPHER_CHACHA20_POLY1305)
  2064. cipher_overhead += prot->iv_size;
  2065. if (data_len > TLS_MAX_PAYLOAD_SIZE + cipher_overhead +
  2066. prot->tail_size) {
  2067. ret = -EMSGSIZE;
  2068. goto read_failure;
  2069. }
  2070. if (data_len < cipher_overhead) {
  2071. ret = -EBADMSG;
  2072. goto read_failure;
  2073. }
  2074. /* Note that both TLS1.3 and TLS1.2 use TLS_1_2 version here */
  2075. if (header[1] != TLS_1_2_VERSION_MINOR ||
  2076. header[2] != TLS_1_2_VERSION_MAJOR) {
  2077. ret = -EINVAL;
  2078. goto read_failure;
  2079. }
  2080. tls_device_rx_resync_new_rec(strp->sk, data_len + TLS_HEADER_SIZE,
  2081. TCP_SKB_CB(skb)->seq + strp->stm.offset);
  2082. return data_len + TLS_HEADER_SIZE;
  2083. read_failure:
  2084. tls_strp_abort_strp(strp, ret);
  2085. return ret;
  2086. }
  2087. void tls_rx_msg_ready(struct tls_strparser *strp)
  2088. {
  2089. struct tls_sw_context_rx *ctx;
  2090. ctx = container_of(strp, struct tls_sw_context_rx, strp);
  2091. ctx->saved_data_ready(strp->sk);
  2092. }
  2093. static void tls_data_ready(struct sock *sk)
  2094. {
  2095. struct tls_context *tls_ctx = tls_get_ctx(sk);
  2096. struct tls_sw_context_rx *ctx = tls_sw_ctx_rx(tls_ctx);
  2097. struct sk_psock *psock;
  2098. gfp_t alloc_save;
  2099. trace_sk_data_ready(sk);
  2100. alloc_save = sk->sk_allocation;
  2101. sk->sk_allocation = GFP_ATOMIC;
  2102. tls_strp_data_ready(&ctx->strp);
  2103. sk->sk_allocation = alloc_save;
  2104. psock = sk_psock_get(sk);
  2105. if (psock) {
  2106. if (!list_empty(&psock->ingress_msg))
  2107. ctx->saved_data_ready(sk);
  2108. sk_psock_put(sk, psock);
  2109. }
  2110. }
  2111. void tls_sw_cancel_work_tx(struct tls_context *tls_ctx)
  2112. {
  2113. struct tls_sw_context_tx *ctx = tls_sw_ctx_tx(tls_ctx);
  2114. set_bit(BIT_TX_CLOSING, &ctx->tx_bitmask);
  2115. set_bit(BIT_TX_SCHEDULED, &ctx->tx_bitmask);
  2116. cancel_delayed_work_sync(&ctx->tx_work.work);
  2117. }
  2118. void tls_sw_release_resources_tx(struct sock *sk)
  2119. {
  2120. struct tls_context *tls_ctx = tls_get_ctx(sk);
  2121. struct tls_sw_context_tx *ctx = tls_sw_ctx_tx(tls_ctx);
  2122. struct tls_rec *rec, *tmp;
  2123. /* Wait for any pending async encryptions to complete */
  2124. tls_encrypt_async_wait(ctx);
  2125. tls_tx_records(sk, -1);
  2126. /* Free up un-sent records in tx_list. First, free
  2127. * the partially sent record if any at head of tx_list.
  2128. */
  2129. if (tls_ctx->partially_sent_record) {
  2130. tls_free_partial_record(sk, tls_ctx);
  2131. rec = list_first_entry(&ctx->tx_list,
  2132. struct tls_rec, list);
  2133. list_del(&rec->list);
  2134. sk_msg_free(sk, &rec->msg_plaintext);
  2135. kfree(rec);
  2136. }
  2137. list_for_each_entry_safe(rec, tmp, &ctx->tx_list, list) {
  2138. list_del(&rec->list);
  2139. sk_msg_free(sk, &rec->msg_encrypted);
  2140. sk_msg_free(sk, &rec->msg_plaintext);
  2141. kfree(rec);
  2142. }
  2143. crypto_free_aead(ctx->aead_send);
  2144. tls_free_open_rec(sk);
  2145. }
  2146. void tls_sw_free_ctx_tx(struct tls_context *tls_ctx)
  2147. {
  2148. struct tls_sw_context_tx *ctx = tls_sw_ctx_tx(tls_ctx);
  2149. kfree(ctx);
  2150. }
  2151. void tls_sw_release_resources_rx(struct sock *sk)
  2152. {
  2153. struct tls_context *tls_ctx = tls_get_ctx(sk);
  2154. struct tls_sw_context_rx *ctx = tls_sw_ctx_rx(tls_ctx);
  2155. if (ctx->aead_recv) {
  2156. __skb_queue_purge(&ctx->rx_list);
  2157. crypto_free_aead(ctx->aead_recv);
  2158. tls_strp_stop(&ctx->strp);
  2159. /* If tls_sw_strparser_arm() was not called (cleanup paths)
  2160. * we still want to tls_strp_stop(), but sk->sk_data_ready was
  2161. * never swapped.
  2162. */
  2163. if (ctx->saved_data_ready) {
  2164. write_lock_bh(&sk->sk_callback_lock);
  2165. sk->sk_data_ready = ctx->saved_data_ready;
  2166. write_unlock_bh(&sk->sk_callback_lock);
  2167. }
  2168. }
  2169. }
  2170. void tls_sw_strparser_done(struct tls_context *tls_ctx)
  2171. {
  2172. struct tls_sw_context_rx *ctx = tls_sw_ctx_rx(tls_ctx);
  2173. tls_strp_done(&ctx->strp);
  2174. }
  2175. void tls_sw_free_ctx_rx(struct tls_context *tls_ctx)
  2176. {
  2177. struct tls_sw_context_rx *ctx = tls_sw_ctx_rx(tls_ctx);
  2178. kfree(ctx);
  2179. }
  2180. void tls_sw_free_resources_rx(struct sock *sk)
  2181. {
  2182. struct tls_context *tls_ctx = tls_get_ctx(sk);
  2183. tls_sw_release_resources_rx(sk);
  2184. tls_sw_free_ctx_rx(tls_ctx);
  2185. }
  2186. /* The work handler to transmitt the encrypted records in tx_list */
  2187. static void tx_work_handler(struct work_struct *work)
  2188. {
  2189. struct delayed_work *delayed_work = to_delayed_work(work);
  2190. struct tx_work *tx_work = container_of(delayed_work,
  2191. struct tx_work, work);
  2192. struct sock *sk = tx_work->sk;
  2193. struct tls_context *tls_ctx = tls_get_ctx(sk);
  2194. struct tls_sw_context_tx *ctx;
  2195. if (unlikely(!tls_ctx))
  2196. return;
  2197. ctx = tls_sw_ctx_tx(tls_ctx);
  2198. if (test_bit(BIT_TX_CLOSING, &ctx->tx_bitmask))
  2199. return;
  2200. if (!test_and_clear_bit(BIT_TX_SCHEDULED, &ctx->tx_bitmask))
  2201. return;
  2202. if (mutex_trylock(&tls_ctx->tx_lock)) {
  2203. lock_sock(sk);
  2204. tls_tx_records(sk, -1);
  2205. release_sock(sk);
  2206. mutex_unlock(&tls_ctx->tx_lock);
  2207. } else if (!test_and_set_bit(BIT_TX_SCHEDULED, &ctx->tx_bitmask)) {
  2208. /* Someone is holding the tx_lock, they will likely run Tx
  2209. * and cancel the work on their way out of the lock section.
  2210. * Schedule a long delay just in case.
  2211. */
  2212. schedule_delayed_work(&ctx->tx_work.work, msecs_to_jiffies(10));
  2213. }
  2214. }
  2215. static bool tls_is_tx_ready(struct tls_sw_context_tx *ctx)
  2216. {
  2217. struct tls_rec *rec;
  2218. rec = list_first_entry_or_null(&ctx->tx_list, struct tls_rec, list);
  2219. if (!rec)
  2220. return false;
  2221. return READ_ONCE(rec->tx_ready);
  2222. }
  2223. void tls_sw_write_space(struct sock *sk, struct tls_context *ctx)
  2224. {
  2225. struct tls_sw_context_tx *tx_ctx = tls_sw_ctx_tx(ctx);
  2226. /* Schedule the transmission if tx list is ready */
  2227. if (tls_is_tx_ready(tx_ctx) &&
  2228. !test_and_set_bit(BIT_TX_SCHEDULED, &tx_ctx->tx_bitmask))
  2229. schedule_delayed_work(&tx_ctx->tx_work.work, 0);
  2230. }
  2231. void tls_sw_strparser_arm(struct sock *sk, struct tls_context *tls_ctx)
  2232. {
  2233. struct tls_sw_context_rx *rx_ctx = tls_sw_ctx_rx(tls_ctx);
  2234. write_lock_bh(&sk->sk_callback_lock);
  2235. rx_ctx->saved_data_ready = sk->sk_data_ready;
  2236. sk->sk_data_ready = tls_data_ready;
  2237. write_unlock_bh(&sk->sk_callback_lock);
  2238. }
  2239. void tls_update_rx_zc_capable(struct tls_context *tls_ctx)
  2240. {
  2241. struct tls_sw_context_rx *rx_ctx = tls_sw_ctx_rx(tls_ctx);
  2242. rx_ctx->zc_capable = tls_ctx->rx_no_pad ||
  2243. tls_ctx->prot_info.version != TLS_1_3_VERSION;
  2244. }
  2245. static struct tls_sw_context_tx *init_ctx_tx(struct tls_context *ctx, struct sock *sk)
  2246. {
  2247. struct tls_sw_context_tx *sw_ctx_tx;
  2248. if (!ctx->priv_ctx_tx) {
  2249. sw_ctx_tx = kzalloc(sizeof(*sw_ctx_tx), GFP_KERNEL);
  2250. if (!sw_ctx_tx)
  2251. return NULL;
  2252. } else {
  2253. sw_ctx_tx = ctx->priv_ctx_tx;
  2254. }
  2255. crypto_init_wait(&sw_ctx_tx->async_wait);
  2256. atomic_set(&sw_ctx_tx->encrypt_pending, 1);
  2257. INIT_LIST_HEAD(&sw_ctx_tx->tx_list);
  2258. INIT_DELAYED_WORK(&sw_ctx_tx->tx_work.work, tx_work_handler);
  2259. sw_ctx_tx->tx_work.sk = sk;
  2260. return sw_ctx_tx;
  2261. }
  2262. static struct tls_sw_context_rx *init_ctx_rx(struct tls_context *ctx)
  2263. {
  2264. struct tls_sw_context_rx *sw_ctx_rx;
  2265. if (!ctx->priv_ctx_rx) {
  2266. sw_ctx_rx = kzalloc(sizeof(*sw_ctx_rx), GFP_KERNEL);
  2267. if (!sw_ctx_rx)
  2268. return NULL;
  2269. } else {
  2270. sw_ctx_rx = ctx->priv_ctx_rx;
  2271. }
  2272. crypto_init_wait(&sw_ctx_rx->async_wait);
  2273. atomic_set(&sw_ctx_rx->decrypt_pending, 1);
  2274. init_waitqueue_head(&sw_ctx_rx->wq);
  2275. skb_queue_head_init(&sw_ctx_rx->rx_list);
  2276. skb_queue_head_init(&sw_ctx_rx->async_hold);
  2277. return sw_ctx_rx;
  2278. }
  2279. int init_prot_info(struct tls_prot_info *prot,
  2280. const struct tls_crypto_info *crypto_info,
  2281. const struct tls_cipher_desc *cipher_desc)
  2282. {
  2283. u16 nonce_size = cipher_desc->nonce;
  2284. if (crypto_info->version == TLS_1_3_VERSION) {
  2285. nonce_size = 0;
  2286. prot->aad_size = TLS_HEADER_SIZE;
  2287. prot->tail_size = 1;
  2288. } else {
  2289. prot->aad_size = TLS_AAD_SPACE_SIZE;
  2290. prot->tail_size = 0;
  2291. }
  2292. /* Sanity-check the sizes for stack allocations. */
  2293. if (nonce_size > TLS_MAX_IV_SIZE || prot->aad_size > TLS_MAX_AAD_SIZE)
  2294. return -EINVAL;
  2295. prot->version = crypto_info->version;
  2296. prot->cipher_type = crypto_info->cipher_type;
  2297. prot->prepend_size = TLS_HEADER_SIZE + nonce_size;
  2298. prot->tag_size = cipher_desc->tag;
  2299. prot->overhead_size = prot->prepend_size + prot->tag_size + prot->tail_size;
  2300. prot->iv_size = cipher_desc->iv;
  2301. prot->salt_size = cipher_desc->salt;
  2302. prot->rec_seq_size = cipher_desc->rec_seq;
  2303. return 0;
  2304. }
  2305. int tls_set_sw_offload(struct sock *sk, int tx)
  2306. {
  2307. struct tls_sw_context_tx *sw_ctx_tx = NULL;
  2308. struct tls_sw_context_rx *sw_ctx_rx = NULL;
  2309. const struct tls_cipher_desc *cipher_desc;
  2310. struct tls_crypto_info *crypto_info;
  2311. char *iv, *rec_seq, *key, *salt;
  2312. struct cipher_context *cctx;
  2313. struct tls_prot_info *prot;
  2314. struct crypto_aead **aead;
  2315. struct tls_context *ctx;
  2316. struct crypto_tfm *tfm;
  2317. int rc = 0;
  2318. ctx = tls_get_ctx(sk);
  2319. prot = &ctx->prot_info;
  2320. if (tx) {
  2321. ctx->priv_ctx_tx = init_ctx_tx(ctx, sk);
  2322. if (!ctx->priv_ctx_tx)
  2323. return -ENOMEM;
  2324. sw_ctx_tx = ctx->priv_ctx_tx;
  2325. crypto_info = &ctx->crypto_send.info;
  2326. cctx = &ctx->tx;
  2327. aead = &sw_ctx_tx->aead_send;
  2328. } else {
  2329. ctx->priv_ctx_rx = init_ctx_rx(ctx);
  2330. if (!ctx->priv_ctx_rx)
  2331. return -ENOMEM;
  2332. sw_ctx_rx = ctx->priv_ctx_rx;
  2333. crypto_info = &ctx->crypto_recv.info;
  2334. cctx = &ctx->rx;
  2335. aead = &sw_ctx_rx->aead_recv;
  2336. }
  2337. cipher_desc = get_cipher_desc(crypto_info->cipher_type);
  2338. if (!cipher_desc) {
  2339. rc = -EINVAL;
  2340. goto free_priv;
  2341. }
  2342. rc = init_prot_info(prot, crypto_info, cipher_desc);
  2343. if (rc)
  2344. goto free_priv;
  2345. iv = crypto_info_iv(crypto_info, cipher_desc);
  2346. key = crypto_info_key(crypto_info, cipher_desc);
  2347. salt = crypto_info_salt(crypto_info, cipher_desc);
  2348. rec_seq = crypto_info_rec_seq(crypto_info, cipher_desc);
  2349. memcpy(cctx->iv, salt, cipher_desc->salt);
  2350. memcpy(cctx->iv + cipher_desc->salt, iv, cipher_desc->iv);
  2351. memcpy(cctx->rec_seq, rec_seq, cipher_desc->rec_seq);
  2352. if (!*aead) {
  2353. *aead = crypto_alloc_aead(cipher_desc->cipher_name, 0, 0);
  2354. if (IS_ERR(*aead)) {
  2355. rc = PTR_ERR(*aead);
  2356. *aead = NULL;
  2357. goto free_priv;
  2358. }
  2359. }
  2360. ctx->push_pending_record = tls_sw_push_pending_record;
  2361. rc = crypto_aead_setkey(*aead, key, cipher_desc->key);
  2362. if (rc)
  2363. goto free_aead;
  2364. rc = crypto_aead_setauthsize(*aead, prot->tag_size);
  2365. if (rc)
  2366. goto free_aead;
  2367. if (sw_ctx_rx) {
  2368. tfm = crypto_aead_tfm(sw_ctx_rx->aead_recv);
  2369. tls_update_rx_zc_capable(ctx);
  2370. sw_ctx_rx->async_capable =
  2371. crypto_info->version != TLS_1_3_VERSION &&
  2372. !!(tfm->__crt_alg->cra_flags & CRYPTO_ALG_ASYNC);
  2373. rc = tls_strp_init(&sw_ctx_rx->strp, sk);
  2374. if (rc)
  2375. goto free_aead;
  2376. }
  2377. goto out;
  2378. free_aead:
  2379. crypto_free_aead(*aead);
  2380. *aead = 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. }