ctrl_iface.c 232 KB

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