ctrl_iface.c 156 KB

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