ctrl_iface.c 84 KB

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