ctrl_iface.c 183 KB

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