ctrl_iface.c 105 KB

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