connect.c 106 KB

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