ctrl_iface.c 160 KB

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