ctrl_iface.c 85 KB

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