hci_event.c 123 KB

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