ctrl_iface.c 115 KB

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