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