connect.c 106 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856
  1. /*
  2. * fs/cifs/connect.c
  3. *
  4. * Copyright (C) International Business Machines Corp., 2002,2009
  5. * Author(s): Steve French (sfrench@us.ibm.com)
  6. *
  7. * This library is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU Lesser General Public License as published
  9. * by the Free Software Foundation; either version 2.1 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This library is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
  15. * the GNU Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public License
  18. * along with this library; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20. */
  21. #include <linux/fs.h>
  22. #include <linux/net.h>
  23. #include <linux/string.h>
  24. #include <linux/list.h>
  25. #include <linux/wait.h>
  26. #include <linux/slab.h>
  27. #include <linux/pagemap.h>
  28. #include <linux/ctype.h>
  29. #include <linux/utsname.h>
  30. #include <linux/mempool.h>
  31. #include <linux/delay.h>
  32. #include <linux/completion.h>
  33. #include <linux/kthread.h>
  34. #include <linux/pagevec.h>
  35. #include <linux/freezer.h>
  36. #include <linux/namei.h>
  37. #include <asm/uaccess.h>
  38. #include <asm/processor.h>
  39. #include <linux/inet.h>
  40. #include <net/ipv6.h>
  41. #include "cifspdu.h"
  42. #include "cifsglob.h"
  43. #include "cifsproto.h"
  44. #include "cifs_unicode.h"
  45. #include "cifs_debug.h"
  46. #include "cifs_fs_sb.h"
  47. #include "ntlmssp.h"
  48. #include "nterr.h"
  49. #include "rfc1002pdu.h"
  50. #include "fscache.h"
  51. #define CIFS_PORT 445
  52. #define RFC1001_PORT 139
  53. /* SMB echo "timeout" -- FIXME: tunable? */
  54. #define SMB_ECHO_INTERVAL (60 * HZ)
  55. extern mempool_t *cifs_req_poolp;
  56. /* FIXME: should these be tunable? */
  57. #define TLINK_ERROR_EXPIRE (1 * HZ)
  58. #define TLINK_IDLE_EXPIRE (600 * HZ)
  59. static int ip_connect(struct TCP_Server_Info *server);
  60. static int generic_ip_connect(struct TCP_Server_Info *server);
  61. static void tlink_rb_insert(struct rb_root *root, struct tcon_link *new_tlink);
  62. static void cifs_prune_tlinks(struct work_struct *work);
  63. static int cifs_setup_volume_info(struct smb_vol *volume_info, char *mount_data,
  64. const char *devname);
  65. /*
  66. * cifs tcp session reconnection
  67. *
  68. * mark tcp session as reconnecting so temporarily locked
  69. * mark all smb sessions as reconnecting for tcp session
  70. * reconnect tcp session
  71. * wake up waiters on reconnection? - (not needed currently)
  72. */
  73. static int
  74. cifs_reconnect(struct TCP_Server_Info *server)
  75. {
  76. int rc = 0;
  77. struct list_head *tmp, *tmp2;
  78. struct cifs_ses *ses;
  79. struct cifs_tcon *tcon;
  80. struct mid_q_entry *mid_entry;
  81. struct list_head retry_list;
  82. spin_lock(&GlobalMid_Lock);
  83. if (server->tcpStatus == CifsExiting) {
  84. /* the demux thread will exit normally
  85. next time through the loop */
  86. spin_unlock(&GlobalMid_Lock);
  87. return rc;
  88. } else
  89. server->tcpStatus = CifsNeedReconnect;
  90. spin_unlock(&GlobalMid_Lock);
  91. server->maxBuf = 0;
  92. cFYI(1, "Reconnecting tcp session");
  93. /* before reconnecting the tcp session, mark the smb session (uid)
  94. and the tid bad so they are not used until reconnected */
  95. cFYI(1, "%s: marking sessions and tcons for reconnect", __func__);
  96. spin_lock(&cifs_tcp_ses_lock);
  97. list_for_each(tmp, &server->smb_ses_list) {
  98. ses = list_entry(tmp, struct cifs_ses, smb_ses_list);
  99. ses->need_reconnect = true;
  100. ses->ipc_tid = 0;
  101. list_for_each(tmp2, &ses->tcon_list) {
  102. tcon = list_entry(tmp2, struct cifs_tcon, tcon_list);
  103. tcon->need_reconnect = true;
  104. }
  105. }
  106. spin_unlock(&cifs_tcp_ses_lock);
  107. /* do not want to be sending data on a socket we are freeing */
  108. cFYI(1, "%s: tearing down socket", __func__);
  109. mutex_lock(&server->srv_mutex);
  110. if (server->ssocket) {
  111. cFYI(1, "State: 0x%x Flags: 0x%lx", server->ssocket->state,
  112. server->ssocket->flags);
  113. kernel_sock_shutdown(server->ssocket, SHUT_WR);
  114. cFYI(1, "Post shutdown state: 0x%x Flags: 0x%lx",
  115. server->ssocket->state,
  116. server->ssocket->flags);
  117. sock_release(server->ssocket);
  118. server->ssocket = NULL;
  119. }
  120. server->sequence_number = 0;
  121. server->session_estab = false;
  122. kfree(server->session_key.response);
  123. server->session_key.response = NULL;
  124. server->session_key.len = 0;
  125. server->lstrp = jiffies;
  126. mutex_unlock(&server->srv_mutex);
  127. /* mark submitted MIDs for retry and issue callback */
  128. INIT_LIST_HEAD(&retry_list);
  129. cFYI(1, "%s: moving mids to private list", __func__);
  130. spin_lock(&GlobalMid_Lock);
  131. list_for_each_safe(tmp, tmp2, &server->pending_mid_q) {
  132. mid_entry = list_entry(tmp, struct mid_q_entry, qhead);
  133. if (mid_entry->midState == MID_REQUEST_SUBMITTED)
  134. mid_entry->midState = MID_RETRY_NEEDED;
  135. list_move(&mid_entry->qhead, &retry_list);
  136. }
  137. spin_unlock(&GlobalMid_Lock);
  138. cFYI(1, "%s: issuing mid callbacks", __func__);
  139. list_for_each_safe(tmp, tmp2, &retry_list) {
  140. mid_entry = list_entry(tmp, struct mid_q_entry, qhead);
  141. list_del_init(&mid_entry->qhead);
  142. mid_entry->callback(mid_entry);
  143. }
  144. do {
  145. try_to_freeze();
  146. /* we should try only the port we connected to before */
  147. rc = generic_ip_connect(server);
  148. if (rc) {
  149. cFYI(1, "reconnect error %d", rc);
  150. msleep(3000);
  151. } else {
  152. atomic_inc(&tcpSesReconnectCount);
  153. spin_lock(&GlobalMid_Lock);
  154. if (server->tcpStatus != CifsExiting)
  155. server->tcpStatus = CifsNeedNegotiate;
  156. spin_unlock(&GlobalMid_Lock);
  157. }
  158. } while (server->tcpStatus == CifsNeedReconnect);
  159. return rc;
  160. }
  161. /*
  162. return codes:
  163. 0 not a transact2, or all data present
  164. >0 transact2 with that much data missing
  165. -EINVAL = invalid transact2
  166. */
  167. static int check2ndT2(struct smb_hdr *pSMB)
  168. {
  169. struct smb_t2_rsp *pSMBt;
  170. int remaining;
  171. __u16 total_data_size, data_in_this_rsp;
  172. if (pSMB->Command != SMB_COM_TRANSACTION2)
  173. return 0;
  174. /* check for plausible wct, bcc and t2 data and parm sizes */
  175. /* check for parm and data offset going beyond end of smb */
  176. if (pSMB->WordCount != 10) { /* coalesce_t2 depends on this */
  177. cFYI(1, "invalid transact2 word count");
  178. return -EINVAL;
  179. }
  180. pSMBt = (struct smb_t2_rsp *)pSMB;
  181. total_data_size = get_unaligned_le16(&pSMBt->t2_rsp.TotalDataCount);
  182. data_in_this_rsp = get_unaligned_le16(&pSMBt->t2_rsp.DataCount);
  183. if (total_data_size == data_in_this_rsp)
  184. return 0;
  185. else if (total_data_size < data_in_this_rsp) {
  186. cFYI(1, "total data %d smaller than data in frame %d",
  187. total_data_size, data_in_this_rsp);
  188. return -EINVAL;
  189. }
  190. remaining = total_data_size - data_in_this_rsp;
  191. cFYI(1, "missing %d bytes from transact2, check next response",
  192. remaining);
  193. if (total_data_size > CIFSMaxBufSize) {
  194. cERROR(1, "TotalDataSize %d is over maximum buffer %d",
  195. total_data_size, CIFSMaxBufSize);
  196. return -EINVAL;
  197. }
  198. return remaining;
  199. }
  200. static int coalesce_t2(struct smb_hdr *psecond, struct smb_hdr *pTargetSMB)
  201. {
  202. struct smb_t2_rsp *pSMB2 = (struct smb_t2_rsp *)psecond;
  203. struct smb_t2_rsp *pSMBt = (struct smb_t2_rsp *)pTargetSMB;
  204. char *data_area_of_target;
  205. char *data_area_of_buf2;
  206. int remaining;
  207. unsigned int byte_count, total_in_buf;
  208. __u16 total_data_size, total_in_buf2;
  209. total_data_size = get_unaligned_le16(&pSMBt->t2_rsp.TotalDataCount);
  210. if (total_data_size !=
  211. get_unaligned_le16(&pSMB2->t2_rsp.TotalDataCount))
  212. cFYI(1, "total data size of primary and secondary t2 differ");
  213. total_in_buf = get_unaligned_le16(&pSMBt->t2_rsp.DataCount);
  214. remaining = total_data_size - total_in_buf;
  215. if (remaining < 0)
  216. return -EPROTO;
  217. if (remaining == 0) /* nothing to do, ignore */
  218. return 0;
  219. total_in_buf2 = get_unaligned_le16(&pSMB2->t2_rsp.DataCount);
  220. if (remaining < total_in_buf2) {
  221. cFYI(1, "transact2 2nd response contains too much data");
  222. }
  223. /* find end of first SMB data area */
  224. data_area_of_target = (char *)&pSMBt->hdr.Protocol +
  225. get_unaligned_le16(&pSMBt->t2_rsp.DataOffset);
  226. /* validate target area */
  227. data_area_of_buf2 = (char *)&pSMB2->hdr.Protocol +
  228. get_unaligned_le16(&pSMB2->t2_rsp.DataOffset);
  229. data_area_of_target += total_in_buf;
  230. /* copy second buffer into end of first buffer */
  231. total_in_buf += total_in_buf2;
  232. /* is the result too big for the field? */
  233. if (total_in_buf > USHRT_MAX)
  234. return -EPROTO;
  235. put_unaligned_le16(total_in_buf, &pSMBt->t2_rsp.DataCount);
  236. /* fix up the BCC */
  237. byte_count = get_bcc(pTargetSMB);
  238. byte_count += total_in_buf2;
  239. /* is the result too big for the field? */
  240. if (byte_count > USHRT_MAX)
  241. return -EPROTO;
  242. put_bcc(byte_count, pTargetSMB);
  243. byte_count = be32_to_cpu(pTargetSMB->smb_buf_length);
  244. byte_count += total_in_buf2;
  245. /* don't allow buffer to overflow */
  246. if (byte_count > CIFSMaxBufSize)
  247. return -ENOBUFS;
  248. pTargetSMB->smb_buf_length = cpu_to_be32(byte_count);
  249. memcpy(data_area_of_target, data_area_of_buf2, total_in_buf2);
  250. if (remaining == total_in_buf2) {
  251. cFYI(1, "found the last secondary response");
  252. return 0; /* we are done */
  253. } else /* more responses to go */
  254. return 1;
  255. }
  256. static void
  257. cifs_echo_request(struct work_struct *work)
  258. {
  259. int rc;
  260. struct TCP_Server_Info *server = container_of(work,
  261. struct TCP_Server_Info, echo.work);
  262. /*
  263. * We cannot send an echo until the NEGOTIATE_PROTOCOL request is
  264. * done, which is indicated by maxBuf != 0. Also, no need to ping if
  265. * we got a response recently
  266. */
  267. if (server->maxBuf == 0 ||
  268. time_before(jiffies, server->lstrp + SMB_ECHO_INTERVAL - HZ))
  269. goto requeue_echo;
  270. rc = CIFSSMBEcho(server);
  271. if (rc)
  272. cFYI(1, "Unable to send echo request to server: %s",
  273. server->hostname);
  274. requeue_echo:
  275. queue_delayed_work(system_nrt_wq, &server->echo, SMB_ECHO_INTERVAL);
  276. }
  277. static bool
  278. allocate_buffers(struct TCP_Server_Info *server)
  279. {
  280. if (!server->bigbuf) {
  281. server->bigbuf = (char *)cifs_buf_get();
  282. if (!server->bigbuf) {
  283. cERROR(1, "No memory for large SMB response");
  284. msleep(3000);
  285. /* retry will check if exiting */
  286. return false;
  287. }
  288. } else if (server->large_buf) {
  289. /* we are reusing a dirty large buf, clear its start */
  290. memset(server->bigbuf, 0, sizeof(struct smb_hdr));
  291. }
  292. if (!server->smallbuf) {
  293. server->smallbuf = (char *)cifs_small_buf_get();
  294. if (!server->smallbuf) {
  295. cERROR(1, "No memory for SMB response");
  296. msleep(1000);
  297. /* retry will check if exiting */
  298. return false;
  299. }
  300. /* beginning of smb buffer is cleared in our buf_get */
  301. } else {
  302. /* if existing small buf clear beginning */
  303. memset(server->smallbuf, 0, sizeof(struct smb_hdr));
  304. }
  305. return true;
  306. }
  307. static bool
  308. server_unresponsive(struct TCP_Server_Info *server)
  309. {
  310. if (echo_retries > 0 && server->tcpStatus == CifsGood &&
  311. time_after(jiffies, server->lstrp +
  312. (echo_retries * SMB_ECHO_INTERVAL))) {
  313. cERROR(1, "Server %s has not responded in %d seconds. "
  314. "Reconnecting...", server->hostname,
  315. (echo_retries * SMB_ECHO_INTERVAL / HZ));
  316. cifs_reconnect(server);
  317. wake_up(&server->response_q);
  318. return true;
  319. }
  320. return false;
  321. }
  322. /*
  323. * kvec_array_init - clone a kvec array, and advance into it
  324. * @new: pointer to memory for cloned array
  325. * @iov: pointer to original array
  326. * @nr_segs: number of members in original array
  327. * @bytes: number of bytes to advance into the cloned array
  328. *
  329. * This function will copy the array provided in iov to a section of memory
  330. * and advance the specified number of bytes into the new array. It returns
  331. * the number of segments in the new array. "new" must be at least as big as
  332. * the original iov array.
  333. */
  334. static unsigned int
  335. kvec_array_init(struct kvec *new, struct kvec *iov, unsigned int nr_segs,
  336. size_t bytes)
  337. {
  338. size_t base = 0;
  339. while (bytes || !iov->iov_len) {
  340. int copy = min(bytes, iov->iov_len);
  341. bytes -= copy;
  342. base += copy;
  343. if (iov->iov_len == base) {
  344. iov++;
  345. nr_segs--;
  346. base = 0;
  347. }
  348. }
  349. memcpy(new, iov, sizeof(*iov) * nr_segs);
  350. new->iov_base += base;
  351. new->iov_len -= base;
  352. return nr_segs;
  353. }
  354. static struct kvec *
  355. get_server_iovec(struct TCP_Server_Info *server, unsigned int nr_segs)
  356. {
  357. struct kvec *new_iov;
  358. if (server->iov && nr_segs <= server->nr_iov)
  359. return server->iov;
  360. /* not big enough -- allocate a new one and release the old */
  361. new_iov = kmalloc(sizeof(*new_iov) * nr_segs, GFP_NOFS);
  362. if (new_iov) {
  363. kfree(server->iov);
  364. server->iov = new_iov;
  365. server->nr_iov = nr_segs;
  366. }
  367. return new_iov;
  368. }
  369. static int
  370. readv_from_socket(struct TCP_Server_Info *server, struct kvec *iov_orig,
  371. unsigned int nr_segs, unsigned int to_read)
  372. {
  373. int length = 0;
  374. int total_read;
  375. unsigned int segs;
  376. struct msghdr smb_msg;
  377. struct kvec *iov;
  378. iov = get_server_iovec(server, nr_segs);
  379. if (!iov)
  380. return -ENOMEM;
  381. smb_msg.msg_control = NULL;
  382. smb_msg.msg_controllen = 0;
  383. for (total_read = 0; to_read; total_read += length, to_read -= length) {
  384. if (server_unresponsive(server)) {
  385. total_read = -EAGAIN;
  386. break;
  387. }
  388. segs = kvec_array_init(iov, iov_orig, nr_segs, total_read);
  389. length = kernel_recvmsg(server->ssocket, &smb_msg,
  390. iov, segs, to_read, 0);
  391. if (server->tcpStatus == CifsExiting) {
  392. total_read = -ESHUTDOWN;
  393. break;
  394. } else if (server->tcpStatus == CifsNeedReconnect) {
  395. cifs_reconnect(server);
  396. total_read = -EAGAIN;
  397. break;
  398. } else if (length == -ERESTARTSYS ||
  399. length == -EAGAIN ||
  400. length == -EINTR) {
  401. /*
  402. * Minimum sleep to prevent looping, allowing socket
  403. * to clear and app threads to set tcpStatus
  404. * CifsNeedReconnect if server hung.
  405. */
  406. usleep_range(1000, 2000);
  407. length = 0;
  408. continue;
  409. } else if (length <= 0) {
  410. cFYI(1, "Received no data or error: expecting %d "
  411. "got %d", to_read, length);
  412. cifs_reconnect(server);
  413. total_read = -EAGAIN;
  414. break;
  415. }
  416. }
  417. return total_read;
  418. }
  419. static int
  420. read_from_socket(struct TCP_Server_Info *server, char *buf,
  421. unsigned int to_read)
  422. {
  423. struct kvec iov;
  424. iov.iov_base = buf;
  425. iov.iov_len = to_read;
  426. return readv_from_socket(server, &iov, 1, to_read);
  427. }
  428. static bool
  429. is_smb_response(struct TCP_Server_Info *server, unsigned char type)
  430. {
  431. /*
  432. * The first byte big endian of the length field,
  433. * is actually not part of the length but the type
  434. * with the most common, zero, as regular data.
  435. */
  436. switch (type) {
  437. case RFC1002_SESSION_MESSAGE:
  438. /* Regular SMB response */
  439. return true;
  440. case RFC1002_SESSION_KEEP_ALIVE:
  441. cFYI(1, "RFC 1002 session keep alive");
  442. break;
  443. case RFC1002_POSITIVE_SESSION_RESPONSE:
  444. cFYI(1, "RFC 1002 positive session response");
  445. break;
  446. case RFC1002_NEGATIVE_SESSION_RESPONSE:
  447. /*
  448. * We get this from Windows 98 instead of an error on
  449. * SMB negprot response.
  450. */
  451. cFYI(1, "RFC 1002 negative session response");
  452. /* give server a second to clean up */
  453. msleep(1000);
  454. /*
  455. * Always try 445 first on reconnect since we get NACK
  456. * on some if we ever connected to port 139 (the NACK
  457. * is since we do not begin with RFC1001 session
  458. * initialize frame).
  459. */
  460. cifs_set_port((struct sockaddr *)&server->dstaddr, CIFS_PORT);
  461. cifs_reconnect(server);
  462. wake_up(&server->response_q);
  463. break;
  464. default:
  465. cERROR(1, "RFC 1002 unknown response type 0x%x", type);
  466. cifs_reconnect(server);
  467. }
  468. return false;
  469. }
  470. static struct mid_q_entry *
  471. find_mid(struct TCP_Server_Info *server, struct smb_hdr *buf)
  472. {
  473. struct mid_q_entry *mid;
  474. spin_lock(&GlobalMid_Lock);
  475. list_for_each_entry(mid, &server->pending_mid_q, qhead) {
  476. if (mid->mid == buf->Mid &&
  477. mid->midState == MID_REQUEST_SUBMITTED &&
  478. mid->command == buf->Command) {
  479. spin_unlock(&GlobalMid_Lock);
  480. return mid;
  481. }
  482. }
  483. spin_unlock(&GlobalMid_Lock);
  484. return NULL;
  485. }
  486. static void
  487. dequeue_mid(struct mid_q_entry *mid, int malformed)
  488. {
  489. #ifdef CONFIG_CIFS_STATS2
  490. mid->when_received = jiffies;
  491. #endif
  492. spin_lock(&GlobalMid_Lock);
  493. if (!malformed)
  494. mid->midState = MID_RESPONSE_RECEIVED;
  495. else
  496. mid->midState = MID_RESPONSE_MALFORMED;
  497. list_del_init(&mid->qhead);
  498. spin_unlock(&GlobalMid_Lock);
  499. }
  500. static void
  501. handle_mid(struct mid_q_entry *mid, struct TCP_Server_Info *server,
  502. struct smb_hdr *buf, int malformed)
  503. {
  504. if (malformed == 0 && check2ndT2(buf) > 0) {
  505. mid->multiRsp = true;
  506. if (mid->resp_buf) {
  507. /* merge response - fix up 1st*/
  508. malformed = coalesce_t2(buf, mid->resp_buf);
  509. if (malformed > 0)
  510. return;
  511. /* All parts received or packet is malformed. */
  512. mid->multiEnd = true;
  513. return dequeue_mid(mid, malformed);
  514. }
  515. if (!server->large_buf) {
  516. /*FIXME: switch to already allocated largebuf?*/
  517. cERROR(1, "1st trans2 resp needs bigbuf");
  518. } else {
  519. /* Have first buffer */
  520. mid->resp_buf = buf;
  521. mid->largeBuf = true;
  522. server->bigbuf = NULL;
  523. }
  524. return;
  525. }
  526. mid->resp_buf = buf;
  527. mid->largeBuf = server->large_buf;
  528. /* Was previous buf put in mpx struct for multi-rsp? */
  529. if (!mid->multiRsp) {
  530. /* smb buffer will be freed by user thread */
  531. if (server->large_buf)
  532. server->bigbuf = NULL;
  533. else
  534. server->smallbuf = NULL;
  535. }
  536. dequeue_mid(mid, malformed);
  537. }
  538. static void clean_demultiplex_info(struct TCP_Server_Info *server)
  539. {
  540. int length;
  541. /* take it off the list, if it's not already */
  542. spin_lock(&cifs_tcp_ses_lock);
  543. list_del_init(&server->tcp_ses_list);
  544. spin_unlock(&cifs_tcp_ses_lock);
  545. spin_lock(&GlobalMid_Lock);
  546. server->tcpStatus = CifsExiting;
  547. spin_unlock(&GlobalMid_Lock);
  548. wake_up_all(&server->response_q);
  549. /*
  550. * Check if we have blocked requests that need to free. Note that
  551. * cifs_max_pending is normally 50, but can be set at module install
  552. * time to as little as two.
  553. */
  554. spin_lock(&GlobalMid_Lock);
  555. if (atomic_read(&server->inFlight) >= cifs_max_pending)
  556. atomic_set(&server->inFlight, cifs_max_pending - 1);
  557. /*
  558. * We do not want to set the max_pending too low or we could end up
  559. * with the counter going negative.
  560. */
  561. spin_unlock(&GlobalMid_Lock);
  562. /*
  563. * Although there should not be any requests blocked on this queue it
  564. * can not hurt to be paranoid and try to wake up requests that may
  565. * haven been blocked when more than 50 at time were on the wire to the
  566. * same server - they now will see the session is in exit state and get
  567. * out of SendReceive.
  568. */
  569. wake_up_all(&server->request_q);
  570. /* give those requests time to exit */
  571. msleep(125);
  572. if (server->ssocket) {
  573. sock_release(server->ssocket);
  574. server->ssocket = NULL;
  575. }
  576. if (!list_empty(&server->pending_mid_q)) {
  577. struct list_head dispose_list;
  578. struct mid_q_entry *mid_entry;
  579. struct list_head *tmp, *tmp2;
  580. INIT_LIST_HEAD(&dispose_list);
  581. spin_lock(&GlobalMid_Lock);
  582. list_for_each_safe(tmp, tmp2, &server->pending_mid_q) {
  583. mid_entry = list_entry(tmp, struct mid_q_entry, qhead);
  584. cFYI(1, "Clearing mid 0x%x", mid_entry->mid);
  585. mid_entry->midState = MID_SHUTDOWN;
  586. list_move(&mid_entry->qhead, &dispose_list);
  587. }
  588. spin_unlock(&GlobalMid_Lock);
  589. /* now walk dispose list and issue callbacks */
  590. list_for_each_safe(tmp, tmp2, &dispose_list) {
  591. mid_entry = list_entry(tmp, struct mid_q_entry, qhead);
  592. cFYI(1, "Callback mid 0x%x", mid_entry->mid);
  593. list_del_init(&mid_entry->qhead);
  594. mid_entry->callback(mid_entry);
  595. }
  596. /* 1/8th of sec is more than enough time for them to exit */
  597. msleep(125);
  598. }
  599. if (!list_empty(&server->pending_mid_q)) {
  600. /*
  601. * mpx threads have not exited yet give them at least the smb
  602. * send timeout time for long ops.
  603. *
  604. * Due to delays on oplock break requests, we need to wait at
  605. * least 45 seconds before giving up on a request getting a
  606. * response and going ahead and killing cifsd.
  607. */
  608. cFYI(1, "Wait for exit from demultiplex thread");
  609. msleep(46000);
  610. /*
  611. * If threads still have not exited they are probably never
  612. * coming home not much else we can do but free the memory.
  613. */
  614. }
  615. kfree(server->hostname);
  616. kfree(server->iov);
  617. kfree(server);
  618. length = atomic_dec_return(&tcpSesAllocCount);
  619. if (length > 0)
  620. mempool_resize(cifs_req_poolp, length + cifs_min_rcv,
  621. GFP_KERNEL);
  622. }
  623. static int
  624. standard_receive3(struct TCP_Server_Info *server, struct mid_q_entry *mid)
  625. {
  626. int length;
  627. char *buf = server->smallbuf;
  628. struct smb_hdr *smb_buffer = (struct smb_hdr *)buf;
  629. unsigned int pdu_length = be32_to_cpu(smb_buffer->smb_buf_length);
  630. /* make sure this will fit in a large buffer */
  631. if (pdu_length > CIFSMaxBufSize + MAX_CIFS_HDR_SIZE - 4) {
  632. cERROR(1, "SMB response too long (%u bytes)",
  633. pdu_length);
  634. cifs_reconnect(server);
  635. wake_up(&server->response_q);
  636. return -EAGAIN;
  637. }
  638. /* switch to large buffer if too big for a small one */
  639. if (pdu_length > MAX_CIFS_SMALL_BUFFER_SIZE - 4) {
  640. server->large_buf = true;
  641. memcpy(server->bigbuf, server->smallbuf, server->total_read);
  642. buf = server->bigbuf;
  643. smb_buffer = (struct smb_hdr *)buf;
  644. }
  645. /* now read the rest */
  646. length = read_from_socket(server,
  647. buf + sizeof(struct smb_hdr) - 1,
  648. pdu_length - sizeof(struct smb_hdr) + 1 + 4);
  649. if (length < 0)
  650. return length;
  651. server->total_read += length;
  652. dump_smb(smb_buffer, server->total_read);
  653. /*
  654. * We know that we received enough to get to the MID as we
  655. * checked the pdu_length earlier. Now check to see
  656. * if the rest of the header is OK. We borrow the length
  657. * var for the rest of the loop to avoid a new stack var.
  658. *
  659. * 48 bytes is enough to display the header and a little bit
  660. * into the payload for debugging purposes.
  661. */
  662. length = checkSMB(smb_buffer, smb_buffer->Mid, server->total_read);
  663. if (length != 0)
  664. cifs_dump_mem("Bad SMB: ", buf,
  665. min_t(unsigned int, server->total_read, 48));
  666. if (mid)
  667. handle_mid(mid, server, smb_buffer, length);
  668. return length;
  669. }
  670. static int
  671. cifs_demultiplex_thread(void *p)
  672. {
  673. int length;
  674. struct TCP_Server_Info *server = p;
  675. unsigned int pdu_length;
  676. char *buf = NULL;
  677. struct smb_hdr *smb_buffer = NULL;
  678. struct task_struct *task_to_wake = NULL;
  679. struct mid_q_entry *mid_entry;
  680. current->flags |= PF_MEMALLOC;
  681. cFYI(1, "Demultiplex PID: %d", task_pid_nr(current));
  682. length = atomic_inc_return(&tcpSesAllocCount);
  683. if (length > 1)
  684. mempool_resize(cifs_req_poolp, length + cifs_min_rcv,
  685. GFP_KERNEL);
  686. set_freezable();
  687. while (server->tcpStatus != CifsExiting) {
  688. if (try_to_freeze())
  689. continue;
  690. if (!allocate_buffers(server))
  691. continue;
  692. server->large_buf = false;
  693. smb_buffer = (struct smb_hdr *)server->smallbuf;
  694. buf = server->smallbuf;
  695. pdu_length = 4; /* enough to get RFC1001 header */
  696. length = read_from_socket(server, buf, pdu_length);
  697. if (length < 0)
  698. continue;
  699. server->total_read = length;
  700. /*
  701. * The right amount was read from socket - 4 bytes,
  702. * so we can now interpret the length field.
  703. */
  704. pdu_length = be32_to_cpu(smb_buffer->smb_buf_length);
  705. cFYI(1, "RFC1002 header 0x%x", pdu_length);
  706. if (!is_smb_response(server, buf[0]))
  707. continue;
  708. /* make sure we have enough to get to the MID */
  709. if (pdu_length < sizeof(struct smb_hdr) - 1 - 4) {
  710. cERROR(1, "SMB response too short (%u bytes)",
  711. pdu_length);
  712. cifs_reconnect(server);
  713. wake_up(&server->response_q);
  714. continue;
  715. }
  716. /* read down to the MID */
  717. length = read_from_socket(server, buf + 4,
  718. sizeof(struct smb_hdr) - 1 - 4);
  719. if (length < 0)
  720. continue;
  721. server->total_read += length;
  722. mid_entry = find_mid(server, smb_buffer);
  723. length = standard_receive3(server, mid_entry);
  724. if (length < 0)
  725. continue;
  726. if (server->large_buf) {
  727. buf = server->bigbuf;
  728. smb_buffer = (struct smb_hdr *)buf;
  729. }
  730. server->lstrp = jiffies;
  731. if (mid_entry != NULL) {
  732. if (!mid_entry->multiRsp || mid_entry->multiEnd)
  733. mid_entry->callback(mid_entry);
  734. } else if (!is_valid_oplock_break(smb_buffer, server)) {
  735. cERROR(1, "No task to wake, unknown frame received! "
  736. "NumMids %d", atomic_read(&midCount));
  737. cifs_dump_mem("Received Data is: ", buf,
  738. sizeof(struct smb_hdr));
  739. #ifdef CONFIG_CIFS_DEBUG2
  740. cifs_dump_detail(smb_buffer);
  741. cifs_dump_mids(server);
  742. #endif /* CIFS_DEBUG2 */
  743. }
  744. } /* end while !EXITING */
  745. /* buffer usually freed in free_mid - need to free it here on exit */
  746. cifs_buf_release(server->bigbuf);
  747. if (server->smallbuf) /* no sense logging a debug message if NULL */
  748. cifs_small_buf_release(server->smallbuf);
  749. task_to_wake = xchg(&server->tsk, NULL);
  750. clean_demultiplex_info(server);
  751. /* if server->tsk was NULL then wait for a signal before exiting */
  752. if (!task_to_wake) {
  753. set_current_state(TASK_INTERRUPTIBLE);
  754. while (!signal_pending(current)) {
  755. schedule();
  756. set_current_state(TASK_INTERRUPTIBLE);
  757. }
  758. set_current_state(TASK_RUNNING);
  759. }
  760. module_put_and_exit(0);
  761. }
  762. /* extract the host portion of the UNC string */
  763. static char *
  764. extract_hostname(const char *unc)
  765. {
  766. const char *src;
  767. char *dst, *delim;
  768. unsigned int len;
  769. /* skip double chars at beginning of string */
  770. /* BB: check validity of these bytes? */
  771. src = unc + 2;
  772. /* delimiter between hostname and sharename is always '\\' now */
  773. delim = strchr(src, '\\');
  774. if (!delim)
  775. return ERR_PTR(-EINVAL);
  776. len = delim - src;
  777. dst = kmalloc((len + 1), GFP_KERNEL);
  778. if (dst == NULL)
  779. return ERR_PTR(-ENOMEM);
  780. memcpy(dst, src, len);
  781. dst[len] = '\0';
  782. return dst;
  783. }
  784. static int
  785. cifs_parse_mount_options(const char *mountdata, const char *devname,
  786. struct smb_vol *vol)
  787. {
  788. char *value, *data, *end;
  789. char *mountdata_copy = NULL, *options;
  790. int err;
  791. unsigned int temp_len, i, j;
  792. char separator[2];
  793. short int override_uid = -1;
  794. short int override_gid = -1;
  795. bool uid_specified = false;
  796. bool gid_specified = false;
  797. char *nodename = utsname()->nodename;
  798. separator[0] = ',';
  799. separator[1] = 0;
  800. /*
  801. * does not have to be perfect mapping since field is
  802. * informational, only used for servers that do not support
  803. * port 445 and it can be overridden at mount time
  804. */
  805. memset(vol->source_rfc1001_name, 0x20, RFC1001_NAME_LEN);
  806. for (i = 0; i < strnlen(nodename, RFC1001_NAME_LEN); i++)
  807. vol->source_rfc1001_name[i] = toupper(nodename[i]);
  808. vol->source_rfc1001_name[RFC1001_NAME_LEN] = 0;
  809. /* null target name indicates to use *SMBSERVR default called name
  810. if we end up sending RFC1001 session initialize */
  811. vol->target_rfc1001_name[0] = 0;
  812. vol->cred_uid = current_uid();
  813. vol->linux_uid = current_uid();
  814. vol->linux_gid = current_gid();
  815. /* default to only allowing write access to owner of the mount */
  816. vol->dir_mode = vol->file_mode = S_IRUGO | S_IXUGO | S_IWUSR;
  817. /* vol->retry default is 0 (i.e. "soft" limited retry not hard retry) */
  818. /* default is always to request posix paths. */
  819. vol->posix_paths = 1;
  820. /* default to using server inode numbers where available */
  821. vol->server_ino = 1;
  822. vol->actimeo = CIFS_DEF_ACTIMEO;
  823. if (!mountdata)
  824. goto cifs_parse_mount_err;
  825. mountdata_copy = kstrndup(mountdata, PAGE_SIZE, GFP_KERNEL);
  826. if (!mountdata_copy)
  827. goto cifs_parse_mount_err;
  828. options = mountdata_copy;
  829. end = options + strlen(options);
  830. if (strncmp(options, "sep=", 4) == 0) {
  831. if (options[4] != 0) {
  832. separator[0] = options[4];
  833. options += 5;
  834. } else {
  835. cFYI(1, "Null separator not allowed");
  836. }
  837. }
  838. vol->backupuid_specified = false; /* no backup intent for a user */
  839. vol->backupgid_specified = false; /* no backup intent for a group */
  840. while ((data = strsep(&options, separator)) != NULL) {
  841. if (!*data)
  842. continue;
  843. if ((value = strchr(data, '=')) != NULL)
  844. *value++ = '\0';
  845. /* Have to parse this before we parse for "user" */
  846. if (strnicmp(data, "user_xattr", 10) == 0) {
  847. vol->no_xattr = 0;
  848. } else if (strnicmp(data, "nouser_xattr", 12) == 0) {
  849. vol->no_xattr = 1;
  850. } else if (strnicmp(data, "user", 4) == 0) {
  851. if (!value) {
  852. printk(KERN_WARNING
  853. "CIFS: invalid or missing username\n");
  854. goto cifs_parse_mount_err;
  855. } else if (!*value) {
  856. /* null user, ie anonymous, authentication */
  857. vol->nullauth = 1;
  858. }
  859. if (strnlen(value, MAX_USERNAME_SIZE) <
  860. MAX_USERNAME_SIZE) {
  861. vol->username = kstrdup(value, GFP_KERNEL);
  862. if (!vol->username) {
  863. printk(KERN_WARNING "CIFS: no memory "
  864. "for username\n");
  865. goto cifs_parse_mount_err;
  866. }
  867. } else {
  868. printk(KERN_WARNING "CIFS: username too long\n");
  869. goto cifs_parse_mount_err;
  870. }
  871. } else if (strnicmp(data, "pass", 4) == 0) {
  872. if (!value) {
  873. vol->password = NULL;
  874. continue;
  875. } else if (value[0] == 0) {
  876. /* check if string begins with double comma
  877. since that would mean the password really
  878. does start with a comma, and would not
  879. indicate an empty string */
  880. if (value[1] != separator[0]) {
  881. vol->password = NULL;
  882. continue;
  883. }
  884. }
  885. temp_len = strlen(value);
  886. /* removed password length check, NTLM passwords
  887. can be arbitrarily long */
  888. /* if comma in password, the string will be
  889. prematurely null terminated. Commas in password are
  890. specified across the cifs mount interface by a double
  891. comma ie ,, and a comma used as in other cases ie ','
  892. as a parameter delimiter/separator is single and due
  893. to the strsep above is temporarily zeroed. */
  894. /* NB: password legally can have multiple commas and
  895. the only illegal character in a password is null */
  896. if ((value[temp_len] == 0) &&
  897. (value + temp_len < end) &&
  898. (value[temp_len+1] == separator[0])) {
  899. /* reinsert comma */
  900. value[temp_len] = separator[0];
  901. temp_len += 2; /* move after second comma */
  902. while (value[temp_len] != 0) {
  903. if (value[temp_len] == separator[0]) {
  904. if (value[temp_len+1] ==
  905. separator[0]) {
  906. /* skip second comma */
  907. temp_len++;
  908. } else {
  909. /* single comma indicating start
  910. of next parm */
  911. break;
  912. }
  913. }
  914. temp_len++;
  915. }
  916. if (value[temp_len] == 0) {
  917. options = NULL;
  918. } else {
  919. value[temp_len] = 0;
  920. /* point option to start of next parm */
  921. options = value + temp_len + 1;
  922. }
  923. /* go from value to value + temp_len condensing
  924. double commas to singles. Note that this ends up
  925. allocating a few bytes too many, which is ok */
  926. vol->password = kzalloc(temp_len, GFP_KERNEL);
  927. if (vol->password == NULL) {
  928. printk(KERN_WARNING "CIFS: no memory "
  929. "for password\n");
  930. goto cifs_parse_mount_err;
  931. }
  932. for (i = 0, j = 0; i < temp_len; i++, j++) {
  933. vol->password[j] = value[i];
  934. if (value[i] == separator[0]
  935. && value[i+1] == separator[0]) {
  936. /* skip second comma */
  937. i++;
  938. }
  939. }
  940. vol->password[j] = 0;
  941. } else {
  942. vol->password = kzalloc(temp_len+1, GFP_KERNEL);
  943. if (vol->password == NULL) {
  944. printk(KERN_WARNING "CIFS: no memory "
  945. "for password\n");
  946. goto cifs_parse_mount_err;
  947. }
  948. strcpy(vol->password, value);
  949. }
  950. } else if (!strnicmp(data, "ip", 2) ||
  951. !strnicmp(data, "addr", 4)) {
  952. if (!value || !*value) {
  953. vol->UNCip = NULL;
  954. } else if (strnlen(value, INET6_ADDRSTRLEN) <
  955. INET6_ADDRSTRLEN) {
  956. vol->UNCip = kstrdup(value, GFP_KERNEL);
  957. if (!vol->UNCip) {
  958. printk(KERN_WARNING "CIFS: no memory "
  959. "for UNC IP\n");
  960. goto cifs_parse_mount_err;
  961. }
  962. } else {
  963. printk(KERN_WARNING "CIFS: ip address "
  964. "too long\n");
  965. goto cifs_parse_mount_err;
  966. }
  967. } else if (strnicmp(data, "sec", 3) == 0) {
  968. if (!value || !*value) {
  969. cERROR(1, "no security value specified");
  970. continue;
  971. } else if (strnicmp(value, "krb5i", 5) == 0) {
  972. vol->secFlg |= CIFSSEC_MAY_KRB5 |
  973. CIFSSEC_MUST_SIGN;
  974. } else if (strnicmp(value, "krb5p", 5) == 0) {
  975. /* vol->secFlg |= CIFSSEC_MUST_SEAL |
  976. CIFSSEC_MAY_KRB5; */
  977. cERROR(1, "Krb5 cifs privacy not supported");
  978. goto cifs_parse_mount_err;
  979. } else if (strnicmp(value, "krb5", 4) == 0) {
  980. vol->secFlg |= CIFSSEC_MAY_KRB5;
  981. } else if (strnicmp(value, "ntlmsspi", 8) == 0) {
  982. vol->secFlg |= CIFSSEC_MAY_NTLMSSP |
  983. CIFSSEC_MUST_SIGN;
  984. } else if (strnicmp(value, "ntlmssp", 7) == 0) {
  985. vol->secFlg |= CIFSSEC_MAY_NTLMSSP;
  986. } else if (strnicmp(value, "ntlmv2i", 7) == 0) {
  987. vol->secFlg |= CIFSSEC_MAY_NTLMV2 |
  988. CIFSSEC_MUST_SIGN;
  989. } else if (strnicmp(value, "ntlmv2", 6) == 0) {
  990. vol->secFlg |= CIFSSEC_MAY_NTLMV2;
  991. } else if (strnicmp(value, "ntlmi", 5) == 0) {
  992. vol->secFlg |= CIFSSEC_MAY_NTLM |
  993. CIFSSEC_MUST_SIGN;
  994. } else if (strnicmp(value, "ntlm", 4) == 0) {
  995. /* ntlm is default so can be turned off too */
  996. vol->secFlg |= CIFSSEC_MAY_NTLM;
  997. } else if (strnicmp(value, "nontlm", 6) == 0) {
  998. /* BB is there a better way to do this? */
  999. vol->secFlg |= CIFSSEC_MAY_NTLMV2;
  1000. #ifdef CONFIG_CIFS_WEAK_PW_HASH
  1001. } else if (strnicmp(value, "lanman", 6) == 0) {
  1002. vol->secFlg |= CIFSSEC_MAY_LANMAN;
  1003. #endif
  1004. } else if (strnicmp(value, "none", 4) == 0) {
  1005. vol->nullauth = 1;
  1006. } else {
  1007. cERROR(1, "bad security option: %s", value);
  1008. goto cifs_parse_mount_err;
  1009. }
  1010. } else if (strnicmp(data, "vers", 3) == 0) {
  1011. if (!value || !*value) {
  1012. cERROR(1, "no protocol version specified"
  1013. " after vers= mount option");
  1014. } else if ((strnicmp(value, "cifs", 4) == 0) ||
  1015. (strnicmp(value, "1", 1) == 0)) {
  1016. /* this is the default */
  1017. continue;
  1018. }
  1019. } else if ((strnicmp(data, "unc", 3) == 0)
  1020. || (strnicmp(data, "target", 6) == 0)
  1021. || (strnicmp(data, "path", 4) == 0)) {
  1022. if (!value || !*value) {
  1023. printk(KERN_WARNING "CIFS: invalid path to "
  1024. "network resource\n");
  1025. goto cifs_parse_mount_err;
  1026. }
  1027. if ((temp_len = strnlen(value, 300)) < 300) {
  1028. vol->UNC = kmalloc(temp_len+1, GFP_KERNEL);
  1029. if (vol->UNC == NULL)
  1030. goto cifs_parse_mount_err;
  1031. strcpy(vol->UNC, value);
  1032. if (strncmp(vol->UNC, "//", 2) == 0) {
  1033. vol->UNC[0] = '\\';
  1034. vol->UNC[1] = '\\';
  1035. } else if (strncmp(vol->UNC, "\\\\", 2) != 0) {
  1036. printk(KERN_WARNING
  1037. "CIFS: UNC Path does not begin "
  1038. "with // or \\\\ \n");
  1039. goto cifs_parse_mount_err;
  1040. }
  1041. } else {
  1042. printk(KERN_WARNING "CIFS: UNC name too long\n");
  1043. goto cifs_parse_mount_err;
  1044. }
  1045. } else if ((strnicmp(data, "domain", 3) == 0)
  1046. || (strnicmp(data, "workgroup", 5) == 0)) {
  1047. if (!value || !*value) {
  1048. printk(KERN_WARNING "CIFS: invalid domain name\n");
  1049. goto cifs_parse_mount_err;
  1050. }
  1051. /* BB are there cases in which a comma can be valid in
  1052. a domain name and need special handling? */
  1053. if (strnlen(value, 256) < 256) {
  1054. vol->domainname = kstrdup(value, GFP_KERNEL);
  1055. if (!vol->domainname) {
  1056. printk(KERN_WARNING "CIFS: no memory "
  1057. "for domainname\n");
  1058. goto cifs_parse_mount_err;
  1059. }
  1060. cFYI(1, "Domain name set");
  1061. } else {
  1062. printk(KERN_WARNING "CIFS: domain name too "
  1063. "long\n");
  1064. goto cifs_parse_mount_err;
  1065. }
  1066. } else if (strnicmp(data, "srcaddr", 7) == 0) {
  1067. vol->srcaddr.ss_family = AF_UNSPEC;
  1068. if (!value || !*value) {
  1069. printk(KERN_WARNING "CIFS: srcaddr value"
  1070. " not specified.\n");
  1071. goto cifs_parse_mount_err;
  1072. }
  1073. i = cifs_convert_address((struct sockaddr *)&vol->srcaddr,
  1074. value, strlen(value));
  1075. if (i == 0) {
  1076. printk(KERN_WARNING "CIFS: Could not parse"
  1077. " srcaddr: %s\n",
  1078. value);
  1079. goto cifs_parse_mount_err;
  1080. }
  1081. } else if (strnicmp(data, "prefixpath", 10) == 0) {
  1082. if (!value || !*value) {
  1083. printk(KERN_WARNING
  1084. "CIFS: invalid path prefix\n");
  1085. goto cifs_parse_mount_err;
  1086. }
  1087. if ((temp_len = strnlen(value, 1024)) < 1024) {
  1088. if (value[0] != '/')
  1089. temp_len++; /* missing leading slash */
  1090. vol->prepath = kmalloc(temp_len+1, GFP_KERNEL);
  1091. if (vol->prepath == NULL)
  1092. goto cifs_parse_mount_err;
  1093. if (value[0] != '/') {
  1094. vol->prepath[0] = '/';
  1095. strcpy(vol->prepath+1, value);
  1096. } else
  1097. strcpy(vol->prepath, value);
  1098. cFYI(1, "prefix path %s", vol->prepath);
  1099. } else {
  1100. printk(KERN_WARNING "CIFS: prefix too long\n");
  1101. goto cifs_parse_mount_err;
  1102. }
  1103. } else if (strnicmp(data, "iocharset", 9) == 0) {
  1104. if (!value || !*value) {
  1105. printk(KERN_WARNING "CIFS: invalid iocharset "
  1106. "specified\n");
  1107. goto cifs_parse_mount_err;
  1108. }
  1109. if (strnlen(value, 65) < 65) {
  1110. if (strnicmp(value, "default", 7)) {
  1111. vol->iocharset = kstrdup(value,
  1112. GFP_KERNEL);
  1113. if (!vol->iocharset) {
  1114. printk(KERN_WARNING "CIFS: no "
  1115. "memory for"
  1116. "charset\n");
  1117. goto cifs_parse_mount_err;
  1118. }
  1119. }
  1120. /* if iocharset not set then load_nls_default
  1121. is used by caller */
  1122. cFYI(1, "iocharset set to %s", value);
  1123. } else {
  1124. printk(KERN_WARNING "CIFS: iocharset name "
  1125. "too long.\n");
  1126. goto cifs_parse_mount_err;
  1127. }
  1128. } else if (!strnicmp(data, "uid", 3) && value && *value) {
  1129. vol->linux_uid = simple_strtoul(value, &value, 0);
  1130. uid_specified = true;
  1131. } else if (!strnicmp(data, "cruid", 5) && value && *value) {
  1132. vol->cred_uid = simple_strtoul(value, &value, 0);
  1133. } else if (!strnicmp(data, "forceuid", 8)) {
  1134. override_uid = 1;
  1135. } else if (!strnicmp(data, "noforceuid", 10)) {
  1136. override_uid = 0;
  1137. } else if (!strnicmp(data, "gid", 3) && value && *value) {
  1138. vol->linux_gid = simple_strtoul(value, &value, 0);
  1139. gid_specified = true;
  1140. } else if (!strnicmp(data, "forcegid", 8)) {
  1141. override_gid = 1;
  1142. } else if (!strnicmp(data, "noforcegid", 10)) {
  1143. override_gid = 0;
  1144. } else if (strnicmp(data, "file_mode", 4) == 0) {
  1145. if (value && *value) {
  1146. vol->file_mode =
  1147. simple_strtoul(value, &value, 0);
  1148. }
  1149. } else if (strnicmp(data, "dir_mode", 4) == 0) {
  1150. if (value && *value) {
  1151. vol->dir_mode =
  1152. simple_strtoul(value, &value, 0);
  1153. }
  1154. } else if (strnicmp(data, "dirmode", 4) == 0) {
  1155. if (value && *value) {
  1156. vol->dir_mode =
  1157. simple_strtoul(value, &value, 0);
  1158. }
  1159. } else if (strnicmp(data, "port", 4) == 0) {
  1160. if (value && *value) {
  1161. vol->port =
  1162. simple_strtoul(value, &value, 0);
  1163. }
  1164. } else if (strnicmp(data, "rsize", 5) == 0) {
  1165. if (value && *value) {
  1166. vol->rsize =
  1167. simple_strtoul(value, &value, 0);
  1168. }
  1169. } else if (strnicmp(data, "wsize", 5) == 0) {
  1170. if (value && *value) {
  1171. vol->wsize =
  1172. simple_strtoul(value, &value, 0);
  1173. }
  1174. } else if (strnicmp(data, "sockopt", 5) == 0) {
  1175. if (!value || !*value) {
  1176. cERROR(1, "no socket option specified");
  1177. continue;
  1178. } else if (strnicmp(value, "TCP_NODELAY", 11) == 0) {
  1179. vol->sockopt_tcp_nodelay = 1;
  1180. }
  1181. } else if (strnicmp(data, "netbiosname", 4) == 0) {
  1182. if (!value || !*value || (*value == ' ')) {
  1183. cFYI(1, "invalid (empty) netbiosname");
  1184. } else {
  1185. memset(vol->source_rfc1001_name, 0x20,
  1186. RFC1001_NAME_LEN);
  1187. /*
  1188. * FIXME: are there cases in which a comma can
  1189. * be valid in workstation netbios name (and
  1190. * need special handling)?
  1191. */
  1192. for (i = 0; i < RFC1001_NAME_LEN; i++) {
  1193. /* don't ucase netbiosname for user */
  1194. if (value[i] == 0)
  1195. break;
  1196. vol->source_rfc1001_name[i] = value[i];
  1197. }
  1198. /* The string has 16th byte zero still from
  1199. set at top of the function */
  1200. if (i == RFC1001_NAME_LEN && value[i] != 0)
  1201. printk(KERN_WARNING "CIFS: netbiosname"
  1202. " longer than 15 truncated.\n");
  1203. }
  1204. } else if (strnicmp(data, "servern", 7) == 0) {
  1205. /* servernetbiosname specified override *SMBSERVER */
  1206. if (!value || !*value || (*value == ' ')) {
  1207. cFYI(1, "empty server netbiosname specified");
  1208. } else {
  1209. /* last byte, type, is 0x20 for servr type */
  1210. memset(vol->target_rfc1001_name, 0x20,
  1211. RFC1001_NAME_LEN_WITH_NULL);
  1212. for (i = 0; i < 15; i++) {
  1213. /* BB are there cases in which a comma can be
  1214. valid in this workstation netbios name
  1215. (and need special handling)? */
  1216. /* user or mount helper must uppercase
  1217. the netbiosname */
  1218. if (value[i] == 0)
  1219. break;
  1220. else
  1221. vol->target_rfc1001_name[i] =
  1222. value[i];
  1223. }
  1224. /* The string has 16th byte zero still from
  1225. set at top of the function */
  1226. if (i == RFC1001_NAME_LEN && value[i] != 0)
  1227. printk(KERN_WARNING "CIFS: server net"
  1228. "biosname longer than 15 truncated.\n");
  1229. }
  1230. } else if (strnicmp(data, "actimeo", 7) == 0) {
  1231. if (value && *value) {
  1232. vol->actimeo = HZ * simple_strtoul(value,
  1233. &value, 0);
  1234. if (vol->actimeo > CIFS_MAX_ACTIMEO) {
  1235. cERROR(1, "CIFS: attribute cache"
  1236. "timeout too large");
  1237. goto cifs_parse_mount_err;
  1238. }
  1239. }
  1240. } else if (strnicmp(data, "credentials", 4) == 0) {
  1241. /* ignore */
  1242. } else if (strnicmp(data, "version", 3) == 0) {
  1243. /* ignore */
  1244. } else if (strnicmp(data, "guest", 5) == 0) {
  1245. /* ignore */
  1246. } else if (strnicmp(data, "rw", 2) == 0 && strlen(data) == 2) {
  1247. /* ignore */
  1248. } else if (strnicmp(data, "ro", 2) == 0) {
  1249. /* ignore */
  1250. } else if (strnicmp(data, "noblocksend", 11) == 0) {
  1251. vol->noblocksnd = 1;
  1252. } else if (strnicmp(data, "noautotune", 10) == 0) {
  1253. vol->noautotune = 1;
  1254. } else if ((strnicmp(data, "suid", 4) == 0) ||
  1255. (strnicmp(data, "nosuid", 6) == 0) ||
  1256. (strnicmp(data, "exec", 4) == 0) ||
  1257. (strnicmp(data, "noexec", 6) == 0) ||
  1258. (strnicmp(data, "nodev", 5) == 0) ||
  1259. (strnicmp(data, "noauto", 6) == 0) ||
  1260. (strnicmp(data, "dev", 3) == 0)) {
  1261. /* The mount tool or mount.cifs helper (if present)
  1262. uses these opts to set flags, and the flags are read
  1263. by the kernel vfs layer before we get here (ie
  1264. before read super) so there is no point trying to
  1265. parse these options again and set anything and it
  1266. is ok to just ignore them */
  1267. continue;
  1268. } else if (strnicmp(data, "hard", 4) == 0) {
  1269. vol->retry = 1;
  1270. } else if (strnicmp(data, "soft", 4) == 0) {
  1271. vol->retry = 0;
  1272. } else if (strnicmp(data, "perm", 4) == 0) {
  1273. vol->noperm = 0;
  1274. } else if (strnicmp(data, "noperm", 6) == 0) {
  1275. vol->noperm = 1;
  1276. } else if (strnicmp(data, "mapchars", 8) == 0) {
  1277. vol->remap = 1;
  1278. } else if (strnicmp(data, "nomapchars", 10) == 0) {
  1279. vol->remap = 0;
  1280. } else if (strnicmp(data, "sfu", 3) == 0) {
  1281. vol->sfu_emul = 1;
  1282. } else if (strnicmp(data, "nosfu", 5) == 0) {
  1283. vol->sfu_emul = 0;
  1284. } else if (strnicmp(data, "nodfs", 5) == 0) {
  1285. vol->nodfs = 1;
  1286. } else if (strnicmp(data, "posixpaths", 10) == 0) {
  1287. vol->posix_paths = 1;
  1288. } else if (strnicmp(data, "noposixpaths", 12) == 0) {
  1289. vol->posix_paths = 0;
  1290. } else if (strnicmp(data, "nounix", 6) == 0) {
  1291. vol->no_linux_ext = 1;
  1292. } else if (strnicmp(data, "nolinux", 7) == 0) {
  1293. vol->no_linux_ext = 1;
  1294. } else if ((strnicmp(data, "nocase", 6) == 0) ||
  1295. (strnicmp(data, "ignorecase", 10) == 0)) {
  1296. vol->nocase = 1;
  1297. } else if (strnicmp(data, "mand", 4) == 0) {
  1298. /* ignore */
  1299. } else if (strnicmp(data, "nomand", 6) == 0) {
  1300. /* ignore */
  1301. } else if (strnicmp(data, "_netdev", 7) == 0) {
  1302. /* ignore */
  1303. } else if (strnicmp(data, "brl", 3) == 0) {
  1304. vol->nobrl = 0;
  1305. } else if ((strnicmp(data, "nobrl", 5) == 0) ||
  1306. (strnicmp(data, "nolock", 6) == 0)) {
  1307. vol->nobrl = 1;
  1308. /* turn off mandatory locking in mode
  1309. if remote locking is turned off since the
  1310. local vfs will do advisory */
  1311. if (vol->file_mode ==
  1312. (S_IALLUGO & ~(S_ISUID | S_IXGRP)))
  1313. vol->file_mode = S_IALLUGO;
  1314. } else if (strnicmp(data, "forcemandatorylock", 9) == 0) {
  1315. /* will take the shorter form "forcemand" as well */
  1316. /* This mount option will force use of mandatory
  1317. (DOS/Windows style) byte range locks, instead of
  1318. using posix advisory byte range locks, even if the
  1319. Unix extensions are available and posix locks would
  1320. be supported otherwise. If Unix extensions are not
  1321. negotiated this has no effect since mandatory locks
  1322. would be used (mandatory locks is all that those
  1323. those servers support) */
  1324. vol->mand_lock = 1;
  1325. } else if (strnicmp(data, "setuids", 7) == 0) {
  1326. vol->setuids = 1;
  1327. } else if (strnicmp(data, "nosetuids", 9) == 0) {
  1328. vol->setuids = 0;
  1329. } else if (strnicmp(data, "dynperm", 7) == 0) {
  1330. vol->dynperm = true;
  1331. } else if (strnicmp(data, "nodynperm", 9) == 0) {
  1332. vol->dynperm = false;
  1333. } else if (strnicmp(data, "nohard", 6) == 0) {
  1334. vol->retry = 0;
  1335. } else if (strnicmp(data, "nosoft", 6) == 0) {
  1336. vol->retry = 1;
  1337. } else if (strnicmp(data, "nointr", 6) == 0) {
  1338. vol->intr = 0;
  1339. } else if (strnicmp(data, "intr", 4) == 0) {
  1340. vol->intr = 1;
  1341. } else if (strnicmp(data, "nostrictsync", 12) == 0) {
  1342. vol->nostrictsync = 1;
  1343. } else if (strnicmp(data, "strictsync", 10) == 0) {
  1344. vol->nostrictsync = 0;
  1345. } else if (strnicmp(data, "serverino", 7) == 0) {
  1346. vol->server_ino = 1;
  1347. } else if (strnicmp(data, "noserverino", 9) == 0) {
  1348. vol->server_ino = 0;
  1349. } else if (strnicmp(data, "rwpidforward", 12) == 0) {
  1350. vol->rwpidforward = 1;
  1351. } else if (strnicmp(data, "cifsacl", 7) == 0) {
  1352. vol->cifs_acl = 1;
  1353. } else if (strnicmp(data, "nocifsacl", 9) == 0) {
  1354. vol->cifs_acl = 0;
  1355. } else if (strnicmp(data, "acl", 3) == 0) {
  1356. vol->no_psx_acl = 0;
  1357. } else if (strnicmp(data, "noacl", 5) == 0) {
  1358. vol->no_psx_acl = 1;
  1359. } else if (strnicmp(data, "locallease", 6) == 0) {
  1360. vol->local_lease = 1;
  1361. } else if (strnicmp(data, "sign", 4) == 0) {
  1362. vol->secFlg |= CIFSSEC_MUST_SIGN;
  1363. } else if (strnicmp(data, "seal", 4) == 0) {
  1364. /* we do not do the following in secFlags because seal
  1365. is a per tree connection (mount) not a per socket
  1366. or per-smb connection option in the protocol */
  1367. /* vol->secFlg |= CIFSSEC_MUST_SEAL; */
  1368. vol->seal = 1;
  1369. } else if (strnicmp(data, "direct", 6) == 0) {
  1370. vol->direct_io = 1;
  1371. } else if (strnicmp(data, "forcedirectio", 13) == 0) {
  1372. vol->direct_io = 1;
  1373. } else if (strnicmp(data, "strictcache", 11) == 0) {
  1374. vol->strict_io = 1;
  1375. } else if (strnicmp(data, "noac", 4) == 0) {
  1376. printk(KERN_WARNING "CIFS: Mount option noac not "
  1377. "supported. Instead set "
  1378. "/proc/fs/cifs/LookupCacheEnabled to 0\n");
  1379. } else if (strnicmp(data, "fsc", 3) == 0) {
  1380. #ifndef CONFIG_CIFS_FSCACHE
  1381. cERROR(1, "FS-Cache support needs CONFIG_CIFS_FSCACHE "
  1382. "kernel config option set");
  1383. goto cifs_parse_mount_err;
  1384. #endif
  1385. vol->fsc = true;
  1386. } else if (strnicmp(data, "mfsymlinks", 10) == 0) {
  1387. vol->mfsymlinks = true;
  1388. } else if (strnicmp(data, "multiuser", 8) == 0) {
  1389. vol->multiuser = true;
  1390. } else if (!strnicmp(data, "backupuid", 9) && value && *value) {
  1391. err = kstrtouint(value, 0, &vol->backupuid);
  1392. if (err < 0) {
  1393. cERROR(1, "%s: Invalid backupuid value",
  1394. __func__);
  1395. goto cifs_parse_mount_err;
  1396. }
  1397. vol->backupuid_specified = true;
  1398. } else if (!strnicmp(data, "backupgid", 9) && value && *value) {
  1399. err = kstrtouint(value, 0, &vol->backupgid);
  1400. if (err < 0) {
  1401. cERROR(1, "%s: Invalid backupgid value",
  1402. __func__);
  1403. goto cifs_parse_mount_err;
  1404. }
  1405. vol->backupgid_specified = true;
  1406. } else
  1407. printk(KERN_WARNING "CIFS: Unknown mount option %s\n",
  1408. data);
  1409. }
  1410. if (vol->UNC == NULL) {
  1411. if (devname == NULL) {
  1412. printk(KERN_WARNING "CIFS: Missing UNC name for mount "
  1413. "target\n");
  1414. goto cifs_parse_mount_err;
  1415. }
  1416. if ((temp_len = strnlen(devname, 300)) < 300) {
  1417. vol->UNC = kmalloc(temp_len+1, GFP_KERNEL);
  1418. if (vol->UNC == NULL)
  1419. goto cifs_parse_mount_err;
  1420. strcpy(vol->UNC, devname);
  1421. if (strncmp(vol->UNC, "//", 2) == 0) {
  1422. vol->UNC[0] = '\\';
  1423. vol->UNC[1] = '\\';
  1424. } else if (strncmp(vol->UNC, "\\\\", 2) != 0) {
  1425. printk(KERN_WARNING "CIFS: UNC Path does not "
  1426. "begin with // or \\\\ \n");
  1427. goto cifs_parse_mount_err;
  1428. }
  1429. value = strpbrk(vol->UNC+2, "/\\");
  1430. if (value)
  1431. *value = '\\';
  1432. } else {
  1433. printk(KERN_WARNING "CIFS: UNC name too long\n");
  1434. goto cifs_parse_mount_err;
  1435. }
  1436. }
  1437. if (vol->multiuser && !(vol->secFlg & CIFSSEC_MAY_KRB5)) {
  1438. cERROR(1, "Multiuser mounts currently require krb5 "
  1439. "authentication!");
  1440. goto cifs_parse_mount_err;
  1441. }
  1442. if (vol->UNCip == NULL)
  1443. vol->UNCip = &vol->UNC[2];
  1444. if (uid_specified)
  1445. vol->override_uid = override_uid;
  1446. else if (override_uid == 1)
  1447. printk(KERN_NOTICE "CIFS: ignoring forceuid mount option "
  1448. "specified with no uid= option.\n");
  1449. if (gid_specified)
  1450. vol->override_gid = override_gid;
  1451. else if (override_gid == 1)
  1452. printk(KERN_NOTICE "CIFS: ignoring forcegid mount option "
  1453. "specified with no gid= option.\n");
  1454. kfree(mountdata_copy);
  1455. return 0;
  1456. cifs_parse_mount_err:
  1457. kfree(mountdata_copy);
  1458. return 1;
  1459. }
  1460. /** Returns true if srcaddr isn't specified and rhs isn't
  1461. * specified, or if srcaddr is specified and
  1462. * matches the IP address of the rhs argument.
  1463. */
  1464. static bool
  1465. srcip_matches(struct sockaddr *srcaddr, struct sockaddr *rhs)
  1466. {
  1467. switch (srcaddr->sa_family) {
  1468. case AF_UNSPEC:
  1469. return (rhs->sa_family == AF_UNSPEC);
  1470. case AF_INET: {
  1471. struct sockaddr_in *saddr4 = (struct sockaddr_in *)srcaddr;
  1472. struct sockaddr_in *vaddr4 = (struct sockaddr_in *)rhs;
  1473. return (saddr4->sin_addr.s_addr == vaddr4->sin_addr.s_addr);
  1474. }
  1475. case AF_INET6: {
  1476. struct sockaddr_in6 *saddr6 = (struct sockaddr_in6 *)srcaddr;
  1477. struct sockaddr_in6 *vaddr6 = (struct sockaddr_in6 *)&rhs;
  1478. return ipv6_addr_equal(&saddr6->sin6_addr, &vaddr6->sin6_addr);
  1479. }
  1480. default:
  1481. WARN_ON(1);
  1482. return false; /* don't expect to be here */
  1483. }
  1484. }
  1485. /*
  1486. * If no port is specified in addr structure, we try to match with 445 port
  1487. * and if it fails - with 139 ports. It should be called only if address
  1488. * families of server and addr are equal.
  1489. */
  1490. static bool
  1491. match_port(struct TCP_Server_Info *server, struct sockaddr *addr)
  1492. {
  1493. __be16 port, *sport;
  1494. switch (addr->sa_family) {
  1495. case AF_INET:
  1496. sport = &((struct sockaddr_in *) &server->dstaddr)->sin_port;
  1497. port = ((struct sockaddr_in *) addr)->sin_port;
  1498. break;
  1499. case AF_INET6:
  1500. sport = &((struct sockaddr_in6 *) &server->dstaddr)->sin6_port;
  1501. port = ((struct sockaddr_in6 *) addr)->sin6_port;
  1502. break;
  1503. default:
  1504. WARN_ON(1);
  1505. return false;
  1506. }
  1507. if (!port) {
  1508. port = htons(CIFS_PORT);
  1509. if (port == *sport)
  1510. return true;
  1511. port = htons(RFC1001_PORT);
  1512. }
  1513. return port == *sport;
  1514. }
  1515. static bool
  1516. match_address(struct TCP_Server_Info *server, struct sockaddr *addr,
  1517. struct sockaddr *srcaddr)
  1518. {
  1519. switch (addr->sa_family) {
  1520. case AF_INET: {
  1521. struct sockaddr_in *addr4 = (struct sockaddr_in *)addr;
  1522. struct sockaddr_in *srv_addr4 =
  1523. (struct sockaddr_in *)&server->dstaddr;
  1524. if (addr4->sin_addr.s_addr != srv_addr4->sin_addr.s_addr)
  1525. return false;
  1526. break;
  1527. }
  1528. case AF_INET6: {
  1529. struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)addr;
  1530. struct sockaddr_in6 *srv_addr6 =
  1531. (struct sockaddr_in6 *)&server->dstaddr;
  1532. if (!ipv6_addr_equal(&addr6->sin6_addr,
  1533. &srv_addr6->sin6_addr))
  1534. return false;
  1535. if (addr6->sin6_scope_id != srv_addr6->sin6_scope_id)
  1536. return false;
  1537. break;
  1538. }
  1539. default:
  1540. WARN_ON(1);
  1541. return false; /* don't expect to be here */
  1542. }
  1543. if (!srcip_matches(srcaddr, (struct sockaddr *)&server->srcaddr))
  1544. return false;
  1545. return true;
  1546. }
  1547. static bool
  1548. match_security(struct TCP_Server_Info *server, struct smb_vol *vol)
  1549. {
  1550. unsigned int secFlags;
  1551. if (vol->secFlg & (~(CIFSSEC_MUST_SIGN | CIFSSEC_MUST_SEAL)))
  1552. secFlags = vol->secFlg;
  1553. else
  1554. secFlags = global_secflags | vol->secFlg;
  1555. switch (server->secType) {
  1556. case LANMAN:
  1557. if (!(secFlags & (CIFSSEC_MAY_LANMAN|CIFSSEC_MAY_PLNTXT)))
  1558. return false;
  1559. break;
  1560. case NTLMv2:
  1561. if (!(secFlags & CIFSSEC_MAY_NTLMV2))
  1562. return false;
  1563. break;
  1564. case NTLM:
  1565. if (!(secFlags & CIFSSEC_MAY_NTLM))
  1566. return false;
  1567. break;
  1568. case Kerberos:
  1569. if (!(secFlags & CIFSSEC_MAY_KRB5))
  1570. return false;
  1571. break;
  1572. case RawNTLMSSP:
  1573. if (!(secFlags & CIFSSEC_MAY_NTLMSSP))
  1574. return false;
  1575. break;
  1576. default:
  1577. /* shouldn't happen */
  1578. return false;
  1579. }
  1580. /* now check if signing mode is acceptable */
  1581. if ((secFlags & CIFSSEC_MAY_SIGN) == 0 &&
  1582. (server->sec_mode & SECMODE_SIGN_REQUIRED))
  1583. return false;
  1584. else if (((secFlags & CIFSSEC_MUST_SIGN) == CIFSSEC_MUST_SIGN) &&
  1585. (server->sec_mode &
  1586. (SECMODE_SIGN_ENABLED|SECMODE_SIGN_REQUIRED)) == 0)
  1587. return false;
  1588. return true;
  1589. }
  1590. static int match_server(struct TCP_Server_Info *server, struct sockaddr *addr,
  1591. struct smb_vol *vol)
  1592. {
  1593. if (!net_eq(cifs_net_ns(server), current->nsproxy->net_ns))
  1594. return 0;
  1595. if (!match_address(server, addr,
  1596. (struct sockaddr *)&vol->srcaddr))
  1597. return 0;
  1598. if (!match_port(server, addr))
  1599. return 0;
  1600. if (!match_security(server, vol))
  1601. return 0;
  1602. return 1;
  1603. }
  1604. static struct TCP_Server_Info *
  1605. cifs_find_tcp_session(struct sockaddr *addr, struct smb_vol *vol)
  1606. {
  1607. struct TCP_Server_Info *server;
  1608. spin_lock(&cifs_tcp_ses_lock);
  1609. list_for_each_entry(server, &cifs_tcp_ses_list, tcp_ses_list) {
  1610. if (!match_server(server, addr, vol))
  1611. continue;
  1612. ++server->srv_count;
  1613. spin_unlock(&cifs_tcp_ses_lock);
  1614. cFYI(1, "Existing tcp session with server found");
  1615. return server;
  1616. }
  1617. spin_unlock(&cifs_tcp_ses_lock);
  1618. return NULL;
  1619. }
  1620. static void
  1621. cifs_put_tcp_session(struct TCP_Server_Info *server)
  1622. {
  1623. struct task_struct *task;
  1624. spin_lock(&cifs_tcp_ses_lock);
  1625. if (--server->srv_count > 0) {
  1626. spin_unlock(&cifs_tcp_ses_lock);
  1627. return;
  1628. }
  1629. put_net(cifs_net_ns(server));
  1630. list_del_init(&server->tcp_ses_list);
  1631. spin_unlock(&cifs_tcp_ses_lock);
  1632. cancel_delayed_work_sync(&server->echo);
  1633. spin_lock(&GlobalMid_Lock);
  1634. server->tcpStatus = CifsExiting;
  1635. spin_unlock(&GlobalMid_Lock);
  1636. cifs_crypto_shash_release(server);
  1637. cifs_fscache_release_client_cookie(server);
  1638. kfree(server->session_key.response);
  1639. server->session_key.response = NULL;
  1640. server->session_key.len = 0;
  1641. task = xchg(&server->tsk, NULL);
  1642. if (task)
  1643. force_sig(SIGKILL, task);
  1644. }
  1645. static struct TCP_Server_Info *
  1646. cifs_get_tcp_session(struct smb_vol *volume_info)
  1647. {
  1648. struct TCP_Server_Info *tcp_ses = NULL;
  1649. struct sockaddr_storage addr;
  1650. struct sockaddr_in *sin_server = (struct sockaddr_in *) &addr;
  1651. struct sockaddr_in6 *sin_server6 = (struct sockaddr_in6 *) &addr;
  1652. int rc;
  1653. memset(&addr, 0, sizeof(struct sockaddr_storage));
  1654. cFYI(1, "UNC: %s ip: %s", volume_info->UNC, volume_info->UNCip);
  1655. if (volume_info->UNCip && volume_info->UNC) {
  1656. rc = cifs_fill_sockaddr((struct sockaddr *)&addr,
  1657. volume_info->UNCip,
  1658. strlen(volume_info->UNCip),
  1659. volume_info->port);
  1660. if (!rc) {
  1661. /* we failed translating address */
  1662. rc = -EINVAL;
  1663. goto out_err;
  1664. }
  1665. } else if (volume_info->UNCip) {
  1666. /* BB using ip addr as tcp_ses name to connect to the
  1667. DFS root below */
  1668. cERROR(1, "Connecting to DFS root not implemented yet");
  1669. rc = -EINVAL;
  1670. goto out_err;
  1671. } else /* which tcp_sess DFS root would we conect to */ {
  1672. cERROR(1, "CIFS mount error: No UNC path (e.g. -o "
  1673. "unc=//192.168.1.100/public) specified");
  1674. rc = -EINVAL;
  1675. goto out_err;
  1676. }
  1677. /* see if we already have a matching tcp_ses */
  1678. tcp_ses = cifs_find_tcp_session((struct sockaddr *)&addr, volume_info);
  1679. if (tcp_ses)
  1680. return tcp_ses;
  1681. tcp_ses = kzalloc(sizeof(struct TCP_Server_Info), GFP_KERNEL);
  1682. if (!tcp_ses) {
  1683. rc = -ENOMEM;
  1684. goto out_err;
  1685. }
  1686. rc = cifs_crypto_shash_allocate(tcp_ses);
  1687. if (rc) {
  1688. cERROR(1, "could not setup hash structures rc %d", rc);
  1689. goto out_err;
  1690. }
  1691. cifs_set_net_ns(tcp_ses, get_net(current->nsproxy->net_ns));
  1692. tcp_ses->hostname = extract_hostname(volume_info->UNC);
  1693. if (IS_ERR(tcp_ses->hostname)) {
  1694. rc = PTR_ERR(tcp_ses->hostname);
  1695. goto out_err_crypto_release;
  1696. }
  1697. tcp_ses->noblocksnd = volume_info->noblocksnd;
  1698. tcp_ses->noautotune = volume_info->noautotune;
  1699. tcp_ses->tcp_nodelay = volume_info->sockopt_tcp_nodelay;
  1700. atomic_set(&tcp_ses->inFlight, 0);
  1701. init_waitqueue_head(&tcp_ses->response_q);
  1702. init_waitqueue_head(&tcp_ses->request_q);
  1703. INIT_LIST_HEAD(&tcp_ses->pending_mid_q);
  1704. mutex_init(&tcp_ses->srv_mutex);
  1705. memcpy(tcp_ses->workstation_RFC1001_name,
  1706. volume_info->source_rfc1001_name, RFC1001_NAME_LEN_WITH_NULL);
  1707. memcpy(tcp_ses->server_RFC1001_name,
  1708. volume_info->target_rfc1001_name, RFC1001_NAME_LEN_WITH_NULL);
  1709. tcp_ses->session_estab = false;
  1710. tcp_ses->sequence_number = 0;
  1711. tcp_ses->lstrp = jiffies;
  1712. INIT_LIST_HEAD(&tcp_ses->tcp_ses_list);
  1713. INIT_LIST_HEAD(&tcp_ses->smb_ses_list);
  1714. INIT_DELAYED_WORK(&tcp_ses->echo, cifs_echo_request);
  1715. /*
  1716. * at this point we are the only ones with the pointer
  1717. * to the struct since the kernel thread not created yet
  1718. * no need to spinlock this init of tcpStatus or srv_count
  1719. */
  1720. tcp_ses->tcpStatus = CifsNew;
  1721. memcpy(&tcp_ses->srcaddr, &volume_info->srcaddr,
  1722. sizeof(tcp_ses->srcaddr));
  1723. ++tcp_ses->srv_count;
  1724. if (addr.ss_family == AF_INET6) {
  1725. cFYI(1, "attempting ipv6 connect");
  1726. /* BB should we allow ipv6 on port 139? */
  1727. /* other OS never observed in Wild doing 139 with v6 */
  1728. memcpy(&tcp_ses->dstaddr, sin_server6,
  1729. sizeof(struct sockaddr_in6));
  1730. } else
  1731. memcpy(&tcp_ses->dstaddr, sin_server,
  1732. sizeof(struct sockaddr_in));
  1733. rc = ip_connect(tcp_ses);
  1734. if (rc < 0) {
  1735. cERROR(1, "Error connecting to socket. Aborting operation");
  1736. goto out_err_crypto_release;
  1737. }
  1738. /*
  1739. * since we're in a cifs function already, we know that
  1740. * this will succeed. No need for try_module_get().
  1741. */
  1742. __module_get(THIS_MODULE);
  1743. tcp_ses->tsk = kthread_run(cifs_demultiplex_thread,
  1744. tcp_ses, "cifsd");
  1745. if (IS_ERR(tcp_ses->tsk)) {
  1746. rc = PTR_ERR(tcp_ses->tsk);
  1747. cERROR(1, "error %d create cifsd thread", rc);
  1748. module_put(THIS_MODULE);
  1749. goto out_err_crypto_release;
  1750. }
  1751. tcp_ses->tcpStatus = CifsNeedNegotiate;
  1752. /* thread spawned, put it on the list */
  1753. spin_lock(&cifs_tcp_ses_lock);
  1754. list_add(&tcp_ses->tcp_ses_list, &cifs_tcp_ses_list);
  1755. spin_unlock(&cifs_tcp_ses_lock);
  1756. cifs_fscache_get_client_cookie(tcp_ses);
  1757. /* queue echo request delayed work */
  1758. queue_delayed_work(system_nrt_wq, &tcp_ses->echo, SMB_ECHO_INTERVAL);
  1759. return tcp_ses;
  1760. out_err_crypto_release:
  1761. cifs_crypto_shash_release(tcp_ses);
  1762. put_net(cifs_net_ns(tcp_ses));
  1763. out_err:
  1764. if (tcp_ses) {
  1765. if (!IS_ERR(tcp_ses->hostname))
  1766. kfree(tcp_ses->hostname);
  1767. if (tcp_ses->ssocket)
  1768. sock_release(tcp_ses->ssocket);
  1769. kfree(tcp_ses);
  1770. }
  1771. return ERR_PTR(rc);
  1772. }
  1773. static int match_session(struct cifs_ses *ses, struct smb_vol *vol)
  1774. {
  1775. switch (ses->server->secType) {
  1776. case Kerberos:
  1777. if (vol->cred_uid != ses->cred_uid)
  1778. return 0;
  1779. break;
  1780. default:
  1781. /* anything else takes username/password */
  1782. if (ses->user_name == NULL)
  1783. return 0;
  1784. if (strncmp(ses->user_name, vol->username,
  1785. MAX_USERNAME_SIZE))
  1786. return 0;
  1787. if (strlen(vol->username) != 0 &&
  1788. ses->password != NULL &&
  1789. strncmp(ses->password,
  1790. vol->password ? vol->password : "",
  1791. MAX_PASSWORD_SIZE))
  1792. return 0;
  1793. }
  1794. return 1;
  1795. }
  1796. static struct cifs_ses *
  1797. cifs_find_smb_ses(struct TCP_Server_Info *server, struct smb_vol *vol)
  1798. {
  1799. struct cifs_ses *ses;
  1800. spin_lock(&cifs_tcp_ses_lock);
  1801. list_for_each_entry(ses, &server->smb_ses_list, smb_ses_list) {
  1802. if (!match_session(ses, vol))
  1803. continue;
  1804. ++ses->ses_count;
  1805. spin_unlock(&cifs_tcp_ses_lock);
  1806. return ses;
  1807. }
  1808. spin_unlock(&cifs_tcp_ses_lock);
  1809. return NULL;
  1810. }
  1811. static void
  1812. cifs_put_smb_ses(struct cifs_ses *ses)
  1813. {
  1814. int xid;
  1815. struct TCP_Server_Info *server = ses->server;
  1816. cFYI(1, "%s: ses_count=%d\n", __func__, ses->ses_count);
  1817. spin_lock(&cifs_tcp_ses_lock);
  1818. if (--ses->ses_count > 0) {
  1819. spin_unlock(&cifs_tcp_ses_lock);
  1820. return;
  1821. }
  1822. list_del_init(&ses->smb_ses_list);
  1823. spin_unlock(&cifs_tcp_ses_lock);
  1824. if (ses->status == CifsGood) {
  1825. xid = GetXid();
  1826. CIFSSMBLogoff(xid, ses);
  1827. _FreeXid(xid);
  1828. }
  1829. sesInfoFree(ses);
  1830. cifs_put_tcp_session(server);
  1831. }
  1832. static bool warned_on_ntlm; /* globals init to false automatically */
  1833. static struct cifs_ses *
  1834. cifs_get_smb_ses(struct TCP_Server_Info *server, struct smb_vol *volume_info)
  1835. {
  1836. int rc = -ENOMEM, xid;
  1837. struct cifs_ses *ses;
  1838. struct sockaddr_in *addr = (struct sockaddr_in *)&server->dstaddr;
  1839. struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)&server->dstaddr;
  1840. xid = GetXid();
  1841. ses = cifs_find_smb_ses(server, volume_info);
  1842. if (ses) {
  1843. cFYI(1, "Existing smb sess found (status=%d)", ses->status);
  1844. mutex_lock(&ses->session_mutex);
  1845. rc = cifs_negotiate_protocol(xid, ses);
  1846. if (rc) {
  1847. mutex_unlock(&ses->session_mutex);
  1848. /* problem -- put our ses reference */
  1849. cifs_put_smb_ses(ses);
  1850. FreeXid(xid);
  1851. return ERR_PTR(rc);
  1852. }
  1853. if (ses->need_reconnect) {
  1854. cFYI(1, "Session needs reconnect");
  1855. rc = cifs_setup_session(xid, ses,
  1856. volume_info->local_nls);
  1857. if (rc) {
  1858. mutex_unlock(&ses->session_mutex);
  1859. /* problem -- put our reference */
  1860. cifs_put_smb_ses(ses);
  1861. FreeXid(xid);
  1862. return ERR_PTR(rc);
  1863. }
  1864. }
  1865. mutex_unlock(&ses->session_mutex);
  1866. /* existing SMB ses has a server reference already */
  1867. cifs_put_tcp_session(server);
  1868. FreeXid(xid);
  1869. return ses;
  1870. }
  1871. cFYI(1, "Existing smb sess not found");
  1872. ses = sesInfoAlloc();
  1873. if (ses == NULL)
  1874. goto get_ses_fail;
  1875. /* new SMB session uses our server ref */
  1876. ses->server = server;
  1877. if (server->dstaddr.ss_family == AF_INET6)
  1878. sprintf(ses->serverName, "%pI6", &addr6->sin6_addr);
  1879. else
  1880. sprintf(ses->serverName, "%pI4", &addr->sin_addr);
  1881. if (volume_info->username) {
  1882. ses->user_name = kstrdup(volume_info->username, GFP_KERNEL);
  1883. if (!ses->user_name)
  1884. goto get_ses_fail;
  1885. }
  1886. /* volume_info->password freed at unmount */
  1887. if (volume_info->password) {
  1888. ses->password = kstrdup(volume_info->password, GFP_KERNEL);
  1889. if (!ses->password)
  1890. goto get_ses_fail;
  1891. }
  1892. if (volume_info->domainname) {
  1893. ses->domainName = kstrdup(volume_info->domainname, GFP_KERNEL);
  1894. if (!ses->domainName)
  1895. goto get_ses_fail;
  1896. }
  1897. ses->cred_uid = volume_info->cred_uid;
  1898. ses->linux_uid = volume_info->linux_uid;
  1899. /* ntlmv2 is much stronger than ntlm security, and has been broadly
  1900. supported for many years, time to update default security mechanism */
  1901. if ((volume_info->secFlg == 0) && warned_on_ntlm == false) {
  1902. warned_on_ntlm = true;
  1903. cERROR(1, "default security mechanism requested. The default "
  1904. "security mechanism will be upgraded from ntlm to "
  1905. "ntlmv2 in kernel release 3.2");
  1906. }
  1907. ses->overrideSecFlg = volume_info->secFlg;
  1908. mutex_lock(&ses->session_mutex);
  1909. rc = cifs_negotiate_protocol(xid, ses);
  1910. if (!rc)
  1911. rc = cifs_setup_session(xid, ses, volume_info->local_nls);
  1912. mutex_unlock(&ses->session_mutex);
  1913. if (rc)
  1914. goto get_ses_fail;
  1915. /* success, put it on the list */
  1916. spin_lock(&cifs_tcp_ses_lock);
  1917. list_add(&ses->smb_ses_list, &server->smb_ses_list);
  1918. spin_unlock(&cifs_tcp_ses_lock);
  1919. FreeXid(xid);
  1920. return ses;
  1921. get_ses_fail:
  1922. sesInfoFree(ses);
  1923. FreeXid(xid);
  1924. return ERR_PTR(rc);
  1925. }
  1926. static int match_tcon(struct cifs_tcon *tcon, const char *unc)
  1927. {
  1928. if (tcon->tidStatus == CifsExiting)
  1929. return 0;
  1930. if (strncmp(tcon->treeName, unc, MAX_TREE_SIZE))
  1931. return 0;
  1932. return 1;
  1933. }
  1934. static struct cifs_tcon *
  1935. cifs_find_tcon(struct cifs_ses *ses, const char *unc)
  1936. {
  1937. struct list_head *tmp;
  1938. struct cifs_tcon *tcon;
  1939. spin_lock(&cifs_tcp_ses_lock);
  1940. list_for_each(tmp, &ses->tcon_list) {
  1941. tcon = list_entry(tmp, struct cifs_tcon, tcon_list);
  1942. if (!match_tcon(tcon, unc))
  1943. continue;
  1944. ++tcon->tc_count;
  1945. spin_unlock(&cifs_tcp_ses_lock);
  1946. return tcon;
  1947. }
  1948. spin_unlock(&cifs_tcp_ses_lock);
  1949. return NULL;
  1950. }
  1951. static void
  1952. cifs_put_tcon(struct cifs_tcon *tcon)
  1953. {
  1954. int xid;
  1955. struct cifs_ses *ses = tcon->ses;
  1956. cFYI(1, "%s: tc_count=%d\n", __func__, tcon->tc_count);
  1957. spin_lock(&cifs_tcp_ses_lock);
  1958. if (--tcon->tc_count > 0) {
  1959. spin_unlock(&cifs_tcp_ses_lock);
  1960. return;
  1961. }
  1962. list_del_init(&tcon->tcon_list);
  1963. spin_unlock(&cifs_tcp_ses_lock);
  1964. xid = GetXid();
  1965. CIFSSMBTDis(xid, tcon);
  1966. _FreeXid(xid);
  1967. cifs_fscache_release_super_cookie(tcon);
  1968. tconInfoFree(tcon);
  1969. cifs_put_smb_ses(ses);
  1970. }
  1971. static struct cifs_tcon *
  1972. cifs_get_tcon(struct cifs_ses *ses, struct smb_vol *volume_info)
  1973. {
  1974. int rc, xid;
  1975. struct cifs_tcon *tcon;
  1976. tcon = cifs_find_tcon(ses, volume_info->UNC);
  1977. if (tcon) {
  1978. cFYI(1, "Found match on UNC path");
  1979. /* existing tcon already has a reference */
  1980. cifs_put_smb_ses(ses);
  1981. if (tcon->seal != volume_info->seal)
  1982. cERROR(1, "transport encryption setting "
  1983. "conflicts with existing tid");
  1984. return tcon;
  1985. }
  1986. tcon = tconInfoAlloc();
  1987. if (tcon == NULL) {
  1988. rc = -ENOMEM;
  1989. goto out_fail;
  1990. }
  1991. tcon->ses = ses;
  1992. if (volume_info->password) {
  1993. tcon->password = kstrdup(volume_info->password, GFP_KERNEL);
  1994. if (!tcon->password) {
  1995. rc = -ENOMEM;
  1996. goto out_fail;
  1997. }
  1998. }
  1999. if (strchr(volume_info->UNC + 3, '\\') == NULL
  2000. && strchr(volume_info->UNC + 3, '/') == NULL) {
  2001. cERROR(1, "Missing share name");
  2002. rc = -ENODEV;
  2003. goto out_fail;
  2004. }
  2005. /* BB Do we need to wrap session_mutex around
  2006. * this TCon call and Unix SetFS as
  2007. * we do on SessSetup and reconnect? */
  2008. xid = GetXid();
  2009. rc = CIFSTCon(xid, ses, volume_info->UNC, tcon, volume_info->local_nls);
  2010. FreeXid(xid);
  2011. cFYI(1, "CIFS Tcon rc = %d", rc);
  2012. if (rc)
  2013. goto out_fail;
  2014. if (volume_info->nodfs) {
  2015. tcon->Flags &= ~SMB_SHARE_IS_IN_DFS;
  2016. cFYI(1, "DFS disabled (%d)", tcon->Flags);
  2017. }
  2018. tcon->seal = volume_info->seal;
  2019. /* we can have only one retry value for a connection
  2020. to a share so for resources mounted more than once
  2021. to the same server share the last value passed in
  2022. for the retry flag is used */
  2023. tcon->retry = volume_info->retry;
  2024. tcon->nocase = volume_info->nocase;
  2025. tcon->local_lease = volume_info->local_lease;
  2026. spin_lock(&cifs_tcp_ses_lock);
  2027. list_add(&tcon->tcon_list, &ses->tcon_list);
  2028. spin_unlock(&cifs_tcp_ses_lock);
  2029. cifs_fscache_get_super_cookie(tcon);
  2030. return tcon;
  2031. out_fail:
  2032. tconInfoFree(tcon);
  2033. return ERR_PTR(rc);
  2034. }
  2035. void
  2036. cifs_put_tlink(struct tcon_link *tlink)
  2037. {
  2038. if (!tlink || IS_ERR(tlink))
  2039. return;
  2040. if (!atomic_dec_and_test(&tlink->tl_count) ||
  2041. test_bit(TCON_LINK_IN_TREE, &tlink->tl_flags)) {
  2042. tlink->tl_time = jiffies;
  2043. return;
  2044. }
  2045. if (!IS_ERR(tlink_tcon(tlink)))
  2046. cifs_put_tcon(tlink_tcon(tlink));
  2047. kfree(tlink);
  2048. return;
  2049. }
  2050. static inline struct tcon_link *
  2051. cifs_sb_master_tlink(struct cifs_sb_info *cifs_sb)
  2052. {
  2053. return cifs_sb->master_tlink;
  2054. }
  2055. static int
  2056. compare_mount_options(struct super_block *sb, struct cifs_mnt_data *mnt_data)
  2057. {
  2058. struct cifs_sb_info *old = CIFS_SB(sb);
  2059. struct cifs_sb_info *new = mnt_data->cifs_sb;
  2060. if ((sb->s_flags & CIFS_MS_MASK) != (mnt_data->flags & CIFS_MS_MASK))
  2061. return 0;
  2062. if ((old->mnt_cifs_flags & CIFS_MOUNT_MASK) !=
  2063. (new->mnt_cifs_flags & CIFS_MOUNT_MASK))
  2064. return 0;
  2065. if (old->rsize != new->rsize)
  2066. return 0;
  2067. /*
  2068. * We want to share sb only if we don't specify wsize or specified wsize
  2069. * is greater or equal than existing one.
  2070. */
  2071. if (new->wsize && new->wsize < old->wsize)
  2072. return 0;
  2073. if (old->mnt_uid != new->mnt_uid || old->mnt_gid != new->mnt_gid)
  2074. return 0;
  2075. if (old->mnt_file_mode != new->mnt_file_mode ||
  2076. old->mnt_dir_mode != new->mnt_dir_mode)
  2077. return 0;
  2078. if (strcmp(old->local_nls->charset, new->local_nls->charset))
  2079. return 0;
  2080. if (old->actimeo != new->actimeo)
  2081. return 0;
  2082. return 1;
  2083. }
  2084. int
  2085. cifs_match_super(struct super_block *sb, void *data)
  2086. {
  2087. struct cifs_mnt_data *mnt_data = (struct cifs_mnt_data *)data;
  2088. struct smb_vol *volume_info;
  2089. struct cifs_sb_info *cifs_sb;
  2090. struct TCP_Server_Info *tcp_srv;
  2091. struct cifs_ses *ses;
  2092. struct cifs_tcon *tcon;
  2093. struct tcon_link *tlink;
  2094. struct sockaddr_storage addr;
  2095. int rc = 0;
  2096. memset(&addr, 0, sizeof(struct sockaddr_storage));
  2097. spin_lock(&cifs_tcp_ses_lock);
  2098. cifs_sb = CIFS_SB(sb);
  2099. tlink = cifs_get_tlink(cifs_sb_master_tlink(cifs_sb));
  2100. if (IS_ERR(tlink)) {
  2101. spin_unlock(&cifs_tcp_ses_lock);
  2102. return rc;
  2103. }
  2104. tcon = tlink_tcon(tlink);
  2105. ses = tcon->ses;
  2106. tcp_srv = ses->server;
  2107. volume_info = mnt_data->vol;
  2108. if (!volume_info->UNCip || !volume_info->UNC)
  2109. goto out;
  2110. rc = cifs_fill_sockaddr((struct sockaddr *)&addr,
  2111. volume_info->UNCip,
  2112. strlen(volume_info->UNCip),
  2113. volume_info->port);
  2114. if (!rc)
  2115. goto out;
  2116. if (!match_server(tcp_srv, (struct sockaddr *)&addr, volume_info) ||
  2117. !match_session(ses, volume_info) ||
  2118. !match_tcon(tcon, volume_info->UNC)) {
  2119. rc = 0;
  2120. goto out;
  2121. }
  2122. rc = compare_mount_options(sb, mnt_data);
  2123. out:
  2124. spin_unlock(&cifs_tcp_ses_lock);
  2125. cifs_put_tlink(tlink);
  2126. return rc;
  2127. }
  2128. int
  2129. get_dfs_path(int xid, struct cifs_ses *pSesInfo, const char *old_path,
  2130. const struct nls_table *nls_codepage, unsigned int *pnum_referrals,
  2131. struct dfs_info3_param **preferrals, int remap)
  2132. {
  2133. char *temp_unc;
  2134. int rc = 0;
  2135. *pnum_referrals = 0;
  2136. *preferrals = NULL;
  2137. if (pSesInfo->ipc_tid == 0) {
  2138. temp_unc = kmalloc(2 /* for slashes */ +
  2139. strnlen(pSesInfo->serverName,
  2140. SERVER_NAME_LEN_WITH_NULL * 2)
  2141. + 1 + 4 /* slash IPC$ */ + 2,
  2142. GFP_KERNEL);
  2143. if (temp_unc == NULL)
  2144. return -ENOMEM;
  2145. temp_unc[0] = '\\';
  2146. temp_unc[1] = '\\';
  2147. strcpy(temp_unc + 2, pSesInfo->serverName);
  2148. strcpy(temp_unc + 2 + strlen(pSesInfo->serverName), "\\IPC$");
  2149. rc = CIFSTCon(xid, pSesInfo, temp_unc, NULL, nls_codepage);
  2150. cFYI(1, "CIFS Tcon rc = %d ipc_tid = %d", rc, pSesInfo->ipc_tid);
  2151. kfree(temp_unc);
  2152. }
  2153. if (rc == 0)
  2154. rc = CIFSGetDFSRefer(xid, pSesInfo, old_path, preferrals,
  2155. pnum_referrals, nls_codepage, remap);
  2156. /* BB map targetUNCs to dfs_info3 structures, here or
  2157. in CIFSGetDFSRefer BB */
  2158. return rc;
  2159. }
  2160. #ifdef CONFIG_DEBUG_LOCK_ALLOC
  2161. static struct lock_class_key cifs_key[2];
  2162. static struct lock_class_key cifs_slock_key[2];
  2163. static inline void
  2164. cifs_reclassify_socket4(struct socket *sock)
  2165. {
  2166. struct sock *sk = sock->sk;
  2167. BUG_ON(sock_owned_by_user(sk));
  2168. sock_lock_init_class_and_name(sk, "slock-AF_INET-CIFS",
  2169. &cifs_slock_key[0], "sk_lock-AF_INET-CIFS", &cifs_key[0]);
  2170. }
  2171. static inline void
  2172. cifs_reclassify_socket6(struct socket *sock)
  2173. {
  2174. struct sock *sk = sock->sk;
  2175. BUG_ON(sock_owned_by_user(sk));
  2176. sock_lock_init_class_and_name(sk, "slock-AF_INET6-CIFS",
  2177. &cifs_slock_key[1], "sk_lock-AF_INET6-CIFS", &cifs_key[1]);
  2178. }
  2179. #else
  2180. static inline void
  2181. cifs_reclassify_socket4(struct socket *sock)
  2182. {
  2183. }
  2184. static inline void
  2185. cifs_reclassify_socket6(struct socket *sock)
  2186. {
  2187. }
  2188. #endif
  2189. /* See RFC1001 section 14 on representation of Netbios names */
  2190. static void rfc1002mangle(char *target, char *source, unsigned int length)
  2191. {
  2192. unsigned int i, j;
  2193. for (i = 0, j = 0; i < (length); i++) {
  2194. /* mask a nibble at a time and encode */
  2195. target[j] = 'A' + (0x0F & (source[i] >> 4));
  2196. target[j+1] = 'A' + (0x0F & source[i]);
  2197. j += 2;
  2198. }
  2199. }
  2200. static int
  2201. bind_socket(struct TCP_Server_Info *server)
  2202. {
  2203. int rc = 0;
  2204. if (server->srcaddr.ss_family != AF_UNSPEC) {
  2205. /* Bind to the specified local IP address */
  2206. struct socket *socket = server->ssocket;
  2207. rc = socket->ops->bind(socket,
  2208. (struct sockaddr *) &server->srcaddr,
  2209. sizeof(server->srcaddr));
  2210. if (rc < 0) {
  2211. struct sockaddr_in *saddr4;
  2212. struct sockaddr_in6 *saddr6;
  2213. saddr4 = (struct sockaddr_in *)&server->srcaddr;
  2214. saddr6 = (struct sockaddr_in6 *)&server->srcaddr;
  2215. if (saddr6->sin6_family == AF_INET6)
  2216. cERROR(1, "cifs: "
  2217. "Failed to bind to: %pI6c, error: %d\n",
  2218. &saddr6->sin6_addr, rc);
  2219. else
  2220. cERROR(1, "cifs: "
  2221. "Failed to bind to: %pI4, error: %d\n",
  2222. &saddr4->sin_addr.s_addr, rc);
  2223. }
  2224. }
  2225. return rc;
  2226. }
  2227. static int
  2228. ip_rfc1001_connect(struct TCP_Server_Info *server)
  2229. {
  2230. int rc = 0;
  2231. /*
  2232. * some servers require RFC1001 sessinit before sending
  2233. * negprot - BB check reconnection in case where second
  2234. * sessinit is sent but no second negprot
  2235. */
  2236. struct rfc1002_session_packet *ses_init_buf;
  2237. struct smb_hdr *smb_buf;
  2238. ses_init_buf = kzalloc(sizeof(struct rfc1002_session_packet),
  2239. GFP_KERNEL);
  2240. if (ses_init_buf) {
  2241. ses_init_buf->trailer.session_req.called_len = 32;
  2242. if (server->server_RFC1001_name &&
  2243. server->server_RFC1001_name[0] != 0)
  2244. rfc1002mangle(ses_init_buf->trailer.
  2245. session_req.called_name,
  2246. server->server_RFC1001_name,
  2247. RFC1001_NAME_LEN_WITH_NULL);
  2248. else
  2249. rfc1002mangle(ses_init_buf->trailer.
  2250. session_req.called_name,
  2251. DEFAULT_CIFS_CALLED_NAME,
  2252. RFC1001_NAME_LEN_WITH_NULL);
  2253. ses_init_buf->trailer.session_req.calling_len = 32;
  2254. /*
  2255. * calling name ends in null (byte 16) from old smb
  2256. * convention.
  2257. */
  2258. if (server->workstation_RFC1001_name &&
  2259. server->workstation_RFC1001_name[0] != 0)
  2260. rfc1002mangle(ses_init_buf->trailer.
  2261. session_req.calling_name,
  2262. server->workstation_RFC1001_name,
  2263. RFC1001_NAME_LEN_WITH_NULL);
  2264. else
  2265. rfc1002mangle(ses_init_buf->trailer.
  2266. session_req.calling_name,
  2267. "LINUX_CIFS_CLNT",
  2268. RFC1001_NAME_LEN_WITH_NULL);
  2269. ses_init_buf->trailer.session_req.scope1 = 0;
  2270. ses_init_buf->trailer.session_req.scope2 = 0;
  2271. smb_buf = (struct smb_hdr *)ses_init_buf;
  2272. /* sizeof RFC1002_SESSION_REQUEST with no scope */
  2273. smb_buf->smb_buf_length = cpu_to_be32(0x81000044);
  2274. rc = smb_send(server, smb_buf, 0x44);
  2275. kfree(ses_init_buf);
  2276. /*
  2277. * RFC1001 layer in at least one server
  2278. * requires very short break before negprot
  2279. * presumably because not expecting negprot
  2280. * to follow so fast. This is a simple
  2281. * solution that works without
  2282. * complicating the code and causes no
  2283. * significant slowing down on mount
  2284. * for everyone else
  2285. */
  2286. usleep_range(1000, 2000);
  2287. }
  2288. /*
  2289. * else the negprot may still work without this
  2290. * even though malloc failed
  2291. */
  2292. return rc;
  2293. }
  2294. static int
  2295. generic_ip_connect(struct TCP_Server_Info *server)
  2296. {
  2297. int rc = 0;
  2298. __be16 sport;
  2299. int slen, sfamily;
  2300. struct socket *socket = server->ssocket;
  2301. struct sockaddr *saddr;
  2302. saddr = (struct sockaddr *) &server->dstaddr;
  2303. if (server->dstaddr.ss_family == AF_INET6) {
  2304. sport = ((struct sockaddr_in6 *) saddr)->sin6_port;
  2305. slen = sizeof(struct sockaddr_in6);
  2306. sfamily = AF_INET6;
  2307. } else {
  2308. sport = ((struct sockaddr_in *) saddr)->sin_port;
  2309. slen = sizeof(struct sockaddr_in);
  2310. sfamily = AF_INET;
  2311. }
  2312. if (socket == NULL) {
  2313. rc = __sock_create(cifs_net_ns(server), sfamily, SOCK_STREAM,
  2314. IPPROTO_TCP, &socket, 1);
  2315. if (rc < 0) {
  2316. cERROR(1, "Error %d creating socket", rc);
  2317. server->ssocket = NULL;
  2318. return rc;
  2319. }
  2320. /* BB other socket options to set KEEPALIVE, NODELAY? */
  2321. cFYI(1, "Socket created");
  2322. server->ssocket = socket;
  2323. socket->sk->sk_allocation = GFP_NOFS;
  2324. if (sfamily == AF_INET6)
  2325. cifs_reclassify_socket6(socket);
  2326. else
  2327. cifs_reclassify_socket4(socket);
  2328. }
  2329. rc = bind_socket(server);
  2330. if (rc < 0)
  2331. return rc;
  2332. /*
  2333. * Eventually check for other socket options to change from
  2334. * the default. sock_setsockopt not used because it expects
  2335. * user space buffer
  2336. */
  2337. socket->sk->sk_rcvtimeo = 7 * HZ;
  2338. socket->sk->sk_sndtimeo = 5 * HZ;
  2339. /* make the bufsizes depend on wsize/rsize and max requests */
  2340. if (server->noautotune) {
  2341. if (socket->sk->sk_sndbuf < (200 * 1024))
  2342. socket->sk->sk_sndbuf = 200 * 1024;
  2343. if (socket->sk->sk_rcvbuf < (140 * 1024))
  2344. socket->sk->sk_rcvbuf = 140 * 1024;
  2345. }
  2346. if (server->tcp_nodelay) {
  2347. int val = 1;
  2348. rc = kernel_setsockopt(socket, SOL_TCP, TCP_NODELAY,
  2349. (char *)&val, sizeof(val));
  2350. if (rc)
  2351. cFYI(1, "set TCP_NODELAY socket option error %d", rc);
  2352. }
  2353. cFYI(1, "sndbuf %d rcvbuf %d rcvtimeo 0x%lx",
  2354. socket->sk->sk_sndbuf,
  2355. socket->sk->sk_rcvbuf, socket->sk->sk_rcvtimeo);
  2356. rc = socket->ops->connect(socket, saddr, slen, 0);
  2357. if (rc < 0) {
  2358. cFYI(1, "Error %d connecting to server", rc);
  2359. sock_release(socket);
  2360. server->ssocket = NULL;
  2361. return rc;
  2362. }
  2363. if (sport == htons(RFC1001_PORT))
  2364. rc = ip_rfc1001_connect(server);
  2365. return rc;
  2366. }
  2367. static int
  2368. ip_connect(struct TCP_Server_Info *server)
  2369. {
  2370. __be16 *sport;
  2371. struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)&server->dstaddr;
  2372. struct sockaddr_in *addr = (struct sockaddr_in *)&server->dstaddr;
  2373. if (server->dstaddr.ss_family == AF_INET6)
  2374. sport = &addr6->sin6_port;
  2375. else
  2376. sport = &addr->sin_port;
  2377. if (*sport == 0) {
  2378. int rc;
  2379. /* try with 445 port at first */
  2380. *sport = htons(CIFS_PORT);
  2381. rc = generic_ip_connect(server);
  2382. if (rc >= 0)
  2383. return rc;
  2384. /* if it failed, try with 139 port */
  2385. *sport = htons(RFC1001_PORT);
  2386. }
  2387. return generic_ip_connect(server);
  2388. }
  2389. void reset_cifs_unix_caps(int xid, struct cifs_tcon *tcon,
  2390. struct cifs_sb_info *cifs_sb, struct smb_vol *vol_info)
  2391. {
  2392. /* if we are reconnecting then should we check to see if
  2393. * any requested capabilities changed locally e.g. via
  2394. * remount but we can not do much about it here
  2395. * if they have (even if we could detect it by the following)
  2396. * Perhaps we could add a backpointer to array of sb from tcon
  2397. * or if we change to make all sb to same share the same
  2398. * sb as NFS - then we only have one backpointer to sb.
  2399. * What if we wanted to mount the server share twice once with
  2400. * and once without posixacls or posix paths? */
  2401. __u64 saved_cap = le64_to_cpu(tcon->fsUnixInfo.Capability);
  2402. if (vol_info && vol_info->no_linux_ext) {
  2403. tcon->fsUnixInfo.Capability = 0;
  2404. tcon->unix_ext = 0; /* Unix Extensions disabled */
  2405. cFYI(1, "Linux protocol extensions disabled");
  2406. return;
  2407. } else if (vol_info)
  2408. tcon->unix_ext = 1; /* Unix Extensions supported */
  2409. if (tcon->unix_ext == 0) {
  2410. cFYI(1, "Unix extensions disabled so not set on reconnect");
  2411. return;
  2412. }
  2413. if (!CIFSSMBQFSUnixInfo(xid, tcon)) {
  2414. __u64 cap = le64_to_cpu(tcon->fsUnixInfo.Capability);
  2415. cFYI(1, "unix caps which server supports %lld", cap);
  2416. /* check for reconnect case in which we do not
  2417. want to change the mount behavior if we can avoid it */
  2418. if (vol_info == NULL) {
  2419. /* turn off POSIX ACL and PATHNAMES if not set
  2420. originally at mount time */
  2421. if ((saved_cap & CIFS_UNIX_POSIX_ACL_CAP) == 0)
  2422. cap &= ~CIFS_UNIX_POSIX_ACL_CAP;
  2423. if ((saved_cap & CIFS_UNIX_POSIX_PATHNAMES_CAP) == 0) {
  2424. if (cap & CIFS_UNIX_POSIX_PATHNAMES_CAP)
  2425. cERROR(1, "POSIXPATH support change");
  2426. cap &= ~CIFS_UNIX_POSIX_PATHNAMES_CAP;
  2427. } else if ((cap & CIFS_UNIX_POSIX_PATHNAMES_CAP) == 0) {
  2428. cERROR(1, "possible reconnect error");
  2429. cERROR(1, "server disabled POSIX path support");
  2430. }
  2431. }
  2432. if (cap & CIFS_UNIX_TRANSPORT_ENCRYPTION_MANDATORY_CAP)
  2433. cERROR(1, "per-share encryption not supported yet");
  2434. cap &= CIFS_UNIX_CAP_MASK;
  2435. if (vol_info && vol_info->no_psx_acl)
  2436. cap &= ~CIFS_UNIX_POSIX_ACL_CAP;
  2437. else if (CIFS_UNIX_POSIX_ACL_CAP & cap) {
  2438. cFYI(1, "negotiated posix acl support");
  2439. if (cifs_sb)
  2440. cifs_sb->mnt_cifs_flags |=
  2441. CIFS_MOUNT_POSIXACL;
  2442. }
  2443. if (vol_info && vol_info->posix_paths == 0)
  2444. cap &= ~CIFS_UNIX_POSIX_PATHNAMES_CAP;
  2445. else if (cap & CIFS_UNIX_POSIX_PATHNAMES_CAP) {
  2446. cFYI(1, "negotiate posix pathnames");
  2447. if (cifs_sb)
  2448. cifs_sb->mnt_cifs_flags |=
  2449. CIFS_MOUNT_POSIX_PATHS;
  2450. }
  2451. if (cifs_sb && (cifs_sb->rsize > 127 * 1024)) {
  2452. if ((cap & CIFS_UNIX_LARGE_READ_CAP) == 0) {
  2453. cifs_sb->rsize = 127 * 1024;
  2454. cFYI(DBG2, "larger reads not supported by srv");
  2455. }
  2456. }
  2457. cFYI(1, "Negotiate caps 0x%x", (int)cap);
  2458. #ifdef CONFIG_CIFS_DEBUG2
  2459. if (cap & CIFS_UNIX_FCNTL_CAP)
  2460. cFYI(1, "FCNTL cap");
  2461. if (cap & CIFS_UNIX_EXTATTR_CAP)
  2462. cFYI(1, "EXTATTR cap");
  2463. if (cap & CIFS_UNIX_POSIX_PATHNAMES_CAP)
  2464. cFYI(1, "POSIX path cap");
  2465. if (cap & CIFS_UNIX_XATTR_CAP)
  2466. cFYI(1, "XATTR cap");
  2467. if (cap & CIFS_UNIX_POSIX_ACL_CAP)
  2468. cFYI(1, "POSIX ACL cap");
  2469. if (cap & CIFS_UNIX_LARGE_READ_CAP)
  2470. cFYI(1, "very large read cap");
  2471. if (cap & CIFS_UNIX_LARGE_WRITE_CAP)
  2472. cFYI(1, "very large write cap");
  2473. if (cap & CIFS_UNIX_TRANSPORT_ENCRYPTION_CAP)
  2474. cFYI(1, "transport encryption cap");
  2475. if (cap & CIFS_UNIX_TRANSPORT_ENCRYPTION_MANDATORY_CAP)
  2476. cFYI(1, "mandatory transport encryption cap");
  2477. #endif /* CIFS_DEBUG2 */
  2478. if (CIFSSMBSetFSUnixInfo(xid, tcon, cap)) {
  2479. if (vol_info == NULL) {
  2480. cFYI(1, "resetting capabilities failed");
  2481. } else
  2482. cERROR(1, "Negotiating Unix capabilities "
  2483. "with the server failed. Consider "
  2484. "mounting with the Unix Extensions\n"
  2485. "disabled, if problems are found, "
  2486. "by specifying the nounix mount "
  2487. "option.");
  2488. }
  2489. }
  2490. }
  2491. void cifs_setup_cifs_sb(struct smb_vol *pvolume_info,
  2492. struct cifs_sb_info *cifs_sb)
  2493. {
  2494. INIT_DELAYED_WORK(&cifs_sb->prune_tlinks, cifs_prune_tlinks);
  2495. spin_lock_init(&cifs_sb->tlink_tree_lock);
  2496. cifs_sb->tlink_tree = RB_ROOT;
  2497. if (pvolume_info->rsize > CIFSMaxBufSize) {
  2498. cERROR(1, "rsize %d too large, using MaxBufSize",
  2499. pvolume_info->rsize);
  2500. cifs_sb->rsize = CIFSMaxBufSize;
  2501. } else if ((pvolume_info->rsize) &&
  2502. (pvolume_info->rsize <= CIFSMaxBufSize))
  2503. cifs_sb->rsize = pvolume_info->rsize;
  2504. else /* default */
  2505. cifs_sb->rsize = CIFSMaxBufSize;
  2506. if (cifs_sb->rsize < 2048) {
  2507. cifs_sb->rsize = 2048;
  2508. /* Windows ME may prefer this */
  2509. cFYI(1, "readsize set to minimum: 2048");
  2510. }
  2511. /*
  2512. * Temporarily set wsize for matching superblock. If we end up using
  2513. * new sb then cifs_negotiate_wsize will later negotiate it downward
  2514. * if needed.
  2515. */
  2516. cifs_sb->wsize = pvolume_info->wsize;
  2517. cifs_sb->mnt_uid = pvolume_info->linux_uid;
  2518. cifs_sb->mnt_gid = pvolume_info->linux_gid;
  2519. if (pvolume_info->backupuid_specified)
  2520. cifs_sb->mnt_backupuid = pvolume_info->backupuid;
  2521. if (pvolume_info->backupgid_specified)
  2522. cifs_sb->mnt_backupgid = pvolume_info->backupgid;
  2523. cifs_sb->mnt_file_mode = pvolume_info->file_mode;
  2524. cifs_sb->mnt_dir_mode = pvolume_info->dir_mode;
  2525. cFYI(1, "file mode: 0x%x dir mode: 0x%x",
  2526. cifs_sb->mnt_file_mode, cifs_sb->mnt_dir_mode);
  2527. cifs_sb->actimeo = pvolume_info->actimeo;
  2528. cifs_sb->local_nls = pvolume_info->local_nls;
  2529. if (pvolume_info->noperm)
  2530. cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_NO_PERM;
  2531. if (pvolume_info->setuids)
  2532. cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_SET_UID;
  2533. if (pvolume_info->server_ino)
  2534. cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_SERVER_INUM;
  2535. if (pvolume_info->remap)
  2536. cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_MAP_SPECIAL_CHR;
  2537. if (pvolume_info->no_xattr)
  2538. cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_NO_XATTR;
  2539. if (pvolume_info->sfu_emul)
  2540. cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_UNX_EMUL;
  2541. if (pvolume_info->nobrl)
  2542. cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_NO_BRL;
  2543. if (pvolume_info->nostrictsync)
  2544. cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_NOSSYNC;
  2545. if (pvolume_info->mand_lock)
  2546. cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_NOPOSIXBRL;
  2547. if (pvolume_info->rwpidforward)
  2548. cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_RWPIDFORWARD;
  2549. if (pvolume_info->cifs_acl)
  2550. cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_CIFS_ACL;
  2551. if (pvolume_info->backupuid_specified)
  2552. cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_CIFS_BACKUPUID;
  2553. if (pvolume_info->backupgid_specified)
  2554. cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_CIFS_BACKUPGID;
  2555. if (pvolume_info->override_uid)
  2556. cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_OVERR_UID;
  2557. if (pvolume_info->override_gid)
  2558. cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_OVERR_GID;
  2559. if (pvolume_info->dynperm)
  2560. cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_DYNPERM;
  2561. if (pvolume_info->fsc)
  2562. cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_FSCACHE;
  2563. if (pvolume_info->multiuser)
  2564. cifs_sb->mnt_cifs_flags |= (CIFS_MOUNT_MULTIUSER |
  2565. CIFS_MOUNT_NO_PERM);
  2566. if (pvolume_info->strict_io)
  2567. cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_STRICT_IO;
  2568. if (pvolume_info->direct_io) {
  2569. cFYI(1, "mounting share using direct i/o");
  2570. cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_DIRECT_IO;
  2571. }
  2572. if (pvolume_info->mfsymlinks) {
  2573. if (pvolume_info->sfu_emul) {
  2574. cERROR(1, "mount option mfsymlinks ignored if sfu "
  2575. "mount option is used");
  2576. } else {
  2577. cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_MF_SYMLINKS;
  2578. }
  2579. }
  2580. if ((pvolume_info->cifs_acl) && (pvolume_info->dynperm))
  2581. cERROR(1, "mount option dynperm ignored if cifsacl "
  2582. "mount option supported");
  2583. }
  2584. /*
  2585. * When the server supports very large writes via POSIX extensions, we can
  2586. * allow up to 2^24-1, minus the size of a WRITE_AND_X header, not including
  2587. * the RFC1001 length.
  2588. *
  2589. * Note that this might make for "interesting" allocation problems during
  2590. * writeback however as we have to allocate an array of pointers for the
  2591. * pages. A 16M write means ~32kb page array with PAGE_CACHE_SIZE == 4096.
  2592. */
  2593. #define CIFS_MAX_WSIZE ((1<<24) - 1 - sizeof(WRITE_REQ) + 4)
  2594. /*
  2595. * When the server doesn't allow large posix writes, only allow a wsize of
  2596. * 2^17-1 minus the size of the WRITE_AND_X header. That allows for a write up
  2597. * to the maximum size described by RFC1002.
  2598. */
  2599. #define CIFS_MAX_RFC1002_WSIZE ((1<<17) - 1 - sizeof(WRITE_REQ) + 4)
  2600. /*
  2601. * The default wsize is 1M. find_get_pages seems to return a maximum of 256
  2602. * pages in a single call. With PAGE_CACHE_SIZE == 4k, this means we can fill
  2603. * a single wsize request with a single call.
  2604. */
  2605. #define CIFS_DEFAULT_WSIZE (1024 * 1024)
  2606. static unsigned int
  2607. cifs_negotiate_wsize(struct cifs_tcon *tcon, struct smb_vol *pvolume_info)
  2608. {
  2609. __u64 unix_cap = le64_to_cpu(tcon->fsUnixInfo.Capability);
  2610. struct TCP_Server_Info *server = tcon->ses->server;
  2611. unsigned int wsize = pvolume_info->wsize ? pvolume_info->wsize :
  2612. CIFS_DEFAULT_WSIZE;
  2613. /* can server support 24-bit write sizes? (via UNIX extensions) */
  2614. if (!tcon->unix_ext || !(unix_cap & CIFS_UNIX_LARGE_WRITE_CAP))
  2615. wsize = min_t(unsigned int, wsize, CIFS_MAX_RFC1002_WSIZE);
  2616. /*
  2617. * no CAP_LARGE_WRITE_X or is signing enabled without CAP_UNIX set?
  2618. * Limit it to max buffer offered by the server, minus the size of the
  2619. * WRITEX header, not including the 4 byte RFC1001 length.
  2620. */
  2621. if (!(server->capabilities & CAP_LARGE_WRITE_X) ||
  2622. (!(server->capabilities & CAP_UNIX) &&
  2623. (server->sec_mode & (SECMODE_SIGN_ENABLED|SECMODE_SIGN_REQUIRED))))
  2624. wsize = min_t(unsigned int, wsize,
  2625. server->maxBuf - sizeof(WRITE_REQ) + 4);
  2626. /* hard limit of CIFS_MAX_WSIZE */
  2627. wsize = min_t(unsigned int, wsize, CIFS_MAX_WSIZE);
  2628. return wsize;
  2629. }
  2630. static int
  2631. is_path_accessible(int xid, struct cifs_tcon *tcon,
  2632. struct cifs_sb_info *cifs_sb, const char *full_path)
  2633. {
  2634. int rc;
  2635. FILE_ALL_INFO *pfile_info;
  2636. pfile_info = kmalloc(sizeof(FILE_ALL_INFO), GFP_KERNEL);
  2637. if (pfile_info == NULL)
  2638. return -ENOMEM;
  2639. rc = CIFSSMBQPathInfo(xid, tcon, full_path, pfile_info,
  2640. 0 /* not legacy */, cifs_sb->local_nls,
  2641. cifs_sb->mnt_cifs_flags &
  2642. CIFS_MOUNT_MAP_SPECIAL_CHR);
  2643. if (rc == -EOPNOTSUPP || rc == -EINVAL)
  2644. rc = SMBQueryInformation(xid, tcon, full_path, pfile_info,
  2645. cifs_sb->local_nls, cifs_sb->mnt_cifs_flags &
  2646. CIFS_MOUNT_MAP_SPECIAL_CHR);
  2647. kfree(pfile_info);
  2648. return rc;
  2649. }
  2650. static void
  2651. cleanup_volume_info_contents(struct smb_vol *volume_info)
  2652. {
  2653. kfree(volume_info->username);
  2654. kzfree(volume_info->password);
  2655. kfree(volume_info->UNC);
  2656. if (volume_info->UNCip != volume_info->UNC + 2)
  2657. kfree(volume_info->UNCip);
  2658. kfree(volume_info->domainname);
  2659. kfree(volume_info->iocharset);
  2660. kfree(volume_info->prepath);
  2661. }
  2662. void
  2663. cifs_cleanup_volume_info(struct smb_vol *volume_info)
  2664. {
  2665. if (!volume_info)
  2666. return;
  2667. cleanup_volume_info_contents(volume_info);
  2668. kfree(volume_info);
  2669. }
  2670. #ifdef CONFIG_CIFS_DFS_UPCALL
  2671. /* build_path_to_root returns full path to root when
  2672. * we do not have an exiting connection (tcon) */
  2673. static char *
  2674. build_unc_path_to_root(const struct smb_vol *vol,
  2675. const struct cifs_sb_info *cifs_sb)
  2676. {
  2677. char *full_path, *pos;
  2678. unsigned int pplen = vol->prepath ? strlen(vol->prepath) : 0;
  2679. unsigned int unc_len = strnlen(vol->UNC, MAX_TREE_SIZE + 1);
  2680. full_path = kmalloc(unc_len + pplen + 1, GFP_KERNEL);
  2681. if (full_path == NULL)
  2682. return ERR_PTR(-ENOMEM);
  2683. strncpy(full_path, vol->UNC, unc_len);
  2684. pos = full_path + unc_len;
  2685. if (pplen) {
  2686. strncpy(pos, vol->prepath, pplen);
  2687. pos += pplen;
  2688. }
  2689. *pos = '\0'; /* add trailing null */
  2690. convert_delimiter(full_path, CIFS_DIR_SEP(cifs_sb));
  2691. cFYI(1, "%s: full_path=%s", __func__, full_path);
  2692. return full_path;
  2693. }
  2694. /*
  2695. * Perform a dfs referral query for a share and (optionally) prefix
  2696. *
  2697. * If a referral is found, cifs_sb->mountdata will be (re-)allocated
  2698. * to a string containing updated options for the submount. Otherwise it
  2699. * will be left untouched.
  2700. *
  2701. * Returns the rc from get_dfs_path to the caller, which can be used to
  2702. * determine whether there were referrals.
  2703. */
  2704. static int
  2705. expand_dfs_referral(int xid, struct cifs_ses *pSesInfo,
  2706. struct smb_vol *volume_info, struct cifs_sb_info *cifs_sb,
  2707. int check_prefix)
  2708. {
  2709. int rc;
  2710. unsigned int num_referrals = 0;
  2711. struct dfs_info3_param *referrals = NULL;
  2712. char *full_path = NULL, *ref_path = NULL, *mdata = NULL;
  2713. full_path = build_unc_path_to_root(volume_info, cifs_sb);
  2714. if (IS_ERR(full_path))
  2715. return PTR_ERR(full_path);
  2716. /* For DFS paths, skip the first '\' of the UNC */
  2717. ref_path = check_prefix ? full_path + 1 : volume_info->UNC + 1;
  2718. rc = get_dfs_path(xid, pSesInfo , ref_path, cifs_sb->local_nls,
  2719. &num_referrals, &referrals,
  2720. cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR);
  2721. if (!rc && num_referrals > 0) {
  2722. char *fake_devname = NULL;
  2723. mdata = cifs_compose_mount_options(cifs_sb->mountdata,
  2724. full_path + 1, referrals,
  2725. &fake_devname);
  2726. free_dfs_info_array(referrals, num_referrals);
  2727. if (IS_ERR(mdata)) {
  2728. rc = PTR_ERR(mdata);
  2729. mdata = NULL;
  2730. } else {
  2731. cleanup_volume_info_contents(volume_info);
  2732. memset(volume_info, '\0', sizeof(*volume_info));
  2733. rc = cifs_setup_volume_info(volume_info, mdata,
  2734. fake_devname);
  2735. }
  2736. kfree(fake_devname);
  2737. kfree(cifs_sb->mountdata);
  2738. cifs_sb->mountdata = mdata;
  2739. }
  2740. kfree(full_path);
  2741. return rc;
  2742. }
  2743. #endif
  2744. static int
  2745. cifs_setup_volume_info(struct smb_vol *volume_info, char *mount_data,
  2746. const char *devname)
  2747. {
  2748. int rc = 0;
  2749. if (cifs_parse_mount_options(mount_data, devname, volume_info))
  2750. return -EINVAL;
  2751. if (volume_info->nullauth) {
  2752. cFYI(1, "null user");
  2753. volume_info->username = kzalloc(1, GFP_KERNEL);
  2754. if (volume_info->username == NULL)
  2755. return -ENOMEM;
  2756. } else if (volume_info->username) {
  2757. /* BB fixme parse for domain name here */
  2758. cFYI(1, "Username: %s", volume_info->username);
  2759. } else {
  2760. cifserror("No username specified");
  2761. /* In userspace mount helper we can get user name from alternate
  2762. locations such as env variables and files on disk */
  2763. return -EINVAL;
  2764. }
  2765. /* this is needed for ASCII cp to Unicode converts */
  2766. if (volume_info->iocharset == NULL) {
  2767. /* load_nls_default cannot return null */
  2768. volume_info->local_nls = load_nls_default();
  2769. } else {
  2770. volume_info->local_nls = load_nls(volume_info->iocharset);
  2771. if (volume_info->local_nls == NULL) {
  2772. cERROR(1, "CIFS mount error: iocharset %s not found",
  2773. volume_info->iocharset);
  2774. return -ELIBACC;
  2775. }
  2776. }
  2777. return rc;
  2778. }
  2779. struct smb_vol *
  2780. cifs_get_volume_info(char *mount_data, const char *devname)
  2781. {
  2782. int rc;
  2783. struct smb_vol *volume_info;
  2784. volume_info = kzalloc(sizeof(struct smb_vol), GFP_KERNEL);
  2785. if (!volume_info)
  2786. return ERR_PTR(-ENOMEM);
  2787. rc = cifs_setup_volume_info(volume_info, mount_data, devname);
  2788. if (rc) {
  2789. cifs_cleanup_volume_info(volume_info);
  2790. volume_info = ERR_PTR(rc);
  2791. }
  2792. return volume_info;
  2793. }
  2794. int
  2795. cifs_mount(struct cifs_sb_info *cifs_sb, struct smb_vol *volume_info)
  2796. {
  2797. int rc = 0;
  2798. int xid;
  2799. struct cifs_ses *pSesInfo;
  2800. struct cifs_tcon *tcon;
  2801. struct TCP_Server_Info *srvTcp;
  2802. char *full_path;
  2803. struct tcon_link *tlink;
  2804. #ifdef CONFIG_CIFS_DFS_UPCALL
  2805. int referral_walks_count = 0;
  2806. #endif
  2807. rc = bdi_setup_and_register(&cifs_sb->bdi, "cifs", BDI_CAP_MAP_COPY);
  2808. if (rc)
  2809. return rc;
  2810. cifs_sb->bdi.ra_pages = default_backing_dev_info.ra_pages;
  2811. #ifdef CONFIG_CIFS_DFS_UPCALL
  2812. try_mount_again:
  2813. /* cleanup activities if we're chasing a referral */
  2814. if (referral_walks_count) {
  2815. if (tcon)
  2816. cifs_put_tcon(tcon);
  2817. else if (pSesInfo)
  2818. cifs_put_smb_ses(pSesInfo);
  2819. FreeXid(xid);
  2820. }
  2821. #endif
  2822. tcon = NULL;
  2823. pSesInfo = NULL;
  2824. srvTcp = NULL;
  2825. full_path = NULL;
  2826. tlink = NULL;
  2827. xid = GetXid();
  2828. /* get a reference to a tcp session */
  2829. srvTcp = cifs_get_tcp_session(volume_info);
  2830. if (IS_ERR(srvTcp)) {
  2831. rc = PTR_ERR(srvTcp);
  2832. bdi_destroy(&cifs_sb->bdi);
  2833. goto out;
  2834. }
  2835. /* get a reference to a SMB session */
  2836. pSesInfo = cifs_get_smb_ses(srvTcp, volume_info);
  2837. if (IS_ERR(pSesInfo)) {
  2838. rc = PTR_ERR(pSesInfo);
  2839. pSesInfo = NULL;
  2840. goto mount_fail_check;
  2841. }
  2842. /* search for existing tcon to this server share */
  2843. tcon = cifs_get_tcon(pSesInfo, volume_info);
  2844. if (IS_ERR(tcon)) {
  2845. rc = PTR_ERR(tcon);
  2846. tcon = NULL;
  2847. goto remote_path_check;
  2848. }
  2849. /* tell server which Unix caps we support */
  2850. if (tcon->ses->capabilities & CAP_UNIX) {
  2851. /* reset of caps checks mount to see if unix extensions
  2852. disabled for just this mount */
  2853. reset_cifs_unix_caps(xid, tcon, cifs_sb, volume_info);
  2854. if ((tcon->ses->server->tcpStatus == CifsNeedReconnect) &&
  2855. (le64_to_cpu(tcon->fsUnixInfo.Capability) &
  2856. CIFS_UNIX_TRANSPORT_ENCRYPTION_MANDATORY_CAP)) {
  2857. rc = -EACCES;
  2858. goto mount_fail_check;
  2859. }
  2860. } else
  2861. tcon->unix_ext = 0; /* server does not support them */
  2862. /* do not care if following two calls succeed - informational */
  2863. if (!tcon->ipc) {
  2864. CIFSSMBQFSDeviceInfo(xid, tcon);
  2865. CIFSSMBQFSAttributeInfo(xid, tcon);
  2866. }
  2867. if ((tcon->unix_ext == 0) && (cifs_sb->rsize > (1024 * 127))) {
  2868. cifs_sb->rsize = 1024 * 127;
  2869. cFYI(DBG2, "no very large read support, rsize now 127K");
  2870. }
  2871. if (!(tcon->ses->capabilities & CAP_LARGE_READ_X))
  2872. cifs_sb->rsize = min(cifs_sb->rsize, CIFSMaxBufSize);
  2873. cifs_sb->wsize = cifs_negotiate_wsize(tcon, volume_info);
  2874. remote_path_check:
  2875. #ifdef CONFIG_CIFS_DFS_UPCALL
  2876. /*
  2877. * Perform an unconditional check for whether there are DFS
  2878. * referrals for this path without prefix, to provide support
  2879. * for DFS referrals from w2k8 servers which don't seem to respond
  2880. * with PATH_NOT_COVERED to requests that include the prefix.
  2881. * Chase the referral if found, otherwise continue normally.
  2882. */
  2883. if (referral_walks_count == 0) {
  2884. int refrc = expand_dfs_referral(xid, pSesInfo, volume_info,
  2885. cifs_sb, false);
  2886. if (!refrc) {
  2887. referral_walks_count++;
  2888. goto try_mount_again;
  2889. }
  2890. }
  2891. #endif
  2892. /* check if a whole path is not remote */
  2893. if (!rc && tcon) {
  2894. /* build_path_to_root works only when we have a valid tcon */
  2895. full_path = cifs_build_path_to_root(volume_info, cifs_sb, tcon);
  2896. if (full_path == NULL) {
  2897. rc = -ENOMEM;
  2898. goto mount_fail_check;
  2899. }
  2900. rc = is_path_accessible(xid, tcon, cifs_sb, full_path);
  2901. if (rc != 0 && rc != -EREMOTE) {
  2902. kfree(full_path);
  2903. goto mount_fail_check;
  2904. }
  2905. kfree(full_path);
  2906. }
  2907. /* get referral if needed */
  2908. if (rc == -EREMOTE) {
  2909. #ifdef CONFIG_CIFS_DFS_UPCALL
  2910. if (referral_walks_count > MAX_NESTED_LINKS) {
  2911. /*
  2912. * BB: when we implement proper loop detection,
  2913. * we will remove this check. But now we need it
  2914. * to prevent an indefinite loop if 'DFS tree' is
  2915. * misconfigured (i.e. has loops).
  2916. */
  2917. rc = -ELOOP;
  2918. goto mount_fail_check;
  2919. }
  2920. rc = expand_dfs_referral(xid, pSesInfo, volume_info, cifs_sb,
  2921. true);
  2922. if (!rc) {
  2923. referral_walks_count++;
  2924. goto try_mount_again;
  2925. }
  2926. goto mount_fail_check;
  2927. #else /* No DFS support, return error on mount */
  2928. rc = -EOPNOTSUPP;
  2929. #endif
  2930. }
  2931. if (rc)
  2932. goto mount_fail_check;
  2933. /* now, hang the tcon off of the superblock */
  2934. tlink = kzalloc(sizeof *tlink, GFP_KERNEL);
  2935. if (tlink == NULL) {
  2936. rc = -ENOMEM;
  2937. goto mount_fail_check;
  2938. }
  2939. tlink->tl_uid = pSesInfo->linux_uid;
  2940. tlink->tl_tcon = tcon;
  2941. tlink->tl_time = jiffies;
  2942. set_bit(TCON_LINK_MASTER, &tlink->tl_flags);
  2943. set_bit(TCON_LINK_IN_TREE, &tlink->tl_flags);
  2944. cifs_sb->master_tlink = tlink;
  2945. spin_lock(&cifs_sb->tlink_tree_lock);
  2946. tlink_rb_insert(&cifs_sb->tlink_tree, tlink);
  2947. spin_unlock(&cifs_sb->tlink_tree_lock);
  2948. queue_delayed_work(system_nrt_wq, &cifs_sb->prune_tlinks,
  2949. TLINK_IDLE_EXPIRE);
  2950. mount_fail_check:
  2951. /* on error free sesinfo and tcon struct if needed */
  2952. if (rc) {
  2953. /* If find_unc succeeded then rc == 0 so we can not end */
  2954. /* up accidentally freeing someone elses tcon struct */
  2955. if (tcon)
  2956. cifs_put_tcon(tcon);
  2957. else if (pSesInfo)
  2958. cifs_put_smb_ses(pSesInfo);
  2959. else
  2960. cifs_put_tcp_session(srvTcp);
  2961. bdi_destroy(&cifs_sb->bdi);
  2962. }
  2963. out:
  2964. FreeXid(xid);
  2965. return rc;
  2966. }
  2967. /*
  2968. * Issue a TREE_CONNECT request. Note that for IPC$ shares, that the tcon
  2969. * pointer may be NULL.
  2970. */
  2971. int
  2972. CIFSTCon(unsigned int xid, struct cifs_ses *ses,
  2973. const char *tree, struct cifs_tcon *tcon,
  2974. const struct nls_table *nls_codepage)
  2975. {
  2976. struct smb_hdr *smb_buffer;
  2977. struct smb_hdr *smb_buffer_response;
  2978. TCONX_REQ *pSMB;
  2979. TCONX_RSP *pSMBr;
  2980. unsigned char *bcc_ptr;
  2981. int rc = 0;
  2982. int length;
  2983. __u16 bytes_left, count;
  2984. if (ses == NULL)
  2985. return -EIO;
  2986. smb_buffer = cifs_buf_get();
  2987. if (smb_buffer == NULL)
  2988. return -ENOMEM;
  2989. smb_buffer_response = smb_buffer;
  2990. header_assemble(smb_buffer, SMB_COM_TREE_CONNECT_ANDX,
  2991. NULL /*no tid */ , 4 /*wct */ );
  2992. smb_buffer->Mid = GetNextMid(ses->server);
  2993. smb_buffer->Uid = ses->Suid;
  2994. pSMB = (TCONX_REQ *) smb_buffer;
  2995. pSMBr = (TCONX_RSP *) smb_buffer_response;
  2996. pSMB->AndXCommand = 0xFF;
  2997. pSMB->Flags = cpu_to_le16(TCON_EXTENDED_SECINFO);
  2998. bcc_ptr = &pSMB->Password[0];
  2999. if (!tcon || (ses->server->sec_mode & SECMODE_USER)) {
  3000. pSMB->PasswordLength = cpu_to_le16(1); /* minimum */
  3001. *bcc_ptr = 0; /* password is null byte */
  3002. bcc_ptr++; /* skip password */
  3003. /* already aligned so no need to do it below */
  3004. } else {
  3005. pSMB->PasswordLength = cpu_to_le16(CIFS_AUTH_RESP_SIZE);
  3006. /* BB FIXME add code to fail this if NTLMv2 or Kerberos
  3007. specified as required (when that support is added to
  3008. the vfs in the future) as only NTLM or the much
  3009. weaker LANMAN (which we do not send by default) is accepted
  3010. by Samba (not sure whether other servers allow
  3011. NTLMv2 password here) */
  3012. #ifdef CONFIG_CIFS_WEAK_PW_HASH
  3013. if ((global_secflags & CIFSSEC_MAY_LANMAN) &&
  3014. (ses->server->secType == LANMAN))
  3015. calc_lanman_hash(tcon->password, ses->server->cryptkey,
  3016. ses->server->sec_mode &
  3017. SECMODE_PW_ENCRYPT ? true : false,
  3018. bcc_ptr);
  3019. else
  3020. #endif /* CIFS_WEAK_PW_HASH */
  3021. rc = SMBNTencrypt(tcon->password, ses->server->cryptkey,
  3022. bcc_ptr);
  3023. bcc_ptr += CIFS_AUTH_RESP_SIZE;
  3024. if (ses->capabilities & CAP_UNICODE) {
  3025. /* must align unicode strings */
  3026. *bcc_ptr = 0; /* null byte password */
  3027. bcc_ptr++;
  3028. }
  3029. }
  3030. if (ses->server->sec_mode &
  3031. (SECMODE_SIGN_REQUIRED | SECMODE_SIGN_ENABLED))
  3032. smb_buffer->Flags2 |= SMBFLG2_SECURITY_SIGNATURE;
  3033. if (ses->capabilities & CAP_STATUS32) {
  3034. smb_buffer->Flags2 |= SMBFLG2_ERR_STATUS;
  3035. }
  3036. if (ses->capabilities & CAP_DFS) {
  3037. smb_buffer->Flags2 |= SMBFLG2_DFS;
  3038. }
  3039. if (ses->capabilities & CAP_UNICODE) {
  3040. smb_buffer->Flags2 |= SMBFLG2_UNICODE;
  3041. length =
  3042. cifs_strtoUCS((__le16 *) bcc_ptr, tree,
  3043. 6 /* max utf8 char length in bytes */ *
  3044. (/* server len*/ + 256 /* share len */), nls_codepage);
  3045. bcc_ptr += 2 * length; /* convert num 16 bit words to bytes */
  3046. bcc_ptr += 2; /* skip trailing null */
  3047. } else { /* ASCII */
  3048. strcpy(bcc_ptr, tree);
  3049. bcc_ptr += strlen(tree) + 1;
  3050. }
  3051. strcpy(bcc_ptr, "?????");
  3052. bcc_ptr += strlen("?????");
  3053. bcc_ptr += 1;
  3054. count = bcc_ptr - &pSMB->Password[0];
  3055. pSMB->hdr.smb_buf_length = cpu_to_be32(be32_to_cpu(
  3056. pSMB->hdr.smb_buf_length) + count);
  3057. pSMB->ByteCount = cpu_to_le16(count);
  3058. rc = SendReceive(xid, ses, smb_buffer, smb_buffer_response, &length,
  3059. 0);
  3060. /* above now done in SendReceive */
  3061. if ((rc == 0) && (tcon != NULL)) {
  3062. bool is_unicode;
  3063. tcon->tidStatus = CifsGood;
  3064. tcon->need_reconnect = false;
  3065. tcon->tid = smb_buffer_response->Tid;
  3066. bcc_ptr = pByteArea(smb_buffer_response);
  3067. bytes_left = get_bcc(smb_buffer_response);
  3068. length = strnlen(bcc_ptr, bytes_left - 2);
  3069. if (smb_buffer->Flags2 & SMBFLG2_UNICODE)
  3070. is_unicode = true;
  3071. else
  3072. is_unicode = false;
  3073. /* skip service field (NB: this field is always ASCII) */
  3074. if (length == 3) {
  3075. if ((bcc_ptr[0] == 'I') && (bcc_ptr[1] == 'P') &&
  3076. (bcc_ptr[2] == 'C')) {
  3077. cFYI(1, "IPC connection");
  3078. tcon->ipc = 1;
  3079. }
  3080. } else if (length == 2) {
  3081. if ((bcc_ptr[0] == 'A') && (bcc_ptr[1] == ':')) {
  3082. /* the most common case */
  3083. cFYI(1, "disk share connection");
  3084. }
  3085. }
  3086. bcc_ptr += length + 1;
  3087. bytes_left -= (length + 1);
  3088. strncpy(tcon->treeName, tree, MAX_TREE_SIZE);
  3089. /* mostly informational -- no need to fail on error here */
  3090. kfree(tcon->nativeFileSystem);
  3091. tcon->nativeFileSystem = cifs_strndup_from_ucs(bcc_ptr,
  3092. bytes_left, is_unicode,
  3093. nls_codepage);
  3094. cFYI(1, "nativeFileSystem=%s", tcon->nativeFileSystem);
  3095. if ((smb_buffer_response->WordCount == 3) ||
  3096. (smb_buffer_response->WordCount == 7))
  3097. /* field is in same location */
  3098. tcon->Flags = le16_to_cpu(pSMBr->OptionalSupport);
  3099. else
  3100. tcon->Flags = 0;
  3101. cFYI(1, "Tcon flags: 0x%x ", tcon->Flags);
  3102. } else if ((rc == 0) && tcon == NULL) {
  3103. /* all we need to save for IPC$ connection */
  3104. ses->ipc_tid = smb_buffer_response->Tid;
  3105. }
  3106. cifs_buf_release(smb_buffer);
  3107. return rc;
  3108. }
  3109. void
  3110. cifs_umount(struct cifs_sb_info *cifs_sb)
  3111. {
  3112. struct rb_root *root = &cifs_sb->tlink_tree;
  3113. struct rb_node *node;
  3114. struct tcon_link *tlink;
  3115. cancel_delayed_work_sync(&cifs_sb->prune_tlinks);
  3116. spin_lock(&cifs_sb->tlink_tree_lock);
  3117. while ((node = rb_first(root))) {
  3118. tlink = rb_entry(node, struct tcon_link, tl_rbnode);
  3119. cifs_get_tlink(tlink);
  3120. clear_bit(TCON_LINK_IN_TREE, &tlink->tl_flags);
  3121. rb_erase(node, root);
  3122. spin_unlock(&cifs_sb->tlink_tree_lock);
  3123. cifs_put_tlink(tlink);
  3124. spin_lock(&cifs_sb->tlink_tree_lock);
  3125. }
  3126. spin_unlock(&cifs_sb->tlink_tree_lock);
  3127. bdi_destroy(&cifs_sb->bdi);
  3128. kfree(cifs_sb->mountdata);
  3129. unload_nls(cifs_sb->local_nls);
  3130. kfree(cifs_sb);
  3131. }
  3132. int cifs_negotiate_protocol(unsigned int xid, struct cifs_ses *ses)
  3133. {
  3134. int rc = 0;
  3135. struct TCP_Server_Info *server = ses->server;
  3136. /* only send once per connect */
  3137. if (server->maxBuf != 0)
  3138. return 0;
  3139. rc = CIFSSMBNegotiate(xid, ses);
  3140. if (rc == -EAGAIN) {
  3141. /* retry only once on 1st time connection */
  3142. rc = CIFSSMBNegotiate(xid, ses);
  3143. if (rc == -EAGAIN)
  3144. rc = -EHOSTDOWN;
  3145. }
  3146. if (rc == 0) {
  3147. spin_lock(&GlobalMid_Lock);
  3148. if (server->tcpStatus == CifsNeedNegotiate)
  3149. server->tcpStatus = CifsGood;
  3150. else
  3151. rc = -EHOSTDOWN;
  3152. spin_unlock(&GlobalMid_Lock);
  3153. }
  3154. return rc;
  3155. }
  3156. int cifs_setup_session(unsigned int xid, struct cifs_ses *ses,
  3157. struct nls_table *nls_info)
  3158. {
  3159. int rc = 0;
  3160. struct TCP_Server_Info *server = ses->server;
  3161. ses->flags = 0;
  3162. ses->capabilities = server->capabilities;
  3163. if (linuxExtEnabled == 0)
  3164. ses->capabilities &= (~CAP_UNIX);
  3165. cFYI(1, "Security Mode: 0x%x Capabilities: 0x%x TimeAdjust: %d",
  3166. server->sec_mode, server->capabilities, server->timeAdj);
  3167. rc = CIFS_SessSetup(xid, ses, nls_info);
  3168. if (rc) {
  3169. cERROR(1, "Send error in SessSetup = %d", rc);
  3170. } else {
  3171. mutex_lock(&ses->server->srv_mutex);
  3172. if (!server->session_estab) {
  3173. server->session_key.response = ses->auth_key.response;
  3174. server->session_key.len = ses->auth_key.len;
  3175. server->sequence_number = 0x2;
  3176. server->session_estab = true;
  3177. ses->auth_key.response = NULL;
  3178. }
  3179. mutex_unlock(&server->srv_mutex);
  3180. cFYI(1, "CIFS Session Established successfully");
  3181. spin_lock(&GlobalMid_Lock);
  3182. ses->status = CifsGood;
  3183. ses->need_reconnect = false;
  3184. spin_unlock(&GlobalMid_Lock);
  3185. }
  3186. kfree(ses->auth_key.response);
  3187. ses->auth_key.response = NULL;
  3188. ses->auth_key.len = 0;
  3189. kfree(ses->ntlmssp);
  3190. ses->ntlmssp = NULL;
  3191. return rc;
  3192. }
  3193. static struct cifs_tcon *
  3194. cifs_construct_tcon(struct cifs_sb_info *cifs_sb, uid_t fsuid)
  3195. {
  3196. struct cifs_tcon *master_tcon = cifs_sb_master_tcon(cifs_sb);
  3197. struct cifs_ses *ses;
  3198. struct cifs_tcon *tcon = NULL;
  3199. struct smb_vol *vol_info;
  3200. char username[28]; /* big enough for "krb50x" + hex of ULONG_MAX 6+16 */
  3201. /* We used to have this as MAX_USERNAME which is */
  3202. /* way too big now (256 instead of 32) */
  3203. vol_info = kzalloc(sizeof(*vol_info), GFP_KERNEL);
  3204. if (vol_info == NULL) {
  3205. tcon = ERR_PTR(-ENOMEM);
  3206. goto out;
  3207. }
  3208. snprintf(username, sizeof(username), "krb50x%x", fsuid);
  3209. vol_info->username = username;
  3210. vol_info->local_nls = cifs_sb->local_nls;
  3211. vol_info->linux_uid = fsuid;
  3212. vol_info->cred_uid = fsuid;
  3213. vol_info->UNC = master_tcon->treeName;
  3214. vol_info->retry = master_tcon->retry;
  3215. vol_info->nocase = master_tcon->nocase;
  3216. vol_info->local_lease = master_tcon->local_lease;
  3217. vol_info->no_linux_ext = !master_tcon->unix_ext;
  3218. /* FIXME: allow for other secFlg settings */
  3219. vol_info->secFlg = CIFSSEC_MUST_KRB5;
  3220. /* get a reference for the same TCP session */
  3221. spin_lock(&cifs_tcp_ses_lock);
  3222. ++master_tcon->ses->server->srv_count;
  3223. spin_unlock(&cifs_tcp_ses_lock);
  3224. ses = cifs_get_smb_ses(master_tcon->ses->server, vol_info);
  3225. if (IS_ERR(ses)) {
  3226. tcon = (struct cifs_tcon *)ses;
  3227. cifs_put_tcp_session(master_tcon->ses->server);
  3228. goto out;
  3229. }
  3230. tcon = cifs_get_tcon(ses, vol_info);
  3231. if (IS_ERR(tcon)) {
  3232. cifs_put_smb_ses(ses);
  3233. goto out;
  3234. }
  3235. if (ses->capabilities & CAP_UNIX)
  3236. reset_cifs_unix_caps(0, tcon, NULL, vol_info);
  3237. out:
  3238. kfree(vol_info);
  3239. return tcon;
  3240. }
  3241. struct cifs_tcon *
  3242. cifs_sb_master_tcon(struct cifs_sb_info *cifs_sb)
  3243. {
  3244. return tlink_tcon(cifs_sb_master_tlink(cifs_sb));
  3245. }
  3246. static int
  3247. cifs_sb_tcon_pending_wait(void *unused)
  3248. {
  3249. schedule();
  3250. return signal_pending(current) ? -ERESTARTSYS : 0;
  3251. }
  3252. /* find and return a tlink with given uid */
  3253. static struct tcon_link *
  3254. tlink_rb_search(struct rb_root *root, uid_t uid)
  3255. {
  3256. struct rb_node *node = root->rb_node;
  3257. struct tcon_link *tlink;
  3258. while (node) {
  3259. tlink = rb_entry(node, struct tcon_link, tl_rbnode);
  3260. if (tlink->tl_uid > uid)
  3261. node = node->rb_left;
  3262. else if (tlink->tl_uid < uid)
  3263. node = node->rb_right;
  3264. else
  3265. return tlink;
  3266. }
  3267. return NULL;
  3268. }
  3269. /* insert a tcon_link into the tree */
  3270. static void
  3271. tlink_rb_insert(struct rb_root *root, struct tcon_link *new_tlink)
  3272. {
  3273. struct rb_node **new = &(root->rb_node), *parent = NULL;
  3274. struct tcon_link *tlink;
  3275. while (*new) {
  3276. tlink = rb_entry(*new, struct tcon_link, tl_rbnode);
  3277. parent = *new;
  3278. if (tlink->tl_uid > new_tlink->tl_uid)
  3279. new = &((*new)->rb_left);
  3280. else
  3281. new = &((*new)->rb_right);
  3282. }
  3283. rb_link_node(&new_tlink->tl_rbnode, parent, new);
  3284. rb_insert_color(&new_tlink->tl_rbnode, root);
  3285. }
  3286. /*
  3287. * Find or construct an appropriate tcon given a cifs_sb and the fsuid of the
  3288. * current task.
  3289. *
  3290. * If the superblock doesn't refer to a multiuser mount, then just return
  3291. * the master tcon for the mount.
  3292. *
  3293. * First, search the rbtree for an existing tcon for this fsuid. If one
  3294. * exists, then check to see if it's pending construction. If it is then wait
  3295. * for construction to complete. Once it's no longer pending, check to see if
  3296. * it failed and either return an error or retry construction, depending on
  3297. * the timeout.
  3298. *
  3299. * If one doesn't exist then insert a new tcon_link struct into the tree and
  3300. * try to construct a new one.
  3301. */
  3302. struct tcon_link *
  3303. cifs_sb_tlink(struct cifs_sb_info *cifs_sb)
  3304. {
  3305. int ret;
  3306. uid_t fsuid = current_fsuid();
  3307. struct tcon_link *tlink, *newtlink;
  3308. if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MULTIUSER))
  3309. return cifs_get_tlink(cifs_sb_master_tlink(cifs_sb));
  3310. spin_lock(&cifs_sb->tlink_tree_lock);
  3311. tlink = tlink_rb_search(&cifs_sb->tlink_tree, fsuid);
  3312. if (tlink)
  3313. cifs_get_tlink(tlink);
  3314. spin_unlock(&cifs_sb->tlink_tree_lock);
  3315. if (tlink == NULL) {
  3316. newtlink = kzalloc(sizeof(*tlink), GFP_KERNEL);
  3317. if (newtlink == NULL)
  3318. return ERR_PTR(-ENOMEM);
  3319. newtlink->tl_uid = fsuid;
  3320. newtlink->tl_tcon = ERR_PTR(-EACCES);
  3321. set_bit(TCON_LINK_PENDING, &newtlink->tl_flags);
  3322. set_bit(TCON_LINK_IN_TREE, &newtlink->tl_flags);
  3323. cifs_get_tlink(newtlink);
  3324. spin_lock(&cifs_sb->tlink_tree_lock);
  3325. /* was one inserted after previous search? */
  3326. tlink = tlink_rb_search(&cifs_sb->tlink_tree, fsuid);
  3327. if (tlink) {
  3328. cifs_get_tlink(tlink);
  3329. spin_unlock(&cifs_sb->tlink_tree_lock);
  3330. kfree(newtlink);
  3331. goto wait_for_construction;
  3332. }
  3333. tlink = newtlink;
  3334. tlink_rb_insert(&cifs_sb->tlink_tree, tlink);
  3335. spin_unlock(&cifs_sb->tlink_tree_lock);
  3336. } else {
  3337. wait_for_construction:
  3338. ret = wait_on_bit(&tlink->tl_flags, TCON_LINK_PENDING,
  3339. cifs_sb_tcon_pending_wait,
  3340. TASK_INTERRUPTIBLE);
  3341. if (ret) {
  3342. cifs_put_tlink(tlink);
  3343. return ERR_PTR(ret);
  3344. }
  3345. /* if it's good, return it */
  3346. if (!IS_ERR(tlink->tl_tcon))
  3347. return tlink;
  3348. /* return error if we tried this already recently */
  3349. if (time_before(jiffies, tlink->tl_time + TLINK_ERROR_EXPIRE)) {
  3350. cifs_put_tlink(tlink);
  3351. return ERR_PTR(-EACCES);
  3352. }
  3353. if (test_and_set_bit(TCON_LINK_PENDING, &tlink->tl_flags))
  3354. goto wait_for_construction;
  3355. }
  3356. tlink->tl_tcon = cifs_construct_tcon(cifs_sb, fsuid);
  3357. clear_bit(TCON_LINK_PENDING, &tlink->tl_flags);
  3358. wake_up_bit(&tlink->tl_flags, TCON_LINK_PENDING);
  3359. if (IS_ERR(tlink->tl_tcon)) {
  3360. cifs_put_tlink(tlink);
  3361. return ERR_PTR(-EACCES);
  3362. }
  3363. return tlink;
  3364. }
  3365. /*
  3366. * periodic workqueue job that scans tcon_tree for a superblock and closes
  3367. * out tcons.
  3368. */
  3369. static void
  3370. cifs_prune_tlinks(struct work_struct *work)
  3371. {
  3372. struct cifs_sb_info *cifs_sb = container_of(work, struct cifs_sb_info,
  3373. prune_tlinks.work);
  3374. struct rb_root *root = &cifs_sb->tlink_tree;
  3375. struct rb_node *node = rb_first(root);
  3376. struct rb_node *tmp;
  3377. struct tcon_link *tlink;
  3378. /*
  3379. * Because we drop the spinlock in the loop in order to put the tlink
  3380. * it's not guarded against removal of links from the tree. The only
  3381. * places that remove entries from the tree are this function and
  3382. * umounts. Because this function is non-reentrant and is canceled
  3383. * before umount can proceed, this is safe.
  3384. */
  3385. spin_lock(&cifs_sb->tlink_tree_lock);
  3386. node = rb_first(root);
  3387. while (node != NULL) {
  3388. tmp = node;
  3389. node = rb_next(tmp);
  3390. tlink = rb_entry(tmp, struct tcon_link, tl_rbnode);
  3391. if (test_bit(TCON_LINK_MASTER, &tlink->tl_flags) ||
  3392. atomic_read(&tlink->tl_count) != 0 ||
  3393. time_after(tlink->tl_time + TLINK_IDLE_EXPIRE, jiffies))
  3394. continue;
  3395. cifs_get_tlink(tlink);
  3396. clear_bit(TCON_LINK_IN_TREE, &tlink->tl_flags);
  3397. rb_erase(tmp, root);
  3398. spin_unlock(&cifs_sb->tlink_tree_lock);
  3399. cifs_put_tlink(tlink);
  3400. spin_lock(&cifs_sb->tlink_tree_lock);
  3401. }
  3402. spin_unlock(&cifs_sb->tlink_tree_lock);
  3403. queue_delayed_work(system_nrt_wq, &cifs_sb->prune_tlinks,
  3404. TLINK_IDLE_EXPIRE);
  3405. }