messenger_v2.c 98 KB

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