ctrl_iface.c 167 KB

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