ctrl_iface.c 182 KB

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