ctrl_iface.c 190 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063
  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. wpa_printf(MSG_DEBUG, "Remove network id %d since it "
  2536. "used the removed credential", ssid->id);
  2537. os_snprintf(str, sizeof(str), "%d", ssid->id);
  2538. ssid = ssid->next;
  2539. wpa_supplicant_ctrl_iface_remove_network(wpa_s, str);
  2540. } else
  2541. ssid = ssid->next;
  2542. }
  2543. return 0;
  2544. }
  2545. static int wpa_supplicant_ctrl_iface_remove_cred(struct wpa_supplicant *wpa_s,
  2546. char *cmd)
  2547. {
  2548. int id;
  2549. struct wpa_cred *cred, *prev;
  2550. /* cmd: "<cred id>", "all", "sp_fqdn=<FQDN>", or
  2551. * "provisioning_sp=<FQDN> */
  2552. if (os_strcmp(cmd, "all") == 0) {
  2553. wpa_printf(MSG_DEBUG, "CTRL_IFACE: REMOVE_CRED all");
  2554. cred = wpa_s->conf->cred;
  2555. while (cred) {
  2556. prev = cred;
  2557. cred = cred->next;
  2558. wpas_ctrl_remove_cred(wpa_s, prev);
  2559. }
  2560. return 0;
  2561. }
  2562. if (os_strncmp(cmd, "sp_fqdn=", 8) == 0) {
  2563. wpa_printf(MSG_DEBUG, "CTRL_IFACE: REMOVE_CRED SP FQDN '%s'",
  2564. cmd + 8);
  2565. cred = wpa_s->conf->cred;
  2566. while (cred) {
  2567. prev = cred;
  2568. cred = cred->next;
  2569. if (prev->domain) {
  2570. size_t i;
  2571. for (i = 0; i < prev->num_domain; i++) {
  2572. if (os_strcmp(prev->domain[i], cmd + 8)
  2573. != 0)
  2574. continue;
  2575. wpas_ctrl_remove_cred(wpa_s, prev);
  2576. break;
  2577. }
  2578. }
  2579. }
  2580. return 0;
  2581. }
  2582. if (os_strncmp(cmd, "provisioning_sp=", 16) == 0) {
  2583. wpa_printf(MSG_DEBUG, "CTRL_IFACE: REMOVE_CRED provisioning SP FQDN '%s'",
  2584. cmd + 16);
  2585. cred = wpa_s->conf->cred;
  2586. while (cred) {
  2587. prev = cred;
  2588. cred = cred->next;
  2589. if (prev->provisioning_sp &&
  2590. os_strcmp(prev->provisioning_sp, cmd + 16) == 0)
  2591. wpas_ctrl_remove_cred(wpa_s, prev);
  2592. }
  2593. return 0;
  2594. }
  2595. id = atoi(cmd);
  2596. wpa_printf(MSG_DEBUG, "CTRL_IFACE: REMOVE_CRED id=%d", id);
  2597. cred = wpa_config_get_cred(wpa_s->conf, id);
  2598. return wpas_ctrl_remove_cred(wpa_s, cred);
  2599. }
  2600. static int wpa_supplicant_ctrl_iface_set_cred(struct wpa_supplicant *wpa_s,
  2601. char *cmd)
  2602. {
  2603. int id;
  2604. struct wpa_cred *cred;
  2605. char *name, *value;
  2606. /* cmd: "<cred id> <variable name> <value>" */
  2607. name = os_strchr(cmd, ' ');
  2608. if (name == NULL)
  2609. return -1;
  2610. *name++ = '\0';
  2611. value = os_strchr(name, ' ');
  2612. if (value == NULL)
  2613. return -1;
  2614. *value++ = '\0';
  2615. id = atoi(cmd);
  2616. wpa_printf(MSG_DEBUG, "CTRL_IFACE: SET_CRED id=%d name='%s'",
  2617. id, name);
  2618. wpa_hexdump_ascii_key(MSG_DEBUG, "CTRL_IFACE: value",
  2619. (u8 *) value, os_strlen(value));
  2620. cred = wpa_config_get_cred(wpa_s->conf, id);
  2621. if (cred == NULL) {
  2622. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find cred id=%d",
  2623. id);
  2624. return -1;
  2625. }
  2626. if (wpa_config_set_cred(cred, name, value, 0) < 0) {
  2627. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Failed to set cred "
  2628. "variable '%s'", name);
  2629. return -1;
  2630. }
  2631. wpa_msg(wpa_s, MSG_INFO, CRED_MODIFIED "%d %s", cred->id, name);
  2632. return 0;
  2633. }
  2634. static int wpa_supplicant_ctrl_iface_get_cred(struct wpa_supplicant *wpa_s,
  2635. char *cmd, char *buf,
  2636. size_t buflen)
  2637. {
  2638. int id;
  2639. size_t res;
  2640. struct wpa_cred *cred;
  2641. char *name, *value;
  2642. /* cmd: "<cred id> <variable name>" */
  2643. name = os_strchr(cmd, ' ');
  2644. if (name == NULL)
  2645. return -1;
  2646. *name++ = '\0';
  2647. id = atoi(cmd);
  2648. wpa_printf(MSG_DEBUG, "CTRL_IFACE: GET_CRED id=%d name='%s'",
  2649. id, name);
  2650. cred = wpa_config_get_cred(wpa_s->conf, id);
  2651. if (cred == NULL) {
  2652. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find cred id=%d",
  2653. id);
  2654. return -1;
  2655. }
  2656. value = wpa_config_get_cred_no_key(cred, name);
  2657. if (value == NULL) {
  2658. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Failed to get cred variable '%s'",
  2659. name);
  2660. return -1;
  2661. }
  2662. res = os_strlcpy(buf, value, buflen);
  2663. if (res >= buflen) {
  2664. os_free(value);
  2665. return -1;
  2666. }
  2667. os_free(value);
  2668. return res;
  2669. }
  2670. #ifndef CONFIG_NO_CONFIG_WRITE
  2671. static int wpa_supplicant_ctrl_iface_save_config(struct wpa_supplicant *wpa_s)
  2672. {
  2673. int ret;
  2674. if (!wpa_s->conf->update_config) {
  2675. wpa_printf(MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Not allowed "
  2676. "to update configuration (update_config=0)");
  2677. return -1;
  2678. }
  2679. ret = wpa_config_write(wpa_s->confname, wpa_s->conf);
  2680. if (ret) {
  2681. wpa_printf(MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Failed to "
  2682. "update configuration");
  2683. } else {
  2684. wpa_printf(MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Configuration"
  2685. " updated");
  2686. }
  2687. return ret;
  2688. }
  2689. #endif /* CONFIG_NO_CONFIG_WRITE */
  2690. struct cipher_info {
  2691. unsigned int capa;
  2692. const char *name;
  2693. int group_only;
  2694. };
  2695. static const struct cipher_info ciphers[] = {
  2696. { WPA_DRIVER_CAPA_ENC_CCMP_256, "CCMP-256", 0 },
  2697. { WPA_DRIVER_CAPA_ENC_GCMP_256, "GCMP-256", 0 },
  2698. { WPA_DRIVER_CAPA_ENC_CCMP, "CCMP", 0 },
  2699. { WPA_DRIVER_CAPA_ENC_GCMP, "GCMP", 0 },
  2700. { WPA_DRIVER_CAPA_ENC_TKIP, "TKIP", 0 },
  2701. { WPA_DRIVER_CAPA_KEY_MGMT_WPA_NONE, "NONE", 0 },
  2702. { WPA_DRIVER_CAPA_ENC_WEP104, "WEP104", 1 },
  2703. { WPA_DRIVER_CAPA_ENC_WEP40, "WEP40", 1 }
  2704. };
  2705. static int ctrl_iface_get_capability_pairwise(int res, char *strict,
  2706. struct wpa_driver_capa *capa,
  2707. char *buf, size_t buflen)
  2708. {
  2709. int ret;
  2710. char *pos, *end;
  2711. size_t len;
  2712. unsigned int i;
  2713. pos = buf;
  2714. end = pos + buflen;
  2715. if (res < 0) {
  2716. if (strict)
  2717. return 0;
  2718. len = os_strlcpy(buf, "CCMP TKIP NONE", buflen);
  2719. if (len >= buflen)
  2720. return -1;
  2721. return len;
  2722. }
  2723. for (i = 0; i < ARRAY_SIZE(ciphers); i++) {
  2724. if (!ciphers[i].group_only && capa->enc & ciphers[i].capa) {
  2725. ret = os_snprintf(pos, end - pos, "%s%s",
  2726. pos == buf ? "" : " ",
  2727. ciphers[i].name);
  2728. if (os_snprintf_error(end - pos, ret))
  2729. return pos - buf;
  2730. pos += ret;
  2731. }
  2732. }
  2733. return pos - buf;
  2734. }
  2735. static int ctrl_iface_get_capability_group(int res, char *strict,
  2736. struct wpa_driver_capa *capa,
  2737. char *buf, size_t buflen)
  2738. {
  2739. int ret;
  2740. char *pos, *end;
  2741. size_t len;
  2742. unsigned int i;
  2743. pos = buf;
  2744. end = pos + buflen;
  2745. if (res < 0) {
  2746. if (strict)
  2747. return 0;
  2748. len = os_strlcpy(buf, "CCMP TKIP WEP104 WEP40", buflen);
  2749. if (len >= buflen)
  2750. return -1;
  2751. return len;
  2752. }
  2753. for (i = 0; i < ARRAY_SIZE(ciphers); i++) {
  2754. if (capa->enc & ciphers[i].capa) {
  2755. ret = os_snprintf(pos, end - pos, "%s%s",
  2756. pos == buf ? "" : " ",
  2757. ciphers[i].name);
  2758. if (os_snprintf_error(end - pos, ret))
  2759. return pos - buf;
  2760. pos += ret;
  2761. }
  2762. }
  2763. return pos - buf;
  2764. }
  2765. static int ctrl_iface_get_capability_key_mgmt(int res, char *strict,
  2766. struct wpa_driver_capa *capa,
  2767. char *buf, size_t buflen)
  2768. {
  2769. int ret;
  2770. char *pos, *end;
  2771. size_t len;
  2772. pos = buf;
  2773. end = pos + buflen;
  2774. if (res < 0) {
  2775. if (strict)
  2776. return 0;
  2777. len = os_strlcpy(buf, "WPA-PSK WPA-EAP IEEE8021X WPA-NONE "
  2778. "NONE", buflen);
  2779. if (len >= buflen)
  2780. return -1;
  2781. return len;
  2782. }
  2783. ret = os_snprintf(pos, end - pos, "NONE IEEE8021X");
  2784. if (os_snprintf_error(end - pos, ret))
  2785. return pos - buf;
  2786. pos += ret;
  2787. if (capa->key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA |
  2788. WPA_DRIVER_CAPA_KEY_MGMT_WPA2)) {
  2789. ret = os_snprintf(pos, end - pos, " WPA-EAP");
  2790. if (os_snprintf_error(end - pos, ret))
  2791. return pos - buf;
  2792. pos += ret;
  2793. }
  2794. if (capa->key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA_PSK |
  2795. WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK)) {
  2796. ret = os_snprintf(pos, end - pos, " WPA-PSK");
  2797. if (os_snprintf_error(end - pos, ret))
  2798. return pos - buf;
  2799. pos += ret;
  2800. }
  2801. if (capa->key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_WPA_NONE) {
  2802. ret = os_snprintf(pos, end - pos, " WPA-NONE");
  2803. if (os_snprintf_error(end - pos, ret))
  2804. return pos - buf;
  2805. pos += ret;
  2806. }
  2807. return pos - buf;
  2808. }
  2809. static int ctrl_iface_get_capability_proto(int res, char *strict,
  2810. struct wpa_driver_capa *capa,
  2811. char *buf, size_t buflen)
  2812. {
  2813. int ret;
  2814. char *pos, *end;
  2815. size_t len;
  2816. pos = buf;
  2817. end = pos + buflen;
  2818. if (res < 0) {
  2819. if (strict)
  2820. return 0;
  2821. len = os_strlcpy(buf, "RSN WPA", buflen);
  2822. if (len >= buflen)
  2823. return -1;
  2824. return len;
  2825. }
  2826. if (capa->key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA2 |
  2827. WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK)) {
  2828. ret = os_snprintf(pos, end - pos, "%sRSN",
  2829. pos == buf ? "" : " ");
  2830. if (os_snprintf_error(end - pos, ret))
  2831. return pos - buf;
  2832. pos += ret;
  2833. }
  2834. if (capa->key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA |
  2835. WPA_DRIVER_CAPA_KEY_MGMT_WPA_PSK)) {
  2836. ret = os_snprintf(pos, end - pos, "%sWPA",
  2837. pos == buf ? "" : " ");
  2838. if (os_snprintf_error(end - pos, ret))
  2839. return pos - buf;
  2840. pos += ret;
  2841. }
  2842. return pos - buf;
  2843. }
  2844. static int ctrl_iface_get_capability_auth_alg(int res, char *strict,
  2845. struct wpa_driver_capa *capa,
  2846. char *buf, size_t buflen)
  2847. {
  2848. int ret;
  2849. char *pos, *end;
  2850. size_t len;
  2851. pos = buf;
  2852. end = pos + buflen;
  2853. if (res < 0) {
  2854. if (strict)
  2855. return 0;
  2856. len = os_strlcpy(buf, "OPEN SHARED LEAP", buflen);
  2857. if (len >= buflen)
  2858. return -1;
  2859. return len;
  2860. }
  2861. if (capa->auth & (WPA_DRIVER_AUTH_OPEN)) {
  2862. ret = os_snprintf(pos, end - pos, "%sOPEN",
  2863. pos == buf ? "" : " ");
  2864. if (os_snprintf_error(end - pos, ret))
  2865. return pos - buf;
  2866. pos += ret;
  2867. }
  2868. if (capa->auth & (WPA_DRIVER_AUTH_SHARED)) {
  2869. ret = os_snprintf(pos, end - pos, "%sSHARED",
  2870. pos == buf ? "" : " ");
  2871. if (os_snprintf_error(end - pos, ret))
  2872. return pos - buf;
  2873. pos += ret;
  2874. }
  2875. if (capa->auth & (WPA_DRIVER_AUTH_LEAP)) {
  2876. ret = os_snprintf(pos, end - pos, "%sLEAP",
  2877. pos == buf ? "" : " ");
  2878. if (os_snprintf_error(end - pos, ret))
  2879. return pos - buf;
  2880. pos += ret;
  2881. }
  2882. return pos - buf;
  2883. }
  2884. static int ctrl_iface_get_capability_modes(int res, char *strict,
  2885. struct wpa_driver_capa *capa,
  2886. char *buf, size_t buflen)
  2887. {
  2888. int ret;
  2889. char *pos, *end;
  2890. size_t len;
  2891. pos = buf;
  2892. end = pos + buflen;
  2893. if (res < 0) {
  2894. if (strict)
  2895. return 0;
  2896. len = os_strlcpy(buf, "IBSS AP", buflen);
  2897. if (len >= buflen)
  2898. return -1;
  2899. return len;
  2900. }
  2901. if (capa->flags & WPA_DRIVER_FLAGS_IBSS) {
  2902. ret = os_snprintf(pos, end - pos, "%sIBSS",
  2903. pos == buf ? "" : " ");
  2904. if (os_snprintf_error(end - pos, ret))
  2905. return pos - buf;
  2906. pos += ret;
  2907. }
  2908. if (capa->flags & WPA_DRIVER_FLAGS_AP) {
  2909. ret = os_snprintf(pos, end - pos, "%sAP",
  2910. pos == buf ? "" : " ");
  2911. if (os_snprintf_error(end - pos, ret))
  2912. return pos - buf;
  2913. pos += ret;
  2914. }
  2915. return pos - buf;
  2916. }
  2917. static int ctrl_iface_get_capability_channels(struct wpa_supplicant *wpa_s,
  2918. char *buf, size_t buflen)
  2919. {
  2920. struct hostapd_channel_data *chnl;
  2921. int ret, i, j;
  2922. char *pos, *end, *hmode;
  2923. pos = buf;
  2924. end = pos + buflen;
  2925. for (j = 0; j < wpa_s->hw.num_modes; j++) {
  2926. switch (wpa_s->hw.modes[j].mode) {
  2927. case HOSTAPD_MODE_IEEE80211B:
  2928. hmode = "B";
  2929. break;
  2930. case HOSTAPD_MODE_IEEE80211G:
  2931. hmode = "G";
  2932. break;
  2933. case HOSTAPD_MODE_IEEE80211A:
  2934. hmode = "A";
  2935. break;
  2936. case HOSTAPD_MODE_IEEE80211AD:
  2937. hmode = "AD";
  2938. break;
  2939. default:
  2940. continue;
  2941. }
  2942. ret = os_snprintf(pos, end - pos, "Mode[%s] Channels:", hmode);
  2943. if (os_snprintf_error(end - pos, ret))
  2944. return pos - buf;
  2945. pos += ret;
  2946. chnl = wpa_s->hw.modes[j].channels;
  2947. for (i = 0; i < wpa_s->hw.modes[j].num_channels; i++) {
  2948. if (chnl[i].flag & HOSTAPD_CHAN_DISABLED)
  2949. continue;
  2950. ret = os_snprintf(pos, end - pos, " %d", chnl[i].chan);
  2951. if (os_snprintf_error(end - pos, ret))
  2952. return pos - buf;
  2953. pos += ret;
  2954. }
  2955. ret = os_snprintf(pos, end - pos, "\n");
  2956. if (os_snprintf_error(end - pos, ret))
  2957. return pos - buf;
  2958. pos += ret;
  2959. }
  2960. return pos - buf;
  2961. }
  2962. static int ctrl_iface_get_capability_freq(struct wpa_supplicant *wpa_s,
  2963. char *buf, size_t buflen)
  2964. {
  2965. struct hostapd_channel_data *chnl;
  2966. int ret, i, j;
  2967. char *pos, *end, *hmode;
  2968. pos = buf;
  2969. end = pos + buflen;
  2970. for (j = 0; j < wpa_s->hw.num_modes; j++) {
  2971. switch (wpa_s->hw.modes[j].mode) {
  2972. case HOSTAPD_MODE_IEEE80211B:
  2973. hmode = "B";
  2974. break;
  2975. case HOSTAPD_MODE_IEEE80211G:
  2976. hmode = "G";
  2977. break;
  2978. case HOSTAPD_MODE_IEEE80211A:
  2979. hmode = "A";
  2980. break;
  2981. case HOSTAPD_MODE_IEEE80211AD:
  2982. hmode = "AD";
  2983. break;
  2984. default:
  2985. continue;
  2986. }
  2987. ret = os_snprintf(pos, end - pos, "Mode[%s] Channels:\n",
  2988. hmode);
  2989. if (os_snprintf_error(end - pos, ret))
  2990. return pos - buf;
  2991. pos += ret;
  2992. chnl = wpa_s->hw.modes[j].channels;
  2993. for (i = 0; i < wpa_s->hw.modes[j].num_channels; i++) {
  2994. if (chnl[i].flag & HOSTAPD_CHAN_DISABLED)
  2995. continue;
  2996. ret = os_snprintf(pos, end - pos, " %d = %d MHz%s%s\n",
  2997. chnl[i].chan, chnl[i].freq,
  2998. chnl[i].flag & HOSTAPD_CHAN_NO_IR ?
  2999. " (NO_IR)" : "",
  3000. chnl[i].flag & HOSTAPD_CHAN_RADAR ?
  3001. " (DFS)" : "");
  3002. if (os_snprintf_error(end - pos, ret))
  3003. return pos - buf;
  3004. pos += ret;
  3005. }
  3006. ret = os_snprintf(pos, end - pos, "\n");
  3007. if (os_snprintf_error(end - pos, ret))
  3008. return pos - buf;
  3009. pos += ret;
  3010. }
  3011. return pos - buf;
  3012. }
  3013. static int wpa_supplicant_ctrl_iface_get_capability(
  3014. struct wpa_supplicant *wpa_s, const char *_field, char *buf,
  3015. size_t buflen)
  3016. {
  3017. struct wpa_driver_capa capa;
  3018. int res;
  3019. char *strict;
  3020. char field[30];
  3021. size_t len;
  3022. /* Determine whether or not strict checking was requested */
  3023. len = os_strlcpy(field, _field, sizeof(field));
  3024. if (len >= sizeof(field))
  3025. return -1;
  3026. strict = os_strchr(field, ' ');
  3027. if (strict != NULL) {
  3028. *strict++ = '\0';
  3029. if (os_strcmp(strict, "strict") != 0)
  3030. return -1;
  3031. }
  3032. wpa_printf(MSG_DEBUG, "CTRL_IFACE: GET_CAPABILITY '%s' %s",
  3033. field, strict ? strict : "");
  3034. if (os_strcmp(field, "eap") == 0) {
  3035. return eap_get_names(buf, buflen);
  3036. }
  3037. res = wpa_drv_get_capa(wpa_s, &capa);
  3038. if (os_strcmp(field, "pairwise") == 0)
  3039. return ctrl_iface_get_capability_pairwise(res, strict, &capa,
  3040. buf, buflen);
  3041. if (os_strcmp(field, "group") == 0)
  3042. return ctrl_iface_get_capability_group(res, strict, &capa,
  3043. buf, buflen);
  3044. if (os_strcmp(field, "key_mgmt") == 0)
  3045. return ctrl_iface_get_capability_key_mgmt(res, strict, &capa,
  3046. buf, buflen);
  3047. if (os_strcmp(field, "proto") == 0)
  3048. return ctrl_iface_get_capability_proto(res, strict, &capa,
  3049. buf, buflen);
  3050. if (os_strcmp(field, "auth_alg") == 0)
  3051. return ctrl_iface_get_capability_auth_alg(res, strict, &capa,
  3052. buf, buflen);
  3053. if (os_strcmp(field, "modes") == 0)
  3054. return ctrl_iface_get_capability_modes(res, strict, &capa,
  3055. buf, buflen);
  3056. if (os_strcmp(field, "channels") == 0)
  3057. return ctrl_iface_get_capability_channels(wpa_s, buf, buflen);
  3058. if (os_strcmp(field, "freq") == 0)
  3059. return ctrl_iface_get_capability_freq(wpa_s, buf, buflen);
  3060. #ifdef CONFIG_TDLS
  3061. if (os_strcmp(field, "tdls") == 0)
  3062. return ctrl_iface_get_capability_tdls(wpa_s, buf, buflen);
  3063. #endif /* CONFIG_TDLS */
  3064. #ifdef CONFIG_ERP
  3065. if (os_strcmp(field, "erp") == 0) {
  3066. res = os_snprintf(buf, buflen, "ERP");
  3067. if (os_snprintf_error(buflen, res))
  3068. return -1;
  3069. return res;
  3070. }
  3071. #endif /* CONFIG_EPR */
  3072. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Unknown GET_CAPABILITY field '%s'",
  3073. field);
  3074. return -1;
  3075. }
  3076. #ifdef CONFIG_INTERWORKING
  3077. static char * anqp_add_hex(char *pos, char *end, const char *title,
  3078. struct wpabuf *data)
  3079. {
  3080. char *start = pos;
  3081. size_t i;
  3082. int ret;
  3083. const u8 *d;
  3084. if (data == NULL)
  3085. return start;
  3086. ret = os_snprintf(pos, end - pos, "%s=", title);
  3087. if (os_snprintf_error(end - pos, ret))
  3088. return start;
  3089. pos += ret;
  3090. d = wpabuf_head_u8(data);
  3091. for (i = 0; i < wpabuf_len(data); i++) {
  3092. ret = os_snprintf(pos, end - pos, "%02x", *d++);
  3093. if (os_snprintf_error(end - pos, ret))
  3094. return start;
  3095. pos += ret;
  3096. }
  3097. ret = os_snprintf(pos, end - pos, "\n");
  3098. if (os_snprintf_error(end - pos, ret))
  3099. return start;
  3100. pos += ret;
  3101. return pos;
  3102. }
  3103. #endif /* CONFIG_INTERWORKING */
  3104. static int print_bss_info(struct wpa_supplicant *wpa_s, struct wpa_bss *bss,
  3105. unsigned long mask, char *buf, size_t buflen)
  3106. {
  3107. size_t i;
  3108. int ret;
  3109. char *pos, *end;
  3110. const u8 *ie, *ie2;
  3111. pos = buf;
  3112. end = buf + buflen;
  3113. if (mask & WPA_BSS_MASK_ID) {
  3114. ret = os_snprintf(pos, end - pos, "id=%u\n", bss->id);
  3115. if (os_snprintf_error(end - pos, ret))
  3116. return 0;
  3117. pos += ret;
  3118. }
  3119. if (mask & WPA_BSS_MASK_BSSID) {
  3120. ret = os_snprintf(pos, end - pos, "bssid=" MACSTR "\n",
  3121. MAC2STR(bss->bssid));
  3122. if (os_snprintf_error(end - pos, ret))
  3123. return 0;
  3124. pos += ret;
  3125. }
  3126. if (mask & WPA_BSS_MASK_FREQ) {
  3127. ret = os_snprintf(pos, end - pos, "freq=%d\n", bss->freq);
  3128. if (os_snprintf_error(end - pos, ret))
  3129. return 0;
  3130. pos += ret;
  3131. }
  3132. if (mask & WPA_BSS_MASK_BEACON_INT) {
  3133. ret = os_snprintf(pos, end - pos, "beacon_int=%d\n",
  3134. bss->beacon_int);
  3135. if (os_snprintf_error(end - pos, ret))
  3136. return 0;
  3137. pos += ret;
  3138. }
  3139. if (mask & WPA_BSS_MASK_CAPABILITIES) {
  3140. ret = os_snprintf(pos, end - pos, "capabilities=0x%04x\n",
  3141. bss->caps);
  3142. if (os_snprintf_error(end - pos, ret))
  3143. return 0;
  3144. pos += ret;
  3145. }
  3146. if (mask & WPA_BSS_MASK_QUAL) {
  3147. ret = os_snprintf(pos, end - pos, "qual=%d\n", bss->qual);
  3148. if (os_snprintf_error(end - pos, ret))
  3149. return 0;
  3150. pos += ret;
  3151. }
  3152. if (mask & WPA_BSS_MASK_NOISE) {
  3153. ret = os_snprintf(pos, end - pos, "noise=%d\n", bss->noise);
  3154. if (os_snprintf_error(end - pos, ret))
  3155. return 0;
  3156. pos += ret;
  3157. }
  3158. if (mask & WPA_BSS_MASK_LEVEL) {
  3159. ret = os_snprintf(pos, end - pos, "level=%d\n", bss->level);
  3160. if (os_snprintf_error(end - pos, ret))
  3161. return 0;
  3162. pos += ret;
  3163. }
  3164. if (mask & WPA_BSS_MASK_TSF) {
  3165. ret = os_snprintf(pos, end - pos, "tsf=%016llu\n",
  3166. (unsigned long long) bss->tsf);
  3167. if (os_snprintf_error(end - pos, ret))
  3168. return 0;
  3169. pos += ret;
  3170. }
  3171. if (mask & WPA_BSS_MASK_AGE) {
  3172. struct os_reltime now;
  3173. os_get_reltime(&now);
  3174. ret = os_snprintf(pos, end - pos, "age=%d\n",
  3175. (int) (now.sec - bss->last_update.sec));
  3176. if (os_snprintf_error(end - pos, ret))
  3177. return 0;
  3178. pos += ret;
  3179. }
  3180. if (mask & WPA_BSS_MASK_IE) {
  3181. ret = os_snprintf(pos, end - pos, "ie=");
  3182. if (os_snprintf_error(end - pos, ret))
  3183. return 0;
  3184. pos += ret;
  3185. ie = (const u8 *) (bss + 1);
  3186. for (i = 0; i < bss->ie_len; i++) {
  3187. ret = os_snprintf(pos, end - pos, "%02x", *ie++);
  3188. if (os_snprintf_error(end - pos, ret))
  3189. return 0;
  3190. pos += ret;
  3191. }
  3192. ret = os_snprintf(pos, end - pos, "\n");
  3193. if (os_snprintf_error(end - pos, ret))
  3194. return 0;
  3195. pos += ret;
  3196. }
  3197. if (mask & WPA_BSS_MASK_FLAGS) {
  3198. ret = os_snprintf(pos, end - pos, "flags=");
  3199. if (os_snprintf_error(end - pos, ret))
  3200. return 0;
  3201. pos += ret;
  3202. ie = wpa_bss_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
  3203. if (ie)
  3204. pos = wpa_supplicant_ie_txt(pos, end, "WPA", ie,
  3205. 2 + ie[1]);
  3206. ie2 = wpa_bss_get_ie(bss, WLAN_EID_RSN);
  3207. if (ie2)
  3208. pos = wpa_supplicant_ie_txt(pos, end, "WPA2", ie2,
  3209. 2 + ie2[1]);
  3210. pos = wpa_supplicant_wps_ie_txt(wpa_s, pos, end, bss);
  3211. if (!ie && !ie2 && bss->caps & IEEE80211_CAP_PRIVACY) {
  3212. ret = os_snprintf(pos, end - pos, "[WEP]");
  3213. if (os_snprintf_error(end - pos, ret))
  3214. return 0;
  3215. pos += ret;
  3216. }
  3217. if (bss_is_dmg(bss)) {
  3218. const char *s;
  3219. ret = os_snprintf(pos, end - pos, "[DMG]");
  3220. if (os_snprintf_error(end - pos, ret))
  3221. return 0;
  3222. pos += ret;
  3223. switch (bss->caps & IEEE80211_CAP_DMG_MASK) {
  3224. case IEEE80211_CAP_DMG_IBSS:
  3225. s = "[IBSS]";
  3226. break;
  3227. case IEEE80211_CAP_DMG_AP:
  3228. s = "[ESS]";
  3229. break;
  3230. case IEEE80211_CAP_DMG_PBSS:
  3231. s = "[PBSS]";
  3232. break;
  3233. default:
  3234. s = "";
  3235. break;
  3236. }
  3237. ret = os_snprintf(pos, end - pos, "%s", s);
  3238. if (os_snprintf_error(end - pos, ret))
  3239. return 0;
  3240. pos += ret;
  3241. } else {
  3242. if (bss->caps & IEEE80211_CAP_IBSS) {
  3243. ret = os_snprintf(pos, end - pos, "[IBSS]");
  3244. if (os_snprintf_error(end - pos, ret))
  3245. return 0;
  3246. pos += ret;
  3247. }
  3248. if (bss->caps & IEEE80211_CAP_ESS) {
  3249. ret = os_snprintf(pos, end - pos, "[ESS]");
  3250. if (os_snprintf_error(end - pos, ret))
  3251. return 0;
  3252. pos += ret;
  3253. }
  3254. }
  3255. if (wpa_bss_get_vendor_ie(bss, P2P_IE_VENDOR_TYPE) ||
  3256. wpa_bss_get_vendor_ie_beacon(bss, P2P_IE_VENDOR_TYPE)) {
  3257. ret = os_snprintf(pos, end - pos, "[P2P]");
  3258. if (os_snprintf_error(end - pos, ret))
  3259. return 0;
  3260. pos += ret;
  3261. }
  3262. #ifdef CONFIG_HS20
  3263. if (wpa_bss_get_vendor_ie(bss, HS20_IE_VENDOR_TYPE)) {
  3264. ret = os_snprintf(pos, end - pos, "[HS20]");
  3265. if (os_snprintf_error(end - pos, ret))
  3266. return 0;
  3267. pos += ret;
  3268. }
  3269. #endif /* CONFIG_HS20 */
  3270. ret = os_snprintf(pos, end - pos, "\n");
  3271. if (os_snprintf_error(end - pos, ret))
  3272. return 0;
  3273. pos += ret;
  3274. }
  3275. if (mask & WPA_BSS_MASK_SSID) {
  3276. ret = os_snprintf(pos, end - pos, "ssid=%s\n",
  3277. wpa_ssid_txt(bss->ssid, bss->ssid_len));
  3278. if (os_snprintf_error(end - pos, ret))
  3279. return 0;
  3280. pos += ret;
  3281. }
  3282. #ifdef CONFIG_WPS
  3283. if (mask & WPA_BSS_MASK_WPS_SCAN) {
  3284. ie = (const u8 *) (bss + 1);
  3285. ret = wpas_wps_scan_result_text(ie, bss->ie_len, pos, end);
  3286. if (ret < 0 || ret >= end - pos)
  3287. return 0;
  3288. pos += ret;
  3289. }
  3290. #endif /* CONFIG_WPS */
  3291. #ifdef CONFIG_P2P
  3292. if (mask & WPA_BSS_MASK_P2P_SCAN) {
  3293. ie = (const u8 *) (bss + 1);
  3294. ret = wpas_p2p_scan_result_text(ie, bss->ie_len, pos, end);
  3295. if (ret < 0 || ret >= end - pos)
  3296. return 0;
  3297. pos += ret;
  3298. }
  3299. #endif /* CONFIG_P2P */
  3300. #ifdef CONFIG_WIFI_DISPLAY
  3301. if (mask & WPA_BSS_MASK_WIFI_DISPLAY) {
  3302. struct wpabuf *wfd;
  3303. ie = (const u8 *) (bss + 1);
  3304. wfd = ieee802_11_vendor_ie_concat(ie, bss->ie_len,
  3305. WFD_IE_VENDOR_TYPE);
  3306. if (wfd) {
  3307. ret = os_snprintf(pos, end - pos, "wfd_subelems=");
  3308. if (os_snprintf_error(end - pos, ret)) {
  3309. wpabuf_free(wfd);
  3310. return 0;
  3311. }
  3312. pos += ret;
  3313. pos += wpa_snprintf_hex(pos, end - pos,
  3314. wpabuf_head(wfd),
  3315. wpabuf_len(wfd));
  3316. wpabuf_free(wfd);
  3317. ret = os_snprintf(pos, end - pos, "\n");
  3318. if (os_snprintf_error(end - pos, ret))
  3319. return 0;
  3320. pos += ret;
  3321. }
  3322. }
  3323. #endif /* CONFIG_WIFI_DISPLAY */
  3324. #ifdef CONFIG_INTERWORKING
  3325. if ((mask & WPA_BSS_MASK_INTERNETW) && bss->anqp) {
  3326. struct wpa_bss_anqp *anqp = bss->anqp;
  3327. pos = anqp_add_hex(pos, end, "anqp_venue_name",
  3328. anqp->venue_name);
  3329. pos = anqp_add_hex(pos, end, "anqp_network_auth_type",
  3330. anqp->network_auth_type);
  3331. pos = anqp_add_hex(pos, end, "anqp_roaming_consortium",
  3332. anqp->roaming_consortium);
  3333. pos = anqp_add_hex(pos, end, "anqp_ip_addr_type_availability",
  3334. anqp->ip_addr_type_availability);
  3335. pos = anqp_add_hex(pos, end, "anqp_nai_realm",
  3336. anqp->nai_realm);
  3337. pos = anqp_add_hex(pos, end, "anqp_3gpp", anqp->anqp_3gpp);
  3338. pos = anqp_add_hex(pos, end, "anqp_domain_name",
  3339. anqp->domain_name);
  3340. #ifdef CONFIG_HS20
  3341. pos = anqp_add_hex(pos, end, "hs20_operator_friendly_name",
  3342. anqp->hs20_operator_friendly_name);
  3343. pos = anqp_add_hex(pos, end, "hs20_wan_metrics",
  3344. anqp->hs20_wan_metrics);
  3345. pos = anqp_add_hex(pos, end, "hs20_connection_capability",
  3346. anqp->hs20_connection_capability);
  3347. pos = anqp_add_hex(pos, end, "hs20_operating_class",
  3348. anqp->hs20_operating_class);
  3349. pos = anqp_add_hex(pos, end, "hs20_osu_providers_list",
  3350. anqp->hs20_osu_providers_list);
  3351. #endif /* CONFIG_HS20 */
  3352. }
  3353. #endif /* CONFIG_INTERWORKING */
  3354. #ifdef CONFIG_MESH
  3355. if (mask & WPA_BSS_MASK_MESH_SCAN) {
  3356. ie = (const u8 *) (bss + 1);
  3357. ret = wpas_mesh_scan_result_text(ie, bss->ie_len, pos, end);
  3358. if (ret < 0 || ret >= end - pos)
  3359. return 0;
  3360. pos += ret;
  3361. }
  3362. #endif /* CONFIG_MESH */
  3363. if (mask & WPA_BSS_MASK_DELIM) {
  3364. ret = os_snprintf(pos, end - pos, "====\n");
  3365. if (os_snprintf_error(end - pos, ret))
  3366. return 0;
  3367. pos += ret;
  3368. }
  3369. return pos - buf;
  3370. }
  3371. static int wpa_supplicant_ctrl_iface_bss(struct wpa_supplicant *wpa_s,
  3372. const char *cmd, char *buf,
  3373. size_t buflen)
  3374. {
  3375. u8 bssid[ETH_ALEN];
  3376. size_t i;
  3377. struct wpa_bss *bss;
  3378. struct wpa_bss *bsslast = NULL;
  3379. struct dl_list *next;
  3380. int ret = 0;
  3381. int len;
  3382. char *ctmp;
  3383. unsigned long mask = WPA_BSS_MASK_ALL;
  3384. if (os_strncmp(cmd, "RANGE=", 6) == 0) {
  3385. if (os_strncmp(cmd + 6, "ALL", 3) == 0) {
  3386. bss = dl_list_first(&wpa_s->bss_id, struct wpa_bss,
  3387. list_id);
  3388. bsslast = dl_list_last(&wpa_s->bss_id, struct wpa_bss,
  3389. list_id);
  3390. } else { /* N1-N2 */
  3391. unsigned int id1, id2;
  3392. if ((ctmp = os_strchr(cmd + 6, '-')) == NULL) {
  3393. wpa_printf(MSG_INFO, "Wrong BSS range "
  3394. "format");
  3395. return 0;
  3396. }
  3397. if (*(cmd + 6) == '-')
  3398. id1 = 0;
  3399. else
  3400. id1 = atoi(cmd + 6);
  3401. ctmp++;
  3402. if (*ctmp >= '0' && *ctmp <= '9')
  3403. id2 = atoi(ctmp);
  3404. else
  3405. id2 = (unsigned int) -1;
  3406. bss = wpa_bss_get_id_range(wpa_s, id1, id2);
  3407. if (id2 == (unsigned int) -1)
  3408. bsslast = dl_list_last(&wpa_s->bss_id,
  3409. struct wpa_bss,
  3410. list_id);
  3411. else {
  3412. bsslast = wpa_bss_get_id(wpa_s, id2);
  3413. if (bsslast == NULL && bss && id2 > id1) {
  3414. struct wpa_bss *tmp = bss;
  3415. for (;;) {
  3416. next = tmp->list_id.next;
  3417. if (next == &wpa_s->bss_id)
  3418. break;
  3419. tmp = dl_list_entry(
  3420. next, struct wpa_bss,
  3421. list_id);
  3422. if (tmp->id > id2)
  3423. break;
  3424. bsslast = tmp;
  3425. }
  3426. }
  3427. }
  3428. }
  3429. } else if (os_strncmp(cmd, "FIRST", 5) == 0)
  3430. bss = dl_list_first(&wpa_s->bss_id, struct wpa_bss, list_id);
  3431. else if (os_strncmp(cmd, "LAST", 4) == 0)
  3432. bss = dl_list_last(&wpa_s->bss_id, struct wpa_bss, list_id);
  3433. else if (os_strncmp(cmd, "ID-", 3) == 0) {
  3434. i = atoi(cmd + 3);
  3435. bss = wpa_bss_get_id(wpa_s, i);
  3436. } else if (os_strncmp(cmd, "NEXT-", 5) == 0) {
  3437. i = atoi(cmd + 5);
  3438. bss = wpa_bss_get_id(wpa_s, i);
  3439. if (bss) {
  3440. next = bss->list_id.next;
  3441. if (next == &wpa_s->bss_id)
  3442. bss = NULL;
  3443. else
  3444. bss = dl_list_entry(next, struct wpa_bss,
  3445. list_id);
  3446. }
  3447. #ifdef CONFIG_P2P
  3448. } else if (os_strncmp(cmd, "p2p_dev_addr=", 13) == 0) {
  3449. if (hwaddr_aton(cmd + 13, bssid) == 0)
  3450. bss = wpa_bss_get_p2p_dev_addr(wpa_s, bssid);
  3451. else
  3452. bss = NULL;
  3453. #endif /* CONFIG_P2P */
  3454. } else if (hwaddr_aton(cmd, bssid) == 0)
  3455. bss = wpa_bss_get_bssid(wpa_s, bssid);
  3456. else {
  3457. struct wpa_bss *tmp;
  3458. i = atoi(cmd);
  3459. bss = NULL;
  3460. dl_list_for_each(tmp, &wpa_s->bss_id, struct wpa_bss, list_id)
  3461. {
  3462. if (i-- == 0) {
  3463. bss = tmp;
  3464. break;
  3465. }
  3466. }
  3467. }
  3468. if ((ctmp = os_strstr(cmd, "MASK=")) != NULL) {
  3469. mask = strtoul(ctmp + 5, NULL, 0x10);
  3470. if (mask == 0)
  3471. mask = WPA_BSS_MASK_ALL;
  3472. }
  3473. if (bss == NULL)
  3474. return 0;
  3475. if (bsslast == NULL)
  3476. bsslast = bss;
  3477. do {
  3478. len = print_bss_info(wpa_s, bss, mask, buf, buflen);
  3479. ret += len;
  3480. buf += len;
  3481. buflen -= len;
  3482. if (bss == bsslast) {
  3483. if ((mask & WPA_BSS_MASK_DELIM) && len &&
  3484. (bss == dl_list_last(&wpa_s->bss_id,
  3485. struct wpa_bss, list_id)))
  3486. os_snprintf(buf - 5, 5, "####\n");
  3487. break;
  3488. }
  3489. next = bss->list_id.next;
  3490. if (next == &wpa_s->bss_id)
  3491. break;
  3492. bss = dl_list_entry(next, struct wpa_bss, list_id);
  3493. } while (bss && len);
  3494. return ret;
  3495. }
  3496. static int wpa_supplicant_ctrl_iface_ap_scan(
  3497. struct wpa_supplicant *wpa_s, char *cmd)
  3498. {
  3499. int ap_scan = atoi(cmd);
  3500. return wpa_supplicant_set_ap_scan(wpa_s, ap_scan);
  3501. }
  3502. static int wpa_supplicant_ctrl_iface_scan_interval(
  3503. struct wpa_supplicant *wpa_s, char *cmd)
  3504. {
  3505. int scan_int = atoi(cmd);
  3506. return wpa_supplicant_set_scan_interval(wpa_s, scan_int);
  3507. }
  3508. static int wpa_supplicant_ctrl_iface_bss_expire_age(
  3509. struct wpa_supplicant *wpa_s, char *cmd)
  3510. {
  3511. int expire_age = atoi(cmd);
  3512. return wpa_supplicant_set_bss_expiration_age(wpa_s, expire_age);
  3513. }
  3514. static int wpa_supplicant_ctrl_iface_bss_expire_count(
  3515. struct wpa_supplicant *wpa_s, char *cmd)
  3516. {
  3517. int expire_count = atoi(cmd);
  3518. return wpa_supplicant_set_bss_expiration_count(wpa_s, expire_count);
  3519. }
  3520. static int wpa_supplicant_ctrl_iface_bss_flush(
  3521. struct wpa_supplicant *wpa_s, char *cmd)
  3522. {
  3523. int flush_age = atoi(cmd);
  3524. if (flush_age == 0)
  3525. wpa_bss_flush(wpa_s);
  3526. else
  3527. wpa_bss_flush_by_age(wpa_s, flush_age);
  3528. return 0;
  3529. }
  3530. #ifdef CONFIG_TESTING_OPTIONS
  3531. static void wpa_supplicant_ctrl_iface_drop_sa(struct wpa_supplicant *wpa_s)
  3532. {
  3533. wpa_printf(MSG_DEBUG, "Dropping SA without deauthentication");
  3534. /* MLME-DELETEKEYS.request */
  3535. wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 0, 0, NULL, 0, NULL, 0);
  3536. wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 1, 0, NULL, 0, NULL, 0);
  3537. wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 2, 0, NULL, 0, NULL, 0);
  3538. wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 3, 0, NULL, 0, NULL, 0);
  3539. #ifdef CONFIG_IEEE80211W
  3540. wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 4, 0, NULL, 0, NULL, 0);
  3541. wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 5, 0, NULL, 0, NULL, 0);
  3542. #endif /* CONFIG_IEEE80211W */
  3543. wpa_drv_set_key(wpa_s, WPA_ALG_NONE, wpa_s->bssid, 0, 0, NULL, 0, NULL,
  3544. 0);
  3545. /* MLME-SETPROTECTION.request(None) */
  3546. wpa_drv_mlme_setprotection(wpa_s, wpa_s->bssid,
  3547. MLME_SETPROTECTION_PROTECT_TYPE_NONE,
  3548. MLME_SETPROTECTION_KEY_TYPE_PAIRWISE);
  3549. wpa_sm_drop_sa(wpa_s->wpa);
  3550. }
  3551. #endif /* CONFIG_TESTING_OPTIONS */
  3552. static int wpa_supplicant_ctrl_iface_roam(struct wpa_supplicant *wpa_s,
  3553. char *addr)
  3554. {
  3555. #ifdef CONFIG_NO_SCAN_PROCESSING
  3556. return -1;
  3557. #else /* CONFIG_NO_SCAN_PROCESSING */
  3558. u8 bssid[ETH_ALEN];
  3559. struct wpa_bss *bss;
  3560. struct wpa_ssid *ssid = wpa_s->current_ssid;
  3561. if (hwaddr_aton(addr, bssid)) {
  3562. wpa_printf(MSG_DEBUG, "CTRL_IFACE ROAM: invalid "
  3563. "address '%s'", addr);
  3564. return -1;
  3565. }
  3566. wpa_printf(MSG_DEBUG, "CTRL_IFACE ROAM " MACSTR, MAC2STR(bssid));
  3567. if (!ssid) {
  3568. wpa_printf(MSG_DEBUG, "CTRL_IFACE ROAM: No network "
  3569. "configuration known for the target AP");
  3570. return -1;
  3571. }
  3572. bss = wpa_bss_get(wpa_s, bssid, ssid->ssid, ssid->ssid_len);
  3573. if (!bss) {
  3574. wpa_printf(MSG_DEBUG, "CTRL_IFACE ROAM: Target AP not found "
  3575. "from BSS table");
  3576. return -1;
  3577. }
  3578. /*
  3579. * TODO: Find best network configuration block from configuration to
  3580. * allow roaming to other networks
  3581. */
  3582. wpa_s->reassociate = 1;
  3583. wpa_supplicant_connect(wpa_s, bss, ssid);
  3584. return 0;
  3585. #endif /* CONFIG_NO_SCAN_PROCESSING */
  3586. }
  3587. #ifdef CONFIG_P2P
  3588. static int p2p_ctrl_find(struct wpa_supplicant *wpa_s, char *cmd)
  3589. {
  3590. unsigned int timeout = atoi(cmd);
  3591. enum p2p_discovery_type type = P2P_FIND_START_WITH_FULL;
  3592. u8 dev_id[ETH_ALEN], *_dev_id = NULL;
  3593. u8 dev_type[WPS_DEV_TYPE_LEN], *_dev_type = NULL;
  3594. char *pos;
  3595. unsigned int search_delay;
  3596. if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED) {
  3597. wpa_dbg(wpa_s, MSG_INFO,
  3598. "Reject P2P_FIND since interface is disabled");
  3599. return -1;
  3600. }
  3601. if (os_strstr(cmd, "type=social"))
  3602. type = P2P_FIND_ONLY_SOCIAL;
  3603. else if (os_strstr(cmd, "type=progressive"))
  3604. type = P2P_FIND_PROGRESSIVE;
  3605. pos = os_strstr(cmd, "dev_id=");
  3606. if (pos) {
  3607. pos += 7;
  3608. if (hwaddr_aton(pos, dev_id))
  3609. return -1;
  3610. _dev_id = dev_id;
  3611. }
  3612. pos = os_strstr(cmd, "dev_type=");
  3613. if (pos) {
  3614. pos += 9;
  3615. if (wps_dev_type_str2bin(pos, dev_type) < 0)
  3616. return -1;
  3617. _dev_type = dev_type;
  3618. }
  3619. pos = os_strstr(cmd, "delay=");
  3620. if (pos) {
  3621. pos += 6;
  3622. search_delay = atoi(pos);
  3623. } else
  3624. search_delay = wpas_p2p_search_delay(wpa_s);
  3625. return wpas_p2p_find(wpa_s, timeout, type, _dev_type != NULL, _dev_type,
  3626. _dev_id, search_delay);
  3627. }
  3628. static int p2p_ctrl_connect(struct wpa_supplicant *wpa_s, char *cmd,
  3629. char *buf, size_t buflen)
  3630. {
  3631. u8 addr[ETH_ALEN];
  3632. char *pos, *pos2;
  3633. char *pin = NULL;
  3634. enum p2p_wps_method wps_method;
  3635. int new_pin;
  3636. int ret;
  3637. int persistent_group, persistent_id = -1;
  3638. int join;
  3639. int auth;
  3640. int automatic;
  3641. int go_intent = -1;
  3642. int freq = 0;
  3643. int pd;
  3644. int ht40, vht;
  3645. /* <addr> <"pbc" | "pin" | PIN> [label|display|keypad]
  3646. * [persistent|persistent=<network id>]
  3647. * [join] [auth] [go_intent=<0..15>] [freq=<in MHz>] [provdisc]
  3648. * [ht40] [vht] */
  3649. if (hwaddr_aton(cmd, addr))
  3650. return -1;
  3651. pos = cmd + 17;
  3652. if (*pos != ' ')
  3653. return -1;
  3654. pos++;
  3655. persistent_group = os_strstr(pos, " persistent") != NULL;
  3656. pos2 = os_strstr(pos, " persistent=");
  3657. if (pos2) {
  3658. struct wpa_ssid *ssid;
  3659. persistent_id = atoi(pos2 + 12);
  3660. ssid = wpa_config_get_network(wpa_s->conf, persistent_id);
  3661. if (ssid == NULL || ssid->disabled != 2 ||
  3662. ssid->mode != WPAS_MODE_P2P_GO) {
  3663. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find "
  3664. "SSID id=%d for persistent P2P group (GO)",
  3665. persistent_id);
  3666. return -1;
  3667. }
  3668. }
  3669. join = os_strstr(pos, " join") != NULL;
  3670. auth = os_strstr(pos, " auth") != NULL;
  3671. automatic = os_strstr(pos, " auto") != NULL;
  3672. pd = os_strstr(pos, " provdisc") != NULL;
  3673. vht = (os_strstr(cmd, " vht") != NULL) || wpa_s->conf->p2p_go_vht;
  3674. ht40 = (os_strstr(cmd, " ht40") != NULL) || wpa_s->conf->p2p_go_ht40 ||
  3675. vht;
  3676. pos2 = os_strstr(pos, " go_intent=");
  3677. if (pos2) {
  3678. pos2 += 11;
  3679. go_intent = atoi(pos2);
  3680. if (go_intent < 0 || go_intent > 15)
  3681. return -1;
  3682. }
  3683. pos2 = os_strstr(pos, " freq=");
  3684. if (pos2) {
  3685. pos2 += 6;
  3686. freq = atoi(pos2);
  3687. if (freq <= 0)
  3688. return -1;
  3689. }
  3690. if (os_strncmp(pos, "pin", 3) == 0) {
  3691. /* Request random PIN (to be displayed) and enable the PIN */
  3692. wps_method = WPS_PIN_DISPLAY;
  3693. } else if (os_strncmp(pos, "pbc", 3) == 0) {
  3694. wps_method = WPS_PBC;
  3695. } else {
  3696. pin = pos;
  3697. pos = os_strchr(pin, ' ');
  3698. wps_method = WPS_PIN_KEYPAD;
  3699. if (pos) {
  3700. *pos++ = '\0';
  3701. if (os_strncmp(pos, "display", 7) == 0)
  3702. wps_method = WPS_PIN_DISPLAY;
  3703. }
  3704. if (!wps_pin_str_valid(pin)) {
  3705. os_memcpy(buf, "FAIL-INVALID-PIN\n", 17);
  3706. return 17;
  3707. }
  3708. }
  3709. new_pin = wpas_p2p_connect(wpa_s, addr, pin, wps_method,
  3710. persistent_group, automatic, join,
  3711. auth, go_intent, freq, persistent_id, pd,
  3712. ht40, vht);
  3713. if (new_pin == -2) {
  3714. os_memcpy(buf, "FAIL-CHANNEL-UNAVAILABLE\n", 25);
  3715. return 25;
  3716. }
  3717. if (new_pin == -3) {
  3718. os_memcpy(buf, "FAIL-CHANNEL-UNSUPPORTED\n", 25);
  3719. return 25;
  3720. }
  3721. if (new_pin < 0)
  3722. return -1;
  3723. if (wps_method == WPS_PIN_DISPLAY && pin == NULL) {
  3724. ret = os_snprintf(buf, buflen, "%08d", new_pin);
  3725. if (os_snprintf_error(buflen, ret))
  3726. return -1;
  3727. return ret;
  3728. }
  3729. os_memcpy(buf, "OK\n", 3);
  3730. return 3;
  3731. }
  3732. static int p2p_ctrl_listen(struct wpa_supplicant *wpa_s, char *cmd)
  3733. {
  3734. unsigned int timeout = atoi(cmd);
  3735. if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED) {
  3736. wpa_dbg(wpa_s, MSG_INFO,
  3737. "Reject P2P_LISTEN since interface is disabled");
  3738. return -1;
  3739. }
  3740. return wpas_p2p_listen(wpa_s, timeout);
  3741. }
  3742. static int p2p_ctrl_prov_disc(struct wpa_supplicant *wpa_s, char *cmd)
  3743. {
  3744. u8 addr[ETH_ALEN];
  3745. char *pos;
  3746. enum wpas_p2p_prov_disc_use use = WPAS_P2P_PD_FOR_GO_NEG;
  3747. /* <addr> <config method> [join|auto] */
  3748. if (hwaddr_aton(cmd, addr))
  3749. return -1;
  3750. pos = cmd + 17;
  3751. if (*pos != ' ')
  3752. return -1;
  3753. pos++;
  3754. if (os_strstr(pos, " join") != NULL)
  3755. use = WPAS_P2P_PD_FOR_JOIN;
  3756. else if (os_strstr(pos, " auto") != NULL)
  3757. use = WPAS_P2P_PD_AUTO;
  3758. return wpas_p2p_prov_disc(wpa_s, addr, pos, use);
  3759. }
  3760. static int p2p_get_passphrase(struct wpa_supplicant *wpa_s, char *buf,
  3761. size_t buflen)
  3762. {
  3763. struct wpa_ssid *ssid = wpa_s->current_ssid;
  3764. if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GO ||
  3765. ssid->passphrase == NULL)
  3766. return -1;
  3767. os_strlcpy(buf, ssid->passphrase, buflen);
  3768. return os_strlen(buf);
  3769. }
  3770. static int p2p_ctrl_serv_disc_req(struct wpa_supplicant *wpa_s, char *cmd,
  3771. char *buf, size_t buflen)
  3772. {
  3773. u64 ref;
  3774. int res;
  3775. u8 dst_buf[ETH_ALEN], *dst;
  3776. struct wpabuf *tlvs;
  3777. char *pos;
  3778. size_t len;
  3779. if (hwaddr_aton(cmd, dst_buf))
  3780. return -1;
  3781. dst = dst_buf;
  3782. if (dst[0] == 0 && dst[1] == 0 && dst[2] == 0 &&
  3783. dst[3] == 0 && dst[4] == 0 && dst[5] == 0)
  3784. dst = NULL;
  3785. pos = cmd + 17;
  3786. if (*pos != ' ')
  3787. return -1;
  3788. pos++;
  3789. if (os_strncmp(pos, "upnp ", 5) == 0) {
  3790. u8 version;
  3791. pos += 5;
  3792. if (hexstr2bin(pos, &version, 1) < 0)
  3793. return -1;
  3794. pos += 2;
  3795. if (*pos != ' ')
  3796. return -1;
  3797. pos++;
  3798. ref = wpas_p2p_sd_request_upnp(wpa_s, dst, version, pos);
  3799. #ifdef CONFIG_WIFI_DISPLAY
  3800. } else if (os_strncmp(pos, "wifi-display ", 13) == 0) {
  3801. ref = wpas_p2p_sd_request_wifi_display(wpa_s, dst, pos + 13);
  3802. #endif /* CONFIG_WIFI_DISPLAY */
  3803. } else {
  3804. len = os_strlen(pos);
  3805. if (len & 1)
  3806. return -1;
  3807. len /= 2;
  3808. tlvs = wpabuf_alloc(len);
  3809. if (tlvs == NULL)
  3810. return -1;
  3811. if (hexstr2bin(pos, wpabuf_put(tlvs, len), len) < 0) {
  3812. wpabuf_free(tlvs);
  3813. return -1;
  3814. }
  3815. ref = wpas_p2p_sd_request(wpa_s, dst, tlvs);
  3816. wpabuf_free(tlvs);
  3817. }
  3818. if (ref == 0)
  3819. return -1;
  3820. res = os_snprintf(buf, buflen, "%llx", (long long unsigned) ref);
  3821. if (os_snprintf_error(buflen, res))
  3822. return -1;
  3823. return res;
  3824. }
  3825. static int p2p_ctrl_serv_disc_cancel_req(struct wpa_supplicant *wpa_s,
  3826. char *cmd)
  3827. {
  3828. long long unsigned val;
  3829. u64 req;
  3830. if (sscanf(cmd, "%llx", &val) != 1)
  3831. return -1;
  3832. req = val;
  3833. return wpas_p2p_sd_cancel_request(wpa_s, req);
  3834. }
  3835. static int p2p_ctrl_serv_disc_resp(struct wpa_supplicant *wpa_s, char *cmd)
  3836. {
  3837. int freq;
  3838. u8 dst[ETH_ALEN];
  3839. u8 dialog_token;
  3840. struct wpabuf *resp_tlvs;
  3841. char *pos, *pos2;
  3842. size_t len;
  3843. pos = os_strchr(cmd, ' ');
  3844. if (pos == NULL)
  3845. return -1;
  3846. *pos++ = '\0';
  3847. freq = atoi(cmd);
  3848. if (freq == 0)
  3849. return -1;
  3850. if (hwaddr_aton(pos, dst))
  3851. return -1;
  3852. pos += 17;
  3853. if (*pos != ' ')
  3854. return -1;
  3855. pos++;
  3856. pos2 = os_strchr(pos, ' ');
  3857. if (pos2 == NULL)
  3858. return -1;
  3859. *pos2++ = '\0';
  3860. dialog_token = atoi(pos);
  3861. len = os_strlen(pos2);
  3862. if (len & 1)
  3863. return -1;
  3864. len /= 2;
  3865. resp_tlvs = wpabuf_alloc(len);
  3866. if (resp_tlvs == NULL)
  3867. return -1;
  3868. if (hexstr2bin(pos2, wpabuf_put(resp_tlvs, len), len) < 0) {
  3869. wpabuf_free(resp_tlvs);
  3870. return -1;
  3871. }
  3872. wpas_p2p_sd_response(wpa_s, freq, dst, dialog_token, resp_tlvs);
  3873. wpabuf_free(resp_tlvs);
  3874. return 0;
  3875. }
  3876. static int p2p_ctrl_serv_disc_external(struct wpa_supplicant *wpa_s,
  3877. char *cmd)
  3878. {
  3879. if (os_strcmp(cmd, "0") && os_strcmp(cmd, "1"))
  3880. return -1;
  3881. wpa_s->p2p_sd_over_ctrl_iface = atoi(cmd);
  3882. return 0;
  3883. }
  3884. static int p2p_ctrl_service_add_bonjour(struct wpa_supplicant *wpa_s,
  3885. char *cmd)
  3886. {
  3887. char *pos;
  3888. size_t len;
  3889. struct wpabuf *query, *resp;
  3890. pos = os_strchr(cmd, ' ');
  3891. if (pos == NULL)
  3892. return -1;
  3893. *pos++ = '\0';
  3894. len = os_strlen(cmd);
  3895. if (len & 1)
  3896. return -1;
  3897. len /= 2;
  3898. query = wpabuf_alloc(len);
  3899. if (query == NULL)
  3900. return -1;
  3901. if (hexstr2bin(cmd, wpabuf_put(query, len), len) < 0) {
  3902. wpabuf_free(query);
  3903. return -1;
  3904. }
  3905. len = os_strlen(pos);
  3906. if (len & 1) {
  3907. wpabuf_free(query);
  3908. return -1;
  3909. }
  3910. len /= 2;
  3911. resp = wpabuf_alloc(len);
  3912. if (resp == NULL) {
  3913. wpabuf_free(query);
  3914. return -1;
  3915. }
  3916. if (hexstr2bin(pos, wpabuf_put(resp, len), len) < 0) {
  3917. wpabuf_free(query);
  3918. wpabuf_free(resp);
  3919. return -1;
  3920. }
  3921. if (wpas_p2p_service_add_bonjour(wpa_s, query, resp) < 0) {
  3922. wpabuf_free(query);
  3923. wpabuf_free(resp);
  3924. return -1;
  3925. }
  3926. return 0;
  3927. }
  3928. static int p2p_ctrl_service_add_upnp(struct wpa_supplicant *wpa_s, char *cmd)
  3929. {
  3930. char *pos;
  3931. u8 version;
  3932. pos = os_strchr(cmd, ' ');
  3933. if (pos == NULL)
  3934. return -1;
  3935. *pos++ = '\0';
  3936. if (hexstr2bin(cmd, &version, 1) < 0)
  3937. return -1;
  3938. return wpas_p2p_service_add_upnp(wpa_s, version, pos);
  3939. }
  3940. static int p2p_ctrl_service_add(struct wpa_supplicant *wpa_s, char *cmd)
  3941. {
  3942. char *pos;
  3943. pos = os_strchr(cmd, ' ');
  3944. if (pos == NULL)
  3945. return -1;
  3946. *pos++ = '\0';
  3947. if (os_strcmp(cmd, "bonjour") == 0)
  3948. return p2p_ctrl_service_add_bonjour(wpa_s, pos);
  3949. if (os_strcmp(cmd, "upnp") == 0)
  3950. return p2p_ctrl_service_add_upnp(wpa_s, pos);
  3951. wpa_printf(MSG_DEBUG, "Unknown service '%s'", cmd);
  3952. return -1;
  3953. }
  3954. static int p2p_ctrl_service_del_bonjour(struct wpa_supplicant *wpa_s,
  3955. char *cmd)
  3956. {
  3957. size_t len;
  3958. struct wpabuf *query;
  3959. int ret;
  3960. len = os_strlen(cmd);
  3961. if (len & 1)
  3962. return -1;
  3963. len /= 2;
  3964. query = wpabuf_alloc(len);
  3965. if (query == NULL)
  3966. return -1;
  3967. if (hexstr2bin(cmd, wpabuf_put(query, len), len) < 0) {
  3968. wpabuf_free(query);
  3969. return -1;
  3970. }
  3971. ret = wpas_p2p_service_del_bonjour(wpa_s, query);
  3972. wpabuf_free(query);
  3973. return ret;
  3974. }
  3975. static int p2p_ctrl_service_del_upnp(struct wpa_supplicant *wpa_s, char *cmd)
  3976. {
  3977. char *pos;
  3978. u8 version;
  3979. pos = os_strchr(cmd, ' ');
  3980. if (pos == NULL)
  3981. return -1;
  3982. *pos++ = '\0';
  3983. if (hexstr2bin(cmd, &version, 1) < 0)
  3984. return -1;
  3985. return wpas_p2p_service_del_upnp(wpa_s, version, pos);
  3986. }
  3987. static int p2p_ctrl_service_del(struct wpa_supplicant *wpa_s, char *cmd)
  3988. {
  3989. char *pos;
  3990. pos = os_strchr(cmd, ' ');
  3991. if (pos == NULL)
  3992. return -1;
  3993. *pos++ = '\0';
  3994. if (os_strcmp(cmd, "bonjour") == 0)
  3995. return p2p_ctrl_service_del_bonjour(wpa_s, pos);
  3996. if (os_strcmp(cmd, "upnp") == 0)
  3997. return p2p_ctrl_service_del_upnp(wpa_s, pos);
  3998. wpa_printf(MSG_DEBUG, "Unknown service '%s'", cmd);
  3999. return -1;
  4000. }
  4001. static int p2p_ctrl_reject(struct wpa_supplicant *wpa_s, char *cmd)
  4002. {
  4003. u8 addr[ETH_ALEN];
  4004. /* <addr> */
  4005. if (hwaddr_aton(cmd, addr))
  4006. return -1;
  4007. return wpas_p2p_reject(wpa_s, addr);
  4008. }
  4009. static int p2p_ctrl_invite_persistent(struct wpa_supplicant *wpa_s, char *cmd)
  4010. {
  4011. char *pos;
  4012. int id;
  4013. struct wpa_ssid *ssid;
  4014. u8 *_peer = NULL, peer[ETH_ALEN];
  4015. int freq = 0, pref_freq = 0;
  4016. int ht40, vht;
  4017. id = atoi(cmd);
  4018. pos = os_strstr(cmd, " peer=");
  4019. if (pos) {
  4020. pos += 6;
  4021. if (hwaddr_aton(pos, peer))
  4022. return -1;
  4023. _peer = peer;
  4024. }
  4025. ssid = wpa_config_get_network(wpa_s->conf, id);
  4026. if (ssid == NULL || ssid->disabled != 2) {
  4027. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find SSID id=%d "
  4028. "for persistent P2P group",
  4029. id);
  4030. return -1;
  4031. }
  4032. pos = os_strstr(cmd, " freq=");
  4033. if (pos) {
  4034. pos += 6;
  4035. freq = atoi(pos);
  4036. if (freq <= 0)
  4037. return -1;
  4038. }
  4039. pos = os_strstr(cmd, " pref=");
  4040. if (pos) {
  4041. pos += 6;
  4042. pref_freq = atoi(pos);
  4043. if (pref_freq <= 0)
  4044. return -1;
  4045. }
  4046. vht = (os_strstr(cmd, " vht") != NULL) || wpa_s->conf->p2p_go_vht;
  4047. ht40 = (os_strstr(cmd, " ht40") != NULL) || wpa_s->conf->p2p_go_ht40 ||
  4048. vht;
  4049. return wpas_p2p_invite(wpa_s, _peer, ssid, NULL, freq, ht40, vht,
  4050. pref_freq);
  4051. }
  4052. static int p2p_ctrl_invite_group(struct wpa_supplicant *wpa_s, char *cmd)
  4053. {
  4054. char *pos;
  4055. u8 peer[ETH_ALEN], go_dev_addr[ETH_ALEN], *go_dev = NULL;
  4056. pos = os_strstr(cmd, " peer=");
  4057. if (!pos)
  4058. return -1;
  4059. *pos = '\0';
  4060. pos += 6;
  4061. if (hwaddr_aton(pos, peer)) {
  4062. wpa_printf(MSG_DEBUG, "P2P: Invalid MAC address '%s'", pos);
  4063. return -1;
  4064. }
  4065. pos = os_strstr(pos, " go_dev_addr=");
  4066. if (pos) {
  4067. pos += 13;
  4068. if (hwaddr_aton(pos, go_dev_addr)) {
  4069. wpa_printf(MSG_DEBUG, "P2P: Invalid MAC address '%s'",
  4070. pos);
  4071. return -1;
  4072. }
  4073. go_dev = go_dev_addr;
  4074. }
  4075. return wpas_p2p_invite_group(wpa_s, cmd, peer, go_dev);
  4076. }
  4077. static int p2p_ctrl_invite(struct wpa_supplicant *wpa_s, char *cmd)
  4078. {
  4079. if (os_strncmp(cmd, "persistent=", 11) == 0)
  4080. return p2p_ctrl_invite_persistent(wpa_s, cmd + 11);
  4081. if (os_strncmp(cmd, "group=", 6) == 0)
  4082. return p2p_ctrl_invite_group(wpa_s, cmd + 6);
  4083. return -1;
  4084. }
  4085. static int p2p_ctrl_group_add_persistent(struct wpa_supplicant *wpa_s,
  4086. char *cmd, int freq, int ht40,
  4087. int vht)
  4088. {
  4089. int id;
  4090. struct wpa_ssid *ssid;
  4091. id = atoi(cmd);
  4092. ssid = wpa_config_get_network(wpa_s->conf, id);
  4093. if (ssid == NULL || ssid->disabled != 2) {
  4094. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find SSID id=%d "
  4095. "for persistent P2P group",
  4096. id);
  4097. return -1;
  4098. }
  4099. return wpas_p2p_group_add_persistent(wpa_s, ssid, 0, freq, 0, ht40, vht,
  4100. NULL, 0);
  4101. }
  4102. static int p2p_ctrl_group_add(struct wpa_supplicant *wpa_s, char *cmd)
  4103. {
  4104. int freq = 0, ht40, vht;
  4105. char *pos;
  4106. pos = os_strstr(cmd, "freq=");
  4107. if (pos)
  4108. freq = atoi(pos + 5);
  4109. vht = (os_strstr(cmd, "vht") != NULL) || wpa_s->conf->p2p_go_vht;
  4110. ht40 = (os_strstr(cmd, "ht40") != NULL) || wpa_s->conf->p2p_go_ht40 ||
  4111. vht;
  4112. if (os_strncmp(cmd, "persistent=", 11) == 0)
  4113. return p2p_ctrl_group_add_persistent(wpa_s, cmd + 11, freq,
  4114. ht40, vht);
  4115. if (os_strcmp(cmd, "persistent") == 0 ||
  4116. os_strncmp(cmd, "persistent ", 11) == 0)
  4117. return wpas_p2p_group_add(wpa_s, 1, freq, ht40, vht);
  4118. if (os_strncmp(cmd, "freq=", 5) == 0)
  4119. return wpas_p2p_group_add(wpa_s, 0, freq, ht40, vht);
  4120. if (ht40)
  4121. return wpas_p2p_group_add(wpa_s, 0, freq, ht40, vht);
  4122. wpa_printf(MSG_DEBUG, "CTRL: Invalid P2P_GROUP_ADD parameters '%s'",
  4123. cmd);
  4124. return -1;
  4125. }
  4126. static int p2p_ctrl_peer(struct wpa_supplicant *wpa_s, char *cmd,
  4127. char *buf, size_t buflen)
  4128. {
  4129. u8 addr[ETH_ALEN], *addr_ptr;
  4130. int next, res;
  4131. const struct p2p_peer_info *info;
  4132. char *pos, *end;
  4133. char devtype[WPS_DEV_TYPE_BUFSIZE];
  4134. struct wpa_ssid *ssid;
  4135. size_t i;
  4136. if (!wpa_s->global->p2p)
  4137. return -1;
  4138. if (os_strcmp(cmd, "FIRST") == 0) {
  4139. addr_ptr = NULL;
  4140. next = 0;
  4141. } else if (os_strncmp(cmd, "NEXT-", 5) == 0) {
  4142. if (hwaddr_aton(cmd + 5, addr) < 0)
  4143. return -1;
  4144. addr_ptr = addr;
  4145. next = 1;
  4146. } else {
  4147. if (hwaddr_aton(cmd, addr) < 0)
  4148. return -1;
  4149. addr_ptr = addr;
  4150. next = 0;
  4151. }
  4152. info = p2p_get_peer_info(wpa_s->global->p2p, addr_ptr, next);
  4153. if (info == NULL)
  4154. return -1;
  4155. pos = buf;
  4156. end = buf + buflen;
  4157. res = os_snprintf(pos, end - pos, MACSTR "\n"
  4158. "pri_dev_type=%s\n"
  4159. "device_name=%s\n"
  4160. "manufacturer=%s\n"
  4161. "model_name=%s\n"
  4162. "model_number=%s\n"
  4163. "serial_number=%s\n"
  4164. "config_methods=0x%x\n"
  4165. "dev_capab=0x%x\n"
  4166. "group_capab=0x%x\n"
  4167. "level=%d\n",
  4168. MAC2STR(info->p2p_device_addr),
  4169. wps_dev_type_bin2str(info->pri_dev_type,
  4170. devtype, sizeof(devtype)),
  4171. info->device_name,
  4172. info->manufacturer,
  4173. info->model_name,
  4174. info->model_number,
  4175. info->serial_number,
  4176. info->config_methods,
  4177. info->dev_capab,
  4178. info->group_capab,
  4179. info->level);
  4180. if (os_snprintf_error(end - pos, res))
  4181. return pos - buf;
  4182. pos += res;
  4183. for (i = 0; i < info->wps_sec_dev_type_list_len / WPS_DEV_TYPE_LEN; i++)
  4184. {
  4185. const u8 *t;
  4186. t = &info->wps_sec_dev_type_list[i * WPS_DEV_TYPE_LEN];
  4187. res = os_snprintf(pos, end - pos, "sec_dev_type=%s\n",
  4188. wps_dev_type_bin2str(t, devtype,
  4189. sizeof(devtype)));
  4190. if (os_snprintf_error(end - pos, res))
  4191. return pos - buf;
  4192. pos += res;
  4193. }
  4194. ssid = wpas_p2p_get_persistent(wpa_s, info->p2p_device_addr, NULL, 0);
  4195. if (ssid) {
  4196. res = os_snprintf(pos, end - pos, "persistent=%d\n", ssid->id);
  4197. if (os_snprintf_error(end - pos, res))
  4198. return pos - buf;
  4199. pos += res;
  4200. }
  4201. res = p2p_get_peer_info_txt(info, pos, end - pos);
  4202. if (res < 0)
  4203. return pos - buf;
  4204. pos += res;
  4205. if (info->vendor_elems) {
  4206. res = os_snprintf(pos, end - pos, "vendor_elems=");
  4207. if (os_snprintf_error(end - pos, res))
  4208. return pos - buf;
  4209. pos += res;
  4210. pos += wpa_snprintf_hex(pos, end - pos,
  4211. wpabuf_head(info->vendor_elems),
  4212. wpabuf_len(info->vendor_elems));
  4213. res = os_snprintf(pos, end - pos, "\n");
  4214. if (os_snprintf_error(end - pos, res))
  4215. return pos - buf;
  4216. pos += res;
  4217. }
  4218. return pos - buf;
  4219. }
  4220. static int p2p_ctrl_disallow_freq(struct wpa_supplicant *wpa_s,
  4221. const char *param)
  4222. {
  4223. unsigned int i;
  4224. if (wpa_s->global->p2p == NULL)
  4225. return -1;
  4226. if (freq_range_list_parse(&wpa_s->global->p2p_disallow_freq, param) < 0)
  4227. return -1;
  4228. for (i = 0; i < wpa_s->global->p2p_disallow_freq.num; i++) {
  4229. struct wpa_freq_range *freq;
  4230. freq = &wpa_s->global->p2p_disallow_freq.range[i];
  4231. wpa_printf(MSG_DEBUG, "P2P: Disallowed frequency range %u-%u",
  4232. freq->min, freq->max);
  4233. }
  4234. wpas_p2p_update_channel_list(wpa_s);
  4235. return 0;
  4236. }
  4237. static int p2p_ctrl_set(struct wpa_supplicant *wpa_s, char *cmd)
  4238. {
  4239. char *param;
  4240. if (wpa_s->global->p2p == NULL)
  4241. return -1;
  4242. param = os_strchr(cmd, ' ');
  4243. if (param == NULL)
  4244. return -1;
  4245. *param++ = '\0';
  4246. if (os_strcmp(cmd, "discoverability") == 0) {
  4247. p2p_set_client_discoverability(wpa_s->global->p2p,
  4248. atoi(param));
  4249. return 0;
  4250. }
  4251. if (os_strcmp(cmd, "managed") == 0) {
  4252. p2p_set_managed_oper(wpa_s->global->p2p, atoi(param));
  4253. return 0;
  4254. }
  4255. if (os_strcmp(cmd, "listen_channel") == 0) {
  4256. return p2p_set_listen_channel(wpa_s->global->p2p, 81,
  4257. atoi(param), 1);
  4258. }
  4259. if (os_strcmp(cmd, "ssid_postfix") == 0) {
  4260. return p2p_set_ssid_postfix(wpa_s->global->p2p, (u8 *) param,
  4261. os_strlen(param));
  4262. }
  4263. if (os_strcmp(cmd, "noa") == 0) {
  4264. char *pos;
  4265. int count, start, duration;
  4266. /* GO NoA parameters: count,start_offset(ms),duration(ms) */
  4267. count = atoi(param);
  4268. pos = os_strchr(param, ',');
  4269. if (pos == NULL)
  4270. return -1;
  4271. pos++;
  4272. start = atoi(pos);
  4273. pos = os_strchr(pos, ',');
  4274. if (pos == NULL)
  4275. return -1;
  4276. pos++;
  4277. duration = atoi(pos);
  4278. if (count < 0 || count > 255 || start < 0 || duration < 0)
  4279. return -1;
  4280. if (count == 0 && duration > 0)
  4281. return -1;
  4282. wpa_printf(MSG_DEBUG, "CTRL_IFACE: P2P_SET GO NoA: count=%d "
  4283. "start=%d duration=%d", count, start, duration);
  4284. return wpas_p2p_set_noa(wpa_s, count, start, duration);
  4285. }
  4286. if (os_strcmp(cmd, "ps") == 0)
  4287. return wpa_drv_set_p2p_powersave(wpa_s, atoi(param), -1, -1);
  4288. if (os_strcmp(cmd, "oppps") == 0)
  4289. return wpa_drv_set_p2p_powersave(wpa_s, -1, atoi(param), -1);
  4290. if (os_strcmp(cmd, "ctwindow") == 0)
  4291. return wpa_drv_set_p2p_powersave(wpa_s, -1, -1, atoi(param));
  4292. if (os_strcmp(cmd, "disabled") == 0) {
  4293. wpa_s->global->p2p_disabled = atoi(param);
  4294. wpa_printf(MSG_DEBUG, "P2P functionality %s",
  4295. wpa_s->global->p2p_disabled ?
  4296. "disabled" : "enabled");
  4297. if (wpa_s->global->p2p_disabled) {
  4298. wpas_p2p_stop_find(wpa_s);
  4299. os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN);
  4300. p2p_flush(wpa_s->global->p2p);
  4301. }
  4302. return 0;
  4303. }
  4304. if (os_strcmp(cmd, "conc_pref") == 0) {
  4305. if (os_strcmp(param, "sta") == 0)
  4306. wpa_s->global->conc_pref = WPA_CONC_PREF_STA;
  4307. else if (os_strcmp(param, "p2p") == 0)
  4308. wpa_s->global->conc_pref = WPA_CONC_PREF_P2P;
  4309. else {
  4310. wpa_printf(MSG_INFO, "Invalid conc_pref value");
  4311. return -1;
  4312. }
  4313. wpa_printf(MSG_DEBUG, "Single channel concurrency preference: "
  4314. "%s", param);
  4315. return 0;
  4316. }
  4317. if (os_strcmp(cmd, "force_long_sd") == 0) {
  4318. wpa_s->force_long_sd = atoi(param);
  4319. return 0;
  4320. }
  4321. if (os_strcmp(cmd, "peer_filter") == 0) {
  4322. u8 addr[ETH_ALEN];
  4323. if (hwaddr_aton(param, addr))
  4324. return -1;
  4325. p2p_set_peer_filter(wpa_s->global->p2p, addr);
  4326. return 0;
  4327. }
  4328. if (os_strcmp(cmd, "cross_connect") == 0)
  4329. return wpas_p2p_set_cross_connect(wpa_s, atoi(param));
  4330. if (os_strcmp(cmd, "go_apsd") == 0) {
  4331. if (os_strcmp(param, "disable") == 0)
  4332. wpa_s->set_ap_uapsd = 0;
  4333. else {
  4334. wpa_s->set_ap_uapsd = 1;
  4335. wpa_s->ap_uapsd = atoi(param);
  4336. }
  4337. return 0;
  4338. }
  4339. if (os_strcmp(cmd, "client_apsd") == 0) {
  4340. if (os_strcmp(param, "disable") == 0)
  4341. wpa_s->set_sta_uapsd = 0;
  4342. else {
  4343. int be, bk, vi, vo;
  4344. char *pos;
  4345. /* format: BE,BK,VI,VO;max SP Length */
  4346. be = atoi(param);
  4347. pos = os_strchr(param, ',');
  4348. if (pos == NULL)
  4349. return -1;
  4350. pos++;
  4351. bk = atoi(pos);
  4352. pos = os_strchr(pos, ',');
  4353. if (pos == NULL)
  4354. return -1;
  4355. pos++;
  4356. vi = atoi(pos);
  4357. pos = os_strchr(pos, ',');
  4358. if (pos == NULL)
  4359. return -1;
  4360. pos++;
  4361. vo = atoi(pos);
  4362. /* ignore max SP Length for now */
  4363. wpa_s->set_sta_uapsd = 1;
  4364. wpa_s->sta_uapsd = 0;
  4365. if (be)
  4366. wpa_s->sta_uapsd |= BIT(0);
  4367. if (bk)
  4368. wpa_s->sta_uapsd |= BIT(1);
  4369. if (vi)
  4370. wpa_s->sta_uapsd |= BIT(2);
  4371. if (vo)
  4372. wpa_s->sta_uapsd |= BIT(3);
  4373. }
  4374. return 0;
  4375. }
  4376. if (os_strcmp(cmd, "disallow_freq") == 0)
  4377. return p2p_ctrl_disallow_freq(wpa_s, param);
  4378. if (os_strcmp(cmd, "disc_int") == 0) {
  4379. int min_disc_int, max_disc_int, max_disc_tu;
  4380. char *pos;
  4381. pos = param;
  4382. min_disc_int = atoi(pos);
  4383. pos = os_strchr(pos, ' ');
  4384. if (pos == NULL)
  4385. return -1;
  4386. *pos++ = '\0';
  4387. max_disc_int = atoi(pos);
  4388. pos = os_strchr(pos, ' ');
  4389. if (pos == NULL)
  4390. return -1;
  4391. *pos++ = '\0';
  4392. max_disc_tu = atoi(pos);
  4393. return p2p_set_disc_int(wpa_s->global->p2p, min_disc_int,
  4394. max_disc_int, max_disc_tu);
  4395. }
  4396. if (os_strcmp(cmd, "per_sta_psk") == 0) {
  4397. wpa_s->global->p2p_per_sta_psk = !!atoi(param);
  4398. return 0;
  4399. }
  4400. #ifdef CONFIG_WPS_NFC
  4401. if (os_strcmp(cmd, "nfc_tag") == 0)
  4402. return wpas_p2p_nfc_tag_enabled(wpa_s, !!atoi(param));
  4403. #endif /* CONFIG_WPS_NFC */
  4404. if (os_strcmp(cmd, "disable_ip_addr_req") == 0) {
  4405. wpa_s->p2p_disable_ip_addr_req = !!atoi(param);
  4406. return 0;
  4407. }
  4408. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Unknown P2P_SET field value '%s'",
  4409. cmd);
  4410. return -1;
  4411. }
  4412. static void p2p_ctrl_flush(struct wpa_supplicant *wpa_s)
  4413. {
  4414. os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN);
  4415. wpa_s->force_long_sd = 0;
  4416. wpas_p2p_stop_find(wpa_s);
  4417. if (wpa_s->global->p2p)
  4418. p2p_flush(wpa_s->global->p2p);
  4419. }
  4420. static int p2p_ctrl_presence_req(struct wpa_supplicant *wpa_s, char *cmd)
  4421. {
  4422. char *pos, *pos2;
  4423. unsigned int dur1 = 0, int1 = 0, dur2 = 0, int2 = 0;
  4424. if (cmd[0]) {
  4425. pos = os_strchr(cmd, ' ');
  4426. if (pos == NULL)
  4427. return -1;
  4428. *pos++ = '\0';
  4429. dur1 = atoi(cmd);
  4430. pos2 = os_strchr(pos, ' ');
  4431. if (pos2)
  4432. *pos2++ = '\0';
  4433. int1 = atoi(pos);
  4434. } else
  4435. pos2 = NULL;
  4436. if (pos2) {
  4437. pos = os_strchr(pos2, ' ');
  4438. if (pos == NULL)
  4439. return -1;
  4440. *pos++ = '\0';
  4441. dur2 = atoi(pos2);
  4442. int2 = atoi(pos);
  4443. }
  4444. return wpas_p2p_presence_req(wpa_s, dur1, int1, dur2, int2);
  4445. }
  4446. static int p2p_ctrl_ext_listen(struct wpa_supplicant *wpa_s, char *cmd)
  4447. {
  4448. char *pos;
  4449. unsigned int period = 0, interval = 0;
  4450. if (cmd[0]) {
  4451. pos = os_strchr(cmd, ' ');
  4452. if (pos == NULL)
  4453. return -1;
  4454. *pos++ = '\0';
  4455. period = atoi(cmd);
  4456. interval = atoi(pos);
  4457. }
  4458. return wpas_p2p_ext_listen(wpa_s, period, interval);
  4459. }
  4460. static int p2p_ctrl_remove_client(struct wpa_supplicant *wpa_s, const char *cmd)
  4461. {
  4462. const char *pos;
  4463. u8 peer[ETH_ALEN];
  4464. int iface_addr = 0;
  4465. pos = cmd;
  4466. if (os_strncmp(pos, "iface=", 6) == 0) {
  4467. iface_addr = 1;
  4468. pos += 6;
  4469. }
  4470. if (hwaddr_aton(pos, peer))
  4471. return -1;
  4472. wpas_p2p_remove_client(wpa_s, peer, iface_addr);
  4473. return 0;
  4474. }
  4475. #endif /* CONFIG_P2P */
  4476. static int * freq_range_to_channel_list(struct wpa_supplicant *wpa_s, char *val)
  4477. {
  4478. struct wpa_freq_range_list ranges;
  4479. int *freqs = NULL;
  4480. struct hostapd_hw_modes *mode;
  4481. u16 i;
  4482. if (wpa_s->hw.modes == NULL)
  4483. return NULL;
  4484. os_memset(&ranges, 0, sizeof(ranges));
  4485. if (freq_range_list_parse(&ranges, val) < 0)
  4486. return NULL;
  4487. for (i = 0; i < wpa_s->hw.num_modes; i++) {
  4488. int j;
  4489. mode = &wpa_s->hw.modes[i];
  4490. for (j = 0; j < mode->num_channels; j++) {
  4491. unsigned int freq;
  4492. if (mode->channels[j].flag & HOSTAPD_CHAN_DISABLED)
  4493. continue;
  4494. freq = mode->channels[j].freq;
  4495. if (!freq_range_list_includes(&ranges, freq))
  4496. continue;
  4497. int_array_add_unique(&freqs, freq);
  4498. }
  4499. }
  4500. os_free(ranges.range);
  4501. return freqs;
  4502. }
  4503. #ifdef CONFIG_INTERWORKING
  4504. static int ctrl_interworking_select(struct wpa_supplicant *wpa_s, char *param)
  4505. {
  4506. int auto_sel = 0;
  4507. int *freqs = NULL;
  4508. if (param) {
  4509. char *pos;
  4510. auto_sel = os_strstr(param, "auto") != NULL;
  4511. pos = os_strstr(param, "freq=");
  4512. if (pos) {
  4513. freqs = freq_range_to_channel_list(wpa_s, pos + 5);
  4514. if (freqs == NULL)
  4515. return -1;
  4516. }
  4517. }
  4518. return interworking_select(wpa_s, auto_sel, freqs);
  4519. }
  4520. static int ctrl_interworking_connect(struct wpa_supplicant *wpa_s, char *dst)
  4521. {
  4522. u8 bssid[ETH_ALEN];
  4523. struct wpa_bss *bss;
  4524. if (hwaddr_aton(dst, bssid)) {
  4525. wpa_printf(MSG_DEBUG, "Invalid BSSID '%s'", dst);
  4526. return -1;
  4527. }
  4528. bss = wpa_bss_get_bssid(wpa_s, bssid);
  4529. if (bss == NULL) {
  4530. wpa_printf(MSG_DEBUG, "Could not find BSS " MACSTR,
  4531. MAC2STR(bssid));
  4532. return -1;
  4533. }
  4534. return interworking_connect(wpa_s, bss);
  4535. }
  4536. static int get_anqp(struct wpa_supplicant *wpa_s, char *dst)
  4537. {
  4538. u8 dst_addr[ETH_ALEN];
  4539. int used;
  4540. char *pos;
  4541. #define MAX_ANQP_INFO_ID 100
  4542. u16 id[MAX_ANQP_INFO_ID];
  4543. size_t num_id = 0;
  4544. u32 subtypes = 0;
  4545. used = hwaddr_aton2(dst, dst_addr);
  4546. if (used < 0)
  4547. return -1;
  4548. pos = dst + used;
  4549. while (num_id < MAX_ANQP_INFO_ID) {
  4550. if (os_strncmp(pos, "hs20:", 5) == 0) {
  4551. #ifdef CONFIG_HS20
  4552. int num = atoi(pos + 5);
  4553. if (num <= 0 || num > 31)
  4554. return -1;
  4555. subtypes |= BIT(num);
  4556. #else /* CONFIG_HS20 */
  4557. return -1;
  4558. #endif /* CONFIG_HS20 */
  4559. } else {
  4560. id[num_id] = atoi(pos);
  4561. if (id[num_id])
  4562. num_id++;
  4563. }
  4564. pos = os_strchr(pos + 1, ',');
  4565. if (pos == NULL)
  4566. break;
  4567. pos++;
  4568. }
  4569. if (num_id == 0)
  4570. return -1;
  4571. return anqp_send_req(wpa_s, dst_addr, id, num_id, subtypes);
  4572. }
  4573. static int gas_request(struct wpa_supplicant *wpa_s, char *cmd)
  4574. {
  4575. u8 dst_addr[ETH_ALEN];
  4576. struct wpabuf *advproto, *query = NULL;
  4577. int used, ret = -1;
  4578. char *pos, *end;
  4579. size_t len;
  4580. used = hwaddr_aton2(cmd, dst_addr);
  4581. if (used < 0)
  4582. return -1;
  4583. pos = cmd + used;
  4584. while (*pos == ' ')
  4585. pos++;
  4586. /* Advertisement Protocol ID */
  4587. end = os_strchr(pos, ' ');
  4588. if (end)
  4589. len = end - pos;
  4590. else
  4591. len = os_strlen(pos);
  4592. if (len & 0x01)
  4593. return -1;
  4594. len /= 2;
  4595. if (len == 0)
  4596. return -1;
  4597. advproto = wpabuf_alloc(len);
  4598. if (advproto == NULL)
  4599. return -1;
  4600. if (hexstr2bin(pos, wpabuf_put(advproto, len), len) < 0)
  4601. goto fail;
  4602. if (end) {
  4603. /* Optional Query Request */
  4604. pos = end + 1;
  4605. while (*pos == ' ')
  4606. pos++;
  4607. len = os_strlen(pos);
  4608. if (len) {
  4609. if (len & 0x01)
  4610. goto fail;
  4611. len /= 2;
  4612. if (len == 0)
  4613. goto fail;
  4614. query = wpabuf_alloc(len);
  4615. if (query == NULL)
  4616. goto fail;
  4617. if (hexstr2bin(pos, wpabuf_put(query, len), len) < 0)
  4618. goto fail;
  4619. }
  4620. }
  4621. ret = gas_send_request(wpa_s, dst_addr, advproto, query);
  4622. fail:
  4623. wpabuf_free(advproto);
  4624. wpabuf_free(query);
  4625. return ret;
  4626. }
  4627. static int gas_response_get(struct wpa_supplicant *wpa_s, char *cmd, char *buf,
  4628. size_t buflen)
  4629. {
  4630. u8 addr[ETH_ALEN];
  4631. int dialog_token;
  4632. int used;
  4633. char *pos;
  4634. size_t resp_len, start, requested_len;
  4635. struct wpabuf *resp;
  4636. int ret;
  4637. used = hwaddr_aton2(cmd, addr);
  4638. if (used < 0)
  4639. return -1;
  4640. pos = cmd + used;
  4641. while (*pos == ' ')
  4642. pos++;
  4643. dialog_token = atoi(pos);
  4644. if (wpa_s->last_gas_resp &&
  4645. os_memcmp(addr, wpa_s->last_gas_addr, ETH_ALEN) == 0 &&
  4646. dialog_token == wpa_s->last_gas_dialog_token)
  4647. resp = wpa_s->last_gas_resp;
  4648. else if (wpa_s->prev_gas_resp &&
  4649. os_memcmp(addr, wpa_s->prev_gas_addr, ETH_ALEN) == 0 &&
  4650. dialog_token == wpa_s->prev_gas_dialog_token)
  4651. resp = wpa_s->prev_gas_resp;
  4652. else
  4653. return -1;
  4654. resp_len = wpabuf_len(resp);
  4655. start = 0;
  4656. requested_len = resp_len;
  4657. pos = os_strchr(pos, ' ');
  4658. if (pos) {
  4659. start = atoi(pos);
  4660. if (start > resp_len)
  4661. return os_snprintf(buf, buflen, "FAIL-Invalid range");
  4662. pos = os_strchr(pos, ',');
  4663. if (pos == NULL)
  4664. return -1;
  4665. pos++;
  4666. requested_len = atoi(pos);
  4667. if (start + requested_len > resp_len)
  4668. return os_snprintf(buf, buflen, "FAIL-Invalid range");
  4669. }
  4670. if (requested_len * 2 + 1 > buflen)
  4671. return os_snprintf(buf, buflen, "FAIL-Too long response");
  4672. ret = wpa_snprintf_hex(buf, buflen, wpabuf_head_u8(resp) + start,
  4673. requested_len);
  4674. if (start + requested_len == resp_len) {
  4675. /*
  4676. * Free memory by dropping the response after it has been
  4677. * fetched.
  4678. */
  4679. if (resp == wpa_s->prev_gas_resp) {
  4680. wpabuf_free(wpa_s->prev_gas_resp);
  4681. wpa_s->prev_gas_resp = NULL;
  4682. } else {
  4683. wpabuf_free(wpa_s->last_gas_resp);
  4684. wpa_s->last_gas_resp = NULL;
  4685. }
  4686. }
  4687. return ret;
  4688. }
  4689. #endif /* CONFIG_INTERWORKING */
  4690. #ifdef CONFIG_HS20
  4691. static int get_hs20_anqp(struct wpa_supplicant *wpa_s, char *dst)
  4692. {
  4693. u8 dst_addr[ETH_ALEN];
  4694. int used;
  4695. char *pos;
  4696. u32 subtypes = 0;
  4697. used = hwaddr_aton2(dst, dst_addr);
  4698. if (used < 0)
  4699. return -1;
  4700. pos = dst + used;
  4701. for (;;) {
  4702. int num = atoi(pos);
  4703. if (num <= 0 || num > 31)
  4704. return -1;
  4705. subtypes |= BIT(num);
  4706. pos = os_strchr(pos + 1, ',');
  4707. if (pos == NULL)
  4708. break;
  4709. pos++;
  4710. }
  4711. if (subtypes == 0)
  4712. return -1;
  4713. return hs20_anqp_send_req(wpa_s, dst_addr, subtypes, NULL, 0);
  4714. }
  4715. static int hs20_nai_home_realm_list(struct wpa_supplicant *wpa_s,
  4716. const u8 *addr, const char *realm)
  4717. {
  4718. u8 *buf;
  4719. size_t rlen, len;
  4720. int ret;
  4721. rlen = os_strlen(realm);
  4722. len = 3 + rlen;
  4723. buf = os_malloc(len);
  4724. if (buf == NULL)
  4725. return -1;
  4726. buf[0] = 1; /* NAI Home Realm Count */
  4727. buf[1] = 0; /* Formatted in accordance with RFC 4282 */
  4728. buf[2] = rlen;
  4729. os_memcpy(buf + 3, realm, rlen);
  4730. ret = hs20_anqp_send_req(wpa_s, addr,
  4731. BIT(HS20_STYPE_NAI_HOME_REALM_QUERY),
  4732. buf, len);
  4733. os_free(buf);
  4734. return ret;
  4735. }
  4736. static int hs20_get_nai_home_realm_list(struct wpa_supplicant *wpa_s,
  4737. char *dst)
  4738. {
  4739. struct wpa_cred *cred = wpa_s->conf->cred;
  4740. u8 dst_addr[ETH_ALEN];
  4741. int used;
  4742. u8 *buf;
  4743. size_t len;
  4744. int ret;
  4745. used = hwaddr_aton2(dst, dst_addr);
  4746. if (used < 0)
  4747. return -1;
  4748. while (dst[used] == ' ')
  4749. used++;
  4750. if (os_strncmp(dst + used, "realm=", 6) == 0)
  4751. return hs20_nai_home_realm_list(wpa_s, dst_addr,
  4752. dst + used + 6);
  4753. len = os_strlen(dst + used);
  4754. if (len == 0 && cred && cred->realm)
  4755. return hs20_nai_home_realm_list(wpa_s, dst_addr, cred->realm);
  4756. if (len & 1)
  4757. return -1;
  4758. len /= 2;
  4759. buf = os_malloc(len);
  4760. if (buf == NULL)
  4761. return -1;
  4762. if (hexstr2bin(dst + used, buf, len) < 0) {
  4763. os_free(buf);
  4764. return -1;
  4765. }
  4766. ret = hs20_anqp_send_req(wpa_s, dst_addr,
  4767. BIT(HS20_STYPE_NAI_HOME_REALM_QUERY),
  4768. buf, len);
  4769. os_free(buf);
  4770. return ret;
  4771. }
  4772. static int hs20_icon_request(struct wpa_supplicant *wpa_s, char *cmd)
  4773. {
  4774. u8 dst_addr[ETH_ALEN];
  4775. int used;
  4776. char *icon;
  4777. used = hwaddr_aton2(cmd, dst_addr);
  4778. if (used < 0)
  4779. return -1;
  4780. while (cmd[used] == ' ')
  4781. used++;
  4782. icon = &cmd[used];
  4783. wpa_s->fetch_osu_icon_in_progress = 0;
  4784. return hs20_anqp_send_req(wpa_s, dst_addr, BIT(HS20_STYPE_ICON_REQUEST),
  4785. (u8 *) icon, os_strlen(icon));
  4786. }
  4787. #endif /* CONFIG_HS20 */
  4788. static int wpa_supplicant_ctrl_iface_sta_autoconnect(
  4789. struct wpa_supplicant *wpa_s, char *cmd)
  4790. {
  4791. wpa_s->auto_reconnect_disabled = atoi(cmd) == 0 ? 1 : 0;
  4792. return 0;
  4793. }
  4794. #ifdef CONFIG_AUTOSCAN
  4795. static int wpa_supplicant_ctrl_iface_autoscan(struct wpa_supplicant *wpa_s,
  4796. char *cmd)
  4797. {
  4798. enum wpa_states state = wpa_s->wpa_state;
  4799. char *new_params = NULL;
  4800. if (os_strlen(cmd) > 0) {
  4801. new_params = os_strdup(cmd);
  4802. if (new_params == NULL)
  4803. return -1;
  4804. }
  4805. os_free(wpa_s->conf->autoscan);
  4806. wpa_s->conf->autoscan = new_params;
  4807. if (wpa_s->conf->autoscan == NULL)
  4808. autoscan_deinit(wpa_s);
  4809. else if (state == WPA_DISCONNECTED || state == WPA_INACTIVE)
  4810. autoscan_init(wpa_s, 1);
  4811. else if (state == WPA_SCANNING)
  4812. wpa_supplicant_reinit_autoscan(wpa_s);
  4813. return 0;
  4814. }
  4815. #endif /* CONFIG_AUTOSCAN */
  4816. #ifdef CONFIG_WNM
  4817. static int wpas_ctrl_iface_wnm_sleep(struct wpa_supplicant *wpa_s, char *cmd)
  4818. {
  4819. int enter;
  4820. int intval = 0;
  4821. char *pos;
  4822. int ret;
  4823. struct wpabuf *tfs_req = NULL;
  4824. if (os_strncmp(cmd, "enter", 5) == 0)
  4825. enter = 1;
  4826. else if (os_strncmp(cmd, "exit", 4) == 0)
  4827. enter = 0;
  4828. else
  4829. return -1;
  4830. pos = os_strstr(cmd, " interval=");
  4831. if (pos)
  4832. intval = atoi(pos + 10);
  4833. pos = os_strstr(cmd, " tfs_req=");
  4834. if (pos) {
  4835. char *end;
  4836. size_t len;
  4837. pos += 9;
  4838. end = os_strchr(pos, ' ');
  4839. if (end)
  4840. len = end - pos;
  4841. else
  4842. len = os_strlen(pos);
  4843. if (len & 1)
  4844. return -1;
  4845. len /= 2;
  4846. tfs_req = wpabuf_alloc(len);
  4847. if (tfs_req == NULL)
  4848. return -1;
  4849. if (hexstr2bin(pos, wpabuf_put(tfs_req, len), len) < 0) {
  4850. wpabuf_free(tfs_req);
  4851. return -1;
  4852. }
  4853. }
  4854. ret = ieee802_11_send_wnmsleep_req(wpa_s, enter ? WNM_SLEEP_MODE_ENTER :
  4855. WNM_SLEEP_MODE_EXIT, intval,
  4856. tfs_req);
  4857. wpabuf_free(tfs_req);
  4858. return ret;
  4859. }
  4860. static int wpas_ctrl_iface_wnm_bss_query(struct wpa_supplicant *wpa_s, char *cmd)
  4861. {
  4862. int query_reason;
  4863. query_reason = atoi(cmd);
  4864. wpa_printf(MSG_DEBUG, "CTRL_IFACE: WNM_BSS_QUERY query_reason=%d",
  4865. query_reason);
  4866. return wnm_send_bss_transition_mgmt_query(wpa_s, query_reason);
  4867. }
  4868. #endif /* CONFIG_WNM */
  4869. static int wpa_supplicant_signal_poll(struct wpa_supplicant *wpa_s, char *buf,
  4870. size_t buflen)
  4871. {
  4872. struct wpa_signal_info si;
  4873. int ret;
  4874. char *pos, *end;
  4875. ret = wpa_drv_signal_poll(wpa_s, &si);
  4876. if (ret)
  4877. return -1;
  4878. pos = buf;
  4879. end = buf + buflen;
  4880. ret = os_snprintf(pos, end - pos, "RSSI=%d\nLINKSPEED=%d\n"
  4881. "NOISE=%d\nFREQUENCY=%u\n",
  4882. si.current_signal, si.current_txrate / 1000,
  4883. si.current_noise, si.frequency);
  4884. if (os_snprintf_error(end - pos, ret))
  4885. return -1;
  4886. pos += ret;
  4887. if (si.chanwidth != CHAN_WIDTH_UNKNOWN) {
  4888. ret = os_snprintf(pos, end - pos, "WIDTH=%s\n",
  4889. channel_width_to_string(si.chanwidth));
  4890. if (os_snprintf_error(end - pos, ret))
  4891. return -1;
  4892. pos += ret;
  4893. }
  4894. if (si.center_frq1 > 0 && si.center_frq2 > 0) {
  4895. ret = os_snprintf(pos, end - pos,
  4896. "CENTER_FRQ1=%d\nCENTER_FRQ2=%d\n",
  4897. si.center_frq1, si.center_frq2);
  4898. if (os_snprintf_error(end - pos, ret))
  4899. return -1;
  4900. pos += ret;
  4901. }
  4902. if (si.avg_signal) {
  4903. ret = os_snprintf(pos, end - pos,
  4904. "AVG_RSSI=%d\n", si.avg_signal);
  4905. if (os_snprintf_error(end - pos, ret))
  4906. return -1;
  4907. pos += ret;
  4908. }
  4909. return pos - buf;
  4910. }
  4911. static int wpa_supplicant_pktcnt_poll(struct wpa_supplicant *wpa_s, char *buf,
  4912. size_t buflen)
  4913. {
  4914. struct hostap_sta_driver_data sta;
  4915. int ret;
  4916. ret = wpa_drv_pktcnt_poll(wpa_s, &sta);
  4917. if (ret)
  4918. return -1;
  4919. ret = os_snprintf(buf, buflen, "TXGOOD=%lu\nTXBAD=%lu\nRXGOOD=%lu\n",
  4920. sta.tx_packets, sta.tx_retry_failed, sta.rx_packets);
  4921. if (os_snprintf_error(buflen, ret))
  4922. return -1;
  4923. return ret;
  4924. }
  4925. #ifdef ANDROID
  4926. static int wpa_supplicant_driver_cmd(struct wpa_supplicant *wpa_s, char *cmd,
  4927. char *buf, size_t buflen)
  4928. {
  4929. int ret;
  4930. ret = wpa_drv_driver_cmd(wpa_s, cmd, buf, buflen);
  4931. if (ret == 0) {
  4932. if (os_strncasecmp(cmd, "COUNTRY", 7) == 0) {
  4933. struct p2p_data *p2p = wpa_s->global->p2p;
  4934. if (p2p) {
  4935. char country[3];
  4936. country[0] = cmd[8];
  4937. country[1] = cmd[9];
  4938. country[2] = 0x04;
  4939. p2p_set_country(p2p, country);
  4940. }
  4941. }
  4942. ret = os_snprintf(buf, buflen, "%s\n", "OK");
  4943. }
  4944. return ret;
  4945. }
  4946. #endif /* ANDROID */
  4947. static int wpa_supplicant_vendor_cmd(struct wpa_supplicant *wpa_s, char *cmd,
  4948. char *buf, size_t buflen)
  4949. {
  4950. int ret;
  4951. char *pos;
  4952. u8 *data = NULL;
  4953. unsigned int vendor_id, subcmd;
  4954. struct wpabuf *reply;
  4955. size_t data_len = 0;
  4956. /* cmd: <vendor id> <subcommand id> [<hex formatted data>] */
  4957. vendor_id = strtoul(cmd, &pos, 16);
  4958. if (!isblank(*pos))
  4959. return -EINVAL;
  4960. subcmd = strtoul(pos, &pos, 10);
  4961. if (*pos != '\0') {
  4962. if (!isblank(*pos++))
  4963. return -EINVAL;
  4964. data_len = os_strlen(pos);
  4965. }
  4966. if (data_len) {
  4967. data_len /= 2;
  4968. data = os_malloc(data_len);
  4969. if (!data)
  4970. return -1;
  4971. if (hexstr2bin(pos, data, data_len)) {
  4972. wpa_printf(MSG_DEBUG,
  4973. "Vendor command: wrong parameter format");
  4974. os_free(data);
  4975. return -EINVAL;
  4976. }
  4977. }
  4978. reply = wpabuf_alloc((buflen - 1) / 2);
  4979. if (!reply) {
  4980. os_free(data);
  4981. return -1;
  4982. }
  4983. ret = wpa_drv_vendor_cmd(wpa_s, vendor_id, subcmd, data, data_len,
  4984. reply);
  4985. if (ret == 0)
  4986. ret = wpa_snprintf_hex(buf, buflen, wpabuf_head_u8(reply),
  4987. wpabuf_len(reply));
  4988. wpabuf_free(reply);
  4989. os_free(data);
  4990. return ret;
  4991. }
  4992. static void wpa_supplicant_ctrl_iface_flush(struct wpa_supplicant *wpa_s)
  4993. {
  4994. wpa_dbg(wpa_s, MSG_DEBUG, "Flush all wpa_supplicant state");
  4995. #ifdef CONFIG_P2P
  4996. wpas_p2p_cancel(wpa_s);
  4997. wpas_p2p_stop_find(wpa_s);
  4998. p2p_ctrl_flush(wpa_s);
  4999. wpas_p2p_group_remove(wpa_s, "*");
  5000. wpas_p2p_service_flush(wpa_s);
  5001. wpa_s->global->p2p_disabled = 0;
  5002. wpa_s->global->p2p_per_sta_psk = 0;
  5003. wpa_s->conf->num_sec_device_types = 0;
  5004. wpa_s->p2p_disable_ip_addr_req = 0;
  5005. os_free(wpa_s->global->p2p_go_avoid_freq.range);
  5006. wpa_s->global->p2p_go_avoid_freq.range = NULL;
  5007. #endif /* CONFIG_P2P */
  5008. #ifdef CONFIG_WPS_TESTING
  5009. wps_version_number = 0x20;
  5010. wps_testing_dummy_cred = 0;
  5011. wps_corrupt_pkhash = 0;
  5012. #endif /* CONFIG_WPS_TESTING */
  5013. #ifdef CONFIG_WPS
  5014. wpa_s->wps_fragment_size = 0;
  5015. wpas_wps_cancel(wpa_s);
  5016. #endif /* CONFIG_WPS */
  5017. wpa_s->after_wps = 0;
  5018. wpa_s->known_wps_freq = 0;
  5019. #ifdef CONFIG_TDLS
  5020. #ifdef CONFIG_TDLS_TESTING
  5021. extern unsigned int tdls_testing;
  5022. tdls_testing = 0;
  5023. #endif /* CONFIG_TDLS_TESTING */
  5024. wpa_drv_tdls_oper(wpa_s, TDLS_ENABLE, NULL);
  5025. wpa_tdls_enable(wpa_s->wpa, 1);
  5026. #endif /* CONFIG_TDLS */
  5027. eloop_cancel_timeout(wpa_supplicant_stop_countermeasures, wpa_s, NULL);
  5028. wpa_supplicant_stop_countermeasures(wpa_s, NULL);
  5029. wpa_s->no_keep_alive = 0;
  5030. os_free(wpa_s->disallow_aps_bssid);
  5031. wpa_s->disallow_aps_bssid = NULL;
  5032. wpa_s->disallow_aps_bssid_count = 0;
  5033. os_free(wpa_s->disallow_aps_ssid);
  5034. wpa_s->disallow_aps_ssid = NULL;
  5035. wpa_s->disallow_aps_ssid_count = 0;
  5036. wpa_s->set_sta_uapsd = 0;
  5037. wpa_s->sta_uapsd = 0;
  5038. wpa_drv_radio_disable(wpa_s, 0);
  5039. wpa_bss_flush(wpa_s);
  5040. wpa_blacklist_clear(wpa_s);
  5041. wpa_s->extra_blacklist_count = 0;
  5042. wpa_supplicant_ctrl_iface_remove_network(wpa_s, "all");
  5043. wpa_supplicant_ctrl_iface_remove_cred(wpa_s, "all");
  5044. wpa_config_flush_blobs(wpa_s->conf);
  5045. wpa_s->conf->auto_interworking = 0;
  5046. wpa_s->conf->okc = 0;
  5047. wpa_sm_pmksa_cache_flush(wpa_s->wpa, NULL);
  5048. rsn_preauth_deinit(wpa_s->wpa);
  5049. wpa_sm_set_param(wpa_s->wpa, RSNA_PMK_LIFETIME, 43200);
  5050. wpa_sm_set_param(wpa_s->wpa, RSNA_PMK_REAUTH_THRESHOLD, 70);
  5051. wpa_sm_set_param(wpa_s->wpa, RSNA_SA_TIMEOUT, 60);
  5052. eapol_sm_notify_logoff(wpa_s->eapol, FALSE);
  5053. radio_remove_works(wpa_s, NULL, 1);
  5054. wpa_s->ext_work_in_progress = 0;
  5055. wpa_s->next_ssid = NULL;
  5056. #ifdef CONFIG_INTERWORKING
  5057. hs20_cancel_fetch_osu(wpa_s);
  5058. #endif /* CONFIG_INTERWORKING */
  5059. wpa_s->ext_mgmt_frame_handling = 0;
  5060. wpa_s->ext_eapol_frame_io = 0;
  5061. #ifdef CONFIG_TESTING_OPTIONS
  5062. wpa_s->extra_roc_dur = 0;
  5063. #endif /* CONFIG_TESTING_OPTIONS */
  5064. }
  5065. static int wpas_ctrl_radio_work_show(struct wpa_supplicant *wpa_s,
  5066. char *buf, size_t buflen)
  5067. {
  5068. struct wpa_radio_work *work;
  5069. char *pos, *end;
  5070. struct os_reltime now, diff;
  5071. pos = buf;
  5072. end = buf + buflen;
  5073. os_get_reltime(&now);
  5074. dl_list_for_each(work, &wpa_s->radio->work, struct wpa_radio_work, list)
  5075. {
  5076. int ret;
  5077. os_reltime_sub(&now, &work->time, &diff);
  5078. ret = os_snprintf(pos, end - pos, "%s@%s:%u:%u:%ld.%06ld\n",
  5079. work->type, work->wpa_s->ifname, work->freq,
  5080. work->started, diff.sec, diff.usec);
  5081. if (os_snprintf_error(end - pos, ret))
  5082. break;
  5083. pos += ret;
  5084. }
  5085. return pos - buf;
  5086. }
  5087. static void wpas_ctrl_radio_work_timeout(void *eloop_ctx, void *timeout_ctx)
  5088. {
  5089. struct wpa_radio_work *work = eloop_ctx;
  5090. struct wpa_external_work *ework = work->ctx;
  5091. wpa_dbg(work->wpa_s, MSG_DEBUG,
  5092. "Timing out external radio work %u (%s)",
  5093. ework->id, work->type);
  5094. wpa_msg(work->wpa_s, MSG_INFO, EXT_RADIO_WORK_TIMEOUT "%u", ework->id);
  5095. work->wpa_s->ext_work_in_progress = 0;
  5096. radio_work_done(work);
  5097. os_free(ework);
  5098. }
  5099. static void wpas_ctrl_radio_work_cb(struct wpa_radio_work *work, int deinit)
  5100. {
  5101. struct wpa_external_work *ework = work->ctx;
  5102. if (deinit) {
  5103. if (work->started)
  5104. eloop_cancel_timeout(wpas_ctrl_radio_work_timeout,
  5105. work, NULL);
  5106. os_free(ework);
  5107. return;
  5108. }
  5109. wpa_dbg(work->wpa_s, MSG_DEBUG, "Starting external radio work %u (%s)",
  5110. ework->id, ework->type);
  5111. wpa_msg(work->wpa_s, MSG_INFO, EXT_RADIO_WORK_START "%u", ework->id);
  5112. work->wpa_s->ext_work_in_progress = 1;
  5113. if (!ework->timeout)
  5114. ework->timeout = 10;
  5115. eloop_register_timeout(ework->timeout, 0, wpas_ctrl_radio_work_timeout,
  5116. work, NULL);
  5117. }
  5118. static int wpas_ctrl_radio_work_add(struct wpa_supplicant *wpa_s, char *cmd,
  5119. char *buf, size_t buflen)
  5120. {
  5121. struct wpa_external_work *ework;
  5122. char *pos, *pos2;
  5123. size_t type_len;
  5124. int ret;
  5125. unsigned int freq = 0;
  5126. /* format: <name> [freq=<MHz>] [timeout=<seconds>] */
  5127. ework = os_zalloc(sizeof(*ework));
  5128. if (ework == NULL)
  5129. return -1;
  5130. pos = os_strchr(cmd, ' ');
  5131. if (pos) {
  5132. type_len = pos - cmd;
  5133. pos++;
  5134. pos2 = os_strstr(pos, "freq=");
  5135. if (pos2)
  5136. freq = atoi(pos2 + 5);
  5137. pos2 = os_strstr(pos, "timeout=");
  5138. if (pos2)
  5139. ework->timeout = atoi(pos2 + 8);
  5140. } else {
  5141. type_len = os_strlen(cmd);
  5142. }
  5143. if (4 + type_len >= sizeof(ework->type))
  5144. type_len = sizeof(ework->type) - 4 - 1;
  5145. os_strlcpy(ework->type, "ext:", sizeof(ework->type));
  5146. os_memcpy(ework->type + 4, cmd, type_len);
  5147. ework->type[4 + type_len] = '\0';
  5148. wpa_s->ext_work_id++;
  5149. if (wpa_s->ext_work_id == 0)
  5150. wpa_s->ext_work_id++;
  5151. ework->id = wpa_s->ext_work_id;
  5152. if (radio_add_work(wpa_s, freq, ework->type, 0, wpas_ctrl_radio_work_cb,
  5153. ework) < 0) {
  5154. os_free(ework);
  5155. return -1;
  5156. }
  5157. ret = os_snprintf(buf, buflen, "%u", ework->id);
  5158. if (os_snprintf_error(buflen, ret))
  5159. return -1;
  5160. return ret;
  5161. }
  5162. static int wpas_ctrl_radio_work_done(struct wpa_supplicant *wpa_s, char *cmd)
  5163. {
  5164. struct wpa_radio_work *work;
  5165. unsigned int id = atoi(cmd);
  5166. dl_list_for_each(work, &wpa_s->radio->work, struct wpa_radio_work, list)
  5167. {
  5168. struct wpa_external_work *ework;
  5169. if (os_strncmp(work->type, "ext:", 4) != 0)
  5170. continue;
  5171. ework = work->ctx;
  5172. if (id && ework->id != id)
  5173. continue;
  5174. wpa_dbg(wpa_s, MSG_DEBUG,
  5175. "Completed external radio work %u (%s)",
  5176. ework->id, ework->type);
  5177. eloop_cancel_timeout(wpas_ctrl_radio_work_timeout, work, NULL);
  5178. wpa_s->ext_work_in_progress = 0;
  5179. radio_work_done(work);
  5180. os_free(ework);
  5181. return 3; /* "OK\n" */
  5182. }
  5183. return -1;
  5184. }
  5185. static int wpas_ctrl_radio_work(struct wpa_supplicant *wpa_s, char *cmd,
  5186. char *buf, size_t buflen)
  5187. {
  5188. if (os_strcmp(cmd, "show") == 0)
  5189. return wpas_ctrl_radio_work_show(wpa_s, buf, buflen);
  5190. if (os_strncmp(cmd, "add ", 4) == 0)
  5191. return wpas_ctrl_radio_work_add(wpa_s, cmd + 4, buf, buflen);
  5192. if (os_strncmp(cmd, "done ", 5) == 0)
  5193. return wpas_ctrl_radio_work_done(wpa_s, cmd + 4);
  5194. return -1;
  5195. }
  5196. void wpas_ctrl_radio_work_flush(struct wpa_supplicant *wpa_s)
  5197. {
  5198. struct wpa_radio_work *work, *tmp;
  5199. if (!wpa_s || !wpa_s->radio)
  5200. return;
  5201. dl_list_for_each_safe(work, tmp, &wpa_s->radio->work,
  5202. struct wpa_radio_work, list) {
  5203. struct wpa_external_work *ework;
  5204. if (os_strncmp(work->type, "ext:", 4) != 0)
  5205. continue;
  5206. ework = work->ctx;
  5207. wpa_dbg(wpa_s, MSG_DEBUG,
  5208. "Flushing%s external radio work %u (%s)",
  5209. work->started ? " started" : "", ework->id,
  5210. ework->type);
  5211. if (work->started)
  5212. eloop_cancel_timeout(wpas_ctrl_radio_work_timeout,
  5213. work, NULL);
  5214. radio_work_done(work);
  5215. os_free(ework);
  5216. }
  5217. }
  5218. static void wpas_ctrl_eapol_response(void *eloop_ctx, void *timeout_ctx)
  5219. {
  5220. struct wpa_supplicant *wpa_s = eloop_ctx;
  5221. eapol_sm_notify_ctrl_response(wpa_s->eapol);
  5222. }
  5223. static int set_scan_freqs(struct wpa_supplicant *wpa_s, char *val)
  5224. {
  5225. int *freqs = NULL;
  5226. freqs = freq_range_to_channel_list(wpa_s, val);
  5227. if (freqs == NULL)
  5228. return -1;
  5229. os_free(wpa_s->manual_scan_freqs);
  5230. wpa_s->manual_scan_freqs = freqs;
  5231. return 0;
  5232. }
  5233. static int scan_id_list_parse(struct wpa_supplicant *wpa_s, const char *value)
  5234. {
  5235. const char *pos = value;
  5236. while (pos) {
  5237. if (*pos == ' ' || *pos == '\0')
  5238. break;
  5239. if (wpa_s->scan_id_count == MAX_SCAN_ID)
  5240. return -1;
  5241. wpa_s->scan_id[wpa_s->scan_id_count++] = atoi(pos);
  5242. pos = os_strchr(pos, ',');
  5243. if (pos)
  5244. pos++;
  5245. }
  5246. return 0;
  5247. }
  5248. static void wpas_ctrl_scan(struct wpa_supplicant *wpa_s, char *params,
  5249. char *reply, int reply_size, int *reply_len)
  5250. {
  5251. char *pos;
  5252. if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED) {
  5253. *reply_len = -1;
  5254. return;
  5255. }
  5256. wpa_s->manual_scan_passive = 0;
  5257. wpa_s->manual_scan_use_id = 0;
  5258. wpa_s->manual_scan_only_new = 0;
  5259. wpa_s->scan_id_count = 0;
  5260. if (params) {
  5261. if (os_strncasecmp(params, "TYPE=ONLY", 9) == 0)
  5262. wpa_s->scan_res_handler = scan_only_handler;
  5263. pos = os_strstr(params, "freq=");
  5264. if (pos && set_scan_freqs(wpa_s, pos + 5) < 0) {
  5265. *reply_len = -1;
  5266. return;
  5267. }
  5268. pos = os_strstr(params, "passive=");
  5269. if (pos)
  5270. wpa_s->manual_scan_passive = !!atoi(pos + 8);
  5271. pos = os_strstr(params, "use_id=");
  5272. if (pos)
  5273. wpa_s->manual_scan_use_id = atoi(pos + 7);
  5274. pos = os_strstr(params, "only_new=1");
  5275. if (pos)
  5276. wpa_s->manual_scan_only_new = 1;
  5277. pos = os_strstr(params, "scan_id=");
  5278. if (pos && scan_id_list_parse(wpa_s, pos + 8) < 0) {
  5279. *reply_len = -1;
  5280. return;
  5281. }
  5282. } else {
  5283. os_free(wpa_s->manual_scan_freqs);
  5284. wpa_s->manual_scan_freqs = NULL;
  5285. if (wpa_s->scan_res_handler == scan_only_handler)
  5286. wpa_s->scan_res_handler = NULL;
  5287. }
  5288. if (!wpa_s->sched_scanning && !wpa_s->scanning &&
  5289. ((wpa_s->wpa_state <= WPA_SCANNING) ||
  5290. (wpa_s->wpa_state == WPA_COMPLETED))) {
  5291. wpa_s->normal_scans = 0;
  5292. wpa_s->scan_req = MANUAL_SCAN_REQ;
  5293. wpa_s->after_wps = 0;
  5294. wpa_s->known_wps_freq = 0;
  5295. wpa_supplicant_req_scan(wpa_s, 0, 0);
  5296. if (wpa_s->manual_scan_use_id) {
  5297. wpa_s->manual_scan_id++;
  5298. wpa_dbg(wpa_s, MSG_DEBUG, "Assigned scan id %u",
  5299. wpa_s->manual_scan_id);
  5300. *reply_len = os_snprintf(reply, reply_size, "%u\n",
  5301. wpa_s->manual_scan_id);
  5302. }
  5303. } else if (wpa_s->sched_scanning) {
  5304. wpa_printf(MSG_DEBUG, "Stop ongoing sched_scan to allow requested full scan to proceed");
  5305. wpa_supplicant_cancel_sched_scan(wpa_s);
  5306. wpa_s->scan_req = MANUAL_SCAN_REQ;
  5307. wpa_supplicant_req_scan(wpa_s, 0, 0);
  5308. if (wpa_s->manual_scan_use_id) {
  5309. wpa_s->manual_scan_id++;
  5310. *reply_len = os_snprintf(reply, reply_size, "%u\n",
  5311. wpa_s->manual_scan_id);
  5312. wpa_dbg(wpa_s, MSG_DEBUG, "Assigned scan id %u",
  5313. wpa_s->manual_scan_id);
  5314. }
  5315. } else {
  5316. wpa_printf(MSG_DEBUG, "Ongoing scan action - reject new request");
  5317. *reply_len = os_snprintf(reply, reply_size, "FAIL-BUSY\n");
  5318. }
  5319. }
  5320. #ifdef CONFIG_TESTING_OPTIONS
  5321. static void wpas_ctrl_iface_mgmt_tx_cb(struct wpa_supplicant *wpa_s,
  5322. unsigned int freq, const u8 *dst,
  5323. const u8 *src, const u8 *bssid,
  5324. const u8 *data, size_t data_len,
  5325. enum offchannel_send_action_result
  5326. result)
  5327. {
  5328. wpa_msg(wpa_s, MSG_INFO, "MGMT-TX-STATUS freq=%u dst=" MACSTR
  5329. " src=" MACSTR " bssid=" MACSTR " result=%s",
  5330. freq, MAC2STR(dst), MAC2STR(src), MAC2STR(bssid),
  5331. result == OFFCHANNEL_SEND_ACTION_SUCCESS ?
  5332. "SUCCESS" : (result == OFFCHANNEL_SEND_ACTION_NO_ACK ?
  5333. "NO_ACK" : "FAILED"));
  5334. }
  5335. static int wpas_ctrl_iface_mgmt_tx(struct wpa_supplicant *wpa_s, char *cmd)
  5336. {
  5337. char *pos, *param;
  5338. size_t len;
  5339. u8 *buf, da[ETH_ALEN], bssid[ETH_ALEN];
  5340. int res, used;
  5341. int freq = 0, no_cck = 0, wait_time = 0;
  5342. /* <DA> <BSSID> [freq=<MHz>] [wait_time=<ms>] [no_cck=1]
  5343. * <action=Action frame payload> */
  5344. wpa_printf(MSG_DEBUG, "External MGMT TX: %s", cmd);
  5345. pos = cmd;
  5346. used = hwaddr_aton2(pos, da);
  5347. if (used < 0)
  5348. return -1;
  5349. pos += used;
  5350. while (*pos == ' ')
  5351. pos++;
  5352. used = hwaddr_aton2(pos, bssid);
  5353. if (used < 0)
  5354. return -1;
  5355. pos += used;
  5356. param = os_strstr(pos, " freq=");
  5357. if (param) {
  5358. param += 6;
  5359. freq = atoi(param);
  5360. }
  5361. param = os_strstr(pos, " no_cck=");
  5362. if (param) {
  5363. param += 8;
  5364. no_cck = atoi(param);
  5365. }
  5366. param = os_strstr(pos, " wait_time=");
  5367. if (param) {
  5368. param += 11;
  5369. wait_time = atoi(param);
  5370. }
  5371. param = os_strstr(pos, " action=");
  5372. if (param == NULL)
  5373. return -1;
  5374. param += 8;
  5375. len = os_strlen(param);
  5376. if (len & 1)
  5377. return -1;
  5378. len /= 2;
  5379. buf = os_malloc(len);
  5380. if (buf == NULL)
  5381. return -1;
  5382. if (hexstr2bin(param, buf, len) < 0) {
  5383. os_free(buf);
  5384. return -1;
  5385. }
  5386. res = offchannel_send_action(wpa_s, freq, da, wpa_s->own_addr, bssid,
  5387. buf, len, wait_time,
  5388. wpas_ctrl_iface_mgmt_tx_cb, no_cck);
  5389. os_free(buf);
  5390. return res;
  5391. }
  5392. static void wpas_ctrl_iface_mgmt_tx_done(struct wpa_supplicant *wpa_s)
  5393. {
  5394. wpa_printf(MSG_DEBUG, "External MGMT TX - done waiting");
  5395. offchannel_send_action_done(wpa_s);
  5396. }
  5397. static int wpas_ctrl_iface_driver_event(struct wpa_supplicant *wpa_s, char *cmd)
  5398. {
  5399. char *pos, *param;
  5400. union wpa_event_data event;
  5401. enum wpa_event_type ev;
  5402. /* <event name> [parameters..] */
  5403. wpa_dbg(wpa_s, MSG_DEBUG, "Testing - external driver event: %s", cmd);
  5404. pos = cmd;
  5405. param = os_strchr(pos, ' ');
  5406. if (param)
  5407. *param++ = '\0';
  5408. os_memset(&event, 0, sizeof(event));
  5409. if (os_strcmp(cmd, "INTERFACE_ENABLED") == 0) {
  5410. ev = EVENT_INTERFACE_ENABLED;
  5411. } else if (os_strcmp(cmd, "INTERFACE_DISABLED") == 0) {
  5412. ev = EVENT_INTERFACE_DISABLED;
  5413. } else if (os_strcmp(cmd, "AVOID_FREQUENCIES") == 0) {
  5414. ev = EVENT_AVOID_FREQUENCIES;
  5415. if (param == NULL)
  5416. param = "";
  5417. if (freq_range_list_parse(&event.freq_range, param) < 0)
  5418. return -1;
  5419. wpa_supplicant_event(wpa_s, ev, &event);
  5420. os_free(event.freq_range.range);
  5421. return 0;
  5422. } else {
  5423. wpa_dbg(wpa_s, MSG_DEBUG, "Testing - unknown driver event: %s",
  5424. cmd);
  5425. return -1;
  5426. }
  5427. wpa_supplicant_event(wpa_s, ev, &event);
  5428. return 0;
  5429. }
  5430. static int wpas_ctrl_iface_eapol_rx(struct wpa_supplicant *wpa_s, char *cmd)
  5431. {
  5432. char *pos;
  5433. u8 src[ETH_ALEN], *buf;
  5434. int used;
  5435. size_t len;
  5436. wpa_printf(MSG_DEBUG, "External EAPOL RX: %s", cmd);
  5437. pos = cmd;
  5438. used = hwaddr_aton2(pos, src);
  5439. if (used < 0)
  5440. return -1;
  5441. pos += used;
  5442. while (*pos == ' ')
  5443. pos++;
  5444. len = os_strlen(pos);
  5445. if (len & 1)
  5446. return -1;
  5447. len /= 2;
  5448. buf = os_malloc(len);
  5449. if (buf == NULL)
  5450. return -1;
  5451. if (hexstr2bin(pos, buf, len) < 0) {
  5452. os_free(buf);
  5453. return -1;
  5454. }
  5455. wpa_supplicant_rx_eapol(wpa_s, src, buf, len);
  5456. os_free(buf);
  5457. return 0;
  5458. }
  5459. static u16 ipv4_hdr_checksum(const void *buf, size_t len)
  5460. {
  5461. size_t i;
  5462. u32 sum = 0;
  5463. const u16 *pos = buf;
  5464. for (i = 0; i < len / 2; i++)
  5465. sum += *pos++;
  5466. while (sum >> 16)
  5467. sum = (sum & 0xffff) + (sum >> 16);
  5468. return sum ^ 0xffff;
  5469. }
  5470. #define HWSIM_PACKETLEN 1500
  5471. #define HWSIM_IP_LEN (HWSIM_PACKETLEN - sizeof(struct ether_header))
  5472. void wpas_data_test_rx(void *ctx, const u8 *src_addr, const u8 *buf, size_t len)
  5473. {
  5474. struct wpa_supplicant *wpa_s = ctx;
  5475. const struct ether_header *eth;
  5476. const struct iphdr *ip;
  5477. const u8 *pos;
  5478. unsigned int i;
  5479. if (len != HWSIM_PACKETLEN)
  5480. return;
  5481. eth = (const struct ether_header *) buf;
  5482. ip = (const struct iphdr *) (eth + 1);
  5483. pos = (const u8 *) (ip + 1);
  5484. if (ip->ihl != 5 || ip->version != 4 ||
  5485. ntohs(ip->tot_len) != HWSIM_IP_LEN)
  5486. return;
  5487. for (i = 0; i < HWSIM_IP_LEN - sizeof(*ip); i++) {
  5488. if (*pos != (u8) i)
  5489. return;
  5490. pos++;
  5491. }
  5492. wpa_msg(wpa_s, MSG_INFO, "DATA-TEST-RX " MACSTR " " MACSTR,
  5493. MAC2STR(eth->ether_dhost), MAC2STR(eth->ether_shost));
  5494. }
  5495. static int wpas_ctrl_iface_data_test_config(struct wpa_supplicant *wpa_s,
  5496. char *cmd)
  5497. {
  5498. int enabled = atoi(cmd);
  5499. if (!enabled) {
  5500. if (wpa_s->l2_test) {
  5501. l2_packet_deinit(wpa_s->l2_test);
  5502. wpa_s->l2_test = NULL;
  5503. wpa_dbg(wpa_s, MSG_DEBUG, "test data: Disabled");
  5504. }
  5505. return 0;
  5506. }
  5507. if (wpa_s->l2_test)
  5508. return 0;
  5509. wpa_s->l2_test = l2_packet_init(wpa_s->ifname, wpa_s->own_addr,
  5510. ETHERTYPE_IP, wpas_data_test_rx,
  5511. wpa_s, 1);
  5512. if (wpa_s->l2_test == NULL)
  5513. return -1;
  5514. wpa_dbg(wpa_s, MSG_DEBUG, "test data: Enabled");
  5515. return 0;
  5516. }
  5517. static int wpas_ctrl_iface_data_test_tx(struct wpa_supplicant *wpa_s, char *cmd)
  5518. {
  5519. u8 dst[ETH_ALEN], src[ETH_ALEN];
  5520. char *pos;
  5521. int used;
  5522. long int val;
  5523. u8 tos;
  5524. u8 buf[HWSIM_PACKETLEN];
  5525. struct ether_header *eth;
  5526. struct iphdr *ip;
  5527. u8 *dpos;
  5528. unsigned int i;
  5529. if (wpa_s->l2_test == NULL)
  5530. return -1;
  5531. /* format: <dst> <src> <tos> */
  5532. pos = cmd;
  5533. used = hwaddr_aton2(pos, dst);
  5534. if (used < 0)
  5535. return -1;
  5536. pos += used;
  5537. while (*pos == ' ')
  5538. pos++;
  5539. used = hwaddr_aton2(pos, src);
  5540. if (used < 0)
  5541. return -1;
  5542. pos += used;
  5543. val = strtol(pos, NULL, 0);
  5544. if (val < 0 || val > 0xff)
  5545. return -1;
  5546. tos = val;
  5547. eth = (struct ether_header *) buf;
  5548. os_memcpy(eth->ether_dhost, dst, ETH_ALEN);
  5549. os_memcpy(eth->ether_shost, src, ETH_ALEN);
  5550. eth->ether_type = htons(ETHERTYPE_IP);
  5551. ip = (struct iphdr *) (eth + 1);
  5552. os_memset(ip, 0, sizeof(*ip));
  5553. ip->ihl = 5;
  5554. ip->version = 4;
  5555. ip->ttl = 64;
  5556. ip->tos = tos;
  5557. ip->tot_len = htons(HWSIM_IP_LEN);
  5558. ip->protocol = 1;
  5559. ip->saddr = htonl(192 << 24 | 168 << 16 | 1 << 8 | 1);
  5560. ip->daddr = htonl(192 << 24 | 168 << 16 | 1 << 8 | 2);
  5561. ip->check = ipv4_hdr_checksum(ip, sizeof(*ip));
  5562. dpos = (u8 *) (ip + 1);
  5563. for (i = 0; i < HWSIM_IP_LEN - sizeof(*ip); i++)
  5564. *dpos++ = i;
  5565. if (l2_packet_send(wpa_s->l2_test, dst, ETHERTYPE_IP, buf,
  5566. HWSIM_PACKETLEN) < 0)
  5567. return -1;
  5568. wpa_dbg(wpa_s, MSG_DEBUG, "test data: TX dst=" MACSTR " src=" MACSTR
  5569. " tos=0x%x", MAC2STR(dst), MAC2STR(src), tos);
  5570. return 0;
  5571. }
  5572. static int wpas_ctrl_iface_data_test_frame(struct wpa_supplicant *wpa_s,
  5573. char *cmd)
  5574. {
  5575. u8 *buf;
  5576. struct ether_header *eth;
  5577. struct l2_packet_data *l2 = NULL;
  5578. size_t len;
  5579. u16 ethertype;
  5580. int res = -1;
  5581. len = os_strlen(cmd);
  5582. if (len & 1 || len < ETH_HLEN * 2)
  5583. return -1;
  5584. len /= 2;
  5585. buf = os_malloc(len);
  5586. if (buf == NULL)
  5587. return -1;
  5588. if (hexstr2bin(cmd, buf, len) < 0)
  5589. goto done;
  5590. eth = (struct ether_header *) buf;
  5591. ethertype = ntohs(eth->ether_type);
  5592. l2 = l2_packet_init(wpa_s->ifname, wpa_s->own_addr, ethertype,
  5593. wpas_data_test_rx, wpa_s, 1);
  5594. if (l2 == NULL)
  5595. goto done;
  5596. res = l2_packet_send(l2, eth->ether_dhost, ethertype, buf, len);
  5597. wpa_dbg(wpa_s, MSG_DEBUG, "test data: TX frame res=%d", res);
  5598. done:
  5599. if (l2)
  5600. l2_packet_deinit(l2);
  5601. os_free(buf);
  5602. return res < 0 ? -1 : 0;
  5603. }
  5604. #endif /* CONFIG_TESTING_OPTIONS */
  5605. static void wpas_ctrl_vendor_elem_update(struct wpa_supplicant *wpa_s)
  5606. {
  5607. unsigned int i;
  5608. char buf[30];
  5609. wpa_printf(MSG_DEBUG, "Update vendor elements");
  5610. for (i = 0; i < NUM_VENDOR_ELEM_FRAMES; i++) {
  5611. if (wpa_s->vendor_elem[i]) {
  5612. os_snprintf(buf, sizeof(buf), "frame[%u]", i);
  5613. wpa_hexdump_buf(MSG_DEBUG, buf, wpa_s->vendor_elem[i]);
  5614. }
  5615. }
  5616. #ifdef CONFIG_P2P
  5617. if (wpa_s->parent == wpa_s &&
  5618. wpa_s->global->p2p &&
  5619. !wpa_s->global->p2p_disabled)
  5620. p2p_set_vendor_elems(wpa_s->global->p2p, wpa_s->vendor_elem);
  5621. #endif /* CONFIG_P2P */
  5622. }
  5623. static struct wpa_supplicant *
  5624. wpas_ctrl_vendor_elem_iface(struct wpa_supplicant *wpa_s,
  5625. enum wpa_vendor_elem_frame frame)
  5626. {
  5627. switch (frame) {
  5628. #ifdef CONFIG_P2P
  5629. case VENDOR_ELEM_PROBE_REQ_P2P:
  5630. case VENDOR_ELEM_PROBE_RESP_P2P:
  5631. case VENDOR_ELEM_PROBE_RESP_P2P_GO:
  5632. case VENDOR_ELEM_BEACON_P2P_GO:
  5633. case VENDOR_ELEM_P2P_PD_REQ:
  5634. case VENDOR_ELEM_P2P_PD_RESP:
  5635. case VENDOR_ELEM_P2P_GO_NEG_REQ:
  5636. case VENDOR_ELEM_P2P_GO_NEG_RESP:
  5637. case VENDOR_ELEM_P2P_GO_NEG_CONF:
  5638. case VENDOR_ELEM_P2P_INV_REQ:
  5639. case VENDOR_ELEM_P2P_INV_RESP:
  5640. case VENDOR_ELEM_P2P_ASSOC_REQ:
  5641. return wpa_s->parent;
  5642. #endif /* CONFIG_P2P */
  5643. default:
  5644. return wpa_s;
  5645. }
  5646. }
  5647. static int wpas_ctrl_vendor_elem_add(struct wpa_supplicant *wpa_s, char *cmd)
  5648. {
  5649. char *pos = cmd;
  5650. int frame;
  5651. size_t len;
  5652. struct wpabuf *buf;
  5653. struct ieee802_11_elems elems;
  5654. frame = atoi(pos);
  5655. if (frame < 0 || frame >= NUM_VENDOR_ELEM_FRAMES)
  5656. return -1;
  5657. wpa_s = wpas_ctrl_vendor_elem_iface(wpa_s, frame);
  5658. pos = os_strchr(pos, ' ');
  5659. if (pos == NULL)
  5660. return -1;
  5661. pos++;
  5662. len = os_strlen(pos);
  5663. if (len == 0)
  5664. return 0;
  5665. if (len & 1)
  5666. return -1;
  5667. len /= 2;
  5668. buf = wpabuf_alloc(len);
  5669. if (buf == NULL)
  5670. return -1;
  5671. if (hexstr2bin(pos, wpabuf_put(buf, len), len) < 0) {
  5672. wpabuf_free(buf);
  5673. return -1;
  5674. }
  5675. if (ieee802_11_parse_elems(wpabuf_head_u8(buf), len, &elems, 0) ==
  5676. ParseFailed) {
  5677. wpabuf_free(buf);
  5678. return -1;
  5679. }
  5680. if (wpa_s->vendor_elem[frame] == NULL) {
  5681. wpa_s->vendor_elem[frame] = buf;
  5682. wpas_ctrl_vendor_elem_update(wpa_s);
  5683. return 0;
  5684. }
  5685. if (wpabuf_resize(&wpa_s->vendor_elem[frame], len) < 0) {
  5686. wpabuf_free(buf);
  5687. return -1;
  5688. }
  5689. wpabuf_put_buf(wpa_s->vendor_elem[frame], buf);
  5690. wpabuf_free(buf);
  5691. wpas_ctrl_vendor_elem_update(wpa_s);
  5692. return 0;
  5693. }
  5694. static int wpas_ctrl_vendor_elem_get(struct wpa_supplicant *wpa_s, char *cmd,
  5695. char *buf, size_t buflen)
  5696. {
  5697. int frame = atoi(cmd);
  5698. if (frame < 0 || frame >= NUM_VENDOR_ELEM_FRAMES)
  5699. return -1;
  5700. wpa_s = wpas_ctrl_vendor_elem_iface(wpa_s, frame);
  5701. if (wpa_s->vendor_elem[frame] == NULL)
  5702. return 0;
  5703. return wpa_snprintf_hex(buf, buflen,
  5704. wpabuf_head_u8(wpa_s->vendor_elem[frame]),
  5705. wpabuf_len(wpa_s->vendor_elem[frame]));
  5706. }
  5707. static int wpas_ctrl_vendor_elem_remove(struct wpa_supplicant *wpa_s, char *cmd)
  5708. {
  5709. char *pos = cmd;
  5710. int frame;
  5711. size_t len;
  5712. u8 *buf;
  5713. struct ieee802_11_elems elems;
  5714. u8 *ie, *end;
  5715. frame = atoi(pos);
  5716. if (frame < 0 || frame >= NUM_VENDOR_ELEM_FRAMES)
  5717. return -1;
  5718. wpa_s = wpas_ctrl_vendor_elem_iface(wpa_s, frame);
  5719. pos = os_strchr(pos, ' ');
  5720. if (pos == NULL)
  5721. return -1;
  5722. pos++;
  5723. if (*pos == '*') {
  5724. wpabuf_free(wpa_s->vendor_elem[frame]);
  5725. wpa_s->vendor_elem[frame] = NULL;
  5726. wpas_ctrl_vendor_elem_update(wpa_s);
  5727. return 0;
  5728. }
  5729. if (wpa_s->vendor_elem[frame] == NULL)
  5730. return -1;
  5731. len = os_strlen(pos);
  5732. if (len == 0)
  5733. return 0;
  5734. if (len & 1)
  5735. return -1;
  5736. len /= 2;
  5737. buf = os_malloc(len);
  5738. if (buf == NULL)
  5739. return -1;
  5740. if (hexstr2bin(pos, buf, len) < 0) {
  5741. os_free(buf);
  5742. return -1;
  5743. }
  5744. if (ieee802_11_parse_elems(buf, len, &elems, 0) == ParseFailed) {
  5745. os_free(buf);
  5746. return -1;
  5747. }
  5748. ie = wpabuf_mhead_u8(wpa_s->vendor_elem[frame]);
  5749. end = ie + wpabuf_len(wpa_s->vendor_elem[frame]);
  5750. for (; ie + 1 < end; ie += 2 + ie[1]) {
  5751. if (ie + len > end)
  5752. break;
  5753. if (os_memcmp(ie, buf, len) != 0)
  5754. continue;
  5755. if (wpabuf_len(wpa_s->vendor_elem[frame]) == len) {
  5756. wpabuf_free(wpa_s->vendor_elem[frame]);
  5757. wpa_s->vendor_elem[frame] = NULL;
  5758. } else {
  5759. os_memmove(ie, ie + len,
  5760. end - (ie + len));
  5761. wpa_s->vendor_elem[frame]->used -= len;
  5762. }
  5763. os_free(buf);
  5764. wpas_ctrl_vendor_elem_update(wpa_s);
  5765. return 0;
  5766. }
  5767. os_free(buf);
  5768. return -1;
  5769. }
  5770. static void wpas_ctrl_neighbor_rep_cb(void *ctx, struct wpabuf *neighbor_rep)
  5771. {
  5772. struct wpa_supplicant *wpa_s = ctx;
  5773. if (neighbor_rep) {
  5774. wpa_msg_ctrl(wpa_s, MSG_INFO, RRM_EVENT_NEIGHBOR_REP_RXED
  5775. "length=%u",
  5776. (unsigned int) wpabuf_len(neighbor_rep));
  5777. wpabuf_free(neighbor_rep);
  5778. } else {
  5779. wpa_msg_ctrl(wpa_s, MSG_INFO, RRM_EVENT_NEIGHBOR_REP_FAILED);
  5780. }
  5781. }
  5782. static int wpas_ctrl_iface_send_neigbor_rep(struct wpa_supplicant *wpa_s,
  5783. char *cmd)
  5784. {
  5785. struct wpa_ssid ssid;
  5786. struct wpa_ssid *ssid_p = NULL;
  5787. int ret = 0;
  5788. if (os_strncmp(cmd, " ssid=", 6) == 0) {
  5789. ssid.ssid_len = os_strlen(cmd + 6);
  5790. if (ssid.ssid_len > 32)
  5791. return -1;
  5792. ssid.ssid = (u8 *) (cmd + 6);
  5793. ssid_p = &ssid;
  5794. }
  5795. ret = wpas_rrm_send_neighbor_rep_request(wpa_s, ssid_p,
  5796. wpas_ctrl_neighbor_rep_cb,
  5797. wpa_s);
  5798. return ret;
  5799. }
  5800. static int wpas_ctrl_iface_erp_flush(struct wpa_supplicant *wpa_s)
  5801. {
  5802. eapol_sm_erp_flush(wpa_s->eapol);
  5803. return 0;
  5804. }
  5805. char * wpa_supplicant_ctrl_iface_process(struct wpa_supplicant *wpa_s,
  5806. char *buf, size_t *resp_len)
  5807. {
  5808. char *reply;
  5809. const int reply_size = 4096;
  5810. int reply_len;
  5811. if (os_strncmp(buf, WPA_CTRL_RSP, os_strlen(WPA_CTRL_RSP)) == 0 ||
  5812. os_strncmp(buf, "SET_NETWORK ", 12) == 0) {
  5813. if (wpa_debug_show_keys)
  5814. wpa_dbg(wpa_s, MSG_DEBUG,
  5815. "Control interface command '%s'", buf);
  5816. else
  5817. wpa_dbg(wpa_s, MSG_DEBUG,
  5818. "Control interface command '%s [REMOVED]'",
  5819. os_strncmp(buf, WPA_CTRL_RSP,
  5820. os_strlen(WPA_CTRL_RSP)) == 0 ?
  5821. WPA_CTRL_RSP : "SET_NETWORK");
  5822. } else if (os_strncmp(buf, "WPS_NFC_TAG_READ", 16) == 0 ||
  5823. os_strncmp(buf, "NFC_REPORT_HANDOVER", 19) == 0) {
  5824. wpa_hexdump_ascii_key(MSG_DEBUG, "RX ctrl_iface",
  5825. (const u8 *) buf, os_strlen(buf));
  5826. } else {
  5827. int level = MSG_DEBUG;
  5828. if (os_strcmp(buf, "PING") == 0)
  5829. level = MSG_EXCESSIVE;
  5830. wpa_dbg(wpa_s, level, "Control interface command '%s'", buf);
  5831. }
  5832. reply = os_malloc(reply_size);
  5833. if (reply == NULL) {
  5834. *resp_len = 1;
  5835. return NULL;
  5836. }
  5837. os_memcpy(reply, "OK\n", 3);
  5838. reply_len = 3;
  5839. if (os_strcmp(buf, "PING") == 0) {
  5840. os_memcpy(reply, "PONG\n", 5);
  5841. reply_len = 5;
  5842. } else if (os_strcmp(buf, "IFNAME") == 0) {
  5843. reply_len = os_strlen(wpa_s->ifname);
  5844. os_memcpy(reply, wpa_s->ifname, reply_len);
  5845. } else if (os_strncmp(buf, "RELOG", 5) == 0) {
  5846. if (wpa_debug_reopen_file() < 0)
  5847. reply_len = -1;
  5848. } else if (os_strncmp(buf, "NOTE ", 5) == 0) {
  5849. wpa_printf(MSG_INFO, "NOTE: %s", buf + 5);
  5850. } else if (os_strcmp(buf, "MIB") == 0) {
  5851. reply_len = wpa_sm_get_mib(wpa_s->wpa, reply, reply_size);
  5852. if (reply_len >= 0) {
  5853. int res;
  5854. res = eapol_sm_get_mib(wpa_s->eapol, reply + reply_len,
  5855. reply_size - reply_len);
  5856. if (res < 0)
  5857. reply_len = -1;
  5858. else
  5859. reply_len += res;
  5860. }
  5861. } else if (os_strncmp(buf, "STATUS", 6) == 0) {
  5862. reply_len = wpa_supplicant_ctrl_iface_status(
  5863. wpa_s, buf + 6, reply, reply_size);
  5864. } else if (os_strcmp(buf, "PMKSA") == 0) {
  5865. reply_len = wpa_sm_pmksa_cache_list(wpa_s->wpa, reply,
  5866. reply_size);
  5867. } else if (os_strcmp(buf, "PMKSA_FLUSH") == 0) {
  5868. wpa_sm_pmksa_cache_flush(wpa_s->wpa, NULL);
  5869. } else if (os_strncmp(buf, "SET ", 4) == 0) {
  5870. if (wpa_supplicant_ctrl_iface_set(wpa_s, buf + 4))
  5871. reply_len = -1;
  5872. } else if (os_strncmp(buf, "GET ", 4) == 0) {
  5873. reply_len = wpa_supplicant_ctrl_iface_get(wpa_s, buf + 4,
  5874. reply, reply_size);
  5875. } else if (os_strcmp(buf, "LOGON") == 0) {
  5876. eapol_sm_notify_logoff(wpa_s->eapol, FALSE);
  5877. } else if (os_strcmp(buf, "LOGOFF") == 0) {
  5878. eapol_sm_notify_logoff(wpa_s->eapol, TRUE);
  5879. } else if (os_strcmp(buf, "REASSOCIATE") == 0) {
  5880. if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED)
  5881. reply_len = -1;
  5882. else
  5883. wpas_request_connection(wpa_s);
  5884. } else if (os_strcmp(buf, "REATTACH") == 0) {
  5885. if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED ||
  5886. !wpa_s->current_ssid)
  5887. reply_len = -1;
  5888. else {
  5889. wpa_s->reattach = 1;
  5890. wpas_request_connection(wpa_s);
  5891. }
  5892. } else if (os_strcmp(buf, "RECONNECT") == 0) {
  5893. if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED)
  5894. reply_len = -1;
  5895. else if (wpa_s->disconnected)
  5896. wpas_request_connection(wpa_s);
  5897. #ifdef IEEE8021X_EAPOL
  5898. } else if (os_strncmp(buf, "PREAUTH ", 8) == 0) {
  5899. if (wpa_supplicant_ctrl_iface_preauth(wpa_s, buf + 8))
  5900. reply_len = -1;
  5901. #endif /* IEEE8021X_EAPOL */
  5902. #ifdef CONFIG_PEERKEY
  5903. } else if (os_strncmp(buf, "STKSTART ", 9) == 0) {
  5904. if (wpa_supplicant_ctrl_iface_stkstart(wpa_s, buf + 9))
  5905. reply_len = -1;
  5906. #endif /* CONFIG_PEERKEY */
  5907. #ifdef CONFIG_IEEE80211R
  5908. } else if (os_strncmp(buf, "FT_DS ", 6) == 0) {
  5909. if (wpa_supplicant_ctrl_iface_ft_ds(wpa_s, buf + 6))
  5910. reply_len = -1;
  5911. #endif /* CONFIG_IEEE80211R */
  5912. #ifdef CONFIG_WPS
  5913. } else if (os_strcmp(buf, "WPS_PBC") == 0) {
  5914. int res = wpa_supplicant_ctrl_iface_wps_pbc(wpa_s, NULL);
  5915. if (res == -2) {
  5916. os_memcpy(reply, "FAIL-PBC-OVERLAP\n", 17);
  5917. reply_len = 17;
  5918. } else if (res)
  5919. reply_len = -1;
  5920. } else if (os_strncmp(buf, "WPS_PBC ", 8) == 0) {
  5921. int res = wpa_supplicant_ctrl_iface_wps_pbc(wpa_s, buf + 8);
  5922. if (res == -2) {
  5923. os_memcpy(reply, "FAIL-PBC-OVERLAP\n", 17);
  5924. reply_len = 17;
  5925. } else if (res)
  5926. reply_len = -1;
  5927. } else if (os_strncmp(buf, "WPS_PIN ", 8) == 0) {
  5928. reply_len = wpa_supplicant_ctrl_iface_wps_pin(wpa_s, buf + 8,
  5929. reply,
  5930. reply_size);
  5931. } else if (os_strncmp(buf, "WPS_CHECK_PIN ", 14) == 0) {
  5932. reply_len = wpa_supplicant_ctrl_iface_wps_check_pin(
  5933. wpa_s, buf + 14, reply, reply_size);
  5934. } else if (os_strcmp(buf, "WPS_CANCEL") == 0) {
  5935. if (wpas_wps_cancel(wpa_s))
  5936. reply_len = -1;
  5937. #ifdef CONFIG_WPS_NFC
  5938. } else if (os_strcmp(buf, "WPS_NFC") == 0) {
  5939. if (wpa_supplicant_ctrl_iface_wps_nfc(wpa_s, NULL))
  5940. reply_len = -1;
  5941. } else if (os_strncmp(buf, "WPS_NFC ", 8) == 0) {
  5942. if (wpa_supplicant_ctrl_iface_wps_nfc(wpa_s, buf + 8))
  5943. reply_len = -1;
  5944. } else if (os_strncmp(buf, "WPS_NFC_CONFIG_TOKEN ", 21) == 0) {
  5945. reply_len = wpa_supplicant_ctrl_iface_wps_nfc_config_token(
  5946. wpa_s, buf + 21, reply, reply_size);
  5947. } else if (os_strncmp(buf, "WPS_NFC_TOKEN ", 14) == 0) {
  5948. reply_len = wpa_supplicant_ctrl_iface_wps_nfc_token(
  5949. wpa_s, buf + 14, reply, reply_size);
  5950. } else if (os_strncmp(buf, "WPS_NFC_TAG_READ ", 17) == 0) {
  5951. if (wpa_supplicant_ctrl_iface_wps_nfc_tag_read(wpa_s,
  5952. buf + 17))
  5953. reply_len = -1;
  5954. } else if (os_strncmp(buf, "NFC_GET_HANDOVER_REQ ", 21) == 0) {
  5955. reply_len = wpas_ctrl_nfc_get_handover_req(
  5956. wpa_s, buf + 21, reply, reply_size);
  5957. } else if (os_strncmp(buf, "NFC_GET_HANDOVER_SEL ", 21) == 0) {
  5958. reply_len = wpas_ctrl_nfc_get_handover_sel(
  5959. wpa_s, buf + 21, reply, reply_size);
  5960. } else if (os_strncmp(buf, "NFC_REPORT_HANDOVER ", 20) == 0) {
  5961. if (wpas_ctrl_nfc_report_handover(wpa_s, buf + 20))
  5962. reply_len = -1;
  5963. #endif /* CONFIG_WPS_NFC */
  5964. } else if (os_strncmp(buf, "WPS_REG ", 8) == 0) {
  5965. if (wpa_supplicant_ctrl_iface_wps_reg(wpa_s, buf + 8))
  5966. reply_len = -1;
  5967. #ifdef CONFIG_AP
  5968. } else if (os_strncmp(buf, "WPS_AP_PIN ", 11) == 0) {
  5969. reply_len = wpa_supplicant_ctrl_iface_wps_ap_pin(
  5970. wpa_s, buf + 11, reply, reply_size);
  5971. #endif /* CONFIG_AP */
  5972. #ifdef CONFIG_WPS_ER
  5973. } else if (os_strcmp(buf, "WPS_ER_START") == 0) {
  5974. if (wpas_wps_er_start(wpa_s, NULL))
  5975. reply_len = -1;
  5976. } else if (os_strncmp(buf, "WPS_ER_START ", 13) == 0) {
  5977. if (wpas_wps_er_start(wpa_s, buf + 13))
  5978. reply_len = -1;
  5979. } else if (os_strcmp(buf, "WPS_ER_STOP") == 0) {
  5980. if (wpas_wps_er_stop(wpa_s))
  5981. reply_len = -1;
  5982. } else if (os_strncmp(buf, "WPS_ER_PIN ", 11) == 0) {
  5983. if (wpa_supplicant_ctrl_iface_wps_er_pin(wpa_s, buf + 11))
  5984. reply_len = -1;
  5985. } else if (os_strncmp(buf, "WPS_ER_PBC ", 11) == 0) {
  5986. int ret = wpas_wps_er_pbc(wpa_s, buf + 11);
  5987. if (ret == -2) {
  5988. os_memcpy(reply, "FAIL-PBC-OVERLAP\n", 17);
  5989. reply_len = 17;
  5990. } else if (ret == -3) {
  5991. os_memcpy(reply, "FAIL-UNKNOWN-UUID\n", 18);
  5992. reply_len = 18;
  5993. } else if (ret == -4) {
  5994. os_memcpy(reply, "FAIL-NO-AP-SETTINGS\n", 20);
  5995. reply_len = 20;
  5996. } else if (ret)
  5997. reply_len = -1;
  5998. } else if (os_strncmp(buf, "WPS_ER_LEARN ", 13) == 0) {
  5999. if (wpa_supplicant_ctrl_iface_wps_er_learn(wpa_s, buf + 13))
  6000. reply_len = -1;
  6001. } else if (os_strncmp(buf, "WPS_ER_SET_CONFIG ", 18) == 0) {
  6002. if (wpa_supplicant_ctrl_iface_wps_er_set_config(wpa_s,
  6003. buf + 18))
  6004. reply_len = -1;
  6005. } else if (os_strncmp(buf, "WPS_ER_CONFIG ", 14) == 0) {
  6006. if (wpa_supplicant_ctrl_iface_wps_er_config(wpa_s, buf + 14))
  6007. reply_len = -1;
  6008. #ifdef CONFIG_WPS_NFC
  6009. } else if (os_strncmp(buf, "WPS_ER_NFC_CONFIG_TOKEN ", 24) == 0) {
  6010. reply_len = wpa_supplicant_ctrl_iface_wps_er_nfc_config_token(
  6011. wpa_s, buf + 24, reply, reply_size);
  6012. #endif /* CONFIG_WPS_NFC */
  6013. #endif /* CONFIG_WPS_ER */
  6014. #endif /* CONFIG_WPS */
  6015. #ifdef CONFIG_IBSS_RSN
  6016. } else if (os_strncmp(buf, "IBSS_RSN ", 9) == 0) {
  6017. if (wpa_supplicant_ctrl_iface_ibss_rsn(wpa_s, buf + 9))
  6018. reply_len = -1;
  6019. #endif /* CONFIG_IBSS_RSN */
  6020. #ifdef CONFIG_MESH
  6021. } else if (os_strncmp(buf, "MESH_GROUP_ADD ", 15) == 0) {
  6022. if (wpa_supplicant_ctrl_iface_mesh_group_add(wpa_s, buf + 15))
  6023. reply_len = -1;
  6024. } else if (os_strncmp(buf, "MESH_GROUP_REMOVE ", 18) == 0) {
  6025. if (wpa_supplicant_ctrl_iface_mesh_group_remove(wpa_s,
  6026. buf + 18))
  6027. reply_len = -1;
  6028. #endif /* CONFIG_MESH */
  6029. #ifdef CONFIG_P2P
  6030. } else if (os_strncmp(buf, "P2P_FIND ", 9) == 0) {
  6031. if (p2p_ctrl_find(wpa_s, buf + 9))
  6032. reply_len = -1;
  6033. } else if (os_strcmp(buf, "P2P_FIND") == 0) {
  6034. if (p2p_ctrl_find(wpa_s, ""))
  6035. reply_len = -1;
  6036. } else if (os_strcmp(buf, "P2P_STOP_FIND") == 0) {
  6037. wpas_p2p_stop_find(wpa_s);
  6038. } else if (os_strncmp(buf, "P2P_CONNECT ", 12) == 0) {
  6039. reply_len = p2p_ctrl_connect(wpa_s, buf + 12, reply,
  6040. reply_size);
  6041. } else if (os_strncmp(buf, "P2P_LISTEN ", 11) == 0) {
  6042. if (p2p_ctrl_listen(wpa_s, buf + 11))
  6043. reply_len = -1;
  6044. } else if (os_strcmp(buf, "P2P_LISTEN") == 0) {
  6045. if (p2p_ctrl_listen(wpa_s, ""))
  6046. reply_len = -1;
  6047. } else if (os_strncmp(buf, "P2P_GROUP_REMOVE ", 17) == 0) {
  6048. if (wpas_p2p_group_remove(wpa_s, buf + 17))
  6049. reply_len = -1;
  6050. } else if (os_strcmp(buf, "P2P_GROUP_ADD") == 0) {
  6051. if (wpas_p2p_group_add(wpa_s, 0, 0, 0, 0))
  6052. reply_len = -1;
  6053. } else if (os_strncmp(buf, "P2P_GROUP_ADD ", 14) == 0) {
  6054. if (p2p_ctrl_group_add(wpa_s, buf + 14))
  6055. reply_len = -1;
  6056. } else if (os_strncmp(buf, "P2P_PROV_DISC ", 14) == 0) {
  6057. if (p2p_ctrl_prov_disc(wpa_s, buf + 14))
  6058. reply_len = -1;
  6059. } else if (os_strcmp(buf, "P2P_GET_PASSPHRASE") == 0) {
  6060. reply_len = p2p_get_passphrase(wpa_s, reply, reply_size);
  6061. } else if (os_strncmp(buf, "P2P_SERV_DISC_REQ ", 18) == 0) {
  6062. reply_len = p2p_ctrl_serv_disc_req(wpa_s, buf + 18, reply,
  6063. reply_size);
  6064. } else if (os_strncmp(buf, "P2P_SERV_DISC_CANCEL_REQ ", 25) == 0) {
  6065. if (p2p_ctrl_serv_disc_cancel_req(wpa_s, buf + 25) < 0)
  6066. reply_len = -1;
  6067. } else if (os_strncmp(buf, "P2P_SERV_DISC_RESP ", 19) == 0) {
  6068. if (p2p_ctrl_serv_disc_resp(wpa_s, buf + 19) < 0)
  6069. reply_len = -1;
  6070. } else if (os_strcmp(buf, "P2P_SERVICE_UPDATE") == 0) {
  6071. wpas_p2p_sd_service_update(wpa_s);
  6072. } else if (os_strncmp(buf, "P2P_SERV_DISC_EXTERNAL ", 23) == 0) {
  6073. if (p2p_ctrl_serv_disc_external(wpa_s, buf + 23) < 0)
  6074. reply_len = -1;
  6075. } else if (os_strcmp(buf, "P2P_SERVICE_FLUSH") == 0) {
  6076. wpas_p2p_service_flush(wpa_s);
  6077. } else if (os_strncmp(buf, "P2P_SERVICE_ADD ", 16) == 0) {
  6078. if (p2p_ctrl_service_add(wpa_s, buf + 16) < 0)
  6079. reply_len = -1;
  6080. } else if (os_strncmp(buf, "P2P_SERVICE_DEL ", 16) == 0) {
  6081. if (p2p_ctrl_service_del(wpa_s, buf + 16) < 0)
  6082. reply_len = -1;
  6083. } else if (os_strncmp(buf, "P2P_REJECT ", 11) == 0) {
  6084. if (p2p_ctrl_reject(wpa_s, buf + 11) < 0)
  6085. reply_len = -1;
  6086. } else if (os_strncmp(buf, "P2P_INVITE ", 11) == 0) {
  6087. if (p2p_ctrl_invite(wpa_s, buf + 11) < 0)
  6088. reply_len = -1;
  6089. } else if (os_strncmp(buf, "P2P_PEER ", 9) == 0) {
  6090. reply_len = p2p_ctrl_peer(wpa_s, buf + 9, reply,
  6091. reply_size);
  6092. } else if (os_strncmp(buf, "P2P_SET ", 8) == 0) {
  6093. if (p2p_ctrl_set(wpa_s, buf + 8) < 0)
  6094. reply_len = -1;
  6095. } else if (os_strcmp(buf, "P2P_FLUSH") == 0) {
  6096. p2p_ctrl_flush(wpa_s);
  6097. } else if (os_strncmp(buf, "P2P_UNAUTHORIZE ", 16) == 0) {
  6098. if (wpas_p2p_unauthorize(wpa_s, buf + 16) < 0)
  6099. reply_len = -1;
  6100. } else if (os_strcmp(buf, "P2P_CANCEL") == 0) {
  6101. if (wpas_p2p_cancel(wpa_s))
  6102. reply_len = -1;
  6103. } else if (os_strncmp(buf, "P2P_PRESENCE_REQ ", 17) == 0) {
  6104. if (p2p_ctrl_presence_req(wpa_s, buf + 17) < 0)
  6105. reply_len = -1;
  6106. } else if (os_strcmp(buf, "P2P_PRESENCE_REQ") == 0) {
  6107. if (p2p_ctrl_presence_req(wpa_s, "") < 0)
  6108. reply_len = -1;
  6109. } else if (os_strncmp(buf, "P2P_EXT_LISTEN ", 15) == 0) {
  6110. if (p2p_ctrl_ext_listen(wpa_s, buf + 15) < 0)
  6111. reply_len = -1;
  6112. } else if (os_strcmp(buf, "P2P_EXT_LISTEN") == 0) {
  6113. if (p2p_ctrl_ext_listen(wpa_s, "") < 0)
  6114. reply_len = -1;
  6115. } else if (os_strncmp(buf, "P2P_REMOVE_CLIENT ", 18) == 0) {
  6116. if (p2p_ctrl_remove_client(wpa_s, buf + 18) < 0)
  6117. reply_len = -1;
  6118. #endif /* CONFIG_P2P */
  6119. #ifdef CONFIG_WIFI_DISPLAY
  6120. } else if (os_strncmp(buf, "WFD_SUBELEM_SET ", 16) == 0) {
  6121. if (wifi_display_subelem_set(wpa_s->global, buf + 16) < 0)
  6122. reply_len = -1;
  6123. } else if (os_strncmp(buf, "WFD_SUBELEM_GET ", 16) == 0) {
  6124. reply_len = wifi_display_subelem_get(wpa_s->global, buf + 16,
  6125. reply, reply_size);
  6126. #endif /* CONFIG_WIFI_DISPLAY */
  6127. #ifdef CONFIG_INTERWORKING
  6128. } else if (os_strcmp(buf, "FETCH_ANQP") == 0) {
  6129. if (interworking_fetch_anqp(wpa_s) < 0)
  6130. reply_len = -1;
  6131. } else if (os_strcmp(buf, "STOP_FETCH_ANQP") == 0) {
  6132. interworking_stop_fetch_anqp(wpa_s);
  6133. } else if (os_strcmp(buf, "INTERWORKING_SELECT") == 0) {
  6134. if (ctrl_interworking_select(wpa_s, NULL) < 0)
  6135. reply_len = -1;
  6136. } else if (os_strncmp(buf, "INTERWORKING_SELECT ", 20) == 0) {
  6137. if (ctrl_interworking_select(wpa_s, buf + 20) < 0)
  6138. reply_len = -1;
  6139. } else if (os_strncmp(buf, "INTERWORKING_CONNECT ", 21) == 0) {
  6140. if (ctrl_interworking_connect(wpa_s, buf + 21) < 0)
  6141. reply_len = -1;
  6142. } else if (os_strncmp(buf, "ANQP_GET ", 9) == 0) {
  6143. if (get_anqp(wpa_s, buf + 9) < 0)
  6144. reply_len = -1;
  6145. } else if (os_strncmp(buf, "GAS_REQUEST ", 12) == 0) {
  6146. if (gas_request(wpa_s, buf + 12) < 0)
  6147. reply_len = -1;
  6148. } else if (os_strncmp(buf, "GAS_RESPONSE_GET ", 17) == 0) {
  6149. reply_len = gas_response_get(wpa_s, buf + 17, reply,
  6150. reply_size);
  6151. #endif /* CONFIG_INTERWORKING */
  6152. #ifdef CONFIG_HS20
  6153. } else if (os_strncmp(buf, "HS20_ANQP_GET ", 14) == 0) {
  6154. if (get_hs20_anqp(wpa_s, buf + 14) < 0)
  6155. reply_len = -1;
  6156. } else if (os_strncmp(buf, "HS20_GET_NAI_HOME_REALM_LIST ", 29) == 0) {
  6157. if (hs20_get_nai_home_realm_list(wpa_s, buf + 29) < 0)
  6158. reply_len = -1;
  6159. } else if (os_strncmp(buf, "HS20_ICON_REQUEST ", 18) == 0) {
  6160. if (hs20_icon_request(wpa_s, buf + 18) < 0)
  6161. reply_len = -1;
  6162. } else if (os_strcmp(buf, "FETCH_OSU") == 0) {
  6163. if (hs20_fetch_osu(wpa_s) < 0)
  6164. reply_len = -1;
  6165. } else if (os_strcmp(buf, "CANCEL_FETCH_OSU") == 0) {
  6166. hs20_cancel_fetch_osu(wpa_s);
  6167. #endif /* CONFIG_HS20 */
  6168. } else if (os_strncmp(buf, WPA_CTRL_RSP, os_strlen(WPA_CTRL_RSP)) == 0)
  6169. {
  6170. if (wpa_supplicant_ctrl_iface_ctrl_rsp(
  6171. wpa_s, buf + os_strlen(WPA_CTRL_RSP)))
  6172. reply_len = -1;
  6173. else {
  6174. /*
  6175. * Notify response from timeout to allow the control
  6176. * interface response to be sent first.
  6177. */
  6178. eloop_register_timeout(0, 0, wpas_ctrl_eapol_response,
  6179. wpa_s, NULL);
  6180. }
  6181. } else if (os_strcmp(buf, "RECONFIGURE") == 0) {
  6182. if (wpa_supplicant_reload_configuration(wpa_s))
  6183. reply_len = -1;
  6184. } else if (os_strcmp(buf, "TERMINATE") == 0) {
  6185. wpa_supplicant_terminate_proc(wpa_s->global);
  6186. } else if (os_strncmp(buf, "BSSID ", 6) == 0) {
  6187. if (wpa_supplicant_ctrl_iface_bssid(wpa_s, buf + 6))
  6188. reply_len = -1;
  6189. } else if (os_strncmp(buf, "BLACKLIST", 9) == 0) {
  6190. reply_len = wpa_supplicant_ctrl_iface_blacklist(
  6191. wpa_s, buf + 9, reply, reply_size);
  6192. } else if (os_strncmp(buf, "LOG_LEVEL", 9) == 0) {
  6193. reply_len = wpa_supplicant_ctrl_iface_log_level(
  6194. wpa_s, buf + 9, reply, reply_size);
  6195. } else if (os_strncmp(buf, "LIST_NETWORKS ", 14) == 0) {
  6196. reply_len = wpa_supplicant_ctrl_iface_list_networks(
  6197. wpa_s, buf + 14, reply, reply_size);
  6198. } else if (os_strcmp(buf, "LIST_NETWORKS") == 0) {
  6199. reply_len = wpa_supplicant_ctrl_iface_list_networks(
  6200. wpa_s, NULL, reply, reply_size);
  6201. } else if (os_strcmp(buf, "DISCONNECT") == 0) {
  6202. #ifdef CONFIG_SME
  6203. wpa_s->sme.prev_bssid_set = 0;
  6204. #endif /* CONFIG_SME */
  6205. wpa_s->reassociate = 0;
  6206. wpa_s->disconnected = 1;
  6207. wpa_supplicant_cancel_sched_scan(wpa_s);
  6208. wpa_supplicant_cancel_scan(wpa_s);
  6209. wpa_supplicant_deauthenticate(wpa_s,
  6210. WLAN_REASON_DEAUTH_LEAVING);
  6211. } else if (os_strcmp(buf, "SCAN") == 0) {
  6212. wpas_ctrl_scan(wpa_s, NULL, reply, reply_size, &reply_len);
  6213. } else if (os_strncmp(buf, "SCAN ", 5) == 0) {
  6214. wpas_ctrl_scan(wpa_s, buf + 5, reply, reply_size, &reply_len);
  6215. } else if (os_strcmp(buf, "SCAN_RESULTS") == 0) {
  6216. reply_len = wpa_supplicant_ctrl_iface_scan_results(
  6217. wpa_s, reply, reply_size);
  6218. } else if (os_strncmp(buf, "SELECT_NETWORK ", 15) == 0) {
  6219. if (wpa_supplicant_ctrl_iface_select_network(wpa_s, buf + 15))
  6220. reply_len = -1;
  6221. } else if (os_strncmp(buf, "ENABLE_NETWORK ", 15) == 0) {
  6222. if (wpa_supplicant_ctrl_iface_enable_network(wpa_s, buf + 15))
  6223. reply_len = -1;
  6224. } else if (os_strncmp(buf, "DISABLE_NETWORK ", 16) == 0) {
  6225. if (wpa_supplicant_ctrl_iface_disable_network(wpa_s, buf + 16))
  6226. reply_len = -1;
  6227. } else if (os_strcmp(buf, "ADD_NETWORK") == 0) {
  6228. reply_len = wpa_supplicant_ctrl_iface_add_network(
  6229. wpa_s, reply, reply_size);
  6230. } else if (os_strncmp(buf, "REMOVE_NETWORK ", 15) == 0) {
  6231. if (wpa_supplicant_ctrl_iface_remove_network(wpa_s, buf + 15))
  6232. reply_len = -1;
  6233. } else if (os_strncmp(buf, "SET_NETWORK ", 12) == 0) {
  6234. if (wpa_supplicant_ctrl_iface_set_network(wpa_s, buf + 12))
  6235. reply_len = -1;
  6236. } else if (os_strncmp(buf, "GET_NETWORK ", 12) == 0) {
  6237. reply_len = wpa_supplicant_ctrl_iface_get_network(
  6238. wpa_s, buf + 12, reply, reply_size);
  6239. } else if (os_strncmp(buf, "DUP_NETWORK ", 12) == 0) {
  6240. if (wpa_supplicant_ctrl_iface_dup_network(wpa_s, buf + 12))
  6241. reply_len = -1;
  6242. } else if (os_strcmp(buf, "LIST_CREDS") == 0) {
  6243. reply_len = wpa_supplicant_ctrl_iface_list_creds(
  6244. wpa_s, reply, reply_size);
  6245. } else if (os_strcmp(buf, "ADD_CRED") == 0) {
  6246. reply_len = wpa_supplicant_ctrl_iface_add_cred(
  6247. wpa_s, reply, reply_size);
  6248. } else if (os_strncmp(buf, "REMOVE_CRED ", 12) == 0) {
  6249. if (wpa_supplicant_ctrl_iface_remove_cred(wpa_s, buf + 12))
  6250. reply_len = -1;
  6251. } else if (os_strncmp(buf, "SET_CRED ", 9) == 0) {
  6252. if (wpa_supplicant_ctrl_iface_set_cred(wpa_s, buf + 9))
  6253. reply_len = -1;
  6254. } else if (os_strncmp(buf, "GET_CRED ", 9) == 0) {
  6255. reply_len = wpa_supplicant_ctrl_iface_get_cred(wpa_s, buf + 9,
  6256. reply,
  6257. reply_size);
  6258. #ifndef CONFIG_NO_CONFIG_WRITE
  6259. } else if (os_strcmp(buf, "SAVE_CONFIG") == 0) {
  6260. if (wpa_supplicant_ctrl_iface_save_config(wpa_s))
  6261. reply_len = -1;
  6262. #endif /* CONFIG_NO_CONFIG_WRITE */
  6263. } else if (os_strncmp(buf, "GET_CAPABILITY ", 15) == 0) {
  6264. reply_len = wpa_supplicant_ctrl_iface_get_capability(
  6265. wpa_s, buf + 15, reply, reply_size);
  6266. } else if (os_strncmp(buf, "AP_SCAN ", 8) == 0) {
  6267. if (wpa_supplicant_ctrl_iface_ap_scan(wpa_s, buf + 8))
  6268. reply_len = -1;
  6269. } else if (os_strncmp(buf, "SCAN_INTERVAL ", 14) == 0) {
  6270. if (wpa_supplicant_ctrl_iface_scan_interval(wpa_s, buf + 14))
  6271. reply_len = -1;
  6272. } else if (os_strcmp(buf, "INTERFACE_LIST") == 0) {
  6273. reply_len = wpa_supplicant_global_iface_list(
  6274. wpa_s->global, reply, reply_size);
  6275. } else if (os_strcmp(buf, "INTERFACES") == 0) {
  6276. reply_len = wpa_supplicant_global_iface_interfaces(
  6277. wpa_s->global, reply, reply_size);
  6278. } else if (os_strncmp(buf, "BSS ", 4) == 0) {
  6279. reply_len = wpa_supplicant_ctrl_iface_bss(
  6280. wpa_s, buf + 4, reply, reply_size);
  6281. #ifdef CONFIG_AP
  6282. } else if (os_strcmp(buf, "STA-FIRST") == 0) {
  6283. reply_len = ap_ctrl_iface_sta_first(wpa_s, reply, reply_size);
  6284. } else if (os_strncmp(buf, "STA ", 4) == 0) {
  6285. reply_len = ap_ctrl_iface_sta(wpa_s, buf + 4, reply,
  6286. reply_size);
  6287. } else if (os_strncmp(buf, "STA-NEXT ", 9) == 0) {
  6288. reply_len = ap_ctrl_iface_sta_next(wpa_s, buf + 9, reply,
  6289. reply_size);
  6290. } else if (os_strncmp(buf, "DEAUTHENTICATE ", 15) == 0) {
  6291. if (ap_ctrl_iface_sta_deauthenticate(wpa_s, buf + 15))
  6292. reply_len = -1;
  6293. } else if (os_strncmp(buf, "DISASSOCIATE ", 13) == 0) {
  6294. if (ap_ctrl_iface_sta_disassociate(wpa_s, buf + 13))
  6295. reply_len = -1;
  6296. } else if (os_strncmp(buf, "CHAN_SWITCH ", 12) == 0) {
  6297. if (ap_ctrl_iface_chanswitch(wpa_s, buf + 12))
  6298. reply_len = -1;
  6299. #endif /* CONFIG_AP */
  6300. } else if (os_strcmp(buf, "SUSPEND") == 0) {
  6301. wpas_notify_suspend(wpa_s->global);
  6302. } else if (os_strcmp(buf, "RESUME") == 0) {
  6303. wpas_notify_resume(wpa_s->global);
  6304. #ifdef CONFIG_TESTING_OPTIONS
  6305. } else if (os_strcmp(buf, "DROP_SA") == 0) {
  6306. wpa_supplicant_ctrl_iface_drop_sa(wpa_s);
  6307. #endif /* CONFIG_TESTING_OPTIONS */
  6308. } else if (os_strncmp(buf, "ROAM ", 5) == 0) {
  6309. if (wpa_supplicant_ctrl_iface_roam(wpa_s, buf + 5))
  6310. reply_len = -1;
  6311. } else if (os_strncmp(buf, "STA_AUTOCONNECT ", 16) == 0) {
  6312. if (wpa_supplicant_ctrl_iface_sta_autoconnect(wpa_s, buf + 16))
  6313. reply_len = -1;
  6314. } else if (os_strncmp(buf, "BSS_EXPIRE_AGE ", 15) == 0) {
  6315. if (wpa_supplicant_ctrl_iface_bss_expire_age(wpa_s, buf + 15))
  6316. reply_len = -1;
  6317. } else if (os_strncmp(buf, "BSS_EXPIRE_COUNT ", 17) == 0) {
  6318. if (wpa_supplicant_ctrl_iface_bss_expire_count(wpa_s,
  6319. buf + 17))
  6320. reply_len = -1;
  6321. } else if (os_strncmp(buf, "BSS_FLUSH ", 10) == 0) {
  6322. if (wpa_supplicant_ctrl_iface_bss_flush(wpa_s, buf + 10))
  6323. reply_len = -1;
  6324. #ifdef CONFIG_TDLS
  6325. } else if (os_strncmp(buf, "TDLS_DISCOVER ", 14) == 0) {
  6326. if (wpa_supplicant_ctrl_iface_tdls_discover(wpa_s, buf + 14))
  6327. reply_len = -1;
  6328. } else if (os_strncmp(buf, "TDLS_SETUP ", 11) == 0) {
  6329. if (wpa_supplicant_ctrl_iface_tdls_setup(wpa_s, buf + 11))
  6330. reply_len = -1;
  6331. } else if (os_strncmp(buf, "TDLS_TEARDOWN ", 14) == 0) {
  6332. if (wpa_supplicant_ctrl_iface_tdls_teardown(wpa_s, buf + 14))
  6333. reply_len = -1;
  6334. #endif /* CONFIG_TDLS */
  6335. } else if (os_strcmp(buf, "WMM_AC_STATUS") == 0) {
  6336. reply_len = wpas_wmm_ac_status(wpa_s, reply, reply_size);
  6337. } else if (os_strncmp(buf, "WMM_AC_ADDTS ", 13) == 0) {
  6338. if (wmm_ac_ctrl_addts(wpa_s, buf + 13))
  6339. reply_len = -1;
  6340. } else if (os_strncmp(buf, "WMM_AC_DELTS ", 13) == 0) {
  6341. if (wmm_ac_ctrl_delts(wpa_s, buf + 13))
  6342. reply_len = -1;
  6343. } else if (os_strncmp(buf, "SIGNAL_POLL", 11) == 0) {
  6344. reply_len = wpa_supplicant_signal_poll(wpa_s, reply,
  6345. reply_size);
  6346. } else if (os_strncmp(buf, "PKTCNT_POLL", 11) == 0) {
  6347. reply_len = wpa_supplicant_pktcnt_poll(wpa_s, reply,
  6348. reply_size);
  6349. #ifdef CONFIG_AUTOSCAN
  6350. } else if (os_strncmp(buf, "AUTOSCAN ", 9) == 0) {
  6351. if (wpa_supplicant_ctrl_iface_autoscan(wpa_s, buf + 9))
  6352. reply_len = -1;
  6353. #endif /* CONFIG_AUTOSCAN */
  6354. #ifdef ANDROID
  6355. } else if (os_strncmp(buf, "DRIVER ", 7) == 0) {
  6356. reply_len = wpa_supplicant_driver_cmd(wpa_s, buf + 7, reply,
  6357. reply_size);
  6358. #endif /* ANDROID */
  6359. } else if (os_strncmp(buf, "VENDOR ", 7) == 0) {
  6360. reply_len = wpa_supplicant_vendor_cmd(wpa_s, buf + 7, reply,
  6361. reply_size);
  6362. } else if (os_strcmp(buf, "REAUTHENTICATE") == 0) {
  6363. pmksa_cache_clear_current(wpa_s->wpa);
  6364. eapol_sm_request_reauth(wpa_s->eapol);
  6365. #ifdef CONFIG_WNM
  6366. } else if (os_strncmp(buf, "WNM_SLEEP ", 10) == 0) {
  6367. if (wpas_ctrl_iface_wnm_sleep(wpa_s, buf + 10))
  6368. reply_len = -1;
  6369. } else if (os_strncmp(buf, "WNM_BSS_QUERY ", 10) == 0) {
  6370. if (wpas_ctrl_iface_wnm_bss_query(wpa_s, buf + 10))
  6371. reply_len = -1;
  6372. #endif /* CONFIG_WNM */
  6373. } else if (os_strcmp(buf, "FLUSH") == 0) {
  6374. wpa_supplicant_ctrl_iface_flush(wpa_s);
  6375. } else if (os_strncmp(buf, "RADIO_WORK ", 11) == 0) {
  6376. reply_len = wpas_ctrl_radio_work(wpa_s, buf + 11, reply,
  6377. reply_size);
  6378. #ifdef CONFIG_TESTING_OPTIONS
  6379. } else if (os_strncmp(buf, "MGMT_TX ", 8) == 0) {
  6380. if (wpas_ctrl_iface_mgmt_tx(wpa_s, buf + 8) < 0)
  6381. reply_len = -1;
  6382. } else if (os_strcmp(buf, "MGMT_TX_DONE") == 0) {
  6383. wpas_ctrl_iface_mgmt_tx_done(wpa_s);
  6384. } else if (os_strncmp(buf, "DRIVER_EVENT ", 13) == 0) {
  6385. if (wpas_ctrl_iface_driver_event(wpa_s, buf + 13) < 0)
  6386. reply_len = -1;
  6387. } else if (os_strncmp(buf, "EAPOL_RX ", 9) == 0) {
  6388. if (wpas_ctrl_iface_eapol_rx(wpa_s, buf + 9) < 0)
  6389. reply_len = -1;
  6390. } else if (os_strncmp(buf, "DATA_TEST_CONFIG ", 17) == 0) {
  6391. if (wpas_ctrl_iface_data_test_config(wpa_s, buf + 17) < 0)
  6392. reply_len = -1;
  6393. } else if (os_strncmp(buf, "DATA_TEST_TX ", 13) == 0) {
  6394. if (wpas_ctrl_iface_data_test_tx(wpa_s, buf + 13) < 0)
  6395. reply_len = -1;
  6396. } else if (os_strncmp(buf, "DATA_TEST_FRAME ", 16) == 0) {
  6397. if (wpas_ctrl_iface_data_test_frame(wpa_s, buf + 16) < 0)
  6398. reply_len = -1;
  6399. #endif /* CONFIG_TESTING_OPTIONS */
  6400. } else if (os_strncmp(buf, "VENDOR_ELEM_ADD ", 16) == 0) {
  6401. if (wpas_ctrl_vendor_elem_add(wpa_s, buf + 16) < 0)
  6402. reply_len = -1;
  6403. } else if (os_strncmp(buf, "VENDOR_ELEM_GET ", 16) == 0) {
  6404. reply_len = wpas_ctrl_vendor_elem_get(wpa_s, buf + 16, reply,
  6405. reply_size);
  6406. } else if (os_strncmp(buf, "VENDOR_ELEM_REMOVE ", 19) == 0) {
  6407. if (wpas_ctrl_vendor_elem_remove(wpa_s, buf + 19) < 0)
  6408. reply_len = -1;
  6409. } else if (os_strncmp(buf, "NEIGHBOR_REP_REQUEST", 20) == 0) {
  6410. if (wpas_ctrl_iface_send_neigbor_rep(wpa_s, buf + 20))
  6411. reply_len = -1;
  6412. } else if (os_strcmp(buf, "ERP_FLUSH") == 0) {
  6413. wpas_ctrl_iface_erp_flush(wpa_s);
  6414. } else {
  6415. os_memcpy(reply, "UNKNOWN COMMAND\n", 16);
  6416. reply_len = 16;
  6417. }
  6418. if (reply_len < 0) {
  6419. os_memcpy(reply, "FAIL\n", 5);
  6420. reply_len = 5;
  6421. }
  6422. *resp_len = reply_len;
  6423. return reply;
  6424. }
  6425. static int wpa_supplicant_global_iface_add(struct wpa_global *global,
  6426. char *cmd)
  6427. {
  6428. struct wpa_interface iface;
  6429. char *pos;
  6430. /*
  6431. * <ifname>TAB<confname>TAB<driver>TAB<ctrl_interface>TAB<driver_param>
  6432. * TAB<bridge_ifname>
  6433. */
  6434. wpa_printf(MSG_DEBUG, "CTRL_IFACE GLOBAL INTERFACE_ADD '%s'", cmd);
  6435. os_memset(&iface, 0, sizeof(iface));
  6436. do {
  6437. iface.ifname = pos = cmd;
  6438. pos = os_strchr(pos, '\t');
  6439. if (pos)
  6440. *pos++ = '\0';
  6441. if (iface.ifname[0] == '\0')
  6442. return -1;
  6443. if (pos == NULL)
  6444. break;
  6445. iface.confname = pos;
  6446. pos = os_strchr(pos, '\t');
  6447. if (pos)
  6448. *pos++ = '\0';
  6449. if (iface.confname[0] == '\0')
  6450. iface.confname = NULL;
  6451. if (pos == NULL)
  6452. break;
  6453. iface.driver = pos;
  6454. pos = os_strchr(pos, '\t');
  6455. if (pos)
  6456. *pos++ = '\0';
  6457. if (iface.driver[0] == '\0')
  6458. iface.driver = NULL;
  6459. if (pos == NULL)
  6460. break;
  6461. iface.ctrl_interface = pos;
  6462. pos = os_strchr(pos, '\t');
  6463. if (pos)
  6464. *pos++ = '\0';
  6465. if (iface.ctrl_interface[0] == '\0')
  6466. iface.ctrl_interface = NULL;
  6467. if (pos == NULL)
  6468. break;
  6469. iface.driver_param = pos;
  6470. pos = os_strchr(pos, '\t');
  6471. if (pos)
  6472. *pos++ = '\0';
  6473. if (iface.driver_param[0] == '\0')
  6474. iface.driver_param = NULL;
  6475. if (pos == NULL)
  6476. break;
  6477. iface.bridge_ifname = pos;
  6478. pos = os_strchr(pos, '\t');
  6479. if (pos)
  6480. *pos++ = '\0';
  6481. if (iface.bridge_ifname[0] == '\0')
  6482. iface.bridge_ifname = NULL;
  6483. if (pos == NULL)
  6484. break;
  6485. } while (0);
  6486. if (wpa_supplicant_get_iface(global, iface.ifname))
  6487. return -1;
  6488. return wpa_supplicant_add_iface(global, &iface) ? 0 : -1;
  6489. }
  6490. static int wpa_supplicant_global_iface_remove(struct wpa_global *global,
  6491. char *cmd)
  6492. {
  6493. struct wpa_supplicant *wpa_s;
  6494. wpa_printf(MSG_DEBUG, "CTRL_IFACE GLOBAL INTERFACE_REMOVE '%s'", cmd);
  6495. wpa_s = wpa_supplicant_get_iface(global, cmd);
  6496. if (wpa_s == NULL)
  6497. return -1;
  6498. return wpa_supplicant_remove_iface(global, wpa_s, 0);
  6499. }
  6500. static void wpa_free_iface_info(struct wpa_interface_info *iface)
  6501. {
  6502. struct wpa_interface_info *prev;
  6503. while (iface) {
  6504. prev = iface;
  6505. iface = iface->next;
  6506. os_free(prev->ifname);
  6507. os_free(prev->desc);
  6508. os_free(prev);
  6509. }
  6510. }
  6511. static int wpa_supplicant_global_iface_list(struct wpa_global *global,
  6512. char *buf, int len)
  6513. {
  6514. int i, res;
  6515. struct wpa_interface_info *iface = NULL, *last = NULL, *tmp;
  6516. char *pos, *end;
  6517. for (i = 0; wpa_drivers[i]; i++) {
  6518. struct wpa_driver_ops *drv = wpa_drivers[i];
  6519. if (drv->get_interfaces == NULL)
  6520. continue;
  6521. tmp = drv->get_interfaces(global->drv_priv[i]);
  6522. if (tmp == NULL)
  6523. continue;
  6524. if (last == NULL)
  6525. iface = last = tmp;
  6526. else
  6527. last->next = tmp;
  6528. while (last->next)
  6529. last = last->next;
  6530. }
  6531. pos = buf;
  6532. end = buf + len;
  6533. for (tmp = iface; tmp; tmp = tmp->next) {
  6534. res = os_snprintf(pos, end - pos, "%s\t%s\t%s\n",
  6535. tmp->drv_name, tmp->ifname,
  6536. tmp->desc ? tmp->desc : "");
  6537. if (os_snprintf_error(end - pos, res)) {
  6538. *pos = '\0';
  6539. break;
  6540. }
  6541. pos += res;
  6542. }
  6543. wpa_free_iface_info(iface);
  6544. return pos - buf;
  6545. }
  6546. static int wpa_supplicant_global_iface_interfaces(struct wpa_global *global,
  6547. char *buf, int len)
  6548. {
  6549. int res;
  6550. char *pos, *end;
  6551. struct wpa_supplicant *wpa_s;
  6552. wpa_s = global->ifaces;
  6553. pos = buf;
  6554. end = buf + len;
  6555. while (wpa_s) {
  6556. res = os_snprintf(pos, end - pos, "%s\n", wpa_s->ifname);
  6557. if (os_snprintf_error(end - pos, res)) {
  6558. *pos = '\0';
  6559. break;
  6560. }
  6561. pos += res;
  6562. wpa_s = wpa_s->next;
  6563. }
  6564. return pos - buf;
  6565. }
  6566. static char * wpas_global_ctrl_iface_ifname(struct wpa_global *global,
  6567. const char *ifname,
  6568. char *cmd, size_t *resp_len)
  6569. {
  6570. struct wpa_supplicant *wpa_s;
  6571. for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
  6572. if (os_strcmp(ifname, wpa_s->ifname) == 0)
  6573. break;
  6574. }
  6575. if (wpa_s == NULL) {
  6576. char *resp = os_strdup("FAIL-NO-IFNAME-MATCH\n");
  6577. if (resp)
  6578. *resp_len = os_strlen(resp);
  6579. else
  6580. *resp_len = 1;
  6581. return resp;
  6582. }
  6583. return wpa_supplicant_ctrl_iface_process(wpa_s, cmd, resp_len);
  6584. }
  6585. static char * wpas_global_ctrl_iface_redir_p2p(struct wpa_global *global,
  6586. char *buf, size_t *resp_len)
  6587. {
  6588. #ifdef CONFIG_P2P
  6589. static const char * cmd[] = {
  6590. "LIST_NETWORKS",
  6591. "P2P_FIND",
  6592. "P2P_STOP_FIND",
  6593. "P2P_LISTEN",
  6594. "P2P_GROUP_ADD",
  6595. "P2P_GET_PASSPHRASE",
  6596. "P2P_SERVICE_UPDATE",
  6597. "P2P_SERVICE_FLUSH",
  6598. "P2P_FLUSH",
  6599. "P2P_CANCEL",
  6600. "P2P_PRESENCE_REQ",
  6601. "P2P_EXT_LISTEN",
  6602. NULL
  6603. };
  6604. static const char * prefix[] = {
  6605. #ifdef ANDROID
  6606. "DRIVER ",
  6607. #endif /* ANDROID */
  6608. "GET_NETWORK ",
  6609. "REMOVE_NETWORK ",
  6610. "P2P_FIND ",
  6611. "P2P_CONNECT ",
  6612. "P2P_LISTEN ",
  6613. "P2P_GROUP_REMOVE ",
  6614. "P2P_GROUP_ADD ",
  6615. "P2P_PROV_DISC ",
  6616. "P2P_SERV_DISC_REQ ",
  6617. "P2P_SERV_DISC_CANCEL_REQ ",
  6618. "P2P_SERV_DISC_RESP ",
  6619. "P2P_SERV_DISC_EXTERNAL ",
  6620. "P2P_SERVICE_ADD ",
  6621. "P2P_SERVICE_DEL ",
  6622. "P2P_REJECT ",
  6623. "P2P_INVITE ",
  6624. "P2P_PEER ",
  6625. "P2P_SET ",
  6626. "P2P_UNAUTHORIZE ",
  6627. "P2P_PRESENCE_REQ ",
  6628. "P2P_EXT_LISTEN ",
  6629. "P2P_REMOVE_CLIENT ",
  6630. "NFC_GET_HANDOVER_SEL ",
  6631. "NFC_GET_HANDOVER_REQ ",
  6632. "NFC_REPORT_HANDOVER ",
  6633. NULL
  6634. };
  6635. int found = 0;
  6636. int i;
  6637. if (global->p2p_init_wpa_s == NULL)
  6638. return NULL;
  6639. for (i = 0; !found && cmd[i]; i++) {
  6640. if (os_strcmp(buf, cmd[i]) == 0)
  6641. found = 1;
  6642. }
  6643. for (i = 0; !found && prefix[i]; i++) {
  6644. if (os_strncmp(buf, prefix[i], os_strlen(prefix[i])) == 0)
  6645. found = 1;
  6646. }
  6647. if (found)
  6648. return wpa_supplicant_ctrl_iface_process(global->p2p_init_wpa_s,
  6649. buf, resp_len);
  6650. #endif /* CONFIG_P2P */
  6651. return NULL;
  6652. }
  6653. static char * wpas_global_ctrl_iface_redir_wfd(struct wpa_global *global,
  6654. char *buf, size_t *resp_len)
  6655. {
  6656. #ifdef CONFIG_WIFI_DISPLAY
  6657. if (global->p2p_init_wpa_s == NULL)
  6658. return NULL;
  6659. if (os_strncmp(buf, "WFD_SUBELEM_SET ", 16) == 0 ||
  6660. os_strncmp(buf, "WFD_SUBELEM_GET ", 16) == 0)
  6661. return wpa_supplicant_ctrl_iface_process(global->p2p_init_wpa_s,
  6662. buf, resp_len);
  6663. #endif /* CONFIG_WIFI_DISPLAY */
  6664. return NULL;
  6665. }
  6666. static char * wpas_global_ctrl_iface_redir(struct wpa_global *global,
  6667. char *buf, size_t *resp_len)
  6668. {
  6669. char *ret;
  6670. ret = wpas_global_ctrl_iface_redir_p2p(global, buf, resp_len);
  6671. if (ret)
  6672. return ret;
  6673. ret = wpas_global_ctrl_iface_redir_wfd(global, buf, resp_len);
  6674. if (ret)
  6675. return ret;
  6676. return NULL;
  6677. }
  6678. static int wpas_global_ctrl_iface_set(struct wpa_global *global, char *cmd)
  6679. {
  6680. char *value;
  6681. value = os_strchr(cmd, ' ');
  6682. if (value == NULL)
  6683. return -1;
  6684. *value++ = '\0';
  6685. wpa_printf(MSG_DEBUG, "GLOBAL_CTRL_IFACE SET '%s'='%s'", cmd, value);
  6686. #ifdef CONFIG_WIFI_DISPLAY
  6687. if (os_strcasecmp(cmd, "wifi_display") == 0) {
  6688. wifi_display_enable(global, !!atoi(value));
  6689. return 0;
  6690. }
  6691. #endif /* CONFIG_WIFI_DISPLAY */
  6692. /* Restore cmd to its original value to allow redirection */
  6693. value[-1] = ' ';
  6694. return -1;
  6695. }
  6696. #ifndef CONFIG_NO_CONFIG_WRITE
  6697. static int wpas_global_ctrl_iface_save_config(struct wpa_global *global)
  6698. {
  6699. int ret = 0, saved = 0;
  6700. struct wpa_supplicant *wpa_s;
  6701. for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
  6702. if (!wpa_s->conf->update_config) {
  6703. wpa_dbg(wpa_s, MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Not allowed to update configuration (update_config=0)");
  6704. continue;
  6705. }
  6706. if (wpa_config_write(wpa_s->confname, wpa_s->conf)) {
  6707. wpa_dbg(wpa_s, MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Failed to update configuration");
  6708. ret = 1;
  6709. } else {
  6710. wpa_dbg(wpa_s, MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Configuration updated");
  6711. saved++;
  6712. }
  6713. }
  6714. if (!saved && !ret) {
  6715. wpa_dbg(wpa_s, MSG_DEBUG,
  6716. "CTRL_IFACE: SAVE_CONFIG - No configuration files could be updated");
  6717. ret = 1;
  6718. }
  6719. return ret;
  6720. }
  6721. #endif /* CONFIG_NO_CONFIG_WRITE */
  6722. static int wpas_global_ctrl_iface_status(struct wpa_global *global,
  6723. char *buf, size_t buflen)
  6724. {
  6725. char *pos, *end;
  6726. int ret;
  6727. struct wpa_supplicant *wpa_s;
  6728. pos = buf;
  6729. end = buf + buflen;
  6730. #ifdef CONFIG_P2P
  6731. if (global->p2p && !global->p2p_disabled) {
  6732. ret = os_snprintf(pos, end - pos, "p2p_device_address=" MACSTR
  6733. "\n"
  6734. "p2p_state=%s\n",
  6735. MAC2STR(global->p2p_dev_addr),
  6736. p2p_get_state_txt(global->p2p));
  6737. if (os_snprintf_error(end - pos, ret))
  6738. return pos - buf;
  6739. pos += ret;
  6740. } else if (global->p2p) {
  6741. ret = os_snprintf(pos, end - pos, "p2p_state=DISABLED\n");
  6742. if (os_snprintf_error(end - pos, ret))
  6743. return pos - buf;
  6744. pos += ret;
  6745. }
  6746. #endif /* CONFIG_P2P */
  6747. #ifdef CONFIG_WIFI_DISPLAY
  6748. ret = os_snprintf(pos, end - pos, "wifi_display=%d\n",
  6749. !!global->wifi_display);
  6750. if (os_snprintf_error(end - pos, ret))
  6751. return pos - buf;
  6752. pos += ret;
  6753. #endif /* CONFIG_WIFI_DISPLAY */
  6754. for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
  6755. ret = os_snprintf(pos, end - pos, "ifname=%s\n"
  6756. "address=" MACSTR "\n",
  6757. wpa_s->ifname, MAC2STR(wpa_s->own_addr));
  6758. if (os_snprintf_error(end - pos, ret))
  6759. return pos - buf;
  6760. pos += ret;
  6761. }
  6762. return pos - buf;
  6763. }
  6764. char * wpa_supplicant_global_ctrl_iface_process(struct wpa_global *global,
  6765. char *buf, size_t *resp_len)
  6766. {
  6767. char *reply;
  6768. const int reply_size = 2048;
  6769. int reply_len;
  6770. int level = MSG_DEBUG;
  6771. if (os_strncmp(buf, "IFNAME=", 7) == 0) {
  6772. char *pos = os_strchr(buf + 7, ' ');
  6773. if (pos) {
  6774. *pos++ = '\0';
  6775. return wpas_global_ctrl_iface_ifname(global,
  6776. buf + 7, pos,
  6777. resp_len);
  6778. }
  6779. }
  6780. reply = wpas_global_ctrl_iface_redir(global, buf, resp_len);
  6781. if (reply)
  6782. return reply;
  6783. if (os_strcmp(buf, "PING") == 0)
  6784. level = MSG_EXCESSIVE;
  6785. wpa_hexdump_ascii(level, "RX global ctrl_iface",
  6786. (const u8 *) buf, os_strlen(buf));
  6787. reply = os_malloc(reply_size);
  6788. if (reply == NULL) {
  6789. *resp_len = 1;
  6790. return NULL;
  6791. }
  6792. os_memcpy(reply, "OK\n", 3);
  6793. reply_len = 3;
  6794. if (os_strcmp(buf, "PING") == 0) {
  6795. os_memcpy(reply, "PONG\n", 5);
  6796. reply_len = 5;
  6797. } else if (os_strncmp(buf, "INTERFACE_ADD ", 14) == 0) {
  6798. if (wpa_supplicant_global_iface_add(global, buf + 14))
  6799. reply_len = -1;
  6800. } else if (os_strncmp(buf, "INTERFACE_REMOVE ", 17) == 0) {
  6801. if (wpa_supplicant_global_iface_remove(global, buf + 17))
  6802. reply_len = -1;
  6803. } else if (os_strcmp(buf, "INTERFACE_LIST") == 0) {
  6804. reply_len = wpa_supplicant_global_iface_list(
  6805. global, reply, reply_size);
  6806. } else if (os_strcmp(buf, "INTERFACES") == 0) {
  6807. reply_len = wpa_supplicant_global_iface_interfaces(
  6808. global, reply, reply_size);
  6809. } else if (os_strcmp(buf, "TERMINATE") == 0) {
  6810. wpa_supplicant_terminate_proc(global);
  6811. } else if (os_strcmp(buf, "SUSPEND") == 0) {
  6812. wpas_notify_suspend(global);
  6813. } else if (os_strcmp(buf, "RESUME") == 0) {
  6814. wpas_notify_resume(global);
  6815. } else if (os_strncmp(buf, "SET ", 4) == 0) {
  6816. if (wpas_global_ctrl_iface_set(global, buf + 4)) {
  6817. #ifdef CONFIG_P2P
  6818. if (global->p2p_init_wpa_s) {
  6819. os_free(reply);
  6820. /* Check if P2P redirection would work for this
  6821. * command. */
  6822. return wpa_supplicant_ctrl_iface_process(
  6823. global->p2p_init_wpa_s,
  6824. buf, resp_len);
  6825. }
  6826. #endif /* CONFIG_P2P */
  6827. reply_len = -1;
  6828. }
  6829. #ifndef CONFIG_NO_CONFIG_WRITE
  6830. } else if (os_strcmp(buf, "SAVE_CONFIG") == 0) {
  6831. if (wpas_global_ctrl_iface_save_config(global))
  6832. reply_len = -1;
  6833. #endif /* CONFIG_NO_CONFIG_WRITE */
  6834. } else if (os_strcmp(buf, "STATUS") == 0) {
  6835. reply_len = wpas_global_ctrl_iface_status(global, reply,
  6836. reply_size);
  6837. #ifdef CONFIG_MODULE_TESTS
  6838. } else if (os_strcmp(buf, "MODULE_TESTS") == 0) {
  6839. int wpas_module_tests(void);
  6840. if (wpas_module_tests() < 0)
  6841. reply_len = -1;
  6842. #endif /* CONFIG_MODULE_TESTS */
  6843. } else if (os_strncmp(buf, "RELOG", 5) == 0) {
  6844. if (wpa_debug_reopen_file() < 0)
  6845. reply_len = -1;
  6846. } else {
  6847. os_memcpy(reply, "UNKNOWN COMMAND\n", 16);
  6848. reply_len = 16;
  6849. }
  6850. if (reply_len < 0) {
  6851. os_memcpy(reply, "FAIL\n", 5);
  6852. reply_len = 5;
  6853. }
  6854. *resp_len = reply_len;
  6855. return reply;
  6856. }