l2cap_core.c 193 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069
  1. /*
  2. BlueZ - Bluetooth protocol stack for Linux
  3. Copyright (c) 2000-2001, 2010-2013 The Linux Foundation. All rights reserved.
  4. Copyright (C) 2009-2010 Gustavo F. Padovan <gustavo@padovan.org>
  5. Copyright (C) 2010 Google Inc.
  6. Written 2000,2001 by Maxim Krasnyansky <maxk@qualcomm.com>
  7. This program is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License version 2 as
  9. published by the Free Software Foundation;
  10. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  11. OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  12. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS.
  13. IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) AND AUTHOR(S) BE LIABLE FOR ANY
  14. CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES
  15. WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  16. ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  17. OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  18. ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS,
  19. COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS
  20. SOFTWARE IS DISCLAIMED.
  21. */
  22. /* Bluetooth L2CAP core. */
  23. #include <linux/module.h>
  24. #include <linux/types.h>
  25. #include <linux/capability.h>
  26. #include <linux/errno.h>
  27. #include <linux/kernel.h>
  28. #include <linux/sched.h>
  29. #include <linux/slab.h>
  30. #include <linux/poll.h>
  31. #include <linux/fcntl.h>
  32. #include <linux/init.h>
  33. #include <linux/interrupt.h>
  34. #include <linux/socket.h>
  35. #include <linux/skbuff.h>
  36. #include <linux/list.h>
  37. #include <linux/device.h>
  38. #include <linux/debugfs.h>
  39. #include <linux/seq_file.h>
  40. #include <linux/uaccess.h>
  41. #include <linux/crc16.h>
  42. #include <linux/math64.h>
  43. #include <net/sock.h>
  44. #include <asm/unaligned.h>
  45. #include <net/bluetooth/bluetooth.h>
  46. #include <net/bluetooth/hci_core.h>
  47. #include <net/bluetooth/l2cap.h>
  48. #include <net/bluetooth/smp.h>
  49. #include <net/bluetooth/amp.h>
  50. bool disable_ertm;
  51. bool enable_hs;
  52. bool enable_reconfig;
  53. static u32 l2cap_feat_mask = L2CAP_FEAT_FIXED_CHAN;
  54. static u8 l2cap_fc_mask = L2CAP_FC_L2CAP;
  55. struct workqueue_struct *_l2cap_wq;
  56. struct bt_sock_list l2cap_sk_list = {
  57. .lock = __RW_LOCK_UNLOCKED(l2cap_sk_list.lock)
  58. };
  59. static void l2cap_send_move_chan_req(struct l2cap_conn *conn,
  60. struct l2cap_pinfo *pi, u16 icid, u8 dest_amp_id);
  61. static void l2cap_send_move_chan_cfm(struct l2cap_conn *conn,
  62. struct l2cap_pinfo *pi, u16 icid, u16 result);
  63. static void l2cap_send_move_chan_rsp(struct l2cap_conn *conn, u8 ident,
  64. u16 icid, u16 result);
  65. static void l2cap_amp_move_setup(struct sock *sk);
  66. static void l2cap_amp_move_success(struct sock *sk);
  67. static void l2cap_amp_move_revert(struct sock *sk);
  68. static int l2cap_ertm_rx_queued_iframes(struct sock *sk);
  69. static struct sk_buff *l2cap_build_cmd(struct l2cap_conn *conn,
  70. u8 code, u8 ident, u16 dlen, void *data);
  71. static int l2cap_answer_move_poll(struct sock *sk);
  72. static int l2cap_create_cfm(struct hci_chan *chan, u8 status);
  73. static int l2cap_deaggregate(struct hci_chan *chan, struct l2cap_pinfo *pi);
  74. static void l2cap_chan_ready(struct sock *sk);
  75. static void l2cap_conn_del(struct hci_conn *hcon, int err, u8 is_process);
  76. static u16 l2cap_get_smallest_flushto(struct l2cap_chan_list *l);
  77. static void l2cap_set_acl_flushto(struct hci_conn *hcon, u16 flush_to);
  78. static void l2cap_queue_acl_data(struct work_struct *worker);
  79. static struct att_channel_parameters{
  80. struct sk_buff *skb;
  81. struct l2cap_conn *conn;
  82. __le16 cid;
  83. int dir;
  84. } att_chn_params;
  85. /* ---- L2CAP channels ---- */
  86. static struct sock *__l2cap_get_chan_by_dcid(struct l2cap_chan_list *l, u16 cid)
  87. {
  88. struct sock *s;
  89. for (s = l->head; s; s = l2cap_pi(s)->next_c) {
  90. if (l2cap_pi(s)->dcid == cid)
  91. break;
  92. }
  93. return s;
  94. }
  95. /* Find channel with given DCID.
  96. * Returns locked socket */
  97. static inline struct sock *l2cap_get_chan_by_dcid(struct l2cap_chan_list *l,
  98. u16 cid)
  99. {
  100. struct sock *s;
  101. read_lock(&l->lock);
  102. s = __l2cap_get_chan_by_dcid(l, cid);
  103. if (s)
  104. bh_lock_sock(s);
  105. read_unlock(&l->lock);
  106. return s;
  107. }
  108. static struct sock *__l2cap_get_chan_by_scid(struct l2cap_chan_list *l, u16 cid)
  109. {
  110. struct sock *s;
  111. for (s = l->head; s; s = l2cap_pi(s)->next_c) {
  112. if (l2cap_pi(s)->scid == cid)
  113. break;
  114. }
  115. return s;
  116. }
  117. /* Find channel with given SCID.
  118. * Returns locked socket */
  119. static inline struct sock *l2cap_get_chan_by_scid(struct l2cap_chan_list *l, u16 cid)
  120. {
  121. struct sock *s;
  122. read_lock(&l->lock);
  123. s = __l2cap_get_chan_by_scid(l, cid);
  124. if (s)
  125. bh_lock_sock(s);
  126. read_unlock(&l->lock);
  127. return s;
  128. }
  129. static struct sock *__l2cap_get_chan_by_ident(struct l2cap_chan_list *l, u8 ident)
  130. {
  131. struct sock *s;
  132. for (s = l->head; s; s = l2cap_pi(s)->next_c) {
  133. if (l2cap_pi(s)->ident == ident)
  134. break;
  135. }
  136. return s;
  137. }
  138. static inline struct sock *l2cap_get_chan_by_ident(struct l2cap_chan_list *l, u8 ident)
  139. {
  140. struct sock *s;
  141. read_lock(&l->lock);
  142. s = __l2cap_get_chan_by_ident(l, ident);
  143. if (s)
  144. bh_lock_sock(s);
  145. read_unlock(&l->lock);
  146. return s;
  147. }
  148. static inline struct sk_buff *l2cap_ertm_seq_in_queue(struct sk_buff_head *head,
  149. u16 seq)
  150. {
  151. struct sk_buff *skb;
  152. skb_queue_walk(head, skb) {
  153. if (bt_cb(skb)->control.txseq == seq)
  154. return skb;
  155. }
  156. return NULL;
  157. }
  158. static int l2cap_seq_list_init(struct l2cap_seq_list *seq_list, u16 size)
  159. {
  160. u16 allocSize = 1;
  161. int err = 0;
  162. int i;
  163. /* Actual allocated size must be a power of 2 */
  164. while (allocSize && allocSize <= size)
  165. allocSize <<= 1;
  166. if (!allocSize)
  167. return -ENOMEM;
  168. seq_list->list = kzalloc(sizeof(u16) * allocSize, GFP_ATOMIC);
  169. if (!seq_list->list)
  170. return -ENOMEM;
  171. seq_list->size = allocSize;
  172. seq_list->mask = allocSize - 1;
  173. seq_list->head = L2CAP_SEQ_LIST_CLEAR;
  174. seq_list->tail = L2CAP_SEQ_LIST_CLEAR;
  175. for (i = 0; i < allocSize; i++)
  176. seq_list->list[i] = L2CAP_SEQ_LIST_CLEAR;
  177. return err;
  178. }
  179. static inline void l2cap_seq_list_free(struct l2cap_seq_list *seq_list)
  180. {
  181. kfree(seq_list->list);
  182. }
  183. static inline bool l2cap_seq_list_contains(struct l2cap_seq_list *seq_list,
  184. u16 seq)
  185. {
  186. return seq_list->list[seq & seq_list->mask] != L2CAP_SEQ_LIST_CLEAR;
  187. }
  188. static u16 l2cap_seq_list_remove(struct l2cap_seq_list *seq_list, u16 seq)
  189. {
  190. u16 mask = seq_list->mask;
  191. BT_DBG("seq_list %p, seq %d", seq_list, (int) seq);
  192. if (seq_list->head == L2CAP_SEQ_LIST_CLEAR) {
  193. /* In case someone tries to pop the head of an empty list */
  194. BT_DBG("List empty");
  195. return L2CAP_SEQ_LIST_CLEAR;
  196. } else if (seq_list->head == seq) {
  197. /* Head can be removed quickly */
  198. BT_DBG("Remove head");
  199. seq_list->head = seq_list->list[seq & mask];
  200. seq_list->list[seq & mask] = L2CAP_SEQ_LIST_CLEAR;
  201. if (seq_list->head == L2CAP_SEQ_LIST_TAIL) {
  202. seq_list->head = L2CAP_SEQ_LIST_CLEAR;
  203. seq_list->tail = L2CAP_SEQ_LIST_CLEAR;
  204. }
  205. } else {
  206. /* Non-head item must be found first */
  207. u16 prev = seq_list->head;
  208. BT_DBG("Find and remove");
  209. while (seq_list->list[prev & mask] != seq) {
  210. prev = seq_list->list[prev & mask];
  211. if (prev == L2CAP_SEQ_LIST_TAIL) {
  212. BT_DBG("seq %d not in list", (int) seq);
  213. return L2CAP_SEQ_LIST_CLEAR;
  214. }
  215. }
  216. seq_list->list[prev & mask] = seq_list->list[seq & mask];
  217. seq_list->list[seq & mask] = L2CAP_SEQ_LIST_CLEAR;
  218. if (seq_list->tail == seq)
  219. seq_list->tail = prev;
  220. }
  221. return seq;
  222. }
  223. static inline u16 l2cap_seq_list_pop(struct l2cap_seq_list *seq_list)
  224. {
  225. return l2cap_seq_list_remove(seq_list, seq_list->head);
  226. }
  227. static void l2cap_seq_list_clear(struct l2cap_seq_list *seq_list)
  228. {
  229. if (seq_list->head != L2CAP_SEQ_LIST_CLEAR) {
  230. u16 i;
  231. for (i = 0; i < seq_list->size; i++)
  232. seq_list->list[i] = L2CAP_SEQ_LIST_CLEAR;
  233. seq_list->head = L2CAP_SEQ_LIST_CLEAR;
  234. seq_list->tail = L2CAP_SEQ_LIST_CLEAR;
  235. }
  236. }
  237. static void l2cap_seq_list_append(struct l2cap_seq_list *seq_list, u16 seq)
  238. {
  239. u16 mask = seq_list->mask;
  240. BT_DBG("seq_list %p, seq %d", seq_list, (int) seq);
  241. if (seq_list->list[seq & mask] == L2CAP_SEQ_LIST_CLEAR) {
  242. if (seq_list->tail == L2CAP_SEQ_LIST_CLEAR)
  243. seq_list->head = seq;
  244. else
  245. seq_list->list[seq_list->tail & mask] = seq;
  246. seq_list->tail = seq;
  247. seq_list->list[seq & mask] = L2CAP_SEQ_LIST_TAIL;
  248. }
  249. }
  250. static u16 __pack_enhanced_control(struct bt_l2cap_control *control)
  251. {
  252. u16 packed;
  253. packed = (control->reqseq << L2CAP_CTRL_REQSEQ_SHIFT) &
  254. L2CAP_CTRL_REQSEQ;
  255. packed |= (control->final << L2CAP_CTRL_FINAL_SHIFT) &
  256. L2CAP_CTRL_FINAL;
  257. if (control->frame_type == 's') {
  258. packed |= (control->poll << L2CAP_CTRL_POLL_SHIFT) &
  259. L2CAP_CTRL_POLL;
  260. packed |= (control->super << L2CAP_CTRL_SUPERVISE_SHIFT) &
  261. L2CAP_CTRL_SUPERVISE;
  262. packed |= L2CAP_CTRL_FRAME_TYPE;
  263. } else {
  264. packed |= (control->sar << L2CAP_CTRL_SAR_SHIFT) &
  265. L2CAP_CTRL_SAR;
  266. packed |= (control->txseq << L2CAP_CTRL_TXSEQ_SHIFT) &
  267. L2CAP_CTRL_TXSEQ;
  268. }
  269. return packed;
  270. }
  271. static void __get_enhanced_control(u16 enhanced,
  272. struct bt_l2cap_control *control)
  273. {
  274. control->reqseq = (enhanced & L2CAP_CTRL_REQSEQ) >>
  275. L2CAP_CTRL_REQSEQ_SHIFT;
  276. control->final = (enhanced & L2CAP_CTRL_FINAL) >>
  277. L2CAP_CTRL_FINAL_SHIFT;
  278. if (enhanced & L2CAP_CTRL_FRAME_TYPE) {
  279. control->frame_type = 's';
  280. control->poll = (enhanced & L2CAP_CTRL_POLL) >>
  281. L2CAP_CTRL_POLL_SHIFT;
  282. control->super = (enhanced & L2CAP_CTRL_SUPERVISE) >>
  283. L2CAP_CTRL_SUPERVISE_SHIFT;
  284. control->sar = 0;
  285. control->txseq = 0;
  286. } else {
  287. control->frame_type = 'i';
  288. control->sar = (enhanced & L2CAP_CTRL_SAR) >>
  289. L2CAP_CTRL_SAR_SHIFT;
  290. control->txseq = (enhanced & L2CAP_CTRL_TXSEQ) >>
  291. L2CAP_CTRL_TXSEQ_SHIFT;
  292. control->poll = 0;
  293. control->super = 0;
  294. }
  295. }
  296. static u32 __pack_extended_control(struct bt_l2cap_control *control)
  297. {
  298. u32 packed;
  299. packed = (control->reqseq << L2CAP_EXT_CTRL_REQSEQ_SHIFT) &
  300. L2CAP_EXT_CTRL_REQSEQ;
  301. packed |= (control->final << L2CAP_EXT_CTRL_FINAL_SHIFT) &
  302. L2CAP_EXT_CTRL_FINAL;
  303. if (control->frame_type == 's') {
  304. packed |= (control->poll << L2CAP_EXT_CTRL_POLL_SHIFT) &
  305. L2CAP_EXT_CTRL_POLL;
  306. packed |= (control->super << L2CAP_EXT_CTRL_SUPERVISE_SHIFT) &
  307. L2CAP_EXT_CTRL_SUPERVISE;
  308. packed |= L2CAP_EXT_CTRL_FRAME_TYPE;
  309. } else {
  310. packed |= (control->sar << L2CAP_EXT_CTRL_SAR_SHIFT) &
  311. L2CAP_EXT_CTRL_SAR;
  312. packed |= (control->txseq << L2CAP_EXT_CTRL_TXSEQ_SHIFT) &
  313. L2CAP_EXT_CTRL_TXSEQ;
  314. }
  315. return packed;
  316. }
  317. static void __get_extended_control(u32 extended,
  318. struct bt_l2cap_control *control)
  319. {
  320. control->reqseq = (extended & L2CAP_EXT_CTRL_REQSEQ) >>
  321. L2CAP_EXT_CTRL_REQSEQ_SHIFT;
  322. control->final = (extended & L2CAP_EXT_CTRL_FINAL) >>
  323. L2CAP_EXT_CTRL_FINAL_SHIFT;
  324. if (extended & L2CAP_EXT_CTRL_FRAME_TYPE) {
  325. control->frame_type = 's';
  326. control->poll = (extended & L2CAP_EXT_CTRL_POLL) >>
  327. L2CAP_EXT_CTRL_POLL_SHIFT;
  328. control->super = (extended & L2CAP_EXT_CTRL_SUPERVISE) >>
  329. L2CAP_EXT_CTRL_SUPERVISE_SHIFT;
  330. control->sar = 0;
  331. control->txseq = 0;
  332. } else {
  333. control->frame_type = 'i';
  334. control->sar = (extended & L2CAP_EXT_CTRL_SAR) >>
  335. L2CAP_EXT_CTRL_SAR_SHIFT;
  336. control->txseq = (extended & L2CAP_EXT_CTRL_TXSEQ) >>
  337. L2CAP_EXT_CTRL_TXSEQ_SHIFT;
  338. control->poll = 0;
  339. control->super = 0;
  340. }
  341. }
  342. static inline void l2cap_ertm_stop_ack_timer(struct l2cap_pinfo *pi)
  343. {
  344. BT_DBG("pi %p", pi);
  345. __cancel_delayed_work(&pi->ack_work);
  346. }
  347. static inline void l2cap_ertm_start_ack_timer(struct l2cap_pinfo *pi)
  348. {
  349. BT_DBG("pi %p, pending %d", pi, delayed_work_pending(&pi->ack_work));
  350. if (!delayed_work_pending(&pi->ack_work)) {
  351. queue_delayed_work(_l2cap_wq, &pi->ack_work,
  352. msecs_to_jiffies(L2CAP_DEFAULT_ACK_TO));
  353. }
  354. }
  355. static inline void l2cap_ertm_stop_retrans_timer(struct l2cap_pinfo *pi)
  356. {
  357. BT_DBG("pi %p", pi);
  358. __cancel_delayed_work(&pi->retrans_work);
  359. }
  360. static inline void l2cap_ertm_start_retrans_timer(struct l2cap_pinfo *pi)
  361. {
  362. BT_DBG("pi %p", pi);
  363. if (!delayed_work_pending(&pi->monitor_work) && pi->retrans_timeout) {
  364. __cancel_delayed_work(&pi->retrans_work);
  365. queue_delayed_work(_l2cap_wq, &pi->retrans_work,
  366. msecs_to_jiffies(pi->retrans_timeout));
  367. }
  368. }
  369. static inline void l2cap_ertm_stop_monitor_timer(struct l2cap_pinfo *pi)
  370. {
  371. BT_DBG("pi %p", pi);
  372. __cancel_delayed_work(&pi->monitor_work);
  373. }
  374. static inline void l2cap_ertm_start_monitor_timer(struct l2cap_pinfo *pi)
  375. {
  376. BT_DBG("pi %p", pi);
  377. l2cap_ertm_stop_retrans_timer(pi);
  378. __cancel_delayed_work(&pi->monitor_work);
  379. if (pi->monitor_timeout) {
  380. queue_delayed_work(_l2cap_wq, &pi->monitor_work,
  381. msecs_to_jiffies(pi->monitor_timeout));
  382. }
  383. }
  384. static u16 l2cap_alloc_cid(struct l2cap_chan_list *l)
  385. {
  386. u16 cid = L2CAP_CID_DYN_START;
  387. for (; cid < L2CAP_CID_DYN_END; cid++) {
  388. if (!__l2cap_get_chan_by_scid(l, cid))
  389. return cid;
  390. }
  391. return 0;
  392. }
  393. static inline void __l2cap_chan_link(struct l2cap_chan_list *l, struct sock *sk)
  394. {
  395. sock_hold(sk);
  396. if (l->head)
  397. l2cap_pi(l->head)->prev_c = sk;
  398. l2cap_pi(sk)->next_c = l->head;
  399. l2cap_pi(sk)->prev_c = NULL;
  400. l->head = sk;
  401. }
  402. static inline void l2cap_chan_unlink(struct l2cap_chan_list *l, struct sock *sk)
  403. {
  404. struct sock *next = l2cap_pi(sk)->next_c, *prev = l2cap_pi(sk)->prev_c;
  405. write_lock_bh(&l->lock);
  406. if (sk == l->head)
  407. l->head = next;
  408. if (next)
  409. l2cap_pi(next)->prev_c = prev;
  410. if (prev)
  411. l2cap_pi(prev)->next_c = next;
  412. write_unlock_bh(&l->lock);
  413. __sock_put(sk);
  414. }
  415. static void __l2cap_chan_add(struct l2cap_conn *conn, struct sock *sk)
  416. {
  417. struct l2cap_chan_list *l = &conn->chan_list;
  418. BT_DBG("conn %p, psm 0x%2.2x, dcid 0x%4.4x", conn,
  419. l2cap_pi(sk)->psm, l2cap_pi(sk)->dcid);
  420. conn->disc_reason = 0x13;
  421. l2cap_pi(sk)->conn = conn;
  422. if (!l2cap_pi(sk)->fixed_channel &&
  423. (sk->sk_type == SOCK_SEQPACKET || sk->sk_type == SOCK_STREAM)) {
  424. if (conn->hcon->type == LE_LINK) {
  425. /* LE connection */
  426. if (l2cap_pi(sk)->imtu < L2CAP_LE_DEFAULT_MTU)
  427. l2cap_pi(sk)->imtu = L2CAP_LE_DEFAULT_MTU;
  428. if (l2cap_pi(sk)->omtu < L2CAP_LE_DEFAULT_MTU)
  429. l2cap_pi(sk)->omtu = L2CAP_LE_DEFAULT_MTU;
  430. l2cap_pi(sk)->scid = L2CAP_CID_LE_DATA;
  431. l2cap_pi(sk)->dcid = L2CAP_CID_LE_DATA;
  432. } else {
  433. /* Alloc CID for connection-oriented socket */
  434. l2cap_pi(sk)->scid = l2cap_alloc_cid(l);
  435. l2cap_pi(sk)->omtu = L2CAP_DEFAULT_MTU;
  436. }
  437. } else if (sk->sk_type == SOCK_DGRAM) {
  438. /* Connectionless socket */
  439. l2cap_pi(sk)->scid = L2CAP_CID_CONN_LESS;
  440. l2cap_pi(sk)->dcid = L2CAP_CID_CONN_LESS;
  441. l2cap_pi(sk)->omtu = L2CAP_DEFAULT_MTU;
  442. } else if (sk->sk_type == SOCK_RAW) {
  443. /* Raw socket can send/recv signalling messages only */
  444. l2cap_pi(sk)->scid = L2CAP_CID_SIGNALING;
  445. l2cap_pi(sk)->dcid = L2CAP_CID_SIGNALING;
  446. l2cap_pi(sk)->omtu = L2CAP_DEFAULT_MTU;
  447. }
  448. if (l2cap_get_smallest_flushto(l) > l2cap_pi(sk)->flush_to) {
  449. /*if flush timeout of the channel is lesser than existing */
  450. l2cap_set_acl_flushto(conn->hcon, l2cap_pi(sk)->flush_to);
  451. }
  452. /* Otherwise, do not set scid/dcid/omtu. These will be set up
  453. * by l2cap_fixed_channel_config()
  454. */
  455. __l2cap_chan_link(l, sk);
  456. }
  457. /* Delete channel.
  458. * Must be called on the locked socket. */
  459. void l2cap_chan_del(struct sock *sk, int err)
  460. {
  461. struct l2cap_conn *conn = l2cap_pi(sk)->conn;
  462. struct sock *parent = bt_sk(sk)->parent;
  463. l2cap_sock_clear_timer(sk);
  464. BT_DBG("sk %p, conn %p, err %d", sk, conn, err);
  465. if (conn) {
  466. struct l2cap_chan_list *l = &conn->chan_list;
  467. /* Unlink from channel list */
  468. l2cap_chan_unlink(l, sk);
  469. l2cap_pi(sk)->conn = NULL;
  470. if (!l2cap_pi(sk)->fixed_channel)
  471. hci_conn_put(conn->hcon);
  472. read_lock(&l->lock);
  473. if (l2cap_pi(sk)->flush_to < l2cap_get_smallest_flushto(l))
  474. l2cap_set_acl_flushto(conn->hcon,
  475. l2cap_get_smallest_flushto(l));
  476. read_unlock(&l->lock);
  477. }
  478. if (l2cap_pi(sk)->ampchan) {
  479. struct hci_chan *ampchan = l2cap_pi(sk)->ampchan;
  480. struct hci_conn *ampcon = l2cap_pi(sk)->ampcon;
  481. l2cap_pi(sk)->ampchan = NULL;
  482. l2cap_pi(sk)->ampcon = NULL;
  483. l2cap_pi(sk)->amp_id = 0;
  484. if (hci_chan_put(ampchan))
  485. ampcon->l2cap_data = NULL;
  486. else
  487. l2cap_deaggregate(ampchan, l2cap_pi(sk));
  488. }
  489. sk->sk_state = BT_CLOSED;
  490. sock_set_flag(sk, SOCK_ZAPPED);
  491. if (err)
  492. sk->sk_err = err;
  493. if (parent) {
  494. bt_accept_unlink(sk);
  495. parent->sk_data_ready(parent, 0);
  496. } else
  497. sk->sk_state_change(sk);
  498. sk->sk_send_head = NULL;
  499. skb_queue_purge(TX_QUEUE(sk));
  500. if (l2cap_pi(sk)->mode == L2CAP_MODE_ERTM) {
  501. if (l2cap_pi(sk)->sdu)
  502. kfree_skb(l2cap_pi(sk)->sdu);
  503. skb_queue_purge(SREJ_QUEUE(sk));
  504. __cancel_delayed_work(&l2cap_pi(sk)->ack_work);
  505. __cancel_delayed_work(&l2cap_pi(sk)->retrans_work);
  506. __cancel_delayed_work(&l2cap_pi(sk)->monitor_work);
  507. }
  508. }
  509. static inline u8 l2cap_get_auth_type(struct sock *sk)
  510. {
  511. if (sk->sk_type == SOCK_RAW) {
  512. switch (l2cap_pi(sk)->sec_level) {
  513. case BT_SECURITY_VERY_HIGH:
  514. case BT_SECURITY_HIGH:
  515. return HCI_AT_DEDICATED_BONDING_MITM;
  516. case BT_SECURITY_MEDIUM:
  517. return HCI_AT_DEDICATED_BONDING;
  518. default:
  519. return HCI_AT_NO_BONDING;
  520. }
  521. } else if (l2cap_pi(sk)->psm == cpu_to_le16(0x0001)) {
  522. if (l2cap_pi(sk)->sec_level == BT_SECURITY_LOW)
  523. l2cap_pi(sk)->sec_level = BT_SECURITY_SDP;
  524. if (l2cap_pi(sk)->sec_level == BT_SECURITY_HIGH ||
  525. l2cap_pi(sk)->sec_level == BT_SECURITY_VERY_HIGH)
  526. return HCI_AT_NO_BONDING_MITM;
  527. else
  528. return HCI_AT_NO_BONDING;
  529. } else {
  530. switch (l2cap_pi(sk)->sec_level) {
  531. case BT_SECURITY_VERY_HIGH:
  532. case BT_SECURITY_HIGH:
  533. return HCI_AT_GENERAL_BONDING_MITM;
  534. case BT_SECURITY_MEDIUM:
  535. return HCI_AT_GENERAL_BONDING;
  536. default:
  537. return HCI_AT_NO_BONDING;
  538. }
  539. }
  540. }
  541. /* Service level security */
  542. static inline int l2cap_check_security(struct sock *sk)
  543. {
  544. struct l2cap_conn *conn = l2cap_pi(sk)->conn;
  545. __u8 auth_type;
  546. auth_type = l2cap_get_auth_type(sk);
  547. return hci_conn_security(conn->hcon, l2cap_pi(sk)->sec_level,
  548. auth_type);
  549. }
  550. u8 l2cap_get_ident(struct l2cap_conn *conn)
  551. {
  552. u8 id;
  553. /* Get next available identificator.
  554. * 1 - 128 are used by kernel.
  555. * 129 - 199 are reserved.
  556. * 200 - 254 are used by utilities like l2ping, etc.
  557. */
  558. spin_lock_bh(&conn->lock);
  559. if (++conn->tx_ident > 128)
  560. conn->tx_ident = 1;
  561. id = conn->tx_ident;
  562. spin_unlock_bh(&conn->lock);
  563. return id;
  564. }
  565. static void apply_fcs(struct sk_buff *skb)
  566. {
  567. size_t len;
  568. u16 partial_crc;
  569. struct sk_buff *iter;
  570. struct sk_buff *final_frag = skb;
  571. if (skb_has_frag_list(skb))
  572. len = skb_headlen(skb);
  573. else
  574. len = skb->len - L2CAP_FCS_SIZE;
  575. partial_crc = crc16(0, (u8 *) skb->data, len);
  576. skb_walk_frags(skb, iter) {
  577. len = iter->len;
  578. if (!iter->next)
  579. len -= L2CAP_FCS_SIZE;
  580. partial_crc = crc16(partial_crc, iter->data, len);
  581. final_frag = iter;
  582. }
  583. put_unaligned_le16(partial_crc,
  584. final_frag->data + final_frag->len - L2CAP_FCS_SIZE);
  585. }
  586. void l2cap_send_cmd(struct l2cap_conn *conn, u8 ident, u8 code, u16 len, void *data)
  587. {
  588. struct sk_buff *skb = l2cap_build_cmd(conn, code, ident, len, data);
  589. u8 flags;
  590. BT_DBG("code 0x%2.2x", code);
  591. if (!skb)
  592. return;
  593. if (conn->hcon == NULL || conn->hcon->hdev == NULL)
  594. return;
  595. if (lmp_no_flush_capable(conn->hcon->hdev))
  596. flags = ACL_START_NO_FLUSH;
  597. else
  598. flags = ACL_START;
  599. bt_cb(skb)->force_active = 1;
  600. hci_send_acl(conn->hcon, NULL, skb, flags);
  601. }
  602. static inline int __l2cap_no_conn_pending(struct sock *sk)
  603. {
  604. return !(l2cap_pi(sk)->conf_state & L2CAP_CONF_CONNECT_PEND);
  605. }
  606. static void l2cap_send_conn_req(struct sock *sk)
  607. {
  608. struct l2cap_conn_req req;
  609. req.scid = cpu_to_le16(l2cap_pi(sk)->scid);
  610. req.psm = l2cap_pi(sk)->psm;
  611. l2cap_pi(sk)->ident = l2cap_get_ident(l2cap_pi(sk)->conn);
  612. l2cap_send_cmd(l2cap_pi(sk)->conn, l2cap_pi(sk)->ident,
  613. L2CAP_CONN_REQ, sizeof(req), &req);
  614. }
  615. static void l2cap_send_create_chan_req(struct sock *sk, u8 amp_id)
  616. {
  617. struct l2cap_create_chan_req req;
  618. req.scid = cpu_to_le16(l2cap_pi(sk)->scid);
  619. req.psm = l2cap_pi(sk)->psm;
  620. req.amp_id = amp_id;
  621. l2cap_pi(sk)->conf_state |= L2CAP_CONF_LOCKSTEP;
  622. l2cap_pi(sk)->ident = l2cap_get_ident(l2cap_pi(sk)->conn);
  623. l2cap_send_cmd(l2cap_pi(sk)->conn, l2cap_pi(sk)->ident,
  624. L2CAP_CREATE_CHAN_REQ, sizeof(req), &req);
  625. }
  626. static void l2cap_do_start(struct sock *sk)
  627. {
  628. struct l2cap_conn *conn = l2cap_pi(sk)->conn;
  629. if (conn->info_state & L2CAP_INFO_FEAT_MASK_REQ_SENT) {
  630. if (!(conn->info_state & L2CAP_INFO_FEAT_MASK_REQ_DONE))
  631. return;
  632. if (l2cap_check_security(sk) && __l2cap_no_conn_pending(sk)) {
  633. l2cap_pi(sk)->conf_state |= L2CAP_CONF_CONNECT_PEND;
  634. if (l2cap_pi(sk)->amp_pref ==
  635. BT_AMP_POLICY_PREFER_AMP &&
  636. enable_hs &&
  637. conn->fc_mask & L2CAP_FC_A2MP)
  638. amp_create_physical(conn, sk);
  639. else
  640. l2cap_send_conn_req(sk);
  641. }
  642. } else {
  643. struct l2cap_info_req req;
  644. req.type = cpu_to_le16(L2CAP_IT_FEAT_MASK);
  645. conn->info_state |= L2CAP_INFO_FEAT_MASK_REQ_SENT;
  646. conn->info_ident = l2cap_get_ident(conn);
  647. mod_timer(&conn->info_timer, jiffies +
  648. msecs_to_jiffies(L2CAP_INFO_TIMEOUT));
  649. l2cap_send_cmd(conn, conn->info_ident,
  650. L2CAP_INFO_REQ, sizeof(req), &req);
  651. }
  652. }
  653. static inline int l2cap_mode_supported(__u8 mode, __u32 feat_mask)
  654. {
  655. u32 local_feat_mask = l2cap_feat_mask;
  656. if (!disable_ertm)
  657. local_feat_mask |= L2CAP_FEAT_ERTM | L2CAP_FEAT_STREAMING;
  658. switch (mode) {
  659. case L2CAP_MODE_ERTM:
  660. return L2CAP_FEAT_ERTM & feat_mask & local_feat_mask;
  661. case L2CAP_MODE_STREAMING:
  662. return L2CAP_FEAT_STREAMING & feat_mask & local_feat_mask;
  663. default:
  664. return 0x00;
  665. }
  666. }
  667. void l2cap_send_disconn_req(struct l2cap_conn *conn, struct sock *sk, int err)
  668. {
  669. struct l2cap_disconn_req req;
  670. if (!conn)
  671. return;
  672. sk->sk_send_head = NULL;
  673. skb_queue_purge(TX_QUEUE(sk));
  674. if (l2cap_pi(sk)->mode == L2CAP_MODE_ERTM) {
  675. skb_queue_purge(SREJ_QUEUE(sk));
  676. __cancel_delayed_work(&l2cap_pi(sk)->ack_work);
  677. __cancel_delayed_work(&l2cap_pi(sk)->retrans_work);
  678. __cancel_delayed_work(&l2cap_pi(sk)->monitor_work);
  679. }
  680. req.dcid = cpu_to_le16(l2cap_pi(sk)->dcid);
  681. req.scid = cpu_to_le16(l2cap_pi(sk)->scid);
  682. l2cap_send_cmd(conn, l2cap_get_ident(conn),
  683. L2CAP_DISCONN_REQ, sizeof(req), &req);
  684. sk->sk_state = BT_DISCONN;
  685. sk->sk_err = err;
  686. }
  687. /* ---- L2CAP connections ---- */
  688. static void l2cap_conn_start(struct l2cap_conn *conn)
  689. {
  690. struct l2cap_chan_list *l = &conn->chan_list;
  691. struct sock_del_list del, *tmp1, *tmp2;
  692. struct sock *sk;
  693. BT_DBG("conn %p", conn);
  694. INIT_LIST_HEAD(&del.list);
  695. read_lock(&l->lock);
  696. for (sk = l->head; sk; sk = l2cap_pi(sk)->next_c) {
  697. bh_lock_sock(sk);
  698. if (sk->sk_type != SOCK_SEQPACKET &&
  699. sk->sk_type != SOCK_STREAM) {
  700. bh_unlock_sock(sk);
  701. continue;
  702. }
  703. if (sk->sk_state == BT_CONNECT) {
  704. if (!l2cap_check_security(sk) ||
  705. !__l2cap_no_conn_pending(sk)) {
  706. bh_unlock_sock(sk);
  707. continue;
  708. }
  709. if (!l2cap_mode_supported(l2cap_pi(sk)->mode,
  710. conn->feat_mask)
  711. && l2cap_pi(sk)->conf_state &
  712. L2CAP_CONF_STATE2_DEVICE) {
  713. tmp1 = kzalloc(sizeof(struct sock_del_list),
  714. GFP_ATOMIC);
  715. tmp1->sk = sk;
  716. list_add_tail(&tmp1->list, &del.list);
  717. bh_unlock_sock(sk);
  718. continue;
  719. }
  720. l2cap_pi(sk)->conf_state |= L2CAP_CONF_CONNECT_PEND;
  721. if (l2cap_pi(sk)->amp_pref ==
  722. BT_AMP_POLICY_PREFER_AMP &&
  723. enable_hs &&
  724. conn->fc_mask & L2CAP_FC_A2MP)
  725. amp_create_physical(conn, sk);
  726. else
  727. l2cap_send_conn_req(sk);
  728. } else if (sk->sk_state == BT_CONNECT2) {
  729. struct l2cap_conn_rsp rsp;
  730. char buf[128];
  731. rsp.scid = cpu_to_le16(l2cap_pi(sk)->dcid);
  732. rsp.dcid = cpu_to_le16(l2cap_pi(sk)->scid);
  733. if (l2cap_check_security(sk)) {
  734. if (bt_sk(sk)->defer_setup) {
  735. struct sock *parent = bt_sk(sk)->parent;
  736. rsp.result = cpu_to_le16(L2CAP_CR_PEND);
  737. rsp.status = cpu_to_le16(L2CAP_CS_AUTHOR_PEND);
  738. if (parent)
  739. parent->sk_data_ready(parent, 0);
  740. } else {
  741. sk->sk_state = BT_CONFIG;
  742. rsp.result = cpu_to_le16(L2CAP_CR_SUCCESS);
  743. rsp.status = cpu_to_le16(L2CAP_CS_NO_INFO);
  744. }
  745. } else {
  746. rsp.result = cpu_to_le16(L2CAP_CR_PEND);
  747. rsp.status = cpu_to_le16(L2CAP_CS_AUTHEN_PEND);
  748. }
  749. if (rsp.result == cpu_to_le16(L2CAP_CR_SUCCESS) &&
  750. l2cap_pi(sk)->amp_id) {
  751. amp_accept_physical(conn,
  752. l2cap_pi(sk)->amp_id, sk);
  753. bh_unlock_sock(sk);
  754. continue;
  755. }
  756. l2cap_send_cmd(conn, l2cap_pi(sk)->ident,
  757. L2CAP_CONN_RSP, sizeof(rsp), &rsp);
  758. if (l2cap_pi(sk)->conf_state & L2CAP_CONF_REQ_SENT ||
  759. rsp.result != L2CAP_CR_SUCCESS) {
  760. bh_unlock_sock(sk);
  761. continue;
  762. }
  763. l2cap_pi(sk)->conf_state |= L2CAP_CONF_REQ_SENT;
  764. l2cap_send_cmd(conn, l2cap_get_ident(conn), L2CAP_CONF_REQ,
  765. l2cap_build_conf_req(sk, buf, sizeof(buf)), buf);
  766. l2cap_pi(sk)->num_conf_req++;
  767. }
  768. bh_unlock_sock(sk);
  769. }
  770. read_unlock(&l->lock);
  771. list_for_each_entry_safe(tmp1, tmp2, &del.list, list) {
  772. bh_lock_sock(tmp1->sk);
  773. __l2cap_sock_close(tmp1->sk, ECONNRESET);
  774. bh_unlock_sock(tmp1->sk);
  775. list_del(&tmp1->list);
  776. kfree(tmp1);
  777. }
  778. }
  779. /* Find socket with fixed cid with given source and destination bdaddrs.
  780. * Direction of the req/rsp must match.
  781. */
  782. struct sock *l2cap_find_sock_by_fixed_cid_and_dir(__le16 cid, bdaddr_t *src,
  783. bdaddr_t *dst, int incoming)
  784. {
  785. struct sock *sk = NULL, *sk1 = NULL;
  786. struct hlist_node *node;
  787. BT_DBG(" %d", incoming);
  788. read_lock(&l2cap_sk_list.lock);
  789. sk_for_each(sk, node, &l2cap_sk_list.head) {
  790. if (incoming && !l2cap_pi(sk)->incoming)
  791. continue;
  792. if (!incoming && l2cap_pi(sk)->incoming)
  793. continue;
  794. if (l2cap_pi(sk)->scid == cid && !bacmp(&bt_sk(sk)->dst, dst)) {
  795. /* Exact match. */
  796. if (!bacmp(&bt_sk(sk)->src, src))
  797. break;
  798. /* Closest match */
  799. if (!bacmp(&bt_sk(sk)->src, BDADDR_ANY))
  800. sk1 = sk;
  801. }
  802. }
  803. read_unlock(&l2cap_sk_list.lock);
  804. return node ? sk : sk1;
  805. }
  806. /* Find socket with cid and source bdaddr.
  807. * Returns closest match, locked.
  808. */
  809. static struct sock *l2cap_get_sock_by_scid(int state, __le16 cid, bdaddr_t *src)
  810. {
  811. struct sock *sk = NULL, *sk1 = NULL;
  812. struct hlist_node *node;
  813. read_lock(&l2cap_sk_list.lock);
  814. sk_for_each(sk, node, &l2cap_sk_list.head) {
  815. if (state && sk->sk_state != state)
  816. continue;
  817. if (l2cap_pi(sk)->scid == cid) {
  818. /* Exact match. */
  819. if (!bacmp(&bt_sk(sk)->src, src))
  820. break;
  821. /* Closest match */
  822. if (!bacmp(&bt_sk(sk)->src, BDADDR_ANY))
  823. sk1 = sk;
  824. }
  825. }
  826. read_unlock(&l2cap_sk_list.lock);
  827. return node ? sk : sk1;
  828. }
  829. static void l2cap_le_conn_ready(struct l2cap_conn *conn)
  830. {
  831. struct l2cap_chan_list *list = &conn->chan_list;
  832. struct sock *parent, *uninitialized_var(sk);
  833. BT_DBG("");
  834. /* Check if we have socket listening on cid */
  835. parent = l2cap_get_sock_by_scid(BT_LISTEN, L2CAP_CID_LE_DATA,
  836. conn->src);
  837. if (!parent)
  838. return;
  839. bh_lock_sock(parent);
  840. /* Check for backlog size */
  841. if (sk_acceptq_is_full(parent)) {
  842. BT_DBG("backlog full %d", parent->sk_ack_backlog);
  843. goto clean;
  844. }
  845. sk = l2cap_sock_alloc(sock_net(parent), NULL, BTPROTO_L2CAP, GFP_ATOMIC);
  846. if (!sk)
  847. goto clean;
  848. write_lock_bh(&list->lock);
  849. hci_conn_hold(conn->hcon);
  850. conn->hcon->disc_timeout = HCI_DISCONN_TIMEOUT;
  851. l2cap_sock_init(sk, parent);
  852. bacpy(&bt_sk(sk)->src, conn->src);
  853. bacpy(&bt_sk(sk)->dst, conn->dst);
  854. l2cap_pi(sk)->incoming = 1;
  855. bt_accept_enqueue(parent, sk);
  856. __l2cap_chan_add(conn, sk);
  857. sk->sk_state = BT_CONNECTED;
  858. parent->sk_data_ready(parent, 0);
  859. write_unlock_bh(&list->lock);
  860. clean:
  861. bh_unlock_sock(parent);
  862. }
  863. static void l2cap_conn_ready(struct l2cap_conn *conn)
  864. {
  865. struct l2cap_chan_list *l = &conn->chan_list;
  866. struct sock *sk;
  867. struct hci_conn *hcon = conn->hcon;
  868. BT_DBG("conn %p", conn);
  869. if (!hcon->out && hcon->type == LE_LINK)
  870. l2cap_le_conn_ready(conn);
  871. if (hcon->out && hcon->type == LE_LINK)
  872. smp_conn_security(hcon, hcon->pending_sec_level);
  873. read_lock(&l->lock);
  874. if (l->head) {
  875. for (sk = l->head; sk; sk = l2cap_pi(sk)->next_c) {
  876. bh_lock_sock(sk);
  877. if (hcon->type == LE_LINK) {
  878. u8 sec_level = l2cap_pi(sk)->sec_level;
  879. u8 pending_sec = hcon->pending_sec_level;
  880. if (pending_sec > sec_level)
  881. sec_level = pending_sec;
  882. if (smp_conn_security(hcon, sec_level))
  883. l2cap_chan_ready(sk);
  884. hci_conn_put(conn->hcon);
  885. } else if (sk->sk_type != SOCK_SEQPACKET &&
  886. sk->sk_type != SOCK_STREAM) {
  887. l2cap_sock_clear_timer(sk);
  888. sk->sk_state = BT_CONNECTED;
  889. sk->sk_state_change(sk);
  890. } else if (sk->sk_state == BT_CONNECT)
  891. l2cap_do_start(sk);
  892. bh_unlock_sock(sk);
  893. }
  894. } else if (conn->hcon->type == LE_LINK) {
  895. smp_conn_security(hcon, BT_SECURITY_HIGH);
  896. }
  897. read_unlock(&l->lock);
  898. if (conn->hcon->out && conn->hcon->type == LE_LINK)
  899. l2cap_le_conn_ready(conn);
  900. }
  901. /* Notify sockets that we cannot guaranty reliability anymore */
  902. static void l2cap_conn_unreliable(struct l2cap_conn *conn, int err)
  903. {
  904. struct l2cap_chan_list *l = &conn->chan_list;
  905. struct sock *sk;
  906. BT_DBG("conn %p", conn);
  907. read_lock(&l->lock);
  908. for (sk = l->head; sk; sk = l2cap_pi(sk)->next_c) {
  909. if (l2cap_pi(sk)->force_reliable)
  910. sk->sk_err = err;
  911. }
  912. read_unlock(&l->lock);
  913. }
  914. static void l2cap_info_timeout(unsigned long arg)
  915. {
  916. struct l2cap_conn *conn = (void *) arg;
  917. conn->info_state |= L2CAP_INFO_FEAT_MASK_REQ_DONE;
  918. conn->info_ident = 0;
  919. l2cap_conn_start(conn);
  920. }
  921. static struct l2cap_conn *l2cap_conn_add(struct hci_conn *hcon, u8 status)
  922. {
  923. struct l2cap_conn *conn = hcon->l2cap_data;
  924. if (conn || status)
  925. return conn;
  926. conn = kzalloc(sizeof(struct l2cap_conn), GFP_ATOMIC);
  927. if (!conn)
  928. return NULL;
  929. hcon->l2cap_data = conn;
  930. conn->hcon = hcon;
  931. BT_DBG("hcon %p conn %p", hcon, conn);
  932. if (hcon->hdev->le_mtu && hcon->type == LE_LINK)
  933. conn->mtu = hcon->hdev->le_mtu;
  934. else
  935. conn->mtu = hcon->hdev->acl_mtu;
  936. conn->src = &hcon->hdev->bdaddr;
  937. conn->dst = &hcon->dst;
  938. conn->feat_mask = 0;
  939. spin_lock_init(&conn->lock);
  940. rwlock_init(&conn->chan_list.lock);
  941. if (hcon->type == LE_LINK)
  942. setup_timer(&hcon->smp_timer, smp_timeout,
  943. (unsigned long) conn);
  944. else
  945. setup_timer(&conn->info_timer, l2cap_info_timeout,
  946. (unsigned long) conn);
  947. conn->disc_reason = 0x13;
  948. return conn;
  949. }
  950. static void l2cap_conn_del(struct hci_conn *hcon, int err, u8 is_process)
  951. {
  952. struct l2cap_conn *conn = hcon->l2cap_data;
  953. struct sock *sk;
  954. struct sock *next;
  955. if (!conn)
  956. return;
  957. BT_DBG("hcon %p conn %p, err %d", hcon, conn, err);
  958. if ((conn->hcon == hcon) && (conn->rx_skb))
  959. kfree_skb(conn->rx_skb);
  960. BT_DBG("conn->hcon %p", conn->hcon);
  961. /* Kill channels */
  962. for (sk = conn->chan_list.head; sk; ) {
  963. BT_DBG("ampcon %p", l2cap_pi(sk)->ampcon);
  964. if ((conn->hcon == hcon) || (l2cap_pi(sk)->ampcon == hcon)) {
  965. next = l2cap_pi(sk)->next_c;
  966. if (is_process)
  967. lock_sock(sk);
  968. else
  969. bh_lock_sock(sk);
  970. l2cap_chan_del(sk, err);
  971. if (is_process)
  972. release_sock(sk);
  973. else
  974. bh_unlock_sock(sk);
  975. l2cap_sock_kill(sk);
  976. sk = next;
  977. } else
  978. sk = l2cap_pi(sk)->next_c;
  979. }
  980. if (conn->hcon == hcon) {
  981. if (conn->info_state & L2CAP_INFO_FEAT_MASK_REQ_SENT)
  982. del_timer_sync(&conn->info_timer);
  983. hcon->l2cap_data = NULL;
  984. kfree(conn);
  985. }
  986. att_chn_params.conn = NULL;
  987. BT_DBG("att_chn_params.conn set to NULL");
  988. }
  989. static inline void l2cap_chan_add(struct l2cap_conn *conn, struct sock *sk)
  990. {
  991. struct l2cap_chan_list *l = &conn->chan_list;
  992. write_lock_bh(&l->lock);
  993. __l2cap_chan_add(conn, sk);
  994. write_unlock_bh(&l->lock);
  995. }
  996. /* ---- Socket interface ---- */
  997. /* Find socket with psm and source bdaddr.
  998. * Returns closest match.
  999. */
  1000. static struct sock *l2cap_get_sock_by_psm(int state, __le16 psm, bdaddr_t *src)
  1001. {
  1002. struct sock *sk = NULL, *sk1 = NULL;
  1003. struct hlist_node *node;
  1004. read_lock(&l2cap_sk_list.lock);
  1005. sk_for_each(sk, node, &l2cap_sk_list.head) {
  1006. if (state && sk->sk_state != state)
  1007. continue;
  1008. if (l2cap_pi(sk)->psm == psm) {
  1009. /* Exact match. */
  1010. if (!bacmp(&bt_sk(sk)->src, src))
  1011. break;
  1012. /* Closest match */
  1013. if (!bacmp(&bt_sk(sk)->src, BDADDR_ANY))
  1014. sk1 = sk;
  1015. }
  1016. }
  1017. read_unlock(&l2cap_sk_list.lock);
  1018. return node ? sk : sk1;
  1019. }
  1020. int l2cap_do_connect(struct sock *sk)
  1021. {
  1022. bdaddr_t *src = &bt_sk(sk)->src;
  1023. bdaddr_t *dst = &bt_sk(sk)->dst;
  1024. struct l2cap_conn *conn;
  1025. struct hci_conn *hcon;
  1026. struct hci_dev *hdev;
  1027. __u8 auth_type;
  1028. int err;
  1029. BT_DBG("%s -> %s psm 0x%2.2x", batostr(src), batostr(dst),
  1030. l2cap_pi(sk)->psm);
  1031. hdev = hci_get_route(dst, src);
  1032. if (!hdev)
  1033. return -EHOSTUNREACH;
  1034. hci_dev_lock_bh(hdev);
  1035. auth_type = l2cap_get_auth_type(sk);
  1036. if (l2cap_pi(sk)->fixed_channel) {
  1037. /* Fixed channels piggyback on existing ACL connections */
  1038. hcon = hci_conn_hash_lookup_ba(hdev, ACL_LINK, dst);
  1039. if (!hcon || !hcon->l2cap_data) {
  1040. err = -ENOTCONN;
  1041. goto done;
  1042. }
  1043. conn = hcon->l2cap_data;
  1044. } else {
  1045. if (l2cap_pi(sk)->dcid == L2CAP_CID_LE_DATA)
  1046. hcon = hci_le_connect(hdev, 0, dst,
  1047. l2cap_pi(sk)->sec_level, auth_type,
  1048. &bt_sk(sk)->le_params);
  1049. else
  1050. hcon = hci_connect(hdev, ACL_LINK, 0, dst,
  1051. l2cap_pi(sk)->sec_level, auth_type);
  1052. if (IS_ERR(hcon)) {
  1053. err = PTR_ERR(hcon);
  1054. goto done;
  1055. }
  1056. conn = l2cap_conn_add(hcon, 0);
  1057. if (!conn) {
  1058. hci_conn_put(hcon);
  1059. err = -ENOMEM;
  1060. goto done;
  1061. }
  1062. }
  1063. /* Update source addr of the socket */
  1064. bacpy(src, conn->src);
  1065. l2cap_chan_add(conn, sk);
  1066. if ((l2cap_pi(sk)->fixed_channel) ||
  1067. (l2cap_pi(sk)->dcid == L2CAP_CID_LE_DATA &&
  1068. hcon->state == BT_CONNECTED)) {
  1069. sk->sk_state = BT_CONNECTED;
  1070. sk->sk_state_change(sk);
  1071. } else {
  1072. sk->sk_state = BT_CONNECT;
  1073. /* If we have valid LE Params, let timeout override default */
  1074. if (l2cap_pi(sk)->dcid == L2CAP_CID_LE_DATA &&
  1075. l2cap_sock_le_params_valid(&bt_sk(sk)->le_params)) {
  1076. u16 timeout = bt_sk(sk)->le_params.conn_timeout;
  1077. if (timeout)
  1078. l2cap_sock_set_timer(sk,
  1079. msecs_to_jiffies(timeout*1000));
  1080. } else
  1081. l2cap_sock_set_timer(sk, sk->sk_sndtimeo);
  1082. sk->sk_state_change(sk);
  1083. if (hcon->state == BT_CONNECTED) {
  1084. if (sk->sk_type != SOCK_SEQPACKET &&
  1085. sk->sk_type != SOCK_STREAM) {
  1086. l2cap_sock_clear_timer(sk);
  1087. if (l2cap_check_security(sk)) {
  1088. sk->sk_state = BT_CONNECTED;
  1089. sk->sk_state_change(sk);
  1090. }
  1091. } else
  1092. l2cap_do_start(sk);
  1093. }
  1094. }
  1095. err = 0;
  1096. done:
  1097. hci_dev_unlock_bh(hdev);
  1098. hci_dev_put(hdev);
  1099. return err;
  1100. }
  1101. int __l2cap_wait_ack(struct sock *sk)
  1102. {
  1103. DECLARE_WAITQUEUE(wait, current);
  1104. int err = 0;
  1105. int timeo = HZ/5;
  1106. add_wait_queue(sk_sleep(sk), &wait);
  1107. while (l2cap_pi(sk)->unacked_frames > 0 && l2cap_pi(sk)->conn &&
  1108. atomic_read(&l2cap_pi(sk)->ertm_queued)) {
  1109. set_current_state(TASK_INTERRUPTIBLE);
  1110. if (!timeo)
  1111. timeo = HZ/5;
  1112. if (signal_pending(current)) {
  1113. err = sock_intr_errno(timeo);
  1114. break;
  1115. }
  1116. release_sock(sk);
  1117. timeo = schedule_timeout(timeo);
  1118. lock_sock(sk);
  1119. err = sock_error(sk);
  1120. if (err)
  1121. break;
  1122. }
  1123. set_current_state(TASK_RUNNING);
  1124. remove_wait_queue(sk_sleep(sk), &wait);
  1125. return err;
  1126. }
  1127. static void l2cap_ertm_tx_worker(struct work_struct *work)
  1128. {
  1129. struct l2cap_pinfo *pi =
  1130. container_of(work, struct l2cap_pinfo, tx_work);
  1131. struct sock *sk = (struct sock *)pi;
  1132. BT_DBG("%p", pi);
  1133. lock_sock(sk);
  1134. l2cap_ertm_send(sk);
  1135. release_sock(sk);
  1136. sock_put(sk);
  1137. }
  1138. static void l2cap_skb_destructor(struct sk_buff *skb)
  1139. {
  1140. struct sock *sk = skb->sk;
  1141. int queued;
  1142. int keep_sk = 0;
  1143. queued = atomic_sub_return(1, &l2cap_pi(sk)->ertm_queued);
  1144. if (queued < L2CAP_MIN_ERTM_QUEUED)
  1145. keep_sk = queue_work(_l2cap_wq, &l2cap_pi(sk)->tx_work);
  1146. if (!keep_sk)
  1147. sock_put(sk);
  1148. }
  1149. void l2cap_do_send(struct sock *sk, struct sk_buff *skb)
  1150. {
  1151. struct l2cap_pinfo *pi = l2cap_pi(sk);
  1152. BT_DBG("sk %p, skb %p len %d", sk, skb, skb->len);
  1153. if (pi->ampcon && (pi->amp_move_state == L2CAP_AMP_STATE_STABLE ||
  1154. pi->amp_move_state == L2CAP_AMP_STATE_WAIT_PREPARE)) {
  1155. BT_DBG("Sending on AMP connection %p %p",
  1156. pi->ampcon, pi->ampchan);
  1157. if (pi->ampchan)
  1158. hci_send_acl(pi->ampcon, pi->ampchan, skb,
  1159. ACL_COMPLETE);
  1160. else
  1161. kfree_skb(skb);
  1162. } else {
  1163. u16 flags;
  1164. if (!(pi->conn)) {
  1165. kfree_skb(skb);
  1166. return;
  1167. }
  1168. bt_cb(skb)->force_active = pi->force_active;
  1169. BT_DBG("Sending on BR/EDR connection %p", pi->conn->hcon);
  1170. if (lmp_no_flush_capable(pi->conn->hcon->hdev) &&
  1171. !l2cap_pi(sk)->flushable)
  1172. flags = ACL_START_NO_FLUSH;
  1173. else
  1174. flags = ACL_START;
  1175. hci_send_acl(pi->conn->hcon, NULL, skb, flags);
  1176. }
  1177. }
  1178. int l2cap_ertm_send(struct sock *sk)
  1179. {
  1180. struct sk_buff *skb, *tx_skb;
  1181. struct l2cap_pinfo *pi = l2cap_pi(sk);
  1182. struct bt_l2cap_control *control;
  1183. int sent = 0;
  1184. BT_DBG("sk %p", sk);
  1185. if (sk->sk_state != BT_CONNECTED)
  1186. return -ENOTCONN;
  1187. if (pi->conn_state & L2CAP_CONN_REMOTE_BUSY)
  1188. return 0;
  1189. if (pi->amp_move_state != L2CAP_AMP_STATE_STABLE &&
  1190. pi->amp_move_state != L2CAP_AMP_STATE_WAIT_PREPARE)
  1191. return 0;
  1192. while (sk->sk_send_head && (pi->unacked_frames < pi->remote_tx_win) &&
  1193. atomic_read(&pi->ertm_queued) < L2CAP_MAX_ERTM_QUEUED &&
  1194. (pi->tx_state == L2CAP_ERTM_TX_STATE_XMIT)) {
  1195. skb = sk->sk_send_head;
  1196. bt_cb(skb)->retries = 1;
  1197. control = &bt_cb(skb)->control;
  1198. if (pi->conn_state & L2CAP_CONN_SEND_FBIT) {
  1199. control->final = 1;
  1200. pi->conn_state &= ~L2CAP_CONN_SEND_FBIT;
  1201. }
  1202. control->reqseq = pi->buffer_seq;
  1203. pi->last_acked_seq = pi->buffer_seq;
  1204. control->txseq = pi->next_tx_seq;
  1205. if (pi->extended_control) {
  1206. put_unaligned_le32(__pack_extended_control(control),
  1207. skb->data + L2CAP_HDR_SIZE);
  1208. } else {
  1209. put_unaligned_le16(__pack_enhanced_control(control),
  1210. skb->data + L2CAP_HDR_SIZE);
  1211. }
  1212. if (pi->fcs == L2CAP_FCS_CRC16)
  1213. apply_fcs(skb);
  1214. /* Clone after data has been modified. Data is assumed to be
  1215. read-only (for locking purposes) on cloned sk_buffs.
  1216. */
  1217. tx_skb = skb_clone(skb, GFP_ATOMIC);
  1218. if (!tx_skb)
  1219. break;
  1220. sock_hold(sk);
  1221. tx_skb->sk = sk;
  1222. tx_skb->destructor = l2cap_skb_destructor;
  1223. atomic_inc(&pi->ertm_queued);
  1224. l2cap_ertm_start_retrans_timer(pi);
  1225. pi->next_tx_seq = __next_seq(pi->next_tx_seq, pi);
  1226. pi->unacked_frames += 1;
  1227. pi->frames_sent += 1;
  1228. sent += 1;
  1229. if (skb_queue_is_last(TX_QUEUE(sk), skb))
  1230. sk->sk_send_head = NULL;
  1231. else
  1232. sk->sk_send_head = skb_queue_next(TX_QUEUE(sk), skb);
  1233. l2cap_do_send(sk, tx_skb);
  1234. BT_DBG("Sent txseq %d", (int)control->txseq);
  1235. }
  1236. BT_DBG("Sent %d, %d unacked, %d in ERTM queue, %d in HCI queue", sent,
  1237. (int) pi->unacked_frames, skb_queue_len(TX_QUEUE(sk)),
  1238. atomic_read(&pi->ertm_queued));
  1239. return sent;
  1240. }
  1241. int l2cap_strm_tx(struct sock *sk, struct sk_buff_head *skbs)
  1242. {
  1243. struct sk_buff *skb;
  1244. struct l2cap_pinfo *pi = l2cap_pi(sk);
  1245. struct bt_l2cap_control *control;
  1246. int sent = 0;
  1247. BT_DBG("sk %p, skbs %p", sk, skbs);
  1248. if (sk->sk_state != BT_CONNECTED)
  1249. return -ENOTCONN;
  1250. if (pi->amp_move_state != L2CAP_AMP_STATE_STABLE &&
  1251. pi->amp_move_state != L2CAP_AMP_STATE_WAIT_PREPARE)
  1252. return 0;
  1253. skb_queue_splice_tail_init(skbs, TX_QUEUE(sk));
  1254. BT_DBG("skb queue empty 0x%2.2x", skb_queue_empty(TX_QUEUE(sk)));
  1255. while (!skb_queue_empty(TX_QUEUE(sk))) {
  1256. skb = skb_dequeue(TX_QUEUE(sk));
  1257. BT_DBG("skb %p", skb);
  1258. bt_cb(skb)->retries = 1;
  1259. control = &bt_cb(skb)->control;
  1260. BT_DBG("control %p", control);
  1261. control->reqseq = 0;
  1262. control->txseq = pi->next_tx_seq;
  1263. if (pi->extended_control) {
  1264. put_unaligned_le32(__pack_extended_control(control),
  1265. skb->data + L2CAP_HDR_SIZE);
  1266. } else {
  1267. put_unaligned_le16(__pack_enhanced_control(control),
  1268. skb->data + L2CAP_HDR_SIZE);
  1269. }
  1270. if (pi->fcs == L2CAP_FCS_CRC16)
  1271. apply_fcs(skb);
  1272. l2cap_do_send(sk, skb);
  1273. BT_DBG("Sent txseq %d", (int)control->txseq);
  1274. pi->next_tx_seq = __next_seq(pi->next_tx_seq, pi);
  1275. pi->frames_sent += 1;
  1276. sent += 1;
  1277. }
  1278. BT_DBG("Sent %d", sent);
  1279. return 0;
  1280. }
  1281. static int memcpy_fromkvec(unsigned char *kdata, struct kvec *iv, int len)
  1282. {
  1283. while (len > 0) {
  1284. if (iv->iov_len) {
  1285. int copy = min_t(unsigned int, len, iv->iov_len);
  1286. memcpy(kdata, iv->iov_base, copy);
  1287. len -= copy;
  1288. kdata += copy;
  1289. iv->iov_base += copy;
  1290. iv->iov_len -= copy;
  1291. }
  1292. iv++;
  1293. }
  1294. return 0;
  1295. }
  1296. static inline int l2cap_skbuff_fromiovec(struct sock *sk, struct msghdr *msg,
  1297. int len, int count, struct sk_buff *skb,
  1298. int reseg)
  1299. {
  1300. struct l2cap_conn *conn = l2cap_pi(sk)->conn;
  1301. struct sk_buff **frag;
  1302. struct sk_buff *final;
  1303. int err, sent = 0;
  1304. BT_DBG("sk %p, msg %p, len %d, count %d, skb %p", sk,
  1305. msg, (int)len, (int)count, skb);
  1306. if (!conn)
  1307. return -ENOTCONN;
  1308. /* When resegmenting, data is copied from kernel space */
  1309. if (reseg) {
  1310. err = memcpy_fromkvec(skb_put(skb, count),
  1311. (struct kvec *) msg->msg_iov, count);
  1312. } else {
  1313. err = memcpy_fromiovec(skb_put(skb, count), msg->msg_iov,
  1314. count);
  1315. }
  1316. if (err)
  1317. return -EFAULT;
  1318. sent += count;
  1319. len -= count;
  1320. final = skb;
  1321. /* Continuation fragments (no L2CAP header) */
  1322. frag = &skb_shinfo(skb)->frag_list;
  1323. while (len) {
  1324. int skblen;
  1325. count = min_t(unsigned int, conn->mtu, len);
  1326. /* Add room for the FCS if it fits */
  1327. if (bt_cb(skb)->control.fcs == L2CAP_FCS_CRC16 &&
  1328. len + L2CAP_FCS_SIZE <= conn->mtu)
  1329. skblen = count + L2CAP_FCS_SIZE;
  1330. else
  1331. skblen = count;
  1332. /* Don't use bt_skb_send_alloc() while resegmenting, since
  1333. * it is not ok to block.
  1334. */
  1335. if (reseg) {
  1336. *frag = bt_skb_alloc(skblen, GFP_ATOMIC);
  1337. if (*frag)
  1338. skb_set_owner_w(*frag, sk);
  1339. } else {
  1340. *frag = bt_skb_send_alloc(sk, skblen,
  1341. msg->msg_flags & MSG_DONTWAIT, &err);
  1342. }
  1343. if (!*frag)
  1344. return -EFAULT;
  1345. /* When resegmenting, data is copied from kernel space */
  1346. if (reseg) {
  1347. err = memcpy_fromkvec(skb_put(*frag, count),
  1348. (struct kvec *) msg->msg_iov,
  1349. count);
  1350. } else {
  1351. err = memcpy_fromiovec(skb_put(*frag, count),
  1352. msg->msg_iov, count);
  1353. }
  1354. if (err)
  1355. return -EFAULT;
  1356. sent += count;
  1357. len -= count;
  1358. final = *frag;
  1359. frag = &(*frag)->next;
  1360. }
  1361. if (bt_cb(skb)->control.fcs == L2CAP_FCS_CRC16) {
  1362. if (skb_tailroom(final) < L2CAP_FCS_SIZE) {
  1363. if (reseg) {
  1364. *frag = bt_skb_alloc(L2CAP_FCS_SIZE,
  1365. GFP_ATOMIC);
  1366. if (*frag)
  1367. skb_set_owner_w(*frag, sk);
  1368. } else {
  1369. *frag = bt_skb_send_alloc(sk, L2CAP_FCS_SIZE,
  1370. msg->msg_flags & MSG_DONTWAIT,
  1371. &err);
  1372. }
  1373. if (!*frag)
  1374. return -EFAULT;
  1375. final = *frag;
  1376. }
  1377. skb_put(final, L2CAP_FCS_SIZE);
  1378. }
  1379. return sent;
  1380. }
  1381. struct sk_buff *l2cap_create_connless_pdu(struct sock *sk, struct msghdr *msg, size_t len)
  1382. {
  1383. struct l2cap_conn *conn = l2cap_pi(sk)->conn;
  1384. struct sk_buff *skb;
  1385. int err, count, hlen = L2CAP_HDR_SIZE + 2;
  1386. struct l2cap_hdr *lh;
  1387. BT_DBG("sk %p len %d", sk, (int)len);
  1388. count = min_t(unsigned int, (conn->mtu - hlen), len);
  1389. skb = bt_skb_send_alloc(sk, count + hlen,
  1390. msg->msg_flags & MSG_DONTWAIT, &err);
  1391. if (!skb)
  1392. return ERR_PTR(err);
  1393. /* Create L2CAP header */
  1394. lh = (struct l2cap_hdr *) skb_put(skb, L2CAP_HDR_SIZE);
  1395. lh->cid = cpu_to_le16(l2cap_pi(sk)->dcid);
  1396. lh->len = cpu_to_le16(len + (hlen - L2CAP_HDR_SIZE));
  1397. put_unaligned_le16(l2cap_pi(sk)->psm, skb_put(skb, 2));
  1398. err = l2cap_skbuff_fromiovec(sk, msg, len, count, skb, 0);
  1399. if (unlikely(err < 0)) {
  1400. kfree_skb(skb);
  1401. return ERR_PTR(err);
  1402. }
  1403. return skb;
  1404. }
  1405. struct sk_buff *l2cap_create_basic_pdu(struct sock *sk, struct msghdr *msg, size_t len)
  1406. {
  1407. struct l2cap_conn *conn = l2cap_pi(sk)->conn;
  1408. struct sk_buff *skb;
  1409. int err, count, hlen = L2CAP_HDR_SIZE;
  1410. struct l2cap_hdr *lh;
  1411. BT_DBG("sk %p len %d", sk, (int)len);
  1412. count = min_t(unsigned int, (conn->mtu - hlen), len);
  1413. skb = bt_skb_send_alloc(sk, count + hlen,
  1414. msg->msg_flags & MSG_DONTWAIT, &err);
  1415. if (!skb)
  1416. return ERR_PTR(err);
  1417. /* Create L2CAP header */
  1418. lh = (struct l2cap_hdr *) skb_put(skb, L2CAP_HDR_SIZE);
  1419. lh->cid = cpu_to_le16(l2cap_pi(sk)->dcid);
  1420. lh->len = cpu_to_le16(len + (hlen - L2CAP_HDR_SIZE));
  1421. err = l2cap_skbuff_fromiovec(sk, msg, len, count, skb, 0);
  1422. if (unlikely(err < 0)) {
  1423. kfree_skb(skb);
  1424. return ERR_PTR(err);
  1425. }
  1426. return skb;
  1427. }
  1428. struct sk_buff *l2cap_create_iframe_pdu(struct sock *sk,
  1429. struct msghdr *msg, size_t len,
  1430. u16 sdulen, int reseg)
  1431. {
  1432. struct sk_buff *skb;
  1433. int err, count, hlen;
  1434. int reserve = 0;
  1435. struct l2cap_hdr *lh;
  1436. u8 fcs = l2cap_pi(sk)->fcs;
  1437. if (l2cap_pi(sk)->extended_control)
  1438. hlen = L2CAP_EXTENDED_HDR_SIZE;
  1439. else
  1440. hlen = L2CAP_ENHANCED_HDR_SIZE;
  1441. if (sdulen)
  1442. hlen += L2CAP_SDULEN_SIZE;
  1443. if (fcs == L2CAP_FCS_CRC16)
  1444. hlen += L2CAP_FCS_SIZE;
  1445. BT_DBG("sk %p, msg %p, len %d, sdulen %d, hlen %d",
  1446. sk, msg, (int)len, (int)sdulen, hlen);
  1447. count = min_t(unsigned int, (l2cap_pi(sk)->conn->mtu - hlen), len);
  1448. /* Allocate extra headroom for Qualcomm PAL. This is only
  1449. * necessary in two places (here and when creating sframes)
  1450. * because only unfragmented iframes and sframes are sent
  1451. * using AMP controllers.
  1452. */
  1453. if (l2cap_pi(sk)->ampcon &&
  1454. l2cap_pi(sk)->ampcon->hdev->manufacturer == 0x001d)
  1455. reserve = BT_SKB_RESERVE_80211;
  1456. /* Don't use bt_skb_send_alloc() while resegmenting, since
  1457. * it is not ok to block.
  1458. */
  1459. if (reseg) {
  1460. skb = bt_skb_alloc(count + hlen + reserve, GFP_ATOMIC);
  1461. if (skb)
  1462. skb_set_owner_w(skb, sk);
  1463. } else {
  1464. skb = bt_skb_send_alloc(sk, count + hlen + reserve,
  1465. msg->msg_flags & MSG_DONTWAIT, &err);
  1466. }
  1467. if (!skb)
  1468. return ERR_PTR(err);
  1469. if (reserve)
  1470. skb_reserve(skb, reserve);
  1471. bt_cb(skb)->control.fcs = fcs;
  1472. /* Create L2CAP header */
  1473. lh = (struct l2cap_hdr *) skb_put(skb, L2CAP_HDR_SIZE);
  1474. lh->cid = cpu_to_le16(l2cap_pi(sk)->dcid);
  1475. lh->len = cpu_to_le16(len + hlen - L2CAP_HDR_SIZE);
  1476. /* Control header is populated later */
  1477. if (l2cap_pi(sk)->extended_control)
  1478. put_unaligned_le32(0, skb_put(skb, 4));
  1479. else
  1480. put_unaligned_le16(0, skb_put(skb, 2));
  1481. if (sdulen)
  1482. put_unaligned_le16(sdulen, skb_put(skb, L2CAP_SDULEN_SIZE));
  1483. err = l2cap_skbuff_fromiovec(sk, msg, len, count, skb, reseg);
  1484. if (unlikely(err < 0)) {
  1485. BT_DBG("err %d", err);
  1486. kfree_skb(skb);
  1487. return ERR_PTR(err);
  1488. }
  1489. bt_cb(skb)->retries = 0;
  1490. return skb;
  1491. }
  1492. static void l2cap_ertm_process_reqseq(struct sock *sk, u16 reqseq)
  1493. {
  1494. struct l2cap_pinfo *pi;
  1495. struct sk_buff *acked_skb;
  1496. u16 ackseq;
  1497. BT_DBG("sk %p, reqseq %d", sk, (int) reqseq);
  1498. pi = l2cap_pi(sk);
  1499. if (pi->unacked_frames == 0 || reqseq == pi->expected_ack_seq)
  1500. return;
  1501. BT_DBG("expected_ack_seq %d, unacked_frames %d",
  1502. (int) pi->expected_ack_seq, (int) pi->unacked_frames);
  1503. for (ackseq = pi->expected_ack_seq; ackseq != reqseq;
  1504. ackseq = __next_seq(ackseq, pi)) {
  1505. acked_skb = l2cap_ertm_seq_in_queue(TX_QUEUE(sk), ackseq);
  1506. if (acked_skb) {
  1507. skb_unlink(acked_skb, TX_QUEUE(sk));
  1508. kfree_skb(acked_skb);
  1509. pi->unacked_frames--;
  1510. }
  1511. }
  1512. pi->expected_ack_seq = reqseq;
  1513. if (pi->unacked_frames == 0)
  1514. l2cap_ertm_stop_retrans_timer(pi);
  1515. BT_DBG("unacked_frames %d", (int) pi->unacked_frames);
  1516. }
  1517. static struct sk_buff *l2cap_create_sframe_pdu(struct sock *sk, u32 control)
  1518. {
  1519. struct sk_buff *skb;
  1520. int len;
  1521. int reserve = 0;
  1522. struct l2cap_hdr *lh;
  1523. if (l2cap_pi(sk)->extended_control)
  1524. len = L2CAP_EXTENDED_HDR_SIZE;
  1525. else
  1526. len = L2CAP_ENHANCED_HDR_SIZE;
  1527. if (l2cap_pi(sk)->fcs == L2CAP_FCS_CRC16)
  1528. len += L2CAP_FCS_SIZE;
  1529. /* Allocate extra headroom for Qualcomm PAL */
  1530. if (l2cap_pi(sk)->ampcon &&
  1531. l2cap_pi(sk)->ampcon->hdev->manufacturer == 0x001d)
  1532. reserve = BT_SKB_RESERVE_80211;
  1533. skb = bt_skb_alloc(len + reserve, GFP_ATOMIC);
  1534. if (!skb)
  1535. return ERR_PTR(-ENOMEM);
  1536. if (reserve)
  1537. skb_reserve(skb, reserve);
  1538. lh = (struct l2cap_hdr *) skb_put(skb, L2CAP_HDR_SIZE);
  1539. lh->cid = cpu_to_le16(l2cap_pi(sk)->dcid);
  1540. lh->len = cpu_to_le16(len - L2CAP_HDR_SIZE);
  1541. if (l2cap_pi(sk)->extended_control)
  1542. put_unaligned_le32(control, skb_put(skb, 4));
  1543. else
  1544. put_unaligned_le16(control, skb_put(skb, 2));
  1545. if (l2cap_pi(sk)->fcs == L2CAP_FCS_CRC16) {
  1546. u16 fcs = crc16(0, (u8 *) skb->data, skb->len);
  1547. put_unaligned_le16(fcs, skb_put(skb, L2CAP_FCS_SIZE));
  1548. }
  1549. return skb;
  1550. }
  1551. static void l2cap_ertm_send_sframe(struct sock *sk,
  1552. struct bt_l2cap_control *control)
  1553. {
  1554. struct l2cap_pinfo *pi;
  1555. struct sk_buff *skb;
  1556. u32 control_field;
  1557. BT_DBG("sk %p, control %p", sk, control);
  1558. if (control->frame_type != 's')
  1559. return;
  1560. pi = l2cap_pi(sk);
  1561. if (pi->amp_move_state != L2CAP_AMP_STATE_STABLE &&
  1562. pi->amp_move_state != L2CAP_AMP_STATE_WAIT_PREPARE &&
  1563. pi->amp_move_state != L2CAP_AMP_STATE_RESEGMENT) {
  1564. BT_DBG("AMP error - attempted S-Frame send during AMP move");
  1565. return;
  1566. }
  1567. if ((pi->conn_state & L2CAP_CONN_SEND_FBIT) && !control->poll) {
  1568. control->final = 1;
  1569. pi->conn_state &= ~L2CAP_CONN_SEND_FBIT;
  1570. }
  1571. if (control->super == L2CAP_SFRAME_RR)
  1572. pi->conn_state &= ~L2CAP_CONN_SENT_RNR;
  1573. else if (control->super == L2CAP_SFRAME_RNR)
  1574. pi->conn_state |= L2CAP_CONN_SENT_RNR;
  1575. if (control->super != L2CAP_SFRAME_SREJ) {
  1576. pi->last_acked_seq = control->reqseq;
  1577. l2cap_ertm_stop_ack_timer(pi);
  1578. }
  1579. BT_DBG("reqseq %d, final %d, poll %d, super %d", (int) control->reqseq,
  1580. (int) control->final, (int) control->poll,
  1581. (int) control->super);
  1582. if (pi->extended_control)
  1583. control_field = __pack_extended_control(control);
  1584. else
  1585. control_field = __pack_enhanced_control(control);
  1586. skb = l2cap_create_sframe_pdu(sk, control_field);
  1587. if (!IS_ERR(skb))
  1588. l2cap_do_send(sk, skb);
  1589. }
  1590. static void l2cap_ertm_send_ack(struct sock *sk)
  1591. {
  1592. struct l2cap_pinfo *pi = l2cap_pi(sk);
  1593. struct bt_l2cap_control control;
  1594. u16 frames_to_ack = __delta_seq(pi->buffer_seq, pi->last_acked_seq, pi);
  1595. int threshold;
  1596. BT_DBG("sk %p", sk);
  1597. BT_DBG("last_acked_seq %d, buffer_seq %d", (int)pi->last_acked_seq,
  1598. (int)pi->buffer_seq);
  1599. memset(&control, 0, sizeof(control));
  1600. control.frame_type = 's';
  1601. if ((pi->conn_state & L2CAP_CONN_LOCAL_BUSY) &&
  1602. pi->rx_state == L2CAP_ERTM_RX_STATE_RECV) {
  1603. l2cap_ertm_stop_ack_timer(pi);
  1604. control.super = L2CAP_SFRAME_RNR;
  1605. control.reqseq = pi->buffer_seq;
  1606. l2cap_ertm_send_sframe(sk, &control);
  1607. } else {
  1608. if (!(pi->conn_state & L2CAP_CONN_REMOTE_BUSY)) {
  1609. l2cap_ertm_send(sk);
  1610. /* If any i-frames were sent, they included an ack */
  1611. if (pi->buffer_seq == pi->last_acked_seq)
  1612. frames_to_ack = 0;
  1613. }
  1614. /* Ack now if the window is 3/4ths full.
  1615. * Calculate without mul or div
  1616. */
  1617. threshold = pi->ack_win;
  1618. threshold += threshold << 1;
  1619. threshold >>= 2;
  1620. BT_DBG("frames_to_ack %d, threshold %d", (int)frames_to_ack,
  1621. threshold);
  1622. if (frames_to_ack >= threshold) {
  1623. l2cap_ertm_stop_ack_timer(pi);
  1624. control.super = L2CAP_SFRAME_RR;
  1625. control.reqseq = pi->buffer_seq;
  1626. l2cap_ertm_send_sframe(sk, &control);
  1627. frames_to_ack = 0;
  1628. }
  1629. if (frames_to_ack)
  1630. l2cap_ertm_start_ack_timer(pi);
  1631. }
  1632. }
  1633. static void l2cap_ertm_send_rr_or_rnr(struct sock *sk, bool poll)
  1634. {
  1635. struct l2cap_pinfo *pi;
  1636. struct bt_l2cap_control control;
  1637. BT_DBG("sk %p, poll %d", sk, (int) poll);
  1638. pi = l2cap_pi(sk);
  1639. memset(&control, 0, sizeof(control));
  1640. control.frame_type = 's';
  1641. control.poll = poll;
  1642. if (pi->conn_state & L2CAP_CONN_LOCAL_BUSY)
  1643. control.super = L2CAP_SFRAME_RNR;
  1644. else
  1645. control.super = L2CAP_SFRAME_RR;
  1646. control.reqseq = pi->buffer_seq;
  1647. l2cap_ertm_send_sframe(sk, &control);
  1648. }
  1649. static void l2cap_ertm_send_i_or_rr_or_rnr(struct sock *sk)
  1650. {
  1651. struct l2cap_pinfo *pi;
  1652. struct bt_l2cap_control control;
  1653. BT_DBG("sk %p", sk);
  1654. pi = l2cap_pi(sk);
  1655. memset(&control, 0, sizeof(control));
  1656. control.frame_type = 's';
  1657. control.final = 1;
  1658. control.reqseq = pi->buffer_seq;
  1659. pi->conn_state |= L2CAP_CONN_SEND_FBIT;
  1660. if (pi->conn_state & L2CAP_CONN_LOCAL_BUSY) {
  1661. control.super = L2CAP_SFRAME_RNR;
  1662. l2cap_ertm_send_sframe(sk, &control);
  1663. }
  1664. if ((pi->conn_state & L2CAP_CONN_REMOTE_BUSY) &&
  1665. (pi->unacked_frames > 0))
  1666. l2cap_ertm_start_retrans_timer(pi);
  1667. pi->conn_state &= ~L2CAP_CONN_REMOTE_BUSY;
  1668. /* Send pending iframes */
  1669. l2cap_ertm_send(sk);
  1670. if (pi->conn_state & L2CAP_CONN_SEND_FBIT) {
  1671. /* F-bit wasn't sent in an s-frame or i-frame yet, so
  1672. * send it now.
  1673. */
  1674. control.super = L2CAP_SFRAME_RR;
  1675. l2cap_ertm_send_sframe(sk, &control);
  1676. }
  1677. }
  1678. static void l2cap_ertm_send_srej(struct sock *sk, u16 txseq)
  1679. {
  1680. struct bt_l2cap_control control;
  1681. struct l2cap_pinfo *pi;
  1682. u16 seq;
  1683. BT_DBG("sk %p, txseq %d", sk, (int)txseq);
  1684. pi = l2cap_pi(sk);
  1685. memset(&control, 0, sizeof(control));
  1686. control.frame_type = 's';
  1687. control.super = L2CAP_SFRAME_SREJ;
  1688. for (seq = pi->expected_tx_seq; seq != txseq;
  1689. seq = __next_seq(seq, pi)) {
  1690. if (!l2cap_ertm_seq_in_queue(SREJ_QUEUE(pi), seq)) {
  1691. control.reqseq = seq;
  1692. l2cap_ertm_send_sframe(sk, &control);
  1693. l2cap_seq_list_append(&pi->srej_list, seq);
  1694. }
  1695. }
  1696. pi->expected_tx_seq = __next_seq(txseq, pi);
  1697. }
  1698. static void l2cap_ertm_send_srej_tail(struct sock *sk)
  1699. {
  1700. struct bt_l2cap_control control;
  1701. struct l2cap_pinfo *pi;
  1702. BT_DBG("sk %p", sk);
  1703. pi = l2cap_pi(sk);
  1704. if (pi->srej_list.tail == L2CAP_SEQ_LIST_CLEAR)
  1705. return;
  1706. memset(&control, 0, sizeof(control));
  1707. control.frame_type = 's';
  1708. control.super = L2CAP_SFRAME_SREJ;
  1709. control.reqseq = pi->srej_list.tail;
  1710. l2cap_ertm_send_sframe(sk, &control);
  1711. }
  1712. static void l2cap_ertm_send_srej_list(struct sock *sk, u16 txseq)
  1713. {
  1714. struct bt_l2cap_control control;
  1715. struct l2cap_pinfo *pi;
  1716. u16 initial_head;
  1717. u16 seq;
  1718. BT_DBG("sk %p, txseq %d", sk, (int) txseq);
  1719. pi = l2cap_pi(sk);
  1720. memset(&control, 0, sizeof(control));
  1721. control.frame_type = 's';
  1722. control.super = L2CAP_SFRAME_SREJ;
  1723. /* Capture initial list head to allow only one pass through the list. */
  1724. initial_head = pi->srej_list.head;
  1725. do {
  1726. seq = l2cap_seq_list_pop(&pi->srej_list);
  1727. if ((seq == txseq) || (seq == L2CAP_SEQ_LIST_CLEAR))
  1728. break;
  1729. control.reqseq = seq;
  1730. l2cap_ertm_send_sframe(sk, &control);
  1731. l2cap_seq_list_append(&pi->srej_list, seq);
  1732. } while (pi->srej_list.head != initial_head);
  1733. }
  1734. static void l2cap_ertm_abort_rx_srej_sent(struct sock *sk)
  1735. {
  1736. struct l2cap_pinfo *pi = l2cap_pi(sk);
  1737. BT_DBG("sk %p", sk);
  1738. pi->expected_tx_seq = pi->buffer_seq;
  1739. l2cap_seq_list_clear(&l2cap_pi(sk)->srej_list);
  1740. skb_queue_purge(SREJ_QUEUE(sk));
  1741. pi->rx_state = L2CAP_ERTM_RX_STATE_RECV;
  1742. }
  1743. static int l2cap_ertm_tx_state_xmit(struct sock *sk,
  1744. struct bt_l2cap_control *control,
  1745. struct sk_buff_head *skbs, u8 event)
  1746. {
  1747. struct l2cap_pinfo *pi;
  1748. int err = 0;
  1749. BT_DBG("sk %p, control %p, skbs %p, event %d", sk, control, skbs,
  1750. (int)event);
  1751. pi = l2cap_pi(sk);
  1752. switch (event) {
  1753. case L2CAP_ERTM_EVENT_DATA_REQUEST:
  1754. if (sk->sk_send_head == NULL)
  1755. sk->sk_send_head = skb_peek(skbs);
  1756. skb_queue_splice_tail_init(skbs, TX_QUEUE(sk));
  1757. l2cap_ertm_send(sk);
  1758. break;
  1759. case L2CAP_ERTM_EVENT_LOCAL_BUSY_DETECTED:
  1760. BT_DBG("Enter LOCAL_BUSY");
  1761. pi->conn_state |= L2CAP_CONN_LOCAL_BUSY;
  1762. if (pi->rx_state == L2CAP_ERTM_RX_STATE_SREJ_SENT) {
  1763. /* The SREJ_SENT state must be aborted if we are to
  1764. * enter the LOCAL_BUSY state.
  1765. */
  1766. l2cap_ertm_abort_rx_srej_sent(sk);
  1767. }
  1768. l2cap_ertm_send_ack(sk);
  1769. break;
  1770. case L2CAP_ERTM_EVENT_LOCAL_BUSY_CLEAR:
  1771. BT_DBG("Exit LOCAL_BUSY");
  1772. pi->conn_state &= ~L2CAP_CONN_LOCAL_BUSY;
  1773. if (pi->amp_move_state == L2CAP_AMP_STATE_WAIT_LOCAL_BUSY) {
  1774. if (pi->amp_move_role == L2CAP_AMP_MOVE_INITIATOR) {
  1775. pi->amp_move_state =
  1776. L2CAP_AMP_STATE_WAIT_MOVE_CONFIRM_RSP;
  1777. l2cap_send_move_chan_cfm(pi->conn, pi,
  1778. pi->scid,
  1779. L2CAP_MOVE_CHAN_CONFIRMED);
  1780. l2cap_sock_set_timer(sk, L2CAP_MOVE_TIMEOUT);
  1781. } else if (pi->amp_move_role ==
  1782. L2CAP_AMP_MOVE_RESPONDER) {
  1783. pi->amp_move_state =
  1784. L2CAP_AMP_STATE_WAIT_MOVE_CONFIRM;
  1785. l2cap_send_move_chan_rsp(pi->conn,
  1786. pi->amp_move_cmd_ident,
  1787. pi->dcid,
  1788. L2CAP_MOVE_CHAN_SUCCESS);
  1789. }
  1790. break;
  1791. }
  1792. if (pi->amp_move_role == L2CAP_AMP_MOVE_NONE &&
  1793. (pi->conn_state & L2CAP_CONN_SENT_RNR)) {
  1794. struct bt_l2cap_control local_control;
  1795. memset(&local_control, 0, sizeof(local_control));
  1796. local_control.frame_type = 's';
  1797. local_control.super = L2CAP_SFRAME_RR;
  1798. local_control.poll = 1;
  1799. local_control.reqseq = pi->buffer_seq;
  1800. l2cap_ertm_send_sframe(sk, &local_control);
  1801. pi->retry_count = 1;
  1802. l2cap_ertm_start_monitor_timer(pi);
  1803. pi->tx_state = L2CAP_ERTM_TX_STATE_WAIT_F;
  1804. }
  1805. break;
  1806. case L2CAP_ERTM_EVENT_RECV_REQSEQ_AND_FBIT:
  1807. l2cap_ertm_process_reqseq(sk, control->reqseq);
  1808. break;
  1809. case L2CAP_ERTM_EVENT_EXPLICIT_POLL:
  1810. l2cap_ertm_send_rr_or_rnr(sk, 1);
  1811. pi->retry_count = 1;
  1812. l2cap_ertm_start_monitor_timer(pi);
  1813. l2cap_ertm_stop_ack_timer(pi);
  1814. pi->tx_state = L2CAP_ERTM_TX_STATE_WAIT_F;
  1815. break;
  1816. case L2CAP_ERTM_EVENT_RETRANS_TIMER_EXPIRES:
  1817. l2cap_ertm_send_rr_or_rnr(sk, 1);
  1818. pi->retry_count = 1;
  1819. l2cap_ertm_start_monitor_timer(pi);
  1820. pi->tx_state = L2CAP_ERTM_TX_STATE_WAIT_F;
  1821. break;
  1822. case L2CAP_ERTM_EVENT_RECV_FBIT:
  1823. /* Nothing to process */
  1824. break;
  1825. default:
  1826. break;
  1827. }
  1828. return err;
  1829. }
  1830. static int l2cap_ertm_tx_state_wait_f(struct sock *sk,
  1831. struct bt_l2cap_control *control,
  1832. struct sk_buff_head *skbs, u8 event)
  1833. {
  1834. struct l2cap_pinfo *pi;
  1835. int err = 0;
  1836. BT_DBG("sk %p, control %p, skbs %p, event %d", sk, control, skbs,
  1837. (int)event);
  1838. pi = l2cap_pi(sk);
  1839. switch (event) {
  1840. case L2CAP_ERTM_EVENT_DATA_REQUEST:
  1841. if (sk->sk_send_head == NULL)
  1842. sk->sk_send_head = skb_peek(skbs);
  1843. /* Queue data, but don't send. */
  1844. skb_queue_splice_tail_init(skbs, TX_QUEUE(sk));
  1845. break;
  1846. case L2CAP_ERTM_EVENT_LOCAL_BUSY_DETECTED:
  1847. BT_DBG("Enter LOCAL_BUSY");
  1848. pi->conn_state |= L2CAP_CONN_LOCAL_BUSY;
  1849. if (pi->rx_state == L2CAP_ERTM_RX_STATE_SREJ_SENT) {
  1850. /* The SREJ_SENT state must be aborted if we are to
  1851. * enter the LOCAL_BUSY state.
  1852. */
  1853. l2cap_ertm_abort_rx_srej_sent(sk);
  1854. }
  1855. l2cap_ertm_send_ack(sk);
  1856. break;
  1857. case L2CAP_ERTM_EVENT_LOCAL_BUSY_CLEAR:
  1858. BT_DBG("Exit LOCAL_BUSY");
  1859. pi->conn_state &= ~L2CAP_CONN_LOCAL_BUSY;
  1860. if (pi->conn_state & L2CAP_CONN_SENT_RNR) {
  1861. struct bt_l2cap_control local_control;
  1862. memset(&local_control, 0, sizeof(local_control));
  1863. local_control.frame_type = 's';
  1864. local_control.super = L2CAP_SFRAME_RR;
  1865. local_control.poll = 1;
  1866. local_control.reqseq = pi->buffer_seq;
  1867. l2cap_ertm_send_sframe(sk, &local_control);
  1868. pi->retry_count = 1;
  1869. l2cap_ertm_start_monitor_timer(pi);
  1870. pi->tx_state = L2CAP_ERTM_TX_STATE_WAIT_F;
  1871. }
  1872. break;
  1873. case L2CAP_ERTM_EVENT_RECV_REQSEQ_AND_FBIT:
  1874. l2cap_ertm_process_reqseq(sk, control->reqseq);
  1875. /* Fall through */
  1876. case L2CAP_ERTM_EVENT_RECV_FBIT:
  1877. if (control && control->final) {
  1878. l2cap_ertm_stop_monitor_timer(pi);
  1879. if (pi->unacked_frames > 0)
  1880. l2cap_ertm_start_retrans_timer(pi);
  1881. pi->retry_count = 0;
  1882. pi->tx_state = L2CAP_ERTM_TX_STATE_XMIT;
  1883. BT_DBG("recv fbit tx_state 0x2.2%x", pi->tx_state);
  1884. }
  1885. break;
  1886. case L2CAP_ERTM_EVENT_EXPLICIT_POLL:
  1887. /* Ignore */
  1888. break;
  1889. case L2CAP_ERTM_EVENT_MONITOR_TIMER_EXPIRES:
  1890. if ((pi->max_tx == 0) || (pi->retry_count < pi->max_tx)) {
  1891. l2cap_ertm_send_rr_or_rnr(sk, 1);
  1892. l2cap_ertm_start_monitor_timer(pi);
  1893. pi->retry_count += 1;
  1894. } else
  1895. l2cap_send_disconn_req(pi->conn, sk, ECONNABORTED);
  1896. break;
  1897. default:
  1898. break;
  1899. }
  1900. return err;
  1901. }
  1902. int l2cap_ertm_tx(struct sock *sk, struct bt_l2cap_control *control,
  1903. struct sk_buff_head *skbs, u8 event)
  1904. {
  1905. struct l2cap_pinfo *pi;
  1906. int err = 0;
  1907. BT_DBG("sk %p, control %p, skbs %p, event %d, state %d",
  1908. sk, control, skbs, (int)event, l2cap_pi(sk)->tx_state);
  1909. pi = l2cap_pi(sk);
  1910. switch (pi->tx_state) {
  1911. case L2CAP_ERTM_TX_STATE_XMIT:
  1912. err = l2cap_ertm_tx_state_xmit(sk, control, skbs, event);
  1913. break;
  1914. case L2CAP_ERTM_TX_STATE_WAIT_F:
  1915. err = l2cap_ertm_tx_state_wait_f(sk, control, skbs, event);
  1916. break;
  1917. default:
  1918. /* Ignore event */
  1919. break;
  1920. }
  1921. return err;
  1922. }
  1923. int l2cap_segment_sdu(struct sock *sk, struct sk_buff_head* seg_queue,
  1924. struct msghdr *msg, size_t len, int reseg)
  1925. {
  1926. struct sk_buff *skb;
  1927. u16 sdu_len;
  1928. size_t pdu_len;
  1929. int err = 0;
  1930. u8 sar;
  1931. BT_DBG("sk %p, msg %p, len %d", sk, msg, (int)len);
  1932. /* It is critical that ERTM PDUs fit in a single HCI fragment,
  1933. * so fragmented skbs are not used. The HCI layer's handling
  1934. * of fragmented skbs is not compatible with ERTM's queueing.
  1935. */
  1936. /* PDU size is derived from the HCI MTU */
  1937. pdu_len = l2cap_pi(sk)->conn->mtu;
  1938. /* Constrain BR/EDR PDU size to fit within the largest radio packet */
  1939. if (!l2cap_pi(sk)->ampcon)
  1940. pdu_len = min_t(size_t, pdu_len, L2CAP_BREDR_MAX_PAYLOAD);
  1941. /* Adjust for largest possible L2CAP overhead. */
  1942. pdu_len -= L2CAP_EXTENDED_HDR_SIZE + L2CAP_FCS_SIZE;
  1943. /* Remote device may have requested smaller PDUs */
  1944. pdu_len = min_t(size_t, pdu_len, l2cap_pi(sk)->remote_mps);
  1945. if (len <= pdu_len) {
  1946. sar = L2CAP_SAR_UNSEGMENTED;
  1947. sdu_len = 0;
  1948. pdu_len = len;
  1949. } else {
  1950. sar = L2CAP_SAR_START;
  1951. sdu_len = len;
  1952. pdu_len -= L2CAP_SDULEN_SIZE;
  1953. }
  1954. while (len) {
  1955. skb = l2cap_create_iframe_pdu(sk, msg, pdu_len, sdu_len, reseg);
  1956. BT_DBG("iframe skb %p", skb);
  1957. if (IS_ERR(skb)) {
  1958. __skb_queue_purge(seg_queue);
  1959. return PTR_ERR(skb);
  1960. }
  1961. bt_cb(skb)->control.sar = sar;
  1962. __skb_queue_tail(seg_queue, skb);
  1963. len -= pdu_len;
  1964. if (sdu_len) {
  1965. sdu_len = 0;
  1966. pdu_len += L2CAP_SDULEN_SIZE;
  1967. }
  1968. if (len <= pdu_len) {
  1969. sar = L2CAP_SAR_END;
  1970. pdu_len = len;
  1971. } else {
  1972. sar = L2CAP_SAR_CONTINUE;
  1973. }
  1974. }
  1975. return err;
  1976. }
  1977. static inline int is_initial_frame(u8 sar)
  1978. {
  1979. return (sar == L2CAP_SAR_UNSEGMENTED ||
  1980. sar == L2CAP_SAR_START);
  1981. }
  1982. static inline int l2cap_skbuff_to_kvec(struct sk_buff *skb, struct kvec *iv,
  1983. size_t veclen)
  1984. {
  1985. struct sk_buff *frag_iter;
  1986. BT_DBG("skb %p (len %d), iv %p", skb, (int)skb->len, iv);
  1987. if (iv->iov_len + skb->len > veclen)
  1988. return -ENOMEM;
  1989. memcpy(iv->iov_base + iv->iov_len, skb->data, skb->len);
  1990. iv->iov_len += skb->len;
  1991. skb_walk_frags(skb, frag_iter) {
  1992. if (iv->iov_len + skb->len > veclen)
  1993. return -ENOMEM;
  1994. BT_DBG("Copying %d bytes", (int)frag_iter->len);
  1995. memcpy(iv->iov_base + iv->iov_len, frag_iter->data,
  1996. frag_iter->len);
  1997. iv->iov_len += frag_iter->len;
  1998. }
  1999. return 0;
  2000. }
  2001. int l2cap_resegment_queue(struct sock *sk, struct sk_buff_head *queue)
  2002. {
  2003. void *buf;
  2004. int buflen;
  2005. int err = 0;
  2006. struct sk_buff *skb;
  2007. struct msghdr msg;
  2008. struct kvec iv;
  2009. struct sk_buff_head old_frames;
  2010. struct l2cap_pinfo *pi = l2cap_pi(sk);
  2011. BT_DBG("sk %p", sk);
  2012. if (skb_queue_empty(queue))
  2013. return 0;
  2014. memset(&msg, 0, sizeof(msg));
  2015. msg.msg_iov = (struct iovec *) &iv;
  2016. buflen = pi->omtu + L2CAP_FCS_SIZE;
  2017. buf = kzalloc(buflen, GFP_TEMPORARY);
  2018. if (!buf) {
  2019. BT_DBG("Could not allocate resegmentation buffer");
  2020. return -ENOMEM;
  2021. }
  2022. /* Move current frames off the original queue */
  2023. __skb_queue_head_init(&old_frames);
  2024. skb_queue_splice_tail_init(queue, &old_frames);
  2025. while (!skb_queue_empty(&old_frames)) {
  2026. struct sk_buff_head current_sdu;
  2027. u8 original_sar;
  2028. /* Reassemble each SDU from one or more PDUs */
  2029. iv.iov_base = buf;
  2030. iv.iov_len = 0;
  2031. skb = skb_peek(&old_frames);
  2032. original_sar = bt_cb(skb)->control.sar;
  2033. __skb_unlink(skb, &old_frames);
  2034. /* Append data to SDU */
  2035. if (pi->extended_control)
  2036. skb_pull(skb, L2CAP_EXTENDED_HDR_SIZE);
  2037. else
  2038. skb_pull(skb, L2CAP_ENHANCED_HDR_SIZE);
  2039. if (original_sar == L2CAP_SAR_START)
  2040. skb_pull(skb, L2CAP_SDULEN_SIZE);
  2041. err = l2cap_skbuff_to_kvec(skb, &iv, buflen);
  2042. if (bt_cb(skb)->control.fcs == L2CAP_FCS_CRC16)
  2043. iv.iov_len -= L2CAP_FCS_SIZE;
  2044. /* Free skb */
  2045. kfree_skb(skb);
  2046. if (err)
  2047. break;
  2048. while (!skb_queue_empty(&old_frames) && !err) {
  2049. /* Check next frame */
  2050. skb = skb_peek(&old_frames);
  2051. if (is_initial_frame(bt_cb(skb)->control.sar))
  2052. break;
  2053. __skb_unlink(skb, &old_frames);
  2054. /* Append data to SDU */
  2055. if (pi->extended_control)
  2056. skb_pull(skb, L2CAP_EXTENDED_HDR_SIZE);
  2057. else
  2058. skb_pull(skb, L2CAP_ENHANCED_HDR_SIZE);
  2059. if (bt_cb(skb)->control.sar == L2CAP_SAR_START)
  2060. skb_pull(skb, L2CAP_SDULEN_SIZE);
  2061. err = l2cap_skbuff_to_kvec(skb, &iv, buflen);
  2062. if (bt_cb(skb)->control.fcs == L2CAP_FCS_CRC16)
  2063. iv.iov_len -= L2CAP_FCS_SIZE;
  2064. /* Free skb */
  2065. kfree_skb(skb);
  2066. }
  2067. if (err)
  2068. break;
  2069. /* Segment data */
  2070. __skb_queue_head_init(&current_sdu);
  2071. /* skbs for the SDU were just freed, but the
  2072. * resegmenting process could produce more, smaller
  2073. * skbs due to smaller PDUs and reduced HCI MTU. The
  2074. * overhead from the sk_buff structs could put us over
  2075. * the sk_sndbuf limit.
  2076. *
  2077. * Since this code is running in response to a
  2078. * received poll/final packet, it cannot block.
  2079. * Therefore, memory allocation needs to be allowed by
  2080. * falling back to bt_skb_alloc() (with
  2081. * skb_set_owner_w() to maintain sk_wmem_alloc
  2082. * correctly).
  2083. */
  2084. msg.msg_iovlen = iv.iov_len;
  2085. err = l2cap_segment_sdu(sk, &current_sdu, &msg,
  2086. msg.msg_iovlen, 1);
  2087. if (err || skb_queue_empty(&current_sdu)) {
  2088. BT_DBG("Error %d resegmenting data for socket %p",
  2089. err, sk);
  2090. __skb_queue_purge(&current_sdu);
  2091. break;
  2092. }
  2093. /* Fix up first PDU SAR bits */
  2094. if (!is_initial_frame(original_sar)) {
  2095. BT_DBG("Changing SAR bits, %d PDUs",
  2096. skb_queue_len(&current_sdu));
  2097. skb = skb_peek(&current_sdu);
  2098. if (skb_queue_len(&current_sdu) == 1) {
  2099. /* Change SAR from 'unsegmented' to 'end' */
  2100. bt_cb(skb)->control.sar = L2CAP_SAR_END;
  2101. } else {
  2102. struct l2cap_hdr *lh;
  2103. size_t hdrlen;
  2104. /* Change SAR from 'start' to 'continue' */
  2105. bt_cb(skb)->control.sar = L2CAP_SAR_CONTINUE;
  2106. /* Start frames contain 2 bytes for
  2107. * sdulen and continue frames don't.
  2108. * Must rewrite header to eliminate
  2109. * sdulen and then adjust l2cap frame
  2110. * length.
  2111. */
  2112. if (pi->extended_control)
  2113. hdrlen = L2CAP_EXTENDED_HDR_SIZE;
  2114. else
  2115. hdrlen = L2CAP_ENHANCED_HDR_SIZE;
  2116. memmove(skb->data + L2CAP_SDULEN_SIZE,
  2117. skb->data, hdrlen);
  2118. skb_pull(skb, L2CAP_SDULEN_SIZE);
  2119. lh = (struct l2cap_hdr *)skb->data;
  2120. lh->len = cpu_to_le16(le16_to_cpu(lh->len) -
  2121. L2CAP_SDULEN_SIZE);
  2122. }
  2123. }
  2124. /* Add to queue */
  2125. skb_queue_splice_tail(&current_sdu, queue);
  2126. }
  2127. __skb_queue_purge(&old_frames);
  2128. if (err)
  2129. __skb_queue_purge(queue);
  2130. kfree(buf);
  2131. BT_DBG("Queue resegmented, err=%d", err);
  2132. return err;
  2133. }
  2134. static void l2cap_resegment_worker(struct work_struct *work)
  2135. {
  2136. int err = 0;
  2137. struct l2cap_resegment_work *seg_work =
  2138. container_of(work, struct l2cap_resegment_work, work);
  2139. struct sock *sk = seg_work->sk;
  2140. kfree(seg_work);
  2141. BT_DBG("sk %p", sk);
  2142. lock_sock(sk);
  2143. if (l2cap_pi(sk)->amp_move_state != L2CAP_AMP_STATE_RESEGMENT) {
  2144. release_sock(sk);
  2145. sock_put(sk);
  2146. return;
  2147. }
  2148. err = l2cap_resegment_queue(sk, TX_QUEUE(sk));
  2149. l2cap_pi(sk)->amp_move_state = L2CAP_AMP_STATE_STABLE;
  2150. if (skb_queue_empty(TX_QUEUE(sk)))
  2151. sk->sk_send_head = NULL;
  2152. else
  2153. sk->sk_send_head = skb_peek(TX_QUEUE(sk));
  2154. if (err)
  2155. l2cap_send_disconn_req(l2cap_pi(sk)->conn, sk, ECONNRESET);
  2156. else
  2157. l2cap_ertm_send(sk);
  2158. release_sock(sk);
  2159. sock_put(sk);
  2160. }
  2161. static int l2cap_setup_resegment(struct sock *sk)
  2162. {
  2163. struct l2cap_resegment_work *seg_work;
  2164. BT_DBG("sk %p", sk);
  2165. if (skb_queue_empty(TX_QUEUE(sk)))
  2166. return 0;
  2167. seg_work = kzalloc(sizeof(*seg_work), GFP_ATOMIC);
  2168. if (!seg_work)
  2169. return -ENOMEM;
  2170. INIT_WORK(&seg_work->work, l2cap_resegment_worker);
  2171. sock_hold(sk);
  2172. seg_work->sk = sk;
  2173. if (!queue_work(_l2cap_wq, &seg_work->work)) {
  2174. kfree(seg_work);
  2175. sock_put(sk);
  2176. return -ENOMEM;
  2177. }
  2178. l2cap_pi(sk)->amp_move_state = L2CAP_AMP_STATE_RESEGMENT;
  2179. return 0;
  2180. }
  2181. static inline int l2cap_rmem_available(struct sock *sk)
  2182. {
  2183. BT_DBG("sk_rmem_alloc %d, sk_rcvbuf %d",
  2184. atomic_read(&sk->sk_rmem_alloc), sk->sk_rcvbuf);
  2185. return atomic_read(&sk->sk_rmem_alloc) < sk->sk_rcvbuf / 3;
  2186. }
  2187. static inline int l2cap_rmem_full(struct sock *sk)
  2188. {
  2189. BT_DBG("sk_rmem_alloc %d, sk_rcvbuf %d",
  2190. atomic_read(&sk->sk_rmem_alloc), sk->sk_rcvbuf);
  2191. return atomic_read(&sk->sk_rmem_alloc) > (2 * sk->sk_rcvbuf) / 3;
  2192. }
  2193. void l2cap_amp_move_init(struct sock *sk)
  2194. {
  2195. BT_DBG("sk %p", sk);
  2196. if (!l2cap_pi(sk)->conn)
  2197. return;
  2198. if (!(l2cap_pi(sk)->conn->fc_mask & L2CAP_FC_A2MP) || !enable_hs)
  2199. return;
  2200. if (l2cap_pi(sk)->amp_id == 0) {
  2201. if (l2cap_pi(sk)->amp_pref != BT_AMP_POLICY_PREFER_AMP)
  2202. return;
  2203. l2cap_pi(sk)->amp_move_role = L2CAP_AMP_MOVE_INITIATOR;
  2204. l2cap_pi(sk)->amp_move_state = L2CAP_AMP_STATE_WAIT_PREPARE;
  2205. amp_create_physical(l2cap_pi(sk)->conn, sk);
  2206. } else {
  2207. l2cap_pi(sk)->amp_move_role = L2CAP_AMP_MOVE_INITIATOR;
  2208. l2cap_pi(sk)->amp_move_state =
  2209. L2CAP_AMP_STATE_WAIT_MOVE_RSP_SUCCESS;
  2210. l2cap_pi(sk)->amp_move_id = 0;
  2211. l2cap_amp_move_setup(sk);
  2212. l2cap_send_move_chan_req(l2cap_pi(sk)->conn,
  2213. l2cap_pi(sk), l2cap_pi(sk)->scid, 0);
  2214. l2cap_sock_set_timer(sk, L2CAP_MOVE_TIMEOUT);
  2215. }
  2216. }
  2217. static void l2cap_chan_ready(struct sock *sk)
  2218. {
  2219. struct sock *parent = bt_sk(sk)->parent;
  2220. BT_DBG("sk %p, parent %p", sk, parent);
  2221. l2cap_pi(sk)->conf_state = 0;
  2222. l2cap_sock_clear_timer(sk);
  2223. if (!parent) {
  2224. /* Outgoing channel.
  2225. * Wake up socket sleeping on connect.
  2226. */
  2227. sk->sk_state = BT_CONNECTED;
  2228. sk->sk_state_change(sk);
  2229. } else {
  2230. /* Incoming channel.
  2231. * Wake up socket sleeping on accept.
  2232. */
  2233. parent->sk_data_ready(parent, 0);
  2234. }
  2235. }
  2236. /* Copy frame to all raw sockets on that connection */
  2237. static void l2cap_raw_recv(struct l2cap_conn *conn, struct sk_buff *skb)
  2238. {
  2239. struct l2cap_chan_list *l = &conn->chan_list;
  2240. struct sk_buff *nskb;
  2241. struct sock *sk;
  2242. BT_DBG("conn %p", conn);
  2243. read_lock(&l->lock);
  2244. for (sk = l->head; sk; sk = l2cap_pi(sk)->next_c) {
  2245. if (sk->sk_type != SOCK_RAW)
  2246. continue;
  2247. /* Don't send frame to the socket it came from */
  2248. if (skb->sk == sk)
  2249. continue;
  2250. nskb = skb_clone(skb, GFP_ATOMIC);
  2251. if (!nskb)
  2252. continue;
  2253. if (sock_queue_rcv_skb(sk, nskb))
  2254. kfree_skb(nskb);
  2255. }
  2256. read_unlock(&l->lock);
  2257. }
  2258. /* ---- L2CAP signalling commands ---- */
  2259. static struct sk_buff *l2cap_build_cmd(struct l2cap_conn *conn,
  2260. u8 code, u8 ident, u16 dlen, void *data)
  2261. {
  2262. struct sk_buff *skb, **frag;
  2263. struct l2cap_cmd_hdr *cmd;
  2264. struct l2cap_hdr *lh;
  2265. int len, count;
  2266. unsigned int mtu = conn->hcon->hdev->acl_mtu;
  2267. BT_DBG("conn %p, code 0x%2.2x, ident 0x%2.2x, len %d",
  2268. conn, code, ident, dlen);
  2269. if (conn->mtu < L2CAP_HDR_SIZE + L2CAP_CMD_HDR_SIZE)
  2270. return NULL;
  2271. len = L2CAP_HDR_SIZE + L2CAP_CMD_HDR_SIZE + dlen;
  2272. count = min_t(unsigned int, mtu, len);
  2273. skb = bt_skb_alloc(count, GFP_ATOMIC);
  2274. if (!skb)
  2275. return NULL;
  2276. lh = (struct l2cap_hdr *) skb_put(skb, L2CAP_HDR_SIZE);
  2277. lh->len = cpu_to_le16(L2CAP_CMD_HDR_SIZE + dlen);
  2278. if (conn->hcon->type == LE_LINK)
  2279. lh->cid = cpu_to_le16(L2CAP_CID_LE_SIGNALING);
  2280. else
  2281. lh->cid = cpu_to_le16(L2CAP_CID_SIGNALING);
  2282. cmd = (struct l2cap_cmd_hdr *) skb_put(skb, L2CAP_CMD_HDR_SIZE);
  2283. cmd->code = code;
  2284. cmd->ident = ident;
  2285. cmd->len = cpu_to_le16(dlen);
  2286. if (dlen) {
  2287. count -= L2CAP_HDR_SIZE + L2CAP_CMD_HDR_SIZE;
  2288. memcpy(skb_put(skb, count), data, count);
  2289. data += count;
  2290. }
  2291. len -= skb->len;
  2292. /* Continuation fragments (no L2CAP header) */
  2293. frag = &skb_shinfo(skb)->frag_list;
  2294. while (len) {
  2295. count = min_t(unsigned int, mtu, len);
  2296. *frag = bt_skb_alloc(count, GFP_ATOMIC);
  2297. if (!*frag)
  2298. goto fail;
  2299. memcpy(skb_put(*frag, count), data, count);
  2300. len -= count;
  2301. data += count;
  2302. frag = &(*frag)->next;
  2303. }
  2304. return skb;
  2305. fail:
  2306. kfree_skb(skb);
  2307. return NULL;
  2308. }
  2309. static inline int l2cap_get_conf_opt(void **ptr, int *type, int *olen, unsigned long *val)
  2310. {
  2311. struct l2cap_conf_opt *opt = *ptr;
  2312. int len;
  2313. len = L2CAP_CONF_OPT_SIZE + opt->len;
  2314. *ptr += len;
  2315. *type = opt->type;
  2316. *olen = opt->len;
  2317. switch (opt->len) {
  2318. case 1:
  2319. *val = *((u8 *) opt->val);
  2320. break;
  2321. case 2:
  2322. *val = get_unaligned_le16(opt->val);
  2323. break;
  2324. case 4:
  2325. *val = get_unaligned_le32(opt->val);
  2326. break;
  2327. default:
  2328. *val = (unsigned long) opt->val;
  2329. break;
  2330. }
  2331. BT_DBG("type 0x%2.2x len %d val 0x%lx", *type, opt->len, *val);
  2332. return len;
  2333. }
  2334. static void l2cap_add_conf_opt(void **ptr, u8 type, u8 len, unsigned long val, size_t size)
  2335. {
  2336. struct l2cap_conf_opt *opt = *ptr;
  2337. BT_DBG("type 0x%2.2x len %d val 0x%lx", type, len, val);
  2338. if (size < L2CAP_CONF_OPT_SIZE + len)
  2339. return;
  2340. opt->type = type;
  2341. opt->len = len;
  2342. switch (len) {
  2343. case 1:
  2344. *((u8 *) opt->val) = val;
  2345. break;
  2346. case 2:
  2347. put_unaligned_le16(val, opt->val);
  2348. break;
  2349. case 4:
  2350. put_unaligned_le32(val, opt->val);
  2351. break;
  2352. default:
  2353. memcpy(opt->val, (void *) val, len);
  2354. break;
  2355. }
  2356. *ptr += L2CAP_CONF_OPT_SIZE + len;
  2357. }
  2358. static void l2cap_ertm_ack_timeout(struct work_struct *work)
  2359. {
  2360. struct delayed_work *delayed =
  2361. container_of(work, struct delayed_work, work);
  2362. struct l2cap_pinfo *pi =
  2363. container_of(delayed, struct l2cap_pinfo, ack_work);
  2364. struct sock *sk = (struct sock *)pi;
  2365. u16 frames_to_ack;
  2366. BT_DBG("sk %p", sk);
  2367. if (!sk)
  2368. return;
  2369. lock_sock(sk);
  2370. if (!l2cap_pi(sk)->conn) {
  2371. release_sock(sk);
  2372. return;
  2373. }
  2374. frames_to_ack = __delta_seq(l2cap_pi(sk)->buffer_seq,
  2375. l2cap_pi(sk)->last_acked_seq,
  2376. l2cap_pi(sk));
  2377. if (frames_to_ack)
  2378. l2cap_ertm_send_rr_or_rnr(sk, 0);
  2379. release_sock(sk);
  2380. }
  2381. static void l2cap_ertm_retrans_timeout(struct work_struct *work)
  2382. {
  2383. struct delayed_work *delayed =
  2384. container_of(work, struct delayed_work, work);
  2385. struct l2cap_pinfo *pi =
  2386. container_of(delayed, struct l2cap_pinfo, retrans_work);
  2387. struct sock *sk = (struct sock *)pi;
  2388. BT_DBG("sk %p", sk);
  2389. if (!sk)
  2390. return;
  2391. lock_sock(sk);
  2392. if (!l2cap_pi(sk)->conn) {
  2393. release_sock(sk);
  2394. return;
  2395. }
  2396. l2cap_ertm_tx(sk, 0, 0, L2CAP_ERTM_EVENT_RETRANS_TIMER_EXPIRES);
  2397. release_sock(sk);
  2398. }
  2399. static void l2cap_ertm_monitor_timeout(struct work_struct *work)
  2400. {
  2401. struct delayed_work *delayed =
  2402. container_of(work, struct delayed_work, work);
  2403. struct l2cap_pinfo *pi =
  2404. container_of(delayed, struct l2cap_pinfo, monitor_work);
  2405. struct sock *sk = (struct sock *)pi;
  2406. BT_DBG("sk %p", sk);
  2407. if (!sk)
  2408. return;
  2409. lock_sock(sk);
  2410. if (!l2cap_pi(sk)->conn) {
  2411. release_sock(sk);
  2412. return;
  2413. }
  2414. l2cap_ertm_tx(sk, 0, 0, L2CAP_ERTM_EVENT_MONITOR_TIMER_EXPIRES);
  2415. release_sock(sk);
  2416. }
  2417. static inline void l2cap_ertm_init(struct sock *sk)
  2418. {
  2419. l2cap_pi(sk)->next_tx_seq = 0;
  2420. l2cap_pi(sk)->expected_tx_seq = 0;
  2421. l2cap_pi(sk)->expected_ack_seq = 0;
  2422. l2cap_pi(sk)->unacked_frames = 0;
  2423. l2cap_pi(sk)->buffer_seq = 0;
  2424. l2cap_pi(sk)->frames_sent = 0;
  2425. l2cap_pi(sk)->last_acked_seq = 0;
  2426. l2cap_pi(sk)->sdu = NULL;
  2427. l2cap_pi(sk)->sdu_last_frag = NULL;
  2428. l2cap_pi(sk)->sdu_len = 0;
  2429. atomic_set(&l2cap_pi(sk)->ertm_queued, 0);
  2430. l2cap_pi(sk)->rx_state = L2CAP_ERTM_RX_STATE_RECV;
  2431. l2cap_pi(sk)->tx_state = L2CAP_ERTM_TX_STATE_XMIT;
  2432. BT_DBG("tx_state 0x2.2%x rx_state 0x2.2%x", l2cap_pi(sk)->tx_state,
  2433. l2cap_pi(sk)->rx_state);
  2434. l2cap_pi(sk)->amp_id = 0;
  2435. l2cap_pi(sk)->amp_move_state = L2CAP_AMP_STATE_STABLE;
  2436. l2cap_pi(sk)->amp_move_role = L2CAP_AMP_MOVE_NONE;
  2437. l2cap_pi(sk)->amp_move_reqseq = 0;
  2438. l2cap_pi(sk)->amp_move_event = 0;
  2439. INIT_DELAYED_WORK(&l2cap_pi(sk)->ack_work, l2cap_ertm_ack_timeout);
  2440. INIT_DELAYED_WORK(&l2cap_pi(sk)->retrans_work,
  2441. l2cap_ertm_retrans_timeout);
  2442. INIT_DELAYED_WORK(&l2cap_pi(sk)->monitor_work,
  2443. l2cap_ertm_monitor_timeout);
  2444. INIT_WORK(&l2cap_pi(sk)->tx_work, l2cap_ertm_tx_worker);
  2445. skb_queue_head_init(SREJ_QUEUE(sk));
  2446. skb_queue_head_init(TX_QUEUE(sk));
  2447. l2cap_seq_list_init(&l2cap_pi(sk)->srej_list, l2cap_pi(sk)->tx_win);
  2448. l2cap_seq_list_init(&l2cap_pi(sk)->retrans_list,
  2449. l2cap_pi(sk)->remote_tx_win);
  2450. }
  2451. void l2cap_ertm_destruct(struct sock *sk)
  2452. {
  2453. l2cap_seq_list_free(&l2cap_pi(sk)->srej_list);
  2454. l2cap_seq_list_free(&l2cap_pi(sk)->retrans_list);
  2455. }
  2456. void l2cap_ertm_shutdown(struct sock *sk)
  2457. {
  2458. l2cap_ertm_stop_ack_timer(l2cap_pi(sk));
  2459. l2cap_ertm_stop_retrans_timer(l2cap_pi(sk));
  2460. l2cap_ertm_stop_monitor_timer(l2cap_pi(sk));
  2461. }
  2462. void l2cap_ertm_recv_done(struct sock *sk)
  2463. {
  2464. lock_sock(sk);
  2465. if (l2cap_pi(sk)->mode != L2CAP_MODE_ERTM ||
  2466. sk->sk_state != BT_CONNECTED) {
  2467. release_sock(sk);
  2468. return;
  2469. }
  2470. /* Consume any queued incoming frames and update local busy status */
  2471. if (l2cap_pi(sk)->rx_state == L2CAP_ERTM_RX_STATE_SREJ_SENT &&
  2472. l2cap_ertm_rx_queued_iframes(sk))
  2473. l2cap_send_disconn_req(l2cap_pi(sk)->conn, sk, ECONNRESET);
  2474. else if ((l2cap_pi(sk)->conn_state & L2CAP_CONN_LOCAL_BUSY) &&
  2475. l2cap_rmem_available(sk))
  2476. l2cap_ertm_tx(sk, 0, 0, L2CAP_ERTM_EVENT_LOCAL_BUSY_CLEAR);
  2477. release_sock(sk);
  2478. }
  2479. static inline __u8 l2cap_select_mode(__u8 mode, __u16 remote_feat_mask)
  2480. {
  2481. switch (mode) {
  2482. case L2CAP_MODE_STREAMING:
  2483. case L2CAP_MODE_ERTM:
  2484. if (l2cap_mode_supported(mode, remote_feat_mask))
  2485. return mode;
  2486. /* fall through */
  2487. default:
  2488. return L2CAP_MODE_BASIC;
  2489. }
  2490. }
  2491. static void l2cap_setup_txwin(struct l2cap_pinfo *pi)
  2492. {
  2493. if (pi->tx_win > L2CAP_TX_WIN_MAX_ENHANCED &&
  2494. (pi->conn->feat_mask & L2CAP_FEAT_EXT_WINDOW)) {
  2495. pi->tx_win_max = L2CAP_TX_WIN_MAX_EXTENDED;
  2496. pi->extended_control = 1;
  2497. } else {
  2498. if (pi->tx_win > L2CAP_TX_WIN_MAX_ENHANCED)
  2499. pi->tx_win = L2CAP_TX_WIN_MAX_ENHANCED;
  2500. pi->tx_win_max = L2CAP_TX_WIN_MAX_ENHANCED;
  2501. pi->extended_control = 0;
  2502. }
  2503. pi->ack_win = pi->tx_win;
  2504. }
  2505. static void l2cap_aggregate_fs(struct hci_ext_fs *cur,
  2506. struct hci_ext_fs *new,
  2507. struct hci_ext_fs *agg)
  2508. {
  2509. *agg = *cur;
  2510. if ((cur->max_sdu != 0xFFFF) && (cur->sdu_arr_time != 0xFFFFFFFF)) {
  2511. /* current flow spec has known rate */
  2512. if ((new->max_sdu == 0xFFFF) ||
  2513. (new->sdu_arr_time == 0xFFFFFFFF)) {
  2514. /* new fs has unknown rate, so aggregate is unknown */
  2515. agg->max_sdu = 0xFFFF;
  2516. agg->sdu_arr_time = 0xFFFFFFFF;
  2517. } else {
  2518. /* new fs has known rate, so aggregate is known */
  2519. u64 cur_rate;
  2520. u64 new_rate;
  2521. cur_rate = cur->max_sdu * 1000000ULL;
  2522. if (cur->sdu_arr_time)
  2523. cur_rate = div_u64(cur_rate, cur->sdu_arr_time);
  2524. new_rate = new->max_sdu * 1000000ULL;
  2525. if (new->sdu_arr_time)
  2526. new_rate = div_u64(new_rate, new->sdu_arr_time);
  2527. cur_rate = cur_rate + new_rate;
  2528. if (cur_rate)
  2529. agg->sdu_arr_time = div64_u64(
  2530. agg->max_sdu * 1000000ULL, cur_rate);
  2531. }
  2532. }
  2533. }
  2534. static int l2cap_aggregate(struct hci_chan *chan, struct l2cap_pinfo *pi)
  2535. {
  2536. struct hci_ext_fs tx_fs;
  2537. struct hci_ext_fs rx_fs;
  2538. BT_DBG("chan %p", chan);
  2539. if (((chan->tx_fs.max_sdu == 0xFFFF) ||
  2540. (chan->tx_fs.sdu_arr_time == 0xFFFFFFFF)) &&
  2541. ((chan->rx_fs.max_sdu == 0xFFFF) ||
  2542. (chan->rx_fs.sdu_arr_time == 0xFFFFFFFF)))
  2543. return 0;
  2544. l2cap_aggregate_fs(&chan->tx_fs,
  2545. (struct hci_ext_fs *) &pi->local_fs, &tx_fs);
  2546. l2cap_aggregate_fs(&chan->rx_fs,
  2547. (struct hci_ext_fs *) &pi->remote_fs, &rx_fs);
  2548. hci_chan_modify(chan, &tx_fs, &rx_fs);
  2549. return 1;
  2550. }
  2551. static void l2cap_deaggregate_fs(struct hci_ext_fs *cur,
  2552. struct hci_ext_fs *old,
  2553. struct hci_ext_fs *agg)
  2554. {
  2555. *agg = *cur;
  2556. if ((cur->max_sdu != 0xFFFF) && (cur->sdu_arr_time != 0xFFFFFFFF)) {
  2557. u64 cur_rate;
  2558. u64 old_rate;
  2559. cur_rate = cur->max_sdu * 1000000ULL;
  2560. if (cur->sdu_arr_time)
  2561. cur_rate = div_u64(cur_rate, cur->sdu_arr_time);
  2562. old_rate = old->max_sdu * 1000000ULL;
  2563. if (old->sdu_arr_time)
  2564. old_rate = div_u64(old_rate, old->sdu_arr_time);
  2565. cur_rate = cur_rate - old_rate;
  2566. if (cur_rate)
  2567. agg->sdu_arr_time = div64_u64(
  2568. agg->max_sdu * 1000000ULL, cur_rate);
  2569. }
  2570. }
  2571. static int l2cap_deaggregate(struct hci_chan *chan, struct l2cap_pinfo *pi)
  2572. {
  2573. struct hci_ext_fs tx_fs;
  2574. struct hci_ext_fs rx_fs;
  2575. BT_DBG("chan %p", chan);
  2576. if (((chan->tx_fs.max_sdu == 0xFFFF) ||
  2577. (chan->tx_fs.sdu_arr_time == 0xFFFFFFFF)) &&
  2578. ((chan->rx_fs.max_sdu == 0xFFFF) ||
  2579. (chan->rx_fs.sdu_arr_time == 0xFFFFFFFF)))
  2580. return 0;
  2581. l2cap_deaggregate_fs(&chan->tx_fs,
  2582. (struct hci_ext_fs *) &pi->local_fs, &tx_fs);
  2583. l2cap_deaggregate_fs(&chan->rx_fs,
  2584. (struct hci_ext_fs *) &pi->remote_fs, &rx_fs);
  2585. hci_chan_modify(chan, &tx_fs, &rx_fs);
  2586. return 1;
  2587. }
  2588. static struct hci_chan *l2cap_chan_admit(u8 amp_id, struct sock *sk)
  2589. {
  2590. struct l2cap_pinfo *pi = l2cap_pi(sk);
  2591. struct hci_dev *hdev;
  2592. struct hci_conn *hcon;
  2593. struct hci_chan *chan;
  2594. hdev = hci_dev_get(amp_id);
  2595. if (!hdev)
  2596. return NULL;
  2597. BT_DBG("hdev %s", hdev->name);
  2598. hcon = hci_conn_hash_lookup_ba(hdev, ACL_LINK, pi->conn->dst);
  2599. if (!hcon) {
  2600. chan = NULL;
  2601. goto done;
  2602. }
  2603. chan = hci_chan_list_lookup_id(hdev, hcon->handle);
  2604. if (chan) {
  2605. l2cap_aggregate(chan, pi);
  2606. sock_hold(sk);
  2607. chan->l2cap_sk = sk;
  2608. hci_chan_hold(chan);
  2609. pi->ampchan = chan;
  2610. goto done;
  2611. }
  2612. chan = hci_chan_add(hdev);
  2613. if (chan) {
  2614. chan->conn = hcon;
  2615. sock_hold(sk);
  2616. chan->l2cap_sk = sk;
  2617. hci_chan_hold(chan);
  2618. pi->ampchan = chan;
  2619. hci_chan_create(chan,
  2620. (struct hci_ext_fs *) &pi->local_fs,
  2621. (struct hci_ext_fs *) &pi->remote_fs);
  2622. }
  2623. done:
  2624. hci_dev_put(hdev);
  2625. return chan;
  2626. }
  2627. static void l2cap_get_ertm_timeouts(struct l2cap_conf_rfc *rfc,
  2628. struct l2cap_pinfo *pi)
  2629. {
  2630. if (pi->amp_id && pi->ampcon) {
  2631. u64 ertm_to = pi->ampcon->hdev->amp_be_flush_to;
  2632. /* Class 1 devices have must have ERTM timeouts
  2633. * exceeding the Link Supervision Timeout. The
  2634. * default Link Supervision Timeout for AMP
  2635. * controllers is 10 seconds.
  2636. *
  2637. * Class 1 devices use 0xffffffff for their
  2638. * best-effort flush timeout, so the clamping logic
  2639. * will result in a timeout that meets the above
  2640. * requirement. ERTM timeouts are 16-bit values, so
  2641. * the maximum timeout is 65.535 seconds.
  2642. */
  2643. /* Convert timeout to milliseconds and round */
  2644. ertm_to = div_u64(ertm_to + 999, 1000);
  2645. /* This is the recommended formula for class 2 devices
  2646. * that start ERTM timers when packets are sent to the
  2647. * controller.
  2648. */
  2649. ertm_to = 3 * ertm_to + 500;
  2650. if (ertm_to > 0xffff)
  2651. ertm_to = 0xffff;
  2652. rfc->retrans_timeout = cpu_to_le16((u16) ertm_to);
  2653. rfc->monitor_timeout = rfc->retrans_timeout;
  2654. } else {
  2655. rfc->retrans_timeout = cpu_to_le16(L2CAP_DEFAULT_RETRANS_TO);
  2656. rfc->monitor_timeout = cpu_to_le16(L2CAP_DEFAULT_MONITOR_TO);
  2657. }
  2658. }
  2659. int l2cap_build_conf_req(struct sock *sk, void *data, size_t data_size)
  2660. {
  2661. struct l2cap_pinfo *pi = l2cap_pi(sk);
  2662. struct l2cap_conf_req *req = data;
  2663. struct l2cap_conf_rfc rfc = { .mode = pi->mode };
  2664. void *ptr = req->data;
  2665. void *endptr = data + data_size;
  2666. BT_DBG("sk %p mode %d", sk, pi->mode);
  2667. if (pi->num_conf_req || pi->num_conf_rsp)
  2668. goto done;
  2669. switch (pi->mode) {
  2670. case L2CAP_MODE_STREAMING:
  2671. case L2CAP_MODE_ERTM:
  2672. if (pi->conf_state & L2CAP_CONF_STATE2_DEVICE)
  2673. break;
  2674. /* fall through */
  2675. default:
  2676. pi->mode = l2cap_select_mode(rfc.mode, pi->conn->feat_mask);
  2677. break;
  2678. }
  2679. done:
  2680. if (pi->imtu != L2CAP_DEFAULT_MTU)
  2681. l2cap_add_conf_opt(&ptr, L2CAP_CONF_MTU, 2, pi->imtu, endptr - ptr);
  2682. switch (pi->mode) {
  2683. case L2CAP_MODE_BASIC:
  2684. if (!(pi->conn->feat_mask & L2CAP_FEAT_ERTM) &&
  2685. !(pi->conn->feat_mask & L2CAP_FEAT_STREAMING))
  2686. break;
  2687. rfc.txwin_size = 0;
  2688. rfc.max_transmit = 0;
  2689. rfc.retrans_timeout = 0;
  2690. rfc.monitor_timeout = 0;
  2691. rfc.max_pdu_size = 0;
  2692. l2cap_add_conf_opt(&ptr, L2CAP_CONF_RFC, sizeof(rfc),
  2693. (unsigned long) &rfc, endptr - ptr);
  2694. break;
  2695. case L2CAP_MODE_ERTM:
  2696. l2cap_setup_txwin(pi);
  2697. if (pi->tx_win > L2CAP_TX_WIN_MAX_ENHANCED)
  2698. rfc.txwin_size = L2CAP_TX_WIN_MAX_ENHANCED;
  2699. else
  2700. rfc.txwin_size = pi->tx_win;
  2701. rfc.max_transmit = pi->max_tx;
  2702. rfc.max_pdu_size = cpu_to_le16(L2CAP_DEFAULT_MAX_PDU_SIZE);
  2703. l2cap_get_ertm_timeouts(&rfc, pi);
  2704. if (L2CAP_DEFAULT_MAX_PDU_SIZE > pi->imtu)
  2705. rfc.max_pdu_size = cpu_to_le16(pi->imtu);
  2706. l2cap_add_conf_opt(&ptr, L2CAP_CONF_RFC, sizeof(rfc),
  2707. (unsigned long) &rfc, endptr - ptr);
  2708. if ((pi->conn->feat_mask & L2CAP_FEAT_EXT_WINDOW) &&
  2709. pi->extended_control) {
  2710. l2cap_add_conf_opt(&ptr, L2CAP_CONF_EXT_WINDOW, 2,
  2711. pi->tx_win, endptr - ptr);
  2712. }
  2713. if (pi->amp_id) {
  2714. /* default best effort extended flow spec */
  2715. struct l2cap_conf_ext_fs fs = {1, 1, 0xFFFF,
  2716. 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF};
  2717. l2cap_add_conf_opt(&ptr, L2CAP_CONF_EXT_FS,
  2718. sizeof(fs), (unsigned long) &fs, endptr - ptr);
  2719. }
  2720. if (!(pi->conn->feat_mask & L2CAP_FEAT_FCS))
  2721. break;
  2722. if (pi->fcs == L2CAP_FCS_NONE ||
  2723. pi->conf_state & L2CAP_CONF_NO_FCS_RECV) {
  2724. pi->fcs = L2CAP_FCS_NONE;
  2725. l2cap_add_conf_opt(&ptr, L2CAP_CONF_FCS, 1, pi->fcs, endptr - ptr);
  2726. }
  2727. break;
  2728. case L2CAP_MODE_STREAMING:
  2729. l2cap_setup_txwin(pi);
  2730. rfc.txwin_size = 0;
  2731. rfc.max_transmit = 0;
  2732. rfc.retrans_timeout = 0;
  2733. rfc.monitor_timeout = 0;
  2734. rfc.max_pdu_size = cpu_to_le16(L2CAP_DEFAULT_MAX_PDU_SIZE);
  2735. if (L2CAP_DEFAULT_MAX_PDU_SIZE > pi->imtu)
  2736. rfc.max_pdu_size = cpu_to_le16(pi->imtu);
  2737. l2cap_add_conf_opt(&ptr, L2CAP_CONF_RFC, sizeof(rfc),
  2738. (unsigned long) &rfc, endptr - ptr);
  2739. if ((pi->conn->feat_mask & L2CAP_FEAT_EXT_WINDOW) &&
  2740. pi->extended_control) {
  2741. l2cap_add_conf_opt(&ptr, L2CAP_CONF_EXT_WINDOW, 2, 0, endptr - ptr);
  2742. }
  2743. if (!(pi->conn->feat_mask & L2CAP_FEAT_FCS))
  2744. break;
  2745. if (pi->fcs == L2CAP_FCS_NONE ||
  2746. pi->conf_state & L2CAP_CONF_NO_FCS_RECV) {
  2747. pi->fcs = L2CAP_FCS_NONE;
  2748. l2cap_add_conf_opt(&ptr, L2CAP_CONF_FCS, 1, pi->fcs, endptr - ptr);
  2749. }
  2750. break;
  2751. }
  2752. req->dcid = cpu_to_le16(pi->dcid);
  2753. req->flags = cpu_to_le16(0);
  2754. return ptr - data;
  2755. }
  2756. static int l2cap_build_amp_reconf_req(struct sock *sk, void *data, size_t data_size)
  2757. {
  2758. struct l2cap_pinfo *pi = l2cap_pi(sk);
  2759. struct l2cap_conf_req *req = data;
  2760. struct l2cap_conf_rfc rfc = { .mode = pi->mode };
  2761. void *ptr = req->data;
  2762. void *endptr = data + data_size;
  2763. BT_DBG("sk %p", sk);
  2764. switch (pi->mode) {
  2765. case L2CAP_MODE_ERTM:
  2766. rfc.mode = L2CAP_MODE_ERTM;
  2767. rfc.txwin_size = pi->tx_win;
  2768. rfc.max_transmit = pi->max_tx;
  2769. rfc.max_pdu_size = cpu_to_le16(L2CAP_DEFAULT_MAX_PDU_SIZE);
  2770. l2cap_get_ertm_timeouts(&rfc, pi);
  2771. if (L2CAP_DEFAULT_MAX_PDU_SIZE > pi->imtu)
  2772. rfc.max_pdu_size = cpu_to_le16(pi->imtu);
  2773. break;
  2774. default:
  2775. return -ECONNREFUSED;
  2776. }
  2777. l2cap_add_conf_opt(&ptr, L2CAP_CONF_RFC, sizeof(rfc),
  2778. (unsigned long) &rfc, endptr - ptr);
  2779. if (pi->conn->feat_mask & L2CAP_FEAT_FCS) {
  2780. /* TODO assign fcs for br/edr based on socket config option */
  2781. /* FCS is not used with AMP because it is redundant - lower
  2782. * layers already include a checksum. */
  2783. if (pi->amp_id)
  2784. pi->local_conf.fcs = L2CAP_FCS_NONE;
  2785. else
  2786. pi->local_conf.fcs = L2CAP_FCS_CRC16;
  2787. l2cap_add_conf_opt(&ptr, L2CAP_CONF_FCS, 1, pi->local_conf.fcs, endptr - ptr);
  2788. pi->fcs = pi->local_conf.fcs | pi->remote_conf.fcs;
  2789. }
  2790. req->dcid = cpu_to_le16(pi->dcid);
  2791. req->flags = cpu_to_le16(0);
  2792. return ptr - data;
  2793. }
  2794. static int l2cap_parse_conf_req(struct sock *sk, void *data, size_t data_size)
  2795. {
  2796. struct l2cap_pinfo *pi = l2cap_pi(sk);
  2797. struct l2cap_conf_rsp *rsp = data;
  2798. void *ptr = rsp->data;
  2799. void *endptr = data + data_size;
  2800. void *req = pi->conf_req;
  2801. int len = pi->conf_len;
  2802. int type, hint, olen;
  2803. unsigned long val;
  2804. struct l2cap_conf_rfc rfc = { .mode = L2CAP_MODE_BASIC };
  2805. struct l2cap_conf_ext_fs fs;
  2806. u16 mtu = L2CAP_DEFAULT_MTU;
  2807. u16 result = L2CAP_CONF_SUCCESS;
  2808. BT_DBG("sk %p", sk);
  2809. if (pi->omtu > mtu)
  2810. mtu = pi->omtu;
  2811. while (len >= L2CAP_CONF_OPT_SIZE) {
  2812. len -= l2cap_get_conf_opt(&req, &type, &olen, &val);
  2813. if (len < 0)
  2814. break;
  2815. hint = type & L2CAP_CONF_HINT;
  2816. type &= L2CAP_CONF_MASK;
  2817. switch (type) {
  2818. case L2CAP_CONF_MTU:
  2819. if (olen != 2)
  2820. break;
  2821. mtu = val;
  2822. break;
  2823. case L2CAP_CONF_FLUSH_TO:
  2824. if (olen != 2)
  2825. break;
  2826. pi->flush_to = val;
  2827. if (pi->conf_state & L2CAP_CONF_LOCKSTEP)
  2828. result = L2CAP_CONF_UNACCEPT;
  2829. else
  2830. pi->remote_conf.flush_to = val;
  2831. break;
  2832. case L2CAP_CONF_QOS:
  2833. if (pi->conf_state & L2CAP_CONF_LOCKSTEP)
  2834. result = L2CAP_CONF_UNACCEPT;
  2835. break;
  2836. case L2CAP_CONF_RFC:
  2837. if (olen != sizeof(rfc))
  2838. break;
  2839. memcpy(&rfc, (void *) val, olen);
  2840. break;
  2841. case L2CAP_CONF_FCS:
  2842. if (olen != 1)
  2843. break;
  2844. if (val == L2CAP_FCS_NONE)
  2845. pi->conf_state |= L2CAP_CONF_NO_FCS_RECV;
  2846. pi->remote_conf.fcs = val;
  2847. break;
  2848. case L2CAP_CONF_EXT_FS:
  2849. if (olen == sizeof(fs)) {
  2850. pi->conf_state |= L2CAP_CONF_EFS_RECV;
  2851. if (!(pi->conf_state & L2CAP_CONF_LOCKSTEP)) {
  2852. result = L2CAP_CONF_UNACCEPT;
  2853. break;
  2854. }
  2855. memcpy(&fs, (void *) val, olen);
  2856. if (fs.type != L2CAP_SERVICE_BEST_EFFORT) {
  2857. result = L2CAP_CONF_FLOW_SPEC_REJECT;
  2858. break;
  2859. }
  2860. pi->remote_conf.flush_to =
  2861. le32_to_cpu(fs.flush_to);
  2862. pi->remote_fs.id = fs.id;
  2863. pi->remote_fs.type = fs.type;
  2864. pi->remote_fs.max_sdu =
  2865. le16_to_cpu(fs.max_sdu);
  2866. pi->remote_fs.sdu_arr_time =
  2867. le32_to_cpu(fs.sdu_arr_time);
  2868. pi->remote_fs.acc_latency =
  2869. le32_to_cpu(fs.acc_latency);
  2870. pi->remote_fs.flush_to =
  2871. le32_to_cpu(fs.flush_to);
  2872. }
  2873. break;
  2874. case L2CAP_CONF_EXT_WINDOW:
  2875. pi->extended_control = 1;
  2876. pi->remote_tx_win = val;
  2877. pi->tx_win_max = L2CAP_TX_WIN_MAX_EXTENDED;
  2878. pi->conf_state |= L2CAP_CONF_EXT_WIN_RECV;
  2879. break;
  2880. default:
  2881. if (hint)
  2882. break;
  2883. result = L2CAP_CONF_UNKNOWN;
  2884. *((u8 *) ptr++) = type;
  2885. break;
  2886. }
  2887. }
  2888. if (pi->num_conf_rsp || pi->num_conf_req > 1)
  2889. goto done;
  2890. switch (pi->mode) {
  2891. case L2CAP_MODE_STREAMING:
  2892. case L2CAP_MODE_ERTM:
  2893. if (!(pi->conf_state & L2CAP_CONF_STATE2_DEVICE)) {
  2894. pi->mode = l2cap_select_mode(rfc.mode,
  2895. pi->conn->feat_mask);
  2896. break;
  2897. }
  2898. if (pi->mode != rfc.mode)
  2899. return -ECONNREFUSED;
  2900. break;
  2901. }
  2902. done:
  2903. if (pi->mode != rfc.mode) {
  2904. result = L2CAP_CONF_UNACCEPT;
  2905. rfc.mode = pi->mode;
  2906. if (mtu > L2CAP_DEFAULT_MTU)
  2907. pi->omtu = mtu;
  2908. if (pi->num_conf_rsp == 1)
  2909. return -ECONNREFUSED;
  2910. l2cap_add_conf_opt(&ptr, L2CAP_CONF_RFC,
  2911. sizeof(rfc), (unsigned long) &rfc,
  2912. endptr - ptr);
  2913. }
  2914. if ((pi->conf_state & L2CAP_CONF_LOCKSTEP) &&
  2915. !(pi->conf_state & L2CAP_CONF_EFS_RECV))
  2916. return -ECONNREFUSED;
  2917. if (result == L2CAP_CONF_SUCCESS) {
  2918. /* Configure output options and let the other side know
  2919. * which ones we don't like. */
  2920. if (mtu < L2CAP_DEFAULT_MIN_MTU) {
  2921. result = L2CAP_CONF_UNACCEPT;
  2922. pi->omtu = L2CAP_DEFAULT_MIN_MTU;
  2923. } else {
  2924. pi->omtu = mtu;
  2925. pi->conf_state |= L2CAP_CONF_MTU_DONE;
  2926. }
  2927. l2cap_add_conf_opt(&ptr, L2CAP_CONF_MTU, 2, pi->omtu, endptr - ptr);
  2928. switch (rfc.mode) {
  2929. case L2CAP_MODE_BASIC:
  2930. pi->fcs = L2CAP_FCS_NONE;
  2931. pi->conf_state |= L2CAP_CONF_MODE_DONE;
  2932. break;
  2933. case L2CAP_MODE_ERTM:
  2934. if (!(pi->conf_state & L2CAP_CONF_EXT_WIN_RECV))
  2935. pi->remote_tx_win = rfc.txwin_size;
  2936. pi->remote_max_tx = rfc.max_transmit;
  2937. pi->remote_mps = le16_to_cpu(rfc.max_pdu_size);
  2938. l2cap_get_ertm_timeouts(&rfc, pi);
  2939. pi->conf_state |= L2CAP_CONF_MODE_DONE;
  2940. l2cap_add_conf_opt(&ptr, L2CAP_CONF_RFC,
  2941. sizeof(rfc), (unsigned long) &rfc, endptr - ptr);
  2942. if (pi->conf_state & L2CAP_CONF_LOCKSTEP)
  2943. l2cap_add_conf_opt(&ptr, L2CAP_CONF_EXT_FS,
  2944. sizeof(fs), (unsigned long) &fs, endptr - ptr);
  2945. break;
  2946. case L2CAP_MODE_STREAMING:
  2947. pi->remote_mps = le16_to_cpu(rfc.max_pdu_size);
  2948. pi->conf_state |= L2CAP_CONF_MODE_DONE;
  2949. l2cap_add_conf_opt(&ptr, L2CAP_CONF_RFC,
  2950. sizeof(rfc), (unsigned long) &rfc, endptr - ptr);
  2951. break;
  2952. default:
  2953. result = L2CAP_CONF_UNACCEPT;
  2954. memset(&rfc, 0, sizeof(rfc));
  2955. rfc.mode = pi->mode;
  2956. }
  2957. if (pi->conf_state & L2CAP_CONF_LOCKSTEP &&
  2958. !(pi->conf_state & L2CAP_CONF_PEND_SENT)) {
  2959. pi->conf_state |= L2CAP_CONF_PEND_SENT;
  2960. result = L2CAP_CONF_PENDING;
  2961. if (pi->conf_state & L2CAP_CONF_LOCKSTEP_PEND &&
  2962. pi->amp_id) {
  2963. struct hci_chan *chan;
  2964. /* Trigger logical link creation only on AMP */
  2965. chan = l2cap_chan_admit(pi->amp_id, sk);
  2966. if (!chan)
  2967. return -ECONNREFUSED;
  2968. if (chan->state == BT_CONNECTED)
  2969. l2cap_create_cfm(chan, 0);
  2970. }
  2971. }
  2972. if (result == L2CAP_CONF_SUCCESS)
  2973. pi->conf_state |= L2CAP_CONF_OUTPUT_DONE;
  2974. }
  2975. rsp->scid = cpu_to_le16(pi->dcid);
  2976. rsp->result = cpu_to_le16(result);
  2977. rsp->flags = cpu_to_le16(0x0000);
  2978. return ptr - data;
  2979. }
  2980. static int l2cap_parse_amp_move_reconf_req(struct sock *sk, void *data, size_t data_size)
  2981. {
  2982. struct l2cap_pinfo *pi = l2cap_pi(sk);
  2983. struct l2cap_conf_rsp *rsp = data;
  2984. void *ptr = rsp->data;
  2985. void *endptr = data + data_size;
  2986. void *req = pi->conf_req;
  2987. int len = pi->conf_len;
  2988. int type, hint, olen;
  2989. unsigned long val;
  2990. struct l2cap_conf_rfc rfc = { .mode = L2CAP_MODE_BASIC };
  2991. struct l2cap_conf_ext_fs fs;
  2992. u16 mtu = pi->omtu;
  2993. u16 tx_win = pi->remote_tx_win;
  2994. u16 result = L2CAP_CONF_SUCCESS;
  2995. BT_DBG("sk %p", sk);
  2996. while (len >= L2CAP_CONF_OPT_SIZE) {
  2997. len -= l2cap_get_conf_opt(&req, &type, &olen, &val);
  2998. hint = type & L2CAP_CONF_HINT;
  2999. type &= L2CAP_CONF_MASK;
  3000. switch (type) {
  3001. case L2CAP_CONF_MTU:
  3002. mtu = val;
  3003. break;
  3004. case L2CAP_CONF_FLUSH_TO:
  3005. if (pi->amp_move_id)
  3006. result = L2CAP_CONF_UNACCEPT;
  3007. else
  3008. pi->remote_conf.flush_to = val;
  3009. break;
  3010. case L2CAP_CONF_QOS:
  3011. if (pi->amp_move_id)
  3012. result = L2CAP_CONF_UNACCEPT;
  3013. break;
  3014. case L2CAP_CONF_RFC:
  3015. if (olen != sizeof(rfc))
  3016. break;
  3017. memcpy(&rfc, (void *) val, olen);
  3018. break;
  3019. case L2CAP_CONF_FCS:
  3020. if (olen != 1)
  3021. break;
  3022. pi->remote_conf.fcs = val;
  3023. break;
  3024. case L2CAP_CONF_EXT_FS:
  3025. if (olen != sizeof(fs))
  3026. break;
  3027. memcpy(&fs, (void *) val, olen);
  3028. if (fs.type != L2CAP_SERVICE_BEST_EFFORT)
  3029. result = L2CAP_CONF_FLOW_SPEC_REJECT;
  3030. else {
  3031. pi->remote_conf.flush_to =
  3032. le32_to_cpu(fs.flush_to);
  3033. }
  3034. break;
  3035. case L2CAP_CONF_EXT_WINDOW:
  3036. tx_win = val;
  3037. break;
  3038. default:
  3039. if (hint)
  3040. break;
  3041. result = L2CAP_CONF_UNKNOWN;
  3042. *((u8 *) ptr++) = type;
  3043. break;
  3044. }
  3045. }
  3046. BT_DBG("result 0x%2.2x cur mode 0x%2.2x req mode 0x%2.2x",
  3047. result, pi->mode, rfc.mode);
  3048. if (pi->mode != rfc.mode || rfc.mode == L2CAP_MODE_BASIC)
  3049. result = L2CAP_CONF_UNACCEPT;
  3050. if (result == L2CAP_CONF_SUCCESS) {
  3051. /* Configure output options and let the other side know
  3052. * which ones we don't like. */
  3053. /* Don't allow mtu to decrease. */
  3054. if (mtu < pi->omtu)
  3055. result = L2CAP_CONF_UNACCEPT;
  3056. BT_DBG("mtu %d omtu %d", mtu, pi->omtu);
  3057. l2cap_add_conf_opt(&ptr, L2CAP_CONF_MTU, 2, pi->omtu, endptr - ptr);
  3058. /* Don't allow extended transmit window to change. */
  3059. if (tx_win != pi->remote_tx_win) {
  3060. result = L2CAP_CONF_UNACCEPT;
  3061. l2cap_add_conf_opt(&ptr, L2CAP_CONF_EXT_WINDOW, 2,
  3062. pi->remote_tx_win, endptr - ptr);
  3063. }
  3064. pi->remote_mps = rfc.max_pdu_size;
  3065. if (rfc.mode == L2CAP_MODE_ERTM) {
  3066. l2cap_get_ertm_timeouts(&rfc, pi);
  3067. } else {
  3068. rfc.retrans_timeout = 0;
  3069. rfc.monitor_timeout = 0;
  3070. }
  3071. l2cap_add_conf_opt(&ptr, L2CAP_CONF_RFC,
  3072. sizeof(rfc), (unsigned long) &rfc, endptr - ptr);
  3073. }
  3074. if (result != L2CAP_CONF_SUCCESS)
  3075. goto done;
  3076. pi->fcs = pi->remote_conf.fcs | pi->local_conf.fcs;
  3077. if (pi->rx_state == L2CAP_ERTM_RX_STATE_WAIT_F_FLAG)
  3078. pi->flush_to = pi->remote_conf.flush_to;
  3079. done:
  3080. rsp->scid = cpu_to_le16(pi->dcid);
  3081. rsp->result = cpu_to_le16(result);
  3082. rsp->flags = cpu_to_le16(0x0000);
  3083. return ptr - data;
  3084. }
  3085. static int l2cap_parse_conf_rsp(struct sock *sk, void *rsp, int len, void *data, size_t size, u16 *result)
  3086. {
  3087. struct l2cap_pinfo *pi = l2cap_pi(sk);
  3088. struct l2cap_conf_req *req = data;
  3089. void *ptr = req->data;
  3090. void *endptr = data + size;
  3091. int type, olen;
  3092. unsigned long val;
  3093. struct l2cap_conf_rfc rfc;
  3094. BT_DBG("sk %p, rsp %p, len %d, req %p", sk, rsp, len, data);
  3095. /* Initialize rfc in case no rfc option is received */
  3096. rfc.mode = pi->mode;
  3097. rfc.retrans_timeout = cpu_to_le16(L2CAP_DEFAULT_RETRANS_TO);
  3098. rfc.monitor_timeout = cpu_to_le16(L2CAP_DEFAULT_MONITOR_TO);
  3099. rfc.max_pdu_size = cpu_to_le16(L2CAP_DEFAULT_MAX_PDU_SIZE);
  3100. while (len >= L2CAP_CONF_OPT_SIZE) {
  3101. len -= l2cap_get_conf_opt(&rsp, &type, &olen, &val);
  3102. if (len < 0)
  3103. break;
  3104. switch (type) {
  3105. case L2CAP_CONF_MTU:
  3106. if (olen != 2)
  3107. break;
  3108. if (val < L2CAP_DEFAULT_MIN_MTU) {
  3109. *result = L2CAP_CONF_UNACCEPT;
  3110. pi->imtu = L2CAP_DEFAULT_MIN_MTU;
  3111. } else
  3112. pi->imtu = val;
  3113. l2cap_add_conf_opt(&ptr, L2CAP_CONF_MTU, 2, pi->imtu,
  3114. endptr - ptr);
  3115. break;
  3116. case L2CAP_CONF_FLUSH_TO:
  3117. if (olen != 2)
  3118. break;
  3119. pi->flush_to = val;
  3120. l2cap_add_conf_opt(&ptr, L2CAP_CONF_FLUSH_TO, 2,
  3121. pi->flush_to, endptr - ptr);
  3122. break;
  3123. case L2CAP_CONF_RFC:
  3124. if (olen != sizeof(rfc))
  3125. break;
  3126. memcpy(&rfc, (void *)val, olen);
  3127. if ((pi->conf_state & L2CAP_CONF_STATE2_DEVICE) &&
  3128. rfc.mode != pi->mode)
  3129. return -ECONNREFUSED;
  3130. pi->fcs = 0;
  3131. l2cap_add_conf_opt(&ptr, L2CAP_CONF_RFC, sizeof(rfc),
  3132. (unsigned long) &rfc, endptr - ptr);
  3133. break;
  3134. case L2CAP_CONF_EXT_WINDOW:
  3135. if (olen != 2)
  3136. break;
  3137. pi->ack_win = min_t(u16, val, pi->ack_win);
  3138. l2cap_add_conf_opt(&ptr, L2CAP_CONF_EXT_WINDOW,
  3139. 2, pi->tx_win, endptr - ptr);
  3140. break;
  3141. default:
  3142. break;
  3143. }
  3144. }
  3145. if (pi->mode == L2CAP_MODE_BASIC && pi->mode != rfc.mode)
  3146. return -ECONNREFUSED;
  3147. pi->mode = rfc.mode;
  3148. if (*result == L2CAP_CONF_SUCCESS) {
  3149. switch (rfc.mode) {
  3150. case L2CAP_MODE_ERTM:
  3151. pi->retrans_timeout = le16_to_cpu(rfc.retrans_timeout);
  3152. pi->monitor_timeout = le16_to_cpu(rfc.monitor_timeout);
  3153. pi->mps = le16_to_cpu(rfc.max_pdu_size);
  3154. if (!pi->extended_control) {
  3155. pi->ack_win = min_t(u16, pi->ack_win,
  3156. rfc.txwin_size);
  3157. }
  3158. break;
  3159. case L2CAP_MODE_STREAMING:
  3160. pi->mps = le16_to_cpu(rfc.max_pdu_size);
  3161. }
  3162. }
  3163. req->dcid = cpu_to_le16(pi->dcid);
  3164. req->flags = cpu_to_le16(0x0000);
  3165. return ptr - data;
  3166. }
  3167. static int l2cap_build_conf_rsp(struct sock *sk, void *data, u16 result, u16 flags)
  3168. {
  3169. struct l2cap_conf_rsp *rsp = data;
  3170. void *ptr = rsp->data;
  3171. BT_DBG("sk %p", sk);
  3172. rsp->scid = cpu_to_le16(l2cap_pi(sk)->dcid);
  3173. rsp->result = cpu_to_le16(result);
  3174. rsp->flags = cpu_to_le16(flags);
  3175. return ptr - data;
  3176. }
  3177. static void l2cap_conf_rfc_get(struct sock *sk, void *rsp, int len)
  3178. {
  3179. struct l2cap_pinfo *pi = l2cap_pi(sk);
  3180. int type, olen;
  3181. unsigned long val;
  3182. struct l2cap_conf_rfc rfc;
  3183. u16 txwin_ext = pi->ack_win;
  3184. BT_DBG("sk %p, rsp %p, len %d", sk, rsp, len);
  3185. /* Initialize rfc in case no rfc option is received */
  3186. rfc.mode = pi->mode;
  3187. rfc.retrans_timeout = cpu_to_le16(L2CAP_DEFAULT_RETRANS_TO);
  3188. rfc.monitor_timeout = cpu_to_le16(L2CAP_DEFAULT_MONITOR_TO);
  3189. rfc.max_pdu_size = cpu_to_le16(L2CAP_DEFAULT_MAX_PDU_SIZE);
  3190. rfc.txwin_size = min_t(u16, pi->ack_win, L2CAP_DEFAULT_TX_WINDOW);
  3191. if ((pi->mode != L2CAP_MODE_ERTM) && (pi->mode != L2CAP_MODE_STREAMING))
  3192. return;
  3193. while (len >= L2CAP_CONF_OPT_SIZE) {
  3194. len -= l2cap_get_conf_opt(&rsp, &type, &olen, &val);
  3195. if (len < 0)
  3196. break;
  3197. switch (type) {
  3198. case L2CAP_CONF_RFC:
  3199. if (olen != sizeof(rfc))
  3200. break;
  3201. memcpy(&rfc, (void *)val, olen);
  3202. break;
  3203. case L2CAP_CONF_EXT_WINDOW:
  3204. if (olen != 2)
  3205. break;
  3206. txwin_ext = val;
  3207. break;
  3208. }
  3209. }
  3210. switch (rfc.mode) {
  3211. case L2CAP_MODE_ERTM:
  3212. pi->retrans_timeout = le16_to_cpu(rfc.retrans_timeout);
  3213. pi->monitor_timeout = le16_to_cpu(rfc.monitor_timeout);
  3214. pi->mps = le16_to_cpu(rfc.max_pdu_size);
  3215. if (pi->extended_control)
  3216. pi->ack_win = min_t(u16, pi->ack_win, txwin_ext);
  3217. else
  3218. pi->ack_win = min_t(u16, pi->ack_win, rfc.txwin_size);
  3219. break;
  3220. case L2CAP_MODE_STREAMING:
  3221. pi->mps = le16_to_cpu(rfc.max_pdu_size);
  3222. }
  3223. }
  3224. static void l2cap_conf_ext_fs_get(struct sock *sk, void *rsp, int len)
  3225. {
  3226. struct l2cap_pinfo *pi = l2cap_pi(sk);
  3227. int type, olen;
  3228. unsigned long val;
  3229. struct l2cap_conf_ext_fs fs;
  3230. BT_DBG("sk %p, rsp %p, len %d", sk, rsp, len);
  3231. while (len >= L2CAP_CONF_OPT_SIZE) {
  3232. len -= l2cap_get_conf_opt(&rsp, &type, &olen, &val);
  3233. if ((type == L2CAP_CONF_EXT_FS) &&
  3234. (olen == sizeof(struct l2cap_conf_ext_fs))) {
  3235. memcpy(&fs, (void *)val, olen);
  3236. pi->local_fs.id = fs.id;
  3237. pi->local_fs.type = fs.type;
  3238. pi->local_fs.max_sdu = le16_to_cpu(fs.max_sdu);
  3239. pi->local_fs.sdu_arr_time =
  3240. le32_to_cpu(fs.sdu_arr_time);
  3241. pi->local_fs.acc_latency = le32_to_cpu(fs.acc_latency);
  3242. pi->local_fs.flush_to = le32_to_cpu(fs.flush_to);
  3243. break;
  3244. }
  3245. }
  3246. }
  3247. static int l2cap_finish_amp_move(struct sock *sk)
  3248. {
  3249. struct l2cap_pinfo *pi;
  3250. int err;
  3251. BT_DBG("sk %p", sk);
  3252. pi = l2cap_pi(sk);
  3253. pi->amp_move_role = L2CAP_AMP_MOVE_NONE;
  3254. pi->rx_state = L2CAP_ERTM_RX_STATE_RECV;
  3255. if (pi->ampcon)
  3256. pi->conn->mtu = pi->ampcon->hdev->acl_mtu;
  3257. else
  3258. pi->conn->mtu = pi->conn->hcon->hdev->acl_mtu;
  3259. err = l2cap_setup_resegment(sk);
  3260. return err;
  3261. }
  3262. static int l2cap_amp_move_reconf_rsp(struct sock *sk, void *rsp, int len,
  3263. u16 result)
  3264. {
  3265. int err = 0;
  3266. struct l2cap_conf_rfc rfc = {.mode = L2CAP_MODE_BASIC};
  3267. struct l2cap_pinfo *pi = l2cap_pi(sk);
  3268. BT_DBG("sk %p, rsp %p, len %d, res 0x%2.2x", sk, rsp, len, result);
  3269. if (pi->reconf_state == L2CAP_RECONF_NONE)
  3270. return -ECONNREFUSED;
  3271. if (result == L2CAP_CONF_SUCCESS) {
  3272. while (len >= L2CAP_CONF_OPT_SIZE) {
  3273. int type, olen;
  3274. unsigned long val;
  3275. len -= l2cap_get_conf_opt(&rsp, &type, &olen, &val);
  3276. if (type == L2CAP_CONF_RFC) {
  3277. if (olen == sizeof(rfc))
  3278. memcpy(&rfc, (void *)val, olen);
  3279. if (rfc.mode != pi->mode) {
  3280. l2cap_send_disconn_req(pi->conn, sk,
  3281. ECONNRESET);
  3282. return -ECONNRESET;
  3283. }
  3284. goto done;
  3285. }
  3286. }
  3287. }
  3288. BT_ERR("Expected RFC option was missing, using existing values");
  3289. rfc.mode = pi->mode;
  3290. rfc.retrans_timeout = cpu_to_le16(pi->retrans_timeout);
  3291. rfc.monitor_timeout = cpu_to_le16(pi->monitor_timeout);
  3292. done:
  3293. l2cap_ertm_stop_ack_timer(pi);
  3294. l2cap_ertm_stop_retrans_timer(pi);
  3295. l2cap_ertm_stop_monitor_timer(pi);
  3296. pi->mps = le16_to_cpu(rfc.max_pdu_size);
  3297. if (pi->mode == L2CAP_MODE_ERTM) {
  3298. pi->retrans_timeout = le16_to_cpu(rfc.retrans_timeout);
  3299. pi->monitor_timeout = le16_to_cpu(rfc.monitor_timeout);
  3300. }
  3301. if (l2cap_pi(sk)->reconf_state == L2CAP_RECONF_ACC) {
  3302. l2cap_pi(sk)->reconf_state = L2CAP_RECONF_NONE;
  3303. /* Respond to poll */
  3304. err = l2cap_answer_move_poll(sk);
  3305. } else if (l2cap_pi(sk)->reconf_state == L2CAP_RECONF_INT) {
  3306. if (pi->mode == L2CAP_MODE_ERTM) {
  3307. l2cap_ertm_tx(sk, NULL, NULL,
  3308. L2CAP_ERTM_EVENT_EXPLICIT_POLL);
  3309. pi->rx_state = L2CAP_ERTM_RX_STATE_WAIT_F_FLAG;
  3310. }
  3311. }
  3312. return err;
  3313. }
  3314. static inline int l2cap_command_rej(struct l2cap_conn *conn,
  3315. struct l2cap_cmd_hdr *cmd, u16 cmd_len,
  3316. u8 *data)
  3317. {
  3318. struct l2cap_cmd_rej *rej = (struct l2cap_cmd_rej *) data;
  3319. if (cmd_len < sizeof(*rej))
  3320. return -EPROTO;
  3321. if (rej->reason != 0x0000)
  3322. return 0;
  3323. if ((conn->info_state & L2CAP_INFO_FEAT_MASK_REQ_SENT) &&
  3324. cmd->ident == conn->info_ident) {
  3325. del_timer(&conn->info_timer);
  3326. conn->info_state |= L2CAP_INFO_FEAT_MASK_REQ_DONE;
  3327. conn->info_ident = 0;
  3328. l2cap_conn_start(conn);
  3329. }
  3330. return 0;
  3331. }
  3332. static struct sock *l2cap_create_connect(struct l2cap_conn *conn,
  3333. struct l2cap_cmd_hdr *cmd,
  3334. u16 cmd_len, u8 *data,
  3335. u8 rsp_code,
  3336. u8 amp_id)
  3337. {
  3338. struct l2cap_chan_list *list = &conn->chan_list;
  3339. struct l2cap_conn_req *req = (struct l2cap_conn_req *) data;
  3340. struct l2cap_conn_rsp rsp;
  3341. struct sock *parent, *sk = NULL;
  3342. int result, status = L2CAP_CS_NO_INFO;
  3343. u16 dcid = 0, scid;
  3344. __le16 psm;
  3345. if (cmd_len < sizeof(struct l2cap_conn_req))
  3346. return NULL;
  3347. scid = __le16_to_cpu(req->scid);
  3348. psm = req->psm;
  3349. BT_DBG("psm 0x%2.2x scid 0x%4.4x", psm, scid);
  3350. /* Check if we have socket listening on psm */
  3351. parent = l2cap_get_sock_by_psm(BT_LISTEN, psm, conn->src);
  3352. if (!parent) {
  3353. result = L2CAP_CR_BAD_PSM;
  3354. goto sendresp;
  3355. }
  3356. bh_lock_sock(parent);
  3357. /* Check if the ACL is secure enough (if not SDP) */
  3358. if (psm != cpu_to_le16(0x0001) &&
  3359. !hci_conn_check_link_mode(conn->hcon)) {
  3360. conn->disc_reason = 0x05;
  3361. result = L2CAP_CR_SEC_BLOCK;
  3362. goto response;
  3363. }
  3364. result = L2CAP_CR_NO_MEM;
  3365. /* Check for backlog size */
  3366. if (sk_acceptq_is_full(parent)) {
  3367. BT_DBG("backlog full %d", parent->sk_ack_backlog);
  3368. goto response;
  3369. }
  3370. sk = l2cap_sock_alloc(sock_net(parent), NULL, BTPROTO_L2CAP, GFP_ATOMIC);
  3371. if (!sk)
  3372. goto response;
  3373. write_lock_bh(&list->lock);
  3374. /* Check if we already have channel with that dcid */
  3375. if (__l2cap_get_chan_by_dcid(list, scid)) {
  3376. write_unlock_bh(&list->lock);
  3377. sock_set_flag(sk, SOCK_ZAPPED);
  3378. l2cap_sock_kill(sk);
  3379. sk = NULL;
  3380. goto response;
  3381. }
  3382. hci_conn_hold(conn->hcon);
  3383. l2cap_sock_init(sk, parent);
  3384. bacpy(&bt_sk(sk)->src, conn->src);
  3385. bacpy(&bt_sk(sk)->dst, conn->dst);
  3386. l2cap_pi(sk)->psm = psm;
  3387. l2cap_pi(sk)->dcid = scid;
  3388. bt_accept_enqueue(parent, sk);
  3389. __l2cap_chan_add(conn, sk);
  3390. dcid = l2cap_pi(sk)->scid;
  3391. l2cap_pi(sk)->amp_id = amp_id;
  3392. l2cap_sock_set_timer(sk, sk->sk_sndtimeo);
  3393. l2cap_pi(sk)->ident = cmd->ident;
  3394. if (conn->info_state & L2CAP_INFO_FEAT_MASK_REQ_DONE) {
  3395. if (l2cap_check_security(sk)) {
  3396. if (bt_sk(sk)->defer_setup) {
  3397. sk->sk_state = BT_CONNECT2;
  3398. result = L2CAP_CR_PEND;
  3399. status = L2CAP_CS_AUTHOR_PEND;
  3400. parent->sk_data_ready(parent, 0);
  3401. } else {
  3402. /* Force pending result for AMP controllers.
  3403. * The connection will succeed after the
  3404. * physical link is up. */
  3405. if (amp_id) {
  3406. sk->sk_state = BT_CONNECT2;
  3407. result = L2CAP_CR_PEND;
  3408. } else {
  3409. sk->sk_state = BT_CONFIG;
  3410. result = L2CAP_CR_SUCCESS;
  3411. }
  3412. status = L2CAP_CS_NO_INFO;
  3413. }
  3414. } else {
  3415. sk->sk_state = BT_CONNECT2;
  3416. result = L2CAP_CR_PEND;
  3417. status = L2CAP_CS_AUTHEN_PEND;
  3418. }
  3419. } else {
  3420. sk->sk_state = BT_CONNECT2;
  3421. result = L2CAP_CR_PEND;
  3422. status = L2CAP_CS_NO_INFO;
  3423. }
  3424. write_unlock_bh(&list->lock);
  3425. response:
  3426. bh_unlock_sock(parent);
  3427. sendresp:
  3428. rsp.scid = cpu_to_le16(scid);
  3429. rsp.dcid = cpu_to_le16(dcid);
  3430. rsp.result = cpu_to_le16(result);
  3431. rsp.status = cpu_to_le16(status);
  3432. l2cap_send_cmd(conn, cmd->ident, rsp_code, sizeof(rsp), &rsp);
  3433. if (!(conn->info_state & L2CAP_INFO_FEAT_MASK_REQ_DONE)) {
  3434. struct l2cap_info_req info;
  3435. info.type = cpu_to_le16(L2CAP_IT_FEAT_MASK);
  3436. conn->info_state |= L2CAP_INFO_FEAT_MASK_REQ_SENT;
  3437. conn->info_ident = l2cap_get_ident(conn);
  3438. mod_timer(&conn->info_timer, jiffies +
  3439. msecs_to_jiffies(L2CAP_INFO_TIMEOUT));
  3440. l2cap_send_cmd(conn, conn->info_ident,
  3441. L2CAP_INFO_REQ, sizeof(info), &info);
  3442. }
  3443. if (sk && !(l2cap_pi(sk)->conf_state & L2CAP_CONF_REQ_SENT) &&
  3444. result == L2CAP_CR_SUCCESS) {
  3445. u8 buf[128];
  3446. l2cap_pi(sk)->conf_state |= L2CAP_CONF_REQ_SENT;
  3447. l2cap_send_cmd(conn, l2cap_get_ident(conn), L2CAP_CONF_REQ,
  3448. l2cap_build_conf_req(sk, buf, sizeof(buf)), buf);
  3449. l2cap_pi(sk)->num_conf_req++;
  3450. }
  3451. return sk;
  3452. }
  3453. static inline int l2cap_connect_req(struct l2cap_conn *conn,
  3454. struct l2cap_cmd_hdr *cmd, u16 cmd_len, u8 *data)
  3455. {
  3456. if (!l2cap_create_connect(conn, cmd, cmd_len, data, L2CAP_CONN_RSP, 0))
  3457. return -EPROTO;
  3458. else
  3459. return 0;
  3460. }
  3461. static inline int l2cap_connect_rsp(struct l2cap_conn *conn,
  3462. struct l2cap_cmd_hdr *cmd, u16 cmd_len,
  3463. u8 *data)
  3464. {
  3465. struct l2cap_conn_rsp *rsp = (struct l2cap_conn_rsp *) data;
  3466. u16 scid, dcid, result, status;
  3467. struct sock *sk;
  3468. u8 req[128];
  3469. if (cmd_len < sizeof(*rsp))
  3470. return -EPROTO;
  3471. scid = __le16_to_cpu(rsp->scid);
  3472. dcid = __le16_to_cpu(rsp->dcid);
  3473. result = __le16_to_cpu(rsp->result);
  3474. status = __le16_to_cpu(rsp->status);
  3475. BT_DBG("dcid 0x%4.4x scid 0x%4.4x result 0x%2.2x status 0x%2.2x", dcid, scid, result, status);
  3476. if (scid) {
  3477. sk = l2cap_get_chan_by_scid(&conn->chan_list, scid);
  3478. if (!sk)
  3479. return -EFAULT;
  3480. } else {
  3481. sk = l2cap_get_chan_by_ident(&conn->chan_list, cmd->ident);
  3482. if (!sk)
  3483. return -EFAULT;
  3484. }
  3485. switch (result) {
  3486. case L2CAP_CR_SUCCESS:
  3487. sk->sk_state = BT_CONFIG;
  3488. l2cap_pi(sk)->ident = 0;
  3489. l2cap_pi(sk)->dcid = dcid;
  3490. l2cap_pi(sk)->conf_state &= ~L2CAP_CONF_CONNECT_PEND;
  3491. if (l2cap_pi(sk)->conf_state & L2CAP_CONF_REQ_SENT)
  3492. break;
  3493. l2cap_pi(sk)->conf_state |= L2CAP_CONF_REQ_SENT;
  3494. l2cap_send_cmd(conn, l2cap_get_ident(conn), L2CAP_CONF_REQ,
  3495. l2cap_build_conf_req(sk, req, sizeof(req)), req);
  3496. l2cap_pi(sk)->num_conf_req++;
  3497. break;
  3498. case L2CAP_CR_PEND:
  3499. l2cap_pi(sk)->conf_state |= L2CAP_CONF_CONNECT_PEND;
  3500. break;
  3501. default:
  3502. /* don't delete l2cap channel if sk is owned by user */
  3503. if (sock_owned_by_user(sk)) {
  3504. sk->sk_state = BT_DISCONN;
  3505. l2cap_sock_clear_timer(sk);
  3506. l2cap_sock_set_timer(sk, HZ / 5);
  3507. break;
  3508. }
  3509. l2cap_chan_del(sk, ECONNREFUSED);
  3510. break;
  3511. }
  3512. bh_unlock_sock(sk);
  3513. return 0;
  3514. }
  3515. static inline void set_default_fcs(struct l2cap_pinfo *pi)
  3516. {
  3517. /* FCS is enabled only in ERTM or streaming mode, if one or both
  3518. * sides request it.
  3519. */
  3520. if (pi->mode != L2CAP_MODE_ERTM && pi->mode != L2CAP_MODE_STREAMING)
  3521. pi->fcs = L2CAP_FCS_NONE;
  3522. else if (!(pi->conf_state & L2CAP_CONF_NO_FCS_RECV))
  3523. pi->fcs = L2CAP_FCS_CRC16;
  3524. }
  3525. static inline int l2cap_config_req(struct l2cap_conn *conn, struct l2cap_cmd_hdr *cmd, u16 cmd_len, u8 *data)
  3526. {
  3527. struct l2cap_conf_req *req = (struct l2cap_conf_req *) data;
  3528. u16 dcid, flags;
  3529. u8 rspbuf[64];
  3530. struct l2cap_conf_rsp *rsp = (struct l2cap_conf_rsp *) rspbuf;
  3531. struct sock *sk;
  3532. int len;
  3533. u8 amp_move_reconf = 0;
  3534. if (cmd_len < sizeof(*req))
  3535. return -EPROTO;
  3536. dcid = __le16_to_cpu(req->dcid);
  3537. flags = __le16_to_cpu(req->flags);
  3538. BT_DBG("dcid 0x%4.4x flags 0x%2.2x", dcid, flags);
  3539. sk = l2cap_get_chan_by_scid(&conn->chan_list, dcid);
  3540. if (!sk)
  3541. return -ENOENT;
  3542. BT_DBG("sk_state 0x%2.2x rx_state 0x%2.2x "
  3543. "reconf_state 0x%2.2x amp_id 0x%2.2x amp_move_id 0x%2.2x",
  3544. sk->sk_state, l2cap_pi(sk)->rx_state,
  3545. l2cap_pi(sk)->reconf_state, l2cap_pi(sk)->amp_id,
  3546. l2cap_pi(sk)->amp_move_id);
  3547. /* Detect a reconfig request due to channel move between
  3548. * BR/EDR and AMP
  3549. */
  3550. if (sk->sk_state == BT_CONNECTED &&
  3551. l2cap_pi(sk)->rx_state ==
  3552. L2CAP_ERTM_RX_STATE_WAIT_P_FLAG_RECONFIGURE)
  3553. l2cap_pi(sk)->reconf_state = L2CAP_RECONF_ACC;
  3554. if (l2cap_pi(sk)->reconf_state != L2CAP_RECONF_NONE)
  3555. amp_move_reconf = 1;
  3556. if (sk->sk_state != BT_CONFIG && !amp_move_reconf) {
  3557. struct l2cap_cmd_rej rej;
  3558. rej.reason = cpu_to_le16(0x0002);
  3559. l2cap_send_cmd(conn, cmd->ident, L2CAP_COMMAND_REJ,
  3560. sizeof(rej), &rej);
  3561. goto unlock;
  3562. }
  3563. /* Reject if config buffer is too small. */
  3564. len = cmd_len - sizeof(*req);
  3565. if (l2cap_pi(sk)->conf_len + len > sizeof(l2cap_pi(sk)->conf_req)) {
  3566. l2cap_send_cmd(conn, cmd->ident, L2CAP_CONF_RSP,
  3567. l2cap_build_conf_rsp(sk, rspbuf,
  3568. L2CAP_CONF_REJECT, flags), rspbuf);
  3569. goto unlock;
  3570. }
  3571. /* Store config. */
  3572. memcpy(l2cap_pi(sk)->conf_req + l2cap_pi(sk)->conf_len, req->data, len);
  3573. l2cap_pi(sk)->conf_len += len;
  3574. if (flags & 0x0001) {
  3575. /* Incomplete config. Send empty response. */
  3576. l2cap_send_cmd(conn, cmd->ident, L2CAP_CONF_RSP,
  3577. l2cap_build_conf_rsp(sk, rspbuf,
  3578. L2CAP_CONF_SUCCESS, 0x0001), rspbuf);
  3579. goto unlock;
  3580. }
  3581. /* Complete config. */
  3582. if (!amp_move_reconf)
  3583. len = l2cap_parse_conf_req(sk, rspbuf, sizeof(rspbuf));
  3584. else
  3585. len = l2cap_parse_amp_move_reconf_req(sk, rspbuf, sizeof(rspbuf));
  3586. if (len < 0) {
  3587. l2cap_send_disconn_req(conn, sk, ECONNRESET);
  3588. goto unlock;
  3589. }
  3590. l2cap_pi(sk)->conf_ident = cmd->ident;
  3591. l2cap_send_cmd(conn, cmd->ident, L2CAP_CONF_RSP, len, rspbuf);
  3592. if (l2cap_pi(sk)->conf_state & L2CAP_CONF_LOCKSTEP &&
  3593. rsp->result == cpu_to_le16(L2CAP_CONF_PENDING) &&
  3594. !l2cap_pi(sk)->amp_id) {
  3595. /* Send success response right after pending if using
  3596. * lockstep config on BR/EDR
  3597. */
  3598. rsp->result = cpu_to_le16(L2CAP_CONF_SUCCESS);
  3599. l2cap_pi(sk)->conf_state |= L2CAP_CONF_OUTPUT_DONE;
  3600. l2cap_send_cmd(conn, cmd->ident, L2CAP_CONF_RSP, len, rspbuf);
  3601. }
  3602. /* Reset config buffer. */
  3603. l2cap_pi(sk)->conf_len = 0;
  3604. if (amp_move_reconf)
  3605. goto unlock;
  3606. l2cap_pi(sk)->num_conf_rsp++;
  3607. if (!(l2cap_pi(sk)->conf_state & L2CAP_CONF_OUTPUT_DONE))
  3608. goto unlock;
  3609. if (l2cap_pi(sk)->conf_state & L2CAP_CONF_INPUT_DONE) {
  3610. set_default_fcs(l2cap_pi(sk));
  3611. sk->sk_state = BT_CONNECTED;
  3612. if (l2cap_pi(sk)->mode == L2CAP_MODE_ERTM ||
  3613. l2cap_pi(sk)->mode == L2CAP_MODE_STREAMING)
  3614. l2cap_ertm_init(sk);
  3615. l2cap_chan_ready(sk);
  3616. goto unlock;
  3617. }
  3618. if (!(l2cap_pi(sk)->conf_state & L2CAP_CONF_REQ_SENT)) {
  3619. u8 buf[64];
  3620. l2cap_pi(sk)->conf_state |= L2CAP_CONF_REQ_SENT;
  3621. l2cap_send_cmd(conn, l2cap_get_ident(conn), L2CAP_CONF_REQ,
  3622. l2cap_build_conf_req(sk, buf, sizeof(buf)), buf);
  3623. l2cap_pi(sk)->num_conf_req++;
  3624. }
  3625. unlock:
  3626. bh_unlock_sock(sk);
  3627. return 0;
  3628. }
  3629. static inline int l2cap_config_rsp(struct l2cap_conn *conn,
  3630. struct l2cap_cmd_hdr *cmd, u16 cmd_len,
  3631. u8 *data)
  3632. {
  3633. struct l2cap_conf_rsp *rsp = (struct l2cap_conf_rsp *)data;
  3634. u16 scid, flags, result;
  3635. struct sock *sk;
  3636. struct l2cap_pinfo *pi;
  3637. int len = cmd->len - sizeof(*rsp);
  3638. if (cmd_len != sizeof(*rsp))
  3639. return -EPROTO;
  3640. scid = __le16_to_cpu(rsp->scid);
  3641. flags = __le16_to_cpu(rsp->flags);
  3642. result = __le16_to_cpu(rsp->result);
  3643. BT_DBG("scid 0x%4.4x flags 0x%2.2x result 0x%2.2x",
  3644. scid, flags, result);
  3645. sk = l2cap_get_chan_by_scid(&conn->chan_list, scid);
  3646. if (!sk)
  3647. return 0;
  3648. pi = l2cap_pi(sk);
  3649. if (pi->reconf_state != L2CAP_RECONF_NONE) {
  3650. l2cap_amp_move_reconf_rsp(sk, rsp->data, len, result);
  3651. goto done;
  3652. }
  3653. switch (result) {
  3654. case L2CAP_CONF_SUCCESS:
  3655. if (pi->conf_state & L2CAP_CONF_LOCKSTEP &&
  3656. !(pi->conf_state & L2CAP_CONF_LOCKSTEP_PEND)) {
  3657. /* Lockstep procedure requires a pending response
  3658. * before success.
  3659. */
  3660. l2cap_send_disconn_req(conn, sk, ECONNRESET);
  3661. goto done;
  3662. }
  3663. l2cap_conf_rfc_get(sk, rsp->data, len);
  3664. break;
  3665. case L2CAP_CONF_PENDING:
  3666. if (!(pi->conf_state & L2CAP_CONF_LOCKSTEP)) {
  3667. l2cap_send_disconn_req(conn, sk, ECONNRESET);
  3668. goto done;
  3669. }
  3670. l2cap_conf_rfc_get(sk, rsp->data, len);
  3671. pi->conf_state |= L2CAP_CONF_LOCKSTEP_PEND;
  3672. l2cap_conf_ext_fs_get(sk, rsp->data, len);
  3673. if (pi->amp_id && pi->conf_state & L2CAP_CONF_PEND_SENT) {
  3674. struct hci_chan *chan;
  3675. /* Already sent a 'pending' response, so set up
  3676. * the logical link now
  3677. */
  3678. chan = l2cap_chan_admit(pi->amp_id, sk);
  3679. if (!chan) {
  3680. l2cap_send_disconn_req(pi->conn, sk,
  3681. ECONNRESET);
  3682. goto done;
  3683. }
  3684. if (chan->state == BT_CONNECTED)
  3685. l2cap_create_cfm(chan, 0);
  3686. }
  3687. goto done;
  3688. case L2CAP_CONF_UNACCEPT:
  3689. if (pi->num_conf_rsp <= L2CAP_CONF_MAX_CONF_RSP) {
  3690. char req[64];
  3691. if (len > sizeof(req) - sizeof(struct l2cap_conf_req)) {
  3692. l2cap_send_disconn_req(conn, sk, ECONNRESET);
  3693. goto done;
  3694. }
  3695. /* throw out any old stored conf requests */
  3696. result = L2CAP_CONF_SUCCESS;
  3697. len = l2cap_parse_conf_rsp(sk, rsp->data,
  3698. len, req, sizeof(req), &result);
  3699. if (len < 0) {
  3700. l2cap_send_disconn_req(conn, sk, ECONNRESET);
  3701. goto done;
  3702. }
  3703. l2cap_send_cmd(conn, l2cap_get_ident(conn),
  3704. L2CAP_CONF_REQ, len, req);
  3705. pi->num_conf_req++;
  3706. if (result != L2CAP_CONF_SUCCESS)
  3707. goto done;
  3708. break;
  3709. }
  3710. default:
  3711. sk->sk_err = ECONNRESET;
  3712. l2cap_sock_set_timer(sk, HZ * 5);
  3713. l2cap_send_disconn_req(conn, sk, ECONNRESET);
  3714. goto done;
  3715. }
  3716. if (flags & 0x01)
  3717. goto done;
  3718. pi->conf_state |= L2CAP_CONF_INPUT_DONE;
  3719. if (pi->conf_state & L2CAP_CONF_OUTPUT_DONE) {
  3720. set_default_fcs(pi);
  3721. sk->sk_state = BT_CONNECTED;
  3722. if (pi->mode == L2CAP_MODE_ERTM ||
  3723. pi->mode == L2CAP_MODE_STREAMING)
  3724. l2cap_ertm_init(sk);
  3725. l2cap_chan_ready(sk);
  3726. }
  3727. done:
  3728. bh_unlock_sock(sk);
  3729. return 0;
  3730. }
  3731. static inline int l2cap_disconnect_req(struct l2cap_conn *conn,
  3732. struct l2cap_cmd_hdr *cmd, u16 cmd_len,
  3733. u8 *data)
  3734. {
  3735. struct l2cap_disconn_req *req = (struct l2cap_disconn_req *) data;
  3736. struct l2cap_disconn_rsp rsp;
  3737. u16 dcid, scid;
  3738. struct sock *sk;
  3739. if (cmd_len != sizeof(rsp))
  3740. return -EPROTO;
  3741. scid = __le16_to_cpu(req->scid);
  3742. dcid = __le16_to_cpu(req->dcid);
  3743. BT_DBG("scid 0x%4.4x dcid 0x%4.4x", scid, dcid);
  3744. sk = l2cap_get_chan_by_scid(&conn->chan_list, dcid);
  3745. if (!sk)
  3746. return 0;
  3747. rsp.dcid = cpu_to_le16(l2cap_pi(sk)->scid);
  3748. rsp.scid = cpu_to_le16(l2cap_pi(sk)->dcid);
  3749. l2cap_send_cmd(conn, cmd->ident, L2CAP_DISCONN_RSP, sizeof(rsp), &rsp);
  3750. /* Only do cleanup if a disconnect request was not sent already */
  3751. if (sk->sk_state != BT_DISCONN) {
  3752. sk->sk_shutdown = SHUTDOWN_MASK;
  3753. sk->sk_send_head = NULL;
  3754. skb_queue_purge(TX_QUEUE(sk));
  3755. if (l2cap_pi(sk)->mode == L2CAP_MODE_ERTM) {
  3756. skb_queue_purge(SREJ_QUEUE(sk));
  3757. __cancel_delayed_work(&l2cap_pi(sk)->ack_work);
  3758. __cancel_delayed_work(&l2cap_pi(sk)->retrans_work);
  3759. __cancel_delayed_work(&l2cap_pi(sk)->monitor_work);
  3760. }
  3761. }
  3762. /* don't delete l2cap channel if sk is owned by user */
  3763. if (sock_owned_by_user(sk)) {
  3764. sk->sk_state = BT_DISCONN;
  3765. l2cap_sock_clear_timer(sk);
  3766. l2cap_sock_set_timer(sk, HZ / 5);
  3767. bh_unlock_sock(sk);
  3768. return 0;
  3769. }
  3770. l2cap_chan_del(sk, ECONNRESET);
  3771. bh_unlock_sock(sk);
  3772. l2cap_sock_kill(sk);
  3773. return 0;
  3774. }
  3775. static inline int l2cap_disconnect_rsp(struct l2cap_conn *conn,
  3776. struct l2cap_cmd_hdr *cmd, u16 cmd_len,
  3777. u8 *data)
  3778. {
  3779. struct l2cap_disconn_rsp *rsp = (struct l2cap_disconn_rsp *) data;
  3780. u16 dcid, scid;
  3781. struct sock *sk;
  3782. if (cmd_len != sizeof(*rsp))
  3783. return -EPROTO;
  3784. scid = __le16_to_cpu(rsp->scid);
  3785. dcid = __le16_to_cpu(rsp->dcid);
  3786. BT_DBG("dcid 0x%4.4x scid 0x%4.4x", dcid, scid);
  3787. sk = l2cap_get_chan_by_scid(&conn->chan_list, scid);
  3788. if (!sk)
  3789. return 0;
  3790. /* don't delete l2cap channel if sk is owned by user */
  3791. if (sock_owned_by_user(sk)) {
  3792. sk->sk_state = BT_DISCONN;
  3793. l2cap_sock_clear_timer(sk);
  3794. l2cap_sock_set_timer(sk, HZ / 5);
  3795. bh_unlock_sock(sk);
  3796. return 0;
  3797. }
  3798. l2cap_chan_del(sk, 0);
  3799. bh_unlock_sock(sk);
  3800. l2cap_sock_kill(sk);
  3801. return 0;
  3802. }
  3803. static inline int l2cap_information_req(struct l2cap_conn *conn,
  3804. struct l2cap_cmd_hdr *cmd, u16 cmd_len,
  3805. u8 *data)
  3806. {
  3807. struct l2cap_info_req *req = (struct l2cap_info_req *) data;
  3808. u16 type;
  3809. if (cmd_len != sizeof(*req))
  3810. return -EPROTO;
  3811. type = __le16_to_cpu(req->type);
  3812. BT_DBG("type 0x%4.4x", type);
  3813. if (type == L2CAP_IT_FEAT_MASK) {
  3814. u8 buf[8];
  3815. u32 feat_mask = l2cap_feat_mask;
  3816. struct l2cap_info_rsp *rsp = (struct l2cap_info_rsp *) buf;
  3817. rsp->type = cpu_to_le16(L2CAP_IT_FEAT_MASK);
  3818. rsp->result = cpu_to_le16(L2CAP_IR_SUCCESS);
  3819. if (!disable_ertm)
  3820. feat_mask |= L2CAP_FEAT_ERTM | L2CAP_FEAT_STREAMING
  3821. | L2CAP_FEAT_FCS | L2CAP_FEAT_EXT_WINDOW;
  3822. put_unaligned_le32(feat_mask, rsp->data);
  3823. l2cap_send_cmd(conn, cmd->ident,
  3824. L2CAP_INFO_RSP, sizeof(buf), buf);
  3825. } else if (type == L2CAP_IT_FIXED_CHAN) {
  3826. u8 buf[12];
  3827. u8 fc_mask = l2cap_fc_mask;
  3828. struct l2cap_info_rsp *rsp = (struct l2cap_info_rsp *) buf;
  3829. rsp->type = cpu_to_le16(L2CAP_IT_FIXED_CHAN);
  3830. rsp->result = cpu_to_le16(L2CAP_IR_SUCCESS);
  3831. if (enable_hs)
  3832. fc_mask |= L2CAP_FC_A2MP;
  3833. memset(rsp->data, 0, 8);
  3834. rsp->data[0] = fc_mask;
  3835. l2cap_send_cmd(conn, cmd->ident,
  3836. L2CAP_INFO_RSP, sizeof(buf), buf);
  3837. } else {
  3838. struct l2cap_info_rsp rsp;
  3839. rsp.type = cpu_to_le16(type);
  3840. rsp.result = cpu_to_le16(L2CAP_IR_NOTSUPP);
  3841. l2cap_send_cmd(conn, cmd->ident,
  3842. L2CAP_INFO_RSP, sizeof(rsp), &rsp);
  3843. }
  3844. return 0;
  3845. }
  3846. static inline int l2cap_information_rsp(struct l2cap_conn *conn,
  3847. struct l2cap_cmd_hdr *cmd, u16 cmd_len,
  3848. u8 *data)
  3849. {
  3850. struct l2cap_info_rsp *rsp = (struct l2cap_info_rsp *) data;
  3851. u16 type, result;
  3852. if (cmd_len < sizeof(*rsp))
  3853. return -EPROTO;
  3854. type = __le16_to_cpu(rsp->type);
  3855. result = __le16_to_cpu(rsp->result);
  3856. BT_DBG("type 0x%4.4x result 0x%2.2x", type, result);
  3857. /* L2CAP Info req/rsp are unbound to channels, add extra checks */
  3858. if (cmd->ident != conn->info_ident ||
  3859. conn->info_state & L2CAP_INFO_FEAT_MASK_REQ_DONE)
  3860. return 0;
  3861. del_timer(&conn->info_timer);
  3862. if (result != L2CAP_IR_SUCCESS) {
  3863. conn->info_state |= L2CAP_INFO_FEAT_MASK_REQ_DONE;
  3864. conn->info_ident = 0;
  3865. l2cap_conn_start(conn);
  3866. return 0;
  3867. }
  3868. if (type == L2CAP_IT_FEAT_MASK) {
  3869. conn->feat_mask = get_unaligned_le32(rsp->data);
  3870. if (conn->feat_mask & L2CAP_FEAT_FIXED_CHAN) {
  3871. struct l2cap_info_req req;
  3872. req.type = cpu_to_le16(L2CAP_IT_FIXED_CHAN);
  3873. conn->info_ident = l2cap_get_ident(conn);
  3874. l2cap_send_cmd(conn, conn->info_ident,
  3875. L2CAP_INFO_REQ, sizeof(req), &req);
  3876. } else {
  3877. conn->info_state |= L2CAP_INFO_FEAT_MASK_REQ_DONE;
  3878. conn->info_ident = 0;
  3879. l2cap_conn_start(conn);
  3880. }
  3881. } else if (type == L2CAP_IT_FIXED_CHAN) {
  3882. conn->fc_mask = rsp->data[0];
  3883. conn->info_state |= L2CAP_INFO_FEAT_MASK_REQ_DONE;
  3884. conn->info_ident = 0;
  3885. l2cap_conn_start(conn);
  3886. }
  3887. return 0;
  3888. }
  3889. static void l2cap_send_move_chan_req(struct l2cap_conn *conn,
  3890. struct l2cap_pinfo *pi, u16 icid, u8 dest_amp_id)
  3891. {
  3892. struct l2cap_move_chan_req req;
  3893. u8 ident;
  3894. BT_DBG("pi %p, icid %d, dest_amp_id %d", pi, (int) icid,
  3895. (int) dest_amp_id);
  3896. ident = l2cap_get_ident(conn);
  3897. if (pi)
  3898. pi->ident = ident;
  3899. req.icid = cpu_to_le16(icid);
  3900. req.dest_amp_id = dest_amp_id;
  3901. l2cap_send_cmd(conn, ident, L2CAP_MOVE_CHAN_REQ, sizeof(req), &req);
  3902. }
  3903. static void l2cap_send_move_chan_rsp(struct l2cap_conn *conn, u8 ident,
  3904. u16 icid, u16 result)
  3905. {
  3906. struct l2cap_move_chan_rsp rsp;
  3907. BT_DBG("icid %d, result %d", (int) icid, (int) result);
  3908. rsp.icid = cpu_to_le16(icid);
  3909. rsp.result = cpu_to_le16(result);
  3910. l2cap_send_cmd(conn, ident, L2CAP_MOVE_CHAN_RSP, sizeof(rsp), &rsp);
  3911. }
  3912. static void l2cap_send_move_chan_cfm(struct l2cap_conn *conn,
  3913. struct l2cap_pinfo *pi, u16 icid, u16 result)
  3914. {
  3915. struct l2cap_move_chan_cfm cfm;
  3916. u8 ident;
  3917. BT_DBG("icid %d, result %d", (int) icid, (int) result);
  3918. ident = l2cap_get_ident(conn);
  3919. if (pi)
  3920. pi->ident = ident;
  3921. cfm.icid = cpu_to_le16(icid);
  3922. cfm.result = cpu_to_le16(result);
  3923. l2cap_send_cmd(conn, ident, L2CAP_MOVE_CHAN_CFM, sizeof(cfm), &cfm);
  3924. }
  3925. static void l2cap_send_move_chan_cfm_rsp(struct l2cap_conn *conn, u8 ident,
  3926. u16 icid)
  3927. {
  3928. struct l2cap_move_chan_cfm_rsp rsp;
  3929. BT_DBG("icid %d", (int) icid);
  3930. rsp.icid = cpu_to_le16(icid);
  3931. l2cap_send_cmd(conn, ident, L2CAP_MOVE_CHAN_CFM_RSP, sizeof(rsp), &rsp);
  3932. }
  3933. static inline int l2cap_create_channel_req(struct l2cap_conn *conn,
  3934. struct l2cap_cmd_hdr *cmd, u16 cmd_len,
  3935. u8 *data)
  3936. {
  3937. struct l2cap_create_chan_req *req =
  3938. (struct l2cap_create_chan_req *) data;
  3939. struct sock *sk;
  3940. u16 psm, scid;
  3941. psm = le16_to_cpu(req->psm);
  3942. scid = le16_to_cpu(req->scid);
  3943. BT_DBG("psm %d, scid %d, amp_id %d", (int) psm, (int) scid,
  3944. (int) req->amp_id);
  3945. if (req->amp_id) {
  3946. struct hci_dev *hdev;
  3947. /* Validate AMP controller id */
  3948. hdev = hci_dev_get(req->amp_id);
  3949. if (!hdev || !test_bit(HCI_UP, &hdev->flags)) {
  3950. struct l2cap_create_chan_rsp rsp;
  3951. rsp.dcid = 0;
  3952. rsp.scid = cpu_to_le16(scid);
  3953. rsp.result = L2CAP_CREATE_CHAN_REFUSED_CONTROLLER;
  3954. rsp.status = L2CAP_CREATE_CHAN_STATUS_NONE;
  3955. l2cap_send_cmd(conn, cmd->ident, L2CAP_CREATE_CHAN_RSP,
  3956. sizeof(rsp), &rsp);
  3957. if (hdev)
  3958. hci_dev_put(hdev);
  3959. return 0;
  3960. }
  3961. hci_dev_put(hdev);
  3962. }
  3963. sk = l2cap_create_connect(conn, cmd, cmd_len, data, L2CAP_CREATE_CHAN_RSP,
  3964. req->amp_id);
  3965. if (sk)
  3966. l2cap_pi(sk)->conf_state |= L2CAP_CONF_LOCKSTEP;
  3967. if (sk && req->amp_id &&
  3968. (conn->info_state & L2CAP_INFO_FEAT_MASK_REQ_DONE))
  3969. amp_accept_physical(conn, req->amp_id, sk);
  3970. return 0;
  3971. }
  3972. static inline int l2cap_create_channel_rsp(struct l2cap_conn *conn,
  3973. struct l2cap_cmd_hdr *cmd, u16 cmd_len,
  3974. u8 *data)
  3975. {
  3976. BT_DBG("conn %p", conn);
  3977. return l2cap_connect_rsp(conn, cmd, cmd_len, data);
  3978. }
  3979. static inline int l2cap_move_channel_req(struct l2cap_conn *conn,
  3980. struct l2cap_cmd_hdr *cmd, u8 *data)
  3981. {
  3982. struct l2cap_move_chan_req *req = (struct l2cap_move_chan_req *) data;
  3983. struct sock *sk;
  3984. struct l2cap_pinfo *pi;
  3985. u16 icid = 0;
  3986. u16 result = L2CAP_MOVE_CHAN_REFUSED_NOT_ALLOWED;
  3987. icid = le16_to_cpu(req->icid);
  3988. BT_DBG("icid %d, dest_amp_id %d", (int) icid, (int) req->dest_amp_id);
  3989. read_lock(&conn->chan_list.lock);
  3990. sk = __l2cap_get_chan_by_dcid(&conn->chan_list, icid);
  3991. read_unlock(&conn->chan_list.lock);
  3992. if (!sk)
  3993. goto send_move_response;
  3994. lock_sock(sk);
  3995. pi = l2cap_pi(sk);
  3996. if (pi->scid < L2CAP_CID_DYN_START ||
  3997. (pi->mode != L2CAP_MODE_ERTM &&
  3998. pi->mode != L2CAP_MODE_STREAMING)) {
  3999. goto send_move_response;
  4000. }
  4001. if (pi->amp_id == req->dest_amp_id) {
  4002. result = L2CAP_MOVE_CHAN_REFUSED_SAME_ID;
  4003. goto send_move_response;
  4004. }
  4005. if (req->dest_amp_id) {
  4006. struct hci_dev *hdev;
  4007. hdev = hci_dev_get(req->dest_amp_id);
  4008. if (!hdev || !test_bit(HCI_UP, &hdev->flags)) {
  4009. if (hdev)
  4010. hci_dev_put(hdev);
  4011. result = L2CAP_MOVE_CHAN_REFUSED_CONTROLLER;
  4012. goto send_move_response;
  4013. }
  4014. hci_dev_put(hdev);
  4015. }
  4016. if (((pi->amp_move_state != L2CAP_AMP_STATE_STABLE &&
  4017. pi->amp_move_state != L2CAP_AMP_STATE_WAIT_PREPARE) ||
  4018. pi->amp_move_role != L2CAP_AMP_MOVE_NONE) &&
  4019. bacmp(conn->src, conn->dst) > 0) {
  4020. result = L2CAP_MOVE_CHAN_REFUSED_COLLISION;
  4021. goto send_move_response;
  4022. }
  4023. if (pi->amp_pref == BT_AMP_POLICY_REQUIRE_BR_EDR) {
  4024. result = L2CAP_MOVE_CHAN_REFUSED_NOT_ALLOWED;
  4025. goto send_move_response;
  4026. }
  4027. pi->amp_move_cmd_ident = cmd->ident;
  4028. pi->amp_move_role = L2CAP_AMP_MOVE_RESPONDER;
  4029. l2cap_amp_move_setup(sk);
  4030. pi->amp_move_id = req->dest_amp_id;
  4031. icid = pi->dcid;
  4032. if (req->dest_amp_id == 0) {
  4033. /* Moving to BR/EDR */
  4034. if (pi->conn_state & L2CAP_CONN_LOCAL_BUSY) {
  4035. pi->amp_move_state = L2CAP_AMP_STATE_WAIT_LOCAL_BUSY;
  4036. result = L2CAP_MOVE_CHAN_PENDING;
  4037. } else {
  4038. pi->amp_move_state = L2CAP_AMP_STATE_WAIT_MOVE_CONFIRM;
  4039. result = L2CAP_MOVE_CHAN_SUCCESS;
  4040. }
  4041. } else {
  4042. pi->amp_move_state = L2CAP_AMP_STATE_WAIT_PREPARE;
  4043. amp_accept_physical(pi->conn, req->dest_amp_id, sk);
  4044. result = L2CAP_MOVE_CHAN_PENDING;
  4045. }
  4046. send_move_response:
  4047. l2cap_send_move_chan_rsp(conn, cmd->ident, icid, result);
  4048. if (sk)
  4049. release_sock(sk);
  4050. return 0;
  4051. }
  4052. static inline int l2cap_move_channel_rsp(struct l2cap_conn *conn,
  4053. struct l2cap_cmd_hdr *cmd, u8 *data)
  4054. {
  4055. struct l2cap_move_chan_rsp *rsp = (struct l2cap_move_chan_rsp *) data;
  4056. struct sock *sk;
  4057. struct l2cap_pinfo *pi;
  4058. u16 icid, result;
  4059. icid = le16_to_cpu(rsp->icid);
  4060. result = le16_to_cpu(rsp->result);
  4061. BT_DBG("icid %d, result %d", (int) icid, (int) result);
  4062. switch (result) {
  4063. case L2CAP_MOVE_CHAN_SUCCESS:
  4064. case L2CAP_MOVE_CHAN_PENDING:
  4065. read_lock(&conn->chan_list.lock);
  4066. sk = __l2cap_get_chan_by_scid(&conn->chan_list, icid);
  4067. read_unlock(&conn->chan_list.lock);
  4068. if (!sk) {
  4069. l2cap_send_move_chan_cfm(conn, NULL, icid,
  4070. L2CAP_MOVE_CHAN_UNCONFIRMED);
  4071. break;
  4072. }
  4073. lock_sock(sk);
  4074. pi = l2cap_pi(sk);
  4075. l2cap_sock_clear_timer(sk);
  4076. if (result == L2CAP_MOVE_CHAN_PENDING)
  4077. l2cap_sock_set_timer(sk, L2CAP_MOVE_ERTX_TIMEOUT);
  4078. if (pi->amp_move_state ==
  4079. L2CAP_AMP_STATE_WAIT_LOGICAL_COMPLETE) {
  4080. /* Move confirm will be sent when logical link
  4081. * is complete.
  4082. */
  4083. pi->amp_move_state =
  4084. L2CAP_AMP_STATE_WAIT_LOGICAL_CONFIRM;
  4085. } else if (pi->amp_move_state ==
  4086. L2CAP_AMP_STATE_WAIT_MOVE_RSP_SUCCESS) {
  4087. if (result == L2CAP_MOVE_CHAN_PENDING) {
  4088. break;
  4089. } else if (pi->conn_state & L2CAP_CONN_LOCAL_BUSY) {
  4090. pi->amp_move_state =
  4091. L2CAP_AMP_STATE_WAIT_LOCAL_BUSY;
  4092. } else {
  4093. /* Logical link is up or moving to BR/EDR,
  4094. * proceed with move */
  4095. pi->amp_move_state =
  4096. L2CAP_AMP_STATE_WAIT_MOVE_CONFIRM_RSP;
  4097. l2cap_send_move_chan_cfm(conn, pi, pi->scid,
  4098. L2CAP_MOVE_CHAN_CONFIRMED);
  4099. l2cap_sock_set_timer(sk, L2CAP_MOVE_TIMEOUT);
  4100. }
  4101. } else if (pi->amp_move_state ==
  4102. L2CAP_AMP_STATE_WAIT_MOVE_RSP) {
  4103. struct l2cap_conf_ext_fs default_fs = {1, 1, 0xFFFF,
  4104. 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF};
  4105. struct hci_chan *chan;
  4106. /* Moving to AMP */
  4107. if (result == L2CAP_MOVE_CHAN_SUCCESS) {
  4108. /* Remote is ready, send confirm immediately
  4109. * after logical link is ready
  4110. */
  4111. pi->amp_move_state =
  4112. L2CAP_AMP_STATE_WAIT_LOGICAL_CONFIRM;
  4113. } else {
  4114. /* Both logical link and move success
  4115. * are required to confirm
  4116. */
  4117. pi->amp_move_state =
  4118. L2CAP_AMP_STATE_WAIT_LOGICAL_COMPLETE;
  4119. }
  4120. pi->remote_fs = default_fs;
  4121. pi->local_fs = default_fs;
  4122. chan = l2cap_chan_admit(pi->amp_move_id, sk);
  4123. if (!chan) {
  4124. /* Logical link not available */
  4125. l2cap_send_move_chan_cfm(conn, pi, pi->scid,
  4126. L2CAP_MOVE_CHAN_UNCONFIRMED);
  4127. break;
  4128. }
  4129. if (chan->state == BT_CONNECTED) {
  4130. /* Logical link is already ready to go */
  4131. pi->ampcon = chan->conn;
  4132. pi->ampcon->l2cap_data = pi->conn;
  4133. if (result == L2CAP_MOVE_CHAN_SUCCESS) {
  4134. /* Can confirm now */
  4135. l2cap_send_move_chan_cfm(conn, pi,
  4136. pi->scid,
  4137. L2CAP_MOVE_CHAN_CONFIRMED);
  4138. } else {
  4139. /* Now only need move success
  4140. * required to confirm
  4141. */
  4142. pi->amp_move_state =
  4143. L2CAP_AMP_STATE_WAIT_MOVE_RSP_SUCCESS;
  4144. }
  4145. l2cap_create_cfm(chan, 0);
  4146. }
  4147. } else {
  4148. /* Any other amp move state means the move failed. */
  4149. pi->amp_move_id = pi->amp_id;
  4150. pi->amp_move_state = L2CAP_AMP_STATE_STABLE;
  4151. l2cap_amp_move_revert(sk);
  4152. pi->amp_move_role = L2CAP_AMP_MOVE_NONE;
  4153. l2cap_send_move_chan_cfm(conn, pi, pi->scid,
  4154. L2CAP_MOVE_CHAN_UNCONFIRMED);
  4155. l2cap_sock_set_timer(sk, L2CAP_MOVE_TIMEOUT);
  4156. }
  4157. break;
  4158. default:
  4159. /* Failed (including collision case) */
  4160. read_lock(&conn->chan_list.lock);
  4161. sk = __l2cap_get_chan_by_ident(&conn->chan_list, cmd->ident);
  4162. read_unlock(&conn->chan_list.lock);
  4163. if (!sk) {
  4164. /* Could not locate channel, icid is best guess */
  4165. l2cap_send_move_chan_cfm(conn, NULL, icid,
  4166. L2CAP_MOVE_CHAN_UNCONFIRMED);
  4167. break;
  4168. }
  4169. lock_sock(sk);
  4170. pi = l2cap_pi(sk);
  4171. l2cap_sock_clear_timer(sk);
  4172. if (pi->amp_move_role == L2CAP_AMP_MOVE_INITIATOR) {
  4173. if (result == L2CAP_MOVE_CHAN_REFUSED_COLLISION)
  4174. pi->amp_move_role = L2CAP_AMP_MOVE_RESPONDER;
  4175. else {
  4176. /* Cleanup - cancel move */
  4177. pi->amp_move_id = pi->amp_id;
  4178. pi->amp_move_state = L2CAP_AMP_STATE_STABLE;
  4179. l2cap_amp_move_revert(sk);
  4180. pi->amp_move_role = L2CAP_AMP_MOVE_NONE;
  4181. }
  4182. }
  4183. l2cap_send_move_chan_cfm(conn, pi, pi->scid,
  4184. L2CAP_MOVE_CHAN_UNCONFIRMED);
  4185. l2cap_sock_set_timer(sk, L2CAP_MOVE_TIMEOUT);
  4186. break;
  4187. }
  4188. if (sk)
  4189. release_sock(sk);
  4190. return 0;
  4191. }
  4192. static inline int l2cap_move_channel_confirm(struct l2cap_conn *conn,
  4193. struct l2cap_cmd_hdr *cmd, u8 *data)
  4194. {
  4195. struct l2cap_move_chan_cfm *cfm = (struct l2cap_move_chan_cfm *) data;
  4196. struct sock *sk;
  4197. struct l2cap_pinfo *pi;
  4198. u16 icid, result;
  4199. icid = le16_to_cpu(cfm->icid);
  4200. result = le16_to_cpu(cfm->result);
  4201. BT_DBG("icid %d, result %d", (int) icid, (int) result);
  4202. read_lock(&conn->chan_list.lock);
  4203. sk = __l2cap_get_chan_by_dcid(&conn->chan_list, icid);
  4204. read_unlock(&conn->chan_list.lock);
  4205. if (!sk) {
  4206. BT_DBG("Bad channel (%d)", (int) icid);
  4207. goto send_move_confirm_response;
  4208. }
  4209. lock_sock(sk);
  4210. pi = l2cap_pi(sk);
  4211. if (pi->amp_move_state == L2CAP_AMP_STATE_WAIT_MOVE_CONFIRM) {
  4212. pi->amp_move_state = L2CAP_AMP_STATE_STABLE;
  4213. if (result == L2CAP_MOVE_CHAN_CONFIRMED) {
  4214. pi->amp_id = pi->amp_move_id;
  4215. if (!pi->amp_id && pi->ampchan) {
  4216. struct hci_chan *ampchan = pi->ampchan;
  4217. struct hci_conn *ampcon = pi->ampcon;
  4218. /* Have moved off of AMP, free the channel */
  4219. pi->ampchan = NULL;
  4220. pi->ampcon = NULL;
  4221. if (hci_chan_put(ampchan))
  4222. ampcon->l2cap_data = NULL;
  4223. else
  4224. l2cap_deaggregate(ampchan, pi);
  4225. }
  4226. l2cap_amp_move_success(sk);
  4227. } else {
  4228. pi->amp_move_id = pi->amp_id;
  4229. l2cap_amp_move_revert(sk);
  4230. }
  4231. pi->amp_move_role = L2CAP_AMP_MOVE_NONE;
  4232. } else if (pi->amp_move_state ==
  4233. L2CAP_AMP_STATE_WAIT_LOGICAL_CONFIRM) {
  4234. BT_DBG("Bad AMP_MOVE_STATE (%d)", pi->amp_move_state);
  4235. }
  4236. send_move_confirm_response:
  4237. l2cap_send_move_chan_cfm_rsp(conn, cmd->ident, icid);
  4238. if (sk)
  4239. release_sock(sk);
  4240. return 0;
  4241. }
  4242. static inline int l2cap_move_channel_confirm_rsp(struct l2cap_conn *conn,
  4243. struct l2cap_cmd_hdr *cmd, u8 *data)
  4244. {
  4245. struct l2cap_move_chan_cfm_rsp *rsp =
  4246. (struct l2cap_move_chan_cfm_rsp *) data;
  4247. struct sock *sk;
  4248. struct l2cap_pinfo *pi;
  4249. u16 icid;
  4250. icid = le16_to_cpu(rsp->icid);
  4251. BT_DBG("icid %d", (int) icid);
  4252. read_lock(&conn->chan_list.lock);
  4253. sk = __l2cap_get_chan_by_scid(&conn->chan_list, icid);
  4254. read_unlock(&conn->chan_list.lock);
  4255. if (!sk)
  4256. return 0;
  4257. lock_sock(sk);
  4258. pi = l2cap_pi(sk);
  4259. l2cap_sock_clear_timer(sk);
  4260. if (pi->amp_move_state ==
  4261. L2CAP_AMP_STATE_WAIT_MOVE_CONFIRM_RSP) {
  4262. pi->amp_move_state = L2CAP_AMP_STATE_STABLE;
  4263. pi->amp_id = pi->amp_move_id;
  4264. if (!pi->amp_id && pi->ampchan) {
  4265. struct hci_chan *ampchan = pi->ampchan;
  4266. struct hci_conn *ampcon = pi->ampcon;
  4267. /* Have moved off of AMP, free the channel */
  4268. pi->ampchan = NULL;
  4269. pi->ampcon = NULL;
  4270. if (hci_chan_put(ampchan))
  4271. ampcon->l2cap_data = NULL;
  4272. else
  4273. l2cap_deaggregate(ampchan, pi);
  4274. }
  4275. l2cap_amp_move_success(sk);
  4276. pi->amp_move_role = L2CAP_AMP_MOVE_NONE;
  4277. }
  4278. release_sock(sk);
  4279. return 0;
  4280. }
  4281. static void l2cap_amp_signal_worker(struct work_struct *work)
  4282. {
  4283. int err = 0;
  4284. struct l2cap_amp_signal_work *ampwork =
  4285. container_of(work, struct l2cap_amp_signal_work, work);
  4286. switch (ampwork->cmd.code) {
  4287. case L2CAP_MOVE_CHAN_REQ:
  4288. err = l2cap_move_channel_req(ampwork->conn, &ampwork->cmd,
  4289. ampwork->data);
  4290. break;
  4291. case L2CAP_MOVE_CHAN_RSP:
  4292. err = l2cap_move_channel_rsp(ampwork->conn, &ampwork->cmd,
  4293. ampwork->data);
  4294. break;
  4295. case L2CAP_MOVE_CHAN_CFM:
  4296. err = l2cap_move_channel_confirm(ampwork->conn, &ampwork->cmd,
  4297. ampwork->data);
  4298. break;
  4299. case L2CAP_MOVE_CHAN_CFM_RSP:
  4300. err = l2cap_move_channel_confirm_rsp(ampwork->conn,
  4301. &ampwork->cmd, ampwork->data);
  4302. break;
  4303. default:
  4304. BT_ERR("Unknown signaling command 0x%2.2x", ampwork->cmd.code);
  4305. err = -EINVAL;
  4306. break;
  4307. }
  4308. if (err) {
  4309. struct l2cap_cmd_rej rej;
  4310. BT_DBG("error %d", err);
  4311. /* In this context, commands are only rejected with
  4312. * "command not understood", code 0.
  4313. */
  4314. rej.reason = cpu_to_le16(0);
  4315. l2cap_send_cmd(ampwork->conn, ampwork->cmd.ident,
  4316. L2CAP_COMMAND_REJ, sizeof(rej), &rej);
  4317. }
  4318. kfree_skb(ampwork->skb);
  4319. kfree(ampwork);
  4320. }
  4321. void l2cap_amp_physical_complete(int result, u8 local_id, u8 remote_id,
  4322. struct sock *sk)
  4323. {
  4324. struct l2cap_pinfo *pi;
  4325. BT_DBG("result %d, local_id %d, remote_id %d, sk %p", result,
  4326. (int) local_id, (int) remote_id, sk);
  4327. lock_sock(sk);
  4328. if (sk->sk_state == BT_DISCONN || sk->sk_state == BT_CLOSED) {
  4329. release_sock(sk);
  4330. return;
  4331. }
  4332. pi = l2cap_pi(sk);
  4333. if (sk->sk_state != BT_CONNECTED) {
  4334. if (bt_sk(sk)->parent) {
  4335. struct l2cap_conn_rsp rsp;
  4336. char buf[128];
  4337. rsp.scid = cpu_to_le16(l2cap_pi(sk)->dcid);
  4338. rsp.dcid = cpu_to_le16(l2cap_pi(sk)->scid);
  4339. /* Incoming channel on AMP */
  4340. if (result == L2CAP_CREATE_CHAN_SUCCESS) {
  4341. /* Send successful response */
  4342. rsp.result = cpu_to_le16(L2CAP_CR_SUCCESS);
  4343. rsp.status = cpu_to_le16(L2CAP_CS_NO_INFO);
  4344. } else {
  4345. /* Send negative response */
  4346. rsp.result = cpu_to_le16(L2CAP_CR_NO_MEM);
  4347. rsp.status = cpu_to_le16(L2CAP_CS_NO_INFO);
  4348. }
  4349. l2cap_send_cmd(pi->conn, pi->ident,
  4350. L2CAP_CREATE_CHAN_RSP,
  4351. sizeof(rsp), &rsp);
  4352. if (result == L2CAP_CREATE_CHAN_SUCCESS) {
  4353. sk->sk_state = BT_CONFIG;
  4354. pi->conf_state |= L2CAP_CONF_REQ_SENT;
  4355. l2cap_send_cmd(pi->conn,
  4356. l2cap_get_ident(pi->conn),
  4357. L2CAP_CONF_REQ,
  4358. l2cap_build_conf_req(sk, buf, sizeof(buf)), buf);
  4359. l2cap_pi(sk)->num_conf_req++;
  4360. }
  4361. } else {
  4362. /* Outgoing channel on AMP */
  4363. if (result != L2CAP_CREATE_CHAN_SUCCESS) {
  4364. /* Revert to BR/EDR connect */
  4365. l2cap_send_conn_req(sk);
  4366. } else {
  4367. pi->amp_id = local_id;
  4368. l2cap_send_create_chan_req(sk, remote_id);
  4369. }
  4370. }
  4371. } else if (result == L2CAP_MOVE_CHAN_SUCCESS &&
  4372. pi->amp_move_role == L2CAP_AMP_MOVE_INITIATOR) {
  4373. l2cap_amp_move_setup(sk);
  4374. pi->amp_move_id = local_id;
  4375. pi->amp_move_state = L2CAP_AMP_STATE_WAIT_MOVE_RSP;
  4376. l2cap_send_move_chan_req(pi->conn, pi, pi->scid, remote_id);
  4377. l2cap_sock_set_timer(sk, L2CAP_MOVE_TIMEOUT);
  4378. } else if (result == L2CAP_MOVE_CHAN_SUCCESS &&
  4379. pi->amp_move_role == L2CAP_AMP_MOVE_RESPONDER) {
  4380. struct hci_chan *chan;
  4381. struct l2cap_conf_ext_fs default_fs = {1, 1, 0xFFFF,
  4382. 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF};
  4383. pi->remote_fs = default_fs;
  4384. pi->local_fs = default_fs;
  4385. chan = l2cap_chan_admit(local_id, sk);
  4386. if (chan) {
  4387. if (chan->state == BT_CONNECTED) {
  4388. /* Logical link is ready to go */
  4389. pi->ampcon = chan->conn;
  4390. pi->ampcon->l2cap_data = pi->conn;
  4391. pi->amp_move_state =
  4392. L2CAP_AMP_STATE_WAIT_MOVE_CONFIRM;
  4393. l2cap_send_move_chan_rsp(pi->conn,
  4394. pi->amp_move_cmd_ident, pi->dcid,
  4395. L2CAP_MOVE_CHAN_SUCCESS);
  4396. l2cap_create_cfm(chan, 0);
  4397. } else {
  4398. /* Wait for logical link to be ready */
  4399. pi->amp_move_state =
  4400. L2CAP_AMP_STATE_WAIT_LOGICAL_CONFIRM;
  4401. }
  4402. } else {
  4403. /* Logical link not available */
  4404. l2cap_send_move_chan_rsp(pi->conn,
  4405. pi->amp_move_cmd_ident, pi->dcid,
  4406. L2CAP_MOVE_CHAN_REFUSED_NOT_ALLOWED);
  4407. }
  4408. } else {
  4409. BT_DBG("result %d, role %d, local_busy %d", result,
  4410. (int) pi->amp_move_role,
  4411. (int) ((pi->conn_state & L2CAP_CONN_LOCAL_BUSY) != 0));
  4412. if (pi->amp_move_role == L2CAP_AMP_MOVE_RESPONDER) {
  4413. if (result == -EINVAL)
  4414. l2cap_send_move_chan_rsp(pi->conn,
  4415. pi->amp_move_cmd_ident, pi->dcid,
  4416. L2CAP_MOVE_CHAN_REFUSED_CONTROLLER);
  4417. else
  4418. l2cap_send_move_chan_rsp(pi->conn,
  4419. pi->amp_move_cmd_ident, pi->dcid,
  4420. L2CAP_MOVE_CHAN_REFUSED_NOT_ALLOWED);
  4421. }
  4422. pi->amp_move_role = L2CAP_AMP_MOVE_NONE;
  4423. pi->amp_move_state = L2CAP_AMP_STATE_STABLE;
  4424. if ((l2cap_pi(sk)->conn_state & L2CAP_CONN_LOCAL_BUSY) &&
  4425. l2cap_rmem_available(sk))
  4426. l2cap_ertm_tx(sk, 0, 0,
  4427. L2CAP_ERTM_EVENT_LOCAL_BUSY_CLEAR);
  4428. /* Restart data transmission */
  4429. l2cap_ertm_send(sk);
  4430. }
  4431. release_sock(sk);
  4432. }
  4433. static void l2cap_logical_link_complete(struct hci_chan *chan, u8 status)
  4434. {
  4435. struct l2cap_pinfo *pi;
  4436. struct sock *sk;
  4437. struct hci_chan *ampchan;
  4438. struct hci_conn *ampcon;
  4439. BT_DBG("status %d, chan %p, conn %p", (int) status, chan, chan->conn);
  4440. sk = chan->l2cap_sk;
  4441. chan->l2cap_sk = NULL;
  4442. BT_DBG("sk %p", sk);
  4443. lock_sock(sk);
  4444. if (sk->sk_state != BT_CONNECTED && !l2cap_pi(sk)->amp_id) {
  4445. release_sock(sk);
  4446. return;
  4447. }
  4448. pi = l2cap_pi(sk);
  4449. if ((!status) && (chan != NULL)) {
  4450. pi->ampcon = chan->conn;
  4451. pi->ampcon->l2cap_data = pi->conn;
  4452. BT_DBG("amp_move_state %d", pi->amp_move_state);
  4453. if (sk->sk_state != BT_CONNECTED) {
  4454. struct l2cap_conf_rsp rsp;
  4455. /* Must use spinlock to prevent concurrent
  4456. * execution of l2cap_config_rsp()
  4457. */
  4458. bh_lock_sock(sk);
  4459. l2cap_send_cmd(pi->conn, pi->conf_ident, L2CAP_CONF_RSP,
  4460. l2cap_build_conf_rsp(sk, &rsp,
  4461. L2CAP_CONF_SUCCESS, 0), &rsp);
  4462. pi->conf_state |= L2CAP_CONF_OUTPUT_DONE;
  4463. if (l2cap_pi(sk)->conf_state & L2CAP_CONF_INPUT_DONE) {
  4464. set_default_fcs(l2cap_pi(sk));
  4465. sk->sk_state = BT_CONNECTED;
  4466. if (l2cap_pi(sk)->mode == L2CAP_MODE_ERTM ||
  4467. l2cap_pi(sk)->mode == L2CAP_MODE_STREAMING)
  4468. l2cap_ertm_init(sk);
  4469. l2cap_chan_ready(sk);
  4470. }
  4471. bh_unlock_sock(sk);
  4472. } else if (pi->amp_move_state ==
  4473. L2CAP_AMP_STATE_WAIT_LOGICAL_COMPLETE) {
  4474. /* Move confirm will be sent after a success
  4475. * response is received
  4476. */
  4477. pi->amp_move_state =
  4478. L2CAP_AMP_STATE_WAIT_MOVE_RSP_SUCCESS;
  4479. } else if (pi->amp_move_state ==
  4480. L2CAP_AMP_STATE_WAIT_LOGICAL_CONFIRM) {
  4481. if (pi->conn_state & L2CAP_CONN_LOCAL_BUSY)
  4482. pi->amp_move_state =
  4483. L2CAP_AMP_STATE_WAIT_LOCAL_BUSY;
  4484. else if (pi->amp_move_role ==
  4485. L2CAP_AMP_MOVE_INITIATOR) {
  4486. pi->amp_move_state =
  4487. L2CAP_AMP_STATE_WAIT_MOVE_CONFIRM_RSP;
  4488. l2cap_send_move_chan_cfm(pi->conn, pi, pi->scid,
  4489. L2CAP_MOVE_CHAN_SUCCESS);
  4490. l2cap_sock_set_timer(sk, L2CAP_MOVE_TIMEOUT);
  4491. } else if (pi->amp_move_role ==
  4492. L2CAP_AMP_MOVE_RESPONDER) {
  4493. pi->amp_move_state =
  4494. L2CAP_AMP_STATE_WAIT_MOVE_CONFIRM;
  4495. l2cap_send_move_chan_rsp(pi->conn,
  4496. pi->amp_move_cmd_ident, pi->dcid,
  4497. L2CAP_MOVE_CHAN_SUCCESS);
  4498. }
  4499. } else if ((pi->amp_move_state !=
  4500. L2CAP_AMP_STATE_WAIT_MOVE_RSP_SUCCESS) &&
  4501. (pi->amp_move_state !=
  4502. L2CAP_AMP_STATE_WAIT_MOVE_CONFIRM) &&
  4503. (pi->amp_move_state !=
  4504. L2CAP_AMP_STATE_WAIT_MOVE_CONFIRM_RSP)) {
  4505. /* Move was not in expected state, free the channel */
  4506. ampchan = pi->ampchan;
  4507. ampcon = pi->ampcon;
  4508. pi->ampchan = NULL;
  4509. pi->ampcon = NULL;
  4510. if (ampchan) {
  4511. if (hci_chan_put(ampchan))
  4512. ampcon->l2cap_data = NULL;
  4513. else
  4514. l2cap_deaggregate(ampchan, pi);
  4515. }
  4516. pi->amp_move_state = L2CAP_AMP_STATE_STABLE;
  4517. }
  4518. } else {
  4519. /* Logical link setup failed. */
  4520. if (sk->sk_state != BT_CONNECTED)
  4521. l2cap_send_disconn_req(pi->conn, sk, ECONNRESET);
  4522. else if (pi->amp_move_role == L2CAP_AMP_MOVE_RESPONDER) {
  4523. l2cap_amp_move_revert(sk);
  4524. l2cap_pi(sk)->amp_move_role = L2CAP_AMP_MOVE_NONE;
  4525. pi->amp_move_state = L2CAP_AMP_STATE_STABLE;
  4526. l2cap_send_move_chan_rsp(pi->conn,
  4527. pi->amp_move_cmd_ident, pi->dcid,
  4528. L2CAP_MOVE_CHAN_REFUSED_CONFIG);
  4529. } else if (pi->amp_move_role == L2CAP_AMP_MOVE_INITIATOR) {
  4530. if ((pi->amp_move_state ==
  4531. L2CAP_AMP_STATE_WAIT_LOGICAL_COMPLETE) ||
  4532. (pi->amp_move_state ==
  4533. L2CAP_AMP_STATE_WAIT_LOGICAL_CONFIRM)) {
  4534. /* Remote has only sent pending or
  4535. * success responses, clean up
  4536. */
  4537. l2cap_amp_move_revert(sk);
  4538. l2cap_pi(sk)->amp_move_role =
  4539. L2CAP_AMP_MOVE_NONE;
  4540. pi->amp_move_state = L2CAP_AMP_STATE_STABLE;
  4541. }
  4542. /* Other amp move states imply that the move
  4543. * has already aborted
  4544. */
  4545. l2cap_send_move_chan_cfm(pi->conn, pi, pi->scid,
  4546. L2CAP_MOVE_CHAN_UNCONFIRMED);
  4547. l2cap_sock_set_timer(sk, L2CAP_MOVE_TIMEOUT);
  4548. }
  4549. ampchan = pi->ampchan;
  4550. ampcon = pi->ampcon;
  4551. pi->ampchan = NULL;
  4552. pi->ampcon = NULL;
  4553. if (ampchan) {
  4554. if (hci_chan_put(ampchan))
  4555. ampcon->l2cap_data = NULL;
  4556. else
  4557. l2cap_deaggregate(ampchan, pi);
  4558. }
  4559. }
  4560. release_sock(sk);
  4561. }
  4562. static void l2cap_logical_link_worker(struct work_struct *work)
  4563. {
  4564. struct l2cap_logical_link_work *log_link_work =
  4565. container_of(work, struct l2cap_logical_link_work, work);
  4566. struct sock *sk = log_link_work->chan->l2cap_sk;
  4567. if (sk) {
  4568. l2cap_logical_link_complete(log_link_work->chan,
  4569. log_link_work->status);
  4570. sock_put(sk);
  4571. }
  4572. hci_chan_put(log_link_work->chan);
  4573. kfree(log_link_work);
  4574. }
  4575. static int l2cap_create_cfm(struct hci_chan *chan, u8 status)
  4576. {
  4577. struct l2cap_logical_link_work *amp_work;
  4578. if (!chan->l2cap_sk) {
  4579. BT_ERR("Expected l2cap_sk to point to connecting socket");
  4580. return -EFAULT;
  4581. }
  4582. amp_work = kzalloc(sizeof(*amp_work), GFP_ATOMIC);
  4583. if (!amp_work) {
  4584. sock_put(chan->l2cap_sk);
  4585. return -ENOMEM;
  4586. }
  4587. INIT_WORK(&amp_work->work, l2cap_logical_link_worker);
  4588. amp_work->chan = chan;
  4589. amp_work->status = status;
  4590. hci_chan_hold(chan);
  4591. if (!queue_work(_l2cap_wq, &amp_work->work)) {
  4592. kfree(amp_work);
  4593. sock_put(chan->l2cap_sk);
  4594. hci_chan_put(chan);
  4595. return -ENOMEM;
  4596. }
  4597. return 0;
  4598. }
  4599. int l2cap_modify_cfm(struct hci_chan *chan, u8 status)
  4600. {
  4601. struct l2cap_conn *conn = chan->conn->l2cap_data;
  4602. BT_DBG("chan %p conn %p status %d", chan, conn, status);
  4603. /* TODO: if failed status restore previous fs */
  4604. return 0;
  4605. }
  4606. int l2cap_destroy_cfm(struct hci_chan *chan, u8 reason)
  4607. {
  4608. struct l2cap_chan_list *l;
  4609. struct l2cap_conn *conn = chan->conn->l2cap_data;
  4610. struct sock *sk;
  4611. BT_DBG("chan %p conn %p", chan, conn);
  4612. if (!conn)
  4613. return 0;
  4614. l = &conn->chan_list;
  4615. read_lock(&l->lock);
  4616. for (sk = l->head; sk; sk = l2cap_pi(sk)->next_c) {
  4617. bh_lock_sock(sk);
  4618. /* TODO MM/PK - What to do if connection is LOCAL_BUSY? */
  4619. if (l2cap_pi(sk)->ampchan == chan) {
  4620. struct hci_conn *ampcon = l2cap_pi(sk)->ampcon;
  4621. l2cap_pi(sk)->ampchan = NULL;
  4622. l2cap_pi(sk)->ampcon = NULL;
  4623. if (hci_chan_put(chan))
  4624. ampcon->l2cap_data = NULL;
  4625. else
  4626. l2cap_deaggregate(chan, l2cap_pi(sk));
  4627. l2cap_amp_move_init(sk);
  4628. }
  4629. bh_unlock_sock(sk);
  4630. }
  4631. read_unlock(&l->lock);
  4632. return 0;
  4633. }
  4634. static int l2cap_sig_amp(struct l2cap_conn *conn, struct l2cap_cmd_hdr *cmd,
  4635. u8 *data, struct sk_buff *skb)
  4636. {
  4637. struct l2cap_amp_signal_work *amp_work;
  4638. amp_work = kzalloc(sizeof(*amp_work), GFP_ATOMIC);
  4639. if (!amp_work)
  4640. return -ENOMEM;
  4641. INIT_WORK(&amp_work->work, l2cap_amp_signal_worker);
  4642. amp_work->conn = conn;
  4643. amp_work->cmd = *cmd;
  4644. amp_work->data = data;
  4645. amp_work->skb = skb_clone(skb, GFP_ATOMIC);
  4646. if (!amp_work->skb) {
  4647. kfree(amp_work);
  4648. return -ENOMEM;
  4649. }
  4650. if (!queue_work(_l2cap_wq, &amp_work->work)) {
  4651. kfree_skb(amp_work->skb);
  4652. kfree(amp_work);
  4653. return -ENOMEM;
  4654. }
  4655. return 0;
  4656. }
  4657. static inline int l2cap_check_conn_param(u16 min, u16 max, u16 latency,
  4658. u16 to_multiplier)
  4659. {
  4660. u16 max_latency;
  4661. if (min > max || min < 6 || max > 3200)
  4662. return -EINVAL;
  4663. if (to_multiplier < 10 || to_multiplier > 3200)
  4664. return -EINVAL;
  4665. if (max >= to_multiplier * 8)
  4666. return -EINVAL;
  4667. max_latency = (to_multiplier * 8 / max) - 1;
  4668. if (latency > 499 || latency > max_latency)
  4669. return -EINVAL;
  4670. return 0;
  4671. }
  4672. static inline int l2cap_conn_param_update_req(struct l2cap_conn *conn,
  4673. struct l2cap_cmd_hdr *cmd, u8 *data)
  4674. {
  4675. struct hci_conn *hcon = conn->hcon;
  4676. struct l2cap_conn_param_update_req *req;
  4677. struct l2cap_conn_param_update_rsp rsp;
  4678. struct sock *sk;
  4679. u16 min, max, latency, timeout, cmd_len;
  4680. int err;
  4681. if (!(hcon->link_mode & HCI_LM_MASTER))
  4682. return -EINVAL;
  4683. cmd_len = __le16_to_cpu(cmd->len);
  4684. if (cmd_len != sizeof(struct l2cap_conn_param_update_req))
  4685. return -EPROTO;
  4686. memset(&rsp, 0, sizeof(rsp));
  4687. rsp.result = cpu_to_le16(L2CAP_CONN_PARAM_REJECTED);
  4688. sk = l2cap_find_sock_by_fixed_cid_and_dir(4, conn->src, conn->dst, 0);
  4689. if (sk && !bt_sk(sk)->le_params.prohibit_remote_chg) {
  4690. req = (struct l2cap_conn_param_update_req *) data;
  4691. min = __le16_to_cpu(req->min);
  4692. max = __le16_to_cpu(req->max);
  4693. latency = __le16_to_cpu(req->latency);
  4694. timeout = __le16_to_cpu(req->to_multiplier);
  4695. err = l2cap_check_conn_param(min, max, latency, timeout);
  4696. if (!err) {
  4697. rsp.result = cpu_to_le16(L2CAP_CONN_PARAM_ACCEPTED);
  4698. hci_le_conn_update(hcon, min, max, latency, timeout);
  4699. bt_sk(sk)->le_params.interval_min = min;
  4700. bt_sk(sk)->le_params.interval_max = max;
  4701. bt_sk(sk)->le_params.latency = latency;
  4702. bt_sk(sk)->le_params.supervision_timeout = timeout;
  4703. }
  4704. }
  4705. l2cap_send_cmd(conn, cmd->ident, L2CAP_CONN_PARAM_UPDATE_RSP,
  4706. sizeof(rsp), &rsp);
  4707. return 0;
  4708. }
  4709. static inline int l2cap_bredr_sig_cmd(struct l2cap_conn *conn,
  4710. struct l2cap_cmd_hdr *cmd, u16 cmd_len, u8 *data,
  4711. struct sk_buff *skb)
  4712. {
  4713. int err = 0;
  4714. switch (cmd->code) {
  4715. case L2CAP_COMMAND_REJ:
  4716. l2cap_command_rej(conn, cmd, cmd_len, data);
  4717. break;
  4718. case L2CAP_CONN_REQ:
  4719. err = l2cap_connect_req(conn, cmd, cmd_len, data);
  4720. break;
  4721. case L2CAP_CONN_RSP:
  4722. err = l2cap_connect_rsp(conn, cmd, cmd_len, data);
  4723. break;
  4724. case L2CAP_CONF_REQ:
  4725. err = l2cap_config_req(conn, cmd, cmd_len, data);
  4726. break;
  4727. case L2CAP_CONF_RSP:
  4728. err = l2cap_config_rsp(conn, cmd, cmd_len, data);
  4729. break;
  4730. case L2CAP_DISCONN_REQ:
  4731. err = l2cap_disconnect_req(conn, cmd, cmd_len, data);
  4732. break;
  4733. case L2CAP_DISCONN_RSP:
  4734. err = l2cap_disconnect_rsp(conn, cmd, cmd_len, data);
  4735. break;
  4736. case L2CAP_ECHO_REQ:
  4737. l2cap_send_cmd(conn, cmd->ident, L2CAP_ECHO_RSP, cmd_len, data);
  4738. break;
  4739. case L2CAP_ECHO_RSP:
  4740. break;
  4741. case L2CAP_INFO_REQ:
  4742. err = l2cap_information_req(conn, cmd, cmd_len, data);
  4743. break;
  4744. case L2CAP_INFO_RSP:
  4745. err = l2cap_information_rsp(conn, cmd, cmd_len, data);
  4746. break;
  4747. case L2CAP_CREATE_CHAN_REQ:
  4748. err = l2cap_create_channel_req(conn, cmd, cmd_len, data);
  4749. break;
  4750. case L2CAP_CREATE_CHAN_RSP:
  4751. err = l2cap_create_channel_rsp(conn, cmd, cmd_len, data);
  4752. break;
  4753. case L2CAP_MOVE_CHAN_REQ:
  4754. case L2CAP_MOVE_CHAN_RSP:
  4755. case L2CAP_MOVE_CHAN_CFM:
  4756. case L2CAP_MOVE_CHAN_CFM_RSP:
  4757. err = l2cap_sig_amp(conn, cmd, data, skb);
  4758. break;
  4759. default:
  4760. BT_ERR("Unknown BR/EDR signaling command 0x%2.2x", cmd->code);
  4761. err = -EINVAL;
  4762. break;
  4763. }
  4764. return err;
  4765. }
  4766. static inline int l2cap_le_sig_cmd(struct l2cap_conn *conn,
  4767. struct l2cap_cmd_hdr *cmd, u8 *data)
  4768. {
  4769. switch (cmd->code) {
  4770. case L2CAP_COMMAND_REJ:
  4771. return 0;
  4772. case L2CAP_CONN_PARAM_UPDATE_REQ:
  4773. return l2cap_conn_param_update_req(conn, cmd, data);
  4774. case L2CAP_CONN_PARAM_UPDATE_RSP:
  4775. return 0;
  4776. default:
  4777. BT_ERR("Unknown LE signaling command 0x%2.2x", cmd->code);
  4778. return -EINVAL;
  4779. }
  4780. }
  4781. static inline void l2cap_sig_channel(struct l2cap_conn *conn,
  4782. struct sk_buff *skb)
  4783. {
  4784. u8 *data = skb->data;
  4785. int len = skb->len;
  4786. struct l2cap_cmd_hdr cmd;
  4787. int err;
  4788. l2cap_raw_recv(conn, skb);
  4789. while (len >= L2CAP_CMD_HDR_SIZE) {
  4790. u16 cmd_len;
  4791. memcpy(&cmd, data, L2CAP_CMD_HDR_SIZE);
  4792. data += L2CAP_CMD_HDR_SIZE;
  4793. len -= L2CAP_CMD_HDR_SIZE;
  4794. cmd_len = le16_to_cpu(cmd.len);
  4795. BT_DBG("code 0x%2.2x len %d id 0x%2.2x", cmd.code, cmd_len, cmd.ident);
  4796. if (cmd_len > len || !cmd.ident) {
  4797. BT_DBG("corrupted command");
  4798. break;
  4799. }
  4800. if (conn->hcon->type == LE_LINK)
  4801. err = l2cap_le_sig_cmd(conn, &cmd, data);
  4802. else
  4803. err = l2cap_bredr_sig_cmd(conn, &cmd, cmd_len,
  4804. data, skb);
  4805. if (err) {
  4806. struct l2cap_cmd_rej rej;
  4807. BT_ERR("Wrong link type (%d)", err);
  4808. /* FIXME: Map err to a valid reason */
  4809. rej.reason = cpu_to_le16(0);
  4810. l2cap_send_cmd(conn, cmd.ident, L2CAP_COMMAND_REJ, sizeof(rej), &rej);
  4811. }
  4812. data += cmd_len;
  4813. len -= cmd_len;
  4814. }
  4815. kfree_skb(skb);
  4816. }
  4817. static int l2cap_check_fcs(struct l2cap_pinfo *pi, struct sk_buff *skb)
  4818. {
  4819. u16 our_fcs, rcv_fcs;
  4820. int hdr_size;
  4821. if (pi->extended_control)
  4822. hdr_size = L2CAP_EXTENDED_HDR_SIZE;
  4823. else
  4824. hdr_size = L2CAP_ENHANCED_HDR_SIZE;
  4825. if (pi->fcs == L2CAP_FCS_CRC16) {
  4826. skb_trim(skb, skb->len - L2CAP_FCS_SIZE);
  4827. rcv_fcs = get_unaligned_le16(skb->data + skb->len);
  4828. our_fcs = crc16(0, skb->data - hdr_size, skb->len + hdr_size);
  4829. if (our_fcs != rcv_fcs) {
  4830. BT_DBG("Bad FCS");
  4831. return -EBADMSG;
  4832. }
  4833. }
  4834. return 0;
  4835. }
  4836. static void l2cap_ertm_pass_to_tx(struct sock *sk,
  4837. struct bt_l2cap_control *control)
  4838. {
  4839. BT_DBG("sk %p, control %p", sk, control);
  4840. l2cap_ertm_tx(sk, control, 0, L2CAP_ERTM_EVENT_RECV_REQSEQ_AND_FBIT);
  4841. }
  4842. static void l2cap_ertm_pass_to_tx_fbit(struct sock *sk,
  4843. struct bt_l2cap_control *control)
  4844. {
  4845. BT_DBG("sk %p, control %p", sk, control);
  4846. l2cap_ertm_tx(sk, control, 0, L2CAP_ERTM_EVENT_RECV_FBIT);
  4847. }
  4848. static void l2cap_ertm_resend(struct sock *sk)
  4849. {
  4850. struct bt_l2cap_control control;
  4851. struct l2cap_pinfo *pi;
  4852. struct sk_buff *skb;
  4853. struct sk_buff *tx_skb;
  4854. u16 seq;
  4855. BT_DBG("sk %p", sk);
  4856. pi = l2cap_pi(sk);
  4857. if (pi->conn_state & L2CAP_CONN_REMOTE_BUSY)
  4858. return;
  4859. if (pi->amp_move_state != L2CAP_AMP_STATE_STABLE &&
  4860. pi->amp_move_state != L2CAP_AMP_STATE_WAIT_PREPARE)
  4861. return;
  4862. while (pi->retrans_list.head != L2CAP_SEQ_LIST_CLEAR) {
  4863. seq = l2cap_seq_list_pop(&pi->retrans_list);
  4864. skb = l2cap_ertm_seq_in_queue(TX_QUEUE(sk), seq);
  4865. if (!skb) {
  4866. BT_DBG("Error: Can't retransmit seq %d, frame missing",
  4867. (int) seq);
  4868. continue;
  4869. }
  4870. bt_cb(skb)->retries += 1;
  4871. control = bt_cb(skb)->control;
  4872. if ((pi->max_tx != 0) && (bt_cb(skb)->retries > pi->max_tx)) {
  4873. BT_DBG("Retry limit exceeded (%d)", (int) pi->max_tx);
  4874. l2cap_send_disconn_req(pi->conn, sk, ECONNRESET);
  4875. l2cap_seq_list_clear(&pi->retrans_list);
  4876. break;
  4877. }
  4878. control.reqseq = pi->buffer_seq;
  4879. if (pi->conn_state & L2CAP_CONN_SEND_FBIT) {
  4880. control.final = 1;
  4881. pi->conn_state &= ~L2CAP_CONN_SEND_FBIT;
  4882. } else {
  4883. control.final = 0;
  4884. }
  4885. if (skb_cloned(skb)) {
  4886. /* Cloned sk_buffs are read-only, so we need a
  4887. * writeable copy
  4888. */
  4889. tx_skb = skb_copy(skb, GFP_ATOMIC);
  4890. } else {
  4891. tx_skb = skb_clone(skb, GFP_ATOMIC);
  4892. }
  4893. if (!tx_skb) {
  4894. l2cap_seq_list_clear(&pi->retrans_list);
  4895. break;
  4896. }
  4897. /* Update skb contents */
  4898. if (pi->extended_control) {
  4899. put_unaligned_le32(__pack_extended_control(&control),
  4900. tx_skb->data + L2CAP_HDR_SIZE);
  4901. } else {
  4902. put_unaligned_le16(__pack_enhanced_control(&control),
  4903. tx_skb->data + L2CAP_HDR_SIZE);
  4904. }
  4905. if (pi->fcs == L2CAP_FCS_CRC16)
  4906. apply_fcs(tx_skb);
  4907. sock_hold(sk);
  4908. tx_skb->sk = sk;
  4909. tx_skb->destructor = l2cap_skb_destructor;
  4910. atomic_inc(&pi->ertm_queued);
  4911. l2cap_do_send(sk, tx_skb);
  4912. BT_DBG("Resent txseq %d", (int)control.txseq);
  4913. pi->last_acked_seq = pi->buffer_seq;
  4914. }
  4915. }
  4916. static inline void l2cap_ertm_retransmit(struct sock *sk,
  4917. struct bt_l2cap_control *control)
  4918. {
  4919. BT_DBG("sk %p, control %p", sk, control);
  4920. l2cap_seq_list_append(&l2cap_pi(sk)->retrans_list, control->reqseq);
  4921. l2cap_ertm_resend(sk);
  4922. }
  4923. static void l2cap_ertm_retransmit_all(struct sock *sk,
  4924. struct bt_l2cap_control *control)
  4925. {
  4926. struct l2cap_pinfo *pi;
  4927. struct sk_buff *skb;
  4928. BT_DBG("sk %p, control %p", sk, control);
  4929. pi = l2cap_pi(sk);
  4930. if (control->poll)
  4931. pi->conn_state |= L2CAP_CONN_SEND_FBIT;
  4932. l2cap_seq_list_clear(&pi->retrans_list);
  4933. if (pi->conn_state & L2CAP_CONN_REMOTE_BUSY)
  4934. return;
  4935. if (pi->unacked_frames) {
  4936. skb_queue_walk(TX_QUEUE(sk), skb) {
  4937. if ((bt_cb(skb)->control.txseq == control->reqseq) ||
  4938. skb == sk->sk_send_head)
  4939. break;
  4940. }
  4941. skb_queue_walk_from(TX_QUEUE(sk), skb) {
  4942. if (skb == sk->sk_send_head)
  4943. break;
  4944. l2cap_seq_list_append(&pi->retrans_list,
  4945. bt_cb(skb)->control.txseq);
  4946. }
  4947. l2cap_ertm_resend(sk);
  4948. }
  4949. }
  4950. static inline void append_skb_frag(struct sk_buff *skb,
  4951. struct sk_buff *new_frag, struct sk_buff **last_frag)
  4952. {
  4953. /* skb->len reflects data in skb as well as all fragments
  4954. skb->data_len reflects only data in fragments
  4955. */
  4956. BT_DBG("skb %p, new_frag %p, *last_frag %p", skb, new_frag, *last_frag);
  4957. if (!skb_has_frag_list(skb))
  4958. skb_shinfo(skb)->frag_list = new_frag;
  4959. new_frag->next = NULL;
  4960. (*last_frag)->next = new_frag;
  4961. *last_frag = new_frag;
  4962. skb->len += new_frag->len;
  4963. skb->data_len += new_frag->len;
  4964. skb->truesize += new_frag->truesize;
  4965. }
  4966. static int l2cap_ertm_rx_expected_iframe(struct sock *sk,
  4967. struct bt_l2cap_control *control, struct sk_buff *skb)
  4968. {
  4969. struct l2cap_pinfo *pi;
  4970. int err = -EINVAL;
  4971. BT_DBG("sk %p, control %p, skb %p len %d truesize %d", sk, control,
  4972. skb, skb->len, skb->truesize);
  4973. if (!control)
  4974. return err;
  4975. pi = l2cap_pi(sk);
  4976. BT_DBG("type %c, sar %d, txseq %d, reqseq %d, final %d",
  4977. control->frame_type, control->sar, control->txseq,
  4978. control->reqseq, control->final);
  4979. switch (control->sar) {
  4980. case L2CAP_SAR_UNSEGMENTED:
  4981. if (pi->sdu) {
  4982. BT_DBG("Unexpected unsegmented PDU during reassembly");
  4983. kfree_skb(pi->sdu);
  4984. pi->sdu = NULL;
  4985. pi->sdu_last_frag = NULL;
  4986. pi->sdu_len = 0;
  4987. }
  4988. BT_DBG("Unsegmented");
  4989. err = sock_queue_rcv_skb(sk, skb);
  4990. break;
  4991. case L2CAP_SAR_START:
  4992. if (pi->sdu) {
  4993. BT_DBG("Unexpected start PDU during reassembly");
  4994. kfree_skb(pi->sdu);
  4995. }
  4996. pi->sdu_len = get_unaligned_le16(skb->data);
  4997. skb_pull(skb, 2);
  4998. if (pi->sdu_len > pi->imtu) {
  4999. err = -EMSGSIZE;
  5000. break;
  5001. }
  5002. if (skb->len >= pi->sdu_len)
  5003. break;
  5004. pi->sdu = skb;
  5005. pi->sdu_last_frag = skb;
  5006. BT_DBG("Start");
  5007. skb = NULL;
  5008. err = 0;
  5009. break;
  5010. case L2CAP_SAR_CONTINUE:
  5011. if (!pi->sdu)
  5012. break;
  5013. append_skb_frag(pi->sdu, skb,
  5014. &pi->sdu_last_frag);
  5015. skb = NULL;
  5016. if (pi->sdu->len >= pi->sdu_len)
  5017. break;
  5018. BT_DBG("Continue, reassembled %d", pi->sdu->len);
  5019. err = 0;
  5020. break;
  5021. case L2CAP_SAR_END:
  5022. if (!pi->sdu)
  5023. break;
  5024. append_skb_frag(pi->sdu, skb,
  5025. &pi->sdu_last_frag);
  5026. skb = NULL;
  5027. if (pi->sdu->len != pi->sdu_len)
  5028. break;
  5029. BT_DBG("End, reassembled %d", pi->sdu->len);
  5030. /* If the sender used tiny PDUs, the rcv queuing could fail.
  5031. * Applications that have issues here should use a larger
  5032. * sk_rcvbuf.
  5033. */
  5034. err = sock_queue_rcv_skb(sk, pi->sdu);
  5035. if (!err) {
  5036. /* Reassembly complete */
  5037. pi->sdu = NULL;
  5038. pi->sdu_last_frag = NULL;
  5039. pi->sdu_len = 0;
  5040. }
  5041. break;
  5042. default:
  5043. BT_DBG("Bad SAR value");
  5044. break;
  5045. }
  5046. if (err) {
  5047. BT_DBG("Reassembly error %d, sk_rcvbuf %d, sk_rmem_alloc %d",
  5048. err, sk->sk_rcvbuf, atomic_read(&sk->sk_rmem_alloc));
  5049. if (pi->sdu) {
  5050. kfree_skb(pi->sdu);
  5051. pi->sdu = NULL;
  5052. }
  5053. pi->sdu_last_frag = NULL;
  5054. pi->sdu_len = 0;
  5055. if (skb)
  5056. kfree_skb(skb);
  5057. }
  5058. /* Update local busy state */
  5059. if (!(pi->conn_state & L2CAP_CONN_LOCAL_BUSY) && l2cap_rmem_full(sk))
  5060. l2cap_ertm_tx(sk, 0, 0, L2CAP_ERTM_EVENT_LOCAL_BUSY_DETECTED);
  5061. return err;
  5062. }
  5063. static int l2cap_ertm_rx_queued_iframes(struct sock *sk)
  5064. {
  5065. int err = 0;
  5066. /* Pass sequential frames to l2cap_ertm_rx_expected_iframe()
  5067. * until a gap is encountered.
  5068. */
  5069. struct l2cap_pinfo *pi;
  5070. BT_DBG("sk %p", sk);
  5071. pi = l2cap_pi(sk);
  5072. while (l2cap_rmem_available(sk)) {
  5073. struct sk_buff *skb;
  5074. BT_DBG("Searching for skb with txseq %d (queue len %d)",
  5075. (int) pi->buffer_seq, skb_queue_len(SREJ_QUEUE(sk)));
  5076. skb = l2cap_ertm_seq_in_queue(SREJ_QUEUE(sk), pi->buffer_seq);
  5077. if (!skb)
  5078. break;
  5079. skb_unlink(skb, SREJ_QUEUE(sk));
  5080. pi->buffer_seq = __next_seq(pi->buffer_seq, pi);
  5081. err = l2cap_ertm_rx_expected_iframe(sk,
  5082. &bt_cb(skb)->control, skb);
  5083. if (err)
  5084. break;
  5085. }
  5086. if (skb_queue_empty(SREJ_QUEUE(sk))) {
  5087. pi->rx_state = L2CAP_ERTM_RX_STATE_RECV;
  5088. l2cap_ertm_send_ack(sk);
  5089. }
  5090. return err;
  5091. }
  5092. static void l2cap_ertm_handle_srej(struct sock *sk,
  5093. struct bt_l2cap_control *control)
  5094. {
  5095. struct l2cap_pinfo *pi;
  5096. struct sk_buff *skb;
  5097. BT_DBG("sk %p, control %p", sk, control);
  5098. pi = l2cap_pi(sk);
  5099. if (control->reqseq == pi->next_tx_seq) {
  5100. BT_DBG("Invalid reqseq %d, disconnecting",
  5101. (int) control->reqseq);
  5102. l2cap_send_disconn_req(pi->conn, sk, ECONNRESET);
  5103. return;
  5104. }
  5105. skb = l2cap_ertm_seq_in_queue(TX_QUEUE(sk), control->reqseq);
  5106. if (skb == NULL) {
  5107. BT_DBG("Seq %d not available for retransmission",
  5108. (int) control->reqseq);
  5109. return;
  5110. }
  5111. if ((pi->max_tx != 0) && (bt_cb(skb)->retries >= pi->max_tx)) {
  5112. BT_DBG("Retry limit exceeded (%d)", (int) pi->max_tx);
  5113. l2cap_send_disconn_req(pi->conn, sk, ECONNRESET);
  5114. return;
  5115. }
  5116. pi->conn_state &= ~L2CAP_CONN_REMOTE_BUSY;
  5117. if (control->poll) {
  5118. l2cap_ertm_pass_to_tx(sk, control);
  5119. pi->conn_state |= L2CAP_CONN_SEND_FBIT;
  5120. l2cap_ertm_retransmit(sk, control);
  5121. l2cap_ertm_send(sk);
  5122. if (pi->tx_state == L2CAP_ERTM_TX_STATE_WAIT_F) {
  5123. pi->conn_state |= L2CAP_CONN_SREJ_ACT;
  5124. pi->srej_save_reqseq = control->reqseq;
  5125. }
  5126. } else {
  5127. l2cap_ertm_pass_to_tx_fbit(sk, control);
  5128. if (control->final) {
  5129. if ((pi->conn_state & L2CAP_CONN_SREJ_ACT) &&
  5130. (pi->srej_save_reqseq == control->reqseq)) {
  5131. pi->conn_state &= ~L2CAP_CONN_SREJ_ACT;
  5132. } else {
  5133. l2cap_ertm_retransmit(sk, control);
  5134. }
  5135. } else {
  5136. l2cap_ertm_retransmit(sk, control);
  5137. if (pi->tx_state == L2CAP_ERTM_TX_STATE_WAIT_F) {
  5138. pi->conn_state |= L2CAP_CONN_SREJ_ACT;
  5139. pi->srej_save_reqseq = control->reqseq;
  5140. }
  5141. }
  5142. }
  5143. }
  5144. static void l2cap_ertm_handle_rej(struct sock *sk,
  5145. struct bt_l2cap_control *control)
  5146. {
  5147. struct l2cap_pinfo *pi;
  5148. struct sk_buff *skb;
  5149. BT_DBG("sk %p, control %p", sk, control);
  5150. pi = l2cap_pi(sk);
  5151. if (control->reqseq == pi->next_tx_seq) {
  5152. BT_DBG("Invalid reqseq %d, disconnecting",
  5153. (int) control->reqseq);
  5154. l2cap_send_disconn_req(pi->conn, sk, ECONNRESET);
  5155. return;
  5156. }
  5157. skb = l2cap_ertm_seq_in_queue(TX_QUEUE(sk), control->reqseq);
  5158. if (pi->max_tx && skb && bt_cb(skb)->retries >= pi->max_tx) {
  5159. BT_DBG("Retry limit exceeded (%d)", (int) pi->max_tx);
  5160. l2cap_send_disconn_req(pi->conn, sk, ECONNRESET);
  5161. return;
  5162. }
  5163. pi->conn_state &= ~L2CAP_CONN_REMOTE_BUSY;
  5164. l2cap_ertm_pass_to_tx(sk, control);
  5165. if (control->final) {
  5166. if (pi->conn_state & L2CAP_CONN_REJ_ACT)
  5167. pi->conn_state &= ~L2CAP_CONN_REJ_ACT;
  5168. else
  5169. l2cap_ertm_retransmit_all(sk, control);
  5170. } else {
  5171. l2cap_ertm_retransmit_all(sk, control);
  5172. l2cap_ertm_send(sk);
  5173. if (pi->tx_state == L2CAP_ERTM_TX_STATE_WAIT_F)
  5174. pi->conn_state |= L2CAP_CONN_REJ_ACT;
  5175. }
  5176. }
  5177. static u8 l2cap_ertm_classify_txseq(struct sock *sk, u16 txseq)
  5178. {
  5179. struct l2cap_pinfo *pi;
  5180. BT_DBG("sk %p, txseq %d", sk, (int)txseq);
  5181. pi = l2cap_pi(sk);
  5182. BT_DBG("last_acked_seq %d, expected_tx_seq %d", (int)pi->last_acked_seq,
  5183. (int)pi->expected_tx_seq);
  5184. if (pi->rx_state == L2CAP_ERTM_RX_STATE_SREJ_SENT) {
  5185. if (__delta_seq(txseq, pi->last_acked_seq, pi) >= pi->tx_win) {
  5186. /* See notes below regarding "double poll" and
  5187. * invalid packets.
  5188. */
  5189. if (pi->tx_win <= ((pi->tx_win_max + 1) >> 1)) {
  5190. BT_DBG("Invalid/Ignore - txseq outside "
  5191. "tx window after SREJ sent");
  5192. return L2CAP_ERTM_TXSEQ_INVALID_IGNORE;
  5193. } else {
  5194. BT_DBG("Invalid - bad txseq within tx "
  5195. "window after SREJ sent");
  5196. return L2CAP_ERTM_TXSEQ_INVALID;
  5197. }
  5198. }
  5199. if (pi->srej_list.head == txseq) {
  5200. BT_DBG("Expected SREJ");
  5201. return L2CAP_ERTM_TXSEQ_EXPECTED_SREJ;
  5202. }
  5203. if (l2cap_ertm_seq_in_queue(SREJ_QUEUE(sk), txseq)) {
  5204. BT_DBG("Duplicate SREJ - txseq already stored");
  5205. return L2CAP_ERTM_TXSEQ_DUPLICATE_SREJ;
  5206. }
  5207. if (l2cap_seq_list_contains(&pi->srej_list, txseq)) {
  5208. BT_DBG("Unexpected SREJ - txseq not requested "
  5209. "with SREJ");
  5210. return L2CAP_ERTM_TXSEQ_UNEXPECTED_SREJ;
  5211. }
  5212. }
  5213. if (pi->expected_tx_seq == txseq) {
  5214. if (__delta_seq(txseq, pi->last_acked_seq, pi) >= pi->tx_win) {
  5215. BT_DBG("Invalid - txseq outside tx window");
  5216. return L2CAP_ERTM_TXSEQ_INVALID;
  5217. } else {
  5218. BT_DBG("Expected");
  5219. return L2CAP_ERTM_TXSEQ_EXPECTED;
  5220. }
  5221. }
  5222. if (__delta_seq(txseq, pi->last_acked_seq, pi) <
  5223. __delta_seq(pi->expected_tx_seq, pi->last_acked_seq, pi)) {
  5224. BT_DBG("Duplicate - expected_tx_seq later than txseq");
  5225. return L2CAP_ERTM_TXSEQ_DUPLICATE;
  5226. }
  5227. if (__delta_seq(txseq, pi->last_acked_seq, pi) >= pi->tx_win) {
  5228. /* A source of invalid packets is a "double poll" condition,
  5229. * where delays cause us to send multiple poll packets. If
  5230. * the remote stack receives and processes both polls,
  5231. * sequence numbers can wrap around in such a way that a
  5232. * resent frame has a sequence number that looks like new data
  5233. * with a sequence gap. This would trigger an erroneous SREJ
  5234. * request.
  5235. *
  5236. * Fortunately, this is impossible with a tx window that's
  5237. * less than half of the maximum sequence number, which allows
  5238. * invalid frames to be safely ignored.
  5239. *
  5240. * With tx window sizes greater than half of the tx window
  5241. * maximum, the frame is invalid and cannot be ignored. This
  5242. * causes a disconnect.
  5243. */
  5244. if (pi->tx_win <= ((pi->tx_win_max + 1) >> 1)) {
  5245. BT_DBG("Invalid/Ignore - txseq outside tx window");
  5246. return L2CAP_ERTM_TXSEQ_INVALID_IGNORE;
  5247. } else {
  5248. BT_DBG("Invalid - txseq outside tx window");
  5249. return L2CAP_ERTM_TXSEQ_INVALID;
  5250. }
  5251. } else {
  5252. BT_DBG("Unexpected - txseq indicates missing frames");
  5253. return L2CAP_ERTM_TXSEQ_UNEXPECTED;
  5254. }
  5255. }
  5256. static int l2cap_ertm_rx_state_recv(struct sock *sk,
  5257. struct bt_l2cap_control *control,
  5258. struct sk_buff *skb, u8 event)
  5259. {
  5260. struct l2cap_pinfo *pi;
  5261. int err = 0;
  5262. bool skb_in_use = 0;
  5263. BT_DBG("sk %p, control %p, skb %p, event %d", sk, control, skb,
  5264. (int)event);
  5265. pi = l2cap_pi(sk);
  5266. switch (event) {
  5267. case L2CAP_ERTM_EVENT_RECV_IFRAME:
  5268. switch (l2cap_ertm_classify_txseq(sk, control->txseq)) {
  5269. case L2CAP_ERTM_TXSEQ_EXPECTED:
  5270. l2cap_ertm_pass_to_tx(sk, control);
  5271. if (pi->conn_state & L2CAP_CONN_LOCAL_BUSY) {
  5272. BT_DBG("Busy, discarding expected seq %d",
  5273. control->txseq);
  5274. break;
  5275. }
  5276. pi->expected_tx_seq = __next_seq(control->txseq, pi);
  5277. pi->buffer_seq = pi->expected_tx_seq;
  5278. skb_in_use = 1;
  5279. err = l2cap_ertm_rx_expected_iframe(sk, control, skb);
  5280. if (err)
  5281. break;
  5282. if (control->final) {
  5283. if (pi->conn_state & L2CAP_CONN_REJ_ACT)
  5284. pi->conn_state &= ~L2CAP_CONN_REJ_ACT;
  5285. else {
  5286. control->final = 0;
  5287. l2cap_ertm_retransmit_all(sk, control);
  5288. l2cap_ertm_send(sk);
  5289. }
  5290. }
  5291. if (!(pi->conn_state & L2CAP_CONN_LOCAL_BUSY))
  5292. l2cap_ertm_send_ack(sk);
  5293. break;
  5294. case L2CAP_ERTM_TXSEQ_UNEXPECTED:
  5295. l2cap_ertm_pass_to_tx(sk, control);
  5296. /* Can't issue SREJ frames in the local busy state.
  5297. * Drop this frame, it will be seen as missing
  5298. * when local busy is exited.
  5299. */
  5300. if (pi->conn_state & L2CAP_CONN_LOCAL_BUSY) {
  5301. BT_DBG("Busy, discarding unexpected seq %d",
  5302. control->txseq);
  5303. break;
  5304. }
  5305. /* There was a gap in the sequence, so an SREJ
  5306. * must be sent for each missing frame. The
  5307. * current frame is stored for later use.
  5308. */
  5309. skb_queue_tail(SREJ_QUEUE(sk), skb);
  5310. skb_in_use = 1;
  5311. BT_DBG("Queued %p (queue len %d)", skb,
  5312. skb_queue_len(SREJ_QUEUE(sk)));
  5313. pi->conn_state &= ~L2CAP_CONN_SREJ_ACT;
  5314. l2cap_seq_list_clear(&pi->srej_list);
  5315. l2cap_ertm_send_srej(sk, control->txseq);
  5316. pi->rx_state = L2CAP_ERTM_RX_STATE_SREJ_SENT;
  5317. break;
  5318. case L2CAP_ERTM_TXSEQ_DUPLICATE:
  5319. l2cap_ertm_pass_to_tx(sk, control);
  5320. break;
  5321. case L2CAP_ERTM_TXSEQ_INVALID_IGNORE:
  5322. break;
  5323. case L2CAP_ERTM_TXSEQ_INVALID:
  5324. default:
  5325. l2cap_send_disconn_req(l2cap_pi(sk)->conn, sk,
  5326. ECONNRESET);
  5327. break;
  5328. }
  5329. break;
  5330. case L2CAP_ERTM_EVENT_RECV_RR:
  5331. l2cap_ertm_pass_to_tx(sk, control);
  5332. if (control->final) {
  5333. pi->conn_state &= ~L2CAP_CONN_REMOTE_BUSY;
  5334. if (pi->conn_state & L2CAP_CONN_REJ_ACT)
  5335. pi->conn_state &= ~L2CAP_CONN_REJ_ACT;
  5336. else if (pi->amp_move_state == L2CAP_AMP_STATE_STABLE ||
  5337. pi->amp_move_state ==
  5338. L2CAP_AMP_STATE_WAIT_PREPARE) {
  5339. control->final = 0;
  5340. l2cap_ertm_retransmit_all(sk, control);
  5341. }
  5342. l2cap_ertm_send(sk);
  5343. } else if (control->poll) {
  5344. l2cap_ertm_send_i_or_rr_or_rnr(sk);
  5345. } else {
  5346. if ((pi->conn_state & L2CAP_CONN_REMOTE_BUSY) &&
  5347. pi->unacked_frames)
  5348. l2cap_ertm_start_retrans_timer(pi);
  5349. pi->conn_state &= ~L2CAP_CONN_REMOTE_BUSY;
  5350. l2cap_ertm_send(sk);
  5351. }
  5352. break;
  5353. case L2CAP_ERTM_EVENT_RECV_RNR:
  5354. pi->conn_state |= L2CAP_CONN_REMOTE_BUSY;
  5355. l2cap_ertm_pass_to_tx(sk, control);
  5356. if (control && control->poll) {
  5357. pi->conn_state |= L2CAP_CONN_SEND_FBIT;
  5358. l2cap_ertm_send_rr_or_rnr(sk, 0);
  5359. }
  5360. l2cap_ertm_stop_retrans_timer(pi);
  5361. l2cap_seq_list_clear(&pi->retrans_list);
  5362. break;
  5363. case L2CAP_ERTM_EVENT_RECV_REJ:
  5364. l2cap_ertm_handle_rej(sk, control);
  5365. break;
  5366. case L2CAP_ERTM_EVENT_RECV_SREJ:
  5367. l2cap_ertm_handle_srej(sk, control);
  5368. break;
  5369. default:
  5370. break;
  5371. }
  5372. if (skb && !skb_in_use) {
  5373. BT_DBG("Freeing %p", skb);
  5374. kfree_skb(skb);
  5375. }
  5376. return err;
  5377. }
  5378. static int l2cap_ertm_rx_state_srej_sent(struct sock *sk,
  5379. struct bt_l2cap_control *control,
  5380. struct sk_buff *skb, u8 event)
  5381. {
  5382. struct l2cap_pinfo *pi;
  5383. int err = 0;
  5384. u16 txseq = control->txseq;
  5385. bool skb_in_use = 0;
  5386. BT_DBG("sk %p, control %p, skb %p, event %d", sk, control, skb,
  5387. (int)event);
  5388. pi = l2cap_pi(sk);
  5389. switch (event) {
  5390. case L2CAP_ERTM_EVENT_RECV_IFRAME:
  5391. switch (l2cap_ertm_classify_txseq(sk, txseq)) {
  5392. case L2CAP_ERTM_TXSEQ_EXPECTED:
  5393. /* Keep frame for reassembly later */
  5394. l2cap_ertm_pass_to_tx(sk, control);
  5395. skb_queue_tail(SREJ_QUEUE(sk), skb);
  5396. skb_in_use = 1;
  5397. BT_DBG("Queued %p (queue len %d)", skb,
  5398. skb_queue_len(SREJ_QUEUE(sk)));
  5399. pi->expected_tx_seq = __next_seq(txseq, pi);
  5400. break;
  5401. case L2CAP_ERTM_TXSEQ_EXPECTED_SREJ:
  5402. l2cap_seq_list_pop(&pi->srej_list);
  5403. l2cap_ertm_pass_to_tx(sk, control);
  5404. skb_queue_tail(SREJ_QUEUE(sk), skb);
  5405. skb_in_use = 1;
  5406. BT_DBG("Queued %p (queue len %d)", skb,
  5407. skb_queue_len(SREJ_QUEUE(sk)));
  5408. err = l2cap_ertm_rx_queued_iframes(sk);
  5409. if (err)
  5410. break;
  5411. break;
  5412. case L2CAP_ERTM_TXSEQ_UNEXPECTED:
  5413. /* Got a frame that can't be reassembled yet.
  5414. * Save it for later, and send SREJs to cover
  5415. * the missing frames.
  5416. */
  5417. skb_queue_tail(SREJ_QUEUE(sk), skb);
  5418. skb_in_use = 1;
  5419. BT_DBG("Queued %p (queue len %d)", skb,
  5420. skb_queue_len(SREJ_QUEUE(sk)));
  5421. l2cap_ertm_pass_to_tx(sk, control);
  5422. l2cap_ertm_send_srej(sk, control->txseq);
  5423. break;
  5424. case L2CAP_ERTM_TXSEQ_UNEXPECTED_SREJ:
  5425. /* This frame was requested with an SREJ, but
  5426. * some expected retransmitted frames are
  5427. * missing. Request retransmission of missing
  5428. * SREJ'd frames.
  5429. */
  5430. skb_queue_tail(SREJ_QUEUE(sk), skb);
  5431. skb_in_use = 1;
  5432. BT_DBG("Queued %p (queue len %d)", skb,
  5433. skb_queue_len(SREJ_QUEUE(sk)));
  5434. l2cap_ertm_pass_to_tx(sk, control);
  5435. l2cap_ertm_send_srej_list(sk, control->txseq);
  5436. break;
  5437. case L2CAP_ERTM_TXSEQ_DUPLICATE_SREJ:
  5438. /* We've already queued this frame. Drop this copy. */
  5439. l2cap_ertm_pass_to_tx(sk, control);
  5440. break;
  5441. case L2CAP_ERTM_TXSEQ_DUPLICATE:
  5442. /* Expecting a later sequence number, so this frame
  5443. * was already received. Ignore it completely.
  5444. */
  5445. break;
  5446. case L2CAP_ERTM_TXSEQ_INVALID_IGNORE:
  5447. break;
  5448. case L2CAP_ERTM_TXSEQ_INVALID:
  5449. default:
  5450. l2cap_send_disconn_req(l2cap_pi(sk)->conn, sk,
  5451. ECONNRESET);
  5452. break;
  5453. }
  5454. break;
  5455. case L2CAP_ERTM_EVENT_RECV_RR:
  5456. l2cap_ertm_pass_to_tx(sk, control);
  5457. if (control->final) {
  5458. pi->conn_state &= ~L2CAP_CONN_REMOTE_BUSY;
  5459. if (pi->conn_state & L2CAP_CONN_REJ_ACT)
  5460. pi->conn_state &= ~L2CAP_CONN_REJ_ACT;
  5461. else {
  5462. control->final = 0;
  5463. l2cap_ertm_retransmit_all(sk, control);
  5464. }
  5465. l2cap_ertm_send(sk);
  5466. } else if (control->poll) {
  5467. if ((pi->conn_state & L2CAP_CONN_REMOTE_BUSY) &&
  5468. pi->unacked_frames) {
  5469. l2cap_ertm_start_retrans_timer(pi);
  5470. }
  5471. pi->conn_state &= ~L2CAP_CONN_REMOTE_BUSY;
  5472. pi->conn_state |= L2CAP_CONN_SEND_FBIT;
  5473. l2cap_ertm_send_srej_tail(sk);
  5474. } else {
  5475. if ((pi->conn_state & L2CAP_CONN_REMOTE_BUSY) &&
  5476. pi->unacked_frames) {
  5477. l2cap_ertm_start_retrans_timer(pi);
  5478. }
  5479. pi->conn_state &= ~L2CAP_CONN_REMOTE_BUSY;
  5480. l2cap_ertm_send_ack(sk);
  5481. }
  5482. break;
  5483. case L2CAP_ERTM_EVENT_RECV_RNR:
  5484. pi->conn_state |= L2CAP_CONN_REMOTE_BUSY;
  5485. l2cap_ertm_pass_to_tx(sk, control);
  5486. if (control->poll)
  5487. l2cap_ertm_send_srej_tail(sk);
  5488. else {
  5489. struct bt_l2cap_control rr_control;
  5490. memset(&rr_control, 0, sizeof(rr_control));
  5491. rr_control.frame_type = 's';
  5492. rr_control.super = L2CAP_SFRAME_RR;
  5493. rr_control.reqseq = pi->buffer_seq;
  5494. l2cap_ertm_send_sframe(sk, &rr_control);
  5495. }
  5496. break;
  5497. case L2CAP_ERTM_EVENT_RECV_REJ:
  5498. l2cap_ertm_handle_rej(sk, control);
  5499. break;
  5500. case L2CAP_ERTM_EVENT_RECV_SREJ:
  5501. l2cap_ertm_handle_srej(sk, control);
  5502. break;
  5503. }
  5504. if (skb && !skb_in_use) {
  5505. BT_DBG("Freeing %p", skb);
  5506. kfree_skb(skb);
  5507. }
  5508. return err;
  5509. }
  5510. static int l2cap_ertm_rx_state_amp_move(struct sock *sk,
  5511. struct bt_l2cap_control *control,
  5512. struct sk_buff *skb, u8 event)
  5513. {
  5514. struct l2cap_pinfo *pi;
  5515. int err = 0;
  5516. bool skb_in_use = 0;
  5517. BT_DBG("sk %p, control %p, skb %p, event %d", sk, control, skb,
  5518. (int)event);
  5519. pi = l2cap_pi(sk);
  5520. /* Only handle expected frames, to avoid state changes. */
  5521. switch (event) {
  5522. case L2CAP_ERTM_EVENT_RECV_IFRAME:
  5523. if (l2cap_ertm_classify_txseq(sk, control->txseq) ==
  5524. L2CAP_ERTM_TXSEQ_EXPECTED) {
  5525. l2cap_ertm_pass_to_tx(sk, control);
  5526. if (pi->conn_state & L2CAP_CONN_LOCAL_BUSY) {
  5527. BT_DBG("Busy, discarding expected seq %d",
  5528. control->txseq);
  5529. break;
  5530. }
  5531. pi->expected_tx_seq = __next_seq(control->txseq, pi);
  5532. pi->buffer_seq = pi->expected_tx_seq;
  5533. skb_in_use = 1;
  5534. err = l2cap_ertm_rx_expected_iframe(sk, control, skb);
  5535. if (err)
  5536. break;
  5537. if (control->final) {
  5538. if (pi->conn_state & L2CAP_CONN_REJ_ACT)
  5539. pi->conn_state &= ~L2CAP_CONN_REJ_ACT;
  5540. else
  5541. control->final = 0;
  5542. }
  5543. }
  5544. break;
  5545. case L2CAP_ERTM_EVENT_RECV_RR:
  5546. case L2CAP_ERTM_EVENT_RECV_RNR:
  5547. case L2CAP_ERTM_EVENT_RECV_REJ:
  5548. l2cap_ertm_process_reqseq(sk, control->reqseq);
  5549. break;
  5550. case L2CAP_ERTM_EVENT_RECV_SREJ:
  5551. /* Ignore */
  5552. break;
  5553. default:
  5554. break;
  5555. }
  5556. if (skb && !skb_in_use) {
  5557. BT_DBG("Freeing %p", skb);
  5558. kfree_skb(skb);
  5559. }
  5560. return err;
  5561. }
  5562. static int l2cap_answer_move_poll(struct sock *sk)
  5563. {
  5564. struct l2cap_pinfo *pi;
  5565. struct bt_l2cap_control control;
  5566. int err = 0;
  5567. BT_DBG("sk %p", sk);
  5568. pi = l2cap_pi(sk);
  5569. l2cap_ertm_process_reqseq(sk, pi->amp_move_reqseq);
  5570. if (!skb_queue_empty(TX_QUEUE(sk)))
  5571. sk->sk_send_head = skb_peek(TX_QUEUE(sk));
  5572. else
  5573. sk->sk_send_head = NULL;
  5574. /* Rewind next_tx_seq to the point expected
  5575. * by the receiver.
  5576. */
  5577. pi->next_tx_seq = pi->amp_move_reqseq;
  5578. pi->unacked_frames = 0;
  5579. err = l2cap_finish_amp_move(sk);
  5580. if (err)
  5581. return err;
  5582. pi->conn_state |= L2CAP_CONN_SEND_FBIT;
  5583. l2cap_ertm_send_i_or_rr_or_rnr(sk);
  5584. memset(&control, 0, sizeof(control));
  5585. control.reqseq = pi->amp_move_reqseq;
  5586. if (pi->amp_move_event == L2CAP_ERTM_EVENT_RECV_IFRAME)
  5587. err = -EPROTO;
  5588. else
  5589. err = l2cap_ertm_rx_state_recv(sk, &control, NULL,
  5590. pi->amp_move_event);
  5591. return err;
  5592. }
  5593. static void l2cap_amp_move_setup(struct sock *sk)
  5594. {
  5595. struct l2cap_pinfo *pi;
  5596. struct sk_buff *skb;
  5597. BT_DBG("sk %p", sk);
  5598. pi = l2cap_pi(sk);
  5599. l2cap_ertm_stop_ack_timer(pi);
  5600. l2cap_ertm_stop_retrans_timer(pi);
  5601. l2cap_ertm_stop_monitor_timer(pi);
  5602. pi->retry_count = 0;
  5603. skb_queue_walk(TX_QUEUE(sk), skb) {
  5604. if (bt_cb(skb)->retries)
  5605. bt_cb(skb)->retries = 1;
  5606. else
  5607. break;
  5608. }
  5609. pi->expected_tx_seq = pi->buffer_seq;
  5610. pi->conn_state &= ~(L2CAP_CONN_REJ_ACT | L2CAP_CONN_SREJ_ACT);
  5611. l2cap_seq_list_clear(&pi->retrans_list);
  5612. l2cap_seq_list_clear(&l2cap_pi(sk)->srej_list);
  5613. skb_queue_purge(SREJ_QUEUE(sk));
  5614. pi->tx_state = L2CAP_ERTM_TX_STATE_XMIT;
  5615. pi->rx_state = L2CAP_ERTM_RX_STATE_AMP_MOVE;
  5616. BT_DBG("tx_state 0x2.2%x rx_state 0x2.2%x", pi->tx_state,
  5617. pi->rx_state);
  5618. pi->conn_state |= L2CAP_CONN_REMOTE_BUSY;
  5619. }
  5620. static void l2cap_amp_move_revert(struct sock *sk)
  5621. {
  5622. struct l2cap_pinfo *pi;
  5623. BT_DBG("sk %p", sk);
  5624. pi = l2cap_pi(sk);
  5625. if (pi->amp_move_role == L2CAP_AMP_MOVE_INITIATOR) {
  5626. l2cap_ertm_tx(sk, NULL, NULL, L2CAP_ERTM_EVENT_EXPLICIT_POLL);
  5627. pi->rx_state = L2CAP_ERTM_RX_STATE_WAIT_F_FLAG;
  5628. } else if (pi->amp_move_role == L2CAP_AMP_MOVE_RESPONDER)
  5629. pi->rx_state = L2CAP_ERTM_RX_STATE_WAIT_P_FLAG;
  5630. }
  5631. static int l2cap_amp_move_reconf(struct sock *sk)
  5632. {
  5633. struct l2cap_pinfo *pi;
  5634. u8 buf[64];
  5635. int err = 0;
  5636. BT_DBG("sk %p", sk);
  5637. pi = l2cap_pi(sk);
  5638. l2cap_send_cmd(pi->conn, l2cap_get_ident(pi->conn), L2CAP_CONF_REQ,
  5639. l2cap_build_amp_reconf_req(sk, buf, sizeof(buf)), buf);
  5640. return err;
  5641. }
  5642. static void l2cap_amp_move_success(struct sock *sk)
  5643. {
  5644. struct l2cap_pinfo *pi;
  5645. BT_DBG("sk %p", sk);
  5646. pi = l2cap_pi(sk);
  5647. if (pi->amp_move_role == L2CAP_AMP_MOVE_INITIATOR) {
  5648. int err = 0;
  5649. /* Send reconfigure request */
  5650. if (pi->mode == L2CAP_MODE_ERTM) {
  5651. pi->reconf_state = L2CAP_RECONF_INT;
  5652. if (enable_reconfig)
  5653. err = l2cap_amp_move_reconf(sk);
  5654. if (err || !enable_reconfig) {
  5655. pi->reconf_state = L2CAP_RECONF_NONE;
  5656. l2cap_ertm_tx(sk, NULL, NULL,
  5657. L2CAP_ERTM_EVENT_EXPLICIT_POLL);
  5658. pi->rx_state = L2CAP_ERTM_RX_STATE_WAIT_F_FLAG;
  5659. }
  5660. } else
  5661. pi->rx_state = L2CAP_ERTM_RX_STATE_RECV;
  5662. } else if (pi->amp_move_role == L2CAP_AMP_MOVE_RESPONDER) {
  5663. if (pi->mode == L2CAP_MODE_ERTM)
  5664. pi->rx_state =
  5665. L2CAP_ERTM_RX_STATE_WAIT_P_FLAG_RECONFIGURE;
  5666. else
  5667. pi->rx_state = L2CAP_ERTM_RX_STATE_RECV;
  5668. }
  5669. }
  5670. static inline bool __valid_reqseq(struct l2cap_pinfo *pi, u16 reqseq)
  5671. {
  5672. /* Make sure reqseq is for a packet that has been sent but not acked */
  5673. u16 unacked = __delta_seq(pi->next_tx_seq, pi->expected_ack_seq, pi);
  5674. return __delta_seq(pi->next_tx_seq, reqseq, pi) <= unacked;
  5675. }
  5676. static int l2cap_strm_rx(struct sock *sk, struct bt_l2cap_control *control,
  5677. struct sk_buff *skb)
  5678. {
  5679. struct l2cap_pinfo *pi;
  5680. int err = 0;
  5681. BT_DBG("sk %p, control %p, skb %p, state %d",
  5682. sk, control, skb, l2cap_pi(sk)->rx_state);
  5683. pi = l2cap_pi(sk);
  5684. if (l2cap_ertm_classify_txseq(sk, control->txseq) ==
  5685. L2CAP_ERTM_TXSEQ_EXPECTED) {
  5686. l2cap_ertm_pass_to_tx(sk, control);
  5687. BT_DBG("buffer_seq %d->%d", pi->buffer_seq,
  5688. __next_seq(pi->buffer_seq, pi));
  5689. pi->buffer_seq = __next_seq(pi->buffer_seq, pi);
  5690. l2cap_ertm_rx_expected_iframe(sk, control, skb);
  5691. } else {
  5692. if (pi->sdu) {
  5693. kfree_skb(pi->sdu);
  5694. pi->sdu = NULL;
  5695. }
  5696. pi->sdu_last_frag = NULL;
  5697. pi->sdu_len = 0;
  5698. if (skb) {
  5699. BT_DBG("Freeing %p", skb);
  5700. kfree_skb(skb);
  5701. }
  5702. }
  5703. pi->last_acked_seq = control->txseq;
  5704. pi->expected_tx_seq = __next_seq(control->txseq, pi);
  5705. return err;
  5706. }
  5707. static int l2cap_ertm_rx(struct sock *sk, struct bt_l2cap_control *control,
  5708. struct sk_buff *skb, u8 event)
  5709. {
  5710. struct l2cap_pinfo *pi;
  5711. int err = 0;
  5712. BT_DBG("sk %p, control %p, skb %p, event %d, state %d",
  5713. sk, control, skb, (int)event, l2cap_pi(sk)->rx_state);
  5714. pi = l2cap_pi(sk);
  5715. if (__valid_reqseq(pi, control->reqseq)) {
  5716. switch (pi->rx_state) {
  5717. case L2CAP_ERTM_RX_STATE_RECV:
  5718. err = l2cap_ertm_rx_state_recv(sk, control, skb, event);
  5719. break;
  5720. case L2CAP_ERTM_RX_STATE_SREJ_SENT:
  5721. err = l2cap_ertm_rx_state_srej_sent(sk, control, skb,
  5722. event);
  5723. break;
  5724. case L2CAP_ERTM_RX_STATE_AMP_MOVE:
  5725. err = l2cap_ertm_rx_state_amp_move(sk, control, skb,
  5726. event);
  5727. break;
  5728. case L2CAP_ERTM_RX_STATE_WAIT_F_FLAG:
  5729. if (control->final) {
  5730. pi->conn_state &= ~L2CAP_CONN_REMOTE_BUSY;
  5731. pi->amp_move_role = L2CAP_AMP_MOVE_NONE;
  5732. pi->rx_state = L2CAP_ERTM_RX_STATE_RECV;
  5733. l2cap_ertm_process_reqseq(sk, control->reqseq);
  5734. if (!skb_queue_empty(TX_QUEUE(sk)))
  5735. sk->sk_send_head =
  5736. skb_peek(TX_QUEUE(sk));
  5737. else
  5738. sk->sk_send_head = NULL;
  5739. /* Rewind next_tx_seq to the point expected
  5740. * by the receiver.
  5741. */
  5742. pi->next_tx_seq = control->reqseq;
  5743. pi->unacked_frames = 0;
  5744. if (pi->ampcon)
  5745. pi->conn->mtu =
  5746. pi->ampcon->hdev->acl_mtu;
  5747. else
  5748. pi->conn->mtu =
  5749. pi->conn->hcon->hdev->acl_mtu;
  5750. err = l2cap_setup_resegment(sk);
  5751. if (err)
  5752. break;
  5753. err = l2cap_ertm_rx_state_recv(sk, control, skb,
  5754. event);
  5755. }
  5756. break;
  5757. case L2CAP_ERTM_RX_STATE_WAIT_P_FLAG:
  5758. if (control->poll) {
  5759. pi->amp_move_reqseq = control->reqseq;
  5760. pi->amp_move_event = event;
  5761. err = l2cap_answer_move_poll(sk);
  5762. }
  5763. break;
  5764. case L2CAP_ERTM_RX_STATE_WAIT_P_FLAG_RECONFIGURE:
  5765. if (control->poll) {
  5766. pi->amp_move_reqseq = control->reqseq;
  5767. pi->amp_move_event = event;
  5768. BT_DBG("amp_move_role 0x%2.2x, "
  5769. "reconf_state 0x%2.2x",
  5770. pi->amp_move_role, pi->reconf_state);
  5771. if (pi->reconf_state == L2CAP_RECONF_ACC)
  5772. err = l2cap_amp_move_reconf(sk);
  5773. else
  5774. err = l2cap_answer_move_poll(sk);
  5775. }
  5776. break;
  5777. default:
  5778. /* shut it down */
  5779. break;
  5780. }
  5781. } else {
  5782. BT_DBG("Invalid reqseq %d (next_tx_seq %d, expected_ack_seq %d",
  5783. control->reqseq, pi->next_tx_seq, pi->expected_ack_seq);
  5784. l2cap_send_disconn_req(pi->conn, sk, ECONNRESET);
  5785. }
  5786. return err;
  5787. }
  5788. void l2cap_fixed_channel_config(struct sock *sk, struct l2cap_options *opt)
  5789. {
  5790. lock_sock(sk);
  5791. l2cap_pi(sk)->fixed_channel = 1;
  5792. l2cap_pi(sk)->imtu = opt->imtu;
  5793. l2cap_pi(sk)->omtu = opt->omtu;
  5794. l2cap_pi(sk)->remote_mps = opt->omtu;
  5795. l2cap_pi(sk)->mps = opt->omtu;
  5796. l2cap_pi(sk)->flush_to = opt->flush_to;
  5797. l2cap_pi(sk)->mode = opt->mode;
  5798. l2cap_pi(sk)->fcs = opt->fcs;
  5799. l2cap_pi(sk)->max_tx = opt->max_tx;
  5800. l2cap_pi(sk)->remote_max_tx = opt->max_tx;
  5801. l2cap_pi(sk)->tx_win = opt->txwin_size;
  5802. l2cap_pi(sk)->remote_tx_win = opt->txwin_size;
  5803. l2cap_pi(sk)->retrans_timeout = L2CAP_DEFAULT_RETRANS_TO;
  5804. l2cap_pi(sk)->monitor_timeout = L2CAP_DEFAULT_MONITOR_TO;
  5805. if (opt->mode == L2CAP_MODE_ERTM ||
  5806. l2cap_pi(sk)->mode == L2CAP_MODE_STREAMING)
  5807. l2cap_ertm_init(sk);
  5808. release_sock(sk);
  5809. return;
  5810. }
  5811. static const u8 l2cap_ertm_rx_func_to_event[4] = {
  5812. L2CAP_ERTM_EVENT_RECV_RR, L2CAP_ERTM_EVENT_RECV_REJ,
  5813. L2CAP_ERTM_EVENT_RECV_RNR, L2CAP_ERTM_EVENT_RECV_SREJ
  5814. };
  5815. int l2cap_data_channel(struct sock *sk, struct sk_buff *skb)
  5816. {
  5817. struct l2cap_pinfo *pi;
  5818. struct bt_l2cap_control *control;
  5819. u16 len;
  5820. u8 event;
  5821. pi = l2cap_pi(sk);
  5822. BT_DBG("sk %p, len %d, mode %d", sk, skb->len, pi->mode);
  5823. if (sk->sk_state != BT_CONNECTED)
  5824. goto drop;
  5825. switch (pi->mode) {
  5826. case L2CAP_MODE_BASIC:
  5827. /* If socket recv buffers overflows we drop data here
  5828. * which is *bad* because L2CAP has to be reliable.
  5829. * But we don't have any other choice. L2CAP doesn't
  5830. * provide flow control mechanism. */
  5831. if (pi->imtu < skb->len)
  5832. goto drop;
  5833. if (!sock_queue_rcv_skb(sk, skb))
  5834. goto done;
  5835. break;
  5836. case L2CAP_MODE_ERTM:
  5837. case L2CAP_MODE_STREAMING:
  5838. control = &bt_cb(skb)->control;
  5839. if (pi->extended_control) {
  5840. __get_extended_control(get_unaligned_le32(skb->data),
  5841. control);
  5842. skb_pull(skb, 4);
  5843. } else {
  5844. __get_enhanced_control(get_unaligned_le16(skb->data),
  5845. control);
  5846. skb_pull(skb, 2);
  5847. }
  5848. len = skb->len;
  5849. if (l2cap_check_fcs(pi, skb))
  5850. goto drop;
  5851. if ((control->frame_type == 'i') &&
  5852. (control->sar == L2CAP_SAR_START))
  5853. len -= 2;
  5854. if (pi->fcs == L2CAP_FCS_CRC16)
  5855. len -= 2;
  5856. /*
  5857. * We can just drop the corrupted I-frame here.
  5858. * Receiver will miss it and start proper recovery
  5859. * procedures and ask for retransmission.
  5860. */
  5861. if (len > pi->mps) {
  5862. l2cap_send_disconn_req(pi->conn, sk, ECONNRESET);
  5863. goto drop;
  5864. }
  5865. if (control->frame_type == 'i') {
  5866. int err;
  5867. BT_DBG("iframe sar %d, reqseq %d, final %d, txseq %d",
  5868. control->sar, control->reqseq, control->final,
  5869. control->txseq);
  5870. /* Validate F-bit - F=0 always valid, F=1 only
  5871. * valid in TX WAIT_F
  5872. */
  5873. if (control->final && (pi->tx_state !=
  5874. L2CAP_ERTM_TX_STATE_WAIT_F))
  5875. goto drop;
  5876. if (pi->mode != L2CAP_MODE_STREAMING) {
  5877. event = L2CAP_ERTM_EVENT_RECV_IFRAME;
  5878. err = l2cap_ertm_rx(sk, control, skb, event);
  5879. } else
  5880. err = l2cap_strm_rx(sk, control, skb);
  5881. if (err)
  5882. l2cap_send_disconn_req(pi->conn, sk,
  5883. ECONNRESET);
  5884. } else {
  5885. /* Only I-frames are expected in streaming mode */
  5886. if (pi->mode == L2CAP_MODE_STREAMING)
  5887. goto drop;
  5888. BT_DBG("sframe reqseq %d, final %d, poll %d, super %d",
  5889. control->reqseq, control->final, control->poll,
  5890. control->super);
  5891. if (len != 0) {
  5892. l2cap_send_disconn_req(pi->conn, sk,
  5893. ECONNRESET);
  5894. goto drop;
  5895. }
  5896. /* Validate F and P bits */
  5897. if (control->final &&
  5898. ((pi->tx_state != L2CAP_ERTM_TX_STATE_WAIT_F)
  5899. || control->poll))
  5900. goto drop;
  5901. event = l2cap_ertm_rx_func_to_event[control->super];
  5902. if (l2cap_ertm_rx(sk, control, skb, event))
  5903. l2cap_send_disconn_req(pi->conn, sk,
  5904. ECONNRESET);
  5905. }
  5906. goto done;
  5907. default:
  5908. BT_DBG("sk %p: bad mode 0x%2.2x", sk, pi->mode);
  5909. break;
  5910. }
  5911. drop:
  5912. kfree_skb(skb);
  5913. done:
  5914. return 0;
  5915. }
  5916. void l2cap_recv_deferred_frame(struct sock *sk, struct sk_buff *skb)
  5917. {
  5918. lock_sock(sk);
  5919. l2cap_data_channel(sk, skb);
  5920. release_sock(sk);
  5921. }
  5922. static inline int l2cap_conless_channel(struct l2cap_conn *conn, __le16 psm, struct sk_buff *skb)
  5923. {
  5924. struct sock *sk;
  5925. sk = l2cap_get_sock_by_psm(0, psm, conn->src);
  5926. if (!sk)
  5927. goto drop;
  5928. bh_lock_sock(sk);
  5929. BT_DBG("sk %p, len %d", sk, skb->len);
  5930. if (sk->sk_state != BT_BOUND && sk->sk_state != BT_CONNECTED)
  5931. goto drop;
  5932. if (l2cap_pi(sk)->imtu < skb->len)
  5933. goto drop;
  5934. if (!sock_queue_rcv_skb(sk, skb))
  5935. goto done;
  5936. drop:
  5937. kfree_skb(skb);
  5938. done:
  5939. if (sk)
  5940. bh_unlock_sock(sk);
  5941. return 0;
  5942. }
  5943. static inline int l2cap_att_channel(struct l2cap_conn *conn, __le16 cid,
  5944. struct sk_buff *skb)
  5945. {
  5946. struct sock *sk = NULL;
  5947. struct sk_buff *skb_rsp;
  5948. struct l2cap_hdr *lh;
  5949. int dir;
  5950. struct work_struct *open_worker;
  5951. u8 err_rsp[] = {L2CAP_ATT_ERROR, 0x00, 0x00, 0x00,
  5952. L2CAP_ATT_NOT_SUPPORTED};
  5953. if (skb->data[0] == L2CAP_ATT_MTU_REQ) {
  5954. u8 mtu_rsp[] = {L2CAP_ATT_MTU_RSP, 23, 0};
  5955. skb_rsp = bt_skb_alloc(sizeof(mtu_rsp) + L2CAP_HDR_SIZE,
  5956. GFP_ATOMIC);
  5957. if (!skb_rsp)
  5958. goto drop;
  5959. lh = (struct l2cap_hdr *) skb_put(skb_rsp, L2CAP_HDR_SIZE);
  5960. lh->len = cpu_to_le16(sizeof(mtu_rsp));
  5961. lh->cid = cpu_to_le16(L2CAP_CID_LE_DATA);
  5962. memcpy(skb_put(skb_rsp, sizeof(mtu_rsp)), mtu_rsp,
  5963. sizeof(mtu_rsp));
  5964. hci_send_acl(conn->hcon, NULL, skb_rsp, 0);
  5965. goto free_skb;
  5966. }
  5967. dir = (skb->data[0] & L2CAP_ATT_RESPONSE_BIT) ? 0 : 1;
  5968. sk = l2cap_find_sock_by_fixed_cid_and_dir(cid, conn->src,
  5969. conn->dst, dir);
  5970. BT_DBG("sk %p, dir:%d", sk, dir);
  5971. if (!sk)
  5972. goto drop;
  5973. bh_lock_sock(sk);
  5974. BT_DBG("sk %p, len %d", sk, skb->len);
  5975. if (sk->sk_state != BT_BOUND && sk->sk_state != BT_CONNECTED) {
  5976. att_chn_params.cid = cid;
  5977. att_chn_params.conn = conn;
  5978. att_chn_params.dir = dir;
  5979. att_chn_params.skb = skb;
  5980. open_worker = kzalloc(sizeof(*open_worker), GFP_ATOMIC);
  5981. if (!open_worker)
  5982. BT_ERR("Out of memory");
  5983. INIT_WORK(open_worker, l2cap_queue_acl_data);
  5984. schedule_work(open_worker);
  5985. goto done;
  5986. }
  5987. if (l2cap_pi(sk)->imtu < skb->len)
  5988. goto drop;
  5989. if (!sock_queue_rcv_skb(sk, skb))
  5990. goto done;
  5991. drop:
  5992. if (skb->data[0] != L2CAP_ATT_INDICATE)
  5993. goto not_indicate;
  5994. /* If this is an incoming Indication, we are required to confirm */
  5995. skb_rsp = bt_skb_alloc(sizeof(u8) + L2CAP_HDR_SIZE, GFP_ATOMIC);
  5996. if (!skb_rsp)
  5997. goto free_skb;
  5998. lh = (struct l2cap_hdr *) skb_put(skb_rsp, L2CAP_HDR_SIZE);
  5999. lh->len = cpu_to_le16(sizeof(u8));
  6000. lh->cid = cpu_to_le16(L2CAP_CID_LE_DATA);
  6001. err_rsp[0] = L2CAP_ATT_CONFIRM;
  6002. memcpy(skb_put(skb_rsp, sizeof(u8)), err_rsp, sizeof(u8));
  6003. hci_send_acl(conn->hcon, NULL, skb_rsp, 0);
  6004. goto free_skb;
  6005. not_indicate:
  6006. if (skb->data[0] & L2CAP_ATT_RESPONSE_BIT ||
  6007. skb->data[0] == L2CAP_ATT_CONFIRM)
  6008. goto free_skb;
  6009. /* If this is an incoming PDU that requires a response, respond with
  6010. * a generic error so remote device doesn't hang */
  6011. skb_rsp = bt_skb_alloc(sizeof(err_rsp) + L2CAP_HDR_SIZE, GFP_ATOMIC);
  6012. if (!skb_rsp)
  6013. goto free_skb;
  6014. lh = (struct l2cap_hdr *) skb_put(skb_rsp, L2CAP_HDR_SIZE);
  6015. lh->len = cpu_to_le16(sizeof(err_rsp));
  6016. lh->cid = cpu_to_le16(L2CAP_CID_LE_DATA);
  6017. err_rsp[1] = skb->data[0];
  6018. memcpy(skb_put(skb_rsp, sizeof(err_rsp)), err_rsp, sizeof(err_rsp));
  6019. hci_send_acl(conn->hcon, NULL, skb_rsp, 0);
  6020. free_skb:
  6021. kfree_skb(skb);
  6022. done:
  6023. if (sk)
  6024. bh_unlock_sock(sk);
  6025. return 0;
  6026. }
  6027. static void l2cap_recv_frame(struct l2cap_conn *conn, struct sk_buff *skb)
  6028. {
  6029. struct l2cap_hdr *lh = (void *) skb->data;
  6030. struct sock *sk;
  6031. u16 cid, len;
  6032. __le16 psm;
  6033. skb_pull(skb, L2CAP_HDR_SIZE);
  6034. cid = __le16_to_cpu(lh->cid);
  6035. len = __le16_to_cpu(lh->len);
  6036. if (len != skb->len) {
  6037. kfree_skb(skb);
  6038. return;
  6039. }
  6040. BT_DBG("len %d, cid 0x%4.4x", len, cid);
  6041. switch (cid) {
  6042. case L2CAP_CID_LE_SIGNALING:
  6043. case L2CAP_CID_SIGNALING:
  6044. l2cap_sig_channel(conn, skb);
  6045. break;
  6046. case L2CAP_CID_CONN_LESS:
  6047. psm = get_unaligned_le16(skb->data);
  6048. skb_pull(skb, 2);
  6049. l2cap_conless_channel(conn, psm, skb);
  6050. break;
  6051. case L2CAP_CID_LE_DATA:
  6052. l2cap_att_channel(conn, cid, skb);
  6053. break;
  6054. case L2CAP_CID_SMP:
  6055. if (smp_sig_channel(conn, skb))
  6056. l2cap_conn_del(conn->hcon, EACCES, 0);
  6057. break;
  6058. default:
  6059. sk = l2cap_get_chan_by_scid(&conn->chan_list, cid);
  6060. if (sk) {
  6061. if (sock_owned_by_user(sk)) {
  6062. BT_DBG("backlog sk %p", sk);
  6063. if (sk_add_backlog(sk, skb, sk->sk_rcvbuf))
  6064. kfree_skb(skb);
  6065. } else
  6066. l2cap_data_channel(sk, skb);
  6067. bh_unlock_sock(sk);
  6068. } else if ((cid == L2CAP_CID_A2MP) && enable_hs) {
  6069. BT_DBG("A2MP");
  6070. amp_conn_ind(conn->hcon, skb);
  6071. } else {
  6072. BT_DBG("unknown cid 0x%4.4x", cid);
  6073. kfree_skb(skb);
  6074. }
  6075. break;
  6076. }
  6077. }
  6078. /* ---- L2CAP interface with lower layer (HCI) ---- */
  6079. static int l2cap_connect_ind(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 type)
  6080. {
  6081. int exact = 0, lm1 = 0, lm2 = 0;
  6082. register struct sock *sk;
  6083. struct hlist_node *node;
  6084. if (type != ACL_LINK)
  6085. return 0;
  6086. BT_DBG("hdev %s, bdaddr %s", hdev->name, batostr(bdaddr));
  6087. /* Find listening sockets and check their link_mode */
  6088. read_lock(&l2cap_sk_list.lock);
  6089. sk_for_each(sk, node, &l2cap_sk_list.head) {
  6090. if (sk->sk_state != BT_LISTEN)
  6091. continue;
  6092. if (!bacmp(&bt_sk(sk)->src, &hdev->bdaddr)) {
  6093. lm1 |= HCI_LM_ACCEPT;
  6094. if (l2cap_pi(sk)->role_switch)
  6095. lm1 |= HCI_LM_MASTER;
  6096. exact++;
  6097. } else if (!bacmp(&bt_sk(sk)->src, BDADDR_ANY)) {
  6098. lm2 |= HCI_LM_ACCEPT;
  6099. if (l2cap_pi(sk)->role_switch)
  6100. lm2 |= HCI_LM_MASTER;
  6101. }
  6102. }
  6103. read_unlock(&l2cap_sk_list.lock);
  6104. return exact ? lm1 : lm2;
  6105. }
  6106. static int l2cap_connect_cfm(struct hci_conn *hcon, u8 status)
  6107. {
  6108. struct l2cap_conn *conn;
  6109. BT_DBG("hcon %p bdaddr %s status %d", hcon, batostr(&hcon->dst), status);
  6110. if (!(hcon->type == ACL_LINK || hcon->type == LE_LINK))
  6111. return -EINVAL;
  6112. if (!status) {
  6113. conn = l2cap_conn_add(hcon, status);
  6114. if (conn)
  6115. l2cap_conn_ready(conn);
  6116. } else
  6117. l2cap_conn_del(hcon, bt_err(status), 0);
  6118. return 0;
  6119. }
  6120. static int l2cap_disconn_ind(struct hci_conn *hcon)
  6121. {
  6122. struct l2cap_conn *conn = hcon->l2cap_data;
  6123. BT_DBG("hcon %p", hcon);
  6124. if (hcon->type != ACL_LINK || !conn)
  6125. return 0x13;
  6126. return conn->disc_reason;
  6127. }
  6128. static int l2cap_disconn_cfm(struct hci_conn *hcon, u8 reason, u8 is_process)
  6129. {
  6130. BT_DBG("hcon %p reason %d", hcon, reason);
  6131. if (!(hcon->type == ACL_LINK || hcon->type == LE_LINK))
  6132. return -EINVAL;
  6133. l2cap_conn_del(hcon, bt_err(reason), is_process);
  6134. return 0;
  6135. }
  6136. static inline void l2cap_check_encryption(struct sock *sk, u8 encrypt)
  6137. {
  6138. if (sk->sk_type != SOCK_SEQPACKET && sk->sk_type != SOCK_STREAM)
  6139. return;
  6140. if (encrypt == 0x00) {
  6141. if (l2cap_pi(sk)->sec_level == BT_SECURITY_MEDIUM) {
  6142. l2cap_sock_clear_timer(sk);
  6143. l2cap_sock_set_timer(sk, HZ * 5);
  6144. } else if (l2cap_pi(sk)->sec_level == BT_SECURITY_HIGH ||
  6145. l2cap_pi(sk)->sec_level == BT_SECURITY_VERY_HIGH)
  6146. __l2cap_sock_close(sk, ECONNREFUSED);
  6147. } else {
  6148. if (l2cap_pi(sk)->sec_level == BT_SECURITY_MEDIUM)
  6149. l2cap_sock_clear_timer(sk);
  6150. }
  6151. }
  6152. static int l2cap_security_cfm(struct hci_conn *hcon, u8 status, u8 encrypt)
  6153. {
  6154. struct l2cap_chan_list *l;
  6155. struct l2cap_conn *conn = hcon->l2cap_data;
  6156. struct sock *sk;
  6157. int smp = 0;
  6158. if (!conn)
  6159. return 0;
  6160. l = &conn->chan_list;
  6161. BT_DBG("conn %p", conn);
  6162. read_lock(&l->lock);
  6163. for (sk = l->head; sk; sk = l2cap_pi(sk)->next_c) {
  6164. bh_lock_sock(sk);
  6165. BT_DBG("sk->scid %d", l2cap_pi(sk)->scid);
  6166. if (l2cap_pi(sk)->scid == L2CAP_CID_LE_DATA) {
  6167. if (!status && encrypt) {
  6168. l2cap_pi(sk)->sec_level = hcon->sec_level;
  6169. l2cap_chan_ready(sk);
  6170. }
  6171. smp = 1;
  6172. bh_unlock_sock(sk);
  6173. continue;
  6174. }
  6175. if (l2cap_pi(sk)->conf_state & L2CAP_CONF_CONNECT_PEND) {
  6176. bh_unlock_sock(sk);
  6177. continue;
  6178. }
  6179. if (!status && (sk->sk_state == BT_CONNECTED ||
  6180. sk->sk_state == BT_CONFIG)) {
  6181. l2cap_check_encryption(sk, encrypt);
  6182. bh_unlock_sock(sk);
  6183. continue;
  6184. }
  6185. if (sk->sk_state == BT_CONNECT) {
  6186. if (!status) {
  6187. l2cap_pi(sk)->conf_state |=
  6188. L2CAP_CONF_CONNECT_PEND;
  6189. if ((l2cap_pi(sk)->amp_pref ==
  6190. BT_AMP_POLICY_PREFER_AMP) &&
  6191. enable_hs) {
  6192. amp_create_physical(l2cap_pi(sk)->conn,
  6193. sk);
  6194. } else
  6195. l2cap_send_conn_req(sk);
  6196. } else {
  6197. l2cap_sock_clear_timer(sk);
  6198. l2cap_sock_set_timer(sk, HZ / 10);
  6199. }
  6200. } else if (sk->sk_state == BT_CONNECT2) {
  6201. struct l2cap_conn_rsp rsp;
  6202. __u16 result;
  6203. if (!status) {
  6204. if (l2cap_pi(sk)->amp_id) {
  6205. amp_accept_physical(conn,
  6206. l2cap_pi(sk)->amp_id, sk);
  6207. bh_unlock_sock(sk);
  6208. continue;
  6209. }
  6210. sk->sk_state = BT_CONFIG;
  6211. result = L2CAP_CR_SUCCESS;
  6212. } else {
  6213. sk->sk_state = BT_DISCONN;
  6214. l2cap_sock_set_timer(sk, HZ / 10);
  6215. result = L2CAP_CR_SEC_BLOCK;
  6216. }
  6217. rsp.scid = cpu_to_le16(l2cap_pi(sk)->dcid);
  6218. rsp.dcid = cpu_to_le16(l2cap_pi(sk)->scid);
  6219. rsp.result = cpu_to_le16(result);
  6220. rsp.status = cpu_to_le16(L2CAP_CS_NO_INFO);
  6221. l2cap_send_cmd(conn, l2cap_pi(sk)->ident,
  6222. L2CAP_CONN_RSP, sizeof(rsp), &rsp);
  6223. if (!(l2cap_pi(sk)->conf_state & L2CAP_CONF_REQ_SENT) &&
  6224. result == L2CAP_CR_SUCCESS) {
  6225. char buf[128];
  6226. l2cap_pi(sk)->conf_state |= L2CAP_CONF_REQ_SENT;
  6227. l2cap_send_cmd(conn, l2cap_get_ident(conn),
  6228. L2CAP_CONF_REQ,
  6229. l2cap_build_conf_req(sk, buf, sizeof(buf)),
  6230. buf);
  6231. l2cap_pi(sk)->num_conf_req++;
  6232. }
  6233. }
  6234. bh_unlock_sock(sk);
  6235. }
  6236. read_unlock(&l->lock);
  6237. if (smp) {
  6238. del_timer(&hcon->smp_timer);
  6239. smp_link_encrypt_cmplt(conn, status, encrypt);
  6240. }
  6241. return 0;
  6242. }
  6243. static int l2cap_recv_acldata(struct hci_conn *hcon, struct sk_buff *skb, u16 flags)
  6244. {
  6245. struct l2cap_conn *conn = hcon->l2cap_data;
  6246. if (!conn && hcon->hdev->dev_type != HCI_BREDR)
  6247. goto drop;
  6248. if (!conn)
  6249. conn = l2cap_conn_add(hcon, 0);
  6250. if (!conn)
  6251. goto drop;
  6252. BT_DBG("conn %p len %d flags 0x%x", conn, skb->len, flags);
  6253. if (flags & ACL_START) {
  6254. struct l2cap_hdr *hdr;
  6255. int len;
  6256. if (conn->rx_len) {
  6257. BT_ERR("Unexpected start frame (len %d)", skb->len);
  6258. kfree_skb(conn->rx_skb);
  6259. conn->rx_skb = NULL;
  6260. conn->rx_len = 0;
  6261. l2cap_conn_unreliable(conn, ECOMM);
  6262. }
  6263. /* Start fragment always begin with Basic L2CAP header */
  6264. if (skb->len < L2CAP_HDR_SIZE) {
  6265. BT_ERR("Frame is too short (len %d)", skb->len);
  6266. l2cap_conn_unreliable(conn, ECOMM);
  6267. goto drop;
  6268. }
  6269. hdr = (struct l2cap_hdr *) skb->data;
  6270. len = __le16_to_cpu(hdr->len) + L2CAP_HDR_SIZE;
  6271. if (len == skb->len) {
  6272. /* Complete frame received */
  6273. l2cap_recv_frame(conn, skb);
  6274. return 0;
  6275. }
  6276. if (flags & ACL_CONT) {
  6277. BT_ERR("Complete frame is incomplete "
  6278. "(len %d, expected len %d)",
  6279. skb->len, len);
  6280. l2cap_conn_unreliable(conn, ECOMM);
  6281. goto drop;
  6282. }
  6283. BT_DBG("Start: total len %d, frag len %d", len, skb->len);
  6284. if (skb->len > len) {
  6285. BT_ERR("Frame is too long (len %d, expected len %d)",
  6286. skb->len, len);
  6287. l2cap_conn_unreliable(conn, ECOMM);
  6288. goto drop;
  6289. }
  6290. /* Allocate skb for the complete frame (with header) */
  6291. conn->rx_skb = bt_skb_alloc(len, GFP_ATOMIC);
  6292. if (!conn->rx_skb)
  6293. goto drop;
  6294. skb_copy_from_linear_data(skb, skb_put(conn->rx_skb, skb->len),
  6295. skb->len);
  6296. conn->rx_len = len - skb->len;
  6297. } else {
  6298. BT_DBG("Cont: frag len %d (expecting %d)", skb->len, conn->rx_len);
  6299. if (!conn->rx_len) {
  6300. BT_ERR("Unexpected continuation frame (len %d)", skb->len);
  6301. l2cap_conn_unreliable(conn, ECOMM);
  6302. goto drop;
  6303. }
  6304. if (skb->len > conn->rx_len) {
  6305. BT_ERR("Fragment is too long (len %d, expected %d)",
  6306. skb->len, conn->rx_len);
  6307. kfree_skb(conn->rx_skb);
  6308. conn->rx_skb = NULL;
  6309. conn->rx_len = 0;
  6310. l2cap_conn_unreliable(conn, ECOMM);
  6311. goto drop;
  6312. }
  6313. skb_copy_from_linear_data(skb, skb_put(conn->rx_skb, skb->len),
  6314. skb->len);
  6315. conn->rx_len -= skb->len;
  6316. if (!conn->rx_len) {
  6317. /* Complete frame received */
  6318. l2cap_recv_frame(conn, conn->rx_skb);
  6319. conn->rx_skb = NULL;
  6320. }
  6321. }
  6322. drop:
  6323. kfree_skb(skb);
  6324. return 0;
  6325. }
  6326. static void l2cap_set_acl_flushto(struct hci_conn *hcon, u16 flush_to)
  6327. {
  6328. struct hci_cp_write_automatic_flush_timeout flush_tm;
  6329. if (hcon && hcon->hdev) {
  6330. flush_tm.handle = hcon->handle;
  6331. if (flush_to == L2CAP_DEFAULT_FLUSH_TO)
  6332. flush_to = 0;
  6333. flush_tm.timeout = (flush_to < L2CAP_MAX_FLUSH_TO) ?
  6334. flush_to : L2CAP_MAX_FLUSH_TO;
  6335. hci_send_cmd(hcon->hdev,
  6336. HCI_OP_WRITE_AUTOMATIC_FLUSH_TIMEOUT,
  6337. 4, &(flush_tm));
  6338. }
  6339. }
  6340. static u16 l2cap_get_smallest_flushto(struct l2cap_chan_list *l)
  6341. {
  6342. int ret_flush_to = L2CAP_DEFAULT_FLUSH_TO;
  6343. struct sock *s;
  6344. for (s = l->head; s; s = l2cap_pi(s)->next_c) {
  6345. if (l2cap_pi(s)->flush_to > 0 &&
  6346. l2cap_pi(s)->flush_to < ret_flush_to)
  6347. ret_flush_to = l2cap_pi(s)->flush_to;
  6348. }
  6349. return ret_flush_to;
  6350. }
  6351. static int l2cap_debugfs_show(struct seq_file *f, void *p)
  6352. {
  6353. struct sock *sk;
  6354. struct hlist_node *node;
  6355. read_lock_bh(&l2cap_sk_list.lock);
  6356. sk_for_each(sk, node, &l2cap_sk_list.head) {
  6357. struct l2cap_pinfo *pi = l2cap_pi(sk);
  6358. seq_printf(f, "%s %s %d %d 0x%4.4x 0x%4.4x %d %d %d %d\n",
  6359. batostr(&bt_sk(sk)->src),
  6360. batostr(&bt_sk(sk)->dst),
  6361. sk->sk_state, __le16_to_cpu(pi->psm),
  6362. pi->scid, pi->dcid,
  6363. pi->imtu, pi->omtu, pi->sec_level,
  6364. pi->mode);
  6365. }
  6366. read_unlock_bh(&l2cap_sk_list.lock);
  6367. return 0;
  6368. }
  6369. static void l2cap_queue_acl_data(struct work_struct *worker)
  6370. {
  6371. struct sock *sk = NULL;
  6372. int attempts = 0;
  6373. struct sk_buff *skb_rsp;
  6374. struct l2cap_hdr *lh;
  6375. u8 err_rsp[] = {L2CAP_ATT_ERROR, 0x00, 0x00, 0x00,
  6376. L2CAP_ATT_NOT_SUPPORTED};
  6377. for (attempts = 0; attempts < 40; attempts++) {
  6378. msleep(50);
  6379. if (!att_chn_params.conn) {
  6380. BT_DBG("att_chn_params.conn is NULL");
  6381. return;
  6382. }
  6383. sk = l2cap_find_sock_by_fixed_cid_and_dir
  6384. (att_chn_params.cid,
  6385. att_chn_params.conn->src,
  6386. att_chn_params.conn->dst,
  6387. att_chn_params.dir);
  6388. bh_lock_sock(sk);
  6389. if (sk->sk_state == BT_CONNECTED) {
  6390. sock_queue_rcv_skb(sk, att_chn_params.skb);
  6391. if (sk)
  6392. bh_unlock_sock(sk);
  6393. return;
  6394. }
  6395. bh_unlock_sock(sk);
  6396. }
  6397. bh_lock_sock(sk);
  6398. if (att_chn_params.skb->data[0] != L2CAP_ATT_INDICATE)
  6399. goto not_indicate;
  6400. /* If this is an incoming Indication, we are required to confirm */
  6401. skb_rsp = bt_skb_alloc(sizeof(u8) + L2CAP_HDR_SIZE, GFP_ATOMIC);
  6402. if (!skb_rsp)
  6403. goto free_skb;
  6404. lh = (struct l2cap_hdr *) skb_put(skb_rsp, L2CAP_HDR_SIZE);
  6405. lh->len = cpu_to_le16(sizeof(u8));
  6406. lh->cid = cpu_to_le16(L2CAP_CID_LE_DATA);
  6407. err_rsp[0] = L2CAP_ATT_CONFIRM;
  6408. memcpy(skb_put(skb_rsp, sizeof(u8)), err_rsp, sizeof(u8));
  6409. hci_send_acl(att_chn_params.conn->hcon, NULL, skb_rsp, 0);
  6410. goto free_skb;
  6411. not_indicate:
  6412. if (att_chn_params.skb->data[0] & L2CAP_ATT_RESPONSE_BIT ||
  6413. att_chn_params.skb->data[0] == L2CAP_ATT_CONFIRM)
  6414. goto free_skb;
  6415. /* If this is an incoming PDU that requires a response, respond with
  6416. * a generic error so remote device doesn't hang */
  6417. skb_rsp = bt_skb_alloc(sizeof(err_rsp) + L2CAP_HDR_SIZE, GFP_ATOMIC);
  6418. if (!skb_rsp)
  6419. goto free_skb;
  6420. lh = (struct l2cap_hdr *) skb_put(skb_rsp, L2CAP_HDR_SIZE);
  6421. lh->len = cpu_to_le16(sizeof(err_rsp));
  6422. lh->cid = cpu_to_le16(L2CAP_CID_LE_DATA);
  6423. err_rsp[1] = att_chn_params.skb->data[0];
  6424. memcpy(skb_put(skb_rsp, sizeof(err_rsp)), err_rsp, sizeof(err_rsp));
  6425. hci_send_acl(att_chn_params.conn->hcon, NULL, skb_rsp, 0);
  6426. free_skb:
  6427. kfree_skb(att_chn_params.skb);
  6428. if (sk)
  6429. bh_unlock_sock(sk);
  6430. }
  6431. static int l2cap_debugfs_open(struct inode *inode, struct file *file)
  6432. {
  6433. return single_open(file, l2cap_debugfs_show, inode->i_private);
  6434. }
  6435. static const struct file_operations l2cap_debugfs_fops = {
  6436. .open = l2cap_debugfs_open,
  6437. .read = seq_read,
  6438. .llseek = seq_lseek,
  6439. .release = single_release,
  6440. };
  6441. static struct dentry *l2cap_debugfs;
  6442. static struct hci_proto l2cap_hci_proto = {
  6443. .name = "L2CAP",
  6444. .id = HCI_PROTO_L2CAP,
  6445. .connect_ind = l2cap_connect_ind,
  6446. .connect_cfm = l2cap_connect_cfm,
  6447. .disconn_ind = l2cap_disconn_ind,
  6448. .disconn_cfm = l2cap_disconn_cfm,
  6449. .security_cfm = l2cap_security_cfm,
  6450. .recv_acldata = l2cap_recv_acldata,
  6451. .create_cfm = l2cap_create_cfm,
  6452. .modify_cfm = l2cap_modify_cfm,
  6453. .destroy_cfm = l2cap_destroy_cfm,
  6454. };
  6455. int __init l2cap_init(void)
  6456. {
  6457. int err;
  6458. err = l2cap_init_sockets();
  6459. if (err < 0)
  6460. return err;
  6461. _l2cap_wq = create_singlethread_workqueue("l2cap");
  6462. if (!_l2cap_wq) {
  6463. err = -ENOMEM;
  6464. goto error;
  6465. }
  6466. err = hci_register_proto(&l2cap_hci_proto);
  6467. if (err < 0) {
  6468. BT_ERR("L2CAP protocol registration failed");
  6469. bt_sock_unregister(BTPROTO_L2CAP);
  6470. goto error;
  6471. }
  6472. if (bt_debugfs) {
  6473. l2cap_debugfs = debugfs_create_file("l2cap", 0444,
  6474. bt_debugfs, NULL, &l2cap_debugfs_fops);
  6475. if (!l2cap_debugfs)
  6476. BT_ERR("Failed to create L2CAP debug file");
  6477. }
  6478. if (amp_init() < 0) {
  6479. BT_ERR("AMP Manager initialization failed");
  6480. goto error;
  6481. }
  6482. return 0;
  6483. error:
  6484. destroy_workqueue(_l2cap_wq);
  6485. l2cap_cleanup_sockets();
  6486. return err;
  6487. }
  6488. void l2cap_exit(void)
  6489. {
  6490. amp_exit();
  6491. debugfs_remove(l2cap_debugfs);
  6492. flush_workqueue(_l2cap_wq);
  6493. destroy_workqueue(_l2cap_wq);
  6494. if (hci_unregister_proto(&l2cap_hci_proto) < 0)
  6495. BT_ERR("L2CAP protocol unregistration failed");
  6496. l2cap_cleanup_sockets();
  6497. }
  6498. module_param(disable_ertm, bool, 0644);
  6499. MODULE_PARM_DESC(disable_ertm, "Disable enhanced retransmission mode");
  6500. module_param(enable_hs, bool, 0644);
  6501. MODULE_PARM_DESC(enable_hs, "Enable A2MP protocol");
  6502. module_param(enable_reconfig, bool, 0644);
  6503. MODULE_PARM_DESC(enable_reconfig, "Enable reconfig after initiating AMP move");