messenger_v2.c 98 KB

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