ctrl_iface.c 260 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954
  1. /*
  2. * WPA Supplicant / Control interface (shared code for all backends)
  3. * Copyright (c) 2004-2015, Jouni Malinen <j@w1.fi>
  4. *
  5. * This software may be distributed under the terms of the BSD license.
  6. * See README for more details.
  7. */
  8. #include "utils/includes.h"
  9. #ifdef CONFIG_TESTING_OPTIONS
  10. #include <net/ethernet.h>
  11. #include <netinet/ip.h>
  12. #endif /* CONFIG_TESTING_OPTIONS */
  13. #include "utils/common.h"
  14. #include "utils/eloop.h"
  15. #include "utils/uuid.h"
  16. #include "utils/module_tests.h"
  17. #include "common/version.h"
  18. #include "common/ieee802_11_defs.h"
  19. #include "common/ieee802_11_common.h"
  20. #include "common/wpa_ctrl.h"
  21. #include "crypto/tls.h"
  22. #include "ap/hostapd.h"
  23. #include "eap_peer/eap.h"
  24. #include "eapol_supp/eapol_supp_sm.h"
  25. #include "rsn_supp/wpa.h"
  26. #include "rsn_supp/preauth.h"
  27. #include "rsn_supp/pmksa_cache.h"
  28. #include "l2_packet/l2_packet.h"
  29. #include "wps/wps.h"
  30. #include "fst/fst.h"
  31. #include "fst/fst_ctrl_iface.h"
  32. #include "config.h"
  33. #include "wpa_supplicant_i.h"
  34. #include "driver_i.h"
  35. #include "wps_supplicant.h"
  36. #include "ibss_rsn.h"
  37. #include "ap.h"
  38. #include "p2p_supplicant.h"
  39. #include "p2p/p2p.h"
  40. #include "hs20_supplicant.h"
  41. #include "wifi_display.h"
  42. #include "notify.h"
  43. #include "bss.h"
  44. #include "scan.h"
  45. #include "ctrl_iface.h"
  46. #include "interworking.h"
  47. #include "blacklist.h"
  48. #include "autoscan.h"
  49. #include "wnm_sta.h"
  50. #include "offchannel.h"
  51. #include "drivers/driver.h"
  52. #include "mesh.h"
  53. #include "dpp_supplicant.h"
  54. static int wpa_supplicant_global_iface_list(struct wpa_global *global,
  55. char *buf, int len);
  56. static int wpa_supplicant_global_iface_interfaces(struct wpa_global *global,
  57. const char *input,
  58. char *buf, int len);
  59. static int * freq_range_to_channel_list(struct wpa_supplicant *wpa_s,
  60. char *val);
  61. static int set_bssid_filter(struct wpa_supplicant *wpa_s, char *val)
  62. {
  63. char *pos;
  64. u8 addr[ETH_ALEN], *filter = NULL, *n;
  65. size_t count = 0;
  66. pos = val;
  67. while (pos) {
  68. if (*pos == '\0')
  69. break;
  70. if (hwaddr_aton(pos, addr)) {
  71. os_free(filter);
  72. return -1;
  73. }
  74. n = os_realloc_array(filter, count + 1, ETH_ALEN);
  75. if (n == NULL) {
  76. os_free(filter);
  77. return -1;
  78. }
  79. filter = n;
  80. os_memcpy(filter + count * ETH_ALEN, addr, ETH_ALEN);
  81. count++;
  82. pos = os_strchr(pos, ' ');
  83. if (pos)
  84. pos++;
  85. }
  86. wpa_hexdump(MSG_DEBUG, "bssid_filter", filter, count * ETH_ALEN);
  87. os_free(wpa_s->bssid_filter);
  88. wpa_s->bssid_filter = filter;
  89. wpa_s->bssid_filter_count = count;
  90. return 0;
  91. }
  92. static int set_disallow_aps(struct wpa_supplicant *wpa_s, char *val)
  93. {
  94. char *pos;
  95. u8 addr[ETH_ALEN], *bssid = NULL, *n;
  96. struct wpa_ssid_value *ssid = NULL, *ns;
  97. size_t count = 0, ssid_count = 0;
  98. struct wpa_ssid *c;
  99. /*
  100. * disallow_list ::= <ssid_spec> | <bssid_spec> | <disallow_list> | ""
  101. * SSID_SPEC ::= ssid <SSID_HEX>
  102. * BSSID_SPEC ::= bssid <BSSID_HEX>
  103. */
  104. pos = val;
  105. while (pos) {
  106. if (*pos == '\0')
  107. break;
  108. if (os_strncmp(pos, "bssid ", 6) == 0) {
  109. int res;
  110. pos += 6;
  111. res = hwaddr_aton2(pos, addr);
  112. if (res < 0) {
  113. os_free(ssid);
  114. os_free(bssid);
  115. wpa_printf(MSG_DEBUG, "Invalid disallow_aps "
  116. "BSSID value '%s'", pos);
  117. return -1;
  118. }
  119. pos += res;
  120. n = os_realloc_array(bssid, count + 1, ETH_ALEN);
  121. if (n == NULL) {
  122. os_free(ssid);
  123. os_free(bssid);
  124. return -1;
  125. }
  126. bssid = n;
  127. os_memcpy(bssid + count * ETH_ALEN, addr, ETH_ALEN);
  128. count++;
  129. } else if (os_strncmp(pos, "ssid ", 5) == 0) {
  130. char *end;
  131. pos += 5;
  132. end = pos;
  133. while (*end) {
  134. if (*end == '\0' || *end == ' ')
  135. break;
  136. end++;
  137. }
  138. ns = os_realloc_array(ssid, ssid_count + 1,
  139. sizeof(struct wpa_ssid_value));
  140. if (ns == NULL) {
  141. os_free(ssid);
  142. os_free(bssid);
  143. return -1;
  144. }
  145. ssid = ns;
  146. if ((end - pos) & 0x01 ||
  147. end - pos > 2 * SSID_MAX_LEN ||
  148. hexstr2bin(pos, ssid[ssid_count].ssid,
  149. (end - pos) / 2) < 0) {
  150. os_free(ssid);
  151. os_free(bssid);
  152. wpa_printf(MSG_DEBUG, "Invalid disallow_aps "
  153. "SSID value '%s'", pos);
  154. return -1;
  155. }
  156. ssid[ssid_count].ssid_len = (end - pos) / 2;
  157. wpa_hexdump_ascii(MSG_DEBUG, "disallow_aps SSID",
  158. ssid[ssid_count].ssid,
  159. ssid[ssid_count].ssid_len);
  160. ssid_count++;
  161. pos = end;
  162. } else {
  163. wpa_printf(MSG_DEBUG, "Unexpected disallow_aps value "
  164. "'%s'", pos);
  165. os_free(ssid);
  166. os_free(bssid);
  167. return -1;
  168. }
  169. pos = os_strchr(pos, ' ');
  170. if (pos)
  171. pos++;
  172. }
  173. wpa_hexdump(MSG_DEBUG, "disallow_aps_bssid", bssid, count * ETH_ALEN);
  174. os_free(wpa_s->disallow_aps_bssid);
  175. wpa_s->disallow_aps_bssid = bssid;
  176. wpa_s->disallow_aps_bssid_count = count;
  177. wpa_printf(MSG_DEBUG, "disallow_aps_ssid_count %d", (int) ssid_count);
  178. os_free(wpa_s->disallow_aps_ssid);
  179. wpa_s->disallow_aps_ssid = ssid;
  180. wpa_s->disallow_aps_ssid_count = ssid_count;
  181. if (!wpa_s->current_ssid || wpa_s->wpa_state < WPA_AUTHENTICATING)
  182. return 0;
  183. c = wpa_s->current_ssid;
  184. if (c->mode != WPAS_MODE_INFRA && c->mode != WPAS_MODE_IBSS)
  185. return 0;
  186. if (!disallowed_bssid(wpa_s, wpa_s->bssid) &&
  187. !disallowed_ssid(wpa_s, c->ssid, c->ssid_len))
  188. return 0;
  189. wpa_printf(MSG_DEBUG, "Disconnect and try to find another network "
  190. "because current AP was marked disallowed");
  191. #ifdef CONFIG_SME
  192. wpa_s->sme.prev_bssid_set = 0;
  193. #endif /* CONFIG_SME */
  194. wpa_s->reassociate = 1;
  195. wpa_s->own_disconnect_req = 1;
  196. wpa_supplicant_deauthenticate(wpa_s, WLAN_REASON_DEAUTH_LEAVING);
  197. wpa_supplicant_req_scan(wpa_s, 0, 0);
  198. return 0;
  199. }
  200. #ifndef CONFIG_NO_CONFIG_BLOBS
  201. static int wpas_ctrl_set_blob(struct wpa_supplicant *wpa_s, char *pos)
  202. {
  203. char *name = pos;
  204. struct wpa_config_blob *blob;
  205. size_t len;
  206. pos = os_strchr(pos, ' ');
  207. if (pos == NULL)
  208. return -1;
  209. *pos++ = '\0';
  210. len = os_strlen(pos);
  211. if (len & 1)
  212. return -1;
  213. wpa_printf(MSG_DEBUG, "CTRL: Set blob '%s'", name);
  214. blob = os_zalloc(sizeof(*blob));
  215. if (blob == NULL)
  216. return -1;
  217. blob->name = os_strdup(name);
  218. blob->data = os_malloc(len / 2);
  219. if (blob->name == NULL || blob->data == NULL) {
  220. wpa_config_free_blob(blob);
  221. return -1;
  222. }
  223. if (hexstr2bin(pos, blob->data, len / 2) < 0) {
  224. wpa_printf(MSG_DEBUG, "CTRL: Invalid blob hex data");
  225. wpa_config_free_blob(blob);
  226. return -1;
  227. }
  228. blob->len = len / 2;
  229. wpa_config_set_blob(wpa_s->conf, blob);
  230. return 0;
  231. }
  232. #endif /* CONFIG_NO_CONFIG_BLOBS */
  233. static int wpas_ctrl_pno(struct wpa_supplicant *wpa_s, char *cmd)
  234. {
  235. char *params;
  236. char *pos;
  237. int *freqs = NULL;
  238. int ret;
  239. if (atoi(cmd)) {
  240. params = os_strchr(cmd, ' ');
  241. os_free(wpa_s->manual_sched_scan_freqs);
  242. if (params) {
  243. params++;
  244. pos = os_strstr(params, "freq=");
  245. if (pos)
  246. freqs = freq_range_to_channel_list(wpa_s,
  247. pos + 5);
  248. }
  249. wpa_s->manual_sched_scan_freqs = freqs;
  250. ret = wpas_start_pno(wpa_s);
  251. } else {
  252. ret = wpas_stop_pno(wpa_s);
  253. }
  254. return ret;
  255. }
  256. static int wpas_ctrl_set_band(struct wpa_supplicant *wpa_s, char *band)
  257. {
  258. union wpa_event_data event;
  259. if (os_strcmp(band, "AUTO") == 0)
  260. wpa_s->setband = WPA_SETBAND_AUTO;
  261. else if (os_strcmp(band, "5G") == 0)
  262. wpa_s->setband = WPA_SETBAND_5G;
  263. else if (os_strcmp(band, "2G") == 0)
  264. wpa_s->setband = WPA_SETBAND_2G;
  265. else
  266. return -1;
  267. if (wpa_drv_setband(wpa_s, wpa_s->setband) == 0) {
  268. os_memset(&event, 0, sizeof(event));
  269. event.channel_list_changed.initiator = REGDOM_SET_BY_USER;
  270. event.channel_list_changed.type = REGDOM_TYPE_UNKNOWN;
  271. wpa_supplicant_event(wpa_s, EVENT_CHANNEL_LIST_CHANGED, &event);
  272. }
  273. return 0;
  274. }
  275. static int wpas_ctrl_iface_set_lci(struct wpa_supplicant *wpa_s,
  276. const char *cmd)
  277. {
  278. struct wpabuf *lci;
  279. if (*cmd == '\0' || os_strcmp(cmd, "\"\"") == 0) {
  280. wpabuf_free(wpa_s->lci);
  281. wpa_s->lci = NULL;
  282. return 0;
  283. }
  284. lci = wpabuf_parse_bin(cmd);
  285. if (!lci)
  286. return -1;
  287. if (os_get_reltime(&wpa_s->lci_time)) {
  288. wpabuf_free(lci);
  289. return -1;
  290. }
  291. wpabuf_free(wpa_s->lci);
  292. wpa_s->lci = lci;
  293. return 0;
  294. }
  295. static int
  296. wpas_ctrl_set_relative_rssi(struct wpa_supplicant *wpa_s, const char *cmd)
  297. {
  298. int relative_rssi;
  299. if (os_strcmp(cmd, "disable") == 0) {
  300. wpa_s->srp.relative_rssi_set = 0;
  301. return 0;
  302. }
  303. relative_rssi = atoi(cmd);
  304. if (relative_rssi < 0 || relative_rssi > 100)
  305. return -1;
  306. wpa_s->srp.relative_rssi = relative_rssi;
  307. wpa_s->srp.relative_rssi_set = 1;
  308. return 0;
  309. }
  310. static int wpas_ctrl_set_relative_band_adjust(struct wpa_supplicant *wpa_s,
  311. const char *cmd)
  312. {
  313. char *pos;
  314. int adjust_rssi;
  315. /* <band>:adjust_value */
  316. pos = os_strchr(cmd, ':');
  317. if (!pos)
  318. return -1;
  319. pos++;
  320. adjust_rssi = atoi(pos);
  321. if (adjust_rssi < -100 || adjust_rssi > 100)
  322. return -1;
  323. if (os_strncmp(cmd, "2G", 2) == 0)
  324. wpa_s->srp.relative_adjust_band = WPA_SETBAND_2G;
  325. else if (os_strncmp(cmd, "5G", 2) == 0)
  326. wpa_s->srp.relative_adjust_band = WPA_SETBAND_5G;
  327. else
  328. return -1;
  329. wpa_s->srp.relative_adjust_rssi = adjust_rssi;
  330. return 0;
  331. }
  332. static int wpas_ctrl_iface_set_ric_ies(struct wpa_supplicant *wpa_s,
  333. const char *cmd)
  334. {
  335. struct wpabuf *ric_ies;
  336. if (*cmd == '\0' || os_strcmp(cmd, "\"\"") == 0) {
  337. wpabuf_free(wpa_s->ric_ies);
  338. wpa_s->ric_ies = NULL;
  339. return 0;
  340. }
  341. ric_ies = wpabuf_parse_bin(cmd);
  342. if (!ric_ies)
  343. return -1;
  344. wpabuf_free(wpa_s->ric_ies);
  345. wpa_s->ric_ies = ric_ies;
  346. return 0;
  347. }
  348. static int wpa_supplicant_ctrl_iface_set(struct wpa_supplicant *wpa_s,
  349. char *cmd)
  350. {
  351. char *value;
  352. int ret = 0;
  353. value = os_strchr(cmd, ' ');
  354. if (value == NULL)
  355. return -1;
  356. *value++ = '\0';
  357. wpa_printf(MSG_DEBUG, "CTRL_IFACE SET '%s'='%s'", cmd, value);
  358. if (os_strcasecmp(cmd, "EAPOL::heldPeriod") == 0) {
  359. eapol_sm_configure(wpa_s->eapol,
  360. atoi(value), -1, -1, -1);
  361. } else if (os_strcasecmp(cmd, "EAPOL::authPeriod") == 0) {
  362. eapol_sm_configure(wpa_s->eapol,
  363. -1, atoi(value), -1, -1);
  364. } else if (os_strcasecmp(cmd, "EAPOL::startPeriod") == 0) {
  365. eapol_sm_configure(wpa_s->eapol,
  366. -1, -1, atoi(value), -1);
  367. } else if (os_strcasecmp(cmd, "EAPOL::maxStart") == 0) {
  368. eapol_sm_configure(wpa_s->eapol,
  369. -1, -1, -1, atoi(value));
  370. } else if (os_strcasecmp(cmd, "dot11RSNAConfigPMKLifetime") == 0) {
  371. if (wpa_sm_set_param(wpa_s->wpa, RSNA_PMK_LIFETIME,
  372. atoi(value))) {
  373. ret = -1;
  374. } else {
  375. value[-1] = '=';
  376. wpa_config_process_global(wpa_s->conf, cmd, -1);
  377. }
  378. } else if (os_strcasecmp(cmd, "dot11RSNAConfigPMKReauthThreshold") ==
  379. 0) {
  380. if (wpa_sm_set_param(wpa_s->wpa, RSNA_PMK_REAUTH_THRESHOLD,
  381. atoi(value))) {
  382. ret = -1;
  383. } else {
  384. value[-1] = '=';
  385. wpa_config_process_global(wpa_s->conf, cmd, -1);
  386. }
  387. } else if (os_strcasecmp(cmd, "dot11RSNAConfigSATimeout") == 0) {
  388. if (wpa_sm_set_param(wpa_s->wpa, RSNA_SA_TIMEOUT,
  389. atoi(value))) {
  390. ret = -1;
  391. } else {
  392. value[-1] = '=';
  393. wpa_config_process_global(wpa_s->conf, cmd, -1);
  394. }
  395. } else if (os_strcasecmp(cmd, "wps_fragment_size") == 0) {
  396. wpa_s->wps_fragment_size = atoi(value);
  397. #ifdef CONFIG_WPS_TESTING
  398. } else if (os_strcasecmp(cmd, "wps_version_number") == 0) {
  399. long int val;
  400. val = strtol(value, NULL, 0);
  401. if (val < 0 || val > 0xff) {
  402. ret = -1;
  403. wpa_printf(MSG_DEBUG, "WPS: Invalid "
  404. "wps_version_number %ld", val);
  405. } else {
  406. wps_version_number = val;
  407. wpa_printf(MSG_DEBUG, "WPS: Testing - force WPS "
  408. "version %u.%u",
  409. (wps_version_number & 0xf0) >> 4,
  410. wps_version_number & 0x0f);
  411. }
  412. } else if (os_strcasecmp(cmd, "wps_testing_dummy_cred") == 0) {
  413. wps_testing_dummy_cred = atoi(value);
  414. wpa_printf(MSG_DEBUG, "WPS: Testing - dummy_cred=%d",
  415. wps_testing_dummy_cred);
  416. } else if (os_strcasecmp(cmd, "wps_corrupt_pkhash") == 0) {
  417. wps_corrupt_pkhash = atoi(value);
  418. wpa_printf(MSG_DEBUG, "WPS: Testing - wps_corrupt_pkhash=%d",
  419. wps_corrupt_pkhash);
  420. } else if (os_strcasecmp(cmd, "wps_force_auth_types") == 0) {
  421. if (value[0] == '\0') {
  422. wps_force_auth_types_in_use = 0;
  423. } else {
  424. wps_force_auth_types = strtol(value, NULL, 0);
  425. wps_force_auth_types_in_use = 1;
  426. }
  427. } else if (os_strcasecmp(cmd, "wps_force_encr_types") == 0) {
  428. if (value[0] == '\0') {
  429. wps_force_encr_types_in_use = 0;
  430. } else {
  431. wps_force_encr_types = strtol(value, NULL, 0);
  432. wps_force_encr_types_in_use = 1;
  433. }
  434. #endif /* CONFIG_WPS_TESTING */
  435. } else if (os_strcasecmp(cmd, "ampdu") == 0) {
  436. if (wpa_drv_ampdu(wpa_s, atoi(value)) < 0)
  437. ret = -1;
  438. #ifdef CONFIG_TDLS
  439. #ifdef CONFIG_TDLS_TESTING
  440. } else if (os_strcasecmp(cmd, "tdls_testing") == 0) {
  441. tdls_testing = strtol(value, NULL, 0);
  442. wpa_printf(MSG_DEBUG, "TDLS: tdls_testing=0x%x", tdls_testing);
  443. #endif /* CONFIG_TDLS_TESTING */
  444. } else if (os_strcasecmp(cmd, "tdls_disabled") == 0) {
  445. int disabled = atoi(value);
  446. wpa_printf(MSG_DEBUG, "TDLS: tdls_disabled=%d", disabled);
  447. if (disabled) {
  448. if (wpa_drv_tdls_oper(wpa_s, TDLS_DISABLE, NULL) < 0)
  449. ret = -1;
  450. } else if (wpa_drv_tdls_oper(wpa_s, TDLS_ENABLE, NULL) < 0)
  451. ret = -1;
  452. wpa_tdls_enable(wpa_s->wpa, !disabled);
  453. #endif /* CONFIG_TDLS */
  454. } else if (os_strcasecmp(cmd, "pno") == 0) {
  455. ret = wpas_ctrl_pno(wpa_s, value);
  456. } else if (os_strcasecmp(cmd, "radio_disabled") == 0) {
  457. int disabled = atoi(value);
  458. if (wpa_drv_radio_disable(wpa_s, disabled) < 0)
  459. ret = -1;
  460. else if (disabled)
  461. wpa_supplicant_set_state(wpa_s, WPA_INACTIVE);
  462. } else if (os_strcasecmp(cmd, "uapsd") == 0) {
  463. if (os_strcmp(value, "disable") == 0)
  464. wpa_s->set_sta_uapsd = 0;
  465. else {
  466. int be, bk, vi, vo;
  467. char *pos;
  468. /* format: BE,BK,VI,VO;max SP Length */
  469. be = atoi(value);
  470. pos = os_strchr(value, ',');
  471. if (pos == NULL)
  472. return -1;
  473. pos++;
  474. bk = atoi(pos);
  475. pos = os_strchr(pos, ',');
  476. if (pos == NULL)
  477. return -1;
  478. pos++;
  479. vi = atoi(pos);
  480. pos = os_strchr(pos, ',');
  481. if (pos == NULL)
  482. return -1;
  483. pos++;
  484. vo = atoi(pos);
  485. /* ignore max SP Length for now */
  486. wpa_s->set_sta_uapsd = 1;
  487. wpa_s->sta_uapsd = 0;
  488. if (be)
  489. wpa_s->sta_uapsd |= BIT(0);
  490. if (bk)
  491. wpa_s->sta_uapsd |= BIT(1);
  492. if (vi)
  493. wpa_s->sta_uapsd |= BIT(2);
  494. if (vo)
  495. wpa_s->sta_uapsd |= BIT(3);
  496. }
  497. } else if (os_strcasecmp(cmd, "ps") == 0) {
  498. ret = wpa_drv_set_p2p_powersave(wpa_s, atoi(value), -1, -1);
  499. #ifdef CONFIG_WIFI_DISPLAY
  500. } else if (os_strcasecmp(cmd, "wifi_display") == 0) {
  501. int enabled = !!atoi(value);
  502. if (enabled && !wpa_s->global->p2p)
  503. ret = -1;
  504. else
  505. wifi_display_enable(wpa_s->global, enabled);
  506. #endif /* CONFIG_WIFI_DISPLAY */
  507. } else if (os_strcasecmp(cmd, "bssid_filter") == 0) {
  508. ret = set_bssid_filter(wpa_s, value);
  509. } else if (os_strcasecmp(cmd, "disallow_aps") == 0) {
  510. ret = set_disallow_aps(wpa_s, value);
  511. } else if (os_strcasecmp(cmd, "no_keep_alive") == 0) {
  512. wpa_s->no_keep_alive = !!atoi(value);
  513. #ifdef CONFIG_TESTING_OPTIONS
  514. } else if (os_strcasecmp(cmd, "ext_mgmt_frame_handling") == 0) {
  515. wpa_s->ext_mgmt_frame_handling = !!atoi(value);
  516. } else if (os_strcasecmp(cmd, "ext_eapol_frame_io") == 0) {
  517. wpa_s->ext_eapol_frame_io = !!atoi(value);
  518. #ifdef CONFIG_AP
  519. if (wpa_s->ap_iface) {
  520. wpa_s->ap_iface->bss[0]->ext_eapol_frame_io =
  521. wpa_s->ext_eapol_frame_io;
  522. }
  523. #endif /* CONFIG_AP */
  524. } else if (os_strcasecmp(cmd, "extra_roc_dur") == 0) {
  525. wpa_s->extra_roc_dur = atoi(value);
  526. } else if (os_strcasecmp(cmd, "test_failure") == 0) {
  527. wpa_s->test_failure = atoi(value);
  528. } else if (os_strcasecmp(cmd, "p2p_go_csa_on_inv") == 0) {
  529. wpa_s->p2p_go_csa_on_inv = !!atoi(value);
  530. } else if (os_strcasecmp(cmd, "ignore_auth_resp") == 0) {
  531. wpa_s->ignore_auth_resp = !!atoi(value);
  532. } else if (os_strcasecmp(cmd, "ignore_assoc_disallow") == 0) {
  533. wpa_s->ignore_assoc_disallow = !!atoi(value);
  534. wpa_drv_ignore_assoc_disallow(wpa_s,
  535. wpa_s->ignore_assoc_disallow);
  536. } else if (os_strcasecmp(cmd, "reject_btm_req_reason") == 0) {
  537. wpa_s->reject_btm_req_reason = atoi(value);
  538. } else if (os_strcasecmp(cmd, "get_pref_freq_list_override") == 0) {
  539. os_free(wpa_s->get_pref_freq_list_override);
  540. if (!value[0])
  541. wpa_s->get_pref_freq_list_override = NULL;
  542. else
  543. wpa_s->get_pref_freq_list_override = os_strdup(value);
  544. #ifdef CONFIG_DPP
  545. } else if (os_strcasecmp(cmd, "dpp_config_obj_override") == 0) {
  546. os_free(wpa_s->dpp_config_obj_override);
  547. wpa_s->dpp_config_obj_override = os_strdup(value);
  548. } else if (os_strcasecmp(cmd, "dpp_discovery_override") == 0) {
  549. os_free(wpa_s->dpp_discovery_override);
  550. wpa_s->dpp_discovery_override = os_strdup(value);
  551. } else if (os_strcasecmp(cmd, "dpp_groups_override") == 0) {
  552. os_free(wpa_s->dpp_groups_override);
  553. wpa_s->dpp_groups_override = os_strdup(value);
  554. } else if (os_strcasecmp(cmd, "dpp_devices_override") == 0) {
  555. os_free(wpa_s->dpp_devices_override);
  556. wpa_s->dpp_devices_override = os_strdup(value);
  557. } else if (os_strcasecmp(cmd,
  558. "dpp_ignore_netaccesskey_mismatch") == 0) {
  559. wpa_s->dpp_ignore_netaccesskey_mismatch = atoi(value);
  560. #endif /* CONFIG_DPP */
  561. #endif /* CONFIG_TESTING_OPTIONS */
  562. #ifndef CONFIG_NO_CONFIG_BLOBS
  563. } else if (os_strcmp(cmd, "blob") == 0) {
  564. ret = wpas_ctrl_set_blob(wpa_s, value);
  565. #endif /* CONFIG_NO_CONFIG_BLOBS */
  566. } else if (os_strcasecmp(cmd, "setband") == 0) {
  567. ret = wpas_ctrl_set_band(wpa_s, value);
  568. #ifdef CONFIG_MBO
  569. } else if (os_strcasecmp(cmd, "non_pref_chan") == 0) {
  570. ret = wpas_mbo_update_non_pref_chan(wpa_s, value);
  571. if (ret == 0) {
  572. value[-1] = '=';
  573. wpa_config_process_global(wpa_s->conf, cmd, -1);
  574. }
  575. } else if (os_strcasecmp(cmd, "mbo_cell_capa") == 0) {
  576. wpas_mbo_update_cell_capa(wpa_s, atoi(value));
  577. #endif /* CONFIG_MBO */
  578. } else if (os_strcasecmp(cmd, "lci") == 0) {
  579. ret = wpas_ctrl_iface_set_lci(wpa_s, value);
  580. } else if (os_strcasecmp(cmd, "tdls_trigger_control") == 0) {
  581. ret = wpa_drv_set_tdls_mode(wpa_s, atoi(value));
  582. } else if (os_strcasecmp(cmd, "relative_rssi") == 0) {
  583. ret = wpas_ctrl_set_relative_rssi(wpa_s, value);
  584. } else if (os_strcasecmp(cmd, "relative_band_adjust") == 0) {
  585. ret = wpas_ctrl_set_relative_band_adjust(wpa_s, value);
  586. } else if (os_strcasecmp(cmd, "ric_ies") == 0) {
  587. ret = wpas_ctrl_iface_set_ric_ies(wpa_s, value);
  588. } else if (os_strcasecmp(cmd, "roaming") == 0) {
  589. ret = wpa_drv_roaming(wpa_s, atoi(value), NULL);
  590. } else {
  591. value[-1] = '=';
  592. ret = wpa_config_process_global(wpa_s->conf, cmd, -1);
  593. if (ret == 0)
  594. wpa_supplicant_update_config(wpa_s);
  595. }
  596. return ret;
  597. }
  598. static int wpa_supplicant_ctrl_iface_get(struct wpa_supplicant *wpa_s,
  599. char *cmd, char *buf, size_t buflen)
  600. {
  601. int res = -1;
  602. wpa_printf(MSG_DEBUG, "CTRL_IFACE GET '%s'", cmd);
  603. if (os_strcmp(cmd, "version") == 0) {
  604. res = os_snprintf(buf, buflen, "%s", VERSION_STR);
  605. } else if (os_strcasecmp(cmd, "country") == 0) {
  606. if (wpa_s->conf->country[0] && wpa_s->conf->country[1])
  607. res = os_snprintf(buf, buflen, "%c%c",
  608. wpa_s->conf->country[0],
  609. wpa_s->conf->country[1]);
  610. #ifdef CONFIG_WIFI_DISPLAY
  611. } else if (os_strcasecmp(cmd, "wifi_display") == 0) {
  612. int enabled;
  613. if (wpa_s->global->p2p == NULL ||
  614. wpa_s->global->p2p_disabled)
  615. enabled = 0;
  616. else
  617. enabled = wpa_s->global->wifi_display;
  618. res = os_snprintf(buf, buflen, "%d", enabled);
  619. #endif /* CONFIG_WIFI_DISPLAY */
  620. #ifdef CONFIG_TESTING_GET_GTK
  621. } else if (os_strcmp(cmd, "gtk") == 0) {
  622. if (wpa_s->last_gtk_len == 0)
  623. return -1;
  624. res = wpa_snprintf_hex(buf, buflen, wpa_s->last_gtk,
  625. wpa_s->last_gtk_len);
  626. return res;
  627. #endif /* CONFIG_TESTING_GET_GTK */
  628. } else if (os_strcmp(cmd, "tls_library") == 0) {
  629. res = tls_get_library_version(buf, buflen);
  630. } else {
  631. res = wpa_config_get_value(cmd, wpa_s->conf, buf, buflen);
  632. }
  633. if (os_snprintf_error(buflen, res))
  634. return -1;
  635. return res;
  636. }
  637. #ifdef IEEE8021X_EAPOL
  638. static int wpa_supplicant_ctrl_iface_preauth(struct wpa_supplicant *wpa_s,
  639. char *addr)
  640. {
  641. u8 bssid[ETH_ALEN];
  642. struct wpa_ssid *ssid = wpa_s->current_ssid;
  643. if (hwaddr_aton(addr, bssid)) {
  644. wpa_printf(MSG_DEBUG, "CTRL_IFACE PREAUTH: invalid address "
  645. "'%s'", addr);
  646. return -1;
  647. }
  648. wpa_printf(MSG_DEBUG, "CTRL_IFACE PREAUTH " MACSTR, MAC2STR(bssid));
  649. rsn_preauth_deinit(wpa_s->wpa);
  650. if (rsn_preauth_init(wpa_s->wpa, bssid, ssid ? &ssid->eap : NULL))
  651. return -1;
  652. return 0;
  653. }
  654. #endif /* IEEE8021X_EAPOL */
  655. #ifdef CONFIG_PEERKEY
  656. /* MLME-STKSTART.request(peer) */
  657. static int wpa_supplicant_ctrl_iface_stkstart(
  658. struct wpa_supplicant *wpa_s, char *addr)
  659. {
  660. u8 peer[ETH_ALEN];
  661. if (hwaddr_aton(addr, peer)) {
  662. wpa_printf(MSG_DEBUG, "CTRL_IFACE STKSTART: invalid "
  663. "address '%s'", addr);
  664. return -1;
  665. }
  666. wpa_printf(MSG_DEBUG, "CTRL_IFACE STKSTART " MACSTR,
  667. MAC2STR(peer));
  668. return wpa_sm_stkstart(wpa_s->wpa, peer);
  669. }
  670. #endif /* CONFIG_PEERKEY */
  671. #ifdef CONFIG_TDLS
  672. static int wpa_supplicant_ctrl_iface_tdls_discover(
  673. struct wpa_supplicant *wpa_s, char *addr)
  674. {
  675. u8 peer[ETH_ALEN];
  676. int ret;
  677. if (hwaddr_aton(addr, peer)) {
  678. wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_DISCOVER: invalid "
  679. "address '%s'", addr);
  680. return -1;
  681. }
  682. wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_DISCOVER " MACSTR,
  683. MAC2STR(peer));
  684. if (wpa_tdls_is_external_setup(wpa_s->wpa))
  685. ret = wpa_tdls_send_discovery_request(wpa_s->wpa, peer);
  686. else
  687. ret = wpa_drv_tdls_oper(wpa_s, TDLS_DISCOVERY_REQ, peer);
  688. return ret;
  689. }
  690. static int wpa_supplicant_ctrl_iface_tdls_setup(
  691. struct wpa_supplicant *wpa_s, char *addr)
  692. {
  693. u8 peer[ETH_ALEN];
  694. int ret;
  695. if (hwaddr_aton(addr, peer)) {
  696. wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_SETUP: invalid "
  697. "address '%s'", addr);
  698. return -1;
  699. }
  700. wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_SETUP " MACSTR,
  701. MAC2STR(peer));
  702. if ((wpa_s->conf->tdls_external_control) &&
  703. wpa_tdls_is_external_setup(wpa_s->wpa))
  704. return wpa_drv_tdls_oper(wpa_s, TDLS_SETUP, peer);
  705. wpa_tdls_remove(wpa_s->wpa, peer);
  706. if (wpa_tdls_is_external_setup(wpa_s->wpa))
  707. ret = wpa_tdls_start(wpa_s->wpa, peer);
  708. else
  709. ret = wpa_drv_tdls_oper(wpa_s, TDLS_SETUP, peer);
  710. return ret;
  711. }
  712. static int wpa_supplicant_ctrl_iface_tdls_teardown(
  713. struct wpa_supplicant *wpa_s, char *addr)
  714. {
  715. u8 peer[ETH_ALEN];
  716. int ret;
  717. if (os_strcmp(addr, "*") == 0) {
  718. /* remove everyone */
  719. wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_TEARDOWN *");
  720. wpa_tdls_teardown_peers(wpa_s->wpa);
  721. return 0;
  722. }
  723. if (hwaddr_aton(addr, peer)) {
  724. wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_TEARDOWN: invalid "
  725. "address '%s'", addr);
  726. return -1;
  727. }
  728. wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_TEARDOWN " MACSTR,
  729. MAC2STR(peer));
  730. if ((wpa_s->conf->tdls_external_control) &&
  731. wpa_tdls_is_external_setup(wpa_s->wpa))
  732. return wpa_drv_tdls_oper(wpa_s, TDLS_TEARDOWN, peer);
  733. if (wpa_tdls_is_external_setup(wpa_s->wpa))
  734. ret = wpa_tdls_teardown_link(
  735. wpa_s->wpa, peer,
  736. WLAN_REASON_TDLS_TEARDOWN_UNSPECIFIED);
  737. else
  738. ret = wpa_drv_tdls_oper(wpa_s, TDLS_TEARDOWN, peer);
  739. return ret;
  740. }
  741. static int ctrl_iface_get_capability_tdls(
  742. struct wpa_supplicant *wpa_s, char *buf, size_t buflen)
  743. {
  744. int ret;
  745. ret = os_snprintf(buf, buflen, "%s\n",
  746. wpa_s->drv_flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT ?
  747. (wpa_s->drv_flags &
  748. WPA_DRIVER_FLAGS_TDLS_EXTERNAL_SETUP ?
  749. "EXTERNAL" : "INTERNAL") : "UNSUPPORTED");
  750. if (os_snprintf_error(buflen, ret))
  751. return -1;
  752. return ret;
  753. }
  754. static int wpa_supplicant_ctrl_iface_tdls_chan_switch(
  755. struct wpa_supplicant *wpa_s, char *cmd)
  756. {
  757. u8 peer[ETH_ALEN];
  758. struct hostapd_freq_params freq_params;
  759. u8 oper_class;
  760. char *pos, *end;
  761. if (!wpa_tdls_is_external_setup(wpa_s->wpa)) {
  762. wpa_printf(MSG_INFO,
  763. "tdls_chanswitch: Only supported with external setup");
  764. return -1;
  765. }
  766. os_memset(&freq_params, 0, sizeof(freq_params));
  767. pos = os_strchr(cmd, ' ');
  768. if (pos == NULL)
  769. return -1;
  770. *pos++ = '\0';
  771. oper_class = strtol(pos, &end, 10);
  772. if (pos == end) {
  773. wpa_printf(MSG_INFO,
  774. "tdls_chanswitch: Invalid op class provided");
  775. return -1;
  776. }
  777. pos = end;
  778. freq_params.freq = atoi(pos);
  779. if (freq_params.freq == 0) {
  780. wpa_printf(MSG_INFO, "tdls_chanswitch: Invalid freq provided");
  781. return -1;
  782. }
  783. #define SET_FREQ_SETTING(str) \
  784. do { \
  785. const char *pos2 = os_strstr(pos, " " #str "="); \
  786. if (pos2) { \
  787. pos2 += sizeof(" " #str "=") - 1; \
  788. freq_params.str = atoi(pos2); \
  789. } \
  790. } while (0)
  791. SET_FREQ_SETTING(center_freq1);
  792. SET_FREQ_SETTING(center_freq2);
  793. SET_FREQ_SETTING(bandwidth);
  794. SET_FREQ_SETTING(sec_channel_offset);
  795. #undef SET_FREQ_SETTING
  796. freq_params.ht_enabled = !!os_strstr(pos, " ht");
  797. freq_params.vht_enabled = !!os_strstr(pos, " vht");
  798. if (hwaddr_aton(cmd, peer)) {
  799. wpa_printf(MSG_DEBUG,
  800. "CTRL_IFACE TDLS_CHAN_SWITCH: Invalid address '%s'",
  801. cmd);
  802. return -1;
  803. }
  804. wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_CHAN_SWITCH " MACSTR
  805. " OP CLASS %d FREQ %d CENTER1 %d CENTER2 %d BW %d SEC_OFFSET %d%s%s",
  806. MAC2STR(peer), oper_class, freq_params.freq,
  807. freq_params.center_freq1, freq_params.center_freq2,
  808. freq_params.bandwidth, freq_params.sec_channel_offset,
  809. freq_params.ht_enabled ? " HT" : "",
  810. freq_params.vht_enabled ? " VHT" : "");
  811. return wpa_tdls_enable_chan_switch(wpa_s->wpa, peer, oper_class,
  812. &freq_params);
  813. }
  814. static int wpa_supplicant_ctrl_iface_tdls_cancel_chan_switch(
  815. struct wpa_supplicant *wpa_s, char *cmd)
  816. {
  817. u8 peer[ETH_ALEN];
  818. if (!wpa_tdls_is_external_setup(wpa_s->wpa)) {
  819. wpa_printf(MSG_INFO,
  820. "tdls_chanswitch: Only supported with external setup");
  821. return -1;
  822. }
  823. if (hwaddr_aton(cmd, peer)) {
  824. wpa_printf(MSG_DEBUG,
  825. "CTRL_IFACE TDLS_CANCEL_CHAN_SWITCH: Invalid address '%s'",
  826. cmd);
  827. return -1;
  828. }
  829. wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_CANCEL_CHAN_SWITCH " MACSTR,
  830. MAC2STR(peer));
  831. return wpa_tdls_disable_chan_switch(wpa_s->wpa, peer);
  832. }
  833. static int wpa_supplicant_ctrl_iface_tdls_link_status(
  834. struct wpa_supplicant *wpa_s, const char *addr,
  835. char *buf, size_t buflen)
  836. {
  837. u8 peer[ETH_ALEN];
  838. const char *tdls_status;
  839. int ret;
  840. if (hwaddr_aton(addr, peer)) {
  841. wpa_printf(MSG_DEBUG,
  842. "CTRL_IFACE TDLS_LINK_STATUS: Invalid address '%s'",
  843. addr);
  844. return -1;
  845. }
  846. wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_LINK_STATUS " MACSTR,
  847. MAC2STR(peer));
  848. tdls_status = wpa_tdls_get_link_status(wpa_s->wpa, peer);
  849. wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_LINK_STATUS: %s", tdls_status);
  850. ret = os_snprintf(buf, buflen, "TDLS link status: %s\n", tdls_status);
  851. if (os_snprintf_error(buflen, ret))
  852. return -1;
  853. return ret;
  854. }
  855. #endif /* CONFIG_TDLS */
  856. static int wmm_ac_ctrl_addts(struct wpa_supplicant *wpa_s, char *cmd)
  857. {
  858. char *token, *context = NULL;
  859. struct wmm_ac_ts_setup_params params = {
  860. .tsid = 0xff,
  861. .direction = 0xff,
  862. };
  863. while ((token = str_token(cmd, " ", &context))) {
  864. if (sscanf(token, "tsid=%i", &params.tsid) == 1 ||
  865. sscanf(token, "up=%i", &params.user_priority) == 1 ||
  866. sscanf(token, "nominal_msdu_size=%i",
  867. &params.nominal_msdu_size) == 1 ||
  868. sscanf(token, "mean_data_rate=%i",
  869. &params.mean_data_rate) == 1 ||
  870. sscanf(token, "min_phy_rate=%i",
  871. &params.minimum_phy_rate) == 1 ||
  872. sscanf(token, "sba=%i",
  873. &params.surplus_bandwidth_allowance) == 1)
  874. continue;
  875. if (os_strcasecmp(token, "downlink") == 0) {
  876. params.direction = WMM_TSPEC_DIRECTION_DOWNLINK;
  877. } else if (os_strcasecmp(token, "uplink") == 0) {
  878. params.direction = WMM_TSPEC_DIRECTION_UPLINK;
  879. } else if (os_strcasecmp(token, "bidi") == 0) {
  880. params.direction = WMM_TSPEC_DIRECTION_BI_DIRECTIONAL;
  881. } else if (os_strcasecmp(token, "fixed_nominal_msdu") == 0) {
  882. params.fixed_nominal_msdu = 1;
  883. } else {
  884. wpa_printf(MSG_DEBUG,
  885. "CTRL: Invalid WMM_AC_ADDTS parameter: '%s'",
  886. token);
  887. return -1;
  888. }
  889. }
  890. return wpas_wmm_ac_addts(wpa_s, &params);
  891. }
  892. static int wmm_ac_ctrl_delts(struct wpa_supplicant *wpa_s, char *cmd)
  893. {
  894. u8 tsid = atoi(cmd);
  895. return wpas_wmm_ac_delts(wpa_s, tsid);
  896. }
  897. #ifdef CONFIG_IEEE80211R
  898. static int wpa_supplicant_ctrl_iface_ft_ds(
  899. struct wpa_supplicant *wpa_s, char *addr)
  900. {
  901. u8 target_ap[ETH_ALEN];
  902. struct wpa_bss *bss;
  903. const u8 *mdie;
  904. if (hwaddr_aton(addr, target_ap)) {
  905. wpa_printf(MSG_DEBUG, "CTRL_IFACE FT_DS: invalid "
  906. "address '%s'", addr);
  907. return -1;
  908. }
  909. wpa_printf(MSG_DEBUG, "CTRL_IFACE FT_DS " MACSTR, MAC2STR(target_ap));
  910. bss = wpa_bss_get_bssid(wpa_s, target_ap);
  911. if (bss)
  912. mdie = wpa_bss_get_ie(bss, WLAN_EID_MOBILITY_DOMAIN);
  913. else
  914. mdie = NULL;
  915. return wpa_ft_start_over_ds(wpa_s->wpa, target_ap, mdie);
  916. }
  917. #endif /* CONFIG_IEEE80211R */
  918. #ifdef CONFIG_WPS
  919. static int wpa_supplicant_ctrl_iface_wps_pbc(struct wpa_supplicant *wpa_s,
  920. char *cmd)
  921. {
  922. u8 bssid[ETH_ALEN], *_bssid = bssid;
  923. #ifdef CONFIG_P2P
  924. u8 p2p_dev_addr[ETH_ALEN];
  925. #endif /* CONFIG_P2P */
  926. #ifdef CONFIG_AP
  927. u8 *_p2p_dev_addr = NULL;
  928. #endif /* CONFIG_AP */
  929. if (cmd == NULL || os_strcmp(cmd, "any") == 0) {
  930. _bssid = NULL;
  931. #ifdef CONFIG_P2P
  932. } else if (os_strncmp(cmd, "p2p_dev_addr=", 13) == 0) {
  933. if (hwaddr_aton(cmd + 13, p2p_dev_addr)) {
  934. wpa_printf(MSG_DEBUG, "CTRL_IFACE WPS_PBC: invalid "
  935. "P2P Device Address '%s'",
  936. cmd + 13);
  937. return -1;
  938. }
  939. _p2p_dev_addr = p2p_dev_addr;
  940. #endif /* CONFIG_P2P */
  941. } else if (hwaddr_aton(cmd, bssid)) {
  942. wpa_printf(MSG_DEBUG, "CTRL_IFACE WPS_PBC: invalid BSSID '%s'",
  943. cmd);
  944. return -1;
  945. }
  946. #ifdef CONFIG_AP
  947. if (wpa_s->ap_iface)
  948. return wpa_supplicant_ap_wps_pbc(wpa_s, _bssid, _p2p_dev_addr);
  949. #endif /* CONFIG_AP */
  950. return wpas_wps_start_pbc(wpa_s, _bssid, 0);
  951. }
  952. static int wpa_supplicant_ctrl_iface_wps_pin(struct wpa_supplicant *wpa_s,
  953. char *cmd, char *buf,
  954. size_t buflen)
  955. {
  956. u8 bssid[ETH_ALEN], *_bssid = bssid;
  957. char *pin;
  958. int ret;
  959. pin = os_strchr(cmd, ' ');
  960. if (pin)
  961. *pin++ = '\0';
  962. if (os_strcmp(cmd, "any") == 0)
  963. _bssid = NULL;
  964. else if (os_strcmp(cmd, "get") == 0) {
  965. if (wps_generate_pin((unsigned int *) &ret) < 0)
  966. return -1;
  967. goto done;
  968. } else if (hwaddr_aton(cmd, bssid)) {
  969. wpa_printf(MSG_DEBUG, "CTRL_IFACE WPS_PIN: invalid BSSID '%s'",
  970. cmd);
  971. return -1;
  972. }
  973. #ifdef CONFIG_AP
  974. if (wpa_s->ap_iface) {
  975. int timeout = 0;
  976. char *pos;
  977. if (pin) {
  978. pos = os_strchr(pin, ' ');
  979. if (pos) {
  980. *pos++ = '\0';
  981. timeout = atoi(pos);
  982. }
  983. }
  984. return wpa_supplicant_ap_wps_pin(wpa_s, _bssid, pin,
  985. buf, buflen, timeout);
  986. }
  987. #endif /* CONFIG_AP */
  988. if (pin) {
  989. ret = wpas_wps_start_pin(wpa_s, _bssid, pin, 0,
  990. DEV_PW_DEFAULT);
  991. if (ret < 0)
  992. return -1;
  993. ret = os_snprintf(buf, buflen, "%s", pin);
  994. if (os_snprintf_error(buflen, ret))
  995. return -1;
  996. return ret;
  997. }
  998. ret = wpas_wps_start_pin(wpa_s, _bssid, NULL, 0, DEV_PW_DEFAULT);
  999. if (ret < 0)
  1000. return -1;
  1001. done:
  1002. /* Return the generated PIN */
  1003. ret = os_snprintf(buf, buflen, "%08d", ret);
  1004. if (os_snprintf_error(buflen, ret))
  1005. return -1;
  1006. return ret;
  1007. }
  1008. static int wpa_supplicant_ctrl_iface_wps_check_pin(
  1009. struct wpa_supplicant *wpa_s, char *cmd, char *buf, size_t buflen)
  1010. {
  1011. char pin[9];
  1012. size_t len;
  1013. char *pos;
  1014. int ret;
  1015. wpa_hexdump_ascii_key(MSG_DEBUG, "WPS_CHECK_PIN",
  1016. (u8 *) cmd, os_strlen(cmd));
  1017. for (pos = cmd, len = 0; *pos != '\0'; pos++) {
  1018. if (*pos < '0' || *pos > '9')
  1019. continue;
  1020. pin[len++] = *pos;
  1021. if (len == 9) {
  1022. wpa_printf(MSG_DEBUG, "WPS: Too long PIN");
  1023. return -1;
  1024. }
  1025. }
  1026. if (len != 4 && len != 8) {
  1027. wpa_printf(MSG_DEBUG, "WPS: Invalid PIN length %d", (int) len);
  1028. return -1;
  1029. }
  1030. pin[len] = '\0';
  1031. if (len == 8) {
  1032. unsigned int pin_val;
  1033. pin_val = atoi(pin);
  1034. if (!wps_pin_valid(pin_val)) {
  1035. wpa_printf(MSG_DEBUG, "WPS: Invalid checksum digit");
  1036. ret = os_snprintf(buf, buflen, "FAIL-CHECKSUM\n");
  1037. if (os_snprintf_error(buflen, ret))
  1038. return -1;
  1039. return ret;
  1040. }
  1041. }
  1042. ret = os_snprintf(buf, buflen, "%s", pin);
  1043. if (os_snprintf_error(buflen, ret))
  1044. return -1;
  1045. return ret;
  1046. }
  1047. #ifdef CONFIG_WPS_NFC
  1048. static int wpa_supplicant_ctrl_iface_wps_nfc(struct wpa_supplicant *wpa_s,
  1049. char *cmd)
  1050. {
  1051. u8 bssid[ETH_ALEN], *_bssid = bssid;
  1052. if (cmd == NULL || cmd[0] == '\0')
  1053. _bssid = NULL;
  1054. else if (hwaddr_aton(cmd, bssid))
  1055. return -1;
  1056. return wpas_wps_start_nfc(wpa_s, NULL, _bssid, NULL, 0, 0, NULL, NULL,
  1057. 0, 0);
  1058. }
  1059. static int wpa_supplicant_ctrl_iface_wps_nfc_config_token(
  1060. struct wpa_supplicant *wpa_s, char *cmd, char *reply, size_t max_len)
  1061. {
  1062. int ndef;
  1063. struct wpabuf *buf;
  1064. int res;
  1065. char *pos;
  1066. pos = os_strchr(cmd, ' ');
  1067. if (pos)
  1068. *pos++ = '\0';
  1069. if (os_strcmp(cmd, "WPS") == 0)
  1070. ndef = 0;
  1071. else if (os_strcmp(cmd, "NDEF") == 0)
  1072. ndef = 1;
  1073. else
  1074. return -1;
  1075. buf = wpas_wps_nfc_config_token(wpa_s, ndef, pos);
  1076. if (buf == NULL)
  1077. return -1;
  1078. res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
  1079. wpabuf_len(buf));
  1080. reply[res++] = '\n';
  1081. reply[res] = '\0';
  1082. wpabuf_free(buf);
  1083. return res;
  1084. }
  1085. static int wpa_supplicant_ctrl_iface_wps_nfc_token(
  1086. struct wpa_supplicant *wpa_s, char *cmd, char *reply, size_t max_len)
  1087. {
  1088. int ndef;
  1089. struct wpabuf *buf;
  1090. int res;
  1091. if (os_strcmp(cmd, "WPS") == 0)
  1092. ndef = 0;
  1093. else if (os_strcmp(cmd, "NDEF") == 0)
  1094. ndef = 1;
  1095. else
  1096. return -1;
  1097. buf = wpas_wps_nfc_token(wpa_s, ndef);
  1098. if (buf == NULL)
  1099. return -1;
  1100. res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
  1101. wpabuf_len(buf));
  1102. reply[res++] = '\n';
  1103. reply[res] = '\0';
  1104. wpabuf_free(buf);
  1105. return res;
  1106. }
  1107. static int wpa_supplicant_ctrl_iface_wps_nfc_tag_read(
  1108. struct wpa_supplicant *wpa_s, char *pos)
  1109. {
  1110. size_t len;
  1111. struct wpabuf *buf;
  1112. int ret;
  1113. char *freq;
  1114. int forced_freq = 0;
  1115. freq = strstr(pos, " freq=");
  1116. if (freq) {
  1117. *freq = '\0';
  1118. freq += 6;
  1119. forced_freq = atoi(freq);
  1120. }
  1121. len = os_strlen(pos);
  1122. if (len & 0x01)
  1123. return -1;
  1124. len /= 2;
  1125. buf = wpabuf_alloc(len);
  1126. if (buf == NULL)
  1127. return -1;
  1128. if (hexstr2bin(pos, wpabuf_put(buf, len), len) < 0) {
  1129. wpabuf_free(buf);
  1130. return -1;
  1131. }
  1132. ret = wpas_wps_nfc_tag_read(wpa_s, buf, forced_freq);
  1133. wpabuf_free(buf);
  1134. return ret;
  1135. }
  1136. static int wpas_ctrl_nfc_get_handover_req_wps(struct wpa_supplicant *wpa_s,
  1137. char *reply, size_t max_len,
  1138. int ndef)
  1139. {
  1140. struct wpabuf *buf;
  1141. int res;
  1142. buf = wpas_wps_nfc_handover_req(wpa_s, ndef);
  1143. if (buf == NULL)
  1144. return -1;
  1145. res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
  1146. wpabuf_len(buf));
  1147. reply[res++] = '\n';
  1148. reply[res] = '\0';
  1149. wpabuf_free(buf);
  1150. return res;
  1151. }
  1152. #ifdef CONFIG_P2P
  1153. static int wpas_ctrl_nfc_get_handover_req_p2p(struct wpa_supplicant *wpa_s,
  1154. char *reply, size_t max_len,
  1155. int ndef)
  1156. {
  1157. struct wpabuf *buf;
  1158. int res;
  1159. buf = wpas_p2p_nfc_handover_req(wpa_s, ndef);
  1160. if (buf == NULL) {
  1161. wpa_printf(MSG_DEBUG, "P2P: Could not generate NFC handover request");
  1162. return -1;
  1163. }
  1164. res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
  1165. wpabuf_len(buf));
  1166. reply[res++] = '\n';
  1167. reply[res] = '\0';
  1168. wpabuf_free(buf);
  1169. return res;
  1170. }
  1171. #endif /* CONFIG_P2P */
  1172. static int wpas_ctrl_nfc_get_handover_req(struct wpa_supplicant *wpa_s,
  1173. char *cmd, char *reply,
  1174. size_t max_len)
  1175. {
  1176. char *pos;
  1177. int ndef;
  1178. pos = os_strchr(cmd, ' ');
  1179. if (pos == NULL)
  1180. return -1;
  1181. *pos++ = '\0';
  1182. if (os_strcmp(cmd, "WPS") == 0)
  1183. ndef = 0;
  1184. else if (os_strcmp(cmd, "NDEF") == 0)
  1185. ndef = 1;
  1186. else
  1187. return -1;
  1188. if (os_strcmp(pos, "WPS") == 0 || os_strcmp(pos, "WPS-CR") == 0) {
  1189. if (!ndef)
  1190. return -1;
  1191. return wpas_ctrl_nfc_get_handover_req_wps(
  1192. wpa_s, reply, max_len, ndef);
  1193. }
  1194. #ifdef CONFIG_P2P
  1195. if (os_strcmp(pos, "P2P-CR") == 0) {
  1196. return wpas_ctrl_nfc_get_handover_req_p2p(
  1197. wpa_s, reply, max_len, ndef);
  1198. }
  1199. #endif /* CONFIG_P2P */
  1200. return -1;
  1201. }
  1202. static int wpas_ctrl_nfc_get_handover_sel_wps(struct wpa_supplicant *wpa_s,
  1203. char *reply, size_t max_len,
  1204. int ndef, int cr, char *uuid)
  1205. {
  1206. struct wpabuf *buf;
  1207. int res;
  1208. buf = wpas_wps_nfc_handover_sel(wpa_s, ndef, cr, uuid);
  1209. if (buf == NULL)
  1210. return -1;
  1211. res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
  1212. wpabuf_len(buf));
  1213. reply[res++] = '\n';
  1214. reply[res] = '\0';
  1215. wpabuf_free(buf);
  1216. return res;
  1217. }
  1218. #ifdef CONFIG_P2P
  1219. static int wpas_ctrl_nfc_get_handover_sel_p2p(struct wpa_supplicant *wpa_s,
  1220. char *reply, size_t max_len,
  1221. int ndef, int tag)
  1222. {
  1223. struct wpabuf *buf;
  1224. int res;
  1225. buf = wpas_p2p_nfc_handover_sel(wpa_s, ndef, tag);
  1226. if (buf == NULL)
  1227. return -1;
  1228. res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
  1229. wpabuf_len(buf));
  1230. reply[res++] = '\n';
  1231. reply[res] = '\0';
  1232. wpabuf_free(buf);
  1233. return res;
  1234. }
  1235. #endif /* CONFIG_P2P */
  1236. static int wpas_ctrl_nfc_get_handover_sel(struct wpa_supplicant *wpa_s,
  1237. char *cmd, char *reply,
  1238. size_t max_len)
  1239. {
  1240. char *pos, *pos2;
  1241. int ndef;
  1242. pos = os_strchr(cmd, ' ');
  1243. if (pos == NULL)
  1244. return -1;
  1245. *pos++ = '\0';
  1246. if (os_strcmp(cmd, "WPS") == 0)
  1247. ndef = 0;
  1248. else if (os_strcmp(cmd, "NDEF") == 0)
  1249. ndef = 1;
  1250. else
  1251. return -1;
  1252. pos2 = os_strchr(pos, ' ');
  1253. if (pos2)
  1254. *pos2++ = '\0';
  1255. if (os_strcmp(pos, "WPS") == 0 || os_strcmp(pos, "WPS-CR") == 0) {
  1256. if (!ndef)
  1257. return -1;
  1258. return wpas_ctrl_nfc_get_handover_sel_wps(
  1259. wpa_s, reply, max_len, ndef,
  1260. os_strcmp(pos, "WPS-CR") == 0, pos2);
  1261. }
  1262. #ifdef CONFIG_P2P
  1263. if (os_strcmp(pos, "P2P-CR") == 0) {
  1264. return wpas_ctrl_nfc_get_handover_sel_p2p(
  1265. wpa_s, reply, max_len, ndef, 0);
  1266. }
  1267. if (os_strcmp(pos, "P2P-CR-TAG") == 0) {
  1268. return wpas_ctrl_nfc_get_handover_sel_p2p(
  1269. wpa_s, reply, max_len, ndef, 1);
  1270. }
  1271. #endif /* CONFIG_P2P */
  1272. return -1;
  1273. }
  1274. static int wpas_ctrl_nfc_report_handover(struct wpa_supplicant *wpa_s,
  1275. char *cmd)
  1276. {
  1277. size_t len;
  1278. struct wpabuf *req, *sel;
  1279. int ret;
  1280. char *pos, *role, *type, *pos2;
  1281. #ifdef CONFIG_P2P
  1282. char *freq;
  1283. int forced_freq = 0;
  1284. freq = strstr(cmd, " freq=");
  1285. if (freq) {
  1286. *freq = '\0';
  1287. freq += 6;
  1288. forced_freq = atoi(freq);
  1289. }
  1290. #endif /* CONFIG_P2P */
  1291. role = cmd;
  1292. pos = os_strchr(role, ' ');
  1293. if (pos == NULL) {
  1294. wpa_printf(MSG_DEBUG, "NFC: Missing type in handover report");
  1295. return -1;
  1296. }
  1297. *pos++ = '\0';
  1298. type = pos;
  1299. pos = os_strchr(type, ' ');
  1300. if (pos == NULL) {
  1301. wpa_printf(MSG_DEBUG, "NFC: Missing request message in handover report");
  1302. return -1;
  1303. }
  1304. *pos++ = '\0';
  1305. pos2 = os_strchr(pos, ' ');
  1306. if (pos2 == NULL) {
  1307. wpa_printf(MSG_DEBUG, "NFC: Missing select message in handover report");
  1308. return -1;
  1309. }
  1310. *pos2++ = '\0';
  1311. len = os_strlen(pos);
  1312. if (len & 0x01) {
  1313. wpa_printf(MSG_DEBUG, "NFC: Invalid request message length in handover report");
  1314. return -1;
  1315. }
  1316. len /= 2;
  1317. req = wpabuf_alloc(len);
  1318. if (req == NULL) {
  1319. wpa_printf(MSG_DEBUG, "NFC: Failed to allocate memory for request message");
  1320. return -1;
  1321. }
  1322. if (hexstr2bin(pos, wpabuf_put(req, len), len) < 0) {
  1323. wpa_printf(MSG_DEBUG, "NFC: Invalid request message hexdump in handover report");
  1324. wpabuf_free(req);
  1325. return -1;
  1326. }
  1327. len = os_strlen(pos2);
  1328. if (len & 0x01) {
  1329. wpa_printf(MSG_DEBUG, "NFC: Invalid select message length in handover report");
  1330. wpabuf_free(req);
  1331. return -1;
  1332. }
  1333. len /= 2;
  1334. sel = wpabuf_alloc(len);
  1335. if (sel == NULL) {
  1336. wpa_printf(MSG_DEBUG, "NFC: Failed to allocate memory for select message");
  1337. wpabuf_free(req);
  1338. return -1;
  1339. }
  1340. if (hexstr2bin(pos2, wpabuf_put(sel, len), len) < 0) {
  1341. wpa_printf(MSG_DEBUG, "NFC: Invalid select message hexdump in handover report");
  1342. wpabuf_free(req);
  1343. wpabuf_free(sel);
  1344. return -1;
  1345. }
  1346. wpa_printf(MSG_DEBUG, "NFC: Connection handover reported - role=%s type=%s req_len=%d sel_len=%d",
  1347. role, type, (int) wpabuf_len(req), (int) wpabuf_len(sel));
  1348. if (os_strcmp(role, "INIT") == 0 && os_strcmp(type, "WPS") == 0) {
  1349. ret = wpas_wps_nfc_report_handover(wpa_s, req, sel);
  1350. #ifdef CONFIG_AP
  1351. } else if (os_strcmp(role, "RESP") == 0 && os_strcmp(type, "WPS") == 0)
  1352. {
  1353. ret = wpas_ap_wps_nfc_report_handover(wpa_s, req, sel);
  1354. if (ret < 0)
  1355. ret = wpas_er_wps_nfc_report_handover(wpa_s, req, sel);
  1356. #endif /* CONFIG_AP */
  1357. #ifdef CONFIG_P2P
  1358. } else if (os_strcmp(role, "INIT") == 0 && os_strcmp(type, "P2P") == 0)
  1359. {
  1360. ret = wpas_p2p_nfc_report_handover(wpa_s, 1, req, sel, 0);
  1361. } else if (os_strcmp(role, "RESP") == 0 && os_strcmp(type, "P2P") == 0)
  1362. {
  1363. ret = wpas_p2p_nfc_report_handover(wpa_s, 0, req, sel,
  1364. forced_freq);
  1365. #endif /* CONFIG_P2P */
  1366. } else {
  1367. wpa_printf(MSG_DEBUG, "NFC: Unsupported connection handover "
  1368. "reported: role=%s type=%s", role, type);
  1369. ret = -1;
  1370. }
  1371. wpabuf_free(req);
  1372. wpabuf_free(sel);
  1373. if (ret)
  1374. wpa_printf(MSG_DEBUG, "NFC: Failed to process reported handover messages");
  1375. return ret;
  1376. }
  1377. #endif /* CONFIG_WPS_NFC */
  1378. static int wpa_supplicant_ctrl_iface_wps_reg(struct wpa_supplicant *wpa_s,
  1379. char *cmd)
  1380. {
  1381. u8 bssid[ETH_ALEN];
  1382. char *pin;
  1383. char *new_ssid;
  1384. char *new_auth;
  1385. char *new_encr;
  1386. char *new_key;
  1387. struct wps_new_ap_settings ap;
  1388. pin = os_strchr(cmd, ' ');
  1389. if (pin == NULL)
  1390. return -1;
  1391. *pin++ = '\0';
  1392. if (hwaddr_aton(cmd, bssid)) {
  1393. wpa_printf(MSG_DEBUG, "CTRL_IFACE WPS_REG: invalid BSSID '%s'",
  1394. cmd);
  1395. return -1;
  1396. }
  1397. new_ssid = os_strchr(pin, ' ');
  1398. if (new_ssid == NULL)
  1399. return wpas_wps_start_reg(wpa_s, bssid, pin, NULL);
  1400. *new_ssid++ = '\0';
  1401. new_auth = os_strchr(new_ssid, ' ');
  1402. if (new_auth == NULL)
  1403. return -1;
  1404. *new_auth++ = '\0';
  1405. new_encr = os_strchr(new_auth, ' ');
  1406. if (new_encr == NULL)
  1407. return -1;
  1408. *new_encr++ = '\0';
  1409. new_key = os_strchr(new_encr, ' ');
  1410. if (new_key == NULL)
  1411. return -1;
  1412. *new_key++ = '\0';
  1413. os_memset(&ap, 0, sizeof(ap));
  1414. ap.ssid_hex = new_ssid;
  1415. ap.auth = new_auth;
  1416. ap.encr = new_encr;
  1417. ap.key_hex = new_key;
  1418. return wpas_wps_start_reg(wpa_s, bssid, pin, &ap);
  1419. }
  1420. #ifdef CONFIG_AP
  1421. static int wpa_supplicant_ctrl_iface_wps_ap_pin(struct wpa_supplicant *wpa_s,
  1422. char *cmd, char *buf,
  1423. size_t buflen)
  1424. {
  1425. int timeout = 300;
  1426. char *pos;
  1427. const char *pin_txt;
  1428. if (!wpa_s->ap_iface)
  1429. return -1;
  1430. pos = os_strchr(cmd, ' ');
  1431. if (pos)
  1432. *pos++ = '\0';
  1433. if (os_strcmp(cmd, "disable") == 0) {
  1434. wpas_wps_ap_pin_disable(wpa_s);
  1435. return os_snprintf(buf, buflen, "OK\n");
  1436. }
  1437. if (os_strcmp(cmd, "random") == 0) {
  1438. if (pos)
  1439. timeout = atoi(pos);
  1440. pin_txt = wpas_wps_ap_pin_random(wpa_s, timeout);
  1441. if (pin_txt == NULL)
  1442. return -1;
  1443. return os_snprintf(buf, buflen, "%s", pin_txt);
  1444. }
  1445. if (os_strcmp(cmd, "get") == 0) {
  1446. pin_txt = wpas_wps_ap_pin_get(wpa_s);
  1447. if (pin_txt == NULL)
  1448. return -1;
  1449. return os_snprintf(buf, buflen, "%s", pin_txt);
  1450. }
  1451. if (os_strcmp(cmd, "set") == 0) {
  1452. char *pin;
  1453. if (pos == NULL)
  1454. return -1;
  1455. pin = pos;
  1456. pos = os_strchr(pos, ' ');
  1457. if (pos) {
  1458. *pos++ = '\0';
  1459. timeout = atoi(pos);
  1460. }
  1461. if (os_strlen(pin) > buflen)
  1462. return -1;
  1463. if (wpas_wps_ap_pin_set(wpa_s, pin, timeout) < 0)
  1464. return -1;
  1465. return os_snprintf(buf, buflen, "%s", pin);
  1466. }
  1467. return -1;
  1468. }
  1469. #endif /* CONFIG_AP */
  1470. #ifdef CONFIG_WPS_ER
  1471. static int wpa_supplicant_ctrl_iface_wps_er_pin(struct wpa_supplicant *wpa_s,
  1472. char *cmd)
  1473. {
  1474. char *uuid = cmd, *pin, *pos;
  1475. u8 addr_buf[ETH_ALEN], *addr = NULL;
  1476. pin = os_strchr(uuid, ' ');
  1477. if (pin == NULL)
  1478. return -1;
  1479. *pin++ = '\0';
  1480. pos = os_strchr(pin, ' ');
  1481. if (pos) {
  1482. *pos++ = '\0';
  1483. if (hwaddr_aton(pos, addr_buf) == 0)
  1484. addr = addr_buf;
  1485. }
  1486. return wpas_wps_er_add_pin(wpa_s, addr, uuid, pin);
  1487. }
  1488. static int wpa_supplicant_ctrl_iface_wps_er_learn(struct wpa_supplicant *wpa_s,
  1489. char *cmd)
  1490. {
  1491. char *uuid = cmd, *pin;
  1492. pin = os_strchr(uuid, ' ');
  1493. if (pin == NULL)
  1494. return -1;
  1495. *pin++ = '\0';
  1496. return wpas_wps_er_learn(wpa_s, uuid, pin);
  1497. }
  1498. static int wpa_supplicant_ctrl_iface_wps_er_set_config(
  1499. struct wpa_supplicant *wpa_s, char *cmd)
  1500. {
  1501. char *uuid = cmd, *id;
  1502. id = os_strchr(uuid, ' ');
  1503. if (id == NULL)
  1504. return -1;
  1505. *id++ = '\0';
  1506. return wpas_wps_er_set_config(wpa_s, uuid, atoi(id));
  1507. }
  1508. static int wpa_supplicant_ctrl_iface_wps_er_config(
  1509. struct wpa_supplicant *wpa_s, char *cmd)
  1510. {
  1511. char *pin;
  1512. char *new_ssid;
  1513. char *new_auth;
  1514. char *new_encr;
  1515. char *new_key;
  1516. struct wps_new_ap_settings ap;
  1517. pin = os_strchr(cmd, ' ');
  1518. if (pin == NULL)
  1519. return -1;
  1520. *pin++ = '\0';
  1521. new_ssid = os_strchr(pin, ' ');
  1522. if (new_ssid == NULL)
  1523. return -1;
  1524. *new_ssid++ = '\0';
  1525. new_auth = os_strchr(new_ssid, ' ');
  1526. if (new_auth == NULL)
  1527. return -1;
  1528. *new_auth++ = '\0';
  1529. new_encr = os_strchr(new_auth, ' ');
  1530. if (new_encr == NULL)
  1531. return -1;
  1532. *new_encr++ = '\0';
  1533. new_key = os_strchr(new_encr, ' ');
  1534. if (new_key == NULL)
  1535. return -1;
  1536. *new_key++ = '\0';
  1537. os_memset(&ap, 0, sizeof(ap));
  1538. ap.ssid_hex = new_ssid;
  1539. ap.auth = new_auth;
  1540. ap.encr = new_encr;
  1541. ap.key_hex = new_key;
  1542. return wpas_wps_er_config(wpa_s, cmd, pin, &ap);
  1543. }
  1544. #ifdef CONFIG_WPS_NFC
  1545. static int wpa_supplicant_ctrl_iface_wps_er_nfc_config_token(
  1546. struct wpa_supplicant *wpa_s, char *cmd, char *reply, size_t max_len)
  1547. {
  1548. int ndef;
  1549. struct wpabuf *buf;
  1550. int res;
  1551. char *uuid;
  1552. uuid = os_strchr(cmd, ' ');
  1553. if (uuid == NULL)
  1554. return -1;
  1555. *uuid++ = '\0';
  1556. if (os_strcmp(cmd, "WPS") == 0)
  1557. ndef = 0;
  1558. else if (os_strcmp(cmd, "NDEF") == 0)
  1559. ndef = 1;
  1560. else
  1561. return -1;
  1562. buf = wpas_wps_er_nfc_config_token(wpa_s, ndef, uuid);
  1563. if (buf == NULL)
  1564. return -1;
  1565. res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
  1566. wpabuf_len(buf));
  1567. reply[res++] = '\n';
  1568. reply[res] = '\0';
  1569. wpabuf_free(buf);
  1570. return res;
  1571. }
  1572. #endif /* CONFIG_WPS_NFC */
  1573. #endif /* CONFIG_WPS_ER */
  1574. #endif /* CONFIG_WPS */
  1575. #ifdef CONFIG_IBSS_RSN
  1576. static int wpa_supplicant_ctrl_iface_ibss_rsn(
  1577. struct wpa_supplicant *wpa_s, char *addr)
  1578. {
  1579. u8 peer[ETH_ALEN];
  1580. if (hwaddr_aton(addr, peer)) {
  1581. wpa_printf(MSG_DEBUG, "CTRL_IFACE IBSS_RSN: invalid "
  1582. "address '%s'", addr);
  1583. return -1;
  1584. }
  1585. wpa_printf(MSG_DEBUG, "CTRL_IFACE IBSS_RSN " MACSTR,
  1586. MAC2STR(peer));
  1587. return ibss_rsn_start(wpa_s->ibss_rsn, peer);
  1588. }
  1589. #endif /* CONFIG_IBSS_RSN */
  1590. static int wpa_supplicant_ctrl_iface_ctrl_rsp(struct wpa_supplicant *wpa_s,
  1591. char *rsp)
  1592. {
  1593. #ifdef IEEE8021X_EAPOL
  1594. char *pos, *id_pos;
  1595. int id;
  1596. struct wpa_ssid *ssid;
  1597. pos = os_strchr(rsp, '-');
  1598. if (pos == NULL)
  1599. return -1;
  1600. *pos++ = '\0';
  1601. id_pos = pos;
  1602. pos = os_strchr(pos, ':');
  1603. if (pos == NULL)
  1604. return -1;
  1605. *pos++ = '\0';
  1606. id = atoi(id_pos);
  1607. wpa_printf(MSG_DEBUG, "CTRL_IFACE: field=%s id=%d", rsp, id);
  1608. wpa_hexdump_ascii_key(MSG_DEBUG, "CTRL_IFACE: value",
  1609. (u8 *) pos, os_strlen(pos));
  1610. ssid = wpa_config_get_network(wpa_s->conf, id);
  1611. if (ssid == NULL) {
  1612. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find SSID id=%d "
  1613. "to update", id);
  1614. return -1;
  1615. }
  1616. return wpa_supplicant_ctrl_iface_ctrl_rsp_handle(wpa_s, ssid, rsp,
  1617. pos);
  1618. #else /* IEEE8021X_EAPOL */
  1619. wpa_printf(MSG_DEBUG, "CTRL_IFACE: 802.1X not included");
  1620. return -1;
  1621. #endif /* IEEE8021X_EAPOL */
  1622. }
  1623. static int wpa_supplicant_ctrl_iface_status(struct wpa_supplicant *wpa_s,
  1624. const char *params,
  1625. char *buf, size_t buflen)
  1626. {
  1627. char *pos, *end, tmp[30];
  1628. int res, verbose, wps, ret;
  1629. #ifdef CONFIG_HS20
  1630. const u8 *hs20;
  1631. #endif /* CONFIG_HS20 */
  1632. const u8 *sess_id;
  1633. size_t sess_id_len;
  1634. if (os_strcmp(params, "-DRIVER") == 0)
  1635. return wpa_drv_status(wpa_s, buf, buflen);
  1636. verbose = os_strcmp(params, "-VERBOSE") == 0;
  1637. wps = os_strcmp(params, "-WPS") == 0;
  1638. pos = buf;
  1639. end = buf + buflen;
  1640. if (wpa_s->wpa_state >= WPA_ASSOCIATED) {
  1641. struct wpa_ssid *ssid = wpa_s->current_ssid;
  1642. ret = os_snprintf(pos, end - pos, "bssid=" MACSTR "\n",
  1643. MAC2STR(wpa_s->bssid));
  1644. if (os_snprintf_error(end - pos, ret))
  1645. return pos - buf;
  1646. pos += ret;
  1647. ret = os_snprintf(pos, end - pos, "freq=%u\n",
  1648. wpa_s->assoc_freq);
  1649. if (os_snprintf_error(end - pos, ret))
  1650. return pos - buf;
  1651. pos += ret;
  1652. if (ssid) {
  1653. u8 *_ssid = ssid->ssid;
  1654. size_t ssid_len = ssid->ssid_len;
  1655. u8 ssid_buf[SSID_MAX_LEN];
  1656. if (ssid_len == 0) {
  1657. int _res = wpa_drv_get_ssid(wpa_s, ssid_buf);
  1658. if (_res < 0)
  1659. ssid_len = 0;
  1660. else
  1661. ssid_len = _res;
  1662. _ssid = ssid_buf;
  1663. }
  1664. ret = os_snprintf(pos, end - pos, "ssid=%s\nid=%d\n",
  1665. wpa_ssid_txt(_ssid, ssid_len),
  1666. ssid->id);
  1667. if (os_snprintf_error(end - pos, ret))
  1668. return pos - buf;
  1669. pos += ret;
  1670. if (wps && ssid->passphrase &&
  1671. wpa_key_mgmt_wpa_psk(ssid->key_mgmt) &&
  1672. (ssid->mode == WPAS_MODE_AP ||
  1673. ssid->mode == WPAS_MODE_P2P_GO)) {
  1674. ret = os_snprintf(pos, end - pos,
  1675. "passphrase=%s\n",
  1676. ssid->passphrase);
  1677. if (os_snprintf_error(end - pos, ret))
  1678. return pos - buf;
  1679. pos += ret;
  1680. }
  1681. if (ssid->id_str) {
  1682. ret = os_snprintf(pos, end - pos,
  1683. "id_str=%s\n",
  1684. ssid->id_str);
  1685. if (os_snprintf_error(end - pos, ret))
  1686. return pos - buf;
  1687. pos += ret;
  1688. }
  1689. switch (ssid->mode) {
  1690. case WPAS_MODE_INFRA:
  1691. ret = os_snprintf(pos, end - pos,
  1692. "mode=station\n");
  1693. break;
  1694. case WPAS_MODE_IBSS:
  1695. ret = os_snprintf(pos, end - pos,
  1696. "mode=IBSS\n");
  1697. break;
  1698. case WPAS_MODE_AP:
  1699. ret = os_snprintf(pos, end - pos,
  1700. "mode=AP\n");
  1701. break;
  1702. case WPAS_MODE_P2P_GO:
  1703. ret = os_snprintf(pos, end - pos,
  1704. "mode=P2P GO\n");
  1705. break;
  1706. case WPAS_MODE_P2P_GROUP_FORMATION:
  1707. ret = os_snprintf(pos, end - pos,
  1708. "mode=P2P GO - group "
  1709. "formation\n");
  1710. break;
  1711. case WPAS_MODE_MESH:
  1712. ret = os_snprintf(pos, end - pos,
  1713. "mode=mesh\n");
  1714. break;
  1715. default:
  1716. ret = 0;
  1717. break;
  1718. }
  1719. if (os_snprintf_error(end - pos, ret))
  1720. return pos - buf;
  1721. pos += ret;
  1722. }
  1723. #ifdef CONFIG_AP
  1724. if (wpa_s->ap_iface) {
  1725. pos += ap_ctrl_iface_wpa_get_status(wpa_s, pos,
  1726. end - pos,
  1727. verbose);
  1728. } else
  1729. #endif /* CONFIG_AP */
  1730. pos += wpa_sm_get_status(wpa_s->wpa, pos, end - pos, verbose);
  1731. }
  1732. #ifdef CONFIG_SME
  1733. #ifdef CONFIG_SAE
  1734. if (wpa_s->wpa_state >= WPA_ASSOCIATED &&
  1735. #ifdef CONFIG_AP
  1736. !wpa_s->ap_iface &&
  1737. #endif /* CONFIG_AP */
  1738. wpa_s->sme.sae.state == SAE_ACCEPTED) {
  1739. ret = os_snprintf(pos, end - pos, "sae_group=%d\n",
  1740. wpa_s->sme.sae.group);
  1741. if (os_snprintf_error(end - pos, ret))
  1742. return pos - buf;
  1743. pos += ret;
  1744. }
  1745. #endif /* CONFIG_SAE */
  1746. #endif /* CONFIG_SME */
  1747. ret = os_snprintf(pos, end - pos, "wpa_state=%s\n",
  1748. wpa_supplicant_state_txt(wpa_s->wpa_state));
  1749. if (os_snprintf_error(end - pos, ret))
  1750. return pos - buf;
  1751. pos += ret;
  1752. if (wpa_s->l2 &&
  1753. l2_packet_get_ip_addr(wpa_s->l2, tmp, sizeof(tmp)) >= 0) {
  1754. ret = os_snprintf(pos, end - pos, "ip_address=%s\n", tmp);
  1755. if (os_snprintf_error(end - pos, ret))
  1756. return pos - buf;
  1757. pos += ret;
  1758. }
  1759. #ifdef CONFIG_P2P
  1760. if (wpa_s->global->p2p) {
  1761. ret = os_snprintf(pos, end - pos, "p2p_device_address=" MACSTR
  1762. "\n", MAC2STR(wpa_s->global->p2p_dev_addr));
  1763. if (os_snprintf_error(end - pos, ret))
  1764. return pos - buf;
  1765. pos += ret;
  1766. }
  1767. #endif /* CONFIG_P2P */
  1768. ret = os_snprintf(pos, end - pos, "address=" MACSTR "\n",
  1769. MAC2STR(wpa_s->own_addr));
  1770. if (os_snprintf_error(end - pos, ret))
  1771. return pos - buf;
  1772. pos += ret;
  1773. #ifdef CONFIG_HS20
  1774. if (wpa_s->current_bss &&
  1775. (hs20 = wpa_bss_get_vendor_ie(wpa_s->current_bss,
  1776. HS20_IE_VENDOR_TYPE)) &&
  1777. wpa_s->wpa_proto == WPA_PROTO_RSN &&
  1778. wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt)) {
  1779. int release = 1;
  1780. if (hs20[1] >= 5) {
  1781. u8 rel_num = (hs20[6] & 0xf0) >> 4;
  1782. release = rel_num + 1;
  1783. }
  1784. ret = os_snprintf(pos, end - pos, "hs20=%d\n", release);
  1785. if (os_snprintf_error(end - pos, ret))
  1786. return pos - buf;
  1787. pos += ret;
  1788. }
  1789. if (wpa_s->current_ssid) {
  1790. struct wpa_cred *cred;
  1791. char *type;
  1792. for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
  1793. size_t i;
  1794. if (wpa_s->current_ssid->parent_cred != cred)
  1795. continue;
  1796. if (cred->provisioning_sp) {
  1797. ret = os_snprintf(pos, end - pos,
  1798. "provisioning_sp=%s\n",
  1799. cred->provisioning_sp);
  1800. if (os_snprintf_error(end - pos, ret))
  1801. return pos - buf;
  1802. pos += ret;
  1803. }
  1804. if (!cred->domain)
  1805. goto no_domain;
  1806. i = 0;
  1807. if (wpa_s->current_bss && wpa_s->current_bss->anqp) {
  1808. struct wpabuf *names =
  1809. wpa_s->current_bss->anqp->domain_name;
  1810. for (i = 0; names && i < cred->num_domain; i++)
  1811. {
  1812. if (domain_name_list_contains(
  1813. names, cred->domain[i], 1))
  1814. break;
  1815. }
  1816. if (i == cred->num_domain)
  1817. i = 0; /* show first entry by default */
  1818. }
  1819. ret = os_snprintf(pos, end - pos, "home_sp=%s\n",
  1820. cred->domain[i]);
  1821. if (os_snprintf_error(end - pos, ret))
  1822. return pos - buf;
  1823. pos += ret;
  1824. no_domain:
  1825. if (wpa_s->current_bss == NULL ||
  1826. wpa_s->current_bss->anqp == NULL)
  1827. res = -1;
  1828. else
  1829. res = interworking_home_sp_cred(
  1830. wpa_s, cred,
  1831. wpa_s->current_bss->anqp->domain_name);
  1832. if (res > 0)
  1833. type = "home";
  1834. else if (res == 0)
  1835. type = "roaming";
  1836. else
  1837. type = "unknown";
  1838. ret = os_snprintf(pos, end - pos, "sp_type=%s\n", type);
  1839. if (os_snprintf_error(end - pos, ret))
  1840. return pos - buf;
  1841. pos += ret;
  1842. break;
  1843. }
  1844. }
  1845. #endif /* CONFIG_HS20 */
  1846. if (wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt) ||
  1847. wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
  1848. res = eapol_sm_get_status(wpa_s->eapol, pos, end - pos,
  1849. verbose);
  1850. if (res >= 0)
  1851. pos += res;
  1852. }
  1853. #ifdef CONFIG_MACSEC
  1854. res = ieee802_1x_kay_get_status(wpa_s->kay, pos, end - pos);
  1855. if (res > 0)
  1856. pos += res;
  1857. #endif /* CONFIG_MACSEC */
  1858. sess_id = eapol_sm_get_session_id(wpa_s->eapol, &sess_id_len);
  1859. if (sess_id) {
  1860. char *start = pos;
  1861. ret = os_snprintf(pos, end - pos, "eap_session_id=");
  1862. if (os_snprintf_error(end - pos, ret))
  1863. return start - buf;
  1864. pos += ret;
  1865. ret = wpa_snprintf_hex(pos, end - pos, sess_id, sess_id_len);
  1866. if (ret <= 0)
  1867. return start - buf;
  1868. pos += ret;
  1869. ret = os_snprintf(pos, end - pos, "\n");
  1870. if (os_snprintf_error(end - pos, ret))
  1871. return start - buf;
  1872. pos += ret;
  1873. }
  1874. res = rsn_preauth_get_status(wpa_s->wpa, pos, end - pos, verbose);
  1875. if (res >= 0)
  1876. pos += res;
  1877. #ifdef CONFIG_WPS
  1878. {
  1879. char uuid_str[100];
  1880. uuid_bin2str(wpa_s->wps->uuid, uuid_str, sizeof(uuid_str));
  1881. ret = os_snprintf(pos, end - pos, "uuid=%s\n", uuid_str);
  1882. if (os_snprintf_error(end - pos, ret))
  1883. return pos - buf;
  1884. pos += ret;
  1885. }
  1886. #endif /* CONFIG_WPS */
  1887. #ifdef ANDROID
  1888. /*
  1889. * Allow using the STATUS command with default behavior, say for debug,
  1890. * i.e., don't generate a "fake" CONNECTION and SUPPLICANT_STATE_CHANGE
  1891. * events with STATUS-NO_EVENTS.
  1892. */
  1893. if (os_strcmp(params, "-NO_EVENTS")) {
  1894. wpa_msg_ctrl(wpa_s, MSG_INFO, WPA_EVENT_STATE_CHANGE
  1895. "id=%d state=%d BSSID=" MACSTR " SSID=%s",
  1896. wpa_s->current_ssid ? wpa_s->current_ssid->id : -1,
  1897. wpa_s->wpa_state,
  1898. MAC2STR(wpa_s->bssid),
  1899. wpa_s->current_ssid && wpa_s->current_ssid->ssid ?
  1900. wpa_ssid_txt(wpa_s->current_ssid->ssid,
  1901. wpa_s->current_ssid->ssid_len) : "");
  1902. if (wpa_s->wpa_state == WPA_COMPLETED) {
  1903. struct wpa_ssid *ssid = wpa_s->current_ssid;
  1904. wpa_msg_ctrl(wpa_s, MSG_INFO, WPA_EVENT_CONNECTED
  1905. "- connection to " MACSTR
  1906. " completed %s [id=%d id_str=%s]",
  1907. MAC2STR(wpa_s->bssid), "(auth)",
  1908. ssid ? ssid->id : -1,
  1909. ssid && ssid->id_str ? ssid->id_str : "");
  1910. }
  1911. }
  1912. #endif /* ANDROID */
  1913. return pos - buf;
  1914. }
  1915. static int wpa_supplicant_ctrl_iface_bssid(struct wpa_supplicant *wpa_s,
  1916. char *cmd)
  1917. {
  1918. char *pos;
  1919. int id;
  1920. struct wpa_ssid *ssid;
  1921. u8 bssid[ETH_ALEN];
  1922. /* cmd: "<network id> <BSSID>" */
  1923. pos = os_strchr(cmd, ' ');
  1924. if (pos == NULL)
  1925. return -1;
  1926. *pos++ = '\0';
  1927. id = atoi(cmd);
  1928. wpa_printf(MSG_DEBUG, "CTRL_IFACE: id=%d bssid='%s'", id, pos);
  1929. if (hwaddr_aton(pos, bssid)) {
  1930. wpa_printf(MSG_DEBUG ,"CTRL_IFACE: invalid BSSID '%s'", pos);
  1931. return -1;
  1932. }
  1933. ssid = wpa_config_get_network(wpa_s->conf, id);
  1934. if (ssid == NULL) {
  1935. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find SSID id=%d "
  1936. "to update", id);
  1937. return -1;
  1938. }
  1939. os_memcpy(ssid->bssid, bssid, ETH_ALEN);
  1940. ssid->bssid_set = !is_zero_ether_addr(bssid);
  1941. return 0;
  1942. }
  1943. static int wpa_supplicant_ctrl_iface_blacklist(struct wpa_supplicant *wpa_s,
  1944. char *cmd, char *buf,
  1945. size_t buflen)
  1946. {
  1947. u8 bssid[ETH_ALEN];
  1948. struct wpa_blacklist *e;
  1949. char *pos, *end;
  1950. int ret;
  1951. /* cmd: "BLACKLIST [<BSSID>]" */
  1952. if (*cmd == '\0') {
  1953. pos = buf;
  1954. end = buf + buflen;
  1955. e = wpa_s->blacklist;
  1956. while (e) {
  1957. ret = os_snprintf(pos, end - pos, MACSTR "\n",
  1958. MAC2STR(e->bssid));
  1959. if (os_snprintf_error(end - pos, ret))
  1960. return pos - buf;
  1961. pos += ret;
  1962. e = e->next;
  1963. }
  1964. return pos - buf;
  1965. }
  1966. cmd++;
  1967. if (os_strncmp(cmd, "clear", 5) == 0) {
  1968. wpa_blacklist_clear(wpa_s);
  1969. os_memcpy(buf, "OK\n", 3);
  1970. return 3;
  1971. }
  1972. wpa_printf(MSG_DEBUG, "CTRL_IFACE: BLACKLIST bssid='%s'", cmd);
  1973. if (hwaddr_aton(cmd, bssid)) {
  1974. wpa_printf(MSG_DEBUG, "CTRL_IFACE: invalid BSSID '%s'", cmd);
  1975. return -1;
  1976. }
  1977. /*
  1978. * Add the BSSID twice, so its count will be 2, causing it to be
  1979. * skipped when processing scan results.
  1980. */
  1981. ret = wpa_blacklist_add(wpa_s, bssid);
  1982. if (ret < 0)
  1983. return -1;
  1984. ret = wpa_blacklist_add(wpa_s, bssid);
  1985. if (ret < 0)
  1986. return -1;
  1987. os_memcpy(buf, "OK\n", 3);
  1988. return 3;
  1989. }
  1990. static int wpa_supplicant_ctrl_iface_log_level(struct wpa_supplicant *wpa_s,
  1991. char *cmd, char *buf,
  1992. size_t buflen)
  1993. {
  1994. char *pos, *end, *stamp;
  1995. int ret;
  1996. /* cmd: "LOG_LEVEL [<level>]" */
  1997. if (*cmd == '\0') {
  1998. pos = buf;
  1999. end = buf + buflen;
  2000. ret = os_snprintf(pos, end - pos, "Current level: %s\n"
  2001. "Timestamp: %d\n",
  2002. debug_level_str(wpa_debug_level),
  2003. wpa_debug_timestamp);
  2004. if (os_snprintf_error(end - pos, ret))
  2005. ret = 0;
  2006. return ret;
  2007. }
  2008. while (*cmd == ' ')
  2009. cmd++;
  2010. stamp = os_strchr(cmd, ' ');
  2011. if (stamp) {
  2012. *stamp++ = '\0';
  2013. while (*stamp == ' ') {
  2014. stamp++;
  2015. }
  2016. }
  2017. if (os_strlen(cmd)) {
  2018. int level = str_to_debug_level(cmd);
  2019. if (level < 0)
  2020. return -1;
  2021. wpa_debug_level = level;
  2022. }
  2023. if (stamp && os_strlen(stamp))
  2024. wpa_debug_timestamp = atoi(stamp);
  2025. os_memcpy(buf, "OK\n", 3);
  2026. return 3;
  2027. }
  2028. static int wpa_supplicant_ctrl_iface_list_networks(
  2029. struct wpa_supplicant *wpa_s, char *cmd, char *buf, size_t buflen)
  2030. {
  2031. char *pos, *end, *prev;
  2032. struct wpa_ssid *ssid;
  2033. int ret;
  2034. pos = buf;
  2035. end = buf + buflen;
  2036. ret = os_snprintf(pos, end - pos,
  2037. "network id / ssid / bssid / flags\n");
  2038. if (os_snprintf_error(end - pos, ret))
  2039. return pos - buf;
  2040. pos += ret;
  2041. ssid = wpa_s->conf->ssid;
  2042. /* skip over ssids until we find next one */
  2043. if (cmd != NULL && os_strncmp(cmd, "LAST_ID=", 8) == 0) {
  2044. int last_id = atoi(cmd + 8);
  2045. if (last_id != -1) {
  2046. while (ssid != NULL && ssid->id <= last_id) {
  2047. ssid = ssid->next;
  2048. }
  2049. }
  2050. }
  2051. while (ssid) {
  2052. prev = pos;
  2053. ret = os_snprintf(pos, end - pos, "%d\t%s",
  2054. ssid->id,
  2055. wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
  2056. if (os_snprintf_error(end - pos, ret))
  2057. return prev - buf;
  2058. pos += ret;
  2059. if (ssid->bssid_set) {
  2060. ret = os_snprintf(pos, end - pos, "\t" MACSTR,
  2061. MAC2STR(ssid->bssid));
  2062. } else {
  2063. ret = os_snprintf(pos, end - pos, "\tany");
  2064. }
  2065. if (os_snprintf_error(end - pos, ret))
  2066. return prev - buf;
  2067. pos += ret;
  2068. ret = os_snprintf(pos, end - pos, "\t%s%s%s%s",
  2069. ssid == wpa_s->current_ssid ?
  2070. "[CURRENT]" : "",
  2071. ssid->disabled ? "[DISABLED]" : "",
  2072. ssid->disabled_until.sec ?
  2073. "[TEMP-DISABLED]" : "",
  2074. ssid->disabled == 2 ? "[P2P-PERSISTENT]" :
  2075. "");
  2076. if (os_snprintf_error(end - pos, ret))
  2077. return prev - buf;
  2078. pos += ret;
  2079. ret = os_snprintf(pos, end - pos, "\n");
  2080. if (os_snprintf_error(end - pos, ret))
  2081. return prev - buf;
  2082. pos += ret;
  2083. ssid = ssid->next;
  2084. }
  2085. return pos - buf;
  2086. }
  2087. static char * wpa_supplicant_cipher_txt(char *pos, char *end, int cipher)
  2088. {
  2089. int ret;
  2090. ret = os_snprintf(pos, end - pos, "-");
  2091. if (os_snprintf_error(end - pos, ret))
  2092. return pos;
  2093. pos += ret;
  2094. ret = wpa_write_ciphers(pos, end, cipher, "+");
  2095. if (ret < 0)
  2096. return pos;
  2097. pos += ret;
  2098. return pos;
  2099. }
  2100. static char * wpa_supplicant_ie_txt(char *pos, char *end, const char *proto,
  2101. const u8 *ie, size_t ie_len)
  2102. {
  2103. struct wpa_ie_data data;
  2104. char *start;
  2105. int ret;
  2106. ret = os_snprintf(pos, end - pos, "[%s-", proto);
  2107. if (os_snprintf_error(end - pos, ret))
  2108. return pos;
  2109. pos += ret;
  2110. if (wpa_parse_wpa_ie(ie, ie_len, &data) < 0) {
  2111. ret = os_snprintf(pos, end - pos, "?]");
  2112. if (os_snprintf_error(end - pos, ret))
  2113. return pos;
  2114. pos += ret;
  2115. return pos;
  2116. }
  2117. start = pos;
  2118. if (data.key_mgmt & WPA_KEY_MGMT_IEEE8021X) {
  2119. ret = os_snprintf(pos, end - pos, "%sEAP",
  2120. pos == start ? "" : "+");
  2121. if (os_snprintf_error(end - pos, ret))
  2122. return pos;
  2123. pos += ret;
  2124. }
  2125. if (data.key_mgmt & WPA_KEY_MGMT_PSK) {
  2126. ret = os_snprintf(pos, end - pos, "%sPSK",
  2127. pos == start ? "" : "+");
  2128. if (os_snprintf_error(end - pos, ret))
  2129. return pos;
  2130. pos += ret;
  2131. }
  2132. if (data.key_mgmt & WPA_KEY_MGMT_WPA_NONE) {
  2133. ret = os_snprintf(pos, end - pos, "%sNone",
  2134. pos == start ? "" : "+");
  2135. if (os_snprintf_error(end - pos, ret))
  2136. return pos;
  2137. pos += ret;
  2138. }
  2139. if (data.key_mgmt & WPA_KEY_MGMT_SAE) {
  2140. ret = os_snprintf(pos, end - pos, "%sSAE",
  2141. pos == start ? "" : "+");
  2142. if (os_snprintf_error(end - pos, ret))
  2143. return pos;
  2144. pos += ret;
  2145. }
  2146. #ifdef CONFIG_IEEE80211R
  2147. if (data.key_mgmt & WPA_KEY_MGMT_FT_IEEE8021X) {
  2148. ret = os_snprintf(pos, end - pos, "%sFT/EAP",
  2149. pos == start ? "" : "+");
  2150. if (os_snprintf_error(end - pos, ret))
  2151. return pos;
  2152. pos += ret;
  2153. }
  2154. if (data.key_mgmt & WPA_KEY_MGMT_FT_PSK) {
  2155. ret = os_snprintf(pos, end - pos, "%sFT/PSK",
  2156. pos == start ? "" : "+");
  2157. if (os_snprintf_error(end - pos, ret))
  2158. return pos;
  2159. pos += ret;
  2160. }
  2161. if (data.key_mgmt & WPA_KEY_MGMT_FT_SAE) {
  2162. ret = os_snprintf(pos, end - pos, "%sFT/SAE",
  2163. pos == start ? "" : "+");
  2164. if (os_snprintf_error(end - pos, ret))
  2165. return pos;
  2166. pos += ret;
  2167. }
  2168. #endif /* CONFIG_IEEE80211R */
  2169. #ifdef CONFIG_IEEE80211W
  2170. if (data.key_mgmt & WPA_KEY_MGMT_IEEE8021X_SHA256) {
  2171. ret = os_snprintf(pos, end - pos, "%sEAP-SHA256",
  2172. pos == start ? "" : "+");
  2173. if (os_snprintf_error(end - pos, ret))
  2174. return pos;
  2175. pos += ret;
  2176. }
  2177. if (data.key_mgmt & WPA_KEY_MGMT_PSK_SHA256) {
  2178. ret = os_snprintf(pos, end - pos, "%sPSK-SHA256",
  2179. pos == start ? "" : "+");
  2180. if (os_snprintf_error(end - pos, ret))
  2181. return pos;
  2182. pos += ret;
  2183. }
  2184. #endif /* CONFIG_IEEE80211W */
  2185. #ifdef CONFIG_SUITEB
  2186. if (data.key_mgmt & WPA_KEY_MGMT_IEEE8021X_SUITE_B) {
  2187. ret = os_snprintf(pos, end - pos, "%sEAP-SUITE-B",
  2188. pos == start ? "" : "+");
  2189. if (os_snprintf_error(end - pos, ret))
  2190. return pos;
  2191. pos += ret;
  2192. }
  2193. #endif /* CONFIG_SUITEB */
  2194. #ifdef CONFIG_SUITEB192
  2195. if (data.key_mgmt & WPA_KEY_MGMT_IEEE8021X_SUITE_B_192) {
  2196. ret = os_snprintf(pos, end - pos, "%sEAP-SUITE-B-192",
  2197. pos == start ? "" : "+");
  2198. if (os_snprintf_error(end - pos, ret))
  2199. return pos;
  2200. pos += ret;
  2201. }
  2202. #endif /* CONFIG_SUITEB192 */
  2203. #ifdef CONFIG_FILS
  2204. if (data.key_mgmt & WPA_KEY_MGMT_FILS_SHA256) {
  2205. ret = os_snprintf(pos, end - pos, "%sFILS-SHA256",
  2206. pos == start ? "" : "+");
  2207. if (os_snprintf_error(end - pos, ret))
  2208. return pos;
  2209. pos += ret;
  2210. }
  2211. if (data.key_mgmt & WPA_KEY_MGMT_FILS_SHA384) {
  2212. ret = os_snprintf(pos, end - pos, "%sFILS-SHA384",
  2213. pos == start ? "" : "+");
  2214. if (os_snprintf_error(end - pos, ret))
  2215. return pos;
  2216. pos += ret;
  2217. }
  2218. #ifdef CONFIG_IEEE80211R
  2219. if (data.key_mgmt & WPA_KEY_MGMT_FT_FILS_SHA256) {
  2220. ret = os_snprintf(pos, end - pos, "%sFT-FILS-SHA256",
  2221. pos == start ? "" : "+");
  2222. if (os_snprintf_error(end - pos, ret))
  2223. return pos;
  2224. pos += ret;
  2225. }
  2226. if (data.key_mgmt & WPA_KEY_MGMT_FT_FILS_SHA384) {
  2227. ret = os_snprintf(pos, end - pos, "%sFT-FILS-SHA384",
  2228. pos == start ? "" : "+");
  2229. if (os_snprintf_error(end - pos, ret))
  2230. return pos;
  2231. pos += ret;
  2232. }
  2233. #endif /* CONFIG_IEEE80211R */
  2234. #endif /* CONFIG_FILS */
  2235. #ifdef CONFIG_OWE
  2236. if (data.key_mgmt & WPA_KEY_MGMT_OWE) {
  2237. ret = os_snprintf(pos, end - pos, "%sOWE",
  2238. pos == start ? "" : "+");
  2239. if (os_snprintf_error(end - pos, ret))
  2240. return pos;
  2241. pos += ret;
  2242. }
  2243. #endif /* CONFIG_OWE */
  2244. if (data.key_mgmt & WPA_KEY_MGMT_OSEN) {
  2245. ret = os_snprintf(pos, end - pos, "%sOSEN",
  2246. pos == start ? "" : "+");
  2247. if (os_snprintf_error(end - pos, ret))
  2248. return pos;
  2249. pos += ret;
  2250. }
  2251. pos = wpa_supplicant_cipher_txt(pos, end, data.pairwise_cipher);
  2252. if (data.capabilities & WPA_CAPABILITY_PREAUTH) {
  2253. ret = os_snprintf(pos, end - pos, "-preauth");
  2254. if (os_snprintf_error(end - pos, ret))
  2255. return pos;
  2256. pos += ret;
  2257. }
  2258. ret = os_snprintf(pos, end - pos, "]");
  2259. if (os_snprintf_error(end - pos, ret))
  2260. return pos;
  2261. pos += ret;
  2262. return pos;
  2263. }
  2264. #ifdef CONFIG_WPS
  2265. static char * wpa_supplicant_wps_ie_txt_buf(struct wpa_supplicant *wpa_s,
  2266. char *pos, char *end,
  2267. struct wpabuf *wps_ie)
  2268. {
  2269. int ret;
  2270. const char *txt;
  2271. if (wps_ie == NULL)
  2272. return pos;
  2273. if (wps_is_selected_pbc_registrar(wps_ie))
  2274. txt = "[WPS-PBC]";
  2275. else if (wps_is_addr_authorized(wps_ie, wpa_s->own_addr, 0))
  2276. txt = "[WPS-AUTH]";
  2277. else if (wps_is_selected_pin_registrar(wps_ie))
  2278. txt = "[WPS-PIN]";
  2279. else
  2280. txt = "[WPS]";
  2281. ret = os_snprintf(pos, end - pos, "%s", txt);
  2282. if (!os_snprintf_error(end - pos, ret))
  2283. pos += ret;
  2284. wpabuf_free(wps_ie);
  2285. return pos;
  2286. }
  2287. #endif /* CONFIG_WPS */
  2288. static char * wpa_supplicant_wps_ie_txt(struct wpa_supplicant *wpa_s,
  2289. char *pos, char *end,
  2290. const struct wpa_bss *bss)
  2291. {
  2292. #ifdef CONFIG_WPS
  2293. struct wpabuf *wps_ie;
  2294. wps_ie = wpa_bss_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
  2295. return wpa_supplicant_wps_ie_txt_buf(wpa_s, pos, end, wps_ie);
  2296. #else /* CONFIG_WPS */
  2297. return pos;
  2298. #endif /* CONFIG_WPS */
  2299. }
  2300. /* Format one result on one text line into a buffer. */
  2301. static int wpa_supplicant_ctrl_iface_scan_result(
  2302. struct wpa_supplicant *wpa_s,
  2303. const struct wpa_bss *bss, char *buf, size_t buflen)
  2304. {
  2305. char *pos, *end;
  2306. int ret;
  2307. const u8 *ie, *ie2, *osen_ie, *p2p, *mesh;
  2308. mesh = wpa_bss_get_ie(bss, WLAN_EID_MESH_ID);
  2309. p2p = wpa_bss_get_vendor_ie(bss, P2P_IE_VENDOR_TYPE);
  2310. if (!p2p)
  2311. p2p = wpa_bss_get_vendor_ie_beacon(bss, P2P_IE_VENDOR_TYPE);
  2312. if (p2p && bss->ssid_len == P2P_WILDCARD_SSID_LEN &&
  2313. os_memcmp(bss->ssid, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN) ==
  2314. 0)
  2315. return 0; /* Do not show P2P listen discovery results here */
  2316. pos = buf;
  2317. end = buf + buflen;
  2318. ret = os_snprintf(pos, end - pos, MACSTR "\t%d\t%d\t",
  2319. MAC2STR(bss->bssid), bss->freq, bss->level);
  2320. if (os_snprintf_error(end - pos, ret))
  2321. return -1;
  2322. pos += ret;
  2323. ie = wpa_bss_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
  2324. if (ie)
  2325. pos = wpa_supplicant_ie_txt(pos, end, "WPA", ie, 2 + ie[1]);
  2326. ie2 = wpa_bss_get_ie(bss, WLAN_EID_RSN);
  2327. if (ie2) {
  2328. pos = wpa_supplicant_ie_txt(pos, end, mesh ? "RSN" : "WPA2",
  2329. ie2, 2 + ie2[1]);
  2330. }
  2331. osen_ie = wpa_bss_get_vendor_ie(bss, OSEN_IE_VENDOR_TYPE);
  2332. if (osen_ie)
  2333. pos = wpa_supplicant_ie_txt(pos, end, "OSEN",
  2334. osen_ie, 2 + osen_ie[1]);
  2335. pos = wpa_supplicant_wps_ie_txt(wpa_s, pos, end, bss);
  2336. if (!ie && !ie2 && !osen_ie && (bss->caps & IEEE80211_CAP_PRIVACY)) {
  2337. ret = os_snprintf(pos, end - pos, "[WEP]");
  2338. if (os_snprintf_error(end - pos, ret))
  2339. return -1;
  2340. pos += ret;
  2341. }
  2342. if (mesh) {
  2343. ret = os_snprintf(pos, end - pos, "[MESH]");
  2344. if (os_snprintf_error(end - pos, ret))
  2345. return -1;
  2346. pos += ret;
  2347. }
  2348. if (bss_is_dmg(bss)) {
  2349. const char *s;
  2350. ret = os_snprintf(pos, end - pos, "[DMG]");
  2351. if (os_snprintf_error(end - pos, ret))
  2352. return -1;
  2353. pos += ret;
  2354. switch (bss->caps & IEEE80211_CAP_DMG_MASK) {
  2355. case IEEE80211_CAP_DMG_IBSS:
  2356. s = "[IBSS]";
  2357. break;
  2358. case IEEE80211_CAP_DMG_AP:
  2359. s = "[ESS]";
  2360. break;
  2361. case IEEE80211_CAP_DMG_PBSS:
  2362. s = "[PBSS]";
  2363. break;
  2364. default:
  2365. s = "";
  2366. break;
  2367. }
  2368. ret = os_snprintf(pos, end - pos, "%s", s);
  2369. if (os_snprintf_error(end - pos, ret))
  2370. return -1;
  2371. pos += ret;
  2372. } else {
  2373. if (bss->caps & IEEE80211_CAP_IBSS) {
  2374. ret = os_snprintf(pos, end - pos, "[IBSS]");
  2375. if (os_snprintf_error(end - pos, ret))
  2376. return -1;
  2377. pos += ret;
  2378. }
  2379. if (bss->caps & IEEE80211_CAP_ESS) {
  2380. ret = os_snprintf(pos, end - pos, "[ESS]");
  2381. if (os_snprintf_error(end - pos, ret))
  2382. return -1;
  2383. pos += ret;
  2384. }
  2385. }
  2386. if (p2p) {
  2387. ret = os_snprintf(pos, end - pos, "[P2P]");
  2388. if (os_snprintf_error(end - pos, ret))
  2389. return -1;
  2390. pos += ret;
  2391. }
  2392. #ifdef CONFIG_HS20
  2393. if (wpa_bss_get_vendor_ie(bss, HS20_IE_VENDOR_TYPE) && ie2) {
  2394. ret = os_snprintf(pos, end - pos, "[HS20]");
  2395. if (os_snprintf_error(end - pos, ret))
  2396. return -1;
  2397. pos += ret;
  2398. }
  2399. #endif /* CONFIG_HS20 */
  2400. #ifdef CONFIG_FILS
  2401. if (wpa_bss_get_ie(bss, WLAN_EID_FILS_INDICATION)) {
  2402. ret = os_snprintf(pos, end - pos, "[FILS]");
  2403. if (os_snprintf_error(end - pos, ret))
  2404. return -1;
  2405. pos += ret;
  2406. }
  2407. #endif /* CONFIG_FILS */
  2408. #ifdef CONFIG_FST
  2409. if (wpa_bss_get_ie(bss, WLAN_EID_MULTI_BAND)) {
  2410. ret = os_snprintf(pos, end - pos, "[FST]");
  2411. if (os_snprintf_error(end - pos, ret))
  2412. return -1;
  2413. pos += ret;
  2414. }
  2415. #endif /* CONFIG_FST */
  2416. ret = os_snprintf(pos, end - pos, "\t%s",
  2417. wpa_ssid_txt(bss->ssid, bss->ssid_len));
  2418. if (os_snprintf_error(end - pos, ret))
  2419. return -1;
  2420. pos += ret;
  2421. ret = os_snprintf(pos, end - pos, "\n");
  2422. if (os_snprintf_error(end - pos, ret))
  2423. return -1;
  2424. pos += ret;
  2425. return pos - buf;
  2426. }
  2427. static int wpa_supplicant_ctrl_iface_scan_results(
  2428. struct wpa_supplicant *wpa_s, char *buf, size_t buflen)
  2429. {
  2430. char *pos, *end;
  2431. struct wpa_bss *bss;
  2432. int ret;
  2433. pos = buf;
  2434. end = buf + buflen;
  2435. ret = os_snprintf(pos, end - pos, "bssid / frequency / signal level / "
  2436. "flags / ssid\n");
  2437. if (os_snprintf_error(end - pos, ret))
  2438. return pos - buf;
  2439. pos += ret;
  2440. dl_list_for_each(bss, &wpa_s->bss_id, struct wpa_bss, list_id) {
  2441. ret = wpa_supplicant_ctrl_iface_scan_result(wpa_s, bss, pos,
  2442. end - pos);
  2443. if (ret < 0 || ret >= end - pos)
  2444. return pos - buf;
  2445. pos += ret;
  2446. }
  2447. return pos - buf;
  2448. }
  2449. #ifdef CONFIG_MESH
  2450. static int wpa_supplicant_ctrl_iface_mesh_interface_add(
  2451. struct wpa_supplicant *wpa_s, char *cmd, char *reply, size_t max_len)
  2452. {
  2453. char *pos, ifname[IFNAMSIZ + 1];
  2454. ifname[0] = '\0';
  2455. pos = os_strstr(cmd, "ifname=");
  2456. if (pos) {
  2457. pos += 7;
  2458. os_strlcpy(ifname, pos, sizeof(ifname));
  2459. }
  2460. if (wpas_mesh_add_interface(wpa_s, ifname, sizeof(ifname)) < 0)
  2461. return -1;
  2462. os_strlcpy(reply, ifname, max_len);
  2463. return os_strlen(ifname);
  2464. }
  2465. static int wpa_supplicant_ctrl_iface_mesh_group_add(
  2466. struct wpa_supplicant *wpa_s, char *cmd)
  2467. {
  2468. int id;
  2469. struct wpa_ssid *ssid;
  2470. id = atoi(cmd);
  2471. wpa_printf(MSG_DEBUG, "CTRL_IFACE: MESH_GROUP_ADD id=%d", id);
  2472. ssid = wpa_config_get_network(wpa_s->conf, id);
  2473. if (ssid == NULL) {
  2474. wpa_printf(MSG_DEBUG,
  2475. "CTRL_IFACE: Could not find network id=%d", id);
  2476. return -1;
  2477. }
  2478. if (ssid->mode != WPAS_MODE_MESH) {
  2479. wpa_printf(MSG_DEBUG,
  2480. "CTRL_IFACE: Cannot use MESH_GROUP_ADD on a non mesh network");
  2481. return -1;
  2482. }
  2483. if (ssid->key_mgmt != WPA_KEY_MGMT_NONE &&
  2484. ssid->key_mgmt != WPA_KEY_MGMT_SAE) {
  2485. wpa_printf(MSG_ERROR,
  2486. "CTRL_IFACE: key_mgmt for mesh network should be open or SAE");
  2487. return -1;
  2488. }
  2489. /*
  2490. * TODO: If necessary write our own group_add function,
  2491. * for now we can reuse select_network
  2492. */
  2493. wpa_supplicant_select_network(wpa_s, ssid);
  2494. return 0;
  2495. }
  2496. static int wpa_supplicant_ctrl_iface_mesh_group_remove(
  2497. struct wpa_supplicant *wpa_s, char *cmd)
  2498. {
  2499. struct wpa_supplicant *orig;
  2500. struct wpa_global *global;
  2501. int found = 0;
  2502. wpa_printf(MSG_DEBUG, "CTRL_IFACE: MESH_GROUP_REMOVE ifname=%s", cmd);
  2503. global = wpa_s->global;
  2504. orig = wpa_s;
  2505. for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
  2506. if (os_strcmp(wpa_s->ifname, cmd) == 0) {
  2507. found = 1;
  2508. break;
  2509. }
  2510. }
  2511. if (!found) {
  2512. wpa_printf(MSG_ERROR,
  2513. "CTRL_IFACE: MESH_GROUP_REMOVE ifname=%s not found",
  2514. cmd);
  2515. return -1;
  2516. }
  2517. if (wpa_s->mesh_if_created && wpa_s == orig) {
  2518. wpa_printf(MSG_ERROR,
  2519. "CTRL_IFACE: MESH_GROUP_REMOVE can't remove itself");
  2520. return -1;
  2521. }
  2522. wpa_s->reassociate = 0;
  2523. wpa_s->disconnected = 1;
  2524. wpa_supplicant_cancel_sched_scan(wpa_s);
  2525. wpa_supplicant_cancel_scan(wpa_s);
  2526. /*
  2527. * TODO: If necessary write our own group_remove function,
  2528. * for now we can reuse deauthenticate
  2529. */
  2530. wpa_supplicant_deauthenticate(wpa_s, WLAN_REASON_DEAUTH_LEAVING);
  2531. if (wpa_s->mesh_if_created)
  2532. wpa_supplicant_remove_iface(global, wpa_s, 0);
  2533. return 0;
  2534. }
  2535. static int wpa_supplicant_ctrl_iface_mesh_peer_remove(
  2536. struct wpa_supplicant *wpa_s, char *cmd)
  2537. {
  2538. u8 addr[ETH_ALEN];
  2539. if (hwaddr_aton(cmd, addr) < 0)
  2540. return -1;
  2541. return wpas_mesh_peer_remove(wpa_s, addr);
  2542. }
  2543. static int wpa_supplicant_ctrl_iface_mesh_peer_add(
  2544. struct wpa_supplicant *wpa_s, char *cmd)
  2545. {
  2546. u8 addr[ETH_ALEN];
  2547. int duration;
  2548. char *pos;
  2549. pos = os_strstr(cmd, " duration=");
  2550. if (pos) {
  2551. *pos = '\0';
  2552. duration = atoi(pos + 10);
  2553. } else {
  2554. duration = -1;
  2555. }
  2556. if (hwaddr_aton(cmd, addr))
  2557. return -1;
  2558. return wpas_mesh_peer_add(wpa_s, addr, duration);
  2559. }
  2560. #endif /* CONFIG_MESH */
  2561. static int wpa_supplicant_ctrl_iface_select_network(
  2562. struct wpa_supplicant *wpa_s, char *cmd)
  2563. {
  2564. int id;
  2565. struct wpa_ssid *ssid;
  2566. char *pos;
  2567. /* cmd: "<network id>" or "any" */
  2568. if (os_strncmp(cmd, "any", 3) == 0) {
  2569. wpa_printf(MSG_DEBUG, "CTRL_IFACE: SELECT_NETWORK any");
  2570. ssid = NULL;
  2571. } else {
  2572. id = atoi(cmd);
  2573. wpa_printf(MSG_DEBUG, "CTRL_IFACE: SELECT_NETWORK id=%d", id);
  2574. ssid = wpa_config_get_network(wpa_s->conf, id);
  2575. if (ssid == NULL) {
  2576. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find "
  2577. "network id=%d", id);
  2578. return -1;
  2579. }
  2580. if (ssid->disabled == 2) {
  2581. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Cannot use "
  2582. "SELECT_NETWORK with persistent P2P group");
  2583. return -1;
  2584. }
  2585. }
  2586. pos = os_strstr(cmd, " freq=");
  2587. if (pos) {
  2588. int *freqs = freq_range_to_channel_list(wpa_s, pos + 6);
  2589. if (freqs) {
  2590. os_free(wpa_s->select_network_scan_freqs);
  2591. wpa_s->select_network_scan_freqs = freqs;
  2592. }
  2593. }
  2594. wpa_s->scan_min_time.sec = 0;
  2595. wpa_s->scan_min_time.usec = 0;
  2596. wpa_supplicant_select_network(wpa_s, ssid);
  2597. return 0;
  2598. }
  2599. static int wpa_supplicant_ctrl_iface_enable_network(
  2600. struct wpa_supplicant *wpa_s, char *cmd)
  2601. {
  2602. int id;
  2603. struct wpa_ssid *ssid;
  2604. /* cmd: "<network id>" or "all" */
  2605. if (os_strcmp(cmd, "all") == 0) {
  2606. wpa_printf(MSG_DEBUG, "CTRL_IFACE: ENABLE_NETWORK all");
  2607. ssid = NULL;
  2608. } else {
  2609. id = atoi(cmd);
  2610. wpa_printf(MSG_DEBUG, "CTRL_IFACE: ENABLE_NETWORK id=%d", id);
  2611. ssid = wpa_config_get_network(wpa_s->conf, id);
  2612. if (ssid == NULL) {
  2613. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find "
  2614. "network id=%d", id);
  2615. return -1;
  2616. }
  2617. if (ssid->disabled == 2) {
  2618. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Cannot use "
  2619. "ENABLE_NETWORK with persistent P2P group");
  2620. return -1;
  2621. }
  2622. if (os_strstr(cmd, " no-connect")) {
  2623. ssid->disabled = 0;
  2624. return 0;
  2625. }
  2626. }
  2627. wpa_s->scan_min_time.sec = 0;
  2628. wpa_s->scan_min_time.usec = 0;
  2629. wpa_supplicant_enable_network(wpa_s, ssid);
  2630. return 0;
  2631. }
  2632. static int wpa_supplicant_ctrl_iface_disable_network(
  2633. struct wpa_supplicant *wpa_s, char *cmd)
  2634. {
  2635. int id;
  2636. struct wpa_ssid *ssid;
  2637. /* cmd: "<network id>" or "all" */
  2638. if (os_strcmp(cmd, "all") == 0) {
  2639. wpa_printf(MSG_DEBUG, "CTRL_IFACE: DISABLE_NETWORK all");
  2640. ssid = NULL;
  2641. } else {
  2642. id = atoi(cmd);
  2643. wpa_printf(MSG_DEBUG, "CTRL_IFACE: DISABLE_NETWORK id=%d", id);
  2644. ssid = wpa_config_get_network(wpa_s->conf, id);
  2645. if (ssid == NULL) {
  2646. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find "
  2647. "network id=%d", id);
  2648. return -1;
  2649. }
  2650. if (ssid->disabled == 2) {
  2651. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Cannot use "
  2652. "DISABLE_NETWORK with persistent P2P "
  2653. "group");
  2654. return -1;
  2655. }
  2656. }
  2657. wpa_supplicant_disable_network(wpa_s, ssid);
  2658. return 0;
  2659. }
  2660. static int wpa_supplicant_ctrl_iface_add_network(
  2661. struct wpa_supplicant *wpa_s, char *buf, size_t buflen)
  2662. {
  2663. struct wpa_ssid *ssid;
  2664. int ret;
  2665. wpa_printf(MSG_DEBUG, "CTRL_IFACE: ADD_NETWORK");
  2666. ssid = wpa_supplicant_add_network(wpa_s);
  2667. if (ssid == NULL)
  2668. return -1;
  2669. ret = os_snprintf(buf, buflen, "%d\n", ssid->id);
  2670. if (os_snprintf_error(buflen, ret))
  2671. return -1;
  2672. return ret;
  2673. }
  2674. static int wpa_supplicant_ctrl_iface_remove_network(
  2675. struct wpa_supplicant *wpa_s, char *cmd)
  2676. {
  2677. int id;
  2678. struct wpa_ssid *ssid;
  2679. int result;
  2680. /* cmd: "<network id>" or "all" */
  2681. if (os_strcmp(cmd, "all") == 0) {
  2682. wpa_printf(MSG_DEBUG, "CTRL_IFACE: REMOVE_NETWORK all");
  2683. if (wpa_s->sched_scanning)
  2684. wpa_supplicant_cancel_sched_scan(wpa_s);
  2685. eapol_sm_invalidate_cached_session(wpa_s->eapol);
  2686. if (wpa_s->current_ssid) {
  2687. #ifdef CONFIG_SME
  2688. wpa_s->sme.prev_bssid_set = 0;
  2689. #endif /* CONFIG_SME */
  2690. wpa_sm_set_config(wpa_s->wpa, NULL);
  2691. eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
  2692. if (wpa_s->wpa_state >= WPA_AUTHENTICATING)
  2693. wpa_s->own_disconnect_req = 1;
  2694. wpa_supplicant_deauthenticate(
  2695. wpa_s, WLAN_REASON_DEAUTH_LEAVING);
  2696. }
  2697. ssid = wpa_s->conf->ssid;
  2698. while (ssid) {
  2699. struct wpa_ssid *remove_ssid = ssid;
  2700. id = ssid->id;
  2701. ssid = ssid->next;
  2702. if (wpa_s->last_ssid == remove_ssid)
  2703. wpa_s->last_ssid = NULL;
  2704. wpas_notify_network_removed(wpa_s, remove_ssid);
  2705. wpa_config_remove_network(wpa_s->conf, id);
  2706. }
  2707. return 0;
  2708. }
  2709. id = atoi(cmd);
  2710. wpa_printf(MSG_DEBUG, "CTRL_IFACE: REMOVE_NETWORK id=%d", id);
  2711. result = wpa_supplicant_remove_network(wpa_s, id);
  2712. if (result == -1) {
  2713. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find network "
  2714. "id=%d", id);
  2715. return -1;
  2716. }
  2717. if (result == -2) {
  2718. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Not able to remove the "
  2719. "network id=%d", id);
  2720. return -1;
  2721. }
  2722. return 0;
  2723. }
  2724. static int wpa_supplicant_ctrl_iface_update_network(
  2725. struct wpa_supplicant *wpa_s, struct wpa_ssid *ssid,
  2726. char *name, char *value)
  2727. {
  2728. int ret;
  2729. ret = wpa_config_set(ssid, name, value, 0);
  2730. if (ret < 0) {
  2731. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Failed to set network "
  2732. "variable '%s'", name);
  2733. return -1;
  2734. }
  2735. if (ret == 1)
  2736. return 0; /* No change to the previously configured value */
  2737. if (os_strcmp(name, "bssid") != 0 &&
  2738. os_strcmp(name, "bssid_hint") != 0 &&
  2739. os_strcmp(name, "priority") != 0) {
  2740. wpa_sm_pmksa_cache_flush(wpa_s->wpa, ssid);
  2741. if (wpa_s->current_ssid == ssid ||
  2742. wpa_s->current_ssid == NULL) {
  2743. /*
  2744. * Invalidate the EAP session cache if anything in the
  2745. * current or previously used configuration changes.
  2746. */
  2747. eapol_sm_invalidate_cached_session(wpa_s->eapol);
  2748. }
  2749. }
  2750. if ((os_strcmp(name, "psk") == 0 &&
  2751. value[0] == '"' && ssid->ssid_len) ||
  2752. (os_strcmp(name, "ssid") == 0 && ssid->passphrase))
  2753. wpa_config_update_psk(ssid);
  2754. else if (os_strcmp(name, "priority") == 0)
  2755. wpa_config_update_prio_list(wpa_s->conf);
  2756. return 0;
  2757. }
  2758. static int wpa_supplicant_ctrl_iface_set_network(
  2759. struct wpa_supplicant *wpa_s, char *cmd)
  2760. {
  2761. int id, ret, prev_bssid_set, prev_disabled;
  2762. struct wpa_ssid *ssid;
  2763. char *name, *value;
  2764. u8 prev_bssid[ETH_ALEN];
  2765. /* cmd: "<network id> <variable name> <value>" */
  2766. name = os_strchr(cmd, ' ');
  2767. if (name == NULL)
  2768. return -1;
  2769. *name++ = '\0';
  2770. value = os_strchr(name, ' ');
  2771. if (value == NULL)
  2772. return -1;
  2773. *value++ = '\0';
  2774. id = atoi(cmd);
  2775. wpa_printf(MSG_DEBUG, "CTRL_IFACE: SET_NETWORK id=%d name='%s'",
  2776. id, name);
  2777. wpa_hexdump_ascii_key(MSG_DEBUG, "CTRL_IFACE: value",
  2778. (u8 *) value, os_strlen(value));
  2779. ssid = wpa_config_get_network(wpa_s->conf, id);
  2780. if (ssid == NULL) {
  2781. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find network "
  2782. "id=%d", id);
  2783. return -1;
  2784. }
  2785. prev_bssid_set = ssid->bssid_set;
  2786. prev_disabled = ssid->disabled;
  2787. os_memcpy(prev_bssid, ssid->bssid, ETH_ALEN);
  2788. ret = wpa_supplicant_ctrl_iface_update_network(wpa_s, ssid, name,
  2789. value);
  2790. if (ret == 0 &&
  2791. (ssid->bssid_set != prev_bssid_set ||
  2792. os_memcmp(ssid->bssid, prev_bssid, ETH_ALEN) != 0))
  2793. wpas_notify_network_bssid_set_changed(wpa_s, ssid);
  2794. if (prev_disabled != ssid->disabled &&
  2795. (prev_disabled == 2 || ssid->disabled == 2))
  2796. wpas_notify_network_type_changed(wpa_s, ssid);
  2797. return ret;
  2798. }
  2799. static int wpa_supplicant_ctrl_iface_get_network(
  2800. struct wpa_supplicant *wpa_s, char *cmd, char *buf, size_t buflen)
  2801. {
  2802. int id;
  2803. size_t res;
  2804. struct wpa_ssid *ssid;
  2805. char *name, *value;
  2806. /* cmd: "<network id> <variable name>" */
  2807. name = os_strchr(cmd, ' ');
  2808. if (name == NULL || buflen == 0)
  2809. return -1;
  2810. *name++ = '\0';
  2811. id = atoi(cmd);
  2812. wpa_printf(MSG_EXCESSIVE, "CTRL_IFACE: GET_NETWORK id=%d name='%s'",
  2813. id, name);
  2814. ssid = wpa_config_get_network(wpa_s->conf, id);
  2815. if (ssid == NULL) {
  2816. wpa_printf(MSG_EXCESSIVE, "CTRL_IFACE: Could not find network "
  2817. "id=%d", id);
  2818. return -1;
  2819. }
  2820. value = wpa_config_get_no_key(ssid, name);
  2821. if (value == NULL) {
  2822. wpa_printf(MSG_EXCESSIVE, "CTRL_IFACE: Failed to get network "
  2823. "variable '%s'", name);
  2824. return -1;
  2825. }
  2826. res = os_strlcpy(buf, value, buflen);
  2827. if (res >= buflen) {
  2828. os_free(value);
  2829. return -1;
  2830. }
  2831. os_free(value);
  2832. return res;
  2833. }
  2834. static int wpa_supplicant_ctrl_iface_dup_network(
  2835. struct wpa_supplicant *wpa_s, char *cmd,
  2836. struct wpa_supplicant *dst_wpa_s)
  2837. {
  2838. struct wpa_ssid *ssid_s, *ssid_d;
  2839. char *name, *id, *value;
  2840. int id_s, id_d, ret;
  2841. /* cmd: "<src network id> <dst network id> <variable name>" */
  2842. id = os_strchr(cmd, ' ');
  2843. if (id == NULL)
  2844. return -1;
  2845. *id++ = '\0';
  2846. name = os_strchr(id, ' ');
  2847. if (name == NULL)
  2848. return -1;
  2849. *name++ = '\0';
  2850. id_s = atoi(cmd);
  2851. id_d = atoi(id);
  2852. wpa_printf(MSG_DEBUG,
  2853. "CTRL_IFACE: DUP_NETWORK ifname=%s->%s id=%d->%d name='%s'",
  2854. wpa_s->ifname, dst_wpa_s->ifname, id_s, id_d, name);
  2855. ssid_s = wpa_config_get_network(wpa_s->conf, id_s);
  2856. if (ssid_s == NULL) {
  2857. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find "
  2858. "network id=%d", id_s);
  2859. return -1;
  2860. }
  2861. ssid_d = wpa_config_get_network(dst_wpa_s->conf, id_d);
  2862. if (ssid_d == NULL) {
  2863. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find "
  2864. "network id=%d", id_d);
  2865. return -1;
  2866. }
  2867. value = wpa_config_get(ssid_s, name);
  2868. if (value == NULL) {
  2869. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Failed to get network "
  2870. "variable '%s'", name);
  2871. return -1;
  2872. }
  2873. ret = wpa_supplicant_ctrl_iface_update_network(dst_wpa_s, ssid_d, name,
  2874. value);
  2875. os_free(value);
  2876. return ret;
  2877. }
  2878. static int wpa_supplicant_ctrl_iface_list_creds(struct wpa_supplicant *wpa_s,
  2879. char *buf, size_t buflen)
  2880. {
  2881. char *pos, *end;
  2882. struct wpa_cred *cred;
  2883. int ret;
  2884. pos = buf;
  2885. end = buf + buflen;
  2886. ret = os_snprintf(pos, end - pos,
  2887. "cred id / realm / username / domain / imsi\n");
  2888. if (os_snprintf_error(end - pos, ret))
  2889. return pos - buf;
  2890. pos += ret;
  2891. cred = wpa_s->conf->cred;
  2892. while (cred) {
  2893. ret = os_snprintf(pos, end - pos, "%d\t%s\t%s\t%s\t%s\n",
  2894. cred->id, cred->realm ? cred->realm : "",
  2895. cred->username ? cred->username : "",
  2896. cred->domain ? cred->domain[0] : "",
  2897. cred->imsi ? cred->imsi : "");
  2898. if (os_snprintf_error(end - pos, ret))
  2899. return pos - buf;
  2900. pos += ret;
  2901. cred = cred->next;
  2902. }
  2903. return pos - buf;
  2904. }
  2905. static int wpa_supplicant_ctrl_iface_add_cred(struct wpa_supplicant *wpa_s,
  2906. char *buf, size_t buflen)
  2907. {
  2908. struct wpa_cred *cred;
  2909. int ret;
  2910. wpa_printf(MSG_DEBUG, "CTRL_IFACE: ADD_CRED");
  2911. cred = wpa_config_add_cred(wpa_s->conf);
  2912. if (cred == NULL)
  2913. return -1;
  2914. wpa_msg(wpa_s, MSG_INFO, CRED_ADDED "%d", cred->id);
  2915. ret = os_snprintf(buf, buflen, "%d\n", cred->id);
  2916. if (os_snprintf_error(buflen, ret))
  2917. return -1;
  2918. return ret;
  2919. }
  2920. static int wpas_ctrl_remove_cred(struct wpa_supplicant *wpa_s,
  2921. struct wpa_cred *cred)
  2922. {
  2923. struct wpa_ssid *ssid;
  2924. char str[20];
  2925. int id;
  2926. if (cred == NULL) {
  2927. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find cred");
  2928. return -1;
  2929. }
  2930. id = cred->id;
  2931. if (wpa_config_remove_cred(wpa_s->conf, id) < 0) {
  2932. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find cred");
  2933. return -1;
  2934. }
  2935. wpa_msg(wpa_s, MSG_INFO, CRED_REMOVED "%d", id);
  2936. /* Remove any network entry created based on the removed credential */
  2937. ssid = wpa_s->conf->ssid;
  2938. while (ssid) {
  2939. if (ssid->parent_cred == cred) {
  2940. int res;
  2941. wpa_printf(MSG_DEBUG, "Remove network id %d since it "
  2942. "used the removed credential", ssid->id);
  2943. res = os_snprintf(str, sizeof(str), "%d", ssid->id);
  2944. if (os_snprintf_error(sizeof(str), res))
  2945. str[sizeof(str) - 1] = '\0';
  2946. ssid = ssid->next;
  2947. wpa_supplicant_ctrl_iface_remove_network(wpa_s, str);
  2948. } else
  2949. ssid = ssid->next;
  2950. }
  2951. return 0;
  2952. }
  2953. static int wpa_supplicant_ctrl_iface_remove_cred(struct wpa_supplicant *wpa_s,
  2954. char *cmd)
  2955. {
  2956. int id;
  2957. struct wpa_cred *cred, *prev;
  2958. /* cmd: "<cred id>", "all", "sp_fqdn=<FQDN>", or
  2959. * "provisioning_sp=<FQDN> */
  2960. if (os_strcmp(cmd, "all") == 0) {
  2961. wpa_printf(MSG_DEBUG, "CTRL_IFACE: REMOVE_CRED all");
  2962. cred = wpa_s->conf->cred;
  2963. while (cred) {
  2964. prev = cred;
  2965. cred = cred->next;
  2966. wpas_ctrl_remove_cred(wpa_s, prev);
  2967. }
  2968. return 0;
  2969. }
  2970. if (os_strncmp(cmd, "sp_fqdn=", 8) == 0) {
  2971. wpa_printf(MSG_DEBUG, "CTRL_IFACE: REMOVE_CRED SP FQDN '%s'",
  2972. cmd + 8);
  2973. cred = wpa_s->conf->cred;
  2974. while (cred) {
  2975. prev = cred;
  2976. cred = cred->next;
  2977. if (prev->domain) {
  2978. size_t i;
  2979. for (i = 0; i < prev->num_domain; i++) {
  2980. if (os_strcmp(prev->domain[i], cmd + 8)
  2981. != 0)
  2982. continue;
  2983. wpas_ctrl_remove_cred(wpa_s, prev);
  2984. break;
  2985. }
  2986. }
  2987. }
  2988. return 0;
  2989. }
  2990. if (os_strncmp(cmd, "provisioning_sp=", 16) == 0) {
  2991. wpa_printf(MSG_DEBUG, "CTRL_IFACE: REMOVE_CRED provisioning SP FQDN '%s'",
  2992. cmd + 16);
  2993. cred = wpa_s->conf->cred;
  2994. while (cred) {
  2995. prev = cred;
  2996. cred = cred->next;
  2997. if (prev->provisioning_sp &&
  2998. os_strcmp(prev->provisioning_sp, cmd + 16) == 0)
  2999. wpas_ctrl_remove_cred(wpa_s, prev);
  3000. }
  3001. return 0;
  3002. }
  3003. id = atoi(cmd);
  3004. wpa_printf(MSG_DEBUG, "CTRL_IFACE: REMOVE_CRED id=%d", id);
  3005. cred = wpa_config_get_cred(wpa_s->conf, id);
  3006. return wpas_ctrl_remove_cred(wpa_s, cred);
  3007. }
  3008. static int wpa_supplicant_ctrl_iface_set_cred(struct wpa_supplicant *wpa_s,
  3009. char *cmd)
  3010. {
  3011. int id;
  3012. struct wpa_cred *cred;
  3013. char *name, *value;
  3014. /* cmd: "<cred id> <variable name> <value>" */
  3015. name = os_strchr(cmd, ' ');
  3016. if (name == NULL)
  3017. return -1;
  3018. *name++ = '\0';
  3019. value = os_strchr(name, ' ');
  3020. if (value == NULL)
  3021. return -1;
  3022. *value++ = '\0';
  3023. id = atoi(cmd);
  3024. wpa_printf(MSG_DEBUG, "CTRL_IFACE: SET_CRED id=%d name='%s'",
  3025. id, name);
  3026. wpa_hexdump_ascii_key(MSG_DEBUG, "CTRL_IFACE: value",
  3027. (u8 *) value, os_strlen(value));
  3028. cred = wpa_config_get_cred(wpa_s->conf, id);
  3029. if (cred == NULL) {
  3030. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find cred id=%d",
  3031. id);
  3032. return -1;
  3033. }
  3034. if (wpa_config_set_cred(cred, name, value, 0) < 0) {
  3035. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Failed to set cred "
  3036. "variable '%s'", name);
  3037. return -1;
  3038. }
  3039. wpa_msg(wpa_s, MSG_INFO, CRED_MODIFIED "%d %s", cred->id, name);
  3040. return 0;
  3041. }
  3042. static int wpa_supplicant_ctrl_iface_get_cred(struct wpa_supplicant *wpa_s,
  3043. char *cmd, char *buf,
  3044. size_t buflen)
  3045. {
  3046. int id;
  3047. size_t res;
  3048. struct wpa_cred *cred;
  3049. char *name, *value;
  3050. /* cmd: "<cred id> <variable name>" */
  3051. name = os_strchr(cmd, ' ');
  3052. if (name == NULL)
  3053. return -1;
  3054. *name++ = '\0';
  3055. id = atoi(cmd);
  3056. wpa_printf(MSG_DEBUG, "CTRL_IFACE: GET_CRED id=%d name='%s'",
  3057. id, name);
  3058. cred = wpa_config_get_cred(wpa_s->conf, id);
  3059. if (cred == NULL) {
  3060. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find cred id=%d",
  3061. id);
  3062. return -1;
  3063. }
  3064. value = wpa_config_get_cred_no_key(cred, name);
  3065. if (value == NULL) {
  3066. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Failed to get cred variable '%s'",
  3067. name);
  3068. return -1;
  3069. }
  3070. res = os_strlcpy(buf, value, buflen);
  3071. if (res >= buflen) {
  3072. os_free(value);
  3073. return -1;
  3074. }
  3075. os_free(value);
  3076. return res;
  3077. }
  3078. #ifndef CONFIG_NO_CONFIG_WRITE
  3079. static int wpa_supplicant_ctrl_iface_save_config(struct wpa_supplicant *wpa_s)
  3080. {
  3081. int ret;
  3082. if (!wpa_s->conf->update_config) {
  3083. wpa_printf(MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Not allowed "
  3084. "to update configuration (update_config=0)");
  3085. return -1;
  3086. }
  3087. ret = wpa_config_write(wpa_s->confname, wpa_s->conf);
  3088. if (ret) {
  3089. wpa_printf(MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Failed to "
  3090. "update configuration");
  3091. } else {
  3092. wpa_printf(MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Configuration"
  3093. " updated");
  3094. }
  3095. return ret;
  3096. }
  3097. #endif /* CONFIG_NO_CONFIG_WRITE */
  3098. struct cipher_info {
  3099. unsigned int capa;
  3100. const char *name;
  3101. int group_only;
  3102. };
  3103. static const struct cipher_info ciphers[] = {
  3104. { WPA_DRIVER_CAPA_ENC_CCMP_256, "CCMP-256", 0 },
  3105. { WPA_DRIVER_CAPA_ENC_GCMP_256, "GCMP-256", 0 },
  3106. { WPA_DRIVER_CAPA_ENC_CCMP, "CCMP", 0 },
  3107. { WPA_DRIVER_CAPA_ENC_GCMP, "GCMP", 0 },
  3108. { WPA_DRIVER_CAPA_ENC_TKIP, "TKIP", 0 },
  3109. { WPA_DRIVER_CAPA_KEY_MGMT_WPA_NONE, "NONE", 0 },
  3110. { WPA_DRIVER_CAPA_ENC_WEP104, "WEP104", 1 },
  3111. { WPA_DRIVER_CAPA_ENC_WEP40, "WEP40", 1 }
  3112. };
  3113. static const struct cipher_info ciphers_group_mgmt[] = {
  3114. { WPA_DRIVER_CAPA_ENC_BIP, "AES-128-CMAC", 1 },
  3115. { WPA_DRIVER_CAPA_ENC_BIP_GMAC_128, "BIP-GMAC-128", 1 },
  3116. { WPA_DRIVER_CAPA_ENC_BIP_GMAC_256, "BIP-GMAC-256", 1 },
  3117. { WPA_DRIVER_CAPA_ENC_BIP_CMAC_256, "BIP-CMAC-256", 1 },
  3118. };
  3119. static int ctrl_iface_get_capability_pairwise(int res, char *strict,
  3120. struct wpa_driver_capa *capa,
  3121. char *buf, size_t buflen)
  3122. {
  3123. int ret;
  3124. char *pos, *end;
  3125. size_t len;
  3126. unsigned int i;
  3127. pos = buf;
  3128. end = pos + buflen;
  3129. if (res < 0) {
  3130. if (strict)
  3131. return 0;
  3132. len = os_strlcpy(buf, "CCMP TKIP NONE", buflen);
  3133. if (len >= buflen)
  3134. return -1;
  3135. return len;
  3136. }
  3137. for (i = 0; i < ARRAY_SIZE(ciphers); i++) {
  3138. if (!ciphers[i].group_only && capa->enc & ciphers[i].capa) {
  3139. ret = os_snprintf(pos, end - pos, "%s%s",
  3140. pos == buf ? "" : " ",
  3141. ciphers[i].name);
  3142. if (os_snprintf_error(end - pos, ret))
  3143. return pos - buf;
  3144. pos += ret;
  3145. }
  3146. }
  3147. return pos - buf;
  3148. }
  3149. static int ctrl_iface_get_capability_group(int res, char *strict,
  3150. struct wpa_driver_capa *capa,
  3151. char *buf, size_t buflen)
  3152. {
  3153. int ret;
  3154. char *pos, *end;
  3155. size_t len;
  3156. unsigned int i;
  3157. pos = buf;
  3158. end = pos + buflen;
  3159. if (res < 0) {
  3160. if (strict)
  3161. return 0;
  3162. len = os_strlcpy(buf, "CCMP TKIP WEP104 WEP40", buflen);
  3163. if (len >= buflen)
  3164. return -1;
  3165. return len;
  3166. }
  3167. for (i = 0; i < ARRAY_SIZE(ciphers); i++) {
  3168. if (capa->enc & ciphers[i].capa) {
  3169. ret = os_snprintf(pos, end - pos, "%s%s",
  3170. pos == buf ? "" : " ",
  3171. ciphers[i].name);
  3172. if (os_snprintf_error(end - pos, ret))
  3173. return pos - buf;
  3174. pos += ret;
  3175. }
  3176. }
  3177. return pos - buf;
  3178. }
  3179. static int ctrl_iface_get_capability_group_mgmt(int res, char *strict,
  3180. struct wpa_driver_capa *capa,
  3181. char *buf, size_t buflen)
  3182. {
  3183. int ret;
  3184. char *pos, *end;
  3185. unsigned int i;
  3186. pos = buf;
  3187. end = pos + buflen;
  3188. if (res < 0)
  3189. return 0;
  3190. for (i = 0; i < ARRAY_SIZE(ciphers_group_mgmt); i++) {
  3191. if (capa->enc & ciphers_group_mgmt[i].capa) {
  3192. ret = os_snprintf(pos, end - pos, "%s%s",
  3193. pos == buf ? "" : " ",
  3194. ciphers_group_mgmt[i].name);
  3195. if (os_snprintf_error(end - pos, ret))
  3196. return pos - buf;
  3197. pos += ret;
  3198. }
  3199. }
  3200. return pos - buf;
  3201. }
  3202. static int ctrl_iface_get_capability_key_mgmt(int res, char *strict,
  3203. struct wpa_driver_capa *capa,
  3204. char *buf, size_t buflen)
  3205. {
  3206. int ret;
  3207. char *pos, *end;
  3208. size_t len;
  3209. pos = buf;
  3210. end = pos + buflen;
  3211. if (res < 0) {
  3212. if (strict)
  3213. return 0;
  3214. len = os_strlcpy(buf, "WPA-PSK WPA-EAP IEEE8021X WPA-NONE "
  3215. "NONE", buflen);
  3216. if (len >= buflen)
  3217. return -1;
  3218. return len;
  3219. }
  3220. ret = os_snprintf(pos, end - pos, "NONE IEEE8021X");
  3221. if (os_snprintf_error(end - pos, ret))
  3222. return pos - buf;
  3223. pos += ret;
  3224. if (capa->key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA |
  3225. WPA_DRIVER_CAPA_KEY_MGMT_WPA2)) {
  3226. ret = os_snprintf(pos, end - pos, " WPA-EAP");
  3227. if (os_snprintf_error(end - pos, ret))
  3228. return pos - buf;
  3229. pos += ret;
  3230. }
  3231. if (capa->key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA_PSK |
  3232. WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK)) {
  3233. ret = os_snprintf(pos, end - pos, " WPA-PSK");
  3234. if (os_snprintf_error(end - pos, ret))
  3235. return pos - buf;
  3236. pos += ret;
  3237. }
  3238. if (capa->key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_WPA_NONE) {
  3239. ret = os_snprintf(pos, end - pos, " WPA-NONE");
  3240. if (os_snprintf_error(end - pos, ret))
  3241. return pos - buf;
  3242. pos += ret;
  3243. }
  3244. #ifdef CONFIG_SUITEB
  3245. if (capa->key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_SUITE_B) {
  3246. ret = os_snprintf(pos, end - pos, " WPA-EAP-SUITE-B");
  3247. if (os_snprintf_error(end - pos, ret))
  3248. return pos - buf;
  3249. pos += ret;
  3250. }
  3251. #endif /* CONFIG_SUITEB */
  3252. #ifdef CONFIG_SUITEB192
  3253. if (capa->key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_SUITE_B_192) {
  3254. ret = os_snprintf(pos, end - pos, " WPA-EAP-SUITE-B-192");
  3255. if (os_snprintf_error(end - pos, ret))
  3256. return pos - buf;
  3257. pos += ret;
  3258. }
  3259. #endif /* CONFIG_SUITEB192 */
  3260. #ifdef CONFIG_OWE
  3261. if (capa->key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_OWE) {
  3262. ret = os_snprintf(pos, end - pos, " OWE");
  3263. if (os_snprintf_error(end - pos, ret))
  3264. return pos - buf;
  3265. pos += ret;
  3266. }
  3267. #endif /* CONFIG_OWE */
  3268. return pos - buf;
  3269. }
  3270. static int ctrl_iface_get_capability_proto(int res, char *strict,
  3271. struct wpa_driver_capa *capa,
  3272. char *buf, size_t buflen)
  3273. {
  3274. int ret;
  3275. char *pos, *end;
  3276. size_t len;
  3277. pos = buf;
  3278. end = pos + buflen;
  3279. if (res < 0) {
  3280. if (strict)
  3281. return 0;
  3282. len = os_strlcpy(buf, "RSN WPA", buflen);
  3283. if (len >= buflen)
  3284. return -1;
  3285. return len;
  3286. }
  3287. if (capa->key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA2 |
  3288. WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK)) {
  3289. ret = os_snprintf(pos, end - pos, "%sRSN",
  3290. pos == buf ? "" : " ");
  3291. if (os_snprintf_error(end - pos, ret))
  3292. return pos - buf;
  3293. pos += ret;
  3294. }
  3295. if (capa->key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA |
  3296. WPA_DRIVER_CAPA_KEY_MGMT_WPA_PSK)) {
  3297. ret = os_snprintf(pos, end - pos, "%sWPA",
  3298. pos == buf ? "" : " ");
  3299. if (os_snprintf_error(end - pos, ret))
  3300. return pos - buf;
  3301. pos += ret;
  3302. }
  3303. return pos - buf;
  3304. }
  3305. static int ctrl_iface_get_capability_auth_alg(struct wpa_supplicant *wpa_s,
  3306. int res, char *strict,
  3307. struct wpa_driver_capa *capa,
  3308. char *buf, size_t buflen)
  3309. {
  3310. int ret;
  3311. char *pos, *end;
  3312. size_t len;
  3313. pos = buf;
  3314. end = pos + buflen;
  3315. if (res < 0) {
  3316. if (strict)
  3317. return 0;
  3318. len = os_strlcpy(buf, "OPEN SHARED LEAP", buflen);
  3319. if (len >= buflen)
  3320. return -1;
  3321. return len;
  3322. }
  3323. if (capa->auth & (WPA_DRIVER_AUTH_OPEN)) {
  3324. ret = os_snprintf(pos, end - pos, "%sOPEN",
  3325. pos == buf ? "" : " ");
  3326. if (os_snprintf_error(end - pos, ret))
  3327. return pos - buf;
  3328. pos += ret;
  3329. }
  3330. if (capa->auth & (WPA_DRIVER_AUTH_SHARED)) {
  3331. ret = os_snprintf(pos, end - pos, "%sSHARED",
  3332. pos == buf ? "" : " ");
  3333. if (os_snprintf_error(end - pos, ret))
  3334. return pos - buf;
  3335. pos += ret;
  3336. }
  3337. if (capa->auth & (WPA_DRIVER_AUTH_LEAP)) {
  3338. ret = os_snprintf(pos, end - pos, "%sLEAP",
  3339. pos == buf ? "" : " ");
  3340. if (os_snprintf_error(end - pos, ret))
  3341. return pos - buf;
  3342. pos += ret;
  3343. }
  3344. #ifdef CONFIG_SAE
  3345. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SAE) {
  3346. ret = os_snprintf(pos, end - pos, "%sSAE",
  3347. pos == buf ? "" : " ");
  3348. if (os_snprintf_error(end - pos, ret))
  3349. return pos - buf;
  3350. pos += ret;
  3351. }
  3352. #endif /* CONFIG_SAE */
  3353. return pos - buf;
  3354. }
  3355. static int ctrl_iface_get_capability_modes(int res, char *strict,
  3356. struct wpa_driver_capa *capa,
  3357. char *buf, size_t buflen)
  3358. {
  3359. int ret;
  3360. char *pos, *end;
  3361. size_t len;
  3362. pos = buf;
  3363. end = pos + buflen;
  3364. if (res < 0) {
  3365. if (strict)
  3366. return 0;
  3367. len = os_strlcpy(buf, "IBSS AP", buflen);
  3368. if (len >= buflen)
  3369. return -1;
  3370. return len;
  3371. }
  3372. if (capa->flags & WPA_DRIVER_FLAGS_IBSS) {
  3373. ret = os_snprintf(pos, end - pos, "%sIBSS",
  3374. pos == buf ? "" : " ");
  3375. if (os_snprintf_error(end - pos, ret))
  3376. return pos - buf;
  3377. pos += ret;
  3378. }
  3379. if (capa->flags & WPA_DRIVER_FLAGS_AP) {
  3380. ret = os_snprintf(pos, end - pos, "%sAP",
  3381. pos == buf ? "" : " ");
  3382. if (os_snprintf_error(end - pos, ret))
  3383. return pos - buf;
  3384. pos += ret;
  3385. }
  3386. #ifdef CONFIG_MESH
  3387. if (capa->flags & WPA_DRIVER_FLAGS_MESH) {
  3388. ret = os_snprintf(pos, end - pos, "%sMESH",
  3389. pos == buf ? "" : " ");
  3390. if (os_snprintf_error(end - pos, ret))
  3391. return pos - buf;
  3392. pos += ret;
  3393. }
  3394. #endif /* CONFIG_MESH */
  3395. return pos - buf;
  3396. }
  3397. static int ctrl_iface_get_capability_channels(struct wpa_supplicant *wpa_s,
  3398. char *buf, size_t buflen)
  3399. {
  3400. struct hostapd_channel_data *chnl;
  3401. int ret, i, j;
  3402. char *pos, *end, *hmode;
  3403. pos = buf;
  3404. end = pos + buflen;
  3405. for (j = 0; j < wpa_s->hw.num_modes; j++) {
  3406. switch (wpa_s->hw.modes[j].mode) {
  3407. case HOSTAPD_MODE_IEEE80211B:
  3408. hmode = "B";
  3409. break;
  3410. case HOSTAPD_MODE_IEEE80211G:
  3411. hmode = "G";
  3412. break;
  3413. case HOSTAPD_MODE_IEEE80211A:
  3414. hmode = "A";
  3415. break;
  3416. case HOSTAPD_MODE_IEEE80211AD:
  3417. hmode = "AD";
  3418. break;
  3419. default:
  3420. continue;
  3421. }
  3422. ret = os_snprintf(pos, end - pos, "Mode[%s] Channels:", hmode);
  3423. if (os_snprintf_error(end - pos, ret))
  3424. return pos - buf;
  3425. pos += ret;
  3426. chnl = wpa_s->hw.modes[j].channels;
  3427. for (i = 0; i < wpa_s->hw.modes[j].num_channels; i++) {
  3428. if (chnl[i].flag & HOSTAPD_CHAN_DISABLED)
  3429. continue;
  3430. ret = os_snprintf(pos, end - pos, " %d", chnl[i].chan);
  3431. if (os_snprintf_error(end - pos, ret))
  3432. return pos - buf;
  3433. pos += ret;
  3434. }
  3435. ret = os_snprintf(pos, end - pos, "\n");
  3436. if (os_snprintf_error(end - pos, ret))
  3437. return pos - buf;
  3438. pos += ret;
  3439. }
  3440. return pos - buf;
  3441. }
  3442. static int ctrl_iface_get_capability_freq(struct wpa_supplicant *wpa_s,
  3443. char *buf, size_t buflen)
  3444. {
  3445. struct hostapd_channel_data *chnl;
  3446. int ret, i, j;
  3447. char *pos, *end, *hmode;
  3448. pos = buf;
  3449. end = pos + buflen;
  3450. for (j = 0; j < wpa_s->hw.num_modes; j++) {
  3451. switch (wpa_s->hw.modes[j].mode) {
  3452. case HOSTAPD_MODE_IEEE80211B:
  3453. hmode = "B";
  3454. break;
  3455. case HOSTAPD_MODE_IEEE80211G:
  3456. hmode = "G";
  3457. break;
  3458. case HOSTAPD_MODE_IEEE80211A:
  3459. hmode = "A";
  3460. break;
  3461. case HOSTAPD_MODE_IEEE80211AD:
  3462. hmode = "AD";
  3463. break;
  3464. default:
  3465. continue;
  3466. }
  3467. ret = os_snprintf(pos, end - pos, "Mode[%s] Channels:\n",
  3468. hmode);
  3469. if (os_snprintf_error(end - pos, ret))
  3470. return pos - buf;
  3471. pos += ret;
  3472. chnl = wpa_s->hw.modes[j].channels;
  3473. for (i = 0; i < wpa_s->hw.modes[j].num_channels; i++) {
  3474. if (chnl[i].flag & HOSTAPD_CHAN_DISABLED)
  3475. continue;
  3476. ret = os_snprintf(pos, end - pos, " %d = %d MHz%s%s\n",
  3477. chnl[i].chan, chnl[i].freq,
  3478. chnl[i].flag & HOSTAPD_CHAN_NO_IR ?
  3479. " (NO_IR)" : "",
  3480. chnl[i].flag & HOSTAPD_CHAN_RADAR ?
  3481. " (DFS)" : "");
  3482. if (os_snprintf_error(end - pos, ret))
  3483. return pos - buf;
  3484. pos += ret;
  3485. }
  3486. ret = os_snprintf(pos, end - pos, "\n");
  3487. if (os_snprintf_error(end - pos, ret))
  3488. return pos - buf;
  3489. pos += ret;
  3490. }
  3491. return pos - buf;
  3492. }
  3493. static int wpa_supplicant_ctrl_iface_get_capability(
  3494. struct wpa_supplicant *wpa_s, const char *_field, char *buf,
  3495. size_t buflen)
  3496. {
  3497. struct wpa_driver_capa capa;
  3498. int res;
  3499. char *strict;
  3500. char field[30];
  3501. size_t len;
  3502. /* Determine whether or not strict checking was requested */
  3503. len = os_strlcpy(field, _field, sizeof(field));
  3504. if (len >= sizeof(field))
  3505. return -1;
  3506. strict = os_strchr(field, ' ');
  3507. if (strict != NULL) {
  3508. *strict++ = '\0';
  3509. if (os_strcmp(strict, "strict") != 0)
  3510. return -1;
  3511. }
  3512. wpa_printf(MSG_DEBUG, "CTRL_IFACE: GET_CAPABILITY '%s' %s",
  3513. field, strict ? strict : "");
  3514. if (os_strcmp(field, "eap") == 0) {
  3515. return eap_get_names(buf, buflen);
  3516. }
  3517. res = wpa_drv_get_capa(wpa_s, &capa);
  3518. if (os_strcmp(field, "pairwise") == 0)
  3519. return ctrl_iface_get_capability_pairwise(res, strict, &capa,
  3520. buf, buflen);
  3521. if (os_strcmp(field, "group") == 0)
  3522. return ctrl_iface_get_capability_group(res, strict, &capa,
  3523. buf, buflen);
  3524. if (os_strcmp(field, "group_mgmt") == 0)
  3525. return ctrl_iface_get_capability_group_mgmt(res, strict, &capa,
  3526. buf, buflen);
  3527. if (os_strcmp(field, "key_mgmt") == 0)
  3528. return ctrl_iface_get_capability_key_mgmt(res, strict, &capa,
  3529. buf, buflen);
  3530. if (os_strcmp(field, "proto") == 0)
  3531. return ctrl_iface_get_capability_proto(res, strict, &capa,
  3532. buf, buflen);
  3533. if (os_strcmp(field, "auth_alg") == 0)
  3534. return ctrl_iface_get_capability_auth_alg(wpa_s, res, strict,
  3535. &capa, buf, buflen);
  3536. if (os_strcmp(field, "modes") == 0)
  3537. return ctrl_iface_get_capability_modes(res, strict, &capa,
  3538. buf, buflen);
  3539. if (os_strcmp(field, "channels") == 0)
  3540. return ctrl_iface_get_capability_channels(wpa_s, buf, buflen);
  3541. if (os_strcmp(field, "freq") == 0)
  3542. return ctrl_iface_get_capability_freq(wpa_s, buf, buflen);
  3543. #ifdef CONFIG_TDLS
  3544. if (os_strcmp(field, "tdls") == 0)
  3545. return ctrl_iface_get_capability_tdls(wpa_s, buf, buflen);
  3546. #endif /* CONFIG_TDLS */
  3547. #ifdef CONFIG_ERP
  3548. if (os_strcmp(field, "erp") == 0) {
  3549. res = os_snprintf(buf, buflen, "ERP");
  3550. if (os_snprintf_error(buflen, res))
  3551. return -1;
  3552. return res;
  3553. }
  3554. #endif /* CONFIG_EPR */
  3555. #ifdef CONFIG_FIPS
  3556. if (os_strcmp(field, "fips") == 0) {
  3557. res = os_snprintf(buf, buflen, "FIPS");
  3558. if (os_snprintf_error(buflen, res))
  3559. return -1;
  3560. return res;
  3561. }
  3562. #endif /* CONFIG_FIPS */
  3563. #ifdef CONFIG_ACS
  3564. if (os_strcmp(field, "acs") == 0) {
  3565. res = os_snprintf(buf, buflen, "ACS");
  3566. if (os_snprintf_error(buflen, res))
  3567. return -1;
  3568. return res;
  3569. }
  3570. #endif /* CONFIG_ACS */
  3571. #ifdef CONFIG_FILS
  3572. if (os_strcmp(field, "fils") == 0 &&
  3573. (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SUPPORT_FILS)) {
  3574. #ifdef CONFIG_FILS_SK_PFS
  3575. res = os_snprintf(buf, buflen, "FILS FILS-SK-PFS");
  3576. #else /* CONFIG_FILS_SK_PFS */
  3577. res = os_snprintf(buf, buflen, "FILS");
  3578. #endif /* CONFIG_FILS_SK_PFS */
  3579. if (os_snprintf_error(buflen, res))
  3580. return -1;
  3581. return res;
  3582. }
  3583. #endif /* CONFIG_FILS */
  3584. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Unknown GET_CAPABILITY field '%s'",
  3585. field);
  3586. return -1;
  3587. }
  3588. #ifdef CONFIG_INTERWORKING
  3589. static char * anqp_add_hex(char *pos, char *end, const char *title,
  3590. struct wpabuf *data)
  3591. {
  3592. char *start = pos;
  3593. size_t i;
  3594. int ret;
  3595. const u8 *d;
  3596. if (data == NULL)
  3597. return start;
  3598. ret = os_snprintf(pos, end - pos, "%s=", title);
  3599. if (os_snprintf_error(end - pos, ret))
  3600. return start;
  3601. pos += ret;
  3602. d = wpabuf_head_u8(data);
  3603. for (i = 0; i < wpabuf_len(data); i++) {
  3604. ret = os_snprintf(pos, end - pos, "%02x", *d++);
  3605. if (os_snprintf_error(end - pos, ret))
  3606. return start;
  3607. pos += ret;
  3608. }
  3609. ret = os_snprintf(pos, end - pos, "\n");
  3610. if (os_snprintf_error(end - pos, ret))
  3611. return start;
  3612. pos += ret;
  3613. return pos;
  3614. }
  3615. #endif /* CONFIG_INTERWORKING */
  3616. #ifdef CONFIG_FILS
  3617. static int print_fils_indication(struct wpa_bss *bss, char *pos, char *end)
  3618. {
  3619. char *start = pos;
  3620. const u8 *ie, *ie_end;
  3621. u16 info, realms;
  3622. int ret;
  3623. ie = wpa_bss_get_ie(bss, WLAN_EID_FILS_INDICATION);
  3624. if (!ie)
  3625. return 0;
  3626. ie_end = ie + 2 + ie[1];
  3627. ie += 2;
  3628. if (ie_end - ie < 2)
  3629. return -1;
  3630. info = WPA_GET_LE16(ie);
  3631. ie += 2;
  3632. ret = os_snprintf(pos, end - pos, "fils_info=%04x\n", info);
  3633. if (os_snprintf_error(end - pos, ret))
  3634. return 0;
  3635. pos += ret;
  3636. if (info & BIT(7)) {
  3637. /* Cache Identifier Included */
  3638. if (ie_end - ie < 2)
  3639. return -1;
  3640. ret = os_snprintf(pos, end - pos, "fils_cache_id=%02x%02x\n",
  3641. ie[0], ie[1]);
  3642. if (os_snprintf_error(end - pos, ret))
  3643. return 0;
  3644. pos += ret;
  3645. ie += 2;
  3646. }
  3647. if (info & BIT(8)) {
  3648. /* HESSID Included */
  3649. if (ie_end - ie < ETH_ALEN)
  3650. return -1;
  3651. ret = os_snprintf(pos, end - pos, "fils_hessid=" MACSTR "\n",
  3652. MAC2STR(ie));
  3653. if (os_snprintf_error(end - pos, ret))
  3654. return 0;
  3655. pos += ret;
  3656. ie += ETH_ALEN;
  3657. }
  3658. realms = (info & (BIT(3) | BIT(4) | BIT(5))) >> 3;
  3659. if (realms) {
  3660. if (ie_end - ie < realms * 2)
  3661. return -1;
  3662. ret = os_snprintf(pos, end - pos, "fils_realms=");
  3663. if (os_snprintf_error(end - pos, ret))
  3664. return 0;
  3665. pos += ret;
  3666. ret = wpa_snprintf_hex(pos, end - pos, ie, realms * 2);
  3667. if (ret <= 0)
  3668. return 0;
  3669. pos += ret;
  3670. ie += realms * 2;
  3671. ret = os_snprintf(pos, end - pos, "\n");
  3672. if (os_snprintf_error(end - pos, ret))
  3673. return 0;
  3674. pos += ret;
  3675. }
  3676. return pos - start;
  3677. }
  3678. #endif /* CONFIG_FILS */
  3679. static int print_bss_info(struct wpa_supplicant *wpa_s, struct wpa_bss *bss,
  3680. unsigned long mask, char *buf, size_t buflen)
  3681. {
  3682. size_t i;
  3683. int ret;
  3684. char *pos, *end;
  3685. const u8 *ie, *ie2, *osen_ie, *mesh;
  3686. pos = buf;
  3687. end = buf + buflen;
  3688. if (mask & WPA_BSS_MASK_ID) {
  3689. ret = os_snprintf(pos, end - pos, "id=%u\n", bss->id);
  3690. if (os_snprintf_error(end - pos, ret))
  3691. return 0;
  3692. pos += ret;
  3693. }
  3694. if (mask & WPA_BSS_MASK_BSSID) {
  3695. ret = os_snprintf(pos, end - pos, "bssid=" MACSTR "\n",
  3696. MAC2STR(bss->bssid));
  3697. if (os_snprintf_error(end - pos, ret))
  3698. return 0;
  3699. pos += ret;
  3700. }
  3701. if (mask & WPA_BSS_MASK_FREQ) {
  3702. ret = os_snprintf(pos, end - pos, "freq=%d\n", bss->freq);
  3703. if (os_snprintf_error(end - pos, ret))
  3704. return 0;
  3705. pos += ret;
  3706. }
  3707. if (mask & WPA_BSS_MASK_BEACON_INT) {
  3708. ret = os_snprintf(pos, end - pos, "beacon_int=%d\n",
  3709. bss->beacon_int);
  3710. if (os_snprintf_error(end - pos, ret))
  3711. return 0;
  3712. pos += ret;
  3713. }
  3714. if (mask & WPA_BSS_MASK_CAPABILITIES) {
  3715. ret = os_snprintf(pos, end - pos, "capabilities=0x%04x\n",
  3716. bss->caps);
  3717. if (os_snprintf_error(end - pos, ret))
  3718. return 0;
  3719. pos += ret;
  3720. }
  3721. if (mask & WPA_BSS_MASK_QUAL) {
  3722. ret = os_snprintf(pos, end - pos, "qual=%d\n", bss->qual);
  3723. if (os_snprintf_error(end - pos, ret))
  3724. return 0;
  3725. pos += ret;
  3726. }
  3727. if (mask & WPA_BSS_MASK_NOISE) {
  3728. ret = os_snprintf(pos, end - pos, "noise=%d\n", bss->noise);
  3729. if (os_snprintf_error(end - pos, ret))
  3730. return 0;
  3731. pos += ret;
  3732. }
  3733. if (mask & WPA_BSS_MASK_LEVEL) {
  3734. ret = os_snprintf(pos, end - pos, "level=%d\n", bss->level);
  3735. if (os_snprintf_error(end - pos, ret))
  3736. return 0;
  3737. pos += ret;
  3738. }
  3739. if (mask & WPA_BSS_MASK_TSF) {
  3740. ret = os_snprintf(pos, end - pos, "tsf=%016llu\n",
  3741. (unsigned long long) bss->tsf);
  3742. if (os_snprintf_error(end - pos, ret))
  3743. return 0;
  3744. pos += ret;
  3745. }
  3746. if (mask & WPA_BSS_MASK_AGE) {
  3747. struct os_reltime now;
  3748. os_get_reltime(&now);
  3749. ret = os_snprintf(pos, end - pos, "age=%d\n",
  3750. (int) (now.sec - bss->last_update.sec));
  3751. if (os_snprintf_error(end - pos, ret))
  3752. return 0;
  3753. pos += ret;
  3754. }
  3755. if (mask & WPA_BSS_MASK_IE) {
  3756. ret = os_snprintf(pos, end - pos, "ie=");
  3757. if (os_snprintf_error(end - pos, ret))
  3758. return 0;
  3759. pos += ret;
  3760. ie = (const u8 *) (bss + 1);
  3761. for (i = 0; i < bss->ie_len; i++) {
  3762. ret = os_snprintf(pos, end - pos, "%02x", *ie++);
  3763. if (os_snprintf_error(end - pos, ret))
  3764. return 0;
  3765. pos += ret;
  3766. }
  3767. ret = os_snprintf(pos, end - pos, "\n");
  3768. if (os_snprintf_error(end - pos, ret))
  3769. return 0;
  3770. pos += ret;
  3771. }
  3772. if (mask & WPA_BSS_MASK_FLAGS) {
  3773. ret = os_snprintf(pos, end - pos, "flags=");
  3774. if (os_snprintf_error(end - pos, ret))
  3775. return 0;
  3776. pos += ret;
  3777. mesh = wpa_bss_get_ie(bss, WLAN_EID_MESH_ID);
  3778. ie = wpa_bss_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
  3779. if (ie)
  3780. pos = wpa_supplicant_ie_txt(pos, end, "WPA", ie,
  3781. 2 + ie[1]);
  3782. ie2 = wpa_bss_get_ie(bss, WLAN_EID_RSN);
  3783. if (ie2)
  3784. pos = wpa_supplicant_ie_txt(pos, end,
  3785. mesh ? "RSN" : "WPA2", ie2,
  3786. 2 + ie2[1]);
  3787. osen_ie = wpa_bss_get_vendor_ie(bss, OSEN_IE_VENDOR_TYPE);
  3788. if (osen_ie)
  3789. pos = wpa_supplicant_ie_txt(pos, end, "OSEN",
  3790. osen_ie, 2 + osen_ie[1]);
  3791. pos = wpa_supplicant_wps_ie_txt(wpa_s, pos, end, bss);
  3792. if (!ie && !ie2 && !osen_ie &&
  3793. (bss->caps & IEEE80211_CAP_PRIVACY)) {
  3794. ret = os_snprintf(pos, end - pos, "[WEP]");
  3795. if (os_snprintf_error(end - pos, ret))
  3796. return 0;
  3797. pos += ret;
  3798. }
  3799. if (mesh) {
  3800. ret = os_snprintf(pos, end - pos, "[MESH]");
  3801. if (os_snprintf_error(end - pos, ret))
  3802. return 0;
  3803. pos += ret;
  3804. }
  3805. if (bss_is_dmg(bss)) {
  3806. const char *s;
  3807. ret = os_snprintf(pos, end - pos, "[DMG]");
  3808. if (os_snprintf_error(end - pos, ret))
  3809. return 0;
  3810. pos += ret;
  3811. switch (bss->caps & IEEE80211_CAP_DMG_MASK) {
  3812. case IEEE80211_CAP_DMG_IBSS:
  3813. s = "[IBSS]";
  3814. break;
  3815. case IEEE80211_CAP_DMG_AP:
  3816. s = "[ESS]";
  3817. break;
  3818. case IEEE80211_CAP_DMG_PBSS:
  3819. s = "[PBSS]";
  3820. break;
  3821. default:
  3822. s = "";
  3823. break;
  3824. }
  3825. ret = os_snprintf(pos, end - pos, "%s", s);
  3826. if (os_snprintf_error(end - pos, ret))
  3827. return 0;
  3828. pos += ret;
  3829. } else {
  3830. if (bss->caps & IEEE80211_CAP_IBSS) {
  3831. ret = os_snprintf(pos, end - pos, "[IBSS]");
  3832. if (os_snprintf_error(end - pos, ret))
  3833. return 0;
  3834. pos += ret;
  3835. }
  3836. if (bss->caps & IEEE80211_CAP_ESS) {
  3837. ret = os_snprintf(pos, end - pos, "[ESS]");
  3838. if (os_snprintf_error(end - pos, ret))
  3839. return 0;
  3840. pos += ret;
  3841. }
  3842. }
  3843. if (wpa_bss_get_vendor_ie(bss, P2P_IE_VENDOR_TYPE) ||
  3844. wpa_bss_get_vendor_ie_beacon(bss, P2P_IE_VENDOR_TYPE)) {
  3845. ret = os_snprintf(pos, end - pos, "[P2P]");
  3846. if (os_snprintf_error(end - pos, ret))
  3847. return 0;
  3848. pos += ret;
  3849. }
  3850. #ifdef CONFIG_HS20
  3851. if (wpa_bss_get_vendor_ie(bss, HS20_IE_VENDOR_TYPE)) {
  3852. ret = os_snprintf(pos, end - pos, "[HS20]");
  3853. if (os_snprintf_error(end - pos, ret))
  3854. return 0;
  3855. pos += ret;
  3856. }
  3857. #endif /* CONFIG_HS20 */
  3858. #ifdef CONFIG_FILS
  3859. if (wpa_bss_get_ie(bss, WLAN_EID_FILS_INDICATION)) {
  3860. ret = os_snprintf(pos, end - pos, "[FILS]");
  3861. if (os_snprintf_error(end - pos, ret))
  3862. return 0;
  3863. pos += ret;
  3864. }
  3865. #endif /* CONFIG_FILS */
  3866. ret = os_snprintf(pos, end - pos, "\n");
  3867. if (os_snprintf_error(end - pos, ret))
  3868. return 0;
  3869. pos += ret;
  3870. }
  3871. if (mask & WPA_BSS_MASK_SSID) {
  3872. ret = os_snprintf(pos, end - pos, "ssid=%s\n",
  3873. wpa_ssid_txt(bss->ssid, bss->ssid_len));
  3874. if (os_snprintf_error(end - pos, ret))
  3875. return 0;
  3876. pos += ret;
  3877. }
  3878. #ifdef CONFIG_WPS
  3879. if (mask & WPA_BSS_MASK_WPS_SCAN) {
  3880. ie = (const u8 *) (bss + 1);
  3881. ret = wpas_wps_scan_result_text(ie, bss->ie_len, pos, end);
  3882. if (ret >= end - pos)
  3883. return 0;
  3884. if (ret > 0)
  3885. pos += ret;
  3886. }
  3887. #endif /* CONFIG_WPS */
  3888. #ifdef CONFIG_P2P
  3889. if (mask & WPA_BSS_MASK_P2P_SCAN) {
  3890. ie = (const u8 *) (bss + 1);
  3891. ret = wpas_p2p_scan_result_text(ie, bss->ie_len, pos, end);
  3892. if (ret >= end - pos)
  3893. return 0;
  3894. if (ret > 0)
  3895. pos += ret;
  3896. }
  3897. #endif /* CONFIG_P2P */
  3898. #ifdef CONFIG_WIFI_DISPLAY
  3899. if (mask & WPA_BSS_MASK_WIFI_DISPLAY) {
  3900. struct wpabuf *wfd;
  3901. ie = (const u8 *) (bss + 1);
  3902. wfd = ieee802_11_vendor_ie_concat(ie, bss->ie_len,
  3903. WFD_IE_VENDOR_TYPE);
  3904. if (wfd) {
  3905. ret = os_snprintf(pos, end - pos, "wfd_subelems=");
  3906. if (os_snprintf_error(end - pos, ret)) {
  3907. wpabuf_free(wfd);
  3908. return 0;
  3909. }
  3910. pos += ret;
  3911. pos += wpa_snprintf_hex(pos, end - pos,
  3912. wpabuf_head(wfd),
  3913. wpabuf_len(wfd));
  3914. wpabuf_free(wfd);
  3915. ret = os_snprintf(pos, end - pos, "\n");
  3916. if (os_snprintf_error(end - pos, ret))
  3917. return 0;
  3918. pos += ret;
  3919. }
  3920. }
  3921. #endif /* CONFIG_WIFI_DISPLAY */
  3922. #ifdef CONFIG_INTERWORKING
  3923. if ((mask & WPA_BSS_MASK_INTERNETW) && bss->anqp) {
  3924. struct wpa_bss_anqp *anqp = bss->anqp;
  3925. struct wpa_bss_anqp_elem *elem;
  3926. pos = anqp_add_hex(pos, end, "anqp_capability_list",
  3927. anqp->capability_list);
  3928. pos = anqp_add_hex(pos, end, "anqp_venue_name",
  3929. anqp->venue_name);
  3930. pos = anqp_add_hex(pos, end, "anqp_network_auth_type",
  3931. anqp->network_auth_type);
  3932. pos = anqp_add_hex(pos, end, "anqp_roaming_consortium",
  3933. anqp->roaming_consortium);
  3934. pos = anqp_add_hex(pos, end, "anqp_ip_addr_type_availability",
  3935. anqp->ip_addr_type_availability);
  3936. pos = anqp_add_hex(pos, end, "anqp_nai_realm",
  3937. anqp->nai_realm);
  3938. pos = anqp_add_hex(pos, end, "anqp_3gpp", anqp->anqp_3gpp);
  3939. pos = anqp_add_hex(pos, end, "anqp_domain_name",
  3940. anqp->domain_name);
  3941. pos = anqp_add_hex(pos, end, "anqp_fils_realm_info",
  3942. anqp->fils_realm_info);
  3943. #ifdef CONFIG_HS20
  3944. pos = anqp_add_hex(pos, end, "hs20_capability_list",
  3945. anqp->hs20_capability_list);
  3946. pos = anqp_add_hex(pos, end, "hs20_operator_friendly_name",
  3947. anqp->hs20_operator_friendly_name);
  3948. pos = anqp_add_hex(pos, end, "hs20_wan_metrics",
  3949. anqp->hs20_wan_metrics);
  3950. pos = anqp_add_hex(pos, end, "hs20_connection_capability",
  3951. anqp->hs20_connection_capability);
  3952. pos = anqp_add_hex(pos, end, "hs20_operating_class",
  3953. anqp->hs20_operating_class);
  3954. pos = anqp_add_hex(pos, end, "hs20_osu_providers_list",
  3955. anqp->hs20_osu_providers_list);
  3956. #endif /* CONFIG_HS20 */
  3957. dl_list_for_each(elem, &anqp->anqp_elems,
  3958. struct wpa_bss_anqp_elem, list) {
  3959. char title[20];
  3960. os_snprintf(title, sizeof(title), "anqp[%u]",
  3961. elem->infoid);
  3962. pos = anqp_add_hex(pos, end, title, elem->payload);
  3963. }
  3964. }
  3965. #endif /* CONFIG_INTERWORKING */
  3966. #ifdef CONFIG_MESH
  3967. if (mask & WPA_BSS_MASK_MESH_SCAN) {
  3968. ie = (const u8 *) (bss + 1);
  3969. ret = wpas_mesh_scan_result_text(ie, bss->ie_len, pos, end);
  3970. if (ret >= end - pos)
  3971. return 0;
  3972. if (ret > 0)
  3973. pos += ret;
  3974. }
  3975. #endif /* CONFIG_MESH */
  3976. if (mask & WPA_BSS_MASK_SNR) {
  3977. ret = os_snprintf(pos, end - pos, "snr=%d\n", bss->snr);
  3978. if (os_snprintf_error(end - pos, ret))
  3979. return 0;
  3980. pos += ret;
  3981. }
  3982. if (mask & WPA_BSS_MASK_EST_THROUGHPUT) {
  3983. ret = os_snprintf(pos, end - pos, "est_throughput=%d\n",
  3984. bss->est_throughput);
  3985. if (os_snprintf_error(end - pos, ret))
  3986. return 0;
  3987. pos += ret;
  3988. }
  3989. #ifdef CONFIG_FST
  3990. if (mask & WPA_BSS_MASK_FST) {
  3991. ret = fst_ctrl_iface_mb_info(bss->bssid, pos, end - pos);
  3992. if (ret < 0 || ret >= end - pos)
  3993. return 0;
  3994. pos += ret;
  3995. }
  3996. #endif /* CONFIG_FST */
  3997. if (mask & WPA_BSS_MASK_UPDATE_IDX) {
  3998. ret = os_snprintf(pos, end - pos, "update_idx=%u\n",
  3999. bss->last_update_idx);
  4000. if (os_snprintf_error(end - pos, ret))
  4001. return 0;
  4002. pos += ret;
  4003. }
  4004. if ((mask & WPA_BSS_MASK_BEACON_IE) && bss->beacon_ie_len) {
  4005. ret = os_snprintf(pos, end - pos, "beacon_ie=");
  4006. if (os_snprintf_error(end - pos, ret))
  4007. return 0;
  4008. pos += ret;
  4009. ie = (const u8 *) (bss + 1);
  4010. ie += bss->ie_len;
  4011. for (i = 0; i < bss->beacon_ie_len; i++) {
  4012. ret = os_snprintf(pos, end - pos, "%02x", *ie++);
  4013. if (os_snprintf_error(end - pos, ret))
  4014. return 0;
  4015. pos += ret;
  4016. }
  4017. ret = os_snprintf(pos, end - pos, "\n");
  4018. if (os_snprintf_error(end - pos, ret))
  4019. return 0;
  4020. pos += ret;
  4021. }
  4022. #ifdef CONFIG_FILS
  4023. if (mask & WPA_BSS_MASK_FILS_INDICATION) {
  4024. ret = print_fils_indication(bss, pos, end);
  4025. if (ret < 0)
  4026. return 0;
  4027. pos += ret;
  4028. }
  4029. #endif /* CONFIG_FILS */
  4030. if (mask & WPA_BSS_MASK_DELIM) {
  4031. ret = os_snprintf(pos, end - pos, "====\n");
  4032. if (os_snprintf_error(end - pos, ret))
  4033. return 0;
  4034. pos += ret;
  4035. }
  4036. return pos - buf;
  4037. }
  4038. static int wpa_supplicant_ctrl_iface_bss(struct wpa_supplicant *wpa_s,
  4039. const char *cmd, char *buf,
  4040. size_t buflen)
  4041. {
  4042. u8 bssid[ETH_ALEN];
  4043. size_t i;
  4044. struct wpa_bss *bss;
  4045. struct wpa_bss *bsslast = NULL;
  4046. struct dl_list *next;
  4047. int ret = 0;
  4048. int len;
  4049. char *ctmp, *end = buf + buflen;
  4050. unsigned long mask = WPA_BSS_MASK_ALL;
  4051. if (os_strncmp(cmd, "RANGE=", 6) == 0) {
  4052. if (os_strncmp(cmd + 6, "ALL", 3) == 0) {
  4053. bss = dl_list_first(&wpa_s->bss_id, struct wpa_bss,
  4054. list_id);
  4055. bsslast = dl_list_last(&wpa_s->bss_id, struct wpa_bss,
  4056. list_id);
  4057. } else { /* N1-N2 */
  4058. unsigned int id1, id2;
  4059. if ((ctmp = os_strchr(cmd + 6, '-')) == NULL) {
  4060. wpa_printf(MSG_INFO, "Wrong BSS range "
  4061. "format");
  4062. return 0;
  4063. }
  4064. if (*(cmd + 6) == '-')
  4065. id1 = 0;
  4066. else
  4067. id1 = atoi(cmd + 6);
  4068. ctmp++;
  4069. if (*ctmp >= '0' && *ctmp <= '9')
  4070. id2 = atoi(ctmp);
  4071. else
  4072. id2 = (unsigned int) -1;
  4073. bss = wpa_bss_get_id_range(wpa_s, id1, id2);
  4074. if (id2 == (unsigned int) -1)
  4075. bsslast = dl_list_last(&wpa_s->bss_id,
  4076. struct wpa_bss,
  4077. list_id);
  4078. else {
  4079. bsslast = wpa_bss_get_id(wpa_s, id2);
  4080. if (bsslast == NULL && bss && id2 > id1) {
  4081. struct wpa_bss *tmp = bss;
  4082. for (;;) {
  4083. next = tmp->list_id.next;
  4084. if (next == &wpa_s->bss_id)
  4085. break;
  4086. tmp = dl_list_entry(
  4087. next, struct wpa_bss,
  4088. list_id);
  4089. if (tmp->id > id2)
  4090. break;
  4091. bsslast = tmp;
  4092. }
  4093. }
  4094. }
  4095. }
  4096. } else if (os_strncmp(cmd, "FIRST", 5) == 0)
  4097. bss = dl_list_first(&wpa_s->bss_id, struct wpa_bss, list_id);
  4098. else if (os_strncmp(cmd, "LAST", 4) == 0)
  4099. bss = dl_list_last(&wpa_s->bss_id, struct wpa_bss, list_id);
  4100. else if (os_strncmp(cmd, "ID-", 3) == 0) {
  4101. i = atoi(cmd + 3);
  4102. bss = wpa_bss_get_id(wpa_s, i);
  4103. } else if (os_strncmp(cmd, "NEXT-", 5) == 0) {
  4104. i = atoi(cmd + 5);
  4105. bss = wpa_bss_get_id(wpa_s, i);
  4106. if (bss) {
  4107. next = bss->list_id.next;
  4108. if (next == &wpa_s->bss_id)
  4109. bss = NULL;
  4110. else
  4111. bss = dl_list_entry(next, struct wpa_bss,
  4112. list_id);
  4113. }
  4114. } else if (os_strncmp(cmd, "CURRENT", 7) == 0) {
  4115. bss = wpa_s->current_bss;
  4116. #ifdef CONFIG_P2P
  4117. } else if (os_strncmp(cmd, "p2p_dev_addr=", 13) == 0) {
  4118. if (hwaddr_aton(cmd + 13, bssid) == 0)
  4119. bss = wpa_bss_get_p2p_dev_addr(wpa_s, bssid);
  4120. else
  4121. bss = NULL;
  4122. #endif /* CONFIG_P2P */
  4123. } else if (hwaddr_aton(cmd, bssid) == 0)
  4124. bss = wpa_bss_get_bssid(wpa_s, bssid);
  4125. else {
  4126. struct wpa_bss *tmp;
  4127. i = atoi(cmd);
  4128. bss = NULL;
  4129. dl_list_for_each(tmp, &wpa_s->bss_id, struct wpa_bss, list_id)
  4130. {
  4131. if (i-- == 0) {
  4132. bss = tmp;
  4133. break;
  4134. }
  4135. }
  4136. }
  4137. if ((ctmp = os_strstr(cmd, "MASK=")) != NULL) {
  4138. mask = strtoul(ctmp + 5, NULL, 0x10);
  4139. if (mask == 0)
  4140. mask = WPA_BSS_MASK_ALL;
  4141. }
  4142. if (bss == NULL)
  4143. return 0;
  4144. if (bsslast == NULL)
  4145. bsslast = bss;
  4146. do {
  4147. len = print_bss_info(wpa_s, bss, mask, buf, buflen);
  4148. ret += len;
  4149. buf += len;
  4150. buflen -= len;
  4151. if (bss == bsslast) {
  4152. if ((mask & WPA_BSS_MASK_DELIM) && len &&
  4153. (bss == dl_list_last(&wpa_s->bss_id,
  4154. struct wpa_bss, list_id))) {
  4155. int res;
  4156. res = os_snprintf(buf - 5, end - buf + 5,
  4157. "####\n");
  4158. if (os_snprintf_error(end - buf + 5, res)) {
  4159. wpa_printf(MSG_DEBUG,
  4160. "Could not add end delim");
  4161. }
  4162. }
  4163. break;
  4164. }
  4165. next = bss->list_id.next;
  4166. if (next == &wpa_s->bss_id)
  4167. break;
  4168. bss = dl_list_entry(next, struct wpa_bss, list_id);
  4169. } while (bss && len);
  4170. return ret;
  4171. }
  4172. static int wpa_supplicant_ctrl_iface_ap_scan(
  4173. struct wpa_supplicant *wpa_s, char *cmd)
  4174. {
  4175. int ap_scan = atoi(cmd);
  4176. return wpa_supplicant_set_ap_scan(wpa_s, ap_scan);
  4177. }
  4178. static int wpa_supplicant_ctrl_iface_scan_interval(
  4179. struct wpa_supplicant *wpa_s, char *cmd)
  4180. {
  4181. int scan_int = atoi(cmd);
  4182. return wpa_supplicant_set_scan_interval(wpa_s, scan_int);
  4183. }
  4184. static int wpa_supplicant_ctrl_iface_bss_expire_age(
  4185. struct wpa_supplicant *wpa_s, char *cmd)
  4186. {
  4187. int expire_age = atoi(cmd);
  4188. return wpa_supplicant_set_bss_expiration_age(wpa_s, expire_age);
  4189. }
  4190. static int wpa_supplicant_ctrl_iface_bss_expire_count(
  4191. struct wpa_supplicant *wpa_s, char *cmd)
  4192. {
  4193. int expire_count = atoi(cmd);
  4194. return wpa_supplicant_set_bss_expiration_count(wpa_s, expire_count);
  4195. }
  4196. static void wpa_supplicant_ctrl_iface_bss_flush(
  4197. struct wpa_supplicant *wpa_s, char *cmd)
  4198. {
  4199. int flush_age = atoi(cmd);
  4200. if (flush_age == 0)
  4201. wpa_bss_flush(wpa_s);
  4202. else
  4203. wpa_bss_flush_by_age(wpa_s, flush_age);
  4204. }
  4205. #ifdef CONFIG_TESTING_OPTIONS
  4206. static void wpa_supplicant_ctrl_iface_drop_sa(struct wpa_supplicant *wpa_s)
  4207. {
  4208. wpa_printf(MSG_DEBUG, "Dropping SA without deauthentication");
  4209. /* MLME-DELETEKEYS.request */
  4210. wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 0, 0, NULL, 0, NULL, 0);
  4211. wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 1, 0, NULL, 0, NULL, 0);
  4212. wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 2, 0, NULL, 0, NULL, 0);
  4213. wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 3, 0, NULL, 0, NULL, 0);
  4214. #ifdef CONFIG_IEEE80211W
  4215. wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 4, 0, NULL, 0, NULL, 0);
  4216. wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 5, 0, NULL, 0, NULL, 0);
  4217. #endif /* CONFIG_IEEE80211W */
  4218. wpa_drv_set_key(wpa_s, WPA_ALG_NONE, wpa_s->bssid, 0, 0, NULL, 0, NULL,
  4219. 0);
  4220. /* MLME-SETPROTECTION.request(None) */
  4221. wpa_drv_mlme_setprotection(wpa_s, wpa_s->bssid,
  4222. MLME_SETPROTECTION_PROTECT_TYPE_NONE,
  4223. MLME_SETPROTECTION_KEY_TYPE_PAIRWISE);
  4224. wpa_sm_drop_sa(wpa_s->wpa);
  4225. }
  4226. #endif /* CONFIG_TESTING_OPTIONS */
  4227. static int wpa_supplicant_ctrl_iface_roam(struct wpa_supplicant *wpa_s,
  4228. char *addr)
  4229. {
  4230. #ifdef CONFIG_NO_SCAN_PROCESSING
  4231. return -1;
  4232. #else /* CONFIG_NO_SCAN_PROCESSING */
  4233. u8 bssid[ETH_ALEN];
  4234. struct wpa_bss *bss;
  4235. struct wpa_ssid *ssid = wpa_s->current_ssid;
  4236. if (hwaddr_aton(addr, bssid)) {
  4237. wpa_printf(MSG_DEBUG, "CTRL_IFACE ROAM: invalid "
  4238. "address '%s'", addr);
  4239. return -1;
  4240. }
  4241. wpa_printf(MSG_DEBUG, "CTRL_IFACE ROAM " MACSTR, MAC2STR(bssid));
  4242. if (!ssid) {
  4243. wpa_printf(MSG_DEBUG, "CTRL_IFACE ROAM: No network "
  4244. "configuration known for the target AP");
  4245. return -1;
  4246. }
  4247. bss = wpa_bss_get(wpa_s, bssid, ssid->ssid, ssid->ssid_len);
  4248. if (!bss) {
  4249. wpa_printf(MSG_DEBUG, "CTRL_IFACE ROAM: Target AP not found "
  4250. "from BSS table");
  4251. return -1;
  4252. }
  4253. /*
  4254. * TODO: Find best network configuration block from configuration to
  4255. * allow roaming to other networks
  4256. */
  4257. wpa_s->reassociate = 1;
  4258. wpa_supplicant_connect(wpa_s, bss, ssid);
  4259. return 0;
  4260. #endif /* CONFIG_NO_SCAN_PROCESSING */
  4261. }
  4262. #ifdef CONFIG_P2P
  4263. static int p2p_ctrl_find(struct wpa_supplicant *wpa_s, char *cmd)
  4264. {
  4265. unsigned int timeout = atoi(cmd);
  4266. enum p2p_discovery_type type = P2P_FIND_START_WITH_FULL;
  4267. u8 dev_id[ETH_ALEN], *_dev_id = NULL;
  4268. u8 dev_type[WPS_DEV_TYPE_LEN], *_dev_type = NULL;
  4269. char *pos;
  4270. unsigned int search_delay;
  4271. const char *_seek[P2P_MAX_QUERY_HASH + 1], **seek = NULL;
  4272. u8 seek_count = 0;
  4273. int freq = 0;
  4274. if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED) {
  4275. wpa_dbg(wpa_s, MSG_INFO,
  4276. "Reject P2P_FIND since interface is disabled");
  4277. return -1;
  4278. }
  4279. if (os_strstr(cmd, "type=social"))
  4280. type = P2P_FIND_ONLY_SOCIAL;
  4281. else if (os_strstr(cmd, "type=progressive"))
  4282. type = P2P_FIND_PROGRESSIVE;
  4283. pos = os_strstr(cmd, "dev_id=");
  4284. if (pos) {
  4285. pos += 7;
  4286. if (hwaddr_aton(pos, dev_id))
  4287. return -1;
  4288. _dev_id = dev_id;
  4289. }
  4290. pos = os_strstr(cmd, "dev_type=");
  4291. if (pos) {
  4292. pos += 9;
  4293. if (wps_dev_type_str2bin(pos, dev_type) < 0)
  4294. return -1;
  4295. _dev_type = dev_type;
  4296. }
  4297. pos = os_strstr(cmd, "delay=");
  4298. if (pos) {
  4299. pos += 6;
  4300. search_delay = atoi(pos);
  4301. } else
  4302. search_delay = wpas_p2p_search_delay(wpa_s);
  4303. pos = os_strstr(cmd, "freq=");
  4304. if (pos) {
  4305. pos += 5;
  4306. freq = atoi(pos);
  4307. if (freq <= 0)
  4308. return -1;
  4309. }
  4310. /* Must be searched for last, because it adds nul termination */
  4311. pos = os_strstr(cmd, " seek=");
  4312. if (pos)
  4313. pos += 6;
  4314. while (pos && seek_count < P2P_MAX_QUERY_HASH + 1) {
  4315. char *term;
  4316. _seek[seek_count++] = pos;
  4317. seek = _seek;
  4318. term = os_strchr(pos, ' ');
  4319. if (!term)
  4320. break;
  4321. *term = '\0';
  4322. pos = os_strstr(term + 1, "seek=");
  4323. if (pos)
  4324. pos += 5;
  4325. }
  4326. if (seek_count > P2P_MAX_QUERY_HASH) {
  4327. seek[0] = NULL;
  4328. seek_count = 1;
  4329. }
  4330. return wpas_p2p_find(wpa_s, timeout, type, _dev_type != NULL, _dev_type,
  4331. _dev_id, search_delay, seek_count, seek, freq);
  4332. }
  4333. static int p2ps_ctrl_parse_cpt_priority(const char *pos, u8 *cpt)
  4334. {
  4335. const char *last = NULL;
  4336. const char *token;
  4337. long int token_len;
  4338. unsigned int i;
  4339. /* Expected predefined CPT names delimited by ':' */
  4340. for (i = 0; (token = cstr_token(pos, ": \t", &last)); i++) {
  4341. if (i >= P2PS_FEATURE_CAPAB_CPT_MAX) {
  4342. wpa_printf(MSG_ERROR,
  4343. "P2PS: CPT name list is too long, expected up to %d names",
  4344. P2PS_FEATURE_CAPAB_CPT_MAX);
  4345. cpt[0] = 0;
  4346. return -1;
  4347. }
  4348. token_len = last - token;
  4349. if (token_len == 3 &&
  4350. os_memcmp(token, "UDP", token_len) == 0) {
  4351. cpt[i] = P2PS_FEATURE_CAPAB_UDP_TRANSPORT;
  4352. } else if (token_len == 3 &&
  4353. os_memcmp(token, "MAC", token_len) == 0) {
  4354. cpt[i] = P2PS_FEATURE_CAPAB_MAC_TRANSPORT;
  4355. } else {
  4356. wpa_printf(MSG_ERROR,
  4357. "P2PS: Unsupported CPT name '%s'", token);
  4358. cpt[0] = 0;
  4359. return -1;
  4360. }
  4361. if (isblank((unsigned char) *last)) {
  4362. i++;
  4363. break;
  4364. }
  4365. }
  4366. cpt[i] = 0;
  4367. return 0;
  4368. }
  4369. static struct p2ps_provision * p2p_parse_asp_provision_cmd(const char *cmd)
  4370. {
  4371. struct p2ps_provision *p2ps_prov;
  4372. char *pos;
  4373. size_t info_len = 0;
  4374. char *info = NULL;
  4375. u8 role = P2PS_SETUP_NONE;
  4376. long long unsigned val;
  4377. int i;
  4378. pos = os_strstr(cmd, "info=");
  4379. if (pos) {
  4380. pos += 5;
  4381. info_len = os_strlen(pos);
  4382. if (info_len) {
  4383. info = os_malloc(info_len + 1);
  4384. if (info) {
  4385. info_len = utf8_unescape(pos, info_len,
  4386. info, info_len + 1);
  4387. } else
  4388. info_len = 0;
  4389. }
  4390. }
  4391. p2ps_prov = os_zalloc(sizeof(struct p2ps_provision) + info_len + 1);
  4392. if (p2ps_prov == NULL) {
  4393. os_free(info);
  4394. return NULL;
  4395. }
  4396. if (info) {
  4397. os_memcpy(p2ps_prov->info, info, info_len);
  4398. p2ps_prov->info[info_len] = '\0';
  4399. os_free(info);
  4400. }
  4401. pos = os_strstr(cmd, "status=");
  4402. if (pos)
  4403. p2ps_prov->status = atoi(pos + 7);
  4404. else
  4405. p2ps_prov->status = -1;
  4406. pos = os_strstr(cmd, "adv_id=");
  4407. if (!pos || sscanf(pos + 7, "%llx", &val) != 1 || val > 0xffffffffULL)
  4408. goto invalid_args;
  4409. p2ps_prov->adv_id = val;
  4410. pos = os_strstr(cmd, "method=");
  4411. if (pos)
  4412. p2ps_prov->method = strtol(pos + 7, NULL, 16);
  4413. else
  4414. p2ps_prov->method = 0;
  4415. pos = os_strstr(cmd, "session=");
  4416. if (!pos || sscanf(pos + 8, "%llx", &val) != 1 || val > 0xffffffffULL)
  4417. goto invalid_args;
  4418. p2ps_prov->session_id = val;
  4419. pos = os_strstr(cmd, "adv_mac=");
  4420. if (!pos || hwaddr_aton(pos + 8, p2ps_prov->adv_mac))
  4421. goto invalid_args;
  4422. pos = os_strstr(cmd, "session_mac=");
  4423. if (!pos || hwaddr_aton(pos + 12, p2ps_prov->session_mac))
  4424. goto invalid_args;
  4425. pos = os_strstr(cmd, "cpt=");
  4426. if (pos) {
  4427. if (p2ps_ctrl_parse_cpt_priority(pos + 4,
  4428. p2ps_prov->cpt_priority))
  4429. goto invalid_args;
  4430. } else {
  4431. p2ps_prov->cpt_priority[0] = P2PS_FEATURE_CAPAB_UDP_TRANSPORT;
  4432. }
  4433. for (i = 0; p2ps_prov->cpt_priority[i]; i++)
  4434. p2ps_prov->cpt_mask |= p2ps_prov->cpt_priority[i];
  4435. /* force conncap with tstCap (no sanity checks) */
  4436. pos = os_strstr(cmd, "tstCap=");
  4437. if (pos) {
  4438. role = strtol(pos + 7, NULL, 16);
  4439. } else {
  4440. pos = os_strstr(cmd, "role=");
  4441. if (pos) {
  4442. role = strtol(pos + 5, NULL, 16);
  4443. if (role != P2PS_SETUP_CLIENT &&
  4444. role != P2PS_SETUP_GROUP_OWNER)
  4445. role = P2PS_SETUP_NONE;
  4446. }
  4447. }
  4448. p2ps_prov->role = role;
  4449. return p2ps_prov;
  4450. invalid_args:
  4451. os_free(p2ps_prov);
  4452. return NULL;
  4453. }
  4454. static int p2p_ctrl_asp_provision_resp(struct wpa_supplicant *wpa_s, char *cmd)
  4455. {
  4456. u8 addr[ETH_ALEN];
  4457. struct p2ps_provision *p2ps_prov;
  4458. char *pos;
  4459. /* <addr> id=<adv_id> [role=<conncap>] [info=<infodata>] */
  4460. wpa_printf(MSG_DEBUG, "%s: %s", __func__, cmd);
  4461. if (hwaddr_aton(cmd, addr))
  4462. return -1;
  4463. pos = cmd + 17;
  4464. if (*pos != ' ')
  4465. return -1;
  4466. p2ps_prov = p2p_parse_asp_provision_cmd(pos);
  4467. if (!p2ps_prov)
  4468. return -1;
  4469. if (p2ps_prov->status < 0) {
  4470. os_free(p2ps_prov);
  4471. return -1;
  4472. }
  4473. return wpas_p2p_prov_disc(wpa_s, addr, NULL, WPAS_P2P_PD_FOR_ASP,
  4474. p2ps_prov);
  4475. }
  4476. static int p2p_ctrl_asp_provision(struct wpa_supplicant *wpa_s, char *cmd)
  4477. {
  4478. u8 addr[ETH_ALEN];
  4479. struct p2ps_provision *p2ps_prov;
  4480. char *pos;
  4481. /* <addr> id=<adv_id> adv_mac=<adv_mac> conncap=<conncap>
  4482. * session=<ses_id> mac=<ses_mac> [info=<infodata>]
  4483. */
  4484. wpa_printf(MSG_DEBUG, "%s: %s", __func__, cmd);
  4485. if (hwaddr_aton(cmd, addr))
  4486. return -1;
  4487. pos = cmd + 17;
  4488. if (*pos != ' ')
  4489. return -1;
  4490. p2ps_prov = p2p_parse_asp_provision_cmd(pos);
  4491. if (!p2ps_prov)
  4492. return -1;
  4493. p2ps_prov->pd_seeker = 1;
  4494. return wpas_p2p_prov_disc(wpa_s, addr, NULL, WPAS_P2P_PD_FOR_ASP,
  4495. p2ps_prov);
  4496. }
  4497. static int parse_freq(int chwidth, int freq2)
  4498. {
  4499. if (freq2 < 0)
  4500. return -1;
  4501. if (freq2)
  4502. return VHT_CHANWIDTH_80P80MHZ;
  4503. switch (chwidth) {
  4504. case 0:
  4505. case 20:
  4506. case 40:
  4507. return VHT_CHANWIDTH_USE_HT;
  4508. case 80:
  4509. return VHT_CHANWIDTH_80MHZ;
  4510. case 160:
  4511. return VHT_CHANWIDTH_160MHZ;
  4512. default:
  4513. wpa_printf(MSG_DEBUG, "Unknown max oper bandwidth: %d",
  4514. chwidth);
  4515. return -1;
  4516. }
  4517. }
  4518. static int p2p_ctrl_connect(struct wpa_supplicant *wpa_s, char *cmd,
  4519. char *buf, size_t buflen)
  4520. {
  4521. u8 addr[ETH_ALEN];
  4522. char *pos, *pos2;
  4523. char *pin = NULL;
  4524. enum p2p_wps_method wps_method;
  4525. int new_pin;
  4526. int ret;
  4527. int persistent_group, persistent_id = -1;
  4528. int join;
  4529. int auth;
  4530. int automatic;
  4531. int go_intent = -1;
  4532. int freq = 0;
  4533. int pd;
  4534. int ht40, vht, max_oper_chwidth, chwidth = 0, freq2 = 0;
  4535. u8 _group_ssid[SSID_MAX_LEN], *group_ssid = NULL;
  4536. size_t group_ssid_len = 0;
  4537. if (!wpa_s->global->p2p_init_wpa_s)
  4538. return -1;
  4539. if (wpa_s->global->p2p_init_wpa_s != wpa_s) {
  4540. wpa_dbg(wpa_s, MSG_DEBUG, "Direct P2P_CONNECT command to %s",
  4541. wpa_s->global->p2p_init_wpa_s->ifname);
  4542. wpa_s = wpa_s->global->p2p_init_wpa_s;
  4543. }
  4544. /* <addr> <"pbc" | "pin" | PIN> [label|display|keypad|p2ps]
  4545. * [persistent|persistent=<network id>]
  4546. * [join] [auth] [go_intent=<0..15>] [freq=<in MHz>] [provdisc]
  4547. * [ht40] [vht] [auto] [ssid=<hexdump>] */
  4548. if (hwaddr_aton(cmd, addr))
  4549. return -1;
  4550. pos = cmd + 17;
  4551. if (*pos != ' ')
  4552. return -1;
  4553. pos++;
  4554. persistent_group = os_strstr(pos, " persistent") != NULL;
  4555. pos2 = os_strstr(pos, " persistent=");
  4556. if (pos2) {
  4557. struct wpa_ssid *ssid;
  4558. persistent_id = atoi(pos2 + 12);
  4559. ssid = wpa_config_get_network(wpa_s->conf, persistent_id);
  4560. if (ssid == NULL || ssid->disabled != 2 ||
  4561. ssid->mode != WPAS_MODE_P2P_GO) {
  4562. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find "
  4563. "SSID id=%d for persistent P2P group (GO)",
  4564. persistent_id);
  4565. return -1;
  4566. }
  4567. }
  4568. join = os_strstr(pos, " join") != NULL;
  4569. auth = os_strstr(pos, " auth") != NULL;
  4570. automatic = os_strstr(pos, " auto") != NULL;
  4571. pd = os_strstr(pos, " provdisc") != NULL;
  4572. vht = (os_strstr(cmd, " vht") != NULL) || wpa_s->conf->p2p_go_vht;
  4573. ht40 = (os_strstr(cmd, " ht40") != NULL) || wpa_s->conf->p2p_go_ht40 ||
  4574. vht;
  4575. pos2 = os_strstr(pos, " go_intent=");
  4576. if (pos2) {
  4577. pos2 += 11;
  4578. go_intent = atoi(pos2);
  4579. if (go_intent < 0 || go_intent > 15)
  4580. return -1;
  4581. }
  4582. pos2 = os_strstr(pos, " freq=");
  4583. if (pos2) {
  4584. pos2 += 6;
  4585. freq = atoi(pos2);
  4586. if (freq <= 0)
  4587. return -1;
  4588. }
  4589. pos2 = os_strstr(pos, " freq2=");
  4590. if (pos2)
  4591. freq2 = atoi(pos2 + 7);
  4592. pos2 = os_strstr(pos, " max_oper_chwidth=");
  4593. if (pos2)
  4594. chwidth = atoi(pos2 + 18);
  4595. max_oper_chwidth = parse_freq(chwidth, freq2);
  4596. if (max_oper_chwidth < 0)
  4597. return -1;
  4598. pos2 = os_strstr(pos, " ssid=");
  4599. if (pos2) {
  4600. char *end;
  4601. pos2 += 6;
  4602. end = os_strchr(pos2, ' ');
  4603. if (!end)
  4604. group_ssid_len = os_strlen(pos2) / 2;
  4605. else
  4606. group_ssid_len = (end - pos2) / 2;
  4607. if (group_ssid_len == 0 || group_ssid_len > SSID_MAX_LEN ||
  4608. hexstr2bin(pos2, _group_ssid, group_ssid_len) < 0)
  4609. return -1;
  4610. group_ssid = _group_ssid;
  4611. }
  4612. if (os_strncmp(pos, "pin", 3) == 0) {
  4613. /* Request random PIN (to be displayed) and enable the PIN */
  4614. wps_method = WPS_PIN_DISPLAY;
  4615. } else if (os_strncmp(pos, "pbc", 3) == 0) {
  4616. wps_method = WPS_PBC;
  4617. } else if (os_strstr(pos, "p2ps") != NULL) {
  4618. wps_method = WPS_P2PS;
  4619. } else {
  4620. pin = pos;
  4621. pos = os_strchr(pin, ' ');
  4622. wps_method = WPS_PIN_KEYPAD;
  4623. if (pos) {
  4624. *pos++ = '\0';
  4625. if (os_strncmp(pos, "display", 7) == 0)
  4626. wps_method = WPS_PIN_DISPLAY;
  4627. }
  4628. if (!wps_pin_str_valid(pin)) {
  4629. os_memcpy(buf, "FAIL-INVALID-PIN\n", 17);
  4630. return 17;
  4631. }
  4632. }
  4633. new_pin = wpas_p2p_connect(wpa_s, addr, pin, wps_method,
  4634. persistent_group, automatic, join,
  4635. auth, go_intent, freq, freq2, persistent_id,
  4636. pd, ht40, vht, max_oper_chwidth,
  4637. group_ssid, group_ssid_len);
  4638. if (new_pin == -2) {
  4639. os_memcpy(buf, "FAIL-CHANNEL-UNAVAILABLE\n", 25);
  4640. return 25;
  4641. }
  4642. if (new_pin == -3) {
  4643. os_memcpy(buf, "FAIL-CHANNEL-UNSUPPORTED\n", 25);
  4644. return 25;
  4645. }
  4646. if (new_pin < 0)
  4647. return -1;
  4648. if (wps_method == WPS_PIN_DISPLAY && pin == NULL) {
  4649. ret = os_snprintf(buf, buflen, "%08d", new_pin);
  4650. if (os_snprintf_error(buflen, ret))
  4651. return -1;
  4652. return ret;
  4653. }
  4654. os_memcpy(buf, "OK\n", 3);
  4655. return 3;
  4656. }
  4657. static int p2p_ctrl_listen(struct wpa_supplicant *wpa_s, char *cmd)
  4658. {
  4659. unsigned int timeout = atoi(cmd);
  4660. if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED) {
  4661. wpa_dbg(wpa_s, MSG_INFO,
  4662. "Reject P2P_LISTEN since interface is disabled");
  4663. return -1;
  4664. }
  4665. return wpas_p2p_listen(wpa_s, timeout);
  4666. }
  4667. static int p2p_ctrl_prov_disc(struct wpa_supplicant *wpa_s, char *cmd)
  4668. {
  4669. u8 addr[ETH_ALEN];
  4670. char *pos;
  4671. enum wpas_p2p_prov_disc_use use = WPAS_P2P_PD_FOR_GO_NEG;
  4672. /* <addr> <config method> [join|auto] */
  4673. if (hwaddr_aton(cmd, addr))
  4674. return -1;
  4675. pos = cmd + 17;
  4676. if (*pos != ' ')
  4677. return -1;
  4678. pos++;
  4679. if (os_strstr(pos, " join") != NULL)
  4680. use = WPAS_P2P_PD_FOR_JOIN;
  4681. else if (os_strstr(pos, " auto") != NULL)
  4682. use = WPAS_P2P_PD_AUTO;
  4683. return wpas_p2p_prov_disc(wpa_s, addr, pos, use, NULL);
  4684. }
  4685. static int p2p_get_passphrase(struct wpa_supplicant *wpa_s, char *buf,
  4686. size_t buflen)
  4687. {
  4688. struct wpa_ssid *ssid = wpa_s->current_ssid;
  4689. if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GO ||
  4690. ssid->passphrase == NULL)
  4691. return -1;
  4692. os_strlcpy(buf, ssid->passphrase, buflen);
  4693. return os_strlen(buf);
  4694. }
  4695. static int p2p_ctrl_serv_disc_req(struct wpa_supplicant *wpa_s, char *cmd,
  4696. char *buf, size_t buflen)
  4697. {
  4698. u64 ref;
  4699. int res;
  4700. u8 dst_buf[ETH_ALEN], *dst;
  4701. struct wpabuf *tlvs;
  4702. char *pos;
  4703. size_t len;
  4704. if (hwaddr_aton(cmd, dst_buf))
  4705. return -1;
  4706. dst = dst_buf;
  4707. if (dst[0] == 0 && dst[1] == 0 && dst[2] == 0 &&
  4708. dst[3] == 0 && dst[4] == 0 && dst[5] == 0)
  4709. dst = NULL;
  4710. pos = cmd + 17;
  4711. if (*pos != ' ')
  4712. return -1;
  4713. pos++;
  4714. if (os_strncmp(pos, "upnp ", 5) == 0) {
  4715. u8 version;
  4716. pos += 5;
  4717. if (hexstr2bin(pos, &version, 1) < 0)
  4718. return -1;
  4719. pos += 2;
  4720. if (*pos != ' ')
  4721. return -1;
  4722. pos++;
  4723. ref = wpas_p2p_sd_request_upnp(wpa_s, dst, version, pos);
  4724. #ifdef CONFIG_WIFI_DISPLAY
  4725. } else if (os_strncmp(pos, "wifi-display ", 13) == 0) {
  4726. ref = wpas_p2p_sd_request_wifi_display(wpa_s, dst, pos + 13);
  4727. #endif /* CONFIG_WIFI_DISPLAY */
  4728. } else if (os_strncmp(pos, "asp ", 4) == 0) {
  4729. char *svc_str;
  4730. char *svc_info = NULL;
  4731. u32 id;
  4732. pos += 4;
  4733. if (sscanf(pos, "%x", &id) != 1 || id > 0xff)
  4734. return -1;
  4735. pos = os_strchr(pos, ' ');
  4736. if (pos == NULL || pos[1] == '\0' || pos[1] == ' ')
  4737. return -1;
  4738. svc_str = pos + 1;
  4739. pos = os_strchr(svc_str, ' ');
  4740. if (pos)
  4741. *pos++ = '\0';
  4742. /* All remaining data is the svc_info string */
  4743. if (pos && pos[0] && pos[0] != ' ') {
  4744. len = os_strlen(pos);
  4745. /* Unescape in place */
  4746. len = utf8_unescape(pos, len, pos, len);
  4747. if (len > 0xff)
  4748. return -1;
  4749. svc_info = pos;
  4750. }
  4751. ref = wpas_p2p_sd_request_asp(wpa_s, dst, (u8) id,
  4752. svc_str, svc_info);
  4753. } else {
  4754. len = os_strlen(pos);
  4755. if (len & 1)
  4756. return -1;
  4757. len /= 2;
  4758. tlvs = wpabuf_alloc(len);
  4759. if (tlvs == NULL)
  4760. return -1;
  4761. if (hexstr2bin(pos, wpabuf_put(tlvs, len), len) < 0) {
  4762. wpabuf_free(tlvs);
  4763. return -1;
  4764. }
  4765. ref = wpas_p2p_sd_request(wpa_s, dst, tlvs);
  4766. wpabuf_free(tlvs);
  4767. }
  4768. if (ref == 0)
  4769. return -1;
  4770. res = os_snprintf(buf, buflen, "%llx", (long long unsigned) ref);
  4771. if (os_snprintf_error(buflen, res))
  4772. return -1;
  4773. return res;
  4774. }
  4775. static int p2p_ctrl_serv_disc_cancel_req(struct wpa_supplicant *wpa_s,
  4776. char *cmd)
  4777. {
  4778. long long unsigned val;
  4779. u64 req;
  4780. if (sscanf(cmd, "%llx", &val) != 1)
  4781. return -1;
  4782. req = val;
  4783. return wpas_p2p_sd_cancel_request(wpa_s, req);
  4784. }
  4785. static int p2p_ctrl_serv_disc_resp(struct wpa_supplicant *wpa_s, char *cmd)
  4786. {
  4787. int freq;
  4788. u8 dst[ETH_ALEN];
  4789. u8 dialog_token;
  4790. struct wpabuf *resp_tlvs;
  4791. char *pos, *pos2;
  4792. size_t len;
  4793. pos = os_strchr(cmd, ' ');
  4794. if (pos == NULL)
  4795. return -1;
  4796. *pos++ = '\0';
  4797. freq = atoi(cmd);
  4798. if (freq == 0)
  4799. return -1;
  4800. if (hwaddr_aton(pos, dst))
  4801. return -1;
  4802. pos += 17;
  4803. if (*pos != ' ')
  4804. return -1;
  4805. pos++;
  4806. pos2 = os_strchr(pos, ' ');
  4807. if (pos2 == NULL)
  4808. return -1;
  4809. *pos2++ = '\0';
  4810. dialog_token = atoi(pos);
  4811. len = os_strlen(pos2);
  4812. if (len & 1)
  4813. return -1;
  4814. len /= 2;
  4815. resp_tlvs = wpabuf_alloc(len);
  4816. if (resp_tlvs == NULL)
  4817. return -1;
  4818. if (hexstr2bin(pos2, wpabuf_put(resp_tlvs, len), len) < 0) {
  4819. wpabuf_free(resp_tlvs);
  4820. return -1;
  4821. }
  4822. wpas_p2p_sd_response(wpa_s, freq, dst, dialog_token, resp_tlvs);
  4823. wpabuf_free(resp_tlvs);
  4824. return 0;
  4825. }
  4826. static int p2p_ctrl_serv_disc_external(struct wpa_supplicant *wpa_s,
  4827. char *cmd)
  4828. {
  4829. if (os_strcmp(cmd, "0") && os_strcmp(cmd, "1"))
  4830. return -1;
  4831. wpa_s->p2p_sd_over_ctrl_iface = atoi(cmd);
  4832. return 0;
  4833. }
  4834. static int p2p_ctrl_service_add_bonjour(struct wpa_supplicant *wpa_s,
  4835. char *cmd)
  4836. {
  4837. char *pos;
  4838. size_t len;
  4839. struct wpabuf *query, *resp;
  4840. pos = os_strchr(cmd, ' ');
  4841. if (pos == NULL)
  4842. return -1;
  4843. *pos++ = '\0';
  4844. len = os_strlen(cmd);
  4845. if (len & 1)
  4846. return -1;
  4847. len /= 2;
  4848. query = wpabuf_alloc(len);
  4849. if (query == NULL)
  4850. return -1;
  4851. if (hexstr2bin(cmd, wpabuf_put(query, len), len) < 0) {
  4852. wpabuf_free(query);
  4853. return -1;
  4854. }
  4855. len = os_strlen(pos);
  4856. if (len & 1) {
  4857. wpabuf_free(query);
  4858. return -1;
  4859. }
  4860. len /= 2;
  4861. resp = wpabuf_alloc(len);
  4862. if (resp == NULL) {
  4863. wpabuf_free(query);
  4864. return -1;
  4865. }
  4866. if (hexstr2bin(pos, wpabuf_put(resp, len), len) < 0) {
  4867. wpabuf_free(query);
  4868. wpabuf_free(resp);
  4869. return -1;
  4870. }
  4871. if (wpas_p2p_service_add_bonjour(wpa_s, query, resp) < 0) {
  4872. wpabuf_free(query);
  4873. wpabuf_free(resp);
  4874. return -1;
  4875. }
  4876. return 0;
  4877. }
  4878. static int p2p_ctrl_service_add_upnp(struct wpa_supplicant *wpa_s, char *cmd)
  4879. {
  4880. char *pos;
  4881. u8 version;
  4882. pos = os_strchr(cmd, ' ');
  4883. if (pos == NULL)
  4884. return -1;
  4885. *pos++ = '\0';
  4886. if (hexstr2bin(cmd, &version, 1) < 0)
  4887. return -1;
  4888. return wpas_p2p_service_add_upnp(wpa_s, version, pos);
  4889. }
  4890. static int p2p_ctrl_service_add_asp(struct wpa_supplicant *wpa_s,
  4891. u8 replace, char *cmd)
  4892. {
  4893. char *pos;
  4894. char *adv_str;
  4895. u32 auto_accept, adv_id, svc_state, config_methods;
  4896. char *svc_info = NULL;
  4897. char *cpt_prio_str;
  4898. u8 cpt_prio[P2PS_FEATURE_CAPAB_CPT_MAX + 1];
  4899. pos = os_strchr(cmd, ' ');
  4900. if (pos == NULL)
  4901. return -1;
  4902. *pos++ = '\0';
  4903. /* Auto-Accept value is mandatory, and must be one of the
  4904. * single values (0, 1, 2, 4) */
  4905. auto_accept = atoi(cmd);
  4906. switch (auto_accept) {
  4907. case P2PS_SETUP_NONE: /* No auto-accept */
  4908. case P2PS_SETUP_NEW:
  4909. case P2PS_SETUP_CLIENT:
  4910. case P2PS_SETUP_GROUP_OWNER:
  4911. break;
  4912. default:
  4913. return -1;
  4914. }
  4915. /* Advertisement ID is mandatory */
  4916. cmd = pos;
  4917. pos = os_strchr(cmd, ' ');
  4918. if (pos == NULL)
  4919. return -1;
  4920. *pos++ = '\0';
  4921. /* Handle Adv_ID == 0 (wildcard "org.wi-fi.wfds") internally. */
  4922. if (sscanf(cmd, "%x", &adv_id) != 1 || adv_id == 0)
  4923. return -1;
  4924. /* Only allow replacements if exist, and adds if not */
  4925. if (wpas_p2p_service_p2ps_id_exists(wpa_s, adv_id)) {
  4926. if (!replace)
  4927. return -1;
  4928. } else {
  4929. if (replace)
  4930. return -1;
  4931. }
  4932. /* svc_state between 0 - 0xff is mandatory */
  4933. if (sscanf(pos, "%x", &svc_state) != 1 || svc_state > 0xff)
  4934. return -1;
  4935. pos = os_strchr(pos, ' ');
  4936. if (pos == NULL)
  4937. return -1;
  4938. /* config_methods is mandatory */
  4939. pos++;
  4940. if (sscanf(pos, "%x", &config_methods) != 1)
  4941. return -1;
  4942. if (!(config_methods &
  4943. (WPS_CONFIG_DISPLAY | WPS_CONFIG_KEYPAD | WPS_CONFIG_P2PS)))
  4944. return -1;
  4945. pos = os_strchr(pos, ' ');
  4946. if (pos == NULL)
  4947. return -1;
  4948. pos++;
  4949. adv_str = pos;
  4950. /* Advertisement string is mandatory */
  4951. if (!pos[0] || pos[0] == ' ')
  4952. return -1;
  4953. /* Terminate svc string */
  4954. pos = os_strchr(pos, ' ');
  4955. if (pos != NULL)
  4956. *pos++ = '\0';
  4957. cpt_prio_str = (pos && pos[0]) ? os_strstr(pos, "cpt=") : NULL;
  4958. if (cpt_prio_str) {
  4959. pos = os_strchr(pos, ' ');
  4960. if (pos != NULL)
  4961. *pos++ = '\0';
  4962. if (p2ps_ctrl_parse_cpt_priority(cpt_prio_str + 4, cpt_prio))
  4963. return -1;
  4964. } else {
  4965. cpt_prio[0] = P2PS_FEATURE_CAPAB_UDP_TRANSPORT;
  4966. cpt_prio[1] = 0;
  4967. }
  4968. /* Service and Response Information are optional */
  4969. if (pos && pos[0]) {
  4970. size_t len;
  4971. /* Note the bare ' included, which cannot exist legally
  4972. * in unescaped string. */
  4973. svc_info = os_strstr(pos, "svc_info='");
  4974. if (svc_info) {
  4975. svc_info += 9;
  4976. len = os_strlen(svc_info);
  4977. utf8_unescape(svc_info, len, svc_info, len);
  4978. }
  4979. }
  4980. return wpas_p2p_service_add_asp(wpa_s, auto_accept, adv_id, adv_str,
  4981. (u8) svc_state, (u16) config_methods,
  4982. svc_info, cpt_prio);
  4983. }
  4984. static int p2p_ctrl_service_add(struct wpa_supplicant *wpa_s, char *cmd)
  4985. {
  4986. char *pos;
  4987. pos = os_strchr(cmd, ' ');
  4988. if (pos == NULL)
  4989. return -1;
  4990. *pos++ = '\0';
  4991. if (os_strcmp(cmd, "bonjour") == 0)
  4992. return p2p_ctrl_service_add_bonjour(wpa_s, pos);
  4993. if (os_strcmp(cmd, "upnp") == 0)
  4994. return p2p_ctrl_service_add_upnp(wpa_s, pos);
  4995. if (os_strcmp(cmd, "asp") == 0)
  4996. return p2p_ctrl_service_add_asp(wpa_s, 0, pos);
  4997. wpa_printf(MSG_DEBUG, "Unknown service '%s'", cmd);
  4998. return -1;
  4999. }
  5000. static int p2p_ctrl_service_del_bonjour(struct wpa_supplicant *wpa_s,
  5001. char *cmd)
  5002. {
  5003. size_t len;
  5004. struct wpabuf *query;
  5005. int ret;
  5006. len = os_strlen(cmd);
  5007. if (len & 1)
  5008. return -1;
  5009. len /= 2;
  5010. query = wpabuf_alloc(len);
  5011. if (query == NULL)
  5012. return -1;
  5013. if (hexstr2bin(cmd, wpabuf_put(query, len), len) < 0) {
  5014. wpabuf_free(query);
  5015. return -1;
  5016. }
  5017. ret = wpas_p2p_service_del_bonjour(wpa_s, query);
  5018. wpabuf_free(query);
  5019. return ret;
  5020. }
  5021. static int p2p_ctrl_service_del_upnp(struct wpa_supplicant *wpa_s, char *cmd)
  5022. {
  5023. char *pos;
  5024. u8 version;
  5025. pos = os_strchr(cmd, ' ');
  5026. if (pos == NULL)
  5027. return -1;
  5028. *pos++ = '\0';
  5029. if (hexstr2bin(cmd, &version, 1) < 0)
  5030. return -1;
  5031. return wpas_p2p_service_del_upnp(wpa_s, version, pos);
  5032. }
  5033. static int p2p_ctrl_service_del_asp(struct wpa_supplicant *wpa_s, char *cmd)
  5034. {
  5035. u32 adv_id;
  5036. if (os_strcmp(cmd, "all") == 0) {
  5037. wpas_p2p_service_flush_asp(wpa_s);
  5038. return 0;
  5039. }
  5040. if (sscanf(cmd, "%x", &adv_id) != 1)
  5041. return -1;
  5042. return wpas_p2p_service_del_asp(wpa_s, adv_id);
  5043. }
  5044. static int p2p_ctrl_service_del(struct wpa_supplicant *wpa_s, char *cmd)
  5045. {
  5046. char *pos;
  5047. pos = os_strchr(cmd, ' ');
  5048. if (pos == NULL)
  5049. return -1;
  5050. *pos++ = '\0';
  5051. if (os_strcmp(cmd, "bonjour") == 0)
  5052. return p2p_ctrl_service_del_bonjour(wpa_s, pos);
  5053. if (os_strcmp(cmd, "upnp") == 0)
  5054. return p2p_ctrl_service_del_upnp(wpa_s, pos);
  5055. if (os_strcmp(cmd, "asp") == 0)
  5056. return p2p_ctrl_service_del_asp(wpa_s, pos);
  5057. wpa_printf(MSG_DEBUG, "Unknown service '%s'", cmd);
  5058. return -1;
  5059. }
  5060. static int p2p_ctrl_service_replace(struct wpa_supplicant *wpa_s, char *cmd)
  5061. {
  5062. char *pos;
  5063. pos = os_strchr(cmd, ' ');
  5064. if (pos == NULL)
  5065. return -1;
  5066. *pos++ = '\0';
  5067. if (os_strcmp(cmd, "asp") == 0)
  5068. return p2p_ctrl_service_add_asp(wpa_s, 1, pos);
  5069. wpa_printf(MSG_DEBUG, "Unknown service '%s'", cmd);
  5070. return -1;
  5071. }
  5072. static int p2p_ctrl_reject(struct wpa_supplicant *wpa_s, char *cmd)
  5073. {
  5074. u8 addr[ETH_ALEN];
  5075. /* <addr> */
  5076. if (hwaddr_aton(cmd, addr))
  5077. return -1;
  5078. return wpas_p2p_reject(wpa_s, addr);
  5079. }
  5080. static int p2p_ctrl_invite_persistent(struct wpa_supplicant *wpa_s, char *cmd)
  5081. {
  5082. char *pos;
  5083. int id;
  5084. struct wpa_ssid *ssid;
  5085. u8 *_peer = NULL, peer[ETH_ALEN];
  5086. int freq = 0, pref_freq = 0;
  5087. int ht40, vht, max_oper_chwidth, chwidth = 0, freq2 = 0;
  5088. id = atoi(cmd);
  5089. pos = os_strstr(cmd, " peer=");
  5090. if (pos) {
  5091. pos += 6;
  5092. if (hwaddr_aton(pos, peer))
  5093. return -1;
  5094. _peer = peer;
  5095. }
  5096. ssid = wpa_config_get_network(wpa_s->conf, id);
  5097. if (ssid == NULL || ssid->disabled != 2) {
  5098. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find SSID id=%d "
  5099. "for persistent P2P group",
  5100. id);
  5101. return -1;
  5102. }
  5103. pos = os_strstr(cmd, " freq=");
  5104. if (pos) {
  5105. pos += 6;
  5106. freq = atoi(pos);
  5107. if (freq <= 0)
  5108. return -1;
  5109. }
  5110. pos = os_strstr(cmd, " pref=");
  5111. if (pos) {
  5112. pos += 6;
  5113. pref_freq = atoi(pos);
  5114. if (pref_freq <= 0)
  5115. return -1;
  5116. }
  5117. vht = (os_strstr(cmd, " vht") != NULL) || wpa_s->conf->p2p_go_vht;
  5118. ht40 = (os_strstr(cmd, " ht40") != NULL) || wpa_s->conf->p2p_go_ht40 ||
  5119. vht;
  5120. pos = os_strstr(cmd, "freq2=");
  5121. if (pos)
  5122. freq2 = atoi(pos + 6);
  5123. pos = os_strstr(cmd, " max_oper_chwidth=");
  5124. if (pos)
  5125. chwidth = atoi(pos + 18);
  5126. max_oper_chwidth = parse_freq(chwidth, freq2);
  5127. if (max_oper_chwidth < 0)
  5128. return -1;
  5129. return wpas_p2p_invite(wpa_s, _peer, ssid, NULL, freq, freq2, ht40, vht,
  5130. max_oper_chwidth, pref_freq);
  5131. }
  5132. static int p2p_ctrl_invite_group(struct wpa_supplicant *wpa_s, char *cmd)
  5133. {
  5134. char *pos;
  5135. u8 peer[ETH_ALEN], go_dev_addr[ETH_ALEN], *go_dev = NULL;
  5136. pos = os_strstr(cmd, " peer=");
  5137. if (!pos)
  5138. return -1;
  5139. *pos = '\0';
  5140. pos += 6;
  5141. if (hwaddr_aton(pos, peer)) {
  5142. wpa_printf(MSG_DEBUG, "P2P: Invalid MAC address '%s'", pos);
  5143. return -1;
  5144. }
  5145. pos = os_strstr(pos, " go_dev_addr=");
  5146. if (pos) {
  5147. pos += 13;
  5148. if (hwaddr_aton(pos, go_dev_addr)) {
  5149. wpa_printf(MSG_DEBUG, "P2P: Invalid MAC address '%s'",
  5150. pos);
  5151. return -1;
  5152. }
  5153. go_dev = go_dev_addr;
  5154. }
  5155. return wpas_p2p_invite_group(wpa_s, cmd, peer, go_dev);
  5156. }
  5157. static int p2p_ctrl_invite(struct wpa_supplicant *wpa_s, char *cmd)
  5158. {
  5159. if (os_strncmp(cmd, "persistent=", 11) == 0)
  5160. return p2p_ctrl_invite_persistent(wpa_s, cmd + 11);
  5161. if (os_strncmp(cmd, "group=", 6) == 0)
  5162. return p2p_ctrl_invite_group(wpa_s, cmd + 6);
  5163. return -1;
  5164. }
  5165. static int p2p_ctrl_group_add_persistent(struct wpa_supplicant *wpa_s,
  5166. int id, int freq, int vht_center_freq2,
  5167. int ht40, int vht, int vht_chwidth)
  5168. {
  5169. struct wpa_ssid *ssid;
  5170. ssid = wpa_config_get_network(wpa_s->conf, id);
  5171. if (ssid == NULL || ssid->disabled != 2) {
  5172. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find SSID id=%d "
  5173. "for persistent P2P group",
  5174. id);
  5175. return -1;
  5176. }
  5177. return wpas_p2p_group_add_persistent(wpa_s, ssid, 0, freq,
  5178. vht_center_freq2, 0, ht40, vht,
  5179. vht_chwidth, NULL, 0, 0);
  5180. }
  5181. static int p2p_ctrl_group_add(struct wpa_supplicant *wpa_s, char *cmd)
  5182. {
  5183. int freq = 0, persistent = 0, group_id = -1;
  5184. int vht = wpa_s->conf->p2p_go_vht;
  5185. int ht40 = wpa_s->conf->p2p_go_ht40 || vht;
  5186. int max_oper_chwidth, chwidth = 0, freq2 = 0;
  5187. char *token, *context = NULL;
  5188. while ((token = str_token(cmd, " ", &context))) {
  5189. if (sscanf(token, "freq=%d", &freq) == 1 ||
  5190. sscanf(token, "freq2=%d", &freq2) == 1 ||
  5191. sscanf(token, "persistent=%d", &group_id) == 1 ||
  5192. sscanf(token, "max_oper_chwidth=%d", &chwidth) == 1) {
  5193. continue;
  5194. } else if (os_strcmp(token, "ht40") == 0) {
  5195. ht40 = 1;
  5196. } else if (os_strcmp(token, "vht") == 0) {
  5197. vht = 1;
  5198. ht40 = 1;
  5199. } else if (os_strcmp(token, "persistent") == 0) {
  5200. persistent = 1;
  5201. } else {
  5202. wpa_printf(MSG_DEBUG,
  5203. "CTRL: Invalid P2P_GROUP_ADD parameter: '%s'",
  5204. token);
  5205. return -1;
  5206. }
  5207. }
  5208. max_oper_chwidth = parse_freq(chwidth, freq2);
  5209. if (max_oper_chwidth < 0)
  5210. return -1;
  5211. if (group_id >= 0)
  5212. return p2p_ctrl_group_add_persistent(wpa_s, group_id,
  5213. freq, freq2, ht40, vht,
  5214. max_oper_chwidth);
  5215. return wpas_p2p_group_add(wpa_s, persistent, freq, freq2, ht40, vht,
  5216. max_oper_chwidth);
  5217. }
  5218. static int p2p_ctrl_group_member(struct wpa_supplicant *wpa_s, const char *cmd,
  5219. char *buf, size_t buflen)
  5220. {
  5221. u8 dev_addr[ETH_ALEN];
  5222. struct wpa_ssid *ssid;
  5223. int res;
  5224. const u8 *iaddr;
  5225. ssid = wpa_s->current_ssid;
  5226. if (!wpa_s->global->p2p || !ssid || ssid->mode != WPAS_MODE_P2P_GO ||
  5227. hwaddr_aton(cmd, dev_addr))
  5228. return -1;
  5229. iaddr = p2p_group_get_client_interface_addr(wpa_s->p2p_group, dev_addr);
  5230. if (!iaddr)
  5231. return -1;
  5232. res = os_snprintf(buf, buflen, MACSTR, MAC2STR(iaddr));
  5233. if (os_snprintf_error(buflen, res))
  5234. return -1;
  5235. return res;
  5236. }
  5237. static int wpas_find_p2p_dev_addr_bss(struct wpa_global *global,
  5238. const u8 *p2p_dev_addr)
  5239. {
  5240. struct wpa_supplicant *wpa_s;
  5241. for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
  5242. if (wpa_bss_get_p2p_dev_addr(wpa_s, p2p_dev_addr))
  5243. return 1;
  5244. }
  5245. return 0;
  5246. }
  5247. static int p2p_ctrl_peer(struct wpa_supplicant *wpa_s, char *cmd,
  5248. char *buf, size_t buflen)
  5249. {
  5250. u8 addr[ETH_ALEN], *addr_ptr, group_capab;
  5251. int next, res;
  5252. const struct p2p_peer_info *info;
  5253. char *pos, *end;
  5254. char devtype[WPS_DEV_TYPE_BUFSIZE];
  5255. struct wpa_ssid *ssid;
  5256. size_t i;
  5257. if (!wpa_s->global->p2p)
  5258. return -1;
  5259. if (os_strcmp(cmd, "FIRST") == 0) {
  5260. addr_ptr = NULL;
  5261. next = 0;
  5262. } else if (os_strncmp(cmd, "NEXT-", 5) == 0) {
  5263. if (hwaddr_aton(cmd + 5, addr) < 0)
  5264. return -1;
  5265. addr_ptr = addr;
  5266. next = 1;
  5267. } else {
  5268. if (hwaddr_aton(cmd, addr) < 0)
  5269. return -1;
  5270. addr_ptr = addr;
  5271. next = 0;
  5272. }
  5273. info = p2p_get_peer_info(wpa_s->global->p2p, addr_ptr, next);
  5274. if (info == NULL)
  5275. return -1;
  5276. group_capab = info->group_capab;
  5277. if (group_capab &&
  5278. !wpas_find_p2p_dev_addr_bss(wpa_s->global, info->p2p_device_addr)) {
  5279. wpa_printf(MSG_DEBUG,
  5280. "P2P: Could not find any BSS with p2p_dev_addr "
  5281. MACSTR ", hence override group_capab from 0x%x to 0",
  5282. MAC2STR(info->p2p_device_addr), group_capab);
  5283. group_capab = 0;
  5284. }
  5285. pos = buf;
  5286. end = buf + buflen;
  5287. res = os_snprintf(pos, end - pos, MACSTR "\n"
  5288. "pri_dev_type=%s\n"
  5289. "device_name=%s\n"
  5290. "manufacturer=%s\n"
  5291. "model_name=%s\n"
  5292. "model_number=%s\n"
  5293. "serial_number=%s\n"
  5294. "config_methods=0x%x\n"
  5295. "dev_capab=0x%x\n"
  5296. "group_capab=0x%x\n"
  5297. "level=%d\n",
  5298. MAC2STR(info->p2p_device_addr),
  5299. wps_dev_type_bin2str(info->pri_dev_type,
  5300. devtype, sizeof(devtype)),
  5301. info->device_name,
  5302. info->manufacturer,
  5303. info->model_name,
  5304. info->model_number,
  5305. info->serial_number,
  5306. info->config_methods,
  5307. info->dev_capab,
  5308. group_capab,
  5309. info->level);
  5310. if (os_snprintf_error(end - pos, res))
  5311. return pos - buf;
  5312. pos += res;
  5313. for (i = 0; i < info->wps_sec_dev_type_list_len / WPS_DEV_TYPE_LEN; i++)
  5314. {
  5315. const u8 *t;
  5316. t = &info->wps_sec_dev_type_list[i * WPS_DEV_TYPE_LEN];
  5317. res = os_snprintf(pos, end - pos, "sec_dev_type=%s\n",
  5318. wps_dev_type_bin2str(t, devtype,
  5319. sizeof(devtype)));
  5320. if (os_snprintf_error(end - pos, res))
  5321. return pos - buf;
  5322. pos += res;
  5323. }
  5324. ssid = wpas_p2p_get_persistent(wpa_s, info->p2p_device_addr, NULL, 0);
  5325. if (ssid) {
  5326. res = os_snprintf(pos, end - pos, "persistent=%d\n", ssid->id);
  5327. if (os_snprintf_error(end - pos, res))
  5328. return pos - buf;
  5329. pos += res;
  5330. }
  5331. res = p2p_get_peer_info_txt(info, pos, end - pos);
  5332. if (res < 0)
  5333. return pos - buf;
  5334. pos += res;
  5335. if (info->vendor_elems) {
  5336. res = os_snprintf(pos, end - pos, "vendor_elems=");
  5337. if (os_snprintf_error(end - pos, res))
  5338. return pos - buf;
  5339. pos += res;
  5340. pos += wpa_snprintf_hex(pos, end - pos,
  5341. wpabuf_head(info->vendor_elems),
  5342. wpabuf_len(info->vendor_elems));
  5343. res = os_snprintf(pos, end - pos, "\n");
  5344. if (os_snprintf_error(end - pos, res))
  5345. return pos - buf;
  5346. pos += res;
  5347. }
  5348. return pos - buf;
  5349. }
  5350. static int p2p_ctrl_disallow_freq(struct wpa_supplicant *wpa_s,
  5351. const char *param)
  5352. {
  5353. unsigned int i;
  5354. if (wpa_s->global->p2p == NULL)
  5355. return -1;
  5356. if (freq_range_list_parse(&wpa_s->global->p2p_disallow_freq, param) < 0)
  5357. return -1;
  5358. for (i = 0; i < wpa_s->global->p2p_disallow_freq.num; i++) {
  5359. struct wpa_freq_range *freq;
  5360. freq = &wpa_s->global->p2p_disallow_freq.range[i];
  5361. wpa_printf(MSG_DEBUG, "P2P: Disallowed frequency range %u-%u",
  5362. freq->min, freq->max);
  5363. }
  5364. wpas_p2p_update_channel_list(wpa_s, WPAS_P2P_CHANNEL_UPDATE_DISALLOW);
  5365. return 0;
  5366. }
  5367. static int p2p_ctrl_set(struct wpa_supplicant *wpa_s, char *cmd)
  5368. {
  5369. char *param;
  5370. if (wpa_s->global->p2p == NULL)
  5371. return -1;
  5372. param = os_strchr(cmd, ' ');
  5373. if (param == NULL)
  5374. return -1;
  5375. *param++ = '\0';
  5376. if (os_strcmp(cmd, "discoverability") == 0) {
  5377. p2p_set_client_discoverability(wpa_s->global->p2p,
  5378. atoi(param));
  5379. return 0;
  5380. }
  5381. if (os_strcmp(cmd, "managed") == 0) {
  5382. p2p_set_managed_oper(wpa_s->global->p2p, atoi(param));
  5383. return 0;
  5384. }
  5385. if (os_strcmp(cmd, "listen_channel") == 0) {
  5386. char *pos;
  5387. u8 channel, op_class;
  5388. channel = atoi(param);
  5389. pos = os_strchr(param, ' ');
  5390. op_class = pos ? atoi(pos) : 81;
  5391. return p2p_set_listen_channel(wpa_s->global->p2p, op_class,
  5392. channel, 1);
  5393. }
  5394. if (os_strcmp(cmd, "ssid_postfix") == 0) {
  5395. return p2p_set_ssid_postfix(wpa_s->global->p2p, (u8 *) param,
  5396. os_strlen(param));
  5397. }
  5398. if (os_strcmp(cmd, "noa") == 0) {
  5399. char *pos;
  5400. int count, start, duration;
  5401. /* GO NoA parameters: count,start_offset(ms),duration(ms) */
  5402. count = atoi(param);
  5403. pos = os_strchr(param, ',');
  5404. if (pos == NULL)
  5405. return -1;
  5406. pos++;
  5407. start = atoi(pos);
  5408. pos = os_strchr(pos, ',');
  5409. if (pos == NULL)
  5410. return -1;
  5411. pos++;
  5412. duration = atoi(pos);
  5413. if (count < 0 || count > 255 || start < 0 || duration < 0)
  5414. return -1;
  5415. if (count == 0 && duration > 0)
  5416. return -1;
  5417. wpa_printf(MSG_DEBUG, "CTRL_IFACE: P2P_SET GO NoA: count=%d "
  5418. "start=%d duration=%d", count, start, duration);
  5419. return wpas_p2p_set_noa(wpa_s, count, start, duration);
  5420. }
  5421. if (os_strcmp(cmd, "ps") == 0)
  5422. return wpa_drv_set_p2p_powersave(wpa_s, atoi(param), -1, -1);
  5423. if (os_strcmp(cmd, "oppps") == 0)
  5424. return wpa_drv_set_p2p_powersave(wpa_s, -1, atoi(param), -1);
  5425. if (os_strcmp(cmd, "ctwindow") == 0)
  5426. return wpa_drv_set_p2p_powersave(wpa_s, -1, -1, atoi(param));
  5427. if (os_strcmp(cmd, "disabled") == 0) {
  5428. wpa_s->global->p2p_disabled = atoi(param);
  5429. wpa_printf(MSG_DEBUG, "P2P functionality %s",
  5430. wpa_s->global->p2p_disabled ?
  5431. "disabled" : "enabled");
  5432. if (wpa_s->global->p2p_disabled) {
  5433. wpas_p2p_stop_find(wpa_s);
  5434. os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN);
  5435. p2p_flush(wpa_s->global->p2p);
  5436. }
  5437. return 0;
  5438. }
  5439. if (os_strcmp(cmd, "conc_pref") == 0) {
  5440. if (os_strcmp(param, "sta") == 0)
  5441. wpa_s->global->conc_pref = WPA_CONC_PREF_STA;
  5442. else if (os_strcmp(param, "p2p") == 0)
  5443. wpa_s->global->conc_pref = WPA_CONC_PREF_P2P;
  5444. else {
  5445. wpa_printf(MSG_INFO, "Invalid conc_pref value");
  5446. return -1;
  5447. }
  5448. wpa_printf(MSG_DEBUG, "Single channel concurrency preference: "
  5449. "%s", param);
  5450. return 0;
  5451. }
  5452. if (os_strcmp(cmd, "force_long_sd") == 0) {
  5453. wpa_s->force_long_sd = atoi(param);
  5454. return 0;
  5455. }
  5456. if (os_strcmp(cmd, "peer_filter") == 0) {
  5457. u8 addr[ETH_ALEN];
  5458. if (hwaddr_aton(param, addr))
  5459. return -1;
  5460. p2p_set_peer_filter(wpa_s->global->p2p, addr);
  5461. return 0;
  5462. }
  5463. if (os_strcmp(cmd, "cross_connect") == 0)
  5464. return wpas_p2p_set_cross_connect(wpa_s, atoi(param));
  5465. if (os_strcmp(cmd, "go_apsd") == 0) {
  5466. if (os_strcmp(param, "disable") == 0)
  5467. wpa_s->set_ap_uapsd = 0;
  5468. else {
  5469. wpa_s->set_ap_uapsd = 1;
  5470. wpa_s->ap_uapsd = atoi(param);
  5471. }
  5472. return 0;
  5473. }
  5474. if (os_strcmp(cmd, "client_apsd") == 0) {
  5475. if (os_strcmp(param, "disable") == 0)
  5476. wpa_s->set_sta_uapsd = 0;
  5477. else {
  5478. int be, bk, vi, vo;
  5479. char *pos;
  5480. /* format: BE,BK,VI,VO;max SP Length */
  5481. be = atoi(param);
  5482. pos = os_strchr(param, ',');
  5483. if (pos == NULL)
  5484. return -1;
  5485. pos++;
  5486. bk = atoi(pos);
  5487. pos = os_strchr(pos, ',');
  5488. if (pos == NULL)
  5489. return -1;
  5490. pos++;
  5491. vi = atoi(pos);
  5492. pos = os_strchr(pos, ',');
  5493. if (pos == NULL)
  5494. return -1;
  5495. pos++;
  5496. vo = atoi(pos);
  5497. /* ignore max SP Length for now */
  5498. wpa_s->set_sta_uapsd = 1;
  5499. wpa_s->sta_uapsd = 0;
  5500. if (be)
  5501. wpa_s->sta_uapsd |= BIT(0);
  5502. if (bk)
  5503. wpa_s->sta_uapsd |= BIT(1);
  5504. if (vi)
  5505. wpa_s->sta_uapsd |= BIT(2);
  5506. if (vo)
  5507. wpa_s->sta_uapsd |= BIT(3);
  5508. }
  5509. return 0;
  5510. }
  5511. if (os_strcmp(cmd, "disallow_freq") == 0)
  5512. return p2p_ctrl_disallow_freq(wpa_s, param);
  5513. if (os_strcmp(cmd, "disc_int") == 0) {
  5514. int min_disc_int, max_disc_int, max_disc_tu;
  5515. char *pos;
  5516. pos = param;
  5517. min_disc_int = atoi(pos);
  5518. pos = os_strchr(pos, ' ');
  5519. if (pos == NULL)
  5520. return -1;
  5521. *pos++ = '\0';
  5522. max_disc_int = atoi(pos);
  5523. pos = os_strchr(pos, ' ');
  5524. if (pos == NULL)
  5525. return -1;
  5526. *pos++ = '\0';
  5527. max_disc_tu = atoi(pos);
  5528. return p2p_set_disc_int(wpa_s->global->p2p, min_disc_int,
  5529. max_disc_int, max_disc_tu);
  5530. }
  5531. if (os_strcmp(cmd, "per_sta_psk") == 0) {
  5532. wpa_s->global->p2p_per_sta_psk = !!atoi(param);
  5533. return 0;
  5534. }
  5535. #ifdef CONFIG_WPS_NFC
  5536. if (os_strcmp(cmd, "nfc_tag") == 0)
  5537. return wpas_p2p_nfc_tag_enabled(wpa_s, !!atoi(param));
  5538. #endif /* CONFIG_WPS_NFC */
  5539. if (os_strcmp(cmd, "disable_ip_addr_req") == 0) {
  5540. wpa_s->p2p_disable_ip_addr_req = !!atoi(param);
  5541. return 0;
  5542. }
  5543. if (os_strcmp(cmd, "override_pref_op_chan") == 0) {
  5544. int op_class, chan;
  5545. op_class = atoi(param);
  5546. param = os_strchr(param, ':');
  5547. if (!param)
  5548. return -1;
  5549. param++;
  5550. chan = atoi(param);
  5551. p2p_set_override_pref_op_chan(wpa_s->global->p2p, op_class,
  5552. chan);
  5553. return 0;
  5554. }
  5555. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Unknown P2P_SET field value '%s'",
  5556. cmd);
  5557. return -1;
  5558. }
  5559. static void p2p_ctrl_flush(struct wpa_supplicant *wpa_s)
  5560. {
  5561. os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN);
  5562. wpa_s->force_long_sd = 0;
  5563. wpas_p2p_stop_find(wpa_s);
  5564. wpa_s->parent->p2ps_method_config_any = 0;
  5565. if (wpa_s->global->p2p)
  5566. p2p_flush(wpa_s->global->p2p);
  5567. }
  5568. static int p2p_ctrl_presence_req(struct wpa_supplicant *wpa_s, char *cmd)
  5569. {
  5570. char *pos, *pos2;
  5571. unsigned int dur1 = 0, int1 = 0, dur2 = 0, int2 = 0;
  5572. if (cmd[0]) {
  5573. pos = os_strchr(cmd, ' ');
  5574. if (pos == NULL)
  5575. return -1;
  5576. *pos++ = '\0';
  5577. dur1 = atoi(cmd);
  5578. pos2 = os_strchr(pos, ' ');
  5579. if (pos2)
  5580. *pos2++ = '\0';
  5581. int1 = atoi(pos);
  5582. } else
  5583. pos2 = NULL;
  5584. if (pos2) {
  5585. pos = os_strchr(pos2, ' ');
  5586. if (pos == NULL)
  5587. return -1;
  5588. *pos++ = '\0';
  5589. dur2 = atoi(pos2);
  5590. int2 = atoi(pos);
  5591. }
  5592. return wpas_p2p_presence_req(wpa_s, dur1, int1, dur2, int2);
  5593. }
  5594. static int p2p_ctrl_ext_listen(struct wpa_supplicant *wpa_s, char *cmd)
  5595. {
  5596. char *pos;
  5597. unsigned int period = 0, interval = 0;
  5598. if (cmd[0]) {
  5599. pos = os_strchr(cmd, ' ');
  5600. if (pos == NULL)
  5601. return -1;
  5602. *pos++ = '\0';
  5603. period = atoi(cmd);
  5604. interval = atoi(pos);
  5605. }
  5606. return wpas_p2p_ext_listen(wpa_s, period, interval);
  5607. }
  5608. static int p2p_ctrl_remove_client(struct wpa_supplicant *wpa_s, const char *cmd)
  5609. {
  5610. const char *pos;
  5611. u8 peer[ETH_ALEN];
  5612. int iface_addr = 0;
  5613. pos = cmd;
  5614. if (os_strncmp(pos, "iface=", 6) == 0) {
  5615. iface_addr = 1;
  5616. pos += 6;
  5617. }
  5618. if (hwaddr_aton(pos, peer))
  5619. return -1;
  5620. wpas_p2p_remove_client(wpa_s, peer, iface_addr);
  5621. return 0;
  5622. }
  5623. static int p2p_ctrl_iface_p2p_lo_start(struct wpa_supplicant *wpa_s, char *cmd)
  5624. {
  5625. int freq = 0, period = 0, interval = 0, count = 0;
  5626. if (sscanf(cmd, "%d %d %d %d", &freq, &period, &interval, &count) != 4)
  5627. {
  5628. wpa_printf(MSG_DEBUG,
  5629. "CTRL: Invalid P2P LO Start parameter: '%s'", cmd);
  5630. return -1;
  5631. }
  5632. return wpas_p2p_lo_start(wpa_s, freq, period, interval, count);
  5633. }
  5634. #endif /* CONFIG_P2P */
  5635. static int * freq_range_to_channel_list(struct wpa_supplicant *wpa_s, char *val)
  5636. {
  5637. struct wpa_freq_range_list ranges;
  5638. int *freqs = NULL;
  5639. struct hostapd_hw_modes *mode;
  5640. u16 i;
  5641. if (wpa_s->hw.modes == NULL)
  5642. return NULL;
  5643. os_memset(&ranges, 0, sizeof(ranges));
  5644. if (freq_range_list_parse(&ranges, val) < 0)
  5645. return NULL;
  5646. for (i = 0; i < wpa_s->hw.num_modes; i++) {
  5647. int j;
  5648. mode = &wpa_s->hw.modes[i];
  5649. for (j = 0; j < mode->num_channels; j++) {
  5650. unsigned int freq;
  5651. if (mode->channels[j].flag & HOSTAPD_CHAN_DISABLED)
  5652. continue;
  5653. freq = mode->channels[j].freq;
  5654. if (!freq_range_list_includes(&ranges, freq))
  5655. continue;
  5656. int_array_add_unique(&freqs, freq);
  5657. }
  5658. }
  5659. os_free(ranges.range);
  5660. return freqs;
  5661. }
  5662. #ifdef CONFIG_INTERWORKING
  5663. static int ctrl_interworking_select(struct wpa_supplicant *wpa_s, char *param)
  5664. {
  5665. int auto_sel = 0;
  5666. int *freqs = NULL;
  5667. if (param) {
  5668. char *pos;
  5669. auto_sel = os_strstr(param, "auto") != NULL;
  5670. pos = os_strstr(param, "freq=");
  5671. if (pos) {
  5672. freqs = freq_range_to_channel_list(wpa_s, pos + 5);
  5673. if (freqs == NULL)
  5674. return -1;
  5675. }
  5676. }
  5677. return interworking_select(wpa_s, auto_sel, freqs);
  5678. }
  5679. static int ctrl_interworking_connect(struct wpa_supplicant *wpa_s, char *dst,
  5680. int only_add)
  5681. {
  5682. u8 bssid[ETH_ALEN];
  5683. struct wpa_bss *bss;
  5684. if (hwaddr_aton(dst, bssid)) {
  5685. wpa_printf(MSG_DEBUG, "Invalid BSSID '%s'", dst);
  5686. return -1;
  5687. }
  5688. bss = wpa_bss_get_bssid(wpa_s, bssid);
  5689. if (bss == NULL) {
  5690. wpa_printf(MSG_DEBUG, "Could not find BSS " MACSTR,
  5691. MAC2STR(bssid));
  5692. return -1;
  5693. }
  5694. if (bss->ssid_len == 0) {
  5695. int found = 0;
  5696. wpa_printf(MSG_DEBUG, "Selected BSS entry for " MACSTR
  5697. " does not have SSID information", MAC2STR(bssid));
  5698. dl_list_for_each_reverse(bss, &wpa_s->bss, struct wpa_bss,
  5699. list) {
  5700. if (os_memcmp(bss->bssid, bssid, ETH_ALEN) == 0 &&
  5701. bss->ssid_len > 0) {
  5702. found = 1;
  5703. break;
  5704. }
  5705. }
  5706. if (!found)
  5707. return -1;
  5708. wpa_printf(MSG_DEBUG,
  5709. "Found another matching BSS entry with SSID");
  5710. }
  5711. return interworking_connect(wpa_s, bss, only_add);
  5712. }
  5713. static int get_anqp(struct wpa_supplicant *wpa_s, char *dst)
  5714. {
  5715. u8 dst_addr[ETH_ALEN];
  5716. int used;
  5717. char *pos;
  5718. #define MAX_ANQP_INFO_ID 100
  5719. u16 id[MAX_ANQP_INFO_ID];
  5720. size_t num_id = 0;
  5721. u32 subtypes = 0;
  5722. u32 mbo_subtypes = 0;
  5723. used = hwaddr_aton2(dst, dst_addr);
  5724. if (used < 0)
  5725. return -1;
  5726. pos = dst + used;
  5727. if (*pos == ' ')
  5728. pos++;
  5729. while (num_id < MAX_ANQP_INFO_ID) {
  5730. if (os_strncmp(pos, "hs20:", 5) == 0) {
  5731. #ifdef CONFIG_HS20
  5732. int num = atoi(pos + 5);
  5733. if (num <= 0 || num > 31)
  5734. return -1;
  5735. subtypes |= BIT(num);
  5736. #else /* CONFIG_HS20 */
  5737. return -1;
  5738. #endif /* CONFIG_HS20 */
  5739. } else if (os_strncmp(pos, "mbo:", 4) == 0) {
  5740. #ifdef CONFIG_MBO
  5741. int num = atoi(pos + 4);
  5742. if (num <= 0 || num > MAX_MBO_ANQP_SUBTYPE)
  5743. return -1;
  5744. mbo_subtypes |= BIT(num);
  5745. #else /* CONFIG_MBO */
  5746. return -1;
  5747. #endif /* CONFIG_MBO */
  5748. } else {
  5749. id[num_id] = atoi(pos);
  5750. if (id[num_id])
  5751. num_id++;
  5752. }
  5753. pos = os_strchr(pos + 1, ',');
  5754. if (pos == NULL)
  5755. break;
  5756. pos++;
  5757. }
  5758. if (num_id == 0 && !subtypes && !mbo_subtypes)
  5759. return -1;
  5760. return anqp_send_req(wpa_s, dst_addr, id, num_id, subtypes,
  5761. mbo_subtypes);
  5762. }
  5763. static int gas_request(struct wpa_supplicant *wpa_s, char *cmd)
  5764. {
  5765. u8 dst_addr[ETH_ALEN];
  5766. struct wpabuf *advproto, *query = NULL;
  5767. int used, ret = -1;
  5768. char *pos, *end;
  5769. size_t len;
  5770. used = hwaddr_aton2(cmd, dst_addr);
  5771. if (used < 0)
  5772. return -1;
  5773. pos = cmd + used;
  5774. while (*pos == ' ')
  5775. pos++;
  5776. /* Advertisement Protocol ID */
  5777. end = os_strchr(pos, ' ');
  5778. if (end)
  5779. len = end - pos;
  5780. else
  5781. len = os_strlen(pos);
  5782. if (len & 0x01)
  5783. return -1;
  5784. len /= 2;
  5785. if (len == 0)
  5786. return -1;
  5787. advproto = wpabuf_alloc(len);
  5788. if (advproto == NULL)
  5789. return -1;
  5790. if (hexstr2bin(pos, wpabuf_put(advproto, len), len) < 0)
  5791. goto fail;
  5792. if (end) {
  5793. /* Optional Query Request */
  5794. pos = end + 1;
  5795. while (*pos == ' ')
  5796. pos++;
  5797. len = os_strlen(pos);
  5798. if (len) {
  5799. if (len & 0x01)
  5800. goto fail;
  5801. len /= 2;
  5802. if (len == 0)
  5803. goto fail;
  5804. query = wpabuf_alloc(len);
  5805. if (query == NULL)
  5806. goto fail;
  5807. if (hexstr2bin(pos, wpabuf_put(query, len), len) < 0)
  5808. goto fail;
  5809. }
  5810. }
  5811. ret = gas_send_request(wpa_s, dst_addr, advproto, query);
  5812. fail:
  5813. wpabuf_free(advproto);
  5814. wpabuf_free(query);
  5815. return ret;
  5816. }
  5817. static int gas_response_get(struct wpa_supplicant *wpa_s, char *cmd, char *buf,
  5818. size_t buflen)
  5819. {
  5820. u8 addr[ETH_ALEN];
  5821. int dialog_token;
  5822. int used;
  5823. char *pos;
  5824. size_t resp_len, start, requested_len;
  5825. struct wpabuf *resp;
  5826. int ret;
  5827. used = hwaddr_aton2(cmd, addr);
  5828. if (used < 0)
  5829. return -1;
  5830. pos = cmd + used;
  5831. while (*pos == ' ')
  5832. pos++;
  5833. dialog_token = atoi(pos);
  5834. if (wpa_s->last_gas_resp &&
  5835. os_memcmp(addr, wpa_s->last_gas_addr, ETH_ALEN) == 0 &&
  5836. dialog_token == wpa_s->last_gas_dialog_token)
  5837. resp = wpa_s->last_gas_resp;
  5838. else if (wpa_s->prev_gas_resp &&
  5839. os_memcmp(addr, wpa_s->prev_gas_addr, ETH_ALEN) == 0 &&
  5840. dialog_token == wpa_s->prev_gas_dialog_token)
  5841. resp = wpa_s->prev_gas_resp;
  5842. else
  5843. return -1;
  5844. resp_len = wpabuf_len(resp);
  5845. start = 0;
  5846. requested_len = resp_len;
  5847. pos = os_strchr(pos, ' ');
  5848. if (pos) {
  5849. start = atoi(pos);
  5850. if (start > resp_len)
  5851. return os_snprintf(buf, buflen, "FAIL-Invalid range");
  5852. pos = os_strchr(pos, ',');
  5853. if (pos == NULL)
  5854. return -1;
  5855. pos++;
  5856. requested_len = atoi(pos);
  5857. if (start + requested_len > resp_len)
  5858. return os_snprintf(buf, buflen, "FAIL-Invalid range");
  5859. }
  5860. if (requested_len * 2 + 1 > buflen)
  5861. return os_snprintf(buf, buflen, "FAIL-Too long response");
  5862. ret = wpa_snprintf_hex(buf, buflen, wpabuf_head_u8(resp) + start,
  5863. requested_len);
  5864. if (start + requested_len == resp_len) {
  5865. /*
  5866. * Free memory by dropping the response after it has been
  5867. * fetched.
  5868. */
  5869. if (resp == wpa_s->prev_gas_resp) {
  5870. wpabuf_free(wpa_s->prev_gas_resp);
  5871. wpa_s->prev_gas_resp = NULL;
  5872. } else {
  5873. wpabuf_free(wpa_s->last_gas_resp);
  5874. wpa_s->last_gas_resp = NULL;
  5875. }
  5876. }
  5877. return ret;
  5878. }
  5879. #endif /* CONFIG_INTERWORKING */
  5880. #ifdef CONFIG_HS20
  5881. static int get_hs20_anqp(struct wpa_supplicant *wpa_s, char *dst)
  5882. {
  5883. u8 dst_addr[ETH_ALEN];
  5884. int used;
  5885. char *pos;
  5886. u32 subtypes = 0;
  5887. used = hwaddr_aton2(dst, dst_addr);
  5888. if (used < 0)
  5889. return -1;
  5890. pos = dst + used;
  5891. if (*pos == ' ')
  5892. pos++;
  5893. for (;;) {
  5894. int num = atoi(pos);
  5895. if (num <= 0 || num > 31)
  5896. return -1;
  5897. subtypes |= BIT(num);
  5898. pos = os_strchr(pos + 1, ',');
  5899. if (pos == NULL)
  5900. break;
  5901. pos++;
  5902. }
  5903. if (subtypes == 0)
  5904. return -1;
  5905. return hs20_anqp_send_req(wpa_s, dst_addr, subtypes, NULL, 0, 0);
  5906. }
  5907. static int hs20_nai_home_realm_list(struct wpa_supplicant *wpa_s,
  5908. const u8 *addr, const char *realm)
  5909. {
  5910. u8 *buf;
  5911. size_t rlen, len;
  5912. int ret;
  5913. rlen = os_strlen(realm);
  5914. len = 3 + rlen;
  5915. buf = os_malloc(len);
  5916. if (buf == NULL)
  5917. return -1;
  5918. buf[0] = 1; /* NAI Home Realm Count */
  5919. buf[1] = 0; /* Formatted in accordance with RFC 4282 */
  5920. buf[2] = rlen;
  5921. os_memcpy(buf + 3, realm, rlen);
  5922. ret = hs20_anqp_send_req(wpa_s, addr,
  5923. BIT(HS20_STYPE_NAI_HOME_REALM_QUERY),
  5924. buf, len, 0);
  5925. os_free(buf);
  5926. return ret;
  5927. }
  5928. static int hs20_get_nai_home_realm_list(struct wpa_supplicant *wpa_s,
  5929. char *dst)
  5930. {
  5931. struct wpa_cred *cred = wpa_s->conf->cred;
  5932. u8 dst_addr[ETH_ALEN];
  5933. int used;
  5934. u8 *buf;
  5935. size_t len;
  5936. int ret;
  5937. used = hwaddr_aton2(dst, dst_addr);
  5938. if (used < 0)
  5939. return -1;
  5940. while (dst[used] == ' ')
  5941. used++;
  5942. if (os_strncmp(dst + used, "realm=", 6) == 0)
  5943. return hs20_nai_home_realm_list(wpa_s, dst_addr,
  5944. dst + used + 6);
  5945. len = os_strlen(dst + used);
  5946. if (len == 0 && cred && cred->realm)
  5947. return hs20_nai_home_realm_list(wpa_s, dst_addr, cred->realm);
  5948. if (len & 1)
  5949. return -1;
  5950. len /= 2;
  5951. buf = os_malloc(len);
  5952. if (buf == NULL)
  5953. return -1;
  5954. if (hexstr2bin(dst + used, buf, len) < 0) {
  5955. os_free(buf);
  5956. return -1;
  5957. }
  5958. ret = hs20_anqp_send_req(wpa_s, dst_addr,
  5959. BIT(HS20_STYPE_NAI_HOME_REALM_QUERY),
  5960. buf, len, 0);
  5961. os_free(buf);
  5962. return ret;
  5963. }
  5964. static int get_hs20_icon(struct wpa_supplicant *wpa_s, char *cmd, char *reply,
  5965. int buflen)
  5966. {
  5967. u8 dst_addr[ETH_ALEN];
  5968. int used;
  5969. char *ctx = NULL, *icon, *poffset, *psize;
  5970. used = hwaddr_aton2(cmd, dst_addr);
  5971. if (used < 0)
  5972. return -1;
  5973. cmd += used;
  5974. icon = str_token(cmd, " ", &ctx);
  5975. poffset = str_token(cmd, " ", &ctx);
  5976. psize = str_token(cmd, " ", &ctx);
  5977. if (!icon || !poffset || !psize)
  5978. return -1;
  5979. wpa_s->fetch_osu_icon_in_progress = 0;
  5980. return hs20_get_icon(wpa_s, dst_addr, icon, atoi(poffset), atoi(psize),
  5981. reply, buflen);
  5982. }
  5983. static int del_hs20_icon(struct wpa_supplicant *wpa_s, char *cmd)
  5984. {
  5985. u8 dst_addr[ETH_ALEN];
  5986. int used;
  5987. char *icon;
  5988. if (!cmd[0])
  5989. return hs20_del_icon(wpa_s, NULL, NULL);
  5990. used = hwaddr_aton2(cmd, dst_addr);
  5991. if (used < 0)
  5992. return -1;
  5993. while (cmd[used] == ' ')
  5994. used++;
  5995. icon = cmd[used] ? &cmd[used] : NULL;
  5996. return hs20_del_icon(wpa_s, dst_addr, icon);
  5997. }
  5998. static int hs20_icon_request(struct wpa_supplicant *wpa_s, char *cmd, int inmem)
  5999. {
  6000. u8 dst_addr[ETH_ALEN];
  6001. int used;
  6002. char *icon;
  6003. used = hwaddr_aton2(cmd, dst_addr);
  6004. if (used < 0)
  6005. return -1;
  6006. while (cmd[used] == ' ')
  6007. used++;
  6008. icon = &cmd[used];
  6009. wpa_s->fetch_osu_icon_in_progress = 0;
  6010. return hs20_anqp_send_req(wpa_s, dst_addr, BIT(HS20_STYPE_ICON_REQUEST),
  6011. (u8 *) icon, os_strlen(icon), inmem);
  6012. }
  6013. #endif /* CONFIG_HS20 */
  6014. #ifdef CONFIG_AUTOSCAN
  6015. static int wpa_supplicant_ctrl_iface_autoscan(struct wpa_supplicant *wpa_s,
  6016. char *cmd)
  6017. {
  6018. enum wpa_states state = wpa_s->wpa_state;
  6019. char *new_params = NULL;
  6020. if (os_strlen(cmd) > 0) {
  6021. new_params = os_strdup(cmd);
  6022. if (new_params == NULL)
  6023. return -1;
  6024. }
  6025. os_free(wpa_s->conf->autoscan);
  6026. wpa_s->conf->autoscan = new_params;
  6027. if (wpa_s->conf->autoscan == NULL)
  6028. autoscan_deinit(wpa_s);
  6029. else if (state == WPA_DISCONNECTED || state == WPA_INACTIVE)
  6030. autoscan_init(wpa_s, 1);
  6031. else if (state == WPA_SCANNING)
  6032. wpa_supplicant_reinit_autoscan(wpa_s);
  6033. else
  6034. wpa_printf(MSG_DEBUG, "No autoscan update in state %s",
  6035. wpa_supplicant_state_txt(state));
  6036. return 0;
  6037. }
  6038. #endif /* CONFIG_AUTOSCAN */
  6039. #ifdef CONFIG_WNM
  6040. static int wpas_ctrl_iface_wnm_sleep(struct wpa_supplicant *wpa_s, char *cmd)
  6041. {
  6042. int enter;
  6043. int intval = 0;
  6044. char *pos;
  6045. int ret;
  6046. struct wpabuf *tfs_req = NULL;
  6047. if (os_strncmp(cmd, "enter", 5) == 0)
  6048. enter = 1;
  6049. else if (os_strncmp(cmd, "exit", 4) == 0)
  6050. enter = 0;
  6051. else
  6052. return -1;
  6053. pos = os_strstr(cmd, " interval=");
  6054. if (pos)
  6055. intval = atoi(pos + 10);
  6056. pos = os_strstr(cmd, " tfs_req=");
  6057. if (pos) {
  6058. char *end;
  6059. size_t len;
  6060. pos += 9;
  6061. end = os_strchr(pos, ' ');
  6062. if (end)
  6063. len = end - pos;
  6064. else
  6065. len = os_strlen(pos);
  6066. if (len & 1)
  6067. return -1;
  6068. len /= 2;
  6069. tfs_req = wpabuf_alloc(len);
  6070. if (tfs_req == NULL)
  6071. return -1;
  6072. if (hexstr2bin(pos, wpabuf_put(tfs_req, len), len) < 0) {
  6073. wpabuf_free(tfs_req);
  6074. return -1;
  6075. }
  6076. }
  6077. ret = ieee802_11_send_wnmsleep_req(wpa_s, enter ? WNM_SLEEP_MODE_ENTER :
  6078. WNM_SLEEP_MODE_EXIT, intval,
  6079. tfs_req);
  6080. wpabuf_free(tfs_req);
  6081. return ret;
  6082. }
  6083. static int wpas_ctrl_iface_wnm_bss_query(struct wpa_supplicant *wpa_s, char *cmd)
  6084. {
  6085. int query_reason, list = 0;
  6086. char *btm_candidates = NULL;
  6087. query_reason = atoi(cmd);
  6088. cmd = os_strchr(cmd, ' ');
  6089. if (cmd) {
  6090. if (os_strncmp(cmd, " list", 5) == 0)
  6091. list = 1;
  6092. else
  6093. btm_candidates = cmd;
  6094. }
  6095. wpa_printf(MSG_DEBUG,
  6096. "CTRL_IFACE: WNM_BSS_QUERY query_reason=%d%s",
  6097. query_reason, list ? " candidate list" : "");
  6098. return wnm_send_bss_transition_mgmt_query(wpa_s, query_reason,
  6099. btm_candidates,
  6100. list);
  6101. }
  6102. #endif /* CONFIG_WNM */
  6103. static int wpa_supplicant_signal_poll(struct wpa_supplicant *wpa_s, char *buf,
  6104. size_t buflen)
  6105. {
  6106. struct wpa_signal_info si;
  6107. int ret;
  6108. char *pos, *end;
  6109. ret = wpa_drv_signal_poll(wpa_s, &si);
  6110. if (ret)
  6111. return -1;
  6112. pos = buf;
  6113. end = buf + buflen;
  6114. ret = os_snprintf(pos, end - pos, "RSSI=%d\nLINKSPEED=%d\n"
  6115. "NOISE=%d\nFREQUENCY=%u\n",
  6116. si.current_signal, si.current_txrate / 1000,
  6117. si.current_noise, si.frequency);
  6118. if (os_snprintf_error(end - pos, ret))
  6119. return -1;
  6120. pos += ret;
  6121. if (si.chanwidth != CHAN_WIDTH_UNKNOWN) {
  6122. ret = os_snprintf(pos, end - pos, "WIDTH=%s\n",
  6123. channel_width_to_string(si.chanwidth));
  6124. if (os_snprintf_error(end - pos, ret))
  6125. return -1;
  6126. pos += ret;
  6127. }
  6128. if (si.center_frq1 > 0 && si.center_frq2 > 0) {
  6129. ret = os_snprintf(pos, end - pos,
  6130. "CENTER_FRQ1=%d\nCENTER_FRQ2=%d\n",
  6131. si.center_frq1, si.center_frq2);
  6132. if (os_snprintf_error(end - pos, ret))
  6133. return -1;
  6134. pos += ret;
  6135. }
  6136. if (si.avg_signal) {
  6137. ret = os_snprintf(pos, end - pos,
  6138. "AVG_RSSI=%d\n", si.avg_signal);
  6139. if (os_snprintf_error(end - pos, ret))
  6140. return -1;
  6141. pos += ret;
  6142. }
  6143. if (si.avg_beacon_signal) {
  6144. ret = os_snprintf(pos, end - pos,
  6145. "AVG_BEACON_RSSI=%d\n", si.avg_beacon_signal);
  6146. if (os_snprintf_error(end - pos, ret))
  6147. return -1;
  6148. pos += ret;
  6149. }
  6150. return pos - buf;
  6151. }
  6152. static int wpas_ctrl_iface_signal_monitor(struct wpa_supplicant *wpa_s,
  6153. const char *cmd)
  6154. {
  6155. const char *pos;
  6156. int threshold = 0;
  6157. int hysteresis = 0;
  6158. if (wpa_s->bgscan && wpa_s->bgscan_priv) {
  6159. wpa_printf(MSG_DEBUG,
  6160. "Reject SIGNAL_MONITOR command - bgscan is active");
  6161. return -1;
  6162. }
  6163. pos = os_strstr(cmd, "THRESHOLD=");
  6164. if (pos)
  6165. threshold = atoi(pos + 10);
  6166. pos = os_strstr(cmd, "HYSTERESIS=");
  6167. if (pos)
  6168. hysteresis = atoi(pos + 11);
  6169. return wpa_drv_signal_monitor(wpa_s, threshold, hysteresis);
  6170. }
  6171. #ifdef CONFIG_TESTING_OPTIONS
  6172. int wpas_ctrl_iface_get_pref_freq_list_override(struct wpa_supplicant *wpa_s,
  6173. enum wpa_driver_if_type if_type,
  6174. unsigned int *num,
  6175. unsigned int *freq_list)
  6176. {
  6177. char *pos = wpa_s->get_pref_freq_list_override;
  6178. char *end;
  6179. unsigned int count = 0;
  6180. /* Override string format:
  6181. * <if_type1>:<freq1>,<freq2>,... <if_type2>:... */
  6182. while (pos) {
  6183. if (atoi(pos) == (int) if_type)
  6184. break;
  6185. pos = os_strchr(pos, ' ');
  6186. if (pos)
  6187. pos++;
  6188. }
  6189. if (!pos)
  6190. return -1;
  6191. pos = os_strchr(pos, ':');
  6192. if (!pos)
  6193. return -1;
  6194. pos++;
  6195. end = os_strchr(pos, ' ');
  6196. while (pos && (!end || pos < end) && count < *num) {
  6197. freq_list[count++] = atoi(pos);
  6198. pos = os_strchr(pos, ',');
  6199. if (pos)
  6200. pos++;
  6201. }
  6202. *num = count;
  6203. return 0;
  6204. }
  6205. #endif /* CONFIG_TESTING_OPTIONS */
  6206. static int wpas_ctrl_iface_get_pref_freq_list(
  6207. struct wpa_supplicant *wpa_s, char *cmd, char *buf, size_t buflen)
  6208. {
  6209. unsigned int freq_list[100], num = 100, i;
  6210. int ret;
  6211. enum wpa_driver_if_type iface_type;
  6212. char *pos, *end;
  6213. pos = buf;
  6214. end = buf + buflen;
  6215. /* buf: "<interface_type>" */
  6216. if (os_strcmp(cmd, "STATION") == 0)
  6217. iface_type = WPA_IF_STATION;
  6218. else if (os_strcmp(cmd, "AP") == 0)
  6219. iface_type = WPA_IF_AP_BSS;
  6220. else if (os_strcmp(cmd, "P2P_GO") == 0)
  6221. iface_type = WPA_IF_P2P_GO;
  6222. else if (os_strcmp(cmd, "P2P_CLIENT") == 0)
  6223. iface_type = WPA_IF_P2P_CLIENT;
  6224. else if (os_strcmp(cmd, "IBSS") == 0)
  6225. iface_type = WPA_IF_IBSS;
  6226. else if (os_strcmp(cmd, "TDLS") == 0)
  6227. iface_type = WPA_IF_TDLS;
  6228. else
  6229. return -1;
  6230. wpa_printf(MSG_DEBUG,
  6231. "CTRL_IFACE: GET_PREF_FREQ_LIST iface_type=%d (%s)",
  6232. iface_type, buf);
  6233. ret = wpa_drv_get_pref_freq_list(wpa_s, iface_type, &num, freq_list);
  6234. if (ret)
  6235. return -1;
  6236. for (i = 0; i < num; i++) {
  6237. ret = os_snprintf(pos, end - pos, "%s%u",
  6238. i > 0 ? "," : "", freq_list[i]);
  6239. if (os_snprintf_error(end - pos, ret))
  6240. return -1;
  6241. pos += ret;
  6242. }
  6243. return pos - buf;
  6244. }
  6245. static int wpas_ctrl_iface_driver_flags(struct wpa_supplicant *wpa_s,
  6246. char *buf, size_t buflen)
  6247. {
  6248. int ret, i;
  6249. char *pos, *end;
  6250. ret = os_snprintf(buf, buflen, "%016llX:\n",
  6251. (long long unsigned) wpa_s->drv_flags);
  6252. if (os_snprintf_error(buflen, ret))
  6253. return -1;
  6254. pos = buf + ret;
  6255. end = buf + buflen;
  6256. for (i = 0; i < 64; i++) {
  6257. if (wpa_s->drv_flags & (1LLU << i)) {
  6258. ret = os_snprintf(pos, end - pos, "%s\n",
  6259. driver_flag_to_string(1LLU << i));
  6260. if (os_snprintf_error(end - pos, ret))
  6261. return -1;
  6262. pos += ret;
  6263. }
  6264. }
  6265. return pos - buf;
  6266. }
  6267. static int wpa_supplicant_pktcnt_poll(struct wpa_supplicant *wpa_s, char *buf,
  6268. size_t buflen)
  6269. {
  6270. struct hostap_sta_driver_data sta;
  6271. int ret;
  6272. ret = wpa_drv_pktcnt_poll(wpa_s, &sta);
  6273. if (ret)
  6274. return -1;
  6275. ret = os_snprintf(buf, buflen, "TXGOOD=%lu\nTXBAD=%lu\nRXGOOD=%lu\n",
  6276. sta.tx_packets, sta.tx_retry_failed, sta.rx_packets);
  6277. if (os_snprintf_error(buflen, ret))
  6278. return -1;
  6279. return ret;
  6280. }
  6281. #ifdef ANDROID
  6282. static int wpa_supplicant_driver_cmd(struct wpa_supplicant *wpa_s, char *cmd,
  6283. char *buf, size_t buflen)
  6284. {
  6285. int ret;
  6286. ret = wpa_drv_driver_cmd(wpa_s, cmd, buf, buflen);
  6287. if (ret == 0) {
  6288. if (os_strncasecmp(cmd, "COUNTRY", 7) == 0) {
  6289. struct p2p_data *p2p = wpa_s->global->p2p;
  6290. if (p2p) {
  6291. char country[3];
  6292. country[0] = cmd[8];
  6293. country[1] = cmd[9];
  6294. country[2] = 0x04;
  6295. p2p_set_country(p2p, country);
  6296. }
  6297. }
  6298. ret = os_snprintf(buf, buflen, "%s\n", "OK");
  6299. if (os_snprintf_error(buflen, ret))
  6300. ret = -1;
  6301. }
  6302. return ret;
  6303. }
  6304. #endif /* ANDROID */
  6305. static int wpa_supplicant_vendor_cmd(struct wpa_supplicant *wpa_s, char *cmd,
  6306. char *buf, size_t buflen)
  6307. {
  6308. int ret;
  6309. char *pos;
  6310. u8 *data = NULL;
  6311. unsigned int vendor_id, subcmd;
  6312. struct wpabuf *reply;
  6313. size_t data_len = 0;
  6314. /* cmd: <vendor id> <subcommand id> [<hex formatted data>] */
  6315. vendor_id = strtoul(cmd, &pos, 16);
  6316. if (!isblank((unsigned char) *pos))
  6317. return -EINVAL;
  6318. subcmd = strtoul(pos, &pos, 10);
  6319. if (*pos != '\0') {
  6320. if (!isblank((unsigned char) *pos++))
  6321. return -EINVAL;
  6322. data_len = os_strlen(pos);
  6323. }
  6324. if (data_len) {
  6325. data_len /= 2;
  6326. data = os_malloc(data_len);
  6327. if (!data)
  6328. return -1;
  6329. if (hexstr2bin(pos, data, data_len)) {
  6330. wpa_printf(MSG_DEBUG,
  6331. "Vendor command: wrong parameter format");
  6332. os_free(data);
  6333. return -EINVAL;
  6334. }
  6335. }
  6336. reply = wpabuf_alloc((buflen - 1) / 2);
  6337. if (!reply) {
  6338. os_free(data);
  6339. return -1;
  6340. }
  6341. ret = wpa_drv_vendor_cmd(wpa_s, vendor_id, subcmd, data, data_len,
  6342. reply);
  6343. if (ret == 0)
  6344. ret = wpa_snprintf_hex(buf, buflen, wpabuf_head_u8(reply),
  6345. wpabuf_len(reply));
  6346. wpabuf_free(reply);
  6347. os_free(data);
  6348. return ret;
  6349. }
  6350. static void wpa_supplicant_ctrl_iface_flush(struct wpa_supplicant *wpa_s)
  6351. {
  6352. #ifdef CONFIG_P2P
  6353. struct wpa_supplicant *p2p_wpa_s = wpa_s->global->p2p_init_wpa_s ?
  6354. wpa_s->global->p2p_init_wpa_s : wpa_s;
  6355. #endif /* CONFIG_P2P */
  6356. wpa_dbg(wpa_s, MSG_DEBUG, "Flush all wpa_supplicant state");
  6357. if (wpas_abort_ongoing_scan(wpa_s) == 0)
  6358. wpa_s->ignore_post_flush_scan_res = 1;
  6359. if (wpa_s->wpa_state >= WPA_AUTHENTICATING) {
  6360. /*
  6361. * Avoid possible auto connect re-connection on getting
  6362. * disconnected due to state flush.
  6363. */
  6364. wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
  6365. }
  6366. #ifdef CONFIG_P2P
  6367. wpas_p2p_group_remove(p2p_wpa_s, "*");
  6368. wpas_p2p_cancel(p2p_wpa_s);
  6369. p2p_ctrl_flush(p2p_wpa_s);
  6370. wpas_p2p_service_flush(p2p_wpa_s);
  6371. p2p_wpa_s->global->p2p_disabled = 0;
  6372. p2p_wpa_s->global->p2p_per_sta_psk = 0;
  6373. p2p_wpa_s->conf->num_sec_device_types = 0;
  6374. p2p_wpa_s->p2p_disable_ip_addr_req = 0;
  6375. os_free(p2p_wpa_s->global->p2p_go_avoid_freq.range);
  6376. p2p_wpa_s->global->p2p_go_avoid_freq.range = NULL;
  6377. p2p_wpa_s->global->p2p_go_avoid_freq.num = 0;
  6378. p2p_wpa_s->global->pending_p2ps_group = 0;
  6379. p2p_wpa_s->global->pending_p2ps_group_freq = 0;
  6380. #endif /* CONFIG_P2P */
  6381. #ifdef CONFIG_WPS_TESTING
  6382. wps_version_number = 0x20;
  6383. wps_testing_dummy_cred = 0;
  6384. wps_corrupt_pkhash = 0;
  6385. wps_force_auth_types_in_use = 0;
  6386. wps_force_encr_types_in_use = 0;
  6387. #endif /* CONFIG_WPS_TESTING */
  6388. #ifdef CONFIG_WPS
  6389. wpa_s->wps_fragment_size = 0;
  6390. wpas_wps_cancel(wpa_s);
  6391. wps_registrar_flush(wpa_s->wps->registrar);
  6392. #endif /* CONFIG_WPS */
  6393. wpa_s->after_wps = 0;
  6394. wpa_s->known_wps_freq = 0;
  6395. #ifdef CONFIG_DPP
  6396. wpas_dpp_deinit(wpa_s);
  6397. #endif /* CONFIG_DPP */
  6398. #ifdef CONFIG_TDLS
  6399. #ifdef CONFIG_TDLS_TESTING
  6400. tdls_testing = 0;
  6401. #endif /* CONFIG_TDLS_TESTING */
  6402. wpa_drv_tdls_oper(wpa_s, TDLS_ENABLE, NULL);
  6403. wpa_tdls_enable(wpa_s->wpa, 1);
  6404. #endif /* CONFIG_TDLS */
  6405. eloop_cancel_timeout(wpa_supplicant_stop_countermeasures, wpa_s, NULL);
  6406. wpa_supplicant_stop_countermeasures(wpa_s, NULL);
  6407. wpa_s->no_keep_alive = 0;
  6408. wpa_s->own_disconnect_req = 0;
  6409. os_free(wpa_s->disallow_aps_bssid);
  6410. wpa_s->disallow_aps_bssid = NULL;
  6411. wpa_s->disallow_aps_bssid_count = 0;
  6412. os_free(wpa_s->disallow_aps_ssid);
  6413. wpa_s->disallow_aps_ssid = NULL;
  6414. wpa_s->disallow_aps_ssid_count = 0;
  6415. wpa_s->set_sta_uapsd = 0;
  6416. wpa_s->sta_uapsd = 0;
  6417. wpa_drv_radio_disable(wpa_s, 0);
  6418. wpa_blacklist_clear(wpa_s);
  6419. wpa_s->extra_blacklist_count = 0;
  6420. wpa_supplicant_ctrl_iface_remove_network(wpa_s, "all");
  6421. wpa_supplicant_ctrl_iface_remove_cred(wpa_s, "all");
  6422. wpa_config_flush_blobs(wpa_s->conf);
  6423. wpa_s->conf->auto_interworking = 0;
  6424. wpa_s->conf->okc = 0;
  6425. wpa_sm_pmksa_cache_flush(wpa_s->wpa, NULL);
  6426. rsn_preauth_deinit(wpa_s->wpa);
  6427. wpa_sm_set_param(wpa_s->wpa, RSNA_PMK_LIFETIME, 43200);
  6428. wpa_sm_set_param(wpa_s->wpa, RSNA_PMK_REAUTH_THRESHOLD, 70);
  6429. wpa_sm_set_param(wpa_s->wpa, RSNA_SA_TIMEOUT, 60);
  6430. eapol_sm_notify_logoff(wpa_s->eapol, FALSE);
  6431. radio_remove_works(wpa_s, NULL, 1);
  6432. wpa_s->ext_work_in_progress = 0;
  6433. wpa_s->next_ssid = NULL;
  6434. #ifdef CONFIG_INTERWORKING
  6435. #ifdef CONFIG_HS20
  6436. hs20_cancel_fetch_osu(wpa_s);
  6437. hs20_del_icon(wpa_s, NULL, NULL);
  6438. #endif /* CONFIG_HS20 */
  6439. #endif /* CONFIG_INTERWORKING */
  6440. wpa_s->ext_mgmt_frame_handling = 0;
  6441. wpa_s->ext_eapol_frame_io = 0;
  6442. #ifdef CONFIG_TESTING_OPTIONS
  6443. wpa_s->extra_roc_dur = 0;
  6444. wpa_s->test_failure = WPAS_TEST_FAILURE_NONE;
  6445. wpa_s->p2p_go_csa_on_inv = 0;
  6446. wpa_s->ignore_auth_resp = 0;
  6447. wpa_s->ignore_assoc_disallow = 0;
  6448. wpa_s->reject_btm_req_reason = 0;
  6449. wpa_sm_set_test_assoc_ie(wpa_s->wpa, NULL);
  6450. os_free(wpa_s->get_pref_freq_list_override);
  6451. wpa_s->get_pref_freq_list_override = NULL;
  6452. #endif /* CONFIG_TESTING_OPTIONS */
  6453. wpa_s->disconnected = 0;
  6454. os_free(wpa_s->next_scan_freqs);
  6455. wpa_s->next_scan_freqs = NULL;
  6456. os_free(wpa_s->select_network_scan_freqs);
  6457. wpa_s->select_network_scan_freqs = NULL;
  6458. wpa_bss_flush(wpa_s);
  6459. if (!dl_list_empty(&wpa_s->bss)) {
  6460. wpa_printf(MSG_DEBUG,
  6461. "BSS table not empty after flush: %u entries, current_bss=%p bssid="
  6462. MACSTR " pending_bssid=" MACSTR,
  6463. dl_list_len(&wpa_s->bss), wpa_s->current_bss,
  6464. MAC2STR(wpa_s->bssid),
  6465. MAC2STR(wpa_s->pending_bssid));
  6466. }
  6467. eloop_cancel_timeout(wpas_network_reenabled, wpa_s, NULL);
  6468. wpa_s->wnmsleep_used = 0;
  6469. #ifdef CONFIG_SME
  6470. wpa_s->sme.last_unprot_disconnect.sec = 0;
  6471. #endif /* CONFIG_SME */
  6472. wpabuf_free(wpa_s->ric_ies);
  6473. wpa_s->ric_ies = NULL;
  6474. }
  6475. static int wpas_ctrl_radio_work_show(struct wpa_supplicant *wpa_s,
  6476. char *buf, size_t buflen)
  6477. {
  6478. struct wpa_radio_work *work;
  6479. char *pos, *end;
  6480. struct os_reltime now, diff;
  6481. pos = buf;
  6482. end = buf + buflen;
  6483. os_get_reltime(&now);
  6484. dl_list_for_each(work, &wpa_s->radio->work, struct wpa_radio_work, list)
  6485. {
  6486. int ret;
  6487. os_reltime_sub(&now, &work->time, &diff);
  6488. ret = os_snprintf(pos, end - pos, "%s@%s:%u:%u:%ld.%06ld\n",
  6489. work->type, work->wpa_s->ifname, work->freq,
  6490. work->started, diff.sec, diff.usec);
  6491. if (os_snprintf_error(end - pos, ret))
  6492. break;
  6493. pos += ret;
  6494. }
  6495. return pos - buf;
  6496. }
  6497. static void wpas_ctrl_radio_work_timeout(void *eloop_ctx, void *timeout_ctx)
  6498. {
  6499. struct wpa_radio_work *work = eloop_ctx;
  6500. struct wpa_external_work *ework = work->ctx;
  6501. wpa_dbg(work->wpa_s, MSG_DEBUG,
  6502. "Timing out external radio work %u (%s)",
  6503. ework->id, work->type);
  6504. wpa_msg(work->wpa_s, MSG_INFO, EXT_RADIO_WORK_TIMEOUT "%u", ework->id);
  6505. work->wpa_s->ext_work_in_progress = 0;
  6506. radio_work_done(work);
  6507. os_free(ework);
  6508. }
  6509. static void wpas_ctrl_radio_work_cb(struct wpa_radio_work *work, int deinit)
  6510. {
  6511. struct wpa_external_work *ework = work->ctx;
  6512. if (deinit) {
  6513. if (work->started)
  6514. eloop_cancel_timeout(wpas_ctrl_radio_work_timeout,
  6515. work, NULL);
  6516. /*
  6517. * work->type points to a buffer in ework, so need to replace
  6518. * that here with a fixed string to avoid use of freed memory
  6519. * in debug prints.
  6520. */
  6521. work->type = "freed-ext-work";
  6522. work->ctx = NULL;
  6523. os_free(ework);
  6524. return;
  6525. }
  6526. wpa_dbg(work->wpa_s, MSG_DEBUG, "Starting external radio work %u (%s)",
  6527. ework->id, ework->type);
  6528. wpa_msg(work->wpa_s, MSG_INFO, EXT_RADIO_WORK_START "%u", ework->id);
  6529. work->wpa_s->ext_work_in_progress = 1;
  6530. if (!ework->timeout)
  6531. ework->timeout = 10;
  6532. eloop_register_timeout(ework->timeout, 0, wpas_ctrl_radio_work_timeout,
  6533. work, NULL);
  6534. }
  6535. static int wpas_ctrl_radio_work_add(struct wpa_supplicant *wpa_s, char *cmd,
  6536. char *buf, size_t buflen)
  6537. {
  6538. struct wpa_external_work *ework;
  6539. char *pos, *pos2;
  6540. size_t type_len;
  6541. int ret;
  6542. unsigned int freq = 0;
  6543. /* format: <name> [freq=<MHz>] [timeout=<seconds>] */
  6544. ework = os_zalloc(sizeof(*ework));
  6545. if (ework == NULL)
  6546. return -1;
  6547. pos = os_strchr(cmd, ' ');
  6548. if (pos) {
  6549. type_len = pos - cmd;
  6550. pos++;
  6551. pos2 = os_strstr(pos, "freq=");
  6552. if (pos2)
  6553. freq = atoi(pos2 + 5);
  6554. pos2 = os_strstr(pos, "timeout=");
  6555. if (pos2)
  6556. ework->timeout = atoi(pos2 + 8);
  6557. } else {
  6558. type_len = os_strlen(cmd);
  6559. }
  6560. if (4 + type_len >= sizeof(ework->type))
  6561. type_len = sizeof(ework->type) - 4 - 1;
  6562. os_strlcpy(ework->type, "ext:", sizeof(ework->type));
  6563. os_memcpy(ework->type + 4, cmd, type_len);
  6564. ework->type[4 + type_len] = '\0';
  6565. wpa_s->ext_work_id++;
  6566. if (wpa_s->ext_work_id == 0)
  6567. wpa_s->ext_work_id++;
  6568. ework->id = wpa_s->ext_work_id;
  6569. if (radio_add_work(wpa_s, freq, ework->type, 0, wpas_ctrl_radio_work_cb,
  6570. ework) < 0) {
  6571. os_free(ework);
  6572. return -1;
  6573. }
  6574. ret = os_snprintf(buf, buflen, "%u", ework->id);
  6575. if (os_snprintf_error(buflen, ret))
  6576. return -1;
  6577. return ret;
  6578. }
  6579. static int wpas_ctrl_radio_work_done(struct wpa_supplicant *wpa_s, char *cmd)
  6580. {
  6581. struct wpa_radio_work *work;
  6582. unsigned int id = atoi(cmd);
  6583. dl_list_for_each(work, &wpa_s->radio->work, struct wpa_radio_work, list)
  6584. {
  6585. struct wpa_external_work *ework;
  6586. if (os_strncmp(work->type, "ext:", 4) != 0)
  6587. continue;
  6588. ework = work->ctx;
  6589. if (id && ework->id != id)
  6590. continue;
  6591. wpa_dbg(wpa_s, MSG_DEBUG,
  6592. "Completed external radio work %u (%s)",
  6593. ework->id, ework->type);
  6594. eloop_cancel_timeout(wpas_ctrl_radio_work_timeout, work, NULL);
  6595. wpa_s->ext_work_in_progress = 0;
  6596. radio_work_done(work);
  6597. os_free(ework);
  6598. return 3; /* "OK\n" */
  6599. }
  6600. return -1;
  6601. }
  6602. static int wpas_ctrl_radio_work(struct wpa_supplicant *wpa_s, char *cmd,
  6603. char *buf, size_t buflen)
  6604. {
  6605. if (os_strcmp(cmd, "show") == 0)
  6606. return wpas_ctrl_radio_work_show(wpa_s, buf, buflen);
  6607. if (os_strncmp(cmd, "add ", 4) == 0)
  6608. return wpas_ctrl_radio_work_add(wpa_s, cmd + 4, buf, buflen);
  6609. if (os_strncmp(cmd, "done ", 5) == 0)
  6610. return wpas_ctrl_radio_work_done(wpa_s, cmd + 4);
  6611. return -1;
  6612. }
  6613. void wpas_ctrl_radio_work_flush(struct wpa_supplicant *wpa_s)
  6614. {
  6615. struct wpa_radio_work *work, *tmp;
  6616. if (!wpa_s || !wpa_s->radio)
  6617. return;
  6618. dl_list_for_each_safe(work, tmp, &wpa_s->radio->work,
  6619. struct wpa_radio_work, list) {
  6620. struct wpa_external_work *ework;
  6621. if (os_strncmp(work->type, "ext:", 4) != 0)
  6622. continue;
  6623. ework = work->ctx;
  6624. wpa_dbg(wpa_s, MSG_DEBUG,
  6625. "Flushing%s external radio work %u (%s)",
  6626. work->started ? " started" : "", ework->id,
  6627. ework->type);
  6628. if (work->started)
  6629. eloop_cancel_timeout(wpas_ctrl_radio_work_timeout,
  6630. work, NULL);
  6631. radio_work_done(work);
  6632. os_free(ework);
  6633. }
  6634. }
  6635. static void wpas_ctrl_eapol_response(void *eloop_ctx, void *timeout_ctx)
  6636. {
  6637. struct wpa_supplicant *wpa_s = eloop_ctx;
  6638. eapol_sm_notify_ctrl_response(wpa_s->eapol);
  6639. }
  6640. static int scan_id_list_parse(struct wpa_supplicant *wpa_s, const char *value,
  6641. unsigned int *scan_id_count, int scan_id[])
  6642. {
  6643. const char *pos = value;
  6644. while (pos) {
  6645. if (*pos == ' ' || *pos == '\0')
  6646. break;
  6647. if (*scan_id_count == MAX_SCAN_ID)
  6648. return -1;
  6649. scan_id[(*scan_id_count)++] = atoi(pos);
  6650. pos = os_strchr(pos, ',');
  6651. if (pos)
  6652. pos++;
  6653. }
  6654. return 0;
  6655. }
  6656. static void wpas_ctrl_scan(struct wpa_supplicant *wpa_s, char *params,
  6657. char *reply, int reply_size, int *reply_len)
  6658. {
  6659. char *pos;
  6660. unsigned int manual_scan_passive = 0;
  6661. unsigned int manual_scan_use_id = 0;
  6662. unsigned int manual_scan_only_new = 0;
  6663. unsigned int scan_only = 0;
  6664. unsigned int scan_id_count = 0;
  6665. int scan_id[MAX_SCAN_ID];
  6666. void (*scan_res_handler)(struct wpa_supplicant *wpa_s,
  6667. struct wpa_scan_results *scan_res);
  6668. int *manual_scan_freqs = NULL;
  6669. struct wpa_ssid_value *ssid = NULL, *ns;
  6670. unsigned int ssid_count = 0;
  6671. if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED) {
  6672. *reply_len = -1;
  6673. return;
  6674. }
  6675. if (radio_work_pending(wpa_s, "scan")) {
  6676. wpa_printf(MSG_DEBUG,
  6677. "Pending scan scheduled - reject new request");
  6678. *reply_len = os_snprintf(reply, reply_size, "FAIL-BUSY\n");
  6679. return;
  6680. }
  6681. #ifdef CONFIG_INTERWORKING
  6682. if (wpa_s->fetch_anqp_in_progress || wpa_s->network_select) {
  6683. wpa_printf(MSG_DEBUG,
  6684. "Interworking select in progress - reject new scan");
  6685. *reply_len = os_snprintf(reply, reply_size, "FAIL-BUSY\n");
  6686. return;
  6687. }
  6688. #endif /* CONFIG_INTERWORKING */
  6689. if (params) {
  6690. if (os_strncasecmp(params, "TYPE=ONLY", 9) == 0)
  6691. scan_only = 1;
  6692. pos = os_strstr(params, "freq=");
  6693. if (pos) {
  6694. manual_scan_freqs = freq_range_to_channel_list(wpa_s,
  6695. pos + 5);
  6696. if (manual_scan_freqs == NULL) {
  6697. *reply_len = -1;
  6698. goto done;
  6699. }
  6700. }
  6701. pos = os_strstr(params, "passive=");
  6702. if (pos)
  6703. manual_scan_passive = !!atoi(pos + 8);
  6704. pos = os_strstr(params, "use_id=");
  6705. if (pos)
  6706. manual_scan_use_id = atoi(pos + 7);
  6707. pos = os_strstr(params, "only_new=1");
  6708. if (pos)
  6709. manual_scan_only_new = 1;
  6710. pos = os_strstr(params, "scan_id=");
  6711. if (pos && scan_id_list_parse(wpa_s, pos + 8, &scan_id_count,
  6712. scan_id) < 0) {
  6713. *reply_len = -1;
  6714. goto done;
  6715. }
  6716. pos = params;
  6717. while (pos && *pos != '\0') {
  6718. if (os_strncmp(pos, "ssid ", 5) == 0) {
  6719. char *end;
  6720. pos += 5;
  6721. end = pos;
  6722. while (*end) {
  6723. if (*end == '\0' || *end == ' ')
  6724. break;
  6725. end++;
  6726. }
  6727. ns = os_realloc_array(
  6728. ssid, ssid_count + 1,
  6729. sizeof(struct wpa_ssid_value));
  6730. if (ns == NULL) {
  6731. *reply_len = -1;
  6732. goto done;
  6733. }
  6734. ssid = ns;
  6735. if ((end - pos) & 0x01 ||
  6736. end - pos > 2 * SSID_MAX_LEN ||
  6737. hexstr2bin(pos, ssid[ssid_count].ssid,
  6738. (end - pos) / 2) < 0) {
  6739. wpa_printf(MSG_DEBUG,
  6740. "Invalid SSID value '%s'",
  6741. pos);
  6742. *reply_len = -1;
  6743. goto done;
  6744. }
  6745. ssid[ssid_count].ssid_len = (end - pos) / 2;
  6746. wpa_hexdump_ascii(MSG_DEBUG, "scan SSID",
  6747. ssid[ssid_count].ssid,
  6748. ssid[ssid_count].ssid_len);
  6749. ssid_count++;
  6750. pos = end;
  6751. }
  6752. pos = os_strchr(pos, ' ');
  6753. if (pos)
  6754. pos++;
  6755. }
  6756. }
  6757. wpa_s->num_ssids_from_scan_req = ssid_count;
  6758. os_free(wpa_s->ssids_from_scan_req);
  6759. if (ssid_count) {
  6760. wpa_s->ssids_from_scan_req = ssid;
  6761. ssid = NULL;
  6762. } else {
  6763. wpa_s->ssids_from_scan_req = NULL;
  6764. }
  6765. if (scan_only)
  6766. scan_res_handler = scan_only_handler;
  6767. else if (wpa_s->scan_res_handler == scan_only_handler)
  6768. scan_res_handler = NULL;
  6769. else
  6770. scan_res_handler = wpa_s->scan_res_handler;
  6771. if (!wpa_s->sched_scanning && !wpa_s->scanning &&
  6772. ((wpa_s->wpa_state <= WPA_SCANNING) ||
  6773. (wpa_s->wpa_state == WPA_COMPLETED))) {
  6774. wpa_s->manual_scan_passive = manual_scan_passive;
  6775. wpa_s->manual_scan_use_id = manual_scan_use_id;
  6776. wpa_s->manual_scan_only_new = manual_scan_only_new;
  6777. wpa_s->scan_id_count = scan_id_count;
  6778. os_memcpy(wpa_s->scan_id, scan_id, scan_id_count * sizeof(int));
  6779. wpa_s->scan_res_handler = scan_res_handler;
  6780. os_free(wpa_s->manual_scan_freqs);
  6781. wpa_s->manual_scan_freqs = manual_scan_freqs;
  6782. manual_scan_freqs = NULL;
  6783. wpa_s->normal_scans = 0;
  6784. wpa_s->scan_req = MANUAL_SCAN_REQ;
  6785. wpa_s->after_wps = 0;
  6786. wpa_s->known_wps_freq = 0;
  6787. wpa_supplicant_req_scan(wpa_s, 0, 0);
  6788. if (wpa_s->manual_scan_use_id) {
  6789. wpa_s->manual_scan_id++;
  6790. wpa_dbg(wpa_s, MSG_DEBUG, "Assigned scan id %u",
  6791. wpa_s->manual_scan_id);
  6792. *reply_len = os_snprintf(reply, reply_size, "%u\n",
  6793. wpa_s->manual_scan_id);
  6794. }
  6795. } else if (wpa_s->sched_scanning) {
  6796. wpa_s->manual_scan_passive = manual_scan_passive;
  6797. wpa_s->manual_scan_use_id = manual_scan_use_id;
  6798. wpa_s->manual_scan_only_new = manual_scan_only_new;
  6799. wpa_s->scan_id_count = scan_id_count;
  6800. os_memcpy(wpa_s->scan_id, scan_id, scan_id_count * sizeof(int));
  6801. wpa_s->scan_res_handler = scan_res_handler;
  6802. os_free(wpa_s->manual_scan_freqs);
  6803. wpa_s->manual_scan_freqs = manual_scan_freqs;
  6804. manual_scan_freqs = NULL;
  6805. wpa_printf(MSG_DEBUG, "Stop ongoing sched_scan to allow requested full scan to proceed");
  6806. wpa_supplicant_cancel_sched_scan(wpa_s);
  6807. wpa_s->scan_req = MANUAL_SCAN_REQ;
  6808. wpa_supplicant_req_scan(wpa_s, 0, 0);
  6809. if (wpa_s->manual_scan_use_id) {
  6810. wpa_s->manual_scan_id++;
  6811. *reply_len = os_snprintf(reply, reply_size, "%u\n",
  6812. wpa_s->manual_scan_id);
  6813. wpa_dbg(wpa_s, MSG_DEBUG, "Assigned scan id %u",
  6814. wpa_s->manual_scan_id);
  6815. }
  6816. } else {
  6817. wpa_printf(MSG_DEBUG, "Ongoing scan action - reject new request");
  6818. *reply_len = os_snprintf(reply, reply_size, "FAIL-BUSY\n");
  6819. }
  6820. done:
  6821. os_free(manual_scan_freqs);
  6822. os_free(ssid);
  6823. }
  6824. #ifdef CONFIG_TESTING_OPTIONS
  6825. static void wpas_ctrl_iface_mgmt_tx_cb(struct wpa_supplicant *wpa_s,
  6826. unsigned int freq, const u8 *dst,
  6827. const u8 *src, const u8 *bssid,
  6828. const u8 *data, size_t data_len,
  6829. enum offchannel_send_action_result
  6830. result)
  6831. {
  6832. wpa_msg(wpa_s, MSG_INFO, "MGMT-TX-STATUS freq=%u dst=" MACSTR
  6833. " src=" MACSTR " bssid=" MACSTR " result=%s",
  6834. freq, MAC2STR(dst), MAC2STR(src), MAC2STR(bssid),
  6835. result == OFFCHANNEL_SEND_ACTION_SUCCESS ?
  6836. "SUCCESS" : (result == OFFCHANNEL_SEND_ACTION_NO_ACK ?
  6837. "NO_ACK" : "FAILED"));
  6838. }
  6839. static int wpas_ctrl_iface_mgmt_tx(struct wpa_supplicant *wpa_s, char *cmd)
  6840. {
  6841. char *pos, *param;
  6842. size_t len;
  6843. u8 *buf, da[ETH_ALEN], bssid[ETH_ALEN];
  6844. int res, used;
  6845. int freq = 0, no_cck = 0, wait_time = 0;
  6846. /* <DA> <BSSID> [freq=<MHz>] [wait_time=<ms>] [no_cck=1]
  6847. * <action=Action frame payload> */
  6848. wpa_printf(MSG_DEBUG, "External MGMT TX: %s", cmd);
  6849. pos = cmd;
  6850. used = hwaddr_aton2(pos, da);
  6851. if (used < 0)
  6852. return -1;
  6853. pos += used;
  6854. while (*pos == ' ')
  6855. pos++;
  6856. used = hwaddr_aton2(pos, bssid);
  6857. if (used < 0)
  6858. return -1;
  6859. pos += used;
  6860. param = os_strstr(pos, " freq=");
  6861. if (param) {
  6862. param += 6;
  6863. freq = atoi(param);
  6864. }
  6865. param = os_strstr(pos, " no_cck=");
  6866. if (param) {
  6867. param += 8;
  6868. no_cck = atoi(param);
  6869. }
  6870. param = os_strstr(pos, " wait_time=");
  6871. if (param) {
  6872. param += 11;
  6873. wait_time = atoi(param);
  6874. }
  6875. param = os_strstr(pos, " action=");
  6876. if (param == NULL)
  6877. return -1;
  6878. param += 8;
  6879. len = os_strlen(param);
  6880. if (len & 1)
  6881. return -1;
  6882. len /= 2;
  6883. buf = os_malloc(len);
  6884. if (buf == NULL)
  6885. return -1;
  6886. if (hexstr2bin(param, buf, len) < 0) {
  6887. os_free(buf);
  6888. return -1;
  6889. }
  6890. res = offchannel_send_action(wpa_s, freq, da, wpa_s->own_addr, bssid,
  6891. buf, len, wait_time,
  6892. wpas_ctrl_iface_mgmt_tx_cb, no_cck);
  6893. os_free(buf);
  6894. return res;
  6895. }
  6896. static void wpas_ctrl_iface_mgmt_tx_done(struct wpa_supplicant *wpa_s)
  6897. {
  6898. wpa_printf(MSG_DEBUG, "External MGMT TX - done waiting");
  6899. offchannel_send_action_done(wpa_s);
  6900. }
  6901. static int wpas_ctrl_iface_mgmt_rx_process(struct wpa_supplicant *wpa_s,
  6902. char *cmd)
  6903. {
  6904. char *pos, *param;
  6905. size_t len;
  6906. u8 *buf;
  6907. int freq = 0, datarate = 0, ssi_signal = 0;
  6908. union wpa_event_data event;
  6909. if (!wpa_s->ext_mgmt_frame_handling)
  6910. return -1;
  6911. /* freq=<MHz> datarate=<val> ssi_signal=<val> frame=<frame hexdump> */
  6912. wpa_printf(MSG_DEBUG, "External MGMT RX process: %s", cmd);
  6913. pos = cmd;
  6914. param = os_strstr(pos, "freq=");
  6915. if (param) {
  6916. param += 5;
  6917. freq = atoi(param);
  6918. }
  6919. param = os_strstr(pos, " datarate=");
  6920. if (param) {
  6921. param += 10;
  6922. datarate = atoi(param);
  6923. }
  6924. param = os_strstr(pos, " ssi_signal=");
  6925. if (param) {
  6926. param += 12;
  6927. ssi_signal = atoi(param);
  6928. }
  6929. param = os_strstr(pos, " frame=");
  6930. if (param == NULL)
  6931. return -1;
  6932. param += 7;
  6933. len = os_strlen(param);
  6934. if (len & 1)
  6935. return -1;
  6936. len /= 2;
  6937. buf = os_malloc(len);
  6938. if (buf == NULL)
  6939. return -1;
  6940. if (hexstr2bin(param, buf, len) < 0) {
  6941. os_free(buf);
  6942. return -1;
  6943. }
  6944. os_memset(&event, 0, sizeof(event));
  6945. event.rx_mgmt.freq = freq;
  6946. event.rx_mgmt.frame = buf;
  6947. event.rx_mgmt.frame_len = len;
  6948. event.rx_mgmt.ssi_signal = ssi_signal;
  6949. event.rx_mgmt.datarate = datarate;
  6950. wpa_s->ext_mgmt_frame_handling = 0;
  6951. wpa_supplicant_event(wpa_s, EVENT_RX_MGMT, &event);
  6952. wpa_s->ext_mgmt_frame_handling = 1;
  6953. os_free(buf);
  6954. return 0;
  6955. }
  6956. static int wpas_ctrl_iface_driver_scan_res(struct wpa_supplicant *wpa_s,
  6957. char *param)
  6958. {
  6959. struct wpa_scan_res *res;
  6960. struct os_reltime now;
  6961. char *pos, *end;
  6962. int ret = -1;
  6963. if (!param)
  6964. return -1;
  6965. if (os_strcmp(param, "START") == 0) {
  6966. wpa_bss_update_start(wpa_s);
  6967. return 0;
  6968. }
  6969. if (os_strcmp(param, "END") == 0) {
  6970. wpa_bss_update_end(wpa_s, NULL, 1);
  6971. return 0;
  6972. }
  6973. if (os_strncmp(param, "BSS ", 4) != 0)
  6974. return -1;
  6975. param += 3;
  6976. res = os_zalloc(sizeof(*res) + os_strlen(param) / 2);
  6977. if (!res)
  6978. return -1;
  6979. pos = os_strstr(param, " flags=");
  6980. if (pos)
  6981. res->flags = strtol(pos + 7, NULL, 16);
  6982. pos = os_strstr(param, " bssid=");
  6983. if (pos && hwaddr_aton(pos + 7, res->bssid))
  6984. goto fail;
  6985. pos = os_strstr(param, " freq=");
  6986. if (pos)
  6987. res->freq = atoi(pos + 6);
  6988. pos = os_strstr(param, " beacon_int=");
  6989. if (pos)
  6990. res->beacon_int = atoi(pos + 12);
  6991. pos = os_strstr(param, " caps=");
  6992. if (pos)
  6993. res->caps = strtol(pos + 6, NULL, 16);
  6994. pos = os_strstr(param, " qual=");
  6995. if (pos)
  6996. res->qual = atoi(pos + 6);
  6997. pos = os_strstr(param, " noise=");
  6998. if (pos)
  6999. res->noise = atoi(pos + 7);
  7000. pos = os_strstr(param, " level=");
  7001. if (pos)
  7002. res->level = atoi(pos + 7);
  7003. pos = os_strstr(param, " tsf=");
  7004. if (pos)
  7005. res->tsf = strtoll(pos + 5, NULL, 16);
  7006. pos = os_strstr(param, " age=");
  7007. if (pos)
  7008. res->age = atoi(pos + 5);
  7009. pos = os_strstr(param, " est_throughput=");
  7010. if (pos)
  7011. res->est_throughput = atoi(pos + 16);
  7012. pos = os_strstr(param, " snr=");
  7013. if (pos)
  7014. res->snr = atoi(pos + 5);
  7015. pos = os_strstr(param, " parent_tsf=");
  7016. if (pos)
  7017. res->parent_tsf = strtoll(pos + 7, NULL, 16);
  7018. pos = os_strstr(param, " tsf_bssid=");
  7019. if (pos && hwaddr_aton(pos + 11, res->tsf_bssid))
  7020. goto fail;
  7021. pos = os_strstr(param, " ie=");
  7022. if (pos) {
  7023. pos += 4;
  7024. end = os_strchr(pos, ' ');
  7025. if (!end)
  7026. end = pos + os_strlen(pos);
  7027. res->ie_len = (end - pos) / 2;
  7028. if (hexstr2bin(pos, (u8 *) (res + 1), res->ie_len))
  7029. goto fail;
  7030. }
  7031. pos = os_strstr(param, " beacon_ie=");
  7032. if (pos) {
  7033. pos += 11;
  7034. end = os_strchr(pos, ' ');
  7035. if (!end)
  7036. end = pos + os_strlen(pos);
  7037. res->beacon_ie_len = (end - pos) / 2;
  7038. if (hexstr2bin(pos, ((u8 *) (res + 1)) + res->ie_len,
  7039. res->beacon_ie_len))
  7040. goto fail;
  7041. }
  7042. os_get_reltime(&now);
  7043. wpa_bss_update_scan_res(wpa_s, res, &now);
  7044. ret = 0;
  7045. fail:
  7046. os_free(res);
  7047. return ret;
  7048. }
  7049. static int wpas_ctrl_iface_driver_event(struct wpa_supplicant *wpa_s, char *cmd)
  7050. {
  7051. char *pos, *param;
  7052. union wpa_event_data event;
  7053. enum wpa_event_type ev;
  7054. /* <event name> [parameters..] */
  7055. wpa_dbg(wpa_s, MSG_DEBUG, "Testing - external driver event: %s", cmd);
  7056. pos = cmd;
  7057. param = os_strchr(pos, ' ');
  7058. if (param)
  7059. *param++ = '\0';
  7060. os_memset(&event, 0, sizeof(event));
  7061. if (os_strcmp(cmd, "INTERFACE_ENABLED") == 0) {
  7062. ev = EVENT_INTERFACE_ENABLED;
  7063. } else if (os_strcmp(cmd, "INTERFACE_DISABLED") == 0) {
  7064. ev = EVENT_INTERFACE_DISABLED;
  7065. } else if (os_strcmp(cmd, "AVOID_FREQUENCIES") == 0) {
  7066. ev = EVENT_AVOID_FREQUENCIES;
  7067. if (param == NULL)
  7068. param = "";
  7069. if (freq_range_list_parse(&event.freq_range, param) < 0)
  7070. return -1;
  7071. wpa_supplicant_event(wpa_s, ev, &event);
  7072. os_free(event.freq_range.range);
  7073. return 0;
  7074. } else if (os_strcmp(cmd, "SCAN_RES") == 0) {
  7075. return wpas_ctrl_iface_driver_scan_res(wpa_s, param);
  7076. } else {
  7077. wpa_dbg(wpa_s, MSG_DEBUG, "Testing - unknown driver event: %s",
  7078. cmd);
  7079. return -1;
  7080. }
  7081. wpa_supplicant_event(wpa_s, ev, &event);
  7082. return 0;
  7083. }
  7084. static int wpas_ctrl_iface_eapol_rx(struct wpa_supplicant *wpa_s, char *cmd)
  7085. {
  7086. char *pos;
  7087. u8 src[ETH_ALEN], *buf;
  7088. int used;
  7089. size_t len;
  7090. wpa_printf(MSG_DEBUG, "External EAPOL RX: %s", cmd);
  7091. pos = cmd;
  7092. used = hwaddr_aton2(pos, src);
  7093. if (used < 0)
  7094. return -1;
  7095. pos += used;
  7096. while (*pos == ' ')
  7097. pos++;
  7098. len = os_strlen(pos);
  7099. if (len & 1)
  7100. return -1;
  7101. len /= 2;
  7102. buf = os_malloc(len);
  7103. if (buf == NULL)
  7104. return -1;
  7105. if (hexstr2bin(pos, buf, len) < 0) {
  7106. os_free(buf);
  7107. return -1;
  7108. }
  7109. wpa_supplicant_rx_eapol(wpa_s, src, buf, len);
  7110. os_free(buf);
  7111. return 0;
  7112. }
  7113. static u16 ipv4_hdr_checksum(const void *buf, size_t len)
  7114. {
  7115. size_t i;
  7116. u32 sum = 0;
  7117. const u16 *pos = buf;
  7118. for (i = 0; i < len / 2; i++)
  7119. sum += *pos++;
  7120. while (sum >> 16)
  7121. sum = (sum & 0xffff) + (sum >> 16);
  7122. return sum ^ 0xffff;
  7123. }
  7124. #define HWSIM_PACKETLEN 1500
  7125. #define HWSIM_IP_LEN (HWSIM_PACKETLEN - sizeof(struct ether_header))
  7126. static void wpas_data_test_rx(void *ctx, const u8 *src_addr, const u8 *buf,
  7127. size_t len)
  7128. {
  7129. struct wpa_supplicant *wpa_s = ctx;
  7130. const struct ether_header *eth;
  7131. struct iphdr ip;
  7132. const u8 *pos;
  7133. unsigned int i;
  7134. if (len != HWSIM_PACKETLEN)
  7135. return;
  7136. eth = (const struct ether_header *) buf;
  7137. os_memcpy(&ip, eth + 1, sizeof(ip));
  7138. pos = &buf[sizeof(*eth) + sizeof(ip)];
  7139. if (ip.ihl != 5 || ip.version != 4 ||
  7140. ntohs(ip.tot_len) != HWSIM_IP_LEN)
  7141. return;
  7142. for (i = 0; i < HWSIM_IP_LEN - sizeof(ip); i++) {
  7143. if (*pos != (u8) i)
  7144. return;
  7145. pos++;
  7146. }
  7147. wpa_msg(wpa_s, MSG_INFO, "DATA-TEST-RX " MACSTR " " MACSTR,
  7148. MAC2STR(eth->ether_dhost), MAC2STR(eth->ether_shost));
  7149. }
  7150. static int wpas_ctrl_iface_data_test_config(struct wpa_supplicant *wpa_s,
  7151. char *cmd)
  7152. {
  7153. int enabled = atoi(cmd);
  7154. char *pos;
  7155. const char *ifname;
  7156. if (!enabled) {
  7157. if (wpa_s->l2_test) {
  7158. l2_packet_deinit(wpa_s->l2_test);
  7159. wpa_s->l2_test = NULL;
  7160. wpa_dbg(wpa_s, MSG_DEBUG, "test data: Disabled");
  7161. }
  7162. return 0;
  7163. }
  7164. if (wpa_s->l2_test)
  7165. return 0;
  7166. pos = os_strstr(cmd, " ifname=");
  7167. if (pos)
  7168. ifname = pos + 8;
  7169. else
  7170. ifname = wpa_s->ifname;
  7171. wpa_s->l2_test = l2_packet_init(ifname, wpa_s->own_addr,
  7172. ETHERTYPE_IP, wpas_data_test_rx,
  7173. wpa_s, 1);
  7174. if (wpa_s->l2_test == NULL)
  7175. return -1;
  7176. wpa_dbg(wpa_s, MSG_DEBUG, "test data: Enabled");
  7177. return 0;
  7178. }
  7179. static int wpas_ctrl_iface_data_test_tx(struct wpa_supplicant *wpa_s, char *cmd)
  7180. {
  7181. u8 dst[ETH_ALEN], src[ETH_ALEN];
  7182. char *pos;
  7183. int used;
  7184. long int val;
  7185. u8 tos;
  7186. u8 buf[2 + HWSIM_PACKETLEN];
  7187. struct ether_header *eth;
  7188. struct iphdr *ip;
  7189. u8 *dpos;
  7190. unsigned int i;
  7191. if (wpa_s->l2_test == NULL)
  7192. return -1;
  7193. /* format: <dst> <src> <tos> */
  7194. pos = cmd;
  7195. used = hwaddr_aton2(pos, dst);
  7196. if (used < 0)
  7197. return -1;
  7198. pos += used;
  7199. while (*pos == ' ')
  7200. pos++;
  7201. used = hwaddr_aton2(pos, src);
  7202. if (used < 0)
  7203. return -1;
  7204. pos += used;
  7205. val = strtol(pos, NULL, 0);
  7206. if (val < 0 || val > 0xff)
  7207. return -1;
  7208. tos = val;
  7209. eth = (struct ether_header *) &buf[2];
  7210. os_memcpy(eth->ether_dhost, dst, ETH_ALEN);
  7211. os_memcpy(eth->ether_shost, src, ETH_ALEN);
  7212. eth->ether_type = htons(ETHERTYPE_IP);
  7213. ip = (struct iphdr *) (eth + 1);
  7214. os_memset(ip, 0, sizeof(*ip));
  7215. ip->ihl = 5;
  7216. ip->version = 4;
  7217. ip->ttl = 64;
  7218. ip->tos = tos;
  7219. ip->tot_len = htons(HWSIM_IP_LEN);
  7220. ip->protocol = 1;
  7221. ip->saddr = htonl(192U << 24 | 168 << 16 | 1 << 8 | 1);
  7222. ip->daddr = htonl(192U << 24 | 168 << 16 | 1 << 8 | 2);
  7223. ip->check = ipv4_hdr_checksum(ip, sizeof(*ip));
  7224. dpos = (u8 *) (ip + 1);
  7225. for (i = 0; i < HWSIM_IP_LEN - sizeof(*ip); i++)
  7226. *dpos++ = i;
  7227. if (l2_packet_send(wpa_s->l2_test, dst, ETHERTYPE_IP, &buf[2],
  7228. HWSIM_PACKETLEN) < 0)
  7229. return -1;
  7230. wpa_dbg(wpa_s, MSG_DEBUG, "test data: TX dst=" MACSTR " src=" MACSTR
  7231. " tos=0x%x", MAC2STR(dst), MAC2STR(src), tos);
  7232. return 0;
  7233. }
  7234. static int wpas_ctrl_iface_data_test_frame(struct wpa_supplicant *wpa_s,
  7235. char *cmd)
  7236. {
  7237. u8 *buf;
  7238. struct ether_header *eth;
  7239. struct l2_packet_data *l2 = NULL;
  7240. size_t len;
  7241. u16 ethertype;
  7242. int res = -1;
  7243. len = os_strlen(cmd);
  7244. if (len & 1 || len < ETH_HLEN * 2)
  7245. return -1;
  7246. len /= 2;
  7247. buf = os_malloc(len);
  7248. if (buf == NULL)
  7249. return -1;
  7250. if (hexstr2bin(cmd, buf, len) < 0)
  7251. goto done;
  7252. eth = (struct ether_header *) buf;
  7253. ethertype = ntohs(eth->ether_type);
  7254. l2 = l2_packet_init(wpa_s->ifname, wpa_s->own_addr, ethertype,
  7255. wpas_data_test_rx, wpa_s, 1);
  7256. if (l2 == NULL)
  7257. goto done;
  7258. res = l2_packet_send(l2, eth->ether_dhost, ethertype, buf, len);
  7259. wpa_dbg(wpa_s, MSG_DEBUG, "test data: TX frame res=%d", res);
  7260. done:
  7261. if (l2)
  7262. l2_packet_deinit(l2);
  7263. os_free(buf);
  7264. return res < 0 ? -1 : 0;
  7265. }
  7266. static int wpas_ctrl_test_alloc_fail(struct wpa_supplicant *wpa_s, char *cmd)
  7267. {
  7268. #ifdef WPA_TRACE_BFD
  7269. char *pos;
  7270. wpa_trace_fail_after = atoi(cmd);
  7271. pos = os_strchr(cmd, ':');
  7272. if (pos) {
  7273. pos++;
  7274. os_strlcpy(wpa_trace_fail_func, pos,
  7275. sizeof(wpa_trace_fail_func));
  7276. } else {
  7277. wpa_trace_fail_after = 0;
  7278. }
  7279. return 0;
  7280. #else /* WPA_TRACE_BFD */
  7281. return -1;
  7282. #endif /* WPA_TRACE_BFD */
  7283. }
  7284. static int wpas_ctrl_get_alloc_fail(struct wpa_supplicant *wpa_s,
  7285. char *buf, size_t buflen)
  7286. {
  7287. #ifdef WPA_TRACE_BFD
  7288. return os_snprintf(buf, buflen, "%u:%s", wpa_trace_fail_after,
  7289. wpa_trace_fail_func);
  7290. #else /* WPA_TRACE_BFD */
  7291. return -1;
  7292. #endif /* WPA_TRACE_BFD */
  7293. }
  7294. static int wpas_ctrl_test_fail(struct wpa_supplicant *wpa_s, char *cmd)
  7295. {
  7296. #ifdef WPA_TRACE_BFD
  7297. char *pos;
  7298. wpa_trace_test_fail_after = atoi(cmd);
  7299. pos = os_strchr(cmd, ':');
  7300. if (pos) {
  7301. pos++;
  7302. os_strlcpy(wpa_trace_test_fail_func, pos,
  7303. sizeof(wpa_trace_test_fail_func));
  7304. } else {
  7305. wpa_trace_test_fail_after = 0;
  7306. }
  7307. return 0;
  7308. #else /* WPA_TRACE_BFD */
  7309. return -1;
  7310. #endif /* WPA_TRACE_BFD */
  7311. }
  7312. static int wpas_ctrl_get_fail(struct wpa_supplicant *wpa_s,
  7313. char *buf, size_t buflen)
  7314. {
  7315. #ifdef WPA_TRACE_BFD
  7316. return os_snprintf(buf, buflen, "%u:%s", wpa_trace_test_fail_after,
  7317. wpa_trace_test_fail_func);
  7318. #else /* WPA_TRACE_BFD */
  7319. return -1;
  7320. #endif /* WPA_TRACE_BFD */
  7321. }
  7322. static void wpas_ctrl_event_test_cb(void *eloop_ctx, void *timeout_ctx)
  7323. {
  7324. struct wpa_supplicant *wpa_s = eloop_ctx;
  7325. int i, count = (intptr_t) timeout_ctx;
  7326. wpa_printf(MSG_DEBUG, "TEST: Send %d control interface event messages",
  7327. count);
  7328. for (i = 0; i < count; i++) {
  7329. wpa_msg_ctrl(wpa_s, MSG_INFO, "TEST-EVENT-MESSAGE %d/%d",
  7330. i + 1, count);
  7331. }
  7332. }
  7333. static int wpas_ctrl_event_test(struct wpa_supplicant *wpa_s, const char *cmd)
  7334. {
  7335. int count;
  7336. count = atoi(cmd);
  7337. if (count <= 0)
  7338. return -1;
  7339. return eloop_register_timeout(0, 0, wpas_ctrl_event_test_cb, wpa_s,
  7340. (void *) (intptr_t) count);
  7341. }
  7342. static int wpas_ctrl_test_assoc_ie(struct wpa_supplicant *wpa_s,
  7343. const char *cmd)
  7344. {
  7345. struct wpabuf *buf;
  7346. size_t len;
  7347. len = os_strlen(cmd);
  7348. if (len & 1)
  7349. return -1;
  7350. len /= 2;
  7351. if (len == 0) {
  7352. buf = NULL;
  7353. } else {
  7354. buf = wpabuf_alloc(len);
  7355. if (buf == NULL)
  7356. return -1;
  7357. if (hexstr2bin(cmd, wpabuf_put(buf, len), len) < 0) {
  7358. wpabuf_free(buf);
  7359. return -1;
  7360. }
  7361. }
  7362. wpa_sm_set_test_assoc_ie(wpa_s->wpa, buf);
  7363. return 0;
  7364. }
  7365. #endif /* CONFIG_TESTING_OPTIONS */
  7366. static int wpas_ctrl_vendor_elem_add(struct wpa_supplicant *wpa_s, char *cmd)
  7367. {
  7368. char *pos = cmd;
  7369. int frame;
  7370. size_t len;
  7371. struct wpabuf *buf;
  7372. struct ieee802_11_elems elems;
  7373. frame = atoi(pos);
  7374. if (frame < 0 || frame >= NUM_VENDOR_ELEM_FRAMES)
  7375. return -1;
  7376. wpa_s = wpas_vendor_elem(wpa_s, frame);
  7377. pos = os_strchr(pos, ' ');
  7378. if (pos == NULL)
  7379. return -1;
  7380. pos++;
  7381. len = os_strlen(pos);
  7382. if (len == 0)
  7383. return 0;
  7384. if (len & 1)
  7385. return -1;
  7386. len /= 2;
  7387. buf = wpabuf_alloc(len);
  7388. if (buf == NULL)
  7389. return -1;
  7390. if (hexstr2bin(pos, wpabuf_put(buf, len), len) < 0) {
  7391. wpabuf_free(buf);
  7392. return -1;
  7393. }
  7394. if (ieee802_11_parse_elems(wpabuf_head_u8(buf), len, &elems, 0) ==
  7395. ParseFailed) {
  7396. wpabuf_free(buf);
  7397. return -1;
  7398. }
  7399. if (wpa_s->vendor_elem[frame] == NULL) {
  7400. wpa_s->vendor_elem[frame] = buf;
  7401. wpas_vendor_elem_update(wpa_s);
  7402. return 0;
  7403. }
  7404. if (wpabuf_resize(&wpa_s->vendor_elem[frame], len) < 0) {
  7405. wpabuf_free(buf);
  7406. return -1;
  7407. }
  7408. wpabuf_put_buf(wpa_s->vendor_elem[frame], buf);
  7409. wpabuf_free(buf);
  7410. wpas_vendor_elem_update(wpa_s);
  7411. return 0;
  7412. }
  7413. static int wpas_ctrl_vendor_elem_get(struct wpa_supplicant *wpa_s, char *cmd,
  7414. char *buf, size_t buflen)
  7415. {
  7416. int frame = atoi(cmd);
  7417. if (frame < 0 || frame >= NUM_VENDOR_ELEM_FRAMES)
  7418. return -1;
  7419. wpa_s = wpas_vendor_elem(wpa_s, frame);
  7420. if (wpa_s->vendor_elem[frame] == NULL)
  7421. return 0;
  7422. return wpa_snprintf_hex(buf, buflen,
  7423. wpabuf_head_u8(wpa_s->vendor_elem[frame]),
  7424. wpabuf_len(wpa_s->vendor_elem[frame]));
  7425. }
  7426. static int wpas_ctrl_vendor_elem_remove(struct wpa_supplicant *wpa_s, char *cmd)
  7427. {
  7428. char *pos = cmd;
  7429. int frame;
  7430. size_t len;
  7431. u8 *buf;
  7432. struct ieee802_11_elems elems;
  7433. int res;
  7434. frame = atoi(pos);
  7435. if (frame < 0 || frame >= NUM_VENDOR_ELEM_FRAMES)
  7436. return -1;
  7437. wpa_s = wpas_vendor_elem(wpa_s, frame);
  7438. pos = os_strchr(pos, ' ');
  7439. if (pos == NULL)
  7440. return -1;
  7441. pos++;
  7442. if (*pos == '*') {
  7443. wpabuf_free(wpa_s->vendor_elem[frame]);
  7444. wpa_s->vendor_elem[frame] = NULL;
  7445. wpas_vendor_elem_update(wpa_s);
  7446. return 0;
  7447. }
  7448. if (wpa_s->vendor_elem[frame] == NULL)
  7449. return -1;
  7450. len = os_strlen(pos);
  7451. if (len == 0)
  7452. return 0;
  7453. if (len & 1)
  7454. return -1;
  7455. len /= 2;
  7456. buf = os_malloc(len);
  7457. if (buf == NULL)
  7458. return -1;
  7459. if (hexstr2bin(pos, buf, len) < 0) {
  7460. os_free(buf);
  7461. return -1;
  7462. }
  7463. if (ieee802_11_parse_elems(buf, len, &elems, 0) == ParseFailed) {
  7464. os_free(buf);
  7465. return -1;
  7466. }
  7467. res = wpas_vendor_elem_remove(wpa_s, frame, buf, len);
  7468. os_free(buf);
  7469. return res;
  7470. }
  7471. static void wpas_ctrl_neighbor_rep_cb(void *ctx, struct wpabuf *neighbor_rep)
  7472. {
  7473. struct wpa_supplicant *wpa_s = ctx;
  7474. size_t len;
  7475. const u8 *data;
  7476. /*
  7477. * Neighbor Report element (IEEE P802.11-REVmc/D5.0)
  7478. * BSSID[6]
  7479. * BSSID Information[4]
  7480. * Operating Class[1]
  7481. * Channel Number[1]
  7482. * PHY Type[1]
  7483. * Optional Subelements[variable]
  7484. */
  7485. #define NR_IE_MIN_LEN (ETH_ALEN + 4 + 1 + 1 + 1)
  7486. if (!neighbor_rep || wpabuf_len(neighbor_rep) == 0) {
  7487. wpa_msg_ctrl(wpa_s, MSG_INFO, RRM_EVENT_NEIGHBOR_REP_FAILED);
  7488. goto out;
  7489. }
  7490. data = wpabuf_head_u8(neighbor_rep);
  7491. len = wpabuf_len(neighbor_rep);
  7492. while (len >= 2 + NR_IE_MIN_LEN) {
  7493. const u8 *nr;
  7494. char lci[256 * 2 + 1];
  7495. char civic[256 * 2 + 1];
  7496. u8 nr_len = data[1];
  7497. const u8 *pos = data, *end;
  7498. if (pos[0] != WLAN_EID_NEIGHBOR_REPORT ||
  7499. nr_len < NR_IE_MIN_LEN) {
  7500. wpa_printf(MSG_DEBUG,
  7501. "CTRL: Invalid Neighbor Report element: id=%u len=%u",
  7502. data[0], nr_len);
  7503. goto out;
  7504. }
  7505. if (2U + nr_len > len) {
  7506. wpa_printf(MSG_DEBUG,
  7507. "CTRL: Invalid Neighbor Report element: id=%u len=%zu nr_len=%u",
  7508. data[0], len, nr_len);
  7509. goto out;
  7510. }
  7511. pos += 2;
  7512. end = pos + nr_len;
  7513. nr = pos;
  7514. pos += NR_IE_MIN_LEN;
  7515. lci[0] = '\0';
  7516. civic[0] = '\0';
  7517. while (end - pos > 2) {
  7518. u8 s_id, s_len;
  7519. s_id = *pos++;
  7520. s_len = *pos++;
  7521. if (s_len > end - pos)
  7522. goto out;
  7523. if (s_id == WLAN_EID_MEASURE_REPORT && s_len > 3) {
  7524. /* Measurement Token[1] */
  7525. /* Measurement Report Mode[1] */
  7526. /* Measurement Type[1] */
  7527. /* Measurement Report[variable] */
  7528. switch (pos[2]) {
  7529. case MEASURE_TYPE_LCI:
  7530. if (lci[0])
  7531. break;
  7532. wpa_snprintf_hex(lci, sizeof(lci),
  7533. pos, s_len);
  7534. break;
  7535. case MEASURE_TYPE_LOCATION_CIVIC:
  7536. if (civic[0])
  7537. break;
  7538. wpa_snprintf_hex(civic, sizeof(civic),
  7539. pos, s_len);
  7540. break;
  7541. }
  7542. }
  7543. pos += s_len;
  7544. }
  7545. wpa_msg(wpa_s, MSG_INFO, RRM_EVENT_NEIGHBOR_REP_RXED
  7546. "bssid=" MACSTR
  7547. " info=0x%x op_class=%u chan=%u phy_type=%u%s%s%s%s",
  7548. MAC2STR(nr), WPA_GET_LE32(nr + ETH_ALEN),
  7549. nr[ETH_ALEN + 4], nr[ETH_ALEN + 5],
  7550. nr[ETH_ALEN + 6],
  7551. lci[0] ? " lci=" : "", lci,
  7552. civic[0] ? " civic=" : "", civic);
  7553. data = end;
  7554. len -= 2 + nr_len;
  7555. }
  7556. out:
  7557. wpabuf_free(neighbor_rep);
  7558. }
  7559. static int wpas_ctrl_iface_send_neighbor_rep(struct wpa_supplicant *wpa_s,
  7560. char *cmd)
  7561. {
  7562. struct wpa_ssid_value ssid, *ssid_p = NULL;
  7563. int ret, lci = 0, civic = 0;
  7564. char *ssid_s;
  7565. ssid_s = os_strstr(cmd, "ssid=");
  7566. if (ssid_s) {
  7567. if (ssid_parse(ssid_s + 5, &ssid)) {
  7568. wpa_printf(MSG_ERROR,
  7569. "CTRL: Send Neighbor Report: bad SSID");
  7570. return -1;
  7571. }
  7572. ssid_p = &ssid;
  7573. /*
  7574. * Move cmd after the SSID text that may include "lci" or
  7575. * "civic".
  7576. */
  7577. cmd = os_strchr(ssid_s + 6, ssid_s[5] == '"' ? '"' : ' ');
  7578. if (cmd)
  7579. cmd++;
  7580. }
  7581. if (cmd && os_strstr(cmd, "lci"))
  7582. lci = 1;
  7583. if (cmd && os_strstr(cmd, "civic"))
  7584. civic = 1;
  7585. ret = wpas_rrm_send_neighbor_rep_request(wpa_s, ssid_p, lci, civic,
  7586. wpas_ctrl_neighbor_rep_cb,
  7587. wpa_s);
  7588. return ret;
  7589. }
  7590. static int wpas_ctrl_iface_erp_flush(struct wpa_supplicant *wpa_s)
  7591. {
  7592. eapol_sm_erp_flush(wpa_s->eapol);
  7593. return 0;
  7594. }
  7595. static int wpas_ctrl_iface_mac_rand_scan(struct wpa_supplicant *wpa_s,
  7596. char *cmd)
  7597. {
  7598. char *token, *context = NULL;
  7599. unsigned int enable = ~0, type = 0;
  7600. u8 _addr[ETH_ALEN], _mask[ETH_ALEN];
  7601. u8 *addr = NULL, *mask = NULL;
  7602. while ((token = str_token(cmd, " ", &context))) {
  7603. if (os_strcasecmp(token, "scan") == 0) {
  7604. type |= MAC_ADDR_RAND_SCAN;
  7605. } else if (os_strcasecmp(token, "sched") == 0) {
  7606. type |= MAC_ADDR_RAND_SCHED_SCAN;
  7607. } else if (os_strcasecmp(token, "pno") == 0) {
  7608. type |= MAC_ADDR_RAND_PNO;
  7609. } else if (os_strcasecmp(token, "all") == 0) {
  7610. type = wpa_s->mac_addr_rand_supported;
  7611. } else if (os_strncasecmp(token, "enable=", 7) == 0) {
  7612. enable = atoi(token + 7);
  7613. } else if (os_strncasecmp(token, "addr=", 5) == 0) {
  7614. addr = _addr;
  7615. if (hwaddr_aton(token + 5, addr)) {
  7616. wpa_printf(MSG_INFO,
  7617. "CTRL: Invalid MAC address: %s",
  7618. token);
  7619. return -1;
  7620. }
  7621. } else if (os_strncasecmp(token, "mask=", 5) == 0) {
  7622. mask = _mask;
  7623. if (hwaddr_aton(token + 5, mask)) {
  7624. wpa_printf(MSG_INFO,
  7625. "CTRL: Invalid MAC address mask: %s",
  7626. token);
  7627. return -1;
  7628. }
  7629. } else {
  7630. wpa_printf(MSG_INFO,
  7631. "CTRL: Invalid MAC_RAND_SCAN parameter: %s",
  7632. token);
  7633. return -1;
  7634. }
  7635. }
  7636. if (!type) {
  7637. wpa_printf(MSG_INFO, "CTRL: MAC_RAND_SCAN no type specified");
  7638. return -1;
  7639. }
  7640. if ((wpa_s->mac_addr_rand_supported & type) != type) {
  7641. wpa_printf(MSG_INFO,
  7642. "CTRL: MAC_RAND_SCAN types=%u != supported=%u",
  7643. type, wpa_s->mac_addr_rand_supported);
  7644. return -1;
  7645. }
  7646. if (enable > 1) {
  7647. wpa_printf(MSG_INFO,
  7648. "CTRL: MAC_RAND_SCAN enable=<0/1> not specified");
  7649. return -1;
  7650. }
  7651. if (!enable) {
  7652. wpas_mac_addr_rand_scan_clear(wpa_s, type);
  7653. if (wpa_s->pno) {
  7654. if (type & MAC_ADDR_RAND_PNO) {
  7655. wpas_stop_pno(wpa_s);
  7656. wpas_start_pno(wpa_s);
  7657. }
  7658. } else if (wpa_s->sched_scanning &&
  7659. (type & MAC_ADDR_RAND_SCHED_SCAN)) {
  7660. wpas_scan_restart_sched_scan(wpa_s);
  7661. }
  7662. return 0;
  7663. }
  7664. if ((addr && !mask) || (!addr && mask)) {
  7665. wpa_printf(MSG_INFO,
  7666. "CTRL: MAC_RAND_SCAN invalid addr/mask combination");
  7667. return -1;
  7668. }
  7669. if (addr && mask && (!(mask[0] & 0x01) || (addr[0] & 0x01))) {
  7670. wpa_printf(MSG_INFO,
  7671. "CTRL: MAC_RAND_SCAN cannot allow multicast address");
  7672. return -1;
  7673. }
  7674. if (type & MAC_ADDR_RAND_SCAN) {
  7675. wpas_mac_addr_rand_scan_set(wpa_s, MAC_ADDR_RAND_SCAN,
  7676. addr, mask);
  7677. }
  7678. if (type & MAC_ADDR_RAND_SCHED_SCAN) {
  7679. wpas_mac_addr_rand_scan_set(wpa_s, MAC_ADDR_RAND_SCHED_SCAN,
  7680. addr, mask);
  7681. if (wpa_s->sched_scanning && !wpa_s->pno)
  7682. wpas_scan_restart_sched_scan(wpa_s);
  7683. }
  7684. if (type & MAC_ADDR_RAND_PNO) {
  7685. wpas_mac_addr_rand_scan_set(wpa_s, MAC_ADDR_RAND_PNO,
  7686. addr, mask);
  7687. if (wpa_s->pno) {
  7688. wpas_stop_pno(wpa_s);
  7689. wpas_start_pno(wpa_s);
  7690. }
  7691. }
  7692. return 0;
  7693. }
  7694. static int wpas_ctrl_iface_pmksa(struct wpa_supplicant *wpa_s,
  7695. char *buf, size_t buflen)
  7696. {
  7697. size_t reply_len;
  7698. reply_len = wpa_sm_pmksa_cache_list(wpa_s->wpa, buf, buflen);
  7699. #ifdef CONFIG_AP
  7700. reply_len += wpas_ap_pmksa_cache_list(wpa_s, &buf[reply_len],
  7701. buflen - reply_len);
  7702. #endif /* CONFIG_AP */
  7703. return reply_len;
  7704. }
  7705. static void wpas_ctrl_iface_pmksa_flush(struct wpa_supplicant *wpa_s)
  7706. {
  7707. wpa_sm_pmksa_cache_flush(wpa_s->wpa, NULL);
  7708. #ifdef CONFIG_AP
  7709. wpas_ap_pmksa_cache_flush(wpa_s);
  7710. #endif /* CONFIG_AP */
  7711. }
  7712. #ifdef CONFIG_PMKSA_CACHE_EXTERNAL
  7713. static int wpas_ctrl_iface_pmksa_get(struct wpa_supplicant *wpa_s,
  7714. const char *cmd, char *buf, size_t buflen)
  7715. {
  7716. struct rsn_pmksa_cache_entry *entry;
  7717. struct wpa_ssid *ssid;
  7718. char *pos, *pos2, *end;
  7719. int ret;
  7720. struct os_reltime now;
  7721. ssid = wpa_config_get_network(wpa_s->conf, atoi(cmd));
  7722. if (!ssid)
  7723. return -1;
  7724. pos = buf;
  7725. end = buf + buflen;
  7726. os_get_reltime(&now);
  7727. /*
  7728. * Entry format:
  7729. * <BSSID> <PMKID> <PMK> <reauth_time in seconds>
  7730. * <expiration in seconds> <akmp> <opportunistic>
  7731. * [FILS Cache Identifier]
  7732. */
  7733. for (entry = wpa_sm_pmksa_cache_head(wpa_s->wpa); entry;
  7734. entry = entry->next) {
  7735. if (entry->network_ctx != ssid)
  7736. continue;
  7737. pos2 = pos;
  7738. ret = os_snprintf(pos2, end - pos2, MACSTR " ",
  7739. MAC2STR(entry->aa));
  7740. if (os_snprintf_error(end - pos2, ret))
  7741. break;
  7742. pos2 += ret;
  7743. pos2 += wpa_snprintf_hex(pos2, end - pos2, entry->pmkid,
  7744. PMKID_LEN);
  7745. ret = os_snprintf(pos2, end - pos2, " ");
  7746. if (os_snprintf_error(end - pos2, ret))
  7747. break;
  7748. pos2 += ret;
  7749. pos2 += wpa_snprintf_hex(pos2, end - pos2, entry->pmk,
  7750. entry->pmk_len);
  7751. ret = os_snprintf(pos2, end - pos2, " %d %d %d %d",
  7752. (int) (entry->reauth_time - now.sec),
  7753. (int) (entry->expiration - now.sec),
  7754. entry->akmp,
  7755. entry->opportunistic);
  7756. if (os_snprintf_error(end - pos2, ret))
  7757. break;
  7758. pos2 += ret;
  7759. if (entry->fils_cache_id_set) {
  7760. ret = os_snprintf(pos2, end - pos2, " %02x%02x",
  7761. entry->fils_cache_id[0],
  7762. entry->fils_cache_id[1]);
  7763. if (os_snprintf_error(end - pos2, ret))
  7764. break;
  7765. pos2 += ret;
  7766. }
  7767. ret = os_snprintf(pos2, end - pos2, "\n");
  7768. if (os_snprintf_error(end - pos2, ret))
  7769. break;
  7770. pos2 += ret;
  7771. pos = pos2;
  7772. }
  7773. return pos - buf;
  7774. }
  7775. static int wpas_ctrl_iface_pmksa_add(struct wpa_supplicant *wpa_s,
  7776. char *cmd)
  7777. {
  7778. struct rsn_pmksa_cache_entry *entry;
  7779. struct wpa_ssid *ssid;
  7780. char *pos, *pos2;
  7781. int ret = -1;
  7782. struct os_reltime now;
  7783. int reauth_time = 0, expiration = 0, i;
  7784. /*
  7785. * Entry format:
  7786. * <network_id> <BSSID> <PMKID> <PMK> <reauth_time in seconds>
  7787. * <expiration in seconds> <akmp> <opportunistic>
  7788. * [FILS Cache Identifier]
  7789. */
  7790. ssid = wpa_config_get_network(wpa_s->conf, atoi(cmd));
  7791. if (!ssid)
  7792. return -1;
  7793. pos = os_strchr(cmd, ' ');
  7794. if (!pos)
  7795. return -1;
  7796. pos++;
  7797. entry = os_zalloc(sizeof(*entry));
  7798. if (!entry)
  7799. return -1;
  7800. if (hwaddr_aton(pos, entry->aa))
  7801. goto fail;
  7802. pos = os_strchr(pos, ' ');
  7803. if (!pos)
  7804. goto fail;
  7805. pos++;
  7806. if (hexstr2bin(pos, entry->pmkid, PMKID_LEN) < 0)
  7807. goto fail;
  7808. pos = os_strchr(pos, ' ');
  7809. if (!pos)
  7810. goto fail;
  7811. pos++;
  7812. pos2 = os_strchr(pos, ' ');
  7813. if (!pos2)
  7814. goto fail;
  7815. entry->pmk_len = (pos2 - pos) / 2;
  7816. if (entry->pmk_len < PMK_LEN || entry->pmk_len > PMK_LEN_MAX ||
  7817. hexstr2bin(pos, entry->pmk, entry->pmk_len) < 0)
  7818. goto fail;
  7819. pos = os_strchr(pos, ' ');
  7820. if (!pos)
  7821. goto fail;
  7822. pos++;
  7823. if (sscanf(pos, "%d %d %d %d", &reauth_time, &expiration,
  7824. &entry->akmp, &entry->opportunistic) != 4)
  7825. goto fail;
  7826. for (i = 0; i < 4; i++) {
  7827. pos = os_strchr(pos, ' ');
  7828. if (!pos) {
  7829. if (i < 3)
  7830. goto fail;
  7831. break;
  7832. }
  7833. pos++;
  7834. }
  7835. if (pos) {
  7836. if (hexstr2bin(pos, entry->fils_cache_id,
  7837. FILS_CACHE_ID_LEN) < 0)
  7838. goto fail;
  7839. entry->fils_cache_id_set = 1;
  7840. }
  7841. os_get_reltime(&now);
  7842. entry->expiration = now.sec + expiration;
  7843. entry->reauth_time = now.sec + reauth_time;
  7844. entry->network_ctx = ssid;
  7845. wpa_sm_pmksa_cache_add_entry(wpa_s->wpa, entry);
  7846. entry = NULL;
  7847. ret = 0;
  7848. fail:
  7849. os_free(entry);
  7850. return ret;
  7851. }
  7852. #ifdef CONFIG_MESH
  7853. static int wpas_ctrl_iface_mesh_pmksa_get(struct wpa_supplicant *wpa_s,
  7854. const char *cmd, char *buf,
  7855. size_t buflen)
  7856. {
  7857. u8 spa[ETH_ALEN];
  7858. if (!wpa_s->ifmsh)
  7859. return -1;
  7860. if (os_strcasecmp(cmd, "any") == 0)
  7861. return wpas_ap_pmksa_cache_list_mesh(wpa_s, NULL, buf, buflen);
  7862. if (hwaddr_aton(cmd, spa))
  7863. return -1;
  7864. return wpas_ap_pmksa_cache_list_mesh(wpa_s, spa, buf, buflen);
  7865. }
  7866. static int wpas_ctrl_iface_mesh_pmksa_add(struct wpa_supplicant *wpa_s,
  7867. char *cmd)
  7868. {
  7869. /*
  7870. * We do not check mesh interface existance because PMKSA should be
  7871. * stored before wpa_s->ifmsh creation to suppress commit message
  7872. * creation.
  7873. */
  7874. return wpas_ap_pmksa_cache_add_external(wpa_s, cmd);
  7875. }
  7876. #endif /* CONFIG_MESH */
  7877. #endif /* CONFIG_PMKSA_CACHE_EXTERNAL */
  7878. #ifdef CONFIG_FILS
  7879. static int wpas_ctrl_iface_fils_hlp_req_add(struct wpa_supplicant *wpa_s,
  7880. const char *cmd)
  7881. {
  7882. struct fils_hlp_req *req;
  7883. const char *pos;
  7884. /* format: <dst> <packet starting from ethertype> */
  7885. req = os_zalloc(sizeof(*req));
  7886. if (!req)
  7887. return -1;
  7888. if (hwaddr_aton(cmd, req->dst))
  7889. goto fail;
  7890. pos = os_strchr(cmd, ' ');
  7891. if (!pos)
  7892. goto fail;
  7893. pos++;
  7894. req->pkt = wpabuf_parse_bin(pos);
  7895. if (!req->pkt)
  7896. goto fail;
  7897. dl_list_add_tail(&wpa_s->fils_hlp_req, &req->list);
  7898. return 0;
  7899. fail:
  7900. wpabuf_free(req->pkt);
  7901. os_free(req);
  7902. return -1;
  7903. }
  7904. #endif /* CONFIG_FILS */
  7905. static int wpas_ctrl_cmd_debug_level(const char *cmd)
  7906. {
  7907. if (os_strcmp(cmd, "PING") == 0 ||
  7908. os_strncmp(cmd, "BSS ", 4) == 0 ||
  7909. os_strncmp(cmd, "GET_NETWORK ", 12) == 0 ||
  7910. os_strncmp(cmd, "STATUS", 6) == 0 ||
  7911. os_strncmp(cmd, "STA ", 4) == 0 ||
  7912. os_strncmp(cmd, "STA-", 4) == 0)
  7913. return MSG_EXCESSIVE;
  7914. return MSG_DEBUG;
  7915. }
  7916. char * wpa_supplicant_ctrl_iface_process(struct wpa_supplicant *wpa_s,
  7917. char *buf, size_t *resp_len)
  7918. {
  7919. char *reply;
  7920. const int reply_size = 4096;
  7921. int reply_len;
  7922. if (os_strncmp(buf, WPA_CTRL_RSP, os_strlen(WPA_CTRL_RSP)) == 0 ||
  7923. os_strncmp(buf, "SET_NETWORK ", 12) == 0 ||
  7924. os_strncmp(buf, "PMKSA_ADD ", 10) == 0 ||
  7925. os_strncmp(buf, "MESH_PMKSA_ADD ", 15) == 0) {
  7926. if (wpa_debug_show_keys)
  7927. wpa_dbg(wpa_s, MSG_DEBUG,
  7928. "Control interface command '%s'", buf);
  7929. else
  7930. wpa_dbg(wpa_s, MSG_DEBUG,
  7931. "Control interface command '%s [REMOVED]'",
  7932. os_strncmp(buf, WPA_CTRL_RSP,
  7933. os_strlen(WPA_CTRL_RSP)) == 0 ?
  7934. WPA_CTRL_RSP :
  7935. (os_strncmp(buf, "SET_NETWORK ", 12) == 0 ?
  7936. "SET_NETWORK" : "key-add"));
  7937. } else if (os_strncmp(buf, "WPS_NFC_TAG_READ", 16) == 0 ||
  7938. os_strncmp(buf, "NFC_REPORT_HANDOVER", 19) == 0) {
  7939. wpa_hexdump_ascii_key(MSG_DEBUG, "RX ctrl_iface",
  7940. (const u8 *) buf, os_strlen(buf));
  7941. } else {
  7942. int level = wpas_ctrl_cmd_debug_level(buf);
  7943. wpa_dbg(wpa_s, level, "Control interface command '%s'", buf);
  7944. }
  7945. reply = os_malloc(reply_size);
  7946. if (reply == NULL) {
  7947. *resp_len = 1;
  7948. return NULL;
  7949. }
  7950. os_memcpy(reply, "OK\n", 3);
  7951. reply_len = 3;
  7952. if (os_strcmp(buf, "PING") == 0) {
  7953. os_memcpy(reply, "PONG\n", 5);
  7954. reply_len = 5;
  7955. } else if (os_strcmp(buf, "IFNAME") == 0) {
  7956. reply_len = os_strlen(wpa_s->ifname);
  7957. os_memcpy(reply, wpa_s->ifname, reply_len);
  7958. } else if (os_strncmp(buf, "RELOG", 5) == 0) {
  7959. if (wpa_debug_reopen_file() < 0)
  7960. reply_len = -1;
  7961. } else if (os_strncmp(buf, "NOTE ", 5) == 0) {
  7962. wpa_printf(MSG_INFO, "NOTE: %s", buf + 5);
  7963. } else if (os_strcmp(buf, "MIB") == 0) {
  7964. reply_len = wpa_sm_get_mib(wpa_s->wpa, reply, reply_size);
  7965. if (reply_len >= 0) {
  7966. reply_len += eapol_sm_get_mib(wpa_s->eapol,
  7967. reply + reply_len,
  7968. reply_size - reply_len);
  7969. }
  7970. } else if (os_strncmp(buf, "STATUS", 6) == 0) {
  7971. reply_len = wpa_supplicant_ctrl_iface_status(
  7972. wpa_s, buf + 6, reply, reply_size);
  7973. } else if (os_strcmp(buf, "PMKSA") == 0) {
  7974. reply_len = wpas_ctrl_iface_pmksa(wpa_s, reply, reply_size);
  7975. } else if (os_strcmp(buf, "PMKSA_FLUSH") == 0) {
  7976. wpas_ctrl_iface_pmksa_flush(wpa_s);
  7977. #ifdef CONFIG_PMKSA_CACHE_EXTERNAL
  7978. } else if (os_strncmp(buf, "PMKSA_GET ", 10) == 0) {
  7979. reply_len = wpas_ctrl_iface_pmksa_get(wpa_s, buf + 10,
  7980. reply, reply_size);
  7981. } else if (os_strncmp(buf, "PMKSA_ADD ", 10) == 0) {
  7982. if (wpas_ctrl_iface_pmksa_add(wpa_s, buf + 10) < 0)
  7983. reply_len = -1;
  7984. #ifdef CONFIG_MESH
  7985. } else if (os_strncmp(buf, "MESH_PMKSA_GET ", 15) == 0) {
  7986. reply_len = wpas_ctrl_iface_mesh_pmksa_get(wpa_s, buf + 15,
  7987. reply, reply_size);
  7988. } else if (os_strncmp(buf, "MESH_PMKSA_ADD ", 15) == 0) {
  7989. if (wpas_ctrl_iface_mesh_pmksa_add(wpa_s, buf + 15) < 0)
  7990. reply_len = -1;
  7991. #endif /* CONFIG_MESH */
  7992. #endif /* CONFIG_PMKSA_CACHE_EXTERNAL */
  7993. } else if (os_strncmp(buf, "SET ", 4) == 0) {
  7994. if (wpa_supplicant_ctrl_iface_set(wpa_s, buf + 4))
  7995. reply_len = -1;
  7996. } else if (os_strncmp(buf, "DUMP", 4) == 0) {
  7997. reply_len = wpa_config_dump_values(wpa_s->conf,
  7998. reply, reply_size);
  7999. } else if (os_strncmp(buf, "GET ", 4) == 0) {
  8000. reply_len = wpa_supplicant_ctrl_iface_get(wpa_s, buf + 4,
  8001. reply, reply_size);
  8002. } else if (os_strcmp(buf, "LOGON") == 0) {
  8003. eapol_sm_notify_logoff(wpa_s->eapol, FALSE);
  8004. } else if (os_strcmp(buf, "LOGOFF") == 0) {
  8005. eapol_sm_notify_logoff(wpa_s->eapol, TRUE);
  8006. } else if (os_strcmp(buf, "REASSOCIATE") == 0) {
  8007. if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED)
  8008. reply_len = -1;
  8009. else
  8010. wpas_request_connection(wpa_s);
  8011. } else if (os_strcmp(buf, "REATTACH") == 0) {
  8012. if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED ||
  8013. !wpa_s->current_ssid)
  8014. reply_len = -1;
  8015. else {
  8016. wpa_s->reattach = 1;
  8017. wpas_request_connection(wpa_s);
  8018. }
  8019. } else if (os_strcmp(buf, "RECONNECT") == 0) {
  8020. if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED)
  8021. reply_len = -1;
  8022. else if (wpa_s->disconnected)
  8023. wpas_request_connection(wpa_s);
  8024. #ifdef IEEE8021X_EAPOL
  8025. } else if (os_strncmp(buf, "PREAUTH ", 8) == 0) {
  8026. if (wpa_supplicant_ctrl_iface_preauth(wpa_s, buf + 8))
  8027. reply_len = -1;
  8028. #endif /* IEEE8021X_EAPOL */
  8029. #ifdef CONFIG_PEERKEY
  8030. } else if (os_strncmp(buf, "STKSTART ", 9) == 0) {
  8031. if (wpa_supplicant_ctrl_iface_stkstart(wpa_s, buf + 9))
  8032. reply_len = -1;
  8033. #endif /* CONFIG_PEERKEY */
  8034. #ifdef CONFIG_IEEE80211R
  8035. } else if (os_strncmp(buf, "FT_DS ", 6) == 0) {
  8036. if (wpa_supplicant_ctrl_iface_ft_ds(wpa_s, buf + 6))
  8037. reply_len = -1;
  8038. #endif /* CONFIG_IEEE80211R */
  8039. #ifdef CONFIG_WPS
  8040. } else if (os_strcmp(buf, "WPS_PBC") == 0) {
  8041. int res = wpa_supplicant_ctrl_iface_wps_pbc(wpa_s, NULL);
  8042. if (res == -2) {
  8043. os_memcpy(reply, "FAIL-PBC-OVERLAP\n", 17);
  8044. reply_len = 17;
  8045. } else if (res)
  8046. reply_len = -1;
  8047. } else if (os_strncmp(buf, "WPS_PBC ", 8) == 0) {
  8048. int res = wpa_supplicant_ctrl_iface_wps_pbc(wpa_s, buf + 8);
  8049. if (res == -2) {
  8050. os_memcpy(reply, "FAIL-PBC-OVERLAP\n", 17);
  8051. reply_len = 17;
  8052. } else if (res)
  8053. reply_len = -1;
  8054. } else if (os_strncmp(buf, "WPS_PIN ", 8) == 0) {
  8055. reply_len = wpa_supplicant_ctrl_iface_wps_pin(wpa_s, buf + 8,
  8056. reply,
  8057. reply_size);
  8058. } else if (os_strncmp(buf, "WPS_CHECK_PIN ", 14) == 0) {
  8059. reply_len = wpa_supplicant_ctrl_iface_wps_check_pin(
  8060. wpa_s, buf + 14, reply, reply_size);
  8061. } else if (os_strcmp(buf, "WPS_CANCEL") == 0) {
  8062. if (wpas_wps_cancel(wpa_s))
  8063. reply_len = -1;
  8064. #ifdef CONFIG_WPS_NFC
  8065. } else if (os_strcmp(buf, "WPS_NFC") == 0) {
  8066. if (wpa_supplicant_ctrl_iface_wps_nfc(wpa_s, NULL))
  8067. reply_len = -1;
  8068. } else if (os_strncmp(buf, "WPS_NFC ", 8) == 0) {
  8069. if (wpa_supplicant_ctrl_iface_wps_nfc(wpa_s, buf + 8))
  8070. reply_len = -1;
  8071. } else if (os_strncmp(buf, "WPS_NFC_CONFIG_TOKEN ", 21) == 0) {
  8072. reply_len = wpa_supplicant_ctrl_iface_wps_nfc_config_token(
  8073. wpa_s, buf + 21, reply, reply_size);
  8074. } else if (os_strncmp(buf, "WPS_NFC_TOKEN ", 14) == 0) {
  8075. reply_len = wpa_supplicant_ctrl_iface_wps_nfc_token(
  8076. wpa_s, buf + 14, reply, reply_size);
  8077. } else if (os_strncmp(buf, "WPS_NFC_TAG_READ ", 17) == 0) {
  8078. if (wpa_supplicant_ctrl_iface_wps_nfc_tag_read(wpa_s,
  8079. buf + 17))
  8080. reply_len = -1;
  8081. } else if (os_strncmp(buf, "NFC_GET_HANDOVER_REQ ", 21) == 0) {
  8082. reply_len = wpas_ctrl_nfc_get_handover_req(
  8083. wpa_s, buf + 21, reply, reply_size);
  8084. } else if (os_strncmp(buf, "NFC_GET_HANDOVER_SEL ", 21) == 0) {
  8085. reply_len = wpas_ctrl_nfc_get_handover_sel(
  8086. wpa_s, buf + 21, reply, reply_size);
  8087. } else if (os_strncmp(buf, "NFC_REPORT_HANDOVER ", 20) == 0) {
  8088. if (wpas_ctrl_nfc_report_handover(wpa_s, buf + 20))
  8089. reply_len = -1;
  8090. #endif /* CONFIG_WPS_NFC */
  8091. } else if (os_strncmp(buf, "WPS_REG ", 8) == 0) {
  8092. if (wpa_supplicant_ctrl_iface_wps_reg(wpa_s, buf + 8))
  8093. reply_len = -1;
  8094. #ifdef CONFIG_AP
  8095. } else if (os_strncmp(buf, "WPS_AP_PIN ", 11) == 0) {
  8096. reply_len = wpa_supplicant_ctrl_iface_wps_ap_pin(
  8097. wpa_s, buf + 11, reply, reply_size);
  8098. #endif /* CONFIG_AP */
  8099. #ifdef CONFIG_WPS_ER
  8100. } else if (os_strcmp(buf, "WPS_ER_START") == 0) {
  8101. if (wpas_wps_er_start(wpa_s, NULL))
  8102. reply_len = -1;
  8103. } else if (os_strncmp(buf, "WPS_ER_START ", 13) == 0) {
  8104. if (wpas_wps_er_start(wpa_s, buf + 13))
  8105. reply_len = -1;
  8106. } else if (os_strcmp(buf, "WPS_ER_STOP") == 0) {
  8107. wpas_wps_er_stop(wpa_s);
  8108. } else if (os_strncmp(buf, "WPS_ER_PIN ", 11) == 0) {
  8109. if (wpa_supplicant_ctrl_iface_wps_er_pin(wpa_s, buf + 11))
  8110. reply_len = -1;
  8111. } else if (os_strncmp(buf, "WPS_ER_PBC ", 11) == 0) {
  8112. int ret = wpas_wps_er_pbc(wpa_s, buf + 11);
  8113. if (ret == -2) {
  8114. os_memcpy(reply, "FAIL-PBC-OVERLAP\n", 17);
  8115. reply_len = 17;
  8116. } else if (ret == -3) {
  8117. os_memcpy(reply, "FAIL-UNKNOWN-UUID\n", 18);
  8118. reply_len = 18;
  8119. } else if (ret == -4) {
  8120. os_memcpy(reply, "FAIL-NO-AP-SETTINGS\n", 20);
  8121. reply_len = 20;
  8122. } else if (ret)
  8123. reply_len = -1;
  8124. } else if (os_strncmp(buf, "WPS_ER_LEARN ", 13) == 0) {
  8125. if (wpa_supplicant_ctrl_iface_wps_er_learn(wpa_s, buf + 13))
  8126. reply_len = -1;
  8127. } else if (os_strncmp(buf, "WPS_ER_SET_CONFIG ", 18) == 0) {
  8128. if (wpa_supplicant_ctrl_iface_wps_er_set_config(wpa_s,
  8129. buf + 18))
  8130. reply_len = -1;
  8131. } else if (os_strncmp(buf, "WPS_ER_CONFIG ", 14) == 0) {
  8132. if (wpa_supplicant_ctrl_iface_wps_er_config(wpa_s, buf + 14))
  8133. reply_len = -1;
  8134. #ifdef CONFIG_WPS_NFC
  8135. } else if (os_strncmp(buf, "WPS_ER_NFC_CONFIG_TOKEN ", 24) == 0) {
  8136. reply_len = wpa_supplicant_ctrl_iface_wps_er_nfc_config_token(
  8137. wpa_s, buf + 24, reply, reply_size);
  8138. #endif /* CONFIG_WPS_NFC */
  8139. #endif /* CONFIG_WPS_ER */
  8140. #endif /* CONFIG_WPS */
  8141. #ifdef CONFIG_IBSS_RSN
  8142. } else if (os_strncmp(buf, "IBSS_RSN ", 9) == 0) {
  8143. if (wpa_supplicant_ctrl_iface_ibss_rsn(wpa_s, buf + 9))
  8144. reply_len = -1;
  8145. #endif /* CONFIG_IBSS_RSN */
  8146. #ifdef CONFIG_MESH
  8147. } else if (os_strncmp(buf, "MESH_INTERFACE_ADD ", 19) == 0) {
  8148. reply_len = wpa_supplicant_ctrl_iface_mesh_interface_add(
  8149. wpa_s, buf + 19, reply, reply_size);
  8150. } else if (os_strcmp(buf, "MESH_INTERFACE_ADD") == 0) {
  8151. reply_len = wpa_supplicant_ctrl_iface_mesh_interface_add(
  8152. wpa_s, "", reply, reply_size);
  8153. } else if (os_strncmp(buf, "MESH_GROUP_ADD ", 15) == 0) {
  8154. if (wpa_supplicant_ctrl_iface_mesh_group_add(wpa_s, buf + 15))
  8155. reply_len = -1;
  8156. } else if (os_strncmp(buf, "MESH_GROUP_REMOVE ", 18) == 0) {
  8157. if (wpa_supplicant_ctrl_iface_mesh_group_remove(wpa_s,
  8158. buf + 18))
  8159. reply_len = -1;
  8160. } else if (os_strncmp(buf, "MESH_PEER_REMOVE ", 17) == 0) {
  8161. if (wpa_supplicant_ctrl_iface_mesh_peer_remove(wpa_s, buf + 17))
  8162. reply_len = -1;
  8163. } else if (os_strncmp(buf, "MESH_PEER_ADD ", 14) == 0) {
  8164. if (wpa_supplicant_ctrl_iface_mesh_peer_add(wpa_s, buf + 14))
  8165. reply_len = -1;
  8166. #endif /* CONFIG_MESH */
  8167. #ifdef CONFIG_P2P
  8168. } else if (os_strncmp(buf, "P2P_FIND ", 9) == 0) {
  8169. if (p2p_ctrl_find(wpa_s, buf + 8))
  8170. reply_len = -1;
  8171. } else if (os_strcmp(buf, "P2P_FIND") == 0) {
  8172. if (p2p_ctrl_find(wpa_s, ""))
  8173. reply_len = -1;
  8174. } else if (os_strcmp(buf, "P2P_STOP_FIND") == 0) {
  8175. wpas_p2p_stop_find(wpa_s);
  8176. } else if (os_strncmp(buf, "P2P_ASP_PROVISION ", 18) == 0) {
  8177. if (p2p_ctrl_asp_provision(wpa_s, buf + 18))
  8178. reply_len = -1;
  8179. } else if (os_strncmp(buf, "P2P_ASP_PROVISION_RESP ", 23) == 0) {
  8180. if (p2p_ctrl_asp_provision_resp(wpa_s, buf + 23))
  8181. reply_len = -1;
  8182. } else if (os_strncmp(buf, "P2P_CONNECT ", 12) == 0) {
  8183. reply_len = p2p_ctrl_connect(wpa_s, buf + 12, reply,
  8184. reply_size);
  8185. } else if (os_strncmp(buf, "P2P_LISTEN ", 11) == 0) {
  8186. if (p2p_ctrl_listen(wpa_s, buf + 11))
  8187. reply_len = -1;
  8188. } else if (os_strcmp(buf, "P2P_LISTEN") == 0) {
  8189. if (p2p_ctrl_listen(wpa_s, ""))
  8190. reply_len = -1;
  8191. } else if (os_strncmp(buf, "P2P_GROUP_REMOVE ", 17) == 0) {
  8192. if (wpas_p2p_group_remove(wpa_s, buf + 17))
  8193. reply_len = -1;
  8194. } else if (os_strcmp(buf, "P2P_GROUP_ADD") == 0) {
  8195. if (p2p_ctrl_group_add(wpa_s, ""))
  8196. reply_len = -1;
  8197. } else if (os_strncmp(buf, "P2P_GROUP_ADD ", 14) == 0) {
  8198. if (p2p_ctrl_group_add(wpa_s, buf + 14))
  8199. reply_len = -1;
  8200. } else if (os_strncmp(buf, "P2P_GROUP_MEMBER ", 17) == 0) {
  8201. reply_len = p2p_ctrl_group_member(wpa_s, buf + 17, reply,
  8202. reply_size);
  8203. } else if (os_strncmp(buf, "P2P_PROV_DISC ", 14) == 0) {
  8204. if (p2p_ctrl_prov_disc(wpa_s, buf + 14))
  8205. reply_len = -1;
  8206. } else if (os_strcmp(buf, "P2P_GET_PASSPHRASE") == 0) {
  8207. reply_len = p2p_get_passphrase(wpa_s, reply, reply_size);
  8208. } else if (os_strncmp(buf, "P2P_SERV_DISC_REQ ", 18) == 0) {
  8209. reply_len = p2p_ctrl_serv_disc_req(wpa_s, buf + 18, reply,
  8210. reply_size);
  8211. } else if (os_strncmp(buf, "P2P_SERV_DISC_CANCEL_REQ ", 25) == 0) {
  8212. if (p2p_ctrl_serv_disc_cancel_req(wpa_s, buf + 25) < 0)
  8213. reply_len = -1;
  8214. } else if (os_strncmp(buf, "P2P_SERV_DISC_RESP ", 19) == 0) {
  8215. if (p2p_ctrl_serv_disc_resp(wpa_s, buf + 19) < 0)
  8216. reply_len = -1;
  8217. } else if (os_strcmp(buf, "P2P_SERVICE_UPDATE") == 0) {
  8218. wpas_p2p_sd_service_update(wpa_s);
  8219. } else if (os_strncmp(buf, "P2P_SERV_DISC_EXTERNAL ", 23) == 0) {
  8220. if (p2p_ctrl_serv_disc_external(wpa_s, buf + 23) < 0)
  8221. reply_len = -1;
  8222. } else if (os_strcmp(buf, "P2P_SERVICE_FLUSH") == 0) {
  8223. wpas_p2p_service_flush(wpa_s);
  8224. } else if (os_strncmp(buf, "P2P_SERVICE_ADD ", 16) == 0) {
  8225. if (p2p_ctrl_service_add(wpa_s, buf + 16) < 0)
  8226. reply_len = -1;
  8227. } else if (os_strncmp(buf, "P2P_SERVICE_DEL ", 16) == 0) {
  8228. if (p2p_ctrl_service_del(wpa_s, buf + 16) < 0)
  8229. reply_len = -1;
  8230. } else if (os_strncmp(buf, "P2P_SERVICE_REP ", 16) == 0) {
  8231. if (p2p_ctrl_service_replace(wpa_s, buf + 16) < 0)
  8232. reply_len = -1;
  8233. } else if (os_strncmp(buf, "P2P_REJECT ", 11) == 0) {
  8234. if (p2p_ctrl_reject(wpa_s, buf + 11) < 0)
  8235. reply_len = -1;
  8236. } else if (os_strncmp(buf, "P2P_INVITE ", 11) == 0) {
  8237. if (p2p_ctrl_invite(wpa_s, buf + 11) < 0)
  8238. reply_len = -1;
  8239. } else if (os_strncmp(buf, "P2P_PEER ", 9) == 0) {
  8240. reply_len = p2p_ctrl_peer(wpa_s, buf + 9, reply,
  8241. reply_size);
  8242. } else if (os_strncmp(buf, "P2P_SET ", 8) == 0) {
  8243. if (p2p_ctrl_set(wpa_s, buf + 8) < 0)
  8244. reply_len = -1;
  8245. } else if (os_strcmp(buf, "P2P_FLUSH") == 0) {
  8246. p2p_ctrl_flush(wpa_s);
  8247. } else if (os_strncmp(buf, "P2P_UNAUTHORIZE ", 16) == 0) {
  8248. if (wpas_p2p_unauthorize(wpa_s, buf + 16) < 0)
  8249. reply_len = -1;
  8250. } else if (os_strcmp(buf, "P2P_CANCEL") == 0) {
  8251. if (wpas_p2p_cancel(wpa_s))
  8252. reply_len = -1;
  8253. } else if (os_strncmp(buf, "P2P_PRESENCE_REQ ", 17) == 0) {
  8254. if (p2p_ctrl_presence_req(wpa_s, buf + 17) < 0)
  8255. reply_len = -1;
  8256. } else if (os_strcmp(buf, "P2P_PRESENCE_REQ") == 0) {
  8257. if (p2p_ctrl_presence_req(wpa_s, "") < 0)
  8258. reply_len = -1;
  8259. } else if (os_strncmp(buf, "P2P_EXT_LISTEN ", 15) == 0) {
  8260. if (p2p_ctrl_ext_listen(wpa_s, buf + 15) < 0)
  8261. reply_len = -1;
  8262. } else if (os_strcmp(buf, "P2P_EXT_LISTEN") == 0) {
  8263. if (p2p_ctrl_ext_listen(wpa_s, "") < 0)
  8264. reply_len = -1;
  8265. } else if (os_strncmp(buf, "P2P_REMOVE_CLIENT ", 18) == 0) {
  8266. if (p2p_ctrl_remove_client(wpa_s, buf + 18) < 0)
  8267. reply_len = -1;
  8268. } else if (os_strncmp(buf, "P2P_LO_START ", 13) == 0) {
  8269. if (p2p_ctrl_iface_p2p_lo_start(wpa_s, buf + 13))
  8270. reply_len = -1;
  8271. } else if (os_strcmp(buf, "P2P_LO_STOP") == 0) {
  8272. if (wpas_p2p_lo_stop(wpa_s))
  8273. reply_len = -1;
  8274. #endif /* CONFIG_P2P */
  8275. #ifdef CONFIG_WIFI_DISPLAY
  8276. } else if (os_strncmp(buf, "WFD_SUBELEM_SET ", 16) == 0) {
  8277. if (wifi_display_subelem_set(wpa_s->global, buf + 16) < 0)
  8278. reply_len = -1;
  8279. } else if (os_strncmp(buf, "WFD_SUBELEM_GET ", 16) == 0) {
  8280. reply_len = wifi_display_subelem_get(wpa_s->global, buf + 16,
  8281. reply, reply_size);
  8282. #endif /* CONFIG_WIFI_DISPLAY */
  8283. #ifdef CONFIG_INTERWORKING
  8284. } else if (os_strcmp(buf, "FETCH_ANQP") == 0) {
  8285. if (interworking_fetch_anqp(wpa_s) < 0)
  8286. reply_len = -1;
  8287. } else if (os_strcmp(buf, "STOP_FETCH_ANQP") == 0) {
  8288. interworking_stop_fetch_anqp(wpa_s);
  8289. } else if (os_strcmp(buf, "INTERWORKING_SELECT") == 0) {
  8290. if (ctrl_interworking_select(wpa_s, NULL) < 0)
  8291. reply_len = -1;
  8292. } else if (os_strncmp(buf, "INTERWORKING_SELECT ", 20) == 0) {
  8293. if (ctrl_interworking_select(wpa_s, buf + 20) < 0)
  8294. reply_len = -1;
  8295. } else if (os_strncmp(buf, "INTERWORKING_CONNECT ", 21) == 0) {
  8296. if (ctrl_interworking_connect(wpa_s, buf + 21, 0) < 0)
  8297. reply_len = -1;
  8298. } else if (os_strncmp(buf, "INTERWORKING_ADD_NETWORK ", 25) == 0) {
  8299. int id;
  8300. id = ctrl_interworking_connect(wpa_s, buf + 25, 1);
  8301. if (id < 0)
  8302. reply_len = -1;
  8303. else {
  8304. reply_len = os_snprintf(reply, reply_size, "%d\n", id);
  8305. if (os_snprintf_error(reply_size, reply_len))
  8306. reply_len = -1;
  8307. }
  8308. } else if (os_strncmp(buf, "ANQP_GET ", 9) == 0) {
  8309. if (get_anqp(wpa_s, buf + 9) < 0)
  8310. reply_len = -1;
  8311. } else if (os_strncmp(buf, "GAS_REQUEST ", 12) == 0) {
  8312. if (gas_request(wpa_s, buf + 12) < 0)
  8313. reply_len = -1;
  8314. } else if (os_strncmp(buf, "GAS_RESPONSE_GET ", 17) == 0) {
  8315. reply_len = gas_response_get(wpa_s, buf + 17, reply,
  8316. reply_size);
  8317. #endif /* CONFIG_INTERWORKING */
  8318. #ifdef CONFIG_HS20
  8319. } else if (os_strncmp(buf, "HS20_ANQP_GET ", 14) == 0) {
  8320. if (get_hs20_anqp(wpa_s, buf + 14) < 0)
  8321. reply_len = -1;
  8322. } else if (os_strncmp(buf, "HS20_GET_NAI_HOME_REALM_LIST ", 29) == 0) {
  8323. if (hs20_get_nai_home_realm_list(wpa_s, buf + 29) < 0)
  8324. reply_len = -1;
  8325. } else if (os_strncmp(buf, "HS20_ICON_REQUEST ", 18) == 0) {
  8326. if (hs20_icon_request(wpa_s, buf + 18, 0) < 0)
  8327. reply_len = -1;
  8328. } else if (os_strncmp(buf, "REQ_HS20_ICON ", 14) == 0) {
  8329. if (hs20_icon_request(wpa_s, buf + 14, 1) < 0)
  8330. reply_len = -1;
  8331. } else if (os_strncmp(buf, "GET_HS20_ICON ", 14) == 0) {
  8332. reply_len = get_hs20_icon(wpa_s, buf + 14, reply, reply_size);
  8333. } else if (os_strncmp(buf, "DEL_HS20_ICON ", 14) == 0) {
  8334. if (del_hs20_icon(wpa_s, buf + 14) < 0)
  8335. reply_len = -1;
  8336. } else if (os_strcmp(buf, "FETCH_OSU") == 0) {
  8337. if (hs20_fetch_osu(wpa_s, 0) < 0)
  8338. reply_len = -1;
  8339. } else if (os_strcmp(buf, "FETCH_OSU no-scan") == 0) {
  8340. if (hs20_fetch_osu(wpa_s, 1) < 0)
  8341. reply_len = -1;
  8342. } else if (os_strcmp(buf, "CANCEL_FETCH_OSU") == 0) {
  8343. hs20_cancel_fetch_osu(wpa_s);
  8344. #endif /* CONFIG_HS20 */
  8345. } else if (os_strncmp(buf, WPA_CTRL_RSP, os_strlen(WPA_CTRL_RSP)) == 0)
  8346. {
  8347. if (wpa_supplicant_ctrl_iface_ctrl_rsp(
  8348. wpa_s, buf + os_strlen(WPA_CTRL_RSP)))
  8349. reply_len = -1;
  8350. else {
  8351. /*
  8352. * Notify response from timeout to allow the control
  8353. * interface response to be sent first.
  8354. */
  8355. eloop_register_timeout(0, 0, wpas_ctrl_eapol_response,
  8356. wpa_s, NULL);
  8357. }
  8358. } else if (os_strcmp(buf, "RECONFIGURE") == 0) {
  8359. if (wpa_supplicant_reload_configuration(wpa_s))
  8360. reply_len = -1;
  8361. } else if (os_strcmp(buf, "TERMINATE") == 0) {
  8362. wpa_supplicant_terminate_proc(wpa_s->global);
  8363. } else if (os_strncmp(buf, "BSSID ", 6) == 0) {
  8364. if (wpa_supplicant_ctrl_iface_bssid(wpa_s, buf + 6))
  8365. reply_len = -1;
  8366. } else if (os_strncmp(buf, "BLACKLIST", 9) == 0) {
  8367. reply_len = wpa_supplicant_ctrl_iface_blacklist(
  8368. wpa_s, buf + 9, reply, reply_size);
  8369. } else if (os_strncmp(buf, "LOG_LEVEL", 9) == 0) {
  8370. reply_len = wpa_supplicant_ctrl_iface_log_level(
  8371. wpa_s, buf + 9, reply, reply_size);
  8372. } else if (os_strncmp(buf, "LIST_NETWORKS ", 14) == 0) {
  8373. reply_len = wpa_supplicant_ctrl_iface_list_networks(
  8374. wpa_s, buf + 14, reply, reply_size);
  8375. } else if (os_strcmp(buf, "LIST_NETWORKS") == 0) {
  8376. reply_len = wpa_supplicant_ctrl_iface_list_networks(
  8377. wpa_s, NULL, reply, reply_size);
  8378. } else if (os_strcmp(buf, "DISCONNECT") == 0) {
  8379. wpas_request_disconnection(wpa_s);
  8380. } else if (os_strcmp(buf, "SCAN") == 0) {
  8381. wpas_ctrl_scan(wpa_s, NULL, reply, reply_size, &reply_len);
  8382. } else if (os_strncmp(buf, "SCAN ", 5) == 0) {
  8383. wpas_ctrl_scan(wpa_s, buf + 5, reply, reply_size, &reply_len);
  8384. } else if (os_strcmp(buf, "SCAN_RESULTS") == 0) {
  8385. reply_len = wpa_supplicant_ctrl_iface_scan_results(
  8386. wpa_s, reply, reply_size);
  8387. } else if (os_strcmp(buf, "ABORT_SCAN") == 0) {
  8388. if (wpas_abort_ongoing_scan(wpa_s) < 0)
  8389. reply_len = -1;
  8390. } else if (os_strncmp(buf, "SELECT_NETWORK ", 15) == 0) {
  8391. if (wpa_supplicant_ctrl_iface_select_network(wpa_s, buf + 15))
  8392. reply_len = -1;
  8393. } else if (os_strncmp(buf, "ENABLE_NETWORK ", 15) == 0) {
  8394. if (wpa_supplicant_ctrl_iface_enable_network(wpa_s, buf + 15))
  8395. reply_len = -1;
  8396. } else if (os_strncmp(buf, "DISABLE_NETWORK ", 16) == 0) {
  8397. if (wpa_supplicant_ctrl_iface_disable_network(wpa_s, buf + 16))
  8398. reply_len = -1;
  8399. } else if (os_strcmp(buf, "ADD_NETWORK") == 0) {
  8400. reply_len = wpa_supplicant_ctrl_iface_add_network(
  8401. wpa_s, reply, reply_size);
  8402. } else if (os_strncmp(buf, "REMOVE_NETWORK ", 15) == 0) {
  8403. if (wpa_supplicant_ctrl_iface_remove_network(wpa_s, buf + 15))
  8404. reply_len = -1;
  8405. } else if (os_strncmp(buf, "SET_NETWORK ", 12) == 0) {
  8406. if (wpa_supplicant_ctrl_iface_set_network(wpa_s, buf + 12))
  8407. reply_len = -1;
  8408. } else if (os_strncmp(buf, "GET_NETWORK ", 12) == 0) {
  8409. reply_len = wpa_supplicant_ctrl_iface_get_network(
  8410. wpa_s, buf + 12, reply, reply_size);
  8411. } else if (os_strncmp(buf, "DUP_NETWORK ", 12) == 0) {
  8412. if (wpa_supplicant_ctrl_iface_dup_network(wpa_s, buf + 12,
  8413. wpa_s))
  8414. reply_len = -1;
  8415. } else if (os_strcmp(buf, "LIST_CREDS") == 0) {
  8416. reply_len = wpa_supplicant_ctrl_iface_list_creds(
  8417. wpa_s, reply, reply_size);
  8418. } else if (os_strcmp(buf, "ADD_CRED") == 0) {
  8419. reply_len = wpa_supplicant_ctrl_iface_add_cred(
  8420. wpa_s, reply, reply_size);
  8421. } else if (os_strncmp(buf, "REMOVE_CRED ", 12) == 0) {
  8422. if (wpa_supplicant_ctrl_iface_remove_cred(wpa_s, buf + 12))
  8423. reply_len = -1;
  8424. } else if (os_strncmp(buf, "SET_CRED ", 9) == 0) {
  8425. if (wpa_supplicant_ctrl_iface_set_cred(wpa_s, buf + 9))
  8426. reply_len = -1;
  8427. } else if (os_strncmp(buf, "GET_CRED ", 9) == 0) {
  8428. reply_len = wpa_supplicant_ctrl_iface_get_cred(wpa_s, buf + 9,
  8429. reply,
  8430. reply_size);
  8431. #ifndef CONFIG_NO_CONFIG_WRITE
  8432. } else if (os_strcmp(buf, "SAVE_CONFIG") == 0) {
  8433. if (wpa_supplicant_ctrl_iface_save_config(wpa_s))
  8434. reply_len = -1;
  8435. #endif /* CONFIG_NO_CONFIG_WRITE */
  8436. } else if (os_strncmp(buf, "GET_CAPABILITY ", 15) == 0) {
  8437. reply_len = wpa_supplicant_ctrl_iface_get_capability(
  8438. wpa_s, buf + 15, reply, reply_size);
  8439. } else if (os_strncmp(buf, "AP_SCAN ", 8) == 0) {
  8440. if (wpa_supplicant_ctrl_iface_ap_scan(wpa_s, buf + 8))
  8441. reply_len = -1;
  8442. } else if (os_strncmp(buf, "SCAN_INTERVAL ", 14) == 0) {
  8443. if (wpa_supplicant_ctrl_iface_scan_interval(wpa_s, buf + 14))
  8444. reply_len = -1;
  8445. } else if (os_strcmp(buf, "INTERFACE_LIST") == 0) {
  8446. reply_len = wpa_supplicant_global_iface_list(
  8447. wpa_s->global, reply, reply_size);
  8448. } else if (os_strncmp(buf, "INTERFACES", 10) == 0) {
  8449. reply_len = wpa_supplicant_global_iface_interfaces(
  8450. wpa_s->global, buf + 10, reply, reply_size);
  8451. } else if (os_strncmp(buf, "BSS ", 4) == 0) {
  8452. reply_len = wpa_supplicant_ctrl_iface_bss(
  8453. wpa_s, buf + 4, reply, reply_size);
  8454. #ifdef CONFIG_AP
  8455. } else if (os_strcmp(buf, "STA-FIRST") == 0) {
  8456. reply_len = ap_ctrl_iface_sta_first(wpa_s, reply, reply_size);
  8457. } else if (os_strncmp(buf, "STA ", 4) == 0) {
  8458. reply_len = ap_ctrl_iface_sta(wpa_s, buf + 4, reply,
  8459. reply_size);
  8460. } else if (os_strncmp(buf, "STA-NEXT ", 9) == 0) {
  8461. reply_len = ap_ctrl_iface_sta_next(wpa_s, buf + 9, reply,
  8462. reply_size);
  8463. } else if (os_strncmp(buf, "DEAUTHENTICATE ", 15) == 0) {
  8464. if (ap_ctrl_iface_sta_deauthenticate(wpa_s, buf + 15))
  8465. reply_len = -1;
  8466. } else if (os_strncmp(buf, "DISASSOCIATE ", 13) == 0) {
  8467. if (ap_ctrl_iface_sta_disassociate(wpa_s, buf + 13))
  8468. reply_len = -1;
  8469. } else if (os_strncmp(buf, "CHAN_SWITCH ", 12) == 0) {
  8470. if (ap_ctrl_iface_chanswitch(wpa_s, buf + 12))
  8471. reply_len = -1;
  8472. } else if (os_strcmp(buf, "STOP_AP") == 0) {
  8473. if (wpas_ap_stop_ap(wpa_s))
  8474. reply_len = -1;
  8475. #endif /* CONFIG_AP */
  8476. } else if (os_strcmp(buf, "SUSPEND") == 0) {
  8477. wpas_notify_suspend(wpa_s->global);
  8478. } else if (os_strcmp(buf, "RESUME") == 0) {
  8479. wpas_notify_resume(wpa_s->global);
  8480. #ifdef CONFIG_TESTING_OPTIONS
  8481. } else if (os_strcmp(buf, "DROP_SA") == 0) {
  8482. wpa_supplicant_ctrl_iface_drop_sa(wpa_s);
  8483. #endif /* CONFIG_TESTING_OPTIONS */
  8484. } else if (os_strncmp(buf, "ROAM ", 5) == 0) {
  8485. if (wpa_supplicant_ctrl_iface_roam(wpa_s, buf + 5))
  8486. reply_len = -1;
  8487. } else if (os_strncmp(buf, "STA_AUTOCONNECT ", 16) == 0) {
  8488. wpa_s->auto_reconnect_disabled = atoi(buf + 16) == 0;
  8489. } else if (os_strncmp(buf, "BSS_EXPIRE_AGE ", 15) == 0) {
  8490. if (wpa_supplicant_ctrl_iface_bss_expire_age(wpa_s, buf + 15))
  8491. reply_len = -1;
  8492. } else if (os_strncmp(buf, "BSS_EXPIRE_COUNT ", 17) == 0) {
  8493. if (wpa_supplicant_ctrl_iface_bss_expire_count(wpa_s,
  8494. buf + 17))
  8495. reply_len = -1;
  8496. } else if (os_strncmp(buf, "BSS_FLUSH ", 10) == 0) {
  8497. wpa_supplicant_ctrl_iface_bss_flush(wpa_s, buf + 10);
  8498. #ifdef CONFIG_TDLS
  8499. } else if (os_strncmp(buf, "TDLS_DISCOVER ", 14) == 0) {
  8500. if (wpa_supplicant_ctrl_iface_tdls_discover(wpa_s, buf + 14))
  8501. reply_len = -1;
  8502. } else if (os_strncmp(buf, "TDLS_SETUP ", 11) == 0) {
  8503. if (wpa_supplicant_ctrl_iface_tdls_setup(wpa_s, buf + 11))
  8504. reply_len = -1;
  8505. } else if (os_strncmp(buf, "TDLS_TEARDOWN ", 14) == 0) {
  8506. if (wpa_supplicant_ctrl_iface_tdls_teardown(wpa_s, buf + 14))
  8507. reply_len = -1;
  8508. } else if (os_strncmp(buf, "TDLS_CHAN_SWITCH ", 17) == 0) {
  8509. if (wpa_supplicant_ctrl_iface_tdls_chan_switch(wpa_s,
  8510. buf + 17))
  8511. reply_len = -1;
  8512. } else if (os_strncmp(buf, "TDLS_CANCEL_CHAN_SWITCH ", 24) == 0) {
  8513. if (wpa_supplicant_ctrl_iface_tdls_cancel_chan_switch(wpa_s,
  8514. buf + 24))
  8515. reply_len = -1;
  8516. } else if (os_strncmp(buf, "TDLS_LINK_STATUS ", 17) == 0) {
  8517. reply_len = wpa_supplicant_ctrl_iface_tdls_link_status(
  8518. wpa_s, buf + 17, reply, reply_size);
  8519. #endif /* CONFIG_TDLS */
  8520. } else if (os_strcmp(buf, "WMM_AC_STATUS") == 0) {
  8521. reply_len = wpas_wmm_ac_status(wpa_s, reply, reply_size);
  8522. } else if (os_strncmp(buf, "WMM_AC_ADDTS ", 13) == 0) {
  8523. if (wmm_ac_ctrl_addts(wpa_s, buf + 13))
  8524. reply_len = -1;
  8525. } else if (os_strncmp(buf, "WMM_AC_DELTS ", 13) == 0) {
  8526. if (wmm_ac_ctrl_delts(wpa_s, buf + 13))
  8527. reply_len = -1;
  8528. } else if (os_strncmp(buf, "SIGNAL_POLL", 11) == 0) {
  8529. reply_len = wpa_supplicant_signal_poll(wpa_s, reply,
  8530. reply_size);
  8531. } else if (os_strncmp(buf, "SIGNAL_MONITOR", 14) == 0) {
  8532. if (wpas_ctrl_iface_signal_monitor(wpa_s, buf + 14))
  8533. reply_len = -1;
  8534. } else if (os_strncmp(buf, "PKTCNT_POLL", 11) == 0) {
  8535. reply_len = wpa_supplicant_pktcnt_poll(wpa_s, reply,
  8536. reply_size);
  8537. #ifdef CONFIG_AUTOSCAN
  8538. } else if (os_strncmp(buf, "AUTOSCAN ", 9) == 0) {
  8539. if (wpa_supplicant_ctrl_iface_autoscan(wpa_s, buf + 9))
  8540. reply_len = -1;
  8541. #endif /* CONFIG_AUTOSCAN */
  8542. } else if (os_strcmp(buf, "DRIVER_FLAGS") == 0) {
  8543. reply_len = wpas_ctrl_iface_driver_flags(wpa_s, reply,
  8544. reply_size);
  8545. #ifdef ANDROID
  8546. } else if (os_strncmp(buf, "DRIVER ", 7) == 0) {
  8547. reply_len = wpa_supplicant_driver_cmd(wpa_s, buf + 7, reply,
  8548. reply_size);
  8549. #endif /* ANDROID */
  8550. } else if (os_strncmp(buf, "VENDOR ", 7) == 0) {
  8551. reply_len = wpa_supplicant_vendor_cmd(wpa_s, buf + 7, reply,
  8552. reply_size);
  8553. } else if (os_strcmp(buf, "REAUTHENTICATE") == 0) {
  8554. pmksa_cache_clear_current(wpa_s->wpa);
  8555. eapol_sm_request_reauth(wpa_s->eapol);
  8556. #ifdef CONFIG_WNM
  8557. } else if (os_strncmp(buf, "WNM_SLEEP ", 10) == 0) {
  8558. if (wpas_ctrl_iface_wnm_sleep(wpa_s, buf + 10))
  8559. reply_len = -1;
  8560. } else if (os_strncmp(buf, "WNM_BSS_QUERY ", 14) == 0) {
  8561. if (wpas_ctrl_iface_wnm_bss_query(wpa_s, buf + 14))
  8562. reply_len = -1;
  8563. #endif /* CONFIG_WNM */
  8564. } else if (os_strcmp(buf, "FLUSH") == 0) {
  8565. wpa_supplicant_ctrl_iface_flush(wpa_s);
  8566. } else if (os_strncmp(buf, "RADIO_WORK ", 11) == 0) {
  8567. reply_len = wpas_ctrl_radio_work(wpa_s, buf + 11, reply,
  8568. reply_size);
  8569. #ifdef CONFIG_TESTING_OPTIONS
  8570. } else if (os_strncmp(buf, "MGMT_TX ", 8) == 0) {
  8571. if (wpas_ctrl_iface_mgmt_tx(wpa_s, buf + 8) < 0)
  8572. reply_len = -1;
  8573. } else if (os_strcmp(buf, "MGMT_TX_DONE") == 0) {
  8574. wpas_ctrl_iface_mgmt_tx_done(wpa_s);
  8575. } else if (os_strncmp(buf, "MGMT_RX_PROCESS ", 16) == 0) {
  8576. if (wpas_ctrl_iface_mgmt_rx_process(wpa_s, buf + 16) < 0)
  8577. reply_len = -1;
  8578. } else if (os_strncmp(buf, "DRIVER_EVENT ", 13) == 0) {
  8579. if (wpas_ctrl_iface_driver_event(wpa_s, buf + 13) < 0)
  8580. reply_len = -1;
  8581. } else if (os_strncmp(buf, "EAPOL_RX ", 9) == 0) {
  8582. if (wpas_ctrl_iface_eapol_rx(wpa_s, buf + 9) < 0)
  8583. reply_len = -1;
  8584. } else if (os_strncmp(buf, "DATA_TEST_CONFIG ", 17) == 0) {
  8585. if (wpas_ctrl_iface_data_test_config(wpa_s, buf + 17) < 0)
  8586. reply_len = -1;
  8587. } else if (os_strncmp(buf, "DATA_TEST_TX ", 13) == 0) {
  8588. if (wpas_ctrl_iface_data_test_tx(wpa_s, buf + 13) < 0)
  8589. reply_len = -1;
  8590. } else if (os_strncmp(buf, "DATA_TEST_FRAME ", 16) == 0) {
  8591. if (wpas_ctrl_iface_data_test_frame(wpa_s, buf + 16) < 0)
  8592. reply_len = -1;
  8593. } else if (os_strncmp(buf, "TEST_ALLOC_FAIL ", 16) == 0) {
  8594. if (wpas_ctrl_test_alloc_fail(wpa_s, buf + 16) < 0)
  8595. reply_len = -1;
  8596. } else if (os_strcmp(buf, "GET_ALLOC_FAIL") == 0) {
  8597. reply_len = wpas_ctrl_get_alloc_fail(wpa_s, reply, reply_size);
  8598. } else if (os_strncmp(buf, "TEST_FAIL ", 10) == 0) {
  8599. if (wpas_ctrl_test_fail(wpa_s, buf + 10) < 0)
  8600. reply_len = -1;
  8601. } else if (os_strcmp(buf, "GET_FAIL") == 0) {
  8602. reply_len = wpas_ctrl_get_fail(wpa_s, reply, reply_size);
  8603. } else if (os_strncmp(buf, "EVENT_TEST ", 11) == 0) {
  8604. if (wpas_ctrl_event_test(wpa_s, buf + 11) < 0)
  8605. reply_len = -1;
  8606. } else if (os_strncmp(buf, "TEST_ASSOC_IE ", 14) == 0) {
  8607. if (wpas_ctrl_test_assoc_ie(wpa_s, buf + 14) < 0)
  8608. reply_len = -1;
  8609. #endif /* CONFIG_TESTING_OPTIONS */
  8610. } else if (os_strncmp(buf, "VENDOR_ELEM_ADD ", 16) == 0) {
  8611. if (wpas_ctrl_vendor_elem_add(wpa_s, buf + 16) < 0)
  8612. reply_len = -1;
  8613. } else if (os_strncmp(buf, "VENDOR_ELEM_GET ", 16) == 0) {
  8614. reply_len = wpas_ctrl_vendor_elem_get(wpa_s, buf + 16, reply,
  8615. reply_size);
  8616. } else if (os_strncmp(buf, "VENDOR_ELEM_REMOVE ", 19) == 0) {
  8617. if (wpas_ctrl_vendor_elem_remove(wpa_s, buf + 19) < 0)
  8618. reply_len = -1;
  8619. } else if (os_strncmp(buf, "NEIGHBOR_REP_REQUEST", 20) == 0) {
  8620. if (wpas_ctrl_iface_send_neighbor_rep(wpa_s, buf + 20))
  8621. reply_len = -1;
  8622. } else if (os_strcmp(buf, "ERP_FLUSH") == 0) {
  8623. wpas_ctrl_iface_erp_flush(wpa_s);
  8624. } else if (os_strncmp(buf, "MAC_RAND_SCAN ", 14) == 0) {
  8625. if (wpas_ctrl_iface_mac_rand_scan(wpa_s, buf + 14))
  8626. reply_len = -1;
  8627. } else if (os_strncmp(buf, "GET_PREF_FREQ_LIST ", 19) == 0) {
  8628. reply_len = wpas_ctrl_iface_get_pref_freq_list(
  8629. wpa_s, buf + 19, reply, reply_size);
  8630. #ifdef CONFIG_FILS
  8631. } else if (os_strncmp(buf, "FILS_HLP_REQ_ADD ", 17) == 0) {
  8632. if (wpas_ctrl_iface_fils_hlp_req_add(wpa_s, buf + 17))
  8633. reply_len = -1;
  8634. } else if (os_strcmp(buf, "FILS_HLP_REQ_FLUSH") == 0) {
  8635. wpas_flush_fils_hlp_req(wpa_s);
  8636. #endif /* CONFIG_FILS */
  8637. #ifdef CONFIG_DPP
  8638. } else if (os_strncmp(buf, "DPP_QR_CODE ", 12) == 0) {
  8639. int res;
  8640. res = wpas_dpp_qr_code(wpa_s, buf + 12);
  8641. if (res < 0) {
  8642. reply_len = -1;
  8643. } else {
  8644. reply_len = os_snprintf(reply, reply_size, "%d", res);
  8645. if (os_snprintf_error(reply_size, reply_len))
  8646. reply_len = -1;
  8647. }
  8648. } else if (os_strncmp(buf, "DPP_BOOTSTRAP_GEN ", 18) == 0) {
  8649. int res;
  8650. res = wpas_dpp_bootstrap_gen(wpa_s, buf + 18);
  8651. if (res < 0) {
  8652. reply_len = -1;
  8653. } else {
  8654. reply_len = os_snprintf(reply, reply_size, "%d", res);
  8655. if (os_snprintf_error(reply_size, reply_len))
  8656. reply_len = -1;
  8657. }
  8658. } else if (os_strncmp(buf, "DPP_BOOTSTRAP_REMOVE ", 21) == 0) {
  8659. if (wpas_dpp_bootstrap_remove(wpa_s, buf + 21) < 0)
  8660. reply_len = -1;
  8661. } else if (os_strncmp(buf, "DPP_BOOTSTRAP_GET_URI ", 22) == 0) {
  8662. const char *uri;
  8663. uri = wpas_dpp_bootstrap_get_uri(wpa_s, atoi(buf + 22));
  8664. if (!uri) {
  8665. reply_len = -1;
  8666. } else {
  8667. reply_len = os_snprintf(reply, reply_size, "%s", uri);
  8668. if (os_snprintf_error(reply_size, reply_len))
  8669. reply_len = -1;
  8670. }
  8671. } else if (os_strncmp(buf, "DPP_AUTH_INIT ", 14) == 0) {
  8672. if (wpas_dpp_auth_init(wpa_s, buf + 13) < 0)
  8673. reply_len = -1;
  8674. } else if (os_strncmp(buf, "DPP_LISTEN ", 11) == 0) {
  8675. if (wpas_dpp_listen(wpa_s, buf + 11) < 0)
  8676. reply_len = -1;
  8677. } else if (os_strcmp(buf, "DPP_STOP_LISTEN") == 0) {
  8678. wpas_dpp_listen_stop(wpa_s);
  8679. } else if (os_strncmp(buf, "DPP_CONFIGURATOR_ADD", 20) == 0) {
  8680. int res;
  8681. res = wpas_dpp_configurator_add(wpa_s, buf + 20);
  8682. if (res < 0) {
  8683. reply_len = -1;
  8684. } else {
  8685. reply_len = os_snprintf(reply, reply_size, "%d", res);
  8686. if (os_snprintf_error(reply_size, reply_len))
  8687. reply_len = -1;
  8688. }
  8689. } else if (os_strncmp(buf, "DPP_CONFIGURATOR_REMOVE ", 24) == 0) {
  8690. if (wpas_dpp_configurator_remove(wpa_s, buf + 24) < 0)
  8691. reply_len = -1;
  8692. #endif /* CONFIG_DPP */
  8693. } else {
  8694. os_memcpy(reply, "UNKNOWN COMMAND\n", 16);
  8695. reply_len = 16;
  8696. }
  8697. if (reply_len < 0) {
  8698. os_memcpy(reply, "FAIL\n", 5);
  8699. reply_len = 5;
  8700. }
  8701. *resp_len = reply_len;
  8702. return reply;
  8703. }
  8704. static int wpa_supplicant_global_iface_add(struct wpa_global *global,
  8705. char *cmd)
  8706. {
  8707. struct wpa_interface iface;
  8708. char *pos, *extra;
  8709. struct wpa_supplicant *wpa_s;
  8710. unsigned int create_iface = 0;
  8711. u8 mac_addr[ETH_ALEN];
  8712. enum wpa_driver_if_type type = WPA_IF_STATION;
  8713. /*
  8714. * <ifname>TAB<confname>TAB<driver>TAB<ctrl_interface>TAB<driver_param>
  8715. * TAB<bridge_ifname>[TAB<create>[TAB<interface_type>]]
  8716. */
  8717. wpa_printf(MSG_DEBUG, "CTRL_IFACE GLOBAL INTERFACE_ADD '%s'", cmd);
  8718. os_memset(&iface, 0, sizeof(iface));
  8719. do {
  8720. iface.ifname = pos = cmd;
  8721. pos = os_strchr(pos, '\t');
  8722. if (pos)
  8723. *pos++ = '\0';
  8724. if (iface.ifname[0] == '\0')
  8725. return -1;
  8726. if (pos == NULL)
  8727. break;
  8728. iface.confname = pos;
  8729. pos = os_strchr(pos, '\t');
  8730. if (pos)
  8731. *pos++ = '\0';
  8732. if (iface.confname[0] == '\0')
  8733. iface.confname = NULL;
  8734. if (pos == NULL)
  8735. break;
  8736. iface.driver = pos;
  8737. pos = os_strchr(pos, '\t');
  8738. if (pos)
  8739. *pos++ = '\0';
  8740. if (iface.driver[0] == '\0')
  8741. iface.driver = NULL;
  8742. if (pos == NULL)
  8743. break;
  8744. iface.ctrl_interface = pos;
  8745. pos = os_strchr(pos, '\t');
  8746. if (pos)
  8747. *pos++ = '\0';
  8748. if (iface.ctrl_interface[0] == '\0')
  8749. iface.ctrl_interface = NULL;
  8750. if (pos == NULL)
  8751. break;
  8752. iface.driver_param = pos;
  8753. pos = os_strchr(pos, '\t');
  8754. if (pos)
  8755. *pos++ = '\0';
  8756. if (iface.driver_param[0] == '\0')
  8757. iface.driver_param = NULL;
  8758. if (pos == NULL)
  8759. break;
  8760. iface.bridge_ifname = pos;
  8761. pos = os_strchr(pos, '\t');
  8762. if (pos)
  8763. *pos++ = '\0';
  8764. if (iface.bridge_ifname[0] == '\0')
  8765. iface.bridge_ifname = NULL;
  8766. if (pos == NULL)
  8767. break;
  8768. extra = pos;
  8769. pos = os_strchr(pos, '\t');
  8770. if (pos)
  8771. *pos++ = '\0';
  8772. if (!extra[0])
  8773. break;
  8774. if (os_strcmp(extra, "create") == 0) {
  8775. create_iface = 1;
  8776. if (!pos)
  8777. break;
  8778. if (os_strcmp(pos, "sta") == 0) {
  8779. type = WPA_IF_STATION;
  8780. } else if (os_strcmp(pos, "ap") == 0) {
  8781. type = WPA_IF_AP_BSS;
  8782. } else {
  8783. wpa_printf(MSG_DEBUG,
  8784. "INTERFACE_ADD unsupported interface type: '%s'",
  8785. pos);
  8786. return -1;
  8787. }
  8788. } else {
  8789. wpa_printf(MSG_DEBUG,
  8790. "INTERFACE_ADD unsupported extra parameter: '%s'",
  8791. extra);
  8792. return -1;
  8793. }
  8794. } while (0);
  8795. if (create_iface) {
  8796. wpa_printf(MSG_DEBUG, "CTRL_IFACE creating interface '%s'",
  8797. iface.ifname);
  8798. if (!global->ifaces)
  8799. return -1;
  8800. if (wpa_drv_if_add(global->ifaces, type, iface.ifname,
  8801. NULL, NULL, NULL, mac_addr, NULL) < 0) {
  8802. wpa_printf(MSG_ERROR,
  8803. "CTRL_IFACE interface creation failed");
  8804. return -1;
  8805. }
  8806. wpa_printf(MSG_DEBUG,
  8807. "CTRL_IFACE interface '%s' created with MAC addr: "
  8808. MACSTR, iface.ifname, MAC2STR(mac_addr));
  8809. }
  8810. if (wpa_supplicant_get_iface(global, iface.ifname))
  8811. goto fail;
  8812. wpa_s = wpa_supplicant_add_iface(global, &iface, NULL);
  8813. if (!wpa_s)
  8814. goto fail;
  8815. wpa_s->added_vif = create_iface;
  8816. return 0;
  8817. fail:
  8818. if (create_iface)
  8819. wpa_drv_if_remove(global->ifaces, WPA_IF_STATION, iface.ifname);
  8820. return -1;
  8821. }
  8822. static int wpa_supplicant_global_iface_remove(struct wpa_global *global,
  8823. char *cmd)
  8824. {
  8825. struct wpa_supplicant *wpa_s;
  8826. int ret;
  8827. unsigned int delete_iface;
  8828. wpa_printf(MSG_DEBUG, "CTRL_IFACE GLOBAL INTERFACE_REMOVE '%s'", cmd);
  8829. wpa_s = wpa_supplicant_get_iface(global, cmd);
  8830. if (wpa_s == NULL)
  8831. return -1;
  8832. delete_iface = wpa_s->added_vif;
  8833. ret = wpa_supplicant_remove_iface(global, wpa_s, 0);
  8834. if (!ret && delete_iface) {
  8835. wpa_printf(MSG_DEBUG, "CTRL_IFACE deleting the interface '%s'",
  8836. cmd);
  8837. ret = wpa_drv_if_remove(global->ifaces, WPA_IF_STATION, cmd);
  8838. }
  8839. return ret;
  8840. }
  8841. static void wpa_free_iface_info(struct wpa_interface_info *iface)
  8842. {
  8843. struct wpa_interface_info *prev;
  8844. while (iface) {
  8845. prev = iface;
  8846. iface = iface->next;
  8847. os_free(prev->ifname);
  8848. os_free(prev->desc);
  8849. os_free(prev);
  8850. }
  8851. }
  8852. static int wpa_supplicant_global_iface_list(struct wpa_global *global,
  8853. char *buf, int len)
  8854. {
  8855. int i, res;
  8856. struct wpa_interface_info *iface = NULL, *last = NULL, *tmp;
  8857. char *pos, *end;
  8858. for (i = 0; wpa_drivers[i]; i++) {
  8859. const struct wpa_driver_ops *drv = wpa_drivers[i];
  8860. if (drv->get_interfaces == NULL)
  8861. continue;
  8862. tmp = drv->get_interfaces(global->drv_priv[i]);
  8863. if (tmp == NULL)
  8864. continue;
  8865. if (last == NULL)
  8866. iface = last = tmp;
  8867. else
  8868. last->next = tmp;
  8869. while (last->next)
  8870. last = last->next;
  8871. }
  8872. pos = buf;
  8873. end = buf + len;
  8874. for (tmp = iface; tmp; tmp = tmp->next) {
  8875. res = os_snprintf(pos, end - pos, "%s\t%s\t%s\n",
  8876. tmp->drv_name, tmp->ifname,
  8877. tmp->desc ? tmp->desc : "");
  8878. if (os_snprintf_error(end - pos, res)) {
  8879. *pos = '\0';
  8880. break;
  8881. }
  8882. pos += res;
  8883. }
  8884. wpa_free_iface_info(iface);
  8885. return pos - buf;
  8886. }
  8887. static int wpa_supplicant_global_iface_interfaces(struct wpa_global *global,
  8888. const char *input,
  8889. char *buf, int len)
  8890. {
  8891. int res;
  8892. char *pos, *end;
  8893. struct wpa_supplicant *wpa_s;
  8894. int show_ctrl = 0;
  8895. if (input)
  8896. show_ctrl = !!os_strstr(input, "ctrl");
  8897. wpa_s = global->ifaces;
  8898. pos = buf;
  8899. end = buf + len;
  8900. while (wpa_s) {
  8901. if (show_ctrl)
  8902. res = os_snprintf(pos, end - pos, "%s ctrl_iface=%s\n",
  8903. wpa_s->ifname,
  8904. wpa_s->conf->ctrl_interface ?
  8905. wpa_s->conf->ctrl_interface : "N/A");
  8906. else
  8907. res = os_snprintf(pos, end - pos, "%s\n",
  8908. wpa_s->ifname);
  8909. if (os_snprintf_error(end - pos, res)) {
  8910. *pos = '\0';
  8911. break;
  8912. }
  8913. pos += res;
  8914. wpa_s = wpa_s->next;
  8915. }
  8916. return pos - buf;
  8917. }
  8918. static char * wpas_global_ctrl_iface_ifname(struct wpa_global *global,
  8919. const char *ifname,
  8920. char *cmd, size_t *resp_len)
  8921. {
  8922. struct wpa_supplicant *wpa_s;
  8923. for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
  8924. if (os_strcmp(ifname, wpa_s->ifname) == 0)
  8925. break;
  8926. }
  8927. if (wpa_s == NULL) {
  8928. char *resp = os_strdup("FAIL-NO-IFNAME-MATCH\n");
  8929. if (resp)
  8930. *resp_len = os_strlen(resp);
  8931. else
  8932. *resp_len = 1;
  8933. return resp;
  8934. }
  8935. return wpa_supplicant_ctrl_iface_process(wpa_s, cmd, resp_len);
  8936. }
  8937. static char * wpas_global_ctrl_iface_redir_p2p(struct wpa_global *global,
  8938. char *buf, size_t *resp_len)
  8939. {
  8940. #ifdef CONFIG_P2P
  8941. static const char * cmd[] = {
  8942. "LIST_NETWORKS",
  8943. "P2P_FIND",
  8944. "P2P_STOP_FIND",
  8945. "P2P_LISTEN",
  8946. "P2P_GROUP_ADD",
  8947. "P2P_GET_PASSPHRASE",
  8948. "P2P_SERVICE_UPDATE",
  8949. "P2P_SERVICE_FLUSH",
  8950. "P2P_FLUSH",
  8951. "P2P_CANCEL",
  8952. "P2P_PRESENCE_REQ",
  8953. "P2P_EXT_LISTEN",
  8954. #ifdef CONFIG_AP
  8955. "STA-FIRST",
  8956. #endif /* CONFIG_AP */
  8957. NULL
  8958. };
  8959. static const char * prefix[] = {
  8960. #ifdef ANDROID
  8961. "DRIVER ",
  8962. #endif /* ANDROID */
  8963. "GET_CAPABILITY ",
  8964. "GET_NETWORK ",
  8965. "REMOVE_NETWORK ",
  8966. "P2P_FIND ",
  8967. "P2P_CONNECT ",
  8968. "P2P_LISTEN ",
  8969. "P2P_GROUP_REMOVE ",
  8970. "P2P_GROUP_ADD ",
  8971. "P2P_GROUP_MEMBER ",
  8972. "P2P_PROV_DISC ",
  8973. "P2P_SERV_DISC_REQ ",
  8974. "P2P_SERV_DISC_CANCEL_REQ ",
  8975. "P2P_SERV_DISC_RESP ",
  8976. "P2P_SERV_DISC_EXTERNAL ",
  8977. "P2P_SERVICE_ADD ",
  8978. "P2P_SERVICE_DEL ",
  8979. "P2P_SERVICE_REP ",
  8980. "P2P_REJECT ",
  8981. "P2P_INVITE ",
  8982. "P2P_PEER ",
  8983. "P2P_SET ",
  8984. "P2P_UNAUTHORIZE ",
  8985. "P2P_PRESENCE_REQ ",
  8986. "P2P_EXT_LISTEN ",
  8987. "P2P_REMOVE_CLIENT ",
  8988. "WPS_NFC_TOKEN ",
  8989. "WPS_NFC_TAG_READ ",
  8990. "NFC_GET_HANDOVER_SEL ",
  8991. "NFC_GET_HANDOVER_REQ ",
  8992. "NFC_REPORT_HANDOVER ",
  8993. "P2P_ASP_PROVISION ",
  8994. "P2P_ASP_PROVISION_RESP ",
  8995. #ifdef CONFIG_AP
  8996. "STA ",
  8997. "STA-NEXT ",
  8998. #endif /* CONFIG_AP */
  8999. NULL
  9000. };
  9001. int found = 0;
  9002. int i;
  9003. if (global->p2p_init_wpa_s == NULL)
  9004. return NULL;
  9005. for (i = 0; !found && cmd[i]; i++) {
  9006. if (os_strcmp(buf, cmd[i]) == 0)
  9007. found = 1;
  9008. }
  9009. for (i = 0; !found && prefix[i]; i++) {
  9010. if (os_strncmp(buf, prefix[i], os_strlen(prefix[i])) == 0)
  9011. found = 1;
  9012. }
  9013. if (found)
  9014. return wpa_supplicant_ctrl_iface_process(global->p2p_init_wpa_s,
  9015. buf, resp_len);
  9016. #endif /* CONFIG_P2P */
  9017. return NULL;
  9018. }
  9019. static char * wpas_global_ctrl_iface_redir_wfd(struct wpa_global *global,
  9020. char *buf, size_t *resp_len)
  9021. {
  9022. #ifdef CONFIG_WIFI_DISPLAY
  9023. if (global->p2p_init_wpa_s == NULL)
  9024. return NULL;
  9025. if (os_strncmp(buf, "WFD_SUBELEM_SET ", 16) == 0 ||
  9026. os_strncmp(buf, "WFD_SUBELEM_GET ", 16) == 0)
  9027. return wpa_supplicant_ctrl_iface_process(global->p2p_init_wpa_s,
  9028. buf, resp_len);
  9029. #endif /* CONFIG_WIFI_DISPLAY */
  9030. return NULL;
  9031. }
  9032. static char * wpas_global_ctrl_iface_redir(struct wpa_global *global,
  9033. char *buf, size_t *resp_len)
  9034. {
  9035. char *ret;
  9036. ret = wpas_global_ctrl_iface_redir_p2p(global, buf, resp_len);
  9037. if (ret)
  9038. return ret;
  9039. ret = wpas_global_ctrl_iface_redir_wfd(global, buf, resp_len);
  9040. if (ret)
  9041. return ret;
  9042. return NULL;
  9043. }
  9044. static int wpas_global_ctrl_iface_set(struct wpa_global *global, char *cmd)
  9045. {
  9046. char *value;
  9047. value = os_strchr(cmd, ' ');
  9048. if (value == NULL)
  9049. return -1;
  9050. *value++ = '\0';
  9051. wpa_printf(MSG_DEBUG, "GLOBAL_CTRL_IFACE SET '%s'='%s'", cmd, value);
  9052. #ifdef CONFIG_WIFI_DISPLAY
  9053. if (os_strcasecmp(cmd, "wifi_display") == 0) {
  9054. wifi_display_enable(global, !!atoi(value));
  9055. return 0;
  9056. }
  9057. #endif /* CONFIG_WIFI_DISPLAY */
  9058. /* Restore cmd to its original value to allow redirection */
  9059. value[-1] = ' ';
  9060. return -1;
  9061. }
  9062. static int wpas_global_ctrl_iface_dup_network(struct wpa_global *global,
  9063. char *cmd)
  9064. {
  9065. struct wpa_supplicant *wpa_s[2]; /* src, dst */
  9066. char *p;
  9067. unsigned int i;
  9068. /* cmd: "<src ifname> <dst ifname> <src network id> <dst network id>
  9069. * <variable name> */
  9070. for (i = 0; i < ARRAY_SIZE(wpa_s) ; i++) {
  9071. p = os_strchr(cmd, ' ');
  9072. if (p == NULL)
  9073. return -1;
  9074. *p = '\0';
  9075. wpa_s[i] = global->ifaces;
  9076. for (; wpa_s[i]; wpa_s[i] = wpa_s[i]->next) {
  9077. if (os_strcmp(cmd, wpa_s[i]->ifname) == 0)
  9078. break;
  9079. }
  9080. if (!wpa_s[i]) {
  9081. wpa_printf(MSG_DEBUG,
  9082. "CTRL_IFACE: Could not find iface=%s", cmd);
  9083. return -1;
  9084. }
  9085. cmd = p + 1;
  9086. }
  9087. return wpa_supplicant_ctrl_iface_dup_network(wpa_s[0], cmd, wpa_s[1]);
  9088. }
  9089. #ifndef CONFIG_NO_CONFIG_WRITE
  9090. static int wpas_global_ctrl_iface_save_config(struct wpa_global *global)
  9091. {
  9092. int ret = 0, saved = 0;
  9093. struct wpa_supplicant *wpa_s;
  9094. for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
  9095. if (!wpa_s->conf->update_config) {
  9096. wpa_dbg(wpa_s, MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Not allowed to update configuration (update_config=0)");
  9097. continue;
  9098. }
  9099. if (wpa_config_write(wpa_s->confname, wpa_s->conf)) {
  9100. wpa_dbg(wpa_s, MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Failed to update configuration");
  9101. ret = 1;
  9102. } else {
  9103. wpa_dbg(wpa_s, MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Configuration updated");
  9104. saved++;
  9105. }
  9106. }
  9107. if (!saved && !ret) {
  9108. wpa_dbg(wpa_s, MSG_DEBUG,
  9109. "CTRL_IFACE: SAVE_CONFIG - No configuration files could be updated");
  9110. ret = 1;
  9111. }
  9112. return ret;
  9113. }
  9114. #endif /* CONFIG_NO_CONFIG_WRITE */
  9115. static int wpas_global_ctrl_iface_status(struct wpa_global *global,
  9116. char *buf, size_t buflen)
  9117. {
  9118. char *pos, *end;
  9119. int ret;
  9120. struct wpa_supplicant *wpa_s;
  9121. pos = buf;
  9122. end = buf + buflen;
  9123. #ifdef CONFIG_P2P
  9124. if (global->p2p && !global->p2p_disabled) {
  9125. ret = os_snprintf(pos, end - pos, "p2p_device_address=" MACSTR
  9126. "\n"
  9127. "p2p_state=%s\n",
  9128. MAC2STR(global->p2p_dev_addr),
  9129. p2p_get_state_txt(global->p2p));
  9130. if (os_snprintf_error(end - pos, ret))
  9131. return pos - buf;
  9132. pos += ret;
  9133. } else if (global->p2p) {
  9134. ret = os_snprintf(pos, end - pos, "p2p_state=DISABLED\n");
  9135. if (os_snprintf_error(end - pos, ret))
  9136. return pos - buf;
  9137. pos += ret;
  9138. }
  9139. #endif /* CONFIG_P2P */
  9140. #ifdef CONFIG_WIFI_DISPLAY
  9141. ret = os_snprintf(pos, end - pos, "wifi_display=%d\n",
  9142. !!global->wifi_display);
  9143. if (os_snprintf_error(end - pos, ret))
  9144. return pos - buf;
  9145. pos += ret;
  9146. #endif /* CONFIG_WIFI_DISPLAY */
  9147. for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
  9148. ret = os_snprintf(pos, end - pos, "ifname=%s\n"
  9149. "address=" MACSTR "\n",
  9150. wpa_s->ifname, MAC2STR(wpa_s->own_addr));
  9151. if (os_snprintf_error(end - pos, ret))
  9152. return pos - buf;
  9153. pos += ret;
  9154. }
  9155. return pos - buf;
  9156. }
  9157. #ifdef CONFIG_FST
  9158. static int wpas_global_ctrl_iface_fst_attach(struct wpa_global *global,
  9159. char *cmd, char *buf,
  9160. size_t reply_size)
  9161. {
  9162. char ifname[IFNAMSIZ + 1];
  9163. struct fst_iface_cfg cfg;
  9164. struct wpa_supplicant *wpa_s;
  9165. struct fst_wpa_obj iface_obj;
  9166. if (!fst_parse_attach_command(cmd, ifname, sizeof(ifname), &cfg)) {
  9167. wpa_s = wpa_supplicant_get_iface(global, ifname);
  9168. if (wpa_s) {
  9169. if (wpa_s->fst) {
  9170. wpa_printf(MSG_INFO, "FST: Already attached");
  9171. return -1;
  9172. }
  9173. fst_wpa_supplicant_fill_iface_obj(wpa_s, &iface_obj);
  9174. wpa_s->fst = fst_attach(ifname, wpa_s->own_addr,
  9175. &iface_obj, &cfg);
  9176. if (wpa_s->fst)
  9177. return os_snprintf(buf, reply_size, "OK\n");
  9178. }
  9179. }
  9180. return -1;
  9181. }
  9182. static int wpas_global_ctrl_iface_fst_detach(struct wpa_global *global,
  9183. char *cmd, char *buf,
  9184. size_t reply_size)
  9185. {
  9186. char ifname[IFNAMSIZ + 1];
  9187. struct wpa_supplicant *wpa_s;
  9188. if (!fst_parse_detach_command(cmd, ifname, sizeof(ifname))) {
  9189. wpa_s = wpa_supplicant_get_iface(global, ifname);
  9190. if (wpa_s) {
  9191. if (!fst_iface_detach(ifname)) {
  9192. wpa_s->fst = NULL;
  9193. return os_snprintf(buf, reply_size, "OK\n");
  9194. }
  9195. }
  9196. }
  9197. return -1;
  9198. }
  9199. #endif /* CONFIG_FST */
  9200. char * wpa_supplicant_global_ctrl_iface_process(struct wpa_global *global,
  9201. char *buf, size_t *resp_len)
  9202. {
  9203. char *reply;
  9204. const int reply_size = 2048;
  9205. int reply_len;
  9206. int level = MSG_DEBUG;
  9207. if (os_strncmp(buf, "IFNAME=", 7) == 0) {
  9208. char *pos = os_strchr(buf + 7, ' ');
  9209. if (pos) {
  9210. *pos++ = '\0';
  9211. return wpas_global_ctrl_iface_ifname(global,
  9212. buf + 7, pos,
  9213. resp_len);
  9214. }
  9215. }
  9216. reply = wpas_global_ctrl_iface_redir(global, buf, resp_len);
  9217. if (reply)
  9218. return reply;
  9219. if (os_strcmp(buf, "PING") == 0)
  9220. level = MSG_EXCESSIVE;
  9221. wpa_hexdump_ascii(level, "RX global ctrl_iface",
  9222. (const u8 *) buf, os_strlen(buf));
  9223. reply = os_malloc(reply_size);
  9224. if (reply == NULL) {
  9225. *resp_len = 1;
  9226. return NULL;
  9227. }
  9228. os_memcpy(reply, "OK\n", 3);
  9229. reply_len = 3;
  9230. if (os_strcmp(buf, "PING") == 0) {
  9231. os_memcpy(reply, "PONG\n", 5);
  9232. reply_len = 5;
  9233. } else if (os_strncmp(buf, "INTERFACE_ADD ", 14) == 0) {
  9234. if (wpa_supplicant_global_iface_add(global, buf + 14))
  9235. reply_len = -1;
  9236. } else if (os_strncmp(buf, "INTERFACE_REMOVE ", 17) == 0) {
  9237. if (wpa_supplicant_global_iface_remove(global, buf + 17))
  9238. reply_len = -1;
  9239. } else if (os_strcmp(buf, "INTERFACE_LIST") == 0) {
  9240. reply_len = wpa_supplicant_global_iface_list(
  9241. global, reply, reply_size);
  9242. } else if (os_strncmp(buf, "INTERFACES", 10) == 0) {
  9243. reply_len = wpa_supplicant_global_iface_interfaces(
  9244. global, buf + 10, reply, reply_size);
  9245. #ifdef CONFIG_FST
  9246. } else if (os_strncmp(buf, "FST-ATTACH ", 11) == 0) {
  9247. reply_len = wpas_global_ctrl_iface_fst_attach(global, buf + 11,
  9248. reply,
  9249. reply_size);
  9250. } else if (os_strncmp(buf, "FST-DETACH ", 11) == 0) {
  9251. reply_len = wpas_global_ctrl_iface_fst_detach(global, buf + 11,
  9252. reply,
  9253. reply_size);
  9254. } else if (os_strncmp(buf, "FST-MANAGER ", 12) == 0) {
  9255. reply_len = fst_ctrl_iface_receive(buf + 12, reply, reply_size);
  9256. #endif /* CONFIG_FST */
  9257. } else if (os_strcmp(buf, "TERMINATE") == 0) {
  9258. wpa_supplicant_terminate_proc(global);
  9259. } else if (os_strcmp(buf, "SUSPEND") == 0) {
  9260. wpas_notify_suspend(global);
  9261. } else if (os_strcmp(buf, "RESUME") == 0) {
  9262. wpas_notify_resume(global);
  9263. } else if (os_strncmp(buf, "SET ", 4) == 0) {
  9264. if (wpas_global_ctrl_iface_set(global, buf + 4)) {
  9265. #ifdef CONFIG_P2P
  9266. if (global->p2p_init_wpa_s) {
  9267. os_free(reply);
  9268. /* Check if P2P redirection would work for this
  9269. * command. */
  9270. return wpa_supplicant_ctrl_iface_process(
  9271. global->p2p_init_wpa_s,
  9272. buf, resp_len);
  9273. }
  9274. #endif /* CONFIG_P2P */
  9275. reply_len = -1;
  9276. }
  9277. } else if (os_strncmp(buf, "DUP_NETWORK ", 12) == 0) {
  9278. if (wpas_global_ctrl_iface_dup_network(global, buf + 12))
  9279. reply_len = -1;
  9280. #ifndef CONFIG_NO_CONFIG_WRITE
  9281. } else if (os_strcmp(buf, "SAVE_CONFIG") == 0) {
  9282. if (wpas_global_ctrl_iface_save_config(global))
  9283. reply_len = -1;
  9284. #endif /* CONFIG_NO_CONFIG_WRITE */
  9285. } else if (os_strcmp(buf, "STATUS") == 0) {
  9286. reply_len = wpas_global_ctrl_iface_status(global, reply,
  9287. reply_size);
  9288. #ifdef CONFIG_MODULE_TESTS
  9289. } else if (os_strcmp(buf, "MODULE_TESTS") == 0) {
  9290. if (wpas_module_tests() < 0)
  9291. reply_len = -1;
  9292. #endif /* CONFIG_MODULE_TESTS */
  9293. } else if (os_strncmp(buf, "RELOG", 5) == 0) {
  9294. if (wpa_debug_reopen_file() < 0)
  9295. reply_len = -1;
  9296. } else {
  9297. os_memcpy(reply, "UNKNOWN COMMAND\n", 16);
  9298. reply_len = 16;
  9299. }
  9300. if (reply_len < 0) {
  9301. os_memcpy(reply, "FAIL\n", 5);
  9302. reply_len = 5;
  9303. }
  9304. *resp_len = reply_len;
  9305. return reply;
  9306. }