mgmt.c 211 KB

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