ctrl_iface.c 154 KB

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