connect.c 115 KB

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