ctrl_iface.c 137 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840
  1. /*
  2. * WPA Supplicant / Control interface (shared code for all backends)
  3. * Copyright (c) 2004-2012, 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. #include "utils/common.h"
  10. #include "utils/eloop.h"
  11. #include "common/version.h"
  12. #include "common/ieee802_11_defs.h"
  13. #include "common/ieee802_11_common.h"
  14. #include "common/wpa_ctrl.h"
  15. #include "eap_peer/eap.h"
  16. #include "eapol_supp/eapol_supp_sm.h"
  17. #include "rsn_supp/wpa.h"
  18. #include "rsn_supp/preauth.h"
  19. #include "rsn_supp/pmksa_cache.h"
  20. #include "l2_packet/l2_packet.h"
  21. #include "wps/wps.h"
  22. #include "config.h"
  23. #include "wpa_supplicant_i.h"
  24. #include "driver_i.h"
  25. #include "wps_supplicant.h"
  26. #include "ibss_rsn.h"
  27. #include "ap.h"
  28. #include "p2p_supplicant.h"
  29. #include "p2p/p2p.h"
  30. #include "hs20_supplicant.h"
  31. #include "wifi_display.h"
  32. #include "notify.h"
  33. #include "bss.h"
  34. #include "scan.h"
  35. #include "ctrl_iface.h"
  36. #include "interworking.h"
  37. #include "blacklist.h"
  38. #include "autoscan.h"
  39. #include "wnm_sta.h"
  40. extern struct wpa_driver_ops *wpa_drivers[];
  41. static int wpa_supplicant_global_iface_list(struct wpa_global *global,
  42. char *buf, int len);
  43. static int wpa_supplicant_global_iface_interfaces(struct wpa_global *global,
  44. char *buf, int len);
  45. static int pno_start(struct wpa_supplicant *wpa_s)
  46. {
  47. int ret;
  48. size_t i, num_ssid;
  49. struct wpa_ssid *ssid;
  50. struct wpa_driver_scan_params params;
  51. if (wpa_s->pno)
  52. return 0;
  53. if (wpa_s->wpa_state == WPA_SCANNING) {
  54. wpa_supplicant_cancel_sched_scan(wpa_s);
  55. wpa_supplicant_cancel_scan(wpa_s);
  56. }
  57. os_memset(&params, 0, sizeof(params));
  58. num_ssid = 0;
  59. ssid = wpa_s->conf->ssid;
  60. while (ssid) {
  61. if (!wpas_network_disabled(wpa_s, ssid))
  62. num_ssid++;
  63. ssid = ssid->next;
  64. }
  65. if (num_ssid > WPAS_MAX_SCAN_SSIDS) {
  66. wpa_printf(MSG_DEBUG, "PNO: Use only the first %u SSIDs from "
  67. "%u", WPAS_MAX_SCAN_SSIDS, (unsigned int) num_ssid);
  68. num_ssid = WPAS_MAX_SCAN_SSIDS;
  69. }
  70. if (num_ssid == 0) {
  71. wpa_printf(MSG_DEBUG, "PNO: No configured SSIDs");
  72. return -1;
  73. }
  74. params.filter_ssids = os_malloc(sizeof(struct wpa_driver_scan_filter) *
  75. num_ssid);
  76. if (params.filter_ssids == NULL)
  77. return -1;
  78. i = 0;
  79. ssid = wpa_s->conf->ssid;
  80. while (ssid) {
  81. if (!wpas_network_disabled(wpa_s, ssid)) {
  82. params.ssids[i].ssid = ssid->ssid;
  83. params.ssids[i].ssid_len = ssid->ssid_len;
  84. params.num_ssids++;
  85. os_memcpy(params.filter_ssids[i].ssid, ssid->ssid,
  86. ssid->ssid_len);
  87. params.filter_ssids[i].ssid_len = ssid->ssid_len;
  88. params.num_filter_ssids++;
  89. i++;
  90. if (i == num_ssid)
  91. break;
  92. }
  93. ssid = ssid->next;
  94. }
  95. if (wpa_s->conf->filter_rssi)
  96. params.filter_rssi = wpa_s->conf->filter_rssi;
  97. ret = wpa_drv_sched_scan(wpa_s, &params, 10 * 1000);
  98. os_free(params.filter_ssids);
  99. if (ret == 0)
  100. wpa_s->pno = 1;
  101. return ret;
  102. }
  103. static int pno_stop(struct wpa_supplicant *wpa_s)
  104. {
  105. int ret = 0;
  106. if (wpa_s->pno) {
  107. wpa_s->pno = 0;
  108. ret = wpa_drv_stop_sched_scan(wpa_s);
  109. }
  110. if (wpa_s->wpa_state == WPA_SCANNING)
  111. wpa_supplicant_req_scan(wpa_s, 0, 0);
  112. return ret;
  113. }
  114. static int set_bssid_filter(struct wpa_supplicant *wpa_s, char *val)
  115. {
  116. char *pos;
  117. u8 addr[ETH_ALEN], *filter = NULL, *n;
  118. size_t count = 0;
  119. pos = val;
  120. while (pos) {
  121. if (*pos == '\0')
  122. break;
  123. if (hwaddr_aton(pos, addr)) {
  124. os_free(filter);
  125. return -1;
  126. }
  127. n = os_realloc_array(filter, count + 1, ETH_ALEN);
  128. if (n == NULL) {
  129. os_free(filter);
  130. return -1;
  131. }
  132. filter = n;
  133. os_memcpy(filter + count * ETH_ALEN, addr, ETH_ALEN);
  134. count++;
  135. pos = os_strchr(pos, ' ');
  136. if (pos)
  137. pos++;
  138. }
  139. wpa_hexdump(MSG_DEBUG, "bssid_filter", filter, count * ETH_ALEN);
  140. os_free(wpa_s->bssid_filter);
  141. wpa_s->bssid_filter = filter;
  142. wpa_s->bssid_filter_count = count;
  143. return 0;
  144. }
  145. static int set_disallow_aps(struct wpa_supplicant *wpa_s, char *val)
  146. {
  147. char *pos;
  148. u8 addr[ETH_ALEN], *bssid = NULL, *n;
  149. struct wpa_ssid_value *ssid = NULL, *ns;
  150. size_t count = 0, ssid_count = 0;
  151. struct wpa_ssid *c;
  152. /*
  153. * disallow_list ::= <ssid_spec> | <bssid_spec> | <disallow_list> | “”
  154. * SSID_SPEC ::= ssid <SSID_HEX>
  155. * BSSID_SPEC ::= bssid <BSSID_HEX>
  156. */
  157. pos = val;
  158. while (pos) {
  159. if (*pos == '\0')
  160. break;
  161. if (os_strncmp(pos, "bssid ", 6) == 0) {
  162. int res;
  163. pos += 6;
  164. res = hwaddr_aton2(pos, addr);
  165. if (res < 0) {
  166. os_free(ssid);
  167. os_free(bssid);
  168. wpa_printf(MSG_DEBUG, "Invalid disallow_aps "
  169. "BSSID value '%s'", pos);
  170. return -1;
  171. }
  172. pos += res;
  173. n = os_realloc_array(bssid, count + 1, ETH_ALEN);
  174. if (n == NULL) {
  175. os_free(ssid);
  176. os_free(bssid);
  177. return -1;
  178. }
  179. bssid = n;
  180. os_memcpy(bssid + count * ETH_ALEN, addr, ETH_ALEN);
  181. count++;
  182. } else if (os_strncmp(pos, "ssid ", 5) == 0) {
  183. char *end;
  184. pos += 5;
  185. end = pos;
  186. while (*end) {
  187. if (*end == '\0' || *end == ' ')
  188. break;
  189. end++;
  190. }
  191. ns = os_realloc_array(ssid, ssid_count + 1,
  192. sizeof(struct wpa_ssid_value));
  193. if (ns == NULL) {
  194. os_free(ssid);
  195. os_free(bssid);
  196. return -1;
  197. }
  198. ssid = ns;
  199. if ((end - pos) & 0x01 || end - pos > 2 * 32 ||
  200. hexstr2bin(pos, ssid[ssid_count].ssid,
  201. (end - pos) / 2) < 0) {
  202. os_free(ssid);
  203. os_free(bssid);
  204. wpa_printf(MSG_DEBUG, "Invalid disallow_aps "
  205. "SSID value '%s'", pos);
  206. return -1;
  207. }
  208. ssid[ssid_count].ssid_len = (end - pos) / 2;
  209. wpa_hexdump_ascii(MSG_DEBUG, "disallow_aps SSID",
  210. ssid[ssid_count].ssid,
  211. ssid[ssid_count].ssid_len);
  212. ssid_count++;
  213. pos = end;
  214. } else {
  215. wpa_printf(MSG_DEBUG, "Unexpected disallow_aps value "
  216. "'%s'", pos);
  217. os_free(ssid);
  218. os_free(bssid);
  219. return -1;
  220. }
  221. pos = os_strchr(pos, ' ');
  222. if (pos)
  223. pos++;
  224. }
  225. wpa_hexdump(MSG_DEBUG, "disallow_aps_bssid", bssid, count * ETH_ALEN);
  226. os_free(wpa_s->disallow_aps_bssid);
  227. wpa_s->disallow_aps_bssid = bssid;
  228. wpa_s->disallow_aps_bssid_count = count;
  229. wpa_printf(MSG_DEBUG, "disallow_aps_ssid_count %d", (int) ssid_count);
  230. os_free(wpa_s->disallow_aps_ssid);
  231. wpa_s->disallow_aps_ssid = ssid;
  232. wpa_s->disallow_aps_ssid_count = ssid_count;
  233. if (!wpa_s->current_ssid || wpa_s->wpa_state < WPA_AUTHENTICATING)
  234. return 0;
  235. c = wpa_s->current_ssid;
  236. if (c->mode != WPAS_MODE_INFRA && c->mode != WPAS_MODE_IBSS)
  237. return 0;
  238. if (!disallowed_bssid(wpa_s, wpa_s->bssid) &&
  239. !disallowed_ssid(wpa_s, c->ssid, c->ssid_len))
  240. return 0;
  241. wpa_printf(MSG_DEBUG, "Disconnect and try to find another network "
  242. "because current AP was marked disallowed");
  243. #ifdef CONFIG_SME
  244. wpa_s->sme.prev_bssid_set = 0;
  245. #endif /* CONFIG_SME */
  246. wpa_s->reassociate = 1;
  247. wpa_supplicant_deauthenticate(wpa_s, WLAN_REASON_DEAUTH_LEAVING);
  248. wpa_supplicant_req_scan(wpa_s, 0, 0);
  249. return 0;
  250. }
  251. static int wpa_supplicant_ctrl_iface_set(struct wpa_supplicant *wpa_s,
  252. char *cmd)
  253. {
  254. char *value;
  255. int ret = 0;
  256. value = os_strchr(cmd, ' ');
  257. if (value == NULL)
  258. return -1;
  259. *value++ = '\0';
  260. wpa_printf(MSG_DEBUG, "CTRL_IFACE SET '%s'='%s'", cmd, value);
  261. if (os_strcasecmp(cmd, "EAPOL::heldPeriod") == 0) {
  262. eapol_sm_configure(wpa_s->eapol,
  263. atoi(value), -1, -1, -1);
  264. } else if (os_strcasecmp(cmd, "EAPOL::authPeriod") == 0) {
  265. eapol_sm_configure(wpa_s->eapol,
  266. -1, atoi(value), -1, -1);
  267. } else if (os_strcasecmp(cmd, "EAPOL::startPeriod") == 0) {
  268. eapol_sm_configure(wpa_s->eapol,
  269. -1, -1, atoi(value), -1);
  270. } else if (os_strcasecmp(cmd, "EAPOL::maxStart") == 0) {
  271. eapol_sm_configure(wpa_s->eapol,
  272. -1, -1, -1, atoi(value));
  273. } else if (os_strcasecmp(cmd, "dot11RSNAConfigPMKLifetime") == 0) {
  274. if (wpa_sm_set_param(wpa_s->wpa, RSNA_PMK_LIFETIME,
  275. atoi(value)))
  276. ret = -1;
  277. } else if (os_strcasecmp(cmd, "dot11RSNAConfigPMKReauthThreshold") ==
  278. 0) {
  279. if (wpa_sm_set_param(wpa_s->wpa, RSNA_PMK_REAUTH_THRESHOLD,
  280. atoi(value)))
  281. ret = -1;
  282. } else if (os_strcasecmp(cmd, "dot11RSNAConfigSATimeout") == 0) {
  283. if (wpa_sm_set_param(wpa_s->wpa, RSNA_SA_TIMEOUT, atoi(value)))
  284. ret = -1;
  285. } else if (os_strcasecmp(cmd, "wps_fragment_size") == 0) {
  286. wpa_s->wps_fragment_size = atoi(value);
  287. #ifdef CONFIG_WPS_TESTING
  288. } else if (os_strcasecmp(cmd, "wps_version_number") == 0) {
  289. long int val;
  290. val = strtol(value, NULL, 0);
  291. if (val < 0 || val > 0xff) {
  292. ret = -1;
  293. wpa_printf(MSG_DEBUG, "WPS: Invalid "
  294. "wps_version_number %ld", val);
  295. } else {
  296. wps_version_number = val;
  297. wpa_printf(MSG_DEBUG, "WPS: Testing - force WPS "
  298. "version %u.%u",
  299. (wps_version_number & 0xf0) >> 4,
  300. wps_version_number & 0x0f);
  301. }
  302. } else if (os_strcasecmp(cmd, "wps_testing_dummy_cred") == 0) {
  303. wps_testing_dummy_cred = atoi(value);
  304. wpa_printf(MSG_DEBUG, "WPS: Testing - dummy_cred=%d",
  305. wps_testing_dummy_cred);
  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_TESTING
  311. } else if (os_strcasecmp(cmd, "tdls_testing") == 0) {
  312. extern unsigned int tdls_testing;
  313. tdls_testing = strtol(value, NULL, 0);
  314. wpa_printf(MSG_DEBUG, "TDLS: tdls_testing=0x%x", tdls_testing);
  315. #endif /* CONFIG_TDLS_TESTING */
  316. #ifdef CONFIG_TDLS
  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. if (atoi(value))
  329. ret = pno_start(wpa_s);
  330. else
  331. ret = pno_stop(wpa_s);
  332. } else if (os_strcasecmp(cmd, "radio_disabled") == 0) {
  333. int disabled = atoi(value);
  334. if (wpa_drv_radio_disable(wpa_s, disabled) < 0)
  335. ret = -1;
  336. else if (disabled)
  337. wpa_supplicant_set_state(wpa_s, WPA_INACTIVE);
  338. } else if (os_strcasecmp(cmd, "uapsd") == 0) {
  339. if (os_strcmp(value, "disable") == 0)
  340. wpa_s->set_sta_uapsd = 0;
  341. else {
  342. int be, bk, vi, vo;
  343. char *pos;
  344. /* format: BE,BK,VI,VO;max SP Length */
  345. be = atoi(value);
  346. pos = os_strchr(value, ',');
  347. if (pos == NULL)
  348. return -1;
  349. pos++;
  350. bk = atoi(pos);
  351. pos = os_strchr(pos, ',');
  352. if (pos == NULL)
  353. return -1;
  354. pos++;
  355. vi = atoi(pos);
  356. pos = os_strchr(pos, ',');
  357. if (pos == NULL)
  358. return -1;
  359. pos++;
  360. vo = atoi(pos);
  361. /* ignore max SP Length for now */
  362. wpa_s->set_sta_uapsd = 1;
  363. wpa_s->sta_uapsd = 0;
  364. if (be)
  365. wpa_s->sta_uapsd |= BIT(0);
  366. if (bk)
  367. wpa_s->sta_uapsd |= BIT(1);
  368. if (vi)
  369. wpa_s->sta_uapsd |= BIT(2);
  370. if (vo)
  371. wpa_s->sta_uapsd |= BIT(3);
  372. }
  373. } else if (os_strcasecmp(cmd, "ps") == 0) {
  374. ret = wpa_drv_set_p2p_powersave(wpa_s, atoi(value), -1, -1);
  375. #ifdef CONFIG_WIFI_DISPLAY
  376. } else if (os_strcasecmp(cmd, "wifi_display") == 0) {
  377. wifi_display_enable(wpa_s->global, !!atoi(value));
  378. #endif /* CONFIG_WIFI_DISPLAY */
  379. } else if (os_strcasecmp(cmd, "bssid_filter") == 0) {
  380. ret = set_bssid_filter(wpa_s, value);
  381. } else if (os_strcasecmp(cmd, "disallow_aps") == 0) {
  382. ret = set_disallow_aps(wpa_s, value);
  383. } else if (os_strcasecmp(cmd, "no_keep_alive") == 0) {
  384. wpa_s->no_keep_alive = !!atoi(value);
  385. } else {
  386. value[-1] = '=';
  387. ret = wpa_config_process_global(wpa_s->conf, cmd, -1);
  388. if (ret == 0)
  389. wpa_supplicant_update_config(wpa_s);
  390. }
  391. return ret;
  392. }
  393. static int wpa_supplicant_ctrl_iface_get(struct wpa_supplicant *wpa_s,
  394. char *cmd, char *buf, size_t buflen)
  395. {
  396. int res = -1;
  397. wpa_printf(MSG_DEBUG, "CTRL_IFACE GET '%s'", cmd);
  398. if (os_strcmp(cmd, "version") == 0) {
  399. res = os_snprintf(buf, buflen, "%s", VERSION_STR);
  400. } else if (os_strcasecmp(cmd, "country") == 0) {
  401. if (wpa_s->conf->country[0] && wpa_s->conf->country[1])
  402. res = os_snprintf(buf, buflen, "%c%c",
  403. wpa_s->conf->country[0],
  404. wpa_s->conf->country[1]);
  405. #ifdef CONFIG_WIFI_DISPLAY
  406. } else if (os_strcasecmp(cmd, "wifi_display") == 0) {
  407. res = os_snprintf(buf, buflen, "%d",
  408. wpa_s->global->wifi_display);
  409. if (res < 0 || (unsigned int) res >= buflen)
  410. return -1;
  411. return res;
  412. #endif /* CONFIG_WIFI_DISPLAY */
  413. }
  414. if (res < 0 || (unsigned int) res >= buflen)
  415. return -1;
  416. return res;
  417. }
  418. #ifdef IEEE8021X_EAPOL
  419. static int wpa_supplicant_ctrl_iface_preauth(struct wpa_supplicant *wpa_s,
  420. char *addr)
  421. {
  422. u8 bssid[ETH_ALEN];
  423. struct wpa_ssid *ssid = wpa_s->current_ssid;
  424. if (hwaddr_aton(addr, bssid)) {
  425. wpa_printf(MSG_DEBUG, "CTRL_IFACE PREAUTH: invalid address "
  426. "'%s'", addr);
  427. return -1;
  428. }
  429. wpa_printf(MSG_DEBUG, "CTRL_IFACE PREAUTH " MACSTR, MAC2STR(bssid));
  430. rsn_preauth_deinit(wpa_s->wpa);
  431. if (rsn_preauth_init(wpa_s->wpa, bssid, ssid ? &ssid->eap : NULL))
  432. return -1;
  433. return 0;
  434. }
  435. #endif /* IEEE8021X_EAPOL */
  436. #ifdef CONFIG_PEERKEY
  437. /* MLME-STKSTART.request(peer) */
  438. static int wpa_supplicant_ctrl_iface_stkstart(
  439. struct wpa_supplicant *wpa_s, char *addr)
  440. {
  441. u8 peer[ETH_ALEN];
  442. if (hwaddr_aton(addr, peer)) {
  443. wpa_printf(MSG_DEBUG, "CTRL_IFACE STKSTART: invalid "
  444. "address '%s'", addr);
  445. return -1;
  446. }
  447. wpa_printf(MSG_DEBUG, "CTRL_IFACE STKSTART " MACSTR,
  448. MAC2STR(peer));
  449. return wpa_sm_stkstart(wpa_s->wpa, peer);
  450. }
  451. #endif /* CONFIG_PEERKEY */
  452. #ifdef CONFIG_TDLS
  453. static int wpa_supplicant_ctrl_iface_tdls_discover(
  454. struct wpa_supplicant *wpa_s, char *addr)
  455. {
  456. u8 peer[ETH_ALEN];
  457. int ret;
  458. if (hwaddr_aton(addr, peer)) {
  459. wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_DISCOVER: invalid "
  460. "address '%s'", addr);
  461. return -1;
  462. }
  463. wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_DISCOVER " MACSTR,
  464. MAC2STR(peer));
  465. if (wpa_tdls_is_external_setup(wpa_s->wpa))
  466. ret = wpa_tdls_send_discovery_request(wpa_s->wpa, peer);
  467. else
  468. ret = wpa_drv_tdls_oper(wpa_s, TDLS_DISCOVERY_REQ, peer);
  469. return ret;
  470. }
  471. static int wpa_supplicant_ctrl_iface_tdls_setup(
  472. struct wpa_supplicant *wpa_s, char *addr)
  473. {
  474. u8 peer[ETH_ALEN];
  475. int ret;
  476. if (hwaddr_aton(addr, peer)) {
  477. wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_SETUP: invalid "
  478. "address '%s'", addr);
  479. return -1;
  480. }
  481. wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_SETUP " MACSTR,
  482. MAC2STR(peer));
  483. wpa_tdls_remove(wpa_s->wpa, peer);
  484. if (wpa_tdls_is_external_setup(wpa_s->wpa))
  485. ret = wpa_tdls_start(wpa_s->wpa, peer);
  486. else
  487. ret = wpa_drv_tdls_oper(wpa_s, TDLS_SETUP, peer);
  488. return ret;
  489. }
  490. static int wpa_supplicant_ctrl_iface_tdls_teardown(
  491. struct wpa_supplicant *wpa_s, char *addr)
  492. {
  493. u8 peer[ETH_ALEN];
  494. if (hwaddr_aton(addr, peer)) {
  495. wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_TEARDOWN: invalid "
  496. "address '%s'", addr);
  497. return -1;
  498. }
  499. wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_TEARDOWN " MACSTR,
  500. MAC2STR(peer));
  501. return wpa_tdls_teardown_link(wpa_s->wpa, peer,
  502. WLAN_REASON_TDLS_TEARDOWN_UNSPECIFIED);
  503. }
  504. #endif /* CONFIG_TDLS */
  505. #ifdef CONFIG_IEEE80211R
  506. static int wpa_supplicant_ctrl_iface_ft_ds(
  507. struct wpa_supplicant *wpa_s, char *addr)
  508. {
  509. u8 target_ap[ETH_ALEN];
  510. struct wpa_bss *bss;
  511. const u8 *mdie;
  512. if (hwaddr_aton(addr, target_ap)) {
  513. wpa_printf(MSG_DEBUG, "CTRL_IFACE FT_DS: invalid "
  514. "address '%s'", addr);
  515. return -1;
  516. }
  517. wpa_printf(MSG_DEBUG, "CTRL_IFACE FT_DS " MACSTR, MAC2STR(target_ap));
  518. bss = wpa_bss_get_bssid(wpa_s, target_ap);
  519. if (bss)
  520. mdie = wpa_bss_get_ie(bss, WLAN_EID_MOBILITY_DOMAIN);
  521. else
  522. mdie = NULL;
  523. return wpa_ft_start_over_ds(wpa_s->wpa, target_ap, mdie);
  524. }
  525. #endif /* CONFIG_IEEE80211R */
  526. #ifdef CONFIG_WPS
  527. static int wpa_supplicant_ctrl_iface_wps_pbc(struct wpa_supplicant *wpa_s,
  528. char *cmd)
  529. {
  530. u8 bssid[ETH_ALEN], *_bssid = bssid;
  531. #ifdef CONFIG_P2P
  532. u8 p2p_dev_addr[ETH_ALEN];
  533. #endif /* CONFIG_P2P */
  534. #ifdef CONFIG_AP
  535. u8 *_p2p_dev_addr = NULL;
  536. #endif /* CONFIG_AP */
  537. if (cmd == NULL || os_strcmp(cmd, "any") == 0) {
  538. _bssid = NULL;
  539. #ifdef CONFIG_P2P
  540. } else if (os_strncmp(cmd, "p2p_dev_addr=", 13) == 0) {
  541. if (hwaddr_aton(cmd + 13, p2p_dev_addr)) {
  542. wpa_printf(MSG_DEBUG, "CTRL_IFACE WPS_PBC: invalid "
  543. "P2P Device Address '%s'",
  544. cmd + 13);
  545. return -1;
  546. }
  547. _p2p_dev_addr = p2p_dev_addr;
  548. #endif /* CONFIG_P2P */
  549. } else if (hwaddr_aton(cmd, bssid)) {
  550. wpa_printf(MSG_DEBUG, "CTRL_IFACE WPS_PBC: invalid BSSID '%s'",
  551. cmd);
  552. return -1;
  553. }
  554. #ifdef CONFIG_AP
  555. if (wpa_s->ap_iface)
  556. return wpa_supplicant_ap_wps_pbc(wpa_s, _bssid, _p2p_dev_addr);
  557. #endif /* CONFIG_AP */
  558. return wpas_wps_start_pbc(wpa_s, _bssid, 0);
  559. }
  560. static int wpa_supplicant_ctrl_iface_wps_pin(struct wpa_supplicant *wpa_s,
  561. char *cmd, char *buf,
  562. size_t buflen)
  563. {
  564. u8 bssid[ETH_ALEN], *_bssid = bssid;
  565. char *pin;
  566. int ret;
  567. pin = os_strchr(cmd, ' ');
  568. if (pin)
  569. *pin++ = '\0';
  570. if (os_strcmp(cmd, "any") == 0)
  571. _bssid = NULL;
  572. else if (os_strcmp(cmd, "get") == 0) {
  573. ret = wps_generate_pin();
  574. goto done;
  575. } else if (hwaddr_aton(cmd, bssid)) {
  576. wpa_printf(MSG_DEBUG, "CTRL_IFACE WPS_PIN: invalid BSSID '%s'",
  577. cmd);
  578. return -1;
  579. }
  580. #ifdef CONFIG_AP
  581. if (wpa_s->ap_iface) {
  582. int timeout = 0;
  583. char *pos;
  584. if (pin) {
  585. pos = os_strchr(pin, ' ');
  586. if (pos) {
  587. *pos++ = '\0';
  588. timeout = atoi(pos);
  589. }
  590. }
  591. return wpa_supplicant_ap_wps_pin(wpa_s, _bssid, pin,
  592. buf, buflen, timeout);
  593. }
  594. #endif /* CONFIG_AP */
  595. if (pin) {
  596. ret = wpas_wps_start_pin(wpa_s, _bssid, pin, 0,
  597. DEV_PW_DEFAULT);
  598. if (ret < 0)
  599. return -1;
  600. ret = os_snprintf(buf, buflen, "%s", pin);
  601. if (ret < 0 || (size_t) ret >= buflen)
  602. return -1;
  603. return ret;
  604. }
  605. ret = wpas_wps_start_pin(wpa_s, _bssid, NULL, 0, DEV_PW_DEFAULT);
  606. if (ret < 0)
  607. return -1;
  608. done:
  609. /* Return the generated PIN */
  610. ret = os_snprintf(buf, buflen, "%08d", ret);
  611. if (ret < 0 || (size_t) ret >= buflen)
  612. return -1;
  613. return ret;
  614. }
  615. static int wpa_supplicant_ctrl_iface_wps_check_pin(
  616. struct wpa_supplicant *wpa_s, char *cmd, char *buf, size_t buflen)
  617. {
  618. char pin[9];
  619. size_t len;
  620. char *pos;
  621. int ret;
  622. wpa_hexdump_ascii_key(MSG_DEBUG, "WPS_CHECK_PIN",
  623. (u8 *) cmd, os_strlen(cmd));
  624. for (pos = cmd, len = 0; *pos != '\0'; pos++) {
  625. if (*pos < '0' || *pos > '9')
  626. continue;
  627. pin[len++] = *pos;
  628. if (len == 9) {
  629. wpa_printf(MSG_DEBUG, "WPS: Too long PIN");
  630. return -1;
  631. }
  632. }
  633. if (len != 4 && len != 8) {
  634. wpa_printf(MSG_DEBUG, "WPS: Invalid PIN length %d", (int) len);
  635. return -1;
  636. }
  637. pin[len] = '\0';
  638. if (len == 8) {
  639. unsigned int pin_val;
  640. pin_val = atoi(pin);
  641. if (!wps_pin_valid(pin_val)) {
  642. wpa_printf(MSG_DEBUG, "WPS: Invalid checksum digit");
  643. ret = os_snprintf(buf, buflen, "FAIL-CHECKSUM\n");
  644. if (ret < 0 || (size_t) ret >= buflen)
  645. return -1;
  646. return ret;
  647. }
  648. }
  649. ret = os_snprintf(buf, buflen, "%s", pin);
  650. if (ret < 0 || (size_t) ret >= buflen)
  651. return -1;
  652. return ret;
  653. }
  654. #ifdef CONFIG_WPS_NFC
  655. static int wpa_supplicant_ctrl_iface_wps_nfc(struct wpa_supplicant *wpa_s,
  656. char *cmd)
  657. {
  658. u8 bssid[ETH_ALEN], *_bssid = bssid;
  659. if (cmd == NULL || cmd[0] == '\0')
  660. _bssid = NULL;
  661. else if (hwaddr_aton(cmd, bssid))
  662. return -1;
  663. return wpas_wps_start_nfc(wpa_s, _bssid);
  664. }
  665. static int wpa_supplicant_ctrl_iface_wps_nfc_config_token(
  666. struct wpa_supplicant *wpa_s, char *cmd, char *reply, size_t max_len)
  667. {
  668. int ndef;
  669. struct wpabuf *buf;
  670. int res;
  671. char *pos;
  672. pos = os_strchr(cmd, ' ');
  673. if (pos)
  674. *pos++ = '\0';
  675. if (os_strcmp(cmd, "WPS") == 0)
  676. ndef = 0;
  677. else if (os_strcmp(cmd, "NDEF") == 0)
  678. ndef = 1;
  679. else
  680. return -1;
  681. buf = wpas_wps_nfc_config_token(wpa_s, ndef, pos);
  682. if (buf == NULL)
  683. return -1;
  684. res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
  685. wpabuf_len(buf));
  686. reply[res++] = '\n';
  687. reply[res] = '\0';
  688. wpabuf_free(buf);
  689. return res;
  690. }
  691. static int wpa_supplicant_ctrl_iface_wps_nfc_token(
  692. struct wpa_supplicant *wpa_s, char *cmd, char *reply, size_t max_len)
  693. {
  694. int ndef;
  695. struct wpabuf *buf;
  696. int res;
  697. if (os_strcmp(cmd, "WPS") == 0)
  698. ndef = 0;
  699. else if (os_strcmp(cmd, "NDEF") == 0)
  700. ndef = 1;
  701. else
  702. return -1;
  703. buf = wpas_wps_nfc_token(wpa_s, ndef);
  704. if (buf == NULL)
  705. return -1;
  706. res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
  707. wpabuf_len(buf));
  708. reply[res++] = '\n';
  709. reply[res] = '\0';
  710. wpabuf_free(buf);
  711. return res;
  712. }
  713. static int wpa_supplicant_ctrl_iface_wps_nfc_tag_read(
  714. struct wpa_supplicant *wpa_s, char *pos)
  715. {
  716. size_t len;
  717. struct wpabuf *buf;
  718. int ret;
  719. len = os_strlen(pos);
  720. if (len & 0x01)
  721. return -1;
  722. len /= 2;
  723. buf = wpabuf_alloc(len);
  724. if (buf == NULL)
  725. return -1;
  726. if (hexstr2bin(pos, wpabuf_put(buf, len), len) < 0) {
  727. wpabuf_free(buf);
  728. return -1;
  729. }
  730. ret = wpas_wps_nfc_tag_read(wpa_s, buf);
  731. wpabuf_free(buf);
  732. return ret;
  733. }
  734. static int wpas_ctrl_nfc_get_handover_req_wps(struct wpa_supplicant *wpa_s,
  735. char *reply, size_t max_len,
  736. int cr)
  737. {
  738. struct wpabuf *buf;
  739. int res;
  740. buf = wpas_wps_nfc_handover_req(wpa_s, cr);
  741. if (buf == NULL)
  742. return -1;
  743. res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
  744. wpabuf_len(buf));
  745. reply[res++] = '\n';
  746. reply[res] = '\0';
  747. wpabuf_free(buf);
  748. return res;
  749. }
  750. static int wpas_ctrl_nfc_get_handover_req(struct wpa_supplicant *wpa_s,
  751. char *cmd, char *reply,
  752. size_t max_len)
  753. {
  754. char *pos;
  755. pos = os_strchr(cmd, ' ');
  756. if (pos == NULL)
  757. return -1;
  758. *pos++ = '\0';
  759. if (os_strcmp(cmd, "NDEF") != 0)
  760. return -1;
  761. if (os_strcmp(pos, "WPS") == 0 || os_strcmp(pos, "WPS-CR") == 0) {
  762. return wpas_ctrl_nfc_get_handover_req_wps(
  763. wpa_s, reply, max_len, os_strcmp(pos, "WPS-CR") == 0);
  764. }
  765. return -1;
  766. }
  767. static int wpas_ctrl_nfc_get_handover_sel_wps(struct wpa_supplicant *wpa_s,
  768. char *reply, size_t max_len,
  769. int ndef, int cr, char *uuid)
  770. {
  771. struct wpabuf *buf;
  772. int res;
  773. buf = wpas_wps_nfc_handover_sel(wpa_s, ndef, cr, uuid);
  774. if (buf == NULL)
  775. return -1;
  776. res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
  777. wpabuf_len(buf));
  778. reply[res++] = '\n';
  779. reply[res] = '\0';
  780. wpabuf_free(buf);
  781. return res;
  782. }
  783. static int wpas_ctrl_nfc_get_handover_sel(struct wpa_supplicant *wpa_s,
  784. char *cmd, char *reply,
  785. size_t max_len)
  786. {
  787. char *pos, *pos2;
  788. int ndef;
  789. pos = os_strchr(cmd, ' ');
  790. if (pos == NULL)
  791. return -1;
  792. *pos++ = '\0';
  793. if (os_strcmp(cmd, "WPS") == 0)
  794. ndef = 0;
  795. else if (os_strcmp(cmd, "NDEF") == 0)
  796. ndef = 1;
  797. else
  798. return -1;
  799. pos2 = os_strchr(pos, ' ');
  800. if (pos2)
  801. *pos2++ = '\0';
  802. if (os_strcmp(pos, "WPS") == 0 || os_strcmp(pos, "WPS-CR") == 0) {
  803. return wpas_ctrl_nfc_get_handover_sel_wps(
  804. wpa_s, reply, max_len, ndef,
  805. os_strcmp(pos, "WPS-CR") == 0, pos2);
  806. }
  807. return -1;
  808. }
  809. static int wpas_ctrl_nfc_rx_handover_req(struct wpa_supplicant *wpa_s,
  810. char *cmd, char *reply,
  811. size_t max_len)
  812. {
  813. size_t len;
  814. struct wpabuf *buf;
  815. int ret;
  816. len = os_strlen(cmd);
  817. if (len & 0x01)
  818. return -1;
  819. len /= 2;
  820. buf = wpabuf_alloc(len);
  821. if (buf == NULL)
  822. return -1;
  823. if (hexstr2bin(cmd, wpabuf_put(buf, len), len) < 0) {
  824. wpabuf_free(buf);
  825. return -1;
  826. }
  827. ret = wpas_wps_nfc_rx_handover_req(wpa_s, buf);
  828. wpabuf_free(buf);
  829. return ret;
  830. }
  831. static int wpas_ctrl_nfc_rx_handover_sel(struct wpa_supplicant *wpa_s,
  832. char *cmd)
  833. {
  834. size_t len;
  835. struct wpabuf *buf;
  836. int ret;
  837. len = os_strlen(cmd);
  838. if (len & 0x01)
  839. return -1;
  840. len /= 2;
  841. buf = wpabuf_alloc(len);
  842. if (buf == NULL)
  843. return -1;
  844. if (hexstr2bin(cmd, wpabuf_put(buf, len), len) < 0) {
  845. wpabuf_free(buf);
  846. return -1;
  847. }
  848. ret = wpas_wps_nfc_rx_handover_sel(wpa_s, buf);
  849. wpabuf_free(buf);
  850. return ret;
  851. }
  852. static int wpas_ctrl_nfc_report_handover(struct wpa_supplicant *wpa_s,
  853. char *cmd)
  854. {
  855. size_t len;
  856. struct wpabuf *req, *sel;
  857. int ret;
  858. char *pos, *role, *type, *pos2;
  859. role = cmd;
  860. pos = os_strchr(role, ' ');
  861. if (pos == NULL)
  862. return -1;
  863. *pos++ = '\0';
  864. type = pos;
  865. pos = os_strchr(type, ' ');
  866. if (pos == NULL)
  867. return -1;
  868. *pos++ = '\0';
  869. pos2 = os_strchr(pos, ' ');
  870. if (pos2 == NULL)
  871. return -1;
  872. *pos2++ = '\0';
  873. len = os_strlen(pos);
  874. if (len & 0x01)
  875. return -1;
  876. len /= 2;
  877. req = wpabuf_alloc(len);
  878. if (req == NULL)
  879. return -1;
  880. if (hexstr2bin(pos, wpabuf_put(req, len), len) < 0) {
  881. wpabuf_free(req);
  882. return -1;
  883. }
  884. len = os_strlen(pos2);
  885. if (len & 0x01) {
  886. wpabuf_free(req);
  887. return -1;
  888. }
  889. len /= 2;
  890. sel = wpabuf_alloc(len);
  891. if (sel == NULL) {
  892. wpabuf_free(req);
  893. return -1;
  894. }
  895. if (hexstr2bin(pos2, wpabuf_put(sel, len), len) < 0) {
  896. wpabuf_free(req);
  897. wpabuf_free(sel);
  898. return -1;
  899. }
  900. if (os_strcmp(role, "INIT") == 0 && os_strcmp(type, "WPS") == 0) {
  901. ret = wpas_wps_nfc_report_handover(wpa_s, req, sel);
  902. } else {
  903. wpa_printf(MSG_DEBUG, "NFC: Unsupported connection handover "
  904. "reported: role=%s type=%s", role, type);
  905. ret = -1;
  906. }
  907. wpabuf_free(req);
  908. wpabuf_free(sel);
  909. return ret;
  910. }
  911. #endif /* CONFIG_WPS_NFC */
  912. static int wpa_supplicant_ctrl_iface_wps_reg(struct wpa_supplicant *wpa_s,
  913. char *cmd)
  914. {
  915. u8 bssid[ETH_ALEN];
  916. char *pin;
  917. char *new_ssid;
  918. char *new_auth;
  919. char *new_encr;
  920. char *new_key;
  921. struct wps_new_ap_settings ap;
  922. pin = os_strchr(cmd, ' ');
  923. if (pin == NULL)
  924. return -1;
  925. *pin++ = '\0';
  926. if (hwaddr_aton(cmd, bssid)) {
  927. wpa_printf(MSG_DEBUG, "CTRL_IFACE WPS_REG: invalid BSSID '%s'",
  928. cmd);
  929. return -1;
  930. }
  931. new_ssid = os_strchr(pin, ' ');
  932. if (new_ssid == NULL)
  933. return wpas_wps_start_reg(wpa_s, bssid, pin, NULL);
  934. *new_ssid++ = '\0';
  935. new_auth = os_strchr(new_ssid, ' ');
  936. if (new_auth == NULL)
  937. return -1;
  938. *new_auth++ = '\0';
  939. new_encr = os_strchr(new_auth, ' ');
  940. if (new_encr == NULL)
  941. return -1;
  942. *new_encr++ = '\0';
  943. new_key = os_strchr(new_encr, ' ');
  944. if (new_key == NULL)
  945. return -1;
  946. *new_key++ = '\0';
  947. os_memset(&ap, 0, sizeof(ap));
  948. ap.ssid_hex = new_ssid;
  949. ap.auth = new_auth;
  950. ap.encr = new_encr;
  951. ap.key_hex = new_key;
  952. return wpas_wps_start_reg(wpa_s, bssid, pin, &ap);
  953. }
  954. #ifdef CONFIG_AP
  955. static int wpa_supplicant_ctrl_iface_wps_ap_pin(struct wpa_supplicant *wpa_s,
  956. char *cmd, char *buf,
  957. size_t buflen)
  958. {
  959. int timeout = 300;
  960. char *pos;
  961. const char *pin_txt;
  962. if (!wpa_s->ap_iface)
  963. return -1;
  964. pos = os_strchr(cmd, ' ');
  965. if (pos)
  966. *pos++ = '\0';
  967. if (os_strcmp(cmd, "disable") == 0) {
  968. wpas_wps_ap_pin_disable(wpa_s);
  969. return os_snprintf(buf, buflen, "OK\n");
  970. }
  971. if (os_strcmp(cmd, "random") == 0) {
  972. if (pos)
  973. timeout = atoi(pos);
  974. pin_txt = wpas_wps_ap_pin_random(wpa_s, timeout);
  975. if (pin_txt == NULL)
  976. return -1;
  977. return os_snprintf(buf, buflen, "%s", pin_txt);
  978. }
  979. if (os_strcmp(cmd, "get") == 0) {
  980. pin_txt = wpas_wps_ap_pin_get(wpa_s);
  981. if (pin_txt == NULL)
  982. return -1;
  983. return os_snprintf(buf, buflen, "%s", pin_txt);
  984. }
  985. if (os_strcmp(cmd, "set") == 0) {
  986. char *pin;
  987. if (pos == NULL)
  988. return -1;
  989. pin = pos;
  990. pos = os_strchr(pos, ' ');
  991. if (pos) {
  992. *pos++ = '\0';
  993. timeout = atoi(pos);
  994. }
  995. if (os_strlen(pin) > buflen)
  996. return -1;
  997. if (wpas_wps_ap_pin_set(wpa_s, pin, timeout) < 0)
  998. return -1;
  999. return os_snprintf(buf, buflen, "%s", pin);
  1000. }
  1001. return -1;
  1002. }
  1003. #endif /* CONFIG_AP */
  1004. #ifdef CONFIG_WPS_ER
  1005. static int wpa_supplicant_ctrl_iface_wps_er_pin(struct wpa_supplicant *wpa_s,
  1006. char *cmd)
  1007. {
  1008. char *uuid = cmd, *pin, *pos;
  1009. u8 addr_buf[ETH_ALEN], *addr = NULL;
  1010. pin = os_strchr(uuid, ' ');
  1011. if (pin == NULL)
  1012. return -1;
  1013. *pin++ = '\0';
  1014. pos = os_strchr(pin, ' ');
  1015. if (pos) {
  1016. *pos++ = '\0';
  1017. if (hwaddr_aton(pos, addr_buf) == 0)
  1018. addr = addr_buf;
  1019. }
  1020. return wpas_wps_er_add_pin(wpa_s, addr, uuid, pin);
  1021. }
  1022. static int wpa_supplicant_ctrl_iface_wps_er_learn(struct wpa_supplicant *wpa_s,
  1023. char *cmd)
  1024. {
  1025. char *uuid = cmd, *pin;
  1026. pin = os_strchr(uuid, ' ');
  1027. if (pin == NULL)
  1028. return -1;
  1029. *pin++ = '\0';
  1030. return wpas_wps_er_learn(wpa_s, uuid, pin);
  1031. }
  1032. static int wpa_supplicant_ctrl_iface_wps_er_set_config(
  1033. struct wpa_supplicant *wpa_s, char *cmd)
  1034. {
  1035. char *uuid = cmd, *id;
  1036. id = os_strchr(uuid, ' ');
  1037. if (id == NULL)
  1038. return -1;
  1039. *id++ = '\0';
  1040. return wpas_wps_er_set_config(wpa_s, uuid, atoi(id));
  1041. }
  1042. static int wpa_supplicant_ctrl_iface_wps_er_config(
  1043. struct wpa_supplicant *wpa_s, char *cmd)
  1044. {
  1045. char *pin;
  1046. char *new_ssid;
  1047. char *new_auth;
  1048. char *new_encr;
  1049. char *new_key;
  1050. struct wps_new_ap_settings ap;
  1051. pin = os_strchr(cmd, ' ');
  1052. if (pin == NULL)
  1053. return -1;
  1054. *pin++ = '\0';
  1055. new_ssid = os_strchr(pin, ' ');
  1056. if (new_ssid == NULL)
  1057. return -1;
  1058. *new_ssid++ = '\0';
  1059. new_auth = os_strchr(new_ssid, ' ');
  1060. if (new_auth == NULL)
  1061. return -1;
  1062. *new_auth++ = '\0';
  1063. new_encr = os_strchr(new_auth, ' ');
  1064. if (new_encr == NULL)
  1065. return -1;
  1066. *new_encr++ = '\0';
  1067. new_key = os_strchr(new_encr, ' ');
  1068. if (new_key == NULL)
  1069. return -1;
  1070. *new_key++ = '\0';
  1071. os_memset(&ap, 0, sizeof(ap));
  1072. ap.ssid_hex = new_ssid;
  1073. ap.auth = new_auth;
  1074. ap.encr = new_encr;
  1075. ap.key_hex = new_key;
  1076. return wpas_wps_er_config(wpa_s, cmd, pin, &ap);
  1077. }
  1078. #ifdef CONFIG_WPS_NFC
  1079. static int wpa_supplicant_ctrl_iface_wps_er_nfc_config_token(
  1080. struct wpa_supplicant *wpa_s, char *cmd, char *reply, size_t max_len)
  1081. {
  1082. int ndef;
  1083. struct wpabuf *buf;
  1084. int res;
  1085. char *uuid;
  1086. uuid = os_strchr(cmd, ' ');
  1087. if (uuid == NULL)
  1088. return -1;
  1089. *uuid++ = '\0';
  1090. if (os_strcmp(cmd, "WPS") == 0)
  1091. ndef = 0;
  1092. else if (os_strcmp(cmd, "NDEF") == 0)
  1093. ndef = 1;
  1094. else
  1095. return -1;
  1096. buf = wpas_wps_er_nfc_config_token(wpa_s, ndef, uuid);
  1097. if (buf == NULL)
  1098. return -1;
  1099. res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
  1100. wpabuf_len(buf));
  1101. reply[res++] = '\n';
  1102. reply[res] = '\0';
  1103. wpabuf_free(buf);
  1104. return res;
  1105. }
  1106. #endif /* CONFIG_WPS_NFC */
  1107. #endif /* CONFIG_WPS_ER */
  1108. #endif /* CONFIG_WPS */
  1109. #ifdef CONFIG_IBSS_RSN
  1110. static int wpa_supplicant_ctrl_iface_ibss_rsn(
  1111. struct wpa_supplicant *wpa_s, char *addr)
  1112. {
  1113. u8 peer[ETH_ALEN];
  1114. if (hwaddr_aton(addr, peer)) {
  1115. wpa_printf(MSG_DEBUG, "CTRL_IFACE IBSS_RSN: invalid "
  1116. "address '%s'", addr);
  1117. return -1;
  1118. }
  1119. wpa_printf(MSG_DEBUG, "CTRL_IFACE IBSS_RSN " MACSTR,
  1120. MAC2STR(peer));
  1121. return ibss_rsn_start(wpa_s->ibss_rsn, peer);
  1122. }
  1123. #endif /* CONFIG_IBSS_RSN */
  1124. static int wpa_supplicant_ctrl_iface_ctrl_rsp(struct wpa_supplicant *wpa_s,
  1125. char *rsp)
  1126. {
  1127. #ifdef IEEE8021X_EAPOL
  1128. char *pos, *id_pos;
  1129. int id;
  1130. struct wpa_ssid *ssid;
  1131. pos = os_strchr(rsp, '-');
  1132. if (pos == NULL)
  1133. return -1;
  1134. *pos++ = '\0';
  1135. id_pos = pos;
  1136. pos = os_strchr(pos, ':');
  1137. if (pos == NULL)
  1138. return -1;
  1139. *pos++ = '\0';
  1140. id = atoi(id_pos);
  1141. wpa_printf(MSG_DEBUG, "CTRL_IFACE: field=%s id=%d", rsp, id);
  1142. wpa_hexdump_ascii_key(MSG_DEBUG, "CTRL_IFACE: value",
  1143. (u8 *) pos, os_strlen(pos));
  1144. ssid = wpa_config_get_network(wpa_s->conf, id);
  1145. if (ssid == NULL) {
  1146. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find SSID id=%d "
  1147. "to update", id);
  1148. return -1;
  1149. }
  1150. return wpa_supplicant_ctrl_iface_ctrl_rsp_handle(wpa_s, ssid, rsp,
  1151. pos);
  1152. #else /* IEEE8021X_EAPOL */
  1153. wpa_printf(MSG_DEBUG, "CTRL_IFACE: 802.1X not included");
  1154. return -1;
  1155. #endif /* IEEE8021X_EAPOL */
  1156. }
  1157. static int wpa_supplicant_ctrl_iface_status(struct wpa_supplicant *wpa_s,
  1158. const char *params,
  1159. char *buf, size_t buflen)
  1160. {
  1161. char *pos, *end, tmp[30];
  1162. int res, verbose, wps, ret;
  1163. verbose = os_strcmp(params, "-VERBOSE") == 0;
  1164. wps = os_strcmp(params, "-WPS") == 0;
  1165. pos = buf;
  1166. end = buf + buflen;
  1167. if (wpa_s->wpa_state >= WPA_ASSOCIATED) {
  1168. struct wpa_ssid *ssid = wpa_s->current_ssid;
  1169. ret = os_snprintf(pos, end - pos, "bssid=" MACSTR "\n",
  1170. MAC2STR(wpa_s->bssid));
  1171. if (ret < 0 || ret >= end - pos)
  1172. return pos - buf;
  1173. pos += ret;
  1174. if (ssid) {
  1175. u8 *_ssid = ssid->ssid;
  1176. size_t ssid_len = ssid->ssid_len;
  1177. u8 ssid_buf[MAX_SSID_LEN];
  1178. if (ssid_len == 0) {
  1179. int _res = wpa_drv_get_ssid(wpa_s, ssid_buf);
  1180. if (_res < 0)
  1181. ssid_len = 0;
  1182. else
  1183. ssid_len = _res;
  1184. _ssid = ssid_buf;
  1185. }
  1186. ret = os_snprintf(pos, end - pos, "ssid=%s\nid=%d\n",
  1187. wpa_ssid_txt(_ssid, ssid_len),
  1188. ssid->id);
  1189. if (ret < 0 || ret >= end - pos)
  1190. return pos - buf;
  1191. pos += ret;
  1192. if (wps && ssid->passphrase &&
  1193. wpa_key_mgmt_wpa_psk(ssid->key_mgmt) &&
  1194. (ssid->mode == WPAS_MODE_AP ||
  1195. ssid->mode == WPAS_MODE_P2P_GO)) {
  1196. ret = os_snprintf(pos, end - pos,
  1197. "passphrase=%s\n",
  1198. ssid->passphrase);
  1199. if (ret < 0 || ret >= end - pos)
  1200. return pos - buf;
  1201. pos += ret;
  1202. }
  1203. if (ssid->id_str) {
  1204. ret = os_snprintf(pos, end - pos,
  1205. "id_str=%s\n",
  1206. ssid->id_str);
  1207. if (ret < 0 || ret >= end - pos)
  1208. return pos - buf;
  1209. pos += ret;
  1210. }
  1211. switch (ssid->mode) {
  1212. case WPAS_MODE_INFRA:
  1213. ret = os_snprintf(pos, end - pos,
  1214. "mode=station\n");
  1215. break;
  1216. case WPAS_MODE_IBSS:
  1217. ret = os_snprintf(pos, end - pos,
  1218. "mode=IBSS\n");
  1219. break;
  1220. case WPAS_MODE_AP:
  1221. ret = os_snprintf(pos, end - pos,
  1222. "mode=AP\n");
  1223. break;
  1224. case WPAS_MODE_P2P_GO:
  1225. ret = os_snprintf(pos, end - pos,
  1226. "mode=P2P GO\n");
  1227. break;
  1228. case WPAS_MODE_P2P_GROUP_FORMATION:
  1229. ret = os_snprintf(pos, end - pos,
  1230. "mode=P2P GO - group "
  1231. "formation\n");
  1232. break;
  1233. default:
  1234. ret = 0;
  1235. break;
  1236. }
  1237. if (ret < 0 || ret >= end - pos)
  1238. return pos - buf;
  1239. pos += ret;
  1240. }
  1241. #ifdef CONFIG_AP
  1242. if (wpa_s->ap_iface) {
  1243. pos += ap_ctrl_iface_wpa_get_status(wpa_s, pos,
  1244. end - pos,
  1245. verbose);
  1246. } else
  1247. #endif /* CONFIG_AP */
  1248. pos += wpa_sm_get_status(wpa_s->wpa, pos, end - pos, verbose);
  1249. }
  1250. #ifdef CONFIG_SAE
  1251. if (wpa_s->wpa_state >= WPA_ASSOCIATED &&
  1252. wpa_s->sme.sae.state == SAE_ACCEPTED && !wpa_s->ap_iface) {
  1253. ret = os_snprintf(pos, end - pos, "sae_group=%d\n",
  1254. wpa_s->sme.sae.group);
  1255. if (ret < 0 || ret >= end - pos)
  1256. return pos - buf;
  1257. pos += ret;
  1258. }
  1259. #endif /* CONFIG_SAE */
  1260. ret = os_snprintf(pos, end - pos, "wpa_state=%s\n",
  1261. wpa_supplicant_state_txt(wpa_s->wpa_state));
  1262. if (ret < 0 || ret >= end - pos)
  1263. return pos - buf;
  1264. pos += ret;
  1265. if (wpa_s->l2 &&
  1266. l2_packet_get_ip_addr(wpa_s->l2, tmp, sizeof(tmp)) >= 0) {
  1267. ret = os_snprintf(pos, end - pos, "ip_address=%s\n", tmp);
  1268. if (ret < 0 || ret >= end - pos)
  1269. return pos - buf;
  1270. pos += ret;
  1271. }
  1272. #ifdef CONFIG_P2P
  1273. if (wpa_s->global->p2p) {
  1274. ret = os_snprintf(pos, end - pos, "p2p_device_address=" MACSTR
  1275. "\n", MAC2STR(wpa_s->global->p2p_dev_addr));
  1276. if (ret < 0 || ret >= end - pos)
  1277. return pos - buf;
  1278. pos += ret;
  1279. }
  1280. #endif /* CONFIG_P2P */
  1281. ret = os_snprintf(pos, end - pos, "address=" MACSTR "\n",
  1282. MAC2STR(wpa_s->own_addr));
  1283. if (ret < 0 || ret >= end - pos)
  1284. return pos - buf;
  1285. pos += ret;
  1286. #ifdef CONFIG_HS20
  1287. if (wpa_s->current_bss &&
  1288. wpa_bss_get_vendor_ie(wpa_s->current_bss, HS20_IE_VENDOR_TYPE) &&
  1289. wpa_s->wpa_proto == WPA_PROTO_RSN &&
  1290. wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt)) {
  1291. ret = os_snprintf(pos, end - pos, "hs20=1\n");
  1292. if (ret < 0 || ret >= end - pos)
  1293. return pos - buf;
  1294. pos += ret;
  1295. }
  1296. if (wpa_s->current_ssid) {
  1297. struct wpa_cred *cred;
  1298. char *type;
  1299. for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
  1300. if (wpa_s->current_ssid->parent_cred != cred)
  1301. continue;
  1302. if (!cred->domain)
  1303. continue;
  1304. ret = os_snprintf(pos, end - pos, "home_sp=%s\n",
  1305. cred->domain);
  1306. if (ret < 0 || ret >= end - pos)
  1307. return pos - buf;
  1308. pos += ret;
  1309. if (wpa_s->current_bss == NULL ||
  1310. wpa_s->current_bss->anqp == NULL)
  1311. res = -1;
  1312. else
  1313. res = interworking_home_sp_cred(
  1314. wpa_s, cred,
  1315. wpa_s->current_bss->anqp->domain_name);
  1316. if (res > 0)
  1317. type = "home";
  1318. else if (res == 0)
  1319. type = "roaming";
  1320. else
  1321. type = "unknown";
  1322. ret = os_snprintf(pos, end - pos, "sp_type=%s\n", type);
  1323. if (ret < 0 || ret >= end - pos)
  1324. return pos - buf;
  1325. pos += ret;
  1326. break;
  1327. }
  1328. }
  1329. #endif /* CONFIG_HS20 */
  1330. if (wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt) ||
  1331. wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
  1332. res = eapol_sm_get_status(wpa_s->eapol, pos, end - pos,
  1333. verbose);
  1334. if (res >= 0)
  1335. pos += res;
  1336. }
  1337. res = rsn_preauth_get_status(wpa_s->wpa, pos, end - pos, verbose);
  1338. if (res >= 0)
  1339. pos += res;
  1340. return pos - buf;
  1341. }
  1342. static int wpa_supplicant_ctrl_iface_bssid(struct wpa_supplicant *wpa_s,
  1343. char *cmd)
  1344. {
  1345. char *pos;
  1346. int id;
  1347. struct wpa_ssid *ssid;
  1348. u8 bssid[ETH_ALEN];
  1349. /* cmd: "<network id> <BSSID>" */
  1350. pos = os_strchr(cmd, ' ');
  1351. if (pos == NULL)
  1352. return -1;
  1353. *pos++ = '\0';
  1354. id = atoi(cmd);
  1355. wpa_printf(MSG_DEBUG, "CTRL_IFACE: id=%d bssid='%s'", id, pos);
  1356. if (hwaddr_aton(pos, bssid)) {
  1357. wpa_printf(MSG_DEBUG ,"CTRL_IFACE: invalid BSSID '%s'", pos);
  1358. return -1;
  1359. }
  1360. ssid = wpa_config_get_network(wpa_s->conf, id);
  1361. if (ssid == NULL) {
  1362. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find SSID id=%d "
  1363. "to update", id);
  1364. return -1;
  1365. }
  1366. os_memcpy(ssid->bssid, bssid, ETH_ALEN);
  1367. ssid->bssid_set = !is_zero_ether_addr(bssid);
  1368. return 0;
  1369. }
  1370. static int wpa_supplicant_ctrl_iface_blacklist(struct wpa_supplicant *wpa_s,
  1371. char *cmd, char *buf,
  1372. size_t buflen)
  1373. {
  1374. u8 bssid[ETH_ALEN];
  1375. struct wpa_blacklist *e;
  1376. char *pos, *end;
  1377. int ret;
  1378. /* cmd: "BLACKLIST [<BSSID>]" */
  1379. if (*cmd == '\0') {
  1380. pos = buf;
  1381. end = buf + buflen;
  1382. e = wpa_s->blacklist;
  1383. while (e) {
  1384. ret = os_snprintf(pos, end - pos, MACSTR "\n",
  1385. MAC2STR(e->bssid));
  1386. if (ret < 0 || ret >= end - pos)
  1387. return pos - buf;
  1388. pos += ret;
  1389. e = e->next;
  1390. }
  1391. return pos - buf;
  1392. }
  1393. cmd++;
  1394. if (os_strncmp(cmd, "clear", 5) == 0) {
  1395. wpa_blacklist_clear(wpa_s);
  1396. os_memcpy(buf, "OK\n", 3);
  1397. return 3;
  1398. }
  1399. wpa_printf(MSG_DEBUG, "CTRL_IFACE: BLACKLIST bssid='%s'", cmd);
  1400. if (hwaddr_aton(cmd, bssid)) {
  1401. wpa_printf(MSG_DEBUG, "CTRL_IFACE: invalid BSSID '%s'", cmd);
  1402. return -1;
  1403. }
  1404. /*
  1405. * Add the BSSID twice, so its count will be 2, causing it to be
  1406. * skipped when processing scan results.
  1407. */
  1408. ret = wpa_blacklist_add(wpa_s, bssid);
  1409. if (ret != 0)
  1410. return -1;
  1411. ret = wpa_blacklist_add(wpa_s, bssid);
  1412. if (ret != 0)
  1413. return -1;
  1414. os_memcpy(buf, "OK\n", 3);
  1415. return 3;
  1416. }
  1417. extern int wpa_debug_level;
  1418. extern int wpa_debug_timestamp;
  1419. static const char * debug_level_str(int level)
  1420. {
  1421. switch (level) {
  1422. case MSG_EXCESSIVE:
  1423. return "EXCESSIVE";
  1424. case MSG_MSGDUMP:
  1425. return "MSGDUMP";
  1426. case MSG_DEBUG:
  1427. return "DEBUG";
  1428. case MSG_INFO:
  1429. return "INFO";
  1430. case MSG_WARNING:
  1431. return "WARNING";
  1432. case MSG_ERROR:
  1433. return "ERROR";
  1434. default:
  1435. return "?";
  1436. }
  1437. }
  1438. static int str_to_debug_level(const char *s)
  1439. {
  1440. if (os_strcasecmp(s, "EXCESSIVE") == 0)
  1441. return MSG_EXCESSIVE;
  1442. if (os_strcasecmp(s, "MSGDUMP") == 0)
  1443. return MSG_MSGDUMP;
  1444. if (os_strcasecmp(s, "DEBUG") == 0)
  1445. return MSG_DEBUG;
  1446. if (os_strcasecmp(s, "INFO") == 0)
  1447. return MSG_INFO;
  1448. if (os_strcasecmp(s, "WARNING") == 0)
  1449. return MSG_WARNING;
  1450. if (os_strcasecmp(s, "ERROR") == 0)
  1451. return MSG_ERROR;
  1452. return -1;
  1453. }
  1454. static int wpa_supplicant_ctrl_iface_log_level(struct wpa_supplicant *wpa_s,
  1455. char *cmd, char *buf,
  1456. size_t buflen)
  1457. {
  1458. char *pos, *end, *stamp;
  1459. int ret;
  1460. if (cmd == NULL) {
  1461. return -1;
  1462. }
  1463. /* cmd: "LOG_LEVEL [<level>]" */
  1464. if (*cmd == '\0') {
  1465. pos = buf;
  1466. end = buf + buflen;
  1467. ret = os_snprintf(pos, end - pos, "Current level: %s\n"
  1468. "Timestamp: %d\n",
  1469. debug_level_str(wpa_debug_level),
  1470. wpa_debug_timestamp);
  1471. if (ret < 0 || ret >= end - pos)
  1472. ret = 0;
  1473. return ret;
  1474. }
  1475. while (*cmd == ' ')
  1476. cmd++;
  1477. stamp = os_strchr(cmd, ' ');
  1478. if (stamp) {
  1479. *stamp++ = '\0';
  1480. while (*stamp == ' ') {
  1481. stamp++;
  1482. }
  1483. }
  1484. if (cmd && os_strlen(cmd)) {
  1485. int level = str_to_debug_level(cmd);
  1486. if (level < 0)
  1487. return -1;
  1488. wpa_debug_level = level;
  1489. }
  1490. if (stamp && os_strlen(stamp))
  1491. wpa_debug_timestamp = atoi(stamp);
  1492. os_memcpy(buf, "OK\n", 3);
  1493. return 3;
  1494. }
  1495. static int wpa_supplicant_ctrl_iface_list_networks(
  1496. struct wpa_supplicant *wpa_s, char *buf, size_t buflen)
  1497. {
  1498. char *pos, *end;
  1499. struct wpa_ssid *ssid;
  1500. int ret;
  1501. pos = buf;
  1502. end = buf + buflen;
  1503. ret = os_snprintf(pos, end - pos,
  1504. "network id / ssid / bssid / flags\n");
  1505. if (ret < 0 || ret >= end - pos)
  1506. return pos - buf;
  1507. pos += ret;
  1508. ssid = wpa_s->conf->ssid;
  1509. while (ssid) {
  1510. ret = os_snprintf(pos, end - pos, "%d\t%s",
  1511. ssid->id,
  1512. wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
  1513. if (ret < 0 || ret >= end - pos)
  1514. return pos - buf;
  1515. pos += ret;
  1516. if (ssid->bssid_set) {
  1517. ret = os_snprintf(pos, end - pos, "\t" MACSTR,
  1518. MAC2STR(ssid->bssid));
  1519. } else {
  1520. ret = os_snprintf(pos, end - pos, "\tany");
  1521. }
  1522. if (ret < 0 || ret >= end - pos)
  1523. return pos - buf;
  1524. pos += ret;
  1525. ret = os_snprintf(pos, end - pos, "\t%s%s%s%s",
  1526. ssid == wpa_s->current_ssid ?
  1527. "[CURRENT]" : "",
  1528. ssid->disabled ? "[DISABLED]" : "",
  1529. ssid->disabled_until.sec ?
  1530. "[TEMP-DISABLED]" : "",
  1531. ssid->disabled == 2 ? "[P2P-PERSISTENT]" :
  1532. "");
  1533. if (ret < 0 || ret >= end - pos)
  1534. return pos - buf;
  1535. pos += ret;
  1536. ret = os_snprintf(pos, end - pos, "\n");
  1537. if (ret < 0 || ret >= end - pos)
  1538. return pos - buf;
  1539. pos += ret;
  1540. ssid = ssid->next;
  1541. }
  1542. return pos - buf;
  1543. }
  1544. static char * wpa_supplicant_cipher_txt(char *pos, char *end, int cipher)
  1545. {
  1546. int ret;
  1547. ret = os_snprintf(pos, end - pos, "-");
  1548. if (ret < 0 || ret >= end - pos)
  1549. return pos;
  1550. pos += ret;
  1551. ret = wpa_write_ciphers(pos, end, cipher, "+");
  1552. if (ret < 0)
  1553. return pos;
  1554. pos += ret;
  1555. return pos;
  1556. }
  1557. static char * wpa_supplicant_ie_txt(char *pos, char *end, const char *proto,
  1558. const u8 *ie, size_t ie_len)
  1559. {
  1560. struct wpa_ie_data data;
  1561. int first, ret;
  1562. ret = os_snprintf(pos, end - pos, "[%s-", proto);
  1563. if (ret < 0 || ret >= end - pos)
  1564. return pos;
  1565. pos += ret;
  1566. if (wpa_parse_wpa_ie(ie, ie_len, &data) < 0) {
  1567. ret = os_snprintf(pos, end - pos, "?]");
  1568. if (ret < 0 || ret >= end - pos)
  1569. return pos;
  1570. pos += ret;
  1571. return pos;
  1572. }
  1573. first = 1;
  1574. if (data.key_mgmt & WPA_KEY_MGMT_IEEE8021X) {
  1575. ret = os_snprintf(pos, end - pos, "%sEAP", first ? "" : "+");
  1576. if (ret < 0 || ret >= end - pos)
  1577. return pos;
  1578. pos += ret;
  1579. first = 0;
  1580. }
  1581. if (data.key_mgmt & WPA_KEY_MGMT_PSK) {
  1582. ret = os_snprintf(pos, end - pos, "%sPSK", first ? "" : "+");
  1583. if (ret < 0 || ret >= end - pos)
  1584. return pos;
  1585. pos += ret;
  1586. first = 0;
  1587. }
  1588. if (data.key_mgmt & WPA_KEY_MGMT_WPA_NONE) {
  1589. ret = os_snprintf(pos, end - pos, "%sNone", first ? "" : "+");
  1590. if (ret < 0 || ret >= end - pos)
  1591. return pos;
  1592. pos += ret;
  1593. first = 0;
  1594. }
  1595. #ifdef CONFIG_IEEE80211R
  1596. if (data.key_mgmt & WPA_KEY_MGMT_FT_IEEE8021X) {
  1597. ret = os_snprintf(pos, end - pos, "%sFT/EAP",
  1598. first ? "" : "+");
  1599. if (ret < 0 || ret >= end - pos)
  1600. return pos;
  1601. pos += ret;
  1602. first = 0;
  1603. }
  1604. if (data.key_mgmt & WPA_KEY_MGMT_FT_PSK) {
  1605. ret = os_snprintf(pos, end - pos, "%sFT/PSK",
  1606. first ? "" : "+");
  1607. if (ret < 0 || ret >= end - pos)
  1608. return pos;
  1609. pos += ret;
  1610. first = 0;
  1611. }
  1612. #endif /* CONFIG_IEEE80211R */
  1613. #ifdef CONFIG_IEEE80211W
  1614. if (data.key_mgmt & WPA_KEY_MGMT_IEEE8021X_SHA256) {
  1615. ret = os_snprintf(pos, end - pos, "%sEAP-SHA256",
  1616. first ? "" : "+");
  1617. if (ret < 0 || ret >= end - pos)
  1618. return pos;
  1619. pos += ret;
  1620. first = 0;
  1621. }
  1622. if (data.key_mgmt & WPA_KEY_MGMT_PSK_SHA256) {
  1623. ret = os_snprintf(pos, end - pos, "%sPSK-SHA256",
  1624. first ? "" : "+");
  1625. if (ret < 0 || ret >= end - pos)
  1626. return pos;
  1627. pos += ret;
  1628. first = 0;
  1629. }
  1630. #endif /* CONFIG_IEEE80211W */
  1631. pos = wpa_supplicant_cipher_txt(pos, end, data.pairwise_cipher);
  1632. if (data.capabilities & WPA_CAPABILITY_PREAUTH) {
  1633. ret = os_snprintf(pos, end - pos, "-preauth");
  1634. if (ret < 0 || ret >= end - pos)
  1635. return pos;
  1636. pos += ret;
  1637. }
  1638. ret = os_snprintf(pos, end - pos, "]");
  1639. if (ret < 0 || ret >= end - pos)
  1640. return pos;
  1641. pos += ret;
  1642. return pos;
  1643. }
  1644. #ifdef CONFIG_WPS
  1645. static char * wpa_supplicant_wps_ie_txt_buf(struct wpa_supplicant *wpa_s,
  1646. char *pos, char *end,
  1647. struct wpabuf *wps_ie)
  1648. {
  1649. int ret;
  1650. const char *txt;
  1651. if (wps_ie == NULL)
  1652. return pos;
  1653. if (wps_is_selected_pbc_registrar(wps_ie))
  1654. txt = "[WPS-PBC]";
  1655. #ifdef CONFIG_WPS2
  1656. else if (wps_is_addr_authorized(wps_ie, wpa_s->own_addr, 0))
  1657. txt = "[WPS-AUTH]";
  1658. #endif /* CONFIG_WPS2 */
  1659. else if (wps_is_selected_pin_registrar(wps_ie))
  1660. txt = "[WPS-PIN]";
  1661. else
  1662. txt = "[WPS]";
  1663. ret = os_snprintf(pos, end - pos, "%s", txt);
  1664. if (ret >= 0 && ret < end - pos)
  1665. pos += ret;
  1666. wpabuf_free(wps_ie);
  1667. return pos;
  1668. }
  1669. #endif /* CONFIG_WPS */
  1670. static char * wpa_supplicant_wps_ie_txt(struct wpa_supplicant *wpa_s,
  1671. char *pos, char *end,
  1672. const struct wpa_bss *bss)
  1673. {
  1674. #ifdef CONFIG_WPS
  1675. struct wpabuf *wps_ie;
  1676. wps_ie = wpa_bss_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
  1677. return wpa_supplicant_wps_ie_txt_buf(wpa_s, pos, end, wps_ie);
  1678. #else /* CONFIG_WPS */
  1679. return pos;
  1680. #endif /* CONFIG_WPS */
  1681. }
  1682. /* Format one result on one text line into a buffer. */
  1683. static int wpa_supplicant_ctrl_iface_scan_result(
  1684. struct wpa_supplicant *wpa_s,
  1685. const struct wpa_bss *bss, char *buf, size_t buflen)
  1686. {
  1687. char *pos, *end;
  1688. int ret;
  1689. const u8 *ie, *ie2, *p2p;
  1690. p2p = wpa_bss_get_vendor_ie(bss, P2P_IE_VENDOR_TYPE);
  1691. if (p2p && bss->ssid_len == P2P_WILDCARD_SSID_LEN &&
  1692. os_memcmp(bss->ssid, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN) ==
  1693. 0)
  1694. return 0; /* Do not show P2P listen discovery results here */
  1695. pos = buf;
  1696. end = buf + buflen;
  1697. ret = os_snprintf(pos, end - pos, MACSTR "\t%d\t%d\t",
  1698. MAC2STR(bss->bssid), bss->freq, bss->level);
  1699. if (ret < 0 || ret >= end - pos)
  1700. return -1;
  1701. pos += ret;
  1702. ie = wpa_bss_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
  1703. if (ie)
  1704. pos = wpa_supplicant_ie_txt(pos, end, "WPA", ie, 2 + ie[1]);
  1705. ie2 = wpa_bss_get_ie(bss, WLAN_EID_RSN);
  1706. if (ie2)
  1707. pos = wpa_supplicant_ie_txt(pos, end, "WPA2", ie2, 2 + ie2[1]);
  1708. pos = wpa_supplicant_wps_ie_txt(wpa_s, pos, end, bss);
  1709. if (!ie && !ie2 && bss->caps & IEEE80211_CAP_PRIVACY) {
  1710. ret = os_snprintf(pos, end - pos, "[WEP]");
  1711. if (ret < 0 || ret >= end - pos)
  1712. return -1;
  1713. pos += ret;
  1714. }
  1715. if (bss->caps & IEEE80211_CAP_IBSS) {
  1716. ret = os_snprintf(pos, end - pos, "[IBSS]");
  1717. if (ret < 0 || ret >= end - pos)
  1718. return -1;
  1719. pos += ret;
  1720. }
  1721. if (bss->caps & IEEE80211_CAP_ESS) {
  1722. ret = os_snprintf(pos, end - pos, "[ESS]");
  1723. if (ret < 0 || ret >= end - pos)
  1724. return -1;
  1725. pos += ret;
  1726. }
  1727. if (p2p) {
  1728. ret = os_snprintf(pos, end - pos, "[P2P]");
  1729. if (ret < 0 || ret >= end - pos)
  1730. return -1;
  1731. pos += ret;
  1732. }
  1733. #ifdef CONFIG_HS20
  1734. if (wpa_bss_get_vendor_ie(bss, HS20_IE_VENDOR_TYPE) && ie2) {
  1735. ret = os_snprintf(pos, end - pos, "[HS20]");
  1736. if (ret < 0 || ret >= end - pos)
  1737. return -1;
  1738. pos += ret;
  1739. }
  1740. #endif /* CONFIG_HS20 */
  1741. ret = os_snprintf(pos, end - pos, "\t%s",
  1742. wpa_ssid_txt(bss->ssid, bss->ssid_len));
  1743. if (ret < 0 || ret >= end - pos)
  1744. return -1;
  1745. pos += ret;
  1746. ret = os_snprintf(pos, end - pos, "\n");
  1747. if (ret < 0 || ret >= end - pos)
  1748. return -1;
  1749. pos += ret;
  1750. return pos - buf;
  1751. }
  1752. static int wpa_supplicant_ctrl_iface_scan_results(
  1753. struct wpa_supplicant *wpa_s, char *buf, size_t buflen)
  1754. {
  1755. char *pos, *end;
  1756. struct wpa_bss *bss;
  1757. int ret;
  1758. pos = buf;
  1759. end = buf + buflen;
  1760. ret = os_snprintf(pos, end - pos, "bssid / frequency / signal level / "
  1761. "flags / ssid\n");
  1762. if (ret < 0 || ret >= end - pos)
  1763. return pos - buf;
  1764. pos += ret;
  1765. dl_list_for_each(bss, &wpa_s->bss_id, struct wpa_bss, list_id) {
  1766. ret = wpa_supplicant_ctrl_iface_scan_result(wpa_s, bss, pos,
  1767. end - pos);
  1768. if (ret < 0 || ret >= end - pos)
  1769. return pos - buf;
  1770. pos += ret;
  1771. }
  1772. return pos - buf;
  1773. }
  1774. static int wpa_supplicant_ctrl_iface_select_network(
  1775. struct wpa_supplicant *wpa_s, char *cmd)
  1776. {
  1777. int id;
  1778. struct wpa_ssid *ssid;
  1779. /* cmd: "<network id>" or "any" */
  1780. if (os_strcmp(cmd, "any") == 0) {
  1781. wpa_printf(MSG_DEBUG, "CTRL_IFACE: SELECT_NETWORK any");
  1782. ssid = NULL;
  1783. } else {
  1784. id = atoi(cmd);
  1785. wpa_printf(MSG_DEBUG, "CTRL_IFACE: SELECT_NETWORK id=%d", id);
  1786. ssid = wpa_config_get_network(wpa_s->conf, id);
  1787. if (ssid == NULL) {
  1788. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find "
  1789. "network id=%d", id);
  1790. return -1;
  1791. }
  1792. if (ssid->disabled == 2) {
  1793. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Cannot use "
  1794. "SELECT_NETWORK with persistent P2P group");
  1795. return -1;
  1796. }
  1797. }
  1798. wpa_supplicant_select_network(wpa_s, ssid);
  1799. return 0;
  1800. }
  1801. static int wpa_supplicant_ctrl_iface_enable_network(
  1802. struct wpa_supplicant *wpa_s, char *cmd)
  1803. {
  1804. int id;
  1805. struct wpa_ssid *ssid;
  1806. /* cmd: "<network id>" or "all" */
  1807. if (os_strcmp(cmd, "all") == 0) {
  1808. wpa_printf(MSG_DEBUG, "CTRL_IFACE: ENABLE_NETWORK all");
  1809. ssid = NULL;
  1810. } else {
  1811. id = atoi(cmd);
  1812. wpa_printf(MSG_DEBUG, "CTRL_IFACE: ENABLE_NETWORK id=%d", id);
  1813. ssid = wpa_config_get_network(wpa_s->conf, id);
  1814. if (ssid == NULL) {
  1815. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find "
  1816. "network id=%d", id);
  1817. return -1;
  1818. }
  1819. if (ssid->disabled == 2) {
  1820. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Cannot use "
  1821. "ENABLE_NETWORK with persistent P2P group");
  1822. return -1;
  1823. }
  1824. if (os_strstr(cmd, " no-connect")) {
  1825. ssid->disabled = 0;
  1826. return 0;
  1827. }
  1828. }
  1829. wpa_supplicant_enable_network(wpa_s, ssid);
  1830. return 0;
  1831. }
  1832. static int wpa_supplicant_ctrl_iface_disable_network(
  1833. struct wpa_supplicant *wpa_s, char *cmd)
  1834. {
  1835. int id;
  1836. struct wpa_ssid *ssid;
  1837. /* cmd: "<network id>" or "all" */
  1838. if (os_strcmp(cmd, "all") == 0) {
  1839. wpa_printf(MSG_DEBUG, "CTRL_IFACE: DISABLE_NETWORK all");
  1840. ssid = NULL;
  1841. } else {
  1842. id = atoi(cmd);
  1843. wpa_printf(MSG_DEBUG, "CTRL_IFACE: DISABLE_NETWORK id=%d", id);
  1844. ssid = wpa_config_get_network(wpa_s->conf, id);
  1845. if (ssid == NULL) {
  1846. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find "
  1847. "network id=%d", id);
  1848. return -1;
  1849. }
  1850. if (ssid->disabled == 2) {
  1851. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Cannot use "
  1852. "DISABLE_NETWORK with persistent P2P "
  1853. "group");
  1854. return -1;
  1855. }
  1856. }
  1857. wpa_supplicant_disable_network(wpa_s, ssid);
  1858. return 0;
  1859. }
  1860. static int wpa_supplicant_ctrl_iface_add_network(
  1861. struct wpa_supplicant *wpa_s, char *buf, size_t buflen)
  1862. {
  1863. struct wpa_ssid *ssid;
  1864. int ret;
  1865. wpa_printf(MSG_DEBUG, "CTRL_IFACE: ADD_NETWORK");
  1866. ssid = wpa_config_add_network(wpa_s->conf);
  1867. if (ssid == NULL)
  1868. return -1;
  1869. wpas_notify_network_added(wpa_s, ssid);
  1870. ssid->disabled = 1;
  1871. wpa_config_set_network_defaults(ssid);
  1872. ret = os_snprintf(buf, buflen, "%d\n", ssid->id);
  1873. if (ret < 0 || (size_t) ret >= buflen)
  1874. return -1;
  1875. return ret;
  1876. }
  1877. static int wpa_supplicant_ctrl_iface_remove_network(
  1878. struct wpa_supplicant *wpa_s, char *cmd)
  1879. {
  1880. int id;
  1881. struct wpa_ssid *ssid;
  1882. int was_disabled;
  1883. /* cmd: "<network id>" or "all" */
  1884. if (os_strcmp(cmd, "all") == 0) {
  1885. wpa_printf(MSG_DEBUG, "CTRL_IFACE: REMOVE_NETWORK all");
  1886. if (wpa_s->sched_scanning)
  1887. wpa_supplicant_cancel_sched_scan(wpa_s);
  1888. eapol_sm_invalidate_cached_session(wpa_s->eapol);
  1889. if (wpa_s->current_ssid) {
  1890. #ifdef CONFIG_SME
  1891. wpa_s->sme.prev_bssid_set = 0;
  1892. #endif /* CONFIG_SME */
  1893. wpa_sm_set_config(wpa_s->wpa, NULL);
  1894. eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
  1895. wpa_supplicant_deauthenticate(
  1896. wpa_s, WLAN_REASON_DEAUTH_LEAVING);
  1897. }
  1898. ssid = wpa_s->conf->ssid;
  1899. while (ssid) {
  1900. struct wpa_ssid *remove_ssid = ssid;
  1901. id = ssid->id;
  1902. ssid = ssid->next;
  1903. wpas_notify_network_removed(wpa_s, remove_ssid);
  1904. wpa_config_remove_network(wpa_s->conf, id);
  1905. }
  1906. return 0;
  1907. }
  1908. id = atoi(cmd);
  1909. wpa_printf(MSG_DEBUG, "CTRL_IFACE: REMOVE_NETWORK id=%d", id);
  1910. ssid = wpa_config_get_network(wpa_s->conf, id);
  1911. if (ssid)
  1912. wpas_notify_network_removed(wpa_s, ssid);
  1913. if (ssid == NULL) {
  1914. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find network "
  1915. "id=%d", id);
  1916. return -1;
  1917. }
  1918. if (ssid == wpa_s->current_ssid || wpa_s->current_ssid == NULL) {
  1919. #ifdef CONFIG_SME
  1920. wpa_s->sme.prev_bssid_set = 0;
  1921. #endif /* CONFIG_SME */
  1922. /*
  1923. * Invalidate the EAP session cache if the current or
  1924. * previously used network is removed.
  1925. */
  1926. eapol_sm_invalidate_cached_session(wpa_s->eapol);
  1927. }
  1928. if (ssid == wpa_s->current_ssid) {
  1929. wpa_sm_set_config(wpa_s->wpa, NULL);
  1930. eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
  1931. wpa_supplicant_deauthenticate(wpa_s,
  1932. WLAN_REASON_DEAUTH_LEAVING);
  1933. }
  1934. was_disabled = ssid->disabled;
  1935. if (wpa_config_remove_network(wpa_s->conf, id) < 0) {
  1936. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Not able to remove the "
  1937. "network id=%d", id);
  1938. return -1;
  1939. }
  1940. if (!was_disabled && wpa_s->sched_scanning) {
  1941. wpa_printf(MSG_DEBUG, "Stop ongoing sched_scan to remove "
  1942. "network from filters");
  1943. wpa_supplicant_cancel_sched_scan(wpa_s);
  1944. wpa_supplicant_req_scan(wpa_s, 0, 0);
  1945. }
  1946. return 0;
  1947. }
  1948. static int wpa_supplicant_ctrl_iface_set_network(
  1949. struct wpa_supplicant *wpa_s, char *cmd)
  1950. {
  1951. int id;
  1952. struct wpa_ssid *ssid;
  1953. char *name, *value;
  1954. /* cmd: "<network id> <variable name> <value>" */
  1955. name = os_strchr(cmd, ' ');
  1956. if (name == NULL)
  1957. return -1;
  1958. *name++ = '\0';
  1959. value = os_strchr(name, ' ');
  1960. if (value == NULL)
  1961. return -1;
  1962. *value++ = '\0';
  1963. id = atoi(cmd);
  1964. wpa_printf(MSG_DEBUG, "CTRL_IFACE: SET_NETWORK id=%d name='%s'",
  1965. id, name);
  1966. wpa_hexdump_ascii_key(MSG_DEBUG, "CTRL_IFACE: value",
  1967. (u8 *) value, os_strlen(value));
  1968. ssid = wpa_config_get_network(wpa_s->conf, id);
  1969. if (ssid == NULL) {
  1970. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find network "
  1971. "id=%d", id);
  1972. return -1;
  1973. }
  1974. if (wpa_config_set(ssid, name, value, 0) < 0) {
  1975. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Failed to set network "
  1976. "variable '%s'", name);
  1977. return -1;
  1978. }
  1979. if (os_strcmp(name, "bssid") != 0 &&
  1980. os_strcmp(name, "priority") != 0)
  1981. wpa_sm_pmksa_cache_flush(wpa_s->wpa, ssid);
  1982. if (wpa_s->current_ssid == ssid || wpa_s->current_ssid == NULL) {
  1983. /*
  1984. * Invalidate the EAP session cache if anything in the current
  1985. * or previously used configuration changes.
  1986. */
  1987. eapol_sm_invalidate_cached_session(wpa_s->eapol);
  1988. }
  1989. if ((os_strcmp(name, "psk") == 0 &&
  1990. value[0] == '"' && ssid->ssid_len) ||
  1991. (os_strcmp(name, "ssid") == 0 && ssid->passphrase))
  1992. wpa_config_update_psk(ssid);
  1993. else if (os_strcmp(name, "priority") == 0)
  1994. wpa_config_update_prio_list(wpa_s->conf);
  1995. return 0;
  1996. }
  1997. static int wpa_supplicant_ctrl_iface_get_network(
  1998. struct wpa_supplicant *wpa_s, char *cmd, char *buf, size_t buflen)
  1999. {
  2000. int id;
  2001. size_t res;
  2002. struct wpa_ssid *ssid;
  2003. char *name, *value;
  2004. /* cmd: "<network id> <variable name>" */
  2005. name = os_strchr(cmd, ' ');
  2006. if (name == NULL || buflen == 0)
  2007. return -1;
  2008. *name++ = '\0';
  2009. id = atoi(cmd);
  2010. wpa_printf(MSG_DEBUG, "CTRL_IFACE: GET_NETWORK id=%d name='%s'",
  2011. id, name);
  2012. ssid = wpa_config_get_network(wpa_s->conf, id);
  2013. if (ssid == NULL) {
  2014. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find network "
  2015. "id=%d", id);
  2016. return -1;
  2017. }
  2018. value = wpa_config_get_no_key(ssid, name);
  2019. if (value == NULL) {
  2020. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Failed to get network "
  2021. "variable '%s'", name);
  2022. return -1;
  2023. }
  2024. res = os_strlcpy(buf, value, buflen);
  2025. if (res >= buflen) {
  2026. os_free(value);
  2027. return -1;
  2028. }
  2029. os_free(value);
  2030. return res;
  2031. }
  2032. static int wpa_supplicant_ctrl_iface_list_creds(struct wpa_supplicant *wpa_s,
  2033. char *buf, size_t buflen)
  2034. {
  2035. char *pos, *end;
  2036. struct wpa_cred *cred;
  2037. int ret;
  2038. pos = buf;
  2039. end = buf + buflen;
  2040. ret = os_snprintf(pos, end - pos,
  2041. "cred id / realm / username / domain / imsi\n");
  2042. if (ret < 0 || ret >= end - pos)
  2043. return pos - buf;
  2044. pos += ret;
  2045. cred = wpa_s->conf->cred;
  2046. while (cred) {
  2047. ret = os_snprintf(pos, end - pos, "%d\t%s\t%s\t%s\t%s\n",
  2048. cred->id, cred->realm ? cred->realm : "",
  2049. cred->username ? cred->username : "",
  2050. cred->domain ? cred->domain : "",
  2051. cred->imsi ? cred->imsi : "");
  2052. if (ret < 0 || ret >= end - pos)
  2053. return pos - buf;
  2054. pos += ret;
  2055. cred = cred->next;
  2056. }
  2057. return pos - buf;
  2058. }
  2059. static int wpa_supplicant_ctrl_iface_add_cred(struct wpa_supplicant *wpa_s,
  2060. char *buf, size_t buflen)
  2061. {
  2062. struct wpa_cred *cred;
  2063. int ret;
  2064. wpa_printf(MSG_DEBUG, "CTRL_IFACE: ADD_CRED");
  2065. cred = wpa_config_add_cred(wpa_s->conf);
  2066. if (cred == NULL)
  2067. return -1;
  2068. ret = os_snprintf(buf, buflen, "%d\n", cred->id);
  2069. if (ret < 0 || (size_t) ret >= buflen)
  2070. return -1;
  2071. return ret;
  2072. }
  2073. static int wpas_ctrl_remove_cred(struct wpa_supplicant *wpa_s,
  2074. struct wpa_cred *cred)
  2075. {
  2076. struct wpa_ssid *ssid;
  2077. char str[20];
  2078. if (cred == NULL || wpa_config_remove_cred(wpa_s->conf, cred->id) < 0) {
  2079. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find cred");
  2080. return -1;
  2081. }
  2082. /* Remove any network entry created based on the removed credential */
  2083. ssid = wpa_s->conf->ssid;
  2084. while (ssid) {
  2085. if (ssid->parent_cred == cred) {
  2086. wpa_printf(MSG_DEBUG, "Remove network id %d since it "
  2087. "used the removed credential", ssid->id);
  2088. os_snprintf(str, sizeof(str), "%d", ssid->id);
  2089. ssid = ssid->next;
  2090. wpa_supplicant_ctrl_iface_remove_network(wpa_s, str);
  2091. } else
  2092. ssid = ssid->next;
  2093. }
  2094. return 0;
  2095. }
  2096. static int wpa_supplicant_ctrl_iface_remove_cred(struct wpa_supplicant *wpa_s,
  2097. char *cmd)
  2098. {
  2099. int id;
  2100. struct wpa_cred *cred, *prev;
  2101. /* cmd: "<cred id>", "all", or "sp_fqdn=<FQDN>" */
  2102. if (os_strcmp(cmd, "all") == 0) {
  2103. wpa_printf(MSG_DEBUG, "CTRL_IFACE: REMOVE_CRED all");
  2104. cred = wpa_s->conf->cred;
  2105. while (cred) {
  2106. prev = cred;
  2107. cred = cred->next;
  2108. wpas_ctrl_remove_cred(wpa_s, prev);
  2109. }
  2110. return 0;
  2111. }
  2112. if (os_strncmp(cmd, "sp_fqdn=", 8) == 0) {
  2113. wpa_printf(MSG_DEBUG, "CTRL_IFACE: REMOVE_CRED SP FQDN '%s'",
  2114. cmd + 8);
  2115. cred = wpa_s->conf->cred;
  2116. while (cred) {
  2117. prev = cred;
  2118. cred = cred->next;
  2119. if (prev->domain &&
  2120. os_strcmp(prev->domain, cmd + 8) == 0)
  2121. wpas_ctrl_remove_cred(wpa_s, prev);
  2122. }
  2123. return 0;
  2124. }
  2125. id = atoi(cmd);
  2126. wpa_printf(MSG_DEBUG, "CTRL_IFACE: REMOVE_CRED id=%d", id);
  2127. cred = wpa_config_get_cred(wpa_s->conf, id);
  2128. return wpas_ctrl_remove_cred(wpa_s, cred);
  2129. }
  2130. static int wpa_supplicant_ctrl_iface_set_cred(struct wpa_supplicant *wpa_s,
  2131. char *cmd)
  2132. {
  2133. int id;
  2134. struct wpa_cred *cred;
  2135. char *name, *value;
  2136. /* cmd: "<cred id> <variable name> <value>" */
  2137. name = os_strchr(cmd, ' ');
  2138. if (name == NULL)
  2139. return -1;
  2140. *name++ = '\0';
  2141. value = os_strchr(name, ' ');
  2142. if (value == NULL)
  2143. return -1;
  2144. *value++ = '\0';
  2145. id = atoi(cmd);
  2146. wpa_printf(MSG_DEBUG, "CTRL_IFACE: SET_CRED id=%d name='%s'",
  2147. id, name);
  2148. wpa_hexdump_ascii_key(MSG_DEBUG, "CTRL_IFACE: value",
  2149. (u8 *) value, os_strlen(value));
  2150. cred = wpa_config_get_cred(wpa_s->conf, id);
  2151. if (cred == NULL) {
  2152. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find cred id=%d",
  2153. id);
  2154. return -1;
  2155. }
  2156. if (wpa_config_set_cred(cred, name, value, 0) < 0) {
  2157. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Failed to set cred "
  2158. "variable '%s'", name);
  2159. return -1;
  2160. }
  2161. return 0;
  2162. }
  2163. #ifndef CONFIG_NO_CONFIG_WRITE
  2164. static int wpa_supplicant_ctrl_iface_save_config(struct wpa_supplicant *wpa_s)
  2165. {
  2166. int ret;
  2167. if (!wpa_s->conf->update_config) {
  2168. wpa_printf(MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Not allowed "
  2169. "to update configuration (update_config=0)");
  2170. return -1;
  2171. }
  2172. ret = wpa_config_write(wpa_s->confname, wpa_s->conf);
  2173. if (ret) {
  2174. wpa_printf(MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Failed to "
  2175. "update configuration");
  2176. } else {
  2177. wpa_printf(MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Configuration"
  2178. " updated");
  2179. }
  2180. return ret;
  2181. }
  2182. #endif /* CONFIG_NO_CONFIG_WRITE */
  2183. static int ctrl_iface_get_capability_pairwise(int res, char *strict,
  2184. struct wpa_driver_capa *capa,
  2185. char *buf, size_t buflen)
  2186. {
  2187. int ret, first = 1;
  2188. char *pos, *end;
  2189. size_t len;
  2190. pos = buf;
  2191. end = pos + buflen;
  2192. if (res < 0) {
  2193. if (strict)
  2194. return 0;
  2195. len = os_strlcpy(buf, "CCMP TKIP NONE", buflen);
  2196. if (len >= buflen)
  2197. return -1;
  2198. return len;
  2199. }
  2200. if (capa->enc & WPA_DRIVER_CAPA_ENC_CCMP) {
  2201. ret = os_snprintf(pos, end - pos, "%sCCMP", first ? "" : " ");
  2202. if (ret < 0 || ret >= end - pos)
  2203. return pos - buf;
  2204. pos += ret;
  2205. first = 0;
  2206. }
  2207. if (capa->enc & WPA_DRIVER_CAPA_ENC_GCMP) {
  2208. ret = os_snprintf(pos, end - pos, "%sGCMP", first ? "" : " ");
  2209. if (ret < 0 || ret >= end - pos)
  2210. return pos - buf;
  2211. pos += ret;
  2212. first = 0;
  2213. }
  2214. if (capa->enc & WPA_DRIVER_CAPA_ENC_TKIP) {
  2215. ret = os_snprintf(pos, end - pos, "%sTKIP", first ? "" : " ");
  2216. if (ret < 0 || ret >= end - pos)
  2217. return pos - buf;
  2218. pos += ret;
  2219. first = 0;
  2220. }
  2221. if (capa->key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_WPA_NONE) {
  2222. ret = os_snprintf(pos, end - pos, "%sNONE", first ? "" : " ");
  2223. if (ret < 0 || ret >= end - pos)
  2224. return pos - buf;
  2225. pos += ret;
  2226. first = 0;
  2227. }
  2228. return pos - buf;
  2229. }
  2230. static int ctrl_iface_get_capability_group(int res, char *strict,
  2231. struct wpa_driver_capa *capa,
  2232. char *buf, size_t buflen)
  2233. {
  2234. int ret, first = 1;
  2235. char *pos, *end;
  2236. size_t len;
  2237. pos = buf;
  2238. end = pos + buflen;
  2239. if (res < 0) {
  2240. if (strict)
  2241. return 0;
  2242. len = os_strlcpy(buf, "CCMP TKIP WEP104 WEP40", buflen);
  2243. if (len >= buflen)
  2244. return -1;
  2245. return len;
  2246. }
  2247. if (capa->enc & WPA_DRIVER_CAPA_ENC_CCMP) {
  2248. ret = os_snprintf(pos, end - pos, "%sCCMP", first ? "" : " ");
  2249. if (ret < 0 || ret >= end - pos)
  2250. return pos - buf;
  2251. pos += ret;
  2252. first = 0;
  2253. }
  2254. if (capa->enc & WPA_DRIVER_CAPA_ENC_GCMP) {
  2255. ret = os_snprintf(pos, end - pos, "%sGCMP", first ? "" : " ");
  2256. if (ret < 0 || ret >= end - pos)
  2257. return pos - buf;
  2258. pos += ret;
  2259. first = 0;
  2260. }
  2261. if (capa->enc & WPA_DRIVER_CAPA_ENC_TKIP) {
  2262. ret = os_snprintf(pos, end - pos, "%sTKIP", first ? "" : " ");
  2263. if (ret < 0 || ret >= end - pos)
  2264. return pos - buf;
  2265. pos += ret;
  2266. first = 0;
  2267. }
  2268. if (capa->enc & WPA_DRIVER_CAPA_ENC_WEP104) {
  2269. ret = os_snprintf(pos, end - pos, "%sWEP104",
  2270. first ? "" : " ");
  2271. if (ret < 0 || ret >= end - pos)
  2272. return pos - buf;
  2273. pos += ret;
  2274. first = 0;
  2275. }
  2276. if (capa->enc & WPA_DRIVER_CAPA_ENC_WEP40) {
  2277. ret = os_snprintf(pos, end - pos, "%sWEP40", first ? "" : " ");
  2278. if (ret < 0 || ret >= end - pos)
  2279. return pos - buf;
  2280. pos += ret;
  2281. first = 0;
  2282. }
  2283. return pos - buf;
  2284. }
  2285. static int ctrl_iface_get_capability_key_mgmt(int res, char *strict,
  2286. struct wpa_driver_capa *capa,
  2287. char *buf, size_t buflen)
  2288. {
  2289. int ret;
  2290. char *pos, *end;
  2291. size_t len;
  2292. pos = buf;
  2293. end = pos + buflen;
  2294. if (res < 0) {
  2295. if (strict)
  2296. return 0;
  2297. len = os_strlcpy(buf, "WPA-PSK WPA-EAP IEEE8021X WPA-NONE "
  2298. "NONE", buflen);
  2299. if (len >= buflen)
  2300. return -1;
  2301. return len;
  2302. }
  2303. ret = os_snprintf(pos, end - pos, "NONE IEEE8021X");
  2304. if (ret < 0 || ret >= end - pos)
  2305. return pos - buf;
  2306. pos += ret;
  2307. if (capa->key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA |
  2308. WPA_DRIVER_CAPA_KEY_MGMT_WPA2)) {
  2309. ret = os_snprintf(pos, end - pos, " WPA-EAP");
  2310. if (ret < 0 || ret >= end - pos)
  2311. return pos - buf;
  2312. pos += ret;
  2313. }
  2314. if (capa->key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA_PSK |
  2315. WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK)) {
  2316. ret = os_snprintf(pos, end - pos, " WPA-PSK");
  2317. if (ret < 0 || ret >= end - pos)
  2318. return pos - buf;
  2319. pos += ret;
  2320. }
  2321. if (capa->key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_WPA_NONE) {
  2322. ret = os_snprintf(pos, end - pos, " WPA-NONE");
  2323. if (ret < 0 || ret >= end - pos)
  2324. return pos - buf;
  2325. pos += ret;
  2326. }
  2327. return pos - buf;
  2328. }
  2329. static int ctrl_iface_get_capability_proto(int res, char *strict,
  2330. struct wpa_driver_capa *capa,
  2331. char *buf, size_t buflen)
  2332. {
  2333. int ret, first = 1;
  2334. char *pos, *end;
  2335. size_t len;
  2336. pos = buf;
  2337. end = pos + buflen;
  2338. if (res < 0) {
  2339. if (strict)
  2340. return 0;
  2341. len = os_strlcpy(buf, "RSN WPA", buflen);
  2342. if (len >= buflen)
  2343. return -1;
  2344. return len;
  2345. }
  2346. if (capa->key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA2 |
  2347. WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK)) {
  2348. ret = os_snprintf(pos, end - pos, "%sRSN", first ? "" : " ");
  2349. if (ret < 0 || ret >= end - pos)
  2350. return pos - buf;
  2351. pos += ret;
  2352. first = 0;
  2353. }
  2354. if (capa->key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA |
  2355. WPA_DRIVER_CAPA_KEY_MGMT_WPA_PSK)) {
  2356. ret = os_snprintf(pos, end - pos, "%sWPA", first ? "" : " ");
  2357. if (ret < 0 || ret >= end - pos)
  2358. return pos - buf;
  2359. pos += ret;
  2360. first = 0;
  2361. }
  2362. return pos - buf;
  2363. }
  2364. static int ctrl_iface_get_capability_auth_alg(int res, char *strict,
  2365. struct wpa_driver_capa *capa,
  2366. char *buf, size_t buflen)
  2367. {
  2368. int ret, first = 1;
  2369. char *pos, *end;
  2370. size_t len;
  2371. pos = buf;
  2372. end = pos + buflen;
  2373. if (res < 0) {
  2374. if (strict)
  2375. return 0;
  2376. len = os_strlcpy(buf, "OPEN SHARED LEAP", buflen);
  2377. if (len >= buflen)
  2378. return -1;
  2379. return len;
  2380. }
  2381. if (capa->auth & (WPA_DRIVER_AUTH_OPEN)) {
  2382. ret = os_snprintf(pos, end - pos, "%sOPEN", first ? "" : " ");
  2383. if (ret < 0 || ret >= end - pos)
  2384. return pos - buf;
  2385. pos += ret;
  2386. first = 0;
  2387. }
  2388. if (capa->auth & (WPA_DRIVER_AUTH_SHARED)) {
  2389. ret = os_snprintf(pos, end - pos, "%sSHARED",
  2390. first ? "" : " ");
  2391. if (ret < 0 || ret >= end - pos)
  2392. return pos - buf;
  2393. pos += ret;
  2394. first = 0;
  2395. }
  2396. if (capa->auth & (WPA_DRIVER_AUTH_LEAP)) {
  2397. ret = os_snprintf(pos, end - pos, "%sLEAP", first ? "" : " ");
  2398. if (ret < 0 || ret >= end - pos)
  2399. return pos - buf;
  2400. pos += ret;
  2401. first = 0;
  2402. }
  2403. return pos - buf;
  2404. }
  2405. static int ctrl_iface_get_capability_modes(int res, char *strict,
  2406. struct wpa_driver_capa *capa,
  2407. char *buf, size_t buflen)
  2408. {
  2409. int ret, first = 1;
  2410. char *pos, *end;
  2411. size_t len;
  2412. pos = buf;
  2413. end = pos + buflen;
  2414. if (res < 0) {
  2415. if (strict)
  2416. return 0;
  2417. len = os_strlcpy(buf, "IBSS AP", buflen);
  2418. if (len >= buflen)
  2419. return -1;
  2420. return len;
  2421. }
  2422. if (capa->flags & WPA_DRIVER_FLAGS_IBSS) {
  2423. ret = os_snprintf(pos, end - pos, "%sIBSS", first ? "" : " ");
  2424. if (ret < 0 || ret >= end - pos)
  2425. return pos - buf;
  2426. pos += ret;
  2427. first = 0;
  2428. }
  2429. if (capa->flags & WPA_DRIVER_FLAGS_AP) {
  2430. ret = os_snprintf(pos, end - pos, "%sAP", first ? "" : " ");
  2431. if (ret < 0 || ret >= end - pos)
  2432. return pos - buf;
  2433. pos += ret;
  2434. first = 0;
  2435. }
  2436. return pos - buf;
  2437. }
  2438. static int ctrl_iface_get_capability_channels(struct wpa_supplicant *wpa_s,
  2439. char *buf, size_t buflen)
  2440. {
  2441. struct hostapd_channel_data *chnl;
  2442. int ret, i, j;
  2443. char *pos, *end, *hmode;
  2444. pos = buf;
  2445. end = pos + buflen;
  2446. for (j = 0; j < wpa_s->hw.num_modes; j++) {
  2447. switch (wpa_s->hw.modes[j].mode) {
  2448. case HOSTAPD_MODE_IEEE80211B:
  2449. hmode = "B";
  2450. break;
  2451. case HOSTAPD_MODE_IEEE80211G:
  2452. hmode = "G";
  2453. break;
  2454. case HOSTAPD_MODE_IEEE80211A:
  2455. hmode = "A";
  2456. break;
  2457. case HOSTAPD_MODE_IEEE80211AD:
  2458. hmode = "AD";
  2459. break;
  2460. default:
  2461. continue;
  2462. }
  2463. ret = os_snprintf(pos, end - pos, "Mode[%s] Channels:", hmode);
  2464. if (ret < 0 || ret >= end - pos)
  2465. return pos - buf;
  2466. pos += ret;
  2467. chnl = wpa_s->hw.modes[j].channels;
  2468. for (i = 0; i < wpa_s->hw.modes[j].num_channels; i++) {
  2469. if (chnl[i].flag & HOSTAPD_CHAN_DISABLED)
  2470. continue;
  2471. ret = os_snprintf(pos, end - pos, " %d", chnl[i].chan);
  2472. if (ret < 0 || ret >= end - pos)
  2473. return pos - buf;
  2474. pos += ret;
  2475. }
  2476. ret = os_snprintf(pos, end - pos, "\n");
  2477. if (ret < 0 || ret >= end - pos)
  2478. return pos - buf;
  2479. pos += ret;
  2480. }
  2481. return pos - buf;
  2482. }
  2483. static int ctrl_iface_get_capability_freq(struct wpa_supplicant *wpa_s,
  2484. char *buf, size_t buflen)
  2485. {
  2486. struct hostapd_channel_data *chnl;
  2487. int ret, i, j;
  2488. char *pos, *end, *hmode;
  2489. pos = buf;
  2490. end = pos + buflen;
  2491. for (j = 0; j < wpa_s->hw.num_modes; j++) {
  2492. switch (wpa_s->hw.modes[j].mode) {
  2493. case HOSTAPD_MODE_IEEE80211B:
  2494. hmode = "B";
  2495. break;
  2496. case HOSTAPD_MODE_IEEE80211G:
  2497. hmode = "G";
  2498. break;
  2499. case HOSTAPD_MODE_IEEE80211A:
  2500. hmode = "A";
  2501. break;
  2502. case HOSTAPD_MODE_IEEE80211AD:
  2503. hmode = "AD";
  2504. break;
  2505. default:
  2506. continue;
  2507. }
  2508. ret = os_snprintf(pos, end - pos, "Mode[%s] Channels:\n",
  2509. hmode);
  2510. if (ret < 0 || ret >= end - pos)
  2511. return pos - buf;
  2512. pos += ret;
  2513. chnl = wpa_s->hw.modes[j].channels;
  2514. for (i = 0; i < wpa_s->hw.modes[j].num_channels; i++) {
  2515. if (chnl[i].flag & HOSTAPD_CHAN_DISABLED)
  2516. continue;
  2517. ret = os_snprintf(pos, end - pos, " %d = %d MHz%s\n",
  2518. chnl[i].chan, chnl[i].freq,
  2519. chnl[i].flag & HOSTAPD_CHAN_NO_IBSS ?
  2520. " (NO_IBSS)" : "");
  2521. if (ret < 0 || ret >= end - pos)
  2522. return pos - buf;
  2523. pos += ret;
  2524. }
  2525. ret = os_snprintf(pos, end - pos, "\n");
  2526. if (ret < 0 || ret >= end - pos)
  2527. return pos - buf;
  2528. pos += ret;
  2529. }
  2530. return pos - buf;
  2531. }
  2532. static int wpa_supplicant_ctrl_iface_get_capability(
  2533. struct wpa_supplicant *wpa_s, const char *_field, char *buf,
  2534. size_t buflen)
  2535. {
  2536. struct wpa_driver_capa capa;
  2537. int res;
  2538. char *strict;
  2539. char field[30];
  2540. size_t len;
  2541. /* Determine whether or not strict checking was requested */
  2542. len = os_strlcpy(field, _field, sizeof(field));
  2543. if (len >= sizeof(field))
  2544. return -1;
  2545. strict = os_strchr(field, ' ');
  2546. if (strict != NULL) {
  2547. *strict++ = '\0';
  2548. if (os_strcmp(strict, "strict") != 0)
  2549. return -1;
  2550. }
  2551. wpa_printf(MSG_DEBUG, "CTRL_IFACE: GET_CAPABILITY '%s' %s",
  2552. field, strict ? strict : "");
  2553. if (os_strcmp(field, "eap") == 0) {
  2554. return eap_get_names(buf, buflen);
  2555. }
  2556. res = wpa_drv_get_capa(wpa_s, &capa);
  2557. if (os_strcmp(field, "pairwise") == 0)
  2558. return ctrl_iface_get_capability_pairwise(res, strict, &capa,
  2559. buf, buflen);
  2560. if (os_strcmp(field, "group") == 0)
  2561. return ctrl_iface_get_capability_group(res, strict, &capa,
  2562. buf, buflen);
  2563. if (os_strcmp(field, "key_mgmt") == 0)
  2564. return ctrl_iface_get_capability_key_mgmt(res, strict, &capa,
  2565. buf, buflen);
  2566. if (os_strcmp(field, "proto") == 0)
  2567. return ctrl_iface_get_capability_proto(res, strict, &capa,
  2568. buf, buflen);
  2569. if (os_strcmp(field, "auth_alg") == 0)
  2570. return ctrl_iface_get_capability_auth_alg(res, strict, &capa,
  2571. buf, buflen);
  2572. if (os_strcmp(field, "modes") == 0)
  2573. return ctrl_iface_get_capability_modes(res, strict, &capa,
  2574. buf, buflen);
  2575. if (os_strcmp(field, "channels") == 0)
  2576. return ctrl_iface_get_capability_channels(wpa_s, buf, buflen);
  2577. if (os_strcmp(field, "freq") == 0)
  2578. return ctrl_iface_get_capability_freq(wpa_s, buf, buflen);
  2579. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Unknown GET_CAPABILITY field '%s'",
  2580. field);
  2581. return -1;
  2582. }
  2583. #ifdef CONFIG_INTERWORKING
  2584. static char * anqp_add_hex(char *pos, char *end, const char *title,
  2585. struct wpabuf *data)
  2586. {
  2587. char *start = pos;
  2588. size_t i;
  2589. int ret;
  2590. const u8 *d;
  2591. if (data == NULL)
  2592. return start;
  2593. ret = os_snprintf(pos, end - pos, "%s=", title);
  2594. if (ret < 0 || ret >= end - pos)
  2595. return start;
  2596. pos += ret;
  2597. d = wpabuf_head_u8(data);
  2598. for (i = 0; i < wpabuf_len(data); i++) {
  2599. ret = os_snprintf(pos, end - pos, "%02x", *d++);
  2600. if (ret < 0 || ret >= end - pos)
  2601. return start;
  2602. pos += ret;
  2603. }
  2604. ret = os_snprintf(pos, end - pos, "\n");
  2605. if (ret < 0 || ret >= end - pos)
  2606. return start;
  2607. pos += ret;
  2608. return pos;
  2609. }
  2610. #endif /* CONFIG_INTERWORKING */
  2611. static int print_bss_info(struct wpa_supplicant *wpa_s, struct wpa_bss *bss,
  2612. unsigned long mask, char *buf, size_t buflen)
  2613. {
  2614. size_t i;
  2615. int ret;
  2616. char *pos, *end;
  2617. const u8 *ie, *ie2;
  2618. pos = buf;
  2619. end = buf + buflen;
  2620. if (mask & WPA_BSS_MASK_ID) {
  2621. ret = os_snprintf(pos, end - pos, "id=%u\n", bss->id);
  2622. if (ret < 0 || ret >= end - pos)
  2623. return 0;
  2624. pos += ret;
  2625. }
  2626. if (mask & WPA_BSS_MASK_BSSID) {
  2627. ret = os_snprintf(pos, end - pos, "bssid=" MACSTR "\n",
  2628. MAC2STR(bss->bssid));
  2629. if (ret < 0 || ret >= end - pos)
  2630. return 0;
  2631. pos += ret;
  2632. }
  2633. if (mask & WPA_BSS_MASK_FREQ) {
  2634. ret = os_snprintf(pos, end - pos, "freq=%d\n", bss->freq);
  2635. if (ret < 0 || ret >= end - pos)
  2636. return 0;
  2637. pos += ret;
  2638. }
  2639. if (mask & WPA_BSS_MASK_BEACON_INT) {
  2640. ret = os_snprintf(pos, end - pos, "beacon_int=%d\n",
  2641. bss->beacon_int);
  2642. if (ret < 0 || ret >= end - pos)
  2643. return 0;
  2644. pos += ret;
  2645. }
  2646. if (mask & WPA_BSS_MASK_CAPABILITIES) {
  2647. ret = os_snprintf(pos, end - pos, "capabilities=0x%04x\n",
  2648. bss->caps);
  2649. if (ret < 0 || ret >= end - pos)
  2650. return 0;
  2651. pos += ret;
  2652. }
  2653. if (mask & WPA_BSS_MASK_QUAL) {
  2654. ret = os_snprintf(pos, end - pos, "qual=%d\n", bss->qual);
  2655. if (ret < 0 || ret >= end - pos)
  2656. return 0;
  2657. pos += ret;
  2658. }
  2659. if (mask & WPA_BSS_MASK_NOISE) {
  2660. ret = os_snprintf(pos, end - pos, "noise=%d\n", bss->noise);
  2661. if (ret < 0 || ret >= end - pos)
  2662. return 0;
  2663. pos += ret;
  2664. }
  2665. if (mask & WPA_BSS_MASK_LEVEL) {
  2666. ret = os_snprintf(pos, end - pos, "level=%d\n", bss->level);
  2667. if (ret < 0 || ret >= end - pos)
  2668. return 0;
  2669. pos += ret;
  2670. }
  2671. if (mask & WPA_BSS_MASK_TSF) {
  2672. ret = os_snprintf(pos, end - pos, "tsf=%016llu\n",
  2673. (unsigned long long) bss->tsf);
  2674. if (ret < 0 || ret >= end - pos)
  2675. return 0;
  2676. pos += ret;
  2677. }
  2678. if (mask & WPA_BSS_MASK_AGE) {
  2679. struct os_time now;
  2680. os_get_time(&now);
  2681. ret = os_snprintf(pos, end - pos, "age=%d\n",
  2682. (int) (now.sec - bss->last_update.sec));
  2683. if (ret < 0 || ret >= end - pos)
  2684. return 0;
  2685. pos += ret;
  2686. }
  2687. if (mask & WPA_BSS_MASK_IE) {
  2688. ret = os_snprintf(pos, end - pos, "ie=");
  2689. if (ret < 0 || ret >= end - pos)
  2690. return 0;
  2691. pos += ret;
  2692. ie = (const u8 *) (bss + 1);
  2693. for (i = 0; i < bss->ie_len; i++) {
  2694. ret = os_snprintf(pos, end - pos, "%02x", *ie++);
  2695. if (ret < 0 || ret >= end - pos)
  2696. return 0;
  2697. pos += ret;
  2698. }
  2699. ret = os_snprintf(pos, end - pos, "\n");
  2700. if (ret < 0 || ret >= end - pos)
  2701. return 0;
  2702. pos += ret;
  2703. }
  2704. if (mask & WPA_BSS_MASK_FLAGS) {
  2705. ret = os_snprintf(pos, end - pos, "flags=");
  2706. if (ret < 0 || ret >= end - pos)
  2707. return 0;
  2708. pos += ret;
  2709. ie = wpa_bss_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
  2710. if (ie)
  2711. pos = wpa_supplicant_ie_txt(pos, end, "WPA", ie,
  2712. 2 + ie[1]);
  2713. ie2 = wpa_bss_get_ie(bss, WLAN_EID_RSN);
  2714. if (ie2)
  2715. pos = wpa_supplicant_ie_txt(pos, end, "WPA2", ie2,
  2716. 2 + ie2[1]);
  2717. pos = wpa_supplicant_wps_ie_txt(wpa_s, pos, end, bss);
  2718. if (!ie && !ie2 && bss->caps & IEEE80211_CAP_PRIVACY) {
  2719. ret = os_snprintf(pos, end - pos, "[WEP]");
  2720. if (ret < 0 || ret >= end - pos)
  2721. return 0;
  2722. pos += ret;
  2723. }
  2724. if (bss->caps & IEEE80211_CAP_IBSS) {
  2725. ret = os_snprintf(pos, end - pos, "[IBSS]");
  2726. if (ret < 0 || ret >= end - pos)
  2727. return 0;
  2728. pos += ret;
  2729. }
  2730. if (bss->caps & IEEE80211_CAP_ESS) {
  2731. ret = os_snprintf(pos, end - pos, "[ESS]");
  2732. if (ret < 0 || ret >= end - pos)
  2733. return 0;
  2734. pos += ret;
  2735. }
  2736. if (wpa_bss_get_vendor_ie(bss, P2P_IE_VENDOR_TYPE)) {
  2737. ret = os_snprintf(pos, end - pos, "[P2P]");
  2738. if (ret < 0 || ret >= end - pos)
  2739. return 0;
  2740. pos += ret;
  2741. }
  2742. #ifdef CONFIG_HS20
  2743. if (wpa_bss_get_vendor_ie(bss, HS20_IE_VENDOR_TYPE)) {
  2744. ret = os_snprintf(pos, end - pos, "[HS20]");
  2745. if (ret < 0 || ret >= end - pos)
  2746. return 0;
  2747. pos += ret;
  2748. }
  2749. #endif /* CONFIG_HS20 */
  2750. ret = os_snprintf(pos, end - pos, "\n");
  2751. if (ret < 0 || ret >= end - pos)
  2752. return 0;
  2753. pos += ret;
  2754. }
  2755. if (mask & WPA_BSS_MASK_SSID) {
  2756. ret = os_snprintf(pos, end - pos, "ssid=%s\n",
  2757. wpa_ssid_txt(bss->ssid, bss->ssid_len));
  2758. if (ret < 0 || ret >= end - pos)
  2759. return 0;
  2760. pos += ret;
  2761. }
  2762. #ifdef CONFIG_WPS
  2763. if (mask & WPA_BSS_MASK_WPS_SCAN) {
  2764. ie = (const u8 *) (bss + 1);
  2765. ret = wpas_wps_scan_result_text(ie, bss->ie_len, pos, end);
  2766. if (ret < 0 || ret >= end - pos)
  2767. return 0;
  2768. pos += ret;
  2769. }
  2770. #endif /* CONFIG_WPS */
  2771. #ifdef CONFIG_P2P
  2772. if (mask & WPA_BSS_MASK_P2P_SCAN) {
  2773. ie = (const u8 *) (bss + 1);
  2774. ret = wpas_p2p_scan_result_text(ie, bss->ie_len, pos, end);
  2775. if (ret < 0 || ret >= end - pos)
  2776. return 0;
  2777. pos += ret;
  2778. }
  2779. #endif /* CONFIG_P2P */
  2780. #ifdef CONFIG_WIFI_DISPLAY
  2781. if (mask & WPA_BSS_MASK_WIFI_DISPLAY) {
  2782. struct wpabuf *wfd;
  2783. ie = (const u8 *) (bss + 1);
  2784. wfd = ieee802_11_vendor_ie_concat(ie, bss->ie_len,
  2785. WFD_IE_VENDOR_TYPE);
  2786. if (wfd) {
  2787. ret = os_snprintf(pos, end - pos, "wfd_subelems=");
  2788. if (ret < 0 || ret >= end - pos)
  2789. return 0;
  2790. pos += ret;
  2791. pos += wpa_snprintf_hex(pos, end - pos,
  2792. wpabuf_head(wfd),
  2793. wpabuf_len(wfd));
  2794. wpabuf_free(wfd);
  2795. ret = os_snprintf(pos, end - pos, "\n");
  2796. if (ret < 0 || ret >= end - pos)
  2797. return 0;
  2798. pos += ret;
  2799. }
  2800. }
  2801. #endif /* CONFIG_WIFI_DISPLAY */
  2802. #ifdef CONFIG_INTERWORKING
  2803. if ((mask & WPA_BSS_MASK_INTERNETW) && bss->anqp) {
  2804. struct wpa_bss_anqp *anqp = bss->anqp;
  2805. pos = anqp_add_hex(pos, end, "anqp_venue_name",
  2806. anqp->venue_name);
  2807. pos = anqp_add_hex(pos, end, "anqp_network_auth_type",
  2808. anqp->network_auth_type);
  2809. pos = anqp_add_hex(pos, end, "anqp_roaming_consortium",
  2810. anqp->roaming_consortium);
  2811. pos = anqp_add_hex(pos, end, "anqp_ip_addr_type_availability",
  2812. anqp->ip_addr_type_availability);
  2813. pos = anqp_add_hex(pos, end, "anqp_nai_realm",
  2814. anqp->nai_realm);
  2815. pos = anqp_add_hex(pos, end, "anqp_3gpp", anqp->anqp_3gpp);
  2816. pos = anqp_add_hex(pos, end, "anqp_domain_name",
  2817. anqp->domain_name);
  2818. #ifdef CONFIG_HS20
  2819. pos = anqp_add_hex(pos, end, "hs20_operator_friendly_name",
  2820. anqp->hs20_operator_friendly_name);
  2821. pos = anqp_add_hex(pos, end, "hs20_wan_metrics",
  2822. anqp->hs20_wan_metrics);
  2823. pos = anqp_add_hex(pos, end, "hs20_connection_capability",
  2824. anqp->hs20_connection_capability);
  2825. #endif /* CONFIG_HS20 */
  2826. }
  2827. #endif /* CONFIG_INTERWORKING */
  2828. if (mask & WPA_BSS_MASK_DELIM) {
  2829. ret = os_snprintf(pos, end - pos, "====\n");
  2830. if (ret < 0 || ret >= end - pos)
  2831. return 0;
  2832. pos += ret;
  2833. }
  2834. return pos - buf;
  2835. }
  2836. static int wpa_supplicant_ctrl_iface_bss(struct wpa_supplicant *wpa_s,
  2837. const char *cmd, char *buf,
  2838. size_t buflen)
  2839. {
  2840. u8 bssid[ETH_ALEN];
  2841. size_t i;
  2842. struct wpa_bss *bss;
  2843. struct wpa_bss *bsslast = NULL;
  2844. struct dl_list *next;
  2845. int ret = 0;
  2846. int len;
  2847. char *ctmp;
  2848. unsigned long mask = WPA_BSS_MASK_ALL;
  2849. if (os_strncmp(cmd, "RANGE=", 6) == 0) {
  2850. if (os_strncmp(cmd + 6, "ALL", 3) == 0) {
  2851. bss = dl_list_first(&wpa_s->bss_id, struct wpa_bss,
  2852. list_id);
  2853. bsslast = dl_list_last(&wpa_s->bss_id, struct wpa_bss,
  2854. list_id);
  2855. } else { /* N1-N2 */
  2856. unsigned int id1, id2;
  2857. if ((ctmp = os_strchr(cmd + 6, '-')) == NULL) {
  2858. wpa_printf(MSG_INFO, "Wrong BSS range "
  2859. "format");
  2860. return 0;
  2861. }
  2862. if (*(cmd + 6) == '-')
  2863. id1 = 0;
  2864. else
  2865. id1 = atoi(cmd + 6);
  2866. ctmp++;
  2867. if (*ctmp >= '0' && *ctmp <= '9')
  2868. id2 = atoi(ctmp);
  2869. else
  2870. id2 = (unsigned int) -1;
  2871. bss = wpa_bss_get_id_range(wpa_s, id1, id2);
  2872. if (id2 == (unsigned int) -1)
  2873. bsslast = dl_list_last(&wpa_s->bss_id,
  2874. struct wpa_bss,
  2875. list_id);
  2876. else {
  2877. bsslast = wpa_bss_get_id(wpa_s, id2);
  2878. if (bsslast == NULL && bss && id2 > id1) {
  2879. struct wpa_bss *tmp = bss;
  2880. for (;;) {
  2881. next = tmp->list_id.next;
  2882. if (next == &wpa_s->bss_id)
  2883. break;
  2884. tmp = dl_list_entry(
  2885. next, struct wpa_bss,
  2886. list_id);
  2887. if (tmp->id > id2)
  2888. break;
  2889. bsslast = tmp;
  2890. }
  2891. }
  2892. }
  2893. }
  2894. } else if (os_strncmp(cmd, "FIRST", 5) == 0)
  2895. bss = dl_list_first(&wpa_s->bss_id, struct wpa_bss, list_id);
  2896. else if (os_strncmp(cmd, "LAST", 4) == 0)
  2897. bss = dl_list_last(&wpa_s->bss_id, struct wpa_bss, list_id);
  2898. else if (os_strncmp(cmd, "ID-", 3) == 0) {
  2899. i = atoi(cmd + 3);
  2900. bss = wpa_bss_get_id(wpa_s, i);
  2901. } else if (os_strncmp(cmd, "NEXT-", 5) == 0) {
  2902. i = atoi(cmd + 5);
  2903. bss = wpa_bss_get_id(wpa_s, i);
  2904. if (bss) {
  2905. next = bss->list_id.next;
  2906. if (next == &wpa_s->bss_id)
  2907. bss = NULL;
  2908. else
  2909. bss = dl_list_entry(next, struct wpa_bss,
  2910. list_id);
  2911. }
  2912. #ifdef CONFIG_P2P
  2913. } else if (os_strncmp(cmd, "p2p_dev_addr=", 13) == 0) {
  2914. if (hwaddr_aton(cmd + 13, bssid) == 0)
  2915. bss = wpa_bss_get_p2p_dev_addr(wpa_s, bssid);
  2916. else
  2917. bss = NULL;
  2918. #endif /* CONFIG_P2P */
  2919. } else if (hwaddr_aton(cmd, bssid) == 0)
  2920. bss = wpa_bss_get_bssid(wpa_s, bssid);
  2921. else {
  2922. struct wpa_bss *tmp;
  2923. i = atoi(cmd);
  2924. bss = NULL;
  2925. dl_list_for_each(tmp, &wpa_s->bss_id, struct wpa_bss, list_id)
  2926. {
  2927. if (i-- == 0) {
  2928. bss = tmp;
  2929. break;
  2930. }
  2931. }
  2932. }
  2933. if ((ctmp = os_strstr(cmd, "MASK=")) != NULL) {
  2934. mask = strtoul(ctmp + 5, NULL, 0x10);
  2935. if (mask == 0)
  2936. mask = WPA_BSS_MASK_ALL;
  2937. }
  2938. if (bss == NULL)
  2939. return 0;
  2940. if (bsslast == NULL)
  2941. bsslast = bss;
  2942. do {
  2943. len = print_bss_info(wpa_s, bss, mask, buf, buflen);
  2944. ret += len;
  2945. buf += len;
  2946. buflen -= len;
  2947. if (bss == bsslast) {
  2948. if ((mask & WPA_BSS_MASK_DELIM) && len &&
  2949. (bss == dl_list_last(&wpa_s->bss_id,
  2950. struct wpa_bss, list_id)))
  2951. os_snprintf(buf - 5, 5, "####\n");
  2952. break;
  2953. }
  2954. next = bss->list_id.next;
  2955. if (next == &wpa_s->bss_id)
  2956. break;
  2957. bss = dl_list_entry(next, struct wpa_bss, list_id);
  2958. } while (bss && len);
  2959. return ret;
  2960. }
  2961. static int wpa_supplicant_ctrl_iface_ap_scan(
  2962. struct wpa_supplicant *wpa_s, char *cmd)
  2963. {
  2964. int ap_scan = atoi(cmd);
  2965. return wpa_supplicant_set_ap_scan(wpa_s, ap_scan);
  2966. }
  2967. static int wpa_supplicant_ctrl_iface_scan_interval(
  2968. struct wpa_supplicant *wpa_s, char *cmd)
  2969. {
  2970. int scan_int = atoi(cmd);
  2971. return wpa_supplicant_set_scan_interval(wpa_s, scan_int);
  2972. }
  2973. static int wpa_supplicant_ctrl_iface_bss_expire_age(
  2974. struct wpa_supplicant *wpa_s, char *cmd)
  2975. {
  2976. int expire_age = atoi(cmd);
  2977. return wpa_supplicant_set_bss_expiration_age(wpa_s, expire_age);
  2978. }
  2979. static int wpa_supplicant_ctrl_iface_bss_expire_count(
  2980. struct wpa_supplicant *wpa_s, char *cmd)
  2981. {
  2982. int expire_count = atoi(cmd);
  2983. return wpa_supplicant_set_bss_expiration_count(wpa_s, expire_count);
  2984. }
  2985. static int wpa_supplicant_ctrl_iface_bss_flush(
  2986. struct wpa_supplicant *wpa_s, char *cmd)
  2987. {
  2988. int flush_age = atoi(cmd);
  2989. if (flush_age == 0)
  2990. wpa_bss_flush(wpa_s);
  2991. else
  2992. wpa_bss_flush_by_age(wpa_s, flush_age);
  2993. return 0;
  2994. }
  2995. static void wpa_supplicant_ctrl_iface_drop_sa(struct wpa_supplicant *wpa_s)
  2996. {
  2997. wpa_printf(MSG_DEBUG, "Dropping SA without deauthentication");
  2998. /* MLME-DELETEKEYS.request */
  2999. wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 0, 0, NULL, 0, NULL, 0);
  3000. wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 1, 0, NULL, 0, NULL, 0);
  3001. wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 2, 0, NULL, 0, NULL, 0);
  3002. wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 3, 0, NULL, 0, NULL, 0);
  3003. #ifdef CONFIG_IEEE80211W
  3004. wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 4, 0, NULL, 0, NULL, 0);
  3005. wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 5, 0, NULL, 0, NULL, 0);
  3006. #endif /* CONFIG_IEEE80211W */
  3007. wpa_drv_set_key(wpa_s, WPA_ALG_NONE, wpa_s->bssid, 0, 0, NULL, 0, NULL,
  3008. 0);
  3009. /* MLME-SETPROTECTION.request(None) */
  3010. wpa_drv_mlme_setprotection(wpa_s, wpa_s->bssid,
  3011. MLME_SETPROTECTION_PROTECT_TYPE_NONE,
  3012. MLME_SETPROTECTION_KEY_TYPE_PAIRWISE);
  3013. wpa_sm_drop_sa(wpa_s->wpa);
  3014. }
  3015. static int wpa_supplicant_ctrl_iface_roam(struct wpa_supplicant *wpa_s,
  3016. char *addr)
  3017. {
  3018. #ifdef CONFIG_NO_SCAN_PROCESSING
  3019. return -1;
  3020. #else /* CONFIG_NO_SCAN_PROCESSING */
  3021. u8 bssid[ETH_ALEN];
  3022. struct wpa_bss *bss;
  3023. struct wpa_ssid *ssid = wpa_s->current_ssid;
  3024. if (hwaddr_aton(addr, bssid)) {
  3025. wpa_printf(MSG_DEBUG, "CTRL_IFACE ROAM: invalid "
  3026. "address '%s'", addr);
  3027. return -1;
  3028. }
  3029. wpa_printf(MSG_DEBUG, "CTRL_IFACE ROAM " MACSTR, MAC2STR(bssid));
  3030. if (!ssid) {
  3031. wpa_printf(MSG_DEBUG, "CTRL_IFACE ROAM: No network "
  3032. "configuration known for the target AP");
  3033. return -1;
  3034. }
  3035. bss = wpa_bss_get(wpa_s, bssid, ssid->ssid, ssid->ssid_len);
  3036. if (!bss) {
  3037. wpa_printf(MSG_DEBUG, "CTRL_IFACE ROAM: Target AP not found "
  3038. "from BSS table");
  3039. return -1;
  3040. }
  3041. /*
  3042. * TODO: Find best network configuration block from configuration to
  3043. * allow roaming to other networks
  3044. */
  3045. wpa_s->reassociate = 1;
  3046. wpa_supplicant_connect(wpa_s, bss, ssid);
  3047. return 0;
  3048. #endif /* CONFIG_NO_SCAN_PROCESSING */
  3049. }
  3050. #ifdef CONFIG_P2P
  3051. static int p2p_ctrl_find(struct wpa_supplicant *wpa_s, char *cmd)
  3052. {
  3053. unsigned int timeout = atoi(cmd);
  3054. enum p2p_discovery_type type = P2P_FIND_START_WITH_FULL;
  3055. u8 dev_id[ETH_ALEN], *_dev_id = NULL;
  3056. char *pos;
  3057. unsigned int search_delay;
  3058. if (os_strstr(cmd, "type=social"))
  3059. type = P2P_FIND_ONLY_SOCIAL;
  3060. else if (os_strstr(cmd, "type=progressive"))
  3061. type = P2P_FIND_PROGRESSIVE;
  3062. pos = os_strstr(cmd, "dev_id=");
  3063. if (pos) {
  3064. pos += 7;
  3065. if (hwaddr_aton(pos, dev_id))
  3066. return -1;
  3067. _dev_id = dev_id;
  3068. }
  3069. pos = os_strstr(cmd, "delay=");
  3070. if (pos) {
  3071. pos += 6;
  3072. search_delay = atoi(pos);
  3073. } else
  3074. search_delay = wpas_p2p_search_delay(wpa_s);
  3075. return wpas_p2p_find(wpa_s, timeout, type, 0, NULL, _dev_id,
  3076. search_delay);
  3077. }
  3078. static int p2p_ctrl_connect(struct wpa_supplicant *wpa_s, char *cmd,
  3079. char *buf, size_t buflen)
  3080. {
  3081. u8 addr[ETH_ALEN];
  3082. char *pos, *pos2;
  3083. char *pin = NULL;
  3084. enum p2p_wps_method wps_method;
  3085. int new_pin;
  3086. int ret;
  3087. int persistent_group, persistent_id = -1;
  3088. int join;
  3089. int auth;
  3090. int automatic;
  3091. int go_intent = -1;
  3092. int freq = 0;
  3093. int pd;
  3094. int ht40;
  3095. /* <addr> <"pbc" | "pin" | PIN> [label|display|keypad]
  3096. * [persistent|persistent=<network id>]
  3097. * [join] [auth] [go_intent=<0..15>] [freq=<in MHz>] [provdisc]
  3098. * [ht40] */
  3099. if (hwaddr_aton(cmd, addr))
  3100. return -1;
  3101. pos = cmd + 17;
  3102. if (*pos != ' ')
  3103. return -1;
  3104. pos++;
  3105. persistent_group = os_strstr(pos, " persistent") != NULL;
  3106. pos2 = os_strstr(pos, " persistent=");
  3107. if (pos2) {
  3108. struct wpa_ssid *ssid;
  3109. persistent_id = atoi(pos2 + 12);
  3110. ssid = wpa_config_get_network(wpa_s->conf, persistent_id);
  3111. if (ssid == NULL || ssid->disabled != 2 ||
  3112. ssid->mode != WPAS_MODE_P2P_GO) {
  3113. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find "
  3114. "SSID id=%d for persistent P2P group (GO)",
  3115. persistent_id);
  3116. return -1;
  3117. }
  3118. }
  3119. join = os_strstr(pos, " join") != NULL;
  3120. auth = os_strstr(pos, " auth") != NULL;
  3121. automatic = os_strstr(pos, " auto") != NULL;
  3122. pd = os_strstr(pos, " provdisc") != NULL;
  3123. ht40 = (os_strstr(cmd, " ht40") != NULL) || wpa_s->conf->p2p_go_ht40;
  3124. pos2 = os_strstr(pos, " go_intent=");
  3125. if (pos2) {
  3126. pos2 += 11;
  3127. go_intent = atoi(pos2);
  3128. if (go_intent < 0 || go_intent > 15)
  3129. return -1;
  3130. }
  3131. pos2 = os_strstr(pos, " freq=");
  3132. if (pos2) {
  3133. pos2 += 6;
  3134. freq = atoi(pos2);
  3135. if (freq <= 0)
  3136. return -1;
  3137. }
  3138. if (os_strncmp(pos, "pin", 3) == 0) {
  3139. /* Request random PIN (to be displayed) and enable the PIN */
  3140. wps_method = WPS_PIN_DISPLAY;
  3141. } else if (os_strncmp(pos, "pbc", 3) == 0) {
  3142. wps_method = WPS_PBC;
  3143. } else {
  3144. pin = pos;
  3145. pos = os_strchr(pin, ' ');
  3146. wps_method = WPS_PIN_KEYPAD;
  3147. if (pos) {
  3148. *pos++ = '\0';
  3149. if (os_strncmp(pos, "display", 7) == 0)
  3150. wps_method = WPS_PIN_DISPLAY;
  3151. }
  3152. if (!wps_pin_str_valid(pin)) {
  3153. os_memcpy(buf, "FAIL-INVALID-PIN\n", 17);
  3154. return 17;
  3155. }
  3156. }
  3157. new_pin = wpas_p2p_connect(wpa_s, addr, pin, wps_method,
  3158. persistent_group, automatic, join,
  3159. auth, go_intent, freq, persistent_id, pd,
  3160. ht40);
  3161. if (new_pin == -2) {
  3162. os_memcpy(buf, "FAIL-CHANNEL-UNAVAILABLE\n", 25);
  3163. return 25;
  3164. }
  3165. if (new_pin == -3) {
  3166. os_memcpy(buf, "FAIL-CHANNEL-UNSUPPORTED\n", 25);
  3167. return 25;
  3168. }
  3169. if (new_pin < 0)
  3170. return -1;
  3171. if (wps_method == WPS_PIN_DISPLAY && pin == NULL) {
  3172. ret = os_snprintf(buf, buflen, "%08d", new_pin);
  3173. if (ret < 0 || (size_t) ret >= buflen)
  3174. return -1;
  3175. return ret;
  3176. }
  3177. os_memcpy(buf, "OK\n", 3);
  3178. return 3;
  3179. }
  3180. static int p2p_ctrl_listen(struct wpa_supplicant *wpa_s, char *cmd)
  3181. {
  3182. unsigned int timeout = atoi(cmd);
  3183. return wpas_p2p_listen(wpa_s, timeout);
  3184. }
  3185. static int p2p_ctrl_prov_disc(struct wpa_supplicant *wpa_s, char *cmd)
  3186. {
  3187. u8 addr[ETH_ALEN];
  3188. char *pos;
  3189. enum wpas_p2p_prov_disc_use use = WPAS_P2P_PD_FOR_GO_NEG;
  3190. /* <addr> <config method> [join|auto] */
  3191. if (hwaddr_aton(cmd, addr))
  3192. return -1;
  3193. pos = cmd + 17;
  3194. if (*pos != ' ')
  3195. return -1;
  3196. pos++;
  3197. if (os_strstr(pos, " join") != NULL)
  3198. use = WPAS_P2P_PD_FOR_JOIN;
  3199. else if (os_strstr(pos, " auto") != NULL)
  3200. use = WPAS_P2P_PD_AUTO;
  3201. return wpas_p2p_prov_disc(wpa_s, addr, pos, use);
  3202. }
  3203. static int p2p_get_passphrase(struct wpa_supplicant *wpa_s, char *buf,
  3204. size_t buflen)
  3205. {
  3206. struct wpa_ssid *ssid = wpa_s->current_ssid;
  3207. if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GO ||
  3208. ssid->passphrase == NULL)
  3209. return -1;
  3210. os_strlcpy(buf, ssid->passphrase, buflen);
  3211. return os_strlen(buf);
  3212. }
  3213. static int p2p_ctrl_serv_disc_req(struct wpa_supplicant *wpa_s, char *cmd,
  3214. char *buf, size_t buflen)
  3215. {
  3216. u64 ref;
  3217. int res;
  3218. u8 dst_buf[ETH_ALEN], *dst;
  3219. struct wpabuf *tlvs;
  3220. char *pos;
  3221. size_t len;
  3222. if (hwaddr_aton(cmd, dst_buf))
  3223. return -1;
  3224. dst = dst_buf;
  3225. if (dst[0] == 0 && dst[1] == 0 && dst[2] == 0 &&
  3226. dst[3] == 0 && dst[4] == 0 && dst[5] == 0)
  3227. dst = NULL;
  3228. pos = cmd + 17;
  3229. if (*pos != ' ')
  3230. return -1;
  3231. pos++;
  3232. if (os_strncmp(pos, "upnp ", 5) == 0) {
  3233. u8 version;
  3234. pos += 5;
  3235. if (hexstr2bin(pos, &version, 1) < 0)
  3236. return -1;
  3237. pos += 2;
  3238. if (*pos != ' ')
  3239. return -1;
  3240. pos++;
  3241. ref = wpas_p2p_sd_request_upnp(wpa_s, dst, version, pos);
  3242. #ifdef CONFIG_WIFI_DISPLAY
  3243. } else if (os_strncmp(pos, "wifi-display ", 13) == 0) {
  3244. ref = wpas_p2p_sd_request_wifi_display(wpa_s, dst, pos + 13);
  3245. #endif /* CONFIG_WIFI_DISPLAY */
  3246. } else {
  3247. len = os_strlen(pos);
  3248. if (len & 1)
  3249. return -1;
  3250. len /= 2;
  3251. tlvs = wpabuf_alloc(len);
  3252. if (tlvs == NULL)
  3253. return -1;
  3254. if (hexstr2bin(pos, wpabuf_put(tlvs, len), len) < 0) {
  3255. wpabuf_free(tlvs);
  3256. return -1;
  3257. }
  3258. ref = wpas_p2p_sd_request(wpa_s, dst, tlvs);
  3259. wpabuf_free(tlvs);
  3260. }
  3261. if (ref == 0)
  3262. return -1;
  3263. res = os_snprintf(buf, buflen, "%llx", (long long unsigned) ref);
  3264. if (res < 0 || (unsigned) res >= buflen)
  3265. return -1;
  3266. return res;
  3267. }
  3268. static int p2p_ctrl_serv_disc_cancel_req(struct wpa_supplicant *wpa_s,
  3269. char *cmd)
  3270. {
  3271. long long unsigned val;
  3272. u64 req;
  3273. if (sscanf(cmd, "%llx", &val) != 1)
  3274. return -1;
  3275. req = val;
  3276. return wpas_p2p_sd_cancel_request(wpa_s, req);
  3277. }
  3278. static int p2p_ctrl_serv_disc_resp(struct wpa_supplicant *wpa_s, char *cmd)
  3279. {
  3280. int freq;
  3281. u8 dst[ETH_ALEN];
  3282. u8 dialog_token;
  3283. struct wpabuf *resp_tlvs;
  3284. char *pos, *pos2;
  3285. size_t len;
  3286. pos = os_strchr(cmd, ' ');
  3287. if (pos == NULL)
  3288. return -1;
  3289. *pos++ = '\0';
  3290. freq = atoi(cmd);
  3291. if (freq == 0)
  3292. return -1;
  3293. if (hwaddr_aton(pos, dst))
  3294. return -1;
  3295. pos += 17;
  3296. if (*pos != ' ')
  3297. return -1;
  3298. pos++;
  3299. pos2 = os_strchr(pos, ' ');
  3300. if (pos2 == NULL)
  3301. return -1;
  3302. *pos2++ = '\0';
  3303. dialog_token = atoi(pos);
  3304. len = os_strlen(pos2);
  3305. if (len & 1)
  3306. return -1;
  3307. len /= 2;
  3308. resp_tlvs = wpabuf_alloc(len);
  3309. if (resp_tlvs == NULL)
  3310. return -1;
  3311. if (hexstr2bin(pos2, wpabuf_put(resp_tlvs, len), len) < 0) {
  3312. wpabuf_free(resp_tlvs);
  3313. return -1;
  3314. }
  3315. wpas_p2p_sd_response(wpa_s, freq, dst, dialog_token, resp_tlvs);
  3316. wpabuf_free(resp_tlvs);
  3317. return 0;
  3318. }
  3319. static int p2p_ctrl_serv_disc_external(struct wpa_supplicant *wpa_s,
  3320. char *cmd)
  3321. {
  3322. if (os_strcmp(cmd, "0") && os_strcmp(cmd, "1"))
  3323. return -1;
  3324. wpa_s->p2p_sd_over_ctrl_iface = atoi(cmd);
  3325. return 0;
  3326. }
  3327. static int p2p_ctrl_service_add_bonjour(struct wpa_supplicant *wpa_s,
  3328. char *cmd)
  3329. {
  3330. char *pos;
  3331. size_t len;
  3332. struct wpabuf *query, *resp;
  3333. pos = os_strchr(cmd, ' ');
  3334. if (pos == NULL)
  3335. return -1;
  3336. *pos++ = '\0';
  3337. len = os_strlen(cmd);
  3338. if (len & 1)
  3339. return -1;
  3340. len /= 2;
  3341. query = wpabuf_alloc(len);
  3342. if (query == NULL)
  3343. return -1;
  3344. if (hexstr2bin(cmd, wpabuf_put(query, len), len) < 0) {
  3345. wpabuf_free(query);
  3346. return -1;
  3347. }
  3348. len = os_strlen(pos);
  3349. if (len & 1) {
  3350. wpabuf_free(query);
  3351. return -1;
  3352. }
  3353. len /= 2;
  3354. resp = wpabuf_alloc(len);
  3355. if (resp == NULL) {
  3356. wpabuf_free(query);
  3357. return -1;
  3358. }
  3359. if (hexstr2bin(pos, wpabuf_put(resp, len), len) < 0) {
  3360. wpabuf_free(query);
  3361. wpabuf_free(resp);
  3362. return -1;
  3363. }
  3364. if (wpas_p2p_service_add_bonjour(wpa_s, query, resp) < 0) {
  3365. wpabuf_free(query);
  3366. wpabuf_free(resp);
  3367. return -1;
  3368. }
  3369. return 0;
  3370. }
  3371. static int p2p_ctrl_service_add_upnp(struct wpa_supplicant *wpa_s, char *cmd)
  3372. {
  3373. char *pos;
  3374. u8 version;
  3375. pos = os_strchr(cmd, ' ');
  3376. if (pos == NULL)
  3377. return -1;
  3378. *pos++ = '\0';
  3379. if (hexstr2bin(cmd, &version, 1) < 0)
  3380. return -1;
  3381. return wpas_p2p_service_add_upnp(wpa_s, version, pos);
  3382. }
  3383. static int p2p_ctrl_service_add(struct wpa_supplicant *wpa_s, char *cmd)
  3384. {
  3385. char *pos;
  3386. pos = os_strchr(cmd, ' ');
  3387. if (pos == NULL)
  3388. return -1;
  3389. *pos++ = '\0';
  3390. if (os_strcmp(cmd, "bonjour") == 0)
  3391. return p2p_ctrl_service_add_bonjour(wpa_s, pos);
  3392. if (os_strcmp(cmd, "upnp") == 0)
  3393. return p2p_ctrl_service_add_upnp(wpa_s, pos);
  3394. wpa_printf(MSG_DEBUG, "Unknown service '%s'", cmd);
  3395. return -1;
  3396. }
  3397. static int p2p_ctrl_service_del_bonjour(struct wpa_supplicant *wpa_s,
  3398. char *cmd)
  3399. {
  3400. size_t len;
  3401. struct wpabuf *query;
  3402. int ret;
  3403. len = os_strlen(cmd);
  3404. if (len & 1)
  3405. return -1;
  3406. len /= 2;
  3407. query = wpabuf_alloc(len);
  3408. if (query == NULL)
  3409. return -1;
  3410. if (hexstr2bin(cmd, wpabuf_put(query, len), len) < 0) {
  3411. wpabuf_free(query);
  3412. return -1;
  3413. }
  3414. ret = wpas_p2p_service_del_bonjour(wpa_s, query);
  3415. wpabuf_free(query);
  3416. return ret;
  3417. }
  3418. static int p2p_ctrl_service_del_upnp(struct wpa_supplicant *wpa_s, char *cmd)
  3419. {
  3420. char *pos;
  3421. u8 version;
  3422. pos = os_strchr(cmd, ' ');
  3423. if (pos == NULL)
  3424. return -1;
  3425. *pos++ = '\0';
  3426. if (hexstr2bin(cmd, &version, 1) < 0)
  3427. return -1;
  3428. return wpas_p2p_service_del_upnp(wpa_s, version, pos);
  3429. }
  3430. static int p2p_ctrl_service_del(struct wpa_supplicant *wpa_s, char *cmd)
  3431. {
  3432. char *pos;
  3433. pos = os_strchr(cmd, ' ');
  3434. if (pos == NULL)
  3435. return -1;
  3436. *pos++ = '\0';
  3437. if (os_strcmp(cmd, "bonjour") == 0)
  3438. return p2p_ctrl_service_del_bonjour(wpa_s, pos);
  3439. if (os_strcmp(cmd, "upnp") == 0)
  3440. return p2p_ctrl_service_del_upnp(wpa_s, pos);
  3441. wpa_printf(MSG_DEBUG, "Unknown service '%s'", cmd);
  3442. return -1;
  3443. }
  3444. static int p2p_ctrl_reject(struct wpa_supplicant *wpa_s, char *cmd)
  3445. {
  3446. u8 addr[ETH_ALEN];
  3447. /* <addr> */
  3448. if (hwaddr_aton(cmd, addr))
  3449. return -1;
  3450. return wpas_p2p_reject(wpa_s, addr);
  3451. }
  3452. static int p2p_ctrl_invite_persistent(struct wpa_supplicant *wpa_s, char *cmd)
  3453. {
  3454. char *pos;
  3455. int id;
  3456. struct wpa_ssid *ssid;
  3457. u8 *_peer = NULL, peer[ETH_ALEN];
  3458. int freq = 0, pref_freq = 0;
  3459. int ht40;
  3460. id = atoi(cmd);
  3461. pos = os_strstr(cmd, " peer=");
  3462. if (pos) {
  3463. pos += 6;
  3464. if (hwaddr_aton(pos, peer))
  3465. return -1;
  3466. _peer = peer;
  3467. }
  3468. ssid = wpa_config_get_network(wpa_s->conf, id);
  3469. if (ssid == NULL || ssid->disabled != 2) {
  3470. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find SSID id=%d "
  3471. "for persistent P2P group",
  3472. id);
  3473. return -1;
  3474. }
  3475. pos = os_strstr(cmd, " freq=");
  3476. if (pos) {
  3477. pos += 6;
  3478. freq = atoi(pos);
  3479. if (freq <= 0)
  3480. return -1;
  3481. }
  3482. pos = os_strstr(cmd, " pref=");
  3483. if (pos) {
  3484. pos += 6;
  3485. pref_freq = atoi(pos);
  3486. if (pref_freq <= 0)
  3487. return -1;
  3488. }
  3489. ht40 = (os_strstr(cmd, " ht40") != NULL) || wpa_s->conf->p2p_go_ht40;
  3490. return wpas_p2p_invite(wpa_s, _peer, ssid, NULL, freq, ht40, pref_freq);
  3491. }
  3492. static int p2p_ctrl_invite_group(struct wpa_supplicant *wpa_s, char *cmd)
  3493. {
  3494. char *pos;
  3495. u8 peer[ETH_ALEN], go_dev_addr[ETH_ALEN], *go_dev = NULL;
  3496. pos = os_strstr(cmd, " peer=");
  3497. if (!pos)
  3498. return -1;
  3499. *pos = '\0';
  3500. pos += 6;
  3501. if (hwaddr_aton(pos, peer)) {
  3502. wpa_printf(MSG_DEBUG, "P2P: Invalid MAC address '%s'", pos);
  3503. return -1;
  3504. }
  3505. pos = os_strstr(pos, " go_dev_addr=");
  3506. if (pos) {
  3507. pos += 13;
  3508. if (hwaddr_aton(pos, go_dev_addr)) {
  3509. wpa_printf(MSG_DEBUG, "P2P: Invalid MAC address '%s'",
  3510. pos);
  3511. return -1;
  3512. }
  3513. go_dev = go_dev_addr;
  3514. }
  3515. return wpas_p2p_invite_group(wpa_s, cmd, peer, go_dev);
  3516. }
  3517. static int p2p_ctrl_invite(struct wpa_supplicant *wpa_s, char *cmd)
  3518. {
  3519. if (os_strncmp(cmd, "persistent=", 11) == 0)
  3520. return p2p_ctrl_invite_persistent(wpa_s, cmd + 11);
  3521. if (os_strncmp(cmd, "group=", 6) == 0)
  3522. return p2p_ctrl_invite_group(wpa_s, cmd + 6);
  3523. return -1;
  3524. }
  3525. static int p2p_ctrl_group_add_persistent(struct wpa_supplicant *wpa_s,
  3526. char *cmd, int freq, int ht40)
  3527. {
  3528. int id;
  3529. struct wpa_ssid *ssid;
  3530. id = atoi(cmd);
  3531. ssid = wpa_config_get_network(wpa_s->conf, id);
  3532. if (ssid == NULL || ssid->disabled != 2) {
  3533. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find SSID id=%d "
  3534. "for persistent P2P group",
  3535. id);
  3536. return -1;
  3537. }
  3538. return wpas_p2p_group_add_persistent(wpa_s, ssid, 0, freq, ht40, NULL);
  3539. }
  3540. static int p2p_ctrl_group_add(struct wpa_supplicant *wpa_s, char *cmd)
  3541. {
  3542. int freq = 0, ht40;
  3543. char *pos;
  3544. pos = os_strstr(cmd, "freq=");
  3545. if (pos)
  3546. freq = atoi(pos + 5);
  3547. ht40 = (os_strstr(cmd, "ht40") != NULL) || wpa_s->conf->p2p_go_ht40;
  3548. if (os_strncmp(cmd, "persistent=", 11) == 0)
  3549. return p2p_ctrl_group_add_persistent(wpa_s, cmd + 11, freq,
  3550. ht40);
  3551. if (os_strcmp(cmd, "persistent") == 0 ||
  3552. os_strncmp(cmd, "persistent ", 11) == 0)
  3553. return wpas_p2p_group_add(wpa_s, 1, freq, ht40);
  3554. if (os_strncmp(cmd, "freq=", 5) == 0)
  3555. return wpas_p2p_group_add(wpa_s, 0, freq, ht40);
  3556. if (ht40)
  3557. return wpas_p2p_group_add(wpa_s, 0, freq, ht40);
  3558. wpa_printf(MSG_DEBUG, "CTRL: Invalid P2P_GROUP_ADD parameters '%s'",
  3559. cmd);
  3560. return -1;
  3561. }
  3562. static int p2p_ctrl_peer(struct wpa_supplicant *wpa_s, char *cmd,
  3563. char *buf, size_t buflen)
  3564. {
  3565. u8 addr[ETH_ALEN], *addr_ptr;
  3566. int next, res;
  3567. const struct p2p_peer_info *info;
  3568. char *pos, *end;
  3569. char devtype[WPS_DEV_TYPE_BUFSIZE];
  3570. struct wpa_ssid *ssid;
  3571. size_t i;
  3572. if (!wpa_s->global->p2p)
  3573. return -1;
  3574. if (os_strcmp(cmd, "FIRST") == 0) {
  3575. addr_ptr = NULL;
  3576. next = 0;
  3577. } else if (os_strncmp(cmd, "NEXT-", 5) == 0) {
  3578. if (hwaddr_aton(cmd + 5, addr) < 0)
  3579. return -1;
  3580. addr_ptr = addr;
  3581. next = 1;
  3582. } else {
  3583. if (hwaddr_aton(cmd, addr) < 0)
  3584. return -1;
  3585. addr_ptr = addr;
  3586. next = 0;
  3587. }
  3588. info = p2p_get_peer_info(wpa_s->global->p2p, addr_ptr, next);
  3589. if (info == NULL)
  3590. return -1;
  3591. pos = buf;
  3592. end = buf + buflen;
  3593. res = os_snprintf(pos, end - pos, MACSTR "\n"
  3594. "pri_dev_type=%s\n"
  3595. "device_name=%s\n"
  3596. "manufacturer=%s\n"
  3597. "model_name=%s\n"
  3598. "model_number=%s\n"
  3599. "serial_number=%s\n"
  3600. "config_methods=0x%x\n"
  3601. "dev_capab=0x%x\n"
  3602. "group_capab=0x%x\n"
  3603. "level=%d\n",
  3604. MAC2STR(info->p2p_device_addr),
  3605. wps_dev_type_bin2str(info->pri_dev_type,
  3606. devtype, sizeof(devtype)),
  3607. info->device_name,
  3608. info->manufacturer,
  3609. info->model_name,
  3610. info->model_number,
  3611. info->serial_number,
  3612. info->config_methods,
  3613. info->dev_capab,
  3614. info->group_capab,
  3615. info->level);
  3616. if (res < 0 || res >= end - pos)
  3617. return pos - buf;
  3618. pos += res;
  3619. for (i = 0; i < info->wps_sec_dev_type_list_len / WPS_DEV_TYPE_LEN; i++)
  3620. {
  3621. const u8 *t;
  3622. t = &info->wps_sec_dev_type_list[i * WPS_DEV_TYPE_LEN];
  3623. res = os_snprintf(pos, end - pos, "sec_dev_type=%s\n",
  3624. wps_dev_type_bin2str(t, devtype,
  3625. sizeof(devtype)));
  3626. if (res < 0 || res >= end - pos)
  3627. return pos - buf;
  3628. pos += res;
  3629. }
  3630. ssid = wpas_p2p_get_persistent(wpa_s, info->p2p_device_addr, NULL, 0);
  3631. if (ssid) {
  3632. res = os_snprintf(pos, end - pos, "persistent=%d\n", ssid->id);
  3633. if (res < 0 || res >= end - pos)
  3634. return pos - buf;
  3635. pos += res;
  3636. }
  3637. res = p2p_get_peer_info_txt(info, pos, end - pos);
  3638. if (res < 0)
  3639. return pos - buf;
  3640. pos += res;
  3641. return pos - buf;
  3642. }
  3643. static int p2p_ctrl_disallow_freq(struct wpa_supplicant *wpa_s,
  3644. const char *param)
  3645. {
  3646. struct wpa_freq_range *freq = NULL, *n;
  3647. unsigned int count = 0, i;
  3648. const char *pos, *pos2, *pos3;
  3649. if (wpa_s->global->p2p == NULL)
  3650. return -1;
  3651. /*
  3652. * param includes comma separated frequency range.
  3653. * For example: 2412-2432,2462,5000-6000
  3654. */
  3655. pos = param;
  3656. while (pos && pos[0]) {
  3657. n = os_realloc_array(freq, count + 1,
  3658. sizeof(struct wpa_freq_range));
  3659. if (n == NULL) {
  3660. os_free(freq);
  3661. return -1;
  3662. }
  3663. freq = n;
  3664. freq[count].min = atoi(pos);
  3665. pos2 = os_strchr(pos, '-');
  3666. pos3 = os_strchr(pos, ',');
  3667. if (pos2 && (!pos3 || pos2 < pos3)) {
  3668. pos2++;
  3669. freq[count].max = atoi(pos2);
  3670. } else
  3671. freq[count].max = freq[count].min;
  3672. pos = pos3;
  3673. if (pos)
  3674. pos++;
  3675. count++;
  3676. }
  3677. for (i = 0; i < count; i++) {
  3678. wpa_printf(MSG_DEBUG, "P2P: Disallowed frequency range %u-%u",
  3679. freq[i].min, freq[i].max);
  3680. }
  3681. os_free(wpa_s->global->p2p_disallow_freq);
  3682. wpa_s->global->p2p_disallow_freq = freq;
  3683. wpa_s->global->num_p2p_disallow_freq = count;
  3684. wpas_p2p_update_channel_list(wpa_s);
  3685. return 0;
  3686. }
  3687. static int p2p_ctrl_set(struct wpa_supplicant *wpa_s, char *cmd)
  3688. {
  3689. char *param;
  3690. if (wpa_s->global->p2p == NULL)
  3691. return -1;
  3692. param = os_strchr(cmd, ' ');
  3693. if (param == NULL)
  3694. return -1;
  3695. *param++ = '\0';
  3696. if (os_strcmp(cmd, "discoverability") == 0) {
  3697. p2p_set_client_discoverability(wpa_s->global->p2p,
  3698. atoi(param));
  3699. return 0;
  3700. }
  3701. if (os_strcmp(cmd, "managed") == 0) {
  3702. p2p_set_managed_oper(wpa_s->global->p2p, atoi(param));
  3703. return 0;
  3704. }
  3705. if (os_strcmp(cmd, "listen_channel") == 0) {
  3706. return p2p_set_listen_channel(wpa_s->global->p2p, 81,
  3707. atoi(param));
  3708. }
  3709. if (os_strcmp(cmd, "ssid_postfix") == 0) {
  3710. return p2p_set_ssid_postfix(wpa_s->global->p2p, (u8 *) param,
  3711. os_strlen(param));
  3712. }
  3713. if (os_strcmp(cmd, "noa") == 0) {
  3714. char *pos;
  3715. int count, start, duration;
  3716. /* GO NoA parameters: count,start_offset(ms),duration(ms) */
  3717. count = atoi(param);
  3718. pos = os_strchr(param, ',');
  3719. if (pos == NULL)
  3720. return -1;
  3721. pos++;
  3722. start = atoi(pos);
  3723. pos = os_strchr(pos, ',');
  3724. if (pos == NULL)
  3725. return -1;
  3726. pos++;
  3727. duration = atoi(pos);
  3728. if (count < 0 || count > 255 || start < 0 || duration < 0)
  3729. return -1;
  3730. if (count == 0 && duration > 0)
  3731. return -1;
  3732. wpa_printf(MSG_DEBUG, "CTRL_IFACE: P2P_SET GO NoA: count=%d "
  3733. "start=%d duration=%d", count, start, duration);
  3734. return wpas_p2p_set_noa(wpa_s, count, start, duration);
  3735. }
  3736. if (os_strcmp(cmd, "ps") == 0)
  3737. return wpa_drv_set_p2p_powersave(wpa_s, atoi(param), -1, -1);
  3738. if (os_strcmp(cmd, "oppps") == 0)
  3739. return wpa_drv_set_p2p_powersave(wpa_s, -1, atoi(param), -1);
  3740. if (os_strcmp(cmd, "ctwindow") == 0)
  3741. return wpa_drv_set_p2p_powersave(wpa_s, -1, -1, atoi(param));
  3742. if (os_strcmp(cmd, "disabled") == 0) {
  3743. wpa_s->global->p2p_disabled = atoi(param);
  3744. wpa_printf(MSG_DEBUG, "P2P functionality %s",
  3745. wpa_s->global->p2p_disabled ?
  3746. "disabled" : "enabled");
  3747. if (wpa_s->global->p2p_disabled) {
  3748. wpas_p2p_stop_find(wpa_s);
  3749. os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN);
  3750. p2p_flush(wpa_s->global->p2p);
  3751. }
  3752. return 0;
  3753. }
  3754. if (os_strcmp(cmd, "conc_pref") == 0) {
  3755. if (os_strcmp(param, "sta") == 0)
  3756. wpa_s->global->conc_pref = WPA_CONC_PREF_STA;
  3757. else if (os_strcmp(param, "p2p") == 0)
  3758. wpa_s->global->conc_pref = WPA_CONC_PREF_P2P;
  3759. else {
  3760. wpa_printf(MSG_INFO, "Invalid conc_pref value");
  3761. return -1;
  3762. }
  3763. wpa_printf(MSG_DEBUG, "Single channel concurrency preference: "
  3764. "%s", param);
  3765. return 0;
  3766. }
  3767. if (os_strcmp(cmd, "force_long_sd") == 0) {
  3768. wpa_s->force_long_sd = atoi(param);
  3769. return 0;
  3770. }
  3771. if (os_strcmp(cmd, "peer_filter") == 0) {
  3772. u8 addr[ETH_ALEN];
  3773. if (hwaddr_aton(param, addr))
  3774. return -1;
  3775. p2p_set_peer_filter(wpa_s->global->p2p, addr);
  3776. return 0;
  3777. }
  3778. if (os_strcmp(cmd, "cross_connect") == 0)
  3779. return wpas_p2p_set_cross_connect(wpa_s, atoi(param));
  3780. if (os_strcmp(cmd, "go_apsd") == 0) {
  3781. if (os_strcmp(param, "disable") == 0)
  3782. wpa_s->set_ap_uapsd = 0;
  3783. else {
  3784. wpa_s->set_ap_uapsd = 1;
  3785. wpa_s->ap_uapsd = atoi(param);
  3786. }
  3787. return 0;
  3788. }
  3789. if (os_strcmp(cmd, "client_apsd") == 0) {
  3790. if (os_strcmp(param, "disable") == 0)
  3791. wpa_s->set_sta_uapsd = 0;
  3792. else {
  3793. int be, bk, vi, vo;
  3794. char *pos;
  3795. /* format: BE,BK,VI,VO;max SP Length */
  3796. be = atoi(param);
  3797. pos = os_strchr(param, ',');
  3798. if (pos == NULL)
  3799. return -1;
  3800. pos++;
  3801. bk = atoi(pos);
  3802. pos = os_strchr(pos, ',');
  3803. if (pos == NULL)
  3804. return -1;
  3805. pos++;
  3806. vi = atoi(pos);
  3807. pos = os_strchr(pos, ',');
  3808. if (pos == NULL)
  3809. return -1;
  3810. pos++;
  3811. vo = atoi(pos);
  3812. /* ignore max SP Length for now */
  3813. wpa_s->set_sta_uapsd = 1;
  3814. wpa_s->sta_uapsd = 0;
  3815. if (be)
  3816. wpa_s->sta_uapsd |= BIT(0);
  3817. if (bk)
  3818. wpa_s->sta_uapsd |= BIT(1);
  3819. if (vi)
  3820. wpa_s->sta_uapsd |= BIT(2);
  3821. if (vo)
  3822. wpa_s->sta_uapsd |= BIT(3);
  3823. }
  3824. return 0;
  3825. }
  3826. if (os_strcmp(cmd, "disallow_freq") == 0)
  3827. return p2p_ctrl_disallow_freq(wpa_s, param);
  3828. if (os_strcmp(cmd, "disc_int") == 0) {
  3829. int min_disc_int, max_disc_int, max_disc_tu;
  3830. char *pos;
  3831. pos = param;
  3832. min_disc_int = atoi(pos);
  3833. pos = os_strchr(pos, ' ');
  3834. if (pos == NULL)
  3835. return -1;
  3836. *pos++ = '\0';
  3837. max_disc_int = atoi(pos);
  3838. pos = os_strchr(pos, ' ');
  3839. if (pos == NULL)
  3840. return -1;
  3841. *pos++ = '\0';
  3842. max_disc_tu = atoi(pos);
  3843. return p2p_set_disc_int(wpa_s->global->p2p, min_disc_int,
  3844. max_disc_int, max_disc_tu);
  3845. }
  3846. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Unknown P2P_SET field value '%s'",
  3847. cmd);
  3848. return -1;
  3849. }
  3850. static void p2p_ctrl_flush(struct wpa_supplicant *wpa_s)
  3851. {
  3852. os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN);
  3853. wpa_s->force_long_sd = 0;
  3854. if (wpa_s->global->p2p)
  3855. p2p_flush(wpa_s->global->p2p);
  3856. }
  3857. static int p2p_ctrl_presence_req(struct wpa_supplicant *wpa_s, char *cmd)
  3858. {
  3859. char *pos, *pos2;
  3860. unsigned int dur1 = 0, int1 = 0, dur2 = 0, int2 = 0;
  3861. if (cmd[0]) {
  3862. pos = os_strchr(cmd, ' ');
  3863. if (pos == NULL)
  3864. return -1;
  3865. *pos++ = '\0';
  3866. dur1 = atoi(cmd);
  3867. pos2 = os_strchr(pos, ' ');
  3868. if (pos2)
  3869. *pos2++ = '\0';
  3870. int1 = atoi(pos);
  3871. } else
  3872. pos2 = NULL;
  3873. if (pos2) {
  3874. pos = os_strchr(pos2, ' ');
  3875. if (pos == NULL)
  3876. return -1;
  3877. *pos++ = '\0';
  3878. dur2 = atoi(pos2);
  3879. int2 = atoi(pos);
  3880. }
  3881. return wpas_p2p_presence_req(wpa_s, dur1, int1, dur2, int2);
  3882. }
  3883. static int p2p_ctrl_ext_listen(struct wpa_supplicant *wpa_s, char *cmd)
  3884. {
  3885. char *pos;
  3886. unsigned int period = 0, interval = 0;
  3887. if (cmd[0]) {
  3888. pos = os_strchr(cmd, ' ');
  3889. if (pos == NULL)
  3890. return -1;
  3891. *pos++ = '\0';
  3892. period = atoi(cmd);
  3893. interval = atoi(pos);
  3894. }
  3895. return wpas_p2p_ext_listen(wpa_s, period, interval);
  3896. }
  3897. #endif /* CONFIG_P2P */
  3898. #ifdef CONFIG_INTERWORKING
  3899. static int ctrl_interworking_connect(struct wpa_supplicant *wpa_s, char *dst)
  3900. {
  3901. u8 bssid[ETH_ALEN];
  3902. struct wpa_bss *bss;
  3903. if (hwaddr_aton(dst, bssid)) {
  3904. wpa_printf(MSG_DEBUG, "Invalid BSSID '%s'", dst);
  3905. return -1;
  3906. }
  3907. bss = wpa_bss_get_bssid(wpa_s, bssid);
  3908. if (bss == NULL) {
  3909. wpa_printf(MSG_DEBUG, "Could not find BSS " MACSTR,
  3910. MAC2STR(bssid));
  3911. return -1;
  3912. }
  3913. return interworking_connect(wpa_s, bss);
  3914. }
  3915. static int get_anqp(struct wpa_supplicant *wpa_s, char *dst)
  3916. {
  3917. u8 dst_addr[ETH_ALEN];
  3918. int used;
  3919. char *pos;
  3920. #define MAX_ANQP_INFO_ID 100
  3921. u16 id[MAX_ANQP_INFO_ID];
  3922. size_t num_id = 0;
  3923. used = hwaddr_aton2(dst, dst_addr);
  3924. if (used < 0)
  3925. return -1;
  3926. pos = dst + used;
  3927. while (num_id < MAX_ANQP_INFO_ID) {
  3928. id[num_id] = atoi(pos);
  3929. if (id[num_id])
  3930. num_id++;
  3931. pos = os_strchr(pos + 1, ',');
  3932. if (pos == NULL)
  3933. break;
  3934. pos++;
  3935. }
  3936. if (num_id == 0)
  3937. return -1;
  3938. return anqp_send_req(wpa_s, dst_addr, id, num_id);
  3939. }
  3940. static int gas_request(struct wpa_supplicant *wpa_s, char *cmd)
  3941. {
  3942. u8 dst_addr[ETH_ALEN];
  3943. struct wpabuf *advproto, *query = NULL;
  3944. int used, ret = -1;
  3945. char *pos, *end;
  3946. size_t len;
  3947. used = hwaddr_aton2(cmd, dst_addr);
  3948. if (used < 0)
  3949. return -1;
  3950. pos = cmd + used;
  3951. while (*pos == ' ')
  3952. pos++;
  3953. /* Advertisement Protocol ID */
  3954. end = os_strchr(pos, ' ');
  3955. if (end)
  3956. len = end - pos;
  3957. else
  3958. len = os_strlen(pos);
  3959. if (len & 0x01)
  3960. return -1;
  3961. len /= 2;
  3962. if (len == 0)
  3963. return -1;
  3964. advproto = wpabuf_alloc(len);
  3965. if (advproto == NULL)
  3966. return -1;
  3967. if (hexstr2bin(pos, wpabuf_put(advproto, len), len) < 0)
  3968. goto fail;
  3969. if (end) {
  3970. /* Optional Query Request */
  3971. pos = end + 1;
  3972. while (*pos == ' ')
  3973. pos++;
  3974. len = os_strlen(pos);
  3975. if (len) {
  3976. if (len & 0x01)
  3977. goto fail;
  3978. len /= 2;
  3979. if (len == 0)
  3980. goto fail;
  3981. query = wpabuf_alloc(len);
  3982. if (query == NULL)
  3983. goto fail;
  3984. if (hexstr2bin(pos, wpabuf_put(query, len), len) < 0)
  3985. goto fail;
  3986. }
  3987. }
  3988. ret = gas_send_request(wpa_s, dst_addr, advproto, query);
  3989. fail:
  3990. wpabuf_free(advproto);
  3991. wpabuf_free(query);
  3992. return ret;
  3993. }
  3994. static int gas_response_get(struct wpa_supplicant *wpa_s, char *cmd, char *buf,
  3995. size_t buflen)
  3996. {
  3997. u8 addr[ETH_ALEN];
  3998. int dialog_token;
  3999. int used;
  4000. char *pos;
  4001. size_t resp_len, start, requested_len;
  4002. if (!wpa_s->last_gas_resp)
  4003. return -1;
  4004. used = hwaddr_aton2(cmd, addr);
  4005. if (used < 0)
  4006. return -1;
  4007. pos = cmd + used;
  4008. while (*pos == ' ')
  4009. pos++;
  4010. dialog_token = atoi(pos);
  4011. if (os_memcmp(addr, wpa_s->last_gas_addr, ETH_ALEN) != 0 ||
  4012. dialog_token != wpa_s->last_gas_dialog_token)
  4013. return -1;
  4014. resp_len = wpabuf_len(wpa_s->last_gas_resp);
  4015. start = 0;
  4016. requested_len = resp_len;
  4017. pos = os_strchr(pos, ' ');
  4018. if (pos) {
  4019. start = atoi(pos);
  4020. if (start > resp_len)
  4021. return os_snprintf(buf, buflen, "FAIL-Invalid range");
  4022. pos = os_strchr(pos, ',');
  4023. if (pos == NULL)
  4024. return -1;
  4025. pos++;
  4026. requested_len = atoi(pos);
  4027. if (start + requested_len > resp_len)
  4028. return os_snprintf(buf, buflen, "FAIL-Invalid range");
  4029. }
  4030. if (requested_len * 2 + 1 > buflen)
  4031. return os_snprintf(buf, buflen, "FAIL-Too long response");
  4032. return wpa_snprintf_hex(buf, buflen,
  4033. wpabuf_head_u8(wpa_s->last_gas_resp) + start,
  4034. requested_len);
  4035. }
  4036. #endif /* CONFIG_INTERWORKING */
  4037. #ifdef CONFIG_HS20
  4038. static int get_hs20_anqp(struct wpa_supplicant *wpa_s, char *dst)
  4039. {
  4040. u8 dst_addr[ETH_ALEN];
  4041. int used;
  4042. char *pos;
  4043. u32 subtypes = 0;
  4044. used = hwaddr_aton2(dst, dst_addr);
  4045. if (used < 0)
  4046. return -1;
  4047. pos = dst + used;
  4048. for (;;) {
  4049. int num = atoi(pos);
  4050. if (num <= 0 || num > 31)
  4051. return -1;
  4052. subtypes |= BIT(num);
  4053. pos = os_strchr(pos + 1, ',');
  4054. if (pos == NULL)
  4055. break;
  4056. pos++;
  4057. }
  4058. if (subtypes == 0)
  4059. return -1;
  4060. return hs20_anqp_send_req(wpa_s, dst_addr, subtypes, NULL, 0);
  4061. }
  4062. static int hs20_nai_home_realm_list(struct wpa_supplicant *wpa_s,
  4063. const u8 *addr, const char *realm)
  4064. {
  4065. u8 *buf;
  4066. size_t rlen, len;
  4067. int ret;
  4068. rlen = os_strlen(realm);
  4069. len = 3 + rlen;
  4070. buf = os_malloc(len);
  4071. if (buf == NULL)
  4072. return -1;
  4073. buf[0] = 1; /* NAI Home Realm Count */
  4074. buf[1] = 0; /* Formatted in accordance with RFC 4282 */
  4075. buf[2] = rlen;
  4076. os_memcpy(buf + 3, realm, rlen);
  4077. ret = hs20_anqp_send_req(wpa_s, addr,
  4078. BIT(HS20_STYPE_NAI_HOME_REALM_QUERY),
  4079. buf, len);
  4080. os_free(buf);
  4081. return ret;
  4082. }
  4083. static int hs20_get_nai_home_realm_list(struct wpa_supplicant *wpa_s,
  4084. char *dst)
  4085. {
  4086. struct wpa_cred *cred = wpa_s->conf->cred;
  4087. u8 dst_addr[ETH_ALEN];
  4088. int used;
  4089. u8 *buf;
  4090. size_t len;
  4091. int ret;
  4092. used = hwaddr_aton2(dst, dst_addr);
  4093. if (used < 0)
  4094. return -1;
  4095. while (dst[used] == ' ')
  4096. used++;
  4097. if (os_strncmp(dst + used, "realm=", 6) == 0)
  4098. return hs20_nai_home_realm_list(wpa_s, dst_addr,
  4099. dst + used + 6);
  4100. len = os_strlen(dst + used);
  4101. if (len == 0 && cred && cred->realm)
  4102. return hs20_nai_home_realm_list(wpa_s, dst_addr, cred->realm);
  4103. if (len % 1)
  4104. return -1;
  4105. len /= 2;
  4106. buf = os_malloc(len);
  4107. if (buf == NULL)
  4108. return -1;
  4109. if (hexstr2bin(dst + used, buf, len) < 0) {
  4110. os_free(buf);
  4111. return -1;
  4112. }
  4113. ret = hs20_anqp_send_req(wpa_s, dst_addr,
  4114. BIT(HS20_STYPE_NAI_HOME_REALM_QUERY),
  4115. buf, len);
  4116. os_free(buf);
  4117. return ret;
  4118. }
  4119. #endif /* CONFIG_HS20 */
  4120. static int wpa_supplicant_ctrl_iface_sta_autoconnect(
  4121. struct wpa_supplicant *wpa_s, char *cmd)
  4122. {
  4123. wpa_s->auto_reconnect_disabled = atoi(cmd) == 0 ? 1 : 0;
  4124. return 0;
  4125. }
  4126. #ifdef CONFIG_AUTOSCAN
  4127. static int wpa_supplicant_ctrl_iface_autoscan(struct wpa_supplicant *wpa_s,
  4128. char *cmd)
  4129. {
  4130. enum wpa_states state = wpa_s->wpa_state;
  4131. char *new_params = NULL;
  4132. if (os_strlen(cmd) > 0) {
  4133. new_params = os_strdup(cmd);
  4134. if (new_params == NULL)
  4135. return -1;
  4136. }
  4137. os_free(wpa_s->conf->autoscan);
  4138. wpa_s->conf->autoscan = new_params;
  4139. if (wpa_s->conf->autoscan == NULL)
  4140. autoscan_deinit(wpa_s);
  4141. else if (state == WPA_DISCONNECTED || state == WPA_INACTIVE)
  4142. autoscan_init(wpa_s, 1);
  4143. else if (state == WPA_SCANNING)
  4144. wpa_supplicant_reinit_autoscan(wpa_s);
  4145. return 0;
  4146. }
  4147. #endif /* CONFIG_AUTOSCAN */
  4148. #ifdef CONFIG_WNM
  4149. static int wpas_ctrl_iface_wnm_sleep(struct wpa_supplicant *wpa_s, char *cmd)
  4150. {
  4151. int enter;
  4152. int intval = 0;
  4153. char *pos;
  4154. int ret;
  4155. struct wpabuf *tfs_req = NULL;
  4156. if (os_strncmp(cmd, "enter", 5) == 0)
  4157. enter = 1;
  4158. else if (os_strncmp(cmd, "exit", 4) == 0)
  4159. enter = 0;
  4160. else
  4161. return -1;
  4162. pos = os_strstr(cmd, " interval=");
  4163. if (pos)
  4164. intval = atoi(pos + 10);
  4165. pos = os_strstr(cmd, " tfs_req=");
  4166. if (pos) {
  4167. char *end;
  4168. size_t len;
  4169. pos += 9;
  4170. end = os_strchr(pos, ' ');
  4171. if (end)
  4172. len = end - pos;
  4173. else
  4174. len = os_strlen(pos);
  4175. if (len & 1)
  4176. return -1;
  4177. len /= 2;
  4178. tfs_req = wpabuf_alloc(len);
  4179. if (tfs_req == NULL)
  4180. return -1;
  4181. if (hexstr2bin(pos, wpabuf_put(tfs_req, len), len) < 0) {
  4182. wpabuf_free(tfs_req);
  4183. return -1;
  4184. }
  4185. }
  4186. ret = ieee802_11_send_wnmsleep_req(wpa_s, enter ? WNM_SLEEP_MODE_ENTER :
  4187. WNM_SLEEP_MODE_EXIT, intval,
  4188. tfs_req);
  4189. wpabuf_free(tfs_req);
  4190. return ret;
  4191. }
  4192. #endif /* CONFIG_WNM */
  4193. static int wpa_supplicant_signal_poll(struct wpa_supplicant *wpa_s, char *buf,
  4194. size_t buflen)
  4195. {
  4196. struct wpa_signal_info si;
  4197. int ret;
  4198. ret = wpa_drv_signal_poll(wpa_s, &si);
  4199. if (ret)
  4200. return -1;
  4201. ret = os_snprintf(buf, buflen, "RSSI=%d\nLINKSPEED=%d\n"
  4202. "NOISE=%d\nFREQUENCY=%u\n",
  4203. si.current_signal, si.current_txrate / 1000,
  4204. si.current_noise, si.frequency);
  4205. if (ret < 0 || (unsigned int) ret > buflen)
  4206. return -1;
  4207. return ret;
  4208. }
  4209. static int wpa_supplicant_pktcnt_poll(struct wpa_supplicant *wpa_s, char *buf,
  4210. size_t buflen)
  4211. {
  4212. struct hostap_sta_driver_data sta;
  4213. int ret;
  4214. ret = wpa_drv_pktcnt_poll(wpa_s, &sta);
  4215. if (ret)
  4216. return -1;
  4217. ret = os_snprintf(buf, buflen, "TXGOOD=%lu\nTXBAD=%lu\nRXGOOD=%lu\n",
  4218. sta.tx_packets, sta.tx_retry_failed, sta.rx_packets);
  4219. if (ret < 0 || (size_t) ret > buflen)
  4220. return -1;
  4221. return ret;
  4222. }
  4223. static void wpa_supplicant_ctrl_iface_flush(struct wpa_supplicant *wpa_s)
  4224. {
  4225. wpa_dbg(wpa_s, MSG_DEBUG, "Flush all wpa_supplicant state");
  4226. #ifdef CONFIG_P2P
  4227. wpas_p2p_stop_find(wpa_s);
  4228. p2p_ctrl_flush(wpa_s);
  4229. wpas_p2p_group_remove(wpa_s, "*");
  4230. #endif /* CONFIG_P2P */
  4231. #ifdef CONFIG_WPS_TESTING
  4232. wps_version_number = 0x20;
  4233. wps_testing_dummy_cred = 0;
  4234. #endif /* CONFIG_WPS_TESTING */
  4235. #ifdef CONFIG_WPS
  4236. wpas_wps_cancel(wpa_s);
  4237. #endif /* CONFIG_WPS */
  4238. #ifdef CONFIG_TDLS_TESTING
  4239. extern unsigned int tdls_testing;
  4240. tdls_testing = 0;
  4241. #endif /* CONFIG_TDLS_TESTING */
  4242. #ifdef CONFIG_TDLS
  4243. wpa_drv_tdls_oper(wpa_s, TDLS_ENABLE, NULL);
  4244. wpa_tdls_enable(wpa_s->wpa, 1);
  4245. #endif /* CONFIG_TDLS */
  4246. wpa_s->no_keep_alive = 0;
  4247. os_free(wpa_s->disallow_aps_bssid);
  4248. wpa_s->disallow_aps_bssid = NULL;
  4249. wpa_s->disallow_aps_bssid_count = 0;
  4250. os_free(wpa_s->disallow_aps_ssid);
  4251. wpa_s->disallow_aps_ssid = NULL;
  4252. wpa_s->disallow_aps_ssid_count = 0;
  4253. wpa_s->set_sta_uapsd = 0;
  4254. wpa_s->sta_uapsd = 0;
  4255. wpa_drv_radio_disable(wpa_s, 0);
  4256. wpa_bss_flush(wpa_s);
  4257. wpa_blacklist_clear(wpa_s);
  4258. wpa_s->extra_blacklist_count = 0;
  4259. wpa_supplicant_ctrl_iface_remove_network(wpa_s, "all");
  4260. wpa_supplicant_ctrl_iface_remove_cred(wpa_s, "all");
  4261. }
  4262. char * wpa_supplicant_ctrl_iface_process(struct wpa_supplicant *wpa_s,
  4263. char *buf, size_t *resp_len)
  4264. {
  4265. char *reply;
  4266. const int reply_size = 4096;
  4267. int ctrl_rsp = 0;
  4268. int reply_len;
  4269. if (os_strncmp(buf, WPA_CTRL_RSP, os_strlen(WPA_CTRL_RSP)) == 0 ||
  4270. os_strncmp(buf, "SET_NETWORK ", 12) == 0 ||
  4271. os_strncmp(buf, "WPS_NFC_TAG_READ", 16) == 0 ||
  4272. os_strncmp(buf, "NFC_REPORT_HANDOVER", 19) == 0 ||
  4273. os_strncmp(buf, "NFC_RX_HANDOVER_SEL", 19) == 0) {
  4274. wpa_hexdump_ascii_key(MSG_DEBUG, "RX ctrl_iface",
  4275. (const u8 *) buf, os_strlen(buf));
  4276. } else {
  4277. int level = MSG_DEBUG;
  4278. if (os_strcmp(buf, "PING") == 0)
  4279. level = MSG_EXCESSIVE;
  4280. wpa_hexdump_ascii(level, "RX ctrl_iface",
  4281. (const u8 *) buf, os_strlen(buf));
  4282. wpa_dbg(wpa_s, level, "Control interface command '%s'", buf);
  4283. }
  4284. reply = os_malloc(reply_size);
  4285. if (reply == NULL) {
  4286. *resp_len = 1;
  4287. return NULL;
  4288. }
  4289. os_memcpy(reply, "OK\n", 3);
  4290. reply_len = 3;
  4291. if (os_strcmp(buf, "PING") == 0) {
  4292. os_memcpy(reply, "PONG\n", 5);
  4293. reply_len = 5;
  4294. } else if (os_strcmp(buf, "IFNAME") == 0) {
  4295. reply_len = os_strlen(wpa_s->ifname);
  4296. os_memcpy(reply, wpa_s->ifname, reply_len);
  4297. } else if (os_strncmp(buf, "RELOG", 5) == 0) {
  4298. if (wpa_debug_reopen_file() < 0)
  4299. reply_len = -1;
  4300. } else if (os_strncmp(buf, "NOTE ", 5) == 0) {
  4301. wpa_printf(MSG_INFO, "NOTE: %s", buf + 5);
  4302. } else if (os_strcmp(buf, "MIB") == 0) {
  4303. reply_len = wpa_sm_get_mib(wpa_s->wpa, reply, reply_size);
  4304. if (reply_len >= 0) {
  4305. int res;
  4306. res = eapol_sm_get_mib(wpa_s->eapol, reply + reply_len,
  4307. reply_size - reply_len);
  4308. if (res < 0)
  4309. reply_len = -1;
  4310. else
  4311. reply_len += res;
  4312. }
  4313. } else if (os_strncmp(buf, "STATUS", 6) == 0) {
  4314. reply_len = wpa_supplicant_ctrl_iface_status(
  4315. wpa_s, buf + 6, reply, reply_size);
  4316. } else if (os_strcmp(buf, "PMKSA") == 0) {
  4317. reply_len = wpa_sm_pmksa_cache_list(wpa_s->wpa, reply,
  4318. reply_size);
  4319. } else if (os_strncmp(buf, "SET ", 4) == 0) {
  4320. if (wpa_supplicant_ctrl_iface_set(wpa_s, buf + 4))
  4321. reply_len = -1;
  4322. } else if (os_strncmp(buf, "GET ", 4) == 0) {
  4323. reply_len = wpa_supplicant_ctrl_iface_get(wpa_s, buf + 4,
  4324. reply, reply_size);
  4325. } else if (os_strcmp(buf, "LOGON") == 0) {
  4326. eapol_sm_notify_logoff(wpa_s->eapol, FALSE);
  4327. } else if (os_strcmp(buf, "LOGOFF") == 0) {
  4328. eapol_sm_notify_logoff(wpa_s->eapol, TRUE);
  4329. } else if (os_strcmp(buf, "REASSOCIATE") == 0) {
  4330. if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED)
  4331. reply_len = -1;
  4332. else
  4333. wpas_request_connection(wpa_s);
  4334. } else if (os_strcmp(buf, "RECONNECT") == 0) {
  4335. if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED)
  4336. reply_len = -1;
  4337. else if (wpa_s->disconnected)
  4338. wpas_request_connection(wpa_s);
  4339. #ifdef IEEE8021X_EAPOL
  4340. } else if (os_strncmp(buf, "PREAUTH ", 8) == 0) {
  4341. if (wpa_supplicant_ctrl_iface_preauth(wpa_s, buf + 8))
  4342. reply_len = -1;
  4343. #endif /* IEEE8021X_EAPOL */
  4344. #ifdef CONFIG_PEERKEY
  4345. } else if (os_strncmp(buf, "STKSTART ", 9) == 0) {
  4346. if (wpa_supplicant_ctrl_iface_stkstart(wpa_s, buf + 9))
  4347. reply_len = -1;
  4348. #endif /* CONFIG_PEERKEY */
  4349. #ifdef CONFIG_IEEE80211R
  4350. } else if (os_strncmp(buf, "FT_DS ", 6) == 0) {
  4351. if (wpa_supplicant_ctrl_iface_ft_ds(wpa_s, buf + 6))
  4352. reply_len = -1;
  4353. #endif /* CONFIG_IEEE80211R */
  4354. #ifdef CONFIG_WPS
  4355. } else if (os_strcmp(buf, "WPS_PBC") == 0) {
  4356. int res = wpa_supplicant_ctrl_iface_wps_pbc(wpa_s, NULL);
  4357. if (res == -2) {
  4358. os_memcpy(reply, "FAIL-PBC-OVERLAP\n", 17);
  4359. reply_len = 17;
  4360. } else if (res)
  4361. reply_len = -1;
  4362. } else if (os_strncmp(buf, "WPS_PBC ", 8) == 0) {
  4363. int res = wpa_supplicant_ctrl_iface_wps_pbc(wpa_s, buf + 8);
  4364. if (res == -2) {
  4365. os_memcpy(reply, "FAIL-PBC-OVERLAP\n", 17);
  4366. reply_len = 17;
  4367. } else if (res)
  4368. reply_len = -1;
  4369. } else if (os_strncmp(buf, "WPS_PIN ", 8) == 0) {
  4370. reply_len = wpa_supplicant_ctrl_iface_wps_pin(wpa_s, buf + 8,
  4371. reply,
  4372. reply_size);
  4373. } else if (os_strncmp(buf, "WPS_CHECK_PIN ", 14) == 0) {
  4374. reply_len = wpa_supplicant_ctrl_iface_wps_check_pin(
  4375. wpa_s, buf + 14, reply, reply_size);
  4376. } else if (os_strcmp(buf, "WPS_CANCEL") == 0) {
  4377. if (wpas_wps_cancel(wpa_s))
  4378. reply_len = -1;
  4379. #ifdef CONFIG_WPS_NFC
  4380. } else if (os_strcmp(buf, "WPS_NFC") == 0) {
  4381. if (wpa_supplicant_ctrl_iface_wps_nfc(wpa_s, NULL))
  4382. reply_len = -1;
  4383. } else if (os_strncmp(buf, "WPS_NFC ", 8) == 0) {
  4384. if (wpa_supplicant_ctrl_iface_wps_nfc(wpa_s, buf + 8))
  4385. reply_len = -1;
  4386. } else if (os_strncmp(buf, "WPS_NFC_CONFIG_TOKEN ", 21) == 0) {
  4387. reply_len = wpa_supplicant_ctrl_iface_wps_nfc_config_token(
  4388. wpa_s, buf + 21, reply, reply_size);
  4389. } else if (os_strncmp(buf, "WPS_NFC_TOKEN ", 14) == 0) {
  4390. reply_len = wpa_supplicant_ctrl_iface_wps_nfc_token(
  4391. wpa_s, buf + 14, reply, reply_size);
  4392. } else if (os_strncmp(buf, "WPS_NFC_TAG_READ ", 17) == 0) {
  4393. if (wpa_supplicant_ctrl_iface_wps_nfc_tag_read(wpa_s,
  4394. buf + 17))
  4395. reply_len = -1;
  4396. } else if (os_strncmp(buf, "NFC_GET_HANDOVER_REQ ", 21) == 0) {
  4397. reply_len = wpas_ctrl_nfc_get_handover_req(
  4398. wpa_s, buf + 21, reply, reply_size);
  4399. } else if (os_strncmp(buf, "NFC_GET_HANDOVER_SEL ", 21) == 0) {
  4400. reply_len = wpas_ctrl_nfc_get_handover_sel(
  4401. wpa_s, buf + 21, reply, reply_size);
  4402. } else if (os_strncmp(buf, "NFC_RX_HANDOVER_REQ ", 20) == 0) {
  4403. reply_len = wpas_ctrl_nfc_rx_handover_req(
  4404. wpa_s, buf + 20, reply, reply_size);
  4405. } else if (os_strncmp(buf, "NFC_RX_HANDOVER_SEL ", 20) == 0) {
  4406. if (wpas_ctrl_nfc_rx_handover_sel(wpa_s, buf + 20))
  4407. reply_len = -1;
  4408. } else if (os_strncmp(buf, "NFC_REPORT_HANDOVER ", 20) == 0) {
  4409. if (wpas_ctrl_nfc_report_handover(wpa_s, buf + 20))
  4410. reply_len = -1;
  4411. #endif /* CONFIG_WPS_NFC */
  4412. } else if (os_strncmp(buf, "WPS_REG ", 8) == 0) {
  4413. if (wpa_supplicant_ctrl_iface_wps_reg(wpa_s, buf + 8))
  4414. reply_len = -1;
  4415. #ifdef CONFIG_AP
  4416. } else if (os_strncmp(buf, "WPS_AP_PIN ", 11) == 0) {
  4417. reply_len = wpa_supplicant_ctrl_iface_wps_ap_pin(
  4418. wpa_s, buf + 11, reply, reply_size);
  4419. #endif /* CONFIG_AP */
  4420. #ifdef CONFIG_WPS_ER
  4421. } else if (os_strcmp(buf, "WPS_ER_START") == 0) {
  4422. if (wpas_wps_er_start(wpa_s, NULL))
  4423. reply_len = -1;
  4424. } else if (os_strncmp(buf, "WPS_ER_START ", 13) == 0) {
  4425. if (wpas_wps_er_start(wpa_s, buf + 13))
  4426. reply_len = -1;
  4427. } else if (os_strcmp(buf, "WPS_ER_STOP") == 0) {
  4428. if (wpas_wps_er_stop(wpa_s))
  4429. reply_len = -1;
  4430. } else if (os_strncmp(buf, "WPS_ER_PIN ", 11) == 0) {
  4431. if (wpa_supplicant_ctrl_iface_wps_er_pin(wpa_s, buf + 11))
  4432. reply_len = -1;
  4433. } else if (os_strncmp(buf, "WPS_ER_PBC ", 11) == 0) {
  4434. int ret = wpas_wps_er_pbc(wpa_s, buf + 11);
  4435. if (ret == -2) {
  4436. os_memcpy(reply, "FAIL-PBC-OVERLAP\n", 17);
  4437. reply_len = 17;
  4438. } else if (ret == -3) {
  4439. os_memcpy(reply, "FAIL-UNKNOWN-UUID\n", 18);
  4440. reply_len = 18;
  4441. } else if (ret == -4) {
  4442. os_memcpy(reply, "FAIL-NO-AP-SETTINGS\n", 20);
  4443. reply_len = 20;
  4444. } else if (ret)
  4445. reply_len = -1;
  4446. } else if (os_strncmp(buf, "WPS_ER_LEARN ", 13) == 0) {
  4447. if (wpa_supplicant_ctrl_iface_wps_er_learn(wpa_s, buf + 13))
  4448. reply_len = -1;
  4449. } else if (os_strncmp(buf, "WPS_ER_SET_CONFIG ", 18) == 0) {
  4450. if (wpa_supplicant_ctrl_iface_wps_er_set_config(wpa_s,
  4451. buf + 18))
  4452. reply_len = -1;
  4453. } else if (os_strncmp(buf, "WPS_ER_CONFIG ", 14) == 0) {
  4454. if (wpa_supplicant_ctrl_iface_wps_er_config(wpa_s, buf + 14))
  4455. reply_len = -1;
  4456. #ifdef CONFIG_WPS_NFC
  4457. } else if (os_strncmp(buf, "WPS_ER_NFC_CONFIG_TOKEN ", 24) == 0) {
  4458. reply_len = wpa_supplicant_ctrl_iface_wps_er_nfc_config_token(
  4459. wpa_s, buf + 24, reply, reply_size);
  4460. #endif /* CONFIG_WPS_NFC */
  4461. #endif /* CONFIG_WPS_ER */
  4462. #endif /* CONFIG_WPS */
  4463. #ifdef CONFIG_IBSS_RSN
  4464. } else if (os_strncmp(buf, "IBSS_RSN ", 9) == 0) {
  4465. if (wpa_supplicant_ctrl_iface_ibss_rsn(wpa_s, buf + 9))
  4466. reply_len = -1;
  4467. #endif /* CONFIG_IBSS_RSN */
  4468. #ifdef CONFIG_P2P
  4469. } else if (os_strncmp(buf, "P2P_FIND ", 9) == 0) {
  4470. if (p2p_ctrl_find(wpa_s, buf + 9))
  4471. reply_len = -1;
  4472. } else if (os_strcmp(buf, "P2P_FIND") == 0) {
  4473. if (p2p_ctrl_find(wpa_s, ""))
  4474. reply_len = -1;
  4475. } else if (os_strcmp(buf, "P2P_STOP_FIND") == 0) {
  4476. wpas_p2p_stop_find(wpa_s);
  4477. } else if (os_strncmp(buf, "P2P_CONNECT ", 12) == 0) {
  4478. reply_len = p2p_ctrl_connect(wpa_s, buf + 12, reply,
  4479. reply_size);
  4480. } else if (os_strncmp(buf, "P2P_LISTEN ", 11) == 0) {
  4481. if (p2p_ctrl_listen(wpa_s, buf + 11))
  4482. reply_len = -1;
  4483. } else if (os_strcmp(buf, "P2P_LISTEN") == 0) {
  4484. if (p2p_ctrl_listen(wpa_s, ""))
  4485. reply_len = -1;
  4486. } else if (os_strncmp(buf, "P2P_GROUP_REMOVE ", 17) == 0) {
  4487. if (wpas_p2p_group_remove(wpa_s, buf + 17))
  4488. reply_len = -1;
  4489. } else if (os_strcmp(buf, "P2P_GROUP_ADD") == 0) {
  4490. if (wpas_p2p_group_add(wpa_s, 0, 0, 0))
  4491. reply_len = -1;
  4492. } else if (os_strncmp(buf, "P2P_GROUP_ADD ", 14) == 0) {
  4493. if (p2p_ctrl_group_add(wpa_s, buf + 14))
  4494. reply_len = -1;
  4495. } else if (os_strncmp(buf, "P2P_PROV_DISC ", 14) == 0) {
  4496. if (p2p_ctrl_prov_disc(wpa_s, buf + 14))
  4497. reply_len = -1;
  4498. } else if (os_strcmp(buf, "P2P_GET_PASSPHRASE") == 0) {
  4499. reply_len = p2p_get_passphrase(wpa_s, reply, reply_size);
  4500. } else if (os_strncmp(buf, "P2P_SERV_DISC_REQ ", 18) == 0) {
  4501. reply_len = p2p_ctrl_serv_disc_req(wpa_s, buf + 18, reply,
  4502. reply_size);
  4503. } else if (os_strncmp(buf, "P2P_SERV_DISC_CANCEL_REQ ", 25) == 0) {
  4504. if (p2p_ctrl_serv_disc_cancel_req(wpa_s, buf + 25) < 0)
  4505. reply_len = -1;
  4506. } else if (os_strncmp(buf, "P2P_SERV_DISC_RESP ", 19) == 0) {
  4507. if (p2p_ctrl_serv_disc_resp(wpa_s, buf + 19) < 0)
  4508. reply_len = -1;
  4509. } else if (os_strcmp(buf, "P2P_SERVICE_UPDATE") == 0) {
  4510. wpas_p2p_sd_service_update(wpa_s);
  4511. } else if (os_strncmp(buf, "P2P_SERV_DISC_EXTERNAL ", 23) == 0) {
  4512. if (p2p_ctrl_serv_disc_external(wpa_s, buf + 23) < 0)
  4513. reply_len = -1;
  4514. } else if (os_strcmp(buf, "P2P_SERVICE_FLUSH") == 0) {
  4515. wpas_p2p_service_flush(wpa_s);
  4516. } else if (os_strncmp(buf, "P2P_SERVICE_ADD ", 16) == 0) {
  4517. if (p2p_ctrl_service_add(wpa_s, buf + 16) < 0)
  4518. reply_len = -1;
  4519. } else if (os_strncmp(buf, "P2P_SERVICE_DEL ", 16) == 0) {
  4520. if (p2p_ctrl_service_del(wpa_s, buf + 16) < 0)
  4521. reply_len = -1;
  4522. } else if (os_strncmp(buf, "P2P_REJECT ", 11) == 0) {
  4523. if (p2p_ctrl_reject(wpa_s, buf + 11) < 0)
  4524. reply_len = -1;
  4525. } else if (os_strncmp(buf, "P2P_INVITE ", 11) == 0) {
  4526. if (p2p_ctrl_invite(wpa_s, buf + 11) < 0)
  4527. reply_len = -1;
  4528. } else if (os_strncmp(buf, "P2P_PEER ", 9) == 0) {
  4529. reply_len = p2p_ctrl_peer(wpa_s, buf + 9, reply,
  4530. reply_size);
  4531. } else if (os_strncmp(buf, "P2P_SET ", 8) == 0) {
  4532. if (p2p_ctrl_set(wpa_s, buf + 8) < 0)
  4533. reply_len = -1;
  4534. } else if (os_strcmp(buf, "P2P_FLUSH") == 0) {
  4535. p2p_ctrl_flush(wpa_s);
  4536. } else if (os_strncmp(buf, "P2P_UNAUTHORIZE ", 16) == 0) {
  4537. if (wpas_p2p_unauthorize(wpa_s, buf + 16) < 0)
  4538. reply_len = -1;
  4539. } else if (os_strcmp(buf, "P2P_CANCEL") == 0) {
  4540. if (wpas_p2p_cancel(wpa_s))
  4541. reply_len = -1;
  4542. } else if (os_strncmp(buf, "P2P_PRESENCE_REQ ", 17) == 0) {
  4543. if (p2p_ctrl_presence_req(wpa_s, buf + 17) < 0)
  4544. reply_len = -1;
  4545. } else if (os_strcmp(buf, "P2P_PRESENCE_REQ") == 0) {
  4546. if (p2p_ctrl_presence_req(wpa_s, "") < 0)
  4547. reply_len = -1;
  4548. } else if (os_strncmp(buf, "P2P_EXT_LISTEN ", 15) == 0) {
  4549. if (p2p_ctrl_ext_listen(wpa_s, buf + 15) < 0)
  4550. reply_len = -1;
  4551. } else if (os_strcmp(buf, "P2P_EXT_LISTEN") == 0) {
  4552. if (p2p_ctrl_ext_listen(wpa_s, "") < 0)
  4553. reply_len = -1;
  4554. #endif /* CONFIG_P2P */
  4555. #ifdef CONFIG_WIFI_DISPLAY
  4556. } else if (os_strncmp(buf, "WFD_SUBELEM_SET ", 16) == 0) {
  4557. if (wifi_display_subelem_set(wpa_s->global, buf + 16) < 0)
  4558. reply_len = -1;
  4559. } else if (os_strncmp(buf, "WFD_SUBELEM_GET ", 16) == 0) {
  4560. reply_len = wifi_display_subelem_get(wpa_s->global, buf + 16,
  4561. reply, reply_size);
  4562. #endif /* CONFIG_WIFI_DISPLAY */
  4563. #ifdef CONFIG_INTERWORKING
  4564. } else if (os_strcmp(buf, "FETCH_ANQP") == 0) {
  4565. if (interworking_fetch_anqp(wpa_s) < 0)
  4566. reply_len = -1;
  4567. } else if (os_strcmp(buf, "STOP_FETCH_ANQP") == 0) {
  4568. interworking_stop_fetch_anqp(wpa_s);
  4569. } else if (os_strncmp(buf, "INTERWORKING_SELECT", 19) == 0) {
  4570. if (interworking_select(wpa_s, os_strstr(buf + 19, "auto") !=
  4571. NULL) < 0)
  4572. reply_len = -1;
  4573. } else if (os_strncmp(buf, "INTERWORKING_CONNECT ", 21) == 0) {
  4574. if (ctrl_interworking_connect(wpa_s, buf + 21) < 0)
  4575. reply_len = -1;
  4576. } else if (os_strncmp(buf, "ANQP_GET ", 9) == 0) {
  4577. if (get_anqp(wpa_s, buf + 9) < 0)
  4578. reply_len = -1;
  4579. } else if (os_strncmp(buf, "GAS_REQUEST ", 12) == 0) {
  4580. if (gas_request(wpa_s, buf + 12) < 0)
  4581. reply_len = -1;
  4582. } else if (os_strncmp(buf, "GAS_RESPONSE_GET ", 17) == 0) {
  4583. reply_len = gas_response_get(wpa_s, buf + 17, reply,
  4584. reply_size);
  4585. #endif /* CONFIG_INTERWORKING */
  4586. #ifdef CONFIG_HS20
  4587. } else if (os_strncmp(buf, "HS20_ANQP_GET ", 14) == 0) {
  4588. if (get_hs20_anqp(wpa_s, buf + 14) < 0)
  4589. reply_len = -1;
  4590. } else if (os_strncmp(buf, "HS20_GET_NAI_HOME_REALM_LIST ", 29) == 0) {
  4591. if (hs20_get_nai_home_realm_list(wpa_s, buf + 29) < 0)
  4592. reply_len = -1;
  4593. #endif /* CONFIG_HS20 */
  4594. } else if (os_strncmp(buf, WPA_CTRL_RSP, os_strlen(WPA_CTRL_RSP)) == 0)
  4595. {
  4596. if (wpa_supplicant_ctrl_iface_ctrl_rsp(
  4597. wpa_s, buf + os_strlen(WPA_CTRL_RSP)))
  4598. reply_len = -1;
  4599. else
  4600. ctrl_rsp = 1;
  4601. } else if (os_strcmp(buf, "RECONFIGURE") == 0) {
  4602. if (wpa_supplicant_reload_configuration(wpa_s))
  4603. reply_len = -1;
  4604. } else if (os_strcmp(buf, "TERMINATE") == 0) {
  4605. wpa_supplicant_terminate_proc(wpa_s->global);
  4606. } else if (os_strncmp(buf, "BSSID ", 6) == 0) {
  4607. if (wpa_supplicant_ctrl_iface_bssid(wpa_s, buf + 6))
  4608. reply_len = -1;
  4609. } else if (os_strncmp(buf, "BLACKLIST", 9) == 0) {
  4610. reply_len = wpa_supplicant_ctrl_iface_blacklist(
  4611. wpa_s, buf + 9, reply, reply_size);
  4612. } else if (os_strncmp(buf, "LOG_LEVEL", 9) == 0) {
  4613. reply_len = wpa_supplicant_ctrl_iface_log_level(
  4614. wpa_s, buf + 9, reply, reply_size);
  4615. } else if (os_strcmp(buf, "LIST_NETWORKS") == 0) {
  4616. reply_len = wpa_supplicant_ctrl_iface_list_networks(
  4617. wpa_s, reply, reply_size);
  4618. } else if (os_strcmp(buf, "DISCONNECT") == 0) {
  4619. #ifdef CONFIG_SME
  4620. wpa_s->sme.prev_bssid_set = 0;
  4621. #endif /* CONFIG_SME */
  4622. wpa_s->reassociate = 0;
  4623. wpa_s->disconnected = 1;
  4624. wpa_supplicant_cancel_sched_scan(wpa_s);
  4625. wpa_supplicant_cancel_scan(wpa_s);
  4626. wpa_supplicant_deauthenticate(wpa_s,
  4627. WLAN_REASON_DEAUTH_LEAVING);
  4628. } else if (os_strcmp(buf, "SCAN") == 0 ||
  4629. os_strncmp(buf, "SCAN ", 5) == 0) {
  4630. if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED)
  4631. reply_len = -1;
  4632. else {
  4633. if (os_strlen(buf) > 4 &&
  4634. os_strncasecmp(buf + 5, "TYPE=ONLY", 9) == 0)
  4635. wpa_s->scan_res_handler = scan_only_handler;
  4636. if (!wpa_s->sched_scanning && !wpa_s->scanning &&
  4637. ((wpa_s->wpa_state <= WPA_SCANNING) ||
  4638. (wpa_s->wpa_state == WPA_COMPLETED))) {
  4639. wpa_s->normal_scans = 0;
  4640. wpa_s->scan_req = MANUAL_SCAN_REQ;
  4641. wpa_supplicant_req_scan(wpa_s, 0, 0);
  4642. } else if (wpa_s->sched_scanning) {
  4643. wpa_printf(MSG_DEBUG, "Stop ongoing "
  4644. "sched_scan to allow requested "
  4645. "full scan to proceed");
  4646. wpa_supplicant_cancel_sched_scan(wpa_s);
  4647. wpa_s->scan_req = MANUAL_SCAN_REQ;
  4648. wpa_supplicant_req_scan(wpa_s, 0, 0);
  4649. } else {
  4650. wpa_printf(MSG_DEBUG, "Ongoing scan action - "
  4651. "reject new request");
  4652. reply_len = os_snprintf(reply, reply_size,
  4653. "FAIL-BUSY\n");
  4654. }
  4655. }
  4656. } else if (os_strcmp(buf, "SCAN_RESULTS") == 0) {
  4657. reply_len = wpa_supplicant_ctrl_iface_scan_results(
  4658. wpa_s, reply, reply_size);
  4659. } else if (os_strncmp(buf, "SELECT_NETWORK ", 15) == 0) {
  4660. if (wpa_supplicant_ctrl_iface_select_network(wpa_s, buf + 15))
  4661. reply_len = -1;
  4662. } else if (os_strncmp(buf, "ENABLE_NETWORK ", 15) == 0) {
  4663. if (wpa_supplicant_ctrl_iface_enable_network(wpa_s, buf + 15))
  4664. reply_len = -1;
  4665. } else if (os_strncmp(buf, "DISABLE_NETWORK ", 16) == 0) {
  4666. if (wpa_supplicant_ctrl_iface_disable_network(wpa_s, buf + 16))
  4667. reply_len = -1;
  4668. } else if (os_strcmp(buf, "ADD_NETWORK") == 0) {
  4669. reply_len = wpa_supplicant_ctrl_iface_add_network(
  4670. wpa_s, reply, reply_size);
  4671. } else if (os_strncmp(buf, "REMOVE_NETWORK ", 15) == 0) {
  4672. if (wpa_supplicant_ctrl_iface_remove_network(wpa_s, buf + 15))
  4673. reply_len = -1;
  4674. } else if (os_strncmp(buf, "SET_NETWORK ", 12) == 0) {
  4675. if (wpa_supplicant_ctrl_iface_set_network(wpa_s, buf + 12))
  4676. reply_len = -1;
  4677. } else if (os_strncmp(buf, "GET_NETWORK ", 12) == 0) {
  4678. reply_len = wpa_supplicant_ctrl_iface_get_network(
  4679. wpa_s, buf + 12, reply, reply_size);
  4680. } else if (os_strcmp(buf, "LIST_CREDS") == 0) {
  4681. reply_len = wpa_supplicant_ctrl_iface_list_creds(
  4682. wpa_s, reply, reply_size);
  4683. } else if (os_strcmp(buf, "ADD_CRED") == 0) {
  4684. reply_len = wpa_supplicant_ctrl_iface_add_cred(
  4685. wpa_s, reply, reply_size);
  4686. } else if (os_strncmp(buf, "REMOVE_CRED ", 12) == 0) {
  4687. if (wpa_supplicant_ctrl_iface_remove_cred(wpa_s, buf + 12))
  4688. reply_len = -1;
  4689. } else if (os_strncmp(buf, "SET_CRED ", 9) == 0) {
  4690. if (wpa_supplicant_ctrl_iface_set_cred(wpa_s, buf + 9))
  4691. reply_len = -1;
  4692. #ifndef CONFIG_NO_CONFIG_WRITE
  4693. } else if (os_strcmp(buf, "SAVE_CONFIG") == 0) {
  4694. if (wpa_supplicant_ctrl_iface_save_config(wpa_s))
  4695. reply_len = -1;
  4696. #endif /* CONFIG_NO_CONFIG_WRITE */
  4697. } else if (os_strncmp(buf, "GET_CAPABILITY ", 15) == 0) {
  4698. reply_len = wpa_supplicant_ctrl_iface_get_capability(
  4699. wpa_s, buf + 15, reply, reply_size);
  4700. } else if (os_strncmp(buf, "AP_SCAN ", 8) == 0) {
  4701. if (wpa_supplicant_ctrl_iface_ap_scan(wpa_s, buf + 8))
  4702. reply_len = -1;
  4703. } else if (os_strncmp(buf, "SCAN_INTERVAL ", 14) == 0) {
  4704. if (wpa_supplicant_ctrl_iface_scan_interval(wpa_s, buf + 14))
  4705. reply_len = -1;
  4706. } else if (os_strcmp(buf, "INTERFACE_LIST") == 0) {
  4707. reply_len = wpa_supplicant_global_iface_list(
  4708. wpa_s->global, reply, reply_size);
  4709. } else if (os_strcmp(buf, "INTERFACES") == 0) {
  4710. reply_len = wpa_supplicant_global_iface_interfaces(
  4711. wpa_s->global, reply, reply_size);
  4712. } else if (os_strncmp(buf, "BSS ", 4) == 0) {
  4713. reply_len = wpa_supplicant_ctrl_iface_bss(
  4714. wpa_s, buf + 4, reply, reply_size);
  4715. #ifdef CONFIG_AP
  4716. } else if (os_strcmp(buf, "STA-FIRST") == 0) {
  4717. reply_len = ap_ctrl_iface_sta_first(wpa_s, reply, reply_size);
  4718. } else if (os_strncmp(buf, "STA ", 4) == 0) {
  4719. reply_len = ap_ctrl_iface_sta(wpa_s, buf + 4, reply,
  4720. reply_size);
  4721. } else if (os_strncmp(buf, "STA-NEXT ", 9) == 0) {
  4722. reply_len = ap_ctrl_iface_sta_next(wpa_s, buf + 9, reply,
  4723. reply_size);
  4724. } else if (os_strncmp(buf, "DEAUTHENTICATE ", 15) == 0) {
  4725. if (ap_ctrl_iface_sta_deauthenticate(wpa_s, buf + 15))
  4726. reply_len = -1;
  4727. } else if (os_strncmp(buf, "DISASSOCIATE ", 13) == 0) {
  4728. if (ap_ctrl_iface_sta_disassociate(wpa_s, buf + 13))
  4729. reply_len = -1;
  4730. #endif /* CONFIG_AP */
  4731. } else if (os_strcmp(buf, "SUSPEND") == 0) {
  4732. wpas_notify_suspend(wpa_s->global);
  4733. } else if (os_strcmp(buf, "RESUME") == 0) {
  4734. wpas_notify_resume(wpa_s->global);
  4735. } else if (os_strcmp(buf, "DROP_SA") == 0) {
  4736. wpa_supplicant_ctrl_iface_drop_sa(wpa_s);
  4737. } else if (os_strncmp(buf, "ROAM ", 5) == 0) {
  4738. if (wpa_supplicant_ctrl_iface_roam(wpa_s, buf + 5))
  4739. reply_len = -1;
  4740. } else if (os_strncmp(buf, "STA_AUTOCONNECT ", 16) == 0) {
  4741. if (wpa_supplicant_ctrl_iface_sta_autoconnect(wpa_s, buf + 16))
  4742. reply_len = -1;
  4743. } else if (os_strncmp(buf, "BSS_EXPIRE_AGE ", 15) == 0) {
  4744. if (wpa_supplicant_ctrl_iface_bss_expire_age(wpa_s, buf + 15))
  4745. reply_len = -1;
  4746. } else if (os_strncmp(buf, "BSS_EXPIRE_COUNT ", 17) == 0) {
  4747. if (wpa_supplicant_ctrl_iface_bss_expire_count(wpa_s,
  4748. buf + 17))
  4749. reply_len = -1;
  4750. } else if (os_strncmp(buf, "BSS_FLUSH ", 10) == 0) {
  4751. if (wpa_supplicant_ctrl_iface_bss_flush(wpa_s, buf + 10))
  4752. reply_len = -1;
  4753. #ifdef CONFIG_TDLS
  4754. } else if (os_strncmp(buf, "TDLS_DISCOVER ", 14) == 0) {
  4755. if (wpa_supplicant_ctrl_iface_tdls_discover(wpa_s, buf + 14))
  4756. reply_len = -1;
  4757. } else if (os_strncmp(buf, "TDLS_SETUP ", 11) == 0) {
  4758. if (wpa_supplicant_ctrl_iface_tdls_setup(wpa_s, buf + 11))
  4759. reply_len = -1;
  4760. } else if (os_strncmp(buf, "TDLS_TEARDOWN ", 14) == 0) {
  4761. if (wpa_supplicant_ctrl_iface_tdls_teardown(wpa_s, buf + 14))
  4762. reply_len = -1;
  4763. #endif /* CONFIG_TDLS */
  4764. } else if (os_strncmp(buf, "SIGNAL_POLL", 11) == 0) {
  4765. reply_len = wpa_supplicant_signal_poll(wpa_s, reply,
  4766. reply_size);
  4767. } else if (os_strncmp(buf, "PKTCNT_POLL", 11) == 0) {
  4768. reply_len = wpa_supplicant_pktcnt_poll(wpa_s, reply,
  4769. reply_size);
  4770. #ifdef CONFIG_AUTOSCAN
  4771. } else if (os_strncmp(buf, "AUTOSCAN ", 9) == 0) {
  4772. if (wpa_supplicant_ctrl_iface_autoscan(wpa_s, buf + 9))
  4773. reply_len = -1;
  4774. #endif /* CONFIG_AUTOSCAN */
  4775. } else if (os_strcmp(buf, "REAUTHENTICATE") == 0) {
  4776. pmksa_cache_clear_current(wpa_s->wpa);
  4777. eapol_sm_request_reauth(wpa_s->eapol);
  4778. #ifdef CONFIG_WNM
  4779. } else if (os_strncmp(buf, "WNM_SLEEP ", 10) == 0) {
  4780. if (wpas_ctrl_iface_wnm_sleep(wpa_s, buf + 10))
  4781. reply_len = -1;
  4782. #endif /* CONFIG_WNM */
  4783. } else if (os_strcmp(buf, "FLUSH") == 0) {
  4784. wpa_supplicant_ctrl_iface_flush(wpa_s);
  4785. } else {
  4786. os_memcpy(reply, "UNKNOWN COMMAND\n", 16);
  4787. reply_len = 16;
  4788. }
  4789. if (reply_len < 0) {
  4790. os_memcpy(reply, "FAIL\n", 5);
  4791. reply_len = 5;
  4792. }
  4793. if (ctrl_rsp)
  4794. eapol_sm_notify_ctrl_response(wpa_s->eapol);
  4795. *resp_len = reply_len;
  4796. return reply;
  4797. }
  4798. static int wpa_supplicant_global_iface_add(struct wpa_global *global,
  4799. char *cmd)
  4800. {
  4801. struct wpa_interface iface;
  4802. char *pos;
  4803. /*
  4804. * <ifname>TAB<confname>TAB<driver>TAB<ctrl_interface>TAB<driver_param>
  4805. * TAB<bridge_ifname>
  4806. */
  4807. wpa_printf(MSG_DEBUG, "CTRL_IFACE GLOBAL INTERFACE_ADD '%s'", cmd);
  4808. os_memset(&iface, 0, sizeof(iface));
  4809. do {
  4810. iface.ifname = pos = cmd;
  4811. pos = os_strchr(pos, '\t');
  4812. if (pos)
  4813. *pos++ = '\0';
  4814. if (iface.ifname[0] == '\0')
  4815. return -1;
  4816. if (pos == NULL)
  4817. break;
  4818. iface.confname = pos;
  4819. pos = os_strchr(pos, '\t');
  4820. if (pos)
  4821. *pos++ = '\0';
  4822. if (iface.confname[0] == '\0')
  4823. iface.confname = NULL;
  4824. if (pos == NULL)
  4825. break;
  4826. iface.driver = pos;
  4827. pos = os_strchr(pos, '\t');
  4828. if (pos)
  4829. *pos++ = '\0';
  4830. if (iface.driver[0] == '\0')
  4831. iface.driver = NULL;
  4832. if (pos == NULL)
  4833. break;
  4834. iface.ctrl_interface = pos;
  4835. pos = os_strchr(pos, '\t');
  4836. if (pos)
  4837. *pos++ = '\0';
  4838. if (iface.ctrl_interface[0] == '\0')
  4839. iface.ctrl_interface = NULL;
  4840. if (pos == NULL)
  4841. break;
  4842. iface.driver_param = pos;
  4843. pos = os_strchr(pos, '\t');
  4844. if (pos)
  4845. *pos++ = '\0';
  4846. if (iface.driver_param[0] == '\0')
  4847. iface.driver_param = NULL;
  4848. if (pos == NULL)
  4849. break;
  4850. iface.bridge_ifname = pos;
  4851. pos = os_strchr(pos, '\t');
  4852. if (pos)
  4853. *pos++ = '\0';
  4854. if (iface.bridge_ifname[0] == '\0')
  4855. iface.bridge_ifname = NULL;
  4856. if (pos == NULL)
  4857. break;
  4858. } while (0);
  4859. if (wpa_supplicant_get_iface(global, iface.ifname))
  4860. return -1;
  4861. return wpa_supplicant_add_iface(global, &iface) ? 0 : -1;
  4862. }
  4863. static int wpa_supplicant_global_iface_remove(struct wpa_global *global,
  4864. char *cmd)
  4865. {
  4866. struct wpa_supplicant *wpa_s;
  4867. wpa_printf(MSG_DEBUG, "CTRL_IFACE GLOBAL INTERFACE_REMOVE '%s'", cmd);
  4868. wpa_s = wpa_supplicant_get_iface(global, cmd);
  4869. if (wpa_s == NULL)
  4870. return -1;
  4871. return wpa_supplicant_remove_iface(global, wpa_s, 0);
  4872. }
  4873. static void wpa_free_iface_info(struct wpa_interface_info *iface)
  4874. {
  4875. struct wpa_interface_info *prev;
  4876. while (iface) {
  4877. prev = iface;
  4878. iface = iface->next;
  4879. os_free(prev->ifname);
  4880. os_free(prev->desc);
  4881. os_free(prev);
  4882. }
  4883. }
  4884. static int wpa_supplicant_global_iface_list(struct wpa_global *global,
  4885. char *buf, int len)
  4886. {
  4887. int i, res;
  4888. struct wpa_interface_info *iface = NULL, *last = NULL, *tmp;
  4889. char *pos, *end;
  4890. for (i = 0; wpa_drivers[i]; i++) {
  4891. struct wpa_driver_ops *drv = wpa_drivers[i];
  4892. if (drv->get_interfaces == NULL)
  4893. continue;
  4894. tmp = drv->get_interfaces(global->drv_priv[i]);
  4895. if (tmp == NULL)
  4896. continue;
  4897. if (last == NULL)
  4898. iface = last = tmp;
  4899. else
  4900. last->next = tmp;
  4901. while (last->next)
  4902. last = last->next;
  4903. }
  4904. pos = buf;
  4905. end = buf + len;
  4906. for (tmp = iface; tmp; tmp = tmp->next) {
  4907. res = os_snprintf(pos, end - pos, "%s\t%s\t%s\n",
  4908. tmp->drv_name, tmp->ifname,
  4909. tmp->desc ? tmp->desc : "");
  4910. if (res < 0 || res >= end - pos) {
  4911. *pos = '\0';
  4912. break;
  4913. }
  4914. pos += res;
  4915. }
  4916. wpa_free_iface_info(iface);
  4917. return pos - buf;
  4918. }
  4919. static int wpa_supplicant_global_iface_interfaces(struct wpa_global *global,
  4920. char *buf, int len)
  4921. {
  4922. int res;
  4923. char *pos, *end;
  4924. struct wpa_supplicant *wpa_s;
  4925. wpa_s = global->ifaces;
  4926. pos = buf;
  4927. end = buf + len;
  4928. while (wpa_s) {
  4929. res = os_snprintf(pos, end - pos, "%s\n", wpa_s->ifname);
  4930. if (res < 0 || res >= end - pos) {
  4931. *pos = '\0';
  4932. break;
  4933. }
  4934. pos += res;
  4935. wpa_s = wpa_s->next;
  4936. }
  4937. return pos - buf;
  4938. }
  4939. char * wpa_supplicant_global_ctrl_iface_process(struct wpa_global *global,
  4940. char *buf, size_t *resp_len)
  4941. {
  4942. char *reply;
  4943. const int reply_size = 2048;
  4944. int reply_len;
  4945. int level = MSG_DEBUG;
  4946. if (os_strcmp(buf, "PING") == 0)
  4947. level = MSG_EXCESSIVE;
  4948. wpa_hexdump_ascii(level, "RX global ctrl_iface",
  4949. (const u8 *) buf, os_strlen(buf));
  4950. reply = os_malloc(reply_size);
  4951. if (reply == NULL) {
  4952. *resp_len = 1;
  4953. return NULL;
  4954. }
  4955. os_memcpy(reply, "OK\n", 3);
  4956. reply_len = 3;
  4957. if (os_strcmp(buf, "PING") == 0) {
  4958. os_memcpy(reply, "PONG\n", 5);
  4959. reply_len = 5;
  4960. } else if (os_strncmp(buf, "INTERFACE_ADD ", 14) == 0) {
  4961. if (wpa_supplicant_global_iface_add(global, buf + 14))
  4962. reply_len = -1;
  4963. } else if (os_strncmp(buf, "INTERFACE_REMOVE ", 17) == 0) {
  4964. if (wpa_supplicant_global_iface_remove(global, buf + 17))
  4965. reply_len = -1;
  4966. } else if (os_strcmp(buf, "INTERFACE_LIST") == 0) {
  4967. reply_len = wpa_supplicant_global_iface_list(
  4968. global, reply, reply_size);
  4969. } else if (os_strcmp(buf, "INTERFACES") == 0) {
  4970. reply_len = wpa_supplicant_global_iface_interfaces(
  4971. global, reply, reply_size);
  4972. } else if (os_strcmp(buf, "TERMINATE") == 0) {
  4973. wpa_supplicant_terminate_proc(global);
  4974. } else if (os_strcmp(buf, "SUSPEND") == 0) {
  4975. wpas_notify_suspend(global);
  4976. } else if (os_strcmp(buf, "RESUME") == 0) {
  4977. wpas_notify_resume(global);
  4978. } else {
  4979. os_memcpy(reply, "UNKNOWN COMMAND\n", 16);
  4980. reply_len = 16;
  4981. }
  4982. if (reply_len < 0) {
  4983. os_memcpy(reply, "FAIL\n", 5);
  4984. reply_len = 5;
  4985. }
  4986. *resp_len = reply_len;
  4987. return reply;
  4988. }