ctrl_iface.c 83 KB

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