messenger_v2.c 92 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Ceph msgr2 protocol implementation
  4. *
  5. * Copyright (C) 2020 Ilya Dryomov <[email protected]>
  6. */
  7. #include <linux/ceph/ceph_debug.h>
  8. #include <crypto/aead.h>
  9. #include <crypto/algapi.h> /* for crypto_memneq() */
  10. #include <crypto/hash.h>
  11. #include <crypto/sha2.h>
  12. #include <linux/bvec.h>
  13. #include <linux/crc32c.h>
  14. #include <linux/net.h>
  15. #include <linux/scatterlist.h>
  16. #include <linux/socket.h>
  17. #include <linux/sched/mm.h>
  18. #include <net/sock.h>
  19. #include <net/tcp.h>
  20. #include <linux/ceph/ceph_features.h>
  21. #include <linux/ceph/decode.h>
  22. #include <linux/ceph/libceph.h>
  23. #include <linux/ceph/messenger.h>
  24. #include "crypto.h" /* for CEPH_KEY_LEN and CEPH_MAX_CON_SECRET_LEN */
  25. #define FRAME_TAG_HELLO 1
  26. #define FRAME_TAG_AUTH_REQUEST 2
  27. #define FRAME_TAG_AUTH_BAD_METHOD 3
  28. #define FRAME_TAG_AUTH_REPLY_MORE 4
  29. #define FRAME_TAG_AUTH_REQUEST_MORE 5
  30. #define FRAME_TAG_AUTH_DONE 6
  31. #define FRAME_TAG_AUTH_SIGNATURE 7
  32. #define FRAME_TAG_CLIENT_IDENT 8
  33. #define FRAME_TAG_SERVER_IDENT 9
  34. #define FRAME_TAG_IDENT_MISSING_FEATURES 10
  35. #define FRAME_TAG_SESSION_RECONNECT 11
  36. #define FRAME_TAG_SESSION_RESET 12
  37. #define FRAME_TAG_SESSION_RETRY 13
  38. #define FRAME_TAG_SESSION_RETRY_GLOBAL 14
  39. #define FRAME_TAG_SESSION_RECONNECT_OK 15
  40. #define FRAME_TAG_WAIT 16
  41. #define FRAME_TAG_MESSAGE 17
  42. #define FRAME_TAG_KEEPALIVE2 18
  43. #define FRAME_TAG_KEEPALIVE2_ACK 19
  44. #define FRAME_TAG_ACK 20
  45. #define FRAME_LATE_STATUS_ABORTED 0x1
  46. #define FRAME_LATE_STATUS_COMPLETE 0xe
  47. #define FRAME_LATE_STATUS_ABORTED_MASK 0xf
  48. #define IN_S_HANDLE_PREAMBLE 1
  49. #define IN_S_HANDLE_CONTROL 2
  50. #define IN_S_HANDLE_CONTROL_REMAINDER 3
  51. #define IN_S_PREPARE_READ_DATA 4
  52. #define IN_S_PREPARE_READ_DATA_CONT 5
  53. #define IN_S_PREPARE_READ_ENC_PAGE 6
  54. #define IN_S_HANDLE_EPILOGUE 7
  55. #define IN_S_FINISH_SKIP 8
  56. #define OUT_S_QUEUE_DATA 1
  57. #define OUT_S_QUEUE_DATA_CONT 2
  58. #define OUT_S_QUEUE_ENC_PAGE 3
  59. #define OUT_S_QUEUE_ZEROS 4
  60. #define OUT_S_FINISH_MESSAGE 5
  61. #define OUT_S_GET_NEXT 6
  62. #define CTRL_BODY(p) ((void *)(p) + CEPH_PREAMBLE_LEN)
  63. #define FRONT_PAD(p) ((void *)(p) + CEPH_EPILOGUE_SECURE_LEN)
  64. #define MIDDLE_PAD(p) (FRONT_PAD(p) + CEPH_GCM_BLOCK_LEN)
  65. #define DATA_PAD(p) (MIDDLE_PAD(p) + CEPH_GCM_BLOCK_LEN)
  66. #define CEPH_MSG_FLAGS (MSG_DONTWAIT | MSG_NOSIGNAL)
  67. static int do_recvmsg(struct socket *sock, struct iov_iter *it)
  68. {
  69. struct msghdr msg = { .msg_flags = CEPH_MSG_FLAGS };
  70. int ret;
  71. msg.msg_iter = *it;
  72. while (iov_iter_count(it)) {
  73. ret = sock_recvmsg(sock, &msg, msg.msg_flags);
  74. if (ret <= 0) {
  75. if (ret == -EAGAIN)
  76. ret = 0;
  77. return ret;
  78. }
  79. iov_iter_advance(it, ret);
  80. }
  81. WARN_ON(msg_data_left(&msg));
  82. return 1;
  83. }
  84. /*
  85. * Read as much as possible.
  86. *
  87. * Return:
  88. * 1 - done, nothing (else) to read
  89. * 0 - socket is empty, need to wait
  90. * <0 - error
  91. */
  92. static int ceph_tcp_recv(struct ceph_connection *con)
  93. {
  94. int ret;
  95. dout("%s con %p %s %zu\n", __func__, con,
  96. iov_iter_is_discard(&con->v2.in_iter) ? "discard" : "need",
  97. iov_iter_count(&con->v2.in_iter));
  98. ret = do_recvmsg(con->sock, &con->v2.in_iter);
  99. dout("%s con %p ret %d left %zu\n", __func__, con, ret,
  100. iov_iter_count(&con->v2.in_iter));
  101. return ret;
  102. }
  103. static int do_sendmsg(struct socket *sock, struct iov_iter *it)
  104. {
  105. struct msghdr msg = { .msg_flags = CEPH_MSG_FLAGS };
  106. int ret;
  107. msg.msg_iter = *it;
  108. while (iov_iter_count(it)) {
  109. ret = sock_sendmsg(sock, &msg);
  110. if (ret <= 0) {
  111. if (ret == -EAGAIN)
  112. ret = 0;
  113. return ret;
  114. }
  115. iov_iter_advance(it, ret);
  116. }
  117. WARN_ON(msg_data_left(&msg));
  118. return 1;
  119. }
  120. static int do_try_sendpage(struct socket *sock, struct iov_iter *it)
  121. {
  122. struct msghdr msg = { .msg_flags = CEPH_MSG_FLAGS };
  123. struct bio_vec bv;
  124. int ret;
  125. if (WARN_ON(!iov_iter_is_bvec(it)))
  126. return -EINVAL;
  127. while (iov_iter_count(it)) {
  128. /* iov_iter_iovec() for ITER_BVEC */
  129. bv.bv_page = it->bvec->bv_page;
  130. bv.bv_offset = it->bvec->bv_offset + it->iov_offset;
  131. bv.bv_len = min(iov_iter_count(it),
  132. it->bvec->bv_len - it->iov_offset);
  133. /*
  134. * sendpage cannot properly handle pages with
  135. * page_count == 0, we need to fall back to sendmsg if
  136. * that's the case.
  137. *
  138. * Same goes for slab pages: skb_can_coalesce() allows
  139. * coalescing neighboring slab objects into a single frag
  140. * which triggers one of hardened usercopy checks.
  141. */
  142. if (sendpage_ok(bv.bv_page)) {
  143. ret = sock->ops->sendpage(sock, bv.bv_page,
  144. bv.bv_offset, bv.bv_len,
  145. CEPH_MSG_FLAGS);
  146. } else {
  147. iov_iter_bvec(&msg.msg_iter, ITER_SOURCE, &bv, 1, bv.bv_len);
  148. ret = sock_sendmsg(sock, &msg);
  149. }
  150. if (ret <= 0) {
  151. if (ret == -EAGAIN)
  152. ret = 0;
  153. return ret;
  154. }
  155. iov_iter_advance(it, ret);
  156. }
  157. return 1;
  158. }
  159. /*
  160. * Write as much as possible. The socket is expected to be corked,
  161. * so we don't bother with MSG_MORE/MSG_SENDPAGE_NOTLAST here.
  162. *
  163. * Return:
  164. * 1 - done, nothing (else) to write
  165. * 0 - socket is full, need to wait
  166. * <0 - error
  167. */
  168. static int ceph_tcp_send(struct ceph_connection *con)
  169. {
  170. int ret;
  171. dout("%s con %p have %zu try_sendpage %d\n", __func__, con,
  172. iov_iter_count(&con->v2.out_iter), con->v2.out_iter_sendpage);
  173. if (con->v2.out_iter_sendpage)
  174. ret = do_try_sendpage(con->sock, &con->v2.out_iter);
  175. else
  176. ret = do_sendmsg(con->sock, &con->v2.out_iter);
  177. dout("%s con %p ret %d left %zu\n", __func__, con, ret,
  178. iov_iter_count(&con->v2.out_iter));
  179. return ret;
  180. }
  181. static void add_in_kvec(struct ceph_connection *con, void *buf, int len)
  182. {
  183. BUG_ON(con->v2.in_kvec_cnt >= ARRAY_SIZE(con->v2.in_kvecs));
  184. WARN_ON(!iov_iter_is_kvec(&con->v2.in_iter));
  185. con->v2.in_kvecs[con->v2.in_kvec_cnt].iov_base = buf;
  186. con->v2.in_kvecs[con->v2.in_kvec_cnt].iov_len = len;
  187. con->v2.in_kvec_cnt++;
  188. con->v2.in_iter.nr_segs++;
  189. con->v2.in_iter.count += len;
  190. }
  191. static void reset_in_kvecs(struct ceph_connection *con)
  192. {
  193. WARN_ON(iov_iter_count(&con->v2.in_iter));
  194. con->v2.in_kvec_cnt = 0;
  195. iov_iter_kvec(&con->v2.in_iter, ITER_DEST, con->v2.in_kvecs, 0, 0);
  196. }
  197. static void set_in_bvec(struct ceph_connection *con, const struct bio_vec *bv)
  198. {
  199. WARN_ON(iov_iter_count(&con->v2.in_iter));
  200. con->v2.in_bvec = *bv;
  201. iov_iter_bvec(&con->v2.in_iter, ITER_DEST, &con->v2.in_bvec, 1, bv->bv_len);
  202. }
  203. static void set_in_skip(struct ceph_connection *con, int len)
  204. {
  205. WARN_ON(iov_iter_count(&con->v2.in_iter));
  206. dout("%s con %p len %d\n", __func__, con, len);
  207. iov_iter_discard(&con->v2.in_iter, ITER_DEST, len);
  208. }
  209. static void add_out_kvec(struct ceph_connection *con, void *buf, int len)
  210. {
  211. BUG_ON(con->v2.out_kvec_cnt >= ARRAY_SIZE(con->v2.out_kvecs));
  212. WARN_ON(!iov_iter_is_kvec(&con->v2.out_iter));
  213. WARN_ON(con->v2.out_zero);
  214. con->v2.out_kvecs[con->v2.out_kvec_cnt].iov_base = buf;
  215. con->v2.out_kvecs[con->v2.out_kvec_cnt].iov_len = len;
  216. con->v2.out_kvec_cnt++;
  217. con->v2.out_iter.nr_segs++;
  218. con->v2.out_iter.count += len;
  219. }
  220. static void reset_out_kvecs(struct ceph_connection *con)
  221. {
  222. WARN_ON(iov_iter_count(&con->v2.out_iter));
  223. WARN_ON(con->v2.out_zero);
  224. con->v2.out_kvec_cnt = 0;
  225. iov_iter_kvec(&con->v2.out_iter, ITER_SOURCE, con->v2.out_kvecs, 0, 0);
  226. con->v2.out_iter_sendpage = false;
  227. }
  228. static void set_out_bvec(struct ceph_connection *con, const struct bio_vec *bv,
  229. bool zerocopy)
  230. {
  231. WARN_ON(iov_iter_count(&con->v2.out_iter));
  232. WARN_ON(con->v2.out_zero);
  233. con->v2.out_bvec = *bv;
  234. con->v2.out_iter_sendpage = zerocopy;
  235. iov_iter_bvec(&con->v2.out_iter, ITER_SOURCE, &con->v2.out_bvec, 1,
  236. con->v2.out_bvec.bv_len);
  237. }
  238. static void set_out_bvec_zero(struct ceph_connection *con)
  239. {
  240. WARN_ON(iov_iter_count(&con->v2.out_iter));
  241. WARN_ON(!con->v2.out_zero);
  242. con->v2.out_bvec.bv_page = ceph_zero_page;
  243. con->v2.out_bvec.bv_offset = 0;
  244. con->v2.out_bvec.bv_len = min(con->v2.out_zero, (int)PAGE_SIZE);
  245. con->v2.out_iter_sendpage = true;
  246. iov_iter_bvec(&con->v2.out_iter, ITER_SOURCE, &con->v2.out_bvec, 1,
  247. con->v2.out_bvec.bv_len);
  248. }
  249. static void out_zero_add(struct ceph_connection *con, int len)
  250. {
  251. dout("%s con %p len %d\n", __func__, con, len);
  252. con->v2.out_zero += len;
  253. }
  254. static void *alloc_conn_buf(struct ceph_connection *con, int len)
  255. {
  256. void *buf;
  257. dout("%s con %p len %d\n", __func__, con, len);
  258. if (WARN_ON(con->v2.conn_buf_cnt >= ARRAY_SIZE(con->v2.conn_bufs)))
  259. return NULL;
  260. buf = kvmalloc(len, GFP_NOIO);
  261. if (!buf)
  262. return NULL;
  263. con->v2.conn_bufs[con->v2.conn_buf_cnt++] = buf;
  264. return buf;
  265. }
  266. static void free_conn_bufs(struct ceph_connection *con)
  267. {
  268. while (con->v2.conn_buf_cnt)
  269. kvfree(con->v2.conn_bufs[--con->v2.conn_buf_cnt]);
  270. }
  271. static void add_in_sign_kvec(struct ceph_connection *con, void *buf, int len)
  272. {
  273. BUG_ON(con->v2.in_sign_kvec_cnt >= ARRAY_SIZE(con->v2.in_sign_kvecs));
  274. con->v2.in_sign_kvecs[con->v2.in_sign_kvec_cnt].iov_base = buf;
  275. con->v2.in_sign_kvecs[con->v2.in_sign_kvec_cnt].iov_len = len;
  276. con->v2.in_sign_kvec_cnt++;
  277. }
  278. static void clear_in_sign_kvecs(struct ceph_connection *con)
  279. {
  280. con->v2.in_sign_kvec_cnt = 0;
  281. }
  282. static void add_out_sign_kvec(struct ceph_connection *con, void *buf, int len)
  283. {
  284. BUG_ON(con->v2.out_sign_kvec_cnt >= ARRAY_SIZE(con->v2.out_sign_kvecs));
  285. con->v2.out_sign_kvecs[con->v2.out_sign_kvec_cnt].iov_base = buf;
  286. con->v2.out_sign_kvecs[con->v2.out_sign_kvec_cnt].iov_len = len;
  287. con->v2.out_sign_kvec_cnt++;
  288. }
  289. static void clear_out_sign_kvecs(struct ceph_connection *con)
  290. {
  291. con->v2.out_sign_kvec_cnt = 0;
  292. }
  293. static bool con_secure(struct ceph_connection *con)
  294. {
  295. return con->v2.con_mode == CEPH_CON_MODE_SECURE;
  296. }
  297. static int front_len(const struct ceph_msg *msg)
  298. {
  299. return le32_to_cpu(msg->hdr.front_len);
  300. }
  301. static int middle_len(const struct ceph_msg *msg)
  302. {
  303. return le32_to_cpu(msg->hdr.middle_len);
  304. }
  305. static int data_len(const struct ceph_msg *msg)
  306. {
  307. return le32_to_cpu(msg->hdr.data_len);
  308. }
  309. static bool need_padding(int len)
  310. {
  311. return !IS_ALIGNED(len, CEPH_GCM_BLOCK_LEN);
  312. }
  313. static int padded_len(int len)
  314. {
  315. return ALIGN(len, CEPH_GCM_BLOCK_LEN);
  316. }
  317. static int padding_len(int len)
  318. {
  319. return padded_len(len) - len;
  320. }
  321. /* preamble + control segment */
  322. static int head_onwire_len(int ctrl_len, bool secure)
  323. {
  324. int head_len;
  325. int rem_len;
  326. BUG_ON(ctrl_len < 0 || ctrl_len > CEPH_MSG_MAX_CONTROL_LEN);
  327. if (secure) {
  328. head_len = CEPH_PREAMBLE_SECURE_LEN;
  329. if (ctrl_len > CEPH_PREAMBLE_INLINE_LEN) {
  330. rem_len = ctrl_len - CEPH_PREAMBLE_INLINE_LEN;
  331. head_len += padded_len(rem_len) + CEPH_GCM_TAG_LEN;
  332. }
  333. } else {
  334. head_len = CEPH_PREAMBLE_PLAIN_LEN;
  335. if (ctrl_len)
  336. head_len += ctrl_len + CEPH_CRC_LEN;
  337. }
  338. return head_len;
  339. }
  340. /* front, middle and data segments + epilogue */
  341. static int __tail_onwire_len(int front_len, int middle_len, int data_len,
  342. bool secure)
  343. {
  344. BUG_ON(front_len < 0 || front_len > CEPH_MSG_MAX_FRONT_LEN ||
  345. middle_len < 0 || middle_len > CEPH_MSG_MAX_MIDDLE_LEN ||
  346. data_len < 0 || data_len > CEPH_MSG_MAX_DATA_LEN);
  347. if (!front_len && !middle_len && !data_len)
  348. return 0;
  349. if (!secure)
  350. return front_len + middle_len + data_len +
  351. CEPH_EPILOGUE_PLAIN_LEN;
  352. return padded_len(front_len) + padded_len(middle_len) +
  353. padded_len(data_len) + CEPH_EPILOGUE_SECURE_LEN;
  354. }
  355. static int tail_onwire_len(const struct ceph_msg *msg, bool secure)
  356. {
  357. return __tail_onwire_len(front_len(msg), middle_len(msg),
  358. data_len(msg), secure);
  359. }
  360. /* head_onwire_len(sizeof(struct ceph_msg_header2), false) */
  361. #define MESSAGE_HEAD_PLAIN_LEN (CEPH_PREAMBLE_PLAIN_LEN + \
  362. sizeof(struct ceph_msg_header2) + \
  363. CEPH_CRC_LEN)
  364. static const int frame_aligns[] = {
  365. sizeof(void *),
  366. sizeof(void *),
  367. sizeof(void *),
  368. PAGE_SIZE
  369. };
  370. /*
  371. * Discards trailing empty segments, unless there is just one segment.
  372. * A frame always has at least one (possibly empty) segment.
  373. */
  374. static int calc_segment_count(const int *lens, int len_cnt)
  375. {
  376. int i;
  377. for (i = len_cnt - 1; i >= 0; i--) {
  378. if (lens[i])
  379. return i + 1;
  380. }
  381. return 1;
  382. }
  383. static void init_frame_desc(struct ceph_frame_desc *desc, int tag,
  384. const int *lens, int len_cnt)
  385. {
  386. int i;
  387. memset(desc, 0, sizeof(*desc));
  388. desc->fd_tag = tag;
  389. desc->fd_seg_cnt = calc_segment_count(lens, len_cnt);
  390. BUG_ON(desc->fd_seg_cnt > CEPH_FRAME_MAX_SEGMENT_COUNT);
  391. for (i = 0; i < desc->fd_seg_cnt; i++) {
  392. desc->fd_lens[i] = lens[i];
  393. desc->fd_aligns[i] = frame_aligns[i];
  394. }
  395. }
  396. /*
  397. * Preamble crc covers everything up to itself (28 bytes) and
  398. * is calculated and verified irrespective of the connection mode
  399. * (i.e. even if the frame is encrypted).
  400. */
  401. static void encode_preamble(const struct ceph_frame_desc *desc, void *p)
  402. {
  403. void *crcp = p + CEPH_PREAMBLE_LEN - CEPH_CRC_LEN;
  404. void *start = p;
  405. int i;
  406. memset(p, 0, CEPH_PREAMBLE_LEN);
  407. ceph_encode_8(&p, desc->fd_tag);
  408. ceph_encode_8(&p, desc->fd_seg_cnt);
  409. for (i = 0; i < desc->fd_seg_cnt; i++) {
  410. ceph_encode_32(&p, desc->fd_lens[i]);
  411. ceph_encode_16(&p, desc->fd_aligns[i]);
  412. }
  413. put_unaligned_le32(crc32c(0, start, crcp - start), crcp);
  414. }
  415. static int decode_preamble(void *p, struct ceph_frame_desc *desc)
  416. {
  417. void *crcp = p + CEPH_PREAMBLE_LEN - CEPH_CRC_LEN;
  418. u32 crc, expected_crc;
  419. int i;
  420. crc = crc32c(0, p, crcp - p);
  421. expected_crc = get_unaligned_le32(crcp);
  422. if (crc != expected_crc) {
  423. pr_err("bad preamble crc, calculated %u, expected %u\n",
  424. crc, expected_crc);
  425. return -EBADMSG;
  426. }
  427. memset(desc, 0, sizeof(*desc));
  428. desc->fd_tag = ceph_decode_8(&p);
  429. desc->fd_seg_cnt = ceph_decode_8(&p);
  430. if (desc->fd_seg_cnt < 1 ||
  431. desc->fd_seg_cnt > CEPH_FRAME_MAX_SEGMENT_COUNT) {
  432. pr_err("bad segment count %d\n", desc->fd_seg_cnt);
  433. return -EINVAL;
  434. }
  435. for (i = 0; i < desc->fd_seg_cnt; i++) {
  436. desc->fd_lens[i] = ceph_decode_32(&p);
  437. desc->fd_aligns[i] = ceph_decode_16(&p);
  438. }
  439. if (desc->fd_lens[0] < 0 ||
  440. desc->fd_lens[0] > CEPH_MSG_MAX_CONTROL_LEN) {
  441. pr_err("bad control segment length %d\n", desc->fd_lens[0]);
  442. return -EINVAL;
  443. }
  444. if (desc->fd_lens[1] < 0 ||
  445. desc->fd_lens[1] > CEPH_MSG_MAX_FRONT_LEN) {
  446. pr_err("bad front segment length %d\n", desc->fd_lens[1]);
  447. return -EINVAL;
  448. }
  449. if (desc->fd_lens[2] < 0 ||
  450. desc->fd_lens[2] > CEPH_MSG_MAX_MIDDLE_LEN) {
  451. pr_err("bad middle segment length %d\n", desc->fd_lens[2]);
  452. return -EINVAL;
  453. }
  454. if (desc->fd_lens[3] < 0 ||
  455. desc->fd_lens[3] > CEPH_MSG_MAX_DATA_LEN) {
  456. pr_err("bad data segment length %d\n", desc->fd_lens[3]);
  457. return -EINVAL;
  458. }
  459. /*
  460. * This would fire for FRAME_TAG_WAIT (it has one empty
  461. * segment), but we should never get it as client.
  462. */
  463. if (!desc->fd_lens[desc->fd_seg_cnt - 1]) {
  464. pr_err("last segment empty, segment count %d\n",
  465. desc->fd_seg_cnt);
  466. return -EINVAL;
  467. }
  468. return 0;
  469. }
  470. static void encode_epilogue_plain(struct ceph_connection *con, bool aborted)
  471. {
  472. con->v2.out_epil.late_status = aborted ? FRAME_LATE_STATUS_ABORTED :
  473. FRAME_LATE_STATUS_COMPLETE;
  474. cpu_to_le32s(&con->v2.out_epil.front_crc);
  475. cpu_to_le32s(&con->v2.out_epil.middle_crc);
  476. cpu_to_le32s(&con->v2.out_epil.data_crc);
  477. }
  478. static void encode_epilogue_secure(struct ceph_connection *con, bool aborted)
  479. {
  480. memset(&con->v2.out_epil, 0, sizeof(con->v2.out_epil));
  481. con->v2.out_epil.late_status = aborted ? FRAME_LATE_STATUS_ABORTED :
  482. FRAME_LATE_STATUS_COMPLETE;
  483. }
  484. static int decode_epilogue(void *p, u32 *front_crc, u32 *middle_crc,
  485. u32 *data_crc)
  486. {
  487. u8 late_status;
  488. late_status = ceph_decode_8(&p);
  489. if ((late_status & FRAME_LATE_STATUS_ABORTED_MASK) !=
  490. FRAME_LATE_STATUS_COMPLETE) {
  491. /* we should never get an aborted message as client */
  492. pr_err("bad late_status 0x%x\n", late_status);
  493. return -EINVAL;
  494. }
  495. if (front_crc && middle_crc && data_crc) {
  496. *front_crc = ceph_decode_32(&p);
  497. *middle_crc = ceph_decode_32(&p);
  498. *data_crc = ceph_decode_32(&p);
  499. }
  500. return 0;
  501. }
  502. static void fill_header(struct ceph_msg_header *hdr,
  503. const struct ceph_msg_header2 *hdr2,
  504. int front_len, int middle_len, int data_len,
  505. const struct ceph_entity_name *peer_name)
  506. {
  507. hdr->seq = hdr2->seq;
  508. hdr->tid = hdr2->tid;
  509. hdr->type = hdr2->type;
  510. hdr->priority = hdr2->priority;
  511. hdr->version = hdr2->version;
  512. hdr->front_len = cpu_to_le32(front_len);
  513. hdr->middle_len = cpu_to_le32(middle_len);
  514. hdr->data_len = cpu_to_le32(data_len);
  515. hdr->data_off = hdr2->data_off;
  516. hdr->src = *peer_name;
  517. hdr->compat_version = hdr2->compat_version;
  518. hdr->reserved = 0;
  519. hdr->crc = 0;
  520. }
  521. static void fill_header2(struct ceph_msg_header2 *hdr2,
  522. const struct ceph_msg_header *hdr, u64 ack_seq)
  523. {
  524. hdr2->seq = hdr->seq;
  525. hdr2->tid = hdr->tid;
  526. hdr2->type = hdr->type;
  527. hdr2->priority = hdr->priority;
  528. hdr2->version = hdr->version;
  529. hdr2->data_pre_padding_len = 0;
  530. hdr2->data_off = hdr->data_off;
  531. hdr2->ack_seq = cpu_to_le64(ack_seq);
  532. hdr2->flags = 0;
  533. hdr2->compat_version = hdr->compat_version;
  534. hdr2->reserved = 0;
  535. }
  536. static int verify_control_crc(struct ceph_connection *con)
  537. {
  538. int ctrl_len = con->v2.in_desc.fd_lens[0];
  539. u32 crc, expected_crc;
  540. WARN_ON(con->v2.in_kvecs[0].iov_len != ctrl_len);
  541. WARN_ON(con->v2.in_kvecs[1].iov_len != CEPH_CRC_LEN);
  542. crc = crc32c(-1, con->v2.in_kvecs[0].iov_base, ctrl_len);
  543. expected_crc = get_unaligned_le32(con->v2.in_kvecs[1].iov_base);
  544. if (crc != expected_crc) {
  545. pr_err("bad control crc, calculated %u, expected %u\n",
  546. crc, expected_crc);
  547. return -EBADMSG;
  548. }
  549. return 0;
  550. }
  551. static int verify_epilogue_crcs(struct ceph_connection *con, u32 front_crc,
  552. u32 middle_crc, u32 data_crc)
  553. {
  554. if (front_len(con->in_msg)) {
  555. con->in_front_crc = crc32c(-1, con->in_msg->front.iov_base,
  556. front_len(con->in_msg));
  557. } else {
  558. WARN_ON(!middle_len(con->in_msg) && !data_len(con->in_msg));
  559. con->in_front_crc = -1;
  560. }
  561. if (middle_len(con->in_msg))
  562. con->in_middle_crc = crc32c(-1,
  563. con->in_msg->middle->vec.iov_base,
  564. middle_len(con->in_msg));
  565. else if (data_len(con->in_msg))
  566. con->in_middle_crc = -1;
  567. else
  568. con->in_middle_crc = 0;
  569. if (!data_len(con->in_msg))
  570. con->in_data_crc = 0;
  571. dout("%s con %p msg %p crcs %u %u %u\n", __func__, con, con->in_msg,
  572. con->in_front_crc, con->in_middle_crc, con->in_data_crc);
  573. if (con->in_front_crc != front_crc) {
  574. pr_err("bad front crc, calculated %u, expected %u\n",
  575. con->in_front_crc, front_crc);
  576. return -EBADMSG;
  577. }
  578. if (con->in_middle_crc != middle_crc) {
  579. pr_err("bad middle crc, calculated %u, expected %u\n",
  580. con->in_middle_crc, middle_crc);
  581. return -EBADMSG;
  582. }
  583. if (con->in_data_crc != data_crc) {
  584. pr_err("bad data crc, calculated %u, expected %u\n",
  585. con->in_data_crc, data_crc);
  586. return -EBADMSG;
  587. }
  588. return 0;
  589. }
  590. static int setup_crypto(struct ceph_connection *con,
  591. const u8 *session_key, int session_key_len,
  592. const u8 *con_secret, int con_secret_len)
  593. {
  594. unsigned int noio_flag;
  595. int ret;
  596. dout("%s con %p con_mode %d session_key_len %d con_secret_len %d\n",
  597. __func__, con, con->v2.con_mode, session_key_len, con_secret_len);
  598. WARN_ON(con->v2.hmac_tfm || con->v2.gcm_tfm || con->v2.gcm_req);
  599. if (con->v2.con_mode != CEPH_CON_MODE_CRC &&
  600. con->v2.con_mode != CEPH_CON_MODE_SECURE) {
  601. pr_err("bad con_mode %d\n", con->v2.con_mode);
  602. return -EINVAL;
  603. }
  604. if (!session_key_len) {
  605. WARN_ON(con->v2.con_mode != CEPH_CON_MODE_CRC);
  606. WARN_ON(con_secret_len);
  607. return 0; /* auth_none */
  608. }
  609. noio_flag = memalloc_noio_save();
  610. con->v2.hmac_tfm = crypto_alloc_shash("hmac(sha256)", 0, 0);
  611. memalloc_noio_restore(noio_flag);
  612. if (IS_ERR(con->v2.hmac_tfm)) {
  613. ret = PTR_ERR(con->v2.hmac_tfm);
  614. con->v2.hmac_tfm = NULL;
  615. pr_err("failed to allocate hmac tfm context: %d\n", ret);
  616. return ret;
  617. }
  618. WARN_ON((unsigned long)session_key &
  619. crypto_shash_alignmask(con->v2.hmac_tfm));
  620. ret = crypto_shash_setkey(con->v2.hmac_tfm, session_key,
  621. session_key_len);
  622. if (ret) {
  623. pr_err("failed to set hmac key: %d\n", ret);
  624. return ret;
  625. }
  626. if (con->v2.con_mode == CEPH_CON_MODE_CRC) {
  627. WARN_ON(con_secret_len);
  628. return 0; /* auth_x, plain mode */
  629. }
  630. if (con_secret_len < CEPH_GCM_KEY_LEN + 2 * CEPH_GCM_IV_LEN) {
  631. pr_err("con_secret too small %d\n", con_secret_len);
  632. return -EINVAL;
  633. }
  634. noio_flag = memalloc_noio_save();
  635. con->v2.gcm_tfm = crypto_alloc_aead("gcm(aes)", 0, 0);
  636. memalloc_noio_restore(noio_flag);
  637. if (IS_ERR(con->v2.gcm_tfm)) {
  638. ret = PTR_ERR(con->v2.gcm_tfm);
  639. con->v2.gcm_tfm = NULL;
  640. pr_err("failed to allocate gcm tfm context: %d\n", ret);
  641. return ret;
  642. }
  643. WARN_ON((unsigned long)con_secret &
  644. crypto_aead_alignmask(con->v2.gcm_tfm));
  645. ret = crypto_aead_setkey(con->v2.gcm_tfm, con_secret, CEPH_GCM_KEY_LEN);
  646. if (ret) {
  647. pr_err("failed to set gcm key: %d\n", ret);
  648. return ret;
  649. }
  650. WARN_ON(crypto_aead_ivsize(con->v2.gcm_tfm) != CEPH_GCM_IV_LEN);
  651. ret = crypto_aead_setauthsize(con->v2.gcm_tfm, CEPH_GCM_TAG_LEN);
  652. if (ret) {
  653. pr_err("failed to set gcm tag size: %d\n", ret);
  654. return ret;
  655. }
  656. con->v2.gcm_req = aead_request_alloc(con->v2.gcm_tfm, GFP_NOIO);
  657. if (!con->v2.gcm_req) {
  658. pr_err("failed to allocate gcm request\n");
  659. return -ENOMEM;
  660. }
  661. crypto_init_wait(&con->v2.gcm_wait);
  662. aead_request_set_callback(con->v2.gcm_req, CRYPTO_TFM_REQ_MAY_BACKLOG,
  663. crypto_req_done, &con->v2.gcm_wait);
  664. memcpy(&con->v2.in_gcm_nonce, con_secret + CEPH_GCM_KEY_LEN,
  665. CEPH_GCM_IV_LEN);
  666. memcpy(&con->v2.out_gcm_nonce,
  667. con_secret + CEPH_GCM_KEY_LEN + CEPH_GCM_IV_LEN,
  668. CEPH_GCM_IV_LEN);
  669. return 0; /* auth_x, secure mode */
  670. }
  671. static int hmac_sha256(struct ceph_connection *con, const struct kvec *kvecs,
  672. int kvec_cnt, u8 *hmac)
  673. {
  674. SHASH_DESC_ON_STACK(desc, con->v2.hmac_tfm); /* tfm arg is ignored */
  675. int ret;
  676. int i;
  677. dout("%s con %p hmac_tfm %p kvec_cnt %d\n", __func__, con,
  678. con->v2.hmac_tfm, kvec_cnt);
  679. if (!con->v2.hmac_tfm) {
  680. memset(hmac, 0, SHA256_DIGEST_SIZE);
  681. return 0; /* auth_none */
  682. }
  683. desc->tfm = con->v2.hmac_tfm;
  684. ret = crypto_shash_init(desc);
  685. if (ret)
  686. goto out;
  687. for (i = 0; i < kvec_cnt; i++) {
  688. WARN_ON((unsigned long)kvecs[i].iov_base &
  689. crypto_shash_alignmask(con->v2.hmac_tfm));
  690. ret = crypto_shash_update(desc, kvecs[i].iov_base,
  691. kvecs[i].iov_len);
  692. if (ret)
  693. goto out;
  694. }
  695. ret = crypto_shash_final(desc, hmac);
  696. out:
  697. shash_desc_zero(desc);
  698. return ret; /* auth_x, both plain and secure modes */
  699. }
  700. static void gcm_inc_nonce(struct ceph_gcm_nonce *nonce)
  701. {
  702. u64 counter;
  703. counter = le64_to_cpu(nonce->counter);
  704. nonce->counter = cpu_to_le64(counter + 1);
  705. }
  706. static int gcm_crypt(struct ceph_connection *con, bool encrypt,
  707. struct scatterlist *src, struct scatterlist *dst,
  708. int src_len)
  709. {
  710. struct ceph_gcm_nonce *nonce;
  711. int ret;
  712. nonce = encrypt ? &con->v2.out_gcm_nonce : &con->v2.in_gcm_nonce;
  713. aead_request_set_ad(con->v2.gcm_req, 0); /* no AAD */
  714. aead_request_set_crypt(con->v2.gcm_req, src, dst, src_len, (u8 *)nonce);
  715. ret = crypto_wait_req(encrypt ? crypto_aead_encrypt(con->v2.gcm_req) :
  716. crypto_aead_decrypt(con->v2.gcm_req),
  717. &con->v2.gcm_wait);
  718. if (ret)
  719. return ret;
  720. gcm_inc_nonce(nonce);
  721. return 0;
  722. }
  723. static void get_bvec_at(struct ceph_msg_data_cursor *cursor,
  724. struct bio_vec *bv)
  725. {
  726. struct page *page;
  727. size_t off, len;
  728. WARN_ON(!cursor->total_resid);
  729. /* skip zero-length data items */
  730. while (!cursor->resid)
  731. ceph_msg_data_advance(cursor, 0);
  732. /* get a piece of data, cursor isn't advanced */
  733. page = ceph_msg_data_next(cursor, &off, &len);
  734. bv->bv_page = page;
  735. bv->bv_offset = off;
  736. bv->bv_len = len;
  737. }
  738. static int calc_sg_cnt(void *buf, int buf_len)
  739. {
  740. int sg_cnt;
  741. if (!buf_len)
  742. return 0;
  743. sg_cnt = need_padding(buf_len) ? 1 : 0;
  744. if (is_vmalloc_addr(buf)) {
  745. WARN_ON(offset_in_page(buf));
  746. sg_cnt += PAGE_ALIGN(buf_len) >> PAGE_SHIFT;
  747. } else {
  748. sg_cnt++;
  749. }
  750. return sg_cnt;
  751. }
  752. static int calc_sg_cnt_cursor(struct ceph_msg_data_cursor *cursor)
  753. {
  754. int data_len = cursor->total_resid;
  755. struct bio_vec bv;
  756. int sg_cnt;
  757. if (!data_len)
  758. return 0;
  759. sg_cnt = need_padding(data_len) ? 1 : 0;
  760. do {
  761. get_bvec_at(cursor, &bv);
  762. sg_cnt++;
  763. ceph_msg_data_advance(cursor, bv.bv_len);
  764. } while (cursor->total_resid);
  765. return sg_cnt;
  766. }
  767. static void init_sgs(struct scatterlist **sg, void *buf, int buf_len, u8 *pad)
  768. {
  769. void *end = buf + buf_len;
  770. struct page *page;
  771. int len;
  772. void *p;
  773. if (!buf_len)
  774. return;
  775. if (is_vmalloc_addr(buf)) {
  776. p = buf;
  777. do {
  778. page = vmalloc_to_page(p);
  779. len = min_t(int, end - p, PAGE_SIZE);
  780. WARN_ON(!page || !len || offset_in_page(p));
  781. sg_set_page(*sg, page, len, 0);
  782. *sg = sg_next(*sg);
  783. p += len;
  784. } while (p != end);
  785. } else {
  786. sg_set_buf(*sg, buf, buf_len);
  787. *sg = sg_next(*sg);
  788. }
  789. if (need_padding(buf_len)) {
  790. sg_set_buf(*sg, pad, padding_len(buf_len));
  791. *sg = sg_next(*sg);
  792. }
  793. }
  794. static void init_sgs_cursor(struct scatterlist **sg,
  795. struct ceph_msg_data_cursor *cursor, u8 *pad)
  796. {
  797. int data_len = cursor->total_resid;
  798. struct bio_vec bv;
  799. if (!data_len)
  800. return;
  801. do {
  802. get_bvec_at(cursor, &bv);
  803. sg_set_page(*sg, bv.bv_page, bv.bv_len, bv.bv_offset);
  804. *sg = sg_next(*sg);
  805. ceph_msg_data_advance(cursor, bv.bv_len);
  806. } while (cursor->total_resid);
  807. if (need_padding(data_len)) {
  808. sg_set_buf(*sg, pad, padding_len(data_len));
  809. *sg = sg_next(*sg);
  810. }
  811. }
  812. static int setup_message_sgs(struct sg_table *sgt, struct ceph_msg *msg,
  813. u8 *front_pad, u8 *middle_pad, u8 *data_pad,
  814. void *epilogue, bool add_tag)
  815. {
  816. struct ceph_msg_data_cursor cursor;
  817. struct scatterlist *cur_sg;
  818. int sg_cnt;
  819. int ret;
  820. if (!front_len(msg) && !middle_len(msg) && !data_len(msg))
  821. return 0;
  822. sg_cnt = 1; /* epilogue + [auth tag] */
  823. if (front_len(msg))
  824. sg_cnt += calc_sg_cnt(msg->front.iov_base,
  825. front_len(msg));
  826. if (middle_len(msg))
  827. sg_cnt += calc_sg_cnt(msg->middle->vec.iov_base,
  828. middle_len(msg));
  829. if (data_len(msg)) {
  830. ceph_msg_data_cursor_init(&cursor, msg, data_len(msg));
  831. sg_cnt += calc_sg_cnt_cursor(&cursor);
  832. }
  833. ret = sg_alloc_table(sgt, sg_cnt, GFP_NOIO);
  834. if (ret)
  835. return ret;
  836. cur_sg = sgt->sgl;
  837. if (front_len(msg))
  838. init_sgs(&cur_sg, msg->front.iov_base, front_len(msg),
  839. front_pad);
  840. if (middle_len(msg))
  841. init_sgs(&cur_sg, msg->middle->vec.iov_base, middle_len(msg),
  842. middle_pad);
  843. if (data_len(msg)) {
  844. ceph_msg_data_cursor_init(&cursor, msg, data_len(msg));
  845. init_sgs_cursor(&cur_sg, &cursor, data_pad);
  846. }
  847. WARN_ON(!sg_is_last(cur_sg));
  848. sg_set_buf(cur_sg, epilogue,
  849. CEPH_GCM_BLOCK_LEN + (add_tag ? CEPH_GCM_TAG_LEN : 0));
  850. return 0;
  851. }
  852. static int decrypt_preamble(struct ceph_connection *con)
  853. {
  854. struct scatterlist sg;
  855. sg_init_one(&sg, con->v2.in_buf, CEPH_PREAMBLE_SECURE_LEN);
  856. return gcm_crypt(con, false, &sg, &sg, CEPH_PREAMBLE_SECURE_LEN);
  857. }
  858. static int decrypt_control_remainder(struct ceph_connection *con)
  859. {
  860. int ctrl_len = con->v2.in_desc.fd_lens[0];
  861. int rem_len = ctrl_len - CEPH_PREAMBLE_INLINE_LEN;
  862. int pt_len = padding_len(rem_len) + CEPH_GCM_TAG_LEN;
  863. struct scatterlist sgs[2];
  864. WARN_ON(con->v2.in_kvecs[0].iov_len != rem_len);
  865. WARN_ON(con->v2.in_kvecs[1].iov_len != pt_len);
  866. sg_init_table(sgs, 2);
  867. sg_set_buf(&sgs[0], con->v2.in_kvecs[0].iov_base, rem_len);
  868. sg_set_buf(&sgs[1], con->v2.in_buf, pt_len);
  869. return gcm_crypt(con, false, sgs, sgs,
  870. padded_len(rem_len) + CEPH_GCM_TAG_LEN);
  871. }
  872. static int decrypt_tail(struct ceph_connection *con)
  873. {
  874. struct sg_table enc_sgt = {};
  875. struct sg_table sgt = {};
  876. int tail_len;
  877. int ret;
  878. tail_len = tail_onwire_len(con->in_msg, true);
  879. ret = sg_alloc_table_from_pages(&enc_sgt, con->v2.in_enc_pages,
  880. con->v2.in_enc_page_cnt, 0, tail_len,
  881. GFP_NOIO);
  882. if (ret)
  883. goto out;
  884. ret = setup_message_sgs(&sgt, con->in_msg, FRONT_PAD(con->v2.in_buf),
  885. MIDDLE_PAD(con->v2.in_buf), DATA_PAD(con->v2.in_buf),
  886. con->v2.in_buf, true);
  887. if (ret)
  888. goto out;
  889. dout("%s con %p msg %p enc_page_cnt %d sg_cnt %d\n", __func__, con,
  890. con->in_msg, con->v2.in_enc_page_cnt, sgt.orig_nents);
  891. ret = gcm_crypt(con, false, enc_sgt.sgl, sgt.sgl, tail_len);
  892. if (ret)
  893. goto out;
  894. WARN_ON(!con->v2.in_enc_page_cnt);
  895. ceph_release_page_vector(con->v2.in_enc_pages,
  896. con->v2.in_enc_page_cnt);
  897. con->v2.in_enc_pages = NULL;
  898. con->v2.in_enc_page_cnt = 0;
  899. out:
  900. sg_free_table(&sgt);
  901. sg_free_table(&enc_sgt);
  902. return ret;
  903. }
  904. static int prepare_banner(struct ceph_connection *con)
  905. {
  906. int buf_len = CEPH_BANNER_V2_LEN + 2 + 8 + 8;
  907. void *buf, *p;
  908. buf = alloc_conn_buf(con, buf_len);
  909. if (!buf)
  910. return -ENOMEM;
  911. p = buf;
  912. ceph_encode_copy(&p, CEPH_BANNER_V2, CEPH_BANNER_V2_LEN);
  913. ceph_encode_16(&p, sizeof(u64) + sizeof(u64));
  914. ceph_encode_64(&p, CEPH_MSGR2_SUPPORTED_FEATURES);
  915. ceph_encode_64(&p, CEPH_MSGR2_REQUIRED_FEATURES);
  916. WARN_ON(p != buf + buf_len);
  917. add_out_kvec(con, buf, buf_len);
  918. add_out_sign_kvec(con, buf, buf_len);
  919. ceph_con_flag_set(con, CEPH_CON_F_WRITE_PENDING);
  920. return 0;
  921. }
  922. /*
  923. * base:
  924. * preamble
  925. * control body (ctrl_len bytes)
  926. * space for control crc
  927. *
  928. * extdata (optional):
  929. * control body (extdata_len bytes)
  930. *
  931. * Compute control crc and gather base and extdata into:
  932. *
  933. * preamble
  934. * control body (ctrl_len + extdata_len bytes)
  935. * control crc
  936. *
  937. * Preamble should already be encoded at the start of base.
  938. */
  939. static void prepare_head_plain(struct ceph_connection *con, void *base,
  940. int ctrl_len, void *extdata, int extdata_len,
  941. bool to_be_signed)
  942. {
  943. int base_len = CEPH_PREAMBLE_LEN + ctrl_len + CEPH_CRC_LEN;
  944. void *crcp = base + base_len - CEPH_CRC_LEN;
  945. u32 crc;
  946. crc = crc32c(-1, CTRL_BODY(base), ctrl_len);
  947. if (extdata_len)
  948. crc = crc32c(crc, extdata, extdata_len);
  949. put_unaligned_le32(crc, crcp);
  950. if (!extdata_len) {
  951. add_out_kvec(con, base, base_len);
  952. if (to_be_signed)
  953. add_out_sign_kvec(con, base, base_len);
  954. return;
  955. }
  956. add_out_kvec(con, base, crcp - base);
  957. add_out_kvec(con, extdata, extdata_len);
  958. add_out_kvec(con, crcp, CEPH_CRC_LEN);
  959. if (to_be_signed) {
  960. add_out_sign_kvec(con, base, crcp - base);
  961. add_out_sign_kvec(con, extdata, extdata_len);
  962. add_out_sign_kvec(con, crcp, CEPH_CRC_LEN);
  963. }
  964. }
  965. static int prepare_head_secure_small(struct ceph_connection *con,
  966. void *base, int ctrl_len)
  967. {
  968. struct scatterlist sg;
  969. int ret;
  970. /* inline buffer padding? */
  971. if (ctrl_len < CEPH_PREAMBLE_INLINE_LEN)
  972. memset(CTRL_BODY(base) + ctrl_len, 0,
  973. CEPH_PREAMBLE_INLINE_LEN - ctrl_len);
  974. sg_init_one(&sg, base, CEPH_PREAMBLE_SECURE_LEN);
  975. ret = gcm_crypt(con, true, &sg, &sg,
  976. CEPH_PREAMBLE_SECURE_LEN - CEPH_GCM_TAG_LEN);
  977. if (ret)
  978. return ret;
  979. add_out_kvec(con, base, CEPH_PREAMBLE_SECURE_LEN);
  980. return 0;
  981. }
  982. /*
  983. * base:
  984. * preamble
  985. * control body (ctrl_len bytes)
  986. * space for padding, if needed
  987. * space for control remainder auth tag
  988. * space for preamble auth tag
  989. *
  990. * Encrypt preamble and the inline portion, then encrypt the remainder
  991. * and gather into:
  992. *
  993. * preamble
  994. * control body (48 bytes)
  995. * preamble auth tag
  996. * control body (ctrl_len - 48 bytes)
  997. * zero padding, if needed
  998. * control remainder auth tag
  999. *
  1000. * Preamble should already be encoded at the start of base.
  1001. */
  1002. static int prepare_head_secure_big(struct ceph_connection *con,
  1003. void *base, int ctrl_len)
  1004. {
  1005. int rem_len = ctrl_len - CEPH_PREAMBLE_INLINE_LEN;
  1006. void *rem = CTRL_BODY(base) + CEPH_PREAMBLE_INLINE_LEN;
  1007. void *rem_tag = rem + padded_len(rem_len);
  1008. void *pmbl_tag = rem_tag + CEPH_GCM_TAG_LEN;
  1009. struct scatterlist sgs[2];
  1010. int ret;
  1011. sg_init_table(sgs, 2);
  1012. sg_set_buf(&sgs[0], base, rem - base);
  1013. sg_set_buf(&sgs[1], pmbl_tag, CEPH_GCM_TAG_LEN);
  1014. ret = gcm_crypt(con, true, sgs, sgs, rem - base);
  1015. if (ret)
  1016. return ret;
  1017. /* control remainder padding? */
  1018. if (need_padding(rem_len))
  1019. memset(rem + rem_len, 0, padding_len(rem_len));
  1020. sg_init_one(&sgs[0], rem, pmbl_tag - rem);
  1021. ret = gcm_crypt(con, true, sgs, sgs, rem_tag - rem);
  1022. if (ret)
  1023. return ret;
  1024. add_out_kvec(con, base, rem - base);
  1025. add_out_kvec(con, pmbl_tag, CEPH_GCM_TAG_LEN);
  1026. add_out_kvec(con, rem, pmbl_tag - rem);
  1027. return 0;
  1028. }
  1029. static int __prepare_control(struct ceph_connection *con, int tag,
  1030. void *base, int ctrl_len, void *extdata,
  1031. int extdata_len, bool to_be_signed)
  1032. {
  1033. int total_len = ctrl_len + extdata_len;
  1034. struct ceph_frame_desc desc;
  1035. int ret;
  1036. dout("%s con %p tag %d len %d (%d+%d)\n", __func__, con, tag,
  1037. total_len, ctrl_len, extdata_len);
  1038. /* extdata may be vmalloc'ed but not base */
  1039. if (WARN_ON(is_vmalloc_addr(base) || !ctrl_len))
  1040. return -EINVAL;
  1041. init_frame_desc(&desc, tag, &total_len, 1);
  1042. encode_preamble(&desc, base);
  1043. if (con_secure(con)) {
  1044. if (WARN_ON(extdata_len || to_be_signed))
  1045. return -EINVAL;
  1046. if (ctrl_len <= CEPH_PREAMBLE_INLINE_LEN)
  1047. /* fully inlined, inline buffer may need padding */
  1048. ret = prepare_head_secure_small(con, base, ctrl_len);
  1049. else
  1050. /* partially inlined, inline buffer is full */
  1051. ret = prepare_head_secure_big(con, base, ctrl_len);
  1052. if (ret)
  1053. return ret;
  1054. } else {
  1055. prepare_head_plain(con, base, ctrl_len, extdata, extdata_len,
  1056. to_be_signed);
  1057. }
  1058. ceph_con_flag_set(con, CEPH_CON_F_WRITE_PENDING);
  1059. return 0;
  1060. }
  1061. static int prepare_control(struct ceph_connection *con, int tag,
  1062. void *base, int ctrl_len)
  1063. {
  1064. return __prepare_control(con, tag, base, ctrl_len, NULL, 0, false);
  1065. }
  1066. static int prepare_hello(struct ceph_connection *con)
  1067. {
  1068. void *buf, *p;
  1069. int ctrl_len;
  1070. ctrl_len = 1 + ceph_entity_addr_encoding_len(&con->peer_addr);
  1071. buf = alloc_conn_buf(con, head_onwire_len(ctrl_len, false));
  1072. if (!buf)
  1073. return -ENOMEM;
  1074. p = CTRL_BODY(buf);
  1075. ceph_encode_8(&p, CEPH_ENTITY_TYPE_CLIENT);
  1076. ceph_encode_entity_addr(&p, &con->peer_addr);
  1077. WARN_ON(p != CTRL_BODY(buf) + ctrl_len);
  1078. return __prepare_control(con, FRAME_TAG_HELLO, buf, ctrl_len,
  1079. NULL, 0, true);
  1080. }
  1081. /* so that head_onwire_len(AUTH_BUF_LEN, false) is 512 */
  1082. #define AUTH_BUF_LEN (512 - CEPH_CRC_LEN - CEPH_PREAMBLE_PLAIN_LEN)
  1083. static int prepare_auth_request(struct ceph_connection *con)
  1084. {
  1085. void *authorizer, *authorizer_copy;
  1086. int ctrl_len, authorizer_len;
  1087. void *buf;
  1088. int ret;
  1089. ctrl_len = AUTH_BUF_LEN;
  1090. buf = alloc_conn_buf(con, head_onwire_len(ctrl_len, false));
  1091. if (!buf)
  1092. return -ENOMEM;
  1093. mutex_unlock(&con->mutex);
  1094. ret = con->ops->get_auth_request(con, CTRL_BODY(buf), &ctrl_len,
  1095. &authorizer, &authorizer_len);
  1096. mutex_lock(&con->mutex);
  1097. if (con->state != CEPH_CON_S_V2_HELLO) {
  1098. dout("%s con %p state changed to %d\n", __func__, con,
  1099. con->state);
  1100. return -EAGAIN;
  1101. }
  1102. dout("%s con %p get_auth_request ret %d\n", __func__, con, ret);
  1103. if (ret)
  1104. return ret;
  1105. authorizer_copy = alloc_conn_buf(con, authorizer_len);
  1106. if (!authorizer_copy)
  1107. return -ENOMEM;
  1108. memcpy(authorizer_copy, authorizer, authorizer_len);
  1109. return __prepare_control(con, FRAME_TAG_AUTH_REQUEST, buf, ctrl_len,
  1110. authorizer_copy, authorizer_len, true);
  1111. }
  1112. static int prepare_auth_request_more(struct ceph_connection *con,
  1113. void *reply, int reply_len)
  1114. {
  1115. int ctrl_len, authorizer_len;
  1116. void *authorizer;
  1117. void *buf;
  1118. int ret;
  1119. ctrl_len = AUTH_BUF_LEN;
  1120. buf = alloc_conn_buf(con, head_onwire_len(ctrl_len, false));
  1121. if (!buf)
  1122. return -ENOMEM;
  1123. mutex_unlock(&con->mutex);
  1124. ret = con->ops->handle_auth_reply_more(con, reply, reply_len,
  1125. CTRL_BODY(buf), &ctrl_len,
  1126. &authorizer, &authorizer_len);
  1127. mutex_lock(&con->mutex);
  1128. if (con->state != CEPH_CON_S_V2_AUTH) {
  1129. dout("%s con %p state changed to %d\n", __func__, con,
  1130. con->state);
  1131. return -EAGAIN;
  1132. }
  1133. dout("%s con %p handle_auth_reply_more ret %d\n", __func__, con, ret);
  1134. if (ret)
  1135. return ret;
  1136. return __prepare_control(con, FRAME_TAG_AUTH_REQUEST_MORE, buf,
  1137. ctrl_len, authorizer, authorizer_len, true);
  1138. }
  1139. static int prepare_auth_signature(struct ceph_connection *con)
  1140. {
  1141. void *buf;
  1142. int ret;
  1143. buf = alloc_conn_buf(con, head_onwire_len(SHA256_DIGEST_SIZE,
  1144. con_secure(con)));
  1145. if (!buf)
  1146. return -ENOMEM;
  1147. ret = hmac_sha256(con, con->v2.in_sign_kvecs, con->v2.in_sign_kvec_cnt,
  1148. CTRL_BODY(buf));
  1149. if (ret)
  1150. return ret;
  1151. return prepare_control(con, FRAME_TAG_AUTH_SIGNATURE, buf,
  1152. SHA256_DIGEST_SIZE);
  1153. }
  1154. static int prepare_client_ident(struct ceph_connection *con)
  1155. {
  1156. struct ceph_entity_addr *my_addr = &con->msgr->inst.addr;
  1157. struct ceph_client *client = from_msgr(con->msgr);
  1158. u64 global_id = ceph_client_gid(client);
  1159. void *buf, *p;
  1160. int ctrl_len;
  1161. WARN_ON(con->v2.server_cookie);
  1162. WARN_ON(con->v2.connect_seq);
  1163. WARN_ON(con->v2.peer_global_seq);
  1164. if (!con->v2.client_cookie) {
  1165. do {
  1166. get_random_bytes(&con->v2.client_cookie,
  1167. sizeof(con->v2.client_cookie));
  1168. } while (!con->v2.client_cookie);
  1169. dout("%s con %p generated cookie 0x%llx\n", __func__, con,
  1170. con->v2.client_cookie);
  1171. } else {
  1172. dout("%s con %p cookie already set 0x%llx\n", __func__, con,
  1173. con->v2.client_cookie);
  1174. }
  1175. dout("%s con %p my_addr %s/%u peer_addr %s/%u global_id %llu global_seq %llu features 0x%llx required_features 0x%llx cookie 0x%llx\n",
  1176. __func__, con, ceph_pr_addr(my_addr), le32_to_cpu(my_addr->nonce),
  1177. ceph_pr_addr(&con->peer_addr), le32_to_cpu(con->peer_addr.nonce),
  1178. global_id, con->v2.global_seq, client->supported_features,
  1179. client->required_features, con->v2.client_cookie);
  1180. ctrl_len = 1 + 4 + ceph_entity_addr_encoding_len(my_addr) +
  1181. ceph_entity_addr_encoding_len(&con->peer_addr) + 6 * 8;
  1182. buf = alloc_conn_buf(con, head_onwire_len(ctrl_len, con_secure(con)));
  1183. if (!buf)
  1184. return -ENOMEM;
  1185. p = CTRL_BODY(buf);
  1186. ceph_encode_8(&p, 2); /* addrvec marker */
  1187. ceph_encode_32(&p, 1); /* addr_cnt */
  1188. ceph_encode_entity_addr(&p, my_addr);
  1189. ceph_encode_entity_addr(&p, &con->peer_addr);
  1190. ceph_encode_64(&p, global_id);
  1191. ceph_encode_64(&p, con->v2.global_seq);
  1192. ceph_encode_64(&p, client->supported_features);
  1193. ceph_encode_64(&p, client->required_features);
  1194. ceph_encode_64(&p, 0); /* flags */
  1195. ceph_encode_64(&p, con->v2.client_cookie);
  1196. WARN_ON(p != CTRL_BODY(buf) + ctrl_len);
  1197. return prepare_control(con, FRAME_TAG_CLIENT_IDENT, buf, ctrl_len);
  1198. }
  1199. static int prepare_session_reconnect(struct ceph_connection *con)
  1200. {
  1201. struct ceph_entity_addr *my_addr = &con->msgr->inst.addr;
  1202. void *buf, *p;
  1203. int ctrl_len;
  1204. WARN_ON(!con->v2.client_cookie);
  1205. WARN_ON(!con->v2.server_cookie);
  1206. WARN_ON(!con->v2.connect_seq);
  1207. WARN_ON(!con->v2.peer_global_seq);
  1208. dout("%s con %p my_addr %s/%u client_cookie 0x%llx server_cookie 0x%llx global_seq %llu connect_seq %llu in_seq %llu\n",
  1209. __func__, con, ceph_pr_addr(my_addr), le32_to_cpu(my_addr->nonce),
  1210. con->v2.client_cookie, con->v2.server_cookie, con->v2.global_seq,
  1211. con->v2.connect_seq, con->in_seq);
  1212. ctrl_len = 1 + 4 + ceph_entity_addr_encoding_len(my_addr) + 5 * 8;
  1213. buf = alloc_conn_buf(con, head_onwire_len(ctrl_len, con_secure(con)));
  1214. if (!buf)
  1215. return -ENOMEM;
  1216. p = CTRL_BODY(buf);
  1217. ceph_encode_8(&p, 2); /* entity_addrvec_t marker */
  1218. ceph_encode_32(&p, 1); /* my_addrs len */
  1219. ceph_encode_entity_addr(&p, my_addr);
  1220. ceph_encode_64(&p, con->v2.client_cookie);
  1221. ceph_encode_64(&p, con->v2.server_cookie);
  1222. ceph_encode_64(&p, con->v2.global_seq);
  1223. ceph_encode_64(&p, con->v2.connect_seq);
  1224. ceph_encode_64(&p, con->in_seq);
  1225. WARN_ON(p != CTRL_BODY(buf) + ctrl_len);
  1226. return prepare_control(con, FRAME_TAG_SESSION_RECONNECT, buf, ctrl_len);
  1227. }
  1228. static int prepare_keepalive2(struct ceph_connection *con)
  1229. {
  1230. struct ceph_timespec *ts = CTRL_BODY(con->v2.out_buf);
  1231. struct timespec64 now;
  1232. ktime_get_real_ts64(&now);
  1233. dout("%s con %p timestamp %lld.%09ld\n", __func__, con, now.tv_sec,
  1234. now.tv_nsec);
  1235. ceph_encode_timespec64(ts, &now);
  1236. reset_out_kvecs(con);
  1237. return prepare_control(con, FRAME_TAG_KEEPALIVE2, con->v2.out_buf,
  1238. sizeof(struct ceph_timespec));
  1239. }
  1240. static int prepare_ack(struct ceph_connection *con)
  1241. {
  1242. void *p;
  1243. dout("%s con %p in_seq_acked %llu -> %llu\n", __func__, con,
  1244. con->in_seq_acked, con->in_seq);
  1245. con->in_seq_acked = con->in_seq;
  1246. p = CTRL_BODY(con->v2.out_buf);
  1247. ceph_encode_64(&p, con->in_seq_acked);
  1248. reset_out_kvecs(con);
  1249. return prepare_control(con, FRAME_TAG_ACK, con->v2.out_buf, 8);
  1250. }
  1251. static void prepare_epilogue_plain(struct ceph_connection *con, bool aborted)
  1252. {
  1253. dout("%s con %p msg %p aborted %d crcs %u %u %u\n", __func__, con,
  1254. con->out_msg, aborted, con->v2.out_epil.front_crc,
  1255. con->v2.out_epil.middle_crc, con->v2.out_epil.data_crc);
  1256. encode_epilogue_plain(con, aborted);
  1257. add_out_kvec(con, &con->v2.out_epil, CEPH_EPILOGUE_PLAIN_LEN);
  1258. }
  1259. /*
  1260. * For "used" empty segments, crc is -1. For unused (trailing)
  1261. * segments, crc is 0.
  1262. */
  1263. static void prepare_message_plain(struct ceph_connection *con)
  1264. {
  1265. struct ceph_msg *msg = con->out_msg;
  1266. prepare_head_plain(con, con->v2.out_buf,
  1267. sizeof(struct ceph_msg_header2), NULL, 0, false);
  1268. if (!front_len(msg) && !middle_len(msg)) {
  1269. if (!data_len(msg)) {
  1270. /*
  1271. * Empty message: once the head is written,
  1272. * we are done -- there is no epilogue.
  1273. */
  1274. con->v2.out_state = OUT_S_FINISH_MESSAGE;
  1275. return;
  1276. }
  1277. con->v2.out_epil.front_crc = -1;
  1278. con->v2.out_epil.middle_crc = -1;
  1279. con->v2.out_state = OUT_S_QUEUE_DATA;
  1280. return;
  1281. }
  1282. if (front_len(msg)) {
  1283. con->v2.out_epil.front_crc = crc32c(-1, msg->front.iov_base,
  1284. front_len(msg));
  1285. add_out_kvec(con, msg->front.iov_base, front_len(msg));
  1286. } else {
  1287. /* middle (at least) is there, checked above */
  1288. con->v2.out_epil.front_crc = -1;
  1289. }
  1290. if (middle_len(msg)) {
  1291. con->v2.out_epil.middle_crc =
  1292. crc32c(-1, msg->middle->vec.iov_base, middle_len(msg));
  1293. add_out_kvec(con, msg->middle->vec.iov_base, middle_len(msg));
  1294. } else {
  1295. con->v2.out_epil.middle_crc = data_len(msg) ? -1 : 0;
  1296. }
  1297. if (data_len(msg)) {
  1298. con->v2.out_state = OUT_S_QUEUE_DATA;
  1299. } else {
  1300. con->v2.out_epil.data_crc = 0;
  1301. prepare_epilogue_plain(con, false);
  1302. con->v2.out_state = OUT_S_FINISH_MESSAGE;
  1303. }
  1304. }
  1305. /*
  1306. * Unfortunately the kernel crypto API doesn't support streaming
  1307. * (piecewise) operation for AEAD algorithms, so we can't get away
  1308. * with a fixed size buffer and a couple sgs. Instead, we have to
  1309. * allocate pages for the entire tail of the message (currently up
  1310. * to ~32M) and two sgs arrays (up to ~256K each)...
  1311. */
  1312. static int prepare_message_secure(struct ceph_connection *con)
  1313. {
  1314. void *zerop = page_address(ceph_zero_page);
  1315. struct sg_table enc_sgt = {};
  1316. struct sg_table sgt = {};
  1317. struct page **enc_pages;
  1318. int enc_page_cnt;
  1319. int tail_len;
  1320. int ret;
  1321. ret = prepare_head_secure_small(con, con->v2.out_buf,
  1322. sizeof(struct ceph_msg_header2));
  1323. if (ret)
  1324. return ret;
  1325. tail_len = tail_onwire_len(con->out_msg, true);
  1326. if (!tail_len) {
  1327. /*
  1328. * Empty message: once the head is written,
  1329. * we are done -- there is no epilogue.
  1330. */
  1331. con->v2.out_state = OUT_S_FINISH_MESSAGE;
  1332. return 0;
  1333. }
  1334. encode_epilogue_secure(con, false);
  1335. ret = setup_message_sgs(&sgt, con->out_msg, zerop, zerop, zerop,
  1336. &con->v2.out_epil, false);
  1337. if (ret)
  1338. goto out;
  1339. enc_page_cnt = calc_pages_for(0, tail_len);
  1340. enc_pages = ceph_alloc_page_vector(enc_page_cnt, GFP_NOIO);
  1341. if (IS_ERR(enc_pages)) {
  1342. ret = PTR_ERR(enc_pages);
  1343. goto out;
  1344. }
  1345. WARN_ON(con->v2.out_enc_pages || con->v2.out_enc_page_cnt);
  1346. con->v2.out_enc_pages = enc_pages;
  1347. con->v2.out_enc_page_cnt = enc_page_cnt;
  1348. con->v2.out_enc_resid = tail_len;
  1349. con->v2.out_enc_i = 0;
  1350. ret = sg_alloc_table_from_pages(&enc_sgt, enc_pages, enc_page_cnt,
  1351. 0, tail_len, GFP_NOIO);
  1352. if (ret)
  1353. goto out;
  1354. ret = gcm_crypt(con, true, sgt.sgl, enc_sgt.sgl,
  1355. tail_len - CEPH_GCM_TAG_LEN);
  1356. if (ret)
  1357. goto out;
  1358. dout("%s con %p msg %p sg_cnt %d enc_page_cnt %d\n", __func__, con,
  1359. con->out_msg, sgt.orig_nents, enc_page_cnt);
  1360. con->v2.out_state = OUT_S_QUEUE_ENC_PAGE;
  1361. out:
  1362. sg_free_table(&sgt);
  1363. sg_free_table(&enc_sgt);
  1364. return ret;
  1365. }
  1366. static int prepare_message(struct ceph_connection *con)
  1367. {
  1368. int lens[] = {
  1369. sizeof(struct ceph_msg_header2),
  1370. front_len(con->out_msg),
  1371. middle_len(con->out_msg),
  1372. data_len(con->out_msg)
  1373. };
  1374. struct ceph_frame_desc desc;
  1375. int ret;
  1376. dout("%s con %p msg %p logical %d+%d+%d+%d\n", __func__, con,
  1377. con->out_msg, lens[0], lens[1], lens[2], lens[3]);
  1378. if (con->in_seq > con->in_seq_acked) {
  1379. dout("%s con %p in_seq_acked %llu -> %llu\n", __func__, con,
  1380. con->in_seq_acked, con->in_seq);
  1381. con->in_seq_acked = con->in_seq;
  1382. }
  1383. reset_out_kvecs(con);
  1384. init_frame_desc(&desc, FRAME_TAG_MESSAGE, lens, 4);
  1385. encode_preamble(&desc, con->v2.out_buf);
  1386. fill_header2(CTRL_BODY(con->v2.out_buf), &con->out_msg->hdr,
  1387. con->in_seq_acked);
  1388. if (con_secure(con)) {
  1389. ret = prepare_message_secure(con);
  1390. if (ret)
  1391. return ret;
  1392. } else {
  1393. prepare_message_plain(con);
  1394. }
  1395. ceph_con_flag_set(con, CEPH_CON_F_WRITE_PENDING);
  1396. return 0;
  1397. }
  1398. static int prepare_read_banner_prefix(struct ceph_connection *con)
  1399. {
  1400. void *buf;
  1401. buf = alloc_conn_buf(con, CEPH_BANNER_V2_PREFIX_LEN);
  1402. if (!buf)
  1403. return -ENOMEM;
  1404. reset_in_kvecs(con);
  1405. add_in_kvec(con, buf, CEPH_BANNER_V2_PREFIX_LEN);
  1406. add_in_sign_kvec(con, buf, CEPH_BANNER_V2_PREFIX_LEN);
  1407. con->state = CEPH_CON_S_V2_BANNER_PREFIX;
  1408. return 0;
  1409. }
  1410. static int prepare_read_banner_payload(struct ceph_connection *con,
  1411. int payload_len)
  1412. {
  1413. void *buf;
  1414. buf = alloc_conn_buf(con, payload_len);
  1415. if (!buf)
  1416. return -ENOMEM;
  1417. reset_in_kvecs(con);
  1418. add_in_kvec(con, buf, payload_len);
  1419. add_in_sign_kvec(con, buf, payload_len);
  1420. con->state = CEPH_CON_S_V2_BANNER_PAYLOAD;
  1421. return 0;
  1422. }
  1423. static void prepare_read_preamble(struct ceph_connection *con)
  1424. {
  1425. reset_in_kvecs(con);
  1426. add_in_kvec(con, con->v2.in_buf,
  1427. con_secure(con) ? CEPH_PREAMBLE_SECURE_LEN :
  1428. CEPH_PREAMBLE_PLAIN_LEN);
  1429. con->v2.in_state = IN_S_HANDLE_PREAMBLE;
  1430. }
  1431. static int prepare_read_control(struct ceph_connection *con)
  1432. {
  1433. int ctrl_len = con->v2.in_desc.fd_lens[0];
  1434. int head_len;
  1435. void *buf;
  1436. reset_in_kvecs(con);
  1437. if (con->state == CEPH_CON_S_V2_HELLO ||
  1438. con->state == CEPH_CON_S_V2_AUTH) {
  1439. head_len = head_onwire_len(ctrl_len, false);
  1440. buf = alloc_conn_buf(con, head_len);
  1441. if (!buf)
  1442. return -ENOMEM;
  1443. /* preserve preamble */
  1444. memcpy(buf, con->v2.in_buf, CEPH_PREAMBLE_LEN);
  1445. add_in_kvec(con, CTRL_BODY(buf), ctrl_len);
  1446. add_in_kvec(con, CTRL_BODY(buf) + ctrl_len, CEPH_CRC_LEN);
  1447. add_in_sign_kvec(con, buf, head_len);
  1448. } else {
  1449. if (ctrl_len > CEPH_PREAMBLE_INLINE_LEN) {
  1450. buf = alloc_conn_buf(con, ctrl_len);
  1451. if (!buf)
  1452. return -ENOMEM;
  1453. add_in_kvec(con, buf, ctrl_len);
  1454. } else {
  1455. add_in_kvec(con, CTRL_BODY(con->v2.in_buf), ctrl_len);
  1456. }
  1457. add_in_kvec(con, con->v2.in_buf, CEPH_CRC_LEN);
  1458. }
  1459. con->v2.in_state = IN_S_HANDLE_CONTROL;
  1460. return 0;
  1461. }
  1462. static int prepare_read_control_remainder(struct ceph_connection *con)
  1463. {
  1464. int ctrl_len = con->v2.in_desc.fd_lens[0];
  1465. int rem_len = ctrl_len - CEPH_PREAMBLE_INLINE_LEN;
  1466. void *buf;
  1467. buf = alloc_conn_buf(con, ctrl_len);
  1468. if (!buf)
  1469. return -ENOMEM;
  1470. memcpy(buf, CTRL_BODY(con->v2.in_buf), CEPH_PREAMBLE_INLINE_LEN);
  1471. reset_in_kvecs(con);
  1472. add_in_kvec(con, buf + CEPH_PREAMBLE_INLINE_LEN, rem_len);
  1473. add_in_kvec(con, con->v2.in_buf,
  1474. padding_len(rem_len) + CEPH_GCM_TAG_LEN);
  1475. con->v2.in_state = IN_S_HANDLE_CONTROL_REMAINDER;
  1476. return 0;
  1477. }
  1478. static int prepare_read_data(struct ceph_connection *con)
  1479. {
  1480. struct bio_vec bv;
  1481. con->in_data_crc = -1;
  1482. ceph_msg_data_cursor_init(&con->v2.in_cursor, con->in_msg,
  1483. data_len(con->in_msg));
  1484. get_bvec_at(&con->v2.in_cursor, &bv);
  1485. if (ceph_test_opt(from_msgr(con->msgr), RXBOUNCE)) {
  1486. if (unlikely(!con->bounce_page)) {
  1487. con->bounce_page = alloc_page(GFP_NOIO);
  1488. if (!con->bounce_page) {
  1489. pr_err("failed to allocate bounce page\n");
  1490. return -ENOMEM;
  1491. }
  1492. }
  1493. bv.bv_page = con->bounce_page;
  1494. bv.bv_offset = 0;
  1495. }
  1496. set_in_bvec(con, &bv);
  1497. con->v2.in_state = IN_S_PREPARE_READ_DATA_CONT;
  1498. return 0;
  1499. }
  1500. static void prepare_read_data_cont(struct ceph_connection *con)
  1501. {
  1502. struct bio_vec bv;
  1503. if (ceph_test_opt(from_msgr(con->msgr), RXBOUNCE)) {
  1504. con->in_data_crc = crc32c(con->in_data_crc,
  1505. page_address(con->bounce_page),
  1506. con->v2.in_bvec.bv_len);
  1507. get_bvec_at(&con->v2.in_cursor, &bv);
  1508. memcpy_to_page(bv.bv_page, bv.bv_offset,
  1509. page_address(con->bounce_page),
  1510. con->v2.in_bvec.bv_len);
  1511. } else {
  1512. con->in_data_crc = ceph_crc32c_page(con->in_data_crc,
  1513. con->v2.in_bvec.bv_page,
  1514. con->v2.in_bvec.bv_offset,
  1515. con->v2.in_bvec.bv_len);
  1516. }
  1517. ceph_msg_data_advance(&con->v2.in_cursor, con->v2.in_bvec.bv_len);
  1518. if (con->v2.in_cursor.total_resid) {
  1519. get_bvec_at(&con->v2.in_cursor, &bv);
  1520. if (ceph_test_opt(from_msgr(con->msgr), RXBOUNCE)) {
  1521. bv.bv_page = con->bounce_page;
  1522. bv.bv_offset = 0;
  1523. }
  1524. set_in_bvec(con, &bv);
  1525. WARN_ON(con->v2.in_state != IN_S_PREPARE_READ_DATA_CONT);
  1526. return;
  1527. }
  1528. /*
  1529. * We've read all data. Prepare to read epilogue.
  1530. */
  1531. reset_in_kvecs(con);
  1532. add_in_kvec(con, con->v2.in_buf, CEPH_EPILOGUE_PLAIN_LEN);
  1533. con->v2.in_state = IN_S_HANDLE_EPILOGUE;
  1534. }
  1535. static int prepare_read_tail_plain(struct ceph_connection *con)
  1536. {
  1537. struct ceph_msg *msg = con->in_msg;
  1538. if (!front_len(msg) && !middle_len(msg)) {
  1539. WARN_ON(!data_len(msg));
  1540. return prepare_read_data(con);
  1541. }
  1542. reset_in_kvecs(con);
  1543. if (front_len(msg)) {
  1544. add_in_kvec(con, msg->front.iov_base, front_len(msg));
  1545. WARN_ON(msg->front.iov_len != front_len(msg));
  1546. }
  1547. if (middle_len(msg)) {
  1548. add_in_kvec(con, msg->middle->vec.iov_base, middle_len(msg));
  1549. WARN_ON(msg->middle->vec.iov_len != middle_len(msg));
  1550. }
  1551. if (data_len(msg)) {
  1552. con->v2.in_state = IN_S_PREPARE_READ_DATA;
  1553. } else {
  1554. add_in_kvec(con, con->v2.in_buf, CEPH_EPILOGUE_PLAIN_LEN);
  1555. con->v2.in_state = IN_S_HANDLE_EPILOGUE;
  1556. }
  1557. return 0;
  1558. }
  1559. static void prepare_read_enc_page(struct ceph_connection *con)
  1560. {
  1561. struct bio_vec bv;
  1562. dout("%s con %p i %d resid %d\n", __func__, con, con->v2.in_enc_i,
  1563. con->v2.in_enc_resid);
  1564. WARN_ON(!con->v2.in_enc_resid);
  1565. bv.bv_page = con->v2.in_enc_pages[con->v2.in_enc_i];
  1566. bv.bv_offset = 0;
  1567. bv.bv_len = min(con->v2.in_enc_resid, (int)PAGE_SIZE);
  1568. set_in_bvec(con, &bv);
  1569. con->v2.in_enc_i++;
  1570. con->v2.in_enc_resid -= bv.bv_len;
  1571. if (con->v2.in_enc_resid) {
  1572. con->v2.in_state = IN_S_PREPARE_READ_ENC_PAGE;
  1573. return;
  1574. }
  1575. /*
  1576. * We are set to read the last piece of ciphertext (ending
  1577. * with epilogue) + auth tag.
  1578. */
  1579. WARN_ON(con->v2.in_enc_i != con->v2.in_enc_page_cnt);
  1580. con->v2.in_state = IN_S_HANDLE_EPILOGUE;
  1581. }
  1582. static int prepare_read_tail_secure(struct ceph_connection *con)
  1583. {
  1584. struct page **enc_pages;
  1585. int enc_page_cnt;
  1586. int tail_len;
  1587. tail_len = tail_onwire_len(con->in_msg, true);
  1588. WARN_ON(!tail_len);
  1589. enc_page_cnt = calc_pages_for(0, tail_len);
  1590. enc_pages = ceph_alloc_page_vector(enc_page_cnt, GFP_NOIO);
  1591. if (IS_ERR(enc_pages))
  1592. return PTR_ERR(enc_pages);
  1593. WARN_ON(con->v2.in_enc_pages || con->v2.in_enc_page_cnt);
  1594. con->v2.in_enc_pages = enc_pages;
  1595. con->v2.in_enc_page_cnt = enc_page_cnt;
  1596. con->v2.in_enc_resid = tail_len;
  1597. con->v2.in_enc_i = 0;
  1598. prepare_read_enc_page(con);
  1599. return 0;
  1600. }
  1601. static void __finish_skip(struct ceph_connection *con)
  1602. {
  1603. con->in_seq++;
  1604. prepare_read_preamble(con);
  1605. }
  1606. static void prepare_skip_message(struct ceph_connection *con)
  1607. {
  1608. struct ceph_frame_desc *desc = &con->v2.in_desc;
  1609. int tail_len;
  1610. dout("%s con %p %d+%d+%d\n", __func__, con, desc->fd_lens[1],
  1611. desc->fd_lens[2], desc->fd_lens[3]);
  1612. tail_len = __tail_onwire_len(desc->fd_lens[1], desc->fd_lens[2],
  1613. desc->fd_lens[3], con_secure(con));
  1614. if (!tail_len) {
  1615. __finish_skip(con);
  1616. } else {
  1617. set_in_skip(con, tail_len);
  1618. con->v2.in_state = IN_S_FINISH_SKIP;
  1619. }
  1620. }
  1621. static int process_banner_prefix(struct ceph_connection *con)
  1622. {
  1623. int payload_len;
  1624. void *p;
  1625. WARN_ON(con->v2.in_kvecs[0].iov_len != CEPH_BANNER_V2_PREFIX_LEN);
  1626. p = con->v2.in_kvecs[0].iov_base;
  1627. if (memcmp(p, CEPH_BANNER_V2, CEPH_BANNER_V2_LEN)) {
  1628. if (!memcmp(p, CEPH_BANNER, CEPH_BANNER_LEN))
  1629. con->error_msg = "server is speaking msgr1 protocol";
  1630. else
  1631. con->error_msg = "protocol error, bad banner";
  1632. return -EINVAL;
  1633. }
  1634. p += CEPH_BANNER_V2_LEN;
  1635. payload_len = ceph_decode_16(&p);
  1636. dout("%s con %p payload_len %d\n", __func__, con, payload_len);
  1637. return prepare_read_banner_payload(con, payload_len);
  1638. }
  1639. static int process_banner_payload(struct ceph_connection *con)
  1640. {
  1641. void *end = con->v2.in_kvecs[0].iov_base + con->v2.in_kvecs[0].iov_len;
  1642. u64 feat = CEPH_MSGR2_SUPPORTED_FEATURES;
  1643. u64 req_feat = CEPH_MSGR2_REQUIRED_FEATURES;
  1644. u64 server_feat, server_req_feat;
  1645. void *p;
  1646. int ret;
  1647. p = con->v2.in_kvecs[0].iov_base;
  1648. ceph_decode_64_safe(&p, end, server_feat, bad);
  1649. ceph_decode_64_safe(&p, end, server_req_feat, bad);
  1650. dout("%s con %p server_feat 0x%llx server_req_feat 0x%llx\n",
  1651. __func__, con, server_feat, server_req_feat);
  1652. if (req_feat & ~server_feat) {
  1653. pr_err("msgr2 feature set mismatch: my required > server's supported 0x%llx, need 0x%llx\n",
  1654. server_feat, req_feat & ~server_feat);
  1655. con->error_msg = "missing required protocol features";
  1656. return -EINVAL;
  1657. }
  1658. if (server_req_feat & ~feat) {
  1659. pr_err("msgr2 feature set mismatch: server's required > my supported 0x%llx, missing 0x%llx\n",
  1660. feat, server_req_feat & ~feat);
  1661. con->error_msg = "missing required protocol features";
  1662. return -EINVAL;
  1663. }
  1664. /* no reset_out_kvecs() as our banner may still be pending */
  1665. ret = prepare_hello(con);
  1666. if (ret) {
  1667. pr_err("prepare_hello failed: %d\n", ret);
  1668. return ret;
  1669. }
  1670. con->state = CEPH_CON_S_V2_HELLO;
  1671. prepare_read_preamble(con);
  1672. return 0;
  1673. bad:
  1674. pr_err("failed to decode banner payload\n");
  1675. return -EINVAL;
  1676. }
  1677. static int process_hello(struct ceph_connection *con, void *p, void *end)
  1678. {
  1679. struct ceph_entity_addr *my_addr = &con->msgr->inst.addr;
  1680. struct ceph_entity_addr addr_for_me;
  1681. u8 entity_type;
  1682. int ret;
  1683. if (con->state != CEPH_CON_S_V2_HELLO) {
  1684. con->error_msg = "protocol error, unexpected hello";
  1685. return -EINVAL;
  1686. }
  1687. ceph_decode_8_safe(&p, end, entity_type, bad);
  1688. ret = ceph_decode_entity_addr(&p, end, &addr_for_me);
  1689. if (ret) {
  1690. pr_err("failed to decode addr_for_me: %d\n", ret);
  1691. return ret;
  1692. }
  1693. dout("%s con %p entity_type %d addr_for_me %s\n", __func__, con,
  1694. entity_type, ceph_pr_addr(&addr_for_me));
  1695. if (entity_type != con->peer_name.type) {
  1696. pr_err("bad peer type, want %d, got %d\n",
  1697. con->peer_name.type, entity_type);
  1698. con->error_msg = "wrong peer at address";
  1699. return -EINVAL;
  1700. }
  1701. /*
  1702. * Set our address to the address our first peer (i.e. monitor)
  1703. * sees that we are connecting from. If we are behind some sort
  1704. * of NAT and want to be identified by some private (not NATed)
  1705. * address, ip option should be used.
  1706. */
  1707. if (ceph_addr_is_blank(my_addr)) {
  1708. memcpy(&my_addr->in_addr, &addr_for_me.in_addr,
  1709. sizeof(my_addr->in_addr));
  1710. ceph_addr_set_port(my_addr, 0);
  1711. dout("%s con %p set my addr %s, as seen by peer %s\n",
  1712. __func__, con, ceph_pr_addr(my_addr),
  1713. ceph_pr_addr(&con->peer_addr));
  1714. } else {
  1715. dout("%s con %p my addr already set %s\n",
  1716. __func__, con, ceph_pr_addr(my_addr));
  1717. }
  1718. WARN_ON(ceph_addr_is_blank(my_addr) || ceph_addr_port(my_addr));
  1719. WARN_ON(my_addr->type != CEPH_ENTITY_ADDR_TYPE_ANY);
  1720. WARN_ON(!my_addr->nonce);
  1721. /* no reset_out_kvecs() as our hello may still be pending */
  1722. ret = prepare_auth_request(con);
  1723. if (ret) {
  1724. if (ret != -EAGAIN)
  1725. pr_err("prepare_auth_request failed: %d\n", ret);
  1726. return ret;
  1727. }
  1728. con->state = CEPH_CON_S_V2_AUTH;
  1729. return 0;
  1730. bad:
  1731. pr_err("failed to decode hello\n");
  1732. return -EINVAL;
  1733. }
  1734. static int process_auth_bad_method(struct ceph_connection *con,
  1735. void *p, void *end)
  1736. {
  1737. int allowed_protos[8], allowed_modes[8];
  1738. int allowed_proto_cnt, allowed_mode_cnt;
  1739. int used_proto, result;
  1740. int ret;
  1741. int i;
  1742. if (con->state != CEPH_CON_S_V2_AUTH) {
  1743. con->error_msg = "protocol error, unexpected auth_bad_method";
  1744. return -EINVAL;
  1745. }
  1746. ceph_decode_32_safe(&p, end, used_proto, bad);
  1747. ceph_decode_32_safe(&p, end, result, bad);
  1748. dout("%s con %p used_proto %d result %d\n", __func__, con, used_proto,
  1749. result);
  1750. ceph_decode_32_safe(&p, end, allowed_proto_cnt, bad);
  1751. if (allowed_proto_cnt > ARRAY_SIZE(allowed_protos)) {
  1752. pr_err("allowed_protos too big %d\n", allowed_proto_cnt);
  1753. return -EINVAL;
  1754. }
  1755. for (i = 0; i < allowed_proto_cnt; i++) {
  1756. ceph_decode_32_safe(&p, end, allowed_protos[i], bad);
  1757. dout("%s con %p allowed_protos[%d] %d\n", __func__, con,
  1758. i, allowed_protos[i]);
  1759. }
  1760. ceph_decode_32_safe(&p, end, allowed_mode_cnt, bad);
  1761. if (allowed_mode_cnt > ARRAY_SIZE(allowed_modes)) {
  1762. pr_err("allowed_modes too big %d\n", allowed_mode_cnt);
  1763. return -EINVAL;
  1764. }
  1765. for (i = 0; i < allowed_mode_cnt; i++) {
  1766. ceph_decode_32_safe(&p, end, allowed_modes[i], bad);
  1767. dout("%s con %p allowed_modes[%d] %d\n", __func__, con,
  1768. i, allowed_modes[i]);
  1769. }
  1770. mutex_unlock(&con->mutex);
  1771. ret = con->ops->handle_auth_bad_method(con, used_proto, result,
  1772. allowed_protos,
  1773. allowed_proto_cnt,
  1774. allowed_modes,
  1775. allowed_mode_cnt);
  1776. mutex_lock(&con->mutex);
  1777. if (con->state != CEPH_CON_S_V2_AUTH) {
  1778. dout("%s con %p state changed to %d\n", __func__, con,
  1779. con->state);
  1780. return -EAGAIN;
  1781. }
  1782. dout("%s con %p handle_auth_bad_method ret %d\n", __func__, con, ret);
  1783. return ret;
  1784. bad:
  1785. pr_err("failed to decode auth_bad_method\n");
  1786. return -EINVAL;
  1787. }
  1788. static int process_auth_reply_more(struct ceph_connection *con,
  1789. void *p, void *end)
  1790. {
  1791. int payload_len;
  1792. int ret;
  1793. if (con->state != CEPH_CON_S_V2_AUTH) {
  1794. con->error_msg = "protocol error, unexpected auth_reply_more";
  1795. return -EINVAL;
  1796. }
  1797. ceph_decode_32_safe(&p, end, payload_len, bad);
  1798. ceph_decode_need(&p, end, payload_len, bad);
  1799. dout("%s con %p payload_len %d\n", __func__, con, payload_len);
  1800. reset_out_kvecs(con);
  1801. ret = prepare_auth_request_more(con, p, payload_len);
  1802. if (ret) {
  1803. if (ret != -EAGAIN)
  1804. pr_err("prepare_auth_request_more failed: %d\n", ret);
  1805. return ret;
  1806. }
  1807. return 0;
  1808. bad:
  1809. pr_err("failed to decode auth_reply_more\n");
  1810. return -EINVAL;
  1811. }
  1812. /*
  1813. * Align session_key and con_secret to avoid GFP_ATOMIC allocation
  1814. * inside crypto_shash_setkey() and crypto_aead_setkey() called from
  1815. * setup_crypto(). __aligned(16) isn't guaranteed to work for stack
  1816. * objects, so do it by hand.
  1817. */
  1818. static int process_auth_done(struct ceph_connection *con, void *p, void *end)
  1819. {
  1820. u8 session_key_buf[CEPH_KEY_LEN + 16];
  1821. u8 con_secret_buf[CEPH_MAX_CON_SECRET_LEN + 16];
  1822. u8 *session_key = PTR_ALIGN(&session_key_buf[0], 16);
  1823. u8 *con_secret = PTR_ALIGN(&con_secret_buf[0], 16);
  1824. int session_key_len, con_secret_len;
  1825. int payload_len;
  1826. u64 global_id;
  1827. int ret;
  1828. if (con->state != CEPH_CON_S_V2_AUTH) {
  1829. con->error_msg = "protocol error, unexpected auth_done";
  1830. return -EINVAL;
  1831. }
  1832. ceph_decode_64_safe(&p, end, global_id, bad);
  1833. ceph_decode_32_safe(&p, end, con->v2.con_mode, bad);
  1834. ceph_decode_32_safe(&p, end, payload_len, bad);
  1835. dout("%s con %p global_id %llu con_mode %d payload_len %d\n",
  1836. __func__, con, global_id, con->v2.con_mode, payload_len);
  1837. mutex_unlock(&con->mutex);
  1838. session_key_len = 0;
  1839. con_secret_len = 0;
  1840. ret = con->ops->handle_auth_done(con, global_id, p, payload_len,
  1841. session_key, &session_key_len,
  1842. con_secret, &con_secret_len);
  1843. mutex_lock(&con->mutex);
  1844. if (con->state != CEPH_CON_S_V2_AUTH) {
  1845. dout("%s con %p state changed to %d\n", __func__, con,
  1846. con->state);
  1847. ret = -EAGAIN;
  1848. goto out;
  1849. }
  1850. dout("%s con %p handle_auth_done ret %d\n", __func__, con, ret);
  1851. if (ret)
  1852. goto out;
  1853. ret = setup_crypto(con, session_key, session_key_len, con_secret,
  1854. con_secret_len);
  1855. if (ret)
  1856. goto out;
  1857. reset_out_kvecs(con);
  1858. ret = prepare_auth_signature(con);
  1859. if (ret) {
  1860. pr_err("prepare_auth_signature failed: %d\n", ret);
  1861. goto out;
  1862. }
  1863. con->state = CEPH_CON_S_V2_AUTH_SIGNATURE;
  1864. out:
  1865. memzero_explicit(session_key_buf, sizeof(session_key_buf));
  1866. memzero_explicit(con_secret_buf, sizeof(con_secret_buf));
  1867. return ret;
  1868. bad:
  1869. pr_err("failed to decode auth_done\n");
  1870. return -EINVAL;
  1871. }
  1872. static int process_auth_signature(struct ceph_connection *con,
  1873. void *p, void *end)
  1874. {
  1875. u8 hmac[SHA256_DIGEST_SIZE];
  1876. int ret;
  1877. if (con->state != CEPH_CON_S_V2_AUTH_SIGNATURE) {
  1878. con->error_msg = "protocol error, unexpected auth_signature";
  1879. return -EINVAL;
  1880. }
  1881. ret = hmac_sha256(con, con->v2.out_sign_kvecs,
  1882. con->v2.out_sign_kvec_cnt, hmac);
  1883. if (ret)
  1884. return ret;
  1885. ceph_decode_need(&p, end, SHA256_DIGEST_SIZE, bad);
  1886. if (crypto_memneq(p, hmac, SHA256_DIGEST_SIZE)) {
  1887. con->error_msg = "integrity error, bad auth signature";
  1888. return -EBADMSG;
  1889. }
  1890. dout("%s con %p auth signature ok\n", __func__, con);
  1891. /* no reset_out_kvecs() as our auth_signature may still be pending */
  1892. if (!con->v2.server_cookie) {
  1893. ret = prepare_client_ident(con);
  1894. if (ret) {
  1895. pr_err("prepare_client_ident failed: %d\n", ret);
  1896. return ret;
  1897. }
  1898. con->state = CEPH_CON_S_V2_SESSION_CONNECT;
  1899. } else {
  1900. ret = prepare_session_reconnect(con);
  1901. if (ret) {
  1902. pr_err("prepare_session_reconnect failed: %d\n", ret);
  1903. return ret;
  1904. }
  1905. con->state = CEPH_CON_S_V2_SESSION_RECONNECT;
  1906. }
  1907. return 0;
  1908. bad:
  1909. pr_err("failed to decode auth_signature\n");
  1910. return -EINVAL;
  1911. }
  1912. static int process_server_ident(struct ceph_connection *con,
  1913. void *p, void *end)
  1914. {
  1915. struct ceph_client *client = from_msgr(con->msgr);
  1916. u64 features, required_features;
  1917. struct ceph_entity_addr addr;
  1918. u64 global_seq;
  1919. u64 global_id;
  1920. u64 cookie;
  1921. u64 flags;
  1922. int ret;
  1923. if (con->state != CEPH_CON_S_V2_SESSION_CONNECT) {
  1924. con->error_msg = "protocol error, unexpected server_ident";
  1925. return -EINVAL;
  1926. }
  1927. ret = ceph_decode_entity_addrvec(&p, end, true, &addr);
  1928. if (ret) {
  1929. pr_err("failed to decode server addrs: %d\n", ret);
  1930. return ret;
  1931. }
  1932. ceph_decode_64_safe(&p, end, global_id, bad);
  1933. ceph_decode_64_safe(&p, end, global_seq, bad);
  1934. ceph_decode_64_safe(&p, end, features, bad);
  1935. ceph_decode_64_safe(&p, end, required_features, bad);
  1936. ceph_decode_64_safe(&p, end, flags, bad);
  1937. ceph_decode_64_safe(&p, end, cookie, bad);
  1938. dout("%s con %p addr %s/%u global_id %llu global_seq %llu features 0x%llx required_features 0x%llx flags 0x%llx cookie 0x%llx\n",
  1939. __func__, con, ceph_pr_addr(&addr), le32_to_cpu(addr.nonce),
  1940. global_id, global_seq, features, required_features, flags, cookie);
  1941. /* is this who we intended to talk to? */
  1942. if (memcmp(&addr, &con->peer_addr, sizeof(con->peer_addr))) {
  1943. pr_err("bad peer addr/nonce, want %s/%u, got %s/%u\n",
  1944. ceph_pr_addr(&con->peer_addr),
  1945. le32_to_cpu(con->peer_addr.nonce),
  1946. ceph_pr_addr(&addr), le32_to_cpu(addr.nonce));
  1947. con->error_msg = "wrong peer at address";
  1948. return -EINVAL;
  1949. }
  1950. if (client->required_features & ~features) {
  1951. pr_err("RADOS feature set mismatch: my required > server's supported 0x%llx, need 0x%llx\n",
  1952. features, client->required_features & ~features);
  1953. con->error_msg = "missing required protocol features";
  1954. return -EINVAL;
  1955. }
  1956. /*
  1957. * Both name->type and name->num are set in ceph_con_open() but
  1958. * name->num may be bogus in the initial monmap. name->type is
  1959. * verified in handle_hello().
  1960. */
  1961. WARN_ON(!con->peer_name.type);
  1962. con->peer_name.num = cpu_to_le64(global_id);
  1963. con->v2.peer_global_seq = global_seq;
  1964. con->peer_features = features;
  1965. WARN_ON(required_features & ~client->supported_features);
  1966. con->v2.server_cookie = cookie;
  1967. if (flags & CEPH_MSG_CONNECT_LOSSY) {
  1968. ceph_con_flag_set(con, CEPH_CON_F_LOSSYTX);
  1969. WARN_ON(con->v2.server_cookie);
  1970. } else {
  1971. WARN_ON(!con->v2.server_cookie);
  1972. }
  1973. clear_in_sign_kvecs(con);
  1974. clear_out_sign_kvecs(con);
  1975. free_conn_bufs(con);
  1976. con->delay = 0; /* reset backoff memory */
  1977. con->state = CEPH_CON_S_OPEN;
  1978. con->v2.out_state = OUT_S_GET_NEXT;
  1979. return 0;
  1980. bad:
  1981. pr_err("failed to decode server_ident\n");
  1982. return -EINVAL;
  1983. }
  1984. static int process_ident_missing_features(struct ceph_connection *con,
  1985. void *p, void *end)
  1986. {
  1987. struct ceph_client *client = from_msgr(con->msgr);
  1988. u64 missing_features;
  1989. if (con->state != CEPH_CON_S_V2_SESSION_CONNECT) {
  1990. con->error_msg = "protocol error, unexpected ident_missing_features";
  1991. return -EINVAL;
  1992. }
  1993. ceph_decode_64_safe(&p, end, missing_features, bad);
  1994. pr_err("RADOS feature set mismatch: server's required > my supported 0x%llx, missing 0x%llx\n",
  1995. client->supported_features, missing_features);
  1996. con->error_msg = "missing required protocol features";
  1997. return -EINVAL;
  1998. bad:
  1999. pr_err("failed to decode ident_missing_features\n");
  2000. return -EINVAL;
  2001. }
  2002. static int process_session_reconnect_ok(struct ceph_connection *con,
  2003. void *p, void *end)
  2004. {
  2005. u64 seq;
  2006. if (con->state != CEPH_CON_S_V2_SESSION_RECONNECT) {
  2007. con->error_msg = "protocol error, unexpected session_reconnect_ok";
  2008. return -EINVAL;
  2009. }
  2010. ceph_decode_64_safe(&p, end, seq, bad);
  2011. dout("%s con %p seq %llu\n", __func__, con, seq);
  2012. ceph_con_discard_requeued(con, seq);
  2013. clear_in_sign_kvecs(con);
  2014. clear_out_sign_kvecs(con);
  2015. free_conn_bufs(con);
  2016. con->delay = 0; /* reset backoff memory */
  2017. con->state = CEPH_CON_S_OPEN;
  2018. con->v2.out_state = OUT_S_GET_NEXT;
  2019. return 0;
  2020. bad:
  2021. pr_err("failed to decode session_reconnect_ok\n");
  2022. return -EINVAL;
  2023. }
  2024. static int process_session_retry(struct ceph_connection *con,
  2025. void *p, void *end)
  2026. {
  2027. u64 connect_seq;
  2028. int ret;
  2029. if (con->state != CEPH_CON_S_V2_SESSION_RECONNECT) {
  2030. con->error_msg = "protocol error, unexpected session_retry";
  2031. return -EINVAL;
  2032. }
  2033. ceph_decode_64_safe(&p, end, connect_seq, bad);
  2034. dout("%s con %p connect_seq %llu\n", __func__, con, connect_seq);
  2035. WARN_ON(connect_seq <= con->v2.connect_seq);
  2036. con->v2.connect_seq = connect_seq + 1;
  2037. free_conn_bufs(con);
  2038. reset_out_kvecs(con);
  2039. ret = prepare_session_reconnect(con);
  2040. if (ret) {
  2041. pr_err("prepare_session_reconnect (cseq) failed: %d\n", ret);
  2042. return ret;
  2043. }
  2044. return 0;
  2045. bad:
  2046. pr_err("failed to decode session_retry\n");
  2047. return -EINVAL;
  2048. }
  2049. static int process_session_retry_global(struct ceph_connection *con,
  2050. void *p, void *end)
  2051. {
  2052. u64 global_seq;
  2053. int ret;
  2054. if (con->state != CEPH_CON_S_V2_SESSION_RECONNECT) {
  2055. con->error_msg = "protocol error, unexpected session_retry_global";
  2056. return -EINVAL;
  2057. }
  2058. ceph_decode_64_safe(&p, end, global_seq, bad);
  2059. dout("%s con %p global_seq %llu\n", __func__, con, global_seq);
  2060. WARN_ON(global_seq <= con->v2.global_seq);
  2061. con->v2.global_seq = ceph_get_global_seq(con->msgr, global_seq);
  2062. free_conn_bufs(con);
  2063. reset_out_kvecs(con);
  2064. ret = prepare_session_reconnect(con);
  2065. if (ret) {
  2066. pr_err("prepare_session_reconnect (gseq) failed: %d\n", ret);
  2067. return ret;
  2068. }
  2069. return 0;
  2070. bad:
  2071. pr_err("failed to decode session_retry_global\n");
  2072. return -EINVAL;
  2073. }
  2074. static int process_session_reset(struct ceph_connection *con,
  2075. void *p, void *end)
  2076. {
  2077. bool full;
  2078. int ret;
  2079. if (con->state != CEPH_CON_S_V2_SESSION_RECONNECT) {
  2080. con->error_msg = "protocol error, unexpected session_reset";
  2081. return -EINVAL;
  2082. }
  2083. ceph_decode_8_safe(&p, end, full, bad);
  2084. if (!full) {
  2085. con->error_msg = "protocol error, bad session_reset";
  2086. return -EINVAL;
  2087. }
  2088. pr_info("%s%lld %s session reset\n", ENTITY_NAME(con->peer_name),
  2089. ceph_pr_addr(&con->peer_addr));
  2090. ceph_con_reset_session(con);
  2091. mutex_unlock(&con->mutex);
  2092. if (con->ops->peer_reset)
  2093. con->ops->peer_reset(con);
  2094. mutex_lock(&con->mutex);
  2095. if (con->state != CEPH_CON_S_V2_SESSION_RECONNECT) {
  2096. dout("%s con %p state changed to %d\n", __func__, con,
  2097. con->state);
  2098. return -EAGAIN;
  2099. }
  2100. free_conn_bufs(con);
  2101. reset_out_kvecs(con);
  2102. ret = prepare_client_ident(con);
  2103. if (ret) {
  2104. pr_err("prepare_client_ident (rst) failed: %d\n", ret);
  2105. return ret;
  2106. }
  2107. con->state = CEPH_CON_S_V2_SESSION_CONNECT;
  2108. return 0;
  2109. bad:
  2110. pr_err("failed to decode session_reset\n");
  2111. return -EINVAL;
  2112. }
  2113. static int process_keepalive2_ack(struct ceph_connection *con,
  2114. void *p, void *end)
  2115. {
  2116. if (con->state != CEPH_CON_S_OPEN) {
  2117. con->error_msg = "protocol error, unexpected keepalive2_ack";
  2118. return -EINVAL;
  2119. }
  2120. ceph_decode_need(&p, end, sizeof(struct ceph_timespec), bad);
  2121. ceph_decode_timespec64(&con->last_keepalive_ack, p);
  2122. dout("%s con %p timestamp %lld.%09ld\n", __func__, con,
  2123. con->last_keepalive_ack.tv_sec, con->last_keepalive_ack.tv_nsec);
  2124. return 0;
  2125. bad:
  2126. pr_err("failed to decode keepalive2_ack\n");
  2127. return -EINVAL;
  2128. }
  2129. static int process_ack(struct ceph_connection *con, void *p, void *end)
  2130. {
  2131. u64 seq;
  2132. if (con->state != CEPH_CON_S_OPEN) {
  2133. con->error_msg = "protocol error, unexpected ack";
  2134. return -EINVAL;
  2135. }
  2136. ceph_decode_64_safe(&p, end, seq, bad);
  2137. dout("%s con %p seq %llu\n", __func__, con, seq);
  2138. ceph_con_discard_sent(con, seq);
  2139. return 0;
  2140. bad:
  2141. pr_err("failed to decode ack\n");
  2142. return -EINVAL;
  2143. }
  2144. static int process_control(struct ceph_connection *con, void *p, void *end)
  2145. {
  2146. int tag = con->v2.in_desc.fd_tag;
  2147. int ret;
  2148. dout("%s con %p tag %d len %d\n", __func__, con, tag, (int)(end - p));
  2149. switch (tag) {
  2150. case FRAME_TAG_HELLO:
  2151. ret = process_hello(con, p, end);
  2152. break;
  2153. case FRAME_TAG_AUTH_BAD_METHOD:
  2154. ret = process_auth_bad_method(con, p, end);
  2155. break;
  2156. case FRAME_TAG_AUTH_REPLY_MORE:
  2157. ret = process_auth_reply_more(con, p, end);
  2158. break;
  2159. case FRAME_TAG_AUTH_DONE:
  2160. ret = process_auth_done(con, p, end);
  2161. break;
  2162. case FRAME_TAG_AUTH_SIGNATURE:
  2163. ret = process_auth_signature(con, p, end);
  2164. break;
  2165. case FRAME_TAG_SERVER_IDENT:
  2166. ret = process_server_ident(con, p, end);
  2167. break;
  2168. case FRAME_TAG_IDENT_MISSING_FEATURES:
  2169. ret = process_ident_missing_features(con, p, end);
  2170. break;
  2171. case FRAME_TAG_SESSION_RECONNECT_OK:
  2172. ret = process_session_reconnect_ok(con, p, end);
  2173. break;
  2174. case FRAME_TAG_SESSION_RETRY:
  2175. ret = process_session_retry(con, p, end);
  2176. break;
  2177. case FRAME_TAG_SESSION_RETRY_GLOBAL:
  2178. ret = process_session_retry_global(con, p, end);
  2179. break;
  2180. case FRAME_TAG_SESSION_RESET:
  2181. ret = process_session_reset(con, p, end);
  2182. break;
  2183. case FRAME_TAG_KEEPALIVE2_ACK:
  2184. ret = process_keepalive2_ack(con, p, end);
  2185. break;
  2186. case FRAME_TAG_ACK:
  2187. ret = process_ack(con, p, end);
  2188. break;
  2189. default:
  2190. pr_err("bad tag %d\n", tag);
  2191. con->error_msg = "protocol error, bad tag";
  2192. return -EINVAL;
  2193. }
  2194. if (ret) {
  2195. dout("%s con %p error %d\n", __func__, con, ret);
  2196. return ret;
  2197. }
  2198. prepare_read_preamble(con);
  2199. return 0;
  2200. }
  2201. /*
  2202. * Return:
  2203. * 1 - con->in_msg set, read message
  2204. * 0 - skip message
  2205. * <0 - error
  2206. */
  2207. static int process_message_header(struct ceph_connection *con,
  2208. void *p, void *end)
  2209. {
  2210. struct ceph_frame_desc *desc = &con->v2.in_desc;
  2211. struct ceph_msg_header2 *hdr2 = p;
  2212. struct ceph_msg_header hdr;
  2213. int skip;
  2214. int ret;
  2215. u64 seq;
  2216. /* verify seq# */
  2217. seq = le64_to_cpu(hdr2->seq);
  2218. if ((s64)seq - (s64)con->in_seq < 1) {
  2219. pr_info("%s%lld %s skipping old message: seq %llu, expected %llu\n",
  2220. ENTITY_NAME(con->peer_name),
  2221. ceph_pr_addr(&con->peer_addr),
  2222. seq, con->in_seq + 1);
  2223. return 0;
  2224. }
  2225. if ((s64)seq - (s64)con->in_seq > 1) {
  2226. pr_err("bad seq %llu, expected %llu\n", seq, con->in_seq + 1);
  2227. con->error_msg = "bad message sequence # for incoming message";
  2228. return -EBADE;
  2229. }
  2230. ceph_con_discard_sent(con, le64_to_cpu(hdr2->ack_seq));
  2231. fill_header(&hdr, hdr2, desc->fd_lens[1], desc->fd_lens[2],
  2232. desc->fd_lens[3], &con->peer_name);
  2233. ret = ceph_con_in_msg_alloc(con, &hdr, &skip);
  2234. if (ret)
  2235. return ret;
  2236. WARN_ON(!con->in_msg ^ skip);
  2237. if (skip)
  2238. return 0;
  2239. WARN_ON(!con->in_msg);
  2240. WARN_ON(con->in_msg->con != con);
  2241. return 1;
  2242. }
  2243. static int process_message(struct ceph_connection *con)
  2244. {
  2245. ceph_con_process_message(con);
  2246. /*
  2247. * We could have been closed by ceph_con_close() because
  2248. * ceph_con_process_message() temporarily drops con->mutex.
  2249. */
  2250. if (con->state != CEPH_CON_S_OPEN) {
  2251. dout("%s con %p state changed to %d\n", __func__, con,
  2252. con->state);
  2253. return -EAGAIN;
  2254. }
  2255. prepare_read_preamble(con);
  2256. return 0;
  2257. }
  2258. static int __handle_control(struct ceph_connection *con, void *p)
  2259. {
  2260. void *end = p + con->v2.in_desc.fd_lens[0];
  2261. struct ceph_msg *msg;
  2262. int ret;
  2263. if (con->v2.in_desc.fd_tag != FRAME_TAG_MESSAGE)
  2264. return process_control(con, p, end);
  2265. ret = process_message_header(con, p, end);
  2266. if (ret < 0)
  2267. return ret;
  2268. if (ret == 0) {
  2269. prepare_skip_message(con);
  2270. return 0;
  2271. }
  2272. msg = con->in_msg; /* set in process_message_header() */
  2273. if (front_len(msg)) {
  2274. WARN_ON(front_len(msg) > msg->front_alloc_len);
  2275. msg->front.iov_len = front_len(msg);
  2276. } else {
  2277. msg->front.iov_len = 0;
  2278. }
  2279. if (middle_len(msg)) {
  2280. WARN_ON(middle_len(msg) > msg->middle->alloc_len);
  2281. msg->middle->vec.iov_len = middle_len(msg);
  2282. } else if (msg->middle) {
  2283. msg->middle->vec.iov_len = 0;
  2284. }
  2285. if (!front_len(msg) && !middle_len(msg) && !data_len(msg))
  2286. return process_message(con);
  2287. if (con_secure(con))
  2288. return prepare_read_tail_secure(con);
  2289. return prepare_read_tail_plain(con);
  2290. }
  2291. static int handle_preamble(struct ceph_connection *con)
  2292. {
  2293. struct ceph_frame_desc *desc = &con->v2.in_desc;
  2294. int ret;
  2295. if (con_secure(con)) {
  2296. ret = decrypt_preamble(con);
  2297. if (ret) {
  2298. if (ret == -EBADMSG)
  2299. con->error_msg = "integrity error, bad preamble auth tag";
  2300. return ret;
  2301. }
  2302. }
  2303. ret = decode_preamble(con->v2.in_buf, desc);
  2304. if (ret) {
  2305. if (ret == -EBADMSG)
  2306. con->error_msg = "integrity error, bad crc";
  2307. else
  2308. con->error_msg = "protocol error, bad preamble";
  2309. return ret;
  2310. }
  2311. dout("%s con %p tag %d seg_cnt %d %d+%d+%d+%d\n", __func__,
  2312. con, desc->fd_tag, desc->fd_seg_cnt, desc->fd_lens[0],
  2313. desc->fd_lens[1], desc->fd_lens[2], desc->fd_lens[3]);
  2314. if (!con_secure(con))
  2315. return prepare_read_control(con);
  2316. if (desc->fd_lens[0] > CEPH_PREAMBLE_INLINE_LEN)
  2317. return prepare_read_control_remainder(con);
  2318. return __handle_control(con, CTRL_BODY(con->v2.in_buf));
  2319. }
  2320. static int handle_control(struct ceph_connection *con)
  2321. {
  2322. int ctrl_len = con->v2.in_desc.fd_lens[0];
  2323. void *buf;
  2324. int ret;
  2325. WARN_ON(con_secure(con));
  2326. ret = verify_control_crc(con);
  2327. if (ret) {
  2328. con->error_msg = "integrity error, bad crc";
  2329. return ret;
  2330. }
  2331. if (con->state == CEPH_CON_S_V2_AUTH) {
  2332. buf = alloc_conn_buf(con, ctrl_len);
  2333. if (!buf)
  2334. return -ENOMEM;
  2335. memcpy(buf, con->v2.in_kvecs[0].iov_base, ctrl_len);
  2336. return __handle_control(con, buf);
  2337. }
  2338. return __handle_control(con, con->v2.in_kvecs[0].iov_base);
  2339. }
  2340. static int handle_control_remainder(struct ceph_connection *con)
  2341. {
  2342. int ret;
  2343. WARN_ON(!con_secure(con));
  2344. ret = decrypt_control_remainder(con);
  2345. if (ret) {
  2346. if (ret == -EBADMSG)
  2347. con->error_msg = "integrity error, bad control remainder auth tag";
  2348. return ret;
  2349. }
  2350. return __handle_control(con, con->v2.in_kvecs[0].iov_base -
  2351. CEPH_PREAMBLE_INLINE_LEN);
  2352. }
  2353. static int handle_epilogue(struct ceph_connection *con)
  2354. {
  2355. u32 front_crc, middle_crc, data_crc;
  2356. int ret;
  2357. if (con_secure(con)) {
  2358. ret = decrypt_tail(con);
  2359. if (ret) {
  2360. if (ret == -EBADMSG)
  2361. con->error_msg = "integrity error, bad epilogue auth tag";
  2362. return ret;
  2363. }
  2364. /* just late_status */
  2365. ret = decode_epilogue(con->v2.in_buf, NULL, NULL, NULL);
  2366. if (ret) {
  2367. con->error_msg = "protocol error, bad epilogue";
  2368. return ret;
  2369. }
  2370. } else {
  2371. ret = decode_epilogue(con->v2.in_buf, &front_crc,
  2372. &middle_crc, &data_crc);
  2373. if (ret) {
  2374. con->error_msg = "protocol error, bad epilogue";
  2375. return ret;
  2376. }
  2377. ret = verify_epilogue_crcs(con, front_crc, middle_crc,
  2378. data_crc);
  2379. if (ret) {
  2380. con->error_msg = "integrity error, bad crc";
  2381. return ret;
  2382. }
  2383. }
  2384. return process_message(con);
  2385. }
  2386. static void finish_skip(struct ceph_connection *con)
  2387. {
  2388. dout("%s con %p\n", __func__, con);
  2389. if (con_secure(con))
  2390. gcm_inc_nonce(&con->v2.in_gcm_nonce);
  2391. __finish_skip(con);
  2392. }
  2393. static int populate_in_iter(struct ceph_connection *con)
  2394. {
  2395. int ret;
  2396. dout("%s con %p state %d in_state %d\n", __func__, con, con->state,
  2397. con->v2.in_state);
  2398. WARN_ON(iov_iter_count(&con->v2.in_iter));
  2399. if (con->state == CEPH_CON_S_V2_BANNER_PREFIX) {
  2400. ret = process_banner_prefix(con);
  2401. } else if (con->state == CEPH_CON_S_V2_BANNER_PAYLOAD) {
  2402. ret = process_banner_payload(con);
  2403. } else if ((con->state >= CEPH_CON_S_V2_HELLO &&
  2404. con->state <= CEPH_CON_S_V2_SESSION_RECONNECT) ||
  2405. con->state == CEPH_CON_S_OPEN) {
  2406. switch (con->v2.in_state) {
  2407. case IN_S_HANDLE_PREAMBLE:
  2408. ret = handle_preamble(con);
  2409. break;
  2410. case IN_S_HANDLE_CONTROL:
  2411. ret = handle_control(con);
  2412. break;
  2413. case IN_S_HANDLE_CONTROL_REMAINDER:
  2414. ret = handle_control_remainder(con);
  2415. break;
  2416. case IN_S_PREPARE_READ_DATA:
  2417. ret = prepare_read_data(con);
  2418. break;
  2419. case IN_S_PREPARE_READ_DATA_CONT:
  2420. prepare_read_data_cont(con);
  2421. ret = 0;
  2422. break;
  2423. case IN_S_PREPARE_READ_ENC_PAGE:
  2424. prepare_read_enc_page(con);
  2425. ret = 0;
  2426. break;
  2427. case IN_S_HANDLE_EPILOGUE:
  2428. ret = handle_epilogue(con);
  2429. break;
  2430. case IN_S_FINISH_SKIP:
  2431. finish_skip(con);
  2432. ret = 0;
  2433. break;
  2434. default:
  2435. WARN(1, "bad in_state %d", con->v2.in_state);
  2436. return -EINVAL;
  2437. }
  2438. } else {
  2439. WARN(1, "bad state %d", con->state);
  2440. return -EINVAL;
  2441. }
  2442. if (ret) {
  2443. dout("%s con %p error %d\n", __func__, con, ret);
  2444. return ret;
  2445. }
  2446. if (WARN_ON(!iov_iter_count(&con->v2.in_iter)))
  2447. return -ENODATA;
  2448. dout("%s con %p populated %zu\n", __func__, con,
  2449. iov_iter_count(&con->v2.in_iter));
  2450. return 1;
  2451. }
  2452. int ceph_con_v2_try_read(struct ceph_connection *con)
  2453. {
  2454. int ret;
  2455. dout("%s con %p state %d need %zu\n", __func__, con, con->state,
  2456. iov_iter_count(&con->v2.in_iter));
  2457. if (con->state == CEPH_CON_S_PREOPEN)
  2458. return 0;
  2459. /*
  2460. * We should always have something pending here. If not,
  2461. * avoid calling populate_in_iter() as if we read something
  2462. * (ceph_tcp_recv() would immediately return 1).
  2463. */
  2464. if (WARN_ON(!iov_iter_count(&con->v2.in_iter)))
  2465. return -ENODATA;
  2466. for (;;) {
  2467. ret = ceph_tcp_recv(con);
  2468. if (ret <= 0)
  2469. return ret;
  2470. ret = populate_in_iter(con);
  2471. if (ret <= 0) {
  2472. if (ret && ret != -EAGAIN && !con->error_msg)
  2473. con->error_msg = "read processing error";
  2474. return ret;
  2475. }
  2476. }
  2477. }
  2478. static void queue_data(struct ceph_connection *con)
  2479. {
  2480. struct bio_vec bv;
  2481. con->v2.out_epil.data_crc = -1;
  2482. ceph_msg_data_cursor_init(&con->v2.out_cursor, con->out_msg,
  2483. data_len(con->out_msg));
  2484. get_bvec_at(&con->v2.out_cursor, &bv);
  2485. set_out_bvec(con, &bv, true);
  2486. con->v2.out_state = OUT_S_QUEUE_DATA_CONT;
  2487. }
  2488. static void queue_data_cont(struct ceph_connection *con)
  2489. {
  2490. struct bio_vec bv;
  2491. con->v2.out_epil.data_crc = ceph_crc32c_page(
  2492. con->v2.out_epil.data_crc, con->v2.out_bvec.bv_page,
  2493. con->v2.out_bvec.bv_offset, con->v2.out_bvec.bv_len);
  2494. ceph_msg_data_advance(&con->v2.out_cursor, con->v2.out_bvec.bv_len);
  2495. if (con->v2.out_cursor.total_resid) {
  2496. get_bvec_at(&con->v2.out_cursor, &bv);
  2497. set_out_bvec(con, &bv, true);
  2498. WARN_ON(con->v2.out_state != OUT_S_QUEUE_DATA_CONT);
  2499. return;
  2500. }
  2501. /*
  2502. * We've written all data. Queue epilogue. Once it's written,
  2503. * we are done.
  2504. */
  2505. reset_out_kvecs(con);
  2506. prepare_epilogue_plain(con, false);
  2507. con->v2.out_state = OUT_S_FINISH_MESSAGE;
  2508. }
  2509. static void queue_enc_page(struct ceph_connection *con)
  2510. {
  2511. struct bio_vec bv;
  2512. dout("%s con %p i %d resid %d\n", __func__, con, con->v2.out_enc_i,
  2513. con->v2.out_enc_resid);
  2514. WARN_ON(!con->v2.out_enc_resid);
  2515. bv.bv_page = con->v2.out_enc_pages[con->v2.out_enc_i];
  2516. bv.bv_offset = 0;
  2517. bv.bv_len = min(con->v2.out_enc_resid, (int)PAGE_SIZE);
  2518. set_out_bvec(con, &bv, false);
  2519. con->v2.out_enc_i++;
  2520. con->v2.out_enc_resid -= bv.bv_len;
  2521. if (con->v2.out_enc_resid) {
  2522. WARN_ON(con->v2.out_state != OUT_S_QUEUE_ENC_PAGE);
  2523. return;
  2524. }
  2525. /*
  2526. * We've queued the last piece of ciphertext (ending with
  2527. * epilogue) + auth tag. Once it's written, we are done.
  2528. */
  2529. WARN_ON(con->v2.out_enc_i != con->v2.out_enc_page_cnt);
  2530. con->v2.out_state = OUT_S_FINISH_MESSAGE;
  2531. }
  2532. static void queue_zeros(struct ceph_connection *con)
  2533. {
  2534. dout("%s con %p out_zero %d\n", __func__, con, con->v2.out_zero);
  2535. if (con->v2.out_zero) {
  2536. set_out_bvec_zero(con);
  2537. con->v2.out_zero -= con->v2.out_bvec.bv_len;
  2538. con->v2.out_state = OUT_S_QUEUE_ZEROS;
  2539. return;
  2540. }
  2541. /*
  2542. * We've zero-filled everything up to epilogue. Queue epilogue
  2543. * with late_status set to ABORTED and crcs adjusted for zeros.
  2544. * Once it's written, we are done patching up for the revoke.
  2545. */
  2546. reset_out_kvecs(con);
  2547. prepare_epilogue_plain(con, true);
  2548. con->v2.out_state = OUT_S_FINISH_MESSAGE;
  2549. }
  2550. static void finish_message(struct ceph_connection *con)
  2551. {
  2552. dout("%s con %p msg %p\n", __func__, con, con->out_msg);
  2553. /* we end up here both plain and secure modes */
  2554. if (con->v2.out_enc_pages) {
  2555. WARN_ON(!con->v2.out_enc_page_cnt);
  2556. ceph_release_page_vector(con->v2.out_enc_pages,
  2557. con->v2.out_enc_page_cnt);
  2558. con->v2.out_enc_pages = NULL;
  2559. con->v2.out_enc_page_cnt = 0;
  2560. }
  2561. /* message may have been revoked */
  2562. if (con->out_msg) {
  2563. ceph_msg_put(con->out_msg);
  2564. con->out_msg = NULL;
  2565. }
  2566. con->v2.out_state = OUT_S_GET_NEXT;
  2567. }
  2568. static int populate_out_iter(struct ceph_connection *con)
  2569. {
  2570. int ret;
  2571. dout("%s con %p state %d out_state %d\n", __func__, con, con->state,
  2572. con->v2.out_state);
  2573. WARN_ON(iov_iter_count(&con->v2.out_iter));
  2574. if (con->state != CEPH_CON_S_OPEN) {
  2575. WARN_ON(con->state < CEPH_CON_S_V2_BANNER_PREFIX ||
  2576. con->state > CEPH_CON_S_V2_SESSION_RECONNECT);
  2577. goto nothing_pending;
  2578. }
  2579. switch (con->v2.out_state) {
  2580. case OUT_S_QUEUE_DATA:
  2581. WARN_ON(!con->out_msg);
  2582. queue_data(con);
  2583. goto populated;
  2584. case OUT_S_QUEUE_DATA_CONT:
  2585. WARN_ON(!con->out_msg);
  2586. queue_data_cont(con);
  2587. goto populated;
  2588. case OUT_S_QUEUE_ENC_PAGE:
  2589. queue_enc_page(con);
  2590. goto populated;
  2591. case OUT_S_QUEUE_ZEROS:
  2592. WARN_ON(con->out_msg); /* revoked */
  2593. queue_zeros(con);
  2594. goto populated;
  2595. case OUT_S_FINISH_MESSAGE:
  2596. finish_message(con);
  2597. break;
  2598. case OUT_S_GET_NEXT:
  2599. break;
  2600. default:
  2601. WARN(1, "bad out_state %d", con->v2.out_state);
  2602. return -EINVAL;
  2603. }
  2604. WARN_ON(con->v2.out_state != OUT_S_GET_NEXT);
  2605. if (ceph_con_flag_test_and_clear(con, CEPH_CON_F_KEEPALIVE_PENDING)) {
  2606. ret = prepare_keepalive2(con);
  2607. if (ret) {
  2608. pr_err("prepare_keepalive2 failed: %d\n", ret);
  2609. return ret;
  2610. }
  2611. } else if (!list_empty(&con->out_queue)) {
  2612. ceph_con_get_out_msg(con);
  2613. ret = prepare_message(con);
  2614. if (ret) {
  2615. pr_err("prepare_message failed: %d\n", ret);
  2616. return ret;
  2617. }
  2618. } else if (con->in_seq > con->in_seq_acked) {
  2619. ret = prepare_ack(con);
  2620. if (ret) {
  2621. pr_err("prepare_ack failed: %d\n", ret);
  2622. return ret;
  2623. }
  2624. } else {
  2625. goto nothing_pending;
  2626. }
  2627. populated:
  2628. if (WARN_ON(!iov_iter_count(&con->v2.out_iter)))
  2629. return -ENODATA;
  2630. dout("%s con %p populated %zu\n", __func__, con,
  2631. iov_iter_count(&con->v2.out_iter));
  2632. return 1;
  2633. nothing_pending:
  2634. WARN_ON(iov_iter_count(&con->v2.out_iter));
  2635. dout("%s con %p nothing pending\n", __func__, con);
  2636. ceph_con_flag_clear(con, CEPH_CON_F_WRITE_PENDING);
  2637. return 0;
  2638. }
  2639. int ceph_con_v2_try_write(struct ceph_connection *con)
  2640. {
  2641. int ret;
  2642. dout("%s con %p state %d have %zu\n", __func__, con, con->state,
  2643. iov_iter_count(&con->v2.out_iter));
  2644. /* open the socket first? */
  2645. if (con->state == CEPH_CON_S_PREOPEN) {
  2646. WARN_ON(con->peer_addr.type != CEPH_ENTITY_ADDR_TYPE_MSGR2);
  2647. /*
  2648. * Always bump global_seq. Bump connect_seq only if
  2649. * there is a session (i.e. we are reconnecting and will
  2650. * send session_reconnect instead of client_ident).
  2651. */
  2652. con->v2.global_seq = ceph_get_global_seq(con->msgr, 0);
  2653. if (con->v2.server_cookie)
  2654. con->v2.connect_seq++;
  2655. ret = prepare_read_banner_prefix(con);
  2656. if (ret) {
  2657. pr_err("prepare_read_banner_prefix failed: %d\n", ret);
  2658. con->error_msg = "connect error";
  2659. return ret;
  2660. }
  2661. reset_out_kvecs(con);
  2662. ret = prepare_banner(con);
  2663. if (ret) {
  2664. pr_err("prepare_banner failed: %d\n", ret);
  2665. con->error_msg = "connect error";
  2666. return ret;
  2667. }
  2668. ret = ceph_tcp_connect(con);
  2669. if (ret) {
  2670. pr_err("ceph_tcp_connect failed: %d\n", ret);
  2671. con->error_msg = "connect error";
  2672. return ret;
  2673. }
  2674. }
  2675. if (!iov_iter_count(&con->v2.out_iter)) {
  2676. ret = populate_out_iter(con);
  2677. if (ret <= 0) {
  2678. if (ret && ret != -EAGAIN && !con->error_msg)
  2679. con->error_msg = "write processing error";
  2680. return ret;
  2681. }
  2682. }
  2683. tcp_sock_set_cork(con->sock->sk, true);
  2684. for (;;) {
  2685. ret = ceph_tcp_send(con);
  2686. if (ret <= 0)
  2687. break;
  2688. ret = populate_out_iter(con);
  2689. if (ret <= 0) {
  2690. if (ret && ret != -EAGAIN && !con->error_msg)
  2691. con->error_msg = "write processing error";
  2692. break;
  2693. }
  2694. }
  2695. tcp_sock_set_cork(con->sock->sk, false);
  2696. return ret;
  2697. }
  2698. static u32 crc32c_zeros(u32 crc, int zero_len)
  2699. {
  2700. int len;
  2701. while (zero_len) {
  2702. len = min(zero_len, (int)PAGE_SIZE);
  2703. crc = crc32c(crc, page_address(ceph_zero_page), len);
  2704. zero_len -= len;
  2705. }
  2706. return crc;
  2707. }
  2708. static void prepare_zero_front(struct ceph_connection *con, int resid)
  2709. {
  2710. int sent;
  2711. WARN_ON(!resid || resid > front_len(con->out_msg));
  2712. sent = front_len(con->out_msg) - resid;
  2713. dout("%s con %p sent %d resid %d\n", __func__, con, sent, resid);
  2714. if (sent) {
  2715. con->v2.out_epil.front_crc =
  2716. crc32c(-1, con->out_msg->front.iov_base, sent);
  2717. con->v2.out_epil.front_crc =
  2718. crc32c_zeros(con->v2.out_epil.front_crc, resid);
  2719. } else {
  2720. con->v2.out_epil.front_crc = crc32c_zeros(-1, resid);
  2721. }
  2722. con->v2.out_iter.count -= resid;
  2723. out_zero_add(con, resid);
  2724. }
  2725. static void prepare_zero_middle(struct ceph_connection *con, int resid)
  2726. {
  2727. int sent;
  2728. WARN_ON(!resid || resid > middle_len(con->out_msg));
  2729. sent = middle_len(con->out_msg) - resid;
  2730. dout("%s con %p sent %d resid %d\n", __func__, con, sent, resid);
  2731. if (sent) {
  2732. con->v2.out_epil.middle_crc =
  2733. crc32c(-1, con->out_msg->middle->vec.iov_base, sent);
  2734. con->v2.out_epil.middle_crc =
  2735. crc32c_zeros(con->v2.out_epil.middle_crc, resid);
  2736. } else {
  2737. con->v2.out_epil.middle_crc = crc32c_zeros(-1, resid);
  2738. }
  2739. con->v2.out_iter.count -= resid;
  2740. out_zero_add(con, resid);
  2741. }
  2742. static void prepare_zero_data(struct ceph_connection *con)
  2743. {
  2744. dout("%s con %p\n", __func__, con);
  2745. con->v2.out_epil.data_crc = crc32c_zeros(-1, data_len(con->out_msg));
  2746. out_zero_add(con, data_len(con->out_msg));
  2747. }
  2748. static void revoke_at_queue_data(struct ceph_connection *con)
  2749. {
  2750. int boundary;
  2751. int resid;
  2752. WARN_ON(!data_len(con->out_msg));
  2753. WARN_ON(!iov_iter_is_kvec(&con->v2.out_iter));
  2754. resid = iov_iter_count(&con->v2.out_iter);
  2755. boundary = front_len(con->out_msg) + middle_len(con->out_msg);
  2756. if (resid > boundary) {
  2757. resid -= boundary;
  2758. WARN_ON(resid > MESSAGE_HEAD_PLAIN_LEN);
  2759. dout("%s con %p was sending head\n", __func__, con);
  2760. if (front_len(con->out_msg))
  2761. prepare_zero_front(con, front_len(con->out_msg));
  2762. if (middle_len(con->out_msg))
  2763. prepare_zero_middle(con, middle_len(con->out_msg));
  2764. prepare_zero_data(con);
  2765. WARN_ON(iov_iter_count(&con->v2.out_iter) != resid);
  2766. con->v2.out_state = OUT_S_QUEUE_ZEROS;
  2767. return;
  2768. }
  2769. boundary = middle_len(con->out_msg);
  2770. if (resid > boundary) {
  2771. resid -= boundary;
  2772. dout("%s con %p was sending front\n", __func__, con);
  2773. prepare_zero_front(con, resid);
  2774. if (middle_len(con->out_msg))
  2775. prepare_zero_middle(con, middle_len(con->out_msg));
  2776. prepare_zero_data(con);
  2777. queue_zeros(con);
  2778. return;
  2779. }
  2780. WARN_ON(!resid);
  2781. dout("%s con %p was sending middle\n", __func__, con);
  2782. prepare_zero_middle(con, resid);
  2783. prepare_zero_data(con);
  2784. queue_zeros(con);
  2785. }
  2786. static void revoke_at_queue_data_cont(struct ceph_connection *con)
  2787. {
  2788. int sent, resid; /* current piece of data */
  2789. WARN_ON(!data_len(con->out_msg));
  2790. WARN_ON(!iov_iter_is_bvec(&con->v2.out_iter));
  2791. resid = iov_iter_count(&con->v2.out_iter);
  2792. WARN_ON(!resid || resid > con->v2.out_bvec.bv_len);
  2793. sent = con->v2.out_bvec.bv_len - resid;
  2794. dout("%s con %p sent %d resid %d\n", __func__, con, sent, resid);
  2795. if (sent) {
  2796. con->v2.out_epil.data_crc = ceph_crc32c_page(
  2797. con->v2.out_epil.data_crc, con->v2.out_bvec.bv_page,
  2798. con->v2.out_bvec.bv_offset, sent);
  2799. ceph_msg_data_advance(&con->v2.out_cursor, sent);
  2800. }
  2801. WARN_ON(resid > con->v2.out_cursor.total_resid);
  2802. con->v2.out_epil.data_crc = crc32c_zeros(con->v2.out_epil.data_crc,
  2803. con->v2.out_cursor.total_resid);
  2804. con->v2.out_iter.count -= resid;
  2805. out_zero_add(con, con->v2.out_cursor.total_resid);
  2806. queue_zeros(con);
  2807. }
  2808. static void revoke_at_finish_message(struct ceph_connection *con)
  2809. {
  2810. int boundary;
  2811. int resid;
  2812. WARN_ON(!iov_iter_is_kvec(&con->v2.out_iter));
  2813. resid = iov_iter_count(&con->v2.out_iter);
  2814. if (!front_len(con->out_msg) && !middle_len(con->out_msg) &&
  2815. !data_len(con->out_msg)) {
  2816. WARN_ON(!resid || resid > MESSAGE_HEAD_PLAIN_LEN);
  2817. dout("%s con %p was sending head (empty message) - noop\n",
  2818. __func__, con);
  2819. return;
  2820. }
  2821. boundary = front_len(con->out_msg) + middle_len(con->out_msg) +
  2822. CEPH_EPILOGUE_PLAIN_LEN;
  2823. if (resid > boundary) {
  2824. resid -= boundary;
  2825. WARN_ON(resid > MESSAGE_HEAD_PLAIN_LEN);
  2826. dout("%s con %p was sending head\n", __func__, con);
  2827. if (front_len(con->out_msg))
  2828. prepare_zero_front(con, front_len(con->out_msg));
  2829. if (middle_len(con->out_msg))
  2830. prepare_zero_middle(con, middle_len(con->out_msg));
  2831. con->v2.out_iter.count -= CEPH_EPILOGUE_PLAIN_LEN;
  2832. WARN_ON(iov_iter_count(&con->v2.out_iter) != resid);
  2833. con->v2.out_state = OUT_S_QUEUE_ZEROS;
  2834. return;
  2835. }
  2836. boundary = middle_len(con->out_msg) + CEPH_EPILOGUE_PLAIN_LEN;
  2837. if (resid > boundary) {
  2838. resid -= boundary;
  2839. dout("%s con %p was sending front\n", __func__, con);
  2840. prepare_zero_front(con, resid);
  2841. if (middle_len(con->out_msg))
  2842. prepare_zero_middle(con, middle_len(con->out_msg));
  2843. con->v2.out_iter.count -= CEPH_EPILOGUE_PLAIN_LEN;
  2844. queue_zeros(con);
  2845. return;
  2846. }
  2847. boundary = CEPH_EPILOGUE_PLAIN_LEN;
  2848. if (resid > boundary) {
  2849. resid -= boundary;
  2850. dout("%s con %p was sending middle\n", __func__, con);
  2851. prepare_zero_middle(con, resid);
  2852. con->v2.out_iter.count -= CEPH_EPILOGUE_PLAIN_LEN;
  2853. queue_zeros(con);
  2854. return;
  2855. }
  2856. WARN_ON(!resid);
  2857. dout("%s con %p was sending epilogue - noop\n", __func__, con);
  2858. }
  2859. void ceph_con_v2_revoke(struct ceph_connection *con)
  2860. {
  2861. WARN_ON(con->v2.out_zero);
  2862. if (con_secure(con)) {
  2863. WARN_ON(con->v2.out_state != OUT_S_QUEUE_ENC_PAGE &&
  2864. con->v2.out_state != OUT_S_FINISH_MESSAGE);
  2865. dout("%s con %p secure - noop\n", __func__, con);
  2866. return;
  2867. }
  2868. switch (con->v2.out_state) {
  2869. case OUT_S_QUEUE_DATA:
  2870. revoke_at_queue_data(con);
  2871. break;
  2872. case OUT_S_QUEUE_DATA_CONT:
  2873. revoke_at_queue_data_cont(con);
  2874. break;
  2875. case OUT_S_FINISH_MESSAGE:
  2876. revoke_at_finish_message(con);
  2877. break;
  2878. default:
  2879. WARN(1, "bad out_state %d", con->v2.out_state);
  2880. break;
  2881. }
  2882. }
  2883. static void revoke_at_prepare_read_data(struct ceph_connection *con)
  2884. {
  2885. int remaining;
  2886. int resid;
  2887. WARN_ON(con_secure(con));
  2888. WARN_ON(!data_len(con->in_msg));
  2889. WARN_ON(!iov_iter_is_kvec(&con->v2.in_iter));
  2890. resid = iov_iter_count(&con->v2.in_iter);
  2891. WARN_ON(!resid);
  2892. remaining = data_len(con->in_msg) + CEPH_EPILOGUE_PLAIN_LEN;
  2893. dout("%s con %p resid %d remaining %d\n", __func__, con, resid,
  2894. remaining);
  2895. con->v2.in_iter.count -= resid;
  2896. set_in_skip(con, resid + remaining);
  2897. con->v2.in_state = IN_S_FINISH_SKIP;
  2898. }
  2899. static void revoke_at_prepare_read_data_cont(struct ceph_connection *con)
  2900. {
  2901. int recved, resid; /* current piece of data */
  2902. int remaining;
  2903. WARN_ON(con_secure(con));
  2904. WARN_ON(!data_len(con->in_msg));
  2905. WARN_ON(!iov_iter_is_bvec(&con->v2.in_iter));
  2906. resid = iov_iter_count(&con->v2.in_iter);
  2907. WARN_ON(!resid || resid > con->v2.in_bvec.bv_len);
  2908. recved = con->v2.in_bvec.bv_len - resid;
  2909. dout("%s con %p recved %d resid %d\n", __func__, con, recved, resid);
  2910. if (recved)
  2911. ceph_msg_data_advance(&con->v2.in_cursor, recved);
  2912. WARN_ON(resid > con->v2.in_cursor.total_resid);
  2913. remaining = CEPH_EPILOGUE_PLAIN_LEN;
  2914. dout("%s con %p total_resid %zu remaining %d\n", __func__, con,
  2915. con->v2.in_cursor.total_resid, remaining);
  2916. con->v2.in_iter.count -= resid;
  2917. set_in_skip(con, con->v2.in_cursor.total_resid + remaining);
  2918. con->v2.in_state = IN_S_FINISH_SKIP;
  2919. }
  2920. static void revoke_at_prepare_read_enc_page(struct ceph_connection *con)
  2921. {
  2922. int resid; /* current enc page (not necessarily data) */
  2923. WARN_ON(!con_secure(con));
  2924. WARN_ON(!iov_iter_is_bvec(&con->v2.in_iter));
  2925. resid = iov_iter_count(&con->v2.in_iter);
  2926. WARN_ON(!resid || resid > con->v2.in_bvec.bv_len);
  2927. dout("%s con %p resid %d enc_resid %d\n", __func__, con, resid,
  2928. con->v2.in_enc_resid);
  2929. con->v2.in_iter.count -= resid;
  2930. set_in_skip(con, resid + con->v2.in_enc_resid);
  2931. con->v2.in_state = IN_S_FINISH_SKIP;
  2932. }
  2933. static void revoke_at_handle_epilogue(struct ceph_connection *con)
  2934. {
  2935. int resid;
  2936. resid = iov_iter_count(&con->v2.in_iter);
  2937. WARN_ON(!resid);
  2938. dout("%s con %p resid %d\n", __func__, con, resid);
  2939. con->v2.in_iter.count -= resid;
  2940. set_in_skip(con, resid);
  2941. con->v2.in_state = IN_S_FINISH_SKIP;
  2942. }
  2943. void ceph_con_v2_revoke_incoming(struct ceph_connection *con)
  2944. {
  2945. switch (con->v2.in_state) {
  2946. case IN_S_PREPARE_READ_DATA:
  2947. revoke_at_prepare_read_data(con);
  2948. break;
  2949. case IN_S_PREPARE_READ_DATA_CONT:
  2950. revoke_at_prepare_read_data_cont(con);
  2951. break;
  2952. case IN_S_PREPARE_READ_ENC_PAGE:
  2953. revoke_at_prepare_read_enc_page(con);
  2954. break;
  2955. case IN_S_HANDLE_EPILOGUE:
  2956. revoke_at_handle_epilogue(con);
  2957. break;
  2958. default:
  2959. WARN(1, "bad in_state %d", con->v2.in_state);
  2960. break;
  2961. }
  2962. }
  2963. bool ceph_con_v2_opened(struct ceph_connection *con)
  2964. {
  2965. return con->v2.peer_global_seq;
  2966. }
  2967. void ceph_con_v2_reset_session(struct ceph_connection *con)
  2968. {
  2969. con->v2.client_cookie = 0;
  2970. con->v2.server_cookie = 0;
  2971. con->v2.global_seq = 0;
  2972. con->v2.connect_seq = 0;
  2973. con->v2.peer_global_seq = 0;
  2974. }
  2975. void ceph_con_v2_reset_protocol(struct ceph_connection *con)
  2976. {
  2977. iov_iter_truncate(&con->v2.in_iter, 0);
  2978. iov_iter_truncate(&con->v2.out_iter, 0);
  2979. con->v2.out_zero = 0;
  2980. clear_in_sign_kvecs(con);
  2981. clear_out_sign_kvecs(con);
  2982. free_conn_bufs(con);
  2983. if (con->v2.in_enc_pages) {
  2984. WARN_ON(!con->v2.in_enc_page_cnt);
  2985. ceph_release_page_vector(con->v2.in_enc_pages,
  2986. con->v2.in_enc_page_cnt);
  2987. con->v2.in_enc_pages = NULL;
  2988. con->v2.in_enc_page_cnt = 0;
  2989. }
  2990. if (con->v2.out_enc_pages) {
  2991. WARN_ON(!con->v2.out_enc_page_cnt);
  2992. ceph_release_page_vector(con->v2.out_enc_pages,
  2993. con->v2.out_enc_page_cnt);
  2994. con->v2.out_enc_pages = NULL;
  2995. con->v2.out_enc_page_cnt = 0;
  2996. }
  2997. con->v2.con_mode = CEPH_CON_MODE_UNKNOWN;
  2998. memzero_explicit(&con->v2.in_gcm_nonce, CEPH_GCM_IV_LEN);
  2999. memzero_explicit(&con->v2.out_gcm_nonce, CEPH_GCM_IV_LEN);
  3000. if (con->v2.hmac_tfm) {
  3001. crypto_free_shash(con->v2.hmac_tfm);
  3002. con->v2.hmac_tfm = NULL;
  3003. }
  3004. if (con->v2.gcm_req) {
  3005. aead_request_free(con->v2.gcm_req);
  3006. con->v2.gcm_req = NULL;
  3007. }
  3008. if (con->v2.gcm_tfm) {
  3009. crypto_free_aead(con->v2.gcm_tfm);
  3010. con->v2.gcm_tfm = NULL;
  3011. }
  3012. }