123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070 |
- /*
- BlueZ - Bluetooth protocol stack for Linux
- Copyright (c) 2000-2001, 2010-2013 The Linux Foundation. All rights reserved.
- Copyright (C) 2009-2010 Gustavo F. Padovan <gustavo@padovan.org>
- Copyright (C) 2010 Google Inc.
- Written 2000,2001 by Maxim Krasnyansky <maxk@qualcomm.com>
- This program is free software; you can redistribute it and/or modify
- it under the terms of the GNU General Public License version 2 as
- published by the Free Software Foundation;
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
- OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS.
- IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) AND AUTHOR(S) BE LIABLE FOR ANY
- CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES
- WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
- ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
- OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
- ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS,
- COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS
- SOFTWARE IS DISCLAIMED.
- */
- /* Bluetooth L2CAP core. */
- #include <linux/module.h>
- #include <linux/types.h>
- #include <linux/capability.h>
- #include <linux/errno.h>
- #include <linux/kernel.h>
- #include <linux/sched.h>
- #include <linux/slab.h>
- #include <linux/poll.h>
- #include <linux/fcntl.h>
- #include <linux/init.h>
- #include <linux/interrupt.h>
- #include <linux/socket.h>
- #include <linux/skbuff.h>
- #include <linux/list.h>
- #include <linux/device.h>
- #include <linux/debugfs.h>
- #include <linux/seq_file.h>
- #include <linux/uaccess.h>
- #include <linux/crc16.h>
- #include <linux/math64.h>
- #include <net/sock.h>
- #include <asm/system.h>
- #include <asm/unaligned.h>
- #include <net/bluetooth/bluetooth.h>
- #include <net/bluetooth/hci_core.h>
- #include <net/bluetooth/l2cap.h>
- #include <net/bluetooth/smp.h>
- #include <net/bluetooth/amp.h>
- bool disable_ertm;
- bool enable_hs;
- bool enable_reconfig;
- static u32 l2cap_feat_mask = L2CAP_FEAT_FIXED_CHAN;
- static u8 l2cap_fc_mask = L2CAP_FC_L2CAP;
- struct workqueue_struct *_l2cap_wq;
- struct bt_sock_list l2cap_sk_list = {
- .lock = __RW_LOCK_UNLOCKED(l2cap_sk_list.lock)
- };
- static void l2cap_send_move_chan_req(struct l2cap_conn *conn,
- struct l2cap_pinfo *pi, u16 icid, u8 dest_amp_id);
- static void l2cap_send_move_chan_cfm(struct l2cap_conn *conn,
- struct l2cap_pinfo *pi, u16 icid, u16 result);
- static void l2cap_send_move_chan_rsp(struct l2cap_conn *conn, u8 ident,
- u16 icid, u16 result);
- static void l2cap_amp_move_setup(struct sock *sk);
- static void l2cap_amp_move_success(struct sock *sk);
- static void l2cap_amp_move_revert(struct sock *sk);
- static int l2cap_ertm_rx_queued_iframes(struct sock *sk);
- static struct sk_buff *l2cap_build_cmd(struct l2cap_conn *conn,
- u8 code, u8 ident, u16 dlen, void *data);
- static int l2cap_answer_move_poll(struct sock *sk);
- static int l2cap_create_cfm(struct hci_chan *chan, u8 status);
- static int l2cap_deaggregate(struct hci_chan *chan, struct l2cap_pinfo *pi);
- static void l2cap_chan_ready(struct sock *sk);
- static void l2cap_conn_del(struct hci_conn *hcon, int err, u8 is_process);
- static u16 l2cap_get_smallest_flushto(struct l2cap_chan_list *l);
- static void l2cap_set_acl_flushto(struct hci_conn *hcon, u16 flush_to);
- static void l2cap_queue_acl_data(struct work_struct *worker);
- static struct att_channel_parameters{
- struct sk_buff *skb;
- struct l2cap_conn *conn;
- __le16 cid;
- int dir;
- } att_chn_params;
- /* ---- L2CAP channels ---- */
- static struct sock *__l2cap_get_chan_by_dcid(struct l2cap_chan_list *l, u16 cid)
- {
- struct sock *s;
- for (s = l->head; s; s = l2cap_pi(s)->next_c) {
- if (l2cap_pi(s)->dcid == cid)
- break;
- }
- return s;
- }
- /* Find channel with given DCID.
- * Returns locked socket */
- static inline struct sock *l2cap_get_chan_by_dcid(struct l2cap_chan_list *l,
- u16 cid)
- {
- struct sock *s;
- read_lock(&l->lock);
- s = __l2cap_get_chan_by_dcid(l, cid);
- if (s)
- bh_lock_sock(s);
- read_unlock(&l->lock);
- return s;
- }
- static struct sock *__l2cap_get_chan_by_scid(struct l2cap_chan_list *l, u16 cid)
- {
- struct sock *s;
- for (s = l->head; s; s = l2cap_pi(s)->next_c) {
- if (l2cap_pi(s)->scid == cid)
- break;
- }
- return s;
- }
- /* Find channel with given SCID.
- * Returns locked socket */
- static inline struct sock *l2cap_get_chan_by_scid(struct l2cap_chan_list *l, u16 cid)
- {
- struct sock *s;
- read_lock(&l->lock);
- s = __l2cap_get_chan_by_scid(l, cid);
- if (s)
- bh_lock_sock(s);
- read_unlock(&l->lock);
- return s;
- }
- static struct sock *__l2cap_get_chan_by_ident(struct l2cap_chan_list *l, u8 ident)
- {
- struct sock *s;
- for (s = l->head; s; s = l2cap_pi(s)->next_c) {
- if (l2cap_pi(s)->ident == ident)
- break;
- }
- return s;
- }
- static inline struct sock *l2cap_get_chan_by_ident(struct l2cap_chan_list *l, u8 ident)
- {
- struct sock *s;
- read_lock(&l->lock);
- s = __l2cap_get_chan_by_ident(l, ident);
- if (s)
- bh_lock_sock(s);
- read_unlock(&l->lock);
- return s;
- }
- static inline struct sk_buff *l2cap_ertm_seq_in_queue(struct sk_buff_head *head,
- u16 seq)
- {
- struct sk_buff *skb;
- skb_queue_walk(head, skb) {
- if (bt_cb(skb)->control.txseq == seq)
- return skb;
- }
- return NULL;
- }
- static int l2cap_seq_list_init(struct l2cap_seq_list *seq_list, u16 size)
- {
- u16 allocSize = 1;
- int err = 0;
- int i;
- /* Actual allocated size must be a power of 2 */
- while (allocSize && allocSize <= size)
- allocSize <<= 1;
- if (!allocSize)
- return -ENOMEM;
- seq_list->list = kzalloc(sizeof(u16) * allocSize, GFP_ATOMIC);
- if (!seq_list->list)
- return -ENOMEM;
- seq_list->size = allocSize;
- seq_list->mask = allocSize - 1;
- seq_list->head = L2CAP_SEQ_LIST_CLEAR;
- seq_list->tail = L2CAP_SEQ_LIST_CLEAR;
- for (i = 0; i < allocSize; i++)
- seq_list->list[i] = L2CAP_SEQ_LIST_CLEAR;
- return err;
- }
- static inline void l2cap_seq_list_free(struct l2cap_seq_list *seq_list)
- {
- kfree(seq_list->list);
- }
- static inline bool l2cap_seq_list_contains(struct l2cap_seq_list *seq_list,
- u16 seq)
- {
- return seq_list->list[seq & seq_list->mask] != L2CAP_SEQ_LIST_CLEAR;
- }
- static u16 l2cap_seq_list_remove(struct l2cap_seq_list *seq_list, u16 seq)
- {
- u16 mask = seq_list->mask;
- BT_DBG("seq_list %p, seq %d", seq_list, (int) seq);
- if (seq_list->head == L2CAP_SEQ_LIST_CLEAR) {
- /* In case someone tries to pop the head of an empty list */
- BT_DBG("List empty");
- return L2CAP_SEQ_LIST_CLEAR;
- } else if (seq_list->head == seq) {
- /* Head can be removed quickly */
- BT_DBG("Remove head");
- seq_list->head = seq_list->list[seq & mask];
- seq_list->list[seq & mask] = L2CAP_SEQ_LIST_CLEAR;
- if (seq_list->head == L2CAP_SEQ_LIST_TAIL) {
- seq_list->head = L2CAP_SEQ_LIST_CLEAR;
- seq_list->tail = L2CAP_SEQ_LIST_CLEAR;
- }
- } else {
- /* Non-head item must be found first */
- u16 prev = seq_list->head;
- BT_DBG("Find and remove");
- while (seq_list->list[prev & mask] != seq) {
- prev = seq_list->list[prev & mask];
- if (prev == L2CAP_SEQ_LIST_TAIL) {
- BT_DBG("seq %d not in list", (int) seq);
- return L2CAP_SEQ_LIST_CLEAR;
- }
- }
- seq_list->list[prev & mask] = seq_list->list[seq & mask];
- seq_list->list[seq & mask] = L2CAP_SEQ_LIST_CLEAR;
- if (seq_list->tail == seq)
- seq_list->tail = prev;
- }
- return seq;
- }
- static inline u16 l2cap_seq_list_pop(struct l2cap_seq_list *seq_list)
- {
- return l2cap_seq_list_remove(seq_list, seq_list->head);
- }
- static void l2cap_seq_list_clear(struct l2cap_seq_list *seq_list)
- {
- if (seq_list->head != L2CAP_SEQ_LIST_CLEAR) {
- u16 i;
- for (i = 0; i < seq_list->size; i++)
- seq_list->list[i] = L2CAP_SEQ_LIST_CLEAR;
- seq_list->head = L2CAP_SEQ_LIST_CLEAR;
- seq_list->tail = L2CAP_SEQ_LIST_CLEAR;
- }
- }
- static void l2cap_seq_list_append(struct l2cap_seq_list *seq_list, u16 seq)
- {
- u16 mask = seq_list->mask;
- BT_DBG("seq_list %p, seq %d", seq_list, (int) seq);
- if (seq_list->list[seq & mask] == L2CAP_SEQ_LIST_CLEAR) {
- if (seq_list->tail == L2CAP_SEQ_LIST_CLEAR)
- seq_list->head = seq;
- else
- seq_list->list[seq_list->tail & mask] = seq;
- seq_list->tail = seq;
- seq_list->list[seq & mask] = L2CAP_SEQ_LIST_TAIL;
- }
- }
- static u16 __pack_enhanced_control(struct bt_l2cap_control *control)
- {
- u16 packed;
- packed = (control->reqseq << L2CAP_CTRL_REQSEQ_SHIFT) &
- L2CAP_CTRL_REQSEQ;
- packed |= (control->final << L2CAP_CTRL_FINAL_SHIFT) &
- L2CAP_CTRL_FINAL;
- if (control->frame_type == 's') {
- packed |= (control->poll << L2CAP_CTRL_POLL_SHIFT) &
- L2CAP_CTRL_POLL;
- packed |= (control->super << L2CAP_CTRL_SUPERVISE_SHIFT) &
- L2CAP_CTRL_SUPERVISE;
- packed |= L2CAP_CTRL_FRAME_TYPE;
- } else {
- packed |= (control->sar << L2CAP_CTRL_SAR_SHIFT) &
- L2CAP_CTRL_SAR;
- packed |= (control->txseq << L2CAP_CTRL_TXSEQ_SHIFT) &
- L2CAP_CTRL_TXSEQ;
- }
- return packed;
- }
- static void __get_enhanced_control(u16 enhanced,
- struct bt_l2cap_control *control)
- {
- control->reqseq = (enhanced & L2CAP_CTRL_REQSEQ) >>
- L2CAP_CTRL_REQSEQ_SHIFT;
- control->final = (enhanced & L2CAP_CTRL_FINAL) >>
- L2CAP_CTRL_FINAL_SHIFT;
- if (enhanced & L2CAP_CTRL_FRAME_TYPE) {
- control->frame_type = 's';
- control->poll = (enhanced & L2CAP_CTRL_POLL) >>
- L2CAP_CTRL_POLL_SHIFT;
- control->super = (enhanced & L2CAP_CTRL_SUPERVISE) >>
- L2CAP_CTRL_SUPERVISE_SHIFT;
- control->sar = 0;
- control->txseq = 0;
- } else {
- control->frame_type = 'i';
- control->sar = (enhanced & L2CAP_CTRL_SAR) >>
- L2CAP_CTRL_SAR_SHIFT;
- control->txseq = (enhanced & L2CAP_CTRL_TXSEQ) >>
- L2CAP_CTRL_TXSEQ_SHIFT;
- control->poll = 0;
- control->super = 0;
- }
- }
- static u32 __pack_extended_control(struct bt_l2cap_control *control)
- {
- u32 packed;
- packed = (control->reqseq << L2CAP_EXT_CTRL_REQSEQ_SHIFT) &
- L2CAP_EXT_CTRL_REQSEQ;
- packed |= (control->final << L2CAP_EXT_CTRL_FINAL_SHIFT) &
- L2CAP_EXT_CTRL_FINAL;
- if (control->frame_type == 's') {
- packed |= (control->poll << L2CAP_EXT_CTRL_POLL_SHIFT) &
- L2CAP_EXT_CTRL_POLL;
- packed |= (control->super << L2CAP_EXT_CTRL_SUPERVISE_SHIFT) &
- L2CAP_EXT_CTRL_SUPERVISE;
- packed |= L2CAP_EXT_CTRL_FRAME_TYPE;
- } else {
- packed |= (control->sar << L2CAP_EXT_CTRL_SAR_SHIFT) &
- L2CAP_EXT_CTRL_SAR;
- packed |= (control->txseq << L2CAP_EXT_CTRL_TXSEQ_SHIFT) &
- L2CAP_EXT_CTRL_TXSEQ;
- }
- return packed;
- }
- static void __get_extended_control(u32 extended,
- struct bt_l2cap_control *control)
- {
- control->reqseq = (extended & L2CAP_EXT_CTRL_REQSEQ) >>
- L2CAP_EXT_CTRL_REQSEQ_SHIFT;
- control->final = (extended & L2CAP_EXT_CTRL_FINAL) >>
- L2CAP_EXT_CTRL_FINAL_SHIFT;
- if (extended & L2CAP_EXT_CTRL_FRAME_TYPE) {
- control->frame_type = 's';
- control->poll = (extended & L2CAP_EXT_CTRL_POLL) >>
- L2CAP_EXT_CTRL_POLL_SHIFT;
- control->super = (extended & L2CAP_EXT_CTRL_SUPERVISE) >>
- L2CAP_EXT_CTRL_SUPERVISE_SHIFT;
- control->sar = 0;
- control->txseq = 0;
- } else {
- control->frame_type = 'i';
- control->sar = (extended & L2CAP_EXT_CTRL_SAR) >>
- L2CAP_EXT_CTRL_SAR_SHIFT;
- control->txseq = (extended & L2CAP_EXT_CTRL_TXSEQ) >>
- L2CAP_EXT_CTRL_TXSEQ_SHIFT;
- control->poll = 0;
- control->super = 0;
- }
- }
- static inline void l2cap_ertm_stop_ack_timer(struct l2cap_pinfo *pi)
- {
- BT_DBG("pi %p", pi);
- __cancel_delayed_work(&pi->ack_work);
- }
- static inline void l2cap_ertm_start_ack_timer(struct l2cap_pinfo *pi)
- {
- BT_DBG("pi %p, pending %d", pi, delayed_work_pending(&pi->ack_work));
- if (!delayed_work_pending(&pi->ack_work)) {
- queue_delayed_work(_l2cap_wq, &pi->ack_work,
- msecs_to_jiffies(L2CAP_DEFAULT_ACK_TO));
- }
- }
- static inline void l2cap_ertm_stop_retrans_timer(struct l2cap_pinfo *pi)
- {
- BT_DBG("pi %p", pi);
- __cancel_delayed_work(&pi->retrans_work);
- }
- static inline void l2cap_ertm_start_retrans_timer(struct l2cap_pinfo *pi)
- {
- BT_DBG("pi %p", pi);
- if (!delayed_work_pending(&pi->monitor_work) && pi->retrans_timeout) {
- __cancel_delayed_work(&pi->retrans_work);
- queue_delayed_work(_l2cap_wq, &pi->retrans_work,
- msecs_to_jiffies(pi->retrans_timeout));
- }
- }
- static inline void l2cap_ertm_stop_monitor_timer(struct l2cap_pinfo *pi)
- {
- BT_DBG("pi %p", pi);
- __cancel_delayed_work(&pi->monitor_work);
- }
- static inline void l2cap_ertm_start_monitor_timer(struct l2cap_pinfo *pi)
- {
- BT_DBG("pi %p", pi);
- l2cap_ertm_stop_retrans_timer(pi);
- __cancel_delayed_work(&pi->monitor_work);
- if (pi->monitor_timeout) {
- queue_delayed_work(_l2cap_wq, &pi->monitor_work,
- msecs_to_jiffies(pi->monitor_timeout));
- }
- }
- static u16 l2cap_alloc_cid(struct l2cap_chan_list *l)
- {
- u16 cid = L2CAP_CID_DYN_START;
- for (; cid < L2CAP_CID_DYN_END; cid++) {
- if (!__l2cap_get_chan_by_scid(l, cid))
- return cid;
- }
- return 0;
- }
- static inline void __l2cap_chan_link(struct l2cap_chan_list *l, struct sock *sk)
- {
- sock_hold(sk);
- if (l->head)
- l2cap_pi(l->head)->prev_c = sk;
- l2cap_pi(sk)->next_c = l->head;
- l2cap_pi(sk)->prev_c = NULL;
- l->head = sk;
- }
- static inline void l2cap_chan_unlink(struct l2cap_chan_list *l, struct sock *sk)
- {
- struct sock *next = l2cap_pi(sk)->next_c, *prev = l2cap_pi(sk)->prev_c;
- write_lock_bh(&l->lock);
- if (sk == l->head)
- l->head = next;
- if (next)
- l2cap_pi(next)->prev_c = prev;
- if (prev)
- l2cap_pi(prev)->next_c = next;
- write_unlock_bh(&l->lock);
- __sock_put(sk);
- }
- static void __l2cap_chan_add(struct l2cap_conn *conn, struct sock *sk)
- {
- struct l2cap_chan_list *l = &conn->chan_list;
- BT_DBG("conn %p, psm 0x%2.2x, dcid 0x%4.4x", conn,
- l2cap_pi(sk)->psm, l2cap_pi(sk)->dcid);
- conn->disc_reason = 0x13;
- l2cap_pi(sk)->conn = conn;
- if (!l2cap_pi(sk)->fixed_channel &&
- (sk->sk_type == SOCK_SEQPACKET || sk->sk_type == SOCK_STREAM)) {
- if (conn->hcon->type == LE_LINK) {
- /* LE connection */
- if (l2cap_pi(sk)->imtu < L2CAP_LE_DEFAULT_MTU)
- l2cap_pi(sk)->imtu = L2CAP_LE_DEFAULT_MTU;
- if (l2cap_pi(sk)->omtu < L2CAP_LE_DEFAULT_MTU)
- l2cap_pi(sk)->omtu = L2CAP_LE_DEFAULT_MTU;
- l2cap_pi(sk)->scid = L2CAP_CID_LE_DATA;
- l2cap_pi(sk)->dcid = L2CAP_CID_LE_DATA;
- } else {
- /* Alloc CID for connection-oriented socket */
- l2cap_pi(sk)->scid = l2cap_alloc_cid(l);
- l2cap_pi(sk)->omtu = L2CAP_DEFAULT_MTU;
- }
- } else if (sk->sk_type == SOCK_DGRAM) {
- /* Connectionless socket */
- l2cap_pi(sk)->scid = L2CAP_CID_CONN_LESS;
- l2cap_pi(sk)->dcid = L2CAP_CID_CONN_LESS;
- l2cap_pi(sk)->omtu = L2CAP_DEFAULT_MTU;
- } else if (sk->sk_type == SOCK_RAW) {
- /* Raw socket can send/recv signalling messages only */
- l2cap_pi(sk)->scid = L2CAP_CID_SIGNALING;
- l2cap_pi(sk)->dcid = L2CAP_CID_SIGNALING;
- l2cap_pi(sk)->omtu = L2CAP_DEFAULT_MTU;
- }
- if (l2cap_get_smallest_flushto(l) > l2cap_pi(sk)->flush_to) {
- /*if flush timeout of the channel is lesser than existing */
- l2cap_set_acl_flushto(conn->hcon, l2cap_pi(sk)->flush_to);
- }
- /* Otherwise, do not set scid/dcid/omtu. These will be set up
- * by l2cap_fixed_channel_config()
- */
- __l2cap_chan_link(l, sk);
- }
- /* Delete channel.
- * Must be called on the locked socket. */
- void l2cap_chan_del(struct sock *sk, int err)
- {
- struct l2cap_conn *conn = l2cap_pi(sk)->conn;
- struct sock *parent = bt_sk(sk)->parent;
- l2cap_sock_clear_timer(sk);
- BT_DBG("sk %p, conn %p, err %d", sk, conn, err);
- if (conn) {
- struct l2cap_chan_list *l = &conn->chan_list;
- /* Unlink from channel list */
- l2cap_chan_unlink(l, sk);
- l2cap_pi(sk)->conn = NULL;
- if (!l2cap_pi(sk)->fixed_channel)
- hci_conn_put(conn->hcon);
- read_lock(&l->lock);
- if (l2cap_pi(sk)->flush_to < l2cap_get_smallest_flushto(l))
- l2cap_set_acl_flushto(conn->hcon,
- l2cap_get_smallest_flushto(l));
- read_unlock(&l->lock);
- }
- if (l2cap_pi(sk)->ampchan) {
- struct hci_chan *ampchan = l2cap_pi(sk)->ampchan;
- struct hci_conn *ampcon = l2cap_pi(sk)->ampcon;
- l2cap_pi(sk)->ampchan = NULL;
- l2cap_pi(sk)->ampcon = NULL;
- l2cap_pi(sk)->amp_id = 0;
- if (hci_chan_put(ampchan))
- ampcon->l2cap_data = NULL;
- else
- l2cap_deaggregate(ampchan, l2cap_pi(sk));
- }
- sk->sk_state = BT_CLOSED;
- sock_set_flag(sk, SOCK_ZAPPED);
- if (err)
- sk->sk_err = err;
- if (parent) {
- bt_accept_unlink(sk);
- parent->sk_data_ready(parent, 0);
- } else
- sk->sk_state_change(sk);
- sk->sk_send_head = NULL;
- skb_queue_purge(TX_QUEUE(sk));
- if (l2cap_pi(sk)->mode == L2CAP_MODE_ERTM) {
- if (l2cap_pi(sk)->sdu)
- kfree_skb(l2cap_pi(sk)->sdu);
- skb_queue_purge(SREJ_QUEUE(sk));
- __cancel_delayed_work(&l2cap_pi(sk)->ack_work);
- __cancel_delayed_work(&l2cap_pi(sk)->retrans_work);
- __cancel_delayed_work(&l2cap_pi(sk)->monitor_work);
- }
- }
- static inline u8 l2cap_get_auth_type(struct sock *sk)
- {
- if (sk->sk_type == SOCK_RAW) {
- switch (l2cap_pi(sk)->sec_level) {
- case BT_SECURITY_VERY_HIGH:
- case BT_SECURITY_HIGH:
- return HCI_AT_DEDICATED_BONDING_MITM;
- case BT_SECURITY_MEDIUM:
- return HCI_AT_DEDICATED_BONDING;
- default:
- return HCI_AT_NO_BONDING;
- }
- } else if (l2cap_pi(sk)->psm == cpu_to_le16(0x0001)) {
- if (l2cap_pi(sk)->sec_level == BT_SECURITY_LOW)
- l2cap_pi(sk)->sec_level = BT_SECURITY_SDP;
- if (l2cap_pi(sk)->sec_level == BT_SECURITY_HIGH ||
- l2cap_pi(sk)->sec_level == BT_SECURITY_VERY_HIGH)
- return HCI_AT_NO_BONDING_MITM;
- else
- return HCI_AT_NO_BONDING;
- } else {
- switch (l2cap_pi(sk)->sec_level) {
- case BT_SECURITY_VERY_HIGH:
- case BT_SECURITY_HIGH:
- return HCI_AT_GENERAL_BONDING_MITM;
- case BT_SECURITY_MEDIUM:
- return HCI_AT_GENERAL_BONDING;
- default:
- return HCI_AT_NO_BONDING;
- }
- }
- }
- /* Service level security */
- static inline int l2cap_check_security(struct sock *sk)
- {
- struct l2cap_conn *conn = l2cap_pi(sk)->conn;
- __u8 auth_type;
- auth_type = l2cap_get_auth_type(sk);
- return hci_conn_security(conn->hcon, l2cap_pi(sk)->sec_level,
- auth_type);
- }
- u8 l2cap_get_ident(struct l2cap_conn *conn)
- {
- u8 id;
- /* Get next available identificator.
- * 1 - 128 are used by kernel.
- * 129 - 199 are reserved.
- * 200 - 254 are used by utilities like l2ping, etc.
- */
- spin_lock_bh(&conn->lock);
- if (++conn->tx_ident > 128)
- conn->tx_ident = 1;
- id = conn->tx_ident;
- spin_unlock_bh(&conn->lock);
- return id;
- }
- static void apply_fcs(struct sk_buff *skb)
- {
- size_t len;
- u16 partial_crc;
- struct sk_buff *iter;
- struct sk_buff *final_frag = skb;
- if (skb_has_frag_list(skb))
- len = skb_headlen(skb);
- else
- len = skb->len - L2CAP_FCS_SIZE;
- partial_crc = crc16(0, (u8 *) skb->data, len);
- skb_walk_frags(skb, iter) {
- len = iter->len;
- if (!iter->next)
- len -= L2CAP_FCS_SIZE;
- partial_crc = crc16(partial_crc, iter->data, len);
- final_frag = iter;
- }
- put_unaligned_le16(partial_crc,
- final_frag->data + final_frag->len - L2CAP_FCS_SIZE);
- }
- void l2cap_send_cmd(struct l2cap_conn *conn, u8 ident, u8 code, u16 len, void *data)
- {
- struct sk_buff *skb = l2cap_build_cmd(conn, code, ident, len, data);
- u8 flags;
- BT_DBG("code 0x%2.2x", code);
- if (!skb)
- return;
- if (conn->hcon == NULL || conn->hcon->hdev == NULL)
- return;
- if (lmp_no_flush_capable(conn->hcon->hdev))
- flags = ACL_START_NO_FLUSH;
- else
- flags = ACL_START;
- bt_cb(skb)->force_active = 1;
- hci_send_acl(conn->hcon, NULL, skb, flags);
- }
- static inline int __l2cap_no_conn_pending(struct sock *sk)
- {
- return !(l2cap_pi(sk)->conf_state & L2CAP_CONF_CONNECT_PEND);
- }
- static void l2cap_send_conn_req(struct sock *sk)
- {
- struct l2cap_conn_req req;
- req.scid = cpu_to_le16(l2cap_pi(sk)->scid);
- req.psm = l2cap_pi(sk)->psm;
- l2cap_pi(sk)->ident = l2cap_get_ident(l2cap_pi(sk)->conn);
- l2cap_send_cmd(l2cap_pi(sk)->conn, l2cap_pi(sk)->ident,
- L2CAP_CONN_REQ, sizeof(req), &req);
- }
- static void l2cap_send_create_chan_req(struct sock *sk, u8 amp_id)
- {
- struct l2cap_create_chan_req req;
- req.scid = cpu_to_le16(l2cap_pi(sk)->scid);
- req.psm = l2cap_pi(sk)->psm;
- req.amp_id = amp_id;
- l2cap_pi(sk)->conf_state |= L2CAP_CONF_LOCKSTEP;
- l2cap_pi(sk)->ident = l2cap_get_ident(l2cap_pi(sk)->conn);
- l2cap_send_cmd(l2cap_pi(sk)->conn, l2cap_pi(sk)->ident,
- L2CAP_CREATE_CHAN_REQ, sizeof(req), &req);
- }
- static void l2cap_do_start(struct sock *sk)
- {
- struct l2cap_conn *conn = l2cap_pi(sk)->conn;
- if (conn->info_state & L2CAP_INFO_FEAT_MASK_REQ_SENT) {
- if (!(conn->info_state & L2CAP_INFO_FEAT_MASK_REQ_DONE))
- return;
- if (l2cap_check_security(sk) && __l2cap_no_conn_pending(sk)) {
- l2cap_pi(sk)->conf_state |= L2CAP_CONF_CONNECT_PEND;
- if (l2cap_pi(sk)->amp_pref ==
- BT_AMP_POLICY_PREFER_AMP &&
- enable_hs &&
- conn->fc_mask & L2CAP_FC_A2MP)
- amp_create_physical(conn, sk);
- else
- l2cap_send_conn_req(sk);
- }
- } else {
- struct l2cap_info_req req;
- req.type = cpu_to_le16(L2CAP_IT_FEAT_MASK);
- conn->info_state |= L2CAP_INFO_FEAT_MASK_REQ_SENT;
- conn->info_ident = l2cap_get_ident(conn);
- mod_timer(&conn->info_timer, jiffies +
- msecs_to_jiffies(L2CAP_INFO_TIMEOUT));
- l2cap_send_cmd(conn, conn->info_ident,
- L2CAP_INFO_REQ, sizeof(req), &req);
- }
- }
- static inline int l2cap_mode_supported(__u8 mode, __u32 feat_mask)
- {
- u32 local_feat_mask = l2cap_feat_mask;
- if (!disable_ertm)
- local_feat_mask |= L2CAP_FEAT_ERTM | L2CAP_FEAT_STREAMING;
- switch (mode) {
- case L2CAP_MODE_ERTM:
- return L2CAP_FEAT_ERTM & feat_mask & local_feat_mask;
- case L2CAP_MODE_STREAMING:
- return L2CAP_FEAT_STREAMING & feat_mask & local_feat_mask;
- default:
- return 0x00;
- }
- }
- void l2cap_send_disconn_req(struct l2cap_conn *conn, struct sock *sk, int err)
- {
- struct l2cap_disconn_req req;
- if (!conn)
- return;
- sk->sk_send_head = NULL;
- skb_queue_purge(TX_QUEUE(sk));
- if (l2cap_pi(sk)->mode == L2CAP_MODE_ERTM) {
- skb_queue_purge(SREJ_QUEUE(sk));
- __cancel_delayed_work(&l2cap_pi(sk)->ack_work);
- __cancel_delayed_work(&l2cap_pi(sk)->retrans_work);
- __cancel_delayed_work(&l2cap_pi(sk)->monitor_work);
- }
- req.dcid = cpu_to_le16(l2cap_pi(sk)->dcid);
- req.scid = cpu_to_le16(l2cap_pi(sk)->scid);
- l2cap_send_cmd(conn, l2cap_get_ident(conn),
- L2CAP_DISCONN_REQ, sizeof(req), &req);
- sk->sk_state = BT_DISCONN;
- sk->sk_err = err;
- }
- /* ---- L2CAP connections ---- */
- static void l2cap_conn_start(struct l2cap_conn *conn)
- {
- struct l2cap_chan_list *l = &conn->chan_list;
- struct sock_del_list del, *tmp1, *tmp2;
- struct sock *sk;
- BT_DBG("conn %p", conn);
- INIT_LIST_HEAD(&del.list);
- read_lock(&l->lock);
- for (sk = l->head; sk; sk = l2cap_pi(sk)->next_c) {
- bh_lock_sock(sk);
- if (sk->sk_type != SOCK_SEQPACKET &&
- sk->sk_type != SOCK_STREAM) {
- bh_unlock_sock(sk);
- continue;
- }
- if (sk->sk_state == BT_CONNECT) {
- if (!l2cap_check_security(sk) ||
- !__l2cap_no_conn_pending(sk)) {
- bh_unlock_sock(sk);
- continue;
- }
- if (!l2cap_mode_supported(l2cap_pi(sk)->mode,
- conn->feat_mask)
- && l2cap_pi(sk)->conf_state &
- L2CAP_CONF_STATE2_DEVICE) {
- tmp1 = kzalloc(sizeof(struct sock_del_list),
- GFP_ATOMIC);
- tmp1->sk = sk;
- list_add_tail(&tmp1->list, &del.list);
- bh_unlock_sock(sk);
- continue;
- }
- l2cap_pi(sk)->conf_state |= L2CAP_CONF_CONNECT_PEND;
- if (l2cap_pi(sk)->amp_pref ==
- BT_AMP_POLICY_PREFER_AMP &&
- enable_hs &&
- conn->fc_mask & L2CAP_FC_A2MP)
- amp_create_physical(conn, sk);
- else
- l2cap_send_conn_req(sk);
- } else if (sk->sk_state == BT_CONNECT2) {
- struct l2cap_conn_rsp rsp;
- char buf[128];
- rsp.scid = cpu_to_le16(l2cap_pi(sk)->dcid);
- rsp.dcid = cpu_to_le16(l2cap_pi(sk)->scid);
- if (l2cap_check_security(sk)) {
- if (bt_sk(sk)->defer_setup) {
- struct sock *parent = bt_sk(sk)->parent;
- rsp.result = cpu_to_le16(L2CAP_CR_PEND);
- rsp.status = cpu_to_le16(L2CAP_CS_AUTHOR_PEND);
- if (parent)
- parent->sk_data_ready(parent, 0);
- } else {
- sk->sk_state = BT_CONFIG;
- rsp.result = cpu_to_le16(L2CAP_CR_SUCCESS);
- rsp.status = cpu_to_le16(L2CAP_CS_NO_INFO);
- }
- } else {
- rsp.result = cpu_to_le16(L2CAP_CR_PEND);
- rsp.status = cpu_to_le16(L2CAP_CS_AUTHEN_PEND);
- }
- if (rsp.result == cpu_to_le16(L2CAP_CR_SUCCESS) &&
- l2cap_pi(sk)->amp_id) {
- amp_accept_physical(conn,
- l2cap_pi(sk)->amp_id, sk);
- bh_unlock_sock(sk);
- continue;
- }
- l2cap_send_cmd(conn, l2cap_pi(sk)->ident,
- L2CAP_CONN_RSP, sizeof(rsp), &rsp);
- if (l2cap_pi(sk)->conf_state & L2CAP_CONF_REQ_SENT ||
- rsp.result != L2CAP_CR_SUCCESS) {
- bh_unlock_sock(sk);
- continue;
- }
- l2cap_pi(sk)->conf_state |= L2CAP_CONF_REQ_SENT;
- l2cap_send_cmd(conn, l2cap_get_ident(conn), L2CAP_CONF_REQ,
- l2cap_build_conf_req(sk, buf, sizeof(buf)), buf);
- l2cap_pi(sk)->num_conf_req++;
- }
- bh_unlock_sock(sk);
- }
- read_unlock(&l->lock);
- list_for_each_entry_safe(tmp1, tmp2, &del.list, list) {
- bh_lock_sock(tmp1->sk);
- __l2cap_sock_close(tmp1->sk, ECONNRESET);
- bh_unlock_sock(tmp1->sk);
- list_del(&tmp1->list);
- kfree(tmp1);
- }
- }
- /* Find socket with fixed cid with given source and destination bdaddrs.
- * Direction of the req/rsp must match.
- */
- struct sock *l2cap_find_sock_by_fixed_cid_and_dir(__le16 cid, bdaddr_t *src,
- bdaddr_t *dst, int incoming)
- {
- struct sock *sk = NULL, *sk1 = NULL;
- struct hlist_node *node;
- BT_DBG(" %d", incoming);
- read_lock(&l2cap_sk_list.lock);
- sk_for_each(sk, node, &l2cap_sk_list.head) {
- if (incoming && !l2cap_pi(sk)->incoming)
- continue;
- if (!incoming && l2cap_pi(sk)->incoming)
- continue;
- if (l2cap_pi(sk)->scid == cid && !bacmp(&bt_sk(sk)->dst, dst)) {
- /* Exact match. */
- if (!bacmp(&bt_sk(sk)->src, src))
- break;
- /* Closest match */
- if (!bacmp(&bt_sk(sk)->src, BDADDR_ANY))
- sk1 = sk;
- }
- }
- read_unlock(&l2cap_sk_list.lock);
- return node ? sk : sk1;
- }
- /* Find socket with cid and source bdaddr.
- * Returns closest match, locked.
- */
- static struct sock *l2cap_get_sock_by_scid(int state, __le16 cid, bdaddr_t *src)
- {
- struct sock *sk = NULL, *sk1 = NULL;
- struct hlist_node *node;
- read_lock(&l2cap_sk_list.lock);
- sk_for_each(sk, node, &l2cap_sk_list.head) {
- if (state && sk->sk_state != state)
- continue;
- if (l2cap_pi(sk)->scid == cid) {
- /* Exact match. */
- if (!bacmp(&bt_sk(sk)->src, src))
- break;
- /* Closest match */
- if (!bacmp(&bt_sk(sk)->src, BDADDR_ANY))
- sk1 = sk;
- }
- }
- read_unlock(&l2cap_sk_list.lock);
- return node ? sk : sk1;
- }
- static void l2cap_le_conn_ready(struct l2cap_conn *conn)
- {
- struct l2cap_chan_list *list = &conn->chan_list;
- struct sock *parent, *uninitialized_var(sk);
- BT_DBG("");
- /* Check if we have socket listening on cid */
- parent = l2cap_get_sock_by_scid(BT_LISTEN, L2CAP_CID_LE_DATA,
- conn->src);
- if (!parent)
- return;
- bh_lock_sock(parent);
- /* Check for backlog size */
- if (sk_acceptq_is_full(parent)) {
- BT_DBG("backlog full %d", parent->sk_ack_backlog);
- goto clean;
- }
- sk = l2cap_sock_alloc(sock_net(parent), NULL, BTPROTO_L2CAP, GFP_ATOMIC);
- if (!sk)
- goto clean;
- write_lock_bh(&list->lock);
- hci_conn_hold(conn->hcon);
- conn->hcon->disc_timeout = HCI_DISCONN_TIMEOUT;
- l2cap_sock_init(sk, parent);
- bacpy(&bt_sk(sk)->src, conn->src);
- bacpy(&bt_sk(sk)->dst, conn->dst);
- l2cap_pi(sk)->incoming = 1;
- bt_accept_enqueue(parent, sk);
- __l2cap_chan_add(conn, sk);
- sk->sk_state = BT_CONNECTED;
- parent->sk_data_ready(parent, 0);
- write_unlock_bh(&list->lock);
- clean:
- bh_unlock_sock(parent);
- }
- static void l2cap_conn_ready(struct l2cap_conn *conn)
- {
- struct l2cap_chan_list *l = &conn->chan_list;
- struct sock *sk;
- struct hci_conn *hcon = conn->hcon;
- BT_DBG("conn %p", conn);
- if (!hcon->out && hcon->type == LE_LINK)
- l2cap_le_conn_ready(conn);
- if (hcon->out && hcon->type == LE_LINK)
- smp_conn_security(hcon, hcon->pending_sec_level);
- read_lock(&l->lock);
- if (l->head) {
- for (sk = l->head; sk; sk = l2cap_pi(sk)->next_c) {
- bh_lock_sock(sk);
- if (hcon->type == LE_LINK) {
- u8 sec_level = l2cap_pi(sk)->sec_level;
- u8 pending_sec = hcon->pending_sec_level;
- if (pending_sec > sec_level)
- sec_level = pending_sec;
- if (smp_conn_security(hcon, sec_level))
- l2cap_chan_ready(sk);
- hci_conn_put(conn->hcon);
- } else if (sk->sk_type != SOCK_SEQPACKET &&
- sk->sk_type != SOCK_STREAM) {
- l2cap_sock_clear_timer(sk);
- sk->sk_state = BT_CONNECTED;
- sk->sk_state_change(sk);
- } else if (sk->sk_state == BT_CONNECT)
- l2cap_do_start(sk);
- bh_unlock_sock(sk);
- }
- } else if (conn->hcon->type == LE_LINK) {
- smp_conn_security(hcon, BT_SECURITY_HIGH);
- }
- read_unlock(&l->lock);
- if (conn->hcon->out && conn->hcon->type == LE_LINK)
- l2cap_le_conn_ready(conn);
- }
- /* Notify sockets that we cannot guaranty reliability anymore */
- static void l2cap_conn_unreliable(struct l2cap_conn *conn, int err)
- {
- struct l2cap_chan_list *l = &conn->chan_list;
- struct sock *sk;
- BT_DBG("conn %p", conn);
- read_lock(&l->lock);
- for (sk = l->head; sk; sk = l2cap_pi(sk)->next_c) {
- if (l2cap_pi(sk)->force_reliable)
- sk->sk_err = err;
- }
- read_unlock(&l->lock);
- }
- static void l2cap_info_timeout(unsigned long arg)
- {
- struct l2cap_conn *conn = (void *) arg;
- conn->info_state |= L2CAP_INFO_FEAT_MASK_REQ_DONE;
- conn->info_ident = 0;
- l2cap_conn_start(conn);
- }
- static struct l2cap_conn *l2cap_conn_add(struct hci_conn *hcon, u8 status)
- {
- struct l2cap_conn *conn = hcon->l2cap_data;
- if (conn || status)
- return conn;
- conn = kzalloc(sizeof(struct l2cap_conn), GFP_ATOMIC);
- if (!conn)
- return NULL;
- hcon->l2cap_data = conn;
- conn->hcon = hcon;
- BT_DBG("hcon %p conn %p", hcon, conn);
- if (hcon->hdev->le_mtu && hcon->type == LE_LINK)
- conn->mtu = hcon->hdev->le_mtu;
- else
- conn->mtu = hcon->hdev->acl_mtu;
- conn->src = &hcon->hdev->bdaddr;
- conn->dst = &hcon->dst;
- conn->feat_mask = 0;
- spin_lock_init(&conn->lock);
- rwlock_init(&conn->chan_list.lock);
- if (hcon->type == LE_LINK)
- setup_timer(&hcon->smp_timer, smp_timeout,
- (unsigned long) conn);
- else
- setup_timer(&conn->info_timer, l2cap_info_timeout,
- (unsigned long) conn);
- conn->disc_reason = 0x13;
- return conn;
- }
- static void l2cap_conn_del(struct hci_conn *hcon, int err, u8 is_process)
- {
- struct l2cap_conn *conn = hcon->l2cap_data;
- struct sock *sk;
- struct sock *next;
- if (!conn)
- return;
- BT_DBG("hcon %p conn %p, err %d", hcon, conn, err);
- if ((conn->hcon == hcon) && (conn->rx_skb))
- kfree_skb(conn->rx_skb);
- BT_DBG("conn->hcon %p", conn->hcon);
- /* Kill channels */
- for (sk = conn->chan_list.head; sk; ) {
- BT_DBG("ampcon %p", l2cap_pi(sk)->ampcon);
- if ((conn->hcon == hcon) || (l2cap_pi(sk)->ampcon == hcon)) {
- next = l2cap_pi(sk)->next_c;
- if (is_process)
- lock_sock(sk);
- else
- bh_lock_sock(sk);
- l2cap_chan_del(sk, err);
- if (is_process)
- release_sock(sk);
- else
- bh_unlock_sock(sk);
- l2cap_sock_kill(sk);
- sk = next;
- } else
- sk = l2cap_pi(sk)->next_c;
- }
- if (conn->hcon == hcon) {
- if (conn->info_state & L2CAP_INFO_FEAT_MASK_REQ_SENT)
- del_timer_sync(&conn->info_timer);
- hcon->l2cap_data = NULL;
- kfree(conn);
- }
- att_chn_params.conn = NULL;
- BT_DBG("att_chn_params.conn set to NULL");
- }
- static inline void l2cap_chan_add(struct l2cap_conn *conn, struct sock *sk)
- {
- struct l2cap_chan_list *l = &conn->chan_list;
- write_lock_bh(&l->lock);
- __l2cap_chan_add(conn, sk);
- write_unlock_bh(&l->lock);
- }
- /* ---- Socket interface ---- */
- /* Find socket with psm and source bdaddr.
- * Returns closest match.
- */
- static struct sock *l2cap_get_sock_by_psm(int state, __le16 psm, bdaddr_t *src)
- {
- struct sock *sk = NULL, *sk1 = NULL;
- struct hlist_node *node;
- read_lock(&l2cap_sk_list.lock);
- sk_for_each(sk, node, &l2cap_sk_list.head) {
- if (state && sk->sk_state != state)
- continue;
- if (l2cap_pi(sk)->psm == psm) {
- /* Exact match. */
- if (!bacmp(&bt_sk(sk)->src, src))
- break;
- /* Closest match */
- if (!bacmp(&bt_sk(sk)->src, BDADDR_ANY))
- sk1 = sk;
- }
- }
- read_unlock(&l2cap_sk_list.lock);
- return node ? sk : sk1;
- }
- int l2cap_do_connect(struct sock *sk)
- {
- bdaddr_t *src = &bt_sk(sk)->src;
- bdaddr_t *dst = &bt_sk(sk)->dst;
- struct l2cap_conn *conn;
- struct hci_conn *hcon;
- struct hci_dev *hdev;
- __u8 auth_type;
- int err;
- BT_DBG("%s -> %s psm 0x%2.2x", batostr(src), batostr(dst),
- l2cap_pi(sk)->psm);
- hdev = hci_get_route(dst, src);
- if (!hdev)
- return -EHOSTUNREACH;
- hci_dev_lock_bh(hdev);
- auth_type = l2cap_get_auth_type(sk);
- if (l2cap_pi(sk)->fixed_channel) {
- /* Fixed channels piggyback on existing ACL connections */
- hcon = hci_conn_hash_lookup_ba(hdev, ACL_LINK, dst);
- if (!hcon || !hcon->l2cap_data) {
- err = -ENOTCONN;
- goto done;
- }
- conn = hcon->l2cap_data;
- } else {
- if (l2cap_pi(sk)->dcid == L2CAP_CID_LE_DATA)
- hcon = hci_le_connect(hdev, 0, dst,
- l2cap_pi(sk)->sec_level, auth_type,
- &bt_sk(sk)->le_params);
- else
- hcon = hci_connect(hdev, ACL_LINK, 0, dst,
- l2cap_pi(sk)->sec_level, auth_type);
- if (IS_ERR(hcon)) {
- err = PTR_ERR(hcon);
- goto done;
- }
- conn = l2cap_conn_add(hcon, 0);
- if (!conn) {
- hci_conn_put(hcon);
- err = -ENOMEM;
- goto done;
- }
- }
- /* Update source addr of the socket */
- bacpy(src, conn->src);
- l2cap_chan_add(conn, sk);
- if ((l2cap_pi(sk)->fixed_channel) ||
- (l2cap_pi(sk)->dcid == L2CAP_CID_LE_DATA &&
- hcon->state == BT_CONNECTED)) {
- sk->sk_state = BT_CONNECTED;
- sk->sk_state_change(sk);
- } else {
- sk->sk_state = BT_CONNECT;
- /* If we have valid LE Params, let timeout override default */
- if (l2cap_pi(sk)->dcid == L2CAP_CID_LE_DATA &&
- l2cap_sock_le_params_valid(&bt_sk(sk)->le_params)) {
- u16 timeout = bt_sk(sk)->le_params.conn_timeout;
- if (timeout)
- l2cap_sock_set_timer(sk,
- msecs_to_jiffies(timeout*1000));
- } else
- l2cap_sock_set_timer(sk, sk->sk_sndtimeo);
- sk->sk_state_change(sk);
- if (hcon->state == BT_CONNECTED) {
- if (sk->sk_type != SOCK_SEQPACKET &&
- sk->sk_type != SOCK_STREAM) {
- l2cap_sock_clear_timer(sk);
- if (l2cap_check_security(sk)) {
- sk->sk_state = BT_CONNECTED;
- sk->sk_state_change(sk);
- }
- } else
- l2cap_do_start(sk);
- }
- }
- err = 0;
- done:
- hci_dev_unlock_bh(hdev);
- hci_dev_put(hdev);
- return err;
- }
- int __l2cap_wait_ack(struct sock *sk)
- {
- DECLARE_WAITQUEUE(wait, current);
- int err = 0;
- int timeo = HZ/5;
- add_wait_queue(sk_sleep(sk), &wait);
- while (l2cap_pi(sk)->unacked_frames > 0 && l2cap_pi(sk)->conn &&
- atomic_read(&l2cap_pi(sk)->ertm_queued)) {
- set_current_state(TASK_INTERRUPTIBLE);
- if (!timeo)
- timeo = HZ/5;
- if (signal_pending(current)) {
- err = sock_intr_errno(timeo);
- break;
- }
- release_sock(sk);
- timeo = schedule_timeout(timeo);
- lock_sock(sk);
- err = sock_error(sk);
- if (err)
- break;
- }
- set_current_state(TASK_RUNNING);
- remove_wait_queue(sk_sleep(sk), &wait);
- return err;
- }
- static void l2cap_ertm_tx_worker(struct work_struct *work)
- {
- struct l2cap_pinfo *pi =
- container_of(work, struct l2cap_pinfo, tx_work);
- struct sock *sk = (struct sock *)pi;
- BT_DBG("%p", pi);
- lock_sock(sk);
- l2cap_ertm_send(sk);
- release_sock(sk);
- sock_put(sk);
- }
- static void l2cap_skb_destructor(struct sk_buff *skb)
- {
- struct sock *sk = skb->sk;
- int queued;
- int keep_sk = 0;
- queued = atomic_sub_return(1, &l2cap_pi(sk)->ertm_queued);
- if (queued < L2CAP_MIN_ERTM_QUEUED)
- keep_sk = queue_work(_l2cap_wq, &l2cap_pi(sk)->tx_work);
- if (!keep_sk)
- sock_put(sk);
- }
- void l2cap_do_send(struct sock *sk, struct sk_buff *skb)
- {
- struct l2cap_pinfo *pi = l2cap_pi(sk);
- BT_DBG("sk %p, skb %p len %d", sk, skb, skb->len);
- if (pi->ampcon && (pi->amp_move_state == L2CAP_AMP_STATE_STABLE ||
- pi->amp_move_state == L2CAP_AMP_STATE_WAIT_PREPARE)) {
- BT_DBG("Sending on AMP connection %p %p",
- pi->ampcon, pi->ampchan);
- if (pi->ampchan)
- hci_send_acl(pi->ampcon, pi->ampchan, skb,
- ACL_COMPLETE);
- else
- kfree_skb(skb);
- } else {
- u16 flags;
- if (!(pi->conn)) {
- kfree_skb(skb);
- return;
- }
- bt_cb(skb)->force_active = pi->force_active;
- BT_DBG("Sending on BR/EDR connection %p", pi->conn->hcon);
- if (lmp_no_flush_capable(pi->conn->hcon->hdev) &&
- !l2cap_pi(sk)->flushable)
- flags = ACL_START_NO_FLUSH;
- else
- flags = ACL_START;
- hci_send_acl(pi->conn->hcon, NULL, skb, flags);
- }
- }
- int l2cap_ertm_send(struct sock *sk)
- {
- struct sk_buff *skb, *tx_skb;
- struct l2cap_pinfo *pi = l2cap_pi(sk);
- struct bt_l2cap_control *control;
- int sent = 0;
- BT_DBG("sk %p", sk);
- if (sk->sk_state != BT_CONNECTED)
- return -ENOTCONN;
- if (pi->conn_state & L2CAP_CONN_REMOTE_BUSY)
- return 0;
- if (pi->amp_move_state != L2CAP_AMP_STATE_STABLE &&
- pi->amp_move_state != L2CAP_AMP_STATE_WAIT_PREPARE)
- return 0;
- while (sk->sk_send_head && (pi->unacked_frames < pi->remote_tx_win) &&
- atomic_read(&pi->ertm_queued) < L2CAP_MAX_ERTM_QUEUED &&
- (pi->tx_state == L2CAP_ERTM_TX_STATE_XMIT)) {
- skb = sk->sk_send_head;
- bt_cb(skb)->retries = 1;
- control = &bt_cb(skb)->control;
- if (pi->conn_state & L2CAP_CONN_SEND_FBIT) {
- control->final = 1;
- pi->conn_state &= ~L2CAP_CONN_SEND_FBIT;
- }
- control->reqseq = pi->buffer_seq;
- pi->last_acked_seq = pi->buffer_seq;
- control->txseq = pi->next_tx_seq;
- if (pi->extended_control) {
- put_unaligned_le32(__pack_extended_control(control),
- skb->data + L2CAP_HDR_SIZE);
- } else {
- put_unaligned_le16(__pack_enhanced_control(control),
- skb->data + L2CAP_HDR_SIZE);
- }
- if (pi->fcs == L2CAP_FCS_CRC16)
- apply_fcs(skb);
- /* Clone after data has been modified. Data is assumed to be
- read-only (for locking purposes) on cloned sk_buffs.
- */
- tx_skb = skb_clone(skb, GFP_ATOMIC);
- if (!tx_skb)
- break;
- sock_hold(sk);
- tx_skb->sk = sk;
- tx_skb->destructor = l2cap_skb_destructor;
- atomic_inc(&pi->ertm_queued);
- l2cap_ertm_start_retrans_timer(pi);
- pi->next_tx_seq = __next_seq(pi->next_tx_seq, pi);
- pi->unacked_frames += 1;
- pi->frames_sent += 1;
- sent += 1;
- if (skb_queue_is_last(TX_QUEUE(sk), skb))
- sk->sk_send_head = NULL;
- else
- sk->sk_send_head = skb_queue_next(TX_QUEUE(sk), skb);
- l2cap_do_send(sk, tx_skb);
- BT_DBG("Sent txseq %d", (int)control->txseq);
- }
- BT_DBG("Sent %d, %d unacked, %d in ERTM queue, %d in HCI queue", sent,
- (int) pi->unacked_frames, skb_queue_len(TX_QUEUE(sk)),
- atomic_read(&pi->ertm_queued));
- return sent;
- }
- int l2cap_strm_tx(struct sock *sk, struct sk_buff_head *skbs)
- {
- struct sk_buff *skb;
- struct l2cap_pinfo *pi = l2cap_pi(sk);
- struct bt_l2cap_control *control;
- int sent = 0;
- BT_DBG("sk %p, skbs %p", sk, skbs);
- if (sk->sk_state != BT_CONNECTED)
- return -ENOTCONN;
- if (pi->amp_move_state != L2CAP_AMP_STATE_STABLE &&
- pi->amp_move_state != L2CAP_AMP_STATE_WAIT_PREPARE)
- return 0;
- skb_queue_splice_tail_init(skbs, TX_QUEUE(sk));
- BT_DBG("skb queue empty 0x%2.2x", skb_queue_empty(TX_QUEUE(sk)));
- while (!skb_queue_empty(TX_QUEUE(sk))) {
- skb = skb_dequeue(TX_QUEUE(sk));
- BT_DBG("skb %p", skb);
- bt_cb(skb)->retries = 1;
- control = &bt_cb(skb)->control;
- BT_DBG("control %p", control);
- control->reqseq = 0;
- control->txseq = pi->next_tx_seq;
- if (pi->extended_control) {
- put_unaligned_le32(__pack_extended_control(control),
- skb->data + L2CAP_HDR_SIZE);
- } else {
- put_unaligned_le16(__pack_enhanced_control(control),
- skb->data + L2CAP_HDR_SIZE);
- }
- if (pi->fcs == L2CAP_FCS_CRC16)
- apply_fcs(skb);
- l2cap_do_send(sk, skb);
- BT_DBG("Sent txseq %d", (int)control->txseq);
- pi->next_tx_seq = __next_seq(pi->next_tx_seq, pi);
- pi->frames_sent += 1;
- sent += 1;
- }
- BT_DBG("Sent %d", sent);
- return 0;
- }
- static int memcpy_fromkvec(unsigned char *kdata, struct kvec *iv, int len)
- {
- while (len > 0) {
- if (iv->iov_len) {
- int copy = min_t(unsigned int, len, iv->iov_len);
- memcpy(kdata, iv->iov_base, copy);
- len -= copy;
- kdata += copy;
- iv->iov_base += copy;
- iv->iov_len -= copy;
- }
- iv++;
- }
- return 0;
- }
- static inline int l2cap_skbuff_fromiovec(struct sock *sk, struct msghdr *msg,
- int len, int count, struct sk_buff *skb,
- int reseg)
- {
- struct l2cap_conn *conn = l2cap_pi(sk)->conn;
- struct sk_buff **frag;
- struct sk_buff *final;
- int err, sent = 0;
- BT_DBG("sk %p, msg %p, len %d, count %d, skb %p", sk,
- msg, (int)len, (int)count, skb);
- if (!conn)
- return -ENOTCONN;
- /* When resegmenting, data is copied from kernel space */
- if (reseg) {
- err = memcpy_fromkvec(skb_put(skb, count),
- (struct kvec *) msg->msg_iov, count);
- } else {
- err = memcpy_fromiovec(skb_put(skb, count), msg->msg_iov,
- count);
- }
- if (err)
- return -EFAULT;
- sent += count;
- len -= count;
- final = skb;
- /* Continuation fragments (no L2CAP header) */
- frag = &skb_shinfo(skb)->frag_list;
- while (len) {
- int skblen;
- count = min_t(unsigned int, conn->mtu, len);
- /* Add room for the FCS if it fits */
- if (bt_cb(skb)->control.fcs == L2CAP_FCS_CRC16 &&
- len + L2CAP_FCS_SIZE <= conn->mtu)
- skblen = count + L2CAP_FCS_SIZE;
- else
- skblen = count;
- /* Don't use bt_skb_send_alloc() while resegmenting, since
- * it is not ok to block.
- */
- if (reseg) {
- *frag = bt_skb_alloc(skblen, GFP_ATOMIC);
- if (*frag)
- skb_set_owner_w(*frag, sk);
- } else {
- *frag = bt_skb_send_alloc(sk, skblen,
- msg->msg_flags & MSG_DONTWAIT, &err);
- }
- if (!*frag)
- return -EFAULT;
- /* When resegmenting, data is copied from kernel space */
- if (reseg) {
- err = memcpy_fromkvec(skb_put(*frag, count),
- (struct kvec *) msg->msg_iov,
- count);
- } else {
- err = memcpy_fromiovec(skb_put(*frag, count),
- msg->msg_iov, count);
- }
- if (err)
- return -EFAULT;
- sent += count;
- len -= count;
- final = *frag;
- frag = &(*frag)->next;
- }
- if (bt_cb(skb)->control.fcs == L2CAP_FCS_CRC16) {
- if (skb_tailroom(final) < L2CAP_FCS_SIZE) {
- if (reseg) {
- *frag = bt_skb_alloc(L2CAP_FCS_SIZE,
- GFP_ATOMIC);
- if (*frag)
- skb_set_owner_w(*frag, sk);
- } else {
- *frag = bt_skb_send_alloc(sk, L2CAP_FCS_SIZE,
- msg->msg_flags & MSG_DONTWAIT,
- &err);
- }
- if (!*frag)
- return -EFAULT;
- final = *frag;
- }
- skb_put(final, L2CAP_FCS_SIZE);
- }
- return sent;
- }
- struct sk_buff *l2cap_create_connless_pdu(struct sock *sk, struct msghdr *msg, size_t len)
- {
- struct l2cap_conn *conn = l2cap_pi(sk)->conn;
- struct sk_buff *skb;
- int err, count, hlen = L2CAP_HDR_SIZE + 2;
- struct l2cap_hdr *lh;
- BT_DBG("sk %p len %d", sk, (int)len);
- count = min_t(unsigned int, (conn->mtu - hlen), len);
- skb = bt_skb_send_alloc(sk, count + hlen,
- msg->msg_flags & MSG_DONTWAIT, &err);
- if (!skb)
- return ERR_PTR(err);
- /* Create L2CAP header */
- lh = (struct l2cap_hdr *) skb_put(skb, L2CAP_HDR_SIZE);
- lh->cid = cpu_to_le16(l2cap_pi(sk)->dcid);
- lh->len = cpu_to_le16(len + (hlen - L2CAP_HDR_SIZE));
- put_unaligned_le16(l2cap_pi(sk)->psm, skb_put(skb, 2));
- err = l2cap_skbuff_fromiovec(sk, msg, len, count, skb, 0);
- if (unlikely(err < 0)) {
- kfree_skb(skb);
- return ERR_PTR(err);
- }
- return skb;
- }
- struct sk_buff *l2cap_create_basic_pdu(struct sock *sk, struct msghdr *msg, size_t len)
- {
- struct l2cap_conn *conn = l2cap_pi(sk)->conn;
- struct sk_buff *skb;
- int err, count, hlen = L2CAP_HDR_SIZE;
- struct l2cap_hdr *lh;
- BT_DBG("sk %p len %d", sk, (int)len);
- count = min_t(unsigned int, (conn->mtu - hlen), len);
- skb = bt_skb_send_alloc(sk, count + hlen,
- msg->msg_flags & MSG_DONTWAIT, &err);
- if (!skb)
- return ERR_PTR(err);
- /* Create L2CAP header */
- lh = (struct l2cap_hdr *) skb_put(skb, L2CAP_HDR_SIZE);
- lh->cid = cpu_to_le16(l2cap_pi(sk)->dcid);
- lh->len = cpu_to_le16(len + (hlen - L2CAP_HDR_SIZE));
- err = l2cap_skbuff_fromiovec(sk, msg, len, count, skb, 0);
- if (unlikely(err < 0)) {
- kfree_skb(skb);
- return ERR_PTR(err);
- }
- return skb;
- }
- struct sk_buff *l2cap_create_iframe_pdu(struct sock *sk,
- struct msghdr *msg, size_t len,
- u16 sdulen, int reseg)
- {
- struct sk_buff *skb;
- int err, count, hlen;
- int reserve = 0;
- struct l2cap_hdr *lh;
- u8 fcs = l2cap_pi(sk)->fcs;
- if (l2cap_pi(sk)->extended_control)
- hlen = L2CAP_EXTENDED_HDR_SIZE;
- else
- hlen = L2CAP_ENHANCED_HDR_SIZE;
- if (sdulen)
- hlen += L2CAP_SDULEN_SIZE;
- if (fcs == L2CAP_FCS_CRC16)
- hlen += L2CAP_FCS_SIZE;
- BT_DBG("sk %p, msg %p, len %d, sdulen %d, hlen %d",
- sk, msg, (int)len, (int)sdulen, hlen);
- count = min_t(unsigned int, (l2cap_pi(sk)->conn->mtu - hlen), len);
- /* Allocate extra headroom for Qualcomm PAL. This is only
- * necessary in two places (here and when creating sframes)
- * because only unfragmented iframes and sframes are sent
- * using AMP controllers.
- */
- if (l2cap_pi(sk)->ampcon &&
- l2cap_pi(sk)->ampcon->hdev->manufacturer == 0x001d)
- reserve = BT_SKB_RESERVE_80211;
- /* Don't use bt_skb_send_alloc() while resegmenting, since
- * it is not ok to block.
- */
- if (reseg) {
- skb = bt_skb_alloc(count + hlen + reserve, GFP_ATOMIC);
- if (skb)
- skb_set_owner_w(skb, sk);
- } else {
- skb = bt_skb_send_alloc(sk, count + hlen + reserve,
- msg->msg_flags & MSG_DONTWAIT, &err);
- }
- if (!skb)
- return ERR_PTR(err);
- if (reserve)
- skb_reserve(skb, reserve);
- bt_cb(skb)->control.fcs = fcs;
- /* Create L2CAP header */
- lh = (struct l2cap_hdr *) skb_put(skb, L2CAP_HDR_SIZE);
- lh->cid = cpu_to_le16(l2cap_pi(sk)->dcid);
- lh->len = cpu_to_le16(len + hlen - L2CAP_HDR_SIZE);
- /* Control header is populated later */
- if (l2cap_pi(sk)->extended_control)
- put_unaligned_le32(0, skb_put(skb, 4));
- else
- put_unaligned_le16(0, skb_put(skb, 2));
- if (sdulen)
- put_unaligned_le16(sdulen, skb_put(skb, L2CAP_SDULEN_SIZE));
- err = l2cap_skbuff_fromiovec(sk, msg, len, count, skb, reseg);
- if (unlikely(err < 0)) {
- BT_DBG("err %d", err);
- kfree_skb(skb);
- return ERR_PTR(err);
- }
- bt_cb(skb)->retries = 0;
- return skb;
- }
- static void l2cap_ertm_process_reqseq(struct sock *sk, u16 reqseq)
- {
- struct l2cap_pinfo *pi;
- struct sk_buff *acked_skb;
- u16 ackseq;
- BT_DBG("sk %p, reqseq %d", sk, (int) reqseq);
- pi = l2cap_pi(sk);
- if (pi->unacked_frames == 0 || reqseq == pi->expected_ack_seq)
- return;
- BT_DBG("expected_ack_seq %d, unacked_frames %d",
- (int) pi->expected_ack_seq, (int) pi->unacked_frames);
- for (ackseq = pi->expected_ack_seq; ackseq != reqseq;
- ackseq = __next_seq(ackseq, pi)) {
- acked_skb = l2cap_ertm_seq_in_queue(TX_QUEUE(sk), ackseq);
- if (acked_skb) {
- skb_unlink(acked_skb, TX_QUEUE(sk));
- kfree_skb(acked_skb);
- pi->unacked_frames--;
- }
- }
- pi->expected_ack_seq = reqseq;
- if (pi->unacked_frames == 0)
- l2cap_ertm_stop_retrans_timer(pi);
- BT_DBG("unacked_frames %d", (int) pi->unacked_frames);
- }
- static struct sk_buff *l2cap_create_sframe_pdu(struct sock *sk, u32 control)
- {
- struct sk_buff *skb;
- int len;
- int reserve = 0;
- struct l2cap_hdr *lh;
- if (l2cap_pi(sk)->extended_control)
- len = L2CAP_EXTENDED_HDR_SIZE;
- else
- len = L2CAP_ENHANCED_HDR_SIZE;
- if (l2cap_pi(sk)->fcs == L2CAP_FCS_CRC16)
- len += L2CAP_FCS_SIZE;
- /* Allocate extra headroom for Qualcomm PAL */
- if (l2cap_pi(sk)->ampcon &&
- l2cap_pi(sk)->ampcon->hdev->manufacturer == 0x001d)
- reserve = BT_SKB_RESERVE_80211;
- skb = bt_skb_alloc(len + reserve, GFP_ATOMIC);
- if (!skb)
- return ERR_PTR(-ENOMEM);
- if (reserve)
- skb_reserve(skb, reserve);
- lh = (struct l2cap_hdr *) skb_put(skb, L2CAP_HDR_SIZE);
- lh->cid = cpu_to_le16(l2cap_pi(sk)->dcid);
- lh->len = cpu_to_le16(len - L2CAP_HDR_SIZE);
- if (l2cap_pi(sk)->extended_control)
- put_unaligned_le32(control, skb_put(skb, 4));
- else
- put_unaligned_le16(control, skb_put(skb, 2));
- if (l2cap_pi(sk)->fcs == L2CAP_FCS_CRC16) {
- u16 fcs = crc16(0, (u8 *) skb->data, skb->len);
- put_unaligned_le16(fcs, skb_put(skb, L2CAP_FCS_SIZE));
- }
- return skb;
- }
- static void l2cap_ertm_send_sframe(struct sock *sk,
- struct bt_l2cap_control *control)
- {
- struct l2cap_pinfo *pi;
- struct sk_buff *skb;
- u32 control_field;
- BT_DBG("sk %p, control %p", sk, control);
- if (control->frame_type != 's')
- return;
- pi = l2cap_pi(sk);
- if (pi->amp_move_state != L2CAP_AMP_STATE_STABLE &&
- pi->amp_move_state != L2CAP_AMP_STATE_WAIT_PREPARE &&
- pi->amp_move_state != L2CAP_AMP_STATE_RESEGMENT) {
- BT_DBG("AMP error - attempted S-Frame send during AMP move");
- return;
- }
- if ((pi->conn_state & L2CAP_CONN_SEND_FBIT) && !control->poll) {
- control->final = 1;
- pi->conn_state &= ~L2CAP_CONN_SEND_FBIT;
- }
- if (control->super == L2CAP_SFRAME_RR)
- pi->conn_state &= ~L2CAP_CONN_SENT_RNR;
- else if (control->super == L2CAP_SFRAME_RNR)
- pi->conn_state |= L2CAP_CONN_SENT_RNR;
- if (control->super != L2CAP_SFRAME_SREJ) {
- pi->last_acked_seq = control->reqseq;
- l2cap_ertm_stop_ack_timer(pi);
- }
- BT_DBG("reqseq %d, final %d, poll %d, super %d", (int) control->reqseq,
- (int) control->final, (int) control->poll,
- (int) control->super);
- if (pi->extended_control)
- control_field = __pack_extended_control(control);
- else
- control_field = __pack_enhanced_control(control);
- skb = l2cap_create_sframe_pdu(sk, control_field);
- if (!IS_ERR(skb))
- l2cap_do_send(sk, skb);
- }
- static void l2cap_ertm_send_ack(struct sock *sk)
- {
- struct l2cap_pinfo *pi = l2cap_pi(sk);
- struct bt_l2cap_control control;
- u16 frames_to_ack = __delta_seq(pi->buffer_seq, pi->last_acked_seq, pi);
- int threshold;
- BT_DBG("sk %p", sk);
- BT_DBG("last_acked_seq %d, buffer_seq %d", (int)pi->last_acked_seq,
- (int)pi->buffer_seq);
- memset(&control, 0, sizeof(control));
- control.frame_type = 's';
- if ((pi->conn_state & L2CAP_CONN_LOCAL_BUSY) &&
- pi->rx_state == L2CAP_ERTM_RX_STATE_RECV) {
- l2cap_ertm_stop_ack_timer(pi);
- control.super = L2CAP_SFRAME_RNR;
- control.reqseq = pi->buffer_seq;
- l2cap_ertm_send_sframe(sk, &control);
- } else {
- if (!(pi->conn_state & L2CAP_CONN_REMOTE_BUSY)) {
- l2cap_ertm_send(sk);
- /* If any i-frames were sent, they included an ack */
- if (pi->buffer_seq == pi->last_acked_seq)
- frames_to_ack = 0;
- }
- /* Ack now if the window is 3/4ths full.
- * Calculate without mul or div
- */
- threshold = pi->ack_win;
- threshold += threshold << 1;
- threshold >>= 2;
- BT_DBG("frames_to_ack %d, threshold %d", (int)frames_to_ack,
- threshold);
- if (frames_to_ack >= threshold) {
- l2cap_ertm_stop_ack_timer(pi);
- control.super = L2CAP_SFRAME_RR;
- control.reqseq = pi->buffer_seq;
- l2cap_ertm_send_sframe(sk, &control);
- frames_to_ack = 0;
- }
- if (frames_to_ack)
- l2cap_ertm_start_ack_timer(pi);
- }
- }
- static void l2cap_ertm_send_rr_or_rnr(struct sock *sk, bool poll)
- {
- struct l2cap_pinfo *pi;
- struct bt_l2cap_control control;
- BT_DBG("sk %p, poll %d", sk, (int) poll);
- pi = l2cap_pi(sk);
- memset(&control, 0, sizeof(control));
- control.frame_type = 's';
- control.poll = poll;
- if (pi->conn_state & L2CAP_CONN_LOCAL_BUSY)
- control.super = L2CAP_SFRAME_RNR;
- else
- control.super = L2CAP_SFRAME_RR;
- control.reqseq = pi->buffer_seq;
- l2cap_ertm_send_sframe(sk, &control);
- }
- static void l2cap_ertm_send_i_or_rr_or_rnr(struct sock *sk)
- {
- struct l2cap_pinfo *pi;
- struct bt_l2cap_control control;
- BT_DBG("sk %p", sk);
- pi = l2cap_pi(sk);
- memset(&control, 0, sizeof(control));
- control.frame_type = 's';
- control.final = 1;
- control.reqseq = pi->buffer_seq;
- pi->conn_state |= L2CAP_CONN_SEND_FBIT;
- if (pi->conn_state & L2CAP_CONN_LOCAL_BUSY) {
- control.super = L2CAP_SFRAME_RNR;
- l2cap_ertm_send_sframe(sk, &control);
- }
- if ((pi->conn_state & L2CAP_CONN_REMOTE_BUSY) &&
- (pi->unacked_frames > 0))
- l2cap_ertm_start_retrans_timer(pi);
- pi->conn_state &= ~L2CAP_CONN_REMOTE_BUSY;
- /* Send pending iframes */
- l2cap_ertm_send(sk);
- if (pi->conn_state & L2CAP_CONN_SEND_FBIT) {
- /* F-bit wasn't sent in an s-frame or i-frame yet, so
- * send it now.
- */
- control.super = L2CAP_SFRAME_RR;
- l2cap_ertm_send_sframe(sk, &control);
- }
- }
- static void l2cap_ertm_send_srej(struct sock *sk, u16 txseq)
- {
- struct bt_l2cap_control control;
- struct l2cap_pinfo *pi;
- u16 seq;
- BT_DBG("sk %p, txseq %d", sk, (int)txseq);
- pi = l2cap_pi(sk);
- memset(&control, 0, sizeof(control));
- control.frame_type = 's';
- control.super = L2CAP_SFRAME_SREJ;
- for (seq = pi->expected_tx_seq; seq != txseq;
- seq = __next_seq(seq, pi)) {
- if (!l2cap_ertm_seq_in_queue(SREJ_QUEUE(pi), seq)) {
- control.reqseq = seq;
- l2cap_ertm_send_sframe(sk, &control);
- l2cap_seq_list_append(&pi->srej_list, seq);
- }
- }
- pi->expected_tx_seq = __next_seq(txseq, pi);
- }
- static void l2cap_ertm_send_srej_tail(struct sock *sk)
- {
- struct bt_l2cap_control control;
- struct l2cap_pinfo *pi;
- BT_DBG("sk %p", sk);
- pi = l2cap_pi(sk);
- if (pi->srej_list.tail == L2CAP_SEQ_LIST_CLEAR)
- return;
- memset(&control, 0, sizeof(control));
- control.frame_type = 's';
- control.super = L2CAP_SFRAME_SREJ;
- control.reqseq = pi->srej_list.tail;
- l2cap_ertm_send_sframe(sk, &control);
- }
- static void l2cap_ertm_send_srej_list(struct sock *sk, u16 txseq)
- {
- struct bt_l2cap_control control;
- struct l2cap_pinfo *pi;
- u16 initial_head;
- u16 seq;
- BT_DBG("sk %p, txseq %d", sk, (int) txseq);
- pi = l2cap_pi(sk);
- memset(&control, 0, sizeof(control));
- control.frame_type = 's';
- control.super = L2CAP_SFRAME_SREJ;
- /* Capture initial list head to allow only one pass through the list. */
- initial_head = pi->srej_list.head;
- do {
- seq = l2cap_seq_list_pop(&pi->srej_list);
- if ((seq == txseq) || (seq == L2CAP_SEQ_LIST_CLEAR))
- break;
- control.reqseq = seq;
- l2cap_ertm_send_sframe(sk, &control);
- l2cap_seq_list_append(&pi->srej_list, seq);
- } while (pi->srej_list.head != initial_head);
- }
- static void l2cap_ertm_abort_rx_srej_sent(struct sock *sk)
- {
- struct l2cap_pinfo *pi = l2cap_pi(sk);
- BT_DBG("sk %p", sk);
- pi->expected_tx_seq = pi->buffer_seq;
- l2cap_seq_list_clear(&l2cap_pi(sk)->srej_list);
- skb_queue_purge(SREJ_QUEUE(sk));
- pi->rx_state = L2CAP_ERTM_RX_STATE_RECV;
- }
- static int l2cap_ertm_tx_state_xmit(struct sock *sk,
- struct bt_l2cap_control *control,
- struct sk_buff_head *skbs, u8 event)
- {
- struct l2cap_pinfo *pi;
- int err = 0;
- BT_DBG("sk %p, control %p, skbs %p, event %d", sk, control, skbs,
- (int)event);
- pi = l2cap_pi(sk);
- switch (event) {
- case L2CAP_ERTM_EVENT_DATA_REQUEST:
- if (sk->sk_send_head == NULL)
- sk->sk_send_head = skb_peek(skbs);
- skb_queue_splice_tail_init(skbs, TX_QUEUE(sk));
- l2cap_ertm_send(sk);
- break;
- case L2CAP_ERTM_EVENT_LOCAL_BUSY_DETECTED:
- BT_DBG("Enter LOCAL_BUSY");
- pi->conn_state |= L2CAP_CONN_LOCAL_BUSY;
- if (pi->rx_state == L2CAP_ERTM_RX_STATE_SREJ_SENT) {
- /* The SREJ_SENT state must be aborted if we are to
- * enter the LOCAL_BUSY state.
- */
- l2cap_ertm_abort_rx_srej_sent(sk);
- }
- l2cap_ertm_send_ack(sk);
- break;
- case L2CAP_ERTM_EVENT_LOCAL_BUSY_CLEAR:
- BT_DBG("Exit LOCAL_BUSY");
- pi->conn_state &= ~L2CAP_CONN_LOCAL_BUSY;
- if (pi->amp_move_state == L2CAP_AMP_STATE_WAIT_LOCAL_BUSY) {
- if (pi->amp_move_role == L2CAP_AMP_MOVE_INITIATOR) {
- pi->amp_move_state =
- L2CAP_AMP_STATE_WAIT_MOVE_CONFIRM_RSP;
- l2cap_send_move_chan_cfm(pi->conn, pi,
- pi->scid,
- L2CAP_MOVE_CHAN_CONFIRMED);
- l2cap_sock_set_timer(sk, L2CAP_MOVE_TIMEOUT);
- } else if (pi->amp_move_role ==
- L2CAP_AMP_MOVE_RESPONDER) {
- pi->amp_move_state =
- L2CAP_AMP_STATE_WAIT_MOVE_CONFIRM;
- l2cap_send_move_chan_rsp(pi->conn,
- pi->amp_move_cmd_ident,
- pi->dcid,
- L2CAP_MOVE_CHAN_SUCCESS);
- }
- break;
- }
- if (pi->amp_move_role == L2CAP_AMP_MOVE_NONE &&
- (pi->conn_state & L2CAP_CONN_SENT_RNR)) {
- struct bt_l2cap_control local_control;
- memset(&local_control, 0, sizeof(local_control));
- local_control.frame_type = 's';
- local_control.super = L2CAP_SFRAME_RR;
- local_control.poll = 1;
- local_control.reqseq = pi->buffer_seq;
- l2cap_ertm_send_sframe(sk, &local_control);
- pi->retry_count = 1;
- l2cap_ertm_start_monitor_timer(pi);
- pi->tx_state = L2CAP_ERTM_TX_STATE_WAIT_F;
- }
- break;
- case L2CAP_ERTM_EVENT_RECV_REQSEQ_AND_FBIT:
- l2cap_ertm_process_reqseq(sk, control->reqseq);
- break;
- case L2CAP_ERTM_EVENT_EXPLICIT_POLL:
- l2cap_ertm_send_rr_or_rnr(sk, 1);
- pi->retry_count = 1;
- l2cap_ertm_start_monitor_timer(pi);
- l2cap_ertm_stop_ack_timer(pi);
- pi->tx_state = L2CAP_ERTM_TX_STATE_WAIT_F;
- break;
- case L2CAP_ERTM_EVENT_RETRANS_TIMER_EXPIRES:
- l2cap_ertm_send_rr_or_rnr(sk, 1);
- pi->retry_count = 1;
- l2cap_ertm_start_monitor_timer(pi);
- pi->tx_state = L2CAP_ERTM_TX_STATE_WAIT_F;
- break;
- case L2CAP_ERTM_EVENT_RECV_FBIT:
- /* Nothing to process */
- break;
- default:
- break;
- }
- return err;
- }
- static int l2cap_ertm_tx_state_wait_f(struct sock *sk,
- struct bt_l2cap_control *control,
- struct sk_buff_head *skbs, u8 event)
- {
- struct l2cap_pinfo *pi;
- int err = 0;
- BT_DBG("sk %p, control %p, skbs %p, event %d", sk, control, skbs,
- (int)event);
- pi = l2cap_pi(sk);
- switch (event) {
- case L2CAP_ERTM_EVENT_DATA_REQUEST:
- if (sk->sk_send_head == NULL)
- sk->sk_send_head = skb_peek(skbs);
- /* Queue data, but don't send. */
- skb_queue_splice_tail_init(skbs, TX_QUEUE(sk));
- break;
- case L2CAP_ERTM_EVENT_LOCAL_BUSY_DETECTED:
- BT_DBG("Enter LOCAL_BUSY");
- pi->conn_state |= L2CAP_CONN_LOCAL_BUSY;
- if (pi->rx_state == L2CAP_ERTM_RX_STATE_SREJ_SENT) {
- /* The SREJ_SENT state must be aborted if we are to
- * enter the LOCAL_BUSY state.
- */
- l2cap_ertm_abort_rx_srej_sent(sk);
- }
- l2cap_ertm_send_ack(sk);
- break;
- case L2CAP_ERTM_EVENT_LOCAL_BUSY_CLEAR:
- BT_DBG("Exit LOCAL_BUSY");
- pi->conn_state &= ~L2CAP_CONN_LOCAL_BUSY;
- if (pi->conn_state & L2CAP_CONN_SENT_RNR) {
- struct bt_l2cap_control local_control;
- memset(&local_control, 0, sizeof(local_control));
- local_control.frame_type = 's';
- local_control.super = L2CAP_SFRAME_RR;
- local_control.poll = 1;
- local_control.reqseq = pi->buffer_seq;
- l2cap_ertm_send_sframe(sk, &local_control);
- pi->retry_count = 1;
- l2cap_ertm_start_monitor_timer(pi);
- pi->tx_state = L2CAP_ERTM_TX_STATE_WAIT_F;
- }
- break;
- case L2CAP_ERTM_EVENT_RECV_REQSEQ_AND_FBIT:
- l2cap_ertm_process_reqseq(sk, control->reqseq);
- /* Fall through */
- case L2CAP_ERTM_EVENT_RECV_FBIT:
- if (control && control->final) {
- l2cap_ertm_stop_monitor_timer(pi);
- if (pi->unacked_frames > 0)
- l2cap_ertm_start_retrans_timer(pi);
- pi->retry_count = 0;
- pi->tx_state = L2CAP_ERTM_TX_STATE_XMIT;
- BT_DBG("recv fbit tx_state 0x2.2%x", pi->tx_state);
- }
- break;
- case L2CAP_ERTM_EVENT_EXPLICIT_POLL:
- /* Ignore */
- break;
- case L2CAP_ERTM_EVENT_MONITOR_TIMER_EXPIRES:
- if ((pi->max_tx == 0) || (pi->retry_count < pi->max_tx)) {
- l2cap_ertm_send_rr_or_rnr(sk, 1);
- l2cap_ertm_start_monitor_timer(pi);
- pi->retry_count += 1;
- } else
- l2cap_send_disconn_req(pi->conn, sk, ECONNABORTED);
- break;
- default:
- break;
- }
- return err;
- }
- int l2cap_ertm_tx(struct sock *sk, struct bt_l2cap_control *control,
- struct sk_buff_head *skbs, u8 event)
- {
- struct l2cap_pinfo *pi;
- int err = 0;
- BT_DBG("sk %p, control %p, skbs %p, event %d, state %d",
- sk, control, skbs, (int)event, l2cap_pi(sk)->tx_state);
- pi = l2cap_pi(sk);
- switch (pi->tx_state) {
- case L2CAP_ERTM_TX_STATE_XMIT:
- err = l2cap_ertm_tx_state_xmit(sk, control, skbs, event);
- break;
- case L2CAP_ERTM_TX_STATE_WAIT_F:
- err = l2cap_ertm_tx_state_wait_f(sk, control, skbs, event);
- break;
- default:
- /* Ignore event */
- break;
- }
- return err;
- }
- int l2cap_segment_sdu(struct sock *sk, struct sk_buff_head* seg_queue,
- struct msghdr *msg, size_t len, int reseg)
- {
- struct sk_buff *skb;
- u16 sdu_len;
- size_t pdu_len;
- int err = 0;
- u8 sar;
- BT_DBG("sk %p, msg %p, len %d", sk, msg, (int)len);
- /* It is critical that ERTM PDUs fit in a single HCI fragment,
- * so fragmented skbs are not used. The HCI layer's handling
- * of fragmented skbs is not compatible with ERTM's queueing.
- */
- /* PDU size is derived from the HCI MTU */
- pdu_len = l2cap_pi(sk)->conn->mtu;
- /* Constrain BR/EDR PDU size to fit within the largest radio packet */
- if (!l2cap_pi(sk)->ampcon)
- pdu_len = min_t(size_t, pdu_len, L2CAP_BREDR_MAX_PAYLOAD);
- /* Adjust for largest possible L2CAP overhead. */
- pdu_len -= L2CAP_EXTENDED_HDR_SIZE + L2CAP_FCS_SIZE;
- /* Remote device may have requested smaller PDUs */
- pdu_len = min_t(size_t, pdu_len, l2cap_pi(sk)->remote_mps);
- if (len <= pdu_len) {
- sar = L2CAP_SAR_UNSEGMENTED;
- sdu_len = 0;
- pdu_len = len;
- } else {
- sar = L2CAP_SAR_START;
- sdu_len = len;
- pdu_len -= L2CAP_SDULEN_SIZE;
- }
- while (len) {
- skb = l2cap_create_iframe_pdu(sk, msg, pdu_len, sdu_len, reseg);
- BT_DBG("iframe skb %p", skb);
- if (IS_ERR(skb)) {
- __skb_queue_purge(seg_queue);
- return PTR_ERR(skb);
- }
- bt_cb(skb)->control.sar = sar;
- __skb_queue_tail(seg_queue, skb);
- len -= pdu_len;
- if (sdu_len) {
- sdu_len = 0;
- pdu_len += L2CAP_SDULEN_SIZE;
- }
- if (len <= pdu_len) {
- sar = L2CAP_SAR_END;
- pdu_len = len;
- } else {
- sar = L2CAP_SAR_CONTINUE;
- }
- }
- return err;
- }
- static inline int is_initial_frame(u8 sar)
- {
- return (sar == L2CAP_SAR_UNSEGMENTED ||
- sar == L2CAP_SAR_START);
- }
- static inline int l2cap_skbuff_to_kvec(struct sk_buff *skb, struct kvec *iv,
- size_t veclen)
- {
- struct sk_buff *frag_iter;
- BT_DBG("skb %p (len %d), iv %p", skb, (int)skb->len, iv);
- if (iv->iov_len + skb->len > veclen)
- return -ENOMEM;
- memcpy(iv->iov_base + iv->iov_len, skb->data, skb->len);
- iv->iov_len += skb->len;
- skb_walk_frags(skb, frag_iter) {
- if (iv->iov_len + skb->len > veclen)
- return -ENOMEM;
- BT_DBG("Copying %d bytes", (int)frag_iter->len);
- memcpy(iv->iov_base + iv->iov_len, frag_iter->data,
- frag_iter->len);
- iv->iov_len += frag_iter->len;
- }
- return 0;
- }
- int l2cap_resegment_queue(struct sock *sk, struct sk_buff_head *queue)
- {
- void *buf;
- int buflen;
- int err = 0;
- struct sk_buff *skb;
- struct msghdr msg;
- struct kvec iv;
- struct sk_buff_head old_frames;
- struct l2cap_pinfo *pi = l2cap_pi(sk);
- BT_DBG("sk %p", sk);
- if (skb_queue_empty(queue))
- return 0;
- memset(&msg, 0, sizeof(msg));
- msg.msg_iov = (struct iovec *) &iv;
- buflen = pi->omtu + L2CAP_FCS_SIZE;
- buf = kzalloc(buflen, GFP_TEMPORARY);
- if (!buf) {
- BT_DBG("Could not allocate resegmentation buffer");
- return -ENOMEM;
- }
- /* Move current frames off the original queue */
- __skb_queue_head_init(&old_frames);
- skb_queue_splice_tail_init(queue, &old_frames);
- while (!skb_queue_empty(&old_frames)) {
- struct sk_buff_head current_sdu;
- u8 original_sar;
- /* Reassemble each SDU from one or more PDUs */
- iv.iov_base = buf;
- iv.iov_len = 0;
- skb = skb_peek(&old_frames);
- original_sar = bt_cb(skb)->control.sar;
- __skb_unlink(skb, &old_frames);
- /* Append data to SDU */
- if (pi->extended_control)
- skb_pull(skb, L2CAP_EXTENDED_HDR_SIZE);
- else
- skb_pull(skb, L2CAP_ENHANCED_HDR_SIZE);
- if (original_sar == L2CAP_SAR_START)
- skb_pull(skb, L2CAP_SDULEN_SIZE);
- err = l2cap_skbuff_to_kvec(skb, &iv, buflen);
- if (bt_cb(skb)->control.fcs == L2CAP_FCS_CRC16)
- iv.iov_len -= L2CAP_FCS_SIZE;
- /* Free skb */
- kfree_skb(skb);
- if (err)
- break;
- while (!skb_queue_empty(&old_frames) && !err) {
- /* Check next frame */
- skb = skb_peek(&old_frames);
- if (is_initial_frame(bt_cb(skb)->control.sar))
- break;
- __skb_unlink(skb, &old_frames);
- /* Append data to SDU */
- if (pi->extended_control)
- skb_pull(skb, L2CAP_EXTENDED_HDR_SIZE);
- else
- skb_pull(skb, L2CAP_ENHANCED_HDR_SIZE);
- if (bt_cb(skb)->control.sar == L2CAP_SAR_START)
- skb_pull(skb, L2CAP_SDULEN_SIZE);
- err = l2cap_skbuff_to_kvec(skb, &iv, buflen);
- if (bt_cb(skb)->control.fcs == L2CAP_FCS_CRC16)
- iv.iov_len -= L2CAP_FCS_SIZE;
- /* Free skb */
- kfree_skb(skb);
- }
- if (err)
- break;
- /* Segment data */
- __skb_queue_head_init(¤t_sdu);
- /* skbs for the SDU were just freed, but the
- * resegmenting process could produce more, smaller
- * skbs due to smaller PDUs and reduced HCI MTU. The
- * overhead from the sk_buff structs could put us over
- * the sk_sndbuf limit.
- *
- * Since this code is running in response to a
- * received poll/final packet, it cannot block.
- * Therefore, memory allocation needs to be allowed by
- * falling back to bt_skb_alloc() (with
- * skb_set_owner_w() to maintain sk_wmem_alloc
- * correctly).
- */
- msg.msg_iovlen = iv.iov_len;
- err = l2cap_segment_sdu(sk, ¤t_sdu, &msg,
- msg.msg_iovlen, 1);
- if (err || skb_queue_empty(¤t_sdu)) {
- BT_DBG("Error %d resegmenting data for socket %p",
- err, sk);
- __skb_queue_purge(¤t_sdu);
- break;
- }
- /* Fix up first PDU SAR bits */
- if (!is_initial_frame(original_sar)) {
- BT_DBG("Changing SAR bits, %d PDUs",
- skb_queue_len(¤t_sdu));
- skb = skb_peek(¤t_sdu);
- if (skb_queue_len(¤t_sdu) == 1) {
- /* Change SAR from 'unsegmented' to 'end' */
- bt_cb(skb)->control.sar = L2CAP_SAR_END;
- } else {
- struct l2cap_hdr *lh;
- size_t hdrlen;
- /* Change SAR from 'start' to 'continue' */
- bt_cb(skb)->control.sar = L2CAP_SAR_CONTINUE;
- /* Start frames contain 2 bytes for
- * sdulen and continue frames don't.
- * Must rewrite header to eliminate
- * sdulen and then adjust l2cap frame
- * length.
- */
- if (pi->extended_control)
- hdrlen = L2CAP_EXTENDED_HDR_SIZE;
- else
- hdrlen = L2CAP_ENHANCED_HDR_SIZE;
- memmove(skb->data + L2CAP_SDULEN_SIZE,
- skb->data, hdrlen);
- skb_pull(skb, L2CAP_SDULEN_SIZE);
- lh = (struct l2cap_hdr *)skb->data;
- lh->len = cpu_to_le16(le16_to_cpu(lh->len) -
- L2CAP_SDULEN_SIZE);
- }
- }
- /* Add to queue */
- skb_queue_splice_tail(¤t_sdu, queue);
- }
- __skb_queue_purge(&old_frames);
- if (err)
- __skb_queue_purge(queue);
- kfree(buf);
- BT_DBG("Queue resegmented, err=%d", err);
- return err;
- }
- static void l2cap_resegment_worker(struct work_struct *work)
- {
- int err = 0;
- struct l2cap_resegment_work *seg_work =
- container_of(work, struct l2cap_resegment_work, work);
- struct sock *sk = seg_work->sk;
- kfree(seg_work);
- BT_DBG("sk %p", sk);
- lock_sock(sk);
- if (l2cap_pi(sk)->amp_move_state != L2CAP_AMP_STATE_RESEGMENT) {
- release_sock(sk);
- sock_put(sk);
- return;
- }
- err = l2cap_resegment_queue(sk, TX_QUEUE(sk));
- l2cap_pi(sk)->amp_move_state = L2CAP_AMP_STATE_STABLE;
- if (skb_queue_empty(TX_QUEUE(sk)))
- sk->sk_send_head = NULL;
- else
- sk->sk_send_head = skb_peek(TX_QUEUE(sk));
- if (err)
- l2cap_send_disconn_req(l2cap_pi(sk)->conn, sk, ECONNRESET);
- else
- l2cap_ertm_send(sk);
- release_sock(sk);
- sock_put(sk);
- }
- static int l2cap_setup_resegment(struct sock *sk)
- {
- struct l2cap_resegment_work *seg_work;
- BT_DBG("sk %p", sk);
- if (skb_queue_empty(TX_QUEUE(sk)))
- return 0;
- seg_work = kzalloc(sizeof(*seg_work), GFP_ATOMIC);
- if (!seg_work)
- return -ENOMEM;
- INIT_WORK(&seg_work->work, l2cap_resegment_worker);
- sock_hold(sk);
- seg_work->sk = sk;
- if (!queue_work(_l2cap_wq, &seg_work->work)) {
- kfree(seg_work);
- sock_put(sk);
- return -ENOMEM;
- }
- l2cap_pi(sk)->amp_move_state = L2CAP_AMP_STATE_RESEGMENT;
- return 0;
- }
- static inline int l2cap_rmem_available(struct sock *sk)
- {
- BT_DBG("sk_rmem_alloc %d, sk_rcvbuf %d",
- atomic_read(&sk->sk_rmem_alloc), sk->sk_rcvbuf);
- return atomic_read(&sk->sk_rmem_alloc) < sk->sk_rcvbuf / 3;
- }
- static inline int l2cap_rmem_full(struct sock *sk)
- {
- BT_DBG("sk_rmem_alloc %d, sk_rcvbuf %d",
- atomic_read(&sk->sk_rmem_alloc), sk->sk_rcvbuf);
- return atomic_read(&sk->sk_rmem_alloc) > (2 * sk->sk_rcvbuf) / 3;
- }
- void l2cap_amp_move_init(struct sock *sk)
- {
- BT_DBG("sk %p", sk);
- if (!l2cap_pi(sk)->conn)
- return;
- if (!(l2cap_pi(sk)->conn->fc_mask & L2CAP_FC_A2MP) || !enable_hs)
- return;
- if (l2cap_pi(sk)->amp_id == 0) {
- if (l2cap_pi(sk)->amp_pref != BT_AMP_POLICY_PREFER_AMP)
- return;
- l2cap_pi(sk)->amp_move_role = L2CAP_AMP_MOVE_INITIATOR;
- l2cap_pi(sk)->amp_move_state = L2CAP_AMP_STATE_WAIT_PREPARE;
- amp_create_physical(l2cap_pi(sk)->conn, sk);
- } else {
- l2cap_pi(sk)->amp_move_role = L2CAP_AMP_MOVE_INITIATOR;
- l2cap_pi(sk)->amp_move_state =
- L2CAP_AMP_STATE_WAIT_MOVE_RSP_SUCCESS;
- l2cap_pi(sk)->amp_move_id = 0;
- l2cap_amp_move_setup(sk);
- l2cap_send_move_chan_req(l2cap_pi(sk)->conn,
- l2cap_pi(sk), l2cap_pi(sk)->scid, 0);
- l2cap_sock_set_timer(sk, L2CAP_MOVE_TIMEOUT);
- }
- }
- static void l2cap_chan_ready(struct sock *sk)
- {
- struct sock *parent = bt_sk(sk)->parent;
- BT_DBG("sk %p, parent %p", sk, parent);
- l2cap_pi(sk)->conf_state = 0;
- l2cap_sock_clear_timer(sk);
- if (!parent) {
- /* Outgoing channel.
- * Wake up socket sleeping on connect.
- */
- sk->sk_state = BT_CONNECTED;
- sk->sk_state_change(sk);
- } else {
- /* Incoming channel.
- * Wake up socket sleeping on accept.
- */
- parent->sk_data_ready(parent, 0);
- }
- }
- /* Copy frame to all raw sockets on that connection */
- static void l2cap_raw_recv(struct l2cap_conn *conn, struct sk_buff *skb)
- {
- struct l2cap_chan_list *l = &conn->chan_list;
- struct sk_buff *nskb;
- struct sock *sk;
- BT_DBG("conn %p", conn);
- read_lock(&l->lock);
- for (sk = l->head; sk; sk = l2cap_pi(sk)->next_c) {
- if (sk->sk_type != SOCK_RAW)
- continue;
- /* Don't send frame to the socket it came from */
- if (skb->sk == sk)
- continue;
- nskb = skb_clone(skb, GFP_ATOMIC);
- if (!nskb)
- continue;
- if (sock_queue_rcv_skb(sk, nskb))
- kfree_skb(nskb);
- }
- read_unlock(&l->lock);
- }
- /* ---- L2CAP signalling commands ---- */
- static struct sk_buff *l2cap_build_cmd(struct l2cap_conn *conn,
- u8 code, u8 ident, u16 dlen, void *data)
- {
- struct sk_buff *skb, **frag;
- struct l2cap_cmd_hdr *cmd;
- struct l2cap_hdr *lh;
- int len, count;
- unsigned int mtu = conn->hcon->hdev->acl_mtu;
- BT_DBG("conn %p, code 0x%2.2x, ident 0x%2.2x, len %d",
- conn, code, ident, dlen);
- if (conn->mtu < L2CAP_HDR_SIZE + L2CAP_CMD_HDR_SIZE)
- return NULL;
- len = L2CAP_HDR_SIZE + L2CAP_CMD_HDR_SIZE + dlen;
- count = min_t(unsigned int, mtu, len);
- skb = bt_skb_alloc(count, GFP_ATOMIC);
- if (!skb)
- return NULL;
- lh = (struct l2cap_hdr *) skb_put(skb, L2CAP_HDR_SIZE);
- lh->len = cpu_to_le16(L2CAP_CMD_HDR_SIZE + dlen);
- if (conn->hcon->type == LE_LINK)
- lh->cid = cpu_to_le16(L2CAP_CID_LE_SIGNALING);
- else
- lh->cid = cpu_to_le16(L2CAP_CID_SIGNALING);
- cmd = (struct l2cap_cmd_hdr *) skb_put(skb, L2CAP_CMD_HDR_SIZE);
- cmd->code = code;
- cmd->ident = ident;
- cmd->len = cpu_to_le16(dlen);
- if (dlen) {
- count -= L2CAP_HDR_SIZE + L2CAP_CMD_HDR_SIZE;
- memcpy(skb_put(skb, count), data, count);
- data += count;
- }
- len -= skb->len;
- /* Continuation fragments (no L2CAP header) */
- frag = &skb_shinfo(skb)->frag_list;
- while (len) {
- count = min_t(unsigned int, mtu, len);
- *frag = bt_skb_alloc(count, GFP_ATOMIC);
- if (!*frag)
- goto fail;
- memcpy(skb_put(*frag, count), data, count);
- len -= count;
- data += count;
- frag = &(*frag)->next;
- }
- return skb;
- fail:
- kfree_skb(skb);
- return NULL;
- }
- static inline int l2cap_get_conf_opt(void **ptr, int *type, int *olen, unsigned long *val)
- {
- struct l2cap_conf_opt *opt = *ptr;
- int len;
- len = L2CAP_CONF_OPT_SIZE + opt->len;
- *ptr += len;
- *type = opt->type;
- *olen = opt->len;
- switch (opt->len) {
- case 1:
- *val = *((u8 *) opt->val);
- break;
- case 2:
- *val = get_unaligned_le16(opt->val);
- break;
- case 4:
- *val = get_unaligned_le32(opt->val);
- break;
- default:
- *val = (unsigned long) opt->val;
- break;
- }
- BT_DBG("type 0x%2.2x len %d val 0x%lx", *type, opt->len, *val);
- return len;
- }
- static void l2cap_add_conf_opt(void **ptr, u8 type, u8 len, unsigned long val, size_t size)
- {
- struct l2cap_conf_opt *opt = *ptr;
- BT_DBG("type 0x%2.2x len %d val 0x%lx", type, len, val);
- if (size < L2CAP_CONF_OPT_SIZE + len)
- return;
- opt->type = type;
- opt->len = len;
- switch (len) {
- case 1:
- *((u8 *) opt->val) = val;
- break;
- case 2:
- put_unaligned_le16(val, opt->val);
- break;
- case 4:
- put_unaligned_le32(val, opt->val);
- break;
- default:
- memcpy(opt->val, (void *) val, len);
- break;
- }
- *ptr += L2CAP_CONF_OPT_SIZE + len;
- }
- static void l2cap_ertm_ack_timeout(struct work_struct *work)
- {
- struct delayed_work *delayed =
- container_of(work, struct delayed_work, work);
- struct l2cap_pinfo *pi =
- container_of(delayed, struct l2cap_pinfo, ack_work);
- struct sock *sk = (struct sock *)pi;
- u16 frames_to_ack;
- BT_DBG("sk %p", sk);
- if (!sk)
- return;
- lock_sock(sk);
- if (!l2cap_pi(sk)->conn) {
- release_sock(sk);
- return;
- }
- frames_to_ack = __delta_seq(l2cap_pi(sk)->buffer_seq,
- l2cap_pi(sk)->last_acked_seq,
- l2cap_pi(sk));
- if (frames_to_ack)
- l2cap_ertm_send_rr_or_rnr(sk, 0);
- release_sock(sk);
- }
- static void l2cap_ertm_retrans_timeout(struct work_struct *work)
- {
- struct delayed_work *delayed =
- container_of(work, struct delayed_work, work);
- struct l2cap_pinfo *pi =
- container_of(delayed, struct l2cap_pinfo, retrans_work);
- struct sock *sk = (struct sock *)pi;
- BT_DBG("sk %p", sk);
- if (!sk)
- return;
- lock_sock(sk);
- if (!l2cap_pi(sk)->conn) {
- release_sock(sk);
- return;
- }
- l2cap_ertm_tx(sk, 0, 0, L2CAP_ERTM_EVENT_RETRANS_TIMER_EXPIRES);
- release_sock(sk);
- }
- static void l2cap_ertm_monitor_timeout(struct work_struct *work)
- {
- struct delayed_work *delayed =
- container_of(work, struct delayed_work, work);
- struct l2cap_pinfo *pi =
- container_of(delayed, struct l2cap_pinfo, monitor_work);
- struct sock *sk = (struct sock *)pi;
- BT_DBG("sk %p", sk);
- if (!sk)
- return;
- lock_sock(sk);
- if (!l2cap_pi(sk)->conn) {
- release_sock(sk);
- return;
- }
- l2cap_ertm_tx(sk, 0, 0, L2CAP_ERTM_EVENT_MONITOR_TIMER_EXPIRES);
- release_sock(sk);
- }
- static inline void l2cap_ertm_init(struct sock *sk)
- {
- l2cap_pi(sk)->next_tx_seq = 0;
- l2cap_pi(sk)->expected_tx_seq = 0;
- l2cap_pi(sk)->expected_ack_seq = 0;
- l2cap_pi(sk)->unacked_frames = 0;
- l2cap_pi(sk)->buffer_seq = 0;
- l2cap_pi(sk)->frames_sent = 0;
- l2cap_pi(sk)->last_acked_seq = 0;
- l2cap_pi(sk)->sdu = NULL;
- l2cap_pi(sk)->sdu_last_frag = NULL;
- l2cap_pi(sk)->sdu_len = 0;
- atomic_set(&l2cap_pi(sk)->ertm_queued, 0);
- l2cap_pi(sk)->rx_state = L2CAP_ERTM_RX_STATE_RECV;
- l2cap_pi(sk)->tx_state = L2CAP_ERTM_TX_STATE_XMIT;
- BT_DBG("tx_state 0x2.2%x rx_state 0x2.2%x", l2cap_pi(sk)->tx_state,
- l2cap_pi(sk)->rx_state);
- l2cap_pi(sk)->amp_id = 0;
- l2cap_pi(sk)->amp_move_state = L2CAP_AMP_STATE_STABLE;
- l2cap_pi(sk)->amp_move_role = L2CAP_AMP_MOVE_NONE;
- l2cap_pi(sk)->amp_move_reqseq = 0;
- l2cap_pi(sk)->amp_move_event = 0;
- INIT_DELAYED_WORK(&l2cap_pi(sk)->ack_work, l2cap_ertm_ack_timeout);
- INIT_DELAYED_WORK(&l2cap_pi(sk)->retrans_work,
- l2cap_ertm_retrans_timeout);
- INIT_DELAYED_WORK(&l2cap_pi(sk)->monitor_work,
- l2cap_ertm_monitor_timeout);
- INIT_WORK(&l2cap_pi(sk)->tx_work, l2cap_ertm_tx_worker);
- skb_queue_head_init(SREJ_QUEUE(sk));
- skb_queue_head_init(TX_QUEUE(sk));
- l2cap_seq_list_init(&l2cap_pi(sk)->srej_list, l2cap_pi(sk)->tx_win);
- l2cap_seq_list_init(&l2cap_pi(sk)->retrans_list,
- l2cap_pi(sk)->remote_tx_win);
- }
- void l2cap_ertm_destruct(struct sock *sk)
- {
- l2cap_seq_list_free(&l2cap_pi(sk)->srej_list);
- l2cap_seq_list_free(&l2cap_pi(sk)->retrans_list);
- }
- void l2cap_ertm_shutdown(struct sock *sk)
- {
- l2cap_ertm_stop_ack_timer(l2cap_pi(sk));
- l2cap_ertm_stop_retrans_timer(l2cap_pi(sk));
- l2cap_ertm_stop_monitor_timer(l2cap_pi(sk));
- }
- void l2cap_ertm_recv_done(struct sock *sk)
- {
- lock_sock(sk);
- if (l2cap_pi(sk)->mode != L2CAP_MODE_ERTM ||
- sk->sk_state != BT_CONNECTED) {
- release_sock(sk);
- return;
- }
- /* Consume any queued incoming frames and update local busy status */
- if (l2cap_pi(sk)->rx_state == L2CAP_ERTM_RX_STATE_SREJ_SENT &&
- l2cap_ertm_rx_queued_iframes(sk))
- l2cap_send_disconn_req(l2cap_pi(sk)->conn, sk, ECONNRESET);
- else if ((l2cap_pi(sk)->conn_state & L2CAP_CONN_LOCAL_BUSY) &&
- l2cap_rmem_available(sk))
- l2cap_ertm_tx(sk, 0, 0, L2CAP_ERTM_EVENT_LOCAL_BUSY_CLEAR);
- release_sock(sk);
- }
- static inline __u8 l2cap_select_mode(__u8 mode, __u16 remote_feat_mask)
- {
- switch (mode) {
- case L2CAP_MODE_STREAMING:
- case L2CAP_MODE_ERTM:
- if (l2cap_mode_supported(mode, remote_feat_mask))
- return mode;
- /* fall through */
- default:
- return L2CAP_MODE_BASIC;
- }
- }
- static void l2cap_setup_txwin(struct l2cap_pinfo *pi)
- {
- if (pi->tx_win > L2CAP_TX_WIN_MAX_ENHANCED &&
- (pi->conn->feat_mask & L2CAP_FEAT_EXT_WINDOW)) {
- pi->tx_win_max = L2CAP_TX_WIN_MAX_EXTENDED;
- pi->extended_control = 1;
- } else {
- if (pi->tx_win > L2CAP_TX_WIN_MAX_ENHANCED)
- pi->tx_win = L2CAP_TX_WIN_MAX_ENHANCED;
- pi->tx_win_max = L2CAP_TX_WIN_MAX_ENHANCED;
- pi->extended_control = 0;
- }
- pi->ack_win = pi->tx_win;
- }
- static void l2cap_aggregate_fs(struct hci_ext_fs *cur,
- struct hci_ext_fs *new,
- struct hci_ext_fs *agg)
- {
- *agg = *cur;
- if ((cur->max_sdu != 0xFFFF) && (cur->sdu_arr_time != 0xFFFFFFFF)) {
- /* current flow spec has known rate */
- if ((new->max_sdu == 0xFFFF) ||
- (new->sdu_arr_time == 0xFFFFFFFF)) {
- /* new fs has unknown rate, so aggregate is unknown */
- agg->max_sdu = 0xFFFF;
- agg->sdu_arr_time = 0xFFFFFFFF;
- } else {
- /* new fs has known rate, so aggregate is known */
- u64 cur_rate;
- u64 new_rate;
- cur_rate = cur->max_sdu * 1000000ULL;
- if (cur->sdu_arr_time)
- cur_rate = div_u64(cur_rate, cur->sdu_arr_time);
- new_rate = new->max_sdu * 1000000ULL;
- if (new->sdu_arr_time)
- new_rate = div_u64(new_rate, new->sdu_arr_time);
- cur_rate = cur_rate + new_rate;
- if (cur_rate)
- agg->sdu_arr_time = div64_u64(
- agg->max_sdu * 1000000ULL, cur_rate);
- }
- }
- }
- static int l2cap_aggregate(struct hci_chan *chan, struct l2cap_pinfo *pi)
- {
- struct hci_ext_fs tx_fs;
- struct hci_ext_fs rx_fs;
- BT_DBG("chan %p", chan);
- if (((chan->tx_fs.max_sdu == 0xFFFF) ||
- (chan->tx_fs.sdu_arr_time == 0xFFFFFFFF)) &&
- ((chan->rx_fs.max_sdu == 0xFFFF) ||
- (chan->rx_fs.sdu_arr_time == 0xFFFFFFFF)))
- return 0;
- l2cap_aggregate_fs(&chan->tx_fs,
- (struct hci_ext_fs *) &pi->local_fs, &tx_fs);
- l2cap_aggregate_fs(&chan->rx_fs,
- (struct hci_ext_fs *) &pi->remote_fs, &rx_fs);
- hci_chan_modify(chan, &tx_fs, &rx_fs);
- return 1;
- }
- static void l2cap_deaggregate_fs(struct hci_ext_fs *cur,
- struct hci_ext_fs *old,
- struct hci_ext_fs *agg)
- {
- *agg = *cur;
- if ((cur->max_sdu != 0xFFFF) && (cur->sdu_arr_time != 0xFFFFFFFF)) {
- u64 cur_rate;
- u64 old_rate;
- cur_rate = cur->max_sdu * 1000000ULL;
- if (cur->sdu_arr_time)
- cur_rate = div_u64(cur_rate, cur->sdu_arr_time);
- old_rate = old->max_sdu * 1000000ULL;
- if (old->sdu_arr_time)
- old_rate = div_u64(old_rate, old->sdu_arr_time);
- cur_rate = cur_rate - old_rate;
- if (cur_rate)
- agg->sdu_arr_time = div64_u64(
- agg->max_sdu * 1000000ULL, cur_rate);
- }
- }
- static int l2cap_deaggregate(struct hci_chan *chan, struct l2cap_pinfo *pi)
- {
- struct hci_ext_fs tx_fs;
- struct hci_ext_fs rx_fs;
- BT_DBG("chan %p", chan);
- if (((chan->tx_fs.max_sdu == 0xFFFF) ||
- (chan->tx_fs.sdu_arr_time == 0xFFFFFFFF)) &&
- ((chan->rx_fs.max_sdu == 0xFFFF) ||
- (chan->rx_fs.sdu_arr_time == 0xFFFFFFFF)))
- return 0;
- l2cap_deaggregate_fs(&chan->tx_fs,
- (struct hci_ext_fs *) &pi->local_fs, &tx_fs);
- l2cap_deaggregate_fs(&chan->rx_fs,
- (struct hci_ext_fs *) &pi->remote_fs, &rx_fs);
- hci_chan_modify(chan, &tx_fs, &rx_fs);
- return 1;
- }
- static struct hci_chan *l2cap_chan_admit(u8 amp_id, struct sock *sk)
- {
- struct l2cap_pinfo *pi = l2cap_pi(sk);
- struct hci_dev *hdev;
- struct hci_conn *hcon;
- struct hci_chan *chan;
- hdev = hci_dev_get(amp_id);
- if (!hdev)
- return NULL;
- BT_DBG("hdev %s", hdev->name);
- hcon = hci_conn_hash_lookup_ba(hdev, ACL_LINK, pi->conn->dst);
- if (!hcon) {
- chan = NULL;
- goto done;
- }
- chan = hci_chan_list_lookup_id(hdev, hcon->handle);
- if (chan) {
- l2cap_aggregate(chan, pi);
- sock_hold(sk);
- chan->l2cap_sk = sk;
- hci_chan_hold(chan);
- pi->ampchan = chan;
- goto done;
- }
- chan = hci_chan_add(hdev);
- if (chan) {
- chan->conn = hcon;
- sock_hold(sk);
- chan->l2cap_sk = sk;
- hci_chan_hold(chan);
- pi->ampchan = chan;
- hci_chan_create(chan,
- (struct hci_ext_fs *) &pi->local_fs,
- (struct hci_ext_fs *) &pi->remote_fs);
- }
- done:
- hci_dev_put(hdev);
- return chan;
- }
- static void l2cap_get_ertm_timeouts(struct l2cap_conf_rfc *rfc,
- struct l2cap_pinfo *pi)
- {
- if (pi->amp_id && pi->ampcon) {
- u64 ertm_to = pi->ampcon->hdev->amp_be_flush_to;
- /* Class 1 devices have must have ERTM timeouts
- * exceeding the Link Supervision Timeout. The
- * default Link Supervision Timeout for AMP
- * controllers is 10 seconds.
- *
- * Class 1 devices use 0xffffffff for their
- * best-effort flush timeout, so the clamping logic
- * will result in a timeout that meets the above
- * requirement. ERTM timeouts are 16-bit values, so
- * the maximum timeout is 65.535 seconds.
- */
- /* Convert timeout to milliseconds and round */
- ertm_to = div_u64(ertm_to + 999, 1000);
- /* This is the recommended formula for class 2 devices
- * that start ERTM timers when packets are sent to the
- * controller.
- */
- ertm_to = 3 * ertm_to + 500;
- if (ertm_to > 0xffff)
- ertm_to = 0xffff;
- rfc->retrans_timeout = cpu_to_le16((u16) ertm_to);
- rfc->monitor_timeout = rfc->retrans_timeout;
- } else {
- rfc->retrans_timeout = cpu_to_le16(L2CAP_DEFAULT_RETRANS_TO);
- rfc->monitor_timeout = cpu_to_le16(L2CAP_DEFAULT_MONITOR_TO);
- }
- }
- int l2cap_build_conf_req(struct sock *sk, void *data, size_t data_size)
- {
- struct l2cap_pinfo *pi = l2cap_pi(sk);
- struct l2cap_conf_req *req = data;
- struct l2cap_conf_rfc rfc = { .mode = pi->mode };
- void *ptr = req->data;
- void *endptr = data + data_size;
- BT_DBG("sk %p mode %d", sk, pi->mode);
- if (pi->num_conf_req || pi->num_conf_rsp)
- goto done;
- switch (pi->mode) {
- case L2CAP_MODE_STREAMING:
- case L2CAP_MODE_ERTM:
- if (pi->conf_state & L2CAP_CONF_STATE2_DEVICE)
- break;
- /* fall through */
- default:
- pi->mode = l2cap_select_mode(rfc.mode, pi->conn->feat_mask);
- break;
- }
- done:
- if (pi->imtu != L2CAP_DEFAULT_MTU)
- l2cap_add_conf_opt(&ptr, L2CAP_CONF_MTU, 2, pi->imtu, endptr - ptr);
- switch (pi->mode) {
- case L2CAP_MODE_BASIC:
- if (!(pi->conn->feat_mask & L2CAP_FEAT_ERTM) &&
- !(pi->conn->feat_mask & L2CAP_FEAT_STREAMING))
- break;
- rfc.txwin_size = 0;
- rfc.max_transmit = 0;
- rfc.retrans_timeout = 0;
- rfc.monitor_timeout = 0;
- rfc.max_pdu_size = 0;
- l2cap_add_conf_opt(&ptr, L2CAP_CONF_RFC, sizeof(rfc),
- (unsigned long) &rfc, endptr - ptr);
- break;
- case L2CAP_MODE_ERTM:
- l2cap_setup_txwin(pi);
- if (pi->tx_win > L2CAP_TX_WIN_MAX_ENHANCED)
- rfc.txwin_size = L2CAP_TX_WIN_MAX_ENHANCED;
- else
- rfc.txwin_size = pi->tx_win;
- rfc.max_transmit = pi->max_tx;
- rfc.max_pdu_size = cpu_to_le16(L2CAP_DEFAULT_MAX_PDU_SIZE);
- l2cap_get_ertm_timeouts(&rfc, pi);
- if (L2CAP_DEFAULT_MAX_PDU_SIZE > pi->imtu)
- rfc.max_pdu_size = cpu_to_le16(pi->imtu);
- l2cap_add_conf_opt(&ptr, L2CAP_CONF_RFC, sizeof(rfc),
- (unsigned long) &rfc, endptr - ptr);
- if ((pi->conn->feat_mask & L2CAP_FEAT_EXT_WINDOW) &&
- pi->extended_control) {
- l2cap_add_conf_opt(&ptr, L2CAP_CONF_EXT_WINDOW, 2,
- pi->tx_win, endptr - ptr);
- }
- if (pi->amp_id) {
- /* default best effort extended flow spec */
- struct l2cap_conf_ext_fs fs = {1, 1, 0xFFFF,
- 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF};
- l2cap_add_conf_opt(&ptr, L2CAP_CONF_EXT_FS,
- sizeof(fs), (unsigned long) &fs, endptr - ptr);
- }
- if (!(pi->conn->feat_mask & L2CAP_FEAT_FCS))
- break;
- if (pi->fcs == L2CAP_FCS_NONE ||
- pi->conf_state & L2CAP_CONF_NO_FCS_RECV) {
- pi->fcs = L2CAP_FCS_NONE;
- l2cap_add_conf_opt(&ptr, L2CAP_CONF_FCS, 1, pi->fcs, endptr - ptr);
- }
- break;
- case L2CAP_MODE_STREAMING:
- l2cap_setup_txwin(pi);
- rfc.txwin_size = 0;
- rfc.max_transmit = 0;
- rfc.retrans_timeout = 0;
- rfc.monitor_timeout = 0;
- rfc.max_pdu_size = cpu_to_le16(L2CAP_DEFAULT_MAX_PDU_SIZE);
- if (L2CAP_DEFAULT_MAX_PDU_SIZE > pi->imtu)
- rfc.max_pdu_size = cpu_to_le16(pi->imtu);
- l2cap_add_conf_opt(&ptr, L2CAP_CONF_RFC, sizeof(rfc),
- (unsigned long) &rfc, endptr - ptr);
- if ((pi->conn->feat_mask & L2CAP_FEAT_EXT_WINDOW) &&
- pi->extended_control) {
- l2cap_add_conf_opt(&ptr, L2CAP_CONF_EXT_WINDOW, 2, 0, endptr - ptr);
- }
- if (!(pi->conn->feat_mask & L2CAP_FEAT_FCS))
- break;
- if (pi->fcs == L2CAP_FCS_NONE ||
- pi->conf_state & L2CAP_CONF_NO_FCS_RECV) {
- pi->fcs = L2CAP_FCS_NONE;
- l2cap_add_conf_opt(&ptr, L2CAP_CONF_FCS, 1, pi->fcs, endptr - ptr);
- }
- break;
- }
- req->dcid = cpu_to_le16(pi->dcid);
- req->flags = cpu_to_le16(0);
- return ptr - data;
- }
- static int l2cap_build_amp_reconf_req(struct sock *sk, void *data, size_t data_size)
- {
- struct l2cap_pinfo *pi = l2cap_pi(sk);
- struct l2cap_conf_req *req = data;
- struct l2cap_conf_rfc rfc = { .mode = pi->mode };
- void *ptr = req->data;
- void *endptr = data + data_size;
- BT_DBG("sk %p", sk);
- switch (pi->mode) {
- case L2CAP_MODE_ERTM:
- rfc.mode = L2CAP_MODE_ERTM;
- rfc.txwin_size = pi->tx_win;
- rfc.max_transmit = pi->max_tx;
- rfc.max_pdu_size = cpu_to_le16(L2CAP_DEFAULT_MAX_PDU_SIZE);
- l2cap_get_ertm_timeouts(&rfc, pi);
- if (L2CAP_DEFAULT_MAX_PDU_SIZE > pi->imtu)
- rfc.max_pdu_size = cpu_to_le16(pi->imtu);
- break;
- default:
- return -ECONNREFUSED;
- }
- l2cap_add_conf_opt(&ptr, L2CAP_CONF_RFC, sizeof(rfc),
- (unsigned long) &rfc, endptr - ptr);
- if (pi->conn->feat_mask & L2CAP_FEAT_FCS) {
- /* TODO assign fcs for br/edr based on socket config option */
- /* FCS is not used with AMP because it is redundant - lower
- * layers already include a checksum. */
- if (pi->amp_id)
- pi->local_conf.fcs = L2CAP_FCS_NONE;
- else
- pi->local_conf.fcs = L2CAP_FCS_CRC16;
- l2cap_add_conf_opt(&ptr, L2CAP_CONF_FCS, 1, pi->local_conf.fcs, endptr - ptr);
- pi->fcs = pi->local_conf.fcs | pi->remote_conf.fcs;
- }
- req->dcid = cpu_to_le16(pi->dcid);
- req->flags = cpu_to_le16(0);
- return ptr - data;
- }
- static int l2cap_parse_conf_req(struct sock *sk, void *data, size_t data_size)
- {
- struct l2cap_pinfo *pi = l2cap_pi(sk);
- struct l2cap_conf_rsp *rsp = data;
- void *ptr = rsp->data;
- void *endptr = data + data_size;
- void *req = pi->conf_req;
- int len = pi->conf_len;
- int type, hint, olen;
- unsigned long val;
- struct l2cap_conf_rfc rfc = { .mode = L2CAP_MODE_BASIC };
- struct l2cap_conf_ext_fs fs;
- u16 mtu = L2CAP_DEFAULT_MTU;
- u16 result = L2CAP_CONF_SUCCESS;
- BT_DBG("sk %p", sk);
- if (pi->omtu > mtu)
- mtu = pi->omtu;
- while (len >= L2CAP_CONF_OPT_SIZE) {
- len -= l2cap_get_conf_opt(&req, &type, &olen, &val);
- if (len < 0)
- break;
- hint = type & L2CAP_CONF_HINT;
- type &= L2CAP_CONF_MASK;
- switch (type) {
- case L2CAP_CONF_MTU:
- if (olen != 2)
- break;
- mtu = val;
- break;
- case L2CAP_CONF_FLUSH_TO:
- if (olen != 2)
- break;
- pi->flush_to = val;
- if (pi->conf_state & L2CAP_CONF_LOCKSTEP)
- result = L2CAP_CONF_UNACCEPT;
- else
- pi->remote_conf.flush_to = val;
- break;
- case L2CAP_CONF_QOS:
- if (pi->conf_state & L2CAP_CONF_LOCKSTEP)
- result = L2CAP_CONF_UNACCEPT;
- break;
- case L2CAP_CONF_RFC:
- if (olen != sizeof(rfc))
- break;
- memcpy(&rfc, (void *) val, olen);
- break;
- case L2CAP_CONF_FCS:
- if (olen != 1)
- break;
- if (val == L2CAP_FCS_NONE)
- pi->conf_state |= L2CAP_CONF_NO_FCS_RECV;
- pi->remote_conf.fcs = val;
- break;
- case L2CAP_CONF_EXT_FS:
- if (olen == sizeof(fs)) {
- pi->conf_state |= L2CAP_CONF_EFS_RECV;
- if (!(pi->conf_state & L2CAP_CONF_LOCKSTEP)) {
- result = L2CAP_CONF_UNACCEPT;
- break;
- }
- memcpy(&fs, (void *) val, olen);
- if (fs.type != L2CAP_SERVICE_BEST_EFFORT) {
- result = L2CAP_CONF_FLOW_SPEC_REJECT;
- break;
- }
- pi->remote_conf.flush_to =
- le32_to_cpu(fs.flush_to);
- pi->remote_fs.id = fs.id;
- pi->remote_fs.type = fs.type;
- pi->remote_fs.max_sdu =
- le16_to_cpu(fs.max_sdu);
- pi->remote_fs.sdu_arr_time =
- le32_to_cpu(fs.sdu_arr_time);
- pi->remote_fs.acc_latency =
- le32_to_cpu(fs.acc_latency);
- pi->remote_fs.flush_to =
- le32_to_cpu(fs.flush_to);
- }
- break;
- case L2CAP_CONF_EXT_WINDOW:
- pi->extended_control = 1;
- pi->remote_tx_win = val;
- pi->tx_win_max = L2CAP_TX_WIN_MAX_EXTENDED;
- pi->conf_state |= L2CAP_CONF_EXT_WIN_RECV;
- break;
- default:
- if (hint)
- break;
- result = L2CAP_CONF_UNKNOWN;
- *((u8 *) ptr++) = type;
- break;
- }
- }
- if (pi->num_conf_rsp || pi->num_conf_req > 1)
- goto done;
- switch (pi->mode) {
- case L2CAP_MODE_STREAMING:
- case L2CAP_MODE_ERTM:
- if (!(pi->conf_state & L2CAP_CONF_STATE2_DEVICE)) {
- pi->mode = l2cap_select_mode(rfc.mode,
- pi->conn->feat_mask);
- break;
- }
- if (pi->mode != rfc.mode)
- return -ECONNREFUSED;
- break;
- }
- done:
- if (pi->mode != rfc.mode) {
- result = L2CAP_CONF_UNACCEPT;
- rfc.mode = pi->mode;
- if (mtu > L2CAP_DEFAULT_MTU)
- pi->omtu = mtu;
- if (pi->num_conf_rsp == 1)
- return -ECONNREFUSED;
- l2cap_add_conf_opt(&ptr, L2CAP_CONF_RFC,
- sizeof(rfc), (unsigned long) &rfc,
- endptr - ptr);
- }
- if ((pi->conf_state & L2CAP_CONF_LOCKSTEP) &&
- !(pi->conf_state & L2CAP_CONF_EFS_RECV))
- return -ECONNREFUSED;
- if (result == L2CAP_CONF_SUCCESS) {
- /* Configure output options and let the other side know
- * which ones we don't like. */
- if (mtu < L2CAP_DEFAULT_MIN_MTU) {
- result = L2CAP_CONF_UNACCEPT;
- pi->omtu = L2CAP_DEFAULT_MIN_MTU;
- } else {
- pi->omtu = mtu;
- pi->conf_state |= L2CAP_CONF_MTU_DONE;
- }
- l2cap_add_conf_opt(&ptr, L2CAP_CONF_MTU, 2, pi->omtu, endptr - ptr);
- switch (rfc.mode) {
- case L2CAP_MODE_BASIC:
- pi->fcs = L2CAP_FCS_NONE;
- pi->conf_state |= L2CAP_CONF_MODE_DONE;
- break;
- case L2CAP_MODE_ERTM:
- if (!(pi->conf_state & L2CAP_CONF_EXT_WIN_RECV))
- pi->remote_tx_win = rfc.txwin_size;
- pi->remote_max_tx = rfc.max_transmit;
- pi->remote_mps = le16_to_cpu(rfc.max_pdu_size);
- l2cap_get_ertm_timeouts(&rfc, pi);
- pi->conf_state |= L2CAP_CONF_MODE_DONE;
- l2cap_add_conf_opt(&ptr, L2CAP_CONF_RFC,
- sizeof(rfc), (unsigned long) &rfc, endptr - ptr);
- if (pi->conf_state & L2CAP_CONF_LOCKSTEP)
- l2cap_add_conf_opt(&ptr, L2CAP_CONF_EXT_FS,
- sizeof(fs), (unsigned long) &fs, endptr - ptr);
- break;
- case L2CAP_MODE_STREAMING:
- pi->remote_mps = le16_to_cpu(rfc.max_pdu_size);
- pi->conf_state |= L2CAP_CONF_MODE_DONE;
- l2cap_add_conf_opt(&ptr, L2CAP_CONF_RFC,
- sizeof(rfc), (unsigned long) &rfc, endptr - ptr);
- break;
- default:
- result = L2CAP_CONF_UNACCEPT;
- memset(&rfc, 0, sizeof(rfc));
- rfc.mode = pi->mode;
- }
- if (pi->conf_state & L2CAP_CONF_LOCKSTEP &&
- !(pi->conf_state & L2CAP_CONF_PEND_SENT)) {
- pi->conf_state |= L2CAP_CONF_PEND_SENT;
- result = L2CAP_CONF_PENDING;
- if (pi->conf_state & L2CAP_CONF_LOCKSTEP_PEND &&
- pi->amp_id) {
- struct hci_chan *chan;
- /* Trigger logical link creation only on AMP */
- chan = l2cap_chan_admit(pi->amp_id, sk);
- if (!chan)
- return -ECONNREFUSED;
- if (chan->state == BT_CONNECTED)
- l2cap_create_cfm(chan, 0);
- }
- }
- if (result == L2CAP_CONF_SUCCESS)
- pi->conf_state |= L2CAP_CONF_OUTPUT_DONE;
- }
- rsp->scid = cpu_to_le16(pi->dcid);
- rsp->result = cpu_to_le16(result);
- rsp->flags = cpu_to_le16(0x0000);
- return ptr - data;
- }
- static int l2cap_parse_amp_move_reconf_req(struct sock *sk, void *data, size_t data_size)
- {
- struct l2cap_pinfo *pi = l2cap_pi(sk);
- struct l2cap_conf_rsp *rsp = data;
- void *ptr = rsp->data;
- void *endptr = data + data_size;
- void *req = pi->conf_req;
- int len = pi->conf_len;
- int type, hint, olen;
- unsigned long val;
- struct l2cap_conf_rfc rfc = { .mode = L2CAP_MODE_BASIC };
- struct l2cap_conf_ext_fs fs;
- u16 mtu = pi->omtu;
- u16 tx_win = pi->remote_tx_win;
- u16 result = L2CAP_CONF_SUCCESS;
- BT_DBG("sk %p", sk);
- while (len >= L2CAP_CONF_OPT_SIZE) {
- len -= l2cap_get_conf_opt(&req, &type, &olen, &val);
- hint = type & L2CAP_CONF_HINT;
- type &= L2CAP_CONF_MASK;
- switch (type) {
- case L2CAP_CONF_MTU:
- mtu = val;
- break;
- case L2CAP_CONF_FLUSH_TO:
- if (pi->amp_move_id)
- result = L2CAP_CONF_UNACCEPT;
- else
- pi->remote_conf.flush_to = val;
- break;
- case L2CAP_CONF_QOS:
- if (pi->amp_move_id)
- result = L2CAP_CONF_UNACCEPT;
- break;
- case L2CAP_CONF_RFC:
- if (olen != sizeof(rfc))
- break;
- memcpy(&rfc, (void *) val, olen);
- break;
- case L2CAP_CONF_FCS:
- if (olen != 1)
- break;
- pi->remote_conf.fcs = val;
- break;
- case L2CAP_CONF_EXT_FS:
- if (olen != sizeof(fs))
- break;
- memcpy(&fs, (void *) val, olen);
- if (fs.type != L2CAP_SERVICE_BEST_EFFORT)
- result = L2CAP_CONF_FLOW_SPEC_REJECT;
- else {
- pi->remote_conf.flush_to =
- le32_to_cpu(fs.flush_to);
- }
- break;
- case L2CAP_CONF_EXT_WINDOW:
- tx_win = val;
- break;
- default:
- if (hint)
- break;
- result = L2CAP_CONF_UNKNOWN;
- *((u8 *) ptr++) = type;
- break;
- }
- }
- BT_DBG("result 0x%2.2x cur mode 0x%2.2x req mode 0x%2.2x",
- result, pi->mode, rfc.mode);
- if (pi->mode != rfc.mode || rfc.mode == L2CAP_MODE_BASIC)
- result = L2CAP_CONF_UNACCEPT;
- if (result == L2CAP_CONF_SUCCESS) {
- /* Configure output options and let the other side know
- * which ones we don't like. */
- /* Don't allow mtu to decrease. */
- if (mtu < pi->omtu)
- result = L2CAP_CONF_UNACCEPT;
- BT_DBG("mtu %d omtu %d", mtu, pi->omtu);
- l2cap_add_conf_opt(&ptr, L2CAP_CONF_MTU, 2, pi->omtu, endptr - ptr);
- /* Don't allow extended transmit window to change. */
- if (tx_win != pi->remote_tx_win) {
- result = L2CAP_CONF_UNACCEPT;
- l2cap_add_conf_opt(&ptr, L2CAP_CONF_EXT_WINDOW, 2,
- pi->remote_tx_win, endptr - ptr);
- }
- pi->remote_mps = rfc.max_pdu_size;
- if (rfc.mode == L2CAP_MODE_ERTM) {
- l2cap_get_ertm_timeouts(&rfc, pi);
- } else {
- rfc.retrans_timeout = 0;
- rfc.monitor_timeout = 0;
- }
- l2cap_add_conf_opt(&ptr, L2CAP_CONF_RFC,
- sizeof(rfc), (unsigned long) &rfc, endptr - ptr);
- }
- if (result != L2CAP_CONF_SUCCESS)
- goto done;
- pi->fcs = pi->remote_conf.fcs | pi->local_conf.fcs;
- if (pi->rx_state == L2CAP_ERTM_RX_STATE_WAIT_F_FLAG)
- pi->flush_to = pi->remote_conf.flush_to;
- done:
- rsp->scid = cpu_to_le16(pi->dcid);
- rsp->result = cpu_to_le16(result);
- rsp->flags = cpu_to_le16(0x0000);
- return ptr - data;
- }
- static int l2cap_parse_conf_rsp(struct sock *sk, void *rsp, int len, void *data, size_t size, u16 *result)
- {
- struct l2cap_pinfo *pi = l2cap_pi(sk);
- struct l2cap_conf_req *req = data;
- void *ptr = req->data;
- void *endptr = data + size;
- int type, olen;
- unsigned long val;
- struct l2cap_conf_rfc rfc;
- BT_DBG("sk %p, rsp %p, len %d, req %p", sk, rsp, len, data);
- /* Initialize rfc in case no rfc option is received */
- rfc.mode = pi->mode;
- rfc.retrans_timeout = cpu_to_le16(L2CAP_DEFAULT_RETRANS_TO);
- rfc.monitor_timeout = cpu_to_le16(L2CAP_DEFAULT_MONITOR_TO);
- rfc.max_pdu_size = cpu_to_le16(L2CAP_DEFAULT_MAX_PDU_SIZE);
- while (len >= L2CAP_CONF_OPT_SIZE) {
- len -= l2cap_get_conf_opt(&rsp, &type, &olen, &val);
- if (len < 0)
- break;
- switch (type) {
- case L2CAP_CONF_MTU:
- if (olen != 2)
- break;
- if (val < L2CAP_DEFAULT_MIN_MTU) {
- *result = L2CAP_CONF_UNACCEPT;
- pi->imtu = L2CAP_DEFAULT_MIN_MTU;
- } else
- pi->imtu = val;
- l2cap_add_conf_opt(&ptr, L2CAP_CONF_MTU, 2, pi->imtu,
- endptr - ptr);
- break;
- case L2CAP_CONF_FLUSH_TO:
- if (olen != 2)
- break;
- pi->flush_to = val;
- l2cap_add_conf_opt(&ptr, L2CAP_CONF_FLUSH_TO, 2,
- pi->flush_to, endptr - ptr);
- break;
- case L2CAP_CONF_RFC:
- if (olen != sizeof(rfc))
- break;
- memcpy(&rfc, (void *)val, olen);
- if ((pi->conf_state & L2CAP_CONF_STATE2_DEVICE) &&
- rfc.mode != pi->mode)
- return -ECONNREFUSED;
- pi->fcs = 0;
- l2cap_add_conf_opt(&ptr, L2CAP_CONF_RFC, sizeof(rfc),
- (unsigned long) &rfc, endptr - ptr);
- break;
- case L2CAP_CONF_EXT_WINDOW:
- if (olen != 2)
- break;
- pi->ack_win = min_t(u16, val, pi->ack_win);
- l2cap_add_conf_opt(&ptr, L2CAP_CONF_EXT_WINDOW,
- 2, pi->tx_win, endptr - ptr);
- break;
- default:
- break;
- }
- }
- if (pi->mode == L2CAP_MODE_BASIC && pi->mode != rfc.mode)
- return -ECONNREFUSED;
- pi->mode = rfc.mode;
- if (*result == L2CAP_CONF_SUCCESS) {
- switch (rfc.mode) {
- case L2CAP_MODE_ERTM:
- pi->retrans_timeout = le16_to_cpu(rfc.retrans_timeout);
- pi->monitor_timeout = le16_to_cpu(rfc.monitor_timeout);
- pi->mps = le16_to_cpu(rfc.max_pdu_size);
- if (!pi->extended_control) {
- pi->ack_win = min_t(u16, pi->ack_win,
- rfc.txwin_size);
- }
- break;
- case L2CAP_MODE_STREAMING:
- pi->mps = le16_to_cpu(rfc.max_pdu_size);
- }
- }
- req->dcid = cpu_to_le16(pi->dcid);
- req->flags = cpu_to_le16(0x0000);
- return ptr - data;
- }
- static int l2cap_build_conf_rsp(struct sock *sk, void *data, u16 result, u16 flags)
- {
- struct l2cap_conf_rsp *rsp = data;
- void *ptr = rsp->data;
- BT_DBG("sk %p", sk);
- rsp->scid = cpu_to_le16(l2cap_pi(sk)->dcid);
- rsp->result = cpu_to_le16(result);
- rsp->flags = cpu_to_le16(flags);
- return ptr - data;
- }
- static void l2cap_conf_rfc_get(struct sock *sk, void *rsp, int len)
- {
- struct l2cap_pinfo *pi = l2cap_pi(sk);
- int type, olen;
- unsigned long val;
- struct l2cap_conf_rfc rfc;
- u16 txwin_ext = pi->ack_win;
- BT_DBG("sk %p, rsp %p, len %d", sk, rsp, len);
- /* Initialize rfc in case no rfc option is received */
- rfc.mode = pi->mode;
- rfc.retrans_timeout = cpu_to_le16(L2CAP_DEFAULT_RETRANS_TO);
- rfc.monitor_timeout = cpu_to_le16(L2CAP_DEFAULT_MONITOR_TO);
- rfc.max_pdu_size = cpu_to_le16(L2CAP_DEFAULT_MAX_PDU_SIZE);
- rfc.txwin_size = min_t(u16, pi->ack_win, L2CAP_DEFAULT_TX_WINDOW);
- if ((pi->mode != L2CAP_MODE_ERTM) && (pi->mode != L2CAP_MODE_STREAMING))
- return;
- while (len >= L2CAP_CONF_OPT_SIZE) {
- len -= l2cap_get_conf_opt(&rsp, &type, &olen, &val);
- if (len < 0)
- break;
- switch (type) {
- case L2CAP_CONF_RFC:
- if (olen != sizeof(rfc))
- break;
- memcpy(&rfc, (void *)val, olen);
- break;
- case L2CAP_CONF_EXT_WINDOW:
- if (olen != 2)
- break;
- txwin_ext = val;
- break;
- }
- }
- switch (rfc.mode) {
- case L2CAP_MODE_ERTM:
- pi->retrans_timeout = le16_to_cpu(rfc.retrans_timeout);
- pi->monitor_timeout = le16_to_cpu(rfc.monitor_timeout);
- pi->mps = le16_to_cpu(rfc.max_pdu_size);
- if (pi->extended_control)
- pi->ack_win = min_t(u16, pi->ack_win, txwin_ext);
- else
- pi->ack_win = min_t(u16, pi->ack_win, rfc.txwin_size);
- break;
- case L2CAP_MODE_STREAMING:
- pi->mps = le16_to_cpu(rfc.max_pdu_size);
- }
- }
- static void l2cap_conf_ext_fs_get(struct sock *sk, void *rsp, int len)
- {
- struct l2cap_pinfo *pi = l2cap_pi(sk);
- int type, olen;
- unsigned long val;
- struct l2cap_conf_ext_fs fs;
- BT_DBG("sk %p, rsp %p, len %d", sk, rsp, len);
- while (len >= L2CAP_CONF_OPT_SIZE) {
- len -= l2cap_get_conf_opt(&rsp, &type, &olen, &val);
- if ((type == L2CAP_CONF_EXT_FS) &&
- (olen == sizeof(struct l2cap_conf_ext_fs))) {
- memcpy(&fs, (void *)val, olen);
- pi->local_fs.id = fs.id;
- pi->local_fs.type = fs.type;
- pi->local_fs.max_sdu = le16_to_cpu(fs.max_sdu);
- pi->local_fs.sdu_arr_time =
- le32_to_cpu(fs.sdu_arr_time);
- pi->local_fs.acc_latency = le32_to_cpu(fs.acc_latency);
- pi->local_fs.flush_to = le32_to_cpu(fs.flush_to);
- break;
- }
- }
- }
- static int l2cap_finish_amp_move(struct sock *sk)
- {
- struct l2cap_pinfo *pi;
- int err;
- BT_DBG("sk %p", sk);
- pi = l2cap_pi(sk);
- pi->amp_move_role = L2CAP_AMP_MOVE_NONE;
- pi->rx_state = L2CAP_ERTM_RX_STATE_RECV;
- if (pi->ampcon)
- pi->conn->mtu = pi->ampcon->hdev->acl_mtu;
- else
- pi->conn->mtu = pi->conn->hcon->hdev->acl_mtu;
- err = l2cap_setup_resegment(sk);
- return err;
- }
- static int l2cap_amp_move_reconf_rsp(struct sock *sk, void *rsp, int len,
- u16 result)
- {
- int err = 0;
- struct l2cap_conf_rfc rfc = {.mode = L2CAP_MODE_BASIC};
- struct l2cap_pinfo *pi = l2cap_pi(sk);
- BT_DBG("sk %p, rsp %p, len %d, res 0x%2.2x", sk, rsp, len, result);
- if (pi->reconf_state == L2CAP_RECONF_NONE)
- return -ECONNREFUSED;
- if (result == L2CAP_CONF_SUCCESS) {
- while (len >= L2CAP_CONF_OPT_SIZE) {
- int type, olen;
- unsigned long val;
- len -= l2cap_get_conf_opt(&rsp, &type, &olen, &val);
- if (type == L2CAP_CONF_RFC) {
- if (olen == sizeof(rfc))
- memcpy(&rfc, (void *)val, olen);
- if (rfc.mode != pi->mode) {
- l2cap_send_disconn_req(pi->conn, sk,
- ECONNRESET);
- return -ECONNRESET;
- }
- goto done;
- }
- }
- }
- BT_ERR("Expected RFC option was missing, using existing values");
- rfc.mode = pi->mode;
- rfc.retrans_timeout = cpu_to_le16(pi->retrans_timeout);
- rfc.monitor_timeout = cpu_to_le16(pi->monitor_timeout);
- done:
- l2cap_ertm_stop_ack_timer(pi);
- l2cap_ertm_stop_retrans_timer(pi);
- l2cap_ertm_stop_monitor_timer(pi);
- pi->mps = le16_to_cpu(rfc.max_pdu_size);
- if (pi->mode == L2CAP_MODE_ERTM) {
- pi->retrans_timeout = le16_to_cpu(rfc.retrans_timeout);
- pi->monitor_timeout = le16_to_cpu(rfc.monitor_timeout);
- }
- if (l2cap_pi(sk)->reconf_state == L2CAP_RECONF_ACC) {
- l2cap_pi(sk)->reconf_state = L2CAP_RECONF_NONE;
- /* Respond to poll */
- err = l2cap_answer_move_poll(sk);
- } else if (l2cap_pi(sk)->reconf_state == L2CAP_RECONF_INT) {
- if (pi->mode == L2CAP_MODE_ERTM) {
- l2cap_ertm_tx(sk, NULL, NULL,
- L2CAP_ERTM_EVENT_EXPLICIT_POLL);
- pi->rx_state = L2CAP_ERTM_RX_STATE_WAIT_F_FLAG;
- }
- }
- return err;
- }
- static inline int l2cap_command_rej(struct l2cap_conn *conn,
- struct l2cap_cmd_hdr *cmd, u16 cmd_len,
- u8 *data)
- {
- struct l2cap_cmd_rej *rej = (struct l2cap_cmd_rej *) data;
- if (cmd_len < sizeof(*rej))
- return -EPROTO;
- if (rej->reason != 0x0000)
- return 0;
- if ((conn->info_state & L2CAP_INFO_FEAT_MASK_REQ_SENT) &&
- cmd->ident == conn->info_ident) {
- del_timer(&conn->info_timer);
- conn->info_state |= L2CAP_INFO_FEAT_MASK_REQ_DONE;
- conn->info_ident = 0;
- l2cap_conn_start(conn);
- }
- return 0;
- }
- static struct sock *l2cap_create_connect(struct l2cap_conn *conn,
- struct l2cap_cmd_hdr *cmd,
- u16 cmd_len, u8 *data,
- u8 rsp_code,
- u8 amp_id)
- {
- struct l2cap_chan_list *list = &conn->chan_list;
- struct l2cap_conn_req *req = (struct l2cap_conn_req *) data;
- struct l2cap_conn_rsp rsp;
- struct sock *parent, *sk = NULL;
- int result, status = L2CAP_CS_NO_INFO;
- u16 dcid = 0, scid;
- __le16 psm;
- if (cmd_len < sizeof(struct l2cap_conn_req))
- return NULL;
- scid = __le16_to_cpu(req->scid);
- psm = req->psm;
- BT_DBG("psm 0x%2.2x scid 0x%4.4x", psm, scid);
- /* Check if we have socket listening on psm */
- parent = l2cap_get_sock_by_psm(BT_LISTEN, psm, conn->src);
- if (!parent) {
- result = L2CAP_CR_BAD_PSM;
- goto sendresp;
- }
- bh_lock_sock(parent);
- /* Check if the ACL is secure enough (if not SDP) */
- if (psm != cpu_to_le16(0x0001) &&
- !hci_conn_check_link_mode(conn->hcon)) {
- conn->disc_reason = 0x05;
- result = L2CAP_CR_SEC_BLOCK;
- goto response;
- }
- result = L2CAP_CR_NO_MEM;
- /* Check for backlog size */
- if (sk_acceptq_is_full(parent)) {
- BT_DBG("backlog full %d", parent->sk_ack_backlog);
- goto response;
- }
- sk = l2cap_sock_alloc(sock_net(parent), NULL, BTPROTO_L2CAP, GFP_ATOMIC);
- if (!sk)
- goto response;
- write_lock_bh(&list->lock);
- /* Check if we already have channel with that dcid */
- if (__l2cap_get_chan_by_dcid(list, scid)) {
- write_unlock_bh(&list->lock);
- sock_set_flag(sk, SOCK_ZAPPED);
- l2cap_sock_kill(sk);
- sk = NULL;
- goto response;
- }
- hci_conn_hold(conn->hcon);
- l2cap_sock_init(sk, parent);
- bacpy(&bt_sk(sk)->src, conn->src);
- bacpy(&bt_sk(sk)->dst, conn->dst);
- l2cap_pi(sk)->psm = psm;
- l2cap_pi(sk)->dcid = scid;
- bt_accept_enqueue(parent, sk);
- __l2cap_chan_add(conn, sk);
- dcid = l2cap_pi(sk)->scid;
- l2cap_pi(sk)->amp_id = amp_id;
- l2cap_sock_set_timer(sk, sk->sk_sndtimeo);
- l2cap_pi(sk)->ident = cmd->ident;
- if (conn->info_state & L2CAP_INFO_FEAT_MASK_REQ_DONE) {
- if (l2cap_check_security(sk)) {
- if (bt_sk(sk)->defer_setup) {
- sk->sk_state = BT_CONNECT2;
- result = L2CAP_CR_PEND;
- status = L2CAP_CS_AUTHOR_PEND;
- parent->sk_data_ready(parent, 0);
- } else {
- /* Force pending result for AMP controllers.
- * The connection will succeed after the
- * physical link is up. */
- if (amp_id) {
- sk->sk_state = BT_CONNECT2;
- result = L2CAP_CR_PEND;
- } else {
- sk->sk_state = BT_CONFIG;
- result = L2CAP_CR_SUCCESS;
- }
- status = L2CAP_CS_NO_INFO;
- }
- } else {
- sk->sk_state = BT_CONNECT2;
- result = L2CAP_CR_PEND;
- status = L2CAP_CS_AUTHEN_PEND;
- }
- } else {
- sk->sk_state = BT_CONNECT2;
- result = L2CAP_CR_PEND;
- status = L2CAP_CS_NO_INFO;
- }
- write_unlock_bh(&list->lock);
- response:
- bh_unlock_sock(parent);
- sendresp:
- rsp.scid = cpu_to_le16(scid);
- rsp.dcid = cpu_to_le16(dcid);
- rsp.result = cpu_to_le16(result);
- rsp.status = cpu_to_le16(status);
- l2cap_send_cmd(conn, cmd->ident, rsp_code, sizeof(rsp), &rsp);
- if (!(conn->info_state & L2CAP_INFO_FEAT_MASK_REQ_DONE)) {
- struct l2cap_info_req info;
- info.type = cpu_to_le16(L2CAP_IT_FEAT_MASK);
- conn->info_state |= L2CAP_INFO_FEAT_MASK_REQ_SENT;
- conn->info_ident = l2cap_get_ident(conn);
- mod_timer(&conn->info_timer, jiffies +
- msecs_to_jiffies(L2CAP_INFO_TIMEOUT));
- l2cap_send_cmd(conn, conn->info_ident,
- L2CAP_INFO_REQ, sizeof(info), &info);
- }
- if (sk && !(l2cap_pi(sk)->conf_state & L2CAP_CONF_REQ_SENT) &&
- result == L2CAP_CR_SUCCESS) {
- u8 buf[128];
- l2cap_pi(sk)->conf_state |= L2CAP_CONF_REQ_SENT;
- l2cap_send_cmd(conn, l2cap_get_ident(conn), L2CAP_CONF_REQ,
- l2cap_build_conf_req(sk, buf, sizeof(buf)), buf);
- l2cap_pi(sk)->num_conf_req++;
- }
- return sk;
- }
- static inline int l2cap_connect_req(struct l2cap_conn *conn,
- struct l2cap_cmd_hdr *cmd, u16 cmd_len, u8 *data)
- {
- if (!l2cap_create_connect(conn, cmd, cmd_len, data, L2CAP_CONN_RSP, 0))
- return -EPROTO;
- else
- return 0;
- }
- static inline int l2cap_connect_rsp(struct l2cap_conn *conn,
- struct l2cap_cmd_hdr *cmd, u16 cmd_len,
- u8 *data)
- {
- struct l2cap_conn_rsp *rsp = (struct l2cap_conn_rsp *) data;
- u16 scid, dcid, result, status;
- struct sock *sk;
- u8 req[128];
- if (cmd_len < sizeof(*rsp))
- return -EPROTO;
- scid = __le16_to_cpu(rsp->scid);
- dcid = __le16_to_cpu(rsp->dcid);
- result = __le16_to_cpu(rsp->result);
- status = __le16_to_cpu(rsp->status);
- BT_DBG("dcid 0x%4.4x scid 0x%4.4x result 0x%2.2x status 0x%2.2x", dcid, scid, result, status);
- if (scid) {
- sk = l2cap_get_chan_by_scid(&conn->chan_list, scid);
- if (!sk)
- return -EFAULT;
- } else {
- sk = l2cap_get_chan_by_ident(&conn->chan_list, cmd->ident);
- if (!sk)
- return -EFAULT;
- }
- switch (result) {
- case L2CAP_CR_SUCCESS:
- sk->sk_state = BT_CONFIG;
- l2cap_pi(sk)->ident = 0;
- l2cap_pi(sk)->dcid = dcid;
- l2cap_pi(sk)->conf_state &= ~L2CAP_CONF_CONNECT_PEND;
- if (l2cap_pi(sk)->conf_state & L2CAP_CONF_REQ_SENT)
- break;
- l2cap_pi(sk)->conf_state |= L2CAP_CONF_REQ_SENT;
- l2cap_send_cmd(conn, l2cap_get_ident(conn), L2CAP_CONF_REQ,
- l2cap_build_conf_req(sk, req, sizeof(req)), req);
- l2cap_pi(sk)->num_conf_req++;
- break;
- case L2CAP_CR_PEND:
- l2cap_pi(sk)->conf_state |= L2CAP_CONF_CONNECT_PEND;
- break;
- default:
- /* don't delete l2cap channel if sk is owned by user */
- if (sock_owned_by_user(sk)) {
- sk->sk_state = BT_DISCONN;
- l2cap_sock_clear_timer(sk);
- l2cap_sock_set_timer(sk, HZ / 5);
- break;
- }
- l2cap_chan_del(sk, ECONNREFUSED);
- break;
- }
- bh_unlock_sock(sk);
- return 0;
- }
- static inline void set_default_fcs(struct l2cap_pinfo *pi)
- {
- /* FCS is enabled only in ERTM or streaming mode, if one or both
- * sides request it.
- */
- if (pi->mode != L2CAP_MODE_ERTM && pi->mode != L2CAP_MODE_STREAMING)
- pi->fcs = L2CAP_FCS_NONE;
- else if (!(pi->conf_state & L2CAP_CONF_NO_FCS_RECV))
- pi->fcs = L2CAP_FCS_CRC16;
- }
- static inline int l2cap_config_req(struct l2cap_conn *conn, struct l2cap_cmd_hdr *cmd, u16 cmd_len, u8 *data)
- {
- struct l2cap_conf_req *req = (struct l2cap_conf_req *) data;
- u16 dcid, flags;
- u8 rspbuf[64];
- struct l2cap_conf_rsp *rsp = (struct l2cap_conf_rsp *) rspbuf;
- struct sock *sk;
- int len;
- u8 amp_move_reconf = 0;
- if (cmd_len < sizeof(*req))
- return -EPROTO;
- dcid = __le16_to_cpu(req->dcid);
- flags = __le16_to_cpu(req->flags);
- BT_DBG("dcid 0x%4.4x flags 0x%2.2x", dcid, flags);
- sk = l2cap_get_chan_by_scid(&conn->chan_list, dcid);
- if (!sk)
- return -ENOENT;
- BT_DBG("sk_state 0x%2.2x rx_state 0x%2.2x "
- "reconf_state 0x%2.2x amp_id 0x%2.2x amp_move_id 0x%2.2x",
- sk->sk_state, l2cap_pi(sk)->rx_state,
- l2cap_pi(sk)->reconf_state, l2cap_pi(sk)->amp_id,
- l2cap_pi(sk)->amp_move_id);
- /* Detect a reconfig request due to channel move between
- * BR/EDR and AMP
- */
- if (sk->sk_state == BT_CONNECTED &&
- l2cap_pi(sk)->rx_state ==
- L2CAP_ERTM_RX_STATE_WAIT_P_FLAG_RECONFIGURE)
- l2cap_pi(sk)->reconf_state = L2CAP_RECONF_ACC;
- if (l2cap_pi(sk)->reconf_state != L2CAP_RECONF_NONE)
- amp_move_reconf = 1;
- if (sk->sk_state != BT_CONFIG && !amp_move_reconf) {
- struct l2cap_cmd_rej rej;
- rej.reason = cpu_to_le16(0x0002);
- l2cap_send_cmd(conn, cmd->ident, L2CAP_COMMAND_REJ,
- sizeof(rej), &rej);
- goto unlock;
- }
- /* Reject if config buffer is too small. */
- len = cmd_len - sizeof(*req);
- if (l2cap_pi(sk)->conf_len + len > sizeof(l2cap_pi(sk)->conf_req)) {
- l2cap_send_cmd(conn, cmd->ident, L2CAP_CONF_RSP,
- l2cap_build_conf_rsp(sk, rspbuf,
- L2CAP_CONF_REJECT, flags), rspbuf);
- goto unlock;
- }
- /* Store config. */
- memcpy(l2cap_pi(sk)->conf_req + l2cap_pi(sk)->conf_len, req->data, len);
- l2cap_pi(sk)->conf_len += len;
- if (flags & 0x0001) {
- /* Incomplete config. Send empty response. */
- l2cap_send_cmd(conn, cmd->ident, L2CAP_CONF_RSP,
- l2cap_build_conf_rsp(sk, rspbuf,
- L2CAP_CONF_SUCCESS, 0x0001), rspbuf);
- goto unlock;
- }
- /* Complete config. */
- if (!amp_move_reconf)
- len = l2cap_parse_conf_req(sk, rspbuf, sizeof(rspbuf));
- else
- len = l2cap_parse_amp_move_reconf_req(sk, rspbuf, sizeof(rspbuf));
- if (len < 0) {
- l2cap_send_disconn_req(conn, sk, ECONNRESET);
- goto unlock;
- }
- l2cap_pi(sk)->conf_ident = cmd->ident;
- l2cap_send_cmd(conn, cmd->ident, L2CAP_CONF_RSP, len, rspbuf);
- if (l2cap_pi(sk)->conf_state & L2CAP_CONF_LOCKSTEP &&
- rsp->result == cpu_to_le16(L2CAP_CONF_PENDING) &&
- !l2cap_pi(sk)->amp_id) {
- /* Send success response right after pending if using
- * lockstep config on BR/EDR
- */
- rsp->result = cpu_to_le16(L2CAP_CONF_SUCCESS);
- l2cap_pi(sk)->conf_state |= L2CAP_CONF_OUTPUT_DONE;
- l2cap_send_cmd(conn, cmd->ident, L2CAP_CONF_RSP, len, rspbuf);
- }
- /* Reset config buffer. */
- l2cap_pi(sk)->conf_len = 0;
- if (amp_move_reconf)
- goto unlock;
- l2cap_pi(sk)->num_conf_rsp++;
- if (!(l2cap_pi(sk)->conf_state & L2CAP_CONF_OUTPUT_DONE))
- goto unlock;
- if (l2cap_pi(sk)->conf_state & L2CAP_CONF_INPUT_DONE) {
- set_default_fcs(l2cap_pi(sk));
- sk->sk_state = BT_CONNECTED;
- if (l2cap_pi(sk)->mode == L2CAP_MODE_ERTM ||
- l2cap_pi(sk)->mode == L2CAP_MODE_STREAMING)
- l2cap_ertm_init(sk);
- l2cap_chan_ready(sk);
- goto unlock;
- }
- if (!(l2cap_pi(sk)->conf_state & L2CAP_CONF_REQ_SENT)) {
- u8 buf[64];
- l2cap_pi(sk)->conf_state |= L2CAP_CONF_REQ_SENT;
- l2cap_send_cmd(conn, l2cap_get_ident(conn), L2CAP_CONF_REQ,
- l2cap_build_conf_req(sk, buf, sizeof(buf)), buf);
- l2cap_pi(sk)->num_conf_req++;
- }
- unlock:
- bh_unlock_sock(sk);
- return 0;
- }
- static inline int l2cap_config_rsp(struct l2cap_conn *conn,
- struct l2cap_cmd_hdr *cmd, u16 cmd_len,
- u8 *data)
- {
- struct l2cap_conf_rsp *rsp = (struct l2cap_conf_rsp *)data;
- u16 scid, flags, result;
- struct sock *sk;
- struct l2cap_pinfo *pi;
- int len = cmd->len - sizeof(*rsp);
- if (cmd_len != sizeof(*rsp))
- return -EPROTO;
- scid = __le16_to_cpu(rsp->scid);
- flags = __le16_to_cpu(rsp->flags);
- result = __le16_to_cpu(rsp->result);
- BT_DBG("scid 0x%4.4x flags 0x%2.2x result 0x%2.2x",
- scid, flags, result);
- sk = l2cap_get_chan_by_scid(&conn->chan_list, scid);
- if (!sk)
- return 0;
- pi = l2cap_pi(sk);
- if (pi->reconf_state != L2CAP_RECONF_NONE) {
- l2cap_amp_move_reconf_rsp(sk, rsp->data, len, result);
- goto done;
- }
- switch (result) {
- case L2CAP_CONF_SUCCESS:
- if (pi->conf_state & L2CAP_CONF_LOCKSTEP &&
- !(pi->conf_state & L2CAP_CONF_LOCKSTEP_PEND)) {
- /* Lockstep procedure requires a pending response
- * before success.
- */
- l2cap_send_disconn_req(conn, sk, ECONNRESET);
- goto done;
- }
- l2cap_conf_rfc_get(sk, rsp->data, len);
- break;
- case L2CAP_CONF_PENDING:
- if (!(pi->conf_state & L2CAP_CONF_LOCKSTEP)) {
- l2cap_send_disconn_req(conn, sk, ECONNRESET);
- goto done;
- }
- l2cap_conf_rfc_get(sk, rsp->data, len);
- pi->conf_state |= L2CAP_CONF_LOCKSTEP_PEND;
- l2cap_conf_ext_fs_get(sk, rsp->data, len);
- if (pi->amp_id && pi->conf_state & L2CAP_CONF_PEND_SENT) {
- struct hci_chan *chan;
- /* Already sent a 'pending' response, so set up
- * the logical link now
- */
- chan = l2cap_chan_admit(pi->amp_id, sk);
- if (!chan) {
- l2cap_send_disconn_req(pi->conn, sk,
- ECONNRESET);
- goto done;
- }
- if (chan->state == BT_CONNECTED)
- l2cap_create_cfm(chan, 0);
- }
- goto done;
- case L2CAP_CONF_UNACCEPT:
- if (pi->num_conf_rsp <= L2CAP_CONF_MAX_CONF_RSP) {
- char req[64];
- if (len > sizeof(req) - sizeof(struct l2cap_conf_req)) {
- l2cap_send_disconn_req(conn, sk, ECONNRESET);
- goto done;
- }
- /* throw out any old stored conf requests */
- result = L2CAP_CONF_SUCCESS;
- len = l2cap_parse_conf_rsp(sk, rsp->data,
- len, req, sizeof(req), &result);
- if (len < 0) {
- l2cap_send_disconn_req(conn, sk, ECONNRESET);
- goto done;
- }
- l2cap_send_cmd(conn, l2cap_get_ident(conn),
- L2CAP_CONF_REQ, len, req);
- pi->num_conf_req++;
- if (result != L2CAP_CONF_SUCCESS)
- goto done;
- break;
- }
- default:
- sk->sk_err = ECONNRESET;
- l2cap_sock_set_timer(sk, HZ * 5);
- l2cap_send_disconn_req(conn, sk, ECONNRESET);
- goto done;
- }
- if (flags & 0x01)
- goto done;
- pi->conf_state |= L2CAP_CONF_INPUT_DONE;
- if (pi->conf_state & L2CAP_CONF_OUTPUT_DONE) {
- set_default_fcs(pi);
- sk->sk_state = BT_CONNECTED;
- if (pi->mode == L2CAP_MODE_ERTM ||
- pi->mode == L2CAP_MODE_STREAMING)
- l2cap_ertm_init(sk);
- l2cap_chan_ready(sk);
- }
- done:
- bh_unlock_sock(sk);
- return 0;
- }
- static inline int l2cap_disconnect_req(struct l2cap_conn *conn,
- struct l2cap_cmd_hdr *cmd, u16 cmd_len,
- u8 *data)
- {
- struct l2cap_disconn_req *req = (struct l2cap_disconn_req *) data;
- struct l2cap_disconn_rsp rsp;
- u16 dcid, scid;
- struct sock *sk;
- if (cmd_len != sizeof(rsp))
- return -EPROTO;
- scid = __le16_to_cpu(req->scid);
- dcid = __le16_to_cpu(req->dcid);
- BT_DBG("scid 0x%4.4x dcid 0x%4.4x", scid, dcid);
- sk = l2cap_get_chan_by_scid(&conn->chan_list, dcid);
- if (!sk)
- return 0;
- rsp.dcid = cpu_to_le16(l2cap_pi(sk)->scid);
- rsp.scid = cpu_to_le16(l2cap_pi(sk)->dcid);
- l2cap_send_cmd(conn, cmd->ident, L2CAP_DISCONN_RSP, sizeof(rsp), &rsp);
- /* Only do cleanup if a disconnect request was not sent already */
- if (sk->sk_state != BT_DISCONN) {
- sk->sk_shutdown = SHUTDOWN_MASK;
- sk->sk_send_head = NULL;
- skb_queue_purge(TX_QUEUE(sk));
- if (l2cap_pi(sk)->mode == L2CAP_MODE_ERTM) {
- skb_queue_purge(SREJ_QUEUE(sk));
- __cancel_delayed_work(&l2cap_pi(sk)->ack_work);
- __cancel_delayed_work(&l2cap_pi(sk)->retrans_work);
- __cancel_delayed_work(&l2cap_pi(sk)->monitor_work);
- }
- }
- /* don't delete l2cap channel if sk is owned by user */
- if (sock_owned_by_user(sk)) {
- sk->sk_state = BT_DISCONN;
- l2cap_sock_clear_timer(sk);
- l2cap_sock_set_timer(sk, HZ / 5);
- bh_unlock_sock(sk);
- return 0;
- }
- l2cap_chan_del(sk, ECONNRESET);
- bh_unlock_sock(sk);
- l2cap_sock_kill(sk);
- return 0;
- }
- static inline int l2cap_disconnect_rsp(struct l2cap_conn *conn,
- struct l2cap_cmd_hdr *cmd, u16 cmd_len,
- u8 *data)
- {
- struct l2cap_disconn_rsp *rsp = (struct l2cap_disconn_rsp *) data;
- u16 dcid, scid;
- struct sock *sk;
- if (cmd_len != sizeof(*rsp))
- return -EPROTO;
- scid = __le16_to_cpu(rsp->scid);
- dcid = __le16_to_cpu(rsp->dcid);
- BT_DBG("dcid 0x%4.4x scid 0x%4.4x", dcid, scid);
- sk = l2cap_get_chan_by_scid(&conn->chan_list, scid);
- if (!sk)
- return 0;
- /* don't delete l2cap channel if sk is owned by user */
- if (sock_owned_by_user(sk)) {
- sk->sk_state = BT_DISCONN;
- l2cap_sock_clear_timer(sk);
- l2cap_sock_set_timer(sk, HZ / 5);
- bh_unlock_sock(sk);
- return 0;
- }
- l2cap_chan_del(sk, 0);
- bh_unlock_sock(sk);
- l2cap_sock_kill(sk);
- return 0;
- }
- static inline int l2cap_information_req(struct l2cap_conn *conn,
- struct l2cap_cmd_hdr *cmd, u16 cmd_len,
- u8 *data)
- {
- struct l2cap_info_req *req = (struct l2cap_info_req *) data;
- u16 type;
- if (cmd_len != sizeof(*req))
- return -EPROTO;
- type = __le16_to_cpu(req->type);
- BT_DBG("type 0x%4.4x", type);
- if (type == L2CAP_IT_FEAT_MASK) {
- u8 buf[8];
- u32 feat_mask = l2cap_feat_mask;
- struct l2cap_info_rsp *rsp = (struct l2cap_info_rsp *) buf;
- rsp->type = cpu_to_le16(L2CAP_IT_FEAT_MASK);
- rsp->result = cpu_to_le16(L2CAP_IR_SUCCESS);
- if (!disable_ertm)
- feat_mask |= L2CAP_FEAT_ERTM | L2CAP_FEAT_STREAMING
- | L2CAP_FEAT_FCS | L2CAP_FEAT_EXT_WINDOW;
- put_unaligned_le32(feat_mask, rsp->data);
- l2cap_send_cmd(conn, cmd->ident,
- L2CAP_INFO_RSP, sizeof(buf), buf);
- } else if (type == L2CAP_IT_FIXED_CHAN) {
- u8 buf[12];
- u8 fc_mask = l2cap_fc_mask;
- struct l2cap_info_rsp *rsp = (struct l2cap_info_rsp *) buf;
- rsp->type = cpu_to_le16(L2CAP_IT_FIXED_CHAN);
- rsp->result = cpu_to_le16(L2CAP_IR_SUCCESS);
- if (enable_hs)
- fc_mask |= L2CAP_FC_A2MP;
- memset(rsp->data, 0, 8);
- rsp->data[0] = fc_mask;
- l2cap_send_cmd(conn, cmd->ident,
- L2CAP_INFO_RSP, sizeof(buf), buf);
- } else {
- struct l2cap_info_rsp rsp;
- rsp.type = cpu_to_le16(type);
- rsp.result = cpu_to_le16(L2CAP_IR_NOTSUPP);
- l2cap_send_cmd(conn, cmd->ident,
- L2CAP_INFO_RSP, sizeof(rsp), &rsp);
- }
- return 0;
- }
- static inline int l2cap_information_rsp(struct l2cap_conn *conn,
- struct l2cap_cmd_hdr *cmd, u16 cmd_len,
- u8 *data)
- {
- struct l2cap_info_rsp *rsp = (struct l2cap_info_rsp *) data;
- u16 type, result;
- if (cmd_len < sizeof(*rsp))
- return -EPROTO;
- type = __le16_to_cpu(rsp->type);
- result = __le16_to_cpu(rsp->result);
- BT_DBG("type 0x%4.4x result 0x%2.2x", type, result);
- /* L2CAP Info req/rsp are unbound to channels, add extra checks */
- if (cmd->ident != conn->info_ident ||
- conn->info_state & L2CAP_INFO_FEAT_MASK_REQ_DONE)
- return 0;
- del_timer(&conn->info_timer);
- if (result != L2CAP_IR_SUCCESS) {
- conn->info_state |= L2CAP_INFO_FEAT_MASK_REQ_DONE;
- conn->info_ident = 0;
- l2cap_conn_start(conn);
- return 0;
- }
- if (type == L2CAP_IT_FEAT_MASK) {
- conn->feat_mask = get_unaligned_le32(rsp->data);
- if (conn->feat_mask & L2CAP_FEAT_FIXED_CHAN) {
- struct l2cap_info_req req;
- req.type = cpu_to_le16(L2CAP_IT_FIXED_CHAN);
- conn->info_ident = l2cap_get_ident(conn);
- l2cap_send_cmd(conn, conn->info_ident,
- L2CAP_INFO_REQ, sizeof(req), &req);
- } else {
- conn->info_state |= L2CAP_INFO_FEAT_MASK_REQ_DONE;
- conn->info_ident = 0;
- l2cap_conn_start(conn);
- }
- } else if (type == L2CAP_IT_FIXED_CHAN) {
- conn->fc_mask = rsp->data[0];
- conn->info_state |= L2CAP_INFO_FEAT_MASK_REQ_DONE;
- conn->info_ident = 0;
- l2cap_conn_start(conn);
- }
- return 0;
- }
- static void l2cap_send_move_chan_req(struct l2cap_conn *conn,
- struct l2cap_pinfo *pi, u16 icid, u8 dest_amp_id)
- {
- struct l2cap_move_chan_req req;
- u8 ident;
- BT_DBG("pi %p, icid %d, dest_amp_id %d", pi, (int) icid,
- (int) dest_amp_id);
- ident = l2cap_get_ident(conn);
- if (pi)
- pi->ident = ident;
- req.icid = cpu_to_le16(icid);
- req.dest_amp_id = dest_amp_id;
- l2cap_send_cmd(conn, ident, L2CAP_MOVE_CHAN_REQ, sizeof(req), &req);
- }
- static void l2cap_send_move_chan_rsp(struct l2cap_conn *conn, u8 ident,
- u16 icid, u16 result)
- {
- struct l2cap_move_chan_rsp rsp;
- BT_DBG("icid %d, result %d", (int) icid, (int) result);
- rsp.icid = cpu_to_le16(icid);
- rsp.result = cpu_to_le16(result);
- l2cap_send_cmd(conn, ident, L2CAP_MOVE_CHAN_RSP, sizeof(rsp), &rsp);
- }
- static void l2cap_send_move_chan_cfm(struct l2cap_conn *conn,
- struct l2cap_pinfo *pi, u16 icid, u16 result)
- {
- struct l2cap_move_chan_cfm cfm;
- u8 ident;
- BT_DBG("icid %d, result %d", (int) icid, (int) result);
- ident = l2cap_get_ident(conn);
- if (pi)
- pi->ident = ident;
- cfm.icid = cpu_to_le16(icid);
- cfm.result = cpu_to_le16(result);
- l2cap_send_cmd(conn, ident, L2CAP_MOVE_CHAN_CFM, sizeof(cfm), &cfm);
- }
- static void l2cap_send_move_chan_cfm_rsp(struct l2cap_conn *conn, u8 ident,
- u16 icid)
- {
- struct l2cap_move_chan_cfm_rsp rsp;
- BT_DBG("icid %d", (int) icid);
- rsp.icid = cpu_to_le16(icid);
- l2cap_send_cmd(conn, ident, L2CAP_MOVE_CHAN_CFM_RSP, sizeof(rsp), &rsp);
- }
- static inline int l2cap_create_channel_req(struct l2cap_conn *conn,
- struct l2cap_cmd_hdr *cmd, u16 cmd_len,
- u8 *data)
- {
- struct l2cap_create_chan_req *req =
- (struct l2cap_create_chan_req *) data;
- struct sock *sk;
- u16 psm, scid;
- psm = le16_to_cpu(req->psm);
- scid = le16_to_cpu(req->scid);
- BT_DBG("psm %d, scid %d, amp_id %d", (int) psm, (int) scid,
- (int) req->amp_id);
- if (req->amp_id) {
- struct hci_dev *hdev;
- /* Validate AMP controller id */
- hdev = hci_dev_get(req->amp_id);
- if (!hdev || !test_bit(HCI_UP, &hdev->flags)) {
- struct l2cap_create_chan_rsp rsp;
- rsp.dcid = 0;
- rsp.scid = cpu_to_le16(scid);
- rsp.result = L2CAP_CREATE_CHAN_REFUSED_CONTROLLER;
- rsp.status = L2CAP_CREATE_CHAN_STATUS_NONE;
- l2cap_send_cmd(conn, cmd->ident, L2CAP_CREATE_CHAN_RSP,
- sizeof(rsp), &rsp);
- if (hdev)
- hci_dev_put(hdev);
- return 0;
- }
- hci_dev_put(hdev);
- }
- sk = l2cap_create_connect(conn, cmd, cmd_len, data, L2CAP_CREATE_CHAN_RSP,
- req->amp_id);
- if (sk)
- l2cap_pi(sk)->conf_state |= L2CAP_CONF_LOCKSTEP;
- if (sk && req->amp_id &&
- (conn->info_state & L2CAP_INFO_FEAT_MASK_REQ_DONE))
- amp_accept_physical(conn, req->amp_id, sk);
- return 0;
- }
- static inline int l2cap_create_channel_rsp(struct l2cap_conn *conn,
- struct l2cap_cmd_hdr *cmd, u16 cmd_len,
- u8 *data)
- {
- BT_DBG("conn %p", conn);
- return l2cap_connect_rsp(conn, cmd, cmd_len, data);
- }
- static inline int l2cap_move_channel_req(struct l2cap_conn *conn,
- struct l2cap_cmd_hdr *cmd, u8 *data)
- {
- struct l2cap_move_chan_req *req = (struct l2cap_move_chan_req *) data;
- struct sock *sk;
- struct l2cap_pinfo *pi;
- u16 icid = 0;
- u16 result = L2CAP_MOVE_CHAN_REFUSED_NOT_ALLOWED;
- icid = le16_to_cpu(req->icid);
- BT_DBG("icid %d, dest_amp_id %d", (int) icid, (int) req->dest_amp_id);
- read_lock(&conn->chan_list.lock);
- sk = __l2cap_get_chan_by_dcid(&conn->chan_list, icid);
- read_unlock(&conn->chan_list.lock);
- if (!sk)
- goto send_move_response;
- lock_sock(sk);
- pi = l2cap_pi(sk);
- if (pi->scid < L2CAP_CID_DYN_START ||
- (pi->mode != L2CAP_MODE_ERTM &&
- pi->mode != L2CAP_MODE_STREAMING)) {
- goto send_move_response;
- }
- if (pi->amp_id == req->dest_amp_id) {
- result = L2CAP_MOVE_CHAN_REFUSED_SAME_ID;
- goto send_move_response;
- }
- if (req->dest_amp_id) {
- struct hci_dev *hdev;
- hdev = hci_dev_get(req->dest_amp_id);
- if (!hdev || !test_bit(HCI_UP, &hdev->flags)) {
- if (hdev)
- hci_dev_put(hdev);
- result = L2CAP_MOVE_CHAN_REFUSED_CONTROLLER;
- goto send_move_response;
- }
- hci_dev_put(hdev);
- }
- if (((pi->amp_move_state != L2CAP_AMP_STATE_STABLE &&
- pi->amp_move_state != L2CAP_AMP_STATE_WAIT_PREPARE) ||
- pi->amp_move_role != L2CAP_AMP_MOVE_NONE) &&
- bacmp(conn->src, conn->dst) > 0) {
- result = L2CAP_MOVE_CHAN_REFUSED_COLLISION;
- goto send_move_response;
- }
- if (pi->amp_pref == BT_AMP_POLICY_REQUIRE_BR_EDR) {
- result = L2CAP_MOVE_CHAN_REFUSED_NOT_ALLOWED;
- goto send_move_response;
- }
- pi->amp_move_cmd_ident = cmd->ident;
- pi->amp_move_role = L2CAP_AMP_MOVE_RESPONDER;
- l2cap_amp_move_setup(sk);
- pi->amp_move_id = req->dest_amp_id;
- icid = pi->dcid;
- if (req->dest_amp_id == 0) {
- /* Moving to BR/EDR */
- if (pi->conn_state & L2CAP_CONN_LOCAL_BUSY) {
- pi->amp_move_state = L2CAP_AMP_STATE_WAIT_LOCAL_BUSY;
- result = L2CAP_MOVE_CHAN_PENDING;
- } else {
- pi->amp_move_state = L2CAP_AMP_STATE_WAIT_MOVE_CONFIRM;
- result = L2CAP_MOVE_CHAN_SUCCESS;
- }
- } else {
- pi->amp_move_state = L2CAP_AMP_STATE_WAIT_PREPARE;
- amp_accept_physical(pi->conn, req->dest_amp_id, sk);
- result = L2CAP_MOVE_CHAN_PENDING;
- }
- send_move_response:
- l2cap_send_move_chan_rsp(conn, cmd->ident, icid, result);
- if (sk)
- release_sock(sk);
- return 0;
- }
- static inline int l2cap_move_channel_rsp(struct l2cap_conn *conn,
- struct l2cap_cmd_hdr *cmd, u8 *data)
- {
- struct l2cap_move_chan_rsp *rsp = (struct l2cap_move_chan_rsp *) data;
- struct sock *sk;
- struct l2cap_pinfo *pi;
- u16 icid, result;
- icid = le16_to_cpu(rsp->icid);
- result = le16_to_cpu(rsp->result);
- BT_DBG("icid %d, result %d", (int) icid, (int) result);
- switch (result) {
- case L2CAP_MOVE_CHAN_SUCCESS:
- case L2CAP_MOVE_CHAN_PENDING:
- read_lock(&conn->chan_list.lock);
- sk = __l2cap_get_chan_by_scid(&conn->chan_list, icid);
- read_unlock(&conn->chan_list.lock);
- if (!sk) {
- l2cap_send_move_chan_cfm(conn, NULL, icid,
- L2CAP_MOVE_CHAN_UNCONFIRMED);
- break;
- }
- lock_sock(sk);
- pi = l2cap_pi(sk);
- l2cap_sock_clear_timer(sk);
- if (result == L2CAP_MOVE_CHAN_PENDING)
- l2cap_sock_set_timer(sk, L2CAP_MOVE_ERTX_TIMEOUT);
- if (pi->amp_move_state ==
- L2CAP_AMP_STATE_WAIT_LOGICAL_COMPLETE) {
- /* Move confirm will be sent when logical link
- * is complete.
- */
- pi->amp_move_state =
- L2CAP_AMP_STATE_WAIT_LOGICAL_CONFIRM;
- } else if (pi->amp_move_state ==
- L2CAP_AMP_STATE_WAIT_MOVE_RSP_SUCCESS) {
- if (result == L2CAP_MOVE_CHAN_PENDING) {
- break;
- } else if (pi->conn_state & L2CAP_CONN_LOCAL_BUSY) {
- pi->amp_move_state =
- L2CAP_AMP_STATE_WAIT_LOCAL_BUSY;
- } else {
- /* Logical link is up or moving to BR/EDR,
- * proceed with move */
- pi->amp_move_state =
- L2CAP_AMP_STATE_WAIT_MOVE_CONFIRM_RSP;
- l2cap_send_move_chan_cfm(conn, pi, pi->scid,
- L2CAP_MOVE_CHAN_CONFIRMED);
- l2cap_sock_set_timer(sk, L2CAP_MOVE_TIMEOUT);
- }
- } else if (pi->amp_move_state ==
- L2CAP_AMP_STATE_WAIT_MOVE_RSP) {
- struct l2cap_conf_ext_fs default_fs = {1, 1, 0xFFFF,
- 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF};
- struct hci_chan *chan;
- /* Moving to AMP */
- if (result == L2CAP_MOVE_CHAN_SUCCESS) {
- /* Remote is ready, send confirm immediately
- * after logical link is ready
- */
- pi->amp_move_state =
- L2CAP_AMP_STATE_WAIT_LOGICAL_CONFIRM;
- } else {
- /* Both logical link and move success
- * are required to confirm
- */
- pi->amp_move_state =
- L2CAP_AMP_STATE_WAIT_LOGICAL_COMPLETE;
- }
- pi->remote_fs = default_fs;
- pi->local_fs = default_fs;
- chan = l2cap_chan_admit(pi->amp_move_id, sk);
- if (!chan) {
- /* Logical link not available */
- l2cap_send_move_chan_cfm(conn, pi, pi->scid,
- L2CAP_MOVE_CHAN_UNCONFIRMED);
- break;
- }
- if (chan->state == BT_CONNECTED) {
- /* Logical link is already ready to go */
- pi->ampcon = chan->conn;
- pi->ampcon->l2cap_data = pi->conn;
- if (result == L2CAP_MOVE_CHAN_SUCCESS) {
- /* Can confirm now */
- l2cap_send_move_chan_cfm(conn, pi,
- pi->scid,
- L2CAP_MOVE_CHAN_CONFIRMED);
- } else {
- /* Now only need move success
- * required to confirm
- */
- pi->amp_move_state =
- L2CAP_AMP_STATE_WAIT_MOVE_RSP_SUCCESS;
- }
- l2cap_create_cfm(chan, 0);
- }
- } else {
- /* Any other amp move state means the move failed. */
- pi->amp_move_id = pi->amp_id;
- pi->amp_move_state = L2CAP_AMP_STATE_STABLE;
- l2cap_amp_move_revert(sk);
- pi->amp_move_role = L2CAP_AMP_MOVE_NONE;
- l2cap_send_move_chan_cfm(conn, pi, pi->scid,
- L2CAP_MOVE_CHAN_UNCONFIRMED);
- l2cap_sock_set_timer(sk, L2CAP_MOVE_TIMEOUT);
- }
- break;
- default:
- /* Failed (including collision case) */
- read_lock(&conn->chan_list.lock);
- sk = __l2cap_get_chan_by_ident(&conn->chan_list, cmd->ident);
- read_unlock(&conn->chan_list.lock);
- if (!sk) {
- /* Could not locate channel, icid is best guess */
- l2cap_send_move_chan_cfm(conn, NULL, icid,
- L2CAP_MOVE_CHAN_UNCONFIRMED);
- break;
- }
- lock_sock(sk);
- pi = l2cap_pi(sk);
- l2cap_sock_clear_timer(sk);
- if (pi->amp_move_role == L2CAP_AMP_MOVE_INITIATOR) {
- if (result == L2CAP_MOVE_CHAN_REFUSED_COLLISION)
- pi->amp_move_role = L2CAP_AMP_MOVE_RESPONDER;
- else {
- /* Cleanup - cancel move */
- pi->amp_move_id = pi->amp_id;
- pi->amp_move_state = L2CAP_AMP_STATE_STABLE;
- l2cap_amp_move_revert(sk);
- pi->amp_move_role = L2CAP_AMP_MOVE_NONE;
- }
- }
- l2cap_send_move_chan_cfm(conn, pi, pi->scid,
- L2CAP_MOVE_CHAN_UNCONFIRMED);
- l2cap_sock_set_timer(sk, L2CAP_MOVE_TIMEOUT);
- break;
- }
- if (sk)
- release_sock(sk);
- return 0;
- }
- static inline int l2cap_move_channel_confirm(struct l2cap_conn *conn,
- struct l2cap_cmd_hdr *cmd, u8 *data)
- {
- struct l2cap_move_chan_cfm *cfm = (struct l2cap_move_chan_cfm *) data;
- struct sock *sk;
- struct l2cap_pinfo *pi;
- u16 icid, result;
- icid = le16_to_cpu(cfm->icid);
- result = le16_to_cpu(cfm->result);
- BT_DBG("icid %d, result %d", (int) icid, (int) result);
- read_lock(&conn->chan_list.lock);
- sk = __l2cap_get_chan_by_dcid(&conn->chan_list, icid);
- read_unlock(&conn->chan_list.lock);
- if (!sk) {
- BT_DBG("Bad channel (%d)", (int) icid);
- goto send_move_confirm_response;
- }
- lock_sock(sk);
- pi = l2cap_pi(sk);
- if (pi->amp_move_state == L2CAP_AMP_STATE_WAIT_MOVE_CONFIRM) {
- pi->amp_move_state = L2CAP_AMP_STATE_STABLE;
- if (result == L2CAP_MOVE_CHAN_CONFIRMED) {
- pi->amp_id = pi->amp_move_id;
- if (!pi->amp_id && pi->ampchan) {
- struct hci_chan *ampchan = pi->ampchan;
- struct hci_conn *ampcon = pi->ampcon;
- /* Have moved off of AMP, free the channel */
- pi->ampchan = NULL;
- pi->ampcon = NULL;
- if (hci_chan_put(ampchan))
- ampcon->l2cap_data = NULL;
- else
- l2cap_deaggregate(ampchan, pi);
- }
- l2cap_amp_move_success(sk);
- } else {
- pi->amp_move_id = pi->amp_id;
- l2cap_amp_move_revert(sk);
- }
- pi->amp_move_role = L2CAP_AMP_MOVE_NONE;
- } else if (pi->amp_move_state ==
- L2CAP_AMP_STATE_WAIT_LOGICAL_CONFIRM) {
- BT_DBG("Bad AMP_MOVE_STATE (%d)", pi->amp_move_state);
- }
- send_move_confirm_response:
- l2cap_send_move_chan_cfm_rsp(conn, cmd->ident, icid);
- if (sk)
- release_sock(sk);
- return 0;
- }
- static inline int l2cap_move_channel_confirm_rsp(struct l2cap_conn *conn,
- struct l2cap_cmd_hdr *cmd, u8 *data)
- {
- struct l2cap_move_chan_cfm_rsp *rsp =
- (struct l2cap_move_chan_cfm_rsp *) data;
- struct sock *sk;
- struct l2cap_pinfo *pi;
- u16 icid;
- icid = le16_to_cpu(rsp->icid);
- BT_DBG("icid %d", (int) icid);
- read_lock(&conn->chan_list.lock);
- sk = __l2cap_get_chan_by_scid(&conn->chan_list, icid);
- read_unlock(&conn->chan_list.lock);
- if (!sk)
- return 0;
- lock_sock(sk);
- pi = l2cap_pi(sk);
- l2cap_sock_clear_timer(sk);
- if (pi->amp_move_state ==
- L2CAP_AMP_STATE_WAIT_MOVE_CONFIRM_RSP) {
- pi->amp_move_state = L2CAP_AMP_STATE_STABLE;
- pi->amp_id = pi->amp_move_id;
- if (!pi->amp_id && pi->ampchan) {
- struct hci_chan *ampchan = pi->ampchan;
- struct hci_conn *ampcon = pi->ampcon;
- /* Have moved off of AMP, free the channel */
- pi->ampchan = NULL;
- pi->ampcon = NULL;
- if (hci_chan_put(ampchan))
- ampcon->l2cap_data = NULL;
- else
- l2cap_deaggregate(ampchan, pi);
- }
- l2cap_amp_move_success(sk);
- pi->amp_move_role = L2CAP_AMP_MOVE_NONE;
- }
- release_sock(sk);
- return 0;
- }
- static void l2cap_amp_signal_worker(struct work_struct *work)
- {
- int err = 0;
- struct l2cap_amp_signal_work *ampwork =
- container_of(work, struct l2cap_amp_signal_work, work);
- switch (ampwork->cmd.code) {
- case L2CAP_MOVE_CHAN_REQ:
- err = l2cap_move_channel_req(ampwork->conn, &work->cmd,
- ampwork->data);
- break;
- case L2CAP_MOVE_CHAN_RSP:
- err = l2cap_move_channel_rsp(ampwork->conn, &work->cmd,
- ampwork->data);
- break;
- case L2CAP_MOVE_CHAN_CFM:
- err = l2cap_move_channel_confirm(ampwork->conn, &work->cmd,
- ampwork->data);
- break;
- case L2CAP_MOVE_CHAN_CFM_RSP:
- err = l2cap_move_channel_confirm_rsp(ampwork->conn,
- &work->cmd, ampwork->data);
- break;
- default:
- BT_ERR("Unknown signaling command 0x%2.2x", ampwork->cmd.code);
- err = -EINVAL;
- break;
- }
- if (err) {
- struct l2cap_cmd_rej rej;
- BT_DBG("error %d", err);
- /* In this context, commands are only rejected with
- * "command not understood", code 0.
- */
- rej.reason = cpu_to_le16(0);
- l2cap_send_cmd(ampwork->conn, ampwork->cmd.ident,
- L2CAP_COMMAND_REJ, sizeof(rej), &rej);
- }
- kfree_skb(ampwork->skb);
- kfree(ampwork);
- }
- void l2cap_amp_physical_complete(int result, u8 local_id, u8 remote_id,
- struct sock *sk)
- {
- struct l2cap_pinfo *pi;
- BT_DBG("result %d, local_id %d, remote_id %d, sk %p", result,
- (int) local_id, (int) remote_id, sk);
- lock_sock(sk);
- if (sk->sk_state == BT_DISCONN || sk->sk_state == BT_CLOSED) {
- release_sock(sk);
- return;
- }
- pi = l2cap_pi(sk);
- if (sk->sk_state != BT_CONNECTED) {
- if (bt_sk(sk)->parent) {
- struct l2cap_conn_rsp rsp;
- char buf[128];
- rsp.scid = cpu_to_le16(l2cap_pi(sk)->dcid);
- rsp.dcid = cpu_to_le16(l2cap_pi(sk)->scid);
- /* Incoming channel on AMP */
- if (result == L2CAP_CREATE_CHAN_SUCCESS) {
- /* Send successful response */
- rsp.result = cpu_to_le16(L2CAP_CR_SUCCESS);
- rsp.status = cpu_to_le16(L2CAP_CS_NO_INFO);
- } else {
- /* Send negative response */
- rsp.result = cpu_to_le16(L2CAP_CR_NO_MEM);
- rsp.status = cpu_to_le16(L2CAP_CS_NO_INFO);
- }
- l2cap_send_cmd(pi->conn, pi->ident,
- L2CAP_CREATE_CHAN_RSP,
- sizeof(rsp), &rsp);
- if (result == L2CAP_CREATE_CHAN_SUCCESS) {
- sk->sk_state = BT_CONFIG;
- pi->conf_state |= L2CAP_CONF_REQ_SENT;
- l2cap_send_cmd(pi->conn,
- l2cap_get_ident(pi->conn),
- L2CAP_CONF_REQ,
- l2cap_build_conf_req(sk, buf, sizeof(buf)), buf);
- l2cap_pi(sk)->num_conf_req++;
- }
- } else {
- /* Outgoing channel on AMP */
- if (result != L2CAP_CREATE_CHAN_SUCCESS) {
- /* Revert to BR/EDR connect */
- l2cap_send_conn_req(sk);
- } else {
- pi->amp_id = local_id;
- l2cap_send_create_chan_req(sk, remote_id);
- }
- }
- } else if (result == L2CAP_MOVE_CHAN_SUCCESS &&
- pi->amp_move_role == L2CAP_AMP_MOVE_INITIATOR) {
- l2cap_amp_move_setup(sk);
- pi->amp_move_id = local_id;
- pi->amp_move_state = L2CAP_AMP_STATE_WAIT_MOVE_RSP;
- l2cap_send_move_chan_req(pi->conn, pi, pi->scid, remote_id);
- l2cap_sock_set_timer(sk, L2CAP_MOVE_TIMEOUT);
- } else if (result == L2CAP_MOVE_CHAN_SUCCESS &&
- pi->amp_move_role == L2CAP_AMP_MOVE_RESPONDER) {
- struct hci_chan *chan;
- struct l2cap_conf_ext_fs default_fs = {1, 1, 0xFFFF,
- 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF};
- pi->remote_fs = default_fs;
- pi->local_fs = default_fs;
- chan = l2cap_chan_admit(local_id, sk);
- if (chan) {
- if (chan->state == BT_CONNECTED) {
- /* Logical link is ready to go */
- pi->ampcon = chan->conn;
- pi->ampcon->l2cap_data = pi->conn;
- pi->amp_move_state =
- L2CAP_AMP_STATE_WAIT_MOVE_CONFIRM;
- l2cap_send_move_chan_rsp(pi->conn,
- pi->amp_move_cmd_ident, pi->dcid,
- L2CAP_MOVE_CHAN_SUCCESS);
- l2cap_create_cfm(chan, 0);
- } else {
- /* Wait for logical link to be ready */
- pi->amp_move_state =
- L2CAP_AMP_STATE_WAIT_LOGICAL_CONFIRM;
- }
- } else {
- /* Logical link not available */
- l2cap_send_move_chan_rsp(pi->conn,
- pi->amp_move_cmd_ident, pi->dcid,
- L2CAP_MOVE_CHAN_REFUSED_NOT_ALLOWED);
- }
- } else {
- BT_DBG("result %d, role %d, local_busy %d", result,
- (int) pi->amp_move_role,
- (int) ((pi->conn_state & L2CAP_CONN_LOCAL_BUSY) != 0));
- if (pi->amp_move_role == L2CAP_AMP_MOVE_RESPONDER) {
- if (result == -EINVAL)
- l2cap_send_move_chan_rsp(pi->conn,
- pi->amp_move_cmd_ident, pi->dcid,
- L2CAP_MOVE_CHAN_REFUSED_CONTROLLER);
- else
- l2cap_send_move_chan_rsp(pi->conn,
- pi->amp_move_cmd_ident, pi->dcid,
- L2CAP_MOVE_CHAN_REFUSED_NOT_ALLOWED);
- }
- pi->amp_move_role = L2CAP_AMP_MOVE_NONE;
- pi->amp_move_state = L2CAP_AMP_STATE_STABLE;
- if ((l2cap_pi(sk)->conn_state & L2CAP_CONN_LOCAL_BUSY) &&
- l2cap_rmem_available(sk))
- l2cap_ertm_tx(sk, 0, 0,
- L2CAP_ERTM_EVENT_LOCAL_BUSY_CLEAR);
- /* Restart data transmission */
- l2cap_ertm_send(sk);
- }
- release_sock(sk);
- }
- static void l2cap_logical_link_complete(struct hci_chan *chan, u8 status)
- {
- struct l2cap_pinfo *pi;
- struct sock *sk;
- struct hci_chan *ampchan;
- struct hci_conn *ampcon;
- BT_DBG("status %d, chan %p, conn %p", (int) status, chan, chan->conn);
- sk = chan->l2cap_sk;
- chan->l2cap_sk = NULL;
- BT_DBG("sk %p", sk);
- lock_sock(sk);
- if (sk->sk_state != BT_CONNECTED && !l2cap_pi(sk)->amp_id) {
- release_sock(sk);
- return;
- }
- pi = l2cap_pi(sk);
- if ((!status) && (chan != NULL)) {
- pi->ampcon = chan->conn;
- pi->ampcon->l2cap_data = pi->conn;
- BT_DBG("amp_move_state %d", pi->amp_move_state);
- if (sk->sk_state != BT_CONNECTED) {
- struct l2cap_conf_rsp rsp;
- /* Must use spinlock to prevent concurrent
- * execution of l2cap_config_rsp()
- */
- bh_lock_sock(sk);
- l2cap_send_cmd(pi->conn, pi->conf_ident, L2CAP_CONF_RSP,
- l2cap_build_conf_rsp(sk, &rsp,
- L2CAP_CONF_SUCCESS, 0), &rsp);
- pi->conf_state |= L2CAP_CONF_OUTPUT_DONE;
- if (l2cap_pi(sk)->conf_state & L2CAP_CONF_INPUT_DONE) {
- set_default_fcs(l2cap_pi(sk));
- sk->sk_state = BT_CONNECTED;
- if (l2cap_pi(sk)->mode == L2CAP_MODE_ERTM ||
- l2cap_pi(sk)->mode == L2CAP_MODE_STREAMING)
- l2cap_ertm_init(sk);
- l2cap_chan_ready(sk);
- }
- bh_unlock_sock(sk);
- } else if (pi->amp_move_state ==
- L2CAP_AMP_STATE_WAIT_LOGICAL_COMPLETE) {
- /* Move confirm will be sent after a success
- * response is received
- */
- pi->amp_move_state =
- L2CAP_AMP_STATE_WAIT_MOVE_RSP_SUCCESS;
- } else if (pi->amp_move_state ==
- L2CAP_AMP_STATE_WAIT_LOGICAL_CONFIRM) {
- if (pi->conn_state & L2CAP_CONN_LOCAL_BUSY)
- pi->amp_move_state =
- L2CAP_AMP_STATE_WAIT_LOCAL_BUSY;
- else if (pi->amp_move_role ==
- L2CAP_AMP_MOVE_INITIATOR) {
- pi->amp_move_state =
- L2CAP_AMP_STATE_WAIT_MOVE_CONFIRM_RSP;
- l2cap_send_move_chan_cfm(pi->conn, pi, pi->scid,
- L2CAP_MOVE_CHAN_SUCCESS);
- l2cap_sock_set_timer(sk, L2CAP_MOVE_TIMEOUT);
- } else if (pi->amp_move_role ==
- L2CAP_AMP_MOVE_RESPONDER) {
- pi->amp_move_state =
- L2CAP_AMP_STATE_WAIT_MOVE_CONFIRM;
- l2cap_send_move_chan_rsp(pi->conn,
- pi->amp_move_cmd_ident, pi->dcid,
- L2CAP_MOVE_CHAN_SUCCESS);
- }
- } else if ((pi->amp_move_state !=
- L2CAP_AMP_STATE_WAIT_MOVE_RSP_SUCCESS) &&
- (pi->amp_move_state !=
- L2CAP_AMP_STATE_WAIT_MOVE_CONFIRM) &&
- (pi->amp_move_state !=
- L2CAP_AMP_STATE_WAIT_MOVE_CONFIRM_RSP)) {
- /* Move was not in expected state, free the channel */
- ampchan = pi->ampchan;
- ampcon = pi->ampcon;
- pi->ampchan = NULL;
- pi->ampcon = NULL;
- if (ampchan) {
- if (hci_chan_put(ampchan))
- ampcon->l2cap_data = NULL;
- else
- l2cap_deaggregate(ampchan, pi);
- }
- pi->amp_move_state = L2CAP_AMP_STATE_STABLE;
- }
- } else {
- /* Logical link setup failed. */
- if (sk->sk_state != BT_CONNECTED)
- l2cap_send_disconn_req(pi->conn, sk, ECONNRESET);
- else if (pi->amp_move_role == L2CAP_AMP_MOVE_RESPONDER) {
- l2cap_amp_move_revert(sk);
- l2cap_pi(sk)->amp_move_role = L2CAP_AMP_MOVE_NONE;
- pi->amp_move_state = L2CAP_AMP_STATE_STABLE;
- l2cap_send_move_chan_rsp(pi->conn,
- pi->amp_move_cmd_ident, pi->dcid,
- L2CAP_MOVE_CHAN_REFUSED_CONFIG);
- } else if (pi->amp_move_role == L2CAP_AMP_MOVE_INITIATOR) {
- if ((pi->amp_move_state ==
- L2CAP_AMP_STATE_WAIT_LOGICAL_COMPLETE) ||
- (pi->amp_move_state ==
- L2CAP_AMP_STATE_WAIT_LOGICAL_CONFIRM)) {
- /* Remote has only sent pending or
- * success responses, clean up
- */
- l2cap_amp_move_revert(sk);
- l2cap_pi(sk)->amp_move_role =
- L2CAP_AMP_MOVE_NONE;
- pi->amp_move_state = L2CAP_AMP_STATE_STABLE;
- }
- /* Other amp move states imply that the move
- * has already aborted
- */
- l2cap_send_move_chan_cfm(pi->conn, pi, pi->scid,
- L2CAP_MOVE_CHAN_UNCONFIRMED);
- l2cap_sock_set_timer(sk, L2CAP_MOVE_TIMEOUT);
- }
- ampchan = pi->ampchan;
- ampcon = pi->ampcon;
- pi->ampchan = NULL;
- pi->ampcon = NULL;
- if (ampchan) {
- if (hci_chan_put(ampchan))
- ampcon->l2cap_data = NULL;
- else
- l2cap_deaggregate(ampchan, pi);
- }
- }
- release_sock(sk);
- }
- static void l2cap_logical_link_worker(struct work_struct *work)
- {
- struct l2cap_logical_link_work *log_link_work =
- container_of(work, struct l2cap_logical_link_work, work);
- struct sock *sk = log_link_work->chan->l2cap_sk;
- if (sk) {
- l2cap_logical_link_complete(log_link_work->chan,
- log_link_work->status);
- sock_put(sk);
- }
- hci_chan_put(log_link_work->chan);
- kfree(log_link_work);
- }
- static int l2cap_create_cfm(struct hci_chan *chan, u8 status)
- {
- struct l2cap_logical_link_work *amp_work;
- if (!chan->l2cap_sk) {
- BT_ERR("Expected l2cap_sk to point to connecting socket");
- return -EFAULT;
- }
- amp_work = kzalloc(sizeof(*amp_work), GFP_ATOMIC);
- if (!amp_work) {
- sock_put(chan->l2cap_sk);
- return -ENOMEM;
- }
- INIT_WORK(&_work->work, l2cap_logical_link_worker);
- amp_work->chan = chan;
- amp_work->status = status;
- hci_chan_hold(chan);
- if (!queue_work(_l2cap_wq, &_work->work)) {
- kfree(amp_work);
- sock_put(chan->l2cap_sk);
- hci_chan_put(chan);
- return -ENOMEM;
- }
- return 0;
- }
- int l2cap_modify_cfm(struct hci_chan *chan, u8 status)
- {
- struct l2cap_conn *conn = chan->conn->l2cap_data;
- BT_DBG("chan %p conn %p status %d", chan, conn, status);
- /* TODO: if failed status restore previous fs */
- return 0;
- }
- int l2cap_destroy_cfm(struct hci_chan *chan, u8 reason)
- {
- struct l2cap_chan_list *l;
- struct l2cap_conn *conn = chan->conn->l2cap_data;
- struct sock *sk;
- BT_DBG("chan %p conn %p", chan, conn);
- if (!conn)
- return 0;
- l = &conn->chan_list;
- read_lock(&l->lock);
- for (sk = l->head; sk; sk = l2cap_pi(sk)->next_c) {
- bh_lock_sock(sk);
- /* TODO MM/PK - What to do if connection is LOCAL_BUSY? */
- if (l2cap_pi(sk)->ampchan == chan) {
- struct hci_conn *ampcon = l2cap_pi(sk)->ampcon;
- l2cap_pi(sk)->ampchan = NULL;
- l2cap_pi(sk)->ampcon = NULL;
- if (hci_chan_put(chan))
- ampcon->l2cap_data = NULL;
- else
- l2cap_deaggregate(chan, l2cap_pi(sk));
- l2cap_amp_move_init(sk);
- }
- bh_unlock_sock(sk);
- }
- read_unlock(&l->lock);
- return 0;
- }
- static int l2cap_sig_amp(struct l2cap_conn *conn, struct l2cap_cmd_hdr *cmd,
- u8 *data, struct sk_buff *skb)
- {
- struct l2cap_amp_signal_work *amp_work;
- amp_work = kzalloc(sizeof(*amp_work), GFP_ATOMIC);
- if (!amp_work)
- return -ENOMEM;
- INIT_WORK(&_work->work, l2cap_amp_signal_worker);
- amp_work->conn = conn;
- amp_work->cmd = *cmd;
- amp_work->data = data;
- amp_work->skb = skb_clone(skb, GFP_ATOMIC);
- if (!amp_work->skb) {
- kfree(amp_work);
- return -ENOMEM;
- }
- if (!queue_work(_l2cap_wq, &_work->work)) {
- kfree_skb(amp_work->skb);
- kfree(amp_work);
- return -ENOMEM;
- }
- return 0;
- }
- static inline int l2cap_check_conn_param(u16 min, u16 max, u16 latency,
- u16 to_multiplier)
- {
- u16 max_latency;
- if (min > max || min < 6 || max > 3200)
- return -EINVAL;
- if (to_multiplier < 10 || to_multiplier > 3200)
- return -EINVAL;
- if (max >= to_multiplier * 8)
- return -EINVAL;
- max_latency = (to_multiplier * 8 / max) - 1;
- if (latency > 499 || latency > max_latency)
- return -EINVAL;
- return 0;
- }
- static inline int l2cap_conn_param_update_req(struct l2cap_conn *conn,
- struct l2cap_cmd_hdr *cmd, u8 *data)
- {
- struct hci_conn *hcon = conn->hcon;
- struct l2cap_conn_param_update_req *req;
- struct l2cap_conn_param_update_rsp rsp;
- struct sock *sk;
- u16 min, max, latency, timeout, cmd_len;
- int err;
- if (!(hcon->link_mode & HCI_LM_MASTER))
- return -EINVAL;
- cmd_len = __le16_to_cpu(cmd->len);
- if (cmd_len != sizeof(struct l2cap_conn_param_update_req))
- return -EPROTO;
- memset(&rsp, 0, sizeof(rsp));
- rsp.result = cpu_to_le16(L2CAP_CONN_PARAM_REJECTED);
- sk = l2cap_find_sock_by_fixed_cid_and_dir(4, conn->src, conn->dst, 0);
- if (sk && !bt_sk(sk)->le_params.prohibit_remote_chg) {
- req = (struct l2cap_conn_param_update_req *) data;
- min = __le16_to_cpu(req->min);
- max = __le16_to_cpu(req->max);
- latency = __le16_to_cpu(req->latency);
- timeout = __le16_to_cpu(req->to_multiplier);
- err = l2cap_check_conn_param(min, max, latency, timeout);
- if (!err) {
- rsp.result = cpu_to_le16(L2CAP_CONN_PARAM_ACCEPTED);
- hci_le_conn_update(hcon, min, max, latency, timeout);
- bt_sk(sk)->le_params.interval_min = min;
- bt_sk(sk)->le_params.interval_max = max;
- bt_sk(sk)->le_params.latency = latency;
- bt_sk(sk)->le_params.supervision_timeout = timeout;
- }
- }
- l2cap_send_cmd(conn, cmd->ident, L2CAP_CONN_PARAM_UPDATE_RSP,
- sizeof(rsp), &rsp);
- return 0;
- }
- static inline int l2cap_bredr_sig_cmd(struct l2cap_conn *conn,
- struct l2cap_cmd_hdr *cmd, u16 cmd_len, u8 *data,
- struct sk_buff *skb)
- {
- int err = 0;
- switch (cmd->code) {
- case L2CAP_COMMAND_REJ:
- l2cap_command_rej(conn, cmd, cmd_len, data);
- break;
- case L2CAP_CONN_REQ:
- err = l2cap_connect_req(conn, cmd, cmd_len, data);
- break;
- case L2CAP_CONN_RSP:
- err = l2cap_connect_rsp(conn, cmd, cmd_len, data);
- break;
- case L2CAP_CONF_REQ:
- err = l2cap_config_req(conn, cmd, cmd_len, data);
- break;
- case L2CAP_CONF_RSP:
- err = l2cap_config_rsp(conn, cmd, cmd_len, data);
- break;
- case L2CAP_DISCONN_REQ:
- err = l2cap_disconnect_req(conn, cmd, cmd_len, data);
- break;
- case L2CAP_DISCONN_RSP:
- err = l2cap_disconnect_rsp(conn, cmd, cmd_len, data);
- break;
- case L2CAP_ECHO_REQ:
- l2cap_send_cmd(conn, cmd->ident, L2CAP_ECHO_RSP, cmd_len, data);
- break;
- case L2CAP_ECHO_RSP:
- break;
- case L2CAP_INFO_REQ:
- err = l2cap_information_req(conn, cmd, cmd_len, data);
- break;
- case L2CAP_INFO_RSP:
- err = l2cap_information_rsp(conn, cmd, cmd_len, data);
- break;
- case L2CAP_CREATE_CHAN_REQ:
- err = l2cap_create_channel_req(conn, cmd, cmd_len, data);
- break;
- case L2CAP_CREATE_CHAN_RSP:
- err = l2cap_create_channel_rsp(conn, cmd, cmd_len, data);
- break;
- case L2CAP_MOVE_CHAN_REQ:
- case L2CAP_MOVE_CHAN_RSP:
- case L2CAP_MOVE_CHAN_CFM:
- case L2CAP_MOVE_CHAN_CFM_RSP:
- err = l2cap_sig_amp(conn, cmd, data, skb);
- break;
- default:
- BT_ERR("Unknown BR/EDR signaling command 0x%2.2x", cmd->code);
- err = -EINVAL;
- break;
- }
- return err;
- }
- static inline int l2cap_le_sig_cmd(struct l2cap_conn *conn,
- struct l2cap_cmd_hdr *cmd, u8 *data)
- {
- switch (cmd->code) {
- case L2CAP_COMMAND_REJ:
- return 0;
- case L2CAP_CONN_PARAM_UPDATE_REQ:
- return l2cap_conn_param_update_req(conn, cmd, data);
- case L2CAP_CONN_PARAM_UPDATE_RSP:
- return 0;
- default:
- BT_ERR("Unknown LE signaling command 0x%2.2x", cmd->code);
- return -EINVAL;
- }
- }
- static inline void l2cap_sig_channel(struct l2cap_conn *conn,
- struct sk_buff *skb)
- {
- u8 *data = skb->data;
- int len = skb->len;
- struct l2cap_cmd_hdr cmd;
- int err;
- l2cap_raw_recv(conn, skb);
- while (len >= L2CAP_CMD_HDR_SIZE) {
- u16 cmd_len;
- memcpy(&cmd, data, L2CAP_CMD_HDR_SIZE);
- data += L2CAP_CMD_HDR_SIZE;
- len -= L2CAP_CMD_HDR_SIZE;
- cmd_len = le16_to_cpu(cmd.len);
- BT_DBG("code 0x%2.2x len %d id 0x%2.2x", cmd.code, cmd_len, cmd.ident);
- if (cmd_len > len || !cmd.ident) {
- BT_DBG("corrupted command");
- break;
- }
- if (conn->hcon->type == LE_LINK)
- err = l2cap_le_sig_cmd(conn, &cmd, data);
- else
- err = l2cap_bredr_sig_cmd(conn, &cmd, cmd_len,
- data, skb);
- if (err) {
- struct l2cap_cmd_rej rej;
- BT_ERR("Wrong link type (%d)", err);
- /* FIXME: Map err to a valid reason */
- rej.reason = cpu_to_le16(0);
- l2cap_send_cmd(conn, cmd.ident, L2CAP_COMMAND_REJ, sizeof(rej), &rej);
- }
- data += cmd_len;
- len -= cmd_len;
- }
- kfree_skb(skb);
- }
- static int l2cap_check_fcs(struct l2cap_pinfo *pi, struct sk_buff *skb)
- {
- u16 our_fcs, rcv_fcs;
- int hdr_size;
- if (pi->extended_control)
- hdr_size = L2CAP_EXTENDED_HDR_SIZE;
- else
- hdr_size = L2CAP_ENHANCED_HDR_SIZE;
- if (pi->fcs == L2CAP_FCS_CRC16) {
- skb_trim(skb, skb->len - L2CAP_FCS_SIZE);
- rcv_fcs = get_unaligned_le16(skb->data + skb->len);
- our_fcs = crc16(0, skb->data - hdr_size, skb->len + hdr_size);
- if (our_fcs != rcv_fcs) {
- BT_DBG("Bad FCS");
- return -EBADMSG;
- }
- }
- return 0;
- }
- static void l2cap_ertm_pass_to_tx(struct sock *sk,
- struct bt_l2cap_control *control)
- {
- BT_DBG("sk %p, control %p", sk, control);
- l2cap_ertm_tx(sk, control, 0, L2CAP_ERTM_EVENT_RECV_REQSEQ_AND_FBIT);
- }
- static void l2cap_ertm_pass_to_tx_fbit(struct sock *sk,
- struct bt_l2cap_control *control)
- {
- BT_DBG("sk %p, control %p", sk, control);
- l2cap_ertm_tx(sk, control, 0, L2CAP_ERTM_EVENT_RECV_FBIT);
- }
- static void l2cap_ertm_resend(struct sock *sk)
- {
- struct bt_l2cap_control control;
- struct l2cap_pinfo *pi;
- struct sk_buff *skb;
- struct sk_buff *tx_skb;
- u16 seq;
- BT_DBG("sk %p", sk);
- pi = l2cap_pi(sk);
- if (pi->conn_state & L2CAP_CONN_REMOTE_BUSY)
- return;
- if (pi->amp_move_state != L2CAP_AMP_STATE_STABLE &&
- pi->amp_move_state != L2CAP_AMP_STATE_WAIT_PREPARE)
- return;
- while (pi->retrans_list.head != L2CAP_SEQ_LIST_CLEAR) {
- seq = l2cap_seq_list_pop(&pi->retrans_list);
- skb = l2cap_ertm_seq_in_queue(TX_QUEUE(sk), seq);
- if (!skb) {
- BT_DBG("Error: Can't retransmit seq %d, frame missing",
- (int) seq);
- continue;
- }
- bt_cb(skb)->retries += 1;
- control = bt_cb(skb)->control;
- if ((pi->max_tx != 0) && (bt_cb(skb)->retries > pi->max_tx)) {
- BT_DBG("Retry limit exceeded (%d)", (int) pi->max_tx);
- l2cap_send_disconn_req(pi->conn, sk, ECONNRESET);
- l2cap_seq_list_clear(&pi->retrans_list);
- break;
- }
- control.reqseq = pi->buffer_seq;
- if (pi->conn_state & L2CAP_CONN_SEND_FBIT) {
- control.final = 1;
- pi->conn_state &= ~L2CAP_CONN_SEND_FBIT;
- } else {
- control.final = 0;
- }
- if (skb_cloned(skb)) {
- /* Cloned sk_buffs are read-only, so we need a
- * writeable copy
- */
- tx_skb = skb_copy(skb, GFP_ATOMIC);
- } else {
- tx_skb = skb_clone(skb, GFP_ATOMIC);
- }
- if (!tx_skb) {
- l2cap_seq_list_clear(&pi->retrans_list);
- break;
- }
- /* Update skb contents */
- if (pi->extended_control) {
- put_unaligned_le32(__pack_extended_control(&control),
- tx_skb->data + L2CAP_HDR_SIZE);
- } else {
- put_unaligned_le16(__pack_enhanced_control(&control),
- tx_skb->data + L2CAP_HDR_SIZE);
- }
- if (pi->fcs == L2CAP_FCS_CRC16)
- apply_fcs(tx_skb);
- sock_hold(sk);
- tx_skb->sk = sk;
- tx_skb->destructor = l2cap_skb_destructor;
- atomic_inc(&pi->ertm_queued);
- l2cap_do_send(sk, tx_skb);
- BT_DBG("Resent txseq %d", (int)control.txseq);
- pi->last_acked_seq = pi->buffer_seq;
- }
- }
- static inline void l2cap_ertm_retransmit(struct sock *sk,
- struct bt_l2cap_control *control)
- {
- BT_DBG("sk %p, control %p", sk, control);
- l2cap_seq_list_append(&l2cap_pi(sk)->retrans_list, control->reqseq);
- l2cap_ertm_resend(sk);
- }
- static void l2cap_ertm_retransmit_all(struct sock *sk,
- struct bt_l2cap_control *control)
- {
- struct l2cap_pinfo *pi;
- struct sk_buff *skb;
- BT_DBG("sk %p, control %p", sk, control);
- pi = l2cap_pi(sk);
- if (control->poll)
- pi->conn_state |= L2CAP_CONN_SEND_FBIT;
- l2cap_seq_list_clear(&pi->retrans_list);
- if (pi->conn_state & L2CAP_CONN_REMOTE_BUSY)
- return;
- if (pi->unacked_frames) {
- skb_queue_walk(TX_QUEUE(sk), skb) {
- if ((bt_cb(skb)->control.txseq == control->reqseq) ||
- skb == sk->sk_send_head)
- break;
- }
- skb_queue_walk_from(TX_QUEUE(sk), skb) {
- if (skb == sk->sk_send_head)
- break;
- l2cap_seq_list_append(&pi->retrans_list,
- bt_cb(skb)->control.txseq);
- }
- l2cap_ertm_resend(sk);
- }
- }
- static inline void append_skb_frag(struct sk_buff *skb,
- struct sk_buff *new_frag, struct sk_buff **last_frag)
- {
- /* skb->len reflects data in skb as well as all fragments
- skb->data_len reflects only data in fragments
- */
- BT_DBG("skb %p, new_frag %p, *last_frag %p", skb, new_frag, *last_frag);
- if (!skb_has_frag_list(skb))
- skb_shinfo(skb)->frag_list = new_frag;
- new_frag->next = NULL;
- (*last_frag)->next = new_frag;
- *last_frag = new_frag;
- skb->len += new_frag->len;
- skb->data_len += new_frag->len;
- skb->truesize += new_frag->truesize;
- }
- static int l2cap_ertm_rx_expected_iframe(struct sock *sk,
- struct bt_l2cap_control *control, struct sk_buff *skb)
- {
- struct l2cap_pinfo *pi;
- int err = -EINVAL;
- BT_DBG("sk %p, control %p, skb %p len %d truesize %d", sk, control,
- skb, skb->len, skb->truesize);
- if (!control)
- return err;
- pi = l2cap_pi(sk);
- BT_DBG("type %c, sar %d, txseq %d, reqseq %d, final %d",
- control->frame_type, control->sar, control->txseq,
- control->reqseq, control->final);
- switch (control->sar) {
- case L2CAP_SAR_UNSEGMENTED:
- if (pi->sdu) {
- BT_DBG("Unexpected unsegmented PDU during reassembly");
- kfree_skb(pi->sdu);
- pi->sdu = NULL;
- pi->sdu_last_frag = NULL;
- pi->sdu_len = 0;
- }
- BT_DBG("Unsegmented");
- err = sock_queue_rcv_skb(sk, skb);
- break;
- case L2CAP_SAR_START:
- if (pi->sdu) {
- BT_DBG("Unexpected start PDU during reassembly");
- kfree_skb(pi->sdu);
- }
- pi->sdu_len = get_unaligned_le16(skb->data);
- skb_pull(skb, 2);
- if (pi->sdu_len > pi->imtu) {
- err = -EMSGSIZE;
- break;
- }
- if (skb->len >= pi->sdu_len)
- break;
- pi->sdu = skb;
- pi->sdu_last_frag = skb;
- BT_DBG("Start");
- skb = NULL;
- err = 0;
- break;
- case L2CAP_SAR_CONTINUE:
- if (!pi->sdu)
- break;
- append_skb_frag(pi->sdu, skb,
- &pi->sdu_last_frag);
- skb = NULL;
- if (pi->sdu->len >= pi->sdu_len)
- break;
- BT_DBG("Continue, reassembled %d", pi->sdu->len);
- err = 0;
- break;
- case L2CAP_SAR_END:
- if (!pi->sdu)
- break;
- append_skb_frag(pi->sdu, skb,
- &pi->sdu_last_frag);
- skb = NULL;
- if (pi->sdu->len != pi->sdu_len)
- break;
- BT_DBG("End, reassembled %d", pi->sdu->len);
- /* If the sender used tiny PDUs, the rcv queuing could fail.
- * Applications that have issues here should use a larger
- * sk_rcvbuf.
- */
- err = sock_queue_rcv_skb(sk, pi->sdu);
- if (!err) {
- /* Reassembly complete */
- pi->sdu = NULL;
- pi->sdu_last_frag = NULL;
- pi->sdu_len = 0;
- }
- break;
- default:
- BT_DBG("Bad SAR value");
- break;
- }
- if (err) {
- BT_DBG("Reassembly error %d, sk_rcvbuf %d, sk_rmem_alloc %d",
- err, sk->sk_rcvbuf, atomic_read(&sk->sk_rmem_alloc));
- if (pi->sdu) {
- kfree_skb(pi->sdu);
- pi->sdu = NULL;
- }
- pi->sdu_last_frag = NULL;
- pi->sdu_len = 0;
- if (skb)
- kfree_skb(skb);
- }
- /* Update local busy state */
- if (!(pi->conn_state & L2CAP_CONN_LOCAL_BUSY) && l2cap_rmem_full(sk))
- l2cap_ertm_tx(sk, 0, 0, L2CAP_ERTM_EVENT_LOCAL_BUSY_DETECTED);
- return err;
- }
- static int l2cap_ertm_rx_queued_iframes(struct sock *sk)
- {
- int err = 0;
- /* Pass sequential frames to l2cap_ertm_rx_expected_iframe()
- * until a gap is encountered.
- */
- struct l2cap_pinfo *pi;
- BT_DBG("sk %p", sk);
- pi = l2cap_pi(sk);
- while (l2cap_rmem_available(sk)) {
- struct sk_buff *skb;
- BT_DBG("Searching for skb with txseq %d (queue len %d)",
- (int) pi->buffer_seq, skb_queue_len(SREJ_QUEUE(sk)));
- skb = l2cap_ertm_seq_in_queue(SREJ_QUEUE(sk), pi->buffer_seq);
- if (!skb)
- break;
- skb_unlink(skb, SREJ_QUEUE(sk));
- pi->buffer_seq = __next_seq(pi->buffer_seq, pi);
- err = l2cap_ertm_rx_expected_iframe(sk,
- &bt_cb(skb)->control, skb);
- if (err)
- break;
- }
- if (skb_queue_empty(SREJ_QUEUE(sk))) {
- pi->rx_state = L2CAP_ERTM_RX_STATE_RECV;
- l2cap_ertm_send_ack(sk);
- }
- return err;
- }
- static void l2cap_ertm_handle_srej(struct sock *sk,
- struct bt_l2cap_control *control)
- {
- struct l2cap_pinfo *pi;
- struct sk_buff *skb;
- BT_DBG("sk %p, control %p", sk, control);
- pi = l2cap_pi(sk);
- if (control->reqseq == pi->next_tx_seq) {
- BT_DBG("Invalid reqseq %d, disconnecting",
- (int) control->reqseq);
- l2cap_send_disconn_req(pi->conn, sk, ECONNRESET);
- return;
- }
- skb = l2cap_ertm_seq_in_queue(TX_QUEUE(sk), control->reqseq);
- if (skb == NULL) {
- BT_DBG("Seq %d not available for retransmission",
- (int) control->reqseq);
- return;
- }
- if ((pi->max_tx != 0) && (bt_cb(skb)->retries >= pi->max_tx)) {
- BT_DBG("Retry limit exceeded (%d)", (int) pi->max_tx);
- l2cap_send_disconn_req(pi->conn, sk, ECONNRESET);
- return;
- }
- pi->conn_state &= ~L2CAP_CONN_REMOTE_BUSY;
- if (control->poll) {
- l2cap_ertm_pass_to_tx(sk, control);
- pi->conn_state |= L2CAP_CONN_SEND_FBIT;
- l2cap_ertm_retransmit(sk, control);
- l2cap_ertm_send(sk);
- if (pi->tx_state == L2CAP_ERTM_TX_STATE_WAIT_F) {
- pi->conn_state |= L2CAP_CONN_SREJ_ACT;
- pi->srej_save_reqseq = control->reqseq;
- }
- } else {
- l2cap_ertm_pass_to_tx_fbit(sk, control);
- if (control->final) {
- if ((pi->conn_state & L2CAP_CONN_SREJ_ACT) &&
- (pi->srej_save_reqseq == control->reqseq)) {
- pi->conn_state &= ~L2CAP_CONN_SREJ_ACT;
- } else {
- l2cap_ertm_retransmit(sk, control);
- }
- } else {
- l2cap_ertm_retransmit(sk, control);
- if (pi->tx_state == L2CAP_ERTM_TX_STATE_WAIT_F) {
- pi->conn_state |= L2CAP_CONN_SREJ_ACT;
- pi->srej_save_reqseq = control->reqseq;
- }
- }
- }
- }
- static void l2cap_ertm_handle_rej(struct sock *sk,
- struct bt_l2cap_control *control)
- {
- struct l2cap_pinfo *pi;
- struct sk_buff *skb;
- BT_DBG("sk %p, control %p", sk, control);
- pi = l2cap_pi(sk);
- if (control->reqseq == pi->next_tx_seq) {
- BT_DBG("Invalid reqseq %d, disconnecting",
- (int) control->reqseq);
- l2cap_send_disconn_req(pi->conn, sk, ECONNRESET);
- return;
- }
- skb = l2cap_ertm_seq_in_queue(TX_QUEUE(sk), control->reqseq);
- if (pi->max_tx && skb && bt_cb(skb)->retries >= pi->max_tx) {
- BT_DBG("Retry limit exceeded (%d)", (int) pi->max_tx);
- l2cap_send_disconn_req(pi->conn, sk, ECONNRESET);
- return;
- }
- pi->conn_state &= ~L2CAP_CONN_REMOTE_BUSY;
- l2cap_ertm_pass_to_tx(sk, control);
- if (control->final) {
- if (pi->conn_state & L2CAP_CONN_REJ_ACT)
- pi->conn_state &= ~L2CAP_CONN_REJ_ACT;
- else
- l2cap_ertm_retransmit_all(sk, control);
- } else {
- l2cap_ertm_retransmit_all(sk, control);
- l2cap_ertm_send(sk);
- if (pi->tx_state == L2CAP_ERTM_TX_STATE_WAIT_F)
- pi->conn_state |= L2CAP_CONN_REJ_ACT;
- }
- }
- static u8 l2cap_ertm_classify_txseq(struct sock *sk, u16 txseq)
- {
- struct l2cap_pinfo *pi;
- BT_DBG("sk %p, txseq %d", sk, (int)txseq);
- pi = l2cap_pi(sk);
- BT_DBG("last_acked_seq %d, expected_tx_seq %d", (int)pi->last_acked_seq,
- (int)pi->expected_tx_seq);
- if (pi->rx_state == L2CAP_ERTM_RX_STATE_SREJ_SENT) {
- if (__delta_seq(txseq, pi->last_acked_seq, pi) >= pi->tx_win) {
- /* See notes below regarding "double poll" and
- * invalid packets.
- */
- if (pi->tx_win <= ((pi->tx_win_max + 1) >> 1)) {
- BT_DBG("Invalid/Ignore - txseq outside "
- "tx window after SREJ sent");
- return L2CAP_ERTM_TXSEQ_INVALID_IGNORE;
- } else {
- BT_DBG("Invalid - bad txseq within tx "
- "window after SREJ sent");
- return L2CAP_ERTM_TXSEQ_INVALID;
- }
- }
- if (pi->srej_list.head == txseq) {
- BT_DBG("Expected SREJ");
- return L2CAP_ERTM_TXSEQ_EXPECTED_SREJ;
- }
- if (l2cap_ertm_seq_in_queue(SREJ_QUEUE(sk), txseq)) {
- BT_DBG("Duplicate SREJ - txseq already stored");
- return L2CAP_ERTM_TXSEQ_DUPLICATE_SREJ;
- }
- if (l2cap_seq_list_contains(&pi->srej_list, txseq)) {
- BT_DBG("Unexpected SREJ - txseq not requested "
- "with SREJ");
- return L2CAP_ERTM_TXSEQ_UNEXPECTED_SREJ;
- }
- }
- if (pi->expected_tx_seq == txseq) {
- if (__delta_seq(txseq, pi->last_acked_seq, pi) >= pi->tx_win) {
- BT_DBG("Invalid - txseq outside tx window");
- return L2CAP_ERTM_TXSEQ_INVALID;
- } else {
- BT_DBG("Expected");
- return L2CAP_ERTM_TXSEQ_EXPECTED;
- }
- }
- if (__delta_seq(txseq, pi->last_acked_seq, pi) <
- __delta_seq(pi->expected_tx_seq, pi->last_acked_seq, pi)) {
- BT_DBG("Duplicate - expected_tx_seq later than txseq");
- return L2CAP_ERTM_TXSEQ_DUPLICATE;
- }
- if (__delta_seq(txseq, pi->last_acked_seq, pi) >= pi->tx_win) {
- /* A source of invalid packets is a "double poll" condition,
- * where delays cause us to send multiple poll packets. If
- * the remote stack receives and processes both polls,
- * sequence numbers can wrap around in such a way that a
- * resent frame has a sequence number that looks like new data
- * with a sequence gap. This would trigger an erroneous SREJ
- * request.
- *
- * Fortunately, this is impossible with a tx window that's
- * less than half of the maximum sequence number, which allows
- * invalid frames to be safely ignored.
- *
- * With tx window sizes greater than half of the tx window
- * maximum, the frame is invalid and cannot be ignored. This
- * causes a disconnect.
- */
- if (pi->tx_win <= ((pi->tx_win_max + 1) >> 1)) {
- BT_DBG("Invalid/Ignore - txseq outside tx window");
- return L2CAP_ERTM_TXSEQ_INVALID_IGNORE;
- } else {
- BT_DBG("Invalid - txseq outside tx window");
- return L2CAP_ERTM_TXSEQ_INVALID;
- }
- } else {
- BT_DBG("Unexpected - txseq indicates missing frames");
- return L2CAP_ERTM_TXSEQ_UNEXPECTED;
- }
- }
- static int l2cap_ertm_rx_state_recv(struct sock *sk,
- struct bt_l2cap_control *control,
- struct sk_buff *skb, u8 event)
- {
- struct l2cap_pinfo *pi;
- int err = 0;
- bool skb_in_use = 0;
- BT_DBG("sk %p, control %p, skb %p, event %d", sk, control, skb,
- (int)event);
- pi = l2cap_pi(sk);
- switch (event) {
- case L2CAP_ERTM_EVENT_RECV_IFRAME:
- switch (l2cap_ertm_classify_txseq(sk, control->txseq)) {
- case L2CAP_ERTM_TXSEQ_EXPECTED:
- l2cap_ertm_pass_to_tx(sk, control);
- if (pi->conn_state & L2CAP_CONN_LOCAL_BUSY) {
- BT_DBG("Busy, discarding expected seq %d",
- control->txseq);
- break;
- }
- pi->expected_tx_seq = __next_seq(control->txseq, pi);
- pi->buffer_seq = pi->expected_tx_seq;
- skb_in_use = 1;
- err = l2cap_ertm_rx_expected_iframe(sk, control, skb);
- if (err)
- break;
- if (control->final) {
- if (pi->conn_state & L2CAP_CONN_REJ_ACT)
- pi->conn_state &= ~L2CAP_CONN_REJ_ACT;
- else {
- control->final = 0;
- l2cap_ertm_retransmit_all(sk, control);
- l2cap_ertm_send(sk);
- }
- }
- if (!(pi->conn_state & L2CAP_CONN_LOCAL_BUSY))
- l2cap_ertm_send_ack(sk);
- break;
- case L2CAP_ERTM_TXSEQ_UNEXPECTED:
- l2cap_ertm_pass_to_tx(sk, control);
- /* Can't issue SREJ frames in the local busy state.
- * Drop this frame, it will be seen as missing
- * when local busy is exited.
- */
- if (pi->conn_state & L2CAP_CONN_LOCAL_BUSY) {
- BT_DBG("Busy, discarding unexpected seq %d",
- control->txseq);
- break;
- }
- /* There was a gap in the sequence, so an SREJ
- * must be sent for each missing frame. The
- * current frame is stored for later use.
- */
- skb_queue_tail(SREJ_QUEUE(sk), skb);
- skb_in_use = 1;
- BT_DBG("Queued %p (queue len %d)", skb,
- skb_queue_len(SREJ_QUEUE(sk)));
- pi->conn_state &= ~L2CAP_CONN_SREJ_ACT;
- l2cap_seq_list_clear(&pi->srej_list);
- l2cap_ertm_send_srej(sk, control->txseq);
- pi->rx_state = L2CAP_ERTM_RX_STATE_SREJ_SENT;
- break;
- case L2CAP_ERTM_TXSEQ_DUPLICATE:
- l2cap_ertm_pass_to_tx(sk, control);
- break;
- case L2CAP_ERTM_TXSEQ_INVALID_IGNORE:
- break;
- case L2CAP_ERTM_TXSEQ_INVALID:
- default:
- l2cap_send_disconn_req(l2cap_pi(sk)->conn, sk,
- ECONNRESET);
- break;
- }
- break;
- case L2CAP_ERTM_EVENT_RECV_RR:
- l2cap_ertm_pass_to_tx(sk, control);
- if (control->final) {
- pi->conn_state &= ~L2CAP_CONN_REMOTE_BUSY;
- if (pi->conn_state & L2CAP_CONN_REJ_ACT)
- pi->conn_state &= ~L2CAP_CONN_REJ_ACT;
- else if (pi->amp_move_state == L2CAP_AMP_STATE_STABLE ||
- pi->amp_move_state ==
- L2CAP_AMP_STATE_WAIT_PREPARE) {
- control->final = 0;
- l2cap_ertm_retransmit_all(sk, control);
- }
- l2cap_ertm_send(sk);
- } else if (control->poll) {
- l2cap_ertm_send_i_or_rr_or_rnr(sk);
- } else {
- if ((pi->conn_state & L2CAP_CONN_REMOTE_BUSY) &&
- pi->unacked_frames)
- l2cap_ertm_start_retrans_timer(pi);
- pi->conn_state &= ~L2CAP_CONN_REMOTE_BUSY;
- l2cap_ertm_send(sk);
- }
- break;
- case L2CAP_ERTM_EVENT_RECV_RNR:
- pi->conn_state |= L2CAP_CONN_REMOTE_BUSY;
- l2cap_ertm_pass_to_tx(sk, control);
- if (control && control->poll) {
- pi->conn_state |= L2CAP_CONN_SEND_FBIT;
- l2cap_ertm_send_rr_or_rnr(sk, 0);
- }
- l2cap_ertm_stop_retrans_timer(pi);
- l2cap_seq_list_clear(&pi->retrans_list);
- break;
- case L2CAP_ERTM_EVENT_RECV_REJ:
- l2cap_ertm_handle_rej(sk, control);
- break;
- case L2CAP_ERTM_EVENT_RECV_SREJ:
- l2cap_ertm_handle_srej(sk, control);
- break;
- default:
- break;
- }
- if (skb && !skb_in_use) {
- BT_DBG("Freeing %p", skb);
- kfree_skb(skb);
- }
- return err;
- }
- static int l2cap_ertm_rx_state_srej_sent(struct sock *sk,
- struct bt_l2cap_control *control,
- struct sk_buff *skb, u8 event)
- {
- struct l2cap_pinfo *pi;
- int err = 0;
- u16 txseq = control->txseq;
- bool skb_in_use = 0;
- BT_DBG("sk %p, control %p, skb %p, event %d", sk, control, skb,
- (int)event);
- pi = l2cap_pi(sk);
- switch (event) {
- case L2CAP_ERTM_EVENT_RECV_IFRAME:
- switch (l2cap_ertm_classify_txseq(sk, txseq)) {
- case L2CAP_ERTM_TXSEQ_EXPECTED:
- /* Keep frame for reassembly later */
- l2cap_ertm_pass_to_tx(sk, control);
- skb_queue_tail(SREJ_QUEUE(sk), skb);
- skb_in_use = 1;
- BT_DBG("Queued %p (queue len %d)", skb,
- skb_queue_len(SREJ_QUEUE(sk)));
- pi->expected_tx_seq = __next_seq(txseq, pi);
- break;
- case L2CAP_ERTM_TXSEQ_EXPECTED_SREJ:
- l2cap_seq_list_pop(&pi->srej_list);
- l2cap_ertm_pass_to_tx(sk, control);
- skb_queue_tail(SREJ_QUEUE(sk), skb);
- skb_in_use = 1;
- BT_DBG("Queued %p (queue len %d)", skb,
- skb_queue_len(SREJ_QUEUE(sk)));
- err = l2cap_ertm_rx_queued_iframes(sk);
- if (err)
- break;
- break;
- case L2CAP_ERTM_TXSEQ_UNEXPECTED:
- /* Got a frame that can't be reassembled yet.
- * Save it for later, and send SREJs to cover
- * the missing frames.
- */
- skb_queue_tail(SREJ_QUEUE(sk), skb);
- skb_in_use = 1;
- BT_DBG("Queued %p (queue len %d)", skb,
- skb_queue_len(SREJ_QUEUE(sk)));
- l2cap_ertm_pass_to_tx(sk, control);
- l2cap_ertm_send_srej(sk, control->txseq);
- break;
- case L2CAP_ERTM_TXSEQ_UNEXPECTED_SREJ:
- /* This frame was requested with an SREJ, but
- * some expected retransmitted frames are
- * missing. Request retransmission of missing
- * SREJ'd frames.
- */
- skb_queue_tail(SREJ_QUEUE(sk), skb);
- skb_in_use = 1;
- BT_DBG("Queued %p (queue len %d)", skb,
- skb_queue_len(SREJ_QUEUE(sk)));
- l2cap_ertm_pass_to_tx(sk, control);
- l2cap_ertm_send_srej_list(sk, control->txseq);
- break;
- case L2CAP_ERTM_TXSEQ_DUPLICATE_SREJ:
- /* We've already queued this frame. Drop this copy. */
- l2cap_ertm_pass_to_tx(sk, control);
- break;
- case L2CAP_ERTM_TXSEQ_DUPLICATE:
- /* Expecting a later sequence number, so this frame
- * was already received. Ignore it completely.
- */
- break;
- case L2CAP_ERTM_TXSEQ_INVALID_IGNORE:
- break;
- case L2CAP_ERTM_TXSEQ_INVALID:
- default:
- l2cap_send_disconn_req(l2cap_pi(sk)->conn, sk,
- ECONNRESET);
- break;
- }
- break;
- case L2CAP_ERTM_EVENT_RECV_RR:
- l2cap_ertm_pass_to_tx(sk, control);
- if (control->final) {
- pi->conn_state &= ~L2CAP_CONN_REMOTE_BUSY;
- if (pi->conn_state & L2CAP_CONN_REJ_ACT)
- pi->conn_state &= ~L2CAP_CONN_REJ_ACT;
- else {
- control->final = 0;
- l2cap_ertm_retransmit_all(sk, control);
- }
- l2cap_ertm_send(sk);
- } else if (control->poll) {
- if ((pi->conn_state & L2CAP_CONN_REMOTE_BUSY) &&
- pi->unacked_frames) {
- l2cap_ertm_start_retrans_timer(pi);
- }
- pi->conn_state &= ~L2CAP_CONN_REMOTE_BUSY;
- pi->conn_state |= L2CAP_CONN_SEND_FBIT;
- l2cap_ertm_send_srej_tail(sk);
- } else {
- if ((pi->conn_state & L2CAP_CONN_REMOTE_BUSY) &&
- pi->unacked_frames) {
- l2cap_ertm_start_retrans_timer(pi);
- }
- pi->conn_state &= ~L2CAP_CONN_REMOTE_BUSY;
- l2cap_ertm_send_ack(sk);
- }
- break;
- case L2CAP_ERTM_EVENT_RECV_RNR:
- pi->conn_state |= L2CAP_CONN_REMOTE_BUSY;
- l2cap_ertm_pass_to_tx(sk, control);
- if (control->poll)
- l2cap_ertm_send_srej_tail(sk);
- else {
- struct bt_l2cap_control rr_control;
- memset(&rr_control, 0, sizeof(rr_control));
- rr_control.frame_type = 's';
- rr_control.super = L2CAP_SFRAME_RR;
- rr_control.reqseq = pi->buffer_seq;
- l2cap_ertm_send_sframe(sk, &rr_control);
- }
- break;
- case L2CAP_ERTM_EVENT_RECV_REJ:
- l2cap_ertm_handle_rej(sk, control);
- break;
- case L2CAP_ERTM_EVENT_RECV_SREJ:
- l2cap_ertm_handle_srej(sk, control);
- break;
- }
- if (skb && !skb_in_use) {
- BT_DBG("Freeing %p", skb);
- kfree_skb(skb);
- }
- return err;
- }
- static int l2cap_ertm_rx_state_amp_move(struct sock *sk,
- struct bt_l2cap_control *control,
- struct sk_buff *skb, u8 event)
- {
- struct l2cap_pinfo *pi;
- int err = 0;
- bool skb_in_use = 0;
- BT_DBG("sk %p, control %p, skb %p, event %d", sk, control, skb,
- (int)event);
- pi = l2cap_pi(sk);
- /* Only handle expected frames, to avoid state changes. */
- switch (event) {
- case L2CAP_ERTM_EVENT_RECV_IFRAME:
- if (l2cap_ertm_classify_txseq(sk, control->txseq) ==
- L2CAP_ERTM_TXSEQ_EXPECTED) {
- l2cap_ertm_pass_to_tx(sk, control);
- if (pi->conn_state & L2CAP_CONN_LOCAL_BUSY) {
- BT_DBG("Busy, discarding expected seq %d",
- control->txseq);
- break;
- }
- pi->expected_tx_seq = __next_seq(control->txseq, pi);
- pi->buffer_seq = pi->expected_tx_seq;
- skb_in_use = 1;
- err = l2cap_ertm_rx_expected_iframe(sk, control, skb);
- if (err)
- break;
- if (control->final) {
- if (pi->conn_state & L2CAP_CONN_REJ_ACT)
- pi->conn_state &= ~L2CAP_CONN_REJ_ACT;
- else
- control->final = 0;
- }
- }
- break;
- case L2CAP_ERTM_EVENT_RECV_RR:
- case L2CAP_ERTM_EVENT_RECV_RNR:
- case L2CAP_ERTM_EVENT_RECV_REJ:
- l2cap_ertm_process_reqseq(sk, control->reqseq);
- break;
- case L2CAP_ERTM_EVENT_RECV_SREJ:
- /* Ignore */
- break;
- default:
- break;
- }
- if (skb && !skb_in_use) {
- BT_DBG("Freeing %p", skb);
- kfree_skb(skb);
- }
- return err;
- }
- static int l2cap_answer_move_poll(struct sock *sk)
- {
- struct l2cap_pinfo *pi;
- struct bt_l2cap_control control;
- int err = 0;
- BT_DBG("sk %p", sk);
- pi = l2cap_pi(sk);
- l2cap_ertm_process_reqseq(sk, pi->amp_move_reqseq);
- if (!skb_queue_empty(TX_QUEUE(sk)))
- sk->sk_send_head = skb_peek(TX_QUEUE(sk));
- else
- sk->sk_send_head = NULL;
- /* Rewind next_tx_seq to the point expected
- * by the receiver.
- */
- pi->next_tx_seq = pi->amp_move_reqseq;
- pi->unacked_frames = 0;
- err = l2cap_finish_amp_move(sk);
- if (err)
- return err;
- pi->conn_state |= L2CAP_CONN_SEND_FBIT;
- l2cap_ertm_send_i_or_rr_or_rnr(sk);
- memset(&control, 0, sizeof(control));
- control.reqseq = pi->amp_move_reqseq;
- if (pi->amp_move_event == L2CAP_ERTM_EVENT_RECV_IFRAME)
- err = -EPROTO;
- else
- err = l2cap_ertm_rx_state_recv(sk, &control, NULL,
- pi->amp_move_event);
- return err;
- }
- static void l2cap_amp_move_setup(struct sock *sk)
- {
- struct l2cap_pinfo *pi;
- struct sk_buff *skb;
- BT_DBG("sk %p", sk);
- pi = l2cap_pi(sk);
- l2cap_ertm_stop_ack_timer(pi);
- l2cap_ertm_stop_retrans_timer(pi);
- l2cap_ertm_stop_monitor_timer(pi);
- pi->retry_count = 0;
- skb_queue_walk(TX_QUEUE(sk), skb) {
- if (bt_cb(skb)->retries)
- bt_cb(skb)->retries = 1;
- else
- break;
- }
- pi->expected_tx_seq = pi->buffer_seq;
- pi->conn_state &= ~(L2CAP_CONN_REJ_ACT | L2CAP_CONN_SREJ_ACT);
- l2cap_seq_list_clear(&pi->retrans_list);
- l2cap_seq_list_clear(&l2cap_pi(sk)->srej_list);
- skb_queue_purge(SREJ_QUEUE(sk));
- pi->tx_state = L2CAP_ERTM_TX_STATE_XMIT;
- pi->rx_state = L2CAP_ERTM_RX_STATE_AMP_MOVE;
- BT_DBG("tx_state 0x2.2%x rx_state 0x2.2%x", pi->tx_state,
- pi->rx_state);
- pi->conn_state |= L2CAP_CONN_REMOTE_BUSY;
- }
- static void l2cap_amp_move_revert(struct sock *sk)
- {
- struct l2cap_pinfo *pi;
- BT_DBG("sk %p", sk);
- pi = l2cap_pi(sk);
- if (pi->amp_move_role == L2CAP_AMP_MOVE_INITIATOR) {
- l2cap_ertm_tx(sk, NULL, NULL, L2CAP_ERTM_EVENT_EXPLICIT_POLL);
- pi->rx_state = L2CAP_ERTM_RX_STATE_WAIT_F_FLAG;
- } else if (pi->amp_move_role == L2CAP_AMP_MOVE_RESPONDER)
- pi->rx_state = L2CAP_ERTM_RX_STATE_WAIT_P_FLAG;
- }
- static int l2cap_amp_move_reconf(struct sock *sk)
- {
- struct l2cap_pinfo *pi;
- u8 buf[64];
- int err = 0;
- BT_DBG("sk %p", sk);
- pi = l2cap_pi(sk);
- l2cap_send_cmd(pi->conn, l2cap_get_ident(pi->conn), L2CAP_CONF_REQ,
- l2cap_build_amp_reconf_req(sk, buf, sizeof(buf)), buf);
- return err;
- }
- static void l2cap_amp_move_success(struct sock *sk)
- {
- struct l2cap_pinfo *pi;
- BT_DBG("sk %p", sk);
- pi = l2cap_pi(sk);
- if (pi->amp_move_role == L2CAP_AMP_MOVE_INITIATOR) {
- int err = 0;
- /* Send reconfigure request */
- if (pi->mode == L2CAP_MODE_ERTM) {
- pi->reconf_state = L2CAP_RECONF_INT;
- if (enable_reconfig)
- err = l2cap_amp_move_reconf(sk);
- if (err || !enable_reconfig) {
- pi->reconf_state = L2CAP_RECONF_NONE;
- l2cap_ertm_tx(sk, NULL, NULL,
- L2CAP_ERTM_EVENT_EXPLICIT_POLL);
- pi->rx_state = L2CAP_ERTM_RX_STATE_WAIT_F_FLAG;
- }
- } else
- pi->rx_state = L2CAP_ERTM_RX_STATE_RECV;
- } else if (pi->amp_move_role == L2CAP_AMP_MOVE_RESPONDER) {
- if (pi->mode == L2CAP_MODE_ERTM)
- pi->rx_state =
- L2CAP_ERTM_RX_STATE_WAIT_P_FLAG_RECONFIGURE;
- else
- pi->rx_state = L2CAP_ERTM_RX_STATE_RECV;
- }
- }
- static inline bool __valid_reqseq(struct l2cap_pinfo *pi, u16 reqseq)
- {
- /* Make sure reqseq is for a packet that has been sent but not acked */
- u16 unacked = __delta_seq(pi->next_tx_seq, pi->expected_ack_seq, pi);
- return __delta_seq(pi->next_tx_seq, reqseq, pi) <= unacked;
- }
- static int l2cap_strm_rx(struct sock *sk, struct bt_l2cap_control *control,
- struct sk_buff *skb)
- {
- struct l2cap_pinfo *pi;
- int err = 0;
- BT_DBG("sk %p, control %p, skb %p, state %d",
- sk, control, skb, l2cap_pi(sk)->rx_state);
- pi = l2cap_pi(sk);
- if (l2cap_ertm_classify_txseq(sk, control->txseq) ==
- L2CAP_ERTM_TXSEQ_EXPECTED) {
- l2cap_ertm_pass_to_tx(sk, control);
- BT_DBG("buffer_seq %d->%d", pi->buffer_seq,
- __next_seq(pi->buffer_seq, pi));
- pi->buffer_seq = __next_seq(pi->buffer_seq, pi);
- l2cap_ertm_rx_expected_iframe(sk, control, skb);
- } else {
- if (pi->sdu) {
- kfree_skb(pi->sdu);
- pi->sdu = NULL;
- }
- pi->sdu_last_frag = NULL;
- pi->sdu_len = 0;
- if (skb) {
- BT_DBG("Freeing %p", skb);
- kfree_skb(skb);
- }
- }
- pi->last_acked_seq = control->txseq;
- pi->expected_tx_seq = __next_seq(control->txseq, pi);
- return err;
- }
- static int l2cap_ertm_rx(struct sock *sk, struct bt_l2cap_control *control,
- struct sk_buff *skb, u8 event)
- {
- struct l2cap_pinfo *pi;
- int err = 0;
- BT_DBG("sk %p, control %p, skb %p, event %d, state %d",
- sk, control, skb, (int)event, l2cap_pi(sk)->rx_state);
- pi = l2cap_pi(sk);
- if (__valid_reqseq(pi, control->reqseq)) {
- switch (pi->rx_state) {
- case L2CAP_ERTM_RX_STATE_RECV:
- err = l2cap_ertm_rx_state_recv(sk, control, skb, event);
- break;
- case L2CAP_ERTM_RX_STATE_SREJ_SENT:
- err = l2cap_ertm_rx_state_srej_sent(sk, control, skb,
- event);
- break;
- case L2CAP_ERTM_RX_STATE_AMP_MOVE:
- err = l2cap_ertm_rx_state_amp_move(sk, control, skb,
- event);
- break;
- case L2CAP_ERTM_RX_STATE_WAIT_F_FLAG:
- if (control->final) {
- pi->conn_state &= ~L2CAP_CONN_REMOTE_BUSY;
- pi->amp_move_role = L2CAP_AMP_MOVE_NONE;
- pi->rx_state = L2CAP_ERTM_RX_STATE_RECV;
- l2cap_ertm_process_reqseq(sk, control->reqseq);
- if (!skb_queue_empty(TX_QUEUE(sk)))
- sk->sk_send_head =
- skb_peek(TX_QUEUE(sk));
- else
- sk->sk_send_head = NULL;
- /* Rewind next_tx_seq to the point expected
- * by the receiver.
- */
- pi->next_tx_seq = control->reqseq;
- pi->unacked_frames = 0;
- if (pi->ampcon)
- pi->conn->mtu =
- pi->ampcon->hdev->acl_mtu;
- else
- pi->conn->mtu =
- pi->conn->hcon->hdev->acl_mtu;
- err = l2cap_setup_resegment(sk);
- if (err)
- break;
- err = l2cap_ertm_rx_state_recv(sk, control, skb,
- event);
- }
- break;
- case L2CAP_ERTM_RX_STATE_WAIT_P_FLAG:
- if (control->poll) {
- pi->amp_move_reqseq = control->reqseq;
- pi->amp_move_event = event;
- err = l2cap_answer_move_poll(sk);
- }
- break;
- case L2CAP_ERTM_RX_STATE_WAIT_P_FLAG_RECONFIGURE:
- if (control->poll) {
- pi->amp_move_reqseq = control->reqseq;
- pi->amp_move_event = event;
- BT_DBG("amp_move_role 0x%2.2x, "
- "reconf_state 0x%2.2x",
- pi->amp_move_role, pi->reconf_state);
- if (pi->reconf_state == L2CAP_RECONF_ACC)
- err = l2cap_amp_move_reconf(sk);
- else
- err = l2cap_answer_move_poll(sk);
- }
- break;
- default:
- /* shut it down */
- break;
- }
- } else {
- BT_DBG("Invalid reqseq %d (next_tx_seq %d, expected_ack_seq %d",
- control->reqseq, pi->next_tx_seq, pi->expected_ack_seq);
- l2cap_send_disconn_req(pi->conn, sk, ECONNRESET);
- }
- return err;
- }
- void l2cap_fixed_channel_config(struct sock *sk, struct l2cap_options *opt)
- {
- lock_sock(sk);
- l2cap_pi(sk)->fixed_channel = 1;
- l2cap_pi(sk)->imtu = opt->imtu;
- l2cap_pi(sk)->omtu = opt->omtu;
- l2cap_pi(sk)->remote_mps = opt->omtu;
- l2cap_pi(sk)->mps = opt->omtu;
- l2cap_pi(sk)->flush_to = opt->flush_to;
- l2cap_pi(sk)->mode = opt->mode;
- l2cap_pi(sk)->fcs = opt->fcs;
- l2cap_pi(sk)->max_tx = opt->max_tx;
- l2cap_pi(sk)->remote_max_tx = opt->max_tx;
- l2cap_pi(sk)->tx_win = opt->txwin_size;
- l2cap_pi(sk)->remote_tx_win = opt->txwin_size;
- l2cap_pi(sk)->retrans_timeout = L2CAP_DEFAULT_RETRANS_TO;
- l2cap_pi(sk)->monitor_timeout = L2CAP_DEFAULT_MONITOR_TO;
- if (opt->mode == L2CAP_MODE_ERTM ||
- l2cap_pi(sk)->mode == L2CAP_MODE_STREAMING)
- l2cap_ertm_init(sk);
- release_sock(sk);
- return;
- }
- static const u8 l2cap_ertm_rx_func_to_event[4] = {
- L2CAP_ERTM_EVENT_RECV_RR, L2CAP_ERTM_EVENT_RECV_REJ,
- L2CAP_ERTM_EVENT_RECV_RNR, L2CAP_ERTM_EVENT_RECV_SREJ
- };
- int l2cap_data_channel(struct sock *sk, struct sk_buff *skb)
- {
- struct l2cap_pinfo *pi;
- struct bt_l2cap_control *control;
- u16 len;
- u8 event;
- pi = l2cap_pi(sk);
- BT_DBG("sk %p, len %d, mode %d", sk, skb->len, pi->mode);
- if (sk->sk_state != BT_CONNECTED)
- goto drop;
- switch (pi->mode) {
- case L2CAP_MODE_BASIC:
- /* If socket recv buffers overflows we drop data here
- * which is *bad* because L2CAP has to be reliable.
- * But we don't have any other choice. L2CAP doesn't
- * provide flow control mechanism. */
- if (pi->imtu < skb->len)
- goto drop;
- if (!sock_queue_rcv_skb(sk, skb))
- goto done;
- break;
- case L2CAP_MODE_ERTM:
- case L2CAP_MODE_STREAMING:
- control = &bt_cb(skb)->control;
- if (pi->extended_control) {
- __get_extended_control(get_unaligned_le32(skb->data),
- control);
- skb_pull(skb, 4);
- } else {
- __get_enhanced_control(get_unaligned_le16(skb->data),
- control);
- skb_pull(skb, 2);
- }
- len = skb->len;
- if (l2cap_check_fcs(pi, skb))
- goto drop;
- if ((control->frame_type == 'i') &&
- (control->sar == L2CAP_SAR_START))
- len -= 2;
- if (pi->fcs == L2CAP_FCS_CRC16)
- len -= 2;
- /*
- * We can just drop the corrupted I-frame here.
- * Receiver will miss it and start proper recovery
- * procedures and ask for retransmission.
- */
- if (len > pi->mps) {
- l2cap_send_disconn_req(pi->conn, sk, ECONNRESET);
- goto drop;
- }
- if (control->frame_type == 'i') {
- int err;
- BT_DBG("iframe sar %d, reqseq %d, final %d, txseq %d",
- control->sar, control->reqseq, control->final,
- control->txseq);
- /* Validate F-bit - F=0 always valid, F=1 only
- * valid in TX WAIT_F
- */
- if (control->final && (pi->tx_state !=
- L2CAP_ERTM_TX_STATE_WAIT_F))
- goto drop;
- if (pi->mode != L2CAP_MODE_STREAMING) {
- event = L2CAP_ERTM_EVENT_RECV_IFRAME;
- err = l2cap_ertm_rx(sk, control, skb, event);
- } else
- err = l2cap_strm_rx(sk, control, skb);
- if (err)
- l2cap_send_disconn_req(pi->conn, sk,
- ECONNRESET);
- } else {
- /* Only I-frames are expected in streaming mode */
- if (pi->mode == L2CAP_MODE_STREAMING)
- goto drop;
- BT_DBG("sframe reqseq %d, final %d, poll %d, super %d",
- control->reqseq, control->final, control->poll,
- control->super);
- if (len != 0) {
- l2cap_send_disconn_req(pi->conn, sk,
- ECONNRESET);
- goto drop;
- }
- /* Validate F and P bits */
- if (control->final &&
- ((pi->tx_state != L2CAP_ERTM_TX_STATE_WAIT_F)
- || control->poll))
- goto drop;
- event = l2cap_ertm_rx_func_to_event[control->super];
- if (l2cap_ertm_rx(sk, control, skb, event))
- l2cap_send_disconn_req(pi->conn, sk,
- ECONNRESET);
- }
- goto done;
- default:
- BT_DBG("sk %p: bad mode 0x%2.2x", sk, pi->mode);
- break;
- }
- drop:
- kfree_skb(skb);
- done:
- return 0;
- }
- void l2cap_recv_deferred_frame(struct sock *sk, struct sk_buff *skb)
- {
- lock_sock(sk);
- l2cap_data_channel(sk, skb);
- release_sock(sk);
- }
- static inline int l2cap_conless_channel(struct l2cap_conn *conn, __le16 psm, struct sk_buff *skb)
- {
- struct sock *sk;
- sk = l2cap_get_sock_by_psm(0, psm, conn->src);
- if (!sk)
- goto drop;
- bh_lock_sock(sk);
- BT_DBG("sk %p, len %d", sk, skb->len);
- if (sk->sk_state != BT_BOUND && sk->sk_state != BT_CONNECTED)
- goto drop;
- if (l2cap_pi(sk)->imtu < skb->len)
- goto drop;
- if (!sock_queue_rcv_skb(sk, skb))
- goto done;
- drop:
- kfree_skb(skb);
- done:
- if (sk)
- bh_unlock_sock(sk);
- return 0;
- }
- static inline int l2cap_att_channel(struct l2cap_conn *conn, __le16 cid,
- struct sk_buff *skb)
- {
- struct sock *sk = NULL;
- struct sk_buff *skb_rsp;
- struct l2cap_hdr *lh;
- int dir;
- struct work_struct *open_worker;
- u8 err_rsp[] = {L2CAP_ATT_ERROR, 0x00, 0x00, 0x00,
- L2CAP_ATT_NOT_SUPPORTED};
- if (skb->data[0] == L2CAP_ATT_MTU_REQ) {
- u8 mtu_rsp[] = {L2CAP_ATT_MTU_RSP, 23, 0};
- skb_rsp = bt_skb_alloc(sizeof(mtu_rsp) + L2CAP_HDR_SIZE,
- GFP_ATOMIC);
- if (!skb_rsp)
- goto drop;
- lh = (struct l2cap_hdr *) skb_put(skb_rsp, L2CAP_HDR_SIZE);
- lh->len = cpu_to_le16(sizeof(mtu_rsp));
- lh->cid = cpu_to_le16(L2CAP_CID_LE_DATA);
- memcpy(skb_put(skb_rsp, sizeof(mtu_rsp)), mtu_rsp,
- sizeof(mtu_rsp));
- hci_send_acl(conn->hcon, NULL, skb_rsp, 0);
- goto free_skb;
- }
- dir = (skb->data[0] & L2CAP_ATT_RESPONSE_BIT) ? 0 : 1;
- sk = l2cap_find_sock_by_fixed_cid_and_dir(cid, conn->src,
- conn->dst, dir);
- BT_DBG("sk %p, dir:%d", sk, dir);
- if (!sk)
- goto drop;
- bh_lock_sock(sk);
- BT_DBG("sk %p, len %d", sk, skb->len);
- if (sk->sk_state != BT_BOUND && sk->sk_state != BT_CONNECTED) {
- att_chn_params.cid = cid;
- att_chn_params.conn = conn;
- att_chn_params.dir = dir;
- att_chn_params.skb = skb;
- open_worker = kzalloc(sizeof(*open_worker), GFP_ATOMIC);
- if (!open_worker)
- BT_ERR("Out of memory");
- INIT_WORK(open_worker, l2cap_queue_acl_data);
- schedule_work(open_worker);
- goto done;
- }
- if (l2cap_pi(sk)->imtu < skb->len)
- goto drop;
- if (!sock_queue_rcv_skb(sk, skb))
- goto done;
- drop:
- if (skb->data[0] != L2CAP_ATT_INDICATE)
- goto not_indicate;
- /* If this is an incoming Indication, we are required to confirm */
- skb_rsp = bt_skb_alloc(sizeof(u8) + L2CAP_HDR_SIZE, GFP_ATOMIC);
- if (!skb_rsp)
- goto free_skb;
- lh = (struct l2cap_hdr *) skb_put(skb_rsp, L2CAP_HDR_SIZE);
- lh->len = cpu_to_le16(sizeof(u8));
- lh->cid = cpu_to_le16(L2CAP_CID_LE_DATA);
- err_rsp[0] = L2CAP_ATT_CONFIRM;
- memcpy(skb_put(skb_rsp, sizeof(u8)), err_rsp, sizeof(u8));
- hci_send_acl(conn->hcon, NULL, skb_rsp, 0);
- goto free_skb;
- not_indicate:
- if (skb->data[0] & L2CAP_ATT_RESPONSE_BIT ||
- skb->data[0] == L2CAP_ATT_CONFIRM)
- goto free_skb;
- /* If this is an incoming PDU that requires a response, respond with
- * a generic error so remote device doesn't hang */
- skb_rsp = bt_skb_alloc(sizeof(err_rsp) + L2CAP_HDR_SIZE, GFP_ATOMIC);
- if (!skb_rsp)
- goto free_skb;
- lh = (struct l2cap_hdr *) skb_put(skb_rsp, L2CAP_HDR_SIZE);
- lh->len = cpu_to_le16(sizeof(err_rsp));
- lh->cid = cpu_to_le16(L2CAP_CID_LE_DATA);
- err_rsp[1] = skb->data[0];
- memcpy(skb_put(skb_rsp, sizeof(err_rsp)), err_rsp, sizeof(err_rsp));
- hci_send_acl(conn->hcon, NULL, skb_rsp, 0);
- free_skb:
- kfree_skb(skb);
- done:
- if (sk)
- bh_unlock_sock(sk);
- return 0;
- }
- static void l2cap_recv_frame(struct l2cap_conn *conn, struct sk_buff *skb)
- {
- struct l2cap_hdr *lh = (void *) skb->data;
- struct sock *sk;
- u16 cid, len;
- __le16 psm;
- skb_pull(skb, L2CAP_HDR_SIZE);
- cid = __le16_to_cpu(lh->cid);
- len = __le16_to_cpu(lh->len);
- if (len != skb->len) {
- kfree_skb(skb);
- return;
- }
- BT_DBG("len %d, cid 0x%4.4x", len, cid);
- switch (cid) {
- case L2CAP_CID_LE_SIGNALING:
- case L2CAP_CID_SIGNALING:
- l2cap_sig_channel(conn, skb);
- break;
- case L2CAP_CID_CONN_LESS:
- psm = get_unaligned_le16(skb->data);
- skb_pull(skb, 2);
- l2cap_conless_channel(conn, psm, skb);
- break;
- case L2CAP_CID_LE_DATA:
- l2cap_att_channel(conn, cid, skb);
- break;
- case L2CAP_CID_SMP:
- if (smp_sig_channel(conn, skb))
- l2cap_conn_del(conn->hcon, EACCES, 0);
- break;
- default:
- sk = l2cap_get_chan_by_scid(&conn->chan_list, cid);
- if (sk) {
- if (sock_owned_by_user(sk)) {
- BT_DBG("backlog sk %p", sk);
- if (sk_add_backlog(sk, skb))
- kfree_skb(skb);
- } else
- l2cap_data_channel(sk, skb);
- bh_unlock_sock(sk);
- } else if ((cid == L2CAP_CID_A2MP) && enable_hs) {
- BT_DBG("A2MP");
- amp_conn_ind(conn->hcon, skb);
- } else {
- BT_DBG("unknown cid 0x%4.4x", cid);
- kfree_skb(skb);
- }
- break;
- }
- }
- /* ---- L2CAP interface with lower layer (HCI) ---- */
- static int l2cap_connect_ind(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 type)
- {
- int exact = 0, lm1 = 0, lm2 = 0;
- register struct sock *sk;
- struct hlist_node *node;
- if (type != ACL_LINK)
- return 0;
- BT_DBG("hdev %s, bdaddr %s", hdev->name, batostr(bdaddr));
- /* Find listening sockets and check their link_mode */
- read_lock(&l2cap_sk_list.lock);
- sk_for_each(sk, node, &l2cap_sk_list.head) {
- if (sk->sk_state != BT_LISTEN)
- continue;
- if (!bacmp(&bt_sk(sk)->src, &hdev->bdaddr)) {
- lm1 |= HCI_LM_ACCEPT;
- if (l2cap_pi(sk)->role_switch)
- lm1 |= HCI_LM_MASTER;
- exact++;
- } else if (!bacmp(&bt_sk(sk)->src, BDADDR_ANY)) {
- lm2 |= HCI_LM_ACCEPT;
- if (l2cap_pi(sk)->role_switch)
- lm2 |= HCI_LM_MASTER;
- }
- }
- read_unlock(&l2cap_sk_list.lock);
- return exact ? lm1 : lm2;
- }
- static int l2cap_connect_cfm(struct hci_conn *hcon, u8 status)
- {
- struct l2cap_conn *conn;
- BT_DBG("hcon %p bdaddr %s status %d", hcon, batostr(&hcon->dst), status);
- if (!(hcon->type == ACL_LINK || hcon->type == LE_LINK))
- return -EINVAL;
- if (!status) {
- conn = l2cap_conn_add(hcon, status);
- if (conn)
- l2cap_conn_ready(conn);
- } else
- l2cap_conn_del(hcon, bt_err(status), 0);
- return 0;
- }
- static int l2cap_disconn_ind(struct hci_conn *hcon)
- {
- struct l2cap_conn *conn = hcon->l2cap_data;
- BT_DBG("hcon %p", hcon);
- if (hcon->type != ACL_LINK || !conn)
- return 0x13;
- return conn->disc_reason;
- }
- static int l2cap_disconn_cfm(struct hci_conn *hcon, u8 reason, u8 is_process)
- {
- BT_DBG("hcon %p reason %d", hcon, reason);
- if (!(hcon->type == ACL_LINK || hcon->type == LE_LINK))
- return -EINVAL;
- l2cap_conn_del(hcon, bt_err(reason), is_process);
- return 0;
- }
- static inline void l2cap_check_encryption(struct sock *sk, u8 encrypt)
- {
- if (sk->sk_type != SOCK_SEQPACKET && sk->sk_type != SOCK_STREAM)
- return;
- if (encrypt == 0x00) {
- if (l2cap_pi(sk)->sec_level == BT_SECURITY_MEDIUM) {
- l2cap_sock_clear_timer(sk);
- l2cap_sock_set_timer(sk, HZ * 5);
- } else if (l2cap_pi(sk)->sec_level == BT_SECURITY_HIGH ||
- l2cap_pi(sk)->sec_level == BT_SECURITY_VERY_HIGH)
- __l2cap_sock_close(sk, ECONNREFUSED);
- } else {
- if (l2cap_pi(sk)->sec_level == BT_SECURITY_MEDIUM)
- l2cap_sock_clear_timer(sk);
- }
- }
- static int l2cap_security_cfm(struct hci_conn *hcon, u8 status, u8 encrypt)
- {
- struct l2cap_chan_list *l;
- struct l2cap_conn *conn = hcon->l2cap_data;
- struct sock *sk;
- int smp = 0;
- if (!conn)
- return 0;
- l = &conn->chan_list;
- BT_DBG("conn %p", conn);
- read_lock(&l->lock);
- for (sk = l->head; sk; sk = l2cap_pi(sk)->next_c) {
- bh_lock_sock(sk);
- BT_DBG("sk->scid %d", l2cap_pi(sk)->scid);
- if (l2cap_pi(sk)->scid == L2CAP_CID_LE_DATA) {
- if (!status && encrypt) {
- l2cap_pi(sk)->sec_level = hcon->sec_level;
- l2cap_chan_ready(sk);
- }
- smp = 1;
- bh_unlock_sock(sk);
- continue;
- }
- if (l2cap_pi(sk)->conf_state & L2CAP_CONF_CONNECT_PEND) {
- bh_unlock_sock(sk);
- continue;
- }
- if (!status && (sk->sk_state == BT_CONNECTED ||
- sk->sk_state == BT_CONFIG)) {
- l2cap_check_encryption(sk, encrypt);
- bh_unlock_sock(sk);
- continue;
- }
- if (sk->sk_state == BT_CONNECT) {
- if (!status) {
- l2cap_pi(sk)->conf_state |=
- L2CAP_CONF_CONNECT_PEND;
- if ((l2cap_pi(sk)->amp_pref ==
- BT_AMP_POLICY_PREFER_AMP) &&
- enable_hs) {
- amp_create_physical(l2cap_pi(sk)->conn,
- sk);
- } else
- l2cap_send_conn_req(sk);
- } else {
- l2cap_sock_clear_timer(sk);
- l2cap_sock_set_timer(sk, HZ / 10);
- }
- } else if (sk->sk_state == BT_CONNECT2) {
- struct l2cap_conn_rsp rsp;
- __u16 result;
- if (!status) {
- if (l2cap_pi(sk)->amp_id) {
- amp_accept_physical(conn,
- l2cap_pi(sk)->amp_id, sk);
- bh_unlock_sock(sk);
- continue;
- }
- sk->sk_state = BT_CONFIG;
- result = L2CAP_CR_SUCCESS;
- } else {
- sk->sk_state = BT_DISCONN;
- l2cap_sock_set_timer(sk, HZ / 10);
- result = L2CAP_CR_SEC_BLOCK;
- }
- rsp.scid = cpu_to_le16(l2cap_pi(sk)->dcid);
- rsp.dcid = cpu_to_le16(l2cap_pi(sk)->scid);
- rsp.result = cpu_to_le16(result);
- rsp.status = cpu_to_le16(L2CAP_CS_NO_INFO);
- l2cap_send_cmd(conn, l2cap_pi(sk)->ident,
- L2CAP_CONN_RSP, sizeof(rsp), &rsp);
- if (!(l2cap_pi(sk)->conf_state & L2CAP_CONF_REQ_SENT) &&
- result == L2CAP_CR_SUCCESS) {
- char buf[128];
- l2cap_pi(sk)->conf_state |= L2CAP_CONF_REQ_SENT;
- l2cap_send_cmd(conn, l2cap_get_ident(conn),
- L2CAP_CONF_REQ,
- l2cap_build_conf_req(sk, buf, sizeof(buf)),
- buf);
- l2cap_pi(sk)->num_conf_req++;
- }
- }
- bh_unlock_sock(sk);
- }
- read_unlock(&l->lock);
- if (smp) {
- del_timer(&hcon->smp_timer);
- smp_link_encrypt_cmplt(conn, status, encrypt);
- }
- return 0;
- }
- static int l2cap_recv_acldata(struct hci_conn *hcon, struct sk_buff *skb, u16 flags)
- {
- struct l2cap_conn *conn = hcon->l2cap_data;
- if (!conn && hcon->hdev->dev_type != HCI_BREDR)
- goto drop;
- if (!conn)
- conn = l2cap_conn_add(hcon, 0);
- if (!conn)
- goto drop;
- BT_DBG("conn %p len %d flags 0x%x", conn, skb->len, flags);
- if (flags & ACL_START) {
- struct l2cap_hdr *hdr;
- int len;
- if (conn->rx_len) {
- BT_ERR("Unexpected start frame (len %d)", skb->len);
- kfree_skb(conn->rx_skb);
- conn->rx_skb = NULL;
- conn->rx_len = 0;
- l2cap_conn_unreliable(conn, ECOMM);
- }
- /* Start fragment always begin with Basic L2CAP header */
- if (skb->len < L2CAP_HDR_SIZE) {
- BT_ERR("Frame is too short (len %d)", skb->len);
- l2cap_conn_unreliable(conn, ECOMM);
- goto drop;
- }
- hdr = (struct l2cap_hdr *) skb->data;
- len = __le16_to_cpu(hdr->len) + L2CAP_HDR_SIZE;
- if (len == skb->len) {
- /* Complete frame received */
- l2cap_recv_frame(conn, skb);
- return 0;
- }
- if (flags & ACL_CONT) {
- BT_ERR("Complete frame is incomplete "
- "(len %d, expected len %d)",
- skb->len, len);
- l2cap_conn_unreliable(conn, ECOMM);
- goto drop;
- }
- BT_DBG("Start: total len %d, frag len %d", len, skb->len);
- if (skb->len > len) {
- BT_ERR("Frame is too long (len %d, expected len %d)",
- skb->len, len);
- l2cap_conn_unreliable(conn, ECOMM);
- goto drop;
- }
- /* Allocate skb for the complete frame (with header) */
- conn->rx_skb = bt_skb_alloc(len, GFP_ATOMIC);
- if (!conn->rx_skb)
- goto drop;
- skb_copy_from_linear_data(skb, skb_put(conn->rx_skb, skb->len),
- skb->len);
- conn->rx_len = len - skb->len;
- } else {
- BT_DBG("Cont: frag len %d (expecting %d)", skb->len, conn->rx_len);
- if (!conn->rx_len) {
- BT_ERR("Unexpected continuation frame (len %d)", skb->len);
- l2cap_conn_unreliable(conn, ECOMM);
- goto drop;
- }
- if (skb->len > conn->rx_len) {
- BT_ERR("Fragment is too long (len %d, expected %d)",
- skb->len, conn->rx_len);
- kfree_skb(conn->rx_skb);
- conn->rx_skb = NULL;
- conn->rx_len = 0;
- l2cap_conn_unreliable(conn, ECOMM);
- goto drop;
- }
- skb_copy_from_linear_data(skb, skb_put(conn->rx_skb, skb->len),
- skb->len);
- conn->rx_len -= skb->len;
- if (!conn->rx_len) {
- /* Complete frame received */
- l2cap_recv_frame(conn, conn->rx_skb);
- conn->rx_skb = NULL;
- }
- }
- drop:
- kfree_skb(skb);
- return 0;
- }
- static void l2cap_set_acl_flushto(struct hci_conn *hcon, u16 flush_to)
- {
- struct hci_cp_write_automatic_flush_timeout flush_tm;
- if (hcon && hcon->hdev) {
- flush_tm.handle = hcon->handle;
- if (flush_to == L2CAP_DEFAULT_FLUSH_TO)
- flush_to = 0;
- flush_tm.timeout = (flush_to < L2CAP_MAX_FLUSH_TO) ?
- flush_to : L2CAP_MAX_FLUSH_TO;
- hci_send_cmd(hcon->hdev,
- HCI_OP_WRITE_AUTOMATIC_FLUSH_TIMEOUT,
- 4, &(flush_tm));
- }
- }
- static u16 l2cap_get_smallest_flushto(struct l2cap_chan_list *l)
- {
- int ret_flush_to = L2CAP_DEFAULT_FLUSH_TO;
- struct sock *s;
- for (s = l->head; s; s = l2cap_pi(s)->next_c) {
- if (l2cap_pi(s)->flush_to > 0 &&
- l2cap_pi(s)->flush_to < ret_flush_to)
- ret_flush_to = l2cap_pi(s)->flush_to;
- }
- return ret_flush_to;
- }
- static int l2cap_debugfs_show(struct seq_file *f, void *p)
- {
- struct sock *sk;
- struct hlist_node *node;
- read_lock_bh(&l2cap_sk_list.lock);
- sk_for_each(sk, node, &l2cap_sk_list.head) {
- struct l2cap_pinfo *pi = l2cap_pi(sk);
- seq_printf(f, "%s %s %d %d 0x%4.4x 0x%4.4x %d %d %d %d\n",
- batostr(&bt_sk(sk)->src),
- batostr(&bt_sk(sk)->dst),
- sk->sk_state, __le16_to_cpu(pi->psm),
- pi->scid, pi->dcid,
- pi->imtu, pi->omtu, pi->sec_level,
- pi->mode);
- }
- read_unlock_bh(&l2cap_sk_list.lock);
- return 0;
- }
- static void l2cap_queue_acl_data(struct work_struct *worker)
- {
- struct sock *sk = NULL;
- int attempts = 0;
- struct sk_buff *skb_rsp;
- struct l2cap_hdr *lh;
- u8 err_rsp[] = {L2CAP_ATT_ERROR, 0x00, 0x00, 0x00,
- L2CAP_ATT_NOT_SUPPORTED};
- for (attempts = 0; attempts < 40; attempts++) {
- msleep(50);
- if (!att_chn_params.conn) {
- BT_DBG("att_chn_params.conn is NULL");
- return;
- }
- sk = l2cap_find_sock_by_fixed_cid_and_dir
- (att_chn_params.cid,
- att_chn_params.conn->src,
- att_chn_params.conn->dst,
- att_chn_params.dir);
- bh_lock_sock(sk);
- if (sk->sk_state == BT_CONNECTED) {
- sock_queue_rcv_skb(sk, att_chn_params.skb);
- if (sk)
- bh_unlock_sock(sk);
- return;
- }
- bh_unlock_sock(sk);
- }
- bh_lock_sock(sk);
- if (att_chn_params.skb->data[0] != L2CAP_ATT_INDICATE)
- goto not_indicate;
- /* If this is an incoming Indication, we are required to confirm */
- skb_rsp = bt_skb_alloc(sizeof(u8) + L2CAP_HDR_SIZE, GFP_ATOMIC);
- if (!skb_rsp)
- goto free_skb;
- lh = (struct l2cap_hdr *) skb_put(skb_rsp, L2CAP_HDR_SIZE);
- lh->len = cpu_to_le16(sizeof(u8));
- lh->cid = cpu_to_le16(L2CAP_CID_LE_DATA);
- err_rsp[0] = L2CAP_ATT_CONFIRM;
- memcpy(skb_put(skb_rsp, sizeof(u8)), err_rsp, sizeof(u8));
- hci_send_acl(att_chn_params.conn->hcon, NULL, skb_rsp, 0);
- goto free_skb;
- not_indicate:
- if (att_chn_params.skb->data[0] & L2CAP_ATT_RESPONSE_BIT ||
- att_chn_params.skb->data[0] == L2CAP_ATT_CONFIRM)
- goto free_skb;
- /* If this is an incoming PDU that requires a response, respond with
- * a generic error so remote device doesn't hang */
- skb_rsp = bt_skb_alloc(sizeof(err_rsp) + L2CAP_HDR_SIZE, GFP_ATOMIC);
- if (!skb_rsp)
- goto free_skb;
- lh = (struct l2cap_hdr *) skb_put(skb_rsp, L2CAP_HDR_SIZE);
- lh->len = cpu_to_le16(sizeof(err_rsp));
- lh->cid = cpu_to_le16(L2CAP_CID_LE_DATA);
- err_rsp[1] = att_chn_params.skb->data[0];
- memcpy(skb_put(skb_rsp, sizeof(err_rsp)), err_rsp, sizeof(err_rsp));
- hci_send_acl(att_chn_params.conn->hcon, NULL, skb_rsp, 0);
- free_skb:
- kfree_skb(att_chn_params.skb);
- if (sk)
- bh_unlock_sock(sk);
- }
- static int l2cap_debugfs_open(struct inode *inode, struct file *file)
- {
- return single_open(file, l2cap_debugfs_show, inode->i_private);
- }
- static const struct file_operations l2cap_debugfs_fops = {
- .open = l2cap_debugfs_open,
- .read = seq_read,
- .llseek = seq_lseek,
- .release = single_release,
- };
- static struct dentry *l2cap_debugfs;
- static struct hci_proto l2cap_hci_proto = {
- .name = "L2CAP",
- .id = HCI_PROTO_L2CAP,
- .connect_ind = l2cap_connect_ind,
- .connect_cfm = l2cap_connect_cfm,
- .disconn_ind = l2cap_disconn_ind,
- .disconn_cfm = l2cap_disconn_cfm,
- .security_cfm = l2cap_security_cfm,
- .recv_acldata = l2cap_recv_acldata,
- .create_cfm = l2cap_create_cfm,
- .modify_cfm = l2cap_modify_cfm,
- .destroy_cfm = l2cap_destroy_cfm,
- };
- int __init l2cap_init(void)
- {
- int err;
- err = l2cap_init_sockets();
- if (err < 0)
- return err;
- _l2cap_wq = create_singlethread_workqueue("l2cap");
- if (!_l2cap_wq) {
- err = -ENOMEM;
- goto error;
- }
- err = hci_register_proto(&l2cap_hci_proto);
- if (err < 0) {
- BT_ERR("L2CAP protocol registration failed");
- bt_sock_unregister(BTPROTO_L2CAP);
- goto error;
- }
- if (bt_debugfs) {
- l2cap_debugfs = debugfs_create_file("l2cap", 0444,
- bt_debugfs, NULL, &l2cap_debugfs_fops);
- if (!l2cap_debugfs)
- BT_ERR("Failed to create L2CAP debug file");
- }
- if (amp_init() < 0) {
- BT_ERR("AMP Manager initialization failed");
- goto error;
- }
- return 0;
- error:
- destroy_workqueue(_l2cap_wq);
- l2cap_cleanup_sockets();
- return err;
- }
- void l2cap_exit(void)
- {
- amp_exit();
- debugfs_remove(l2cap_debugfs);
- flush_workqueue(_l2cap_wq);
- destroy_workqueue(_l2cap_wq);
- if (hci_unregister_proto(&l2cap_hci_proto) < 0)
- BT_ERR("L2CAP protocol unregistration failed");
- l2cap_cleanup_sockets();
- }
- module_param(disable_ertm, bool, 0644);
- MODULE_PARM_DESC(disable_ertm, "Disable enhanced retransmission mode");
- module_param(enable_hs, bool, 0644);
- MODULE_PARM_DESC(enable_hs, "Enable A2MP protocol");
- module_param(enable_reconfig, bool, 0644);
- MODULE_PARM_DESC(enable_reconfig, "Enable reconfig after initiating AMP move");
|