driver_nl80211.c 225 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540
  1. /*
  2. * Driver interaction with Linux nl80211/cfg80211
  3. * Copyright (c) 2002-2015, Jouni Malinen <j@w1.fi>
  4. * Copyright (c) 2003-2004, Instant802 Networks, Inc.
  5. * Copyright (c) 2005-2006, Devicescape Software, Inc.
  6. * Copyright (c) 2007, Johannes Berg <johannes@sipsolutions.net>
  7. * Copyright (c) 2009-2010, Atheros Communications
  8. *
  9. * This software may be distributed under the terms of the BSD license.
  10. * See README for more details.
  11. */
  12. #include "includes.h"
  13. #include <sys/types.h>
  14. #include <fcntl.h>
  15. #include <net/if.h>
  16. #include <netlink/genl/genl.h>
  17. #include <netlink/genl/ctrl.h>
  18. #ifdef CONFIG_LIBNL3_ROUTE
  19. #include <netlink/route/neighbour.h>
  20. #endif /* CONFIG_LIBNL3_ROUTE */
  21. #include <linux/rtnetlink.h>
  22. #include <netpacket/packet.h>
  23. #include <linux/errqueue.h>
  24. #include "common.h"
  25. #include "eloop.h"
  26. #include "common/qca-vendor.h"
  27. #include "common/qca-vendor-attr.h"
  28. #include "common/ieee802_11_defs.h"
  29. #include "common/ieee802_11_common.h"
  30. #include "l2_packet/l2_packet.h"
  31. #include "netlink.h"
  32. #include "linux_defines.h"
  33. #include "linux_ioctl.h"
  34. #include "radiotap.h"
  35. #include "radiotap_iter.h"
  36. #include "rfkill.h"
  37. #include "driver_nl80211.h"
  38. #ifndef CONFIG_LIBNL20
  39. /*
  40. * libnl 1.1 has a bug, it tries to allocate socket numbers densely
  41. * but when you free a socket again it will mess up its bitmap and
  42. * and use the wrong number the next time it needs a socket ID.
  43. * Therefore, we wrap the handle alloc/destroy and add our own pid
  44. * accounting.
  45. */
  46. static uint32_t port_bitmap[32] = { 0 };
  47. static struct nl_handle *nl80211_handle_alloc(void *cb)
  48. {
  49. struct nl_handle *handle;
  50. uint32_t pid = getpid() & 0x3FFFFF;
  51. int i;
  52. handle = nl_handle_alloc_cb(cb);
  53. for (i = 0; i < 1024; i++) {
  54. if (port_bitmap[i / 32] & (1 << (i % 32)))
  55. continue;
  56. port_bitmap[i / 32] |= 1 << (i % 32);
  57. pid += i << 22;
  58. break;
  59. }
  60. nl_socket_set_local_port(handle, pid);
  61. return handle;
  62. }
  63. static void nl80211_handle_destroy(struct nl_handle *handle)
  64. {
  65. uint32_t port = nl_socket_get_local_port(handle);
  66. port >>= 22;
  67. port_bitmap[port / 32] &= ~(1 << (port % 32));
  68. nl_handle_destroy(handle);
  69. }
  70. #endif /* CONFIG_LIBNL20 */
  71. #ifdef ANDROID
  72. /* system/core/libnl_2 does not include nl_socket_set_nonblocking() */
  73. #undef nl_socket_set_nonblocking
  74. #define nl_socket_set_nonblocking(h) android_nl_socket_set_nonblocking(h)
  75. #define genl_ctrl_resolve android_genl_ctrl_resolve
  76. #endif /* ANDROID */
  77. static struct nl_handle * nl_create_handle(struct nl_cb *cb, const char *dbg)
  78. {
  79. struct nl_handle *handle;
  80. handle = nl80211_handle_alloc(cb);
  81. if (handle == NULL) {
  82. wpa_printf(MSG_ERROR, "nl80211: Failed to allocate netlink "
  83. "callbacks (%s)", dbg);
  84. return NULL;
  85. }
  86. if (genl_connect(handle)) {
  87. wpa_printf(MSG_ERROR, "nl80211: Failed to connect to generic "
  88. "netlink (%s)", dbg);
  89. nl80211_handle_destroy(handle);
  90. return NULL;
  91. }
  92. return handle;
  93. }
  94. static void nl_destroy_handles(struct nl_handle **handle)
  95. {
  96. if (*handle == NULL)
  97. return;
  98. nl80211_handle_destroy(*handle);
  99. *handle = NULL;
  100. }
  101. #if __WORDSIZE == 64
  102. #define ELOOP_SOCKET_INVALID (intptr_t) 0x8888888888888889ULL
  103. #else
  104. #define ELOOP_SOCKET_INVALID (intptr_t) 0x88888889ULL
  105. #endif
  106. static void nl80211_register_eloop_read(struct nl_handle **handle,
  107. eloop_sock_handler handler,
  108. void *eloop_data)
  109. {
  110. #ifdef CONFIG_LIBNL20
  111. /*
  112. * libnl uses a pretty small buffer (32 kB that gets converted to 64 kB)
  113. * by default. It is possible to hit that limit in some cases where
  114. * operations are blocked, e.g., with a burst of Deauthentication frames
  115. * to hostapd and STA entry deletion. Try to increase the buffer to make
  116. * this less likely to occur.
  117. */
  118. if (nl_socket_set_buffer_size(*handle, 262144, 0) < 0) {
  119. wpa_printf(MSG_DEBUG,
  120. "nl80211: Could not set nl_socket RX buffer size: %s",
  121. strerror(errno));
  122. /* continue anyway with the default (smaller) buffer */
  123. }
  124. #endif /* CONFIG_LIBNL20 */
  125. nl_socket_set_nonblocking(*handle);
  126. eloop_register_read_sock(nl_socket_get_fd(*handle), handler,
  127. eloop_data, *handle);
  128. *handle = (void *) (((intptr_t) *handle) ^ ELOOP_SOCKET_INVALID);
  129. }
  130. static void nl80211_destroy_eloop_handle(struct nl_handle **handle)
  131. {
  132. *handle = (void *) (((intptr_t) *handle) ^ ELOOP_SOCKET_INVALID);
  133. eloop_unregister_read_sock(nl_socket_get_fd(*handle));
  134. nl_destroy_handles(handle);
  135. }
  136. static void nl80211_global_deinit(void *priv);
  137. static void nl80211_check_global(struct nl80211_global *global);
  138. static void wpa_driver_nl80211_deinit(struct i802_bss *bss);
  139. static int wpa_driver_nl80211_set_mode_ibss(struct i802_bss *bss,
  140. struct hostapd_freq_params *freq);
  141. static int
  142. wpa_driver_nl80211_finish_drv_init(struct wpa_driver_nl80211_data *drv,
  143. const u8 *set_addr, int first,
  144. const char *driver_params);
  145. static int nl80211_send_frame_cmd(struct i802_bss *bss,
  146. unsigned int freq, unsigned int wait,
  147. const u8 *buf, size_t buf_len, u64 *cookie,
  148. int no_cck, int no_ack, int offchanok);
  149. static int wpa_driver_nl80211_probe_req_report(struct i802_bss *bss,
  150. int report);
  151. static void add_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx);
  152. static void del_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx);
  153. static int have_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx);
  154. static int nl80211_set_channel(struct i802_bss *bss,
  155. struct hostapd_freq_params *freq, int set_chan);
  156. static int nl80211_disable_11b_rates(struct wpa_driver_nl80211_data *drv,
  157. int ifindex, int disabled);
  158. static int nl80211_leave_ibss(struct wpa_driver_nl80211_data *drv,
  159. int reset_mode);
  160. static int i802_set_iface_flags(struct i802_bss *bss, int up);
  161. static int nl80211_set_param(void *priv, const char *param);
  162. /* Converts nl80211_chan_width to a common format */
  163. enum chan_width convert2width(int width)
  164. {
  165. switch (width) {
  166. case NL80211_CHAN_WIDTH_20_NOHT:
  167. return CHAN_WIDTH_20_NOHT;
  168. case NL80211_CHAN_WIDTH_20:
  169. return CHAN_WIDTH_20;
  170. case NL80211_CHAN_WIDTH_40:
  171. return CHAN_WIDTH_40;
  172. case NL80211_CHAN_WIDTH_80:
  173. return CHAN_WIDTH_80;
  174. case NL80211_CHAN_WIDTH_80P80:
  175. return CHAN_WIDTH_80P80;
  176. case NL80211_CHAN_WIDTH_160:
  177. return CHAN_WIDTH_160;
  178. }
  179. return CHAN_WIDTH_UNKNOWN;
  180. }
  181. int is_ap_interface(enum nl80211_iftype nlmode)
  182. {
  183. return nlmode == NL80211_IFTYPE_AP ||
  184. nlmode == NL80211_IFTYPE_P2P_GO;
  185. }
  186. int is_sta_interface(enum nl80211_iftype nlmode)
  187. {
  188. return nlmode == NL80211_IFTYPE_STATION ||
  189. nlmode == NL80211_IFTYPE_P2P_CLIENT;
  190. }
  191. static int is_p2p_net_interface(enum nl80211_iftype nlmode)
  192. {
  193. return nlmode == NL80211_IFTYPE_P2P_CLIENT ||
  194. nlmode == NL80211_IFTYPE_P2P_GO;
  195. }
  196. struct i802_bss * get_bss_ifindex(struct wpa_driver_nl80211_data *drv,
  197. int ifindex)
  198. {
  199. struct i802_bss *bss;
  200. for (bss = drv->first_bss; bss; bss = bss->next) {
  201. if (bss->ifindex == ifindex)
  202. return bss;
  203. }
  204. return NULL;
  205. }
  206. static int is_mesh_interface(enum nl80211_iftype nlmode)
  207. {
  208. return nlmode == NL80211_IFTYPE_MESH_POINT;
  209. }
  210. void nl80211_mark_disconnected(struct wpa_driver_nl80211_data *drv)
  211. {
  212. if (drv->associated)
  213. os_memcpy(drv->prev_bssid, drv->bssid, ETH_ALEN);
  214. drv->associated = 0;
  215. os_memset(drv->bssid, 0, ETH_ALEN);
  216. }
  217. /* nl80211 code */
  218. static int ack_handler(struct nl_msg *msg, void *arg)
  219. {
  220. int *err = arg;
  221. *err = 0;
  222. return NL_STOP;
  223. }
  224. static int finish_handler(struct nl_msg *msg, void *arg)
  225. {
  226. int *ret = arg;
  227. *ret = 0;
  228. return NL_SKIP;
  229. }
  230. static int error_handler(struct sockaddr_nl *nla, struct nlmsgerr *err,
  231. void *arg)
  232. {
  233. int *ret = arg;
  234. *ret = err->error;
  235. return NL_SKIP;
  236. }
  237. static int no_seq_check(struct nl_msg *msg, void *arg)
  238. {
  239. return NL_OK;
  240. }
  241. static void nl80211_nlmsg_clear(struct nl_msg *msg)
  242. {
  243. /*
  244. * Clear nlmsg data, e.g., to make sure key material is not left in
  245. * heap memory for unnecessarily long time.
  246. */
  247. if (msg) {
  248. struct nlmsghdr *hdr = nlmsg_hdr(msg);
  249. void *data = nlmsg_data(hdr);
  250. /*
  251. * This would use nlmsg_datalen() or the older nlmsg_len() if
  252. * only libnl were to maintain a stable API.. Neither will work
  253. * with all released versions, so just calculate the length
  254. * here.
  255. */
  256. int len = hdr->nlmsg_len - NLMSG_HDRLEN;
  257. os_memset(data, 0, len);
  258. }
  259. }
  260. static int send_and_recv(struct nl80211_global *global,
  261. struct nl_handle *nl_handle, struct nl_msg *msg,
  262. int (*valid_handler)(struct nl_msg *, void *),
  263. void *valid_data)
  264. {
  265. struct nl_cb *cb;
  266. int err = -ENOMEM;
  267. if (!msg)
  268. return -ENOMEM;
  269. cb = nl_cb_clone(global->nl_cb);
  270. if (!cb)
  271. goto out;
  272. err = nl_send_auto_complete(nl_handle, msg);
  273. if (err < 0)
  274. goto out;
  275. err = 1;
  276. nl_cb_err(cb, NL_CB_CUSTOM, error_handler, &err);
  277. nl_cb_set(cb, NL_CB_FINISH, NL_CB_CUSTOM, finish_handler, &err);
  278. nl_cb_set(cb, NL_CB_ACK, NL_CB_CUSTOM, ack_handler, &err);
  279. if (valid_handler)
  280. nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM,
  281. valid_handler, valid_data);
  282. while (err > 0) {
  283. int res = nl_recvmsgs(nl_handle, cb);
  284. if (res < 0) {
  285. wpa_printf(MSG_INFO,
  286. "nl80211: %s->nl_recvmsgs failed: %d",
  287. __func__, res);
  288. }
  289. }
  290. out:
  291. nl_cb_put(cb);
  292. if (!valid_handler && valid_data == (void *) -1)
  293. nl80211_nlmsg_clear(msg);
  294. nlmsg_free(msg);
  295. return err;
  296. }
  297. int send_and_recv_msgs(struct wpa_driver_nl80211_data *drv,
  298. struct nl_msg *msg,
  299. int (*valid_handler)(struct nl_msg *, void *),
  300. void *valid_data)
  301. {
  302. return send_and_recv(drv->global, drv->global->nl, msg,
  303. valid_handler, valid_data);
  304. }
  305. struct family_data {
  306. const char *group;
  307. int id;
  308. };
  309. static int family_handler(struct nl_msg *msg, void *arg)
  310. {
  311. struct family_data *res = arg;
  312. struct nlattr *tb[CTRL_ATTR_MAX + 1];
  313. struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
  314. struct nlattr *mcgrp;
  315. int i;
  316. nla_parse(tb, CTRL_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
  317. genlmsg_attrlen(gnlh, 0), NULL);
  318. if (!tb[CTRL_ATTR_MCAST_GROUPS])
  319. return NL_SKIP;
  320. nla_for_each_nested(mcgrp, tb[CTRL_ATTR_MCAST_GROUPS], i) {
  321. struct nlattr *tb2[CTRL_ATTR_MCAST_GRP_MAX + 1];
  322. nla_parse(tb2, CTRL_ATTR_MCAST_GRP_MAX, nla_data(mcgrp),
  323. nla_len(mcgrp), NULL);
  324. if (!tb2[CTRL_ATTR_MCAST_GRP_NAME] ||
  325. !tb2[CTRL_ATTR_MCAST_GRP_ID] ||
  326. os_strncmp(nla_data(tb2[CTRL_ATTR_MCAST_GRP_NAME]),
  327. res->group,
  328. nla_len(tb2[CTRL_ATTR_MCAST_GRP_NAME])) != 0)
  329. continue;
  330. res->id = nla_get_u32(tb2[CTRL_ATTR_MCAST_GRP_ID]);
  331. break;
  332. };
  333. return NL_SKIP;
  334. }
  335. static int nl_get_multicast_id(struct nl80211_global *global,
  336. const char *family, const char *group)
  337. {
  338. struct nl_msg *msg;
  339. int ret;
  340. struct family_data res = { group, -ENOENT };
  341. msg = nlmsg_alloc();
  342. if (!msg)
  343. return -ENOMEM;
  344. if (!genlmsg_put(msg, 0, 0, genl_ctrl_resolve(global->nl, "nlctrl"),
  345. 0, 0, CTRL_CMD_GETFAMILY, 0) ||
  346. nla_put_string(msg, CTRL_ATTR_FAMILY_NAME, family)) {
  347. nlmsg_free(msg);
  348. return -1;
  349. }
  350. ret = send_and_recv(global, global->nl, msg, family_handler, &res);
  351. if (ret == 0)
  352. ret = res.id;
  353. return ret;
  354. }
  355. void * nl80211_cmd(struct wpa_driver_nl80211_data *drv,
  356. struct nl_msg *msg, int flags, uint8_t cmd)
  357. {
  358. return genlmsg_put(msg, 0, 0, drv->global->nl80211_id,
  359. 0, flags, cmd, 0);
  360. }
  361. static int nl80211_set_iface_id(struct nl_msg *msg, struct i802_bss *bss)
  362. {
  363. if (bss->wdev_id_set)
  364. return nla_put_u64(msg, NL80211_ATTR_WDEV, bss->wdev_id);
  365. return nla_put_u32(msg, NL80211_ATTR_IFINDEX, bss->ifindex);
  366. }
  367. struct nl_msg * nl80211_cmd_msg(struct i802_bss *bss, int flags, uint8_t cmd)
  368. {
  369. struct nl_msg *msg;
  370. msg = nlmsg_alloc();
  371. if (!msg)
  372. return NULL;
  373. if (!nl80211_cmd(bss->drv, msg, flags, cmd) ||
  374. nl80211_set_iface_id(msg, bss) < 0) {
  375. nlmsg_free(msg);
  376. return NULL;
  377. }
  378. return msg;
  379. }
  380. static struct nl_msg *
  381. nl80211_ifindex_msg(struct wpa_driver_nl80211_data *drv, int ifindex,
  382. int flags, uint8_t cmd)
  383. {
  384. struct nl_msg *msg;
  385. msg = nlmsg_alloc();
  386. if (!msg)
  387. return NULL;
  388. if (!nl80211_cmd(drv, msg, flags, cmd) ||
  389. nla_put_u32(msg, NL80211_ATTR_IFINDEX, ifindex)) {
  390. nlmsg_free(msg);
  391. return NULL;
  392. }
  393. return msg;
  394. }
  395. struct nl_msg * nl80211_drv_msg(struct wpa_driver_nl80211_data *drv, int flags,
  396. uint8_t cmd)
  397. {
  398. return nl80211_ifindex_msg(drv, drv->ifindex, flags, cmd);
  399. }
  400. struct nl_msg * nl80211_bss_msg(struct i802_bss *bss, int flags, uint8_t cmd)
  401. {
  402. return nl80211_ifindex_msg(bss->drv, bss->ifindex, flags, cmd);
  403. }
  404. struct wiphy_idx_data {
  405. int wiphy_idx;
  406. enum nl80211_iftype nlmode;
  407. u8 *macaddr;
  408. };
  409. static int netdev_info_handler(struct nl_msg *msg, void *arg)
  410. {
  411. struct nlattr *tb[NL80211_ATTR_MAX + 1];
  412. struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
  413. struct wiphy_idx_data *info = arg;
  414. nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
  415. genlmsg_attrlen(gnlh, 0), NULL);
  416. if (tb[NL80211_ATTR_WIPHY])
  417. info->wiphy_idx = nla_get_u32(tb[NL80211_ATTR_WIPHY]);
  418. if (tb[NL80211_ATTR_IFTYPE])
  419. info->nlmode = nla_get_u32(tb[NL80211_ATTR_IFTYPE]);
  420. if (tb[NL80211_ATTR_MAC] && info->macaddr)
  421. os_memcpy(info->macaddr, nla_data(tb[NL80211_ATTR_MAC]),
  422. ETH_ALEN);
  423. return NL_SKIP;
  424. }
  425. int nl80211_get_wiphy_index(struct i802_bss *bss)
  426. {
  427. struct nl_msg *msg;
  428. struct wiphy_idx_data data = {
  429. .wiphy_idx = -1,
  430. .macaddr = NULL,
  431. };
  432. if (!(msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_GET_INTERFACE)))
  433. return -1;
  434. if (send_and_recv_msgs(bss->drv, msg, netdev_info_handler, &data) == 0)
  435. return data.wiphy_idx;
  436. return -1;
  437. }
  438. static enum nl80211_iftype nl80211_get_ifmode(struct i802_bss *bss)
  439. {
  440. struct nl_msg *msg;
  441. struct wiphy_idx_data data = {
  442. .nlmode = NL80211_IFTYPE_UNSPECIFIED,
  443. .macaddr = NULL,
  444. };
  445. if (!(msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_GET_INTERFACE)))
  446. return NL80211_IFTYPE_UNSPECIFIED;
  447. if (send_and_recv_msgs(bss->drv, msg, netdev_info_handler, &data) == 0)
  448. return data.nlmode;
  449. return NL80211_IFTYPE_UNSPECIFIED;
  450. }
  451. static int nl80211_get_macaddr(struct i802_bss *bss)
  452. {
  453. struct nl_msg *msg;
  454. struct wiphy_idx_data data = {
  455. .macaddr = bss->addr,
  456. };
  457. if (!(msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_GET_INTERFACE)))
  458. return -1;
  459. return send_and_recv_msgs(bss->drv, msg, netdev_info_handler, &data);
  460. }
  461. static int nl80211_register_beacons(struct wpa_driver_nl80211_data *drv,
  462. struct nl80211_wiphy_data *w)
  463. {
  464. struct nl_msg *msg;
  465. int ret;
  466. msg = nlmsg_alloc();
  467. if (!msg)
  468. return -1;
  469. if (!nl80211_cmd(drv, msg, 0, NL80211_CMD_REGISTER_BEACONS) ||
  470. nla_put_u32(msg, NL80211_ATTR_WIPHY, w->wiphy_idx)) {
  471. nlmsg_free(msg);
  472. return -1;
  473. }
  474. ret = send_and_recv(drv->global, w->nl_beacons, msg, NULL, NULL);
  475. if (ret) {
  476. wpa_printf(MSG_DEBUG, "nl80211: Register beacons command "
  477. "failed: ret=%d (%s)",
  478. ret, strerror(-ret));
  479. }
  480. return ret;
  481. }
  482. static void nl80211_recv_beacons(int sock, void *eloop_ctx, void *handle)
  483. {
  484. struct nl80211_wiphy_data *w = eloop_ctx;
  485. int res;
  486. wpa_printf(MSG_EXCESSIVE, "nl80211: Beacon event message available");
  487. res = nl_recvmsgs(handle, w->nl_cb);
  488. if (res < 0) {
  489. wpa_printf(MSG_INFO, "nl80211: %s->nl_recvmsgs failed: %d",
  490. __func__, res);
  491. }
  492. }
  493. static int process_beacon_event(struct nl_msg *msg, void *arg)
  494. {
  495. struct nl80211_wiphy_data *w = arg;
  496. struct wpa_driver_nl80211_data *drv;
  497. struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
  498. struct nlattr *tb[NL80211_ATTR_MAX + 1];
  499. union wpa_event_data event;
  500. nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
  501. genlmsg_attrlen(gnlh, 0), NULL);
  502. if (gnlh->cmd != NL80211_CMD_FRAME) {
  503. wpa_printf(MSG_DEBUG, "nl80211: Unexpected beacon event? (%d)",
  504. gnlh->cmd);
  505. return NL_SKIP;
  506. }
  507. if (!tb[NL80211_ATTR_FRAME])
  508. return NL_SKIP;
  509. dl_list_for_each(drv, &w->drvs, struct wpa_driver_nl80211_data,
  510. wiphy_list) {
  511. os_memset(&event, 0, sizeof(event));
  512. event.rx_mgmt.frame = nla_data(tb[NL80211_ATTR_FRAME]);
  513. event.rx_mgmt.frame_len = nla_len(tb[NL80211_ATTR_FRAME]);
  514. wpa_supplicant_event(drv->ctx, EVENT_RX_MGMT, &event);
  515. }
  516. return NL_SKIP;
  517. }
  518. static struct nl80211_wiphy_data *
  519. nl80211_get_wiphy_data_ap(struct i802_bss *bss)
  520. {
  521. static DEFINE_DL_LIST(nl80211_wiphys);
  522. struct nl80211_wiphy_data *w;
  523. int wiphy_idx, found = 0;
  524. struct i802_bss *tmp_bss;
  525. if (bss->wiphy_data != NULL)
  526. return bss->wiphy_data;
  527. wiphy_idx = nl80211_get_wiphy_index(bss);
  528. dl_list_for_each(w, &nl80211_wiphys, struct nl80211_wiphy_data, list) {
  529. if (w->wiphy_idx == wiphy_idx)
  530. goto add;
  531. }
  532. /* alloc new one */
  533. w = os_zalloc(sizeof(*w));
  534. if (w == NULL)
  535. return NULL;
  536. w->wiphy_idx = wiphy_idx;
  537. dl_list_init(&w->bsss);
  538. dl_list_init(&w->drvs);
  539. w->nl_cb = nl_cb_alloc(NL_CB_DEFAULT);
  540. if (!w->nl_cb) {
  541. os_free(w);
  542. return NULL;
  543. }
  544. nl_cb_set(w->nl_cb, NL_CB_SEQ_CHECK, NL_CB_CUSTOM, no_seq_check, NULL);
  545. nl_cb_set(w->nl_cb, NL_CB_VALID, NL_CB_CUSTOM, process_beacon_event,
  546. w);
  547. w->nl_beacons = nl_create_handle(bss->drv->global->nl_cb,
  548. "wiphy beacons");
  549. if (w->nl_beacons == NULL) {
  550. os_free(w);
  551. return NULL;
  552. }
  553. if (nl80211_register_beacons(bss->drv, w)) {
  554. nl_destroy_handles(&w->nl_beacons);
  555. os_free(w);
  556. return NULL;
  557. }
  558. nl80211_register_eloop_read(&w->nl_beacons, nl80211_recv_beacons, w);
  559. dl_list_add(&nl80211_wiphys, &w->list);
  560. add:
  561. /* drv entry for this bss already there? */
  562. dl_list_for_each(tmp_bss, &w->bsss, struct i802_bss, wiphy_list) {
  563. if (tmp_bss->drv == bss->drv) {
  564. found = 1;
  565. break;
  566. }
  567. }
  568. /* if not add it */
  569. if (!found)
  570. dl_list_add(&w->drvs, &bss->drv->wiphy_list);
  571. dl_list_add(&w->bsss, &bss->wiphy_list);
  572. bss->wiphy_data = w;
  573. return w;
  574. }
  575. static void nl80211_put_wiphy_data_ap(struct i802_bss *bss)
  576. {
  577. struct nl80211_wiphy_data *w = bss->wiphy_data;
  578. struct i802_bss *tmp_bss;
  579. int found = 0;
  580. if (w == NULL)
  581. return;
  582. bss->wiphy_data = NULL;
  583. dl_list_del(&bss->wiphy_list);
  584. /* still any for this drv present? */
  585. dl_list_for_each(tmp_bss, &w->bsss, struct i802_bss, wiphy_list) {
  586. if (tmp_bss->drv == bss->drv) {
  587. found = 1;
  588. break;
  589. }
  590. }
  591. /* if not remove it */
  592. if (!found)
  593. dl_list_del(&bss->drv->wiphy_list);
  594. if (!dl_list_empty(&w->bsss))
  595. return;
  596. nl80211_destroy_eloop_handle(&w->nl_beacons);
  597. nl_cb_put(w->nl_cb);
  598. dl_list_del(&w->list);
  599. os_free(w);
  600. }
  601. static int wpa_driver_nl80211_get_bssid(void *priv, u8 *bssid)
  602. {
  603. struct i802_bss *bss = priv;
  604. struct wpa_driver_nl80211_data *drv = bss->drv;
  605. if (!drv->associated)
  606. return -1;
  607. os_memcpy(bssid, drv->bssid, ETH_ALEN);
  608. return 0;
  609. }
  610. static int wpa_driver_nl80211_get_ssid(void *priv, u8 *ssid)
  611. {
  612. struct i802_bss *bss = priv;
  613. struct wpa_driver_nl80211_data *drv = bss->drv;
  614. if (!drv->associated)
  615. return -1;
  616. os_memcpy(ssid, drv->ssid, drv->ssid_len);
  617. return drv->ssid_len;
  618. }
  619. static void wpa_driver_nl80211_event_newlink(
  620. struct wpa_driver_nl80211_data *drv, const char *ifname)
  621. {
  622. union wpa_event_data event;
  623. if (os_strcmp(drv->first_bss->ifname, ifname) == 0) {
  624. if (if_nametoindex(drv->first_bss->ifname) == 0) {
  625. wpa_printf(MSG_DEBUG, "nl80211: Interface %s does not exist - ignore RTM_NEWLINK",
  626. drv->first_bss->ifname);
  627. return;
  628. }
  629. if (!drv->if_removed)
  630. return;
  631. wpa_printf(MSG_DEBUG, "nl80211: Mark if_removed=0 for %s based on RTM_NEWLINK event",
  632. drv->first_bss->ifname);
  633. drv->if_removed = 0;
  634. }
  635. os_memset(&event, 0, sizeof(event));
  636. os_strlcpy(event.interface_status.ifname, ifname,
  637. sizeof(event.interface_status.ifname));
  638. event.interface_status.ievent = EVENT_INTERFACE_ADDED;
  639. wpa_supplicant_event(drv->ctx, EVENT_INTERFACE_STATUS, &event);
  640. }
  641. static void wpa_driver_nl80211_event_dellink(
  642. struct wpa_driver_nl80211_data *drv, const char *ifname)
  643. {
  644. union wpa_event_data event;
  645. if (os_strcmp(drv->first_bss->ifname, ifname) == 0) {
  646. if (drv->if_removed) {
  647. wpa_printf(MSG_DEBUG, "nl80211: if_removed already set - ignore RTM_DELLINK event for %s",
  648. ifname);
  649. return;
  650. }
  651. wpa_printf(MSG_DEBUG, "RTM_DELLINK: Interface '%s' removed - mark if_removed=1",
  652. ifname);
  653. drv->if_removed = 1;
  654. } else {
  655. wpa_printf(MSG_DEBUG, "RTM_DELLINK: Interface '%s' removed",
  656. ifname);
  657. }
  658. os_memset(&event, 0, sizeof(event));
  659. os_strlcpy(event.interface_status.ifname, ifname,
  660. sizeof(event.interface_status.ifname));
  661. event.interface_status.ievent = EVENT_INTERFACE_REMOVED;
  662. wpa_supplicant_event(drv->ctx, EVENT_INTERFACE_STATUS, &event);
  663. }
  664. static int wpa_driver_nl80211_own_ifname(struct wpa_driver_nl80211_data *drv,
  665. u8 *buf, size_t len)
  666. {
  667. int attrlen, rta_len;
  668. struct rtattr *attr;
  669. attrlen = len;
  670. attr = (struct rtattr *) buf;
  671. rta_len = RTA_ALIGN(sizeof(struct rtattr));
  672. while (RTA_OK(attr, attrlen)) {
  673. if (attr->rta_type == IFLA_IFNAME) {
  674. if (os_strcmp(((char *) attr) + rta_len,
  675. drv->first_bss->ifname) == 0)
  676. return 1;
  677. else
  678. break;
  679. }
  680. attr = RTA_NEXT(attr, attrlen);
  681. }
  682. return 0;
  683. }
  684. static int wpa_driver_nl80211_own_ifindex(struct wpa_driver_nl80211_data *drv,
  685. int ifindex, u8 *buf, size_t len)
  686. {
  687. if (drv->ifindex == ifindex)
  688. return 1;
  689. if (drv->if_removed && wpa_driver_nl80211_own_ifname(drv, buf, len)) {
  690. nl80211_check_global(drv->global);
  691. wpa_printf(MSG_DEBUG, "nl80211: Update ifindex for a removed "
  692. "interface");
  693. wpa_driver_nl80211_finish_drv_init(drv, NULL, 0, NULL);
  694. return 1;
  695. }
  696. return 0;
  697. }
  698. static struct wpa_driver_nl80211_data *
  699. nl80211_find_drv(struct nl80211_global *global, int idx, u8 *buf, size_t len)
  700. {
  701. struct wpa_driver_nl80211_data *drv;
  702. dl_list_for_each(drv, &global->interfaces,
  703. struct wpa_driver_nl80211_data, list) {
  704. if (wpa_driver_nl80211_own_ifindex(drv, idx, buf, len) ||
  705. have_ifidx(drv, idx))
  706. return drv;
  707. }
  708. return NULL;
  709. }
  710. static void wpa_driver_nl80211_event_rtm_newlink(void *ctx,
  711. struct ifinfomsg *ifi,
  712. u8 *buf, size_t len)
  713. {
  714. struct nl80211_global *global = ctx;
  715. struct wpa_driver_nl80211_data *drv;
  716. int attrlen;
  717. struct rtattr *attr;
  718. u32 brid = 0;
  719. char namebuf[IFNAMSIZ];
  720. char ifname[IFNAMSIZ + 1];
  721. char extra[100], *pos, *end;
  722. drv = nl80211_find_drv(global, ifi->ifi_index, buf, len);
  723. if (!drv) {
  724. wpa_printf(MSG_DEBUG, "nl80211: Ignore RTM_NEWLINK event for foreign ifindex %d",
  725. ifi->ifi_index);
  726. return;
  727. }
  728. extra[0] = '\0';
  729. pos = extra;
  730. end = pos + sizeof(extra);
  731. ifname[0] = '\0';
  732. attrlen = len;
  733. attr = (struct rtattr *) buf;
  734. while (RTA_OK(attr, attrlen)) {
  735. switch (attr->rta_type) {
  736. case IFLA_IFNAME:
  737. if (RTA_PAYLOAD(attr) >= IFNAMSIZ)
  738. break;
  739. os_memcpy(ifname, RTA_DATA(attr), RTA_PAYLOAD(attr));
  740. ifname[RTA_PAYLOAD(attr)] = '\0';
  741. break;
  742. case IFLA_MASTER:
  743. brid = nla_get_u32((struct nlattr *) attr);
  744. pos += os_snprintf(pos, end - pos, " master=%u", brid);
  745. break;
  746. case IFLA_WIRELESS:
  747. pos += os_snprintf(pos, end - pos, " wext");
  748. break;
  749. case IFLA_OPERSTATE:
  750. pos += os_snprintf(pos, end - pos, " operstate=%u",
  751. nla_get_u32((struct nlattr *) attr));
  752. break;
  753. case IFLA_LINKMODE:
  754. pos += os_snprintf(pos, end - pos, " linkmode=%u",
  755. nla_get_u32((struct nlattr *) attr));
  756. break;
  757. }
  758. attr = RTA_NEXT(attr, attrlen);
  759. }
  760. extra[sizeof(extra) - 1] = '\0';
  761. wpa_printf(MSG_DEBUG, "RTM_NEWLINK: ifi_index=%d ifname=%s%s ifi_family=%d ifi_flags=0x%x (%s%s%s%s)",
  762. ifi->ifi_index, ifname, extra, ifi->ifi_family,
  763. ifi->ifi_flags,
  764. (ifi->ifi_flags & IFF_UP) ? "[UP]" : "",
  765. (ifi->ifi_flags & IFF_RUNNING) ? "[RUNNING]" : "",
  766. (ifi->ifi_flags & IFF_LOWER_UP) ? "[LOWER_UP]" : "",
  767. (ifi->ifi_flags & IFF_DORMANT) ? "[DORMANT]" : "");
  768. if (!drv->if_disabled && !(ifi->ifi_flags & IFF_UP)) {
  769. namebuf[0] = '\0';
  770. if (if_indextoname(ifi->ifi_index, namebuf) &&
  771. linux_iface_up(drv->global->ioctl_sock, namebuf) > 0) {
  772. wpa_printf(MSG_DEBUG, "nl80211: Ignore interface down "
  773. "event since interface %s is up", namebuf);
  774. drv->ignore_if_down_event = 0;
  775. return;
  776. }
  777. wpa_printf(MSG_DEBUG, "nl80211: Interface down (%s/%s)",
  778. namebuf, ifname);
  779. if (os_strcmp(drv->first_bss->ifname, ifname) != 0) {
  780. wpa_printf(MSG_DEBUG,
  781. "nl80211: Not the main interface (%s) - do not indicate interface down",
  782. drv->first_bss->ifname);
  783. } else if (drv->ignore_if_down_event) {
  784. wpa_printf(MSG_DEBUG, "nl80211: Ignore interface down "
  785. "event generated by mode change");
  786. drv->ignore_if_down_event = 0;
  787. } else {
  788. drv->if_disabled = 1;
  789. wpa_supplicant_event(drv->ctx,
  790. EVENT_INTERFACE_DISABLED, NULL);
  791. /*
  792. * Try to get drv again, since it may be removed as
  793. * part of the EVENT_INTERFACE_DISABLED handling for
  794. * dynamic interfaces
  795. */
  796. drv = nl80211_find_drv(global, ifi->ifi_index,
  797. buf, len);
  798. if (!drv)
  799. return;
  800. }
  801. }
  802. if (drv->if_disabled && (ifi->ifi_flags & IFF_UP)) {
  803. if (if_indextoname(ifi->ifi_index, namebuf) &&
  804. linux_iface_up(drv->global->ioctl_sock, namebuf) == 0) {
  805. wpa_printf(MSG_DEBUG, "nl80211: Ignore interface up "
  806. "event since interface %s is down",
  807. namebuf);
  808. } else if (if_nametoindex(drv->first_bss->ifname) == 0) {
  809. wpa_printf(MSG_DEBUG, "nl80211: Ignore interface up "
  810. "event since interface %s does not exist",
  811. drv->first_bss->ifname);
  812. } else if (drv->if_removed) {
  813. wpa_printf(MSG_DEBUG, "nl80211: Ignore interface up "
  814. "event since interface %s is marked "
  815. "removed", drv->first_bss->ifname);
  816. } else {
  817. struct i802_bss *bss;
  818. u8 addr[ETH_ALEN];
  819. /* Re-read MAC address as it may have changed */
  820. bss = get_bss_ifindex(drv, ifi->ifi_index);
  821. if (bss &&
  822. linux_get_ifhwaddr(drv->global->ioctl_sock,
  823. bss->ifname, addr) < 0) {
  824. wpa_printf(MSG_DEBUG,
  825. "nl80211: %s: failed to re-read MAC address",
  826. bss->ifname);
  827. } else if (bss &&
  828. os_memcmp(addr, bss->addr, ETH_ALEN) != 0) {
  829. wpa_printf(MSG_DEBUG,
  830. "nl80211: Own MAC address on ifindex %d (%s) changed from "
  831. MACSTR " to " MACSTR,
  832. ifi->ifi_index, bss->ifname,
  833. MAC2STR(bss->addr),
  834. MAC2STR(addr));
  835. os_memcpy(bss->addr, addr, ETH_ALEN);
  836. }
  837. wpa_printf(MSG_DEBUG, "nl80211: Interface up");
  838. drv->if_disabled = 0;
  839. wpa_supplicant_event(drv->ctx, EVENT_INTERFACE_ENABLED,
  840. NULL);
  841. }
  842. }
  843. /*
  844. * Some drivers send the association event before the operup event--in
  845. * this case, lifting operstate in wpa_driver_nl80211_set_operstate()
  846. * fails. This will hit us when wpa_supplicant does not need to do
  847. * IEEE 802.1X authentication
  848. */
  849. if (drv->operstate == 1 &&
  850. (ifi->ifi_flags & (IFF_LOWER_UP | IFF_DORMANT)) == IFF_LOWER_UP &&
  851. !(ifi->ifi_flags & IFF_RUNNING)) {
  852. wpa_printf(MSG_DEBUG, "nl80211: Set IF_OPER_UP again based on ifi_flags and expected operstate");
  853. netlink_send_oper_ifla(drv->global->netlink, drv->ifindex,
  854. -1, IF_OPER_UP);
  855. }
  856. if (ifname[0])
  857. wpa_driver_nl80211_event_newlink(drv, ifname);
  858. if (ifi->ifi_family == AF_BRIDGE && brid) {
  859. struct i802_bss *bss;
  860. /* device has been added to bridge */
  861. if (!if_indextoname(brid, namebuf)) {
  862. wpa_printf(MSG_DEBUG,
  863. "nl80211: Could not find bridge ifname for ifindex %u",
  864. brid);
  865. return;
  866. }
  867. wpa_printf(MSG_DEBUG, "nl80211: Add ifindex %u for bridge %s",
  868. brid, namebuf);
  869. add_ifidx(drv, brid);
  870. for (bss = drv->first_bss; bss; bss = bss->next) {
  871. if (os_strcmp(ifname, bss->ifname) == 0) {
  872. os_strlcpy(bss->brname, namebuf, IFNAMSIZ);
  873. break;
  874. }
  875. }
  876. }
  877. }
  878. static void wpa_driver_nl80211_event_rtm_dellink(void *ctx,
  879. struct ifinfomsg *ifi,
  880. u8 *buf, size_t len)
  881. {
  882. struct nl80211_global *global = ctx;
  883. struct wpa_driver_nl80211_data *drv;
  884. int attrlen;
  885. struct rtattr *attr;
  886. u32 brid = 0;
  887. char ifname[IFNAMSIZ + 1];
  888. char extra[100], *pos, *end;
  889. drv = nl80211_find_drv(global, ifi->ifi_index, buf, len);
  890. if (!drv) {
  891. wpa_printf(MSG_DEBUG, "nl80211: Ignore RTM_DELLINK event for foreign ifindex %d",
  892. ifi->ifi_index);
  893. return;
  894. }
  895. extra[0] = '\0';
  896. pos = extra;
  897. end = pos + sizeof(extra);
  898. ifname[0] = '\0';
  899. attrlen = len;
  900. attr = (struct rtattr *) buf;
  901. while (RTA_OK(attr, attrlen)) {
  902. switch (attr->rta_type) {
  903. case IFLA_IFNAME:
  904. if (RTA_PAYLOAD(attr) >= IFNAMSIZ)
  905. break;
  906. os_memcpy(ifname, RTA_DATA(attr), RTA_PAYLOAD(attr));
  907. ifname[RTA_PAYLOAD(attr)] = '\0';
  908. break;
  909. case IFLA_MASTER:
  910. brid = nla_get_u32((struct nlattr *) attr);
  911. pos += os_snprintf(pos, end - pos, " master=%u", brid);
  912. break;
  913. case IFLA_OPERSTATE:
  914. pos += os_snprintf(pos, end - pos, " operstate=%u",
  915. nla_get_u32((struct nlattr *) attr));
  916. break;
  917. case IFLA_LINKMODE:
  918. pos += os_snprintf(pos, end - pos, " linkmode=%u",
  919. nla_get_u32((struct nlattr *) attr));
  920. break;
  921. }
  922. attr = RTA_NEXT(attr, attrlen);
  923. }
  924. extra[sizeof(extra) - 1] = '\0';
  925. wpa_printf(MSG_DEBUG, "RTM_DELLINK: ifi_index=%d ifname=%s%s ifi_family=%d ifi_flags=0x%x (%s%s%s%s)",
  926. ifi->ifi_index, ifname, extra, ifi->ifi_family,
  927. ifi->ifi_flags,
  928. (ifi->ifi_flags & IFF_UP) ? "[UP]" : "",
  929. (ifi->ifi_flags & IFF_RUNNING) ? "[RUNNING]" : "",
  930. (ifi->ifi_flags & IFF_LOWER_UP) ? "[LOWER_UP]" : "",
  931. (ifi->ifi_flags & IFF_DORMANT) ? "[DORMANT]" : "");
  932. if (ifname[0] && (ifi->ifi_family != AF_BRIDGE || !brid))
  933. wpa_driver_nl80211_event_dellink(drv, ifname);
  934. if (ifi->ifi_family == AF_BRIDGE && brid) {
  935. /* device has been removed from bridge */
  936. char namebuf[IFNAMSIZ];
  937. if (!if_indextoname(brid, namebuf)) {
  938. wpa_printf(MSG_DEBUG,
  939. "nl80211: Could not find bridge ifname for ifindex %u",
  940. brid);
  941. } else {
  942. wpa_printf(MSG_DEBUG,
  943. "nl80211: Remove ifindex %u for bridge %s",
  944. brid, namebuf);
  945. }
  946. del_ifidx(drv, brid);
  947. }
  948. }
  949. unsigned int nl80211_get_assoc_freq(struct wpa_driver_nl80211_data *drv)
  950. {
  951. struct nl_msg *msg;
  952. int ret;
  953. struct nl80211_bss_info_arg arg;
  954. msg = nl80211_drv_msg(drv, NLM_F_DUMP, NL80211_CMD_GET_SCAN);
  955. os_memset(&arg, 0, sizeof(arg));
  956. arg.drv = drv;
  957. ret = send_and_recv_msgs(drv, msg, bss_info_handler, &arg);
  958. if (ret == 0) {
  959. unsigned int freq = drv->nlmode == NL80211_IFTYPE_ADHOC ?
  960. arg.ibss_freq : arg.assoc_freq;
  961. wpa_printf(MSG_DEBUG, "nl80211: Operating frequency for the "
  962. "associated BSS from scan results: %u MHz", freq);
  963. if (freq)
  964. drv->assoc_freq = freq;
  965. return drv->assoc_freq;
  966. }
  967. wpa_printf(MSG_DEBUG, "nl80211: Scan result fetch failed: ret=%d "
  968. "(%s)", ret, strerror(-ret));
  969. return drv->assoc_freq;
  970. }
  971. static int get_link_signal(struct nl_msg *msg, void *arg)
  972. {
  973. struct nlattr *tb[NL80211_ATTR_MAX + 1];
  974. struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
  975. struct nlattr *sinfo[NL80211_STA_INFO_MAX + 1];
  976. static struct nla_policy policy[NL80211_STA_INFO_MAX + 1] = {
  977. [NL80211_STA_INFO_SIGNAL] = { .type = NLA_U8 },
  978. [NL80211_STA_INFO_SIGNAL_AVG] = { .type = NLA_U8 },
  979. };
  980. struct nlattr *rinfo[NL80211_RATE_INFO_MAX + 1];
  981. static struct nla_policy rate_policy[NL80211_RATE_INFO_MAX + 1] = {
  982. [NL80211_RATE_INFO_BITRATE] = { .type = NLA_U16 },
  983. [NL80211_RATE_INFO_MCS] = { .type = NLA_U8 },
  984. [NL80211_RATE_INFO_40_MHZ_WIDTH] = { .type = NLA_FLAG },
  985. [NL80211_RATE_INFO_SHORT_GI] = { .type = NLA_FLAG },
  986. };
  987. struct wpa_signal_info *sig_change = arg;
  988. nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
  989. genlmsg_attrlen(gnlh, 0), NULL);
  990. if (!tb[NL80211_ATTR_STA_INFO] ||
  991. nla_parse_nested(sinfo, NL80211_STA_INFO_MAX,
  992. tb[NL80211_ATTR_STA_INFO], policy))
  993. return NL_SKIP;
  994. if (!sinfo[NL80211_STA_INFO_SIGNAL])
  995. return NL_SKIP;
  996. sig_change->current_signal =
  997. (s8) nla_get_u8(sinfo[NL80211_STA_INFO_SIGNAL]);
  998. if (sinfo[NL80211_STA_INFO_SIGNAL_AVG])
  999. sig_change->avg_signal =
  1000. (s8) nla_get_u8(sinfo[NL80211_STA_INFO_SIGNAL_AVG]);
  1001. else
  1002. sig_change->avg_signal = 0;
  1003. if (sinfo[NL80211_STA_INFO_TX_BITRATE]) {
  1004. if (nla_parse_nested(rinfo, NL80211_RATE_INFO_MAX,
  1005. sinfo[NL80211_STA_INFO_TX_BITRATE],
  1006. rate_policy)) {
  1007. sig_change->current_txrate = 0;
  1008. } else {
  1009. if (rinfo[NL80211_RATE_INFO_BITRATE]) {
  1010. sig_change->current_txrate =
  1011. nla_get_u16(rinfo[
  1012. NL80211_RATE_INFO_BITRATE]) * 100;
  1013. }
  1014. }
  1015. }
  1016. return NL_SKIP;
  1017. }
  1018. int nl80211_get_link_signal(struct wpa_driver_nl80211_data *drv,
  1019. struct wpa_signal_info *sig)
  1020. {
  1021. struct nl_msg *msg;
  1022. sig->current_signal = -9999;
  1023. sig->current_txrate = 0;
  1024. if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_GET_STATION)) ||
  1025. nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, drv->bssid)) {
  1026. nlmsg_free(msg);
  1027. return -ENOBUFS;
  1028. }
  1029. return send_and_recv_msgs(drv, msg, get_link_signal, sig);
  1030. }
  1031. static int get_link_noise(struct nl_msg *msg, void *arg)
  1032. {
  1033. struct nlattr *tb[NL80211_ATTR_MAX + 1];
  1034. struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
  1035. struct nlattr *sinfo[NL80211_SURVEY_INFO_MAX + 1];
  1036. static struct nla_policy survey_policy[NL80211_SURVEY_INFO_MAX + 1] = {
  1037. [NL80211_SURVEY_INFO_FREQUENCY] = { .type = NLA_U32 },
  1038. [NL80211_SURVEY_INFO_NOISE] = { .type = NLA_U8 },
  1039. };
  1040. struct wpa_signal_info *sig_change = arg;
  1041. nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
  1042. genlmsg_attrlen(gnlh, 0), NULL);
  1043. if (!tb[NL80211_ATTR_SURVEY_INFO]) {
  1044. wpa_printf(MSG_DEBUG, "nl80211: survey data missing!");
  1045. return NL_SKIP;
  1046. }
  1047. if (nla_parse_nested(sinfo, NL80211_SURVEY_INFO_MAX,
  1048. tb[NL80211_ATTR_SURVEY_INFO],
  1049. survey_policy)) {
  1050. wpa_printf(MSG_DEBUG, "nl80211: failed to parse nested "
  1051. "attributes!");
  1052. return NL_SKIP;
  1053. }
  1054. if (!sinfo[NL80211_SURVEY_INFO_FREQUENCY])
  1055. return NL_SKIP;
  1056. if (nla_get_u32(sinfo[NL80211_SURVEY_INFO_FREQUENCY]) !=
  1057. sig_change->frequency)
  1058. return NL_SKIP;
  1059. if (!sinfo[NL80211_SURVEY_INFO_NOISE])
  1060. return NL_SKIP;
  1061. sig_change->current_noise =
  1062. (s8) nla_get_u8(sinfo[NL80211_SURVEY_INFO_NOISE]);
  1063. return NL_SKIP;
  1064. }
  1065. int nl80211_get_link_noise(struct wpa_driver_nl80211_data *drv,
  1066. struct wpa_signal_info *sig_change)
  1067. {
  1068. struct nl_msg *msg;
  1069. sig_change->current_noise = 9999;
  1070. sig_change->frequency = drv->assoc_freq;
  1071. msg = nl80211_drv_msg(drv, NLM_F_DUMP, NL80211_CMD_GET_SURVEY);
  1072. return send_and_recv_msgs(drv, msg, get_link_noise, sig_change);
  1073. }
  1074. static void wpa_driver_nl80211_event_receive(int sock, void *eloop_ctx,
  1075. void *handle)
  1076. {
  1077. struct nl_cb *cb = eloop_ctx;
  1078. int res;
  1079. wpa_printf(MSG_MSGDUMP, "nl80211: Event message available");
  1080. res = nl_recvmsgs(handle, cb);
  1081. if (res < 0) {
  1082. wpa_printf(MSG_INFO, "nl80211: %s->nl_recvmsgs failed: %d",
  1083. __func__, res);
  1084. }
  1085. }
  1086. /**
  1087. * wpa_driver_nl80211_set_country - ask nl80211 to set the regulatory domain
  1088. * @priv: driver_nl80211 private data
  1089. * @alpha2_arg: country to which to switch to
  1090. * Returns: 0 on success, -1 on failure
  1091. *
  1092. * This asks nl80211 to set the regulatory domain for given
  1093. * country ISO / IEC alpha2.
  1094. */
  1095. static int wpa_driver_nl80211_set_country(void *priv, const char *alpha2_arg)
  1096. {
  1097. struct i802_bss *bss = priv;
  1098. struct wpa_driver_nl80211_data *drv = bss->drv;
  1099. char alpha2[3];
  1100. struct nl_msg *msg;
  1101. msg = nlmsg_alloc();
  1102. if (!msg)
  1103. return -ENOMEM;
  1104. alpha2[0] = alpha2_arg[0];
  1105. alpha2[1] = alpha2_arg[1];
  1106. alpha2[2] = '\0';
  1107. if (!nl80211_cmd(drv, msg, 0, NL80211_CMD_REQ_SET_REG) ||
  1108. nla_put_string(msg, NL80211_ATTR_REG_ALPHA2, alpha2)) {
  1109. nlmsg_free(msg);
  1110. return -EINVAL;
  1111. }
  1112. if (send_and_recv_msgs(drv, msg, NULL, NULL))
  1113. return -EINVAL;
  1114. return 0;
  1115. }
  1116. static int nl80211_get_country(struct nl_msg *msg, void *arg)
  1117. {
  1118. char *alpha2 = arg;
  1119. struct nlattr *tb_msg[NL80211_ATTR_MAX + 1];
  1120. struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
  1121. nla_parse(tb_msg, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
  1122. genlmsg_attrlen(gnlh, 0), NULL);
  1123. if (!tb_msg[NL80211_ATTR_REG_ALPHA2]) {
  1124. wpa_printf(MSG_DEBUG, "nl80211: No country information available");
  1125. return NL_SKIP;
  1126. }
  1127. os_strlcpy(alpha2, nla_data(tb_msg[NL80211_ATTR_REG_ALPHA2]), 3);
  1128. return NL_SKIP;
  1129. }
  1130. static int wpa_driver_nl80211_get_country(void *priv, char *alpha2)
  1131. {
  1132. struct i802_bss *bss = priv;
  1133. struct wpa_driver_nl80211_data *drv = bss->drv;
  1134. struct nl_msg *msg;
  1135. int ret;
  1136. msg = nlmsg_alloc();
  1137. if (!msg)
  1138. return -ENOMEM;
  1139. nl80211_cmd(drv, msg, 0, NL80211_CMD_GET_REG);
  1140. alpha2[0] = '\0';
  1141. ret = send_and_recv_msgs(drv, msg, nl80211_get_country, alpha2);
  1142. if (!alpha2[0])
  1143. ret = -1;
  1144. return ret;
  1145. }
  1146. static int wpa_driver_nl80211_init_nl_global(struct nl80211_global *global)
  1147. {
  1148. int ret;
  1149. global->nl_cb = nl_cb_alloc(NL_CB_DEFAULT);
  1150. if (global->nl_cb == NULL) {
  1151. wpa_printf(MSG_ERROR, "nl80211: Failed to allocate netlink "
  1152. "callbacks");
  1153. return -1;
  1154. }
  1155. global->nl = nl_create_handle(global->nl_cb, "nl");
  1156. if (global->nl == NULL)
  1157. goto err;
  1158. global->nl80211_id = genl_ctrl_resolve(global->nl, "nl80211");
  1159. if (global->nl80211_id < 0) {
  1160. wpa_printf(MSG_ERROR, "nl80211: 'nl80211' generic netlink not "
  1161. "found");
  1162. goto err;
  1163. }
  1164. global->nl_event = nl_create_handle(global->nl_cb, "event");
  1165. if (global->nl_event == NULL)
  1166. goto err;
  1167. ret = nl_get_multicast_id(global, "nl80211", "scan");
  1168. if (ret >= 0)
  1169. ret = nl_socket_add_membership(global->nl_event, ret);
  1170. if (ret < 0) {
  1171. wpa_printf(MSG_ERROR, "nl80211: Could not add multicast "
  1172. "membership for scan events: %d (%s)",
  1173. ret, strerror(-ret));
  1174. goto err;
  1175. }
  1176. ret = nl_get_multicast_id(global, "nl80211", "mlme");
  1177. if (ret >= 0)
  1178. ret = nl_socket_add_membership(global->nl_event, ret);
  1179. if (ret < 0) {
  1180. wpa_printf(MSG_ERROR, "nl80211: Could not add multicast "
  1181. "membership for mlme events: %d (%s)",
  1182. ret, strerror(-ret));
  1183. goto err;
  1184. }
  1185. ret = nl_get_multicast_id(global, "nl80211", "regulatory");
  1186. if (ret >= 0)
  1187. ret = nl_socket_add_membership(global->nl_event, ret);
  1188. if (ret < 0) {
  1189. wpa_printf(MSG_DEBUG, "nl80211: Could not add multicast "
  1190. "membership for regulatory events: %d (%s)",
  1191. ret, strerror(-ret));
  1192. /* Continue without regulatory events */
  1193. }
  1194. ret = nl_get_multicast_id(global, "nl80211", "vendor");
  1195. if (ret >= 0)
  1196. ret = nl_socket_add_membership(global->nl_event, ret);
  1197. if (ret < 0) {
  1198. wpa_printf(MSG_DEBUG, "nl80211: Could not add multicast "
  1199. "membership for vendor events: %d (%s)",
  1200. ret, strerror(-ret));
  1201. /* Continue without vendor events */
  1202. }
  1203. nl_cb_set(global->nl_cb, NL_CB_SEQ_CHECK, NL_CB_CUSTOM,
  1204. no_seq_check, NULL);
  1205. nl_cb_set(global->nl_cb, NL_CB_VALID, NL_CB_CUSTOM,
  1206. process_global_event, global);
  1207. nl80211_register_eloop_read(&global->nl_event,
  1208. wpa_driver_nl80211_event_receive,
  1209. global->nl_cb);
  1210. return 0;
  1211. err:
  1212. nl_destroy_handles(&global->nl_event);
  1213. nl_destroy_handles(&global->nl);
  1214. nl_cb_put(global->nl_cb);
  1215. global->nl_cb = NULL;
  1216. return -1;
  1217. }
  1218. static void nl80211_check_global(struct nl80211_global *global)
  1219. {
  1220. struct nl_handle *handle;
  1221. const char *groups[] = { "scan", "mlme", "regulatory", "vendor", NULL };
  1222. int ret;
  1223. unsigned int i;
  1224. /*
  1225. * Try to re-add memberships to handle case of cfg80211 getting reloaded
  1226. * and all registration having been cleared.
  1227. */
  1228. handle = (void *) (((intptr_t) global->nl_event) ^
  1229. ELOOP_SOCKET_INVALID);
  1230. for (i = 0; groups[i]; i++) {
  1231. ret = nl_get_multicast_id(global, "nl80211", groups[i]);
  1232. if (ret >= 0)
  1233. ret = nl_socket_add_membership(handle, ret);
  1234. if (ret < 0) {
  1235. wpa_printf(MSG_INFO,
  1236. "nl80211: Could not re-add multicast membership for %s events: %d (%s)",
  1237. groups[i], ret, strerror(-ret));
  1238. }
  1239. }
  1240. }
  1241. static void wpa_driver_nl80211_rfkill_blocked(void *ctx)
  1242. {
  1243. wpa_printf(MSG_DEBUG, "nl80211: RFKILL blocked");
  1244. /*
  1245. * This may be for any interface; use ifdown event to disable
  1246. * interface.
  1247. */
  1248. }
  1249. static void wpa_driver_nl80211_rfkill_unblocked(void *ctx)
  1250. {
  1251. struct wpa_driver_nl80211_data *drv = ctx;
  1252. wpa_printf(MSG_DEBUG, "nl80211: RFKILL unblocked");
  1253. if (i802_set_iface_flags(drv->first_bss, 1)) {
  1254. wpa_printf(MSG_DEBUG, "nl80211: Could not set interface UP "
  1255. "after rfkill unblock");
  1256. return;
  1257. }
  1258. /* rtnetlink ifup handler will report interface as enabled */
  1259. }
  1260. static void wpa_driver_nl80211_handle_eapol_tx_status(int sock,
  1261. void *eloop_ctx,
  1262. void *handle)
  1263. {
  1264. struct wpa_driver_nl80211_data *drv = eloop_ctx;
  1265. u8 data[2048];
  1266. struct msghdr msg;
  1267. struct iovec entry;
  1268. u8 control[512];
  1269. struct cmsghdr *cmsg;
  1270. int res, found_ee = 0, found_wifi = 0, acked = 0;
  1271. union wpa_event_data event;
  1272. memset(&msg, 0, sizeof(msg));
  1273. msg.msg_iov = &entry;
  1274. msg.msg_iovlen = 1;
  1275. entry.iov_base = data;
  1276. entry.iov_len = sizeof(data);
  1277. msg.msg_control = &control;
  1278. msg.msg_controllen = sizeof(control);
  1279. res = recvmsg(sock, &msg, MSG_ERRQUEUE);
  1280. /* if error or not fitting 802.3 header, return */
  1281. if (res < 14)
  1282. return;
  1283. for (cmsg = CMSG_FIRSTHDR(&msg); cmsg; cmsg = CMSG_NXTHDR(&msg, cmsg))
  1284. {
  1285. if (cmsg->cmsg_level == SOL_SOCKET &&
  1286. cmsg->cmsg_type == SCM_WIFI_STATUS) {
  1287. int *ack;
  1288. found_wifi = 1;
  1289. ack = (void *)CMSG_DATA(cmsg);
  1290. acked = *ack;
  1291. }
  1292. if (cmsg->cmsg_level == SOL_PACKET &&
  1293. cmsg->cmsg_type == PACKET_TX_TIMESTAMP) {
  1294. struct sock_extended_err *err =
  1295. (struct sock_extended_err *)CMSG_DATA(cmsg);
  1296. if (err->ee_origin == SO_EE_ORIGIN_TXSTATUS)
  1297. found_ee = 1;
  1298. }
  1299. }
  1300. if (!found_ee || !found_wifi)
  1301. return;
  1302. memset(&event, 0, sizeof(event));
  1303. event.eapol_tx_status.dst = data;
  1304. event.eapol_tx_status.data = data + 14;
  1305. event.eapol_tx_status.data_len = res - 14;
  1306. event.eapol_tx_status.ack = acked;
  1307. wpa_supplicant_event(drv->ctx, EVENT_EAPOL_TX_STATUS, &event);
  1308. }
  1309. static int nl80211_init_bss(struct i802_bss *bss)
  1310. {
  1311. bss->nl_cb = nl_cb_alloc(NL_CB_DEFAULT);
  1312. if (!bss->nl_cb)
  1313. return -1;
  1314. nl_cb_set(bss->nl_cb, NL_CB_SEQ_CHECK, NL_CB_CUSTOM,
  1315. no_seq_check, NULL);
  1316. nl_cb_set(bss->nl_cb, NL_CB_VALID, NL_CB_CUSTOM,
  1317. process_bss_event, bss);
  1318. return 0;
  1319. }
  1320. static void nl80211_destroy_bss(struct i802_bss *bss)
  1321. {
  1322. nl_cb_put(bss->nl_cb);
  1323. bss->nl_cb = NULL;
  1324. }
  1325. static void * wpa_driver_nl80211_drv_init(void *ctx, const char *ifname,
  1326. void *global_priv, int hostapd,
  1327. const u8 *set_addr,
  1328. const char *driver_params)
  1329. {
  1330. struct wpa_driver_nl80211_data *drv;
  1331. struct rfkill_config *rcfg;
  1332. struct i802_bss *bss;
  1333. if (global_priv == NULL)
  1334. return NULL;
  1335. drv = os_zalloc(sizeof(*drv));
  1336. if (drv == NULL)
  1337. return NULL;
  1338. drv->global = global_priv;
  1339. drv->ctx = ctx;
  1340. drv->hostapd = !!hostapd;
  1341. drv->eapol_sock = -1;
  1342. /*
  1343. * There is no driver capability flag for this, so assume it is
  1344. * supported and disable this on first attempt to use if the driver
  1345. * rejects the command due to missing support.
  1346. */
  1347. drv->set_rekey_offload = 1;
  1348. drv->num_if_indices = sizeof(drv->default_if_indices) / sizeof(int);
  1349. drv->if_indices = drv->default_if_indices;
  1350. drv->first_bss = os_zalloc(sizeof(*drv->first_bss));
  1351. if (!drv->first_bss) {
  1352. os_free(drv);
  1353. return NULL;
  1354. }
  1355. bss = drv->first_bss;
  1356. bss->drv = drv;
  1357. bss->ctx = ctx;
  1358. os_strlcpy(bss->ifname, ifname, sizeof(bss->ifname));
  1359. drv->monitor_ifidx = -1;
  1360. drv->monitor_sock = -1;
  1361. drv->eapol_tx_sock = -1;
  1362. drv->ap_scan_as_station = NL80211_IFTYPE_UNSPECIFIED;
  1363. if (nl80211_init_bss(bss))
  1364. goto failed;
  1365. rcfg = os_zalloc(sizeof(*rcfg));
  1366. if (rcfg == NULL)
  1367. goto failed;
  1368. rcfg->ctx = drv;
  1369. os_strlcpy(rcfg->ifname, ifname, sizeof(rcfg->ifname));
  1370. rcfg->blocked_cb = wpa_driver_nl80211_rfkill_blocked;
  1371. rcfg->unblocked_cb = wpa_driver_nl80211_rfkill_unblocked;
  1372. drv->rfkill = rfkill_init(rcfg);
  1373. if (drv->rfkill == NULL) {
  1374. wpa_printf(MSG_DEBUG, "nl80211: RFKILL status not available");
  1375. os_free(rcfg);
  1376. }
  1377. if (linux_iface_up(drv->global->ioctl_sock, ifname) > 0)
  1378. drv->start_iface_up = 1;
  1379. if (wpa_driver_nl80211_finish_drv_init(drv, set_addr, 1, driver_params))
  1380. goto failed;
  1381. drv->eapol_tx_sock = socket(PF_PACKET, SOCK_DGRAM, 0);
  1382. if (drv->eapol_tx_sock < 0)
  1383. goto failed;
  1384. if (drv->data_tx_status) {
  1385. int enabled = 1;
  1386. if (setsockopt(drv->eapol_tx_sock, SOL_SOCKET, SO_WIFI_STATUS,
  1387. &enabled, sizeof(enabled)) < 0) {
  1388. wpa_printf(MSG_DEBUG,
  1389. "nl80211: wifi status sockopt failed\n");
  1390. drv->data_tx_status = 0;
  1391. if (!drv->use_monitor)
  1392. drv->capa.flags &=
  1393. ~WPA_DRIVER_FLAGS_EAPOL_TX_STATUS;
  1394. } else {
  1395. eloop_register_read_sock(drv->eapol_tx_sock,
  1396. wpa_driver_nl80211_handle_eapol_tx_status,
  1397. drv, NULL);
  1398. }
  1399. }
  1400. if (drv->global) {
  1401. nl80211_check_global(drv->global);
  1402. dl_list_add(&drv->global->interfaces, &drv->list);
  1403. drv->in_interface_list = 1;
  1404. }
  1405. return bss;
  1406. failed:
  1407. wpa_driver_nl80211_deinit(bss);
  1408. return NULL;
  1409. }
  1410. /**
  1411. * wpa_driver_nl80211_init - Initialize nl80211 driver interface
  1412. * @ctx: context to be used when calling wpa_supplicant functions,
  1413. * e.g., wpa_supplicant_event()
  1414. * @ifname: interface name, e.g., wlan0
  1415. * @global_priv: private driver global data from global_init()
  1416. * Returns: Pointer to private data, %NULL on failure
  1417. */
  1418. static void * wpa_driver_nl80211_init(void *ctx, const char *ifname,
  1419. void *global_priv)
  1420. {
  1421. return wpa_driver_nl80211_drv_init(ctx, ifname, global_priv, 0, NULL,
  1422. NULL);
  1423. }
  1424. static int nl80211_register_frame(struct i802_bss *bss,
  1425. struct nl_handle *nl_handle,
  1426. u16 type, const u8 *match, size_t match_len)
  1427. {
  1428. struct wpa_driver_nl80211_data *drv = bss->drv;
  1429. struct nl_msg *msg;
  1430. int ret;
  1431. char buf[30];
  1432. buf[0] = '\0';
  1433. wpa_snprintf_hex(buf, sizeof(buf), match, match_len);
  1434. wpa_printf(MSG_DEBUG, "nl80211: Register frame type=0x%x (%s) nl_handle=%p match=%s",
  1435. type, fc2str(type), nl_handle, buf);
  1436. if (!(msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_REGISTER_ACTION)) ||
  1437. nla_put_u16(msg, NL80211_ATTR_FRAME_TYPE, type) ||
  1438. nla_put(msg, NL80211_ATTR_FRAME_MATCH, match_len, match)) {
  1439. nlmsg_free(msg);
  1440. return -1;
  1441. }
  1442. ret = send_and_recv(drv->global, nl_handle, msg, NULL, NULL);
  1443. if (ret) {
  1444. wpa_printf(MSG_DEBUG, "nl80211: Register frame command "
  1445. "failed (type=%u): ret=%d (%s)",
  1446. type, ret, strerror(-ret));
  1447. wpa_hexdump(MSG_DEBUG, "nl80211: Register frame match",
  1448. match, match_len);
  1449. }
  1450. return ret;
  1451. }
  1452. static int nl80211_alloc_mgmt_handle(struct i802_bss *bss)
  1453. {
  1454. if (bss->nl_mgmt) {
  1455. wpa_printf(MSG_DEBUG, "nl80211: Mgmt reporting "
  1456. "already on! (nl_mgmt=%p)", bss->nl_mgmt);
  1457. return -1;
  1458. }
  1459. bss->nl_mgmt = nl_create_handle(bss->nl_cb, "mgmt");
  1460. if (bss->nl_mgmt == NULL)
  1461. return -1;
  1462. return 0;
  1463. }
  1464. static void nl80211_mgmt_handle_register_eloop(struct i802_bss *bss)
  1465. {
  1466. nl80211_register_eloop_read(&bss->nl_mgmt,
  1467. wpa_driver_nl80211_event_receive,
  1468. bss->nl_cb);
  1469. }
  1470. static int nl80211_register_action_frame(struct i802_bss *bss,
  1471. const u8 *match, size_t match_len)
  1472. {
  1473. u16 type = (WLAN_FC_TYPE_MGMT << 2) | (WLAN_FC_STYPE_ACTION << 4);
  1474. return nl80211_register_frame(bss, bss->nl_mgmt,
  1475. type, match, match_len);
  1476. }
  1477. static int nl80211_mgmt_subscribe_non_ap(struct i802_bss *bss)
  1478. {
  1479. struct wpa_driver_nl80211_data *drv = bss->drv;
  1480. int ret = 0;
  1481. if (nl80211_alloc_mgmt_handle(bss))
  1482. return -1;
  1483. wpa_printf(MSG_DEBUG, "nl80211: Subscribe to mgmt frames with non-AP "
  1484. "handle %p", bss->nl_mgmt);
  1485. if (drv->nlmode == NL80211_IFTYPE_ADHOC) {
  1486. u16 type = (WLAN_FC_TYPE_MGMT << 2) | (WLAN_FC_STYPE_AUTH << 4);
  1487. /* register for any AUTH message */
  1488. nl80211_register_frame(bss, bss->nl_mgmt, type, NULL, 0);
  1489. }
  1490. #ifdef CONFIG_INTERWORKING
  1491. /* QoS Map Configure */
  1492. if (nl80211_register_action_frame(bss, (u8 *) "\x01\x04", 2) < 0)
  1493. ret = -1;
  1494. #endif /* CONFIG_INTERWORKING */
  1495. #if defined(CONFIG_P2P) || defined(CONFIG_INTERWORKING)
  1496. /* GAS Initial Request */
  1497. if (nl80211_register_action_frame(bss, (u8 *) "\x04\x0a", 2) < 0)
  1498. ret = -1;
  1499. /* GAS Initial Response */
  1500. if (nl80211_register_action_frame(bss, (u8 *) "\x04\x0b", 2) < 0)
  1501. ret = -1;
  1502. /* GAS Comeback Request */
  1503. if (nl80211_register_action_frame(bss, (u8 *) "\x04\x0c", 2) < 0)
  1504. ret = -1;
  1505. /* GAS Comeback Response */
  1506. if (nl80211_register_action_frame(bss, (u8 *) "\x04\x0d", 2) < 0)
  1507. ret = -1;
  1508. /* Protected GAS Initial Request */
  1509. if (nl80211_register_action_frame(bss, (u8 *) "\x09\x0a", 2) < 0)
  1510. ret = -1;
  1511. /* Protected GAS Initial Response */
  1512. if (nl80211_register_action_frame(bss, (u8 *) "\x09\x0b", 2) < 0)
  1513. ret = -1;
  1514. /* Protected GAS Comeback Request */
  1515. if (nl80211_register_action_frame(bss, (u8 *) "\x09\x0c", 2) < 0)
  1516. ret = -1;
  1517. /* Protected GAS Comeback Response */
  1518. if (nl80211_register_action_frame(bss, (u8 *) "\x09\x0d", 2) < 0)
  1519. ret = -1;
  1520. #endif /* CONFIG_P2P || CONFIG_INTERWORKING */
  1521. #ifdef CONFIG_P2P
  1522. /* P2P Public Action */
  1523. if (nl80211_register_action_frame(bss,
  1524. (u8 *) "\x04\x09\x50\x6f\x9a\x09",
  1525. 6) < 0)
  1526. ret = -1;
  1527. /* P2P Action */
  1528. if (nl80211_register_action_frame(bss,
  1529. (u8 *) "\x7f\x50\x6f\x9a\x09",
  1530. 5) < 0)
  1531. ret = -1;
  1532. #endif /* CONFIG_P2P */
  1533. #ifdef CONFIG_IEEE80211W
  1534. /* SA Query Response */
  1535. if (nl80211_register_action_frame(bss, (u8 *) "\x08\x01", 2) < 0)
  1536. ret = -1;
  1537. #endif /* CONFIG_IEEE80211W */
  1538. #ifdef CONFIG_TDLS
  1539. if ((drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT)) {
  1540. /* TDLS Discovery Response */
  1541. if (nl80211_register_action_frame(bss, (u8 *) "\x04\x0e", 2) <
  1542. 0)
  1543. ret = -1;
  1544. }
  1545. #endif /* CONFIG_TDLS */
  1546. /* FT Action frames */
  1547. if (nl80211_register_action_frame(bss, (u8 *) "\x06", 1) < 0)
  1548. ret = -1;
  1549. else
  1550. drv->capa.key_mgmt |= WPA_DRIVER_CAPA_KEY_MGMT_FT |
  1551. WPA_DRIVER_CAPA_KEY_MGMT_FT_PSK;
  1552. /* WNM - BSS Transition Management Request */
  1553. if (nl80211_register_action_frame(bss, (u8 *) "\x0a\x07", 2) < 0)
  1554. ret = -1;
  1555. /* WNM-Sleep Mode Response */
  1556. if (nl80211_register_action_frame(bss, (u8 *) "\x0a\x11", 2) < 0)
  1557. ret = -1;
  1558. #ifdef CONFIG_HS20
  1559. /* WNM-Notification */
  1560. if (nl80211_register_action_frame(bss, (u8 *) "\x0a\x1a", 2) < 0)
  1561. ret = -1;
  1562. #endif /* CONFIG_HS20 */
  1563. /* WMM-AC ADDTS Response */
  1564. if (nl80211_register_action_frame(bss, (u8 *) "\x11\x01", 2) < 0)
  1565. ret = -1;
  1566. /* WMM-AC DELTS */
  1567. if (nl80211_register_action_frame(bss, (u8 *) "\x11\x02", 2) < 0)
  1568. ret = -1;
  1569. /* Radio Measurement - Neighbor Report Response */
  1570. if (nl80211_register_action_frame(bss, (u8 *) "\x05\x05", 2) < 0)
  1571. ret = -1;
  1572. /* Radio Measurement - Link Measurement Request */
  1573. if ((drv->capa.rrm_flags & WPA_DRIVER_FLAGS_TX_POWER_INSERTION) &&
  1574. (nl80211_register_action_frame(bss, (u8 *) "\x05\x02", 2) < 0))
  1575. ret = -1;
  1576. nl80211_mgmt_handle_register_eloop(bss);
  1577. return ret;
  1578. }
  1579. static int nl80211_mgmt_subscribe_mesh(struct i802_bss *bss)
  1580. {
  1581. int ret = 0;
  1582. if (nl80211_alloc_mgmt_handle(bss))
  1583. return -1;
  1584. wpa_printf(MSG_DEBUG,
  1585. "nl80211: Subscribe to mgmt frames with mesh handle %p",
  1586. bss->nl_mgmt);
  1587. /* Auth frames for mesh SAE */
  1588. if (nl80211_register_frame(bss, bss->nl_mgmt,
  1589. (WLAN_FC_TYPE_MGMT << 2) |
  1590. (WLAN_FC_STYPE_AUTH << 4),
  1591. NULL, 0) < 0)
  1592. ret = -1;
  1593. /* Mesh peering open */
  1594. if (nl80211_register_action_frame(bss, (u8 *) "\x0f\x01", 2) < 0)
  1595. ret = -1;
  1596. /* Mesh peering confirm */
  1597. if (nl80211_register_action_frame(bss, (u8 *) "\x0f\x02", 2) < 0)
  1598. ret = -1;
  1599. /* Mesh peering close */
  1600. if (nl80211_register_action_frame(bss, (u8 *) "\x0f\x03", 2) < 0)
  1601. ret = -1;
  1602. nl80211_mgmt_handle_register_eloop(bss);
  1603. return ret;
  1604. }
  1605. static int nl80211_register_spurious_class3(struct i802_bss *bss)
  1606. {
  1607. struct nl_msg *msg;
  1608. int ret;
  1609. msg = nl80211_bss_msg(bss, 0, NL80211_CMD_UNEXPECTED_FRAME);
  1610. ret = send_and_recv(bss->drv->global, bss->nl_mgmt, msg, NULL, NULL);
  1611. if (ret) {
  1612. wpa_printf(MSG_DEBUG, "nl80211: Register spurious class3 "
  1613. "failed: ret=%d (%s)",
  1614. ret, strerror(-ret));
  1615. }
  1616. return ret;
  1617. }
  1618. static int nl80211_mgmt_subscribe_ap(struct i802_bss *bss)
  1619. {
  1620. static const int stypes[] = {
  1621. WLAN_FC_STYPE_AUTH,
  1622. WLAN_FC_STYPE_ASSOC_REQ,
  1623. WLAN_FC_STYPE_REASSOC_REQ,
  1624. WLAN_FC_STYPE_DISASSOC,
  1625. WLAN_FC_STYPE_DEAUTH,
  1626. WLAN_FC_STYPE_ACTION,
  1627. WLAN_FC_STYPE_PROBE_REQ,
  1628. /* Beacon doesn't work as mac80211 doesn't currently allow
  1629. * it, but it wouldn't really be the right thing anyway as
  1630. * it isn't per interface ... maybe just dump the scan
  1631. * results periodically for OLBC?
  1632. */
  1633. /* WLAN_FC_STYPE_BEACON, */
  1634. };
  1635. unsigned int i;
  1636. if (nl80211_alloc_mgmt_handle(bss))
  1637. return -1;
  1638. wpa_printf(MSG_DEBUG, "nl80211: Subscribe to mgmt frames with AP "
  1639. "handle %p", bss->nl_mgmt);
  1640. for (i = 0; i < ARRAY_SIZE(stypes); i++) {
  1641. if (nl80211_register_frame(bss, bss->nl_mgmt,
  1642. (WLAN_FC_TYPE_MGMT << 2) |
  1643. (stypes[i] << 4),
  1644. NULL, 0) < 0) {
  1645. goto out_err;
  1646. }
  1647. }
  1648. if (nl80211_register_spurious_class3(bss))
  1649. goto out_err;
  1650. if (nl80211_get_wiphy_data_ap(bss) == NULL)
  1651. goto out_err;
  1652. nl80211_mgmt_handle_register_eloop(bss);
  1653. return 0;
  1654. out_err:
  1655. nl_destroy_handles(&bss->nl_mgmt);
  1656. return -1;
  1657. }
  1658. static int nl80211_mgmt_subscribe_ap_dev_sme(struct i802_bss *bss)
  1659. {
  1660. if (nl80211_alloc_mgmt_handle(bss))
  1661. return -1;
  1662. wpa_printf(MSG_DEBUG, "nl80211: Subscribe to mgmt frames with AP "
  1663. "handle %p (device SME)", bss->nl_mgmt);
  1664. if (nl80211_register_frame(bss, bss->nl_mgmt,
  1665. (WLAN_FC_TYPE_MGMT << 2) |
  1666. (WLAN_FC_STYPE_ACTION << 4),
  1667. NULL, 0) < 0)
  1668. goto out_err;
  1669. nl80211_mgmt_handle_register_eloop(bss);
  1670. return 0;
  1671. out_err:
  1672. nl_destroy_handles(&bss->nl_mgmt);
  1673. return -1;
  1674. }
  1675. static void nl80211_mgmt_unsubscribe(struct i802_bss *bss, const char *reason)
  1676. {
  1677. if (bss->nl_mgmt == NULL)
  1678. return;
  1679. wpa_printf(MSG_DEBUG, "nl80211: Unsubscribe mgmt frames handle %p "
  1680. "(%s)", bss->nl_mgmt, reason);
  1681. nl80211_destroy_eloop_handle(&bss->nl_mgmt);
  1682. nl80211_put_wiphy_data_ap(bss);
  1683. }
  1684. static void wpa_driver_nl80211_send_rfkill(void *eloop_ctx, void *timeout_ctx)
  1685. {
  1686. wpa_supplicant_event(timeout_ctx, EVENT_INTERFACE_DISABLED, NULL);
  1687. }
  1688. static void nl80211_del_p2pdev(struct i802_bss *bss)
  1689. {
  1690. struct nl_msg *msg;
  1691. int ret;
  1692. msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_DEL_INTERFACE);
  1693. ret = send_and_recv_msgs(bss->drv, msg, NULL, NULL);
  1694. wpa_printf(MSG_DEBUG, "nl80211: Delete P2P Device %s (0x%llx): %s",
  1695. bss->ifname, (long long unsigned int) bss->wdev_id,
  1696. strerror(-ret));
  1697. }
  1698. static int nl80211_set_p2pdev(struct i802_bss *bss, int start)
  1699. {
  1700. struct nl_msg *msg;
  1701. int ret;
  1702. msg = nl80211_cmd_msg(bss, 0, start ? NL80211_CMD_START_P2P_DEVICE :
  1703. NL80211_CMD_STOP_P2P_DEVICE);
  1704. ret = send_and_recv_msgs(bss->drv, msg, NULL, NULL);
  1705. wpa_printf(MSG_DEBUG, "nl80211: %s P2P Device %s (0x%llx): %s",
  1706. start ? "Start" : "Stop",
  1707. bss->ifname, (long long unsigned int) bss->wdev_id,
  1708. strerror(-ret));
  1709. return ret;
  1710. }
  1711. static int i802_set_iface_flags(struct i802_bss *bss, int up)
  1712. {
  1713. enum nl80211_iftype nlmode;
  1714. nlmode = nl80211_get_ifmode(bss);
  1715. if (nlmode != NL80211_IFTYPE_P2P_DEVICE) {
  1716. return linux_set_iface_flags(bss->drv->global->ioctl_sock,
  1717. bss->ifname, up);
  1718. }
  1719. /* P2P Device has start/stop which is equivalent */
  1720. return nl80211_set_p2pdev(bss, up);
  1721. }
  1722. #ifdef CONFIG_TESTING_OPTIONS
  1723. static int qca_vendor_test_cmd_handler(struct nl_msg *msg, void *arg)
  1724. {
  1725. /* struct wpa_driver_nl80211_data *drv = arg; */
  1726. struct nlattr *tb[NL80211_ATTR_MAX + 1];
  1727. struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
  1728. wpa_printf(MSG_DEBUG,
  1729. "nl80211: QCA vendor test command response received");
  1730. nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
  1731. genlmsg_attrlen(gnlh, 0), NULL);
  1732. if (!tb[NL80211_ATTR_VENDOR_DATA]) {
  1733. wpa_printf(MSG_DEBUG, "nl80211: No vendor data attribute");
  1734. return NL_SKIP;
  1735. }
  1736. wpa_hexdump(MSG_DEBUG,
  1737. "nl80211: Received QCA vendor test command response",
  1738. nla_data(tb[NL80211_ATTR_VENDOR_DATA]),
  1739. nla_len(tb[NL80211_ATTR_VENDOR_DATA]));
  1740. return NL_SKIP;
  1741. }
  1742. #endif /* CONFIG_TESTING_OPTIONS */
  1743. static void qca_vendor_test(struct wpa_driver_nl80211_data *drv)
  1744. {
  1745. #ifdef CONFIG_TESTING_OPTIONS
  1746. struct nl_msg *msg;
  1747. struct nlattr *params;
  1748. int ret;
  1749. if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_VENDOR)) ||
  1750. nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, OUI_QCA) ||
  1751. nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD,
  1752. QCA_NL80211_VENDOR_SUBCMD_TEST) ||
  1753. !(params = nla_nest_start(msg, NL80211_ATTR_VENDOR_DATA)) ||
  1754. nla_put_u32(msg, QCA_WLAN_VENDOR_ATTR_TEST, 123)) {
  1755. nlmsg_free(msg);
  1756. return;
  1757. }
  1758. nla_nest_end(msg, params);
  1759. ret = send_and_recv_msgs(drv, msg, qca_vendor_test_cmd_handler, drv);
  1760. wpa_printf(MSG_DEBUG,
  1761. "nl80211: QCA vendor test command returned %d (%s)",
  1762. ret, strerror(-ret));
  1763. #endif /* CONFIG_TESTING_OPTIONS */
  1764. }
  1765. static int
  1766. wpa_driver_nl80211_finish_drv_init(struct wpa_driver_nl80211_data *drv,
  1767. const u8 *set_addr, int first,
  1768. const char *driver_params)
  1769. {
  1770. struct i802_bss *bss = drv->first_bss;
  1771. int send_rfkill_event = 0;
  1772. enum nl80211_iftype nlmode;
  1773. drv->ifindex = if_nametoindex(bss->ifname);
  1774. bss->ifindex = drv->ifindex;
  1775. bss->wdev_id = drv->global->if_add_wdevid;
  1776. bss->wdev_id_set = drv->global->if_add_wdevid_set;
  1777. bss->if_dynamic = drv->ifindex == drv->global->if_add_ifindex;
  1778. bss->if_dynamic = bss->if_dynamic || drv->global->if_add_wdevid_set;
  1779. drv->global->if_add_wdevid_set = 0;
  1780. if (!bss->if_dynamic && nl80211_get_ifmode(bss) == NL80211_IFTYPE_AP)
  1781. bss->static_ap = 1;
  1782. if (wpa_driver_nl80211_capa(drv))
  1783. return -1;
  1784. if (driver_params && nl80211_set_param(bss, driver_params) < 0)
  1785. return -1;
  1786. wpa_printf(MSG_DEBUG, "nl80211: interface %s in phy %s",
  1787. bss->ifname, drv->phyname);
  1788. if (set_addr &&
  1789. (linux_set_iface_flags(drv->global->ioctl_sock, bss->ifname, 0) ||
  1790. linux_set_ifhwaddr(drv->global->ioctl_sock, bss->ifname,
  1791. set_addr)))
  1792. return -1;
  1793. if (first && nl80211_get_ifmode(bss) == NL80211_IFTYPE_AP)
  1794. drv->start_mode_ap = 1;
  1795. if (drv->hostapd || bss->static_ap)
  1796. nlmode = NL80211_IFTYPE_AP;
  1797. else if (bss->if_dynamic)
  1798. nlmode = nl80211_get_ifmode(bss);
  1799. else
  1800. nlmode = NL80211_IFTYPE_STATION;
  1801. if (wpa_driver_nl80211_set_mode(bss, nlmode) < 0) {
  1802. wpa_printf(MSG_ERROR, "nl80211: Could not configure driver mode");
  1803. return -1;
  1804. }
  1805. if (nlmode == NL80211_IFTYPE_P2P_DEVICE)
  1806. nl80211_get_macaddr(bss);
  1807. if (!rfkill_is_blocked(drv->rfkill)) {
  1808. int ret = i802_set_iface_flags(bss, 1);
  1809. if (ret) {
  1810. wpa_printf(MSG_ERROR, "nl80211: Could not set "
  1811. "interface '%s' UP", bss->ifname);
  1812. return ret;
  1813. }
  1814. if (nlmode == NL80211_IFTYPE_P2P_DEVICE)
  1815. return ret;
  1816. } else {
  1817. wpa_printf(MSG_DEBUG, "nl80211: Could not yet enable "
  1818. "interface '%s' due to rfkill", bss->ifname);
  1819. if (nlmode == NL80211_IFTYPE_P2P_DEVICE)
  1820. return 0;
  1821. drv->if_disabled = 1;
  1822. send_rfkill_event = 1;
  1823. }
  1824. if (!drv->hostapd)
  1825. netlink_send_oper_ifla(drv->global->netlink, drv->ifindex,
  1826. 1, IF_OPER_DORMANT);
  1827. if (linux_get_ifhwaddr(drv->global->ioctl_sock, bss->ifname,
  1828. bss->addr))
  1829. return -1;
  1830. os_memcpy(drv->perm_addr, bss->addr, ETH_ALEN);
  1831. if (send_rfkill_event) {
  1832. eloop_register_timeout(0, 0, wpa_driver_nl80211_send_rfkill,
  1833. drv, drv->ctx);
  1834. }
  1835. if (drv->vendor_cmd_test_avail)
  1836. qca_vendor_test(drv);
  1837. return 0;
  1838. }
  1839. static int wpa_driver_nl80211_del_beacon(struct wpa_driver_nl80211_data *drv)
  1840. {
  1841. struct nl_msg *msg;
  1842. wpa_printf(MSG_DEBUG, "nl80211: Remove beacon (ifindex=%d)",
  1843. drv->ifindex);
  1844. msg = nl80211_drv_msg(drv, 0, NL80211_CMD_DEL_BEACON);
  1845. return send_and_recv_msgs(drv, msg, NULL, NULL);
  1846. }
  1847. /**
  1848. * wpa_driver_nl80211_deinit - Deinitialize nl80211 driver interface
  1849. * @bss: Pointer to private nl80211 data from wpa_driver_nl80211_init()
  1850. *
  1851. * Shut down driver interface and processing of driver events. Free
  1852. * private data buffer if one was allocated in wpa_driver_nl80211_init().
  1853. */
  1854. static void wpa_driver_nl80211_deinit(struct i802_bss *bss)
  1855. {
  1856. struct wpa_driver_nl80211_data *drv = bss->drv;
  1857. wpa_printf(MSG_INFO, "nl80211: deinit ifname=%s disabled_11b_rates=%d",
  1858. bss->ifname, drv->disabled_11b_rates);
  1859. bss->in_deinit = 1;
  1860. if (drv->data_tx_status)
  1861. eloop_unregister_read_sock(drv->eapol_tx_sock);
  1862. if (drv->eapol_tx_sock >= 0)
  1863. close(drv->eapol_tx_sock);
  1864. if (bss->nl_preq)
  1865. wpa_driver_nl80211_probe_req_report(bss, 0);
  1866. if (bss->added_if_into_bridge) {
  1867. if (linux_br_del_if(drv->global->ioctl_sock, bss->brname,
  1868. bss->ifname) < 0)
  1869. wpa_printf(MSG_INFO, "nl80211: Failed to remove "
  1870. "interface %s from bridge %s: %s",
  1871. bss->ifname, bss->brname, strerror(errno));
  1872. if (drv->rtnl_sk)
  1873. nl80211_handle_destroy(drv->rtnl_sk);
  1874. }
  1875. if (bss->added_bridge) {
  1876. if (linux_set_iface_flags(drv->global->ioctl_sock, bss->brname,
  1877. 0) < 0)
  1878. wpa_printf(MSG_INFO,
  1879. "nl80211: Could not set bridge %s down",
  1880. bss->brname);
  1881. if (linux_br_del(drv->global->ioctl_sock, bss->brname) < 0)
  1882. wpa_printf(MSG_INFO, "nl80211: Failed to remove "
  1883. "bridge %s: %s",
  1884. bss->brname, strerror(errno));
  1885. }
  1886. nl80211_remove_monitor_interface(drv);
  1887. if (is_ap_interface(drv->nlmode))
  1888. wpa_driver_nl80211_del_beacon(drv);
  1889. if (drv->eapol_sock >= 0) {
  1890. eloop_unregister_read_sock(drv->eapol_sock);
  1891. close(drv->eapol_sock);
  1892. }
  1893. if (drv->if_indices != drv->default_if_indices)
  1894. os_free(drv->if_indices);
  1895. if (drv->disabled_11b_rates)
  1896. nl80211_disable_11b_rates(drv, drv->ifindex, 0);
  1897. netlink_send_oper_ifla(drv->global->netlink, drv->ifindex, 0,
  1898. IF_OPER_UP);
  1899. eloop_cancel_timeout(wpa_driver_nl80211_send_rfkill, drv, drv->ctx);
  1900. rfkill_deinit(drv->rfkill);
  1901. eloop_cancel_timeout(wpa_driver_nl80211_scan_timeout, drv, drv->ctx);
  1902. if (!drv->start_iface_up)
  1903. (void) i802_set_iface_flags(bss, 0);
  1904. if (drv->addr_changed) {
  1905. if (linux_set_iface_flags(drv->global->ioctl_sock, bss->ifname,
  1906. 0) < 0) {
  1907. wpa_printf(MSG_DEBUG,
  1908. "nl80211: Could not set interface down to restore permanent MAC address");
  1909. }
  1910. if (linux_set_ifhwaddr(drv->global->ioctl_sock, bss->ifname,
  1911. drv->perm_addr) < 0) {
  1912. wpa_printf(MSG_DEBUG,
  1913. "nl80211: Could not restore permanent MAC address");
  1914. }
  1915. }
  1916. if (drv->nlmode != NL80211_IFTYPE_P2P_DEVICE) {
  1917. if (!drv->hostapd || !drv->start_mode_ap)
  1918. wpa_driver_nl80211_set_mode(bss,
  1919. NL80211_IFTYPE_STATION);
  1920. nl80211_mgmt_unsubscribe(bss, "deinit");
  1921. } else {
  1922. nl80211_mgmt_unsubscribe(bss, "deinit");
  1923. nl80211_del_p2pdev(bss);
  1924. }
  1925. nl80211_destroy_bss(drv->first_bss);
  1926. os_free(drv->filter_ssids);
  1927. os_free(drv->auth_ie);
  1928. if (drv->in_interface_list)
  1929. dl_list_del(&drv->list);
  1930. os_free(drv->extended_capa);
  1931. os_free(drv->extended_capa_mask);
  1932. os_free(drv->first_bss);
  1933. os_free(drv);
  1934. }
  1935. static u32 wpa_alg_to_cipher_suite(enum wpa_alg alg, size_t key_len)
  1936. {
  1937. switch (alg) {
  1938. case WPA_ALG_WEP:
  1939. if (key_len == 5)
  1940. return WLAN_CIPHER_SUITE_WEP40;
  1941. return WLAN_CIPHER_SUITE_WEP104;
  1942. case WPA_ALG_TKIP:
  1943. return WLAN_CIPHER_SUITE_TKIP;
  1944. case WPA_ALG_CCMP:
  1945. return WLAN_CIPHER_SUITE_CCMP;
  1946. case WPA_ALG_GCMP:
  1947. return WLAN_CIPHER_SUITE_GCMP;
  1948. case WPA_ALG_CCMP_256:
  1949. return WLAN_CIPHER_SUITE_CCMP_256;
  1950. case WPA_ALG_GCMP_256:
  1951. return WLAN_CIPHER_SUITE_GCMP_256;
  1952. case WPA_ALG_IGTK:
  1953. return WLAN_CIPHER_SUITE_AES_CMAC;
  1954. case WPA_ALG_BIP_GMAC_128:
  1955. return WLAN_CIPHER_SUITE_BIP_GMAC_128;
  1956. case WPA_ALG_BIP_GMAC_256:
  1957. return WLAN_CIPHER_SUITE_BIP_GMAC_256;
  1958. case WPA_ALG_BIP_CMAC_256:
  1959. return WLAN_CIPHER_SUITE_BIP_CMAC_256;
  1960. case WPA_ALG_SMS4:
  1961. return WLAN_CIPHER_SUITE_SMS4;
  1962. case WPA_ALG_KRK:
  1963. return WLAN_CIPHER_SUITE_KRK;
  1964. case WPA_ALG_NONE:
  1965. case WPA_ALG_PMK:
  1966. wpa_printf(MSG_ERROR, "nl80211: Unexpected encryption algorithm %d",
  1967. alg);
  1968. return 0;
  1969. }
  1970. wpa_printf(MSG_ERROR, "nl80211: Unsupported encryption algorithm %d",
  1971. alg);
  1972. return 0;
  1973. }
  1974. static u32 wpa_cipher_to_cipher_suite(unsigned int cipher)
  1975. {
  1976. switch (cipher) {
  1977. case WPA_CIPHER_CCMP_256:
  1978. return WLAN_CIPHER_SUITE_CCMP_256;
  1979. case WPA_CIPHER_GCMP_256:
  1980. return WLAN_CIPHER_SUITE_GCMP_256;
  1981. case WPA_CIPHER_CCMP:
  1982. return WLAN_CIPHER_SUITE_CCMP;
  1983. case WPA_CIPHER_GCMP:
  1984. return WLAN_CIPHER_SUITE_GCMP;
  1985. case WPA_CIPHER_TKIP:
  1986. return WLAN_CIPHER_SUITE_TKIP;
  1987. case WPA_CIPHER_WEP104:
  1988. return WLAN_CIPHER_SUITE_WEP104;
  1989. case WPA_CIPHER_WEP40:
  1990. return WLAN_CIPHER_SUITE_WEP40;
  1991. case WPA_CIPHER_GTK_NOT_USED:
  1992. return WLAN_CIPHER_SUITE_NO_GROUP_ADDR;
  1993. }
  1994. return 0;
  1995. }
  1996. static int wpa_cipher_to_cipher_suites(unsigned int ciphers, u32 suites[],
  1997. int max_suites)
  1998. {
  1999. int num_suites = 0;
  2000. if (num_suites < max_suites && ciphers & WPA_CIPHER_CCMP_256)
  2001. suites[num_suites++] = WLAN_CIPHER_SUITE_CCMP_256;
  2002. if (num_suites < max_suites && ciphers & WPA_CIPHER_GCMP_256)
  2003. suites[num_suites++] = WLAN_CIPHER_SUITE_GCMP_256;
  2004. if (num_suites < max_suites && ciphers & WPA_CIPHER_CCMP)
  2005. suites[num_suites++] = WLAN_CIPHER_SUITE_CCMP;
  2006. if (num_suites < max_suites && ciphers & WPA_CIPHER_GCMP)
  2007. suites[num_suites++] = WLAN_CIPHER_SUITE_GCMP;
  2008. if (num_suites < max_suites && ciphers & WPA_CIPHER_TKIP)
  2009. suites[num_suites++] = WLAN_CIPHER_SUITE_TKIP;
  2010. if (num_suites < max_suites && ciphers & WPA_CIPHER_WEP104)
  2011. suites[num_suites++] = WLAN_CIPHER_SUITE_WEP104;
  2012. if (num_suites < max_suites && ciphers & WPA_CIPHER_WEP40)
  2013. suites[num_suites++] = WLAN_CIPHER_SUITE_WEP40;
  2014. return num_suites;
  2015. }
  2016. static int issue_key_mgmt_set_key(struct wpa_driver_nl80211_data *drv,
  2017. const u8 *key, size_t key_len)
  2018. {
  2019. struct nl_msg *msg;
  2020. int ret;
  2021. if (!(drv->capa.flags & WPA_DRIVER_FLAGS_KEY_MGMT_OFFLOAD))
  2022. return 0;
  2023. if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_VENDOR)) ||
  2024. nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, OUI_QCA) ||
  2025. nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD,
  2026. QCA_NL80211_VENDOR_SUBCMD_KEY_MGMT_SET_KEY) ||
  2027. nla_put(msg, NL80211_ATTR_VENDOR_DATA, key_len, key)) {
  2028. nl80211_nlmsg_clear(msg);
  2029. nlmsg_free(msg);
  2030. return -1;
  2031. }
  2032. ret = send_and_recv_msgs(drv, msg, NULL, (void *) -1);
  2033. if (ret) {
  2034. wpa_printf(MSG_DEBUG,
  2035. "nl80211: Key management set key failed: ret=%d (%s)",
  2036. ret, strerror(-ret));
  2037. }
  2038. return ret;
  2039. }
  2040. static int wpa_driver_nl80211_set_key(const char *ifname, struct i802_bss *bss,
  2041. enum wpa_alg alg, const u8 *addr,
  2042. int key_idx, int set_tx,
  2043. const u8 *seq, size_t seq_len,
  2044. const u8 *key, size_t key_len)
  2045. {
  2046. struct wpa_driver_nl80211_data *drv = bss->drv;
  2047. int ifindex;
  2048. struct nl_msg *msg;
  2049. int ret;
  2050. int tdls = 0;
  2051. /* Ignore for P2P Device */
  2052. if (drv->nlmode == NL80211_IFTYPE_P2P_DEVICE)
  2053. return 0;
  2054. ifindex = if_nametoindex(ifname);
  2055. wpa_printf(MSG_DEBUG, "%s: ifindex=%d (%s) alg=%d addr=%p key_idx=%d "
  2056. "set_tx=%d seq_len=%lu key_len=%lu",
  2057. __func__, ifindex, ifname, alg, addr, key_idx, set_tx,
  2058. (unsigned long) seq_len, (unsigned long) key_len);
  2059. #ifdef CONFIG_TDLS
  2060. if (key_idx == -1) {
  2061. key_idx = 0;
  2062. tdls = 1;
  2063. }
  2064. #endif /* CONFIG_TDLS */
  2065. if (alg == WPA_ALG_PMK &&
  2066. (drv->capa.flags & WPA_DRIVER_FLAGS_KEY_MGMT_OFFLOAD)) {
  2067. wpa_printf(MSG_DEBUG, "%s: calling issue_key_mgmt_set_key",
  2068. __func__);
  2069. ret = issue_key_mgmt_set_key(drv, key, key_len);
  2070. return ret;
  2071. }
  2072. if (alg == WPA_ALG_NONE) {
  2073. msg = nl80211_ifindex_msg(drv, ifindex, 0, NL80211_CMD_DEL_KEY);
  2074. if (!msg)
  2075. return -ENOBUFS;
  2076. } else {
  2077. msg = nl80211_ifindex_msg(drv, ifindex, 0, NL80211_CMD_NEW_KEY);
  2078. if (!msg ||
  2079. nla_put(msg, NL80211_ATTR_KEY_DATA, key_len, key) ||
  2080. nla_put_u32(msg, NL80211_ATTR_KEY_CIPHER,
  2081. wpa_alg_to_cipher_suite(alg, key_len)))
  2082. goto fail;
  2083. wpa_hexdump_key(MSG_DEBUG, "nl80211: KEY_DATA", key, key_len);
  2084. }
  2085. if (seq && seq_len) {
  2086. if (nla_put(msg, NL80211_ATTR_KEY_SEQ, seq_len, seq))
  2087. goto fail;
  2088. wpa_hexdump(MSG_DEBUG, "nl80211: KEY_SEQ", seq, seq_len);
  2089. }
  2090. if (addr && !is_broadcast_ether_addr(addr)) {
  2091. wpa_printf(MSG_DEBUG, " addr=" MACSTR, MAC2STR(addr));
  2092. if (nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr))
  2093. goto fail;
  2094. if (alg != WPA_ALG_WEP && key_idx && !set_tx) {
  2095. wpa_printf(MSG_DEBUG, " RSN IBSS RX GTK");
  2096. if (nla_put_u32(msg, NL80211_ATTR_KEY_TYPE,
  2097. NL80211_KEYTYPE_GROUP))
  2098. goto fail;
  2099. }
  2100. } else if (addr && is_broadcast_ether_addr(addr)) {
  2101. struct nlattr *types;
  2102. wpa_printf(MSG_DEBUG, " broadcast key");
  2103. types = nla_nest_start(msg, NL80211_ATTR_KEY_DEFAULT_TYPES);
  2104. if (!types ||
  2105. nla_put_flag(msg, NL80211_KEY_DEFAULT_TYPE_MULTICAST))
  2106. goto fail;
  2107. nla_nest_end(msg, types);
  2108. }
  2109. if (nla_put_u8(msg, NL80211_ATTR_KEY_IDX, key_idx))
  2110. goto fail;
  2111. ret = send_and_recv_msgs(drv, msg, NULL, key ? (void *) -1 : NULL);
  2112. if ((ret == -ENOENT || ret == -ENOLINK) && alg == WPA_ALG_NONE)
  2113. ret = 0;
  2114. if (ret)
  2115. wpa_printf(MSG_DEBUG, "nl80211: set_key failed; err=%d %s)",
  2116. ret, strerror(-ret));
  2117. /*
  2118. * If we failed or don't need to set the default TX key (below),
  2119. * we're done here.
  2120. */
  2121. if (ret || !set_tx || alg == WPA_ALG_NONE || tdls)
  2122. return ret;
  2123. if (is_ap_interface(drv->nlmode) && addr &&
  2124. !is_broadcast_ether_addr(addr))
  2125. return ret;
  2126. msg = nl80211_ifindex_msg(drv, ifindex, 0, NL80211_CMD_SET_KEY);
  2127. if (!msg ||
  2128. nla_put_u8(msg, NL80211_ATTR_KEY_IDX, key_idx) ||
  2129. nla_put_flag(msg, (alg == WPA_ALG_IGTK ||
  2130. alg == WPA_ALG_BIP_GMAC_128 ||
  2131. alg == WPA_ALG_BIP_GMAC_256 ||
  2132. alg == WPA_ALG_BIP_CMAC_256) ?
  2133. NL80211_ATTR_KEY_DEFAULT_MGMT :
  2134. NL80211_ATTR_KEY_DEFAULT))
  2135. goto fail;
  2136. if (addr && is_broadcast_ether_addr(addr)) {
  2137. struct nlattr *types;
  2138. types = nla_nest_start(msg, NL80211_ATTR_KEY_DEFAULT_TYPES);
  2139. if (!types ||
  2140. nla_put_flag(msg, NL80211_KEY_DEFAULT_TYPE_MULTICAST))
  2141. goto fail;
  2142. nla_nest_end(msg, types);
  2143. } else if (addr) {
  2144. struct nlattr *types;
  2145. types = nla_nest_start(msg, NL80211_ATTR_KEY_DEFAULT_TYPES);
  2146. if (!types ||
  2147. nla_put_flag(msg, NL80211_KEY_DEFAULT_TYPE_UNICAST))
  2148. goto fail;
  2149. nla_nest_end(msg, types);
  2150. }
  2151. ret = send_and_recv_msgs(drv, msg, NULL, NULL);
  2152. if (ret == -ENOENT)
  2153. ret = 0;
  2154. if (ret)
  2155. wpa_printf(MSG_DEBUG, "nl80211: set_key default failed; "
  2156. "err=%d %s)", ret, strerror(-ret));
  2157. return ret;
  2158. fail:
  2159. nl80211_nlmsg_clear(msg);
  2160. nlmsg_free(msg);
  2161. return -ENOBUFS;
  2162. }
  2163. static int nl_add_key(struct nl_msg *msg, enum wpa_alg alg,
  2164. int key_idx, int defkey,
  2165. const u8 *seq, size_t seq_len,
  2166. const u8 *key, size_t key_len)
  2167. {
  2168. struct nlattr *key_attr = nla_nest_start(msg, NL80211_ATTR_KEY);
  2169. if (!key_attr)
  2170. return -1;
  2171. if (defkey && alg == WPA_ALG_IGTK) {
  2172. if (nla_put_flag(msg, NL80211_KEY_DEFAULT_MGMT))
  2173. return -1;
  2174. } else if (defkey) {
  2175. if (nla_put_flag(msg, NL80211_KEY_DEFAULT))
  2176. return -1;
  2177. }
  2178. if (nla_put_u8(msg, NL80211_KEY_IDX, key_idx) ||
  2179. nla_put_u32(msg, NL80211_KEY_CIPHER,
  2180. wpa_alg_to_cipher_suite(alg, key_len)) ||
  2181. (seq && seq_len &&
  2182. nla_put(msg, NL80211_KEY_SEQ, seq_len, seq)) ||
  2183. nla_put(msg, NL80211_KEY_DATA, key_len, key))
  2184. return -1;
  2185. nla_nest_end(msg, key_attr);
  2186. return 0;
  2187. }
  2188. static int nl80211_set_conn_keys(struct wpa_driver_associate_params *params,
  2189. struct nl_msg *msg)
  2190. {
  2191. int i, privacy = 0;
  2192. struct nlattr *nl_keys, *nl_key;
  2193. for (i = 0; i < 4; i++) {
  2194. if (!params->wep_key[i])
  2195. continue;
  2196. privacy = 1;
  2197. break;
  2198. }
  2199. if (params->wps == WPS_MODE_PRIVACY)
  2200. privacy = 1;
  2201. if (params->pairwise_suite &&
  2202. params->pairwise_suite != WPA_CIPHER_NONE)
  2203. privacy = 1;
  2204. if (!privacy)
  2205. return 0;
  2206. if (nla_put_flag(msg, NL80211_ATTR_PRIVACY))
  2207. return -ENOBUFS;
  2208. nl_keys = nla_nest_start(msg, NL80211_ATTR_KEYS);
  2209. if (!nl_keys)
  2210. return -ENOBUFS;
  2211. for (i = 0; i < 4; i++) {
  2212. if (!params->wep_key[i])
  2213. continue;
  2214. nl_key = nla_nest_start(msg, i);
  2215. if (!nl_key ||
  2216. nla_put(msg, NL80211_KEY_DATA, params->wep_key_len[i],
  2217. params->wep_key[i]) ||
  2218. nla_put_u32(msg, NL80211_KEY_CIPHER,
  2219. params->wep_key_len[i] == 5 ?
  2220. WLAN_CIPHER_SUITE_WEP40 :
  2221. WLAN_CIPHER_SUITE_WEP104) ||
  2222. nla_put_u8(msg, NL80211_KEY_IDX, i) ||
  2223. (i == params->wep_tx_keyidx &&
  2224. nla_put_flag(msg, NL80211_KEY_DEFAULT)))
  2225. return -ENOBUFS;
  2226. nla_nest_end(msg, nl_key);
  2227. }
  2228. nla_nest_end(msg, nl_keys);
  2229. return 0;
  2230. }
  2231. int wpa_driver_nl80211_mlme(struct wpa_driver_nl80211_data *drv,
  2232. const u8 *addr, int cmd, u16 reason_code,
  2233. int local_state_change)
  2234. {
  2235. int ret;
  2236. struct nl_msg *msg;
  2237. if (!(msg = nl80211_drv_msg(drv, 0, cmd)) ||
  2238. nla_put_u16(msg, NL80211_ATTR_REASON_CODE, reason_code) ||
  2239. (addr && nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr)) ||
  2240. (local_state_change &&
  2241. nla_put_flag(msg, NL80211_ATTR_LOCAL_STATE_CHANGE))) {
  2242. nlmsg_free(msg);
  2243. return -1;
  2244. }
  2245. ret = send_and_recv_msgs(drv, msg, NULL, NULL);
  2246. if (ret) {
  2247. wpa_dbg(drv->ctx, MSG_DEBUG,
  2248. "nl80211: MLME command failed: reason=%u ret=%d (%s)",
  2249. reason_code, ret, strerror(-ret));
  2250. }
  2251. return ret;
  2252. }
  2253. static int wpa_driver_nl80211_disconnect(struct wpa_driver_nl80211_data *drv,
  2254. int reason_code)
  2255. {
  2256. int ret;
  2257. wpa_printf(MSG_DEBUG, "%s(reason_code=%d)", __func__, reason_code);
  2258. nl80211_mark_disconnected(drv);
  2259. /* Disconnect command doesn't need BSSID - it uses cached value */
  2260. ret = wpa_driver_nl80211_mlme(drv, NULL, NL80211_CMD_DISCONNECT,
  2261. reason_code, 0);
  2262. /*
  2263. * For locally generated disconnect, supplicant already generates a
  2264. * DEAUTH event, so ignore the event from NL80211.
  2265. */
  2266. drv->ignore_next_local_disconnect = ret == 0;
  2267. return ret;
  2268. }
  2269. static int wpa_driver_nl80211_deauthenticate(struct i802_bss *bss,
  2270. const u8 *addr, int reason_code)
  2271. {
  2272. struct wpa_driver_nl80211_data *drv = bss->drv;
  2273. int ret;
  2274. if (drv->nlmode == NL80211_IFTYPE_ADHOC) {
  2275. nl80211_mark_disconnected(drv);
  2276. return nl80211_leave_ibss(drv, 1);
  2277. }
  2278. if (!(drv->capa.flags & WPA_DRIVER_FLAGS_SME))
  2279. return wpa_driver_nl80211_disconnect(drv, reason_code);
  2280. wpa_printf(MSG_DEBUG, "%s(addr=" MACSTR " reason_code=%d)",
  2281. __func__, MAC2STR(addr), reason_code);
  2282. nl80211_mark_disconnected(drv);
  2283. ret = wpa_driver_nl80211_mlme(drv, addr, NL80211_CMD_DEAUTHENTICATE,
  2284. reason_code, 0);
  2285. /*
  2286. * For locally generated deauthenticate, supplicant already generates a
  2287. * DEAUTH event, so ignore the event from NL80211.
  2288. */
  2289. drv->ignore_next_local_deauth = ret == 0;
  2290. return ret;
  2291. }
  2292. static void nl80211_copy_auth_params(struct wpa_driver_nl80211_data *drv,
  2293. struct wpa_driver_auth_params *params)
  2294. {
  2295. int i;
  2296. drv->auth_freq = params->freq;
  2297. drv->auth_alg = params->auth_alg;
  2298. drv->auth_wep_tx_keyidx = params->wep_tx_keyidx;
  2299. drv->auth_local_state_change = params->local_state_change;
  2300. drv->auth_p2p = params->p2p;
  2301. if (params->bssid)
  2302. os_memcpy(drv->auth_bssid_, params->bssid, ETH_ALEN);
  2303. else
  2304. os_memset(drv->auth_bssid_, 0, ETH_ALEN);
  2305. if (params->ssid) {
  2306. os_memcpy(drv->auth_ssid, params->ssid, params->ssid_len);
  2307. drv->auth_ssid_len = params->ssid_len;
  2308. } else
  2309. drv->auth_ssid_len = 0;
  2310. os_free(drv->auth_ie);
  2311. drv->auth_ie = NULL;
  2312. drv->auth_ie_len = 0;
  2313. if (params->ie) {
  2314. drv->auth_ie = os_malloc(params->ie_len);
  2315. if (drv->auth_ie) {
  2316. os_memcpy(drv->auth_ie, params->ie, params->ie_len);
  2317. drv->auth_ie_len = params->ie_len;
  2318. }
  2319. }
  2320. for (i = 0; i < 4; i++) {
  2321. if (params->wep_key[i] && params->wep_key_len[i] &&
  2322. params->wep_key_len[i] <= 16) {
  2323. os_memcpy(drv->auth_wep_key[i], params->wep_key[i],
  2324. params->wep_key_len[i]);
  2325. drv->auth_wep_key_len[i] = params->wep_key_len[i];
  2326. } else
  2327. drv->auth_wep_key_len[i] = 0;
  2328. }
  2329. }
  2330. static void nl80211_unmask_11b_rates(struct i802_bss *bss)
  2331. {
  2332. struct wpa_driver_nl80211_data *drv = bss->drv;
  2333. if (is_p2p_net_interface(drv->nlmode) || !drv->disabled_11b_rates)
  2334. return;
  2335. /*
  2336. * Looks like we failed to unmask 11b rates previously. This could
  2337. * happen, e.g., if the interface was down at the point in time when a
  2338. * P2P group was terminated.
  2339. */
  2340. wpa_printf(MSG_DEBUG,
  2341. "nl80211: Interface %s mode is for non-P2P, but 11b rates were disabled - re-enable them",
  2342. bss->ifname);
  2343. nl80211_disable_11b_rates(drv, drv->ifindex, 0);
  2344. }
  2345. static int wpa_driver_nl80211_authenticate(
  2346. struct i802_bss *bss, struct wpa_driver_auth_params *params)
  2347. {
  2348. struct wpa_driver_nl80211_data *drv = bss->drv;
  2349. int ret = -1, i;
  2350. struct nl_msg *msg;
  2351. enum nl80211_auth_type type;
  2352. enum nl80211_iftype nlmode;
  2353. int count = 0;
  2354. int is_retry;
  2355. nl80211_unmask_11b_rates(bss);
  2356. is_retry = drv->retry_auth;
  2357. drv->retry_auth = 0;
  2358. drv->ignore_deauth_event = 0;
  2359. nl80211_mark_disconnected(drv);
  2360. os_memset(drv->auth_bssid, 0, ETH_ALEN);
  2361. if (params->bssid)
  2362. os_memcpy(drv->auth_attempt_bssid, params->bssid, ETH_ALEN);
  2363. else
  2364. os_memset(drv->auth_attempt_bssid, 0, ETH_ALEN);
  2365. /* FIX: IBSS mode */
  2366. nlmode = params->p2p ?
  2367. NL80211_IFTYPE_P2P_CLIENT : NL80211_IFTYPE_STATION;
  2368. if (drv->nlmode != nlmode &&
  2369. wpa_driver_nl80211_set_mode(bss, nlmode) < 0)
  2370. return -1;
  2371. retry:
  2372. wpa_printf(MSG_DEBUG, "nl80211: Authenticate (ifindex=%d)",
  2373. drv->ifindex);
  2374. msg = nl80211_drv_msg(drv, 0, NL80211_CMD_AUTHENTICATE);
  2375. if (!msg)
  2376. goto fail;
  2377. for (i = 0; i < 4; i++) {
  2378. if (!params->wep_key[i])
  2379. continue;
  2380. wpa_driver_nl80211_set_key(bss->ifname, bss, WPA_ALG_WEP,
  2381. NULL, i,
  2382. i == params->wep_tx_keyidx, NULL, 0,
  2383. params->wep_key[i],
  2384. params->wep_key_len[i]);
  2385. if (params->wep_tx_keyidx != i)
  2386. continue;
  2387. if (nl_add_key(msg, WPA_ALG_WEP, i, 1, NULL, 0,
  2388. params->wep_key[i], params->wep_key_len[i]))
  2389. goto fail;
  2390. }
  2391. if (params->bssid) {
  2392. wpa_printf(MSG_DEBUG, " * bssid=" MACSTR,
  2393. MAC2STR(params->bssid));
  2394. if (nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, params->bssid))
  2395. goto fail;
  2396. }
  2397. if (params->freq) {
  2398. wpa_printf(MSG_DEBUG, " * freq=%d", params->freq);
  2399. if (nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, params->freq))
  2400. goto fail;
  2401. }
  2402. if (params->ssid) {
  2403. wpa_hexdump_ascii(MSG_DEBUG, " * SSID",
  2404. params->ssid, params->ssid_len);
  2405. if (nla_put(msg, NL80211_ATTR_SSID, params->ssid_len,
  2406. params->ssid))
  2407. goto fail;
  2408. }
  2409. wpa_hexdump(MSG_DEBUG, " * IEs", params->ie, params->ie_len);
  2410. if (params->ie &&
  2411. nla_put(msg, NL80211_ATTR_IE, params->ie_len, params->ie))
  2412. goto fail;
  2413. if (params->sae_data) {
  2414. wpa_hexdump(MSG_DEBUG, " * SAE data", params->sae_data,
  2415. params->sae_data_len);
  2416. if (nla_put(msg, NL80211_ATTR_SAE_DATA, params->sae_data_len,
  2417. params->sae_data))
  2418. goto fail;
  2419. }
  2420. if (params->auth_alg & WPA_AUTH_ALG_OPEN)
  2421. type = NL80211_AUTHTYPE_OPEN_SYSTEM;
  2422. else if (params->auth_alg & WPA_AUTH_ALG_SHARED)
  2423. type = NL80211_AUTHTYPE_SHARED_KEY;
  2424. else if (params->auth_alg & WPA_AUTH_ALG_LEAP)
  2425. type = NL80211_AUTHTYPE_NETWORK_EAP;
  2426. else if (params->auth_alg & WPA_AUTH_ALG_FT)
  2427. type = NL80211_AUTHTYPE_FT;
  2428. else if (params->auth_alg & WPA_AUTH_ALG_SAE)
  2429. type = NL80211_AUTHTYPE_SAE;
  2430. else
  2431. goto fail;
  2432. wpa_printf(MSG_DEBUG, " * Auth Type %d", type);
  2433. if (nla_put_u32(msg, NL80211_ATTR_AUTH_TYPE, type))
  2434. goto fail;
  2435. if (params->local_state_change) {
  2436. wpa_printf(MSG_DEBUG, " * Local state change only");
  2437. if (nla_put_flag(msg, NL80211_ATTR_LOCAL_STATE_CHANGE))
  2438. goto fail;
  2439. }
  2440. ret = send_and_recv_msgs(drv, msg, NULL, NULL);
  2441. msg = NULL;
  2442. if (ret) {
  2443. wpa_dbg(drv->ctx, MSG_DEBUG,
  2444. "nl80211: MLME command failed (auth): ret=%d (%s)",
  2445. ret, strerror(-ret));
  2446. count++;
  2447. if (ret == -EALREADY && count == 1 && params->bssid &&
  2448. !params->local_state_change) {
  2449. /*
  2450. * mac80211 does not currently accept new
  2451. * authentication if we are already authenticated. As a
  2452. * workaround, force deauthentication and try again.
  2453. */
  2454. wpa_printf(MSG_DEBUG, "nl80211: Retry authentication "
  2455. "after forced deauthentication");
  2456. drv->ignore_deauth_event = 1;
  2457. wpa_driver_nl80211_deauthenticate(
  2458. bss, params->bssid,
  2459. WLAN_REASON_PREV_AUTH_NOT_VALID);
  2460. nlmsg_free(msg);
  2461. goto retry;
  2462. }
  2463. if (ret == -ENOENT && params->freq && !is_retry) {
  2464. /*
  2465. * cfg80211 has likely expired the BSS entry even
  2466. * though it was previously available in our internal
  2467. * BSS table. To recover quickly, start a single
  2468. * channel scan on the specified channel.
  2469. */
  2470. struct wpa_driver_scan_params scan;
  2471. int freqs[2];
  2472. os_memset(&scan, 0, sizeof(scan));
  2473. scan.num_ssids = 1;
  2474. if (params->ssid) {
  2475. scan.ssids[0].ssid = params->ssid;
  2476. scan.ssids[0].ssid_len = params->ssid_len;
  2477. }
  2478. freqs[0] = params->freq;
  2479. freqs[1] = 0;
  2480. scan.freqs = freqs;
  2481. wpa_printf(MSG_DEBUG, "nl80211: Trigger single "
  2482. "channel scan to refresh cfg80211 BSS "
  2483. "entry");
  2484. ret = wpa_driver_nl80211_scan(bss, &scan);
  2485. if (ret == 0) {
  2486. nl80211_copy_auth_params(drv, params);
  2487. drv->scan_for_auth = 1;
  2488. }
  2489. } else if (is_retry) {
  2490. /*
  2491. * Need to indicate this with an event since the return
  2492. * value from the retry is not delivered to core code.
  2493. */
  2494. union wpa_event_data event;
  2495. wpa_printf(MSG_DEBUG, "nl80211: Authentication retry "
  2496. "failed");
  2497. os_memset(&event, 0, sizeof(event));
  2498. os_memcpy(event.timeout_event.addr, drv->auth_bssid_,
  2499. ETH_ALEN);
  2500. wpa_supplicant_event(drv->ctx, EVENT_AUTH_TIMED_OUT,
  2501. &event);
  2502. }
  2503. } else {
  2504. wpa_printf(MSG_DEBUG,
  2505. "nl80211: Authentication request send successfully");
  2506. }
  2507. fail:
  2508. nlmsg_free(msg);
  2509. return ret;
  2510. }
  2511. int wpa_driver_nl80211_authenticate_retry(struct wpa_driver_nl80211_data *drv)
  2512. {
  2513. struct wpa_driver_auth_params params;
  2514. struct i802_bss *bss = drv->first_bss;
  2515. int i;
  2516. wpa_printf(MSG_DEBUG, "nl80211: Try to authenticate again");
  2517. os_memset(&params, 0, sizeof(params));
  2518. params.freq = drv->auth_freq;
  2519. params.auth_alg = drv->auth_alg;
  2520. params.wep_tx_keyidx = drv->auth_wep_tx_keyidx;
  2521. params.local_state_change = drv->auth_local_state_change;
  2522. params.p2p = drv->auth_p2p;
  2523. if (!is_zero_ether_addr(drv->auth_bssid_))
  2524. params.bssid = drv->auth_bssid_;
  2525. if (drv->auth_ssid_len) {
  2526. params.ssid = drv->auth_ssid;
  2527. params.ssid_len = drv->auth_ssid_len;
  2528. }
  2529. params.ie = drv->auth_ie;
  2530. params.ie_len = drv->auth_ie_len;
  2531. for (i = 0; i < 4; i++) {
  2532. if (drv->auth_wep_key_len[i]) {
  2533. params.wep_key[i] = drv->auth_wep_key[i];
  2534. params.wep_key_len[i] = drv->auth_wep_key_len[i];
  2535. }
  2536. }
  2537. drv->retry_auth = 1;
  2538. return wpa_driver_nl80211_authenticate(bss, &params);
  2539. }
  2540. static int wpa_driver_nl80211_send_frame(struct i802_bss *bss,
  2541. const void *data, size_t len,
  2542. int encrypt, int noack,
  2543. unsigned int freq, int no_cck,
  2544. int offchanok, unsigned int wait_time)
  2545. {
  2546. struct wpa_driver_nl80211_data *drv = bss->drv;
  2547. u64 cookie;
  2548. int res;
  2549. if (freq == 0 && drv->nlmode == NL80211_IFTYPE_ADHOC) {
  2550. freq = nl80211_get_assoc_freq(drv);
  2551. wpa_printf(MSG_DEBUG,
  2552. "nl80211: send_frame - Use assoc_freq=%u for IBSS",
  2553. freq);
  2554. }
  2555. if (freq == 0) {
  2556. wpa_printf(MSG_DEBUG, "nl80211: send_frame - Use bss->freq=%u",
  2557. bss->freq);
  2558. freq = bss->freq;
  2559. }
  2560. if (drv->use_monitor) {
  2561. wpa_printf(MSG_DEBUG, "nl80211: send_frame(freq=%u bss->freq=%u) -> send_monitor",
  2562. freq, bss->freq);
  2563. return nl80211_send_monitor(drv, data, len, encrypt, noack);
  2564. }
  2565. wpa_printf(MSG_DEBUG, "nl80211: send_frame -> send_frame_cmd");
  2566. res = nl80211_send_frame_cmd(bss, freq, wait_time, data, len,
  2567. &cookie, no_cck, noack, offchanok);
  2568. if (res == 0 && !noack) {
  2569. const struct ieee80211_mgmt *mgmt;
  2570. u16 fc;
  2571. mgmt = (const struct ieee80211_mgmt *) data;
  2572. fc = le_to_host16(mgmt->frame_control);
  2573. if (WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT &&
  2574. WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_ACTION) {
  2575. wpa_printf(MSG_MSGDUMP,
  2576. "nl80211: Update send_action_cookie from 0x%llx to 0x%llx",
  2577. (long long unsigned int)
  2578. drv->send_action_cookie,
  2579. (long long unsigned int) cookie);
  2580. drv->send_action_cookie = cookie;
  2581. }
  2582. }
  2583. return res;
  2584. }
  2585. static int wpa_driver_nl80211_send_mlme(struct i802_bss *bss, const u8 *data,
  2586. size_t data_len, int noack,
  2587. unsigned int freq, int no_cck,
  2588. int offchanok,
  2589. unsigned int wait_time)
  2590. {
  2591. struct wpa_driver_nl80211_data *drv = bss->drv;
  2592. struct ieee80211_mgmt *mgmt;
  2593. int encrypt = 1;
  2594. u16 fc;
  2595. mgmt = (struct ieee80211_mgmt *) data;
  2596. fc = le_to_host16(mgmt->frame_control);
  2597. wpa_printf(MSG_DEBUG, "nl80211: send_mlme - da= " MACSTR
  2598. " noack=%d freq=%u no_cck=%d offchanok=%d wait_time=%u fc=0x%x (%s) nlmode=%d",
  2599. MAC2STR(mgmt->da), noack, freq, no_cck, offchanok, wait_time,
  2600. fc, fc2str(fc), drv->nlmode);
  2601. if ((is_sta_interface(drv->nlmode) ||
  2602. drv->nlmode == NL80211_IFTYPE_P2P_DEVICE) &&
  2603. WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT &&
  2604. WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_PROBE_RESP) {
  2605. /*
  2606. * The use of last_mgmt_freq is a bit of a hack,
  2607. * but it works due to the single-threaded nature
  2608. * of wpa_supplicant.
  2609. */
  2610. if (freq == 0) {
  2611. wpa_printf(MSG_DEBUG, "nl80211: Use last_mgmt_freq=%d",
  2612. drv->last_mgmt_freq);
  2613. freq = drv->last_mgmt_freq;
  2614. }
  2615. return nl80211_send_frame_cmd(bss, freq, 0,
  2616. data, data_len, NULL, 1, noack,
  2617. 1);
  2618. }
  2619. if (drv->device_ap_sme && is_ap_interface(drv->nlmode)) {
  2620. if (freq == 0) {
  2621. wpa_printf(MSG_DEBUG, "nl80211: Use bss->freq=%d",
  2622. bss->freq);
  2623. freq = bss->freq;
  2624. }
  2625. return nl80211_send_frame_cmd(bss, freq,
  2626. (int) freq == bss->freq ? 0 :
  2627. wait_time,
  2628. data, data_len,
  2629. &drv->send_action_cookie,
  2630. no_cck, noack, offchanok);
  2631. }
  2632. if (WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT &&
  2633. WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_AUTH) {
  2634. /*
  2635. * Only one of the authentication frame types is encrypted.
  2636. * In order for static WEP encryption to work properly (i.e.,
  2637. * to not encrypt the frame), we need to tell mac80211 about
  2638. * the frames that must not be encrypted.
  2639. */
  2640. u16 auth_alg = le_to_host16(mgmt->u.auth.auth_alg);
  2641. u16 auth_trans = le_to_host16(mgmt->u.auth.auth_transaction);
  2642. if (auth_alg != WLAN_AUTH_SHARED_KEY || auth_trans != 3)
  2643. encrypt = 0;
  2644. }
  2645. wpa_printf(MSG_DEBUG, "nl80211: send_mlme -> send_frame");
  2646. return wpa_driver_nl80211_send_frame(bss, data, data_len, encrypt,
  2647. noack, freq, no_cck, offchanok,
  2648. wait_time);
  2649. }
  2650. static int nl80211_put_basic_rates(struct nl_msg *msg, const int *basic_rates)
  2651. {
  2652. u8 rates[NL80211_MAX_SUPP_RATES];
  2653. u8 rates_len = 0;
  2654. int i;
  2655. if (!basic_rates)
  2656. return 0;
  2657. for (i = 0; i < NL80211_MAX_SUPP_RATES && basic_rates[i] >= 0; i++)
  2658. rates[rates_len++] = basic_rates[i] / 5;
  2659. return nla_put(msg, NL80211_ATTR_BSS_BASIC_RATES, rates_len, rates);
  2660. }
  2661. static int nl80211_set_bss(struct i802_bss *bss, int cts, int preamble,
  2662. int slot, int ht_opmode, int ap_isolate,
  2663. const int *basic_rates)
  2664. {
  2665. struct wpa_driver_nl80211_data *drv = bss->drv;
  2666. struct nl_msg *msg;
  2667. if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_SET_BSS)) ||
  2668. (cts >= 0 &&
  2669. nla_put_u8(msg, NL80211_ATTR_BSS_CTS_PROT, cts)) ||
  2670. (preamble >= 0 &&
  2671. nla_put_u8(msg, NL80211_ATTR_BSS_SHORT_PREAMBLE, preamble)) ||
  2672. (slot >= 0 &&
  2673. nla_put_u8(msg, NL80211_ATTR_BSS_SHORT_SLOT_TIME, slot)) ||
  2674. (ht_opmode >= 0 &&
  2675. nla_put_u16(msg, NL80211_ATTR_BSS_HT_OPMODE, ht_opmode)) ||
  2676. (ap_isolate >= 0 &&
  2677. nla_put_u8(msg, NL80211_ATTR_AP_ISOLATE, ap_isolate)) ||
  2678. nl80211_put_basic_rates(msg, basic_rates)) {
  2679. nlmsg_free(msg);
  2680. return -ENOBUFS;
  2681. }
  2682. return send_and_recv_msgs(drv, msg, NULL, NULL);
  2683. }
  2684. static int wpa_driver_nl80211_set_acl(void *priv,
  2685. struct hostapd_acl_params *params)
  2686. {
  2687. struct i802_bss *bss = priv;
  2688. struct wpa_driver_nl80211_data *drv = bss->drv;
  2689. struct nl_msg *msg;
  2690. struct nlattr *acl;
  2691. unsigned int i;
  2692. int ret;
  2693. if (!(drv->capa.max_acl_mac_addrs))
  2694. return -ENOTSUP;
  2695. if (params->num_mac_acl > drv->capa.max_acl_mac_addrs)
  2696. return -ENOTSUP;
  2697. wpa_printf(MSG_DEBUG, "nl80211: Set %s ACL (num_mac_acl=%u)",
  2698. params->acl_policy ? "Accept" : "Deny", params->num_mac_acl);
  2699. if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_SET_MAC_ACL)) ||
  2700. nla_put_u32(msg, NL80211_ATTR_ACL_POLICY, params->acl_policy ?
  2701. NL80211_ACL_POLICY_DENY_UNLESS_LISTED :
  2702. NL80211_ACL_POLICY_ACCEPT_UNLESS_LISTED) ||
  2703. (acl = nla_nest_start(msg, NL80211_ATTR_MAC_ADDRS)) == NULL) {
  2704. nlmsg_free(msg);
  2705. return -ENOMEM;
  2706. }
  2707. for (i = 0; i < params->num_mac_acl; i++) {
  2708. if (nla_put(msg, i + 1, ETH_ALEN, params->mac_acl[i].addr)) {
  2709. nlmsg_free(msg);
  2710. return -ENOMEM;
  2711. }
  2712. }
  2713. nla_nest_end(msg, acl);
  2714. ret = send_and_recv_msgs(drv, msg, NULL, NULL);
  2715. if (ret) {
  2716. wpa_printf(MSG_DEBUG, "nl80211: Failed to set MAC ACL: %d (%s)",
  2717. ret, strerror(-ret));
  2718. }
  2719. return ret;
  2720. }
  2721. static int nl80211_put_beacon_int(struct nl_msg *msg, int beacon_int)
  2722. {
  2723. if (beacon_int > 0) {
  2724. wpa_printf(MSG_DEBUG, " * beacon_int=%d", beacon_int);
  2725. return nla_put_u32(msg, NL80211_ATTR_BEACON_INTERVAL,
  2726. beacon_int);
  2727. }
  2728. return 0;
  2729. }
  2730. static int wpa_driver_nl80211_set_ap(void *priv,
  2731. struct wpa_driver_ap_params *params)
  2732. {
  2733. struct i802_bss *bss = priv;
  2734. struct wpa_driver_nl80211_data *drv = bss->drv;
  2735. struct nl_msg *msg;
  2736. u8 cmd = NL80211_CMD_NEW_BEACON;
  2737. int ret;
  2738. int beacon_set;
  2739. int num_suites;
  2740. int smps_mode;
  2741. u32 suites[10], suite;
  2742. u32 ver;
  2743. beacon_set = params->reenable ? 0 : bss->beacon_set;
  2744. wpa_printf(MSG_DEBUG, "nl80211: Set beacon (beacon_set=%d)",
  2745. beacon_set);
  2746. if (beacon_set)
  2747. cmd = NL80211_CMD_SET_BEACON;
  2748. wpa_hexdump(MSG_DEBUG, "nl80211: Beacon head",
  2749. params->head, params->head_len);
  2750. wpa_hexdump(MSG_DEBUG, "nl80211: Beacon tail",
  2751. params->tail, params->tail_len);
  2752. wpa_printf(MSG_DEBUG, "nl80211: ifindex=%d", bss->ifindex);
  2753. wpa_printf(MSG_DEBUG, "nl80211: beacon_int=%d", params->beacon_int);
  2754. wpa_printf(MSG_DEBUG, "nl80211: dtim_period=%d", params->dtim_period);
  2755. wpa_hexdump_ascii(MSG_DEBUG, "nl80211: ssid",
  2756. params->ssid, params->ssid_len);
  2757. if (!(msg = nl80211_bss_msg(bss, 0, cmd)) ||
  2758. nla_put(msg, NL80211_ATTR_BEACON_HEAD, params->head_len,
  2759. params->head) ||
  2760. nla_put(msg, NL80211_ATTR_BEACON_TAIL, params->tail_len,
  2761. params->tail) ||
  2762. nl80211_put_beacon_int(msg, params->beacon_int) ||
  2763. nla_put_u32(msg, NL80211_ATTR_DTIM_PERIOD, params->dtim_period) ||
  2764. nla_put(msg, NL80211_ATTR_SSID, params->ssid_len, params->ssid))
  2765. goto fail;
  2766. if (params->proberesp && params->proberesp_len) {
  2767. wpa_hexdump(MSG_DEBUG, "nl80211: proberesp (offload)",
  2768. params->proberesp, params->proberesp_len);
  2769. if (nla_put(msg, NL80211_ATTR_PROBE_RESP, params->proberesp_len,
  2770. params->proberesp))
  2771. goto fail;
  2772. }
  2773. switch (params->hide_ssid) {
  2774. case NO_SSID_HIDING:
  2775. wpa_printf(MSG_DEBUG, "nl80211: hidden SSID not in use");
  2776. if (nla_put_u32(msg, NL80211_ATTR_HIDDEN_SSID,
  2777. NL80211_HIDDEN_SSID_NOT_IN_USE))
  2778. goto fail;
  2779. break;
  2780. case HIDDEN_SSID_ZERO_LEN:
  2781. wpa_printf(MSG_DEBUG, "nl80211: hidden SSID zero len");
  2782. if (nla_put_u32(msg, NL80211_ATTR_HIDDEN_SSID,
  2783. NL80211_HIDDEN_SSID_ZERO_LEN))
  2784. goto fail;
  2785. break;
  2786. case HIDDEN_SSID_ZERO_CONTENTS:
  2787. wpa_printf(MSG_DEBUG, "nl80211: hidden SSID zero contents");
  2788. if (nla_put_u32(msg, NL80211_ATTR_HIDDEN_SSID,
  2789. NL80211_HIDDEN_SSID_ZERO_CONTENTS))
  2790. goto fail;
  2791. break;
  2792. }
  2793. wpa_printf(MSG_DEBUG, "nl80211: privacy=%d", params->privacy);
  2794. if (params->privacy &&
  2795. nla_put_flag(msg, NL80211_ATTR_PRIVACY))
  2796. goto fail;
  2797. wpa_printf(MSG_DEBUG, "nl80211: auth_algs=0x%x", params->auth_algs);
  2798. if ((params->auth_algs & (WPA_AUTH_ALG_OPEN | WPA_AUTH_ALG_SHARED)) ==
  2799. (WPA_AUTH_ALG_OPEN | WPA_AUTH_ALG_SHARED)) {
  2800. /* Leave out the attribute */
  2801. } else if (params->auth_algs & WPA_AUTH_ALG_SHARED) {
  2802. if (nla_put_u32(msg, NL80211_ATTR_AUTH_TYPE,
  2803. NL80211_AUTHTYPE_SHARED_KEY))
  2804. goto fail;
  2805. } else {
  2806. if (nla_put_u32(msg, NL80211_ATTR_AUTH_TYPE,
  2807. NL80211_AUTHTYPE_OPEN_SYSTEM))
  2808. goto fail;
  2809. }
  2810. wpa_printf(MSG_DEBUG, "nl80211: wpa_version=0x%x", params->wpa_version);
  2811. ver = 0;
  2812. if (params->wpa_version & WPA_PROTO_WPA)
  2813. ver |= NL80211_WPA_VERSION_1;
  2814. if (params->wpa_version & WPA_PROTO_RSN)
  2815. ver |= NL80211_WPA_VERSION_2;
  2816. if (ver &&
  2817. nla_put_u32(msg, NL80211_ATTR_WPA_VERSIONS, ver))
  2818. goto fail;
  2819. wpa_printf(MSG_DEBUG, "nl80211: key_mgmt_suites=0x%x",
  2820. params->key_mgmt_suites);
  2821. num_suites = 0;
  2822. if (params->key_mgmt_suites & WPA_KEY_MGMT_IEEE8021X)
  2823. suites[num_suites++] = WLAN_AKM_SUITE_8021X;
  2824. if (params->key_mgmt_suites & WPA_KEY_MGMT_PSK)
  2825. suites[num_suites++] = WLAN_AKM_SUITE_PSK;
  2826. if (num_suites &&
  2827. nla_put(msg, NL80211_ATTR_AKM_SUITES, num_suites * sizeof(u32),
  2828. suites))
  2829. goto fail;
  2830. if (params->key_mgmt_suites & WPA_KEY_MGMT_IEEE8021X_NO_WPA &&
  2831. params->pairwise_ciphers & (WPA_CIPHER_WEP104 | WPA_CIPHER_WEP40) &&
  2832. nla_put_flag(msg, NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT))
  2833. goto fail;
  2834. wpa_printf(MSG_DEBUG, "nl80211: pairwise_ciphers=0x%x",
  2835. params->pairwise_ciphers);
  2836. num_suites = wpa_cipher_to_cipher_suites(params->pairwise_ciphers,
  2837. suites, ARRAY_SIZE(suites));
  2838. if (num_suites &&
  2839. nla_put(msg, NL80211_ATTR_CIPHER_SUITES_PAIRWISE,
  2840. num_suites * sizeof(u32), suites))
  2841. goto fail;
  2842. wpa_printf(MSG_DEBUG, "nl80211: group_cipher=0x%x",
  2843. params->group_cipher);
  2844. suite = wpa_cipher_to_cipher_suite(params->group_cipher);
  2845. if (suite &&
  2846. nla_put_u32(msg, NL80211_ATTR_CIPHER_SUITE_GROUP, suite))
  2847. goto fail;
  2848. switch (params->smps_mode) {
  2849. case HT_CAP_INFO_SMPS_DYNAMIC:
  2850. wpa_printf(MSG_DEBUG, "nl80211: SMPS mode - dynamic");
  2851. smps_mode = NL80211_SMPS_DYNAMIC;
  2852. break;
  2853. case HT_CAP_INFO_SMPS_STATIC:
  2854. wpa_printf(MSG_DEBUG, "nl80211: SMPS mode - static");
  2855. smps_mode = NL80211_SMPS_STATIC;
  2856. break;
  2857. default:
  2858. /* invalid - fallback to smps off */
  2859. case HT_CAP_INFO_SMPS_DISABLED:
  2860. wpa_printf(MSG_DEBUG, "nl80211: SMPS mode - off");
  2861. smps_mode = NL80211_SMPS_OFF;
  2862. break;
  2863. }
  2864. if (nla_put_u32(msg, NL80211_ATTR_SMPS_MODE, smps_mode))
  2865. goto fail;
  2866. if (params->beacon_ies) {
  2867. wpa_hexdump_buf(MSG_DEBUG, "nl80211: beacon_ies",
  2868. params->beacon_ies);
  2869. if (nla_put(msg, NL80211_ATTR_IE,
  2870. wpabuf_len(params->beacon_ies),
  2871. wpabuf_head(params->beacon_ies)))
  2872. goto fail;
  2873. }
  2874. if (params->proberesp_ies) {
  2875. wpa_hexdump_buf(MSG_DEBUG, "nl80211: proberesp_ies",
  2876. params->proberesp_ies);
  2877. if (nla_put(msg, NL80211_ATTR_IE_PROBE_RESP,
  2878. wpabuf_len(params->proberesp_ies),
  2879. wpabuf_head(params->proberesp_ies)))
  2880. goto fail;
  2881. }
  2882. if (params->assocresp_ies) {
  2883. wpa_hexdump_buf(MSG_DEBUG, "nl80211: assocresp_ies",
  2884. params->assocresp_ies);
  2885. if (nla_put(msg, NL80211_ATTR_IE_ASSOC_RESP,
  2886. wpabuf_len(params->assocresp_ies),
  2887. wpabuf_head(params->assocresp_ies)))
  2888. goto fail;
  2889. }
  2890. if (drv->capa.flags & WPA_DRIVER_FLAGS_INACTIVITY_TIMER) {
  2891. wpa_printf(MSG_DEBUG, "nl80211: ap_max_inactivity=%d",
  2892. params->ap_max_inactivity);
  2893. if (nla_put_u16(msg, NL80211_ATTR_INACTIVITY_TIMEOUT,
  2894. params->ap_max_inactivity))
  2895. goto fail;
  2896. }
  2897. #ifdef CONFIG_P2P
  2898. if (params->p2p_go_ctwindow > 0) {
  2899. if (drv->p2p_go_ctwindow_supported) {
  2900. wpa_printf(MSG_DEBUG, "nl80211: P2P GO ctwindow=%d",
  2901. params->p2p_go_ctwindow);
  2902. if (nla_put_u8(msg, NL80211_ATTR_P2P_CTWINDOW,
  2903. params->p2p_go_ctwindow))
  2904. goto fail;
  2905. } else {
  2906. wpa_printf(MSG_INFO,
  2907. "nl80211: Driver does not support CTWindow configuration - ignore this parameter");
  2908. }
  2909. }
  2910. #endif /* CONFIG_P2P */
  2911. ret = send_and_recv_msgs(drv, msg, NULL, NULL);
  2912. if (ret) {
  2913. wpa_printf(MSG_DEBUG, "nl80211: Beacon set failed: %d (%s)",
  2914. ret, strerror(-ret));
  2915. } else {
  2916. bss->beacon_set = 1;
  2917. nl80211_set_bss(bss, params->cts_protect, params->preamble,
  2918. params->short_slot_time, params->ht_opmode,
  2919. params->isolate, params->basic_rates);
  2920. if (beacon_set && params->freq &&
  2921. params->freq->bandwidth != bss->bandwidth) {
  2922. wpa_printf(MSG_DEBUG,
  2923. "nl80211: Update BSS %s bandwidth: %d -> %d",
  2924. bss->ifname, bss->bandwidth,
  2925. params->freq->bandwidth);
  2926. ret = nl80211_set_channel(bss, params->freq, 1);
  2927. if (ret) {
  2928. wpa_printf(MSG_DEBUG,
  2929. "nl80211: Frequency set failed: %d (%s)",
  2930. ret, strerror(-ret));
  2931. } else {
  2932. wpa_printf(MSG_DEBUG,
  2933. "nl80211: Frequency set succeeded for ht2040 coex");
  2934. bss->bandwidth = params->freq->bandwidth;
  2935. }
  2936. } else if (!beacon_set) {
  2937. /*
  2938. * cfg80211 updates the driver on frequence change in AP
  2939. * mode only at the point when beaconing is started, so
  2940. * set the initial value here.
  2941. */
  2942. bss->bandwidth = params->freq->bandwidth;
  2943. }
  2944. }
  2945. return ret;
  2946. fail:
  2947. nlmsg_free(msg);
  2948. return -ENOBUFS;
  2949. }
  2950. static int nl80211_put_freq_params(struct nl_msg *msg,
  2951. const struct hostapd_freq_params *freq)
  2952. {
  2953. wpa_printf(MSG_DEBUG, " * freq=%d", freq->freq);
  2954. if (nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, freq->freq))
  2955. return -ENOBUFS;
  2956. wpa_printf(MSG_DEBUG, " * vht_enabled=%d", freq->vht_enabled);
  2957. wpa_printf(MSG_DEBUG, " * ht_enabled=%d", freq->ht_enabled);
  2958. if (freq->vht_enabled) {
  2959. enum nl80211_chan_width cw;
  2960. wpa_printf(MSG_DEBUG, " * bandwidth=%d", freq->bandwidth);
  2961. switch (freq->bandwidth) {
  2962. case 20:
  2963. cw = NL80211_CHAN_WIDTH_20;
  2964. break;
  2965. case 40:
  2966. cw = NL80211_CHAN_WIDTH_40;
  2967. break;
  2968. case 80:
  2969. if (freq->center_freq2)
  2970. cw = NL80211_CHAN_WIDTH_80P80;
  2971. else
  2972. cw = NL80211_CHAN_WIDTH_80;
  2973. break;
  2974. case 160:
  2975. cw = NL80211_CHAN_WIDTH_160;
  2976. break;
  2977. default:
  2978. return -EINVAL;
  2979. }
  2980. wpa_printf(MSG_DEBUG, " * channel_width=%d", cw);
  2981. wpa_printf(MSG_DEBUG, " * center_freq1=%d",
  2982. freq->center_freq1);
  2983. wpa_printf(MSG_DEBUG, " * center_freq2=%d",
  2984. freq->center_freq2);
  2985. if (nla_put_u32(msg, NL80211_ATTR_CHANNEL_WIDTH, cw) ||
  2986. nla_put_u32(msg, NL80211_ATTR_CENTER_FREQ1,
  2987. freq->center_freq1) ||
  2988. (freq->center_freq2 &&
  2989. nla_put_u32(msg, NL80211_ATTR_CENTER_FREQ2,
  2990. freq->center_freq2)))
  2991. return -ENOBUFS;
  2992. } else if (freq->ht_enabled) {
  2993. enum nl80211_channel_type ct;
  2994. wpa_printf(MSG_DEBUG, " * sec_channel_offset=%d",
  2995. freq->sec_channel_offset);
  2996. switch (freq->sec_channel_offset) {
  2997. case -1:
  2998. ct = NL80211_CHAN_HT40MINUS;
  2999. break;
  3000. case 1:
  3001. ct = NL80211_CHAN_HT40PLUS;
  3002. break;
  3003. default:
  3004. ct = NL80211_CHAN_HT20;
  3005. break;
  3006. }
  3007. wpa_printf(MSG_DEBUG, " * channel_type=%d", ct);
  3008. if (nla_put_u32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE, ct))
  3009. return -ENOBUFS;
  3010. }
  3011. return 0;
  3012. }
  3013. static int nl80211_set_channel(struct i802_bss *bss,
  3014. struct hostapd_freq_params *freq, int set_chan)
  3015. {
  3016. struct wpa_driver_nl80211_data *drv = bss->drv;
  3017. struct nl_msg *msg;
  3018. int ret;
  3019. wpa_printf(MSG_DEBUG,
  3020. "nl80211: Set freq %d (ht_enabled=%d, vht_enabled=%d, bandwidth=%d MHz, cf1=%d MHz, cf2=%d MHz)",
  3021. freq->freq, freq->ht_enabled, freq->vht_enabled,
  3022. freq->bandwidth, freq->center_freq1, freq->center_freq2);
  3023. msg = nl80211_drv_msg(drv, 0, set_chan ? NL80211_CMD_SET_CHANNEL :
  3024. NL80211_CMD_SET_WIPHY);
  3025. if (!msg || nl80211_put_freq_params(msg, freq) < 0) {
  3026. nlmsg_free(msg);
  3027. return -1;
  3028. }
  3029. ret = send_and_recv_msgs(drv, msg, NULL, NULL);
  3030. if (ret == 0) {
  3031. bss->freq = freq->freq;
  3032. return 0;
  3033. }
  3034. wpa_printf(MSG_DEBUG, "nl80211: Failed to set channel (freq=%d): "
  3035. "%d (%s)", freq->freq, ret, strerror(-ret));
  3036. return -1;
  3037. }
  3038. static u32 sta_flags_nl80211(int flags)
  3039. {
  3040. u32 f = 0;
  3041. if (flags & WPA_STA_AUTHORIZED)
  3042. f |= BIT(NL80211_STA_FLAG_AUTHORIZED);
  3043. if (flags & WPA_STA_WMM)
  3044. f |= BIT(NL80211_STA_FLAG_WME);
  3045. if (flags & WPA_STA_SHORT_PREAMBLE)
  3046. f |= BIT(NL80211_STA_FLAG_SHORT_PREAMBLE);
  3047. if (flags & WPA_STA_MFP)
  3048. f |= BIT(NL80211_STA_FLAG_MFP);
  3049. if (flags & WPA_STA_TDLS_PEER)
  3050. f |= BIT(NL80211_STA_FLAG_TDLS_PEER);
  3051. if (flags & WPA_STA_AUTHENTICATED)
  3052. f |= BIT(NL80211_STA_FLAG_AUTHENTICATED);
  3053. return f;
  3054. }
  3055. #ifdef CONFIG_MESH
  3056. static u32 sta_plink_state_nl80211(enum mesh_plink_state state)
  3057. {
  3058. switch (state) {
  3059. case PLINK_LISTEN:
  3060. return NL80211_PLINK_LISTEN;
  3061. case PLINK_OPEN_SENT:
  3062. return NL80211_PLINK_OPN_SNT;
  3063. case PLINK_OPEN_RCVD:
  3064. return NL80211_PLINK_OPN_RCVD;
  3065. case PLINK_CNF_RCVD:
  3066. return NL80211_PLINK_CNF_RCVD;
  3067. case PLINK_ESTAB:
  3068. return NL80211_PLINK_ESTAB;
  3069. case PLINK_HOLDING:
  3070. return NL80211_PLINK_HOLDING;
  3071. case PLINK_BLOCKED:
  3072. return NL80211_PLINK_BLOCKED;
  3073. default:
  3074. wpa_printf(MSG_ERROR, "nl80211: Invalid mesh plink state %d",
  3075. state);
  3076. }
  3077. return -1;
  3078. }
  3079. #endif /* CONFIG_MESH */
  3080. static int wpa_driver_nl80211_sta_add(void *priv,
  3081. struct hostapd_sta_add_params *params)
  3082. {
  3083. struct i802_bss *bss = priv;
  3084. struct wpa_driver_nl80211_data *drv = bss->drv;
  3085. struct nl_msg *msg;
  3086. struct nl80211_sta_flag_update upd;
  3087. int ret = -ENOBUFS;
  3088. if ((params->flags & WPA_STA_TDLS_PEER) &&
  3089. !(drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT))
  3090. return -EOPNOTSUPP;
  3091. wpa_printf(MSG_DEBUG, "nl80211: %s STA " MACSTR,
  3092. params->set ? "Set" : "Add", MAC2STR(params->addr));
  3093. msg = nl80211_bss_msg(bss, 0, params->set ? NL80211_CMD_SET_STATION :
  3094. NL80211_CMD_NEW_STATION);
  3095. if (!msg || nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, params->addr))
  3096. goto fail;
  3097. if (!params->set || (params->flags & WPA_STA_TDLS_PEER)) {
  3098. wpa_hexdump(MSG_DEBUG, " * supported rates",
  3099. params->supp_rates, params->supp_rates_len);
  3100. wpa_printf(MSG_DEBUG, " * capability=0x%x",
  3101. params->capability);
  3102. if (nla_put(msg, NL80211_ATTR_STA_SUPPORTED_RATES,
  3103. params->supp_rates_len, params->supp_rates) ||
  3104. nla_put_u16(msg, NL80211_ATTR_STA_CAPABILITY,
  3105. params->capability))
  3106. goto fail;
  3107. if (params->ht_capabilities) {
  3108. wpa_hexdump(MSG_DEBUG, " * ht_capabilities",
  3109. (u8 *) params->ht_capabilities,
  3110. sizeof(*params->ht_capabilities));
  3111. if (nla_put(msg, NL80211_ATTR_HT_CAPABILITY,
  3112. sizeof(*params->ht_capabilities),
  3113. params->ht_capabilities))
  3114. goto fail;
  3115. }
  3116. if (params->vht_capabilities) {
  3117. wpa_hexdump(MSG_DEBUG, " * vht_capabilities",
  3118. (u8 *) params->vht_capabilities,
  3119. sizeof(*params->vht_capabilities));
  3120. if (nla_put(msg, NL80211_ATTR_VHT_CAPABILITY,
  3121. sizeof(*params->vht_capabilities),
  3122. params->vht_capabilities))
  3123. goto fail;
  3124. }
  3125. if (params->ext_capab) {
  3126. wpa_hexdump(MSG_DEBUG, " * ext_capab",
  3127. params->ext_capab, params->ext_capab_len);
  3128. if (nla_put(msg, NL80211_ATTR_STA_EXT_CAPABILITY,
  3129. params->ext_capab_len, params->ext_capab))
  3130. goto fail;
  3131. }
  3132. }
  3133. if (!params->set) {
  3134. if (params->aid) {
  3135. wpa_printf(MSG_DEBUG, " * aid=%u", params->aid);
  3136. if (nla_put_u16(msg, NL80211_ATTR_STA_AID, params->aid))
  3137. goto fail;
  3138. } else {
  3139. /*
  3140. * cfg80211 validates that AID is non-zero, so we have
  3141. * to make this a non-zero value for the TDLS case where
  3142. * a dummy STA entry is used for now.
  3143. */
  3144. wpa_printf(MSG_DEBUG, " * aid=1 (TDLS workaround)");
  3145. if (nla_put_u16(msg, NL80211_ATTR_STA_AID, 1))
  3146. goto fail;
  3147. }
  3148. wpa_printf(MSG_DEBUG, " * listen_interval=%u",
  3149. params->listen_interval);
  3150. if (nla_put_u16(msg, NL80211_ATTR_STA_LISTEN_INTERVAL,
  3151. params->listen_interval))
  3152. goto fail;
  3153. } else if (params->aid && (params->flags & WPA_STA_TDLS_PEER)) {
  3154. wpa_printf(MSG_DEBUG, " * peer_aid=%u", params->aid);
  3155. if (nla_put_u16(msg, NL80211_ATTR_PEER_AID, params->aid))
  3156. goto fail;
  3157. }
  3158. if (params->vht_opmode_enabled) {
  3159. wpa_printf(MSG_DEBUG, " * opmode=%u", params->vht_opmode);
  3160. if (nla_put_u8(msg, NL80211_ATTR_OPMODE_NOTIF,
  3161. params->vht_opmode))
  3162. goto fail;
  3163. }
  3164. if (params->supp_channels) {
  3165. wpa_hexdump(MSG_DEBUG, " * supported channels",
  3166. params->supp_channels, params->supp_channels_len);
  3167. if (nla_put(msg, NL80211_ATTR_STA_SUPPORTED_CHANNELS,
  3168. params->supp_channels_len, params->supp_channels))
  3169. goto fail;
  3170. }
  3171. if (params->supp_oper_classes) {
  3172. wpa_hexdump(MSG_DEBUG, " * supported operating classes",
  3173. params->supp_oper_classes,
  3174. params->supp_oper_classes_len);
  3175. if (nla_put(msg, NL80211_ATTR_STA_SUPPORTED_OPER_CLASSES,
  3176. params->supp_oper_classes_len,
  3177. params->supp_oper_classes))
  3178. goto fail;
  3179. }
  3180. os_memset(&upd, 0, sizeof(upd));
  3181. upd.set = sta_flags_nl80211(params->flags);
  3182. upd.mask = upd.set | sta_flags_nl80211(params->flags_mask);
  3183. wpa_printf(MSG_DEBUG, " * flags set=0x%x mask=0x%x",
  3184. upd.set, upd.mask);
  3185. if (nla_put(msg, NL80211_ATTR_STA_FLAGS2, sizeof(upd), &upd))
  3186. goto fail;
  3187. #ifdef CONFIG_MESH
  3188. if (params->plink_state &&
  3189. nla_put_u8(msg, NL80211_ATTR_STA_PLINK_STATE,
  3190. sta_plink_state_nl80211(params->plink_state)))
  3191. goto fail;
  3192. #endif /* CONFIG_MESH */
  3193. if (params->flags & WPA_STA_WMM) {
  3194. struct nlattr *wme = nla_nest_start(msg, NL80211_ATTR_STA_WME);
  3195. wpa_printf(MSG_DEBUG, " * qosinfo=0x%x", params->qosinfo);
  3196. if (!wme ||
  3197. nla_put_u8(msg, NL80211_STA_WME_UAPSD_QUEUES,
  3198. params->qosinfo & WMM_QOSINFO_STA_AC_MASK) ||
  3199. nla_put_u8(msg, NL80211_STA_WME_MAX_SP,
  3200. (params->qosinfo >> WMM_QOSINFO_STA_SP_SHIFT) &
  3201. WMM_QOSINFO_STA_SP_MASK))
  3202. goto fail;
  3203. nla_nest_end(msg, wme);
  3204. }
  3205. ret = send_and_recv_msgs(drv, msg, NULL, NULL);
  3206. msg = NULL;
  3207. if (ret)
  3208. wpa_printf(MSG_DEBUG, "nl80211: NL80211_CMD_%s_STATION "
  3209. "result: %d (%s)", params->set ? "SET" : "NEW", ret,
  3210. strerror(-ret));
  3211. if (ret == -EEXIST)
  3212. ret = 0;
  3213. fail:
  3214. nlmsg_free(msg);
  3215. return ret;
  3216. }
  3217. static void rtnl_neigh_delete_fdb_entry(struct i802_bss *bss, const u8 *addr)
  3218. {
  3219. #ifdef CONFIG_LIBNL3_ROUTE
  3220. struct wpa_driver_nl80211_data *drv = bss->drv;
  3221. struct rtnl_neigh *rn;
  3222. struct nl_addr *nl_addr;
  3223. int err;
  3224. rn = rtnl_neigh_alloc();
  3225. if (!rn)
  3226. return;
  3227. rtnl_neigh_set_family(rn, AF_BRIDGE);
  3228. rtnl_neigh_set_ifindex(rn, bss->ifindex);
  3229. nl_addr = nl_addr_build(AF_BRIDGE, (void *) addr, ETH_ALEN);
  3230. if (!nl_addr) {
  3231. rtnl_neigh_put(rn);
  3232. return;
  3233. }
  3234. rtnl_neigh_set_lladdr(rn, nl_addr);
  3235. err = rtnl_neigh_delete(drv->rtnl_sk, rn, 0);
  3236. if (err < 0) {
  3237. wpa_printf(MSG_DEBUG, "nl80211: bridge FDB entry delete for "
  3238. MACSTR " ifindex=%d failed: %s", MAC2STR(addr),
  3239. bss->ifindex, nl_geterror(err));
  3240. } else {
  3241. wpa_printf(MSG_DEBUG, "nl80211: deleted bridge FDB entry for "
  3242. MACSTR, MAC2STR(addr));
  3243. }
  3244. nl_addr_put(nl_addr);
  3245. rtnl_neigh_put(rn);
  3246. #endif /* CONFIG_LIBNL3_ROUTE */
  3247. }
  3248. static int wpa_driver_nl80211_sta_remove(struct i802_bss *bss, const u8 *addr,
  3249. int deauth, u16 reason_code)
  3250. {
  3251. struct wpa_driver_nl80211_data *drv = bss->drv;
  3252. struct nl_msg *msg;
  3253. int ret;
  3254. if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_DEL_STATION)) ||
  3255. nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr) ||
  3256. (deauth == 0 &&
  3257. nla_put_u8(msg, NL80211_ATTR_MGMT_SUBTYPE,
  3258. WLAN_FC_STYPE_DISASSOC)) ||
  3259. (deauth == 1 &&
  3260. nla_put_u8(msg, NL80211_ATTR_MGMT_SUBTYPE,
  3261. WLAN_FC_STYPE_DEAUTH)) ||
  3262. (reason_code &&
  3263. nla_put_u16(msg, NL80211_ATTR_REASON_CODE, reason_code))) {
  3264. nlmsg_free(msg);
  3265. return -ENOBUFS;
  3266. }
  3267. ret = send_and_recv_msgs(drv, msg, NULL, NULL);
  3268. wpa_printf(MSG_DEBUG, "nl80211: sta_remove -> DEL_STATION %s " MACSTR
  3269. " --> %d (%s)",
  3270. bss->ifname, MAC2STR(addr), ret, strerror(-ret));
  3271. if (drv->rtnl_sk)
  3272. rtnl_neigh_delete_fdb_entry(bss, addr);
  3273. if (ret == -ENOENT)
  3274. return 0;
  3275. return ret;
  3276. }
  3277. void nl80211_remove_iface(struct wpa_driver_nl80211_data *drv, int ifidx)
  3278. {
  3279. struct nl_msg *msg;
  3280. struct wpa_driver_nl80211_data *drv2;
  3281. wpa_printf(MSG_DEBUG, "nl80211: Remove interface ifindex=%d", ifidx);
  3282. /* stop listening for EAPOL on this interface */
  3283. dl_list_for_each(drv2, &drv->global->interfaces,
  3284. struct wpa_driver_nl80211_data, list)
  3285. del_ifidx(drv2, ifidx);
  3286. msg = nl80211_ifindex_msg(drv, ifidx, 0, NL80211_CMD_DEL_INTERFACE);
  3287. if (send_and_recv_msgs(drv, msg, NULL, NULL) == 0)
  3288. return;
  3289. wpa_printf(MSG_ERROR, "Failed to remove interface (ifidx=%d)", ifidx);
  3290. }
  3291. static const char * nl80211_iftype_str(enum nl80211_iftype mode)
  3292. {
  3293. switch (mode) {
  3294. case NL80211_IFTYPE_ADHOC:
  3295. return "ADHOC";
  3296. case NL80211_IFTYPE_STATION:
  3297. return "STATION";
  3298. case NL80211_IFTYPE_AP:
  3299. return "AP";
  3300. case NL80211_IFTYPE_AP_VLAN:
  3301. return "AP_VLAN";
  3302. case NL80211_IFTYPE_WDS:
  3303. return "WDS";
  3304. case NL80211_IFTYPE_MONITOR:
  3305. return "MONITOR";
  3306. case NL80211_IFTYPE_MESH_POINT:
  3307. return "MESH_POINT";
  3308. case NL80211_IFTYPE_P2P_CLIENT:
  3309. return "P2P_CLIENT";
  3310. case NL80211_IFTYPE_P2P_GO:
  3311. return "P2P_GO";
  3312. case NL80211_IFTYPE_P2P_DEVICE:
  3313. return "P2P_DEVICE";
  3314. default:
  3315. return "unknown";
  3316. }
  3317. }
  3318. static int nl80211_create_iface_once(struct wpa_driver_nl80211_data *drv,
  3319. const char *ifname,
  3320. enum nl80211_iftype iftype,
  3321. const u8 *addr, int wds,
  3322. int (*handler)(struct nl_msg *, void *),
  3323. void *arg)
  3324. {
  3325. struct nl_msg *msg;
  3326. int ifidx;
  3327. int ret = -ENOBUFS;
  3328. wpa_printf(MSG_DEBUG, "nl80211: Create interface iftype %d (%s)",
  3329. iftype, nl80211_iftype_str(iftype));
  3330. msg = nl80211_cmd_msg(drv->first_bss, 0, NL80211_CMD_NEW_INTERFACE);
  3331. if (!msg ||
  3332. nla_put_string(msg, NL80211_ATTR_IFNAME, ifname) ||
  3333. nla_put_u32(msg, NL80211_ATTR_IFTYPE, iftype))
  3334. goto fail;
  3335. if (iftype == NL80211_IFTYPE_MONITOR) {
  3336. struct nlattr *flags;
  3337. flags = nla_nest_start(msg, NL80211_ATTR_MNTR_FLAGS);
  3338. if (!flags ||
  3339. nla_put_flag(msg, NL80211_MNTR_FLAG_COOK_FRAMES))
  3340. goto fail;
  3341. nla_nest_end(msg, flags);
  3342. } else if (wds) {
  3343. if (nla_put_u8(msg, NL80211_ATTR_4ADDR, wds))
  3344. goto fail;
  3345. }
  3346. /*
  3347. * Tell cfg80211 that the interface belongs to the socket that created
  3348. * it, and the interface should be deleted when the socket is closed.
  3349. */
  3350. if (nla_put_flag(msg, NL80211_ATTR_IFACE_SOCKET_OWNER))
  3351. goto fail;
  3352. ret = send_and_recv_msgs(drv, msg, handler, arg);
  3353. msg = NULL;
  3354. if (ret) {
  3355. fail:
  3356. nlmsg_free(msg);
  3357. wpa_printf(MSG_ERROR, "Failed to create interface %s: %d (%s)",
  3358. ifname, ret, strerror(-ret));
  3359. return ret;
  3360. }
  3361. if (iftype == NL80211_IFTYPE_P2P_DEVICE)
  3362. return 0;
  3363. ifidx = if_nametoindex(ifname);
  3364. wpa_printf(MSG_DEBUG, "nl80211: New interface %s created: ifindex=%d",
  3365. ifname, ifidx);
  3366. if (ifidx <= 0)
  3367. return -1;
  3368. /*
  3369. * Some virtual interfaces need to process EAPOL packets and events on
  3370. * the parent interface. This is used mainly with hostapd.
  3371. */
  3372. if (drv->hostapd ||
  3373. iftype == NL80211_IFTYPE_AP_VLAN ||
  3374. iftype == NL80211_IFTYPE_WDS ||
  3375. iftype == NL80211_IFTYPE_MONITOR) {
  3376. /* start listening for EAPOL on this interface */
  3377. add_ifidx(drv, ifidx);
  3378. }
  3379. if (addr && iftype != NL80211_IFTYPE_MONITOR &&
  3380. linux_set_ifhwaddr(drv->global->ioctl_sock, ifname, addr)) {
  3381. nl80211_remove_iface(drv, ifidx);
  3382. return -1;
  3383. }
  3384. return ifidx;
  3385. }
  3386. int nl80211_create_iface(struct wpa_driver_nl80211_data *drv,
  3387. const char *ifname, enum nl80211_iftype iftype,
  3388. const u8 *addr, int wds,
  3389. int (*handler)(struct nl_msg *, void *),
  3390. void *arg, int use_existing)
  3391. {
  3392. int ret;
  3393. ret = nl80211_create_iface_once(drv, ifname, iftype, addr, wds, handler,
  3394. arg);
  3395. /* if error occurred and interface exists already */
  3396. if (ret == -ENFILE && if_nametoindex(ifname)) {
  3397. if (use_existing) {
  3398. wpa_printf(MSG_DEBUG, "nl80211: Continue using existing interface %s",
  3399. ifname);
  3400. if (addr && iftype != NL80211_IFTYPE_MONITOR &&
  3401. linux_set_ifhwaddr(drv->global->ioctl_sock, ifname,
  3402. addr) < 0 &&
  3403. (linux_set_iface_flags(drv->global->ioctl_sock,
  3404. ifname, 0) < 0 ||
  3405. linux_set_ifhwaddr(drv->global->ioctl_sock, ifname,
  3406. addr) < 0 ||
  3407. linux_set_iface_flags(drv->global->ioctl_sock,
  3408. ifname, 1) < 0))
  3409. return -1;
  3410. return -ENFILE;
  3411. }
  3412. wpa_printf(MSG_INFO, "Try to remove and re-create %s", ifname);
  3413. /* Try to remove the interface that was already there. */
  3414. nl80211_remove_iface(drv, if_nametoindex(ifname));
  3415. /* Try to create the interface again */
  3416. ret = nl80211_create_iface_once(drv, ifname, iftype, addr,
  3417. wds, handler, arg);
  3418. }
  3419. if (ret >= 0 && is_p2p_net_interface(iftype)) {
  3420. wpa_printf(MSG_DEBUG,
  3421. "nl80211: Interface %s created for P2P - disable 11b rates",
  3422. ifname);
  3423. nl80211_disable_11b_rates(drv, ret, 1);
  3424. }
  3425. return ret;
  3426. }
  3427. static int nl80211_setup_ap(struct i802_bss *bss)
  3428. {
  3429. struct wpa_driver_nl80211_data *drv = bss->drv;
  3430. wpa_printf(MSG_DEBUG, "nl80211: Setup AP(%s) - device_ap_sme=%d use_monitor=%d",
  3431. bss->ifname, drv->device_ap_sme, drv->use_monitor);
  3432. /*
  3433. * Disable Probe Request reporting unless we need it in this way for
  3434. * devices that include the AP SME, in the other case (unless using
  3435. * monitor iface) we'll get it through the nl_mgmt socket instead.
  3436. */
  3437. if (!drv->device_ap_sme)
  3438. wpa_driver_nl80211_probe_req_report(bss, 0);
  3439. if (!drv->device_ap_sme && !drv->use_monitor)
  3440. if (nl80211_mgmt_subscribe_ap(bss))
  3441. return -1;
  3442. if (drv->device_ap_sme && !drv->use_monitor)
  3443. if (nl80211_mgmt_subscribe_ap_dev_sme(bss))
  3444. return -1;
  3445. if (!drv->device_ap_sme && drv->use_monitor &&
  3446. nl80211_create_monitor_interface(drv) &&
  3447. !drv->device_ap_sme)
  3448. return -1;
  3449. if (drv->device_ap_sme &&
  3450. wpa_driver_nl80211_probe_req_report(bss, 1) < 0) {
  3451. wpa_printf(MSG_DEBUG, "nl80211: Failed to enable "
  3452. "Probe Request frame reporting in AP mode");
  3453. /* Try to survive without this */
  3454. }
  3455. return 0;
  3456. }
  3457. static void nl80211_teardown_ap(struct i802_bss *bss)
  3458. {
  3459. struct wpa_driver_nl80211_data *drv = bss->drv;
  3460. wpa_printf(MSG_DEBUG, "nl80211: Teardown AP(%s) - device_ap_sme=%d use_monitor=%d",
  3461. bss->ifname, drv->device_ap_sme, drv->use_monitor);
  3462. if (drv->device_ap_sme) {
  3463. wpa_driver_nl80211_probe_req_report(bss, 0);
  3464. if (!drv->use_monitor)
  3465. nl80211_mgmt_unsubscribe(bss, "AP teardown (dev SME)");
  3466. } else if (drv->use_monitor)
  3467. nl80211_remove_monitor_interface(drv);
  3468. else
  3469. nl80211_mgmt_unsubscribe(bss, "AP teardown");
  3470. bss->beacon_set = 0;
  3471. }
  3472. static int nl80211_send_eapol_data(struct i802_bss *bss,
  3473. const u8 *addr, const u8 *data,
  3474. size_t data_len)
  3475. {
  3476. struct sockaddr_ll ll;
  3477. int ret;
  3478. if (bss->drv->eapol_tx_sock < 0) {
  3479. wpa_printf(MSG_DEBUG, "nl80211: No socket to send EAPOL");
  3480. return -1;
  3481. }
  3482. os_memset(&ll, 0, sizeof(ll));
  3483. ll.sll_family = AF_PACKET;
  3484. ll.sll_ifindex = bss->ifindex;
  3485. ll.sll_protocol = htons(ETH_P_PAE);
  3486. ll.sll_halen = ETH_ALEN;
  3487. os_memcpy(ll.sll_addr, addr, ETH_ALEN);
  3488. ret = sendto(bss->drv->eapol_tx_sock, data, data_len, 0,
  3489. (struct sockaddr *) &ll, sizeof(ll));
  3490. if (ret < 0)
  3491. wpa_printf(MSG_ERROR, "nl80211: EAPOL TX: %s",
  3492. strerror(errno));
  3493. return ret;
  3494. }
  3495. static const u8 rfc1042_header[6] = { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00 };
  3496. static int wpa_driver_nl80211_hapd_send_eapol(
  3497. void *priv, const u8 *addr, const u8 *data,
  3498. size_t data_len, int encrypt, const u8 *own_addr, u32 flags)
  3499. {
  3500. struct i802_bss *bss = priv;
  3501. struct wpa_driver_nl80211_data *drv = bss->drv;
  3502. struct ieee80211_hdr *hdr;
  3503. size_t len;
  3504. u8 *pos;
  3505. int res;
  3506. int qos = flags & WPA_STA_WMM;
  3507. if (drv->device_ap_sme || !drv->use_monitor)
  3508. return nl80211_send_eapol_data(bss, addr, data, data_len);
  3509. len = sizeof(*hdr) + (qos ? 2 : 0) + sizeof(rfc1042_header) + 2 +
  3510. data_len;
  3511. hdr = os_zalloc(len);
  3512. if (hdr == NULL) {
  3513. wpa_printf(MSG_INFO, "nl80211: Failed to allocate EAPOL buffer(len=%lu)",
  3514. (unsigned long) len);
  3515. return -1;
  3516. }
  3517. hdr->frame_control =
  3518. IEEE80211_FC(WLAN_FC_TYPE_DATA, WLAN_FC_STYPE_DATA);
  3519. hdr->frame_control |= host_to_le16(WLAN_FC_FROMDS);
  3520. if (encrypt)
  3521. hdr->frame_control |= host_to_le16(WLAN_FC_ISWEP);
  3522. if (qos) {
  3523. hdr->frame_control |=
  3524. host_to_le16(WLAN_FC_STYPE_QOS_DATA << 4);
  3525. }
  3526. memcpy(hdr->IEEE80211_DA_FROMDS, addr, ETH_ALEN);
  3527. memcpy(hdr->IEEE80211_BSSID_FROMDS, own_addr, ETH_ALEN);
  3528. memcpy(hdr->IEEE80211_SA_FROMDS, own_addr, ETH_ALEN);
  3529. pos = (u8 *) (hdr + 1);
  3530. if (qos) {
  3531. /* Set highest priority in QoS header */
  3532. pos[0] = 7;
  3533. pos[1] = 0;
  3534. pos += 2;
  3535. }
  3536. memcpy(pos, rfc1042_header, sizeof(rfc1042_header));
  3537. pos += sizeof(rfc1042_header);
  3538. WPA_PUT_BE16(pos, ETH_P_PAE);
  3539. pos += 2;
  3540. memcpy(pos, data, data_len);
  3541. res = wpa_driver_nl80211_send_frame(bss, (u8 *) hdr, len, encrypt, 0,
  3542. 0, 0, 0, 0);
  3543. if (res < 0) {
  3544. wpa_printf(MSG_ERROR, "i802_send_eapol - packet len: %lu - "
  3545. "failed: %d (%s)",
  3546. (unsigned long) len, errno, strerror(errno));
  3547. }
  3548. os_free(hdr);
  3549. return res;
  3550. }
  3551. static int wpa_driver_nl80211_sta_set_flags(void *priv, const u8 *addr,
  3552. int total_flags,
  3553. int flags_or, int flags_and)
  3554. {
  3555. struct i802_bss *bss = priv;
  3556. struct nl_msg *msg;
  3557. struct nlattr *flags;
  3558. struct nl80211_sta_flag_update upd;
  3559. wpa_printf(MSG_DEBUG, "nl80211: Set STA flags - ifname=%s addr=" MACSTR
  3560. " total_flags=0x%x flags_or=0x%x flags_and=0x%x authorized=%d",
  3561. bss->ifname, MAC2STR(addr), total_flags, flags_or, flags_and,
  3562. !!(total_flags & WPA_STA_AUTHORIZED));
  3563. if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_SET_STATION)) ||
  3564. nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr))
  3565. goto fail;
  3566. /*
  3567. * Backwards compatibility version using NL80211_ATTR_STA_FLAGS. This
  3568. * can be removed eventually.
  3569. */
  3570. flags = nla_nest_start(msg, NL80211_ATTR_STA_FLAGS);
  3571. if (!flags ||
  3572. ((total_flags & WPA_STA_AUTHORIZED) &&
  3573. nla_put_flag(msg, NL80211_STA_FLAG_AUTHORIZED)) ||
  3574. ((total_flags & WPA_STA_WMM) &&
  3575. nla_put_flag(msg, NL80211_STA_FLAG_WME)) ||
  3576. ((total_flags & WPA_STA_SHORT_PREAMBLE) &&
  3577. nla_put_flag(msg, NL80211_STA_FLAG_SHORT_PREAMBLE)) ||
  3578. ((total_flags & WPA_STA_MFP) &&
  3579. nla_put_flag(msg, NL80211_STA_FLAG_MFP)) ||
  3580. ((total_flags & WPA_STA_TDLS_PEER) &&
  3581. nla_put_flag(msg, NL80211_STA_FLAG_TDLS_PEER)))
  3582. goto fail;
  3583. nla_nest_end(msg, flags);
  3584. os_memset(&upd, 0, sizeof(upd));
  3585. upd.mask = sta_flags_nl80211(flags_or | ~flags_and);
  3586. upd.set = sta_flags_nl80211(flags_or);
  3587. if (nla_put(msg, NL80211_ATTR_STA_FLAGS2, sizeof(upd), &upd))
  3588. goto fail;
  3589. return send_and_recv_msgs(bss->drv, msg, NULL, NULL);
  3590. fail:
  3591. nlmsg_free(msg);
  3592. return -ENOBUFS;
  3593. }
  3594. static int wpa_driver_nl80211_ap(struct wpa_driver_nl80211_data *drv,
  3595. struct wpa_driver_associate_params *params)
  3596. {
  3597. enum nl80211_iftype nlmode, old_mode;
  3598. if (params->p2p) {
  3599. wpa_printf(MSG_DEBUG, "nl80211: Setup AP operations for P2P "
  3600. "group (GO)");
  3601. nlmode = NL80211_IFTYPE_P2P_GO;
  3602. } else
  3603. nlmode = NL80211_IFTYPE_AP;
  3604. old_mode = drv->nlmode;
  3605. if (wpa_driver_nl80211_set_mode(drv->first_bss, nlmode)) {
  3606. nl80211_remove_monitor_interface(drv);
  3607. return -1;
  3608. }
  3609. if (params->freq.freq &&
  3610. nl80211_set_channel(drv->first_bss, &params->freq, 0)) {
  3611. if (old_mode != nlmode)
  3612. wpa_driver_nl80211_set_mode(drv->first_bss, old_mode);
  3613. nl80211_remove_monitor_interface(drv);
  3614. return -1;
  3615. }
  3616. return 0;
  3617. }
  3618. static int nl80211_leave_ibss(struct wpa_driver_nl80211_data *drv,
  3619. int reset_mode)
  3620. {
  3621. struct nl_msg *msg;
  3622. int ret;
  3623. msg = nl80211_drv_msg(drv, 0, NL80211_CMD_LEAVE_IBSS);
  3624. ret = send_and_recv_msgs(drv, msg, NULL, NULL);
  3625. if (ret) {
  3626. wpa_printf(MSG_DEBUG, "nl80211: Leave IBSS failed: ret=%d "
  3627. "(%s)", ret, strerror(-ret));
  3628. } else {
  3629. wpa_printf(MSG_DEBUG,
  3630. "nl80211: Leave IBSS request sent successfully");
  3631. }
  3632. if (reset_mode &&
  3633. wpa_driver_nl80211_set_mode(drv->first_bss,
  3634. NL80211_IFTYPE_STATION)) {
  3635. wpa_printf(MSG_INFO, "nl80211: Failed to set interface into "
  3636. "station mode");
  3637. }
  3638. return ret;
  3639. }
  3640. static int nl80211_ht_vht_overrides(struct nl_msg *msg,
  3641. struct wpa_driver_associate_params *params)
  3642. {
  3643. if (params->disable_ht && nla_put_flag(msg, NL80211_ATTR_DISABLE_HT))
  3644. return -1;
  3645. if (params->htcaps && params->htcaps_mask) {
  3646. int sz = sizeof(struct ieee80211_ht_capabilities);
  3647. wpa_hexdump(MSG_DEBUG, " * htcaps", params->htcaps, sz);
  3648. wpa_hexdump(MSG_DEBUG, " * htcaps_mask",
  3649. params->htcaps_mask, sz);
  3650. if (nla_put(msg, NL80211_ATTR_HT_CAPABILITY, sz,
  3651. params->htcaps) ||
  3652. nla_put(msg, NL80211_ATTR_HT_CAPABILITY_MASK, sz,
  3653. params->htcaps_mask))
  3654. return -1;
  3655. }
  3656. #ifdef CONFIG_VHT_OVERRIDES
  3657. if (params->disable_vht) {
  3658. wpa_printf(MSG_DEBUG, " * VHT disabled");
  3659. if (nla_put_flag(msg, NL80211_ATTR_DISABLE_VHT))
  3660. return -1;
  3661. }
  3662. if (params->vhtcaps && params->vhtcaps_mask) {
  3663. int sz = sizeof(struct ieee80211_vht_capabilities);
  3664. wpa_hexdump(MSG_DEBUG, " * vhtcaps", params->vhtcaps, sz);
  3665. wpa_hexdump(MSG_DEBUG, " * vhtcaps_mask",
  3666. params->vhtcaps_mask, sz);
  3667. if (nla_put(msg, NL80211_ATTR_VHT_CAPABILITY, sz,
  3668. params->vhtcaps) ||
  3669. nla_put(msg, NL80211_ATTR_VHT_CAPABILITY_MASK, sz,
  3670. params->vhtcaps_mask))
  3671. return -1;
  3672. }
  3673. #endif /* CONFIG_VHT_OVERRIDES */
  3674. return 0;
  3675. }
  3676. static int wpa_driver_nl80211_ibss(struct wpa_driver_nl80211_data *drv,
  3677. struct wpa_driver_associate_params *params)
  3678. {
  3679. struct nl_msg *msg;
  3680. int ret = -1;
  3681. int count = 0;
  3682. wpa_printf(MSG_DEBUG, "nl80211: Join IBSS (ifindex=%d)", drv->ifindex);
  3683. if (wpa_driver_nl80211_set_mode_ibss(drv->first_bss, &params->freq)) {
  3684. wpa_printf(MSG_INFO, "nl80211: Failed to set interface into "
  3685. "IBSS mode");
  3686. return -1;
  3687. }
  3688. retry:
  3689. if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_JOIN_IBSS)) ||
  3690. params->ssid == NULL || params->ssid_len > sizeof(drv->ssid))
  3691. goto fail;
  3692. wpa_hexdump_ascii(MSG_DEBUG, " * SSID",
  3693. params->ssid, params->ssid_len);
  3694. if (nla_put(msg, NL80211_ATTR_SSID, params->ssid_len, params->ssid))
  3695. goto fail;
  3696. os_memcpy(drv->ssid, params->ssid, params->ssid_len);
  3697. drv->ssid_len = params->ssid_len;
  3698. if (nl80211_put_freq_params(msg, &params->freq) < 0 ||
  3699. nl80211_put_beacon_int(msg, params->beacon_int))
  3700. goto fail;
  3701. ret = nl80211_set_conn_keys(params, msg);
  3702. if (ret)
  3703. goto fail;
  3704. if (params->bssid && params->fixed_bssid) {
  3705. wpa_printf(MSG_DEBUG, " * BSSID=" MACSTR,
  3706. MAC2STR(params->bssid));
  3707. if (nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, params->bssid))
  3708. goto fail;
  3709. }
  3710. if (params->fixed_freq) {
  3711. wpa_printf(MSG_DEBUG, " * fixed_freq");
  3712. if (nla_put_flag(msg, NL80211_ATTR_FREQ_FIXED))
  3713. goto fail;
  3714. }
  3715. if (params->key_mgmt_suite == WPA_KEY_MGMT_IEEE8021X ||
  3716. params->key_mgmt_suite == WPA_KEY_MGMT_PSK ||
  3717. params->key_mgmt_suite == WPA_KEY_MGMT_IEEE8021X_SHA256 ||
  3718. params->key_mgmt_suite == WPA_KEY_MGMT_PSK_SHA256) {
  3719. wpa_printf(MSG_DEBUG, " * control port");
  3720. if (nla_put_flag(msg, NL80211_ATTR_CONTROL_PORT))
  3721. goto fail;
  3722. }
  3723. if (params->wpa_ie) {
  3724. wpa_hexdump(MSG_DEBUG,
  3725. " * Extra IEs for Beacon/Probe Response frames",
  3726. params->wpa_ie, params->wpa_ie_len);
  3727. if (nla_put(msg, NL80211_ATTR_IE, params->wpa_ie_len,
  3728. params->wpa_ie))
  3729. goto fail;
  3730. }
  3731. if (nl80211_ht_vht_overrides(msg, params) < 0)
  3732. return -1;
  3733. ret = send_and_recv_msgs(drv, msg, NULL, NULL);
  3734. msg = NULL;
  3735. if (ret) {
  3736. wpa_printf(MSG_DEBUG, "nl80211: Join IBSS failed: ret=%d (%s)",
  3737. ret, strerror(-ret));
  3738. count++;
  3739. if (ret == -EALREADY && count == 1) {
  3740. wpa_printf(MSG_DEBUG, "nl80211: Retry IBSS join after "
  3741. "forced leave");
  3742. nl80211_leave_ibss(drv, 0);
  3743. nlmsg_free(msg);
  3744. goto retry;
  3745. }
  3746. } else {
  3747. wpa_printf(MSG_DEBUG,
  3748. "nl80211: Join IBSS request sent successfully");
  3749. }
  3750. fail:
  3751. nlmsg_free(msg);
  3752. return ret;
  3753. }
  3754. static int nl80211_connect_common(struct wpa_driver_nl80211_data *drv,
  3755. struct wpa_driver_associate_params *params,
  3756. struct nl_msg *msg)
  3757. {
  3758. if (params->bssid) {
  3759. wpa_printf(MSG_DEBUG, " * bssid=" MACSTR,
  3760. MAC2STR(params->bssid));
  3761. if (nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, params->bssid))
  3762. return -1;
  3763. }
  3764. if (params->bssid_hint) {
  3765. wpa_printf(MSG_DEBUG, " * bssid_hint=" MACSTR,
  3766. MAC2STR(params->bssid_hint));
  3767. if (nla_put(msg, NL80211_ATTR_MAC_HINT, ETH_ALEN,
  3768. params->bssid_hint))
  3769. return -1;
  3770. }
  3771. if (params->freq.freq) {
  3772. wpa_printf(MSG_DEBUG, " * freq=%d", params->freq.freq);
  3773. if (nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ,
  3774. params->freq.freq))
  3775. return -1;
  3776. drv->assoc_freq = params->freq.freq;
  3777. } else
  3778. drv->assoc_freq = 0;
  3779. if (params->freq_hint) {
  3780. wpa_printf(MSG_DEBUG, " * freq_hint=%d", params->freq_hint);
  3781. if (nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ_HINT,
  3782. params->freq_hint))
  3783. return -1;
  3784. }
  3785. if (params->bg_scan_period >= 0) {
  3786. wpa_printf(MSG_DEBUG, " * bg scan period=%d",
  3787. params->bg_scan_period);
  3788. if (nla_put_u16(msg, NL80211_ATTR_BG_SCAN_PERIOD,
  3789. params->bg_scan_period))
  3790. return -1;
  3791. }
  3792. if (params->ssid) {
  3793. wpa_hexdump_ascii(MSG_DEBUG, " * SSID",
  3794. params->ssid, params->ssid_len);
  3795. if (nla_put(msg, NL80211_ATTR_SSID, params->ssid_len,
  3796. params->ssid))
  3797. return -1;
  3798. if (params->ssid_len > sizeof(drv->ssid))
  3799. return -1;
  3800. os_memcpy(drv->ssid, params->ssid, params->ssid_len);
  3801. drv->ssid_len = params->ssid_len;
  3802. }
  3803. wpa_hexdump(MSG_DEBUG, " * IEs", params->wpa_ie, params->wpa_ie_len);
  3804. if (params->wpa_ie &&
  3805. nla_put(msg, NL80211_ATTR_IE, params->wpa_ie_len, params->wpa_ie))
  3806. return -1;
  3807. if (params->wpa_proto) {
  3808. enum nl80211_wpa_versions ver = 0;
  3809. if (params->wpa_proto & WPA_PROTO_WPA)
  3810. ver |= NL80211_WPA_VERSION_1;
  3811. if (params->wpa_proto & WPA_PROTO_RSN)
  3812. ver |= NL80211_WPA_VERSION_2;
  3813. wpa_printf(MSG_DEBUG, " * WPA Versions 0x%x", ver);
  3814. if (nla_put_u32(msg, NL80211_ATTR_WPA_VERSIONS, ver))
  3815. return -1;
  3816. }
  3817. if (params->pairwise_suite != WPA_CIPHER_NONE) {
  3818. u32 cipher = wpa_cipher_to_cipher_suite(params->pairwise_suite);
  3819. wpa_printf(MSG_DEBUG, " * pairwise=0x%x", cipher);
  3820. if (nla_put_u32(msg, NL80211_ATTR_CIPHER_SUITES_PAIRWISE,
  3821. cipher))
  3822. return -1;
  3823. }
  3824. if (params->group_suite == WPA_CIPHER_GTK_NOT_USED &&
  3825. !(drv->capa.enc & WPA_DRIVER_CAPA_ENC_GTK_NOT_USED)) {
  3826. /*
  3827. * This is likely to work even though many drivers do not
  3828. * advertise support for operations without GTK.
  3829. */
  3830. wpa_printf(MSG_DEBUG, " * skip group cipher configuration for GTK_NOT_USED due to missing driver support advertisement");
  3831. } else if (params->group_suite != WPA_CIPHER_NONE) {
  3832. u32 cipher = wpa_cipher_to_cipher_suite(params->group_suite);
  3833. wpa_printf(MSG_DEBUG, " * group=0x%x", cipher);
  3834. if (nla_put_u32(msg, NL80211_ATTR_CIPHER_SUITE_GROUP, cipher))
  3835. return -1;
  3836. }
  3837. if (params->key_mgmt_suite == WPA_KEY_MGMT_IEEE8021X ||
  3838. params->key_mgmt_suite == WPA_KEY_MGMT_PSK ||
  3839. params->key_mgmt_suite == WPA_KEY_MGMT_FT_IEEE8021X ||
  3840. params->key_mgmt_suite == WPA_KEY_MGMT_FT_PSK ||
  3841. params->key_mgmt_suite == WPA_KEY_MGMT_CCKM ||
  3842. params->key_mgmt_suite == WPA_KEY_MGMT_OSEN ||
  3843. params->key_mgmt_suite == WPA_KEY_MGMT_IEEE8021X_SHA256 ||
  3844. params->key_mgmt_suite == WPA_KEY_MGMT_PSK_SHA256 ||
  3845. params->key_mgmt_suite == WPA_KEY_MGMT_IEEE8021X_SUITE_B ||
  3846. params->key_mgmt_suite == WPA_KEY_MGMT_IEEE8021X_SUITE_B_192) {
  3847. int mgmt = WLAN_AKM_SUITE_PSK;
  3848. switch (params->key_mgmt_suite) {
  3849. case WPA_KEY_MGMT_CCKM:
  3850. mgmt = WLAN_AKM_SUITE_CCKM;
  3851. break;
  3852. case WPA_KEY_MGMT_IEEE8021X:
  3853. mgmt = WLAN_AKM_SUITE_8021X;
  3854. break;
  3855. case WPA_KEY_MGMT_FT_IEEE8021X:
  3856. mgmt = WLAN_AKM_SUITE_FT_8021X;
  3857. break;
  3858. case WPA_KEY_MGMT_FT_PSK:
  3859. mgmt = WLAN_AKM_SUITE_FT_PSK;
  3860. break;
  3861. case WPA_KEY_MGMT_IEEE8021X_SHA256:
  3862. mgmt = WLAN_AKM_SUITE_8021X_SHA256;
  3863. break;
  3864. case WPA_KEY_MGMT_PSK_SHA256:
  3865. mgmt = WLAN_AKM_SUITE_PSK_SHA256;
  3866. break;
  3867. case WPA_KEY_MGMT_OSEN:
  3868. mgmt = WLAN_AKM_SUITE_OSEN;
  3869. break;
  3870. case WPA_KEY_MGMT_IEEE8021X_SUITE_B:
  3871. mgmt = WLAN_AKM_SUITE_8021X_SUITE_B;
  3872. break;
  3873. case WPA_KEY_MGMT_IEEE8021X_SUITE_B_192:
  3874. mgmt = WLAN_AKM_SUITE_8021X_SUITE_B_192;
  3875. break;
  3876. case WPA_KEY_MGMT_PSK:
  3877. default:
  3878. mgmt = WLAN_AKM_SUITE_PSK;
  3879. break;
  3880. }
  3881. wpa_printf(MSG_DEBUG, " * akm=0x%x", mgmt);
  3882. if (nla_put_u32(msg, NL80211_ATTR_AKM_SUITES, mgmt))
  3883. return -1;
  3884. }
  3885. if (nla_put_flag(msg, NL80211_ATTR_CONTROL_PORT))
  3886. return -1;
  3887. if (params->mgmt_frame_protection == MGMT_FRAME_PROTECTION_REQUIRED &&
  3888. nla_put_u32(msg, NL80211_ATTR_USE_MFP, NL80211_MFP_REQUIRED))
  3889. return -1;
  3890. if (params->rrm_used) {
  3891. u32 drv_rrm_flags = drv->capa.rrm_flags;
  3892. if (!(drv_rrm_flags &
  3893. WPA_DRIVER_FLAGS_DS_PARAM_SET_IE_IN_PROBES) ||
  3894. !(drv_rrm_flags & WPA_DRIVER_FLAGS_QUIET) ||
  3895. nla_put_flag(msg, NL80211_ATTR_USE_RRM))
  3896. return -1;
  3897. }
  3898. if (nl80211_ht_vht_overrides(msg, params) < 0)
  3899. return -1;
  3900. if (params->p2p)
  3901. wpa_printf(MSG_DEBUG, " * P2P group");
  3902. return 0;
  3903. }
  3904. static int wpa_driver_nl80211_try_connect(
  3905. struct wpa_driver_nl80211_data *drv,
  3906. struct wpa_driver_associate_params *params)
  3907. {
  3908. struct nl_msg *msg;
  3909. enum nl80211_auth_type type;
  3910. int ret;
  3911. int algs;
  3912. if (params->req_key_mgmt_offload && params->psk &&
  3913. (params->key_mgmt_suite == WPA_KEY_MGMT_PSK ||
  3914. params->key_mgmt_suite == WPA_KEY_MGMT_PSK_SHA256 ||
  3915. params->key_mgmt_suite == WPA_KEY_MGMT_FT_PSK)) {
  3916. wpa_printf(MSG_DEBUG, "nl80211: Key management set PSK");
  3917. ret = issue_key_mgmt_set_key(drv, params->psk, 32);
  3918. if (ret)
  3919. return ret;
  3920. }
  3921. wpa_printf(MSG_DEBUG, "nl80211: Connect (ifindex=%d)", drv->ifindex);
  3922. msg = nl80211_drv_msg(drv, 0, NL80211_CMD_CONNECT);
  3923. if (!msg)
  3924. return -1;
  3925. ret = nl80211_connect_common(drv, params, msg);
  3926. if (ret)
  3927. goto fail;
  3928. algs = 0;
  3929. if (params->auth_alg & WPA_AUTH_ALG_OPEN)
  3930. algs++;
  3931. if (params->auth_alg & WPA_AUTH_ALG_SHARED)
  3932. algs++;
  3933. if (params->auth_alg & WPA_AUTH_ALG_LEAP)
  3934. algs++;
  3935. if (algs > 1) {
  3936. wpa_printf(MSG_DEBUG, " * Leave out Auth Type for automatic "
  3937. "selection");
  3938. goto skip_auth_type;
  3939. }
  3940. if (params->auth_alg & WPA_AUTH_ALG_OPEN)
  3941. type = NL80211_AUTHTYPE_OPEN_SYSTEM;
  3942. else if (params->auth_alg & WPA_AUTH_ALG_SHARED)
  3943. type = NL80211_AUTHTYPE_SHARED_KEY;
  3944. else if (params->auth_alg & WPA_AUTH_ALG_LEAP)
  3945. type = NL80211_AUTHTYPE_NETWORK_EAP;
  3946. else if (params->auth_alg & WPA_AUTH_ALG_FT)
  3947. type = NL80211_AUTHTYPE_FT;
  3948. else
  3949. goto fail;
  3950. wpa_printf(MSG_DEBUG, " * Auth Type %d", type);
  3951. if (nla_put_u32(msg, NL80211_ATTR_AUTH_TYPE, type))
  3952. goto fail;
  3953. skip_auth_type:
  3954. ret = nl80211_set_conn_keys(params, msg);
  3955. if (ret)
  3956. goto fail;
  3957. ret = send_and_recv_msgs(drv, msg, NULL, NULL);
  3958. msg = NULL;
  3959. if (ret) {
  3960. wpa_printf(MSG_DEBUG, "nl80211: MLME connect failed: ret=%d "
  3961. "(%s)", ret, strerror(-ret));
  3962. } else {
  3963. wpa_printf(MSG_DEBUG,
  3964. "nl80211: Connect request send successfully");
  3965. }
  3966. fail:
  3967. nlmsg_free(msg);
  3968. return ret;
  3969. }
  3970. static int wpa_driver_nl80211_connect(
  3971. struct wpa_driver_nl80211_data *drv,
  3972. struct wpa_driver_associate_params *params)
  3973. {
  3974. int ret;
  3975. /* Store the connection attempted bssid for future use */
  3976. if (params->bssid)
  3977. os_memcpy(drv->auth_attempt_bssid, params->bssid, ETH_ALEN);
  3978. else
  3979. os_memset(drv->auth_attempt_bssid, 0, ETH_ALEN);
  3980. ret = wpa_driver_nl80211_try_connect(drv, params);
  3981. if (ret == -EALREADY) {
  3982. /*
  3983. * cfg80211 does not currently accept new connections if
  3984. * we are already connected. As a workaround, force
  3985. * disconnection and try again.
  3986. */
  3987. wpa_printf(MSG_DEBUG, "nl80211: Explicitly "
  3988. "disconnecting before reassociation "
  3989. "attempt");
  3990. if (wpa_driver_nl80211_disconnect(
  3991. drv, WLAN_REASON_PREV_AUTH_NOT_VALID))
  3992. return -1;
  3993. ret = wpa_driver_nl80211_try_connect(drv, params);
  3994. }
  3995. return ret;
  3996. }
  3997. static int wpa_driver_nl80211_associate(
  3998. void *priv, struct wpa_driver_associate_params *params)
  3999. {
  4000. struct i802_bss *bss = priv;
  4001. struct wpa_driver_nl80211_data *drv = bss->drv;
  4002. int ret = -1;
  4003. struct nl_msg *msg;
  4004. nl80211_unmask_11b_rates(bss);
  4005. if (params->mode == IEEE80211_MODE_AP)
  4006. return wpa_driver_nl80211_ap(drv, params);
  4007. if (params->mode == IEEE80211_MODE_IBSS)
  4008. return wpa_driver_nl80211_ibss(drv, params);
  4009. if (!(drv->capa.flags & WPA_DRIVER_FLAGS_SME)) {
  4010. enum nl80211_iftype nlmode = params->p2p ?
  4011. NL80211_IFTYPE_P2P_CLIENT : NL80211_IFTYPE_STATION;
  4012. if (wpa_driver_nl80211_set_mode(priv, nlmode) < 0)
  4013. return -1;
  4014. return wpa_driver_nl80211_connect(drv, params);
  4015. }
  4016. nl80211_mark_disconnected(drv);
  4017. wpa_printf(MSG_DEBUG, "nl80211: Associate (ifindex=%d)",
  4018. drv->ifindex);
  4019. msg = nl80211_drv_msg(drv, 0, NL80211_CMD_ASSOCIATE);
  4020. if (!msg)
  4021. return -1;
  4022. ret = nl80211_connect_common(drv, params, msg);
  4023. if (ret)
  4024. goto fail;
  4025. if (params->prev_bssid) {
  4026. wpa_printf(MSG_DEBUG, " * prev_bssid=" MACSTR,
  4027. MAC2STR(params->prev_bssid));
  4028. if (nla_put(msg, NL80211_ATTR_PREV_BSSID, ETH_ALEN,
  4029. params->prev_bssid))
  4030. goto fail;
  4031. }
  4032. ret = send_and_recv_msgs(drv, msg, NULL, NULL);
  4033. msg = NULL;
  4034. if (ret) {
  4035. wpa_dbg(drv->ctx, MSG_DEBUG,
  4036. "nl80211: MLME command failed (assoc): ret=%d (%s)",
  4037. ret, strerror(-ret));
  4038. nl80211_dump_scan(drv);
  4039. } else {
  4040. wpa_printf(MSG_DEBUG,
  4041. "nl80211: Association request send successfully");
  4042. }
  4043. fail:
  4044. nlmsg_free(msg);
  4045. return ret;
  4046. }
  4047. static int nl80211_set_mode(struct wpa_driver_nl80211_data *drv,
  4048. int ifindex, enum nl80211_iftype mode)
  4049. {
  4050. struct nl_msg *msg;
  4051. int ret = -ENOBUFS;
  4052. wpa_printf(MSG_DEBUG, "nl80211: Set mode ifindex %d iftype %d (%s)",
  4053. ifindex, mode, nl80211_iftype_str(mode));
  4054. msg = nl80211_cmd_msg(drv->first_bss, 0, NL80211_CMD_SET_INTERFACE);
  4055. if (!msg || nla_put_u32(msg, NL80211_ATTR_IFTYPE, mode))
  4056. goto fail;
  4057. ret = send_and_recv_msgs(drv, msg, NULL, NULL);
  4058. msg = NULL;
  4059. if (!ret)
  4060. return 0;
  4061. fail:
  4062. nlmsg_free(msg);
  4063. wpa_printf(MSG_DEBUG, "nl80211: Failed to set interface %d to mode %d:"
  4064. " %d (%s)", ifindex, mode, ret, strerror(-ret));
  4065. return ret;
  4066. }
  4067. static int wpa_driver_nl80211_set_mode_impl(
  4068. struct i802_bss *bss,
  4069. enum nl80211_iftype nlmode,
  4070. struct hostapd_freq_params *desired_freq_params)
  4071. {
  4072. struct wpa_driver_nl80211_data *drv = bss->drv;
  4073. int ret = -1;
  4074. int i;
  4075. int was_ap = is_ap_interface(drv->nlmode);
  4076. int res;
  4077. int mode_switch_res;
  4078. mode_switch_res = nl80211_set_mode(drv, drv->ifindex, nlmode);
  4079. if (mode_switch_res && nlmode == nl80211_get_ifmode(bss))
  4080. mode_switch_res = 0;
  4081. if (mode_switch_res == 0) {
  4082. drv->nlmode = nlmode;
  4083. ret = 0;
  4084. goto done;
  4085. }
  4086. if (mode_switch_res == -ENODEV)
  4087. return -1;
  4088. if (nlmode == drv->nlmode) {
  4089. wpa_printf(MSG_DEBUG, "nl80211: Interface already in "
  4090. "requested mode - ignore error");
  4091. ret = 0;
  4092. goto done; /* Already in the requested mode */
  4093. }
  4094. /* mac80211 doesn't allow mode changes while the device is up, so
  4095. * take the device down, try to set the mode again, and bring the
  4096. * device back up.
  4097. */
  4098. wpa_printf(MSG_DEBUG, "nl80211: Try mode change after setting "
  4099. "interface down");
  4100. for (i = 0; i < 10; i++) {
  4101. res = i802_set_iface_flags(bss, 0);
  4102. if (res == -EACCES || res == -ENODEV)
  4103. break;
  4104. if (res != 0) {
  4105. wpa_printf(MSG_DEBUG, "nl80211: Failed to set "
  4106. "interface down");
  4107. os_sleep(0, 100000);
  4108. continue;
  4109. }
  4110. /*
  4111. * Setting the mode will fail for some drivers if the phy is
  4112. * on a frequency that the mode is disallowed in.
  4113. */
  4114. if (desired_freq_params) {
  4115. res = nl80211_set_channel(bss, desired_freq_params, 0);
  4116. if (res) {
  4117. wpa_printf(MSG_DEBUG,
  4118. "nl80211: Failed to set frequency on interface");
  4119. }
  4120. }
  4121. /* Try to set the mode again while the interface is down */
  4122. mode_switch_res = nl80211_set_mode(drv, drv->ifindex, nlmode);
  4123. if (mode_switch_res == -EBUSY) {
  4124. wpa_printf(MSG_DEBUG,
  4125. "nl80211: Delaying mode set while interface going down");
  4126. os_sleep(0, 100000);
  4127. continue;
  4128. }
  4129. ret = mode_switch_res;
  4130. break;
  4131. }
  4132. if (!ret) {
  4133. wpa_printf(MSG_DEBUG, "nl80211: Mode change succeeded while "
  4134. "interface is down");
  4135. drv->nlmode = nlmode;
  4136. drv->ignore_if_down_event = 1;
  4137. }
  4138. /* Bring the interface back up */
  4139. res = linux_set_iface_flags(drv->global->ioctl_sock, bss->ifname, 1);
  4140. if (res != 0) {
  4141. wpa_printf(MSG_DEBUG,
  4142. "nl80211: Failed to set interface up after switching mode");
  4143. ret = -1;
  4144. }
  4145. done:
  4146. if (ret) {
  4147. wpa_printf(MSG_DEBUG, "nl80211: Interface mode change to %d "
  4148. "from %d failed", nlmode, drv->nlmode);
  4149. return ret;
  4150. }
  4151. if (is_p2p_net_interface(nlmode)) {
  4152. wpa_printf(MSG_DEBUG,
  4153. "nl80211: Interface %s mode change to P2P - disable 11b rates",
  4154. bss->ifname);
  4155. nl80211_disable_11b_rates(drv, drv->ifindex, 1);
  4156. } else if (drv->disabled_11b_rates) {
  4157. wpa_printf(MSG_DEBUG,
  4158. "nl80211: Interface %s mode changed to non-P2P - re-enable 11b rates",
  4159. bss->ifname);
  4160. nl80211_disable_11b_rates(drv, drv->ifindex, 0);
  4161. }
  4162. if (is_ap_interface(nlmode)) {
  4163. nl80211_mgmt_unsubscribe(bss, "start AP");
  4164. /* Setup additional AP mode functionality if needed */
  4165. if (nl80211_setup_ap(bss))
  4166. return -1;
  4167. } else if (was_ap) {
  4168. /* Remove additional AP mode functionality */
  4169. nl80211_teardown_ap(bss);
  4170. } else {
  4171. nl80211_mgmt_unsubscribe(bss, "mode change");
  4172. }
  4173. if (is_mesh_interface(nlmode) &&
  4174. nl80211_mgmt_subscribe_mesh(bss))
  4175. return -1;
  4176. if (!bss->in_deinit && !is_ap_interface(nlmode) &&
  4177. !is_mesh_interface(nlmode) &&
  4178. nl80211_mgmt_subscribe_non_ap(bss) < 0)
  4179. wpa_printf(MSG_DEBUG, "nl80211: Failed to register Action "
  4180. "frame processing - ignore for now");
  4181. return 0;
  4182. }
  4183. int wpa_driver_nl80211_set_mode(struct i802_bss *bss,
  4184. enum nl80211_iftype nlmode)
  4185. {
  4186. return wpa_driver_nl80211_set_mode_impl(bss, nlmode, NULL);
  4187. }
  4188. static int wpa_driver_nl80211_set_mode_ibss(struct i802_bss *bss,
  4189. struct hostapd_freq_params *freq)
  4190. {
  4191. return wpa_driver_nl80211_set_mode_impl(bss, NL80211_IFTYPE_ADHOC,
  4192. freq);
  4193. }
  4194. static int wpa_driver_nl80211_get_capa(void *priv,
  4195. struct wpa_driver_capa *capa)
  4196. {
  4197. struct i802_bss *bss = priv;
  4198. struct wpa_driver_nl80211_data *drv = bss->drv;
  4199. if (!drv->has_capability)
  4200. return -1;
  4201. os_memcpy(capa, &drv->capa, sizeof(*capa));
  4202. if (drv->extended_capa && drv->extended_capa_mask) {
  4203. capa->extended_capa = drv->extended_capa;
  4204. capa->extended_capa_mask = drv->extended_capa_mask;
  4205. capa->extended_capa_len = drv->extended_capa_len;
  4206. }
  4207. return 0;
  4208. }
  4209. static int wpa_driver_nl80211_set_operstate(void *priv, int state)
  4210. {
  4211. struct i802_bss *bss = priv;
  4212. struct wpa_driver_nl80211_data *drv = bss->drv;
  4213. wpa_printf(MSG_DEBUG, "nl80211: Set %s operstate %d->%d (%s)",
  4214. bss->ifname, drv->operstate, state,
  4215. state ? "UP" : "DORMANT");
  4216. drv->operstate = state;
  4217. return netlink_send_oper_ifla(drv->global->netlink, drv->ifindex, -1,
  4218. state ? IF_OPER_UP : IF_OPER_DORMANT);
  4219. }
  4220. static int wpa_driver_nl80211_set_supp_port(void *priv, int authorized)
  4221. {
  4222. struct i802_bss *bss = priv;
  4223. struct wpa_driver_nl80211_data *drv = bss->drv;
  4224. struct nl_msg *msg;
  4225. struct nl80211_sta_flag_update upd;
  4226. int ret;
  4227. if (!drv->associated && is_zero_ether_addr(drv->bssid) && !authorized) {
  4228. wpa_printf(MSG_DEBUG, "nl80211: Skip set_supp_port(unauthorized) while not associated");
  4229. return 0;
  4230. }
  4231. wpa_printf(MSG_DEBUG, "nl80211: Set supplicant port %sauthorized for "
  4232. MACSTR, authorized ? "" : "un", MAC2STR(drv->bssid));
  4233. os_memset(&upd, 0, sizeof(upd));
  4234. upd.mask = BIT(NL80211_STA_FLAG_AUTHORIZED);
  4235. if (authorized)
  4236. upd.set = BIT(NL80211_STA_FLAG_AUTHORIZED);
  4237. if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_SET_STATION)) ||
  4238. nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, drv->bssid) ||
  4239. nla_put(msg, NL80211_ATTR_STA_FLAGS2, sizeof(upd), &upd)) {
  4240. nlmsg_free(msg);
  4241. return -ENOBUFS;
  4242. }
  4243. ret = send_and_recv_msgs(drv, msg, NULL, NULL);
  4244. if (!ret)
  4245. return 0;
  4246. wpa_printf(MSG_DEBUG, "nl80211: Failed to set STA flag: %d (%s)",
  4247. ret, strerror(-ret));
  4248. return ret;
  4249. }
  4250. /* Set kernel driver on given frequency (MHz) */
  4251. static int i802_set_freq(void *priv, struct hostapd_freq_params *freq)
  4252. {
  4253. struct i802_bss *bss = priv;
  4254. return nl80211_set_channel(bss, freq, 0);
  4255. }
  4256. static inline int min_int(int a, int b)
  4257. {
  4258. if (a < b)
  4259. return a;
  4260. return b;
  4261. }
  4262. static int get_key_handler(struct nl_msg *msg, void *arg)
  4263. {
  4264. struct nlattr *tb[NL80211_ATTR_MAX + 1];
  4265. struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
  4266. nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
  4267. genlmsg_attrlen(gnlh, 0), NULL);
  4268. /*
  4269. * TODO: validate the key index and mac address!
  4270. * Otherwise, there's a race condition as soon as
  4271. * the kernel starts sending key notifications.
  4272. */
  4273. if (tb[NL80211_ATTR_KEY_SEQ])
  4274. memcpy(arg, nla_data(tb[NL80211_ATTR_KEY_SEQ]),
  4275. min_int(nla_len(tb[NL80211_ATTR_KEY_SEQ]), 6));
  4276. return NL_SKIP;
  4277. }
  4278. static int i802_get_seqnum(const char *iface, void *priv, const u8 *addr,
  4279. int idx, u8 *seq)
  4280. {
  4281. struct i802_bss *bss = priv;
  4282. struct wpa_driver_nl80211_data *drv = bss->drv;
  4283. struct nl_msg *msg;
  4284. msg = nl80211_ifindex_msg(drv, if_nametoindex(iface), 0,
  4285. NL80211_CMD_GET_KEY);
  4286. if (!msg ||
  4287. (addr && nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr)) ||
  4288. nla_put_u8(msg, NL80211_ATTR_KEY_IDX, idx)) {
  4289. nlmsg_free(msg);
  4290. return -ENOBUFS;
  4291. }
  4292. memset(seq, 0, 6);
  4293. return send_and_recv_msgs(drv, msg, get_key_handler, seq);
  4294. }
  4295. static int i802_set_rts(void *priv, int rts)
  4296. {
  4297. struct i802_bss *bss = priv;
  4298. struct wpa_driver_nl80211_data *drv = bss->drv;
  4299. struct nl_msg *msg;
  4300. int ret;
  4301. u32 val;
  4302. if (rts >= 2347)
  4303. val = (u32) -1;
  4304. else
  4305. val = rts;
  4306. if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_SET_WIPHY)) ||
  4307. nla_put_u32(msg, NL80211_ATTR_WIPHY_RTS_THRESHOLD, val)) {
  4308. nlmsg_free(msg);
  4309. return -ENOBUFS;
  4310. }
  4311. ret = send_and_recv_msgs(drv, msg, NULL, NULL);
  4312. if (!ret)
  4313. return 0;
  4314. wpa_printf(MSG_DEBUG, "nl80211: Failed to set RTS threshold %d: "
  4315. "%d (%s)", rts, ret, strerror(-ret));
  4316. return ret;
  4317. }
  4318. static int i802_set_frag(void *priv, int frag)
  4319. {
  4320. struct i802_bss *bss = priv;
  4321. struct wpa_driver_nl80211_data *drv = bss->drv;
  4322. struct nl_msg *msg;
  4323. int ret;
  4324. u32 val;
  4325. if (frag >= 2346)
  4326. val = (u32) -1;
  4327. else
  4328. val = frag;
  4329. if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_SET_WIPHY)) ||
  4330. nla_put_u32(msg, NL80211_ATTR_WIPHY_FRAG_THRESHOLD, val)) {
  4331. nlmsg_free(msg);
  4332. return -ENOBUFS;
  4333. }
  4334. ret = send_and_recv_msgs(drv, msg, NULL, NULL);
  4335. if (!ret)
  4336. return 0;
  4337. wpa_printf(MSG_DEBUG, "nl80211: Failed to set fragmentation threshold "
  4338. "%d: %d (%s)", frag, ret, strerror(-ret));
  4339. return ret;
  4340. }
  4341. static int i802_flush(void *priv)
  4342. {
  4343. struct i802_bss *bss = priv;
  4344. struct nl_msg *msg;
  4345. int res;
  4346. wpa_printf(MSG_DEBUG, "nl80211: flush -> DEL_STATION %s (all)",
  4347. bss->ifname);
  4348. /*
  4349. * XXX: FIX! this needs to flush all VLANs too
  4350. */
  4351. msg = nl80211_bss_msg(bss, 0, NL80211_CMD_DEL_STATION);
  4352. res = send_and_recv_msgs(bss->drv, msg, NULL, NULL);
  4353. if (res) {
  4354. wpa_printf(MSG_DEBUG, "nl80211: Station flush failed: ret=%d "
  4355. "(%s)", res, strerror(-res));
  4356. }
  4357. return res;
  4358. }
  4359. static int get_sta_handler(struct nl_msg *msg, void *arg)
  4360. {
  4361. struct nlattr *tb[NL80211_ATTR_MAX + 1];
  4362. struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
  4363. struct hostap_sta_driver_data *data = arg;
  4364. struct nlattr *stats[NL80211_STA_INFO_MAX + 1];
  4365. static struct nla_policy stats_policy[NL80211_STA_INFO_MAX + 1] = {
  4366. [NL80211_STA_INFO_INACTIVE_TIME] = { .type = NLA_U32 },
  4367. [NL80211_STA_INFO_RX_BYTES] = { .type = NLA_U32 },
  4368. [NL80211_STA_INFO_TX_BYTES] = { .type = NLA_U32 },
  4369. [NL80211_STA_INFO_RX_PACKETS] = { .type = NLA_U32 },
  4370. [NL80211_STA_INFO_TX_PACKETS] = { .type = NLA_U32 },
  4371. [NL80211_STA_INFO_TX_FAILED] = { .type = NLA_U32 },
  4372. };
  4373. nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
  4374. genlmsg_attrlen(gnlh, 0), NULL);
  4375. /*
  4376. * TODO: validate the interface and mac address!
  4377. * Otherwise, there's a race condition as soon as
  4378. * the kernel starts sending station notifications.
  4379. */
  4380. if (!tb[NL80211_ATTR_STA_INFO]) {
  4381. wpa_printf(MSG_DEBUG, "sta stats missing!");
  4382. return NL_SKIP;
  4383. }
  4384. if (nla_parse_nested(stats, NL80211_STA_INFO_MAX,
  4385. tb[NL80211_ATTR_STA_INFO],
  4386. stats_policy)) {
  4387. wpa_printf(MSG_DEBUG, "failed to parse nested attributes!");
  4388. return NL_SKIP;
  4389. }
  4390. if (stats[NL80211_STA_INFO_INACTIVE_TIME])
  4391. data->inactive_msec =
  4392. nla_get_u32(stats[NL80211_STA_INFO_INACTIVE_TIME]);
  4393. if (stats[NL80211_STA_INFO_RX_BYTES])
  4394. data->rx_bytes = nla_get_u32(stats[NL80211_STA_INFO_RX_BYTES]);
  4395. if (stats[NL80211_STA_INFO_TX_BYTES])
  4396. data->tx_bytes = nla_get_u32(stats[NL80211_STA_INFO_TX_BYTES]);
  4397. if (stats[NL80211_STA_INFO_RX_PACKETS])
  4398. data->rx_packets =
  4399. nla_get_u32(stats[NL80211_STA_INFO_RX_PACKETS]);
  4400. if (stats[NL80211_STA_INFO_TX_PACKETS])
  4401. data->tx_packets =
  4402. nla_get_u32(stats[NL80211_STA_INFO_TX_PACKETS]);
  4403. if (stats[NL80211_STA_INFO_TX_FAILED])
  4404. data->tx_retry_failed =
  4405. nla_get_u32(stats[NL80211_STA_INFO_TX_FAILED]);
  4406. return NL_SKIP;
  4407. }
  4408. static int i802_read_sta_data(struct i802_bss *bss,
  4409. struct hostap_sta_driver_data *data,
  4410. const u8 *addr)
  4411. {
  4412. struct nl_msg *msg;
  4413. os_memset(data, 0, sizeof(*data));
  4414. if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_GET_STATION)) ||
  4415. nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr)) {
  4416. nlmsg_free(msg);
  4417. return -ENOBUFS;
  4418. }
  4419. return send_and_recv_msgs(bss->drv, msg, get_sta_handler, data);
  4420. }
  4421. static int i802_set_tx_queue_params(void *priv, int queue, int aifs,
  4422. int cw_min, int cw_max, int burst_time)
  4423. {
  4424. struct i802_bss *bss = priv;
  4425. struct wpa_driver_nl80211_data *drv = bss->drv;
  4426. struct nl_msg *msg;
  4427. struct nlattr *txq, *params;
  4428. msg = nl80211_bss_msg(bss, 0, NL80211_CMD_SET_WIPHY);
  4429. if (!msg)
  4430. return -1;
  4431. txq = nla_nest_start(msg, NL80211_ATTR_WIPHY_TXQ_PARAMS);
  4432. if (!txq)
  4433. goto fail;
  4434. /* We are only sending parameters for a single TXQ at a time */
  4435. params = nla_nest_start(msg, 1);
  4436. if (!params)
  4437. goto fail;
  4438. switch (queue) {
  4439. case 0:
  4440. if (nla_put_u8(msg, NL80211_TXQ_ATTR_QUEUE, NL80211_TXQ_Q_VO))
  4441. goto fail;
  4442. break;
  4443. case 1:
  4444. if (nla_put_u8(msg, NL80211_TXQ_ATTR_QUEUE, NL80211_TXQ_Q_VI))
  4445. goto fail;
  4446. break;
  4447. case 2:
  4448. if (nla_put_u8(msg, NL80211_TXQ_ATTR_QUEUE, NL80211_TXQ_Q_BE))
  4449. goto fail;
  4450. break;
  4451. case 3:
  4452. if (nla_put_u8(msg, NL80211_TXQ_ATTR_QUEUE, NL80211_TXQ_Q_BK))
  4453. goto fail;
  4454. break;
  4455. }
  4456. /* Burst time is configured in units of 0.1 msec and TXOP parameter in
  4457. * 32 usec, so need to convert the value here. */
  4458. if (nla_put_u16(msg, NL80211_TXQ_ATTR_TXOP,
  4459. (burst_time * 100 + 16) / 32) ||
  4460. nla_put_u16(msg, NL80211_TXQ_ATTR_CWMIN, cw_min) ||
  4461. nla_put_u16(msg, NL80211_TXQ_ATTR_CWMAX, cw_max) ||
  4462. nla_put_u8(msg, NL80211_TXQ_ATTR_AIFS, aifs))
  4463. goto fail;
  4464. nla_nest_end(msg, params);
  4465. nla_nest_end(msg, txq);
  4466. if (send_and_recv_msgs(drv, msg, NULL, NULL) == 0)
  4467. return 0;
  4468. msg = NULL;
  4469. fail:
  4470. nlmsg_free(msg);
  4471. return -1;
  4472. }
  4473. static int i802_set_sta_vlan(struct i802_bss *bss, const u8 *addr,
  4474. const char *ifname, int vlan_id)
  4475. {
  4476. struct wpa_driver_nl80211_data *drv = bss->drv;
  4477. struct nl_msg *msg;
  4478. int ret;
  4479. wpa_printf(MSG_DEBUG, "nl80211: %s[%d]: set_sta_vlan(" MACSTR
  4480. ", ifname=%s[%d], vlan_id=%d)",
  4481. bss->ifname, if_nametoindex(bss->ifname),
  4482. MAC2STR(addr), ifname, if_nametoindex(ifname), vlan_id);
  4483. if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_SET_STATION)) ||
  4484. nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr) ||
  4485. nla_put_u32(msg, NL80211_ATTR_STA_VLAN, if_nametoindex(ifname))) {
  4486. nlmsg_free(msg);
  4487. return -ENOBUFS;
  4488. }
  4489. ret = send_and_recv_msgs(drv, msg, NULL, NULL);
  4490. if (ret < 0) {
  4491. wpa_printf(MSG_ERROR, "nl80211: NL80211_ATTR_STA_VLAN (addr="
  4492. MACSTR " ifname=%s vlan_id=%d) failed: %d (%s)",
  4493. MAC2STR(addr), ifname, vlan_id, ret,
  4494. strerror(-ret));
  4495. }
  4496. return ret;
  4497. }
  4498. static int i802_get_inact_sec(void *priv, const u8 *addr)
  4499. {
  4500. struct hostap_sta_driver_data data;
  4501. int ret;
  4502. data.inactive_msec = (unsigned long) -1;
  4503. ret = i802_read_sta_data(priv, &data, addr);
  4504. if (ret == -ENOENT)
  4505. return -ENOENT;
  4506. if (ret || data.inactive_msec == (unsigned long) -1)
  4507. return -1;
  4508. return data.inactive_msec / 1000;
  4509. }
  4510. static int i802_sta_clear_stats(void *priv, const u8 *addr)
  4511. {
  4512. #if 0
  4513. /* TODO */
  4514. #endif
  4515. return 0;
  4516. }
  4517. static int i802_sta_deauth(void *priv, const u8 *own_addr, const u8 *addr,
  4518. int reason)
  4519. {
  4520. struct i802_bss *bss = priv;
  4521. struct wpa_driver_nl80211_data *drv = bss->drv;
  4522. struct ieee80211_mgmt mgmt;
  4523. if (is_mesh_interface(drv->nlmode))
  4524. return -1;
  4525. if (drv->device_ap_sme)
  4526. return wpa_driver_nl80211_sta_remove(bss, addr, 1, reason);
  4527. memset(&mgmt, 0, sizeof(mgmt));
  4528. mgmt.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
  4529. WLAN_FC_STYPE_DEAUTH);
  4530. memcpy(mgmt.da, addr, ETH_ALEN);
  4531. memcpy(mgmt.sa, own_addr, ETH_ALEN);
  4532. memcpy(mgmt.bssid, own_addr, ETH_ALEN);
  4533. mgmt.u.deauth.reason_code = host_to_le16(reason);
  4534. return wpa_driver_nl80211_send_mlme(bss, (u8 *) &mgmt,
  4535. IEEE80211_HDRLEN +
  4536. sizeof(mgmt.u.deauth), 0, 0, 0, 0,
  4537. 0);
  4538. }
  4539. static int i802_sta_disassoc(void *priv, const u8 *own_addr, const u8 *addr,
  4540. int reason)
  4541. {
  4542. struct i802_bss *bss = priv;
  4543. struct wpa_driver_nl80211_data *drv = bss->drv;
  4544. struct ieee80211_mgmt mgmt;
  4545. if (is_mesh_interface(drv->nlmode))
  4546. return -1;
  4547. if (drv->device_ap_sme)
  4548. return wpa_driver_nl80211_sta_remove(bss, addr, 0, reason);
  4549. memset(&mgmt, 0, sizeof(mgmt));
  4550. mgmt.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
  4551. WLAN_FC_STYPE_DISASSOC);
  4552. memcpy(mgmt.da, addr, ETH_ALEN);
  4553. memcpy(mgmt.sa, own_addr, ETH_ALEN);
  4554. memcpy(mgmt.bssid, own_addr, ETH_ALEN);
  4555. mgmt.u.disassoc.reason_code = host_to_le16(reason);
  4556. return wpa_driver_nl80211_send_mlme(bss, (u8 *) &mgmt,
  4557. IEEE80211_HDRLEN +
  4558. sizeof(mgmt.u.disassoc), 0, 0, 0, 0,
  4559. 0);
  4560. }
  4561. static void dump_ifidx(struct wpa_driver_nl80211_data *drv)
  4562. {
  4563. char buf[200], *pos, *end;
  4564. int i, res;
  4565. pos = buf;
  4566. end = pos + sizeof(buf);
  4567. for (i = 0; i < drv->num_if_indices; i++) {
  4568. if (!drv->if_indices[i])
  4569. continue;
  4570. res = os_snprintf(pos, end - pos, " %d", drv->if_indices[i]);
  4571. if (os_snprintf_error(end - pos, res))
  4572. break;
  4573. pos += res;
  4574. }
  4575. *pos = '\0';
  4576. wpa_printf(MSG_DEBUG, "nl80211: if_indices[%d]:%s",
  4577. drv->num_if_indices, buf);
  4578. }
  4579. static void add_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx)
  4580. {
  4581. int i;
  4582. int *old;
  4583. wpa_printf(MSG_DEBUG, "nl80211: Add own interface ifindex %d",
  4584. ifidx);
  4585. if (have_ifidx(drv, ifidx)) {
  4586. wpa_printf(MSG_DEBUG, "nl80211: ifindex %d already in the list",
  4587. ifidx);
  4588. return;
  4589. }
  4590. for (i = 0; i < drv->num_if_indices; i++) {
  4591. if (drv->if_indices[i] == 0) {
  4592. drv->if_indices[i] = ifidx;
  4593. dump_ifidx(drv);
  4594. return;
  4595. }
  4596. }
  4597. if (drv->if_indices != drv->default_if_indices)
  4598. old = drv->if_indices;
  4599. else
  4600. old = NULL;
  4601. drv->if_indices = os_realloc_array(old, drv->num_if_indices + 1,
  4602. sizeof(int));
  4603. if (!drv->if_indices) {
  4604. if (!old)
  4605. drv->if_indices = drv->default_if_indices;
  4606. else
  4607. drv->if_indices = old;
  4608. wpa_printf(MSG_ERROR, "Failed to reallocate memory for "
  4609. "interfaces");
  4610. wpa_printf(MSG_ERROR, "Ignoring EAPOL on interface %d", ifidx);
  4611. return;
  4612. } else if (!old)
  4613. os_memcpy(drv->if_indices, drv->default_if_indices,
  4614. sizeof(drv->default_if_indices));
  4615. drv->if_indices[drv->num_if_indices] = ifidx;
  4616. drv->num_if_indices++;
  4617. dump_ifidx(drv);
  4618. }
  4619. static void del_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx)
  4620. {
  4621. int i;
  4622. for (i = 0; i < drv->num_if_indices; i++) {
  4623. if (drv->if_indices[i] == ifidx) {
  4624. drv->if_indices[i] = 0;
  4625. break;
  4626. }
  4627. }
  4628. dump_ifidx(drv);
  4629. }
  4630. static int have_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx)
  4631. {
  4632. int i;
  4633. for (i = 0; i < drv->num_if_indices; i++)
  4634. if (drv->if_indices[i] == ifidx)
  4635. return 1;
  4636. return 0;
  4637. }
  4638. static int i802_set_wds_sta(void *priv, const u8 *addr, int aid, int val,
  4639. const char *bridge_ifname, char *ifname_wds)
  4640. {
  4641. struct i802_bss *bss = priv;
  4642. struct wpa_driver_nl80211_data *drv = bss->drv;
  4643. char name[IFNAMSIZ + 1];
  4644. os_snprintf(name, sizeof(name), "%s.sta%d", bss->ifname, aid);
  4645. if (ifname_wds)
  4646. os_strlcpy(ifname_wds, name, IFNAMSIZ + 1);
  4647. wpa_printf(MSG_DEBUG, "nl80211: Set WDS STA addr=" MACSTR
  4648. " aid=%d val=%d name=%s", MAC2STR(addr), aid, val, name);
  4649. if (val) {
  4650. if (!if_nametoindex(name)) {
  4651. if (nl80211_create_iface(drv, name,
  4652. NL80211_IFTYPE_AP_VLAN,
  4653. bss->addr, 1, NULL, NULL, 0) <
  4654. 0)
  4655. return -1;
  4656. if (bridge_ifname &&
  4657. linux_br_add_if(drv->global->ioctl_sock,
  4658. bridge_ifname, name) < 0)
  4659. return -1;
  4660. }
  4661. if (linux_set_iface_flags(drv->global->ioctl_sock, name, 1)) {
  4662. wpa_printf(MSG_ERROR, "nl80211: Failed to set WDS STA "
  4663. "interface %s up", name);
  4664. }
  4665. return i802_set_sta_vlan(priv, addr, name, 0);
  4666. } else {
  4667. if (bridge_ifname)
  4668. linux_br_del_if(drv->global->ioctl_sock, bridge_ifname,
  4669. name);
  4670. i802_set_sta_vlan(priv, addr, bss->ifname, 0);
  4671. nl80211_remove_iface(drv, if_nametoindex(name));
  4672. return 0;
  4673. }
  4674. }
  4675. static void handle_eapol(int sock, void *eloop_ctx, void *sock_ctx)
  4676. {
  4677. struct wpa_driver_nl80211_data *drv = eloop_ctx;
  4678. struct sockaddr_ll lladdr;
  4679. unsigned char buf[3000];
  4680. int len;
  4681. socklen_t fromlen = sizeof(lladdr);
  4682. len = recvfrom(sock, buf, sizeof(buf), 0,
  4683. (struct sockaddr *)&lladdr, &fromlen);
  4684. if (len < 0) {
  4685. wpa_printf(MSG_ERROR, "nl80211: EAPOL recv failed: %s",
  4686. strerror(errno));
  4687. return;
  4688. }
  4689. if (have_ifidx(drv, lladdr.sll_ifindex))
  4690. drv_event_eapol_rx(drv->ctx, lladdr.sll_addr, buf, len);
  4691. }
  4692. static int i802_check_bridge(struct wpa_driver_nl80211_data *drv,
  4693. struct i802_bss *bss,
  4694. const char *brname, const char *ifname)
  4695. {
  4696. int br_ifindex;
  4697. char in_br[IFNAMSIZ];
  4698. os_strlcpy(bss->brname, brname, IFNAMSIZ);
  4699. br_ifindex = if_nametoindex(brname);
  4700. if (br_ifindex == 0) {
  4701. /*
  4702. * Bridge was configured, but the bridge device does
  4703. * not exist. Try to add it now.
  4704. */
  4705. if (linux_br_add(drv->global->ioctl_sock, brname) < 0) {
  4706. wpa_printf(MSG_ERROR, "nl80211: Failed to add the "
  4707. "bridge interface %s: %s",
  4708. brname, strerror(errno));
  4709. return -1;
  4710. }
  4711. bss->added_bridge = 1;
  4712. br_ifindex = if_nametoindex(brname);
  4713. add_ifidx(drv, br_ifindex);
  4714. }
  4715. bss->br_ifindex = br_ifindex;
  4716. if (linux_br_get(in_br, ifname) == 0) {
  4717. if (os_strcmp(in_br, brname) == 0)
  4718. return 0; /* already in the bridge */
  4719. wpa_printf(MSG_DEBUG, "nl80211: Removing interface %s from "
  4720. "bridge %s", ifname, in_br);
  4721. if (linux_br_del_if(drv->global->ioctl_sock, in_br, ifname) <
  4722. 0) {
  4723. wpa_printf(MSG_ERROR, "nl80211: Failed to "
  4724. "remove interface %s from bridge "
  4725. "%s: %s",
  4726. ifname, brname, strerror(errno));
  4727. return -1;
  4728. }
  4729. }
  4730. wpa_printf(MSG_DEBUG, "nl80211: Adding interface %s into bridge %s",
  4731. ifname, brname);
  4732. if (linux_br_add_if(drv->global->ioctl_sock, brname, ifname) < 0) {
  4733. wpa_printf(MSG_ERROR, "nl80211: Failed to add interface %s "
  4734. "into bridge %s: %s",
  4735. ifname, brname, strerror(errno));
  4736. return -1;
  4737. }
  4738. bss->added_if_into_bridge = 1;
  4739. return 0;
  4740. }
  4741. static void *i802_init(struct hostapd_data *hapd,
  4742. struct wpa_init_params *params)
  4743. {
  4744. struct wpa_driver_nl80211_data *drv;
  4745. struct i802_bss *bss;
  4746. size_t i;
  4747. char brname[IFNAMSIZ];
  4748. int ifindex, br_ifindex;
  4749. int br_added = 0;
  4750. bss = wpa_driver_nl80211_drv_init(hapd, params->ifname,
  4751. params->global_priv, 1,
  4752. params->bssid, params->driver_params);
  4753. if (bss == NULL)
  4754. return NULL;
  4755. drv = bss->drv;
  4756. if (linux_br_get(brname, params->ifname) == 0) {
  4757. wpa_printf(MSG_DEBUG, "nl80211: Interface %s is in bridge %s",
  4758. params->ifname, brname);
  4759. br_ifindex = if_nametoindex(brname);
  4760. os_strlcpy(bss->brname, brname, IFNAMSIZ);
  4761. } else {
  4762. brname[0] = '\0';
  4763. br_ifindex = 0;
  4764. }
  4765. bss->br_ifindex = br_ifindex;
  4766. for (i = 0; i < params->num_bridge; i++) {
  4767. if (params->bridge[i]) {
  4768. ifindex = if_nametoindex(params->bridge[i]);
  4769. if (ifindex)
  4770. add_ifidx(drv, ifindex);
  4771. if (ifindex == br_ifindex)
  4772. br_added = 1;
  4773. }
  4774. }
  4775. /* start listening for EAPOL on the default AP interface */
  4776. add_ifidx(drv, drv->ifindex);
  4777. if (params->num_bridge && params->bridge[0]) {
  4778. if (i802_check_bridge(drv, bss, params->bridge[0],
  4779. params->ifname) < 0)
  4780. goto failed;
  4781. if (os_strcmp(params->bridge[0], brname) != 0)
  4782. br_added = 1;
  4783. }
  4784. if (!br_added && br_ifindex &&
  4785. (params->num_bridge == 0 || !params->bridge[0]))
  4786. add_ifidx(drv, br_ifindex);
  4787. #ifdef CONFIG_LIBNL3_ROUTE
  4788. if (bss->added_if_into_bridge) {
  4789. drv->rtnl_sk = nl_socket_alloc();
  4790. if (drv->rtnl_sk == NULL) {
  4791. wpa_printf(MSG_ERROR, "nl80211: Failed to allocate nl_sock");
  4792. goto failed;
  4793. }
  4794. if (nl_connect(drv->rtnl_sk, NETLINK_ROUTE)) {
  4795. wpa_printf(MSG_ERROR, "nl80211: Failed to connect nl_sock to NETLINK_ROUTE: %s",
  4796. strerror(errno));
  4797. goto failed;
  4798. }
  4799. }
  4800. #endif /* CONFIG_LIBNL3_ROUTE */
  4801. drv->eapol_sock = socket(PF_PACKET, SOCK_DGRAM, htons(ETH_P_PAE));
  4802. if (drv->eapol_sock < 0) {
  4803. wpa_printf(MSG_ERROR, "nl80211: socket(PF_PACKET, SOCK_DGRAM, ETH_P_PAE) failed: %s",
  4804. strerror(errno));
  4805. goto failed;
  4806. }
  4807. if (eloop_register_read_sock(drv->eapol_sock, handle_eapol, drv, NULL))
  4808. {
  4809. wpa_printf(MSG_INFO, "nl80211: Could not register read socket for eapol");
  4810. goto failed;
  4811. }
  4812. if (linux_get_ifhwaddr(drv->global->ioctl_sock, bss->ifname,
  4813. params->own_addr))
  4814. goto failed;
  4815. os_memcpy(drv->perm_addr, params->own_addr, ETH_ALEN);
  4816. memcpy(bss->addr, params->own_addr, ETH_ALEN);
  4817. return bss;
  4818. failed:
  4819. wpa_driver_nl80211_deinit(bss);
  4820. return NULL;
  4821. }
  4822. static void i802_deinit(void *priv)
  4823. {
  4824. struct i802_bss *bss = priv;
  4825. wpa_driver_nl80211_deinit(bss);
  4826. }
  4827. static enum nl80211_iftype wpa_driver_nl80211_if_type(
  4828. enum wpa_driver_if_type type)
  4829. {
  4830. switch (type) {
  4831. case WPA_IF_STATION:
  4832. return NL80211_IFTYPE_STATION;
  4833. case WPA_IF_P2P_CLIENT:
  4834. case WPA_IF_P2P_GROUP:
  4835. return NL80211_IFTYPE_P2P_CLIENT;
  4836. case WPA_IF_AP_VLAN:
  4837. return NL80211_IFTYPE_AP_VLAN;
  4838. case WPA_IF_AP_BSS:
  4839. return NL80211_IFTYPE_AP;
  4840. case WPA_IF_P2P_GO:
  4841. return NL80211_IFTYPE_P2P_GO;
  4842. case WPA_IF_P2P_DEVICE:
  4843. return NL80211_IFTYPE_P2P_DEVICE;
  4844. case WPA_IF_MESH:
  4845. return NL80211_IFTYPE_MESH_POINT;
  4846. }
  4847. return -1;
  4848. }
  4849. #if defined(CONFIG_P2P) || defined(CONFIG_MESH)
  4850. static int nl80211_addr_in_use(struct nl80211_global *global, const u8 *addr)
  4851. {
  4852. struct wpa_driver_nl80211_data *drv;
  4853. dl_list_for_each(drv, &global->interfaces,
  4854. struct wpa_driver_nl80211_data, list) {
  4855. if (os_memcmp(addr, drv->first_bss->addr, ETH_ALEN) == 0)
  4856. return 1;
  4857. }
  4858. return 0;
  4859. }
  4860. static int nl80211_vif_addr(struct wpa_driver_nl80211_data *drv, u8 *new_addr)
  4861. {
  4862. unsigned int idx;
  4863. if (!drv->global)
  4864. return -1;
  4865. os_memcpy(new_addr, drv->first_bss->addr, ETH_ALEN);
  4866. for (idx = 0; idx < 64; idx++) {
  4867. new_addr[0] = drv->first_bss->addr[0] | 0x02;
  4868. new_addr[0] ^= idx << 2;
  4869. if (!nl80211_addr_in_use(drv->global, new_addr))
  4870. break;
  4871. }
  4872. if (idx == 64)
  4873. return -1;
  4874. wpa_printf(MSG_DEBUG, "nl80211: Assigned new virtual interface address "
  4875. MACSTR, MAC2STR(new_addr));
  4876. return 0;
  4877. }
  4878. #endif /* CONFIG_P2P || CONFIG_MESH */
  4879. struct wdev_info {
  4880. u64 wdev_id;
  4881. int wdev_id_set;
  4882. u8 macaddr[ETH_ALEN];
  4883. };
  4884. static int nl80211_wdev_handler(struct nl_msg *msg, void *arg)
  4885. {
  4886. struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
  4887. struct nlattr *tb[NL80211_ATTR_MAX + 1];
  4888. struct wdev_info *wi = arg;
  4889. nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
  4890. genlmsg_attrlen(gnlh, 0), NULL);
  4891. if (tb[NL80211_ATTR_WDEV]) {
  4892. wi->wdev_id = nla_get_u64(tb[NL80211_ATTR_WDEV]);
  4893. wi->wdev_id_set = 1;
  4894. }
  4895. if (tb[NL80211_ATTR_MAC])
  4896. os_memcpy(wi->macaddr, nla_data(tb[NL80211_ATTR_MAC]),
  4897. ETH_ALEN);
  4898. return NL_SKIP;
  4899. }
  4900. static int wpa_driver_nl80211_if_add(void *priv, enum wpa_driver_if_type type,
  4901. const char *ifname, const u8 *addr,
  4902. void *bss_ctx, void **drv_priv,
  4903. char *force_ifname, u8 *if_addr,
  4904. const char *bridge, int use_existing)
  4905. {
  4906. enum nl80211_iftype nlmode;
  4907. struct i802_bss *bss = priv;
  4908. struct wpa_driver_nl80211_data *drv = bss->drv;
  4909. int ifidx;
  4910. int added = 1;
  4911. if (addr)
  4912. os_memcpy(if_addr, addr, ETH_ALEN);
  4913. nlmode = wpa_driver_nl80211_if_type(type);
  4914. if (nlmode == NL80211_IFTYPE_P2P_DEVICE) {
  4915. struct wdev_info p2pdev_info;
  4916. os_memset(&p2pdev_info, 0, sizeof(p2pdev_info));
  4917. ifidx = nl80211_create_iface(drv, ifname, nlmode, addr,
  4918. 0, nl80211_wdev_handler,
  4919. &p2pdev_info, use_existing);
  4920. if (!p2pdev_info.wdev_id_set || ifidx != 0) {
  4921. wpa_printf(MSG_ERROR, "nl80211: Failed to create a P2P Device interface %s",
  4922. ifname);
  4923. return -1;
  4924. }
  4925. drv->global->if_add_wdevid = p2pdev_info.wdev_id;
  4926. drv->global->if_add_wdevid_set = p2pdev_info.wdev_id_set;
  4927. if (!is_zero_ether_addr(p2pdev_info.macaddr))
  4928. os_memcpy(if_addr, p2pdev_info.macaddr, ETH_ALEN);
  4929. wpa_printf(MSG_DEBUG, "nl80211: New P2P Device interface %s (0x%llx) created",
  4930. ifname,
  4931. (long long unsigned int) p2pdev_info.wdev_id);
  4932. } else {
  4933. ifidx = nl80211_create_iface(drv, ifname, nlmode, addr,
  4934. 0, NULL, NULL, use_existing);
  4935. if (use_existing && ifidx == -ENFILE) {
  4936. added = 0;
  4937. ifidx = if_nametoindex(ifname);
  4938. } else if (ifidx < 0) {
  4939. return -1;
  4940. }
  4941. }
  4942. if (!addr) {
  4943. if (drv->nlmode == NL80211_IFTYPE_P2P_DEVICE)
  4944. os_memcpy(if_addr, bss->addr, ETH_ALEN);
  4945. else if (linux_get_ifhwaddr(drv->global->ioctl_sock,
  4946. bss->ifname, if_addr) < 0) {
  4947. if (added)
  4948. nl80211_remove_iface(drv, ifidx);
  4949. return -1;
  4950. }
  4951. }
  4952. #if defined(CONFIG_P2P) || defined(CONFIG_MESH)
  4953. if (!addr &&
  4954. (type == WPA_IF_P2P_CLIENT || type == WPA_IF_P2P_GROUP ||
  4955. type == WPA_IF_P2P_GO || type == WPA_IF_MESH)) {
  4956. /* Enforce unique P2P Interface Address */
  4957. u8 new_addr[ETH_ALEN];
  4958. if (linux_get_ifhwaddr(drv->global->ioctl_sock, ifname,
  4959. new_addr) < 0) {
  4960. if (added)
  4961. nl80211_remove_iface(drv, ifidx);
  4962. return -1;
  4963. }
  4964. if (nl80211_addr_in_use(drv->global, new_addr)) {
  4965. wpa_printf(MSG_DEBUG, "nl80211: Allocate new address "
  4966. "for %s interface", type == WPA_IF_MESH ?
  4967. "mesh" : "P2P group");
  4968. if (nl80211_vif_addr(drv, new_addr) < 0) {
  4969. if (added)
  4970. nl80211_remove_iface(drv, ifidx);
  4971. return -1;
  4972. }
  4973. if (linux_set_ifhwaddr(drv->global->ioctl_sock, ifname,
  4974. new_addr) < 0) {
  4975. if (added)
  4976. nl80211_remove_iface(drv, ifidx);
  4977. return -1;
  4978. }
  4979. }
  4980. os_memcpy(if_addr, new_addr, ETH_ALEN);
  4981. }
  4982. #endif /* CONFIG_P2P || CONFIG_MESH */
  4983. if (type == WPA_IF_AP_BSS) {
  4984. struct i802_bss *new_bss = os_zalloc(sizeof(*new_bss));
  4985. if (new_bss == NULL) {
  4986. if (added)
  4987. nl80211_remove_iface(drv, ifidx);
  4988. return -1;
  4989. }
  4990. if (bridge &&
  4991. i802_check_bridge(drv, new_bss, bridge, ifname) < 0) {
  4992. wpa_printf(MSG_ERROR, "nl80211: Failed to add the new "
  4993. "interface %s to a bridge %s",
  4994. ifname, bridge);
  4995. if (added)
  4996. nl80211_remove_iface(drv, ifidx);
  4997. os_free(new_bss);
  4998. return -1;
  4999. }
  5000. if (linux_set_iface_flags(drv->global->ioctl_sock, ifname, 1))
  5001. {
  5002. if (added)
  5003. nl80211_remove_iface(drv, ifidx);
  5004. os_free(new_bss);
  5005. return -1;
  5006. }
  5007. os_strlcpy(new_bss->ifname, ifname, IFNAMSIZ);
  5008. os_memcpy(new_bss->addr, if_addr, ETH_ALEN);
  5009. new_bss->ifindex = ifidx;
  5010. new_bss->drv = drv;
  5011. new_bss->next = drv->first_bss->next;
  5012. new_bss->freq = drv->first_bss->freq;
  5013. new_bss->ctx = bss_ctx;
  5014. new_bss->added_if = added;
  5015. drv->first_bss->next = new_bss;
  5016. if (drv_priv)
  5017. *drv_priv = new_bss;
  5018. nl80211_init_bss(new_bss);
  5019. /* Subscribe management frames for this WPA_IF_AP_BSS */
  5020. if (nl80211_setup_ap(new_bss))
  5021. return -1;
  5022. }
  5023. if (drv->global)
  5024. drv->global->if_add_ifindex = ifidx;
  5025. /*
  5026. * Some virtual interfaces need to process EAPOL packets and events on
  5027. * the parent interface. This is used mainly with hostapd.
  5028. */
  5029. if (ifidx > 0 &&
  5030. (drv->hostapd ||
  5031. nlmode == NL80211_IFTYPE_AP_VLAN ||
  5032. nlmode == NL80211_IFTYPE_WDS ||
  5033. nlmode == NL80211_IFTYPE_MONITOR))
  5034. add_ifidx(drv, ifidx);
  5035. return 0;
  5036. }
  5037. static int wpa_driver_nl80211_if_remove(struct i802_bss *bss,
  5038. enum wpa_driver_if_type type,
  5039. const char *ifname)
  5040. {
  5041. struct wpa_driver_nl80211_data *drv = bss->drv;
  5042. int ifindex = if_nametoindex(ifname);
  5043. wpa_printf(MSG_DEBUG, "nl80211: %s(type=%d ifname=%s) ifindex=%d added_if=%d",
  5044. __func__, type, ifname, ifindex, bss->added_if);
  5045. if (ifindex > 0 && (bss->added_if || bss->ifindex != ifindex))
  5046. nl80211_remove_iface(drv, ifindex);
  5047. else if (ifindex > 0 && !bss->added_if) {
  5048. struct wpa_driver_nl80211_data *drv2;
  5049. dl_list_for_each(drv2, &drv->global->interfaces,
  5050. struct wpa_driver_nl80211_data, list)
  5051. del_ifidx(drv2, ifindex);
  5052. }
  5053. if (type != WPA_IF_AP_BSS)
  5054. return 0;
  5055. if (bss->added_if_into_bridge) {
  5056. if (linux_br_del_if(drv->global->ioctl_sock, bss->brname,
  5057. bss->ifname) < 0)
  5058. wpa_printf(MSG_INFO, "nl80211: Failed to remove "
  5059. "interface %s from bridge %s: %s",
  5060. bss->ifname, bss->brname, strerror(errno));
  5061. }
  5062. if (bss->added_bridge) {
  5063. if (linux_br_del(drv->global->ioctl_sock, bss->brname) < 0)
  5064. wpa_printf(MSG_INFO, "nl80211: Failed to remove "
  5065. "bridge %s: %s",
  5066. bss->brname, strerror(errno));
  5067. }
  5068. if (bss != drv->first_bss) {
  5069. struct i802_bss *tbss;
  5070. wpa_printf(MSG_DEBUG, "nl80211: Not the first BSS - remove it");
  5071. for (tbss = drv->first_bss; tbss; tbss = tbss->next) {
  5072. if (tbss->next == bss) {
  5073. tbss->next = bss->next;
  5074. /* Unsubscribe management frames */
  5075. nl80211_teardown_ap(bss);
  5076. nl80211_destroy_bss(bss);
  5077. if (!bss->added_if)
  5078. i802_set_iface_flags(bss, 0);
  5079. os_free(bss);
  5080. bss = NULL;
  5081. break;
  5082. }
  5083. }
  5084. if (bss)
  5085. wpa_printf(MSG_INFO, "nl80211: %s - could not find "
  5086. "BSS %p in the list", __func__, bss);
  5087. } else {
  5088. wpa_printf(MSG_DEBUG, "nl80211: First BSS - reassign context");
  5089. nl80211_teardown_ap(bss);
  5090. if (!bss->added_if && !drv->first_bss->next)
  5091. wpa_driver_nl80211_del_beacon(drv);
  5092. nl80211_destroy_bss(bss);
  5093. if (!bss->added_if)
  5094. i802_set_iface_flags(bss, 0);
  5095. if (drv->first_bss->next) {
  5096. drv->first_bss = drv->first_bss->next;
  5097. drv->ctx = drv->first_bss->ctx;
  5098. os_free(bss);
  5099. } else {
  5100. wpa_printf(MSG_DEBUG, "nl80211: No second BSS to reassign context to");
  5101. }
  5102. }
  5103. return 0;
  5104. }
  5105. static int cookie_handler(struct nl_msg *msg, void *arg)
  5106. {
  5107. struct nlattr *tb[NL80211_ATTR_MAX + 1];
  5108. struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
  5109. u64 *cookie = arg;
  5110. nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
  5111. genlmsg_attrlen(gnlh, 0), NULL);
  5112. if (tb[NL80211_ATTR_COOKIE])
  5113. *cookie = nla_get_u64(tb[NL80211_ATTR_COOKIE]);
  5114. return NL_SKIP;
  5115. }
  5116. static int nl80211_send_frame_cmd(struct i802_bss *bss,
  5117. unsigned int freq, unsigned int wait,
  5118. const u8 *buf, size_t buf_len,
  5119. u64 *cookie_out, int no_cck, int no_ack,
  5120. int offchanok)
  5121. {
  5122. struct wpa_driver_nl80211_data *drv = bss->drv;
  5123. struct nl_msg *msg;
  5124. u64 cookie;
  5125. int ret = -1;
  5126. wpa_printf(MSG_MSGDUMP, "nl80211: CMD_FRAME freq=%u wait=%u no_cck=%d "
  5127. "no_ack=%d offchanok=%d",
  5128. freq, wait, no_cck, no_ack, offchanok);
  5129. wpa_hexdump(MSG_MSGDUMP, "CMD_FRAME", buf, buf_len);
  5130. if (!(msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_FRAME)) ||
  5131. (freq && nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, freq)) ||
  5132. (wait && nla_put_u32(msg, NL80211_ATTR_DURATION, wait)) ||
  5133. (offchanok && ((drv->capa.flags & WPA_DRIVER_FLAGS_OFFCHANNEL_TX) ||
  5134. drv->test_use_roc_tx) &&
  5135. nla_put_flag(msg, NL80211_ATTR_OFFCHANNEL_TX_OK)) ||
  5136. (no_cck && nla_put_flag(msg, NL80211_ATTR_TX_NO_CCK_RATE)) ||
  5137. (no_ack && nla_put_flag(msg, NL80211_ATTR_DONT_WAIT_FOR_ACK)) ||
  5138. nla_put(msg, NL80211_ATTR_FRAME, buf_len, buf))
  5139. goto fail;
  5140. cookie = 0;
  5141. ret = send_and_recv_msgs(drv, msg, cookie_handler, &cookie);
  5142. msg = NULL;
  5143. if (ret) {
  5144. wpa_printf(MSG_DEBUG, "nl80211: Frame command failed: ret=%d "
  5145. "(%s) (freq=%u wait=%u)", ret, strerror(-ret),
  5146. freq, wait);
  5147. } else {
  5148. wpa_printf(MSG_MSGDUMP, "nl80211: Frame TX command accepted%s; "
  5149. "cookie 0x%llx", no_ack ? " (no ACK)" : "",
  5150. (long long unsigned int) cookie);
  5151. if (cookie_out)
  5152. *cookie_out = no_ack ? (u64) -1 : cookie;
  5153. }
  5154. fail:
  5155. nlmsg_free(msg);
  5156. return ret;
  5157. }
  5158. static int wpa_driver_nl80211_send_action(struct i802_bss *bss,
  5159. unsigned int freq,
  5160. unsigned int wait_time,
  5161. const u8 *dst, const u8 *src,
  5162. const u8 *bssid,
  5163. const u8 *data, size_t data_len,
  5164. int no_cck)
  5165. {
  5166. struct wpa_driver_nl80211_data *drv = bss->drv;
  5167. int ret = -1;
  5168. u8 *buf;
  5169. struct ieee80211_hdr *hdr;
  5170. wpa_printf(MSG_DEBUG, "nl80211: Send Action frame (ifindex=%d, "
  5171. "freq=%u MHz wait=%d ms no_cck=%d)",
  5172. drv->ifindex, freq, wait_time, no_cck);
  5173. buf = os_zalloc(24 + data_len);
  5174. if (buf == NULL)
  5175. return ret;
  5176. os_memcpy(buf + 24, data, data_len);
  5177. hdr = (struct ieee80211_hdr *) buf;
  5178. hdr->frame_control =
  5179. IEEE80211_FC(WLAN_FC_TYPE_MGMT, WLAN_FC_STYPE_ACTION);
  5180. os_memcpy(hdr->addr1, dst, ETH_ALEN);
  5181. os_memcpy(hdr->addr2, src, ETH_ALEN);
  5182. os_memcpy(hdr->addr3, bssid, ETH_ALEN);
  5183. if (is_ap_interface(drv->nlmode) &&
  5184. (!(drv->capa.flags & WPA_DRIVER_FLAGS_OFFCHANNEL_TX) ||
  5185. (int) freq == bss->freq || drv->device_ap_sme ||
  5186. !drv->use_monitor))
  5187. ret = wpa_driver_nl80211_send_mlme(bss, buf, 24 + data_len,
  5188. 0, freq, no_cck, 1,
  5189. wait_time);
  5190. else
  5191. ret = nl80211_send_frame_cmd(bss, freq, wait_time, buf,
  5192. 24 + data_len,
  5193. &drv->send_action_cookie,
  5194. no_cck, 0, 1);
  5195. os_free(buf);
  5196. return ret;
  5197. }
  5198. static void wpa_driver_nl80211_send_action_cancel_wait(void *priv)
  5199. {
  5200. struct i802_bss *bss = priv;
  5201. struct wpa_driver_nl80211_data *drv = bss->drv;
  5202. struct nl_msg *msg;
  5203. int ret;
  5204. wpa_printf(MSG_DEBUG, "nl80211: Cancel TX frame wait: cookie=0x%llx",
  5205. (long long unsigned int) drv->send_action_cookie);
  5206. if (!(msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_FRAME_WAIT_CANCEL)) ||
  5207. nla_put_u64(msg, NL80211_ATTR_COOKIE, drv->send_action_cookie)) {
  5208. nlmsg_free(msg);
  5209. return;
  5210. }
  5211. ret = send_and_recv_msgs(drv, msg, NULL, NULL);
  5212. if (ret)
  5213. wpa_printf(MSG_DEBUG, "nl80211: wait cancel failed: ret=%d "
  5214. "(%s)", ret, strerror(-ret));
  5215. }
  5216. static int wpa_driver_nl80211_remain_on_channel(void *priv, unsigned int freq,
  5217. unsigned int duration)
  5218. {
  5219. struct i802_bss *bss = priv;
  5220. struct wpa_driver_nl80211_data *drv = bss->drv;
  5221. struct nl_msg *msg;
  5222. int ret;
  5223. u64 cookie;
  5224. if (!(msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_REMAIN_ON_CHANNEL)) ||
  5225. nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, freq) ||
  5226. nla_put_u32(msg, NL80211_ATTR_DURATION, duration)) {
  5227. nlmsg_free(msg);
  5228. return -1;
  5229. }
  5230. cookie = 0;
  5231. ret = send_and_recv_msgs(drv, msg, cookie_handler, &cookie);
  5232. if (ret == 0) {
  5233. wpa_printf(MSG_DEBUG, "nl80211: Remain-on-channel cookie "
  5234. "0x%llx for freq=%u MHz duration=%u",
  5235. (long long unsigned int) cookie, freq, duration);
  5236. drv->remain_on_chan_cookie = cookie;
  5237. drv->pending_remain_on_chan = 1;
  5238. return 0;
  5239. }
  5240. wpa_printf(MSG_DEBUG, "nl80211: Failed to request remain-on-channel "
  5241. "(freq=%d duration=%u): %d (%s)",
  5242. freq, duration, ret, strerror(-ret));
  5243. return -1;
  5244. }
  5245. static int wpa_driver_nl80211_cancel_remain_on_channel(void *priv)
  5246. {
  5247. struct i802_bss *bss = priv;
  5248. struct wpa_driver_nl80211_data *drv = bss->drv;
  5249. struct nl_msg *msg;
  5250. int ret;
  5251. if (!drv->pending_remain_on_chan) {
  5252. wpa_printf(MSG_DEBUG, "nl80211: No pending remain-on-channel "
  5253. "to cancel");
  5254. return -1;
  5255. }
  5256. wpa_printf(MSG_DEBUG, "nl80211: Cancel remain-on-channel with cookie "
  5257. "0x%llx",
  5258. (long long unsigned int) drv->remain_on_chan_cookie);
  5259. msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL);
  5260. if (!msg ||
  5261. nla_put_u64(msg, NL80211_ATTR_COOKIE, drv->remain_on_chan_cookie)) {
  5262. nlmsg_free(msg);
  5263. return -1;
  5264. }
  5265. ret = send_and_recv_msgs(drv, msg, NULL, NULL);
  5266. if (ret == 0)
  5267. return 0;
  5268. wpa_printf(MSG_DEBUG, "nl80211: Failed to cancel remain-on-channel: "
  5269. "%d (%s)", ret, strerror(-ret));
  5270. return -1;
  5271. }
  5272. static int wpa_driver_nl80211_probe_req_report(struct i802_bss *bss, int report)
  5273. {
  5274. struct wpa_driver_nl80211_data *drv = bss->drv;
  5275. if (!report) {
  5276. if (bss->nl_preq && drv->device_ap_sme &&
  5277. is_ap_interface(drv->nlmode) && !bss->in_deinit &&
  5278. !bss->static_ap) {
  5279. /*
  5280. * Do not disable Probe Request reporting that was
  5281. * enabled in nl80211_setup_ap().
  5282. */
  5283. wpa_printf(MSG_DEBUG, "nl80211: Skip disabling of "
  5284. "Probe Request reporting nl_preq=%p while "
  5285. "in AP mode", bss->nl_preq);
  5286. } else if (bss->nl_preq) {
  5287. wpa_printf(MSG_DEBUG, "nl80211: Disable Probe Request "
  5288. "reporting nl_preq=%p", bss->nl_preq);
  5289. nl80211_destroy_eloop_handle(&bss->nl_preq);
  5290. }
  5291. return 0;
  5292. }
  5293. if (bss->nl_preq) {
  5294. wpa_printf(MSG_DEBUG, "nl80211: Probe Request reporting "
  5295. "already on! nl_preq=%p", bss->nl_preq);
  5296. return 0;
  5297. }
  5298. bss->nl_preq = nl_create_handle(drv->global->nl_cb, "preq");
  5299. if (bss->nl_preq == NULL)
  5300. return -1;
  5301. wpa_printf(MSG_DEBUG, "nl80211: Enable Probe Request "
  5302. "reporting nl_preq=%p", bss->nl_preq);
  5303. if (nl80211_register_frame(bss, bss->nl_preq,
  5304. (WLAN_FC_TYPE_MGMT << 2) |
  5305. (WLAN_FC_STYPE_PROBE_REQ << 4),
  5306. NULL, 0) < 0)
  5307. goto out_err;
  5308. nl80211_register_eloop_read(&bss->nl_preq,
  5309. wpa_driver_nl80211_event_receive,
  5310. bss->nl_cb);
  5311. return 0;
  5312. out_err:
  5313. nl_destroy_handles(&bss->nl_preq);
  5314. return -1;
  5315. }
  5316. static int nl80211_disable_11b_rates(struct wpa_driver_nl80211_data *drv,
  5317. int ifindex, int disabled)
  5318. {
  5319. struct nl_msg *msg;
  5320. struct nlattr *bands, *band;
  5321. int ret;
  5322. wpa_printf(MSG_DEBUG,
  5323. "nl80211: NL80211_CMD_SET_TX_BITRATE_MASK (ifindex=%d %s)",
  5324. ifindex, disabled ? "NL80211_TXRATE_LEGACY=OFDM-only" :
  5325. "no NL80211_TXRATE_LEGACY constraint");
  5326. msg = nl80211_ifindex_msg(drv, ifindex, 0,
  5327. NL80211_CMD_SET_TX_BITRATE_MASK);
  5328. if (!msg)
  5329. return -1;
  5330. bands = nla_nest_start(msg, NL80211_ATTR_TX_RATES);
  5331. if (!bands)
  5332. goto fail;
  5333. /*
  5334. * Disable 2 GHz rates 1, 2, 5.5, 11 Mbps by masking out everything
  5335. * else apart from 6, 9, 12, 18, 24, 36, 48, 54 Mbps from non-MCS
  5336. * rates. All 5 GHz rates are left enabled.
  5337. */
  5338. band = nla_nest_start(msg, NL80211_BAND_2GHZ);
  5339. if (!band ||
  5340. (disabled && nla_put(msg, NL80211_TXRATE_LEGACY, 8,
  5341. "\x0c\x12\x18\x24\x30\x48\x60\x6c")))
  5342. goto fail;
  5343. nla_nest_end(msg, band);
  5344. nla_nest_end(msg, bands);
  5345. ret = send_and_recv_msgs(drv, msg, NULL, NULL);
  5346. if (ret) {
  5347. wpa_printf(MSG_DEBUG, "nl80211: Set TX rates failed: ret=%d "
  5348. "(%s)", ret, strerror(-ret));
  5349. } else
  5350. drv->disabled_11b_rates = disabled;
  5351. return ret;
  5352. fail:
  5353. nlmsg_free(msg);
  5354. return -1;
  5355. }
  5356. static int wpa_driver_nl80211_deinit_ap(void *priv)
  5357. {
  5358. struct i802_bss *bss = priv;
  5359. struct wpa_driver_nl80211_data *drv = bss->drv;
  5360. if (!is_ap_interface(drv->nlmode))
  5361. return -1;
  5362. wpa_driver_nl80211_del_beacon(drv);
  5363. bss->beacon_set = 0;
  5364. /*
  5365. * If the P2P GO interface was dynamically added, then it is
  5366. * possible that the interface change to station is not possible.
  5367. */
  5368. if (drv->nlmode == NL80211_IFTYPE_P2P_GO && bss->if_dynamic)
  5369. return 0;
  5370. return wpa_driver_nl80211_set_mode(priv, NL80211_IFTYPE_STATION);
  5371. }
  5372. static int wpa_driver_nl80211_stop_ap(void *priv)
  5373. {
  5374. struct i802_bss *bss = priv;
  5375. struct wpa_driver_nl80211_data *drv = bss->drv;
  5376. if (!is_ap_interface(drv->nlmode))
  5377. return -1;
  5378. wpa_driver_nl80211_del_beacon(drv);
  5379. bss->beacon_set = 0;
  5380. return 0;
  5381. }
  5382. static int wpa_driver_nl80211_deinit_p2p_cli(void *priv)
  5383. {
  5384. struct i802_bss *bss = priv;
  5385. struct wpa_driver_nl80211_data *drv = bss->drv;
  5386. if (drv->nlmode != NL80211_IFTYPE_P2P_CLIENT)
  5387. return -1;
  5388. /*
  5389. * If the P2P Client interface was dynamically added, then it is
  5390. * possible that the interface change to station is not possible.
  5391. */
  5392. if (bss->if_dynamic)
  5393. return 0;
  5394. return wpa_driver_nl80211_set_mode(priv, NL80211_IFTYPE_STATION);
  5395. }
  5396. static void wpa_driver_nl80211_resume(void *priv)
  5397. {
  5398. struct i802_bss *bss = priv;
  5399. if (i802_set_iface_flags(bss, 1))
  5400. wpa_printf(MSG_DEBUG, "nl80211: Failed to set interface up on resume event");
  5401. }
  5402. static int nl80211_signal_monitor(void *priv, int threshold, int hysteresis)
  5403. {
  5404. struct i802_bss *bss = priv;
  5405. struct wpa_driver_nl80211_data *drv = bss->drv;
  5406. struct nl_msg *msg;
  5407. struct nlattr *cqm;
  5408. wpa_printf(MSG_DEBUG, "nl80211: Signal monitor threshold=%d "
  5409. "hysteresis=%d", threshold, hysteresis);
  5410. if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_SET_CQM)) ||
  5411. !(cqm = nla_nest_start(msg, NL80211_ATTR_CQM)) ||
  5412. nla_put_u32(msg, NL80211_ATTR_CQM_RSSI_THOLD, threshold) ||
  5413. nla_put_u32(msg, NL80211_ATTR_CQM_RSSI_HYST, hysteresis)) {
  5414. nlmsg_free(msg);
  5415. return -1;
  5416. }
  5417. nla_nest_end(msg, cqm);
  5418. return send_and_recv_msgs(drv, msg, NULL, NULL);
  5419. }
  5420. static int get_channel_width(struct nl_msg *msg, void *arg)
  5421. {
  5422. struct nlattr *tb[NL80211_ATTR_MAX + 1];
  5423. struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
  5424. struct wpa_signal_info *sig_change = arg;
  5425. nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
  5426. genlmsg_attrlen(gnlh, 0), NULL);
  5427. sig_change->center_frq1 = -1;
  5428. sig_change->center_frq2 = -1;
  5429. sig_change->chanwidth = CHAN_WIDTH_UNKNOWN;
  5430. if (tb[NL80211_ATTR_CHANNEL_WIDTH]) {
  5431. sig_change->chanwidth = convert2width(
  5432. nla_get_u32(tb[NL80211_ATTR_CHANNEL_WIDTH]));
  5433. if (tb[NL80211_ATTR_CENTER_FREQ1])
  5434. sig_change->center_frq1 =
  5435. nla_get_u32(tb[NL80211_ATTR_CENTER_FREQ1]);
  5436. if (tb[NL80211_ATTR_CENTER_FREQ2])
  5437. sig_change->center_frq2 =
  5438. nla_get_u32(tb[NL80211_ATTR_CENTER_FREQ2]);
  5439. }
  5440. return NL_SKIP;
  5441. }
  5442. static int nl80211_get_channel_width(struct wpa_driver_nl80211_data *drv,
  5443. struct wpa_signal_info *sig)
  5444. {
  5445. struct nl_msg *msg;
  5446. msg = nl80211_drv_msg(drv, 0, NL80211_CMD_GET_INTERFACE);
  5447. return send_and_recv_msgs(drv, msg, get_channel_width, sig);
  5448. }
  5449. static int nl80211_signal_poll(void *priv, struct wpa_signal_info *si)
  5450. {
  5451. struct i802_bss *bss = priv;
  5452. struct wpa_driver_nl80211_data *drv = bss->drv;
  5453. int res;
  5454. os_memset(si, 0, sizeof(*si));
  5455. res = nl80211_get_link_signal(drv, si);
  5456. if (res != 0)
  5457. return res;
  5458. res = nl80211_get_channel_width(drv, si);
  5459. if (res != 0)
  5460. return res;
  5461. return nl80211_get_link_noise(drv, si);
  5462. }
  5463. static int wpa_driver_nl80211_shared_freq(void *priv)
  5464. {
  5465. struct i802_bss *bss = priv;
  5466. struct wpa_driver_nl80211_data *drv = bss->drv;
  5467. struct wpa_driver_nl80211_data *driver;
  5468. int freq = 0;
  5469. /*
  5470. * If the same PHY is in connected state with some other interface,
  5471. * then retrieve the assoc freq.
  5472. */
  5473. wpa_printf(MSG_DEBUG, "nl80211: Get shared freq for PHY %s",
  5474. drv->phyname);
  5475. dl_list_for_each(driver, &drv->global->interfaces,
  5476. struct wpa_driver_nl80211_data, list) {
  5477. if (drv == driver ||
  5478. os_strcmp(drv->phyname, driver->phyname) != 0 ||
  5479. !driver->associated)
  5480. continue;
  5481. wpa_printf(MSG_DEBUG, "nl80211: Found a match for PHY %s - %s "
  5482. MACSTR,
  5483. driver->phyname, driver->first_bss->ifname,
  5484. MAC2STR(driver->first_bss->addr));
  5485. if (is_ap_interface(driver->nlmode))
  5486. freq = driver->first_bss->freq;
  5487. else
  5488. freq = nl80211_get_assoc_freq(driver);
  5489. wpa_printf(MSG_DEBUG, "nl80211: Shared freq for PHY %s: %d",
  5490. drv->phyname, freq);
  5491. }
  5492. if (!freq)
  5493. wpa_printf(MSG_DEBUG, "nl80211: No shared interface for "
  5494. "PHY (%s) in associated state", drv->phyname);
  5495. return freq;
  5496. }
  5497. static int nl80211_send_frame(void *priv, const u8 *data, size_t data_len,
  5498. int encrypt)
  5499. {
  5500. struct i802_bss *bss = priv;
  5501. return wpa_driver_nl80211_send_frame(bss, data, data_len, encrypt, 0,
  5502. 0, 0, 0, 0);
  5503. }
  5504. static int nl80211_set_param(void *priv, const char *param)
  5505. {
  5506. wpa_printf(MSG_DEBUG, "nl80211: driver param='%s'", param);
  5507. if (param == NULL)
  5508. return 0;
  5509. #ifdef CONFIG_P2P
  5510. if (os_strstr(param, "use_p2p_group_interface=1")) {
  5511. struct i802_bss *bss = priv;
  5512. struct wpa_driver_nl80211_data *drv = bss->drv;
  5513. wpa_printf(MSG_DEBUG, "nl80211: Use separate P2P group "
  5514. "interface");
  5515. drv->capa.flags |= WPA_DRIVER_FLAGS_P2P_CONCURRENT;
  5516. drv->capa.flags |= WPA_DRIVER_FLAGS_P2P_MGMT_AND_NON_P2P;
  5517. }
  5518. #endif /* CONFIG_P2P */
  5519. if (os_strstr(param, "use_monitor=1")) {
  5520. struct i802_bss *bss = priv;
  5521. struct wpa_driver_nl80211_data *drv = bss->drv;
  5522. drv->use_monitor = 1;
  5523. }
  5524. if (os_strstr(param, "force_connect_cmd=1")) {
  5525. struct i802_bss *bss = priv;
  5526. struct wpa_driver_nl80211_data *drv = bss->drv;
  5527. drv->capa.flags &= ~WPA_DRIVER_FLAGS_SME;
  5528. drv->force_connect_cmd = 1;
  5529. }
  5530. if (os_strstr(param, "no_offchannel_tx=1")) {
  5531. struct i802_bss *bss = priv;
  5532. struct wpa_driver_nl80211_data *drv = bss->drv;
  5533. drv->capa.flags &= ~WPA_DRIVER_FLAGS_OFFCHANNEL_TX;
  5534. drv->test_use_roc_tx = 1;
  5535. }
  5536. return 0;
  5537. }
  5538. static void * nl80211_global_init(void)
  5539. {
  5540. struct nl80211_global *global;
  5541. struct netlink_config *cfg;
  5542. global = os_zalloc(sizeof(*global));
  5543. if (global == NULL)
  5544. return NULL;
  5545. global->ioctl_sock = -1;
  5546. dl_list_init(&global->interfaces);
  5547. global->if_add_ifindex = -1;
  5548. cfg = os_zalloc(sizeof(*cfg));
  5549. if (cfg == NULL)
  5550. goto err;
  5551. cfg->ctx = global;
  5552. cfg->newlink_cb = wpa_driver_nl80211_event_rtm_newlink;
  5553. cfg->dellink_cb = wpa_driver_nl80211_event_rtm_dellink;
  5554. global->netlink = netlink_init(cfg);
  5555. if (global->netlink == NULL) {
  5556. os_free(cfg);
  5557. goto err;
  5558. }
  5559. if (wpa_driver_nl80211_init_nl_global(global) < 0)
  5560. goto err;
  5561. global->ioctl_sock = socket(PF_INET, SOCK_DGRAM, 0);
  5562. if (global->ioctl_sock < 0) {
  5563. wpa_printf(MSG_ERROR, "nl80211: socket(PF_INET,SOCK_DGRAM) failed: %s",
  5564. strerror(errno));
  5565. goto err;
  5566. }
  5567. return global;
  5568. err:
  5569. nl80211_global_deinit(global);
  5570. return NULL;
  5571. }
  5572. static void nl80211_global_deinit(void *priv)
  5573. {
  5574. struct nl80211_global *global = priv;
  5575. if (global == NULL)
  5576. return;
  5577. if (!dl_list_empty(&global->interfaces)) {
  5578. wpa_printf(MSG_ERROR, "nl80211: %u interface(s) remain at "
  5579. "nl80211_global_deinit",
  5580. dl_list_len(&global->interfaces));
  5581. }
  5582. if (global->netlink)
  5583. netlink_deinit(global->netlink);
  5584. nl_destroy_handles(&global->nl);
  5585. if (global->nl_event)
  5586. nl80211_destroy_eloop_handle(&global->nl_event);
  5587. nl_cb_put(global->nl_cb);
  5588. if (global->ioctl_sock >= 0)
  5589. close(global->ioctl_sock);
  5590. os_free(global);
  5591. }
  5592. static const char * nl80211_get_radio_name(void *priv)
  5593. {
  5594. struct i802_bss *bss = priv;
  5595. struct wpa_driver_nl80211_data *drv = bss->drv;
  5596. return drv->phyname;
  5597. }
  5598. static int nl80211_pmkid(struct i802_bss *bss, int cmd, const u8 *bssid,
  5599. const u8 *pmkid)
  5600. {
  5601. struct nl_msg *msg;
  5602. if (!(msg = nl80211_bss_msg(bss, 0, cmd)) ||
  5603. (pmkid && nla_put(msg, NL80211_ATTR_PMKID, 16, pmkid)) ||
  5604. (bssid && nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid))) {
  5605. nlmsg_free(msg);
  5606. return -ENOBUFS;
  5607. }
  5608. return send_and_recv_msgs(bss->drv, msg, NULL, NULL);
  5609. }
  5610. static int nl80211_add_pmkid(void *priv, const u8 *bssid, const u8 *pmkid)
  5611. {
  5612. struct i802_bss *bss = priv;
  5613. wpa_printf(MSG_DEBUG, "nl80211: Add PMKID for " MACSTR, MAC2STR(bssid));
  5614. return nl80211_pmkid(bss, NL80211_CMD_SET_PMKSA, bssid, pmkid);
  5615. }
  5616. static int nl80211_remove_pmkid(void *priv, const u8 *bssid, const u8 *pmkid)
  5617. {
  5618. struct i802_bss *bss = priv;
  5619. wpa_printf(MSG_DEBUG, "nl80211: Delete PMKID for " MACSTR,
  5620. MAC2STR(bssid));
  5621. return nl80211_pmkid(bss, NL80211_CMD_DEL_PMKSA, bssid, pmkid);
  5622. }
  5623. static int nl80211_flush_pmkid(void *priv)
  5624. {
  5625. struct i802_bss *bss = priv;
  5626. wpa_printf(MSG_DEBUG, "nl80211: Flush PMKIDs");
  5627. return nl80211_pmkid(bss, NL80211_CMD_FLUSH_PMKSA, NULL, NULL);
  5628. }
  5629. static void clean_survey_results(struct survey_results *survey_results)
  5630. {
  5631. struct freq_survey *survey, *tmp;
  5632. if (dl_list_empty(&survey_results->survey_list))
  5633. return;
  5634. dl_list_for_each_safe(survey, tmp, &survey_results->survey_list,
  5635. struct freq_survey, list) {
  5636. dl_list_del(&survey->list);
  5637. os_free(survey);
  5638. }
  5639. }
  5640. static void add_survey(struct nlattr **sinfo, u32 ifidx,
  5641. struct dl_list *survey_list)
  5642. {
  5643. struct freq_survey *survey;
  5644. survey = os_zalloc(sizeof(struct freq_survey));
  5645. if (!survey)
  5646. return;
  5647. survey->ifidx = ifidx;
  5648. survey->freq = nla_get_u32(sinfo[NL80211_SURVEY_INFO_FREQUENCY]);
  5649. survey->filled = 0;
  5650. if (sinfo[NL80211_SURVEY_INFO_NOISE]) {
  5651. survey->nf = (int8_t)
  5652. nla_get_u8(sinfo[NL80211_SURVEY_INFO_NOISE]);
  5653. survey->filled |= SURVEY_HAS_NF;
  5654. }
  5655. if (sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME]) {
  5656. survey->channel_time =
  5657. nla_get_u64(sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME]);
  5658. survey->filled |= SURVEY_HAS_CHAN_TIME;
  5659. }
  5660. if (sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME_BUSY]) {
  5661. survey->channel_time_busy =
  5662. nla_get_u64(sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME_BUSY]);
  5663. survey->filled |= SURVEY_HAS_CHAN_TIME_BUSY;
  5664. }
  5665. if (sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME_RX]) {
  5666. survey->channel_time_rx =
  5667. nla_get_u64(sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME_RX]);
  5668. survey->filled |= SURVEY_HAS_CHAN_TIME_RX;
  5669. }
  5670. if (sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME_TX]) {
  5671. survey->channel_time_tx =
  5672. nla_get_u64(sinfo[NL80211_SURVEY_INFO_CHANNEL_TIME_TX]);
  5673. survey->filled |= SURVEY_HAS_CHAN_TIME_TX;
  5674. }
  5675. wpa_printf(MSG_DEBUG, "nl80211: Freq survey dump event (freq=%d MHz noise=%d channel_time=%ld busy_time=%ld tx_time=%ld rx_time=%ld filled=%04x)",
  5676. survey->freq,
  5677. survey->nf,
  5678. (unsigned long int) survey->channel_time,
  5679. (unsigned long int) survey->channel_time_busy,
  5680. (unsigned long int) survey->channel_time_tx,
  5681. (unsigned long int) survey->channel_time_rx,
  5682. survey->filled);
  5683. dl_list_add_tail(survey_list, &survey->list);
  5684. }
  5685. static int check_survey_ok(struct nlattr **sinfo, u32 surveyed_freq,
  5686. unsigned int freq_filter)
  5687. {
  5688. if (!freq_filter)
  5689. return 1;
  5690. return freq_filter == surveyed_freq;
  5691. }
  5692. static int survey_handler(struct nl_msg *msg, void *arg)
  5693. {
  5694. struct nlattr *tb[NL80211_ATTR_MAX + 1];
  5695. struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
  5696. struct nlattr *sinfo[NL80211_SURVEY_INFO_MAX + 1];
  5697. struct survey_results *survey_results;
  5698. u32 surveyed_freq = 0;
  5699. u32 ifidx;
  5700. static struct nla_policy survey_policy[NL80211_SURVEY_INFO_MAX + 1] = {
  5701. [NL80211_SURVEY_INFO_FREQUENCY] = { .type = NLA_U32 },
  5702. [NL80211_SURVEY_INFO_NOISE] = { .type = NLA_U8 },
  5703. };
  5704. survey_results = (struct survey_results *) arg;
  5705. nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
  5706. genlmsg_attrlen(gnlh, 0), NULL);
  5707. if (!tb[NL80211_ATTR_IFINDEX])
  5708. return NL_SKIP;
  5709. ifidx = nla_get_u32(tb[NL80211_ATTR_IFINDEX]);
  5710. if (!tb[NL80211_ATTR_SURVEY_INFO])
  5711. return NL_SKIP;
  5712. if (nla_parse_nested(sinfo, NL80211_SURVEY_INFO_MAX,
  5713. tb[NL80211_ATTR_SURVEY_INFO],
  5714. survey_policy))
  5715. return NL_SKIP;
  5716. if (!sinfo[NL80211_SURVEY_INFO_FREQUENCY]) {
  5717. wpa_printf(MSG_ERROR, "nl80211: Invalid survey data");
  5718. return NL_SKIP;
  5719. }
  5720. surveyed_freq = nla_get_u32(sinfo[NL80211_SURVEY_INFO_FREQUENCY]);
  5721. if (!check_survey_ok(sinfo, surveyed_freq,
  5722. survey_results->freq_filter))
  5723. return NL_SKIP;
  5724. if (survey_results->freq_filter &&
  5725. survey_results->freq_filter != surveyed_freq) {
  5726. wpa_printf(MSG_EXCESSIVE, "nl80211: Ignoring survey data for freq %d MHz",
  5727. surveyed_freq);
  5728. return NL_SKIP;
  5729. }
  5730. add_survey(sinfo, ifidx, &survey_results->survey_list);
  5731. return NL_SKIP;
  5732. }
  5733. static int wpa_driver_nl80211_get_survey(void *priv, unsigned int freq)
  5734. {
  5735. struct i802_bss *bss = priv;
  5736. struct wpa_driver_nl80211_data *drv = bss->drv;
  5737. struct nl_msg *msg;
  5738. int err;
  5739. union wpa_event_data data;
  5740. struct survey_results *survey_results;
  5741. os_memset(&data, 0, sizeof(data));
  5742. survey_results = &data.survey_results;
  5743. dl_list_init(&survey_results->survey_list);
  5744. msg = nl80211_drv_msg(drv, NLM_F_DUMP, NL80211_CMD_GET_SURVEY);
  5745. if (!msg)
  5746. return -ENOBUFS;
  5747. if (freq)
  5748. data.survey_results.freq_filter = freq;
  5749. do {
  5750. wpa_printf(MSG_DEBUG, "nl80211: Fetch survey data");
  5751. err = send_and_recv_msgs(drv, msg, survey_handler,
  5752. survey_results);
  5753. } while (err > 0);
  5754. if (err)
  5755. wpa_printf(MSG_ERROR, "nl80211: Failed to process survey data");
  5756. else
  5757. wpa_supplicant_event(drv->ctx, EVENT_SURVEY, &data);
  5758. clean_survey_results(survey_results);
  5759. return err;
  5760. }
  5761. static void nl80211_set_rekey_info(void *priv, const u8 *kek, size_t kek_len,
  5762. const u8 *kck, size_t kck_len,
  5763. const u8 *replay_ctr)
  5764. {
  5765. struct i802_bss *bss = priv;
  5766. struct wpa_driver_nl80211_data *drv = bss->drv;
  5767. struct nlattr *replay_nested;
  5768. struct nl_msg *msg;
  5769. int ret;
  5770. if (!drv->set_rekey_offload)
  5771. return;
  5772. wpa_printf(MSG_DEBUG, "nl80211: Set rekey offload");
  5773. if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_SET_REKEY_OFFLOAD)) ||
  5774. !(replay_nested = nla_nest_start(msg, NL80211_ATTR_REKEY_DATA)) ||
  5775. nla_put(msg, NL80211_REKEY_DATA_KEK, kek_len, kek) ||
  5776. nla_put(msg, NL80211_REKEY_DATA_KCK, kck_len, kck) ||
  5777. nla_put(msg, NL80211_REKEY_DATA_REPLAY_CTR, NL80211_REPLAY_CTR_LEN,
  5778. replay_ctr)) {
  5779. nl80211_nlmsg_clear(msg);
  5780. nlmsg_free(msg);
  5781. return;
  5782. }
  5783. nla_nest_end(msg, replay_nested);
  5784. ret = send_and_recv_msgs(drv, msg, NULL, (void *) -1);
  5785. if (ret == -EOPNOTSUPP) {
  5786. wpa_printf(MSG_DEBUG,
  5787. "nl80211: Driver does not support rekey offload");
  5788. drv->set_rekey_offload = 0;
  5789. }
  5790. }
  5791. static void nl80211_send_null_frame(struct i802_bss *bss, const u8 *own_addr,
  5792. const u8 *addr, int qos)
  5793. {
  5794. /* send data frame to poll STA and check whether
  5795. * this frame is ACKed */
  5796. struct {
  5797. struct ieee80211_hdr hdr;
  5798. u16 qos_ctl;
  5799. } STRUCT_PACKED nulldata;
  5800. size_t size;
  5801. /* Send data frame to poll STA and check whether this frame is ACKed */
  5802. os_memset(&nulldata, 0, sizeof(nulldata));
  5803. if (qos) {
  5804. nulldata.hdr.frame_control =
  5805. IEEE80211_FC(WLAN_FC_TYPE_DATA,
  5806. WLAN_FC_STYPE_QOS_NULL);
  5807. size = sizeof(nulldata);
  5808. } else {
  5809. nulldata.hdr.frame_control =
  5810. IEEE80211_FC(WLAN_FC_TYPE_DATA,
  5811. WLAN_FC_STYPE_NULLFUNC);
  5812. size = sizeof(struct ieee80211_hdr);
  5813. }
  5814. nulldata.hdr.frame_control |= host_to_le16(WLAN_FC_FROMDS);
  5815. os_memcpy(nulldata.hdr.IEEE80211_DA_FROMDS, addr, ETH_ALEN);
  5816. os_memcpy(nulldata.hdr.IEEE80211_BSSID_FROMDS, own_addr, ETH_ALEN);
  5817. os_memcpy(nulldata.hdr.IEEE80211_SA_FROMDS, own_addr, ETH_ALEN);
  5818. if (wpa_driver_nl80211_send_mlme(bss, (u8 *) &nulldata, size, 0, 0, 0,
  5819. 0, 0) < 0)
  5820. wpa_printf(MSG_DEBUG, "nl80211_send_null_frame: Failed to "
  5821. "send poll frame");
  5822. }
  5823. static void nl80211_poll_client(void *priv, const u8 *own_addr, const u8 *addr,
  5824. int qos)
  5825. {
  5826. struct i802_bss *bss = priv;
  5827. struct wpa_driver_nl80211_data *drv = bss->drv;
  5828. struct nl_msg *msg;
  5829. int ret;
  5830. if (!drv->poll_command_supported) {
  5831. nl80211_send_null_frame(bss, own_addr, addr, qos);
  5832. return;
  5833. }
  5834. if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_PROBE_CLIENT)) ||
  5835. nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr)) {
  5836. nlmsg_free(msg);
  5837. return;
  5838. }
  5839. ret = send_and_recv_msgs(drv, msg, NULL, NULL);
  5840. if (ret < 0) {
  5841. wpa_printf(MSG_DEBUG, "nl80211: Client probe request for "
  5842. MACSTR " failed: ret=%d (%s)",
  5843. MAC2STR(addr), ret, strerror(-ret));
  5844. }
  5845. }
  5846. static int nl80211_set_power_save(struct i802_bss *bss, int enabled)
  5847. {
  5848. struct nl_msg *msg;
  5849. if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_SET_POWER_SAVE)) ||
  5850. nla_put_u32(msg, NL80211_ATTR_PS_STATE,
  5851. enabled ? NL80211_PS_ENABLED : NL80211_PS_DISABLED)) {
  5852. nlmsg_free(msg);
  5853. return -ENOBUFS;
  5854. }
  5855. return send_and_recv_msgs(bss->drv, msg, NULL, NULL);
  5856. }
  5857. static int nl80211_set_p2p_powersave(void *priv, int legacy_ps, int opp_ps,
  5858. int ctwindow)
  5859. {
  5860. struct i802_bss *bss = priv;
  5861. wpa_printf(MSG_DEBUG, "nl80211: set_p2p_powersave (legacy_ps=%d "
  5862. "opp_ps=%d ctwindow=%d)", legacy_ps, opp_ps, ctwindow);
  5863. if (opp_ps != -1 || ctwindow != -1) {
  5864. #ifdef ANDROID_P2P
  5865. wpa_driver_set_p2p_ps(priv, legacy_ps, opp_ps, ctwindow);
  5866. #else /* ANDROID_P2P */
  5867. return -1; /* Not yet supported */
  5868. #endif /* ANDROID_P2P */
  5869. }
  5870. if (legacy_ps == -1)
  5871. return 0;
  5872. if (legacy_ps != 0 && legacy_ps != 1)
  5873. return -1; /* Not yet supported */
  5874. return nl80211_set_power_save(bss, legacy_ps);
  5875. }
  5876. static int nl80211_start_radar_detection(void *priv,
  5877. struct hostapd_freq_params *freq)
  5878. {
  5879. struct i802_bss *bss = priv;
  5880. struct wpa_driver_nl80211_data *drv = bss->drv;
  5881. struct nl_msg *msg;
  5882. int ret;
  5883. wpa_printf(MSG_DEBUG, "nl80211: Start radar detection (CAC) %d MHz (ht_enabled=%d, vht_enabled=%d, bandwidth=%d MHz, cf1=%d MHz, cf2=%d MHz)",
  5884. freq->freq, freq->ht_enabled, freq->vht_enabled,
  5885. freq->bandwidth, freq->center_freq1, freq->center_freq2);
  5886. if (!(drv->capa.flags & WPA_DRIVER_FLAGS_RADAR)) {
  5887. wpa_printf(MSG_DEBUG, "nl80211: Driver does not support radar "
  5888. "detection");
  5889. return -1;
  5890. }
  5891. if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_RADAR_DETECT)) ||
  5892. nl80211_put_freq_params(msg, freq) < 0) {
  5893. nlmsg_free(msg);
  5894. return -1;
  5895. }
  5896. ret = send_and_recv_msgs(drv, msg, NULL, NULL);
  5897. if (ret == 0)
  5898. return 0;
  5899. wpa_printf(MSG_DEBUG, "nl80211: Failed to start radar detection: "
  5900. "%d (%s)", ret, strerror(-ret));
  5901. return -1;
  5902. }
  5903. #ifdef CONFIG_TDLS
  5904. static int nl80211_send_tdls_mgmt(void *priv, const u8 *dst, u8 action_code,
  5905. u8 dialog_token, u16 status_code,
  5906. u32 peer_capab, int initiator, const u8 *buf,
  5907. size_t len)
  5908. {
  5909. struct i802_bss *bss = priv;
  5910. struct wpa_driver_nl80211_data *drv = bss->drv;
  5911. struct nl_msg *msg;
  5912. if (!(drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT))
  5913. return -EOPNOTSUPP;
  5914. if (!dst)
  5915. return -EINVAL;
  5916. if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_TDLS_MGMT)) ||
  5917. nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, dst) ||
  5918. nla_put_u8(msg, NL80211_ATTR_TDLS_ACTION, action_code) ||
  5919. nla_put_u8(msg, NL80211_ATTR_TDLS_DIALOG_TOKEN, dialog_token) ||
  5920. nla_put_u16(msg, NL80211_ATTR_STATUS_CODE, status_code))
  5921. goto fail;
  5922. if (peer_capab) {
  5923. /*
  5924. * The internal enum tdls_peer_capability definition is
  5925. * currently identical with the nl80211 enum
  5926. * nl80211_tdls_peer_capability, so no conversion is needed
  5927. * here.
  5928. */
  5929. if (nla_put_u32(msg, NL80211_ATTR_TDLS_PEER_CAPABILITY,
  5930. peer_capab))
  5931. goto fail;
  5932. }
  5933. if ((initiator &&
  5934. nla_put_flag(msg, NL80211_ATTR_TDLS_INITIATOR)) ||
  5935. nla_put(msg, NL80211_ATTR_IE, len, buf))
  5936. goto fail;
  5937. return send_and_recv_msgs(drv, msg, NULL, NULL);
  5938. fail:
  5939. nlmsg_free(msg);
  5940. return -ENOBUFS;
  5941. }
  5942. static int nl80211_tdls_oper(void *priv, enum tdls_oper oper, const u8 *peer)
  5943. {
  5944. struct i802_bss *bss = priv;
  5945. struct wpa_driver_nl80211_data *drv = bss->drv;
  5946. struct nl_msg *msg;
  5947. enum nl80211_tdls_operation nl80211_oper;
  5948. if (!(drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT))
  5949. return -EOPNOTSUPP;
  5950. switch (oper) {
  5951. case TDLS_DISCOVERY_REQ:
  5952. nl80211_oper = NL80211_TDLS_DISCOVERY_REQ;
  5953. break;
  5954. case TDLS_SETUP:
  5955. nl80211_oper = NL80211_TDLS_SETUP;
  5956. break;
  5957. case TDLS_TEARDOWN:
  5958. nl80211_oper = NL80211_TDLS_TEARDOWN;
  5959. break;
  5960. case TDLS_ENABLE_LINK:
  5961. nl80211_oper = NL80211_TDLS_ENABLE_LINK;
  5962. break;
  5963. case TDLS_DISABLE_LINK:
  5964. nl80211_oper = NL80211_TDLS_DISABLE_LINK;
  5965. break;
  5966. case TDLS_ENABLE:
  5967. return 0;
  5968. case TDLS_DISABLE:
  5969. return 0;
  5970. default:
  5971. return -EINVAL;
  5972. }
  5973. if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_TDLS_OPER)) ||
  5974. nla_put_u8(msg, NL80211_ATTR_TDLS_OPERATION, nl80211_oper) ||
  5975. nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, peer)) {
  5976. nlmsg_free(msg);
  5977. return -ENOBUFS;
  5978. }
  5979. return send_and_recv_msgs(drv, msg, NULL, NULL);
  5980. }
  5981. static int
  5982. nl80211_tdls_enable_channel_switch(void *priv, const u8 *addr, u8 oper_class,
  5983. const struct hostapd_freq_params *params)
  5984. {
  5985. struct i802_bss *bss = priv;
  5986. struct wpa_driver_nl80211_data *drv = bss->drv;
  5987. struct nl_msg *msg;
  5988. int ret = -ENOBUFS;
  5989. if (!(drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT) ||
  5990. !(drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_CHANNEL_SWITCH))
  5991. return -EOPNOTSUPP;
  5992. wpa_printf(MSG_DEBUG, "nl80211: Enable TDLS channel switch " MACSTR
  5993. " oper_class=%u freq=%u",
  5994. MAC2STR(addr), oper_class, params->freq);
  5995. msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_TDLS_CHANNEL_SWITCH);
  5996. if (!msg ||
  5997. nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr) ||
  5998. nla_put_u8(msg, NL80211_ATTR_OPER_CLASS, oper_class) ||
  5999. (ret = nl80211_put_freq_params(msg, params))) {
  6000. nlmsg_free(msg);
  6001. wpa_printf(MSG_DEBUG, "nl80211: Could not build TDLS chan switch");
  6002. return ret;
  6003. }
  6004. return send_and_recv_msgs(drv, msg, NULL, NULL);
  6005. }
  6006. static int
  6007. nl80211_tdls_disable_channel_switch(void *priv, const u8 *addr)
  6008. {
  6009. struct i802_bss *bss = priv;
  6010. struct wpa_driver_nl80211_data *drv = bss->drv;
  6011. struct nl_msg *msg;
  6012. if (!(drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT) ||
  6013. !(drv->capa.flags & WPA_DRIVER_FLAGS_TDLS_CHANNEL_SWITCH))
  6014. return -EOPNOTSUPP;
  6015. wpa_printf(MSG_DEBUG, "nl80211: Disable TDLS channel switch " MACSTR,
  6016. MAC2STR(addr));
  6017. msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_TDLS_CANCEL_CHANNEL_SWITCH);
  6018. if (!msg ||
  6019. nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr)) {
  6020. nlmsg_free(msg);
  6021. wpa_printf(MSG_DEBUG,
  6022. "nl80211: Could not build TDLS cancel chan switch");
  6023. return -ENOBUFS;
  6024. }
  6025. return send_and_recv_msgs(drv, msg, NULL, NULL);
  6026. }
  6027. #endif /* CONFIG TDLS */
  6028. static int driver_nl80211_set_key(const char *ifname, void *priv,
  6029. enum wpa_alg alg, const u8 *addr,
  6030. int key_idx, int set_tx,
  6031. const u8 *seq, size_t seq_len,
  6032. const u8 *key, size_t key_len)
  6033. {
  6034. struct i802_bss *bss = priv;
  6035. return wpa_driver_nl80211_set_key(ifname, bss, alg, addr, key_idx,
  6036. set_tx, seq, seq_len, key, key_len);
  6037. }
  6038. static int driver_nl80211_scan2(void *priv,
  6039. struct wpa_driver_scan_params *params)
  6040. {
  6041. struct i802_bss *bss = priv;
  6042. return wpa_driver_nl80211_scan(bss, params);
  6043. }
  6044. static int driver_nl80211_deauthenticate(void *priv, const u8 *addr,
  6045. int reason_code)
  6046. {
  6047. struct i802_bss *bss = priv;
  6048. return wpa_driver_nl80211_deauthenticate(bss, addr, reason_code);
  6049. }
  6050. static int driver_nl80211_authenticate(void *priv,
  6051. struct wpa_driver_auth_params *params)
  6052. {
  6053. struct i802_bss *bss = priv;
  6054. return wpa_driver_nl80211_authenticate(bss, params);
  6055. }
  6056. static void driver_nl80211_deinit(void *priv)
  6057. {
  6058. struct i802_bss *bss = priv;
  6059. wpa_driver_nl80211_deinit(bss);
  6060. }
  6061. static int driver_nl80211_if_remove(void *priv, enum wpa_driver_if_type type,
  6062. const char *ifname)
  6063. {
  6064. struct i802_bss *bss = priv;
  6065. return wpa_driver_nl80211_if_remove(bss, type, ifname);
  6066. }
  6067. static int driver_nl80211_send_mlme(void *priv, const u8 *data,
  6068. size_t data_len, int noack)
  6069. {
  6070. struct i802_bss *bss = priv;
  6071. return wpa_driver_nl80211_send_mlme(bss, data, data_len, noack,
  6072. 0, 0, 0, 0);
  6073. }
  6074. static int driver_nl80211_sta_remove(void *priv, const u8 *addr)
  6075. {
  6076. struct i802_bss *bss = priv;
  6077. return wpa_driver_nl80211_sta_remove(bss, addr, -1, 0);
  6078. }
  6079. static int driver_nl80211_set_sta_vlan(void *priv, const u8 *addr,
  6080. const char *ifname, int vlan_id)
  6081. {
  6082. struct i802_bss *bss = priv;
  6083. return i802_set_sta_vlan(bss, addr, ifname, vlan_id);
  6084. }
  6085. static int driver_nl80211_read_sta_data(void *priv,
  6086. struct hostap_sta_driver_data *data,
  6087. const u8 *addr)
  6088. {
  6089. struct i802_bss *bss = priv;
  6090. return i802_read_sta_data(bss, data, addr);
  6091. }
  6092. static int driver_nl80211_send_action(void *priv, unsigned int freq,
  6093. unsigned int wait_time,
  6094. const u8 *dst, const u8 *src,
  6095. const u8 *bssid,
  6096. const u8 *data, size_t data_len,
  6097. int no_cck)
  6098. {
  6099. struct i802_bss *bss = priv;
  6100. return wpa_driver_nl80211_send_action(bss, freq, wait_time, dst, src,
  6101. bssid, data, data_len, no_cck);
  6102. }
  6103. static int driver_nl80211_probe_req_report(void *priv, int report)
  6104. {
  6105. struct i802_bss *bss = priv;
  6106. return wpa_driver_nl80211_probe_req_report(bss, report);
  6107. }
  6108. static int wpa_driver_nl80211_update_ft_ies(void *priv, const u8 *md,
  6109. const u8 *ies, size_t ies_len)
  6110. {
  6111. int ret;
  6112. struct nl_msg *msg;
  6113. struct i802_bss *bss = priv;
  6114. struct wpa_driver_nl80211_data *drv = bss->drv;
  6115. u16 mdid = WPA_GET_LE16(md);
  6116. wpa_printf(MSG_DEBUG, "nl80211: Updating FT IEs");
  6117. if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_UPDATE_FT_IES)) ||
  6118. nla_put(msg, NL80211_ATTR_IE, ies_len, ies) ||
  6119. nla_put_u16(msg, NL80211_ATTR_MDID, mdid)) {
  6120. nlmsg_free(msg);
  6121. return -ENOBUFS;
  6122. }
  6123. ret = send_and_recv_msgs(drv, msg, NULL, NULL);
  6124. if (ret) {
  6125. wpa_printf(MSG_DEBUG, "nl80211: update_ft_ies failed "
  6126. "err=%d (%s)", ret, strerror(-ret));
  6127. }
  6128. return ret;
  6129. }
  6130. const u8 * wpa_driver_nl80211_get_macaddr(void *priv)
  6131. {
  6132. struct i802_bss *bss = priv;
  6133. struct wpa_driver_nl80211_data *drv = bss->drv;
  6134. if (drv->nlmode != NL80211_IFTYPE_P2P_DEVICE)
  6135. return NULL;
  6136. return bss->addr;
  6137. }
  6138. static const char * scan_state_str(enum scan_states scan_state)
  6139. {
  6140. switch (scan_state) {
  6141. case NO_SCAN:
  6142. return "NO_SCAN";
  6143. case SCAN_REQUESTED:
  6144. return "SCAN_REQUESTED";
  6145. case SCAN_STARTED:
  6146. return "SCAN_STARTED";
  6147. case SCAN_COMPLETED:
  6148. return "SCAN_COMPLETED";
  6149. case SCAN_ABORTED:
  6150. return "SCAN_ABORTED";
  6151. case SCHED_SCAN_STARTED:
  6152. return "SCHED_SCAN_STARTED";
  6153. case SCHED_SCAN_STOPPED:
  6154. return "SCHED_SCAN_STOPPED";
  6155. case SCHED_SCAN_RESULTS:
  6156. return "SCHED_SCAN_RESULTS";
  6157. }
  6158. return "??";
  6159. }
  6160. static int wpa_driver_nl80211_status(void *priv, char *buf, size_t buflen)
  6161. {
  6162. struct i802_bss *bss = priv;
  6163. struct wpa_driver_nl80211_data *drv = bss->drv;
  6164. int res;
  6165. char *pos, *end;
  6166. pos = buf;
  6167. end = buf + buflen;
  6168. res = os_snprintf(pos, end - pos,
  6169. "ifindex=%d\n"
  6170. "ifname=%s\n"
  6171. "brname=%s\n"
  6172. "addr=" MACSTR "\n"
  6173. "freq=%d\n"
  6174. "%s%s%s%s%s",
  6175. bss->ifindex,
  6176. bss->ifname,
  6177. bss->brname,
  6178. MAC2STR(bss->addr),
  6179. bss->freq,
  6180. bss->beacon_set ? "beacon_set=1\n" : "",
  6181. bss->added_if_into_bridge ?
  6182. "added_if_into_bridge=1\n" : "",
  6183. bss->added_bridge ? "added_bridge=1\n" : "",
  6184. bss->in_deinit ? "in_deinit=1\n" : "",
  6185. bss->if_dynamic ? "if_dynamic=1\n" : "");
  6186. if (os_snprintf_error(end - pos, res))
  6187. return pos - buf;
  6188. pos += res;
  6189. if (bss->wdev_id_set) {
  6190. res = os_snprintf(pos, end - pos, "wdev_id=%llu\n",
  6191. (unsigned long long) bss->wdev_id);
  6192. if (os_snprintf_error(end - pos, res))
  6193. return pos - buf;
  6194. pos += res;
  6195. }
  6196. res = os_snprintf(pos, end - pos,
  6197. "phyname=%s\n"
  6198. "perm_addr=" MACSTR "\n"
  6199. "drv_ifindex=%d\n"
  6200. "operstate=%d\n"
  6201. "scan_state=%s\n"
  6202. "auth_bssid=" MACSTR "\n"
  6203. "auth_attempt_bssid=" MACSTR "\n"
  6204. "bssid=" MACSTR "\n"
  6205. "prev_bssid=" MACSTR "\n"
  6206. "associated=%d\n"
  6207. "assoc_freq=%u\n"
  6208. "monitor_sock=%d\n"
  6209. "monitor_ifidx=%d\n"
  6210. "monitor_refcount=%d\n"
  6211. "last_mgmt_freq=%u\n"
  6212. "eapol_tx_sock=%d\n"
  6213. "%s%s%s%s%s%s%s%s%s%s%s%s%s",
  6214. drv->phyname,
  6215. MAC2STR(drv->perm_addr),
  6216. drv->ifindex,
  6217. drv->operstate,
  6218. scan_state_str(drv->scan_state),
  6219. MAC2STR(drv->auth_bssid),
  6220. MAC2STR(drv->auth_attempt_bssid),
  6221. MAC2STR(drv->bssid),
  6222. MAC2STR(drv->prev_bssid),
  6223. drv->associated,
  6224. drv->assoc_freq,
  6225. drv->monitor_sock,
  6226. drv->monitor_ifidx,
  6227. drv->monitor_refcount,
  6228. drv->last_mgmt_freq,
  6229. drv->eapol_tx_sock,
  6230. drv->ignore_if_down_event ?
  6231. "ignore_if_down_event=1\n" : "",
  6232. drv->scan_complete_events ?
  6233. "scan_complete_events=1\n" : "",
  6234. drv->disabled_11b_rates ?
  6235. "disabled_11b_rates=1\n" : "",
  6236. drv->pending_remain_on_chan ?
  6237. "pending_remain_on_chan=1\n" : "",
  6238. drv->in_interface_list ? "in_interface_list=1\n" : "",
  6239. drv->device_ap_sme ? "device_ap_sme=1\n" : "",
  6240. drv->poll_command_supported ?
  6241. "poll_command_supported=1\n" : "",
  6242. drv->data_tx_status ? "data_tx_status=1\n" : "",
  6243. drv->scan_for_auth ? "scan_for_auth=1\n" : "",
  6244. drv->retry_auth ? "retry_auth=1\n" : "",
  6245. drv->use_monitor ? "use_monitor=1\n" : "",
  6246. drv->ignore_next_local_disconnect ?
  6247. "ignore_next_local_disconnect=1\n" : "",
  6248. drv->ignore_next_local_deauth ?
  6249. "ignore_next_local_deauth=1\n" : "");
  6250. if (os_snprintf_error(end - pos, res))
  6251. return pos - buf;
  6252. pos += res;
  6253. if (drv->has_capability) {
  6254. res = os_snprintf(pos, end - pos,
  6255. "capa.key_mgmt=0x%x\n"
  6256. "capa.enc=0x%x\n"
  6257. "capa.auth=0x%x\n"
  6258. "capa.flags=0x%llx\n"
  6259. "capa.rrm_flags=0x%x\n"
  6260. "capa.max_scan_ssids=%d\n"
  6261. "capa.max_sched_scan_ssids=%d\n"
  6262. "capa.sched_scan_supported=%d\n"
  6263. "capa.max_match_sets=%d\n"
  6264. "capa.max_remain_on_chan=%u\n"
  6265. "capa.max_stations=%u\n"
  6266. "capa.probe_resp_offloads=0x%x\n"
  6267. "capa.max_acl_mac_addrs=%u\n"
  6268. "capa.num_multichan_concurrent=%u\n"
  6269. "capa.mac_addr_rand_sched_scan_supported=%d\n"
  6270. "capa.mac_addr_rand_scan_supported=%d\n",
  6271. drv->capa.key_mgmt,
  6272. drv->capa.enc,
  6273. drv->capa.auth,
  6274. (unsigned long long) drv->capa.flags,
  6275. drv->capa.rrm_flags,
  6276. drv->capa.max_scan_ssids,
  6277. drv->capa.max_sched_scan_ssids,
  6278. drv->capa.sched_scan_supported,
  6279. drv->capa.max_match_sets,
  6280. drv->capa.max_remain_on_chan,
  6281. drv->capa.max_stations,
  6282. drv->capa.probe_resp_offloads,
  6283. drv->capa.max_acl_mac_addrs,
  6284. drv->capa.num_multichan_concurrent,
  6285. drv->capa.mac_addr_rand_sched_scan_supported,
  6286. drv->capa.mac_addr_rand_scan_supported);
  6287. if (os_snprintf_error(end - pos, res))
  6288. return pos - buf;
  6289. pos += res;
  6290. }
  6291. return pos - buf;
  6292. }
  6293. static int set_beacon_data(struct nl_msg *msg, struct beacon_data *settings)
  6294. {
  6295. if ((settings->head &&
  6296. nla_put(msg, NL80211_ATTR_BEACON_HEAD,
  6297. settings->head_len, settings->head)) ||
  6298. (settings->tail &&
  6299. nla_put(msg, NL80211_ATTR_BEACON_TAIL,
  6300. settings->tail_len, settings->tail)) ||
  6301. (settings->beacon_ies &&
  6302. nla_put(msg, NL80211_ATTR_IE,
  6303. settings->beacon_ies_len, settings->beacon_ies)) ||
  6304. (settings->proberesp_ies &&
  6305. nla_put(msg, NL80211_ATTR_IE_PROBE_RESP,
  6306. settings->proberesp_ies_len, settings->proberesp_ies)) ||
  6307. (settings->assocresp_ies &&
  6308. nla_put(msg, NL80211_ATTR_IE_ASSOC_RESP,
  6309. settings->assocresp_ies_len, settings->assocresp_ies)) ||
  6310. (settings->probe_resp &&
  6311. nla_put(msg, NL80211_ATTR_PROBE_RESP,
  6312. settings->probe_resp_len, settings->probe_resp)))
  6313. return -ENOBUFS;
  6314. return 0;
  6315. }
  6316. static int nl80211_switch_channel(void *priv, struct csa_settings *settings)
  6317. {
  6318. struct nl_msg *msg;
  6319. struct i802_bss *bss = priv;
  6320. struct wpa_driver_nl80211_data *drv = bss->drv;
  6321. struct nlattr *beacon_csa;
  6322. int ret = -ENOBUFS;
  6323. wpa_printf(MSG_DEBUG, "nl80211: Channel switch request (cs_count=%u block_tx=%u freq=%d width=%d cf1=%d cf2=%d)",
  6324. settings->cs_count, settings->block_tx,
  6325. settings->freq_params.freq, settings->freq_params.bandwidth,
  6326. settings->freq_params.center_freq1,
  6327. settings->freq_params.center_freq2);
  6328. if (!(drv->capa.flags & WPA_DRIVER_FLAGS_AP_CSA)) {
  6329. wpa_printf(MSG_DEBUG, "nl80211: Driver does not support channel switch command");
  6330. return -EOPNOTSUPP;
  6331. }
  6332. if ((drv->nlmode != NL80211_IFTYPE_AP) &&
  6333. (drv->nlmode != NL80211_IFTYPE_P2P_GO))
  6334. return -EOPNOTSUPP;
  6335. /* check settings validity */
  6336. if (!settings->beacon_csa.tail ||
  6337. ((settings->beacon_csa.tail_len <=
  6338. settings->counter_offset_beacon) ||
  6339. (settings->beacon_csa.tail[settings->counter_offset_beacon] !=
  6340. settings->cs_count)))
  6341. return -EINVAL;
  6342. if (settings->beacon_csa.probe_resp &&
  6343. ((settings->beacon_csa.probe_resp_len <=
  6344. settings->counter_offset_presp) ||
  6345. (settings->beacon_csa.probe_resp[settings->counter_offset_presp] !=
  6346. settings->cs_count)))
  6347. return -EINVAL;
  6348. if (!(msg = nl80211_bss_msg(bss, 0, NL80211_CMD_CHANNEL_SWITCH)) ||
  6349. nla_put_u32(msg, NL80211_ATTR_CH_SWITCH_COUNT,
  6350. settings->cs_count) ||
  6351. (ret = nl80211_put_freq_params(msg, &settings->freq_params)) ||
  6352. (settings->block_tx &&
  6353. nla_put_flag(msg, NL80211_ATTR_CH_SWITCH_BLOCK_TX)))
  6354. goto error;
  6355. /* beacon_after params */
  6356. ret = set_beacon_data(msg, &settings->beacon_after);
  6357. if (ret)
  6358. goto error;
  6359. /* beacon_csa params */
  6360. beacon_csa = nla_nest_start(msg, NL80211_ATTR_CSA_IES);
  6361. if (!beacon_csa)
  6362. goto fail;
  6363. ret = set_beacon_data(msg, &settings->beacon_csa);
  6364. if (ret)
  6365. goto error;
  6366. if (nla_put_u16(msg, NL80211_ATTR_CSA_C_OFF_BEACON,
  6367. settings->counter_offset_beacon) ||
  6368. (settings->beacon_csa.probe_resp &&
  6369. nla_put_u16(msg, NL80211_ATTR_CSA_C_OFF_PRESP,
  6370. settings->counter_offset_presp)))
  6371. goto fail;
  6372. nla_nest_end(msg, beacon_csa);
  6373. ret = send_and_recv_msgs(drv, msg, NULL, NULL);
  6374. if (ret) {
  6375. wpa_printf(MSG_DEBUG, "nl80211: switch_channel failed err=%d (%s)",
  6376. ret, strerror(-ret));
  6377. }
  6378. return ret;
  6379. fail:
  6380. ret = -ENOBUFS;
  6381. error:
  6382. nlmsg_free(msg);
  6383. wpa_printf(MSG_DEBUG, "nl80211: Could not build channel switch request");
  6384. return ret;
  6385. }
  6386. static int nl80211_add_ts(void *priv, u8 tsid, const u8 *addr,
  6387. u8 user_priority, u16 admitted_time)
  6388. {
  6389. struct i802_bss *bss = priv;
  6390. struct wpa_driver_nl80211_data *drv = bss->drv;
  6391. struct nl_msg *msg;
  6392. int ret;
  6393. wpa_printf(MSG_DEBUG,
  6394. "nl80211: add_ts request: tsid=%u admitted_time=%u up=%d",
  6395. tsid, admitted_time, user_priority);
  6396. if (!is_sta_interface(drv->nlmode))
  6397. return -ENOTSUP;
  6398. msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_ADD_TX_TS);
  6399. if (!msg ||
  6400. nla_put_u8(msg, NL80211_ATTR_TSID, tsid) ||
  6401. nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr) ||
  6402. nla_put_u8(msg, NL80211_ATTR_USER_PRIO, user_priority) ||
  6403. nla_put_u16(msg, NL80211_ATTR_ADMITTED_TIME, admitted_time)) {
  6404. nlmsg_free(msg);
  6405. return -ENOBUFS;
  6406. }
  6407. ret = send_and_recv_msgs(drv, msg, NULL, NULL);
  6408. if (ret)
  6409. wpa_printf(MSG_DEBUG, "nl80211: add_ts failed err=%d (%s)",
  6410. ret, strerror(-ret));
  6411. return ret;
  6412. }
  6413. static int nl80211_del_ts(void *priv, u8 tsid, const u8 *addr)
  6414. {
  6415. struct i802_bss *bss = priv;
  6416. struct wpa_driver_nl80211_data *drv = bss->drv;
  6417. struct nl_msg *msg;
  6418. int ret;
  6419. wpa_printf(MSG_DEBUG, "nl80211: del_ts request: tsid=%u", tsid);
  6420. if (!is_sta_interface(drv->nlmode))
  6421. return -ENOTSUP;
  6422. if (!(msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_DEL_TX_TS)) ||
  6423. nla_put_u8(msg, NL80211_ATTR_TSID, tsid) ||
  6424. nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr)) {
  6425. nlmsg_free(msg);
  6426. return -ENOBUFS;
  6427. }
  6428. ret = send_and_recv_msgs(drv, msg, NULL, NULL);
  6429. if (ret)
  6430. wpa_printf(MSG_DEBUG, "nl80211: del_ts failed err=%d (%s)",
  6431. ret, strerror(-ret));
  6432. return ret;
  6433. }
  6434. #ifdef CONFIG_TESTING_OPTIONS
  6435. static int cmd_reply_handler(struct nl_msg *msg, void *arg)
  6436. {
  6437. struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
  6438. struct wpabuf *buf = arg;
  6439. if (!buf)
  6440. return NL_SKIP;
  6441. if ((size_t) genlmsg_attrlen(gnlh, 0) > wpabuf_tailroom(buf)) {
  6442. wpa_printf(MSG_INFO, "nl80211: insufficient buffer space for reply");
  6443. return NL_SKIP;
  6444. }
  6445. wpabuf_put_data(buf, genlmsg_attrdata(gnlh, 0),
  6446. genlmsg_attrlen(gnlh, 0));
  6447. return NL_SKIP;
  6448. }
  6449. #endif /* CONFIG_TESTING_OPTIONS */
  6450. static int vendor_reply_handler(struct nl_msg *msg, void *arg)
  6451. {
  6452. struct nlattr *tb[NL80211_ATTR_MAX + 1];
  6453. struct nlattr *nl_vendor_reply, *nl;
  6454. struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
  6455. struct wpabuf *buf = arg;
  6456. int rem;
  6457. if (!buf)
  6458. return NL_SKIP;
  6459. nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
  6460. genlmsg_attrlen(gnlh, 0), NULL);
  6461. nl_vendor_reply = tb[NL80211_ATTR_VENDOR_DATA];
  6462. if (!nl_vendor_reply)
  6463. return NL_SKIP;
  6464. if ((size_t) nla_len(nl_vendor_reply) > wpabuf_tailroom(buf)) {
  6465. wpa_printf(MSG_INFO, "nl80211: Vendor command: insufficient buffer space for reply");
  6466. return NL_SKIP;
  6467. }
  6468. nla_for_each_nested(nl, nl_vendor_reply, rem) {
  6469. wpabuf_put_data(buf, nla_data(nl), nla_len(nl));
  6470. }
  6471. return NL_SKIP;
  6472. }
  6473. static int nl80211_vendor_cmd(void *priv, unsigned int vendor_id,
  6474. unsigned int subcmd, const u8 *data,
  6475. size_t data_len, struct wpabuf *buf)
  6476. {
  6477. struct i802_bss *bss = priv;
  6478. struct wpa_driver_nl80211_data *drv = bss->drv;
  6479. struct nl_msg *msg;
  6480. int ret;
  6481. #ifdef CONFIG_TESTING_OPTIONS
  6482. if (vendor_id == 0xffffffff) {
  6483. msg = nlmsg_alloc();
  6484. if (!msg)
  6485. return -ENOMEM;
  6486. nl80211_cmd(drv, msg, 0, subcmd);
  6487. if (nlmsg_append(msg, (void *) data, data_len, NLMSG_ALIGNTO) <
  6488. 0)
  6489. goto fail;
  6490. ret = send_and_recv_msgs(drv, msg, cmd_reply_handler, buf);
  6491. if (ret)
  6492. wpa_printf(MSG_DEBUG, "nl80211: command failed err=%d",
  6493. ret);
  6494. return ret;
  6495. }
  6496. #endif /* CONFIG_TESTING_OPTIONS */
  6497. if (!(msg = nl80211_cmd_msg(bss, 0, NL80211_CMD_VENDOR)) ||
  6498. nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, vendor_id) ||
  6499. nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD, subcmd) ||
  6500. (data &&
  6501. nla_put(msg, NL80211_ATTR_VENDOR_DATA, data_len, data)))
  6502. goto fail;
  6503. ret = send_and_recv_msgs(drv, msg, vendor_reply_handler, buf);
  6504. if (ret)
  6505. wpa_printf(MSG_DEBUG, "nl80211: vendor command failed err=%d",
  6506. ret);
  6507. return ret;
  6508. fail:
  6509. nlmsg_free(msg);
  6510. return -ENOBUFS;
  6511. }
  6512. static int nl80211_set_qos_map(void *priv, const u8 *qos_map_set,
  6513. u8 qos_map_set_len)
  6514. {
  6515. struct i802_bss *bss = priv;
  6516. struct wpa_driver_nl80211_data *drv = bss->drv;
  6517. struct nl_msg *msg;
  6518. int ret;
  6519. wpa_hexdump(MSG_DEBUG, "nl80211: Setting QoS Map",
  6520. qos_map_set, qos_map_set_len);
  6521. if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_SET_QOS_MAP)) ||
  6522. nla_put(msg, NL80211_ATTR_QOS_MAP, qos_map_set_len, qos_map_set)) {
  6523. nlmsg_free(msg);
  6524. return -ENOBUFS;
  6525. }
  6526. ret = send_and_recv_msgs(drv, msg, NULL, NULL);
  6527. if (ret)
  6528. wpa_printf(MSG_DEBUG, "nl80211: Setting QoS Map failed");
  6529. return ret;
  6530. }
  6531. static int nl80211_set_wowlan(void *priv,
  6532. const struct wowlan_triggers *triggers)
  6533. {
  6534. struct i802_bss *bss = priv;
  6535. struct wpa_driver_nl80211_data *drv = bss->drv;
  6536. struct nl_msg *msg;
  6537. struct nlattr *wowlan_triggers;
  6538. int ret;
  6539. wpa_printf(MSG_DEBUG, "nl80211: Setting wowlan");
  6540. if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_SET_WOWLAN)) ||
  6541. !(wowlan_triggers = nla_nest_start(msg,
  6542. NL80211_ATTR_WOWLAN_TRIGGERS)) ||
  6543. (triggers->any &&
  6544. nla_put_flag(msg, NL80211_WOWLAN_TRIG_ANY)) ||
  6545. (triggers->disconnect &&
  6546. nla_put_flag(msg, NL80211_WOWLAN_TRIG_DISCONNECT)) ||
  6547. (triggers->magic_pkt &&
  6548. nla_put_flag(msg, NL80211_WOWLAN_TRIG_MAGIC_PKT)) ||
  6549. (triggers->gtk_rekey_failure &&
  6550. nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE)) ||
  6551. (triggers->eap_identity_req &&
  6552. nla_put_flag(msg, NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST)) ||
  6553. (triggers->four_way_handshake &&
  6554. nla_put_flag(msg, NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE)) ||
  6555. (triggers->rfkill_release &&
  6556. nla_put_flag(msg, NL80211_WOWLAN_TRIG_RFKILL_RELEASE))) {
  6557. nlmsg_free(msg);
  6558. return -ENOBUFS;
  6559. }
  6560. nla_nest_end(msg, wowlan_triggers);
  6561. ret = send_and_recv_msgs(drv, msg, NULL, NULL);
  6562. if (ret)
  6563. wpa_printf(MSG_DEBUG, "nl80211: Setting wowlan failed");
  6564. return ret;
  6565. }
  6566. static int nl80211_roaming(void *priv, int allowed, const u8 *bssid)
  6567. {
  6568. struct i802_bss *bss = priv;
  6569. struct wpa_driver_nl80211_data *drv = bss->drv;
  6570. struct nl_msg *msg;
  6571. struct nlattr *params;
  6572. wpa_printf(MSG_DEBUG, "nl80211: Roaming policy: allowed=%d", allowed);
  6573. if (!drv->roaming_vendor_cmd_avail) {
  6574. wpa_printf(MSG_DEBUG,
  6575. "nl80211: Ignore roaming policy change since driver does not provide command for setting it");
  6576. return -1;
  6577. }
  6578. if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_VENDOR)) ||
  6579. nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, OUI_QCA) ||
  6580. nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD,
  6581. QCA_NL80211_VENDOR_SUBCMD_ROAMING) ||
  6582. !(params = nla_nest_start(msg, NL80211_ATTR_VENDOR_DATA)) ||
  6583. nla_put_u32(msg, QCA_WLAN_VENDOR_ATTR_ROAMING_POLICY,
  6584. allowed ? QCA_ROAMING_ALLOWED_WITHIN_ESS :
  6585. QCA_ROAMING_NOT_ALLOWED) ||
  6586. (bssid &&
  6587. nla_put(msg, QCA_WLAN_VENDOR_ATTR_MAC_ADDR, ETH_ALEN, bssid))) {
  6588. nlmsg_free(msg);
  6589. return -1;
  6590. }
  6591. nla_nest_end(msg, params);
  6592. return send_and_recv_msgs(drv, msg, NULL, NULL);
  6593. }
  6594. static int nl80211_set_mac_addr(void *priv, const u8 *addr)
  6595. {
  6596. struct i802_bss *bss = priv;
  6597. struct wpa_driver_nl80211_data *drv = bss->drv;
  6598. int new_addr = addr != NULL;
  6599. if (!addr)
  6600. addr = drv->perm_addr;
  6601. if (linux_set_iface_flags(drv->global->ioctl_sock, bss->ifname, 0) < 0)
  6602. return -1;
  6603. if (linux_set_ifhwaddr(drv->global->ioctl_sock, bss->ifname, addr) < 0)
  6604. {
  6605. wpa_printf(MSG_DEBUG,
  6606. "nl80211: failed to set_mac_addr for %s to " MACSTR,
  6607. bss->ifname, MAC2STR(addr));
  6608. if (linux_set_iface_flags(drv->global->ioctl_sock, bss->ifname,
  6609. 1) < 0) {
  6610. wpa_printf(MSG_DEBUG,
  6611. "nl80211: Could not restore interface UP after failed set_mac_addr");
  6612. }
  6613. return -1;
  6614. }
  6615. wpa_printf(MSG_DEBUG, "nl80211: set_mac_addr for %s to " MACSTR,
  6616. bss->ifname, MAC2STR(addr));
  6617. drv->addr_changed = new_addr;
  6618. os_memcpy(bss->addr, addr, ETH_ALEN);
  6619. if (linux_set_iface_flags(drv->global->ioctl_sock, bss->ifname, 1) < 0)
  6620. {
  6621. wpa_printf(MSG_DEBUG,
  6622. "nl80211: Could not restore interface UP after set_mac_addr");
  6623. }
  6624. return 0;
  6625. }
  6626. #ifdef CONFIG_MESH
  6627. static int wpa_driver_nl80211_init_mesh(void *priv)
  6628. {
  6629. if (wpa_driver_nl80211_set_mode(priv, NL80211_IFTYPE_MESH_POINT)) {
  6630. wpa_printf(MSG_INFO,
  6631. "nl80211: Failed to set interface into mesh mode");
  6632. return -1;
  6633. }
  6634. return 0;
  6635. }
  6636. static int nl80211_put_mesh_id(struct nl_msg *msg, const u8 *mesh_id,
  6637. size_t mesh_id_len)
  6638. {
  6639. if (mesh_id) {
  6640. wpa_hexdump_ascii(MSG_DEBUG, " * Mesh ID (SSID)",
  6641. mesh_id, mesh_id_len);
  6642. return nla_put(msg, NL80211_ATTR_MESH_ID, mesh_id_len, mesh_id);
  6643. }
  6644. return 0;
  6645. }
  6646. static int nl80211_join_mesh(struct i802_bss *bss,
  6647. struct wpa_driver_mesh_join_params *params)
  6648. {
  6649. struct wpa_driver_nl80211_data *drv = bss->drv;
  6650. struct nl_msg *msg;
  6651. struct nlattr *container;
  6652. int ret = -1;
  6653. wpa_printf(MSG_DEBUG, "nl80211: mesh join (ifindex=%d)", drv->ifindex);
  6654. msg = nl80211_drv_msg(drv, 0, NL80211_CMD_JOIN_MESH);
  6655. if (!msg ||
  6656. nl80211_put_freq_params(msg, &params->freq) ||
  6657. nl80211_put_basic_rates(msg, params->basic_rates) ||
  6658. nl80211_put_mesh_id(msg, params->meshid, params->meshid_len) ||
  6659. nl80211_put_beacon_int(msg, params->beacon_int))
  6660. goto fail;
  6661. wpa_printf(MSG_DEBUG, " * flags=%08X", params->flags);
  6662. container = nla_nest_start(msg, NL80211_ATTR_MESH_SETUP);
  6663. if (!container)
  6664. goto fail;
  6665. if (params->ies) {
  6666. wpa_hexdump(MSG_DEBUG, " * IEs", params->ies, params->ie_len);
  6667. if (nla_put(msg, NL80211_MESH_SETUP_IE, params->ie_len,
  6668. params->ies))
  6669. goto fail;
  6670. }
  6671. /* WPA_DRIVER_MESH_FLAG_OPEN_AUTH is treated as default by nl80211 */
  6672. if (params->flags & WPA_DRIVER_MESH_FLAG_SAE_AUTH) {
  6673. if (nla_put_u8(msg, NL80211_MESH_SETUP_AUTH_PROTOCOL, 0x1) ||
  6674. nla_put_flag(msg, NL80211_MESH_SETUP_USERSPACE_AUTH))
  6675. goto fail;
  6676. }
  6677. if ((params->flags & WPA_DRIVER_MESH_FLAG_AMPE) &&
  6678. nla_put_flag(msg, NL80211_MESH_SETUP_USERSPACE_AMPE))
  6679. goto fail;
  6680. if ((params->flags & WPA_DRIVER_MESH_FLAG_USER_MPM) &&
  6681. nla_put_flag(msg, NL80211_MESH_SETUP_USERSPACE_MPM))
  6682. goto fail;
  6683. nla_nest_end(msg, container);
  6684. container = nla_nest_start(msg, NL80211_ATTR_MESH_CONFIG);
  6685. if (!container)
  6686. goto fail;
  6687. if (!(params->conf.flags & WPA_DRIVER_MESH_CONF_FLAG_AUTO_PLINKS) &&
  6688. nla_put_u32(msg, NL80211_MESHCONF_AUTO_OPEN_PLINKS, 0))
  6689. goto fail;
  6690. if ((params->conf.flags & WPA_DRIVER_MESH_FLAG_DRIVER_MPM) &&
  6691. nla_put_u16(msg, NL80211_MESHCONF_MAX_PEER_LINKS,
  6692. params->max_peer_links))
  6693. goto fail;
  6694. /*
  6695. * Set NL80211_MESHCONF_PLINK_TIMEOUT even if user mpm is used because
  6696. * the timer could disconnect stations even in that case.
  6697. */
  6698. if (nla_put_u32(msg, NL80211_MESHCONF_PLINK_TIMEOUT,
  6699. params->conf.peer_link_timeout)) {
  6700. wpa_printf(MSG_ERROR, "nl80211: Failed to set PLINK_TIMEOUT");
  6701. goto fail;
  6702. }
  6703. nla_nest_end(msg, container);
  6704. ret = send_and_recv_msgs(drv, msg, NULL, NULL);
  6705. msg = NULL;
  6706. if (ret) {
  6707. wpa_printf(MSG_DEBUG, "nl80211: mesh join failed: ret=%d (%s)",
  6708. ret, strerror(-ret));
  6709. goto fail;
  6710. }
  6711. ret = 0;
  6712. bss->freq = params->freq.freq;
  6713. wpa_printf(MSG_DEBUG, "nl80211: mesh join request send successfully");
  6714. fail:
  6715. nlmsg_free(msg);
  6716. return ret;
  6717. }
  6718. static int
  6719. wpa_driver_nl80211_join_mesh(void *priv,
  6720. struct wpa_driver_mesh_join_params *params)
  6721. {
  6722. struct i802_bss *bss = priv;
  6723. int ret, timeout;
  6724. timeout = params->conf.peer_link_timeout;
  6725. /* Disable kernel inactivity timer */
  6726. if (params->flags & WPA_DRIVER_MESH_FLAG_USER_MPM)
  6727. params->conf.peer_link_timeout = 0;
  6728. ret = nl80211_join_mesh(bss, params);
  6729. if (ret == -EINVAL && params->conf.peer_link_timeout == 0) {
  6730. wpa_printf(MSG_DEBUG,
  6731. "nl80211: Mesh join retry for peer_link_timeout");
  6732. /*
  6733. * Old kernel does not support setting
  6734. * NL80211_MESHCONF_PLINK_TIMEOUT to zero, so set 60 seconds
  6735. * into future from peer_link_timeout.
  6736. */
  6737. params->conf.peer_link_timeout = timeout + 60;
  6738. ret = nl80211_join_mesh(priv, params);
  6739. }
  6740. params->conf.peer_link_timeout = timeout;
  6741. return ret;
  6742. }
  6743. static int wpa_driver_nl80211_leave_mesh(void *priv)
  6744. {
  6745. struct i802_bss *bss = priv;
  6746. struct wpa_driver_nl80211_data *drv = bss->drv;
  6747. struct nl_msg *msg;
  6748. int ret;
  6749. wpa_printf(MSG_DEBUG, "nl80211: mesh leave (ifindex=%d)", drv->ifindex);
  6750. msg = nl80211_drv_msg(drv, 0, NL80211_CMD_LEAVE_MESH);
  6751. ret = send_and_recv_msgs(drv, msg, NULL, NULL);
  6752. if (ret) {
  6753. wpa_printf(MSG_DEBUG, "nl80211: mesh leave failed: ret=%d (%s)",
  6754. ret, strerror(-ret));
  6755. } else {
  6756. wpa_printf(MSG_DEBUG,
  6757. "nl80211: mesh leave request send successfully");
  6758. }
  6759. if (wpa_driver_nl80211_set_mode(drv->first_bss,
  6760. NL80211_IFTYPE_STATION)) {
  6761. wpa_printf(MSG_INFO,
  6762. "nl80211: Failed to set interface into station mode");
  6763. }
  6764. return ret;
  6765. }
  6766. #endif /* CONFIG_MESH */
  6767. static int wpa_driver_br_add_ip_neigh(void *priv, u8 version,
  6768. const u8 *ipaddr, int prefixlen,
  6769. const u8 *addr)
  6770. {
  6771. #ifdef CONFIG_LIBNL3_ROUTE
  6772. struct i802_bss *bss = priv;
  6773. struct wpa_driver_nl80211_data *drv = bss->drv;
  6774. struct rtnl_neigh *rn;
  6775. struct nl_addr *nl_ipaddr = NULL;
  6776. struct nl_addr *nl_lladdr = NULL;
  6777. int family, addrsize;
  6778. int res;
  6779. if (!ipaddr || prefixlen == 0 || !addr)
  6780. return -EINVAL;
  6781. if (bss->br_ifindex == 0) {
  6782. wpa_printf(MSG_DEBUG,
  6783. "nl80211: bridge must be set before adding an ip neigh to it");
  6784. return -1;
  6785. }
  6786. if (!drv->rtnl_sk) {
  6787. wpa_printf(MSG_DEBUG,
  6788. "nl80211: nl_sock for NETLINK_ROUTE is not initialized");
  6789. return -1;
  6790. }
  6791. if (version == 4) {
  6792. family = AF_INET;
  6793. addrsize = 4;
  6794. } else if (version == 6) {
  6795. family = AF_INET6;
  6796. addrsize = 16;
  6797. } else {
  6798. return -EINVAL;
  6799. }
  6800. rn = rtnl_neigh_alloc();
  6801. if (rn == NULL)
  6802. return -ENOMEM;
  6803. /* set the destination ip address for neigh */
  6804. nl_ipaddr = nl_addr_build(family, (void *) ipaddr, addrsize);
  6805. if (nl_ipaddr == NULL) {
  6806. wpa_printf(MSG_DEBUG, "nl80211: nl_ipaddr build failed");
  6807. res = -ENOMEM;
  6808. goto errout;
  6809. }
  6810. nl_addr_set_prefixlen(nl_ipaddr, prefixlen);
  6811. res = rtnl_neigh_set_dst(rn, nl_ipaddr);
  6812. if (res) {
  6813. wpa_printf(MSG_DEBUG,
  6814. "nl80211: neigh set destination addr failed");
  6815. goto errout;
  6816. }
  6817. /* set the corresponding lladdr for neigh */
  6818. nl_lladdr = nl_addr_build(AF_BRIDGE, (u8 *) addr, ETH_ALEN);
  6819. if (nl_lladdr == NULL) {
  6820. wpa_printf(MSG_DEBUG, "nl80211: neigh set lladdr failed");
  6821. res = -ENOMEM;
  6822. goto errout;
  6823. }
  6824. rtnl_neigh_set_lladdr(rn, nl_lladdr);
  6825. rtnl_neigh_set_ifindex(rn, bss->br_ifindex);
  6826. rtnl_neigh_set_state(rn, NUD_PERMANENT);
  6827. res = rtnl_neigh_add(drv->rtnl_sk, rn, NLM_F_CREATE);
  6828. if (res) {
  6829. wpa_printf(MSG_DEBUG,
  6830. "nl80211: Adding bridge ip neigh failed: %s",
  6831. strerror(errno));
  6832. }
  6833. errout:
  6834. if (nl_lladdr)
  6835. nl_addr_put(nl_lladdr);
  6836. if (nl_ipaddr)
  6837. nl_addr_put(nl_ipaddr);
  6838. if (rn)
  6839. rtnl_neigh_put(rn);
  6840. return res;
  6841. #else /* CONFIG_LIBNL3_ROUTE */
  6842. return -1;
  6843. #endif /* CONFIG_LIBNL3_ROUTE */
  6844. }
  6845. static int wpa_driver_br_delete_ip_neigh(void *priv, u8 version,
  6846. const u8 *ipaddr)
  6847. {
  6848. #ifdef CONFIG_LIBNL3_ROUTE
  6849. struct i802_bss *bss = priv;
  6850. struct wpa_driver_nl80211_data *drv = bss->drv;
  6851. struct rtnl_neigh *rn;
  6852. struct nl_addr *nl_ipaddr;
  6853. int family, addrsize;
  6854. int res;
  6855. if (!ipaddr)
  6856. return -EINVAL;
  6857. if (version == 4) {
  6858. family = AF_INET;
  6859. addrsize = 4;
  6860. } else if (version == 6) {
  6861. family = AF_INET6;
  6862. addrsize = 16;
  6863. } else {
  6864. return -EINVAL;
  6865. }
  6866. if (bss->br_ifindex == 0) {
  6867. wpa_printf(MSG_DEBUG,
  6868. "nl80211: bridge must be set to delete an ip neigh");
  6869. return -1;
  6870. }
  6871. if (!drv->rtnl_sk) {
  6872. wpa_printf(MSG_DEBUG,
  6873. "nl80211: nl_sock for NETLINK_ROUTE is not initialized");
  6874. return -1;
  6875. }
  6876. rn = rtnl_neigh_alloc();
  6877. if (rn == NULL)
  6878. return -ENOMEM;
  6879. /* set the destination ip address for neigh */
  6880. nl_ipaddr = nl_addr_build(family, (void *) ipaddr, addrsize);
  6881. if (nl_ipaddr == NULL) {
  6882. wpa_printf(MSG_DEBUG, "nl80211: nl_ipaddr build failed");
  6883. res = -ENOMEM;
  6884. goto errout;
  6885. }
  6886. res = rtnl_neigh_set_dst(rn, nl_ipaddr);
  6887. if (res) {
  6888. wpa_printf(MSG_DEBUG,
  6889. "nl80211: neigh set destination addr failed");
  6890. goto errout;
  6891. }
  6892. rtnl_neigh_set_ifindex(rn, bss->br_ifindex);
  6893. res = rtnl_neigh_delete(drv->rtnl_sk, rn, 0);
  6894. if (res) {
  6895. wpa_printf(MSG_DEBUG,
  6896. "nl80211: Deleting bridge ip neigh failed: %s",
  6897. strerror(errno));
  6898. }
  6899. errout:
  6900. if (nl_ipaddr)
  6901. nl_addr_put(nl_ipaddr);
  6902. if (rn)
  6903. rtnl_neigh_put(rn);
  6904. return res;
  6905. #else /* CONFIG_LIBNL3_ROUTE */
  6906. return -1;
  6907. #endif /* CONFIG_LIBNL3_ROUTE */
  6908. }
  6909. static int linux_write_system_file(const char *path, unsigned int val)
  6910. {
  6911. char buf[50];
  6912. int fd, len;
  6913. len = os_snprintf(buf, sizeof(buf), "%u\n", val);
  6914. if (os_snprintf_error(sizeof(buf), len))
  6915. return -1;
  6916. fd = open(path, O_WRONLY);
  6917. if (fd < 0)
  6918. return -1;
  6919. if (write(fd, buf, len) < 0) {
  6920. wpa_printf(MSG_DEBUG,
  6921. "nl80211: Failed to write Linux system file: %s with the value of %d",
  6922. path, val);
  6923. close(fd);
  6924. return -1;
  6925. }
  6926. close(fd);
  6927. return 0;
  6928. }
  6929. static const char * drv_br_port_attr_str(enum drv_br_port_attr attr)
  6930. {
  6931. switch (attr) {
  6932. case DRV_BR_PORT_ATTR_PROXYARP:
  6933. return "proxyarp_wifi";
  6934. case DRV_BR_PORT_ATTR_HAIRPIN_MODE:
  6935. return "hairpin_mode";
  6936. }
  6937. return NULL;
  6938. }
  6939. static int wpa_driver_br_port_set_attr(void *priv, enum drv_br_port_attr attr,
  6940. unsigned int val)
  6941. {
  6942. struct i802_bss *bss = priv;
  6943. char path[128];
  6944. const char *attr_txt;
  6945. attr_txt = drv_br_port_attr_str(attr);
  6946. if (attr_txt == NULL)
  6947. return -EINVAL;
  6948. os_snprintf(path, sizeof(path), "/sys/class/net/%s/brport/%s",
  6949. bss->ifname, attr_txt);
  6950. if (linux_write_system_file(path, val))
  6951. return -1;
  6952. return 0;
  6953. }
  6954. static const char * drv_br_net_param_str(enum drv_br_net_param param)
  6955. {
  6956. switch (param) {
  6957. case DRV_BR_NET_PARAM_GARP_ACCEPT:
  6958. return "arp_accept";
  6959. }
  6960. return NULL;
  6961. }
  6962. static int wpa_driver_br_set_net_param(void *priv, enum drv_br_net_param param,
  6963. unsigned int val)
  6964. {
  6965. struct i802_bss *bss = priv;
  6966. char path[128];
  6967. const char *param_txt;
  6968. int ip_version = 4;
  6969. param_txt = drv_br_net_param_str(param);
  6970. if (param_txt == NULL)
  6971. return -EINVAL;
  6972. switch (param) {
  6973. case DRV_BR_NET_PARAM_GARP_ACCEPT:
  6974. ip_version = 4;
  6975. break;
  6976. default:
  6977. return -EINVAL;
  6978. }
  6979. os_snprintf(path, sizeof(path), "/proc/sys/net/ipv%d/conf/%s/%s",
  6980. ip_version, bss->brname, param_txt);
  6981. if (linux_write_system_file(path, val))
  6982. return -1;
  6983. return 0;
  6984. }
  6985. static int hw_mode_to_qca_acs(enum hostapd_hw_mode hw_mode)
  6986. {
  6987. switch (hw_mode) {
  6988. case HOSTAPD_MODE_IEEE80211B:
  6989. return QCA_ACS_MODE_IEEE80211B;
  6990. case HOSTAPD_MODE_IEEE80211G:
  6991. return QCA_ACS_MODE_IEEE80211G;
  6992. case HOSTAPD_MODE_IEEE80211A:
  6993. return QCA_ACS_MODE_IEEE80211A;
  6994. case HOSTAPD_MODE_IEEE80211AD:
  6995. return QCA_ACS_MODE_IEEE80211AD;
  6996. default:
  6997. return -1;
  6998. }
  6999. }
  7000. static int wpa_driver_do_acs(void *priv, struct drv_acs_params *params)
  7001. {
  7002. struct i802_bss *bss = priv;
  7003. struct wpa_driver_nl80211_data *drv = bss->drv;
  7004. struct nl_msg *msg;
  7005. struct nlattr *data;
  7006. int ret;
  7007. int mode;
  7008. mode = hw_mode_to_qca_acs(params->hw_mode);
  7009. if (mode < 0)
  7010. return -1;
  7011. if (!(msg = nl80211_drv_msg(drv, 0, NL80211_CMD_VENDOR)) ||
  7012. nla_put_u32(msg, NL80211_ATTR_VENDOR_ID, OUI_QCA) ||
  7013. nla_put_u32(msg, NL80211_ATTR_VENDOR_SUBCMD,
  7014. QCA_NL80211_VENDOR_SUBCMD_DO_ACS) ||
  7015. !(data = nla_nest_start(msg, NL80211_ATTR_VENDOR_DATA)) ||
  7016. nla_put_u8(msg, QCA_WLAN_VENDOR_ATTR_ACS_HW_MODE, mode) ||
  7017. (params->ht_enabled &&
  7018. nla_put_flag(msg, QCA_WLAN_VENDOR_ATTR_ACS_HT_ENABLED)) ||
  7019. (params->ht40_enabled &&
  7020. nla_put_flag(msg, QCA_WLAN_VENDOR_ATTR_ACS_HT40_ENABLED))) {
  7021. nlmsg_free(msg);
  7022. return -ENOBUFS;
  7023. }
  7024. nla_nest_end(msg, data);
  7025. ret = send_and_recv_msgs(drv, msg, NULL, NULL);
  7026. if (ret) {
  7027. wpa_printf(MSG_DEBUG,
  7028. "nl80211: Failed to invoke driver ACS function: %s",
  7029. strerror(errno));
  7030. }
  7031. return ret;
  7032. }
  7033. const struct wpa_driver_ops wpa_driver_nl80211_ops = {
  7034. .name = "nl80211",
  7035. .desc = "Linux nl80211/cfg80211",
  7036. .get_bssid = wpa_driver_nl80211_get_bssid,
  7037. .get_ssid = wpa_driver_nl80211_get_ssid,
  7038. .set_key = driver_nl80211_set_key,
  7039. .scan2 = driver_nl80211_scan2,
  7040. .sched_scan = wpa_driver_nl80211_sched_scan,
  7041. .stop_sched_scan = wpa_driver_nl80211_stop_sched_scan,
  7042. .get_scan_results2 = wpa_driver_nl80211_get_scan_results,
  7043. .deauthenticate = driver_nl80211_deauthenticate,
  7044. .authenticate = driver_nl80211_authenticate,
  7045. .associate = wpa_driver_nl80211_associate,
  7046. .global_init = nl80211_global_init,
  7047. .global_deinit = nl80211_global_deinit,
  7048. .init2 = wpa_driver_nl80211_init,
  7049. .deinit = driver_nl80211_deinit,
  7050. .get_capa = wpa_driver_nl80211_get_capa,
  7051. .set_operstate = wpa_driver_nl80211_set_operstate,
  7052. .set_supp_port = wpa_driver_nl80211_set_supp_port,
  7053. .set_country = wpa_driver_nl80211_set_country,
  7054. .get_country = wpa_driver_nl80211_get_country,
  7055. .set_ap = wpa_driver_nl80211_set_ap,
  7056. .set_acl = wpa_driver_nl80211_set_acl,
  7057. .if_add = wpa_driver_nl80211_if_add,
  7058. .if_remove = driver_nl80211_if_remove,
  7059. .send_mlme = driver_nl80211_send_mlme,
  7060. .get_hw_feature_data = nl80211_get_hw_feature_data,
  7061. .sta_add = wpa_driver_nl80211_sta_add,
  7062. .sta_remove = driver_nl80211_sta_remove,
  7063. .hapd_send_eapol = wpa_driver_nl80211_hapd_send_eapol,
  7064. .sta_set_flags = wpa_driver_nl80211_sta_set_flags,
  7065. .hapd_init = i802_init,
  7066. .hapd_deinit = i802_deinit,
  7067. .set_wds_sta = i802_set_wds_sta,
  7068. .get_seqnum = i802_get_seqnum,
  7069. .flush = i802_flush,
  7070. .get_inact_sec = i802_get_inact_sec,
  7071. .sta_clear_stats = i802_sta_clear_stats,
  7072. .set_rts = i802_set_rts,
  7073. .set_frag = i802_set_frag,
  7074. .set_tx_queue_params = i802_set_tx_queue_params,
  7075. .set_sta_vlan = driver_nl80211_set_sta_vlan,
  7076. .sta_deauth = i802_sta_deauth,
  7077. .sta_disassoc = i802_sta_disassoc,
  7078. .read_sta_data = driver_nl80211_read_sta_data,
  7079. .set_freq = i802_set_freq,
  7080. .send_action = driver_nl80211_send_action,
  7081. .send_action_cancel_wait = wpa_driver_nl80211_send_action_cancel_wait,
  7082. .remain_on_channel = wpa_driver_nl80211_remain_on_channel,
  7083. .cancel_remain_on_channel =
  7084. wpa_driver_nl80211_cancel_remain_on_channel,
  7085. .probe_req_report = driver_nl80211_probe_req_report,
  7086. .deinit_ap = wpa_driver_nl80211_deinit_ap,
  7087. .deinit_p2p_cli = wpa_driver_nl80211_deinit_p2p_cli,
  7088. .resume = wpa_driver_nl80211_resume,
  7089. .signal_monitor = nl80211_signal_monitor,
  7090. .signal_poll = nl80211_signal_poll,
  7091. .send_frame = nl80211_send_frame,
  7092. .shared_freq = wpa_driver_nl80211_shared_freq,
  7093. .set_param = nl80211_set_param,
  7094. .get_radio_name = nl80211_get_radio_name,
  7095. .add_pmkid = nl80211_add_pmkid,
  7096. .remove_pmkid = nl80211_remove_pmkid,
  7097. .flush_pmkid = nl80211_flush_pmkid,
  7098. .set_rekey_info = nl80211_set_rekey_info,
  7099. .poll_client = nl80211_poll_client,
  7100. .set_p2p_powersave = nl80211_set_p2p_powersave,
  7101. .start_dfs_cac = nl80211_start_radar_detection,
  7102. .stop_ap = wpa_driver_nl80211_stop_ap,
  7103. #ifdef CONFIG_TDLS
  7104. .send_tdls_mgmt = nl80211_send_tdls_mgmt,
  7105. .tdls_oper = nl80211_tdls_oper,
  7106. .tdls_enable_channel_switch = nl80211_tdls_enable_channel_switch,
  7107. .tdls_disable_channel_switch = nl80211_tdls_disable_channel_switch,
  7108. #endif /* CONFIG_TDLS */
  7109. .update_ft_ies = wpa_driver_nl80211_update_ft_ies,
  7110. .get_mac_addr = wpa_driver_nl80211_get_macaddr,
  7111. .get_survey = wpa_driver_nl80211_get_survey,
  7112. .status = wpa_driver_nl80211_status,
  7113. .switch_channel = nl80211_switch_channel,
  7114. #ifdef ANDROID_P2P
  7115. .set_noa = wpa_driver_set_p2p_noa,
  7116. .get_noa = wpa_driver_get_p2p_noa,
  7117. .set_ap_wps_ie = wpa_driver_set_ap_wps_p2p_ie,
  7118. #endif /* ANDROID_P2P */
  7119. #ifdef ANDROID
  7120. .driver_cmd = wpa_driver_nl80211_driver_cmd,
  7121. #endif /* ANDROID */
  7122. .vendor_cmd = nl80211_vendor_cmd,
  7123. .set_qos_map = nl80211_set_qos_map,
  7124. .set_wowlan = nl80211_set_wowlan,
  7125. .roaming = nl80211_roaming,
  7126. .set_mac_addr = nl80211_set_mac_addr,
  7127. #ifdef CONFIG_MESH
  7128. .init_mesh = wpa_driver_nl80211_init_mesh,
  7129. .join_mesh = wpa_driver_nl80211_join_mesh,
  7130. .leave_mesh = wpa_driver_nl80211_leave_mesh,
  7131. #endif /* CONFIG_MESH */
  7132. .br_add_ip_neigh = wpa_driver_br_add_ip_neigh,
  7133. .br_delete_ip_neigh = wpa_driver_br_delete_ip_neigh,
  7134. .br_port_set_attr = wpa_driver_br_port_set_attr,
  7135. .br_set_net_param = wpa_driver_br_set_net_param,
  7136. .add_tx_ts = nl80211_add_ts,
  7137. .del_tx_ts = nl80211_del_ts,
  7138. .do_acs = wpa_driver_do_acs,
  7139. };