ctrl_iface.c 157 KB

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