ctrl_iface.c 251 KB

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