connect.c 115 KB

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