ctrl_iface.c 255 KB

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