hci_event.c 200 KB

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