mgmt.c 196 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962
  1. /*
  2. BlueZ - Bluetooth protocol stack for Linux
  3. Copyright (C) 2010 Nokia Corporation
  4. Copyright (C) 2011-2012 Intel Corporation
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License version 2 as
  7. published by the Free Software Foundation;
  8. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  9. OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  10. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS.
  11. IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) AND AUTHOR(S) BE LIABLE FOR ANY
  12. CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES
  13. WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  14. ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  15. OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  16. ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS,
  17. COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS
  18. SOFTWARE IS DISCLAIMED.
  19. */
  20. /* Bluetooth HCI Management interface */
  21. #include <linux/module.h>
  22. #include <asm/unaligned.h>
  23. #include <net/bluetooth/bluetooth.h>
  24. #include <net/bluetooth/hci_core.h>
  25. #include <net/bluetooth/hci_sock.h>
  26. #include <net/bluetooth/l2cap.h>
  27. #include <net/bluetooth/mgmt.h>
  28. #include "hci_request.h"
  29. #include "smp.h"
  30. #include "mgmt_util.h"
  31. #define MGMT_VERSION 1
  32. #define MGMT_REVISION 14
  33. static const u16 mgmt_commands[] = {
  34. MGMT_OP_READ_INDEX_LIST,
  35. MGMT_OP_READ_INFO,
  36. MGMT_OP_SET_POWERED,
  37. MGMT_OP_SET_DISCOVERABLE,
  38. MGMT_OP_SET_CONNECTABLE,
  39. MGMT_OP_SET_FAST_CONNECTABLE,
  40. MGMT_OP_SET_BONDABLE,
  41. MGMT_OP_SET_LINK_SECURITY,
  42. MGMT_OP_SET_SSP,
  43. MGMT_OP_SET_HS,
  44. MGMT_OP_SET_LE,
  45. MGMT_OP_SET_DEV_CLASS,
  46. MGMT_OP_SET_LOCAL_NAME,
  47. MGMT_OP_ADD_UUID,
  48. MGMT_OP_REMOVE_UUID,
  49. MGMT_OP_LOAD_LINK_KEYS,
  50. MGMT_OP_LOAD_LONG_TERM_KEYS,
  51. MGMT_OP_DISCONNECT,
  52. MGMT_OP_GET_CONNECTIONS,
  53. MGMT_OP_PIN_CODE_REPLY,
  54. MGMT_OP_PIN_CODE_NEG_REPLY,
  55. MGMT_OP_SET_IO_CAPABILITY,
  56. MGMT_OP_PAIR_DEVICE,
  57. MGMT_OP_CANCEL_PAIR_DEVICE,
  58. MGMT_OP_UNPAIR_DEVICE,
  59. MGMT_OP_USER_CONFIRM_REPLY,
  60. MGMT_OP_USER_CONFIRM_NEG_REPLY,
  61. MGMT_OP_USER_PASSKEY_REPLY,
  62. MGMT_OP_USER_PASSKEY_NEG_REPLY,
  63. MGMT_OP_READ_LOCAL_OOB_DATA,
  64. MGMT_OP_ADD_REMOTE_OOB_DATA,
  65. MGMT_OP_REMOVE_REMOTE_OOB_DATA,
  66. MGMT_OP_START_DISCOVERY,
  67. MGMT_OP_STOP_DISCOVERY,
  68. MGMT_OP_CONFIRM_NAME,
  69. MGMT_OP_BLOCK_DEVICE,
  70. MGMT_OP_UNBLOCK_DEVICE,
  71. MGMT_OP_SET_DEVICE_ID,
  72. MGMT_OP_SET_ADVERTISING,
  73. MGMT_OP_SET_BREDR,
  74. MGMT_OP_SET_STATIC_ADDRESS,
  75. MGMT_OP_SET_SCAN_PARAMS,
  76. MGMT_OP_SET_SECURE_CONN,
  77. MGMT_OP_SET_DEBUG_KEYS,
  78. MGMT_OP_SET_PRIVACY,
  79. MGMT_OP_LOAD_IRKS,
  80. MGMT_OP_GET_CONN_INFO,
  81. MGMT_OP_GET_CLOCK_INFO,
  82. MGMT_OP_ADD_DEVICE,
  83. MGMT_OP_REMOVE_DEVICE,
  84. MGMT_OP_LOAD_CONN_PARAM,
  85. MGMT_OP_READ_UNCONF_INDEX_LIST,
  86. MGMT_OP_READ_CONFIG_INFO,
  87. MGMT_OP_SET_EXTERNAL_CONFIG,
  88. MGMT_OP_SET_PUBLIC_ADDRESS,
  89. MGMT_OP_START_SERVICE_DISCOVERY,
  90. MGMT_OP_READ_LOCAL_OOB_EXT_DATA,
  91. MGMT_OP_READ_EXT_INDEX_LIST,
  92. MGMT_OP_READ_ADV_FEATURES,
  93. MGMT_OP_ADD_ADVERTISING,
  94. MGMT_OP_REMOVE_ADVERTISING,
  95. MGMT_OP_GET_ADV_SIZE_INFO,
  96. MGMT_OP_START_LIMITED_DISCOVERY,
  97. MGMT_OP_READ_EXT_INFO,
  98. MGMT_OP_SET_APPEARANCE,
  99. };
  100. static const u16 mgmt_events[] = {
  101. MGMT_EV_CONTROLLER_ERROR,
  102. MGMT_EV_INDEX_ADDED,
  103. MGMT_EV_INDEX_REMOVED,
  104. MGMT_EV_NEW_SETTINGS,
  105. MGMT_EV_CLASS_OF_DEV_CHANGED,
  106. MGMT_EV_LOCAL_NAME_CHANGED,
  107. MGMT_EV_NEW_LINK_KEY,
  108. MGMT_EV_NEW_LONG_TERM_KEY,
  109. MGMT_EV_DEVICE_CONNECTED,
  110. MGMT_EV_DEVICE_DISCONNECTED,
  111. MGMT_EV_CONNECT_FAILED,
  112. MGMT_EV_PIN_CODE_REQUEST,
  113. MGMT_EV_USER_CONFIRM_REQUEST,
  114. MGMT_EV_USER_PASSKEY_REQUEST,
  115. MGMT_EV_AUTH_FAILED,
  116. MGMT_EV_DEVICE_FOUND,
  117. MGMT_EV_DISCOVERING,
  118. MGMT_EV_DEVICE_BLOCKED,
  119. MGMT_EV_DEVICE_UNBLOCKED,
  120. MGMT_EV_DEVICE_UNPAIRED,
  121. MGMT_EV_PASSKEY_NOTIFY,
  122. MGMT_EV_NEW_IRK,
  123. MGMT_EV_NEW_CSRK,
  124. MGMT_EV_DEVICE_ADDED,
  125. MGMT_EV_DEVICE_REMOVED,
  126. MGMT_EV_NEW_CONN_PARAM,
  127. MGMT_EV_UNCONF_INDEX_ADDED,
  128. MGMT_EV_UNCONF_INDEX_REMOVED,
  129. MGMT_EV_NEW_CONFIG_OPTIONS,
  130. MGMT_EV_EXT_INDEX_ADDED,
  131. MGMT_EV_EXT_INDEX_REMOVED,
  132. MGMT_EV_LOCAL_OOB_DATA_UPDATED,
  133. MGMT_EV_ADVERTISING_ADDED,
  134. MGMT_EV_ADVERTISING_REMOVED,
  135. MGMT_EV_EXT_INFO_CHANGED,
  136. };
  137. static const u16 mgmt_untrusted_commands[] = {
  138. MGMT_OP_READ_INDEX_LIST,
  139. MGMT_OP_READ_INFO,
  140. MGMT_OP_READ_UNCONF_INDEX_LIST,
  141. MGMT_OP_READ_CONFIG_INFO,
  142. MGMT_OP_READ_EXT_INDEX_LIST,
  143. MGMT_OP_READ_EXT_INFO,
  144. };
  145. static const u16 mgmt_untrusted_events[] = {
  146. MGMT_EV_INDEX_ADDED,
  147. MGMT_EV_INDEX_REMOVED,
  148. MGMT_EV_NEW_SETTINGS,
  149. MGMT_EV_CLASS_OF_DEV_CHANGED,
  150. MGMT_EV_LOCAL_NAME_CHANGED,
  151. MGMT_EV_UNCONF_INDEX_ADDED,
  152. MGMT_EV_UNCONF_INDEX_REMOVED,
  153. MGMT_EV_NEW_CONFIG_OPTIONS,
  154. MGMT_EV_EXT_INDEX_ADDED,
  155. MGMT_EV_EXT_INDEX_REMOVED,
  156. MGMT_EV_EXT_INFO_CHANGED,
  157. };
  158. #define CACHE_TIMEOUT msecs_to_jiffies(2 * 1000)
  159. #define ZERO_KEY "\x00\x00\x00\x00\x00\x00\x00\x00" \
  160. "\x00\x00\x00\x00\x00\x00\x00\x00"
  161. /* HCI to MGMT error code conversion table */
  162. static u8 mgmt_status_table[] = {
  163. MGMT_STATUS_SUCCESS,
  164. MGMT_STATUS_UNKNOWN_COMMAND, /* Unknown Command */
  165. MGMT_STATUS_NOT_CONNECTED, /* No Connection */
  166. MGMT_STATUS_FAILED, /* Hardware Failure */
  167. MGMT_STATUS_CONNECT_FAILED, /* Page Timeout */
  168. MGMT_STATUS_AUTH_FAILED, /* Authentication Failed */
  169. MGMT_STATUS_AUTH_FAILED, /* PIN or Key Missing */
  170. MGMT_STATUS_NO_RESOURCES, /* Memory Full */
  171. MGMT_STATUS_TIMEOUT, /* Connection Timeout */
  172. MGMT_STATUS_NO_RESOURCES, /* Max Number of Connections */
  173. MGMT_STATUS_NO_RESOURCES, /* Max Number of SCO Connections */
  174. MGMT_STATUS_ALREADY_CONNECTED, /* ACL Connection Exists */
  175. MGMT_STATUS_BUSY, /* Command Disallowed */
  176. MGMT_STATUS_NO_RESOURCES, /* Rejected Limited Resources */
  177. MGMT_STATUS_REJECTED, /* Rejected Security */
  178. MGMT_STATUS_REJECTED, /* Rejected Personal */
  179. MGMT_STATUS_TIMEOUT, /* Host Timeout */
  180. MGMT_STATUS_NOT_SUPPORTED, /* Unsupported Feature */
  181. MGMT_STATUS_INVALID_PARAMS, /* Invalid Parameters */
  182. MGMT_STATUS_DISCONNECTED, /* OE User Ended Connection */
  183. MGMT_STATUS_NO_RESOURCES, /* OE Low Resources */
  184. MGMT_STATUS_DISCONNECTED, /* OE Power Off */
  185. MGMT_STATUS_DISCONNECTED, /* Connection Terminated */
  186. MGMT_STATUS_BUSY, /* Repeated Attempts */
  187. MGMT_STATUS_REJECTED, /* Pairing Not Allowed */
  188. MGMT_STATUS_FAILED, /* Unknown LMP PDU */
  189. MGMT_STATUS_NOT_SUPPORTED, /* Unsupported Remote Feature */
  190. MGMT_STATUS_REJECTED, /* SCO Offset Rejected */
  191. MGMT_STATUS_REJECTED, /* SCO Interval Rejected */
  192. MGMT_STATUS_REJECTED, /* Air Mode Rejected */
  193. MGMT_STATUS_INVALID_PARAMS, /* Invalid LMP Parameters */
  194. MGMT_STATUS_FAILED, /* Unspecified Error */
  195. MGMT_STATUS_NOT_SUPPORTED, /* Unsupported LMP Parameter Value */
  196. MGMT_STATUS_FAILED, /* Role Change Not Allowed */
  197. MGMT_STATUS_TIMEOUT, /* LMP Response Timeout */
  198. MGMT_STATUS_FAILED, /* LMP Error Transaction Collision */
  199. MGMT_STATUS_FAILED, /* LMP PDU Not Allowed */
  200. MGMT_STATUS_REJECTED, /* Encryption Mode Not Accepted */
  201. MGMT_STATUS_FAILED, /* Unit Link Key Used */
  202. MGMT_STATUS_NOT_SUPPORTED, /* QoS Not Supported */
  203. MGMT_STATUS_TIMEOUT, /* Instant Passed */
  204. MGMT_STATUS_NOT_SUPPORTED, /* Pairing Not Supported */
  205. MGMT_STATUS_FAILED, /* Transaction Collision */
  206. MGMT_STATUS_INVALID_PARAMS, /* Unacceptable Parameter */
  207. MGMT_STATUS_REJECTED, /* QoS Rejected */
  208. MGMT_STATUS_NOT_SUPPORTED, /* Classification Not Supported */
  209. MGMT_STATUS_REJECTED, /* Insufficient Security */
  210. MGMT_STATUS_INVALID_PARAMS, /* Parameter Out Of Range */
  211. MGMT_STATUS_BUSY, /* Role Switch Pending */
  212. MGMT_STATUS_FAILED, /* Slot Violation */
  213. MGMT_STATUS_FAILED, /* Role Switch Failed */
  214. MGMT_STATUS_INVALID_PARAMS, /* EIR Too Large */
  215. MGMT_STATUS_NOT_SUPPORTED, /* Simple Pairing Not Supported */
  216. MGMT_STATUS_BUSY, /* Host Busy Pairing */
  217. MGMT_STATUS_REJECTED, /* Rejected, No Suitable Channel */
  218. MGMT_STATUS_BUSY, /* Controller Busy */
  219. MGMT_STATUS_INVALID_PARAMS, /* Unsuitable Connection Interval */
  220. MGMT_STATUS_TIMEOUT, /* Directed Advertising Timeout */
  221. MGMT_STATUS_AUTH_FAILED, /* Terminated Due to MIC Failure */
  222. MGMT_STATUS_CONNECT_FAILED, /* Connection Establishment Failed */
  223. MGMT_STATUS_CONNECT_FAILED, /* MAC Connection Failed */
  224. };
  225. static u8 mgmt_status(u8 hci_status)
  226. {
  227. if (hci_status < ARRAY_SIZE(mgmt_status_table))
  228. return mgmt_status_table[hci_status];
  229. return MGMT_STATUS_FAILED;
  230. }
  231. static int mgmt_index_event(u16 event, struct hci_dev *hdev, void *data,
  232. u16 len, int flag)
  233. {
  234. return mgmt_send_event(event, hdev, HCI_CHANNEL_CONTROL, data, len,
  235. flag, NULL);
  236. }
  237. static int mgmt_limited_event(u16 event, struct hci_dev *hdev, void *data,
  238. u16 len, int flag, struct sock *skip_sk)
  239. {
  240. return mgmt_send_event(event, hdev, HCI_CHANNEL_CONTROL, data, len,
  241. flag, skip_sk);
  242. }
  243. static int mgmt_event(u16 event, struct hci_dev *hdev, void *data, u16 len,
  244. struct sock *skip_sk)
  245. {
  246. return mgmt_send_event(event, hdev, HCI_CHANNEL_CONTROL, data, len,
  247. HCI_SOCK_TRUSTED, skip_sk);
  248. }
  249. static u8 le_addr_type(u8 mgmt_addr_type)
  250. {
  251. if (mgmt_addr_type == BDADDR_LE_PUBLIC)
  252. return ADDR_LE_DEV_PUBLIC;
  253. else
  254. return ADDR_LE_DEV_RANDOM;
  255. }
  256. void mgmt_fill_version_info(void *ver)
  257. {
  258. struct mgmt_rp_read_version *rp = ver;
  259. rp->version = MGMT_VERSION;
  260. rp->revision = cpu_to_le16(MGMT_REVISION);
  261. }
  262. static int read_version(struct sock *sk, struct hci_dev *hdev, void *data,
  263. u16 data_len)
  264. {
  265. struct mgmt_rp_read_version rp;
  266. BT_DBG("sock %p", sk);
  267. mgmt_fill_version_info(&rp);
  268. return mgmt_cmd_complete(sk, MGMT_INDEX_NONE, MGMT_OP_READ_VERSION, 0,
  269. &rp, sizeof(rp));
  270. }
  271. static int read_commands(struct sock *sk, struct hci_dev *hdev, void *data,
  272. u16 data_len)
  273. {
  274. struct mgmt_rp_read_commands *rp;
  275. u16 num_commands, num_events;
  276. size_t rp_size;
  277. int i, err;
  278. BT_DBG("sock %p", sk);
  279. if (hci_sock_test_flag(sk, HCI_SOCK_TRUSTED)) {
  280. num_commands = ARRAY_SIZE(mgmt_commands);
  281. num_events = ARRAY_SIZE(mgmt_events);
  282. } else {
  283. num_commands = ARRAY_SIZE(mgmt_untrusted_commands);
  284. num_events = ARRAY_SIZE(mgmt_untrusted_events);
  285. }
  286. rp_size = sizeof(*rp) + ((num_commands + num_events) * sizeof(u16));
  287. rp = kmalloc(rp_size, GFP_KERNEL);
  288. if (!rp)
  289. return -ENOMEM;
  290. rp->num_commands = cpu_to_le16(num_commands);
  291. rp->num_events = cpu_to_le16(num_events);
  292. if (hci_sock_test_flag(sk, HCI_SOCK_TRUSTED)) {
  293. __le16 *opcode = rp->opcodes;
  294. for (i = 0; i < num_commands; i++, opcode++)
  295. put_unaligned_le16(mgmt_commands[i], opcode);
  296. for (i = 0; i < num_events; i++, opcode++)
  297. put_unaligned_le16(mgmt_events[i], opcode);
  298. } else {
  299. __le16 *opcode = rp->opcodes;
  300. for (i = 0; i < num_commands; i++, opcode++)
  301. put_unaligned_le16(mgmt_untrusted_commands[i], opcode);
  302. for (i = 0; i < num_events; i++, opcode++)
  303. put_unaligned_le16(mgmt_untrusted_events[i], opcode);
  304. }
  305. err = mgmt_cmd_complete(sk, MGMT_INDEX_NONE, MGMT_OP_READ_COMMANDS, 0,
  306. rp, rp_size);
  307. kfree(rp);
  308. return err;
  309. }
  310. static int read_index_list(struct sock *sk, struct hci_dev *hdev, void *data,
  311. u16 data_len)
  312. {
  313. struct mgmt_rp_read_index_list *rp;
  314. struct hci_dev *d;
  315. size_t rp_len;
  316. u16 count;
  317. int err;
  318. BT_DBG("sock %p", sk);
  319. read_lock(&hci_dev_list_lock);
  320. count = 0;
  321. list_for_each_entry(d, &hci_dev_list, list) {
  322. if (d->dev_type == HCI_PRIMARY &&
  323. !hci_dev_test_flag(d, HCI_UNCONFIGURED))
  324. count++;
  325. }
  326. rp_len = sizeof(*rp) + (2 * count);
  327. rp = kmalloc(rp_len, GFP_ATOMIC);
  328. if (!rp) {
  329. read_unlock(&hci_dev_list_lock);
  330. return -ENOMEM;
  331. }
  332. count = 0;
  333. list_for_each_entry(d, &hci_dev_list, list) {
  334. if (hci_dev_test_flag(d, HCI_SETUP) ||
  335. hci_dev_test_flag(d, HCI_CONFIG) ||
  336. hci_dev_test_flag(d, HCI_USER_CHANNEL))
  337. continue;
  338. /* Devices marked as raw-only are neither configured
  339. * nor unconfigured controllers.
  340. */
  341. if (test_bit(HCI_QUIRK_RAW_DEVICE, &d->quirks))
  342. continue;
  343. if (d->dev_type == HCI_PRIMARY &&
  344. !hci_dev_test_flag(d, HCI_UNCONFIGURED)) {
  345. rp->index[count++] = cpu_to_le16(d->id);
  346. BT_DBG("Added hci%u", d->id);
  347. }
  348. }
  349. rp->num_controllers = cpu_to_le16(count);
  350. rp_len = sizeof(*rp) + (2 * count);
  351. read_unlock(&hci_dev_list_lock);
  352. err = mgmt_cmd_complete(sk, MGMT_INDEX_NONE, MGMT_OP_READ_INDEX_LIST,
  353. 0, rp, rp_len);
  354. kfree(rp);
  355. return err;
  356. }
  357. static int read_unconf_index_list(struct sock *sk, struct hci_dev *hdev,
  358. void *data, u16 data_len)
  359. {
  360. struct mgmt_rp_read_unconf_index_list *rp;
  361. struct hci_dev *d;
  362. size_t rp_len;
  363. u16 count;
  364. int err;
  365. BT_DBG("sock %p", sk);
  366. read_lock(&hci_dev_list_lock);
  367. count = 0;
  368. list_for_each_entry(d, &hci_dev_list, list) {
  369. if (d->dev_type == HCI_PRIMARY &&
  370. hci_dev_test_flag(d, HCI_UNCONFIGURED))
  371. count++;
  372. }
  373. rp_len = sizeof(*rp) + (2 * count);
  374. rp = kmalloc(rp_len, GFP_ATOMIC);
  375. if (!rp) {
  376. read_unlock(&hci_dev_list_lock);
  377. return -ENOMEM;
  378. }
  379. count = 0;
  380. list_for_each_entry(d, &hci_dev_list, list) {
  381. if (hci_dev_test_flag(d, HCI_SETUP) ||
  382. hci_dev_test_flag(d, HCI_CONFIG) ||
  383. hci_dev_test_flag(d, HCI_USER_CHANNEL))
  384. continue;
  385. /* Devices marked as raw-only are neither configured
  386. * nor unconfigured controllers.
  387. */
  388. if (test_bit(HCI_QUIRK_RAW_DEVICE, &d->quirks))
  389. continue;
  390. if (d->dev_type == HCI_PRIMARY &&
  391. hci_dev_test_flag(d, HCI_UNCONFIGURED)) {
  392. rp->index[count++] = cpu_to_le16(d->id);
  393. BT_DBG("Added hci%u", d->id);
  394. }
  395. }
  396. rp->num_controllers = cpu_to_le16(count);
  397. rp_len = sizeof(*rp) + (2 * count);
  398. read_unlock(&hci_dev_list_lock);
  399. err = mgmt_cmd_complete(sk, MGMT_INDEX_NONE,
  400. MGMT_OP_READ_UNCONF_INDEX_LIST, 0, rp, rp_len);
  401. kfree(rp);
  402. return err;
  403. }
  404. static int read_ext_index_list(struct sock *sk, struct hci_dev *hdev,
  405. void *data, u16 data_len)
  406. {
  407. struct mgmt_rp_read_ext_index_list *rp;
  408. struct hci_dev *d;
  409. size_t rp_len;
  410. u16 count;
  411. int err;
  412. BT_DBG("sock %p", sk);
  413. read_lock(&hci_dev_list_lock);
  414. count = 0;
  415. list_for_each_entry(d, &hci_dev_list, list) {
  416. if (d->dev_type == HCI_PRIMARY || d->dev_type == HCI_AMP)
  417. count++;
  418. }
  419. rp_len = sizeof(*rp) + (sizeof(rp->entry[0]) * count);
  420. rp = kmalloc(rp_len, GFP_ATOMIC);
  421. if (!rp) {
  422. read_unlock(&hci_dev_list_lock);
  423. return -ENOMEM;
  424. }
  425. count = 0;
  426. list_for_each_entry(d, &hci_dev_list, list) {
  427. if (hci_dev_test_flag(d, HCI_SETUP) ||
  428. hci_dev_test_flag(d, HCI_CONFIG) ||
  429. hci_dev_test_flag(d, HCI_USER_CHANNEL))
  430. continue;
  431. /* Devices marked as raw-only are neither configured
  432. * nor unconfigured controllers.
  433. */
  434. if (test_bit(HCI_QUIRK_RAW_DEVICE, &d->quirks))
  435. continue;
  436. if (d->dev_type == HCI_PRIMARY) {
  437. if (hci_dev_test_flag(d, HCI_UNCONFIGURED))
  438. rp->entry[count].type = 0x01;
  439. else
  440. rp->entry[count].type = 0x00;
  441. } else if (d->dev_type == HCI_AMP) {
  442. rp->entry[count].type = 0x02;
  443. } else {
  444. continue;
  445. }
  446. rp->entry[count].bus = d->bus;
  447. rp->entry[count++].index = cpu_to_le16(d->id);
  448. BT_DBG("Added hci%u", d->id);
  449. }
  450. rp->num_controllers = cpu_to_le16(count);
  451. rp_len = sizeof(*rp) + (sizeof(rp->entry[0]) * count);
  452. read_unlock(&hci_dev_list_lock);
  453. /* If this command is called at least once, then all the
  454. * default index and unconfigured index events are disabled
  455. * and from now on only extended index events are used.
  456. */
  457. hci_sock_set_flag(sk, HCI_MGMT_EXT_INDEX_EVENTS);
  458. hci_sock_clear_flag(sk, HCI_MGMT_INDEX_EVENTS);
  459. hci_sock_clear_flag(sk, HCI_MGMT_UNCONF_INDEX_EVENTS);
  460. err = mgmt_cmd_complete(sk, MGMT_INDEX_NONE,
  461. MGMT_OP_READ_EXT_INDEX_LIST, 0, rp, rp_len);
  462. kfree(rp);
  463. return err;
  464. }
  465. static bool is_configured(struct hci_dev *hdev)
  466. {
  467. if (test_bit(HCI_QUIRK_EXTERNAL_CONFIG, &hdev->quirks) &&
  468. !hci_dev_test_flag(hdev, HCI_EXT_CONFIGURED))
  469. return false;
  470. if (test_bit(HCI_QUIRK_INVALID_BDADDR, &hdev->quirks) &&
  471. !bacmp(&hdev->public_addr, BDADDR_ANY))
  472. return false;
  473. return true;
  474. }
  475. static __le32 get_missing_options(struct hci_dev *hdev)
  476. {
  477. u32 options = 0;
  478. if (test_bit(HCI_QUIRK_EXTERNAL_CONFIG, &hdev->quirks) &&
  479. !hci_dev_test_flag(hdev, HCI_EXT_CONFIGURED))
  480. options |= MGMT_OPTION_EXTERNAL_CONFIG;
  481. if (test_bit(HCI_QUIRK_INVALID_BDADDR, &hdev->quirks) &&
  482. !bacmp(&hdev->public_addr, BDADDR_ANY))
  483. options |= MGMT_OPTION_PUBLIC_ADDRESS;
  484. return cpu_to_le32(options);
  485. }
  486. static int new_options(struct hci_dev *hdev, struct sock *skip)
  487. {
  488. __le32 options = get_missing_options(hdev);
  489. return mgmt_limited_event(MGMT_EV_NEW_CONFIG_OPTIONS, hdev, &options,
  490. sizeof(options), HCI_MGMT_OPTION_EVENTS, skip);
  491. }
  492. static int send_options_rsp(struct sock *sk, u16 opcode, struct hci_dev *hdev)
  493. {
  494. __le32 options = get_missing_options(hdev);
  495. return mgmt_cmd_complete(sk, hdev->id, opcode, 0, &options,
  496. sizeof(options));
  497. }
  498. static int read_config_info(struct sock *sk, struct hci_dev *hdev,
  499. void *data, u16 data_len)
  500. {
  501. struct mgmt_rp_read_config_info rp;
  502. u32 options = 0;
  503. BT_DBG("sock %p %s", sk, hdev->name);
  504. hci_dev_lock(hdev);
  505. memset(&rp, 0, sizeof(rp));
  506. rp.manufacturer = cpu_to_le16(hdev->manufacturer);
  507. if (test_bit(HCI_QUIRK_EXTERNAL_CONFIG, &hdev->quirks))
  508. options |= MGMT_OPTION_EXTERNAL_CONFIG;
  509. if (hdev->set_bdaddr)
  510. options |= MGMT_OPTION_PUBLIC_ADDRESS;
  511. rp.supported_options = cpu_to_le32(options);
  512. rp.missing_options = get_missing_options(hdev);
  513. hci_dev_unlock(hdev);
  514. return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_READ_CONFIG_INFO, 0,
  515. &rp, sizeof(rp));
  516. }
  517. static u32 get_supported_phys(struct hci_dev *hdev)
  518. {
  519. u32 supported_phys = 0;
  520. if (lmp_bredr_capable(hdev)) {
  521. supported_phys |= MGMT_PHY_BR_1M_1SLOT;
  522. if (hdev->features[0][0] & LMP_3SLOT)
  523. supported_phys |= MGMT_PHY_BR_1M_3SLOT;
  524. if (hdev->features[0][0] & LMP_5SLOT)
  525. supported_phys |= MGMT_PHY_BR_1M_5SLOT;
  526. if (lmp_edr_2m_capable(hdev)) {
  527. supported_phys |= MGMT_PHY_EDR_2M_1SLOT;
  528. if (lmp_edr_3slot_capable(hdev))
  529. supported_phys |= MGMT_PHY_EDR_2M_3SLOT;
  530. if (lmp_edr_5slot_capable(hdev))
  531. supported_phys |= MGMT_PHY_EDR_2M_5SLOT;
  532. if (lmp_edr_3m_capable(hdev)) {
  533. supported_phys |= MGMT_PHY_EDR_3M_1SLOT;
  534. if (lmp_edr_3slot_capable(hdev))
  535. supported_phys |= MGMT_PHY_EDR_3M_3SLOT;
  536. if (lmp_edr_5slot_capable(hdev))
  537. supported_phys |= MGMT_PHY_EDR_3M_5SLOT;
  538. }
  539. }
  540. }
  541. if (lmp_le_capable(hdev)) {
  542. supported_phys |= MGMT_PHY_LE_1M_TX;
  543. supported_phys |= MGMT_PHY_LE_1M_RX;
  544. if (hdev->le_features[1] & HCI_LE_PHY_2M) {
  545. supported_phys |= MGMT_PHY_LE_2M_TX;
  546. supported_phys |= MGMT_PHY_LE_2M_RX;
  547. }
  548. if (hdev->le_features[1] & HCI_LE_PHY_CODED) {
  549. supported_phys |= MGMT_PHY_LE_CODED_TX;
  550. supported_phys |= MGMT_PHY_LE_CODED_RX;
  551. }
  552. }
  553. return supported_phys;
  554. }
  555. static u32 get_selected_phys(struct hci_dev *hdev)
  556. {
  557. u32 selected_phys = 0;
  558. if (lmp_bredr_capable(hdev)) {
  559. selected_phys |= MGMT_PHY_BR_1M_1SLOT;
  560. if (hdev->pkt_type & (HCI_DM3 | HCI_DH3))
  561. selected_phys |= MGMT_PHY_BR_1M_3SLOT;
  562. if (hdev->pkt_type & (HCI_DM5 | HCI_DH5))
  563. selected_phys |= MGMT_PHY_BR_1M_5SLOT;
  564. if (lmp_edr_2m_capable(hdev)) {
  565. if (!(hdev->pkt_type & HCI_2DH1))
  566. selected_phys |= MGMT_PHY_EDR_2M_1SLOT;
  567. if (lmp_edr_3slot_capable(hdev) &&
  568. !(hdev->pkt_type & HCI_2DH3))
  569. selected_phys |= MGMT_PHY_EDR_2M_3SLOT;
  570. if (lmp_edr_5slot_capable(hdev) &&
  571. !(hdev->pkt_type & HCI_2DH5))
  572. selected_phys |= MGMT_PHY_EDR_2M_5SLOT;
  573. if (lmp_edr_3m_capable(hdev)) {
  574. if (!(hdev->pkt_type & HCI_3DH1))
  575. selected_phys |= MGMT_PHY_EDR_3M_1SLOT;
  576. if (lmp_edr_3slot_capable(hdev) &&
  577. !(hdev->pkt_type & HCI_3DH3))
  578. selected_phys |= MGMT_PHY_EDR_3M_3SLOT;
  579. if (lmp_edr_5slot_capable(hdev) &&
  580. !(hdev->pkt_type & HCI_3DH5))
  581. selected_phys |= MGMT_PHY_EDR_3M_5SLOT;
  582. }
  583. }
  584. }
  585. if (lmp_le_capable(hdev)) {
  586. if (hdev->le_tx_def_phys & HCI_LE_SET_PHY_1M)
  587. selected_phys |= MGMT_PHY_LE_1M_TX;
  588. if (hdev->le_rx_def_phys & HCI_LE_SET_PHY_1M)
  589. selected_phys |= MGMT_PHY_LE_1M_RX;
  590. if (hdev->le_tx_def_phys & HCI_LE_SET_PHY_2M)
  591. selected_phys |= MGMT_PHY_LE_2M_TX;
  592. if (hdev->le_rx_def_phys & HCI_LE_SET_PHY_2M)
  593. selected_phys |= MGMT_PHY_LE_2M_RX;
  594. if (hdev->le_tx_def_phys & HCI_LE_SET_PHY_CODED)
  595. selected_phys |= MGMT_PHY_LE_CODED_TX;
  596. if (hdev->le_rx_def_phys & HCI_LE_SET_PHY_CODED)
  597. selected_phys |= MGMT_PHY_LE_CODED_RX;
  598. }
  599. return selected_phys;
  600. }
  601. static u32 get_configurable_phys(struct hci_dev *hdev)
  602. {
  603. return (get_supported_phys(hdev) & ~MGMT_PHY_BR_1M_1SLOT &
  604. ~MGMT_PHY_LE_1M_TX & ~MGMT_PHY_LE_1M_RX);
  605. }
  606. static u32 get_supported_settings(struct hci_dev *hdev)
  607. {
  608. u32 settings = 0;
  609. settings |= MGMT_SETTING_POWERED;
  610. settings |= MGMT_SETTING_BONDABLE;
  611. settings |= MGMT_SETTING_DEBUG_KEYS;
  612. settings |= MGMT_SETTING_CONNECTABLE;
  613. settings |= MGMT_SETTING_DISCOVERABLE;
  614. if (lmp_bredr_capable(hdev)) {
  615. if (hdev->hci_ver >= BLUETOOTH_VER_1_2)
  616. settings |= MGMT_SETTING_FAST_CONNECTABLE;
  617. settings |= MGMT_SETTING_BREDR;
  618. settings |= MGMT_SETTING_LINK_SECURITY;
  619. if (lmp_ssp_capable(hdev)) {
  620. settings |= MGMT_SETTING_SSP;
  621. settings |= MGMT_SETTING_HS;
  622. }
  623. if (lmp_sc_capable(hdev))
  624. settings |= MGMT_SETTING_SECURE_CONN;
  625. }
  626. if (lmp_le_capable(hdev)) {
  627. settings |= MGMT_SETTING_LE;
  628. settings |= MGMT_SETTING_ADVERTISING;
  629. settings |= MGMT_SETTING_SECURE_CONN;
  630. settings |= MGMT_SETTING_PRIVACY;
  631. settings |= MGMT_SETTING_STATIC_ADDRESS;
  632. }
  633. if (test_bit(HCI_QUIRK_EXTERNAL_CONFIG, &hdev->quirks) ||
  634. hdev->set_bdaddr)
  635. settings |= MGMT_SETTING_CONFIGURATION;
  636. settings |= MGMT_SETTING_PHY_CONFIGURATION;
  637. return settings;
  638. }
  639. static u32 get_current_settings(struct hci_dev *hdev)
  640. {
  641. u32 settings = 0;
  642. if (hdev_is_powered(hdev))
  643. settings |= MGMT_SETTING_POWERED;
  644. if (hci_dev_test_flag(hdev, HCI_CONNECTABLE))
  645. settings |= MGMT_SETTING_CONNECTABLE;
  646. if (hci_dev_test_flag(hdev, HCI_FAST_CONNECTABLE))
  647. settings |= MGMT_SETTING_FAST_CONNECTABLE;
  648. if (hci_dev_test_flag(hdev, HCI_DISCOVERABLE))
  649. settings |= MGMT_SETTING_DISCOVERABLE;
  650. if (hci_dev_test_flag(hdev, HCI_BONDABLE))
  651. settings |= MGMT_SETTING_BONDABLE;
  652. if (hci_dev_test_flag(hdev, HCI_BREDR_ENABLED))
  653. settings |= MGMT_SETTING_BREDR;
  654. if (hci_dev_test_flag(hdev, HCI_LE_ENABLED))
  655. settings |= MGMT_SETTING_LE;
  656. if (hci_dev_test_flag(hdev, HCI_LINK_SECURITY))
  657. settings |= MGMT_SETTING_LINK_SECURITY;
  658. if (hci_dev_test_flag(hdev, HCI_SSP_ENABLED))
  659. settings |= MGMT_SETTING_SSP;
  660. if (hci_dev_test_flag(hdev, HCI_HS_ENABLED))
  661. settings |= MGMT_SETTING_HS;
  662. if (hci_dev_test_flag(hdev, HCI_ADVERTISING))
  663. settings |= MGMT_SETTING_ADVERTISING;
  664. if (hci_dev_test_flag(hdev, HCI_SC_ENABLED))
  665. settings |= MGMT_SETTING_SECURE_CONN;
  666. if (hci_dev_test_flag(hdev, HCI_KEEP_DEBUG_KEYS))
  667. settings |= MGMT_SETTING_DEBUG_KEYS;
  668. if (hci_dev_test_flag(hdev, HCI_PRIVACY))
  669. settings |= MGMT_SETTING_PRIVACY;
  670. /* The current setting for static address has two purposes. The
  671. * first is to indicate if the static address will be used and
  672. * the second is to indicate if it is actually set.
  673. *
  674. * This means if the static address is not configured, this flag
  675. * will never be set. If the address is configured, then if the
  676. * address is actually used decides if the flag is set or not.
  677. *
  678. * For single mode LE only controllers and dual-mode controllers
  679. * with BR/EDR disabled, the existence of the static address will
  680. * be evaluated.
  681. */
  682. if (hci_dev_test_flag(hdev, HCI_FORCE_STATIC_ADDR) ||
  683. !hci_dev_test_flag(hdev, HCI_BREDR_ENABLED) ||
  684. !bacmp(&hdev->bdaddr, BDADDR_ANY)) {
  685. if (bacmp(&hdev->static_addr, BDADDR_ANY))
  686. settings |= MGMT_SETTING_STATIC_ADDRESS;
  687. }
  688. return settings;
  689. }
  690. static struct mgmt_pending_cmd *pending_find(u16 opcode, struct hci_dev *hdev)
  691. {
  692. return mgmt_pending_find(HCI_CHANNEL_CONTROL, opcode, hdev);
  693. }
  694. static struct mgmt_pending_cmd *pending_find_data(u16 opcode,
  695. struct hci_dev *hdev,
  696. const void *data)
  697. {
  698. return mgmt_pending_find_data(HCI_CHANNEL_CONTROL, opcode, hdev, data);
  699. }
  700. u8 mgmt_get_adv_discov_flags(struct hci_dev *hdev)
  701. {
  702. struct mgmt_pending_cmd *cmd;
  703. /* If there's a pending mgmt command the flags will not yet have
  704. * their final values, so check for this first.
  705. */
  706. cmd = pending_find(MGMT_OP_SET_DISCOVERABLE, hdev);
  707. if (cmd) {
  708. struct mgmt_mode *cp = cmd->param;
  709. if (cp->val == 0x01)
  710. return LE_AD_GENERAL;
  711. else if (cp->val == 0x02)
  712. return LE_AD_LIMITED;
  713. } else {
  714. if (hci_dev_test_flag(hdev, HCI_LIMITED_DISCOVERABLE))
  715. return LE_AD_LIMITED;
  716. else if (hci_dev_test_flag(hdev, HCI_DISCOVERABLE))
  717. return LE_AD_GENERAL;
  718. }
  719. return 0;
  720. }
  721. bool mgmt_get_connectable(struct hci_dev *hdev)
  722. {
  723. struct mgmt_pending_cmd *cmd;
  724. /* If there's a pending mgmt command the flag will not yet have
  725. * it's final value, so check for this first.
  726. */
  727. cmd = pending_find(MGMT_OP_SET_CONNECTABLE, hdev);
  728. if (cmd) {
  729. struct mgmt_mode *cp = cmd->param;
  730. return cp->val;
  731. }
  732. return hci_dev_test_flag(hdev, HCI_CONNECTABLE);
  733. }
  734. static void service_cache_off(struct work_struct *work)
  735. {
  736. struct hci_dev *hdev = container_of(work, struct hci_dev,
  737. service_cache.work);
  738. struct hci_request req;
  739. if (!hci_dev_test_and_clear_flag(hdev, HCI_SERVICE_CACHE))
  740. return;
  741. hci_req_init(&req, hdev);
  742. hci_dev_lock(hdev);
  743. __hci_req_update_eir(&req);
  744. __hci_req_update_class(&req);
  745. hci_dev_unlock(hdev);
  746. hci_req_run(&req, NULL);
  747. }
  748. static void rpa_expired(struct work_struct *work)
  749. {
  750. struct hci_dev *hdev = container_of(work, struct hci_dev,
  751. rpa_expired.work);
  752. struct hci_request req;
  753. BT_DBG("");
  754. hci_dev_set_flag(hdev, HCI_RPA_EXPIRED);
  755. if (!hci_dev_test_flag(hdev, HCI_ADVERTISING))
  756. return;
  757. /* The generation of a new RPA and programming it into the
  758. * controller happens in the hci_req_enable_advertising()
  759. * function.
  760. */
  761. hci_req_init(&req, hdev);
  762. if (ext_adv_capable(hdev))
  763. __hci_req_start_ext_adv(&req, hdev->cur_adv_instance);
  764. else
  765. __hci_req_enable_advertising(&req);
  766. hci_req_run(&req, NULL);
  767. }
  768. static void mgmt_init_hdev(struct sock *sk, struct hci_dev *hdev)
  769. {
  770. if (hci_dev_test_and_set_flag(hdev, HCI_MGMT))
  771. return;
  772. INIT_DELAYED_WORK(&hdev->service_cache, service_cache_off);
  773. INIT_DELAYED_WORK(&hdev->rpa_expired, rpa_expired);
  774. /* Non-mgmt controlled devices get this bit set
  775. * implicitly so that pairing works for them, however
  776. * for mgmt we require user-space to explicitly enable
  777. * it
  778. */
  779. hci_dev_clear_flag(hdev, HCI_BONDABLE);
  780. }
  781. static int read_controller_info(struct sock *sk, struct hci_dev *hdev,
  782. void *data, u16 data_len)
  783. {
  784. struct mgmt_rp_read_info rp;
  785. BT_DBG("sock %p %s", sk, hdev->name);
  786. hci_dev_lock(hdev);
  787. memset(&rp, 0, sizeof(rp));
  788. bacpy(&rp.bdaddr, &hdev->bdaddr);
  789. rp.version = hdev->hci_ver;
  790. rp.manufacturer = cpu_to_le16(hdev->manufacturer);
  791. rp.supported_settings = cpu_to_le32(get_supported_settings(hdev));
  792. rp.current_settings = cpu_to_le32(get_current_settings(hdev));
  793. memcpy(rp.dev_class, hdev->dev_class, 3);
  794. memcpy(rp.name, hdev->dev_name, sizeof(hdev->dev_name));
  795. memcpy(rp.short_name, hdev->short_name, sizeof(hdev->short_name));
  796. hci_dev_unlock(hdev);
  797. return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_READ_INFO, 0, &rp,
  798. sizeof(rp));
  799. }
  800. static u16 append_eir_data_to_buf(struct hci_dev *hdev, u8 *eir)
  801. {
  802. u16 eir_len = 0;
  803. size_t name_len;
  804. if (hci_dev_test_flag(hdev, HCI_BREDR_ENABLED))
  805. eir_len = eir_append_data(eir, eir_len, EIR_CLASS_OF_DEV,
  806. hdev->dev_class, 3);
  807. if (hci_dev_test_flag(hdev, HCI_LE_ENABLED))
  808. eir_len = eir_append_le16(eir, eir_len, EIR_APPEARANCE,
  809. hdev->appearance);
  810. name_len = strlen(hdev->dev_name);
  811. eir_len = eir_append_data(eir, eir_len, EIR_NAME_COMPLETE,
  812. hdev->dev_name, name_len);
  813. name_len = strlen(hdev->short_name);
  814. eir_len = eir_append_data(eir, eir_len, EIR_NAME_SHORT,
  815. hdev->short_name, name_len);
  816. return eir_len;
  817. }
  818. static int read_ext_controller_info(struct sock *sk, struct hci_dev *hdev,
  819. void *data, u16 data_len)
  820. {
  821. char buf[512];
  822. struct mgmt_rp_read_ext_info *rp = (void *)buf;
  823. u16 eir_len;
  824. BT_DBG("sock %p %s", sk, hdev->name);
  825. memset(&buf, 0, sizeof(buf));
  826. hci_dev_lock(hdev);
  827. bacpy(&rp->bdaddr, &hdev->bdaddr);
  828. rp->version = hdev->hci_ver;
  829. rp->manufacturer = cpu_to_le16(hdev->manufacturer);
  830. rp->supported_settings = cpu_to_le32(get_supported_settings(hdev));
  831. rp->current_settings = cpu_to_le32(get_current_settings(hdev));
  832. eir_len = append_eir_data_to_buf(hdev, rp->eir);
  833. rp->eir_len = cpu_to_le16(eir_len);
  834. hci_dev_unlock(hdev);
  835. /* If this command is called at least once, then the events
  836. * for class of device and local name changes are disabled
  837. * and only the new extended controller information event
  838. * is used.
  839. */
  840. hci_sock_set_flag(sk, HCI_MGMT_EXT_INFO_EVENTS);
  841. hci_sock_clear_flag(sk, HCI_MGMT_DEV_CLASS_EVENTS);
  842. hci_sock_clear_flag(sk, HCI_MGMT_LOCAL_NAME_EVENTS);
  843. return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_READ_EXT_INFO, 0, rp,
  844. sizeof(*rp) + eir_len);
  845. }
  846. static int ext_info_changed(struct hci_dev *hdev, struct sock *skip)
  847. {
  848. char buf[512];
  849. struct mgmt_ev_ext_info_changed *ev = (void *)buf;
  850. u16 eir_len;
  851. memset(buf, 0, sizeof(buf));
  852. eir_len = append_eir_data_to_buf(hdev, ev->eir);
  853. ev->eir_len = cpu_to_le16(eir_len);
  854. return mgmt_limited_event(MGMT_EV_EXT_INFO_CHANGED, hdev, ev,
  855. sizeof(*ev) + eir_len,
  856. HCI_MGMT_EXT_INFO_EVENTS, skip);
  857. }
  858. static int send_settings_rsp(struct sock *sk, u16 opcode, struct hci_dev *hdev)
  859. {
  860. __le32 settings = cpu_to_le32(get_current_settings(hdev));
  861. return mgmt_cmd_complete(sk, hdev->id, opcode, 0, &settings,
  862. sizeof(settings));
  863. }
  864. static void clean_up_hci_complete(struct hci_dev *hdev, u8 status, u16 opcode)
  865. {
  866. BT_DBG("%s status 0x%02x", hdev->name, status);
  867. if (hci_conn_count(hdev) == 0) {
  868. cancel_delayed_work(&hdev->power_off);
  869. queue_work(hdev->req_workqueue, &hdev->power_off.work);
  870. }
  871. }
  872. void mgmt_advertising_added(struct sock *sk, struct hci_dev *hdev, u8 instance)
  873. {
  874. struct mgmt_ev_advertising_added ev;
  875. ev.instance = instance;
  876. mgmt_event(MGMT_EV_ADVERTISING_ADDED, hdev, &ev, sizeof(ev), sk);
  877. }
  878. void mgmt_advertising_removed(struct sock *sk, struct hci_dev *hdev,
  879. u8 instance)
  880. {
  881. struct mgmt_ev_advertising_removed ev;
  882. ev.instance = instance;
  883. mgmt_event(MGMT_EV_ADVERTISING_REMOVED, hdev, &ev, sizeof(ev), sk);
  884. }
  885. static void cancel_adv_timeout(struct hci_dev *hdev)
  886. {
  887. if (hdev->adv_instance_timeout) {
  888. hdev->adv_instance_timeout = 0;
  889. cancel_delayed_work(&hdev->adv_instance_expire);
  890. }
  891. }
  892. static int clean_up_hci_state(struct hci_dev *hdev)
  893. {
  894. struct hci_request req;
  895. struct hci_conn *conn;
  896. bool discov_stopped;
  897. int err;
  898. hci_req_init(&req, hdev);
  899. if (test_bit(HCI_ISCAN, &hdev->flags) ||
  900. test_bit(HCI_PSCAN, &hdev->flags)) {
  901. u8 scan = 0x00;
  902. hci_req_add(&req, HCI_OP_WRITE_SCAN_ENABLE, 1, &scan);
  903. }
  904. hci_req_clear_adv_instance(hdev, NULL, NULL, 0x00, false);
  905. if (hci_dev_test_flag(hdev, HCI_LE_ADV))
  906. __hci_req_disable_advertising(&req);
  907. discov_stopped = hci_req_stop_discovery(&req);
  908. list_for_each_entry(conn, &hdev->conn_hash.list, list) {
  909. /* 0x15 == Terminated due to Power Off */
  910. __hci_abort_conn(&req, conn, 0x15);
  911. }
  912. err = hci_req_run(&req, clean_up_hci_complete);
  913. if (!err && discov_stopped)
  914. hci_discovery_set_state(hdev, DISCOVERY_STOPPING);
  915. return err;
  916. }
  917. static int set_powered(struct sock *sk, struct hci_dev *hdev, void *data,
  918. u16 len)
  919. {
  920. struct mgmt_mode *cp = data;
  921. struct mgmt_pending_cmd *cmd;
  922. int err;
  923. BT_DBG("request for %s", hdev->name);
  924. if (cp->val != 0x00 && cp->val != 0x01)
  925. return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_POWERED,
  926. MGMT_STATUS_INVALID_PARAMS);
  927. hci_dev_lock(hdev);
  928. if (pending_find(MGMT_OP_SET_POWERED, hdev)) {
  929. err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_POWERED,
  930. MGMT_STATUS_BUSY);
  931. goto failed;
  932. }
  933. if (!!cp->val == hdev_is_powered(hdev)) {
  934. err = send_settings_rsp(sk, MGMT_OP_SET_POWERED, hdev);
  935. goto failed;
  936. }
  937. cmd = mgmt_pending_add(sk, MGMT_OP_SET_POWERED, hdev, data, len);
  938. if (!cmd) {
  939. err = -ENOMEM;
  940. goto failed;
  941. }
  942. if (cp->val) {
  943. queue_work(hdev->req_workqueue, &hdev->power_on);
  944. err = 0;
  945. } else {
  946. /* Disconnect connections, stop scans, etc */
  947. err = clean_up_hci_state(hdev);
  948. if (!err)
  949. queue_delayed_work(hdev->req_workqueue, &hdev->power_off,
  950. HCI_POWER_OFF_TIMEOUT);
  951. /* ENODATA means there were no HCI commands queued */
  952. if (err == -ENODATA) {
  953. cancel_delayed_work(&hdev->power_off);
  954. queue_work(hdev->req_workqueue, &hdev->power_off.work);
  955. err = 0;
  956. }
  957. }
  958. failed:
  959. hci_dev_unlock(hdev);
  960. return err;
  961. }
  962. static int new_settings(struct hci_dev *hdev, struct sock *skip)
  963. {
  964. __le32 ev = cpu_to_le32(get_current_settings(hdev));
  965. return mgmt_limited_event(MGMT_EV_NEW_SETTINGS, hdev, &ev,
  966. sizeof(ev), HCI_MGMT_SETTING_EVENTS, skip);
  967. }
  968. int mgmt_new_settings(struct hci_dev *hdev)
  969. {
  970. return new_settings(hdev, NULL);
  971. }
  972. struct cmd_lookup {
  973. struct sock *sk;
  974. struct hci_dev *hdev;
  975. u8 mgmt_status;
  976. };
  977. static void settings_rsp(struct mgmt_pending_cmd *cmd, void *data)
  978. {
  979. struct cmd_lookup *match = data;
  980. send_settings_rsp(cmd->sk, cmd->opcode, match->hdev);
  981. list_del(&cmd->list);
  982. if (match->sk == NULL) {
  983. match->sk = cmd->sk;
  984. sock_hold(match->sk);
  985. }
  986. mgmt_pending_free(cmd);
  987. }
  988. static void cmd_status_rsp(struct mgmt_pending_cmd *cmd, void *data)
  989. {
  990. u8 *status = data;
  991. mgmt_cmd_status(cmd->sk, cmd->index, cmd->opcode, *status);
  992. mgmt_pending_remove(cmd);
  993. }
  994. static void cmd_complete_rsp(struct mgmt_pending_cmd *cmd, void *data)
  995. {
  996. if (cmd->cmd_complete) {
  997. u8 *status = data;
  998. cmd->cmd_complete(cmd, *status);
  999. mgmt_pending_remove(cmd);
  1000. return;
  1001. }
  1002. cmd_status_rsp(cmd, data);
  1003. }
  1004. static int generic_cmd_complete(struct mgmt_pending_cmd *cmd, u8 status)
  1005. {
  1006. return mgmt_cmd_complete(cmd->sk, cmd->index, cmd->opcode, status,
  1007. cmd->param, cmd->param_len);
  1008. }
  1009. static int addr_cmd_complete(struct mgmt_pending_cmd *cmd, u8 status)
  1010. {
  1011. return mgmt_cmd_complete(cmd->sk, cmd->index, cmd->opcode, status,
  1012. cmd->param, sizeof(struct mgmt_addr_info));
  1013. }
  1014. static u8 mgmt_bredr_support(struct hci_dev *hdev)
  1015. {
  1016. if (!lmp_bredr_capable(hdev))
  1017. return MGMT_STATUS_NOT_SUPPORTED;
  1018. else if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED))
  1019. return MGMT_STATUS_REJECTED;
  1020. else
  1021. return MGMT_STATUS_SUCCESS;
  1022. }
  1023. static u8 mgmt_le_support(struct hci_dev *hdev)
  1024. {
  1025. if (!lmp_le_capable(hdev))
  1026. return MGMT_STATUS_NOT_SUPPORTED;
  1027. else if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED))
  1028. return MGMT_STATUS_REJECTED;
  1029. else
  1030. return MGMT_STATUS_SUCCESS;
  1031. }
  1032. void mgmt_set_discoverable_complete(struct hci_dev *hdev, u8 status)
  1033. {
  1034. struct mgmt_pending_cmd *cmd;
  1035. BT_DBG("status 0x%02x", status);
  1036. hci_dev_lock(hdev);
  1037. cmd = pending_find(MGMT_OP_SET_DISCOVERABLE, hdev);
  1038. if (!cmd)
  1039. goto unlock;
  1040. if (status) {
  1041. u8 mgmt_err = mgmt_status(status);
  1042. mgmt_cmd_status(cmd->sk, cmd->index, cmd->opcode, mgmt_err);
  1043. hci_dev_clear_flag(hdev, HCI_LIMITED_DISCOVERABLE);
  1044. goto remove_cmd;
  1045. }
  1046. if (hci_dev_test_flag(hdev, HCI_DISCOVERABLE) &&
  1047. hdev->discov_timeout > 0) {
  1048. int to = msecs_to_jiffies(hdev->discov_timeout * 1000);
  1049. queue_delayed_work(hdev->req_workqueue, &hdev->discov_off, to);
  1050. }
  1051. send_settings_rsp(cmd->sk, MGMT_OP_SET_DISCOVERABLE, hdev);
  1052. new_settings(hdev, cmd->sk);
  1053. remove_cmd:
  1054. mgmt_pending_remove(cmd);
  1055. unlock:
  1056. hci_dev_unlock(hdev);
  1057. }
  1058. static int set_discoverable(struct sock *sk, struct hci_dev *hdev, void *data,
  1059. u16 len)
  1060. {
  1061. struct mgmt_cp_set_discoverable *cp = data;
  1062. struct mgmt_pending_cmd *cmd;
  1063. u16 timeout;
  1064. int err;
  1065. BT_DBG("request for %s", hdev->name);
  1066. if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED) &&
  1067. !hci_dev_test_flag(hdev, HCI_BREDR_ENABLED))
  1068. return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE,
  1069. MGMT_STATUS_REJECTED);
  1070. if (cp->val != 0x00 && cp->val != 0x01 && cp->val != 0x02)
  1071. return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE,
  1072. MGMT_STATUS_INVALID_PARAMS);
  1073. timeout = __le16_to_cpu(cp->timeout);
  1074. /* Disabling discoverable requires that no timeout is set,
  1075. * and enabling limited discoverable requires a timeout.
  1076. */
  1077. if ((cp->val == 0x00 && timeout > 0) ||
  1078. (cp->val == 0x02 && timeout == 0))
  1079. return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE,
  1080. MGMT_STATUS_INVALID_PARAMS);
  1081. hci_dev_lock(hdev);
  1082. if (!hdev_is_powered(hdev) && timeout > 0) {
  1083. err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE,
  1084. MGMT_STATUS_NOT_POWERED);
  1085. goto failed;
  1086. }
  1087. if (pending_find(MGMT_OP_SET_DISCOVERABLE, hdev) ||
  1088. pending_find(MGMT_OP_SET_CONNECTABLE, hdev)) {
  1089. err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE,
  1090. MGMT_STATUS_BUSY);
  1091. goto failed;
  1092. }
  1093. if (!hci_dev_test_flag(hdev, HCI_CONNECTABLE)) {
  1094. err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE,
  1095. MGMT_STATUS_REJECTED);
  1096. goto failed;
  1097. }
  1098. if (!hdev_is_powered(hdev)) {
  1099. bool changed = false;
  1100. /* Setting limited discoverable when powered off is
  1101. * not a valid operation since it requires a timeout
  1102. * and so no need to check HCI_LIMITED_DISCOVERABLE.
  1103. */
  1104. if (!!cp->val != hci_dev_test_flag(hdev, HCI_DISCOVERABLE)) {
  1105. hci_dev_change_flag(hdev, HCI_DISCOVERABLE);
  1106. changed = true;
  1107. }
  1108. err = send_settings_rsp(sk, MGMT_OP_SET_DISCOVERABLE, hdev);
  1109. if (err < 0)
  1110. goto failed;
  1111. if (changed)
  1112. err = new_settings(hdev, sk);
  1113. goto failed;
  1114. }
  1115. /* If the current mode is the same, then just update the timeout
  1116. * value with the new value. And if only the timeout gets updated,
  1117. * then no need for any HCI transactions.
  1118. */
  1119. if (!!cp->val == hci_dev_test_flag(hdev, HCI_DISCOVERABLE) &&
  1120. (cp->val == 0x02) == hci_dev_test_flag(hdev,
  1121. HCI_LIMITED_DISCOVERABLE)) {
  1122. cancel_delayed_work(&hdev->discov_off);
  1123. hdev->discov_timeout = timeout;
  1124. if (cp->val && hdev->discov_timeout > 0) {
  1125. int to = msecs_to_jiffies(hdev->discov_timeout * 1000);
  1126. queue_delayed_work(hdev->req_workqueue,
  1127. &hdev->discov_off, to);
  1128. }
  1129. err = send_settings_rsp(sk, MGMT_OP_SET_DISCOVERABLE, hdev);
  1130. goto failed;
  1131. }
  1132. cmd = mgmt_pending_add(sk, MGMT_OP_SET_DISCOVERABLE, hdev, data, len);
  1133. if (!cmd) {
  1134. err = -ENOMEM;
  1135. goto failed;
  1136. }
  1137. /* Cancel any potential discoverable timeout that might be
  1138. * still active and store new timeout value. The arming of
  1139. * the timeout happens in the complete handler.
  1140. */
  1141. cancel_delayed_work(&hdev->discov_off);
  1142. hdev->discov_timeout = timeout;
  1143. if (cp->val)
  1144. hci_dev_set_flag(hdev, HCI_DISCOVERABLE);
  1145. else
  1146. hci_dev_clear_flag(hdev, HCI_DISCOVERABLE);
  1147. /* Limited discoverable mode */
  1148. if (cp->val == 0x02)
  1149. hci_dev_set_flag(hdev, HCI_LIMITED_DISCOVERABLE);
  1150. else
  1151. hci_dev_clear_flag(hdev, HCI_LIMITED_DISCOVERABLE);
  1152. queue_work(hdev->req_workqueue, &hdev->discoverable_update);
  1153. err = 0;
  1154. failed:
  1155. hci_dev_unlock(hdev);
  1156. return err;
  1157. }
  1158. void mgmt_set_connectable_complete(struct hci_dev *hdev, u8 status)
  1159. {
  1160. struct mgmt_pending_cmd *cmd;
  1161. BT_DBG("status 0x%02x", status);
  1162. hci_dev_lock(hdev);
  1163. cmd = pending_find(MGMT_OP_SET_CONNECTABLE, hdev);
  1164. if (!cmd)
  1165. goto unlock;
  1166. if (status) {
  1167. u8 mgmt_err = mgmt_status(status);
  1168. mgmt_cmd_status(cmd->sk, cmd->index, cmd->opcode, mgmt_err);
  1169. goto remove_cmd;
  1170. }
  1171. send_settings_rsp(cmd->sk, MGMT_OP_SET_CONNECTABLE, hdev);
  1172. new_settings(hdev, cmd->sk);
  1173. remove_cmd:
  1174. mgmt_pending_remove(cmd);
  1175. unlock:
  1176. hci_dev_unlock(hdev);
  1177. }
  1178. static int set_connectable_update_settings(struct hci_dev *hdev,
  1179. struct sock *sk, u8 val)
  1180. {
  1181. bool changed = false;
  1182. int err;
  1183. if (!!val != hci_dev_test_flag(hdev, HCI_CONNECTABLE))
  1184. changed = true;
  1185. if (val) {
  1186. hci_dev_set_flag(hdev, HCI_CONNECTABLE);
  1187. } else {
  1188. hci_dev_clear_flag(hdev, HCI_CONNECTABLE);
  1189. hci_dev_clear_flag(hdev, HCI_DISCOVERABLE);
  1190. }
  1191. err = send_settings_rsp(sk, MGMT_OP_SET_CONNECTABLE, hdev);
  1192. if (err < 0)
  1193. return err;
  1194. if (changed) {
  1195. hci_req_update_scan(hdev);
  1196. hci_update_background_scan(hdev);
  1197. return new_settings(hdev, sk);
  1198. }
  1199. return 0;
  1200. }
  1201. static int set_connectable(struct sock *sk, struct hci_dev *hdev, void *data,
  1202. u16 len)
  1203. {
  1204. struct mgmt_mode *cp = data;
  1205. struct mgmt_pending_cmd *cmd;
  1206. int err;
  1207. BT_DBG("request for %s", hdev->name);
  1208. if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED) &&
  1209. !hci_dev_test_flag(hdev, HCI_BREDR_ENABLED))
  1210. return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_CONNECTABLE,
  1211. MGMT_STATUS_REJECTED);
  1212. if (cp->val != 0x00 && cp->val != 0x01)
  1213. return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_CONNECTABLE,
  1214. MGMT_STATUS_INVALID_PARAMS);
  1215. hci_dev_lock(hdev);
  1216. if (!hdev_is_powered(hdev)) {
  1217. err = set_connectable_update_settings(hdev, sk, cp->val);
  1218. goto failed;
  1219. }
  1220. if (pending_find(MGMT_OP_SET_DISCOVERABLE, hdev) ||
  1221. pending_find(MGMT_OP_SET_CONNECTABLE, hdev)) {
  1222. err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_CONNECTABLE,
  1223. MGMT_STATUS_BUSY);
  1224. goto failed;
  1225. }
  1226. cmd = mgmt_pending_add(sk, MGMT_OP_SET_CONNECTABLE, hdev, data, len);
  1227. if (!cmd) {
  1228. err = -ENOMEM;
  1229. goto failed;
  1230. }
  1231. if (cp->val) {
  1232. hci_dev_set_flag(hdev, HCI_CONNECTABLE);
  1233. } else {
  1234. if (hdev->discov_timeout > 0)
  1235. cancel_delayed_work(&hdev->discov_off);
  1236. hci_dev_clear_flag(hdev, HCI_LIMITED_DISCOVERABLE);
  1237. hci_dev_clear_flag(hdev, HCI_DISCOVERABLE);
  1238. hci_dev_clear_flag(hdev, HCI_CONNECTABLE);
  1239. }
  1240. queue_work(hdev->req_workqueue, &hdev->connectable_update);
  1241. err = 0;
  1242. failed:
  1243. hci_dev_unlock(hdev);
  1244. return err;
  1245. }
  1246. static int set_bondable(struct sock *sk, struct hci_dev *hdev, void *data,
  1247. u16 len)
  1248. {
  1249. struct mgmt_mode *cp = data;
  1250. bool changed;
  1251. int err;
  1252. BT_DBG("request for %s", hdev->name);
  1253. if (cp->val != 0x00 && cp->val != 0x01)
  1254. return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_BONDABLE,
  1255. MGMT_STATUS_INVALID_PARAMS);
  1256. hci_dev_lock(hdev);
  1257. if (cp->val)
  1258. changed = !hci_dev_test_and_set_flag(hdev, HCI_BONDABLE);
  1259. else
  1260. changed = hci_dev_test_and_clear_flag(hdev, HCI_BONDABLE);
  1261. err = send_settings_rsp(sk, MGMT_OP_SET_BONDABLE, hdev);
  1262. if (err < 0)
  1263. goto unlock;
  1264. if (changed) {
  1265. /* In limited privacy mode the change of bondable mode
  1266. * may affect the local advertising address.
  1267. */
  1268. if (hdev_is_powered(hdev) &&
  1269. hci_dev_test_flag(hdev, HCI_ADVERTISING) &&
  1270. hci_dev_test_flag(hdev, HCI_DISCOVERABLE) &&
  1271. hci_dev_test_flag(hdev, HCI_LIMITED_PRIVACY))
  1272. queue_work(hdev->req_workqueue,
  1273. &hdev->discoverable_update);
  1274. err = new_settings(hdev, sk);
  1275. }
  1276. unlock:
  1277. hci_dev_unlock(hdev);
  1278. return err;
  1279. }
  1280. static int set_link_security(struct sock *sk, struct hci_dev *hdev, void *data,
  1281. u16 len)
  1282. {
  1283. struct mgmt_mode *cp = data;
  1284. struct mgmt_pending_cmd *cmd;
  1285. u8 val, status;
  1286. int err;
  1287. BT_DBG("request for %s", hdev->name);
  1288. status = mgmt_bredr_support(hdev);
  1289. if (status)
  1290. return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_LINK_SECURITY,
  1291. status);
  1292. if (cp->val != 0x00 && cp->val != 0x01)
  1293. return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_LINK_SECURITY,
  1294. MGMT_STATUS_INVALID_PARAMS);
  1295. hci_dev_lock(hdev);
  1296. if (!hdev_is_powered(hdev)) {
  1297. bool changed = false;
  1298. if (!!cp->val != hci_dev_test_flag(hdev, HCI_LINK_SECURITY)) {
  1299. hci_dev_change_flag(hdev, HCI_LINK_SECURITY);
  1300. changed = true;
  1301. }
  1302. err = send_settings_rsp(sk, MGMT_OP_SET_LINK_SECURITY, hdev);
  1303. if (err < 0)
  1304. goto failed;
  1305. if (changed)
  1306. err = new_settings(hdev, sk);
  1307. goto failed;
  1308. }
  1309. if (pending_find(MGMT_OP_SET_LINK_SECURITY, hdev)) {
  1310. err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_LINK_SECURITY,
  1311. MGMT_STATUS_BUSY);
  1312. goto failed;
  1313. }
  1314. val = !!cp->val;
  1315. if (test_bit(HCI_AUTH, &hdev->flags) == val) {
  1316. err = send_settings_rsp(sk, MGMT_OP_SET_LINK_SECURITY, hdev);
  1317. goto failed;
  1318. }
  1319. cmd = mgmt_pending_add(sk, MGMT_OP_SET_LINK_SECURITY, hdev, data, len);
  1320. if (!cmd) {
  1321. err = -ENOMEM;
  1322. goto failed;
  1323. }
  1324. err = hci_send_cmd(hdev, HCI_OP_WRITE_AUTH_ENABLE, sizeof(val), &val);
  1325. if (err < 0) {
  1326. mgmt_pending_remove(cmd);
  1327. goto failed;
  1328. }
  1329. failed:
  1330. hci_dev_unlock(hdev);
  1331. return err;
  1332. }
  1333. static int set_ssp(struct sock *sk, struct hci_dev *hdev, void *data, u16 len)
  1334. {
  1335. struct mgmt_mode *cp = data;
  1336. struct mgmt_pending_cmd *cmd;
  1337. u8 status;
  1338. int err;
  1339. BT_DBG("request for %s", hdev->name);
  1340. status = mgmt_bredr_support(hdev);
  1341. if (status)
  1342. return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SSP, status);
  1343. if (!lmp_ssp_capable(hdev))
  1344. return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SSP,
  1345. MGMT_STATUS_NOT_SUPPORTED);
  1346. if (cp->val != 0x00 && cp->val != 0x01)
  1347. return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SSP,
  1348. MGMT_STATUS_INVALID_PARAMS);
  1349. hci_dev_lock(hdev);
  1350. if (!hdev_is_powered(hdev)) {
  1351. bool changed;
  1352. if (cp->val) {
  1353. changed = !hci_dev_test_and_set_flag(hdev,
  1354. HCI_SSP_ENABLED);
  1355. } else {
  1356. changed = hci_dev_test_and_clear_flag(hdev,
  1357. HCI_SSP_ENABLED);
  1358. if (!changed)
  1359. changed = hci_dev_test_and_clear_flag(hdev,
  1360. HCI_HS_ENABLED);
  1361. else
  1362. hci_dev_clear_flag(hdev, HCI_HS_ENABLED);
  1363. }
  1364. err = send_settings_rsp(sk, MGMT_OP_SET_SSP, hdev);
  1365. if (err < 0)
  1366. goto failed;
  1367. if (changed)
  1368. err = new_settings(hdev, sk);
  1369. goto failed;
  1370. }
  1371. if (pending_find(MGMT_OP_SET_SSP, hdev)) {
  1372. err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SSP,
  1373. MGMT_STATUS_BUSY);
  1374. goto failed;
  1375. }
  1376. if (!!cp->val == hci_dev_test_flag(hdev, HCI_SSP_ENABLED)) {
  1377. err = send_settings_rsp(sk, MGMT_OP_SET_SSP, hdev);
  1378. goto failed;
  1379. }
  1380. cmd = mgmt_pending_add(sk, MGMT_OP_SET_SSP, hdev, data, len);
  1381. if (!cmd) {
  1382. err = -ENOMEM;
  1383. goto failed;
  1384. }
  1385. if (!cp->val && hci_dev_test_flag(hdev, HCI_USE_DEBUG_KEYS))
  1386. hci_send_cmd(hdev, HCI_OP_WRITE_SSP_DEBUG_MODE,
  1387. sizeof(cp->val), &cp->val);
  1388. err = hci_send_cmd(hdev, HCI_OP_WRITE_SSP_MODE, 1, &cp->val);
  1389. if (err < 0) {
  1390. mgmt_pending_remove(cmd);
  1391. goto failed;
  1392. }
  1393. failed:
  1394. hci_dev_unlock(hdev);
  1395. return err;
  1396. }
  1397. static int set_hs(struct sock *sk, struct hci_dev *hdev, void *data, u16 len)
  1398. {
  1399. struct mgmt_mode *cp = data;
  1400. bool changed;
  1401. u8 status;
  1402. int err;
  1403. BT_DBG("request for %s", hdev->name);
  1404. status = mgmt_bredr_support(hdev);
  1405. if (status)
  1406. return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_HS, status);
  1407. if (!lmp_ssp_capable(hdev))
  1408. return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_HS,
  1409. MGMT_STATUS_NOT_SUPPORTED);
  1410. if (!hci_dev_test_flag(hdev, HCI_SSP_ENABLED))
  1411. return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_HS,
  1412. MGMT_STATUS_REJECTED);
  1413. if (cp->val != 0x00 && cp->val != 0x01)
  1414. return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_HS,
  1415. MGMT_STATUS_INVALID_PARAMS);
  1416. hci_dev_lock(hdev);
  1417. if (pending_find(MGMT_OP_SET_SSP, hdev)) {
  1418. err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_HS,
  1419. MGMT_STATUS_BUSY);
  1420. goto unlock;
  1421. }
  1422. if (cp->val) {
  1423. changed = !hci_dev_test_and_set_flag(hdev, HCI_HS_ENABLED);
  1424. } else {
  1425. if (hdev_is_powered(hdev)) {
  1426. err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_HS,
  1427. MGMT_STATUS_REJECTED);
  1428. goto unlock;
  1429. }
  1430. changed = hci_dev_test_and_clear_flag(hdev, HCI_HS_ENABLED);
  1431. }
  1432. err = send_settings_rsp(sk, MGMT_OP_SET_HS, hdev);
  1433. if (err < 0)
  1434. goto unlock;
  1435. if (changed)
  1436. err = new_settings(hdev, sk);
  1437. unlock:
  1438. hci_dev_unlock(hdev);
  1439. return err;
  1440. }
  1441. static void le_enable_complete(struct hci_dev *hdev, u8 status, u16 opcode)
  1442. {
  1443. struct cmd_lookup match = { NULL, hdev };
  1444. hci_dev_lock(hdev);
  1445. if (status) {
  1446. u8 mgmt_err = mgmt_status(status);
  1447. mgmt_pending_foreach(MGMT_OP_SET_LE, hdev, cmd_status_rsp,
  1448. &mgmt_err);
  1449. goto unlock;
  1450. }
  1451. mgmt_pending_foreach(MGMT_OP_SET_LE, hdev, settings_rsp, &match);
  1452. new_settings(hdev, match.sk);
  1453. if (match.sk)
  1454. sock_put(match.sk);
  1455. /* Make sure the controller has a good default for
  1456. * advertising data. Restrict the update to when LE
  1457. * has actually been enabled. During power on, the
  1458. * update in powered_update_hci will take care of it.
  1459. */
  1460. if (hci_dev_test_flag(hdev, HCI_LE_ENABLED)) {
  1461. struct hci_request req;
  1462. hci_req_init(&req, hdev);
  1463. if (ext_adv_capable(hdev)) {
  1464. int err;
  1465. err = __hci_req_setup_ext_adv_instance(&req, 0x00);
  1466. if (!err)
  1467. __hci_req_update_scan_rsp_data(&req, 0x00);
  1468. } else {
  1469. __hci_req_update_adv_data(&req, 0x00);
  1470. __hci_req_update_scan_rsp_data(&req, 0x00);
  1471. }
  1472. hci_req_run(&req, NULL);
  1473. hci_update_background_scan(hdev);
  1474. }
  1475. unlock:
  1476. hci_dev_unlock(hdev);
  1477. }
  1478. static int set_le(struct sock *sk, struct hci_dev *hdev, void *data, u16 len)
  1479. {
  1480. struct mgmt_mode *cp = data;
  1481. struct hci_cp_write_le_host_supported hci_cp;
  1482. struct mgmt_pending_cmd *cmd;
  1483. struct hci_request req;
  1484. int err;
  1485. u8 val, enabled;
  1486. BT_DBG("request for %s", hdev->name);
  1487. if (!lmp_le_capable(hdev))
  1488. return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_LE,
  1489. MGMT_STATUS_NOT_SUPPORTED);
  1490. if (cp->val != 0x00 && cp->val != 0x01)
  1491. return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_LE,
  1492. MGMT_STATUS_INVALID_PARAMS);
  1493. /* Bluetooth single mode LE only controllers or dual-mode
  1494. * controllers configured as LE only devices, do not allow
  1495. * switching LE off. These have either LE enabled explicitly
  1496. * or BR/EDR has been previously switched off.
  1497. *
  1498. * When trying to enable an already enabled LE, then gracefully
  1499. * send a positive response. Trying to disable it however will
  1500. * result into rejection.
  1501. */
  1502. if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED)) {
  1503. if (cp->val == 0x01)
  1504. return send_settings_rsp(sk, MGMT_OP_SET_LE, hdev);
  1505. return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_LE,
  1506. MGMT_STATUS_REJECTED);
  1507. }
  1508. hci_dev_lock(hdev);
  1509. val = !!cp->val;
  1510. enabled = lmp_host_le_capable(hdev);
  1511. if (!val)
  1512. hci_req_clear_adv_instance(hdev, NULL, NULL, 0x00, true);
  1513. if (!hdev_is_powered(hdev) || val == enabled) {
  1514. bool changed = false;
  1515. if (val != hci_dev_test_flag(hdev, HCI_LE_ENABLED)) {
  1516. hci_dev_change_flag(hdev, HCI_LE_ENABLED);
  1517. changed = true;
  1518. }
  1519. if (!val && hci_dev_test_flag(hdev, HCI_ADVERTISING)) {
  1520. hci_dev_clear_flag(hdev, HCI_ADVERTISING);
  1521. changed = true;
  1522. }
  1523. err = send_settings_rsp(sk, MGMT_OP_SET_LE, hdev);
  1524. if (err < 0)
  1525. goto unlock;
  1526. if (changed)
  1527. err = new_settings(hdev, sk);
  1528. goto unlock;
  1529. }
  1530. if (pending_find(MGMT_OP_SET_LE, hdev) ||
  1531. pending_find(MGMT_OP_SET_ADVERTISING, hdev)) {
  1532. err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_LE,
  1533. MGMT_STATUS_BUSY);
  1534. goto unlock;
  1535. }
  1536. cmd = mgmt_pending_add(sk, MGMT_OP_SET_LE, hdev, data, len);
  1537. if (!cmd) {
  1538. err = -ENOMEM;
  1539. goto unlock;
  1540. }
  1541. hci_req_init(&req, hdev);
  1542. memset(&hci_cp, 0, sizeof(hci_cp));
  1543. if (val) {
  1544. hci_cp.le = val;
  1545. hci_cp.simul = 0x00;
  1546. } else {
  1547. if (hci_dev_test_flag(hdev, HCI_LE_ADV))
  1548. __hci_req_disable_advertising(&req);
  1549. if (ext_adv_capable(hdev))
  1550. __hci_req_clear_ext_adv_sets(&req);
  1551. }
  1552. hci_req_add(&req, HCI_OP_WRITE_LE_HOST_SUPPORTED, sizeof(hci_cp),
  1553. &hci_cp);
  1554. err = hci_req_run(&req, le_enable_complete);
  1555. if (err < 0)
  1556. mgmt_pending_remove(cmd);
  1557. unlock:
  1558. hci_dev_unlock(hdev);
  1559. return err;
  1560. }
  1561. /* This is a helper function to test for pending mgmt commands that can
  1562. * cause CoD or EIR HCI commands. We can only allow one such pending
  1563. * mgmt command at a time since otherwise we cannot easily track what
  1564. * the current values are, will be, and based on that calculate if a new
  1565. * HCI command needs to be sent and if yes with what value.
  1566. */
  1567. static bool pending_eir_or_class(struct hci_dev *hdev)
  1568. {
  1569. struct mgmt_pending_cmd *cmd;
  1570. list_for_each_entry(cmd, &hdev->mgmt_pending, list) {
  1571. switch (cmd->opcode) {
  1572. case MGMT_OP_ADD_UUID:
  1573. case MGMT_OP_REMOVE_UUID:
  1574. case MGMT_OP_SET_DEV_CLASS:
  1575. case MGMT_OP_SET_POWERED:
  1576. return true;
  1577. }
  1578. }
  1579. return false;
  1580. }
  1581. static const u8 bluetooth_base_uuid[] = {
  1582. 0xfb, 0x34, 0x9b, 0x5f, 0x80, 0x00, 0x00, 0x80,
  1583. 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  1584. };
  1585. static u8 get_uuid_size(const u8 *uuid)
  1586. {
  1587. u32 val;
  1588. if (memcmp(uuid, bluetooth_base_uuid, 12))
  1589. return 128;
  1590. val = get_unaligned_le32(&uuid[12]);
  1591. if (val > 0xffff)
  1592. return 32;
  1593. return 16;
  1594. }
  1595. static void mgmt_class_complete(struct hci_dev *hdev, u16 mgmt_op, u8 status)
  1596. {
  1597. struct mgmt_pending_cmd *cmd;
  1598. hci_dev_lock(hdev);
  1599. cmd = pending_find(mgmt_op, hdev);
  1600. if (!cmd)
  1601. goto unlock;
  1602. mgmt_cmd_complete(cmd->sk, cmd->index, cmd->opcode,
  1603. mgmt_status(status), hdev->dev_class, 3);
  1604. mgmt_pending_remove(cmd);
  1605. unlock:
  1606. hci_dev_unlock(hdev);
  1607. }
  1608. static void add_uuid_complete(struct hci_dev *hdev, u8 status, u16 opcode)
  1609. {
  1610. BT_DBG("status 0x%02x", status);
  1611. mgmt_class_complete(hdev, MGMT_OP_ADD_UUID, status);
  1612. }
  1613. static int add_uuid(struct sock *sk, struct hci_dev *hdev, void *data, u16 len)
  1614. {
  1615. struct mgmt_cp_add_uuid *cp = data;
  1616. struct mgmt_pending_cmd *cmd;
  1617. struct hci_request req;
  1618. struct bt_uuid *uuid;
  1619. int err;
  1620. BT_DBG("request for %s", hdev->name);
  1621. hci_dev_lock(hdev);
  1622. if (pending_eir_or_class(hdev)) {
  1623. err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_UUID,
  1624. MGMT_STATUS_BUSY);
  1625. goto failed;
  1626. }
  1627. uuid = kmalloc(sizeof(*uuid), GFP_KERNEL);
  1628. if (!uuid) {
  1629. err = -ENOMEM;
  1630. goto failed;
  1631. }
  1632. memcpy(uuid->uuid, cp->uuid, 16);
  1633. uuid->svc_hint = cp->svc_hint;
  1634. uuid->size = get_uuid_size(cp->uuid);
  1635. list_add_tail(&uuid->list, &hdev->uuids);
  1636. hci_req_init(&req, hdev);
  1637. __hci_req_update_class(&req);
  1638. __hci_req_update_eir(&req);
  1639. err = hci_req_run(&req, add_uuid_complete);
  1640. if (err < 0) {
  1641. if (err != -ENODATA)
  1642. goto failed;
  1643. err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_ADD_UUID, 0,
  1644. hdev->dev_class, 3);
  1645. goto failed;
  1646. }
  1647. cmd = mgmt_pending_add(sk, MGMT_OP_ADD_UUID, hdev, data, len);
  1648. if (!cmd) {
  1649. err = -ENOMEM;
  1650. goto failed;
  1651. }
  1652. err = 0;
  1653. failed:
  1654. hci_dev_unlock(hdev);
  1655. return err;
  1656. }
  1657. static bool enable_service_cache(struct hci_dev *hdev)
  1658. {
  1659. if (!hdev_is_powered(hdev))
  1660. return false;
  1661. if (!hci_dev_test_and_set_flag(hdev, HCI_SERVICE_CACHE)) {
  1662. queue_delayed_work(hdev->workqueue, &hdev->service_cache,
  1663. CACHE_TIMEOUT);
  1664. return true;
  1665. }
  1666. return false;
  1667. }
  1668. static void remove_uuid_complete(struct hci_dev *hdev, u8 status, u16 opcode)
  1669. {
  1670. BT_DBG("status 0x%02x", status);
  1671. mgmt_class_complete(hdev, MGMT_OP_REMOVE_UUID, status);
  1672. }
  1673. static int remove_uuid(struct sock *sk, struct hci_dev *hdev, void *data,
  1674. u16 len)
  1675. {
  1676. struct mgmt_cp_remove_uuid *cp = data;
  1677. struct mgmt_pending_cmd *cmd;
  1678. struct bt_uuid *match, *tmp;
  1679. u8 bt_uuid_any[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
  1680. struct hci_request req;
  1681. int err, found;
  1682. BT_DBG("request for %s", hdev->name);
  1683. hci_dev_lock(hdev);
  1684. if (pending_eir_or_class(hdev)) {
  1685. err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_REMOVE_UUID,
  1686. MGMT_STATUS_BUSY);
  1687. goto unlock;
  1688. }
  1689. if (memcmp(cp->uuid, bt_uuid_any, 16) == 0) {
  1690. hci_uuids_clear(hdev);
  1691. if (enable_service_cache(hdev)) {
  1692. err = mgmt_cmd_complete(sk, hdev->id,
  1693. MGMT_OP_REMOVE_UUID,
  1694. 0, hdev->dev_class, 3);
  1695. goto unlock;
  1696. }
  1697. goto update_class;
  1698. }
  1699. found = 0;
  1700. list_for_each_entry_safe(match, tmp, &hdev->uuids, list) {
  1701. if (memcmp(match->uuid, cp->uuid, 16) != 0)
  1702. continue;
  1703. list_del(&match->list);
  1704. kfree(match);
  1705. found++;
  1706. }
  1707. if (found == 0) {
  1708. err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_REMOVE_UUID,
  1709. MGMT_STATUS_INVALID_PARAMS);
  1710. goto unlock;
  1711. }
  1712. update_class:
  1713. hci_req_init(&req, hdev);
  1714. __hci_req_update_class(&req);
  1715. __hci_req_update_eir(&req);
  1716. err = hci_req_run(&req, remove_uuid_complete);
  1717. if (err < 0) {
  1718. if (err != -ENODATA)
  1719. goto unlock;
  1720. err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_REMOVE_UUID, 0,
  1721. hdev->dev_class, 3);
  1722. goto unlock;
  1723. }
  1724. cmd = mgmt_pending_add(sk, MGMT_OP_REMOVE_UUID, hdev, data, len);
  1725. if (!cmd) {
  1726. err = -ENOMEM;
  1727. goto unlock;
  1728. }
  1729. err = 0;
  1730. unlock:
  1731. hci_dev_unlock(hdev);
  1732. return err;
  1733. }
  1734. static void set_class_complete(struct hci_dev *hdev, u8 status, u16 opcode)
  1735. {
  1736. BT_DBG("status 0x%02x", status);
  1737. mgmt_class_complete(hdev, MGMT_OP_SET_DEV_CLASS, status);
  1738. }
  1739. static int set_dev_class(struct sock *sk, struct hci_dev *hdev, void *data,
  1740. u16 len)
  1741. {
  1742. struct mgmt_cp_set_dev_class *cp = data;
  1743. struct mgmt_pending_cmd *cmd;
  1744. struct hci_request req;
  1745. int err;
  1746. BT_DBG("request for %s", hdev->name);
  1747. if (!lmp_bredr_capable(hdev))
  1748. return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DEV_CLASS,
  1749. MGMT_STATUS_NOT_SUPPORTED);
  1750. hci_dev_lock(hdev);
  1751. if (pending_eir_or_class(hdev)) {
  1752. err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DEV_CLASS,
  1753. MGMT_STATUS_BUSY);
  1754. goto unlock;
  1755. }
  1756. if ((cp->minor & 0x03) != 0 || (cp->major & 0xe0) != 0) {
  1757. err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DEV_CLASS,
  1758. MGMT_STATUS_INVALID_PARAMS);
  1759. goto unlock;
  1760. }
  1761. hdev->major_class = cp->major;
  1762. hdev->minor_class = cp->minor;
  1763. if (!hdev_is_powered(hdev)) {
  1764. err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_SET_DEV_CLASS, 0,
  1765. hdev->dev_class, 3);
  1766. goto unlock;
  1767. }
  1768. hci_req_init(&req, hdev);
  1769. if (hci_dev_test_and_clear_flag(hdev, HCI_SERVICE_CACHE)) {
  1770. hci_dev_unlock(hdev);
  1771. cancel_delayed_work_sync(&hdev->service_cache);
  1772. hci_dev_lock(hdev);
  1773. __hci_req_update_eir(&req);
  1774. }
  1775. __hci_req_update_class(&req);
  1776. err = hci_req_run(&req, set_class_complete);
  1777. if (err < 0) {
  1778. if (err != -ENODATA)
  1779. goto unlock;
  1780. err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_SET_DEV_CLASS, 0,
  1781. hdev->dev_class, 3);
  1782. goto unlock;
  1783. }
  1784. cmd = mgmt_pending_add(sk, MGMT_OP_SET_DEV_CLASS, hdev, data, len);
  1785. if (!cmd) {
  1786. err = -ENOMEM;
  1787. goto unlock;
  1788. }
  1789. err = 0;
  1790. unlock:
  1791. hci_dev_unlock(hdev);
  1792. return err;
  1793. }
  1794. static int load_link_keys(struct sock *sk, struct hci_dev *hdev, void *data,
  1795. u16 len)
  1796. {
  1797. struct mgmt_cp_load_link_keys *cp = data;
  1798. const u16 max_key_count = ((U16_MAX - sizeof(*cp)) /
  1799. sizeof(struct mgmt_link_key_info));
  1800. u16 key_count, expected_len;
  1801. bool changed;
  1802. int i;
  1803. BT_DBG("request for %s", hdev->name);
  1804. if (!lmp_bredr_capable(hdev))
  1805. return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_LINK_KEYS,
  1806. MGMT_STATUS_NOT_SUPPORTED);
  1807. key_count = __le16_to_cpu(cp->key_count);
  1808. if (key_count > max_key_count) {
  1809. bt_dev_err(hdev, "load_link_keys: too big key_count value %u",
  1810. key_count);
  1811. return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_LINK_KEYS,
  1812. MGMT_STATUS_INVALID_PARAMS);
  1813. }
  1814. expected_len = sizeof(*cp) + key_count *
  1815. sizeof(struct mgmt_link_key_info);
  1816. if (expected_len != len) {
  1817. bt_dev_err(hdev, "load_link_keys: expected %u bytes, got %u bytes",
  1818. expected_len, len);
  1819. return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_LINK_KEYS,
  1820. MGMT_STATUS_INVALID_PARAMS);
  1821. }
  1822. if (cp->debug_keys != 0x00 && cp->debug_keys != 0x01)
  1823. return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_LINK_KEYS,
  1824. MGMT_STATUS_INVALID_PARAMS);
  1825. BT_DBG("%s debug_keys %u key_count %u", hdev->name, cp->debug_keys,
  1826. key_count);
  1827. for (i = 0; i < key_count; i++) {
  1828. struct mgmt_link_key_info *key = &cp->keys[i];
  1829. if (key->addr.type != BDADDR_BREDR || key->type > 0x08)
  1830. return mgmt_cmd_status(sk, hdev->id,
  1831. MGMT_OP_LOAD_LINK_KEYS,
  1832. MGMT_STATUS_INVALID_PARAMS);
  1833. }
  1834. hci_dev_lock(hdev);
  1835. hci_link_keys_clear(hdev);
  1836. if (cp->debug_keys)
  1837. changed = !hci_dev_test_and_set_flag(hdev, HCI_KEEP_DEBUG_KEYS);
  1838. else
  1839. changed = hci_dev_test_and_clear_flag(hdev,
  1840. HCI_KEEP_DEBUG_KEYS);
  1841. if (changed)
  1842. new_settings(hdev, NULL);
  1843. for (i = 0; i < key_count; i++) {
  1844. struct mgmt_link_key_info *key = &cp->keys[i];
  1845. /* Always ignore debug keys and require a new pairing if
  1846. * the user wants to use them.
  1847. */
  1848. if (key->type == HCI_LK_DEBUG_COMBINATION)
  1849. continue;
  1850. hci_add_link_key(hdev, NULL, &key->addr.bdaddr, key->val,
  1851. key->type, key->pin_len, NULL);
  1852. }
  1853. mgmt_cmd_complete(sk, hdev->id, MGMT_OP_LOAD_LINK_KEYS, 0, NULL, 0);
  1854. hci_dev_unlock(hdev);
  1855. return 0;
  1856. }
  1857. static int device_unpaired(struct hci_dev *hdev, bdaddr_t *bdaddr,
  1858. u8 addr_type, struct sock *skip_sk)
  1859. {
  1860. struct mgmt_ev_device_unpaired ev;
  1861. bacpy(&ev.addr.bdaddr, bdaddr);
  1862. ev.addr.type = addr_type;
  1863. return mgmt_event(MGMT_EV_DEVICE_UNPAIRED, hdev, &ev, sizeof(ev),
  1864. skip_sk);
  1865. }
  1866. static int unpair_device(struct sock *sk, struct hci_dev *hdev, void *data,
  1867. u16 len)
  1868. {
  1869. struct mgmt_cp_unpair_device *cp = data;
  1870. struct mgmt_rp_unpair_device rp;
  1871. struct hci_conn_params *params;
  1872. struct mgmt_pending_cmd *cmd;
  1873. struct hci_conn *conn;
  1874. u8 addr_type;
  1875. int err;
  1876. memset(&rp, 0, sizeof(rp));
  1877. bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr);
  1878. rp.addr.type = cp->addr.type;
  1879. if (!bdaddr_type_is_valid(cp->addr.type))
  1880. return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_UNPAIR_DEVICE,
  1881. MGMT_STATUS_INVALID_PARAMS,
  1882. &rp, sizeof(rp));
  1883. if (cp->disconnect != 0x00 && cp->disconnect != 0x01)
  1884. return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_UNPAIR_DEVICE,
  1885. MGMT_STATUS_INVALID_PARAMS,
  1886. &rp, sizeof(rp));
  1887. hci_dev_lock(hdev);
  1888. if (!hdev_is_powered(hdev)) {
  1889. err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_UNPAIR_DEVICE,
  1890. MGMT_STATUS_NOT_POWERED, &rp,
  1891. sizeof(rp));
  1892. goto unlock;
  1893. }
  1894. if (cp->addr.type == BDADDR_BREDR) {
  1895. /* If disconnection is requested, then look up the
  1896. * connection. If the remote device is connected, it
  1897. * will be later used to terminate the link.
  1898. *
  1899. * Setting it to NULL explicitly will cause no
  1900. * termination of the link.
  1901. */
  1902. if (cp->disconnect)
  1903. conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK,
  1904. &cp->addr.bdaddr);
  1905. else
  1906. conn = NULL;
  1907. err = hci_remove_link_key(hdev, &cp->addr.bdaddr);
  1908. if (err < 0) {
  1909. err = mgmt_cmd_complete(sk, hdev->id,
  1910. MGMT_OP_UNPAIR_DEVICE,
  1911. MGMT_STATUS_NOT_PAIRED, &rp,
  1912. sizeof(rp));
  1913. goto unlock;
  1914. }
  1915. goto done;
  1916. }
  1917. /* LE address type */
  1918. addr_type = le_addr_type(cp->addr.type);
  1919. /* Abort any ongoing SMP pairing. Removes ltk and irk if they exist. */
  1920. err = smp_cancel_and_remove_pairing(hdev, &cp->addr.bdaddr, addr_type);
  1921. if (err < 0) {
  1922. err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_UNPAIR_DEVICE,
  1923. MGMT_STATUS_NOT_PAIRED, &rp,
  1924. sizeof(rp));
  1925. goto unlock;
  1926. }
  1927. conn = hci_conn_hash_lookup_le(hdev, &cp->addr.bdaddr, addr_type);
  1928. if (!conn) {
  1929. hci_conn_params_del(hdev, &cp->addr.bdaddr, addr_type);
  1930. goto done;
  1931. }
  1932. /* Defer clearing up the connection parameters until closing to
  1933. * give a chance of keeping them if a repairing happens.
  1934. */
  1935. set_bit(HCI_CONN_PARAM_REMOVAL_PEND, &conn->flags);
  1936. /* Disable auto-connection parameters if present */
  1937. params = hci_conn_params_lookup(hdev, &cp->addr.bdaddr, addr_type);
  1938. if (params) {
  1939. if (params->explicit_connect)
  1940. params->auto_connect = HCI_AUTO_CONN_EXPLICIT;
  1941. else
  1942. params->auto_connect = HCI_AUTO_CONN_DISABLED;
  1943. }
  1944. /* If disconnection is not requested, then clear the connection
  1945. * variable so that the link is not terminated.
  1946. */
  1947. if (!cp->disconnect)
  1948. conn = NULL;
  1949. done:
  1950. /* If the connection variable is set, then termination of the
  1951. * link is requested.
  1952. */
  1953. if (!conn) {
  1954. err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_UNPAIR_DEVICE, 0,
  1955. &rp, sizeof(rp));
  1956. device_unpaired(hdev, &cp->addr.bdaddr, cp->addr.type, sk);
  1957. goto unlock;
  1958. }
  1959. cmd = mgmt_pending_add(sk, MGMT_OP_UNPAIR_DEVICE, hdev, cp,
  1960. sizeof(*cp));
  1961. if (!cmd) {
  1962. err = -ENOMEM;
  1963. goto unlock;
  1964. }
  1965. cmd->cmd_complete = addr_cmd_complete;
  1966. err = hci_abort_conn(conn, HCI_ERROR_REMOTE_USER_TERM);
  1967. if (err < 0)
  1968. mgmt_pending_remove(cmd);
  1969. unlock:
  1970. hci_dev_unlock(hdev);
  1971. return err;
  1972. }
  1973. static int disconnect(struct sock *sk, struct hci_dev *hdev, void *data,
  1974. u16 len)
  1975. {
  1976. struct mgmt_cp_disconnect *cp = data;
  1977. struct mgmt_rp_disconnect rp;
  1978. struct mgmt_pending_cmd *cmd;
  1979. struct hci_conn *conn;
  1980. int err;
  1981. BT_DBG("");
  1982. memset(&rp, 0, sizeof(rp));
  1983. bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr);
  1984. rp.addr.type = cp->addr.type;
  1985. if (!bdaddr_type_is_valid(cp->addr.type))
  1986. return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_DISCONNECT,
  1987. MGMT_STATUS_INVALID_PARAMS,
  1988. &rp, sizeof(rp));
  1989. hci_dev_lock(hdev);
  1990. if (!test_bit(HCI_UP, &hdev->flags)) {
  1991. err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_DISCONNECT,
  1992. MGMT_STATUS_NOT_POWERED, &rp,
  1993. sizeof(rp));
  1994. goto failed;
  1995. }
  1996. if (pending_find(MGMT_OP_DISCONNECT, hdev)) {
  1997. err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_DISCONNECT,
  1998. MGMT_STATUS_BUSY, &rp, sizeof(rp));
  1999. goto failed;
  2000. }
  2001. if (cp->addr.type == BDADDR_BREDR)
  2002. conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK,
  2003. &cp->addr.bdaddr);
  2004. else
  2005. conn = hci_conn_hash_lookup_le(hdev, &cp->addr.bdaddr,
  2006. le_addr_type(cp->addr.type));
  2007. if (!conn || conn->state == BT_OPEN || conn->state == BT_CLOSED) {
  2008. err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_DISCONNECT,
  2009. MGMT_STATUS_NOT_CONNECTED, &rp,
  2010. sizeof(rp));
  2011. goto failed;
  2012. }
  2013. cmd = mgmt_pending_add(sk, MGMT_OP_DISCONNECT, hdev, data, len);
  2014. if (!cmd) {
  2015. err = -ENOMEM;
  2016. goto failed;
  2017. }
  2018. cmd->cmd_complete = generic_cmd_complete;
  2019. err = hci_disconnect(conn, HCI_ERROR_REMOTE_USER_TERM);
  2020. if (err < 0)
  2021. mgmt_pending_remove(cmd);
  2022. failed:
  2023. hci_dev_unlock(hdev);
  2024. return err;
  2025. }
  2026. static u8 link_to_bdaddr(u8 link_type, u8 addr_type)
  2027. {
  2028. switch (link_type) {
  2029. case LE_LINK:
  2030. switch (addr_type) {
  2031. case ADDR_LE_DEV_PUBLIC:
  2032. return BDADDR_LE_PUBLIC;
  2033. default:
  2034. /* Fallback to LE Random address type */
  2035. return BDADDR_LE_RANDOM;
  2036. }
  2037. default:
  2038. /* Fallback to BR/EDR type */
  2039. return BDADDR_BREDR;
  2040. }
  2041. }
  2042. static int get_connections(struct sock *sk, struct hci_dev *hdev, void *data,
  2043. u16 data_len)
  2044. {
  2045. struct mgmt_rp_get_connections *rp;
  2046. struct hci_conn *c;
  2047. size_t rp_len;
  2048. int err;
  2049. u16 i;
  2050. BT_DBG("");
  2051. hci_dev_lock(hdev);
  2052. if (!hdev_is_powered(hdev)) {
  2053. err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_GET_CONNECTIONS,
  2054. MGMT_STATUS_NOT_POWERED);
  2055. goto unlock;
  2056. }
  2057. i = 0;
  2058. list_for_each_entry(c, &hdev->conn_hash.list, list) {
  2059. if (test_bit(HCI_CONN_MGMT_CONNECTED, &c->flags))
  2060. i++;
  2061. }
  2062. rp_len = sizeof(*rp) + (i * sizeof(struct mgmt_addr_info));
  2063. rp = kmalloc(rp_len, GFP_KERNEL);
  2064. if (!rp) {
  2065. err = -ENOMEM;
  2066. goto unlock;
  2067. }
  2068. i = 0;
  2069. list_for_each_entry(c, &hdev->conn_hash.list, list) {
  2070. if (!test_bit(HCI_CONN_MGMT_CONNECTED, &c->flags))
  2071. continue;
  2072. bacpy(&rp->addr[i].bdaddr, &c->dst);
  2073. rp->addr[i].type = link_to_bdaddr(c->type, c->dst_type);
  2074. if (c->type == SCO_LINK || c->type == ESCO_LINK)
  2075. continue;
  2076. i++;
  2077. }
  2078. rp->conn_count = cpu_to_le16(i);
  2079. /* Recalculate length in case of filtered SCO connections, etc */
  2080. rp_len = sizeof(*rp) + (i * sizeof(struct mgmt_addr_info));
  2081. err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_GET_CONNECTIONS, 0, rp,
  2082. rp_len);
  2083. kfree(rp);
  2084. unlock:
  2085. hci_dev_unlock(hdev);
  2086. return err;
  2087. }
  2088. static int send_pin_code_neg_reply(struct sock *sk, struct hci_dev *hdev,
  2089. struct mgmt_cp_pin_code_neg_reply *cp)
  2090. {
  2091. struct mgmt_pending_cmd *cmd;
  2092. int err;
  2093. cmd = mgmt_pending_add(sk, MGMT_OP_PIN_CODE_NEG_REPLY, hdev, cp,
  2094. sizeof(*cp));
  2095. if (!cmd)
  2096. return -ENOMEM;
  2097. cmd->cmd_complete = addr_cmd_complete;
  2098. err = hci_send_cmd(hdev, HCI_OP_PIN_CODE_NEG_REPLY,
  2099. sizeof(cp->addr.bdaddr), &cp->addr.bdaddr);
  2100. if (err < 0)
  2101. mgmt_pending_remove(cmd);
  2102. return err;
  2103. }
  2104. static int pin_code_reply(struct sock *sk, struct hci_dev *hdev, void *data,
  2105. u16 len)
  2106. {
  2107. struct hci_conn *conn;
  2108. struct mgmt_cp_pin_code_reply *cp = data;
  2109. struct hci_cp_pin_code_reply reply;
  2110. struct mgmt_pending_cmd *cmd;
  2111. int err;
  2112. BT_DBG("");
  2113. hci_dev_lock(hdev);
  2114. if (!hdev_is_powered(hdev)) {
  2115. err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_PIN_CODE_REPLY,
  2116. MGMT_STATUS_NOT_POWERED);
  2117. goto failed;
  2118. }
  2119. conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &cp->addr.bdaddr);
  2120. if (!conn) {
  2121. err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_PIN_CODE_REPLY,
  2122. MGMT_STATUS_NOT_CONNECTED);
  2123. goto failed;
  2124. }
  2125. if (conn->pending_sec_level == BT_SECURITY_HIGH && cp->pin_len != 16) {
  2126. struct mgmt_cp_pin_code_neg_reply ncp;
  2127. memcpy(&ncp.addr, &cp->addr, sizeof(ncp.addr));
  2128. bt_dev_err(hdev, "PIN code is not 16 bytes long");
  2129. err = send_pin_code_neg_reply(sk, hdev, &ncp);
  2130. if (err >= 0)
  2131. err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_PIN_CODE_REPLY,
  2132. MGMT_STATUS_INVALID_PARAMS);
  2133. goto failed;
  2134. }
  2135. cmd = mgmt_pending_add(sk, MGMT_OP_PIN_CODE_REPLY, hdev, data, len);
  2136. if (!cmd) {
  2137. err = -ENOMEM;
  2138. goto failed;
  2139. }
  2140. cmd->cmd_complete = addr_cmd_complete;
  2141. bacpy(&reply.bdaddr, &cp->addr.bdaddr);
  2142. reply.pin_len = cp->pin_len;
  2143. memcpy(reply.pin_code, cp->pin_code, sizeof(reply.pin_code));
  2144. err = hci_send_cmd(hdev, HCI_OP_PIN_CODE_REPLY, sizeof(reply), &reply);
  2145. if (err < 0)
  2146. mgmt_pending_remove(cmd);
  2147. failed:
  2148. hci_dev_unlock(hdev);
  2149. return err;
  2150. }
  2151. static int set_io_capability(struct sock *sk, struct hci_dev *hdev, void *data,
  2152. u16 len)
  2153. {
  2154. struct mgmt_cp_set_io_capability *cp = data;
  2155. BT_DBG("");
  2156. if (cp->io_capability > SMP_IO_KEYBOARD_DISPLAY)
  2157. return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_IO_CAPABILITY,
  2158. MGMT_STATUS_INVALID_PARAMS);
  2159. hci_dev_lock(hdev);
  2160. hdev->io_capability = cp->io_capability;
  2161. BT_DBG("%s IO capability set to 0x%02x", hdev->name,
  2162. hdev->io_capability);
  2163. hci_dev_unlock(hdev);
  2164. return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_SET_IO_CAPABILITY, 0,
  2165. NULL, 0);
  2166. }
  2167. static struct mgmt_pending_cmd *find_pairing(struct hci_conn *conn)
  2168. {
  2169. struct hci_dev *hdev = conn->hdev;
  2170. struct mgmt_pending_cmd *cmd;
  2171. list_for_each_entry(cmd, &hdev->mgmt_pending, list) {
  2172. if (cmd->opcode != MGMT_OP_PAIR_DEVICE)
  2173. continue;
  2174. if (cmd->user_data != conn)
  2175. continue;
  2176. return cmd;
  2177. }
  2178. return NULL;
  2179. }
  2180. static int pairing_complete(struct mgmt_pending_cmd *cmd, u8 status)
  2181. {
  2182. struct mgmt_rp_pair_device rp;
  2183. struct hci_conn *conn = cmd->user_data;
  2184. int err;
  2185. bacpy(&rp.addr.bdaddr, &conn->dst);
  2186. rp.addr.type = link_to_bdaddr(conn->type, conn->dst_type);
  2187. err = mgmt_cmd_complete(cmd->sk, cmd->index, MGMT_OP_PAIR_DEVICE,
  2188. status, &rp, sizeof(rp));
  2189. /* So we don't get further callbacks for this connection */
  2190. conn->connect_cfm_cb = NULL;
  2191. conn->security_cfm_cb = NULL;
  2192. conn->disconn_cfm_cb = NULL;
  2193. hci_conn_drop(conn);
  2194. /* The device is paired so there is no need to remove
  2195. * its connection parameters anymore.
  2196. */
  2197. clear_bit(HCI_CONN_PARAM_REMOVAL_PEND, &conn->flags);
  2198. hci_conn_put(conn);
  2199. return err;
  2200. }
  2201. void mgmt_smp_complete(struct hci_conn *conn, bool complete)
  2202. {
  2203. u8 status = complete ? MGMT_STATUS_SUCCESS : MGMT_STATUS_FAILED;
  2204. struct mgmt_pending_cmd *cmd;
  2205. cmd = find_pairing(conn);
  2206. if (cmd) {
  2207. cmd->cmd_complete(cmd, status);
  2208. mgmt_pending_remove(cmd);
  2209. }
  2210. }
  2211. static void pairing_complete_cb(struct hci_conn *conn, u8 status)
  2212. {
  2213. struct mgmt_pending_cmd *cmd;
  2214. BT_DBG("status %u", status);
  2215. cmd = find_pairing(conn);
  2216. if (!cmd) {
  2217. BT_DBG("Unable to find a pending command");
  2218. return;
  2219. }
  2220. cmd->cmd_complete(cmd, mgmt_status(status));
  2221. mgmt_pending_remove(cmd);
  2222. }
  2223. static void le_pairing_complete_cb(struct hci_conn *conn, u8 status)
  2224. {
  2225. struct mgmt_pending_cmd *cmd;
  2226. BT_DBG("status %u", status);
  2227. if (!status)
  2228. return;
  2229. cmd = find_pairing(conn);
  2230. if (!cmd) {
  2231. BT_DBG("Unable to find a pending command");
  2232. return;
  2233. }
  2234. cmd->cmd_complete(cmd, mgmt_status(status));
  2235. mgmt_pending_remove(cmd);
  2236. }
  2237. static int pair_device(struct sock *sk, struct hci_dev *hdev, void *data,
  2238. u16 len)
  2239. {
  2240. struct mgmt_cp_pair_device *cp = data;
  2241. struct mgmt_rp_pair_device rp;
  2242. struct mgmt_pending_cmd *cmd;
  2243. u8 sec_level, auth_type;
  2244. struct hci_conn *conn;
  2245. int err;
  2246. BT_DBG("");
  2247. memset(&rp, 0, sizeof(rp));
  2248. bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr);
  2249. rp.addr.type = cp->addr.type;
  2250. if (!bdaddr_type_is_valid(cp->addr.type))
  2251. return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_PAIR_DEVICE,
  2252. MGMT_STATUS_INVALID_PARAMS,
  2253. &rp, sizeof(rp));
  2254. if (cp->io_cap > SMP_IO_KEYBOARD_DISPLAY)
  2255. return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_PAIR_DEVICE,
  2256. MGMT_STATUS_INVALID_PARAMS,
  2257. &rp, sizeof(rp));
  2258. hci_dev_lock(hdev);
  2259. if (!hdev_is_powered(hdev)) {
  2260. err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_PAIR_DEVICE,
  2261. MGMT_STATUS_NOT_POWERED, &rp,
  2262. sizeof(rp));
  2263. goto unlock;
  2264. }
  2265. if (hci_bdaddr_is_paired(hdev, &cp->addr.bdaddr, cp->addr.type)) {
  2266. err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_PAIR_DEVICE,
  2267. MGMT_STATUS_ALREADY_PAIRED, &rp,
  2268. sizeof(rp));
  2269. goto unlock;
  2270. }
  2271. sec_level = BT_SECURITY_MEDIUM;
  2272. auth_type = HCI_AT_DEDICATED_BONDING;
  2273. if (cp->addr.type == BDADDR_BREDR) {
  2274. conn = hci_connect_acl(hdev, &cp->addr.bdaddr, sec_level,
  2275. auth_type);
  2276. } else {
  2277. u8 addr_type = le_addr_type(cp->addr.type);
  2278. struct hci_conn_params *p;
  2279. /* When pairing a new device, it is expected to remember
  2280. * this device for future connections. Adding the connection
  2281. * parameter information ahead of time allows tracking
  2282. * of the slave preferred values and will speed up any
  2283. * further connection establishment.
  2284. *
  2285. * If connection parameters already exist, then they
  2286. * will be kept and this function does nothing.
  2287. */
  2288. p = hci_conn_params_add(hdev, &cp->addr.bdaddr, addr_type);
  2289. if (p->auto_connect == HCI_AUTO_CONN_EXPLICIT)
  2290. p->auto_connect = HCI_AUTO_CONN_DISABLED;
  2291. conn = hci_connect_le_scan(hdev, &cp->addr.bdaddr,
  2292. addr_type, sec_level,
  2293. HCI_LE_CONN_TIMEOUT);
  2294. }
  2295. if (IS_ERR(conn)) {
  2296. int status;
  2297. if (PTR_ERR(conn) == -EBUSY)
  2298. status = MGMT_STATUS_BUSY;
  2299. else if (PTR_ERR(conn) == -EOPNOTSUPP)
  2300. status = MGMT_STATUS_NOT_SUPPORTED;
  2301. else if (PTR_ERR(conn) == -ECONNREFUSED)
  2302. status = MGMT_STATUS_REJECTED;
  2303. else
  2304. status = MGMT_STATUS_CONNECT_FAILED;
  2305. err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_PAIR_DEVICE,
  2306. status, &rp, sizeof(rp));
  2307. goto unlock;
  2308. }
  2309. if (conn->connect_cfm_cb) {
  2310. hci_conn_drop(conn);
  2311. err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_PAIR_DEVICE,
  2312. MGMT_STATUS_BUSY, &rp, sizeof(rp));
  2313. goto unlock;
  2314. }
  2315. cmd = mgmt_pending_add(sk, MGMT_OP_PAIR_DEVICE, hdev, data, len);
  2316. if (!cmd) {
  2317. err = -ENOMEM;
  2318. hci_conn_drop(conn);
  2319. goto unlock;
  2320. }
  2321. cmd->cmd_complete = pairing_complete;
  2322. /* For LE, just connecting isn't a proof that the pairing finished */
  2323. if (cp->addr.type == BDADDR_BREDR) {
  2324. conn->connect_cfm_cb = pairing_complete_cb;
  2325. conn->security_cfm_cb = pairing_complete_cb;
  2326. conn->disconn_cfm_cb = pairing_complete_cb;
  2327. } else {
  2328. conn->connect_cfm_cb = le_pairing_complete_cb;
  2329. conn->security_cfm_cb = le_pairing_complete_cb;
  2330. conn->disconn_cfm_cb = le_pairing_complete_cb;
  2331. }
  2332. conn->io_capability = cp->io_cap;
  2333. cmd->user_data = hci_conn_get(conn);
  2334. if ((conn->state == BT_CONNECTED || conn->state == BT_CONFIG) &&
  2335. hci_conn_security(conn, sec_level, auth_type, true)) {
  2336. cmd->cmd_complete(cmd, 0);
  2337. mgmt_pending_remove(cmd);
  2338. }
  2339. err = 0;
  2340. unlock:
  2341. hci_dev_unlock(hdev);
  2342. return err;
  2343. }
  2344. static int cancel_pair_device(struct sock *sk, struct hci_dev *hdev, void *data,
  2345. u16 len)
  2346. {
  2347. struct mgmt_addr_info *addr = data;
  2348. struct mgmt_pending_cmd *cmd;
  2349. struct hci_conn *conn;
  2350. int err;
  2351. BT_DBG("");
  2352. hci_dev_lock(hdev);
  2353. if (!hdev_is_powered(hdev)) {
  2354. err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_CANCEL_PAIR_DEVICE,
  2355. MGMT_STATUS_NOT_POWERED);
  2356. goto unlock;
  2357. }
  2358. cmd = pending_find(MGMT_OP_PAIR_DEVICE, hdev);
  2359. if (!cmd) {
  2360. err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_CANCEL_PAIR_DEVICE,
  2361. MGMT_STATUS_INVALID_PARAMS);
  2362. goto unlock;
  2363. }
  2364. conn = cmd->user_data;
  2365. if (bacmp(&addr->bdaddr, &conn->dst) != 0) {
  2366. err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_CANCEL_PAIR_DEVICE,
  2367. MGMT_STATUS_INVALID_PARAMS);
  2368. goto unlock;
  2369. }
  2370. cmd->cmd_complete(cmd, MGMT_STATUS_CANCELLED);
  2371. mgmt_pending_remove(cmd);
  2372. err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_CANCEL_PAIR_DEVICE, 0,
  2373. addr, sizeof(*addr));
  2374. unlock:
  2375. hci_dev_unlock(hdev);
  2376. return err;
  2377. }
  2378. static int user_pairing_resp(struct sock *sk, struct hci_dev *hdev,
  2379. struct mgmt_addr_info *addr, u16 mgmt_op,
  2380. u16 hci_op, __le32 passkey)
  2381. {
  2382. struct mgmt_pending_cmd *cmd;
  2383. struct hci_conn *conn;
  2384. int err;
  2385. hci_dev_lock(hdev);
  2386. if (!hdev_is_powered(hdev)) {
  2387. err = mgmt_cmd_complete(sk, hdev->id, mgmt_op,
  2388. MGMT_STATUS_NOT_POWERED, addr,
  2389. sizeof(*addr));
  2390. goto done;
  2391. }
  2392. if (addr->type == BDADDR_BREDR)
  2393. conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &addr->bdaddr);
  2394. else
  2395. conn = hci_conn_hash_lookup_le(hdev, &addr->bdaddr,
  2396. le_addr_type(addr->type));
  2397. if (!conn) {
  2398. err = mgmt_cmd_complete(sk, hdev->id, mgmt_op,
  2399. MGMT_STATUS_NOT_CONNECTED, addr,
  2400. sizeof(*addr));
  2401. goto done;
  2402. }
  2403. if (addr->type == BDADDR_LE_PUBLIC || addr->type == BDADDR_LE_RANDOM) {
  2404. err = smp_user_confirm_reply(conn, mgmt_op, passkey);
  2405. if (!err)
  2406. err = mgmt_cmd_complete(sk, hdev->id, mgmt_op,
  2407. MGMT_STATUS_SUCCESS, addr,
  2408. sizeof(*addr));
  2409. else
  2410. err = mgmt_cmd_complete(sk, hdev->id, mgmt_op,
  2411. MGMT_STATUS_FAILED, addr,
  2412. sizeof(*addr));
  2413. goto done;
  2414. }
  2415. cmd = mgmt_pending_add(sk, mgmt_op, hdev, addr, sizeof(*addr));
  2416. if (!cmd) {
  2417. err = -ENOMEM;
  2418. goto done;
  2419. }
  2420. cmd->cmd_complete = addr_cmd_complete;
  2421. /* Continue with pairing via HCI */
  2422. if (hci_op == HCI_OP_USER_PASSKEY_REPLY) {
  2423. struct hci_cp_user_passkey_reply cp;
  2424. bacpy(&cp.bdaddr, &addr->bdaddr);
  2425. cp.passkey = passkey;
  2426. err = hci_send_cmd(hdev, hci_op, sizeof(cp), &cp);
  2427. } else
  2428. err = hci_send_cmd(hdev, hci_op, sizeof(addr->bdaddr),
  2429. &addr->bdaddr);
  2430. if (err < 0)
  2431. mgmt_pending_remove(cmd);
  2432. done:
  2433. hci_dev_unlock(hdev);
  2434. return err;
  2435. }
  2436. static int pin_code_neg_reply(struct sock *sk, struct hci_dev *hdev,
  2437. void *data, u16 len)
  2438. {
  2439. struct mgmt_cp_pin_code_neg_reply *cp = data;
  2440. BT_DBG("");
  2441. return user_pairing_resp(sk, hdev, &cp->addr,
  2442. MGMT_OP_PIN_CODE_NEG_REPLY,
  2443. HCI_OP_PIN_CODE_NEG_REPLY, 0);
  2444. }
  2445. static int user_confirm_reply(struct sock *sk, struct hci_dev *hdev, void *data,
  2446. u16 len)
  2447. {
  2448. struct mgmt_cp_user_confirm_reply *cp = data;
  2449. BT_DBG("");
  2450. if (len != sizeof(*cp))
  2451. return mgmt_cmd_status(sk, hdev->id, MGMT_OP_USER_CONFIRM_REPLY,
  2452. MGMT_STATUS_INVALID_PARAMS);
  2453. return user_pairing_resp(sk, hdev, &cp->addr,
  2454. MGMT_OP_USER_CONFIRM_REPLY,
  2455. HCI_OP_USER_CONFIRM_REPLY, 0);
  2456. }
  2457. static int user_confirm_neg_reply(struct sock *sk, struct hci_dev *hdev,
  2458. void *data, u16 len)
  2459. {
  2460. struct mgmt_cp_user_confirm_neg_reply *cp = data;
  2461. BT_DBG("");
  2462. return user_pairing_resp(sk, hdev, &cp->addr,
  2463. MGMT_OP_USER_CONFIRM_NEG_REPLY,
  2464. HCI_OP_USER_CONFIRM_NEG_REPLY, 0);
  2465. }
  2466. static int user_passkey_reply(struct sock *sk, struct hci_dev *hdev, void *data,
  2467. u16 len)
  2468. {
  2469. struct mgmt_cp_user_passkey_reply *cp = data;
  2470. BT_DBG("");
  2471. return user_pairing_resp(sk, hdev, &cp->addr,
  2472. MGMT_OP_USER_PASSKEY_REPLY,
  2473. HCI_OP_USER_PASSKEY_REPLY, cp->passkey);
  2474. }
  2475. static int user_passkey_neg_reply(struct sock *sk, struct hci_dev *hdev,
  2476. void *data, u16 len)
  2477. {
  2478. struct mgmt_cp_user_passkey_neg_reply *cp = data;
  2479. BT_DBG("");
  2480. return user_pairing_resp(sk, hdev, &cp->addr,
  2481. MGMT_OP_USER_PASSKEY_NEG_REPLY,
  2482. HCI_OP_USER_PASSKEY_NEG_REPLY, 0);
  2483. }
  2484. static void adv_expire(struct hci_dev *hdev, u32 flags)
  2485. {
  2486. struct adv_info *adv_instance;
  2487. struct hci_request req;
  2488. int err;
  2489. adv_instance = hci_find_adv_instance(hdev, hdev->cur_adv_instance);
  2490. if (!adv_instance)
  2491. return;
  2492. /* stop if current instance doesn't need to be changed */
  2493. if (!(adv_instance->flags & flags))
  2494. return;
  2495. cancel_adv_timeout(hdev);
  2496. adv_instance = hci_get_next_instance(hdev, adv_instance->instance);
  2497. if (!adv_instance)
  2498. return;
  2499. hci_req_init(&req, hdev);
  2500. err = __hci_req_schedule_adv_instance(&req, adv_instance->instance,
  2501. true);
  2502. if (err)
  2503. return;
  2504. hci_req_run(&req, NULL);
  2505. }
  2506. static void set_name_complete(struct hci_dev *hdev, u8 status, u16 opcode)
  2507. {
  2508. struct mgmt_cp_set_local_name *cp;
  2509. struct mgmt_pending_cmd *cmd;
  2510. BT_DBG("status 0x%02x", status);
  2511. hci_dev_lock(hdev);
  2512. cmd = pending_find(MGMT_OP_SET_LOCAL_NAME, hdev);
  2513. if (!cmd)
  2514. goto unlock;
  2515. cp = cmd->param;
  2516. if (status) {
  2517. mgmt_cmd_status(cmd->sk, hdev->id, MGMT_OP_SET_LOCAL_NAME,
  2518. mgmt_status(status));
  2519. } else {
  2520. mgmt_cmd_complete(cmd->sk, hdev->id, MGMT_OP_SET_LOCAL_NAME, 0,
  2521. cp, sizeof(*cp));
  2522. if (hci_dev_test_flag(hdev, HCI_LE_ADV))
  2523. adv_expire(hdev, MGMT_ADV_FLAG_LOCAL_NAME);
  2524. }
  2525. mgmt_pending_remove(cmd);
  2526. unlock:
  2527. hci_dev_unlock(hdev);
  2528. }
  2529. static int set_local_name(struct sock *sk, struct hci_dev *hdev, void *data,
  2530. u16 len)
  2531. {
  2532. struct mgmt_cp_set_local_name *cp = data;
  2533. struct mgmt_pending_cmd *cmd;
  2534. struct hci_request req;
  2535. int err;
  2536. BT_DBG("");
  2537. hci_dev_lock(hdev);
  2538. /* If the old values are the same as the new ones just return a
  2539. * direct command complete event.
  2540. */
  2541. if (!memcmp(hdev->dev_name, cp->name, sizeof(hdev->dev_name)) &&
  2542. !memcmp(hdev->short_name, cp->short_name,
  2543. sizeof(hdev->short_name))) {
  2544. err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_SET_LOCAL_NAME, 0,
  2545. data, len);
  2546. goto failed;
  2547. }
  2548. memcpy(hdev->short_name, cp->short_name, sizeof(hdev->short_name));
  2549. if (!hdev_is_powered(hdev)) {
  2550. memcpy(hdev->dev_name, cp->name, sizeof(hdev->dev_name));
  2551. err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_SET_LOCAL_NAME, 0,
  2552. data, len);
  2553. if (err < 0)
  2554. goto failed;
  2555. err = mgmt_limited_event(MGMT_EV_LOCAL_NAME_CHANGED, hdev, data,
  2556. len, HCI_MGMT_LOCAL_NAME_EVENTS, sk);
  2557. ext_info_changed(hdev, sk);
  2558. goto failed;
  2559. }
  2560. cmd = mgmt_pending_add(sk, MGMT_OP_SET_LOCAL_NAME, hdev, data, len);
  2561. if (!cmd) {
  2562. err = -ENOMEM;
  2563. goto failed;
  2564. }
  2565. memcpy(hdev->dev_name, cp->name, sizeof(hdev->dev_name));
  2566. hci_req_init(&req, hdev);
  2567. if (lmp_bredr_capable(hdev)) {
  2568. __hci_req_update_name(&req);
  2569. __hci_req_update_eir(&req);
  2570. }
  2571. /* The name is stored in the scan response data and so
  2572. * no need to udpate the advertising data here.
  2573. */
  2574. if (lmp_le_capable(hdev) && hci_dev_test_flag(hdev, HCI_ADVERTISING))
  2575. __hci_req_update_scan_rsp_data(&req, hdev->cur_adv_instance);
  2576. err = hci_req_run(&req, set_name_complete);
  2577. if (err < 0)
  2578. mgmt_pending_remove(cmd);
  2579. failed:
  2580. hci_dev_unlock(hdev);
  2581. return err;
  2582. }
  2583. static int set_appearance(struct sock *sk, struct hci_dev *hdev, void *data,
  2584. u16 len)
  2585. {
  2586. struct mgmt_cp_set_appearance *cp = data;
  2587. u16 apperance;
  2588. int err;
  2589. BT_DBG("");
  2590. if (!lmp_le_capable(hdev))
  2591. return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_APPEARANCE,
  2592. MGMT_STATUS_NOT_SUPPORTED);
  2593. apperance = le16_to_cpu(cp->appearance);
  2594. hci_dev_lock(hdev);
  2595. if (hdev->appearance != apperance) {
  2596. hdev->appearance = apperance;
  2597. if (hci_dev_test_flag(hdev, HCI_LE_ADV))
  2598. adv_expire(hdev, MGMT_ADV_FLAG_APPEARANCE);
  2599. ext_info_changed(hdev, sk);
  2600. }
  2601. err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_SET_APPEARANCE, 0, NULL,
  2602. 0);
  2603. hci_dev_unlock(hdev);
  2604. return err;
  2605. }
  2606. static int get_phy_configuration(struct sock *sk, struct hci_dev *hdev,
  2607. void *data, u16 len)
  2608. {
  2609. struct mgmt_rp_get_phy_confguration rp;
  2610. BT_DBG("sock %p %s", sk, hdev->name);
  2611. hci_dev_lock(hdev);
  2612. memset(&rp, 0, sizeof(rp));
  2613. rp.supported_phys = cpu_to_le32(get_supported_phys(hdev));
  2614. rp.selected_phys = cpu_to_le32(get_selected_phys(hdev));
  2615. rp.configurable_phys = cpu_to_le32(get_configurable_phys(hdev));
  2616. hci_dev_unlock(hdev);
  2617. return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_GET_PHY_CONFIGURATION, 0,
  2618. &rp, sizeof(rp));
  2619. }
  2620. int mgmt_phy_configuration_changed(struct hci_dev *hdev, struct sock *skip)
  2621. {
  2622. struct mgmt_ev_phy_configuration_changed ev;
  2623. memset(&ev, 0, sizeof(ev));
  2624. ev.selected_phys = cpu_to_le32(get_selected_phys(hdev));
  2625. return mgmt_event(MGMT_EV_PHY_CONFIGURATION_CHANGED, hdev, &ev,
  2626. sizeof(ev), skip);
  2627. }
  2628. static void set_default_phy_complete(struct hci_dev *hdev, u8 status,
  2629. u16 opcode, struct sk_buff *skb)
  2630. {
  2631. struct mgmt_pending_cmd *cmd;
  2632. BT_DBG("status 0x%02x", status);
  2633. hci_dev_lock(hdev);
  2634. cmd = pending_find(MGMT_OP_SET_PHY_CONFIGURATION, hdev);
  2635. if (!cmd)
  2636. goto unlock;
  2637. if (status) {
  2638. mgmt_cmd_status(cmd->sk, hdev->id,
  2639. MGMT_OP_SET_PHY_CONFIGURATION,
  2640. mgmt_status(status));
  2641. } else {
  2642. mgmt_cmd_complete(cmd->sk, hdev->id,
  2643. MGMT_OP_SET_PHY_CONFIGURATION, 0,
  2644. NULL, 0);
  2645. mgmt_phy_configuration_changed(hdev, cmd->sk);
  2646. }
  2647. mgmt_pending_remove(cmd);
  2648. unlock:
  2649. hci_dev_unlock(hdev);
  2650. }
  2651. static int set_phy_configuration(struct sock *sk, struct hci_dev *hdev,
  2652. void *data, u16 len)
  2653. {
  2654. struct mgmt_cp_set_phy_confguration *cp = data;
  2655. struct hci_cp_le_set_default_phy cp_phy;
  2656. struct mgmt_pending_cmd *cmd;
  2657. struct hci_request req;
  2658. u32 selected_phys, configurable_phys, supported_phys, unconfigure_phys;
  2659. u16 pkt_type = (HCI_DH1 | HCI_DM1);
  2660. bool changed = false;
  2661. int err;
  2662. BT_DBG("sock %p %s", sk, hdev->name);
  2663. configurable_phys = get_configurable_phys(hdev);
  2664. supported_phys = get_supported_phys(hdev);
  2665. selected_phys = __le32_to_cpu(cp->selected_phys);
  2666. if (selected_phys & ~supported_phys)
  2667. return mgmt_cmd_status(sk, hdev->id,
  2668. MGMT_OP_SET_PHY_CONFIGURATION,
  2669. MGMT_STATUS_INVALID_PARAMS);
  2670. unconfigure_phys = supported_phys & ~configurable_phys;
  2671. if ((selected_phys & unconfigure_phys) != unconfigure_phys)
  2672. return mgmt_cmd_status(sk, hdev->id,
  2673. MGMT_OP_SET_PHY_CONFIGURATION,
  2674. MGMT_STATUS_INVALID_PARAMS);
  2675. if (selected_phys == get_selected_phys(hdev))
  2676. return mgmt_cmd_complete(sk, hdev->id,
  2677. MGMT_OP_SET_PHY_CONFIGURATION,
  2678. 0, NULL, 0);
  2679. hci_dev_lock(hdev);
  2680. if (!hdev_is_powered(hdev)) {
  2681. err = mgmt_cmd_status(sk, hdev->id,
  2682. MGMT_OP_SET_PHY_CONFIGURATION,
  2683. MGMT_STATUS_REJECTED);
  2684. goto unlock;
  2685. }
  2686. if (pending_find(MGMT_OP_SET_PHY_CONFIGURATION, hdev)) {
  2687. err = mgmt_cmd_status(sk, hdev->id,
  2688. MGMT_OP_SET_PHY_CONFIGURATION,
  2689. MGMT_STATUS_BUSY);
  2690. goto unlock;
  2691. }
  2692. if (selected_phys & MGMT_PHY_BR_1M_3SLOT)
  2693. pkt_type |= (HCI_DH3 | HCI_DM3);
  2694. else
  2695. pkt_type &= ~(HCI_DH3 | HCI_DM3);
  2696. if (selected_phys & MGMT_PHY_BR_1M_5SLOT)
  2697. pkt_type |= (HCI_DH5 | HCI_DM5);
  2698. else
  2699. pkt_type &= ~(HCI_DH5 | HCI_DM5);
  2700. if (selected_phys & MGMT_PHY_EDR_2M_1SLOT)
  2701. pkt_type &= ~HCI_2DH1;
  2702. else
  2703. pkt_type |= HCI_2DH1;
  2704. if (selected_phys & MGMT_PHY_EDR_2M_3SLOT)
  2705. pkt_type &= ~HCI_2DH3;
  2706. else
  2707. pkt_type |= HCI_2DH3;
  2708. if (selected_phys & MGMT_PHY_EDR_2M_5SLOT)
  2709. pkt_type &= ~HCI_2DH5;
  2710. else
  2711. pkt_type |= HCI_2DH5;
  2712. if (selected_phys & MGMT_PHY_EDR_3M_1SLOT)
  2713. pkt_type &= ~HCI_3DH1;
  2714. else
  2715. pkt_type |= HCI_3DH1;
  2716. if (selected_phys & MGMT_PHY_EDR_3M_3SLOT)
  2717. pkt_type &= ~HCI_3DH3;
  2718. else
  2719. pkt_type |= HCI_3DH3;
  2720. if (selected_phys & MGMT_PHY_EDR_3M_5SLOT)
  2721. pkt_type &= ~HCI_3DH5;
  2722. else
  2723. pkt_type |= HCI_3DH5;
  2724. if (pkt_type != hdev->pkt_type) {
  2725. hdev->pkt_type = pkt_type;
  2726. changed = true;
  2727. }
  2728. if ((selected_phys & MGMT_PHY_LE_MASK) ==
  2729. (get_selected_phys(hdev) & MGMT_PHY_LE_MASK)) {
  2730. if (changed)
  2731. mgmt_phy_configuration_changed(hdev, sk);
  2732. err = mgmt_cmd_complete(sk, hdev->id,
  2733. MGMT_OP_SET_PHY_CONFIGURATION,
  2734. 0, NULL, 0);
  2735. goto unlock;
  2736. }
  2737. cmd = mgmt_pending_add(sk, MGMT_OP_SET_PHY_CONFIGURATION, hdev, data,
  2738. len);
  2739. if (!cmd) {
  2740. err = -ENOMEM;
  2741. goto unlock;
  2742. }
  2743. hci_req_init(&req, hdev);
  2744. memset(&cp_phy, 0, sizeof(cp_phy));
  2745. if (!(selected_phys & MGMT_PHY_LE_TX_MASK))
  2746. cp_phy.all_phys |= 0x01;
  2747. if (!(selected_phys & MGMT_PHY_LE_RX_MASK))
  2748. cp_phy.all_phys |= 0x02;
  2749. if (selected_phys & MGMT_PHY_LE_1M_TX)
  2750. cp_phy.tx_phys |= HCI_LE_SET_PHY_1M;
  2751. if (selected_phys & MGMT_PHY_LE_2M_TX)
  2752. cp_phy.tx_phys |= HCI_LE_SET_PHY_2M;
  2753. if (selected_phys & MGMT_PHY_LE_CODED_TX)
  2754. cp_phy.tx_phys |= HCI_LE_SET_PHY_CODED;
  2755. if (selected_phys & MGMT_PHY_LE_1M_RX)
  2756. cp_phy.rx_phys |= HCI_LE_SET_PHY_1M;
  2757. if (selected_phys & MGMT_PHY_LE_2M_RX)
  2758. cp_phy.rx_phys |= HCI_LE_SET_PHY_2M;
  2759. if (selected_phys & MGMT_PHY_LE_CODED_RX)
  2760. cp_phy.rx_phys |= HCI_LE_SET_PHY_CODED;
  2761. hci_req_add(&req, HCI_OP_LE_SET_DEFAULT_PHY, sizeof(cp_phy), &cp_phy);
  2762. err = hci_req_run_skb(&req, set_default_phy_complete);
  2763. if (err < 0)
  2764. mgmt_pending_remove(cmd);
  2765. unlock:
  2766. hci_dev_unlock(hdev);
  2767. return err;
  2768. }
  2769. static void read_local_oob_data_complete(struct hci_dev *hdev, u8 status,
  2770. u16 opcode, struct sk_buff *skb)
  2771. {
  2772. struct mgmt_rp_read_local_oob_data mgmt_rp;
  2773. size_t rp_size = sizeof(mgmt_rp);
  2774. struct mgmt_pending_cmd *cmd;
  2775. BT_DBG("%s status %u", hdev->name, status);
  2776. cmd = pending_find(MGMT_OP_READ_LOCAL_OOB_DATA, hdev);
  2777. if (!cmd)
  2778. return;
  2779. if (status || !skb) {
  2780. mgmt_cmd_status(cmd->sk, hdev->id, MGMT_OP_READ_LOCAL_OOB_DATA,
  2781. status ? mgmt_status(status) : MGMT_STATUS_FAILED);
  2782. goto remove;
  2783. }
  2784. memset(&mgmt_rp, 0, sizeof(mgmt_rp));
  2785. if (opcode == HCI_OP_READ_LOCAL_OOB_DATA) {
  2786. struct hci_rp_read_local_oob_data *rp = (void *) skb->data;
  2787. if (skb->len < sizeof(*rp)) {
  2788. mgmt_cmd_status(cmd->sk, hdev->id,
  2789. MGMT_OP_READ_LOCAL_OOB_DATA,
  2790. MGMT_STATUS_FAILED);
  2791. goto remove;
  2792. }
  2793. memcpy(mgmt_rp.hash192, rp->hash, sizeof(rp->hash));
  2794. memcpy(mgmt_rp.rand192, rp->rand, sizeof(rp->rand));
  2795. rp_size -= sizeof(mgmt_rp.hash256) + sizeof(mgmt_rp.rand256);
  2796. } else {
  2797. struct hci_rp_read_local_oob_ext_data *rp = (void *) skb->data;
  2798. if (skb->len < sizeof(*rp)) {
  2799. mgmt_cmd_status(cmd->sk, hdev->id,
  2800. MGMT_OP_READ_LOCAL_OOB_DATA,
  2801. MGMT_STATUS_FAILED);
  2802. goto remove;
  2803. }
  2804. memcpy(mgmt_rp.hash192, rp->hash192, sizeof(rp->hash192));
  2805. memcpy(mgmt_rp.rand192, rp->rand192, sizeof(rp->rand192));
  2806. memcpy(mgmt_rp.hash256, rp->hash256, sizeof(rp->hash256));
  2807. memcpy(mgmt_rp.rand256, rp->rand256, sizeof(rp->rand256));
  2808. }
  2809. mgmt_cmd_complete(cmd->sk, hdev->id, MGMT_OP_READ_LOCAL_OOB_DATA,
  2810. MGMT_STATUS_SUCCESS, &mgmt_rp, rp_size);
  2811. remove:
  2812. mgmt_pending_remove(cmd);
  2813. }
  2814. static int read_local_oob_data(struct sock *sk, struct hci_dev *hdev,
  2815. void *data, u16 data_len)
  2816. {
  2817. struct mgmt_pending_cmd *cmd;
  2818. struct hci_request req;
  2819. int err;
  2820. BT_DBG("%s", hdev->name);
  2821. hci_dev_lock(hdev);
  2822. if (!hdev_is_powered(hdev)) {
  2823. err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_READ_LOCAL_OOB_DATA,
  2824. MGMT_STATUS_NOT_POWERED);
  2825. goto unlock;
  2826. }
  2827. if (!lmp_ssp_capable(hdev)) {
  2828. err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_READ_LOCAL_OOB_DATA,
  2829. MGMT_STATUS_NOT_SUPPORTED);
  2830. goto unlock;
  2831. }
  2832. if (pending_find(MGMT_OP_READ_LOCAL_OOB_DATA, hdev)) {
  2833. err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_READ_LOCAL_OOB_DATA,
  2834. MGMT_STATUS_BUSY);
  2835. goto unlock;
  2836. }
  2837. cmd = mgmt_pending_add(sk, MGMT_OP_READ_LOCAL_OOB_DATA, hdev, NULL, 0);
  2838. if (!cmd) {
  2839. err = -ENOMEM;
  2840. goto unlock;
  2841. }
  2842. hci_req_init(&req, hdev);
  2843. if (bredr_sc_enabled(hdev))
  2844. hci_req_add(&req, HCI_OP_READ_LOCAL_OOB_EXT_DATA, 0, NULL);
  2845. else
  2846. hci_req_add(&req, HCI_OP_READ_LOCAL_OOB_DATA, 0, NULL);
  2847. err = hci_req_run_skb(&req, read_local_oob_data_complete);
  2848. if (err < 0)
  2849. mgmt_pending_remove(cmd);
  2850. unlock:
  2851. hci_dev_unlock(hdev);
  2852. return err;
  2853. }
  2854. static int add_remote_oob_data(struct sock *sk, struct hci_dev *hdev,
  2855. void *data, u16 len)
  2856. {
  2857. struct mgmt_addr_info *addr = data;
  2858. int err;
  2859. BT_DBG("%s ", hdev->name);
  2860. if (!bdaddr_type_is_valid(addr->type))
  2861. return mgmt_cmd_complete(sk, hdev->id,
  2862. MGMT_OP_ADD_REMOTE_OOB_DATA,
  2863. MGMT_STATUS_INVALID_PARAMS,
  2864. addr, sizeof(*addr));
  2865. hci_dev_lock(hdev);
  2866. if (len == MGMT_ADD_REMOTE_OOB_DATA_SIZE) {
  2867. struct mgmt_cp_add_remote_oob_data *cp = data;
  2868. u8 status;
  2869. if (cp->addr.type != BDADDR_BREDR) {
  2870. err = mgmt_cmd_complete(sk, hdev->id,
  2871. MGMT_OP_ADD_REMOTE_OOB_DATA,
  2872. MGMT_STATUS_INVALID_PARAMS,
  2873. &cp->addr, sizeof(cp->addr));
  2874. goto unlock;
  2875. }
  2876. err = hci_add_remote_oob_data(hdev, &cp->addr.bdaddr,
  2877. cp->addr.type, cp->hash,
  2878. cp->rand, NULL, NULL);
  2879. if (err < 0)
  2880. status = MGMT_STATUS_FAILED;
  2881. else
  2882. status = MGMT_STATUS_SUCCESS;
  2883. err = mgmt_cmd_complete(sk, hdev->id,
  2884. MGMT_OP_ADD_REMOTE_OOB_DATA, status,
  2885. &cp->addr, sizeof(cp->addr));
  2886. } else if (len == MGMT_ADD_REMOTE_OOB_EXT_DATA_SIZE) {
  2887. struct mgmt_cp_add_remote_oob_ext_data *cp = data;
  2888. u8 *rand192, *hash192, *rand256, *hash256;
  2889. u8 status;
  2890. if (bdaddr_type_is_le(cp->addr.type)) {
  2891. /* Enforce zero-valued 192-bit parameters as
  2892. * long as legacy SMP OOB isn't implemented.
  2893. */
  2894. if (memcmp(cp->rand192, ZERO_KEY, 16) ||
  2895. memcmp(cp->hash192, ZERO_KEY, 16)) {
  2896. err = mgmt_cmd_complete(sk, hdev->id,
  2897. MGMT_OP_ADD_REMOTE_OOB_DATA,
  2898. MGMT_STATUS_INVALID_PARAMS,
  2899. addr, sizeof(*addr));
  2900. goto unlock;
  2901. }
  2902. rand192 = NULL;
  2903. hash192 = NULL;
  2904. } else {
  2905. /* In case one of the P-192 values is set to zero,
  2906. * then just disable OOB data for P-192.
  2907. */
  2908. if (!memcmp(cp->rand192, ZERO_KEY, 16) ||
  2909. !memcmp(cp->hash192, ZERO_KEY, 16)) {
  2910. rand192 = NULL;
  2911. hash192 = NULL;
  2912. } else {
  2913. rand192 = cp->rand192;
  2914. hash192 = cp->hash192;
  2915. }
  2916. }
  2917. /* In case one of the P-256 values is set to zero, then just
  2918. * disable OOB data for P-256.
  2919. */
  2920. if (!memcmp(cp->rand256, ZERO_KEY, 16) ||
  2921. !memcmp(cp->hash256, ZERO_KEY, 16)) {
  2922. rand256 = NULL;
  2923. hash256 = NULL;
  2924. } else {
  2925. rand256 = cp->rand256;
  2926. hash256 = cp->hash256;
  2927. }
  2928. err = hci_add_remote_oob_data(hdev, &cp->addr.bdaddr,
  2929. cp->addr.type, hash192, rand192,
  2930. hash256, rand256);
  2931. if (err < 0)
  2932. status = MGMT_STATUS_FAILED;
  2933. else
  2934. status = MGMT_STATUS_SUCCESS;
  2935. err = mgmt_cmd_complete(sk, hdev->id,
  2936. MGMT_OP_ADD_REMOTE_OOB_DATA,
  2937. status, &cp->addr, sizeof(cp->addr));
  2938. } else {
  2939. bt_dev_err(hdev, "add_remote_oob_data: invalid len of %u bytes",
  2940. len);
  2941. err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_REMOTE_OOB_DATA,
  2942. MGMT_STATUS_INVALID_PARAMS);
  2943. }
  2944. unlock:
  2945. hci_dev_unlock(hdev);
  2946. return err;
  2947. }
  2948. static int remove_remote_oob_data(struct sock *sk, struct hci_dev *hdev,
  2949. void *data, u16 len)
  2950. {
  2951. struct mgmt_cp_remove_remote_oob_data *cp = data;
  2952. u8 status;
  2953. int err;
  2954. BT_DBG("%s", hdev->name);
  2955. if (cp->addr.type != BDADDR_BREDR)
  2956. return mgmt_cmd_complete(sk, hdev->id,
  2957. MGMT_OP_REMOVE_REMOTE_OOB_DATA,
  2958. MGMT_STATUS_INVALID_PARAMS,
  2959. &cp->addr, sizeof(cp->addr));
  2960. hci_dev_lock(hdev);
  2961. if (!bacmp(&cp->addr.bdaddr, BDADDR_ANY)) {
  2962. hci_remote_oob_data_clear(hdev);
  2963. status = MGMT_STATUS_SUCCESS;
  2964. goto done;
  2965. }
  2966. err = hci_remove_remote_oob_data(hdev, &cp->addr.bdaddr, cp->addr.type);
  2967. if (err < 0)
  2968. status = MGMT_STATUS_INVALID_PARAMS;
  2969. else
  2970. status = MGMT_STATUS_SUCCESS;
  2971. done:
  2972. err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_REMOVE_REMOTE_OOB_DATA,
  2973. status, &cp->addr, sizeof(cp->addr));
  2974. hci_dev_unlock(hdev);
  2975. return err;
  2976. }
  2977. void mgmt_start_discovery_complete(struct hci_dev *hdev, u8 status)
  2978. {
  2979. struct mgmt_pending_cmd *cmd;
  2980. BT_DBG("status %d", status);
  2981. hci_dev_lock(hdev);
  2982. cmd = pending_find(MGMT_OP_START_DISCOVERY, hdev);
  2983. if (!cmd)
  2984. cmd = pending_find(MGMT_OP_START_SERVICE_DISCOVERY, hdev);
  2985. if (!cmd)
  2986. cmd = pending_find(MGMT_OP_START_LIMITED_DISCOVERY, hdev);
  2987. if (cmd) {
  2988. cmd->cmd_complete(cmd, mgmt_status(status));
  2989. mgmt_pending_remove(cmd);
  2990. }
  2991. hci_dev_unlock(hdev);
  2992. }
  2993. static bool discovery_type_is_valid(struct hci_dev *hdev, uint8_t type,
  2994. uint8_t *mgmt_status)
  2995. {
  2996. switch (type) {
  2997. case DISCOV_TYPE_LE:
  2998. *mgmt_status = mgmt_le_support(hdev);
  2999. if (*mgmt_status)
  3000. return false;
  3001. break;
  3002. case DISCOV_TYPE_INTERLEAVED:
  3003. *mgmt_status = mgmt_le_support(hdev);
  3004. if (*mgmt_status)
  3005. return false;
  3006. /* Intentional fall-through */
  3007. case DISCOV_TYPE_BREDR:
  3008. *mgmt_status = mgmt_bredr_support(hdev);
  3009. if (*mgmt_status)
  3010. return false;
  3011. break;
  3012. default:
  3013. *mgmt_status = MGMT_STATUS_INVALID_PARAMS;
  3014. return false;
  3015. }
  3016. return true;
  3017. }
  3018. static int start_discovery_internal(struct sock *sk, struct hci_dev *hdev,
  3019. u16 op, void *data, u16 len)
  3020. {
  3021. struct mgmt_cp_start_discovery *cp = data;
  3022. struct mgmt_pending_cmd *cmd;
  3023. u8 status;
  3024. int err;
  3025. BT_DBG("%s", hdev->name);
  3026. hci_dev_lock(hdev);
  3027. if (!hdev_is_powered(hdev)) {
  3028. err = mgmt_cmd_complete(sk, hdev->id, op,
  3029. MGMT_STATUS_NOT_POWERED,
  3030. &cp->type, sizeof(cp->type));
  3031. goto failed;
  3032. }
  3033. if (hdev->discovery.state != DISCOVERY_STOPPED ||
  3034. hci_dev_test_flag(hdev, HCI_PERIODIC_INQ)) {
  3035. err = mgmt_cmd_complete(sk, hdev->id, op, MGMT_STATUS_BUSY,
  3036. &cp->type, sizeof(cp->type));
  3037. goto failed;
  3038. }
  3039. if (!discovery_type_is_valid(hdev, cp->type, &status)) {
  3040. err = mgmt_cmd_complete(sk, hdev->id, op, status,
  3041. &cp->type, sizeof(cp->type));
  3042. goto failed;
  3043. }
  3044. /* Clear the discovery filter first to free any previously
  3045. * allocated memory for the UUID list.
  3046. */
  3047. hci_discovery_filter_clear(hdev);
  3048. hdev->discovery.type = cp->type;
  3049. hdev->discovery.report_invalid_rssi = false;
  3050. if (op == MGMT_OP_START_LIMITED_DISCOVERY)
  3051. hdev->discovery.limited = true;
  3052. else
  3053. hdev->discovery.limited = false;
  3054. cmd = mgmt_pending_add(sk, op, hdev, data, len);
  3055. if (!cmd) {
  3056. err = -ENOMEM;
  3057. goto failed;
  3058. }
  3059. cmd->cmd_complete = generic_cmd_complete;
  3060. hci_discovery_set_state(hdev, DISCOVERY_STARTING);
  3061. queue_work(hdev->req_workqueue, &hdev->discov_update);
  3062. err = 0;
  3063. failed:
  3064. hci_dev_unlock(hdev);
  3065. return err;
  3066. }
  3067. static int start_discovery(struct sock *sk, struct hci_dev *hdev,
  3068. void *data, u16 len)
  3069. {
  3070. return start_discovery_internal(sk, hdev, MGMT_OP_START_DISCOVERY,
  3071. data, len);
  3072. }
  3073. static int start_limited_discovery(struct sock *sk, struct hci_dev *hdev,
  3074. void *data, u16 len)
  3075. {
  3076. return start_discovery_internal(sk, hdev,
  3077. MGMT_OP_START_LIMITED_DISCOVERY,
  3078. data, len);
  3079. }
  3080. static int service_discovery_cmd_complete(struct mgmt_pending_cmd *cmd,
  3081. u8 status)
  3082. {
  3083. return mgmt_cmd_complete(cmd->sk, cmd->index, cmd->opcode, status,
  3084. cmd->param, 1);
  3085. }
  3086. static int start_service_discovery(struct sock *sk, struct hci_dev *hdev,
  3087. void *data, u16 len)
  3088. {
  3089. struct mgmt_cp_start_service_discovery *cp = data;
  3090. struct mgmt_pending_cmd *cmd;
  3091. const u16 max_uuid_count = ((U16_MAX - sizeof(*cp)) / 16);
  3092. u16 uuid_count, expected_len;
  3093. u8 status;
  3094. int err;
  3095. BT_DBG("%s", hdev->name);
  3096. hci_dev_lock(hdev);
  3097. if (!hdev_is_powered(hdev)) {
  3098. err = mgmt_cmd_complete(sk, hdev->id,
  3099. MGMT_OP_START_SERVICE_DISCOVERY,
  3100. MGMT_STATUS_NOT_POWERED,
  3101. &cp->type, sizeof(cp->type));
  3102. goto failed;
  3103. }
  3104. if (hdev->discovery.state != DISCOVERY_STOPPED ||
  3105. hci_dev_test_flag(hdev, HCI_PERIODIC_INQ)) {
  3106. err = mgmt_cmd_complete(sk, hdev->id,
  3107. MGMT_OP_START_SERVICE_DISCOVERY,
  3108. MGMT_STATUS_BUSY, &cp->type,
  3109. sizeof(cp->type));
  3110. goto failed;
  3111. }
  3112. uuid_count = __le16_to_cpu(cp->uuid_count);
  3113. if (uuid_count > max_uuid_count) {
  3114. bt_dev_err(hdev, "service_discovery: too big uuid_count value %u",
  3115. uuid_count);
  3116. err = mgmt_cmd_complete(sk, hdev->id,
  3117. MGMT_OP_START_SERVICE_DISCOVERY,
  3118. MGMT_STATUS_INVALID_PARAMS, &cp->type,
  3119. sizeof(cp->type));
  3120. goto failed;
  3121. }
  3122. expected_len = sizeof(*cp) + uuid_count * 16;
  3123. if (expected_len != len) {
  3124. bt_dev_err(hdev, "service_discovery: expected %u bytes, got %u bytes",
  3125. expected_len, len);
  3126. err = mgmt_cmd_complete(sk, hdev->id,
  3127. MGMT_OP_START_SERVICE_DISCOVERY,
  3128. MGMT_STATUS_INVALID_PARAMS, &cp->type,
  3129. sizeof(cp->type));
  3130. goto failed;
  3131. }
  3132. if (!discovery_type_is_valid(hdev, cp->type, &status)) {
  3133. err = mgmt_cmd_complete(sk, hdev->id,
  3134. MGMT_OP_START_SERVICE_DISCOVERY,
  3135. status, &cp->type, sizeof(cp->type));
  3136. goto failed;
  3137. }
  3138. cmd = mgmt_pending_add(sk, MGMT_OP_START_SERVICE_DISCOVERY,
  3139. hdev, data, len);
  3140. if (!cmd) {
  3141. err = -ENOMEM;
  3142. goto failed;
  3143. }
  3144. cmd->cmd_complete = service_discovery_cmd_complete;
  3145. /* Clear the discovery filter first to free any previously
  3146. * allocated memory for the UUID list.
  3147. */
  3148. hci_discovery_filter_clear(hdev);
  3149. hdev->discovery.result_filtering = true;
  3150. hdev->discovery.type = cp->type;
  3151. hdev->discovery.rssi = cp->rssi;
  3152. hdev->discovery.uuid_count = uuid_count;
  3153. if (uuid_count > 0) {
  3154. hdev->discovery.uuids = kmemdup(cp->uuids, uuid_count * 16,
  3155. GFP_KERNEL);
  3156. if (!hdev->discovery.uuids) {
  3157. err = mgmt_cmd_complete(sk, hdev->id,
  3158. MGMT_OP_START_SERVICE_DISCOVERY,
  3159. MGMT_STATUS_FAILED,
  3160. &cp->type, sizeof(cp->type));
  3161. mgmt_pending_remove(cmd);
  3162. goto failed;
  3163. }
  3164. }
  3165. hci_discovery_set_state(hdev, DISCOVERY_STARTING);
  3166. queue_work(hdev->req_workqueue, &hdev->discov_update);
  3167. err = 0;
  3168. failed:
  3169. hci_dev_unlock(hdev);
  3170. return err;
  3171. }
  3172. void mgmt_stop_discovery_complete(struct hci_dev *hdev, u8 status)
  3173. {
  3174. struct mgmt_pending_cmd *cmd;
  3175. BT_DBG("status %d", status);
  3176. hci_dev_lock(hdev);
  3177. cmd = pending_find(MGMT_OP_STOP_DISCOVERY, hdev);
  3178. if (cmd) {
  3179. cmd->cmd_complete(cmd, mgmt_status(status));
  3180. mgmt_pending_remove(cmd);
  3181. }
  3182. hci_dev_unlock(hdev);
  3183. }
  3184. static int stop_discovery(struct sock *sk, struct hci_dev *hdev, void *data,
  3185. u16 len)
  3186. {
  3187. struct mgmt_cp_stop_discovery *mgmt_cp = data;
  3188. struct mgmt_pending_cmd *cmd;
  3189. int err;
  3190. BT_DBG("%s", hdev->name);
  3191. hci_dev_lock(hdev);
  3192. if (!hci_discovery_active(hdev)) {
  3193. err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_STOP_DISCOVERY,
  3194. MGMT_STATUS_REJECTED, &mgmt_cp->type,
  3195. sizeof(mgmt_cp->type));
  3196. goto unlock;
  3197. }
  3198. if (hdev->discovery.type != mgmt_cp->type) {
  3199. err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_STOP_DISCOVERY,
  3200. MGMT_STATUS_INVALID_PARAMS,
  3201. &mgmt_cp->type, sizeof(mgmt_cp->type));
  3202. goto unlock;
  3203. }
  3204. cmd = mgmt_pending_add(sk, MGMT_OP_STOP_DISCOVERY, hdev, data, len);
  3205. if (!cmd) {
  3206. err = -ENOMEM;
  3207. goto unlock;
  3208. }
  3209. cmd->cmd_complete = generic_cmd_complete;
  3210. hci_discovery_set_state(hdev, DISCOVERY_STOPPING);
  3211. queue_work(hdev->req_workqueue, &hdev->discov_update);
  3212. err = 0;
  3213. unlock:
  3214. hci_dev_unlock(hdev);
  3215. return err;
  3216. }
  3217. static int confirm_name(struct sock *sk, struct hci_dev *hdev, void *data,
  3218. u16 len)
  3219. {
  3220. struct mgmt_cp_confirm_name *cp = data;
  3221. struct inquiry_entry *e;
  3222. int err;
  3223. BT_DBG("%s", hdev->name);
  3224. hci_dev_lock(hdev);
  3225. if (!hci_discovery_active(hdev)) {
  3226. err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_CONFIRM_NAME,
  3227. MGMT_STATUS_FAILED, &cp->addr,
  3228. sizeof(cp->addr));
  3229. goto failed;
  3230. }
  3231. e = hci_inquiry_cache_lookup_unknown(hdev, &cp->addr.bdaddr);
  3232. if (!e) {
  3233. err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_CONFIRM_NAME,
  3234. MGMT_STATUS_INVALID_PARAMS, &cp->addr,
  3235. sizeof(cp->addr));
  3236. goto failed;
  3237. }
  3238. if (cp->name_known) {
  3239. e->name_state = NAME_KNOWN;
  3240. list_del(&e->list);
  3241. } else {
  3242. e->name_state = NAME_NEEDED;
  3243. hci_inquiry_cache_update_resolve(hdev, e);
  3244. }
  3245. err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_CONFIRM_NAME, 0,
  3246. &cp->addr, sizeof(cp->addr));
  3247. failed:
  3248. hci_dev_unlock(hdev);
  3249. return err;
  3250. }
  3251. static int block_device(struct sock *sk, struct hci_dev *hdev, void *data,
  3252. u16 len)
  3253. {
  3254. struct mgmt_cp_block_device *cp = data;
  3255. u8 status;
  3256. int err;
  3257. BT_DBG("%s", hdev->name);
  3258. if (!bdaddr_type_is_valid(cp->addr.type))
  3259. return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_BLOCK_DEVICE,
  3260. MGMT_STATUS_INVALID_PARAMS,
  3261. &cp->addr, sizeof(cp->addr));
  3262. hci_dev_lock(hdev);
  3263. err = hci_bdaddr_list_add(&hdev->blacklist, &cp->addr.bdaddr,
  3264. cp->addr.type);
  3265. if (err < 0) {
  3266. status = MGMT_STATUS_FAILED;
  3267. goto done;
  3268. }
  3269. mgmt_event(MGMT_EV_DEVICE_BLOCKED, hdev, &cp->addr, sizeof(cp->addr),
  3270. sk);
  3271. status = MGMT_STATUS_SUCCESS;
  3272. done:
  3273. err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_BLOCK_DEVICE, status,
  3274. &cp->addr, sizeof(cp->addr));
  3275. hci_dev_unlock(hdev);
  3276. return err;
  3277. }
  3278. static int unblock_device(struct sock *sk, struct hci_dev *hdev, void *data,
  3279. u16 len)
  3280. {
  3281. struct mgmt_cp_unblock_device *cp = data;
  3282. u8 status;
  3283. int err;
  3284. BT_DBG("%s", hdev->name);
  3285. if (!bdaddr_type_is_valid(cp->addr.type))
  3286. return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_UNBLOCK_DEVICE,
  3287. MGMT_STATUS_INVALID_PARAMS,
  3288. &cp->addr, sizeof(cp->addr));
  3289. hci_dev_lock(hdev);
  3290. err = hci_bdaddr_list_del(&hdev->blacklist, &cp->addr.bdaddr,
  3291. cp->addr.type);
  3292. if (err < 0) {
  3293. status = MGMT_STATUS_INVALID_PARAMS;
  3294. goto done;
  3295. }
  3296. mgmt_event(MGMT_EV_DEVICE_UNBLOCKED, hdev, &cp->addr, sizeof(cp->addr),
  3297. sk);
  3298. status = MGMT_STATUS_SUCCESS;
  3299. done:
  3300. err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_UNBLOCK_DEVICE, status,
  3301. &cp->addr, sizeof(cp->addr));
  3302. hci_dev_unlock(hdev);
  3303. return err;
  3304. }
  3305. static int set_device_id(struct sock *sk, struct hci_dev *hdev, void *data,
  3306. u16 len)
  3307. {
  3308. struct mgmt_cp_set_device_id *cp = data;
  3309. struct hci_request req;
  3310. int err;
  3311. __u16 source;
  3312. BT_DBG("%s", hdev->name);
  3313. source = __le16_to_cpu(cp->source);
  3314. if (source > 0x0002)
  3315. return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DEVICE_ID,
  3316. MGMT_STATUS_INVALID_PARAMS);
  3317. hci_dev_lock(hdev);
  3318. hdev->devid_source = source;
  3319. hdev->devid_vendor = __le16_to_cpu(cp->vendor);
  3320. hdev->devid_product = __le16_to_cpu(cp->product);
  3321. hdev->devid_version = __le16_to_cpu(cp->version);
  3322. err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_SET_DEVICE_ID, 0,
  3323. NULL, 0);
  3324. hci_req_init(&req, hdev);
  3325. __hci_req_update_eir(&req);
  3326. hci_req_run(&req, NULL);
  3327. hci_dev_unlock(hdev);
  3328. return err;
  3329. }
  3330. static void enable_advertising_instance(struct hci_dev *hdev, u8 status,
  3331. u16 opcode)
  3332. {
  3333. BT_DBG("status %d", status);
  3334. }
  3335. static void set_advertising_complete(struct hci_dev *hdev, u8 status,
  3336. u16 opcode)
  3337. {
  3338. struct cmd_lookup match = { NULL, hdev };
  3339. struct hci_request req;
  3340. u8 instance;
  3341. struct adv_info *adv_instance;
  3342. int err;
  3343. hci_dev_lock(hdev);
  3344. if (status) {
  3345. u8 mgmt_err = mgmt_status(status);
  3346. mgmt_pending_foreach(MGMT_OP_SET_ADVERTISING, hdev,
  3347. cmd_status_rsp, &mgmt_err);
  3348. goto unlock;
  3349. }
  3350. if (hci_dev_test_flag(hdev, HCI_LE_ADV))
  3351. hci_dev_set_flag(hdev, HCI_ADVERTISING);
  3352. else
  3353. hci_dev_clear_flag(hdev, HCI_ADVERTISING);
  3354. mgmt_pending_foreach(MGMT_OP_SET_ADVERTISING, hdev, settings_rsp,
  3355. &match);
  3356. new_settings(hdev, match.sk);
  3357. if (match.sk)
  3358. sock_put(match.sk);
  3359. /* If "Set Advertising" was just disabled and instance advertising was
  3360. * set up earlier, then re-enable multi-instance advertising.
  3361. */
  3362. if (hci_dev_test_flag(hdev, HCI_ADVERTISING) ||
  3363. list_empty(&hdev->adv_instances))
  3364. goto unlock;
  3365. instance = hdev->cur_adv_instance;
  3366. if (!instance) {
  3367. adv_instance = list_first_entry_or_null(&hdev->adv_instances,
  3368. struct adv_info, list);
  3369. if (!adv_instance)
  3370. goto unlock;
  3371. instance = adv_instance->instance;
  3372. }
  3373. hci_req_init(&req, hdev);
  3374. err = __hci_req_schedule_adv_instance(&req, instance, true);
  3375. if (!err)
  3376. err = hci_req_run(&req, enable_advertising_instance);
  3377. if (err)
  3378. bt_dev_err(hdev, "failed to re-configure advertising");
  3379. unlock:
  3380. hci_dev_unlock(hdev);
  3381. }
  3382. static int set_advertising(struct sock *sk, struct hci_dev *hdev, void *data,
  3383. u16 len)
  3384. {
  3385. struct mgmt_mode *cp = data;
  3386. struct mgmt_pending_cmd *cmd;
  3387. struct hci_request req;
  3388. u8 val, status;
  3389. int err;
  3390. BT_DBG("request for %s", hdev->name);
  3391. status = mgmt_le_support(hdev);
  3392. if (status)
  3393. return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_ADVERTISING,
  3394. status);
  3395. if (cp->val != 0x00 && cp->val != 0x01 && cp->val != 0x02)
  3396. return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_ADVERTISING,
  3397. MGMT_STATUS_INVALID_PARAMS);
  3398. hci_dev_lock(hdev);
  3399. val = !!cp->val;
  3400. /* The following conditions are ones which mean that we should
  3401. * not do any HCI communication but directly send a mgmt
  3402. * response to user space (after toggling the flag if
  3403. * necessary).
  3404. */
  3405. if (!hdev_is_powered(hdev) ||
  3406. (val == hci_dev_test_flag(hdev, HCI_ADVERTISING) &&
  3407. (cp->val == 0x02) == hci_dev_test_flag(hdev, HCI_ADVERTISING_CONNECTABLE)) ||
  3408. hci_conn_num(hdev, LE_LINK) > 0 ||
  3409. (hci_dev_test_flag(hdev, HCI_LE_SCAN) &&
  3410. hdev->le_scan_type == LE_SCAN_ACTIVE)) {
  3411. bool changed;
  3412. if (cp->val) {
  3413. hdev->cur_adv_instance = 0x00;
  3414. changed = !hci_dev_test_and_set_flag(hdev, HCI_ADVERTISING);
  3415. if (cp->val == 0x02)
  3416. hci_dev_set_flag(hdev, HCI_ADVERTISING_CONNECTABLE);
  3417. else
  3418. hci_dev_clear_flag(hdev, HCI_ADVERTISING_CONNECTABLE);
  3419. } else {
  3420. changed = hci_dev_test_and_clear_flag(hdev, HCI_ADVERTISING);
  3421. hci_dev_clear_flag(hdev, HCI_ADVERTISING_CONNECTABLE);
  3422. }
  3423. err = send_settings_rsp(sk, MGMT_OP_SET_ADVERTISING, hdev);
  3424. if (err < 0)
  3425. goto unlock;
  3426. if (changed)
  3427. err = new_settings(hdev, sk);
  3428. goto unlock;
  3429. }
  3430. if (pending_find(MGMT_OP_SET_ADVERTISING, hdev) ||
  3431. pending_find(MGMT_OP_SET_LE, hdev)) {
  3432. err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_ADVERTISING,
  3433. MGMT_STATUS_BUSY);
  3434. goto unlock;
  3435. }
  3436. cmd = mgmt_pending_add(sk, MGMT_OP_SET_ADVERTISING, hdev, data, len);
  3437. if (!cmd) {
  3438. err = -ENOMEM;
  3439. goto unlock;
  3440. }
  3441. hci_req_init(&req, hdev);
  3442. if (cp->val == 0x02)
  3443. hci_dev_set_flag(hdev, HCI_ADVERTISING_CONNECTABLE);
  3444. else
  3445. hci_dev_clear_flag(hdev, HCI_ADVERTISING_CONNECTABLE);
  3446. cancel_adv_timeout(hdev);
  3447. if (val) {
  3448. /* Switch to instance "0" for the Set Advertising setting.
  3449. * We cannot use update_[adv|scan_rsp]_data() here as the
  3450. * HCI_ADVERTISING flag is not yet set.
  3451. */
  3452. hdev->cur_adv_instance = 0x00;
  3453. if (ext_adv_capable(hdev)) {
  3454. __hci_req_start_ext_adv(&req, 0x00);
  3455. } else {
  3456. __hci_req_update_adv_data(&req, 0x00);
  3457. __hci_req_update_scan_rsp_data(&req, 0x00);
  3458. __hci_req_enable_advertising(&req);
  3459. }
  3460. } else {
  3461. __hci_req_disable_advertising(&req);
  3462. }
  3463. err = hci_req_run(&req, set_advertising_complete);
  3464. if (err < 0)
  3465. mgmt_pending_remove(cmd);
  3466. unlock:
  3467. hci_dev_unlock(hdev);
  3468. return err;
  3469. }
  3470. static int set_static_address(struct sock *sk, struct hci_dev *hdev,
  3471. void *data, u16 len)
  3472. {
  3473. struct mgmt_cp_set_static_address *cp = data;
  3474. int err;
  3475. BT_DBG("%s", hdev->name);
  3476. if (!lmp_le_capable(hdev))
  3477. return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_STATIC_ADDRESS,
  3478. MGMT_STATUS_NOT_SUPPORTED);
  3479. if (hdev_is_powered(hdev))
  3480. return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_STATIC_ADDRESS,
  3481. MGMT_STATUS_REJECTED);
  3482. if (bacmp(&cp->bdaddr, BDADDR_ANY)) {
  3483. if (!bacmp(&cp->bdaddr, BDADDR_NONE))
  3484. return mgmt_cmd_status(sk, hdev->id,
  3485. MGMT_OP_SET_STATIC_ADDRESS,
  3486. MGMT_STATUS_INVALID_PARAMS);
  3487. /* Two most significant bits shall be set */
  3488. if ((cp->bdaddr.b[5] & 0xc0) != 0xc0)
  3489. return mgmt_cmd_status(sk, hdev->id,
  3490. MGMT_OP_SET_STATIC_ADDRESS,
  3491. MGMT_STATUS_INVALID_PARAMS);
  3492. }
  3493. hci_dev_lock(hdev);
  3494. bacpy(&hdev->static_addr, &cp->bdaddr);
  3495. err = send_settings_rsp(sk, MGMT_OP_SET_STATIC_ADDRESS, hdev);
  3496. if (err < 0)
  3497. goto unlock;
  3498. err = new_settings(hdev, sk);
  3499. unlock:
  3500. hci_dev_unlock(hdev);
  3501. return err;
  3502. }
  3503. static int set_scan_params(struct sock *sk, struct hci_dev *hdev,
  3504. void *data, u16 len)
  3505. {
  3506. struct mgmt_cp_set_scan_params *cp = data;
  3507. __u16 interval, window;
  3508. int err;
  3509. BT_DBG("%s", hdev->name);
  3510. if (!lmp_le_capable(hdev))
  3511. return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SCAN_PARAMS,
  3512. MGMT_STATUS_NOT_SUPPORTED);
  3513. interval = __le16_to_cpu(cp->interval);
  3514. if (interval < 0x0004 || interval > 0x4000)
  3515. return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SCAN_PARAMS,
  3516. MGMT_STATUS_INVALID_PARAMS);
  3517. window = __le16_to_cpu(cp->window);
  3518. if (window < 0x0004 || window > 0x4000)
  3519. return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SCAN_PARAMS,
  3520. MGMT_STATUS_INVALID_PARAMS);
  3521. if (window > interval)
  3522. return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SCAN_PARAMS,
  3523. MGMT_STATUS_INVALID_PARAMS);
  3524. hci_dev_lock(hdev);
  3525. hdev->le_scan_interval = interval;
  3526. hdev->le_scan_window = window;
  3527. err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_SET_SCAN_PARAMS, 0,
  3528. NULL, 0);
  3529. /* If background scan is running, restart it so new parameters are
  3530. * loaded.
  3531. */
  3532. if (hci_dev_test_flag(hdev, HCI_LE_SCAN) &&
  3533. hdev->discovery.state == DISCOVERY_STOPPED) {
  3534. struct hci_request req;
  3535. hci_req_init(&req, hdev);
  3536. hci_req_add_le_scan_disable(&req);
  3537. hci_req_add_le_passive_scan(&req);
  3538. hci_req_run(&req, NULL);
  3539. }
  3540. hci_dev_unlock(hdev);
  3541. return err;
  3542. }
  3543. static void fast_connectable_complete(struct hci_dev *hdev, u8 status,
  3544. u16 opcode)
  3545. {
  3546. struct mgmt_pending_cmd *cmd;
  3547. BT_DBG("status 0x%02x", status);
  3548. hci_dev_lock(hdev);
  3549. cmd = pending_find(MGMT_OP_SET_FAST_CONNECTABLE, hdev);
  3550. if (!cmd)
  3551. goto unlock;
  3552. if (status) {
  3553. mgmt_cmd_status(cmd->sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE,
  3554. mgmt_status(status));
  3555. } else {
  3556. struct mgmt_mode *cp = cmd->param;
  3557. if (cp->val)
  3558. hci_dev_set_flag(hdev, HCI_FAST_CONNECTABLE);
  3559. else
  3560. hci_dev_clear_flag(hdev, HCI_FAST_CONNECTABLE);
  3561. send_settings_rsp(cmd->sk, MGMT_OP_SET_FAST_CONNECTABLE, hdev);
  3562. new_settings(hdev, cmd->sk);
  3563. }
  3564. mgmt_pending_remove(cmd);
  3565. unlock:
  3566. hci_dev_unlock(hdev);
  3567. }
  3568. static int set_fast_connectable(struct sock *sk, struct hci_dev *hdev,
  3569. void *data, u16 len)
  3570. {
  3571. struct mgmt_mode *cp = data;
  3572. struct mgmt_pending_cmd *cmd;
  3573. struct hci_request req;
  3574. int err;
  3575. BT_DBG("%s", hdev->name);
  3576. if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED) ||
  3577. hdev->hci_ver < BLUETOOTH_VER_1_2)
  3578. return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE,
  3579. MGMT_STATUS_NOT_SUPPORTED);
  3580. if (cp->val != 0x00 && cp->val != 0x01)
  3581. return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE,
  3582. MGMT_STATUS_INVALID_PARAMS);
  3583. hci_dev_lock(hdev);
  3584. if (pending_find(MGMT_OP_SET_FAST_CONNECTABLE, hdev)) {
  3585. err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE,
  3586. MGMT_STATUS_BUSY);
  3587. goto unlock;
  3588. }
  3589. if (!!cp->val == hci_dev_test_flag(hdev, HCI_FAST_CONNECTABLE)) {
  3590. err = send_settings_rsp(sk, MGMT_OP_SET_FAST_CONNECTABLE,
  3591. hdev);
  3592. goto unlock;
  3593. }
  3594. if (!hdev_is_powered(hdev)) {
  3595. hci_dev_change_flag(hdev, HCI_FAST_CONNECTABLE);
  3596. err = send_settings_rsp(sk, MGMT_OP_SET_FAST_CONNECTABLE,
  3597. hdev);
  3598. new_settings(hdev, sk);
  3599. goto unlock;
  3600. }
  3601. cmd = mgmt_pending_add(sk, MGMT_OP_SET_FAST_CONNECTABLE, hdev,
  3602. data, len);
  3603. if (!cmd) {
  3604. err = -ENOMEM;
  3605. goto unlock;
  3606. }
  3607. hci_req_init(&req, hdev);
  3608. __hci_req_write_fast_connectable(&req, cp->val);
  3609. err = hci_req_run(&req, fast_connectable_complete);
  3610. if (err < 0) {
  3611. err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE,
  3612. MGMT_STATUS_FAILED);
  3613. mgmt_pending_remove(cmd);
  3614. }
  3615. unlock:
  3616. hci_dev_unlock(hdev);
  3617. return err;
  3618. }
  3619. static void set_bredr_complete(struct hci_dev *hdev, u8 status, u16 opcode)
  3620. {
  3621. struct mgmt_pending_cmd *cmd;
  3622. BT_DBG("status 0x%02x", status);
  3623. hci_dev_lock(hdev);
  3624. cmd = pending_find(MGMT_OP_SET_BREDR, hdev);
  3625. if (!cmd)
  3626. goto unlock;
  3627. if (status) {
  3628. u8 mgmt_err = mgmt_status(status);
  3629. /* We need to restore the flag if related HCI commands
  3630. * failed.
  3631. */
  3632. hci_dev_clear_flag(hdev, HCI_BREDR_ENABLED);
  3633. mgmt_cmd_status(cmd->sk, cmd->index, cmd->opcode, mgmt_err);
  3634. } else {
  3635. send_settings_rsp(cmd->sk, MGMT_OP_SET_BREDR, hdev);
  3636. new_settings(hdev, cmd->sk);
  3637. }
  3638. mgmt_pending_remove(cmd);
  3639. unlock:
  3640. hci_dev_unlock(hdev);
  3641. }
  3642. static int set_bredr(struct sock *sk, struct hci_dev *hdev, void *data, u16 len)
  3643. {
  3644. struct mgmt_mode *cp = data;
  3645. struct mgmt_pending_cmd *cmd;
  3646. struct hci_request req;
  3647. int err;
  3648. BT_DBG("request for %s", hdev->name);
  3649. if (!lmp_bredr_capable(hdev) || !lmp_le_capable(hdev))
  3650. return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_BREDR,
  3651. MGMT_STATUS_NOT_SUPPORTED);
  3652. if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED))
  3653. return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_BREDR,
  3654. MGMT_STATUS_REJECTED);
  3655. if (cp->val != 0x00 && cp->val != 0x01)
  3656. return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_BREDR,
  3657. MGMT_STATUS_INVALID_PARAMS);
  3658. hci_dev_lock(hdev);
  3659. if (cp->val == hci_dev_test_flag(hdev, HCI_BREDR_ENABLED)) {
  3660. err = send_settings_rsp(sk, MGMT_OP_SET_BREDR, hdev);
  3661. goto unlock;
  3662. }
  3663. if (!hdev_is_powered(hdev)) {
  3664. if (!cp->val) {
  3665. hci_dev_clear_flag(hdev, HCI_DISCOVERABLE);
  3666. hci_dev_clear_flag(hdev, HCI_SSP_ENABLED);
  3667. hci_dev_clear_flag(hdev, HCI_LINK_SECURITY);
  3668. hci_dev_clear_flag(hdev, HCI_FAST_CONNECTABLE);
  3669. hci_dev_clear_flag(hdev, HCI_HS_ENABLED);
  3670. }
  3671. hci_dev_change_flag(hdev, HCI_BREDR_ENABLED);
  3672. err = send_settings_rsp(sk, MGMT_OP_SET_BREDR, hdev);
  3673. if (err < 0)
  3674. goto unlock;
  3675. err = new_settings(hdev, sk);
  3676. goto unlock;
  3677. }
  3678. /* Reject disabling when powered on */
  3679. if (!cp->val) {
  3680. err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_BREDR,
  3681. MGMT_STATUS_REJECTED);
  3682. goto unlock;
  3683. } else {
  3684. /* When configuring a dual-mode controller to operate
  3685. * with LE only and using a static address, then switching
  3686. * BR/EDR back on is not allowed.
  3687. *
  3688. * Dual-mode controllers shall operate with the public
  3689. * address as its identity address for BR/EDR and LE. So
  3690. * reject the attempt to create an invalid configuration.
  3691. *
  3692. * The same restrictions applies when secure connections
  3693. * has been enabled. For BR/EDR this is a controller feature
  3694. * while for LE it is a host stack feature. This means that
  3695. * switching BR/EDR back on when secure connections has been
  3696. * enabled is not a supported transaction.
  3697. */
  3698. if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED) &&
  3699. (bacmp(&hdev->static_addr, BDADDR_ANY) ||
  3700. hci_dev_test_flag(hdev, HCI_SC_ENABLED))) {
  3701. err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_BREDR,
  3702. MGMT_STATUS_REJECTED);
  3703. goto unlock;
  3704. }
  3705. }
  3706. if (pending_find(MGMT_OP_SET_BREDR, hdev)) {
  3707. err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_BREDR,
  3708. MGMT_STATUS_BUSY);
  3709. goto unlock;
  3710. }
  3711. cmd = mgmt_pending_add(sk, MGMT_OP_SET_BREDR, hdev, data, len);
  3712. if (!cmd) {
  3713. err = -ENOMEM;
  3714. goto unlock;
  3715. }
  3716. /* We need to flip the bit already here so that
  3717. * hci_req_update_adv_data generates the correct flags.
  3718. */
  3719. hci_dev_set_flag(hdev, HCI_BREDR_ENABLED);
  3720. hci_req_init(&req, hdev);
  3721. __hci_req_write_fast_connectable(&req, false);
  3722. __hci_req_update_scan(&req);
  3723. /* Since only the advertising data flags will change, there
  3724. * is no need to update the scan response data.
  3725. */
  3726. __hci_req_update_adv_data(&req, hdev->cur_adv_instance);
  3727. err = hci_req_run(&req, set_bredr_complete);
  3728. if (err < 0)
  3729. mgmt_pending_remove(cmd);
  3730. unlock:
  3731. hci_dev_unlock(hdev);
  3732. return err;
  3733. }
  3734. static void sc_enable_complete(struct hci_dev *hdev, u8 status, u16 opcode)
  3735. {
  3736. struct mgmt_pending_cmd *cmd;
  3737. struct mgmt_mode *cp;
  3738. BT_DBG("%s status %u", hdev->name, status);
  3739. hci_dev_lock(hdev);
  3740. cmd = pending_find(MGMT_OP_SET_SECURE_CONN, hdev);
  3741. if (!cmd)
  3742. goto unlock;
  3743. if (status) {
  3744. mgmt_cmd_status(cmd->sk, cmd->index, cmd->opcode,
  3745. mgmt_status(status));
  3746. goto remove;
  3747. }
  3748. cp = cmd->param;
  3749. switch (cp->val) {
  3750. case 0x00:
  3751. hci_dev_clear_flag(hdev, HCI_SC_ENABLED);
  3752. hci_dev_clear_flag(hdev, HCI_SC_ONLY);
  3753. break;
  3754. case 0x01:
  3755. hci_dev_set_flag(hdev, HCI_SC_ENABLED);
  3756. hci_dev_clear_flag(hdev, HCI_SC_ONLY);
  3757. break;
  3758. case 0x02:
  3759. hci_dev_set_flag(hdev, HCI_SC_ENABLED);
  3760. hci_dev_set_flag(hdev, HCI_SC_ONLY);
  3761. break;
  3762. }
  3763. send_settings_rsp(cmd->sk, MGMT_OP_SET_SECURE_CONN, hdev);
  3764. new_settings(hdev, cmd->sk);
  3765. remove:
  3766. mgmt_pending_remove(cmd);
  3767. unlock:
  3768. hci_dev_unlock(hdev);
  3769. }
  3770. static int set_secure_conn(struct sock *sk, struct hci_dev *hdev,
  3771. void *data, u16 len)
  3772. {
  3773. struct mgmt_mode *cp = data;
  3774. struct mgmt_pending_cmd *cmd;
  3775. struct hci_request req;
  3776. u8 val;
  3777. int err;
  3778. BT_DBG("request for %s", hdev->name);
  3779. if (!lmp_sc_capable(hdev) &&
  3780. !hci_dev_test_flag(hdev, HCI_LE_ENABLED))
  3781. return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SECURE_CONN,
  3782. MGMT_STATUS_NOT_SUPPORTED);
  3783. if (hci_dev_test_flag(hdev, HCI_BREDR_ENABLED) &&
  3784. lmp_sc_capable(hdev) &&
  3785. !hci_dev_test_flag(hdev, HCI_SSP_ENABLED))
  3786. return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SECURE_CONN,
  3787. MGMT_STATUS_REJECTED);
  3788. if (cp->val != 0x00 && cp->val != 0x01 && cp->val != 0x02)
  3789. return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SECURE_CONN,
  3790. MGMT_STATUS_INVALID_PARAMS);
  3791. hci_dev_lock(hdev);
  3792. if (!hdev_is_powered(hdev) || !lmp_sc_capable(hdev) ||
  3793. !hci_dev_test_flag(hdev, HCI_BREDR_ENABLED)) {
  3794. bool changed;
  3795. if (cp->val) {
  3796. changed = !hci_dev_test_and_set_flag(hdev,
  3797. HCI_SC_ENABLED);
  3798. if (cp->val == 0x02)
  3799. hci_dev_set_flag(hdev, HCI_SC_ONLY);
  3800. else
  3801. hci_dev_clear_flag(hdev, HCI_SC_ONLY);
  3802. } else {
  3803. changed = hci_dev_test_and_clear_flag(hdev,
  3804. HCI_SC_ENABLED);
  3805. hci_dev_clear_flag(hdev, HCI_SC_ONLY);
  3806. }
  3807. err = send_settings_rsp(sk, MGMT_OP_SET_SECURE_CONN, hdev);
  3808. if (err < 0)
  3809. goto failed;
  3810. if (changed)
  3811. err = new_settings(hdev, sk);
  3812. goto failed;
  3813. }
  3814. if (pending_find(MGMT_OP_SET_SECURE_CONN, hdev)) {
  3815. err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SECURE_CONN,
  3816. MGMT_STATUS_BUSY);
  3817. goto failed;
  3818. }
  3819. val = !!cp->val;
  3820. if (val == hci_dev_test_flag(hdev, HCI_SC_ENABLED) &&
  3821. (cp->val == 0x02) == hci_dev_test_flag(hdev, HCI_SC_ONLY)) {
  3822. err = send_settings_rsp(sk, MGMT_OP_SET_SECURE_CONN, hdev);
  3823. goto failed;
  3824. }
  3825. cmd = mgmt_pending_add(sk, MGMT_OP_SET_SECURE_CONN, hdev, data, len);
  3826. if (!cmd) {
  3827. err = -ENOMEM;
  3828. goto failed;
  3829. }
  3830. hci_req_init(&req, hdev);
  3831. hci_req_add(&req, HCI_OP_WRITE_SC_SUPPORT, 1, &val);
  3832. err = hci_req_run(&req, sc_enable_complete);
  3833. if (err < 0) {
  3834. mgmt_pending_remove(cmd);
  3835. goto failed;
  3836. }
  3837. failed:
  3838. hci_dev_unlock(hdev);
  3839. return err;
  3840. }
  3841. static int set_debug_keys(struct sock *sk, struct hci_dev *hdev,
  3842. void *data, u16 len)
  3843. {
  3844. struct mgmt_mode *cp = data;
  3845. bool changed, use_changed;
  3846. int err;
  3847. BT_DBG("request for %s", hdev->name);
  3848. if (cp->val != 0x00 && cp->val != 0x01 && cp->val != 0x02)
  3849. return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DEBUG_KEYS,
  3850. MGMT_STATUS_INVALID_PARAMS);
  3851. hci_dev_lock(hdev);
  3852. if (cp->val)
  3853. changed = !hci_dev_test_and_set_flag(hdev, HCI_KEEP_DEBUG_KEYS);
  3854. else
  3855. changed = hci_dev_test_and_clear_flag(hdev,
  3856. HCI_KEEP_DEBUG_KEYS);
  3857. if (cp->val == 0x02)
  3858. use_changed = !hci_dev_test_and_set_flag(hdev,
  3859. HCI_USE_DEBUG_KEYS);
  3860. else
  3861. use_changed = hci_dev_test_and_clear_flag(hdev,
  3862. HCI_USE_DEBUG_KEYS);
  3863. if (hdev_is_powered(hdev) && use_changed &&
  3864. hci_dev_test_flag(hdev, HCI_SSP_ENABLED)) {
  3865. u8 mode = (cp->val == 0x02) ? 0x01 : 0x00;
  3866. hci_send_cmd(hdev, HCI_OP_WRITE_SSP_DEBUG_MODE,
  3867. sizeof(mode), &mode);
  3868. }
  3869. err = send_settings_rsp(sk, MGMT_OP_SET_DEBUG_KEYS, hdev);
  3870. if (err < 0)
  3871. goto unlock;
  3872. if (changed)
  3873. err = new_settings(hdev, sk);
  3874. unlock:
  3875. hci_dev_unlock(hdev);
  3876. return err;
  3877. }
  3878. static int set_privacy(struct sock *sk, struct hci_dev *hdev, void *cp_data,
  3879. u16 len)
  3880. {
  3881. struct mgmt_cp_set_privacy *cp = cp_data;
  3882. bool changed;
  3883. int err;
  3884. BT_DBG("request for %s", hdev->name);
  3885. if (!lmp_le_capable(hdev))
  3886. return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_PRIVACY,
  3887. MGMT_STATUS_NOT_SUPPORTED);
  3888. if (cp->privacy != 0x00 && cp->privacy != 0x01 && cp->privacy != 0x02)
  3889. return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_PRIVACY,
  3890. MGMT_STATUS_INVALID_PARAMS);
  3891. if (hdev_is_powered(hdev))
  3892. return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_PRIVACY,
  3893. MGMT_STATUS_REJECTED);
  3894. hci_dev_lock(hdev);
  3895. /* If user space supports this command it is also expected to
  3896. * handle IRKs. Therefore, set the HCI_RPA_RESOLVING flag.
  3897. */
  3898. hci_dev_set_flag(hdev, HCI_RPA_RESOLVING);
  3899. if (cp->privacy) {
  3900. changed = !hci_dev_test_and_set_flag(hdev, HCI_PRIVACY);
  3901. memcpy(hdev->irk, cp->irk, sizeof(hdev->irk));
  3902. hci_dev_set_flag(hdev, HCI_RPA_EXPIRED);
  3903. hci_adv_instances_set_rpa_expired(hdev, true);
  3904. if (cp->privacy == 0x02)
  3905. hci_dev_set_flag(hdev, HCI_LIMITED_PRIVACY);
  3906. else
  3907. hci_dev_clear_flag(hdev, HCI_LIMITED_PRIVACY);
  3908. } else {
  3909. changed = hci_dev_test_and_clear_flag(hdev, HCI_PRIVACY);
  3910. memset(hdev->irk, 0, sizeof(hdev->irk));
  3911. hci_dev_clear_flag(hdev, HCI_RPA_EXPIRED);
  3912. hci_adv_instances_set_rpa_expired(hdev, false);
  3913. hci_dev_clear_flag(hdev, HCI_LIMITED_PRIVACY);
  3914. }
  3915. err = send_settings_rsp(sk, MGMT_OP_SET_PRIVACY, hdev);
  3916. if (err < 0)
  3917. goto unlock;
  3918. if (changed)
  3919. err = new_settings(hdev, sk);
  3920. unlock:
  3921. hci_dev_unlock(hdev);
  3922. return err;
  3923. }
  3924. static bool irk_is_valid(struct mgmt_irk_info *irk)
  3925. {
  3926. switch (irk->addr.type) {
  3927. case BDADDR_LE_PUBLIC:
  3928. return true;
  3929. case BDADDR_LE_RANDOM:
  3930. /* Two most significant bits shall be set */
  3931. if ((irk->addr.bdaddr.b[5] & 0xc0) != 0xc0)
  3932. return false;
  3933. return true;
  3934. }
  3935. return false;
  3936. }
  3937. static int load_irks(struct sock *sk, struct hci_dev *hdev, void *cp_data,
  3938. u16 len)
  3939. {
  3940. struct mgmt_cp_load_irks *cp = cp_data;
  3941. const u16 max_irk_count = ((U16_MAX - sizeof(*cp)) /
  3942. sizeof(struct mgmt_irk_info));
  3943. u16 irk_count, expected_len;
  3944. int i, err;
  3945. BT_DBG("request for %s", hdev->name);
  3946. if (!lmp_le_capable(hdev))
  3947. return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_IRKS,
  3948. MGMT_STATUS_NOT_SUPPORTED);
  3949. irk_count = __le16_to_cpu(cp->irk_count);
  3950. if (irk_count > max_irk_count) {
  3951. bt_dev_err(hdev, "load_irks: too big irk_count value %u",
  3952. irk_count);
  3953. return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_IRKS,
  3954. MGMT_STATUS_INVALID_PARAMS);
  3955. }
  3956. expected_len = sizeof(*cp) + irk_count * sizeof(struct mgmt_irk_info);
  3957. if (expected_len != len) {
  3958. bt_dev_err(hdev, "load_irks: expected %u bytes, got %u bytes",
  3959. expected_len, len);
  3960. return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_IRKS,
  3961. MGMT_STATUS_INVALID_PARAMS);
  3962. }
  3963. BT_DBG("%s irk_count %u", hdev->name, irk_count);
  3964. for (i = 0; i < irk_count; i++) {
  3965. struct mgmt_irk_info *key = &cp->irks[i];
  3966. if (!irk_is_valid(key))
  3967. return mgmt_cmd_status(sk, hdev->id,
  3968. MGMT_OP_LOAD_IRKS,
  3969. MGMT_STATUS_INVALID_PARAMS);
  3970. }
  3971. hci_dev_lock(hdev);
  3972. hci_smp_irks_clear(hdev);
  3973. for (i = 0; i < irk_count; i++) {
  3974. struct mgmt_irk_info *irk = &cp->irks[i];
  3975. hci_add_irk(hdev, &irk->addr.bdaddr,
  3976. le_addr_type(irk->addr.type), irk->val,
  3977. BDADDR_ANY);
  3978. }
  3979. hci_dev_set_flag(hdev, HCI_RPA_RESOLVING);
  3980. err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_LOAD_IRKS, 0, NULL, 0);
  3981. hci_dev_unlock(hdev);
  3982. return err;
  3983. }
  3984. static bool ltk_is_valid(struct mgmt_ltk_info *key)
  3985. {
  3986. if (key->master != 0x00 && key->master != 0x01)
  3987. return false;
  3988. switch (key->addr.type) {
  3989. case BDADDR_LE_PUBLIC:
  3990. return true;
  3991. case BDADDR_LE_RANDOM:
  3992. /* Two most significant bits shall be set */
  3993. if ((key->addr.bdaddr.b[5] & 0xc0) != 0xc0)
  3994. return false;
  3995. return true;
  3996. }
  3997. return false;
  3998. }
  3999. static int load_long_term_keys(struct sock *sk, struct hci_dev *hdev,
  4000. void *cp_data, u16 len)
  4001. {
  4002. struct mgmt_cp_load_long_term_keys *cp = cp_data;
  4003. const u16 max_key_count = ((U16_MAX - sizeof(*cp)) /
  4004. sizeof(struct mgmt_ltk_info));
  4005. u16 key_count, expected_len;
  4006. int i, err;
  4007. BT_DBG("request for %s", hdev->name);
  4008. if (!lmp_le_capable(hdev))
  4009. return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_LONG_TERM_KEYS,
  4010. MGMT_STATUS_NOT_SUPPORTED);
  4011. key_count = __le16_to_cpu(cp->key_count);
  4012. if (key_count > max_key_count) {
  4013. bt_dev_err(hdev, "load_ltks: too big key_count value %u",
  4014. key_count);
  4015. return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_LONG_TERM_KEYS,
  4016. MGMT_STATUS_INVALID_PARAMS);
  4017. }
  4018. expected_len = sizeof(*cp) + key_count *
  4019. sizeof(struct mgmt_ltk_info);
  4020. if (expected_len != len) {
  4021. bt_dev_err(hdev, "load_keys: expected %u bytes, got %u bytes",
  4022. expected_len, len);
  4023. return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_LONG_TERM_KEYS,
  4024. MGMT_STATUS_INVALID_PARAMS);
  4025. }
  4026. BT_DBG("%s key_count %u", hdev->name, key_count);
  4027. for (i = 0; i < key_count; i++) {
  4028. struct mgmt_ltk_info *key = &cp->keys[i];
  4029. if (!ltk_is_valid(key))
  4030. return mgmt_cmd_status(sk, hdev->id,
  4031. MGMT_OP_LOAD_LONG_TERM_KEYS,
  4032. MGMT_STATUS_INVALID_PARAMS);
  4033. }
  4034. hci_dev_lock(hdev);
  4035. hci_smp_ltks_clear(hdev);
  4036. for (i = 0; i < key_count; i++) {
  4037. struct mgmt_ltk_info *key = &cp->keys[i];
  4038. u8 type, authenticated;
  4039. switch (key->type) {
  4040. case MGMT_LTK_UNAUTHENTICATED:
  4041. authenticated = 0x00;
  4042. type = key->master ? SMP_LTK : SMP_LTK_SLAVE;
  4043. break;
  4044. case MGMT_LTK_AUTHENTICATED:
  4045. authenticated = 0x01;
  4046. type = key->master ? SMP_LTK : SMP_LTK_SLAVE;
  4047. break;
  4048. case MGMT_LTK_P256_UNAUTH:
  4049. authenticated = 0x00;
  4050. type = SMP_LTK_P256;
  4051. break;
  4052. case MGMT_LTK_P256_AUTH:
  4053. authenticated = 0x01;
  4054. type = SMP_LTK_P256;
  4055. break;
  4056. case MGMT_LTK_P256_DEBUG:
  4057. authenticated = 0x00;
  4058. type = SMP_LTK_P256_DEBUG;
  4059. /* fall through */
  4060. default:
  4061. continue;
  4062. }
  4063. hci_add_ltk(hdev, &key->addr.bdaddr,
  4064. le_addr_type(key->addr.type), type, authenticated,
  4065. key->val, key->enc_size, key->ediv, key->rand);
  4066. }
  4067. err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_LOAD_LONG_TERM_KEYS, 0,
  4068. NULL, 0);
  4069. hci_dev_unlock(hdev);
  4070. return err;
  4071. }
  4072. static int conn_info_cmd_complete(struct mgmt_pending_cmd *cmd, u8 status)
  4073. {
  4074. struct hci_conn *conn = cmd->user_data;
  4075. struct mgmt_rp_get_conn_info rp;
  4076. int err;
  4077. memcpy(&rp.addr, cmd->param, sizeof(rp.addr));
  4078. if (status == MGMT_STATUS_SUCCESS) {
  4079. rp.rssi = conn->rssi;
  4080. rp.tx_power = conn->tx_power;
  4081. rp.max_tx_power = conn->max_tx_power;
  4082. } else {
  4083. rp.rssi = HCI_RSSI_INVALID;
  4084. rp.tx_power = HCI_TX_POWER_INVALID;
  4085. rp.max_tx_power = HCI_TX_POWER_INVALID;
  4086. }
  4087. err = mgmt_cmd_complete(cmd->sk, cmd->index, MGMT_OP_GET_CONN_INFO,
  4088. status, &rp, sizeof(rp));
  4089. hci_conn_drop(conn);
  4090. hci_conn_put(conn);
  4091. return err;
  4092. }
  4093. static void conn_info_refresh_complete(struct hci_dev *hdev, u8 hci_status,
  4094. u16 opcode)
  4095. {
  4096. struct hci_cp_read_rssi *cp;
  4097. struct mgmt_pending_cmd *cmd;
  4098. struct hci_conn *conn;
  4099. u16 handle;
  4100. u8 status;
  4101. BT_DBG("status 0x%02x", hci_status);
  4102. hci_dev_lock(hdev);
  4103. /* Commands sent in request are either Read RSSI or Read Transmit Power
  4104. * Level so we check which one was last sent to retrieve connection
  4105. * handle. Both commands have handle as first parameter so it's safe to
  4106. * cast data on the same command struct.
  4107. *
  4108. * First command sent is always Read RSSI and we fail only if it fails.
  4109. * In other case we simply override error to indicate success as we
  4110. * already remembered if TX power value is actually valid.
  4111. */
  4112. cp = hci_sent_cmd_data(hdev, HCI_OP_READ_RSSI);
  4113. if (!cp) {
  4114. cp = hci_sent_cmd_data(hdev, HCI_OP_READ_TX_POWER);
  4115. status = MGMT_STATUS_SUCCESS;
  4116. } else {
  4117. status = mgmt_status(hci_status);
  4118. }
  4119. if (!cp) {
  4120. bt_dev_err(hdev, "invalid sent_cmd in conn_info response");
  4121. goto unlock;
  4122. }
  4123. handle = __le16_to_cpu(cp->handle);
  4124. conn = hci_conn_hash_lookup_handle(hdev, handle);
  4125. if (!conn) {
  4126. bt_dev_err(hdev, "unknown handle (%d) in conn_info response",
  4127. handle);
  4128. goto unlock;
  4129. }
  4130. cmd = pending_find_data(MGMT_OP_GET_CONN_INFO, hdev, conn);
  4131. if (!cmd)
  4132. goto unlock;
  4133. cmd->cmd_complete(cmd, status);
  4134. mgmt_pending_remove(cmd);
  4135. unlock:
  4136. hci_dev_unlock(hdev);
  4137. }
  4138. static int get_conn_info(struct sock *sk, struct hci_dev *hdev, void *data,
  4139. u16 len)
  4140. {
  4141. struct mgmt_cp_get_conn_info *cp = data;
  4142. struct mgmt_rp_get_conn_info rp;
  4143. struct hci_conn *conn;
  4144. unsigned long conn_info_age;
  4145. int err = 0;
  4146. BT_DBG("%s", hdev->name);
  4147. memset(&rp, 0, sizeof(rp));
  4148. bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr);
  4149. rp.addr.type = cp->addr.type;
  4150. if (!bdaddr_type_is_valid(cp->addr.type))
  4151. return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_GET_CONN_INFO,
  4152. MGMT_STATUS_INVALID_PARAMS,
  4153. &rp, sizeof(rp));
  4154. hci_dev_lock(hdev);
  4155. if (!hdev_is_powered(hdev)) {
  4156. err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_GET_CONN_INFO,
  4157. MGMT_STATUS_NOT_POWERED, &rp,
  4158. sizeof(rp));
  4159. goto unlock;
  4160. }
  4161. if (cp->addr.type == BDADDR_BREDR)
  4162. conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK,
  4163. &cp->addr.bdaddr);
  4164. else
  4165. conn = hci_conn_hash_lookup_ba(hdev, LE_LINK, &cp->addr.bdaddr);
  4166. if (!conn || conn->state != BT_CONNECTED) {
  4167. err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_GET_CONN_INFO,
  4168. MGMT_STATUS_NOT_CONNECTED, &rp,
  4169. sizeof(rp));
  4170. goto unlock;
  4171. }
  4172. if (pending_find_data(MGMT_OP_GET_CONN_INFO, hdev, conn)) {
  4173. err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_GET_CONN_INFO,
  4174. MGMT_STATUS_BUSY, &rp, sizeof(rp));
  4175. goto unlock;
  4176. }
  4177. /* To avoid client trying to guess when to poll again for information we
  4178. * calculate conn info age as random value between min/max set in hdev.
  4179. */
  4180. conn_info_age = hdev->conn_info_min_age +
  4181. prandom_u32_max(hdev->conn_info_max_age -
  4182. hdev->conn_info_min_age);
  4183. /* Query controller to refresh cached values if they are too old or were
  4184. * never read.
  4185. */
  4186. if (time_after(jiffies, conn->conn_info_timestamp +
  4187. msecs_to_jiffies(conn_info_age)) ||
  4188. !conn->conn_info_timestamp) {
  4189. struct hci_request req;
  4190. struct hci_cp_read_tx_power req_txp_cp;
  4191. struct hci_cp_read_rssi req_rssi_cp;
  4192. struct mgmt_pending_cmd *cmd;
  4193. hci_req_init(&req, hdev);
  4194. req_rssi_cp.handle = cpu_to_le16(conn->handle);
  4195. hci_req_add(&req, HCI_OP_READ_RSSI, sizeof(req_rssi_cp),
  4196. &req_rssi_cp);
  4197. /* For LE links TX power does not change thus we don't need to
  4198. * query for it once value is known.
  4199. */
  4200. if (!bdaddr_type_is_le(cp->addr.type) ||
  4201. conn->tx_power == HCI_TX_POWER_INVALID) {
  4202. req_txp_cp.handle = cpu_to_le16(conn->handle);
  4203. req_txp_cp.type = 0x00;
  4204. hci_req_add(&req, HCI_OP_READ_TX_POWER,
  4205. sizeof(req_txp_cp), &req_txp_cp);
  4206. }
  4207. /* Max TX power needs to be read only once per connection */
  4208. if (conn->max_tx_power == HCI_TX_POWER_INVALID) {
  4209. req_txp_cp.handle = cpu_to_le16(conn->handle);
  4210. req_txp_cp.type = 0x01;
  4211. hci_req_add(&req, HCI_OP_READ_TX_POWER,
  4212. sizeof(req_txp_cp), &req_txp_cp);
  4213. }
  4214. err = hci_req_run(&req, conn_info_refresh_complete);
  4215. if (err < 0)
  4216. goto unlock;
  4217. cmd = mgmt_pending_add(sk, MGMT_OP_GET_CONN_INFO, hdev,
  4218. data, len);
  4219. if (!cmd) {
  4220. err = -ENOMEM;
  4221. goto unlock;
  4222. }
  4223. hci_conn_hold(conn);
  4224. cmd->user_data = hci_conn_get(conn);
  4225. cmd->cmd_complete = conn_info_cmd_complete;
  4226. conn->conn_info_timestamp = jiffies;
  4227. } else {
  4228. /* Cache is valid, just reply with values cached in hci_conn */
  4229. rp.rssi = conn->rssi;
  4230. rp.tx_power = conn->tx_power;
  4231. rp.max_tx_power = conn->max_tx_power;
  4232. err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_GET_CONN_INFO,
  4233. MGMT_STATUS_SUCCESS, &rp, sizeof(rp));
  4234. }
  4235. unlock:
  4236. hci_dev_unlock(hdev);
  4237. return err;
  4238. }
  4239. static int clock_info_cmd_complete(struct mgmt_pending_cmd *cmd, u8 status)
  4240. {
  4241. struct hci_conn *conn = cmd->user_data;
  4242. struct mgmt_rp_get_clock_info rp;
  4243. struct hci_dev *hdev;
  4244. int err;
  4245. memset(&rp, 0, sizeof(rp));
  4246. memcpy(&rp.addr, cmd->param, sizeof(rp.addr));
  4247. if (status)
  4248. goto complete;
  4249. hdev = hci_dev_get(cmd->index);
  4250. if (hdev) {
  4251. rp.local_clock = cpu_to_le32(hdev->clock);
  4252. hci_dev_put(hdev);
  4253. }
  4254. if (conn) {
  4255. rp.piconet_clock = cpu_to_le32(conn->clock);
  4256. rp.accuracy = cpu_to_le16(conn->clock_accuracy);
  4257. }
  4258. complete:
  4259. err = mgmt_cmd_complete(cmd->sk, cmd->index, cmd->opcode, status, &rp,
  4260. sizeof(rp));
  4261. if (conn) {
  4262. hci_conn_drop(conn);
  4263. hci_conn_put(conn);
  4264. }
  4265. return err;
  4266. }
  4267. static void get_clock_info_complete(struct hci_dev *hdev, u8 status, u16 opcode)
  4268. {
  4269. struct hci_cp_read_clock *hci_cp;
  4270. struct mgmt_pending_cmd *cmd;
  4271. struct hci_conn *conn;
  4272. BT_DBG("%s status %u", hdev->name, status);
  4273. hci_dev_lock(hdev);
  4274. hci_cp = hci_sent_cmd_data(hdev, HCI_OP_READ_CLOCK);
  4275. if (!hci_cp)
  4276. goto unlock;
  4277. if (hci_cp->which) {
  4278. u16 handle = __le16_to_cpu(hci_cp->handle);
  4279. conn = hci_conn_hash_lookup_handle(hdev, handle);
  4280. } else {
  4281. conn = NULL;
  4282. }
  4283. cmd = pending_find_data(MGMT_OP_GET_CLOCK_INFO, hdev, conn);
  4284. if (!cmd)
  4285. goto unlock;
  4286. cmd->cmd_complete(cmd, mgmt_status(status));
  4287. mgmt_pending_remove(cmd);
  4288. unlock:
  4289. hci_dev_unlock(hdev);
  4290. }
  4291. static int get_clock_info(struct sock *sk, struct hci_dev *hdev, void *data,
  4292. u16 len)
  4293. {
  4294. struct mgmt_cp_get_clock_info *cp = data;
  4295. struct mgmt_rp_get_clock_info rp;
  4296. struct hci_cp_read_clock hci_cp;
  4297. struct mgmt_pending_cmd *cmd;
  4298. struct hci_request req;
  4299. struct hci_conn *conn;
  4300. int err;
  4301. BT_DBG("%s", hdev->name);
  4302. memset(&rp, 0, sizeof(rp));
  4303. bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr);
  4304. rp.addr.type = cp->addr.type;
  4305. if (cp->addr.type != BDADDR_BREDR)
  4306. return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_GET_CLOCK_INFO,
  4307. MGMT_STATUS_INVALID_PARAMS,
  4308. &rp, sizeof(rp));
  4309. hci_dev_lock(hdev);
  4310. if (!hdev_is_powered(hdev)) {
  4311. err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_GET_CLOCK_INFO,
  4312. MGMT_STATUS_NOT_POWERED, &rp,
  4313. sizeof(rp));
  4314. goto unlock;
  4315. }
  4316. if (bacmp(&cp->addr.bdaddr, BDADDR_ANY)) {
  4317. conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK,
  4318. &cp->addr.bdaddr);
  4319. if (!conn || conn->state != BT_CONNECTED) {
  4320. err = mgmt_cmd_complete(sk, hdev->id,
  4321. MGMT_OP_GET_CLOCK_INFO,
  4322. MGMT_STATUS_NOT_CONNECTED,
  4323. &rp, sizeof(rp));
  4324. goto unlock;
  4325. }
  4326. } else {
  4327. conn = NULL;
  4328. }
  4329. cmd = mgmt_pending_add(sk, MGMT_OP_GET_CLOCK_INFO, hdev, data, len);
  4330. if (!cmd) {
  4331. err = -ENOMEM;
  4332. goto unlock;
  4333. }
  4334. cmd->cmd_complete = clock_info_cmd_complete;
  4335. hci_req_init(&req, hdev);
  4336. memset(&hci_cp, 0, sizeof(hci_cp));
  4337. hci_req_add(&req, HCI_OP_READ_CLOCK, sizeof(hci_cp), &hci_cp);
  4338. if (conn) {
  4339. hci_conn_hold(conn);
  4340. cmd->user_data = hci_conn_get(conn);
  4341. hci_cp.handle = cpu_to_le16(conn->handle);
  4342. hci_cp.which = 0x01; /* Piconet clock */
  4343. hci_req_add(&req, HCI_OP_READ_CLOCK, sizeof(hci_cp), &hci_cp);
  4344. }
  4345. err = hci_req_run(&req, get_clock_info_complete);
  4346. if (err < 0)
  4347. mgmt_pending_remove(cmd);
  4348. unlock:
  4349. hci_dev_unlock(hdev);
  4350. return err;
  4351. }
  4352. static bool is_connected(struct hci_dev *hdev, bdaddr_t *addr, u8 type)
  4353. {
  4354. struct hci_conn *conn;
  4355. conn = hci_conn_hash_lookup_ba(hdev, LE_LINK, addr);
  4356. if (!conn)
  4357. return false;
  4358. if (conn->dst_type != type)
  4359. return false;
  4360. if (conn->state != BT_CONNECTED)
  4361. return false;
  4362. return true;
  4363. }
  4364. /* This function requires the caller holds hdev->lock */
  4365. static int hci_conn_params_set(struct hci_dev *hdev, bdaddr_t *addr,
  4366. u8 addr_type, u8 auto_connect)
  4367. {
  4368. struct hci_conn_params *params;
  4369. params = hci_conn_params_add(hdev, addr, addr_type);
  4370. if (!params)
  4371. return -EIO;
  4372. if (params->auto_connect == auto_connect)
  4373. return 0;
  4374. list_del_init(&params->action);
  4375. switch (auto_connect) {
  4376. case HCI_AUTO_CONN_DISABLED:
  4377. case HCI_AUTO_CONN_LINK_LOSS:
  4378. /* If auto connect is being disabled when we're trying to
  4379. * connect to device, keep connecting.
  4380. */
  4381. if (params->explicit_connect)
  4382. list_add(&params->action, &hdev->pend_le_conns);
  4383. break;
  4384. case HCI_AUTO_CONN_REPORT:
  4385. if (params->explicit_connect)
  4386. list_add(&params->action, &hdev->pend_le_conns);
  4387. else
  4388. list_add(&params->action, &hdev->pend_le_reports);
  4389. break;
  4390. case HCI_AUTO_CONN_DIRECT:
  4391. case HCI_AUTO_CONN_ALWAYS:
  4392. if (!is_connected(hdev, addr, addr_type))
  4393. list_add(&params->action, &hdev->pend_le_conns);
  4394. break;
  4395. }
  4396. params->auto_connect = auto_connect;
  4397. BT_DBG("addr %pMR (type %u) auto_connect %u", addr, addr_type,
  4398. auto_connect);
  4399. return 0;
  4400. }
  4401. static void device_added(struct sock *sk, struct hci_dev *hdev,
  4402. bdaddr_t *bdaddr, u8 type, u8 action)
  4403. {
  4404. struct mgmt_ev_device_added ev;
  4405. bacpy(&ev.addr.bdaddr, bdaddr);
  4406. ev.addr.type = type;
  4407. ev.action = action;
  4408. mgmt_event(MGMT_EV_DEVICE_ADDED, hdev, &ev, sizeof(ev), sk);
  4409. }
  4410. static int add_device(struct sock *sk, struct hci_dev *hdev,
  4411. void *data, u16 len)
  4412. {
  4413. struct mgmt_cp_add_device *cp = data;
  4414. u8 auto_conn, addr_type;
  4415. int err;
  4416. BT_DBG("%s", hdev->name);
  4417. if (!bdaddr_type_is_valid(cp->addr.type) ||
  4418. !bacmp(&cp->addr.bdaddr, BDADDR_ANY))
  4419. return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_ADD_DEVICE,
  4420. MGMT_STATUS_INVALID_PARAMS,
  4421. &cp->addr, sizeof(cp->addr));
  4422. if (cp->action != 0x00 && cp->action != 0x01 && cp->action != 0x02)
  4423. return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_ADD_DEVICE,
  4424. MGMT_STATUS_INVALID_PARAMS,
  4425. &cp->addr, sizeof(cp->addr));
  4426. hci_dev_lock(hdev);
  4427. if (cp->addr.type == BDADDR_BREDR) {
  4428. /* Only incoming connections action is supported for now */
  4429. if (cp->action != 0x01) {
  4430. err = mgmt_cmd_complete(sk, hdev->id,
  4431. MGMT_OP_ADD_DEVICE,
  4432. MGMT_STATUS_INVALID_PARAMS,
  4433. &cp->addr, sizeof(cp->addr));
  4434. goto unlock;
  4435. }
  4436. err = hci_bdaddr_list_add(&hdev->whitelist, &cp->addr.bdaddr,
  4437. cp->addr.type);
  4438. if (err)
  4439. goto unlock;
  4440. hci_req_update_scan(hdev);
  4441. goto added;
  4442. }
  4443. addr_type = le_addr_type(cp->addr.type);
  4444. if (cp->action == 0x02)
  4445. auto_conn = HCI_AUTO_CONN_ALWAYS;
  4446. else if (cp->action == 0x01)
  4447. auto_conn = HCI_AUTO_CONN_DIRECT;
  4448. else
  4449. auto_conn = HCI_AUTO_CONN_REPORT;
  4450. /* Kernel internally uses conn_params with resolvable private
  4451. * address, but Add Device allows only identity addresses.
  4452. * Make sure it is enforced before calling
  4453. * hci_conn_params_lookup.
  4454. */
  4455. if (!hci_is_identity_address(&cp->addr.bdaddr, addr_type)) {
  4456. err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_ADD_DEVICE,
  4457. MGMT_STATUS_INVALID_PARAMS,
  4458. &cp->addr, sizeof(cp->addr));
  4459. goto unlock;
  4460. }
  4461. /* If the connection parameters don't exist for this device,
  4462. * they will be created and configured with defaults.
  4463. */
  4464. if (hci_conn_params_set(hdev, &cp->addr.bdaddr, addr_type,
  4465. auto_conn) < 0) {
  4466. err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_ADD_DEVICE,
  4467. MGMT_STATUS_FAILED, &cp->addr,
  4468. sizeof(cp->addr));
  4469. goto unlock;
  4470. }
  4471. hci_update_background_scan(hdev);
  4472. added:
  4473. device_added(sk, hdev, &cp->addr.bdaddr, cp->addr.type, cp->action);
  4474. err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_ADD_DEVICE,
  4475. MGMT_STATUS_SUCCESS, &cp->addr,
  4476. sizeof(cp->addr));
  4477. unlock:
  4478. hci_dev_unlock(hdev);
  4479. return err;
  4480. }
  4481. static void device_removed(struct sock *sk, struct hci_dev *hdev,
  4482. bdaddr_t *bdaddr, u8 type)
  4483. {
  4484. struct mgmt_ev_device_removed ev;
  4485. bacpy(&ev.addr.bdaddr, bdaddr);
  4486. ev.addr.type = type;
  4487. mgmt_event(MGMT_EV_DEVICE_REMOVED, hdev, &ev, sizeof(ev), sk);
  4488. }
  4489. static int remove_device(struct sock *sk, struct hci_dev *hdev,
  4490. void *data, u16 len)
  4491. {
  4492. struct mgmt_cp_remove_device *cp = data;
  4493. int err;
  4494. BT_DBG("%s", hdev->name);
  4495. hci_dev_lock(hdev);
  4496. if (bacmp(&cp->addr.bdaddr, BDADDR_ANY)) {
  4497. struct hci_conn_params *params;
  4498. u8 addr_type;
  4499. if (!bdaddr_type_is_valid(cp->addr.type)) {
  4500. err = mgmt_cmd_complete(sk, hdev->id,
  4501. MGMT_OP_REMOVE_DEVICE,
  4502. MGMT_STATUS_INVALID_PARAMS,
  4503. &cp->addr, sizeof(cp->addr));
  4504. goto unlock;
  4505. }
  4506. if (cp->addr.type == BDADDR_BREDR) {
  4507. err = hci_bdaddr_list_del(&hdev->whitelist,
  4508. &cp->addr.bdaddr,
  4509. cp->addr.type);
  4510. if (err) {
  4511. err = mgmt_cmd_complete(sk, hdev->id,
  4512. MGMT_OP_REMOVE_DEVICE,
  4513. MGMT_STATUS_INVALID_PARAMS,
  4514. &cp->addr,
  4515. sizeof(cp->addr));
  4516. goto unlock;
  4517. }
  4518. hci_req_update_scan(hdev);
  4519. device_removed(sk, hdev, &cp->addr.bdaddr,
  4520. cp->addr.type);
  4521. goto complete;
  4522. }
  4523. addr_type = le_addr_type(cp->addr.type);
  4524. /* Kernel internally uses conn_params with resolvable private
  4525. * address, but Remove Device allows only identity addresses.
  4526. * Make sure it is enforced before calling
  4527. * hci_conn_params_lookup.
  4528. */
  4529. if (!hci_is_identity_address(&cp->addr.bdaddr, addr_type)) {
  4530. err = mgmt_cmd_complete(sk, hdev->id,
  4531. MGMT_OP_REMOVE_DEVICE,
  4532. MGMT_STATUS_INVALID_PARAMS,
  4533. &cp->addr, sizeof(cp->addr));
  4534. goto unlock;
  4535. }
  4536. params = hci_conn_params_lookup(hdev, &cp->addr.bdaddr,
  4537. addr_type);
  4538. if (!params) {
  4539. err = mgmt_cmd_complete(sk, hdev->id,
  4540. MGMT_OP_REMOVE_DEVICE,
  4541. MGMT_STATUS_INVALID_PARAMS,
  4542. &cp->addr, sizeof(cp->addr));
  4543. goto unlock;
  4544. }
  4545. if (params->auto_connect == HCI_AUTO_CONN_DISABLED ||
  4546. params->auto_connect == HCI_AUTO_CONN_EXPLICIT) {
  4547. err = mgmt_cmd_complete(sk, hdev->id,
  4548. MGMT_OP_REMOVE_DEVICE,
  4549. MGMT_STATUS_INVALID_PARAMS,
  4550. &cp->addr, sizeof(cp->addr));
  4551. goto unlock;
  4552. }
  4553. list_del(&params->action);
  4554. list_del(&params->list);
  4555. kfree(params);
  4556. hci_update_background_scan(hdev);
  4557. device_removed(sk, hdev, &cp->addr.bdaddr, cp->addr.type);
  4558. } else {
  4559. struct hci_conn_params *p, *tmp;
  4560. struct bdaddr_list *b, *btmp;
  4561. if (cp->addr.type) {
  4562. err = mgmt_cmd_complete(sk, hdev->id,
  4563. MGMT_OP_REMOVE_DEVICE,
  4564. MGMT_STATUS_INVALID_PARAMS,
  4565. &cp->addr, sizeof(cp->addr));
  4566. goto unlock;
  4567. }
  4568. list_for_each_entry_safe(b, btmp, &hdev->whitelist, list) {
  4569. device_removed(sk, hdev, &b->bdaddr, b->bdaddr_type);
  4570. list_del(&b->list);
  4571. kfree(b);
  4572. }
  4573. hci_req_update_scan(hdev);
  4574. list_for_each_entry_safe(p, tmp, &hdev->le_conn_params, list) {
  4575. if (p->auto_connect == HCI_AUTO_CONN_DISABLED)
  4576. continue;
  4577. device_removed(sk, hdev, &p->addr, p->addr_type);
  4578. if (p->explicit_connect) {
  4579. p->auto_connect = HCI_AUTO_CONN_EXPLICIT;
  4580. continue;
  4581. }
  4582. list_del(&p->action);
  4583. list_del(&p->list);
  4584. kfree(p);
  4585. }
  4586. BT_DBG("All LE connection parameters were removed");
  4587. hci_update_background_scan(hdev);
  4588. }
  4589. complete:
  4590. err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_REMOVE_DEVICE,
  4591. MGMT_STATUS_SUCCESS, &cp->addr,
  4592. sizeof(cp->addr));
  4593. unlock:
  4594. hci_dev_unlock(hdev);
  4595. return err;
  4596. }
  4597. static int load_conn_param(struct sock *sk, struct hci_dev *hdev, void *data,
  4598. u16 len)
  4599. {
  4600. struct mgmt_cp_load_conn_param *cp = data;
  4601. const u16 max_param_count = ((U16_MAX - sizeof(*cp)) /
  4602. sizeof(struct mgmt_conn_param));
  4603. u16 param_count, expected_len;
  4604. int i;
  4605. if (!lmp_le_capable(hdev))
  4606. return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_CONN_PARAM,
  4607. MGMT_STATUS_NOT_SUPPORTED);
  4608. param_count = __le16_to_cpu(cp->param_count);
  4609. if (param_count > max_param_count) {
  4610. bt_dev_err(hdev, "load_conn_param: too big param_count value %u",
  4611. param_count);
  4612. return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_CONN_PARAM,
  4613. MGMT_STATUS_INVALID_PARAMS);
  4614. }
  4615. expected_len = sizeof(*cp) + param_count *
  4616. sizeof(struct mgmt_conn_param);
  4617. if (expected_len != len) {
  4618. bt_dev_err(hdev, "load_conn_param: expected %u bytes, got %u bytes",
  4619. expected_len, len);
  4620. return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_CONN_PARAM,
  4621. MGMT_STATUS_INVALID_PARAMS);
  4622. }
  4623. BT_DBG("%s param_count %u", hdev->name, param_count);
  4624. hci_dev_lock(hdev);
  4625. hci_conn_params_clear_disabled(hdev);
  4626. for (i = 0; i < param_count; i++) {
  4627. struct mgmt_conn_param *param = &cp->params[i];
  4628. struct hci_conn_params *hci_param;
  4629. u16 min, max, latency, timeout;
  4630. u8 addr_type;
  4631. BT_DBG("Adding %pMR (type %u)", &param->addr.bdaddr,
  4632. param->addr.type);
  4633. if (param->addr.type == BDADDR_LE_PUBLIC) {
  4634. addr_type = ADDR_LE_DEV_PUBLIC;
  4635. } else if (param->addr.type == BDADDR_LE_RANDOM) {
  4636. addr_type = ADDR_LE_DEV_RANDOM;
  4637. } else {
  4638. bt_dev_err(hdev, "ignoring invalid connection parameters");
  4639. continue;
  4640. }
  4641. min = le16_to_cpu(param->min_interval);
  4642. max = le16_to_cpu(param->max_interval);
  4643. latency = le16_to_cpu(param->latency);
  4644. timeout = le16_to_cpu(param->timeout);
  4645. BT_DBG("min 0x%04x max 0x%04x latency 0x%04x timeout 0x%04x",
  4646. min, max, latency, timeout);
  4647. if (hci_check_conn_params(min, max, latency, timeout) < 0) {
  4648. bt_dev_err(hdev, "ignoring invalid connection parameters");
  4649. continue;
  4650. }
  4651. hci_param = hci_conn_params_add(hdev, &param->addr.bdaddr,
  4652. addr_type);
  4653. if (!hci_param) {
  4654. bt_dev_err(hdev, "failed to add connection parameters");
  4655. continue;
  4656. }
  4657. hci_param->conn_min_interval = min;
  4658. hci_param->conn_max_interval = max;
  4659. hci_param->conn_latency = latency;
  4660. hci_param->supervision_timeout = timeout;
  4661. }
  4662. hci_dev_unlock(hdev);
  4663. return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_LOAD_CONN_PARAM, 0,
  4664. NULL, 0);
  4665. }
  4666. static int set_external_config(struct sock *sk, struct hci_dev *hdev,
  4667. void *data, u16 len)
  4668. {
  4669. struct mgmt_cp_set_external_config *cp = data;
  4670. bool changed;
  4671. int err;
  4672. BT_DBG("%s", hdev->name);
  4673. if (hdev_is_powered(hdev))
  4674. return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_EXTERNAL_CONFIG,
  4675. MGMT_STATUS_REJECTED);
  4676. if (cp->config != 0x00 && cp->config != 0x01)
  4677. return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_EXTERNAL_CONFIG,
  4678. MGMT_STATUS_INVALID_PARAMS);
  4679. if (!test_bit(HCI_QUIRK_EXTERNAL_CONFIG, &hdev->quirks))
  4680. return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_EXTERNAL_CONFIG,
  4681. MGMT_STATUS_NOT_SUPPORTED);
  4682. hci_dev_lock(hdev);
  4683. if (cp->config)
  4684. changed = !hci_dev_test_and_set_flag(hdev, HCI_EXT_CONFIGURED);
  4685. else
  4686. changed = hci_dev_test_and_clear_flag(hdev, HCI_EXT_CONFIGURED);
  4687. err = send_options_rsp(sk, MGMT_OP_SET_EXTERNAL_CONFIG, hdev);
  4688. if (err < 0)
  4689. goto unlock;
  4690. if (!changed)
  4691. goto unlock;
  4692. err = new_options(hdev, sk);
  4693. if (hci_dev_test_flag(hdev, HCI_UNCONFIGURED) == is_configured(hdev)) {
  4694. mgmt_index_removed(hdev);
  4695. if (hci_dev_test_and_change_flag(hdev, HCI_UNCONFIGURED)) {
  4696. hci_dev_set_flag(hdev, HCI_CONFIG);
  4697. hci_dev_set_flag(hdev, HCI_AUTO_OFF);
  4698. queue_work(hdev->req_workqueue, &hdev->power_on);
  4699. } else {
  4700. set_bit(HCI_RAW, &hdev->flags);
  4701. mgmt_index_added(hdev);
  4702. }
  4703. }
  4704. unlock:
  4705. hci_dev_unlock(hdev);
  4706. return err;
  4707. }
  4708. static int set_public_address(struct sock *sk, struct hci_dev *hdev,
  4709. void *data, u16 len)
  4710. {
  4711. struct mgmt_cp_set_public_address *cp = data;
  4712. bool changed;
  4713. int err;
  4714. BT_DBG("%s", hdev->name);
  4715. if (hdev_is_powered(hdev))
  4716. return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_PUBLIC_ADDRESS,
  4717. MGMT_STATUS_REJECTED);
  4718. if (!bacmp(&cp->bdaddr, BDADDR_ANY))
  4719. return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_PUBLIC_ADDRESS,
  4720. MGMT_STATUS_INVALID_PARAMS);
  4721. if (!hdev->set_bdaddr)
  4722. return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_PUBLIC_ADDRESS,
  4723. MGMT_STATUS_NOT_SUPPORTED);
  4724. hci_dev_lock(hdev);
  4725. changed = !!bacmp(&hdev->public_addr, &cp->bdaddr);
  4726. bacpy(&hdev->public_addr, &cp->bdaddr);
  4727. err = send_options_rsp(sk, MGMT_OP_SET_PUBLIC_ADDRESS, hdev);
  4728. if (err < 0)
  4729. goto unlock;
  4730. if (!changed)
  4731. goto unlock;
  4732. if (hci_dev_test_flag(hdev, HCI_UNCONFIGURED))
  4733. err = new_options(hdev, sk);
  4734. if (is_configured(hdev)) {
  4735. mgmt_index_removed(hdev);
  4736. hci_dev_clear_flag(hdev, HCI_UNCONFIGURED);
  4737. hci_dev_set_flag(hdev, HCI_CONFIG);
  4738. hci_dev_set_flag(hdev, HCI_AUTO_OFF);
  4739. queue_work(hdev->req_workqueue, &hdev->power_on);
  4740. }
  4741. unlock:
  4742. hci_dev_unlock(hdev);
  4743. return err;
  4744. }
  4745. static void read_local_oob_ext_data_complete(struct hci_dev *hdev, u8 status,
  4746. u16 opcode, struct sk_buff *skb)
  4747. {
  4748. const struct mgmt_cp_read_local_oob_ext_data *mgmt_cp;
  4749. struct mgmt_rp_read_local_oob_ext_data *mgmt_rp;
  4750. u8 *h192, *r192, *h256, *r256;
  4751. struct mgmt_pending_cmd *cmd;
  4752. u16 eir_len;
  4753. int err;
  4754. BT_DBG("%s status %u", hdev->name, status);
  4755. cmd = pending_find(MGMT_OP_READ_LOCAL_OOB_EXT_DATA, hdev);
  4756. if (!cmd)
  4757. return;
  4758. mgmt_cp = cmd->param;
  4759. if (status) {
  4760. status = mgmt_status(status);
  4761. eir_len = 0;
  4762. h192 = NULL;
  4763. r192 = NULL;
  4764. h256 = NULL;
  4765. r256 = NULL;
  4766. } else if (opcode == HCI_OP_READ_LOCAL_OOB_DATA) {
  4767. struct hci_rp_read_local_oob_data *rp;
  4768. if (skb->len != sizeof(*rp)) {
  4769. status = MGMT_STATUS_FAILED;
  4770. eir_len = 0;
  4771. } else {
  4772. status = MGMT_STATUS_SUCCESS;
  4773. rp = (void *)skb->data;
  4774. eir_len = 5 + 18 + 18;
  4775. h192 = rp->hash;
  4776. r192 = rp->rand;
  4777. h256 = NULL;
  4778. r256 = NULL;
  4779. }
  4780. } else {
  4781. struct hci_rp_read_local_oob_ext_data *rp;
  4782. if (skb->len != sizeof(*rp)) {
  4783. status = MGMT_STATUS_FAILED;
  4784. eir_len = 0;
  4785. } else {
  4786. status = MGMT_STATUS_SUCCESS;
  4787. rp = (void *)skb->data;
  4788. if (hci_dev_test_flag(hdev, HCI_SC_ONLY)) {
  4789. eir_len = 5 + 18 + 18;
  4790. h192 = NULL;
  4791. r192 = NULL;
  4792. } else {
  4793. eir_len = 5 + 18 + 18 + 18 + 18;
  4794. h192 = rp->hash192;
  4795. r192 = rp->rand192;
  4796. }
  4797. h256 = rp->hash256;
  4798. r256 = rp->rand256;
  4799. }
  4800. }
  4801. mgmt_rp = kmalloc(sizeof(*mgmt_rp) + eir_len, GFP_KERNEL);
  4802. if (!mgmt_rp)
  4803. goto done;
  4804. if (status)
  4805. goto send_rsp;
  4806. eir_len = eir_append_data(mgmt_rp->eir, 0, EIR_CLASS_OF_DEV,
  4807. hdev->dev_class, 3);
  4808. if (h192 && r192) {
  4809. eir_len = eir_append_data(mgmt_rp->eir, eir_len,
  4810. EIR_SSP_HASH_C192, h192, 16);
  4811. eir_len = eir_append_data(mgmt_rp->eir, eir_len,
  4812. EIR_SSP_RAND_R192, r192, 16);
  4813. }
  4814. if (h256 && r256) {
  4815. eir_len = eir_append_data(mgmt_rp->eir, eir_len,
  4816. EIR_SSP_HASH_C256, h256, 16);
  4817. eir_len = eir_append_data(mgmt_rp->eir, eir_len,
  4818. EIR_SSP_RAND_R256, r256, 16);
  4819. }
  4820. send_rsp:
  4821. mgmt_rp->type = mgmt_cp->type;
  4822. mgmt_rp->eir_len = cpu_to_le16(eir_len);
  4823. err = mgmt_cmd_complete(cmd->sk, hdev->id,
  4824. MGMT_OP_READ_LOCAL_OOB_EXT_DATA, status,
  4825. mgmt_rp, sizeof(*mgmt_rp) + eir_len);
  4826. if (err < 0 || status)
  4827. goto done;
  4828. hci_sock_set_flag(cmd->sk, HCI_MGMT_OOB_DATA_EVENTS);
  4829. err = mgmt_limited_event(MGMT_EV_LOCAL_OOB_DATA_UPDATED, hdev,
  4830. mgmt_rp, sizeof(*mgmt_rp) + eir_len,
  4831. HCI_MGMT_OOB_DATA_EVENTS, cmd->sk);
  4832. done:
  4833. kfree(mgmt_rp);
  4834. mgmt_pending_remove(cmd);
  4835. }
  4836. static int read_local_ssp_oob_req(struct hci_dev *hdev, struct sock *sk,
  4837. struct mgmt_cp_read_local_oob_ext_data *cp)
  4838. {
  4839. struct mgmt_pending_cmd *cmd;
  4840. struct hci_request req;
  4841. int err;
  4842. cmd = mgmt_pending_add(sk, MGMT_OP_READ_LOCAL_OOB_EXT_DATA, hdev,
  4843. cp, sizeof(*cp));
  4844. if (!cmd)
  4845. return -ENOMEM;
  4846. hci_req_init(&req, hdev);
  4847. if (bredr_sc_enabled(hdev))
  4848. hci_req_add(&req, HCI_OP_READ_LOCAL_OOB_EXT_DATA, 0, NULL);
  4849. else
  4850. hci_req_add(&req, HCI_OP_READ_LOCAL_OOB_DATA, 0, NULL);
  4851. err = hci_req_run_skb(&req, read_local_oob_ext_data_complete);
  4852. if (err < 0) {
  4853. mgmt_pending_remove(cmd);
  4854. return err;
  4855. }
  4856. return 0;
  4857. }
  4858. static int read_local_oob_ext_data(struct sock *sk, struct hci_dev *hdev,
  4859. void *data, u16 data_len)
  4860. {
  4861. struct mgmt_cp_read_local_oob_ext_data *cp = data;
  4862. struct mgmt_rp_read_local_oob_ext_data *rp;
  4863. size_t rp_len;
  4864. u16 eir_len;
  4865. u8 status, flags, role, addr[7], hash[16], rand[16];
  4866. int err;
  4867. BT_DBG("%s", hdev->name);
  4868. if (hdev_is_powered(hdev)) {
  4869. switch (cp->type) {
  4870. case BIT(BDADDR_BREDR):
  4871. status = mgmt_bredr_support(hdev);
  4872. if (status)
  4873. eir_len = 0;
  4874. else
  4875. eir_len = 5;
  4876. break;
  4877. case (BIT(BDADDR_LE_PUBLIC) | BIT(BDADDR_LE_RANDOM)):
  4878. status = mgmt_le_support(hdev);
  4879. if (status)
  4880. eir_len = 0;
  4881. else
  4882. eir_len = 9 + 3 + 18 + 18 + 3;
  4883. break;
  4884. default:
  4885. status = MGMT_STATUS_INVALID_PARAMS;
  4886. eir_len = 0;
  4887. break;
  4888. }
  4889. } else {
  4890. status = MGMT_STATUS_NOT_POWERED;
  4891. eir_len = 0;
  4892. }
  4893. rp_len = sizeof(*rp) + eir_len;
  4894. rp = kmalloc(rp_len, GFP_ATOMIC);
  4895. if (!rp)
  4896. return -ENOMEM;
  4897. if (status)
  4898. goto complete;
  4899. hci_dev_lock(hdev);
  4900. eir_len = 0;
  4901. switch (cp->type) {
  4902. case BIT(BDADDR_BREDR):
  4903. if (hci_dev_test_flag(hdev, HCI_SSP_ENABLED)) {
  4904. err = read_local_ssp_oob_req(hdev, sk, cp);
  4905. hci_dev_unlock(hdev);
  4906. if (!err)
  4907. goto done;
  4908. status = MGMT_STATUS_FAILED;
  4909. goto complete;
  4910. } else {
  4911. eir_len = eir_append_data(rp->eir, eir_len,
  4912. EIR_CLASS_OF_DEV,
  4913. hdev->dev_class, 3);
  4914. }
  4915. break;
  4916. case (BIT(BDADDR_LE_PUBLIC) | BIT(BDADDR_LE_RANDOM)):
  4917. if (hci_dev_test_flag(hdev, HCI_SC_ENABLED) &&
  4918. smp_generate_oob(hdev, hash, rand) < 0) {
  4919. hci_dev_unlock(hdev);
  4920. status = MGMT_STATUS_FAILED;
  4921. goto complete;
  4922. }
  4923. /* This should return the active RPA, but since the RPA
  4924. * is only programmed on demand, it is really hard to fill
  4925. * this in at the moment. For now disallow retrieving
  4926. * local out-of-band data when privacy is in use.
  4927. *
  4928. * Returning the identity address will not help here since
  4929. * pairing happens before the identity resolving key is
  4930. * known and thus the connection establishment happens
  4931. * based on the RPA and not the identity address.
  4932. */
  4933. if (hci_dev_test_flag(hdev, HCI_PRIVACY)) {
  4934. hci_dev_unlock(hdev);
  4935. status = MGMT_STATUS_REJECTED;
  4936. goto complete;
  4937. }
  4938. if (hci_dev_test_flag(hdev, HCI_FORCE_STATIC_ADDR) ||
  4939. !bacmp(&hdev->bdaddr, BDADDR_ANY) ||
  4940. (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED) &&
  4941. bacmp(&hdev->static_addr, BDADDR_ANY))) {
  4942. memcpy(addr, &hdev->static_addr, 6);
  4943. addr[6] = 0x01;
  4944. } else {
  4945. memcpy(addr, &hdev->bdaddr, 6);
  4946. addr[6] = 0x00;
  4947. }
  4948. eir_len = eir_append_data(rp->eir, eir_len, EIR_LE_BDADDR,
  4949. addr, sizeof(addr));
  4950. if (hci_dev_test_flag(hdev, HCI_ADVERTISING))
  4951. role = 0x02;
  4952. else
  4953. role = 0x01;
  4954. eir_len = eir_append_data(rp->eir, eir_len, EIR_LE_ROLE,
  4955. &role, sizeof(role));
  4956. if (hci_dev_test_flag(hdev, HCI_SC_ENABLED)) {
  4957. eir_len = eir_append_data(rp->eir, eir_len,
  4958. EIR_LE_SC_CONFIRM,
  4959. hash, sizeof(hash));
  4960. eir_len = eir_append_data(rp->eir, eir_len,
  4961. EIR_LE_SC_RANDOM,
  4962. rand, sizeof(rand));
  4963. }
  4964. flags = mgmt_get_adv_discov_flags(hdev);
  4965. if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED))
  4966. flags |= LE_AD_NO_BREDR;
  4967. eir_len = eir_append_data(rp->eir, eir_len, EIR_FLAGS,
  4968. &flags, sizeof(flags));
  4969. break;
  4970. }
  4971. hci_dev_unlock(hdev);
  4972. hci_sock_set_flag(sk, HCI_MGMT_OOB_DATA_EVENTS);
  4973. status = MGMT_STATUS_SUCCESS;
  4974. complete:
  4975. rp->type = cp->type;
  4976. rp->eir_len = cpu_to_le16(eir_len);
  4977. err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_READ_LOCAL_OOB_EXT_DATA,
  4978. status, rp, sizeof(*rp) + eir_len);
  4979. if (err < 0 || status)
  4980. goto done;
  4981. err = mgmt_limited_event(MGMT_EV_LOCAL_OOB_DATA_UPDATED, hdev,
  4982. rp, sizeof(*rp) + eir_len,
  4983. HCI_MGMT_OOB_DATA_EVENTS, sk);
  4984. done:
  4985. kfree(rp);
  4986. return err;
  4987. }
  4988. static u32 get_supported_adv_flags(struct hci_dev *hdev)
  4989. {
  4990. u32 flags = 0;
  4991. flags |= MGMT_ADV_FLAG_CONNECTABLE;
  4992. flags |= MGMT_ADV_FLAG_DISCOV;
  4993. flags |= MGMT_ADV_FLAG_LIMITED_DISCOV;
  4994. flags |= MGMT_ADV_FLAG_MANAGED_FLAGS;
  4995. flags |= MGMT_ADV_FLAG_APPEARANCE;
  4996. flags |= MGMT_ADV_FLAG_LOCAL_NAME;
  4997. /* In extended adv TX_POWER returned from Set Adv Param
  4998. * will be always valid.
  4999. */
  5000. if ((hdev->adv_tx_power != HCI_TX_POWER_INVALID) ||
  5001. ext_adv_capable(hdev))
  5002. flags |= MGMT_ADV_FLAG_TX_POWER;
  5003. if (ext_adv_capable(hdev)) {
  5004. flags |= MGMT_ADV_FLAG_SEC_1M;
  5005. if (hdev->le_features[1] & HCI_LE_PHY_2M)
  5006. flags |= MGMT_ADV_FLAG_SEC_2M;
  5007. if (hdev->le_features[1] & HCI_LE_PHY_CODED)
  5008. flags |= MGMT_ADV_FLAG_SEC_CODED;
  5009. }
  5010. return flags;
  5011. }
  5012. static int read_adv_features(struct sock *sk, struct hci_dev *hdev,
  5013. void *data, u16 data_len)
  5014. {
  5015. struct mgmt_rp_read_adv_features *rp;
  5016. size_t rp_len;
  5017. int err;
  5018. struct adv_info *adv_instance;
  5019. u32 supported_flags;
  5020. u8 *instance;
  5021. BT_DBG("%s", hdev->name);
  5022. if (!lmp_le_capable(hdev))
  5023. return mgmt_cmd_status(sk, hdev->id, MGMT_OP_READ_ADV_FEATURES,
  5024. MGMT_STATUS_REJECTED);
  5025. hci_dev_lock(hdev);
  5026. rp_len = sizeof(*rp) + hdev->adv_instance_cnt;
  5027. rp = kmalloc(rp_len, GFP_ATOMIC);
  5028. if (!rp) {
  5029. hci_dev_unlock(hdev);
  5030. return -ENOMEM;
  5031. }
  5032. supported_flags = get_supported_adv_flags(hdev);
  5033. rp->supported_flags = cpu_to_le32(supported_flags);
  5034. rp->max_adv_data_len = HCI_MAX_AD_LENGTH;
  5035. rp->max_scan_rsp_len = HCI_MAX_AD_LENGTH;
  5036. rp->max_instances = HCI_MAX_ADV_INSTANCES;
  5037. rp->num_instances = hdev->adv_instance_cnt;
  5038. instance = rp->instance;
  5039. list_for_each_entry(adv_instance, &hdev->adv_instances, list) {
  5040. *instance = adv_instance->instance;
  5041. instance++;
  5042. }
  5043. hci_dev_unlock(hdev);
  5044. err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_READ_ADV_FEATURES,
  5045. MGMT_STATUS_SUCCESS, rp, rp_len);
  5046. kfree(rp);
  5047. return err;
  5048. }
  5049. static u8 calculate_name_len(struct hci_dev *hdev)
  5050. {
  5051. u8 buf[HCI_MAX_SHORT_NAME_LENGTH + 3];
  5052. return append_local_name(hdev, buf, 0);
  5053. }
  5054. static u8 tlv_data_max_len(struct hci_dev *hdev, u32 adv_flags,
  5055. bool is_adv_data)
  5056. {
  5057. u8 max_len = HCI_MAX_AD_LENGTH;
  5058. if (is_adv_data) {
  5059. if (adv_flags & (MGMT_ADV_FLAG_DISCOV |
  5060. MGMT_ADV_FLAG_LIMITED_DISCOV |
  5061. MGMT_ADV_FLAG_MANAGED_FLAGS))
  5062. max_len -= 3;
  5063. if (adv_flags & MGMT_ADV_FLAG_TX_POWER)
  5064. max_len -= 3;
  5065. } else {
  5066. if (adv_flags & MGMT_ADV_FLAG_LOCAL_NAME)
  5067. max_len -= calculate_name_len(hdev);
  5068. if (adv_flags & (MGMT_ADV_FLAG_APPEARANCE))
  5069. max_len -= 4;
  5070. }
  5071. return max_len;
  5072. }
  5073. static bool flags_managed(u32 adv_flags)
  5074. {
  5075. return adv_flags & (MGMT_ADV_FLAG_DISCOV |
  5076. MGMT_ADV_FLAG_LIMITED_DISCOV |
  5077. MGMT_ADV_FLAG_MANAGED_FLAGS);
  5078. }
  5079. static bool tx_power_managed(u32 adv_flags)
  5080. {
  5081. return adv_flags & MGMT_ADV_FLAG_TX_POWER;
  5082. }
  5083. static bool name_managed(u32 adv_flags)
  5084. {
  5085. return adv_flags & MGMT_ADV_FLAG_LOCAL_NAME;
  5086. }
  5087. static bool appearance_managed(u32 adv_flags)
  5088. {
  5089. return adv_flags & MGMT_ADV_FLAG_APPEARANCE;
  5090. }
  5091. static bool tlv_data_is_valid(struct hci_dev *hdev, u32 adv_flags, u8 *data,
  5092. u8 len, bool is_adv_data)
  5093. {
  5094. int i, cur_len;
  5095. u8 max_len;
  5096. max_len = tlv_data_max_len(hdev, adv_flags, is_adv_data);
  5097. if (len > max_len)
  5098. return false;
  5099. /* Make sure that the data is correctly formatted. */
  5100. for (i = 0, cur_len = 0; i < len; i += (cur_len + 1)) {
  5101. cur_len = data[i];
  5102. if (data[i + 1] == EIR_FLAGS &&
  5103. (!is_adv_data || flags_managed(adv_flags)))
  5104. return false;
  5105. if (data[i + 1] == EIR_TX_POWER && tx_power_managed(adv_flags))
  5106. return false;
  5107. if (data[i + 1] == EIR_NAME_COMPLETE && name_managed(adv_flags))
  5108. return false;
  5109. if (data[i + 1] == EIR_NAME_SHORT && name_managed(adv_flags))
  5110. return false;
  5111. if (data[i + 1] == EIR_APPEARANCE &&
  5112. appearance_managed(adv_flags))
  5113. return false;
  5114. /* If the current field length would exceed the total data
  5115. * length, then it's invalid.
  5116. */
  5117. if (i + cur_len >= len)
  5118. return false;
  5119. }
  5120. return true;
  5121. }
  5122. static void add_advertising_complete(struct hci_dev *hdev, u8 status,
  5123. u16 opcode)
  5124. {
  5125. struct mgmt_pending_cmd *cmd;
  5126. struct mgmt_cp_add_advertising *cp;
  5127. struct mgmt_rp_add_advertising rp;
  5128. struct adv_info *adv_instance, *n;
  5129. u8 instance;
  5130. BT_DBG("status %d", status);
  5131. hci_dev_lock(hdev);
  5132. cmd = pending_find(MGMT_OP_ADD_ADVERTISING, hdev);
  5133. list_for_each_entry_safe(adv_instance, n, &hdev->adv_instances, list) {
  5134. if (!adv_instance->pending)
  5135. continue;
  5136. if (!status) {
  5137. adv_instance->pending = false;
  5138. continue;
  5139. }
  5140. instance = adv_instance->instance;
  5141. if (hdev->cur_adv_instance == instance)
  5142. cancel_adv_timeout(hdev);
  5143. hci_remove_adv_instance(hdev, instance);
  5144. mgmt_advertising_removed(cmd ? cmd->sk : NULL, hdev, instance);
  5145. }
  5146. if (!cmd)
  5147. goto unlock;
  5148. cp = cmd->param;
  5149. rp.instance = cp->instance;
  5150. if (status)
  5151. mgmt_cmd_status(cmd->sk, cmd->index, cmd->opcode,
  5152. mgmt_status(status));
  5153. else
  5154. mgmt_cmd_complete(cmd->sk, cmd->index, cmd->opcode,
  5155. mgmt_status(status), &rp, sizeof(rp));
  5156. mgmt_pending_remove(cmd);
  5157. unlock:
  5158. hci_dev_unlock(hdev);
  5159. }
  5160. static int add_advertising(struct sock *sk, struct hci_dev *hdev,
  5161. void *data, u16 data_len)
  5162. {
  5163. struct mgmt_cp_add_advertising *cp = data;
  5164. struct mgmt_rp_add_advertising rp;
  5165. u32 flags;
  5166. u32 supported_flags, phy_flags;
  5167. u8 status;
  5168. u16 timeout, duration;
  5169. unsigned int prev_instance_cnt = hdev->adv_instance_cnt;
  5170. u8 schedule_instance = 0;
  5171. struct adv_info *next_instance;
  5172. int err;
  5173. struct mgmt_pending_cmd *cmd;
  5174. struct hci_request req;
  5175. BT_DBG("%s", hdev->name);
  5176. status = mgmt_le_support(hdev);
  5177. if (status)
  5178. return mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_ADVERTISING,
  5179. status);
  5180. if (cp->instance < 1 || cp->instance > HCI_MAX_ADV_INSTANCES)
  5181. return mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_ADVERTISING,
  5182. MGMT_STATUS_INVALID_PARAMS);
  5183. if (data_len != sizeof(*cp) + cp->adv_data_len + cp->scan_rsp_len)
  5184. return mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_ADVERTISING,
  5185. MGMT_STATUS_INVALID_PARAMS);
  5186. flags = __le32_to_cpu(cp->flags);
  5187. timeout = __le16_to_cpu(cp->timeout);
  5188. duration = __le16_to_cpu(cp->duration);
  5189. /* The current implementation only supports a subset of the specified
  5190. * flags. Also need to check mutual exclusiveness of sec flags.
  5191. */
  5192. supported_flags = get_supported_adv_flags(hdev);
  5193. phy_flags = flags & MGMT_ADV_FLAG_SEC_MASK;
  5194. if (flags & ~supported_flags ||
  5195. ((phy_flags && (phy_flags ^ (phy_flags & -phy_flags)))))
  5196. return mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_ADVERTISING,
  5197. MGMT_STATUS_INVALID_PARAMS);
  5198. hci_dev_lock(hdev);
  5199. if (timeout && !hdev_is_powered(hdev)) {
  5200. err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_ADVERTISING,
  5201. MGMT_STATUS_REJECTED);
  5202. goto unlock;
  5203. }
  5204. if (pending_find(MGMT_OP_ADD_ADVERTISING, hdev) ||
  5205. pending_find(MGMT_OP_REMOVE_ADVERTISING, hdev) ||
  5206. pending_find(MGMT_OP_SET_LE, hdev)) {
  5207. err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_ADVERTISING,
  5208. MGMT_STATUS_BUSY);
  5209. goto unlock;
  5210. }
  5211. if (!tlv_data_is_valid(hdev, flags, cp->data, cp->adv_data_len, true) ||
  5212. !tlv_data_is_valid(hdev, flags, cp->data + cp->adv_data_len,
  5213. cp->scan_rsp_len, false)) {
  5214. err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_ADVERTISING,
  5215. MGMT_STATUS_INVALID_PARAMS);
  5216. goto unlock;
  5217. }
  5218. err = hci_add_adv_instance(hdev, cp->instance, flags,
  5219. cp->adv_data_len, cp->data,
  5220. cp->scan_rsp_len,
  5221. cp->data + cp->adv_data_len,
  5222. timeout, duration);
  5223. if (err < 0) {
  5224. err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_ADVERTISING,
  5225. MGMT_STATUS_FAILED);
  5226. goto unlock;
  5227. }
  5228. /* Only trigger an advertising added event if a new instance was
  5229. * actually added.
  5230. */
  5231. if (hdev->adv_instance_cnt > prev_instance_cnt)
  5232. mgmt_advertising_added(sk, hdev, cp->instance);
  5233. if (hdev->cur_adv_instance == cp->instance) {
  5234. /* If the currently advertised instance is being changed then
  5235. * cancel the current advertising and schedule the next
  5236. * instance. If there is only one instance then the overridden
  5237. * advertising data will be visible right away.
  5238. */
  5239. cancel_adv_timeout(hdev);
  5240. next_instance = hci_get_next_instance(hdev, cp->instance);
  5241. if (next_instance)
  5242. schedule_instance = next_instance->instance;
  5243. } else if (!hdev->adv_instance_timeout) {
  5244. /* Immediately advertise the new instance if no other
  5245. * instance is currently being advertised.
  5246. */
  5247. schedule_instance = cp->instance;
  5248. }
  5249. /* If the HCI_ADVERTISING flag is set or the device isn't powered or
  5250. * there is no instance to be advertised then we have no HCI
  5251. * communication to make. Simply return.
  5252. */
  5253. if (!hdev_is_powered(hdev) ||
  5254. hci_dev_test_flag(hdev, HCI_ADVERTISING) ||
  5255. !schedule_instance) {
  5256. rp.instance = cp->instance;
  5257. err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_ADD_ADVERTISING,
  5258. MGMT_STATUS_SUCCESS, &rp, sizeof(rp));
  5259. goto unlock;
  5260. }
  5261. /* We're good to go, update advertising data, parameters, and start
  5262. * advertising.
  5263. */
  5264. cmd = mgmt_pending_add(sk, MGMT_OP_ADD_ADVERTISING, hdev, data,
  5265. data_len);
  5266. if (!cmd) {
  5267. err = -ENOMEM;
  5268. goto unlock;
  5269. }
  5270. hci_req_init(&req, hdev);
  5271. err = __hci_req_schedule_adv_instance(&req, schedule_instance, true);
  5272. if (!err)
  5273. err = hci_req_run(&req, add_advertising_complete);
  5274. if (err < 0)
  5275. mgmt_pending_remove(cmd);
  5276. unlock:
  5277. hci_dev_unlock(hdev);
  5278. return err;
  5279. }
  5280. static void remove_advertising_complete(struct hci_dev *hdev, u8 status,
  5281. u16 opcode)
  5282. {
  5283. struct mgmt_pending_cmd *cmd;
  5284. struct mgmt_cp_remove_advertising *cp;
  5285. struct mgmt_rp_remove_advertising rp;
  5286. BT_DBG("status %d", status);
  5287. hci_dev_lock(hdev);
  5288. /* A failure status here only means that we failed to disable
  5289. * advertising. Otherwise, the advertising instance has been removed,
  5290. * so report success.
  5291. */
  5292. cmd = pending_find(MGMT_OP_REMOVE_ADVERTISING, hdev);
  5293. if (!cmd)
  5294. goto unlock;
  5295. cp = cmd->param;
  5296. rp.instance = cp->instance;
  5297. mgmt_cmd_complete(cmd->sk, cmd->index, cmd->opcode, MGMT_STATUS_SUCCESS,
  5298. &rp, sizeof(rp));
  5299. mgmt_pending_remove(cmd);
  5300. unlock:
  5301. hci_dev_unlock(hdev);
  5302. }
  5303. static int remove_advertising(struct sock *sk, struct hci_dev *hdev,
  5304. void *data, u16 data_len)
  5305. {
  5306. struct mgmt_cp_remove_advertising *cp = data;
  5307. struct mgmt_rp_remove_advertising rp;
  5308. struct mgmt_pending_cmd *cmd;
  5309. struct hci_request req;
  5310. int err;
  5311. BT_DBG("%s", hdev->name);
  5312. hci_dev_lock(hdev);
  5313. if (cp->instance && !hci_find_adv_instance(hdev, cp->instance)) {
  5314. err = mgmt_cmd_status(sk, hdev->id,
  5315. MGMT_OP_REMOVE_ADVERTISING,
  5316. MGMT_STATUS_INVALID_PARAMS);
  5317. goto unlock;
  5318. }
  5319. if (pending_find(MGMT_OP_ADD_ADVERTISING, hdev) ||
  5320. pending_find(MGMT_OP_REMOVE_ADVERTISING, hdev) ||
  5321. pending_find(MGMT_OP_SET_LE, hdev)) {
  5322. err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_REMOVE_ADVERTISING,
  5323. MGMT_STATUS_BUSY);
  5324. goto unlock;
  5325. }
  5326. if (list_empty(&hdev->adv_instances)) {
  5327. err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_REMOVE_ADVERTISING,
  5328. MGMT_STATUS_INVALID_PARAMS);
  5329. goto unlock;
  5330. }
  5331. hci_req_init(&req, hdev);
  5332. hci_req_clear_adv_instance(hdev, sk, &req, cp->instance, true);
  5333. if (list_empty(&hdev->adv_instances))
  5334. __hci_req_disable_advertising(&req);
  5335. /* If no HCI commands have been collected so far or the HCI_ADVERTISING
  5336. * flag is set or the device isn't powered then we have no HCI
  5337. * communication to make. Simply return.
  5338. */
  5339. if (skb_queue_empty(&req.cmd_q) ||
  5340. !hdev_is_powered(hdev) ||
  5341. hci_dev_test_flag(hdev, HCI_ADVERTISING)) {
  5342. hci_req_purge(&req);
  5343. rp.instance = cp->instance;
  5344. err = mgmt_cmd_complete(sk, hdev->id,
  5345. MGMT_OP_REMOVE_ADVERTISING,
  5346. MGMT_STATUS_SUCCESS, &rp, sizeof(rp));
  5347. goto unlock;
  5348. }
  5349. cmd = mgmt_pending_add(sk, MGMT_OP_REMOVE_ADVERTISING, hdev, data,
  5350. data_len);
  5351. if (!cmd) {
  5352. err = -ENOMEM;
  5353. goto unlock;
  5354. }
  5355. err = hci_req_run(&req, remove_advertising_complete);
  5356. if (err < 0)
  5357. mgmt_pending_remove(cmd);
  5358. unlock:
  5359. hci_dev_unlock(hdev);
  5360. return err;
  5361. }
  5362. static int get_adv_size_info(struct sock *sk, struct hci_dev *hdev,
  5363. void *data, u16 data_len)
  5364. {
  5365. struct mgmt_cp_get_adv_size_info *cp = data;
  5366. struct mgmt_rp_get_adv_size_info rp;
  5367. u32 flags, supported_flags;
  5368. int err;
  5369. BT_DBG("%s", hdev->name);
  5370. if (!lmp_le_capable(hdev))
  5371. return mgmt_cmd_status(sk, hdev->id, MGMT_OP_GET_ADV_SIZE_INFO,
  5372. MGMT_STATUS_REJECTED);
  5373. if (cp->instance < 1 || cp->instance > HCI_MAX_ADV_INSTANCES)
  5374. return mgmt_cmd_status(sk, hdev->id, MGMT_OP_GET_ADV_SIZE_INFO,
  5375. MGMT_STATUS_INVALID_PARAMS);
  5376. flags = __le32_to_cpu(cp->flags);
  5377. /* The current implementation only supports a subset of the specified
  5378. * flags.
  5379. */
  5380. supported_flags = get_supported_adv_flags(hdev);
  5381. if (flags & ~supported_flags)
  5382. return mgmt_cmd_status(sk, hdev->id, MGMT_OP_GET_ADV_SIZE_INFO,
  5383. MGMT_STATUS_INVALID_PARAMS);
  5384. rp.instance = cp->instance;
  5385. rp.flags = cp->flags;
  5386. rp.max_adv_data_len = tlv_data_max_len(hdev, flags, true);
  5387. rp.max_scan_rsp_len = tlv_data_max_len(hdev, flags, false);
  5388. err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_GET_ADV_SIZE_INFO,
  5389. MGMT_STATUS_SUCCESS, &rp, sizeof(rp));
  5390. return err;
  5391. }
  5392. static const struct hci_mgmt_handler mgmt_handlers[] = {
  5393. { NULL }, /* 0x0000 (no command) */
  5394. { read_version, MGMT_READ_VERSION_SIZE,
  5395. HCI_MGMT_NO_HDEV |
  5396. HCI_MGMT_UNTRUSTED },
  5397. { read_commands, MGMT_READ_COMMANDS_SIZE,
  5398. HCI_MGMT_NO_HDEV |
  5399. HCI_MGMT_UNTRUSTED },
  5400. { read_index_list, MGMT_READ_INDEX_LIST_SIZE,
  5401. HCI_MGMT_NO_HDEV |
  5402. HCI_MGMT_UNTRUSTED },
  5403. { read_controller_info, MGMT_READ_INFO_SIZE,
  5404. HCI_MGMT_UNTRUSTED },
  5405. { set_powered, MGMT_SETTING_SIZE },
  5406. { set_discoverable, MGMT_SET_DISCOVERABLE_SIZE },
  5407. { set_connectable, MGMT_SETTING_SIZE },
  5408. { set_fast_connectable, MGMT_SETTING_SIZE },
  5409. { set_bondable, MGMT_SETTING_SIZE },
  5410. { set_link_security, MGMT_SETTING_SIZE },
  5411. { set_ssp, MGMT_SETTING_SIZE },
  5412. { set_hs, MGMT_SETTING_SIZE },
  5413. { set_le, MGMT_SETTING_SIZE },
  5414. { set_dev_class, MGMT_SET_DEV_CLASS_SIZE },
  5415. { set_local_name, MGMT_SET_LOCAL_NAME_SIZE },
  5416. { add_uuid, MGMT_ADD_UUID_SIZE },
  5417. { remove_uuid, MGMT_REMOVE_UUID_SIZE },
  5418. { load_link_keys, MGMT_LOAD_LINK_KEYS_SIZE,
  5419. HCI_MGMT_VAR_LEN },
  5420. { load_long_term_keys, MGMT_LOAD_LONG_TERM_KEYS_SIZE,
  5421. HCI_MGMT_VAR_LEN },
  5422. { disconnect, MGMT_DISCONNECT_SIZE },
  5423. { get_connections, MGMT_GET_CONNECTIONS_SIZE },
  5424. { pin_code_reply, MGMT_PIN_CODE_REPLY_SIZE },
  5425. { pin_code_neg_reply, MGMT_PIN_CODE_NEG_REPLY_SIZE },
  5426. { set_io_capability, MGMT_SET_IO_CAPABILITY_SIZE },
  5427. { pair_device, MGMT_PAIR_DEVICE_SIZE },
  5428. { cancel_pair_device, MGMT_CANCEL_PAIR_DEVICE_SIZE },
  5429. { unpair_device, MGMT_UNPAIR_DEVICE_SIZE },
  5430. { user_confirm_reply, MGMT_USER_CONFIRM_REPLY_SIZE },
  5431. { user_confirm_neg_reply, MGMT_USER_CONFIRM_NEG_REPLY_SIZE },
  5432. { user_passkey_reply, MGMT_USER_PASSKEY_REPLY_SIZE },
  5433. { user_passkey_neg_reply, MGMT_USER_PASSKEY_NEG_REPLY_SIZE },
  5434. { read_local_oob_data, MGMT_READ_LOCAL_OOB_DATA_SIZE },
  5435. { add_remote_oob_data, MGMT_ADD_REMOTE_OOB_DATA_SIZE,
  5436. HCI_MGMT_VAR_LEN },
  5437. { remove_remote_oob_data, MGMT_REMOVE_REMOTE_OOB_DATA_SIZE },
  5438. { start_discovery, MGMT_START_DISCOVERY_SIZE },
  5439. { stop_discovery, MGMT_STOP_DISCOVERY_SIZE },
  5440. { confirm_name, MGMT_CONFIRM_NAME_SIZE },
  5441. { block_device, MGMT_BLOCK_DEVICE_SIZE },
  5442. { unblock_device, MGMT_UNBLOCK_DEVICE_SIZE },
  5443. { set_device_id, MGMT_SET_DEVICE_ID_SIZE },
  5444. { set_advertising, MGMT_SETTING_SIZE },
  5445. { set_bredr, MGMT_SETTING_SIZE },
  5446. { set_static_address, MGMT_SET_STATIC_ADDRESS_SIZE },
  5447. { set_scan_params, MGMT_SET_SCAN_PARAMS_SIZE },
  5448. { set_secure_conn, MGMT_SETTING_SIZE },
  5449. { set_debug_keys, MGMT_SETTING_SIZE },
  5450. { set_privacy, MGMT_SET_PRIVACY_SIZE },
  5451. { load_irks, MGMT_LOAD_IRKS_SIZE,
  5452. HCI_MGMT_VAR_LEN },
  5453. { get_conn_info, MGMT_GET_CONN_INFO_SIZE },
  5454. { get_clock_info, MGMT_GET_CLOCK_INFO_SIZE },
  5455. { add_device, MGMT_ADD_DEVICE_SIZE },
  5456. { remove_device, MGMT_REMOVE_DEVICE_SIZE },
  5457. { load_conn_param, MGMT_LOAD_CONN_PARAM_SIZE,
  5458. HCI_MGMT_VAR_LEN },
  5459. { read_unconf_index_list, MGMT_READ_UNCONF_INDEX_LIST_SIZE,
  5460. HCI_MGMT_NO_HDEV |
  5461. HCI_MGMT_UNTRUSTED },
  5462. { read_config_info, MGMT_READ_CONFIG_INFO_SIZE,
  5463. HCI_MGMT_UNCONFIGURED |
  5464. HCI_MGMT_UNTRUSTED },
  5465. { set_external_config, MGMT_SET_EXTERNAL_CONFIG_SIZE,
  5466. HCI_MGMT_UNCONFIGURED },
  5467. { set_public_address, MGMT_SET_PUBLIC_ADDRESS_SIZE,
  5468. HCI_MGMT_UNCONFIGURED },
  5469. { start_service_discovery, MGMT_START_SERVICE_DISCOVERY_SIZE,
  5470. HCI_MGMT_VAR_LEN },
  5471. { read_local_oob_ext_data, MGMT_READ_LOCAL_OOB_EXT_DATA_SIZE },
  5472. { read_ext_index_list, MGMT_READ_EXT_INDEX_LIST_SIZE,
  5473. HCI_MGMT_NO_HDEV |
  5474. HCI_MGMT_UNTRUSTED },
  5475. { read_adv_features, MGMT_READ_ADV_FEATURES_SIZE },
  5476. { add_advertising, MGMT_ADD_ADVERTISING_SIZE,
  5477. HCI_MGMT_VAR_LEN },
  5478. { remove_advertising, MGMT_REMOVE_ADVERTISING_SIZE },
  5479. { get_adv_size_info, MGMT_GET_ADV_SIZE_INFO_SIZE },
  5480. { start_limited_discovery, MGMT_START_DISCOVERY_SIZE },
  5481. { read_ext_controller_info,MGMT_READ_EXT_INFO_SIZE,
  5482. HCI_MGMT_UNTRUSTED },
  5483. { set_appearance, MGMT_SET_APPEARANCE_SIZE },
  5484. { get_phy_configuration, MGMT_GET_PHY_CONFIGURATION_SIZE },
  5485. { set_phy_configuration, MGMT_SET_PHY_CONFIGURATION_SIZE },
  5486. };
  5487. void mgmt_index_added(struct hci_dev *hdev)
  5488. {
  5489. struct mgmt_ev_ext_index ev;
  5490. if (test_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks))
  5491. return;
  5492. switch (hdev->dev_type) {
  5493. case HCI_PRIMARY:
  5494. if (hci_dev_test_flag(hdev, HCI_UNCONFIGURED)) {
  5495. mgmt_index_event(MGMT_EV_UNCONF_INDEX_ADDED, hdev,
  5496. NULL, 0, HCI_MGMT_UNCONF_INDEX_EVENTS);
  5497. ev.type = 0x01;
  5498. } else {
  5499. mgmt_index_event(MGMT_EV_INDEX_ADDED, hdev, NULL, 0,
  5500. HCI_MGMT_INDEX_EVENTS);
  5501. ev.type = 0x00;
  5502. }
  5503. break;
  5504. case HCI_AMP:
  5505. ev.type = 0x02;
  5506. break;
  5507. default:
  5508. return;
  5509. }
  5510. ev.bus = hdev->bus;
  5511. mgmt_index_event(MGMT_EV_EXT_INDEX_ADDED, hdev, &ev, sizeof(ev),
  5512. HCI_MGMT_EXT_INDEX_EVENTS);
  5513. }
  5514. void mgmt_index_removed(struct hci_dev *hdev)
  5515. {
  5516. struct mgmt_ev_ext_index ev;
  5517. u8 status = MGMT_STATUS_INVALID_INDEX;
  5518. if (test_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks))
  5519. return;
  5520. switch (hdev->dev_type) {
  5521. case HCI_PRIMARY:
  5522. mgmt_pending_foreach(0, hdev, cmd_complete_rsp, &status);
  5523. if (hci_dev_test_flag(hdev, HCI_UNCONFIGURED)) {
  5524. mgmt_index_event(MGMT_EV_UNCONF_INDEX_REMOVED, hdev,
  5525. NULL, 0, HCI_MGMT_UNCONF_INDEX_EVENTS);
  5526. ev.type = 0x01;
  5527. } else {
  5528. mgmt_index_event(MGMT_EV_INDEX_REMOVED, hdev, NULL, 0,
  5529. HCI_MGMT_INDEX_EVENTS);
  5530. ev.type = 0x00;
  5531. }
  5532. break;
  5533. case HCI_AMP:
  5534. ev.type = 0x02;
  5535. break;
  5536. default:
  5537. return;
  5538. }
  5539. ev.bus = hdev->bus;
  5540. mgmt_index_event(MGMT_EV_EXT_INDEX_REMOVED, hdev, &ev, sizeof(ev),
  5541. HCI_MGMT_EXT_INDEX_EVENTS);
  5542. }
  5543. /* This function requires the caller holds hdev->lock */
  5544. static void restart_le_actions(struct hci_dev *hdev)
  5545. {
  5546. struct hci_conn_params *p;
  5547. list_for_each_entry(p, &hdev->le_conn_params, list) {
  5548. /* Needed for AUTO_OFF case where might not "really"
  5549. * have been powered off.
  5550. */
  5551. list_del_init(&p->action);
  5552. switch (p->auto_connect) {
  5553. case HCI_AUTO_CONN_DIRECT:
  5554. case HCI_AUTO_CONN_ALWAYS:
  5555. list_add(&p->action, &hdev->pend_le_conns);
  5556. break;
  5557. case HCI_AUTO_CONN_REPORT:
  5558. list_add(&p->action, &hdev->pend_le_reports);
  5559. break;
  5560. default:
  5561. break;
  5562. }
  5563. }
  5564. }
  5565. void mgmt_power_on(struct hci_dev *hdev, int err)
  5566. {
  5567. struct cmd_lookup match = { NULL, hdev };
  5568. BT_DBG("err %d", err);
  5569. hci_dev_lock(hdev);
  5570. if (!err) {
  5571. restart_le_actions(hdev);
  5572. hci_update_background_scan(hdev);
  5573. }
  5574. mgmt_pending_foreach(MGMT_OP_SET_POWERED, hdev, settings_rsp, &match);
  5575. new_settings(hdev, match.sk);
  5576. if (match.sk)
  5577. sock_put(match.sk);
  5578. hci_dev_unlock(hdev);
  5579. }
  5580. void __mgmt_power_off(struct hci_dev *hdev)
  5581. {
  5582. struct cmd_lookup match = { NULL, hdev };
  5583. u8 status, zero_cod[] = { 0, 0, 0 };
  5584. mgmt_pending_foreach(MGMT_OP_SET_POWERED, hdev, settings_rsp, &match);
  5585. /* If the power off is because of hdev unregistration let
  5586. * use the appropriate INVALID_INDEX status. Otherwise use
  5587. * NOT_POWERED. We cover both scenarios here since later in
  5588. * mgmt_index_removed() any hci_conn callbacks will have already
  5589. * been triggered, potentially causing misleading DISCONNECTED
  5590. * status responses.
  5591. */
  5592. if (hci_dev_test_flag(hdev, HCI_UNREGISTER))
  5593. status = MGMT_STATUS_INVALID_INDEX;
  5594. else
  5595. status = MGMT_STATUS_NOT_POWERED;
  5596. mgmt_pending_foreach(0, hdev, cmd_complete_rsp, &status);
  5597. if (memcmp(hdev->dev_class, zero_cod, sizeof(zero_cod)) != 0) {
  5598. mgmt_limited_event(MGMT_EV_CLASS_OF_DEV_CHANGED, hdev,
  5599. zero_cod, sizeof(zero_cod),
  5600. HCI_MGMT_DEV_CLASS_EVENTS, NULL);
  5601. ext_info_changed(hdev, NULL);
  5602. }
  5603. new_settings(hdev, match.sk);
  5604. if (match.sk)
  5605. sock_put(match.sk);
  5606. }
  5607. void mgmt_set_powered_failed(struct hci_dev *hdev, int err)
  5608. {
  5609. struct mgmt_pending_cmd *cmd;
  5610. u8 status;
  5611. cmd = pending_find(MGMT_OP_SET_POWERED, hdev);
  5612. if (!cmd)
  5613. return;
  5614. if (err == -ERFKILL)
  5615. status = MGMT_STATUS_RFKILLED;
  5616. else
  5617. status = MGMT_STATUS_FAILED;
  5618. mgmt_cmd_status(cmd->sk, hdev->id, MGMT_OP_SET_POWERED, status);
  5619. mgmt_pending_remove(cmd);
  5620. }
  5621. void mgmt_new_link_key(struct hci_dev *hdev, struct link_key *key,
  5622. bool persistent)
  5623. {
  5624. struct mgmt_ev_new_link_key ev;
  5625. memset(&ev, 0, sizeof(ev));
  5626. ev.store_hint = persistent;
  5627. bacpy(&ev.key.addr.bdaddr, &key->bdaddr);
  5628. ev.key.addr.type = BDADDR_BREDR;
  5629. ev.key.type = key->type;
  5630. memcpy(ev.key.val, key->val, HCI_LINK_KEY_SIZE);
  5631. ev.key.pin_len = key->pin_len;
  5632. mgmt_event(MGMT_EV_NEW_LINK_KEY, hdev, &ev, sizeof(ev), NULL);
  5633. }
  5634. static u8 mgmt_ltk_type(struct smp_ltk *ltk)
  5635. {
  5636. switch (ltk->type) {
  5637. case SMP_LTK:
  5638. case SMP_LTK_SLAVE:
  5639. if (ltk->authenticated)
  5640. return MGMT_LTK_AUTHENTICATED;
  5641. return MGMT_LTK_UNAUTHENTICATED;
  5642. case SMP_LTK_P256:
  5643. if (ltk->authenticated)
  5644. return MGMT_LTK_P256_AUTH;
  5645. return MGMT_LTK_P256_UNAUTH;
  5646. case SMP_LTK_P256_DEBUG:
  5647. return MGMT_LTK_P256_DEBUG;
  5648. }
  5649. return MGMT_LTK_UNAUTHENTICATED;
  5650. }
  5651. void mgmt_new_ltk(struct hci_dev *hdev, struct smp_ltk *key, bool persistent)
  5652. {
  5653. struct mgmt_ev_new_long_term_key ev;
  5654. memset(&ev, 0, sizeof(ev));
  5655. /* Devices using resolvable or non-resolvable random addresses
  5656. * without providing an identity resolving key don't require
  5657. * to store long term keys. Their addresses will change the
  5658. * next time around.
  5659. *
  5660. * Only when a remote device provides an identity address
  5661. * make sure the long term key is stored. If the remote
  5662. * identity is known, the long term keys are internally
  5663. * mapped to the identity address. So allow static random
  5664. * and public addresses here.
  5665. */
  5666. if (key->bdaddr_type == ADDR_LE_DEV_RANDOM &&
  5667. (key->bdaddr.b[5] & 0xc0) != 0xc0)
  5668. ev.store_hint = 0x00;
  5669. else
  5670. ev.store_hint = persistent;
  5671. bacpy(&ev.key.addr.bdaddr, &key->bdaddr);
  5672. ev.key.addr.type = link_to_bdaddr(LE_LINK, key->bdaddr_type);
  5673. ev.key.type = mgmt_ltk_type(key);
  5674. ev.key.enc_size = key->enc_size;
  5675. ev.key.ediv = key->ediv;
  5676. ev.key.rand = key->rand;
  5677. if (key->type == SMP_LTK)
  5678. ev.key.master = 1;
  5679. /* Make sure we copy only the significant bytes based on the
  5680. * encryption key size, and set the rest of the value to zeroes.
  5681. */
  5682. memcpy(ev.key.val, key->val, key->enc_size);
  5683. memset(ev.key.val + key->enc_size, 0,
  5684. sizeof(ev.key.val) - key->enc_size);
  5685. mgmt_event(MGMT_EV_NEW_LONG_TERM_KEY, hdev, &ev, sizeof(ev), NULL);
  5686. }
  5687. void mgmt_new_irk(struct hci_dev *hdev, struct smp_irk *irk, bool persistent)
  5688. {
  5689. struct mgmt_ev_new_irk ev;
  5690. memset(&ev, 0, sizeof(ev));
  5691. ev.store_hint = persistent;
  5692. bacpy(&ev.rpa, &irk->rpa);
  5693. bacpy(&ev.irk.addr.bdaddr, &irk->bdaddr);
  5694. ev.irk.addr.type = link_to_bdaddr(LE_LINK, irk->addr_type);
  5695. memcpy(ev.irk.val, irk->val, sizeof(irk->val));
  5696. mgmt_event(MGMT_EV_NEW_IRK, hdev, &ev, sizeof(ev), NULL);
  5697. }
  5698. void mgmt_new_csrk(struct hci_dev *hdev, struct smp_csrk *csrk,
  5699. bool persistent)
  5700. {
  5701. struct mgmt_ev_new_csrk ev;
  5702. memset(&ev, 0, sizeof(ev));
  5703. /* Devices using resolvable or non-resolvable random addresses
  5704. * without providing an identity resolving key don't require
  5705. * to store signature resolving keys. Their addresses will change
  5706. * the next time around.
  5707. *
  5708. * Only when a remote device provides an identity address
  5709. * make sure the signature resolving key is stored. So allow
  5710. * static random and public addresses here.
  5711. */
  5712. if (csrk->bdaddr_type == ADDR_LE_DEV_RANDOM &&
  5713. (csrk->bdaddr.b[5] & 0xc0) != 0xc0)
  5714. ev.store_hint = 0x00;
  5715. else
  5716. ev.store_hint = persistent;
  5717. bacpy(&ev.key.addr.bdaddr, &csrk->bdaddr);
  5718. ev.key.addr.type = link_to_bdaddr(LE_LINK, csrk->bdaddr_type);
  5719. ev.key.type = csrk->type;
  5720. memcpy(ev.key.val, csrk->val, sizeof(csrk->val));
  5721. mgmt_event(MGMT_EV_NEW_CSRK, hdev, &ev, sizeof(ev), NULL);
  5722. }
  5723. void mgmt_new_conn_param(struct hci_dev *hdev, bdaddr_t *bdaddr,
  5724. u8 bdaddr_type, u8 store_hint, u16 min_interval,
  5725. u16 max_interval, u16 latency, u16 timeout)
  5726. {
  5727. struct mgmt_ev_new_conn_param ev;
  5728. if (!hci_is_identity_address(bdaddr, bdaddr_type))
  5729. return;
  5730. memset(&ev, 0, sizeof(ev));
  5731. bacpy(&ev.addr.bdaddr, bdaddr);
  5732. ev.addr.type = link_to_bdaddr(LE_LINK, bdaddr_type);
  5733. ev.store_hint = store_hint;
  5734. ev.min_interval = cpu_to_le16(min_interval);
  5735. ev.max_interval = cpu_to_le16(max_interval);
  5736. ev.latency = cpu_to_le16(latency);
  5737. ev.timeout = cpu_to_le16(timeout);
  5738. mgmt_event(MGMT_EV_NEW_CONN_PARAM, hdev, &ev, sizeof(ev), NULL);
  5739. }
  5740. void mgmt_device_connected(struct hci_dev *hdev, struct hci_conn *conn,
  5741. u32 flags, u8 *name, u8 name_len)
  5742. {
  5743. char buf[512];
  5744. struct mgmt_ev_device_connected *ev = (void *) buf;
  5745. u16 eir_len = 0;
  5746. bacpy(&ev->addr.bdaddr, &conn->dst);
  5747. ev->addr.type = link_to_bdaddr(conn->type, conn->dst_type);
  5748. ev->flags = __cpu_to_le32(flags);
  5749. /* We must ensure that the EIR Data fields are ordered and
  5750. * unique. Keep it simple for now and avoid the problem by not
  5751. * adding any BR/EDR data to the LE adv.
  5752. */
  5753. if (conn->le_adv_data_len > 0) {
  5754. memcpy(&ev->eir[eir_len],
  5755. conn->le_adv_data, conn->le_adv_data_len);
  5756. eir_len = conn->le_adv_data_len;
  5757. } else {
  5758. if (name_len > 0)
  5759. eir_len = eir_append_data(ev->eir, 0, EIR_NAME_COMPLETE,
  5760. name, name_len);
  5761. if (memcmp(conn->dev_class, "\0\0\0", 3) != 0)
  5762. eir_len = eir_append_data(ev->eir, eir_len,
  5763. EIR_CLASS_OF_DEV,
  5764. conn->dev_class, 3);
  5765. }
  5766. ev->eir_len = cpu_to_le16(eir_len);
  5767. mgmt_event(MGMT_EV_DEVICE_CONNECTED, hdev, buf,
  5768. sizeof(*ev) + eir_len, NULL);
  5769. }
  5770. static void disconnect_rsp(struct mgmt_pending_cmd *cmd, void *data)
  5771. {
  5772. struct sock **sk = data;
  5773. cmd->cmd_complete(cmd, 0);
  5774. *sk = cmd->sk;
  5775. sock_hold(*sk);
  5776. mgmt_pending_remove(cmd);
  5777. }
  5778. static void unpair_device_rsp(struct mgmt_pending_cmd *cmd, void *data)
  5779. {
  5780. struct hci_dev *hdev = data;
  5781. struct mgmt_cp_unpair_device *cp = cmd->param;
  5782. device_unpaired(hdev, &cp->addr.bdaddr, cp->addr.type, cmd->sk);
  5783. cmd->cmd_complete(cmd, 0);
  5784. mgmt_pending_remove(cmd);
  5785. }
  5786. bool mgmt_powering_down(struct hci_dev *hdev)
  5787. {
  5788. struct mgmt_pending_cmd *cmd;
  5789. struct mgmt_mode *cp;
  5790. cmd = pending_find(MGMT_OP_SET_POWERED, hdev);
  5791. if (!cmd)
  5792. return false;
  5793. cp = cmd->param;
  5794. if (!cp->val)
  5795. return true;
  5796. return false;
  5797. }
  5798. void mgmt_device_disconnected(struct hci_dev *hdev, bdaddr_t *bdaddr,
  5799. u8 link_type, u8 addr_type, u8 reason,
  5800. bool mgmt_connected)
  5801. {
  5802. struct mgmt_ev_device_disconnected ev;
  5803. struct sock *sk = NULL;
  5804. /* The connection is still in hci_conn_hash so test for 1
  5805. * instead of 0 to know if this is the last one.
  5806. */
  5807. if (mgmt_powering_down(hdev) && hci_conn_count(hdev) == 1) {
  5808. cancel_delayed_work(&hdev->power_off);
  5809. queue_work(hdev->req_workqueue, &hdev->power_off.work);
  5810. }
  5811. if (!mgmt_connected)
  5812. return;
  5813. if (link_type != ACL_LINK && link_type != LE_LINK)
  5814. return;
  5815. mgmt_pending_foreach(MGMT_OP_DISCONNECT, hdev, disconnect_rsp, &sk);
  5816. bacpy(&ev.addr.bdaddr, bdaddr);
  5817. ev.addr.type = link_to_bdaddr(link_type, addr_type);
  5818. ev.reason = reason;
  5819. mgmt_event(MGMT_EV_DEVICE_DISCONNECTED, hdev, &ev, sizeof(ev), sk);
  5820. if (sk)
  5821. sock_put(sk);
  5822. mgmt_pending_foreach(MGMT_OP_UNPAIR_DEVICE, hdev, unpair_device_rsp,
  5823. hdev);
  5824. }
  5825. void mgmt_disconnect_failed(struct hci_dev *hdev, bdaddr_t *bdaddr,
  5826. u8 link_type, u8 addr_type, u8 status)
  5827. {
  5828. u8 bdaddr_type = link_to_bdaddr(link_type, addr_type);
  5829. struct mgmt_cp_disconnect *cp;
  5830. struct mgmt_pending_cmd *cmd;
  5831. mgmt_pending_foreach(MGMT_OP_UNPAIR_DEVICE, hdev, unpair_device_rsp,
  5832. hdev);
  5833. cmd = pending_find(MGMT_OP_DISCONNECT, hdev);
  5834. if (!cmd)
  5835. return;
  5836. cp = cmd->param;
  5837. if (bacmp(bdaddr, &cp->addr.bdaddr))
  5838. return;
  5839. if (cp->addr.type != bdaddr_type)
  5840. return;
  5841. cmd->cmd_complete(cmd, mgmt_status(status));
  5842. mgmt_pending_remove(cmd);
  5843. }
  5844. void mgmt_connect_failed(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 link_type,
  5845. u8 addr_type, u8 status)
  5846. {
  5847. struct mgmt_ev_connect_failed ev;
  5848. /* The connection is still in hci_conn_hash so test for 1
  5849. * instead of 0 to know if this is the last one.
  5850. */
  5851. if (mgmt_powering_down(hdev) && hci_conn_count(hdev) == 1) {
  5852. cancel_delayed_work(&hdev->power_off);
  5853. queue_work(hdev->req_workqueue, &hdev->power_off.work);
  5854. }
  5855. bacpy(&ev.addr.bdaddr, bdaddr);
  5856. ev.addr.type = link_to_bdaddr(link_type, addr_type);
  5857. ev.status = mgmt_status(status);
  5858. mgmt_event(MGMT_EV_CONNECT_FAILED, hdev, &ev, sizeof(ev), NULL);
  5859. }
  5860. void mgmt_pin_code_request(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 secure)
  5861. {
  5862. struct mgmt_ev_pin_code_request ev;
  5863. bacpy(&ev.addr.bdaddr, bdaddr);
  5864. ev.addr.type = BDADDR_BREDR;
  5865. ev.secure = secure;
  5866. mgmt_event(MGMT_EV_PIN_CODE_REQUEST, hdev, &ev, sizeof(ev), NULL);
  5867. }
  5868. void mgmt_pin_code_reply_complete(struct hci_dev *hdev, bdaddr_t *bdaddr,
  5869. u8 status)
  5870. {
  5871. struct mgmt_pending_cmd *cmd;
  5872. cmd = pending_find(MGMT_OP_PIN_CODE_REPLY, hdev);
  5873. if (!cmd)
  5874. return;
  5875. cmd->cmd_complete(cmd, mgmt_status(status));
  5876. mgmt_pending_remove(cmd);
  5877. }
  5878. void mgmt_pin_code_neg_reply_complete(struct hci_dev *hdev, bdaddr_t *bdaddr,
  5879. u8 status)
  5880. {
  5881. struct mgmt_pending_cmd *cmd;
  5882. cmd = pending_find(MGMT_OP_PIN_CODE_NEG_REPLY, hdev);
  5883. if (!cmd)
  5884. return;
  5885. cmd->cmd_complete(cmd, mgmt_status(status));
  5886. mgmt_pending_remove(cmd);
  5887. }
  5888. int mgmt_user_confirm_request(struct hci_dev *hdev, bdaddr_t *bdaddr,
  5889. u8 link_type, u8 addr_type, u32 value,
  5890. u8 confirm_hint)
  5891. {
  5892. struct mgmt_ev_user_confirm_request ev;
  5893. BT_DBG("%s", hdev->name);
  5894. bacpy(&ev.addr.bdaddr, bdaddr);
  5895. ev.addr.type = link_to_bdaddr(link_type, addr_type);
  5896. ev.confirm_hint = confirm_hint;
  5897. ev.value = cpu_to_le32(value);
  5898. return mgmt_event(MGMT_EV_USER_CONFIRM_REQUEST, hdev, &ev, sizeof(ev),
  5899. NULL);
  5900. }
  5901. int mgmt_user_passkey_request(struct hci_dev *hdev, bdaddr_t *bdaddr,
  5902. u8 link_type, u8 addr_type)
  5903. {
  5904. struct mgmt_ev_user_passkey_request ev;
  5905. BT_DBG("%s", hdev->name);
  5906. bacpy(&ev.addr.bdaddr, bdaddr);
  5907. ev.addr.type = link_to_bdaddr(link_type, addr_type);
  5908. return mgmt_event(MGMT_EV_USER_PASSKEY_REQUEST, hdev, &ev, sizeof(ev),
  5909. NULL);
  5910. }
  5911. static int user_pairing_resp_complete(struct hci_dev *hdev, bdaddr_t *bdaddr,
  5912. u8 link_type, u8 addr_type, u8 status,
  5913. u8 opcode)
  5914. {
  5915. struct mgmt_pending_cmd *cmd;
  5916. cmd = pending_find(opcode, hdev);
  5917. if (!cmd)
  5918. return -ENOENT;
  5919. cmd->cmd_complete(cmd, mgmt_status(status));
  5920. mgmt_pending_remove(cmd);
  5921. return 0;
  5922. }
  5923. int mgmt_user_confirm_reply_complete(struct hci_dev *hdev, bdaddr_t *bdaddr,
  5924. u8 link_type, u8 addr_type, u8 status)
  5925. {
  5926. return user_pairing_resp_complete(hdev, bdaddr, link_type, addr_type,
  5927. status, MGMT_OP_USER_CONFIRM_REPLY);
  5928. }
  5929. int mgmt_user_confirm_neg_reply_complete(struct hci_dev *hdev, bdaddr_t *bdaddr,
  5930. u8 link_type, u8 addr_type, u8 status)
  5931. {
  5932. return user_pairing_resp_complete(hdev, bdaddr, link_type, addr_type,
  5933. status,
  5934. MGMT_OP_USER_CONFIRM_NEG_REPLY);
  5935. }
  5936. int mgmt_user_passkey_reply_complete(struct hci_dev *hdev, bdaddr_t *bdaddr,
  5937. u8 link_type, u8 addr_type, u8 status)
  5938. {
  5939. return user_pairing_resp_complete(hdev, bdaddr, link_type, addr_type,
  5940. status, MGMT_OP_USER_PASSKEY_REPLY);
  5941. }
  5942. int mgmt_user_passkey_neg_reply_complete(struct hci_dev *hdev, bdaddr_t *bdaddr,
  5943. u8 link_type, u8 addr_type, u8 status)
  5944. {
  5945. return user_pairing_resp_complete(hdev, bdaddr, link_type, addr_type,
  5946. status,
  5947. MGMT_OP_USER_PASSKEY_NEG_REPLY);
  5948. }
  5949. int mgmt_user_passkey_notify(struct hci_dev *hdev, bdaddr_t *bdaddr,
  5950. u8 link_type, u8 addr_type, u32 passkey,
  5951. u8 entered)
  5952. {
  5953. struct mgmt_ev_passkey_notify ev;
  5954. BT_DBG("%s", hdev->name);
  5955. bacpy(&ev.addr.bdaddr, bdaddr);
  5956. ev.addr.type = link_to_bdaddr(link_type, addr_type);
  5957. ev.passkey = __cpu_to_le32(passkey);
  5958. ev.entered = entered;
  5959. return mgmt_event(MGMT_EV_PASSKEY_NOTIFY, hdev, &ev, sizeof(ev), NULL);
  5960. }
  5961. void mgmt_auth_failed(struct hci_conn *conn, u8 hci_status)
  5962. {
  5963. struct mgmt_ev_auth_failed ev;
  5964. struct mgmt_pending_cmd *cmd;
  5965. u8 status = mgmt_status(hci_status);
  5966. bacpy(&ev.addr.bdaddr, &conn->dst);
  5967. ev.addr.type = link_to_bdaddr(conn->type, conn->dst_type);
  5968. ev.status = status;
  5969. cmd = find_pairing(conn);
  5970. mgmt_event(MGMT_EV_AUTH_FAILED, conn->hdev, &ev, sizeof(ev),
  5971. cmd ? cmd->sk : NULL);
  5972. if (cmd) {
  5973. cmd->cmd_complete(cmd, status);
  5974. mgmt_pending_remove(cmd);
  5975. }
  5976. }
  5977. void mgmt_auth_enable_complete(struct hci_dev *hdev, u8 status)
  5978. {
  5979. struct cmd_lookup match = { NULL, hdev };
  5980. bool changed;
  5981. if (status) {
  5982. u8 mgmt_err = mgmt_status(status);
  5983. mgmt_pending_foreach(MGMT_OP_SET_LINK_SECURITY, hdev,
  5984. cmd_status_rsp, &mgmt_err);
  5985. return;
  5986. }
  5987. if (test_bit(HCI_AUTH, &hdev->flags))
  5988. changed = !hci_dev_test_and_set_flag(hdev, HCI_LINK_SECURITY);
  5989. else
  5990. changed = hci_dev_test_and_clear_flag(hdev, HCI_LINK_SECURITY);
  5991. mgmt_pending_foreach(MGMT_OP_SET_LINK_SECURITY, hdev, settings_rsp,
  5992. &match);
  5993. if (changed)
  5994. new_settings(hdev, match.sk);
  5995. if (match.sk)
  5996. sock_put(match.sk);
  5997. }
  5998. static void clear_eir(struct hci_request *req)
  5999. {
  6000. struct hci_dev *hdev = req->hdev;
  6001. struct hci_cp_write_eir cp;
  6002. if (!lmp_ext_inq_capable(hdev))
  6003. return;
  6004. memset(hdev->eir, 0, sizeof(hdev->eir));
  6005. memset(&cp, 0, sizeof(cp));
  6006. hci_req_add(req, HCI_OP_WRITE_EIR, sizeof(cp), &cp);
  6007. }
  6008. void mgmt_ssp_enable_complete(struct hci_dev *hdev, u8 enable, u8 status)
  6009. {
  6010. struct cmd_lookup match = { NULL, hdev };
  6011. struct hci_request req;
  6012. bool changed = false;
  6013. if (status) {
  6014. u8 mgmt_err = mgmt_status(status);
  6015. if (enable && hci_dev_test_and_clear_flag(hdev,
  6016. HCI_SSP_ENABLED)) {
  6017. hci_dev_clear_flag(hdev, HCI_HS_ENABLED);
  6018. new_settings(hdev, NULL);
  6019. }
  6020. mgmt_pending_foreach(MGMT_OP_SET_SSP, hdev, cmd_status_rsp,
  6021. &mgmt_err);
  6022. return;
  6023. }
  6024. if (enable) {
  6025. changed = !hci_dev_test_and_set_flag(hdev, HCI_SSP_ENABLED);
  6026. } else {
  6027. changed = hci_dev_test_and_clear_flag(hdev, HCI_SSP_ENABLED);
  6028. if (!changed)
  6029. changed = hci_dev_test_and_clear_flag(hdev,
  6030. HCI_HS_ENABLED);
  6031. else
  6032. hci_dev_clear_flag(hdev, HCI_HS_ENABLED);
  6033. }
  6034. mgmt_pending_foreach(MGMT_OP_SET_SSP, hdev, settings_rsp, &match);
  6035. if (changed)
  6036. new_settings(hdev, match.sk);
  6037. if (match.sk)
  6038. sock_put(match.sk);
  6039. hci_req_init(&req, hdev);
  6040. if (hci_dev_test_flag(hdev, HCI_SSP_ENABLED)) {
  6041. if (hci_dev_test_flag(hdev, HCI_USE_DEBUG_KEYS))
  6042. hci_req_add(&req, HCI_OP_WRITE_SSP_DEBUG_MODE,
  6043. sizeof(enable), &enable);
  6044. __hci_req_update_eir(&req);
  6045. } else {
  6046. clear_eir(&req);
  6047. }
  6048. hci_req_run(&req, NULL);
  6049. }
  6050. static void sk_lookup(struct mgmt_pending_cmd *cmd, void *data)
  6051. {
  6052. struct cmd_lookup *match = data;
  6053. if (match->sk == NULL) {
  6054. match->sk = cmd->sk;
  6055. sock_hold(match->sk);
  6056. }
  6057. }
  6058. void mgmt_set_class_of_dev_complete(struct hci_dev *hdev, u8 *dev_class,
  6059. u8 status)
  6060. {
  6061. struct cmd_lookup match = { NULL, hdev, mgmt_status(status) };
  6062. mgmt_pending_foreach(MGMT_OP_SET_DEV_CLASS, hdev, sk_lookup, &match);
  6063. mgmt_pending_foreach(MGMT_OP_ADD_UUID, hdev, sk_lookup, &match);
  6064. mgmt_pending_foreach(MGMT_OP_REMOVE_UUID, hdev, sk_lookup, &match);
  6065. if (!status) {
  6066. mgmt_limited_event(MGMT_EV_CLASS_OF_DEV_CHANGED, hdev, dev_class,
  6067. 3, HCI_MGMT_DEV_CLASS_EVENTS, NULL);
  6068. ext_info_changed(hdev, NULL);
  6069. }
  6070. if (match.sk)
  6071. sock_put(match.sk);
  6072. }
  6073. void mgmt_set_local_name_complete(struct hci_dev *hdev, u8 *name, u8 status)
  6074. {
  6075. struct mgmt_cp_set_local_name ev;
  6076. struct mgmt_pending_cmd *cmd;
  6077. if (status)
  6078. return;
  6079. memset(&ev, 0, sizeof(ev));
  6080. memcpy(ev.name, name, HCI_MAX_NAME_LENGTH);
  6081. memcpy(ev.short_name, hdev->short_name, HCI_MAX_SHORT_NAME_LENGTH);
  6082. cmd = pending_find(MGMT_OP_SET_LOCAL_NAME, hdev);
  6083. if (!cmd) {
  6084. memcpy(hdev->dev_name, name, sizeof(hdev->dev_name));
  6085. /* If this is a HCI command related to powering on the
  6086. * HCI dev don't send any mgmt signals.
  6087. */
  6088. if (pending_find(MGMT_OP_SET_POWERED, hdev))
  6089. return;
  6090. }
  6091. mgmt_limited_event(MGMT_EV_LOCAL_NAME_CHANGED, hdev, &ev, sizeof(ev),
  6092. HCI_MGMT_LOCAL_NAME_EVENTS, cmd ? cmd->sk : NULL);
  6093. ext_info_changed(hdev, cmd ? cmd->sk : NULL);
  6094. }
  6095. static inline bool has_uuid(u8 *uuid, u16 uuid_count, u8 (*uuids)[16])
  6096. {
  6097. int i;
  6098. for (i = 0; i < uuid_count; i++) {
  6099. if (!memcmp(uuid, uuids[i], 16))
  6100. return true;
  6101. }
  6102. return false;
  6103. }
  6104. static bool eir_has_uuids(u8 *eir, u16 eir_len, u16 uuid_count, u8 (*uuids)[16])
  6105. {
  6106. u16 parsed = 0;
  6107. while (parsed < eir_len) {
  6108. u8 field_len = eir[0];
  6109. u8 uuid[16];
  6110. int i;
  6111. if (field_len == 0)
  6112. break;
  6113. if (eir_len - parsed < field_len + 1)
  6114. break;
  6115. switch (eir[1]) {
  6116. case EIR_UUID16_ALL:
  6117. case EIR_UUID16_SOME:
  6118. for (i = 0; i + 3 <= field_len; i += 2) {
  6119. memcpy(uuid, bluetooth_base_uuid, 16);
  6120. uuid[13] = eir[i + 3];
  6121. uuid[12] = eir[i + 2];
  6122. if (has_uuid(uuid, uuid_count, uuids))
  6123. return true;
  6124. }
  6125. break;
  6126. case EIR_UUID32_ALL:
  6127. case EIR_UUID32_SOME:
  6128. for (i = 0; i + 5 <= field_len; i += 4) {
  6129. memcpy(uuid, bluetooth_base_uuid, 16);
  6130. uuid[15] = eir[i + 5];
  6131. uuid[14] = eir[i + 4];
  6132. uuid[13] = eir[i + 3];
  6133. uuid[12] = eir[i + 2];
  6134. if (has_uuid(uuid, uuid_count, uuids))
  6135. return true;
  6136. }
  6137. break;
  6138. case EIR_UUID128_ALL:
  6139. case EIR_UUID128_SOME:
  6140. for (i = 0; i + 17 <= field_len; i += 16) {
  6141. memcpy(uuid, eir + i + 2, 16);
  6142. if (has_uuid(uuid, uuid_count, uuids))
  6143. return true;
  6144. }
  6145. break;
  6146. }
  6147. parsed += field_len + 1;
  6148. eir += field_len + 1;
  6149. }
  6150. return false;
  6151. }
  6152. static void restart_le_scan(struct hci_dev *hdev)
  6153. {
  6154. /* If controller is not scanning we are done. */
  6155. if (!hci_dev_test_flag(hdev, HCI_LE_SCAN))
  6156. return;
  6157. if (time_after(jiffies + DISCOV_LE_RESTART_DELAY,
  6158. hdev->discovery.scan_start +
  6159. hdev->discovery.scan_duration))
  6160. return;
  6161. queue_delayed_work(hdev->req_workqueue, &hdev->le_scan_restart,
  6162. DISCOV_LE_RESTART_DELAY);
  6163. }
  6164. static bool is_filter_match(struct hci_dev *hdev, s8 rssi, u8 *eir,
  6165. u16 eir_len, u8 *scan_rsp, u8 scan_rsp_len)
  6166. {
  6167. /* If a RSSI threshold has been specified, and
  6168. * HCI_QUIRK_STRICT_DUPLICATE_FILTER is not set, then all results with
  6169. * a RSSI smaller than the RSSI threshold will be dropped. If the quirk
  6170. * is set, let it through for further processing, as we might need to
  6171. * restart the scan.
  6172. *
  6173. * For BR/EDR devices (pre 1.2) providing no RSSI during inquiry,
  6174. * the results are also dropped.
  6175. */
  6176. if (hdev->discovery.rssi != HCI_RSSI_INVALID &&
  6177. (rssi == HCI_RSSI_INVALID ||
  6178. (rssi < hdev->discovery.rssi &&
  6179. !test_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks))))
  6180. return false;
  6181. if (hdev->discovery.uuid_count != 0) {
  6182. /* If a list of UUIDs is provided in filter, results with no
  6183. * matching UUID should be dropped.
  6184. */
  6185. if (!eir_has_uuids(eir, eir_len, hdev->discovery.uuid_count,
  6186. hdev->discovery.uuids) &&
  6187. !eir_has_uuids(scan_rsp, scan_rsp_len,
  6188. hdev->discovery.uuid_count,
  6189. hdev->discovery.uuids))
  6190. return false;
  6191. }
  6192. /* If duplicate filtering does not report RSSI changes, then restart
  6193. * scanning to ensure updated result with updated RSSI values.
  6194. */
  6195. if (test_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks)) {
  6196. restart_le_scan(hdev);
  6197. /* Validate RSSI value against the RSSI threshold once more. */
  6198. if (hdev->discovery.rssi != HCI_RSSI_INVALID &&
  6199. rssi < hdev->discovery.rssi)
  6200. return false;
  6201. }
  6202. return true;
  6203. }
  6204. void mgmt_device_found(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 link_type,
  6205. u8 addr_type, u8 *dev_class, s8 rssi, u32 flags,
  6206. u8 *eir, u16 eir_len, u8 *scan_rsp, u8 scan_rsp_len)
  6207. {
  6208. char buf[512];
  6209. struct mgmt_ev_device_found *ev = (void *)buf;
  6210. size_t ev_size;
  6211. /* Don't send events for a non-kernel initiated discovery. With
  6212. * LE one exception is if we have pend_le_reports > 0 in which
  6213. * case we're doing passive scanning and want these events.
  6214. */
  6215. if (!hci_discovery_active(hdev)) {
  6216. if (link_type == ACL_LINK)
  6217. return;
  6218. if (link_type == LE_LINK && list_empty(&hdev->pend_le_reports))
  6219. return;
  6220. }
  6221. if (hdev->discovery.result_filtering) {
  6222. /* We are using service discovery */
  6223. if (!is_filter_match(hdev, rssi, eir, eir_len, scan_rsp,
  6224. scan_rsp_len))
  6225. return;
  6226. }
  6227. if (hdev->discovery.limited) {
  6228. /* Check for limited discoverable bit */
  6229. if (dev_class) {
  6230. if (!(dev_class[1] & 0x20))
  6231. return;
  6232. } else {
  6233. u8 *flags = eir_get_data(eir, eir_len, EIR_FLAGS, NULL);
  6234. if (!flags || !(flags[0] & LE_AD_LIMITED))
  6235. return;
  6236. }
  6237. }
  6238. /* Make sure that the buffer is big enough. The 5 extra bytes
  6239. * are for the potential CoD field.
  6240. */
  6241. if (sizeof(*ev) + eir_len + scan_rsp_len + 5 > sizeof(buf))
  6242. return;
  6243. memset(buf, 0, sizeof(buf));
  6244. /* In case of device discovery with BR/EDR devices (pre 1.2), the
  6245. * RSSI value was reported as 0 when not available. This behavior
  6246. * is kept when using device discovery. This is required for full
  6247. * backwards compatibility with the API.
  6248. *
  6249. * However when using service discovery, the value 127 will be
  6250. * returned when the RSSI is not available.
  6251. */
  6252. if (rssi == HCI_RSSI_INVALID && !hdev->discovery.report_invalid_rssi &&
  6253. link_type == ACL_LINK)
  6254. rssi = 0;
  6255. bacpy(&ev->addr.bdaddr, bdaddr);
  6256. ev->addr.type = link_to_bdaddr(link_type, addr_type);
  6257. ev->rssi = rssi;
  6258. ev->flags = cpu_to_le32(flags);
  6259. if (eir_len > 0)
  6260. /* Copy EIR or advertising data into event */
  6261. memcpy(ev->eir, eir, eir_len);
  6262. if (dev_class && !eir_get_data(ev->eir, eir_len, EIR_CLASS_OF_DEV,
  6263. NULL))
  6264. eir_len = eir_append_data(ev->eir, eir_len, EIR_CLASS_OF_DEV,
  6265. dev_class, 3);
  6266. if (scan_rsp_len > 0)
  6267. /* Append scan response data to event */
  6268. memcpy(ev->eir + eir_len, scan_rsp, scan_rsp_len);
  6269. ev->eir_len = cpu_to_le16(eir_len + scan_rsp_len);
  6270. ev_size = sizeof(*ev) + eir_len + scan_rsp_len;
  6271. mgmt_event(MGMT_EV_DEVICE_FOUND, hdev, ev, ev_size, NULL);
  6272. }
  6273. void mgmt_remote_name(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 link_type,
  6274. u8 addr_type, s8 rssi, u8 *name, u8 name_len)
  6275. {
  6276. struct mgmt_ev_device_found *ev;
  6277. char buf[sizeof(*ev) + HCI_MAX_NAME_LENGTH + 2];
  6278. u16 eir_len;
  6279. ev = (struct mgmt_ev_device_found *) buf;
  6280. memset(buf, 0, sizeof(buf));
  6281. bacpy(&ev->addr.bdaddr, bdaddr);
  6282. ev->addr.type = link_to_bdaddr(link_type, addr_type);
  6283. ev->rssi = rssi;
  6284. eir_len = eir_append_data(ev->eir, 0, EIR_NAME_COMPLETE, name,
  6285. name_len);
  6286. ev->eir_len = cpu_to_le16(eir_len);
  6287. mgmt_event(MGMT_EV_DEVICE_FOUND, hdev, ev, sizeof(*ev) + eir_len, NULL);
  6288. }
  6289. void mgmt_discovering(struct hci_dev *hdev, u8 discovering)
  6290. {
  6291. struct mgmt_ev_discovering ev;
  6292. BT_DBG("%s discovering %u", hdev->name, discovering);
  6293. memset(&ev, 0, sizeof(ev));
  6294. ev.type = hdev->discovery.type;
  6295. ev.discovering = discovering;
  6296. mgmt_event(MGMT_EV_DISCOVERING, hdev, &ev, sizeof(ev), NULL);
  6297. }
  6298. static struct hci_mgmt_chan chan = {
  6299. .channel = HCI_CHANNEL_CONTROL,
  6300. .handler_count = ARRAY_SIZE(mgmt_handlers),
  6301. .handlers = mgmt_handlers,
  6302. .hdev_init = mgmt_init_hdev,
  6303. };
  6304. int mgmt_init(void)
  6305. {
  6306. return hci_mgmt_chan_register(&chan);
  6307. }
  6308. void mgmt_exit(void)
  6309. {
  6310. hci_mgmt_chan_unregister(&chan);
  6311. }