hci_event.c 193 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568
  1. /*
  2. BlueZ - Bluetooth protocol stack for Linux
  3. Copyright (c) 2000-2001, 2010, Code Aurora Forum. All rights reserved.
  4. Copyright 2023-2024 NXP
  5. Written 2000,2001 by Maxim Krasnyansky <maxk@qualcomm.com>
  6. This program is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License version 2 as
  8. published by the Free Software Foundation;
  9. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  10. OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  11. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS.
  12. IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) AND AUTHOR(S) BE LIABLE FOR ANY
  13. CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES
  14. WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  15. ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  16. OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  17. ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS,
  18. COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS
  19. SOFTWARE IS DISCLAIMED.
  20. */
  21. /* Bluetooth HCI event handling. */
  22. #include <linux/unaligned.h>
  23. #include <linux/crypto.h>
  24. #include <crypto/algapi.h>
  25. #include <net/bluetooth/bluetooth.h>
  26. #include <net/bluetooth/hci_core.h>
  27. #include <net/bluetooth/mgmt.h>
  28. #include "hci_debugfs.h"
  29. #include "hci_codec.h"
  30. #include "smp.h"
  31. #include "msft.h"
  32. #include "eir.h"
  33. #define ZERO_KEY "\x00\x00\x00\x00\x00\x00\x00\x00" \
  34. "\x00\x00\x00\x00\x00\x00\x00\x00"
  35. #define secs_to_jiffies(_secs) msecs_to_jiffies((_secs) * 1000)
  36. /* Handle HCI Event packets */
  37. static void *hci_ev_skb_pull(struct hci_dev *hdev, struct sk_buff *skb,
  38. u8 ev, size_t len)
  39. {
  40. void *data;
  41. data = skb_pull_data(skb, len);
  42. if (!data)
  43. bt_dev_err(hdev, "Malformed Event: 0x%2.2x", ev);
  44. return data;
  45. }
  46. static void *hci_cc_skb_pull(struct hci_dev *hdev, struct sk_buff *skb,
  47. u16 op, size_t len)
  48. {
  49. void *data;
  50. data = skb_pull_data(skb, len);
  51. if (!data)
  52. bt_dev_err(hdev, "Malformed Command Complete: 0x%4.4x", op);
  53. return data;
  54. }
  55. static void *hci_le_ev_skb_pull(struct hci_dev *hdev, struct sk_buff *skb,
  56. u8 ev, size_t len)
  57. {
  58. void *data;
  59. data = skb_pull_data(skb, len);
  60. if (!data)
  61. bt_dev_err(hdev, "Malformed LE Event: 0x%2.2x", ev);
  62. return data;
  63. }
  64. static u8 hci_cc_inquiry_cancel(struct hci_dev *hdev, void *data,
  65. struct sk_buff *skb)
  66. {
  67. struct hci_ev_status *rp = data;
  68. bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
  69. /* It is possible that we receive Inquiry Complete event right
  70. * before we receive Inquiry Cancel Command Complete event, in
  71. * which case the latter event should have status of Command
  72. * Disallowed. This should not be treated as error, since
  73. * we actually achieve what Inquiry Cancel wants to achieve,
  74. * which is to end the last Inquiry session.
  75. */
  76. if (rp->status == HCI_ERROR_COMMAND_DISALLOWED && !test_bit(HCI_INQUIRY, &hdev->flags)) {
  77. bt_dev_warn(hdev, "Ignoring error of Inquiry Cancel command");
  78. rp->status = 0x00;
  79. }
  80. if (rp->status)
  81. return rp->status;
  82. clear_bit(HCI_INQUIRY, &hdev->flags);
  83. smp_mb__after_atomic(); /* wake_up_bit advises about this barrier */
  84. wake_up_bit(&hdev->flags, HCI_INQUIRY);
  85. hci_dev_lock(hdev);
  86. /* Set discovery state to stopped if we're not doing LE active
  87. * scanning.
  88. */
  89. if (!hci_dev_test_flag(hdev, HCI_LE_SCAN) ||
  90. hdev->le_scan_type != LE_SCAN_ACTIVE)
  91. hci_discovery_set_state(hdev, DISCOVERY_STOPPED);
  92. hci_dev_unlock(hdev);
  93. return rp->status;
  94. }
  95. static u8 hci_cc_periodic_inq(struct hci_dev *hdev, void *data,
  96. struct sk_buff *skb)
  97. {
  98. struct hci_ev_status *rp = data;
  99. bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
  100. if (rp->status)
  101. return rp->status;
  102. hci_dev_set_flag(hdev, HCI_PERIODIC_INQ);
  103. return rp->status;
  104. }
  105. static u8 hci_cc_exit_periodic_inq(struct hci_dev *hdev, void *data,
  106. struct sk_buff *skb)
  107. {
  108. struct hci_ev_status *rp = data;
  109. bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
  110. if (rp->status)
  111. return rp->status;
  112. hci_dev_clear_flag(hdev, HCI_PERIODIC_INQ);
  113. return rp->status;
  114. }
  115. static u8 hci_cc_remote_name_req_cancel(struct hci_dev *hdev, void *data,
  116. struct sk_buff *skb)
  117. {
  118. struct hci_ev_status *rp = data;
  119. bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
  120. return rp->status;
  121. }
  122. static u8 hci_cc_role_discovery(struct hci_dev *hdev, void *data,
  123. struct sk_buff *skb)
  124. {
  125. struct hci_rp_role_discovery *rp = data;
  126. struct hci_conn *conn;
  127. bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
  128. if (rp->status)
  129. return rp->status;
  130. hci_dev_lock(hdev);
  131. conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(rp->handle));
  132. if (conn)
  133. conn->role = rp->role;
  134. hci_dev_unlock(hdev);
  135. return rp->status;
  136. }
  137. static u8 hci_cc_read_link_policy(struct hci_dev *hdev, void *data,
  138. struct sk_buff *skb)
  139. {
  140. struct hci_rp_read_link_policy *rp = data;
  141. struct hci_conn *conn;
  142. bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
  143. if (rp->status)
  144. return rp->status;
  145. hci_dev_lock(hdev);
  146. conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(rp->handle));
  147. if (conn)
  148. conn->link_policy = __le16_to_cpu(rp->policy);
  149. hci_dev_unlock(hdev);
  150. return rp->status;
  151. }
  152. static u8 hci_cc_write_link_policy(struct hci_dev *hdev, void *data,
  153. struct sk_buff *skb)
  154. {
  155. struct hci_rp_write_link_policy *rp = data;
  156. struct hci_conn *conn;
  157. void *sent;
  158. bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
  159. if (rp->status)
  160. return rp->status;
  161. sent = hci_sent_cmd_data(hdev, HCI_OP_WRITE_LINK_POLICY);
  162. if (!sent)
  163. return rp->status;
  164. hci_dev_lock(hdev);
  165. conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(rp->handle));
  166. if (conn)
  167. conn->link_policy = get_unaligned_le16(sent + 2);
  168. hci_dev_unlock(hdev);
  169. return rp->status;
  170. }
  171. static u8 hci_cc_read_def_link_policy(struct hci_dev *hdev, void *data,
  172. struct sk_buff *skb)
  173. {
  174. struct hci_rp_read_def_link_policy *rp = data;
  175. bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
  176. if (rp->status)
  177. return rp->status;
  178. hdev->link_policy = __le16_to_cpu(rp->policy);
  179. return rp->status;
  180. }
  181. static u8 hci_cc_write_def_link_policy(struct hci_dev *hdev, void *data,
  182. struct sk_buff *skb)
  183. {
  184. struct hci_ev_status *rp = data;
  185. void *sent;
  186. bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
  187. if (rp->status)
  188. return rp->status;
  189. sent = hci_sent_cmd_data(hdev, HCI_OP_WRITE_DEF_LINK_POLICY);
  190. if (!sent)
  191. return rp->status;
  192. hdev->link_policy = get_unaligned_le16(sent);
  193. return rp->status;
  194. }
  195. static u8 hci_cc_reset(struct hci_dev *hdev, void *data, struct sk_buff *skb)
  196. {
  197. struct hci_ev_status *rp = data;
  198. bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
  199. clear_bit(HCI_RESET, &hdev->flags);
  200. if (rp->status)
  201. return rp->status;
  202. /* Reset all non-persistent flags */
  203. hci_dev_clear_volatile_flags(hdev);
  204. hci_discovery_set_state(hdev, DISCOVERY_STOPPED);
  205. hdev->inq_tx_power = HCI_TX_POWER_INVALID;
  206. hdev->adv_tx_power = HCI_TX_POWER_INVALID;
  207. memset(hdev->adv_data, 0, sizeof(hdev->adv_data));
  208. hdev->adv_data_len = 0;
  209. memset(hdev->scan_rsp_data, 0, sizeof(hdev->scan_rsp_data));
  210. hdev->scan_rsp_data_len = 0;
  211. hdev->le_scan_type = LE_SCAN_PASSIVE;
  212. hdev->ssp_debug_mode = 0;
  213. hci_bdaddr_list_clear(&hdev->le_accept_list);
  214. hci_bdaddr_list_clear(&hdev->le_resolv_list);
  215. return rp->status;
  216. }
  217. static u8 hci_cc_read_stored_link_key(struct hci_dev *hdev, void *data,
  218. struct sk_buff *skb)
  219. {
  220. struct hci_rp_read_stored_link_key *rp = data;
  221. struct hci_cp_read_stored_link_key *sent;
  222. bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
  223. sent = hci_sent_cmd_data(hdev, HCI_OP_READ_STORED_LINK_KEY);
  224. if (!sent)
  225. return rp->status;
  226. if (!rp->status && sent->read_all == 0x01) {
  227. hdev->stored_max_keys = le16_to_cpu(rp->max_keys);
  228. hdev->stored_num_keys = le16_to_cpu(rp->num_keys);
  229. }
  230. return rp->status;
  231. }
  232. static u8 hci_cc_delete_stored_link_key(struct hci_dev *hdev, void *data,
  233. struct sk_buff *skb)
  234. {
  235. struct hci_rp_delete_stored_link_key *rp = data;
  236. u16 num_keys;
  237. bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
  238. if (rp->status)
  239. return rp->status;
  240. num_keys = le16_to_cpu(rp->num_keys);
  241. if (num_keys <= hdev->stored_num_keys)
  242. hdev->stored_num_keys -= num_keys;
  243. else
  244. hdev->stored_num_keys = 0;
  245. return rp->status;
  246. }
  247. static u8 hci_cc_write_local_name(struct hci_dev *hdev, void *data,
  248. struct sk_buff *skb)
  249. {
  250. struct hci_ev_status *rp = data;
  251. void *sent;
  252. bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
  253. sent = hci_sent_cmd_data(hdev, HCI_OP_WRITE_LOCAL_NAME);
  254. if (!sent)
  255. return rp->status;
  256. hci_dev_lock(hdev);
  257. if (hci_dev_test_flag(hdev, HCI_MGMT))
  258. mgmt_set_local_name_complete(hdev, sent, rp->status);
  259. else if (!rp->status)
  260. memcpy(hdev->dev_name, sent, HCI_MAX_NAME_LENGTH);
  261. hci_dev_unlock(hdev);
  262. return rp->status;
  263. }
  264. static u8 hci_cc_read_local_name(struct hci_dev *hdev, void *data,
  265. struct sk_buff *skb)
  266. {
  267. struct hci_rp_read_local_name *rp = data;
  268. bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
  269. if (rp->status)
  270. return rp->status;
  271. if (hci_dev_test_flag(hdev, HCI_SETUP) ||
  272. hci_dev_test_flag(hdev, HCI_CONFIG))
  273. memcpy(hdev->dev_name, rp->name, HCI_MAX_NAME_LENGTH);
  274. return rp->status;
  275. }
  276. static u8 hci_cc_write_auth_enable(struct hci_dev *hdev, void *data,
  277. struct sk_buff *skb)
  278. {
  279. struct hci_ev_status *rp = data;
  280. void *sent;
  281. bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
  282. sent = hci_sent_cmd_data(hdev, HCI_OP_WRITE_AUTH_ENABLE);
  283. if (!sent)
  284. return rp->status;
  285. hci_dev_lock(hdev);
  286. if (!rp->status) {
  287. __u8 param = *((__u8 *) sent);
  288. if (param == AUTH_ENABLED)
  289. set_bit(HCI_AUTH, &hdev->flags);
  290. else
  291. clear_bit(HCI_AUTH, &hdev->flags);
  292. }
  293. if (hci_dev_test_flag(hdev, HCI_MGMT))
  294. mgmt_auth_enable_complete(hdev, rp->status);
  295. hci_dev_unlock(hdev);
  296. return rp->status;
  297. }
  298. static u8 hci_cc_write_encrypt_mode(struct hci_dev *hdev, void *data,
  299. struct sk_buff *skb)
  300. {
  301. struct hci_ev_status *rp = data;
  302. __u8 param;
  303. void *sent;
  304. bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
  305. if (rp->status)
  306. return rp->status;
  307. sent = hci_sent_cmd_data(hdev, HCI_OP_WRITE_ENCRYPT_MODE);
  308. if (!sent)
  309. return rp->status;
  310. param = *((__u8 *) sent);
  311. if (param)
  312. set_bit(HCI_ENCRYPT, &hdev->flags);
  313. else
  314. clear_bit(HCI_ENCRYPT, &hdev->flags);
  315. return rp->status;
  316. }
  317. static u8 hci_cc_write_scan_enable(struct hci_dev *hdev, void *data,
  318. struct sk_buff *skb)
  319. {
  320. struct hci_ev_status *rp = data;
  321. __u8 param;
  322. void *sent;
  323. bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
  324. sent = hci_sent_cmd_data(hdev, HCI_OP_WRITE_SCAN_ENABLE);
  325. if (!sent)
  326. return rp->status;
  327. param = *((__u8 *) sent);
  328. hci_dev_lock(hdev);
  329. if (rp->status) {
  330. hdev->discov_timeout = 0;
  331. goto done;
  332. }
  333. if (param & SCAN_INQUIRY)
  334. set_bit(HCI_ISCAN, &hdev->flags);
  335. else
  336. clear_bit(HCI_ISCAN, &hdev->flags);
  337. if (param & SCAN_PAGE)
  338. set_bit(HCI_PSCAN, &hdev->flags);
  339. else
  340. clear_bit(HCI_PSCAN, &hdev->flags);
  341. done:
  342. hci_dev_unlock(hdev);
  343. return rp->status;
  344. }
  345. static u8 hci_cc_set_event_filter(struct hci_dev *hdev, void *data,
  346. struct sk_buff *skb)
  347. {
  348. struct hci_ev_status *rp = data;
  349. struct hci_cp_set_event_filter *cp;
  350. void *sent;
  351. bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
  352. if (rp->status)
  353. return rp->status;
  354. sent = hci_sent_cmd_data(hdev, HCI_OP_SET_EVENT_FLT);
  355. if (!sent)
  356. return rp->status;
  357. cp = (struct hci_cp_set_event_filter *)sent;
  358. if (cp->flt_type == HCI_FLT_CLEAR_ALL)
  359. hci_dev_clear_flag(hdev, HCI_EVENT_FILTER_CONFIGURED);
  360. else
  361. hci_dev_set_flag(hdev, HCI_EVENT_FILTER_CONFIGURED);
  362. return rp->status;
  363. }
  364. static u8 hci_cc_read_class_of_dev(struct hci_dev *hdev, void *data,
  365. struct sk_buff *skb)
  366. {
  367. struct hci_rp_read_class_of_dev *rp = data;
  368. if (WARN_ON(!hdev))
  369. return HCI_ERROR_UNSPECIFIED;
  370. bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
  371. if (rp->status)
  372. return rp->status;
  373. memcpy(hdev->dev_class, rp->dev_class, 3);
  374. bt_dev_dbg(hdev, "class 0x%.2x%.2x%.2x", hdev->dev_class[2],
  375. hdev->dev_class[1], hdev->dev_class[0]);
  376. return rp->status;
  377. }
  378. static u8 hci_cc_write_class_of_dev(struct hci_dev *hdev, void *data,
  379. struct sk_buff *skb)
  380. {
  381. struct hci_ev_status *rp = data;
  382. void *sent;
  383. bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
  384. sent = hci_sent_cmd_data(hdev, HCI_OP_WRITE_CLASS_OF_DEV);
  385. if (!sent)
  386. return rp->status;
  387. hci_dev_lock(hdev);
  388. if (!rp->status)
  389. memcpy(hdev->dev_class, sent, 3);
  390. if (hci_dev_test_flag(hdev, HCI_MGMT))
  391. mgmt_set_class_of_dev_complete(hdev, sent, rp->status);
  392. hci_dev_unlock(hdev);
  393. return rp->status;
  394. }
  395. static u8 hci_cc_read_voice_setting(struct hci_dev *hdev, void *data,
  396. struct sk_buff *skb)
  397. {
  398. struct hci_rp_read_voice_setting *rp = data;
  399. __u16 setting;
  400. bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
  401. if (rp->status)
  402. return rp->status;
  403. setting = __le16_to_cpu(rp->voice_setting);
  404. if (hdev->voice_setting == setting)
  405. return rp->status;
  406. hdev->voice_setting = setting;
  407. bt_dev_dbg(hdev, "voice setting 0x%4.4x", setting);
  408. if (hdev->notify)
  409. hdev->notify(hdev, HCI_NOTIFY_VOICE_SETTING);
  410. return rp->status;
  411. }
  412. static u8 hci_cc_write_voice_setting(struct hci_dev *hdev, void *data,
  413. struct sk_buff *skb)
  414. {
  415. struct hci_ev_status *rp = data;
  416. __u16 setting;
  417. void *sent;
  418. bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
  419. if (rp->status)
  420. return rp->status;
  421. sent = hci_sent_cmd_data(hdev, HCI_OP_WRITE_VOICE_SETTING);
  422. if (!sent)
  423. return rp->status;
  424. setting = get_unaligned_le16(sent);
  425. if (hdev->voice_setting == setting)
  426. return rp->status;
  427. hdev->voice_setting = setting;
  428. bt_dev_dbg(hdev, "voice setting 0x%4.4x", setting);
  429. if (hdev->notify)
  430. hdev->notify(hdev, HCI_NOTIFY_VOICE_SETTING);
  431. return rp->status;
  432. }
  433. static u8 hci_cc_read_num_supported_iac(struct hci_dev *hdev, void *data,
  434. struct sk_buff *skb)
  435. {
  436. struct hci_rp_read_num_supported_iac *rp = data;
  437. bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
  438. if (rp->status)
  439. return rp->status;
  440. hdev->num_iac = rp->num_iac;
  441. bt_dev_dbg(hdev, "num iac %d", hdev->num_iac);
  442. return rp->status;
  443. }
  444. static u8 hci_cc_write_ssp_mode(struct hci_dev *hdev, void *data,
  445. struct sk_buff *skb)
  446. {
  447. struct hci_ev_status *rp = data;
  448. struct hci_cp_write_ssp_mode *sent;
  449. bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
  450. sent = hci_sent_cmd_data(hdev, HCI_OP_WRITE_SSP_MODE);
  451. if (!sent)
  452. return rp->status;
  453. hci_dev_lock(hdev);
  454. if (!rp->status) {
  455. if (sent->mode)
  456. hdev->features[1][0] |= LMP_HOST_SSP;
  457. else
  458. hdev->features[1][0] &= ~LMP_HOST_SSP;
  459. }
  460. if (!rp->status) {
  461. if (sent->mode)
  462. hci_dev_set_flag(hdev, HCI_SSP_ENABLED);
  463. else
  464. hci_dev_clear_flag(hdev, HCI_SSP_ENABLED);
  465. }
  466. hci_dev_unlock(hdev);
  467. return rp->status;
  468. }
  469. static u8 hci_cc_write_sc_support(struct hci_dev *hdev, void *data,
  470. struct sk_buff *skb)
  471. {
  472. struct hci_ev_status *rp = data;
  473. struct hci_cp_write_sc_support *sent;
  474. bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
  475. sent = hci_sent_cmd_data(hdev, HCI_OP_WRITE_SC_SUPPORT);
  476. if (!sent)
  477. return rp->status;
  478. hci_dev_lock(hdev);
  479. if (!rp->status) {
  480. if (sent->support)
  481. hdev->features[1][0] |= LMP_HOST_SC;
  482. else
  483. hdev->features[1][0] &= ~LMP_HOST_SC;
  484. }
  485. if (!hci_dev_test_flag(hdev, HCI_MGMT) && !rp->status) {
  486. if (sent->support)
  487. hci_dev_set_flag(hdev, HCI_SC_ENABLED);
  488. else
  489. hci_dev_clear_flag(hdev, HCI_SC_ENABLED);
  490. }
  491. hci_dev_unlock(hdev);
  492. return rp->status;
  493. }
  494. static u8 hci_cc_read_local_version(struct hci_dev *hdev, void *data,
  495. struct sk_buff *skb)
  496. {
  497. struct hci_rp_read_local_version *rp = data;
  498. bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
  499. if (rp->status)
  500. return rp->status;
  501. if (hci_dev_test_flag(hdev, HCI_SETUP) ||
  502. hci_dev_test_flag(hdev, HCI_CONFIG)) {
  503. hdev->hci_ver = rp->hci_ver;
  504. hdev->hci_rev = __le16_to_cpu(rp->hci_rev);
  505. hdev->lmp_ver = rp->lmp_ver;
  506. hdev->manufacturer = __le16_to_cpu(rp->manufacturer);
  507. hdev->lmp_subver = __le16_to_cpu(rp->lmp_subver);
  508. }
  509. return rp->status;
  510. }
  511. static u8 hci_cc_read_enc_key_size(struct hci_dev *hdev, void *data,
  512. struct sk_buff *skb)
  513. {
  514. struct hci_rp_read_enc_key_size *rp = data;
  515. struct hci_conn *conn;
  516. u16 handle;
  517. u8 status = rp->status;
  518. bt_dev_dbg(hdev, "status 0x%2.2x", status);
  519. handle = le16_to_cpu(rp->handle);
  520. hci_dev_lock(hdev);
  521. conn = hci_conn_hash_lookup_handle(hdev, handle);
  522. if (!conn) {
  523. status = 0xFF;
  524. goto done;
  525. }
  526. /* While unexpected, the read_enc_key_size command may fail. The most
  527. * secure approach is to then assume the key size is 0 to force a
  528. * disconnection.
  529. */
  530. if (status) {
  531. bt_dev_err(hdev, "failed to read key size for handle %u",
  532. handle);
  533. conn->enc_key_size = 0;
  534. } else {
  535. conn->enc_key_size = rp->key_size;
  536. status = 0;
  537. if (conn->enc_key_size < hdev->min_enc_key_size) {
  538. /* As slave role, the conn->state has been set to
  539. * BT_CONNECTED and l2cap conn req might not be received
  540. * yet, at this moment the l2cap layer almost does
  541. * nothing with the non-zero status.
  542. * So we also clear encrypt related bits, and then the
  543. * handler of l2cap conn req will get the right secure
  544. * state at a later time.
  545. */
  546. status = HCI_ERROR_AUTH_FAILURE;
  547. clear_bit(HCI_CONN_ENCRYPT, &conn->flags);
  548. clear_bit(HCI_CONN_AES_CCM, &conn->flags);
  549. }
  550. }
  551. hci_encrypt_cfm(conn, status);
  552. done:
  553. hci_dev_unlock(hdev);
  554. return status;
  555. }
  556. static u8 hci_cc_read_local_commands(struct hci_dev *hdev, void *data,
  557. struct sk_buff *skb)
  558. {
  559. struct hci_rp_read_local_commands *rp = data;
  560. bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
  561. if (rp->status)
  562. return rp->status;
  563. if (hci_dev_test_flag(hdev, HCI_SETUP) ||
  564. hci_dev_test_flag(hdev, HCI_CONFIG))
  565. memcpy(hdev->commands, rp->commands, sizeof(hdev->commands));
  566. return rp->status;
  567. }
  568. static u8 hci_cc_read_auth_payload_timeout(struct hci_dev *hdev, void *data,
  569. struct sk_buff *skb)
  570. {
  571. struct hci_rp_read_auth_payload_to *rp = data;
  572. struct hci_conn *conn;
  573. bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
  574. if (rp->status)
  575. return rp->status;
  576. hci_dev_lock(hdev);
  577. conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(rp->handle));
  578. if (conn)
  579. conn->auth_payload_timeout = __le16_to_cpu(rp->timeout);
  580. hci_dev_unlock(hdev);
  581. return rp->status;
  582. }
  583. static u8 hci_cc_write_auth_payload_timeout(struct hci_dev *hdev, void *data,
  584. struct sk_buff *skb)
  585. {
  586. struct hci_rp_write_auth_payload_to *rp = data;
  587. struct hci_conn *conn;
  588. void *sent;
  589. bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
  590. sent = hci_sent_cmd_data(hdev, HCI_OP_WRITE_AUTH_PAYLOAD_TO);
  591. if (!sent)
  592. return rp->status;
  593. hci_dev_lock(hdev);
  594. conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(rp->handle));
  595. if (!conn) {
  596. rp->status = 0xff;
  597. goto unlock;
  598. }
  599. if (!rp->status)
  600. conn->auth_payload_timeout = get_unaligned_le16(sent + 2);
  601. unlock:
  602. hci_dev_unlock(hdev);
  603. return rp->status;
  604. }
  605. static u8 hci_cc_read_local_features(struct hci_dev *hdev, void *data,
  606. struct sk_buff *skb)
  607. {
  608. struct hci_rp_read_local_features *rp = data;
  609. bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
  610. if (rp->status)
  611. return rp->status;
  612. memcpy(hdev->features, rp->features, 8);
  613. /* Adjust default settings according to features
  614. * supported by device. */
  615. if (hdev->features[0][0] & LMP_3SLOT)
  616. hdev->pkt_type |= (HCI_DM3 | HCI_DH3);
  617. if (hdev->features[0][0] & LMP_5SLOT)
  618. hdev->pkt_type |= (HCI_DM5 | HCI_DH5);
  619. if (hdev->features[0][1] & LMP_HV2) {
  620. hdev->pkt_type |= (HCI_HV2);
  621. hdev->esco_type |= (ESCO_HV2);
  622. }
  623. if (hdev->features[0][1] & LMP_HV3) {
  624. hdev->pkt_type |= (HCI_HV3);
  625. hdev->esco_type |= (ESCO_HV3);
  626. }
  627. if (lmp_esco_capable(hdev))
  628. hdev->esco_type |= (ESCO_EV3);
  629. if (hdev->features[0][4] & LMP_EV4)
  630. hdev->esco_type |= (ESCO_EV4);
  631. if (hdev->features[0][4] & LMP_EV5)
  632. hdev->esco_type |= (ESCO_EV5);
  633. if (hdev->features[0][5] & LMP_EDR_ESCO_2M)
  634. hdev->esco_type |= (ESCO_2EV3);
  635. if (hdev->features[0][5] & LMP_EDR_ESCO_3M)
  636. hdev->esco_type |= (ESCO_3EV3);
  637. if (hdev->features[0][5] & LMP_EDR_3S_ESCO)
  638. hdev->esco_type |= (ESCO_2EV5 | ESCO_3EV5);
  639. return rp->status;
  640. }
  641. static u8 hci_cc_read_local_ext_features(struct hci_dev *hdev, void *data,
  642. struct sk_buff *skb)
  643. {
  644. struct hci_rp_read_local_ext_features *rp = data;
  645. bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
  646. if (rp->status)
  647. return rp->status;
  648. if (hdev->max_page < rp->max_page) {
  649. if (test_bit(HCI_QUIRK_BROKEN_LOCAL_EXT_FEATURES_PAGE_2,
  650. &hdev->quirks))
  651. bt_dev_warn(hdev, "broken local ext features page 2");
  652. else
  653. hdev->max_page = rp->max_page;
  654. }
  655. if (rp->page < HCI_MAX_PAGES)
  656. memcpy(hdev->features[rp->page], rp->features, 8);
  657. return rp->status;
  658. }
  659. static u8 hci_cc_read_buffer_size(struct hci_dev *hdev, void *data,
  660. struct sk_buff *skb)
  661. {
  662. struct hci_rp_read_buffer_size *rp = data;
  663. bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
  664. if (rp->status)
  665. return rp->status;
  666. hdev->acl_mtu = __le16_to_cpu(rp->acl_mtu);
  667. hdev->sco_mtu = rp->sco_mtu;
  668. hdev->acl_pkts = __le16_to_cpu(rp->acl_max_pkt);
  669. hdev->sco_pkts = __le16_to_cpu(rp->sco_max_pkt);
  670. if (test_bit(HCI_QUIRK_FIXUP_BUFFER_SIZE, &hdev->quirks)) {
  671. hdev->sco_mtu = 64;
  672. hdev->sco_pkts = 8;
  673. }
  674. if (!read_voice_setting_capable(hdev))
  675. hdev->sco_pkts = 0;
  676. hdev->acl_cnt = hdev->acl_pkts;
  677. hdev->sco_cnt = hdev->sco_pkts;
  678. BT_DBG("%s acl mtu %d:%d sco mtu %d:%d", hdev->name, hdev->acl_mtu,
  679. hdev->acl_pkts, hdev->sco_mtu, hdev->sco_pkts);
  680. if (!hdev->acl_mtu || !hdev->acl_pkts)
  681. return HCI_ERROR_INVALID_PARAMETERS;
  682. return rp->status;
  683. }
  684. static u8 hci_cc_read_bd_addr(struct hci_dev *hdev, void *data,
  685. struct sk_buff *skb)
  686. {
  687. struct hci_rp_read_bd_addr *rp = data;
  688. bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
  689. if (rp->status)
  690. return rp->status;
  691. if (test_bit(HCI_INIT, &hdev->flags))
  692. bacpy(&hdev->bdaddr, &rp->bdaddr);
  693. if (hci_dev_test_flag(hdev, HCI_SETUP))
  694. bacpy(&hdev->setup_addr, &rp->bdaddr);
  695. return rp->status;
  696. }
  697. static u8 hci_cc_read_local_pairing_opts(struct hci_dev *hdev, void *data,
  698. struct sk_buff *skb)
  699. {
  700. struct hci_rp_read_local_pairing_opts *rp = data;
  701. bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
  702. if (rp->status)
  703. return rp->status;
  704. if (hci_dev_test_flag(hdev, HCI_SETUP) ||
  705. hci_dev_test_flag(hdev, HCI_CONFIG)) {
  706. hdev->pairing_opts = rp->pairing_opts;
  707. hdev->max_enc_key_size = rp->max_key_size;
  708. }
  709. return rp->status;
  710. }
  711. static u8 hci_cc_read_page_scan_activity(struct hci_dev *hdev, void *data,
  712. struct sk_buff *skb)
  713. {
  714. struct hci_rp_read_page_scan_activity *rp = data;
  715. bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
  716. if (rp->status)
  717. return rp->status;
  718. if (test_bit(HCI_INIT, &hdev->flags)) {
  719. hdev->page_scan_interval = __le16_to_cpu(rp->interval);
  720. hdev->page_scan_window = __le16_to_cpu(rp->window);
  721. }
  722. return rp->status;
  723. }
  724. static u8 hci_cc_write_page_scan_activity(struct hci_dev *hdev, void *data,
  725. struct sk_buff *skb)
  726. {
  727. struct hci_ev_status *rp = data;
  728. struct hci_cp_write_page_scan_activity *sent;
  729. bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
  730. if (rp->status)
  731. return rp->status;
  732. sent = hci_sent_cmd_data(hdev, HCI_OP_WRITE_PAGE_SCAN_ACTIVITY);
  733. if (!sent)
  734. return rp->status;
  735. hdev->page_scan_interval = __le16_to_cpu(sent->interval);
  736. hdev->page_scan_window = __le16_to_cpu(sent->window);
  737. return rp->status;
  738. }
  739. static u8 hci_cc_read_page_scan_type(struct hci_dev *hdev, void *data,
  740. struct sk_buff *skb)
  741. {
  742. struct hci_rp_read_page_scan_type *rp = data;
  743. bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
  744. if (rp->status)
  745. return rp->status;
  746. if (test_bit(HCI_INIT, &hdev->flags))
  747. hdev->page_scan_type = rp->type;
  748. return rp->status;
  749. }
  750. static u8 hci_cc_write_page_scan_type(struct hci_dev *hdev, void *data,
  751. struct sk_buff *skb)
  752. {
  753. struct hci_ev_status *rp = data;
  754. u8 *type;
  755. bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
  756. if (rp->status)
  757. return rp->status;
  758. type = hci_sent_cmd_data(hdev, HCI_OP_WRITE_PAGE_SCAN_TYPE);
  759. if (type)
  760. hdev->page_scan_type = *type;
  761. return rp->status;
  762. }
  763. static u8 hci_cc_read_clock(struct hci_dev *hdev, void *data,
  764. struct sk_buff *skb)
  765. {
  766. struct hci_rp_read_clock *rp = data;
  767. struct hci_cp_read_clock *cp;
  768. struct hci_conn *conn;
  769. bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
  770. if (rp->status)
  771. return rp->status;
  772. hci_dev_lock(hdev);
  773. cp = hci_sent_cmd_data(hdev, HCI_OP_READ_CLOCK);
  774. if (!cp)
  775. goto unlock;
  776. if (cp->which == 0x00) {
  777. hdev->clock = le32_to_cpu(rp->clock);
  778. goto unlock;
  779. }
  780. conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(rp->handle));
  781. if (conn) {
  782. conn->clock = le32_to_cpu(rp->clock);
  783. conn->clock_accuracy = le16_to_cpu(rp->accuracy);
  784. }
  785. unlock:
  786. hci_dev_unlock(hdev);
  787. return rp->status;
  788. }
  789. static u8 hci_cc_read_inq_rsp_tx_power(struct hci_dev *hdev, void *data,
  790. struct sk_buff *skb)
  791. {
  792. struct hci_rp_read_inq_rsp_tx_power *rp = data;
  793. bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
  794. if (rp->status)
  795. return rp->status;
  796. hdev->inq_tx_power = rp->tx_power;
  797. return rp->status;
  798. }
  799. static u8 hci_cc_read_def_err_data_reporting(struct hci_dev *hdev, void *data,
  800. struct sk_buff *skb)
  801. {
  802. struct hci_rp_read_def_err_data_reporting *rp = data;
  803. bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
  804. if (rp->status)
  805. return rp->status;
  806. hdev->err_data_reporting = rp->err_data_reporting;
  807. return rp->status;
  808. }
  809. static u8 hci_cc_write_def_err_data_reporting(struct hci_dev *hdev, void *data,
  810. struct sk_buff *skb)
  811. {
  812. struct hci_ev_status *rp = data;
  813. struct hci_cp_write_def_err_data_reporting *cp;
  814. bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
  815. if (rp->status)
  816. return rp->status;
  817. cp = hci_sent_cmd_data(hdev, HCI_OP_WRITE_DEF_ERR_DATA_REPORTING);
  818. if (!cp)
  819. return rp->status;
  820. hdev->err_data_reporting = cp->err_data_reporting;
  821. return rp->status;
  822. }
  823. static u8 hci_cc_pin_code_reply(struct hci_dev *hdev, void *data,
  824. struct sk_buff *skb)
  825. {
  826. struct hci_rp_pin_code_reply *rp = data;
  827. struct hci_cp_pin_code_reply *cp;
  828. struct hci_conn *conn;
  829. bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
  830. hci_dev_lock(hdev);
  831. if (hci_dev_test_flag(hdev, HCI_MGMT))
  832. mgmt_pin_code_reply_complete(hdev, &rp->bdaddr, rp->status);
  833. if (rp->status)
  834. goto unlock;
  835. cp = hci_sent_cmd_data(hdev, HCI_OP_PIN_CODE_REPLY);
  836. if (!cp)
  837. goto unlock;
  838. conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &cp->bdaddr);
  839. if (conn)
  840. conn->pin_length = cp->pin_len;
  841. unlock:
  842. hci_dev_unlock(hdev);
  843. return rp->status;
  844. }
  845. static u8 hci_cc_pin_code_neg_reply(struct hci_dev *hdev, void *data,
  846. struct sk_buff *skb)
  847. {
  848. struct hci_rp_pin_code_neg_reply *rp = data;
  849. bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
  850. hci_dev_lock(hdev);
  851. if (hci_dev_test_flag(hdev, HCI_MGMT))
  852. mgmt_pin_code_neg_reply_complete(hdev, &rp->bdaddr,
  853. rp->status);
  854. hci_dev_unlock(hdev);
  855. return rp->status;
  856. }
  857. static u8 hci_cc_le_read_buffer_size(struct hci_dev *hdev, void *data,
  858. struct sk_buff *skb)
  859. {
  860. struct hci_rp_le_read_buffer_size *rp = data;
  861. bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
  862. if (rp->status)
  863. return rp->status;
  864. hdev->le_mtu = __le16_to_cpu(rp->le_mtu);
  865. hdev->le_pkts = rp->le_max_pkt;
  866. hdev->le_cnt = hdev->le_pkts;
  867. BT_DBG("%s le mtu %d:%d", hdev->name, hdev->le_mtu, hdev->le_pkts);
  868. if (hdev->le_mtu && hdev->le_mtu < HCI_MIN_LE_MTU)
  869. return HCI_ERROR_INVALID_PARAMETERS;
  870. return rp->status;
  871. }
  872. static u8 hci_cc_le_read_local_features(struct hci_dev *hdev, void *data,
  873. struct sk_buff *skb)
  874. {
  875. struct hci_rp_le_read_local_features *rp = data;
  876. BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
  877. if (rp->status)
  878. return rp->status;
  879. memcpy(hdev->le_features, rp->features, 8);
  880. return rp->status;
  881. }
  882. static u8 hci_cc_le_read_adv_tx_power(struct hci_dev *hdev, void *data,
  883. struct sk_buff *skb)
  884. {
  885. struct hci_rp_le_read_adv_tx_power *rp = data;
  886. bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
  887. if (rp->status)
  888. return rp->status;
  889. hdev->adv_tx_power = rp->tx_power;
  890. return rp->status;
  891. }
  892. static u8 hci_cc_user_confirm_reply(struct hci_dev *hdev, void *data,
  893. struct sk_buff *skb)
  894. {
  895. struct hci_rp_user_confirm_reply *rp = data;
  896. bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
  897. hci_dev_lock(hdev);
  898. if (hci_dev_test_flag(hdev, HCI_MGMT))
  899. mgmt_user_confirm_reply_complete(hdev, &rp->bdaddr, ACL_LINK, 0,
  900. rp->status);
  901. hci_dev_unlock(hdev);
  902. return rp->status;
  903. }
  904. static u8 hci_cc_user_confirm_neg_reply(struct hci_dev *hdev, void *data,
  905. struct sk_buff *skb)
  906. {
  907. struct hci_rp_user_confirm_reply *rp = data;
  908. bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
  909. hci_dev_lock(hdev);
  910. if (hci_dev_test_flag(hdev, HCI_MGMT))
  911. mgmt_user_confirm_neg_reply_complete(hdev, &rp->bdaddr,
  912. ACL_LINK, 0, rp->status);
  913. hci_dev_unlock(hdev);
  914. return rp->status;
  915. }
  916. static u8 hci_cc_user_passkey_reply(struct hci_dev *hdev, void *data,
  917. struct sk_buff *skb)
  918. {
  919. struct hci_rp_user_confirm_reply *rp = data;
  920. bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
  921. hci_dev_lock(hdev);
  922. if (hci_dev_test_flag(hdev, HCI_MGMT))
  923. mgmt_user_passkey_reply_complete(hdev, &rp->bdaddr, ACL_LINK,
  924. 0, rp->status);
  925. hci_dev_unlock(hdev);
  926. return rp->status;
  927. }
  928. static u8 hci_cc_user_passkey_neg_reply(struct hci_dev *hdev, void *data,
  929. struct sk_buff *skb)
  930. {
  931. struct hci_rp_user_confirm_reply *rp = data;
  932. bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
  933. hci_dev_lock(hdev);
  934. if (hci_dev_test_flag(hdev, HCI_MGMT))
  935. mgmt_user_passkey_neg_reply_complete(hdev, &rp->bdaddr,
  936. ACL_LINK, 0, rp->status);
  937. hci_dev_unlock(hdev);
  938. return rp->status;
  939. }
  940. static u8 hci_cc_read_local_oob_data(struct hci_dev *hdev, void *data,
  941. struct sk_buff *skb)
  942. {
  943. struct hci_rp_read_local_oob_data *rp = data;
  944. bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
  945. return rp->status;
  946. }
  947. static u8 hci_cc_read_local_oob_ext_data(struct hci_dev *hdev, void *data,
  948. struct sk_buff *skb)
  949. {
  950. struct hci_rp_read_local_oob_ext_data *rp = data;
  951. bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
  952. return rp->status;
  953. }
  954. static u8 hci_cc_le_set_random_addr(struct hci_dev *hdev, void *data,
  955. struct sk_buff *skb)
  956. {
  957. struct hci_ev_status *rp = data;
  958. bdaddr_t *sent;
  959. bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
  960. if (rp->status)
  961. return rp->status;
  962. sent = hci_sent_cmd_data(hdev, HCI_OP_LE_SET_RANDOM_ADDR);
  963. if (!sent)
  964. return rp->status;
  965. hci_dev_lock(hdev);
  966. bacpy(&hdev->random_addr, sent);
  967. if (!bacmp(&hdev->rpa, sent)) {
  968. hci_dev_clear_flag(hdev, HCI_RPA_EXPIRED);
  969. queue_delayed_work(hdev->workqueue, &hdev->rpa_expired,
  970. secs_to_jiffies(hdev->rpa_timeout));
  971. }
  972. hci_dev_unlock(hdev);
  973. return rp->status;
  974. }
  975. static u8 hci_cc_le_set_default_phy(struct hci_dev *hdev, void *data,
  976. struct sk_buff *skb)
  977. {
  978. struct hci_ev_status *rp = data;
  979. struct hci_cp_le_set_default_phy *cp;
  980. bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
  981. if (rp->status)
  982. return rp->status;
  983. cp = hci_sent_cmd_data(hdev, HCI_OP_LE_SET_DEFAULT_PHY);
  984. if (!cp)
  985. return rp->status;
  986. hci_dev_lock(hdev);
  987. hdev->le_tx_def_phys = cp->tx_phys;
  988. hdev->le_rx_def_phys = cp->rx_phys;
  989. hci_dev_unlock(hdev);
  990. return rp->status;
  991. }
  992. static u8 hci_cc_le_set_adv_set_random_addr(struct hci_dev *hdev, void *data,
  993. struct sk_buff *skb)
  994. {
  995. struct hci_ev_status *rp = data;
  996. struct hci_cp_le_set_adv_set_rand_addr *cp;
  997. struct adv_info *adv;
  998. bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
  999. if (rp->status)
  1000. return rp->status;
  1001. cp = hci_sent_cmd_data(hdev, HCI_OP_LE_SET_ADV_SET_RAND_ADDR);
  1002. /* Update only in case the adv instance since handle 0x00 shall be using
  1003. * HCI_OP_LE_SET_RANDOM_ADDR since that allows both extended and
  1004. * non-extended adverting.
  1005. */
  1006. if (!cp || !cp->handle)
  1007. return rp->status;
  1008. hci_dev_lock(hdev);
  1009. adv = hci_find_adv_instance(hdev, cp->handle);
  1010. if (adv) {
  1011. bacpy(&adv->random_addr, &cp->bdaddr);
  1012. if (!bacmp(&hdev->rpa, &cp->bdaddr)) {
  1013. adv->rpa_expired = false;
  1014. queue_delayed_work(hdev->workqueue,
  1015. &adv->rpa_expired_cb,
  1016. secs_to_jiffies(hdev->rpa_timeout));
  1017. }
  1018. }
  1019. hci_dev_unlock(hdev);
  1020. return rp->status;
  1021. }
  1022. static u8 hci_cc_le_remove_adv_set(struct hci_dev *hdev, void *data,
  1023. struct sk_buff *skb)
  1024. {
  1025. struct hci_ev_status *rp = data;
  1026. u8 *instance;
  1027. int err;
  1028. bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
  1029. if (rp->status)
  1030. return rp->status;
  1031. instance = hci_sent_cmd_data(hdev, HCI_OP_LE_REMOVE_ADV_SET);
  1032. if (!instance)
  1033. return rp->status;
  1034. hci_dev_lock(hdev);
  1035. err = hci_remove_adv_instance(hdev, *instance);
  1036. if (!err)
  1037. mgmt_advertising_removed(hci_skb_sk(hdev->sent_cmd), hdev,
  1038. *instance);
  1039. hci_dev_unlock(hdev);
  1040. return rp->status;
  1041. }
  1042. static u8 hci_cc_le_clear_adv_sets(struct hci_dev *hdev, void *data,
  1043. struct sk_buff *skb)
  1044. {
  1045. struct hci_ev_status *rp = data;
  1046. struct adv_info *adv, *n;
  1047. int err;
  1048. bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
  1049. if (rp->status)
  1050. return rp->status;
  1051. if (!hci_sent_cmd_data(hdev, HCI_OP_LE_CLEAR_ADV_SETS))
  1052. return rp->status;
  1053. hci_dev_lock(hdev);
  1054. list_for_each_entry_safe(adv, n, &hdev->adv_instances, list) {
  1055. u8 instance = adv->instance;
  1056. err = hci_remove_adv_instance(hdev, instance);
  1057. if (!err)
  1058. mgmt_advertising_removed(hci_skb_sk(hdev->sent_cmd),
  1059. hdev, instance);
  1060. }
  1061. hci_dev_unlock(hdev);
  1062. return rp->status;
  1063. }
  1064. static u8 hci_cc_le_read_transmit_power(struct hci_dev *hdev, void *data,
  1065. struct sk_buff *skb)
  1066. {
  1067. struct hci_rp_le_read_transmit_power *rp = data;
  1068. bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
  1069. if (rp->status)
  1070. return rp->status;
  1071. hdev->min_le_tx_power = rp->min_le_tx_power;
  1072. hdev->max_le_tx_power = rp->max_le_tx_power;
  1073. return rp->status;
  1074. }
  1075. static u8 hci_cc_le_set_privacy_mode(struct hci_dev *hdev, void *data,
  1076. struct sk_buff *skb)
  1077. {
  1078. struct hci_ev_status *rp = data;
  1079. struct hci_cp_le_set_privacy_mode *cp;
  1080. struct hci_conn_params *params;
  1081. bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
  1082. if (rp->status)
  1083. return rp->status;
  1084. cp = hci_sent_cmd_data(hdev, HCI_OP_LE_SET_PRIVACY_MODE);
  1085. if (!cp)
  1086. return rp->status;
  1087. hci_dev_lock(hdev);
  1088. params = hci_conn_params_lookup(hdev, &cp->bdaddr, cp->bdaddr_type);
  1089. if (params)
  1090. WRITE_ONCE(params->privacy_mode, cp->mode);
  1091. hci_dev_unlock(hdev);
  1092. return rp->status;
  1093. }
  1094. static u8 hci_cc_le_set_adv_enable(struct hci_dev *hdev, void *data,
  1095. struct sk_buff *skb)
  1096. {
  1097. struct hci_ev_status *rp = data;
  1098. __u8 *sent;
  1099. bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
  1100. if (rp->status)
  1101. return rp->status;
  1102. sent = hci_sent_cmd_data(hdev, HCI_OP_LE_SET_ADV_ENABLE);
  1103. if (!sent)
  1104. return rp->status;
  1105. hci_dev_lock(hdev);
  1106. /* If we're doing connection initiation as peripheral. Set a
  1107. * timeout in case something goes wrong.
  1108. */
  1109. if (*sent) {
  1110. struct hci_conn *conn;
  1111. hci_dev_set_flag(hdev, HCI_LE_ADV);
  1112. conn = hci_lookup_le_connect(hdev);
  1113. if (conn)
  1114. queue_delayed_work(hdev->workqueue,
  1115. &conn->le_conn_timeout,
  1116. conn->conn_timeout);
  1117. } else {
  1118. hci_dev_clear_flag(hdev, HCI_LE_ADV);
  1119. }
  1120. hci_dev_unlock(hdev);
  1121. return rp->status;
  1122. }
  1123. static u8 hci_cc_le_set_ext_adv_enable(struct hci_dev *hdev, void *data,
  1124. struct sk_buff *skb)
  1125. {
  1126. struct hci_cp_le_set_ext_adv_enable *cp;
  1127. struct hci_cp_ext_adv_set *set;
  1128. struct adv_info *adv = NULL, *n;
  1129. struct hci_ev_status *rp = data;
  1130. bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
  1131. if (rp->status)
  1132. return rp->status;
  1133. cp = hci_sent_cmd_data(hdev, HCI_OP_LE_SET_EXT_ADV_ENABLE);
  1134. if (!cp)
  1135. return rp->status;
  1136. set = (void *)cp->data;
  1137. hci_dev_lock(hdev);
  1138. if (cp->num_of_sets)
  1139. adv = hci_find_adv_instance(hdev, set->handle);
  1140. if (cp->enable) {
  1141. struct hci_conn *conn;
  1142. hci_dev_set_flag(hdev, HCI_LE_ADV);
  1143. if (adv && !adv->periodic)
  1144. adv->enabled = true;
  1145. conn = hci_lookup_le_connect(hdev);
  1146. if (conn)
  1147. queue_delayed_work(hdev->workqueue,
  1148. &conn->le_conn_timeout,
  1149. conn->conn_timeout);
  1150. } else {
  1151. if (cp->num_of_sets) {
  1152. if (adv)
  1153. adv->enabled = false;
  1154. /* If just one instance was disabled check if there are
  1155. * any other instance enabled before clearing HCI_LE_ADV
  1156. */
  1157. list_for_each_entry_safe(adv, n, &hdev->adv_instances,
  1158. list) {
  1159. if (adv->enabled)
  1160. goto unlock;
  1161. }
  1162. } else {
  1163. /* All instances shall be considered disabled */
  1164. list_for_each_entry_safe(adv, n, &hdev->adv_instances,
  1165. list)
  1166. adv->enabled = false;
  1167. }
  1168. hci_dev_clear_flag(hdev, HCI_LE_ADV);
  1169. }
  1170. unlock:
  1171. hci_dev_unlock(hdev);
  1172. return rp->status;
  1173. }
  1174. static u8 hci_cc_le_set_scan_param(struct hci_dev *hdev, void *data,
  1175. struct sk_buff *skb)
  1176. {
  1177. struct hci_cp_le_set_scan_param *cp;
  1178. struct hci_ev_status *rp = data;
  1179. bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
  1180. if (rp->status)
  1181. return rp->status;
  1182. cp = hci_sent_cmd_data(hdev, HCI_OP_LE_SET_SCAN_PARAM);
  1183. if (!cp)
  1184. return rp->status;
  1185. hci_dev_lock(hdev);
  1186. hdev->le_scan_type = cp->type;
  1187. hci_dev_unlock(hdev);
  1188. return rp->status;
  1189. }
  1190. static u8 hci_cc_le_set_ext_scan_param(struct hci_dev *hdev, void *data,
  1191. struct sk_buff *skb)
  1192. {
  1193. struct hci_cp_le_set_ext_scan_params *cp;
  1194. struct hci_ev_status *rp = data;
  1195. struct hci_cp_le_scan_phy_params *phy_param;
  1196. bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
  1197. if (rp->status)
  1198. return rp->status;
  1199. cp = hci_sent_cmd_data(hdev, HCI_OP_LE_SET_EXT_SCAN_PARAMS);
  1200. if (!cp)
  1201. return rp->status;
  1202. phy_param = (void *)cp->data;
  1203. hci_dev_lock(hdev);
  1204. hdev->le_scan_type = phy_param->type;
  1205. hci_dev_unlock(hdev);
  1206. return rp->status;
  1207. }
  1208. static bool has_pending_adv_report(struct hci_dev *hdev)
  1209. {
  1210. struct discovery_state *d = &hdev->discovery;
  1211. return bacmp(&d->last_adv_addr, BDADDR_ANY);
  1212. }
  1213. static void clear_pending_adv_report(struct hci_dev *hdev)
  1214. {
  1215. struct discovery_state *d = &hdev->discovery;
  1216. bacpy(&d->last_adv_addr, BDADDR_ANY);
  1217. d->last_adv_data_len = 0;
  1218. }
  1219. static void store_pending_adv_report(struct hci_dev *hdev, bdaddr_t *bdaddr,
  1220. u8 bdaddr_type, s8 rssi, u32 flags,
  1221. u8 *data, u8 len)
  1222. {
  1223. struct discovery_state *d = &hdev->discovery;
  1224. if (len > max_adv_len(hdev))
  1225. return;
  1226. bacpy(&d->last_adv_addr, bdaddr);
  1227. d->last_adv_addr_type = bdaddr_type;
  1228. d->last_adv_rssi = rssi;
  1229. d->last_adv_flags = flags;
  1230. memcpy(d->last_adv_data, data, len);
  1231. d->last_adv_data_len = len;
  1232. }
  1233. static void le_set_scan_enable_complete(struct hci_dev *hdev, u8 enable)
  1234. {
  1235. hci_dev_lock(hdev);
  1236. switch (enable) {
  1237. case LE_SCAN_ENABLE:
  1238. hci_dev_set_flag(hdev, HCI_LE_SCAN);
  1239. if (hdev->le_scan_type == LE_SCAN_ACTIVE) {
  1240. clear_pending_adv_report(hdev);
  1241. hci_discovery_set_state(hdev, DISCOVERY_FINDING);
  1242. }
  1243. break;
  1244. case LE_SCAN_DISABLE:
  1245. /* We do this here instead of when setting DISCOVERY_STOPPED
  1246. * since the latter would potentially require waiting for
  1247. * inquiry to stop too.
  1248. */
  1249. if (has_pending_adv_report(hdev)) {
  1250. struct discovery_state *d = &hdev->discovery;
  1251. mgmt_device_found(hdev, &d->last_adv_addr, LE_LINK,
  1252. d->last_adv_addr_type, NULL,
  1253. d->last_adv_rssi, d->last_adv_flags,
  1254. d->last_adv_data,
  1255. d->last_adv_data_len, NULL, 0, 0);
  1256. }
  1257. /* Cancel this timer so that we don't try to disable scanning
  1258. * when it's already disabled.
  1259. */
  1260. cancel_delayed_work(&hdev->le_scan_disable);
  1261. hci_dev_clear_flag(hdev, HCI_LE_SCAN);
  1262. /* The HCI_LE_SCAN_INTERRUPTED flag indicates that we
  1263. * interrupted scanning due to a connect request. Mark
  1264. * therefore discovery as stopped.
  1265. */
  1266. if (hci_dev_test_and_clear_flag(hdev, HCI_LE_SCAN_INTERRUPTED))
  1267. hci_discovery_set_state(hdev, DISCOVERY_STOPPED);
  1268. else if (!hci_dev_test_flag(hdev, HCI_LE_ADV) &&
  1269. hdev->discovery.state == DISCOVERY_FINDING)
  1270. queue_work(hdev->workqueue, &hdev->reenable_adv_work);
  1271. break;
  1272. default:
  1273. bt_dev_err(hdev, "use of reserved LE_Scan_Enable param %d",
  1274. enable);
  1275. break;
  1276. }
  1277. hci_dev_unlock(hdev);
  1278. }
  1279. static u8 hci_cc_le_set_scan_enable(struct hci_dev *hdev, void *data,
  1280. struct sk_buff *skb)
  1281. {
  1282. struct hci_cp_le_set_scan_enable *cp;
  1283. struct hci_ev_status *rp = data;
  1284. bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
  1285. if (rp->status)
  1286. return rp->status;
  1287. cp = hci_sent_cmd_data(hdev, HCI_OP_LE_SET_SCAN_ENABLE);
  1288. if (!cp)
  1289. return rp->status;
  1290. le_set_scan_enable_complete(hdev, cp->enable);
  1291. return rp->status;
  1292. }
  1293. static u8 hci_cc_le_set_ext_scan_enable(struct hci_dev *hdev, void *data,
  1294. struct sk_buff *skb)
  1295. {
  1296. struct hci_cp_le_set_ext_scan_enable *cp;
  1297. struct hci_ev_status *rp = data;
  1298. bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
  1299. if (rp->status)
  1300. return rp->status;
  1301. cp = hci_sent_cmd_data(hdev, HCI_OP_LE_SET_EXT_SCAN_ENABLE);
  1302. if (!cp)
  1303. return rp->status;
  1304. le_set_scan_enable_complete(hdev, cp->enable);
  1305. return rp->status;
  1306. }
  1307. static u8 hci_cc_le_read_num_adv_sets(struct hci_dev *hdev, void *data,
  1308. struct sk_buff *skb)
  1309. {
  1310. struct hci_rp_le_read_num_supported_adv_sets *rp = data;
  1311. bt_dev_dbg(hdev, "status 0x%2.2x No of Adv sets %u", rp->status,
  1312. rp->num_of_sets);
  1313. if (rp->status)
  1314. return rp->status;
  1315. hdev->le_num_of_adv_sets = rp->num_of_sets;
  1316. return rp->status;
  1317. }
  1318. static u8 hci_cc_le_read_accept_list_size(struct hci_dev *hdev, void *data,
  1319. struct sk_buff *skb)
  1320. {
  1321. struct hci_rp_le_read_accept_list_size *rp = data;
  1322. bt_dev_dbg(hdev, "status 0x%2.2x size %u", rp->status, rp->size);
  1323. if (rp->status)
  1324. return rp->status;
  1325. hdev->le_accept_list_size = rp->size;
  1326. return rp->status;
  1327. }
  1328. static u8 hci_cc_le_clear_accept_list(struct hci_dev *hdev, void *data,
  1329. struct sk_buff *skb)
  1330. {
  1331. struct hci_ev_status *rp = data;
  1332. bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
  1333. if (rp->status)
  1334. return rp->status;
  1335. hci_dev_lock(hdev);
  1336. hci_bdaddr_list_clear(&hdev->le_accept_list);
  1337. hci_dev_unlock(hdev);
  1338. return rp->status;
  1339. }
  1340. static u8 hci_cc_le_add_to_accept_list(struct hci_dev *hdev, void *data,
  1341. struct sk_buff *skb)
  1342. {
  1343. struct hci_cp_le_add_to_accept_list *sent;
  1344. struct hci_ev_status *rp = data;
  1345. bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
  1346. if (rp->status)
  1347. return rp->status;
  1348. sent = hci_sent_cmd_data(hdev, HCI_OP_LE_ADD_TO_ACCEPT_LIST);
  1349. if (!sent)
  1350. return rp->status;
  1351. hci_dev_lock(hdev);
  1352. hci_bdaddr_list_add(&hdev->le_accept_list, &sent->bdaddr,
  1353. sent->bdaddr_type);
  1354. hci_dev_unlock(hdev);
  1355. return rp->status;
  1356. }
  1357. static u8 hci_cc_le_del_from_accept_list(struct hci_dev *hdev, void *data,
  1358. struct sk_buff *skb)
  1359. {
  1360. struct hci_cp_le_del_from_accept_list *sent;
  1361. struct hci_ev_status *rp = data;
  1362. bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
  1363. if (rp->status)
  1364. return rp->status;
  1365. sent = hci_sent_cmd_data(hdev, HCI_OP_LE_DEL_FROM_ACCEPT_LIST);
  1366. if (!sent)
  1367. return rp->status;
  1368. hci_dev_lock(hdev);
  1369. hci_bdaddr_list_del(&hdev->le_accept_list, &sent->bdaddr,
  1370. sent->bdaddr_type);
  1371. hci_dev_unlock(hdev);
  1372. return rp->status;
  1373. }
  1374. static u8 hci_cc_le_read_supported_states(struct hci_dev *hdev, void *data,
  1375. struct sk_buff *skb)
  1376. {
  1377. struct hci_rp_le_read_supported_states *rp = data;
  1378. bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
  1379. if (rp->status)
  1380. return rp->status;
  1381. memcpy(hdev->le_states, rp->le_states, 8);
  1382. return rp->status;
  1383. }
  1384. static u8 hci_cc_le_read_def_data_len(struct hci_dev *hdev, void *data,
  1385. struct sk_buff *skb)
  1386. {
  1387. struct hci_rp_le_read_def_data_len *rp = data;
  1388. bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
  1389. if (rp->status)
  1390. return rp->status;
  1391. hdev->le_def_tx_len = le16_to_cpu(rp->tx_len);
  1392. hdev->le_def_tx_time = le16_to_cpu(rp->tx_time);
  1393. return rp->status;
  1394. }
  1395. static u8 hci_cc_le_write_def_data_len(struct hci_dev *hdev, void *data,
  1396. struct sk_buff *skb)
  1397. {
  1398. struct hci_cp_le_write_def_data_len *sent;
  1399. struct hci_ev_status *rp = data;
  1400. bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
  1401. if (rp->status)
  1402. return rp->status;
  1403. sent = hci_sent_cmd_data(hdev, HCI_OP_LE_WRITE_DEF_DATA_LEN);
  1404. if (!sent)
  1405. return rp->status;
  1406. hdev->le_def_tx_len = le16_to_cpu(sent->tx_len);
  1407. hdev->le_def_tx_time = le16_to_cpu(sent->tx_time);
  1408. return rp->status;
  1409. }
  1410. static u8 hci_cc_le_add_to_resolv_list(struct hci_dev *hdev, void *data,
  1411. struct sk_buff *skb)
  1412. {
  1413. struct hci_cp_le_add_to_resolv_list *sent;
  1414. struct hci_ev_status *rp = data;
  1415. bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
  1416. if (rp->status)
  1417. return rp->status;
  1418. sent = hci_sent_cmd_data(hdev, HCI_OP_LE_ADD_TO_RESOLV_LIST);
  1419. if (!sent)
  1420. return rp->status;
  1421. hci_dev_lock(hdev);
  1422. hci_bdaddr_list_add_with_irk(&hdev->le_resolv_list, &sent->bdaddr,
  1423. sent->bdaddr_type, sent->peer_irk,
  1424. sent->local_irk);
  1425. hci_dev_unlock(hdev);
  1426. return rp->status;
  1427. }
  1428. static u8 hci_cc_le_del_from_resolv_list(struct hci_dev *hdev, void *data,
  1429. struct sk_buff *skb)
  1430. {
  1431. struct hci_cp_le_del_from_resolv_list *sent;
  1432. struct hci_ev_status *rp = data;
  1433. bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
  1434. if (rp->status)
  1435. return rp->status;
  1436. sent = hci_sent_cmd_data(hdev, HCI_OP_LE_DEL_FROM_RESOLV_LIST);
  1437. if (!sent)
  1438. return rp->status;
  1439. hci_dev_lock(hdev);
  1440. hci_bdaddr_list_del_with_irk(&hdev->le_resolv_list, &sent->bdaddr,
  1441. sent->bdaddr_type);
  1442. hci_dev_unlock(hdev);
  1443. return rp->status;
  1444. }
  1445. static u8 hci_cc_le_clear_resolv_list(struct hci_dev *hdev, void *data,
  1446. struct sk_buff *skb)
  1447. {
  1448. struct hci_ev_status *rp = data;
  1449. bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
  1450. if (rp->status)
  1451. return rp->status;
  1452. hci_dev_lock(hdev);
  1453. hci_bdaddr_list_clear(&hdev->le_resolv_list);
  1454. hci_dev_unlock(hdev);
  1455. return rp->status;
  1456. }
  1457. static u8 hci_cc_le_read_resolv_list_size(struct hci_dev *hdev, void *data,
  1458. struct sk_buff *skb)
  1459. {
  1460. struct hci_rp_le_read_resolv_list_size *rp = data;
  1461. bt_dev_dbg(hdev, "status 0x%2.2x size %u", rp->status, rp->size);
  1462. if (rp->status)
  1463. return rp->status;
  1464. hdev->le_resolv_list_size = rp->size;
  1465. return rp->status;
  1466. }
  1467. static u8 hci_cc_le_set_addr_resolution_enable(struct hci_dev *hdev, void *data,
  1468. struct sk_buff *skb)
  1469. {
  1470. struct hci_ev_status *rp = data;
  1471. __u8 *sent;
  1472. bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
  1473. if (rp->status)
  1474. return rp->status;
  1475. sent = hci_sent_cmd_data(hdev, HCI_OP_LE_SET_ADDR_RESOLV_ENABLE);
  1476. if (!sent)
  1477. return rp->status;
  1478. hci_dev_lock(hdev);
  1479. if (*sent)
  1480. hci_dev_set_flag(hdev, HCI_LL_RPA_RESOLUTION);
  1481. else
  1482. hci_dev_clear_flag(hdev, HCI_LL_RPA_RESOLUTION);
  1483. hci_dev_unlock(hdev);
  1484. return rp->status;
  1485. }
  1486. static u8 hci_cc_le_read_max_data_len(struct hci_dev *hdev, void *data,
  1487. struct sk_buff *skb)
  1488. {
  1489. struct hci_rp_le_read_max_data_len *rp = data;
  1490. bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
  1491. if (rp->status)
  1492. return rp->status;
  1493. hdev->le_max_tx_len = le16_to_cpu(rp->tx_len);
  1494. hdev->le_max_tx_time = le16_to_cpu(rp->tx_time);
  1495. hdev->le_max_rx_len = le16_to_cpu(rp->rx_len);
  1496. hdev->le_max_rx_time = le16_to_cpu(rp->rx_time);
  1497. return rp->status;
  1498. }
  1499. static u8 hci_cc_write_le_host_supported(struct hci_dev *hdev, void *data,
  1500. struct sk_buff *skb)
  1501. {
  1502. struct hci_cp_write_le_host_supported *sent;
  1503. struct hci_ev_status *rp = data;
  1504. bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
  1505. if (rp->status)
  1506. return rp->status;
  1507. sent = hci_sent_cmd_data(hdev, HCI_OP_WRITE_LE_HOST_SUPPORTED);
  1508. if (!sent)
  1509. return rp->status;
  1510. hci_dev_lock(hdev);
  1511. if (sent->le) {
  1512. hdev->features[1][0] |= LMP_HOST_LE;
  1513. hci_dev_set_flag(hdev, HCI_LE_ENABLED);
  1514. } else {
  1515. hdev->features[1][0] &= ~LMP_HOST_LE;
  1516. hci_dev_clear_flag(hdev, HCI_LE_ENABLED);
  1517. hci_dev_clear_flag(hdev, HCI_ADVERTISING);
  1518. }
  1519. if (sent->simul)
  1520. hdev->features[1][0] |= LMP_HOST_LE_BREDR;
  1521. else
  1522. hdev->features[1][0] &= ~LMP_HOST_LE_BREDR;
  1523. hci_dev_unlock(hdev);
  1524. return rp->status;
  1525. }
  1526. static u8 hci_cc_set_adv_param(struct hci_dev *hdev, void *data,
  1527. struct sk_buff *skb)
  1528. {
  1529. struct hci_cp_le_set_adv_param *cp;
  1530. struct hci_ev_status *rp = data;
  1531. bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
  1532. if (rp->status)
  1533. return rp->status;
  1534. cp = hci_sent_cmd_data(hdev, HCI_OP_LE_SET_ADV_PARAM);
  1535. if (!cp)
  1536. return rp->status;
  1537. hci_dev_lock(hdev);
  1538. hdev->adv_addr_type = cp->own_address_type;
  1539. hci_dev_unlock(hdev);
  1540. return rp->status;
  1541. }
  1542. static u8 hci_cc_read_rssi(struct hci_dev *hdev, void *data,
  1543. struct sk_buff *skb)
  1544. {
  1545. struct hci_rp_read_rssi *rp = data;
  1546. struct hci_conn *conn;
  1547. bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
  1548. if (rp->status)
  1549. return rp->status;
  1550. hci_dev_lock(hdev);
  1551. conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(rp->handle));
  1552. if (conn)
  1553. conn->rssi = rp->rssi;
  1554. hci_dev_unlock(hdev);
  1555. return rp->status;
  1556. }
  1557. static u8 hci_cc_read_tx_power(struct hci_dev *hdev, void *data,
  1558. struct sk_buff *skb)
  1559. {
  1560. struct hci_cp_read_tx_power *sent;
  1561. struct hci_rp_read_tx_power *rp = data;
  1562. struct hci_conn *conn;
  1563. bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
  1564. if (rp->status)
  1565. return rp->status;
  1566. sent = hci_sent_cmd_data(hdev, HCI_OP_READ_TX_POWER);
  1567. if (!sent)
  1568. return rp->status;
  1569. hci_dev_lock(hdev);
  1570. conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(rp->handle));
  1571. if (!conn)
  1572. goto unlock;
  1573. switch (sent->type) {
  1574. case 0x00:
  1575. conn->tx_power = rp->tx_power;
  1576. break;
  1577. case 0x01:
  1578. conn->max_tx_power = rp->tx_power;
  1579. break;
  1580. }
  1581. unlock:
  1582. hci_dev_unlock(hdev);
  1583. return rp->status;
  1584. }
  1585. static u8 hci_cc_write_ssp_debug_mode(struct hci_dev *hdev, void *data,
  1586. struct sk_buff *skb)
  1587. {
  1588. struct hci_ev_status *rp = data;
  1589. u8 *mode;
  1590. bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
  1591. if (rp->status)
  1592. return rp->status;
  1593. mode = hci_sent_cmd_data(hdev, HCI_OP_WRITE_SSP_DEBUG_MODE);
  1594. if (mode)
  1595. hdev->ssp_debug_mode = *mode;
  1596. return rp->status;
  1597. }
  1598. static void hci_cs_inquiry(struct hci_dev *hdev, __u8 status)
  1599. {
  1600. bt_dev_dbg(hdev, "status 0x%2.2x", status);
  1601. if (status)
  1602. return;
  1603. if (hci_sent_cmd_data(hdev, HCI_OP_INQUIRY))
  1604. set_bit(HCI_INQUIRY, &hdev->flags);
  1605. }
  1606. static void hci_cs_create_conn(struct hci_dev *hdev, __u8 status)
  1607. {
  1608. struct hci_cp_create_conn *cp;
  1609. struct hci_conn *conn;
  1610. bt_dev_dbg(hdev, "status 0x%2.2x", status);
  1611. cp = hci_sent_cmd_data(hdev, HCI_OP_CREATE_CONN);
  1612. if (!cp)
  1613. return;
  1614. hci_dev_lock(hdev);
  1615. conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &cp->bdaddr);
  1616. bt_dev_dbg(hdev, "bdaddr %pMR hcon %p", &cp->bdaddr, conn);
  1617. if (status) {
  1618. if (conn && conn->state == BT_CONNECT) {
  1619. conn->state = BT_CLOSED;
  1620. hci_connect_cfm(conn, status);
  1621. hci_conn_del(conn);
  1622. }
  1623. } else {
  1624. if (!conn) {
  1625. conn = hci_conn_add_unset(hdev, ACL_LINK, &cp->bdaddr,
  1626. HCI_ROLE_MASTER);
  1627. if (IS_ERR(conn))
  1628. bt_dev_err(hdev, "connection err: %ld", PTR_ERR(conn));
  1629. }
  1630. }
  1631. hci_dev_unlock(hdev);
  1632. }
  1633. static void hci_cs_add_sco(struct hci_dev *hdev, __u8 status)
  1634. {
  1635. struct hci_cp_add_sco *cp;
  1636. struct hci_conn *acl;
  1637. struct hci_link *link;
  1638. __u16 handle;
  1639. bt_dev_dbg(hdev, "status 0x%2.2x", status);
  1640. if (!status)
  1641. return;
  1642. cp = hci_sent_cmd_data(hdev, HCI_OP_ADD_SCO);
  1643. if (!cp)
  1644. return;
  1645. handle = __le16_to_cpu(cp->handle);
  1646. bt_dev_dbg(hdev, "handle 0x%4.4x", handle);
  1647. hci_dev_lock(hdev);
  1648. acl = hci_conn_hash_lookup_handle(hdev, handle);
  1649. if (acl) {
  1650. link = list_first_entry_or_null(&acl->link_list,
  1651. struct hci_link, list);
  1652. if (link && link->conn) {
  1653. link->conn->state = BT_CLOSED;
  1654. hci_connect_cfm(link->conn, status);
  1655. hci_conn_del(link->conn);
  1656. }
  1657. }
  1658. hci_dev_unlock(hdev);
  1659. }
  1660. static void hci_cs_auth_requested(struct hci_dev *hdev, __u8 status)
  1661. {
  1662. struct hci_cp_auth_requested *cp;
  1663. struct hci_conn *conn;
  1664. bt_dev_dbg(hdev, "status 0x%2.2x", status);
  1665. if (!status)
  1666. return;
  1667. cp = hci_sent_cmd_data(hdev, HCI_OP_AUTH_REQUESTED);
  1668. if (!cp)
  1669. return;
  1670. hci_dev_lock(hdev);
  1671. conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(cp->handle));
  1672. if (conn) {
  1673. if (conn->state == BT_CONFIG) {
  1674. hci_connect_cfm(conn, status);
  1675. hci_conn_drop(conn);
  1676. }
  1677. }
  1678. hci_dev_unlock(hdev);
  1679. }
  1680. static void hci_cs_set_conn_encrypt(struct hci_dev *hdev, __u8 status)
  1681. {
  1682. struct hci_cp_set_conn_encrypt *cp;
  1683. struct hci_conn *conn;
  1684. bt_dev_dbg(hdev, "status 0x%2.2x", status);
  1685. if (!status)
  1686. return;
  1687. cp = hci_sent_cmd_data(hdev, HCI_OP_SET_CONN_ENCRYPT);
  1688. if (!cp)
  1689. return;
  1690. hci_dev_lock(hdev);
  1691. conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(cp->handle));
  1692. if (conn) {
  1693. if (conn->state == BT_CONFIG) {
  1694. hci_connect_cfm(conn, status);
  1695. hci_conn_drop(conn);
  1696. }
  1697. }
  1698. hci_dev_unlock(hdev);
  1699. }
  1700. static int hci_outgoing_auth_needed(struct hci_dev *hdev,
  1701. struct hci_conn *conn)
  1702. {
  1703. if (conn->state != BT_CONFIG || !conn->out)
  1704. return 0;
  1705. if (conn->pending_sec_level == BT_SECURITY_SDP)
  1706. return 0;
  1707. /* Only request authentication for SSP connections or non-SSP
  1708. * devices with sec_level MEDIUM or HIGH or if MITM protection
  1709. * is requested.
  1710. */
  1711. if (!hci_conn_ssp_enabled(conn) && !(conn->auth_type & 0x01) &&
  1712. conn->pending_sec_level != BT_SECURITY_FIPS &&
  1713. conn->pending_sec_level != BT_SECURITY_HIGH &&
  1714. conn->pending_sec_level != BT_SECURITY_MEDIUM)
  1715. return 0;
  1716. return 1;
  1717. }
  1718. static int hci_resolve_name(struct hci_dev *hdev,
  1719. struct inquiry_entry *e)
  1720. {
  1721. struct hci_cp_remote_name_req cp;
  1722. memset(&cp, 0, sizeof(cp));
  1723. bacpy(&cp.bdaddr, &e->data.bdaddr);
  1724. cp.pscan_rep_mode = e->data.pscan_rep_mode;
  1725. cp.pscan_mode = e->data.pscan_mode;
  1726. cp.clock_offset = e->data.clock_offset;
  1727. return hci_send_cmd(hdev, HCI_OP_REMOTE_NAME_REQ, sizeof(cp), &cp);
  1728. }
  1729. static bool hci_resolve_next_name(struct hci_dev *hdev)
  1730. {
  1731. struct discovery_state *discov = &hdev->discovery;
  1732. struct inquiry_entry *e;
  1733. if (list_empty(&discov->resolve))
  1734. return false;
  1735. /* We should stop if we already spent too much time resolving names. */
  1736. if (time_after(jiffies, discov->name_resolve_timeout)) {
  1737. bt_dev_warn_ratelimited(hdev, "Name resolve takes too long.");
  1738. return false;
  1739. }
  1740. e = hci_inquiry_cache_lookup_resolve(hdev, BDADDR_ANY, NAME_NEEDED);
  1741. if (!e)
  1742. return false;
  1743. if (hci_resolve_name(hdev, e) == 0) {
  1744. e->name_state = NAME_PENDING;
  1745. return true;
  1746. }
  1747. return false;
  1748. }
  1749. static void hci_check_pending_name(struct hci_dev *hdev, struct hci_conn *conn,
  1750. bdaddr_t *bdaddr, u8 *name, u8 name_len)
  1751. {
  1752. struct discovery_state *discov = &hdev->discovery;
  1753. struct inquiry_entry *e;
  1754. /* Update the mgmt connected state if necessary. Be careful with
  1755. * conn objects that exist but are not (yet) connected however.
  1756. * Only those in BT_CONFIG or BT_CONNECTED states can be
  1757. * considered connected.
  1758. */
  1759. if (conn && (conn->state == BT_CONFIG || conn->state == BT_CONNECTED))
  1760. mgmt_device_connected(hdev, conn, name, name_len);
  1761. if (discov->state == DISCOVERY_STOPPED)
  1762. return;
  1763. if (discov->state == DISCOVERY_STOPPING)
  1764. goto discov_complete;
  1765. if (discov->state != DISCOVERY_RESOLVING)
  1766. return;
  1767. e = hci_inquiry_cache_lookup_resolve(hdev, bdaddr, NAME_PENDING);
  1768. /* If the device was not found in a list of found devices names of which
  1769. * are pending. there is no need to continue resolving a next name as it
  1770. * will be done upon receiving another Remote Name Request Complete
  1771. * Event */
  1772. if (!e)
  1773. return;
  1774. list_del(&e->list);
  1775. e->name_state = name ? NAME_KNOWN : NAME_NOT_KNOWN;
  1776. mgmt_remote_name(hdev, bdaddr, ACL_LINK, 0x00, e->data.rssi,
  1777. name, name_len);
  1778. if (hci_resolve_next_name(hdev))
  1779. return;
  1780. discov_complete:
  1781. hci_discovery_set_state(hdev, DISCOVERY_STOPPED);
  1782. }
  1783. static void hci_cs_remote_name_req(struct hci_dev *hdev, __u8 status)
  1784. {
  1785. struct hci_cp_remote_name_req *cp;
  1786. struct hci_conn *conn;
  1787. bt_dev_dbg(hdev, "status 0x%2.2x", status);
  1788. /* If successful wait for the name req complete event before
  1789. * checking for the need to do authentication */
  1790. if (!status)
  1791. return;
  1792. cp = hci_sent_cmd_data(hdev, HCI_OP_REMOTE_NAME_REQ);
  1793. if (!cp)
  1794. return;
  1795. hci_dev_lock(hdev);
  1796. conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &cp->bdaddr);
  1797. if (hci_dev_test_flag(hdev, HCI_MGMT))
  1798. hci_check_pending_name(hdev, conn, &cp->bdaddr, NULL, 0);
  1799. if (!conn)
  1800. goto unlock;
  1801. if (!hci_outgoing_auth_needed(hdev, conn))
  1802. goto unlock;
  1803. if (!test_and_set_bit(HCI_CONN_AUTH_PEND, &conn->flags)) {
  1804. struct hci_cp_auth_requested auth_cp;
  1805. set_bit(HCI_CONN_AUTH_INITIATOR, &conn->flags);
  1806. auth_cp.handle = __cpu_to_le16(conn->handle);
  1807. hci_send_cmd(hdev, HCI_OP_AUTH_REQUESTED,
  1808. sizeof(auth_cp), &auth_cp);
  1809. }
  1810. unlock:
  1811. hci_dev_unlock(hdev);
  1812. }
  1813. static void hci_cs_read_remote_features(struct hci_dev *hdev, __u8 status)
  1814. {
  1815. struct hci_cp_read_remote_features *cp;
  1816. struct hci_conn *conn;
  1817. bt_dev_dbg(hdev, "status 0x%2.2x", status);
  1818. if (!status)
  1819. return;
  1820. cp = hci_sent_cmd_data(hdev, HCI_OP_READ_REMOTE_FEATURES);
  1821. if (!cp)
  1822. return;
  1823. hci_dev_lock(hdev);
  1824. conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(cp->handle));
  1825. if (conn) {
  1826. if (conn->state == BT_CONFIG) {
  1827. hci_connect_cfm(conn, status);
  1828. hci_conn_drop(conn);
  1829. }
  1830. }
  1831. hci_dev_unlock(hdev);
  1832. }
  1833. static void hci_cs_read_remote_ext_features(struct hci_dev *hdev, __u8 status)
  1834. {
  1835. struct hci_cp_read_remote_ext_features *cp;
  1836. struct hci_conn *conn;
  1837. bt_dev_dbg(hdev, "status 0x%2.2x", status);
  1838. if (!status)
  1839. return;
  1840. cp = hci_sent_cmd_data(hdev, HCI_OP_READ_REMOTE_EXT_FEATURES);
  1841. if (!cp)
  1842. return;
  1843. hci_dev_lock(hdev);
  1844. conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(cp->handle));
  1845. if (conn) {
  1846. if (conn->state == BT_CONFIG) {
  1847. hci_connect_cfm(conn, status);
  1848. hci_conn_drop(conn);
  1849. }
  1850. }
  1851. hci_dev_unlock(hdev);
  1852. }
  1853. static void hci_setup_sync_conn_status(struct hci_dev *hdev, __u16 handle,
  1854. __u8 status)
  1855. {
  1856. struct hci_conn *acl;
  1857. struct hci_link *link;
  1858. bt_dev_dbg(hdev, "handle 0x%4.4x status 0x%2.2x", handle, status);
  1859. hci_dev_lock(hdev);
  1860. acl = hci_conn_hash_lookup_handle(hdev, handle);
  1861. if (acl) {
  1862. link = list_first_entry_or_null(&acl->link_list,
  1863. struct hci_link, list);
  1864. if (link && link->conn) {
  1865. link->conn->state = BT_CLOSED;
  1866. hci_connect_cfm(link->conn, status);
  1867. hci_conn_del(link->conn);
  1868. }
  1869. }
  1870. hci_dev_unlock(hdev);
  1871. }
  1872. static void hci_cs_setup_sync_conn(struct hci_dev *hdev, __u8 status)
  1873. {
  1874. struct hci_cp_setup_sync_conn *cp;
  1875. bt_dev_dbg(hdev, "status 0x%2.2x", status);
  1876. if (!status)
  1877. return;
  1878. cp = hci_sent_cmd_data(hdev, HCI_OP_SETUP_SYNC_CONN);
  1879. if (!cp)
  1880. return;
  1881. hci_setup_sync_conn_status(hdev, __le16_to_cpu(cp->handle), status);
  1882. }
  1883. static void hci_cs_enhanced_setup_sync_conn(struct hci_dev *hdev, __u8 status)
  1884. {
  1885. struct hci_cp_enhanced_setup_sync_conn *cp;
  1886. bt_dev_dbg(hdev, "status 0x%2.2x", status);
  1887. if (!status)
  1888. return;
  1889. cp = hci_sent_cmd_data(hdev, HCI_OP_ENHANCED_SETUP_SYNC_CONN);
  1890. if (!cp)
  1891. return;
  1892. hci_setup_sync_conn_status(hdev, __le16_to_cpu(cp->handle), status);
  1893. }
  1894. static void hci_cs_sniff_mode(struct hci_dev *hdev, __u8 status)
  1895. {
  1896. struct hci_cp_sniff_mode *cp;
  1897. struct hci_conn *conn;
  1898. bt_dev_dbg(hdev, "status 0x%2.2x", status);
  1899. if (!status)
  1900. return;
  1901. cp = hci_sent_cmd_data(hdev, HCI_OP_SNIFF_MODE);
  1902. if (!cp)
  1903. return;
  1904. hci_dev_lock(hdev);
  1905. conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(cp->handle));
  1906. if (conn) {
  1907. clear_bit(HCI_CONN_MODE_CHANGE_PEND, &conn->flags);
  1908. if (test_and_clear_bit(HCI_CONN_SCO_SETUP_PEND, &conn->flags))
  1909. hci_sco_setup(conn, status);
  1910. }
  1911. hci_dev_unlock(hdev);
  1912. }
  1913. static void hci_cs_exit_sniff_mode(struct hci_dev *hdev, __u8 status)
  1914. {
  1915. struct hci_cp_exit_sniff_mode *cp;
  1916. struct hci_conn *conn;
  1917. bt_dev_dbg(hdev, "status 0x%2.2x", status);
  1918. if (!status)
  1919. return;
  1920. cp = hci_sent_cmd_data(hdev, HCI_OP_EXIT_SNIFF_MODE);
  1921. if (!cp)
  1922. return;
  1923. hci_dev_lock(hdev);
  1924. conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(cp->handle));
  1925. if (conn) {
  1926. clear_bit(HCI_CONN_MODE_CHANGE_PEND, &conn->flags);
  1927. if (test_and_clear_bit(HCI_CONN_SCO_SETUP_PEND, &conn->flags))
  1928. hci_sco_setup(conn, status);
  1929. }
  1930. hci_dev_unlock(hdev);
  1931. }
  1932. static void hci_cs_disconnect(struct hci_dev *hdev, u8 status)
  1933. {
  1934. struct hci_cp_disconnect *cp;
  1935. struct hci_conn_params *params;
  1936. struct hci_conn *conn;
  1937. bool mgmt_conn;
  1938. bt_dev_dbg(hdev, "status 0x%2.2x", status);
  1939. /* Wait for HCI_EV_DISCONN_COMPLETE if status 0x00 and not suspended
  1940. * otherwise cleanup the connection immediately.
  1941. */
  1942. if (!status && !hdev->suspended)
  1943. return;
  1944. cp = hci_sent_cmd_data(hdev, HCI_OP_DISCONNECT);
  1945. if (!cp)
  1946. return;
  1947. hci_dev_lock(hdev);
  1948. conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(cp->handle));
  1949. if (!conn)
  1950. goto unlock;
  1951. if (status && status != HCI_ERROR_UNKNOWN_CONN_ID) {
  1952. mgmt_disconnect_failed(hdev, &conn->dst, conn->type,
  1953. conn->dst_type, status);
  1954. if (conn->type == LE_LINK && conn->role == HCI_ROLE_SLAVE) {
  1955. hdev->cur_adv_instance = conn->adv_instance;
  1956. hci_enable_advertising(hdev);
  1957. }
  1958. /* Inform sockets conn is gone before we delete it */
  1959. hci_disconn_cfm(conn, HCI_ERROR_UNSPECIFIED);
  1960. goto done;
  1961. }
  1962. /* During suspend, mark connection as closed immediately
  1963. * since we might not receive HCI_EV_DISCONN_COMPLETE
  1964. */
  1965. if (hdev->suspended)
  1966. conn->state = BT_CLOSED;
  1967. mgmt_conn = test_and_clear_bit(HCI_CONN_MGMT_CONNECTED, &conn->flags);
  1968. if (conn->type == ACL_LINK) {
  1969. if (test_and_clear_bit(HCI_CONN_FLUSH_KEY, &conn->flags))
  1970. hci_remove_link_key(hdev, &conn->dst);
  1971. }
  1972. params = hci_conn_params_lookup(hdev, &conn->dst, conn->dst_type);
  1973. if (params) {
  1974. switch (params->auto_connect) {
  1975. case HCI_AUTO_CONN_LINK_LOSS:
  1976. if (cp->reason != HCI_ERROR_CONNECTION_TIMEOUT)
  1977. break;
  1978. fallthrough;
  1979. case HCI_AUTO_CONN_DIRECT:
  1980. case HCI_AUTO_CONN_ALWAYS:
  1981. hci_pend_le_list_del_init(params);
  1982. hci_pend_le_list_add(params, &hdev->pend_le_conns);
  1983. break;
  1984. default:
  1985. break;
  1986. }
  1987. }
  1988. mgmt_device_disconnected(hdev, &conn->dst, conn->type, conn->dst_type,
  1989. cp->reason, mgmt_conn);
  1990. hci_disconn_cfm(conn, cp->reason);
  1991. done:
  1992. /* If the disconnection failed for any reason, the upper layer
  1993. * does not retry to disconnect in current implementation.
  1994. * Hence, we need to do some basic cleanup here and re-enable
  1995. * advertising if necessary.
  1996. */
  1997. hci_conn_del(conn);
  1998. unlock:
  1999. hci_dev_unlock(hdev);
  2000. }
  2001. static u8 ev_bdaddr_type(struct hci_dev *hdev, u8 type, bool *resolved)
  2002. {
  2003. /* When using controller based address resolution, then the new
  2004. * address types 0x02 and 0x03 are used. These types need to be
  2005. * converted back into either public address or random address type
  2006. */
  2007. switch (type) {
  2008. case ADDR_LE_DEV_PUBLIC_RESOLVED:
  2009. if (resolved)
  2010. *resolved = true;
  2011. return ADDR_LE_DEV_PUBLIC;
  2012. case ADDR_LE_DEV_RANDOM_RESOLVED:
  2013. if (resolved)
  2014. *resolved = true;
  2015. return ADDR_LE_DEV_RANDOM;
  2016. }
  2017. if (resolved)
  2018. *resolved = false;
  2019. return type;
  2020. }
  2021. static void cs_le_create_conn(struct hci_dev *hdev, bdaddr_t *peer_addr,
  2022. u8 peer_addr_type, u8 own_address_type,
  2023. u8 filter_policy)
  2024. {
  2025. struct hci_conn *conn;
  2026. conn = hci_conn_hash_lookup_le(hdev, peer_addr,
  2027. peer_addr_type);
  2028. if (!conn)
  2029. return;
  2030. own_address_type = ev_bdaddr_type(hdev, own_address_type, NULL);
  2031. /* Store the initiator and responder address information which
  2032. * is needed for SMP. These values will not change during the
  2033. * lifetime of the connection.
  2034. */
  2035. conn->init_addr_type = own_address_type;
  2036. if (own_address_type == ADDR_LE_DEV_RANDOM)
  2037. bacpy(&conn->init_addr, &hdev->random_addr);
  2038. else
  2039. bacpy(&conn->init_addr, &hdev->bdaddr);
  2040. conn->resp_addr_type = peer_addr_type;
  2041. bacpy(&conn->resp_addr, peer_addr);
  2042. }
  2043. static void hci_cs_le_create_conn(struct hci_dev *hdev, u8 status)
  2044. {
  2045. struct hci_cp_le_create_conn *cp;
  2046. bt_dev_dbg(hdev, "status 0x%2.2x", status);
  2047. /* All connection failure handling is taken care of by the
  2048. * hci_conn_failed function which is triggered by the HCI
  2049. * request completion callbacks used for connecting.
  2050. */
  2051. if (status)
  2052. return;
  2053. cp = hci_sent_cmd_data(hdev, HCI_OP_LE_CREATE_CONN);
  2054. if (!cp)
  2055. return;
  2056. hci_dev_lock(hdev);
  2057. cs_le_create_conn(hdev, &cp->peer_addr, cp->peer_addr_type,
  2058. cp->own_address_type, cp->filter_policy);
  2059. hci_dev_unlock(hdev);
  2060. }
  2061. static void hci_cs_le_ext_create_conn(struct hci_dev *hdev, u8 status)
  2062. {
  2063. struct hci_cp_le_ext_create_conn *cp;
  2064. bt_dev_dbg(hdev, "status 0x%2.2x", status);
  2065. /* All connection failure handling is taken care of by the
  2066. * hci_conn_failed function which is triggered by the HCI
  2067. * request completion callbacks used for connecting.
  2068. */
  2069. if (status)
  2070. return;
  2071. cp = hci_sent_cmd_data(hdev, HCI_OP_LE_EXT_CREATE_CONN);
  2072. if (!cp)
  2073. return;
  2074. hci_dev_lock(hdev);
  2075. cs_le_create_conn(hdev, &cp->peer_addr, cp->peer_addr_type,
  2076. cp->own_addr_type, cp->filter_policy);
  2077. hci_dev_unlock(hdev);
  2078. }
  2079. static void hci_cs_le_read_remote_features(struct hci_dev *hdev, u8 status)
  2080. {
  2081. struct hci_cp_le_read_remote_features *cp;
  2082. struct hci_conn *conn;
  2083. bt_dev_dbg(hdev, "status 0x%2.2x", status);
  2084. if (!status)
  2085. return;
  2086. cp = hci_sent_cmd_data(hdev, HCI_OP_LE_READ_REMOTE_FEATURES);
  2087. if (!cp)
  2088. return;
  2089. hci_dev_lock(hdev);
  2090. conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(cp->handle));
  2091. if (conn) {
  2092. if (conn->state == BT_CONFIG) {
  2093. hci_connect_cfm(conn, status);
  2094. hci_conn_drop(conn);
  2095. }
  2096. }
  2097. hci_dev_unlock(hdev);
  2098. }
  2099. static void hci_cs_le_start_enc(struct hci_dev *hdev, u8 status)
  2100. {
  2101. struct hci_cp_le_start_enc *cp;
  2102. struct hci_conn *conn;
  2103. bt_dev_dbg(hdev, "status 0x%2.2x", status);
  2104. if (!status)
  2105. return;
  2106. hci_dev_lock(hdev);
  2107. cp = hci_sent_cmd_data(hdev, HCI_OP_LE_START_ENC);
  2108. if (!cp)
  2109. goto unlock;
  2110. conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(cp->handle));
  2111. if (!conn)
  2112. goto unlock;
  2113. if (conn->state != BT_CONNECTED)
  2114. goto unlock;
  2115. hci_disconnect(conn, HCI_ERROR_AUTH_FAILURE);
  2116. hci_conn_drop(conn);
  2117. unlock:
  2118. hci_dev_unlock(hdev);
  2119. }
  2120. static void hci_cs_switch_role(struct hci_dev *hdev, u8 status)
  2121. {
  2122. struct hci_cp_switch_role *cp;
  2123. struct hci_conn *conn;
  2124. BT_DBG("%s status 0x%2.2x", hdev->name, status);
  2125. if (!status)
  2126. return;
  2127. cp = hci_sent_cmd_data(hdev, HCI_OP_SWITCH_ROLE);
  2128. if (!cp)
  2129. return;
  2130. hci_dev_lock(hdev);
  2131. conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &cp->bdaddr);
  2132. if (conn)
  2133. clear_bit(HCI_CONN_RSWITCH_PEND, &conn->flags);
  2134. hci_dev_unlock(hdev);
  2135. }
  2136. static void hci_inquiry_complete_evt(struct hci_dev *hdev, void *data,
  2137. struct sk_buff *skb)
  2138. {
  2139. struct hci_ev_status *ev = data;
  2140. struct discovery_state *discov = &hdev->discovery;
  2141. struct inquiry_entry *e;
  2142. bt_dev_dbg(hdev, "status 0x%2.2x", ev->status);
  2143. if (!test_and_clear_bit(HCI_INQUIRY, &hdev->flags))
  2144. return;
  2145. smp_mb__after_atomic(); /* wake_up_bit advises about this barrier */
  2146. wake_up_bit(&hdev->flags, HCI_INQUIRY);
  2147. if (!hci_dev_test_flag(hdev, HCI_MGMT))
  2148. return;
  2149. hci_dev_lock(hdev);
  2150. if (discov->state != DISCOVERY_FINDING)
  2151. goto unlock;
  2152. if (list_empty(&discov->resolve)) {
  2153. /* When BR/EDR inquiry is active and no LE scanning is in
  2154. * progress, then change discovery state to indicate completion.
  2155. *
  2156. * When running LE scanning and BR/EDR inquiry simultaneously
  2157. * and the LE scan already finished, then change the discovery
  2158. * state to indicate completion.
  2159. */
  2160. if (!hci_dev_test_flag(hdev, HCI_LE_SCAN) ||
  2161. !test_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks))
  2162. hci_discovery_set_state(hdev, DISCOVERY_STOPPED);
  2163. goto unlock;
  2164. }
  2165. e = hci_inquiry_cache_lookup_resolve(hdev, BDADDR_ANY, NAME_NEEDED);
  2166. if (e && hci_resolve_name(hdev, e) == 0) {
  2167. e->name_state = NAME_PENDING;
  2168. hci_discovery_set_state(hdev, DISCOVERY_RESOLVING);
  2169. discov->name_resolve_timeout = jiffies + NAME_RESOLVE_DURATION;
  2170. } else {
  2171. /* When BR/EDR inquiry is active and no LE scanning is in
  2172. * progress, then change discovery state to indicate completion.
  2173. *
  2174. * When running LE scanning and BR/EDR inquiry simultaneously
  2175. * and the LE scan already finished, then change the discovery
  2176. * state to indicate completion.
  2177. */
  2178. if (!hci_dev_test_flag(hdev, HCI_LE_SCAN) ||
  2179. !test_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks))
  2180. hci_discovery_set_state(hdev, DISCOVERY_STOPPED);
  2181. }
  2182. unlock:
  2183. hci_dev_unlock(hdev);
  2184. }
  2185. static void hci_inquiry_result_evt(struct hci_dev *hdev, void *edata,
  2186. struct sk_buff *skb)
  2187. {
  2188. struct hci_ev_inquiry_result *ev = edata;
  2189. struct inquiry_data data;
  2190. int i;
  2191. if (!hci_ev_skb_pull(hdev, skb, HCI_EV_INQUIRY_RESULT,
  2192. flex_array_size(ev, info, ev->num)))
  2193. return;
  2194. bt_dev_dbg(hdev, "num %d", ev->num);
  2195. if (!ev->num)
  2196. return;
  2197. if (hci_dev_test_flag(hdev, HCI_PERIODIC_INQ))
  2198. return;
  2199. hci_dev_lock(hdev);
  2200. for (i = 0; i < ev->num; i++) {
  2201. struct inquiry_info *info = &ev->info[i];
  2202. u32 flags;
  2203. bacpy(&data.bdaddr, &info->bdaddr);
  2204. data.pscan_rep_mode = info->pscan_rep_mode;
  2205. data.pscan_period_mode = info->pscan_period_mode;
  2206. data.pscan_mode = info->pscan_mode;
  2207. memcpy(data.dev_class, info->dev_class, 3);
  2208. data.clock_offset = info->clock_offset;
  2209. data.rssi = HCI_RSSI_INVALID;
  2210. data.ssp_mode = 0x00;
  2211. flags = hci_inquiry_cache_update(hdev, &data, false);
  2212. mgmt_device_found(hdev, &info->bdaddr, ACL_LINK, 0x00,
  2213. info->dev_class, HCI_RSSI_INVALID,
  2214. flags, NULL, 0, NULL, 0, 0);
  2215. }
  2216. hci_dev_unlock(hdev);
  2217. }
  2218. static void hci_conn_complete_evt(struct hci_dev *hdev, void *data,
  2219. struct sk_buff *skb)
  2220. {
  2221. struct hci_ev_conn_complete *ev = data;
  2222. struct hci_conn *conn;
  2223. u8 status = ev->status;
  2224. bt_dev_dbg(hdev, "status 0x%2.2x", status);
  2225. hci_dev_lock(hdev);
  2226. /* Check for existing connection:
  2227. *
  2228. * 1. If it doesn't exist then it must be receiver/slave role.
  2229. * 2. If it does exist confirm that it is connecting/BT_CONNECT in case
  2230. * of initiator/master role since there could be a collision where
  2231. * either side is attempting to connect or something like a fuzzing
  2232. * testing is trying to play tricks to destroy the hcon object before
  2233. * it even attempts to connect (e.g. hcon->state == BT_OPEN).
  2234. */
  2235. conn = hci_conn_hash_lookup_ba(hdev, ev->link_type, &ev->bdaddr);
  2236. if (!conn ||
  2237. (conn->role == HCI_ROLE_MASTER && conn->state != BT_CONNECT)) {
  2238. /* In case of error status and there is no connection pending
  2239. * just unlock as there is nothing to cleanup.
  2240. */
  2241. if (ev->status)
  2242. goto unlock;
  2243. /* Connection may not exist if auto-connected. Check the bredr
  2244. * allowlist to see if this device is allowed to auto connect.
  2245. * If link is an ACL type, create a connection class
  2246. * automatically.
  2247. *
  2248. * Auto-connect will only occur if the event filter is
  2249. * programmed with a given address. Right now, event filter is
  2250. * only used during suspend.
  2251. */
  2252. if (ev->link_type == ACL_LINK &&
  2253. hci_bdaddr_list_lookup_with_flags(&hdev->accept_list,
  2254. &ev->bdaddr,
  2255. BDADDR_BREDR)) {
  2256. conn = hci_conn_add_unset(hdev, ev->link_type,
  2257. &ev->bdaddr, HCI_ROLE_SLAVE);
  2258. if (IS_ERR(conn)) {
  2259. bt_dev_err(hdev, "connection err: %ld", PTR_ERR(conn));
  2260. goto unlock;
  2261. }
  2262. } else {
  2263. if (ev->link_type != SCO_LINK)
  2264. goto unlock;
  2265. conn = hci_conn_hash_lookup_ba(hdev, ESCO_LINK,
  2266. &ev->bdaddr);
  2267. if (!conn)
  2268. goto unlock;
  2269. conn->type = SCO_LINK;
  2270. }
  2271. }
  2272. /* The HCI_Connection_Complete event is only sent once per connection.
  2273. * Processing it more than once per connection can corrupt kernel memory.
  2274. *
  2275. * As the connection handle is set here for the first time, it indicates
  2276. * whether the connection is already set up.
  2277. */
  2278. if (!HCI_CONN_HANDLE_UNSET(conn->handle)) {
  2279. bt_dev_err(hdev, "Ignoring HCI_Connection_Complete for existing connection");
  2280. goto unlock;
  2281. }
  2282. if (!status) {
  2283. status = hci_conn_set_handle(conn, __le16_to_cpu(ev->handle));
  2284. if (status)
  2285. goto done;
  2286. if (conn->type == ACL_LINK) {
  2287. conn->state = BT_CONFIG;
  2288. hci_conn_hold(conn);
  2289. if (!conn->out && !hci_conn_ssp_enabled(conn) &&
  2290. !hci_find_link_key(hdev, &ev->bdaddr))
  2291. conn->disc_timeout = HCI_PAIRING_TIMEOUT;
  2292. else
  2293. conn->disc_timeout = HCI_DISCONN_TIMEOUT;
  2294. } else
  2295. conn->state = BT_CONNECTED;
  2296. hci_debugfs_create_conn(conn);
  2297. hci_conn_add_sysfs(conn);
  2298. if (test_bit(HCI_AUTH, &hdev->flags))
  2299. set_bit(HCI_CONN_AUTH, &conn->flags);
  2300. if (test_bit(HCI_ENCRYPT, &hdev->flags))
  2301. set_bit(HCI_CONN_ENCRYPT, &conn->flags);
  2302. /* "Link key request" completed ahead of "connect request" completes */
  2303. if (ev->encr_mode == 1 && !test_bit(HCI_CONN_ENCRYPT, &conn->flags) &&
  2304. ev->link_type == ACL_LINK) {
  2305. struct link_key *key;
  2306. struct hci_cp_read_enc_key_size cp;
  2307. key = hci_find_link_key(hdev, &ev->bdaddr);
  2308. if (key) {
  2309. set_bit(HCI_CONN_ENCRYPT, &conn->flags);
  2310. if (!read_key_size_capable(hdev)) {
  2311. conn->enc_key_size = HCI_LINK_KEY_SIZE;
  2312. } else {
  2313. cp.handle = cpu_to_le16(conn->handle);
  2314. if (hci_send_cmd(hdev, HCI_OP_READ_ENC_KEY_SIZE,
  2315. sizeof(cp), &cp)) {
  2316. bt_dev_err(hdev, "sending read key size failed");
  2317. conn->enc_key_size = HCI_LINK_KEY_SIZE;
  2318. }
  2319. }
  2320. hci_encrypt_cfm(conn, ev->status);
  2321. }
  2322. }
  2323. /* Get remote features */
  2324. if (conn->type == ACL_LINK) {
  2325. struct hci_cp_read_remote_features cp;
  2326. cp.handle = ev->handle;
  2327. hci_send_cmd(hdev, HCI_OP_READ_REMOTE_FEATURES,
  2328. sizeof(cp), &cp);
  2329. hci_update_scan(hdev);
  2330. }
  2331. /* Set packet type for incoming connection */
  2332. if (!conn->out && hdev->hci_ver < BLUETOOTH_VER_2_0) {
  2333. struct hci_cp_change_conn_ptype cp;
  2334. cp.handle = ev->handle;
  2335. cp.pkt_type = cpu_to_le16(conn->pkt_type);
  2336. hci_send_cmd(hdev, HCI_OP_CHANGE_CONN_PTYPE, sizeof(cp),
  2337. &cp);
  2338. }
  2339. }
  2340. if (conn->type == ACL_LINK)
  2341. hci_sco_setup(conn, ev->status);
  2342. done:
  2343. if (status) {
  2344. hci_conn_failed(conn, status);
  2345. } else if (ev->link_type == SCO_LINK) {
  2346. switch (conn->setting & SCO_AIRMODE_MASK) {
  2347. case SCO_AIRMODE_CVSD:
  2348. if (hdev->notify)
  2349. hdev->notify(hdev, HCI_NOTIFY_ENABLE_SCO_CVSD);
  2350. break;
  2351. }
  2352. hci_connect_cfm(conn, status);
  2353. }
  2354. unlock:
  2355. hci_dev_unlock(hdev);
  2356. }
  2357. static void hci_reject_conn(struct hci_dev *hdev, bdaddr_t *bdaddr)
  2358. {
  2359. struct hci_cp_reject_conn_req cp;
  2360. bacpy(&cp.bdaddr, bdaddr);
  2361. cp.reason = HCI_ERROR_REJ_BAD_ADDR;
  2362. hci_send_cmd(hdev, HCI_OP_REJECT_CONN_REQ, sizeof(cp), &cp);
  2363. }
  2364. static void hci_conn_request_evt(struct hci_dev *hdev, void *data,
  2365. struct sk_buff *skb)
  2366. {
  2367. struct hci_ev_conn_request *ev = data;
  2368. int mask = hdev->link_mode;
  2369. struct inquiry_entry *ie;
  2370. struct hci_conn *conn;
  2371. __u8 flags = 0;
  2372. bt_dev_dbg(hdev, "bdaddr %pMR type 0x%x", &ev->bdaddr, ev->link_type);
  2373. /* Reject incoming connection from device with same BD ADDR against
  2374. * CVE-2020-26555
  2375. */
  2376. if (hdev && !bacmp(&hdev->bdaddr, &ev->bdaddr)) {
  2377. bt_dev_dbg(hdev, "Reject connection with same BD_ADDR %pMR\n",
  2378. &ev->bdaddr);
  2379. hci_reject_conn(hdev, &ev->bdaddr);
  2380. return;
  2381. }
  2382. mask |= hci_proto_connect_ind(hdev, &ev->bdaddr, ev->link_type,
  2383. &flags);
  2384. if (!(mask & HCI_LM_ACCEPT)) {
  2385. hci_reject_conn(hdev, &ev->bdaddr);
  2386. return;
  2387. }
  2388. hci_dev_lock(hdev);
  2389. if (hci_bdaddr_list_lookup(&hdev->reject_list, &ev->bdaddr,
  2390. BDADDR_BREDR)) {
  2391. hci_reject_conn(hdev, &ev->bdaddr);
  2392. goto unlock;
  2393. }
  2394. /* Require HCI_CONNECTABLE or an accept list entry to accept the
  2395. * connection. These features are only touched through mgmt so
  2396. * only do the checks if HCI_MGMT is set.
  2397. */
  2398. if (hci_dev_test_flag(hdev, HCI_MGMT) &&
  2399. !hci_dev_test_flag(hdev, HCI_CONNECTABLE) &&
  2400. !hci_bdaddr_list_lookup_with_flags(&hdev->accept_list, &ev->bdaddr,
  2401. BDADDR_BREDR)) {
  2402. hci_reject_conn(hdev, &ev->bdaddr);
  2403. goto unlock;
  2404. }
  2405. /* Connection accepted */
  2406. ie = hci_inquiry_cache_lookup(hdev, &ev->bdaddr);
  2407. if (ie)
  2408. memcpy(ie->data.dev_class, ev->dev_class, 3);
  2409. conn = hci_conn_hash_lookup_ba(hdev, ev->link_type,
  2410. &ev->bdaddr);
  2411. if (!conn) {
  2412. conn = hci_conn_add_unset(hdev, ev->link_type, &ev->bdaddr,
  2413. HCI_ROLE_SLAVE);
  2414. if (IS_ERR(conn)) {
  2415. bt_dev_err(hdev, "connection err: %ld", PTR_ERR(conn));
  2416. goto unlock;
  2417. }
  2418. }
  2419. memcpy(conn->dev_class, ev->dev_class, 3);
  2420. hci_dev_unlock(hdev);
  2421. if (ev->link_type == ACL_LINK ||
  2422. (!(flags & HCI_PROTO_DEFER) && !lmp_esco_capable(hdev))) {
  2423. struct hci_cp_accept_conn_req cp;
  2424. conn->state = BT_CONNECT;
  2425. bacpy(&cp.bdaddr, &ev->bdaddr);
  2426. if (lmp_rswitch_capable(hdev) && (mask & HCI_LM_MASTER))
  2427. cp.role = 0x00; /* Become central */
  2428. else
  2429. cp.role = 0x01; /* Remain peripheral */
  2430. hci_send_cmd(hdev, HCI_OP_ACCEPT_CONN_REQ, sizeof(cp), &cp);
  2431. } else if (!(flags & HCI_PROTO_DEFER)) {
  2432. struct hci_cp_accept_sync_conn_req cp;
  2433. conn->state = BT_CONNECT;
  2434. bacpy(&cp.bdaddr, &ev->bdaddr);
  2435. cp.pkt_type = cpu_to_le16(conn->pkt_type);
  2436. cp.tx_bandwidth = cpu_to_le32(0x00001f40);
  2437. cp.rx_bandwidth = cpu_to_le32(0x00001f40);
  2438. cp.max_latency = cpu_to_le16(0xffff);
  2439. cp.content_format = cpu_to_le16(hdev->voice_setting);
  2440. cp.retrans_effort = 0xff;
  2441. hci_send_cmd(hdev, HCI_OP_ACCEPT_SYNC_CONN_REQ, sizeof(cp),
  2442. &cp);
  2443. } else {
  2444. conn->state = BT_CONNECT2;
  2445. hci_connect_cfm(conn, 0);
  2446. }
  2447. return;
  2448. unlock:
  2449. hci_dev_unlock(hdev);
  2450. }
  2451. static u8 hci_to_mgmt_reason(u8 err)
  2452. {
  2453. switch (err) {
  2454. case HCI_ERROR_CONNECTION_TIMEOUT:
  2455. return MGMT_DEV_DISCONN_TIMEOUT;
  2456. case HCI_ERROR_REMOTE_USER_TERM:
  2457. case HCI_ERROR_REMOTE_LOW_RESOURCES:
  2458. case HCI_ERROR_REMOTE_POWER_OFF:
  2459. return MGMT_DEV_DISCONN_REMOTE;
  2460. case HCI_ERROR_LOCAL_HOST_TERM:
  2461. return MGMT_DEV_DISCONN_LOCAL_HOST;
  2462. default:
  2463. return MGMT_DEV_DISCONN_UNKNOWN;
  2464. }
  2465. }
  2466. static void hci_disconn_complete_evt(struct hci_dev *hdev, void *data,
  2467. struct sk_buff *skb)
  2468. {
  2469. struct hci_ev_disconn_complete *ev = data;
  2470. u8 reason;
  2471. struct hci_conn_params *params;
  2472. struct hci_conn *conn;
  2473. bool mgmt_connected;
  2474. bt_dev_dbg(hdev, "status 0x%2.2x", ev->status);
  2475. hci_dev_lock(hdev);
  2476. conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
  2477. if (!conn)
  2478. goto unlock;
  2479. if (ev->status) {
  2480. mgmt_disconnect_failed(hdev, &conn->dst, conn->type,
  2481. conn->dst_type, ev->status);
  2482. goto unlock;
  2483. }
  2484. conn->state = BT_CLOSED;
  2485. mgmt_connected = test_and_clear_bit(HCI_CONN_MGMT_CONNECTED, &conn->flags);
  2486. if (test_bit(HCI_CONN_AUTH_FAILURE, &conn->flags))
  2487. reason = MGMT_DEV_DISCONN_AUTH_FAILURE;
  2488. else
  2489. reason = hci_to_mgmt_reason(ev->reason);
  2490. mgmt_device_disconnected(hdev, &conn->dst, conn->type, conn->dst_type,
  2491. reason, mgmt_connected);
  2492. if (conn->type == ACL_LINK) {
  2493. if (test_and_clear_bit(HCI_CONN_FLUSH_KEY, &conn->flags))
  2494. hci_remove_link_key(hdev, &conn->dst);
  2495. hci_update_scan(hdev);
  2496. }
  2497. /* Re-enable passive scanning if disconnected device is marked
  2498. * as auto-connectable.
  2499. */
  2500. if (conn->type == LE_LINK) {
  2501. params = hci_conn_params_lookup(hdev, &conn->dst,
  2502. conn->dst_type);
  2503. if (params) {
  2504. switch (params->auto_connect) {
  2505. case HCI_AUTO_CONN_LINK_LOSS:
  2506. if (ev->reason != HCI_ERROR_CONNECTION_TIMEOUT)
  2507. break;
  2508. fallthrough;
  2509. case HCI_AUTO_CONN_DIRECT:
  2510. case HCI_AUTO_CONN_ALWAYS:
  2511. hci_pend_le_list_del_init(params);
  2512. hci_pend_le_list_add(params,
  2513. &hdev->pend_le_conns);
  2514. hci_update_passive_scan(hdev);
  2515. break;
  2516. default:
  2517. break;
  2518. }
  2519. }
  2520. }
  2521. hci_disconn_cfm(conn, ev->reason);
  2522. /* Re-enable advertising if necessary, since it might
  2523. * have been disabled by the connection. From the
  2524. * HCI_LE_Set_Advertise_Enable command description in
  2525. * the core specification (v4.0):
  2526. * "The Controller shall continue advertising until the Host
  2527. * issues an LE_Set_Advertise_Enable command with
  2528. * Advertising_Enable set to 0x00 (Advertising is disabled)
  2529. * or until a connection is created or until the Advertising
  2530. * is timed out due to Directed Advertising."
  2531. */
  2532. if (conn->type == LE_LINK && conn->role == HCI_ROLE_SLAVE) {
  2533. hdev->cur_adv_instance = conn->adv_instance;
  2534. hci_enable_advertising(hdev);
  2535. }
  2536. hci_conn_del(conn);
  2537. unlock:
  2538. hci_dev_unlock(hdev);
  2539. }
  2540. static void hci_auth_complete_evt(struct hci_dev *hdev, void *data,
  2541. struct sk_buff *skb)
  2542. {
  2543. struct hci_ev_auth_complete *ev = data;
  2544. struct hci_conn *conn;
  2545. bt_dev_dbg(hdev, "status 0x%2.2x", ev->status);
  2546. hci_dev_lock(hdev);
  2547. conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
  2548. if (!conn)
  2549. goto unlock;
  2550. if (!ev->status) {
  2551. clear_bit(HCI_CONN_AUTH_FAILURE, &conn->flags);
  2552. set_bit(HCI_CONN_AUTH, &conn->flags);
  2553. conn->sec_level = conn->pending_sec_level;
  2554. } else {
  2555. if (ev->status == HCI_ERROR_PIN_OR_KEY_MISSING)
  2556. set_bit(HCI_CONN_AUTH_FAILURE, &conn->flags);
  2557. mgmt_auth_failed(conn, ev->status);
  2558. }
  2559. clear_bit(HCI_CONN_AUTH_PEND, &conn->flags);
  2560. if (conn->state == BT_CONFIG) {
  2561. if (!ev->status && hci_conn_ssp_enabled(conn)) {
  2562. struct hci_cp_set_conn_encrypt cp;
  2563. cp.handle = ev->handle;
  2564. cp.encrypt = 0x01;
  2565. hci_send_cmd(hdev, HCI_OP_SET_CONN_ENCRYPT, sizeof(cp),
  2566. &cp);
  2567. } else {
  2568. conn->state = BT_CONNECTED;
  2569. hci_connect_cfm(conn, ev->status);
  2570. hci_conn_drop(conn);
  2571. }
  2572. } else {
  2573. hci_auth_cfm(conn, ev->status);
  2574. hci_conn_hold(conn);
  2575. conn->disc_timeout = HCI_DISCONN_TIMEOUT;
  2576. hci_conn_drop(conn);
  2577. }
  2578. if (test_bit(HCI_CONN_ENCRYPT_PEND, &conn->flags)) {
  2579. if (!ev->status) {
  2580. struct hci_cp_set_conn_encrypt cp;
  2581. cp.handle = ev->handle;
  2582. cp.encrypt = 0x01;
  2583. hci_send_cmd(hdev, HCI_OP_SET_CONN_ENCRYPT, sizeof(cp),
  2584. &cp);
  2585. } else {
  2586. clear_bit(HCI_CONN_ENCRYPT_PEND, &conn->flags);
  2587. hci_encrypt_cfm(conn, ev->status);
  2588. }
  2589. }
  2590. unlock:
  2591. hci_dev_unlock(hdev);
  2592. }
  2593. static void hci_remote_name_evt(struct hci_dev *hdev, void *data,
  2594. struct sk_buff *skb)
  2595. {
  2596. struct hci_ev_remote_name *ev = data;
  2597. struct hci_conn *conn;
  2598. bt_dev_dbg(hdev, "status 0x%2.2x", ev->status);
  2599. hci_dev_lock(hdev);
  2600. conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &ev->bdaddr);
  2601. if (!hci_dev_test_flag(hdev, HCI_MGMT))
  2602. goto check_auth;
  2603. if (ev->status == 0)
  2604. hci_check_pending_name(hdev, conn, &ev->bdaddr, ev->name,
  2605. strnlen(ev->name, HCI_MAX_NAME_LENGTH));
  2606. else
  2607. hci_check_pending_name(hdev, conn, &ev->bdaddr, NULL, 0);
  2608. check_auth:
  2609. if (!conn)
  2610. goto unlock;
  2611. if (!hci_outgoing_auth_needed(hdev, conn))
  2612. goto unlock;
  2613. if (!test_and_set_bit(HCI_CONN_AUTH_PEND, &conn->flags)) {
  2614. struct hci_cp_auth_requested cp;
  2615. set_bit(HCI_CONN_AUTH_INITIATOR, &conn->flags);
  2616. cp.handle = __cpu_to_le16(conn->handle);
  2617. hci_send_cmd(hdev, HCI_OP_AUTH_REQUESTED, sizeof(cp), &cp);
  2618. }
  2619. unlock:
  2620. hci_dev_unlock(hdev);
  2621. }
  2622. static void hci_encrypt_change_evt(struct hci_dev *hdev, void *data,
  2623. struct sk_buff *skb)
  2624. {
  2625. struct hci_ev_encrypt_change *ev = data;
  2626. struct hci_conn *conn;
  2627. bt_dev_dbg(hdev, "status 0x%2.2x", ev->status);
  2628. hci_dev_lock(hdev);
  2629. conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
  2630. if (!conn)
  2631. goto unlock;
  2632. if (!ev->status) {
  2633. if (ev->encrypt) {
  2634. /* Encryption implies authentication */
  2635. set_bit(HCI_CONN_AUTH, &conn->flags);
  2636. set_bit(HCI_CONN_ENCRYPT, &conn->flags);
  2637. conn->sec_level = conn->pending_sec_level;
  2638. /* P-256 authentication key implies FIPS */
  2639. if (conn->key_type == HCI_LK_AUTH_COMBINATION_P256)
  2640. set_bit(HCI_CONN_FIPS, &conn->flags);
  2641. if ((conn->type == ACL_LINK && ev->encrypt == 0x02) ||
  2642. conn->type == LE_LINK)
  2643. set_bit(HCI_CONN_AES_CCM, &conn->flags);
  2644. } else {
  2645. clear_bit(HCI_CONN_ENCRYPT, &conn->flags);
  2646. clear_bit(HCI_CONN_AES_CCM, &conn->flags);
  2647. }
  2648. }
  2649. /* We should disregard the current RPA and generate a new one
  2650. * whenever the encryption procedure fails.
  2651. */
  2652. if (ev->status && conn->type == LE_LINK) {
  2653. hci_dev_set_flag(hdev, HCI_RPA_EXPIRED);
  2654. hci_adv_instances_set_rpa_expired(hdev, true);
  2655. }
  2656. clear_bit(HCI_CONN_ENCRYPT_PEND, &conn->flags);
  2657. /* Check link security requirements are met */
  2658. if (!hci_conn_check_link_mode(conn))
  2659. ev->status = HCI_ERROR_AUTH_FAILURE;
  2660. if (ev->status && conn->state == BT_CONNECTED) {
  2661. if (ev->status == HCI_ERROR_PIN_OR_KEY_MISSING)
  2662. set_bit(HCI_CONN_AUTH_FAILURE, &conn->flags);
  2663. /* Notify upper layers so they can cleanup before
  2664. * disconnecting.
  2665. */
  2666. hci_encrypt_cfm(conn, ev->status);
  2667. hci_disconnect(conn, HCI_ERROR_AUTH_FAILURE);
  2668. hci_conn_drop(conn);
  2669. goto unlock;
  2670. }
  2671. /* Try reading the encryption key size for encrypted ACL links */
  2672. if (!ev->status && ev->encrypt && conn->type == ACL_LINK) {
  2673. struct hci_cp_read_enc_key_size cp;
  2674. /* Only send HCI_Read_Encryption_Key_Size if the
  2675. * controller really supports it. If it doesn't, assume
  2676. * the default size (16).
  2677. */
  2678. if (!read_key_size_capable(hdev)) {
  2679. conn->enc_key_size = HCI_LINK_KEY_SIZE;
  2680. goto notify;
  2681. }
  2682. cp.handle = cpu_to_le16(conn->handle);
  2683. if (hci_send_cmd(hdev, HCI_OP_READ_ENC_KEY_SIZE,
  2684. sizeof(cp), &cp)) {
  2685. bt_dev_err(hdev, "sending read key size failed");
  2686. conn->enc_key_size = HCI_LINK_KEY_SIZE;
  2687. goto notify;
  2688. }
  2689. goto unlock;
  2690. }
  2691. /* We skip the WRITE_AUTH_PAYLOAD_TIMEOUT for ATS2851 based controllers
  2692. * to avoid unexpected SMP command errors when pairing.
  2693. */
  2694. if (test_bit(HCI_QUIRK_BROKEN_WRITE_AUTH_PAYLOAD_TIMEOUT,
  2695. &hdev->quirks))
  2696. goto notify;
  2697. /* Set the default Authenticated Payload Timeout after
  2698. * an LE Link is established. As per Core Spec v5.0, Vol 2, Part B
  2699. * Section 3.3, the HCI command WRITE_AUTH_PAYLOAD_TIMEOUT should be
  2700. * sent when the link is active and Encryption is enabled, the conn
  2701. * type can be either LE or ACL and controller must support LMP Ping.
  2702. * Ensure for AES-CCM encryption as well.
  2703. */
  2704. if (test_bit(HCI_CONN_ENCRYPT, &conn->flags) &&
  2705. test_bit(HCI_CONN_AES_CCM, &conn->flags) &&
  2706. ((conn->type == ACL_LINK && lmp_ping_capable(hdev)) ||
  2707. (conn->type == LE_LINK && (hdev->le_features[0] & HCI_LE_PING)))) {
  2708. struct hci_cp_write_auth_payload_to cp;
  2709. cp.handle = cpu_to_le16(conn->handle);
  2710. cp.timeout = cpu_to_le16(hdev->auth_payload_timeout);
  2711. if (hci_send_cmd(conn->hdev, HCI_OP_WRITE_AUTH_PAYLOAD_TO,
  2712. sizeof(cp), &cp))
  2713. bt_dev_err(hdev, "write auth payload timeout failed");
  2714. }
  2715. notify:
  2716. hci_encrypt_cfm(conn, ev->status);
  2717. unlock:
  2718. hci_dev_unlock(hdev);
  2719. }
  2720. static void hci_change_link_key_complete_evt(struct hci_dev *hdev, void *data,
  2721. struct sk_buff *skb)
  2722. {
  2723. struct hci_ev_change_link_key_complete *ev = data;
  2724. struct hci_conn *conn;
  2725. bt_dev_dbg(hdev, "status 0x%2.2x", ev->status);
  2726. hci_dev_lock(hdev);
  2727. conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
  2728. if (conn) {
  2729. if (!ev->status)
  2730. set_bit(HCI_CONN_SECURE, &conn->flags);
  2731. clear_bit(HCI_CONN_AUTH_PEND, &conn->flags);
  2732. hci_key_change_cfm(conn, ev->status);
  2733. }
  2734. hci_dev_unlock(hdev);
  2735. }
  2736. static void hci_remote_features_evt(struct hci_dev *hdev, void *data,
  2737. struct sk_buff *skb)
  2738. {
  2739. struct hci_ev_remote_features *ev = data;
  2740. struct hci_conn *conn;
  2741. bt_dev_dbg(hdev, "status 0x%2.2x", ev->status);
  2742. hci_dev_lock(hdev);
  2743. conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
  2744. if (!conn)
  2745. goto unlock;
  2746. if (!ev->status)
  2747. memcpy(conn->features[0], ev->features, 8);
  2748. if (conn->state != BT_CONFIG)
  2749. goto unlock;
  2750. if (!ev->status && lmp_ext_feat_capable(hdev) &&
  2751. lmp_ext_feat_capable(conn)) {
  2752. struct hci_cp_read_remote_ext_features cp;
  2753. cp.handle = ev->handle;
  2754. cp.page = 0x01;
  2755. hci_send_cmd(hdev, HCI_OP_READ_REMOTE_EXT_FEATURES,
  2756. sizeof(cp), &cp);
  2757. goto unlock;
  2758. }
  2759. if (!ev->status) {
  2760. struct hci_cp_remote_name_req cp;
  2761. memset(&cp, 0, sizeof(cp));
  2762. bacpy(&cp.bdaddr, &conn->dst);
  2763. cp.pscan_rep_mode = 0x02;
  2764. hci_send_cmd(hdev, HCI_OP_REMOTE_NAME_REQ, sizeof(cp), &cp);
  2765. } else {
  2766. mgmt_device_connected(hdev, conn, NULL, 0);
  2767. }
  2768. if (!hci_outgoing_auth_needed(hdev, conn)) {
  2769. conn->state = BT_CONNECTED;
  2770. hci_connect_cfm(conn, ev->status);
  2771. hci_conn_drop(conn);
  2772. }
  2773. unlock:
  2774. hci_dev_unlock(hdev);
  2775. }
  2776. static inline void handle_cmd_cnt_and_timer(struct hci_dev *hdev, u8 ncmd)
  2777. {
  2778. cancel_delayed_work(&hdev->cmd_timer);
  2779. rcu_read_lock();
  2780. if (!test_bit(HCI_RESET, &hdev->flags)) {
  2781. if (ncmd) {
  2782. cancel_delayed_work(&hdev->ncmd_timer);
  2783. atomic_set(&hdev->cmd_cnt, 1);
  2784. } else {
  2785. if (!hci_dev_test_flag(hdev, HCI_CMD_DRAIN_WORKQUEUE))
  2786. queue_delayed_work(hdev->workqueue, &hdev->ncmd_timer,
  2787. HCI_NCMD_TIMEOUT);
  2788. }
  2789. }
  2790. rcu_read_unlock();
  2791. }
  2792. static u8 hci_cc_le_read_buffer_size_v2(struct hci_dev *hdev, void *data,
  2793. struct sk_buff *skb)
  2794. {
  2795. struct hci_rp_le_read_buffer_size_v2 *rp = data;
  2796. bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
  2797. if (rp->status)
  2798. return rp->status;
  2799. hdev->le_mtu = __le16_to_cpu(rp->acl_mtu);
  2800. hdev->le_pkts = rp->acl_max_pkt;
  2801. hdev->iso_mtu = __le16_to_cpu(rp->iso_mtu);
  2802. hdev->iso_pkts = rp->iso_max_pkt;
  2803. hdev->le_cnt = hdev->le_pkts;
  2804. hdev->iso_cnt = hdev->iso_pkts;
  2805. BT_DBG("%s acl mtu %d:%d iso mtu %d:%d", hdev->name, hdev->acl_mtu,
  2806. hdev->acl_pkts, hdev->iso_mtu, hdev->iso_pkts);
  2807. if (hdev->le_mtu && hdev->le_mtu < HCI_MIN_LE_MTU)
  2808. return HCI_ERROR_INVALID_PARAMETERS;
  2809. return rp->status;
  2810. }
  2811. static void hci_unbound_cis_failed(struct hci_dev *hdev, u8 cig, u8 status)
  2812. {
  2813. struct hci_conn *conn, *tmp;
  2814. lockdep_assert_held(&hdev->lock);
  2815. list_for_each_entry_safe(conn, tmp, &hdev->conn_hash.list, list) {
  2816. if (conn->type != ISO_LINK || !bacmp(&conn->dst, BDADDR_ANY) ||
  2817. conn->state == BT_OPEN || conn->iso_qos.ucast.cig != cig)
  2818. continue;
  2819. if (HCI_CONN_HANDLE_UNSET(conn->handle))
  2820. hci_conn_failed(conn, status);
  2821. }
  2822. }
  2823. static u8 hci_cc_le_set_cig_params(struct hci_dev *hdev, void *data,
  2824. struct sk_buff *skb)
  2825. {
  2826. struct hci_rp_le_set_cig_params *rp = data;
  2827. struct hci_cp_le_set_cig_params *cp;
  2828. struct hci_conn *conn;
  2829. u8 status = rp->status;
  2830. bool pending = false;
  2831. int i;
  2832. bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
  2833. cp = hci_sent_cmd_data(hdev, HCI_OP_LE_SET_CIG_PARAMS);
  2834. if (!rp->status && (!cp || rp->num_handles != cp->num_cis ||
  2835. rp->cig_id != cp->cig_id)) {
  2836. bt_dev_err(hdev, "unexpected Set CIG Parameters response data");
  2837. status = HCI_ERROR_UNSPECIFIED;
  2838. }
  2839. hci_dev_lock(hdev);
  2840. /* BLUETOOTH CORE SPECIFICATION Version 5.4 | Vol 4, Part E page 2554
  2841. *
  2842. * If the Status return parameter is non-zero, then the state of the CIG
  2843. * and its CIS configurations shall not be changed by the command. If
  2844. * the CIG did not already exist, it shall not be created.
  2845. */
  2846. if (status) {
  2847. /* Keep current configuration, fail only the unbound CIS */
  2848. hci_unbound_cis_failed(hdev, rp->cig_id, status);
  2849. goto unlock;
  2850. }
  2851. /* BLUETOOTH CORE SPECIFICATION Version 5.3 | Vol 4, Part E page 2553
  2852. *
  2853. * If the Status return parameter is zero, then the Controller shall
  2854. * set the Connection_Handle arrayed return parameter to the connection
  2855. * handle(s) corresponding to the CIS configurations specified in
  2856. * the CIS_IDs command parameter, in the same order.
  2857. */
  2858. for (i = 0; i < rp->num_handles; ++i) {
  2859. conn = hci_conn_hash_lookup_cis(hdev, NULL, 0, rp->cig_id,
  2860. cp->cis[i].cis_id);
  2861. if (!conn || !bacmp(&conn->dst, BDADDR_ANY))
  2862. continue;
  2863. if (conn->state != BT_BOUND && conn->state != BT_CONNECT)
  2864. continue;
  2865. if (hci_conn_set_handle(conn, __le16_to_cpu(rp->handle[i])))
  2866. continue;
  2867. if (conn->state == BT_CONNECT)
  2868. pending = true;
  2869. }
  2870. unlock:
  2871. if (pending)
  2872. hci_le_create_cis_pending(hdev);
  2873. hci_dev_unlock(hdev);
  2874. return rp->status;
  2875. }
  2876. static u8 hci_cc_le_setup_iso_path(struct hci_dev *hdev, void *data,
  2877. struct sk_buff *skb)
  2878. {
  2879. struct hci_rp_le_setup_iso_path *rp = data;
  2880. struct hci_cp_le_setup_iso_path *cp;
  2881. struct hci_conn *conn;
  2882. bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
  2883. cp = hci_sent_cmd_data(hdev, HCI_OP_LE_SETUP_ISO_PATH);
  2884. if (!cp)
  2885. return rp->status;
  2886. hci_dev_lock(hdev);
  2887. conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(cp->handle));
  2888. if (!conn)
  2889. goto unlock;
  2890. if (rp->status) {
  2891. hci_connect_cfm(conn, rp->status);
  2892. hci_conn_del(conn);
  2893. goto unlock;
  2894. }
  2895. switch (cp->direction) {
  2896. /* Input (Host to Controller) */
  2897. case 0x00:
  2898. /* Only confirm connection if output only */
  2899. if (conn->iso_qos.ucast.out.sdu && !conn->iso_qos.ucast.in.sdu)
  2900. hci_connect_cfm(conn, rp->status);
  2901. break;
  2902. /* Output (Controller to Host) */
  2903. case 0x01:
  2904. /* Confirm connection since conn->iso_qos is always configured
  2905. * last.
  2906. */
  2907. hci_connect_cfm(conn, rp->status);
  2908. /* Notify device connected in case it is a BIG Sync */
  2909. if (!rp->status && test_bit(HCI_CONN_BIG_SYNC, &conn->flags))
  2910. mgmt_device_connected(hdev, conn, NULL, 0);
  2911. break;
  2912. }
  2913. unlock:
  2914. hci_dev_unlock(hdev);
  2915. return rp->status;
  2916. }
  2917. static void hci_cs_le_create_big(struct hci_dev *hdev, u8 status)
  2918. {
  2919. bt_dev_dbg(hdev, "status 0x%2.2x", status);
  2920. }
  2921. static u8 hci_cc_set_per_adv_param(struct hci_dev *hdev, void *data,
  2922. struct sk_buff *skb)
  2923. {
  2924. struct hci_ev_status *rp = data;
  2925. struct hci_cp_le_set_per_adv_params *cp;
  2926. bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
  2927. if (rp->status)
  2928. return rp->status;
  2929. cp = hci_sent_cmd_data(hdev, HCI_OP_LE_SET_PER_ADV_PARAMS);
  2930. if (!cp)
  2931. return rp->status;
  2932. /* TODO: set the conn state */
  2933. return rp->status;
  2934. }
  2935. static u8 hci_cc_le_set_per_adv_enable(struct hci_dev *hdev, void *data,
  2936. struct sk_buff *skb)
  2937. {
  2938. struct hci_ev_status *rp = data;
  2939. struct hci_cp_le_set_per_adv_enable *cp;
  2940. struct adv_info *adv = NULL, *n;
  2941. u8 per_adv_cnt = 0;
  2942. bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
  2943. if (rp->status)
  2944. return rp->status;
  2945. cp = hci_sent_cmd_data(hdev, HCI_OP_LE_SET_PER_ADV_ENABLE);
  2946. if (!cp)
  2947. return rp->status;
  2948. hci_dev_lock(hdev);
  2949. adv = hci_find_adv_instance(hdev, cp->handle);
  2950. if (cp->enable) {
  2951. hci_dev_set_flag(hdev, HCI_LE_PER_ADV);
  2952. if (adv)
  2953. adv->enabled = true;
  2954. } else {
  2955. /* If just one instance was disabled check if there are
  2956. * any other instance enabled before clearing HCI_LE_PER_ADV.
  2957. * The current periodic adv instance will be marked as
  2958. * disabled once extended advertising is also disabled.
  2959. */
  2960. list_for_each_entry_safe(adv, n, &hdev->adv_instances,
  2961. list) {
  2962. if (adv->periodic && adv->enabled)
  2963. per_adv_cnt++;
  2964. }
  2965. if (per_adv_cnt > 1)
  2966. goto unlock;
  2967. hci_dev_clear_flag(hdev, HCI_LE_PER_ADV);
  2968. }
  2969. unlock:
  2970. hci_dev_unlock(hdev);
  2971. return rp->status;
  2972. }
  2973. #define HCI_CC_VL(_op, _func, _min, _max) \
  2974. { \
  2975. .op = _op, \
  2976. .func = _func, \
  2977. .min_len = _min, \
  2978. .max_len = _max, \
  2979. }
  2980. #define HCI_CC(_op, _func, _len) \
  2981. HCI_CC_VL(_op, _func, _len, _len)
  2982. #define HCI_CC_STATUS(_op, _func) \
  2983. HCI_CC(_op, _func, sizeof(struct hci_ev_status))
  2984. static const struct hci_cc {
  2985. u16 op;
  2986. u8 (*func)(struct hci_dev *hdev, void *data, struct sk_buff *skb);
  2987. u16 min_len;
  2988. u16 max_len;
  2989. } hci_cc_table[] = {
  2990. HCI_CC_STATUS(HCI_OP_INQUIRY_CANCEL, hci_cc_inquiry_cancel),
  2991. HCI_CC_STATUS(HCI_OP_PERIODIC_INQ, hci_cc_periodic_inq),
  2992. HCI_CC_STATUS(HCI_OP_EXIT_PERIODIC_INQ, hci_cc_exit_periodic_inq),
  2993. HCI_CC_STATUS(HCI_OP_REMOTE_NAME_REQ_CANCEL,
  2994. hci_cc_remote_name_req_cancel),
  2995. HCI_CC(HCI_OP_ROLE_DISCOVERY, hci_cc_role_discovery,
  2996. sizeof(struct hci_rp_role_discovery)),
  2997. HCI_CC(HCI_OP_READ_LINK_POLICY, hci_cc_read_link_policy,
  2998. sizeof(struct hci_rp_read_link_policy)),
  2999. HCI_CC(HCI_OP_WRITE_LINK_POLICY, hci_cc_write_link_policy,
  3000. sizeof(struct hci_rp_write_link_policy)),
  3001. HCI_CC(HCI_OP_READ_DEF_LINK_POLICY, hci_cc_read_def_link_policy,
  3002. sizeof(struct hci_rp_read_def_link_policy)),
  3003. HCI_CC_STATUS(HCI_OP_WRITE_DEF_LINK_POLICY,
  3004. hci_cc_write_def_link_policy),
  3005. HCI_CC_STATUS(HCI_OP_RESET, hci_cc_reset),
  3006. HCI_CC(HCI_OP_READ_STORED_LINK_KEY, hci_cc_read_stored_link_key,
  3007. sizeof(struct hci_rp_read_stored_link_key)),
  3008. HCI_CC(HCI_OP_DELETE_STORED_LINK_KEY, hci_cc_delete_stored_link_key,
  3009. sizeof(struct hci_rp_delete_stored_link_key)),
  3010. HCI_CC_STATUS(HCI_OP_WRITE_LOCAL_NAME, hci_cc_write_local_name),
  3011. HCI_CC(HCI_OP_READ_LOCAL_NAME, hci_cc_read_local_name,
  3012. sizeof(struct hci_rp_read_local_name)),
  3013. HCI_CC_STATUS(HCI_OP_WRITE_AUTH_ENABLE, hci_cc_write_auth_enable),
  3014. HCI_CC_STATUS(HCI_OP_WRITE_ENCRYPT_MODE, hci_cc_write_encrypt_mode),
  3015. HCI_CC_STATUS(HCI_OP_WRITE_SCAN_ENABLE, hci_cc_write_scan_enable),
  3016. HCI_CC_STATUS(HCI_OP_SET_EVENT_FLT, hci_cc_set_event_filter),
  3017. HCI_CC(HCI_OP_READ_CLASS_OF_DEV, hci_cc_read_class_of_dev,
  3018. sizeof(struct hci_rp_read_class_of_dev)),
  3019. HCI_CC_STATUS(HCI_OP_WRITE_CLASS_OF_DEV, hci_cc_write_class_of_dev),
  3020. HCI_CC(HCI_OP_READ_VOICE_SETTING, hci_cc_read_voice_setting,
  3021. sizeof(struct hci_rp_read_voice_setting)),
  3022. HCI_CC_STATUS(HCI_OP_WRITE_VOICE_SETTING, hci_cc_write_voice_setting),
  3023. HCI_CC(HCI_OP_READ_NUM_SUPPORTED_IAC, hci_cc_read_num_supported_iac,
  3024. sizeof(struct hci_rp_read_num_supported_iac)),
  3025. HCI_CC_STATUS(HCI_OP_WRITE_SSP_MODE, hci_cc_write_ssp_mode),
  3026. HCI_CC_STATUS(HCI_OP_WRITE_SC_SUPPORT, hci_cc_write_sc_support),
  3027. HCI_CC(HCI_OP_READ_AUTH_PAYLOAD_TO, hci_cc_read_auth_payload_timeout,
  3028. sizeof(struct hci_rp_read_auth_payload_to)),
  3029. HCI_CC(HCI_OP_WRITE_AUTH_PAYLOAD_TO, hci_cc_write_auth_payload_timeout,
  3030. sizeof(struct hci_rp_write_auth_payload_to)),
  3031. HCI_CC(HCI_OP_READ_LOCAL_VERSION, hci_cc_read_local_version,
  3032. sizeof(struct hci_rp_read_local_version)),
  3033. HCI_CC(HCI_OP_READ_LOCAL_COMMANDS, hci_cc_read_local_commands,
  3034. sizeof(struct hci_rp_read_local_commands)),
  3035. HCI_CC(HCI_OP_READ_LOCAL_FEATURES, hci_cc_read_local_features,
  3036. sizeof(struct hci_rp_read_local_features)),
  3037. HCI_CC(HCI_OP_READ_LOCAL_EXT_FEATURES, hci_cc_read_local_ext_features,
  3038. sizeof(struct hci_rp_read_local_ext_features)),
  3039. HCI_CC(HCI_OP_READ_BUFFER_SIZE, hci_cc_read_buffer_size,
  3040. sizeof(struct hci_rp_read_buffer_size)),
  3041. HCI_CC(HCI_OP_READ_BD_ADDR, hci_cc_read_bd_addr,
  3042. sizeof(struct hci_rp_read_bd_addr)),
  3043. HCI_CC(HCI_OP_READ_LOCAL_PAIRING_OPTS, hci_cc_read_local_pairing_opts,
  3044. sizeof(struct hci_rp_read_local_pairing_opts)),
  3045. HCI_CC(HCI_OP_READ_PAGE_SCAN_ACTIVITY, hci_cc_read_page_scan_activity,
  3046. sizeof(struct hci_rp_read_page_scan_activity)),
  3047. HCI_CC_STATUS(HCI_OP_WRITE_PAGE_SCAN_ACTIVITY,
  3048. hci_cc_write_page_scan_activity),
  3049. HCI_CC(HCI_OP_READ_PAGE_SCAN_TYPE, hci_cc_read_page_scan_type,
  3050. sizeof(struct hci_rp_read_page_scan_type)),
  3051. HCI_CC_STATUS(HCI_OP_WRITE_PAGE_SCAN_TYPE, hci_cc_write_page_scan_type),
  3052. HCI_CC(HCI_OP_READ_CLOCK, hci_cc_read_clock,
  3053. sizeof(struct hci_rp_read_clock)),
  3054. HCI_CC(HCI_OP_READ_ENC_KEY_SIZE, hci_cc_read_enc_key_size,
  3055. sizeof(struct hci_rp_read_enc_key_size)),
  3056. HCI_CC(HCI_OP_READ_INQ_RSP_TX_POWER, hci_cc_read_inq_rsp_tx_power,
  3057. sizeof(struct hci_rp_read_inq_rsp_tx_power)),
  3058. HCI_CC(HCI_OP_READ_DEF_ERR_DATA_REPORTING,
  3059. hci_cc_read_def_err_data_reporting,
  3060. sizeof(struct hci_rp_read_def_err_data_reporting)),
  3061. HCI_CC_STATUS(HCI_OP_WRITE_DEF_ERR_DATA_REPORTING,
  3062. hci_cc_write_def_err_data_reporting),
  3063. HCI_CC(HCI_OP_PIN_CODE_REPLY, hci_cc_pin_code_reply,
  3064. sizeof(struct hci_rp_pin_code_reply)),
  3065. HCI_CC(HCI_OP_PIN_CODE_NEG_REPLY, hci_cc_pin_code_neg_reply,
  3066. sizeof(struct hci_rp_pin_code_neg_reply)),
  3067. HCI_CC(HCI_OP_READ_LOCAL_OOB_DATA, hci_cc_read_local_oob_data,
  3068. sizeof(struct hci_rp_read_local_oob_data)),
  3069. HCI_CC(HCI_OP_READ_LOCAL_OOB_EXT_DATA, hci_cc_read_local_oob_ext_data,
  3070. sizeof(struct hci_rp_read_local_oob_ext_data)),
  3071. HCI_CC(HCI_OP_LE_READ_BUFFER_SIZE, hci_cc_le_read_buffer_size,
  3072. sizeof(struct hci_rp_le_read_buffer_size)),
  3073. HCI_CC(HCI_OP_LE_READ_LOCAL_FEATURES, hci_cc_le_read_local_features,
  3074. sizeof(struct hci_rp_le_read_local_features)),
  3075. HCI_CC(HCI_OP_LE_READ_ADV_TX_POWER, hci_cc_le_read_adv_tx_power,
  3076. sizeof(struct hci_rp_le_read_adv_tx_power)),
  3077. HCI_CC(HCI_OP_USER_CONFIRM_REPLY, hci_cc_user_confirm_reply,
  3078. sizeof(struct hci_rp_user_confirm_reply)),
  3079. HCI_CC(HCI_OP_USER_CONFIRM_NEG_REPLY, hci_cc_user_confirm_neg_reply,
  3080. sizeof(struct hci_rp_user_confirm_reply)),
  3081. HCI_CC(HCI_OP_USER_PASSKEY_REPLY, hci_cc_user_passkey_reply,
  3082. sizeof(struct hci_rp_user_confirm_reply)),
  3083. HCI_CC(HCI_OP_USER_PASSKEY_NEG_REPLY, hci_cc_user_passkey_neg_reply,
  3084. sizeof(struct hci_rp_user_confirm_reply)),
  3085. HCI_CC_STATUS(HCI_OP_LE_SET_RANDOM_ADDR, hci_cc_le_set_random_addr),
  3086. HCI_CC_STATUS(HCI_OP_LE_SET_ADV_ENABLE, hci_cc_le_set_adv_enable),
  3087. HCI_CC_STATUS(HCI_OP_LE_SET_SCAN_PARAM, hci_cc_le_set_scan_param),
  3088. HCI_CC_STATUS(HCI_OP_LE_SET_SCAN_ENABLE, hci_cc_le_set_scan_enable),
  3089. HCI_CC(HCI_OP_LE_READ_ACCEPT_LIST_SIZE,
  3090. hci_cc_le_read_accept_list_size,
  3091. sizeof(struct hci_rp_le_read_accept_list_size)),
  3092. HCI_CC_STATUS(HCI_OP_LE_CLEAR_ACCEPT_LIST, hci_cc_le_clear_accept_list),
  3093. HCI_CC_STATUS(HCI_OP_LE_ADD_TO_ACCEPT_LIST,
  3094. hci_cc_le_add_to_accept_list),
  3095. HCI_CC_STATUS(HCI_OP_LE_DEL_FROM_ACCEPT_LIST,
  3096. hci_cc_le_del_from_accept_list),
  3097. HCI_CC(HCI_OP_LE_READ_SUPPORTED_STATES, hci_cc_le_read_supported_states,
  3098. sizeof(struct hci_rp_le_read_supported_states)),
  3099. HCI_CC(HCI_OP_LE_READ_DEF_DATA_LEN, hci_cc_le_read_def_data_len,
  3100. sizeof(struct hci_rp_le_read_def_data_len)),
  3101. HCI_CC_STATUS(HCI_OP_LE_WRITE_DEF_DATA_LEN,
  3102. hci_cc_le_write_def_data_len),
  3103. HCI_CC_STATUS(HCI_OP_LE_ADD_TO_RESOLV_LIST,
  3104. hci_cc_le_add_to_resolv_list),
  3105. HCI_CC_STATUS(HCI_OP_LE_DEL_FROM_RESOLV_LIST,
  3106. hci_cc_le_del_from_resolv_list),
  3107. HCI_CC_STATUS(HCI_OP_LE_CLEAR_RESOLV_LIST,
  3108. hci_cc_le_clear_resolv_list),
  3109. HCI_CC(HCI_OP_LE_READ_RESOLV_LIST_SIZE, hci_cc_le_read_resolv_list_size,
  3110. sizeof(struct hci_rp_le_read_resolv_list_size)),
  3111. HCI_CC_STATUS(HCI_OP_LE_SET_ADDR_RESOLV_ENABLE,
  3112. hci_cc_le_set_addr_resolution_enable),
  3113. HCI_CC(HCI_OP_LE_READ_MAX_DATA_LEN, hci_cc_le_read_max_data_len,
  3114. sizeof(struct hci_rp_le_read_max_data_len)),
  3115. HCI_CC_STATUS(HCI_OP_WRITE_LE_HOST_SUPPORTED,
  3116. hci_cc_write_le_host_supported),
  3117. HCI_CC_STATUS(HCI_OP_LE_SET_ADV_PARAM, hci_cc_set_adv_param),
  3118. HCI_CC(HCI_OP_READ_RSSI, hci_cc_read_rssi,
  3119. sizeof(struct hci_rp_read_rssi)),
  3120. HCI_CC(HCI_OP_READ_TX_POWER, hci_cc_read_tx_power,
  3121. sizeof(struct hci_rp_read_tx_power)),
  3122. HCI_CC_STATUS(HCI_OP_WRITE_SSP_DEBUG_MODE, hci_cc_write_ssp_debug_mode),
  3123. HCI_CC_STATUS(HCI_OP_LE_SET_EXT_SCAN_PARAMS,
  3124. hci_cc_le_set_ext_scan_param),
  3125. HCI_CC_STATUS(HCI_OP_LE_SET_EXT_SCAN_ENABLE,
  3126. hci_cc_le_set_ext_scan_enable),
  3127. HCI_CC_STATUS(HCI_OP_LE_SET_DEFAULT_PHY, hci_cc_le_set_default_phy),
  3128. HCI_CC(HCI_OP_LE_READ_NUM_SUPPORTED_ADV_SETS,
  3129. hci_cc_le_read_num_adv_sets,
  3130. sizeof(struct hci_rp_le_read_num_supported_adv_sets)),
  3131. HCI_CC_STATUS(HCI_OP_LE_SET_EXT_ADV_ENABLE,
  3132. hci_cc_le_set_ext_adv_enable),
  3133. HCI_CC_STATUS(HCI_OP_LE_SET_ADV_SET_RAND_ADDR,
  3134. hci_cc_le_set_adv_set_random_addr),
  3135. HCI_CC_STATUS(HCI_OP_LE_REMOVE_ADV_SET, hci_cc_le_remove_adv_set),
  3136. HCI_CC_STATUS(HCI_OP_LE_CLEAR_ADV_SETS, hci_cc_le_clear_adv_sets),
  3137. HCI_CC_STATUS(HCI_OP_LE_SET_PER_ADV_PARAMS, hci_cc_set_per_adv_param),
  3138. HCI_CC_STATUS(HCI_OP_LE_SET_PER_ADV_ENABLE,
  3139. hci_cc_le_set_per_adv_enable),
  3140. HCI_CC(HCI_OP_LE_READ_TRANSMIT_POWER, hci_cc_le_read_transmit_power,
  3141. sizeof(struct hci_rp_le_read_transmit_power)),
  3142. HCI_CC_STATUS(HCI_OP_LE_SET_PRIVACY_MODE, hci_cc_le_set_privacy_mode),
  3143. HCI_CC(HCI_OP_LE_READ_BUFFER_SIZE_V2, hci_cc_le_read_buffer_size_v2,
  3144. sizeof(struct hci_rp_le_read_buffer_size_v2)),
  3145. HCI_CC_VL(HCI_OP_LE_SET_CIG_PARAMS, hci_cc_le_set_cig_params,
  3146. sizeof(struct hci_rp_le_set_cig_params), HCI_MAX_EVENT_SIZE),
  3147. HCI_CC(HCI_OP_LE_SETUP_ISO_PATH, hci_cc_le_setup_iso_path,
  3148. sizeof(struct hci_rp_le_setup_iso_path)),
  3149. };
  3150. static u8 hci_cc_func(struct hci_dev *hdev, const struct hci_cc *cc,
  3151. struct sk_buff *skb)
  3152. {
  3153. void *data;
  3154. if (skb->len < cc->min_len) {
  3155. bt_dev_err(hdev, "unexpected cc 0x%4.4x length: %u < %u",
  3156. cc->op, skb->len, cc->min_len);
  3157. return HCI_ERROR_UNSPECIFIED;
  3158. }
  3159. /* Just warn if the length is over max_len size it still be possible to
  3160. * partially parse the cc so leave to callback to decide if that is
  3161. * acceptable.
  3162. */
  3163. if (skb->len > cc->max_len)
  3164. bt_dev_warn(hdev, "unexpected cc 0x%4.4x length: %u > %u",
  3165. cc->op, skb->len, cc->max_len);
  3166. data = hci_cc_skb_pull(hdev, skb, cc->op, cc->min_len);
  3167. if (!data)
  3168. return HCI_ERROR_UNSPECIFIED;
  3169. return cc->func(hdev, data, skb);
  3170. }
  3171. static void hci_cmd_complete_evt(struct hci_dev *hdev, void *data,
  3172. struct sk_buff *skb, u16 *opcode, u8 *status,
  3173. hci_req_complete_t *req_complete,
  3174. hci_req_complete_skb_t *req_complete_skb)
  3175. {
  3176. struct hci_ev_cmd_complete *ev = data;
  3177. int i;
  3178. *opcode = __le16_to_cpu(ev->opcode);
  3179. bt_dev_dbg(hdev, "opcode 0x%4.4x", *opcode);
  3180. for (i = 0; i < ARRAY_SIZE(hci_cc_table); i++) {
  3181. if (hci_cc_table[i].op == *opcode) {
  3182. *status = hci_cc_func(hdev, &hci_cc_table[i], skb);
  3183. break;
  3184. }
  3185. }
  3186. if (i == ARRAY_SIZE(hci_cc_table)) {
  3187. /* Unknown opcode, assume byte 0 contains the status, so
  3188. * that e.g. __hci_cmd_sync() properly returns errors
  3189. * for vendor specific commands send by HCI drivers.
  3190. * If a vendor doesn't actually follow this convention we may
  3191. * need to introduce a vendor CC table in order to properly set
  3192. * the status.
  3193. */
  3194. *status = skb->data[0];
  3195. }
  3196. handle_cmd_cnt_and_timer(hdev, ev->ncmd);
  3197. hci_req_cmd_complete(hdev, *opcode, *status, req_complete,
  3198. req_complete_skb);
  3199. if (hci_dev_test_flag(hdev, HCI_CMD_PENDING)) {
  3200. bt_dev_err(hdev,
  3201. "unexpected event for opcode 0x%4.4x", *opcode);
  3202. return;
  3203. }
  3204. if (atomic_read(&hdev->cmd_cnt) && !skb_queue_empty(&hdev->cmd_q))
  3205. queue_work(hdev->workqueue, &hdev->cmd_work);
  3206. }
  3207. static void hci_cs_le_create_cis(struct hci_dev *hdev, u8 status)
  3208. {
  3209. struct hci_cp_le_create_cis *cp;
  3210. bool pending = false;
  3211. int i;
  3212. bt_dev_dbg(hdev, "status 0x%2.2x", status);
  3213. if (!status)
  3214. return;
  3215. cp = hci_sent_cmd_data(hdev, HCI_OP_LE_CREATE_CIS);
  3216. if (!cp)
  3217. return;
  3218. hci_dev_lock(hdev);
  3219. /* Remove connection if command failed */
  3220. for (i = 0; i < cp->num_cis; i++) {
  3221. struct hci_conn *conn;
  3222. u16 handle;
  3223. handle = __le16_to_cpu(cp->cis[i].cis_handle);
  3224. conn = hci_conn_hash_lookup_handle(hdev, handle);
  3225. if (conn) {
  3226. if (test_and_clear_bit(HCI_CONN_CREATE_CIS,
  3227. &conn->flags))
  3228. pending = true;
  3229. conn->state = BT_CLOSED;
  3230. hci_connect_cfm(conn, status);
  3231. hci_conn_del(conn);
  3232. }
  3233. }
  3234. cp->num_cis = 0;
  3235. if (pending)
  3236. hci_le_create_cis_pending(hdev);
  3237. hci_dev_unlock(hdev);
  3238. }
  3239. #define HCI_CS(_op, _func) \
  3240. { \
  3241. .op = _op, \
  3242. .func = _func, \
  3243. }
  3244. static const struct hci_cs {
  3245. u16 op;
  3246. void (*func)(struct hci_dev *hdev, __u8 status);
  3247. } hci_cs_table[] = {
  3248. HCI_CS(HCI_OP_INQUIRY, hci_cs_inquiry),
  3249. HCI_CS(HCI_OP_CREATE_CONN, hci_cs_create_conn),
  3250. HCI_CS(HCI_OP_DISCONNECT, hci_cs_disconnect),
  3251. HCI_CS(HCI_OP_ADD_SCO, hci_cs_add_sco),
  3252. HCI_CS(HCI_OP_AUTH_REQUESTED, hci_cs_auth_requested),
  3253. HCI_CS(HCI_OP_SET_CONN_ENCRYPT, hci_cs_set_conn_encrypt),
  3254. HCI_CS(HCI_OP_REMOTE_NAME_REQ, hci_cs_remote_name_req),
  3255. HCI_CS(HCI_OP_READ_REMOTE_FEATURES, hci_cs_read_remote_features),
  3256. HCI_CS(HCI_OP_READ_REMOTE_EXT_FEATURES,
  3257. hci_cs_read_remote_ext_features),
  3258. HCI_CS(HCI_OP_SETUP_SYNC_CONN, hci_cs_setup_sync_conn),
  3259. HCI_CS(HCI_OP_ENHANCED_SETUP_SYNC_CONN,
  3260. hci_cs_enhanced_setup_sync_conn),
  3261. HCI_CS(HCI_OP_SNIFF_MODE, hci_cs_sniff_mode),
  3262. HCI_CS(HCI_OP_EXIT_SNIFF_MODE, hci_cs_exit_sniff_mode),
  3263. HCI_CS(HCI_OP_SWITCH_ROLE, hci_cs_switch_role),
  3264. HCI_CS(HCI_OP_LE_CREATE_CONN, hci_cs_le_create_conn),
  3265. HCI_CS(HCI_OP_LE_READ_REMOTE_FEATURES, hci_cs_le_read_remote_features),
  3266. HCI_CS(HCI_OP_LE_START_ENC, hci_cs_le_start_enc),
  3267. HCI_CS(HCI_OP_LE_EXT_CREATE_CONN, hci_cs_le_ext_create_conn),
  3268. HCI_CS(HCI_OP_LE_CREATE_CIS, hci_cs_le_create_cis),
  3269. HCI_CS(HCI_OP_LE_CREATE_BIG, hci_cs_le_create_big),
  3270. };
  3271. static void hci_cmd_status_evt(struct hci_dev *hdev, void *data,
  3272. struct sk_buff *skb, u16 *opcode, u8 *status,
  3273. hci_req_complete_t *req_complete,
  3274. hci_req_complete_skb_t *req_complete_skb)
  3275. {
  3276. struct hci_ev_cmd_status *ev = data;
  3277. int i;
  3278. *opcode = __le16_to_cpu(ev->opcode);
  3279. *status = ev->status;
  3280. bt_dev_dbg(hdev, "opcode 0x%4.4x", *opcode);
  3281. for (i = 0; i < ARRAY_SIZE(hci_cs_table); i++) {
  3282. if (hci_cs_table[i].op == *opcode) {
  3283. hci_cs_table[i].func(hdev, ev->status);
  3284. break;
  3285. }
  3286. }
  3287. handle_cmd_cnt_and_timer(hdev, ev->ncmd);
  3288. /* Indicate request completion if the command failed. Also, if
  3289. * we're not waiting for a special event and we get a success
  3290. * command status we should try to flag the request as completed
  3291. * (since for this kind of commands there will not be a command
  3292. * complete event).
  3293. */
  3294. if (ev->status || (hdev->req_skb && !hci_skb_event(hdev->req_skb))) {
  3295. hci_req_cmd_complete(hdev, *opcode, ev->status, req_complete,
  3296. req_complete_skb);
  3297. if (hci_dev_test_flag(hdev, HCI_CMD_PENDING)) {
  3298. bt_dev_err(hdev, "unexpected event for opcode 0x%4.4x",
  3299. *opcode);
  3300. return;
  3301. }
  3302. }
  3303. if (atomic_read(&hdev->cmd_cnt) && !skb_queue_empty(&hdev->cmd_q))
  3304. queue_work(hdev->workqueue, &hdev->cmd_work);
  3305. }
  3306. static void hci_hardware_error_evt(struct hci_dev *hdev, void *data,
  3307. struct sk_buff *skb)
  3308. {
  3309. struct hci_ev_hardware_error *ev = data;
  3310. bt_dev_dbg(hdev, "code 0x%2.2x", ev->code);
  3311. hdev->hw_error_code = ev->code;
  3312. queue_work(hdev->req_workqueue, &hdev->error_reset);
  3313. }
  3314. static void hci_role_change_evt(struct hci_dev *hdev, void *data,
  3315. struct sk_buff *skb)
  3316. {
  3317. struct hci_ev_role_change *ev = data;
  3318. struct hci_conn *conn;
  3319. bt_dev_dbg(hdev, "status 0x%2.2x", ev->status);
  3320. hci_dev_lock(hdev);
  3321. conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &ev->bdaddr);
  3322. if (conn) {
  3323. if (!ev->status)
  3324. conn->role = ev->role;
  3325. clear_bit(HCI_CONN_RSWITCH_PEND, &conn->flags);
  3326. hci_role_switch_cfm(conn, ev->status, ev->role);
  3327. }
  3328. hci_dev_unlock(hdev);
  3329. }
  3330. static void hci_num_comp_pkts_evt(struct hci_dev *hdev, void *data,
  3331. struct sk_buff *skb)
  3332. {
  3333. struct hci_ev_num_comp_pkts *ev = data;
  3334. int i;
  3335. if (!hci_ev_skb_pull(hdev, skb, HCI_EV_NUM_COMP_PKTS,
  3336. flex_array_size(ev, handles, ev->num)))
  3337. return;
  3338. bt_dev_dbg(hdev, "num %d", ev->num);
  3339. for (i = 0; i < ev->num; i++) {
  3340. struct hci_comp_pkts_info *info = &ev->handles[i];
  3341. struct hci_conn *conn;
  3342. __u16 handle, count;
  3343. handle = __le16_to_cpu(info->handle);
  3344. count = __le16_to_cpu(info->count);
  3345. conn = hci_conn_hash_lookup_handle(hdev, handle);
  3346. if (!conn)
  3347. continue;
  3348. /* Check if there is really enough packets outstanding before
  3349. * attempting to decrease the sent counter otherwise it could
  3350. * underflow..
  3351. */
  3352. if (conn->sent >= count) {
  3353. conn->sent -= count;
  3354. } else {
  3355. bt_dev_warn(hdev, "hcon %p sent %u < count %u",
  3356. conn, conn->sent, count);
  3357. conn->sent = 0;
  3358. }
  3359. switch (conn->type) {
  3360. case ACL_LINK:
  3361. hdev->acl_cnt += count;
  3362. if (hdev->acl_cnt > hdev->acl_pkts)
  3363. hdev->acl_cnt = hdev->acl_pkts;
  3364. break;
  3365. case LE_LINK:
  3366. if (hdev->le_pkts) {
  3367. hdev->le_cnt += count;
  3368. if (hdev->le_cnt > hdev->le_pkts)
  3369. hdev->le_cnt = hdev->le_pkts;
  3370. } else {
  3371. hdev->acl_cnt += count;
  3372. if (hdev->acl_cnt > hdev->acl_pkts)
  3373. hdev->acl_cnt = hdev->acl_pkts;
  3374. }
  3375. break;
  3376. case SCO_LINK:
  3377. hdev->sco_cnt += count;
  3378. if (hdev->sco_cnt > hdev->sco_pkts)
  3379. hdev->sco_cnt = hdev->sco_pkts;
  3380. break;
  3381. case ISO_LINK:
  3382. if (hdev->iso_pkts) {
  3383. hdev->iso_cnt += count;
  3384. if (hdev->iso_cnt > hdev->iso_pkts)
  3385. hdev->iso_cnt = hdev->iso_pkts;
  3386. } else if (hdev->le_pkts) {
  3387. hdev->le_cnt += count;
  3388. if (hdev->le_cnt > hdev->le_pkts)
  3389. hdev->le_cnt = hdev->le_pkts;
  3390. } else {
  3391. hdev->acl_cnt += count;
  3392. if (hdev->acl_cnt > hdev->acl_pkts)
  3393. hdev->acl_cnt = hdev->acl_pkts;
  3394. }
  3395. break;
  3396. default:
  3397. bt_dev_err(hdev, "unknown type %d conn %p",
  3398. conn->type, conn);
  3399. break;
  3400. }
  3401. }
  3402. queue_work(hdev->workqueue, &hdev->tx_work);
  3403. }
  3404. static void hci_mode_change_evt(struct hci_dev *hdev, void *data,
  3405. struct sk_buff *skb)
  3406. {
  3407. struct hci_ev_mode_change *ev = data;
  3408. struct hci_conn *conn;
  3409. bt_dev_dbg(hdev, "status 0x%2.2x", ev->status);
  3410. hci_dev_lock(hdev);
  3411. conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
  3412. if (conn) {
  3413. conn->mode = ev->mode;
  3414. if (!test_and_clear_bit(HCI_CONN_MODE_CHANGE_PEND,
  3415. &conn->flags)) {
  3416. if (conn->mode == HCI_CM_ACTIVE)
  3417. set_bit(HCI_CONN_POWER_SAVE, &conn->flags);
  3418. else
  3419. clear_bit(HCI_CONN_POWER_SAVE, &conn->flags);
  3420. }
  3421. if (test_and_clear_bit(HCI_CONN_SCO_SETUP_PEND, &conn->flags))
  3422. hci_sco_setup(conn, ev->status);
  3423. }
  3424. hci_dev_unlock(hdev);
  3425. }
  3426. static void hci_pin_code_request_evt(struct hci_dev *hdev, void *data,
  3427. struct sk_buff *skb)
  3428. {
  3429. struct hci_ev_pin_code_req *ev = data;
  3430. struct hci_conn *conn;
  3431. bt_dev_dbg(hdev, "");
  3432. hci_dev_lock(hdev);
  3433. conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &ev->bdaddr);
  3434. if (!conn)
  3435. goto unlock;
  3436. if (conn->state == BT_CONNECTED) {
  3437. hci_conn_hold(conn);
  3438. conn->disc_timeout = HCI_PAIRING_TIMEOUT;
  3439. hci_conn_drop(conn);
  3440. }
  3441. if (!hci_dev_test_flag(hdev, HCI_BONDABLE) &&
  3442. !test_bit(HCI_CONN_AUTH_INITIATOR, &conn->flags)) {
  3443. hci_send_cmd(hdev, HCI_OP_PIN_CODE_NEG_REPLY,
  3444. sizeof(ev->bdaddr), &ev->bdaddr);
  3445. } else if (hci_dev_test_flag(hdev, HCI_MGMT)) {
  3446. u8 secure;
  3447. if (conn->pending_sec_level == BT_SECURITY_HIGH)
  3448. secure = 1;
  3449. else
  3450. secure = 0;
  3451. mgmt_pin_code_request(hdev, &ev->bdaddr, secure);
  3452. }
  3453. unlock:
  3454. hci_dev_unlock(hdev);
  3455. }
  3456. static void conn_set_key(struct hci_conn *conn, u8 key_type, u8 pin_len)
  3457. {
  3458. if (key_type == HCI_LK_CHANGED_COMBINATION)
  3459. return;
  3460. conn->pin_length = pin_len;
  3461. conn->key_type = key_type;
  3462. switch (key_type) {
  3463. case HCI_LK_LOCAL_UNIT:
  3464. case HCI_LK_REMOTE_UNIT:
  3465. case HCI_LK_DEBUG_COMBINATION:
  3466. return;
  3467. case HCI_LK_COMBINATION:
  3468. if (pin_len == 16)
  3469. conn->pending_sec_level = BT_SECURITY_HIGH;
  3470. else
  3471. conn->pending_sec_level = BT_SECURITY_MEDIUM;
  3472. break;
  3473. case HCI_LK_UNAUTH_COMBINATION_P192:
  3474. case HCI_LK_UNAUTH_COMBINATION_P256:
  3475. conn->pending_sec_level = BT_SECURITY_MEDIUM;
  3476. break;
  3477. case HCI_LK_AUTH_COMBINATION_P192:
  3478. conn->pending_sec_level = BT_SECURITY_HIGH;
  3479. break;
  3480. case HCI_LK_AUTH_COMBINATION_P256:
  3481. conn->pending_sec_level = BT_SECURITY_FIPS;
  3482. break;
  3483. }
  3484. }
  3485. static void hci_link_key_request_evt(struct hci_dev *hdev, void *data,
  3486. struct sk_buff *skb)
  3487. {
  3488. struct hci_ev_link_key_req *ev = data;
  3489. struct hci_cp_link_key_reply cp;
  3490. struct hci_conn *conn;
  3491. struct link_key *key;
  3492. bt_dev_dbg(hdev, "");
  3493. if (!hci_dev_test_flag(hdev, HCI_MGMT))
  3494. return;
  3495. hci_dev_lock(hdev);
  3496. key = hci_find_link_key(hdev, &ev->bdaddr);
  3497. if (!key) {
  3498. bt_dev_dbg(hdev, "link key not found for %pMR", &ev->bdaddr);
  3499. goto not_found;
  3500. }
  3501. bt_dev_dbg(hdev, "found key type %u for %pMR", key->type, &ev->bdaddr);
  3502. conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &ev->bdaddr);
  3503. if (conn) {
  3504. clear_bit(HCI_CONN_NEW_LINK_KEY, &conn->flags);
  3505. if ((key->type == HCI_LK_UNAUTH_COMBINATION_P192 ||
  3506. key->type == HCI_LK_UNAUTH_COMBINATION_P256) &&
  3507. conn->auth_type != 0xff && (conn->auth_type & 0x01)) {
  3508. bt_dev_dbg(hdev, "ignoring unauthenticated key");
  3509. goto not_found;
  3510. }
  3511. if (key->type == HCI_LK_COMBINATION && key->pin_len < 16 &&
  3512. (conn->pending_sec_level == BT_SECURITY_HIGH ||
  3513. conn->pending_sec_level == BT_SECURITY_FIPS)) {
  3514. bt_dev_dbg(hdev, "ignoring key unauthenticated for high security");
  3515. goto not_found;
  3516. }
  3517. conn_set_key(conn, key->type, key->pin_len);
  3518. }
  3519. bacpy(&cp.bdaddr, &ev->bdaddr);
  3520. memcpy(cp.link_key, key->val, HCI_LINK_KEY_SIZE);
  3521. hci_send_cmd(hdev, HCI_OP_LINK_KEY_REPLY, sizeof(cp), &cp);
  3522. hci_dev_unlock(hdev);
  3523. return;
  3524. not_found:
  3525. hci_send_cmd(hdev, HCI_OP_LINK_KEY_NEG_REPLY, 6, &ev->bdaddr);
  3526. hci_dev_unlock(hdev);
  3527. }
  3528. static void hci_link_key_notify_evt(struct hci_dev *hdev, void *data,
  3529. struct sk_buff *skb)
  3530. {
  3531. struct hci_ev_link_key_notify *ev = data;
  3532. struct hci_conn *conn;
  3533. struct link_key *key;
  3534. bool persistent;
  3535. u8 pin_len = 0;
  3536. bt_dev_dbg(hdev, "");
  3537. hci_dev_lock(hdev);
  3538. conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &ev->bdaddr);
  3539. if (!conn)
  3540. goto unlock;
  3541. /* Ignore NULL link key against CVE-2020-26555 */
  3542. if (!crypto_memneq(ev->link_key, ZERO_KEY, HCI_LINK_KEY_SIZE)) {
  3543. bt_dev_dbg(hdev, "Ignore NULL link key (ZERO KEY) for %pMR",
  3544. &ev->bdaddr);
  3545. hci_disconnect(conn, HCI_ERROR_AUTH_FAILURE);
  3546. hci_conn_drop(conn);
  3547. goto unlock;
  3548. }
  3549. hci_conn_hold(conn);
  3550. conn->disc_timeout = HCI_DISCONN_TIMEOUT;
  3551. hci_conn_drop(conn);
  3552. set_bit(HCI_CONN_NEW_LINK_KEY, &conn->flags);
  3553. conn_set_key(conn, ev->key_type, conn->pin_length);
  3554. if (!hci_dev_test_flag(hdev, HCI_MGMT))
  3555. goto unlock;
  3556. key = hci_add_link_key(hdev, conn, &ev->bdaddr, ev->link_key,
  3557. ev->key_type, pin_len, &persistent);
  3558. if (!key)
  3559. goto unlock;
  3560. /* Update connection information since adding the key will have
  3561. * fixed up the type in the case of changed combination keys.
  3562. */
  3563. if (ev->key_type == HCI_LK_CHANGED_COMBINATION)
  3564. conn_set_key(conn, key->type, key->pin_len);
  3565. mgmt_new_link_key(hdev, key, persistent);
  3566. /* Keep debug keys around only if the HCI_KEEP_DEBUG_KEYS flag
  3567. * is set. If it's not set simply remove the key from the kernel
  3568. * list (we've still notified user space about it but with
  3569. * store_hint being 0).
  3570. */
  3571. if (key->type == HCI_LK_DEBUG_COMBINATION &&
  3572. !hci_dev_test_flag(hdev, HCI_KEEP_DEBUG_KEYS)) {
  3573. list_del_rcu(&key->list);
  3574. kfree_rcu(key, rcu);
  3575. goto unlock;
  3576. }
  3577. if (persistent)
  3578. clear_bit(HCI_CONN_FLUSH_KEY, &conn->flags);
  3579. else
  3580. set_bit(HCI_CONN_FLUSH_KEY, &conn->flags);
  3581. unlock:
  3582. hci_dev_unlock(hdev);
  3583. }
  3584. static void hci_clock_offset_evt(struct hci_dev *hdev, void *data,
  3585. struct sk_buff *skb)
  3586. {
  3587. struct hci_ev_clock_offset *ev = data;
  3588. struct hci_conn *conn;
  3589. bt_dev_dbg(hdev, "status 0x%2.2x", ev->status);
  3590. hci_dev_lock(hdev);
  3591. conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
  3592. if (conn && !ev->status) {
  3593. struct inquiry_entry *ie;
  3594. ie = hci_inquiry_cache_lookup(hdev, &conn->dst);
  3595. if (ie) {
  3596. ie->data.clock_offset = ev->clock_offset;
  3597. ie->timestamp = jiffies;
  3598. }
  3599. }
  3600. hci_dev_unlock(hdev);
  3601. }
  3602. static void hci_pkt_type_change_evt(struct hci_dev *hdev, void *data,
  3603. struct sk_buff *skb)
  3604. {
  3605. struct hci_ev_pkt_type_change *ev = data;
  3606. struct hci_conn *conn;
  3607. bt_dev_dbg(hdev, "status 0x%2.2x", ev->status);
  3608. hci_dev_lock(hdev);
  3609. conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
  3610. if (conn && !ev->status)
  3611. conn->pkt_type = __le16_to_cpu(ev->pkt_type);
  3612. hci_dev_unlock(hdev);
  3613. }
  3614. static void hci_pscan_rep_mode_evt(struct hci_dev *hdev, void *data,
  3615. struct sk_buff *skb)
  3616. {
  3617. struct hci_ev_pscan_rep_mode *ev = data;
  3618. struct inquiry_entry *ie;
  3619. bt_dev_dbg(hdev, "");
  3620. hci_dev_lock(hdev);
  3621. ie = hci_inquiry_cache_lookup(hdev, &ev->bdaddr);
  3622. if (ie) {
  3623. ie->data.pscan_rep_mode = ev->pscan_rep_mode;
  3624. ie->timestamp = jiffies;
  3625. }
  3626. hci_dev_unlock(hdev);
  3627. }
  3628. static void hci_inquiry_result_with_rssi_evt(struct hci_dev *hdev, void *edata,
  3629. struct sk_buff *skb)
  3630. {
  3631. struct hci_ev_inquiry_result_rssi *ev = edata;
  3632. struct inquiry_data data;
  3633. int i;
  3634. bt_dev_dbg(hdev, "num_rsp %d", ev->num);
  3635. if (!ev->num)
  3636. return;
  3637. if (hci_dev_test_flag(hdev, HCI_PERIODIC_INQ))
  3638. return;
  3639. hci_dev_lock(hdev);
  3640. if (skb->len == array_size(ev->num,
  3641. sizeof(struct inquiry_info_rssi_pscan))) {
  3642. struct inquiry_info_rssi_pscan *info;
  3643. for (i = 0; i < ev->num; i++) {
  3644. u32 flags;
  3645. info = hci_ev_skb_pull(hdev, skb,
  3646. HCI_EV_INQUIRY_RESULT_WITH_RSSI,
  3647. sizeof(*info));
  3648. if (!info) {
  3649. bt_dev_err(hdev, "Malformed HCI Event: 0x%2.2x",
  3650. HCI_EV_INQUIRY_RESULT_WITH_RSSI);
  3651. goto unlock;
  3652. }
  3653. bacpy(&data.bdaddr, &info->bdaddr);
  3654. data.pscan_rep_mode = info->pscan_rep_mode;
  3655. data.pscan_period_mode = info->pscan_period_mode;
  3656. data.pscan_mode = info->pscan_mode;
  3657. memcpy(data.dev_class, info->dev_class, 3);
  3658. data.clock_offset = info->clock_offset;
  3659. data.rssi = info->rssi;
  3660. data.ssp_mode = 0x00;
  3661. flags = hci_inquiry_cache_update(hdev, &data, false);
  3662. mgmt_device_found(hdev, &info->bdaddr, ACL_LINK, 0x00,
  3663. info->dev_class, info->rssi,
  3664. flags, NULL, 0, NULL, 0, 0);
  3665. }
  3666. } else if (skb->len == array_size(ev->num,
  3667. sizeof(struct inquiry_info_rssi))) {
  3668. struct inquiry_info_rssi *info;
  3669. for (i = 0; i < ev->num; i++) {
  3670. u32 flags;
  3671. info = hci_ev_skb_pull(hdev, skb,
  3672. HCI_EV_INQUIRY_RESULT_WITH_RSSI,
  3673. sizeof(*info));
  3674. if (!info) {
  3675. bt_dev_err(hdev, "Malformed HCI Event: 0x%2.2x",
  3676. HCI_EV_INQUIRY_RESULT_WITH_RSSI);
  3677. goto unlock;
  3678. }
  3679. bacpy(&data.bdaddr, &info->bdaddr);
  3680. data.pscan_rep_mode = info->pscan_rep_mode;
  3681. data.pscan_period_mode = info->pscan_period_mode;
  3682. data.pscan_mode = 0x00;
  3683. memcpy(data.dev_class, info->dev_class, 3);
  3684. data.clock_offset = info->clock_offset;
  3685. data.rssi = info->rssi;
  3686. data.ssp_mode = 0x00;
  3687. flags = hci_inquiry_cache_update(hdev, &data, false);
  3688. mgmt_device_found(hdev, &info->bdaddr, ACL_LINK, 0x00,
  3689. info->dev_class, info->rssi,
  3690. flags, NULL, 0, NULL, 0, 0);
  3691. }
  3692. } else {
  3693. bt_dev_err(hdev, "Malformed HCI Event: 0x%2.2x",
  3694. HCI_EV_INQUIRY_RESULT_WITH_RSSI);
  3695. }
  3696. unlock:
  3697. hci_dev_unlock(hdev);
  3698. }
  3699. static void hci_remote_ext_features_evt(struct hci_dev *hdev, void *data,
  3700. struct sk_buff *skb)
  3701. {
  3702. struct hci_ev_remote_ext_features *ev = data;
  3703. struct hci_conn *conn;
  3704. bt_dev_dbg(hdev, "status 0x%2.2x", ev->status);
  3705. hci_dev_lock(hdev);
  3706. conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
  3707. if (!conn)
  3708. goto unlock;
  3709. if (ev->page < HCI_MAX_PAGES)
  3710. memcpy(conn->features[ev->page], ev->features, 8);
  3711. if (!ev->status && ev->page == 0x01) {
  3712. struct inquiry_entry *ie;
  3713. ie = hci_inquiry_cache_lookup(hdev, &conn->dst);
  3714. if (ie)
  3715. ie->data.ssp_mode = (ev->features[0] & LMP_HOST_SSP);
  3716. if (ev->features[0] & LMP_HOST_SSP) {
  3717. set_bit(HCI_CONN_SSP_ENABLED, &conn->flags);
  3718. } else {
  3719. /* It is mandatory by the Bluetooth specification that
  3720. * Extended Inquiry Results are only used when Secure
  3721. * Simple Pairing is enabled, but some devices violate
  3722. * this.
  3723. *
  3724. * To make these devices work, the internal SSP
  3725. * enabled flag needs to be cleared if the remote host
  3726. * features do not indicate SSP support */
  3727. clear_bit(HCI_CONN_SSP_ENABLED, &conn->flags);
  3728. }
  3729. if (ev->features[0] & LMP_HOST_SC)
  3730. set_bit(HCI_CONN_SC_ENABLED, &conn->flags);
  3731. }
  3732. if (conn->state != BT_CONFIG)
  3733. goto unlock;
  3734. if (!ev->status && !test_bit(HCI_CONN_MGMT_CONNECTED, &conn->flags)) {
  3735. struct hci_cp_remote_name_req cp;
  3736. memset(&cp, 0, sizeof(cp));
  3737. bacpy(&cp.bdaddr, &conn->dst);
  3738. cp.pscan_rep_mode = 0x02;
  3739. hci_send_cmd(hdev, HCI_OP_REMOTE_NAME_REQ, sizeof(cp), &cp);
  3740. } else {
  3741. mgmt_device_connected(hdev, conn, NULL, 0);
  3742. }
  3743. if (!hci_outgoing_auth_needed(hdev, conn)) {
  3744. conn->state = BT_CONNECTED;
  3745. hci_connect_cfm(conn, ev->status);
  3746. hci_conn_drop(conn);
  3747. }
  3748. unlock:
  3749. hci_dev_unlock(hdev);
  3750. }
  3751. static void hci_sync_conn_complete_evt(struct hci_dev *hdev, void *data,
  3752. struct sk_buff *skb)
  3753. {
  3754. struct hci_ev_sync_conn_complete *ev = data;
  3755. struct hci_conn *conn;
  3756. u8 status = ev->status;
  3757. switch (ev->link_type) {
  3758. case SCO_LINK:
  3759. case ESCO_LINK:
  3760. break;
  3761. default:
  3762. /* As per Core 5.3 Vol 4 Part E 7.7.35 (p.2219), Link_Type
  3763. * for HCI_Synchronous_Connection_Complete is limited to
  3764. * either SCO or eSCO
  3765. */
  3766. bt_dev_err(hdev, "Ignoring connect complete event for invalid link type");
  3767. return;
  3768. }
  3769. bt_dev_dbg(hdev, "status 0x%2.2x", status);
  3770. hci_dev_lock(hdev);
  3771. conn = hci_conn_hash_lookup_ba(hdev, ev->link_type, &ev->bdaddr);
  3772. if (!conn) {
  3773. if (ev->link_type == ESCO_LINK)
  3774. goto unlock;
  3775. /* When the link type in the event indicates SCO connection
  3776. * and lookup of the connection object fails, then check
  3777. * if an eSCO connection object exists.
  3778. *
  3779. * The core limits the synchronous connections to either
  3780. * SCO or eSCO. The eSCO connection is preferred and tried
  3781. * to be setup first and until successfully established,
  3782. * the link type will be hinted as eSCO.
  3783. */
  3784. conn = hci_conn_hash_lookup_ba(hdev, ESCO_LINK, &ev->bdaddr);
  3785. if (!conn)
  3786. goto unlock;
  3787. }
  3788. /* The HCI_Synchronous_Connection_Complete event is only sent once per connection.
  3789. * Processing it more than once per connection can corrupt kernel memory.
  3790. *
  3791. * As the connection handle is set here for the first time, it indicates
  3792. * whether the connection is already set up.
  3793. */
  3794. if (!HCI_CONN_HANDLE_UNSET(conn->handle)) {
  3795. bt_dev_err(hdev, "Ignoring HCI_Sync_Conn_Complete event for existing connection");
  3796. goto unlock;
  3797. }
  3798. switch (status) {
  3799. case 0x00:
  3800. status = hci_conn_set_handle(conn, __le16_to_cpu(ev->handle));
  3801. if (status) {
  3802. conn->state = BT_CLOSED;
  3803. break;
  3804. }
  3805. conn->state = BT_CONNECTED;
  3806. conn->type = ev->link_type;
  3807. hci_debugfs_create_conn(conn);
  3808. hci_conn_add_sysfs(conn);
  3809. break;
  3810. case 0x10: /* Connection Accept Timeout */
  3811. case 0x0d: /* Connection Rejected due to Limited Resources */
  3812. case 0x11: /* Unsupported Feature or Parameter Value */
  3813. case 0x1c: /* SCO interval rejected */
  3814. case 0x1a: /* Unsupported Remote Feature */
  3815. case 0x1e: /* Invalid LMP Parameters */
  3816. case 0x1f: /* Unspecified error */
  3817. case 0x20: /* Unsupported LMP Parameter value */
  3818. if (conn->out) {
  3819. conn->pkt_type = (hdev->esco_type & SCO_ESCO_MASK) |
  3820. (hdev->esco_type & EDR_ESCO_MASK);
  3821. if (hci_setup_sync(conn, conn->parent->handle))
  3822. goto unlock;
  3823. }
  3824. fallthrough;
  3825. default:
  3826. conn->state = BT_CLOSED;
  3827. break;
  3828. }
  3829. bt_dev_dbg(hdev, "SCO connected with air mode: %02x", ev->air_mode);
  3830. /* Notify only in case of SCO over HCI transport data path which
  3831. * is zero and non-zero value shall be non-HCI transport data path
  3832. */
  3833. if (conn->codec.data_path == 0 && hdev->notify) {
  3834. switch (ev->air_mode) {
  3835. case 0x02:
  3836. hdev->notify(hdev, HCI_NOTIFY_ENABLE_SCO_CVSD);
  3837. break;
  3838. case 0x03:
  3839. hdev->notify(hdev, HCI_NOTIFY_ENABLE_SCO_TRANSP);
  3840. break;
  3841. }
  3842. }
  3843. hci_connect_cfm(conn, status);
  3844. if (status)
  3845. hci_conn_del(conn);
  3846. unlock:
  3847. hci_dev_unlock(hdev);
  3848. }
  3849. static inline size_t eir_get_length(u8 *eir, size_t eir_len)
  3850. {
  3851. size_t parsed = 0;
  3852. while (parsed < eir_len) {
  3853. u8 field_len = eir[0];
  3854. if (field_len == 0)
  3855. return parsed;
  3856. parsed += field_len + 1;
  3857. eir += field_len + 1;
  3858. }
  3859. return eir_len;
  3860. }
  3861. static void hci_extended_inquiry_result_evt(struct hci_dev *hdev, void *edata,
  3862. struct sk_buff *skb)
  3863. {
  3864. struct hci_ev_ext_inquiry_result *ev = edata;
  3865. struct inquiry_data data;
  3866. size_t eir_len;
  3867. int i;
  3868. if (!hci_ev_skb_pull(hdev, skb, HCI_EV_EXTENDED_INQUIRY_RESULT,
  3869. flex_array_size(ev, info, ev->num)))
  3870. return;
  3871. bt_dev_dbg(hdev, "num %d", ev->num);
  3872. if (!ev->num)
  3873. return;
  3874. if (hci_dev_test_flag(hdev, HCI_PERIODIC_INQ))
  3875. return;
  3876. hci_dev_lock(hdev);
  3877. for (i = 0; i < ev->num; i++) {
  3878. struct extended_inquiry_info *info = &ev->info[i];
  3879. u32 flags;
  3880. bool name_known;
  3881. bacpy(&data.bdaddr, &info->bdaddr);
  3882. data.pscan_rep_mode = info->pscan_rep_mode;
  3883. data.pscan_period_mode = info->pscan_period_mode;
  3884. data.pscan_mode = 0x00;
  3885. memcpy(data.dev_class, info->dev_class, 3);
  3886. data.clock_offset = info->clock_offset;
  3887. data.rssi = info->rssi;
  3888. data.ssp_mode = 0x01;
  3889. if (hci_dev_test_flag(hdev, HCI_MGMT))
  3890. name_known = eir_get_data(info->data,
  3891. sizeof(info->data),
  3892. EIR_NAME_COMPLETE, NULL);
  3893. else
  3894. name_known = true;
  3895. flags = hci_inquiry_cache_update(hdev, &data, name_known);
  3896. eir_len = eir_get_length(info->data, sizeof(info->data));
  3897. mgmt_device_found(hdev, &info->bdaddr, ACL_LINK, 0x00,
  3898. info->dev_class, info->rssi,
  3899. flags, info->data, eir_len, NULL, 0, 0);
  3900. }
  3901. hci_dev_unlock(hdev);
  3902. }
  3903. static void hci_key_refresh_complete_evt(struct hci_dev *hdev, void *data,
  3904. struct sk_buff *skb)
  3905. {
  3906. struct hci_ev_key_refresh_complete *ev = data;
  3907. struct hci_conn *conn;
  3908. bt_dev_dbg(hdev, "status 0x%2.2x handle 0x%4.4x", ev->status,
  3909. __le16_to_cpu(ev->handle));
  3910. hci_dev_lock(hdev);
  3911. conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
  3912. if (!conn)
  3913. goto unlock;
  3914. /* For BR/EDR the necessary steps are taken through the
  3915. * auth_complete event.
  3916. */
  3917. if (conn->type != LE_LINK)
  3918. goto unlock;
  3919. if (!ev->status)
  3920. conn->sec_level = conn->pending_sec_level;
  3921. clear_bit(HCI_CONN_ENCRYPT_PEND, &conn->flags);
  3922. if (ev->status && conn->state == BT_CONNECTED) {
  3923. hci_disconnect(conn, HCI_ERROR_AUTH_FAILURE);
  3924. hci_conn_drop(conn);
  3925. goto unlock;
  3926. }
  3927. if (conn->state == BT_CONFIG) {
  3928. if (!ev->status)
  3929. conn->state = BT_CONNECTED;
  3930. hci_connect_cfm(conn, ev->status);
  3931. hci_conn_drop(conn);
  3932. } else {
  3933. hci_auth_cfm(conn, ev->status);
  3934. hci_conn_hold(conn);
  3935. conn->disc_timeout = HCI_DISCONN_TIMEOUT;
  3936. hci_conn_drop(conn);
  3937. }
  3938. unlock:
  3939. hci_dev_unlock(hdev);
  3940. }
  3941. static u8 hci_get_auth_req(struct hci_conn *conn)
  3942. {
  3943. /* If remote requests no-bonding follow that lead */
  3944. if (conn->remote_auth == HCI_AT_NO_BONDING ||
  3945. conn->remote_auth == HCI_AT_NO_BONDING_MITM)
  3946. return conn->remote_auth | (conn->auth_type & 0x01);
  3947. /* If both remote and local have enough IO capabilities, require
  3948. * MITM protection
  3949. */
  3950. if (conn->remote_cap != HCI_IO_NO_INPUT_OUTPUT &&
  3951. conn->io_capability != HCI_IO_NO_INPUT_OUTPUT)
  3952. return conn->remote_auth | 0x01;
  3953. /* No MITM protection possible so ignore remote requirement */
  3954. return (conn->remote_auth & ~0x01) | (conn->auth_type & 0x01);
  3955. }
  3956. static u8 bredr_oob_data_present(struct hci_conn *conn)
  3957. {
  3958. struct hci_dev *hdev = conn->hdev;
  3959. struct oob_data *data;
  3960. data = hci_find_remote_oob_data(hdev, &conn->dst, BDADDR_BREDR);
  3961. if (!data)
  3962. return 0x00;
  3963. if (bredr_sc_enabled(hdev)) {
  3964. /* When Secure Connections is enabled, then just
  3965. * return the present value stored with the OOB
  3966. * data. The stored value contains the right present
  3967. * information. However it can only be trusted when
  3968. * not in Secure Connection Only mode.
  3969. */
  3970. if (!hci_dev_test_flag(hdev, HCI_SC_ONLY))
  3971. return data->present;
  3972. /* When Secure Connections Only mode is enabled, then
  3973. * the P-256 values are required. If they are not
  3974. * available, then do not declare that OOB data is
  3975. * present.
  3976. */
  3977. if (!crypto_memneq(data->rand256, ZERO_KEY, 16) ||
  3978. !crypto_memneq(data->hash256, ZERO_KEY, 16))
  3979. return 0x00;
  3980. return 0x02;
  3981. }
  3982. /* When Secure Connections is not enabled or actually
  3983. * not supported by the hardware, then check that if
  3984. * P-192 data values are present.
  3985. */
  3986. if (!crypto_memneq(data->rand192, ZERO_KEY, 16) ||
  3987. !crypto_memneq(data->hash192, ZERO_KEY, 16))
  3988. return 0x00;
  3989. return 0x01;
  3990. }
  3991. static void hci_io_capa_request_evt(struct hci_dev *hdev, void *data,
  3992. struct sk_buff *skb)
  3993. {
  3994. struct hci_ev_io_capa_request *ev = data;
  3995. struct hci_conn *conn;
  3996. bt_dev_dbg(hdev, "");
  3997. hci_dev_lock(hdev);
  3998. conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &ev->bdaddr);
  3999. if (!conn || !hci_dev_test_flag(hdev, HCI_SSP_ENABLED))
  4000. goto unlock;
  4001. /* Assume remote supports SSP since it has triggered this event */
  4002. set_bit(HCI_CONN_SSP_ENABLED, &conn->flags);
  4003. hci_conn_hold(conn);
  4004. if (!hci_dev_test_flag(hdev, HCI_MGMT))
  4005. goto unlock;
  4006. /* Allow pairing if we're pairable, the initiators of the
  4007. * pairing or if the remote is not requesting bonding.
  4008. */
  4009. if (hci_dev_test_flag(hdev, HCI_BONDABLE) ||
  4010. test_bit(HCI_CONN_AUTH_INITIATOR, &conn->flags) ||
  4011. (conn->remote_auth & ~0x01) == HCI_AT_NO_BONDING) {
  4012. struct hci_cp_io_capability_reply cp;
  4013. bacpy(&cp.bdaddr, &ev->bdaddr);
  4014. /* Change the IO capability from KeyboardDisplay
  4015. * to DisplayYesNo as it is not supported by BT spec. */
  4016. cp.capability = (conn->io_capability == 0x04) ?
  4017. HCI_IO_DISPLAY_YESNO : conn->io_capability;
  4018. /* If we are initiators, there is no remote information yet */
  4019. if (conn->remote_auth == 0xff) {
  4020. /* Request MITM protection if our IO caps allow it
  4021. * except for the no-bonding case.
  4022. */
  4023. if (conn->io_capability != HCI_IO_NO_INPUT_OUTPUT &&
  4024. conn->auth_type != HCI_AT_NO_BONDING)
  4025. conn->auth_type |= 0x01;
  4026. } else {
  4027. conn->auth_type = hci_get_auth_req(conn);
  4028. }
  4029. /* If we're not bondable, force one of the non-bondable
  4030. * authentication requirement values.
  4031. */
  4032. if (!hci_dev_test_flag(hdev, HCI_BONDABLE))
  4033. conn->auth_type &= HCI_AT_NO_BONDING_MITM;
  4034. cp.authentication = conn->auth_type;
  4035. cp.oob_data = bredr_oob_data_present(conn);
  4036. hci_send_cmd(hdev, HCI_OP_IO_CAPABILITY_REPLY,
  4037. sizeof(cp), &cp);
  4038. } else {
  4039. struct hci_cp_io_capability_neg_reply cp;
  4040. bacpy(&cp.bdaddr, &ev->bdaddr);
  4041. cp.reason = HCI_ERROR_PAIRING_NOT_ALLOWED;
  4042. hci_send_cmd(hdev, HCI_OP_IO_CAPABILITY_NEG_REPLY,
  4043. sizeof(cp), &cp);
  4044. }
  4045. unlock:
  4046. hci_dev_unlock(hdev);
  4047. }
  4048. static void hci_io_capa_reply_evt(struct hci_dev *hdev, void *data,
  4049. struct sk_buff *skb)
  4050. {
  4051. struct hci_ev_io_capa_reply *ev = data;
  4052. struct hci_conn *conn;
  4053. bt_dev_dbg(hdev, "");
  4054. hci_dev_lock(hdev);
  4055. conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &ev->bdaddr);
  4056. if (!conn)
  4057. goto unlock;
  4058. conn->remote_cap = ev->capability;
  4059. conn->remote_auth = ev->authentication;
  4060. unlock:
  4061. hci_dev_unlock(hdev);
  4062. }
  4063. static void hci_user_confirm_request_evt(struct hci_dev *hdev, void *data,
  4064. struct sk_buff *skb)
  4065. {
  4066. struct hci_ev_user_confirm_req *ev = data;
  4067. int loc_mitm, rem_mitm, confirm_hint = 0;
  4068. struct hci_conn *conn;
  4069. bt_dev_dbg(hdev, "");
  4070. hci_dev_lock(hdev);
  4071. if (!hci_dev_test_flag(hdev, HCI_MGMT))
  4072. goto unlock;
  4073. conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &ev->bdaddr);
  4074. if (!conn)
  4075. goto unlock;
  4076. loc_mitm = (conn->auth_type & 0x01);
  4077. rem_mitm = (conn->remote_auth & 0x01);
  4078. /* If we require MITM but the remote device can't provide that
  4079. * (it has NoInputNoOutput) then reject the confirmation
  4080. * request. We check the security level here since it doesn't
  4081. * necessarily match conn->auth_type.
  4082. */
  4083. if (conn->pending_sec_level > BT_SECURITY_MEDIUM &&
  4084. conn->remote_cap == HCI_IO_NO_INPUT_OUTPUT) {
  4085. bt_dev_dbg(hdev, "Rejecting request: remote device can't provide MITM");
  4086. hci_send_cmd(hdev, HCI_OP_USER_CONFIRM_NEG_REPLY,
  4087. sizeof(ev->bdaddr), &ev->bdaddr);
  4088. goto unlock;
  4089. }
  4090. /* If no side requires MITM protection; use JUST_CFM method */
  4091. if ((!loc_mitm || conn->remote_cap == HCI_IO_NO_INPUT_OUTPUT) &&
  4092. (!rem_mitm || conn->io_capability == HCI_IO_NO_INPUT_OUTPUT)) {
  4093. /* If we're not the initiator of request authorization and the
  4094. * local IO capability is not NoInputNoOutput, use JUST_WORKS
  4095. * method (mgmt_user_confirm with confirm_hint set to 1).
  4096. */
  4097. if (!test_bit(HCI_CONN_AUTH_PEND, &conn->flags) &&
  4098. conn->io_capability != HCI_IO_NO_INPUT_OUTPUT) {
  4099. bt_dev_dbg(hdev, "Confirming auto-accept as acceptor");
  4100. confirm_hint = 1;
  4101. goto confirm;
  4102. }
  4103. /* If there already exists link key in local host, leave the
  4104. * decision to user space since the remote device could be
  4105. * legitimate or malicious.
  4106. */
  4107. if (hci_find_link_key(hdev, &ev->bdaddr)) {
  4108. bt_dev_dbg(hdev, "Local host already has link key");
  4109. confirm_hint = 1;
  4110. goto confirm;
  4111. }
  4112. BT_DBG("Auto-accept of user confirmation with %ums delay",
  4113. hdev->auto_accept_delay);
  4114. if (hdev->auto_accept_delay > 0) {
  4115. int delay = msecs_to_jiffies(hdev->auto_accept_delay);
  4116. queue_delayed_work(conn->hdev->workqueue,
  4117. &conn->auto_accept_work, delay);
  4118. goto unlock;
  4119. }
  4120. hci_send_cmd(hdev, HCI_OP_USER_CONFIRM_REPLY,
  4121. sizeof(ev->bdaddr), &ev->bdaddr);
  4122. goto unlock;
  4123. }
  4124. confirm:
  4125. mgmt_user_confirm_request(hdev, &ev->bdaddr, ACL_LINK, 0,
  4126. le32_to_cpu(ev->passkey), confirm_hint);
  4127. unlock:
  4128. hci_dev_unlock(hdev);
  4129. }
  4130. static void hci_user_passkey_request_evt(struct hci_dev *hdev, void *data,
  4131. struct sk_buff *skb)
  4132. {
  4133. struct hci_ev_user_passkey_req *ev = data;
  4134. bt_dev_dbg(hdev, "");
  4135. if (hci_dev_test_flag(hdev, HCI_MGMT))
  4136. mgmt_user_passkey_request(hdev, &ev->bdaddr, ACL_LINK, 0);
  4137. }
  4138. static void hci_user_passkey_notify_evt(struct hci_dev *hdev, void *data,
  4139. struct sk_buff *skb)
  4140. {
  4141. struct hci_ev_user_passkey_notify *ev = data;
  4142. struct hci_conn *conn;
  4143. bt_dev_dbg(hdev, "");
  4144. conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &ev->bdaddr);
  4145. if (!conn)
  4146. return;
  4147. conn->passkey_notify = __le32_to_cpu(ev->passkey);
  4148. conn->passkey_entered = 0;
  4149. if (hci_dev_test_flag(hdev, HCI_MGMT))
  4150. mgmt_user_passkey_notify(hdev, &conn->dst, conn->type,
  4151. conn->dst_type, conn->passkey_notify,
  4152. conn->passkey_entered);
  4153. }
  4154. static void hci_keypress_notify_evt(struct hci_dev *hdev, void *data,
  4155. struct sk_buff *skb)
  4156. {
  4157. struct hci_ev_keypress_notify *ev = data;
  4158. struct hci_conn *conn;
  4159. bt_dev_dbg(hdev, "");
  4160. conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &ev->bdaddr);
  4161. if (!conn)
  4162. return;
  4163. switch (ev->type) {
  4164. case HCI_KEYPRESS_STARTED:
  4165. conn->passkey_entered = 0;
  4166. return;
  4167. case HCI_KEYPRESS_ENTERED:
  4168. conn->passkey_entered++;
  4169. break;
  4170. case HCI_KEYPRESS_ERASED:
  4171. conn->passkey_entered--;
  4172. break;
  4173. case HCI_KEYPRESS_CLEARED:
  4174. conn->passkey_entered = 0;
  4175. break;
  4176. case HCI_KEYPRESS_COMPLETED:
  4177. return;
  4178. }
  4179. if (hci_dev_test_flag(hdev, HCI_MGMT))
  4180. mgmt_user_passkey_notify(hdev, &conn->dst, conn->type,
  4181. conn->dst_type, conn->passkey_notify,
  4182. conn->passkey_entered);
  4183. }
  4184. static void hci_simple_pair_complete_evt(struct hci_dev *hdev, void *data,
  4185. struct sk_buff *skb)
  4186. {
  4187. struct hci_ev_simple_pair_complete *ev = data;
  4188. struct hci_conn *conn;
  4189. bt_dev_dbg(hdev, "");
  4190. hci_dev_lock(hdev);
  4191. conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &ev->bdaddr);
  4192. if (!conn || !hci_conn_ssp_enabled(conn))
  4193. goto unlock;
  4194. /* Reset the authentication requirement to unknown */
  4195. conn->remote_auth = 0xff;
  4196. /* To avoid duplicate auth_failed events to user space we check
  4197. * the HCI_CONN_AUTH_PEND flag which will be set if we
  4198. * initiated the authentication. A traditional auth_complete
  4199. * event gets always produced as initiator and is also mapped to
  4200. * the mgmt_auth_failed event */
  4201. if (!test_bit(HCI_CONN_AUTH_PEND, &conn->flags) && ev->status)
  4202. mgmt_auth_failed(conn, ev->status);
  4203. hci_conn_drop(conn);
  4204. unlock:
  4205. hci_dev_unlock(hdev);
  4206. }
  4207. static void hci_remote_host_features_evt(struct hci_dev *hdev, void *data,
  4208. struct sk_buff *skb)
  4209. {
  4210. struct hci_ev_remote_host_features *ev = data;
  4211. struct inquiry_entry *ie;
  4212. struct hci_conn *conn;
  4213. bt_dev_dbg(hdev, "");
  4214. hci_dev_lock(hdev);
  4215. conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &ev->bdaddr);
  4216. if (conn)
  4217. memcpy(conn->features[1], ev->features, 8);
  4218. ie = hci_inquiry_cache_lookup(hdev, &ev->bdaddr);
  4219. if (ie)
  4220. ie->data.ssp_mode = (ev->features[0] & LMP_HOST_SSP);
  4221. hci_dev_unlock(hdev);
  4222. }
  4223. static void hci_remote_oob_data_request_evt(struct hci_dev *hdev, void *edata,
  4224. struct sk_buff *skb)
  4225. {
  4226. struct hci_ev_remote_oob_data_request *ev = edata;
  4227. struct oob_data *data;
  4228. bt_dev_dbg(hdev, "");
  4229. hci_dev_lock(hdev);
  4230. if (!hci_dev_test_flag(hdev, HCI_MGMT))
  4231. goto unlock;
  4232. data = hci_find_remote_oob_data(hdev, &ev->bdaddr, BDADDR_BREDR);
  4233. if (!data) {
  4234. struct hci_cp_remote_oob_data_neg_reply cp;
  4235. bacpy(&cp.bdaddr, &ev->bdaddr);
  4236. hci_send_cmd(hdev, HCI_OP_REMOTE_OOB_DATA_NEG_REPLY,
  4237. sizeof(cp), &cp);
  4238. goto unlock;
  4239. }
  4240. if (bredr_sc_enabled(hdev)) {
  4241. struct hci_cp_remote_oob_ext_data_reply cp;
  4242. bacpy(&cp.bdaddr, &ev->bdaddr);
  4243. if (hci_dev_test_flag(hdev, HCI_SC_ONLY)) {
  4244. memset(cp.hash192, 0, sizeof(cp.hash192));
  4245. memset(cp.rand192, 0, sizeof(cp.rand192));
  4246. } else {
  4247. memcpy(cp.hash192, data->hash192, sizeof(cp.hash192));
  4248. memcpy(cp.rand192, data->rand192, sizeof(cp.rand192));
  4249. }
  4250. memcpy(cp.hash256, data->hash256, sizeof(cp.hash256));
  4251. memcpy(cp.rand256, data->rand256, sizeof(cp.rand256));
  4252. hci_send_cmd(hdev, HCI_OP_REMOTE_OOB_EXT_DATA_REPLY,
  4253. sizeof(cp), &cp);
  4254. } else {
  4255. struct hci_cp_remote_oob_data_reply cp;
  4256. bacpy(&cp.bdaddr, &ev->bdaddr);
  4257. memcpy(cp.hash, data->hash192, sizeof(cp.hash));
  4258. memcpy(cp.rand, data->rand192, sizeof(cp.rand));
  4259. hci_send_cmd(hdev, HCI_OP_REMOTE_OOB_DATA_REPLY,
  4260. sizeof(cp), &cp);
  4261. }
  4262. unlock:
  4263. hci_dev_unlock(hdev);
  4264. }
  4265. static void le_conn_update_addr(struct hci_conn *conn, bdaddr_t *bdaddr,
  4266. u8 bdaddr_type, bdaddr_t *local_rpa)
  4267. {
  4268. if (conn->out) {
  4269. conn->dst_type = bdaddr_type;
  4270. conn->resp_addr_type = bdaddr_type;
  4271. bacpy(&conn->resp_addr, bdaddr);
  4272. /* Check if the controller has set a Local RPA then it must be
  4273. * used instead or hdev->rpa.
  4274. */
  4275. if (local_rpa && bacmp(local_rpa, BDADDR_ANY)) {
  4276. conn->init_addr_type = ADDR_LE_DEV_RANDOM;
  4277. bacpy(&conn->init_addr, local_rpa);
  4278. } else if (hci_dev_test_flag(conn->hdev, HCI_PRIVACY)) {
  4279. conn->init_addr_type = ADDR_LE_DEV_RANDOM;
  4280. bacpy(&conn->init_addr, &conn->hdev->rpa);
  4281. } else {
  4282. hci_copy_identity_address(conn->hdev, &conn->init_addr,
  4283. &conn->init_addr_type);
  4284. }
  4285. } else {
  4286. conn->resp_addr_type = conn->hdev->adv_addr_type;
  4287. /* Check if the controller has set a Local RPA then it must be
  4288. * used instead or hdev->rpa.
  4289. */
  4290. if (local_rpa && bacmp(local_rpa, BDADDR_ANY)) {
  4291. conn->resp_addr_type = ADDR_LE_DEV_RANDOM;
  4292. bacpy(&conn->resp_addr, local_rpa);
  4293. } else if (conn->hdev->adv_addr_type == ADDR_LE_DEV_RANDOM) {
  4294. /* In case of ext adv, resp_addr will be updated in
  4295. * Adv Terminated event.
  4296. */
  4297. if (!ext_adv_capable(conn->hdev))
  4298. bacpy(&conn->resp_addr,
  4299. &conn->hdev->random_addr);
  4300. } else {
  4301. bacpy(&conn->resp_addr, &conn->hdev->bdaddr);
  4302. }
  4303. conn->init_addr_type = bdaddr_type;
  4304. bacpy(&conn->init_addr, bdaddr);
  4305. /* For incoming connections, set the default minimum
  4306. * and maximum connection interval. They will be used
  4307. * to check if the parameters are in range and if not
  4308. * trigger the connection update procedure.
  4309. */
  4310. conn->le_conn_min_interval = conn->hdev->le_conn_min_interval;
  4311. conn->le_conn_max_interval = conn->hdev->le_conn_max_interval;
  4312. }
  4313. }
  4314. static void le_conn_complete_evt(struct hci_dev *hdev, u8 status,
  4315. bdaddr_t *bdaddr, u8 bdaddr_type,
  4316. bdaddr_t *local_rpa, u8 role, u16 handle,
  4317. u16 interval, u16 latency,
  4318. u16 supervision_timeout)
  4319. {
  4320. struct hci_conn_params *params;
  4321. struct hci_conn *conn;
  4322. struct smp_irk *irk;
  4323. u8 addr_type;
  4324. hci_dev_lock(hdev);
  4325. /* All controllers implicitly stop advertising in the event of a
  4326. * connection, so ensure that the state bit is cleared.
  4327. */
  4328. hci_dev_clear_flag(hdev, HCI_LE_ADV);
  4329. /* Check for existing connection:
  4330. *
  4331. * 1. If it doesn't exist then use the role to create a new object.
  4332. * 2. If it does exist confirm that it is connecting/BT_CONNECT in case
  4333. * of initiator/master role since there could be a collision where
  4334. * either side is attempting to connect or something like a fuzzing
  4335. * testing is trying to play tricks to destroy the hcon object before
  4336. * it even attempts to connect (e.g. hcon->state == BT_OPEN).
  4337. */
  4338. conn = hci_conn_hash_lookup_role(hdev, LE_LINK, role, bdaddr);
  4339. if (!conn ||
  4340. (conn->role == HCI_ROLE_MASTER && conn->state != BT_CONNECT)) {
  4341. /* In case of error status and there is no connection pending
  4342. * just unlock as there is nothing to cleanup.
  4343. */
  4344. if (status)
  4345. goto unlock;
  4346. conn = hci_conn_add_unset(hdev, LE_LINK, bdaddr, role);
  4347. if (IS_ERR(conn)) {
  4348. bt_dev_err(hdev, "connection err: %ld", PTR_ERR(conn));
  4349. goto unlock;
  4350. }
  4351. conn->dst_type = bdaddr_type;
  4352. /* If we didn't have a hci_conn object previously
  4353. * but we're in central role this must be something
  4354. * initiated using an accept list. Since accept list based
  4355. * connections are not "first class citizens" we don't
  4356. * have full tracking of them. Therefore, we go ahead
  4357. * with a "best effort" approach of determining the
  4358. * initiator address based on the HCI_PRIVACY flag.
  4359. */
  4360. if (conn->out) {
  4361. conn->resp_addr_type = bdaddr_type;
  4362. bacpy(&conn->resp_addr, bdaddr);
  4363. if (hci_dev_test_flag(hdev, HCI_PRIVACY)) {
  4364. conn->init_addr_type = ADDR_LE_DEV_RANDOM;
  4365. bacpy(&conn->init_addr, &hdev->rpa);
  4366. } else {
  4367. hci_copy_identity_address(hdev,
  4368. &conn->init_addr,
  4369. &conn->init_addr_type);
  4370. }
  4371. }
  4372. } else {
  4373. cancel_delayed_work(&conn->le_conn_timeout);
  4374. }
  4375. /* The HCI_LE_Connection_Complete event is only sent once per connection.
  4376. * Processing it more than once per connection can corrupt kernel memory.
  4377. *
  4378. * As the connection handle is set here for the first time, it indicates
  4379. * whether the connection is already set up.
  4380. */
  4381. if (!HCI_CONN_HANDLE_UNSET(conn->handle)) {
  4382. bt_dev_err(hdev, "Ignoring HCI_Connection_Complete for existing connection");
  4383. goto unlock;
  4384. }
  4385. le_conn_update_addr(conn, bdaddr, bdaddr_type, local_rpa);
  4386. /* Lookup the identity address from the stored connection
  4387. * address and address type.
  4388. *
  4389. * When establishing connections to an identity address, the
  4390. * connection procedure will store the resolvable random
  4391. * address first. Now if it can be converted back into the
  4392. * identity address, start using the identity address from
  4393. * now on.
  4394. */
  4395. irk = hci_get_irk(hdev, &conn->dst, conn->dst_type);
  4396. if (irk) {
  4397. bacpy(&conn->dst, &irk->bdaddr);
  4398. conn->dst_type = irk->addr_type;
  4399. }
  4400. conn->dst_type = ev_bdaddr_type(hdev, conn->dst_type, NULL);
  4401. /* All connection failure handling is taken care of by the
  4402. * hci_conn_failed function which is triggered by the HCI
  4403. * request completion callbacks used for connecting.
  4404. */
  4405. if (status || hci_conn_set_handle(conn, handle))
  4406. goto unlock;
  4407. /* Drop the connection if it has been aborted */
  4408. if (test_bit(HCI_CONN_CANCEL, &conn->flags)) {
  4409. hci_conn_drop(conn);
  4410. goto unlock;
  4411. }
  4412. if (conn->dst_type == ADDR_LE_DEV_PUBLIC)
  4413. addr_type = BDADDR_LE_PUBLIC;
  4414. else
  4415. addr_type = BDADDR_LE_RANDOM;
  4416. /* Drop the connection if the device is blocked */
  4417. if (hci_bdaddr_list_lookup(&hdev->reject_list, &conn->dst, addr_type)) {
  4418. hci_conn_drop(conn);
  4419. goto unlock;
  4420. }
  4421. mgmt_device_connected(hdev, conn, NULL, 0);
  4422. conn->sec_level = BT_SECURITY_LOW;
  4423. conn->state = BT_CONFIG;
  4424. /* Store current advertising instance as connection advertising instance
  4425. * when sotfware rotation is in use so it can be re-enabled when
  4426. * disconnected.
  4427. */
  4428. if (!ext_adv_capable(hdev))
  4429. conn->adv_instance = hdev->cur_adv_instance;
  4430. conn->le_conn_interval = interval;
  4431. conn->le_conn_latency = latency;
  4432. conn->le_supv_timeout = supervision_timeout;
  4433. hci_debugfs_create_conn(conn);
  4434. hci_conn_add_sysfs(conn);
  4435. /* The remote features procedure is defined for central
  4436. * role only. So only in case of an initiated connection
  4437. * request the remote features.
  4438. *
  4439. * If the local controller supports peripheral-initiated features
  4440. * exchange, then requesting the remote features in peripheral
  4441. * role is possible. Otherwise just transition into the
  4442. * connected state without requesting the remote features.
  4443. */
  4444. if (conn->out ||
  4445. (hdev->le_features[0] & HCI_LE_PERIPHERAL_FEATURES)) {
  4446. struct hci_cp_le_read_remote_features cp;
  4447. cp.handle = __cpu_to_le16(conn->handle);
  4448. hci_send_cmd(hdev, HCI_OP_LE_READ_REMOTE_FEATURES,
  4449. sizeof(cp), &cp);
  4450. hci_conn_hold(conn);
  4451. } else {
  4452. conn->state = BT_CONNECTED;
  4453. hci_connect_cfm(conn, status);
  4454. }
  4455. params = hci_pend_le_action_lookup(&hdev->pend_le_conns, &conn->dst,
  4456. conn->dst_type);
  4457. if (params) {
  4458. hci_pend_le_list_del_init(params);
  4459. if (params->conn) {
  4460. hci_conn_drop(params->conn);
  4461. hci_conn_put(params->conn);
  4462. params->conn = NULL;
  4463. }
  4464. }
  4465. unlock:
  4466. hci_update_passive_scan(hdev);
  4467. hci_dev_unlock(hdev);
  4468. }
  4469. static void hci_le_conn_complete_evt(struct hci_dev *hdev, void *data,
  4470. struct sk_buff *skb)
  4471. {
  4472. struct hci_ev_le_conn_complete *ev = data;
  4473. bt_dev_dbg(hdev, "status 0x%2.2x", ev->status);
  4474. le_conn_complete_evt(hdev, ev->status, &ev->bdaddr, ev->bdaddr_type,
  4475. NULL, ev->role, le16_to_cpu(ev->handle),
  4476. le16_to_cpu(ev->interval),
  4477. le16_to_cpu(ev->latency),
  4478. le16_to_cpu(ev->supervision_timeout));
  4479. }
  4480. static void hci_le_enh_conn_complete_evt(struct hci_dev *hdev, void *data,
  4481. struct sk_buff *skb)
  4482. {
  4483. struct hci_ev_le_enh_conn_complete *ev = data;
  4484. bt_dev_dbg(hdev, "status 0x%2.2x", ev->status);
  4485. le_conn_complete_evt(hdev, ev->status, &ev->bdaddr, ev->bdaddr_type,
  4486. &ev->local_rpa, ev->role, le16_to_cpu(ev->handle),
  4487. le16_to_cpu(ev->interval),
  4488. le16_to_cpu(ev->latency),
  4489. le16_to_cpu(ev->supervision_timeout));
  4490. }
  4491. static void hci_le_ext_adv_term_evt(struct hci_dev *hdev, void *data,
  4492. struct sk_buff *skb)
  4493. {
  4494. struct hci_evt_le_ext_adv_set_term *ev = data;
  4495. struct hci_conn *conn;
  4496. struct adv_info *adv, *n;
  4497. bt_dev_dbg(hdev, "status 0x%2.2x", ev->status);
  4498. /* The Bluetooth Core 5.3 specification clearly states that this event
  4499. * shall not be sent when the Host disables the advertising set. So in
  4500. * case of HCI_ERROR_CANCELLED_BY_HOST, just ignore the event.
  4501. *
  4502. * When the Host disables an advertising set, all cleanup is done via
  4503. * its command callback and not needed to be duplicated here.
  4504. */
  4505. if (ev->status == HCI_ERROR_CANCELLED_BY_HOST) {
  4506. bt_dev_warn_ratelimited(hdev, "Unexpected advertising set terminated event");
  4507. return;
  4508. }
  4509. hci_dev_lock(hdev);
  4510. adv = hci_find_adv_instance(hdev, ev->handle);
  4511. if (ev->status) {
  4512. if (!adv)
  4513. goto unlock;
  4514. /* Remove advertising as it has been terminated */
  4515. hci_remove_adv_instance(hdev, ev->handle);
  4516. mgmt_advertising_removed(NULL, hdev, ev->handle);
  4517. list_for_each_entry_safe(adv, n, &hdev->adv_instances, list) {
  4518. if (adv->enabled)
  4519. goto unlock;
  4520. }
  4521. /* We are no longer advertising, clear HCI_LE_ADV */
  4522. hci_dev_clear_flag(hdev, HCI_LE_ADV);
  4523. goto unlock;
  4524. }
  4525. if (adv)
  4526. adv->enabled = false;
  4527. conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->conn_handle));
  4528. if (conn) {
  4529. /* Store handle in the connection so the correct advertising
  4530. * instance can be re-enabled when disconnected.
  4531. */
  4532. conn->adv_instance = ev->handle;
  4533. if (hdev->adv_addr_type != ADDR_LE_DEV_RANDOM ||
  4534. bacmp(&conn->resp_addr, BDADDR_ANY))
  4535. goto unlock;
  4536. if (!ev->handle) {
  4537. bacpy(&conn->resp_addr, &hdev->random_addr);
  4538. goto unlock;
  4539. }
  4540. if (adv)
  4541. bacpy(&conn->resp_addr, &adv->random_addr);
  4542. }
  4543. unlock:
  4544. hci_dev_unlock(hdev);
  4545. }
  4546. static void hci_le_conn_update_complete_evt(struct hci_dev *hdev, void *data,
  4547. struct sk_buff *skb)
  4548. {
  4549. struct hci_ev_le_conn_update_complete *ev = data;
  4550. struct hci_conn *conn;
  4551. bt_dev_dbg(hdev, "status 0x%2.2x", ev->status);
  4552. if (ev->status)
  4553. return;
  4554. hci_dev_lock(hdev);
  4555. conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
  4556. if (conn) {
  4557. conn->le_conn_interval = le16_to_cpu(ev->interval);
  4558. conn->le_conn_latency = le16_to_cpu(ev->latency);
  4559. conn->le_supv_timeout = le16_to_cpu(ev->supervision_timeout);
  4560. }
  4561. hci_dev_unlock(hdev);
  4562. }
  4563. /* This function requires the caller holds hdev->lock */
  4564. static struct hci_conn *check_pending_le_conn(struct hci_dev *hdev,
  4565. bdaddr_t *addr,
  4566. u8 addr_type, bool addr_resolved,
  4567. u8 adv_type, u8 phy, u8 sec_phy)
  4568. {
  4569. struct hci_conn *conn;
  4570. struct hci_conn_params *params;
  4571. /* If the event is not connectable don't proceed further */
  4572. if (adv_type != LE_ADV_IND && adv_type != LE_ADV_DIRECT_IND)
  4573. return NULL;
  4574. /* Ignore if the device is blocked or hdev is suspended */
  4575. if (hci_bdaddr_list_lookup(&hdev->reject_list, addr, addr_type) ||
  4576. hdev->suspended)
  4577. return NULL;
  4578. /* Most controller will fail if we try to create new connections
  4579. * while we have an existing one in peripheral role.
  4580. */
  4581. if (hdev->conn_hash.le_num_peripheral > 0 &&
  4582. (test_bit(HCI_QUIRK_BROKEN_LE_STATES, &hdev->quirks) ||
  4583. !(hdev->le_states[3] & 0x10)))
  4584. return NULL;
  4585. /* If we're not connectable only connect devices that we have in
  4586. * our pend_le_conns list.
  4587. */
  4588. params = hci_pend_le_action_lookup(&hdev->pend_le_conns, addr,
  4589. addr_type);
  4590. if (!params)
  4591. return NULL;
  4592. if (!params->explicit_connect) {
  4593. switch (params->auto_connect) {
  4594. case HCI_AUTO_CONN_DIRECT:
  4595. /* Only devices advertising with ADV_DIRECT_IND are
  4596. * triggering a connection attempt. This is allowing
  4597. * incoming connections from peripheral devices.
  4598. */
  4599. if (adv_type != LE_ADV_DIRECT_IND)
  4600. return NULL;
  4601. break;
  4602. case HCI_AUTO_CONN_ALWAYS:
  4603. /* Devices advertising with ADV_IND or ADV_DIRECT_IND
  4604. * are triggering a connection attempt. This means
  4605. * that incoming connections from peripheral device are
  4606. * accepted and also outgoing connections to peripheral
  4607. * devices are established when found.
  4608. */
  4609. break;
  4610. default:
  4611. return NULL;
  4612. }
  4613. }
  4614. conn = hci_connect_le(hdev, addr, addr_type, addr_resolved,
  4615. BT_SECURITY_LOW, hdev->def_le_autoconnect_timeout,
  4616. HCI_ROLE_MASTER, phy, sec_phy);
  4617. if (!IS_ERR(conn)) {
  4618. /* If HCI_AUTO_CONN_EXPLICIT is set, conn is already owned
  4619. * by higher layer that tried to connect, if no then
  4620. * store the pointer since we don't really have any
  4621. * other owner of the object besides the params that
  4622. * triggered it. This way we can abort the connection if
  4623. * the parameters get removed and keep the reference
  4624. * count consistent once the connection is established.
  4625. */
  4626. if (!params->explicit_connect)
  4627. params->conn = hci_conn_get(conn);
  4628. return conn;
  4629. }
  4630. switch (PTR_ERR(conn)) {
  4631. case -EBUSY:
  4632. /* If hci_connect() returns -EBUSY it means there is already
  4633. * an LE connection attempt going on. Since controllers don't
  4634. * support more than one connection attempt at the time, we
  4635. * don't consider this an error case.
  4636. */
  4637. break;
  4638. default:
  4639. BT_DBG("Failed to connect: err %ld", PTR_ERR(conn));
  4640. return NULL;
  4641. }
  4642. return NULL;
  4643. }
  4644. static void process_adv_report(struct hci_dev *hdev, u8 type, bdaddr_t *bdaddr,
  4645. u8 bdaddr_type, bdaddr_t *direct_addr,
  4646. u8 direct_addr_type, u8 phy, u8 sec_phy, s8 rssi,
  4647. u8 *data, u8 len, bool ext_adv, bool ctl_time,
  4648. u64 instant)
  4649. {
  4650. struct discovery_state *d = &hdev->discovery;
  4651. struct smp_irk *irk;
  4652. struct hci_conn *conn;
  4653. bool match, bdaddr_resolved;
  4654. u32 flags;
  4655. u8 *ptr;
  4656. switch (type) {
  4657. case LE_ADV_IND:
  4658. case LE_ADV_DIRECT_IND:
  4659. case LE_ADV_SCAN_IND:
  4660. case LE_ADV_NONCONN_IND:
  4661. case LE_ADV_SCAN_RSP:
  4662. break;
  4663. default:
  4664. bt_dev_err_ratelimited(hdev, "unknown advertising packet "
  4665. "type: 0x%02x", type);
  4666. return;
  4667. }
  4668. if (len > max_adv_len(hdev)) {
  4669. bt_dev_err_ratelimited(hdev,
  4670. "adv larger than maximum supported");
  4671. return;
  4672. }
  4673. /* Find the end of the data in case the report contains padded zero
  4674. * bytes at the end causing an invalid length value.
  4675. *
  4676. * When data is NULL, len is 0 so there is no need for extra ptr
  4677. * check as 'ptr < data + 0' is already false in such case.
  4678. */
  4679. for (ptr = data; ptr < data + len && *ptr; ptr += *ptr + 1) {
  4680. if (ptr + 1 + *ptr > data + len)
  4681. break;
  4682. }
  4683. /* Adjust for actual length. This handles the case when remote
  4684. * device is advertising with incorrect data length.
  4685. */
  4686. len = ptr - data;
  4687. /* If the direct address is present, then this report is from
  4688. * a LE Direct Advertising Report event. In that case it is
  4689. * important to see if the address is matching the local
  4690. * controller address.
  4691. */
  4692. if (!hci_dev_test_flag(hdev, HCI_MESH) && direct_addr) {
  4693. direct_addr_type = ev_bdaddr_type(hdev, direct_addr_type,
  4694. &bdaddr_resolved);
  4695. /* Only resolvable random addresses are valid for these
  4696. * kind of reports and others can be ignored.
  4697. */
  4698. if (!hci_bdaddr_is_rpa(direct_addr, direct_addr_type))
  4699. return;
  4700. /* If the controller is not using resolvable random
  4701. * addresses, then this report can be ignored.
  4702. */
  4703. if (!hci_dev_test_flag(hdev, HCI_PRIVACY))
  4704. return;
  4705. /* If the local IRK of the controller does not match
  4706. * with the resolvable random address provided, then
  4707. * this report can be ignored.
  4708. */
  4709. if (!smp_irk_matches(hdev, hdev->irk, direct_addr))
  4710. return;
  4711. }
  4712. /* Check if we need to convert to identity address */
  4713. irk = hci_get_irk(hdev, bdaddr, bdaddr_type);
  4714. if (irk) {
  4715. bdaddr = &irk->bdaddr;
  4716. bdaddr_type = irk->addr_type;
  4717. }
  4718. bdaddr_type = ev_bdaddr_type(hdev, bdaddr_type, &bdaddr_resolved);
  4719. /* Check if we have been requested to connect to this device.
  4720. *
  4721. * direct_addr is set only for directed advertising reports (it is NULL
  4722. * for advertising reports) and is already verified to be RPA above.
  4723. */
  4724. conn = check_pending_le_conn(hdev, bdaddr, bdaddr_type, bdaddr_resolved,
  4725. type, phy, sec_phy);
  4726. if (!ext_adv && conn && type == LE_ADV_IND &&
  4727. len <= max_adv_len(hdev)) {
  4728. /* Store report for later inclusion by
  4729. * mgmt_device_connected
  4730. */
  4731. memcpy(conn->le_adv_data, data, len);
  4732. conn->le_adv_data_len = len;
  4733. }
  4734. if (type == LE_ADV_NONCONN_IND || type == LE_ADV_SCAN_IND)
  4735. flags = MGMT_DEV_FOUND_NOT_CONNECTABLE;
  4736. else
  4737. flags = 0;
  4738. /* All scan results should be sent up for Mesh systems */
  4739. if (hci_dev_test_flag(hdev, HCI_MESH)) {
  4740. mgmt_device_found(hdev, bdaddr, LE_LINK, bdaddr_type, NULL,
  4741. rssi, flags, data, len, NULL, 0, instant);
  4742. return;
  4743. }
  4744. /* Passive scanning shouldn't trigger any device found events,
  4745. * except for devices marked as CONN_REPORT for which we do send
  4746. * device found events, or advertisement monitoring requested.
  4747. */
  4748. if (hdev->le_scan_type == LE_SCAN_PASSIVE) {
  4749. if (type == LE_ADV_DIRECT_IND)
  4750. return;
  4751. if (!hci_pend_le_action_lookup(&hdev->pend_le_reports,
  4752. bdaddr, bdaddr_type) &&
  4753. idr_is_empty(&hdev->adv_monitors_idr))
  4754. return;
  4755. mgmt_device_found(hdev, bdaddr, LE_LINK, bdaddr_type, NULL,
  4756. rssi, flags, data, len, NULL, 0, 0);
  4757. return;
  4758. }
  4759. /* When receiving a scan response, then there is no way to
  4760. * know if the remote device is connectable or not. However
  4761. * since scan responses are merged with a previously seen
  4762. * advertising report, the flags field from that report
  4763. * will be used.
  4764. *
  4765. * In the unlikely case that a controller just sends a scan
  4766. * response event that doesn't match the pending report, then
  4767. * it is marked as a standalone SCAN_RSP.
  4768. */
  4769. if (type == LE_ADV_SCAN_RSP)
  4770. flags = MGMT_DEV_FOUND_SCAN_RSP;
  4771. /* If there's nothing pending either store the data from this
  4772. * event or send an immediate device found event if the data
  4773. * should not be stored for later.
  4774. */
  4775. if (!has_pending_adv_report(hdev)) {
  4776. /* If the report will trigger a SCAN_REQ store it for
  4777. * later merging.
  4778. */
  4779. if (!ext_adv && (type == LE_ADV_IND ||
  4780. type == LE_ADV_SCAN_IND)) {
  4781. store_pending_adv_report(hdev, bdaddr, bdaddr_type,
  4782. rssi, flags, data, len);
  4783. return;
  4784. }
  4785. mgmt_device_found(hdev, bdaddr, LE_LINK, bdaddr_type, NULL,
  4786. rssi, flags, data, len, NULL, 0, 0);
  4787. return;
  4788. }
  4789. /* Check if the pending report is for the same device as the new one */
  4790. match = (!bacmp(bdaddr, &d->last_adv_addr) &&
  4791. bdaddr_type == d->last_adv_addr_type);
  4792. /* If the pending data doesn't match this report or this isn't a
  4793. * scan response (e.g. we got a duplicate ADV_IND) then force
  4794. * sending of the pending data.
  4795. */
  4796. if (type != LE_ADV_SCAN_RSP || !match) {
  4797. /* Send out whatever is in the cache, but skip duplicates */
  4798. if (!match)
  4799. mgmt_device_found(hdev, &d->last_adv_addr, LE_LINK,
  4800. d->last_adv_addr_type, NULL,
  4801. d->last_adv_rssi, d->last_adv_flags,
  4802. d->last_adv_data,
  4803. d->last_adv_data_len, NULL, 0, 0);
  4804. /* If the new report will trigger a SCAN_REQ store it for
  4805. * later merging.
  4806. */
  4807. if (!ext_adv && (type == LE_ADV_IND ||
  4808. type == LE_ADV_SCAN_IND)) {
  4809. store_pending_adv_report(hdev, bdaddr, bdaddr_type,
  4810. rssi, flags, data, len);
  4811. return;
  4812. }
  4813. /* The advertising reports cannot be merged, so clear
  4814. * the pending report and send out a device found event.
  4815. */
  4816. clear_pending_adv_report(hdev);
  4817. mgmt_device_found(hdev, bdaddr, LE_LINK, bdaddr_type, NULL,
  4818. rssi, flags, data, len, NULL, 0, 0);
  4819. return;
  4820. }
  4821. /* If we get here we've got a pending ADV_IND or ADV_SCAN_IND and
  4822. * the new event is a SCAN_RSP. We can therefore proceed with
  4823. * sending a merged device found event.
  4824. */
  4825. mgmt_device_found(hdev, &d->last_adv_addr, LE_LINK,
  4826. d->last_adv_addr_type, NULL, rssi, d->last_adv_flags,
  4827. d->last_adv_data, d->last_adv_data_len, data, len, 0);
  4828. clear_pending_adv_report(hdev);
  4829. }
  4830. static void hci_le_adv_report_evt(struct hci_dev *hdev, void *data,
  4831. struct sk_buff *skb)
  4832. {
  4833. struct hci_ev_le_advertising_report *ev = data;
  4834. u64 instant = jiffies;
  4835. if (!ev->num)
  4836. return;
  4837. hci_dev_lock(hdev);
  4838. while (ev->num--) {
  4839. struct hci_ev_le_advertising_info *info;
  4840. s8 rssi;
  4841. info = hci_le_ev_skb_pull(hdev, skb,
  4842. HCI_EV_LE_ADVERTISING_REPORT,
  4843. sizeof(*info));
  4844. if (!info)
  4845. break;
  4846. if (!hci_le_ev_skb_pull(hdev, skb, HCI_EV_LE_ADVERTISING_REPORT,
  4847. info->length + 1))
  4848. break;
  4849. if (info->length <= max_adv_len(hdev)) {
  4850. rssi = info->data[info->length];
  4851. process_adv_report(hdev, info->type, &info->bdaddr,
  4852. info->bdaddr_type, NULL, 0,
  4853. HCI_ADV_PHY_1M, 0, rssi,
  4854. info->data, info->length, false,
  4855. false, instant);
  4856. } else {
  4857. bt_dev_err(hdev, "Dropping invalid advertising data");
  4858. }
  4859. }
  4860. hci_dev_unlock(hdev);
  4861. }
  4862. static u8 ext_evt_type_to_legacy(struct hci_dev *hdev, u16 evt_type)
  4863. {
  4864. u16 pdu_type = evt_type & ~LE_EXT_ADV_DATA_STATUS_MASK;
  4865. if (!pdu_type)
  4866. return LE_ADV_NONCONN_IND;
  4867. if (evt_type & LE_EXT_ADV_LEGACY_PDU) {
  4868. switch (evt_type) {
  4869. case LE_LEGACY_ADV_IND:
  4870. return LE_ADV_IND;
  4871. case LE_LEGACY_ADV_DIRECT_IND:
  4872. return LE_ADV_DIRECT_IND;
  4873. case LE_LEGACY_ADV_SCAN_IND:
  4874. return LE_ADV_SCAN_IND;
  4875. case LE_LEGACY_NONCONN_IND:
  4876. return LE_ADV_NONCONN_IND;
  4877. case LE_LEGACY_SCAN_RSP_ADV:
  4878. case LE_LEGACY_SCAN_RSP_ADV_SCAN:
  4879. return LE_ADV_SCAN_RSP;
  4880. }
  4881. goto invalid;
  4882. }
  4883. if (evt_type & LE_EXT_ADV_CONN_IND) {
  4884. if (evt_type & LE_EXT_ADV_DIRECT_IND)
  4885. return LE_ADV_DIRECT_IND;
  4886. return LE_ADV_IND;
  4887. }
  4888. if (evt_type & LE_EXT_ADV_SCAN_RSP)
  4889. return LE_ADV_SCAN_RSP;
  4890. if (evt_type & LE_EXT_ADV_SCAN_IND)
  4891. return LE_ADV_SCAN_IND;
  4892. if (evt_type & LE_EXT_ADV_DIRECT_IND)
  4893. return LE_ADV_NONCONN_IND;
  4894. invalid:
  4895. bt_dev_err_ratelimited(hdev, "Unknown advertising packet type: 0x%02x",
  4896. evt_type);
  4897. return LE_ADV_INVALID;
  4898. }
  4899. static void hci_le_ext_adv_report_evt(struct hci_dev *hdev, void *data,
  4900. struct sk_buff *skb)
  4901. {
  4902. struct hci_ev_le_ext_adv_report *ev = data;
  4903. u64 instant = jiffies;
  4904. if (!ev->num)
  4905. return;
  4906. hci_dev_lock(hdev);
  4907. while (ev->num--) {
  4908. struct hci_ev_le_ext_adv_info *info;
  4909. u8 legacy_evt_type;
  4910. u16 evt_type;
  4911. info = hci_le_ev_skb_pull(hdev, skb, HCI_EV_LE_EXT_ADV_REPORT,
  4912. sizeof(*info));
  4913. if (!info)
  4914. break;
  4915. if (!hci_le_ev_skb_pull(hdev, skb, HCI_EV_LE_EXT_ADV_REPORT,
  4916. info->length))
  4917. break;
  4918. evt_type = __le16_to_cpu(info->type) & LE_EXT_ADV_EVT_TYPE_MASK;
  4919. legacy_evt_type = ext_evt_type_to_legacy(hdev, evt_type);
  4920. if (test_bit(HCI_QUIRK_FIXUP_LE_EXT_ADV_REPORT_PHY,
  4921. &hdev->quirks)) {
  4922. info->primary_phy &= 0x1f;
  4923. info->secondary_phy &= 0x1f;
  4924. }
  4925. /* Check if PA Sync is pending and if the hci_conn SID has not
  4926. * been set update it.
  4927. */
  4928. if (hci_dev_test_flag(hdev, HCI_PA_SYNC)) {
  4929. struct hci_conn *conn;
  4930. conn = hci_conn_hash_lookup_create_pa_sync(hdev);
  4931. if (conn && conn->sid == HCI_SID_INVALID)
  4932. conn->sid = info->sid;
  4933. }
  4934. if (legacy_evt_type != LE_ADV_INVALID) {
  4935. process_adv_report(hdev, legacy_evt_type, &info->bdaddr,
  4936. info->bdaddr_type, NULL, 0,
  4937. info->primary_phy,
  4938. info->secondary_phy,
  4939. info->rssi, info->data, info->length,
  4940. !(evt_type & LE_EXT_ADV_LEGACY_PDU),
  4941. false, instant);
  4942. }
  4943. }
  4944. hci_dev_unlock(hdev);
  4945. }
  4946. static int hci_le_pa_term_sync(struct hci_dev *hdev, __le16 handle)
  4947. {
  4948. struct hci_cp_le_pa_term_sync cp;
  4949. memset(&cp, 0, sizeof(cp));
  4950. cp.handle = handle;
  4951. return hci_send_cmd(hdev, HCI_OP_LE_PA_TERM_SYNC, sizeof(cp), &cp);
  4952. }
  4953. static void hci_le_pa_sync_estabilished_evt(struct hci_dev *hdev, void *data,
  4954. struct sk_buff *skb)
  4955. {
  4956. struct hci_ev_le_pa_sync_established *ev = data;
  4957. int mask = hdev->link_mode;
  4958. __u8 flags = 0;
  4959. struct hci_conn *pa_sync, *conn;
  4960. bt_dev_dbg(hdev, "status 0x%2.2x", ev->status);
  4961. hci_dev_lock(hdev);
  4962. hci_dev_clear_flag(hdev, HCI_PA_SYNC);
  4963. conn = hci_conn_hash_lookup_create_pa_sync(hdev);
  4964. if (!conn) {
  4965. bt_dev_err(hdev,
  4966. "Unable to find connection for dst %pMR sid 0x%2.2x",
  4967. &ev->bdaddr, ev->sid);
  4968. goto unlock;
  4969. }
  4970. clear_bit(HCI_CONN_CREATE_PA_SYNC, &conn->flags);
  4971. conn->sync_handle = le16_to_cpu(ev->handle);
  4972. conn->sid = HCI_SID_INVALID;
  4973. mask |= hci_proto_connect_ind(hdev, &ev->bdaddr, ISO_LINK, &flags);
  4974. if (!(mask & HCI_LM_ACCEPT)) {
  4975. hci_le_pa_term_sync(hdev, ev->handle);
  4976. goto unlock;
  4977. }
  4978. if (!(flags & HCI_PROTO_DEFER))
  4979. goto unlock;
  4980. /* Add connection to indicate PA sync event */
  4981. pa_sync = hci_conn_add_unset(hdev, ISO_LINK, BDADDR_ANY,
  4982. HCI_ROLE_SLAVE);
  4983. if (IS_ERR(pa_sync))
  4984. goto unlock;
  4985. pa_sync->sync_handle = le16_to_cpu(ev->handle);
  4986. if (ev->status) {
  4987. set_bit(HCI_CONN_PA_SYNC_FAILED, &pa_sync->flags);
  4988. /* Notify iso layer */
  4989. hci_connect_cfm(pa_sync, ev->status);
  4990. }
  4991. unlock:
  4992. hci_dev_unlock(hdev);
  4993. }
  4994. static void hci_le_per_adv_report_evt(struct hci_dev *hdev, void *data,
  4995. struct sk_buff *skb)
  4996. {
  4997. struct hci_ev_le_per_adv_report *ev = data;
  4998. int mask = hdev->link_mode;
  4999. __u8 flags = 0;
  5000. struct hci_conn *pa_sync;
  5001. bt_dev_dbg(hdev, "sync_handle 0x%4.4x", le16_to_cpu(ev->sync_handle));
  5002. hci_dev_lock(hdev);
  5003. mask |= hci_proto_connect_ind(hdev, BDADDR_ANY, ISO_LINK, &flags);
  5004. if (!(mask & HCI_LM_ACCEPT))
  5005. goto unlock;
  5006. if (!(flags & HCI_PROTO_DEFER))
  5007. goto unlock;
  5008. pa_sync = hci_conn_hash_lookup_pa_sync_handle
  5009. (hdev,
  5010. le16_to_cpu(ev->sync_handle));
  5011. if (!pa_sync)
  5012. goto unlock;
  5013. if (ev->data_status == LE_PA_DATA_COMPLETE &&
  5014. !test_and_set_bit(HCI_CONN_PA_SYNC, &pa_sync->flags)) {
  5015. /* Notify iso layer */
  5016. hci_connect_cfm(pa_sync, 0);
  5017. /* Notify MGMT layer */
  5018. mgmt_device_connected(hdev, pa_sync, NULL, 0);
  5019. }
  5020. unlock:
  5021. hci_dev_unlock(hdev);
  5022. }
  5023. static void hci_le_remote_feat_complete_evt(struct hci_dev *hdev, void *data,
  5024. struct sk_buff *skb)
  5025. {
  5026. struct hci_ev_le_remote_feat_complete *ev = data;
  5027. struct hci_conn *conn;
  5028. bt_dev_dbg(hdev, "status 0x%2.2x", ev->status);
  5029. hci_dev_lock(hdev);
  5030. conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
  5031. if (conn) {
  5032. if (!ev->status)
  5033. memcpy(conn->features[0], ev->features, 8);
  5034. if (conn->state == BT_CONFIG) {
  5035. __u8 status;
  5036. /* If the local controller supports peripheral-initiated
  5037. * features exchange, but the remote controller does
  5038. * not, then it is possible that the error code 0x1a
  5039. * for unsupported remote feature gets returned.
  5040. *
  5041. * In this specific case, allow the connection to
  5042. * transition into connected state and mark it as
  5043. * successful.
  5044. */
  5045. if (!conn->out && ev->status == HCI_ERROR_UNSUPPORTED_REMOTE_FEATURE &&
  5046. (hdev->le_features[0] & HCI_LE_PERIPHERAL_FEATURES))
  5047. status = 0x00;
  5048. else
  5049. status = ev->status;
  5050. conn->state = BT_CONNECTED;
  5051. hci_connect_cfm(conn, status);
  5052. hci_conn_drop(conn);
  5053. }
  5054. }
  5055. hci_dev_unlock(hdev);
  5056. }
  5057. static void hci_le_ltk_request_evt(struct hci_dev *hdev, void *data,
  5058. struct sk_buff *skb)
  5059. {
  5060. struct hci_ev_le_ltk_req *ev = data;
  5061. struct hci_cp_le_ltk_reply cp;
  5062. struct hci_cp_le_ltk_neg_reply neg;
  5063. struct hci_conn *conn;
  5064. struct smp_ltk *ltk;
  5065. bt_dev_dbg(hdev, "handle 0x%4.4x", __le16_to_cpu(ev->handle));
  5066. hci_dev_lock(hdev);
  5067. conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
  5068. if (conn == NULL)
  5069. goto not_found;
  5070. ltk = hci_find_ltk(hdev, &conn->dst, conn->dst_type, conn->role);
  5071. if (!ltk)
  5072. goto not_found;
  5073. if (smp_ltk_is_sc(ltk)) {
  5074. /* With SC both EDiv and Rand are set to zero */
  5075. if (ev->ediv || ev->rand)
  5076. goto not_found;
  5077. } else {
  5078. /* For non-SC keys check that EDiv and Rand match */
  5079. if (ev->ediv != ltk->ediv || ev->rand != ltk->rand)
  5080. goto not_found;
  5081. }
  5082. memcpy(cp.ltk, ltk->val, ltk->enc_size);
  5083. memset(cp.ltk + ltk->enc_size, 0, sizeof(cp.ltk) - ltk->enc_size);
  5084. cp.handle = cpu_to_le16(conn->handle);
  5085. conn->pending_sec_level = smp_ltk_sec_level(ltk);
  5086. conn->enc_key_size = ltk->enc_size;
  5087. hci_send_cmd(hdev, HCI_OP_LE_LTK_REPLY, sizeof(cp), &cp);
  5088. /* Ref. Bluetooth Core SPEC pages 1975 and 2004. STK is a
  5089. * temporary key used to encrypt a connection following
  5090. * pairing. It is used during the Encrypted Session Setup to
  5091. * distribute the keys. Later, security can be re-established
  5092. * using a distributed LTK.
  5093. */
  5094. if (ltk->type == SMP_STK) {
  5095. set_bit(HCI_CONN_STK_ENCRYPT, &conn->flags);
  5096. list_del_rcu(&ltk->list);
  5097. kfree_rcu(ltk, rcu);
  5098. } else {
  5099. clear_bit(HCI_CONN_STK_ENCRYPT, &conn->flags);
  5100. }
  5101. hci_dev_unlock(hdev);
  5102. return;
  5103. not_found:
  5104. neg.handle = ev->handle;
  5105. hci_send_cmd(hdev, HCI_OP_LE_LTK_NEG_REPLY, sizeof(neg), &neg);
  5106. hci_dev_unlock(hdev);
  5107. }
  5108. static void send_conn_param_neg_reply(struct hci_dev *hdev, u16 handle,
  5109. u8 reason)
  5110. {
  5111. struct hci_cp_le_conn_param_req_neg_reply cp;
  5112. cp.handle = cpu_to_le16(handle);
  5113. cp.reason = reason;
  5114. hci_send_cmd(hdev, HCI_OP_LE_CONN_PARAM_REQ_NEG_REPLY, sizeof(cp),
  5115. &cp);
  5116. }
  5117. static void hci_le_remote_conn_param_req_evt(struct hci_dev *hdev, void *data,
  5118. struct sk_buff *skb)
  5119. {
  5120. struct hci_ev_le_remote_conn_param_req *ev = data;
  5121. struct hci_cp_le_conn_param_req_reply cp;
  5122. struct hci_conn *hcon;
  5123. u16 handle, min, max, latency, timeout;
  5124. bt_dev_dbg(hdev, "handle 0x%4.4x", __le16_to_cpu(ev->handle));
  5125. handle = le16_to_cpu(ev->handle);
  5126. min = le16_to_cpu(ev->interval_min);
  5127. max = le16_to_cpu(ev->interval_max);
  5128. latency = le16_to_cpu(ev->latency);
  5129. timeout = le16_to_cpu(ev->timeout);
  5130. hcon = hci_conn_hash_lookup_handle(hdev, handle);
  5131. if (!hcon || hcon->state != BT_CONNECTED)
  5132. return send_conn_param_neg_reply(hdev, handle,
  5133. HCI_ERROR_UNKNOWN_CONN_ID);
  5134. if (max > hcon->le_conn_max_interval)
  5135. return send_conn_param_neg_reply(hdev, handle,
  5136. HCI_ERROR_INVALID_LL_PARAMS);
  5137. if (hci_check_conn_params(min, max, latency, timeout))
  5138. return send_conn_param_neg_reply(hdev, handle,
  5139. HCI_ERROR_INVALID_LL_PARAMS);
  5140. if (hcon->role == HCI_ROLE_MASTER) {
  5141. struct hci_conn_params *params;
  5142. u8 store_hint;
  5143. hci_dev_lock(hdev);
  5144. params = hci_conn_params_lookup(hdev, &hcon->dst,
  5145. hcon->dst_type);
  5146. if (params) {
  5147. params->conn_min_interval = min;
  5148. params->conn_max_interval = max;
  5149. params->conn_latency = latency;
  5150. params->supervision_timeout = timeout;
  5151. store_hint = 0x01;
  5152. } else {
  5153. store_hint = 0x00;
  5154. }
  5155. hci_dev_unlock(hdev);
  5156. mgmt_new_conn_param(hdev, &hcon->dst, hcon->dst_type,
  5157. store_hint, min, max, latency, timeout);
  5158. }
  5159. cp.handle = ev->handle;
  5160. cp.interval_min = ev->interval_min;
  5161. cp.interval_max = ev->interval_max;
  5162. cp.latency = ev->latency;
  5163. cp.timeout = ev->timeout;
  5164. cp.min_ce_len = 0;
  5165. cp.max_ce_len = 0;
  5166. hci_send_cmd(hdev, HCI_OP_LE_CONN_PARAM_REQ_REPLY, sizeof(cp), &cp);
  5167. }
  5168. static void hci_le_direct_adv_report_evt(struct hci_dev *hdev, void *data,
  5169. struct sk_buff *skb)
  5170. {
  5171. struct hci_ev_le_direct_adv_report *ev = data;
  5172. u64 instant = jiffies;
  5173. int i;
  5174. if (!hci_le_ev_skb_pull(hdev, skb, HCI_EV_LE_DIRECT_ADV_REPORT,
  5175. flex_array_size(ev, info, ev->num)))
  5176. return;
  5177. if (!ev->num)
  5178. return;
  5179. hci_dev_lock(hdev);
  5180. for (i = 0; i < ev->num; i++) {
  5181. struct hci_ev_le_direct_adv_info *info = &ev->info[i];
  5182. process_adv_report(hdev, info->type, &info->bdaddr,
  5183. info->bdaddr_type, &info->direct_addr,
  5184. info->direct_addr_type, HCI_ADV_PHY_1M, 0,
  5185. info->rssi, NULL, 0, false, false, instant);
  5186. }
  5187. hci_dev_unlock(hdev);
  5188. }
  5189. static void hci_le_phy_update_evt(struct hci_dev *hdev, void *data,
  5190. struct sk_buff *skb)
  5191. {
  5192. struct hci_ev_le_phy_update_complete *ev = data;
  5193. struct hci_conn *conn;
  5194. bt_dev_dbg(hdev, "status 0x%2.2x", ev->status);
  5195. if (ev->status)
  5196. return;
  5197. hci_dev_lock(hdev);
  5198. conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
  5199. if (!conn)
  5200. goto unlock;
  5201. conn->le_tx_phy = ev->tx_phy;
  5202. conn->le_rx_phy = ev->rx_phy;
  5203. unlock:
  5204. hci_dev_unlock(hdev);
  5205. }
  5206. static void hci_le_cis_estabilished_evt(struct hci_dev *hdev, void *data,
  5207. struct sk_buff *skb)
  5208. {
  5209. struct hci_evt_le_cis_established *ev = data;
  5210. struct hci_conn *conn;
  5211. struct bt_iso_qos *qos;
  5212. bool pending = false;
  5213. u16 handle = __le16_to_cpu(ev->handle);
  5214. u32 c_sdu_interval, p_sdu_interval;
  5215. bt_dev_dbg(hdev, "status 0x%2.2x", ev->status);
  5216. hci_dev_lock(hdev);
  5217. conn = hci_conn_hash_lookup_handle(hdev, handle);
  5218. if (!conn) {
  5219. bt_dev_err(hdev,
  5220. "Unable to find connection with handle 0x%4.4x",
  5221. handle);
  5222. goto unlock;
  5223. }
  5224. if (conn->type != ISO_LINK) {
  5225. bt_dev_err(hdev,
  5226. "Invalid connection link type handle 0x%4.4x",
  5227. handle);
  5228. goto unlock;
  5229. }
  5230. qos = &conn->iso_qos;
  5231. pending = test_and_clear_bit(HCI_CONN_CREATE_CIS, &conn->flags);
  5232. /* BLUETOOTH CORE SPECIFICATION Version 5.4 | Vol 6, Part G
  5233. * page 3075:
  5234. * Transport_Latency_C_To_P = CIG_Sync_Delay + (FT_C_To_P) ×
  5235. * ISO_Interval + SDU_Interval_C_To_P
  5236. * ...
  5237. * SDU_Interval = (CIG_Sync_Delay + (FT) x ISO_Interval) -
  5238. * Transport_Latency
  5239. */
  5240. c_sdu_interval = (get_unaligned_le24(ev->cig_sync_delay) +
  5241. (ev->c_ft * le16_to_cpu(ev->interval) * 1250)) -
  5242. get_unaligned_le24(ev->c_latency);
  5243. p_sdu_interval = (get_unaligned_le24(ev->cig_sync_delay) +
  5244. (ev->p_ft * le16_to_cpu(ev->interval) * 1250)) -
  5245. get_unaligned_le24(ev->p_latency);
  5246. switch (conn->role) {
  5247. case HCI_ROLE_SLAVE:
  5248. qos->ucast.in.interval = c_sdu_interval;
  5249. qos->ucast.out.interval = p_sdu_interval;
  5250. /* Convert Transport Latency (us) to Latency (msec) */
  5251. qos->ucast.in.latency =
  5252. DIV_ROUND_CLOSEST(get_unaligned_le24(ev->c_latency),
  5253. 1000);
  5254. qos->ucast.out.latency =
  5255. DIV_ROUND_CLOSEST(get_unaligned_le24(ev->p_latency),
  5256. 1000);
  5257. qos->ucast.in.sdu = ev->c_bn ? le16_to_cpu(ev->c_mtu) : 0;
  5258. qos->ucast.out.sdu = ev->p_bn ? le16_to_cpu(ev->p_mtu) : 0;
  5259. qos->ucast.in.phy = ev->c_phy;
  5260. qos->ucast.out.phy = ev->p_phy;
  5261. break;
  5262. case HCI_ROLE_MASTER:
  5263. qos->ucast.in.interval = p_sdu_interval;
  5264. qos->ucast.out.interval = c_sdu_interval;
  5265. /* Convert Transport Latency (us) to Latency (msec) */
  5266. qos->ucast.out.latency =
  5267. DIV_ROUND_CLOSEST(get_unaligned_le24(ev->c_latency),
  5268. 1000);
  5269. qos->ucast.in.latency =
  5270. DIV_ROUND_CLOSEST(get_unaligned_le24(ev->p_latency),
  5271. 1000);
  5272. qos->ucast.out.sdu = ev->c_bn ? le16_to_cpu(ev->c_mtu) : 0;
  5273. qos->ucast.in.sdu = ev->p_bn ? le16_to_cpu(ev->p_mtu) : 0;
  5274. qos->ucast.out.phy = ev->c_phy;
  5275. qos->ucast.in.phy = ev->p_phy;
  5276. break;
  5277. }
  5278. if (!ev->status) {
  5279. conn->state = BT_CONNECTED;
  5280. hci_debugfs_create_conn(conn);
  5281. hci_conn_add_sysfs(conn);
  5282. hci_iso_setup_path(conn);
  5283. goto unlock;
  5284. }
  5285. conn->state = BT_CLOSED;
  5286. hci_connect_cfm(conn, ev->status);
  5287. hci_conn_del(conn);
  5288. unlock:
  5289. if (pending)
  5290. hci_le_create_cis_pending(hdev);
  5291. hci_dev_unlock(hdev);
  5292. }
  5293. static void hci_le_reject_cis(struct hci_dev *hdev, __le16 handle)
  5294. {
  5295. struct hci_cp_le_reject_cis cp;
  5296. memset(&cp, 0, sizeof(cp));
  5297. cp.handle = handle;
  5298. cp.reason = HCI_ERROR_REJ_BAD_ADDR;
  5299. hci_send_cmd(hdev, HCI_OP_LE_REJECT_CIS, sizeof(cp), &cp);
  5300. }
  5301. static void hci_le_accept_cis(struct hci_dev *hdev, __le16 handle)
  5302. {
  5303. struct hci_cp_le_accept_cis cp;
  5304. memset(&cp, 0, sizeof(cp));
  5305. cp.handle = handle;
  5306. hci_send_cmd(hdev, HCI_OP_LE_ACCEPT_CIS, sizeof(cp), &cp);
  5307. }
  5308. static void hci_le_cis_req_evt(struct hci_dev *hdev, void *data,
  5309. struct sk_buff *skb)
  5310. {
  5311. struct hci_evt_le_cis_req *ev = data;
  5312. u16 acl_handle, cis_handle;
  5313. struct hci_conn *acl, *cis;
  5314. int mask;
  5315. __u8 flags = 0;
  5316. acl_handle = __le16_to_cpu(ev->acl_handle);
  5317. cis_handle = __le16_to_cpu(ev->cis_handle);
  5318. bt_dev_dbg(hdev, "acl 0x%4.4x handle 0x%4.4x cig 0x%2.2x cis 0x%2.2x",
  5319. acl_handle, cis_handle, ev->cig_id, ev->cis_id);
  5320. hci_dev_lock(hdev);
  5321. acl = hci_conn_hash_lookup_handle(hdev, acl_handle);
  5322. if (!acl)
  5323. goto unlock;
  5324. mask = hci_proto_connect_ind(hdev, &acl->dst, ISO_LINK, &flags);
  5325. if (!(mask & HCI_LM_ACCEPT)) {
  5326. hci_le_reject_cis(hdev, ev->cis_handle);
  5327. goto unlock;
  5328. }
  5329. cis = hci_conn_hash_lookup_handle(hdev, cis_handle);
  5330. if (!cis) {
  5331. cis = hci_conn_add(hdev, ISO_LINK, &acl->dst, HCI_ROLE_SLAVE,
  5332. cis_handle);
  5333. if (IS_ERR(cis)) {
  5334. hci_le_reject_cis(hdev, ev->cis_handle);
  5335. goto unlock;
  5336. }
  5337. }
  5338. cis->iso_qos.ucast.cig = ev->cig_id;
  5339. cis->iso_qos.ucast.cis = ev->cis_id;
  5340. if (!(flags & HCI_PROTO_DEFER)) {
  5341. hci_le_accept_cis(hdev, ev->cis_handle);
  5342. } else {
  5343. cis->state = BT_CONNECT2;
  5344. hci_connect_cfm(cis, 0);
  5345. }
  5346. unlock:
  5347. hci_dev_unlock(hdev);
  5348. }
  5349. static int hci_iso_term_big_sync(struct hci_dev *hdev, void *data)
  5350. {
  5351. u8 handle = PTR_UINT(data);
  5352. return hci_le_terminate_big_sync(hdev, handle,
  5353. HCI_ERROR_LOCAL_HOST_TERM);
  5354. }
  5355. static void hci_le_create_big_complete_evt(struct hci_dev *hdev, void *data,
  5356. struct sk_buff *skb)
  5357. {
  5358. struct hci_evt_le_create_big_complete *ev = data;
  5359. struct hci_conn *conn;
  5360. __u8 i = 0;
  5361. BT_DBG("%s status 0x%2.2x", hdev->name, ev->status);
  5362. if (!hci_le_ev_skb_pull(hdev, skb, HCI_EVT_LE_CREATE_BIG_COMPLETE,
  5363. flex_array_size(ev, bis_handle, ev->num_bis)))
  5364. return;
  5365. hci_dev_lock(hdev);
  5366. /* Connect all BISes that are bound to the BIG */
  5367. while ((conn = hci_conn_hash_lookup_big_state(hdev, ev->handle,
  5368. BT_BOUND))) {
  5369. if (ev->status) {
  5370. hci_connect_cfm(conn, ev->status);
  5371. hci_conn_del(conn);
  5372. continue;
  5373. }
  5374. if (hci_conn_set_handle(conn,
  5375. __le16_to_cpu(ev->bis_handle[i++])))
  5376. continue;
  5377. conn->state = BT_CONNECTED;
  5378. set_bit(HCI_CONN_BIG_CREATED, &conn->flags);
  5379. hci_debugfs_create_conn(conn);
  5380. hci_conn_add_sysfs(conn);
  5381. hci_iso_setup_path(conn);
  5382. }
  5383. if (!ev->status && !i)
  5384. /* If no BISes have been connected for the BIG,
  5385. * terminate. This is in case all bound connections
  5386. * have been closed before the BIG creation
  5387. * has completed.
  5388. */
  5389. hci_cmd_sync_queue(hdev, hci_iso_term_big_sync,
  5390. UINT_PTR(ev->handle), NULL);
  5391. hci_dev_unlock(hdev);
  5392. }
  5393. static void hci_le_big_sync_established_evt(struct hci_dev *hdev, void *data,
  5394. struct sk_buff *skb)
  5395. {
  5396. struct hci_evt_le_big_sync_estabilished *ev = data;
  5397. struct hci_conn *bis, *conn;
  5398. int i;
  5399. bt_dev_dbg(hdev, "status 0x%2.2x", ev->status);
  5400. if (!hci_le_ev_skb_pull(hdev, skb, HCI_EVT_LE_BIG_SYNC_ESTABLISHED,
  5401. flex_array_size(ev, bis, ev->num_bis)))
  5402. return;
  5403. hci_dev_lock(hdev);
  5404. conn = hci_conn_hash_lookup_big_sync_pend(hdev, ev->handle,
  5405. ev->num_bis);
  5406. if (!conn) {
  5407. bt_dev_err(hdev,
  5408. "Unable to find connection for big 0x%2.2x",
  5409. ev->handle);
  5410. goto unlock;
  5411. }
  5412. clear_bit(HCI_CONN_CREATE_BIG_SYNC, &conn->flags);
  5413. conn->num_bis = 0;
  5414. memset(conn->bis, 0, sizeof(conn->num_bis));
  5415. for (i = 0; i < ev->num_bis; i++) {
  5416. u16 handle = le16_to_cpu(ev->bis[i]);
  5417. __le32 interval;
  5418. bis = hci_conn_hash_lookup_handle(hdev, handle);
  5419. if (!bis) {
  5420. if (handle > HCI_CONN_HANDLE_MAX) {
  5421. bt_dev_dbg(hdev, "ignore too large handle %u", handle);
  5422. continue;
  5423. }
  5424. bis = hci_conn_add(hdev, ISO_LINK, BDADDR_ANY,
  5425. HCI_ROLE_SLAVE, handle);
  5426. if (IS_ERR(bis))
  5427. continue;
  5428. }
  5429. if (ev->status != 0x42)
  5430. /* Mark PA sync as established */
  5431. set_bit(HCI_CONN_PA_SYNC, &bis->flags);
  5432. bis->iso_qos.bcast.big = ev->handle;
  5433. memset(&interval, 0, sizeof(interval));
  5434. memcpy(&interval, ev->latency, sizeof(ev->latency));
  5435. bis->iso_qos.bcast.in.interval = le32_to_cpu(interval);
  5436. /* Convert ISO Interval (1.25 ms slots) to latency (ms) */
  5437. bis->iso_qos.bcast.in.latency = le16_to_cpu(ev->interval) * 125 / 100;
  5438. bis->iso_qos.bcast.in.sdu = le16_to_cpu(ev->max_pdu);
  5439. if (!ev->status) {
  5440. bis->state = BT_CONNECTED;
  5441. set_bit(HCI_CONN_BIG_SYNC, &bis->flags);
  5442. hci_debugfs_create_conn(bis);
  5443. hci_conn_add_sysfs(bis);
  5444. hci_iso_setup_path(bis);
  5445. }
  5446. }
  5447. /* In case BIG sync failed, notify each failed connection to
  5448. * the user after all hci connections have been added
  5449. */
  5450. if (ev->status)
  5451. for (i = 0; i < ev->num_bis; i++) {
  5452. u16 handle = le16_to_cpu(ev->bis[i]);
  5453. bis = hci_conn_hash_lookup_handle(hdev, handle);
  5454. if (!bis)
  5455. continue;
  5456. set_bit(HCI_CONN_BIG_SYNC_FAILED, &bis->flags);
  5457. hci_connect_cfm(bis, ev->status);
  5458. }
  5459. unlock:
  5460. hci_dev_unlock(hdev);
  5461. }
  5462. static void hci_le_big_info_adv_report_evt(struct hci_dev *hdev, void *data,
  5463. struct sk_buff *skb)
  5464. {
  5465. struct hci_evt_le_big_info_adv_report *ev = data;
  5466. int mask = hdev->link_mode;
  5467. __u8 flags = 0;
  5468. struct hci_conn *pa_sync;
  5469. bt_dev_dbg(hdev, "sync_handle 0x%4.4x", le16_to_cpu(ev->sync_handle));
  5470. hci_dev_lock(hdev);
  5471. mask |= hci_proto_connect_ind(hdev, BDADDR_ANY, ISO_LINK, &flags);
  5472. if (!(mask & HCI_LM_ACCEPT))
  5473. goto unlock;
  5474. if (!(flags & HCI_PROTO_DEFER))
  5475. goto unlock;
  5476. pa_sync = hci_conn_hash_lookup_pa_sync_handle
  5477. (hdev,
  5478. le16_to_cpu(ev->sync_handle));
  5479. if (!pa_sync)
  5480. goto unlock;
  5481. pa_sync->iso_qos.bcast.encryption = ev->encryption;
  5482. /* Notify iso layer */
  5483. hci_connect_cfm(pa_sync, 0);
  5484. unlock:
  5485. hci_dev_unlock(hdev);
  5486. }
  5487. #define HCI_LE_EV_VL(_op, _func, _min_len, _max_len) \
  5488. [_op] = { \
  5489. .func = _func, \
  5490. .min_len = _min_len, \
  5491. .max_len = _max_len, \
  5492. }
  5493. #define HCI_LE_EV(_op, _func, _len) \
  5494. HCI_LE_EV_VL(_op, _func, _len, _len)
  5495. #define HCI_LE_EV_STATUS(_op, _func) \
  5496. HCI_LE_EV(_op, _func, sizeof(struct hci_ev_status))
  5497. /* Entries in this table shall have their position according to the subevent
  5498. * opcode they handle so the use of the macros above is recommend since it does
  5499. * attempt to initialize at its proper index using Designated Initializers that
  5500. * way events without a callback function can be ommited.
  5501. */
  5502. static const struct hci_le_ev {
  5503. void (*func)(struct hci_dev *hdev, void *data, struct sk_buff *skb);
  5504. u16 min_len;
  5505. u16 max_len;
  5506. } hci_le_ev_table[U8_MAX + 1] = {
  5507. /* [0x01 = HCI_EV_LE_CONN_COMPLETE] */
  5508. HCI_LE_EV(HCI_EV_LE_CONN_COMPLETE, hci_le_conn_complete_evt,
  5509. sizeof(struct hci_ev_le_conn_complete)),
  5510. /* [0x02 = HCI_EV_LE_ADVERTISING_REPORT] */
  5511. HCI_LE_EV_VL(HCI_EV_LE_ADVERTISING_REPORT, hci_le_adv_report_evt,
  5512. sizeof(struct hci_ev_le_advertising_report),
  5513. HCI_MAX_EVENT_SIZE),
  5514. /* [0x03 = HCI_EV_LE_CONN_UPDATE_COMPLETE] */
  5515. HCI_LE_EV(HCI_EV_LE_CONN_UPDATE_COMPLETE,
  5516. hci_le_conn_update_complete_evt,
  5517. sizeof(struct hci_ev_le_conn_update_complete)),
  5518. /* [0x04 = HCI_EV_LE_REMOTE_FEAT_COMPLETE] */
  5519. HCI_LE_EV(HCI_EV_LE_REMOTE_FEAT_COMPLETE,
  5520. hci_le_remote_feat_complete_evt,
  5521. sizeof(struct hci_ev_le_remote_feat_complete)),
  5522. /* [0x05 = HCI_EV_LE_LTK_REQ] */
  5523. HCI_LE_EV(HCI_EV_LE_LTK_REQ, hci_le_ltk_request_evt,
  5524. sizeof(struct hci_ev_le_ltk_req)),
  5525. /* [0x06 = HCI_EV_LE_REMOTE_CONN_PARAM_REQ] */
  5526. HCI_LE_EV(HCI_EV_LE_REMOTE_CONN_PARAM_REQ,
  5527. hci_le_remote_conn_param_req_evt,
  5528. sizeof(struct hci_ev_le_remote_conn_param_req)),
  5529. /* [0x0a = HCI_EV_LE_ENHANCED_CONN_COMPLETE] */
  5530. HCI_LE_EV(HCI_EV_LE_ENHANCED_CONN_COMPLETE,
  5531. hci_le_enh_conn_complete_evt,
  5532. sizeof(struct hci_ev_le_enh_conn_complete)),
  5533. /* [0x0b = HCI_EV_LE_DIRECT_ADV_REPORT] */
  5534. HCI_LE_EV_VL(HCI_EV_LE_DIRECT_ADV_REPORT, hci_le_direct_adv_report_evt,
  5535. sizeof(struct hci_ev_le_direct_adv_report),
  5536. HCI_MAX_EVENT_SIZE),
  5537. /* [0x0c = HCI_EV_LE_PHY_UPDATE_COMPLETE] */
  5538. HCI_LE_EV(HCI_EV_LE_PHY_UPDATE_COMPLETE, hci_le_phy_update_evt,
  5539. sizeof(struct hci_ev_le_phy_update_complete)),
  5540. /* [0x0d = HCI_EV_LE_EXT_ADV_REPORT] */
  5541. HCI_LE_EV_VL(HCI_EV_LE_EXT_ADV_REPORT, hci_le_ext_adv_report_evt,
  5542. sizeof(struct hci_ev_le_ext_adv_report),
  5543. HCI_MAX_EVENT_SIZE),
  5544. /* [0x0e = HCI_EV_LE_PA_SYNC_ESTABLISHED] */
  5545. HCI_LE_EV(HCI_EV_LE_PA_SYNC_ESTABLISHED,
  5546. hci_le_pa_sync_estabilished_evt,
  5547. sizeof(struct hci_ev_le_pa_sync_established)),
  5548. /* [0x0f = HCI_EV_LE_PER_ADV_REPORT] */
  5549. HCI_LE_EV_VL(HCI_EV_LE_PER_ADV_REPORT,
  5550. hci_le_per_adv_report_evt,
  5551. sizeof(struct hci_ev_le_per_adv_report),
  5552. HCI_MAX_EVENT_SIZE),
  5553. /* [0x12 = HCI_EV_LE_EXT_ADV_SET_TERM] */
  5554. HCI_LE_EV(HCI_EV_LE_EXT_ADV_SET_TERM, hci_le_ext_adv_term_evt,
  5555. sizeof(struct hci_evt_le_ext_adv_set_term)),
  5556. /* [0x19 = HCI_EVT_LE_CIS_ESTABLISHED] */
  5557. HCI_LE_EV(HCI_EVT_LE_CIS_ESTABLISHED, hci_le_cis_estabilished_evt,
  5558. sizeof(struct hci_evt_le_cis_established)),
  5559. /* [0x1a = HCI_EVT_LE_CIS_REQ] */
  5560. HCI_LE_EV(HCI_EVT_LE_CIS_REQ, hci_le_cis_req_evt,
  5561. sizeof(struct hci_evt_le_cis_req)),
  5562. /* [0x1b = HCI_EVT_LE_CREATE_BIG_COMPLETE] */
  5563. HCI_LE_EV_VL(HCI_EVT_LE_CREATE_BIG_COMPLETE,
  5564. hci_le_create_big_complete_evt,
  5565. sizeof(struct hci_evt_le_create_big_complete),
  5566. HCI_MAX_EVENT_SIZE),
  5567. /* [0x1d = HCI_EV_LE_BIG_SYNC_ESTABLISHED] */
  5568. HCI_LE_EV_VL(HCI_EVT_LE_BIG_SYNC_ESTABLISHED,
  5569. hci_le_big_sync_established_evt,
  5570. sizeof(struct hci_evt_le_big_sync_estabilished),
  5571. HCI_MAX_EVENT_SIZE),
  5572. /* [0x22 = HCI_EVT_LE_BIG_INFO_ADV_REPORT] */
  5573. HCI_LE_EV_VL(HCI_EVT_LE_BIG_INFO_ADV_REPORT,
  5574. hci_le_big_info_adv_report_evt,
  5575. sizeof(struct hci_evt_le_big_info_adv_report),
  5576. HCI_MAX_EVENT_SIZE),
  5577. };
  5578. static void hci_le_meta_evt(struct hci_dev *hdev, void *data,
  5579. struct sk_buff *skb, u16 *opcode, u8 *status,
  5580. hci_req_complete_t *req_complete,
  5581. hci_req_complete_skb_t *req_complete_skb)
  5582. {
  5583. struct hci_ev_le_meta *ev = data;
  5584. const struct hci_le_ev *subev;
  5585. bt_dev_dbg(hdev, "subevent 0x%2.2x", ev->subevent);
  5586. /* Only match event if command OGF is for LE */
  5587. if (hdev->req_skb &&
  5588. (hci_opcode_ogf(hci_skb_opcode(hdev->req_skb)) == 0x08 ||
  5589. hci_skb_opcode(hdev->req_skb) == HCI_OP_NOP) &&
  5590. hci_skb_event(hdev->req_skb) == ev->subevent) {
  5591. *opcode = hci_skb_opcode(hdev->req_skb);
  5592. hci_req_cmd_complete(hdev, *opcode, 0x00, req_complete,
  5593. req_complete_skb);
  5594. }
  5595. subev = &hci_le_ev_table[ev->subevent];
  5596. if (!subev->func)
  5597. return;
  5598. if (skb->len < subev->min_len) {
  5599. bt_dev_err(hdev, "unexpected subevent 0x%2.2x length: %u < %u",
  5600. ev->subevent, skb->len, subev->min_len);
  5601. return;
  5602. }
  5603. /* Just warn if the length is over max_len size it still be
  5604. * possible to partially parse the event so leave to callback to
  5605. * decide if that is acceptable.
  5606. */
  5607. if (skb->len > subev->max_len)
  5608. bt_dev_warn(hdev, "unexpected subevent 0x%2.2x length: %u > %u",
  5609. ev->subevent, skb->len, subev->max_len);
  5610. data = hci_le_ev_skb_pull(hdev, skb, ev->subevent, subev->min_len);
  5611. if (!data)
  5612. return;
  5613. subev->func(hdev, data, skb);
  5614. }
  5615. static bool hci_get_cmd_complete(struct hci_dev *hdev, u16 opcode,
  5616. u8 event, struct sk_buff *skb)
  5617. {
  5618. struct hci_ev_cmd_complete *ev;
  5619. struct hci_event_hdr *hdr;
  5620. if (!skb)
  5621. return false;
  5622. hdr = hci_ev_skb_pull(hdev, skb, event, sizeof(*hdr));
  5623. if (!hdr)
  5624. return false;
  5625. if (event) {
  5626. if (hdr->evt != event)
  5627. return false;
  5628. return true;
  5629. }
  5630. /* Check if request ended in Command Status - no way to retrieve
  5631. * any extra parameters in this case.
  5632. */
  5633. if (hdr->evt == HCI_EV_CMD_STATUS)
  5634. return false;
  5635. if (hdr->evt != HCI_EV_CMD_COMPLETE) {
  5636. bt_dev_err(hdev, "last event is not cmd complete (0x%2.2x)",
  5637. hdr->evt);
  5638. return false;
  5639. }
  5640. ev = hci_cc_skb_pull(hdev, skb, opcode, sizeof(*ev));
  5641. if (!ev)
  5642. return false;
  5643. if (opcode != __le16_to_cpu(ev->opcode)) {
  5644. BT_DBG("opcode doesn't match (0x%2.2x != 0x%2.2x)", opcode,
  5645. __le16_to_cpu(ev->opcode));
  5646. return false;
  5647. }
  5648. return true;
  5649. }
  5650. static void hci_store_wake_reason(struct hci_dev *hdev, u8 event,
  5651. struct sk_buff *skb)
  5652. {
  5653. struct hci_ev_le_advertising_info *adv;
  5654. struct hci_ev_le_direct_adv_info *direct_adv;
  5655. struct hci_ev_le_ext_adv_info *ext_adv;
  5656. const struct hci_ev_conn_complete *conn_complete = (void *)skb->data;
  5657. const struct hci_ev_conn_request *conn_request = (void *)skb->data;
  5658. hci_dev_lock(hdev);
  5659. /* If we are currently suspended and this is the first BT event seen,
  5660. * save the wake reason associated with the event.
  5661. */
  5662. if (!hdev->suspended || hdev->wake_reason)
  5663. goto unlock;
  5664. /* Default to remote wake. Values for wake_reason are documented in the
  5665. * Bluez mgmt api docs.
  5666. */
  5667. hdev->wake_reason = MGMT_WAKE_REASON_REMOTE_WAKE;
  5668. /* Once configured for remote wakeup, we should only wake up for
  5669. * reconnections. It's useful to see which device is waking us up so
  5670. * keep track of the bdaddr of the connection event that woke us up.
  5671. */
  5672. if (event == HCI_EV_CONN_REQUEST) {
  5673. bacpy(&hdev->wake_addr, &conn_request->bdaddr);
  5674. hdev->wake_addr_type = BDADDR_BREDR;
  5675. } else if (event == HCI_EV_CONN_COMPLETE) {
  5676. bacpy(&hdev->wake_addr, &conn_complete->bdaddr);
  5677. hdev->wake_addr_type = BDADDR_BREDR;
  5678. } else if (event == HCI_EV_LE_META) {
  5679. struct hci_ev_le_meta *le_ev = (void *)skb->data;
  5680. u8 subevent = le_ev->subevent;
  5681. u8 *ptr = &skb->data[sizeof(*le_ev)];
  5682. u8 num_reports = *ptr;
  5683. if ((subevent == HCI_EV_LE_ADVERTISING_REPORT ||
  5684. subevent == HCI_EV_LE_DIRECT_ADV_REPORT ||
  5685. subevent == HCI_EV_LE_EXT_ADV_REPORT) &&
  5686. num_reports) {
  5687. adv = (void *)(ptr + 1);
  5688. direct_adv = (void *)(ptr + 1);
  5689. ext_adv = (void *)(ptr + 1);
  5690. switch (subevent) {
  5691. case HCI_EV_LE_ADVERTISING_REPORT:
  5692. bacpy(&hdev->wake_addr, &adv->bdaddr);
  5693. hdev->wake_addr_type = adv->bdaddr_type;
  5694. break;
  5695. case HCI_EV_LE_DIRECT_ADV_REPORT:
  5696. bacpy(&hdev->wake_addr, &direct_adv->bdaddr);
  5697. hdev->wake_addr_type = direct_adv->bdaddr_type;
  5698. break;
  5699. case HCI_EV_LE_EXT_ADV_REPORT:
  5700. bacpy(&hdev->wake_addr, &ext_adv->bdaddr);
  5701. hdev->wake_addr_type = ext_adv->bdaddr_type;
  5702. break;
  5703. }
  5704. }
  5705. } else {
  5706. hdev->wake_reason = MGMT_WAKE_REASON_UNEXPECTED;
  5707. }
  5708. unlock:
  5709. hci_dev_unlock(hdev);
  5710. }
  5711. #define HCI_EV_VL(_op, _func, _min_len, _max_len) \
  5712. [_op] = { \
  5713. .req = false, \
  5714. .func = _func, \
  5715. .min_len = _min_len, \
  5716. .max_len = _max_len, \
  5717. }
  5718. #define HCI_EV(_op, _func, _len) \
  5719. HCI_EV_VL(_op, _func, _len, _len)
  5720. #define HCI_EV_STATUS(_op, _func) \
  5721. HCI_EV(_op, _func, sizeof(struct hci_ev_status))
  5722. #define HCI_EV_REQ_VL(_op, _func, _min_len, _max_len) \
  5723. [_op] = { \
  5724. .req = true, \
  5725. .func_req = _func, \
  5726. .min_len = _min_len, \
  5727. .max_len = _max_len, \
  5728. }
  5729. #define HCI_EV_REQ(_op, _func, _len) \
  5730. HCI_EV_REQ_VL(_op, _func, _len, _len)
  5731. /* Entries in this table shall have their position according to the event opcode
  5732. * they handle so the use of the macros above is recommend since it does attempt
  5733. * to initialize at its proper index using Designated Initializers that way
  5734. * events without a callback function don't have entered.
  5735. */
  5736. static const struct hci_ev {
  5737. bool req;
  5738. union {
  5739. void (*func)(struct hci_dev *hdev, void *data,
  5740. struct sk_buff *skb);
  5741. void (*func_req)(struct hci_dev *hdev, void *data,
  5742. struct sk_buff *skb, u16 *opcode, u8 *status,
  5743. hci_req_complete_t *req_complete,
  5744. hci_req_complete_skb_t *req_complete_skb);
  5745. };
  5746. u16 min_len;
  5747. u16 max_len;
  5748. } hci_ev_table[U8_MAX + 1] = {
  5749. /* [0x01 = HCI_EV_INQUIRY_COMPLETE] */
  5750. HCI_EV_STATUS(HCI_EV_INQUIRY_COMPLETE, hci_inquiry_complete_evt),
  5751. /* [0x02 = HCI_EV_INQUIRY_RESULT] */
  5752. HCI_EV_VL(HCI_EV_INQUIRY_RESULT, hci_inquiry_result_evt,
  5753. sizeof(struct hci_ev_inquiry_result), HCI_MAX_EVENT_SIZE),
  5754. /* [0x03 = HCI_EV_CONN_COMPLETE] */
  5755. HCI_EV(HCI_EV_CONN_COMPLETE, hci_conn_complete_evt,
  5756. sizeof(struct hci_ev_conn_complete)),
  5757. /* [0x04 = HCI_EV_CONN_REQUEST] */
  5758. HCI_EV(HCI_EV_CONN_REQUEST, hci_conn_request_evt,
  5759. sizeof(struct hci_ev_conn_request)),
  5760. /* [0x05 = HCI_EV_DISCONN_COMPLETE] */
  5761. HCI_EV(HCI_EV_DISCONN_COMPLETE, hci_disconn_complete_evt,
  5762. sizeof(struct hci_ev_disconn_complete)),
  5763. /* [0x06 = HCI_EV_AUTH_COMPLETE] */
  5764. HCI_EV(HCI_EV_AUTH_COMPLETE, hci_auth_complete_evt,
  5765. sizeof(struct hci_ev_auth_complete)),
  5766. /* [0x07 = HCI_EV_REMOTE_NAME] */
  5767. HCI_EV(HCI_EV_REMOTE_NAME, hci_remote_name_evt,
  5768. sizeof(struct hci_ev_remote_name)),
  5769. /* [0x08 = HCI_EV_ENCRYPT_CHANGE] */
  5770. HCI_EV(HCI_EV_ENCRYPT_CHANGE, hci_encrypt_change_evt,
  5771. sizeof(struct hci_ev_encrypt_change)),
  5772. /* [0x09 = HCI_EV_CHANGE_LINK_KEY_COMPLETE] */
  5773. HCI_EV(HCI_EV_CHANGE_LINK_KEY_COMPLETE,
  5774. hci_change_link_key_complete_evt,
  5775. sizeof(struct hci_ev_change_link_key_complete)),
  5776. /* [0x0b = HCI_EV_REMOTE_FEATURES] */
  5777. HCI_EV(HCI_EV_REMOTE_FEATURES, hci_remote_features_evt,
  5778. sizeof(struct hci_ev_remote_features)),
  5779. /* [0x0e = HCI_EV_CMD_COMPLETE] */
  5780. HCI_EV_REQ_VL(HCI_EV_CMD_COMPLETE, hci_cmd_complete_evt,
  5781. sizeof(struct hci_ev_cmd_complete), HCI_MAX_EVENT_SIZE),
  5782. /* [0x0f = HCI_EV_CMD_STATUS] */
  5783. HCI_EV_REQ(HCI_EV_CMD_STATUS, hci_cmd_status_evt,
  5784. sizeof(struct hci_ev_cmd_status)),
  5785. /* [0x10 = HCI_EV_CMD_STATUS] */
  5786. HCI_EV(HCI_EV_HARDWARE_ERROR, hci_hardware_error_evt,
  5787. sizeof(struct hci_ev_hardware_error)),
  5788. /* [0x12 = HCI_EV_ROLE_CHANGE] */
  5789. HCI_EV(HCI_EV_ROLE_CHANGE, hci_role_change_evt,
  5790. sizeof(struct hci_ev_role_change)),
  5791. /* [0x13 = HCI_EV_NUM_COMP_PKTS] */
  5792. HCI_EV_VL(HCI_EV_NUM_COMP_PKTS, hci_num_comp_pkts_evt,
  5793. sizeof(struct hci_ev_num_comp_pkts), HCI_MAX_EVENT_SIZE),
  5794. /* [0x14 = HCI_EV_MODE_CHANGE] */
  5795. HCI_EV(HCI_EV_MODE_CHANGE, hci_mode_change_evt,
  5796. sizeof(struct hci_ev_mode_change)),
  5797. /* [0x16 = HCI_EV_PIN_CODE_REQ] */
  5798. HCI_EV(HCI_EV_PIN_CODE_REQ, hci_pin_code_request_evt,
  5799. sizeof(struct hci_ev_pin_code_req)),
  5800. /* [0x17 = HCI_EV_LINK_KEY_REQ] */
  5801. HCI_EV(HCI_EV_LINK_KEY_REQ, hci_link_key_request_evt,
  5802. sizeof(struct hci_ev_link_key_req)),
  5803. /* [0x18 = HCI_EV_LINK_KEY_NOTIFY] */
  5804. HCI_EV(HCI_EV_LINK_KEY_NOTIFY, hci_link_key_notify_evt,
  5805. sizeof(struct hci_ev_link_key_notify)),
  5806. /* [0x1c = HCI_EV_CLOCK_OFFSET] */
  5807. HCI_EV(HCI_EV_CLOCK_OFFSET, hci_clock_offset_evt,
  5808. sizeof(struct hci_ev_clock_offset)),
  5809. /* [0x1d = HCI_EV_PKT_TYPE_CHANGE] */
  5810. HCI_EV(HCI_EV_PKT_TYPE_CHANGE, hci_pkt_type_change_evt,
  5811. sizeof(struct hci_ev_pkt_type_change)),
  5812. /* [0x20 = HCI_EV_PSCAN_REP_MODE] */
  5813. HCI_EV(HCI_EV_PSCAN_REP_MODE, hci_pscan_rep_mode_evt,
  5814. sizeof(struct hci_ev_pscan_rep_mode)),
  5815. /* [0x22 = HCI_EV_INQUIRY_RESULT_WITH_RSSI] */
  5816. HCI_EV_VL(HCI_EV_INQUIRY_RESULT_WITH_RSSI,
  5817. hci_inquiry_result_with_rssi_evt,
  5818. sizeof(struct hci_ev_inquiry_result_rssi),
  5819. HCI_MAX_EVENT_SIZE),
  5820. /* [0x23 = HCI_EV_REMOTE_EXT_FEATURES] */
  5821. HCI_EV(HCI_EV_REMOTE_EXT_FEATURES, hci_remote_ext_features_evt,
  5822. sizeof(struct hci_ev_remote_ext_features)),
  5823. /* [0x2c = HCI_EV_SYNC_CONN_COMPLETE] */
  5824. HCI_EV(HCI_EV_SYNC_CONN_COMPLETE, hci_sync_conn_complete_evt,
  5825. sizeof(struct hci_ev_sync_conn_complete)),
  5826. /* [0x2d = HCI_EV_EXTENDED_INQUIRY_RESULT] */
  5827. HCI_EV_VL(HCI_EV_EXTENDED_INQUIRY_RESULT,
  5828. hci_extended_inquiry_result_evt,
  5829. sizeof(struct hci_ev_ext_inquiry_result), HCI_MAX_EVENT_SIZE),
  5830. /* [0x30 = HCI_EV_KEY_REFRESH_COMPLETE] */
  5831. HCI_EV(HCI_EV_KEY_REFRESH_COMPLETE, hci_key_refresh_complete_evt,
  5832. sizeof(struct hci_ev_key_refresh_complete)),
  5833. /* [0x31 = HCI_EV_IO_CAPA_REQUEST] */
  5834. HCI_EV(HCI_EV_IO_CAPA_REQUEST, hci_io_capa_request_evt,
  5835. sizeof(struct hci_ev_io_capa_request)),
  5836. /* [0x32 = HCI_EV_IO_CAPA_REPLY] */
  5837. HCI_EV(HCI_EV_IO_CAPA_REPLY, hci_io_capa_reply_evt,
  5838. sizeof(struct hci_ev_io_capa_reply)),
  5839. /* [0x33 = HCI_EV_USER_CONFIRM_REQUEST] */
  5840. HCI_EV(HCI_EV_USER_CONFIRM_REQUEST, hci_user_confirm_request_evt,
  5841. sizeof(struct hci_ev_user_confirm_req)),
  5842. /* [0x34 = HCI_EV_USER_PASSKEY_REQUEST] */
  5843. HCI_EV(HCI_EV_USER_PASSKEY_REQUEST, hci_user_passkey_request_evt,
  5844. sizeof(struct hci_ev_user_passkey_req)),
  5845. /* [0x35 = HCI_EV_REMOTE_OOB_DATA_REQUEST] */
  5846. HCI_EV(HCI_EV_REMOTE_OOB_DATA_REQUEST, hci_remote_oob_data_request_evt,
  5847. sizeof(struct hci_ev_remote_oob_data_request)),
  5848. /* [0x36 = HCI_EV_SIMPLE_PAIR_COMPLETE] */
  5849. HCI_EV(HCI_EV_SIMPLE_PAIR_COMPLETE, hci_simple_pair_complete_evt,
  5850. sizeof(struct hci_ev_simple_pair_complete)),
  5851. /* [0x3b = HCI_EV_USER_PASSKEY_NOTIFY] */
  5852. HCI_EV(HCI_EV_USER_PASSKEY_NOTIFY, hci_user_passkey_notify_evt,
  5853. sizeof(struct hci_ev_user_passkey_notify)),
  5854. /* [0x3c = HCI_EV_KEYPRESS_NOTIFY] */
  5855. HCI_EV(HCI_EV_KEYPRESS_NOTIFY, hci_keypress_notify_evt,
  5856. sizeof(struct hci_ev_keypress_notify)),
  5857. /* [0x3d = HCI_EV_REMOTE_HOST_FEATURES] */
  5858. HCI_EV(HCI_EV_REMOTE_HOST_FEATURES, hci_remote_host_features_evt,
  5859. sizeof(struct hci_ev_remote_host_features)),
  5860. /* [0x3e = HCI_EV_LE_META] */
  5861. HCI_EV_REQ_VL(HCI_EV_LE_META, hci_le_meta_evt,
  5862. sizeof(struct hci_ev_le_meta), HCI_MAX_EVENT_SIZE),
  5863. /* [0xff = HCI_EV_VENDOR] */
  5864. HCI_EV_VL(HCI_EV_VENDOR, msft_vendor_evt, 0, HCI_MAX_EVENT_SIZE),
  5865. };
  5866. static void hci_event_func(struct hci_dev *hdev, u8 event, struct sk_buff *skb,
  5867. u16 *opcode, u8 *status,
  5868. hci_req_complete_t *req_complete,
  5869. hci_req_complete_skb_t *req_complete_skb)
  5870. {
  5871. const struct hci_ev *ev = &hci_ev_table[event];
  5872. void *data;
  5873. if (!ev->func)
  5874. return;
  5875. if (skb->len < ev->min_len) {
  5876. bt_dev_err(hdev, "unexpected event 0x%2.2x length: %u < %u",
  5877. event, skb->len, ev->min_len);
  5878. return;
  5879. }
  5880. /* Just warn if the length is over max_len size it still be
  5881. * possible to partially parse the event so leave to callback to
  5882. * decide if that is acceptable.
  5883. */
  5884. if (skb->len > ev->max_len)
  5885. bt_dev_warn_ratelimited(hdev,
  5886. "unexpected event 0x%2.2x length: %u > %u",
  5887. event, skb->len, ev->max_len);
  5888. data = hci_ev_skb_pull(hdev, skb, event, ev->min_len);
  5889. if (!data)
  5890. return;
  5891. if (ev->req)
  5892. ev->func_req(hdev, data, skb, opcode, status, req_complete,
  5893. req_complete_skb);
  5894. else
  5895. ev->func(hdev, data, skb);
  5896. }
  5897. void hci_event_packet(struct hci_dev *hdev, struct sk_buff *skb)
  5898. {
  5899. struct hci_event_hdr *hdr = (void *) skb->data;
  5900. hci_req_complete_t req_complete = NULL;
  5901. hci_req_complete_skb_t req_complete_skb = NULL;
  5902. struct sk_buff *orig_skb = NULL;
  5903. u8 status = 0, event, req_evt = 0;
  5904. u16 opcode = HCI_OP_NOP;
  5905. if (skb->len < sizeof(*hdr)) {
  5906. bt_dev_err(hdev, "Malformed HCI Event");
  5907. goto done;
  5908. }
  5909. hci_dev_lock(hdev);
  5910. kfree_skb(hdev->recv_event);
  5911. hdev->recv_event = skb_clone(skb, GFP_KERNEL);
  5912. hci_dev_unlock(hdev);
  5913. event = hdr->evt;
  5914. if (!event) {
  5915. bt_dev_warn(hdev, "Received unexpected HCI Event 0x%2.2x",
  5916. event);
  5917. goto done;
  5918. }
  5919. /* Only match event if command OGF is not for LE */
  5920. if (hdev->req_skb &&
  5921. hci_opcode_ogf(hci_skb_opcode(hdev->req_skb)) != 0x08 &&
  5922. hci_skb_event(hdev->req_skb) == event) {
  5923. hci_req_cmd_complete(hdev, hci_skb_opcode(hdev->req_skb),
  5924. status, &req_complete, &req_complete_skb);
  5925. req_evt = event;
  5926. }
  5927. /* If it looks like we might end up having to call
  5928. * req_complete_skb, store a pristine copy of the skb since the
  5929. * various handlers may modify the original one through
  5930. * skb_pull() calls, etc.
  5931. */
  5932. if (req_complete_skb || event == HCI_EV_CMD_STATUS ||
  5933. event == HCI_EV_CMD_COMPLETE)
  5934. orig_skb = skb_clone(skb, GFP_KERNEL);
  5935. skb_pull(skb, HCI_EVENT_HDR_SIZE);
  5936. /* Store wake reason if we're suspended */
  5937. hci_store_wake_reason(hdev, event, skb);
  5938. bt_dev_dbg(hdev, "event 0x%2.2x", event);
  5939. hci_event_func(hdev, event, skb, &opcode, &status, &req_complete,
  5940. &req_complete_skb);
  5941. if (req_complete) {
  5942. req_complete(hdev, status, opcode);
  5943. } else if (req_complete_skb) {
  5944. if (!hci_get_cmd_complete(hdev, opcode, req_evt, orig_skb)) {
  5945. kfree_skb(orig_skb);
  5946. orig_skb = NULL;
  5947. }
  5948. req_complete_skb(hdev, status, opcode, orig_skb);
  5949. }
  5950. done:
  5951. kfree_skb(orig_skb);
  5952. kfree_skb(skb);
  5953. hdev->stat.evt_rx++;
  5954. }