ctrl_iface.c 114 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838
  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",
  1160. ssid == wpa_s->current_ssid ?
  1161. "[CURRENT]" : "",
  1162. ssid->disabled ? "[DISABLED]" : "",
  1163. ssid->disabled == 2 ? "[P2P-PERSISTENT]" :
  1164. "");
  1165. if (ret < 0 || ret >= end - pos)
  1166. return pos - buf;
  1167. pos += ret;
  1168. ret = os_snprintf(pos, end - pos, "\n");
  1169. if (ret < 0 || ret >= end - pos)
  1170. return pos - buf;
  1171. pos += ret;
  1172. ssid = ssid->next;
  1173. }
  1174. return pos - buf;
  1175. }
  1176. static char * wpa_supplicant_cipher_txt(char *pos, char *end, int cipher)
  1177. {
  1178. int first = 1, ret;
  1179. ret = os_snprintf(pos, end - pos, "-");
  1180. if (ret < 0 || ret >= end - pos)
  1181. return pos;
  1182. pos += ret;
  1183. if (cipher & WPA_CIPHER_NONE) {
  1184. ret = os_snprintf(pos, end - pos, "%sNONE", first ? "" : "+");
  1185. if (ret < 0 || ret >= end - pos)
  1186. return pos;
  1187. pos += ret;
  1188. first = 0;
  1189. }
  1190. if (cipher & WPA_CIPHER_WEP40) {
  1191. ret = os_snprintf(pos, end - pos, "%sWEP40", first ? "" : "+");
  1192. if (ret < 0 || ret >= end - pos)
  1193. return pos;
  1194. pos += ret;
  1195. first = 0;
  1196. }
  1197. if (cipher & WPA_CIPHER_WEP104) {
  1198. ret = os_snprintf(pos, end - pos, "%sWEP104",
  1199. first ? "" : "+");
  1200. if (ret < 0 || ret >= end - pos)
  1201. return pos;
  1202. pos += ret;
  1203. first = 0;
  1204. }
  1205. if (cipher & WPA_CIPHER_TKIP) {
  1206. ret = os_snprintf(pos, end - pos, "%sTKIP", first ? "" : "+");
  1207. if (ret < 0 || ret >= end - pos)
  1208. return pos;
  1209. pos += ret;
  1210. first = 0;
  1211. }
  1212. if (cipher & WPA_CIPHER_CCMP) {
  1213. ret = os_snprintf(pos, end - pos, "%sCCMP", first ? "" : "+");
  1214. if (ret < 0 || ret >= end - pos)
  1215. return pos;
  1216. pos += ret;
  1217. first = 0;
  1218. }
  1219. return pos;
  1220. }
  1221. static char * wpa_supplicant_ie_txt(char *pos, char *end, const char *proto,
  1222. const u8 *ie, size_t ie_len)
  1223. {
  1224. struct wpa_ie_data data;
  1225. int first, ret;
  1226. ret = os_snprintf(pos, end - pos, "[%s-", proto);
  1227. if (ret < 0 || ret >= end - pos)
  1228. return pos;
  1229. pos += ret;
  1230. if (wpa_parse_wpa_ie(ie, ie_len, &data) < 0) {
  1231. ret = os_snprintf(pos, end - pos, "?]");
  1232. if (ret < 0 || ret >= end - pos)
  1233. return pos;
  1234. pos += ret;
  1235. return pos;
  1236. }
  1237. first = 1;
  1238. if (data.key_mgmt & WPA_KEY_MGMT_IEEE8021X) {
  1239. ret = os_snprintf(pos, end - pos, "%sEAP", first ? "" : "+");
  1240. if (ret < 0 || ret >= end - pos)
  1241. return pos;
  1242. pos += ret;
  1243. first = 0;
  1244. }
  1245. if (data.key_mgmt & WPA_KEY_MGMT_PSK) {
  1246. ret = os_snprintf(pos, end - pos, "%sPSK", first ? "" : "+");
  1247. if (ret < 0 || ret >= end - pos)
  1248. return pos;
  1249. pos += ret;
  1250. first = 0;
  1251. }
  1252. if (data.key_mgmt & WPA_KEY_MGMT_WPA_NONE) {
  1253. ret = os_snprintf(pos, end - pos, "%sNone", first ? "" : "+");
  1254. if (ret < 0 || ret >= end - pos)
  1255. return pos;
  1256. pos += ret;
  1257. first = 0;
  1258. }
  1259. #ifdef CONFIG_IEEE80211R
  1260. if (data.key_mgmt & WPA_KEY_MGMT_FT_IEEE8021X) {
  1261. ret = os_snprintf(pos, end - pos, "%sFT/EAP",
  1262. first ? "" : "+");
  1263. if (ret < 0 || ret >= end - pos)
  1264. return pos;
  1265. pos += ret;
  1266. first = 0;
  1267. }
  1268. if (data.key_mgmt & WPA_KEY_MGMT_FT_PSK) {
  1269. ret = os_snprintf(pos, end - pos, "%sFT/PSK",
  1270. first ? "" : "+");
  1271. if (ret < 0 || ret >= end - pos)
  1272. return pos;
  1273. pos += ret;
  1274. first = 0;
  1275. }
  1276. #endif /* CONFIG_IEEE80211R */
  1277. #ifdef CONFIG_IEEE80211W
  1278. if (data.key_mgmt & WPA_KEY_MGMT_IEEE8021X_SHA256) {
  1279. ret = os_snprintf(pos, end - pos, "%sEAP-SHA256",
  1280. first ? "" : "+");
  1281. if (ret < 0 || ret >= end - pos)
  1282. return pos;
  1283. pos += ret;
  1284. first = 0;
  1285. }
  1286. if (data.key_mgmt & WPA_KEY_MGMT_PSK_SHA256) {
  1287. ret = os_snprintf(pos, end - pos, "%sPSK-SHA256",
  1288. first ? "" : "+");
  1289. if (ret < 0 || ret >= end - pos)
  1290. return pos;
  1291. pos += ret;
  1292. first = 0;
  1293. }
  1294. #endif /* CONFIG_IEEE80211W */
  1295. pos = wpa_supplicant_cipher_txt(pos, end, data.pairwise_cipher);
  1296. if (data.capabilities & WPA_CAPABILITY_PREAUTH) {
  1297. ret = os_snprintf(pos, end - pos, "-preauth");
  1298. if (ret < 0 || ret >= end - pos)
  1299. return pos;
  1300. pos += ret;
  1301. }
  1302. ret = os_snprintf(pos, end - pos, "]");
  1303. if (ret < 0 || ret >= end - pos)
  1304. return pos;
  1305. pos += ret;
  1306. return pos;
  1307. }
  1308. #ifdef CONFIG_WPS
  1309. static char * wpa_supplicant_wps_ie_txt_buf(struct wpa_supplicant *wpa_s,
  1310. char *pos, char *end,
  1311. struct wpabuf *wps_ie)
  1312. {
  1313. int ret;
  1314. const char *txt;
  1315. if (wps_ie == NULL)
  1316. return pos;
  1317. if (wps_is_selected_pbc_registrar(wps_ie))
  1318. txt = "[WPS-PBC]";
  1319. #ifdef CONFIG_WPS2
  1320. else if (wps_is_addr_authorized(wps_ie, wpa_s->own_addr, 0))
  1321. txt = "[WPS-AUTH]";
  1322. #endif /* CONFIG_WPS2 */
  1323. else if (wps_is_selected_pin_registrar(wps_ie))
  1324. txt = "[WPS-PIN]";
  1325. else
  1326. txt = "[WPS]";
  1327. ret = os_snprintf(pos, end - pos, "%s", txt);
  1328. if (ret >= 0 && ret < end - pos)
  1329. pos += ret;
  1330. wpabuf_free(wps_ie);
  1331. return pos;
  1332. }
  1333. #endif /* CONFIG_WPS */
  1334. static char * wpa_supplicant_wps_ie_txt(struct wpa_supplicant *wpa_s,
  1335. char *pos, char *end,
  1336. const struct wpa_bss *bss)
  1337. {
  1338. #ifdef CONFIG_WPS
  1339. struct wpabuf *wps_ie;
  1340. wps_ie = wpa_bss_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
  1341. return wpa_supplicant_wps_ie_txt_buf(wpa_s, pos, end, wps_ie);
  1342. #else /* CONFIG_WPS */
  1343. return pos;
  1344. #endif /* CONFIG_WPS */
  1345. }
  1346. /* Format one result on one text line into a buffer. */
  1347. static int wpa_supplicant_ctrl_iface_scan_result(
  1348. struct wpa_supplicant *wpa_s,
  1349. const struct wpa_bss *bss, char *buf, size_t buflen)
  1350. {
  1351. char *pos, *end;
  1352. int ret;
  1353. const u8 *ie, *ie2, *p2p;
  1354. p2p = wpa_bss_get_vendor_ie(bss, P2P_IE_VENDOR_TYPE);
  1355. if (p2p && bss->ssid_len == P2P_WILDCARD_SSID_LEN &&
  1356. os_memcmp(bss->ssid, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN) ==
  1357. 0)
  1358. return 0; /* Do not show P2P listen discovery results here */
  1359. pos = buf;
  1360. end = buf + buflen;
  1361. ret = os_snprintf(pos, end - pos, MACSTR "\t%d\t%d\t",
  1362. MAC2STR(bss->bssid), bss->freq, bss->level);
  1363. if (ret < 0 || ret >= end - pos)
  1364. return -1;
  1365. pos += ret;
  1366. ie = wpa_bss_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
  1367. if (ie)
  1368. pos = wpa_supplicant_ie_txt(pos, end, "WPA", ie, 2 + ie[1]);
  1369. ie2 = wpa_bss_get_ie(bss, WLAN_EID_RSN);
  1370. if (ie2)
  1371. pos = wpa_supplicant_ie_txt(pos, end, "WPA2", ie2, 2 + ie2[1]);
  1372. pos = wpa_supplicant_wps_ie_txt(wpa_s, pos, end, bss);
  1373. if (!ie && !ie2 && bss->caps & IEEE80211_CAP_PRIVACY) {
  1374. ret = os_snprintf(pos, end - pos, "[WEP]");
  1375. if (ret < 0 || ret >= end - pos)
  1376. return -1;
  1377. pos += ret;
  1378. }
  1379. if (bss->caps & IEEE80211_CAP_IBSS) {
  1380. ret = os_snprintf(pos, end - pos, "[IBSS]");
  1381. if (ret < 0 || ret >= end - pos)
  1382. return -1;
  1383. pos += ret;
  1384. }
  1385. if (bss->caps & IEEE80211_CAP_ESS) {
  1386. ret = os_snprintf(pos, end - pos, "[ESS]");
  1387. if (ret < 0 || ret >= end - pos)
  1388. return -1;
  1389. pos += ret;
  1390. }
  1391. if (p2p) {
  1392. ret = os_snprintf(pos, end - pos, "[P2P]");
  1393. if (ret < 0 || ret >= end - pos)
  1394. return -1;
  1395. pos += ret;
  1396. }
  1397. #ifdef CONFIG_HS20
  1398. if (wpa_bss_get_vendor_ie(bss, HS20_IE_VENDOR_TYPE) && ie2) {
  1399. ret = os_snprintf(pos, end - pos, "[HS20]");
  1400. if (ret < 0 || ret >= end - pos)
  1401. return -1;
  1402. pos += ret;
  1403. }
  1404. #endif /* CONFIG_HS20 */
  1405. ret = os_snprintf(pos, end - pos, "\t%s",
  1406. wpa_ssid_txt(bss->ssid, bss->ssid_len));
  1407. if (ret < 0 || ret >= end - pos)
  1408. return -1;
  1409. pos += ret;
  1410. ret = os_snprintf(pos, end - pos, "\n");
  1411. if (ret < 0 || ret >= end - pos)
  1412. return -1;
  1413. pos += ret;
  1414. return pos - buf;
  1415. }
  1416. static int wpa_supplicant_ctrl_iface_scan_results(
  1417. struct wpa_supplicant *wpa_s, char *buf, size_t buflen)
  1418. {
  1419. char *pos, *end;
  1420. struct wpa_bss *bss;
  1421. int ret;
  1422. pos = buf;
  1423. end = buf + buflen;
  1424. ret = os_snprintf(pos, end - pos, "bssid / frequency / signal level / "
  1425. "flags / ssid\n");
  1426. if (ret < 0 || ret >= end - pos)
  1427. return pos - buf;
  1428. pos += ret;
  1429. dl_list_for_each(bss, &wpa_s->bss_id, struct wpa_bss, list_id) {
  1430. ret = wpa_supplicant_ctrl_iface_scan_result(wpa_s, bss, pos,
  1431. end - pos);
  1432. if (ret < 0 || ret >= end - pos)
  1433. return pos - buf;
  1434. pos += ret;
  1435. }
  1436. return pos - buf;
  1437. }
  1438. static int wpa_supplicant_ctrl_iface_select_network(
  1439. struct wpa_supplicant *wpa_s, char *cmd)
  1440. {
  1441. int id;
  1442. struct wpa_ssid *ssid;
  1443. /* cmd: "<network id>" or "any" */
  1444. if (os_strcmp(cmd, "any") == 0) {
  1445. wpa_printf(MSG_DEBUG, "CTRL_IFACE: SELECT_NETWORK any");
  1446. ssid = NULL;
  1447. } else {
  1448. id = atoi(cmd);
  1449. wpa_printf(MSG_DEBUG, "CTRL_IFACE: SELECT_NETWORK id=%d", id);
  1450. ssid = wpa_config_get_network(wpa_s->conf, id);
  1451. if (ssid == NULL) {
  1452. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find "
  1453. "network id=%d", id);
  1454. return -1;
  1455. }
  1456. if (ssid->disabled == 2) {
  1457. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Cannot use "
  1458. "SELECT_NETWORK with persistent P2P group");
  1459. return -1;
  1460. }
  1461. }
  1462. wpa_supplicant_select_network(wpa_s, ssid);
  1463. return 0;
  1464. }
  1465. static int wpa_supplicant_ctrl_iface_enable_network(
  1466. struct wpa_supplicant *wpa_s, char *cmd)
  1467. {
  1468. int id;
  1469. struct wpa_ssid *ssid;
  1470. /* cmd: "<network id>" or "all" */
  1471. if (os_strcmp(cmd, "all") == 0) {
  1472. wpa_printf(MSG_DEBUG, "CTRL_IFACE: ENABLE_NETWORK all");
  1473. ssid = NULL;
  1474. } else {
  1475. id = atoi(cmd);
  1476. wpa_printf(MSG_DEBUG, "CTRL_IFACE: ENABLE_NETWORK id=%d", id);
  1477. ssid = wpa_config_get_network(wpa_s->conf, id);
  1478. if (ssid == NULL) {
  1479. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find "
  1480. "network id=%d", id);
  1481. return -1;
  1482. }
  1483. if (ssid->disabled == 2) {
  1484. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Cannot use "
  1485. "ENABLE_NETWORK with persistent P2P group");
  1486. return -1;
  1487. }
  1488. if (os_strstr(cmd, " no-connect")) {
  1489. ssid->disabled = 0;
  1490. return 0;
  1491. }
  1492. }
  1493. wpa_supplicant_enable_network(wpa_s, ssid);
  1494. return 0;
  1495. }
  1496. static int wpa_supplicant_ctrl_iface_disable_network(
  1497. struct wpa_supplicant *wpa_s, char *cmd)
  1498. {
  1499. int id;
  1500. struct wpa_ssid *ssid;
  1501. /* cmd: "<network id>" or "all" */
  1502. if (os_strcmp(cmd, "all") == 0) {
  1503. wpa_printf(MSG_DEBUG, "CTRL_IFACE: DISABLE_NETWORK all");
  1504. ssid = NULL;
  1505. } else {
  1506. id = atoi(cmd);
  1507. wpa_printf(MSG_DEBUG, "CTRL_IFACE: DISABLE_NETWORK id=%d", id);
  1508. ssid = wpa_config_get_network(wpa_s->conf, id);
  1509. if (ssid == NULL) {
  1510. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find "
  1511. "network id=%d", id);
  1512. return -1;
  1513. }
  1514. if (ssid->disabled == 2) {
  1515. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Cannot use "
  1516. "DISABLE_NETWORK with persistent P2P "
  1517. "group");
  1518. return -1;
  1519. }
  1520. }
  1521. wpa_supplicant_disable_network(wpa_s, ssid);
  1522. return 0;
  1523. }
  1524. static int wpa_supplicant_ctrl_iface_add_network(
  1525. struct wpa_supplicant *wpa_s, char *buf, size_t buflen)
  1526. {
  1527. struct wpa_ssid *ssid;
  1528. int ret;
  1529. wpa_printf(MSG_DEBUG, "CTRL_IFACE: ADD_NETWORK");
  1530. ssid = wpa_config_add_network(wpa_s->conf);
  1531. if (ssid == NULL)
  1532. return -1;
  1533. wpas_notify_network_added(wpa_s, ssid);
  1534. ssid->disabled = 1;
  1535. wpa_config_set_network_defaults(ssid);
  1536. ret = os_snprintf(buf, buflen, "%d\n", ssid->id);
  1537. if (ret < 0 || (size_t) ret >= buflen)
  1538. return -1;
  1539. return ret;
  1540. }
  1541. static int wpa_supplicant_ctrl_iface_remove_network(
  1542. struct wpa_supplicant *wpa_s, char *cmd)
  1543. {
  1544. int id;
  1545. struct wpa_ssid *ssid;
  1546. /* cmd: "<network id>" or "all" */
  1547. if (os_strcmp(cmd, "all") == 0) {
  1548. wpa_printf(MSG_DEBUG, "CTRL_IFACE: REMOVE_NETWORK all");
  1549. ssid = wpa_s->conf->ssid;
  1550. while (ssid) {
  1551. struct wpa_ssid *remove_ssid = ssid;
  1552. id = ssid->id;
  1553. ssid = ssid->next;
  1554. wpas_notify_network_removed(wpa_s, remove_ssid);
  1555. wpa_config_remove_network(wpa_s->conf, id);
  1556. }
  1557. eapol_sm_invalidate_cached_session(wpa_s->eapol);
  1558. if (wpa_s->current_ssid) {
  1559. #ifdef CONFIG_SME
  1560. wpa_s->sme.prev_bssid_set = 0;
  1561. #endif /* CONFIG_SME */
  1562. wpa_sm_set_config(wpa_s->wpa, NULL);
  1563. eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
  1564. wpa_supplicant_disassociate(wpa_s,
  1565. WLAN_REASON_DEAUTH_LEAVING);
  1566. }
  1567. return 0;
  1568. }
  1569. id = atoi(cmd);
  1570. wpa_printf(MSG_DEBUG, "CTRL_IFACE: REMOVE_NETWORK id=%d", id);
  1571. ssid = wpa_config_get_network(wpa_s->conf, id);
  1572. if (ssid)
  1573. wpas_notify_network_removed(wpa_s, ssid);
  1574. if (ssid == NULL ||
  1575. wpa_config_remove_network(wpa_s->conf, id) < 0) {
  1576. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find network "
  1577. "id=%d", id);
  1578. return -1;
  1579. }
  1580. if (ssid == wpa_s->current_ssid || wpa_s->current_ssid == NULL) {
  1581. #ifdef CONFIG_SME
  1582. wpa_s->sme.prev_bssid_set = 0;
  1583. #endif /* CONFIG_SME */
  1584. /*
  1585. * Invalidate the EAP session cache if the current or
  1586. * previously used network is removed.
  1587. */
  1588. eapol_sm_invalidate_cached_session(wpa_s->eapol);
  1589. }
  1590. if (ssid == wpa_s->current_ssid) {
  1591. wpa_sm_set_config(wpa_s->wpa, NULL);
  1592. eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
  1593. wpa_supplicant_disassociate(wpa_s, WLAN_REASON_DEAUTH_LEAVING);
  1594. }
  1595. return 0;
  1596. }
  1597. static int wpa_supplicant_ctrl_iface_set_network(
  1598. struct wpa_supplicant *wpa_s, char *cmd)
  1599. {
  1600. int id;
  1601. struct wpa_ssid *ssid;
  1602. char *name, *value;
  1603. /* cmd: "<network id> <variable name> <value>" */
  1604. name = os_strchr(cmd, ' ');
  1605. if (name == NULL)
  1606. return -1;
  1607. *name++ = '\0';
  1608. value = os_strchr(name, ' ');
  1609. if (value == NULL)
  1610. return -1;
  1611. *value++ = '\0';
  1612. id = atoi(cmd);
  1613. wpa_printf(MSG_DEBUG, "CTRL_IFACE: SET_NETWORK id=%d name='%s'",
  1614. id, name);
  1615. wpa_hexdump_ascii_key(MSG_DEBUG, "CTRL_IFACE: value",
  1616. (u8 *) value, os_strlen(value));
  1617. ssid = wpa_config_get_network(wpa_s->conf, id);
  1618. if (ssid == NULL) {
  1619. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find network "
  1620. "id=%d", id);
  1621. return -1;
  1622. }
  1623. if (wpa_config_set(ssid, name, value, 0) < 0) {
  1624. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Failed to set network "
  1625. "variable '%s'", name);
  1626. return -1;
  1627. }
  1628. wpa_sm_pmksa_cache_flush(wpa_s->wpa, ssid);
  1629. if (wpa_s->current_ssid == ssid || wpa_s->current_ssid == NULL) {
  1630. /*
  1631. * Invalidate the EAP session cache if anything in the current
  1632. * or previously used configuration changes.
  1633. */
  1634. eapol_sm_invalidate_cached_session(wpa_s->eapol);
  1635. }
  1636. if ((os_strcmp(name, "psk") == 0 &&
  1637. value[0] == '"' && ssid->ssid_len) ||
  1638. (os_strcmp(name, "ssid") == 0 && ssid->passphrase))
  1639. wpa_config_update_psk(ssid);
  1640. else if (os_strcmp(name, "priority") == 0)
  1641. wpa_config_update_prio_list(wpa_s->conf);
  1642. return 0;
  1643. }
  1644. static int wpa_supplicant_ctrl_iface_get_network(
  1645. struct wpa_supplicant *wpa_s, char *cmd, char *buf, size_t buflen)
  1646. {
  1647. int id;
  1648. size_t res;
  1649. struct wpa_ssid *ssid;
  1650. char *name, *value;
  1651. /* cmd: "<network id> <variable name>" */
  1652. name = os_strchr(cmd, ' ');
  1653. if (name == NULL || buflen == 0)
  1654. return -1;
  1655. *name++ = '\0';
  1656. id = atoi(cmd);
  1657. wpa_printf(MSG_DEBUG, "CTRL_IFACE: GET_NETWORK id=%d name='%s'",
  1658. id, name);
  1659. ssid = wpa_config_get_network(wpa_s->conf, id);
  1660. if (ssid == NULL) {
  1661. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find network "
  1662. "id=%d", id);
  1663. return -1;
  1664. }
  1665. value = wpa_config_get_no_key(ssid, name);
  1666. if (value == NULL) {
  1667. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Failed to get network "
  1668. "variable '%s'", name);
  1669. return -1;
  1670. }
  1671. res = os_strlcpy(buf, value, buflen);
  1672. if (res >= buflen) {
  1673. os_free(value);
  1674. return -1;
  1675. }
  1676. os_free(value);
  1677. return res;
  1678. }
  1679. static int wpa_supplicant_ctrl_iface_list_creds(struct wpa_supplicant *wpa_s,
  1680. char *buf, size_t buflen)
  1681. {
  1682. char *pos, *end;
  1683. struct wpa_cred *cred;
  1684. int ret;
  1685. pos = buf;
  1686. end = buf + buflen;
  1687. ret = os_snprintf(pos, end - pos,
  1688. "cred id / realm / username / domain / imsi\n");
  1689. if (ret < 0 || ret >= end - pos)
  1690. return pos - buf;
  1691. pos += ret;
  1692. cred = wpa_s->conf->cred;
  1693. while (cred) {
  1694. ret = os_snprintf(pos, end - pos, "%d\t%s\t%s\t%s\t%s\n",
  1695. cred->id, cred->realm ? cred->realm : "",
  1696. cred->username ? cred->username : "",
  1697. cred->domain ? cred->domain : "",
  1698. cred->imsi ? cred->imsi : "");
  1699. if (ret < 0 || ret >= end - pos)
  1700. return pos - buf;
  1701. pos += ret;
  1702. cred = cred->next;
  1703. }
  1704. return pos - buf;
  1705. }
  1706. static int wpa_supplicant_ctrl_iface_add_cred(struct wpa_supplicant *wpa_s,
  1707. char *buf, size_t buflen)
  1708. {
  1709. struct wpa_cred *cred;
  1710. int ret;
  1711. wpa_printf(MSG_DEBUG, "CTRL_IFACE: ADD_CRED");
  1712. cred = wpa_config_add_cred(wpa_s->conf);
  1713. if (cred == NULL)
  1714. return -1;
  1715. ret = os_snprintf(buf, buflen, "%d\n", cred->id);
  1716. if (ret < 0 || (size_t) ret >= buflen)
  1717. return -1;
  1718. return ret;
  1719. }
  1720. static int wpa_supplicant_ctrl_iface_remove_cred(struct wpa_supplicant *wpa_s,
  1721. char *cmd)
  1722. {
  1723. int id;
  1724. struct wpa_cred *cred;
  1725. /* cmd: "<cred id>" or "all" */
  1726. if (os_strcmp(cmd, "all") == 0) {
  1727. wpa_printf(MSG_DEBUG, "CTRL_IFACE: REMOVE_CRED all");
  1728. cred = wpa_s->conf->cred;
  1729. while (cred) {
  1730. id = cred->id;
  1731. cred = cred->next;
  1732. wpa_config_remove_cred(wpa_s->conf, id);
  1733. }
  1734. return 0;
  1735. }
  1736. id = atoi(cmd);
  1737. wpa_printf(MSG_DEBUG, "CTRL_IFACE: REMOVE_CRED id=%d", id);
  1738. cred = wpa_config_get_cred(wpa_s->conf, id);
  1739. if (cred == NULL ||
  1740. wpa_config_remove_cred(wpa_s->conf, id) < 0) {
  1741. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find cred id=%d",
  1742. id);
  1743. return -1;
  1744. }
  1745. return 0;
  1746. }
  1747. static int wpa_supplicant_ctrl_iface_set_cred(struct wpa_supplicant *wpa_s,
  1748. char *cmd)
  1749. {
  1750. int id;
  1751. struct wpa_cred *cred;
  1752. char *name, *value;
  1753. /* cmd: "<cred id> <variable name> <value>" */
  1754. name = os_strchr(cmd, ' ');
  1755. if (name == NULL)
  1756. return -1;
  1757. *name++ = '\0';
  1758. value = os_strchr(name, ' ');
  1759. if (value == NULL)
  1760. return -1;
  1761. *value++ = '\0';
  1762. id = atoi(cmd);
  1763. wpa_printf(MSG_DEBUG, "CTRL_IFACE: SET_CRED id=%d name='%s'",
  1764. id, name);
  1765. wpa_hexdump_ascii_key(MSG_DEBUG, "CTRL_IFACE: value",
  1766. (u8 *) value, os_strlen(value));
  1767. cred = wpa_config_get_cred(wpa_s->conf, id);
  1768. if (cred == NULL) {
  1769. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find cred id=%d",
  1770. id);
  1771. return -1;
  1772. }
  1773. if (wpa_config_set_cred(cred, name, value, 0) < 0) {
  1774. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Failed to set cred "
  1775. "variable '%s'", name);
  1776. return -1;
  1777. }
  1778. return 0;
  1779. }
  1780. #ifndef CONFIG_NO_CONFIG_WRITE
  1781. static int wpa_supplicant_ctrl_iface_save_config(struct wpa_supplicant *wpa_s)
  1782. {
  1783. int ret;
  1784. if (!wpa_s->conf->update_config) {
  1785. wpa_printf(MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Not allowed "
  1786. "to update configuration (update_config=0)");
  1787. return -1;
  1788. }
  1789. ret = wpa_config_write(wpa_s->confname, wpa_s->conf);
  1790. if (ret) {
  1791. wpa_printf(MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Failed to "
  1792. "update configuration");
  1793. } else {
  1794. wpa_printf(MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Configuration"
  1795. " updated");
  1796. }
  1797. return ret;
  1798. }
  1799. #endif /* CONFIG_NO_CONFIG_WRITE */
  1800. static int ctrl_iface_get_capability_pairwise(int res, char *strict,
  1801. struct wpa_driver_capa *capa,
  1802. char *buf, size_t buflen)
  1803. {
  1804. int ret, first = 1;
  1805. char *pos, *end;
  1806. size_t len;
  1807. pos = buf;
  1808. end = pos + buflen;
  1809. if (res < 0) {
  1810. if (strict)
  1811. return 0;
  1812. len = os_strlcpy(buf, "CCMP TKIP NONE", buflen);
  1813. if (len >= buflen)
  1814. return -1;
  1815. return len;
  1816. }
  1817. if (capa->enc & WPA_DRIVER_CAPA_ENC_CCMP) {
  1818. ret = os_snprintf(pos, end - pos, "%sCCMP", first ? "" : " ");
  1819. if (ret < 0 || ret >= end - pos)
  1820. return pos - buf;
  1821. pos += ret;
  1822. first = 0;
  1823. }
  1824. if (capa->enc & WPA_DRIVER_CAPA_ENC_TKIP) {
  1825. ret = os_snprintf(pos, end - pos, "%sTKIP", first ? "" : " ");
  1826. if (ret < 0 || ret >= end - pos)
  1827. return pos - buf;
  1828. pos += ret;
  1829. first = 0;
  1830. }
  1831. if (capa->key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_WPA_NONE) {
  1832. ret = os_snprintf(pos, end - pos, "%sNONE", first ? "" : " ");
  1833. if (ret < 0 || ret >= end - pos)
  1834. return pos - buf;
  1835. pos += ret;
  1836. first = 0;
  1837. }
  1838. return pos - buf;
  1839. }
  1840. static int ctrl_iface_get_capability_group(int res, char *strict,
  1841. struct wpa_driver_capa *capa,
  1842. char *buf, size_t buflen)
  1843. {
  1844. int ret, first = 1;
  1845. char *pos, *end;
  1846. size_t len;
  1847. pos = buf;
  1848. end = pos + buflen;
  1849. if (res < 0) {
  1850. if (strict)
  1851. return 0;
  1852. len = os_strlcpy(buf, "CCMP TKIP WEP104 WEP40", buflen);
  1853. if (len >= buflen)
  1854. return -1;
  1855. return len;
  1856. }
  1857. if (capa->enc & WPA_DRIVER_CAPA_ENC_CCMP) {
  1858. ret = os_snprintf(pos, end - pos, "%sCCMP", first ? "" : " ");
  1859. if (ret < 0 || ret >= end - pos)
  1860. return pos - buf;
  1861. pos += ret;
  1862. first = 0;
  1863. }
  1864. if (capa->enc & WPA_DRIVER_CAPA_ENC_TKIP) {
  1865. ret = os_snprintf(pos, end - pos, "%sTKIP", first ? "" : " ");
  1866. if (ret < 0 || ret >= end - pos)
  1867. return pos - buf;
  1868. pos += ret;
  1869. first = 0;
  1870. }
  1871. if (capa->enc & WPA_DRIVER_CAPA_ENC_WEP104) {
  1872. ret = os_snprintf(pos, end - pos, "%sWEP104",
  1873. first ? "" : " ");
  1874. if (ret < 0 || ret >= end - pos)
  1875. return pos - buf;
  1876. pos += ret;
  1877. first = 0;
  1878. }
  1879. if (capa->enc & WPA_DRIVER_CAPA_ENC_WEP40) {
  1880. ret = os_snprintf(pos, end - pos, "%sWEP40", first ? "" : " ");
  1881. if (ret < 0 || ret >= end - pos)
  1882. return pos - buf;
  1883. pos += ret;
  1884. first = 0;
  1885. }
  1886. return pos - buf;
  1887. }
  1888. static int ctrl_iface_get_capability_key_mgmt(int res, char *strict,
  1889. struct wpa_driver_capa *capa,
  1890. char *buf, size_t buflen)
  1891. {
  1892. int ret;
  1893. char *pos, *end;
  1894. size_t len;
  1895. pos = buf;
  1896. end = pos + buflen;
  1897. if (res < 0) {
  1898. if (strict)
  1899. return 0;
  1900. len = os_strlcpy(buf, "WPA-PSK WPA-EAP IEEE8021X WPA-NONE "
  1901. "NONE", buflen);
  1902. if (len >= buflen)
  1903. return -1;
  1904. return len;
  1905. }
  1906. ret = os_snprintf(pos, end - pos, "NONE IEEE8021X");
  1907. if (ret < 0 || ret >= end - pos)
  1908. return pos - buf;
  1909. pos += ret;
  1910. if (capa->key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA |
  1911. WPA_DRIVER_CAPA_KEY_MGMT_WPA2)) {
  1912. ret = os_snprintf(pos, end - pos, " WPA-EAP");
  1913. if (ret < 0 || ret >= end - pos)
  1914. return pos - buf;
  1915. pos += ret;
  1916. }
  1917. if (capa->key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA_PSK |
  1918. WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK)) {
  1919. ret = os_snprintf(pos, end - pos, " WPA-PSK");
  1920. if (ret < 0 || ret >= end - pos)
  1921. return pos - buf;
  1922. pos += ret;
  1923. }
  1924. if (capa->key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_WPA_NONE) {
  1925. ret = os_snprintf(pos, end - pos, " WPA-NONE");
  1926. if (ret < 0 || ret >= end - pos)
  1927. return pos - buf;
  1928. pos += ret;
  1929. }
  1930. return pos - buf;
  1931. }
  1932. static int ctrl_iface_get_capability_proto(int res, char *strict,
  1933. struct wpa_driver_capa *capa,
  1934. char *buf, size_t buflen)
  1935. {
  1936. int ret, first = 1;
  1937. char *pos, *end;
  1938. size_t len;
  1939. pos = buf;
  1940. end = pos + buflen;
  1941. if (res < 0) {
  1942. if (strict)
  1943. return 0;
  1944. len = os_strlcpy(buf, "RSN WPA", buflen);
  1945. if (len >= buflen)
  1946. return -1;
  1947. return len;
  1948. }
  1949. if (capa->key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA2 |
  1950. WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK)) {
  1951. ret = os_snprintf(pos, end - pos, "%sRSN", first ? "" : " ");
  1952. if (ret < 0 || ret >= end - pos)
  1953. return pos - buf;
  1954. pos += ret;
  1955. first = 0;
  1956. }
  1957. if (capa->key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA |
  1958. WPA_DRIVER_CAPA_KEY_MGMT_WPA_PSK)) {
  1959. ret = os_snprintf(pos, end - pos, "%sWPA", first ? "" : " ");
  1960. if (ret < 0 || ret >= end - pos)
  1961. return pos - buf;
  1962. pos += ret;
  1963. first = 0;
  1964. }
  1965. return pos - buf;
  1966. }
  1967. static int ctrl_iface_get_capability_auth_alg(int res, char *strict,
  1968. struct wpa_driver_capa *capa,
  1969. char *buf, size_t buflen)
  1970. {
  1971. int ret, first = 1;
  1972. char *pos, *end;
  1973. size_t len;
  1974. pos = buf;
  1975. end = pos + buflen;
  1976. if (res < 0) {
  1977. if (strict)
  1978. return 0;
  1979. len = os_strlcpy(buf, "OPEN SHARED LEAP", buflen);
  1980. if (len >= buflen)
  1981. return -1;
  1982. return len;
  1983. }
  1984. if (capa->auth & (WPA_DRIVER_AUTH_OPEN)) {
  1985. ret = os_snprintf(pos, end - pos, "%sOPEN", first ? "" : " ");
  1986. if (ret < 0 || ret >= end - pos)
  1987. return pos - buf;
  1988. pos += ret;
  1989. first = 0;
  1990. }
  1991. if (capa->auth & (WPA_DRIVER_AUTH_SHARED)) {
  1992. ret = os_snprintf(pos, end - pos, "%sSHARED",
  1993. first ? "" : " ");
  1994. if (ret < 0 || ret >= end - pos)
  1995. return pos - buf;
  1996. pos += ret;
  1997. first = 0;
  1998. }
  1999. if (capa->auth & (WPA_DRIVER_AUTH_LEAP)) {
  2000. ret = os_snprintf(pos, end - pos, "%sLEAP", first ? "" : " ");
  2001. if (ret < 0 || ret >= end - pos)
  2002. return pos - buf;
  2003. pos += ret;
  2004. first = 0;
  2005. }
  2006. return pos - buf;
  2007. }
  2008. static int ctrl_iface_get_capability_channels(struct wpa_supplicant *wpa_s,
  2009. char *buf, size_t buflen)
  2010. {
  2011. struct hostapd_channel_data *chnl;
  2012. int ret, i, j;
  2013. char *pos, *end, *hmode;
  2014. pos = buf;
  2015. end = pos + buflen;
  2016. for (j = 0; j < wpa_s->hw.num_modes; j++) {
  2017. switch (wpa_s->hw.modes[j].mode) {
  2018. case HOSTAPD_MODE_IEEE80211B:
  2019. hmode = "B";
  2020. break;
  2021. case HOSTAPD_MODE_IEEE80211G:
  2022. hmode = "G";
  2023. break;
  2024. case HOSTAPD_MODE_IEEE80211A:
  2025. hmode = "A";
  2026. break;
  2027. default:
  2028. continue;
  2029. }
  2030. ret = os_snprintf(pos, end - pos, "Mode[%s] Channels:", hmode);
  2031. if (ret < 0 || ret >= end - pos)
  2032. return pos - buf;
  2033. pos += ret;
  2034. chnl = wpa_s->hw.modes[j].channels;
  2035. for (i = 0; i < wpa_s->hw.modes[j].num_channels; i++) {
  2036. if (chnl[i].flag & HOSTAPD_CHAN_DISABLED)
  2037. continue;
  2038. ret = os_snprintf(pos, end - pos, " %d", chnl[i].chan);
  2039. if (ret < 0 || ret >= end - pos)
  2040. return pos - buf;
  2041. pos += ret;
  2042. }
  2043. ret = os_snprintf(pos, end - pos, "\n");
  2044. if (ret < 0 || ret >= end - pos)
  2045. return pos - buf;
  2046. pos += ret;
  2047. }
  2048. return pos - buf;
  2049. }
  2050. static int wpa_supplicant_ctrl_iface_get_capability(
  2051. struct wpa_supplicant *wpa_s, const char *_field, char *buf,
  2052. size_t buflen)
  2053. {
  2054. struct wpa_driver_capa capa;
  2055. int res;
  2056. char *strict;
  2057. char field[30];
  2058. size_t len;
  2059. /* Determine whether or not strict checking was requested */
  2060. len = os_strlcpy(field, _field, sizeof(field));
  2061. if (len >= sizeof(field))
  2062. return -1;
  2063. strict = os_strchr(field, ' ');
  2064. if (strict != NULL) {
  2065. *strict++ = '\0';
  2066. if (os_strcmp(strict, "strict") != 0)
  2067. return -1;
  2068. }
  2069. wpa_printf(MSG_DEBUG, "CTRL_IFACE: GET_CAPABILITY '%s' %s",
  2070. field, strict ? strict : "");
  2071. if (os_strcmp(field, "eap") == 0) {
  2072. return eap_get_names(buf, buflen);
  2073. }
  2074. res = wpa_drv_get_capa(wpa_s, &capa);
  2075. if (os_strcmp(field, "pairwise") == 0)
  2076. return ctrl_iface_get_capability_pairwise(res, strict, &capa,
  2077. buf, buflen);
  2078. if (os_strcmp(field, "group") == 0)
  2079. return ctrl_iface_get_capability_group(res, strict, &capa,
  2080. buf, buflen);
  2081. if (os_strcmp(field, "key_mgmt") == 0)
  2082. return ctrl_iface_get_capability_key_mgmt(res, strict, &capa,
  2083. buf, buflen);
  2084. if (os_strcmp(field, "proto") == 0)
  2085. return ctrl_iface_get_capability_proto(res, strict, &capa,
  2086. buf, buflen);
  2087. if (os_strcmp(field, "auth_alg") == 0)
  2088. return ctrl_iface_get_capability_auth_alg(res, strict, &capa,
  2089. buf, buflen);
  2090. if (os_strcmp(field, "channels") == 0)
  2091. return ctrl_iface_get_capability_channels(wpa_s, buf, buflen);
  2092. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Unknown GET_CAPABILITY field '%s'",
  2093. field);
  2094. return -1;
  2095. }
  2096. #ifdef CONFIG_INTERWORKING
  2097. static char * anqp_add_hex(char *pos, char *end, const char *title,
  2098. struct wpabuf *data)
  2099. {
  2100. char *start = pos;
  2101. size_t i;
  2102. int ret;
  2103. const u8 *d;
  2104. if (data == NULL)
  2105. return start;
  2106. ret = os_snprintf(pos, end - pos, "%s=", title);
  2107. if (ret < 0 || ret >= end - pos)
  2108. return start;
  2109. pos += ret;
  2110. d = wpabuf_head_u8(data);
  2111. for (i = 0; i < wpabuf_len(data); i++) {
  2112. ret = os_snprintf(pos, end - pos, "%02x", *d++);
  2113. if (ret < 0 || ret >= end - pos)
  2114. return start;
  2115. pos += ret;
  2116. }
  2117. ret = os_snprintf(pos, end - pos, "\n");
  2118. if (ret < 0 || ret >= end - pos)
  2119. return start;
  2120. pos += ret;
  2121. return pos;
  2122. }
  2123. #endif /* CONFIG_INTERWORKING */
  2124. static int print_bss_info(struct wpa_supplicant *wpa_s, struct wpa_bss *bss,
  2125. unsigned long mask, char *buf, size_t buflen)
  2126. {
  2127. size_t i;
  2128. int ret;
  2129. char *pos, *end;
  2130. const u8 *ie, *ie2;
  2131. pos = buf;
  2132. end = buf + buflen;
  2133. if (mask & WPA_BSS_MASK_ID) {
  2134. ret = os_snprintf(pos, end - pos, "id=%u\n", bss->id);
  2135. if (ret < 0 || ret >= end - pos)
  2136. return 0;
  2137. pos += ret;
  2138. }
  2139. if (mask & WPA_BSS_MASK_BSSID) {
  2140. ret = os_snprintf(pos, end - pos, "bssid=" MACSTR "\n",
  2141. MAC2STR(bss->bssid));
  2142. if (ret < 0 || ret >= end - pos)
  2143. return 0;
  2144. pos += ret;
  2145. }
  2146. if (mask & WPA_BSS_MASK_FREQ) {
  2147. ret = os_snprintf(pos, end - pos, "freq=%d\n", bss->freq);
  2148. if (ret < 0 || ret >= end - pos)
  2149. return 0;
  2150. pos += ret;
  2151. }
  2152. if (mask & WPA_BSS_MASK_BEACON_INT) {
  2153. ret = os_snprintf(pos, end - pos, "beacon_int=%d\n",
  2154. bss->beacon_int);
  2155. if (ret < 0 || ret >= end - pos)
  2156. return 0;
  2157. pos += ret;
  2158. }
  2159. if (mask & WPA_BSS_MASK_CAPABILITIES) {
  2160. ret = os_snprintf(pos, end - pos, "capabilities=0x%04x\n",
  2161. bss->caps);
  2162. if (ret < 0 || ret >= end - pos)
  2163. return 0;
  2164. pos += ret;
  2165. }
  2166. if (mask & WPA_BSS_MASK_QUAL) {
  2167. ret = os_snprintf(pos, end - pos, "qual=%d\n", bss->qual);
  2168. if (ret < 0 || ret >= end - pos)
  2169. return 0;
  2170. pos += ret;
  2171. }
  2172. if (mask & WPA_BSS_MASK_NOISE) {
  2173. ret = os_snprintf(pos, end - pos, "noise=%d\n", bss->noise);
  2174. if (ret < 0 || ret >= end - pos)
  2175. return 0;
  2176. pos += ret;
  2177. }
  2178. if (mask & WPA_BSS_MASK_LEVEL) {
  2179. ret = os_snprintf(pos, end - pos, "level=%d\n", bss->level);
  2180. if (ret < 0 || ret >= end - pos)
  2181. return 0;
  2182. pos += ret;
  2183. }
  2184. if (mask & WPA_BSS_MASK_TSF) {
  2185. ret = os_snprintf(pos, end - pos, "tsf=%016llu\n",
  2186. (unsigned long long) bss->tsf);
  2187. if (ret < 0 || ret >= end - pos)
  2188. return 0;
  2189. pos += ret;
  2190. }
  2191. if (mask & WPA_BSS_MASK_AGE) {
  2192. struct os_time now;
  2193. os_get_time(&now);
  2194. ret = os_snprintf(pos, end - pos, "age=%d\n",
  2195. (int) (now.sec - bss->last_update.sec));
  2196. if (ret < 0 || ret >= end - pos)
  2197. return 0;
  2198. pos += ret;
  2199. }
  2200. if (mask & WPA_BSS_MASK_IE) {
  2201. ret = os_snprintf(pos, end - pos, "ie=");
  2202. if (ret < 0 || ret >= end - pos)
  2203. return 0;
  2204. pos += ret;
  2205. ie = (const u8 *) (bss + 1);
  2206. for (i = 0; i < bss->ie_len; i++) {
  2207. ret = os_snprintf(pos, end - pos, "%02x", *ie++);
  2208. if (ret < 0 || ret >= end - pos)
  2209. return 0;
  2210. pos += ret;
  2211. }
  2212. ret = os_snprintf(pos, end - pos, "\n");
  2213. if (ret < 0 || ret >= end - pos)
  2214. return 0;
  2215. pos += ret;
  2216. }
  2217. if (mask & WPA_BSS_MASK_FLAGS) {
  2218. ret = os_snprintf(pos, end - pos, "flags=");
  2219. if (ret < 0 || ret >= end - pos)
  2220. return 0;
  2221. pos += ret;
  2222. ie = wpa_bss_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
  2223. if (ie)
  2224. pos = wpa_supplicant_ie_txt(pos, end, "WPA", ie,
  2225. 2 + ie[1]);
  2226. ie2 = wpa_bss_get_ie(bss, WLAN_EID_RSN);
  2227. if (ie2)
  2228. pos = wpa_supplicant_ie_txt(pos, end, "WPA2", ie2,
  2229. 2 + ie2[1]);
  2230. pos = wpa_supplicant_wps_ie_txt(wpa_s, pos, end, bss);
  2231. if (!ie && !ie2 && bss->caps & IEEE80211_CAP_PRIVACY) {
  2232. ret = os_snprintf(pos, end - pos, "[WEP]");
  2233. if (ret < 0 || ret >= end - pos)
  2234. return 0;
  2235. pos += ret;
  2236. }
  2237. if (bss->caps & IEEE80211_CAP_IBSS) {
  2238. ret = os_snprintf(pos, end - pos, "[IBSS]");
  2239. if (ret < 0 || ret >= end - pos)
  2240. return 0;
  2241. pos += ret;
  2242. }
  2243. if (bss->caps & IEEE80211_CAP_ESS) {
  2244. ret = os_snprintf(pos, end - pos, "[ESS]");
  2245. if (ret < 0 || ret >= end - pos)
  2246. return 0;
  2247. pos += ret;
  2248. }
  2249. if (wpa_bss_get_vendor_ie(bss, P2P_IE_VENDOR_TYPE)) {
  2250. ret = os_snprintf(pos, end - pos, "[P2P]");
  2251. if (ret < 0 || ret >= end - pos)
  2252. return 0;
  2253. pos += ret;
  2254. }
  2255. #ifdef CONFIG_HS20
  2256. if (wpa_bss_get_vendor_ie(bss, HS20_IE_VENDOR_TYPE)) {
  2257. ret = os_snprintf(pos, end - pos, "[HS20]");
  2258. if (ret < 0 || ret >= end - pos)
  2259. return -1;
  2260. pos += ret;
  2261. }
  2262. #endif /* CONFIG_HS20 */
  2263. ret = os_snprintf(pos, end - pos, "\n");
  2264. if (ret < 0 || ret >= end - pos)
  2265. return 0;
  2266. pos += ret;
  2267. }
  2268. if (mask & WPA_BSS_MASK_SSID) {
  2269. ret = os_snprintf(pos, end - pos, "ssid=%s\n",
  2270. wpa_ssid_txt(bss->ssid, bss->ssid_len));
  2271. if (ret < 0 || ret >= end - pos)
  2272. return 0;
  2273. pos += ret;
  2274. }
  2275. #ifdef CONFIG_WPS
  2276. if (mask & WPA_BSS_MASK_WPS_SCAN) {
  2277. ie = (const u8 *) (bss + 1);
  2278. ret = wpas_wps_scan_result_text(ie, bss->ie_len, pos, end);
  2279. if (ret < 0 || ret >= end - pos)
  2280. return 0;
  2281. pos += ret;
  2282. }
  2283. #endif /* CONFIG_WPS */
  2284. #ifdef CONFIG_P2P
  2285. if (mask & WPA_BSS_MASK_P2P_SCAN) {
  2286. ie = (const u8 *) (bss + 1);
  2287. ret = wpas_p2p_scan_result_text(ie, bss->ie_len, pos, end);
  2288. if (ret < 0 || ret >= end - pos)
  2289. return 0;
  2290. pos += ret;
  2291. }
  2292. #endif /* CONFIG_P2P */
  2293. #ifdef CONFIG_INTERWORKING
  2294. if (mask & WPA_BSS_MASK_INTERNETW) {
  2295. pos = anqp_add_hex(pos, end, "anqp_venue_name",
  2296. bss->anqp_venue_name);
  2297. pos = anqp_add_hex(pos, end, "anqp_network_auth_type",
  2298. bss->anqp_network_auth_type);
  2299. pos = anqp_add_hex(pos, end, "anqp_roaming_consortium",
  2300. bss->anqp_roaming_consortium);
  2301. pos = anqp_add_hex(pos, end, "anqp_ip_addr_type_availability",
  2302. bss->anqp_ip_addr_type_availability);
  2303. pos = anqp_add_hex(pos, end, "anqp_nai_realm",
  2304. bss->anqp_nai_realm);
  2305. pos = anqp_add_hex(pos, end, "anqp_3gpp", bss->anqp_3gpp);
  2306. pos = anqp_add_hex(pos, end, "anqp_domain_name",
  2307. bss->anqp_domain_name);
  2308. #ifdef CONFIG_HS20
  2309. pos = anqp_add_hex(pos, end, "hs20_operator_friendly_name",
  2310. bss->hs20_operator_friendly_name);
  2311. pos = anqp_add_hex(pos, end, "hs20_wan_metrics",
  2312. bss->hs20_wan_metrics);
  2313. pos = anqp_add_hex(pos, end, "hs20_connection_capability",
  2314. bss->hs20_connection_capability);
  2315. #endif /* CONFIG_HS20 */
  2316. }
  2317. #endif /* CONFIG_INTERWORKING */
  2318. return pos - buf;
  2319. }
  2320. static int wpa_supplicant_ctrl_iface_bss(struct wpa_supplicant *wpa_s,
  2321. const char *cmd, char *buf,
  2322. size_t buflen)
  2323. {
  2324. u8 bssid[ETH_ALEN];
  2325. size_t i;
  2326. struct wpa_bss *bss;
  2327. struct wpa_bss *bsslast = NULL;
  2328. struct dl_list *next;
  2329. int ret = 0;
  2330. int len;
  2331. char *ctmp;
  2332. unsigned long mask = WPA_BSS_MASK_ALL;
  2333. if (os_strncmp(cmd, "RANGE=", 6) == 0) {
  2334. if (os_strncmp(cmd + 6, "ALL", 3) == 0) {
  2335. bss = dl_list_first(&wpa_s->bss_id, struct wpa_bss,
  2336. list_id);
  2337. bsslast = dl_list_last(&wpa_s->bss_id, struct wpa_bss,
  2338. list_id);
  2339. } else { /* N1-N2 */
  2340. unsigned int id1, id2;
  2341. if ((ctmp = os_strchr(cmd + 6, '-')) == NULL) {
  2342. wpa_printf(MSG_INFO, "Wrong BSS range "
  2343. "format");
  2344. return 0;
  2345. }
  2346. id1 = atoi(cmd + 6);
  2347. bss = wpa_bss_get_id(wpa_s, id1);
  2348. id2 = atoi(ctmp + 1);
  2349. if (id2 == 0)
  2350. bsslast = dl_list_last(&wpa_s->bss_id,
  2351. struct wpa_bss,
  2352. list_id);
  2353. else {
  2354. bsslast = wpa_bss_get_id(wpa_s, id2);
  2355. if (bsslast == NULL && bss && id2 > id1) {
  2356. struct wpa_bss *tmp = bss;
  2357. for (;;) {
  2358. next = tmp->list_id.next;
  2359. if (next == &wpa_s->bss_id)
  2360. break;
  2361. tmp = dl_list_entry(
  2362. next, struct wpa_bss,
  2363. list_id);
  2364. if (tmp->id > id2)
  2365. break;
  2366. bsslast = tmp;
  2367. }
  2368. }
  2369. }
  2370. }
  2371. } else if (os_strcmp(cmd, "FIRST") == 0)
  2372. bss = dl_list_first(&wpa_s->bss_id, struct wpa_bss, list_id);
  2373. else if (os_strncmp(cmd, "ID-", 3) == 0) {
  2374. i = atoi(cmd + 3);
  2375. bss = wpa_bss_get_id(wpa_s, i);
  2376. } else if (os_strncmp(cmd, "NEXT-", 5) == 0) {
  2377. i = atoi(cmd + 5);
  2378. bss = wpa_bss_get_id(wpa_s, i);
  2379. if (bss) {
  2380. next = bss->list_id.next;
  2381. if (next == &wpa_s->bss_id)
  2382. bss = NULL;
  2383. else
  2384. bss = dl_list_entry(next, struct wpa_bss,
  2385. list_id);
  2386. }
  2387. #ifdef CONFIG_P2P
  2388. } else if (os_strncmp(cmd, "p2p_dev_addr=", 13) == 0) {
  2389. if (hwaddr_aton(cmd + 13, bssid) == 0)
  2390. bss = wpa_bss_get_p2p_dev_addr(wpa_s, bssid);
  2391. else
  2392. bss = NULL;
  2393. #endif /* CONFIG_P2P */
  2394. } else if (hwaddr_aton(cmd, bssid) == 0)
  2395. bss = wpa_bss_get_bssid(wpa_s, bssid);
  2396. else {
  2397. struct wpa_bss *tmp;
  2398. i = atoi(cmd);
  2399. bss = NULL;
  2400. dl_list_for_each(tmp, &wpa_s->bss_id, struct wpa_bss, list_id)
  2401. {
  2402. if (i-- == 0) {
  2403. bss = tmp;
  2404. break;
  2405. }
  2406. }
  2407. }
  2408. if ((ctmp = os_strstr(cmd, "MASK=")) != NULL) {
  2409. mask = strtoul(ctmp + 5, NULL, 0x10);
  2410. if (mask == 0)
  2411. mask = WPA_BSS_MASK_ALL;
  2412. }
  2413. if (bss == NULL)
  2414. return 0;
  2415. if (bsslast == NULL)
  2416. bsslast = bss;
  2417. do {
  2418. len = print_bss_info(wpa_s, bss, mask, buf, buflen);
  2419. ret += len;
  2420. buf += len;
  2421. buflen -= len;
  2422. if (bss == bsslast)
  2423. break;
  2424. next = bss->list_id.next;
  2425. if (next == &wpa_s->bss_id)
  2426. break;
  2427. bss = dl_list_entry(next, struct wpa_bss, list_id);
  2428. } while (bss && len);
  2429. return ret;
  2430. }
  2431. static int wpa_supplicant_ctrl_iface_ap_scan(
  2432. struct wpa_supplicant *wpa_s, char *cmd)
  2433. {
  2434. int ap_scan = atoi(cmd);
  2435. return wpa_supplicant_set_ap_scan(wpa_s, ap_scan);
  2436. }
  2437. static int wpa_supplicant_ctrl_iface_scan_interval(
  2438. struct wpa_supplicant *wpa_s, char *cmd)
  2439. {
  2440. int scan_int = atoi(cmd);
  2441. return wpa_supplicant_set_scan_interval(wpa_s, scan_int);
  2442. }
  2443. static int wpa_supplicant_ctrl_iface_bss_expire_age(
  2444. struct wpa_supplicant *wpa_s, char *cmd)
  2445. {
  2446. int expire_age = atoi(cmd);
  2447. return wpa_supplicant_set_bss_expiration_age(wpa_s, expire_age);
  2448. }
  2449. static int wpa_supplicant_ctrl_iface_bss_expire_count(
  2450. struct wpa_supplicant *wpa_s, char *cmd)
  2451. {
  2452. int expire_count = atoi(cmd);
  2453. return wpa_supplicant_set_bss_expiration_count(wpa_s, expire_count);
  2454. }
  2455. static void wpa_supplicant_ctrl_iface_drop_sa(struct wpa_supplicant *wpa_s)
  2456. {
  2457. wpa_printf(MSG_DEBUG, "Dropping SA without deauthentication");
  2458. /* MLME-DELETEKEYS.request */
  2459. wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 0, 0, NULL, 0, NULL, 0);
  2460. wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 1, 0, NULL, 0, NULL, 0);
  2461. wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 2, 0, NULL, 0, NULL, 0);
  2462. wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 3, 0, NULL, 0, NULL, 0);
  2463. #ifdef CONFIG_IEEE80211W
  2464. wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 4, 0, NULL, 0, NULL, 0);
  2465. wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 5, 0, NULL, 0, NULL, 0);
  2466. #endif /* CONFIG_IEEE80211W */
  2467. wpa_drv_set_key(wpa_s, WPA_ALG_NONE, wpa_s->bssid, 0, 0, NULL, 0, NULL,
  2468. 0);
  2469. /* MLME-SETPROTECTION.request(None) */
  2470. wpa_drv_mlme_setprotection(wpa_s, wpa_s->bssid,
  2471. MLME_SETPROTECTION_PROTECT_TYPE_NONE,
  2472. MLME_SETPROTECTION_KEY_TYPE_PAIRWISE);
  2473. wpa_sm_drop_sa(wpa_s->wpa);
  2474. }
  2475. static int wpa_supplicant_ctrl_iface_roam(struct wpa_supplicant *wpa_s,
  2476. char *addr)
  2477. {
  2478. #ifdef CONFIG_NO_SCAN_PROCESSING
  2479. return -1;
  2480. #else /* CONFIG_NO_SCAN_PROCESSING */
  2481. u8 bssid[ETH_ALEN];
  2482. struct wpa_bss *bss;
  2483. struct wpa_ssid *ssid = wpa_s->current_ssid;
  2484. if (hwaddr_aton(addr, bssid)) {
  2485. wpa_printf(MSG_DEBUG, "CTRL_IFACE ROAM: invalid "
  2486. "address '%s'", addr);
  2487. return -1;
  2488. }
  2489. wpa_printf(MSG_DEBUG, "CTRL_IFACE ROAM " MACSTR, MAC2STR(bssid));
  2490. bss = wpa_bss_get_bssid(wpa_s, bssid);
  2491. if (!bss) {
  2492. wpa_printf(MSG_DEBUG, "CTRL_IFACE ROAM: Target AP not found "
  2493. "from BSS table");
  2494. return -1;
  2495. }
  2496. /*
  2497. * TODO: Find best network configuration block from configuration to
  2498. * allow roaming to other networks
  2499. */
  2500. if (!ssid) {
  2501. wpa_printf(MSG_DEBUG, "CTRL_IFACE ROAM: No network "
  2502. "configuration known for the target AP");
  2503. return -1;
  2504. }
  2505. wpa_s->reassociate = 1;
  2506. wpa_supplicant_connect(wpa_s, bss, ssid);
  2507. return 0;
  2508. #endif /* CONFIG_NO_SCAN_PROCESSING */
  2509. }
  2510. #ifdef CONFIG_P2P
  2511. static int p2p_ctrl_find(struct wpa_supplicant *wpa_s, char *cmd)
  2512. {
  2513. unsigned int timeout = atoi(cmd);
  2514. enum p2p_discovery_type type = P2P_FIND_START_WITH_FULL;
  2515. u8 dev_id[ETH_ALEN], *_dev_id = NULL;
  2516. char *pos;
  2517. if (os_strstr(cmd, "type=social"))
  2518. type = P2P_FIND_ONLY_SOCIAL;
  2519. else if (os_strstr(cmd, "type=progressive"))
  2520. type = P2P_FIND_PROGRESSIVE;
  2521. pos = os_strstr(cmd, "dev_id=");
  2522. if (pos) {
  2523. pos += 7;
  2524. if (hwaddr_aton(pos, dev_id))
  2525. return -1;
  2526. _dev_id = dev_id;
  2527. }
  2528. return wpas_p2p_find(wpa_s, timeout, type, 0, NULL, _dev_id);
  2529. }
  2530. static int p2p_ctrl_connect(struct wpa_supplicant *wpa_s, char *cmd,
  2531. char *buf, size_t buflen)
  2532. {
  2533. u8 addr[ETH_ALEN];
  2534. char *pos, *pos2;
  2535. char *pin = NULL;
  2536. enum p2p_wps_method wps_method;
  2537. int new_pin;
  2538. int ret;
  2539. int persistent_group, persistent_id = -1;
  2540. int join;
  2541. int auth;
  2542. int automatic;
  2543. int go_intent = -1;
  2544. int freq = 0;
  2545. int pd;
  2546. int ht40;
  2547. /* <addr> <"pbc" | "pin" | PIN> [label|display|keypad]
  2548. * [persistent|persistent=<network id>]
  2549. * [join] [auth] [go_intent=<0..15>] [freq=<in MHz>] [provdisc]
  2550. * [ht40] */
  2551. if (hwaddr_aton(cmd, addr))
  2552. return -1;
  2553. pos = cmd + 17;
  2554. if (*pos != ' ')
  2555. return -1;
  2556. pos++;
  2557. persistent_group = os_strstr(pos, " persistent") != NULL;
  2558. pos2 = os_strstr(pos, " persistent=");
  2559. if (pos2) {
  2560. struct wpa_ssid *ssid;
  2561. persistent_id = atoi(pos2 + 12);
  2562. ssid = wpa_config_get_network(wpa_s->conf, persistent_id);
  2563. if (ssid == NULL || ssid->disabled != 2 ||
  2564. ssid->mode != WPAS_MODE_P2P_GO) {
  2565. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find "
  2566. "SSID id=%d for persistent P2P group (GO)",
  2567. persistent_id);
  2568. return -1;
  2569. }
  2570. }
  2571. join = os_strstr(pos, " join") != NULL;
  2572. auth = os_strstr(pos, " auth") != NULL;
  2573. automatic = os_strstr(pos, " auto") != NULL;
  2574. pd = os_strstr(pos, " provdisc") != NULL;
  2575. ht40 = os_strstr(pos, " ht40") != NULL;
  2576. pos2 = os_strstr(pos, " go_intent=");
  2577. if (pos2) {
  2578. pos2 += 11;
  2579. go_intent = atoi(pos2);
  2580. if (go_intent < 0 || go_intent > 15)
  2581. return -1;
  2582. }
  2583. pos2 = os_strstr(pos, " freq=");
  2584. if (pos2) {
  2585. pos2 += 6;
  2586. freq = atoi(pos2);
  2587. if (freq <= 0)
  2588. return -1;
  2589. }
  2590. if (os_strncmp(pos, "pin", 3) == 0) {
  2591. /* Request random PIN (to be displayed) and enable the PIN */
  2592. wps_method = WPS_PIN_DISPLAY;
  2593. } else if (os_strncmp(pos, "pbc", 3) == 0) {
  2594. wps_method = WPS_PBC;
  2595. } else {
  2596. pin = pos;
  2597. pos = os_strchr(pin, ' ');
  2598. wps_method = WPS_PIN_KEYPAD;
  2599. if (pos) {
  2600. *pos++ = '\0';
  2601. if (os_strncmp(pos, "display", 7) == 0)
  2602. wps_method = WPS_PIN_DISPLAY;
  2603. }
  2604. if (!wps_pin_str_valid(pin)) {
  2605. os_memcpy(buf, "FAIL-INVALID-PIN\n", 17);
  2606. return 17;
  2607. }
  2608. }
  2609. new_pin = wpas_p2p_connect(wpa_s, addr, pin, wps_method,
  2610. persistent_group, automatic, join,
  2611. auth, go_intent, freq, persistent_id, pd,
  2612. ht40);
  2613. if (new_pin == -2) {
  2614. os_memcpy(buf, "FAIL-CHANNEL-UNAVAILABLE\n", 25);
  2615. return 25;
  2616. }
  2617. if (new_pin == -3) {
  2618. os_memcpy(buf, "FAIL-CHANNEL-UNSUPPORTED\n", 25);
  2619. return 25;
  2620. }
  2621. if (new_pin < 0)
  2622. return -1;
  2623. if (wps_method == WPS_PIN_DISPLAY && pin == NULL) {
  2624. ret = os_snprintf(buf, buflen, "%08d", new_pin);
  2625. if (ret < 0 || (size_t) ret >= buflen)
  2626. return -1;
  2627. return ret;
  2628. }
  2629. os_memcpy(buf, "OK\n", 3);
  2630. return 3;
  2631. }
  2632. static int p2p_ctrl_listen(struct wpa_supplicant *wpa_s, char *cmd)
  2633. {
  2634. unsigned int timeout = atoi(cmd);
  2635. return wpas_p2p_listen(wpa_s, timeout);
  2636. }
  2637. static int p2p_ctrl_prov_disc(struct wpa_supplicant *wpa_s, char *cmd)
  2638. {
  2639. u8 addr[ETH_ALEN];
  2640. char *pos;
  2641. enum wpas_p2p_prov_disc_use use = WPAS_P2P_PD_FOR_GO_NEG;
  2642. /* <addr> <config method> [join|auto] */
  2643. if (hwaddr_aton(cmd, addr))
  2644. return -1;
  2645. pos = cmd + 17;
  2646. if (*pos != ' ')
  2647. return -1;
  2648. pos++;
  2649. if (os_strstr(pos, " join") != NULL)
  2650. use = WPAS_P2P_PD_FOR_JOIN;
  2651. else if (os_strstr(pos, " auto") != NULL)
  2652. use = WPAS_P2P_PD_AUTO;
  2653. return wpas_p2p_prov_disc(wpa_s, addr, pos, use);
  2654. }
  2655. static int p2p_get_passphrase(struct wpa_supplicant *wpa_s, char *buf,
  2656. size_t buflen)
  2657. {
  2658. struct wpa_ssid *ssid = wpa_s->current_ssid;
  2659. if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GO ||
  2660. ssid->passphrase == NULL)
  2661. return -1;
  2662. os_strlcpy(buf, ssid->passphrase, buflen);
  2663. return os_strlen(buf);
  2664. }
  2665. static int p2p_ctrl_serv_disc_req(struct wpa_supplicant *wpa_s, char *cmd,
  2666. char *buf, size_t buflen)
  2667. {
  2668. u64 ref;
  2669. int res;
  2670. u8 dst_buf[ETH_ALEN], *dst;
  2671. struct wpabuf *tlvs;
  2672. char *pos;
  2673. size_t len;
  2674. if (hwaddr_aton(cmd, dst_buf))
  2675. return -1;
  2676. dst = dst_buf;
  2677. if (dst[0] == 0 && dst[1] == 0 && dst[2] == 0 &&
  2678. dst[3] == 0 && dst[4] == 0 && dst[5] == 0)
  2679. dst = NULL;
  2680. pos = cmd + 17;
  2681. if (*pos != ' ')
  2682. return -1;
  2683. pos++;
  2684. if (os_strncmp(pos, "upnp ", 5) == 0) {
  2685. u8 version;
  2686. pos += 5;
  2687. if (hexstr2bin(pos, &version, 1) < 0)
  2688. return -1;
  2689. pos += 2;
  2690. if (*pos != ' ')
  2691. return -1;
  2692. pos++;
  2693. ref = wpas_p2p_sd_request_upnp(wpa_s, dst, version, pos);
  2694. } else {
  2695. len = os_strlen(pos);
  2696. if (len & 1)
  2697. return -1;
  2698. len /= 2;
  2699. tlvs = wpabuf_alloc(len);
  2700. if (tlvs == NULL)
  2701. return -1;
  2702. if (hexstr2bin(pos, wpabuf_put(tlvs, len), len) < 0) {
  2703. wpabuf_free(tlvs);
  2704. return -1;
  2705. }
  2706. ref = wpas_p2p_sd_request(wpa_s, dst, tlvs);
  2707. wpabuf_free(tlvs);
  2708. }
  2709. if (ref == 0)
  2710. return -1;
  2711. res = os_snprintf(buf, buflen, "%llx", (long long unsigned) ref);
  2712. if (res < 0 || (unsigned) res >= buflen)
  2713. return -1;
  2714. return res;
  2715. }
  2716. static int p2p_ctrl_serv_disc_cancel_req(struct wpa_supplicant *wpa_s,
  2717. char *cmd)
  2718. {
  2719. long long unsigned val;
  2720. u64 req;
  2721. if (sscanf(cmd, "%llx", &val) != 1)
  2722. return -1;
  2723. req = val;
  2724. return wpas_p2p_sd_cancel_request(wpa_s, req);
  2725. }
  2726. static int p2p_ctrl_serv_disc_resp(struct wpa_supplicant *wpa_s, char *cmd)
  2727. {
  2728. int freq;
  2729. u8 dst[ETH_ALEN];
  2730. u8 dialog_token;
  2731. struct wpabuf *resp_tlvs;
  2732. char *pos, *pos2;
  2733. size_t len;
  2734. pos = os_strchr(cmd, ' ');
  2735. if (pos == NULL)
  2736. return -1;
  2737. *pos++ = '\0';
  2738. freq = atoi(cmd);
  2739. if (freq == 0)
  2740. return -1;
  2741. if (hwaddr_aton(pos, dst))
  2742. return -1;
  2743. pos += 17;
  2744. if (*pos != ' ')
  2745. return -1;
  2746. pos++;
  2747. pos2 = os_strchr(pos, ' ');
  2748. if (pos2 == NULL)
  2749. return -1;
  2750. *pos2++ = '\0';
  2751. dialog_token = atoi(pos);
  2752. len = os_strlen(pos2);
  2753. if (len & 1)
  2754. return -1;
  2755. len /= 2;
  2756. resp_tlvs = wpabuf_alloc(len);
  2757. if (resp_tlvs == NULL)
  2758. return -1;
  2759. if (hexstr2bin(pos2, wpabuf_put(resp_tlvs, len), len) < 0) {
  2760. wpabuf_free(resp_tlvs);
  2761. return -1;
  2762. }
  2763. wpas_p2p_sd_response(wpa_s, freq, dst, dialog_token, resp_tlvs);
  2764. wpabuf_free(resp_tlvs);
  2765. return 0;
  2766. }
  2767. static int p2p_ctrl_serv_disc_external(struct wpa_supplicant *wpa_s,
  2768. char *cmd)
  2769. {
  2770. if (os_strcmp(cmd, "0") && os_strcmp(cmd, "1"))
  2771. return -1;
  2772. wpa_s->p2p_sd_over_ctrl_iface = atoi(cmd);
  2773. return 0;
  2774. }
  2775. static int p2p_ctrl_service_add_bonjour(struct wpa_supplicant *wpa_s,
  2776. char *cmd)
  2777. {
  2778. char *pos;
  2779. size_t len;
  2780. struct wpabuf *query, *resp;
  2781. pos = os_strchr(cmd, ' ');
  2782. if (pos == NULL)
  2783. return -1;
  2784. *pos++ = '\0';
  2785. len = os_strlen(cmd);
  2786. if (len & 1)
  2787. return -1;
  2788. len /= 2;
  2789. query = wpabuf_alloc(len);
  2790. if (query == NULL)
  2791. return -1;
  2792. if (hexstr2bin(cmd, wpabuf_put(query, len), len) < 0) {
  2793. wpabuf_free(query);
  2794. return -1;
  2795. }
  2796. len = os_strlen(pos);
  2797. if (len & 1) {
  2798. wpabuf_free(query);
  2799. return -1;
  2800. }
  2801. len /= 2;
  2802. resp = wpabuf_alloc(len);
  2803. if (resp == NULL) {
  2804. wpabuf_free(query);
  2805. return -1;
  2806. }
  2807. if (hexstr2bin(pos, wpabuf_put(resp, len), len) < 0) {
  2808. wpabuf_free(query);
  2809. wpabuf_free(resp);
  2810. return -1;
  2811. }
  2812. if (wpas_p2p_service_add_bonjour(wpa_s, query, resp) < 0) {
  2813. wpabuf_free(query);
  2814. wpabuf_free(resp);
  2815. return -1;
  2816. }
  2817. return 0;
  2818. }
  2819. static int p2p_ctrl_service_add_upnp(struct wpa_supplicant *wpa_s, char *cmd)
  2820. {
  2821. char *pos;
  2822. u8 version;
  2823. pos = os_strchr(cmd, ' ');
  2824. if (pos == NULL)
  2825. return -1;
  2826. *pos++ = '\0';
  2827. if (hexstr2bin(cmd, &version, 1) < 0)
  2828. return -1;
  2829. return wpas_p2p_service_add_upnp(wpa_s, version, pos);
  2830. }
  2831. static int p2p_ctrl_service_add(struct wpa_supplicant *wpa_s, char *cmd)
  2832. {
  2833. char *pos;
  2834. pos = os_strchr(cmd, ' ');
  2835. if (pos == NULL)
  2836. return -1;
  2837. *pos++ = '\0';
  2838. if (os_strcmp(cmd, "bonjour") == 0)
  2839. return p2p_ctrl_service_add_bonjour(wpa_s, pos);
  2840. if (os_strcmp(cmd, "upnp") == 0)
  2841. return p2p_ctrl_service_add_upnp(wpa_s, pos);
  2842. wpa_printf(MSG_DEBUG, "Unknown service '%s'", cmd);
  2843. return -1;
  2844. }
  2845. static int p2p_ctrl_service_del_bonjour(struct wpa_supplicant *wpa_s,
  2846. char *cmd)
  2847. {
  2848. size_t len;
  2849. struct wpabuf *query;
  2850. int ret;
  2851. len = os_strlen(cmd);
  2852. if (len & 1)
  2853. return -1;
  2854. len /= 2;
  2855. query = wpabuf_alloc(len);
  2856. if (query == NULL)
  2857. return -1;
  2858. if (hexstr2bin(cmd, wpabuf_put(query, len), len) < 0) {
  2859. wpabuf_free(query);
  2860. return -1;
  2861. }
  2862. ret = wpas_p2p_service_del_bonjour(wpa_s, query);
  2863. wpabuf_free(query);
  2864. return ret;
  2865. }
  2866. static int p2p_ctrl_service_del_upnp(struct wpa_supplicant *wpa_s, char *cmd)
  2867. {
  2868. char *pos;
  2869. u8 version;
  2870. pos = os_strchr(cmd, ' ');
  2871. if (pos == NULL)
  2872. return -1;
  2873. *pos++ = '\0';
  2874. if (hexstr2bin(cmd, &version, 1) < 0)
  2875. return -1;
  2876. return wpas_p2p_service_del_upnp(wpa_s, version, pos);
  2877. }
  2878. static int p2p_ctrl_service_del(struct wpa_supplicant *wpa_s, char *cmd)
  2879. {
  2880. char *pos;
  2881. pos = os_strchr(cmd, ' ');
  2882. if (pos == NULL)
  2883. return -1;
  2884. *pos++ = '\0';
  2885. if (os_strcmp(cmd, "bonjour") == 0)
  2886. return p2p_ctrl_service_del_bonjour(wpa_s, pos);
  2887. if (os_strcmp(cmd, "upnp") == 0)
  2888. return p2p_ctrl_service_del_upnp(wpa_s, pos);
  2889. wpa_printf(MSG_DEBUG, "Unknown service '%s'", cmd);
  2890. return -1;
  2891. }
  2892. static int p2p_ctrl_reject(struct wpa_supplicant *wpa_s, char *cmd)
  2893. {
  2894. u8 addr[ETH_ALEN];
  2895. /* <addr> */
  2896. if (hwaddr_aton(cmd, addr))
  2897. return -1;
  2898. return wpas_p2p_reject(wpa_s, addr);
  2899. }
  2900. static int p2p_ctrl_invite_persistent(struct wpa_supplicant *wpa_s, char *cmd)
  2901. {
  2902. char *pos;
  2903. int id;
  2904. struct wpa_ssid *ssid;
  2905. u8 peer[ETH_ALEN];
  2906. id = atoi(cmd);
  2907. pos = os_strstr(cmd, " peer=");
  2908. if (pos) {
  2909. pos += 6;
  2910. if (hwaddr_aton(pos, peer))
  2911. return -1;
  2912. }
  2913. ssid = wpa_config_get_network(wpa_s->conf, id);
  2914. if (ssid == NULL || ssid->disabled != 2) {
  2915. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find SSID id=%d "
  2916. "for persistent P2P group",
  2917. id);
  2918. return -1;
  2919. }
  2920. return wpas_p2p_invite(wpa_s, pos ? peer : NULL, ssid, NULL);
  2921. }
  2922. static int p2p_ctrl_invite_group(struct wpa_supplicant *wpa_s, char *cmd)
  2923. {
  2924. char *pos;
  2925. u8 peer[ETH_ALEN], go_dev_addr[ETH_ALEN], *go_dev = NULL;
  2926. pos = os_strstr(cmd, " peer=");
  2927. if (!pos)
  2928. return -1;
  2929. *pos = '\0';
  2930. pos += 6;
  2931. if (hwaddr_aton(pos, peer)) {
  2932. wpa_printf(MSG_DEBUG, "P2P: Invalid MAC address '%s'", pos);
  2933. return -1;
  2934. }
  2935. pos = os_strstr(pos, " go_dev_addr=");
  2936. if (pos) {
  2937. pos += 13;
  2938. if (hwaddr_aton(pos, go_dev_addr)) {
  2939. wpa_printf(MSG_DEBUG, "P2P: Invalid MAC address '%s'",
  2940. pos);
  2941. return -1;
  2942. }
  2943. go_dev = go_dev_addr;
  2944. }
  2945. return wpas_p2p_invite_group(wpa_s, cmd, peer, go_dev);
  2946. }
  2947. static int p2p_ctrl_invite(struct wpa_supplicant *wpa_s, char *cmd)
  2948. {
  2949. if (os_strncmp(cmd, "persistent=", 11) == 0)
  2950. return p2p_ctrl_invite_persistent(wpa_s, cmd + 11);
  2951. if (os_strncmp(cmd, "group=", 6) == 0)
  2952. return p2p_ctrl_invite_group(wpa_s, cmd + 6);
  2953. return -1;
  2954. }
  2955. static int p2p_ctrl_group_add_persistent(struct wpa_supplicant *wpa_s,
  2956. char *cmd, int freq, int ht40)
  2957. {
  2958. int id;
  2959. struct wpa_ssid *ssid;
  2960. id = atoi(cmd);
  2961. ssid = wpa_config_get_network(wpa_s->conf, id);
  2962. if (ssid == NULL || ssid->disabled != 2) {
  2963. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find SSID id=%d "
  2964. "for persistent P2P group",
  2965. id);
  2966. return -1;
  2967. }
  2968. return wpas_p2p_group_add_persistent(wpa_s, ssid, 0, freq, ht40);
  2969. }
  2970. static int p2p_ctrl_group_add(struct wpa_supplicant *wpa_s, char *cmd)
  2971. {
  2972. int freq = 0, ht40;
  2973. char *pos;
  2974. pos = os_strstr(cmd, "freq=");
  2975. if (pos)
  2976. freq = atoi(pos + 5);
  2977. ht40 = os_strstr(cmd, "ht40") != NULL;
  2978. if (os_strncmp(cmd, "persistent=", 11) == 0)
  2979. return p2p_ctrl_group_add_persistent(wpa_s, cmd + 11, freq,
  2980. ht40);
  2981. if (os_strcmp(cmd, "persistent") == 0 ||
  2982. os_strncmp(cmd, "persistent ", 11) == 0)
  2983. return wpas_p2p_group_add(wpa_s, 1, freq, ht40);
  2984. if (os_strncmp(cmd, "freq=", 5) == 0)
  2985. return wpas_p2p_group_add(wpa_s, 0, freq, ht40);
  2986. if (ht40)
  2987. return wpas_p2p_group_add(wpa_s, 0, freq, ht40);
  2988. wpa_printf(MSG_DEBUG, "CTRL: Invalid P2P_GROUP_ADD parameters '%s'",
  2989. cmd);
  2990. return -1;
  2991. }
  2992. static int p2p_ctrl_peer(struct wpa_supplicant *wpa_s, char *cmd,
  2993. char *buf, size_t buflen)
  2994. {
  2995. u8 addr[ETH_ALEN], *addr_ptr;
  2996. int next, res;
  2997. const struct p2p_peer_info *info;
  2998. char *pos, *end;
  2999. char devtype[WPS_DEV_TYPE_BUFSIZE];
  3000. struct wpa_ssid *ssid;
  3001. if (!wpa_s->global->p2p)
  3002. return -1;
  3003. if (os_strcmp(cmd, "FIRST") == 0) {
  3004. addr_ptr = NULL;
  3005. next = 0;
  3006. } else if (os_strncmp(cmd, "NEXT-", 5) == 0) {
  3007. if (hwaddr_aton(cmd + 5, addr) < 0)
  3008. return -1;
  3009. addr_ptr = addr;
  3010. next = 1;
  3011. } else {
  3012. if (hwaddr_aton(cmd, addr) < 0)
  3013. return -1;
  3014. addr_ptr = addr;
  3015. next = 0;
  3016. }
  3017. info = p2p_get_peer_info(wpa_s->global->p2p, addr_ptr, next);
  3018. if (info == NULL)
  3019. return -1;
  3020. pos = buf;
  3021. end = buf + buflen;
  3022. res = os_snprintf(pos, end - pos, MACSTR "\n"
  3023. "pri_dev_type=%s\n"
  3024. "device_name=%s\n"
  3025. "manufacturer=%s\n"
  3026. "model_name=%s\n"
  3027. "model_number=%s\n"
  3028. "serial_number=%s\n"
  3029. "config_methods=0x%x\n"
  3030. "dev_capab=0x%x\n"
  3031. "group_capab=0x%x\n"
  3032. "level=%d\n",
  3033. MAC2STR(info->p2p_device_addr),
  3034. wps_dev_type_bin2str(info->pri_dev_type,
  3035. devtype, sizeof(devtype)),
  3036. info->device_name,
  3037. info->manufacturer,
  3038. info->model_name,
  3039. info->model_number,
  3040. info->serial_number,
  3041. info->config_methods,
  3042. info->dev_capab,
  3043. info->group_capab,
  3044. info->level);
  3045. if (res < 0 || res >= end - pos)
  3046. return pos - buf;
  3047. pos += res;
  3048. ssid = wpas_p2p_get_persistent(wpa_s, info->p2p_device_addr, NULL, 0);
  3049. if (ssid) {
  3050. res = os_snprintf(pos, end - pos, "persistent=%d\n", ssid->id);
  3051. if (res < 0 || res >= end - pos)
  3052. return pos - buf;
  3053. pos += res;
  3054. }
  3055. res = p2p_get_peer_info_txt(info, pos, end - pos);
  3056. if (res < 0)
  3057. return pos - buf;
  3058. pos += res;
  3059. return pos - buf;
  3060. }
  3061. static int p2p_ctrl_disallow_freq(struct wpa_supplicant *wpa_s,
  3062. const char *param)
  3063. {
  3064. struct wpa_freq_range *freq = NULL, *n;
  3065. unsigned int count = 0, i;
  3066. const char *pos, *pos2, *pos3;
  3067. if (wpa_s->global->p2p == NULL)
  3068. return -1;
  3069. /*
  3070. * param includes comma separated frequency range.
  3071. * For example: 2412-2432,2462,5000-6000
  3072. */
  3073. pos = param;
  3074. while (pos && pos[0]) {
  3075. n = os_realloc_array(freq, count + 1,
  3076. sizeof(struct wpa_freq_range));
  3077. if (n == NULL) {
  3078. os_free(freq);
  3079. return -1;
  3080. }
  3081. freq = n;
  3082. freq[count].min = atoi(pos);
  3083. pos2 = os_strchr(pos, '-');
  3084. pos3 = os_strchr(pos, ',');
  3085. if (pos2 && (!pos3 || pos2 < pos3)) {
  3086. pos2++;
  3087. freq[count].max = atoi(pos2);
  3088. } else
  3089. freq[count].max = freq[count].min;
  3090. pos = pos3;
  3091. if (pos)
  3092. pos++;
  3093. count++;
  3094. }
  3095. for (i = 0; i < count; i++) {
  3096. wpa_printf(MSG_DEBUG, "P2P: Disallowed frequency range %u-%u",
  3097. freq[i].min, freq[i].max);
  3098. }
  3099. os_free(wpa_s->global->p2p_disallow_freq);
  3100. wpa_s->global->p2p_disallow_freq = freq;
  3101. wpa_s->global->num_p2p_disallow_freq = count;
  3102. wpas_p2p_update_channel_list(wpa_s);
  3103. return 0;
  3104. }
  3105. static int p2p_ctrl_set(struct wpa_supplicant *wpa_s, char *cmd)
  3106. {
  3107. char *param;
  3108. if (wpa_s->global->p2p == NULL)
  3109. return -1;
  3110. param = os_strchr(cmd, ' ');
  3111. if (param == NULL)
  3112. return -1;
  3113. *param++ = '\0';
  3114. if (os_strcmp(cmd, "discoverability") == 0) {
  3115. p2p_set_client_discoverability(wpa_s->global->p2p,
  3116. atoi(param));
  3117. return 0;
  3118. }
  3119. if (os_strcmp(cmd, "managed") == 0) {
  3120. p2p_set_managed_oper(wpa_s->global->p2p, atoi(param));
  3121. return 0;
  3122. }
  3123. if (os_strcmp(cmd, "listen_channel") == 0) {
  3124. return p2p_set_listen_channel(wpa_s->global->p2p, 81,
  3125. atoi(param));
  3126. }
  3127. if (os_strcmp(cmd, "ssid_postfix") == 0) {
  3128. return p2p_set_ssid_postfix(wpa_s->global->p2p, (u8 *) param,
  3129. os_strlen(param));
  3130. }
  3131. if (os_strcmp(cmd, "noa") == 0) {
  3132. char *pos;
  3133. int count, start, duration;
  3134. /* GO NoA parameters: count,start_offset(ms),duration(ms) */
  3135. count = atoi(param);
  3136. pos = os_strchr(param, ',');
  3137. if (pos == NULL)
  3138. return -1;
  3139. pos++;
  3140. start = atoi(pos);
  3141. pos = os_strchr(pos, ',');
  3142. if (pos == NULL)
  3143. return -1;
  3144. pos++;
  3145. duration = atoi(pos);
  3146. if (count < 0 || count > 255 || start < 0 || duration < 0)
  3147. return -1;
  3148. if (count == 0 && duration > 0)
  3149. return -1;
  3150. wpa_printf(MSG_DEBUG, "CTRL_IFACE: P2P_SET GO NoA: count=%d "
  3151. "start=%d duration=%d", count, start, duration);
  3152. return wpas_p2p_set_noa(wpa_s, count, start, duration);
  3153. }
  3154. if (os_strcmp(cmd, "ps") == 0)
  3155. return wpa_drv_set_p2p_powersave(wpa_s, atoi(param), -1, -1);
  3156. if (os_strcmp(cmd, "oppps") == 0)
  3157. return wpa_drv_set_p2p_powersave(wpa_s, -1, atoi(param), -1);
  3158. if (os_strcmp(cmd, "ctwindow") == 0)
  3159. return wpa_drv_set_p2p_powersave(wpa_s, -1, -1, atoi(param));
  3160. if (os_strcmp(cmd, "disabled") == 0) {
  3161. wpa_s->global->p2p_disabled = atoi(param);
  3162. wpa_printf(MSG_DEBUG, "P2P functionality %s",
  3163. wpa_s->global->p2p_disabled ?
  3164. "disabled" : "enabled");
  3165. if (wpa_s->global->p2p_disabled) {
  3166. wpas_p2p_stop_find(wpa_s);
  3167. os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN);
  3168. p2p_flush(wpa_s->global->p2p);
  3169. }
  3170. return 0;
  3171. }
  3172. if (os_strcmp(cmd, "conc_pref") == 0) {
  3173. if (os_strcmp(param, "sta") == 0)
  3174. wpa_s->global->conc_pref = WPA_CONC_PREF_STA;
  3175. else if (os_strcmp(param, "p2p") == 0)
  3176. wpa_s->global->conc_pref = WPA_CONC_PREF_P2P;
  3177. else {
  3178. wpa_printf(MSG_INFO, "Invalid conc_pref value");
  3179. return -1;
  3180. }
  3181. wpa_printf(MSG_DEBUG, "Single channel concurrency preference: "
  3182. "%s", param);
  3183. return 0;
  3184. }
  3185. if (os_strcmp(cmd, "force_long_sd") == 0) {
  3186. wpa_s->force_long_sd = atoi(param);
  3187. return 0;
  3188. }
  3189. if (os_strcmp(cmd, "peer_filter") == 0) {
  3190. u8 addr[ETH_ALEN];
  3191. if (hwaddr_aton(param, addr))
  3192. return -1;
  3193. p2p_set_peer_filter(wpa_s->global->p2p, addr);
  3194. return 0;
  3195. }
  3196. if (os_strcmp(cmd, "cross_connect") == 0)
  3197. return wpas_p2p_set_cross_connect(wpa_s, atoi(param));
  3198. if (os_strcmp(cmd, "go_apsd") == 0) {
  3199. if (os_strcmp(param, "disable") == 0)
  3200. wpa_s->set_ap_uapsd = 0;
  3201. else {
  3202. wpa_s->set_ap_uapsd = 1;
  3203. wpa_s->ap_uapsd = atoi(param);
  3204. }
  3205. return 0;
  3206. }
  3207. if (os_strcmp(cmd, "client_apsd") == 0) {
  3208. if (os_strcmp(param, "disable") == 0)
  3209. wpa_s->set_sta_uapsd = 0;
  3210. else {
  3211. int be, bk, vi, vo;
  3212. char *pos;
  3213. /* format: BE,BK,VI,VO;max SP Length */
  3214. be = atoi(param);
  3215. pos = os_strchr(param, ',');
  3216. if (pos == NULL)
  3217. return -1;
  3218. pos++;
  3219. bk = atoi(pos);
  3220. pos = os_strchr(pos, ',');
  3221. if (pos == NULL)
  3222. return -1;
  3223. pos++;
  3224. vi = atoi(pos);
  3225. pos = os_strchr(pos, ',');
  3226. if (pos == NULL)
  3227. return -1;
  3228. pos++;
  3229. vo = atoi(pos);
  3230. /* ignore max SP Length for now */
  3231. wpa_s->set_sta_uapsd = 1;
  3232. wpa_s->sta_uapsd = 0;
  3233. if (be)
  3234. wpa_s->sta_uapsd |= BIT(0);
  3235. if (bk)
  3236. wpa_s->sta_uapsd |= BIT(1);
  3237. if (vi)
  3238. wpa_s->sta_uapsd |= BIT(2);
  3239. if (vo)
  3240. wpa_s->sta_uapsd |= BIT(3);
  3241. }
  3242. return 0;
  3243. }
  3244. if (os_strcmp(cmd, "disallow_freq") == 0)
  3245. return p2p_ctrl_disallow_freq(wpa_s, param);
  3246. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Unknown P2P_SET field value '%s'",
  3247. cmd);
  3248. return -1;
  3249. }
  3250. static int p2p_ctrl_presence_req(struct wpa_supplicant *wpa_s, char *cmd)
  3251. {
  3252. char *pos, *pos2;
  3253. unsigned int dur1 = 0, int1 = 0, dur2 = 0, int2 = 0;
  3254. if (cmd[0]) {
  3255. pos = os_strchr(cmd, ' ');
  3256. if (pos == NULL)
  3257. return -1;
  3258. *pos++ = '\0';
  3259. dur1 = atoi(cmd);
  3260. pos2 = os_strchr(pos, ' ');
  3261. if (pos2)
  3262. *pos2++ = '\0';
  3263. int1 = atoi(pos);
  3264. } else
  3265. pos2 = NULL;
  3266. if (pos2) {
  3267. pos = os_strchr(pos2, ' ');
  3268. if (pos == NULL)
  3269. return -1;
  3270. *pos++ = '\0';
  3271. dur2 = atoi(pos2);
  3272. int2 = atoi(pos);
  3273. }
  3274. return wpas_p2p_presence_req(wpa_s, dur1, int1, dur2, int2);
  3275. }
  3276. static int p2p_ctrl_ext_listen(struct wpa_supplicant *wpa_s, char *cmd)
  3277. {
  3278. char *pos;
  3279. unsigned int period = 0, interval = 0;
  3280. if (cmd[0]) {
  3281. pos = os_strchr(cmd, ' ');
  3282. if (pos == NULL)
  3283. return -1;
  3284. *pos++ = '\0';
  3285. period = atoi(cmd);
  3286. interval = atoi(pos);
  3287. }
  3288. return wpas_p2p_ext_listen(wpa_s, period, interval);
  3289. }
  3290. #endif /* CONFIG_P2P */
  3291. #ifdef CONFIG_INTERWORKING
  3292. static int ctrl_interworking_connect(struct wpa_supplicant *wpa_s, char *dst)
  3293. {
  3294. u8 bssid[ETH_ALEN];
  3295. struct wpa_bss *bss;
  3296. if (hwaddr_aton(dst, bssid)) {
  3297. wpa_printf(MSG_DEBUG, "Invalid BSSID '%s'", dst);
  3298. return -1;
  3299. }
  3300. bss = wpa_bss_get_bssid(wpa_s, bssid);
  3301. if (bss == NULL) {
  3302. wpa_printf(MSG_DEBUG, "Could not find BSS " MACSTR,
  3303. MAC2STR(bssid));
  3304. return -1;
  3305. }
  3306. return interworking_connect(wpa_s, bss);
  3307. }
  3308. static int get_anqp(struct wpa_supplicant *wpa_s, char *dst)
  3309. {
  3310. u8 dst_addr[ETH_ALEN];
  3311. int used;
  3312. char *pos;
  3313. #define MAX_ANQP_INFO_ID 100
  3314. u16 id[MAX_ANQP_INFO_ID];
  3315. size_t num_id = 0;
  3316. used = hwaddr_aton2(dst, dst_addr);
  3317. if (used < 0)
  3318. return -1;
  3319. pos = dst + used;
  3320. while (num_id < MAX_ANQP_INFO_ID) {
  3321. id[num_id] = atoi(pos);
  3322. if (id[num_id])
  3323. num_id++;
  3324. pos = os_strchr(pos + 1, ',');
  3325. if (pos == NULL)
  3326. break;
  3327. pos++;
  3328. }
  3329. if (num_id == 0)
  3330. return -1;
  3331. return anqp_send_req(wpa_s, dst_addr, id, num_id);
  3332. }
  3333. #endif /* CONFIG_INTERWORKING */
  3334. #ifdef CONFIG_HS20
  3335. static int get_hs20_anqp(struct wpa_supplicant *wpa_s, char *dst)
  3336. {
  3337. u8 dst_addr[ETH_ALEN];
  3338. int used;
  3339. char *pos;
  3340. u32 subtypes = 0;
  3341. used = hwaddr_aton2(dst, dst_addr);
  3342. if (used < 0)
  3343. return -1;
  3344. pos = dst + used;
  3345. for (;;) {
  3346. int num = atoi(pos);
  3347. if (num <= 0 || num > 31)
  3348. return -1;
  3349. subtypes |= BIT(num);
  3350. pos = os_strchr(pos + 1, ',');
  3351. if (pos == NULL)
  3352. break;
  3353. pos++;
  3354. }
  3355. if (subtypes == 0)
  3356. return -1;
  3357. return hs20_anqp_send_req(wpa_s, dst_addr, subtypes, NULL, 0);
  3358. }
  3359. static int hs20_nai_home_realm_list(struct wpa_supplicant *wpa_s,
  3360. const u8 *addr, const char *realm)
  3361. {
  3362. u8 *buf;
  3363. size_t rlen, len;
  3364. int ret;
  3365. rlen = os_strlen(realm);
  3366. len = 3 + rlen;
  3367. buf = os_malloc(len);
  3368. if (buf == NULL)
  3369. return -1;
  3370. buf[0] = 1; /* NAI Home Realm Count */
  3371. buf[1] = 0; /* Formatted in accordance with RFC 4282 */
  3372. buf[2] = rlen;
  3373. os_memcpy(buf + 3, realm, rlen);
  3374. ret = hs20_anqp_send_req(wpa_s, addr,
  3375. BIT(HS20_STYPE_NAI_HOME_REALM_QUERY),
  3376. buf, len);
  3377. os_free(buf);
  3378. return ret;
  3379. }
  3380. static int hs20_get_nai_home_realm_list(struct wpa_supplicant *wpa_s,
  3381. char *dst)
  3382. {
  3383. struct wpa_cred *cred = wpa_s->conf->cred;
  3384. u8 dst_addr[ETH_ALEN];
  3385. int used;
  3386. u8 *buf;
  3387. size_t len;
  3388. int ret;
  3389. used = hwaddr_aton2(dst, dst_addr);
  3390. if (used < 0)
  3391. return -1;
  3392. while (dst[used] == ' ')
  3393. used++;
  3394. if (os_strncmp(dst + used, "realm=", 6) == 0)
  3395. return hs20_nai_home_realm_list(wpa_s, dst_addr,
  3396. dst + used + 6);
  3397. len = os_strlen(dst + used);
  3398. if (len == 0 && cred && cred->realm)
  3399. return hs20_nai_home_realm_list(wpa_s, dst_addr, cred->realm);
  3400. if (len % 1)
  3401. return -1;
  3402. len /= 2;
  3403. buf = os_malloc(len);
  3404. if (buf == NULL)
  3405. return -1;
  3406. if (hexstr2bin(dst + used, buf, len) < 0) {
  3407. os_free(buf);
  3408. return -1;
  3409. }
  3410. ret = hs20_anqp_send_req(wpa_s, dst_addr,
  3411. BIT(HS20_STYPE_NAI_HOME_REALM_QUERY),
  3412. buf, len);
  3413. os_free(buf);
  3414. return ret;
  3415. }
  3416. #endif /* CONFIG_HS20 */
  3417. static int wpa_supplicant_ctrl_iface_sta_autoconnect(
  3418. struct wpa_supplicant *wpa_s, char *cmd)
  3419. {
  3420. wpa_s->auto_reconnect_disabled = atoi(cmd) == 0 ? 1 : 0;
  3421. return 0;
  3422. }
  3423. #ifdef CONFIG_AUTOSCAN
  3424. static int wpa_supplicant_ctrl_iface_autoscan(struct wpa_supplicant *wpa_s,
  3425. char *cmd)
  3426. {
  3427. enum wpa_states state = wpa_s->wpa_state;
  3428. char *new_params = NULL;
  3429. if (os_strlen(cmd) > 0) {
  3430. new_params = os_strdup(cmd);
  3431. if (new_params == NULL)
  3432. return -1;
  3433. }
  3434. os_free(wpa_s->conf->autoscan);
  3435. wpa_s->conf->autoscan = new_params;
  3436. if (wpa_s->conf->autoscan == NULL)
  3437. autoscan_deinit(wpa_s);
  3438. else if (state == WPA_DISCONNECTED || state == WPA_INACTIVE)
  3439. autoscan_init(wpa_s, 1);
  3440. else if (state == WPA_SCANNING)
  3441. wpa_supplicant_reinit_autoscan(wpa_s);
  3442. return 0;
  3443. }
  3444. #endif /* CONFIG_AUTOSCAN */
  3445. static int wpa_supplicant_signal_poll(struct wpa_supplicant *wpa_s, char *buf,
  3446. size_t buflen)
  3447. {
  3448. struct wpa_signal_info si;
  3449. int ret;
  3450. ret = wpa_drv_signal_poll(wpa_s, &si);
  3451. if (ret)
  3452. return -1;
  3453. ret = os_snprintf(buf, buflen, "RSSI=%d\nLINKSPEED=%d\n"
  3454. "NOISE=%d\nFREQUENCY=%u\n",
  3455. si.current_signal, si.current_txrate / 1000,
  3456. si.current_noise, si.frequency);
  3457. if (ret < 0 || (unsigned int) ret > buflen)
  3458. return -1;
  3459. return ret;
  3460. }
  3461. char * wpa_supplicant_ctrl_iface_process(struct wpa_supplicant *wpa_s,
  3462. char *buf, size_t *resp_len)
  3463. {
  3464. char *reply;
  3465. const int reply_size = 4096;
  3466. int ctrl_rsp = 0;
  3467. int reply_len;
  3468. if (os_strncmp(buf, WPA_CTRL_RSP, os_strlen(WPA_CTRL_RSP)) == 0 ||
  3469. os_strncmp(buf, "SET_NETWORK ", 12) == 0) {
  3470. wpa_hexdump_ascii_key(MSG_DEBUG, "RX ctrl_iface",
  3471. (const u8 *) buf, os_strlen(buf));
  3472. } else {
  3473. int level = MSG_DEBUG;
  3474. if (os_strcmp(buf, "PING") == 0)
  3475. level = MSG_EXCESSIVE;
  3476. wpa_hexdump_ascii(level, "RX ctrl_iface",
  3477. (const u8 *) buf, os_strlen(buf));
  3478. }
  3479. reply = os_malloc(reply_size);
  3480. if (reply == NULL) {
  3481. *resp_len = 1;
  3482. return NULL;
  3483. }
  3484. os_memcpy(reply, "OK\n", 3);
  3485. reply_len = 3;
  3486. if (os_strcmp(buf, "PING") == 0) {
  3487. os_memcpy(reply, "PONG\n", 5);
  3488. reply_len = 5;
  3489. } else if (os_strcmp(buf, "IFNAME") == 0) {
  3490. reply_len = os_strlen(wpa_s->ifname);
  3491. os_memcpy(reply, wpa_s->ifname, reply_len);
  3492. } else if (os_strncmp(buf, "RELOG", 5) == 0) {
  3493. if (wpa_debug_reopen_file() < 0)
  3494. reply_len = -1;
  3495. } else if (os_strncmp(buf, "NOTE ", 5) == 0) {
  3496. wpa_printf(MSG_INFO, "NOTE: %s", buf + 5);
  3497. } else if (os_strcmp(buf, "MIB") == 0) {
  3498. reply_len = wpa_sm_get_mib(wpa_s->wpa, reply, reply_size);
  3499. if (reply_len >= 0) {
  3500. int res;
  3501. res = eapol_sm_get_mib(wpa_s->eapol, reply + reply_len,
  3502. reply_size - reply_len);
  3503. if (res < 0)
  3504. reply_len = -1;
  3505. else
  3506. reply_len += res;
  3507. }
  3508. } else if (os_strncmp(buf, "STATUS", 6) == 0) {
  3509. reply_len = wpa_supplicant_ctrl_iface_status(
  3510. wpa_s, buf + 6, reply, reply_size);
  3511. } else if (os_strcmp(buf, "PMKSA") == 0) {
  3512. reply_len = wpa_sm_pmksa_cache_list(wpa_s->wpa, reply,
  3513. reply_size);
  3514. } else if (os_strncmp(buf, "SET ", 4) == 0) {
  3515. if (wpa_supplicant_ctrl_iface_set(wpa_s, buf + 4))
  3516. reply_len = -1;
  3517. } else if (os_strncmp(buf, "GET ", 4) == 0) {
  3518. reply_len = wpa_supplicant_ctrl_iface_get(wpa_s, buf + 4,
  3519. reply, reply_size);
  3520. } else if (os_strcmp(buf, "LOGON") == 0) {
  3521. eapol_sm_notify_logoff(wpa_s->eapol, FALSE);
  3522. } else if (os_strcmp(buf, "LOGOFF") == 0) {
  3523. eapol_sm_notify_logoff(wpa_s->eapol, TRUE);
  3524. } else if (os_strcmp(buf, "REASSOCIATE") == 0) {
  3525. wpa_s->normal_scans = 0;
  3526. wpa_supplicant_reinit_autoscan(wpa_s);
  3527. if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED)
  3528. reply_len = -1;
  3529. else {
  3530. wpa_s->disconnected = 0;
  3531. wpa_s->reassociate = 1;
  3532. wpa_supplicant_req_scan(wpa_s, 0, 0);
  3533. }
  3534. } else if (os_strcmp(buf, "RECONNECT") == 0) {
  3535. wpa_s->normal_scans = 0;
  3536. wpa_supplicant_reinit_autoscan(wpa_s);
  3537. if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED)
  3538. reply_len = -1;
  3539. else if (wpa_s->disconnected) {
  3540. wpa_s->disconnected = 0;
  3541. wpa_s->reassociate = 1;
  3542. wpa_supplicant_req_scan(wpa_s, 0, 0);
  3543. }
  3544. #ifdef IEEE8021X_EAPOL
  3545. } else if (os_strncmp(buf, "PREAUTH ", 8) == 0) {
  3546. if (wpa_supplicant_ctrl_iface_preauth(wpa_s, buf + 8))
  3547. reply_len = -1;
  3548. #endif /* IEEE8021X_EAPOL */
  3549. #ifdef CONFIG_PEERKEY
  3550. } else if (os_strncmp(buf, "STKSTART ", 9) == 0) {
  3551. if (wpa_supplicant_ctrl_iface_stkstart(wpa_s, buf + 9))
  3552. reply_len = -1;
  3553. #endif /* CONFIG_PEERKEY */
  3554. #ifdef CONFIG_IEEE80211R
  3555. } else if (os_strncmp(buf, "FT_DS ", 6) == 0) {
  3556. if (wpa_supplicant_ctrl_iface_ft_ds(wpa_s, buf + 6))
  3557. reply_len = -1;
  3558. #endif /* CONFIG_IEEE80211R */
  3559. #ifdef CONFIG_WPS
  3560. } else if (os_strcmp(buf, "WPS_PBC") == 0) {
  3561. int res = wpa_supplicant_ctrl_iface_wps_pbc(wpa_s, NULL);
  3562. if (res == -2) {
  3563. os_memcpy(reply, "FAIL-PBC-OVERLAP\n", 17);
  3564. reply_len = 17;
  3565. } else if (res)
  3566. reply_len = -1;
  3567. } else if (os_strncmp(buf, "WPS_PBC ", 8) == 0) {
  3568. int res = wpa_supplicant_ctrl_iface_wps_pbc(wpa_s, buf + 8);
  3569. if (res == -2) {
  3570. os_memcpy(reply, "FAIL-PBC-OVERLAP\n", 17);
  3571. reply_len = 17;
  3572. } else if (res)
  3573. reply_len = -1;
  3574. } else if (os_strncmp(buf, "WPS_PIN ", 8) == 0) {
  3575. reply_len = wpa_supplicant_ctrl_iface_wps_pin(wpa_s, buf + 8,
  3576. reply,
  3577. reply_size);
  3578. } else if (os_strncmp(buf, "WPS_CHECK_PIN ", 14) == 0) {
  3579. reply_len = wpa_supplicant_ctrl_iface_wps_check_pin(
  3580. wpa_s, buf + 14, reply, reply_size);
  3581. } else if (os_strcmp(buf, "WPS_CANCEL") == 0) {
  3582. if (wpas_wps_cancel(wpa_s))
  3583. reply_len = -1;
  3584. #ifdef CONFIG_WPS_OOB
  3585. } else if (os_strncmp(buf, "WPS_OOB ", 8) == 0) {
  3586. if (wpa_supplicant_ctrl_iface_wps_oob(wpa_s, buf + 8))
  3587. reply_len = -1;
  3588. #endif /* CONFIG_WPS_OOB */
  3589. #ifdef CONFIG_WPS_NFC
  3590. } else if (os_strcmp(buf, "WPS_NFC") == 0) {
  3591. if (wpa_supplicant_ctrl_iface_wps_nfc(wpa_s, NULL))
  3592. reply_len = -1;
  3593. } else if (os_strncmp(buf, "WPS_NFC ", 8) == 0) {
  3594. if (wpa_supplicant_ctrl_iface_wps_nfc(wpa_s, buf + 8))
  3595. reply_len = -1;
  3596. } else if (os_strncmp(buf, "WPS_NFC_TOKEN ", 14) == 0) {
  3597. reply_len = wpa_supplicant_ctrl_iface_wps_nfc_token(
  3598. wpa_s, buf + 14, reply, reply_size);
  3599. } else if (os_strncmp(buf, "WPS_NFC_TAG_READ ", 17) == 0) {
  3600. if (wpa_supplicant_ctrl_iface_wps_nfc_tag_read(wpa_s,
  3601. buf + 17))
  3602. reply_len = -1;
  3603. #endif /* CONFIG_WPS_NFC */
  3604. } else if (os_strncmp(buf, "WPS_REG ", 8) == 0) {
  3605. if (wpa_supplicant_ctrl_iface_wps_reg(wpa_s, buf + 8))
  3606. reply_len = -1;
  3607. #ifdef CONFIG_AP
  3608. } else if (os_strncmp(buf, "WPS_AP_PIN ", 11) == 0) {
  3609. reply_len = wpa_supplicant_ctrl_iface_wps_ap_pin(
  3610. wpa_s, buf + 11, reply, reply_size);
  3611. #endif /* CONFIG_AP */
  3612. #ifdef CONFIG_WPS_ER
  3613. } else if (os_strcmp(buf, "WPS_ER_START") == 0) {
  3614. if (wpas_wps_er_start(wpa_s, NULL))
  3615. reply_len = -1;
  3616. } else if (os_strncmp(buf, "WPS_ER_START ", 13) == 0) {
  3617. if (wpas_wps_er_start(wpa_s, buf + 13))
  3618. reply_len = -1;
  3619. } else if (os_strcmp(buf, "WPS_ER_STOP") == 0) {
  3620. if (wpas_wps_er_stop(wpa_s))
  3621. reply_len = -1;
  3622. } else if (os_strncmp(buf, "WPS_ER_PIN ", 11) == 0) {
  3623. if (wpa_supplicant_ctrl_iface_wps_er_pin(wpa_s, buf + 11))
  3624. reply_len = -1;
  3625. } else if (os_strncmp(buf, "WPS_ER_PBC ", 11) == 0) {
  3626. int ret = wpas_wps_er_pbc(wpa_s, buf + 11);
  3627. if (ret == -2) {
  3628. os_memcpy(reply, "FAIL-PBC-OVERLAP\n", 17);
  3629. reply_len = 17;
  3630. } else if (ret == -3) {
  3631. os_memcpy(reply, "FAIL-UNKNOWN-UUID\n", 18);
  3632. reply_len = 18;
  3633. } else if (ret == -4) {
  3634. os_memcpy(reply, "FAIL-NO-AP-SETTINGS\n", 20);
  3635. reply_len = 20;
  3636. } else if (ret)
  3637. reply_len = -1;
  3638. } else if (os_strncmp(buf, "WPS_ER_LEARN ", 13) == 0) {
  3639. if (wpa_supplicant_ctrl_iface_wps_er_learn(wpa_s, buf + 13))
  3640. reply_len = -1;
  3641. } else if (os_strncmp(buf, "WPS_ER_SET_CONFIG ", 18) == 0) {
  3642. if (wpa_supplicant_ctrl_iface_wps_er_set_config(wpa_s,
  3643. buf + 18))
  3644. reply_len = -1;
  3645. } else if (os_strncmp(buf, "WPS_ER_CONFIG ", 14) == 0) {
  3646. if (wpa_supplicant_ctrl_iface_wps_er_config(wpa_s, buf + 14))
  3647. reply_len = -1;
  3648. #ifdef CONFIG_WPS_NFC
  3649. } else if (os_strncmp(buf, "WPS_ER_NFC_CONFIG_TOKEN ", 24) == 0) {
  3650. reply_len = wpa_supplicant_ctrl_iface_wps_er_nfc_config_token(
  3651. wpa_s, buf + 24, reply, reply_size);
  3652. #endif /* CONFIG_WPS_NFC */
  3653. #endif /* CONFIG_WPS_ER */
  3654. #endif /* CONFIG_WPS */
  3655. #ifdef CONFIG_IBSS_RSN
  3656. } else if (os_strncmp(buf, "IBSS_RSN ", 9) == 0) {
  3657. if (wpa_supplicant_ctrl_iface_ibss_rsn(wpa_s, buf + 9))
  3658. reply_len = -1;
  3659. #endif /* CONFIG_IBSS_RSN */
  3660. #ifdef CONFIG_P2P
  3661. } else if (os_strncmp(buf, "P2P_FIND ", 9) == 0) {
  3662. if (p2p_ctrl_find(wpa_s, buf + 9))
  3663. reply_len = -1;
  3664. } else if (os_strcmp(buf, "P2P_FIND") == 0) {
  3665. if (p2p_ctrl_find(wpa_s, ""))
  3666. reply_len = -1;
  3667. } else if (os_strcmp(buf, "P2P_STOP_FIND") == 0) {
  3668. wpas_p2p_stop_find(wpa_s);
  3669. } else if (os_strncmp(buf, "P2P_CONNECT ", 12) == 0) {
  3670. reply_len = p2p_ctrl_connect(wpa_s, buf + 12, reply,
  3671. reply_size);
  3672. } else if (os_strncmp(buf, "P2P_LISTEN ", 11) == 0) {
  3673. if (p2p_ctrl_listen(wpa_s, buf + 11))
  3674. reply_len = -1;
  3675. } else if (os_strcmp(buf, "P2P_LISTEN") == 0) {
  3676. if (p2p_ctrl_listen(wpa_s, ""))
  3677. reply_len = -1;
  3678. } else if (os_strncmp(buf, "P2P_GROUP_REMOVE ", 17) == 0) {
  3679. if (wpas_p2p_group_remove(wpa_s, buf + 17))
  3680. reply_len = -1;
  3681. } else if (os_strcmp(buf, "P2P_GROUP_ADD") == 0) {
  3682. if (wpas_p2p_group_add(wpa_s, 0, 0, 0))
  3683. reply_len = -1;
  3684. } else if (os_strncmp(buf, "P2P_GROUP_ADD ", 14) == 0) {
  3685. if (p2p_ctrl_group_add(wpa_s, buf + 14))
  3686. reply_len = -1;
  3687. } else if (os_strncmp(buf, "P2P_PROV_DISC ", 14) == 0) {
  3688. if (p2p_ctrl_prov_disc(wpa_s, buf + 14))
  3689. reply_len = -1;
  3690. } else if (os_strcmp(buf, "P2P_GET_PASSPHRASE") == 0) {
  3691. reply_len = p2p_get_passphrase(wpa_s, reply, reply_size);
  3692. } else if (os_strncmp(buf, "P2P_SERV_DISC_REQ ", 18) == 0) {
  3693. reply_len = p2p_ctrl_serv_disc_req(wpa_s, buf + 18, reply,
  3694. reply_size);
  3695. } else if (os_strncmp(buf, "P2P_SERV_DISC_CANCEL_REQ ", 25) == 0) {
  3696. if (p2p_ctrl_serv_disc_cancel_req(wpa_s, buf + 25) < 0)
  3697. reply_len = -1;
  3698. } else if (os_strncmp(buf, "P2P_SERV_DISC_RESP ", 19) == 0) {
  3699. if (p2p_ctrl_serv_disc_resp(wpa_s, buf + 19) < 0)
  3700. reply_len = -1;
  3701. } else if (os_strcmp(buf, "P2P_SERVICE_UPDATE") == 0) {
  3702. wpas_p2p_sd_service_update(wpa_s);
  3703. } else if (os_strncmp(buf, "P2P_SERV_DISC_EXTERNAL ", 23) == 0) {
  3704. if (p2p_ctrl_serv_disc_external(wpa_s, buf + 23) < 0)
  3705. reply_len = -1;
  3706. } else if (os_strcmp(buf, "P2P_SERVICE_FLUSH") == 0) {
  3707. wpas_p2p_service_flush(wpa_s);
  3708. } else if (os_strncmp(buf, "P2P_SERVICE_ADD ", 16) == 0) {
  3709. if (p2p_ctrl_service_add(wpa_s, buf + 16) < 0)
  3710. reply_len = -1;
  3711. } else if (os_strncmp(buf, "P2P_SERVICE_DEL ", 16) == 0) {
  3712. if (p2p_ctrl_service_del(wpa_s, buf + 16) < 0)
  3713. reply_len = -1;
  3714. } else if (os_strncmp(buf, "P2P_REJECT ", 11) == 0) {
  3715. if (p2p_ctrl_reject(wpa_s, buf + 11) < 0)
  3716. reply_len = -1;
  3717. } else if (os_strncmp(buf, "P2P_INVITE ", 11) == 0) {
  3718. if (p2p_ctrl_invite(wpa_s, buf + 11) < 0)
  3719. reply_len = -1;
  3720. } else if (os_strncmp(buf, "P2P_PEER ", 9) == 0) {
  3721. reply_len = p2p_ctrl_peer(wpa_s, buf + 9, reply,
  3722. reply_size);
  3723. } else if (os_strncmp(buf, "P2P_SET ", 8) == 0) {
  3724. if (p2p_ctrl_set(wpa_s, buf + 8) < 0)
  3725. reply_len = -1;
  3726. } else if (os_strcmp(buf, "P2P_FLUSH") == 0) {
  3727. os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN);
  3728. wpa_s->force_long_sd = 0;
  3729. if (wpa_s->global->p2p)
  3730. p2p_flush(wpa_s->global->p2p);
  3731. } else if (os_strncmp(buf, "P2P_UNAUTHORIZE ", 16) == 0) {
  3732. if (wpas_p2p_unauthorize(wpa_s, buf + 16) < 0)
  3733. reply_len = -1;
  3734. } else if (os_strcmp(buf, "P2P_CANCEL") == 0) {
  3735. if (wpas_p2p_cancel(wpa_s))
  3736. reply_len = -1;
  3737. } else if (os_strncmp(buf, "P2P_PRESENCE_REQ ", 17) == 0) {
  3738. if (p2p_ctrl_presence_req(wpa_s, buf + 17) < 0)
  3739. reply_len = -1;
  3740. } else if (os_strcmp(buf, "P2P_PRESENCE_REQ") == 0) {
  3741. if (p2p_ctrl_presence_req(wpa_s, "") < 0)
  3742. reply_len = -1;
  3743. } else if (os_strncmp(buf, "P2P_EXT_LISTEN ", 15) == 0) {
  3744. if (p2p_ctrl_ext_listen(wpa_s, buf + 15) < 0)
  3745. reply_len = -1;
  3746. } else if (os_strcmp(buf, "P2P_EXT_LISTEN") == 0) {
  3747. if (p2p_ctrl_ext_listen(wpa_s, "") < 0)
  3748. reply_len = -1;
  3749. #endif /* CONFIG_P2P */
  3750. #ifdef CONFIG_INTERWORKING
  3751. } else if (os_strcmp(buf, "FETCH_ANQP") == 0) {
  3752. if (interworking_fetch_anqp(wpa_s) < 0)
  3753. reply_len = -1;
  3754. } else if (os_strcmp(buf, "STOP_FETCH_ANQP") == 0) {
  3755. interworking_stop_fetch_anqp(wpa_s);
  3756. } else if (os_strncmp(buf, "INTERWORKING_SELECT", 19) == 0) {
  3757. if (interworking_select(wpa_s, os_strstr(buf + 19, "auto") !=
  3758. NULL) < 0)
  3759. reply_len = -1;
  3760. } else if (os_strncmp(buf, "INTERWORKING_CONNECT ", 21) == 0) {
  3761. if (ctrl_interworking_connect(wpa_s, buf + 21) < 0)
  3762. reply_len = -1;
  3763. } else if (os_strncmp(buf, "ANQP_GET ", 9) == 0) {
  3764. if (get_anqp(wpa_s, buf + 9) < 0)
  3765. reply_len = -1;
  3766. #endif /* CONFIG_INTERWORKING */
  3767. #ifdef CONFIG_HS20
  3768. } else if (os_strncmp(buf, "HS20_ANQP_GET ", 14) == 0) {
  3769. if (get_hs20_anqp(wpa_s, buf + 14) < 0)
  3770. reply_len = -1;
  3771. } else if (os_strncmp(buf, "HS20_GET_NAI_HOME_REALM_LIST ", 29) == 0) {
  3772. if (hs20_get_nai_home_realm_list(wpa_s, buf + 29) < 0)
  3773. reply_len = -1;
  3774. #endif /* CONFIG_HS20 */
  3775. } else if (os_strncmp(buf, WPA_CTRL_RSP, os_strlen(WPA_CTRL_RSP)) == 0)
  3776. {
  3777. if (wpa_supplicant_ctrl_iface_ctrl_rsp(
  3778. wpa_s, buf + os_strlen(WPA_CTRL_RSP)))
  3779. reply_len = -1;
  3780. else
  3781. ctrl_rsp = 1;
  3782. } else if (os_strcmp(buf, "RECONFIGURE") == 0) {
  3783. if (wpa_supplicant_reload_configuration(wpa_s))
  3784. reply_len = -1;
  3785. } else if (os_strcmp(buf, "TERMINATE") == 0) {
  3786. wpa_supplicant_terminate_proc(wpa_s->global);
  3787. } else if (os_strncmp(buf, "BSSID ", 6) == 0) {
  3788. if (wpa_supplicant_ctrl_iface_bssid(wpa_s, buf + 6))
  3789. reply_len = -1;
  3790. } else if (os_strncmp(buf, "BLACKLIST", 9) == 0) {
  3791. reply_len = wpa_supplicant_ctrl_iface_blacklist(
  3792. wpa_s, buf + 9, reply, reply_size);
  3793. } else if (os_strncmp(buf, "LOG_LEVEL", 9) == 0) {
  3794. reply_len = wpa_supplicant_ctrl_iface_log_level(
  3795. wpa_s, buf + 9, reply, reply_size);
  3796. } else if (os_strcmp(buf, "LIST_NETWORKS") == 0) {
  3797. reply_len = wpa_supplicant_ctrl_iface_list_networks(
  3798. wpa_s, reply, reply_size);
  3799. } else if (os_strcmp(buf, "DISCONNECT") == 0) {
  3800. #ifdef CONFIG_SME
  3801. wpa_s->sme.prev_bssid_set = 0;
  3802. #endif /* CONFIG_SME */
  3803. wpa_s->reassociate = 0;
  3804. wpa_s->disconnected = 1;
  3805. wpa_supplicant_cancel_sched_scan(wpa_s);
  3806. wpa_supplicant_cancel_scan(wpa_s);
  3807. wpa_supplicant_deauthenticate(wpa_s,
  3808. WLAN_REASON_DEAUTH_LEAVING);
  3809. } else if (os_strcmp(buf, "SCAN") == 0) {
  3810. if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED)
  3811. reply_len = -1;
  3812. else {
  3813. if (!wpa_s->scanning &&
  3814. ((wpa_s->wpa_state <= WPA_SCANNING) ||
  3815. (wpa_s->wpa_state == WPA_COMPLETED))) {
  3816. wpa_s->normal_scans = 0;
  3817. wpa_s->scan_req = 2;
  3818. wpa_supplicant_req_scan(wpa_s, 0, 0);
  3819. } else if (wpa_s->sched_scanning) {
  3820. wpa_printf(MSG_DEBUG, "Stop ongoing "
  3821. "sched_scan to allow requested "
  3822. "full scan to proceed");
  3823. wpa_supplicant_cancel_sched_scan(wpa_s);
  3824. wpa_s->scan_req = 2;
  3825. wpa_supplicant_req_scan(wpa_s, 0, 0);
  3826. } else {
  3827. wpa_printf(MSG_DEBUG, "Ongoing scan action - "
  3828. "reject new request");
  3829. reply_len = os_snprintf(reply, reply_size,
  3830. "FAIL-BUSY\n");
  3831. }
  3832. }
  3833. } else if (os_strcmp(buf, "SCAN_RESULTS") == 0) {
  3834. reply_len = wpa_supplicant_ctrl_iface_scan_results(
  3835. wpa_s, reply, reply_size);
  3836. } else if (os_strncmp(buf, "SELECT_NETWORK ", 15) == 0) {
  3837. if (wpa_supplicant_ctrl_iface_select_network(wpa_s, buf + 15))
  3838. reply_len = -1;
  3839. } else if (os_strncmp(buf, "ENABLE_NETWORK ", 15) == 0) {
  3840. if (wpa_supplicant_ctrl_iface_enable_network(wpa_s, buf + 15))
  3841. reply_len = -1;
  3842. } else if (os_strncmp(buf, "DISABLE_NETWORK ", 16) == 0) {
  3843. if (wpa_supplicant_ctrl_iface_disable_network(wpa_s, buf + 16))
  3844. reply_len = -1;
  3845. } else if (os_strcmp(buf, "ADD_NETWORK") == 0) {
  3846. reply_len = wpa_supplicant_ctrl_iface_add_network(
  3847. wpa_s, reply, reply_size);
  3848. } else if (os_strncmp(buf, "REMOVE_NETWORK ", 15) == 0) {
  3849. if (wpa_supplicant_ctrl_iface_remove_network(wpa_s, buf + 15))
  3850. reply_len = -1;
  3851. } else if (os_strncmp(buf, "SET_NETWORK ", 12) == 0) {
  3852. if (wpa_supplicant_ctrl_iface_set_network(wpa_s, buf + 12))
  3853. reply_len = -1;
  3854. } else if (os_strncmp(buf, "GET_NETWORK ", 12) == 0) {
  3855. reply_len = wpa_supplicant_ctrl_iface_get_network(
  3856. wpa_s, buf + 12, reply, reply_size);
  3857. } else if (os_strcmp(buf, "LIST_CREDS") == 0) {
  3858. reply_len = wpa_supplicant_ctrl_iface_list_creds(
  3859. wpa_s, reply, reply_size);
  3860. } else if (os_strcmp(buf, "ADD_CRED") == 0) {
  3861. reply_len = wpa_supplicant_ctrl_iface_add_cred(
  3862. wpa_s, reply, reply_size);
  3863. } else if (os_strncmp(buf, "REMOVE_CRED ", 12) == 0) {
  3864. if (wpa_supplicant_ctrl_iface_remove_cred(wpa_s, buf + 12))
  3865. reply_len = -1;
  3866. } else if (os_strncmp(buf, "SET_CRED ", 9) == 0) {
  3867. if (wpa_supplicant_ctrl_iface_set_cred(wpa_s, buf + 9))
  3868. reply_len = -1;
  3869. #ifndef CONFIG_NO_CONFIG_WRITE
  3870. } else if (os_strcmp(buf, "SAVE_CONFIG") == 0) {
  3871. if (wpa_supplicant_ctrl_iface_save_config(wpa_s))
  3872. reply_len = -1;
  3873. #endif /* CONFIG_NO_CONFIG_WRITE */
  3874. } else if (os_strncmp(buf, "GET_CAPABILITY ", 15) == 0) {
  3875. reply_len = wpa_supplicant_ctrl_iface_get_capability(
  3876. wpa_s, buf + 15, reply, reply_size);
  3877. } else if (os_strncmp(buf, "AP_SCAN ", 8) == 0) {
  3878. if (wpa_supplicant_ctrl_iface_ap_scan(wpa_s, buf + 8))
  3879. reply_len = -1;
  3880. } else if (os_strncmp(buf, "SCAN_INTERVAL ", 14) == 0) {
  3881. if (wpa_supplicant_ctrl_iface_scan_interval(wpa_s, buf + 14))
  3882. reply_len = -1;
  3883. } else if (os_strcmp(buf, "INTERFACE_LIST") == 0) {
  3884. reply_len = wpa_supplicant_global_iface_list(
  3885. wpa_s->global, reply, reply_size);
  3886. } else if (os_strcmp(buf, "INTERFACES") == 0) {
  3887. reply_len = wpa_supplicant_global_iface_interfaces(
  3888. wpa_s->global, reply, reply_size);
  3889. } else if (os_strncmp(buf, "BSS ", 4) == 0) {
  3890. reply_len = wpa_supplicant_ctrl_iface_bss(
  3891. wpa_s, buf + 4, reply, reply_size);
  3892. #ifdef CONFIG_AP
  3893. } else if (os_strcmp(buf, "STA-FIRST") == 0) {
  3894. reply_len = ap_ctrl_iface_sta_first(wpa_s, reply, reply_size);
  3895. } else if (os_strncmp(buf, "STA ", 4) == 0) {
  3896. reply_len = ap_ctrl_iface_sta(wpa_s, buf + 4, reply,
  3897. reply_size);
  3898. } else if (os_strncmp(buf, "STA-NEXT ", 9) == 0) {
  3899. reply_len = ap_ctrl_iface_sta_next(wpa_s, buf + 9, reply,
  3900. reply_size);
  3901. } else if (os_strncmp(buf, "DEAUTHENTICATE ", 15) == 0) {
  3902. if (ap_ctrl_iface_sta_deauthenticate(wpa_s, buf + 15))
  3903. reply_len = -1;
  3904. } else if (os_strncmp(buf, "DISASSOCIATE ", 13) == 0) {
  3905. if (ap_ctrl_iface_sta_disassociate(wpa_s, buf + 13))
  3906. reply_len = -1;
  3907. #endif /* CONFIG_AP */
  3908. } else if (os_strcmp(buf, "SUSPEND") == 0) {
  3909. wpas_notify_suspend(wpa_s->global);
  3910. } else if (os_strcmp(buf, "RESUME") == 0) {
  3911. wpas_notify_resume(wpa_s->global);
  3912. } else if (os_strcmp(buf, "DROP_SA") == 0) {
  3913. wpa_supplicant_ctrl_iface_drop_sa(wpa_s);
  3914. } else if (os_strncmp(buf, "ROAM ", 5) == 0) {
  3915. if (wpa_supplicant_ctrl_iface_roam(wpa_s, buf + 5))
  3916. reply_len = -1;
  3917. } else if (os_strncmp(buf, "STA_AUTOCONNECT ", 16) == 0) {
  3918. if (wpa_supplicant_ctrl_iface_sta_autoconnect(wpa_s, buf + 16))
  3919. reply_len = -1;
  3920. } else if (os_strncmp(buf, "BSS_EXPIRE_AGE ", 15) == 0) {
  3921. if (wpa_supplicant_ctrl_iface_bss_expire_age(wpa_s, buf + 15))
  3922. reply_len = -1;
  3923. } else if (os_strncmp(buf, "BSS_EXPIRE_COUNT ", 17) == 0) {
  3924. if (wpa_supplicant_ctrl_iface_bss_expire_count(wpa_s,
  3925. buf + 17))
  3926. reply_len = -1;
  3927. #ifdef CONFIG_TDLS
  3928. } else if (os_strncmp(buf, "TDLS_DISCOVER ", 14) == 0) {
  3929. if (wpa_supplicant_ctrl_iface_tdls_discover(wpa_s, buf + 14))
  3930. reply_len = -1;
  3931. } else if (os_strncmp(buf, "TDLS_SETUP ", 11) == 0) {
  3932. if (wpa_supplicant_ctrl_iface_tdls_setup(wpa_s, buf + 11))
  3933. reply_len = -1;
  3934. } else if (os_strncmp(buf, "TDLS_TEARDOWN ", 14) == 0) {
  3935. if (wpa_supplicant_ctrl_iface_tdls_teardown(wpa_s, buf + 14))
  3936. reply_len = -1;
  3937. #endif /* CONFIG_TDLS */
  3938. } else if (os_strncmp(buf, "SIGNAL_POLL", 11) == 0) {
  3939. reply_len = wpa_supplicant_signal_poll(wpa_s, reply,
  3940. reply_size);
  3941. #ifdef CONFIG_AUTOSCAN
  3942. } else if (os_strncmp(buf, "AUTOSCAN ", 9) == 0) {
  3943. if (wpa_supplicant_ctrl_iface_autoscan(wpa_s, buf + 9))
  3944. reply_len = -1;
  3945. #endif /* CONFIG_AUTOSCAN */
  3946. } else if (os_strcmp(buf, "REAUTHENTICATE") == 0) {
  3947. eapol_sm_request_reauth(wpa_s->eapol);
  3948. } else {
  3949. os_memcpy(reply, "UNKNOWN COMMAND\n", 16);
  3950. reply_len = 16;
  3951. }
  3952. if (reply_len < 0) {
  3953. os_memcpy(reply, "FAIL\n", 5);
  3954. reply_len = 5;
  3955. }
  3956. if (ctrl_rsp)
  3957. eapol_sm_notify_ctrl_response(wpa_s->eapol);
  3958. *resp_len = reply_len;
  3959. return reply;
  3960. }
  3961. static int wpa_supplicant_global_iface_add(struct wpa_global *global,
  3962. char *cmd)
  3963. {
  3964. struct wpa_interface iface;
  3965. char *pos;
  3966. /*
  3967. * <ifname>TAB<confname>TAB<driver>TAB<ctrl_interface>TAB<driver_param>
  3968. * TAB<bridge_ifname>
  3969. */
  3970. wpa_printf(MSG_DEBUG, "CTRL_IFACE GLOBAL INTERFACE_ADD '%s'", cmd);
  3971. os_memset(&iface, 0, sizeof(iface));
  3972. do {
  3973. iface.ifname = pos = cmd;
  3974. pos = os_strchr(pos, '\t');
  3975. if (pos)
  3976. *pos++ = '\0';
  3977. if (iface.ifname[0] == '\0')
  3978. return -1;
  3979. if (pos == NULL)
  3980. break;
  3981. iface.confname = pos;
  3982. pos = os_strchr(pos, '\t');
  3983. if (pos)
  3984. *pos++ = '\0';
  3985. if (iface.confname[0] == '\0')
  3986. iface.confname = NULL;
  3987. if (pos == NULL)
  3988. break;
  3989. iface.driver = pos;
  3990. pos = os_strchr(pos, '\t');
  3991. if (pos)
  3992. *pos++ = '\0';
  3993. if (iface.driver[0] == '\0')
  3994. iface.driver = NULL;
  3995. if (pos == NULL)
  3996. break;
  3997. iface.ctrl_interface = pos;
  3998. pos = os_strchr(pos, '\t');
  3999. if (pos)
  4000. *pos++ = '\0';
  4001. if (iface.ctrl_interface[0] == '\0')
  4002. iface.ctrl_interface = NULL;
  4003. if (pos == NULL)
  4004. break;
  4005. iface.driver_param = pos;
  4006. pos = os_strchr(pos, '\t');
  4007. if (pos)
  4008. *pos++ = '\0';
  4009. if (iface.driver_param[0] == '\0')
  4010. iface.driver_param = NULL;
  4011. if (pos == NULL)
  4012. break;
  4013. iface.bridge_ifname = pos;
  4014. pos = os_strchr(pos, '\t');
  4015. if (pos)
  4016. *pos++ = '\0';
  4017. if (iface.bridge_ifname[0] == '\0')
  4018. iface.bridge_ifname = NULL;
  4019. if (pos == NULL)
  4020. break;
  4021. } while (0);
  4022. if (wpa_supplicant_get_iface(global, iface.ifname))
  4023. return -1;
  4024. return wpa_supplicant_add_iface(global, &iface) ? 0 : -1;
  4025. }
  4026. static int wpa_supplicant_global_iface_remove(struct wpa_global *global,
  4027. char *cmd)
  4028. {
  4029. struct wpa_supplicant *wpa_s;
  4030. wpa_printf(MSG_DEBUG, "CTRL_IFACE GLOBAL INTERFACE_REMOVE '%s'", cmd);
  4031. wpa_s = wpa_supplicant_get_iface(global, cmd);
  4032. if (wpa_s == NULL)
  4033. return -1;
  4034. return wpa_supplicant_remove_iface(global, wpa_s, 0);
  4035. }
  4036. static void wpa_free_iface_info(struct wpa_interface_info *iface)
  4037. {
  4038. struct wpa_interface_info *prev;
  4039. while (iface) {
  4040. prev = iface;
  4041. iface = iface->next;
  4042. os_free(prev->ifname);
  4043. os_free(prev->desc);
  4044. os_free(prev);
  4045. }
  4046. }
  4047. static int wpa_supplicant_global_iface_list(struct wpa_global *global,
  4048. char *buf, int len)
  4049. {
  4050. int i, res;
  4051. struct wpa_interface_info *iface = NULL, *last = NULL, *tmp;
  4052. char *pos, *end;
  4053. for (i = 0; wpa_drivers[i]; i++) {
  4054. struct wpa_driver_ops *drv = wpa_drivers[i];
  4055. if (drv->get_interfaces == NULL)
  4056. continue;
  4057. tmp = drv->get_interfaces(global->drv_priv[i]);
  4058. if (tmp == NULL)
  4059. continue;
  4060. if (last == NULL)
  4061. iface = last = tmp;
  4062. else
  4063. last->next = tmp;
  4064. while (last->next)
  4065. last = last->next;
  4066. }
  4067. pos = buf;
  4068. end = buf + len;
  4069. for (tmp = iface; tmp; tmp = tmp->next) {
  4070. res = os_snprintf(pos, end - pos, "%s\t%s\t%s\n",
  4071. tmp->drv_name, tmp->ifname,
  4072. tmp->desc ? tmp->desc : "");
  4073. if (res < 0 || res >= end - pos) {
  4074. *pos = '\0';
  4075. break;
  4076. }
  4077. pos += res;
  4078. }
  4079. wpa_free_iface_info(iface);
  4080. return pos - buf;
  4081. }
  4082. static int wpa_supplicant_global_iface_interfaces(struct wpa_global *global,
  4083. char *buf, int len)
  4084. {
  4085. int res;
  4086. char *pos, *end;
  4087. struct wpa_supplicant *wpa_s;
  4088. wpa_s = global->ifaces;
  4089. pos = buf;
  4090. end = buf + len;
  4091. while (wpa_s) {
  4092. res = os_snprintf(pos, end - pos, "%s\n", wpa_s->ifname);
  4093. if (res < 0 || res >= end - pos) {
  4094. *pos = '\0';
  4095. break;
  4096. }
  4097. pos += res;
  4098. wpa_s = wpa_s->next;
  4099. }
  4100. return pos - buf;
  4101. }
  4102. char * wpa_supplicant_global_ctrl_iface_process(struct wpa_global *global,
  4103. char *buf, size_t *resp_len)
  4104. {
  4105. char *reply;
  4106. const int reply_size = 2048;
  4107. int reply_len;
  4108. int level = MSG_DEBUG;
  4109. if (os_strcmp(buf, "PING") == 0)
  4110. level = MSG_EXCESSIVE;
  4111. wpa_hexdump_ascii(level, "RX global ctrl_iface",
  4112. (const u8 *) buf, os_strlen(buf));
  4113. reply = os_malloc(reply_size);
  4114. if (reply == NULL) {
  4115. *resp_len = 1;
  4116. return NULL;
  4117. }
  4118. os_memcpy(reply, "OK\n", 3);
  4119. reply_len = 3;
  4120. if (os_strcmp(buf, "PING") == 0) {
  4121. os_memcpy(reply, "PONG\n", 5);
  4122. reply_len = 5;
  4123. } else if (os_strncmp(buf, "INTERFACE_ADD ", 14) == 0) {
  4124. if (wpa_supplicant_global_iface_add(global, buf + 14))
  4125. reply_len = -1;
  4126. } else if (os_strncmp(buf, "INTERFACE_REMOVE ", 17) == 0) {
  4127. if (wpa_supplicant_global_iface_remove(global, buf + 17))
  4128. reply_len = -1;
  4129. } else if (os_strcmp(buf, "INTERFACE_LIST") == 0) {
  4130. reply_len = wpa_supplicant_global_iface_list(
  4131. global, reply, reply_size);
  4132. } else if (os_strcmp(buf, "INTERFACES") == 0) {
  4133. reply_len = wpa_supplicant_global_iface_interfaces(
  4134. global, reply, reply_size);
  4135. } else if (os_strcmp(buf, "TERMINATE") == 0) {
  4136. wpa_supplicant_terminate_proc(global);
  4137. } else if (os_strcmp(buf, "SUSPEND") == 0) {
  4138. wpas_notify_suspend(global);
  4139. } else if (os_strcmp(buf, "RESUME") == 0) {
  4140. wpas_notify_resume(global);
  4141. } else {
  4142. os_memcpy(reply, "UNKNOWN COMMAND\n", 16);
  4143. reply_len = 16;
  4144. }
  4145. if (reply_len < 0) {
  4146. os_memcpy(reply, "FAIL\n", 5);
  4147. reply_len = 5;
  4148. }
  4149. *resp_len = reply_len;
  4150. return reply;
  4151. }