ctrl_iface.c 178 KB

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