ctrl_iface.c 90 KB

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