ctrl_iface.c 191 KB

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