ctrl_iface.c 161 KB

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