ctrl_iface.c 178 KB

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