hci_event.c 140 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936
  1. /*
  2. BlueZ - Bluetooth protocol stack for Linux
  3. Copyright (c) 2000-2001, 2010, Code Aurora Forum. All rights reserved.
  4. Written 2000,2001 by Maxim Krasnyansky <maxk@qualcomm.com>
  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 event handling. */
  21. #include <asm/unaligned.h>
  22. #include <net/bluetooth/bluetooth.h>
  23. #include <net/bluetooth/hci_core.h>
  24. #include <net/bluetooth/mgmt.h>
  25. #include "hci_request.h"
  26. #include "hci_debugfs.h"
  27. #include "a2mp.h"
  28. #include "amp.h"
  29. #include "smp.h"
  30. #define ZERO_KEY "\x00\x00\x00\x00\x00\x00\x00\x00" \
  31. "\x00\x00\x00\x00\x00\x00\x00\x00"
  32. /* Handle HCI Event packets */
  33. static void hci_cc_inquiry_cancel(struct hci_dev *hdev, struct sk_buff *skb)
  34. {
  35. __u8 status = *((__u8 *) skb->data);
  36. BT_DBG("%s status 0x%2.2x", hdev->name, status);
  37. if (status)
  38. return;
  39. clear_bit(HCI_INQUIRY, &hdev->flags);
  40. smp_mb__after_atomic(); /* wake_up_bit advises about this barrier */
  41. wake_up_bit(&hdev->flags, HCI_INQUIRY);
  42. hci_dev_lock(hdev);
  43. /* Set discovery state to stopped if we're not doing LE active
  44. * scanning.
  45. */
  46. if (!hci_dev_test_flag(hdev, HCI_LE_SCAN) ||
  47. hdev->le_scan_type != LE_SCAN_ACTIVE)
  48. hci_discovery_set_state(hdev, DISCOVERY_STOPPED);
  49. hci_dev_unlock(hdev);
  50. hci_conn_check_pending(hdev);
  51. }
  52. static void hci_cc_periodic_inq(struct hci_dev *hdev, struct sk_buff *skb)
  53. {
  54. __u8 status = *((__u8 *) skb->data);
  55. BT_DBG("%s status 0x%2.2x", hdev->name, status);
  56. if (status)
  57. return;
  58. hci_dev_set_flag(hdev, HCI_PERIODIC_INQ);
  59. }
  60. static void hci_cc_exit_periodic_inq(struct hci_dev *hdev, struct sk_buff *skb)
  61. {
  62. __u8 status = *((__u8 *) skb->data);
  63. BT_DBG("%s status 0x%2.2x", hdev->name, status);
  64. if (status)
  65. return;
  66. hci_dev_clear_flag(hdev, HCI_PERIODIC_INQ);
  67. hci_conn_check_pending(hdev);
  68. }
  69. static void hci_cc_remote_name_req_cancel(struct hci_dev *hdev,
  70. struct sk_buff *skb)
  71. {
  72. BT_DBG("%s", hdev->name);
  73. }
  74. static void hci_cc_role_discovery(struct hci_dev *hdev, struct sk_buff *skb)
  75. {
  76. struct hci_rp_role_discovery *rp = (void *) skb->data;
  77. struct hci_conn *conn;
  78. BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
  79. if (rp->status)
  80. return;
  81. hci_dev_lock(hdev);
  82. conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(rp->handle));
  83. if (conn)
  84. conn->role = rp->role;
  85. hci_dev_unlock(hdev);
  86. }
  87. static void hci_cc_read_link_policy(struct hci_dev *hdev, struct sk_buff *skb)
  88. {
  89. struct hci_rp_read_link_policy *rp = (void *) skb->data;
  90. struct hci_conn *conn;
  91. BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
  92. if (rp->status)
  93. return;
  94. hci_dev_lock(hdev);
  95. conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(rp->handle));
  96. if (conn)
  97. conn->link_policy = __le16_to_cpu(rp->policy);
  98. hci_dev_unlock(hdev);
  99. }
  100. static void hci_cc_write_link_policy(struct hci_dev *hdev, struct sk_buff *skb)
  101. {
  102. struct hci_rp_write_link_policy *rp = (void *) skb->data;
  103. struct hci_conn *conn;
  104. void *sent;
  105. BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
  106. if (rp->status)
  107. return;
  108. sent = hci_sent_cmd_data(hdev, HCI_OP_WRITE_LINK_POLICY);
  109. if (!sent)
  110. return;
  111. hci_dev_lock(hdev);
  112. conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(rp->handle));
  113. if (conn)
  114. conn->link_policy = get_unaligned_le16(sent + 2);
  115. hci_dev_unlock(hdev);
  116. }
  117. static void hci_cc_read_def_link_policy(struct hci_dev *hdev,
  118. struct sk_buff *skb)
  119. {
  120. struct hci_rp_read_def_link_policy *rp = (void *) skb->data;
  121. BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
  122. if (rp->status)
  123. return;
  124. hdev->link_policy = __le16_to_cpu(rp->policy);
  125. }
  126. static void hci_cc_write_def_link_policy(struct hci_dev *hdev,
  127. struct sk_buff *skb)
  128. {
  129. __u8 status = *((__u8 *) skb->data);
  130. void *sent;
  131. BT_DBG("%s status 0x%2.2x", hdev->name, status);
  132. if (status)
  133. return;
  134. sent = hci_sent_cmd_data(hdev, HCI_OP_WRITE_DEF_LINK_POLICY);
  135. if (!sent)
  136. return;
  137. hdev->link_policy = get_unaligned_le16(sent);
  138. }
  139. static void hci_cc_reset(struct hci_dev *hdev, struct sk_buff *skb)
  140. {
  141. __u8 status = *((__u8 *) skb->data);
  142. BT_DBG("%s status 0x%2.2x", hdev->name, status);
  143. clear_bit(HCI_RESET, &hdev->flags);
  144. if (status)
  145. return;
  146. /* Reset all non-persistent flags */
  147. hci_dev_clear_volatile_flags(hdev);
  148. hci_discovery_set_state(hdev, DISCOVERY_STOPPED);
  149. hdev->inq_tx_power = HCI_TX_POWER_INVALID;
  150. hdev->adv_tx_power = HCI_TX_POWER_INVALID;
  151. memset(hdev->adv_data, 0, sizeof(hdev->adv_data));
  152. hdev->adv_data_len = 0;
  153. memset(hdev->scan_rsp_data, 0, sizeof(hdev->scan_rsp_data));
  154. hdev->scan_rsp_data_len = 0;
  155. hdev->le_scan_type = LE_SCAN_PASSIVE;
  156. hdev->ssp_debug_mode = 0;
  157. hci_bdaddr_list_clear(&hdev->le_white_list);
  158. hci_bdaddr_list_clear(&hdev->le_resolv_list);
  159. }
  160. static void hci_cc_read_stored_link_key(struct hci_dev *hdev,
  161. struct sk_buff *skb)
  162. {
  163. struct hci_rp_read_stored_link_key *rp = (void *)skb->data;
  164. struct hci_cp_read_stored_link_key *sent;
  165. BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
  166. sent = hci_sent_cmd_data(hdev, HCI_OP_READ_STORED_LINK_KEY);
  167. if (!sent)
  168. return;
  169. if (!rp->status && sent->read_all == 0x01) {
  170. hdev->stored_max_keys = rp->max_keys;
  171. hdev->stored_num_keys = rp->num_keys;
  172. }
  173. }
  174. static void hci_cc_delete_stored_link_key(struct hci_dev *hdev,
  175. struct sk_buff *skb)
  176. {
  177. struct hci_rp_delete_stored_link_key *rp = (void *)skb->data;
  178. BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
  179. if (rp->status)
  180. return;
  181. if (rp->num_keys <= hdev->stored_num_keys)
  182. hdev->stored_num_keys -= rp->num_keys;
  183. else
  184. hdev->stored_num_keys = 0;
  185. }
  186. static void hci_cc_write_local_name(struct hci_dev *hdev, struct sk_buff *skb)
  187. {
  188. __u8 status = *((__u8 *) skb->data);
  189. void *sent;
  190. BT_DBG("%s status 0x%2.2x", hdev->name, status);
  191. sent = hci_sent_cmd_data(hdev, HCI_OP_WRITE_LOCAL_NAME);
  192. if (!sent)
  193. return;
  194. hci_dev_lock(hdev);
  195. if (hci_dev_test_flag(hdev, HCI_MGMT))
  196. mgmt_set_local_name_complete(hdev, sent, status);
  197. else if (!status)
  198. memcpy(hdev->dev_name, sent, HCI_MAX_NAME_LENGTH);
  199. hci_dev_unlock(hdev);
  200. }
  201. static void hci_cc_read_local_name(struct hci_dev *hdev, struct sk_buff *skb)
  202. {
  203. struct hci_rp_read_local_name *rp = (void *) skb->data;
  204. BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
  205. if (rp->status)
  206. return;
  207. if (hci_dev_test_flag(hdev, HCI_SETUP) ||
  208. hci_dev_test_flag(hdev, HCI_CONFIG))
  209. memcpy(hdev->dev_name, rp->name, HCI_MAX_NAME_LENGTH);
  210. }
  211. static void hci_cc_write_auth_enable(struct hci_dev *hdev, struct sk_buff *skb)
  212. {
  213. __u8 status = *((__u8 *) skb->data);
  214. void *sent;
  215. BT_DBG("%s status 0x%2.2x", hdev->name, status);
  216. sent = hci_sent_cmd_data(hdev, HCI_OP_WRITE_AUTH_ENABLE);
  217. if (!sent)
  218. return;
  219. hci_dev_lock(hdev);
  220. if (!status) {
  221. __u8 param = *((__u8 *) sent);
  222. if (param == AUTH_ENABLED)
  223. set_bit(HCI_AUTH, &hdev->flags);
  224. else
  225. clear_bit(HCI_AUTH, &hdev->flags);
  226. }
  227. if (hci_dev_test_flag(hdev, HCI_MGMT))
  228. mgmt_auth_enable_complete(hdev, status);
  229. hci_dev_unlock(hdev);
  230. }
  231. static void hci_cc_write_encrypt_mode(struct hci_dev *hdev, struct sk_buff *skb)
  232. {
  233. __u8 status = *((__u8 *) skb->data);
  234. __u8 param;
  235. void *sent;
  236. BT_DBG("%s status 0x%2.2x", hdev->name, status);
  237. if (status)
  238. return;
  239. sent = hci_sent_cmd_data(hdev, HCI_OP_WRITE_ENCRYPT_MODE);
  240. if (!sent)
  241. return;
  242. param = *((__u8 *) sent);
  243. if (param)
  244. set_bit(HCI_ENCRYPT, &hdev->flags);
  245. else
  246. clear_bit(HCI_ENCRYPT, &hdev->flags);
  247. }
  248. static void hci_cc_write_scan_enable(struct hci_dev *hdev, struct sk_buff *skb)
  249. {
  250. __u8 status = *((__u8 *) skb->data);
  251. __u8 param;
  252. void *sent;
  253. BT_DBG("%s status 0x%2.2x", hdev->name, status);
  254. sent = hci_sent_cmd_data(hdev, HCI_OP_WRITE_SCAN_ENABLE);
  255. if (!sent)
  256. return;
  257. param = *((__u8 *) sent);
  258. hci_dev_lock(hdev);
  259. if (status) {
  260. hdev->discov_timeout = 0;
  261. goto done;
  262. }
  263. if (param & SCAN_INQUIRY)
  264. set_bit(HCI_ISCAN, &hdev->flags);
  265. else
  266. clear_bit(HCI_ISCAN, &hdev->flags);
  267. if (param & SCAN_PAGE)
  268. set_bit(HCI_PSCAN, &hdev->flags);
  269. else
  270. clear_bit(HCI_PSCAN, &hdev->flags);
  271. done:
  272. hci_dev_unlock(hdev);
  273. }
  274. static void hci_cc_read_class_of_dev(struct hci_dev *hdev, struct sk_buff *skb)
  275. {
  276. struct hci_rp_read_class_of_dev *rp = (void *) skb->data;
  277. BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
  278. if (rp->status)
  279. return;
  280. memcpy(hdev->dev_class, rp->dev_class, 3);
  281. BT_DBG("%s class 0x%.2x%.2x%.2x", hdev->name,
  282. hdev->dev_class[2], hdev->dev_class[1], hdev->dev_class[0]);
  283. }
  284. static void hci_cc_write_class_of_dev(struct hci_dev *hdev, struct sk_buff *skb)
  285. {
  286. __u8 status = *((__u8 *) skb->data);
  287. void *sent;
  288. BT_DBG("%s status 0x%2.2x", hdev->name, status);
  289. sent = hci_sent_cmd_data(hdev, HCI_OP_WRITE_CLASS_OF_DEV);
  290. if (!sent)
  291. return;
  292. hci_dev_lock(hdev);
  293. if (status == 0)
  294. memcpy(hdev->dev_class, sent, 3);
  295. if (hci_dev_test_flag(hdev, HCI_MGMT))
  296. mgmt_set_class_of_dev_complete(hdev, sent, status);
  297. hci_dev_unlock(hdev);
  298. }
  299. static void hci_cc_read_voice_setting(struct hci_dev *hdev, struct sk_buff *skb)
  300. {
  301. struct hci_rp_read_voice_setting *rp = (void *) skb->data;
  302. __u16 setting;
  303. BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
  304. if (rp->status)
  305. return;
  306. setting = __le16_to_cpu(rp->voice_setting);
  307. if (hdev->voice_setting == setting)
  308. return;
  309. hdev->voice_setting = setting;
  310. BT_DBG("%s voice setting 0x%4.4x", hdev->name, setting);
  311. if (hdev->notify)
  312. hdev->notify(hdev, HCI_NOTIFY_VOICE_SETTING);
  313. }
  314. static void hci_cc_write_voice_setting(struct hci_dev *hdev,
  315. struct sk_buff *skb)
  316. {
  317. __u8 status = *((__u8 *) skb->data);
  318. __u16 setting;
  319. void *sent;
  320. BT_DBG("%s status 0x%2.2x", hdev->name, status);
  321. if (status)
  322. return;
  323. sent = hci_sent_cmd_data(hdev, HCI_OP_WRITE_VOICE_SETTING);
  324. if (!sent)
  325. return;
  326. setting = get_unaligned_le16(sent);
  327. if (hdev->voice_setting == setting)
  328. return;
  329. hdev->voice_setting = setting;
  330. BT_DBG("%s voice setting 0x%4.4x", hdev->name, setting);
  331. if (hdev->notify)
  332. hdev->notify(hdev, HCI_NOTIFY_VOICE_SETTING);
  333. }
  334. static void hci_cc_read_num_supported_iac(struct hci_dev *hdev,
  335. struct sk_buff *skb)
  336. {
  337. struct hci_rp_read_num_supported_iac *rp = (void *) skb->data;
  338. BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
  339. if (rp->status)
  340. return;
  341. hdev->num_iac = rp->num_iac;
  342. BT_DBG("%s num iac %d", hdev->name, hdev->num_iac);
  343. }
  344. static void hci_cc_write_ssp_mode(struct hci_dev *hdev, struct sk_buff *skb)
  345. {
  346. __u8 status = *((__u8 *) skb->data);
  347. struct hci_cp_write_ssp_mode *sent;
  348. BT_DBG("%s status 0x%2.2x", hdev->name, status);
  349. sent = hci_sent_cmd_data(hdev, HCI_OP_WRITE_SSP_MODE);
  350. if (!sent)
  351. return;
  352. hci_dev_lock(hdev);
  353. if (!status) {
  354. if (sent->mode)
  355. hdev->features[1][0] |= LMP_HOST_SSP;
  356. else
  357. hdev->features[1][0] &= ~LMP_HOST_SSP;
  358. }
  359. if (hci_dev_test_flag(hdev, HCI_MGMT))
  360. mgmt_ssp_enable_complete(hdev, sent->mode, status);
  361. else if (!status) {
  362. if (sent->mode)
  363. hci_dev_set_flag(hdev, HCI_SSP_ENABLED);
  364. else
  365. hci_dev_clear_flag(hdev, HCI_SSP_ENABLED);
  366. }
  367. hci_dev_unlock(hdev);
  368. }
  369. static void hci_cc_write_sc_support(struct hci_dev *hdev, struct sk_buff *skb)
  370. {
  371. u8 status = *((u8 *) skb->data);
  372. struct hci_cp_write_sc_support *sent;
  373. BT_DBG("%s status 0x%2.2x", hdev->name, status);
  374. sent = hci_sent_cmd_data(hdev, HCI_OP_WRITE_SC_SUPPORT);
  375. if (!sent)
  376. return;
  377. hci_dev_lock(hdev);
  378. if (!status) {
  379. if (sent->support)
  380. hdev->features[1][0] |= LMP_HOST_SC;
  381. else
  382. hdev->features[1][0] &= ~LMP_HOST_SC;
  383. }
  384. if (!hci_dev_test_flag(hdev, HCI_MGMT) && !status) {
  385. if (sent->support)
  386. hci_dev_set_flag(hdev, HCI_SC_ENABLED);
  387. else
  388. hci_dev_clear_flag(hdev, HCI_SC_ENABLED);
  389. }
  390. hci_dev_unlock(hdev);
  391. }
  392. static void hci_cc_read_local_version(struct hci_dev *hdev, struct sk_buff *skb)
  393. {
  394. struct hci_rp_read_local_version *rp = (void *) skb->data;
  395. BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
  396. if (rp->status)
  397. return;
  398. if (hci_dev_test_flag(hdev, HCI_SETUP) ||
  399. hci_dev_test_flag(hdev, HCI_CONFIG)) {
  400. hdev->hci_ver = rp->hci_ver;
  401. hdev->hci_rev = __le16_to_cpu(rp->hci_rev);
  402. hdev->lmp_ver = rp->lmp_ver;
  403. hdev->manufacturer = __le16_to_cpu(rp->manufacturer);
  404. hdev->lmp_subver = __le16_to_cpu(rp->lmp_subver);
  405. }
  406. }
  407. static void hci_cc_read_local_commands(struct hci_dev *hdev,
  408. struct sk_buff *skb)
  409. {
  410. struct hci_rp_read_local_commands *rp = (void *) skb->data;
  411. BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
  412. if (rp->status)
  413. return;
  414. if (hci_dev_test_flag(hdev, HCI_SETUP) ||
  415. hci_dev_test_flag(hdev, HCI_CONFIG))
  416. memcpy(hdev->commands, rp->commands, sizeof(hdev->commands));
  417. }
  418. static void hci_cc_read_local_features(struct hci_dev *hdev,
  419. struct sk_buff *skb)
  420. {
  421. struct hci_rp_read_local_features *rp = (void *) skb->data;
  422. BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
  423. if (rp->status)
  424. return;
  425. memcpy(hdev->features, rp->features, 8);
  426. /* Adjust default settings according to features
  427. * supported by device. */
  428. if (hdev->features[0][0] & LMP_3SLOT)
  429. hdev->pkt_type |= (HCI_DM3 | HCI_DH3);
  430. if (hdev->features[0][0] & LMP_5SLOT)
  431. hdev->pkt_type |= (HCI_DM5 | HCI_DH5);
  432. if (hdev->features[0][1] & LMP_HV2) {
  433. hdev->pkt_type |= (HCI_HV2);
  434. hdev->esco_type |= (ESCO_HV2);
  435. }
  436. if (hdev->features[0][1] & LMP_HV3) {
  437. hdev->pkt_type |= (HCI_HV3);
  438. hdev->esco_type |= (ESCO_HV3);
  439. }
  440. if (lmp_esco_capable(hdev))
  441. hdev->esco_type |= (ESCO_EV3);
  442. if (hdev->features[0][4] & LMP_EV4)
  443. hdev->esco_type |= (ESCO_EV4);
  444. if (hdev->features[0][4] & LMP_EV5)
  445. hdev->esco_type |= (ESCO_EV5);
  446. if (hdev->features[0][5] & LMP_EDR_ESCO_2M)
  447. hdev->esco_type |= (ESCO_2EV3);
  448. if (hdev->features[0][5] & LMP_EDR_ESCO_3M)
  449. hdev->esco_type |= (ESCO_3EV3);
  450. if (hdev->features[0][5] & LMP_EDR_3S_ESCO)
  451. hdev->esco_type |= (ESCO_2EV5 | ESCO_3EV5);
  452. }
  453. static void hci_cc_read_local_ext_features(struct hci_dev *hdev,
  454. struct sk_buff *skb)
  455. {
  456. struct hci_rp_read_local_ext_features *rp = (void *) skb->data;
  457. BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
  458. if (rp->status)
  459. return;
  460. if (hdev->max_page < rp->max_page)
  461. hdev->max_page = rp->max_page;
  462. if (rp->page < HCI_MAX_PAGES)
  463. memcpy(hdev->features[rp->page], rp->features, 8);
  464. }
  465. static void hci_cc_read_flow_control_mode(struct hci_dev *hdev,
  466. struct sk_buff *skb)
  467. {
  468. struct hci_rp_read_flow_control_mode *rp = (void *) skb->data;
  469. BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
  470. if (rp->status)
  471. return;
  472. hdev->flow_ctl_mode = rp->mode;
  473. }
  474. static void hci_cc_read_buffer_size(struct hci_dev *hdev, struct sk_buff *skb)
  475. {
  476. struct hci_rp_read_buffer_size *rp = (void *) skb->data;
  477. BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
  478. if (rp->status)
  479. return;
  480. hdev->acl_mtu = __le16_to_cpu(rp->acl_mtu);
  481. hdev->sco_mtu = rp->sco_mtu;
  482. hdev->acl_pkts = __le16_to_cpu(rp->acl_max_pkt);
  483. hdev->sco_pkts = __le16_to_cpu(rp->sco_max_pkt);
  484. if (test_bit(HCI_QUIRK_FIXUP_BUFFER_SIZE, &hdev->quirks)) {
  485. hdev->sco_mtu = 64;
  486. hdev->sco_pkts = 8;
  487. }
  488. hdev->acl_cnt = hdev->acl_pkts;
  489. hdev->sco_cnt = hdev->sco_pkts;
  490. BT_DBG("%s acl mtu %d:%d sco mtu %d:%d", hdev->name, hdev->acl_mtu,
  491. hdev->acl_pkts, hdev->sco_mtu, hdev->sco_pkts);
  492. }
  493. static void hci_cc_read_bd_addr(struct hci_dev *hdev, struct sk_buff *skb)
  494. {
  495. struct hci_rp_read_bd_addr *rp = (void *) skb->data;
  496. BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
  497. if (rp->status)
  498. return;
  499. if (test_bit(HCI_INIT, &hdev->flags))
  500. bacpy(&hdev->bdaddr, &rp->bdaddr);
  501. if (hci_dev_test_flag(hdev, HCI_SETUP))
  502. bacpy(&hdev->setup_addr, &rp->bdaddr);
  503. }
  504. static void hci_cc_read_page_scan_activity(struct hci_dev *hdev,
  505. struct sk_buff *skb)
  506. {
  507. struct hci_rp_read_page_scan_activity *rp = (void *) skb->data;
  508. BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
  509. if (rp->status)
  510. return;
  511. if (test_bit(HCI_INIT, &hdev->flags)) {
  512. hdev->page_scan_interval = __le16_to_cpu(rp->interval);
  513. hdev->page_scan_window = __le16_to_cpu(rp->window);
  514. }
  515. }
  516. static void hci_cc_write_page_scan_activity(struct hci_dev *hdev,
  517. struct sk_buff *skb)
  518. {
  519. u8 status = *((u8 *) skb->data);
  520. struct hci_cp_write_page_scan_activity *sent;
  521. BT_DBG("%s status 0x%2.2x", hdev->name, status);
  522. if (status)
  523. return;
  524. sent = hci_sent_cmd_data(hdev, HCI_OP_WRITE_PAGE_SCAN_ACTIVITY);
  525. if (!sent)
  526. return;
  527. hdev->page_scan_interval = __le16_to_cpu(sent->interval);
  528. hdev->page_scan_window = __le16_to_cpu(sent->window);
  529. }
  530. static void hci_cc_read_page_scan_type(struct hci_dev *hdev,
  531. struct sk_buff *skb)
  532. {
  533. struct hci_rp_read_page_scan_type *rp = (void *) skb->data;
  534. BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
  535. if (rp->status)
  536. return;
  537. if (test_bit(HCI_INIT, &hdev->flags))
  538. hdev->page_scan_type = rp->type;
  539. }
  540. static void hci_cc_write_page_scan_type(struct hci_dev *hdev,
  541. struct sk_buff *skb)
  542. {
  543. u8 status = *((u8 *) skb->data);
  544. u8 *type;
  545. BT_DBG("%s status 0x%2.2x", hdev->name, status);
  546. if (status)
  547. return;
  548. type = hci_sent_cmd_data(hdev, HCI_OP_WRITE_PAGE_SCAN_TYPE);
  549. if (type)
  550. hdev->page_scan_type = *type;
  551. }
  552. static void hci_cc_read_data_block_size(struct hci_dev *hdev,
  553. struct sk_buff *skb)
  554. {
  555. struct hci_rp_read_data_block_size *rp = (void *) skb->data;
  556. BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
  557. if (rp->status)
  558. return;
  559. hdev->block_mtu = __le16_to_cpu(rp->max_acl_len);
  560. hdev->block_len = __le16_to_cpu(rp->block_len);
  561. hdev->num_blocks = __le16_to_cpu(rp->num_blocks);
  562. hdev->block_cnt = hdev->num_blocks;
  563. BT_DBG("%s blk mtu %d cnt %d len %d", hdev->name, hdev->block_mtu,
  564. hdev->block_cnt, hdev->block_len);
  565. }
  566. static void hci_cc_read_clock(struct hci_dev *hdev, struct sk_buff *skb)
  567. {
  568. struct hci_rp_read_clock *rp = (void *) skb->data;
  569. struct hci_cp_read_clock *cp;
  570. struct hci_conn *conn;
  571. BT_DBG("%s", hdev->name);
  572. if (skb->len < sizeof(*rp))
  573. return;
  574. if (rp->status)
  575. return;
  576. hci_dev_lock(hdev);
  577. cp = hci_sent_cmd_data(hdev, HCI_OP_READ_CLOCK);
  578. if (!cp)
  579. goto unlock;
  580. if (cp->which == 0x00) {
  581. hdev->clock = le32_to_cpu(rp->clock);
  582. goto unlock;
  583. }
  584. conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(rp->handle));
  585. if (conn) {
  586. conn->clock = le32_to_cpu(rp->clock);
  587. conn->clock_accuracy = le16_to_cpu(rp->accuracy);
  588. }
  589. unlock:
  590. hci_dev_unlock(hdev);
  591. }
  592. static void hci_cc_read_local_amp_info(struct hci_dev *hdev,
  593. struct sk_buff *skb)
  594. {
  595. struct hci_rp_read_local_amp_info *rp = (void *) skb->data;
  596. BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
  597. if (rp->status)
  598. return;
  599. hdev->amp_status = rp->amp_status;
  600. hdev->amp_total_bw = __le32_to_cpu(rp->total_bw);
  601. hdev->amp_max_bw = __le32_to_cpu(rp->max_bw);
  602. hdev->amp_min_latency = __le32_to_cpu(rp->min_latency);
  603. hdev->amp_max_pdu = __le32_to_cpu(rp->max_pdu);
  604. hdev->amp_type = rp->amp_type;
  605. hdev->amp_pal_cap = __le16_to_cpu(rp->pal_cap);
  606. hdev->amp_assoc_size = __le16_to_cpu(rp->max_assoc_size);
  607. hdev->amp_be_flush_to = __le32_to_cpu(rp->be_flush_to);
  608. hdev->amp_max_flush_to = __le32_to_cpu(rp->max_flush_to);
  609. }
  610. static void hci_cc_read_inq_rsp_tx_power(struct hci_dev *hdev,
  611. struct sk_buff *skb)
  612. {
  613. struct hci_rp_read_inq_rsp_tx_power *rp = (void *) skb->data;
  614. BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
  615. if (rp->status)
  616. return;
  617. hdev->inq_tx_power = rp->tx_power;
  618. }
  619. static void hci_cc_pin_code_reply(struct hci_dev *hdev, struct sk_buff *skb)
  620. {
  621. struct hci_rp_pin_code_reply *rp = (void *) skb->data;
  622. struct hci_cp_pin_code_reply *cp;
  623. struct hci_conn *conn;
  624. BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
  625. hci_dev_lock(hdev);
  626. if (hci_dev_test_flag(hdev, HCI_MGMT))
  627. mgmt_pin_code_reply_complete(hdev, &rp->bdaddr, rp->status);
  628. if (rp->status)
  629. goto unlock;
  630. cp = hci_sent_cmd_data(hdev, HCI_OP_PIN_CODE_REPLY);
  631. if (!cp)
  632. goto unlock;
  633. conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &cp->bdaddr);
  634. if (conn)
  635. conn->pin_length = cp->pin_len;
  636. unlock:
  637. hci_dev_unlock(hdev);
  638. }
  639. static void hci_cc_pin_code_neg_reply(struct hci_dev *hdev, struct sk_buff *skb)
  640. {
  641. struct hci_rp_pin_code_neg_reply *rp = (void *) skb->data;
  642. BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
  643. hci_dev_lock(hdev);
  644. if (hci_dev_test_flag(hdev, HCI_MGMT))
  645. mgmt_pin_code_neg_reply_complete(hdev, &rp->bdaddr,
  646. rp->status);
  647. hci_dev_unlock(hdev);
  648. }
  649. static void hci_cc_le_read_buffer_size(struct hci_dev *hdev,
  650. struct sk_buff *skb)
  651. {
  652. struct hci_rp_le_read_buffer_size *rp = (void *) skb->data;
  653. BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
  654. if (rp->status)
  655. return;
  656. hdev->le_mtu = __le16_to_cpu(rp->le_mtu);
  657. hdev->le_pkts = rp->le_max_pkt;
  658. hdev->le_cnt = hdev->le_pkts;
  659. BT_DBG("%s le mtu %d:%d", hdev->name, hdev->le_mtu, hdev->le_pkts);
  660. }
  661. static void hci_cc_le_read_local_features(struct hci_dev *hdev,
  662. struct sk_buff *skb)
  663. {
  664. struct hci_rp_le_read_local_features *rp = (void *) skb->data;
  665. BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
  666. if (rp->status)
  667. return;
  668. memcpy(hdev->le_features, rp->features, 8);
  669. }
  670. static void hci_cc_le_read_adv_tx_power(struct hci_dev *hdev,
  671. struct sk_buff *skb)
  672. {
  673. struct hci_rp_le_read_adv_tx_power *rp = (void *) skb->data;
  674. BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
  675. if (rp->status)
  676. return;
  677. hdev->adv_tx_power = rp->tx_power;
  678. }
  679. static void hci_cc_user_confirm_reply(struct hci_dev *hdev, struct sk_buff *skb)
  680. {
  681. struct hci_rp_user_confirm_reply *rp = (void *) skb->data;
  682. BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
  683. hci_dev_lock(hdev);
  684. if (hci_dev_test_flag(hdev, HCI_MGMT))
  685. mgmt_user_confirm_reply_complete(hdev, &rp->bdaddr, ACL_LINK, 0,
  686. rp->status);
  687. hci_dev_unlock(hdev);
  688. }
  689. static void hci_cc_user_confirm_neg_reply(struct hci_dev *hdev,
  690. struct sk_buff *skb)
  691. {
  692. struct hci_rp_user_confirm_reply *rp = (void *) skb->data;
  693. BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
  694. hci_dev_lock(hdev);
  695. if (hci_dev_test_flag(hdev, HCI_MGMT))
  696. mgmt_user_confirm_neg_reply_complete(hdev, &rp->bdaddr,
  697. ACL_LINK, 0, rp->status);
  698. hci_dev_unlock(hdev);
  699. }
  700. static void hci_cc_user_passkey_reply(struct hci_dev *hdev, struct sk_buff *skb)
  701. {
  702. struct hci_rp_user_confirm_reply *rp = (void *) skb->data;
  703. BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
  704. hci_dev_lock(hdev);
  705. if (hci_dev_test_flag(hdev, HCI_MGMT))
  706. mgmt_user_passkey_reply_complete(hdev, &rp->bdaddr, ACL_LINK,
  707. 0, rp->status);
  708. hci_dev_unlock(hdev);
  709. }
  710. static void hci_cc_user_passkey_neg_reply(struct hci_dev *hdev,
  711. struct sk_buff *skb)
  712. {
  713. struct hci_rp_user_confirm_reply *rp = (void *) skb->data;
  714. BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
  715. hci_dev_lock(hdev);
  716. if (hci_dev_test_flag(hdev, HCI_MGMT))
  717. mgmt_user_passkey_neg_reply_complete(hdev, &rp->bdaddr,
  718. ACL_LINK, 0, rp->status);
  719. hci_dev_unlock(hdev);
  720. }
  721. static void hci_cc_read_local_oob_data(struct hci_dev *hdev,
  722. struct sk_buff *skb)
  723. {
  724. struct hci_rp_read_local_oob_data *rp = (void *) skb->data;
  725. BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
  726. }
  727. static void hci_cc_read_local_oob_ext_data(struct hci_dev *hdev,
  728. struct sk_buff *skb)
  729. {
  730. struct hci_rp_read_local_oob_ext_data *rp = (void *) skb->data;
  731. BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
  732. }
  733. static void hci_cc_le_set_random_addr(struct hci_dev *hdev, struct sk_buff *skb)
  734. {
  735. __u8 status = *((__u8 *) skb->data);
  736. bdaddr_t *sent;
  737. BT_DBG("%s status 0x%2.2x", hdev->name, status);
  738. if (status)
  739. return;
  740. sent = hci_sent_cmd_data(hdev, HCI_OP_LE_SET_RANDOM_ADDR);
  741. if (!sent)
  742. return;
  743. hci_dev_lock(hdev);
  744. bacpy(&hdev->random_addr, sent);
  745. hci_dev_unlock(hdev);
  746. }
  747. static void hci_cc_le_set_default_phy(struct hci_dev *hdev, struct sk_buff *skb)
  748. {
  749. __u8 status = *((__u8 *) skb->data);
  750. struct hci_cp_le_set_default_phy *cp;
  751. BT_DBG("%s status 0x%2.2x", hdev->name, status);
  752. if (status)
  753. return;
  754. cp = hci_sent_cmd_data(hdev, HCI_OP_LE_SET_DEFAULT_PHY);
  755. if (!cp)
  756. return;
  757. hci_dev_lock(hdev);
  758. hdev->le_tx_def_phys = cp->tx_phys;
  759. hdev->le_rx_def_phys = cp->rx_phys;
  760. hci_dev_unlock(hdev);
  761. }
  762. static void hci_cc_le_set_adv_set_random_addr(struct hci_dev *hdev,
  763. struct sk_buff *skb)
  764. {
  765. __u8 status = *((__u8 *) skb->data);
  766. struct hci_cp_le_set_adv_set_rand_addr *cp;
  767. struct adv_info *adv_instance;
  768. if (status)
  769. return;
  770. cp = hci_sent_cmd_data(hdev, HCI_OP_LE_SET_ADV_SET_RAND_ADDR);
  771. if (!cp)
  772. return;
  773. hci_dev_lock(hdev);
  774. if (!hdev->cur_adv_instance) {
  775. /* Store in hdev for instance 0 (Set adv and Directed advs) */
  776. bacpy(&hdev->random_addr, &cp->bdaddr);
  777. } else {
  778. adv_instance = hci_find_adv_instance(hdev,
  779. hdev->cur_adv_instance);
  780. if (adv_instance)
  781. bacpy(&adv_instance->random_addr, &cp->bdaddr);
  782. }
  783. hci_dev_unlock(hdev);
  784. }
  785. static void hci_cc_le_set_adv_enable(struct hci_dev *hdev, struct sk_buff *skb)
  786. {
  787. __u8 *sent, status = *((__u8 *) skb->data);
  788. BT_DBG("%s status 0x%2.2x", hdev->name, status);
  789. if (status)
  790. return;
  791. sent = hci_sent_cmd_data(hdev, HCI_OP_LE_SET_ADV_ENABLE);
  792. if (!sent)
  793. return;
  794. hci_dev_lock(hdev);
  795. /* If we're doing connection initiation as peripheral. Set a
  796. * timeout in case something goes wrong.
  797. */
  798. if (*sent) {
  799. struct hci_conn *conn;
  800. hci_dev_set_flag(hdev, HCI_LE_ADV);
  801. conn = hci_lookup_le_connect(hdev);
  802. if (conn)
  803. queue_delayed_work(hdev->workqueue,
  804. &conn->le_conn_timeout,
  805. conn->conn_timeout);
  806. } else {
  807. hci_dev_clear_flag(hdev, HCI_LE_ADV);
  808. }
  809. hci_dev_unlock(hdev);
  810. }
  811. static void hci_cc_le_set_ext_adv_enable(struct hci_dev *hdev,
  812. struct sk_buff *skb)
  813. {
  814. struct hci_cp_le_set_ext_adv_enable *cp;
  815. __u8 status = *((__u8 *) skb->data);
  816. BT_DBG("%s status 0x%2.2x", hdev->name, status);
  817. if (status)
  818. return;
  819. cp = hci_sent_cmd_data(hdev, HCI_OP_LE_SET_EXT_ADV_ENABLE);
  820. if (!cp)
  821. return;
  822. hci_dev_lock(hdev);
  823. if (cp->enable) {
  824. struct hci_conn *conn;
  825. hci_dev_set_flag(hdev, HCI_LE_ADV);
  826. conn = hci_lookup_le_connect(hdev);
  827. if (conn)
  828. queue_delayed_work(hdev->workqueue,
  829. &conn->le_conn_timeout,
  830. conn->conn_timeout);
  831. } else {
  832. hci_dev_clear_flag(hdev, HCI_LE_ADV);
  833. }
  834. hci_dev_unlock(hdev);
  835. }
  836. static void hci_cc_le_set_scan_param(struct hci_dev *hdev, struct sk_buff *skb)
  837. {
  838. struct hci_cp_le_set_scan_param *cp;
  839. __u8 status = *((__u8 *) skb->data);
  840. BT_DBG("%s status 0x%2.2x", hdev->name, status);
  841. if (status)
  842. return;
  843. cp = hci_sent_cmd_data(hdev, HCI_OP_LE_SET_SCAN_PARAM);
  844. if (!cp)
  845. return;
  846. hci_dev_lock(hdev);
  847. hdev->le_scan_type = cp->type;
  848. hci_dev_unlock(hdev);
  849. }
  850. static void hci_cc_le_set_ext_scan_param(struct hci_dev *hdev,
  851. struct sk_buff *skb)
  852. {
  853. struct hci_cp_le_set_ext_scan_params *cp;
  854. __u8 status = *((__u8 *) skb->data);
  855. struct hci_cp_le_scan_phy_params *phy_param;
  856. BT_DBG("%s status 0x%2.2x", hdev->name, status);
  857. if (status)
  858. return;
  859. cp = hci_sent_cmd_data(hdev, HCI_OP_LE_SET_EXT_SCAN_PARAMS);
  860. if (!cp)
  861. return;
  862. phy_param = (void *)cp->data;
  863. hci_dev_lock(hdev);
  864. hdev->le_scan_type = phy_param->type;
  865. hci_dev_unlock(hdev);
  866. }
  867. static bool has_pending_adv_report(struct hci_dev *hdev)
  868. {
  869. struct discovery_state *d = &hdev->discovery;
  870. return bacmp(&d->last_adv_addr, BDADDR_ANY);
  871. }
  872. static void clear_pending_adv_report(struct hci_dev *hdev)
  873. {
  874. struct discovery_state *d = &hdev->discovery;
  875. bacpy(&d->last_adv_addr, BDADDR_ANY);
  876. d->last_adv_data_len = 0;
  877. }
  878. static void store_pending_adv_report(struct hci_dev *hdev, bdaddr_t *bdaddr,
  879. u8 bdaddr_type, s8 rssi, u32 flags,
  880. u8 *data, u8 len)
  881. {
  882. struct discovery_state *d = &hdev->discovery;
  883. bacpy(&d->last_adv_addr, bdaddr);
  884. d->last_adv_addr_type = bdaddr_type;
  885. d->last_adv_rssi = rssi;
  886. d->last_adv_flags = flags;
  887. memcpy(d->last_adv_data, data, len);
  888. d->last_adv_data_len = len;
  889. }
  890. static void le_set_scan_enable_complete(struct hci_dev *hdev, u8 enable)
  891. {
  892. hci_dev_lock(hdev);
  893. switch (enable) {
  894. case LE_SCAN_ENABLE:
  895. hci_dev_set_flag(hdev, HCI_LE_SCAN);
  896. if (hdev->le_scan_type == LE_SCAN_ACTIVE)
  897. clear_pending_adv_report(hdev);
  898. break;
  899. case LE_SCAN_DISABLE:
  900. /* We do this here instead of when setting DISCOVERY_STOPPED
  901. * since the latter would potentially require waiting for
  902. * inquiry to stop too.
  903. */
  904. if (has_pending_adv_report(hdev)) {
  905. struct discovery_state *d = &hdev->discovery;
  906. mgmt_device_found(hdev, &d->last_adv_addr, LE_LINK,
  907. d->last_adv_addr_type, NULL,
  908. d->last_adv_rssi, d->last_adv_flags,
  909. d->last_adv_data,
  910. d->last_adv_data_len, NULL, 0);
  911. }
  912. /* Cancel this timer so that we don't try to disable scanning
  913. * when it's already disabled.
  914. */
  915. cancel_delayed_work(&hdev->le_scan_disable);
  916. hci_dev_clear_flag(hdev, HCI_LE_SCAN);
  917. /* The HCI_LE_SCAN_INTERRUPTED flag indicates that we
  918. * interrupted scanning due to a connect request. Mark
  919. * therefore discovery as stopped. If this was not
  920. * because of a connect request advertising might have
  921. * been disabled because of active scanning, so
  922. * re-enable it again if necessary.
  923. */
  924. if (hci_dev_test_and_clear_flag(hdev, HCI_LE_SCAN_INTERRUPTED))
  925. hci_discovery_set_state(hdev, DISCOVERY_STOPPED);
  926. else if (!hci_dev_test_flag(hdev, HCI_LE_ADV) &&
  927. hdev->discovery.state == DISCOVERY_FINDING)
  928. hci_req_reenable_advertising(hdev);
  929. break;
  930. default:
  931. bt_dev_err(hdev, "use of reserved LE_Scan_Enable param %d",
  932. enable);
  933. break;
  934. }
  935. hci_dev_unlock(hdev);
  936. }
  937. static void hci_cc_le_set_scan_enable(struct hci_dev *hdev,
  938. struct sk_buff *skb)
  939. {
  940. struct hci_cp_le_set_scan_enable *cp;
  941. __u8 status = *((__u8 *) skb->data);
  942. BT_DBG("%s status 0x%2.2x", hdev->name, status);
  943. if (status)
  944. return;
  945. cp = hci_sent_cmd_data(hdev, HCI_OP_LE_SET_SCAN_ENABLE);
  946. if (!cp)
  947. return;
  948. le_set_scan_enable_complete(hdev, cp->enable);
  949. }
  950. static void hci_cc_le_set_ext_scan_enable(struct hci_dev *hdev,
  951. struct sk_buff *skb)
  952. {
  953. struct hci_cp_le_set_ext_scan_enable *cp;
  954. __u8 status = *((__u8 *) skb->data);
  955. BT_DBG("%s status 0x%2.2x", hdev->name, status);
  956. if (status)
  957. return;
  958. cp = hci_sent_cmd_data(hdev, HCI_OP_LE_SET_EXT_SCAN_ENABLE);
  959. if (!cp)
  960. return;
  961. le_set_scan_enable_complete(hdev, cp->enable);
  962. }
  963. static void hci_cc_le_read_num_adv_sets(struct hci_dev *hdev,
  964. struct sk_buff *skb)
  965. {
  966. struct hci_rp_le_read_num_supported_adv_sets *rp = (void *) skb->data;
  967. BT_DBG("%s status 0x%2.2x No of Adv sets %u", hdev->name, rp->status,
  968. rp->num_of_sets);
  969. if (rp->status)
  970. return;
  971. hdev->le_num_of_adv_sets = rp->num_of_sets;
  972. }
  973. static void hci_cc_le_read_white_list_size(struct hci_dev *hdev,
  974. struct sk_buff *skb)
  975. {
  976. struct hci_rp_le_read_white_list_size *rp = (void *) skb->data;
  977. BT_DBG("%s status 0x%2.2x size %u", hdev->name, rp->status, rp->size);
  978. if (rp->status)
  979. return;
  980. hdev->le_white_list_size = rp->size;
  981. }
  982. static void hci_cc_le_clear_white_list(struct hci_dev *hdev,
  983. struct sk_buff *skb)
  984. {
  985. __u8 status = *((__u8 *) skb->data);
  986. BT_DBG("%s status 0x%2.2x", hdev->name, status);
  987. if (status)
  988. return;
  989. hci_bdaddr_list_clear(&hdev->le_white_list);
  990. }
  991. static void hci_cc_le_add_to_white_list(struct hci_dev *hdev,
  992. struct sk_buff *skb)
  993. {
  994. struct hci_cp_le_add_to_white_list *sent;
  995. __u8 status = *((__u8 *) skb->data);
  996. BT_DBG("%s status 0x%2.2x", hdev->name, status);
  997. if (status)
  998. return;
  999. sent = hci_sent_cmd_data(hdev, HCI_OP_LE_ADD_TO_WHITE_LIST);
  1000. if (!sent)
  1001. return;
  1002. hci_bdaddr_list_add(&hdev->le_white_list, &sent->bdaddr,
  1003. sent->bdaddr_type);
  1004. }
  1005. static void hci_cc_le_del_from_white_list(struct hci_dev *hdev,
  1006. struct sk_buff *skb)
  1007. {
  1008. struct hci_cp_le_del_from_white_list *sent;
  1009. __u8 status = *((__u8 *) skb->data);
  1010. BT_DBG("%s status 0x%2.2x", hdev->name, status);
  1011. if (status)
  1012. return;
  1013. sent = hci_sent_cmd_data(hdev, HCI_OP_LE_DEL_FROM_WHITE_LIST);
  1014. if (!sent)
  1015. return;
  1016. hci_bdaddr_list_del(&hdev->le_white_list, &sent->bdaddr,
  1017. sent->bdaddr_type);
  1018. }
  1019. static void hci_cc_le_read_supported_states(struct hci_dev *hdev,
  1020. struct sk_buff *skb)
  1021. {
  1022. struct hci_rp_le_read_supported_states *rp = (void *) skb->data;
  1023. BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
  1024. if (rp->status)
  1025. return;
  1026. memcpy(hdev->le_states, rp->le_states, 8);
  1027. }
  1028. static void hci_cc_le_read_def_data_len(struct hci_dev *hdev,
  1029. struct sk_buff *skb)
  1030. {
  1031. struct hci_rp_le_read_def_data_len *rp = (void *) skb->data;
  1032. BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
  1033. if (rp->status)
  1034. return;
  1035. hdev->le_def_tx_len = le16_to_cpu(rp->tx_len);
  1036. hdev->le_def_tx_time = le16_to_cpu(rp->tx_time);
  1037. }
  1038. static void hci_cc_le_write_def_data_len(struct hci_dev *hdev,
  1039. struct sk_buff *skb)
  1040. {
  1041. struct hci_cp_le_write_def_data_len *sent;
  1042. __u8 status = *((__u8 *) skb->data);
  1043. BT_DBG("%s status 0x%2.2x", hdev->name, status);
  1044. if (status)
  1045. return;
  1046. sent = hci_sent_cmd_data(hdev, HCI_OP_LE_WRITE_DEF_DATA_LEN);
  1047. if (!sent)
  1048. return;
  1049. hdev->le_def_tx_len = le16_to_cpu(sent->tx_len);
  1050. hdev->le_def_tx_time = le16_to_cpu(sent->tx_time);
  1051. }
  1052. static void hci_cc_le_clear_resolv_list(struct hci_dev *hdev,
  1053. struct sk_buff *skb)
  1054. {
  1055. __u8 status = *((__u8 *) skb->data);
  1056. BT_DBG("%s status 0x%2.2x", hdev->name, status);
  1057. if (status)
  1058. return;
  1059. hci_bdaddr_list_clear(&hdev->le_resolv_list);
  1060. }
  1061. static void hci_cc_le_read_resolv_list_size(struct hci_dev *hdev,
  1062. struct sk_buff *skb)
  1063. {
  1064. struct hci_rp_le_read_resolv_list_size *rp = (void *) skb->data;
  1065. BT_DBG("%s status 0x%2.2x size %u", hdev->name, rp->status, rp->size);
  1066. if (rp->status)
  1067. return;
  1068. hdev->le_resolv_list_size = rp->size;
  1069. }
  1070. static void hci_cc_le_set_addr_resolution_enable(struct hci_dev *hdev,
  1071. struct sk_buff *skb)
  1072. {
  1073. __u8 *sent, status = *((__u8 *) skb->data);
  1074. BT_DBG("%s status 0x%2.2x", hdev->name, status);
  1075. if (status)
  1076. return;
  1077. sent = hci_sent_cmd_data(hdev, HCI_OP_LE_SET_ADDR_RESOLV_ENABLE);
  1078. if (!sent)
  1079. return;
  1080. hci_dev_lock(hdev);
  1081. if (*sent)
  1082. hci_dev_set_flag(hdev, HCI_LL_RPA_RESOLUTION);
  1083. else
  1084. hci_dev_clear_flag(hdev, HCI_LL_RPA_RESOLUTION);
  1085. hci_dev_unlock(hdev);
  1086. }
  1087. static void hci_cc_le_read_max_data_len(struct hci_dev *hdev,
  1088. struct sk_buff *skb)
  1089. {
  1090. struct hci_rp_le_read_max_data_len *rp = (void *) skb->data;
  1091. BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
  1092. if (rp->status)
  1093. return;
  1094. hdev->le_max_tx_len = le16_to_cpu(rp->tx_len);
  1095. hdev->le_max_tx_time = le16_to_cpu(rp->tx_time);
  1096. hdev->le_max_rx_len = le16_to_cpu(rp->rx_len);
  1097. hdev->le_max_rx_time = le16_to_cpu(rp->rx_time);
  1098. }
  1099. static void hci_cc_write_le_host_supported(struct hci_dev *hdev,
  1100. struct sk_buff *skb)
  1101. {
  1102. struct hci_cp_write_le_host_supported *sent;
  1103. __u8 status = *((__u8 *) skb->data);
  1104. BT_DBG("%s status 0x%2.2x", hdev->name, status);
  1105. if (status)
  1106. return;
  1107. sent = hci_sent_cmd_data(hdev, HCI_OP_WRITE_LE_HOST_SUPPORTED);
  1108. if (!sent)
  1109. return;
  1110. hci_dev_lock(hdev);
  1111. if (sent->le) {
  1112. hdev->features[1][0] |= LMP_HOST_LE;
  1113. hci_dev_set_flag(hdev, HCI_LE_ENABLED);
  1114. } else {
  1115. hdev->features[1][0] &= ~LMP_HOST_LE;
  1116. hci_dev_clear_flag(hdev, HCI_LE_ENABLED);
  1117. hci_dev_clear_flag(hdev, HCI_ADVERTISING);
  1118. }
  1119. if (sent->simul)
  1120. hdev->features[1][0] |= LMP_HOST_LE_BREDR;
  1121. else
  1122. hdev->features[1][0] &= ~LMP_HOST_LE_BREDR;
  1123. hci_dev_unlock(hdev);
  1124. }
  1125. static void hci_cc_set_adv_param(struct hci_dev *hdev, struct sk_buff *skb)
  1126. {
  1127. struct hci_cp_le_set_adv_param *cp;
  1128. u8 status = *((u8 *) skb->data);
  1129. BT_DBG("%s status 0x%2.2x", hdev->name, status);
  1130. if (status)
  1131. return;
  1132. cp = hci_sent_cmd_data(hdev, HCI_OP_LE_SET_ADV_PARAM);
  1133. if (!cp)
  1134. return;
  1135. hci_dev_lock(hdev);
  1136. hdev->adv_addr_type = cp->own_address_type;
  1137. hci_dev_unlock(hdev);
  1138. }
  1139. static void hci_cc_set_ext_adv_param(struct hci_dev *hdev, struct sk_buff *skb)
  1140. {
  1141. struct hci_rp_le_set_ext_adv_params *rp = (void *) skb->data;
  1142. struct hci_cp_le_set_ext_adv_params *cp;
  1143. struct adv_info *adv_instance;
  1144. BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
  1145. if (rp->status)
  1146. return;
  1147. cp = hci_sent_cmd_data(hdev, HCI_OP_LE_SET_EXT_ADV_PARAMS);
  1148. if (!cp)
  1149. return;
  1150. hci_dev_lock(hdev);
  1151. hdev->adv_addr_type = cp->own_addr_type;
  1152. if (!hdev->cur_adv_instance) {
  1153. /* Store in hdev for instance 0 */
  1154. hdev->adv_tx_power = rp->tx_power;
  1155. } else {
  1156. adv_instance = hci_find_adv_instance(hdev,
  1157. hdev->cur_adv_instance);
  1158. if (adv_instance)
  1159. adv_instance->tx_power = rp->tx_power;
  1160. }
  1161. /* Update adv data as tx power is known now */
  1162. hci_req_update_adv_data(hdev, hdev->cur_adv_instance);
  1163. hci_dev_unlock(hdev);
  1164. }
  1165. static void hci_cc_read_rssi(struct hci_dev *hdev, struct sk_buff *skb)
  1166. {
  1167. struct hci_rp_read_rssi *rp = (void *) skb->data;
  1168. struct hci_conn *conn;
  1169. BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
  1170. if (rp->status)
  1171. return;
  1172. hci_dev_lock(hdev);
  1173. conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(rp->handle));
  1174. if (conn)
  1175. conn->rssi = rp->rssi;
  1176. hci_dev_unlock(hdev);
  1177. }
  1178. static void hci_cc_read_tx_power(struct hci_dev *hdev, struct sk_buff *skb)
  1179. {
  1180. struct hci_cp_read_tx_power *sent;
  1181. struct hci_rp_read_tx_power *rp = (void *) skb->data;
  1182. struct hci_conn *conn;
  1183. BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
  1184. if (rp->status)
  1185. return;
  1186. sent = hci_sent_cmd_data(hdev, HCI_OP_READ_TX_POWER);
  1187. if (!sent)
  1188. return;
  1189. hci_dev_lock(hdev);
  1190. conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(rp->handle));
  1191. if (!conn)
  1192. goto unlock;
  1193. switch (sent->type) {
  1194. case 0x00:
  1195. conn->tx_power = rp->tx_power;
  1196. break;
  1197. case 0x01:
  1198. conn->max_tx_power = rp->tx_power;
  1199. break;
  1200. }
  1201. unlock:
  1202. hci_dev_unlock(hdev);
  1203. }
  1204. static void hci_cc_write_ssp_debug_mode(struct hci_dev *hdev, struct sk_buff *skb)
  1205. {
  1206. u8 status = *((u8 *) skb->data);
  1207. u8 *mode;
  1208. BT_DBG("%s status 0x%2.2x", hdev->name, status);
  1209. if (status)
  1210. return;
  1211. mode = hci_sent_cmd_data(hdev, HCI_OP_WRITE_SSP_DEBUG_MODE);
  1212. if (mode)
  1213. hdev->ssp_debug_mode = *mode;
  1214. }
  1215. static void hci_cs_inquiry(struct hci_dev *hdev, __u8 status)
  1216. {
  1217. BT_DBG("%s status 0x%2.2x", hdev->name, status);
  1218. if (status) {
  1219. hci_conn_check_pending(hdev);
  1220. return;
  1221. }
  1222. set_bit(HCI_INQUIRY, &hdev->flags);
  1223. }
  1224. static void hci_cs_create_conn(struct hci_dev *hdev, __u8 status)
  1225. {
  1226. struct hci_cp_create_conn *cp;
  1227. struct hci_conn *conn;
  1228. BT_DBG("%s status 0x%2.2x", hdev->name, status);
  1229. cp = hci_sent_cmd_data(hdev, HCI_OP_CREATE_CONN);
  1230. if (!cp)
  1231. return;
  1232. hci_dev_lock(hdev);
  1233. conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &cp->bdaddr);
  1234. BT_DBG("%s bdaddr %pMR hcon %p", hdev->name, &cp->bdaddr, conn);
  1235. if (status) {
  1236. if (conn && conn->state == BT_CONNECT) {
  1237. if (status != 0x0c || conn->attempt > 2) {
  1238. conn->state = BT_CLOSED;
  1239. hci_connect_cfm(conn, status);
  1240. hci_conn_del(conn);
  1241. } else
  1242. conn->state = BT_CONNECT2;
  1243. }
  1244. } else {
  1245. if (!conn) {
  1246. conn = hci_conn_add(hdev, ACL_LINK, &cp->bdaddr,
  1247. HCI_ROLE_MASTER);
  1248. if (!conn)
  1249. bt_dev_err(hdev, "no memory for new connection");
  1250. }
  1251. }
  1252. hci_dev_unlock(hdev);
  1253. }
  1254. static void hci_cs_add_sco(struct hci_dev *hdev, __u8 status)
  1255. {
  1256. struct hci_cp_add_sco *cp;
  1257. struct hci_conn *acl, *sco;
  1258. __u16 handle;
  1259. BT_DBG("%s status 0x%2.2x", hdev->name, status);
  1260. if (!status)
  1261. return;
  1262. cp = hci_sent_cmd_data(hdev, HCI_OP_ADD_SCO);
  1263. if (!cp)
  1264. return;
  1265. handle = __le16_to_cpu(cp->handle);
  1266. BT_DBG("%s handle 0x%4.4x", hdev->name, handle);
  1267. hci_dev_lock(hdev);
  1268. acl = hci_conn_hash_lookup_handle(hdev, handle);
  1269. if (acl) {
  1270. sco = acl->link;
  1271. if (sco) {
  1272. sco->state = BT_CLOSED;
  1273. hci_connect_cfm(sco, status);
  1274. hci_conn_del(sco);
  1275. }
  1276. }
  1277. hci_dev_unlock(hdev);
  1278. }
  1279. static void hci_cs_auth_requested(struct hci_dev *hdev, __u8 status)
  1280. {
  1281. struct hci_cp_auth_requested *cp;
  1282. struct hci_conn *conn;
  1283. BT_DBG("%s status 0x%2.2x", hdev->name, status);
  1284. if (!status)
  1285. return;
  1286. cp = hci_sent_cmd_data(hdev, HCI_OP_AUTH_REQUESTED);
  1287. if (!cp)
  1288. return;
  1289. hci_dev_lock(hdev);
  1290. conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(cp->handle));
  1291. if (conn) {
  1292. if (conn->state == BT_CONFIG) {
  1293. hci_connect_cfm(conn, status);
  1294. hci_conn_drop(conn);
  1295. }
  1296. }
  1297. hci_dev_unlock(hdev);
  1298. }
  1299. static void hci_cs_set_conn_encrypt(struct hci_dev *hdev, __u8 status)
  1300. {
  1301. struct hci_cp_set_conn_encrypt *cp;
  1302. struct hci_conn *conn;
  1303. BT_DBG("%s status 0x%2.2x", hdev->name, status);
  1304. if (!status)
  1305. return;
  1306. cp = hci_sent_cmd_data(hdev, HCI_OP_SET_CONN_ENCRYPT);
  1307. if (!cp)
  1308. return;
  1309. hci_dev_lock(hdev);
  1310. conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(cp->handle));
  1311. if (conn) {
  1312. if (conn->state == BT_CONFIG) {
  1313. hci_connect_cfm(conn, status);
  1314. hci_conn_drop(conn);
  1315. }
  1316. }
  1317. hci_dev_unlock(hdev);
  1318. }
  1319. static int hci_outgoing_auth_needed(struct hci_dev *hdev,
  1320. struct hci_conn *conn)
  1321. {
  1322. if (conn->state != BT_CONFIG || !conn->out)
  1323. return 0;
  1324. if (conn->pending_sec_level == BT_SECURITY_SDP)
  1325. return 0;
  1326. /* Only request authentication for SSP connections or non-SSP
  1327. * devices with sec_level MEDIUM or HIGH or if MITM protection
  1328. * is requested.
  1329. */
  1330. if (!hci_conn_ssp_enabled(conn) && !(conn->auth_type & 0x01) &&
  1331. conn->pending_sec_level != BT_SECURITY_FIPS &&
  1332. conn->pending_sec_level != BT_SECURITY_HIGH &&
  1333. conn->pending_sec_level != BT_SECURITY_MEDIUM)
  1334. return 0;
  1335. return 1;
  1336. }
  1337. static int hci_resolve_name(struct hci_dev *hdev,
  1338. struct inquiry_entry *e)
  1339. {
  1340. struct hci_cp_remote_name_req cp;
  1341. memset(&cp, 0, sizeof(cp));
  1342. bacpy(&cp.bdaddr, &e->data.bdaddr);
  1343. cp.pscan_rep_mode = e->data.pscan_rep_mode;
  1344. cp.pscan_mode = e->data.pscan_mode;
  1345. cp.clock_offset = e->data.clock_offset;
  1346. return hci_send_cmd(hdev, HCI_OP_REMOTE_NAME_REQ, sizeof(cp), &cp);
  1347. }
  1348. static bool hci_resolve_next_name(struct hci_dev *hdev)
  1349. {
  1350. struct discovery_state *discov = &hdev->discovery;
  1351. struct inquiry_entry *e;
  1352. if (list_empty(&discov->resolve))
  1353. return false;
  1354. e = hci_inquiry_cache_lookup_resolve(hdev, BDADDR_ANY, NAME_NEEDED);
  1355. if (!e)
  1356. return false;
  1357. if (hci_resolve_name(hdev, e) == 0) {
  1358. e->name_state = NAME_PENDING;
  1359. return true;
  1360. }
  1361. return false;
  1362. }
  1363. static void hci_check_pending_name(struct hci_dev *hdev, struct hci_conn *conn,
  1364. bdaddr_t *bdaddr, u8 *name, u8 name_len)
  1365. {
  1366. struct discovery_state *discov = &hdev->discovery;
  1367. struct inquiry_entry *e;
  1368. /* Update the mgmt connected state if necessary. Be careful with
  1369. * conn objects that exist but are not (yet) connected however.
  1370. * Only those in BT_CONFIG or BT_CONNECTED states can be
  1371. * considered connected.
  1372. */
  1373. if (conn &&
  1374. (conn->state == BT_CONFIG || conn->state == BT_CONNECTED) &&
  1375. !test_and_set_bit(HCI_CONN_MGMT_CONNECTED, &conn->flags))
  1376. mgmt_device_connected(hdev, conn, 0, name, name_len);
  1377. if (discov->state == DISCOVERY_STOPPED)
  1378. return;
  1379. if (discov->state == DISCOVERY_STOPPING)
  1380. goto discov_complete;
  1381. if (discov->state != DISCOVERY_RESOLVING)
  1382. return;
  1383. e = hci_inquiry_cache_lookup_resolve(hdev, bdaddr, NAME_PENDING);
  1384. /* If the device was not found in a list of found devices names of which
  1385. * are pending. there is no need to continue resolving a next name as it
  1386. * will be done upon receiving another Remote Name Request Complete
  1387. * Event */
  1388. if (!e)
  1389. return;
  1390. list_del(&e->list);
  1391. if (name) {
  1392. e->name_state = NAME_KNOWN;
  1393. mgmt_remote_name(hdev, bdaddr, ACL_LINK, 0x00,
  1394. e->data.rssi, name, name_len);
  1395. } else {
  1396. e->name_state = NAME_NOT_KNOWN;
  1397. }
  1398. if (hci_resolve_next_name(hdev))
  1399. return;
  1400. discov_complete:
  1401. hci_discovery_set_state(hdev, DISCOVERY_STOPPED);
  1402. }
  1403. static void hci_cs_remote_name_req(struct hci_dev *hdev, __u8 status)
  1404. {
  1405. struct hci_cp_remote_name_req *cp;
  1406. struct hci_conn *conn;
  1407. BT_DBG("%s status 0x%2.2x", hdev->name, status);
  1408. /* If successful wait for the name req complete event before
  1409. * checking for the need to do authentication */
  1410. if (!status)
  1411. return;
  1412. cp = hci_sent_cmd_data(hdev, HCI_OP_REMOTE_NAME_REQ);
  1413. if (!cp)
  1414. return;
  1415. hci_dev_lock(hdev);
  1416. conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &cp->bdaddr);
  1417. if (hci_dev_test_flag(hdev, HCI_MGMT))
  1418. hci_check_pending_name(hdev, conn, &cp->bdaddr, NULL, 0);
  1419. if (!conn)
  1420. goto unlock;
  1421. if (!hci_outgoing_auth_needed(hdev, conn))
  1422. goto unlock;
  1423. if (!test_and_set_bit(HCI_CONN_AUTH_PEND, &conn->flags)) {
  1424. struct hci_cp_auth_requested auth_cp;
  1425. set_bit(HCI_CONN_AUTH_INITIATOR, &conn->flags);
  1426. auth_cp.handle = __cpu_to_le16(conn->handle);
  1427. hci_send_cmd(hdev, HCI_OP_AUTH_REQUESTED,
  1428. sizeof(auth_cp), &auth_cp);
  1429. }
  1430. unlock:
  1431. hci_dev_unlock(hdev);
  1432. }
  1433. static void hci_cs_read_remote_features(struct hci_dev *hdev, __u8 status)
  1434. {
  1435. struct hci_cp_read_remote_features *cp;
  1436. struct hci_conn *conn;
  1437. BT_DBG("%s status 0x%2.2x", hdev->name, status);
  1438. if (!status)
  1439. return;
  1440. cp = hci_sent_cmd_data(hdev, HCI_OP_READ_REMOTE_FEATURES);
  1441. if (!cp)
  1442. return;
  1443. hci_dev_lock(hdev);
  1444. conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(cp->handle));
  1445. if (conn) {
  1446. if (conn->state == BT_CONFIG) {
  1447. hci_connect_cfm(conn, status);
  1448. hci_conn_drop(conn);
  1449. }
  1450. }
  1451. hci_dev_unlock(hdev);
  1452. }
  1453. static void hci_cs_read_remote_ext_features(struct hci_dev *hdev, __u8 status)
  1454. {
  1455. struct hci_cp_read_remote_ext_features *cp;
  1456. struct hci_conn *conn;
  1457. BT_DBG("%s status 0x%2.2x", hdev->name, status);
  1458. if (!status)
  1459. return;
  1460. cp = hci_sent_cmd_data(hdev, HCI_OP_READ_REMOTE_EXT_FEATURES);
  1461. if (!cp)
  1462. return;
  1463. hci_dev_lock(hdev);
  1464. conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(cp->handle));
  1465. if (conn) {
  1466. if (conn->state == BT_CONFIG) {
  1467. hci_connect_cfm(conn, status);
  1468. hci_conn_drop(conn);
  1469. }
  1470. }
  1471. hci_dev_unlock(hdev);
  1472. }
  1473. static void hci_cs_setup_sync_conn(struct hci_dev *hdev, __u8 status)
  1474. {
  1475. struct hci_cp_setup_sync_conn *cp;
  1476. struct hci_conn *acl, *sco;
  1477. __u16 handle;
  1478. BT_DBG("%s status 0x%2.2x", hdev->name, status);
  1479. if (!status)
  1480. return;
  1481. cp = hci_sent_cmd_data(hdev, HCI_OP_SETUP_SYNC_CONN);
  1482. if (!cp)
  1483. return;
  1484. handle = __le16_to_cpu(cp->handle);
  1485. BT_DBG("%s handle 0x%4.4x", hdev->name, handle);
  1486. hci_dev_lock(hdev);
  1487. acl = hci_conn_hash_lookup_handle(hdev, handle);
  1488. if (acl) {
  1489. sco = acl->link;
  1490. if (sco) {
  1491. sco->state = BT_CLOSED;
  1492. hci_connect_cfm(sco, status);
  1493. hci_conn_del(sco);
  1494. }
  1495. }
  1496. hci_dev_unlock(hdev);
  1497. }
  1498. static void hci_cs_sniff_mode(struct hci_dev *hdev, __u8 status)
  1499. {
  1500. struct hci_cp_sniff_mode *cp;
  1501. struct hci_conn *conn;
  1502. BT_DBG("%s status 0x%2.2x", hdev->name, status);
  1503. if (!status)
  1504. return;
  1505. cp = hci_sent_cmd_data(hdev, HCI_OP_SNIFF_MODE);
  1506. if (!cp)
  1507. return;
  1508. hci_dev_lock(hdev);
  1509. conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(cp->handle));
  1510. if (conn) {
  1511. clear_bit(HCI_CONN_MODE_CHANGE_PEND, &conn->flags);
  1512. if (test_and_clear_bit(HCI_CONN_SCO_SETUP_PEND, &conn->flags))
  1513. hci_sco_setup(conn, status);
  1514. }
  1515. hci_dev_unlock(hdev);
  1516. }
  1517. static void hci_cs_exit_sniff_mode(struct hci_dev *hdev, __u8 status)
  1518. {
  1519. struct hci_cp_exit_sniff_mode *cp;
  1520. struct hci_conn *conn;
  1521. BT_DBG("%s status 0x%2.2x", hdev->name, status);
  1522. if (!status)
  1523. return;
  1524. cp = hci_sent_cmd_data(hdev, HCI_OP_EXIT_SNIFF_MODE);
  1525. if (!cp)
  1526. return;
  1527. hci_dev_lock(hdev);
  1528. conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(cp->handle));
  1529. if (conn) {
  1530. clear_bit(HCI_CONN_MODE_CHANGE_PEND, &conn->flags);
  1531. if (test_and_clear_bit(HCI_CONN_SCO_SETUP_PEND, &conn->flags))
  1532. hci_sco_setup(conn, status);
  1533. }
  1534. hci_dev_unlock(hdev);
  1535. }
  1536. static void hci_cs_disconnect(struct hci_dev *hdev, u8 status)
  1537. {
  1538. struct hci_cp_disconnect *cp;
  1539. struct hci_conn *conn;
  1540. if (!status)
  1541. return;
  1542. cp = hci_sent_cmd_data(hdev, HCI_OP_DISCONNECT);
  1543. if (!cp)
  1544. return;
  1545. hci_dev_lock(hdev);
  1546. conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(cp->handle));
  1547. if (conn)
  1548. mgmt_disconnect_failed(hdev, &conn->dst, conn->type,
  1549. conn->dst_type, status);
  1550. hci_dev_unlock(hdev);
  1551. }
  1552. static void cs_le_create_conn(struct hci_dev *hdev, bdaddr_t *peer_addr,
  1553. u8 peer_addr_type, u8 own_address_type,
  1554. u8 filter_policy)
  1555. {
  1556. struct hci_conn *conn;
  1557. conn = hci_conn_hash_lookup_le(hdev, peer_addr,
  1558. peer_addr_type);
  1559. if (!conn)
  1560. return;
  1561. /* Store the initiator and responder address information which
  1562. * is needed for SMP. These values will not change during the
  1563. * lifetime of the connection.
  1564. */
  1565. conn->init_addr_type = own_address_type;
  1566. if (own_address_type == ADDR_LE_DEV_RANDOM)
  1567. bacpy(&conn->init_addr, &hdev->random_addr);
  1568. else
  1569. bacpy(&conn->init_addr, &hdev->bdaddr);
  1570. conn->resp_addr_type = peer_addr_type;
  1571. bacpy(&conn->resp_addr, peer_addr);
  1572. /* We don't want the connection attempt to stick around
  1573. * indefinitely since LE doesn't have a page timeout concept
  1574. * like BR/EDR. Set a timer for any connection that doesn't use
  1575. * the white list for connecting.
  1576. */
  1577. if (filter_policy == HCI_LE_USE_PEER_ADDR)
  1578. queue_delayed_work(conn->hdev->workqueue,
  1579. &conn->le_conn_timeout,
  1580. conn->conn_timeout);
  1581. }
  1582. static void hci_cs_le_create_conn(struct hci_dev *hdev, u8 status)
  1583. {
  1584. struct hci_cp_le_create_conn *cp;
  1585. BT_DBG("%s status 0x%2.2x", hdev->name, status);
  1586. /* All connection failure handling is taken care of by the
  1587. * hci_le_conn_failed function which is triggered by the HCI
  1588. * request completion callbacks used for connecting.
  1589. */
  1590. if (status)
  1591. return;
  1592. cp = hci_sent_cmd_data(hdev, HCI_OP_LE_CREATE_CONN);
  1593. if (!cp)
  1594. return;
  1595. hci_dev_lock(hdev);
  1596. cs_le_create_conn(hdev, &cp->peer_addr, cp->peer_addr_type,
  1597. cp->own_address_type, cp->filter_policy);
  1598. hci_dev_unlock(hdev);
  1599. }
  1600. static void hci_cs_le_ext_create_conn(struct hci_dev *hdev, u8 status)
  1601. {
  1602. struct hci_cp_le_ext_create_conn *cp;
  1603. BT_DBG("%s status 0x%2.2x", hdev->name, status);
  1604. /* All connection failure handling is taken care of by the
  1605. * hci_le_conn_failed function which is triggered by the HCI
  1606. * request completion callbacks used for connecting.
  1607. */
  1608. if (status)
  1609. return;
  1610. cp = hci_sent_cmd_data(hdev, HCI_OP_LE_EXT_CREATE_CONN);
  1611. if (!cp)
  1612. return;
  1613. hci_dev_lock(hdev);
  1614. cs_le_create_conn(hdev, &cp->peer_addr, cp->peer_addr_type,
  1615. cp->own_addr_type, cp->filter_policy);
  1616. hci_dev_unlock(hdev);
  1617. }
  1618. static void hci_cs_le_read_remote_features(struct hci_dev *hdev, u8 status)
  1619. {
  1620. struct hci_cp_le_read_remote_features *cp;
  1621. struct hci_conn *conn;
  1622. BT_DBG("%s status 0x%2.2x", hdev->name, status);
  1623. if (!status)
  1624. return;
  1625. cp = hci_sent_cmd_data(hdev, HCI_OP_LE_READ_REMOTE_FEATURES);
  1626. if (!cp)
  1627. return;
  1628. hci_dev_lock(hdev);
  1629. conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(cp->handle));
  1630. if (conn) {
  1631. if (conn->state == BT_CONFIG) {
  1632. hci_connect_cfm(conn, status);
  1633. hci_conn_drop(conn);
  1634. }
  1635. }
  1636. hci_dev_unlock(hdev);
  1637. }
  1638. static void hci_cs_le_start_enc(struct hci_dev *hdev, u8 status)
  1639. {
  1640. struct hci_cp_le_start_enc *cp;
  1641. struct hci_conn *conn;
  1642. BT_DBG("%s status 0x%2.2x", hdev->name, status);
  1643. if (!status)
  1644. return;
  1645. hci_dev_lock(hdev);
  1646. cp = hci_sent_cmd_data(hdev, HCI_OP_LE_START_ENC);
  1647. if (!cp)
  1648. goto unlock;
  1649. conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(cp->handle));
  1650. if (!conn)
  1651. goto unlock;
  1652. if (conn->state != BT_CONNECTED)
  1653. goto unlock;
  1654. hci_disconnect(conn, HCI_ERROR_AUTH_FAILURE);
  1655. hci_conn_drop(conn);
  1656. unlock:
  1657. hci_dev_unlock(hdev);
  1658. }
  1659. static void hci_cs_switch_role(struct hci_dev *hdev, u8 status)
  1660. {
  1661. struct hci_cp_switch_role *cp;
  1662. struct hci_conn *conn;
  1663. BT_DBG("%s status 0x%2.2x", hdev->name, status);
  1664. if (!status)
  1665. return;
  1666. cp = hci_sent_cmd_data(hdev, HCI_OP_SWITCH_ROLE);
  1667. if (!cp)
  1668. return;
  1669. hci_dev_lock(hdev);
  1670. conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &cp->bdaddr);
  1671. if (conn)
  1672. clear_bit(HCI_CONN_RSWITCH_PEND, &conn->flags);
  1673. hci_dev_unlock(hdev);
  1674. }
  1675. static void hci_inquiry_complete_evt(struct hci_dev *hdev, struct sk_buff *skb)
  1676. {
  1677. __u8 status = *((__u8 *) skb->data);
  1678. struct discovery_state *discov = &hdev->discovery;
  1679. struct inquiry_entry *e;
  1680. BT_DBG("%s status 0x%2.2x", hdev->name, status);
  1681. hci_conn_check_pending(hdev);
  1682. if (!test_and_clear_bit(HCI_INQUIRY, &hdev->flags))
  1683. return;
  1684. smp_mb__after_atomic(); /* wake_up_bit advises about this barrier */
  1685. wake_up_bit(&hdev->flags, HCI_INQUIRY);
  1686. if (!hci_dev_test_flag(hdev, HCI_MGMT))
  1687. return;
  1688. hci_dev_lock(hdev);
  1689. if (discov->state != DISCOVERY_FINDING)
  1690. goto unlock;
  1691. if (list_empty(&discov->resolve)) {
  1692. /* When BR/EDR inquiry is active and no LE scanning is in
  1693. * progress, then change discovery state to indicate completion.
  1694. *
  1695. * When running LE scanning and BR/EDR inquiry simultaneously
  1696. * and the LE scan already finished, then change the discovery
  1697. * state to indicate completion.
  1698. */
  1699. if (!hci_dev_test_flag(hdev, HCI_LE_SCAN) ||
  1700. !test_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks))
  1701. hci_discovery_set_state(hdev, DISCOVERY_STOPPED);
  1702. goto unlock;
  1703. }
  1704. e = hci_inquiry_cache_lookup_resolve(hdev, BDADDR_ANY, NAME_NEEDED);
  1705. if (e && hci_resolve_name(hdev, e) == 0) {
  1706. e->name_state = NAME_PENDING;
  1707. hci_discovery_set_state(hdev, DISCOVERY_RESOLVING);
  1708. } else {
  1709. /* When BR/EDR inquiry is active and no LE scanning is in
  1710. * progress, then change discovery state to indicate completion.
  1711. *
  1712. * When running LE scanning and BR/EDR inquiry simultaneously
  1713. * and the LE scan already finished, then change the discovery
  1714. * state to indicate completion.
  1715. */
  1716. if (!hci_dev_test_flag(hdev, HCI_LE_SCAN) ||
  1717. !test_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks))
  1718. hci_discovery_set_state(hdev, DISCOVERY_STOPPED);
  1719. }
  1720. unlock:
  1721. hci_dev_unlock(hdev);
  1722. }
  1723. static void hci_inquiry_result_evt(struct hci_dev *hdev, struct sk_buff *skb)
  1724. {
  1725. struct inquiry_data data;
  1726. struct inquiry_info *info = (void *) (skb->data + 1);
  1727. int num_rsp = *((__u8 *) skb->data);
  1728. BT_DBG("%s num_rsp %d", hdev->name, num_rsp);
  1729. if (!num_rsp)
  1730. return;
  1731. if (hci_dev_test_flag(hdev, HCI_PERIODIC_INQ))
  1732. return;
  1733. hci_dev_lock(hdev);
  1734. for (; num_rsp; num_rsp--, info++) {
  1735. u32 flags;
  1736. bacpy(&data.bdaddr, &info->bdaddr);
  1737. data.pscan_rep_mode = info->pscan_rep_mode;
  1738. data.pscan_period_mode = info->pscan_period_mode;
  1739. data.pscan_mode = info->pscan_mode;
  1740. memcpy(data.dev_class, info->dev_class, 3);
  1741. data.clock_offset = info->clock_offset;
  1742. data.rssi = HCI_RSSI_INVALID;
  1743. data.ssp_mode = 0x00;
  1744. flags = hci_inquiry_cache_update(hdev, &data, false);
  1745. mgmt_device_found(hdev, &info->bdaddr, ACL_LINK, 0x00,
  1746. info->dev_class, HCI_RSSI_INVALID,
  1747. flags, NULL, 0, NULL, 0);
  1748. }
  1749. hci_dev_unlock(hdev);
  1750. }
  1751. static void hci_conn_complete_evt(struct hci_dev *hdev, struct sk_buff *skb)
  1752. {
  1753. struct hci_ev_conn_complete *ev = (void *) skb->data;
  1754. struct hci_conn *conn;
  1755. BT_DBG("%s", hdev->name);
  1756. hci_dev_lock(hdev);
  1757. conn = hci_conn_hash_lookup_ba(hdev, ev->link_type, &ev->bdaddr);
  1758. if (!conn) {
  1759. if (ev->link_type != SCO_LINK)
  1760. goto unlock;
  1761. conn = hci_conn_hash_lookup_ba(hdev, ESCO_LINK, &ev->bdaddr);
  1762. if (!conn)
  1763. goto unlock;
  1764. conn->type = SCO_LINK;
  1765. }
  1766. if (!ev->status) {
  1767. conn->handle = __le16_to_cpu(ev->handle);
  1768. if (conn->type == ACL_LINK) {
  1769. conn->state = BT_CONFIG;
  1770. hci_conn_hold(conn);
  1771. if (!conn->out && !hci_conn_ssp_enabled(conn) &&
  1772. !hci_find_link_key(hdev, &ev->bdaddr))
  1773. conn->disc_timeout = HCI_PAIRING_TIMEOUT;
  1774. else
  1775. conn->disc_timeout = HCI_DISCONN_TIMEOUT;
  1776. } else
  1777. conn->state = BT_CONNECTED;
  1778. hci_debugfs_create_conn(conn);
  1779. hci_conn_add_sysfs(conn);
  1780. if (test_bit(HCI_AUTH, &hdev->flags))
  1781. set_bit(HCI_CONN_AUTH, &conn->flags);
  1782. if (test_bit(HCI_ENCRYPT, &hdev->flags))
  1783. set_bit(HCI_CONN_ENCRYPT, &conn->flags);
  1784. /* Get remote features */
  1785. if (conn->type == ACL_LINK) {
  1786. struct hci_cp_read_remote_features cp;
  1787. cp.handle = ev->handle;
  1788. hci_send_cmd(hdev, HCI_OP_READ_REMOTE_FEATURES,
  1789. sizeof(cp), &cp);
  1790. hci_req_update_scan(hdev);
  1791. }
  1792. /* Set packet type for incoming connection */
  1793. if (!conn->out && hdev->hci_ver < BLUETOOTH_VER_2_0) {
  1794. struct hci_cp_change_conn_ptype cp;
  1795. cp.handle = ev->handle;
  1796. cp.pkt_type = cpu_to_le16(conn->pkt_type);
  1797. hci_send_cmd(hdev, HCI_OP_CHANGE_CONN_PTYPE, sizeof(cp),
  1798. &cp);
  1799. }
  1800. } else {
  1801. conn->state = BT_CLOSED;
  1802. if (conn->type == ACL_LINK)
  1803. mgmt_connect_failed(hdev, &conn->dst, conn->type,
  1804. conn->dst_type, ev->status);
  1805. }
  1806. if (conn->type == ACL_LINK)
  1807. hci_sco_setup(conn, ev->status);
  1808. if (ev->status) {
  1809. hci_connect_cfm(conn, ev->status);
  1810. hci_conn_del(conn);
  1811. } else if (ev->link_type != ACL_LINK)
  1812. hci_connect_cfm(conn, ev->status);
  1813. unlock:
  1814. hci_dev_unlock(hdev);
  1815. hci_conn_check_pending(hdev);
  1816. }
  1817. static void hci_reject_conn(struct hci_dev *hdev, bdaddr_t *bdaddr)
  1818. {
  1819. struct hci_cp_reject_conn_req cp;
  1820. bacpy(&cp.bdaddr, bdaddr);
  1821. cp.reason = HCI_ERROR_REJ_BAD_ADDR;
  1822. hci_send_cmd(hdev, HCI_OP_REJECT_CONN_REQ, sizeof(cp), &cp);
  1823. }
  1824. static void hci_conn_request_evt(struct hci_dev *hdev, struct sk_buff *skb)
  1825. {
  1826. struct hci_ev_conn_request *ev = (void *) skb->data;
  1827. int mask = hdev->link_mode;
  1828. struct inquiry_entry *ie;
  1829. struct hci_conn *conn;
  1830. __u8 flags = 0;
  1831. BT_DBG("%s bdaddr %pMR type 0x%x", hdev->name, &ev->bdaddr,
  1832. ev->link_type);
  1833. mask |= hci_proto_connect_ind(hdev, &ev->bdaddr, ev->link_type,
  1834. &flags);
  1835. if (!(mask & HCI_LM_ACCEPT)) {
  1836. hci_reject_conn(hdev, &ev->bdaddr);
  1837. return;
  1838. }
  1839. if (hci_bdaddr_list_lookup(&hdev->blacklist, &ev->bdaddr,
  1840. BDADDR_BREDR)) {
  1841. hci_reject_conn(hdev, &ev->bdaddr);
  1842. return;
  1843. }
  1844. /* Require HCI_CONNECTABLE or a whitelist entry to accept the
  1845. * connection. These features are only touched through mgmt so
  1846. * only do the checks if HCI_MGMT is set.
  1847. */
  1848. if (hci_dev_test_flag(hdev, HCI_MGMT) &&
  1849. !hci_dev_test_flag(hdev, HCI_CONNECTABLE) &&
  1850. !hci_bdaddr_list_lookup(&hdev->whitelist, &ev->bdaddr,
  1851. BDADDR_BREDR)) {
  1852. hci_reject_conn(hdev, &ev->bdaddr);
  1853. return;
  1854. }
  1855. /* Connection accepted */
  1856. hci_dev_lock(hdev);
  1857. ie = hci_inquiry_cache_lookup(hdev, &ev->bdaddr);
  1858. if (ie)
  1859. memcpy(ie->data.dev_class, ev->dev_class, 3);
  1860. conn = hci_conn_hash_lookup_ba(hdev, ev->link_type,
  1861. &ev->bdaddr);
  1862. if (!conn) {
  1863. conn = hci_conn_add(hdev, ev->link_type, &ev->bdaddr,
  1864. HCI_ROLE_SLAVE);
  1865. if (!conn) {
  1866. bt_dev_err(hdev, "no memory for new connection");
  1867. hci_dev_unlock(hdev);
  1868. return;
  1869. }
  1870. }
  1871. memcpy(conn->dev_class, ev->dev_class, 3);
  1872. hci_dev_unlock(hdev);
  1873. if (ev->link_type == ACL_LINK ||
  1874. (!(flags & HCI_PROTO_DEFER) && !lmp_esco_capable(hdev))) {
  1875. struct hci_cp_accept_conn_req cp;
  1876. conn->state = BT_CONNECT;
  1877. bacpy(&cp.bdaddr, &ev->bdaddr);
  1878. if (lmp_rswitch_capable(hdev) && (mask & HCI_LM_MASTER))
  1879. cp.role = 0x00; /* Become master */
  1880. else
  1881. cp.role = 0x01; /* Remain slave */
  1882. hci_send_cmd(hdev, HCI_OP_ACCEPT_CONN_REQ, sizeof(cp), &cp);
  1883. } else if (!(flags & HCI_PROTO_DEFER)) {
  1884. struct hci_cp_accept_sync_conn_req cp;
  1885. conn->state = BT_CONNECT;
  1886. bacpy(&cp.bdaddr, &ev->bdaddr);
  1887. cp.pkt_type = cpu_to_le16(conn->pkt_type);
  1888. cp.tx_bandwidth = cpu_to_le32(0x00001f40);
  1889. cp.rx_bandwidth = cpu_to_le32(0x00001f40);
  1890. cp.max_latency = cpu_to_le16(0xffff);
  1891. cp.content_format = cpu_to_le16(hdev->voice_setting);
  1892. cp.retrans_effort = 0xff;
  1893. hci_send_cmd(hdev, HCI_OP_ACCEPT_SYNC_CONN_REQ, sizeof(cp),
  1894. &cp);
  1895. } else {
  1896. conn->state = BT_CONNECT2;
  1897. hci_connect_cfm(conn, 0);
  1898. }
  1899. }
  1900. static u8 hci_to_mgmt_reason(u8 err)
  1901. {
  1902. switch (err) {
  1903. case HCI_ERROR_CONNECTION_TIMEOUT:
  1904. return MGMT_DEV_DISCONN_TIMEOUT;
  1905. case HCI_ERROR_REMOTE_USER_TERM:
  1906. case HCI_ERROR_REMOTE_LOW_RESOURCES:
  1907. case HCI_ERROR_REMOTE_POWER_OFF:
  1908. return MGMT_DEV_DISCONN_REMOTE;
  1909. case HCI_ERROR_LOCAL_HOST_TERM:
  1910. return MGMT_DEV_DISCONN_LOCAL_HOST;
  1911. default:
  1912. return MGMT_DEV_DISCONN_UNKNOWN;
  1913. }
  1914. }
  1915. static void hci_disconn_complete_evt(struct hci_dev *hdev, struct sk_buff *skb)
  1916. {
  1917. struct hci_ev_disconn_complete *ev = (void *) skb->data;
  1918. u8 reason;
  1919. struct hci_conn_params *params;
  1920. struct hci_conn *conn;
  1921. bool mgmt_connected;
  1922. u8 type;
  1923. BT_DBG("%s status 0x%2.2x", hdev->name, ev->status);
  1924. hci_dev_lock(hdev);
  1925. conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
  1926. if (!conn)
  1927. goto unlock;
  1928. if (ev->status) {
  1929. mgmt_disconnect_failed(hdev, &conn->dst, conn->type,
  1930. conn->dst_type, ev->status);
  1931. goto unlock;
  1932. }
  1933. conn->state = BT_CLOSED;
  1934. mgmt_connected = test_and_clear_bit(HCI_CONN_MGMT_CONNECTED, &conn->flags);
  1935. if (test_bit(HCI_CONN_AUTH_FAILURE, &conn->flags))
  1936. reason = MGMT_DEV_DISCONN_AUTH_FAILURE;
  1937. else
  1938. reason = hci_to_mgmt_reason(ev->reason);
  1939. mgmt_device_disconnected(hdev, &conn->dst, conn->type, conn->dst_type,
  1940. reason, mgmt_connected);
  1941. if (conn->type == ACL_LINK) {
  1942. if (test_bit(HCI_CONN_FLUSH_KEY, &conn->flags))
  1943. hci_remove_link_key(hdev, &conn->dst);
  1944. hci_req_update_scan(hdev);
  1945. }
  1946. params = hci_conn_params_lookup(hdev, &conn->dst, conn->dst_type);
  1947. if (params) {
  1948. switch (params->auto_connect) {
  1949. case HCI_AUTO_CONN_LINK_LOSS:
  1950. if (ev->reason != HCI_ERROR_CONNECTION_TIMEOUT)
  1951. break;
  1952. /* Fall through */
  1953. case HCI_AUTO_CONN_DIRECT:
  1954. case HCI_AUTO_CONN_ALWAYS:
  1955. list_del_init(&params->action);
  1956. list_add(&params->action, &hdev->pend_le_conns);
  1957. hci_update_background_scan(hdev);
  1958. break;
  1959. default:
  1960. break;
  1961. }
  1962. }
  1963. type = conn->type;
  1964. hci_disconn_cfm(conn, ev->reason);
  1965. hci_conn_del(conn);
  1966. /* Re-enable advertising if necessary, since it might
  1967. * have been disabled by the connection. From the
  1968. * HCI_LE_Set_Advertise_Enable command description in
  1969. * the core specification (v4.0):
  1970. * "The Controller shall continue advertising until the Host
  1971. * issues an LE_Set_Advertise_Enable command with
  1972. * Advertising_Enable set to 0x00 (Advertising is disabled)
  1973. * or until a connection is created or until the Advertising
  1974. * is timed out due to Directed Advertising."
  1975. */
  1976. if (type == LE_LINK)
  1977. hci_req_reenable_advertising(hdev);
  1978. unlock:
  1979. hci_dev_unlock(hdev);
  1980. }
  1981. static void hci_auth_complete_evt(struct hci_dev *hdev, struct sk_buff *skb)
  1982. {
  1983. struct hci_ev_auth_complete *ev = (void *) skb->data;
  1984. struct hci_conn *conn;
  1985. BT_DBG("%s status 0x%2.2x", hdev->name, ev->status);
  1986. hci_dev_lock(hdev);
  1987. conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
  1988. if (!conn)
  1989. goto unlock;
  1990. if (!ev->status) {
  1991. clear_bit(HCI_CONN_AUTH_FAILURE, &conn->flags);
  1992. if (!hci_conn_ssp_enabled(conn) &&
  1993. test_bit(HCI_CONN_REAUTH_PEND, &conn->flags)) {
  1994. bt_dev_info(hdev, "re-auth of legacy device is not possible.");
  1995. } else {
  1996. set_bit(HCI_CONN_AUTH, &conn->flags);
  1997. conn->sec_level = conn->pending_sec_level;
  1998. }
  1999. } else {
  2000. if (ev->status == HCI_ERROR_PIN_OR_KEY_MISSING)
  2001. set_bit(HCI_CONN_AUTH_FAILURE, &conn->flags);
  2002. mgmt_auth_failed(conn, ev->status);
  2003. }
  2004. clear_bit(HCI_CONN_AUTH_PEND, &conn->flags);
  2005. clear_bit(HCI_CONN_REAUTH_PEND, &conn->flags);
  2006. if (conn->state == BT_CONFIG) {
  2007. if (!ev->status && hci_conn_ssp_enabled(conn)) {
  2008. struct hci_cp_set_conn_encrypt cp;
  2009. cp.handle = ev->handle;
  2010. cp.encrypt = 0x01;
  2011. hci_send_cmd(hdev, HCI_OP_SET_CONN_ENCRYPT, sizeof(cp),
  2012. &cp);
  2013. } else {
  2014. conn->state = BT_CONNECTED;
  2015. hci_connect_cfm(conn, ev->status);
  2016. hci_conn_drop(conn);
  2017. }
  2018. } else {
  2019. hci_auth_cfm(conn, ev->status);
  2020. hci_conn_hold(conn);
  2021. conn->disc_timeout = HCI_DISCONN_TIMEOUT;
  2022. hci_conn_drop(conn);
  2023. }
  2024. if (test_bit(HCI_CONN_ENCRYPT_PEND, &conn->flags)) {
  2025. if (!ev->status) {
  2026. struct hci_cp_set_conn_encrypt cp;
  2027. cp.handle = ev->handle;
  2028. cp.encrypt = 0x01;
  2029. hci_send_cmd(hdev, HCI_OP_SET_CONN_ENCRYPT, sizeof(cp),
  2030. &cp);
  2031. } else {
  2032. clear_bit(HCI_CONN_ENCRYPT_PEND, &conn->flags);
  2033. hci_encrypt_cfm(conn, ev->status, 0x00);
  2034. }
  2035. }
  2036. unlock:
  2037. hci_dev_unlock(hdev);
  2038. }
  2039. static void hci_remote_name_evt(struct hci_dev *hdev, struct sk_buff *skb)
  2040. {
  2041. struct hci_ev_remote_name *ev = (void *) skb->data;
  2042. struct hci_conn *conn;
  2043. BT_DBG("%s", hdev->name);
  2044. hci_conn_check_pending(hdev);
  2045. hci_dev_lock(hdev);
  2046. conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &ev->bdaddr);
  2047. if (!hci_dev_test_flag(hdev, HCI_MGMT))
  2048. goto check_auth;
  2049. if (ev->status == 0)
  2050. hci_check_pending_name(hdev, conn, &ev->bdaddr, ev->name,
  2051. strnlen(ev->name, HCI_MAX_NAME_LENGTH));
  2052. else
  2053. hci_check_pending_name(hdev, conn, &ev->bdaddr, NULL, 0);
  2054. check_auth:
  2055. if (!conn)
  2056. goto unlock;
  2057. if (!hci_outgoing_auth_needed(hdev, conn))
  2058. goto unlock;
  2059. if (!test_and_set_bit(HCI_CONN_AUTH_PEND, &conn->flags)) {
  2060. struct hci_cp_auth_requested cp;
  2061. set_bit(HCI_CONN_AUTH_INITIATOR, &conn->flags);
  2062. cp.handle = __cpu_to_le16(conn->handle);
  2063. hci_send_cmd(hdev, HCI_OP_AUTH_REQUESTED, sizeof(cp), &cp);
  2064. }
  2065. unlock:
  2066. hci_dev_unlock(hdev);
  2067. }
  2068. static void read_enc_key_size_complete(struct hci_dev *hdev, u8 status,
  2069. u16 opcode, struct sk_buff *skb)
  2070. {
  2071. const struct hci_rp_read_enc_key_size *rp;
  2072. struct hci_conn *conn;
  2073. u16 handle;
  2074. BT_DBG("%s status 0x%02x", hdev->name, status);
  2075. if (!skb || skb->len < sizeof(*rp)) {
  2076. bt_dev_err(hdev, "invalid read key size response");
  2077. return;
  2078. }
  2079. rp = (void *)skb->data;
  2080. handle = le16_to_cpu(rp->handle);
  2081. hci_dev_lock(hdev);
  2082. conn = hci_conn_hash_lookup_handle(hdev, handle);
  2083. if (!conn)
  2084. goto unlock;
  2085. /* If we fail to read the encryption key size, assume maximum
  2086. * (which is the same we do also when this HCI command isn't
  2087. * supported.
  2088. */
  2089. if (rp->status) {
  2090. bt_dev_err(hdev, "failed to read key size for handle %u",
  2091. handle);
  2092. conn->enc_key_size = HCI_LINK_KEY_SIZE;
  2093. } else {
  2094. conn->enc_key_size = rp->key_size;
  2095. }
  2096. if (conn->state == BT_CONFIG) {
  2097. conn->state = BT_CONNECTED;
  2098. hci_connect_cfm(conn, 0);
  2099. hci_conn_drop(conn);
  2100. } else {
  2101. u8 encrypt;
  2102. if (!test_bit(HCI_CONN_ENCRYPT, &conn->flags))
  2103. encrypt = 0x00;
  2104. else if (test_bit(HCI_CONN_AES_CCM, &conn->flags))
  2105. encrypt = 0x02;
  2106. else
  2107. encrypt = 0x01;
  2108. hci_encrypt_cfm(conn, 0, encrypt);
  2109. }
  2110. unlock:
  2111. hci_dev_unlock(hdev);
  2112. }
  2113. static void hci_encrypt_change_evt(struct hci_dev *hdev, struct sk_buff *skb)
  2114. {
  2115. struct hci_ev_encrypt_change *ev = (void *) skb->data;
  2116. struct hci_conn *conn;
  2117. BT_DBG("%s status 0x%2.2x", hdev->name, ev->status);
  2118. hci_dev_lock(hdev);
  2119. conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
  2120. if (!conn)
  2121. goto unlock;
  2122. if (!ev->status) {
  2123. if (ev->encrypt) {
  2124. /* Encryption implies authentication */
  2125. set_bit(HCI_CONN_AUTH, &conn->flags);
  2126. set_bit(HCI_CONN_ENCRYPT, &conn->flags);
  2127. conn->sec_level = conn->pending_sec_level;
  2128. /* P-256 authentication key implies FIPS */
  2129. if (conn->key_type == HCI_LK_AUTH_COMBINATION_P256)
  2130. set_bit(HCI_CONN_FIPS, &conn->flags);
  2131. if ((conn->type == ACL_LINK && ev->encrypt == 0x02) ||
  2132. conn->type == LE_LINK)
  2133. set_bit(HCI_CONN_AES_CCM, &conn->flags);
  2134. } else {
  2135. clear_bit(HCI_CONN_ENCRYPT, &conn->flags);
  2136. clear_bit(HCI_CONN_AES_CCM, &conn->flags);
  2137. }
  2138. }
  2139. /* We should disregard the current RPA and generate a new one
  2140. * whenever the encryption procedure fails.
  2141. */
  2142. if (ev->status && conn->type == LE_LINK) {
  2143. hci_dev_set_flag(hdev, HCI_RPA_EXPIRED);
  2144. hci_adv_instances_set_rpa_expired(hdev, true);
  2145. }
  2146. clear_bit(HCI_CONN_ENCRYPT_PEND, &conn->flags);
  2147. if (ev->status && conn->state == BT_CONNECTED) {
  2148. if (ev->status == HCI_ERROR_PIN_OR_KEY_MISSING)
  2149. set_bit(HCI_CONN_AUTH_FAILURE, &conn->flags);
  2150. hci_disconnect(conn, HCI_ERROR_AUTH_FAILURE);
  2151. hci_conn_drop(conn);
  2152. goto unlock;
  2153. }
  2154. /* In Secure Connections Only mode, do not allow any connections
  2155. * that are not encrypted with AES-CCM using a P-256 authenticated
  2156. * combination key.
  2157. */
  2158. if (hci_dev_test_flag(hdev, HCI_SC_ONLY) &&
  2159. (!test_bit(HCI_CONN_AES_CCM, &conn->flags) ||
  2160. conn->key_type != HCI_LK_AUTH_COMBINATION_P256)) {
  2161. hci_connect_cfm(conn, HCI_ERROR_AUTH_FAILURE);
  2162. hci_conn_drop(conn);
  2163. goto unlock;
  2164. }
  2165. /* Try reading the encryption key size for encrypted ACL links */
  2166. if (!ev->status && ev->encrypt && conn->type == ACL_LINK) {
  2167. struct hci_cp_read_enc_key_size cp;
  2168. struct hci_request req;
  2169. /* Only send HCI_Read_Encryption_Key_Size if the
  2170. * controller really supports it. If it doesn't, assume
  2171. * the default size (16).
  2172. */
  2173. if (!(hdev->commands[20] & 0x10)) {
  2174. conn->enc_key_size = HCI_LINK_KEY_SIZE;
  2175. goto notify;
  2176. }
  2177. hci_req_init(&req, hdev);
  2178. cp.handle = cpu_to_le16(conn->handle);
  2179. hci_req_add(&req, HCI_OP_READ_ENC_KEY_SIZE, sizeof(cp), &cp);
  2180. if (hci_req_run_skb(&req, read_enc_key_size_complete)) {
  2181. bt_dev_err(hdev, "sending read key size failed");
  2182. conn->enc_key_size = HCI_LINK_KEY_SIZE;
  2183. goto notify;
  2184. }
  2185. goto unlock;
  2186. }
  2187. notify:
  2188. if (conn->state == BT_CONFIG) {
  2189. if (!ev->status)
  2190. conn->state = BT_CONNECTED;
  2191. hci_connect_cfm(conn, ev->status);
  2192. hci_conn_drop(conn);
  2193. } else
  2194. hci_encrypt_cfm(conn, ev->status, ev->encrypt);
  2195. unlock:
  2196. hci_dev_unlock(hdev);
  2197. }
  2198. static void hci_change_link_key_complete_evt(struct hci_dev *hdev,
  2199. struct sk_buff *skb)
  2200. {
  2201. struct hci_ev_change_link_key_complete *ev = (void *) skb->data;
  2202. struct hci_conn *conn;
  2203. BT_DBG("%s status 0x%2.2x", hdev->name, ev->status);
  2204. hci_dev_lock(hdev);
  2205. conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
  2206. if (conn) {
  2207. if (!ev->status)
  2208. set_bit(HCI_CONN_SECURE, &conn->flags);
  2209. clear_bit(HCI_CONN_AUTH_PEND, &conn->flags);
  2210. hci_key_change_cfm(conn, ev->status);
  2211. }
  2212. hci_dev_unlock(hdev);
  2213. }
  2214. static void hci_remote_features_evt(struct hci_dev *hdev,
  2215. struct sk_buff *skb)
  2216. {
  2217. struct hci_ev_remote_features *ev = (void *) skb->data;
  2218. struct hci_conn *conn;
  2219. BT_DBG("%s status 0x%2.2x", hdev->name, ev->status);
  2220. hci_dev_lock(hdev);
  2221. conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
  2222. if (!conn)
  2223. goto unlock;
  2224. if (!ev->status)
  2225. memcpy(conn->features[0], ev->features, 8);
  2226. if (conn->state != BT_CONFIG)
  2227. goto unlock;
  2228. if (!ev->status && lmp_ext_feat_capable(hdev) &&
  2229. lmp_ext_feat_capable(conn)) {
  2230. struct hci_cp_read_remote_ext_features cp;
  2231. cp.handle = ev->handle;
  2232. cp.page = 0x01;
  2233. hci_send_cmd(hdev, HCI_OP_READ_REMOTE_EXT_FEATURES,
  2234. sizeof(cp), &cp);
  2235. goto unlock;
  2236. }
  2237. if (!ev->status && !test_bit(HCI_CONN_MGMT_CONNECTED, &conn->flags)) {
  2238. struct hci_cp_remote_name_req cp;
  2239. memset(&cp, 0, sizeof(cp));
  2240. bacpy(&cp.bdaddr, &conn->dst);
  2241. cp.pscan_rep_mode = 0x02;
  2242. hci_send_cmd(hdev, HCI_OP_REMOTE_NAME_REQ, sizeof(cp), &cp);
  2243. } else if (!test_and_set_bit(HCI_CONN_MGMT_CONNECTED, &conn->flags))
  2244. mgmt_device_connected(hdev, conn, 0, NULL, 0);
  2245. if (!hci_outgoing_auth_needed(hdev, conn)) {
  2246. conn->state = BT_CONNECTED;
  2247. hci_connect_cfm(conn, ev->status);
  2248. hci_conn_drop(conn);
  2249. }
  2250. unlock:
  2251. hci_dev_unlock(hdev);
  2252. }
  2253. static void hci_cmd_complete_evt(struct hci_dev *hdev, struct sk_buff *skb,
  2254. u16 *opcode, u8 *status,
  2255. hci_req_complete_t *req_complete,
  2256. hci_req_complete_skb_t *req_complete_skb)
  2257. {
  2258. struct hci_ev_cmd_complete *ev = (void *) skb->data;
  2259. *opcode = __le16_to_cpu(ev->opcode);
  2260. *status = skb->data[sizeof(*ev)];
  2261. skb_pull(skb, sizeof(*ev));
  2262. switch (*opcode) {
  2263. case HCI_OP_INQUIRY_CANCEL:
  2264. hci_cc_inquiry_cancel(hdev, skb);
  2265. break;
  2266. case HCI_OP_PERIODIC_INQ:
  2267. hci_cc_periodic_inq(hdev, skb);
  2268. break;
  2269. case HCI_OP_EXIT_PERIODIC_INQ:
  2270. hci_cc_exit_periodic_inq(hdev, skb);
  2271. break;
  2272. case HCI_OP_REMOTE_NAME_REQ_CANCEL:
  2273. hci_cc_remote_name_req_cancel(hdev, skb);
  2274. break;
  2275. case HCI_OP_ROLE_DISCOVERY:
  2276. hci_cc_role_discovery(hdev, skb);
  2277. break;
  2278. case HCI_OP_READ_LINK_POLICY:
  2279. hci_cc_read_link_policy(hdev, skb);
  2280. break;
  2281. case HCI_OP_WRITE_LINK_POLICY:
  2282. hci_cc_write_link_policy(hdev, skb);
  2283. break;
  2284. case HCI_OP_READ_DEF_LINK_POLICY:
  2285. hci_cc_read_def_link_policy(hdev, skb);
  2286. break;
  2287. case HCI_OP_WRITE_DEF_LINK_POLICY:
  2288. hci_cc_write_def_link_policy(hdev, skb);
  2289. break;
  2290. case HCI_OP_RESET:
  2291. hci_cc_reset(hdev, skb);
  2292. break;
  2293. case HCI_OP_READ_STORED_LINK_KEY:
  2294. hci_cc_read_stored_link_key(hdev, skb);
  2295. break;
  2296. case HCI_OP_DELETE_STORED_LINK_KEY:
  2297. hci_cc_delete_stored_link_key(hdev, skb);
  2298. break;
  2299. case HCI_OP_WRITE_LOCAL_NAME:
  2300. hci_cc_write_local_name(hdev, skb);
  2301. break;
  2302. case HCI_OP_READ_LOCAL_NAME:
  2303. hci_cc_read_local_name(hdev, skb);
  2304. break;
  2305. case HCI_OP_WRITE_AUTH_ENABLE:
  2306. hci_cc_write_auth_enable(hdev, skb);
  2307. break;
  2308. case HCI_OP_WRITE_ENCRYPT_MODE:
  2309. hci_cc_write_encrypt_mode(hdev, skb);
  2310. break;
  2311. case HCI_OP_WRITE_SCAN_ENABLE:
  2312. hci_cc_write_scan_enable(hdev, skb);
  2313. break;
  2314. case HCI_OP_READ_CLASS_OF_DEV:
  2315. hci_cc_read_class_of_dev(hdev, skb);
  2316. break;
  2317. case HCI_OP_WRITE_CLASS_OF_DEV:
  2318. hci_cc_write_class_of_dev(hdev, skb);
  2319. break;
  2320. case HCI_OP_READ_VOICE_SETTING:
  2321. hci_cc_read_voice_setting(hdev, skb);
  2322. break;
  2323. case HCI_OP_WRITE_VOICE_SETTING:
  2324. hci_cc_write_voice_setting(hdev, skb);
  2325. break;
  2326. case HCI_OP_READ_NUM_SUPPORTED_IAC:
  2327. hci_cc_read_num_supported_iac(hdev, skb);
  2328. break;
  2329. case HCI_OP_WRITE_SSP_MODE:
  2330. hci_cc_write_ssp_mode(hdev, skb);
  2331. break;
  2332. case HCI_OP_WRITE_SC_SUPPORT:
  2333. hci_cc_write_sc_support(hdev, skb);
  2334. break;
  2335. case HCI_OP_READ_LOCAL_VERSION:
  2336. hci_cc_read_local_version(hdev, skb);
  2337. break;
  2338. case HCI_OP_READ_LOCAL_COMMANDS:
  2339. hci_cc_read_local_commands(hdev, skb);
  2340. break;
  2341. case HCI_OP_READ_LOCAL_FEATURES:
  2342. hci_cc_read_local_features(hdev, skb);
  2343. break;
  2344. case HCI_OP_READ_LOCAL_EXT_FEATURES:
  2345. hci_cc_read_local_ext_features(hdev, skb);
  2346. break;
  2347. case HCI_OP_READ_BUFFER_SIZE:
  2348. hci_cc_read_buffer_size(hdev, skb);
  2349. break;
  2350. case HCI_OP_READ_BD_ADDR:
  2351. hci_cc_read_bd_addr(hdev, skb);
  2352. break;
  2353. case HCI_OP_READ_PAGE_SCAN_ACTIVITY:
  2354. hci_cc_read_page_scan_activity(hdev, skb);
  2355. break;
  2356. case HCI_OP_WRITE_PAGE_SCAN_ACTIVITY:
  2357. hci_cc_write_page_scan_activity(hdev, skb);
  2358. break;
  2359. case HCI_OP_READ_PAGE_SCAN_TYPE:
  2360. hci_cc_read_page_scan_type(hdev, skb);
  2361. break;
  2362. case HCI_OP_WRITE_PAGE_SCAN_TYPE:
  2363. hci_cc_write_page_scan_type(hdev, skb);
  2364. break;
  2365. case HCI_OP_READ_DATA_BLOCK_SIZE:
  2366. hci_cc_read_data_block_size(hdev, skb);
  2367. break;
  2368. case HCI_OP_READ_FLOW_CONTROL_MODE:
  2369. hci_cc_read_flow_control_mode(hdev, skb);
  2370. break;
  2371. case HCI_OP_READ_LOCAL_AMP_INFO:
  2372. hci_cc_read_local_amp_info(hdev, skb);
  2373. break;
  2374. case HCI_OP_READ_CLOCK:
  2375. hci_cc_read_clock(hdev, skb);
  2376. break;
  2377. case HCI_OP_READ_INQ_RSP_TX_POWER:
  2378. hci_cc_read_inq_rsp_tx_power(hdev, skb);
  2379. break;
  2380. case HCI_OP_PIN_CODE_REPLY:
  2381. hci_cc_pin_code_reply(hdev, skb);
  2382. break;
  2383. case HCI_OP_PIN_CODE_NEG_REPLY:
  2384. hci_cc_pin_code_neg_reply(hdev, skb);
  2385. break;
  2386. case HCI_OP_READ_LOCAL_OOB_DATA:
  2387. hci_cc_read_local_oob_data(hdev, skb);
  2388. break;
  2389. case HCI_OP_READ_LOCAL_OOB_EXT_DATA:
  2390. hci_cc_read_local_oob_ext_data(hdev, skb);
  2391. break;
  2392. case HCI_OP_LE_READ_BUFFER_SIZE:
  2393. hci_cc_le_read_buffer_size(hdev, skb);
  2394. break;
  2395. case HCI_OP_LE_READ_LOCAL_FEATURES:
  2396. hci_cc_le_read_local_features(hdev, skb);
  2397. break;
  2398. case HCI_OP_LE_READ_ADV_TX_POWER:
  2399. hci_cc_le_read_adv_tx_power(hdev, skb);
  2400. break;
  2401. case HCI_OP_USER_CONFIRM_REPLY:
  2402. hci_cc_user_confirm_reply(hdev, skb);
  2403. break;
  2404. case HCI_OP_USER_CONFIRM_NEG_REPLY:
  2405. hci_cc_user_confirm_neg_reply(hdev, skb);
  2406. break;
  2407. case HCI_OP_USER_PASSKEY_REPLY:
  2408. hci_cc_user_passkey_reply(hdev, skb);
  2409. break;
  2410. case HCI_OP_USER_PASSKEY_NEG_REPLY:
  2411. hci_cc_user_passkey_neg_reply(hdev, skb);
  2412. break;
  2413. case HCI_OP_LE_SET_RANDOM_ADDR:
  2414. hci_cc_le_set_random_addr(hdev, skb);
  2415. break;
  2416. case HCI_OP_LE_SET_ADV_ENABLE:
  2417. hci_cc_le_set_adv_enable(hdev, skb);
  2418. break;
  2419. case HCI_OP_LE_SET_SCAN_PARAM:
  2420. hci_cc_le_set_scan_param(hdev, skb);
  2421. break;
  2422. case HCI_OP_LE_SET_SCAN_ENABLE:
  2423. hci_cc_le_set_scan_enable(hdev, skb);
  2424. break;
  2425. case HCI_OP_LE_READ_WHITE_LIST_SIZE:
  2426. hci_cc_le_read_white_list_size(hdev, skb);
  2427. break;
  2428. case HCI_OP_LE_CLEAR_WHITE_LIST:
  2429. hci_cc_le_clear_white_list(hdev, skb);
  2430. break;
  2431. case HCI_OP_LE_ADD_TO_WHITE_LIST:
  2432. hci_cc_le_add_to_white_list(hdev, skb);
  2433. break;
  2434. case HCI_OP_LE_DEL_FROM_WHITE_LIST:
  2435. hci_cc_le_del_from_white_list(hdev, skb);
  2436. break;
  2437. case HCI_OP_LE_READ_SUPPORTED_STATES:
  2438. hci_cc_le_read_supported_states(hdev, skb);
  2439. break;
  2440. case HCI_OP_LE_READ_DEF_DATA_LEN:
  2441. hci_cc_le_read_def_data_len(hdev, skb);
  2442. break;
  2443. case HCI_OP_LE_WRITE_DEF_DATA_LEN:
  2444. hci_cc_le_write_def_data_len(hdev, skb);
  2445. break;
  2446. case HCI_OP_LE_CLEAR_RESOLV_LIST:
  2447. hci_cc_le_clear_resolv_list(hdev, skb);
  2448. break;
  2449. case HCI_OP_LE_READ_RESOLV_LIST_SIZE:
  2450. hci_cc_le_read_resolv_list_size(hdev, skb);
  2451. break;
  2452. case HCI_OP_LE_SET_ADDR_RESOLV_ENABLE:
  2453. hci_cc_le_set_addr_resolution_enable(hdev, skb);
  2454. break;
  2455. case HCI_OP_LE_READ_MAX_DATA_LEN:
  2456. hci_cc_le_read_max_data_len(hdev, skb);
  2457. break;
  2458. case HCI_OP_WRITE_LE_HOST_SUPPORTED:
  2459. hci_cc_write_le_host_supported(hdev, skb);
  2460. break;
  2461. case HCI_OP_LE_SET_ADV_PARAM:
  2462. hci_cc_set_adv_param(hdev, skb);
  2463. break;
  2464. case HCI_OP_READ_RSSI:
  2465. hci_cc_read_rssi(hdev, skb);
  2466. break;
  2467. case HCI_OP_READ_TX_POWER:
  2468. hci_cc_read_tx_power(hdev, skb);
  2469. break;
  2470. case HCI_OP_WRITE_SSP_DEBUG_MODE:
  2471. hci_cc_write_ssp_debug_mode(hdev, skb);
  2472. break;
  2473. case HCI_OP_LE_SET_EXT_SCAN_PARAMS:
  2474. hci_cc_le_set_ext_scan_param(hdev, skb);
  2475. break;
  2476. case HCI_OP_LE_SET_EXT_SCAN_ENABLE:
  2477. hci_cc_le_set_ext_scan_enable(hdev, skb);
  2478. break;
  2479. case HCI_OP_LE_SET_DEFAULT_PHY:
  2480. hci_cc_le_set_default_phy(hdev, skb);
  2481. break;
  2482. case HCI_OP_LE_READ_NUM_SUPPORTED_ADV_SETS:
  2483. hci_cc_le_read_num_adv_sets(hdev, skb);
  2484. break;
  2485. case HCI_OP_LE_SET_EXT_ADV_PARAMS:
  2486. hci_cc_set_ext_adv_param(hdev, skb);
  2487. break;
  2488. case HCI_OP_LE_SET_EXT_ADV_ENABLE:
  2489. hci_cc_le_set_ext_adv_enable(hdev, skb);
  2490. break;
  2491. case HCI_OP_LE_SET_ADV_SET_RAND_ADDR:
  2492. hci_cc_le_set_adv_set_random_addr(hdev, skb);
  2493. break;
  2494. default:
  2495. BT_DBG("%s opcode 0x%4.4x", hdev->name, *opcode);
  2496. break;
  2497. }
  2498. if (*opcode != HCI_OP_NOP)
  2499. cancel_delayed_work(&hdev->cmd_timer);
  2500. if (ev->ncmd && !test_bit(HCI_RESET, &hdev->flags))
  2501. atomic_set(&hdev->cmd_cnt, 1);
  2502. hci_req_cmd_complete(hdev, *opcode, *status, req_complete,
  2503. req_complete_skb);
  2504. if (hci_dev_test_flag(hdev, HCI_CMD_PENDING)) {
  2505. bt_dev_err(hdev,
  2506. "unexpected event for opcode 0x%4.4x", *opcode);
  2507. return;
  2508. }
  2509. if (atomic_read(&hdev->cmd_cnt) && !skb_queue_empty(&hdev->cmd_q))
  2510. queue_work(hdev->workqueue, &hdev->cmd_work);
  2511. }
  2512. static void hci_cmd_status_evt(struct hci_dev *hdev, struct sk_buff *skb,
  2513. u16 *opcode, u8 *status,
  2514. hci_req_complete_t *req_complete,
  2515. hci_req_complete_skb_t *req_complete_skb)
  2516. {
  2517. struct hci_ev_cmd_status *ev = (void *) skb->data;
  2518. skb_pull(skb, sizeof(*ev));
  2519. *opcode = __le16_to_cpu(ev->opcode);
  2520. *status = ev->status;
  2521. switch (*opcode) {
  2522. case HCI_OP_INQUIRY:
  2523. hci_cs_inquiry(hdev, ev->status);
  2524. break;
  2525. case HCI_OP_CREATE_CONN:
  2526. hci_cs_create_conn(hdev, ev->status);
  2527. break;
  2528. case HCI_OP_DISCONNECT:
  2529. hci_cs_disconnect(hdev, ev->status);
  2530. break;
  2531. case HCI_OP_ADD_SCO:
  2532. hci_cs_add_sco(hdev, ev->status);
  2533. break;
  2534. case HCI_OP_AUTH_REQUESTED:
  2535. hci_cs_auth_requested(hdev, ev->status);
  2536. break;
  2537. case HCI_OP_SET_CONN_ENCRYPT:
  2538. hci_cs_set_conn_encrypt(hdev, ev->status);
  2539. break;
  2540. case HCI_OP_REMOTE_NAME_REQ:
  2541. hci_cs_remote_name_req(hdev, ev->status);
  2542. break;
  2543. case HCI_OP_READ_REMOTE_FEATURES:
  2544. hci_cs_read_remote_features(hdev, ev->status);
  2545. break;
  2546. case HCI_OP_READ_REMOTE_EXT_FEATURES:
  2547. hci_cs_read_remote_ext_features(hdev, ev->status);
  2548. break;
  2549. case HCI_OP_SETUP_SYNC_CONN:
  2550. hci_cs_setup_sync_conn(hdev, ev->status);
  2551. break;
  2552. case HCI_OP_SNIFF_MODE:
  2553. hci_cs_sniff_mode(hdev, ev->status);
  2554. break;
  2555. case HCI_OP_EXIT_SNIFF_MODE:
  2556. hci_cs_exit_sniff_mode(hdev, ev->status);
  2557. break;
  2558. case HCI_OP_SWITCH_ROLE:
  2559. hci_cs_switch_role(hdev, ev->status);
  2560. break;
  2561. case HCI_OP_LE_CREATE_CONN:
  2562. hci_cs_le_create_conn(hdev, ev->status);
  2563. break;
  2564. case HCI_OP_LE_READ_REMOTE_FEATURES:
  2565. hci_cs_le_read_remote_features(hdev, ev->status);
  2566. break;
  2567. case HCI_OP_LE_START_ENC:
  2568. hci_cs_le_start_enc(hdev, ev->status);
  2569. break;
  2570. case HCI_OP_LE_EXT_CREATE_CONN:
  2571. hci_cs_le_ext_create_conn(hdev, ev->status);
  2572. break;
  2573. default:
  2574. BT_DBG("%s opcode 0x%4.4x", hdev->name, *opcode);
  2575. break;
  2576. }
  2577. if (*opcode != HCI_OP_NOP)
  2578. cancel_delayed_work(&hdev->cmd_timer);
  2579. if (ev->ncmd && !test_bit(HCI_RESET, &hdev->flags))
  2580. atomic_set(&hdev->cmd_cnt, 1);
  2581. /* Indicate request completion if the command failed. Also, if
  2582. * we're not waiting for a special event and we get a success
  2583. * command status we should try to flag the request as completed
  2584. * (since for this kind of commands there will not be a command
  2585. * complete event).
  2586. */
  2587. if (ev->status ||
  2588. (hdev->sent_cmd && !bt_cb(hdev->sent_cmd)->hci.req_event))
  2589. hci_req_cmd_complete(hdev, *opcode, ev->status, req_complete,
  2590. req_complete_skb);
  2591. if (hci_dev_test_flag(hdev, HCI_CMD_PENDING)) {
  2592. bt_dev_err(hdev,
  2593. "unexpected event for opcode 0x%4.4x", *opcode);
  2594. return;
  2595. }
  2596. if (atomic_read(&hdev->cmd_cnt) && !skb_queue_empty(&hdev->cmd_q))
  2597. queue_work(hdev->workqueue, &hdev->cmd_work);
  2598. }
  2599. static void hci_hardware_error_evt(struct hci_dev *hdev, struct sk_buff *skb)
  2600. {
  2601. struct hci_ev_hardware_error *ev = (void *) skb->data;
  2602. hdev->hw_error_code = ev->code;
  2603. queue_work(hdev->req_workqueue, &hdev->error_reset);
  2604. }
  2605. static void hci_role_change_evt(struct hci_dev *hdev, struct sk_buff *skb)
  2606. {
  2607. struct hci_ev_role_change *ev = (void *) skb->data;
  2608. struct hci_conn *conn;
  2609. BT_DBG("%s status 0x%2.2x", hdev->name, ev->status);
  2610. hci_dev_lock(hdev);
  2611. conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &ev->bdaddr);
  2612. if (conn) {
  2613. if (!ev->status)
  2614. conn->role = ev->role;
  2615. clear_bit(HCI_CONN_RSWITCH_PEND, &conn->flags);
  2616. hci_role_switch_cfm(conn, ev->status, ev->role);
  2617. }
  2618. hci_dev_unlock(hdev);
  2619. }
  2620. static void hci_num_comp_pkts_evt(struct hci_dev *hdev, struct sk_buff *skb)
  2621. {
  2622. struct hci_ev_num_comp_pkts *ev = (void *) skb->data;
  2623. int i;
  2624. if (hdev->flow_ctl_mode != HCI_FLOW_CTL_MODE_PACKET_BASED) {
  2625. bt_dev_err(hdev, "wrong event for mode %d", hdev->flow_ctl_mode);
  2626. return;
  2627. }
  2628. if (skb->len < sizeof(*ev) || skb->len < sizeof(*ev) +
  2629. ev->num_hndl * sizeof(struct hci_comp_pkts_info)) {
  2630. BT_DBG("%s bad parameters", hdev->name);
  2631. return;
  2632. }
  2633. BT_DBG("%s num_hndl %d", hdev->name, ev->num_hndl);
  2634. for (i = 0; i < ev->num_hndl; i++) {
  2635. struct hci_comp_pkts_info *info = &ev->handles[i];
  2636. struct hci_conn *conn;
  2637. __u16 handle, count;
  2638. handle = __le16_to_cpu(info->handle);
  2639. count = __le16_to_cpu(info->count);
  2640. conn = hci_conn_hash_lookup_handle(hdev, handle);
  2641. if (!conn)
  2642. continue;
  2643. conn->sent -= count;
  2644. switch (conn->type) {
  2645. case ACL_LINK:
  2646. hdev->acl_cnt += count;
  2647. if (hdev->acl_cnt > hdev->acl_pkts)
  2648. hdev->acl_cnt = hdev->acl_pkts;
  2649. break;
  2650. case LE_LINK:
  2651. if (hdev->le_pkts) {
  2652. hdev->le_cnt += count;
  2653. if (hdev->le_cnt > hdev->le_pkts)
  2654. hdev->le_cnt = hdev->le_pkts;
  2655. } else {
  2656. hdev->acl_cnt += count;
  2657. if (hdev->acl_cnt > hdev->acl_pkts)
  2658. hdev->acl_cnt = hdev->acl_pkts;
  2659. }
  2660. break;
  2661. case SCO_LINK:
  2662. hdev->sco_cnt += count;
  2663. if (hdev->sco_cnt > hdev->sco_pkts)
  2664. hdev->sco_cnt = hdev->sco_pkts;
  2665. break;
  2666. default:
  2667. bt_dev_err(hdev, "unknown type %d conn %p",
  2668. conn->type, conn);
  2669. break;
  2670. }
  2671. }
  2672. queue_work(hdev->workqueue, &hdev->tx_work);
  2673. }
  2674. static struct hci_conn *__hci_conn_lookup_handle(struct hci_dev *hdev,
  2675. __u16 handle)
  2676. {
  2677. struct hci_chan *chan;
  2678. switch (hdev->dev_type) {
  2679. case HCI_PRIMARY:
  2680. return hci_conn_hash_lookup_handle(hdev, handle);
  2681. case HCI_AMP:
  2682. chan = hci_chan_lookup_handle(hdev, handle);
  2683. if (chan)
  2684. return chan->conn;
  2685. break;
  2686. default:
  2687. bt_dev_err(hdev, "unknown dev_type %d", hdev->dev_type);
  2688. break;
  2689. }
  2690. return NULL;
  2691. }
  2692. static void hci_num_comp_blocks_evt(struct hci_dev *hdev, struct sk_buff *skb)
  2693. {
  2694. struct hci_ev_num_comp_blocks *ev = (void *) skb->data;
  2695. int i;
  2696. if (hdev->flow_ctl_mode != HCI_FLOW_CTL_MODE_BLOCK_BASED) {
  2697. bt_dev_err(hdev, "wrong event for mode %d", hdev->flow_ctl_mode);
  2698. return;
  2699. }
  2700. if (skb->len < sizeof(*ev) || skb->len < sizeof(*ev) +
  2701. ev->num_hndl * sizeof(struct hci_comp_blocks_info)) {
  2702. BT_DBG("%s bad parameters", hdev->name);
  2703. return;
  2704. }
  2705. BT_DBG("%s num_blocks %d num_hndl %d", hdev->name, ev->num_blocks,
  2706. ev->num_hndl);
  2707. for (i = 0; i < ev->num_hndl; i++) {
  2708. struct hci_comp_blocks_info *info = &ev->handles[i];
  2709. struct hci_conn *conn = NULL;
  2710. __u16 handle, block_count;
  2711. handle = __le16_to_cpu(info->handle);
  2712. block_count = __le16_to_cpu(info->blocks);
  2713. conn = __hci_conn_lookup_handle(hdev, handle);
  2714. if (!conn)
  2715. continue;
  2716. conn->sent -= block_count;
  2717. switch (conn->type) {
  2718. case ACL_LINK:
  2719. case AMP_LINK:
  2720. hdev->block_cnt += block_count;
  2721. if (hdev->block_cnt > hdev->num_blocks)
  2722. hdev->block_cnt = hdev->num_blocks;
  2723. break;
  2724. default:
  2725. bt_dev_err(hdev, "unknown type %d conn %p",
  2726. conn->type, conn);
  2727. break;
  2728. }
  2729. }
  2730. queue_work(hdev->workqueue, &hdev->tx_work);
  2731. }
  2732. static void hci_mode_change_evt(struct hci_dev *hdev, struct sk_buff *skb)
  2733. {
  2734. struct hci_ev_mode_change *ev = (void *) skb->data;
  2735. struct hci_conn *conn;
  2736. BT_DBG("%s status 0x%2.2x", hdev->name, ev->status);
  2737. hci_dev_lock(hdev);
  2738. conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
  2739. if (conn) {
  2740. conn->mode = ev->mode;
  2741. if (!test_and_clear_bit(HCI_CONN_MODE_CHANGE_PEND,
  2742. &conn->flags)) {
  2743. if (conn->mode == HCI_CM_ACTIVE)
  2744. set_bit(HCI_CONN_POWER_SAVE, &conn->flags);
  2745. else
  2746. clear_bit(HCI_CONN_POWER_SAVE, &conn->flags);
  2747. }
  2748. if (test_and_clear_bit(HCI_CONN_SCO_SETUP_PEND, &conn->flags))
  2749. hci_sco_setup(conn, ev->status);
  2750. }
  2751. hci_dev_unlock(hdev);
  2752. }
  2753. static void hci_pin_code_request_evt(struct hci_dev *hdev, struct sk_buff *skb)
  2754. {
  2755. struct hci_ev_pin_code_req *ev = (void *) skb->data;
  2756. struct hci_conn *conn;
  2757. BT_DBG("%s", hdev->name);
  2758. hci_dev_lock(hdev);
  2759. conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &ev->bdaddr);
  2760. if (!conn)
  2761. goto unlock;
  2762. if (conn->state == BT_CONNECTED) {
  2763. hci_conn_hold(conn);
  2764. conn->disc_timeout = HCI_PAIRING_TIMEOUT;
  2765. hci_conn_drop(conn);
  2766. }
  2767. if (!hci_dev_test_flag(hdev, HCI_BONDABLE) &&
  2768. !test_bit(HCI_CONN_AUTH_INITIATOR, &conn->flags)) {
  2769. hci_send_cmd(hdev, HCI_OP_PIN_CODE_NEG_REPLY,
  2770. sizeof(ev->bdaddr), &ev->bdaddr);
  2771. } else if (hci_dev_test_flag(hdev, HCI_MGMT)) {
  2772. u8 secure;
  2773. if (conn->pending_sec_level == BT_SECURITY_HIGH)
  2774. secure = 1;
  2775. else
  2776. secure = 0;
  2777. mgmt_pin_code_request(hdev, &ev->bdaddr, secure);
  2778. }
  2779. unlock:
  2780. hci_dev_unlock(hdev);
  2781. }
  2782. static void conn_set_key(struct hci_conn *conn, u8 key_type, u8 pin_len)
  2783. {
  2784. if (key_type == HCI_LK_CHANGED_COMBINATION)
  2785. return;
  2786. conn->pin_length = pin_len;
  2787. conn->key_type = key_type;
  2788. switch (key_type) {
  2789. case HCI_LK_LOCAL_UNIT:
  2790. case HCI_LK_REMOTE_UNIT:
  2791. case HCI_LK_DEBUG_COMBINATION:
  2792. return;
  2793. case HCI_LK_COMBINATION:
  2794. if (pin_len == 16)
  2795. conn->pending_sec_level = BT_SECURITY_HIGH;
  2796. else
  2797. conn->pending_sec_level = BT_SECURITY_MEDIUM;
  2798. break;
  2799. case HCI_LK_UNAUTH_COMBINATION_P192:
  2800. case HCI_LK_UNAUTH_COMBINATION_P256:
  2801. conn->pending_sec_level = BT_SECURITY_MEDIUM;
  2802. break;
  2803. case HCI_LK_AUTH_COMBINATION_P192:
  2804. conn->pending_sec_level = BT_SECURITY_HIGH;
  2805. break;
  2806. case HCI_LK_AUTH_COMBINATION_P256:
  2807. conn->pending_sec_level = BT_SECURITY_FIPS;
  2808. break;
  2809. }
  2810. }
  2811. static void hci_link_key_request_evt(struct hci_dev *hdev, struct sk_buff *skb)
  2812. {
  2813. struct hci_ev_link_key_req *ev = (void *) skb->data;
  2814. struct hci_cp_link_key_reply cp;
  2815. struct hci_conn *conn;
  2816. struct link_key *key;
  2817. BT_DBG("%s", hdev->name);
  2818. if (!hci_dev_test_flag(hdev, HCI_MGMT))
  2819. return;
  2820. hci_dev_lock(hdev);
  2821. key = hci_find_link_key(hdev, &ev->bdaddr);
  2822. if (!key) {
  2823. BT_DBG("%s link key not found for %pMR", hdev->name,
  2824. &ev->bdaddr);
  2825. goto not_found;
  2826. }
  2827. BT_DBG("%s found key type %u for %pMR", hdev->name, key->type,
  2828. &ev->bdaddr);
  2829. conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &ev->bdaddr);
  2830. if (conn) {
  2831. clear_bit(HCI_CONN_NEW_LINK_KEY, &conn->flags);
  2832. if ((key->type == HCI_LK_UNAUTH_COMBINATION_P192 ||
  2833. key->type == HCI_LK_UNAUTH_COMBINATION_P256) &&
  2834. conn->auth_type != 0xff && (conn->auth_type & 0x01)) {
  2835. BT_DBG("%s ignoring unauthenticated key", hdev->name);
  2836. goto not_found;
  2837. }
  2838. if (key->type == HCI_LK_COMBINATION && key->pin_len < 16 &&
  2839. (conn->pending_sec_level == BT_SECURITY_HIGH ||
  2840. conn->pending_sec_level == BT_SECURITY_FIPS)) {
  2841. BT_DBG("%s ignoring key unauthenticated for high security",
  2842. hdev->name);
  2843. goto not_found;
  2844. }
  2845. conn_set_key(conn, key->type, key->pin_len);
  2846. }
  2847. bacpy(&cp.bdaddr, &ev->bdaddr);
  2848. memcpy(cp.link_key, key->val, HCI_LINK_KEY_SIZE);
  2849. hci_send_cmd(hdev, HCI_OP_LINK_KEY_REPLY, sizeof(cp), &cp);
  2850. hci_dev_unlock(hdev);
  2851. return;
  2852. not_found:
  2853. hci_send_cmd(hdev, HCI_OP_LINK_KEY_NEG_REPLY, 6, &ev->bdaddr);
  2854. hci_dev_unlock(hdev);
  2855. }
  2856. static void hci_link_key_notify_evt(struct hci_dev *hdev, struct sk_buff *skb)
  2857. {
  2858. struct hci_ev_link_key_notify *ev = (void *) skb->data;
  2859. struct hci_conn *conn;
  2860. struct link_key *key;
  2861. bool persistent;
  2862. u8 pin_len = 0;
  2863. BT_DBG("%s", hdev->name);
  2864. hci_dev_lock(hdev);
  2865. conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &ev->bdaddr);
  2866. if (!conn)
  2867. goto unlock;
  2868. hci_conn_hold(conn);
  2869. conn->disc_timeout = HCI_DISCONN_TIMEOUT;
  2870. hci_conn_drop(conn);
  2871. set_bit(HCI_CONN_NEW_LINK_KEY, &conn->flags);
  2872. conn_set_key(conn, ev->key_type, conn->pin_length);
  2873. if (!hci_dev_test_flag(hdev, HCI_MGMT))
  2874. goto unlock;
  2875. key = hci_add_link_key(hdev, conn, &ev->bdaddr, ev->link_key,
  2876. ev->key_type, pin_len, &persistent);
  2877. if (!key)
  2878. goto unlock;
  2879. /* Update connection information since adding the key will have
  2880. * fixed up the type in the case of changed combination keys.
  2881. */
  2882. if (ev->key_type == HCI_LK_CHANGED_COMBINATION)
  2883. conn_set_key(conn, key->type, key->pin_len);
  2884. mgmt_new_link_key(hdev, key, persistent);
  2885. /* Keep debug keys around only if the HCI_KEEP_DEBUG_KEYS flag
  2886. * is set. If it's not set simply remove the key from the kernel
  2887. * list (we've still notified user space about it but with
  2888. * store_hint being 0).
  2889. */
  2890. if (key->type == HCI_LK_DEBUG_COMBINATION &&
  2891. !hci_dev_test_flag(hdev, HCI_KEEP_DEBUG_KEYS)) {
  2892. list_del_rcu(&key->list);
  2893. kfree_rcu(key, rcu);
  2894. goto unlock;
  2895. }
  2896. if (persistent)
  2897. clear_bit(HCI_CONN_FLUSH_KEY, &conn->flags);
  2898. else
  2899. set_bit(HCI_CONN_FLUSH_KEY, &conn->flags);
  2900. unlock:
  2901. hci_dev_unlock(hdev);
  2902. }
  2903. static void hci_clock_offset_evt(struct hci_dev *hdev, struct sk_buff *skb)
  2904. {
  2905. struct hci_ev_clock_offset *ev = (void *) skb->data;
  2906. struct hci_conn *conn;
  2907. BT_DBG("%s status 0x%2.2x", hdev->name, ev->status);
  2908. hci_dev_lock(hdev);
  2909. conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
  2910. if (conn && !ev->status) {
  2911. struct inquiry_entry *ie;
  2912. ie = hci_inquiry_cache_lookup(hdev, &conn->dst);
  2913. if (ie) {
  2914. ie->data.clock_offset = ev->clock_offset;
  2915. ie->timestamp = jiffies;
  2916. }
  2917. }
  2918. hci_dev_unlock(hdev);
  2919. }
  2920. static void hci_pkt_type_change_evt(struct hci_dev *hdev, struct sk_buff *skb)
  2921. {
  2922. struct hci_ev_pkt_type_change *ev = (void *) skb->data;
  2923. struct hci_conn *conn;
  2924. BT_DBG("%s status 0x%2.2x", hdev->name, ev->status);
  2925. hci_dev_lock(hdev);
  2926. conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
  2927. if (conn && !ev->status)
  2928. conn->pkt_type = __le16_to_cpu(ev->pkt_type);
  2929. hci_dev_unlock(hdev);
  2930. }
  2931. static void hci_pscan_rep_mode_evt(struct hci_dev *hdev, struct sk_buff *skb)
  2932. {
  2933. struct hci_ev_pscan_rep_mode *ev = (void *) skb->data;
  2934. struct inquiry_entry *ie;
  2935. BT_DBG("%s", hdev->name);
  2936. hci_dev_lock(hdev);
  2937. ie = hci_inquiry_cache_lookup(hdev, &ev->bdaddr);
  2938. if (ie) {
  2939. ie->data.pscan_rep_mode = ev->pscan_rep_mode;
  2940. ie->timestamp = jiffies;
  2941. }
  2942. hci_dev_unlock(hdev);
  2943. }
  2944. static void hci_inquiry_result_with_rssi_evt(struct hci_dev *hdev,
  2945. struct sk_buff *skb)
  2946. {
  2947. struct inquiry_data data;
  2948. int num_rsp = *((__u8 *) skb->data);
  2949. BT_DBG("%s num_rsp %d", hdev->name, num_rsp);
  2950. if (!num_rsp)
  2951. return;
  2952. if (hci_dev_test_flag(hdev, HCI_PERIODIC_INQ))
  2953. return;
  2954. hci_dev_lock(hdev);
  2955. if ((skb->len - 1) / num_rsp != sizeof(struct inquiry_info_with_rssi)) {
  2956. struct inquiry_info_with_rssi_and_pscan_mode *info;
  2957. info = (void *) (skb->data + 1);
  2958. for (; num_rsp; num_rsp--, info++) {
  2959. u32 flags;
  2960. bacpy(&data.bdaddr, &info->bdaddr);
  2961. data.pscan_rep_mode = info->pscan_rep_mode;
  2962. data.pscan_period_mode = info->pscan_period_mode;
  2963. data.pscan_mode = info->pscan_mode;
  2964. memcpy(data.dev_class, info->dev_class, 3);
  2965. data.clock_offset = info->clock_offset;
  2966. data.rssi = info->rssi;
  2967. data.ssp_mode = 0x00;
  2968. flags = hci_inquiry_cache_update(hdev, &data, false);
  2969. mgmt_device_found(hdev, &info->bdaddr, ACL_LINK, 0x00,
  2970. info->dev_class, info->rssi,
  2971. flags, NULL, 0, NULL, 0);
  2972. }
  2973. } else {
  2974. struct inquiry_info_with_rssi *info = (void *) (skb->data + 1);
  2975. for (; num_rsp; num_rsp--, info++) {
  2976. u32 flags;
  2977. bacpy(&data.bdaddr, &info->bdaddr);
  2978. data.pscan_rep_mode = info->pscan_rep_mode;
  2979. data.pscan_period_mode = info->pscan_period_mode;
  2980. data.pscan_mode = 0x00;
  2981. memcpy(data.dev_class, info->dev_class, 3);
  2982. data.clock_offset = info->clock_offset;
  2983. data.rssi = info->rssi;
  2984. data.ssp_mode = 0x00;
  2985. flags = hci_inquiry_cache_update(hdev, &data, false);
  2986. mgmt_device_found(hdev, &info->bdaddr, ACL_LINK, 0x00,
  2987. info->dev_class, info->rssi,
  2988. flags, NULL, 0, NULL, 0);
  2989. }
  2990. }
  2991. hci_dev_unlock(hdev);
  2992. }
  2993. static void hci_remote_ext_features_evt(struct hci_dev *hdev,
  2994. struct sk_buff *skb)
  2995. {
  2996. struct hci_ev_remote_ext_features *ev = (void *) skb->data;
  2997. struct hci_conn *conn;
  2998. BT_DBG("%s", hdev->name);
  2999. hci_dev_lock(hdev);
  3000. conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
  3001. if (!conn)
  3002. goto unlock;
  3003. if (ev->page < HCI_MAX_PAGES)
  3004. memcpy(conn->features[ev->page], ev->features, 8);
  3005. if (!ev->status && ev->page == 0x01) {
  3006. struct inquiry_entry *ie;
  3007. ie = hci_inquiry_cache_lookup(hdev, &conn->dst);
  3008. if (ie)
  3009. ie->data.ssp_mode = (ev->features[0] & LMP_HOST_SSP);
  3010. if (ev->features[0] & LMP_HOST_SSP) {
  3011. set_bit(HCI_CONN_SSP_ENABLED, &conn->flags);
  3012. } else {
  3013. /* It is mandatory by the Bluetooth specification that
  3014. * Extended Inquiry Results are only used when Secure
  3015. * Simple Pairing is enabled, but some devices violate
  3016. * this.
  3017. *
  3018. * To make these devices work, the internal SSP
  3019. * enabled flag needs to be cleared if the remote host
  3020. * features do not indicate SSP support */
  3021. clear_bit(HCI_CONN_SSP_ENABLED, &conn->flags);
  3022. }
  3023. if (ev->features[0] & LMP_HOST_SC)
  3024. set_bit(HCI_CONN_SC_ENABLED, &conn->flags);
  3025. }
  3026. if (conn->state != BT_CONFIG)
  3027. goto unlock;
  3028. if (!ev->status && !test_bit(HCI_CONN_MGMT_CONNECTED, &conn->flags)) {
  3029. struct hci_cp_remote_name_req cp;
  3030. memset(&cp, 0, sizeof(cp));
  3031. bacpy(&cp.bdaddr, &conn->dst);
  3032. cp.pscan_rep_mode = 0x02;
  3033. hci_send_cmd(hdev, HCI_OP_REMOTE_NAME_REQ, sizeof(cp), &cp);
  3034. } else if (!test_and_set_bit(HCI_CONN_MGMT_CONNECTED, &conn->flags))
  3035. mgmt_device_connected(hdev, conn, 0, NULL, 0);
  3036. if (!hci_outgoing_auth_needed(hdev, conn)) {
  3037. conn->state = BT_CONNECTED;
  3038. hci_connect_cfm(conn, ev->status);
  3039. hci_conn_drop(conn);
  3040. }
  3041. unlock:
  3042. hci_dev_unlock(hdev);
  3043. }
  3044. static void hci_sync_conn_complete_evt(struct hci_dev *hdev,
  3045. struct sk_buff *skb)
  3046. {
  3047. struct hci_ev_sync_conn_complete *ev = (void *) skb->data;
  3048. struct hci_conn *conn;
  3049. BT_DBG("%s status 0x%2.2x", hdev->name, ev->status);
  3050. hci_dev_lock(hdev);
  3051. conn = hci_conn_hash_lookup_ba(hdev, ev->link_type, &ev->bdaddr);
  3052. if (!conn) {
  3053. if (ev->link_type == ESCO_LINK)
  3054. goto unlock;
  3055. /* When the link type in the event indicates SCO connection
  3056. * and lookup of the connection object fails, then check
  3057. * if an eSCO connection object exists.
  3058. *
  3059. * The core limits the synchronous connections to either
  3060. * SCO or eSCO. The eSCO connection is preferred and tried
  3061. * to be setup first and until successfully established,
  3062. * the link type will be hinted as eSCO.
  3063. */
  3064. conn = hci_conn_hash_lookup_ba(hdev, ESCO_LINK, &ev->bdaddr);
  3065. if (!conn)
  3066. goto unlock;
  3067. }
  3068. switch (ev->status) {
  3069. case 0x00:
  3070. conn->handle = __le16_to_cpu(ev->handle);
  3071. conn->state = BT_CONNECTED;
  3072. conn->type = ev->link_type;
  3073. hci_debugfs_create_conn(conn);
  3074. hci_conn_add_sysfs(conn);
  3075. break;
  3076. case 0x10: /* Connection Accept Timeout */
  3077. case 0x0d: /* Connection Rejected due to Limited Resources */
  3078. case 0x11: /* Unsupported Feature or Parameter Value */
  3079. case 0x1c: /* SCO interval rejected */
  3080. case 0x1a: /* Unsupported Remote Feature */
  3081. case 0x1f: /* Unspecified error */
  3082. case 0x20: /* Unsupported LMP Parameter value */
  3083. if (conn->out) {
  3084. conn->pkt_type = (hdev->esco_type & SCO_ESCO_MASK) |
  3085. (hdev->esco_type & EDR_ESCO_MASK);
  3086. if (hci_setup_sync(conn, conn->link->handle))
  3087. goto unlock;
  3088. }
  3089. /* fall through */
  3090. default:
  3091. conn->state = BT_CLOSED;
  3092. break;
  3093. }
  3094. hci_connect_cfm(conn, ev->status);
  3095. if (ev->status)
  3096. hci_conn_del(conn);
  3097. unlock:
  3098. hci_dev_unlock(hdev);
  3099. }
  3100. static inline size_t eir_get_length(u8 *eir, size_t eir_len)
  3101. {
  3102. size_t parsed = 0;
  3103. while (parsed < eir_len) {
  3104. u8 field_len = eir[0];
  3105. if (field_len == 0)
  3106. return parsed;
  3107. parsed += field_len + 1;
  3108. eir += field_len + 1;
  3109. }
  3110. return eir_len;
  3111. }
  3112. static void hci_extended_inquiry_result_evt(struct hci_dev *hdev,
  3113. struct sk_buff *skb)
  3114. {
  3115. struct inquiry_data data;
  3116. struct extended_inquiry_info *info = (void *) (skb->data + 1);
  3117. int num_rsp = *((__u8 *) skb->data);
  3118. size_t eir_len;
  3119. BT_DBG("%s num_rsp %d", hdev->name, num_rsp);
  3120. if (!num_rsp)
  3121. return;
  3122. if (hci_dev_test_flag(hdev, HCI_PERIODIC_INQ))
  3123. return;
  3124. hci_dev_lock(hdev);
  3125. for (; num_rsp; num_rsp--, info++) {
  3126. u32 flags;
  3127. bool name_known;
  3128. bacpy(&data.bdaddr, &info->bdaddr);
  3129. data.pscan_rep_mode = info->pscan_rep_mode;
  3130. data.pscan_period_mode = info->pscan_period_mode;
  3131. data.pscan_mode = 0x00;
  3132. memcpy(data.dev_class, info->dev_class, 3);
  3133. data.clock_offset = info->clock_offset;
  3134. data.rssi = info->rssi;
  3135. data.ssp_mode = 0x01;
  3136. if (hci_dev_test_flag(hdev, HCI_MGMT))
  3137. name_known = eir_get_data(info->data,
  3138. sizeof(info->data),
  3139. EIR_NAME_COMPLETE, NULL);
  3140. else
  3141. name_known = true;
  3142. flags = hci_inquiry_cache_update(hdev, &data, name_known);
  3143. eir_len = eir_get_length(info->data, sizeof(info->data));
  3144. mgmt_device_found(hdev, &info->bdaddr, ACL_LINK, 0x00,
  3145. info->dev_class, info->rssi,
  3146. flags, info->data, eir_len, NULL, 0);
  3147. }
  3148. hci_dev_unlock(hdev);
  3149. }
  3150. static void hci_key_refresh_complete_evt(struct hci_dev *hdev,
  3151. struct sk_buff *skb)
  3152. {
  3153. struct hci_ev_key_refresh_complete *ev = (void *) skb->data;
  3154. struct hci_conn *conn;
  3155. BT_DBG("%s status 0x%2.2x handle 0x%4.4x", hdev->name, ev->status,
  3156. __le16_to_cpu(ev->handle));
  3157. hci_dev_lock(hdev);
  3158. conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
  3159. if (!conn)
  3160. goto unlock;
  3161. /* For BR/EDR the necessary steps are taken through the
  3162. * auth_complete event.
  3163. */
  3164. if (conn->type != LE_LINK)
  3165. goto unlock;
  3166. if (!ev->status)
  3167. conn->sec_level = conn->pending_sec_level;
  3168. clear_bit(HCI_CONN_ENCRYPT_PEND, &conn->flags);
  3169. if (ev->status && conn->state == BT_CONNECTED) {
  3170. hci_disconnect(conn, HCI_ERROR_AUTH_FAILURE);
  3171. hci_conn_drop(conn);
  3172. goto unlock;
  3173. }
  3174. if (conn->state == BT_CONFIG) {
  3175. if (!ev->status)
  3176. conn->state = BT_CONNECTED;
  3177. hci_connect_cfm(conn, ev->status);
  3178. hci_conn_drop(conn);
  3179. } else {
  3180. hci_auth_cfm(conn, ev->status);
  3181. hci_conn_hold(conn);
  3182. conn->disc_timeout = HCI_DISCONN_TIMEOUT;
  3183. hci_conn_drop(conn);
  3184. }
  3185. unlock:
  3186. hci_dev_unlock(hdev);
  3187. }
  3188. static u8 hci_get_auth_req(struct hci_conn *conn)
  3189. {
  3190. /* If remote requests no-bonding follow that lead */
  3191. if (conn->remote_auth == HCI_AT_NO_BONDING ||
  3192. conn->remote_auth == HCI_AT_NO_BONDING_MITM)
  3193. return conn->remote_auth | (conn->auth_type & 0x01);
  3194. /* If both remote and local have enough IO capabilities, require
  3195. * MITM protection
  3196. */
  3197. if (conn->remote_cap != HCI_IO_NO_INPUT_OUTPUT &&
  3198. conn->io_capability != HCI_IO_NO_INPUT_OUTPUT)
  3199. return conn->remote_auth | 0x01;
  3200. /* No MITM protection possible so ignore remote requirement */
  3201. return (conn->remote_auth & ~0x01) | (conn->auth_type & 0x01);
  3202. }
  3203. static u8 bredr_oob_data_present(struct hci_conn *conn)
  3204. {
  3205. struct hci_dev *hdev = conn->hdev;
  3206. struct oob_data *data;
  3207. data = hci_find_remote_oob_data(hdev, &conn->dst, BDADDR_BREDR);
  3208. if (!data)
  3209. return 0x00;
  3210. if (bredr_sc_enabled(hdev)) {
  3211. /* When Secure Connections is enabled, then just
  3212. * return the present value stored with the OOB
  3213. * data. The stored value contains the right present
  3214. * information. However it can only be trusted when
  3215. * not in Secure Connection Only mode.
  3216. */
  3217. if (!hci_dev_test_flag(hdev, HCI_SC_ONLY))
  3218. return data->present;
  3219. /* When Secure Connections Only mode is enabled, then
  3220. * the P-256 values are required. If they are not
  3221. * available, then do not declare that OOB data is
  3222. * present.
  3223. */
  3224. if (!memcmp(data->rand256, ZERO_KEY, 16) ||
  3225. !memcmp(data->hash256, ZERO_KEY, 16))
  3226. return 0x00;
  3227. return 0x02;
  3228. }
  3229. /* When Secure Connections is not enabled or actually
  3230. * not supported by the hardware, then check that if
  3231. * P-192 data values are present.
  3232. */
  3233. if (!memcmp(data->rand192, ZERO_KEY, 16) ||
  3234. !memcmp(data->hash192, ZERO_KEY, 16))
  3235. return 0x00;
  3236. return 0x01;
  3237. }
  3238. static void hci_io_capa_request_evt(struct hci_dev *hdev, struct sk_buff *skb)
  3239. {
  3240. struct hci_ev_io_capa_request *ev = (void *) skb->data;
  3241. struct hci_conn *conn;
  3242. BT_DBG("%s", hdev->name);
  3243. hci_dev_lock(hdev);
  3244. conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &ev->bdaddr);
  3245. if (!conn)
  3246. goto unlock;
  3247. hci_conn_hold(conn);
  3248. if (!hci_dev_test_flag(hdev, HCI_MGMT))
  3249. goto unlock;
  3250. /* Allow pairing if we're pairable, the initiators of the
  3251. * pairing or if the remote is not requesting bonding.
  3252. */
  3253. if (hci_dev_test_flag(hdev, HCI_BONDABLE) ||
  3254. test_bit(HCI_CONN_AUTH_INITIATOR, &conn->flags) ||
  3255. (conn->remote_auth & ~0x01) == HCI_AT_NO_BONDING) {
  3256. struct hci_cp_io_capability_reply cp;
  3257. bacpy(&cp.bdaddr, &ev->bdaddr);
  3258. /* Change the IO capability from KeyboardDisplay
  3259. * to DisplayYesNo as it is not supported by BT spec. */
  3260. cp.capability = (conn->io_capability == 0x04) ?
  3261. HCI_IO_DISPLAY_YESNO : conn->io_capability;
  3262. /* If we are initiators, there is no remote information yet */
  3263. if (conn->remote_auth == 0xff) {
  3264. /* Request MITM protection if our IO caps allow it
  3265. * except for the no-bonding case.
  3266. */
  3267. if (conn->io_capability != HCI_IO_NO_INPUT_OUTPUT &&
  3268. conn->auth_type != HCI_AT_NO_BONDING)
  3269. conn->auth_type |= 0x01;
  3270. } else {
  3271. conn->auth_type = hci_get_auth_req(conn);
  3272. }
  3273. /* If we're not bondable, force one of the non-bondable
  3274. * authentication requirement values.
  3275. */
  3276. if (!hci_dev_test_flag(hdev, HCI_BONDABLE))
  3277. conn->auth_type &= HCI_AT_NO_BONDING_MITM;
  3278. cp.authentication = conn->auth_type;
  3279. cp.oob_data = bredr_oob_data_present(conn);
  3280. hci_send_cmd(hdev, HCI_OP_IO_CAPABILITY_REPLY,
  3281. sizeof(cp), &cp);
  3282. } else {
  3283. struct hci_cp_io_capability_neg_reply cp;
  3284. bacpy(&cp.bdaddr, &ev->bdaddr);
  3285. cp.reason = HCI_ERROR_PAIRING_NOT_ALLOWED;
  3286. hci_send_cmd(hdev, HCI_OP_IO_CAPABILITY_NEG_REPLY,
  3287. sizeof(cp), &cp);
  3288. }
  3289. unlock:
  3290. hci_dev_unlock(hdev);
  3291. }
  3292. static void hci_io_capa_reply_evt(struct hci_dev *hdev, struct sk_buff *skb)
  3293. {
  3294. struct hci_ev_io_capa_reply *ev = (void *) skb->data;
  3295. struct hci_conn *conn;
  3296. BT_DBG("%s", hdev->name);
  3297. hci_dev_lock(hdev);
  3298. conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &ev->bdaddr);
  3299. if (!conn)
  3300. goto unlock;
  3301. conn->remote_cap = ev->capability;
  3302. conn->remote_auth = ev->authentication;
  3303. unlock:
  3304. hci_dev_unlock(hdev);
  3305. }
  3306. static void hci_user_confirm_request_evt(struct hci_dev *hdev,
  3307. struct sk_buff *skb)
  3308. {
  3309. struct hci_ev_user_confirm_req *ev = (void *) skb->data;
  3310. int loc_mitm, rem_mitm, confirm_hint = 0;
  3311. struct hci_conn *conn;
  3312. BT_DBG("%s", hdev->name);
  3313. hci_dev_lock(hdev);
  3314. if (!hci_dev_test_flag(hdev, HCI_MGMT))
  3315. goto unlock;
  3316. conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &ev->bdaddr);
  3317. if (!conn)
  3318. goto unlock;
  3319. loc_mitm = (conn->auth_type & 0x01);
  3320. rem_mitm = (conn->remote_auth & 0x01);
  3321. /* If we require MITM but the remote device can't provide that
  3322. * (it has NoInputNoOutput) then reject the confirmation
  3323. * request. We check the security level here since it doesn't
  3324. * necessarily match conn->auth_type.
  3325. */
  3326. if (conn->pending_sec_level > BT_SECURITY_MEDIUM &&
  3327. conn->remote_cap == HCI_IO_NO_INPUT_OUTPUT) {
  3328. BT_DBG("Rejecting request: remote device can't provide MITM");
  3329. hci_send_cmd(hdev, HCI_OP_USER_CONFIRM_NEG_REPLY,
  3330. sizeof(ev->bdaddr), &ev->bdaddr);
  3331. goto unlock;
  3332. }
  3333. /* If no side requires MITM protection; auto-accept */
  3334. if ((!loc_mitm || conn->remote_cap == HCI_IO_NO_INPUT_OUTPUT) &&
  3335. (!rem_mitm || conn->io_capability == HCI_IO_NO_INPUT_OUTPUT)) {
  3336. /* If we're not the initiators request authorization to
  3337. * proceed from user space (mgmt_user_confirm with
  3338. * confirm_hint set to 1). The exception is if neither
  3339. * side had MITM or if the local IO capability is
  3340. * NoInputNoOutput, in which case we do auto-accept
  3341. */
  3342. if (!test_bit(HCI_CONN_AUTH_PEND, &conn->flags) &&
  3343. conn->io_capability != HCI_IO_NO_INPUT_OUTPUT &&
  3344. (loc_mitm || rem_mitm)) {
  3345. BT_DBG("Confirming auto-accept as acceptor");
  3346. confirm_hint = 1;
  3347. goto confirm;
  3348. }
  3349. BT_DBG("Auto-accept of user confirmation with %ums delay",
  3350. hdev->auto_accept_delay);
  3351. if (hdev->auto_accept_delay > 0) {
  3352. int delay = msecs_to_jiffies(hdev->auto_accept_delay);
  3353. queue_delayed_work(conn->hdev->workqueue,
  3354. &conn->auto_accept_work, delay);
  3355. goto unlock;
  3356. }
  3357. hci_send_cmd(hdev, HCI_OP_USER_CONFIRM_REPLY,
  3358. sizeof(ev->bdaddr), &ev->bdaddr);
  3359. goto unlock;
  3360. }
  3361. confirm:
  3362. mgmt_user_confirm_request(hdev, &ev->bdaddr, ACL_LINK, 0,
  3363. le32_to_cpu(ev->passkey), confirm_hint);
  3364. unlock:
  3365. hci_dev_unlock(hdev);
  3366. }
  3367. static void hci_user_passkey_request_evt(struct hci_dev *hdev,
  3368. struct sk_buff *skb)
  3369. {
  3370. struct hci_ev_user_passkey_req *ev = (void *) skb->data;
  3371. BT_DBG("%s", hdev->name);
  3372. if (hci_dev_test_flag(hdev, HCI_MGMT))
  3373. mgmt_user_passkey_request(hdev, &ev->bdaddr, ACL_LINK, 0);
  3374. }
  3375. static void hci_user_passkey_notify_evt(struct hci_dev *hdev,
  3376. struct sk_buff *skb)
  3377. {
  3378. struct hci_ev_user_passkey_notify *ev = (void *) skb->data;
  3379. struct hci_conn *conn;
  3380. BT_DBG("%s", hdev->name);
  3381. conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &ev->bdaddr);
  3382. if (!conn)
  3383. return;
  3384. conn->passkey_notify = __le32_to_cpu(ev->passkey);
  3385. conn->passkey_entered = 0;
  3386. if (hci_dev_test_flag(hdev, HCI_MGMT))
  3387. mgmt_user_passkey_notify(hdev, &conn->dst, conn->type,
  3388. conn->dst_type, conn->passkey_notify,
  3389. conn->passkey_entered);
  3390. }
  3391. static void hci_keypress_notify_evt(struct hci_dev *hdev, struct sk_buff *skb)
  3392. {
  3393. struct hci_ev_keypress_notify *ev = (void *) skb->data;
  3394. struct hci_conn *conn;
  3395. BT_DBG("%s", hdev->name);
  3396. conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &ev->bdaddr);
  3397. if (!conn)
  3398. return;
  3399. switch (ev->type) {
  3400. case HCI_KEYPRESS_STARTED:
  3401. conn->passkey_entered = 0;
  3402. return;
  3403. case HCI_KEYPRESS_ENTERED:
  3404. conn->passkey_entered++;
  3405. break;
  3406. case HCI_KEYPRESS_ERASED:
  3407. conn->passkey_entered--;
  3408. break;
  3409. case HCI_KEYPRESS_CLEARED:
  3410. conn->passkey_entered = 0;
  3411. break;
  3412. case HCI_KEYPRESS_COMPLETED:
  3413. return;
  3414. }
  3415. if (hci_dev_test_flag(hdev, HCI_MGMT))
  3416. mgmt_user_passkey_notify(hdev, &conn->dst, conn->type,
  3417. conn->dst_type, conn->passkey_notify,
  3418. conn->passkey_entered);
  3419. }
  3420. static void hci_simple_pair_complete_evt(struct hci_dev *hdev,
  3421. struct sk_buff *skb)
  3422. {
  3423. struct hci_ev_simple_pair_complete *ev = (void *) skb->data;
  3424. struct hci_conn *conn;
  3425. BT_DBG("%s", hdev->name);
  3426. hci_dev_lock(hdev);
  3427. conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &ev->bdaddr);
  3428. if (!conn)
  3429. goto unlock;
  3430. /* Reset the authentication requirement to unknown */
  3431. conn->remote_auth = 0xff;
  3432. /* To avoid duplicate auth_failed events to user space we check
  3433. * the HCI_CONN_AUTH_PEND flag which will be set if we
  3434. * initiated the authentication. A traditional auth_complete
  3435. * event gets always produced as initiator and is also mapped to
  3436. * the mgmt_auth_failed event */
  3437. if (!test_bit(HCI_CONN_AUTH_PEND, &conn->flags) && ev->status)
  3438. mgmt_auth_failed(conn, ev->status);
  3439. hci_conn_drop(conn);
  3440. unlock:
  3441. hci_dev_unlock(hdev);
  3442. }
  3443. static void hci_remote_host_features_evt(struct hci_dev *hdev,
  3444. struct sk_buff *skb)
  3445. {
  3446. struct hci_ev_remote_host_features *ev = (void *) skb->data;
  3447. struct inquiry_entry *ie;
  3448. struct hci_conn *conn;
  3449. BT_DBG("%s", hdev->name);
  3450. hci_dev_lock(hdev);
  3451. conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &ev->bdaddr);
  3452. if (conn)
  3453. memcpy(conn->features[1], ev->features, 8);
  3454. ie = hci_inquiry_cache_lookup(hdev, &ev->bdaddr);
  3455. if (ie)
  3456. ie->data.ssp_mode = (ev->features[0] & LMP_HOST_SSP);
  3457. hci_dev_unlock(hdev);
  3458. }
  3459. static void hci_remote_oob_data_request_evt(struct hci_dev *hdev,
  3460. struct sk_buff *skb)
  3461. {
  3462. struct hci_ev_remote_oob_data_request *ev = (void *) skb->data;
  3463. struct oob_data *data;
  3464. BT_DBG("%s", hdev->name);
  3465. hci_dev_lock(hdev);
  3466. if (!hci_dev_test_flag(hdev, HCI_MGMT))
  3467. goto unlock;
  3468. data = hci_find_remote_oob_data(hdev, &ev->bdaddr, BDADDR_BREDR);
  3469. if (!data) {
  3470. struct hci_cp_remote_oob_data_neg_reply cp;
  3471. bacpy(&cp.bdaddr, &ev->bdaddr);
  3472. hci_send_cmd(hdev, HCI_OP_REMOTE_OOB_DATA_NEG_REPLY,
  3473. sizeof(cp), &cp);
  3474. goto unlock;
  3475. }
  3476. if (bredr_sc_enabled(hdev)) {
  3477. struct hci_cp_remote_oob_ext_data_reply cp;
  3478. bacpy(&cp.bdaddr, &ev->bdaddr);
  3479. if (hci_dev_test_flag(hdev, HCI_SC_ONLY)) {
  3480. memset(cp.hash192, 0, sizeof(cp.hash192));
  3481. memset(cp.rand192, 0, sizeof(cp.rand192));
  3482. } else {
  3483. memcpy(cp.hash192, data->hash192, sizeof(cp.hash192));
  3484. memcpy(cp.rand192, data->rand192, sizeof(cp.rand192));
  3485. }
  3486. memcpy(cp.hash256, data->hash256, sizeof(cp.hash256));
  3487. memcpy(cp.rand256, data->rand256, sizeof(cp.rand256));
  3488. hci_send_cmd(hdev, HCI_OP_REMOTE_OOB_EXT_DATA_REPLY,
  3489. sizeof(cp), &cp);
  3490. } else {
  3491. struct hci_cp_remote_oob_data_reply cp;
  3492. bacpy(&cp.bdaddr, &ev->bdaddr);
  3493. memcpy(cp.hash, data->hash192, sizeof(cp.hash));
  3494. memcpy(cp.rand, data->rand192, sizeof(cp.rand));
  3495. hci_send_cmd(hdev, HCI_OP_REMOTE_OOB_DATA_REPLY,
  3496. sizeof(cp), &cp);
  3497. }
  3498. unlock:
  3499. hci_dev_unlock(hdev);
  3500. }
  3501. #if IS_ENABLED(CONFIG_BT_HS)
  3502. static void hci_chan_selected_evt(struct hci_dev *hdev, struct sk_buff *skb)
  3503. {
  3504. struct hci_ev_channel_selected *ev = (void *)skb->data;
  3505. struct hci_conn *hcon;
  3506. BT_DBG("%s handle 0x%2.2x", hdev->name, ev->phy_handle);
  3507. skb_pull(skb, sizeof(*ev));
  3508. hcon = hci_conn_hash_lookup_handle(hdev, ev->phy_handle);
  3509. if (!hcon)
  3510. return;
  3511. amp_read_loc_assoc_final_data(hdev, hcon);
  3512. }
  3513. static void hci_phy_link_complete_evt(struct hci_dev *hdev,
  3514. struct sk_buff *skb)
  3515. {
  3516. struct hci_ev_phy_link_complete *ev = (void *) skb->data;
  3517. struct hci_conn *hcon, *bredr_hcon;
  3518. BT_DBG("%s handle 0x%2.2x status 0x%2.2x", hdev->name, ev->phy_handle,
  3519. ev->status);
  3520. hci_dev_lock(hdev);
  3521. hcon = hci_conn_hash_lookup_handle(hdev, ev->phy_handle);
  3522. if (!hcon) {
  3523. hci_dev_unlock(hdev);
  3524. return;
  3525. }
  3526. if (ev->status) {
  3527. hci_conn_del(hcon);
  3528. hci_dev_unlock(hdev);
  3529. return;
  3530. }
  3531. bredr_hcon = hcon->amp_mgr->l2cap_conn->hcon;
  3532. hcon->state = BT_CONNECTED;
  3533. bacpy(&hcon->dst, &bredr_hcon->dst);
  3534. hci_conn_hold(hcon);
  3535. hcon->disc_timeout = HCI_DISCONN_TIMEOUT;
  3536. hci_conn_drop(hcon);
  3537. hci_debugfs_create_conn(hcon);
  3538. hci_conn_add_sysfs(hcon);
  3539. amp_physical_cfm(bredr_hcon, hcon);
  3540. hci_dev_unlock(hdev);
  3541. }
  3542. static void hci_loglink_complete_evt(struct hci_dev *hdev, struct sk_buff *skb)
  3543. {
  3544. struct hci_ev_logical_link_complete *ev = (void *) skb->data;
  3545. struct hci_conn *hcon;
  3546. struct hci_chan *hchan;
  3547. struct amp_mgr *mgr;
  3548. BT_DBG("%s log_handle 0x%4.4x phy_handle 0x%2.2x status 0x%2.2x",
  3549. hdev->name, le16_to_cpu(ev->handle), ev->phy_handle,
  3550. ev->status);
  3551. hcon = hci_conn_hash_lookup_handle(hdev, ev->phy_handle);
  3552. if (!hcon)
  3553. return;
  3554. /* Create AMP hchan */
  3555. hchan = hci_chan_create(hcon);
  3556. if (!hchan)
  3557. return;
  3558. hchan->handle = le16_to_cpu(ev->handle);
  3559. BT_DBG("hcon %p mgr %p hchan %p", hcon, hcon->amp_mgr, hchan);
  3560. mgr = hcon->amp_mgr;
  3561. if (mgr && mgr->bredr_chan) {
  3562. struct l2cap_chan *bredr_chan = mgr->bredr_chan;
  3563. l2cap_chan_lock(bredr_chan);
  3564. bredr_chan->conn->mtu = hdev->block_mtu;
  3565. l2cap_logical_cfm(bredr_chan, hchan, 0);
  3566. hci_conn_hold(hcon);
  3567. l2cap_chan_unlock(bredr_chan);
  3568. }
  3569. }
  3570. static void hci_disconn_loglink_complete_evt(struct hci_dev *hdev,
  3571. struct sk_buff *skb)
  3572. {
  3573. struct hci_ev_disconn_logical_link_complete *ev = (void *) skb->data;
  3574. struct hci_chan *hchan;
  3575. BT_DBG("%s log handle 0x%4.4x status 0x%2.2x", hdev->name,
  3576. le16_to_cpu(ev->handle), ev->status);
  3577. if (ev->status)
  3578. return;
  3579. hci_dev_lock(hdev);
  3580. hchan = hci_chan_lookup_handle(hdev, le16_to_cpu(ev->handle));
  3581. if (!hchan)
  3582. goto unlock;
  3583. amp_destroy_logical_link(hchan, ev->reason);
  3584. unlock:
  3585. hci_dev_unlock(hdev);
  3586. }
  3587. static void hci_disconn_phylink_complete_evt(struct hci_dev *hdev,
  3588. struct sk_buff *skb)
  3589. {
  3590. struct hci_ev_disconn_phy_link_complete *ev = (void *) skb->data;
  3591. struct hci_conn *hcon;
  3592. BT_DBG("%s status 0x%2.2x", hdev->name, ev->status);
  3593. if (ev->status)
  3594. return;
  3595. hci_dev_lock(hdev);
  3596. hcon = hci_conn_hash_lookup_handle(hdev, ev->phy_handle);
  3597. if (hcon) {
  3598. hcon->state = BT_CLOSED;
  3599. hci_conn_del(hcon);
  3600. }
  3601. hci_dev_unlock(hdev);
  3602. }
  3603. #endif
  3604. static void le_conn_complete_evt(struct hci_dev *hdev, u8 status,
  3605. bdaddr_t *bdaddr, u8 bdaddr_type, u8 role, u16 handle,
  3606. u16 interval, u16 latency, u16 supervision_timeout)
  3607. {
  3608. struct hci_conn_params *params;
  3609. struct hci_conn *conn;
  3610. struct smp_irk *irk;
  3611. u8 addr_type;
  3612. hci_dev_lock(hdev);
  3613. /* All controllers implicitly stop advertising in the event of a
  3614. * connection, so ensure that the state bit is cleared.
  3615. */
  3616. hci_dev_clear_flag(hdev, HCI_LE_ADV);
  3617. conn = hci_lookup_le_connect(hdev);
  3618. if (!conn) {
  3619. conn = hci_conn_add(hdev, LE_LINK, bdaddr, role);
  3620. if (!conn) {
  3621. bt_dev_err(hdev, "no memory for new connection");
  3622. goto unlock;
  3623. }
  3624. conn->dst_type = bdaddr_type;
  3625. /* If we didn't have a hci_conn object previously
  3626. * but we're in master role this must be something
  3627. * initiated using a white list. Since white list based
  3628. * connections are not "first class citizens" we don't
  3629. * have full tracking of them. Therefore, we go ahead
  3630. * with a "best effort" approach of determining the
  3631. * initiator address based on the HCI_PRIVACY flag.
  3632. */
  3633. if (conn->out) {
  3634. conn->resp_addr_type = bdaddr_type;
  3635. bacpy(&conn->resp_addr, bdaddr);
  3636. if (hci_dev_test_flag(hdev, HCI_PRIVACY)) {
  3637. conn->init_addr_type = ADDR_LE_DEV_RANDOM;
  3638. bacpy(&conn->init_addr, &hdev->rpa);
  3639. } else {
  3640. hci_copy_identity_address(hdev,
  3641. &conn->init_addr,
  3642. &conn->init_addr_type);
  3643. }
  3644. }
  3645. } else {
  3646. cancel_delayed_work(&conn->le_conn_timeout);
  3647. }
  3648. if (!conn->out) {
  3649. /* Set the responder (our side) address type based on
  3650. * the advertising address type.
  3651. */
  3652. conn->resp_addr_type = hdev->adv_addr_type;
  3653. if (hdev->adv_addr_type == ADDR_LE_DEV_RANDOM) {
  3654. /* In case of ext adv, resp_addr will be updated in
  3655. * Adv Terminated event.
  3656. */
  3657. if (!ext_adv_capable(hdev))
  3658. bacpy(&conn->resp_addr, &hdev->random_addr);
  3659. } else {
  3660. bacpy(&conn->resp_addr, &hdev->bdaddr);
  3661. }
  3662. conn->init_addr_type = bdaddr_type;
  3663. bacpy(&conn->init_addr, bdaddr);
  3664. /* For incoming connections, set the default minimum
  3665. * and maximum connection interval. They will be used
  3666. * to check if the parameters are in range and if not
  3667. * trigger the connection update procedure.
  3668. */
  3669. conn->le_conn_min_interval = hdev->le_conn_min_interval;
  3670. conn->le_conn_max_interval = hdev->le_conn_max_interval;
  3671. }
  3672. /* Lookup the identity address from the stored connection
  3673. * address and address type.
  3674. *
  3675. * When establishing connections to an identity address, the
  3676. * connection procedure will store the resolvable random
  3677. * address first. Now if it can be converted back into the
  3678. * identity address, start using the identity address from
  3679. * now on.
  3680. */
  3681. irk = hci_get_irk(hdev, &conn->dst, conn->dst_type);
  3682. if (irk) {
  3683. bacpy(&conn->dst, &irk->bdaddr);
  3684. conn->dst_type = irk->addr_type;
  3685. }
  3686. if (status) {
  3687. hci_le_conn_failed(conn, status);
  3688. goto unlock;
  3689. }
  3690. if (conn->dst_type == ADDR_LE_DEV_PUBLIC)
  3691. addr_type = BDADDR_LE_PUBLIC;
  3692. else
  3693. addr_type = BDADDR_LE_RANDOM;
  3694. /* Drop the connection if the device is blocked */
  3695. if (hci_bdaddr_list_lookup(&hdev->blacklist, &conn->dst, addr_type)) {
  3696. hci_conn_drop(conn);
  3697. goto unlock;
  3698. }
  3699. if (!test_and_set_bit(HCI_CONN_MGMT_CONNECTED, &conn->flags))
  3700. mgmt_device_connected(hdev, conn, 0, NULL, 0);
  3701. conn->sec_level = BT_SECURITY_LOW;
  3702. conn->handle = handle;
  3703. conn->state = BT_CONFIG;
  3704. conn->le_conn_interval = interval;
  3705. conn->le_conn_latency = latency;
  3706. conn->le_supv_timeout = supervision_timeout;
  3707. hci_debugfs_create_conn(conn);
  3708. hci_conn_add_sysfs(conn);
  3709. if (!status) {
  3710. /* The remote features procedure is defined for master
  3711. * role only. So only in case of an initiated connection
  3712. * request the remote features.
  3713. *
  3714. * If the local controller supports slave-initiated features
  3715. * exchange, then requesting the remote features in slave
  3716. * role is possible. Otherwise just transition into the
  3717. * connected state without requesting the remote features.
  3718. */
  3719. if (conn->out ||
  3720. (hdev->le_features[0] & HCI_LE_SLAVE_FEATURES)) {
  3721. struct hci_cp_le_read_remote_features cp;
  3722. cp.handle = __cpu_to_le16(conn->handle);
  3723. hci_send_cmd(hdev, HCI_OP_LE_READ_REMOTE_FEATURES,
  3724. sizeof(cp), &cp);
  3725. hci_conn_hold(conn);
  3726. } else {
  3727. conn->state = BT_CONNECTED;
  3728. hci_connect_cfm(conn, status);
  3729. }
  3730. } else {
  3731. hci_connect_cfm(conn, status);
  3732. }
  3733. params = hci_pend_le_action_lookup(&hdev->pend_le_conns, &conn->dst,
  3734. conn->dst_type);
  3735. if (params) {
  3736. list_del_init(&params->action);
  3737. if (params->conn) {
  3738. hci_conn_drop(params->conn);
  3739. hci_conn_put(params->conn);
  3740. params->conn = NULL;
  3741. }
  3742. }
  3743. unlock:
  3744. hci_update_background_scan(hdev);
  3745. hci_dev_unlock(hdev);
  3746. }
  3747. static void hci_le_conn_complete_evt(struct hci_dev *hdev, struct sk_buff *skb)
  3748. {
  3749. struct hci_ev_le_conn_complete *ev = (void *) skb->data;
  3750. BT_DBG("%s status 0x%2.2x", hdev->name, ev->status);
  3751. le_conn_complete_evt(hdev, ev->status, &ev->bdaddr, ev->bdaddr_type,
  3752. ev->role, le16_to_cpu(ev->handle),
  3753. le16_to_cpu(ev->interval),
  3754. le16_to_cpu(ev->latency),
  3755. le16_to_cpu(ev->supervision_timeout));
  3756. }
  3757. static void hci_le_enh_conn_complete_evt(struct hci_dev *hdev,
  3758. struct sk_buff *skb)
  3759. {
  3760. struct hci_ev_le_enh_conn_complete *ev = (void *) skb->data;
  3761. BT_DBG("%s status 0x%2.2x", hdev->name, ev->status);
  3762. le_conn_complete_evt(hdev, ev->status, &ev->bdaddr, ev->bdaddr_type,
  3763. ev->role, le16_to_cpu(ev->handle),
  3764. le16_to_cpu(ev->interval),
  3765. le16_to_cpu(ev->latency),
  3766. le16_to_cpu(ev->supervision_timeout));
  3767. }
  3768. static void hci_le_ext_adv_term_evt(struct hci_dev *hdev, struct sk_buff *skb)
  3769. {
  3770. struct hci_evt_le_ext_adv_set_term *ev = (void *) skb->data;
  3771. struct hci_conn *conn;
  3772. BT_DBG("%s status 0x%2.2x", hdev->name, ev->status);
  3773. if (ev->status)
  3774. return;
  3775. conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->conn_handle));
  3776. if (conn) {
  3777. struct adv_info *adv_instance;
  3778. if (hdev->adv_addr_type != ADDR_LE_DEV_RANDOM)
  3779. return;
  3780. if (!hdev->cur_adv_instance) {
  3781. bacpy(&conn->resp_addr, &hdev->random_addr);
  3782. return;
  3783. }
  3784. adv_instance = hci_find_adv_instance(hdev, hdev->cur_adv_instance);
  3785. if (adv_instance)
  3786. bacpy(&conn->resp_addr, &adv_instance->random_addr);
  3787. }
  3788. }
  3789. static void hci_le_conn_update_complete_evt(struct hci_dev *hdev,
  3790. struct sk_buff *skb)
  3791. {
  3792. struct hci_ev_le_conn_update_complete *ev = (void *) skb->data;
  3793. struct hci_conn *conn;
  3794. BT_DBG("%s status 0x%2.2x", hdev->name, ev->status);
  3795. if (ev->status)
  3796. return;
  3797. hci_dev_lock(hdev);
  3798. conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
  3799. if (conn) {
  3800. conn->le_conn_interval = le16_to_cpu(ev->interval);
  3801. conn->le_conn_latency = le16_to_cpu(ev->latency);
  3802. conn->le_supv_timeout = le16_to_cpu(ev->supervision_timeout);
  3803. }
  3804. hci_dev_unlock(hdev);
  3805. }
  3806. /* This function requires the caller holds hdev->lock */
  3807. static struct hci_conn *check_pending_le_conn(struct hci_dev *hdev,
  3808. bdaddr_t *addr,
  3809. u8 addr_type, u8 adv_type,
  3810. bdaddr_t *direct_rpa)
  3811. {
  3812. struct hci_conn *conn;
  3813. struct hci_conn_params *params;
  3814. /* If the event is not connectable don't proceed further */
  3815. if (adv_type != LE_ADV_IND && adv_type != LE_ADV_DIRECT_IND)
  3816. return NULL;
  3817. /* Ignore if the device is blocked */
  3818. if (hci_bdaddr_list_lookup(&hdev->blacklist, addr, addr_type))
  3819. return NULL;
  3820. /* Most controller will fail if we try to create new connections
  3821. * while we have an existing one in slave role.
  3822. */
  3823. if (hdev->conn_hash.le_num_slave > 0)
  3824. return NULL;
  3825. /* If we're not connectable only connect devices that we have in
  3826. * our pend_le_conns list.
  3827. */
  3828. params = hci_pend_le_action_lookup(&hdev->pend_le_conns, addr,
  3829. addr_type);
  3830. if (!params)
  3831. return NULL;
  3832. if (!params->explicit_connect) {
  3833. switch (params->auto_connect) {
  3834. case HCI_AUTO_CONN_DIRECT:
  3835. /* Only devices advertising with ADV_DIRECT_IND are
  3836. * triggering a connection attempt. This is allowing
  3837. * incoming connections from slave devices.
  3838. */
  3839. if (adv_type != LE_ADV_DIRECT_IND)
  3840. return NULL;
  3841. break;
  3842. case HCI_AUTO_CONN_ALWAYS:
  3843. /* Devices advertising with ADV_IND or ADV_DIRECT_IND
  3844. * are triggering a connection attempt. This means
  3845. * that incoming connectioms from slave device are
  3846. * accepted and also outgoing connections to slave
  3847. * devices are established when found.
  3848. */
  3849. break;
  3850. default:
  3851. return NULL;
  3852. }
  3853. }
  3854. conn = hci_connect_le(hdev, addr, addr_type, BT_SECURITY_LOW,
  3855. HCI_LE_AUTOCONN_TIMEOUT, HCI_ROLE_MASTER,
  3856. direct_rpa);
  3857. if (!IS_ERR(conn)) {
  3858. /* If HCI_AUTO_CONN_EXPLICIT is set, conn is already owned
  3859. * by higher layer that tried to connect, if no then
  3860. * store the pointer since we don't really have any
  3861. * other owner of the object besides the params that
  3862. * triggered it. This way we can abort the connection if
  3863. * the parameters get removed and keep the reference
  3864. * count consistent once the connection is established.
  3865. */
  3866. if (!params->explicit_connect)
  3867. params->conn = hci_conn_get(conn);
  3868. return conn;
  3869. }
  3870. switch (PTR_ERR(conn)) {
  3871. case -EBUSY:
  3872. /* If hci_connect() returns -EBUSY it means there is already
  3873. * an LE connection attempt going on. Since controllers don't
  3874. * support more than one connection attempt at the time, we
  3875. * don't consider this an error case.
  3876. */
  3877. break;
  3878. default:
  3879. BT_DBG("Failed to connect: err %ld", PTR_ERR(conn));
  3880. return NULL;
  3881. }
  3882. return NULL;
  3883. }
  3884. static void process_adv_report(struct hci_dev *hdev, u8 type, bdaddr_t *bdaddr,
  3885. u8 bdaddr_type, bdaddr_t *direct_addr,
  3886. u8 direct_addr_type, s8 rssi, u8 *data, u8 len)
  3887. {
  3888. struct discovery_state *d = &hdev->discovery;
  3889. struct smp_irk *irk;
  3890. struct hci_conn *conn;
  3891. bool match;
  3892. u32 flags;
  3893. u8 *ptr, real_len;
  3894. switch (type) {
  3895. case LE_ADV_IND:
  3896. case LE_ADV_DIRECT_IND:
  3897. case LE_ADV_SCAN_IND:
  3898. case LE_ADV_NONCONN_IND:
  3899. case LE_ADV_SCAN_RSP:
  3900. break;
  3901. default:
  3902. bt_dev_err_ratelimited(hdev, "unknown advertising packet "
  3903. "type: 0x%02x", type);
  3904. return;
  3905. }
  3906. /* Find the end of the data in case the report contains padded zero
  3907. * bytes at the end causing an invalid length value.
  3908. *
  3909. * When data is NULL, len is 0 so there is no need for extra ptr
  3910. * check as 'ptr < data + 0' is already false in such case.
  3911. */
  3912. for (ptr = data; ptr < data + len && *ptr; ptr += *ptr + 1) {
  3913. if (ptr + 1 + *ptr > data + len)
  3914. break;
  3915. }
  3916. real_len = ptr - data;
  3917. /* Adjust for actual length */
  3918. if (len != real_len) {
  3919. bt_dev_err_ratelimited(hdev, "advertising data len corrected");
  3920. len = real_len;
  3921. }
  3922. /* If the direct address is present, then this report is from
  3923. * a LE Direct Advertising Report event. In that case it is
  3924. * important to see if the address is matching the local
  3925. * controller address.
  3926. */
  3927. if (direct_addr) {
  3928. /* Only resolvable random addresses are valid for these
  3929. * kind of reports and others can be ignored.
  3930. */
  3931. if (!hci_bdaddr_is_rpa(direct_addr, direct_addr_type))
  3932. return;
  3933. /* If the controller is not using resolvable random
  3934. * addresses, then this report can be ignored.
  3935. */
  3936. if (!hci_dev_test_flag(hdev, HCI_PRIVACY))
  3937. return;
  3938. /* If the local IRK of the controller does not match
  3939. * with the resolvable random address provided, then
  3940. * this report can be ignored.
  3941. */
  3942. if (!smp_irk_matches(hdev, hdev->irk, direct_addr))
  3943. return;
  3944. }
  3945. /* Check if we need to convert to identity address */
  3946. irk = hci_get_irk(hdev, bdaddr, bdaddr_type);
  3947. if (irk) {
  3948. bdaddr = &irk->bdaddr;
  3949. bdaddr_type = irk->addr_type;
  3950. }
  3951. /* Check if we have been requested to connect to this device.
  3952. *
  3953. * direct_addr is set only for directed advertising reports (it is NULL
  3954. * for advertising reports) and is already verified to be RPA above.
  3955. */
  3956. conn = check_pending_le_conn(hdev, bdaddr, bdaddr_type, type,
  3957. direct_addr);
  3958. if (conn && type == LE_ADV_IND) {
  3959. /* Store report for later inclusion by
  3960. * mgmt_device_connected
  3961. */
  3962. memcpy(conn->le_adv_data, data, len);
  3963. conn->le_adv_data_len = len;
  3964. }
  3965. /* Passive scanning shouldn't trigger any device found events,
  3966. * except for devices marked as CONN_REPORT for which we do send
  3967. * device found events.
  3968. */
  3969. if (hdev->le_scan_type == LE_SCAN_PASSIVE) {
  3970. if (type == LE_ADV_DIRECT_IND)
  3971. return;
  3972. if (!hci_pend_le_action_lookup(&hdev->pend_le_reports,
  3973. bdaddr, bdaddr_type))
  3974. return;
  3975. if (type == LE_ADV_NONCONN_IND || type == LE_ADV_SCAN_IND)
  3976. flags = MGMT_DEV_FOUND_NOT_CONNECTABLE;
  3977. else
  3978. flags = 0;
  3979. mgmt_device_found(hdev, bdaddr, LE_LINK, bdaddr_type, NULL,
  3980. rssi, flags, data, len, NULL, 0);
  3981. return;
  3982. }
  3983. /* When receiving non-connectable or scannable undirected
  3984. * advertising reports, this means that the remote device is
  3985. * not connectable and then clearly indicate this in the
  3986. * device found event.
  3987. *
  3988. * When receiving a scan response, then there is no way to
  3989. * know if the remote device is connectable or not. However
  3990. * since scan responses are merged with a previously seen
  3991. * advertising report, the flags field from that report
  3992. * will be used.
  3993. *
  3994. * In the really unlikely case that a controller get confused
  3995. * and just sends a scan response event, then it is marked as
  3996. * not connectable as well.
  3997. */
  3998. if (type == LE_ADV_NONCONN_IND || type == LE_ADV_SCAN_IND ||
  3999. type == LE_ADV_SCAN_RSP)
  4000. flags = MGMT_DEV_FOUND_NOT_CONNECTABLE;
  4001. else
  4002. flags = 0;
  4003. /* If there's nothing pending either store the data from this
  4004. * event or send an immediate device found event if the data
  4005. * should not be stored for later.
  4006. */
  4007. if (!has_pending_adv_report(hdev)) {
  4008. /* If the report will trigger a SCAN_REQ store it for
  4009. * later merging.
  4010. */
  4011. if (type == LE_ADV_IND || type == LE_ADV_SCAN_IND) {
  4012. store_pending_adv_report(hdev, bdaddr, bdaddr_type,
  4013. rssi, flags, data, len);
  4014. return;
  4015. }
  4016. mgmt_device_found(hdev, bdaddr, LE_LINK, bdaddr_type, NULL,
  4017. rssi, flags, data, len, NULL, 0);
  4018. return;
  4019. }
  4020. /* Check if the pending report is for the same device as the new one */
  4021. match = (!bacmp(bdaddr, &d->last_adv_addr) &&
  4022. bdaddr_type == d->last_adv_addr_type);
  4023. /* If the pending data doesn't match this report or this isn't a
  4024. * scan response (e.g. we got a duplicate ADV_IND) then force
  4025. * sending of the pending data.
  4026. */
  4027. if (type != LE_ADV_SCAN_RSP || !match) {
  4028. /* Send out whatever is in the cache, but skip duplicates */
  4029. if (!match)
  4030. mgmt_device_found(hdev, &d->last_adv_addr, LE_LINK,
  4031. d->last_adv_addr_type, NULL,
  4032. d->last_adv_rssi, d->last_adv_flags,
  4033. d->last_adv_data,
  4034. d->last_adv_data_len, NULL, 0);
  4035. /* If the new report will trigger a SCAN_REQ store it for
  4036. * later merging.
  4037. */
  4038. if (type == LE_ADV_IND || type == LE_ADV_SCAN_IND) {
  4039. store_pending_adv_report(hdev, bdaddr, bdaddr_type,
  4040. rssi, flags, data, len);
  4041. return;
  4042. }
  4043. /* The advertising reports cannot be merged, so clear
  4044. * the pending report and send out a device found event.
  4045. */
  4046. clear_pending_adv_report(hdev);
  4047. mgmt_device_found(hdev, bdaddr, LE_LINK, bdaddr_type, NULL,
  4048. rssi, flags, data, len, NULL, 0);
  4049. return;
  4050. }
  4051. /* If we get here we've got a pending ADV_IND or ADV_SCAN_IND and
  4052. * the new event is a SCAN_RSP. We can therefore proceed with
  4053. * sending a merged device found event.
  4054. */
  4055. mgmt_device_found(hdev, &d->last_adv_addr, LE_LINK,
  4056. d->last_adv_addr_type, NULL, rssi, d->last_adv_flags,
  4057. d->last_adv_data, d->last_adv_data_len, data, len);
  4058. clear_pending_adv_report(hdev);
  4059. }
  4060. static void hci_le_adv_report_evt(struct hci_dev *hdev, struct sk_buff *skb)
  4061. {
  4062. u8 num_reports = skb->data[0];
  4063. void *ptr = &skb->data[1];
  4064. hci_dev_lock(hdev);
  4065. while (num_reports--) {
  4066. struct hci_ev_le_advertising_info *ev = ptr;
  4067. s8 rssi;
  4068. if (ev->length <= HCI_MAX_AD_LENGTH) {
  4069. rssi = ev->data[ev->length];
  4070. process_adv_report(hdev, ev->evt_type, &ev->bdaddr,
  4071. ev->bdaddr_type, NULL, 0, rssi,
  4072. ev->data, ev->length);
  4073. } else {
  4074. bt_dev_err(hdev, "Dropping invalid advertising data");
  4075. }
  4076. ptr += sizeof(*ev) + ev->length + 1;
  4077. }
  4078. hci_dev_unlock(hdev);
  4079. }
  4080. static u8 ext_evt_type_to_legacy(u16 evt_type)
  4081. {
  4082. if (evt_type & LE_EXT_ADV_LEGACY_PDU) {
  4083. switch (evt_type) {
  4084. case LE_LEGACY_ADV_IND:
  4085. return LE_ADV_IND;
  4086. case LE_LEGACY_ADV_DIRECT_IND:
  4087. return LE_ADV_DIRECT_IND;
  4088. case LE_LEGACY_ADV_SCAN_IND:
  4089. return LE_ADV_SCAN_IND;
  4090. case LE_LEGACY_NONCONN_IND:
  4091. return LE_ADV_NONCONN_IND;
  4092. case LE_LEGACY_SCAN_RSP_ADV:
  4093. case LE_LEGACY_SCAN_RSP_ADV_SCAN:
  4094. return LE_ADV_SCAN_RSP;
  4095. }
  4096. BT_ERR_RATELIMITED("Unknown advertising packet type: 0x%02x",
  4097. evt_type);
  4098. return LE_ADV_INVALID;
  4099. }
  4100. if (evt_type & LE_EXT_ADV_CONN_IND) {
  4101. if (evt_type & LE_EXT_ADV_DIRECT_IND)
  4102. return LE_ADV_DIRECT_IND;
  4103. return LE_ADV_IND;
  4104. }
  4105. if (evt_type & LE_EXT_ADV_SCAN_RSP)
  4106. return LE_ADV_SCAN_RSP;
  4107. if (evt_type & LE_EXT_ADV_SCAN_IND)
  4108. return LE_ADV_SCAN_IND;
  4109. if (evt_type == LE_EXT_ADV_NON_CONN_IND ||
  4110. evt_type & LE_EXT_ADV_DIRECT_IND)
  4111. return LE_ADV_NONCONN_IND;
  4112. BT_ERR_RATELIMITED("Unknown advertising packet type: 0x%02x",
  4113. evt_type);
  4114. return LE_ADV_INVALID;
  4115. }
  4116. static void hci_le_ext_adv_report_evt(struct hci_dev *hdev, struct sk_buff *skb)
  4117. {
  4118. u8 num_reports = skb->data[0];
  4119. void *ptr = &skb->data[1];
  4120. hci_dev_lock(hdev);
  4121. while (num_reports--) {
  4122. struct hci_ev_le_ext_adv_report *ev = ptr;
  4123. u8 legacy_evt_type;
  4124. u16 evt_type;
  4125. evt_type = __le16_to_cpu(ev->evt_type);
  4126. legacy_evt_type = ext_evt_type_to_legacy(evt_type);
  4127. if (legacy_evt_type != LE_ADV_INVALID) {
  4128. process_adv_report(hdev, legacy_evt_type, &ev->bdaddr,
  4129. ev->bdaddr_type, NULL, 0, ev->rssi,
  4130. ev->data, ev->length);
  4131. }
  4132. ptr += sizeof(*ev) + ev->length + 1;
  4133. }
  4134. hci_dev_unlock(hdev);
  4135. }
  4136. static void hci_le_remote_feat_complete_evt(struct hci_dev *hdev,
  4137. struct sk_buff *skb)
  4138. {
  4139. struct hci_ev_le_remote_feat_complete *ev = (void *)skb->data;
  4140. struct hci_conn *conn;
  4141. BT_DBG("%s status 0x%2.2x", hdev->name, ev->status);
  4142. hci_dev_lock(hdev);
  4143. conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
  4144. if (conn) {
  4145. if (!ev->status)
  4146. memcpy(conn->features[0], ev->features, 8);
  4147. if (conn->state == BT_CONFIG) {
  4148. __u8 status;
  4149. /* If the local controller supports slave-initiated
  4150. * features exchange, but the remote controller does
  4151. * not, then it is possible that the error code 0x1a
  4152. * for unsupported remote feature gets returned.
  4153. *
  4154. * In this specific case, allow the connection to
  4155. * transition into connected state and mark it as
  4156. * successful.
  4157. */
  4158. if ((hdev->le_features[0] & HCI_LE_SLAVE_FEATURES) &&
  4159. !conn->out && ev->status == 0x1a)
  4160. status = 0x00;
  4161. else
  4162. status = ev->status;
  4163. conn->state = BT_CONNECTED;
  4164. hci_connect_cfm(conn, status);
  4165. hci_conn_drop(conn);
  4166. }
  4167. }
  4168. hci_dev_unlock(hdev);
  4169. }
  4170. static void hci_le_ltk_request_evt(struct hci_dev *hdev, struct sk_buff *skb)
  4171. {
  4172. struct hci_ev_le_ltk_req *ev = (void *) skb->data;
  4173. struct hci_cp_le_ltk_reply cp;
  4174. struct hci_cp_le_ltk_neg_reply neg;
  4175. struct hci_conn *conn;
  4176. struct smp_ltk *ltk;
  4177. BT_DBG("%s handle 0x%4.4x", hdev->name, __le16_to_cpu(ev->handle));
  4178. hci_dev_lock(hdev);
  4179. conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
  4180. if (conn == NULL)
  4181. goto not_found;
  4182. ltk = hci_find_ltk(hdev, &conn->dst, conn->dst_type, conn->role);
  4183. if (!ltk)
  4184. goto not_found;
  4185. if (smp_ltk_is_sc(ltk)) {
  4186. /* With SC both EDiv and Rand are set to zero */
  4187. if (ev->ediv || ev->rand)
  4188. goto not_found;
  4189. } else {
  4190. /* For non-SC keys check that EDiv and Rand match */
  4191. if (ev->ediv != ltk->ediv || ev->rand != ltk->rand)
  4192. goto not_found;
  4193. }
  4194. memcpy(cp.ltk, ltk->val, ltk->enc_size);
  4195. memset(cp.ltk + ltk->enc_size, 0, sizeof(cp.ltk) - ltk->enc_size);
  4196. cp.handle = cpu_to_le16(conn->handle);
  4197. conn->pending_sec_level = smp_ltk_sec_level(ltk);
  4198. conn->enc_key_size = ltk->enc_size;
  4199. hci_send_cmd(hdev, HCI_OP_LE_LTK_REPLY, sizeof(cp), &cp);
  4200. /* Ref. Bluetooth Core SPEC pages 1975 and 2004. STK is a
  4201. * temporary key used to encrypt a connection following
  4202. * pairing. It is used during the Encrypted Session Setup to
  4203. * distribute the keys. Later, security can be re-established
  4204. * using a distributed LTK.
  4205. */
  4206. if (ltk->type == SMP_STK) {
  4207. set_bit(HCI_CONN_STK_ENCRYPT, &conn->flags);
  4208. list_del_rcu(&ltk->list);
  4209. kfree_rcu(ltk, rcu);
  4210. } else {
  4211. clear_bit(HCI_CONN_STK_ENCRYPT, &conn->flags);
  4212. }
  4213. hci_dev_unlock(hdev);
  4214. return;
  4215. not_found:
  4216. neg.handle = ev->handle;
  4217. hci_send_cmd(hdev, HCI_OP_LE_LTK_NEG_REPLY, sizeof(neg), &neg);
  4218. hci_dev_unlock(hdev);
  4219. }
  4220. static void send_conn_param_neg_reply(struct hci_dev *hdev, u16 handle,
  4221. u8 reason)
  4222. {
  4223. struct hci_cp_le_conn_param_req_neg_reply cp;
  4224. cp.handle = cpu_to_le16(handle);
  4225. cp.reason = reason;
  4226. hci_send_cmd(hdev, HCI_OP_LE_CONN_PARAM_REQ_NEG_REPLY, sizeof(cp),
  4227. &cp);
  4228. }
  4229. static void hci_le_remote_conn_param_req_evt(struct hci_dev *hdev,
  4230. struct sk_buff *skb)
  4231. {
  4232. struct hci_ev_le_remote_conn_param_req *ev = (void *) skb->data;
  4233. struct hci_cp_le_conn_param_req_reply cp;
  4234. struct hci_conn *hcon;
  4235. u16 handle, min, max, latency, timeout;
  4236. handle = le16_to_cpu(ev->handle);
  4237. min = le16_to_cpu(ev->interval_min);
  4238. max = le16_to_cpu(ev->interval_max);
  4239. latency = le16_to_cpu(ev->latency);
  4240. timeout = le16_to_cpu(ev->timeout);
  4241. hcon = hci_conn_hash_lookup_handle(hdev, handle);
  4242. if (!hcon || hcon->state != BT_CONNECTED)
  4243. return send_conn_param_neg_reply(hdev, handle,
  4244. HCI_ERROR_UNKNOWN_CONN_ID);
  4245. if (hci_check_conn_params(min, max, latency, timeout))
  4246. return send_conn_param_neg_reply(hdev, handle,
  4247. HCI_ERROR_INVALID_LL_PARAMS);
  4248. if (hcon->role == HCI_ROLE_MASTER) {
  4249. struct hci_conn_params *params;
  4250. u8 store_hint;
  4251. hci_dev_lock(hdev);
  4252. params = hci_conn_params_lookup(hdev, &hcon->dst,
  4253. hcon->dst_type);
  4254. if (params) {
  4255. params->conn_min_interval = min;
  4256. params->conn_max_interval = max;
  4257. params->conn_latency = latency;
  4258. params->supervision_timeout = timeout;
  4259. store_hint = 0x01;
  4260. } else{
  4261. store_hint = 0x00;
  4262. }
  4263. hci_dev_unlock(hdev);
  4264. mgmt_new_conn_param(hdev, &hcon->dst, hcon->dst_type,
  4265. store_hint, min, max, latency, timeout);
  4266. }
  4267. cp.handle = ev->handle;
  4268. cp.interval_min = ev->interval_min;
  4269. cp.interval_max = ev->interval_max;
  4270. cp.latency = ev->latency;
  4271. cp.timeout = ev->timeout;
  4272. cp.min_ce_len = 0;
  4273. cp.max_ce_len = 0;
  4274. hci_send_cmd(hdev, HCI_OP_LE_CONN_PARAM_REQ_REPLY, sizeof(cp), &cp);
  4275. }
  4276. static void hci_le_direct_adv_report_evt(struct hci_dev *hdev,
  4277. struct sk_buff *skb)
  4278. {
  4279. u8 num_reports = skb->data[0];
  4280. void *ptr = &skb->data[1];
  4281. hci_dev_lock(hdev);
  4282. while (num_reports--) {
  4283. struct hci_ev_le_direct_adv_info *ev = ptr;
  4284. process_adv_report(hdev, ev->evt_type, &ev->bdaddr,
  4285. ev->bdaddr_type, &ev->direct_addr,
  4286. ev->direct_addr_type, ev->rssi, NULL, 0);
  4287. ptr += sizeof(*ev);
  4288. }
  4289. hci_dev_unlock(hdev);
  4290. }
  4291. static void hci_le_meta_evt(struct hci_dev *hdev, struct sk_buff *skb)
  4292. {
  4293. struct hci_ev_le_meta *le_ev = (void *) skb->data;
  4294. skb_pull(skb, sizeof(*le_ev));
  4295. switch (le_ev->subevent) {
  4296. case HCI_EV_LE_CONN_COMPLETE:
  4297. hci_le_conn_complete_evt(hdev, skb);
  4298. break;
  4299. case HCI_EV_LE_CONN_UPDATE_COMPLETE:
  4300. hci_le_conn_update_complete_evt(hdev, skb);
  4301. break;
  4302. case HCI_EV_LE_ADVERTISING_REPORT:
  4303. hci_le_adv_report_evt(hdev, skb);
  4304. break;
  4305. case HCI_EV_LE_REMOTE_FEAT_COMPLETE:
  4306. hci_le_remote_feat_complete_evt(hdev, skb);
  4307. break;
  4308. case HCI_EV_LE_LTK_REQ:
  4309. hci_le_ltk_request_evt(hdev, skb);
  4310. break;
  4311. case HCI_EV_LE_REMOTE_CONN_PARAM_REQ:
  4312. hci_le_remote_conn_param_req_evt(hdev, skb);
  4313. break;
  4314. case HCI_EV_LE_DIRECT_ADV_REPORT:
  4315. hci_le_direct_adv_report_evt(hdev, skb);
  4316. break;
  4317. case HCI_EV_LE_EXT_ADV_REPORT:
  4318. hci_le_ext_adv_report_evt(hdev, skb);
  4319. break;
  4320. case HCI_EV_LE_ENHANCED_CONN_COMPLETE:
  4321. hci_le_enh_conn_complete_evt(hdev, skb);
  4322. break;
  4323. case HCI_EV_LE_EXT_ADV_SET_TERM:
  4324. hci_le_ext_adv_term_evt(hdev, skb);
  4325. break;
  4326. default:
  4327. break;
  4328. }
  4329. }
  4330. static bool hci_get_cmd_complete(struct hci_dev *hdev, u16 opcode,
  4331. u8 event, struct sk_buff *skb)
  4332. {
  4333. struct hci_ev_cmd_complete *ev;
  4334. struct hci_event_hdr *hdr;
  4335. if (!skb)
  4336. return false;
  4337. if (skb->len < sizeof(*hdr)) {
  4338. bt_dev_err(hdev, "too short HCI event");
  4339. return false;
  4340. }
  4341. hdr = (void *) skb->data;
  4342. skb_pull(skb, HCI_EVENT_HDR_SIZE);
  4343. if (event) {
  4344. if (hdr->evt != event)
  4345. return false;
  4346. return true;
  4347. }
  4348. /* Check if request ended in Command Status - no way to retreive
  4349. * any extra parameters in this case.
  4350. */
  4351. if (hdr->evt == HCI_EV_CMD_STATUS)
  4352. return false;
  4353. if (hdr->evt != HCI_EV_CMD_COMPLETE) {
  4354. bt_dev_err(hdev, "last event is not cmd complete (0x%2.2x)",
  4355. hdr->evt);
  4356. return false;
  4357. }
  4358. if (skb->len < sizeof(*ev)) {
  4359. bt_dev_err(hdev, "too short cmd_complete event");
  4360. return false;
  4361. }
  4362. ev = (void *) skb->data;
  4363. skb_pull(skb, sizeof(*ev));
  4364. if (opcode != __le16_to_cpu(ev->opcode)) {
  4365. BT_DBG("opcode doesn't match (0x%2.2x != 0x%2.2x)", opcode,
  4366. __le16_to_cpu(ev->opcode));
  4367. return false;
  4368. }
  4369. return true;
  4370. }
  4371. void hci_event_packet(struct hci_dev *hdev, struct sk_buff *skb)
  4372. {
  4373. struct hci_event_hdr *hdr = (void *) skb->data;
  4374. hci_req_complete_t req_complete = NULL;
  4375. hci_req_complete_skb_t req_complete_skb = NULL;
  4376. struct sk_buff *orig_skb = NULL;
  4377. u8 status = 0, event = hdr->evt, req_evt = 0;
  4378. u16 opcode = HCI_OP_NOP;
  4379. if (hdev->sent_cmd && bt_cb(hdev->sent_cmd)->hci.req_event == event) {
  4380. struct hci_command_hdr *cmd_hdr = (void *) hdev->sent_cmd->data;
  4381. opcode = __le16_to_cpu(cmd_hdr->opcode);
  4382. hci_req_cmd_complete(hdev, opcode, status, &req_complete,
  4383. &req_complete_skb);
  4384. req_evt = event;
  4385. }
  4386. /* If it looks like we might end up having to call
  4387. * req_complete_skb, store a pristine copy of the skb since the
  4388. * various handlers may modify the original one through
  4389. * skb_pull() calls, etc.
  4390. */
  4391. if (req_complete_skb || event == HCI_EV_CMD_STATUS ||
  4392. event == HCI_EV_CMD_COMPLETE)
  4393. orig_skb = skb_clone(skb, GFP_KERNEL);
  4394. skb_pull(skb, HCI_EVENT_HDR_SIZE);
  4395. switch (event) {
  4396. case HCI_EV_INQUIRY_COMPLETE:
  4397. hci_inquiry_complete_evt(hdev, skb);
  4398. break;
  4399. case HCI_EV_INQUIRY_RESULT:
  4400. hci_inquiry_result_evt(hdev, skb);
  4401. break;
  4402. case HCI_EV_CONN_COMPLETE:
  4403. hci_conn_complete_evt(hdev, skb);
  4404. break;
  4405. case HCI_EV_CONN_REQUEST:
  4406. hci_conn_request_evt(hdev, skb);
  4407. break;
  4408. case HCI_EV_DISCONN_COMPLETE:
  4409. hci_disconn_complete_evt(hdev, skb);
  4410. break;
  4411. case HCI_EV_AUTH_COMPLETE:
  4412. hci_auth_complete_evt(hdev, skb);
  4413. break;
  4414. case HCI_EV_REMOTE_NAME:
  4415. hci_remote_name_evt(hdev, skb);
  4416. break;
  4417. case HCI_EV_ENCRYPT_CHANGE:
  4418. hci_encrypt_change_evt(hdev, skb);
  4419. break;
  4420. case HCI_EV_CHANGE_LINK_KEY_COMPLETE:
  4421. hci_change_link_key_complete_evt(hdev, skb);
  4422. break;
  4423. case HCI_EV_REMOTE_FEATURES:
  4424. hci_remote_features_evt(hdev, skb);
  4425. break;
  4426. case HCI_EV_CMD_COMPLETE:
  4427. hci_cmd_complete_evt(hdev, skb, &opcode, &status,
  4428. &req_complete, &req_complete_skb);
  4429. break;
  4430. case HCI_EV_CMD_STATUS:
  4431. hci_cmd_status_evt(hdev, skb, &opcode, &status, &req_complete,
  4432. &req_complete_skb);
  4433. break;
  4434. case HCI_EV_HARDWARE_ERROR:
  4435. hci_hardware_error_evt(hdev, skb);
  4436. break;
  4437. case HCI_EV_ROLE_CHANGE:
  4438. hci_role_change_evt(hdev, skb);
  4439. break;
  4440. case HCI_EV_NUM_COMP_PKTS:
  4441. hci_num_comp_pkts_evt(hdev, skb);
  4442. break;
  4443. case HCI_EV_MODE_CHANGE:
  4444. hci_mode_change_evt(hdev, skb);
  4445. break;
  4446. case HCI_EV_PIN_CODE_REQ:
  4447. hci_pin_code_request_evt(hdev, skb);
  4448. break;
  4449. case HCI_EV_LINK_KEY_REQ:
  4450. hci_link_key_request_evt(hdev, skb);
  4451. break;
  4452. case HCI_EV_LINK_KEY_NOTIFY:
  4453. hci_link_key_notify_evt(hdev, skb);
  4454. break;
  4455. case HCI_EV_CLOCK_OFFSET:
  4456. hci_clock_offset_evt(hdev, skb);
  4457. break;
  4458. case HCI_EV_PKT_TYPE_CHANGE:
  4459. hci_pkt_type_change_evt(hdev, skb);
  4460. break;
  4461. case HCI_EV_PSCAN_REP_MODE:
  4462. hci_pscan_rep_mode_evt(hdev, skb);
  4463. break;
  4464. case HCI_EV_INQUIRY_RESULT_WITH_RSSI:
  4465. hci_inquiry_result_with_rssi_evt(hdev, skb);
  4466. break;
  4467. case HCI_EV_REMOTE_EXT_FEATURES:
  4468. hci_remote_ext_features_evt(hdev, skb);
  4469. break;
  4470. case HCI_EV_SYNC_CONN_COMPLETE:
  4471. hci_sync_conn_complete_evt(hdev, skb);
  4472. break;
  4473. case HCI_EV_EXTENDED_INQUIRY_RESULT:
  4474. hci_extended_inquiry_result_evt(hdev, skb);
  4475. break;
  4476. case HCI_EV_KEY_REFRESH_COMPLETE:
  4477. hci_key_refresh_complete_evt(hdev, skb);
  4478. break;
  4479. case HCI_EV_IO_CAPA_REQUEST:
  4480. hci_io_capa_request_evt(hdev, skb);
  4481. break;
  4482. case HCI_EV_IO_CAPA_REPLY:
  4483. hci_io_capa_reply_evt(hdev, skb);
  4484. break;
  4485. case HCI_EV_USER_CONFIRM_REQUEST:
  4486. hci_user_confirm_request_evt(hdev, skb);
  4487. break;
  4488. case HCI_EV_USER_PASSKEY_REQUEST:
  4489. hci_user_passkey_request_evt(hdev, skb);
  4490. break;
  4491. case HCI_EV_USER_PASSKEY_NOTIFY:
  4492. hci_user_passkey_notify_evt(hdev, skb);
  4493. break;
  4494. case HCI_EV_KEYPRESS_NOTIFY:
  4495. hci_keypress_notify_evt(hdev, skb);
  4496. break;
  4497. case HCI_EV_SIMPLE_PAIR_COMPLETE:
  4498. hci_simple_pair_complete_evt(hdev, skb);
  4499. break;
  4500. case HCI_EV_REMOTE_HOST_FEATURES:
  4501. hci_remote_host_features_evt(hdev, skb);
  4502. break;
  4503. case HCI_EV_LE_META:
  4504. hci_le_meta_evt(hdev, skb);
  4505. break;
  4506. case HCI_EV_REMOTE_OOB_DATA_REQUEST:
  4507. hci_remote_oob_data_request_evt(hdev, skb);
  4508. break;
  4509. #if IS_ENABLED(CONFIG_BT_HS)
  4510. case HCI_EV_CHANNEL_SELECTED:
  4511. hci_chan_selected_evt(hdev, skb);
  4512. break;
  4513. case HCI_EV_PHY_LINK_COMPLETE:
  4514. hci_phy_link_complete_evt(hdev, skb);
  4515. break;
  4516. case HCI_EV_LOGICAL_LINK_COMPLETE:
  4517. hci_loglink_complete_evt(hdev, skb);
  4518. break;
  4519. case HCI_EV_DISCONN_LOGICAL_LINK_COMPLETE:
  4520. hci_disconn_loglink_complete_evt(hdev, skb);
  4521. break;
  4522. case HCI_EV_DISCONN_PHY_LINK_COMPLETE:
  4523. hci_disconn_phylink_complete_evt(hdev, skb);
  4524. break;
  4525. #endif
  4526. case HCI_EV_NUM_COMP_BLOCKS:
  4527. hci_num_comp_blocks_evt(hdev, skb);
  4528. break;
  4529. default:
  4530. BT_DBG("%s event 0x%2.2x", hdev->name, event);
  4531. break;
  4532. }
  4533. if (req_complete) {
  4534. req_complete(hdev, status, opcode);
  4535. } else if (req_complete_skb) {
  4536. if (!hci_get_cmd_complete(hdev, opcode, req_evt, orig_skb)) {
  4537. kfree_skb(orig_skb);
  4538. orig_skb = NULL;
  4539. }
  4540. req_complete_skb(hdev, status, opcode, orig_skb);
  4541. }
  4542. kfree_skb(orig_skb);
  4543. kfree_skb(skb);
  4544. hdev->stat.evt_rx++;
  4545. }