ctrl_iface.c 85 KB

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