ctrl_iface.c 177 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538
  1. /*
  2. * WPA Supplicant / Control interface (shared code for all backends)
  3. * Copyright (c) 2004-2014, 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. #include "utils/common.h"
  10. #include "utils/eloop.h"
  11. #include "utils/uuid.h"
  12. #include "common/version.h"
  13. #include "common/ieee802_11_defs.h"
  14. #include "common/ieee802_11_common.h"
  15. #include "common/wpa_ctrl.h"
  16. #include "eap_peer/eap.h"
  17. #include "eapol_supp/eapol_supp_sm.h"
  18. #include "rsn_supp/wpa.h"
  19. #include "rsn_supp/preauth.h"
  20. #include "rsn_supp/pmksa_cache.h"
  21. #include "l2_packet/l2_packet.h"
  22. #include "wps/wps.h"
  23. #include "config.h"
  24. #include "wpa_supplicant_i.h"
  25. #include "driver_i.h"
  26. #include "wps_supplicant.h"
  27. #include "ibss_rsn.h"
  28. #include "ap.h"
  29. #include "p2p_supplicant.h"
  30. #include "p2p/p2p.h"
  31. #include "hs20_supplicant.h"
  32. #include "wifi_display.h"
  33. #include "notify.h"
  34. #include "bss.h"
  35. #include "scan.h"
  36. #include "ctrl_iface.h"
  37. #include "interworking.h"
  38. #include "blacklist.h"
  39. #include "autoscan.h"
  40. #include "wnm_sta.h"
  41. #include "offchannel.h"
  42. #include "drivers/driver.h"
  43. static int wpa_supplicant_global_iface_list(struct wpa_global *global,
  44. char *buf, int len);
  45. static int wpa_supplicant_global_iface_interfaces(struct wpa_global *global,
  46. char *buf, int len);
  47. static int * freq_range_to_channel_list(struct wpa_supplicant *wpa_s,
  48. char *val);
  49. static int set_bssid_filter(struct wpa_supplicant *wpa_s, char *val)
  50. {
  51. char *pos;
  52. u8 addr[ETH_ALEN], *filter = NULL, *n;
  53. size_t count = 0;
  54. pos = val;
  55. while (pos) {
  56. if (*pos == '\0')
  57. break;
  58. if (hwaddr_aton(pos, addr)) {
  59. os_free(filter);
  60. return -1;
  61. }
  62. n = os_realloc_array(filter, count + 1, ETH_ALEN);
  63. if (n == NULL) {
  64. os_free(filter);
  65. return -1;
  66. }
  67. filter = n;
  68. os_memcpy(filter + count * ETH_ALEN, addr, ETH_ALEN);
  69. count++;
  70. pos = os_strchr(pos, ' ');
  71. if (pos)
  72. pos++;
  73. }
  74. wpa_hexdump(MSG_DEBUG, "bssid_filter", filter, count * ETH_ALEN);
  75. os_free(wpa_s->bssid_filter);
  76. wpa_s->bssid_filter = filter;
  77. wpa_s->bssid_filter_count = count;
  78. return 0;
  79. }
  80. static int set_disallow_aps(struct wpa_supplicant *wpa_s, char *val)
  81. {
  82. char *pos;
  83. u8 addr[ETH_ALEN], *bssid = NULL, *n;
  84. struct wpa_ssid_value *ssid = NULL, *ns;
  85. size_t count = 0, ssid_count = 0;
  86. struct wpa_ssid *c;
  87. /*
  88. * disallow_list ::= <ssid_spec> | <bssid_spec> | <disallow_list> | ""
  89. * SSID_SPEC ::= ssid <SSID_HEX>
  90. * BSSID_SPEC ::= bssid <BSSID_HEX>
  91. */
  92. pos = val;
  93. while (pos) {
  94. if (*pos == '\0')
  95. break;
  96. if (os_strncmp(pos, "bssid ", 6) == 0) {
  97. int res;
  98. pos += 6;
  99. res = hwaddr_aton2(pos, addr);
  100. if (res < 0) {
  101. os_free(ssid);
  102. os_free(bssid);
  103. wpa_printf(MSG_DEBUG, "Invalid disallow_aps "
  104. "BSSID value '%s'", pos);
  105. return -1;
  106. }
  107. pos += res;
  108. n = os_realloc_array(bssid, count + 1, ETH_ALEN);
  109. if (n == NULL) {
  110. os_free(ssid);
  111. os_free(bssid);
  112. return -1;
  113. }
  114. bssid = n;
  115. os_memcpy(bssid + count * ETH_ALEN, addr, ETH_ALEN);
  116. count++;
  117. } else if (os_strncmp(pos, "ssid ", 5) == 0) {
  118. char *end;
  119. pos += 5;
  120. end = pos;
  121. while (*end) {
  122. if (*end == '\0' || *end == ' ')
  123. break;
  124. end++;
  125. }
  126. ns = os_realloc_array(ssid, ssid_count + 1,
  127. sizeof(struct wpa_ssid_value));
  128. if (ns == NULL) {
  129. os_free(ssid);
  130. os_free(bssid);
  131. return -1;
  132. }
  133. ssid = ns;
  134. if ((end - pos) & 0x01 || end - pos > 2 * 32 ||
  135. hexstr2bin(pos, ssid[ssid_count].ssid,
  136. (end - pos) / 2) < 0) {
  137. os_free(ssid);
  138. os_free(bssid);
  139. wpa_printf(MSG_DEBUG, "Invalid disallow_aps "
  140. "SSID value '%s'", pos);
  141. return -1;
  142. }
  143. ssid[ssid_count].ssid_len = (end - pos) / 2;
  144. wpa_hexdump_ascii(MSG_DEBUG, "disallow_aps SSID",
  145. ssid[ssid_count].ssid,
  146. ssid[ssid_count].ssid_len);
  147. ssid_count++;
  148. pos = end;
  149. } else {
  150. wpa_printf(MSG_DEBUG, "Unexpected disallow_aps value "
  151. "'%s'", pos);
  152. os_free(ssid);
  153. os_free(bssid);
  154. return -1;
  155. }
  156. pos = os_strchr(pos, ' ');
  157. if (pos)
  158. pos++;
  159. }
  160. wpa_hexdump(MSG_DEBUG, "disallow_aps_bssid", bssid, count * ETH_ALEN);
  161. os_free(wpa_s->disallow_aps_bssid);
  162. wpa_s->disallow_aps_bssid = bssid;
  163. wpa_s->disallow_aps_bssid_count = count;
  164. wpa_printf(MSG_DEBUG, "disallow_aps_ssid_count %d", (int) ssid_count);
  165. os_free(wpa_s->disallow_aps_ssid);
  166. wpa_s->disallow_aps_ssid = ssid;
  167. wpa_s->disallow_aps_ssid_count = ssid_count;
  168. if (!wpa_s->current_ssid || wpa_s->wpa_state < WPA_AUTHENTICATING)
  169. return 0;
  170. c = wpa_s->current_ssid;
  171. if (c->mode != WPAS_MODE_INFRA && c->mode != WPAS_MODE_IBSS)
  172. return 0;
  173. if (!disallowed_bssid(wpa_s, wpa_s->bssid) &&
  174. !disallowed_ssid(wpa_s, c->ssid, c->ssid_len))
  175. return 0;
  176. wpa_printf(MSG_DEBUG, "Disconnect and try to find another network "
  177. "because current AP was marked disallowed");
  178. #ifdef CONFIG_SME
  179. wpa_s->sme.prev_bssid_set = 0;
  180. #endif /* CONFIG_SME */
  181. wpa_s->reassociate = 1;
  182. wpa_supplicant_deauthenticate(wpa_s, WLAN_REASON_DEAUTH_LEAVING);
  183. wpa_supplicant_req_scan(wpa_s, 0, 0);
  184. return 0;
  185. }
  186. #ifndef CONFIG_NO_CONFIG_BLOBS
  187. static int wpas_ctrl_set_blob(struct wpa_supplicant *wpa_s, char *pos)
  188. {
  189. char *name = pos;
  190. struct wpa_config_blob *blob;
  191. size_t len;
  192. pos = os_strchr(pos, ' ');
  193. if (pos == NULL)
  194. return -1;
  195. *pos++ = '\0';
  196. len = os_strlen(pos);
  197. if (len & 1)
  198. return -1;
  199. wpa_printf(MSG_DEBUG, "CTRL: Set blob '%s'", name);
  200. blob = os_zalloc(sizeof(*blob));
  201. if (blob == NULL)
  202. return -1;
  203. blob->name = os_strdup(name);
  204. blob->data = os_malloc(len / 2);
  205. if (blob->name == NULL || blob->data == NULL) {
  206. wpa_config_free_blob(blob);
  207. return -1;
  208. }
  209. if (hexstr2bin(pos, blob->data, len / 2) < 0) {
  210. wpa_printf(MSG_DEBUG, "CTRL: Invalid blob hex data");
  211. wpa_config_free_blob(blob);
  212. return -1;
  213. }
  214. blob->len = len / 2;
  215. wpa_config_set_blob(wpa_s->conf, blob);
  216. return 0;
  217. }
  218. #endif /* CONFIG_NO_CONFIG_BLOBS */
  219. static int wpas_ctrl_pno(struct wpa_supplicant *wpa_s, char *cmd)
  220. {
  221. char *params;
  222. char *pos;
  223. int *freqs = NULL;
  224. int ret;
  225. if (atoi(cmd)) {
  226. params = os_strchr(cmd, ' ');
  227. os_free(wpa_s->manual_sched_scan_freqs);
  228. if (params) {
  229. params++;
  230. pos = os_strstr(params, "freq=");
  231. if (pos)
  232. freqs = freq_range_to_channel_list(wpa_s,
  233. pos + 5);
  234. }
  235. wpa_s->manual_sched_scan_freqs = freqs;
  236. ret = wpas_start_pno(wpa_s);
  237. } else {
  238. ret = wpas_stop_pno(wpa_s);
  239. }
  240. return ret;
  241. }
  242. static int wpa_supplicant_ctrl_iface_set(struct wpa_supplicant *wpa_s,
  243. char *cmd)
  244. {
  245. char *value;
  246. int ret = 0;
  247. value = os_strchr(cmd, ' ');
  248. if (value == NULL)
  249. return -1;
  250. *value++ = '\0';
  251. wpa_printf(MSG_DEBUG, "CTRL_IFACE SET '%s'='%s'", cmd, value);
  252. if (os_strcasecmp(cmd, "EAPOL::heldPeriod") == 0) {
  253. eapol_sm_configure(wpa_s->eapol,
  254. atoi(value), -1, -1, -1);
  255. } else if (os_strcasecmp(cmd, "EAPOL::authPeriod") == 0) {
  256. eapol_sm_configure(wpa_s->eapol,
  257. -1, atoi(value), -1, -1);
  258. } else if (os_strcasecmp(cmd, "EAPOL::startPeriod") == 0) {
  259. eapol_sm_configure(wpa_s->eapol,
  260. -1, -1, atoi(value), -1);
  261. } else if (os_strcasecmp(cmd, "EAPOL::maxStart") == 0) {
  262. eapol_sm_configure(wpa_s->eapol,
  263. -1, -1, -1, atoi(value));
  264. } else if (os_strcasecmp(cmd, "dot11RSNAConfigPMKLifetime") == 0) {
  265. if (wpa_sm_set_param(wpa_s->wpa, RSNA_PMK_LIFETIME,
  266. atoi(value)))
  267. ret = -1;
  268. } else if (os_strcasecmp(cmd, "dot11RSNAConfigPMKReauthThreshold") ==
  269. 0) {
  270. if (wpa_sm_set_param(wpa_s->wpa, RSNA_PMK_REAUTH_THRESHOLD,
  271. atoi(value)))
  272. ret = -1;
  273. } else if (os_strcasecmp(cmd, "dot11RSNAConfigSATimeout") == 0) {
  274. if (wpa_sm_set_param(wpa_s->wpa, RSNA_SA_TIMEOUT, atoi(value)))
  275. ret = -1;
  276. } else if (os_strcasecmp(cmd, "wps_fragment_size") == 0) {
  277. wpa_s->wps_fragment_size = atoi(value);
  278. #ifdef CONFIG_WPS_TESTING
  279. } else if (os_strcasecmp(cmd, "wps_version_number") == 0) {
  280. long int val;
  281. val = strtol(value, NULL, 0);
  282. if (val < 0 || val > 0xff) {
  283. ret = -1;
  284. wpa_printf(MSG_DEBUG, "WPS: Invalid "
  285. "wps_version_number %ld", val);
  286. } else {
  287. wps_version_number = val;
  288. wpa_printf(MSG_DEBUG, "WPS: Testing - force WPS "
  289. "version %u.%u",
  290. (wps_version_number & 0xf0) >> 4,
  291. wps_version_number & 0x0f);
  292. }
  293. } else if (os_strcasecmp(cmd, "wps_testing_dummy_cred") == 0) {
  294. wps_testing_dummy_cred = atoi(value);
  295. wpa_printf(MSG_DEBUG, "WPS: Testing - dummy_cred=%d",
  296. wps_testing_dummy_cred);
  297. } else if (os_strcasecmp(cmd, "wps_corrupt_pkhash") == 0) {
  298. wps_corrupt_pkhash = atoi(value);
  299. wpa_printf(MSG_DEBUG, "WPS: Testing - wps_corrupt_pkhash=%d",
  300. wps_corrupt_pkhash);
  301. #endif /* CONFIG_WPS_TESTING */
  302. } else if (os_strcasecmp(cmd, "ampdu") == 0) {
  303. if (wpa_drv_ampdu(wpa_s, atoi(value)) < 0)
  304. ret = -1;
  305. #ifdef CONFIG_TDLS
  306. #ifdef CONFIG_TDLS_TESTING
  307. } else if (os_strcasecmp(cmd, "tdls_testing") == 0) {
  308. extern unsigned int tdls_testing;
  309. tdls_testing = strtol(value, NULL, 0);
  310. wpa_printf(MSG_DEBUG, "TDLS: tdls_testing=0x%x", tdls_testing);
  311. #endif /* CONFIG_TDLS_TESTING */
  312. } else if (os_strcasecmp(cmd, "tdls_disabled") == 0) {
  313. int disabled = atoi(value);
  314. wpa_printf(MSG_DEBUG, "TDLS: tdls_disabled=%d", disabled);
  315. if (disabled) {
  316. if (wpa_drv_tdls_oper(wpa_s, TDLS_DISABLE, NULL) < 0)
  317. ret = -1;
  318. } else if (wpa_drv_tdls_oper(wpa_s, TDLS_ENABLE, NULL) < 0)
  319. ret = -1;
  320. wpa_tdls_enable(wpa_s->wpa, !disabled);
  321. #endif /* CONFIG_TDLS */
  322. } else if (os_strcasecmp(cmd, "pno") == 0) {
  323. ret = wpas_ctrl_pno(wpa_s, value);
  324. } else if (os_strcasecmp(cmd, "radio_disabled") == 0) {
  325. int disabled = atoi(value);
  326. if (wpa_drv_radio_disable(wpa_s, disabled) < 0)
  327. ret = -1;
  328. else if (disabled)
  329. wpa_supplicant_set_state(wpa_s, WPA_INACTIVE);
  330. } else if (os_strcasecmp(cmd, "uapsd") == 0) {
  331. if (os_strcmp(value, "disable") == 0)
  332. wpa_s->set_sta_uapsd = 0;
  333. else {
  334. int be, bk, vi, vo;
  335. char *pos;
  336. /* format: BE,BK,VI,VO;max SP Length */
  337. be = atoi(value);
  338. pos = os_strchr(value, ',');
  339. if (pos == NULL)
  340. return -1;
  341. pos++;
  342. bk = atoi(pos);
  343. pos = os_strchr(pos, ',');
  344. if (pos == NULL)
  345. return -1;
  346. pos++;
  347. vi = atoi(pos);
  348. pos = os_strchr(pos, ',');
  349. if (pos == NULL)
  350. return -1;
  351. pos++;
  352. vo = atoi(pos);
  353. /* ignore max SP Length for now */
  354. wpa_s->set_sta_uapsd = 1;
  355. wpa_s->sta_uapsd = 0;
  356. if (be)
  357. wpa_s->sta_uapsd |= BIT(0);
  358. if (bk)
  359. wpa_s->sta_uapsd |= BIT(1);
  360. if (vi)
  361. wpa_s->sta_uapsd |= BIT(2);
  362. if (vo)
  363. wpa_s->sta_uapsd |= BIT(3);
  364. }
  365. } else if (os_strcasecmp(cmd, "ps") == 0) {
  366. ret = wpa_drv_set_p2p_powersave(wpa_s, atoi(value), -1, -1);
  367. #ifdef CONFIG_WIFI_DISPLAY
  368. } else if (os_strcasecmp(cmd, "wifi_display") == 0) {
  369. int enabled = !!atoi(value);
  370. if (enabled && !wpa_s->global->p2p)
  371. ret = -1;
  372. else
  373. wifi_display_enable(wpa_s->global, enabled);
  374. #endif /* CONFIG_WIFI_DISPLAY */
  375. } else if (os_strcasecmp(cmd, "bssid_filter") == 0) {
  376. ret = set_bssid_filter(wpa_s, value);
  377. } else if (os_strcasecmp(cmd, "disallow_aps") == 0) {
  378. ret = set_disallow_aps(wpa_s, value);
  379. } else if (os_strcasecmp(cmd, "no_keep_alive") == 0) {
  380. wpa_s->no_keep_alive = !!atoi(value);
  381. #ifdef CONFIG_TESTING_OPTIONS
  382. } else if (os_strcasecmp(cmd, "ext_mgmt_frame_handling") == 0) {
  383. wpa_s->ext_mgmt_frame_handling = !!atoi(value);
  384. #endif /* CONFIG_TESTING_OPTIONS */
  385. #ifndef CONFIG_NO_CONFIG_BLOBS
  386. } else if (os_strcmp(cmd, "blob") == 0) {
  387. ret = wpas_ctrl_set_blob(wpa_s, value);
  388. #endif /* CONFIG_NO_CONFIG_BLOBS */
  389. } else {
  390. value[-1] = '=';
  391. ret = wpa_config_process_global(wpa_s->conf, cmd, -1);
  392. if (ret == 0)
  393. wpa_supplicant_update_config(wpa_s);
  394. }
  395. return ret;
  396. }
  397. static int wpa_supplicant_ctrl_iface_get(struct wpa_supplicant *wpa_s,
  398. char *cmd, char *buf, size_t buflen)
  399. {
  400. int res = -1;
  401. wpa_printf(MSG_DEBUG, "CTRL_IFACE GET '%s'", cmd);
  402. if (os_strcmp(cmd, "version") == 0) {
  403. res = os_snprintf(buf, buflen, "%s", VERSION_STR);
  404. } else if (os_strcasecmp(cmd, "country") == 0) {
  405. if (wpa_s->conf->country[0] && wpa_s->conf->country[1])
  406. res = os_snprintf(buf, buflen, "%c%c",
  407. wpa_s->conf->country[0],
  408. wpa_s->conf->country[1]);
  409. #ifdef CONFIG_WIFI_DISPLAY
  410. } else if (os_strcasecmp(cmd, "wifi_display") == 0) {
  411. int enabled;
  412. if (wpa_s->global->p2p == NULL ||
  413. wpa_s->global->p2p_disabled)
  414. enabled = 0;
  415. else
  416. enabled = wpa_s->global->wifi_display;
  417. res = os_snprintf(buf, buflen, "%d", enabled);
  418. if (res < 0 || (unsigned int) res >= buflen)
  419. return -1;
  420. return res;
  421. #endif /* CONFIG_WIFI_DISPLAY */
  422. #ifdef CONFIG_TESTING_GET_GTK
  423. } else if (os_strcmp(cmd, "gtk") == 0) {
  424. if (wpa_s->last_gtk_len == 0)
  425. return -1;
  426. res = wpa_snprintf_hex(buf, buflen, wpa_s->last_gtk,
  427. wpa_s->last_gtk_len);
  428. return res;
  429. #endif /* CONFIG_TESTING_GET_GTK */
  430. }
  431. if (res < 0 || (unsigned int) res >= buflen)
  432. return -1;
  433. return res;
  434. }
  435. #ifdef IEEE8021X_EAPOL
  436. static int wpa_supplicant_ctrl_iface_preauth(struct wpa_supplicant *wpa_s,
  437. char *addr)
  438. {
  439. u8 bssid[ETH_ALEN];
  440. struct wpa_ssid *ssid = wpa_s->current_ssid;
  441. if (hwaddr_aton(addr, bssid)) {
  442. wpa_printf(MSG_DEBUG, "CTRL_IFACE PREAUTH: invalid address "
  443. "'%s'", addr);
  444. return -1;
  445. }
  446. wpa_printf(MSG_DEBUG, "CTRL_IFACE PREAUTH " MACSTR, MAC2STR(bssid));
  447. rsn_preauth_deinit(wpa_s->wpa);
  448. if (rsn_preauth_init(wpa_s->wpa, bssid, ssid ? &ssid->eap : NULL))
  449. return -1;
  450. return 0;
  451. }
  452. #endif /* IEEE8021X_EAPOL */
  453. #ifdef CONFIG_PEERKEY
  454. /* MLME-STKSTART.request(peer) */
  455. static int wpa_supplicant_ctrl_iface_stkstart(
  456. struct wpa_supplicant *wpa_s, char *addr)
  457. {
  458. u8 peer[ETH_ALEN];
  459. if (hwaddr_aton(addr, peer)) {
  460. wpa_printf(MSG_DEBUG, "CTRL_IFACE STKSTART: invalid "
  461. "address '%s'", addr);
  462. return -1;
  463. }
  464. wpa_printf(MSG_DEBUG, "CTRL_IFACE STKSTART " MACSTR,
  465. MAC2STR(peer));
  466. return wpa_sm_stkstart(wpa_s->wpa, peer);
  467. }
  468. #endif /* CONFIG_PEERKEY */
  469. #ifdef CONFIG_TDLS
  470. static int wpa_supplicant_ctrl_iface_tdls_discover(
  471. struct wpa_supplicant *wpa_s, char *addr)
  472. {
  473. u8 peer[ETH_ALEN];
  474. int ret;
  475. if (hwaddr_aton(addr, peer)) {
  476. wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_DISCOVER: invalid "
  477. "address '%s'", addr);
  478. return -1;
  479. }
  480. wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_DISCOVER " MACSTR,
  481. MAC2STR(peer));
  482. if (wpa_tdls_is_external_setup(wpa_s->wpa))
  483. ret = wpa_tdls_send_discovery_request(wpa_s->wpa, peer);
  484. else
  485. ret = wpa_drv_tdls_oper(wpa_s, TDLS_DISCOVERY_REQ, peer);
  486. return ret;
  487. }
  488. static int wpa_supplicant_ctrl_iface_tdls_setup(
  489. struct wpa_supplicant *wpa_s, char *addr)
  490. {
  491. u8 peer[ETH_ALEN];
  492. int ret;
  493. if (hwaddr_aton(addr, peer)) {
  494. wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_SETUP: invalid "
  495. "address '%s'", addr);
  496. return -1;
  497. }
  498. wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_SETUP " MACSTR,
  499. MAC2STR(peer));
  500. if ((wpa_s->conf->tdls_external_control) &&
  501. wpa_tdls_is_external_setup(wpa_s->wpa))
  502. return wpa_drv_tdls_oper(wpa_s, TDLS_SETUP, peer);
  503. wpa_tdls_remove(wpa_s->wpa, peer);
  504. if (wpa_tdls_is_external_setup(wpa_s->wpa))
  505. ret = wpa_tdls_start(wpa_s->wpa, peer);
  506. else
  507. ret = wpa_drv_tdls_oper(wpa_s, TDLS_SETUP, peer);
  508. return ret;
  509. }
  510. static int wpa_supplicant_ctrl_iface_tdls_teardown(
  511. struct wpa_supplicant *wpa_s, char *addr)
  512. {
  513. u8 peer[ETH_ALEN];
  514. int ret;
  515. if (os_strcmp(addr, "*") == 0) {
  516. /* remove everyone */
  517. wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_TEARDOWN *");
  518. wpa_tdls_teardown_peers(wpa_s->wpa);
  519. return 0;
  520. }
  521. if (hwaddr_aton(addr, peer)) {
  522. wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_TEARDOWN: invalid "
  523. "address '%s'", addr);
  524. return -1;
  525. }
  526. wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_TEARDOWN " MACSTR,
  527. MAC2STR(peer));
  528. if ((wpa_s->conf->tdls_external_control) &&
  529. wpa_tdls_is_external_setup(wpa_s->wpa))
  530. return wpa_drv_tdls_oper(wpa_s, TDLS_TEARDOWN, peer);
  531. if (wpa_tdls_is_external_setup(wpa_s->wpa))
  532. ret = wpa_tdls_teardown_link(
  533. wpa_s->wpa, peer,
  534. WLAN_REASON_TDLS_TEARDOWN_UNSPECIFIED);
  535. else
  536. ret = wpa_drv_tdls_oper(wpa_s, TDLS_TEARDOWN, peer);
  537. return ret;
  538. }
  539. static int ctrl_iface_get_capability_tdls(
  540. struct wpa_supplicant *wpa_s, char *buf, size_t buflen)
  541. {
  542. int ret;
  543. ret = os_snprintf(buf, buflen, "%s\n",
  544. wpa_s->drv_flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT ?
  545. (wpa_s->drv_flags &
  546. WPA_DRIVER_FLAGS_TDLS_EXTERNAL_SETUP ?
  547. "EXTERNAL" : "INTERNAL") : "UNSUPPORTED");
  548. if (ret < 0 || (size_t) ret > buflen)
  549. return -1;
  550. return ret;
  551. }
  552. #endif /* CONFIG_TDLS */
  553. #ifdef CONFIG_IEEE80211R
  554. static int wpa_supplicant_ctrl_iface_ft_ds(
  555. struct wpa_supplicant *wpa_s, char *addr)
  556. {
  557. u8 target_ap[ETH_ALEN];
  558. struct wpa_bss *bss;
  559. const u8 *mdie;
  560. if (hwaddr_aton(addr, target_ap)) {
  561. wpa_printf(MSG_DEBUG, "CTRL_IFACE FT_DS: invalid "
  562. "address '%s'", addr);
  563. return -1;
  564. }
  565. wpa_printf(MSG_DEBUG, "CTRL_IFACE FT_DS " MACSTR, MAC2STR(target_ap));
  566. bss = wpa_bss_get_bssid(wpa_s, target_ap);
  567. if (bss)
  568. mdie = wpa_bss_get_ie(bss, WLAN_EID_MOBILITY_DOMAIN);
  569. else
  570. mdie = NULL;
  571. return wpa_ft_start_over_ds(wpa_s->wpa, target_ap, mdie);
  572. }
  573. #endif /* CONFIG_IEEE80211R */
  574. #ifdef CONFIG_WPS
  575. static int wpa_supplicant_ctrl_iface_wps_pbc(struct wpa_supplicant *wpa_s,
  576. char *cmd)
  577. {
  578. u8 bssid[ETH_ALEN], *_bssid = bssid;
  579. #ifdef CONFIG_P2P
  580. u8 p2p_dev_addr[ETH_ALEN];
  581. #endif /* CONFIG_P2P */
  582. #ifdef CONFIG_AP
  583. u8 *_p2p_dev_addr = NULL;
  584. #endif /* CONFIG_AP */
  585. if (cmd == NULL || os_strcmp(cmd, "any") == 0) {
  586. _bssid = NULL;
  587. #ifdef CONFIG_P2P
  588. } else if (os_strncmp(cmd, "p2p_dev_addr=", 13) == 0) {
  589. if (hwaddr_aton(cmd + 13, p2p_dev_addr)) {
  590. wpa_printf(MSG_DEBUG, "CTRL_IFACE WPS_PBC: invalid "
  591. "P2P Device Address '%s'",
  592. cmd + 13);
  593. return -1;
  594. }
  595. _p2p_dev_addr = p2p_dev_addr;
  596. #endif /* CONFIG_P2P */
  597. } else if (hwaddr_aton(cmd, bssid)) {
  598. wpa_printf(MSG_DEBUG, "CTRL_IFACE WPS_PBC: invalid BSSID '%s'",
  599. cmd);
  600. return -1;
  601. }
  602. #ifdef CONFIG_AP
  603. if (wpa_s->ap_iface)
  604. return wpa_supplicant_ap_wps_pbc(wpa_s, _bssid, _p2p_dev_addr);
  605. #endif /* CONFIG_AP */
  606. return wpas_wps_start_pbc(wpa_s, _bssid, 0);
  607. }
  608. static int wpa_supplicant_ctrl_iface_wps_pin(struct wpa_supplicant *wpa_s,
  609. char *cmd, char *buf,
  610. size_t buflen)
  611. {
  612. u8 bssid[ETH_ALEN], *_bssid = bssid;
  613. char *pin;
  614. int ret;
  615. pin = os_strchr(cmd, ' ');
  616. if (pin)
  617. *pin++ = '\0';
  618. if (os_strcmp(cmd, "any") == 0)
  619. _bssid = NULL;
  620. else if (os_strcmp(cmd, "get") == 0) {
  621. ret = wps_generate_pin();
  622. goto done;
  623. } else if (hwaddr_aton(cmd, bssid)) {
  624. wpa_printf(MSG_DEBUG, "CTRL_IFACE WPS_PIN: invalid BSSID '%s'",
  625. cmd);
  626. return -1;
  627. }
  628. #ifdef CONFIG_AP
  629. if (wpa_s->ap_iface) {
  630. int timeout = 0;
  631. char *pos;
  632. if (pin) {
  633. pos = os_strchr(pin, ' ');
  634. if (pos) {
  635. *pos++ = '\0';
  636. timeout = atoi(pos);
  637. }
  638. }
  639. return wpa_supplicant_ap_wps_pin(wpa_s, _bssid, pin,
  640. buf, buflen, timeout);
  641. }
  642. #endif /* CONFIG_AP */
  643. if (pin) {
  644. ret = wpas_wps_start_pin(wpa_s, _bssid, pin, 0,
  645. DEV_PW_DEFAULT);
  646. if (ret < 0)
  647. return -1;
  648. ret = os_snprintf(buf, buflen, "%s", pin);
  649. if (ret < 0 || (size_t) ret >= buflen)
  650. return -1;
  651. return ret;
  652. }
  653. ret = wpas_wps_start_pin(wpa_s, _bssid, NULL, 0, DEV_PW_DEFAULT);
  654. if (ret < 0)
  655. return -1;
  656. done:
  657. /* Return the generated PIN */
  658. ret = os_snprintf(buf, buflen, "%08d", ret);
  659. if (ret < 0 || (size_t) ret >= buflen)
  660. return -1;
  661. return ret;
  662. }
  663. static int wpa_supplicant_ctrl_iface_wps_check_pin(
  664. struct wpa_supplicant *wpa_s, char *cmd, char *buf, size_t buflen)
  665. {
  666. char pin[9];
  667. size_t len;
  668. char *pos;
  669. int ret;
  670. wpa_hexdump_ascii_key(MSG_DEBUG, "WPS_CHECK_PIN",
  671. (u8 *) cmd, os_strlen(cmd));
  672. for (pos = cmd, len = 0; *pos != '\0'; pos++) {
  673. if (*pos < '0' || *pos > '9')
  674. continue;
  675. pin[len++] = *pos;
  676. if (len == 9) {
  677. wpa_printf(MSG_DEBUG, "WPS: Too long PIN");
  678. return -1;
  679. }
  680. }
  681. if (len != 4 && len != 8) {
  682. wpa_printf(MSG_DEBUG, "WPS: Invalid PIN length %d", (int) len);
  683. return -1;
  684. }
  685. pin[len] = '\0';
  686. if (len == 8) {
  687. unsigned int pin_val;
  688. pin_val = atoi(pin);
  689. if (!wps_pin_valid(pin_val)) {
  690. wpa_printf(MSG_DEBUG, "WPS: Invalid checksum digit");
  691. ret = os_snprintf(buf, buflen, "FAIL-CHECKSUM\n");
  692. if (ret < 0 || (size_t) ret >= buflen)
  693. return -1;
  694. return ret;
  695. }
  696. }
  697. ret = os_snprintf(buf, buflen, "%s", pin);
  698. if (ret < 0 || (size_t) ret >= buflen)
  699. return -1;
  700. return ret;
  701. }
  702. #ifdef CONFIG_WPS_NFC
  703. static int wpa_supplicant_ctrl_iface_wps_nfc(struct wpa_supplicant *wpa_s,
  704. char *cmd)
  705. {
  706. u8 bssid[ETH_ALEN], *_bssid = bssid;
  707. if (cmd == NULL || cmd[0] == '\0')
  708. _bssid = NULL;
  709. else if (hwaddr_aton(cmd, bssid))
  710. return -1;
  711. return wpas_wps_start_nfc(wpa_s, NULL, _bssid, NULL, 0, 0, NULL, NULL,
  712. 0, 0);
  713. }
  714. static int wpa_supplicant_ctrl_iface_wps_nfc_config_token(
  715. struct wpa_supplicant *wpa_s, char *cmd, char *reply, size_t max_len)
  716. {
  717. int ndef;
  718. struct wpabuf *buf;
  719. int res;
  720. char *pos;
  721. pos = os_strchr(cmd, ' ');
  722. if (pos)
  723. *pos++ = '\0';
  724. if (os_strcmp(cmd, "WPS") == 0)
  725. ndef = 0;
  726. else if (os_strcmp(cmd, "NDEF") == 0)
  727. ndef = 1;
  728. else
  729. return -1;
  730. buf = wpas_wps_nfc_config_token(wpa_s, ndef, pos);
  731. if (buf == NULL)
  732. return -1;
  733. res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
  734. wpabuf_len(buf));
  735. reply[res++] = '\n';
  736. reply[res] = '\0';
  737. wpabuf_free(buf);
  738. return res;
  739. }
  740. static int wpa_supplicant_ctrl_iface_wps_nfc_token(
  741. struct wpa_supplicant *wpa_s, char *cmd, char *reply, size_t max_len)
  742. {
  743. int ndef;
  744. struct wpabuf *buf;
  745. int res;
  746. if (os_strcmp(cmd, "WPS") == 0)
  747. ndef = 0;
  748. else if (os_strcmp(cmd, "NDEF") == 0)
  749. ndef = 1;
  750. else
  751. return -1;
  752. buf = wpas_wps_nfc_token(wpa_s, ndef);
  753. if (buf == NULL)
  754. return -1;
  755. res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
  756. wpabuf_len(buf));
  757. reply[res++] = '\n';
  758. reply[res] = '\0';
  759. wpabuf_free(buf);
  760. return res;
  761. }
  762. static int wpa_supplicant_ctrl_iface_wps_nfc_tag_read(
  763. struct wpa_supplicant *wpa_s, char *pos)
  764. {
  765. size_t len;
  766. struct wpabuf *buf;
  767. int ret;
  768. char *freq;
  769. int forced_freq = 0;
  770. freq = strstr(pos, " freq=");
  771. if (freq) {
  772. *freq = '\0';
  773. freq += 6;
  774. forced_freq = atoi(freq);
  775. }
  776. len = os_strlen(pos);
  777. if (len & 0x01)
  778. return -1;
  779. len /= 2;
  780. buf = wpabuf_alloc(len);
  781. if (buf == NULL)
  782. return -1;
  783. if (hexstr2bin(pos, wpabuf_put(buf, len), len) < 0) {
  784. wpabuf_free(buf);
  785. return -1;
  786. }
  787. ret = wpas_wps_nfc_tag_read(wpa_s, buf, forced_freq);
  788. wpabuf_free(buf);
  789. return ret;
  790. }
  791. static int wpas_ctrl_nfc_get_handover_req_wps(struct wpa_supplicant *wpa_s,
  792. char *reply, size_t max_len,
  793. int ndef)
  794. {
  795. struct wpabuf *buf;
  796. int res;
  797. buf = wpas_wps_nfc_handover_req(wpa_s, ndef);
  798. if (buf == NULL)
  799. return -1;
  800. res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
  801. wpabuf_len(buf));
  802. reply[res++] = '\n';
  803. reply[res] = '\0';
  804. wpabuf_free(buf);
  805. return res;
  806. }
  807. #ifdef CONFIG_P2P
  808. static int wpas_ctrl_nfc_get_handover_req_p2p(struct wpa_supplicant *wpa_s,
  809. char *reply, size_t max_len,
  810. int ndef)
  811. {
  812. struct wpabuf *buf;
  813. int res;
  814. buf = wpas_p2p_nfc_handover_req(wpa_s, ndef);
  815. if (buf == NULL) {
  816. wpa_printf(MSG_DEBUG, "P2P: Could not generate NFC handover request");
  817. return -1;
  818. }
  819. res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
  820. wpabuf_len(buf));
  821. reply[res++] = '\n';
  822. reply[res] = '\0';
  823. wpabuf_free(buf);
  824. return res;
  825. }
  826. #endif /* CONFIG_P2P */
  827. static int wpas_ctrl_nfc_get_handover_req(struct wpa_supplicant *wpa_s,
  828. char *cmd, char *reply,
  829. size_t max_len)
  830. {
  831. char *pos;
  832. int ndef;
  833. pos = os_strchr(cmd, ' ');
  834. if (pos == NULL)
  835. return -1;
  836. *pos++ = '\0';
  837. if (os_strcmp(cmd, "WPS") == 0)
  838. ndef = 0;
  839. else if (os_strcmp(cmd, "NDEF") == 0)
  840. ndef = 1;
  841. else
  842. return -1;
  843. if (os_strcmp(pos, "WPS") == 0 || os_strcmp(pos, "WPS-CR") == 0) {
  844. if (!ndef)
  845. return -1;
  846. return wpas_ctrl_nfc_get_handover_req_wps(
  847. wpa_s, reply, max_len, ndef);
  848. }
  849. #ifdef CONFIG_P2P
  850. if (os_strcmp(pos, "P2P-CR") == 0) {
  851. return wpas_ctrl_nfc_get_handover_req_p2p(
  852. wpa_s, reply, max_len, ndef);
  853. }
  854. #endif /* CONFIG_P2P */
  855. return -1;
  856. }
  857. static int wpas_ctrl_nfc_get_handover_sel_wps(struct wpa_supplicant *wpa_s,
  858. char *reply, size_t max_len,
  859. int ndef, int cr, char *uuid)
  860. {
  861. struct wpabuf *buf;
  862. int res;
  863. buf = wpas_wps_nfc_handover_sel(wpa_s, ndef, cr, uuid);
  864. if (buf == NULL)
  865. return -1;
  866. res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
  867. wpabuf_len(buf));
  868. reply[res++] = '\n';
  869. reply[res] = '\0';
  870. wpabuf_free(buf);
  871. return res;
  872. }
  873. #ifdef CONFIG_P2P
  874. static int wpas_ctrl_nfc_get_handover_sel_p2p(struct wpa_supplicant *wpa_s,
  875. char *reply, size_t max_len,
  876. int ndef, int tag)
  877. {
  878. struct wpabuf *buf;
  879. int res;
  880. buf = wpas_p2p_nfc_handover_sel(wpa_s, ndef, tag);
  881. if (buf == NULL)
  882. return -1;
  883. res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
  884. wpabuf_len(buf));
  885. reply[res++] = '\n';
  886. reply[res] = '\0';
  887. wpabuf_free(buf);
  888. return res;
  889. }
  890. #endif /* CONFIG_P2P */
  891. static int wpas_ctrl_nfc_get_handover_sel(struct wpa_supplicant *wpa_s,
  892. char *cmd, char *reply,
  893. size_t max_len)
  894. {
  895. char *pos, *pos2;
  896. int ndef;
  897. pos = os_strchr(cmd, ' ');
  898. if (pos == NULL)
  899. return -1;
  900. *pos++ = '\0';
  901. if (os_strcmp(cmd, "WPS") == 0)
  902. ndef = 0;
  903. else if (os_strcmp(cmd, "NDEF") == 0)
  904. ndef = 1;
  905. else
  906. return -1;
  907. pos2 = os_strchr(pos, ' ');
  908. if (pos2)
  909. *pos2++ = '\0';
  910. if (os_strcmp(pos, "WPS") == 0 || os_strcmp(pos, "WPS-CR") == 0) {
  911. if (!ndef)
  912. return -1;
  913. return wpas_ctrl_nfc_get_handover_sel_wps(
  914. wpa_s, reply, max_len, ndef,
  915. os_strcmp(pos, "WPS-CR") == 0, pos2);
  916. }
  917. #ifdef CONFIG_P2P
  918. if (os_strcmp(pos, "P2P-CR") == 0) {
  919. return wpas_ctrl_nfc_get_handover_sel_p2p(
  920. wpa_s, reply, max_len, ndef, 0);
  921. }
  922. if (os_strcmp(pos, "P2P-CR-TAG") == 0) {
  923. return wpas_ctrl_nfc_get_handover_sel_p2p(
  924. wpa_s, reply, max_len, ndef, 1);
  925. }
  926. #endif /* CONFIG_P2P */
  927. return -1;
  928. }
  929. static int wpas_ctrl_nfc_report_handover(struct wpa_supplicant *wpa_s,
  930. char *cmd)
  931. {
  932. size_t len;
  933. struct wpabuf *req, *sel;
  934. int ret;
  935. char *pos, *role, *type, *pos2;
  936. #ifdef CONFIG_P2P
  937. char *freq;
  938. int forced_freq = 0;
  939. freq = strstr(cmd, " freq=");
  940. if (freq) {
  941. *freq = '\0';
  942. freq += 6;
  943. forced_freq = atoi(freq);
  944. }
  945. #endif /* CONFIG_P2P */
  946. role = cmd;
  947. pos = os_strchr(role, ' ');
  948. if (pos == NULL) {
  949. wpa_printf(MSG_DEBUG, "NFC: Missing type in handover report");
  950. return -1;
  951. }
  952. *pos++ = '\0';
  953. type = pos;
  954. pos = os_strchr(type, ' ');
  955. if (pos == NULL) {
  956. wpa_printf(MSG_DEBUG, "NFC: Missing request message in handover report");
  957. return -1;
  958. }
  959. *pos++ = '\0';
  960. pos2 = os_strchr(pos, ' ');
  961. if (pos2 == NULL) {
  962. wpa_printf(MSG_DEBUG, "NFC: Missing select message in handover report");
  963. return -1;
  964. }
  965. *pos2++ = '\0';
  966. len = os_strlen(pos);
  967. if (len & 0x01) {
  968. wpa_printf(MSG_DEBUG, "NFC: Invalid request message length in handover report");
  969. return -1;
  970. }
  971. len /= 2;
  972. req = wpabuf_alloc(len);
  973. if (req == NULL) {
  974. wpa_printf(MSG_DEBUG, "NFC: Failed to allocate memory for request message");
  975. return -1;
  976. }
  977. if (hexstr2bin(pos, wpabuf_put(req, len), len) < 0) {
  978. wpa_printf(MSG_DEBUG, "NFC: Invalid request message hexdump in handover report");
  979. wpabuf_free(req);
  980. return -1;
  981. }
  982. len = os_strlen(pos2);
  983. if (len & 0x01) {
  984. wpa_printf(MSG_DEBUG, "NFC: Invalid select message length in handover report");
  985. wpabuf_free(req);
  986. return -1;
  987. }
  988. len /= 2;
  989. sel = wpabuf_alloc(len);
  990. if (sel == NULL) {
  991. wpa_printf(MSG_DEBUG, "NFC: Failed to allocate memory for select message");
  992. wpabuf_free(req);
  993. return -1;
  994. }
  995. if (hexstr2bin(pos2, wpabuf_put(sel, len), len) < 0) {
  996. wpa_printf(MSG_DEBUG, "NFC: Invalid select message hexdump in handover report");
  997. wpabuf_free(req);
  998. wpabuf_free(sel);
  999. return -1;
  1000. }
  1001. wpa_printf(MSG_DEBUG, "NFC: Connection handover reported - role=%s type=%s req_len=%d sel_len=%d",
  1002. role, type, (int) wpabuf_len(req), (int) wpabuf_len(sel));
  1003. if (os_strcmp(role, "INIT") == 0 && os_strcmp(type, "WPS") == 0) {
  1004. ret = wpas_wps_nfc_report_handover(wpa_s, req, sel);
  1005. #ifdef CONFIG_AP
  1006. } else if (os_strcmp(role, "RESP") == 0 && os_strcmp(type, "WPS") == 0)
  1007. {
  1008. ret = wpas_ap_wps_nfc_report_handover(wpa_s, req, sel);
  1009. if (ret < 0)
  1010. ret = wpas_er_wps_nfc_report_handover(wpa_s, req, sel);
  1011. #endif /* CONFIG_AP */
  1012. #ifdef CONFIG_P2P
  1013. } else if (os_strcmp(role, "INIT") == 0 && os_strcmp(type, "P2P") == 0)
  1014. {
  1015. ret = wpas_p2p_nfc_report_handover(wpa_s, 1, req, sel, 0);
  1016. } else if (os_strcmp(role, "RESP") == 0 && os_strcmp(type, "P2P") == 0)
  1017. {
  1018. ret = wpas_p2p_nfc_report_handover(wpa_s, 0, req, sel,
  1019. forced_freq);
  1020. #endif /* CONFIG_P2P */
  1021. } else {
  1022. wpa_printf(MSG_DEBUG, "NFC: Unsupported connection handover "
  1023. "reported: role=%s type=%s", role, type);
  1024. ret = -1;
  1025. }
  1026. wpabuf_free(req);
  1027. wpabuf_free(sel);
  1028. if (ret)
  1029. wpa_printf(MSG_DEBUG, "NFC: Failed to process reported handover messages");
  1030. return ret;
  1031. }
  1032. #endif /* CONFIG_WPS_NFC */
  1033. static int wpa_supplicant_ctrl_iface_wps_reg(struct wpa_supplicant *wpa_s,
  1034. char *cmd)
  1035. {
  1036. u8 bssid[ETH_ALEN];
  1037. char *pin;
  1038. char *new_ssid;
  1039. char *new_auth;
  1040. char *new_encr;
  1041. char *new_key;
  1042. struct wps_new_ap_settings ap;
  1043. pin = os_strchr(cmd, ' ');
  1044. if (pin == NULL)
  1045. return -1;
  1046. *pin++ = '\0';
  1047. if (hwaddr_aton(cmd, bssid)) {
  1048. wpa_printf(MSG_DEBUG, "CTRL_IFACE WPS_REG: invalid BSSID '%s'",
  1049. cmd);
  1050. return -1;
  1051. }
  1052. new_ssid = os_strchr(pin, ' ');
  1053. if (new_ssid == NULL)
  1054. return wpas_wps_start_reg(wpa_s, bssid, pin, NULL);
  1055. *new_ssid++ = '\0';
  1056. new_auth = os_strchr(new_ssid, ' ');
  1057. if (new_auth == NULL)
  1058. return -1;
  1059. *new_auth++ = '\0';
  1060. new_encr = os_strchr(new_auth, ' ');
  1061. if (new_encr == NULL)
  1062. return -1;
  1063. *new_encr++ = '\0';
  1064. new_key = os_strchr(new_encr, ' ');
  1065. if (new_key == NULL)
  1066. return -1;
  1067. *new_key++ = '\0';
  1068. os_memset(&ap, 0, sizeof(ap));
  1069. ap.ssid_hex = new_ssid;
  1070. ap.auth = new_auth;
  1071. ap.encr = new_encr;
  1072. ap.key_hex = new_key;
  1073. return wpas_wps_start_reg(wpa_s, bssid, pin, &ap);
  1074. }
  1075. #ifdef CONFIG_AP
  1076. static int wpa_supplicant_ctrl_iface_wps_ap_pin(struct wpa_supplicant *wpa_s,
  1077. char *cmd, char *buf,
  1078. size_t buflen)
  1079. {
  1080. int timeout = 300;
  1081. char *pos;
  1082. const char *pin_txt;
  1083. if (!wpa_s->ap_iface)
  1084. return -1;
  1085. pos = os_strchr(cmd, ' ');
  1086. if (pos)
  1087. *pos++ = '\0';
  1088. if (os_strcmp(cmd, "disable") == 0) {
  1089. wpas_wps_ap_pin_disable(wpa_s);
  1090. return os_snprintf(buf, buflen, "OK\n");
  1091. }
  1092. if (os_strcmp(cmd, "random") == 0) {
  1093. if (pos)
  1094. timeout = atoi(pos);
  1095. pin_txt = wpas_wps_ap_pin_random(wpa_s, timeout);
  1096. if (pin_txt == NULL)
  1097. return -1;
  1098. return os_snprintf(buf, buflen, "%s", pin_txt);
  1099. }
  1100. if (os_strcmp(cmd, "get") == 0) {
  1101. pin_txt = wpas_wps_ap_pin_get(wpa_s);
  1102. if (pin_txt == NULL)
  1103. return -1;
  1104. return os_snprintf(buf, buflen, "%s", pin_txt);
  1105. }
  1106. if (os_strcmp(cmd, "set") == 0) {
  1107. char *pin;
  1108. if (pos == NULL)
  1109. return -1;
  1110. pin = pos;
  1111. pos = os_strchr(pos, ' ');
  1112. if (pos) {
  1113. *pos++ = '\0';
  1114. timeout = atoi(pos);
  1115. }
  1116. if (os_strlen(pin) > buflen)
  1117. return -1;
  1118. if (wpas_wps_ap_pin_set(wpa_s, pin, timeout) < 0)
  1119. return -1;
  1120. return os_snprintf(buf, buflen, "%s", pin);
  1121. }
  1122. return -1;
  1123. }
  1124. #endif /* CONFIG_AP */
  1125. #ifdef CONFIG_WPS_ER
  1126. static int wpa_supplicant_ctrl_iface_wps_er_pin(struct wpa_supplicant *wpa_s,
  1127. char *cmd)
  1128. {
  1129. char *uuid = cmd, *pin, *pos;
  1130. u8 addr_buf[ETH_ALEN], *addr = NULL;
  1131. pin = os_strchr(uuid, ' ');
  1132. if (pin == NULL)
  1133. return -1;
  1134. *pin++ = '\0';
  1135. pos = os_strchr(pin, ' ');
  1136. if (pos) {
  1137. *pos++ = '\0';
  1138. if (hwaddr_aton(pos, addr_buf) == 0)
  1139. addr = addr_buf;
  1140. }
  1141. return wpas_wps_er_add_pin(wpa_s, addr, uuid, pin);
  1142. }
  1143. static int wpa_supplicant_ctrl_iface_wps_er_learn(struct wpa_supplicant *wpa_s,
  1144. char *cmd)
  1145. {
  1146. char *uuid = cmd, *pin;
  1147. pin = os_strchr(uuid, ' ');
  1148. if (pin == NULL)
  1149. return -1;
  1150. *pin++ = '\0';
  1151. return wpas_wps_er_learn(wpa_s, uuid, pin);
  1152. }
  1153. static int wpa_supplicant_ctrl_iface_wps_er_set_config(
  1154. struct wpa_supplicant *wpa_s, char *cmd)
  1155. {
  1156. char *uuid = cmd, *id;
  1157. id = os_strchr(uuid, ' ');
  1158. if (id == NULL)
  1159. return -1;
  1160. *id++ = '\0';
  1161. return wpas_wps_er_set_config(wpa_s, uuid, atoi(id));
  1162. }
  1163. static int wpa_supplicant_ctrl_iface_wps_er_config(
  1164. struct wpa_supplicant *wpa_s, char *cmd)
  1165. {
  1166. char *pin;
  1167. char *new_ssid;
  1168. char *new_auth;
  1169. char *new_encr;
  1170. char *new_key;
  1171. struct wps_new_ap_settings ap;
  1172. pin = os_strchr(cmd, ' ');
  1173. if (pin == NULL)
  1174. return -1;
  1175. *pin++ = '\0';
  1176. new_ssid = os_strchr(pin, ' ');
  1177. if (new_ssid == NULL)
  1178. return -1;
  1179. *new_ssid++ = '\0';
  1180. new_auth = os_strchr(new_ssid, ' ');
  1181. if (new_auth == NULL)
  1182. return -1;
  1183. *new_auth++ = '\0';
  1184. new_encr = os_strchr(new_auth, ' ');
  1185. if (new_encr == NULL)
  1186. return -1;
  1187. *new_encr++ = '\0';
  1188. new_key = os_strchr(new_encr, ' ');
  1189. if (new_key == NULL)
  1190. return -1;
  1191. *new_key++ = '\0';
  1192. os_memset(&ap, 0, sizeof(ap));
  1193. ap.ssid_hex = new_ssid;
  1194. ap.auth = new_auth;
  1195. ap.encr = new_encr;
  1196. ap.key_hex = new_key;
  1197. return wpas_wps_er_config(wpa_s, cmd, pin, &ap);
  1198. }
  1199. #ifdef CONFIG_WPS_NFC
  1200. static int wpa_supplicant_ctrl_iface_wps_er_nfc_config_token(
  1201. struct wpa_supplicant *wpa_s, char *cmd, char *reply, size_t max_len)
  1202. {
  1203. int ndef;
  1204. struct wpabuf *buf;
  1205. int res;
  1206. char *uuid;
  1207. uuid = os_strchr(cmd, ' ');
  1208. if (uuid == NULL)
  1209. return -1;
  1210. *uuid++ = '\0';
  1211. if (os_strcmp(cmd, "WPS") == 0)
  1212. ndef = 0;
  1213. else if (os_strcmp(cmd, "NDEF") == 0)
  1214. ndef = 1;
  1215. else
  1216. return -1;
  1217. buf = wpas_wps_er_nfc_config_token(wpa_s, ndef, uuid);
  1218. if (buf == NULL)
  1219. return -1;
  1220. res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
  1221. wpabuf_len(buf));
  1222. reply[res++] = '\n';
  1223. reply[res] = '\0';
  1224. wpabuf_free(buf);
  1225. return res;
  1226. }
  1227. #endif /* CONFIG_WPS_NFC */
  1228. #endif /* CONFIG_WPS_ER */
  1229. #endif /* CONFIG_WPS */
  1230. #ifdef CONFIG_IBSS_RSN
  1231. static int wpa_supplicant_ctrl_iface_ibss_rsn(
  1232. struct wpa_supplicant *wpa_s, char *addr)
  1233. {
  1234. u8 peer[ETH_ALEN];
  1235. if (hwaddr_aton(addr, peer)) {
  1236. wpa_printf(MSG_DEBUG, "CTRL_IFACE IBSS_RSN: invalid "
  1237. "address '%s'", addr);
  1238. return -1;
  1239. }
  1240. wpa_printf(MSG_DEBUG, "CTRL_IFACE IBSS_RSN " MACSTR,
  1241. MAC2STR(peer));
  1242. return ibss_rsn_start(wpa_s->ibss_rsn, peer);
  1243. }
  1244. #endif /* CONFIG_IBSS_RSN */
  1245. static int wpa_supplicant_ctrl_iface_ctrl_rsp(struct wpa_supplicant *wpa_s,
  1246. char *rsp)
  1247. {
  1248. #ifdef IEEE8021X_EAPOL
  1249. char *pos, *id_pos;
  1250. int id;
  1251. struct wpa_ssid *ssid;
  1252. pos = os_strchr(rsp, '-');
  1253. if (pos == NULL)
  1254. return -1;
  1255. *pos++ = '\0';
  1256. id_pos = pos;
  1257. pos = os_strchr(pos, ':');
  1258. if (pos == NULL)
  1259. return -1;
  1260. *pos++ = '\0';
  1261. id = atoi(id_pos);
  1262. wpa_printf(MSG_DEBUG, "CTRL_IFACE: field=%s id=%d", rsp, id);
  1263. wpa_hexdump_ascii_key(MSG_DEBUG, "CTRL_IFACE: value",
  1264. (u8 *) pos, os_strlen(pos));
  1265. ssid = wpa_config_get_network(wpa_s->conf, id);
  1266. if (ssid == NULL) {
  1267. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find SSID id=%d "
  1268. "to update", id);
  1269. return -1;
  1270. }
  1271. return wpa_supplicant_ctrl_iface_ctrl_rsp_handle(wpa_s, ssid, rsp,
  1272. pos);
  1273. #else /* IEEE8021X_EAPOL */
  1274. wpa_printf(MSG_DEBUG, "CTRL_IFACE: 802.1X not included");
  1275. return -1;
  1276. #endif /* IEEE8021X_EAPOL */
  1277. }
  1278. static int wpa_supplicant_ctrl_iface_status(struct wpa_supplicant *wpa_s,
  1279. const char *params,
  1280. char *buf, size_t buflen)
  1281. {
  1282. char *pos, *end, tmp[30];
  1283. int res, verbose, wps, ret;
  1284. #ifdef CONFIG_HS20
  1285. const u8 *hs20;
  1286. #endif /* CONFIG_HS20 */
  1287. if (os_strcmp(params, "-DRIVER") == 0)
  1288. return wpa_drv_status(wpa_s, buf, buflen);
  1289. verbose = os_strcmp(params, "-VERBOSE") == 0;
  1290. wps = os_strcmp(params, "-WPS") == 0;
  1291. pos = buf;
  1292. end = buf + buflen;
  1293. if (wpa_s->wpa_state >= WPA_ASSOCIATED) {
  1294. struct wpa_ssid *ssid = wpa_s->current_ssid;
  1295. ret = os_snprintf(pos, end - pos, "bssid=" MACSTR "\n",
  1296. MAC2STR(wpa_s->bssid));
  1297. if (ret < 0 || ret >= end - pos)
  1298. return pos - buf;
  1299. pos += ret;
  1300. if (ssid) {
  1301. u8 *_ssid = ssid->ssid;
  1302. size_t ssid_len = ssid->ssid_len;
  1303. u8 ssid_buf[MAX_SSID_LEN];
  1304. if (ssid_len == 0) {
  1305. int _res = wpa_drv_get_ssid(wpa_s, ssid_buf);
  1306. if (_res < 0)
  1307. ssid_len = 0;
  1308. else
  1309. ssid_len = _res;
  1310. _ssid = ssid_buf;
  1311. }
  1312. ret = os_snprintf(pos, end - pos, "ssid=%s\nid=%d\n",
  1313. wpa_ssid_txt(_ssid, ssid_len),
  1314. ssid->id);
  1315. if (ret < 0 || ret >= end - pos)
  1316. return pos - buf;
  1317. pos += ret;
  1318. if (wps && ssid->passphrase &&
  1319. wpa_key_mgmt_wpa_psk(ssid->key_mgmt) &&
  1320. (ssid->mode == WPAS_MODE_AP ||
  1321. ssid->mode == WPAS_MODE_P2P_GO)) {
  1322. ret = os_snprintf(pos, end - pos,
  1323. "passphrase=%s\n",
  1324. ssid->passphrase);
  1325. if (ret < 0 || ret >= end - pos)
  1326. return pos - buf;
  1327. pos += ret;
  1328. }
  1329. if (ssid->id_str) {
  1330. ret = os_snprintf(pos, end - pos,
  1331. "id_str=%s\n",
  1332. ssid->id_str);
  1333. if (ret < 0 || ret >= end - pos)
  1334. return pos - buf;
  1335. pos += ret;
  1336. }
  1337. switch (ssid->mode) {
  1338. case WPAS_MODE_INFRA:
  1339. ret = os_snprintf(pos, end - pos,
  1340. "mode=station\n");
  1341. break;
  1342. case WPAS_MODE_IBSS:
  1343. ret = os_snprintf(pos, end - pos,
  1344. "mode=IBSS\n");
  1345. break;
  1346. case WPAS_MODE_AP:
  1347. ret = os_snprintf(pos, end - pos,
  1348. "mode=AP\n");
  1349. break;
  1350. case WPAS_MODE_P2P_GO:
  1351. ret = os_snprintf(pos, end - pos,
  1352. "mode=P2P GO\n");
  1353. break;
  1354. case WPAS_MODE_P2P_GROUP_FORMATION:
  1355. ret = os_snprintf(pos, end - pos,
  1356. "mode=P2P GO - group "
  1357. "formation\n");
  1358. break;
  1359. default:
  1360. ret = 0;
  1361. break;
  1362. }
  1363. if (ret < 0 || ret >= end - pos)
  1364. return pos - buf;
  1365. pos += ret;
  1366. }
  1367. #ifdef CONFIG_AP
  1368. if (wpa_s->ap_iface) {
  1369. pos += ap_ctrl_iface_wpa_get_status(wpa_s, pos,
  1370. end - pos,
  1371. verbose);
  1372. } else
  1373. #endif /* CONFIG_AP */
  1374. pos += wpa_sm_get_status(wpa_s->wpa, pos, end - pos, verbose);
  1375. }
  1376. #ifdef CONFIG_SAE
  1377. if (wpa_s->wpa_state >= WPA_ASSOCIATED &&
  1378. #ifdef CONFIG_AP
  1379. !wpa_s->ap_iface &&
  1380. #endif /* CONFIG_AP */
  1381. wpa_s->sme.sae.state == SAE_ACCEPTED) {
  1382. ret = os_snprintf(pos, end - pos, "sae_group=%d\n",
  1383. wpa_s->sme.sae.group);
  1384. if (ret < 0 || ret >= end - pos)
  1385. return pos - buf;
  1386. pos += ret;
  1387. }
  1388. #endif /* CONFIG_SAE */
  1389. ret = os_snprintf(pos, end - pos, "wpa_state=%s\n",
  1390. wpa_supplicant_state_txt(wpa_s->wpa_state));
  1391. if (ret < 0 || ret >= end - pos)
  1392. return pos - buf;
  1393. pos += ret;
  1394. if (wpa_s->l2 &&
  1395. l2_packet_get_ip_addr(wpa_s->l2, tmp, sizeof(tmp)) >= 0) {
  1396. ret = os_snprintf(pos, end - pos, "ip_address=%s\n", tmp);
  1397. if (ret < 0 || ret >= end - pos)
  1398. return pos - buf;
  1399. pos += ret;
  1400. }
  1401. #ifdef CONFIG_P2P
  1402. if (wpa_s->global->p2p) {
  1403. ret = os_snprintf(pos, end - pos, "p2p_device_address=" MACSTR
  1404. "\n", MAC2STR(wpa_s->global->p2p_dev_addr));
  1405. if (ret < 0 || ret >= end - pos)
  1406. return pos - buf;
  1407. pos += ret;
  1408. }
  1409. #endif /* CONFIG_P2P */
  1410. ret = os_snprintf(pos, end - pos, "address=" MACSTR "\n",
  1411. MAC2STR(wpa_s->own_addr));
  1412. if (ret < 0 || ret >= end - pos)
  1413. return pos - buf;
  1414. pos += ret;
  1415. #ifdef CONFIG_HS20
  1416. if (wpa_s->current_bss &&
  1417. (hs20 = wpa_bss_get_vendor_ie(wpa_s->current_bss,
  1418. HS20_IE_VENDOR_TYPE)) &&
  1419. wpa_s->wpa_proto == WPA_PROTO_RSN &&
  1420. wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt)) {
  1421. int release = 1;
  1422. if (hs20[1] >= 5) {
  1423. u8 rel_num = (hs20[6] & 0xf0) >> 4;
  1424. release = rel_num + 1;
  1425. }
  1426. ret = os_snprintf(pos, end - pos, "hs20=%d\n", release);
  1427. if (ret < 0 || ret >= end - pos)
  1428. return pos - buf;
  1429. pos += ret;
  1430. }
  1431. if (wpa_s->current_ssid) {
  1432. struct wpa_cred *cred;
  1433. char *type;
  1434. for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
  1435. size_t i;
  1436. if (wpa_s->current_ssid->parent_cred != cred)
  1437. continue;
  1438. if (cred->provisioning_sp) {
  1439. ret = os_snprintf(pos, end - pos,
  1440. "provisioning_sp=%s\n",
  1441. cred->provisioning_sp);
  1442. if (ret < 0 || ret >= end - pos)
  1443. return pos - buf;
  1444. pos += ret;
  1445. }
  1446. if (!cred->domain)
  1447. goto no_domain;
  1448. i = 0;
  1449. if (wpa_s->current_bss && wpa_s->current_bss->anqp) {
  1450. struct wpabuf *names =
  1451. wpa_s->current_bss->anqp->domain_name;
  1452. for (i = 0; names && i < cred->num_domain; i++)
  1453. {
  1454. if (domain_name_list_contains(
  1455. names, cred->domain[i], 1))
  1456. break;
  1457. }
  1458. if (i == cred->num_domain)
  1459. i = 0; /* show first entry by default */
  1460. }
  1461. ret = os_snprintf(pos, end - pos, "home_sp=%s\n",
  1462. cred->domain[i]);
  1463. if (ret < 0 || ret >= end - pos)
  1464. return pos - buf;
  1465. pos += ret;
  1466. no_domain:
  1467. if (wpa_s->current_bss == NULL ||
  1468. wpa_s->current_bss->anqp == NULL)
  1469. res = -1;
  1470. else
  1471. res = interworking_home_sp_cred(
  1472. wpa_s, cred,
  1473. wpa_s->current_bss->anqp->domain_name);
  1474. if (res > 0)
  1475. type = "home";
  1476. else if (res == 0)
  1477. type = "roaming";
  1478. else
  1479. type = "unknown";
  1480. ret = os_snprintf(pos, end - pos, "sp_type=%s\n", type);
  1481. if (ret < 0 || ret >= end - pos)
  1482. return pos - buf;
  1483. pos += ret;
  1484. break;
  1485. }
  1486. }
  1487. #endif /* CONFIG_HS20 */
  1488. if (wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt) ||
  1489. wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
  1490. res = eapol_sm_get_status(wpa_s->eapol, pos, end - pos,
  1491. verbose);
  1492. if (res >= 0)
  1493. pos += res;
  1494. }
  1495. res = rsn_preauth_get_status(wpa_s->wpa, pos, end - pos, verbose);
  1496. if (res >= 0)
  1497. pos += res;
  1498. #ifdef CONFIG_WPS
  1499. {
  1500. char uuid_str[100];
  1501. uuid_bin2str(wpa_s->wps->uuid, uuid_str, sizeof(uuid_str));
  1502. ret = os_snprintf(pos, end - pos, "uuid=%s\n", uuid_str);
  1503. if (ret < 0 || ret >= end - pos)
  1504. return pos - buf;
  1505. pos += ret;
  1506. }
  1507. #endif /* CONFIG_WPS */
  1508. #ifdef ANDROID
  1509. /*
  1510. * Allow using the STATUS command with default behavior, say for debug,
  1511. * i.e., don't generate a "fake" CONNECTION and SUPPLICANT_STATE_CHANGE
  1512. * events with STATUS-NO_EVENTS.
  1513. */
  1514. if (os_strcmp(params, "-NO_EVENTS")) {
  1515. wpa_msg_ctrl(wpa_s, MSG_INFO, WPA_EVENT_STATE_CHANGE
  1516. "id=%d state=%d BSSID=" MACSTR " SSID=%s",
  1517. wpa_s->current_ssid ? wpa_s->current_ssid->id : -1,
  1518. wpa_s->wpa_state,
  1519. MAC2STR(wpa_s->bssid),
  1520. wpa_s->current_ssid && wpa_s->current_ssid->ssid ?
  1521. wpa_ssid_txt(wpa_s->current_ssid->ssid,
  1522. wpa_s->current_ssid->ssid_len) : "");
  1523. if (wpa_s->wpa_state == WPA_COMPLETED) {
  1524. struct wpa_ssid *ssid = wpa_s->current_ssid;
  1525. wpa_msg_ctrl(wpa_s, MSG_INFO, WPA_EVENT_CONNECTED
  1526. "- connection to " MACSTR
  1527. " completed %s [id=%d id_str=%s]",
  1528. MAC2STR(wpa_s->bssid), "(auth)",
  1529. ssid ? ssid->id : -1,
  1530. ssid && ssid->id_str ? ssid->id_str : "");
  1531. }
  1532. }
  1533. #endif /* ANDROID */
  1534. return pos - buf;
  1535. }
  1536. static int wpa_supplicant_ctrl_iface_bssid(struct wpa_supplicant *wpa_s,
  1537. char *cmd)
  1538. {
  1539. char *pos;
  1540. int id;
  1541. struct wpa_ssid *ssid;
  1542. u8 bssid[ETH_ALEN];
  1543. /* cmd: "<network id> <BSSID>" */
  1544. pos = os_strchr(cmd, ' ');
  1545. if (pos == NULL)
  1546. return -1;
  1547. *pos++ = '\0';
  1548. id = atoi(cmd);
  1549. wpa_printf(MSG_DEBUG, "CTRL_IFACE: id=%d bssid='%s'", id, pos);
  1550. if (hwaddr_aton(pos, bssid)) {
  1551. wpa_printf(MSG_DEBUG ,"CTRL_IFACE: invalid BSSID '%s'", pos);
  1552. return -1;
  1553. }
  1554. ssid = wpa_config_get_network(wpa_s->conf, id);
  1555. if (ssid == NULL) {
  1556. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find SSID id=%d "
  1557. "to update", id);
  1558. return -1;
  1559. }
  1560. os_memcpy(ssid->bssid, bssid, ETH_ALEN);
  1561. ssid->bssid_set = !is_zero_ether_addr(bssid);
  1562. return 0;
  1563. }
  1564. static int wpa_supplicant_ctrl_iface_blacklist(struct wpa_supplicant *wpa_s,
  1565. char *cmd, char *buf,
  1566. size_t buflen)
  1567. {
  1568. u8 bssid[ETH_ALEN];
  1569. struct wpa_blacklist *e;
  1570. char *pos, *end;
  1571. int ret;
  1572. /* cmd: "BLACKLIST [<BSSID>]" */
  1573. if (*cmd == '\0') {
  1574. pos = buf;
  1575. end = buf + buflen;
  1576. e = wpa_s->blacklist;
  1577. while (e) {
  1578. ret = os_snprintf(pos, end - pos, MACSTR "\n",
  1579. MAC2STR(e->bssid));
  1580. if (ret < 0 || ret >= end - pos)
  1581. return pos - buf;
  1582. pos += ret;
  1583. e = e->next;
  1584. }
  1585. return pos - buf;
  1586. }
  1587. cmd++;
  1588. if (os_strncmp(cmd, "clear", 5) == 0) {
  1589. wpa_blacklist_clear(wpa_s);
  1590. os_memcpy(buf, "OK\n", 3);
  1591. return 3;
  1592. }
  1593. wpa_printf(MSG_DEBUG, "CTRL_IFACE: BLACKLIST bssid='%s'", cmd);
  1594. if (hwaddr_aton(cmd, bssid)) {
  1595. wpa_printf(MSG_DEBUG, "CTRL_IFACE: invalid BSSID '%s'", cmd);
  1596. return -1;
  1597. }
  1598. /*
  1599. * Add the BSSID twice, so its count will be 2, causing it to be
  1600. * skipped when processing scan results.
  1601. */
  1602. ret = wpa_blacklist_add(wpa_s, bssid);
  1603. if (ret < 0)
  1604. return -1;
  1605. ret = wpa_blacklist_add(wpa_s, bssid);
  1606. if (ret < 0)
  1607. return -1;
  1608. os_memcpy(buf, "OK\n", 3);
  1609. return 3;
  1610. }
  1611. static const char * debug_level_str(int level)
  1612. {
  1613. switch (level) {
  1614. case MSG_EXCESSIVE:
  1615. return "EXCESSIVE";
  1616. case MSG_MSGDUMP:
  1617. return "MSGDUMP";
  1618. case MSG_DEBUG:
  1619. return "DEBUG";
  1620. case MSG_INFO:
  1621. return "INFO";
  1622. case MSG_WARNING:
  1623. return "WARNING";
  1624. case MSG_ERROR:
  1625. return "ERROR";
  1626. default:
  1627. return "?";
  1628. }
  1629. }
  1630. static int str_to_debug_level(const char *s)
  1631. {
  1632. if (os_strcasecmp(s, "EXCESSIVE") == 0)
  1633. return MSG_EXCESSIVE;
  1634. if (os_strcasecmp(s, "MSGDUMP") == 0)
  1635. return MSG_MSGDUMP;
  1636. if (os_strcasecmp(s, "DEBUG") == 0)
  1637. return MSG_DEBUG;
  1638. if (os_strcasecmp(s, "INFO") == 0)
  1639. return MSG_INFO;
  1640. if (os_strcasecmp(s, "WARNING") == 0)
  1641. return MSG_WARNING;
  1642. if (os_strcasecmp(s, "ERROR") == 0)
  1643. return MSG_ERROR;
  1644. return -1;
  1645. }
  1646. static int wpa_supplicant_ctrl_iface_log_level(struct wpa_supplicant *wpa_s,
  1647. char *cmd, char *buf,
  1648. size_t buflen)
  1649. {
  1650. char *pos, *end, *stamp;
  1651. int ret;
  1652. if (cmd == NULL) {
  1653. return -1;
  1654. }
  1655. /* cmd: "LOG_LEVEL [<level>]" */
  1656. if (*cmd == '\0') {
  1657. pos = buf;
  1658. end = buf + buflen;
  1659. ret = os_snprintf(pos, end - pos, "Current level: %s\n"
  1660. "Timestamp: %d\n",
  1661. debug_level_str(wpa_debug_level),
  1662. wpa_debug_timestamp);
  1663. if (ret < 0 || ret >= end - pos)
  1664. ret = 0;
  1665. return ret;
  1666. }
  1667. while (*cmd == ' ')
  1668. cmd++;
  1669. stamp = os_strchr(cmd, ' ');
  1670. if (stamp) {
  1671. *stamp++ = '\0';
  1672. while (*stamp == ' ') {
  1673. stamp++;
  1674. }
  1675. }
  1676. if (cmd && os_strlen(cmd)) {
  1677. int level = str_to_debug_level(cmd);
  1678. if (level < 0)
  1679. return -1;
  1680. wpa_debug_level = level;
  1681. }
  1682. if (stamp && os_strlen(stamp))
  1683. wpa_debug_timestamp = atoi(stamp);
  1684. os_memcpy(buf, "OK\n", 3);
  1685. return 3;
  1686. }
  1687. static int wpa_supplicant_ctrl_iface_list_networks(
  1688. struct wpa_supplicant *wpa_s, char *buf, size_t buflen)
  1689. {
  1690. char *pos, *end;
  1691. struct wpa_ssid *ssid;
  1692. int ret;
  1693. pos = buf;
  1694. end = buf + buflen;
  1695. ret = os_snprintf(pos, end - pos,
  1696. "network id / ssid / bssid / flags\n");
  1697. if (ret < 0 || ret >= end - pos)
  1698. return pos - buf;
  1699. pos += ret;
  1700. ssid = wpa_s->conf->ssid;
  1701. while (ssid) {
  1702. ret = os_snprintf(pos, end - pos, "%d\t%s",
  1703. ssid->id,
  1704. wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
  1705. if (ret < 0 || ret >= end - pos)
  1706. return pos - buf;
  1707. pos += ret;
  1708. if (ssid->bssid_set) {
  1709. ret = os_snprintf(pos, end - pos, "\t" MACSTR,
  1710. MAC2STR(ssid->bssid));
  1711. } else {
  1712. ret = os_snprintf(pos, end - pos, "\tany");
  1713. }
  1714. if (ret < 0 || ret >= end - pos)
  1715. return pos - buf;
  1716. pos += ret;
  1717. ret = os_snprintf(pos, end - pos, "\t%s%s%s%s",
  1718. ssid == wpa_s->current_ssid ?
  1719. "[CURRENT]" : "",
  1720. ssid->disabled ? "[DISABLED]" : "",
  1721. ssid->disabled_until.sec ?
  1722. "[TEMP-DISABLED]" : "",
  1723. ssid->disabled == 2 ? "[P2P-PERSISTENT]" :
  1724. "");
  1725. if (ret < 0 || ret >= end - pos)
  1726. return pos - buf;
  1727. pos += ret;
  1728. ret = os_snprintf(pos, end - pos, "\n");
  1729. if (ret < 0 || ret >= end - pos)
  1730. return pos - buf;
  1731. pos += ret;
  1732. ssid = ssid->next;
  1733. }
  1734. return pos - buf;
  1735. }
  1736. static char * wpa_supplicant_cipher_txt(char *pos, char *end, int cipher)
  1737. {
  1738. int ret;
  1739. ret = os_snprintf(pos, end - pos, "-");
  1740. if (ret < 0 || ret >= end - pos)
  1741. return pos;
  1742. pos += ret;
  1743. ret = wpa_write_ciphers(pos, end, cipher, "+");
  1744. if (ret < 0)
  1745. return pos;
  1746. pos += ret;
  1747. return pos;
  1748. }
  1749. static char * wpa_supplicant_ie_txt(char *pos, char *end, const char *proto,
  1750. const u8 *ie, size_t ie_len)
  1751. {
  1752. struct wpa_ie_data data;
  1753. char *start;
  1754. int ret;
  1755. ret = os_snprintf(pos, end - pos, "[%s-", proto);
  1756. if (ret < 0 || ret >= end - pos)
  1757. return pos;
  1758. pos += ret;
  1759. if (wpa_parse_wpa_ie(ie, ie_len, &data) < 0) {
  1760. ret = os_snprintf(pos, end - pos, "?]");
  1761. if (ret < 0 || ret >= end - pos)
  1762. return pos;
  1763. pos += ret;
  1764. return pos;
  1765. }
  1766. start = pos;
  1767. if (data.key_mgmt & WPA_KEY_MGMT_IEEE8021X) {
  1768. ret = os_snprintf(pos, end - pos, "%sEAP",
  1769. pos == start ? "" : "+");
  1770. if (ret < 0 || ret >= end - pos)
  1771. return pos;
  1772. pos += ret;
  1773. }
  1774. if (data.key_mgmt & WPA_KEY_MGMT_PSK) {
  1775. ret = os_snprintf(pos, end - pos, "%sPSK",
  1776. pos == start ? "" : "+");
  1777. if (ret < 0 || ret >= end - pos)
  1778. return pos;
  1779. pos += ret;
  1780. }
  1781. if (data.key_mgmt & WPA_KEY_MGMT_WPA_NONE) {
  1782. ret = os_snprintf(pos, end - pos, "%sNone",
  1783. pos == start ? "" : "+");
  1784. if (ret < 0 || ret >= end - pos)
  1785. return pos;
  1786. pos += ret;
  1787. }
  1788. #ifdef CONFIG_IEEE80211R
  1789. if (data.key_mgmt & WPA_KEY_MGMT_FT_IEEE8021X) {
  1790. ret = os_snprintf(pos, end - pos, "%sFT/EAP",
  1791. pos == start ? "" : "+");
  1792. if (ret < 0 || ret >= end - pos)
  1793. return pos;
  1794. pos += ret;
  1795. }
  1796. if (data.key_mgmt & WPA_KEY_MGMT_FT_PSK) {
  1797. ret = os_snprintf(pos, end - pos, "%sFT/PSK",
  1798. pos == start ? "" : "+");
  1799. if (ret < 0 || ret >= end - pos)
  1800. return pos;
  1801. pos += ret;
  1802. }
  1803. #endif /* CONFIG_IEEE80211R */
  1804. #ifdef CONFIG_IEEE80211W
  1805. if (data.key_mgmt & WPA_KEY_MGMT_IEEE8021X_SHA256) {
  1806. ret = os_snprintf(pos, end - pos, "%sEAP-SHA256",
  1807. pos == start ? "" : "+");
  1808. if (ret < 0 || ret >= end - pos)
  1809. return pos;
  1810. pos += ret;
  1811. }
  1812. if (data.key_mgmt & WPA_KEY_MGMT_PSK_SHA256) {
  1813. ret = os_snprintf(pos, end - pos, "%sPSK-SHA256",
  1814. pos == start ? "" : "+");
  1815. if (ret < 0 || ret >= end - pos)
  1816. return pos;
  1817. pos += ret;
  1818. }
  1819. #endif /* CONFIG_IEEE80211W */
  1820. pos = wpa_supplicant_cipher_txt(pos, end, data.pairwise_cipher);
  1821. if (data.capabilities & WPA_CAPABILITY_PREAUTH) {
  1822. ret = os_snprintf(pos, end - pos, "-preauth");
  1823. if (ret < 0 || ret >= end - pos)
  1824. return pos;
  1825. pos += ret;
  1826. }
  1827. ret = os_snprintf(pos, end - pos, "]");
  1828. if (ret < 0 || ret >= end - pos)
  1829. return pos;
  1830. pos += ret;
  1831. return pos;
  1832. }
  1833. #ifdef CONFIG_WPS
  1834. static char * wpa_supplicant_wps_ie_txt_buf(struct wpa_supplicant *wpa_s,
  1835. char *pos, char *end,
  1836. struct wpabuf *wps_ie)
  1837. {
  1838. int ret;
  1839. const char *txt;
  1840. if (wps_ie == NULL)
  1841. return pos;
  1842. if (wps_is_selected_pbc_registrar(wps_ie))
  1843. txt = "[WPS-PBC]";
  1844. else if (wps_is_addr_authorized(wps_ie, wpa_s->own_addr, 0))
  1845. txt = "[WPS-AUTH]";
  1846. else if (wps_is_selected_pin_registrar(wps_ie))
  1847. txt = "[WPS-PIN]";
  1848. else
  1849. txt = "[WPS]";
  1850. ret = os_snprintf(pos, end - pos, "%s", txt);
  1851. if (ret >= 0 && ret < end - pos)
  1852. pos += ret;
  1853. wpabuf_free(wps_ie);
  1854. return pos;
  1855. }
  1856. #endif /* CONFIG_WPS */
  1857. static char * wpa_supplicant_wps_ie_txt(struct wpa_supplicant *wpa_s,
  1858. char *pos, char *end,
  1859. const struct wpa_bss *bss)
  1860. {
  1861. #ifdef CONFIG_WPS
  1862. struct wpabuf *wps_ie;
  1863. wps_ie = wpa_bss_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
  1864. return wpa_supplicant_wps_ie_txt_buf(wpa_s, pos, end, wps_ie);
  1865. #else /* CONFIG_WPS */
  1866. return pos;
  1867. #endif /* CONFIG_WPS */
  1868. }
  1869. /* Format one result on one text line into a buffer. */
  1870. static int wpa_supplicant_ctrl_iface_scan_result(
  1871. struct wpa_supplicant *wpa_s,
  1872. const struct wpa_bss *bss, char *buf, size_t buflen)
  1873. {
  1874. char *pos, *end;
  1875. int ret;
  1876. const u8 *ie, *ie2, *p2p;
  1877. p2p = wpa_bss_get_vendor_ie(bss, P2P_IE_VENDOR_TYPE);
  1878. if (!p2p)
  1879. p2p = wpa_bss_get_vendor_ie_beacon(bss, P2P_IE_VENDOR_TYPE);
  1880. if (p2p && bss->ssid_len == P2P_WILDCARD_SSID_LEN &&
  1881. os_memcmp(bss->ssid, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN) ==
  1882. 0)
  1883. return 0; /* Do not show P2P listen discovery results here */
  1884. pos = buf;
  1885. end = buf + buflen;
  1886. ret = os_snprintf(pos, end - pos, MACSTR "\t%d\t%d\t",
  1887. MAC2STR(bss->bssid), bss->freq, bss->level);
  1888. if (ret < 0 || ret >= end - pos)
  1889. return -1;
  1890. pos += ret;
  1891. ie = wpa_bss_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
  1892. if (ie)
  1893. pos = wpa_supplicant_ie_txt(pos, end, "WPA", ie, 2 + ie[1]);
  1894. ie2 = wpa_bss_get_ie(bss, WLAN_EID_RSN);
  1895. if (ie2)
  1896. pos = wpa_supplicant_ie_txt(pos, end, "WPA2", ie2, 2 + ie2[1]);
  1897. pos = wpa_supplicant_wps_ie_txt(wpa_s, pos, end, bss);
  1898. if (!ie && !ie2 && bss->caps & IEEE80211_CAP_PRIVACY) {
  1899. ret = os_snprintf(pos, end - pos, "[WEP]");
  1900. if (ret < 0 || ret >= end - pos)
  1901. return -1;
  1902. pos += ret;
  1903. }
  1904. if (bss_is_dmg(bss)) {
  1905. const char *s;
  1906. ret = os_snprintf(pos, end - pos, "[DMG]");
  1907. if (ret < 0 || ret >= end - pos)
  1908. return -1;
  1909. pos += ret;
  1910. switch (bss->caps & IEEE80211_CAP_DMG_MASK) {
  1911. case IEEE80211_CAP_DMG_IBSS:
  1912. s = "[IBSS]";
  1913. break;
  1914. case IEEE80211_CAP_DMG_AP:
  1915. s = "[ESS]";
  1916. break;
  1917. case IEEE80211_CAP_DMG_PBSS:
  1918. s = "[PBSS]";
  1919. break;
  1920. default:
  1921. s = "";
  1922. break;
  1923. }
  1924. ret = os_snprintf(pos, end - pos, "%s", s);
  1925. if (ret < 0 || ret >= end - pos)
  1926. return -1;
  1927. pos += ret;
  1928. } else {
  1929. if (bss->caps & IEEE80211_CAP_IBSS) {
  1930. ret = os_snprintf(pos, end - pos, "[IBSS]");
  1931. if (ret < 0 || ret >= end - pos)
  1932. return -1;
  1933. pos += ret;
  1934. }
  1935. if (bss->caps & IEEE80211_CAP_ESS) {
  1936. ret = os_snprintf(pos, end - pos, "[ESS]");
  1937. if (ret < 0 || ret >= end - pos)
  1938. return -1;
  1939. pos += ret;
  1940. }
  1941. }
  1942. if (p2p) {
  1943. ret = os_snprintf(pos, end - pos, "[P2P]");
  1944. if (ret < 0 || ret >= end - pos)
  1945. return -1;
  1946. pos += ret;
  1947. }
  1948. #ifdef CONFIG_HS20
  1949. if (wpa_bss_get_vendor_ie(bss, HS20_IE_VENDOR_TYPE) && ie2) {
  1950. ret = os_snprintf(pos, end - pos, "[HS20]");
  1951. if (ret < 0 || ret >= end - pos)
  1952. return -1;
  1953. pos += ret;
  1954. }
  1955. #endif /* CONFIG_HS20 */
  1956. ret = os_snprintf(pos, end - pos, "\t%s",
  1957. wpa_ssid_txt(bss->ssid, bss->ssid_len));
  1958. if (ret < 0 || ret >= end - pos)
  1959. return -1;
  1960. pos += ret;
  1961. ret = os_snprintf(pos, end - pos, "\n");
  1962. if (ret < 0 || ret >= end - pos)
  1963. return -1;
  1964. pos += ret;
  1965. return pos - buf;
  1966. }
  1967. static int wpa_supplicant_ctrl_iface_scan_results(
  1968. struct wpa_supplicant *wpa_s, char *buf, size_t buflen)
  1969. {
  1970. char *pos, *end;
  1971. struct wpa_bss *bss;
  1972. int ret;
  1973. pos = buf;
  1974. end = buf + buflen;
  1975. ret = os_snprintf(pos, end - pos, "bssid / frequency / signal level / "
  1976. "flags / ssid\n");
  1977. if (ret < 0 || ret >= end - pos)
  1978. return pos - buf;
  1979. pos += ret;
  1980. dl_list_for_each(bss, &wpa_s->bss_id, struct wpa_bss, list_id) {
  1981. ret = wpa_supplicant_ctrl_iface_scan_result(wpa_s, bss, pos,
  1982. end - pos);
  1983. if (ret < 0 || ret >= end - pos)
  1984. return pos - buf;
  1985. pos += ret;
  1986. }
  1987. return pos - buf;
  1988. }
  1989. static int wpa_supplicant_ctrl_iface_select_network(
  1990. struct wpa_supplicant *wpa_s, char *cmd)
  1991. {
  1992. int id;
  1993. struct wpa_ssid *ssid;
  1994. char *pos;
  1995. /* cmd: "<network id>" or "any" */
  1996. if (os_strncmp(cmd, "any", 3) == 0) {
  1997. wpa_printf(MSG_DEBUG, "CTRL_IFACE: SELECT_NETWORK any");
  1998. ssid = NULL;
  1999. } else {
  2000. id = atoi(cmd);
  2001. wpa_printf(MSG_DEBUG, "CTRL_IFACE: SELECT_NETWORK id=%d", id);
  2002. ssid = wpa_config_get_network(wpa_s->conf, id);
  2003. if (ssid == NULL) {
  2004. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find "
  2005. "network id=%d", id);
  2006. return -1;
  2007. }
  2008. if (ssid->disabled == 2) {
  2009. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Cannot use "
  2010. "SELECT_NETWORK with persistent P2P group");
  2011. return -1;
  2012. }
  2013. }
  2014. pos = os_strstr(cmd, " freq=");
  2015. if (pos) {
  2016. int *freqs = freq_range_to_channel_list(wpa_s, pos + 6);
  2017. if (freqs) {
  2018. wpa_s->scan_req = MANUAL_SCAN_REQ;
  2019. os_free(wpa_s->manual_scan_freqs);
  2020. wpa_s->manual_scan_freqs = freqs;
  2021. }
  2022. }
  2023. wpa_supplicant_select_network(wpa_s, ssid);
  2024. return 0;
  2025. }
  2026. static int wpa_supplicant_ctrl_iface_enable_network(
  2027. struct wpa_supplicant *wpa_s, char *cmd)
  2028. {
  2029. int id;
  2030. struct wpa_ssid *ssid;
  2031. /* cmd: "<network id>" or "all" */
  2032. if (os_strcmp(cmd, "all") == 0) {
  2033. wpa_printf(MSG_DEBUG, "CTRL_IFACE: ENABLE_NETWORK all");
  2034. ssid = NULL;
  2035. } else {
  2036. id = atoi(cmd);
  2037. wpa_printf(MSG_DEBUG, "CTRL_IFACE: ENABLE_NETWORK id=%d", id);
  2038. ssid = wpa_config_get_network(wpa_s->conf, id);
  2039. if (ssid == NULL) {
  2040. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find "
  2041. "network id=%d", id);
  2042. return -1;
  2043. }
  2044. if (ssid->disabled == 2) {
  2045. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Cannot use "
  2046. "ENABLE_NETWORK with persistent P2P group");
  2047. return -1;
  2048. }
  2049. if (os_strstr(cmd, " no-connect")) {
  2050. ssid->disabled = 0;
  2051. return 0;
  2052. }
  2053. }
  2054. wpa_supplicant_enable_network(wpa_s, ssid);
  2055. return 0;
  2056. }
  2057. static int wpa_supplicant_ctrl_iface_disable_network(
  2058. struct wpa_supplicant *wpa_s, char *cmd)
  2059. {
  2060. int id;
  2061. struct wpa_ssid *ssid;
  2062. /* cmd: "<network id>" or "all" */
  2063. if (os_strcmp(cmd, "all") == 0) {
  2064. wpa_printf(MSG_DEBUG, "CTRL_IFACE: DISABLE_NETWORK all");
  2065. ssid = NULL;
  2066. } else {
  2067. id = atoi(cmd);
  2068. wpa_printf(MSG_DEBUG, "CTRL_IFACE: DISABLE_NETWORK id=%d", id);
  2069. ssid = wpa_config_get_network(wpa_s->conf, id);
  2070. if (ssid == NULL) {
  2071. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find "
  2072. "network id=%d", id);
  2073. return -1;
  2074. }
  2075. if (ssid->disabled == 2) {
  2076. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Cannot use "
  2077. "DISABLE_NETWORK with persistent P2P "
  2078. "group");
  2079. return -1;
  2080. }
  2081. }
  2082. wpa_supplicant_disable_network(wpa_s, ssid);
  2083. return 0;
  2084. }
  2085. static int wpa_supplicant_ctrl_iface_add_network(
  2086. struct wpa_supplicant *wpa_s, char *buf, size_t buflen)
  2087. {
  2088. struct wpa_ssid *ssid;
  2089. int ret;
  2090. wpa_printf(MSG_DEBUG, "CTRL_IFACE: ADD_NETWORK");
  2091. ssid = wpa_config_add_network(wpa_s->conf);
  2092. if (ssid == NULL)
  2093. return -1;
  2094. wpas_notify_network_added(wpa_s, ssid);
  2095. ssid->disabled = 1;
  2096. wpa_config_set_network_defaults(ssid);
  2097. ret = os_snprintf(buf, buflen, "%d\n", ssid->id);
  2098. if (ret < 0 || (size_t) ret >= buflen)
  2099. return -1;
  2100. return ret;
  2101. }
  2102. static int wpa_supplicant_ctrl_iface_remove_network(
  2103. struct wpa_supplicant *wpa_s, char *cmd)
  2104. {
  2105. int id;
  2106. struct wpa_ssid *ssid;
  2107. int was_disabled;
  2108. /* cmd: "<network id>" or "all" */
  2109. if (os_strcmp(cmd, "all") == 0) {
  2110. wpa_printf(MSG_DEBUG, "CTRL_IFACE: REMOVE_NETWORK all");
  2111. if (wpa_s->sched_scanning)
  2112. wpa_supplicant_cancel_sched_scan(wpa_s);
  2113. eapol_sm_invalidate_cached_session(wpa_s->eapol);
  2114. if (wpa_s->current_ssid) {
  2115. #ifdef CONFIG_SME
  2116. wpa_s->sme.prev_bssid_set = 0;
  2117. #endif /* CONFIG_SME */
  2118. wpa_sm_set_config(wpa_s->wpa, NULL);
  2119. eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
  2120. wpa_supplicant_deauthenticate(
  2121. wpa_s, WLAN_REASON_DEAUTH_LEAVING);
  2122. }
  2123. ssid = wpa_s->conf->ssid;
  2124. while (ssid) {
  2125. struct wpa_ssid *remove_ssid = ssid;
  2126. id = ssid->id;
  2127. ssid = ssid->next;
  2128. wpas_notify_network_removed(wpa_s, remove_ssid);
  2129. wpa_config_remove_network(wpa_s->conf, id);
  2130. }
  2131. return 0;
  2132. }
  2133. id = atoi(cmd);
  2134. wpa_printf(MSG_DEBUG, "CTRL_IFACE: REMOVE_NETWORK id=%d", id);
  2135. ssid = wpa_config_get_network(wpa_s->conf, id);
  2136. if (ssid)
  2137. wpas_notify_network_removed(wpa_s, ssid);
  2138. if (ssid == NULL) {
  2139. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find network "
  2140. "id=%d", id);
  2141. return -1;
  2142. }
  2143. if (ssid == wpa_s->current_ssid || wpa_s->current_ssid == NULL) {
  2144. #ifdef CONFIG_SME
  2145. wpa_s->sme.prev_bssid_set = 0;
  2146. #endif /* CONFIG_SME */
  2147. /*
  2148. * Invalidate the EAP session cache if the current or
  2149. * previously used network is removed.
  2150. */
  2151. eapol_sm_invalidate_cached_session(wpa_s->eapol);
  2152. }
  2153. if (ssid == wpa_s->current_ssid) {
  2154. wpa_sm_set_config(wpa_s->wpa, NULL);
  2155. eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
  2156. wpa_supplicant_deauthenticate(wpa_s,
  2157. WLAN_REASON_DEAUTH_LEAVING);
  2158. }
  2159. was_disabled = ssid->disabled;
  2160. if (wpa_config_remove_network(wpa_s->conf, id) < 0) {
  2161. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Not able to remove the "
  2162. "network id=%d", id);
  2163. return -1;
  2164. }
  2165. if (!was_disabled && wpa_s->sched_scanning) {
  2166. wpa_printf(MSG_DEBUG, "Stop ongoing sched_scan to remove "
  2167. "network from filters");
  2168. wpa_supplicant_cancel_sched_scan(wpa_s);
  2169. wpa_supplicant_req_scan(wpa_s, 0, 0);
  2170. }
  2171. return 0;
  2172. }
  2173. static int wpa_supplicant_ctrl_iface_update_network(
  2174. struct wpa_supplicant *wpa_s, struct wpa_ssid *ssid,
  2175. char *name, char *value)
  2176. {
  2177. if (wpa_config_set(ssid, name, value, 0) < 0) {
  2178. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Failed to set network "
  2179. "variable '%s'", name);
  2180. return -1;
  2181. }
  2182. if (os_strcmp(name, "bssid") != 0 &&
  2183. os_strcmp(name, "priority") != 0)
  2184. wpa_sm_pmksa_cache_flush(wpa_s->wpa, ssid);
  2185. if (wpa_s->current_ssid == ssid || wpa_s->current_ssid == NULL) {
  2186. /*
  2187. * Invalidate the EAP session cache if anything in the current
  2188. * or previously used configuration changes.
  2189. */
  2190. eapol_sm_invalidate_cached_session(wpa_s->eapol);
  2191. }
  2192. if ((os_strcmp(name, "psk") == 0 &&
  2193. value[0] == '"' && ssid->ssid_len) ||
  2194. (os_strcmp(name, "ssid") == 0 && ssid->passphrase))
  2195. wpa_config_update_psk(ssid);
  2196. else if (os_strcmp(name, "priority") == 0)
  2197. wpa_config_update_prio_list(wpa_s->conf);
  2198. return 0;
  2199. }
  2200. static int wpa_supplicant_ctrl_iface_set_network(
  2201. struct wpa_supplicant *wpa_s, char *cmd)
  2202. {
  2203. int id, ret, prev_bssid_set;
  2204. struct wpa_ssid *ssid;
  2205. char *name, *value;
  2206. u8 prev_bssid[ETH_ALEN];
  2207. /* cmd: "<network id> <variable name> <value>" */
  2208. name = os_strchr(cmd, ' ');
  2209. if (name == NULL)
  2210. return -1;
  2211. *name++ = '\0';
  2212. value = os_strchr(name, ' ');
  2213. if (value == NULL)
  2214. return -1;
  2215. *value++ = '\0';
  2216. id = atoi(cmd);
  2217. wpa_printf(MSG_DEBUG, "CTRL_IFACE: SET_NETWORK id=%d name='%s'",
  2218. id, name);
  2219. wpa_hexdump_ascii_key(MSG_DEBUG, "CTRL_IFACE: value",
  2220. (u8 *) value, os_strlen(value));
  2221. ssid = wpa_config_get_network(wpa_s->conf, id);
  2222. if (ssid == NULL) {
  2223. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find network "
  2224. "id=%d", id);
  2225. return -1;
  2226. }
  2227. prev_bssid_set = ssid->bssid_set;
  2228. os_memcpy(prev_bssid, ssid->bssid, ETH_ALEN);
  2229. ret = wpa_supplicant_ctrl_iface_update_network(wpa_s, ssid, name,
  2230. value);
  2231. if (ret == 0 &&
  2232. (ssid->bssid_set != prev_bssid_set ||
  2233. os_memcmp(ssid->bssid, prev_bssid, ETH_ALEN) != 0))
  2234. wpas_notify_network_bssid_set_changed(wpa_s, ssid);
  2235. return ret;
  2236. }
  2237. static int wpa_supplicant_ctrl_iface_get_network(
  2238. struct wpa_supplicant *wpa_s, char *cmd, char *buf, size_t buflen)
  2239. {
  2240. int id;
  2241. size_t res;
  2242. struct wpa_ssid *ssid;
  2243. char *name, *value;
  2244. /* cmd: "<network id> <variable name>" */
  2245. name = os_strchr(cmd, ' ');
  2246. if (name == NULL || buflen == 0)
  2247. return -1;
  2248. *name++ = '\0';
  2249. id = atoi(cmd);
  2250. wpa_printf(MSG_DEBUG, "CTRL_IFACE: GET_NETWORK id=%d name='%s'",
  2251. id, name);
  2252. ssid = wpa_config_get_network(wpa_s->conf, id);
  2253. if (ssid == NULL) {
  2254. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find network "
  2255. "id=%d", id);
  2256. return -1;
  2257. }
  2258. value = wpa_config_get_no_key(ssid, name);
  2259. if (value == NULL) {
  2260. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Failed to get network "
  2261. "variable '%s'", name);
  2262. return -1;
  2263. }
  2264. res = os_strlcpy(buf, value, buflen);
  2265. if (res >= buflen) {
  2266. os_free(value);
  2267. return -1;
  2268. }
  2269. os_free(value);
  2270. return res;
  2271. }
  2272. static int wpa_supplicant_ctrl_iface_dup_network(
  2273. struct wpa_supplicant *wpa_s, char *cmd)
  2274. {
  2275. struct wpa_ssid *ssid_s, *ssid_d;
  2276. char *name, *id, *value;
  2277. int id_s, id_d, ret;
  2278. /* cmd: "<src network id> <dst network id> <variable name>" */
  2279. id = os_strchr(cmd, ' ');
  2280. if (id == NULL)
  2281. return -1;
  2282. *id++ = '\0';
  2283. name = os_strchr(id, ' ');
  2284. if (name == NULL)
  2285. return -1;
  2286. *name++ = '\0';
  2287. id_s = atoi(cmd);
  2288. id_d = atoi(id);
  2289. wpa_printf(MSG_DEBUG, "CTRL_IFACE: DUP_NETWORK id=%d -> %d name='%s'",
  2290. id_s, id_d, name);
  2291. ssid_s = wpa_config_get_network(wpa_s->conf, id_s);
  2292. if (ssid_s == NULL) {
  2293. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find "
  2294. "network id=%d", id_s);
  2295. return -1;
  2296. }
  2297. ssid_d = wpa_config_get_network(wpa_s->conf, id_d);
  2298. if (ssid_d == NULL) {
  2299. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find "
  2300. "network id=%d", id_s);
  2301. return -1;
  2302. }
  2303. value = wpa_config_get(ssid_s, name);
  2304. if (value == NULL) {
  2305. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Failed to get network "
  2306. "variable '%s'", name);
  2307. return -1;
  2308. }
  2309. ret = wpa_supplicant_ctrl_iface_update_network(wpa_s, ssid_d, name,
  2310. value);
  2311. os_free(value);
  2312. return ret;
  2313. }
  2314. static int wpa_supplicant_ctrl_iface_list_creds(struct wpa_supplicant *wpa_s,
  2315. char *buf, size_t buflen)
  2316. {
  2317. char *pos, *end;
  2318. struct wpa_cred *cred;
  2319. int ret;
  2320. pos = buf;
  2321. end = buf + buflen;
  2322. ret = os_snprintf(pos, end - pos,
  2323. "cred id / realm / username / domain / imsi\n");
  2324. if (ret < 0 || ret >= end - pos)
  2325. return pos - buf;
  2326. pos += ret;
  2327. cred = wpa_s->conf->cred;
  2328. while (cred) {
  2329. ret = os_snprintf(pos, end - pos, "%d\t%s\t%s\t%s\t%s\n",
  2330. cred->id, cred->realm ? cred->realm : "",
  2331. cred->username ? cred->username : "",
  2332. cred->domain ? cred->domain[0] : "",
  2333. cred->imsi ? cred->imsi : "");
  2334. if (ret < 0 || ret >= end - pos)
  2335. return pos - buf;
  2336. pos += ret;
  2337. cred = cred->next;
  2338. }
  2339. return pos - buf;
  2340. }
  2341. static int wpa_supplicant_ctrl_iface_add_cred(struct wpa_supplicant *wpa_s,
  2342. char *buf, size_t buflen)
  2343. {
  2344. struct wpa_cred *cred;
  2345. int ret;
  2346. wpa_printf(MSG_DEBUG, "CTRL_IFACE: ADD_CRED");
  2347. cred = wpa_config_add_cred(wpa_s->conf);
  2348. if (cred == NULL)
  2349. return -1;
  2350. wpa_msg(wpa_s, MSG_INFO, CRED_ADDED "%d", cred->id);
  2351. ret = os_snprintf(buf, buflen, "%d\n", cred->id);
  2352. if (ret < 0 || (size_t) ret >= buflen)
  2353. return -1;
  2354. return ret;
  2355. }
  2356. static int wpas_ctrl_remove_cred(struct wpa_supplicant *wpa_s,
  2357. struct wpa_cred *cred)
  2358. {
  2359. struct wpa_ssid *ssid;
  2360. char str[20];
  2361. int id;
  2362. if (cred == NULL) {
  2363. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find cred");
  2364. return -1;
  2365. }
  2366. id = cred->id;
  2367. if (wpa_config_remove_cred(wpa_s->conf, id) < 0) {
  2368. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find cred");
  2369. return -1;
  2370. }
  2371. wpa_msg(wpa_s, MSG_INFO, CRED_REMOVED "%d", id);
  2372. /* Remove any network entry created based on the removed credential */
  2373. ssid = wpa_s->conf->ssid;
  2374. while (ssid) {
  2375. if (ssid->parent_cred == cred) {
  2376. wpa_printf(MSG_DEBUG, "Remove network id %d since it "
  2377. "used the removed credential", ssid->id);
  2378. os_snprintf(str, sizeof(str), "%d", ssid->id);
  2379. ssid = ssid->next;
  2380. wpa_supplicant_ctrl_iface_remove_network(wpa_s, str);
  2381. } else
  2382. ssid = ssid->next;
  2383. }
  2384. return 0;
  2385. }
  2386. static int wpa_supplicant_ctrl_iface_remove_cred(struct wpa_supplicant *wpa_s,
  2387. char *cmd)
  2388. {
  2389. int id;
  2390. struct wpa_cred *cred, *prev;
  2391. /* cmd: "<cred id>", "all", "sp_fqdn=<FQDN>", or
  2392. * "provisioning_sp=<FQDN> */
  2393. if (os_strcmp(cmd, "all") == 0) {
  2394. wpa_printf(MSG_DEBUG, "CTRL_IFACE: REMOVE_CRED all");
  2395. cred = wpa_s->conf->cred;
  2396. while (cred) {
  2397. prev = cred;
  2398. cred = cred->next;
  2399. wpas_ctrl_remove_cred(wpa_s, prev);
  2400. }
  2401. return 0;
  2402. }
  2403. if (os_strncmp(cmd, "sp_fqdn=", 8) == 0) {
  2404. wpa_printf(MSG_DEBUG, "CTRL_IFACE: REMOVE_CRED SP FQDN '%s'",
  2405. cmd + 8);
  2406. cred = wpa_s->conf->cred;
  2407. while (cred) {
  2408. prev = cred;
  2409. cred = cred->next;
  2410. if (prev->domain) {
  2411. size_t i;
  2412. for (i = 0; i < prev->num_domain; i++) {
  2413. if (os_strcmp(prev->domain[i], cmd + 8)
  2414. != 0)
  2415. continue;
  2416. wpas_ctrl_remove_cred(wpa_s, prev);
  2417. break;
  2418. }
  2419. }
  2420. }
  2421. return 0;
  2422. }
  2423. if (os_strncmp(cmd, "provisioning_sp=", 16) == 0) {
  2424. wpa_printf(MSG_DEBUG, "CTRL_IFACE: REMOVE_CRED provisioning SP FQDN '%s'",
  2425. cmd + 16);
  2426. cred = wpa_s->conf->cred;
  2427. while (cred) {
  2428. prev = cred;
  2429. cred = cred->next;
  2430. if (prev->provisioning_sp &&
  2431. os_strcmp(prev->provisioning_sp, cmd + 16) == 0)
  2432. wpas_ctrl_remove_cred(wpa_s, prev);
  2433. }
  2434. return 0;
  2435. }
  2436. id = atoi(cmd);
  2437. wpa_printf(MSG_DEBUG, "CTRL_IFACE: REMOVE_CRED id=%d", id);
  2438. cred = wpa_config_get_cred(wpa_s->conf, id);
  2439. return wpas_ctrl_remove_cred(wpa_s, cred);
  2440. }
  2441. static int wpa_supplicant_ctrl_iface_set_cred(struct wpa_supplicant *wpa_s,
  2442. char *cmd)
  2443. {
  2444. int id;
  2445. struct wpa_cred *cred;
  2446. char *name, *value;
  2447. /* cmd: "<cred id> <variable name> <value>" */
  2448. name = os_strchr(cmd, ' ');
  2449. if (name == NULL)
  2450. return -1;
  2451. *name++ = '\0';
  2452. value = os_strchr(name, ' ');
  2453. if (value == NULL)
  2454. return -1;
  2455. *value++ = '\0';
  2456. id = atoi(cmd);
  2457. wpa_printf(MSG_DEBUG, "CTRL_IFACE: SET_CRED id=%d name='%s'",
  2458. id, name);
  2459. wpa_hexdump_ascii_key(MSG_DEBUG, "CTRL_IFACE: value",
  2460. (u8 *) value, os_strlen(value));
  2461. cred = wpa_config_get_cred(wpa_s->conf, id);
  2462. if (cred == NULL) {
  2463. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find cred id=%d",
  2464. id);
  2465. return -1;
  2466. }
  2467. if (wpa_config_set_cred(cred, name, value, 0) < 0) {
  2468. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Failed to set cred "
  2469. "variable '%s'", name);
  2470. return -1;
  2471. }
  2472. wpa_msg(wpa_s, MSG_INFO, CRED_MODIFIED "%d %s", cred->id, name);
  2473. return 0;
  2474. }
  2475. static int wpa_supplicant_ctrl_iface_get_cred(struct wpa_supplicant *wpa_s,
  2476. char *cmd, char *buf,
  2477. size_t buflen)
  2478. {
  2479. int id;
  2480. size_t res;
  2481. struct wpa_cred *cred;
  2482. char *name, *value;
  2483. /* cmd: "<cred id> <variable name>" */
  2484. name = os_strchr(cmd, ' ');
  2485. if (name == NULL)
  2486. return -1;
  2487. *name++ = '\0';
  2488. id = atoi(cmd);
  2489. wpa_printf(MSG_DEBUG, "CTRL_IFACE: GET_CRED id=%d name='%s'",
  2490. id, name);
  2491. cred = wpa_config_get_cred(wpa_s->conf, id);
  2492. if (cred == NULL) {
  2493. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find cred id=%d",
  2494. id);
  2495. return -1;
  2496. }
  2497. value = wpa_config_get_cred_no_key(cred, name);
  2498. if (value == NULL) {
  2499. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Failed to get cred variable '%s'",
  2500. name);
  2501. return -1;
  2502. }
  2503. res = os_strlcpy(buf, value, buflen);
  2504. if (res >= buflen) {
  2505. os_free(value);
  2506. return -1;
  2507. }
  2508. os_free(value);
  2509. return res;
  2510. }
  2511. #ifndef CONFIG_NO_CONFIG_WRITE
  2512. static int wpa_supplicant_ctrl_iface_save_config(struct wpa_supplicant *wpa_s)
  2513. {
  2514. int ret;
  2515. if (!wpa_s->conf->update_config) {
  2516. wpa_printf(MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Not allowed "
  2517. "to update configuration (update_config=0)");
  2518. return -1;
  2519. }
  2520. ret = wpa_config_write(wpa_s->confname, wpa_s->conf);
  2521. if (ret) {
  2522. wpa_printf(MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Failed to "
  2523. "update configuration");
  2524. } else {
  2525. wpa_printf(MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Configuration"
  2526. " updated");
  2527. }
  2528. return ret;
  2529. }
  2530. #endif /* CONFIG_NO_CONFIG_WRITE */
  2531. struct cipher_info {
  2532. unsigned int capa;
  2533. const char *name;
  2534. int group_only;
  2535. };
  2536. static const struct cipher_info ciphers[] = {
  2537. { WPA_DRIVER_CAPA_ENC_CCMP_256, "CCMP-256", 0 },
  2538. { WPA_DRIVER_CAPA_ENC_GCMP_256, "GCMP-256", 0 },
  2539. { WPA_DRIVER_CAPA_ENC_CCMP, "CCMP", 0 },
  2540. { WPA_DRIVER_CAPA_ENC_GCMP, "GCMP", 0 },
  2541. { WPA_DRIVER_CAPA_ENC_TKIP, "TKIP", 0 },
  2542. { WPA_DRIVER_CAPA_KEY_MGMT_WPA_NONE, "NONE", 0 },
  2543. { WPA_DRIVER_CAPA_ENC_WEP104, "WEP104", 1 },
  2544. { WPA_DRIVER_CAPA_ENC_WEP40, "WEP40", 1 }
  2545. };
  2546. static int ctrl_iface_get_capability_pairwise(int res, char *strict,
  2547. struct wpa_driver_capa *capa,
  2548. char *buf, size_t buflen)
  2549. {
  2550. int ret;
  2551. char *pos, *end;
  2552. size_t len;
  2553. unsigned int i;
  2554. pos = buf;
  2555. end = pos + buflen;
  2556. if (res < 0) {
  2557. if (strict)
  2558. return 0;
  2559. len = os_strlcpy(buf, "CCMP TKIP NONE", buflen);
  2560. if (len >= buflen)
  2561. return -1;
  2562. return len;
  2563. }
  2564. for (i = 0; i < ARRAY_SIZE(ciphers); i++) {
  2565. if (!ciphers[i].group_only && capa->enc & ciphers[i].capa) {
  2566. ret = os_snprintf(pos, end - pos, "%s%s",
  2567. pos == buf ? "" : " ",
  2568. ciphers[i].name);
  2569. if (ret < 0 || ret >= end - pos)
  2570. return pos - buf;
  2571. pos += ret;
  2572. }
  2573. }
  2574. return pos - buf;
  2575. }
  2576. static int ctrl_iface_get_capability_group(int res, char *strict,
  2577. struct wpa_driver_capa *capa,
  2578. char *buf, size_t buflen)
  2579. {
  2580. int ret;
  2581. char *pos, *end;
  2582. size_t len;
  2583. unsigned int i;
  2584. pos = buf;
  2585. end = pos + buflen;
  2586. if (res < 0) {
  2587. if (strict)
  2588. return 0;
  2589. len = os_strlcpy(buf, "CCMP TKIP WEP104 WEP40", buflen);
  2590. if (len >= buflen)
  2591. return -1;
  2592. return len;
  2593. }
  2594. for (i = 0; i < ARRAY_SIZE(ciphers); i++) {
  2595. if (capa->enc & ciphers[i].capa) {
  2596. ret = os_snprintf(pos, end - pos, "%s%s",
  2597. pos == buf ? "" : " ",
  2598. ciphers[i].name);
  2599. if (ret < 0 || ret >= end - pos)
  2600. return pos - buf;
  2601. pos += ret;
  2602. }
  2603. }
  2604. return pos - buf;
  2605. }
  2606. static int ctrl_iface_get_capability_key_mgmt(int res, char *strict,
  2607. struct wpa_driver_capa *capa,
  2608. char *buf, size_t buflen)
  2609. {
  2610. int ret;
  2611. char *pos, *end;
  2612. size_t len;
  2613. pos = buf;
  2614. end = pos + buflen;
  2615. if (res < 0) {
  2616. if (strict)
  2617. return 0;
  2618. len = os_strlcpy(buf, "WPA-PSK WPA-EAP IEEE8021X WPA-NONE "
  2619. "NONE", buflen);
  2620. if (len >= buflen)
  2621. return -1;
  2622. return len;
  2623. }
  2624. ret = os_snprintf(pos, end - pos, "NONE IEEE8021X");
  2625. if (ret < 0 || ret >= end - pos)
  2626. return pos - buf;
  2627. pos += ret;
  2628. if (capa->key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA |
  2629. WPA_DRIVER_CAPA_KEY_MGMT_WPA2)) {
  2630. ret = os_snprintf(pos, end - pos, " WPA-EAP");
  2631. if (ret < 0 || ret >= end - pos)
  2632. return pos - buf;
  2633. pos += ret;
  2634. }
  2635. if (capa->key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA_PSK |
  2636. WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK)) {
  2637. ret = os_snprintf(pos, end - pos, " WPA-PSK");
  2638. if (ret < 0 || ret >= end - pos)
  2639. return pos - buf;
  2640. pos += ret;
  2641. }
  2642. if (capa->key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_WPA_NONE) {
  2643. ret = os_snprintf(pos, end - pos, " WPA-NONE");
  2644. if (ret < 0 || ret >= end - pos)
  2645. return pos - buf;
  2646. pos += ret;
  2647. }
  2648. return pos - buf;
  2649. }
  2650. static int ctrl_iface_get_capability_proto(int res, char *strict,
  2651. struct wpa_driver_capa *capa,
  2652. char *buf, size_t buflen)
  2653. {
  2654. int ret;
  2655. char *pos, *end;
  2656. size_t len;
  2657. pos = buf;
  2658. end = pos + buflen;
  2659. if (res < 0) {
  2660. if (strict)
  2661. return 0;
  2662. len = os_strlcpy(buf, "RSN WPA", buflen);
  2663. if (len >= buflen)
  2664. return -1;
  2665. return len;
  2666. }
  2667. if (capa->key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA2 |
  2668. WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK)) {
  2669. ret = os_snprintf(pos, end - pos, "%sRSN",
  2670. pos == buf ? "" : " ");
  2671. if (ret < 0 || ret >= end - pos)
  2672. return pos - buf;
  2673. pos += ret;
  2674. }
  2675. if (capa->key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA |
  2676. WPA_DRIVER_CAPA_KEY_MGMT_WPA_PSK)) {
  2677. ret = os_snprintf(pos, end - pos, "%sWPA",
  2678. pos == buf ? "" : " ");
  2679. if (ret < 0 || ret >= end - pos)
  2680. return pos - buf;
  2681. pos += ret;
  2682. }
  2683. return pos - buf;
  2684. }
  2685. static int ctrl_iface_get_capability_auth_alg(int res, char *strict,
  2686. struct wpa_driver_capa *capa,
  2687. char *buf, size_t buflen)
  2688. {
  2689. int ret;
  2690. char *pos, *end;
  2691. size_t len;
  2692. pos = buf;
  2693. end = pos + buflen;
  2694. if (res < 0) {
  2695. if (strict)
  2696. return 0;
  2697. len = os_strlcpy(buf, "OPEN SHARED LEAP", buflen);
  2698. if (len >= buflen)
  2699. return -1;
  2700. return len;
  2701. }
  2702. if (capa->auth & (WPA_DRIVER_AUTH_OPEN)) {
  2703. ret = os_snprintf(pos, end - pos, "%sOPEN",
  2704. pos == buf ? "" : " ");
  2705. if (ret < 0 || ret >= end - pos)
  2706. return pos - buf;
  2707. pos += ret;
  2708. }
  2709. if (capa->auth & (WPA_DRIVER_AUTH_SHARED)) {
  2710. ret = os_snprintf(pos, end - pos, "%sSHARED",
  2711. pos == buf ? "" : " ");
  2712. if (ret < 0 || ret >= end - pos)
  2713. return pos - buf;
  2714. pos += ret;
  2715. }
  2716. if (capa->auth & (WPA_DRIVER_AUTH_LEAP)) {
  2717. ret = os_snprintf(pos, end - pos, "%sLEAP",
  2718. pos == buf ? "" : " ");
  2719. if (ret < 0 || ret >= end - pos)
  2720. return pos - buf;
  2721. pos += ret;
  2722. }
  2723. return pos - buf;
  2724. }
  2725. static int ctrl_iface_get_capability_modes(int res, char *strict,
  2726. struct wpa_driver_capa *capa,
  2727. char *buf, size_t buflen)
  2728. {
  2729. int ret;
  2730. char *pos, *end;
  2731. size_t len;
  2732. pos = buf;
  2733. end = pos + buflen;
  2734. if (res < 0) {
  2735. if (strict)
  2736. return 0;
  2737. len = os_strlcpy(buf, "IBSS AP", buflen);
  2738. if (len >= buflen)
  2739. return -1;
  2740. return len;
  2741. }
  2742. if (capa->flags & WPA_DRIVER_FLAGS_IBSS) {
  2743. ret = os_snprintf(pos, end - pos, "%sIBSS",
  2744. pos == buf ? "" : " ");
  2745. if (ret < 0 || ret >= end - pos)
  2746. return pos - buf;
  2747. pos += ret;
  2748. }
  2749. if (capa->flags & WPA_DRIVER_FLAGS_AP) {
  2750. ret = os_snprintf(pos, end - pos, "%sAP",
  2751. pos == buf ? "" : " ");
  2752. if (ret < 0 || ret >= end - pos)
  2753. return pos - buf;
  2754. pos += ret;
  2755. }
  2756. return pos - buf;
  2757. }
  2758. static int ctrl_iface_get_capability_channels(struct wpa_supplicant *wpa_s,
  2759. char *buf, size_t buflen)
  2760. {
  2761. struct hostapd_channel_data *chnl;
  2762. int ret, i, j;
  2763. char *pos, *end, *hmode;
  2764. pos = buf;
  2765. end = pos + buflen;
  2766. for (j = 0; j < wpa_s->hw.num_modes; j++) {
  2767. switch (wpa_s->hw.modes[j].mode) {
  2768. case HOSTAPD_MODE_IEEE80211B:
  2769. hmode = "B";
  2770. break;
  2771. case HOSTAPD_MODE_IEEE80211G:
  2772. hmode = "G";
  2773. break;
  2774. case HOSTAPD_MODE_IEEE80211A:
  2775. hmode = "A";
  2776. break;
  2777. case HOSTAPD_MODE_IEEE80211AD:
  2778. hmode = "AD";
  2779. break;
  2780. default:
  2781. continue;
  2782. }
  2783. ret = os_snprintf(pos, end - pos, "Mode[%s] Channels:", hmode);
  2784. if (ret < 0 || ret >= end - pos)
  2785. return pos - buf;
  2786. pos += ret;
  2787. chnl = wpa_s->hw.modes[j].channels;
  2788. for (i = 0; i < wpa_s->hw.modes[j].num_channels; i++) {
  2789. if (chnl[i].flag & HOSTAPD_CHAN_DISABLED)
  2790. continue;
  2791. ret = os_snprintf(pos, end - pos, " %d", chnl[i].chan);
  2792. if (ret < 0 || ret >= end - pos)
  2793. return pos - buf;
  2794. pos += ret;
  2795. }
  2796. ret = os_snprintf(pos, end - pos, "\n");
  2797. if (ret < 0 || ret >= end - pos)
  2798. return pos - buf;
  2799. pos += ret;
  2800. }
  2801. return pos - buf;
  2802. }
  2803. static int ctrl_iface_get_capability_freq(struct wpa_supplicant *wpa_s,
  2804. char *buf, size_t buflen)
  2805. {
  2806. struct hostapd_channel_data *chnl;
  2807. int ret, i, j;
  2808. char *pos, *end, *hmode;
  2809. pos = buf;
  2810. end = pos + buflen;
  2811. for (j = 0; j < wpa_s->hw.num_modes; j++) {
  2812. switch (wpa_s->hw.modes[j].mode) {
  2813. case HOSTAPD_MODE_IEEE80211B:
  2814. hmode = "B";
  2815. break;
  2816. case HOSTAPD_MODE_IEEE80211G:
  2817. hmode = "G";
  2818. break;
  2819. case HOSTAPD_MODE_IEEE80211A:
  2820. hmode = "A";
  2821. break;
  2822. case HOSTAPD_MODE_IEEE80211AD:
  2823. hmode = "AD";
  2824. break;
  2825. default:
  2826. continue;
  2827. }
  2828. ret = os_snprintf(pos, end - pos, "Mode[%s] Channels:\n",
  2829. hmode);
  2830. if (ret < 0 || ret >= end - pos)
  2831. return pos - buf;
  2832. pos += ret;
  2833. chnl = wpa_s->hw.modes[j].channels;
  2834. for (i = 0; i < wpa_s->hw.modes[j].num_channels; i++) {
  2835. if (chnl[i].flag & HOSTAPD_CHAN_DISABLED)
  2836. continue;
  2837. ret = os_snprintf(pos, end - pos, " %d = %d MHz%s%s\n",
  2838. chnl[i].chan, chnl[i].freq,
  2839. chnl[i].flag & HOSTAPD_CHAN_NO_IBSS ?
  2840. " (NO_IBSS)" : "",
  2841. chnl[i].flag & HOSTAPD_CHAN_RADAR ?
  2842. " (DFS)" : "");
  2843. if (ret < 0 || ret >= end - pos)
  2844. return pos - buf;
  2845. pos += ret;
  2846. }
  2847. ret = os_snprintf(pos, end - pos, "\n");
  2848. if (ret < 0 || ret >= end - pos)
  2849. return pos - buf;
  2850. pos += ret;
  2851. }
  2852. return pos - buf;
  2853. }
  2854. static int wpa_supplicant_ctrl_iface_get_capability(
  2855. struct wpa_supplicant *wpa_s, const char *_field, char *buf,
  2856. size_t buflen)
  2857. {
  2858. struct wpa_driver_capa capa;
  2859. int res;
  2860. char *strict;
  2861. char field[30];
  2862. size_t len;
  2863. /* Determine whether or not strict checking was requested */
  2864. len = os_strlcpy(field, _field, sizeof(field));
  2865. if (len >= sizeof(field))
  2866. return -1;
  2867. strict = os_strchr(field, ' ');
  2868. if (strict != NULL) {
  2869. *strict++ = '\0';
  2870. if (os_strcmp(strict, "strict") != 0)
  2871. return -1;
  2872. }
  2873. wpa_printf(MSG_DEBUG, "CTRL_IFACE: GET_CAPABILITY '%s' %s",
  2874. field, strict ? strict : "");
  2875. if (os_strcmp(field, "eap") == 0) {
  2876. return eap_get_names(buf, buflen);
  2877. }
  2878. res = wpa_drv_get_capa(wpa_s, &capa);
  2879. if (os_strcmp(field, "pairwise") == 0)
  2880. return ctrl_iface_get_capability_pairwise(res, strict, &capa,
  2881. buf, buflen);
  2882. if (os_strcmp(field, "group") == 0)
  2883. return ctrl_iface_get_capability_group(res, strict, &capa,
  2884. buf, buflen);
  2885. if (os_strcmp(field, "key_mgmt") == 0)
  2886. return ctrl_iface_get_capability_key_mgmt(res, strict, &capa,
  2887. buf, buflen);
  2888. if (os_strcmp(field, "proto") == 0)
  2889. return ctrl_iface_get_capability_proto(res, strict, &capa,
  2890. buf, buflen);
  2891. if (os_strcmp(field, "auth_alg") == 0)
  2892. return ctrl_iface_get_capability_auth_alg(res, strict, &capa,
  2893. buf, buflen);
  2894. if (os_strcmp(field, "modes") == 0)
  2895. return ctrl_iface_get_capability_modes(res, strict, &capa,
  2896. buf, buflen);
  2897. if (os_strcmp(field, "channels") == 0)
  2898. return ctrl_iface_get_capability_channels(wpa_s, buf, buflen);
  2899. if (os_strcmp(field, "freq") == 0)
  2900. return ctrl_iface_get_capability_freq(wpa_s, buf, buflen);
  2901. #ifdef CONFIG_TDLS
  2902. if (os_strcmp(field, "tdls") == 0)
  2903. return ctrl_iface_get_capability_tdls(wpa_s, buf, buflen);
  2904. #endif /* CONFIG_TDLS */
  2905. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Unknown GET_CAPABILITY field '%s'",
  2906. field);
  2907. return -1;
  2908. }
  2909. #ifdef CONFIG_INTERWORKING
  2910. static char * anqp_add_hex(char *pos, char *end, const char *title,
  2911. struct wpabuf *data)
  2912. {
  2913. char *start = pos;
  2914. size_t i;
  2915. int ret;
  2916. const u8 *d;
  2917. if (data == NULL)
  2918. return start;
  2919. ret = os_snprintf(pos, end - pos, "%s=", title);
  2920. if (ret < 0 || ret >= end - pos)
  2921. return start;
  2922. pos += ret;
  2923. d = wpabuf_head_u8(data);
  2924. for (i = 0; i < wpabuf_len(data); i++) {
  2925. ret = os_snprintf(pos, end - pos, "%02x", *d++);
  2926. if (ret < 0 || ret >= end - pos)
  2927. return start;
  2928. pos += ret;
  2929. }
  2930. ret = os_snprintf(pos, end - pos, "\n");
  2931. if (ret < 0 || ret >= end - pos)
  2932. return start;
  2933. pos += ret;
  2934. return pos;
  2935. }
  2936. #endif /* CONFIG_INTERWORKING */
  2937. static int print_bss_info(struct wpa_supplicant *wpa_s, struct wpa_bss *bss,
  2938. unsigned long mask, char *buf, size_t buflen)
  2939. {
  2940. size_t i;
  2941. int ret;
  2942. char *pos, *end;
  2943. const u8 *ie, *ie2;
  2944. pos = buf;
  2945. end = buf + buflen;
  2946. if (mask & WPA_BSS_MASK_ID) {
  2947. ret = os_snprintf(pos, end - pos, "id=%u\n", bss->id);
  2948. if (ret < 0 || ret >= end - pos)
  2949. return 0;
  2950. pos += ret;
  2951. }
  2952. if (mask & WPA_BSS_MASK_BSSID) {
  2953. ret = os_snprintf(pos, end - pos, "bssid=" MACSTR "\n",
  2954. MAC2STR(bss->bssid));
  2955. if (ret < 0 || ret >= end - pos)
  2956. return 0;
  2957. pos += ret;
  2958. }
  2959. if (mask & WPA_BSS_MASK_FREQ) {
  2960. ret = os_snprintf(pos, end - pos, "freq=%d\n", bss->freq);
  2961. if (ret < 0 || ret >= end - pos)
  2962. return 0;
  2963. pos += ret;
  2964. }
  2965. if (mask & WPA_BSS_MASK_BEACON_INT) {
  2966. ret = os_snprintf(pos, end - pos, "beacon_int=%d\n",
  2967. bss->beacon_int);
  2968. if (ret < 0 || ret >= end - pos)
  2969. return 0;
  2970. pos += ret;
  2971. }
  2972. if (mask & WPA_BSS_MASK_CAPABILITIES) {
  2973. ret = os_snprintf(pos, end - pos, "capabilities=0x%04x\n",
  2974. bss->caps);
  2975. if (ret < 0 || ret >= end - pos)
  2976. return 0;
  2977. pos += ret;
  2978. }
  2979. if (mask & WPA_BSS_MASK_QUAL) {
  2980. ret = os_snprintf(pos, end - pos, "qual=%d\n", bss->qual);
  2981. if (ret < 0 || ret >= end - pos)
  2982. return 0;
  2983. pos += ret;
  2984. }
  2985. if (mask & WPA_BSS_MASK_NOISE) {
  2986. ret = os_snprintf(pos, end - pos, "noise=%d\n", bss->noise);
  2987. if (ret < 0 || ret >= end - pos)
  2988. return 0;
  2989. pos += ret;
  2990. }
  2991. if (mask & WPA_BSS_MASK_LEVEL) {
  2992. ret = os_snprintf(pos, end - pos, "level=%d\n", bss->level);
  2993. if (ret < 0 || ret >= end - pos)
  2994. return 0;
  2995. pos += ret;
  2996. }
  2997. if (mask & WPA_BSS_MASK_TSF) {
  2998. ret = os_snprintf(pos, end - pos, "tsf=%016llu\n",
  2999. (unsigned long long) bss->tsf);
  3000. if (ret < 0 || ret >= end - pos)
  3001. return 0;
  3002. pos += ret;
  3003. }
  3004. if (mask & WPA_BSS_MASK_AGE) {
  3005. struct os_reltime now;
  3006. os_get_reltime(&now);
  3007. ret = os_snprintf(pos, end - pos, "age=%d\n",
  3008. (int) (now.sec - bss->last_update.sec));
  3009. if (ret < 0 || ret >= end - pos)
  3010. return 0;
  3011. pos += ret;
  3012. }
  3013. if (mask & WPA_BSS_MASK_IE) {
  3014. ret = os_snprintf(pos, end - pos, "ie=");
  3015. if (ret < 0 || ret >= end - pos)
  3016. return 0;
  3017. pos += ret;
  3018. ie = (const u8 *) (bss + 1);
  3019. for (i = 0; i < bss->ie_len; i++) {
  3020. ret = os_snprintf(pos, end - pos, "%02x", *ie++);
  3021. if (ret < 0 || ret >= end - pos)
  3022. return 0;
  3023. pos += ret;
  3024. }
  3025. ret = os_snprintf(pos, end - pos, "\n");
  3026. if (ret < 0 || ret >= end - pos)
  3027. return 0;
  3028. pos += ret;
  3029. }
  3030. if (mask & WPA_BSS_MASK_FLAGS) {
  3031. ret = os_snprintf(pos, end - pos, "flags=");
  3032. if (ret < 0 || ret >= end - pos)
  3033. return 0;
  3034. pos += ret;
  3035. ie = wpa_bss_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
  3036. if (ie)
  3037. pos = wpa_supplicant_ie_txt(pos, end, "WPA", ie,
  3038. 2 + ie[1]);
  3039. ie2 = wpa_bss_get_ie(bss, WLAN_EID_RSN);
  3040. if (ie2)
  3041. pos = wpa_supplicant_ie_txt(pos, end, "WPA2", ie2,
  3042. 2 + ie2[1]);
  3043. pos = wpa_supplicant_wps_ie_txt(wpa_s, pos, end, bss);
  3044. if (!ie && !ie2 && bss->caps & IEEE80211_CAP_PRIVACY) {
  3045. ret = os_snprintf(pos, end - pos, "[WEP]");
  3046. if (ret < 0 || ret >= end - pos)
  3047. return 0;
  3048. pos += ret;
  3049. }
  3050. if (bss_is_dmg(bss)) {
  3051. const char *s;
  3052. ret = os_snprintf(pos, end - pos, "[DMG]");
  3053. if (ret < 0 || ret >= end - pos)
  3054. return 0;
  3055. pos += ret;
  3056. switch (bss->caps & IEEE80211_CAP_DMG_MASK) {
  3057. case IEEE80211_CAP_DMG_IBSS:
  3058. s = "[IBSS]";
  3059. break;
  3060. case IEEE80211_CAP_DMG_AP:
  3061. s = "[ESS]";
  3062. break;
  3063. case IEEE80211_CAP_DMG_PBSS:
  3064. s = "[PBSS]";
  3065. break;
  3066. default:
  3067. s = "";
  3068. break;
  3069. }
  3070. ret = os_snprintf(pos, end - pos, "%s", s);
  3071. if (ret < 0 || ret >= end - pos)
  3072. return 0;
  3073. pos += ret;
  3074. } else {
  3075. if (bss->caps & IEEE80211_CAP_IBSS) {
  3076. ret = os_snprintf(pos, end - pos, "[IBSS]");
  3077. if (ret < 0 || ret >= end - pos)
  3078. return 0;
  3079. pos += ret;
  3080. }
  3081. if (bss->caps & IEEE80211_CAP_ESS) {
  3082. ret = os_snprintf(pos, end - pos, "[ESS]");
  3083. if (ret < 0 || ret >= end - pos)
  3084. return 0;
  3085. pos += ret;
  3086. }
  3087. }
  3088. if (wpa_bss_get_vendor_ie(bss, P2P_IE_VENDOR_TYPE) ||
  3089. wpa_bss_get_vendor_ie_beacon(bss, P2P_IE_VENDOR_TYPE)) {
  3090. ret = os_snprintf(pos, end - pos, "[P2P]");
  3091. if (ret < 0 || ret >= end - pos)
  3092. return 0;
  3093. pos += ret;
  3094. }
  3095. #ifdef CONFIG_HS20
  3096. if (wpa_bss_get_vendor_ie(bss, HS20_IE_VENDOR_TYPE)) {
  3097. ret = os_snprintf(pos, end - pos, "[HS20]");
  3098. if (ret < 0 || ret >= end - pos)
  3099. return 0;
  3100. pos += ret;
  3101. }
  3102. #endif /* CONFIG_HS20 */
  3103. ret = os_snprintf(pos, end - pos, "\n");
  3104. if (ret < 0 || ret >= end - pos)
  3105. return 0;
  3106. pos += ret;
  3107. }
  3108. if (mask & WPA_BSS_MASK_SSID) {
  3109. ret = os_snprintf(pos, end - pos, "ssid=%s\n",
  3110. wpa_ssid_txt(bss->ssid, bss->ssid_len));
  3111. if (ret < 0 || ret >= end - pos)
  3112. return 0;
  3113. pos += ret;
  3114. }
  3115. #ifdef CONFIG_WPS
  3116. if (mask & WPA_BSS_MASK_WPS_SCAN) {
  3117. ie = (const u8 *) (bss + 1);
  3118. ret = wpas_wps_scan_result_text(ie, bss->ie_len, pos, end);
  3119. if (ret < 0 || ret >= end - pos)
  3120. return 0;
  3121. pos += ret;
  3122. }
  3123. #endif /* CONFIG_WPS */
  3124. #ifdef CONFIG_P2P
  3125. if (mask & WPA_BSS_MASK_P2P_SCAN) {
  3126. ie = (const u8 *) (bss + 1);
  3127. ret = wpas_p2p_scan_result_text(ie, bss->ie_len, pos, end);
  3128. if (ret < 0 || ret >= end - pos)
  3129. return 0;
  3130. pos += ret;
  3131. }
  3132. #endif /* CONFIG_P2P */
  3133. #ifdef CONFIG_WIFI_DISPLAY
  3134. if (mask & WPA_BSS_MASK_WIFI_DISPLAY) {
  3135. struct wpabuf *wfd;
  3136. ie = (const u8 *) (bss + 1);
  3137. wfd = ieee802_11_vendor_ie_concat(ie, bss->ie_len,
  3138. WFD_IE_VENDOR_TYPE);
  3139. if (wfd) {
  3140. ret = os_snprintf(pos, end - pos, "wfd_subelems=");
  3141. if (ret < 0 || ret >= end - pos) {
  3142. wpabuf_free(wfd);
  3143. return 0;
  3144. }
  3145. pos += ret;
  3146. pos += wpa_snprintf_hex(pos, end - pos,
  3147. wpabuf_head(wfd),
  3148. wpabuf_len(wfd));
  3149. wpabuf_free(wfd);
  3150. ret = os_snprintf(pos, end - pos, "\n");
  3151. if (ret < 0 || ret >= end - pos)
  3152. return 0;
  3153. pos += ret;
  3154. }
  3155. }
  3156. #endif /* CONFIG_WIFI_DISPLAY */
  3157. #ifdef CONFIG_INTERWORKING
  3158. if ((mask & WPA_BSS_MASK_INTERNETW) && bss->anqp) {
  3159. struct wpa_bss_anqp *anqp = bss->anqp;
  3160. pos = anqp_add_hex(pos, end, "anqp_venue_name",
  3161. anqp->venue_name);
  3162. pos = anqp_add_hex(pos, end, "anqp_network_auth_type",
  3163. anqp->network_auth_type);
  3164. pos = anqp_add_hex(pos, end, "anqp_roaming_consortium",
  3165. anqp->roaming_consortium);
  3166. pos = anqp_add_hex(pos, end, "anqp_ip_addr_type_availability",
  3167. anqp->ip_addr_type_availability);
  3168. pos = anqp_add_hex(pos, end, "anqp_nai_realm",
  3169. anqp->nai_realm);
  3170. pos = anqp_add_hex(pos, end, "anqp_3gpp", anqp->anqp_3gpp);
  3171. pos = anqp_add_hex(pos, end, "anqp_domain_name",
  3172. anqp->domain_name);
  3173. #ifdef CONFIG_HS20
  3174. pos = anqp_add_hex(pos, end, "hs20_operator_friendly_name",
  3175. anqp->hs20_operator_friendly_name);
  3176. pos = anqp_add_hex(pos, end, "hs20_wan_metrics",
  3177. anqp->hs20_wan_metrics);
  3178. pos = anqp_add_hex(pos, end, "hs20_connection_capability",
  3179. anqp->hs20_connection_capability);
  3180. pos = anqp_add_hex(pos, end, "hs20_operating_class",
  3181. anqp->hs20_operating_class);
  3182. pos = anqp_add_hex(pos, end, "hs20_osu_providers_list",
  3183. anqp->hs20_osu_providers_list);
  3184. #endif /* CONFIG_HS20 */
  3185. }
  3186. #endif /* CONFIG_INTERWORKING */
  3187. if (mask & WPA_BSS_MASK_DELIM) {
  3188. ret = os_snprintf(pos, end - pos, "====\n");
  3189. if (ret < 0 || ret >= end - pos)
  3190. return 0;
  3191. pos += ret;
  3192. }
  3193. return pos - buf;
  3194. }
  3195. static int wpa_supplicant_ctrl_iface_bss(struct wpa_supplicant *wpa_s,
  3196. const char *cmd, char *buf,
  3197. size_t buflen)
  3198. {
  3199. u8 bssid[ETH_ALEN];
  3200. size_t i;
  3201. struct wpa_bss *bss;
  3202. struct wpa_bss *bsslast = NULL;
  3203. struct dl_list *next;
  3204. int ret = 0;
  3205. int len;
  3206. char *ctmp;
  3207. unsigned long mask = WPA_BSS_MASK_ALL;
  3208. if (os_strncmp(cmd, "RANGE=", 6) == 0) {
  3209. if (os_strncmp(cmd + 6, "ALL", 3) == 0) {
  3210. bss = dl_list_first(&wpa_s->bss_id, struct wpa_bss,
  3211. list_id);
  3212. bsslast = dl_list_last(&wpa_s->bss_id, struct wpa_bss,
  3213. list_id);
  3214. } else { /* N1-N2 */
  3215. unsigned int id1, id2;
  3216. if ((ctmp = os_strchr(cmd + 6, '-')) == NULL) {
  3217. wpa_printf(MSG_INFO, "Wrong BSS range "
  3218. "format");
  3219. return 0;
  3220. }
  3221. if (*(cmd + 6) == '-')
  3222. id1 = 0;
  3223. else
  3224. id1 = atoi(cmd + 6);
  3225. ctmp++;
  3226. if (*ctmp >= '0' && *ctmp <= '9')
  3227. id2 = atoi(ctmp);
  3228. else
  3229. id2 = (unsigned int) -1;
  3230. bss = wpa_bss_get_id_range(wpa_s, id1, id2);
  3231. if (id2 == (unsigned int) -1)
  3232. bsslast = dl_list_last(&wpa_s->bss_id,
  3233. struct wpa_bss,
  3234. list_id);
  3235. else {
  3236. bsslast = wpa_bss_get_id(wpa_s, id2);
  3237. if (bsslast == NULL && bss && id2 > id1) {
  3238. struct wpa_bss *tmp = bss;
  3239. for (;;) {
  3240. next = tmp->list_id.next;
  3241. if (next == &wpa_s->bss_id)
  3242. break;
  3243. tmp = dl_list_entry(
  3244. next, struct wpa_bss,
  3245. list_id);
  3246. if (tmp->id > id2)
  3247. break;
  3248. bsslast = tmp;
  3249. }
  3250. }
  3251. }
  3252. }
  3253. } else if (os_strncmp(cmd, "FIRST", 5) == 0)
  3254. bss = dl_list_first(&wpa_s->bss_id, struct wpa_bss, list_id);
  3255. else if (os_strncmp(cmd, "LAST", 4) == 0)
  3256. bss = dl_list_last(&wpa_s->bss_id, struct wpa_bss, list_id);
  3257. else if (os_strncmp(cmd, "ID-", 3) == 0) {
  3258. i = atoi(cmd + 3);
  3259. bss = wpa_bss_get_id(wpa_s, i);
  3260. } else if (os_strncmp(cmd, "NEXT-", 5) == 0) {
  3261. i = atoi(cmd + 5);
  3262. bss = wpa_bss_get_id(wpa_s, i);
  3263. if (bss) {
  3264. next = bss->list_id.next;
  3265. if (next == &wpa_s->bss_id)
  3266. bss = NULL;
  3267. else
  3268. bss = dl_list_entry(next, struct wpa_bss,
  3269. list_id);
  3270. }
  3271. #ifdef CONFIG_P2P
  3272. } else if (os_strncmp(cmd, "p2p_dev_addr=", 13) == 0) {
  3273. if (hwaddr_aton(cmd + 13, bssid) == 0)
  3274. bss = wpa_bss_get_p2p_dev_addr(wpa_s, bssid);
  3275. else
  3276. bss = NULL;
  3277. #endif /* CONFIG_P2P */
  3278. } else if (hwaddr_aton(cmd, bssid) == 0)
  3279. bss = wpa_bss_get_bssid(wpa_s, bssid);
  3280. else {
  3281. struct wpa_bss *tmp;
  3282. i = atoi(cmd);
  3283. bss = NULL;
  3284. dl_list_for_each(tmp, &wpa_s->bss_id, struct wpa_bss, list_id)
  3285. {
  3286. if (i-- == 0) {
  3287. bss = tmp;
  3288. break;
  3289. }
  3290. }
  3291. }
  3292. if ((ctmp = os_strstr(cmd, "MASK=")) != NULL) {
  3293. mask = strtoul(ctmp + 5, NULL, 0x10);
  3294. if (mask == 0)
  3295. mask = WPA_BSS_MASK_ALL;
  3296. }
  3297. if (bss == NULL)
  3298. return 0;
  3299. if (bsslast == NULL)
  3300. bsslast = bss;
  3301. do {
  3302. len = print_bss_info(wpa_s, bss, mask, buf, buflen);
  3303. ret += len;
  3304. buf += len;
  3305. buflen -= len;
  3306. if (bss == bsslast) {
  3307. if ((mask & WPA_BSS_MASK_DELIM) && len &&
  3308. (bss == dl_list_last(&wpa_s->bss_id,
  3309. struct wpa_bss, list_id)))
  3310. os_snprintf(buf - 5, 5, "####\n");
  3311. break;
  3312. }
  3313. next = bss->list_id.next;
  3314. if (next == &wpa_s->bss_id)
  3315. break;
  3316. bss = dl_list_entry(next, struct wpa_bss, list_id);
  3317. } while (bss && len);
  3318. return ret;
  3319. }
  3320. static int wpa_supplicant_ctrl_iface_ap_scan(
  3321. struct wpa_supplicant *wpa_s, char *cmd)
  3322. {
  3323. int ap_scan = atoi(cmd);
  3324. return wpa_supplicant_set_ap_scan(wpa_s, ap_scan);
  3325. }
  3326. static int wpa_supplicant_ctrl_iface_scan_interval(
  3327. struct wpa_supplicant *wpa_s, char *cmd)
  3328. {
  3329. int scan_int = atoi(cmd);
  3330. return wpa_supplicant_set_scan_interval(wpa_s, scan_int);
  3331. }
  3332. static int wpa_supplicant_ctrl_iface_bss_expire_age(
  3333. struct wpa_supplicant *wpa_s, char *cmd)
  3334. {
  3335. int expire_age = atoi(cmd);
  3336. return wpa_supplicant_set_bss_expiration_age(wpa_s, expire_age);
  3337. }
  3338. static int wpa_supplicant_ctrl_iface_bss_expire_count(
  3339. struct wpa_supplicant *wpa_s, char *cmd)
  3340. {
  3341. int expire_count = atoi(cmd);
  3342. return wpa_supplicant_set_bss_expiration_count(wpa_s, expire_count);
  3343. }
  3344. static int wpa_supplicant_ctrl_iface_bss_flush(
  3345. struct wpa_supplicant *wpa_s, char *cmd)
  3346. {
  3347. int flush_age = atoi(cmd);
  3348. if (flush_age == 0)
  3349. wpa_bss_flush(wpa_s);
  3350. else
  3351. wpa_bss_flush_by_age(wpa_s, flush_age);
  3352. return 0;
  3353. }
  3354. #ifdef CONFIG_TESTING_OPTIONS
  3355. static void wpa_supplicant_ctrl_iface_drop_sa(struct wpa_supplicant *wpa_s)
  3356. {
  3357. wpa_printf(MSG_DEBUG, "Dropping SA without deauthentication");
  3358. /* MLME-DELETEKEYS.request */
  3359. wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 0, 0, NULL, 0, NULL, 0);
  3360. wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 1, 0, NULL, 0, NULL, 0);
  3361. wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 2, 0, NULL, 0, NULL, 0);
  3362. wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 3, 0, NULL, 0, NULL, 0);
  3363. #ifdef CONFIG_IEEE80211W
  3364. wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 4, 0, NULL, 0, NULL, 0);
  3365. wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 5, 0, NULL, 0, NULL, 0);
  3366. #endif /* CONFIG_IEEE80211W */
  3367. wpa_drv_set_key(wpa_s, WPA_ALG_NONE, wpa_s->bssid, 0, 0, NULL, 0, NULL,
  3368. 0);
  3369. /* MLME-SETPROTECTION.request(None) */
  3370. wpa_drv_mlme_setprotection(wpa_s, wpa_s->bssid,
  3371. MLME_SETPROTECTION_PROTECT_TYPE_NONE,
  3372. MLME_SETPROTECTION_KEY_TYPE_PAIRWISE);
  3373. wpa_sm_drop_sa(wpa_s->wpa);
  3374. }
  3375. #endif /* CONFIG_TESTING_OPTIONS */
  3376. static int wpa_supplicant_ctrl_iface_roam(struct wpa_supplicant *wpa_s,
  3377. char *addr)
  3378. {
  3379. #ifdef CONFIG_NO_SCAN_PROCESSING
  3380. return -1;
  3381. #else /* CONFIG_NO_SCAN_PROCESSING */
  3382. u8 bssid[ETH_ALEN];
  3383. struct wpa_bss *bss;
  3384. struct wpa_ssid *ssid = wpa_s->current_ssid;
  3385. if (hwaddr_aton(addr, bssid)) {
  3386. wpa_printf(MSG_DEBUG, "CTRL_IFACE ROAM: invalid "
  3387. "address '%s'", addr);
  3388. return -1;
  3389. }
  3390. wpa_printf(MSG_DEBUG, "CTRL_IFACE ROAM " MACSTR, MAC2STR(bssid));
  3391. if (!ssid) {
  3392. wpa_printf(MSG_DEBUG, "CTRL_IFACE ROAM: No network "
  3393. "configuration known for the target AP");
  3394. return -1;
  3395. }
  3396. bss = wpa_bss_get(wpa_s, bssid, ssid->ssid, ssid->ssid_len);
  3397. if (!bss) {
  3398. wpa_printf(MSG_DEBUG, "CTRL_IFACE ROAM: Target AP not found "
  3399. "from BSS table");
  3400. return -1;
  3401. }
  3402. /*
  3403. * TODO: Find best network configuration block from configuration to
  3404. * allow roaming to other networks
  3405. */
  3406. wpa_s->reassociate = 1;
  3407. wpa_supplicant_connect(wpa_s, bss, ssid);
  3408. return 0;
  3409. #endif /* CONFIG_NO_SCAN_PROCESSING */
  3410. }
  3411. #ifdef CONFIG_P2P
  3412. static int p2p_ctrl_find(struct wpa_supplicant *wpa_s, char *cmd)
  3413. {
  3414. unsigned int timeout = atoi(cmd);
  3415. enum p2p_discovery_type type = P2P_FIND_START_WITH_FULL;
  3416. u8 dev_id[ETH_ALEN], *_dev_id = NULL;
  3417. u8 dev_type[WPS_DEV_TYPE_LEN], *_dev_type = NULL;
  3418. char *pos;
  3419. unsigned int search_delay;
  3420. if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED) {
  3421. wpa_dbg(wpa_s, MSG_INFO,
  3422. "Reject P2P_FIND since interface is disabled");
  3423. return -1;
  3424. }
  3425. if (os_strstr(cmd, "type=social"))
  3426. type = P2P_FIND_ONLY_SOCIAL;
  3427. else if (os_strstr(cmd, "type=progressive"))
  3428. type = P2P_FIND_PROGRESSIVE;
  3429. pos = os_strstr(cmd, "dev_id=");
  3430. if (pos) {
  3431. pos += 7;
  3432. if (hwaddr_aton(pos, dev_id))
  3433. return -1;
  3434. _dev_id = dev_id;
  3435. }
  3436. pos = os_strstr(cmd, "dev_type=");
  3437. if (pos) {
  3438. pos += 9;
  3439. if (wps_dev_type_str2bin(pos, dev_type) < 0)
  3440. return -1;
  3441. _dev_type = dev_type;
  3442. }
  3443. pos = os_strstr(cmd, "delay=");
  3444. if (pos) {
  3445. pos += 6;
  3446. search_delay = atoi(pos);
  3447. } else
  3448. search_delay = wpas_p2p_search_delay(wpa_s);
  3449. return wpas_p2p_find(wpa_s, timeout, type, _dev_type != NULL, _dev_type,
  3450. _dev_id, search_delay);
  3451. }
  3452. static int p2p_ctrl_connect(struct wpa_supplicant *wpa_s, char *cmd,
  3453. char *buf, size_t buflen)
  3454. {
  3455. u8 addr[ETH_ALEN];
  3456. char *pos, *pos2;
  3457. char *pin = NULL;
  3458. enum p2p_wps_method wps_method;
  3459. int new_pin;
  3460. int ret;
  3461. int persistent_group, persistent_id = -1;
  3462. int join;
  3463. int auth;
  3464. int automatic;
  3465. int go_intent = -1;
  3466. int freq = 0;
  3467. int pd;
  3468. int ht40, vht;
  3469. /* <addr> <"pbc" | "pin" | PIN> [label|display|keypad]
  3470. * [persistent|persistent=<network id>]
  3471. * [join] [auth] [go_intent=<0..15>] [freq=<in MHz>] [provdisc]
  3472. * [ht40] [vht] */
  3473. if (hwaddr_aton(cmd, addr))
  3474. return -1;
  3475. pos = cmd + 17;
  3476. if (*pos != ' ')
  3477. return -1;
  3478. pos++;
  3479. persistent_group = os_strstr(pos, " persistent") != NULL;
  3480. pos2 = os_strstr(pos, " persistent=");
  3481. if (pos2) {
  3482. struct wpa_ssid *ssid;
  3483. persistent_id = atoi(pos2 + 12);
  3484. ssid = wpa_config_get_network(wpa_s->conf, persistent_id);
  3485. if (ssid == NULL || ssid->disabled != 2 ||
  3486. ssid->mode != WPAS_MODE_P2P_GO) {
  3487. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find "
  3488. "SSID id=%d for persistent P2P group (GO)",
  3489. persistent_id);
  3490. return -1;
  3491. }
  3492. }
  3493. join = os_strstr(pos, " join") != NULL;
  3494. auth = os_strstr(pos, " auth") != NULL;
  3495. automatic = os_strstr(pos, " auto") != NULL;
  3496. pd = os_strstr(pos, " provdisc") != NULL;
  3497. vht = (os_strstr(cmd, " vht") != NULL) || wpa_s->conf->p2p_go_vht;
  3498. ht40 = (os_strstr(cmd, " ht40") != NULL) || wpa_s->conf->p2p_go_ht40 ||
  3499. vht;
  3500. pos2 = os_strstr(pos, " go_intent=");
  3501. if (pos2) {
  3502. pos2 += 11;
  3503. go_intent = atoi(pos2);
  3504. if (go_intent < 0 || go_intent > 15)
  3505. return -1;
  3506. }
  3507. pos2 = os_strstr(pos, " freq=");
  3508. if (pos2) {
  3509. pos2 += 6;
  3510. freq = atoi(pos2);
  3511. if (freq <= 0)
  3512. return -1;
  3513. }
  3514. if (os_strncmp(pos, "pin", 3) == 0) {
  3515. /* Request random PIN (to be displayed) and enable the PIN */
  3516. wps_method = WPS_PIN_DISPLAY;
  3517. } else if (os_strncmp(pos, "pbc", 3) == 0) {
  3518. wps_method = WPS_PBC;
  3519. } else {
  3520. pin = pos;
  3521. pos = os_strchr(pin, ' ');
  3522. wps_method = WPS_PIN_KEYPAD;
  3523. if (pos) {
  3524. *pos++ = '\0';
  3525. if (os_strncmp(pos, "display", 7) == 0)
  3526. wps_method = WPS_PIN_DISPLAY;
  3527. }
  3528. if (!wps_pin_str_valid(pin)) {
  3529. os_memcpy(buf, "FAIL-INVALID-PIN\n", 17);
  3530. return 17;
  3531. }
  3532. }
  3533. new_pin = wpas_p2p_connect(wpa_s, addr, pin, wps_method,
  3534. persistent_group, automatic, join,
  3535. auth, go_intent, freq, persistent_id, pd,
  3536. ht40, vht);
  3537. if (new_pin == -2) {
  3538. os_memcpy(buf, "FAIL-CHANNEL-UNAVAILABLE\n", 25);
  3539. return 25;
  3540. }
  3541. if (new_pin == -3) {
  3542. os_memcpy(buf, "FAIL-CHANNEL-UNSUPPORTED\n", 25);
  3543. return 25;
  3544. }
  3545. if (new_pin < 0)
  3546. return -1;
  3547. if (wps_method == WPS_PIN_DISPLAY && pin == NULL) {
  3548. ret = os_snprintf(buf, buflen, "%08d", new_pin);
  3549. if (ret < 0 || (size_t) ret >= buflen)
  3550. return -1;
  3551. return ret;
  3552. }
  3553. os_memcpy(buf, "OK\n", 3);
  3554. return 3;
  3555. }
  3556. static int p2p_ctrl_listen(struct wpa_supplicant *wpa_s, char *cmd)
  3557. {
  3558. unsigned int timeout = atoi(cmd);
  3559. if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED) {
  3560. wpa_dbg(wpa_s, MSG_INFO,
  3561. "Reject P2P_LISTEN since interface is disabled");
  3562. return -1;
  3563. }
  3564. return wpas_p2p_listen(wpa_s, timeout);
  3565. }
  3566. static int p2p_ctrl_prov_disc(struct wpa_supplicant *wpa_s, char *cmd)
  3567. {
  3568. u8 addr[ETH_ALEN];
  3569. char *pos;
  3570. enum wpas_p2p_prov_disc_use use = WPAS_P2P_PD_FOR_GO_NEG;
  3571. /* <addr> <config method> [join|auto] */
  3572. if (hwaddr_aton(cmd, addr))
  3573. return -1;
  3574. pos = cmd + 17;
  3575. if (*pos != ' ')
  3576. return -1;
  3577. pos++;
  3578. if (os_strstr(pos, " join") != NULL)
  3579. use = WPAS_P2P_PD_FOR_JOIN;
  3580. else if (os_strstr(pos, " auto") != NULL)
  3581. use = WPAS_P2P_PD_AUTO;
  3582. return wpas_p2p_prov_disc(wpa_s, addr, pos, use);
  3583. }
  3584. static int p2p_get_passphrase(struct wpa_supplicant *wpa_s, char *buf,
  3585. size_t buflen)
  3586. {
  3587. struct wpa_ssid *ssid = wpa_s->current_ssid;
  3588. if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GO ||
  3589. ssid->passphrase == NULL)
  3590. return -1;
  3591. os_strlcpy(buf, ssid->passphrase, buflen);
  3592. return os_strlen(buf);
  3593. }
  3594. static int p2p_ctrl_serv_disc_req(struct wpa_supplicant *wpa_s, char *cmd,
  3595. char *buf, size_t buflen)
  3596. {
  3597. u64 ref;
  3598. int res;
  3599. u8 dst_buf[ETH_ALEN], *dst;
  3600. struct wpabuf *tlvs;
  3601. char *pos;
  3602. size_t len;
  3603. if (hwaddr_aton(cmd, dst_buf))
  3604. return -1;
  3605. dst = dst_buf;
  3606. if (dst[0] == 0 && dst[1] == 0 && dst[2] == 0 &&
  3607. dst[3] == 0 && dst[4] == 0 && dst[5] == 0)
  3608. dst = NULL;
  3609. pos = cmd + 17;
  3610. if (*pos != ' ')
  3611. return -1;
  3612. pos++;
  3613. if (os_strncmp(pos, "upnp ", 5) == 0) {
  3614. u8 version;
  3615. pos += 5;
  3616. if (hexstr2bin(pos, &version, 1) < 0)
  3617. return -1;
  3618. pos += 2;
  3619. if (*pos != ' ')
  3620. return -1;
  3621. pos++;
  3622. ref = wpas_p2p_sd_request_upnp(wpa_s, dst, version, pos);
  3623. #ifdef CONFIG_WIFI_DISPLAY
  3624. } else if (os_strncmp(pos, "wifi-display ", 13) == 0) {
  3625. ref = wpas_p2p_sd_request_wifi_display(wpa_s, dst, pos + 13);
  3626. #endif /* CONFIG_WIFI_DISPLAY */
  3627. } else {
  3628. len = os_strlen(pos);
  3629. if (len & 1)
  3630. return -1;
  3631. len /= 2;
  3632. tlvs = wpabuf_alloc(len);
  3633. if (tlvs == NULL)
  3634. return -1;
  3635. if (hexstr2bin(pos, wpabuf_put(tlvs, len), len) < 0) {
  3636. wpabuf_free(tlvs);
  3637. return -1;
  3638. }
  3639. ref = wpas_p2p_sd_request(wpa_s, dst, tlvs);
  3640. wpabuf_free(tlvs);
  3641. }
  3642. if (ref == 0)
  3643. return -1;
  3644. res = os_snprintf(buf, buflen, "%llx", (long long unsigned) ref);
  3645. if (res < 0 || (unsigned) res >= buflen)
  3646. return -1;
  3647. return res;
  3648. }
  3649. static int p2p_ctrl_serv_disc_cancel_req(struct wpa_supplicant *wpa_s,
  3650. char *cmd)
  3651. {
  3652. long long unsigned val;
  3653. u64 req;
  3654. if (sscanf(cmd, "%llx", &val) != 1)
  3655. return -1;
  3656. req = val;
  3657. return wpas_p2p_sd_cancel_request(wpa_s, req);
  3658. }
  3659. static int p2p_ctrl_serv_disc_resp(struct wpa_supplicant *wpa_s, char *cmd)
  3660. {
  3661. int freq;
  3662. u8 dst[ETH_ALEN];
  3663. u8 dialog_token;
  3664. struct wpabuf *resp_tlvs;
  3665. char *pos, *pos2;
  3666. size_t len;
  3667. pos = os_strchr(cmd, ' ');
  3668. if (pos == NULL)
  3669. return -1;
  3670. *pos++ = '\0';
  3671. freq = atoi(cmd);
  3672. if (freq == 0)
  3673. return -1;
  3674. if (hwaddr_aton(pos, dst))
  3675. return -1;
  3676. pos += 17;
  3677. if (*pos != ' ')
  3678. return -1;
  3679. pos++;
  3680. pos2 = os_strchr(pos, ' ');
  3681. if (pos2 == NULL)
  3682. return -1;
  3683. *pos2++ = '\0';
  3684. dialog_token = atoi(pos);
  3685. len = os_strlen(pos2);
  3686. if (len & 1)
  3687. return -1;
  3688. len /= 2;
  3689. resp_tlvs = wpabuf_alloc(len);
  3690. if (resp_tlvs == NULL)
  3691. return -1;
  3692. if (hexstr2bin(pos2, wpabuf_put(resp_tlvs, len), len) < 0) {
  3693. wpabuf_free(resp_tlvs);
  3694. return -1;
  3695. }
  3696. wpas_p2p_sd_response(wpa_s, freq, dst, dialog_token, resp_tlvs);
  3697. wpabuf_free(resp_tlvs);
  3698. return 0;
  3699. }
  3700. static int p2p_ctrl_serv_disc_external(struct wpa_supplicant *wpa_s,
  3701. char *cmd)
  3702. {
  3703. if (os_strcmp(cmd, "0") && os_strcmp(cmd, "1"))
  3704. return -1;
  3705. wpa_s->p2p_sd_over_ctrl_iface = atoi(cmd);
  3706. return 0;
  3707. }
  3708. static int p2p_ctrl_service_add_bonjour(struct wpa_supplicant *wpa_s,
  3709. char *cmd)
  3710. {
  3711. char *pos;
  3712. size_t len;
  3713. struct wpabuf *query, *resp;
  3714. pos = os_strchr(cmd, ' ');
  3715. if (pos == NULL)
  3716. return -1;
  3717. *pos++ = '\0';
  3718. len = os_strlen(cmd);
  3719. if (len & 1)
  3720. return -1;
  3721. len /= 2;
  3722. query = wpabuf_alloc(len);
  3723. if (query == NULL)
  3724. return -1;
  3725. if (hexstr2bin(cmd, wpabuf_put(query, len), len) < 0) {
  3726. wpabuf_free(query);
  3727. return -1;
  3728. }
  3729. len = os_strlen(pos);
  3730. if (len & 1) {
  3731. wpabuf_free(query);
  3732. return -1;
  3733. }
  3734. len /= 2;
  3735. resp = wpabuf_alloc(len);
  3736. if (resp == NULL) {
  3737. wpabuf_free(query);
  3738. return -1;
  3739. }
  3740. if (hexstr2bin(pos, wpabuf_put(resp, len), len) < 0) {
  3741. wpabuf_free(query);
  3742. wpabuf_free(resp);
  3743. return -1;
  3744. }
  3745. if (wpas_p2p_service_add_bonjour(wpa_s, query, resp) < 0) {
  3746. wpabuf_free(query);
  3747. wpabuf_free(resp);
  3748. return -1;
  3749. }
  3750. return 0;
  3751. }
  3752. static int p2p_ctrl_service_add_upnp(struct wpa_supplicant *wpa_s, char *cmd)
  3753. {
  3754. char *pos;
  3755. u8 version;
  3756. pos = os_strchr(cmd, ' ');
  3757. if (pos == NULL)
  3758. return -1;
  3759. *pos++ = '\0';
  3760. if (hexstr2bin(cmd, &version, 1) < 0)
  3761. return -1;
  3762. return wpas_p2p_service_add_upnp(wpa_s, version, pos);
  3763. }
  3764. static int p2p_ctrl_service_add(struct wpa_supplicant *wpa_s, char *cmd)
  3765. {
  3766. char *pos;
  3767. pos = os_strchr(cmd, ' ');
  3768. if (pos == NULL)
  3769. return -1;
  3770. *pos++ = '\0';
  3771. if (os_strcmp(cmd, "bonjour") == 0)
  3772. return p2p_ctrl_service_add_bonjour(wpa_s, pos);
  3773. if (os_strcmp(cmd, "upnp") == 0)
  3774. return p2p_ctrl_service_add_upnp(wpa_s, pos);
  3775. wpa_printf(MSG_DEBUG, "Unknown service '%s'", cmd);
  3776. return -1;
  3777. }
  3778. static int p2p_ctrl_service_del_bonjour(struct wpa_supplicant *wpa_s,
  3779. char *cmd)
  3780. {
  3781. size_t len;
  3782. struct wpabuf *query;
  3783. int ret;
  3784. len = os_strlen(cmd);
  3785. if (len & 1)
  3786. return -1;
  3787. len /= 2;
  3788. query = wpabuf_alloc(len);
  3789. if (query == NULL)
  3790. return -1;
  3791. if (hexstr2bin(cmd, wpabuf_put(query, len), len) < 0) {
  3792. wpabuf_free(query);
  3793. return -1;
  3794. }
  3795. ret = wpas_p2p_service_del_bonjour(wpa_s, query);
  3796. wpabuf_free(query);
  3797. return ret;
  3798. }
  3799. static int p2p_ctrl_service_del_upnp(struct wpa_supplicant *wpa_s, char *cmd)
  3800. {
  3801. char *pos;
  3802. u8 version;
  3803. pos = os_strchr(cmd, ' ');
  3804. if (pos == NULL)
  3805. return -1;
  3806. *pos++ = '\0';
  3807. if (hexstr2bin(cmd, &version, 1) < 0)
  3808. return -1;
  3809. return wpas_p2p_service_del_upnp(wpa_s, version, pos);
  3810. }
  3811. static int p2p_ctrl_service_del(struct wpa_supplicant *wpa_s, char *cmd)
  3812. {
  3813. char *pos;
  3814. pos = os_strchr(cmd, ' ');
  3815. if (pos == NULL)
  3816. return -1;
  3817. *pos++ = '\0';
  3818. if (os_strcmp(cmd, "bonjour") == 0)
  3819. return p2p_ctrl_service_del_bonjour(wpa_s, pos);
  3820. if (os_strcmp(cmd, "upnp") == 0)
  3821. return p2p_ctrl_service_del_upnp(wpa_s, pos);
  3822. wpa_printf(MSG_DEBUG, "Unknown service '%s'", cmd);
  3823. return -1;
  3824. }
  3825. static int p2p_ctrl_reject(struct wpa_supplicant *wpa_s, char *cmd)
  3826. {
  3827. u8 addr[ETH_ALEN];
  3828. /* <addr> */
  3829. if (hwaddr_aton(cmd, addr))
  3830. return -1;
  3831. return wpas_p2p_reject(wpa_s, addr);
  3832. }
  3833. static int p2p_ctrl_invite_persistent(struct wpa_supplicant *wpa_s, char *cmd)
  3834. {
  3835. char *pos;
  3836. int id;
  3837. struct wpa_ssid *ssid;
  3838. u8 *_peer = NULL, peer[ETH_ALEN];
  3839. int freq = 0, pref_freq = 0;
  3840. int ht40, vht;
  3841. id = atoi(cmd);
  3842. pos = os_strstr(cmd, " peer=");
  3843. if (pos) {
  3844. pos += 6;
  3845. if (hwaddr_aton(pos, peer))
  3846. return -1;
  3847. _peer = peer;
  3848. }
  3849. ssid = wpa_config_get_network(wpa_s->conf, id);
  3850. if (ssid == NULL || ssid->disabled != 2) {
  3851. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find SSID id=%d "
  3852. "for persistent P2P group",
  3853. id);
  3854. return -1;
  3855. }
  3856. pos = os_strstr(cmd, " freq=");
  3857. if (pos) {
  3858. pos += 6;
  3859. freq = atoi(pos);
  3860. if (freq <= 0)
  3861. return -1;
  3862. }
  3863. pos = os_strstr(cmd, " pref=");
  3864. if (pos) {
  3865. pos += 6;
  3866. pref_freq = atoi(pos);
  3867. if (pref_freq <= 0)
  3868. return -1;
  3869. }
  3870. vht = (os_strstr(cmd, " vht") != NULL) || wpa_s->conf->p2p_go_vht;
  3871. ht40 = (os_strstr(cmd, " ht40") != NULL) || wpa_s->conf->p2p_go_ht40 ||
  3872. vht;
  3873. return wpas_p2p_invite(wpa_s, _peer, ssid, NULL, freq, ht40, vht,
  3874. pref_freq);
  3875. }
  3876. static int p2p_ctrl_invite_group(struct wpa_supplicant *wpa_s, char *cmd)
  3877. {
  3878. char *pos;
  3879. u8 peer[ETH_ALEN], go_dev_addr[ETH_ALEN], *go_dev = NULL;
  3880. pos = os_strstr(cmd, " peer=");
  3881. if (!pos)
  3882. return -1;
  3883. *pos = '\0';
  3884. pos += 6;
  3885. if (hwaddr_aton(pos, peer)) {
  3886. wpa_printf(MSG_DEBUG, "P2P: Invalid MAC address '%s'", pos);
  3887. return -1;
  3888. }
  3889. pos = os_strstr(pos, " go_dev_addr=");
  3890. if (pos) {
  3891. pos += 13;
  3892. if (hwaddr_aton(pos, go_dev_addr)) {
  3893. wpa_printf(MSG_DEBUG, "P2P: Invalid MAC address '%s'",
  3894. pos);
  3895. return -1;
  3896. }
  3897. go_dev = go_dev_addr;
  3898. }
  3899. return wpas_p2p_invite_group(wpa_s, cmd, peer, go_dev);
  3900. }
  3901. static int p2p_ctrl_invite(struct wpa_supplicant *wpa_s, char *cmd)
  3902. {
  3903. if (os_strncmp(cmd, "persistent=", 11) == 0)
  3904. return p2p_ctrl_invite_persistent(wpa_s, cmd + 11);
  3905. if (os_strncmp(cmd, "group=", 6) == 0)
  3906. return p2p_ctrl_invite_group(wpa_s, cmd + 6);
  3907. return -1;
  3908. }
  3909. static int p2p_ctrl_group_add_persistent(struct wpa_supplicant *wpa_s,
  3910. char *cmd, int freq, int ht40,
  3911. int vht)
  3912. {
  3913. int id;
  3914. struct wpa_ssid *ssid;
  3915. id = atoi(cmd);
  3916. ssid = wpa_config_get_network(wpa_s->conf, id);
  3917. if (ssid == NULL || ssid->disabled != 2) {
  3918. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find SSID id=%d "
  3919. "for persistent P2P group",
  3920. id);
  3921. return -1;
  3922. }
  3923. return wpas_p2p_group_add_persistent(wpa_s, ssid, 0, freq, 0, ht40, vht,
  3924. NULL, 0);
  3925. }
  3926. static int p2p_ctrl_group_add(struct wpa_supplicant *wpa_s, char *cmd)
  3927. {
  3928. int freq = 0, ht40, vht;
  3929. char *pos;
  3930. pos = os_strstr(cmd, "freq=");
  3931. if (pos)
  3932. freq = atoi(pos + 5);
  3933. vht = (os_strstr(cmd, "vht") != NULL) || wpa_s->conf->p2p_go_vht;
  3934. ht40 = (os_strstr(cmd, "ht40") != NULL) || wpa_s->conf->p2p_go_ht40 ||
  3935. vht;
  3936. if (os_strncmp(cmd, "persistent=", 11) == 0)
  3937. return p2p_ctrl_group_add_persistent(wpa_s, cmd + 11, freq,
  3938. ht40, vht);
  3939. if (os_strcmp(cmd, "persistent") == 0 ||
  3940. os_strncmp(cmd, "persistent ", 11) == 0)
  3941. return wpas_p2p_group_add(wpa_s, 1, freq, ht40, vht);
  3942. if (os_strncmp(cmd, "freq=", 5) == 0)
  3943. return wpas_p2p_group_add(wpa_s, 0, freq, ht40, vht);
  3944. if (ht40)
  3945. return wpas_p2p_group_add(wpa_s, 0, freq, ht40, vht);
  3946. wpa_printf(MSG_DEBUG, "CTRL: Invalid P2P_GROUP_ADD parameters '%s'",
  3947. cmd);
  3948. return -1;
  3949. }
  3950. static int p2p_ctrl_peer(struct wpa_supplicant *wpa_s, char *cmd,
  3951. char *buf, size_t buflen)
  3952. {
  3953. u8 addr[ETH_ALEN], *addr_ptr;
  3954. int next, res;
  3955. const struct p2p_peer_info *info;
  3956. char *pos, *end;
  3957. char devtype[WPS_DEV_TYPE_BUFSIZE];
  3958. struct wpa_ssid *ssid;
  3959. size_t i;
  3960. if (!wpa_s->global->p2p)
  3961. return -1;
  3962. if (os_strcmp(cmd, "FIRST") == 0) {
  3963. addr_ptr = NULL;
  3964. next = 0;
  3965. } else if (os_strncmp(cmd, "NEXT-", 5) == 0) {
  3966. if (hwaddr_aton(cmd + 5, addr) < 0)
  3967. return -1;
  3968. addr_ptr = addr;
  3969. next = 1;
  3970. } else {
  3971. if (hwaddr_aton(cmd, addr) < 0)
  3972. return -1;
  3973. addr_ptr = addr;
  3974. next = 0;
  3975. }
  3976. info = p2p_get_peer_info(wpa_s->global->p2p, addr_ptr, next);
  3977. if (info == NULL)
  3978. return -1;
  3979. pos = buf;
  3980. end = buf + buflen;
  3981. res = os_snprintf(pos, end - pos, MACSTR "\n"
  3982. "pri_dev_type=%s\n"
  3983. "device_name=%s\n"
  3984. "manufacturer=%s\n"
  3985. "model_name=%s\n"
  3986. "model_number=%s\n"
  3987. "serial_number=%s\n"
  3988. "config_methods=0x%x\n"
  3989. "dev_capab=0x%x\n"
  3990. "group_capab=0x%x\n"
  3991. "level=%d\n",
  3992. MAC2STR(info->p2p_device_addr),
  3993. wps_dev_type_bin2str(info->pri_dev_type,
  3994. devtype, sizeof(devtype)),
  3995. info->device_name,
  3996. info->manufacturer,
  3997. info->model_name,
  3998. info->model_number,
  3999. info->serial_number,
  4000. info->config_methods,
  4001. info->dev_capab,
  4002. info->group_capab,
  4003. info->level);
  4004. if (res < 0 || res >= end - pos)
  4005. return pos - buf;
  4006. pos += res;
  4007. for (i = 0; i < info->wps_sec_dev_type_list_len / WPS_DEV_TYPE_LEN; i++)
  4008. {
  4009. const u8 *t;
  4010. t = &info->wps_sec_dev_type_list[i * WPS_DEV_TYPE_LEN];
  4011. res = os_snprintf(pos, end - pos, "sec_dev_type=%s\n",
  4012. wps_dev_type_bin2str(t, devtype,
  4013. sizeof(devtype)));
  4014. if (res < 0 || res >= end - pos)
  4015. return pos - buf;
  4016. pos += res;
  4017. }
  4018. ssid = wpas_p2p_get_persistent(wpa_s, info->p2p_device_addr, NULL, 0);
  4019. if (ssid) {
  4020. res = os_snprintf(pos, end - pos, "persistent=%d\n", ssid->id);
  4021. if (res < 0 || res >= end - pos)
  4022. return pos - buf;
  4023. pos += res;
  4024. }
  4025. res = p2p_get_peer_info_txt(info, pos, end - pos);
  4026. if (res < 0)
  4027. return pos - buf;
  4028. pos += res;
  4029. if (info->vendor_elems) {
  4030. res = os_snprintf(pos, end - pos, "vendor_elems=");
  4031. if (res < 0 || res >= end - pos)
  4032. return pos - buf;
  4033. pos += res;
  4034. pos += wpa_snprintf_hex(pos, end - pos,
  4035. wpabuf_head(info->vendor_elems),
  4036. wpabuf_len(info->vendor_elems));
  4037. res = os_snprintf(pos, end - pos, "\n");
  4038. if (res < 0 || res >= end - pos)
  4039. return pos - buf;
  4040. pos += res;
  4041. }
  4042. return pos - buf;
  4043. }
  4044. static int p2p_ctrl_disallow_freq(struct wpa_supplicant *wpa_s,
  4045. const char *param)
  4046. {
  4047. unsigned int i;
  4048. if (wpa_s->global->p2p == NULL)
  4049. return -1;
  4050. if (freq_range_list_parse(&wpa_s->global->p2p_disallow_freq, param) < 0)
  4051. return -1;
  4052. for (i = 0; i < wpa_s->global->p2p_disallow_freq.num; i++) {
  4053. struct wpa_freq_range *freq;
  4054. freq = &wpa_s->global->p2p_disallow_freq.range[i];
  4055. wpa_printf(MSG_DEBUG, "P2P: Disallowed frequency range %u-%u",
  4056. freq->min, freq->max);
  4057. }
  4058. wpas_p2p_update_channel_list(wpa_s);
  4059. return 0;
  4060. }
  4061. static int p2p_ctrl_set(struct wpa_supplicant *wpa_s, char *cmd)
  4062. {
  4063. char *param;
  4064. if (wpa_s->global->p2p == NULL)
  4065. return -1;
  4066. param = os_strchr(cmd, ' ');
  4067. if (param == NULL)
  4068. return -1;
  4069. *param++ = '\0';
  4070. if (os_strcmp(cmd, "discoverability") == 0) {
  4071. p2p_set_client_discoverability(wpa_s->global->p2p,
  4072. atoi(param));
  4073. return 0;
  4074. }
  4075. if (os_strcmp(cmd, "managed") == 0) {
  4076. p2p_set_managed_oper(wpa_s->global->p2p, atoi(param));
  4077. return 0;
  4078. }
  4079. if (os_strcmp(cmd, "listen_channel") == 0) {
  4080. return p2p_set_listen_channel(wpa_s->global->p2p, 81,
  4081. atoi(param), 1);
  4082. }
  4083. if (os_strcmp(cmd, "ssid_postfix") == 0) {
  4084. return p2p_set_ssid_postfix(wpa_s->global->p2p, (u8 *) param,
  4085. os_strlen(param));
  4086. }
  4087. if (os_strcmp(cmd, "noa") == 0) {
  4088. char *pos;
  4089. int count, start, duration;
  4090. /* GO NoA parameters: count,start_offset(ms),duration(ms) */
  4091. count = atoi(param);
  4092. pos = os_strchr(param, ',');
  4093. if (pos == NULL)
  4094. return -1;
  4095. pos++;
  4096. start = atoi(pos);
  4097. pos = os_strchr(pos, ',');
  4098. if (pos == NULL)
  4099. return -1;
  4100. pos++;
  4101. duration = atoi(pos);
  4102. if (count < 0 || count > 255 || start < 0 || duration < 0)
  4103. return -1;
  4104. if (count == 0 && duration > 0)
  4105. return -1;
  4106. wpa_printf(MSG_DEBUG, "CTRL_IFACE: P2P_SET GO NoA: count=%d "
  4107. "start=%d duration=%d", count, start, duration);
  4108. return wpas_p2p_set_noa(wpa_s, count, start, duration);
  4109. }
  4110. if (os_strcmp(cmd, "ps") == 0)
  4111. return wpa_drv_set_p2p_powersave(wpa_s, atoi(param), -1, -1);
  4112. if (os_strcmp(cmd, "oppps") == 0)
  4113. return wpa_drv_set_p2p_powersave(wpa_s, -1, atoi(param), -1);
  4114. if (os_strcmp(cmd, "ctwindow") == 0)
  4115. return wpa_drv_set_p2p_powersave(wpa_s, -1, -1, atoi(param));
  4116. if (os_strcmp(cmd, "disabled") == 0) {
  4117. wpa_s->global->p2p_disabled = atoi(param);
  4118. wpa_printf(MSG_DEBUG, "P2P functionality %s",
  4119. wpa_s->global->p2p_disabled ?
  4120. "disabled" : "enabled");
  4121. if (wpa_s->global->p2p_disabled) {
  4122. wpas_p2p_stop_find(wpa_s);
  4123. os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN);
  4124. p2p_flush(wpa_s->global->p2p);
  4125. }
  4126. return 0;
  4127. }
  4128. if (os_strcmp(cmd, "conc_pref") == 0) {
  4129. if (os_strcmp(param, "sta") == 0)
  4130. wpa_s->global->conc_pref = WPA_CONC_PREF_STA;
  4131. else if (os_strcmp(param, "p2p") == 0)
  4132. wpa_s->global->conc_pref = WPA_CONC_PREF_P2P;
  4133. else {
  4134. wpa_printf(MSG_INFO, "Invalid conc_pref value");
  4135. return -1;
  4136. }
  4137. wpa_printf(MSG_DEBUG, "Single channel concurrency preference: "
  4138. "%s", param);
  4139. return 0;
  4140. }
  4141. if (os_strcmp(cmd, "force_long_sd") == 0) {
  4142. wpa_s->force_long_sd = atoi(param);
  4143. return 0;
  4144. }
  4145. if (os_strcmp(cmd, "peer_filter") == 0) {
  4146. u8 addr[ETH_ALEN];
  4147. if (hwaddr_aton(param, addr))
  4148. return -1;
  4149. p2p_set_peer_filter(wpa_s->global->p2p, addr);
  4150. return 0;
  4151. }
  4152. if (os_strcmp(cmd, "cross_connect") == 0)
  4153. return wpas_p2p_set_cross_connect(wpa_s, atoi(param));
  4154. if (os_strcmp(cmd, "go_apsd") == 0) {
  4155. if (os_strcmp(param, "disable") == 0)
  4156. wpa_s->set_ap_uapsd = 0;
  4157. else {
  4158. wpa_s->set_ap_uapsd = 1;
  4159. wpa_s->ap_uapsd = atoi(param);
  4160. }
  4161. return 0;
  4162. }
  4163. if (os_strcmp(cmd, "client_apsd") == 0) {
  4164. if (os_strcmp(param, "disable") == 0)
  4165. wpa_s->set_sta_uapsd = 0;
  4166. else {
  4167. int be, bk, vi, vo;
  4168. char *pos;
  4169. /* format: BE,BK,VI,VO;max SP Length */
  4170. be = atoi(param);
  4171. pos = os_strchr(param, ',');
  4172. if (pos == NULL)
  4173. return -1;
  4174. pos++;
  4175. bk = atoi(pos);
  4176. pos = os_strchr(pos, ',');
  4177. if (pos == NULL)
  4178. return -1;
  4179. pos++;
  4180. vi = atoi(pos);
  4181. pos = os_strchr(pos, ',');
  4182. if (pos == NULL)
  4183. return -1;
  4184. pos++;
  4185. vo = atoi(pos);
  4186. /* ignore max SP Length for now */
  4187. wpa_s->set_sta_uapsd = 1;
  4188. wpa_s->sta_uapsd = 0;
  4189. if (be)
  4190. wpa_s->sta_uapsd |= BIT(0);
  4191. if (bk)
  4192. wpa_s->sta_uapsd |= BIT(1);
  4193. if (vi)
  4194. wpa_s->sta_uapsd |= BIT(2);
  4195. if (vo)
  4196. wpa_s->sta_uapsd |= BIT(3);
  4197. }
  4198. return 0;
  4199. }
  4200. if (os_strcmp(cmd, "disallow_freq") == 0)
  4201. return p2p_ctrl_disallow_freq(wpa_s, param);
  4202. if (os_strcmp(cmd, "disc_int") == 0) {
  4203. int min_disc_int, max_disc_int, max_disc_tu;
  4204. char *pos;
  4205. pos = param;
  4206. min_disc_int = atoi(pos);
  4207. pos = os_strchr(pos, ' ');
  4208. if (pos == NULL)
  4209. return -1;
  4210. *pos++ = '\0';
  4211. max_disc_int = atoi(pos);
  4212. pos = os_strchr(pos, ' ');
  4213. if (pos == NULL)
  4214. return -1;
  4215. *pos++ = '\0';
  4216. max_disc_tu = atoi(pos);
  4217. return p2p_set_disc_int(wpa_s->global->p2p, min_disc_int,
  4218. max_disc_int, max_disc_tu);
  4219. }
  4220. if (os_strcmp(cmd, "per_sta_psk") == 0) {
  4221. wpa_s->global->p2p_per_sta_psk = !!atoi(param);
  4222. return 0;
  4223. }
  4224. #ifdef CONFIG_WPS_NFC
  4225. if (os_strcmp(cmd, "nfc_tag") == 0)
  4226. return wpas_p2p_nfc_tag_enabled(wpa_s, !!atoi(param));
  4227. #endif /* CONFIG_WPS_NFC */
  4228. if (os_strcmp(cmd, "disable_ip_addr_req") == 0) {
  4229. wpa_s->p2p_disable_ip_addr_req = !!atoi(param);
  4230. return 0;
  4231. }
  4232. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Unknown P2P_SET field value '%s'",
  4233. cmd);
  4234. return -1;
  4235. }
  4236. static void p2p_ctrl_flush(struct wpa_supplicant *wpa_s)
  4237. {
  4238. os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN);
  4239. wpa_s->force_long_sd = 0;
  4240. if (wpa_s->global->p2p)
  4241. p2p_flush(wpa_s->global->p2p);
  4242. }
  4243. static int p2p_ctrl_presence_req(struct wpa_supplicant *wpa_s, char *cmd)
  4244. {
  4245. char *pos, *pos2;
  4246. unsigned int dur1 = 0, int1 = 0, dur2 = 0, int2 = 0;
  4247. if (cmd[0]) {
  4248. pos = os_strchr(cmd, ' ');
  4249. if (pos == NULL)
  4250. return -1;
  4251. *pos++ = '\0';
  4252. dur1 = atoi(cmd);
  4253. pos2 = os_strchr(pos, ' ');
  4254. if (pos2)
  4255. *pos2++ = '\0';
  4256. int1 = atoi(pos);
  4257. } else
  4258. pos2 = NULL;
  4259. if (pos2) {
  4260. pos = os_strchr(pos2, ' ');
  4261. if (pos == NULL)
  4262. return -1;
  4263. *pos++ = '\0';
  4264. dur2 = atoi(pos2);
  4265. int2 = atoi(pos);
  4266. }
  4267. return wpas_p2p_presence_req(wpa_s, dur1, int1, dur2, int2);
  4268. }
  4269. static int p2p_ctrl_ext_listen(struct wpa_supplicant *wpa_s, char *cmd)
  4270. {
  4271. char *pos;
  4272. unsigned int period = 0, interval = 0;
  4273. if (cmd[0]) {
  4274. pos = os_strchr(cmd, ' ');
  4275. if (pos == NULL)
  4276. return -1;
  4277. *pos++ = '\0';
  4278. period = atoi(cmd);
  4279. interval = atoi(pos);
  4280. }
  4281. return wpas_p2p_ext_listen(wpa_s, period, interval);
  4282. }
  4283. static int p2p_ctrl_remove_client(struct wpa_supplicant *wpa_s, const char *cmd)
  4284. {
  4285. const char *pos;
  4286. u8 peer[ETH_ALEN];
  4287. int iface_addr = 0;
  4288. pos = cmd;
  4289. if (os_strncmp(pos, "iface=", 6) == 0) {
  4290. iface_addr = 1;
  4291. pos += 6;
  4292. }
  4293. if (hwaddr_aton(pos, peer))
  4294. return -1;
  4295. wpas_p2p_remove_client(wpa_s, peer, iface_addr);
  4296. return 0;
  4297. }
  4298. #endif /* CONFIG_P2P */
  4299. static int * freq_range_to_channel_list(struct wpa_supplicant *wpa_s, char *val)
  4300. {
  4301. struct wpa_freq_range_list ranges;
  4302. int *freqs = NULL;
  4303. struct hostapd_hw_modes *mode;
  4304. u16 i;
  4305. if (wpa_s->hw.modes == NULL)
  4306. return NULL;
  4307. os_memset(&ranges, 0, sizeof(ranges));
  4308. if (freq_range_list_parse(&ranges, val) < 0)
  4309. return NULL;
  4310. for (i = 0; i < wpa_s->hw.num_modes; i++) {
  4311. int j;
  4312. mode = &wpa_s->hw.modes[i];
  4313. for (j = 0; j < mode->num_channels; j++) {
  4314. unsigned int freq;
  4315. if (mode->channels[j].flag & HOSTAPD_CHAN_DISABLED)
  4316. continue;
  4317. freq = mode->channels[j].freq;
  4318. if (!freq_range_list_includes(&ranges, freq))
  4319. continue;
  4320. int_array_add_unique(&freqs, freq);
  4321. }
  4322. }
  4323. os_free(ranges.range);
  4324. return freqs;
  4325. }
  4326. #ifdef CONFIG_INTERWORKING
  4327. static int ctrl_interworking_select(struct wpa_supplicant *wpa_s, char *param)
  4328. {
  4329. int auto_sel = 0;
  4330. int *freqs = NULL;
  4331. if (param) {
  4332. char *pos;
  4333. auto_sel = os_strstr(param, "auto") != NULL;
  4334. pos = os_strstr(param, "freq=");
  4335. if (pos) {
  4336. freqs = freq_range_to_channel_list(wpa_s, pos + 5);
  4337. if (freqs == NULL)
  4338. return -1;
  4339. }
  4340. }
  4341. return interworking_select(wpa_s, auto_sel, freqs);
  4342. }
  4343. static int ctrl_interworking_connect(struct wpa_supplicant *wpa_s, char *dst)
  4344. {
  4345. u8 bssid[ETH_ALEN];
  4346. struct wpa_bss *bss;
  4347. if (hwaddr_aton(dst, bssid)) {
  4348. wpa_printf(MSG_DEBUG, "Invalid BSSID '%s'", dst);
  4349. return -1;
  4350. }
  4351. bss = wpa_bss_get_bssid(wpa_s, bssid);
  4352. if (bss == NULL) {
  4353. wpa_printf(MSG_DEBUG, "Could not find BSS " MACSTR,
  4354. MAC2STR(bssid));
  4355. return -1;
  4356. }
  4357. return interworking_connect(wpa_s, bss);
  4358. }
  4359. static int get_anqp(struct wpa_supplicant *wpa_s, char *dst)
  4360. {
  4361. u8 dst_addr[ETH_ALEN];
  4362. int used;
  4363. char *pos;
  4364. #define MAX_ANQP_INFO_ID 100
  4365. u16 id[MAX_ANQP_INFO_ID];
  4366. size_t num_id = 0;
  4367. u32 subtypes = 0;
  4368. used = hwaddr_aton2(dst, dst_addr);
  4369. if (used < 0)
  4370. return -1;
  4371. pos = dst + used;
  4372. while (num_id < MAX_ANQP_INFO_ID) {
  4373. if (os_strncmp(pos, "hs20:", 5) == 0) {
  4374. #ifdef CONFIG_HS20
  4375. int num = atoi(pos + 5);
  4376. if (num <= 0 || num > 31)
  4377. return -1;
  4378. subtypes |= BIT(num);
  4379. #else /* CONFIG_HS20 */
  4380. return -1;
  4381. #endif /* CONFIG_HS20 */
  4382. } else {
  4383. id[num_id] = atoi(pos);
  4384. if (id[num_id])
  4385. num_id++;
  4386. }
  4387. pos = os_strchr(pos + 1, ',');
  4388. if (pos == NULL)
  4389. break;
  4390. pos++;
  4391. }
  4392. if (num_id == 0)
  4393. return -1;
  4394. return anqp_send_req(wpa_s, dst_addr, id, num_id, subtypes);
  4395. }
  4396. static int gas_request(struct wpa_supplicant *wpa_s, char *cmd)
  4397. {
  4398. u8 dst_addr[ETH_ALEN];
  4399. struct wpabuf *advproto, *query = NULL;
  4400. int used, ret = -1;
  4401. char *pos, *end;
  4402. size_t len;
  4403. used = hwaddr_aton2(cmd, dst_addr);
  4404. if (used < 0)
  4405. return -1;
  4406. pos = cmd + used;
  4407. while (*pos == ' ')
  4408. pos++;
  4409. /* Advertisement Protocol ID */
  4410. end = os_strchr(pos, ' ');
  4411. if (end)
  4412. len = end - pos;
  4413. else
  4414. len = os_strlen(pos);
  4415. if (len & 0x01)
  4416. return -1;
  4417. len /= 2;
  4418. if (len == 0)
  4419. return -1;
  4420. advproto = wpabuf_alloc(len);
  4421. if (advproto == NULL)
  4422. return -1;
  4423. if (hexstr2bin(pos, wpabuf_put(advproto, len), len) < 0)
  4424. goto fail;
  4425. if (end) {
  4426. /* Optional Query Request */
  4427. pos = end + 1;
  4428. while (*pos == ' ')
  4429. pos++;
  4430. len = os_strlen(pos);
  4431. if (len) {
  4432. if (len & 0x01)
  4433. goto fail;
  4434. len /= 2;
  4435. if (len == 0)
  4436. goto fail;
  4437. query = wpabuf_alloc(len);
  4438. if (query == NULL)
  4439. goto fail;
  4440. if (hexstr2bin(pos, wpabuf_put(query, len), len) < 0)
  4441. goto fail;
  4442. }
  4443. }
  4444. ret = gas_send_request(wpa_s, dst_addr, advproto, query);
  4445. fail:
  4446. wpabuf_free(advproto);
  4447. wpabuf_free(query);
  4448. return ret;
  4449. }
  4450. static int gas_response_get(struct wpa_supplicant *wpa_s, char *cmd, char *buf,
  4451. size_t buflen)
  4452. {
  4453. u8 addr[ETH_ALEN];
  4454. int dialog_token;
  4455. int used;
  4456. char *pos;
  4457. size_t resp_len, start, requested_len;
  4458. struct wpabuf *resp;
  4459. int ret;
  4460. used = hwaddr_aton2(cmd, addr);
  4461. if (used < 0)
  4462. return -1;
  4463. pos = cmd + used;
  4464. while (*pos == ' ')
  4465. pos++;
  4466. dialog_token = atoi(pos);
  4467. if (wpa_s->last_gas_resp &&
  4468. os_memcmp(addr, wpa_s->last_gas_addr, ETH_ALEN) == 0 &&
  4469. dialog_token == wpa_s->last_gas_dialog_token)
  4470. resp = wpa_s->last_gas_resp;
  4471. else if (wpa_s->prev_gas_resp &&
  4472. os_memcmp(addr, wpa_s->prev_gas_addr, ETH_ALEN) == 0 &&
  4473. dialog_token == wpa_s->prev_gas_dialog_token)
  4474. resp = wpa_s->prev_gas_resp;
  4475. else
  4476. return -1;
  4477. resp_len = wpabuf_len(resp);
  4478. start = 0;
  4479. requested_len = resp_len;
  4480. pos = os_strchr(pos, ' ');
  4481. if (pos) {
  4482. start = atoi(pos);
  4483. if (start > resp_len)
  4484. return os_snprintf(buf, buflen, "FAIL-Invalid range");
  4485. pos = os_strchr(pos, ',');
  4486. if (pos == NULL)
  4487. return -1;
  4488. pos++;
  4489. requested_len = atoi(pos);
  4490. if (start + requested_len > resp_len)
  4491. return os_snprintf(buf, buflen, "FAIL-Invalid range");
  4492. }
  4493. if (requested_len * 2 + 1 > buflen)
  4494. return os_snprintf(buf, buflen, "FAIL-Too long response");
  4495. ret = wpa_snprintf_hex(buf, buflen, wpabuf_head_u8(resp) + start,
  4496. requested_len);
  4497. if (start + requested_len == resp_len) {
  4498. /*
  4499. * Free memory by dropping the response after it has been
  4500. * fetched.
  4501. */
  4502. if (resp == wpa_s->prev_gas_resp) {
  4503. wpabuf_free(wpa_s->prev_gas_resp);
  4504. wpa_s->prev_gas_resp = NULL;
  4505. } else {
  4506. wpabuf_free(wpa_s->last_gas_resp);
  4507. wpa_s->last_gas_resp = NULL;
  4508. }
  4509. }
  4510. return ret;
  4511. }
  4512. #endif /* CONFIG_INTERWORKING */
  4513. #ifdef CONFIG_HS20
  4514. static int get_hs20_anqp(struct wpa_supplicant *wpa_s, char *dst)
  4515. {
  4516. u8 dst_addr[ETH_ALEN];
  4517. int used;
  4518. char *pos;
  4519. u32 subtypes = 0;
  4520. used = hwaddr_aton2(dst, dst_addr);
  4521. if (used < 0)
  4522. return -1;
  4523. pos = dst + used;
  4524. for (;;) {
  4525. int num = atoi(pos);
  4526. if (num <= 0 || num > 31)
  4527. return -1;
  4528. subtypes |= BIT(num);
  4529. pos = os_strchr(pos + 1, ',');
  4530. if (pos == NULL)
  4531. break;
  4532. pos++;
  4533. }
  4534. if (subtypes == 0)
  4535. return -1;
  4536. return hs20_anqp_send_req(wpa_s, dst_addr, subtypes, NULL, 0);
  4537. }
  4538. static int hs20_nai_home_realm_list(struct wpa_supplicant *wpa_s,
  4539. const u8 *addr, const char *realm)
  4540. {
  4541. u8 *buf;
  4542. size_t rlen, len;
  4543. int ret;
  4544. rlen = os_strlen(realm);
  4545. len = 3 + rlen;
  4546. buf = os_malloc(len);
  4547. if (buf == NULL)
  4548. return -1;
  4549. buf[0] = 1; /* NAI Home Realm Count */
  4550. buf[1] = 0; /* Formatted in accordance with RFC 4282 */
  4551. buf[2] = rlen;
  4552. os_memcpy(buf + 3, realm, rlen);
  4553. ret = hs20_anqp_send_req(wpa_s, addr,
  4554. BIT(HS20_STYPE_NAI_HOME_REALM_QUERY),
  4555. buf, len);
  4556. os_free(buf);
  4557. return ret;
  4558. }
  4559. static int hs20_get_nai_home_realm_list(struct wpa_supplicant *wpa_s,
  4560. char *dst)
  4561. {
  4562. struct wpa_cred *cred = wpa_s->conf->cred;
  4563. u8 dst_addr[ETH_ALEN];
  4564. int used;
  4565. u8 *buf;
  4566. size_t len;
  4567. int ret;
  4568. used = hwaddr_aton2(dst, dst_addr);
  4569. if (used < 0)
  4570. return -1;
  4571. while (dst[used] == ' ')
  4572. used++;
  4573. if (os_strncmp(dst + used, "realm=", 6) == 0)
  4574. return hs20_nai_home_realm_list(wpa_s, dst_addr,
  4575. dst + used + 6);
  4576. len = os_strlen(dst + used);
  4577. if (len == 0 && cred && cred->realm)
  4578. return hs20_nai_home_realm_list(wpa_s, dst_addr, cred->realm);
  4579. if (len & 1)
  4580. return -1;
  4581. len /= 2;
  4582. buf = os_malloc(len);
  4583. if (buf == NULL)
  4584. return -1;
  4585. if (hexstr2bin(dst + used, buf, len) < 0) {
  4586. os_free(buf);
  4587. return -1;
  4588. }
  4589. ret = hs20_anqp_send_req(wpa_s, dst_addr,
  4590. BIT(HS20_STYPE_NAI_HOME_REALM_QUERY),
  4591. buf, len);
  4592. os_free(buf);
  4593. return ret;
  4594. }
  4595. static int hs20_icon_request(struct wpa_supplicant *wpa_s, char *cmd)
  4596. {
  4597. u8 dst_addr[ETH_ALEN];
  4598. int used;
  4599. char *icon;
  4600. used = hwaddr_aton2(cmd, dst_addr);
  4601. if (used < 0)
  4602. return -1;
  4603. while (cmd[used] == ' ')
  4604. used++;
  4605. icon = &cmd[used];
  4606. wpa_s->fetch_osu_icon_in_progress = 0;
  4607. return hs20_anqp_send_req(wpa_s, dst_addr, BIT(HS20_STYPE_ICON_REQUEST),
  4608. (u8 *) icon, os_strlen(icon));
  4609. }
  4610. #endif /* CONFIG_HS20 */
  4611. static int wpa_supplicant_ctrl_iface_sta_autoconnect(
  4612. struct wpa_supplicant *wpa_s, char *cmd)
  4613. {
  4614. wpa_s->auto_reconnect_disabled = atoi(cmd) == 0 ? 1 : 0;
  4615. return 0;
  4616. }
  4617. #ifdef CONFIG_AUTOSCAN
  4618. static int wpa_supplicant_ctrl_iface_autoscan(struct wpa_supplicant *wpa_s,
  4619. char *cmd)
  4620. {
  4621. enum wpa_states state = wpa_s->wpa_state;
  4622. char *new_params = NULL;
  4623. if (os_strlen(cmd) > 0) {
  4624. new_params = os_strdup(cmd);
  4625. if (new_params == NULL)
  4626. return -1;
  4627. }
  4628. os_free(wpa_s->conf->autoscan);
  4629. wpa_s->conf->autoscan = new_params;
  4630. if (wpa_s->conf->autoscan == NULL)
  4631. autoscan_deinit(wpa_s);
  4632. else if (state == WPA_DISCONNECTED || state == WPA_INACTIVE)
  4633. autoscan_init(wpa_s, 1);
  4634. else if (state == WPA_SCANNING)
  4635. wpa_supplicant_reinit_autoscan(wpa_s);
  4636. return 0;
  4637. }
  4638. #endif /* CONFIG_AUTOSCAN */
  4639. #ifdef CONFIG_WNM
  4640. static int wpas_ctrl_iface_wnm_sleep(struct wpa_supplicant *wpa_s, char *cmd)
  4641. {
  4642. int enter;
  4643. int intval = 0;
  4644. char *pos;
  4645. int ret;
  4646. struct wpabuf *tfs_req = NULL;
  4647. if (os_strncmp(cmd, "enter", 5) == 0)
  4648. enter = 1;
  4649. else if (os_strncmp(cmd, "exit", 4) == 0)
  4650. enter = 0;
  4651. else
  4652. return -1;
  4653. pos = os_strstr(cmd, " interval=");
  4654. if (pos)
  4655. intval = atoi(pos + 10);
  4656. pos = os_strstr(cmd, " tfs_req=");
  4657. if (pos) {
  4658. char *end;
  4659. size_t len;
  4660. pos += 9;
  4661. end = os_strchr(pos, ' ');
  4662. if (end)
  4663. len = end - pos;
  4664. else
  4665. len = os_strlen(pos);
  4666. if (len & 1)
  4667. return -1;
  4668. len /= 2;
  4669. tfs_req = wpabuf_alloc(len);
  4670. if (tfs_req == NULL)
  4671. return -1;
  4672. if (hexstr2bin(pos, wpabuf_put(tfs_req, len), len) < 0) {
  4673. wpabuf_free(tfs_req);
  4674. return -1;
  4675. }
  4676. }
  4677. ret = ieee802_11_send_wnmsleep_req(wpa_s, enter ? WNM_SLEEP_MODE_ENTER :
  4678. WNM_SLEEP_MODE_EXIT, intval,
  4679. tfs_req);
  4680. wpabuf_free(tfs_req);
  4681. return ret;
  4682. }
  4683. static int wpas_ctrl_iface_wnm_bss_query(struct wpa_supplicant *wpa_s, char *cmd)
  4684. {
  4685. int query_reason;
  4686. query_reason = atoi(cmd);
  4687. wpa_printf(MSG_DEBUG, "CTRL_IFACE: WNM_BSS_QUERY query_reason=%d",
  4688. query_reason);
  4689. return wnm_send_bss_transition_mgmt_query(wpa_s, query_reason);
  4690. }
  4691. #endif /* CONFIG_WNM */
  4692. static int wpa_supplicant_signal_poll(struct wpa_supplicant *wpa_s, char *buf,
  4693. size_t buflen)
  4694. {
  4695. struct wpa_signal_info si;
  4696. int ret;
  4697. char *pos, *end;
  4698. ret = wpa_drv_signal_poll(wpa_s, &si);
  4699. if (ret)
  4700. return -1;
  4701. pos = buf;
  4702. end = buf + buflen;
  4703. ret = os_snprintf(pos, end - pos, "RSSI=%d\nLINKSPEED=%d\n"
  4704. "NOISE=%d\nFREQUENCY=%u\n",
  4705. si.current_signal, si.current_txrate / 1000,
  4706. si.current_noise, si.frequency);
  4707. if (ret < 0 || ret > end - pos)
  4708. return -1;
  4709. pos += ret;
  4710. if (si.chanwidth != CHAN_WIDTH_UNKNOWN) {
  4711. ret = os_snprintf(pos, end - pos, "WIDTH=%s\n",
  4712. channel_width_to_string(si.chanwidth));
  4713. if (ret < 0 || ret > end - pos)
  4714. return -1;
  4715. pos += ret;
  4716. }
  4717. if (si.center_frq1 > 0 && si.center_frq2 > 0) {
  4718. ret = os_snprintf(pos, end - pos,
  4719. "CENTER_FRQ1=%d\nCENTER_FRQ2=%d\n",
  4720. si.center_frq1, si.center_frq2);
  4721. if (ret < 0 || ret > end - pos)
  4722. return -1;
  4723. pos += ret;
  4724. }
  4725. if (si.avg_signal) {
  4726. ret = os_snprintf(pos, end - pos,
  4727. "AVG_RSSI=%d\n", si.avg_signal);
  4728. if (ret < 0 || ret >= end - pos)
  4729. return -1;
  4730. pos += ret;
  4731. }
  4732. return pos - buf;
  4733. }
  4734. static int wpa_supplicant_pktcnt_poll(struct wpa_supplicant *wpa_s, char *buf,
  4735. size_t buflen)
  4736. {
  4737. struct hostap_sta_driver_data sta;
  4738. int ret;
  4739. ret = wpa_drv_pktcnt_poll(wpa_s, &sta);
  4740. if (ret)
  4741. return -1;
  4742. ret = os_snprintf(buf, buflen, "TXGOOD=%lu\nTXBAD=%lu\nRXGOOD=%lu\n",
  4743. sta.tx_packets, sta.tx_retry_failed, sta.rx_packets);
  4744. if (ret < 0 || (size_t) ret > buflen)
  4745. return -1;
  4746. return ret;
  4747. }
  4748. #ifdef ANDROID
  4749. static int wpa_supplicant_driver_cmd(struct wpa_supplicant *wpa_s, char *cmd,
  4750. char *buf, size_t buflen)
  4751. {
  4752. int ret;
  4753. ret = wpa_drv_driver_cmd(wpa_s, cmd, buf, buflen);
  4754. if (ret == 0) {
  4755. if (os_strncasecmp(cmd, "COUNTRY", 7) == 0) {
  4756. struct p2p_data *p2p = wpa_s->global->p2p;
  4757. if (p2p) {
  4758. char country[3];
  4759. country[0] = cmd[8];
  4760. country[1] = cmd[9];
  4761. country[2] = 0x04;
  4762. p2p_set_country(p2p, country);
  4763. }
  4764. }
  4765. ret = os_snprintf(buf, buflen, "%s\n", "OK");
  4766. }
  4767. return ret;
  4768. }
  4769. #endif /* ANDROID */
  4770. static int wpa_supplicant_vendor_cmd(struct wpa_supplicant *wpa_s, char *cmd,
  4771. char *buf, size_t buflen)
  4772. {
  4773. int ret;
  4774. char *pos;
  4775. u8 *data = NULL;
  4776. unsigned int vendor_id, subcmd;
  4777. struct wpabuf *reply;
  4778. size_t data_len = 0;
  4779. /* cmd: <vendor id> <subcommand id> [<hex formatted data>] */
  4780. vendor_id = strtoul(cmd, &pos, 16);
  4781. if (!isblank(*pos))
  4782. return -EINVAL;
  4783. subcmd = strtoul(pos, &pos, 10);
  4784. if (*pos != '\0') {
  4785. if (!isblank(*pos++))
  4786. return -EINVAL;
  4787. data_len = os_strlen(pos);
  4788. }
  4789. if (data_len) {
  4790. data_len /= 2;
  4791. data = os_malloc(data_len);
  4792. if (!data)
  4793. return -1;
  4794. if (hexstr2bin(pos, data, data_len)) {
  4795. wpa_printf(MSG_DEBUG,
  4796. "Vendor command: wrong parameter format");
  4797. os_free(data);
  4798. return -EINVAL;
  4799. }
  4800. }
  4801. reply = wpabuf_alloc((buflen - 1) / 2);
  4802. if (!reply) {
  4803. os_free(data);
  4804. return -1;
  4805. }
  4806. ret = wpa_drv_vendor_cmd(wpa_s, vendor_id, subcmd, data, data_len,
  4807. reply);
  4808. if (ret == 0)
  4809. ret = wpa_snprintf_hex(buf, buflen, wpabuf_head_u8(reply),
  4810. wpabuf_len(reply));
  4811. wpabuf_free(reply);
  4812. os_free(data);
  4813. return ret;
  4814. }
  4815. static void wpa_supplicant_ctrl_iface_flush(struct wpa_supplicant *wpa_s)
  4816. {
  4817. wpa_dbg(wpa_s, MSG_DEBUG, "Flush all wpa_supplicant state");
  4818. #ifdef CONFIG_P2P
  4819. wpas_p2p_cancel(wpa_s);
  4820. wpas_p2p_stop_find(wpa_s);
  4821. p2p_ctrl_flush(wpa_s);
  4822. wpas_p2p_group_remove(wpa_s, "*");
  4823. wpas_p2p_service_flush(wpa_s);
  4824. wpa_s->global->p2p_disabled = 0;
  4825. wpa_s->global->p2p_per_sta_psk = 0;
  4826. wpa_s->conf->num_sec_device_types = 0;
  4827. wpa_s->p2p_disable_ip_addr_req = 0;
  4828. os_free(wpa_s->global->p2p_go_avoid_freq.range);
  4829. wpa_s->global->p2p_go_avoid_freq.range = NULL;
  4830. #endif /* CONFIG_P2P */
  4831. #ifdef CONFIG_WPS_TESTING
  4832. wps_version_number = 0x20;
  4833. wps_testing_dummy_cred = 0;
  4834. wps_corrupt_pkhash = 0;
  4835. #endif /* CONFIG_WPS_TESTING */
  4836. #ifdef CONFIG_WPS
  4837. wpa_s->wps_fragment_size = 0;
  4838. wpas_wps_cancel(wpa_s);
  4839. #endif /* CONFIG_WPS */
  4840. wpa_s->after_wps = 0;
  4841. wpa_s->known_wps_freq = 0;
  4842. #ifdef CONFIG_TDLS
  4843. #ifdef CONFIG_TDLS_TESTING
  4844. extern unsigned int tdls_testing;
  4845. tdls_testing = 0;
  4846. #endif /* CONFIG_TDLS_TESTING */
  4847. wpa_drv_tdls_oper(wpa_s, TDLS_ENABLE, NULL);
  4848. wpa_tdls_enable(wpa_s->wpa, 1);
  4849. #endif /* CONFIG_TDLS */
  4850. eloop_cancel_timeout(wpa_supplicant_stop_countermeasures, wpa_s, NULL);
  4851. wpa_supplicant_stop_countermeasures(wpa_s, NULL);
  4852. wpa_s->no_keep_alive = 0;
  4853. os_free(wpa_s->disallow_aps_bssid);
  4854. wpa_s->disallow_aps_bssid = NULL;
  4855. wpa_s->disallow_aps_bssid_count = 0;
  4856. os_free(wpa_s->disallow_aps_ssid);
  4857. wpa_s->disallow_aps_ssid = NULL;
  4858. wpa_s->disallow_aps_ssid_count = 0;
  4859. wpa_s->set_sta_uapsd = 0;
  4860. wpa_s->sta_uapsd = 0;
  4861. wpa_drv_radio_disable(wpa_s, 0);
  4862. wpa_bss_flush(wpa_s);
  4863. wpa_blacklist_clear(wpa_s);
  4864. wpa_s->extra_blacklist_count = 0;
  4865. wpa_supplicant_ctrl_iface_remove_network(wpa_s, "all");
  4866. wpa_supplicant_ctrl_iface_remove_cred(wpa_s, "all");
  4867. wpa_config_flush_blobs(wpa_s->conf);
  4868. wpa_s->conf->auto_interworking = 0;
  4869. wpa_s->conf->okc = 0;
  4870. wpa_sm_set_param(wpa_s->wpa, RSNA_PMK_LIFETIME, 43200);
  4871. wpa_sm_set_param(wpa_s->wpa, RSNA_PMK_REAUTH_THRESHOLD, 70);
  4872. wpa_sm_set_param(wpa_s->wpa, RSNA_SA_TIMEOUT, 60);
  4873. eapol_sm_notify_logoff(wpa_s->eapol, FALSE);
  4874. radio_remove_works(wpa_s, NULL, 1);
  4875. wpa_s->next_ssid = NULL;
  4876. #ifdef CONFIG_INTERWORKING
  4877. hs20_cancel_fetch_osu(wpa_s);
  4878. #endif /* CONFIG_INTERWORKING */
  4879. wpa_s->ext_mgmt_frame_handling = 0;
  4880. }
  4881. static int wpas_ctrl_radio_work_show(struct wpa_supplicant *wpa_s,
  4882. char *buf, size_t buflen)
  4883. {
  4884. struct wpa_radio_work *work;
  4885. char *pos, *end;
  4886. struct os_reltime now, diff;
  4887. pos = buf;
  4888. end = buf + buflen;
  4889. os_get_reltime(&now);
  4890. dl_list_for_each(work, &wpa_s->radio->work, struct wpa_radio_work, list)
  4891. {
  4892. int ret;
  4893. os_reltime_sub(&now, &work->time, &diff);
  4894. ret = os_snprintf(pos, end - pos, "%s@%s:%u:%u:%ld.%06ld\n",
  4895. work->type, work->wpa_s->ifname, work->freq,
  4896. work->started, diff.sec, diff.usec);
  4897. if (ret < 0 || ret >= end - pos)
  4898. break;
  4899. pos += ret;
  4900. }
  4901. return pos - buf;
  4902. }
  4903. static void wpas_ctrl_radio_work_timeout(void *eloop_ctx, void *timeout_ctx)
  4904. {
  4905. struct wpa_radio_work *work = eloop_ctx;
  4906. struct wpa_external_work *ework = work->ctx;
  4907. wpa_dbg(work->wpa_s, MSG_DEBUG,
  4908. "Timing out external radio work %u (%s)",
  4909. ework->id, work->type);
  4910. wpa_msg(work->wpa_s, MSG_INFO, EXT_RADIO_WORK_TIMEOUT "%u", ework->id);
  4911. radio_work_done(work);
  4912. os_free(ework);
  4913. }
  4914. static void wpas_ctrl_radio_work_cb(struct wpa_radio_work *work, int deinit)
  4915. {
  4916. struct wpa_external_work *ework = work->ctx;
  4917. if (deinit) {
  4918. if (work->started)
  4919. eloop_cancel_timeout(wpas_ctrl_radio_work_timeout,
  4920. work, NULL);
  4921. os_free(ework);
  4922. return;
  4923. }
  4924. wpa_dbg(work->wpa_s, MSG_DEBUG, "Starting external radio work %u (%s)",
  4925. ework->id, ework->type);
  4926. wpa_msg(work->wpa_s, MSG_INFO, EXT_RADIO_WORK_START "%u", ework->id);
  4927. if (!ework->timeout)
  4928. ework->timeout = 10;
  4929. eloop_register_timeout(ework->timeout, 0, wpas_ctrl_radio_work_timeout,
  4930. work, NULL);
  4931. }
  4932. static int wpas_ctrl_radio_work_add(struct wpa_supplicant *wpa_s, char *cmd,
  4933. char *buf, size_t buflen)
  4934. {
  4935. struct wpa_external_work *ework;
  4936. char *pos, *pos2;
  4937. size_t type_len;
  4938. int ret;
  4939. unsigned int freq = 0;
  4940. /* format: <name> [freq=<MHz>] [timeout=<seconds>] */
  4941. ework = os_zalloc(sizeof(*ework));
  4942. if (ework == NULL)
  4943. return -1;
  4944. pos = os_strchr(cmd, ' ');
  4945. if (pos) {
  4946. type_len = pos - cmd;
  4947. pos++;
  4948. pos2 = os_strstr(pos, "freq=");
  4949. if (pos2)
  4950. freq = atoi(pos2 + 5);
  4951. pos2 = os_strstr(pos, "timeout=");
  4952. if (pos2)
  4953. ework->timeout = atoi(pos2 + 8);
  4954. } else {
  4955. type_len = os_strlen(cmd);
  4956. }
  4957. if (4 + type_len >= sizeof(ework->type))
  4958. type_len = sizeof(ework->type) - 4 - 1;
  4959. os_strlcpy(ework->type, "ext:", sizeof(ework->type));
  4960. os_memcpy(ework->type + 4, cmd, type_len);
  4961. ework->type[4 + type_len] = '\0';
  4962. wpa_s->ext_work_id++;
  4963. if (wpa_s->ext_work_id == 0)
  4964. wpa_s->ext_work_id++;
  4965. ework->id = wpa_s->ext_work_id;
  4966. if (radio_add_work(wpa_s, freq, ework->type, 0, wpas_ctrl_radio_work_cb,
  4967. ework) < 0) {
  4968. os_free(ework);
  4969. return -1;
  4970. }
  4971. ret = os_snprintf(buf, buflen, "%u", ework->id);
  4972. if (ret < 0 || (size_t) ret >= buflen)
  4973. return -1;
  4974. return ret;
  4975. }
  4976. static int wpas_ctrl_radio_work_done(struct wpa_supplicant *wpa_s, char *cmd)
  4977. {
  4978. struct wpa_radio_work *work;
  4979. unsigned int id = atoi(cmd);
  4980. dl_list_for_each(work, &wpa_s->radio->work, struct wpa_radio_work, list)
  4981. {
  4982. struct wpa_external_work *ework;
  4983. if (os_strncmp(work->type, "ext:", 4) != 0)
  4984. continue;
  4985. ework = work->ctx;
  4986. if (id && ework->id != id)
  4987. continue;
  4988. wpa_dbg(wpa_s, MSG_DEBUG,
  4989. "Completed external radio work %u (%s)",
  4990. ework->id, ework->type);
  4991. eloop_cancel_timeout(wpas_ctrl_radio_work_timeout, work, NULL);
  4992. radio_work_done(work);
  4993. os_free(ework);
  4994. return 3; /* "OK\n" */
  4995. }
  4996. return -1;
  4997. }
  4998. static int wpas_ctrl_radio_work(struct wpa_supplicant *wpa_s, char *cmd,
  4999. char *buf, size_t buflen)
  5000. {
  5001. if (os_strcmp(cmd, "show") == 0)
  5002. return wpas_ctrl_radio_work_show(wpa_s, buf, buflen);
  5003. if (os_strncmp(cmd, "add ", 4) == 0)
  5004. return wpas_ctrl_radio_work_add(wpa_s, cmd + 4, buf, buflen);
  5005. if (os_strncmp(cmd, "done ", 5) == 0)
  5006. return wpas_ctrl_radio_work_done(wpa_s, cmd + 4);
  5007. return -1;
  5008. }
  5009. void wpas_ctrl_radio_work_flush(struct wpa_supplicant *wpa_s)
  5010. {
  5011. struct wpa_radio_work *work, *tmp;
  5012. if (!wpa_s || !wpa_s->radio)
  5013. return;
  5014. dl_list_for_each_safe(work, tmp, &wpa_s->radio->work,
  5015. struct wpa_radio_work, list) {
  5016. struct wpa_external_work *ework;
  5017. if (os_strncmp(work->type, "ext:", 4) != 0)
  5018. continue;
  5019. ework = work->ctx;
  5020. wpa_dbg(wpa_s, MSG_DEBUG,
  5021. "Flushing%s external radio work %u (%s)",
  5022. work->started ? " started" : "", ework->id,
  5023. ework->type);
  5024. if (work->started)
  5025. eloop_cancel_timeout(wpas_ctrl_radio_work_timeout,
  5026. work, NULL);
  5027. radio_work_done(work);
  5028. os_free(ework);
  5029. }
  5030. }
  5031. static void wpas_ctrl_eapol_response(void *eloop_ctx, void *timeout_ctx)
  5032. {
  5033. struct wpa_supplicant *wpa_s = eloop_ctx;
  5034. eapol_sm_notify_ctrl_response(wpa_s->eapol);
  5035. }
  5036. static int set_scan_freqs(struct wpa_supplicant *wpa_s, char *val)
  5037. {
  5038. int *freqs = NULL;
  5039. freqs = freq_range_to_channel_list(wpa_s, val);
  5040. if (freqs == NULL)
  5041. return -1;
  5042. os_free(wpa_s->manual_scan_freqs);
  5043. wpa_s->manual_scan_freqs = freqs;
  5044. return 0;
  5045. }
  5046. static int scan_id_list_parse(struct wpa_supplicant *wpa_s, const char *value)
  5047. {
  5048. const char *pos = value;
  5049. while (pos) {
  5050. if (*pos == ' ' || *pos == '\0')
  5051. break;
  5052. if (wpa_s->scan_id_count == MAX_SCAN_ID)
  5053. return -1;
  5054. wpa_s->scan_id[wpa_s->scan_id_count++] = atoi(pos);
  5055. pos = os_strchr(pos, ',');
  5056. if (pos)
  5057. pos++;
  5058. }
  5059. return 0;
  5060. }
  5061. static void wpas_ctrl_scan(struct wpa_supplicant *wpa_s, char *params,
  5062. char *reply, int reply_size, int *reply_len)
  5063. {
  5064. char *pos;
  5065. if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED) {
  5066. *reply_len = -1;
  5067. return;
  5068. }
  5069. wpa_s->manual_scan_passive = 0;
  5070. wpa_s->manual_scan_use_id = 0;
  5071. wpa_s->manual_scan_only_new = 0;
  5072. wpa_s->scan_id_count = 0;
  5073. if (params) {
  5074. if (os_strncasecmp(params, "TYPE=ONLY", 9) == 0)
  5075. wpa_s->scan_res_handler = scan_only_handler;
  5076. pos = os_strstr(params, "freq=");
  5077. if (pos && set_scan_freqs(wpa_s, pos + 5) < 0) {
  5078. *reply_len = -1;
  5079. return;
  5080. }
  5081. pos = os_strstr(params, "passive=");
  5082. if (pos)
  5083. wpa_s->manual_scan_passive = !!atoi(pos + 8);
  5084. pos = os_strstr(params, "use_id=");
  5085. if (pos)
  5086. wpa_s->manual_scan_use_id = atoi(pos + 7);
  5087. pos = os_strstr(params, "only_new=1");
  5088. if (pos)
  5089. wpa_s->manual_scan_only_new = 1;
  5090. pos = os_strstr(params, "scan_id=");
  5091. if (pos && scan_id_list_parse(wpa_s, pos + 8) < 0) {
  5092. *reply_len = -1;
  5093. return;
  5094. }
  5095. } else {
  5096. os_free(wpa_s->manual_scan_freqs);
  5097. wpa_s->manual_scan_freqs = NULL;
  5098. if (wpa_s->scan_res_handler == scan_only_handler)
  5099. wpa_s->scan_res_handler = NULL;
  5100. }
  5101. if (!wpa_s->sched_scanning && !wpa_s->scanning &&
  5102. ((wpa_s->wpa_state <= WPA_SCANNING) ||
  5103. (wpa_s->wpa_state == WPA_COMPLETED))) {
  5104. wpa_s->normal_scans = 0;
  5105. wpa_s->scan_req = MANUAL_SCAN_REQ;
  5106. wpa_s->after_wps = 0;
  5107. wpa_s->known_wps_freq = 0;
  5108. wpa_supplicant_req_scan(wpa_s, 0, 0);
  5109. if (wpa_s->manual_scan_use_id) {
  5110. wpa_s->manual_scan_id++;
  5111. wpa_dbg(wpa_s, MSG_DEBUG, "Assigned scan id %u",
  5112. wpa_s->manual_scan_id);
  5113. *reply_len = os_snprintf(reply, reply_size, "%u\n",
  5114. wpa_s->manual_scan_id);
  5115. }
  5116. } else if (wpa_s->sched_scanning) {
  5117. wpa_printf(MSG_DEBUG, "Stop ongoing sched_scan to allow requested full scan to proceed");
  5118. wpa_supplicant_cancel_sched_scan(wpa_s);
  5119. wpa_s->scan_req = MANUAL_SCAN_REQ;
  5120. wpa_supplicant_req_scan(wpa_s, 0, 0);
  5121. if (wpa_s->manual_scan_use_id) {
  5122. wpa_s->manual_scan_id++;
  5123. *reply_len = os_snprintf(reply, reply_size, "%u\n",
  5124. wpa_s->manual_scan_id);
  5125. wpa_dbg(wpa_s, MSG_DEBUG, "Assigned scan id %u",
  5126. wpa_s->manual_scan_id);
  5127. }
  5128. } else {
  5129. wpa_printf(MSG_DEBUG, "Ongoing scan action - reject new request");
  5130. *reply_len = os_snprintf(reply, reply_size, "FAIL-BUSY\n");
  5131. }
  5132. }
  5133. #ifdef CONFIG_TESTING_OPTIONS
  5134. static void wpas_ctrl_iface_mgmt_tx_cb(struct wpa_supplicant *wpa_s,
  5135. unsigned int freq, const u8 *dst,
  5136. const u8 *src, const u8 *bssid,
  5137. const u8 *data, size_t data_len,
  5138. enum offchannel_send_action_result
  5139. result)
  5140. {
  5141. wpa_msg(wpa_s, MSG_INFO, "MGMT-TX-STATUS freq=%u dst=" MACSTR
  5142. " src=" MACSTR " bssid=" MACSTR " result=%s",
  5143. freq, MAC2STR(dst), MAC2STR(src), MAC2STR(bssid),
  5144. result == OFFCHANNEL_SEND_ACTION_SUCCESS ?
  5145. "SUCCESS" : (result == OFFCHANNEL_SEND_ACTION_NO_ACK ?
  5146. "NO_ACK" : "FAILED"));
  5147. }
  5148. static int wpas_ctrl_iface_mgmt_tx(struct wpa_supplicant *wpa_s, char *cmd)
  5149. {
  5150. char *pos, *param;
  5151. size_t len;
  5152. u8 *buf, da[ETH_ALEN], bssid[ETH_ALEN];
  5153. int res, used;
  5154. int freq = 0, no_cck = 0, wait_time = 0;
  5155. /* <DA> <BSSID> [freq=<MHz>] [wait_time=<ms>] [no_cck=1]
  5156. * <action=Action frame payload> */
  5157. wpa_printf(MSG_DEBUG, "External MGMT TX: %s", cmd);
  5158. pos = cmd;
  5159. used = hwaddr_aton2(pos, da);
  5160. if (used < 0)
  5161. return -1;
  5162. pos += used;
  5163. while (*pos == ' ')
  5164. pos++;
  5165. used = hwaddr_aton2(pos, bssid);
  5166. if (used < 0)
  5167. return -1;
  5168. pos += used;
  5169. param = os_strstr(pos, " freq=");
  5170. if (param) {
  5171. param += 6;
  5172. freq = atoi(param);
  5173. }
  5174. param = os_strstr(pos, " no_cck=");
  5175. if (param) {
  5176. param += 8;
  5177. no_cck = atoi(param);
  5178. }
  5179. param = os_strstr(pos, " wait_time=");
  5180. if (param) {
  5181. param += 11;
  5182. wait_time = atoi(param);
  5183. }
  5184. param = os_strstr(pos, " action=");
  5185. if (param == NULL)
  5186. return -1;
  5187. param += 8;
  5188. len = os_strlen(param);
  5189. if (len & 1)
  5190. return -1;
  5191. len /= 2;
  5192. buf = os_malloc(len);
  5193. if (buf == NULL)
  5194. return -1;
  5195. if (hexstr2bin(param, buf, len) < 0) {
  5196. os_free(buf);
  5197. return -1;
  5198. }
  5199. res = offchannel_send_action(wpa_s, freq, da, wpa_s->own_addr, bssid,
  5200. buf, len, wait_time,
  5201. wpas_ctrl_iface_mgmt_tx_cb, no_cck);
  5202. os_free(buf);
  5203. return res;
  5204. }
  5205. static void wpas_ctrl_iface_mgmt_tx_done(struct wpa_supplicant *wpa_s)
  5206. {
  5207. wpa_printf(MSG_DEBUG, "External MGMT TX - done waiting");
  5208. offchannel_send_action_done(wpa_s);
  5209. }
  5210. static int wpas_ctrl_iface_driver_event(struct wpa_supplicant *wpa_s, char *cmd)
  5211. {
  5212. char *pos, *param;
  5213. union wpa_event_data event;
  5214. enum wpa_event_type ev;
  5215. /* <event name> [parameters..] */
  5216. wpa_dbg(wpa_s, MSG_DEBUG, "Testing - external driver event: %s", cmd);
  5217. pos = cmd;
  5218. param = os_strchr(pos, ' ');
  5219. if (param)
  5220. *param++ = '\0';
  5221. os_memset(&event, 0, sizeof(event));
  5222. if (os_strcmp(cmd, "INTERFACE_ENABLED") == 0) {
  5223. ev = EVENT_INTERFACE_ENABLED;
  5224. } else if (os_strcmp(cmd, "INTERFACE_DISABLED") == 0) {
  5225. ev = EVENT_INTERFACE_DISABLED;
  5226. } else if (os_strcmp(cmd, "AVOID_FREQUENCIES") == 0) {
  5227. ev = EVENT_AVOID_FREQUENCIES;
  5228. if (param == NULL)
  5229. param = "";
  5230. if (freq_range_list_parse(&event.freq_range, param) < 0)
  5231. return -1;
  5232. wpa_supplicant_event(wpa_s, ev, &event);
  5233. os_free(event.freq_range.range);
  5234. return 0;
  5235. } else {
  5236. wpa_dbg(wpa_s, MSG_DEBUG, "Testing - unknown driver event: %s",
  5237. cmd);
  5238. return -1;
  5239. }
  5240. wpa_supplicant_event(wpa_s, ev, &event);
  5241. return 0;
  5242. }
  5243. #endif /* CONFIG_TESTING_OPTIONS */
  5244. static void wpas_ctrl_vendor_elem_update(struct wpa_supplicant *wpa_s)
  5245. {
  5246. unsigned int i;
  5247. char buf[30];
  5248. wpa_printf(MSG_DEBUG, "Update vendor elements");
  5249. for (i = 0; i < NUM_VENDOR_ELEM_FRAMES; i++) {
  5250. if (wpa_s->vendor_elem[i]) {
  5251. os_snprintf(buf, sizeof(buf), "frame[%u]", i);
  5252. wpa_hexdump_buf(MSG_DEBUG, buf, wpa_s->vendor_elem[i]);
  5253. }
  5254. }
  5255. #ifdef CONFIG_P2P
  5256. if (wpa_s->parent == wpa_s &&
  5257. wpa_s->global->p2p &&
  5258. !wpa_s->global->p2p_disabled)
  5259. p2p_set_vendor_elems(wpa_s->global->p2p, wpa_s->vendor_elem);
  5260. #endif /* CONFIG_P2P */
  5261. }
  5262. static struct wpa_supplicant *
  5263. wpas_ctrl_vendor_elem_iface(struct wpa_supplicant *wpa_s,
  5264. enum wpa_vendor_elem_frame frame)
  5265. {
  5266. switch (frame) {
  5267. #ifdef CONFIG_P2P
  5268. case VENDOR_ELEM_PROBE_REQ_P2P:
  5269. case VENDOR_ELEM_PROBE_RESP_P2P:
  5270. case VENDOR_ELEM_PROBE_RESP_P2P_GO:
  5271. case VENDOR_ELEM_BEACON_P2P_GO:
  5272. case VENDOR_ELEM_P2P_PD_REQ:
  5273. case VENDOR_ELEM_P2P_PD_RESP:
  5274. case VENDOR_ELEM_P2P_GO_NEG_REQ:
  5275. case VENDOR_ELEM_P2P_GO_NEG_RESP:
  5276. case VENDOR_ELEM_P2P_GO_NEG_CONF:
  5277. case VENDOR_ELEM_P2P_INV_REQ:
  5278. case VENDOR_ELEM_P2P_INV_RESP:
  5279. case VENDOR_ELEM_P2P_ASSOC_REQ:
  5280. return wpa_s->parent;
  5281. #endif /* CONFIG_P2P */
  5282. default:
  5283. return wpa_s;
  5284. }
  5285. }
  5286. static int wpas_ctrl_vendor_elem_add(struct wpa_supplicant *wpa_s, char *cmd)
  5287. {
  5288. char *pos = cmd;
  5289. int frame;
  5290. size_t len;
  5291. struct wpabuf *buf;
  5292. struct ieee802_11_elems elems;
  5293. frame = atoi(pos);
  5294. if (frame < 0 || frame >= NUM_VENDOR_ELEM_FRAMES)
  5295. return -1;
  5296. wpa_s = wpas_ctrl_vendor_elem_iface(wpa_s, frame);
  5297. pos = os_strchr(pos, ' ');
  5298. if (pos == NULL)
  5299. return -1;
  5300. pos++;
  5301. len = os_strlen(pos);
  5302. if (len == 0)
  5303. return 0;
  5304. if (len & 1)
  5305. return -1;
  5306. len /= 2;
  5307. buf = wpabuf_alloc(len);
  5308. if (buf == NULL)
  5309. return -1;
  5310. if (hexstr2bin(pos, wpabuf_put(buf, len), len) < 0) {
  5311. wpabuf_free(buf);
  5312. return -1;
  5313. }
  5314. if (ieee802_11_parse_elems(wpabuf_head_u8(buf), len, &elems, 0) ==
  5315. ParseFailed) {
  5316. wpabuf_free(buf);
  5317. return -1;
  5318. }
  5319. if (wpa_s->vendor_elem[frame] == NULL) {
  5320. wpa_s->vendor_elem[frame] = buf;
  5321. wpas_ctrl_vendor_elem_update(wpa_s);
  5322. return 0;
  5323. }
  5324. if (wpabuf_resize(&wpa_s->vendor_elem[frame], len) < 0) {
  5325. wpabuf_free(buf);
  5326. return -1;
  5327. }
  5328. wpabuf_put_buf(wpa_s->vendor_elem[frame], buf);
  5329. wpabuf_free(buf);
  5330. wpas_ctrl_vendor_elem_update(wpa_s);
  5331. return 0;
  5332. }
  5333. static int wpas_ctrl_vendor_elem_get(struct wpa_supplicant *wpa_s, char *cmd,
  5334. char *buf, size_t buflen)
  5335. {
  5336. int frame = atoi(cmd);
  5337. if (frame < 0 || frame >= NUM_VENDOR_ELEM_FRAMES)
  5338. return -1;
  5339. wpa_s = wpas_ctrl_vendor_elem_iface(wpa_s, frame);
  5340. if (wpa_s->vendor_elem[frame] == NULL)
  5341. return 0;
  5342. return wpa_snprintf_hex(buf, buflen,
  5343. wpabuf_head_u8(wpa_s->vendor_elem[frame]),
  5344. wpabuf_len(wpa_s->vendor_elem[frame]));
  5345. }
  5346. static int wpas_ctrl_vendor_elem_remove(struct wpa_supplicant *wpa_s, char *cmd)
  5347. {
  5348. char *pos = cmd;
  5349. int frame;
  5350. size_t len;
  5351. u8 *buf;
  5352. struct ieee802_11_elems elems;
  5353. u8 *ie, *end;
  5354. frame = atoi(pos);
  5355. if (frame < 0 || frame >= NUM_VENDOR_ELEM_FRAMES)
  5356. return -1;
  5357. wpa_s = wpas_ctrl_vendor_elem_iface(wpa_s, frame);
  5358. pos = os_strchr(pos, ' ');
  5359. if (pos == NULL)
  5360. return -1;
  5361. pos++;
  5362. if (*pos == '*') {
  5363. wpabuf_free(wpa_s->vendor_elem[frame]);
  5364. wpa_s->vendor_elem[frame] = NULL;
  5365. wpas_ctrl_vendor_elem_update(wpa_s);
  5366. return 0;
  5367. }
  5368. if (wpa_s->vendor_elem[frame] == NULL)
  5369. return -1;
  5370. len = os_strlen(pos);
  5371. if (len == 0)
  5372. return 0;
  5373. if (len & 1)
  5374. return -1;
  5375. len /= 2;
  5376. buf = os_malloc(len);
  5377. if (buf == NULL)
  5378. return -1;
  5379. if (hexstr2bin(pos, buf, len) < 0) {
  5380. os_free(buf);
  5381. return -1;
  5382. }
  5383. if (ieee802_11_parse_elems(buf, len, &elems, 0) == ParseFailed) {
  5384. os_free(buf);
  5385. return -1;
  5386. }
  5387. ie = wpabuf_mhead_u8(wpa_s->vendor_elem[frame]);
  5388. end = ie + wpabuf_len(wpa_s->vendor_elem[frame]);
  5389. for (; ie + 1 < end; ie += 2 + ie[1]) {
  5390. if (ie + len > end)
  5391. break;
  5392. if (os_memcmp(ie, buf, len) != 0)
  5393. continue;
  5394. if (wpabuf_len(wpa_s->vendor_elem[frame]) == len) {
  5395. wpabuf_free(wpa_s->vendor_elem[frame]);
  5396. wpa_s->vendor_elem[frame] = NULL;
  5397. } else {
  5398. os_memmove(ie, ie + len,
  5399. wpabuf_len(wpa_s->vendor_elem[frame]) - len);
  5400. wpa_s->vendor_elem[frame]->used -= len;
  5401. }
  5402. os_free(buf);
  5403. wpas_ctrl_vendor_elem_update(wpa_s);
  5404. return 0;
  5405. }
  5406. os_free(buf);
  5407. return -1;
  5408. }
  5409. char * wpa_supplicant_ctrl_iface_process(struct wpa_supplicant *wpa_s,
  5410. char *buf, size_t *resp_len)
  5411. {
  5412. char *reply;
  5413. const int reply_size = 4096;
  5414. int reply_len;
  5415. if (os_strncmp(buf, WPA_CTRL_RSP, os_strlen(WPA_CTRL_RSP)) == 0 ||
  5416. os_strncmp(buf, "SET_NETWORK ", 12) == 0) {
  5417. if (wpa_debug_show_keys)
  5418. wpa_dbg(wpa_s, MSG_DEBUG,
  5419. "Control interface command '%s'", buf);
  5420. else
  5421. wpa_dbg(wpa_s, MSG_DEBUG,
  5422. "Control interface command '%s [REMOVED]'",
  5423. os_strncmp(buf, WPA_CTRL_RSP,
  5424. os_strlen(WPA_CTRL_RSP)) == 0 ?
  5425. WPA_CTRL_RSP : "SET_NETWORK");
  5426. } else if (os_strncmp(buf, "WPS_NFC_TAG_READ", 16) == 0 ||
  5427. os_strncmp(buf, "NFC_REPORT_HANDOVER", 19) == 0) {
  5428. wpa_hexdump_ascii_key(MSG_DEBUG, "RX ctrl_iface",
  5429. (const u8 *) buf, os_strlen(buf));
  5430. } else {
  5431. int level = MSG_DEBUG;
  5432. if (os_strcmp(buf, "PING") == 0)
  5433. level = MSG_EXCESSIVE;
  5434. wpa_dbg(wpa_s, level, "Control interface command '%s'", buf);
  5435. }
  5436. reply = os_malloc(reply_size);
  5437. if (reply == NULL) {
  5438. *resp_len = 1;
  5439. return NULL;
  5440. }
  5441. os_memcpy(reply, "OK\n", 3);
  5442. reply_len = 3;
  5443. if (os_strcmp(buf, "PING") == 0) {
  5444. os_memcpy(reply, "PONG\n", 5);
  5445. reply_len = 5;
  5446. } else if (os_strcmp(buf, "IFNAME") == 0) {
  5447. reply_len = os_strlen(wpa_s->ifname);
  5448. os_memcpy(reply, wpa_s->ifname, reply_len);
  5449. } else if (os_strncmp(buf, "RELOG", 5) == 0) {
  5450. if (wpa_debug_reopen_file() < 0)
  5451. reply_len = -1;
  5452. } else if (os_strncmp(buf, "NOTE ", 5) == 0) {
  5453. wpa_printf(MSG_INFO, "NOTE: %s", buf + 5);
  5454. } else if (os_strcmp(buf, "MIB") == 0) {
  5455. reply_len = wpa_sm_get_mib(wpa_s->wpa, reply, reply_size);
  5456. if (reply_len >= 0) {
  5457. int res;
  5458. res = eapol_sm_get_mib(wpa_s->eapol, reply + reply_len,
  5459. reply_size - reply_len);
  5460. if (res < 0)
  5461. reply_len = -1;
  5462. else
  5463. reply_len += res;
  5464. }
  5465. } else if (os_strncmp(buf, "STATUS", 6) == 0) {
  5466. reply_len = wpa_supplicant_ctrl_iface_status(
  5467. wpa_s, buf + 6, reply, reply_size);
  5468. } else if (os_strcmp(buf, "PMKSA") == 0) {
  5469. reply_len = wpa_sm_pmksa_cache_list(wpa_s->wpa, reply,
  5470. reply_size);
  5471. } else if (os_strncmp(buf, "SET ", 4) == 0) {
  5472. if (wpa_supplicant_ctrl_iface_set(wpa_s, buf + 4))
  5473. reply_len = -1;
  5474. } else if (os_strncmp(buf, "GET ", 4) == 0) {
  5475. reply_len = wpa_supplicant_ctrl_iface_get(wpa_s, buf + 4,
  5476. reply, reply_size);
  5477. } else if (os_strcmp(buf, "LOGON") == 0) {
  5478. eapol_sm_notify_logoff(wpa_s->eapol, FALSE);
  5479. } else if (os_strcmp(buf, "LOGOFF") == 0) {
  5480. eapol_sm_notify_logoff(wpa_s->eapol, TRUE);
  5481. } else if (os_strcmp(buf, "REASSOCIATE") == 0) {
  5482. if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED)
  5483. reply_len = -1;
  5484. else
  5485. wpas_request_connection(wpa_s);
  5486. } else if (os_strcmp(buf, "REATTACH") == 0) {
  5487. if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED ||
  5488. !wpa_s->current_ssid)
  5489. reply_len = -1;
  5490. else {
  5491. wpa_s->reattach = 1;
  5492. wpas_request_connection(wpa_s);
  5493. }
  5494. } else if (os_strcmp(buf, "RECONNECT") == 0) {
  5495. if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED)
  5496. reply_len = -1;
  5497. else if (wpa_s->disconnected)
  5498. wpas_request_connection(wpa_s);
  5499. #ifdef IEEE8021X_EAPOL
  5500. } else if (os_strncmp(buf, "PREAUTH ", 8) == 0) {
  5501. if (wpa_supplicant_ctrl_iface_preauth(wpa_s, buf + 8))
  5502. reply_len = -1;
  5503. #endif /* IEEE8021X_EAPOL */
  5504. #ifdef CONFIG_PEERKEY
  5505. } else if (os_strncmp(buf, "STKSTART ", 9) == 0) {
  5506. if (wpa_supplicant_ctrl_iface_stkstart(wpa_s, buf + 9))
  5507. reply_len = -1;
  5508. #endif /* CONFIG_PEERKEY */
  5509. #ifdef CONFIG_IEEE80211R
  5510. } else if (os_strncmp(buf, "FT_DS ", 6) == 0) {
  5511. if (wpa_supplicant_ctrl_iface_ft_ds(wpa_s, buf + 6))
  5512. reply_len = -1;
  5513. #endif /* CONFIG_IEEE80211R */
  5514. #ifdef CONFIG_WPS
  5515. } else if (os_strcmp(buf, "WPS_PBC") == 0) {
  5516. int res = wpa_supplicant_ctrl_iface_wps_pbc(wpa_s, NULL);
  5517. if (res == -2) {
  5518. os_memcpy(reply, "FAIL-PBC-OVERLAP\n", 17);
  5519. reply_len = 17;
  5520. } else if (res)
  5521. reply_len = -1;
  5522. } else if (os_strncmp(buf, "WPS_PBC ", 8) == 0) {
  5523. int res = wpa_supplicant_ctrl_iface_wps_pbc(wpa_s, buf + 8);
  5524. if (res == -2) {
  5525. os_memcpy(reply, "FAIL-PBC-OVERLAP\n", 17);
  5526. reply_len = 17;
  5527. } else if (res)
  5528. reply_len = -1;
  5529. } else if (os_strncmp(buf, "WPS_PIN ", 8) == 0) {
  5530. reply_len = wpa_supplicant_ctrl_iface_wps_pin(wpa_s, buf + 8,
  5531. reply,
  5532. reply_size);
  5533. } else if (os_strncmp(buf, "WPS_CHECK_PIN ", 14) == 0) {
  5534. reply_len = wpa_supplicant_ctrl_iface_wps_check_pin(
  5535. wpa_s, buf + 14, reply, reply_size);
  5536. } else if (os_strcmp(buf, "WPS_CANCEL") == 0) {
  5537. if (wpas_wps_cancel(wpa_s))
  5538. reply_len = -1;
  5539. #ifdef CONFIG_WPS_NFC
  5540. } else if (os_strcmp(buf, "WPS_NFC") == 0) {
  5541. if (wpa_supplicant_ctrl_iface_wps_nfc(wpa_s, NULL))
  5542. reply_len = -1;
  5543. } else if (os_strncmp(buf, "WPS_NFC ", 8) == 0) {
  5544. if (wpa_supplicant_ctrl_iface_wps_nfc(wpa_s, buf + 8))
  5545. reply_len = -1;
  5546. } else if (os_strncmp(buf, "WPS_NFC_CONFIG_TOKEN ", 21) == 0) {
  5547. reply_len = wpa_supplicant_ctrl_iface_wps_nfc_config_token(
  5548. wpa_s, buf + 21, reply, reply_size);
  5549. } else if (os_strncmp(buf, "WPS_NFC_TOKEN ", 14) == 0) {
  5550. reply_len = wpa_supplicant_ctrl_iface_wps_nfc_token(
  5551. wpa_s, buf + 14, reply, reply_size);
  5552. } else if (os_strncmp(buf, "WPS_NFC_TAG_READ ", 17) == 0) {
  5553. if (wpa_supplicant_ctrl_iface_wps_nfc_tag_read(wpa_s,
  5554. buf + 17))
  5555. reply_len = -1;
  5556. } else if (os_strncmp(buf, "NFC_GET_HANDOVER_REQ ", 21) == 0) {
  5557. reply_len = wpas_ctrl_nfc_get_handover_req(
  5558. wpa_s, buf + 21, reply, reply_size);
  5559. } else if (os_strncmp(buf, "NFC_GET_HANDOVER_SEL ", 21) == 0) {
  5560. reply_len = wpas_ctrl_nfc_get_handover_sel(
  5561. wpa_s, buf + 21, reply, reply_size);
  5562. } else if (os_strncmp(buf, "NFC_REPORT_HANDOVER ", 20) == 0) {
  5563. if (wpas_ctrl_nfc_report_handover(wpa_s, buf + 20))
  5564. reply_len = -1;
  5565. #endif /* CONFIG_WPS_NFC */
  5566. } else if (os_strncmp(buf, "WPS_REG ", 8) == 0) {
  5567. if (wpa_supplicant_ctrl_iface_wps_reg(wpa_s, buf + 8))
  5568. reply_len = -1;
  5569. #ifdef CONFIG_AP
  5570. } else if (os_strncmp(buf, "WPS_AP_PIN ", 11) == 0) {
  5571. reply_len = wpa_supplicant_ctrl_iface_wps_ap_pin(
  5572. wpa_s, buf + 11, reply, reply_size);
  5573. #endif /* CONFIG_AP */
  5574. #ifdef CONFIG_WPS_ER
  5575. } else if (os_strcmp(buf, "WPS_ER_START") == 0) {
  5576. if (wpas_wps_er_start(wpa_s, NULL))
  5577. reply_len = -1;
  5578. } else if (os_strncmp(buf, "WPS_ER_START ", 13) == 0) {
  5579. if (wpas_wps_er_start(wpa_s, buf + 13))
  5580. reply_len = -1;
  5581. } else if (os_strcmp(buf, "WPS_ER_STOP") == 0) {
  5582. if (wpas_wps_er_stop(wpa_s))
  5583. reply_len = -1;
  5584. } else if (os_strncmp(buf, "WPS_ER_PIN ", 11) == 0) {
  5585. if (wpa_supplicant_ctrl_iface_wps_er_pin(wpa_s, buf + 11))
  5586. reply_len = -1;
  5587. } else if (os_strncmp(buf, "WPS_ER_PBC ", 11) == 0) {
  5588. int ret = wpas_wps_er_pbc(wpa_s, buf + 11);
  5589. if (ret == -2) {
  5590. os_memcpy(reply, "FAIL-PBC-OVERLAP\n", 17);
  5591. reply_len = 17;
  5592. } else if (ret == -3) {
  5593. os_memcpy(reply, "FAIL-UNKNOWN-UUID\n", 18);
  5594. reply_len = 18;
  5595. } else if (ret == -4) {
  5596. os_memcpy(reply, "FAIL-NO-AP-SETTINGS\n", 20);
  5597. reply_len = 20;
  5598. } else if (ret)
  5599. reply_len = -1;
  5600. } else if (os_strncmp(buf, "WPS_ER_LEARN ", 13) == 0) {
  5601. if (wpa_supplicant_ctrl_iface_wps_er_learn(wpa_s, buf + 13))
  5602. reply_len = -1;
  5603. } else if (os_strncmp(buf, "WPS_ER_SET_CONFIG ", 18) == 0) {
  5604. if (wpa_supplicant_ctrl_iface_wps_er_set_config(wpa_s,
  5605. buf + 18))
  5606. reply_len = -1;
  5607. } else if (os_strncmp(buf, "WPS_ER_CONFIG ", 14) == 0) {
  5608. if (wpa_supplicant_ctrl_iface_wps_er_config(wpa_s, buf + 14))
  5609. reply_len = -1;
  5610. #ifdef CONFIG_WPS_NFC
  5611. } else if (os_strncmp(buf, "WPS_ER_NFC_CONFIG_TOKEN ", 24) == 0) {
  5612. reply_len = wpa_supplicant_ctrl_iface_wps_er_nfc_config_token(
  5613. wpa_s, buf + 24, reply, reply_size);
  5614. #endif /* CONFIG_WPS_NFC */
  5615. #endif /* CONFIG_WPS_ER */
  5616. #endif /* CONFIG_WPS */
  5617. #ifdef CONFIG_IBSS_RSN
  5618. } else if (os_strncmp(buf, "IBSS_RSN ", 9) == 0) {
  5619. if (wpa_supplicant_ctrl_iface_ibss_rsn(wpa_s, buf + 9))
  5620. reply_len = -1;
  5621. #endif /* CONFIG_IBSS_RSN */
  5622. #ifdef CONFIG_P2P
  5623. } else if (os_strncmp(buf, "P2P_FIND ", 9) == 0) {
  5624. if (p2p_ctrl_find(wpa_s, buf + 9))
  5625. reply_len = -1;
  5626. } else if (os_strcmp(buf, "P2P_FIND") == 0) {
  5627. if (p2p_ctrl_find(wpa_s, ""))
  5628. reply_len = -1;
  5629. } else if (os_strcmp(buf, "P2P_STOP_FIND") == 0) {
  5630. wpas_p2p_stop_find(wpa_s);
  5631. } else if (os_strncmp(buf, "P2P_CONNECT ", 12) == 0) {
  5632. reply_len = p2p_ctrl_connect(wpa_s, buf + 12, reply,
  5633. reply_size);
  5634. } else if (os_strncmp(buf, "P2P_LISTEN ", 11) == 0) {
  5635. if (p2p_ctrl_listen(wpa_s, buf + 11))
  5636. reply_len = -1;
  5637. } else if (os_strcmp(buf, "P2P_LISTEN") == 0) {
  5638. if (p2p_ctrl_listen(wpa_s, ""))
  5639. reply_len = -1;
  5640. } else if (os_strncmp(buf, "P2P_GROUP_REMOVE ", 17) == 0) {
  5641. if (wpas_p2p_group_remove(wpa_s, buf + 17))
  5642. reply_len = -1;
  5643. } else if (os_strcmp(buf, "P2P_GROUP_ADD") == 0) {
  5644. if (wpas_p2p_group_add(wpa_s, 0, 0, 0, 0))
  5645. reply_len = -1;
  5646. } else if (os_strncmp(buf, "P2P_GROUP_ADD ", 14) == 0) {
  5647. if (p2p_ctrl_group_add(wpa_s, buf + 14))
  5648. reply_len = -1;
  5649. } else if (os_strncmp(buf, "P2P_PROV_DISC ", 14) == 0) {
  5650. if (p2p_ctrl_prov_disc(wpa_s, buf + 14))
  5651. reply_len = -1;
  5652. } else if (os_strcmp(buf, "P2P_GET_PASSPHRASE") == 0) {
  5653. reply_len = p2p_get_passphrase(wpa_s, reply, reply_size);
  5654. } else if (os_strncmp(buf, "P2P_SERV_DISC_REQ ", 18) == 0) {
  5655. reply_len = p2p_ctrl_serv_disc_req(wpa_s, buf + 18, reply,
  5656. reply_size);
  5657. } else if (os_strncmp(buf, "P2P_SERV_DISC_CANCEL_REQ ", 25) == 0) {
  5658. if (p2p_ctrl_serv_disc_cancel_req(wpa_s, buf + 25) < 0)
  5659. reply_len = -1;
  5660. } else if (os_strncmp(buf, "P2P_SERV_DISC_RESP ", 19) == 0) {
  5661. if (p2p_ctrl_serv_disc_resp(wpa_s, buf + 19) < 0)
  5662. reply_len = -1;
  5663. } else if (os_strcmp(buf, "P2P_SERVICE_UPDATE") == 0) {
  5664. wpas_p2p_sd_service_update(wpa_s);
  5665. } else if (os_strncmp(buf, "P2P_SERV_DISC_EXTERNAL ", 23) == 0) {
  5666. if (p2p_ctrl_serv_disc_external(wpa_s, buf + 23) < 0)
  5667. reply_len = -1;
  5668. } else if (os_strcmp(buf, "P2P_SERVICE_FLUSH") == 0) {
  5669. wpas_p2p_service_flush(wpa_s);
  5670. } else if (os_strncmp(buf, "P2P_SERVICE_ADD ", 16) == 0) {
  5671. if (p2p_ctrl_service_add(wpa_s, buf + 16) < 0)
  5672. reply_len = -1;
  5673. } else if (os_strncmp(buf, "P2P_SERVICE_DEL ", 16) == 0) {
  5674. if (p2p_ctrl_service_del(wpa_s, buf + 16) < 0)
  5675. reply_len = -1;
  5676. } else if (os_strncmp(buf, "P2P_REJECT ", 11) == 0) {
  5677. if (p2p_ctrl_reject(wpa_s, buf + 11) < 0)
  5678. reply_len = -1;
  5679. } else if (os_strncmp(buf, "P2P_INVITE ", 11) == 0) {
  5680. if (p2p_ctrl_invite(wpa_s, buf + 11) < 0)
  5681. reply_len = -1;
  5682. } else if (os_strncmp(buf, "P2P_PEER ", 9) == 0) {
  5683. reply_len = p2p_ctrl_peer(wpa_s, buf + 9, reply,
  5684. reply_size);
  5685. } else if (os_strncmp(buf, "P2P_SET ", 8) == 0) {
  5686. if (p2p_ctrl_set(wpa_s, buf + 8) < 0)
  5687. reply_len = -1;
  5688. } else if (os_strcmp(buf, "P2P_FLUSH") == 0) {
  5689. p2p_ctrl_flush(wpa_s);
  5690. } else if (os_strncmp(buf, "P2P_UNAUTHORIZE ", 16) == 0) {
  5691. if (wpas_p2p_unauthorize(wpa_s, buf + 16) < 0)
  5692. reply_len = -1;
  5693. } else if (os_strcmp(buf, "P2P_CANCEL") == 0) {
  5694. if (wpas_p2p_cancel(wpa_s))
  5695. reply_len = -1;
  5696. } else if (os_strncmp(buf, "P2P_PRESENCE_REQ ", 17) == 0) {
  5697. if (p2p_ctrl_presence_req(wpa_s, buf + 17) < 0)
  5698. reply_len = -1;
  5699. } else if (os_strcmp(buf, "P2P_PRESENCE_REQ") == 0) {
  5700. if (p2p_ctrl_presence_req(wpa_s, "") < 0)
  5701. reply_len = -1;
  5702. } else if (os_strncmp(buf, "P2P_EXT_LISTEN ", 15) == 0) {
  5703. if (p2p_ctrl_ext_listen(wpa_s, buf + 15) < 0)
  5704. reply_len = -1;
  5705. } else if (os_strcmp(buf, "P2P_EXT_LISTEN") == 0) {
  5706. if (p2p_ctrl_ext_listen(wpa_s, "") < 0)
  5707. reply_len = -1;
  5708. } else if (os_strncmp(buf, "P2P_REMOVE_CLIENT ", 18) == 0) {
  5709. if (p2p_ctrl_remove_client(wpa_s, buf + 18) < 0)
  5710. reply_len = -1;
  5711. #endif /* CONFIG_P2P */
  5712. #ifdef CONFIG_WIFI_DISPLAY
  5713. } else if (os_strncmp(buf, "WFD_SUBELEM_SET ", 16) == 0) {
  5714. if (wifi_display_subelem_set(wpa_s->global, buf + 16) < 0)
  5715. reply_len = -1;
  5716. } else if (os_strncmp(buf, "WFD_SUBELEM_GET ", 16) == 0) {
  5717. reply_len = wifi_display_subelem_get(wpa_s->global, buf + 16,
  5718. reply, reply_size);
  5719. #endif /* CONFIG_WIFI_DISPLAY */
  5720. #ifdef CONFIG_INTERWORKING
  5721. } else if (os_strcmp(buf, "FETCH_ANQP") == 0) {
  5722. if (interworking_fetch_anqp(wpa_s) < 0)
  5723. reply_len = -1;
  5724. } else if (os_strcmp(buf, "STOP_FETCH_ANQP") == 0) {
  5725. interworking_stop_fetch_anqp(wpa_s);
  5726. } else if (os_strcmp(buf, "INTERWORKING_SELECT") == 0) {
  5727. if (ctrl_interworking_select(wpa_s, NULL) < 0)
  5728. reply_len = -1;
  5729. } else if (os_strncmp(buf, "INTERWORKING_SELECT ", 20) == 0) {
  5730. if (ctrl_interworking_select(wpa_s, buf + 20) < 0)
  5731. reply_len = -1;
  5732. } else if (os_strncmp(buf, "INTERWORKING_CONNECT ", 21) == 0) {
  5733. if (ctrl_interworking_connect(wpa_s, buf + 21) < 0)
  5734. reply_len = -1;
  5735. } else if (os_strncmp(buf, "ANQP_GET ", 9) == 0) {
  5736. if (get_anqp(wpa_s, buf + 9) < 0)
  5737. reply_len = -1;
  5738. } else if (os_strncmp(buf, "GAS_REQUEST ", 12) == 0) {
  5739. if (gas_request(wpa_s, buf + 12) < 0)
  5740. reply_len = -1;
  5741. } else if (os_strncmp(buf, "GAS_RESPONSE_GET ", 17) == 0) {
  5742. reply_len = gas_response_get(wpa_s, buf + 17, reply,
  5743. reply_size);
  5744. #endif /* CONFIG_INTERWORKING */
  5745. #ifdef CONFIG_HS20
  5746. } else if (os_strncmp(buf, "HS20_ANQP_GET ", 14) == 0) {
  5747. if (get_hs20_anqp(wpa_s, buf + 14) < 0)
  5748. reply_len = -1;
  5749. } else if (os_strncmp(buf, "HS20_GET_NAI_HOME_REALM_LIST ", 29) == 0) {
  5750. if (hs20_get_nai_home_realm_list(wpa_s, buf + 29) < 0)
  5751. reply_len = -1;
  5752. } else if (os_strncmp(buf, "HS20_ICON_REQUEST ", 18) == 0) {
  5753. if (hs20_icon_request(wpa_s, buf + 18) < 0)
  5754. reply_len = -1;
  5755. } else if (os_strcmp(buf, "FETCH_OSU") == 0) {
  5756. if (hs20_fetch_osu(wpa_s) < 0)
  5757. reply_len = -1;
  5758. } else if (os_strcmp(buf, "CANCEL_FETCH_OSU") == 0) {
  5759. hs20_cancel_fetch_osu(wpa_s);
  5760. #endif /* CONFIG_HS20 */
  5761. } else if (os_strncmp(buf, WPA_CTRL_RSP, os_strlen(WPA_CTRL_RSP)) == 0)
  5762. {
  5763. if (wpa_supplicant_ctrl_iface_ctrl_rsp(
  5764. wpa_s, buf + os_strlen(WPA_CTRL_RSP)))
  5765. reply_len = -1;
  5766. else {
  5767. /*
  5768. * Notify response from timeout to allow the control
  5769. * interface response to be sent first.
  5770. */
  5771. eloop_register_timeout(0, 0, wpas_ctrl_eapol_response,
  5772. wpa_s, NULL);
  5773. }
  5774. } else if (os_strcmp(buf, "RECONFIGURE") == 0) {
  5775. if (wpa_supplicant_reload_configuration(wpa_s))
  5776. reply_len = -1;
  5777. } else if (os_strcmp(buf, "TERMINATE") == 0) {
  5778. wpa_supplicant_terminate_proc(wpa_s->global);
  5779. } else if (os_strncmp(buf, "BSSID ", 6) == 0) {
  5780. if (wpa_supplicant_ctrl_iface_bssid(wpa_s, buf + 6))
  5781. reply_len = -1;
  5782. } else if (os_strncmp(buf, "BLACKLIST", 9) == 0) {
  5783. reply_len = wpa_supplicant_ctrl_iface_blacklist(
  5784. wpa_s, buf + 9, reply, reply_size);
  5785. } else if (os_strncmp(buf, "LOG_LEVEL", 9) == 0) {
  5786. reply_len = wpa_supplicant_ctrl_iface_log_level(
  5787. wpa_s, buf + 9, reply, reply_size);
  5788. } else if (os_strcmp(buf, "LIST_NETWORKS") == 0) {
  5789. reply_len = wpa_supplicant_ctrl_iface_list_networks(
  5790. wpa_s, reply, reply_size);
  5791. } else if (os_strcmp(buf, "DISCONNECT") == 0) {
  5792. #ifdef CONFIG_SME
  5793. wpa_s->sme.prev_bssid_set = 0;
  5794. #endif /* CONFIG_SME */
  5795. wpa_s->reassociate = 0;
  5796. wpa_s->disconnected = 1;
  5797. wpa_supplicant_cancel_sched_scan(wpa_s);
  5798. wpa_supplicant_cancel_scan(wpa_s);
  5799. wpa_supplicant_deauthenticate(wpa_s,
  5800. WLAN_REASON_DEAUTH_LEAVING);
  5801. } else if (os_strcmp(buf, "SCAN") == 0) {
  5802. wpas_ctrl_scan(wpa_s, NULL, reply, reply_size, &reply_len);
  5803. } else if (os_strncmp(buf, "SCAN ", 5) == 0) {
  5804. wpas_ctrl_scan(wpa_s, buf + 5, reply, reply_size, &reply_len);
  5805. } else if (os_strcmp(buf, "SCAN_RESULTS") == 0) {
  5806. reply_len = wpa_supplicant_ctrl_iface_scan_results(
  5807. wpa_s, reply, reply_size);
  5808. } else if (os_strncmp(buf, "SELECT_NETWORK ", 15) == 0) {
  5809. if (wpa_supplicant_ctrl_iface_select_network(wpa_s, buf + 15))
  5810. reply_len = -1;
  5811. } else if (os_strncmp(buf, "ENABLE_NETWORK ", 15) == 0) {
  5812. if (wpa_supplicant_ctrl_iface_enable_network(wpa_s, buf + 15))
  5813. reply_len = -1;
  5814. } else if (os_strncmp(buf, "DISABLE_NETWORK ", 16) == 0) {
  5815. if (wpa_supplicant_ctrl_iface_disable_network(wpa_s, buf + 16))
  5816. reply_len = -1;
  5817. } else if (os_strcmp(buf, "ADD_NETWORK") == 0) {
  5818. reply_len = wpa_supplicant_ctrl_iface_add_network(
  5819. wpa_s, reply, reply_size);
  5820. } else if (os_strncmp(buf, "REMOVE_NETWORK ", 15) == 0) {
  5821. if (wpa_supplicant_ctrl_iface_remove_network(wpa_s, buf + 15))
  5822. reply_len = -1;
  5823. } else if (os_strncmp(buf, "SET_NETWORK ", 12) == 0) {
  5824. if (wpa_supplicant_ctrl_iface_set_network(wpa_s, buf + 12))
  5825. reply_len = -1;
  5826. } else if (os_strncmp(buf, "GET_NETWORK ", 12) == 0) {
  5827. reply_len = wpa_supplicant_ctrl_iface_get_network(
  5828. wpa_s, buf + 12, reply, reply_size);
  5829. } else if (os_strncmp(buf, "DUP_NETWORK ", 12) == 0) {
  5830. if (wpa_supplicant_ctrl_iface_dup_network(wpa_s, buf + 12))
  5831. reply_len = -1;
  5832. } else if (os_strcmp(buf, "LIST_CREDS") == 0) {
  5833. reply_len = wpa_supplicant_ctrl_iface_list_creds(
  5834. wpa_s, reply, reply_size);
  5835. } else if (os_strcmp(buf, "ADD_CRED") == 0) {
  5836. reply_len = wpa_supplicant_ctrl_iface_add_cred(
  5837. wpa_s, reply, reply_size);
  5838. } else if (os_strncmp(buf, "REMOVE_CRED ", 12) == 0) {
  5839. if (wpa_supplicant_ctrl_iface_remove_cred(wpa_s, buf + 12))
  5840. reply_len = -1;
  5841. } else if (os_strncmp(buf, "SET_CRED ", 9) == 0) {
  5842. if (wpa_supplicant_ctrl_iface_set_cred(wpa_s, buf + 9))
  5843. reply_len = -1;
  5844. } else if (os_strncmp(buf, "GET_CRED ", 9) == 0) {
  5845. reply_len = wpa_supplicant_ctrl_iface_get_cred(wpa_s, buf + 9,
  5846. reply,
  5847. reply_size);
  5848. #ifndef CONFIG_NO_CONFIG_WRITE
  5849. } else if (os_strcmp(buf, "SAVE_CONFIG") == 0) {
  5850. if (wpa_supplicant_ctrl_iface_save_config(wpa_s))
  5851. reply_len = -1;
  5852. #endif /* CONFIG_NO_CONFIG_WRITE */
  5853. } else if (os_strncmp(buf, "GET_CAPABILITY ", 15) == 0) {
  5854. reply_len = wpa_supplicant_ctrl_iface_get_capability(
  5855. wpa_s, buf + 15, reply, reply_size);
  5856. } else if (os_strncmp(buf, "AP_SCAN ", 8) == 0) {
  5857. if (wpa_supplicant_ctrl_iface_ap_scan(wpa_s, buf + 8))
  5858. reply_len = -1;
  5859. } else if (os_strncmp(buf, "SCAN_INTERVAL ", 14) == 0) {
  5860. if (wpa_supplicant_ctrl_iface_scan_interval(wpa_s, buf + 14))
  5861. reply_len = -1;
  5862. } else if (os_strcmp(buf, "INTERFACE_LIST") == 0) {
  5863. reply_len = wpa_supplicant_global_iface_list(
  5864. wpa_s->global, reply, reply_size);
  5865. } else if (os_strcmp(buf, "INTERFACES") == 0) {
  5866. reply_len = wpa_supplicant_global_iface_interfaces(
  5867. wpa_s->global, reply, reply_size);
  5868. } else if (os_strncmp(buf, "BSS ", 4) == 0) {
  5869. reply_len = wpa_supplicant_ctrl_iface_bss(
  5870. wpa_s, buf + 4, reply, reply_size);
  5871. #ifdef CONFIG_AP
  5872. } else if (os_strcmp(buf, "STA-FIRST") == 0) {
  5873. reply_len = ap_ctrl_iface_sta_first(wpa_s, reply, reply_size);
  5874. } else if (os_strncmp(buf, "STA ", 4) == 0) {
  5875. reply_len = ap_ctrl_iface_sta(wpa_s, buf + 4, reply,
  5876. reply_size);
  5877. } else if (os_strncmp(buf, "STA-NEXT ", 9) == 0) {
  5878. reply_len = ap_ctrl_iface_sta_next(wpa_s, buf + 9, reply,
  5879. reply_size);
  5880. } else if (os_strncmp(buf, "DEAUTHENTICATE ", 15) == 0) {
  5881. if (ap_ctrl_iface_sta_deauthenticate(wpa_s, buf + 15))
  5882. reply_len = -1;
  5883. } else if (os_strncmp(buf, "DISASSOCIATE ", 13) == 0) {
  5884. if (ap_ctrl_iface_sta_disassociate(wpa_s, buf + 13))
  5885. reply_len = -1;
  5886. } else if (os_strncmp(buf, "CHAN_SWITCH ", 12) == 0) {
  5887. if (ap_ctrl_iface_chanswitch(wpa_s, buf + 12))
  5888. reply_len = -1;
  5889. #endif /* CONFIG_AP */
  5890. } else if (os_strcmp(buf, "SUSPEND") == 0) {
  5891. wpas_notify_suspend(wpa_s->global);
  5892. } else if (os_strcmp(buf, "RESUME") == 0) {
  5893. wpas_notify_resume(wpa_s->global);
  5894. #ifdef CONFIG_TESTING_OPTIONS
  5895. } else if (os_strcmp(buf, "DROP_SA") == 0) {
  5896. wpa_supplicant_ctrl_iface_drop_sa(wpa_s);
  5897. #endif /* CONFIG_TESTING_OPTIONS */
  5898. } else if (os_strncmp(buf, "ROAM ", 5) == 0) {
  5899. if (wpa_supplicant_ctrl_iface_roam(wpa_s, buf + 5))
  5900. reply_len = -1;
  5901. } else if (os_strncmp(buf, "STA_AUTOCONNECT ", 16) == 0) {
  5902. if (wpa_supplicant_ctrl_iface_sta_autoconnect(wpa_s, buf + 16))
  5903. reply_len = -1;
  5904. } else if (os_strncmp(buf, "BSS_EXPIRE_AGE ", 15) == 0) {
  5905. if (wpa_supplicant_ctrl_iface_bss_expire_age(wpa_s, buf + 15))
  5906. reply_len = -1;
  5907. } else if (os_strncmp(buf, "BSS_EXPIRE_COUNT ", 17) == 0) {
  5908. if (wpa_supplicant_ctrl_iface_bss_expire_count(wpa_s,
  5909. buf + 17))
  5910. reply_len = -1;
  5911. } else if (os_strncmp(buf, "BSS_FLUSH ", 10) == 0) {
  5912. if (wpa_supplicant_ctrl_iface_bss_flush(wpa_s, buf + 10))
  5913. reply_len = -1;
  5914. #ifdef CONFIG_TDLS
  5915. } else if (os_strncmp(buf, "TDLS_DISCOVER ", 14) == 0) {
  5916. if (wpa_supplicant_ctrl_iface_tdls_discover(wpa_s, buf + 14))
  5917. reply_len = -1;
  5918. } else if (os_strncmp(buf, "TDLS_SETUP ", 11) == 0) {
  5919. if (wpa_supplicant_ctrl_iface_tdls_setup(wpa_s, buf + 11))
  5920. reply_len = -1;
  5921. } else if (os_strncmp(buf, "TDLS_TEARDOWN ", 14) == 0) {
  5922. if (wpa_supplicant_ctrl_iface_tdls_teardown(wpa_s, buf + 14))
  5923. reply_len = -1;
  5924. #endif /* CONFIG_TDLS */
  5925. } else if (os_strncmp(buf, "SIGNAL_POLL", 11) == 0) {
  5926. reply_len = wpa_supplicant_signal_poll(wpa_s, reply,
  5927. reply_size);
  5928. } else if (os_strncmp(buf, "PKTCNT_POLL", 11) == 0) {
  5929. reply_len = wpa_supplicant_pktcnt_poll(wpa_s, reply,
  5930. reply_size);
  5931. #ifdef CONFIG_AUTOSCAN
  5932. } else if (os_strncmp(buf, "AUTOSCAN ", 9) == 0) {
  5933. if (wpa_supplicant_ctrl_iface_autoscan(wpa_s, buf + 9))
  5934. reply_len = -1;
  5935. #endif /* CONFIG_AUTOSCAN */
  5936. #ifdef ANDROID
  5937. } else if (os_strncmp(buf, "DRIVER ", 7) == 0) {
  5938. reply_len = wpa_supplicant_driver_cmd(wpa_s, buf + 7, reply,
  5939. reply_size);
  5940. #endif /* ANDROID */
  5941. } else if (os_strncmp(buf, "VENDOR ", 7) == 0) {
  5942. reply_len = wpa_supplicant_vendor_cmd(wpa_s, buf + 7, reply,
  5943. reply_size);
  5944. } else if (os_strcmp(buf, "REAUTHENTICATE") == 0) {
  5945. pmksa_cache_clear_current(wpa_s->wpa);
  5946. eapol_sm_request_reauth(wpa_s->eapol);
  5947. #ifdef CONFIG_WNM
  5948. } else if (os_strncmp(buf, "WNM_SLEEP ", 10) == 0) {
  5949. if (wpas_ctrl_iface_wnm_sleep(wpa_s, buf + 10))
  5950. reply_len = -1;
  5951. } else if (os_strncmp(buf, "WNM_BSS_QUERY ", 10) == 0) {
  5952. if (wpas_ctrl_iface_wnm_bss_query(wpa_s, buf + 10))
  5953. reply_len = -1;
  5954. #endif /* CONFIG_WNM */
  5955. } else if (os_strcmp(buf, "FLUSH") == 0) {
  5956. wpa_supplicant_ctrl_iface_flush(wpa_s);
  5957. } else if (os_strncmp(buf, "RADIO_WORK ", 11) == 0) {
  5958. reply_len = wpas_ctrl_radio_work(wpa_s, buf + 11, reply,
  5959. reply_size);
  5960. #ifdef CONFIG_TESTING_OPTIONS
  5961. } else if (os_strncmp(buf, "MGMT_TX ", 8) == 0) {
  5962. if (wpas_ctrl_iface_mgmt_tx(wpa_s, buf + 8) < 0)
  5963. reply_len = -1;
  5964. } else if (os_strcmp(buf, "MGMT_TX_DONE") == 0) {
  5965. wpas_ctrl_iface_mgmt_tx_done(wpa_s);
  5966. } else if (os_strncmp(buf, "DRIVER_EVENT ", 13) == 0) {
  5967. if (wpas_ctrl_iface_driver_event(wpa_s, buf + 13) < 0)
  5968. reply_len = -1;
  5969. #endif /* CONFIG_TESTING_OPTIONS */
  5970. } else if (os_strncmp(buf, "VENDOR_ELEM_ADD ", 16) == 0) {
  5971. if (wpas_ctrl_vendor_elem_add(wpa_s, buf + 16) < 0)
  5972. reply_len = -1;
  5973. } else if (os_strncmp(buf, "VENDOR_ELEM_GET ", 16) == 0) {
  5974. reply_len = wpas_ctrl_vendor_elem_get(wpa_s, buf + 16, reply,
  5975. reply_size);
  5976. } else if (os_strncmp(buf, "VENDOR_ELEM_REMOVE ", 19) == 0) {
  5977. if (wpas_ctrl_vendor_elem_remove(wpa_s, buf + 19) < 0)
  5978. reply_len = -1;
  5979. } else {
  5980. os_memcpy(reply, "UNKNOWN COMMAND\n", 16);
  5981. reply_len = 16;
  5982. }
  5983. if (reply_len < 0) {
  5984. os_memcpy(reply, "FAIL\n", 5);
  5985. reply_len = 5;
  5986. }
  5987. *resp_len = reply_len;
  5988. return reply;
  5989. }
  5990. static int wpa_supplicant_global_iface_add(struct wpa_global *global,
  5991. char *cmd)
  5992. {
  5993. struct wpa_interface iface;
  5994. char *pos;
  5995. /*
  5996. * <ifname>TAB<confname>TAB<driver>TAB<ctrl_interface>TAB<driver_param>
  5997. * TAB<bridge_ifname>
  5998. */
  5999. wpa_printf(MSG_DEBUG, "CTRL_IFACE GLOBAL INTERFACE_ADD '%s'", cmd);
  6000. os_memset(&iface, 0, sizeof(iface));
  6001. do {
  6002. iface.ifname = pos = cmd;
  6003. pos = os_strchr(pos, '\t');
  6004. if (pos)
  6005. *pos++ = '\0';
  6006. if (iface.ifname[0] == '\0')
  6007. return -1;
  6008. if (pos == NULL)
  6009. break;
  6010. iface.confname = pos;
  6011. pos = os_strchr(pos, '\t');
  6012. if (pos)
  6013. *pos++ = '\0';
  6014. if (iface.confname[0] == '\0')
  6015. iface.confname = NULL;
  6016. if (pos == NULL)
  6017. break;
  6018. iface.driver = pos;
  6019. pos = os_strchr(pos, '\t');
  6020. if (pos)
  6021. *pos++ = '\0';
  6022. if (iface.driver[0] == '\0')
  6023. iface.driver = NULL;
  6024. if (pos == NULL)
  6025. break;
  6026. iface.ctrl_interface = pos;
  6027. pos = os_strchr(pos, '\t');
  6028. if (pos)
  6029. *pos++ = '\0';
  6030. if (iface.ctrl_interface[0] == '\0')
  6031. iface.ctrl_interface = NULL;
  6032. if (pos == NULL)
  6033. break;
  6034. iface.driver_param = pos;
  6035. pos = os_strchr(pos, '\t');
  6036. if (pos)
  6037. *pos++ = '\0';
  6038. if (iface.driver_param[0] == '\0')
  6039. iface.driver_param = NULL;
  6040. if (pos == NULL)
  6041. break;
  6042. iface.bridge_ifname = pos;
  6043. pos = os_strchr(pos, '\t');
  6044. if (pos)
  6045. *pos++ = '\0';
  6046. if (iface.bridge_ifname[0] == '\0')
  6047. iface.bridge_ifname = NULL;
  6048. if (pos == NULL)
  6049. break;
  6050. } while (0);
  6051. if (wpa_supplicant_get_iface(global, iface.ifname))
  6052. return -1;
  6053. return wpa_supplicant_add_iface(global, &iface) ? 0 : -1;
  6054. }
  6055. static int wpa_supplicant_global_iface_remove(struct wpa_global *global,
  6056. char *cmd)
  6057. {
  6058. struct wpa_supplicant *wpa_s;
  6059. wpa_printf(MSG_DEBUG, "CTRL_IFACE GLOBAL INTERFACE_REMOVE '%s'", cmd);
  6060. wpa_s = wpa_supplicant_get_iface(global, cmd);
  6061. if (wpa_s == NULL)
  6062. return -1;
  6063. return wpa_supplicant_remove_iface(global, wpa_s, 0);
  6064. }
  6065. static void wpa_free_iface_info(struct wpa_interface_info *iface)
  6066. {
  6067. struct wpa_interface_info *prev;
  6068. while (iface) {
  6069. prev = iface;
  6070. iface = iface->next;
  6071. os_free(prev->ifname);
  6072. os_free(prev->desc);
  6073. os_free(prev);
  6074. }
  6075. }
  6076. static int wpa_supplicant_global_iface_list(struct wpa_global *global,
  6077. char *buf, int len)
  6078. {
  6079. int i, res;
  6080. struct wpa_interface_info *iface = NULL, *last = NULL, *tmp;
  6081. char *pos, *end;
  6082. for (i = 0; wpa_drivers[i]; i++) {
  6083. struct wpa_driver_ops *drv = wpa_drivers[i];
  6084. if (drv->get_interfaces == NULL)
  6085. continue;
  6086. tmp = drv->get_interfaces(global->drv_priv[i]);
  6087. if (tmp == NULL)
  6088. continue;
  6089. if (last == NULL)
  6090. iface = last = tmp;
  6091. else
  6092. last->next = tmp;
  6093. while (last->next)
  6094. last = last->next;
  6095. }
  6096. pos = buf;
  6097. end = buf + len;
  6098. for (tmp = iface; tmp; tmp = tmp->next) {
  6099. res = os_snprintf(pos, end - pos, "%s\t%s\t%s\n",
  6100. tmp->drv_name, tmp->ifname,
  6101. tmp->desc ? tmp->desc : "");
  6102. if (res < 0 || res >= end - pos) {
  6103. *pos = '\0';
  6104. break;
  6105. }
  6106. pos += res;
  6107. }
  6108. wpa_free_iface_info(iface);
  6109. return pos - buf;
  6110. }
  6111. static int wpa_supplicant_global_iface_interfaces(struct wpa_global *global,
  6112. char *buf, int len)
  6113. {
  6114. int res;
  6115. char *pos, *end;
  6116. struct wpa_supplicant *wpa_s;
  6117. wpa_s = global->ifaces;
  6118. pos = buf;
  6119. end = buf + len;
  6120. while (wpa_s) {
  6121. res = os_snprintf(pos, end - pos, "%s\n", wpa_s->ifname);
  6122. if (res < 0 || res >= end - pos) {
  6123. *pos = '\0';
  6124. break;
  6125. }
  6126. pos += res;
  6127. wpa_s = wpa_s->next;
  6128. }
  6129. return pos - buf;
  6130. }
  6131. static char * wpas_global_ctrl_iface_ifname(struct wpa_global *global,
  6132. const char *ifname,
  6133. char *cmd, size_t *resp_len)
  6134. {
  6135. struct wpa_supplicant *wpa_s;
  6136. for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
  6137. if (os_strcmp(ifname, wpa_s->ifname) == 0)
  6138. break;
  6139. }
  6140. if (wpa_s == NULL) {
  6141. char *resp = os_strdup("FAIL-NO-IFNAME-MATCH\n");
  6142. if (resp)
  6143. *resp_len = os_strlen(resp);
  6144. else
  6145. *resp_len = 1;
  6146. return resp;
  6147. }
  6148. return wpa_supplicant_ctrl_iface_process(wpa_s, cmd, resp_len);
  6149. }
  6150. static char * wpas_global_ctrl_iface_redir_p2p(struct wpa_global *global,
  6151. char *buf, size_t *resp_len)
  6152. {
  6153. #ifdef CONFIG_P2P
  6154. static const char * cmd[] = {
  6155. "LIST_NETWORKS",
  6156. "P2P_FIND",
  6157. "P2P_STOP_FIND",
  6158. "P2P_LISTEN",
  6159. "P2P_GROUP_ADD",
  6160. "P2P_GET_PASSPHRASE",
  6161. "P2P_SERVICE_UPDATE",
  6162. "P2P_SERVICE_FLUSH",
  6163. "P2P_FLUSH",
  6164. "P2P_CANCEL",
  6165. "P2P_PRESENCE_REQ",
  6166. "P2P_EXT_LISTEN",
  6167. NULL
  6168. };
  6169. static const char * prefix[] = {
  6170. #ifdef ANDROID
  6171. "DRIVER ",
  6172. #endif /* ANDROID */
  6173. "GET_NETWORK ",
  6174. "REMOVE_NETWORK ",
  6175. "P2P_FIND ",
  6176. "P2P_CONNECT ",
  6177. "P2P_LISTEN ",
  6178. "P2P_GROUP_REMOVE ",
  6179. "P2P_GROUP_ADD ",
  6180. "P2P_PROV_DISC ",
  6181. "P2P_SERV_DISC_REQ ",
  6182. "P2P_SERV_DISC_CANCEL_REQ ",
  6183. "P2P_SERV_DISC_RESP ",
  6184. "P2P_SERV_DISC_EXTERNAL ",
  6185. "P2P_SERVICE_ADD ",
  6186. "P2P_SERVICE_DEL ",
  6187. "P2P_REJECT ",
  6188. "P2P_INVITE ",
  6189. "P2P_PEER ",
  6190. "P2P_SET ",
  6191. "P2P_UNAUTHORIZE ",
  6192. "P2P_PRESENCE_REQ ",
  6193. "P2P_EXT_LISTEN ",
  6194. "P2P_REMOVE_CLIENT ",
  6195. "NFC_GET_HANDOVER_SEL ",
  6196. "NFC_GET_HANDOVER_REQ ",
  6197. "NFC_REPORT_HANDOVER ",
  6198. NULL
  6199. };
  6200. int found = 0;
  6201. int i;
  6202. if (global->p2p_init_wpa_s == NULL)
  6203. return NULL;
  6204. for (i = 0; !found && cmd[i]; i++) {
  6205. if (os_strcmp(buf, cmd[i]) == 0)
  6206. found = 1;
  6207. }
  6208. for (i = 0; !found && prefix[i]; i++) {
  6209. if (os_strncmp(buf, prefix[i], os_strlen(prefix[i])) == 0)
  6210. found = 1;
  6211. }
  6212. if (found)
  6213. return wpa_supplicant_ctrl_iface_process(global->p2p_init_wpa_s,
  6214. buf, resp_len);
  6215. #endif /* CONFIG_P2P */
  6216. return NULL;
  6217. }
  6218. static char * wpas_global_ctrl_iface_redir_wfd(struct wpa_global *global,
  6219. char *buf, size_t *resp_len)
  6220. {
  6221. #ifdef CONFIG_WIFI_DISPLAY
  6222. if (global->p2p_init_wpa_s == NULL)
  6223. return NULL;
  6224. if (os_strncmp(buf, "WFD_SUBELEM_SET ", 16) == 0 ||
  6225. os_strncmp(buf, "WFD_SUBELEM_GET ", 16) == 0)
  6226. return wpa_supplicant_ctrl_iface_process(global->p2p_init_wpa_s,
  6227. buf, resp_len);
  6228. #endif /* CONFIG_WIFI_DISPLAY */
  6229. return NULL;
  6230. }
  6231. static char * wpas_global_ctrl_iface_redir(struct wpa_global *global,
  6232. char *buf, size_t *resp_len)
  6233. {
  6234. char *ret;
  6235. ret = wpas_global_ctrl_iface_redir_p2p(global, buf, resp_len);
  6236. if (ret)
  6237. return ret;
  6238. ret = wpas_global_ctrl_iface_redir_wfd(global, buf, resp_len);
  6239. if (ret)
  6240. return ret;
  6241. return NULL;
  6242. }
  6243. static int wpas_global_ctrl_iface_set(struct wpa_global *global, char *cmd)
  6244. {
  6245. char *value;
  6246. value = os_strchr(cmd, ' ');
  6247. if (value == NULL)
  6248. return -1;
  6249. *value++ = '\0';
  6250. wpa_printf(MSG_DEBUG, "GLOBAL_CTRL_IFACE SET '%s'='%s'", cmd, value);
  6251. #ifdef CONFIG_WIFI_DISPLAY
  6252. if (os_strcasecmp(cmd, "wifi_display") == 0) {
  6253. wifi_display_enable(global, !!atoi(value));
  6254. return 0;
  6255. }
  6256. #endif /* CONFIG_WIFI_DISPLAY */
  6257. /* Restore cmd to its original value to allow redirection */
  6258. value[-1] = ' ';
  6259. return -1;
  6260. }
  6261. #ifndef CONFIG_NO_CONFIG_WRITE
  6262. static int wpas_global_ctrl_iface_save_config(struct wpa_global *global)
  6263. {
  6264. int ret = 0, saved = 0;
  6265. struct wpa_supplicant *wpa_s;
  6266. for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
  6267. if (!wpa_s->conf->update_config) {
  6268. wpa_dbg(wpa_s, MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Not allowed to update configuration (update_config=0)");
  6269. continue;
  6270. }
  6271. if (wpa_config_write(wpa_s->confname, wpa_s->conf)) {
  6272. wpa_dbg(wpa_s, MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Failed to update configuration");
  6273. ret = 1;
  6274. } else {
  6275. wpa_dbg(wpa_s, MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Configuration updated");
  6276. saved++;
  6277. }
  6278. }
  6279. if (!saved && !ret) {
  6280. wpa_dbg(wpa_s, MSG_DEBUG,
  6281. "CTRL_IFACE: SAVE_CONFIG - No configuration files could be updated");
  6282. ret = 1;
  6283. }
  6284. return ret;
  6285. }
  6286. #endif /* CONFIG_NO_CONFIG_WRITE */
  6287. static int wpas_global_ctrl_iface_status(struct wpa_global *global,
  6288. char *buf, size_t buflen)
  6289. {
  6290. char *pos, *end;
  6291. int ret;
  6292. struct wpa_supplicant *wpa_s;
  6293. pos = buf;
  6294. end = buf + buflen;
  6295. #ifdef CONFIG_P2P
  6296. if (global->p2p && !global->p2p_disabled) {
  6297. ret = os_snprintf(pos, end - pos, "p2p_device_address=" MACSTR
  6298. "\n"
  6299. "p2p_state=%s\n",
  6300. MAC2STR(global->p2p_dev_addr),
  6301. p2p_get_state_txt(global->p2p));
  6302. if (ret < 0 || ret >= end - pos)
  6303. return pos - buf;
  6304. pos += ret;
  6305. } else if (global->p2p) {
  6306. ret = os_snprintf(pos, end - pos, "p2p_state=DISABLED\n");
  6307. if (ret < 0 || ret >= end - pos)
  6308. return pos - buf;
  6309. pos += ret;
  6310. }
  6311. #endif /* CONFIG_P2P */
  6312. #ifdef CONFIG_WIFI_DISPLAY
  6313. ret = os_snprintf(pos, end - pos, "wifi_display=%d\n",
  6314. !!global->wifi_display);
  6315. if (ret < 0 || ret >= end - pos)
  6316. return pos - buf;
  6317. pos += ret;
  6318. #endif /* CONFIG_WIFI_DISPLAY */
  6319. for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
  6320. ret = os_snprintf(pos, end - pos, "ifname=%s\n"
  6321. "address=" MACSTR "\n",
  6322. wpa_s->ifname, MAC2STR(wpa_s->own_addr));
  6323. if (ret < 0 || ret >= end - pos)
  6324. return pos - buf;
  6325. pos += ret;
  6326. }
  6327. return pos - buf;
  6328. }
  6329. char * wpa_supplicant_global_ctrl_iface_process(struct wpa_global *global,
  6330. char *buf, size_t *resp_len)
  6331. {
  6332. char *reply;
  6333. const int reply_size = 2048;
  6334. int reply_len;
  6335. int level = MSG_DEBUG;
  6336. if (os_strncmp(buf, "IFNAME=", 7) == 0) {
  6337. char *pos = os_strchr(buf + 7, ' ');
  6338. if (pos) {
  6339. *pos++ = '\0';
  6340. return wpas_global_ctrl_iface_ifname(global,
  6341. buf + 7, pos,
  6342. resp_len);
  6343. }
  6344. }
  6345. reply = wpas_global_ctrl_iface_redir(global, buf, resp_len);
  6346. if (reply)
  6347. return reply;
  6348. if (os_strcmp(buf, "PING") == 0)
  6349. level = MSG_EXCESSIVE;
  6350. wpa_hexdump_ascii(level, "RX global ctrl_iface",
  6351. (const u8 *) buf, os_strlen(buf));
  6352. reply = os_malloc(reply_size);
  6353. if (reply == NULL) {
  6354. *resp_len = 1;
  6355. return NULL;
  6356. }
  6357. os_memcpy(reply, "OK\n", 3);
  6358. reply_len = 3;
  6359. if (os_strcmp(buf, "PING") == 0) {
  6360. os_memcpy(reply, "PONG\n", 5);
  6361. reply_len = 5;
  6362. } else if (os_strncmp(buf, "INTERFACE_ADD ", 14) == 0) {
  6363. if (wpa_supplicant_global_iface_add(global, buf + 14))
  6364. reply_len = -1;
  6365. } else if (os_strncmp(buf, "INTERFACE_REMOVE ", 17) == 0) {
  6366. if (wpa_supplicant_global_iface_remove(global, buf + 17))
  6367. reply_len = -1;
  6368. } else if (os_strcmp(buf, "INTERFACE_LIST") == 0) {
  6369. reply_len = wpa_supplicant_global_iface_list(
  6370. global, reply, reply_size);
  6371. } else if (os_strcmp(buf, "INTERFACES") == 0) {
  6372. reply_len = wpa_supplicant_global_iface_interfaces(
  6373. global, reply, reply_size);
  6374. } else if (os_strcmp(buf, "TERMINATE") == 0) {
  6375. wpa_supplicant_terminate_proc(global);
  6376. } else if (os_strcmp(buf, "SUSPEND") == 0) {
  6377. wpas_notify_suspend(global);
  6378. } else if (os_strcmp(buf, "RESUME") == 0) {
  6379. wpas_notify_resume(global);
  6380. } else if (os_strncmp(buf, "SET ", 4) == 0) {
  6381. if (wpas_global_ctrl_iface_set(global, buf + 4)) {
  6382. #ifdef CONFIG_P2P
  6383. if (global->p2p_init_wpa_s) {
  6384. os_free(reply);
  6385. /* Check if P2P redirection would work for this
  6386. * command. */
  6387. return wpa_supplicant_ctrl_iface_process(
  6388. global->p2p_init_wpa_s,
  6389. buf, resp_len);
  6390. }
  6391. #endif /* CONFIG_P2P */
  6392. reply_len = -1;
  6393. }
  6394. #ifndef CONFIG_NO_CONFIG_WRITE
  6395. } else if (os_strcmp(buf, "SAVE_CONFIG") == 0) {
  6396. if (wpas_global_ctrl_iface_save_config(global))
  6397. reply_len = -1;
  6398. #endif /* CONFIG_NO_CONFIG_WRITE */
  6399. } else if (os_strcmp(buf, "STATUS") == 0) {
  6400. reply_len = wpas_global_ctrl_iface_status(global, reply,
  6401. reply_size);
  6402. #ifdef CONFIG_MODULE_TESTS
  6403. } else if (os_strcmp(buf, "MODULE_TESTS") == 0) {
  6404. int wpas_module_tests(void);
  6405. if (wpas_module_tests() < 0)
  6406. reply_len = -1;
  6407. #endif /* CONFIG_MODULE_TESTS */
  6408. } else {
  6409. os_memcpy(reply, "UNKNOWN COMMAND\n", 16);
  6410. reply_len = 16;
  6411. }
  6412. if (reply_len < 0) {
  6413. os_memcpy(reply, "FAIL\n", 5);
  6414. reply_len = 5;
  6415. }
  6416. *resp_len = reply_len;
  6417. return reply;
  6418. }