ctrl_iface.c 177 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544
  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. wpa_msg_ctrl(wpa_s, MSG_INFO, WPA_EVENT_STATE_CHANGE
  1509. "id=%d state=%d BSSID=" MACSTR " SSID=%s",
  1510. wpa_s->current_ssid ? wpa_s->current_ssid->id : -1,
  1511. wpa_s->wpa_state,
  1512. MAC2STR(wpa_s->bssid),
  1513. wpa_s->current_ssid && wpa_s->current_ssid->ssid ?
  1514. wpa_ssid_txt(wpa_s->current_ssid->ssid,
  1515. wpa_s->current_ssid->ssid_len) : "");
  1516. if (wpa_s->wpa_state == WPA_COMPLETED) {
  1517. struct wpa_ssid *ssid = wpa_s->current_ssid;
  1518. wpa_msg_ctrl(wpa_s, MSG_INFO, WPA_EVENT_CONNECTED
  1519. "- connection to " MACSTR
  1520. " completed %s [id=%d id_str=%s]",
  1521. MAC2STR(wpa_s->bssid), "(auth)",
  1522. ssid ? ssid->id : -1,
  1523. ssid && ssid->id_str ? ssid->id_str : "");
  1524. }
  1525. #endif /* ANDROID */
  1526. return pos - buf;
  1527. }
  1528. static int wpa_supplicant_ctrl_iface_bssid(struct wpa_supplicant *wpa_s,
  1529. char *cmd)
  1530. {
  1531. char *pos;
  1532. int id;
  1533. struct wpa_ssid *ssid;
  1534. u8 bssid[ETH_ALEN];
  1535. /* cmd: "<network id> <BSSID>" */
  1536. pos = os_strchr(cmd, ' ');
  1537. if (pos == NULL)
  1538. return -1;
  1539. *pos++ = '\0';
  1540. id = atoi(cmd);
  1541. wpa_printf(MSG_DEBUG, "CTRL_IFACE: id=%d bssid='%s'", id, pos);
  1542. if (hwaddr_aton(pos, bssid)) {
  1543. wpa_printf(MSG_DEBUG ,"CTRL_IFACE: invalid BSSID '%s'", pos);
  1544. return -1;
  1545. }
  1546. ssid = wpa_config_get_network(wpa_s->conf, id);
  1547. if (ssid == NULL) {
  1548. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find SSID id=%d "
  1549. "to update", id);
  1550. return -1;
  1551. }
  1552. os_memcpy(ssid->bssid, bssid, ETH_ALEN);
  1553. ssid->bssid_set = !is_zero_ether_addr(bssid);
  1554. return 0;
  1555. }
  1556. static int wpa_supplicant_ctrl_iface_blacklist(struct wpa_supplicant *wpa_s,
  1557. char *cmd, char *buf,
  1558. size_t buflen)
  1559. {
  1560. u8 bssid[ETH_ALEN];
  1561. struct wpa_blacklist *e;
  1562. char *pos, *end;
  1563. int ret;
  1564. /* cmd: "BLACKLIST [<BSSID>]" */
  1565. if (*cmd == '\0') {
  1566. pos = buf;
  1567. end = buf + buflen;
  1568. e = wpa_s->blacklist;
  1569. while (e) {
  1570. ret = os_snprintf(pos, end - pos, MACSTR "\n",
  1571. MAC2STR(e->bssid));
  1572. if (ret < 0 || ret >= end - pos)
  1573. return pos - buf;
  1574. pos += ret;
  1575. e = e->next;
  1576. }
  1577. return pos - buf;
  1578. }
  1579. cmd++;
  1580. if (os_strncmp(cmd, "clear", 5) == 0) {
  1581. wpa_blacklist_clear(wpa_s);
  1582. os_memcpy(buf, "OK\n", 3);
  1583. return 3;
  1584. }
  1585. wpa_printf(MSG_DEBUG, "CTRL_IFACE: BLACKLIST bssid='%s'", cmd);
  1586. if (hwaddr_aton(cmd, bssid)) {
  1587. wpa_printf(MSG_DEBUG, "CTRL_IFACE: invalid BSSID '%s'", cmd);
  1588. return -1;
  1589. }
  1590. /*
  1591. * Add the BSSID twice, so its count will be 2, causing it to be
  1592. * skipped when processing scan results.
  1593. */
  1594. ret = wpa_blacklist_add(wpa_s, bssid);
  1595. if (ret < 0)
  1596. return -1;
  1597. ret = wpa_blacklist_add(wpa_s, bssid);
  1598. if (ret < 0)
  1599. return -1;
  1600. os_memcpy(buf, "OK\n", 3);
  1601. return 3;
  1602. }
  1603. static const char * debug_level_str(int level)
  1604. {
  1605. switch (level) {
  1606. case MSG_EXCESSIVE:
  1607. return "EXCESSIVE";
  1608. case MSG_MSGDUMP:
  1609. return "MSGDUMP";
  1610. case MSG_DEBUG:
  1611. return "DEBUG";
  1612. case MSG_INFO:
  1613. return "INFO";
  1614. case MSG_WARNING:
  1615. return "WARNING";
  1616. case MSG_ERROR:
  1617. return "ERROR";
  1618. default:
  1619. return "?";
  1620. }
  1621. }
  1622. static int str_to_debug_level(const char *s)
  1623. {
  1624. if (os_strcasecmp(s, "EXCESSIVE") == 0)
  1625. return MSG_EXCESSIVE;
  1626. if (os_strcasecmp(s, "MSGDUMP") == 0)
  1627. return MSG_MSGDUMP;
  1628. if (os_strcasecmp(s, "DEBUG") == 0)
  1629. return MSG_DEBUG;
  1630. if (os_strcasecmp(s, "INFO") == 0)
  1631. return MSG_INFO;
  1632. if (os_strcasecmp(s, "WARNING") == 0)
  1633. return MSG_WARNING;
  1634. if (os_strcasecmp(s, "ERROR") == 0)
  1635. return MSG_ERROR;
  1636. return -1;
  1637. }
  1638. static int wpa_supplicant_ctrl_iface_log_level(struct wpa_supplicant *wpa_s,
  1639. char *cmd, char *buf,
  1640. size_t buflen)
  1641. {
  1642. char *pos, *end, *stamp;
  1643. int ret;
  1644. if (cmd == NULL) {
  1645. return -1;
  1646. }
  1647. /* cmd: "LOG_LEVEL [<level>]" */
  1648. if (*cmd == '\0') {
  1649. pos = buf;
  1650. end = buf + buflen;
  1651. ret = os_snprintf(pos, end - pos, "Current level: %s\n"
  1652. "Timestamp: %d\n",
  1653. debug_level_str(wpa_debug_level),
  1654. wpa_debug_timestamp);
  1655. if (ret < 0 || ret >= end - pos)
  1656. ret = 0;
  1657. return ret;
  1658. }
  1659. while (*cmd == ' ')
  1660. cmd++;
  1661. stamp = os_strchr(cmd, ' ');
  1662. if (stamp) {
  1663. *stamp++ = '\0';
  1664. while (*stamp == ' ') {
  1665. stamp++;
  1666. }
  1667. }
  1668. if (cmd && os_strlen(cmd)) {
  1669. int level = str_to_debug_level(cmd);
  1670. if (level < 0)
  1671. return -1;
  1672. wpa_debug_level = level;
  1673. }
  1674. if (stamp && os_strlen(stamp))
  1675. wpa_debug_timestamp = atoi(stamp);
  1676. os_memcpy(buf, "OK\n", 3);
  1677. return 3;
  1678. }
  1679. static int wpa_supplicant_ctrl_iface_list_networks(
  1680. struct wpa_supplicant *wpa_s, char *buf, size_t buflen)
  1681. {
  1682. char *pos, *end;
  1683. struct wpa_ssid *ssid;
  1684. int ret;
  1685. pos = buf;
  1686. end = buf + buflen;
  1687. ret = os_snprintf(pos, end - pos,
  1688. "network id / ssid / bssid / flags\n");
  1689. if (ret < 0 || ret >= end - pos)
  1690. return pos - buf;
  1691. pos += ret;
  1692. ssid = wpa_s->conf->ssid;
  1693. while (ssid) {
  1694. ret = os_snprintf(pos, end - pos, "%d\t%s",
  1695. ssid->id,
  1696. wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
  1697. if (ret < 0 || ret >= end - pos)
  1698. return pos - buf;
  1699. pos += ret;
  1700. if (ssid->bssid_set) {
  1701. ret = os_snprintf(pos, end - pos, "\t" MACSTR,
  1702. MAC2STR(ssid->bssid));
  1703. } else {
  1704. ret = os_snprintf(pos, end - pos, "\tany");
  1705. }
  1706. if (ret < 0 || ret >= end - pos)
  1707. return pos - buf;
  1708. pos += ret;
  1709. ret = os_snprintf(pos, end - pos, "\t%s%s%s%s",
  1710. ssid == wpa_s->current_ssid ?
  1711. "[CURRENT]" : "",
  1712. ssid->disabled ? "[DISABLED]" : "",
  1713. ssid->disabled_until.sec ?
  1714. "[TEMP-DISABLED]" : "",
  1715. ssid->disabled == 2 ? "[P2P-PERSISTENT]" :
  1716. "");
  1717. if (ret < 0 || ret >= end - pos)
  1718. return pos - buf;
  1719. pos += ret;
  1720. ret = os_snprintf(pos, end - pos, "\n");
  1721. if (ret < 0 || ret >= end - pos)
  1722. return pos - buf;
  1723. pos += ret;
  1724. ssid = ssid->next;
  1725. }
  1726. return pos - buf;
  1727. }
  1728. static char * wpa_supplicant_cipher_txt(char *pos, char *end, int cipher)
  1729. {
  1730. int ret;
  1731. ret = os_snprintf(pos, end - pos, "-");
  1732. if (ret < 0 || ret >= end - pos)
  1733. return pos;
  1734. pos += ret;
  1735. ret = wpa_write_ciphers(pos, end, cipher, "+");
  1736. if (ret < 0)
  1737. return pos;
  1738. pos += ret;
  1739. return pos;
  1740. }
  1741. static char * wpa_supplicant_ie_txt(char *pos, char *end, const char *proto,
  1742. const u8 *ie, size_t ie_len)
  1743. {
  1744. struct wpa_ie_data data;
  1745. char *start;
  1746. int ret;
  1747. ret = os_snprintf(pos, end - pos, "[%s-", proto);
  1748. if (ret < 0 || ret >= end - pos)
  1749. return pos;
  1750. pos += ret;
  1751. if (wpa_parse_wpa_ie(ie, ie_len, &data) < 0) {
  1752. ret = os_snprintf(pos, end - pos, "?]");
  1753. if (ret < 0 || ret >= end - pos)
  1754. return pos;
  1755. pos += ret;
  1756. return pos;
  1757. }
  1758. start = pos;
  1759. if (data.key_mgmt & WPA_KEY_MGMT_IEEE8021X) {
  1760. ret = os_snprintf(pos, end - pos, "%sEAP",
  1761. pos == start ? "" : "+");
  1762. if (ret < 0 || ret >= end - pos)
  1763. return pos;
  1764. pos += ret;
  1765. }
  1766. if (data.key_mgmt & WPA_KEY_MGMT_PSK) {
  1767. ret = os_snprintf(pos, end - pos, "%sPSK",
  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_WPA_NONE) {
  1774. ret = os_snprintf(pos, end - pos, "%sNone",
  1775. pos == start ? "" : "+");
  1776. if (ret < 0 || ret >= end - pos)
  1777. return pos;
  1778. pos += ret;
  1779. }
  1780. #ifdef CONFIG_IEEE80211R
  1781. if (data.key_mgmt & WPA_KEY_MGMT_FT_IEEE8021X) {
  1782. ret = os_snprintf(pos, end - pos, "%sFT/EAP",
  1783. pos == start ? "" : "+");
  1784. if (ret < 0 || ret >= end - pos)
  1785. return pos;
  1786. pos += ret;
  1787. }
  1788. if (data.key_mgmt & WPA_KEY_MGMT_FT_PSK) {
  1789. ret = os_snprintf(pos, end - pos, "%sFT/PSK",
  1790. pos == start ? "" : "+");
  1791. if (ret < 0 || ret >= end - pos)
  1792. return pos;
  1793. pos += ret;
  1794. }
  1795. #endif /* CONFIG_IEEE80211R */
  1796. #ifdef CONFIG_IEEE80211W
  1797. if (data.key_mgmt & WPA_KEY_MGMT_IEEE8021X_SHA256) {
  1798. ret = os_snprintf(pos, end - pos, "%sEAP-SHA256",
  1799. pos == start ? "" : "+");
  1800. if (ret < 0 || ret >= end - pos)
  1801. return pos;
  1802. pos += ret;
  1803. }
  1804. if (data.key_mgmt & WPA_KEY_MGMT_PSK_SHA256) {
  1805. ret = os_snprintf(pos, end - pos, "%sPSK-SHA256",
  1806. pos == start ? "" : "+");
  1807. if (ret < 0 || ret >= end - pos)
  1808. return pos;
  1809. pos += ret;
  1810. }
  1811. #endif /* CONFIG_IEEE80211W */
  1812. pos = wpa_supplicant_cipher_txt(pos, end, data.pairwise_cipher);
  1813. if (data.capabilities & WPA_CAPABILITY_PREAUTH) {
  1814. ret = os_snprintf(pos, end - pos, "-preauth");
  1815. if (ret < 0 || ret >= end - pos)
  1816. return pos;
  1817. pos += ret;
  1818. }
  1819. ret = os_snprintf(pos, end - pos, "]");
  1820. if (ret < 0 || ret >= end - pos)
  1821. return pos;
  1822. pos += ret;
  1823. return pos;
  1824. }
  1825. #ifdef CONFIG_WPS
  1826. static char * wpa_supplicant_wps_ie_txt_buf(struct wpa_supplicant *wpa_s,
  1827. char *pos, char *end,
  1828. struct wpabuf *wps_ie)
  1829. {
  1830. int ret;
  1831. const char *txt;
  1832. if (wps_ie == NULL)
  1833. return pos;
  1834. if (wps_is_selected_pbc_registrar(wps_ie))
  1835. txt = "[WPS-PBC]";
  1836. else if (wps_is_addr_authorized(wps_ie, wpa_s->own_addr, 0))
  1837. txt = "[WPS-AUTH]";
  1838. else if (wps_is_selected_pin_registrar(wps_ie))
  1839. txt = "[WPS-PIN]";
  1840. else
  1841. txt = "[WPS]";
  1842. ret = os_snprintf(pos, end - pos, "%s", txt);
  1843. if (ret >= 0 && ret < end - pos)
  1844. pos += ret;
  1845. wpabuf_free(wps_ie);
  1846. return pos;
  1847. }
  1848. #endif /* CONFIG_WPS */
  1849. static char * wpa_supplicant_wps_ie_txt(struct wpa_supplicant *wpa_s,
  1850. char *pos, char *end,
  1851. const struct wpa_bss *bss)
  1852. {
  1853. #ifdef CONFIG_WPS
  1854. struct wpabuf *wps_ie;
  1855. wps_ie = wpa_bss_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
  1856. return wpa_supplicant_wps_ie_txt_buf(wpa_s, pos, end, wps_ie);
  1857. #else /* CONFIG_WPS */
  1858. return pos;
  1859. #endif /* CONFIG_WPS */
  1860. }
  1861. /* Format one result on one text line into a buffer. */
  1862. static int wpa_supplicant_ctrl_iface_scan_result(
  1863. struct wpa_supplicant *wpa_s,
  1864. const struct wpa_bss *bss, char *buf, size_t buflen)
  1865. {
  1866. char *pos, *end;
  1867. int ret;
  1868. const u8 *ie, *ie2, *p2p;
  1869. p2p = wpa_bss_get_vendor_ie(bss, P2P_IE_VENDOR_TYPE);
  1870. if (!p2p)
  1871. p2p = wpa_bss_get_vendor_ie_beacon(bss, P2P_IE_VENDOR_TYPE);
  1872. if (p2p && bss->ssid_len == P2P_WILDCARD_SSID_LEN &&
  1873. os_memcmp(bss->ssid, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN) ==
  1874. 0)
  1875. return 0; /* Do not show P2P listen discovery results here */
  1876. pos = buf;
  1877. end = buf + buflen;
  1878. ret = os_snprintf(pos, end - pos, MACSTR "\t%d\t%d\t",
  1879. MAC2STR(bss->bssid), bss->freq, bss->level);
  1880. if (ret < 0 || ret >= end - pos)
  1881. return -1;
  1882. pos += ret;
  1883. ie = wpa_bss_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
  1884. if (ie)
  1885. pos = wpa_supplicant_ie_txt(pos, end, "WPA", ie, 2 + ie[1]);
  1886. ie2 = wpa_bss_get_ie(bss, WLAN_EID_RSN);
  1887. if (ie2)
  1888. pos = wpa_supplicant_ie_txt(pos, end, "WPA2", ie2, 2 + ie2[1]);
  1889. pos = wpa_supplicant_wps_ie_txt(wpa_s, pos, end, bss);
  1890. if (!ie && !ie2 && bss->caps & IEEE80211_CAP_PRIVACY) {
  1891. ret = os_snprintf(pos, end - pos, "[WEP]");
  1892. if (ret < 0 || ret >= end - pos)
  1893. return -1;
  1894. pos += ret;
  1895. }
  1896. if (bss_is_dmg(bss)) {
  1897. const char *s;
  1898. ret = os_snprintf(pos, end - pos, "[DMG]");
  1899. if (ret < 0 || ret >= end - pos)
  1900. return -1;
  1901. pos += ret;
  1902. switch (bss->caps & IEEE80211_CAP_DMG_MASK) {
  1903. case IEEE80211_CAP_DMG_IBSS:
  1904. s = "[IBSS]";
  1905. break;
  1906. case IEEE80211_CAP_DMG_AP:
  1907. s = "[ESS]";
  1908. break;
  1909. case IEEE80211_CAP_DMG_PBSS:
  1910. s = "[PBSS]";
  1911. break;
  1912. default:
  1913. s = "";
  1914. break;
  1915. }
  1916. ret = os_snprintf(pos, end - pos, "%s", s);
  1917. if (ret < 0 || ret >= end - pos)
  1918. return -1;
  1919. pos += ret;
  1920. } else {
  1921. if (bss->caps & IEEE80211_CAP_IBSS) {
  1922. ret = os_snprintf(pos, end - pos, "[IBSS]");
  1923. if (ret < 0 || ret >= end - pos)
  1924. return -1;
  1925. pos += ret;
  1926. }
  1927. if (bss->caps & IEEE80211_CAP_ESS) {
  1928. ret = os_snprintf(pos, end - pos, "[ESS]");
  1929. if (ret < 0 || ret >= end - pos)
  1930. return -1;
  1931. pos += ret;
  1932. }
  1933. }
  1934. if (p2p) {
  1935. ret = os_snprintf(pos, end - pos, "[P2P]");
  1936. if (ret < 0 || ret >= end - pos)
  1937. return -1;
  1938. pos += ret;
  1939. }
  1940. #ifdef CONFIG_HS20
  1941. if (wpa_bss_get_vendor_ie(bss, HS20_IE_VENDOR_TYPE) && ie2) {
  1942. ret = os_snprintf(pos, end - pos, "[HS20]");
  1943. if (ret < 0 || ret >= end - pos)
  1944. return -1;
  1945. pos += ret;
  1946. }
  1947. #endif /* CONFIG_HS20 */
  1948. ret = os_snprintf(pos, end - pos, "\t%s",
  1949. wpa_ssid_txt(bss->ssid, bss->ssid_len));
  1950. if (ret < 0 || ret >= end - pos)
  1951. return -1;
  1952. pos += ret;
  1953. ret = os_snprintf(pos, end - pos, "\n");
  1954. if (ret < 0 || ret >= end - pos)
  1955. return -1;
  1956. pos += ret;
  1957. return pos - buf;
  1958. }
  1959. static int wpa_supplicant_ctrl_iface_scan_results(
  1960. struct wpa_supplicant *wpa_s, char *buf, size_t buflen)
  1961. {
  1962. char *pos, *end;
  1963. struct wpa_bss *bss;
  1964. int ret;
  1965. pos = buf;
  1966. end = buf + buflen;
  1967. ret = os_snprintf(pos, end - pos, "bssid / frequency / signal level / "
  1968. "flags / ssid\n");
  1969. if (ret < 0 || ret >= end - pos)
  1970. return pos - buf;
  1971. pos += ret;
  1972. dl_list_for_each(bss, &wpa_s->bss_id, struct wpa_bss, list_id) {
  1973. ret = wpa_supplicant_ctrl_iface_scan_result(wpa_s, bss, pos,
  1974. end - pos);
  1975. if (ret < 0 || ret >= end - pos)
  1976. return pos - buf;
  1977. pos += ret;
  1978. }
  1979. return pos - buf;
  1980. }
  1981. static int wpa_supplicant_ctrl_iface_select_network(
  1982. struct wpa_supplicant *wpa_s, char *cmd)
  1983. {
  1984. int id;
  1985. struct wpa_ssid *ssid;
  1986. char *pos;
  1987. /* cmd: "<network id>" or "any" */
  1988. if (os_strncmp(cmd, "any", 3) == 0) {
  1989. wpa_printf(MSG_DEBUG, "CTRL_IFACE: SELECT_NETWORK any");
  1990. ssid = NULL;
  1991. } else {
  1992. id = atoi(cmd);
  1993. wpa_printf(MSG_DEBUG, "CTRL_IFACE: SELECT_NETWORK id=%d", id);
  1994. ssid = wpa_config_get_network(wpa_s->conf, id);
  1995. if (ssid == NULL) {
  1996. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find "
  1997. "network id=%d", id);
  1998. return -1;
  1999. }
  2000. if (ssid->disabled == 2) {
  2001. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Cannot use "
  2002. "SELECT_NETWORK with persistent P2P group");
  2003. return -1;
  2004. }
  2005. }
  2006. pos = os_strstr(cmd, " freq=");
  2007. if (pos) {
  2008. int *freqs = freq_range_to_channel_list(wpa_s, pos + 6);
  2009. if (freqs) {
  2010. wpa_s->scan_req = MANUAL_SCAN_REQ;
  2011. os_free(wpa_s->manual_scan_freqs);
  2012. wpa_s->manual_scan_freqs = freqs;
  2013. }
  2014. }
  2015. wpa_supplicant_select_network(wpa_s, ssid);
  2016. return 0;
  2017. }
  2018. static int wpa_supplicant_ctrl_iface_enable_network(
  2019. struct wpa_supplicant *wpa_s, char *cmd)
  2020. {
  2021. int id;
  2022. struct wpa_ssid *ssid;
  2023. /* cmd: "<network id>" or "all" */
  2024. if (os_strcmp(cmd, "all") == 0) {
  2025. wpa_printf(MSG_DEBUG, "CTRL_IFACE: ENABLE_NETWORK all");
  2026. ssid = NULL;
  2027. } else {
  2028. id = atoi(cmd);
  2029. wpa_printf(MSG_DEBUG, "CTRL_IFACE: ENABLE_NETWORK id=%d", id);
  2030. ssid = wpa_config_get_network(wpa_s->conf, id);
  2031. if (ssid == NULL) {
  2032. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find "
  2033. "network id=%d", id);
  2034. return -1;
  2035. }
  2036. if (ssid->disabled == 2) {
  2037. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Cannot use "
  2038. "ENABLE_NETWORK with persistent P2P group");
  2039. return -1;
  2040. }
  2041. if (os_strstr(cmd, " no-connect")) {
  2042. ssid->disabled = 0;
  2043. return 0;
  2044. }
  2045. }
  2046. wpa_supplicant_enable_network(wpa_s, ssid);
  2047. return 0;
  2048. }
  2049. static int wpa_supplicant_ctrl_iface_disable_network(
  2050. struct wpa_supplicant *wpa_s, char *cmd)
  2051. {
  2052. int id;
  2053. struct wpa_ssid *ssid;
  2054. /* cmd: "<network id>" or "all" */
  2055. if (os_strcmp(cmd, "all") == 0) {
  2056. wpa_printf(MSG_DEBUG, "CTRL_IFACE: DISABLE_NETWORK all");
  2057. ssid = NULL;
  2058. } else {
  2059. id = atoi(cmd);
  2060. wpa_printf(MSG_DEBUG, "CTRL_IFACE: DISABLE_NETWORK id=%d", id);
  2061. ssid = wpa_config_get_network(wpa_s->conf, id);
  2062. if (ssid == NULL) {
  2063. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find "
  2064. "network id=%d", id);
  2065. return -1;
  2066. }
  2067. if (ssid->disabled == 2) {
  2068. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Cannot use "
  2069. "DISABLE_NETWORK with persistent P2P "
  2070. "group");
  2071. return -1;
  2072. }
  2073. }
  2074. wpa_supplicant_disable_network(wpa_s, ssid);
  2075. return 0;
  2076. }
  2077. static int wpa_supplicant_ctrl_iface_add_network(
  2078. struct wpa_supplicant *wpa_s, char *buf, size_t buflen)
  2079. {
  2080. struct wpa_ssid *ssid;
  2081. int ret;
  2082. wpa_printf(MSG_DEBUG, "CTRL_IFACE: ADD_NETWORK");
  2083. ssid = wpa_config_add_network(wpa_s->conf);
  2084. if (ssid == NULL)
  2085. return -1;
  2086. wpas_notify_network_added(wpa_s, ssid);
  2087. ssid->disabled = 1;
  2088. wpa_config_set_network_defaults(ssid);
  2089. ret = os_snprintf(buf, buflen, "%d\n", ssid->id);
  2090. if (ret < 0 || (size_t) ret >= buflen)
  2091. return -1;
  2092. return ret;
  2093. }
  2094. static int wpa_supplicant_ctrl_iface_remove_network(
  2095. struct wpa_supplicant *wpa_s, char *cmd)
  2096. {
  2097. int id;
  2098. struct wpa_ssid *ssid;
  2099. int was_disabled;
  2100. /* cmd: "<network id>" or "all" */
  2101. if (os_strcmp(cmd, "all") == 0) {
  2102. wpa_printf(MSG_DEBUG, "CTRL_IFACE: REMOVE_NETWORK all");
  2103. if (wpa_s->sched_scanning)
  2104. wpa_supplicant_cancel_sched_scan(wpa_s);
  2105. eapol_sm_invalidate_cached_session(wpa_s->eapol);
  2106. if (wpa_s->current_ssid) {
  2107. #ifdef CONFIG_SME
  2108. wpa_s->sme.prev_bssid_set = 0;
  2109. #endif /* CONFIG_SME */
  2110. wpa_sm_set_config(wpa_s->wpa, NULL);
  2111. eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
  2112. wpa_supplicant_deauthenticate(
  2113. wpa_s, WLAN_REASON_DEAUTH_LEAVING);
  2114. }
  2115. ssid = wpa_s->conf->ssid;
  2116. while (ssid) {
  2117. struct wpa_ssid *remove_ssid = ssid;
  2118. id = ssid->id;
  2119. ssid = ssid->next;
  2120. wpas_notify_network_removed(wpa_s, remove_ssid);
  2121. wpa_config_remove_network(wpa_s->conf, id);
  2122. }
  2123. return 0;
  2124. }
  2125. id = atoi(cmd);
  2126. wpa_printf(MSG_DEBUG, "CTRL_IFACE: REMOVE_NETWORK id=%d", id);
  2127. ssid = wpa_config_get_network(wpa_s->conf, id);
  2128. if (ssid)
  2129. wpas_notify_network_removed(wpa_s, ssid);
  2130. if (ssid == NULL) {
  2131. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find network "
  2132. "id=%d", id);
  2133. return -1;
  2134. }
  2135. if (ssid == wpa_s->current_ssid || wpa_s->current_ssid == NULL) {
  2136. #ifdef CONFIG_SME
  2137. wpa_s->sme.prev_bssid_set = 0;
  2138. #endif /* CONFIG_SME */
  2139. /*
  2140. * Invalidate the EAP session cache if the current or
  2141. * previously used network is removed.
  2142. */
  2143. eapol_sm_invalidate_cached_session(wpa_s->eapol);
  2144. }
  2145. if (ssid == wpa_s->current_ssid) {
  2146. wpa_sm_set_config(wpa_s->wpa, NULL);
  2147. eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
  2148. wpa_supplicant_deauthenticate(wpa_s,
  2149. WLAN_REASON_DEAUTH_LEAVING);
  2150. }
  2151. was_disabled = ssid->disabled;
  2152. if (wpa_config_remove_network(wpa_s->conf, id) < 0) {
  2153. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Not able to remove the "
  2154. "network id=%d", id);
  2155. return -1;
  2156. }
  2157. if (!was_disabled && wpa_s->sched_scanning) {
  2158. wpa_printf(MSG_DEBUG, "Stop ongoing sched_scan to remove "
  2159. "network from filters");
  2160. wpa_supplicant_cancel_sched_scan(wpa_s);
  2161. wpa_supplicant_req_scan(wpa_s, 0, 0);
  2162. }
  2163. return 0;
  2164. }
  2165. static int wpa_supplicant_ctrl_iface_update_network(
  2166. struct wpa_supplicant *wpa_s, struct wpa_ssid *ssid,
  2167. char *name, char *value)
  2168. {
  2169. if (wpa_config_set(ssid, name, value, 0) < 0) {
  2170. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Failed to set network "
  2171. "variable '%s'", name);
  2172. return -1;
  2173. }
  2174. if (os_strcmp(name, "bssid") != 0 &&
  2175. os_strcmp(name, "priority") != 0)
  2176. wpa_sm_pmksa_cache_flush(wpa_s->wpa, ssid);
  2177. if (wpa_s->current_ssid == ssid || wpa_s->current_ssid == NULL) {
  2178. /*
  2179. * Invalidate the EAP session cache if anything in the current
  2180. * or previously used configuration changes.
  2181. */
  2182. eapol_sm_invalidate_cached_session(wpa_s->eapol);
  2183. }
  2184. if ((os_strcmp(name, "psk") == 0 &&
  2185. value[0] == '"' && ssid->ssid_len) ||
  2186. (os_strcmp(name, "ssid") == 0 && ssid->passphrase))
  2187. wpa_config_update_psk(ssid);
  2188. else if (os_strcmp(name, "priority") == 0)
  2189. wpa_config_update_prio_list(wpa_s->conf);
  2190. return 0;
  2191. }
  2192. static int wpa_supplicant_ctrl_iface_set_network(
  2193. struct wpa_supplicant *wpa_s, char *cmd)
  2194. {
  2195. int id;
  2196. struct wpa_ssid *ssid;
  2197. char *name, *value;
  2198. /* cmd: "<network id> <variable name> <value>" */
  2199. name = os_strchr(cmd, ' ');
  2200. if (name == NULL)
  2201. return -1;
  2202. *name++ = '\0';
  2203. value = os_strchr(name, ' ');
  2204. if (value == NULL)
  2205. return -1;
  2206. *value++ = '\0';
  2207. id = atoi(cmd);
  2208. wpa_printf(MSG_DEBUG, "CTRL_IFACE: SET_NETWORK id=%d name='%s'",
  2209. id, name);
  2210. wpa_hexdump_ascii_key(MSG_DEBUG, "CTRL_IFACE: value",
  2211. (u8 *) value, os_strlen(value));
  2212. ssid = wpa_config_get_network(wpa_s->conf, id);
  2213. if (ssid == NULL) {
  2214. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find network "
  2215. "id=%d", id);
  2216. return -1;
  2217. }
  2218. return wpa_supplicant_ctrl_iface_update_network(wpa_s, ssid, name,
  2219. value);
  2220. }
  2221. static int wpa_supplicant_ctrl_iface_get_network(
  2222. struct wpa_supplicant *wpa_s, char *cmd, char *buf, size_t buflen)
  2223. {
  2224. int id;
  2225. size_t res;
  2226. struct wpa_ssid *ssid;
  2227. char *name, *value;
  2228. /* cmd: "<network id> <variable name>" */
  2229. name = os_strchr(cmd, ' ');
  2230. if (name == NULL || buflen == 0)
  2231. return -1;
  2232. *name++ = '\0';
  2233. id = atoi(cmd);
  2234. wpa_printf(MSG_DEBUG, "CTRL_IFACE: GET_NETWORK id=%d name='%s'",
  2235. id, name);
  2236. ssid = wpa_config_get_network(wpa_s->conf, id);
  2237. if (ssid == NULL) {
  2238. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find network "
  2239. "id=%d", id);
  2240. return -1;
  2241. }
  2242. value = wpa_config_get_no_key(ssid, name);
  2243. if (value == NULL) {
  2244. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Failed to get network "
  2245. "variable '%s'", name);
  2246. return -1;
  2247. }
  2248. res = os_strlcpy(buf, value, buflen);
  2249. if (res >= buflen) {
  2250. os_free(value);
  2251. return -1;
  2252. }
  2253. os_free(value);
  2254. return res;
  2255. }
  2256. static int wpa_supplicant_ctrl_iface_dup_network(
  2257. struct wpa_supplicant *wpa_s, char *cmd)
  2258. {
  2259. struct wpa_ssid *ssid_s, *ssid_d;
  2260. char *name, *id, *value;
  2261. int id_s, id_d, ret;
  2262. /* cmd: "<src network id> <dst network id> <variable name>" */
  2263. id = os_strchr(cmd, ' ');
  2264. if (id == NULL)
  2265. return -1;
  2266. *id++ = '\0';
  2267. name = os_strchr(id, ' ');
  2268. if (name == NULL)
  2269. return -1;
  2270. *name++ = '\0';
  2271. id_s = atoi(cmd);
  2272. id_d = atoi(id);
  2273. wpa_printf(MSG_DEBUG, "CTRL_IFACE: DUP_NETWORK id=%d -> %d name='%s'",
  2274. id_s, id_d, name);
  2275. ssid_s = wpa_config_get_network(wpa_s->conf, id_s);
  2276. if (ssid_s == NULL) {
  2277. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find "
  2278. "network id=%d", id_s);
  2279. return -1;
  2280. }
  2281. ssid_d = wpa_config_get_network(wpa_s->conf, id_d);
  2282. if (ssid_d == NULL) {
  2283. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find "
  2284. "network id=%d", id_s);
  2285. return -1;
  2286. }
  2287. value = wpa_config_get(ssid_s, name);
  2288. if (value == NULL) {
  2289. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Failed to get network "
  2290. "variable '%s'", name);
  2291. return -1;
  2292. }
  2293. ret = wpa_supplicant_ctrl_iface_update_network(wpa_s, ssid_d, name,
  2294. value);
  2295. os_free(value);
  2296. return ret;
  2297. }
  2298. static int wpa_supplicant_ctrl_iface_list_creds(struct wpa_supplicant *wpa_s,
  2299. char *buf, size_t buflen)
  2300. {
  2301. char *pos, *end;
  2302. struct wpa_cred *cred;
  2303. int ret;
  2304. pos = buf;
  2305. end = buf + buflen;
  2306. ret = os_snprintf(pos, end - pos,
  2307. "cred id / realm / username / domain / imsi\n");
  2308. if (ret < 0 || ret >= end - pos)
  2309. return pos - buf;
  2310. pos += ret;
  2311. cred = wpa_s->conf->cred;
  2312. while (cred) {
  2313. ret = os_snprintf(pos, end - pos, "%d\t%s\t%s\t%s\t%s\n",
  2314. cred->id, cred->realm ? cred->realm : "",
  2315. cred->username ? cred->username : "",
  2316. cred->domain ? cred->domain[0] : "",
  2317. cred->imsi ? cred->imsi : "");
  2318. if (ret < 0 || ret >= end - pos)
  2319. return pos - buf;
  2320. pos += ret;
  2321. cred = cred->next;
  2322. }
  2323. return pos - buf;
  2324. }
  2325. static int wpa_supplicant_ctrl_iface_add_cred(struct wpa_supplicant *wpa_s,
  2326. char *buf, size_t buflen)
  2327. {
  2328. struct wpa_cred *cred;
  2329. int ret;
  2330. wpa_printf(MSG_DEBUG, "CTRL_IFACE: ADD_CRED");
  2331. cred = wpa_config_add_cred(wpa_s->conf);
  2332. if (cred == NULL)
  2333. return -1;
  2334. wpa_msg(wpa_s, MSG_INFO, CRED_ADDED "%d", cred->id);
  2335. ret = os_snprintf(buf, buflen, "%d\n", cred->id);
  2336. if (ret < 0 || (size_t) ret >= buflen)
  2337. return -1;
  2338. return ret;
  2339. }
  2340. static int wpas_ctrl_remove_cred(struct wpa_supplicant *wpa_s,
  2341. struct wpa_cred *cred)
  2342. {
  2343. struct wpa_ssid *ssid;
  2344. char str[20];
  2345. int id;
  2346. if (cred == NULL) {
  2347. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find cred");
  2348. return -1;
  2349. }
  2350. id = cred->id;
  2351. if (wpa_config_remove_cred(wpa_s->conf, id) < 0) {
  2352. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find cred");
  2353. return -1;
  2354. }
  2355. wpa_msg(wpa_s, MSG_INFO, CRED_REMOVED "%d", id);
  2356. /* Remove any network entry created based on the removed credential */
  2357. ssid = wpa_s->conf->ssid;
  2358. while (ssid) {
  2359. if (ssid->parent_cred == cred) {
  2360. wpa_printf(MSG_DEBUG, "Remove network id %d since it "
  2361. "used the removed credential", ssid->id);
  2362. os_snprintf(str, sizeof(str), "%d", ssid->id);
  2363. ssid = ssid->next;
  2364. wpa_supplicant_ctrl_iface_remove_network(wpa_s, str);
  2365. } else
  2366. ssid = ssid->next;
  2367. }
  2368. return 0;
  2369. }
  2370. static int wpa_supplicant_ctrl_iface_remove_cred(struct wpa_supplicant *wpa_s,
  2371. char *cmd)
  2372. {
  2373. int id;
  2374. struct wpa_cred *cred, *prev;
  2375. /* cmd: "<cred id>", "all", "sp_fqdn=<FQDN>", or
  2376. * "provisioning_sp=<FQDN> */
  2377. if (os_strcmp(cmd, "all") == 0) {
  2378. wpa_printf(MSG_DEBUG, "CTRL_IFACE: REMOVE_CRED all");
  2379. cred = wpa_s->conf->cred;
  2380. while (cred) {
  2381. prev = cred;
  2382. cred = cred->next;
  2383. wpas_ctrl_remove_cred(wpa_s, prev);
  2384. }
  2385. return 0;
  2386. }
  2387. if (os_strncmp(cmd, "sp_fqdn=", 8) == 0) {
  2388. wpa_printf(MSG_DEBUG, "CTRL_IFACE: REMOVE_CRED SP FQDN '%s'",
  2389. cmd + 8);
  2390. cred = wpa_s->conf->cred;
  2391. while (cred) {
  2392. prev = cred;
  2393. cred = cred->next;
  2394. if (prev->domain) {
  2395. size_t i;
  2396. for (i = 0; i < prev->num_domain; i++) {
  2397. if (os_strcmp(prev->domain[i], cmd + 8)
  2398. != 0)
  2399. continue;
  2400. wpas_ctrl_remove_cred(wpa_s, prev);
  2401. break;
  2402. }
  2403. }
  2404. }
  2405. return 0;
  2406. }
  2407. if (os_strncmp(cmd, "provisioning_sp=", 16) == 0) {
  2408. wpa_printf(MSG_DEBUG, "CTRL_IFACE: REMOVE_CRED provisioning SP FQDN '%s'",
  2409. cmd + 16);
  2410. cred = wpa_s->conf->cred;
  2411. while (cred) {
  2412. prev = cred;
  2413. cred = cred->next;
  2414. if (prev->provisioning_sp &&
  2415. os_strcmp(prev->provisioning_sp, cmd + 16) == 0)
  2416. wpas_ctrl_remove_cred(wpa_s, prev);
  2417. }
  2418. return 0;
  2419. }
  2420. id = atoi(cmd);
  2421. wpa_printf(MSG_DEBUG, "CTRL_IFACE: REMOVE_CRED id=%d", id);
  2422. cred = wpa_config_get_cred(wpa_s->conf, id);
  2423. return wpas_ctrl_remove_cred(wpa_s, cred);
  2424. }
  2425. static int wpa_supplicant_ctrl_iface_set_cred(struct wpa_supplicant *wpa_s,
  2426. char *cmd)
  2427. {
  2428. int id;
  2429. struct wpa_cred *cred;
  2430. char *name, *value;
  2431. /* cmd: "<cred id> <variable name> <value>" */
  2432. name = os_strchr(cmd, ' ');
  2433. if (name == NULL)
  2434. return -1;
  2435. *name++ = '\0';
  2436. value = os_strchr(name, ' ');
  2437. if (value == NULL)
  2438. return -1;
  2439. *value++ = '\0';
  2440. id = atoi(cmd);
  2441. wpa_printf(MSG_DEBUG, "CTRL_IFACE: SET_CRED id=%d name='%s'",
  2442. id, name);
  2443. wpa_hexdump_ascii_key(MSG_DEBUG, "CTRL_IFACE: value",
  2444. (u8 *) value, os_strlen(value));
  2445. cred = wpa_config_get_cred(wpa_s->conf, id);
  2446. if (cred == NULL) {
  2447. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find cred id=%d",
  2448. id);
  2449. return -1;
  2450. }
  2451. if (wpa_config_set_cred(cred, name, value, 0) < 0) {
  2452. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Failed to set cred "
  2453. "variable '%s'", name);
  2454. return -1;
  2455. }
  2456. wpa_msg(wpa_s, MSG_INFO, CRED_MODIFIED "%d %s", cred->id, name);
  2457. return 0;
  2458. }
  2459. static int wpa_supplicant_ctrl_iface_get_cred(struct wpa_supplicant *wpa_s,
  2460. char *cmd, char *buf,
  2461. size_t buflen)
  2462. {
  2463. int id;
  2464. size_t res;
  2465. struct wpa_cred *cred;
  2466. char *name, *value;
  2467. /* cmd: "<cred id> <variable name>" */
  2468. name = os_strchr(cmd, ' ');
  2469. if (name == NULL)
  2470. return -1;
  2471. *name++ = '\0';
  2472. id = atoi(cmd);
  2473. wpa_printf(MSG_DEBUG, "CTRL_IFACE: GET_CRED id=%d name='%s'",
  2474. id, name);
  2475. cred = wpa_config_get_cred(wpa_s->conf, id);
  2476. if (cred == NULL) {
  2477. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find cred id=%d",
  2478. id);
  2479. return -1;
  2480. }
  2481. value = wpa_config_get_cred_no_key(cred, name);
  2482. if (value == NULL) {
  2483. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Failed to get cred variable '%s'",
  2484. name);
  2485. return -1;
  2486. }
  2487. res = os_strlcpy(buf, value, buflen);
  2488. if (res >= buflen) {
  2489. os_free(value);
  2490. return -1;
  2491. }
  2492. os_free(value);
  2493. return res;
  2494. }
  2495. #ifndef CONFIG_NO_CONFIG_WRITE
  2496. static int wpa_supplicant_ctrl_iface_save_config(struct wpa_supplicant *wpa_s)
  2497. {
  2498. int ret;
  2499. if (!wpa_s->conf->update_config) {
  2500. wpa_printf(MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Not allowed "
  2501. "to update configuration (update_config=0)");
  2502. return -1;
  2503. }
  2504. ret = wpa_config_write(wpa_s->confname, wpa_s->conf);
  2505. if (ret) {
  2506. wpa_printf(MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Failed to "
  2507. "update configuration");
  2508. } else {
  2509. wpa_printf(MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Configuration"
  2510. " updated");
  2511. }
  2512. return ret;
  2513. }
  2514. #endif /* CONFIG_NO_CONFIG_WRITE */
  2515. struct cipher_info {
  2516. unsigned int capa;
  2517. const char *name;
  2518. int group_only;
  2519. };
  2520. static const struct cipher_info ciphers[] = {
  2521. { WPA_DRIVER_CAPA_ENC_CCMP_256, "CCMP-256", 0 },
  2522. { WPA_DRIVER_CAPA_ENC_GCMP_256, "GCMP-256", 0 },
  2523. { WPA_DRIVER_CAPA_ENC_CCMP, "CCMP", 0 },
  2524. { WPA_DRIVER_CAPA_ENC_GCMP, "GCMP", 0 },
  2525. { WPA_DRIVER_CAPA_ENC_TKIP, "TKIP", 0 },
  2526. { WPA_DRIVER_CAPA_KEY_MGMT_WPA_NONE, "NONE", 0 },
  2527. { WPA_DRIVER_CAPA_ENC_WEP104, "WEP104", 1 },
  2528. { WPA_DRIVER_CAPA_ENC_WEP40, "WEP40", 1 }
  2529. };
  2530. static int ctrl_iface_get_capability_pairwise(int res, char *strict,
  2531. struct wpa_driver_capa *capa,
  2532. char *buf, size_t buflen)
  2533. {
  2534. int ret;
  2535. char *pos, *end;
  2536. size_t len;
  2537. unsigned int i;
  2538. pos = buf;
  2539. end = pos + buflen;
  2540. if (res < 0) {
  2541. if (strict)
  2542. return 0;
  2543. len = os_strlcpy(buf, "CCMP TKIP NONE", buflen);
  2544. if (len >= buflen)
  2545. return -1;
  2546. return len;
  2547. }
  2548. for (i = 0; i < ARRAY_SIZE(ciphers); i++) {
  2549. if (!ciphers[i].group_only && capa->enc & ciphers[i].capa) {
  2550. ret = os_snprintf(pos, end - pos, "%s%s",
  2551. pos == buf ? "" : " ",
  2552. ciphers[i].name);
  2553. if (ret < 0 || ret >= end - pos)
  2554. return pos - buf;
  2555. pos += ret;
  2556. }
  2557. }
  2558. return pos - buf;
  2559. }
  2560. static int ctrl_iface_get_capability_group(int res, char *strict,
  2561. struct wpa_driver_capa *capa,
  2562. char *buf, size_t buflen)
  2563. {
  2564. int ret;
  2565. char *pos, *end;
  2566. size_t len;
  2567. unsigned int i;
  2568. pos = buf;
  2569. end = pos + buflen;
  2570. if (res < 0) {
  2571. if (strict)
  2572. return 0;
  2573. len = os_strlcpy(buf, "CCMP TKIP WEP104 WEP40", buflen);
  2574. if (len >= buflen)
  2575. return -1;
  2576. return len;
  2577. }
  2578. for (i = 0; i < ARRAY_SIZE(ciphers); i++) {
  2579. if (capa->enc & ciphers[i].capa) {
  2580. ret = os_snprintf(pos, end - pos, "%s%s",
  2581. pos == buf ? "" : " ",
  2582. ciphers[i].name);
  2583. if (ret < 0 || ret >= end - pos)
  2584. return pos - buf;
  2585. pos += ret;
  2586. }
  2587. }
  2588. return pos - buf;
  2589. }
  2590. static int ctrl_iface_get_capability_key_mgmt(int res, char *strict,
  2591. struct wpa_driver_capa *capa,
  2592. char *buf, size_t buflen)
  2593. {
  2594. int ret;
  2595. char *pos, *end;
  2596. size_t len;
  2597. pos = buf;
  2598. end = pos + buflen;
  2599. if (res < 0) {
  2600. if (strict)
  2601. return 0;
  2602. len = os_strlcpy(buf, "WPA-PSK WPA-EAP IEEE8021X WPA-NONE "
  2603. "NONE", buflen);
  2604. if (len >= buflen)
  2605. return -1;
  2606. return len;
  2607. }
  2608. ret = os_snprintf(pos, end - pos, "NONE IEEE8021X");
  2609. if (ret < 0 || ret >= end - pos)
  2610. return pos - buf;
  2611. pos += ret;
  2612. if (capa->key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA |
  2613. WPA_DRIVER_CAPA_KEY_MGMT_WPA2)) {
  2614. ret = os_snprintf(pos, end - pos, " WPA-EAP");
  2615. if (ret < 0 || ret >= end - pos)
  2616. return pos - buf;
  2617. pos += ret;
  2618. }
  2619. if (capa->key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA_PSK |
  2620. WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK)) {
  2621. ret = os_snprintf(pos, end - pos, " WPA-PSK");
  2622. if (ret < 0 || ret >= end - pos)
  2623. return pos - buf;
  2624. pos += ret;
  2625. }
  2626. if (capa->key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_WPA_NONE) {
  2627. ret = os_snprintf(pos, end - pos, " WPA-NONE");
  2628. if (ret < 0 || ret >= end - pos)
  2629. return pos - buf;
  2630. pos += ret;
  2631. }
  2632. return pos - buf;
  2633. }
  2634. static int ctrl_iface_get_capability_proto(int res, char *strict,
  2635. struct wpa_driver_capa *capa,
  2636. char *buf, size_t buflen)
  2637. {
  2638. int ret;
  2639. char *pos, *end;
  2640. size_t len;
  2641. pos = buf;
  2642. end = pos + buflen;
  2643. if (res < 0) {
  2644. if (strict)
  2645. return 0;
  2646. len = os_strlcpy(buf, "RSN WPA", buflen);
  2647. if (len >= buflen)
  2648. return -1;
  2649. return len;
  2650. }
  2651. if (capa->key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA2 |
  2652. WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK)) {
  2653. ret = os_snprintf(pos, end - pos, "%sRSN",
  2654. pos == buf ? "" : " ");
  2655. if (ret < 0 || ret >= end - pos)
  2656. return pos - buf;
  2657. pos += ret;
  2658. }
  2659. if (capa->key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA |
  2660. WPA_DRIVER_CAPA_KEY_MGMT_WPA_PSK)) {
  2661. ret = os_snprintf(pos, end - pos, "%sWPA",
  2662. pos == buf ? "" : " ");
  2663. if (ret < 0 || ret >= end - pos)
  2664. return pos - buf;
  2665. pos += ret;
  2666. }
  2667. return pos - buf;
  2668. }
  2669. static int ctrl_iface_get_capability_auth_alg(int res, char *strict,
  2670. struct wpa_driver_capa *capa,
  2671. char *buf, size_t buflen)
  2672. {
  2673. int ret;
  2674. char *pos, *end;
  2675. size_t len;
  2676. pos = buf;
  2677. end = pos + buflen;
  2678. if (res < 0) {
  2679. if (strict)
  2680. return 0;
  2681. len = os_strlcpy(buf, "OPEN SHARED LEAP", buflen);
  2682. if (len >= buflen)
  2683. return -1;
  2684. return len;
  2685. }
  2686. if (capa->auth & (WPA_DRIVER_AUTH_OPEN)) {
  2687. ret = os_snprintf(pos, end - pos, "%sOPEN",
  2688. pos == buf ? "" : " ");
  2689. if (ret < 0 || ret >= end - pos)
  2690. return pos - buf;
  2691. pos += ret;
  2692. }
  2693. if (capa->auth & (WPA_DRIVER_AUTH_SHARED)) {
  2694. ret = os_snprintf(pos, end - pos, "%sSHARED",
  2695. pos == buf ? "" : " ");
  2696. if (ret < 0 || ret >= end - pos)
  2697. return pos - buf;
  2698. pos += ret;
  2699. }
  2700. if (capa->auth & (WPA_DRIVER_AUTH_LEAP)) {
  2701. ret = os_snprintf(pos, end - pos, "%sLEAP",
  2702. pos == buf ? "" : " ");
  2703. if (ret < 0 || ret >= end - pos)
  2704. return pos - buf;
  2705. pos += ret;
  2706. }
  2707. return pos - buf;
  2708. }
  2709. static int ctrl_iface_get_capability_modes(int res, char *strict,
  2710. struct wpa_driver_capa *capa,
  2711. char *buf, size_t buflen)
  2712. {
  2713. int ret;
  2714. char *pos, *end;
  2715. size_t len;
  2716. pos = buf;
  2717. end = pos + buflen;
  2718. if (res < 0) {
  2719. if (strict)
  2720. return 0;
  2721. len = os_strlcpy(buf, "IBSS AP", buflen);
  2722. if (len >= buflen)
  2723. return -1;
  2724. return len;
  2725. }
  2726. if (capa->flags & WPA_DRIVER_FLAGS_IBSS) {
  2727. ret = os_snprintf(pos, end - pos, "%sIBSS",
  2728. pos == buf ? "" : " ");
  2729. if (ret < 0 || ret >= end - pos)
  2730. return pos - buf;
  2731. pos += ret;
  2732. }
  2733. if (capa->flags & WPA_DRIVER_FLAGS_AP) {
  2734. ret = os_snprintf(pos, end - pos, "%sAP",
  2735. pos == buf ? "" : " ");
  2736. if (ret < 0 || ret >= end - pos)
  2737. return pos - buf;
  2738. pos += ret;
  2739. }
  2740. return pos - buf;
  2741. }
  2742. static int ctrl_iface_get_capability_channels(struct wpa_supplicant *wpa_s,
  2743. char *buf, size_t buflen)
  2744. {
  2745. struct hostapd_channel_data *chnl;
  2746. int ret, i, j;
  2747. char *pos, *end, *hmode;
  2748. pos = buf;
  2749. end = pos + buflen;
  2750. for (j = 0; j < wpa_s->hw.num_modes; j++) {
  2751. switch (wpa_s->hw.modes[j].mode) {
  2752. case HOSTAPD_MODE_IEEE80211B:
  2753. hmode = "B";
  2754. break;
  2755. case HOSTAPD_MODE_IEEE80211G:
  2756. hmode = "G";
  2757. break;
  2758. case HOSTAPD_MODE_IEEE80211A:
  2759. hmode = "A";
  2760. break;
  2761. case HOSTAPD_MODE_IEEE80211AD:
  2762. hmode = "AD";
  2763. break;
  2764. default:
  2765. continue;
  2766. }
  2767. ret = os_snprintf(pos, end - pos, "Mode[%s] Channels:", hmode);
  2768. if (ret < 0 || ret >= end - pos)
  2769. return pos - buf;
  2770. pos += ret;
  2771. chnl = wpa_s->hw.modes[j].channels;
  2772. for (i = 0; i < wpa_s->hw.modes[j].num_channels; i++) {
  2773. if (chnl[i].flag & HOSTAPD_CHAN_DISABLED)
  2774. continue;
  2775. ret = os_snprintf(pos, end - pos, " %d", chnl[i].chan);
  2776. if (ret < 0 || ret >= end - pos)
  2777. return pos - buf;
  2778. pos += ret;
  2779. }
  2780. ret = os_snprintf(pos, end - pos, "\n");
  2781. if (ret < 0 || ret >= end - pos)
  2782. return pos - buf;
  2783. pos += ret;
  2784. }
  2785. return pos - buf;
  2786. }
  2787. static int ctrl_iface_get_capability_freq(struct wpa_supplicant *wpa_s,
  2788. char *buf, size_t buflen)
  2789. {
  2790. struct hostapd_channel_data *chnl;
  2791. int ret, i, j;
  2792. char *pos, *end, *hmode;
  2793. pos = buf;
  2794. end = pos + buflen;
  2795. for (j = 0; j < wpa_s->hw.num_modes; j++) {
  2796. switch (wpa_s->hw.modes[j].mode) {
  2797. case HOSTAPD_MODE_IEEE80211B:
  2798. hmode = "B";
  2799. break;
  2800. case HOSTAPD_MODE_IEEE80211G:
  2801. hmode = "G";
  2802. break;
  2803. case HOSTAPD_MODE_IEEE80211A:
  2804. hmode = "A";
  2805. break;
  2806. case HOSTAPD_MODE_IEEE80211AD:
  2807. hmode = "AD";
  2808. break;
  2809. default:
  2810. continue;
  2811. }
  2812. ret = os_snprintf(pos, end - pos, "Mode[%s] Channels:\n",
  2813. hmode);
  2814. if (ret < 0 || ret >= end - pos)
  2815. return pos - buf;
  2816. pos += ret;
  2817. chnl = wpa_s->hw.modes[j].channels;
  2818. for (i = 0; i < wpa_s->hw.modes[j].num_channels; i++) {
  2819. if (chnl[i].flag & HOSTAPD_CHAN_DISABLED)
  2820. continue;
  2821. ret = os_snprintf(pos, end - pos, " %d = %d MHz%s%s\n",
  2822. chnl[i].chan, chnl[i].freq,
  2823. chnl[i].flag & HOSTAPD_CHAN_NO_IBSS ?
  2824. " (NO_IBSS)" : "",
  2825. chnl[i].flag & HOSTAPD_CHAN_RADAR ?
  2826. " (DFS)" : "");
  2827. if (ret < 0 || ret >= end - pos)
  2828. return pos - buf;
  2829. pos += ret;
  2830. }
  2831. ret = os_snprintf(pos, end - pos, "\n");
  2832. if (ret < 0 || ret >= end - pos)
  2833. return pos - buf;
  2834. pos += ret;
  2835. }
  2836. return pos - buf;
  2837. }
  2838. static int wpa_supplicant_ctrl_iface_get_capability(
  2839. struct wpa_supplicant *wpa_s, const char *_field, char *buf,
  2840. size_t buflen)
  2841. {
  2842. struct wpa_driver_capa capa;
  2843. int res;
  2844. char *strict;
  2845. char field[30];
  2846. size_t len;
  2847. /* Determine whether or not strict checking was requested */
  2848. len = os_strlcpy(field, _field, sizeof(field));
  2849. if (len >= sizeof(field))
  2850. return -1;
  2851. strict = os_strchr(field, ' ');
  2852. if (strict != NULL) {
  2853. *strict++ = '\0';
  2854. if (os_strcmp(strict, "strict") != 0)
  2855. return -1;
  2856. }
  2857. wpa_printf(MSG_DEBUG, "CTRL_IFACE: GET_CAPABILITY '%s' %s",
  2858. field, strict ? strict : "");
  2859. if (os_strcmp(field, "eap") == 0) {
  2860. return eap_get_names(buf, buflen);
  2861. }
  2862. res = wpa_drv_get_capa(wpa_s, &capa);
  2863. if (os_strcmp(field, "pairwise") == 0)
  2864. return ctrl_iface_get_capability_pairwise(res, strict, &capa,
  2865. buf, buflen);
  2866. if (os_strcmp(field, "group") == 0)
  2867. return ctrl_iface_get_capability_group(res, strict, &capa,
  2868. buf, buflen);
  2869. if (os_strcmp(field, "key_mgmt") == 0)
  2870. return ctrl_iface_get_capability_key_mgmt(res, strict, &capa,
  2871. buf, buflen);
  2872. if (os_strcmp(field, "proto") == 0)
  2873. return ctrl_iface_get_capability_proto(res, strict, &capa,
  2874. buf, buflen);
  2875. if (os_strcmp(field, "auth_alg") == 0)
  2876. return ctrl_iface_get_capability_auth_alg(res, strict, &capa,
  2877. buf, buflen);
  2878. if (os_strcmp(field, "modes") == 0)
  2879. return ctrl_iface_get_capability_modes(res, strict, &capa,
  2880. buf, buflen);
  2881. if (os_strcmp(field, "channels") == 0)
  2882. return ctrl_iface_get_capability_channels(wpa_s, buf, buflen);
  2883. if (os_strcmp(field, "freq") == 0)
  2884. return ctrl_iface_get_capability_freq(wpa_s, buf, buflen);
  2885. #ifdef CONFIG_TDLS
  2886. if (os_strcmp(field, "tdls") == 0)
  2887. return ctrl_iface_get_capability_tdls(wpa_s, buf, buflen);
  2888. #endif /* CONFIG_TDLS */
  2889. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Unknown GET_CAPABILITY field '%s'",
  2890. field);
  2891. return -1;
  2892. }
  2893. #ifdef CONFIG_INTERWORKING
  2894. static char * anqp_add_hex(char *pos, char *end, const char *title,
  2895. struct wpabuf *data)
  2896. {
  2897. char *start = pos;
  2898. size_t i;
  2899. int ret;
  2900. const u8 *d;
  2901. if (data == NULL)
  2902. return start;
  2903. ret = os_snprintf(pos, end - pos, "%s=", title);
  2904. if (ret < 0 || ret >= end - pos)
  2905. return start;
  2906. pos += ret;
  2907. d = wpabuf_head_u8(data);
  2908. for (i = 0; i < wpabuf_len(data); i++) {
  2909. ret = os_snprintf(pos, end - pos, "%02x", *d++);
  2910. if (ret < 0 || ret >= end - pos)
  2911. return start;
  2912. pos += ret;
  2913. }
  2914. ret = os_snprintf(pos, end - pos, "\n");
  2915. if (ret < 0 || ret >= end - pos)
  2916. return start;
  2917. pos += ret;
  2918. return pos;
  2919. }
  2920. #endif /* CONFIG_INTERWORKING */
  2921. static int print_bss_info(struct wpa_supplicant *wpa_s, struct wpa_bss *bss,
  2922. unsigned long mask, char *buf, size_t buflen)
  2923. {
  2924. size_t i;
  2925. int ret;
  2926. char *pos, *end;
  2927. const u8 *ie, *ie2;
  2928. pos = buf;
  2929. end = buf + buflen;
  2930. if (mask & WPA_BSS_MASK_ID) {
  2931. ret = os_snprintf(pos, end - pos, "id=%u\n", bss->id);
  2932. if (ret < 0 || ret >= end - pos)
  2933. return 0;
  2934. pos += ret;
  2935. }
  2936. if (mask & WPA_BSS_MASK_BSSID) {
  2937. ret = os_snprintf(pos, end - pos, "bssid=" MACSTR "\n",
  2938. MAC2STR(bss->bssid));
  2939. if (ret < 0 || ret >= end - pos)
  2940. return 0;
  2941. pos += ret;
  2942. }
  2943. if (mask & WPA_BSS_MASK_FREQ) {
  2944. ret = os_snprintf(pos, end - pos, "freq=%d\n", bss->freq);
  2945. if (ret < 0 || ret >= end - pos)
  2946. return 0;
  2947. pos += ret;
  2948. }
  2949. if (mask & WPA_BSS_MASK_BEACON_INT) {
  2950. ret = os_snprintf(pos, end - pos, "beacon_int=%d\n",
  2951. bss->beacon_int);
  2952. if (ret < 0 || ret >= end - pos)
  2953. return 0;
  2954. pos += ret;
  2955. }
  2956. if (mask & WPA_BSS_MASK_CAPABILITIES) {
  2957. ret = os_snprintf(pos, end - pos, "capabilities=0x%04x\n",
  2958. bss->caps);
  2959. if (ret < 0 || ret >= end - pos)
  2960. return 0;
  2961. pos += ret;
  2962. }
  2963. if (mask & WPA_BSS_MASK_QUAL) {
  2964. ret = os_snprintf(pos, end - pos, "qual=%d\n", bss->qual);
  2965. if (ret < 0 || ret >= end - pos)
  2966. return 0;
  2967. pos += ret;
  2968. }
  2969. if (mask & WPA_BSS_MASK_NOISE) {
  2970. ret = os_snprintf(pos, end - pos, "noise=%d\n", bss->noise);
  2971. if (ret < 0 || ret >= end - pos)
  2972. return 0;
  2973. pos += ret;
  2974. }
  2975. if (mask & WPA_BSS_MASK_LEVEL) {
  2976. ret = os_snprintf(pos, end - pos, "level=%d\n", bss->level);
  2977. if (ret < 0 || ret >= end - pos)
  2978. return 0;
  2979. pos += ret;
  2980. }
  2981. if (mask & WPA_BSS_MASK_TSF) {
  2982. ret = os_snprintf(pos, end - pos, "tsf=%016llu\n",
  2983. (unsigned long long) bss->tsf);
  2984. if (ret < 0 || ret >= end - pos)
  2985. return 0;
  2986. pos += ret;
  2987. }
  2988. if (mask & WPA_BSS_MASK_AGE) {
  2989. struct os_reltime now;
  2990. os_get_reltime(&now);
  2991. ret = os_snprintf(pos, end - pos, "age=%d\n",
  2992. (int) (now.sec - bss->last_update.sec));
  2993. if (ret < 0 || ret >= end - pos)
  2994. return 0;
  2995. pos += ret;
  2996. }
  2997. if (mask & WPA_BSS_MASK_IE) {
  2998. ret = os_snprintf(pos, end - pos, "ie=");
  2999. if (ret < 0 || ret >= end - pos)
  3000. return 0;
  3001. pos += ret;
  3002. ie = (const u8 *) (bss + 1);
  3003. for (i = 0; i < bss->ie_len; i++) {
  3004. ret = os_snprintf(pos, end - pos, "%02x", *ie++);
  3005. if (ret < 0 || ret >= end - pos)
  3006. return 0;
  3007. pos += ret;
  3008. }
  3009. ret = os_snprintf(pos, end - pos, "\n");
  3010. if (ret < 0 || ret >= end - pos)
  3011. return 0;
  3012. pos += ret;
  3013. }
  3014. if (mask & WPA_BSS_MASK_FLAGS) {
  3015. ret = os_snprintf(pos, end - pos, "flags=");
  3016. if (ret < 0 || ret >= end - pos)
  3017. return 0;
  3018. pos += ret;
  3019. ie = wpa_bss_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
  3020. if (ie)
  3021. pos = wpa_supplicant_ie_txt(pos, end, "WPA", ie,
  3022. 2 + ie[1]);
  3023. ie2 = wpa_bss_get_ie(bss, WLAN_EID_RSN);
  3024. if (ie2)
  3025. pos = wpa_supplicant_ie_txt(pos, end, "WPA2", ie2,
  3026. 2 + ie2[1]);
  3027. pos = wpa_supplicant_wps_ie_txt(wpa_s, pos, end, bss);
  3028. if (!ie && !ie2 && bss->caps & IEEE80211_CAP_PRIVACY) {
  3029. ret = os_snprintf(pos, end - pos, "[WEP]");
  3030. if (ret < 0 || ret >= end - pos)
  3031. return 0;
  3032. pos += ret;
  3033. }
  3034. if (bss_is_dmg(bss)) {
  3035. const char *s;
  3036. ret = os_snprintf(pos, end - pos, "[DMG]");
  3037. if (ret < 0 || ret >= end - pos)
  3038. return 0;
  3039. pos += ret;
  3040. switch (bss->caps & IEEE80211_CAP_DMG_MASK) {
  3041. case IEEE80211_CAP_DMG_IBSS:
  3042. s = "[IBSS]";
  3043. break;
  3044. case IEEE80211_CAP_DMG_AP:
  3045. s = "[ESS]";
  3046. break;
  3047. case IEEE80211_CAP_DMG_PBSS:
  3048. s = "[PBSS]";
  3049. break;
  3050. default:
  3051. s = "";
  3052. break;
  3053. }
  3054. ret = os_snprintf(pos, end - pos, "%s", s);
  3055. if (ret < 0 || ret >= end - pos)
  3056. return 0;
  3057. pos += ret;
  3058. } else {
  3059. if (bss->caps & IEEE80211_CAP_IBSS) {
  3060. ret = os_snprintf(pos, end - pos, "[IBSS]");
  3061. if (ret < 0 || ret >= end - pos)
  3062. return 0;
  3063. pos += ret;
  3064. }
  3065. if (bss->caps & IEEE80211_CAP_ESS) {
  3066. ret = os_snprintf(pos, end - pos, "[ESS]");
  3067. if (ret < 0 || ret >= end - pos)
  3068. return 0;
  3069. pos += ret;
  3070. }
  3071. }
  3072. if (wpa_bss_get_vendor_ie(bss, P2P_IE_VENDOR_TYPE) ||
  3073. wpa_bss_get_vendor_ie_beacon(bss, P2P_IE_VENDOR_TYPE)) {
  3074. ret = os_snprintf(pos, end - pos, "[P2P]");
  3075. if (ret < 0 || ret >= end - pos)
  3076. return 0;
  3077. pos += ret;
  3078. }
  3079. #ifdef CONFIG_HS20
  3080. if (wpa_bss_get_vendor_ie(bss, HS20_IE_VENDOR_TYPE)) {
  3081. ret = os_snprintf(pos, end - pos, "[HS20]");
  3082. if (ret < 0 || ret >= end - pos)
  3083. return 0;
  3084. pos += ret;
  3085. }
  3086. #endif /* CONFIG_HS20 */
  3087. ret = os_snprintf(pos, end - pos, "\n");
  3088. if (ret < 0 || ret >= end - pos)
  3089. return 0;
  3090. pos += ret;
  3091. }
  3092. if (mask & WPA_BSS_MASK_SSID) {
  3093. ret = os_snprintf(pos, end - pos, "ssid=%s\n",
  3094. wpa_ssid_txt(bss->ssid, bss->ssid_len));
  3095. if (ret < 0 || ret >= end - pos)
  3096. return 0;
  3097. pos += ret;
  3098. }
  3099. #ifdef CONFIG_WPS
  3100. if (mask & WPA_BSS_MASK_WPS_SCAN) {
  3101. ie = (const u8 *) (bss + 1);
  3102. ret = wpas_wps_scan_result_text(ie, bss->ie_len, pos, end);
  3103. if (ret < 0 || ret >= end - pos)
  3104. return 0;
  3105. pos += ret;
  3106. }
  3107. #endif /* CONFIG_WPS */
  3108. #ifdef CONFIG_P2P
  3109. if (mask & WPA_BSS_MASK_P2P_SCAN) {
  3110. ie = (const u8 *) (bss + 1);
  3111. ret = wpas_p2p_scan_result_text(ie, bss->ie_len, pos, end);
  3112. if (ret < 0 || ret >= end - pos)
  3113. return 0;
  3114. pos += ret;
  3115. }
  3116. #endif /* CONFIG_P2P */
  3117. #ifdef CONFIG_WIFI_DISPLAY
  3118. if (mask & WPA_BSS_MASK_WIFI_DISPLAY) {
  3119. struct wpabuf *wfd;
  3120. ie = (const u8 *) (bss + 1);
  3121. wfd = ieee802_11_vendor_ie_concat(ie, bss->ie_len,
  3122. WFD_IE_VENDOR_TYPE);
  3123. if (wfd) {
  3124. ret = os_snprintf(pos, end - pos, "wfd_subelems=");
  3125. if (ret < 0 || ret >= end - pos) {
  3126. wpabuf_free(wfd);
  3127. return 0;
  3128. }
  3129. pos += ret;
  3130. pos += wpa_snprintf_hex(pos, end - pos,
  3131. wpabuf_head(wfd),
  3132. wpabuf_len(wfd));
  3133. wpabuf_free(wfd);
  3134. ret = os_snprintf(pos, end - pos, "\n");
  3135. if (ret < 0 || ret >= end - pos)
  3136. return 0;
  3137. pos += ret;
  3138. }
  3139. }
  3140. #endif /* CONFIG_WIFI_DISPLAY */
  3141. #ifdef CONFIG_INTERWORKING
  3142. if ((mask & WPA_BSS_MASK_INTERNETW) && bss->anqp) {
  3143. struct wpa_bss_anqp *anqp = bss->anqp;
  3144. pos = anqp_add_hex(pos, end, "anqp_venue_name",
  3145. anqp->venue_name);
  3146. pos = anqp_add_hex(pos, end, "anqp_network_auth_type",
  3147. anqp->network_auth_type);
  3148. pos = anqp_add_hex(pos, end, "anqp_roaming_consortium",
  3149. anqp->roaming_consortium);
  3150. pos = anqp_add_hex(pos, end, "anqp_ip_addr_type_availability",
  3151. anqp->ip_addr_type_availability);
  3152. pos = anqp_add_hex(pos, end, "anqp_nai_realm",
  3153. anqp->nai_realm);
  3154. pos = anqp_add_hex(pos, end, "anqp_3gpp", anqp->anqp_3gpp);
  3155. pos = anqp_add_hex(pos, end, "anqp_domain_name",
  3156. anqp->domain_name);
  3157. #ifdef CONFIG_HS20
  3158. pos = anqp_add_hex(pos, end, "hs20_operator_friendly_name",
  3159. anqp->hs20_operator_friendly_name);
  3160. pos = anqp_add_hex(pos, end, "hs20_wan_metrics",
  3161. anqp->hs20_wan_metrics);
  3162. pos = anqp_add_hex(pos, end, "hs20_connection_capability",
  3163. anqp->hs20_connection_capability);
  3164. pos = anqp_add_hex(pos, end, "hs20_operating_class",
  3165. anqp->hs20_operating_class);
  3166. pos = anqp_add_hex(pos, end, "hs20_osu_providers_list",
  3167. anqp->hs20_osu_providers_list);
  3168. #endif /* CONFIG_HS20 */
  3169. }
  3170. #endif /* CONFIG_INTERWORKING */
  3171. if (mask & WPA_BSS_MASK_DELIM) {
  3172. ret = os_snprintf(pos, end - pos, "====\n");
  3173. if (ret < 0 || ret >= end - pos)
  3174. return 0;
  3175. pos += ret;
  3176. }
  3177. return pos - buf;
  3178. }
  3179. static int wpa_supplicant_ctrl_iface_bss(struct wpa_supplicant *wpa_s,
  3180. const char *cmd, char *buf,
  3181. size_t buflen)
  3182. {
  3183. u8 bssid[ETH_ALEN];
  3184. size_t i;
  3185. struct wpa_bss *bss;
  3186. struct wpa_bss *bsslast = NULL;
  3187. struct dl_list *next;
  3188. int ret = 0;
  3189. int len;
  3190. char *ctmp;
  3191. unsigned long mask = WPA_BSS_MASK_ALL;
  3192. if (os_strncmp(cmd, "RANGE=", 6) == 0) {
  3193. if (os_strncmp(cmd + 6, "ALL", 3) == 0) {
  3194. bss = dl_list_first(&wpa_s->bss_id, struct wpa_bss,
  3195. list_id);
  3196. bsslast = dl_list_last(&wpa_s->bss_id, struct wpa_bss,
  3197. list_id);
  3198. } else { /* N1-N2 */
  3199. unsigned int id1, id2;
  3200. if ((ctmp = os_strchr(cmd + 6, '-')) == NULL) {
  3201. wpa_printf(MSG_INFO, "Wrong BSS range "
  3202. "format");
  3203. return 0;
  3204. }
  3205. if (*(cmd + 6) == '-')
  3206. id1 = 0;
  3207. else
  3208. id1 = atoi(cmd + 6);
  3209. ctmp++;
  3210. if (*ctmp >= '0' && *ctmp <= '9')
  3211. id2 = atoi(ctmp);
  3212. else
  3213. id2 = (unsigned int) -1;
  3214. bss = wpa_bss_get_id_range(wpa_s, id1, id2);
  3215. if (id2 == (unsigned int) -1)
  3216. bsslast = dl_list_last(&wpa_s->bss_id,
  3217. struct wpa_bss,
  3218. list_id);
  3219. else {
  3220. bsslast = wpa_bss_get_id(wpa_s, id2);
  3221. if (bsslast == NULL && bss && id2 > id1) {
  3222. struct wpa_bss *tmp = bss;
  3223. for (;;) {
  3224. next = tmp->list_id.next;
  3225. if (next == &wpa_s->bss_id)
  3226. break;
  3227. tmp = dl_list_entry(
  3228. next, struct wpa_bss,
  3229. list_id);
  3230. if (tmp->id > id2)
  3231. break;
  3232. bsslast = tmp;
  3233. }
  3234. }
  3235. }
  3236. }
  3237. } else if (os_strncmp(cmd, "FIRST", 5) == 0)
  3238. bss = dl_list_first(&wpa_s->bss_id, struct wpa_bss, list_id);
  3239. else if (os_strncmp(cmd, "LAST", 4) == 0)
  3240. bss = dl_list_last(&wpa_s->bss_id, struct wpa_bss, list_id);
  3241. else if (os_strncmp(cmd, "ID-", 3) == 0) {
  3242. i = atoi(cmd + 3);
  3243. bss = wpa_bss_get_id(wpa_s, i);
  3244. } else if (os_strncmp(cmd, "NEXT-", 5) == 0) {
  3245. i = atoi(cmd + 5);
  3246. bss = wpa_bss_get_id(wpa_s, i);
  3247. if (bss) {
  3248. next = bss->list_id.next;
  3249. if (next == &wpa_s->bss_id)
  3250. bss = NULL;
  3251. else
  3252. bss = dl_list_entry(next, struct wpa_bss,
  3253. list_id);
  3254. }
  3255. #ifdef CONFIG_P2P
  3256. } else if (os_strncmp(cmd, "p2p_dev_addr=", 13) == 0) {
  3257. if (hwaddr_aton(cmd + 13, bssid) == 0)
  3258. bss = wpa_bss_get_p2p_dev_addr(wpa_s, bssid);
  3259. else
  3260. bss = NULL;
  3261. #endif /* CONFIG_P2P */
  3262. } else if (hwaddr_aton(cmd, bssid) == 0)
  3263. bss = wpa_bss_get_bssid(wpa_s, bssid);
  3264. else {
  3265. struct wpa_bss *tmp;
  3266. i = atoi(cmd);
  3267. bss = NULL;
  3268. dl_list_for_each(tmp, &wpa_s->bss_id, struct wpa_bss, list_id)
  3269. {
  3270. if (i-- == 0) {
  3271. bss = tmp;
  3272. break;
  3273. }
  3274. }
  3275. }
  3276. if ((ctmp = os_strstr(cmd, "MASK=")) != NULL) {
  3277. mask = strtoul(ctmp + 5, NULL, 0x10);
  3278. if (mask == 0)
  3279. mask = WPA_BSS_MASK_ALL;
  3280. }
  3281. if (bss == NULL)
  3282. return 0;
  3283. if (bsslast == NULL)
  3284. bsslast = bss;
  3285. do {
  3286. len = print_bss_info(wpa_s, bss, mask, buf, buflen);
  3287. ret += len;
  3288. buf += len;
  3289. buflen -= len;
  3290. if (bss == bsslast) {
  3291. if ((mask & WPA_BSS_MASK_DELIM) && len &&
  3292. (bss == dl_list_last(&wpa_s->bss_id,
  3293. struct wpa_bss, list_id)))
  3294. os_snprintf(buf - 5, 5, "####\n");
  3295. break;
  3296. }
  3297. next = bss->list_id.next;
  3298. if (next == &wpa_s->bss_id)
  3299. break;
  3300. bss = dl_list_entry(next, struct wpa_bss, list_id);
  3301. } while (bss && len);
  3302. return ret;
  3303. }
  3304. static int wpa_supplicant_ctrl_iface_ap_scan(
  3305. struct wpa_supplicant *wpa_s, char *cmd)
  3306. {
  3307. int ap_scan = atoi(cmd);
  3308. return wpa_supplicant_set_ap_scan(wpa_s, ap_scan);
  3309. }
  3310. static int wpa_supplicant_ctrl_iface_scan_interval(
  3311. struct wpa_supplicant *wpa_s, char *cmd)
  3312. {
  3313. int scan_int = atoi(cmd);
  3314. return wpa_supplicant_set_scan_interval(wpa_s, scan_int);
  3315. }
  3316. static int wpa_supplicant_ctrl_iface_bss_expire_age(
  3317. struct wpa_supplicant *wpa_s, char *cmd)
  3318. {
  3319. int expire_age = atoi(cmd);
  3320. return wpa_supplicant_set_bss_expiration_age(wpa_s, expire_age);
  3321. }
  3322. static int wpa_supplicant_ctrl_iface_bss_expire_count(
  3323. struct wpa_supplicant *wpa_s, char *cmd)
  3324. {
  3325. int expire_count = atoi(cmd);
  3326. return wpa_supplicant_set_bss_expiration_count(wpa_s, expire_count);
  3327. }
  3328. static int wpa_supplicant_ctrl_iface_bss_flush(
  3329. struct wpa_supplicant *wpa_s, char *cmd)
  3330. {
  3331. int flush_age = atoi(cmd);
  3332. if (flush_age == 0)
  3333. wpa_bss_flush(wpa_s);
  3334. else
  3335. wpa_bss_flush_by_age(wpa_s, flush_age);
  3336. return 0;
  3337. }
  3338. #ifdef CONFIG_TESTING_OPTIONS
  3339. static void wpa_supplicant_ctrl_iface_drop_sa(struct wpa_supplicant *wpa_s)
  3340. {
  3341. wpa_printf(MSG_DEBUG, "Dropping SA without deauthentication");
  3342. /* MLME-DELETEKEYS.request */
  3343. wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 0, 0, NULL, 0, NULL, 0);
  3344. wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 1, 0, NULL, 0, NULL, 0);
  3345. wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 2, 0, NULL, 0, NULL, 0);
  3346. wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 3, 0, NULL, 0, NULL, 0);
  3347. #ifdef CONFIG_IEEE80211W
  3348. wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 4, 0, NULL, 0, NULL, 0);
  3349. wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 5, 0, NULL, 0, NULL, 0);
  3350. #endif /* CONFIG_IEEE80211W */
  3351. wpa_drv_set_key(wpa_s, WPA_ALG_NONE, wpa_s->bssid, 0, 0, NULL, 0, NULL,
  3352. 0);
  3353. /* MLME-SETPROTECTION.request(None) */
  3354. wpa_drv_mlme_setprotection(wpa_s, wpa_s->bssid,
  3355. MLME_SETPROTECTION_PROTECT_TYPE_NONE,
  3356. MLME_SETPROTECTION_KEY_TYPE_PAIRWISE);
  3357. wpa_sm_drop_sa(wpa_s->wpa);
  3358. }
  3359. #endif /* CONFIG_TESTING_OPTIONS */
  3360. static int wpa_supplicant_ctrl_iface_roam(struct wpa_supplicant *wpa_s,
  3361. char *addr)
  3362. {
  3363. #ifdef CONFIG_NO_SCAN_PROCESSING
  3364. return -1;
  3365. #else /* CONFIG_NO_SCAN_PROCESSING */
  3366. u8 bssid[ETH_ALEN];
  3367. struct wpa_bss *bss;
  3368. struct wpa_ssid *ssid = wpa_s->current_ssid;
  3369. if (hwaddr_aton(addr, bssid)) {
  3370. wpa_printf(MSG_DEBUG, "CTRL_IFACE ROAM: invalid "
  3371. "address '%s'", addr);
  3372. return -1;
  3373. }
  3374. wpa_printf(MSG_DEBUG, "CTRL_IFACE ROAM " MACSTR, MAC2STR(bssid));
  3375. if (!ssid) {
  3376. wpa_printf(MSG_DEBUG, "CTRL_IFACE ROAM: No network "
  3377. "configuration known for the target AP");
  3378. return -1;
  3379. }
  3380. bss = wpa_bss_get(wpa_s, bssid, ssid->ssid, ssid->ssid_len);
  3381. if (!bss) {
  3382. wpa_printf(MSG_DEBUG, "CTRL_IFACE ROAM: Target AP not found "
  3383. "from BSS table");
  3384. return -1;
  3385. }
  3386. /*
  3387. * TODO: Find best network configuration block from configuration to
  3388. * allow roaming to other networks
  3389. */
  3390. wpa_s->reassociate = 1;
  3391. wpa_supplicant_connect(wpa_s, bss, ssid);
  3392. return 0;
  3393. #endif /* CONFIG_NO_SCAN_PROCESSING */
  3394. }
  3395. #ifdef CONFIG_P2P
  3396. static int p2p_ctrl_find(struct wpa_supplicant *wpa_s, char *cmd)
  3397. {
  3398. unsigned int timeout = atoi(cmd);
  3399. enum p2p_discovery_type type = P2P_FIND_START_WITH_FULL;
  3400. u8 dev_id[ETH_ALEN], *_dev_id = NULL;
  3401. u8 dev_type[WPS_DEV_TYPE_LEN], *_dev_type = NULL;
  3402. char *pos;
  3403. unsigned int search_delay;
  3404. if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED) {
  3405. wpa_dbg(wpa_s, MSG_INFO,
  3406. "Reject P2P_FIND since interface is disabled");
  3407. return -1;
  3408. }
  3409. if (os_strstr(cmd, "type=social"))
  3410. type = P2P_FIND_ONLY_SOCIAL;
  3411. else if (os_strstr(cmd, "type=progressive"))
  3412. type = P2P_FIND_PROGRESSIVE;
  3413. pos = os_strstr(cmd, "dev_id=");
  3414. if (pos) {
  3415. pos += 7;
  3416. if (hwaddr_aton(pos, dev_id))
  3417. return -1;
  3418. _dev_id = dev_id;
  3419. }
  3420. pos = os_strstr(cmd, "dev_type=");
  3421. if (pos) {
  3422. pos += 9;
  3423. if (wps_dev_type_str2bin(pos, dev_type) < 0)
  3424. return -1;
  3425. _dev_type = dev_type;
  3426. }
  3427. pos = os_strstr(cmd, "delay=");
  3428. if (pos) {
  3429. pos += 6;
  3430. search_delay = atoi(pos);
  3431. } else
  3432. search_delay = wpas_p2p_search_delay(wpa_s);
  3433. return wpas_p2p_find(wpa_s, timeout, type, _dev_type != NULL, _dev_type,
  3434. _dev_id, search_delay);
  3435. }
  3436. static int p2p_ctrl_connect(struct wpa_supplicant *wpa_s, char *cmd,
  3437. char *buf, size_t buflen)
  3438. {
  3439. u8 addr[ETH_ALEN];
  3440. char *pos, *pos2;
  3441. char *pin = NULL;
  3442. enum p2p_wps_method wps_method;
  3443. int new_pin;
  3444. int ret;
  3445. int persistent_group, persistent_id = -1;
  3446. int join;
  3447. int auth;
  3448. int automatic;
  3449. int go_intent = -1;
  3450. int freq = 0;
  3451. int pd;
  3452. int ht40, vht;
  3453. /* <addr> <"pbc" | "pin" | PIN> [label|display|keypad]
  3454. * [persistent|persistent=<network id>]
  3455. * [join] [auth] [go_intent=<0..15>] [freq=<in MHz>] [provdisc]
  3456. * [ht40] [vht] */
  3457. if (hwaddr_aton(cmd, addr))
  3458. return -1;
  3459. pos = cmd + 17;
  3460. if (*pos != ' ')
  3461. return -1;
  3462. pos++;
  3463. persistent_group = os_strstr(pos, " persistent") != NULL;
  3464. pos2 = os_strstr(pos, " persistent=");
  3465. if (pos2) {
  3466. struct wpa_ssid *ssid;
  3467. persistent_id = atoi(pos2 + 12);
  3468. ssid = wpa_config_get_network(wpa_s->conf, persistent_id);
  3469. if (ssid == NULL || ssid->disabled != 2 ||
  3470. ssid->mode != WPAS_MODE_P2P_GO) {
  3471. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find "
  3472. "SSID id=%d for persistent P2P group (GO)",
  3473. persistent_id);
  3474. return -1;
  3475. }
  3476. }
  3477. join = os_strstr(pos, " join") != NULL;
  3478. auth = os_strstr(pos, " auth") != NULL;
  3479. automatic = os_strstr(pos, " auto") != NULL;
  3480. pd = os_strstr(pos, " provdisc") != NULL;
  3481. vht = (os_strstr(cmd, " vht") != NULL) || wpa_s->conf->p2p_go_vht;
  3482. ht40 = (os_strstr(cmd, " ht40") != NULL) || wpa_s->conf->p2p_go_ht40 ||
  3483. vht;
  3484. pos2 = os_strstr(pos, " go_intent=");
  3485. if (pos2) {
  3486. pos2 += 11;
  3487. go_intent = atoi(pos2);
  3488. if (go_intent < 0 || go_intent > 15)
  3489. return -1;
  3490. }
  3491. pos2 = os_strstr(pos, " freq=");
  3492. if (pos2) {
  3493. pos2 += 6;
  3494. freq = atoi(pos2);
  3495. if (freq <= 0)
  3496. return -1;
  3497. }
  3498. if (os_strncmp(pos, "pin", 3) == 0) {
  3499. /* Request random PIN (to be displayed) and enable the PIN */
  3500. wps_method = WPS_PIN_DISPLAY;
  3501. } else if (os_strncmp(pos, "pbc", 3) == 0) {
  3502. wps_method = WPS_PBC;
  3503. } else {
  3504. pin = pos;
  3505. pos = os_strchr(pin, ' ');
  3506. wps_method = WPS_PIN_KEYPAD;
  3507. if (pos) {
  3508. *pos++ = '\0';
  3509. if (os_strncmp(pos, "display", 7) == 0)
  3510. wps_method = WPS_PIN_DISPLAY;
  3511. }
  3512. if (!wps_pin_str_valid(pin)) {
  3513. os_memcpy(buf, "FAIL-INVALID-PIN\n", 17);
  3514. return 17;
  3515. }
  3516. }
  3517. new_pin = wpas_p2p_connect(wpa_s, addr, pin, wps_method,
  3518. persistent_group, automatic, join,
  3519. auth, go_intent, freq, persistent_id, pd,
  3520. ht40, vht);
  3521. if (new_pin == -2) {
  3522. os_memcpy(buf, "FAIL-CHANNEL-UNAVAILABLE\n", 25);
  3523. return 25;
  3524. }
  3525. if (new_pin == -3) {
  3526. os_memcpy(buf, "FAIL-CHANNEL-UNSUPPORTED\n", 25);
  3527. return 25;
  3528. }
  3529. if (new_pin < 0)
  3530. return -1;
  3531. if (wps_method == WPS_PIN_DISPLAY && pin == NULL) {
  3532. ret = os_snprintf(buf, buflen, "%08d", new_pin);
  3533. if (ret < 0 || (size_t) ret >= buflen)
  3534. return -1;
  3535. return ret;
  3536. }
  3537. os_memcpy(buf, "OK\n", 3);
  3538. return 3;
  3539. }
  3540. static int p2p_ctrl_listen(struct wpa_supplicant *wpa_s, char *cmd)
  3541. {
  3542. unsigned int timeout = atoi(cmd);
  3543. if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED) {
  3544. wpa_dbg(wpa_s, MSG_INFO,
  3545. "Reject P2P_LISTEN since interface is disabled");
  3546. return -1;
  3547. }
  3548. return wpas_p2p_listen(wpa_s, timeout);
  3549. }
  3550. static int p2p_ctrl_prov_disc(struct wpa_supplicant *wpa_s, char *cmd)
  3551. {
  3552. u8 addr[ETH_ALEN];
  3553. char *pos;
  3554. enum wpas_p2p_prov_disc_use use = WPAS_P2P_PD_FOR_GO_NEG;
  3555. /* <addr> <config method> [join|auto] */
  3556. if (hwaddr_aton(cmd, addr))
  3557. return -1;
  3558. pos = cmd + 17;
  3559. if (*pos != ' ')
  3560. return -1;
  3561. pos++;
  3562. if (os_strstr(pos, " join") != NULL)
  3563. use = WPAS_P2P_PD_FOR_JOIN;
  3564. else if (os_strstr(pos, " auto") != NULL)
  3565. use = WPAS_P2P_PD_AUTO;
  3566. return wpas_p2p_prov_disc(wpa_s, addr, pos, use);
  3567. }
  3568. static int p2p_get_passphrase(struct wpa_supplicant *wpa_s, char *buf,
  3569. size_t buflen)
  3570. {
  3571. struct wpa_ssid *ssid = wpa_s->current_ssid;
  3572. if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GO ||
  3573. ssid->passphrase == NULL)
  3574. return -1;
  3575. os_strlcpy(buf, ssid->passphrase, buflen);
  3576. return os_strlen(buf);
  3577. }
  3578. static int p2p_ctrl_serv_disc_req(struct wpa_supplicant *wpa_s, char *cmd,
  3579. char *buf, size_t buflen)
  3580. {
  3581. u64 ref;
  3582. int res;
  3583. u8 dst_buf[ETH_ALEN], *dst;
  3584. struct wpabuf *tlvs;
  3585. char *pos;
  3586. size_t len;
  3587. if (hwaddr_aton(cmd, dst_buf))
  3588. return -1;
  3589. dst = dst_buf;
  3590. if (dst[0] == 0 && dst[1] == 0 && dst[2] == 0 &&
  3591. dst[3] == 0 && dst[4] == 0 && dst[5] == 0)
  3592. dst = NULL;
  3593. pos = cmd + 17;
  3594. if (*pos != ' ')
  3595. return -1;
  3596. pos++;
  3597. if (os_strncmp(pos, "upnp ", 5) == 0) {
  3598. u8 version;
  3599. pos += 5;
  3600. if (hexstr2bin(pos, &version, 1) < 0)
  3601. return -1;
  3602. pos += 2;
  3603. if (*pos != ' ')
  3604. return -1;
  3605. pos++;
  3606. ref = wpas_p2p_sd_request_upnp(wpa_s, dst, version, pos);
  3607. #ifdef CONFIG_WIFI_DISPLAY
  3608. } else if (os_strncmp(pos, "wifi-display ", 13) == 0) {
  3609. ref = wpas_p2p_sd_request_wifi_display(wpa_s, dst, pos + 13);
  3610. #endif /* CONFIG_WIFI_DISPLAY */
  3611. } else {
  3612. len = os_strlen(pos);
  3613. if (len & 1)
  3614. return -1;
  3615. len /= 2;
  3616. tlvs = wpabuf_alloc(len);
  3617. if (tlvs == NULL)
  3618. return -1;
  3619. if (hexstr2bin(pos, wpabuf_put(tlvs, len), len) < 0) {
  3620. wpabuf_free(tlvs);
  3621. return -1;
  3622. }
  3623. ref = wpas_p2p_sd_request(wpa_s, dst, tlvs);
  3624. wpabuf_free(tlvs);
  3625. }
  3626. if (ref == 0)
  3627. return -1;
  3628. res = os_snprintf(buf, buflen, "%llx", (long long unsigned) ref);
  3629. if (res < 0 || (unsigned) res >= buflen)
  3630. return -1;
  3631. return res;
  3632. }
  3633. static int p2p_ctrl_serv_disc_cancel_req(struct wpa_supplicant *wpa_s,
  3634. char *cmd)
  3635. {
  3636. long long unsigned val;
  3637. u64 req;
  3638. if (sscanf(cmd, "%llx", &val) != 1)
  3639. return -1;
  3640. req = val;
  3641. return wpas_p2p_sd_cancel_request(wpa_s, req);
  3642. }
  3643. static int p2p_ctrl_serv_disc_resp(struct wpa_supplicant *wpa_s, char *cmd)
  3644. {
  3645. int freq;
  3646. u8 dst[ETH_ALEN];
  3647. u8 dialog_token;
  3648. struct wpabuf *resp_tlvs;
  3649. char *pos, *pos2;
  3650. size_t len;
  3651. pos = os_strchr(cmd, ' ');
  3652. if (pos == NULL)
  3653. return -1;
  3654. *pos++ = '\0';
  3655. freq = atoi(cmd);
  3656. if (freq == 0)
  3657. return -1;
  3658. if (hwaddr_aton(pos, dst))
  3659. return -1;
  3660. pos += 17;
  3661. if (*pos != ' ')
  3662. return -1;
  3663. pos++;
  3664. pos2 = os_strchr(pos, ' ');
  3665. if (pos2 == NULL)
  3666. return -1;
  3667. *pos2++ = '\0';
  3668. dialog_token = atoi(pos);
  3669. len = os_strlen(pos2);
  3670. if (len & 1)
  3671. return -1;
  3672. len /= 2;
  3673. resp_tlvs = wpabuf_alloc(len);
  3674. if (resp_tlvs == NULL)
  3675. return -1;
  3676. if (hexstr2bin(pos2, wpabuf_put(resp_tlvs, len), len) < 0) {
  3677. wpabuf_free(resp_tlvs);
  3678. return -1;
  3679. }
  3680. wpas_p2p_sd_response(wpa_s, freq, dst, dialog_token, resp_tlvs);
  3681. wpabuf_free(resp_tlvs);
  3682. return 0;
  3683. }
  3684. static int p2p_ctrl_serv_disc_external(struct wpa_supplicant *wpa_s,
  3685. char *cmd)
  3686. {
  3687. if (os_strcmp(cmd, "0") && os_strcmp(cmd, "1"))
  3688. return -1;
  3689. wpa_s->p2p_sd_over_ctrl_iface = atoi(cmd);
  3690. return 0;
  3691. }
  3692. static int p2p_ctrl_service_add_bonjour(struct wpa_supplicant *wpa_s,
  3693. char *cmd)
  3694. {
  3695. char *pos;
  3696. size_t len;
  3697. struct wpabuf *query, *resp;
  3698. pos = os_strchr(cmd, ' ');
  3699. if (pos == NULL)
  3700. return -1;
  3701. *pos++ = '\0';
  3702. len = os_strlen(cmd);
  3703. if (len & 1)
  3704. return -1;
  3705. len /= 2;
  3706. query = wpabuf_alloc(len);
  3707. if (query == NULL)
  3708. return -1;
  3709. if (hexstr2bin(cmd, wpabuf_put(query, len), len) < 0) {
  3710. wpabuf_free(query);
  3711. return -1;
  3712. }
  3713. len = os_strlen(pos);
  3714. if (len & 1) {
  3715. wpabuf_free(query);
  3716. return -1;
  3717. }
  3718. len /= 2;
  3719. resp = wpabuf_alloc(len);
  3720. if (resp == NULL) {
  3721. wpabuf_free(query);
  3722. return -1;
  3723. }
  3724. if (hexstr2bin(pos, wpabuf_put(resp, len), len) < 0) {
  3725. wpabuf_free(query);
  3726. wpabuf_free(resp);
  3727. return -1;
  3728. }
  3729. if (wpas_p2p_service_add_bonjour(wpa_s, query, resp) < 0) {
  3730. wpabuf_free(query);
  3731. wpabuf_free(resp);
  3732. return -1;
  3733. }
  3734. return 0;
  3735. }
  3736. static int p2p_ctrl_service_add_upnp(struct wpa_supplicant *wpa_s, char *cmd)
  3737. {
  3738. char *pos;
  3739. u8 version;
  3740. pos = os_strchr(cmd, ' ');
  3741. if (pos == NULL)
  3742. return -1;
  3743. *pos++ = '\0';
  3744. if (hexstr2bin(cmd, &version, 1) < 0)
  3745. return -1;
  3746. return wpas_p2p_service_add_upnp(wpa_s, version, pos);
  3747. }
  3748. static int p2p_ctrl_service_add(struct wpa_supplicant *wpa_s, char *cmd)
  3749. {
  3750. char *pos;
  3751. pos = os_strchr(cmd, ' ');
  3752. if (pos == NULL)
  3753. return -1;
  3754. *pos++ = '\0';
  3755. if (os_strcmp(cmd, "bonjour") == 0)
  3756. return p2p_ctrl_service_add_bonjour(wpa_s, pos);
  3757. if (os_strcmp(cmd, "upnp") == 0)
  3758. return p2p_ctrl_service_add_upnp(wpa_s, pos);
  3759. wpa_printf(MSG_DEBUG, "Unknown service '%s'", cmd);
  3760. return -1;
  3761. }
  3762. static int p2p_ctrl_service_del_bonjour(struct wpa_supplicant *wpa_s,
  3763. char *cmd)
  3764. {
  3765. size_t len;
  3766. struct wpabuf *query;
  3767. int ret;
  3768. len = os_strlen(cmd);
  3769. if (len & 1)
  3770. return -1;
  3771. len /= 2;
  3772. query = wpabuf_alloc(len);
  3773. if (query == NULL)
  3774. return -1;
  3775. if (hexstr2bin(cmd, wpabuf_put(query, len), len) < 0) {
  3776. wpabuf_free(query);
  3777. return -1;
  3778. }
  3779. ret = wpas_p2p_service_del_bonjour(wpa_s, query);
  3780. wpabuf_free(query);
  3781. return ret;
  3782. }
  3783. static int p2p_ctrl_service_del_upnp(struct wpa_supplicant *wpa_s, char *cmd)
  3784. {
  3785. char *pos;
  3786. u8 version;
  3787. pos = os_strchr(cmd, ' ');
  3788. if (pos == NULL)
  3789. return -1;
  3790. *pos++ = '\0';
  3791. if (hexstr2bin(cmd, &version, 1) < 0)
  3792. return -1;
  3793. return wpas_p2p_service_del_upnp(wpa_s, version, pos);
  3794. }
  3795. static int p2p_ctrl_service_del(struct wpa_supplicant *wpa_s, char *cmd)
  3796. {
  3797. char *pos;
  3798. pos = os_strchr(cmd, ' ');
  3799. if (pos == NULL)
  3800. return -1;
  3801. *pos++ = '\0';
  3802. if (os_strcmp(cmd, "bonjour") == 0)
  3803. return p2p_ctrl_service_del_bonjour(wpa_s, pos);
  3804. if (os_strcmp(cmd, "upnp") == 0)
  3805. return p2p_ctrl_service_del_upnp(wpa_s, pos);
  3806. wpa_printf(MSG_DEBUG, "Unknown service '%s'", cmd);
  3807. return -1;
  3808. }
  3809. static int p2p_ctrl_reject(struct wpa_supplicant *wpa_s, char *cmd)
  3810. {
  3811. u8 addr[ETH_ALEN];
  3812. /* <addr> */
  3813. if (hwaddr_aton(cmd, addr))
  3814. return -1;
  3815. return wpas_p2p_reject(wpa_s, addr);
  3816. }
  3817. static int p2p_ctrl_invite_persistent(struct wpa_supplicant *wpa_s, char *cmd)
  3818. {
  3819. char *pos;
  3820. int id;
  3821. struct wpa_ssid *ssid;
  3822. u8 *_peer = NULL, peer[ETH_ALEN];
  3823. int freq = 0, pref_freq = 0;
  3824. int ht40, vht;
  3825. id = atoi(cmd);
  3826. pos = os_strstr(cmd, " peer=");
  3827. if (pos) {
  3828. pos += 6;
  3829. if (hwaddr_aton(pos, peer))
  3830. return -1;
  3831. _peer = peer;
  3832. }
  3833. ssid = wpa_config_get_network(wpa_s->conf, id);
  3834. if (ssid == NULL || ssid->disabled != 2) {
  3835. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find SSID id=%d "
  3836. "for persistent P2P group",
  3837. id);
  3838. return -1;
  3839. }
  3840. pos = os_strstr(cmd, " freq=");
  3841. if (pos) {
  3842. pos += 6;
  3843. freq = atoi(pos);
  3844. if (freq <= 0)
  3845. return -1;
  3846. }
  3847. pos = os_strstr(cmd, " pref=");
  3848. if (pos) {
  3849. pos += 6;
  3850. pref_freq = atoi(pos);
  3851. if (pref_freq <= 0)
  3852. return -1;
  3853. }
  3854. vht = (os_strstr(cmd, " vht") != NULL) || wpa_s->conf->p2p_go_vht;
  3855. ht40 = (os_strstr(cmd, " ht40") != NULL) || wpa_s->conf->p2p_go_ht40 ||
  3856. vht;
  3857. return wpas_p2p_invite(wpa_s, _peer, ssid, NULL, freq, ht40, vht,
  3858. pref_freq);
  3859. }
  3860. static int p2p_ctrl_invite_group(struct wpa_supplicant *wpa_s, char *cmd)
  3861. {
  3862. char *pos;
  3863. u8 peer[ETH_ALEN], go_dev_addr[ETH_ALEN], *go_dev = NULL;
  3864. pos = os_strstr(cmd, " peer=");
  3865. if (!pos)
  3866. return -1;
  3867. *pos = '\0';
  3868. pos += 6;
  3869. if (hwaddr_aton(pos, peer)) {
  3870. wpa_printf(MSG_DEBUG, "P2P: Invalid MAC address '%s'", pos);
  3871. return -1;
  3872. }
  3873. pos = os_strstr(pos, " go_dev_addr=");
  3874. if (pos) {
  3875. pos += 13;
  3876. if (hwaddr_aton(pos, go_dev_addr)) {
  3877. wpa_printf(MSG_DEBUG, "P2P: Invalid MAC address '%s'",
  3878. pos);
  3879. return -1;
  3880. }
  3881. go_dev = go_dev_addr;
  3882. }
  3883. return wpas_p2p_invite_group(wpa_s, cmd, peer, go_dev);
  3884. }
  3885. static int p2p_ctrl_invite(struct wpa_supplicant *wpa_s, char *cmd)
  3886. {
  3887. if (os_strncmp(cmd, "persistent=", 11) == 0)
  3888. return p2p_ctrl_invite_persistent(wpa_s, cmd + 11);
  3889. if (os_strncmp(cmd, "group=", 6) == 0)
  3890. return p2p_ctrl_invite_group(wpa_s, cmd + 6);
  3891. return -1;
  3892. }
  3893. static int p2p_ctrl_group_add_persistent(struct wpa_supplicant *wpa_s,
  3894. char *cmd, int freq, int ht40,
  3895. int vht)
  3896. {
  3897. int id;
  3898. struct wpa_ssid *ssid;
  3899. id = atoi(cmd);
  3900. ssid = wpa_config_get_network(wpa_s->conf, id);
  3901. if (ssid == NULL || ssid->disabled != 2) {
  3902. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find SSID id=%d "
  3903. "for persistent P2P group",
  3904. id);
  3905. return -1;
  3906. }
  3907. return wpas_p2p_group_add_persistent(wpa_s, ssid, 0, freq, 0, ht40, vht,
  3908. NULL, 0);
  3909. }
  3910. static int p2p_ctrl_group_add(struct wpa_supplicant *wpa_s, char *cmd)
  3911. {
  3912. int freq = 0, ht40, vht;
  3913. char *pos;
  3914. pos = os_strstr(cmd, "freq=");
  3915. if (pos)
  3916. freq = atoi(pos + 5);
  3917. vht = (os_strstr(cmd, "vht") != NULL) || wpa_s->conf->p2p_go_vht;
  3918. ht40 = (os_strstr(cmd, "ht40") != NULL) || wpa_s->conf->p2p_go_ht40 ||
  3919. vht;
  3920. if (os_strncmp(cmd, "persistent=", 11) == 0)
  3921. return p2p_ctrl_group_add_persistent(wpa_s, cmd + 11, freq,
  3922. ht40, vht);
  3923. if (os_strcmp(cmd, "persistent") == 0 ||
  3924. os_strncmp(cmd, "persistent ", 11) == 0)
  3925. return wpas_p2p_group_add(wpa_s, 1, freq, ht40, vht);
  3926. if (os_strncmp(cmd, "freq=", 5) == 0)
  3927. return wpas_p2p_group_add(wpa_s, 0, freq, ht40, vht);
  3928. if (ht40)
  3929. return wpas_p2p_group_add(wpa_s, 0, freq, ht40, vht);
  3930. wpa_printf(MSG_DEBUG, "CTRL: Invalid P2P_GROUP_ADD parameters '%s'",
  3931. cmd);
  3932. return -1;
  3933. }
  3934. static int p2p_ctrl_peer(struct wpa_supplicant *wpa_s, char *cmd,
  3935. char *buf, size_t buflen)
  3936. {
  3937. u8 addr[ETH_ALEN], *addr_ptr;
  3938. int next, res;
  3939. const struct p2p_peer_info *info;
  3940. char *pos, *end;
  3941. char devtype[WPS_DEV_TYPE_BUFSIZE];
  3942. struct wpa_ssid *ssid;
  3943. size_t i;
  3944. if (!wpa_s->global->p2p)
  3945. return -1;
  3946. if (os_strcmp(cmd, "FIRST") == 0) {
  3947. addr_ptr = NULL;
  3948. next = 0;
  3949. } else if (os_strncmp(cmd, "NEXT-", 5) == 0) {
  3950. if (hwaddr_aton(cmd + 5, addr) < 0)
  3951. return -1;
  3952. addr_ptr = addr;
  3953. next = 1;
  3954. } else {
  3955. if (hwaddr_aton(cmd, addr) < 0)
  3956. return -1;
  3957. addr_ptr = addr;
  3958. next = 0;
  3959. }
  3960. info = p2p_get_peer_info(wpa_s->global->p2p, addr_ptr, next);
  3961. if (info == NULL)
  3962. return -1;
  3963. pos = buf;
  3964. end = buf + buflen;
  3965. res = os_snprintf(pos, end - pos, MACSTR "\n"
  3966. "pri_dev_type=%s\n"
  3967. "device_name=%s\n"
  3968. "manufacturer=%s\n"
  3969. "model_name=%s\n"
  3970. "model_number=%s\n"
  3971. "serial_number=%s\n"
  3972. "config_methods=0x%x\n"
  3973. "dev_capab=0x%x\n"
  3974. "group_capab=0x%x\n"
  3975. "level=%d\n",
  3976. MAC2STR(info->p2p_device_addr),
  3977. wps_dev_type_bin2str(info->pri_dev_type,
  3978. devtype, sizeof(devtype)),
  3979. info->device_name,
  3980. info->manufacturer,
  3981. info->model_name,
  3982. info->model_number,
  3983. info->serial_number,
  3984. info->config_methods,
  3985. info->dev_capab,
  3986. info->group_capab,
  3987. info->level);
  3988. if (res < 0 || res >= end - pos)
  3989. return pos - buf;
  3990. pos += res;
  3991. for (i = 0; i < info->wps_sec_dev_type_list_len / WPS_DEV_TYPE_LEN; i++)
  3992. {
  3993. const u8 *t;
  3994. t = &info->wps_sec_dev_type_list[i * WPS_DEV_TYPE_LEN];
  3995. res = os_snprintf(pos, end - pos, "sec_dev_type=%s\n",
  3996. wps_dev_type_bin2str(t, devtype,
  3997. sizeof(devtype)));
  3998. if (res < 0 || res >= end - pos)
  3999. return pos - buf;
  4000. pos += res;
  4001. }
  4002. ssid = wpas_p2p_get_persistent(wpa_s, info->p2p_device_addr, NULL, 0);
  4003. if (ssid) {
  4004. res = os_snprintf(pos, end - pos, "persistent=%d\n", ssid->id);
  4005. if (res < 0 || res >= end - pos)
  4006. return pos - buf;
  4007. pos += res;
  4008. }
  4009. res = p2p_get_peer_info_txt(info, pos, end - pos);
  4010. if (res < 0)
  4011. return pos - buf;
  4012. pos += res;
  4013. if (info->vendor_elems) {
  4014. res = os_snprintf(pos, end - pos, "vendor_elems=");
  4015. if (res < 0 || res >= end - pos)
  4016. return pos - buf;
  4017. pos += res;
  4018. pos += wpa_snprintf_hex(pos, end - pos,
  4019. wpabuf_head(info->vendor_elems),
  4020. wpabuf_len(info->vendor_elems));
  4021. res = os_snprintf(pos, end - pos, "\n");
  4022. if (res < 0 || res >= end - pos)
  4023. return pos - buf;
  4024. pos += res;
  4025. }
  4026. return pos - buf;
  4027. }
  4028. static int p2p_ctrl_disallow_freq(struct wpa_supplicant *wpa_s,
  4029. const char *param)
  4030. {
  4031. unsigned int i;
  4032. if (wpa_s->global->p2p == NULL)
  4033. return -1;
  4034. if (freq_range_list_parse(&wpa_s->global->p2p_disallow_freq, param) < 0)
  4035. return -1;
  4036. for (i = 0; i < wpa_s->global->p2p_disallow_freq.num; i++) {
  4037. struct wpa_freq_range *freq;
  4038. freq = &wpa_s->global->p2p_disallow_freq.range[i];
  4039. wpa_printf(MSG_DEBUG, "P2P: Disallowed frequency range %u-%u",
  4040. freq->min, freq->max);
  4041. }
  4042. wpas_p2p_update_channel_list(wpa_s);
  4043. return 0;
  4044. }
  4045. static int p2p_ctrl_set(struct wpa_supplicant *wpa_s, char *cmd)
  4046. {
  4047. char *param;
  4048. if (wpa_s->global->p2p == NULL)
  4049. return -1;
  4050. param = os_strchr(cmd, ' ');
  4051. if (param == NULL)
  4052. return -1;
  4053. *param++ = '\0';
  4054. if (os_strcmp(cmd, "discoverability") == 0) {
  4055. p2p_set_client_discoverability(wpa_s->global->p2p,
  4056. atoi(param));
  4057. return 0;
  4058. }
  4059. if (os_strcmp(cmd, "managed") == 0) {
  4060. p2p_set_managed_oper(wpa_s->global->p2p, atoi(param));
  4061. return 0;
  4062. }
  4063. if (os_strcmp(cmd, "listen_channel") == 0) {
  4064. return p2p_set_listen_channel(wpa_s->global->p2p, 81,
  4065. atoi(param), 1);
  4066. }
  4067. if (os_strcmp(cmd, "ssid_postfix") == 0) {
  4068. return p2p_set_ssid_postfix(wpa_s->global->p2p, (u8 *) param,
  4069. os_strlen(param));
  4070. }
  4071. if (os_strcmp(cmd, "noa") == 0) {
  4072. char *pos;
  4073. int count, start, duration;
  4074. /* GO NoA parameters: count,start_offset(ms),duration(ms) */
  4075. count = atoi(param);
  4076. pos = os_strchr(param, ',');
  4077. if (pos == NULL)
  4078. return -1;
  4079. pos++;
  4080. start = atoi(pos);
  4081. pos = os_strchr(pos, ',');
  4082. if (pos == NULL)
  4083. return -1;
  4084. pos++;
  4085. duration = atoi(pos);
  4086. if (count < 0 || count > 255 || start < 0 || duration < 0)
  4087. return -1;
  4088. if (count == 0 && duration > 0)
  4089. return -1;
  4090. wpa_printf(MSG_DEBUG, "CTRL_IFACE: P2P_SET GO NoA: count=%d "
  4091. "start=%d duration=%d", count, start, duration);
  4092. return wpas_p2p_set_noa(wpa_s, count, start, duration);
  4093. }
  4094. if (os_strcmp(cmd, "ps") == 0)
  4095. return wpa_drv_set_p2p_powersave(wpa_s, atoi(param), -1, -1);
  4096. if (os_strcmp(cmd, "oppps") == 0)
  4097. return wpa_drv_set_p2p_powersave(wpa_s, -1, atoi(param), -1);
  4098. if (os_strcmp(cmd, "ctwindow") == 0)
  4099. return wpa_drv_set_p2p_powersave(wpa_s, -1, -1, atoi(param));
  4100. if (os_strcmp(cmd, "disabled") == 0) {
  4101. wpa_s->global->p2p_disabled = atoi(param);
  4102. wpa_printf(MSG_DEBUG, "P2P functionality %s",
  4103. wpa_s->global->p2p_disabled ?
  4104. "disabled" : "enabled");
  4105. if (wpa_s->global->p2p_disabled) {
  4106. wpas_p2p_stop_find(wpa_s);
  4107. os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN);
  4108. p2p_flush(wpa_s->global->p2p);
  4109. }
  4110. return 0;
  4111. }
  4112. if (os_strcmp(cmd, "conc_pref") == 0) {
  4113. if (os_strcmp(param, "sta") == 0)
  4114. wpa_s->global->conc_pref = WPA_CONC_PREF_STA;
  4115. else if (os_strcmp(param, "p2p") == 0)
  4116. wpa_s->global->conc_pref = WPA_CONC_PREF_P2P;
  4117. else {
  4118. wpa_printf(MSG_INFO, "Invalid conc_pref value");
  4119. return -1;
  4120. }
  4121. wpa_printf(MSG_DEBUG, "Single channel concurrency preference: "
  4122. "%s", param);
  4123. return 0;
  4124. }
  4125. if (os_strcmp(cmd, "force_long_sd") == 0) {
  4126. wpa_s->force_long_sd = atoi(param);
  4127. return 0;
  4128. }
  4129. if (os_strcmp(cmd, "peer_filter") == 0) {
  4130. u8 addr[ETH_ALEN];
  4131. if (hwaddr_aton(param, addr))
  4132. return -1;
  4133. p2p_set_peer_filter(wpa_s->global->p2p, addr);
  4134. return 0;
  4135. }
  4136. if (os_strcmp(cmd, "cross_connect") == 0)
  4137. return wpas_p2p_set_cross_connect(wpa_s, atoi(param));
  4138. if (os_strcmp(cmd, "go_apsd") == 0) {
  4139. if (os_strcmp(param, "disable") == 0)
  4140. wpa_s->set_ap_uapsd = 0;
  4141. else {
  4142. wpa_s->set_ap_uapsd = 1;
  4143. wpa_s->ap_uapsd = atoi(param);
  4144. }
  4145. return 0;
  4146. }
  4147. if (os_strcmp(cmd, "client_apsd") == 0) {
  4148. if (os_strcmp(param, "disable") == 0)
  4149. wpa_s->set_sta_uapsd = 0;
  4150. else {
  4151. int be, bk, vi, vo;
  4152. char *pos;
  4153. /* format: BE,BK,VI,VO;max SP Length */
  4154. be = atoi(param);
  4155. pos = os_strchr(param, ',');
  4156. if (pos == NULL)
  4157. return -1;
  4158. pos++;
  4159. bk = atoi(pos);
  4160. pos = os_strchr(pos, ',');
  4161. if (pos == NULL)
  4162. return -1;
  4163. pos++;
  4164. vi = atoi(pos);
  4165. pos = os_strchr(pos, ',');
  4166. if (pos == NULL)
  4167. return -1;
  4168. pos++;
  4169. vo = atoi(pos);
  4170. /* ignore max SP Length for now */
  4171. wpa_s->set_sta_uapsd = 1;
  4172. wpa_s->sta_uapsd = 0;
  4173. if (be)
  4174. wpa_s->sta_uapsd |= BIT(0);
  4175. if (bk)
  4176. wpa_s->sta_uapsd |= BIT(1);
  4177. if (vi)
  4178. wpa_s->sta_uapsd |= BIT(2);
  4179. if (vo)
  4180. wpa_s->sta_uapsd |= BIT(3);
  4181. }
  4182. return 0;
  4183. }
  4184. if (os_strcmp(cmd, "disallow_freq") == 0)
  4185. return p2p_ctrl_disallow_freq(wpa_s, param);
  4186. if (os_strcmp(cmd, "disc_int") == 0) {
  4187. int min_disc_int, max_disc_int, max_disc_tu;
  4188. char *pos;
  4189. pos = param;
  4190. min_disc_int = atoi(pos);
  4191. pos = os_strchr(pos, ' ');
  4192. if (pos == NULL)
  4193. return -1;
  4194. *pos++ = '\0';
  4195. max_disc_int = atoi(pos);
  4196. pos = os_strchr(pos, ' ');
  4197. if (pos == NULL)
  4198. return -1;
  4199. *pos++ = '\0';
  4200. max_disc_tu = atoi(pos);
  4201. return p2p_set_disc_int(wpa_s->global->p2p, min_disc_int,
  4202. max_disc_int, max_disc_tu);
  4203. }
  4204. if (os_strcmp(cmd, "per_sta_psk") == 0) {
  4205. wpa_s->global->p2p_per_sta_psk = !!atoi(param);
  4206. return 0;
  4207. }
  4208. #ifdef CONFIG_WPS_NFC
  4209. if (os_strcmp(cmd, "nfc_tag") == 0)
  4210. return wpas_p2p_nfc_tag_enabled(wpa_s, !!atoi(param));
  4211. #endif /* CONFIG_WPS_NFC */
  4212. if (os_strcmp(cmd, "disable_ip_addr_req") == 0) {
  4213. wpa_s->p2p_disable_ip_addr_req = !!atoi(param);
  4214. return 0;
  4215. }
  4216. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Unknown P2P_SET field value '%s'",
  4217. cmd);
  4218. return -1;
  4219. }
  4220. static void p2p_ctrl_flush(struct wpa_supplicant *wpa_s)
  4221. {
  4222. os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN);
  4223. wpa_s->force_long_sd = 0;
  4224. if (wpa_s->global->p2p)
  4225. p2p_flush(wpa_s->global->p2p);
  4226. }
  4227. static int p2p_ctrl_presence_req(struct wpa_supplicant *wpa_s, char *cmd)
  4228. {
  4229. char *pos, *pos2;
  4230. unsigned int dur1 = 0, int1 = 0, dur2 = 0, int2 = 0;
  4231. if (cmd[0]) {
  4232. pos = os_strchr(cmd, ' ');
  4233. if (pos == NULL)
  4234. return -1;
  4235. *pos++ = '\0';
  4236. dur1 = atoi(cmd);
  4237. pos2 = os_strchr(pos, ' ');
  4238. if (pos2)
  4239. *pos2++ = '\0';
  4240. int1 = atoi(pos);
  4241. } else
  4242. pos2 = NULL;
  4243. if (pos2) {
  4244. pos = os_strchr(pos2, ' ');
  4245. if (pos == NULL)
  4246. return -1;
  4247. *pos++ = '\0';
  4248. dur2 = atoi(pos2);
  4249. int2 = atoi(pos);
  4250. }
  4251. return wpas_p2p_presence_req(wpa_s, dur1, int1, dur2, int2);
  4252. }
  4253. static int p2p_ctrl_ext_listen(struct wpa_supplicant *wpa_s, char *cmd)
  4254. {
  4255. char *pos;
  4256. unsigned int period = 0, interval = 0;
  4257. if (cmd[0]) {
  4258. pos = os_strchr(cmd, ' ');
  4259. if (pos == NULL)
  4260. return -1;
  4261. *pos++ = '\0';
  4262. period = atoi(cmd);
  4263. interval = atoi(pos);
  4264. }
  4265. return wpas_p2p_ext_listen(wpa_s, period, interval);
  4266. }
  4267. static int p2p_ctrl_remove_client(struct wpa_supplicant *wpa_s, const char *cmd)
  4268. {
  4269. const char *pos;
  4270. u8 peer[ETH_ALEN];
  4271. int iface_addr = 0;
  4272. pos = cmd;
  4273. if (os_strncmp(pos, "iface=", 6) == 0) {
  4274. iface_addr = 1;
  4275. pos += 6;
  4276. }
  4277. if (hwaddr_aton(pos, peer))
  4278. return -1;
  4279. wpas_p2p_remove_client(wpa_s, peer, iface_addr);
  4280. return 0;
  4281. }
  4282. #endif /* CONFIG_P2P */
  4283. static int * freq_range_to_channel_list(struct wpa_supplicant *wpa_s, char *val)
  4284. {
  4285. struct wpa_freq_range_list ranges;
  4286. int *freqs = NULL;
  4287. struct hostapd_hw_modes *mode;
  4288. u16 i;
  4289. if (wpa_s->hw.modes == NULL)
  4290. return NULL;
  4291. os_memset(&ranges, 0, sizeof(ranges));
  4292. if (freq_range_list_parse(&ranges, val) < 0)
  4293. return NULL;
  4294. for (i = 0; i < wpa_s->hw.num_modes; i++) {
  4295. int j;
  4296. mode = &wpa_s->hw.modes[i];
  4297. for (j = 0; j < mode->num_channels; j++) {
  4298. unsigned int freq;
  4299. if (mode->channels[j].flag & HOSTAPD_CHAN_DISABLED)
  4300. continue;
  4301. freq = mode->channels[j].freq;
  4302. if (!freq_range_list_includes(&ranges, freq))
  4303. continue;
  4304. int_array_add_unique(&freqs, freq);
  4305. }
  4306. }
  4307. os_free(ranges.range);
  4308. return freqs;
  4309. }
  4310. #ifdef CONFIG_INTERWORKING
  4311. static int ctrl_interworking_select(struct wpa_supplicant *wpa_s, char *param)
  4312. {
  4313. int auto_sel = 0;
  4314. int *freqs = NULL;
  4315. if (param) {
  4316. char *pos;
  4317. auto_sel = os_strstr(param, "auto") != NULL;
  4318. pos = os_strstr(param, "freq=");
  4319. if (pos) {
  4320. freqs = freq_range_to_channel_list(wpa_s, pos + 5);
  4321. if (freqs == NULL)
  4322. return -1;
  4323. }
  4324. }
  4325. return interworking_select(wpa_s, auto_sel, freqs);
  4326. }
  4327. static int ctrl_interworking_connect(struct wpa_supplicant *wpa_s, char *dst)
  4328. {
  4329. u8 bssid[ETH_ALEN];
  4330. struct wpa_bss *bss;
  4331. if (hwaddr_aton(dst, bssid)) {
  4332. wpa_printf(MSG_DEBUG, "Invalid BSSID '%s'", dst);
  4333. return -1;
  4334. }
  4335. bss = wpa_bss_get_bssid(wpa_s, bssid);
  4336. if (bss == NULL) {
  4337. wpa_printf(MSG_DEBUG, "Could not find BSS " MACSTR,
  4338. MAC2STR(bssid));
  4339. return -1;
  4340. }
  4341. return interworking_connect(wpa_s, bss);
  4342. }
  4343. static int get_anqp(struct wpa_supplicant *wpa_s, char *dst)
  4344. {
  4345. u8 dst_addr[ETH_ALEN];
  4346. int used;
  4347. char *pos;
  4348. #define MAX_ANQP_INFO_ID 100
  4349. u16 id[MAX_ANQP_INFO_ID];
  4350. size_t num_id = 0;
  4351. u32 subtypes = 0;
  4352. used = hwaddr_aton2(dst, dst_addr);
  4353. if (used < 0)
  4354. return -1;
  4355. pos = dst + used;
  4356. while (num_id < MAX_ANQP_INFO_ID) {
  4357. if (os_strncmp(pos, "hs20:", 5) == 0) {
  4358. #ifdef CONFIG_HS20
  4359. int num = atoi(pos + 5);
  4360. if (num <= 0 || num > 31)
  4361. return -1;
  4362. subtypes |= BIT(num);
  4363. #else /* CONFIG_HS20 */
  4364. return -1;
  4365. #endif /* CONFIG_HS20 */
  4366. } else {
  4367. id[num_id] = atoi(pos);
  4368. if (id[num_id])
  4369. num_id++;
  4370. }
  4371. pos = os_strchr(pos + 1, ',');
  4372. if (pos == NULL)
  4373. break;
  4374. pos++;
  4375. }
  4376. if (num_id == 0)
  4377. return -1;
  4378. return anqp_send_req(wpa_s, dst_addr, id, num_id, subtypes);
  4379. }
  4380. static int gas_request(struct wpa_supplicant *wpa_s, char *cmd)
  4381. {
  4382. u8 dst_addr[ETH_ALEN];
  4383. struct wpabuf *advproto, *query = NULL;
  4384. int used, ret = -1;
  4385. char *pos, *end;
  4386. size_t len;
  4387. used = hwaddr_aton2(cmd, dst_addr);
  4388. if (used < 0)
  4389. return -1;
  4390. pos = cmd + used;
  4391. while (*pos == ' ')
  4392. pos++;
  4393. /* Advertisement Protocol ID */
  4394. end = os_strchr(pos, ' ');
  4395. if (end)
  4396. len = end - pos;
  4397. else
  4398. len = os_strlen(pos);
  4399. if (len & 0x01)
  4400. return -1;
  4401. len /= 2;
  4402. if (len == 0)
  4403. return -1;
  4404. advproto = wpabuf_alloc(len);
  4405. if (advproto == NULL)
  4406. return -1;
  4407. if (hexstr2bin(pos, wpabuf_put(advproto, len), len) < 0)
  4408. goto fail;
  4409. if (end) {
  4410. /* Optional Query Request */
  4411. pos = end + 1;
  4412. while (*pos == ' ')
  4413. pos++;
  4414. len = os_strlen(pos);
  4415. if (len) {
  4416. if (len & 0x01)
  4417. goto fail;
  4418. len /= 2;
  4419. if (len == 0)
  4420. goto fail;
  4421. query = wpabuf_alloc(len);
  4422. if (query == NULL)
  4423. goto fail;
  4424. if (hexstr2bin(pos, wpabuf_put(query, len), len) < 0)
  4425. goto fail;
  4426. }
  4427. }
  4428. ret = gas_send_request(wpa_s, dst_addr, advproto, query);
  4429. fail:
  4430. wpabuf_free(advproto);
  4431. wpabuf_free(query);
  4432. return ret;
  4433. }
  4434. static int gas_response_get(struct wpa_supplicant *wpa_s, char *cmd, char *buf,
  4435. size_t buflen)
  4436. {
  4437. u8 addr[ETH_ALEN];
  4438. int dialog_token;
  4439. int used;
  4440. char *pos;
  4441. size_t resp_len, start, requested_len;
  4442. struct wpabuf *resp;
  4443. int ret;
  4444. used = hwaddr_aton2(cmd, addr);
  4445. if (used < 0)
  4446. return -1;
  4447. pos = cmd + used;
  4448. while (*pos == ' ')
  4449. pos++;
  4450. dialog_token = atoi(pos);
  4451. if (wpa_s->last_gas_resp &&
  4452. os_memcmp(addr, wpa_s->last_gas_addr, ETH_ALEN) == 0 &&
  4453. dialog_token == wpa_s->last_gas_dialog_token)
  4454. resp = wpa_s->last_gas_resp;
  4455. else if (wpa_s->prev_gas_resp &&
  4456. os_memcmp(addr, wpa_s->prev_gas_addr, ETH_ALEN) == 0 &&
  4457. dialog_token == wpa_s->prev_gas_dialog_token)
  4458. resp = wpa_s->prev_gas_resp;
  4459. else
  4460. return -1;
  4461. resp_len = wpabuf_len(resp);
  4462. start = 0;
  4463. requested_len = resp_len;
  4464. pos = os_strchr(pos, ' ');
  4465. if (pos) {
  4466. start = atoi(pos);
  4467. if (start > resp_len)
  4468. return os_snprintf(buf, buflen, "FAIL-Invalid range");
  4469. pos = os_strchr(pos, ',');
  4470. if (pos == NULL)
  4471. return -1;
  4472. pos++;
  4473. requested_len = atoi(pos);
  4474. if (start + requested_len > resp_len)
  4475. return os_snprintf(buf, buflen, "FAIL-Invalid range");
  4476. }
  4477. if (requested_len * 2 + 1 > buflen)
  4478. return os_snprintf(buf, buflen, "FAIL-Too long response");
  4479. ret = wpa_snprintf_hex(buf, buflen, wpabuf_head_u8(resp) + start,
  4480. requested_len);
  4481. if (start + requested_len == resp_len) {
  4482. /*
  4483. * Free memory by dropping the response after it has been
  4484. * fetched.
  4485. */
  4486. if (resp == wpa_s->prev_gas_resp) {
  4487. wpabuf_free(wpa_s->prev_gas_resp);
  4488. wpa_s->prev_gas_resp = NULL;
  4489. } else {
  4490. wpabuf_free(wpa_s->last_gas_resp);
  4491. wpa_s->last_gas_resp = NULL;
  4492. }
  4493. }
  4494. return ret;
  4495. }
  4496. #endif /* CONFIG_INTERWORKING */
  4497. #ifdef CONFIG_HS20
  4498. static int get_hs20_anqp(struct wpa_supplicant *wpa_s, char *dst)
  4499. {
  4500. u8 dst_addr[ETH_ALEN];
  4501. int used;
  4502. char *pos;
  4503. u32 subtypes = 0;
  4504. used = hwaddr_aton2(dst, dst_addr);
  4505. if (used < 0)
  4506. return -1;
  4507. pos = dst + used;
  4508. for (;;) {
  4509. int num = atoi(pos);
  4510. if (num <= 0 || num > 31)
  4511. return -1;
  4512. subtypes |= BIT(num);
  4513. pos = os_strchr(pos + 1, ',');
  4514. if (pos == NULL)
  4515. break;
  4516. pos++;
  4517. }
  4518. if (subtypes == 0)
  4519. return -1;
  4520. return hs20_anqp_send_req(wpa_s, dst_addr, subtypes, NULL, 0);
  4521. }
  4522. static int hs20_nai_home_realm_list(struct wpa_supplicant *wpa_s,
  4523. const u8 *addr, const char *realm)
  4524. {
  4525. u8 *buf;
  4526. size_t rlen, len;
  4527. int ret;
  4528. rlen = os_strlen(realm);
  4529. len = 3 + rlen;
  4530. buf = os_malloc(len);
  4531. if (buf == NULL)
  4532. return -1;
  4533. buf[0] = 1; /* NAI Home Realm Count */
  4534. buf[1] = 0; /* Formatted in accordance with RFC 4282 */
  4535. buf[2] = rlen;
  4536. os_memcpy(buf + 3, realm, rlen);
  4537. ret = hs20_anqp_send_req(wpa_s, addr,
  4538. BIT(HS20_STYPE_NAI_HOME_REALM_QUERY),
  4539. buf, len);
  4540. os_free(buf);
  4541. return ret;
  4542. }
  4543. static int hs20_get_nai_home_realm_list(struct wpa_supplicant *wpa_s,
  4544. char *dst)
  4545. {
  4546. struct wpa_cred *cred = wpa_s->conf->cred;
  4547. u8 dst_addr[ETH_ALEN];
  4548. int used;
  4549. u8 *buf;
  4550. size_t len;
  4551. int ret;
  4552. used = hwaddr_aton2(dst, dst_addr);
  4553. if (used < 0)
  4554. return -1;
  4555. while (dst[used] == ' ')
  4556. used++;
  4557. if (os_strncmp(dst + used, "realm=", 6) == 0)
  4558. return hs20_nai_home_realm_list(wpa_s, dst_addr,
  4559. dst + used + 6);
  4560. len = os_strlen(dst + used);
  4561. if (len == 0 && cred && cred->realm)
  4562. return hs20_nai_home_realm_list(wpa_s, dst_addr, cred->realm);
  4563. if (len & 1)
  4564. return -1;
  4565. len /= 2;
  4566. buf = os_malloc(len);
  4567. if (buf == NULL)
  4568. return -1;
  4569. if (hexstr2bin(dst + used, buf, len) < 0) {
  4570. os_free(buf);
  4571. return -1;
  4572. }
  4573. ret = hs20_anqp_send_req(wpa_s, dst_addr,
  4574. BIT(HS20_STYPE_NAI_HOME_REALM_QUERY),
  4575. buf, len);
  4576. os_free(buf);
  4577. return ret;
  4578. }
  4579. static int hs20_icon_request(struct wpa_supplicant *wpa_s, char *cmd)
  4580. {
  4581. u8 dst_addr[ETH_ALEN];
  4582. int used;
  4583. char *icon;
  4584. used = hwaddr_aton2(cmd, dst_addr);
  4585. if (used < 0)
  4586. return -1;
  4587. while (cmd[used] == ' ')
  4588. used++;
  4589. icon = &cmd[used];
  4590. wpa_s->fetch_osu_icon_in_progress = 0;
  4591. return hs20_anqp_send_req(wpa_s, dst_addr, BIT(HS20_STYPE_ICON_REQUEST),
  4592. (u8 *) icon, os_strlen(icon));
  4593. }
  4594. #endif /* CONFIG_HS20 */
  4595. static int wpa_supplicant_ctrl_iface_sta_autoconnect(
  4596. struct wpa_supplicant *wpa_s, char *cmd)
  4597. {
  4598. wpa_s->auto_reconnect_disabled = atoi(cmd) == 0 ? 1 : 0;
  4599. return 0;
  4600. }
  4601. #ifdef CONFIG_AUTOSCAN
  4602. static int wpa_supplicant_ctrl_iface_autoscan(struct wpa_supplicant *wpa_s,
  4603. char *cmd)
  4604. {
  4605. enum wpa_states state = wpa_s->wpa_state;
  4606. char *new_params = NULL;
  4607. if (os_strlen(cmd) > 0) {
  4608. new_params = os_strdup(cmd);
  4609. if (new_params == NULL)
  4610. return -1;
  4611. }
  4612. os_free(wpa_s->conf->autoscan);
  4613. wpa_s->conf->autoscan = new_params;
  4614. if (wpa_s->conf->autoscan == NULL)
  4615. autoscan_deinit(wpa_s);
  4616. else if (state == WPA_DISCONNECTED || state == WPA_INACTIVE)
  4617. autoscan_init(wpa_s, 1);
  4618. else if (state == WPA_SCANNING)
  4619. wpa_supplicant_reinit_autoscan(wpa_s);
  4620. return 0;
  4621. }
  4622. #endif /* CONFIG_AUTOSCAN */
  4623. #ifdef CONFIG_WNM
  4624. static int wpas_ctrl_iface_wnm_sleep(struct wpa_supplicant *wpa_s, char *cmd)
  4625. {
  4626. int enter;
  4627. int intval = 0;
  4628. char *pos;
  4629. int ret;
  4630. struct wpabuf *tfs_req = NULL;
  4631. if (os_strncmp(cmd, "enter", 5) == 0)
  4632. enter = 1;
  4633. else if (os_strncmp(cmd, "exit", 4) == 0)
  4634. enter = 0;
  4635. else
  4636. return -1;
  4637. pos = os_strstr(cmd, " interval=");
  4638. if (pos)
  4639. intval = atoi(pos + 10);
  4640. pos = os_strstr(cmd, " tfs_req=");
  4641. if (pos) {
  4642. char *end;
  4643. size_t len;
  4644. pos += 9;
  4645. end = os_strchr(pos, ' ');
  4646. if (end)
  4647. len = end - pos;
  4648. else
  4649. len = os_strlen(pos);
  4650. if (len & 1)
  4651. return -1;
  4652. len /= 2;
  4653. tfs_req = wpabuf_alloc(len);
  4654. if (tfs_req == NULL)
  4655. return -1;
  4656. if (hexstr2bin(pos, wpabuf_put(tfs_req, len), len) < 0) {
  4657. wpabuf_free(tfs_req);
  4658. return -1;
  4659. }
  4660. }
  4661. ret = ieee802_11_send_wnmsleep_req(wpa_s, enter ? WNM_SLEEP_MODE_ENTER :
  4662. WNM_SLEEP_MODE_EXIT, intval,
  4663. tfs_req);
  4664. wpabuf_free(tfs_req);
  4665. return ret;
  4666. }
  4667. static int wpas_ctrl_iface_wnm_bss_query(struct wpa_supplicant *wpa_s, char *cmd)
  4668. {
  4669. int query_reason;
  4670. query_reason = atoi(cmd);
  4671. wpa_printf(MSG_DEBUG, "CTRL_IFACE: WNM_BSS_QUERY query_reason=%d",
  4672. query_reason);
  4673. return wnm_send_bss_transition_mgmt_query(wpa_s, query_reason);
  4674. }
  4675. #endif /* CONFIG_WNM */
  4676. /* Get string representation of channel width */
  4677. static const char * channel_width_name(enum chan_width width)
  4678. {
  4679. switch (width) {
  4680. case CHAN_WIDTH_20_NOHT:
  4681. return "20 MHz (no HT)";
  4682. case CHAN_WIDTH_20:
  4683. return "20 MHz";
  4684. case CHAN_WIDTH_40:
  4685. return "40 MHz";
  4686. case CHAN_WIDTH_80:
  4687. return "80 MHz";
  4688. case CHAN_WIDTH_80P80:
  4689. return "80+80 MHz";
  4690. case CHAN_WIDTH_160:
  4691. return "160 MHz";
  4692. default:
  4693. return "unknown";
  4694. }
  4695. }
  4696. static int wpa_supplicant_signal_poll(struct wpa_supplicant *wpa_s, char *buf,
  4697. size_t buflen)
  4698. {
  4699. struct wpa_signal_info si;
  4700. int ret;
  4701. char *pos, *end;
  4702. ret = wpa_drv_signal_poll(wpa_s, &si);
  4703. if (ret)
  4704. return -1;
  4705. pos = buf;
  4706. end = buf + buflen;
  4707. ret = os_snprintf(pos, end - pos, "RSSI=%d\nLINKSPEED=%d\n"
  4708. "NOISE=%d\nFREQUENCY=%u\n",
  4709. si.current_signal, si.current_txrate / 1000,
  4710. si.current_noise, si.frequency);
  4711. if (ret < 0 || ret > end - pos)
  4712. return -1;
  4713. pos += ret;
  4714. if (si.chanwidth != CHAN_WIDTH_UNKNOWN) {
  4715. ret = os_snprintf(pos, end - pos, "WIDTH=%s\n",
  4716. channel_width_name(si.chanwidth));
  4717. if (ret < 0 || ret > end - pos)
  4718. return -1;
  4719. pos += ret;
  4720. }
  4721. if (si.center_frq1 > 0 && si.center_frq2 > 0) {
  4722. ret = os_snprintf(pos, end - pos,
  4723. "CENTER_FRQ1=%d\nCENTER_FRQ2=%d\n",
  4724. si.center_frq1, si.center_frq2);
  4725. if (ret < 0 || ret > end - pos)
  4726. return -1;
  4727. pos += ret;
  4728. }
  4729. if (si.avg_signal) {
  4730. ret = os_snprintf(pos, end - pos,
  4731. "AVG_RSSI=%d\n", si.avg_signal);
  4732. if (ret < 0 || ret >= end - pos)
  4733. return -1;
  4734. pos += ret;
  4735. }
  4736. return pos - buf;
  4737. }
  4738. static int wpa_supplicant_pktcnt_poll(struct wpa_supplicant *wpa_s, char *buf,
  4739. size_t buflen)
  4740. {
  4741. struct hostap_sta_driver_data sta;
  4742. int ret;
  4743. ret = wpa_drv_pktcnt_poll(wpa_s, &sta);
  4744. if (ret)
  4745. return -1;
  4746. ret = os_snprintf(buf, buflen, "TXGOOD=%lu\nTXBAD=%lu\nRXGOOD=%lu\n",
  4747. sta.tx_packets, sta.tx_retry_failed, sta.rx_packets);
  4748. if (ret < 0 || (size_t) ret > buflen)
  4749. return -1;
  4750. return ret;
  4751. }
  4752. #ifdef ANDROID
  4753. static int wpa_supplicant_driver_cmd(struct wpa_supplicant *wpa_s, char *cmd,
  4754. char *buf, size_t buflen)
  4755. {
  4756. int ret;
  4757. ret = wpa_drv_driver_cmd(wpa_s, cmd, buf, buflen);
  4758. if (ret == 0) {
  4759. if (os_strncasecmp(cmd, "COUNTRY", 7) == 0) {
  4760. struct p2p_data *p2p = wpa_s->global->p2p;
  4761. if (p2p) {
  4762. char country[3];
  4763. country[0] = cmd[8];
  4764. country[1] = cmd[9];
  4765. country[2] = 0x04;
  4766. p2p_set_country(p2p, country);
  4767. }
  4768. }
  4769. ret = os_snprintf(buf, buflen, "%s\n", "OK");
  4770. }
  4771. return ret;
  4772. }
  4773. #endif /* ANDROID */
  4774. static int wpa_supplicant_vendor_cmd(struct wpa_supplicant *wpa_s, char *cmd,
  4775. char *buf, size_t buflen)
  4776. {
  4777. int ret;
  4778. char *pos;
  4779. u8 *data = NULL;
  4780. unsigned int vendor_id, subcmd;
  4781. struct wpabuf *reply;
  4782. size_t data_len = 0;
  4783. /* cmd: <vendor id> <subcommand id> [<hex formatted data>] */
  4784. vendor_id = strtoul(cmd, &pos, 16);
  4785. if (!isblank(*pos))
  4786. return -EINVAL;
  4787. subcmd = strtoul(pos, &pos, 10);
  4788. if (*pos != '\0') {
  4789. if (!isblank(*pos++))
  4790. return -EINVAL;
  4791. data_len = os_strlen(pos);
  4792. }
  4793. if (data_len) {
  4794. data_len /= 2;
  4795. data = os_malloc(data_len);
  4796. if (!data)
  4797. return -1;
  4798. if (hexstr2bin(pos, data, data_len)) {
  4799. wpa_printf(MSG_DEBUG,
  4800. "Vendor command: wrong parameter format");
  4801. os_free(data);
  4802. return -EINVAL;
  4803. }
  4804. }
  4805. reply = wpabuf_alloc((buflen - 1) / 2);
  4806. if (!reply) {
  4807. os_free(data);
  4808. return -1;
  4809. }
  4810. ret = wpa_drv_vendor_cmd(wpa_s, vendor_id, subcmd, data, data_len,
  4811. reply);
  4812. if (ret == 0)
  4813. ret = wpa_snprintf_hex(buf, buflen, wpabuf_head_u8(reply),
  4814. wpabuf_len(reply));
  4815. wpabuf_free(reply);
  4816. os_free(data);
  4817. return ret;
  4818. }
  4819. static void wpa_supplicant_ctrl_iface_flush(struct wpa_supplicant *wpa_s)
  4820. {
  4821. wpa_dbg(wpa_s, MSG_DEBUG, "Flush all wpa_supplicant state");
  4822. #ifdef CONFIG_P2P
  4823. wpas_p2p_cancel(wpa_s);
  4824. wpas_p2p_stop_find(wpa_s);
  4825. p2p_ctrl_flush(wpa_s);
  4826. wpas_p2p_group_remove(wpa_s, "*");
  4827. wpas_p2p_service_flush(wpa_s);
  4828. wpa_s->global->p2p_disabled = 0;
  4829. wpa_s->global->p2p_per_sta_psk = 0;
  4830. wpa_s->conf->num_sec_device_types = 0;
  4831. wpa_s->p2p_disable_ip_addr_req = 0;
  4832. os_free(wpa_s->global->p2p_go_avoid_freq.range);
  4833. wpa_s->global->p2p_go_avoid_freq.range = NULL;
  4834. #endif /* CONFIG_P2P */
  4835. #ifdef CONFIG_WPS_TESTING
  4836. wps_version_number = 0x20;
  4837. wps_testing_dummy_cred = 0;
  4838. wps_corrupt_pkhash = 0;
  4839. #endif /* CONFIG_WPS_TESTING */
  4840. #ifdef CONFIG_WPS
  4841. wpa_s->wps_fragment_size = 0;
  4842. wpas_wps_cancel(wpa_s);
  4843. #endif /* CONFIG_WPS */
  4844. wpa_s->after_wps = 0;
  4845. wpa_s->known_wps_freq = 0;
  4846. #ifdef CONFIG_TDLS
  4847. #ifdef CONFIG_TDLS_TESTING
  4848. extern unsigned int tdls_testing;
  4849. tdls_testing = 0;
  4850. #endif /* CONFIG_TDLS_TESTING */
  4851. wpa_drv_tdls_oper(wpa_s, TDLS_ENABLE, NULL);
  4852. wpa_tdls_enable(wpa_s->wpa, 1);
  4853. #endif /* CONFIG_TDLS */
  4854. eloop_cancel_timeout(wpa_supplicant_stop_countermeasures, wpa_s, NULL);
  4855. wpa_supplicant_stop_countermeasures(wpa_s, NULL);
  4856. wpa_s->no_keep_alive = 0;
  4857. os_free(wpa_s->disallow_aps_bssid);
  4858. wpa_s->disallow_aps_bssid = NULL;
  4859. wpa_s->disallow_aps_bssid_count = 0;
  4860. os_free(wpa_s->disallow_aps_ssid);
  4861. wpa_s->disallow_aps_ssid = NULL;
  4862. wpa_s->disallow_aps_ssid_count = 0;
  4863. wpa_s->set_sta_uapsd = 0;
  4864. wpa_s->sta_uapsd = 0;
  4865. wpa_drv_radio_disable(wpa_s, 0);
  4866. wpa_bss_flush(wpa_s);
  4867. wpa_blacklist_clear(wpa_s);
  4868. wpa_s->extra_blacklist_count = 0;
  4869. wpa_supplicant_ctrl_iface_remove_network(wpa_s, "all");
  4870. wpa_supplicant_ctrl_iface_remove_cred(wpa_s, "all");
  4871. wpa_config_flush_blobs(wpa_s->conf);
  4872. wpa_s->conf->auto_interworking = 0;
  4873. wpa_s->conf->okc = 0;
  4874. wpa_sm_set_param(wpa_s->wpa, RSNA_PMK_LIFETIME, 43200);
  4875. wpa_sm_set_param(wpa_s->wpa, RSNA_PMK_REAUTH_THRESHOLD, 70);
  4876. wpa_sm_set_param(wpa_s->wpa, RSNA_SA_TIMEOUT, 60);
  4877. eapol_sm_notify_logoff(wpa_s->eapol, FALSE);
  4878. radio_remove_works(wpa_s, NULL, 1);
  4879. wpa_s->next_ssid = NULL;
  4880. #ifdef CONFIG_INTERWORKING
  4881. hs20_cancel_fetch_osu(wpa_s);
  4882. #endif /* CONFIG_INTERWORKING */
  4883. wpa_s->ext_mgmt_frame_handling = 0;
  4884. }
  4885. static int wpas_ctrl_radio_work_show(struct wpa_supplicant *wpa_s,
  4886. char *buf, size_t buflen)
  4887. {
  4888. struct wpa_radio_work *work;
  4889. char *pos, *end;
  4890. struct os_reltime now, diff;
  4891. pos = buf;
  4892. end = buf + buflen;
  4893. os_get_reltime(&now);
  4894. dl_list_for_each(work, &wpa_s->radio->work, struct wpa_radio_work, list)
  4895. {
  4896. int ret;
  4897. os_reltime_sub(&now, &work->time, &diff);
  4898. ret = os_snprintf(pos, end - pos, "%s@%s:%u:%u:%ld.%06ld\n",
  4899. work->type, work->wpa_s->ifname, work->freq,
  4900. work->started, diff.sec, diff.usec);
  4901. if (ret < 0 || ret >= end - pos)
  4902. break;
  4903. pos += ret;
  4904. }
  4905. return pos - buf;
  4906. }
  4907. static void wpas_ctrl_radio_work_timeout(void *eloop_ctx, void *timeout_ctx)
  4908. {
  4909. struct wpa_radio_work *work = eloop_ctx;
  4910. struct wpa_external_work *ework = work->ctx;
  4911. wpa_dbg(work->wpa_s, MSG_DEBUG,
  4912. "Timing out external radio work %u (%s)",
  4913. ework->id, work->type);
  4914. wpa_msg(work->wpa_s, MSG_INFO, EXT_RADIO_WORK_TIMEOUT "%u", ework->id);
  4915. radio_work_done(work);
  4916. os_free(ework);
  4917. }
  4918. static void wpas_ctrl_radio_work_cb(struct wpa_radio_work *work, int deinit)
  4919. {
  4920. struct wpa_external_work *ework = work->ctx;
  4921. if (deinit) {
  4922. if (work->started)
  4923. eloop_cancel_timeout(wpas_ctrl_radio_work_timeout,
  4924. work, NULL);
  4925. os_free(ework);
  4926. return;
  4927. }
  4928. wpa_dbg(work->wpa_s, MSG_DEBUG, "Starting external radio work %u (%s)",
  4929. ework->id, ework->type);
  4930. wpa_msg(work->wpa_s, MSG_INFO, EXT_RADIO_WORK_START "%u", ework->id);
  4931. if (!ework->timeout)
  4932. ework->timeout = 10;
  4933. eloop_register_timeout(ework->timeout, 0, wpas_ctrl_radio_work_timeout,
  4934. work, NULL);
  4935. }
  4936. static int wpas_ctrl_radio_work_add(struct wpa_supplicant *wpa_s, char *cmd,
  4937. char *buf, size_t buflen)
  4938. {
  4939. struct wpa_external_work *ework;
  4940. char *pos, *pos2;
  4941. size_t type_len;
  4942. int ret;
  4943. unsigned int freq = 0;
  4944. /* format: <name> [freq=<MHz>] [timeout=<seconds>] */
  4945. ework = os_zalloc(sizeof(*ework));
  4946. if (ework == NULL)
  4947. return -1;
  4948. pos = os_strchr(cmd, ' ');
  4949. if (pos) {
  4950. type_len = pos - cmd;
  4951. pos++;
  4952. pos2 = os_strstr(pos, "freq=");
  4953. if (pos2)
  4954. freq = atoi(pos2 + 5);
  4955. pos2 = os_strstr(pos, "timeout=");
  4956. if (pos2)
  4957. ework->timeout = atoi(pos2 + 8);
  4958. } else {
  4959. type_len = os_strlen(cmd);
  4960. }
  4961. if (4 + type_len >= sizeof(ework->type))
  4962. type_len = sizeof(ework->type) - 4 - 1;
  4963. os_strlcpy(ework->type, "ext:", sizeof(ework->type));
  4964. os_memcpy(ework->type + 4, cmd, type_len);
  4965. ework->type[4 + type_len] = '\0';
  4966. wpa_s->ext_work_id++;
  4967. if (wpa_s->ext_work_id == 0)
  4968. wpa_s->ext_work_id++;
  4969. ework->id = wpa_s->ext_work_id;
  4970. if (radio_add_work(wpa_s, freq, ework->type, 0, wpas_ctrl_radio_work_cb,
  4971. ework) < 0) {
  4972. os_free(ework);
  4973. return -1;
  4974. }
  4975. ret = os_snprintf(buf, buflen, "%u", ework->id);
  4976. if (ret < 0 || (size_t) ret >= buflen)
  4977. return -1;
  4978. return ret;
  4979. }
  4980. static int wpas_ctrl_radio_work_done(struct wpa_supplicant *wpa_s, char *cmd)
  4981. {
  4982. struct wpa_radio_work *work;
  4983. unsigned int id = atoi(cmd);
  4984. dl_list_for_each(work, &wpa_s->radio->work, struct wpa_radio_work, list)
  4985. {
  4986. struct wpa_external_work *ework;
  4987. if (os_strncmp(work->type, "ext:", 4) != 0)
  4988. continue;
  4989. ework = work->ctx;
  4990. if (id && ework->id != id)
  4991. continue;
  4992. wpa_dbg(wpa_s, MSG_DEBUG,
  4993. "Completed external radio work %u (%s)",
  4994. ework->id, ework->type);
  4995. eloop_cancel_timeout(wpas_ctrl_radio_work_timeout, work, NULL);
  4996. radio_work_done(work);
  4997. os_free(ework);
  4998. return 3; /* "OK\n" */
  4999. }
  5000. return -1;
  5001. }
  5002. static int wpas_ctrl_radio_work(struct wpa_supplicant *wpa_s, char *cmd,
  5003. char *buf, size_t buflen)
  5004. {
  5005. if (os_strcmp(cmd, "show") == 0)
  5006. return wpas_ctrl_radio_work_show(wpa_s, buf, buflen);
  5007. if (os_strncmp(cmd, "add ", 4) == 0)
  5008. return wpas_ctrl_radio_work_add(wpa_s, cmd + 4, buf, buflen);
  5009. if (os_strncmp(cmd, "done ", 5) == 0)
  5010. return wpas_ctrl_radio_work_done(wpa_s, cmd + 4);
  5011. return -1;
  5012. }
  5013. void wpas_ctrl_radio_work_flush(struct wpa_supplicant *wpa_s)
  5014. {
  5015. struct wpa_radio_work *work, *tmp;
  5016. if (!wpa_s || !wpa_s->radio)
  5017. return;
  5018. dl_list_for_each_safe(work, tmp, &wpa_s->radio->work,
  5019. struct wpa_radio_work, list) {
  5020. struct wpa_external_work *ework;
  5021. if (os_strncmp(work->type, "ext:", 4) != 0)
  5022. continue;
  5023. ework = work->ctx;
  5024. wpa_dbg(wpa_s, MSG_DEBUG,
  5025. "Flushing%s external radio work %u (%s)",
  5026. work->started ? " started" : "", ework->id,
  5027. ework->type);
  5028. if (work->started)
  5029. eloop_cancel_timeout(wpas_ctrl_radio_work_timeout,
  5030. work, NULL);
  5031. radio_work_done(work);
  5032. os_free(ework);
  5033. }
  5034. }
  5035. static void wpas_ctrl_eapol_response(void *eloop_ctx, void *timeout_ctx)
  5036. {
  5037. struct wpa_supplicant *wpa_s = eloop_ctx;
  5038. eapol_sm_notify_ctrl_response(wpa_s->eapol);
  5039. }
  5040. static int set_scan_freqs(struct wpa_supplicant *wpa_s, char *val)
  5041. {
  5042. int *freqs = NULL;
  5043. freqs = freq_range_to_channel_list(wpa_s, val);
  5044. if (freqs == NULL)
  5045. return -1;
  5046. os_free(wpa_s->manual_scan_freqs);
  5047. wpa_s->manual_scan_freqs = freqs;
  5048. return 0;
  5049. }
  5050. static int scan_id_list_parse(struct wpa_supplicant *wpa_s, const char *value)
  5051. {
  5052. const char *pos = value;
  5053. while (pos) {
  5054. if (*pos == ' ' || *pos == '\0')
  5055. break;
  5056. if (wpa_s->scan_id_count == MAX_SCAN_ID)
  5057. return -1;
  5058. wpa_s->scan_id[wpa_s->scan_id_count++] = atoi(pos);
  5059. pos = os_strchr(pos, ',');
  5060. if (pos)
  5061. pos++;
  5062. }
  5063. return 0;
  5064. }
  5065. static void wpas_ctrl_scan(struct wpa_supplicant *wpa_s, char *params,
  5066. char *reply, int reply_size, int *reply_len)
  5067. {
  5068. char *pos;
  5069. if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED) {
  5070. *reply_len = -1;
  5071. return;
  5072. }
  5073. wpa_s->manual_scan_passive = 0;
  5074. wpa_s->manual_scan_use_id = 0;
  5075. wpa_s->manual_scan_only_new = 0;
  5076. wpa_s->scan_id_count = 0;
  5077. if (params) {
  5078. if (os_strncasecmp(params, "TYPE=ONLY", 9) == 0)
  5079. wpa_s->scan_res_handler = scan_only_handler;
  5080. pos = os_strstr(params, "freq=");
  5081. if (pos && set_scan_freqs(wpa_s, pos + 5) < 0) {
  5082. *reply_len = -1;
  5083. return;
  5084. }
  5085. pos = os_strstr(params, "passive=");
  5086. if (pos)
  5087. wpa_s->manual_scan_passive = !!atoi(pos + 8);
  5088. pos = os_strstr(params, "use_id=");
  5089. if (pos)
  5090. wpa_s->manual_scan_use_id = atoi(pos + 7);
  5091. pos = os_strstr(params, "only_new=1");
  5092. if (pos)
  5093. wpa_s->manual_scan_only_new = 1;
  5094. pos = os_strstr(params, "scan_id=");
  5095. if (pos && scan_id_list_parse(wpa_s, pos + 8) < 0) {
  5096. *reply_len = -1;
  5097. return;
  5098. }
  5099. } else {
  5100. os_free(wpa_s->manual_scan_freqs);
  5101. wpa_s->manual_scan_freqs = NULL;
  5102. if (wpa_s->scan_res_handler == scan_only_handler)
  5103. wpa_s->scan_res_handler = NULL;
  5104. }
  5105. if (!wpa_s->sched_scanning && !wpa_s->scanning &&
  5106. ((wpa_s->wpa_state <= WPA_SCANNING) ||
  5107. (wpa_s->wpa_state == WPA_COMPLETED))) {
  5108. wpa_s->normal_scans = 0;
  5109. wpa_s->scan_req = MANUAL_SCAN_REQ;
  5110. wpa_s->after_wps = 0;
  5111. wpa_s->known_wps_freq = 0;
  5112. wpa_supplicant_req_scan(wpa_s, 0, 0);
  5113. if (wpa_s->manual_scan_use_id) {
  5114. wpa_s->manual_scan_id++;
  5115. wpa_dbg(wpa_s, MSG_DEBUG, "Assigned scan id %u",
  5116. wpa_s->manual_scan_id);
  5117. *reply_len = os_snprintf(reply, reply_size, "%u\n",
  5118. wpa_s->manual_scan_id);
  5119. }
  5120. } else if (wpa_s->sched_scanning) {
  5121. wpa_printf(MSG_DEBUG, "Stop ongoing sched_scan to allow requested full scan to proceed");
  5122. wpa_supplicant_cancel_sched_scan(wpa_s);
  5123. wpa_s->scan_req = MANUAL_SCAN_REQ;
  5124. wpa_supplicant_req_scan(wpa_s, 0, 0);
  5125. if (wpa_s->manual_scan_use_id) {
  5126. wpa_s->manual_scan_id++;
  5127. *reply_len = os_snprintf(reply, reply_size, "%u\n",
  5128. wpa_s->manual_scan_id);
  5129. wpa_dbg(wpa_s, MSG_DEBUG, "Assigned scan id %u",
  5130. wpa_s->manual_scan_id);
  5131. }
  5132. } else {
  5133. wpa_printf(MSG_DEBUG, "Ongoing scan action - reject new request");
  5134. *reply_len = os_snprintf(reply, reply_size, "FAIL-BUSY\n");
  5135. }
  5136. }
  5137. #ifdef CONFIG_TESTING_OPTIONS
  5138. static void wpas_ctrl_iface_mgmt_tx_cb(struct wpa_supplicant *wpa_s,
  5139. unsigned int freq, const u8 *dst,
  5140. const u8 *src, const u8 *bssid,
  5141. const u8 *data, size_t data_len,
  5142. enum offchannel_send_action_result
  5143. result)
  5144. {
  5145. wpa_msg(wpa_s, MSG_INFO, "MGMT-TX-STATUS freq=%u dst=" MACSTR
  5146. " src=" MACSTR " bssid=" MACSTR " result=%s",
  5147. freq, MAC2STR(dst), MAC2STR(src), MAC2STR(bssid),
  5148. result == OFFCHANNEL_SEND_ACTION_SUCCESS ?
  5149. "SUCCESS" : (result == OFFCHANNEL_SEND_ACTION_NO_ACK ?
  5150. "NO_ACK" : "FAILED"));
  5151. }
  5152. static int wpas_ctrl_iface_mgmt_tx(struct wpa_supplicant *wpa_s, char *cmd)
  5153. {
  5154. char *pos, *param;
  5155. size_t len;
  5156. u8 *buf, da[ETH_ALEN], bssid[ETH_ALEN];
  5157. int res, used;
  5158. int freq = 0, no_cck = 0, wait_time = 0;
  5159. /* <DA> <BSSID> [freq=<MHz>] [wait_time=<ms>] [no_cck=1]
  5160. * <action=Action frame payload> */
  5161. wpa_printf(MSG_DEBUG, "External MGMT TX: %s", cmd);
  5162. pos = cmd;
  5163. used = hwaddr_aton2(pos, da);
  5164. if (used < 0)
  5165. return -1;
  5166. pos += used;
  5167. while (*pos == ' ')
  5168. pos++;
  5169. used = hwaddr_aton2(pos, bssid);
  5170. if (used < 0)
  5171. return -1;
  5172. pos += used;
  5173. param = os_strstr(pos, " freq=");
  5174. if (param) {
  5175. param += 6;
  5176. freq = atoi(param);
  5177. }
  5178. param = os_strstr(pos, " no_cck=");
  5179. if (param) {
  5180. param += 8;
  5181. no_cck = atoi(param);
  5182. }
  5183. param = os_strstr(pos, " wait_time=");
  5184. if (param) {
  5185. param += 11;
  5186. wait_time = atoi(param);
  5187. }
  5188. param = os_strstr(pos, " action=");
  5189. if (param == NULL)
  5190. return -1;
  5191. param += 8;
  5192. len = os_strlen(param);
  5193. if (len & 1)
  5194. return -1;
  5195. len /= 2;
  5196. buf = os_malloc(len);
  5197. if (buf == NULL)
  5198. return -1;
  5199. if (hexstr2bin(param, buf, len) < 0) {
  5200. os_free(buf);
  5201. return -1;
  5202. }
  5203. res = offchannel_send_action(wpa_s, freq, da, wpa_s->own_addr, bssid,
  5204. buf, len, wait_time,
  5205. wpas_ctrl_iface_mgmt_tx_cb, no_cck);
  5206. os_free(buf);
  5207. return res;
  5208. }
  5209. static void wpas_ctrl_iface_mgmt_tx_done(struct wpa_supplicant *wpa_s)
  5210. {
  5211. wpa_printf(MSG_DEBUG, "External MGMT TX - done waiting");
  5212. offchannel_send_action_done(wpa_s);
  5213. }
  5214. static int wpas_ctrl_iface_driver_event(struct wpa_supplicant *wpa_s, char *cmd)
  5215. {
  5216. char *pos, *param;
  5217. union wpa_event_data event;
  5218. enum wpa_event_type ev;
  5219. /* <event name> [parameters..] */
  5220. wpa_dbg(wpa_s, MSG_DEBUG, "Testing - external driver event: %s", cmd);
  5221. pos = cmd;
  5222. param = os_strchr(pos, ' ');
  5223. if (param)
  5224. *param++ = '\0';
  5225. os_memset(&event, 0, sizeof(event));
  5226. if (os_strcmp(cmd, "INTERFACE_ENABLED") == 0) {
  5227. ev = EVENT_INTERFACE_ENABLED;
  5228. } else if (os_strcmp(cmd, "INTERFACE_DISABLED") == 0) {
  5229. ev = EVENT_INTERFACE_DISABLED;
  5230. } else if (os_strcmp(cmd, "AVOID_FREQUENCIES") == 0) {
  5231. ev = EVENT_AVOID_FREQUENCIES;
  5232. if (param == NULL)
  5233. param = "";
  5234. if (freq_range_list_parse(&event.freq_range, param) < 0)
  5235. return -1;
  5236. wpa_supplicant_event(wpa_s, ev, &event);
  5237. os_free(event.freq_range.range);
  5238. return 0;
  5239. } else {
  5240. wpa_dbg(wpa_s, MSG_DEBUG, "Testing - unknown driver event: %s",
  5241. cmd);
  5242. return -1;
  5243. }
  5244. wpa_supplicant_event(wpa_s, ev, &event);
  5245. return 0;
  5246. }
  5247. #endif /* CONFIG_TESTING_OPTIONS */
  5248. static void wpas_ctrl_vendor_elem_update(struct wpa_supplicant *wpa_s)
  5249. {
  5250. unsigned int i;
  5251. char buf[30];
  5252. wpa_printf(MSG_DEBUG, "Update vendor elements");
  5253. for (i = 0; i < NUM_VENDOR_ELEM_FRAMES; i++) {
  5254. if (wpa_s->vendor_elem[i]) {
  5255. os_snprintf(buf, sizeof(buf), "frame[%u]", i);
  5256. wpa_hexdump_buf(MSG_DEBUG, buf, wpa_s->vendor_elem[i]);
  5257. }
  5258. }
  5259. #ifdef CONFIG_P2P
  5260. if (wpa_s->parent == wpa_s &&
  5261. wpa_s->global->p2p &&
  5262. !wpa_s->global->p2p_disabled)
  5263. p2p_set_vendor_elems(wpa_s->global->p2p, wpa_s->vendor_elem);
  5264. #endif /* CONFIG_P2P */
  5265. }
  5266. static struct wpa_supplicant *
  5267. wpas_ctrl_vendor_elem_iface(struct wpa_supplicant *wpa_s,
  5268. enum wpa_vendor_elem_frame frame)
  5269. {
  5270. switch (frame) {
  5271. #ifdef CONFIG_P2P
  5272. case VENDOR_ELEM_PROBE_REQ_P2P:
  5273. case VENDOR_ELEM_PROBE_RESP_P2P:
  5274. case VENDOR_ELEM_PROBE_RESP_P2P_GO:
  5275. case VENDOR_ELEM_BEACON_P2P_GO:
  5276. case VENDOR_ELEM_P2P_PD_REQ:
  5277. case VENDOR_ELEM_P2P_PD_RESP:
  5278. case VENDOR_ELEM_P2P_GO_NEG_REQ:
  5279. case VENDOR_ELEM_P2P_GO_NEG_RESP:
  5280. case VENDOR_ELEM_P2P_GO_NEG_CONF:
  5281. case VENDOR_ELEM_P2P_INV_REQ:
  5282. case VENDOR_ELEM_P2P_INV_RESP:
  5283. case VENDOR_ELEM_P2P_ASSOC_REQ:
  5284. return wpa_s->parent;
  5285. #endif /* CONFIG_P2P */
  5286. default:
  5287. return wpa_s;
  5288. }
  5289. }
  5290. static int wpas_ctrl_vendor_elem_add(struct wpa_supplicant *wpa_s, char *cmd)
  5291. {
  5292. char *pos = cmd;
  5293. int frame;
  5294. size_t len;
  5295. struct wpabuf *buf;
  5296. struct ieee802_11_elems elems;
  5297. frame = atoi(pos);
  5298. if (frame < 0 || frame >= NUM_VENDOR_ELEM_FRAMES)
  5299. return -1;
  5300. wpa_s = wpas_ctrl_vendor_elem_iface(wpa_s, frame);
  5301. pos = os_strchr(pos, ' ');
  5302. if (pos == NULL)
  5303. return -1;
  5304. pos++;
  5305. len = os_strlen(pos);
  5306. if (len == 0)
  5307. return 0;
  5308. if (len & 1)
  5309. return -1;
  5310. len /= 2;
  5311. buf = wpabuf_alloc(len);
  5312. if (buf == NULL)
  5313. return -1;
  5314. if (hexstr2bin(pos, wpabuf_put(buf, len), len) < 0) {
  5315. wpabuf_free(buf);
  5316. return -1;
  5317. }
  5318. if (ieee802_11_parse_elems(wpabuf_head_u8(buf), len, &elems, 0) ==
  5319. ParseFailed) {
  5320. wpabuf_free(buf);
  5321. return -1;
  5322. }
  5323. if (wpa_s->vendor_elem[frame] == NULL) {
  5324. wpa_s->vendor_elem[frame] = buf;
  5325. wpas_ctrl_vendor_elem_update(wpa_s);
  5326. return 0;
  5327. }
  5328. if (wpabuf_resize(&wpa_s->vendor_elem[frame], len) < 0) {
  5329. wpabuf_free(buf);
  5330. return -1;
  5331. }
  5332. wpabuf_put_buf(wpa_s->vendor_elem[frame], buf);
  5333. wpabuf_free(buf);
  5334. wpas_ctrl_vendor_elem_update(wpa_s);
  5335. return 0;
  5336. }
  5337. static int wpas_ctrl_vendor_elem_get(struct wpa_supplicant *wpa_s, char *cmd,
  5338. char *buf, size_t buflen)
  5339. {
  5340. int frame = atoi(cmd);
  5341. if (frame < 0 || frame >= NUM_VENDOR_ELEM_FRAMES)
  5342. return -1;
  5343. wpa_s = wpas_ctrl_vendor_elem_iface(wpa_s, frame);
  5344. if (wpa_s->vendor_elem[frame] == NULL)
  5345. return 0;
  5346. return wpa_snprintf_hex(buf, buflen,
  5347. wpabuf_head_u8(wpa_s->vendor_elem[frame]),
  5348. wpabuf_len(wpa_s->vendor_elem[frame]));
  5349. }
  5350. static int wpas_ctrl_vendor_elem_remove(struct wpa_supplicant *wpa_s, char *cmd)
  5351. {
  5352. char *pos = cmd;
  5353. int frame;
  5354. size_t len;
  5355. u8 *buf;
  5356. struct ieee802_11_elems elems;
  5357. u8 *ie, *end;
  5358. frame = atoi(pos);
  5359. if (frame < 0 || frame >= NUM_VENDOR_ELEM_FRAMES)
  5360. return -1;
  5361. wpa_s = wpas_ctrl_vendor_elem_iface(wpa_s, frame);
  5362. pos = os_strchr(pos, ' ');
  5363. if (pos == NULL)
  5364. return -1;
  5365. pos++;
  5366. if (*pos == '*') {
  5367. wpabuf_free(wpa_s->vendor_elem[frame]);
  5368. wpa_s->vendor_elem[frame] = NULL;
  5369. wpas_ctrl_vendor_elem_update(wpa_s);
  5370. return 0;
  5371. }
  5372. if (wpa_s->vendor_elem[frame] == NULL)
  5373. return -1;
  5374. len = os_strlen(pos);
  5375. if (len == 0)
  5376. return 0;
  5377. if (len & 1)
  5378. return -1;
  5379. len /= 2;
  5380. buf = os_malloc(len);
  5381. if (buf == NULL)
  5382. return -1;
  5383. if (hexstr2bin(pos, buf, len) < 0) {
  5384. os_free(buf);
  5385. return -1;
  5386. }
  5387. if (ieee802_11_parse_elems(buf, len, &elems, 0) == ParseFailed) {
  5388. os_free(buf);
  5389. return -1;
  5390. }
  5391. ie = wpabuf_mhead_u8(wpa_s->vendor_elem[frame]);
  5392. end = ie + wpabuf_len(wpa_s->vendor_elem[frame]);
  5393. for (; ie + 1 < end; ie += 2 + ie[1]) {
  5394. if (ie + len > end)
  5395. break;
  5396. if (os_memcmp(ie, buf, len) != 0)
  5397. continue;
  5398. if (wpabuf_len(wpa_s->vendor_elem[frame]) == len) {
  5399. wpabuf_free(wpa_s->vendor_elem[frame]);
  5400. wpa_s->vendor_elem[frame] = NULL;
  5401. } else {
  5402. os_memmove(ie, ie + len,
  5403. wpabuf_len(wpa_s->vendor_elem[frame]) - len);
  5404. wpa_s->vendor_elem[frame]->used -= len;
  5405. }
  5406. os_free(buf);
  5407. wpas_ctrl_vendor_elem_update(wpa_s);
  5408. return 0;
  5409. }
  5410. os_free(buf);
  5411. return -1;
  5412. }
  5413. char * wpa_supplicant_ctrl_iface_process(struct wpa_supplicant *wpa_s,
  5414. char *buf, size_t *resp_len)
  5415. {
  5416. char *reply;
  5417. const int reply_size = 4096;
  5418. int reply_len;
  5419. if (os_strncmp(buf, WPA_CTRL_RSP, os_strlen(WPA_CTRL_RSP)) == 0 ||
  5420. os_strncmp(buf, "SET_NETWORK ", 12) == 0) {
  5421. if (wpa_debug_show_keys)
  5422. wpa_dbg(wpa_s, MSG_DEBUG,
  5423. "Control interface command '%s'", buf);
  5424. else
  5425. wpa_dbg(wpa_s, MSG_DEBUG,
  5426. "Control interface command '%s [REMOVED]'",
  5427. os_strncmp(buf, WPA_CTRL_RSP,
  5428. os_strlen(WPA_CTRL_RSP)) == 0 ?
  5429. WPA_CTRL_RSP : "SET_NETWORK");
  5430. } else if (os_strncmp(buf, "WPS_NFC_TAG_READ", 16) == 0 ||
  5431. os_strncmp(buf, "NFC_REPORT_HANDOVER", 19) == 0) {
  5432. wpa_hexdump_ascii_key(MSG_DEBUG, "RX ctrl_iface",
  5433. (const u8 *) buf, os_strlen(buf));
  5434. } else {
  5435. int level = MSG_DEBUG;
  5436. if (os_strcmp(buf, "PING") == 0)
  5437. level = MSG_EXCESSIVE;
  5438. wpa_dbg(wpa_s, level, "Control interface command '%s'", buf);
  5439. }
  5440. reply = os_malloc(reply_size);
  5441. if (reply == NULL) {
  5442. *resp_len = 1;
  5443. return NULL;
  5444. }
  5445. os_memcpy(reply, "OK\n", 3);
  5446. reply_len = 3;
  5447. if (os_strcmp(buf, "PING") == 0) {
  5448. os_memcpy(reply, "PONG\n", 5);
  5449. reply_len = 5;
  5450. } else if (os_strcmp(buf, "IFNAME") == 0) {
  5451. reply_len = os_strlen(wpa_s->ifname);
  5452. os_memcpy(reply, wpa_s->ifname, reply_len);
  5453. } else if (os_strncmp(buf, "RELOG", 5) == 0) {
  5454. if (wpa_debug_reopen_file() < 0)
  5455. reply_len = -1;
  5456. } else if (os_strncmp(buf, "NOTE ", 5) == 0) {
  5457. wpa_printf(MSG_INFO, "NOTE: %s", buf + 5);
  5458. } else if (os_strcmp(buf, "MIB") == 0) {
  5459. reply_len = wpa_sm_get_mib(wpa_s->wpa, reply, reply_size);
  5460. if (reply_len >= 0) {
  5461. int res;
  5462. res = eapol_sm_get_mib(wpa_s->eapol, reply + reply_len,
  5463. reply_size - reply_len);
  5464. if (res < 0)
  5465. reply_len = -1;
  5466. else
  5467. reply_len += res;
  5468. }
  5469. } else if (os_strncmp(buf, "STATUS", 6) == 0) {
  5470. reply_len = wpa_supplicant_ctrl_iface_status(
  5471. wpa_s, buf + 6, reply, reply_size);
  5472. } else if (os_strcmp(buf, "PMKSA") == 0) {
  5473. reply_len = wpa_sm_pmksa_cache_list(wpa_s->wpa, reply,
  5474. reply_size);
  5475. } else if (os_strncmp(buf, "SET ", 4) == 0) {
  5476. if (wpa_supplicant_ctrl_iface_set(wpa_s, buf + 4))
  5477. reply_len = -1;
  5478. } else if (os_strncmp(buf, "GET ", 4) == 0) {
  5479. reply_len = wpa_supplicant_ctrl_iface_get(wpa_s, buf + 4,
  5480. reply, reply_size);
  5481. } else if (os_strcmp(buf, "LOGON") == 0) {
  5482. eapol_sm_notify_logoff(wpa_s->eapol, FALSE);
  5483. } else if (os_strcmp(buf, "LOGOFF") == 0) {
  5484. eapol_sm_notify_logoff(wpa_s->eapol, TRUE);
  5485. } else if (os_strcmp(buf, "REASSOCIATE") == 0) {
  5486. if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED)
  5487. reply_len = -1;
  5488. else
  5489. wpas_request_connection(wpa_s);
  5490. } else if (os_strcmp(buf, "REATTACH") == 0) {
  5491. if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED ||
  5492. !wpa_s->current_ssid)
  5493. reply_len = -1;
  5494. else {
  5495. wpa_s->reattach = 1;
  5496. wpas_request_connection(wpa_s);
  5497. }
  5498. } else if (os_strcmp(buf, "RECONNECT") == 0) {
  5499. if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED)
  5500. reply_len = -1;
  5501. else if (wpa_s->disconnected)
  5502. wpas_request_connection(wpa_s);
  5503. #ifdef IEEE8021X_EAPOL
  5504. } else if (os_strncmp(buf, "PREAUTH ", 8) == 0) {
  5505. if (wpa_supplicant_ctrl_iface_preauth(wpa_s, buf + 8))
  5506. reply_len = -1;
  5507. #endif /* IEEE8021X_EAPOL */
  5508. #ifdef CONFIG_PEERKEY
  5509. } else if (os_strncmp(buf, "STKSTART ", 9) == 0) {
  5510. if (wpa_supplicant_ctrl_iface_stkstart(wpa_s, buf + 9))
  5511. reply_len = -1;
  5512. #endif /* CONFIG_PEERKEY */
  5513. #ifdef CONFIG_IEEE80211R
  5514. } else if (os_strncmp(buf, "FT_DS ", 6) == 0) {
  5515. if (wpa_supplicant_ctrl_iface_ft_ds(wpa_s, buf + 6))
  5516. reply_len = -1;
  5517. #endif /* CONFIG_IEEE80211R */
  5518. #ifdef CONFIG_WPS
  5519. } else if (os_strcmp(buf, "WPS_PBC") == 0) {
  5520. int res = wpa_supplicant_ctrl_iface_wps_pbc(wpa_s, NULL);
  5521. if (res == -2) {
  5522. os_memcpy(reply, "FAIL-PBC-OVERLAP\n", 17);
  5523. reply_len = 17;
  5524. } else if (res)
  5525. reply_len = -1;
  5526. } else if (os_strncmp(buf, "WPS_PBC ", 8) == 0) {
  5527. int res = wpa_supplicant_ctrl_iface_wps_pbc(wpa_s, buf + 8);
  5528. if (res == -2) {
  5529. os_memcpy(reply, "FAIL-PBC-OVERLAP\n", 17);
  5530. reply_len = 17;
  5531. } else if (res)
  5532. reply_len = -1;
  5533. } else if (os_strncmp(buf, "WPS_PIN ", 8) == 0) {
  5534. reply_len = wpa_supplicant_ctrl_iface_wps_pin(wpa_s, buf + 8,
  5535. reply,
  5536. reply_size);
  5537. } else if (os_strncmp(buf, "WPS_CHECK_PIN ", 14) == 0) {
  5538. reply_len = wpa_supplicant_ctrl_iface_wps_check_pin(
  5539. wpa_s, buf + 14, reply, reply_size);
  5540. } else if (os_strcmp(buf, "WPS_CANCEL") == 0) {
  5541. if (wpas_wps_cancel(wpa_s))
  5542. reply_len = -1;
  5543. #ifdef CONFIG_WPS_NFC
  5544. } else if (os_strcmp(buf, "WPS_NFC") == 0) {
  5545. if (wpa_supplicant_ctrl_iface_wps_nfc(wpa_s, NULL))
  5546. reply_len = -1;
  5547. } else if (os_strncmp(buf, "WPS_NFC ", 8) == 0) {
  5548. if (wpa_supplicant_ctrl_iface_wps_nfc(wpa_s, buf + 8))
  5549. reply_len = -1;
  5550. } else if (os_strncmp(buf, "WPS_NFC_CONFIG_TOKEN ", 21) == 0) {
  5551. reply_len = wpa_supplicant_ctrl_iface_wps_nfc_config_token(
  5552. wpa_s, buf + 21, reply, reply_size);
  5553. } else if (os_strncmp(buf, "WPS_NFC_TOKEN ", 14) == 0) {
  5554. reply_len = wpa_supplicant_ctrl_iface_wps_nfc_token(
  5555. wpa_s, buf + 14, reply, reply_size);
  5556. } else if (os_strncmp(buf, "WPS_NFC_TAG_READ ", 17) == 0) {
  5557. if (wpa_supplicant_ctrl_iface_wps_nfc_tag_read(wpa_s,
  5558. buf + 17))
  5559. reply_len = -1;
  5560. } else if (os_strncmp(buf, "NFC_GET_HANDOVER_REQ ", 21) == 0) {
  5561. reply_len = wpas_ctrl_nfc_get_handover_req(
  5562. wpa_s, buf + 21, reply, reply_size);
  5563. } else if (os_strncmp(buf, "NFC_GET_HANDOVER_SEL ", 21) == 0) {
  5564. reply_len = wpas_ctrl_nfc_get_handover_sel(
  5565. wpa_s, buf + 21, reply, reply_size);
  5566. } else if (os_strncmp(buf, "NFC_REPORT_HANDOVER ", 20) == 0) {
  5567. if (wpas_ctrl_nfc_report_handover(wpa_s, buf + 20))
  5568. reply_len = -1;
  5569. #endif /* CONFIG_WPS_NFC */
  5570. } else if (os_strncmp(buf, "WPS_REG ", 8) == 0) {
  5571. if (wpa_supplicant_ctrl_iface_wps_reg(wpa_s, buf + 8))
  5572. reply_len = -1;
  5573. #ifdef CONFIG_AP
  5574. } else if (os_strncmp(buf, "WPS_AP_PIN ", 11) == 0) {
  5575. reply_len = wpa_supplicant_ctrl_iface_wps_ap_pin(
  5576. wpa_s, buf + 11, reply, reply_size);
  5577. #endif /* CONFIG_AP */
  5578. #ifdef CONFIG_WPS_ER
  5579. } else if (os_strcmp(buf, "WPS_ER_START") == 0) {
  5580. if (wpas_wps_er_start(wpa_s, NULL))
  5581. reply_len = -1;
  5582. } else if (os_strncmp(buf, "WPS_ER_START ", 13) == 0) {
  5583. if (wpas_wps_er_start(wpa_s, buf + 13))
  5584. reply_len = -1;
  5585. } else if (os_strcmp(buf, "WPS_ER_STOP") == 0) {
  5586. if (wpas_wps_er_stop(wpa_s))
  5587. reply_len = -1;
  5588. } else if (os_strncmp(buf, "WPS_ER_PIN ", 11) == 0) {
  5589. if (wpa_supplicant_ctrl_iface_wps_er_pin(wpa_s, buf + 11))
  5590. reply_len = -1;
  5591. } else if (os_strncmp(buf, "WPS_ER_PBC ", 11) == 0) {
  5592. int ret = wpas_wps_er_pbc(wpa_s, buf + 11);
  5593. if (ret == -2) {
  5594. os_memcpy(reply, "FAIL-PBC-OVERLAP\n", 17);
  5595. reply_len = 17;
  5596. } else if (ret == -3) {
  5597. os_memcpy(reply, "FAIL-UNKNOWN-UUID\n", 18);
  5598. reply_len = 18;
  5599. } else if (ret == -4) {
  5600. os_memcpy(reply, "FAIL-NO-AP-SETTINGS\n", 20);
  5601. reply_len = 20;
  5602. } else if (ret)
  5603. reply_len = -1;
  5604. } else if (os_strncmp(buf, "WPS_ER_LEARN ", 13) == 0) {
  5605. if (wpa_supplicant_ctrl_iface_wps_er_learn(wpa_s, buf + 13))
  5606. reply_len = -1;
  5607. } else if (os_strncmp(buf, "WPS_ER_SET_CONFIG ", 18) == 0) {
  5608. if (wpa_supplicant_ctrl_iface_wps_er_set_config(wpa_s,
  5609. buf + 18))
  5610. reply_len = -1;
  5611. } else if (os_strncmp(buf, "WPS_ER_CONFIG ", 14) == 0) {
  5612. if (wpa_supplicant_ctrl_iface_wps_er_config(wpa_s, buf + 14))
  5613. reply_len = -1;
  5614. #ifdef CONFIG_WPS_NFC
  5615. } else if (os_strncmp(buf, "WPS_ER_NFC_CONFIG_TOKEN ", 24) == 0) {
  5616. reply_len = wpa_supplicant_ctrl_iface_wps_er_nfc_config_token(
  5617. wpa_s, buf + 24, reply, reply_size);
  5618. #endif /* CONFIG_WPS_NFC */
  5619. #endif /* CONFIG_WPS_ER */
  5620. #endif /* CONFIG_WPS */
  5621. #ifdef CONFIG_IBSS_RSN
  5622. } else if (os_strncmp(buf, "IBSS_RSN ", 9) == 0) {
  5623. if (wpa_supplicant_ctrl_iface_ibss_rsn(wpa_s, buf + 9))
  5624. reply_len = -1;
  5625. #endif /* CONFIG_IBSS_RSN */
  5626. #ifdef CONFIG_P2P
  5627. } else if (os_strncmp(buf, "P2P_FIND ", 9) == 0) {
  5628. if (p2p_ctrl_find(wpa_s, buf + 9))
  5629. reply_len = -1;
  5630. } else if (os_strcmp(buf, "P2P_FIND") == 0) {
  5631. if (p2p_ctrl_find(wpa_s, ""))
  5632. reply_len = -1;
  5633. } else if (os_strcmp(buf, "P2P_STOP_FIND") == 0) {
  5634. wpas_p2p_stop_find(wpa_s);
  5635. } else if (os_strncmp(buf, "P2P_CONNECT ", 12) == 0) {
  5636. reply_len = p2p_ctrl_connect(wpa_s, buf + 12, reply,
  5637. reply_size);
  5638. } else if (os_strncmp(buf, "P2P_LISTEN ", 11) == 0) {
  5639. if (p2p_ctrl_listen(wpa_s, buf + 11))
  5640. reply_len = -1;
  5641. } else if (os_strcmp(buf, "P2P_LISTEN") == 0) {
  5642. if (p2p_ctrl_listen(wpa_s, ""))
  5643. reply_len = -1;
  5644. } else if (os_strncmp(buf, "P2P_GROUP_REMOVE ", 17) == 0) {
  5645. if (wpas_p2p_group_remove(wpa_s, buf + 17))
  5646. reply_len = -1;
  5647. } else if (os_strcmp(buf, "P2P_GROUP_ADD") == 0) {
  5648. if (wpas_p2p_group_add(wpa_s, 0, 0, 0, 0))
  5649. reply_len = -1;
  5650. } else if (os_strncmp(buf, "P2P_GROUP_ADD ", 14) == 0) {
  5651. if (p2p_ctrl_group_add(wpa_s, buf + 14))
  5652. reply_len = -1;
  5653. } else if (os_strncmp(buf, "P2P_PROV_DISC ", 14) == 0) {
  5654. if (p2p_ctrl_prov_disc(wpa_s, buf + 14))
  5655. reply_len = -1;
  5656. } else if (os_strcmp(buf, "P2P_GET_PASSPHRASE") == 0) {
  5657. reply_len = p2p_get_passphrase(wpa_s, reply, reply_size);
  5658. } else if (os_strncmp(buf, "P2P_SERV_DISC_REQ ", 18) == 0) {
  5659. reply_len = p2p_ctrl_serv_disc_req(wpa_s, buf + 18, reply,
  5660. reply_size);
  5661. } else if (os_strncmp(buf, "P2P_SERV_DISC_CANCEL_REQ ", 25) == 0) {
  5662. if (p2p_ctrl_serv_disc_cancel_req(wpa_s, buf + 25) < 0)
  5663. reply_len = -1;
  5664. } else if (os_strncmp(buf, "P2P_SERV_DISC_RESP ", 19) == 0) {
  5665. if (p2p_ctrl_serv_disc_resp(wpa_s, buf + 19) < 0)
  5666. reply_len = -1;
  5667. } else if (os_strcmp(buf, "P2P_SERVICE_UPDATE") == 0) {
  5668. wpas_p2p_sd_service_update(wpa_s);
  5669. } else if (os_strncmp(buf, "P2P_SERV_DISC_EXTERNAL ", 23) == 0) {
  5670. if (p2p_ctrl_serv_disc_external(wpa_s, buf + 23) < 0)
  5671. reply_len = -1;
  5672. } else if (os_strcmp(buf, "P2P_SERVICE_FLUSH") == 0) {
  5673. wpas_p2p_service_flush(wpa_s);
  5674. } else if (os_strncmp(buf, "P2P_SERVICE_ADD ", 16) == 0) {
  5675. if (p2p_ctrl_service_add(wpa_s, buf + 16) < 0)
  5676. reply_len = -1;
  5677. } else if (os_strncmp(buf, "P2P_SERVICE_DEL ", 16) == 0) {
  5678. if (p2p_ctrl_service_del(wpa_s, buf + 16) < 0)
  5679. reply_len = -1;
  5680. } else if (os_strncmp(buf, "P2P_REJECT ", 11) == 0) {
  5681. if (p2p_ctrl_reject(wpa_s, buf + 11) < 0)
  5682. reply_len = -1;
  5683. } else if (os_strncmp(buf, "P2P_INVITE ", 11) == 0) {
  5684. if (p2p_ctrl_invite(wpa_s, buf + 11) < 0)
  5685. reply_len = -1;
  5686. } else if (os_strncmp(buf, "P2P_PEER ", 9) == 0) {
  5687. reply_len = p2p_ctrl_peer(wpa_s, buf + 9, reply,
  5688. reply_size);
  5689. } else if (os_strncmp(buf, "P2P_SET ", 8) == 0) {
  5690. if (p2p_ctrl_set(wpa_s, buf + 8) < 0)
  5691. reply_len = -1;
  5692. } else if (os_strcmp(buf, "P2P_FLUSH") == 0) {
  5693. p2p_ctrl_flush(wpa_s);
  5694. } else if (os_strncmp(buf, "P2P_UNAUTHORIZE ", 16) == 0) {
  5695. if (wpas_p2p_unauthorize(wpa_s, buf + 16) < 0)
  5696. reply_len = -1;
  5697. } else if (os_strcmp(buf, "P2P_CANCEL") == 0) {
  5698. if (wpas_p2p_cancel(wpa_s))
  5699. reply_len = -1;
  5700. } else if (os_strncmp(buf, "P2P_PRESENCE_REQ ", 17) == 0) {
  5701. if (p2p_ctrl_presence_req(wpa_s, buf + 17) < 0)
  5702. reply_len = -1;
  5703. } else if (os_strcmp(buf, "P2P_PRESENCE_REQ") == 0) {
  5704. if (p2p_ctrl_presence_req(wpa_s, "") < 0)
  5705. reply_len = -1;
  5706. } else if (os_strncmp(buf, "P2P_EXT_LISTEN ", 15) == 0) {
  5707. if (p2p_ctrl_ext_listen(wpa_s, buf + 15) < 0)
  5708. reply_len = -1;
  5709. } else if (os_strcmp(buf, "P2P_EXT_LISTEN") == 0) {
  5710. if (p2p_ctrl_ext_listen(wpa_s, "") < 0)
  5711. reply_len = -1;
  5712. } else if (os_strncmp(buf, "P2P_REMOVE_CLIENT ", 18) == 0) {
  5713. if (p2p_ctrl_remove_client(wpa_s, buf + 18) < 0)
  5714. reply_len = -1;
  5715. #endif /* CONFIG_P2P */
  5716. #ifdef CONFIG_WIFI_DISPLAY
  5717. } else if (os_strncmp(buf, "WFD_SUBELEM_SET ", 16) == 0) {
  5718. if (wifi_display_subelem_set(wpa_s->global, buf + 16) < 0)
  5719. reply_len = -1;
  5720. } else if (os_strncmp(buf, "WFD_SUBELEM_GET ", 16) == 0) {
  5721. reply_len = wifi_display_subelem_get(wpa_s->global, buf + 16,
  5722. reply, reply_size);
  5723. #endif /* CONFIG_WIFI_DISPLAY */
  5724. #ifdef CONFIG_INTERWORKING
  5725. } else if (os_strcmp(buf, "FETCH_ANQP") == 0) {
  5726. if (interworking_fetch_anqp(wpa_s) < 0)
  5727. reply_len = -1;
  5728. } else if (os_strcmp(buf, "STOP_FETCH_ANQP") == 0) {
  5729. interworking_stop_fetch_anqp(wpa_s);
  5730. } else if (os_strcmp(buf, "INTERWORKING_SELECT") == 0) {
  5731. if (ctrl_interworking_select(wpa_s, NULL) < 0)
  5732. reply_len = -1;
  5733. } else if (os_strncmp(buf, "INTERWORKING_SELECT ", 20) == 0) {
  5734. if (ctrl_interworking_select(wpa_s, buf + 20) < 0)
  5735. reply_len = -1;
  5736. } else if (os_strncmp(buf, "INTERWORKING_CONNECT ", 21) == 0) {
  5737. if (ctrl_interworking_connect(wpa_s, buf + 21) < 0)
  5738. reply_len = -1;
  5739. } else if (os_strncmp(buf, "ANQP_GET ", 9) == 0) {
  5740. if (get_anqp(wpa_s, buf + 9) < 0)
  5741. reply_len = -1;
  5742. } else if (os_strncmp(buf, "GAS_REQUEST ", 12) == 0) {
  5743. if (gas_request(wpa_s, buf + 12) < 0)
  5744. reply_len = -1;
  5745. } else if (os_strncmp(buf, "GAS_RESPONSE_GET ", 17) == 0) {
  5746. reply_len = gas_response_get(wpa_s, buf + 17, reply,
  5747. reply_size);
  5748. #endif /* CONFIG_INTERWORKING */
  5749. #ifdef CONFIG_HS20
  5750. } else if (os_strncmp(buf, "HS20_ANQP_GET ", 14) == 0) {
  5751. if (get_hs20_anqp(wpa_s, buf + 14) < 0)
  5752. reply_len = -1;
  5753. } else if (os_strncmp(buf, "HS20_GET_NAI_HOME_REALM_LIST ", 29) == 0) {
  5754. if (hs20_get_nai_home_realm_list(wpa_s, buf + 29) < 0)
  5755. reply_len = -1;
  5756. } else if (os_strncmp(buf, "HS20_ICON_REQUEST ", 18) == 0) {
  5757. if (hs20_icon_request(wpa_s, buf + 18) < 0)
  5758. reply_len = -1;
  5759. } else if (os_strcmp(buf, "FETCH_OSU") == 0) {
  5760. if (hs20_fetch_osu(wpa_s) < 0)
  5761. reply_len = -1;
  5762. } else if (os_strcmp(buf, "CANCEL_FETCH_OSU") == 0) {
  5763. hs20_cancel_fetch_osu(wpa_s);
  5764. #endif /* CONFIG_HS20 */
  5765. } else if (os_strncmp(buf, WPA_CTRL_RSP, os_strlen(WPA_CTRL_RSP)) == 0)
  5766. {
  5767. if (wpa_supplicant_ctrl_iface_ctrl_rsp(
  5768. wpa_s, buf + os_strlen(WPA_CTRL_RSP)))
  5769. reply_len = -1;
  5770. else {
  5771. /*
  5772. * Notify response from timeout to allow the control
  5773. * interface response to be sent first.
  5774. */
  5775. eloop_register_timeout(0, 0, wpas_ctrl_eapol_response,
  5776. wpa_s, NULL);
  5777. }
  5778. } else if (os_strcmp(buf, "RECONFIGURE") == 0) {
  5779. if (wpa_supplicant_reload_configuration(wpa_s))
  5780. reply_len = -1;
  5781. } else if (os_strcmp(buf, "TERMINATE") == 0) {
  5782. wpa_supplicant_terminate_proc(wpa_s->global);
  5783. } else if (os_strncmp(buf, "BSSID ", 6) == 0) {
  5784. if (wpa_supplicant_ctrl_iface_bssid(wpa_s, buf + 6))
  5785. reply_len = -1;
  5786. } else if (os_strncmp(buf, "BLACKLIST", 9) == 0) {
  5787. reply_len = wpa_supplicant_ctrl_iface_blacklist(
  5788. wpa_s, buf + 9, reply, reply_size);
  5789. } else if (os_strncmp(buf, "LOG_LEVEL", 9) == 0) {
  5790. reply_len = wpa_supplicant_ctrl_iface_log_level(
  5791. wpa_s, buf + 9, reply, reply_size);
  5792. } else if (os_strcmp(buf, "LIST_NETWORKS") == 0) {
  5793. reply_len = wpa_supplicant_ctrl_iface_list_networks(
  5794. wpa_s, reply, reply_size);
  5795. } else if (os_strcmp(buf, "DISCONNECT") == 0) {
  5796. #ifdef CONFIG_SME
  5797. wpa_s->sme.prev_bssid_set = 0;
  5798. #endif /* CONFIG_SME */
  5799. wpa_s->reassociate = 0;
  5800. wpa_s->disconnected = 1;
  5801. wpa_supplicant_cancel_sched_scan(wpa_s);
  5802. wpa_supplicant_cancel_scan(wpa_s);
  5803. wpa_supplicant_deauthenticate(wpa_s,
  5804. WLAN_REASON_DEAUTH_LEAVING);
  5805. } else if (os_strcmp(buf, "SCAN") == 0) {
  5806. wpas_ctrl_scan(wpa_s, NULL, reply, reply_size, &reply_len);
  5807. } else if (os_strncmp(buf, "SCAN ", 5) == 0) {
  5808. wpas_ctrl_scan(wpa_s, buf + 5, reply, reply_size, &reply_len);
  5809. } else if (os_strcmp(buf, "SCAN_RESULTS") == 0) {
  5810. reply_len = wpa_supplicant_ctrl_iface_scan_results(
  5811. wpa_s, reply, reply_size);
  5812. } else if (os_strncmp(buf, "SELECT_NETWORK ", 15) == 0) {
  5813. if (wpa_supplicant_ctrl_iface_select_network(wpa_s, buf + 15))
  5814. reply_len = -1;
  5815. } else if (os_strncmp(buf, "ENABLE_NETWORK ", 15) == 0) {
  5816. if (wpa_supplicant_ctrl_iface_enable_network(wpa_s, buf + 15))
  5817. reply_len = -1;
  5818. } else if (os_strncmp(buf, "DISABLE_NETWORK ", 16) == 0) {
  5819. if (wpa_supplicant_ctrl_iface_disable_network(wpa_s, buf + 16))
  5820. reply_len = -1;
  5821. } else if (os_strcmp(buf, "ADD_NETWORK") == 0) {
  5822. reply_len = wpa_supplicant_ctrl_iface_add_network(
  5823. wpa_s, reply, reply_size);
  5824. } else if (os_strncmp(buf, "REMOVE_NETWORK ", 15) == 0) {
  5825. if (wpa_supplicant_ctrl_iface_remove_network(wpa_s, buf + 15))
  5826. reply_len = -1;
  5827. } else if (os_strncmp(buf, "SET_NETWORK ", 12) == 0) {
  5828. if (wpa_supplicant_ctrl_iface_set_network(wpa_s, buf + 12))
  5829. reply_len = -1;
  5830. } else if (os_strncmp(buf, "GET_NETWORK ", 12) == 0) {
  5831. reply_len = wpa_supplicant_ctrl_iface_get_network(
  5832. wpa_s, buf + 12, reply, reply_size);
  5833. } else if (os_strncmp(buf, "DUP_NETWORK ", 12) == 0) {
  5834. if (wpa_supplicant_ctrl_iface_dup_network(wpa_s, buf + 12))
  5835. reply_len = -1;
  5836. } else if (os_strcmp(buf, "LIST_CREDS") == 0) {
  5837. reply_len = wpa_supplicant_ctrl_iface_list_creds(
  5838. wpa_s, reply, reply_size);
  5839. } else if (os_strcmp(buf, "ADD_CRED") == 0) {
  5840. reply_len = wpa_supplicant_ctrl_iface_add_cred(
  5841. wpa_s, reply, reply_size);
  5842. } else if (os_strncmp(buf, "REMOVE_CRED ", 12) == 0) {
  5843. if (wpa_supplicant_ctrl_iface_remove_cred(wpa_s, buf + 12))
  5844. reply_len = -1;
  5845. } else if (os_strncmp(buf, "SET_CRED ", 9) == 0) {
  5846. if (wpa_supplicant_ctrl_iface_set_cred(wpa_s, buf + 9))
  5847. reply_len = -1;
  5848. } else if (os_strncmp(buf, "GET_CRED ", 9) == 0) {
  5849. reply_len = wpa_supplicant_ctrl_iface_get_cred(wpa_s, buf + 9,
  5850. reply,
  5851. reply_size);
  5852. #ifndef CONFIG_NO_CONFIG_WRITE
  5853. } else if (os_strcmp(buf, "SAVE_CONFIG") == 0) {
  5854. if (wpa_supplicant_ctrl_iface_save_config(wpa_s))
  5855. reply_len = -1;
  5856. #endif /* CONFIG_NO_CONFIG_WRITE */
  5857. } else if (os_strncmp(buf, "GET_CAPABILITY ", 15) == 0) {
  5858. reply_len = wpa_supplicant_ctrl_iface_get_capability(
  5859. wpa_s, buf + 15, reply, reply_size);
  5860. } else if (os_strncmp(buf, "AP_SCAN ", 8) == 0) {
  5861. if (wpa_supplicant_ctrl_iface_ap_scan(wpa_s, buf + 8))
  5862. reply_len = -1;
  5863. } else if (os_strncmp(buf, "SCAN_INTERVAL ", 14) == 0) {
  5864. if (wpa_supplicant_ctrl_iface_scan_interval(wpa_s, buf + 14))
  5865. reply_len = -1;
  5866. } else if (os_strcmp(buf, "INTERFACE_LIST") == 0) {
  5867. reply_len = wpa_supplicant_global_iface_list(
  5868. wpa_s->global, reply, reply_size);
  5869. } else if (os_strcmp(buf, "INTERFACES") == 0) {
  5870. reply_len = wpa_supplicant_global_iface_interfaces(
  5871. wpa_s->global, reply, reply_size);
  5872. } else if (os_strncmp(buf, "BSS ", 4) == 0) {
  5873. reply_len = wpa_supplicant_ctrl_iface_bss(
  5874. wpa_s, buf + 4, reply, reply_size);
  5875. #ifdef CONFIG_AP
  5876. } else if (os_strcmp(buf, "STA-FIRST") == 0) {
  5877. reply_len = ap_ctrl_iface_sta_first(wpa_s, reply, reply_size);
  5878. } else if (os_strncmp(buf, "STA ", 4) == 0) {
  5879. reply_len = ap_ctrl_iface_sta(wpa_s, buf + 4, reply,
  5880. reply_size);
  5881. } else if (os_strncmp(buf, "STA-NEXT ", 9) == 0) {
  5882. reply_len = ap_ctrl_iface_sta_next(wpa_s, buf + 9, reply,
  5883. reply_size);
  5884. } else if (os_strncmp(buf, "DEAUTHENTICATE ", 15) == 0) {
  5885. if (ap_ctrl_iface_sta_deauthenticate(wpa_s, buf + 15))
  5886. reply_len = -1;
  5887. } else if (os_strncmp(buf, "DISASSOCIATE ", 13) == 0) {
  5888. if (ap_ctrl_iface_sta_disassociate(wpa_s, buf + 13))
  5889. reply_len = -1;
  5890. } else if (os_strncmp(buf, "CHAN_SWITCH ", 12) == 0) {
  5891. if (ap_ctrl_iface_chanswitch(wpa_s, buf + 12))
  5892. reply_len = -1;
  5893. #endif /* CONFIG_AP */
  5894. } else if (os_strcmp(buf, "SUSPEND") == 0) {
  5895. wpas_notify_suspend(wpa_s->global);
  5896. } else if (os_strcmp(buf, "RESUME") == 0) {
  5897. wpas_notify_resume(wpa_s->global);
  5898. #ifdef CONFIG_TESTING_OPTIONS
  5899. } else if (os_strcmp(buf, "DROP_SA") == 0) {
  5900. wpa_supplicant_ctrl_iface_drop_sa(wpa_s);
  5901. #endif /* CONFIG_TESTING_OPTIONS */
  5902. } else if (os_strncmp(buf, "ROAM ", 5) == 0) {
  5903. if (wpa_supplicant_ctrl_iface_roam(wpa_s, buf + 5))
  5904. reply_len = -1;
  5905. } else if (os_strncmp(buf, "STA_AUTOCONNECT ", 16) == 0) {
  5906. if (wpa_supplicant_ctrl_iface_sta_autoconnect(wpa_s, buf + 16))
  5907. reply_len = -1;
  5908. } else if (os_strncmp(buf, "BSS_EXPIRE_AGE ", 15) == 0) {
  5909. if (wpa_supplicant_ctrl_iface_bss_expire_age(wpa_s, buf + 15))
  5910. reply_len = -1;
  5911. } else if (os_strncmp(buf, "BSS_EXPIRE_COUNT ", 17) == 0) {
  5912. if (wpa_supplicant_ctrl_iface_bss_expire_count(wpa_s,
  5913. buf + 17))
  5914. reply_len = -1;
  5915. } else if (os_strncmp(buf, "BSS_FLUSH ", 10) == 0) {
  5916. if (wpa_supplicant_ctrl_iface_bss_flush(wpa_s, buf + 10))
  5917. reply_len = -1;
  5918. #ifdef CONFIG_TDLS
  5919. } else if (os_strncmp(buf, "TDLS_DISCOVER ", 14) == 0) {
  5920. if (wpa_supplicant_ctrl_iface_tdls_discover(wpa_s, buf + 14))
  5921. reply_len = -1;
  5922. } else if (os_strncmp(buf, "TDLS_SETUP ", 11) == 0) {
  5923. if (wpa_supplicant_ctrl_iface_tdls_setup(wpa_s, buf + 11))
  5924. reply_len = -1;
  5925. } else if (os_strncmp(buf, "TDLS_TEARDOWN ", 14) == 0) {
  5926. if (wpa_supplicant_ctrl_iface_tdls_teardown(wpa_s, buf + 14))
  5927. reply_len = -1;
  5928. #endif /* CONFIG_TDLS */
  5929. } else if (os_strncmp(buf, "SIGNAL_POLL", 11) == 0) {
  5930. reply_len = wpa_supplicant_signal_poll(wpa_s, reply,
  5931. reply_size);
  5932. } else if (os_strncmp(buf, "PKTCNT_POLL", 11) == 0) {
  5933. reply_len = wpa_supplicant_pktcnt_poll(wpa_s, reply,
  5934. reply_size);
  5935. #ifdef CONFIG_AUTOSCAN
  5936. } else if (os_strncmp(buf, "AUTOSCAN ", 9) == 0) {
  5937. if (wpa_supplicant_ctrl_iface_autoscan(wpa_s, buf + 9))
  5938. reply_len = -1;
  5939. #endif /* CONFIG_AUTOSCAN */
  5940. #ifdef ANDROID
  5941. } else if (os_strncmp(buf, "DRIVER ", 7) == 0) {
  5942. reply_len = wpa_supplicant_driver_cmd(wpa_s, buf + 7, reply,
  5943. reply_size);
  5944. #endif /* ANDROID */
  5945. } else if (os_strncmp(buf, "VENDOR ", 7) == 0) {
  5946. reply_len = wpa_supplicant_vendor_cmd(wpa_s, buf + 7, reply,
  5947. reply_size);
  5948. } else if (os_strcmp(buf, "REAUTHENTICATE") == 0) {
  5949. pmksa_cache_clear_current(wpa_s->wpa);
  5950. eapol_sm_request_reauth(wpa_s->eapol);
  5951. #ifdef CONFIG_WNM
  5952. } else if (os_strncmp(buf, "WNM_SLEEP ", 10) == 0) {
  5953. if (wpas_ctrl_iface_wnm_sleep(wpa_s, buf + 10))
  5954. reply_len = -1;
  5955. } else if (os_strncmp(buf, "WNM_BSS_QUERY ", 10) == 0) {
  5956. if (wpas_ctrl_iface_wnm_bss_query(wpa_s, buf + 10))
  5957. reply_len = -1;
  5958. #endif /* CONFIG_WNM */
  5959. } else if (os_strcmp(buf, "FLUSH") == 0) {
  5960. wpa_supplicant_ctrl_iface_flush(wpa_s);
  5961. } else if (os_strncmp(buf, "RADIO_WORK ", 11) == 0) {
  5962. reply_len = wpas_ctrl_radio_work(wpa_s, buf + 11, reply,
  5963. reply_size);
  5964. #ifdef CONFIG_TESTING_OPTIONS
  5965. } else if (os_strncmp(buf, "MGMT_TX ", 8) == 0) {
  5966. if (wpas_ctrl_iface_mgmt_tx(wpa_s, buf + 8) < 0)
  5967. reply_len = -1;
  5968. } else if (os_strcmp(buf, "MGMT_TX_DONE") == 0) {
  5969. wpas_ctrl_iface_mgmt_tx_done(wpa_s);
  5970. } else if (os_strncmp(buf, "DRIVER_EVENT ", 13) == 0) {
  5971. if (wpas_ctrl_iface_driver_event(wpa_s, buf + 13) < 0)
  5972. reply_len = -1;
  5973. #endif /* CONFIG_TESTING_OPTIONS */
  5974. } else if (os_strncmp(buf, "VENDOR_ELEM_ADD ", 16) == 0) {
  5975. if (wpas_ctrl_vendor_elem_add(wpa_s, buf + 16) < 0)
  5976. reply_len = -1;
  5977. } else if (os_strncmp(buf, "VENDOR_ELEM_GET ", 16) == 0) {
  5978. reply_len = wpas_ctrl_vendor_elem_get(wpa_s, buf + 16, reply,
  5979. reply_size);
  5980. } else if (os_strncmp(buf, "VENDOR_ELEM_REMOVE ", 19) == 0) {
  5981. if (wpas_ctrl_vendor_elem_remove(wpa_s, buf + 19) < 0)
  5982. reply_len = -1;
  5983. } else {
  5984. os_memcpy(reply, "UNKNOWN COMMAND\n", 16);
  5985. reply_len = 16;
  5986. }
  5987. if (reply_len < 0) {
  5988. os_memcpy(reply, "FAIL\n", 5);
  5989. reply_len = 5;
  5990. }
  5991. *resp_len = reply_len;
  5992. return reply;
  5993. }
  5994. static int wpa_supplicant_global_iface_add(struct wpa_global *global,
  5995. char *cmd)
  5996. {
  5997. struct wpa_interface iface;
  5998. char *pos;
  5999. /*
  6000. * <ifname>TAB<confname>TAB<driver>TAB<ctrl_interface>TAB<driver_param>
  6001. * TAB<bridge_ifname>
  6002. */
  6003. wpa_printf(MSG_DEBUG, "CTRL_IFACE GLOBAL INTERFACE_ADD '%s'", cmd);
  6004. os_memset(&iface, 0, sizeof(iface));
  6005. do {
  6006. iface.ifname = pos = cmd;
  6007. pos = os_strchr(pos, '\t');
  6008. if (pos)
  6009. *pos++ = '\0';
  6010. if (iface.ifname[0] == '\0')
  6011. return -1;
  6012. if (pos == NULL)
  6013. break;
  6014. iface.confname = pos;
  6015. pos = os_strchr(pos, '\t');
  6016. if (pos)
  6017. *pos++ = '\0';
  6018. if (iface.confname[0] == '\0')
  6019. iface.confname = NULL;
  6020. if (pos == NULL)
  6021. break;
  6022. iface.driver = pos;
  6023. pos = os_strchr(pos, '\t');
  6024. if (pos)
  6025. *pos++ = '\0';
  6026. if (iface.driver[0] == '\0')
  6027. iface.driver = NULL;
  6028. if (pos == NULL)
  6029. break;
  6030. iface.ctrl_interface = pos;
  6031. pos = os_strchr(pos, '\t');
  6032. if (pos)
  6033. *pos++ = '\0';
  6034. if (iface.ctrl_interface[0] == '\0')
  6035. iface.ctrl_interface = NULL;
  6036. if (pos == NULL)
  6037. break;
  6038. iface.driver_param = pos;
  6039. pos = os_strchr(pos, '\t');
  6040. if (pos)
  6041. *pos++ = '\0';
  6042. if (iface.driver_param[0] == '\0')
  6043. iface.driver_param = NULL;
  6044. if (pos == NULL)
  6045. break;
  6046. iface.bridge_ifname = pos;
  6047. pos = os_strchr(pos, '\t');
  6048. if (pos)
  6049. *pos++ = '\0';
  6050. if (iface.bridge_ifname[0] == '\0')
  6051. iface.bridge_ifname = NULL;
  6052. if (pos == NULL)
  6053. break;
  6054. } while (0);
  6055. if (wpa_supplicant_get_iface(global, iface.ifname))
  6056. return -1;
  6057. return wpa_supplicant_add_iface(global, &iface) ? 0 : -1;
  6058. }
  6059. static int wpa_supplicant_global_iface_remove(struct wpa_global *global,
  6060. char *cmd)
  6061. {
  6062. struct wpa_supplicant *wpa_s;
  6063. wpa_printf(MSG_DEBUG, "CTRL_IFACE GLOBAL INTERFACE_REMOVE '%s'", cmd);
  6064. wpa_s = wpa_supplicant_get_iface(global, cmd);
  6065. if (wpa_s == NULL)
  6066. return -1;
  6067. return wpa_supplicant_remove_iface(global, wpa_s, 0);
  6068. }
  6069. static void wpa_free_iface_info(struct wpa_interface_info *iface)
  6070. {
  6071. struct wpa_interface_info *prev;
  6072. while (iface) {
  6073. prev = iface;
  6074. iface = iface->next;
  6075. os_free(prev->ifname);
  6076. os_free(prev->desc);
  6077. os_free(prev);
  6078. }
  6079. }
  6080. static int wpa_supplicant_global_iface_list(struct wpa_global *global,
  6081. char *buf, int len)
  6082. {
  6083. int i, res;
  6084. struct wpa_interface_info *iface = NULL, *last = NULL, *tmp;
  6085. char *pos, *end;
  6086. for (i = 0; wpa_drivers[i]; i++) {
  6087. struct wpa_driver_ops *drv = wpa_drivers[i];
  6088. if (drv->get_interfaces == NULL)
  6089. continue;
  6090. tmp = drv->get_interfaces(global->drv_priv[i]);
  6091. if (tmp == NULL)
  6092. continue;
  6093. if (last == NULL)
  6094. iface = last = tmp;
  6095. else
  6096. last->next = tmp;
  6097. while (last->next)
  6098. last = last->next;
  6099. }
  6100. pos = buf;
  6101. end = buf + len;
  6102. for (tmp = iface; tmp; tmp = tmp->next) {
  6103. res = os_snprintf(pos, end - pos, "%s\t%s\t%s\n",
  6104. tmp->drv_name, tmp->ifname,
  6105. tmp->desc ? tmp->desc : "");
  6106. if (res < 0 || res >= end - pos) {
  6107. *pos = '\0';
  6108. break;
  6109. }
  6110. pos += res;
  6111. }
  6112. wpa_free_iface_info(iface);
  6113. return pos - buf;
  6114. }
  6115. static int wpa_supplicant_global_iface_interfaces(struct wpa_global *global,
  6116. char *buf, int len)
  6117. {
  6118. int res;
  6119. char *pos, *end;
  6120. struct wpa_supplicant *wpa_s;
  6121. wpa_s = global->ifaces;
  6122. pos = buf;
  6123. end = buf + len;
  6124. while (wpa_s) {
  6125. res = os_snprintf(pos, end - pos, "%s\n", wpa_s->ifname);
  6126. if (res < 0 || res >= end - pos) {
  6127. *pos = '\0';
  6128. break;
  6129. }
  6130. pos += res;
  6131. wpa_s = wpa_s->next;
  6132. }
  6133. return pos - buf;
  6134. }
  6135. static char * wpas_global_ctrl_iface_ifname(struct wpa_global *global,
  6136. const char *ifname,
  6137. char *cmd, size_t *resp_len)
  6138. {
  6139. struct wpa_supplicant *wpa_s;
  6140. for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
  6141. if (os_strcmp(ifname, wpa_s->ifname) == 0)
  6142. break;
  6143. }
  6144. if (wpa_s == NULL) {
  6145. char *resp = os_strdup("FAIL-NO-IFNAME-MATCH\n");
  6146. if (resp)
  6147. *resp_len = os_strlen(resp);
  6148. else
  6149. *resp_len = 1;
  6150. return resp;
  6151. }
  6152. return wpa_supplicant_ctrl_iface_process(wpa_s, cmd, resp_len);
  6153. }
  6154. static char * wpas_global_ctrl_iface_redir_p2p(struct wpa_global *global,
  6155. char *buf, size_t *resp_len)
  6156. {
  6157. #ifdef CONFIG_P2P
  6158. static const char * cmd[] = {
  6159. "LIST_NETWORKS",
  6160. "P2P_FIND",
  6161. "P2P_STOP_FIND",
  6162. "P2P_LISTEN",
  6163. "P2P_GROUP_ADD",
  6164. "P2P_GET_PASSPHRASE",
  6165. "P2P_SERVICE_UPDATE",
  6166. "P2P_SERVICE_FLUSH",
  6167. "P2P_FLUSH",
  6168. "P2P_CANCEL",
  6169. "P2P_PRESENCE_REQ",
  6170. "P2P_EXT_LISTEN",
  6171. NULL
  6172. };
  6173. static const char * prefix[] = {
  6174. #ifdef ANDROID
  6175. "DRIVER ",
  6176. #endif /* ANDROID */
  6177. "GET_NETWORK ",
  6178. "REMOVE_NETWORK ",
  6179. "P2P_FIND ",
  6180. "P2P_CONNECT ",
  6181. "P2P_LISTEN ",
  6182. "P2P_GROUP_REMOVE ",
  6183. "P2P_GROUP_ADD ",
  6184. "P2P_PROV_DISC ",
  6185. "P2P_SERV_DISC_REQ ",
  6186. "P2P_SERV_DISC_CANCEL_REQ ",
  6187. "P2P_SERV_DISC_RESP ",
  6188. "P2P_SERV_DISC_EXTERNAL ",
  6189. "P2P_SERVICE_ADD ",
  6190. "P2P_SERVICE_DEL ",
  6191. "P2P_REJECT ",
  6192. "P2P_INVITE ",
  6193. "P2P_PEER ",
  6194. "P2P_SET ",
  6195. "P2P_UNAUTHORIZE ",
  6196. "P2P_PRESENCE_REQ ",
  6197. "P2P_EXT_LISTEN ",
  6198. "P2P_REMOVE_CLIENT ",
  6199. "NFC_GET_HANDOVER_SEL ",
  6200. "NFC_GET_HANDOVER_REQ ",
  6201. "NFC_REPORT_HANDOVER ",
  6202. NULL
  6203. };
  6204. int found = 0;
  6205. int i;
  6206. if (global->p2p_init_wpa_s == NULL)
  6207. return NULL;
  6208. for (i = 0; !found && cmd[i]; i++) {
  6209. if (os_strcmp(buf, cmd[i]) == 0)
  6210. found = 1;
  6211. }
  6212. for (i = 0; !found && prefix[i]; i++) {
  6213. if (os_strncmp(buf, prefix[i], os_strlen(prefix[i])) == 0)
  6214. found = 1;
  6215. }
  6216. if (found)
  6217. return wpa_supplicant_ctrl_iface_process(global->p2p_init_wpa_s,
  6218. buf, resp_len);
  6219. #endif /* CONFIG_P2P */
  6220. return NULL;
  6221. }
  6222. static char * wpas_global_ctrl_iface_redir_wfd(struct wpa_global *global,
  6223. char *buf, size_t *resp_len)
  6224. {
  6225. #ifdef CONFIG_WIFI_DISPLAY
  6226. if (global->p2p_init_wpa_s == NULL)
  6227. return NULL;
  6228. if (os_strncmp(buf, "WFD_SUBELEM_SET ", 16) == 0 ||
  6229. os_strncmp(buf, "WFD_SUBELEM_GET ", 16) == 0)
  6230. return wpa_supplicant_ctrl_iface_process(global->p2p_init_wpa_s,
  6231. buf, resp_len);
  6232. #endif /* CONFIG_WIFI_DISPLAY */
  6233. return NULL;
  6234. }
  6235. static char * wpas_global_ctrl_iface_redir(struct wpa_global *global,
  6236. char *buf, size_t *resp_len)
  6237. {
  6238. char *ret;
  6239. ret = wpas_global_ctrl_iface_redir_p2p(global, buf, resp_len);
  6240. if (ret)
  6241. return ret;
  6242. ret = wpas_global_ctrl_iface_redir_wfd(global, buf, resp_len);
  6243. if (ret)
  6244. return ret;
  6245. return NULL;
  6246. }
  6247. static int wpas_global_ctrl_iface_set(struct wpa_global *global, char *cmd)
  6248. {
  6249. char *value;
  6250. value = os_strchr(cmd, ' ');
  6251. if (value == NULL)
  6252. return -1;
  6253. *value++ = '\0';
  6254. wpa_printf(MSG_DEBUG, "GLOBAL_CTRL_IFACE SET '%s'='%s'", cmd, value);
  6255. #ifdef CONFIG_WIFI_DISPLAY
  6256. if (os_strcasecmp(cmd, "wifi_display") == 0) {
  6257. wifi_display_enable(global, !!atoi(value));
  6258. return 0;
  6259. }
  6260. #endif /* CONFIG_WIFI_DISPLAY */
  6261. /* Restore cmd to its original value to allow redirection */
  6262. value[-1] = ' ';
  6263. return -1;
  6264. }
  6265. #ifndef CONFIG_NO_CONFIG_WRITE
  6266. static int wpas_global_ctrl_iface_save_config(struct wpa_global *global)
  6267. {
  6268. int ret = 0, saved = 0;
  6269. struct wpa_supplicant *wpa_s;
  6270. for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
  6271. if (!wpa_s->conf->update_config) {
  6272. wpa_dbg(wpa_s, MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Not allowed to update configuration (update_config=0)");
  6273. continue;
  6274. }
  6275. if (wpa_config_write(wpa_s->confname, wpa_s->conf)) {
  6276. wpa_dbg(wpa_s, MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Failed to update configuration");
  6277. ret = 1;
  6278. } else {
  6279. wpa_dbg(wpa_s, MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Configuration updated");
  6280. saved++;
  6281. }
  6282. }
  6283. if (!saved && !ret) {
  6284. wpa_dbg(wpa_s, MSG_DEBUG,
  6285. "CTRL_IFACE: SAVE_CONFIG - No configuration files could be updated");
  6286. ret = 1;
  6287. }
  6288. return ret;
  6289. }
  6290. #endif /* CONFIG_NO_CONFIG_WRITE */
  6291. static int wpas_global_ctrl_iface_status(struct wpa_global *global,
  6292. char *buf, size_t buflen)
  6293. {
  6294. char *pos, *end;
  6295. int ret;
  6296. struct wpa_supplicant *wpa_s;
  6297. pos = buf;
  6298. end = buf + buflen;
  6299. #ifdef CONFIG_P2P
  6300. if (global->p2p && !global->p2p_disabled) {
  6301. ret = os_snprintf(pos, end - pos, "p2p_device_address=" MACSTR
  6302. "\n"
  6303. "p2p_state=%s\n",
  6304. MAC2STR(global->p2p_dev_addr),
  6305. p2p_get_state_txt(global->p2p));
  6306. if (ret < 0 || ret >= end - pos)
  6307. return pos - buf;
  6308. pos += ret;
  6309. } else if (global->p2p) {
  6310. ret = os_snprintf(pos, end - pos, "p2p_state=DISABLED\n");
  6311. if (ret < 0 || ret >= end - pos)
  6312. return pos - buf;
  6313. pos += ret;
  6314. }
  6315. #endif /* CONFIG_P2P */
  6316. #ifdef CONFIG_WIFI_DISPLAY
  6317. ret = os_snprintf(pos, end - pos, "wifi_display=%d\n",
  6318. !!global->wifi_display);
  6319. if (ret < 0 || ret >= end - pos)
  6320. return pos - buf;
  6321. pos += ret;
  6322. #endif /* CONFIG_WIFI_DISPLAY */
  6323. for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
  6324. ret = os_snprintf(pos, end - pos, "ifname=%s\n"
  6325. "address=" MACSTR "\n",
  6326. wpa_s->ifname, MAC2STR(wpa_s->own_addr));
  6327. if (ret < 0 || ret >= end - pos)
  6328. return pos - buf;
  6329. pos += ret;
  6330. }
  6331. return pos - buf;
  6332. }
  6333. char * wpa_supplicant_global_ctrl_iface_process(struct wpa_global *global,
  6334. char *buf, size_t *resp_len)
  6335. {
  6336. char *reply;
  6337. const int reply_size = 2048;
  6338. int reply_len;
  6339. int level = MSG_DEBUG;
  6340. if (os_strncmp(buf, "IFNAME=", 7) == 0) {
  6341. char *pos = os_strchr(buf + 7, ' ');
  6342. if (pos) {
  6343. *pos++ = '\0';
  6344. return wpas_global_ctrl_iface_ifname(global,
  6345. buf + 7, pos,
  6346. resp_len);
  6347. }
  6348. }
  6349. reply = wpas_global_ctrl_iface_redir(global, buf, resp_len);
  6350. if (reply)
  6351. return reply;
  6352. if (os_strcmp(buf, "PING") == 0)
  6353. level = MSG_EXCESSIVE;
  6354. wpa_hexdump_ascii(level, "RX global ctrl_iface",
  6355. (const u8 *) buf, os_strlen(buf));
  6356. reply = os_malloc(reply_size);
  6357. if (reply == NULL) {
  6358. *resp_len = 1;
  6359. return NULL;
  6360. }
  6361. os_memcpy(reply, "OK\n", 3);
  6362. reply_len = 3;
  6363. if (os_strcmp(buf, "PING") == 0) {
  6364. os_memcpy(reply, "PONG\n", 5);
  6365. reply_len = 5;
  6366. } else if (os_strncmp(buf, "INTERFACE_ADD ", 14) == 0) {
  6367. if (wpa_supplicant_global_iface_add(global, buf + 14))
  6368. reply_len = -1;
  6369. } else if (os_strncmp(buf, "INTERFACE_REMOVE ", 17) == 0) {
  6370. if (wpa_supplicant_global_iface_remove(global, buf + 17))
  6371. reply_len = -1;
  6372. } else if (os_strcmp(buf, "INTERFACE_LIST") == 0) {
  6373. reply_len = wpa_supplicant_global_iface_list(
  6374. global, reply, reply_size);
  6375. } else if (os_strcmp(buf, "INTERFACES") == 0) {
  6376. reply_len = wpa_supplicant_global_iface_interfaces(
  6377. global, reply, reply_size);
  6378. } else if (os_strcmp(buf, "TERMINATE") == 0) {
  6379. wpa_supplicant_terminate_proc(global);
  6380. } else if (os_strcmp(buf, "SUSPEND") == 0) {
  6381. wpas_notify_suspend(global);
  6382. } else if (os_strcmp(buf, "RESUME") == 0) {
  6383. wpas_notify_resume(global);
  6384. } else if (os_strncmp(buf, "SET ", 4) == 0) {
  6385. if (wpas_global_ctrl_iface_set(global, buf + 4)) {
  6386. #ifdef CONFIG_P2P
  6387. if (global->p2p_init_wpa_s) {
  6388. os_free(reply);
  6389. /* Check if P2P redirection would work for this
  6390. * command. */
  6391. return wpa_supplicant_ctrl_iface_process(
  6392. global->p2p_init_wpa_s,
  6393. buf, resp_len);
  6394. }
  6395. #endif /* CONFIG_P2P */
  6396. reply_len = -1;
  6397. }
  6398. #ifndef CONFIG_NO_CONFIG_WRITE
  6399. } else if (os_strcmp(buf, "SAVE_CONFIG") == 0) {
  6400. if (wpas_global_ctrl_iface_save_config(global))
  6401. reply_len = -1;
  6402. #endif /* CONFIG_NO_CONFIG_WRITE */
  6403. } else if (os_strcmp(buf, "STATUS") == 0) {
  6404. reply_len = wpas_global_ctrl_iface_status(global, reply,
  6405. reply_size);
  6406. #ifdef CONFIG_MODULE_TESTS
  6407. } else if (os_strcmp(buf, "MODULE_TESTS") == 0) {
  6408. int wpas_module_tests(void);
  6409. if (wpas_module_tests() < 0)
  6410. reply_len = -1;
  6411. #endif /* CONFIG_MODULE_TESTS */
  6412. } else {
  6413. os_memcpy(reply, "UNKNOWN COMMAND\n", 16);
  6414. reply_len = 16;
  6415. }
  6416. if (reply_len < 0) {
  6417. os_memcpy(reply, "FAIL\n", 5);
  6418. reply_len = 5;
  6419. }
  6420. *resp_len = reply_len;
  6421. return reply;
  6422. }