ctrl_iface.c 77 KB

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