ctrl_iface.c 166 KB

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