ctrl_iface.c 53 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216
  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 "notify.h"
  33. #include "bss.h"
  34. #include "scan.h"
  35. #include "ctrl_iface.h"
  36. extern struct wpa_driver_ops *wpa_drivers[];
  37. static int wpa_supplicant_global_iface_list(struct wpa_global *global,
  38. char *buf, int len);
  39. static int wpa_supplicant_global_iface_interfaces(struct wpa_global *global,
  40. char *buf, int len);
  41. static int wpa_supplicant_ctrl_iface_set(struct wpa_supplicant *wpa_s,
  42. char *cmd)
  43. {
  44. char *value;
  45. int ret = 0;
  46. value = os_strchr(cmd, ' ');
  47. if (value == NULL)
  48. return -1;
  49. *value++ = '\0';
  50. wpa_printf(MSG_DEBUG, "CTRL_IFACE SET '%s'='%s'", cmd, value);
  51. if (os_strcasecmp(cmd, "EAPOL::heldPeriod") == 0) {
  52. eapol_sm_configure(wpa_s->eapol,
  53. atoi(value), -1, -1, -1);
  54. } else if (os_strcasecmp(cmd, "EAPOL::authPeriod") == 0) {
  55. eapol_sm_configure(wpa_s->eapol,
  56. -1, atoi(value), -1, -1);
  57. } else if (os_strcasecmp(cmd, "EAPOL::startPeriod") == 0) {
  58. eapol_sm_configure(wpa_s->eapol,
  59. -1, -1, atoi(value), -1);
  60. } else if (os_strcasecmp(cmd, "EAPOL::maxStart") == 0) {
  61. eapol_sm_configure(wpa_s->eapol,
  62. -1, -1, -1, atoi(value));
  63. } else if (os_strcasecmp(cmd, "dot11RSNAConfigPMKLifetime") == 0) {
  64. if (wpa_sm_set_param(wpa_s->wpa, RSNA_PMK_LIFETIME,
  65. atoi(value)))
  66. ret = -1;
  67. } else if (os_strcasecmp(cmd, "dot11RSNAConfigPMKReauthThreshold") ==
  68. 0) {
  69. if (wpa_sm_set_param(wpa_s->wpa, RSNA_PMK_REAUTH_THRESHOLD,
  70. atoi(value)))
  71. ret = -1;
  72. } else if (os_strcasecmp(cmd, "dot11RSNAConfigSATimeout") == 0) {
  73. if (wpa_sm_set_param(wpa_s->wpa, RSNA_SA_TIMEOUT, atoi(value)))
  74. ret = -1;
  75. } else
  76. ret = -1;
  77. return ret;
  78. }
  79. #ifdef IEEE8021X_EAPOL
  80. static int wpa_supplicant_ctrl_iface_preauth(struct wpa_supplicant *wpa_s,
  81. char *addr)
  82. {
  83. u8 bssid[ETH_ALEN];
  84. struct wpa_ssid *ssid = wpa_s->current_ssid;
  85. if (hwaddr_aton(addr, bssid)) {
  86. wpa_printf(MSG_DEBUG, "CTRL_IFACE PREAUTH: invalid address "
  87. "'%s'", addr);
  88. return -1;
  89. }
  90. wpa_printf(MSG_DEBUG, "CTRL_IFACE PREAUTH " MACSTR, MAC2STR(bssid));
  91. rsn_preauth_deinit(wpa_s->wpa);
  92. if (rsn_preauth_init(wpa_s->wpa, bssid, ssid ? &ssid->eap : NULL))
  93. return -1;
  94. return 0;
  95. }
  96. #endif /* IEEE8021X_EAPOL */
  97. #ifdef CONFIG_PEERKEY
  98. /* MLME-STKSTART.request(peer) */
  99. static int wpa_supplicant_ctrl_iface_stkstart(
  100. struct wpa_supplicant *wpa_s, char *addr)
  101. {
  102. u8 peer[ETH_ALEN];
  103. if (hwaddr_aton(addr, peer)) {
  104. wpa_printf(MSG_DEBUG, "CTRL_IFACE STKSTART: invalid "
  105. "address '%s'", addr);
  106. return -1;
  107. }
  108. wpa_printf(MSG_DEBUG, "CTRL_IFACE STKSTART " MACSTR,
  109. MAC2STR(peer));
  110. return wpa_sm_stkstart(wpa_s->wpa, peer);
  111. }
  112. #endif /* CONFIG_PEERKEY */
  113. #ifdef CONFIG_IEEE80211R
  114. static int wpa_supplicant_ctrl_iface_ft_ds(
  115. struct wpa_supplicant *wpa_s, char *addr)
  116. {
  117. u8 target_ap[ETH_ALEN];
  118. struct wpa_bss *bss;
  119. const u8 *mdie;
  120. if (hwaddr_aton(addr, target_ap)) {
  121. wpa_printf(MSG_DEBUG, "CTRL_IFACE FT_DS: invalid "
  122. "address '%s'", addr);
  123. return -1;
  124. }
  125. wpa_printf(MSG_DEBUG, "CTRL_IFACE FT_DS " MACSTR, MAC2STR(target_ap));
  126. bss = wpa_bss_get_bssid(wpa_s, target_ap);
  127. if (bss)
  128. mdie = wpa_bss_get_ie(bss, WLAN_EID_MOBILITY_DOMAIN);
  129. else
  130. mdie = NULL;
  131. return wpa_ft_start_over_ds(wpa_s->wpa, target_ap, mdie);
  132. }
  133. #endif /* CONFIG_IEEE80211R */
  134. #ifdef CONFIG_WPS
  135. static int wpa_supplicant_ctrl_iface_wps_pbc(struct wpa_supplicant *wpa_s,
  136. char *cmd)
  137. {
  138. u8 bssid[ETH_ALEN], *_bssid = bssid;
  139. if (cmd == NULL || os_strcmp(cmd, "any") == 0)
  140. _bssid = NULL;
  141. else if (hwaddr_aton(cmd, bssid)) {
  142. wpa_printf(MSG_DEBUG, "CTRL_IFACE WPS_PBC: invalid BSSID '%s'",
  143. cmd);
  144. return -1;
  145. }
  146. #ifdef CONFIG_AP
  147. if (wpa_s->ap_iface)
  148. return wpa_supplicant_ap_wps_pbc(wpa_s, _bssid);
  149. #endif /* CONFIG_AP */
  150. return wpas_wps_start_pbc(wpa_s, _bssid);
  151. }
  152. static int wpa_supplicant_ctrl_iface_wps_pin(struct wpa_supplicant *wpa_s,
  153. char *cmd, char *buf,
  154. size_t buflen)
  155. {
  156. u8 bssid[ETH_ALEN], *_bssid = bssid;
  157. char *pin;
  158. int ret;
  159. pin = os_strchr(cmd, ' ');
  160. if (pin)
  161. *pin++ = '\0';
  162. if (os_strcmp(cmd, "any") == 0)
  163. _bssid = NULL;
  164. else if (hwaddr_aton(cmd, bssid)) {
  165. wpa_printf(MSG_DEBUG, "CTRL_IFACE WPS_PIN: invalid BSSID '%s'",
  166. cmd);
  167. return -1;
  168. }
  169. #ifdef CONFIG_AP
  170. if (wpa_s->ap_iface)
  171. return wpa_supplicant_ap_wps_pin(wpa_s, _bssid, pin,
  172. buf, buflen);
  173. #endif /* CONFIG_AP */
  174. if (pin) {
  175. ret = wpas_wps_start_pin(wpa_s, _bssid, pin);
  176. if (ret < 0)
  177. return -1;
  178. ret = os_snprintf(buf, buflen, "%s", pin);
  179. if (ret < 0 || (size_t) ret >= buflen)
  180. return -1;
  181. return ret;
  182. }
  183. ret = wpas_wps_start_pin(wpa_s, _bssid, NULL);
  184. if (ret < 0)
  185. return -1;
  186. /* Return the generated PIN */
  187. ret = os_snprintf(buf, buflen, "%08d", ret);
  188. if (ret < 0 || (size_t) ret >= buflen)
  189. return -1;
  190. return ret;
  191. }
  192. #ifdef CONFIG_WPS_OOB
  193. static int wpa_supplicant_ctrl_iface_wps_oob(struct wpa_supplicant *wpa_s,
  194. char *cmd)
  195. {
  196. char *path, *method, *name;
  197. path = os_strchr(cmd, ' ');
  198. if (path == NULL)
  199. return -1;
  200. *path++ = '\0';
  201. method = os_strchr(path, ' ');
  202. if (method == NULL)
  203. return -1;
  204. *method++ = '\0';
  205. name = os_strchr(method, ' ');
  206. if (name != NULL)
  207. *name++ = '\0';
  208. return wpas_wps_start_oob(wpa_s, cmd, path, method, name);
  209. }
  210. #endif /* CONFIG_WPS_OOB */
  211. static int wpa_supplicant_ctrl_iface_wps_reg(struct wpa_supplicant *wpa_s,
  212. char *cmd)
  213. {
  214. u8 bssid[ETH_ALEN], *_bssid = bssid;
  215. char *pin;
  216. char *new_ssid;
  217. char *new_auth;
  218. char *new_encr;
  219. char *new_key;
  220. struct wps_new_ap_settings ap;
  221. pin = os_strchr(cmd, ' ');
  222. if (pin == NULL)
  223. return -1;
  224. *pin++ = '\0';
  225. if (os_strcmp(cmd, "any") == 0)
  226. _bssid = NULL;
  227. else if (hwaddr_aton(cmd, bssid)) {
  228. wpa_printf(MSG_DEBUG, "CTRL_IFACE WPS_REG: invalid BSSID '%s'",
  229. cmd);
  230. return -1;
  231. }
  232. new_ssid = os_strchr(pin, ' ');
  233. if (new_ssid == NULL)
  234. return wpas_wps_start_reg(wpa_s, _bssid, pin, NULL);
  235. *new_ssid++ = '\0';
  236. new_auth = os_strchr(new_ssid, ' ');
  237. if (new_auth == NULL)
  238. return -1;
  239. *new_auth++ = '\0';
  240. new_encr = os_strchr(new_auth, ' ');
  241. if (new_encr == NULL)
  242. return -1;
  243. *new_encr++ = '\0';
  244. new_key = os_strchr(new_encr, ' ');
  245. if (new_key == NULL)
  246. return -1;
  247. *new_key++ = '\0';
  248. os_memset(&ap, 0, sizeof(ap));
  249. ap.ssid_hex = new_ssid;
  250. ap.auth = new_auth;
  251. ap.encr = new_encr;
  252. ap.key_hex = new_key;
  253. return wpas_wps_start_reg(wpa_s, _bssid, pin, &ap);
  254. }
  255. #ifdef CONFIG_WPS_ER
  256. static int wpa_supplicant_ctrl_iface_wps_er_pin(struct wpa_supplicant *wpa_s,
  257. char *cmd)
  258. {
  259. char *uuid = cmd, *pin, *pos;
  260. u8 addr_buf[ETH_ALEN], *addr = NULL;
  261. pin = os_strchr(uuid, ' ');
  262. if (pin == NULL)
  263. return -1;
  264. *pin++ = '\0';
  265. pos = os_strchr(pin, ' ');
  266. if (pos) {
  267. *pos++ = '\0';
  268. if (hwaddr_aton(pos, addr_buf) == 0)
  269. addr = addr_buf;
  270. }
  271. return wpas_wps_er_add_pin(wpa_s, addr, uuid, pin);
  272. }
  273. static int wpa_supplicant_ctrl_iface_wps_er_learn(struct wpa_supplicant *wpa_s,
  274. char *cmd)
  275. {
  276. char *uuid = cmd, *pin;
  277. pin = os_strchr(uuid, ' ');
  278. if (pin == NULL)
  279. return -1;
  280. *pin++ = '\0';
  281. return wpas_wps_er_learn(wpa_s, uuid, pin);
  282. }
  283. static int wpa_supplicant_ctrl_iface_wps_er_config(
  284. struct wpa_supplicant *wpa_s, char *cmd)
  285. {
  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. new_ssid = os_strchr(pin, ' ');
  297. if (new_ssid == NULL)
  298. return -1;
  299. *new_ssid++ = '\0';
  300. new_auth = os_strchr(new_ssid, ' ');
  301. if (new_auth == NULL)
  302. return -1;
  303. *new_auth++ = '\0';
  304. new_encr = os_strchr(new_auth, ' ');
  305. if (new_encr == NULL)
  306. return -1;
  307. *new_encr++ = '\0';
  308. new_key = os_strchr(new_encr, ' ');
  309. if (new_key == NULL)
  310. return -1;
  311. *new_key++ = '\0';
  312. os_memset(&ap, 0, sizeof(ap));
  313. ap.ssid_hex = new_ssid;
  314. ap.auth = new_auth;
  315. ap.encr = new_encr;
  316. ap.key_hex = new_key;
  317. return wpas_wps_er_config(wpa_s, cmd, pin, &ap);
  318. }
  319. #endif /* CONFIG_WPS_ER */
  320. #endif /* CONFIG_WPS */
  321. #ifdef CONFIG_IBSS_RSN
  322. static int wpa_supplicant_ctrl_iface_ibss_rsn(
  323. struct wpa_supplicant *wpa_s, char *addr)
  324. {
  325. u8 peer[ETH_ALEN];
  326. if (hwaddr_aton(addr, peer)) {
  327. wpa_printf(MSG_DEBUG, "CTRL_IFACE IBSS_RSN: invalid "
  328. "address '%s'", addr);
  329. return -1;
  330. }
  331. wpa_printf(MSG_DEBUG, "CTRL_IFACE IBSS_RSN " MACSTR,
  332. MAC2STR(peer));
  333. return ibss_rsn_start(wpa_s->ibss_rsn, peer);
  334. }
  335. #endif /* CONFIG_IBSS_RSN */
  336. static int wpa_supplicant_ctrl_iface_ctrl_rsp(struct wpa_supplicant *wpa_s,
  337. char *rsp)
  338. {
  339. #ifdef IEEE8021X_EAPOL
  340. char *pos, *id_pos;
  341. int id;
  342. struct wpa_ssid *ssid;
  343. struct eap_peer_config *eap;
  344. pos = os_strchr(rsp, '-');
  345. if (pos == NULL)
  346. return -1;
  347. *pos++ = '\0';
  348. id_pos = pos;
  349. pos = os_strchr(pos, ':');
  350. if (pos == NULL)
  351. return -1;
  352. *pos++ = '\0';
  353. id = atoi(id_pos);
  354. wpa_printf(MSG_DEBUG, "CTRL_IFACE: field=%s id=%d", rsp, id);
  355. wpa_hexdump_ascii_key(MSG_DEBUG, "CTRL_IFACE: value",
  356. (u8 *) pos, os_strlen(pos));
  357. ssid = wpa_config_get_network(wpa_s->conf, id);
  358. if (ssid == NULL) {
  359. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find SSID id=%d "
  360. "to update", id);
  361. return -1;
  362. }
  363. eap = &ssid->eap;
  364. if (os_strcmp(rsp, "IDENTITY") == 0) {
  365. os_free(eap->identity);
  366. eap->identity = (u8 *) os_strdup(pos);
  367. eap->identity_len = os_strlen(pos);
  368. eap->pending_req_identity = 0;
  369. if (ssid == wpa_s->current_ssid)
  370. wpa_s->reassociate = 1;
  371. } else if (os_strcmp(rsp, "PASSWORD") == 0) {
  372. os_free(eap->password);
  373. eap->password = (u8 *) os_strdup(pos);
  374. eap->password_len = os_strlen(pos);
  375. eap->pending_req_password = 0;
  376. if (ssid == wpa_s->current_ssid)
  377. wpa_s->reassociate = 1;
  378. } else if (os_strcmp(rsp, "NEW_PASSWORD") == 0) {
  379. os_free(eap->new_password);
  380. eap->new_password = (u8 *) os_strdup(pos);
  381. eap->new_password_len = os_strlen(pos);
  382. eap->pending_req_new_password = 0;
  383. if (ssid == wpa_s->current_ssid)
  384. wpa_s->reassociate = 1;
  385. } else if (os_strcmp(rsp, "PIN") == 0) {
  386. os_free(eap->pin);
  387. eap->pin = os_strdup(pos);
  388. eap->pending_req_pin = 0;
  389. if (ssid == wpa_s->current_ssid)
  390. wpa_s->reassociate = 1;
  391. } else if (os_strcmp(rsp, "OTP") == 0) {
  392. os_free(eap->otp);
  393. eap->otp = (u8 *) os_strdup(pos);
  394. eap->otp_len = os_strlen(pos);
  395. os_free(eap->pending_req_otp);
  396. eap->pending_req_otp = NULL;
  397. eap->pending_req_otp_len = 0;
  398. } else if (os_strcmp(rsp, "PASSPHRASE") == 0) {
  399. os_free(eap->private_key_passwd);
  400. eap->private_key_passwd = (u8 *) os_strdup(pos);
  401. eap->pending_req_passphrase = 0;
  402. if (ssid == wpa_s->current_ssid)
  403. wpa_s->reassociate = 1;
  404. } else {
  405. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Unknown field '%s'", rsp);
  406. return -1;
  407. }
  408. return 0;
  409. #else /* IEEE8021X_EAPOL */
  410. wpa_printf(MSG_DEBUG, "CTRL_IFACE: 802.1X not included");
  411. return -1;
  412. #endif /* IEEE8021X_EAPOL */
  413. }
  414. static int wpa_supplicant_ctrl_iface_status(struct wpa_supplicant *wpa_s,
  415. const char *params,
  416. char *buf, size_t buflen)
  417. {
  418. char *pos, *end, tmp[30];
  419. int res, verbose, ret;
  420. verbose = os_strcmp(params, "-VERBOSE") == 0;
  421. pos = buf;
  422. end = buf + buflen;
  423. if (wpa_s->wpa_state >= WPA_ASSOCIATED) {
  424. struct wpa_ssid *ssid = wpa_s->current_ssid;
  425. ret = os_snprintf(pos, end - pos, "bssid=" MACSTR "\n",
  426. MAC2STR(wpa_s->bssid));
  427. if (ret < 0 || ret >= end - pos)
  428. return pos - buf;
  429. pos += ret;
  430. if (ssid) {
  431. u8 *_ssid = ssid->ssid;
  432. size_t ssid_len = ssid->ssid_len;
  433. u8 ssid_buf[MAX_SSID_LEN];
  434. if (ssid_len == 0) {
  435. int _res = wpa_drv_get_ssid(wpa_s, ssid_buf);
  436. if (_res < 0)
  437. ssid_len = 0;
  438. else
  439. ssid_len = _res;
  440. _ssid = ssid_buf;
  441. }
  442. ret = os_snprintf(pos, end - pos, "ssid=%s\nid=%d\n",
  443. wpa_ssid_txt(_ssid, ssid_len),
  444. ssid->id);
  445. if (ret < 0 || ret >= end - pos)
  446. return pos - buf;
  447. pos += ret;
  448. if (ssid->id_str) {
  449. ret = os_snprintf(pos, end - pos,
  450. "id_str=%s\n",
  451. ssid->id_str);
  452. if (ret < 0 || ret >= end - pos)
  453. return pos - buf;
  454. pos += ret;
  455. }
  456. switch (ssid->mode) {
  457. case WPAS_MODE_INFRA:
  458. ret = os_snprintf(pos, end - pos,
  459. "mode=station\n");
  460. break;
  461. case WPAS_MODE_IBSS:
  462. ret = os_snprintf(pos, end - pos,
  463. "mode=IBSS\n");
  464. break;
  465. case WPAS_MODE_AP:
  466. ret = os_snprintf(pos, end - pos,
  467. "mode=AP\n");
  468. break;
  469. default:
  470. ret = 0;
  471. break;
  472. }
  473. if (ret < 0 || ret >= end - pos)
  474. return pos - buf;
  475. pos += ret;
  476. }
  477. #ifdef CONFIG_AP
  478. if (wpa_s->ap_iface) {
  479. pos += ap_ctrl_iface_wpa_get_status(wpa_s, pos,
  480. end - pos,
  481. verbose);
  482. } else
  483. #endif /* CONFIG_AP */
  484. pos += wpa_sm_get_status(wpa_s->wpa, pos, end - pos, verbose);
  485. }
  486. ret = os_snprintf(pos, end - pos, "wpa_state=%s\n",
  487. wpa_supplicant_state_txt(wpa_s->wpa_state));
  488. if (ret < 0 || ret >= end - pos)
  489. return pos - buf;
  490. pos += ret;
  491. if (wpa_s->l2 &&
  492. l2_packet_get_ip_addr(wpa_s->l2, tmp, sizeof(tmp)) >= 0) {
  493. ret = os_snprintf(pos, end - pos, "ip_address=%s\n", tmp);
  494. if (ret < 0 || ret >= end - pos)
  495. return pos - buf;
  496. pos += ret;
  497. }
  498. if (wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt) ||
  499. wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
  500. res = eapol_sm_get_status(wpa_s->eapol, pos, end - pos,
  501. verbose);
  502. if (res >= 0)
  503. pos += res;
  504. }
  505. res = rsn_preauth_get_status(wpa_s->wpa, pos, end - pos, verbose);
  506. if (res >= 0)
  507. pos += res;
  508. return pos - buf;
  509. }
  510. static int wpa_supplicant_ctrl_iface_bssid(struct wpa_supplicant *wpa_s,
  511. char *cmd)
  512. {
  513. char *pos;
  514. int id;
  515. struct wpa_ssid *ssid;
  516. u8 bssid[ETH_ALEN];
  517. /* cmd: "<network id> <BSSID>" */
  518. pos = os_strchr(cmd, ' ');
  519. if (pos == NULL)
  520. return -1;
  521. *pos++ = '\0';
  522. id = atoi(cmd);
  523. wpa_printf(MSG_DEBUG, "CTRL_IFACE: id=%d bssid='%s'", id, pos);
  524. if (hwaddr_aton(pos, bssid)) {
  525. wpa_printf(MSG_DEBUG ,"CTRL_IFACE: invalid BSSID '%s'", pos);
  526. return -1;
  527. }
  528. ssid = wpa_config_get_network(wpa_s->conf, id);
  529. if (ssid == NULL) {
  530. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find SSID id=%d "
  531. "to update", id);
  532. return -1;
  533. }
  534. os_memcpy(ssid->bssid, bssid, ETH_ALEN);
  535. ssid->bssid_set = !is_zero_ether_addr(bssid);
  536. return 0;
  537. }
  538. static int wpa_supplicant_ctrl_iface_list_networks(
  539. struct wpa_supplicant *wpa_s, char *buf, size_t buflen)
  540. {
  541. char *pos, *end;
  542. struct wpa_ssid *ssid;
  543. int ret;
  544. pos = buf;
  545. end = buf + buflen;
  546. ret = os_snprintf(pos, end - pos,
  547. "network id / ssid / bssid / flags\n");
  548. if (ret < 0 || ret >= end - pos)
  549. return pos - buf;
  550. pos += ret;
  551. ssid = wpa_s->conf->ssid;
  552. while (ssid) {
  553. ret = os_snprintf(pos, end - pos, "%d\t%s",
  554. ssid->id,
  555. wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
  556. if (ret < 0 || ret >= end - pos)
  557. return pos - buf;
  558. pos += ret;
  559. if (ssid->bssid_set) {
  560. ret = os_snprintf(pos, end - pos, "\t" MACSTR,
  561. MAC2STR(ssid->bssid));
  562. } else {
  563. ret = os_snprintf(pos, end - pos, "\tany");
  564. }
  565. if (ret < 0 || ret >= end - pos)
  566. return pos - buf;
  567. pos += ret;
  568. ret = os_snprintf(pos, end - pos, "\t%s%s",
  569. ssid == wpa_s->current_ssid ?
  570. "[CURRENT]" : "",
  571. ssid->disabled ? "[DISABLED]" : "");
  572. if (ret < 0 || ret >= end - pos)
  573. return pos - buf;
  574. pos += ret;
  575. ret = os_snprintf(pos, end - pos, "\n");
  576. if (ret < 0 || ret >= end - pos)
  577. return pos - buf;
  578. pos += ret;
  579. ssid = ssid->next;
  580. }
  581. return pos - buf;
  582. }
  583. static char * wpa_supplicant_cipher_txt(char *pos, char *end, int cipher)
  584. {
  585. int first = 1, ret;
  586. ret = os_snprintf(pos, end - pos, "-");
  587. if (ret < 0 || ret >= end - pos)
  588. return pos;
  589. pos += ret;
  590. if (cipher & WPA_CIPHER_NONE) {
  591. ret = os_snprintf(pos, end - pos, "%sNONE", first ? "" : "+");
  592. if (ret < 0 || ret >= end - pos)
  593. return pos;
  594. pos += ret;
  595. first = 0;
  596. }
  597. if (cipher & WPA_CIPHER_WEP40) {
  598. ret = os_snprintf(pos, end - pos, "%sWEP40", first ? "" : "+");
  599. if (ret < 0 || ret >= end - pos)
  600. return pos;
  601. pos += ret;
  602. first = 0;
  603. }
  604. if (cipher & WPA_CIPHER_WEP104) {
  605. ret = os_snprintf(pos, end - pos, "%sWEP104",
  606. first ? "" : "+");
  607. if (ret < 0 || ret >= end - pos)
  608. return pos;
  609. pos += ret;
  610. first = 0;
  611. }
  612. if (cipher & WPA_CIPHER_TKIP) {
  613. ret = os_snprintf(pos, end - pos, "%sTKIP", first ? "" : "+");
  614. if (ret < 0 || ret >= end - pos)
  615. return pos;
  616. pos += ret;
  617. first = 0;
  618. }
  619. if (cipher & WPA_CIPHER_CCMP) {
  620. ret = os_snprintf(pos, end - pos, "%sCCMP", first ? "" : "+");
  621. if (ret < 0 || ret >= end - pos)
  622. return pos;
  623. pos += ret;
  624. first = 0;
  625. }
  626. return pos;
  627. }
  628. static char * wpa_supplicant_ie_txt(char *pos, char *end, const char *proto,
  629. const u8 *ie, size_t ie_len)
  630. {
  631. struct wpa_ie_data data;
  632. int first, ret;
  633. ret = os_snprintf(pos, end - pos, "[%s-", proto);
  634. if (ret < 0 || ret >= end - pos)
  635. return pos;
  636. pos += ret;
  637. if (wpa_parse_wpa_ie(ie, ie_len, &data) < 0) {
  638. ret = os_snprintf(pos, end - pos, "?]");
  639. if (ret < 0 || ret >= end - pos)
  640. return pos;
  641. pos += ret;
  642. return pos;
  643. }
  644. first = 1;
  645. if (data.key_mgmt & WPA_KEY_MGMT_IEEE8021X) {
  646. ret = os_snprintf(pos, end - pos, "%sEAP", first ? "" : "+");
  647. if (ret < 0 || ret >= end - pos)
  648. return pos;
  649. pos += ret;
  650. first = 0;
  651. }
  652. if (data.key_mgmt & WPA_KEY_MGMT_PSK) {
  653. ret = os_snprintf(pos, end - pos, "%sPSK", first ? "" : "+");
  654. if (ret < 0 || ret >= end - pos)
  655. return pos;
  656. pos += ret;
  657. first = 0;
  658. }
  659. if (data.key_mgmt & WPA_KEY_MGMT_WPA_NONE) {
  660. ret = os_snprintf(pos, end - pos, "%sNone", first ? "" : "+");
  661. if (ret < 0 || ret >= end - pos)
  662. return pos;
  663. pos += ret;
  664. first = 0;
  665. }
  666. #ifdef CONFIG_IEEE80211R
  667. if (data.key_mgmt & WPA_KEY_MGMT_FT_IEEE8021X) {
  668. ret = os_snprintf(pos, end - pos, "%sFT/EAP",
  669. first ? "" : "+");
  670. if (ret < 0 || ret >= end - pos)
  671. return pos;
  672. pos += ret;
  673. first = 0;
  674. }
  675. if (data.key_mgmt & WPA_KEY_MGMT_FT_PSK) {
  676. ret = os_snprintf(pos, end - pos, "%sFT/PSK",
  677. first ? "" : "+");
  678. if (ret < 0 || ret >= end - pos)
  679. return pos;
  680. pos += ret;
  681. first = 0;
  682. }
  683. #endif /* CONFIG_IEEE80211R */
  684. #ifdef CONFIG_IEEE80211W
  685. if (data.key_mgmt & WPA_KEY_MGMT_IEEE8021X_SHA256) {
  686. ret = os_snprintf(pos, end - pos, "%sEAP-SHA256",
  687. first ? "" : "+");
  688. if (ret < 0 || ret >= end - pos)
  689. return pos;
  690. pos += ret;
  691. first = 0;
  692. }
  693. if (data.key_mgmt & WPA_KEY_MGMT_PSK_SHA256) {
  694. ret = os_snprintf(pos, end - pos, "%sPSK-SHA256",
  695. first ? "" : "+");
  696. if (ret < 0 || ret >= end - pos)
  697. return pos;
  698. pos += ret;
  699. first = 0;
  700. }
  701. #endif /* CONFIG_IEEE80211W */
  702. pos = wpa_supplicant_cipher_txt(pos, end, data.pairwise_cipher);
  703. if (data.capabilities & WPA_CAPABILITY_PREAUTH) {
  704. ret = os_snprintf(pos, end - pos, "-preauth");
  705. if (ret < 0 || ret >= end - pos)
  706. return pos;
  707. pos += ret;
  708. }
  709. ret = os_snprintf(pos, end - pos, "]");
  710. if (ret < 0 || ret >= end - pos)
  711. return pos;
  712. pos += ret;
  713. return pos;
  714. }
  715. #ifdef CONFIG_WPS
  716. static char * wpa_supplicant_wps_ie_txt_buf(struct wpa_supplicant *wpa_s,
  717. char *pos, char *end,
  718. struct wpabuf *wps_ie)
  719. {
  720. int ret;
  721. const char *txt;
  722. if (wps_ie == NULL)
  723. return pos;
  724. if (wps_is_selected_pbc_registrar(wps_ie))
  725. txt = "[WPS-PBC]";
  726. else if (wps_is_addr_authorized(wps_ie, wpa_s->own_addr, 0))
  727. txt = "[WPS-AUTH]";
  728. else if (wps_is_selected_pin_registrar(wps_ie))
  729. txt = "[WPS-PIN]";
  730. else
  731. txt = "[WPS]";
  732. ret = os_snprintf(pos, end - pos, "%s", txt);
  733. if (ret >= 0 && ret < end - pos)
  734. pos += ret;
  735. wpabuf_free(wps_ie);
  736. return pos;
  737. }
  738. #endif /* CONFIG_WPS */
  739. static char * wpa_supplicant_wps_ie_txt(struct wpa_supplicant *wpa_s,
  740. char *pos, char *end,
  741. const struct wpa_bss *bss)
  742. {
  743. #ifdef CONFIG_WPS
  744. struct wpabuf *wps_ie;
  745. wps_ie = wpa_bss_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
  746. return wpa_supplicant_wps_ie_txt_buf(wpa_s, pos, end, wps_ie);
  747. #else /* CONFIG_WPS */
  748. return pos;
  749. #endif /* CONFIG_WPS */
  750. }
  751. /* Format one result on one text line into a buffer. */
  752. static int wpa_supplicant_ctrl_iface_scan_result(
  753. struct wpa_supplicant *wpa_s,
  754. const struct wpa_bss *bss, char *buf, size_t buflen)
  755. {
  756. char *pos, *end;
  757. int ret;
  758. const u8 *ie, *ie2;
  759. pos = buf;
  760. end = buf + buflen;
  761. ret = os_snprintf(pos, end - pos, MACSTR "\t%d\t%d\t",
  762. MAC2STR(bss->bssid), bss->freq, bss->level);
  763. if (ret < 0 || ret >= end - pos)
  764. return pos - buf;
  765. pos += ret;
  766. ie = wpa_bss_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
  767. if (ie)
  768. pos = wpa_supplicant_ie_txt(pos, end, "WPA", ie, 2 + ie[1]);
  769. ie2 = wpa_bss_get_ie(bss, WLAN_EID_RSN);
  770. if (ie2)
  771. pos = wpa_supplicant_ie_txt(pos, end, "WPA2", ie2, 2 + ie2[1]);
  772. pos = wpa_supplicant_wps_ie_txt(wpa_s, pos, end, bss);
  773. if (!ie && !ie2 && bss->caps & IEEE80211_CAP_PRIVACY) {
  774. ret = os_snprintf(pos, end - pos, "[WEP]");
  775. if (ret < 0 || ret >= end - pos)
  776. return pos - buf;
  777. pos += ret;
  778. }
  779. if (bss->caps & IEEE80211_CAP_IBSS) {
  780. ret = os_snprintf(pos, end - pos, "[IBSS]");
  781. if (ret < 0 || ret >= end - pos)
  782. return pos - buf;
  783. pos += ret;
  784. }
  785. if (bss->caps & IEEE80211_CAP_ESS) {
  786. ret = os_snprintf(pos, end - pos, "[ESS]");
  787. if (ret < 0 || ret >= end - pos)
  788. return pos - buf;
  789. pos += ret;
  790. }
  791. ret = os_snprintf(pos, end - pos, "\t%s",
  792. wpa_ssid_txt(bss->ssid, bss->ssid_len));
  793. if (ret < 0 || ret >= end - pos)
  794. return pos - buf;
  795. pos += ret;
  796. ret = os_snprintf(pos, end - pos, "\n");
  797. if (ret < 0 || ret >= end - pos)
  798. return pos - buf;
  799. pos += ret;
  800. return pos - buf;
  801. }
  802. static int wpa_supplicant_ctrl_iface_scan_results(
  803. struct wpa_supplicant *wpa_s, char *buf, size_t buflen)
  804. {
  805. char *pos, *end;
  806. struct wpa_bss *bss;
  807. int ret;
  808. pos = buf;
  809. end = buf + buflen;
  810. ret = os_snprintf(pos, end - pos, "bssid / frequency / signal level / "
  811. "flags / ssid\n");
  812. if (ret < 0 || ret >= end - pos)
  813. return pos - buf;
  814. pos += ret;
  815. dl_list_for_each(bss, &wpa_s->bss_id, struct wpa_bss, list_id) {
  816. ret = wpa_supplicant_ctrl_iface_scan_result(wpa_s, bss, pos,
  817. end - pos);
  818. if (ret < 0 || ret >= end - pos)
  819. return pos - buf;
  820. pos += ret;
  821. }
  822. return pos - buf;
  823. }
  824. static int wpa_supplicant_ctrl_iface_select_network(
  825. struct wpa_supplicant *wpa_s, char *cmd)
  826. {
  827. int id;
  828. struct wpa_ssid *ssid;
  829. /* cmd: "<network id>" or "any" */
  830. if (os_strcmp(cmd, "any") == 0) {
  831. wpa_printf(MSG_DEBUG, "CTRL_IFACE: SELECT_NETWORK any");
  832. ssid = NULL;
  833. } else {
  834. id = atoi(cmd);
  835. wpa_printf(MSG_DEBUG, "CTRL_IFACE: SELECT_NETWORK id=%d", id);
  836. ssid = wpa_config_get_network(wpa_s->conf, id);
  837. if (ssid == NULL) {
  838. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find "
  839. "network id=%d", id);
  840. return -1;
  841. }
  842. }
  843. wpa_supplicant_select_network(wpa_s, ssid);
  844. return 0;
  845. }
  846. static int wpa_supplicant_ctrl_iface_enable_network(
  847. struct wpa_supplicant *wpa_s, char *cmd)
  848. {
  849. int id;
  850. struct wpa_ssid *ssid;
  851. /* cmd: "<network id>" or "all" */
  852. if (os_strcmp(cmd, "all") == 0) {
  853. wpa_printf(MSG_DEBUG, "CTRL_IFACE: ENABLE_NETWORK all");
  854. ssid = NULL;
  855. } else {
  856. id = atoi(cmd);
  857. wpa_printf(MSG_DEBUG, "CTRL_IFACE: ENABLE_NETWORK id=%d", id);
  858. ssid = wpa_config_get_network(wpa_s->conf, id);
  859. if (ssid == NULL) {
  860. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find "
  861. "network id=%d", id);
  862. return -1;
  863. }
  864. }
  865. wpa_supplicant_enable_network(wpa_s, ssid);
  866. return 0;
  867. }
  868. static int wpa_supplicant_ctrl_iface_disable_network(
  869. struct wpa_supplicant *wpa_s, char *cmd)
  870. {
  871. int id;
  872. struct wpa_ssid *ssid;
  873. /* cmd: "<network id>" or "all" */
  874. if (os_strcmp(cmd, "all") == 0) {
  875. wpa_printf(MSG_DEBUG, "CTRL_IFACE: DISABLE_NETWORK all");
  876. ssid = NULL;
  877. } else {
  878. id = atoi(cmd);
  879. wpa_printf(MSG_DEBUG, "CTRL_IFACE: DISABLE_NETWORK id=%d", id);
  880. ssid = wpa_config_get_network(wpa_s->conf, id);
  881. if (ssid == NULL) {
  882. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find "
  883. "network id=%d", id);
  884. return -1;
  885. }
  886. }
  887. wpa_supplicant_disable_network(wpa_s, ssid);
  888. return 0;
  889. }
  890. static int wpa_supplicant_ctrl_iface_add_network(
  891. struct wpa_supplicant *wpa_s, char *buf, size_t buflen)
  892. {
  893. struct wpa_ssid *ssid;
  894. int ret;
  895. wpa_printf(MSG_DEBUG, "CTRL_IFACE: ADD_NETWORK");
  896. ssid = wpa_config_add_network(wpa_s->conf);
  897. if (ssid == NULL)
  898. return -1;
  899. wpas_notify_network_added(wpa_s, ssid);
  900. ssid->disabled = 1;
  901. wpa_config_set_network_defaults(ssid);
  902. ret = os_snprintf(buf, buflen, "%d\n", ssid->id);
  903. if (ret < 0 || (size_t) ret >= buflen)
  904. return -1;
  905. return ret;
  906. }
  907. static int wpa_supplicant_ctrl_iface_remove_network(
  908. struct wpa_supplicant *wpa_s, char *cmd)
  909. {
  910. int id;
  911. struct wpa_ssid *ssid;
  912. /* cmd: "<network id>" or "all" */
  913. if (os_strcmp(cmd, "all") == 0) {
  914. wpa_printf(MSG_DEBUG, "CTRL_IFACE: REMOVE_NETWORK all");
  915. ssid = wpa_s->conf->ssid;
  916. while (ssid) {
  917. struct wpa_ssid *remove_ssid = ssid;
  918. id = ssid->id;
  919. ssid = ssid->next;
  920. wpas_notify_network_removed(wpa_s, remove_ssid);
  921. wpa_config_remove_network(wpa_s->conf, id);
  922. }
  923. if (wpa_s->current_ssid) {
  924. eapol_sm_invalidate_cached_session(wpa_s->eapol);
  925. wpa_supplicant_disassociate(wpa_s,
  926. WLAN_REASON_DEAUTH_LEAVING);
  927. }
  928. return 0;
  929. }
  930. id = atoi(cmd);
  931. wpa_printf(MSG_DEBUG, "CTRL_IFACE: REMOVE_NETWORK id=%d", id);
  932. ssid = wpa_config_get_network(wpa_s->conf, id);
  933. if (ssid == NULL ||
  934. wpa_config_remove_network(wpa_s->conf, id) < 0) {
  935. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find network "
  936. "id=%d", id);
  937. return -1;
  938. }
  939. if (ssid == wpa_s->current_ssid) {
  940. /*
  941. * Invalidate the EAP session cache if the current network is
  942. * removed.
  943. */
  944. eapol_sm_invalidate_cached_session(wpa_s->eapol);
  945. wpa_supplicant_disassociate(wpa_s, WLAN_REASON_DEAUTH_LEAVING);
  946. }
  947. return 0;
  948. }
  949. static int wpa_supplicant_ctrl_iface_set_network(
  950. struct wpa_supplicant *wpa_s, char *cmd)
  951. {
  952. int id;
  953. struct wpa_ssid *ssid;
  954. char *name, *value;
  955. /* cmd: "<network id> <variable name> <value>" */
  956. name = os_strchr(cmd, ' ');
  957. if (name == NULL)
  958. return -1;
  959. *name++ = '\0';
  960. value = os_strchr(name, ' ');
  961. if (value == NULL)
  962. return -1;
  963. *value++ = '\0';
  964. id = atoi(cmd);
  965. wpa_printf(MSG_DEBUG, "CTRL_IFACE: SET_NETWORK id=%d name='%s'",
  966. id, name);
  967. wpa_hexdump_ascii_key(MSG_DEBUG, "CTRL_IFACE: value",
  968. (u8 *) value, os_strlen(value));
  969. ssid = wpa_config_get_network(wpa_s->conf, id);
  970. if (ssid == NULL) {
  971. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find network "
  972. "id=%d", id);
  973. return -1;
  974. }
  975. if (wpa_config_set(ssid, name, value, 0) < 0) {
  976. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Failed to set network "
  977. "variable '%s'", name);
  978. return -1;
  979. }
  980. if (wpa_s->current_ssid == ssid) {
  981. /*
  982. * Invalidate the EAP session cache if anything in the current
  983. * configuration changes.
  984. */
  985. eapol_sm_invalidate_cached_session(wpa_s->eapol);
  986. }
  987. if ((os_strcmp(name, "psk") == 0 &&
  988. value[0] == '"' && ssid->ssid_len) ||
  989. (os_strcmp(name, "ssid") == 0 && ssid->passphrase))
  990. wpa_config_update_psk(ssid);
  991. else if (os_strcmp(name, "priority") == 0)
  992. wpa_config_update_prio_list(wpa_s->conf);
  993. return 0;
  994. }
  995. static int wpa_supplicant_ctrl_iface_get_network(
  996. struct wpa_supplicant *wpa_s, char *cmd, char *buf, size_t buflen)
  997. {
  998. int id;
  999. size_t res;
  1000. struct wpa_ssid *ssid;
  1001. char *name, *value;
  1002. /* cmd: "<network id> <variable name>" */
  1003. name = os_strchr(cmd, ' ');
  1004. if (name == NULL || buflen == 0)
  1005. return -1;
  1006. *name++ = '\0';
  1007. id = atoi(cmd);
  1008. wpa_printf(MSG_DEBUG, "CTRL_IFACE: GET_NETWORK id=%d name='%s'",
  1009. id, name);
  1010. ssid = wpa_config_get_network(wpa_s->conf, id);
  1011. if (ssid == NULL) {
  1012. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find network "
  1013. "id=%d", id);
  1014. return -1;
  1015. }
  1016. value = wpa_config_get_no_key(ssid, name);
  1017. if (value == NULL) {
  1018. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Failed to get network "
  1019. "variable '%s'", name);
  1020. return -1;
  1021. }
  1022. res = os_strlcpy(buf, value, buflen);
  1023. if (res >= buflen) {
  1024. os_free(value);
  1025. return -1;
  1026. }
  1027. os_free(value);
  1028. return res;
  1029. }
  1030. #ifndef CONFIG_NO_CONFIG_WRITE
  1031. static int wpa_supplicant_ctrl_iface_save_config(struct wpa_supplicant *wpa_s)
  1032. {
  1033. int ret;
  1034. if (!wpa_s->conf->update_config) {
  1035. wpa_printf(MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Not allowed "
  1036. "to update configuration (update_config=0)");
  1037. return -1;
  1038. }
  1039. ret = wpa_config_write(wpa_s->confname, wpa_s->conf);
  1040. if (ret) {
  1041. wpa_printf(MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Failed to "
  1042. "update configuration");
  1043. } else {
  1044. wpa_printf(MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Configuration"
  1045. " updated");
  1046. }
  1047. return ret;
  1048. }
  1049. #endif /* CONFIG_NO_CONFIG_WRITE */
  1050. static int ctrl_iface_get_capability_pairwise(int res, char *strict,
  1051. struct wpa_driver_capa *capa,
  1052. char *buf, size_t buflen)
  1053. {
  1054. int ret, first = 1;
  1055. char *pos, *end;
  1056. size_t len;
  1057. pos = buf;
  1058. end = pos + buflen;
  1059. if (res < 0) {
  1060. if (strict)
  1061. return 0;
  1062. len = os_strlcpy(buf, "CCMP TKIP NONE", buflen);
  1063. if (len >= buflen)
  1064. return -1;
  1065. return len;
  1066. }
  1067. if (capa->enc & WPA_DRIVER_CAPA_ENC_CCMP) {
  1068. ret = os_snprintf(pos, end - pos, "%sCCMP", first ? "" : " ");
  1069. if (ret < 0 || ret >= end - pos)
  1070. return pos - buf;
  1071. pos += ret;
  1072. first = 0;
  1073. }
  1074. if (capa->enc & WPA_DRIVER_CAPA_ENC_TKIP) {
  1075. ret = os_snprintf(pos, end - pos, "%sTKIP", first ? "" : " ");
  1076. if (ret < 0 || ret >= end - pos)
  1077. return pos - buf;
  1078. pos += ret;
  1079. first = 0;
  1080. }
  1081. if (capa->key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_WPA_NONE) {
  1082. ret = os_snprintf(pos, end - pos, "%sNONE", first ? "" : " ");
  1083. if (ret < 0 || ret >= end - pos)
  1084. return pos - buf;
  1085. pos += ret;
  1086. first = 0;
  1087. }
  1088. return pos - buf;
  1089. }
  1090. static int ctrl_iface_get_capability_group(int res, char *strict,
  1091. struct wpa_driver_capa *capa,
  1092. char *buf, size_t buflen)
  1093. {
  1094. int ret, first = 1;
  1095. char *pos, *end;
  1096. size_t len;
  1097. pos = buf;
  1098. end = pos + buflen;
  1099. if (res < 0) {
  1100. if (strict)
  1101. return 0;
  1102. len = os_strlcpy(buf, "CCMP TKIP WEP104 WEP40", buflen);
  1103. if (len >= buflen)
  1104. return -1;
  1105. return len;
  1106. }
  1107. if (capa->enc & WPA_DRIVER_CAPA_ENC_CCMP) {
  1108. ret = os_snprintf(pos, end - pos, "%sCCMP", first ? "" : " ");
  1109. if (ret < 0 || ret >= end - pos)
  1110. return pos - buf;
  1111. pos += ret;
  1112. first = 0;
  1113. }
  1114. if (capa->enc & WPA_DRIVER_CAPA_ENC_TKIP) {
  1115. ret = os_snprintf(pos, end - pos, "%sTKIP", first ? "" : " ");
  1116. if (ret < 0 || ret >= end - pos)
  1117. return pos - buf;
  1118. pos += ret;
  1119. first = 0;
  1120. }
  1121. if (capa->enc & WPA_DRIVER_CAPA_ENC_WEP104) {
  1122. ret = os_snprintf(pos, end - pos, "%sWEP104",
  1123. first ? "" : " ");
  1124. if (ret < 0 || ret >= end - pos)
  1125. return pos - buf;
  1126. pos += ret;
  1127. first = 0;
  1128. }
  1129. if (capa->enc & WPA_DRIVER_CAPA_ENC_WEP40) {
  1130. ret = os_snprintf(pos, end - pos, "%sWEP40", first ? "" : " ");
  1131. if (ret < 0 || ret >= end - pos)
  1132. return pos - buf;
  1133. pos += ret;
  1134. first = 0;
  1135. }
  1136. return pos - buf;
  1137. }
  1138. static int ctrl_iface_get_capability_key_mgmt(int res, char *strict,
  1139. struct wpa_driver_capa *capa,
  1140. char *buf, size_t buflen)
  1141. {
  1142. int ret;
  1143. char *pos, *end;
  1144. size_t len;
  1145. pos = buf;
  1146. end = pos + buflen;
  1147. if (res < 0) {
  1148. if (strict)
  1149. return 0;
  1150. len = os_strlcpy(buf, "WPA-PSK WPA-EAP IEEE8021X WPA-NONE "
  1151. "NONE", buflen);
  1152. if (len >= buflen)
  1153. return -1;
  1154. return len;
  1155. }
  1156. ret = os_snprintf(pos, end - pos, "NONE IEEE8021X");
  1157. if (ret < 0 || ret >= end - pos)
  1158. return pos - buf;
  1159. pos += ret;
  1160. if (capa->key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA |
  1161. WPA_DRIVER_CAPA_KEY_MGMT_WPA2)) {
  1162. ret = os_snprintf(pos, end - pos, " WPA-EAP");
  1163. if (ret < 0 || ret >= end - pos)
  1164. return pos - buf;
  1165. pos += ret;
  1166. }
  1167. if (capa->key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA_PSK |
  1168. WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK)) {
  1169. ret = os_snprintf(pos, end - pos, " WPA-PSK");
  1170. if (ret < 0 || ret >= end - pos)
  1171. return pos - buf;
  1172. pos += ret;
  1173. }
  1174. if (capa->key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_WPA_NONE) {
  1175. ret = os_snprintf(pos, end - pos, " WPA-NONE");
  1176. if (ret < 0 || ret >= end - pos)
  1177. return pos - buf;
  1178. pos += ret;
  1179. }
  1180. return pos - buf;
  1181. }
  1182. static int ctrl_iface_get_capability_proto(int res, char *strict,
  1183. struct wpa_driver_capa *capa,
  1184. char *buf, size_t buflen)
  1185. {
  1186. int ret, first = 1;
  1187. char *pos, *end;
  1188. size_t len;
  1189. pos = buf;
  1190. end = pos + buflen;
  1191. if (res < 0) {
  1192. if (strict)
  1193. return 0;
  1194. len = os_strlcpy(buf, "RSN WPA", buflen);
  1195. if (len >= buflen)
  1196. return -1;
  1197. return len;
  1198. }
  1199. if (capa->key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA2 |
  1200. WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK)) {
  1201. ret = os_snprintf(pos, end - pos, "%sRSN", first ? "" : " ");
  1202. if (ret < 0 || ret >= end - pos)
  1203. return pos - buf;
  1204. pos += ret;
  1205. first = 0;
  1206. }
  1207. if (capa->key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA |
  1208. WPA_DRIVER_CAPA_KEY_MGMT_WPA_PSK)) {
  1209. ret = os_snprintf(pos, end - pos, "%sWPA", first ? "" : " ");
  1210. if (ret < 0 || ret >= end - pos)
  1211. return pos - buf;
  1212. pos += ret;
  1213. first = 0;
  1214. }
  1215. return pos - buf;
  1216. }
  1217. static int ctrl_iface_get_capability_auth_alg(int res, char *strict,
  1218. struct wpa_driver_capa *capa,
  1219. char *buf, size_t buflen)
  1220. {
  1221. int ret, first = 1;
  1222. char *pos, *end;
  1223. size_t len;
  1224. pos = buf;
  1225. end = pos + buflen;
  1226. if (res < 0) {
  1227. if (strict)
  1228. return 0;
  1229. len = os_strlcpy(buf, "OPEN SHARED LEAP", buflen);
  1230. if (len >= buflen)
  1231. return -1;
  1232. return len;
  1233. }
  1234. if (capa->auth & (WPA_DRIVER_AUTH_OPEN)) {
  1235. ret = os_snprintf(pos, end - pos, "%sOPEN", first ? "" : " ");
  1236. if (ret < 0 || ret >= end - pos)
  1237. return pos - buf;
  1238. pos += ret;
  1239. first = 0;
  1240. }
  1241. if (capa->auth & (WPA_DRIVER_AUTH_SHARED)) {
  1242. ret = os_snprintf(pos, end - pos, "%sSHARED",
  1243. first ? "" : " ");
  1244. if (ret < 0 || ret >= end - pos)
  1245. return pos - buf;
  1246. pos += ret;
  1247. first = 0;
  1248. }
  1249. if (capa->auth & (WPA_DRIVER_AUTH_LEAP)) {
  1250. ret = os_snprintf(pos, end - pos, "%sLEAP", first ? "" : " ");
  1251. if (ret < 0 || ret >= end - pos)
  1252. return pos - buf;
  1253. pos += ret;
  1254. first = 0;
  1255. }
  1256. return pos - buf;
  1257. }
  1258. static int wpa_supplicant_ctrl_iface_get_capability(
  1259. struct wpa_supplicant *wpa_s, const char *_field, char *buf,
  1260. size_t buflen)
  1261. {
  1262. struct wpa_driver_capa capa;
  1263. int res;
  1264. char *strict;
  1265. char field[30];
  1266. size_t len;
  1267. /* Determine whether or not strict checking was requested */
  1268. len = os_strlcpy(field, _field, sizeof(field));
  1269. if (len >= sizeof(field))
  1270. return -1;
  1271. strict = os_strchr(field, ' ');
  1272. if (strict != NULL) {
  1273. *strict++ = '\0';
  1274. if (os_strcmp(strict, "strict") != 0)
  1275. return -1;
  1276. }
  1277. wpa_printf(MSG_DEBUG, "CTRL_IFACE: GET_CAPABILITY '%s' %s",
  1278. field, strict ? strict : "");
  1279. if (os_strcmp(field, "eap") == 0) {
  1280. return eap_get_names(buf, buflen);
  1281. }
  1282. res = wpa_drv_get_capa(wpa_s, &capa);
  1283. if (os_strcmp(field, "pairwise") == 0)
  1284. return ctrl_iface_get_capability_pairwise(res, strict, &capa,
  1285. buf, buflen);
  1286. if (os_strcmp(field, "group") == 0)
  1287. return ctrl_iface_get_capability_group(res, strict, &capa,
  1288. buf, buflen);
  1289. if (os_strcmp(field, "key_mgmt") == 0)
  1290. return ctrl_iface_get_capability_key_mgmt(res, strict, &capa,
  1291. buf, buflen);
  1292. if (os_strcmp(field, "proto") == 0)
  1293. return ctrl_iface_get_capability_proto(res, strict, &capa,
  1294. buf, buflen);
  1295. if (os_strcmp(field, "auth_alg") == 0)
  1296. return ctrl_iface_get_capability_auth_alg(res, strict, &capa,
  1297. buf, buflen);
  1298. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Unknown GET_CAPABILITY field '%s'",
  1299. field);
  1300. return -1;
  1301. }
  1302. static int wpa_supplicant_ctrl_iface_bss(struct wpa_supplicant *wpa_s,
  1303. const char *cmd, char *buf,
  1304. size_t buflen)
  1305. {
  1306. u8 bssid[ETH_ALEN];
  1307. size_t i;
  1308. struct wpa_bss *bss;
  1309. int ret;
  1310. char *pos, *end;
  1311. const u8 *ie, *ie2;
  1312. if (os_strcmp(cmd, "FIRST") == 0)
  1313. bss = dl_list_first(&wpa_s->bss, struct wpa_bss, list);
  1314. else if (os_strncmp(cmd, "ID-", 3) == 0) {
  1315. i = atoi(cmd + 3);
  1316. bss = wpa_bss_get_id(wpa_s, i);
  1317. } else if (os_strncmp(cmd, "NEXT-", 5) == 0) {
  1318. i = atoi(cmd + 5);
  1319. bss = wpa_bss_get_id(wpa_s, i);
  1320. if (bss) {
  1321. struct dl_list *next = bss->list_id.next;
  1322. if (next == &wpa_s->bss_id)
  1323. bss = NULL;
  1324. else
  1325. bss = dl_list_entry(next, struct wpa_bss,
  1326. list_id);
  1327. }
  1328. } else if (hwaddr_aton(cmd, bssid) == 0)
  1329. bss = wpa_bss_get_bssid(wpa_s, bssid);
  1330. else {
  1331. struct wpa_bss *tmp;
  1332. i = atoi(cmd);
  1333. bss = NULL;
  1334. dl_list_for_each(tmp, &wpa_s->bss_id, struct wpa_bss, list_id)
  1335. {
  1336. if (i-- == 0) {
  1337. bss = tmp;
  1338. break;
  1339. }
  1340. }
  1341. }
  1342. if (bss == NULL)
  1343. return 0;
  1344. pos = buf;
  1345. end = buf + buflen;
  1346. ret = os_snprintf(pos, end - pos,
  1347. "id=%u\n"
  1348. "bssid=" MACSTR "\n"
  1349. "freq=%d\n"
  1350. "beacon_int=%d\n"
  1351. "capabilities=0x%04x\n"
  1352. "qual=%d\n"
  1353. "noise=%d\n"
  1354. "level=%d\n"
  1355. "tsf=%016llu\n"
  1356. "ie=",
  1357. bss->id,
  1358. MAC2STR(bss->bssid), bss->freq, bss->beacon_int,
  1359. bss->caps, bss->qual, bss->noise, bss->level,
  1360. (unsigned long long) bss->tsf);
  1361. if (ret < 0 || ret >= end - pos)
  1362. return pos - buf;
  1363. pos += ret;
  1364. ie = (const u8 *) (bss + 1);
  1365. for (i = 0; i < bss->ie_len; i++) {
  1366. ret = os_snprintf(pos, end - pos, "%02x", *ie++);
  1367. if (ret < 0 || ret >= end - pos)
  1368. return pos - buf;
  1369. pos += ret;
  1370. }
  1371. ret = os_snprintf(pos, end - pos, "\n");
  1372. if (ret < 0 || ret >= end - pos)
  1373. return pos - buf;
  1374. pos += ret;
  1375. ret = os_snprintf(pos, end - pos, "flags=");
  1376. if (ret < 0 || ret >= end - pos)
  1377. return pos - buf;
  1378. pos += ret;
  1379. ie = wpa_bss_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
  1380. if (ie)
  1381. pos = wpa_supplicant_ie_txt(pos, end, "WPA", ie, 2 + ie[1]);
  1382. ie2 = wpa_bss_get_ie(bss, WLAN_EID_RSN);
  1383. if (ie2)
  1384. pos = wpa_supplicant_ie_txt(pos, end, "WPA2", ie2, 2 + ie2[1]);
  1385. pos = wpa_supplicant_wps_ie_txt(wpa_s, pos, end, bss);
  1386. if (!ie && !ie2 && bss->caps & IEEE80211_CAP_PRIVACY) {
  1387. ret = os_snprintf(pos, end - pos, "[WEP]");
  1388. if (ret < 0 || ret >= end - pos)
  1389. return pos - buf;
  1390. pos += ret;
  1391. }
  1392. if (bss->caps & IEEE80211_CAP_IBSS) {
  1393. ret = os_snprintf(pos, end - pos, "[IBSS]");
  1394. if (ret < 0 || ret >= end - pos)
  1395. return pos - buf;
  1396. pos += ret;
  1397. }
  1398. if (bss->caps & IEEE80211_CAP_ESS) {
  1399. ret = os_snprintf(pos, end - pos, "[ESS]");
  1400. if (ret < 0 || ret >= end - pos)
  1401. return pos - buf;
  1402. pos += ret;
  1403. }
  1404. ret = os_snprintf(pos, end - pos, "\n");
  1405. if (ret < 0 || ret >= end - pos)
  1406. return pos - buf;
  1407. pos += ret;
  1408. ret = os_snprintf(pos, end - pos, "ssid=%s\n",
  1409. wpa_ssid_txt(bss->ssid, bss->ssid_len));
  1410. if (ret < 0 || ret >= end - pos)
  1411. return pos - buf;
  1412. pos += ret;
  1413. #ifdef CONFIG_WPS
  1414. ie = (const u8 *) (bss + 1);
  1415. ret = wpas_wps_scan_result_text(ie, bss->ie_len, pos, end);
  1416. if (ret < 0 || ret >= end - pos)
  1417. return pos - buf;
  1418. pos += ret;
  1419. #endif /* CONFIG_WPS */
  1420. return pos - buf;
  1421. }
  1422. static int wpa_supplicant_ctrl_iface_ap_scan(
  1423. struct wpa_supplicant *wpa_s, char *cmd)
  1424. {
  1425. int ap_scan = atoi(cmd);
  1426. return wpa_supplicant_set_ap_scan(wpa_s, ap_scan);
  1427. }
  1428. static void wpa_supplicant_ctrl_iface_drop_sa(struct wpa_supplicant *wpa_s)
  1429. {
  1430. u8 *bcast = (u8 *) "\xff\xff\xff\xff\xff\xff";
  1431. wpa_printf(MSG_DEBUG, "Dropping SA without deauthentication");
  1432. /* MLME-DELETEKEYS.request */
  1433. wpa_drv_set_key(wpa_s, WPA_ALG_NONE, bcast, 0, 0, NULL, 0, NULL, 0);
  1434. wpa_drv_set_key(wpa_s, WPA_ALG_NONE, bcast, 1, 0, NULL, 0, NULL, 0);
  1435. wpa_drv_set_key(wpa_s, WPA_ALG_NONE, bcast, 2, 0, NULL, 0, NULL, 0);
  1436. wpa_drv_set_key(wpa_s, WPA_ALG_NONE, bcast, 3, 0, NULL, 0, NULL, 0);
  1437. #ifdef CONFIG_IEEE80211W
  1438. wpa_drv_set_key(wpa_s, WPA_ALG_NONE, bcast, 4, 0, NULL, 0, NULL, 0);
  1439. wpa_drv_set_key(wpa_s, WPA_ALG_NONE, bcast, 5, 0, NULL, 0, NULL, 0);
  1440. #endif /* CONFIG_IEEE80211W */
  1441. wpa_drv_set_key(wpa_s, WPA_ALG_NONE, wpa_s->bssid, 0, 0, NULL, 0, NULL,
  1442. 0);
  1443. /* MLME-SETPROTECTION.request(None) */
  1444. wpa_drv_mlme_setprotection(wpa_s, wpa_s->bssid,
  1445. MLME_SETPROTECTION_PROTECT_TYPE_NONE,
  1446. MLME_SETPROTECTION_KEY_TYPE_PAIRWISE);
  1447. wpa_sm_drop_sa(wpa_s->wpa);
  1448. }
  1449. static int wpa_supplicant_ctrl_iface_roam(struct wpa_supplicant *wpa_s,
  1450. char *addr)
  1451. {
  1452. u8 bssid[ETH_ALEN];
  1453. struct wpa_bss *bss;
  1454. struct wpa_ssid *ssid = wpa_s->current_ssid;
  1455. if (hwaddr_aton(addr, bssid)) {
  1456. wpa_printf(MSG_DEBUG, "CTRL_IFACE ROAM: invalid "
  1457. "address '%s'", addr);
  1458. return -1;
  1459. }
  1460. wpa_printf(MSG_DEBUG, "CTRL_IFACE ROAM " MACSTR, MAC2STR(bssid));
  1461. bss = wpa_bss_get_bssid(wpa_s, bssid);
  1462. if (!bss) {
  1463. wpa_printf(MSG_DEBUG, "CTRL_IFACE ROAM: Target AP not found "
  1464. "from BSS table");
  1465. return -1;
  1466. }
  1467. /*
  1468. * TODO: Find best network configuration block from configuration to
  1469. * allow roaming to other networks
  1470. */
  1471. if (!ssid) {
  1472. wpa_printf(MSG_DEBUG, "CTRL_IFACE ROAM: No network "
  1473. "configuration known for the target AP");
  1474. return -1;
  1475. }
  1476. wpa_s->reassociate = 1;
  1477. wpa_supplicant_connect(wpa_s, bss, ssid);
  1478. return 0;
  1479. }
  1480. char * wpa_supplicant_ctrl_iface_process(struct wpa_supplicant *wpa_s,
  1481. char *buf, size_t *resp_len)
  1482. {
  1483. char *reply;
  1484. const int reply_size = 2048;
  1485. int ctrl_rsp = 0;
  1486. int reply_len;
  1487. if (os_strncmp(buf, WPA_CTRL_RSP, os_strlen(WPA_CTRL_RSP)) == 0 ||
  1488. os_strncmp(buf, "SET_NETWORK ", 12) == 0) {
  1489. wpa_hexdump_ascii_key(MSG_DEBUG, "RX ctrl_iface",
  1490. (const u8 *) buf, os_strlen(buf));
  1491. } else {
  1492. wpa_hexdump_ascii(MSG_DEBUG, "RX ctrl_iface",
  1493. (const u8 *) buf, os_strlen(buf));
  1494. }
  1495. reply = os_malloc(reply_size);
  1496. if (reply == NULL) {
  1497. *resp_len = 1;
  1498. return NULL;
  1499. }
  1500. os_memcpy(reply, "OK\n", 3);
  1501. reply_len = 3;
  1502. if (os_strcmp(buf, "PING") == 0) {
  1503. os_memcpy(reply, "PONG\n", 5);
  1504. reply_len = 5;
  1505. } else if (os_strncmp(buf, "NOTE ", 5) == 0) {
  1506. wpa_printf(MSG_INFO, "NOTE: %s", buf + 5);
  1507. } else if (os_strcmp(buf, "MIB") == 0) {
  1508. reply_len = wpa_sm_get_mib(wpa_s->wpa, reply, reply_size);
  1509. if (reply_len >= 0) {
  1510. int res;
  1511. res = eapol_sm_get_mib(wpa_s->eapol, reply + reply_len,
  1512. reply_size - reply_len);
  1513. if (res < 0)
  1514. reply_len = -1;
  1515. else
  1516. reply_len += res;
  1517. }
  1518. } else if (os_strncmp(buf, "STATUS", 6) == 0) {
  1519. reply_len = wpa_supplicant_ctrl_iface_status(
  1520. wpa_s, buf + 6, reply, reply_size);
  1521. } else if (os_strcmp(buf, "PMKSA") == 0) {
  1522. reply_len = wpa_sm_pmksa_cache_list(wpa_s->wpa, reply,
  1523. reply_size);
  1524. } else if (os_strncmp(buf, "SET ", 4) == 0) {
  1525. if (wpa_supplicant_ctrl_iface_set(wpa_s, buf + 4))
  1526. reply_len = -1;
  1527. } else if (os_strcmp(buf, "LOGON") == 0) {
  1528. eapol_sm_notify_logoff(wpa_s->eapol, FALSE);
  1529. } else if (os_strcmp(buf, "LOGOFF") == 0) {
  1530. eapol_sm_notify_logoff(wpa_s->eapol, TRUE);
  1531. } else if (os_strcmp(buf, "REASSOCIATE") == 0) {
  1532. if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED)
  1533. reply_len = -1;
  1534. else {
  1535. wpa_s->disconnected = 0;
  1536. wpa_s->reassociate = 1;
  1537. wpa_supplicant_req_scan(wpa_s, 0, 0);
  1538. }
  1539. } else if (os_strcmp(buf, "RECONNECT") == 0) {
  1540. if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED)
  1541. reply_len = -1;
  1542. else if (wpa_s->disconnected) {
  1543. wpa_s->disconnected = 0;
  1544. wpa_s->reassociate = 1;
  1545. wpa_supplicant_req_scan(wpa_s, 0, 0);
  1546. }
  1547. #ifdef IEEE8021X_EAPOL
  1548. } else if (os_strncmp(buf, "PREAUTH ", 8) == 0) {
  1549. if (wpa_supplicant_ctrl_iface_preauth(wpa_s, buf + 8))
  1550. reply_len = -1;
  1551. #endif /* IEEE8021X_EAPOL */
  1552. #ifdef CONFIG_PEERKEY
  1553. } else if (os_strncmp(buf, "STKSTART ", 9) == 0) {
  1554. if (wpa_supplicant_ctrl_iface_stkstart(wpa_s, buf + 9))
  1555. reply_len = -1;
  1556. #endif /* CONFIG_PEERKEY */
  1557. #ifdef CONFIG_IEEE80211R
  1558. } else if (os_strncmp(buf, "FT_DS ", 6) == 0) {
  1559. if (wpa_supplicant_ctrl_iface_ft_ds(wpa_s, buf + 6))
  1560. reply_len = -1;
  1561. #endif /* CONFIG_IEEE80211R */
  1562. #ifdef CONFIG_WPS
  1563. } else if (os_strcmp(buf, "WPS_PBC") == 0) {
  1564. if (wpa_supplicant_ctrl_iface_wps_pbc(wpa_s, NULL))
  1565. reply_len = -1;
  1566. } else if (os_strncmp(buf, "WPS_PBC ", 8) == 0) {
  1567. if (wpa_supplicant_ctrl_iface_wps_pbc(wpa_s, buf + 8))
  1568. reply_len = -1;
  1569. } else if (os_strncmp(buf, "WPS_PIN ", 8) == 0) {
  1570. reply_len = wpa_supplicant_ctrl_iface_wps_pin(wpa_s, buf + 8,
  1571. reply,
  1572. reply_size);
  1573. #ifdef CONFIG_WPS_OOB
  1574. } else if (os_strncmp(buf, "WPS_OOB ", 8) == 0) {
  1575. if (wpa_supplicant_ctrl_iface_wps_oob(wpa_s, buf + 8))
  1576. reply_len = -1;
  1577. #endif /* CONFIG_WPS_OOB */
  1578. } else if (os_strncmp(buf, "WPS_REG ", 8) == 0) {
  1579. if (wpa_supplicant_ctrl_iface_wps_reg(wpa_s, buf + 8))
  1580. reply_len = -1;
  1581. #ifdef CONFIG_WPS_ER
  1582. } else if (os_strcmp(buf, "WPS_ER_START") == 0) {
  1583. if (wpas_wps_er_start(wpa_s, NULL))
  1584. reply_len = -1;
  1585. } else if (os_strncmp(buf, "WPS_ER_START ", 13) == 0) {
  1586. if (wpas_wps_er_start(wpa_s, buf + 13))
  1587. reply_len = -1;
  1588. } else if (os_strcmp(buf, "WPS_ER_STOP") == 0) {
  1589. if (wpas_wps_er_stop(wpa_s))
  1590. reply_len = -1;
  1591. } else if (os_strncmp(buf, "WPS_ER_PIN ", 11) == 0) {
  1592. if (wpa_supplicant_ctrl_iface_wps_er_pin(wpa_s, buf + 11))
  1593. reply_len = -1;
  1594. } else if (os_strncmp(buf, "WPS_ER_PBC ", 11) == 0) {
  1595. if (wpas_wps_er_pbc(wpa_s, buf + 11))
  1596. reply_len = -1;
  1597. } else if (os_strncmp(buf, "WPS_ER_LEARN ", 13) == 0) {
  1598. if (wpa_supplicant_ctrl_iface_wps_er_learn(wpa_s, buf + 13))
  1599. reply_len = -1;
  1600. } else if (os_strncmp(buf, "WPS_ER_CONFIG ", 14) == 0) {
  1601. if (wpa_supplicant_ctrl_iface_wps_er_config(wpa_s, buf + 14))
  1602. reply_len = -1;
  1603. #endif /* CONFIG_WPS_ER */
  1604. #endif /* CONFIG_WPS */
  1605. #ifdef CONFIG_IBSS_RSN
  1606. } else if (os_strncmp(buf, "IBSS_RSN ", 9) == 0) {
  1607. if (wpa_supplicant_ctrl_iface_ibss_rsn(wpa_s, buf + 9))
  1608. reply_len = -1;
  1609. #endif /* CONFIG_IBSS_RSN */
  1610. } else if (os_strncmp(buf, WPA_CTRL_RSP, os_strlen(WPA_CTRL_RSP)) == 0)
  1611. {
  1612. if (wpa_supplicant_ctrl_iface_ctrl_rsp(
  1613. wpa_s, buf + os_strlen(WPA_CTRL_RSP)))
  1614. reply_len = -1;
  1615. else
  1616. ctrl_rsp = 1;
  1617. } else if (os_strcmp(buf, "RECONFIGURE") == 0) {
  1618. if (wpa_supplicant_reload_configuration(wpa_s))
  1619. reply_len = -1;
  1620. } else if (os_strcmp(buf, "TERMINATE") == 0) {
  1621. wpa_supplicant_terminate_proc(wpa_s->global);
  1622. } else if (os_strncmp(buf, "BSSID ", 6) == 0) {
  1623. if (wpa_supplicant_ctrl_iface_bssid(wpa_s, buf + 6))
  1624. reply_len = -1;
  1625. } else if (os_strcmp(buf, "LIST_NETWORKS") == 0) {
  1626. reply_len = wpa_supplicant_ctrl_iface_list_networks(
  1627. wpa_s, reply, reply_size);
  1628. } else if (os_strcmp(buf, "DISCONNECT") == 0) {
  1629. wpa_s->reassociate = 0;
  1630. wpa_s->disconnected = 1;
  1631. wpa_supplicant_deauthenticate(wpa_s,
  1632. WLAN_REASON_DEAUTH_LEAVING);
  1633. } else if (os_strcmp(buf, "SCAN") == 0) {
  1634. if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED)
  1635. reply_len = -1;
  1636. else {
  1637. wpa_s->scan_req = 2;
  1638. wpa_supplicant_req_scan(wpa_s, 0, 0);
  1639. }
  1640. } else if (os_strcmp(buf, "SCAN_RESULTS") == 0) {
  1641. reply_len = wpa_supplicant_ctrl_iface_scan_results(
  1642. wpa_s, reply, reply_size);
  1643. } else if (os_strncmp(buf, "SELECT_NETWORK ", 15) == 0) {
  1644. if (wpa_supplicant_ctrl_iface_select_network(wpa_s, buf + 15))
  1645. reply_len = -1;
  1646. } else if (os_strncmp(buf, "ENABLE_NETWORK ", 15) == 0) {
  1647. if (wpa_supplicant_ctrl_iface_enable_network(wpa_s, buf + 15))
  1648. reply_len = -1;
  1649. } else if (os_strncmp(buf, "DISABLE_NETWORK ", 16) == 0) {
  1650. if (wpa_supplicant_ctrl_iface_disable_network(wpa_s, buf + 16))
  1651. reply_len = -1;
  1652. } else if (os_strcmp(buf, "ADD_NETWORK") == 0) {
  1653. reply_len = wpa_supplicant_ctrl_iface_add_network(
  1654. wpa_s, reply, reply_size);
  1655. } else if (os_strncmp(buf, "REMOVE_NETWORK ", 15) == 0) {
  1656. if (wpa_supplicant_ctrl_iface_remove_network(wpa_s, buf + 15))
  1657. reply_len = -1;
  1658. } else if (os_strncmp(buf, "SET_NETWORK ", 12) == 0) {
  1659. if (wpa_supplicant_ctrl_iface_set_network(wpa_s, buf + 12))
  1660. reply_len = -1;
  1661. } else if (os_strncmp(buf, "GET_NETWORK ", 12) == 0) {
  1662. reply_len = wpa_supplicant_ctrl_iface_get_network(
  1663. wpa_s, buf + 12, reply, reply_size);
  1664. #ifndef CONFIG_NO_CONFIG_WRITE
  1665. } else if (os_strcmp(buf, "SAVE_CONFIG") == 0) {
  1666. if (wpa_supplicant_ctrl_iface_save_config(wpa_s))
  1667. reply_len = -1;
  1668. #endif /* CONFIG_NO_CONFIG_WRITE */
  1669. } else if (os_strncmp(buf, "GET_CAPABILITY ", 15) == 0) {
  1670. reply_len = wpa_supplicant_ctrl_iface_get_capability(
  1671. wpa_s, buf + 15, reply, reply_size);
  1672. } else if (os_strncmp(buf, "AP_SCAN ", 8) == 0) {
  1673. if (wpa_supplicant_ctrl_iface_ap_scan(wpa_s, buf + 8))
  1674. reply_len = -1;
  1675. } else if (os_strcmp(buf, "INTERFACE_LIST") == 0) {
  1676. reply_len = wpa_supplicant_global_iface_list(
  1677. wpa_s->global, reply, reply_size);
  1678. } else if (os_strcmp(buf, "INTERFACES") == 0) {
  1679. reply_len = wpa_supplicant_global_iface_interfaces(
  1680. wpa_s->global, reply, reply_size);
  1681. } else if (os_strncmp(buf, "BSS ", 4) == 0) {
  1682. reply_len = wpa_supplicant_ctrl_iface_bss(
  1683. wpa_s, buf + 4, reply, reply_size);
  1684. #ifdef CONFIG_AP
  1685. } else if (os_strcmp(buf, "STA-FIRST") == 0) {
  1686. reply_len = ap_ctrl_iface_sta_first(wpa_s, reply, reply_size);
  1687. } else if (os_strncmp(buf, "STA ", 4) == 0) {
  1688. reply_len = ap_ctrl_iface_sta(wpa_s, buf + 4, reply,
  1689. reply_size);
  1690. } else if (os_strncmp(buf, "STA-NEXT ", 9) == 0) {
  1691. reply_len = ap_ctrl_iface_sta_next(wpa_s, buf + 9, reply,
  1692. reply_size);
  1693. #endif /* CONFIG_AP */
  1694. } else if (os_strcmp(buf, "SUSPEND") == 0) {
  1695. wpas_notify_suspend(wpa_s->global);
  1696. } else if (os_strcmp(buf, "RESUME") == 0) {
  1697. wpas_notify_resume(wpa_s->global);
  1698. } else if (os_strcmp(buf, "DROP_SA") == 0) {
  1699. wpa_supplicant_ctrl_iface_drop_sa(wpa_s);
  1700. } else if (os_strncmp(buf, "ROAM ", 5) == 0) {
  1701. if (wpa_supplicant_ctrl_iface_roam(wpa_s, buf + 5))
  1702. reply_len = -1;
  1703. } else {
  1704. os_memcpy(reply, "UNKNOWN COMMAND\n", 16);
  1705. reply_len = 16;
  1706. }
  1707. if (reply_len < 0) {
  1708. os_memcpy(reply, "FAIL\n", 5);
  1709. reply_len = 5;
  1710. }
  1711. if (ctrl_rsp)
  1712. eapol_sm_notify_ctrl_response(wpa_s->eapol);
  1713. *resp_len = reply_len;
  1714. return reply;
  1715. }
  1716. static int wpa_supplicant_global_iface_add(struct wpa_global *global,
  1717. char *cmd)
  1718. {
  1719. struct wpa_interface iface;
  1720. char *pos;
  1721. /*
  1722. * <ifname>TAB<confname>TAB<driver>TAB<ctrl_interface>TAB<driver_param>
  1723. * TAB<bridge_ifname>
  1724. */
  1725. wpa_printf(MSG_DEBUG, "CTRL_IFACE GLOBAL INTERFACE_ADD '%s'", cmd);
  1726. os_memset(&iface, 0, sizeof(iface));
  1727. do {
  1728. iface.ifname = pos = cmd;
  1729. pos = os_strchr(pos, '\t');
  1730. if (pos)
  1731. *pos++ = '\0';
  1732. if (iface.ifname[0] == '\0')
  1733. return -1;
  1734. if (pos == NULL)
  1735. break;
  1736. iface.confname = pos;
  1737. pos = os_strchr(pos, '\t');
  1738. if (pos)
  1739. *pos++ = '\0';
  1740. if (iface.confname[0] == '\0')
  1741. iface.confname = NULL;
  1742. if (pos == NULL)
  1743. break;
  1744. iface.driver = pos;
  1745. pos = os_strchr(pos, '\t');
  1746. if (pos)
  1747. *pos++ = '\0';
  1748. if (iface.driver[0] == '\0')
  1749. iface.driver = NULL;
  1750. if (pos == NULL)
  1751. break;
  1752. iface.ctrl_interface = pos;
  1753. pos = os_strchr(pos, '\t');
  1754. if (pos)
  1755. *pos++ = '\0';
  1756. if (iface.ctrl_interface[0] == '\0')
  1757. iface.ctrl_interface = NULL;
  1758. if (pos == NULL)
  1759. break;
  1760. iface.driver_param = pos;
  1761. pos = os_strchr(pos, '\t');
  1762. if (pos)
  1763. *pos++ = '\0';
  1764. if (iface.driver_param[0] == '\0')
  1765. iface.driver_param = NULL;
  1766. if (pos == NULL)
  1767. break;
  1768. iface.bridge_ifname = pos;
  1769. pos = os_strchr(pos, '\t');
  1770. if (pos)
  1771. *pos++ = '\0';
  1772. if (iface.bridge_ifname[0] == '\0')
  1773. iface.bridge_ifname = NULL;
  1774. if (pos == NULL)
  1775. break;
  1776. } while (0);
  1777. if (wpa_supplicant_get_iface(global, iface.ifname))
  1778. return -1;
  1779. return wpa_supplicant_add_iface(global, &iface) ? 0 : -1;
  1780. }
  1781. static int wpa_supplicant_global_iface_remove(struct wpa_global *global,
  1782. char *cmd)
  1783. {
  1784. struct wpa_supplicant *wpa_s;
  1785. wpa_printf(MSG_DEBUG, "CTRL_IFACE GLOBAL INTERFACE_REMOVE '%s'", cmd);
  1786. wpa_s = wpa_supplicant_get_iface(global, cmd);
  1787. if (wpa_s == NULL)
  1788. return -1;
  1789. return wpa_supplicant_remove_iface(global, wpa_s);
  1790. }
  1791. static void wpa_free_iface_info(struct wpa_interface_info *iface)
  1792. {
  1793. struct wpa_interface_info *prev;
  1794. while (iface) {
  1795. prev = iface;
  1796. iface = iface->next;
  1797. os_free(prev->ifname);
  1798. os_free(prev->desc);
  1799. os_free(prev);
  1800. }
  1801. }
  1802. static int wpa_supplicant_global_iface_list(struct wpa_global *global,
  1803. char *buf, int len)
  1804. {
  1805. int i, res;
  1806. struct wpa_interface_info *iface = NULL, *last = NULL, *tmp;
  1807. char *pos, *end;
  1808. for (i = 0; wpa_drivers[i]; i++) {
  1809. struct wpa_driver_ops *drv = wpa_drivers[i];
  1810. if (drv->get_interfaces == NULL)
  1811. continue;
  1812. tmp = drv->get_interfaces(global->drv_priv[i]);
  1813. if (tmp == NULL)
  1814. continue;
  1815. if (last == NULL)
  1816. iface = last = tmp;
  1817. else
  1818. last->next = tmp;
  1819. while (last->next)
  1820. last = last->next;
  1821. }
  1822. pos = buf;
  1823. end = buf + len;
  1824. for (tmp = iface; tmp; tmp = tmp->next) {
  1825. res = os_snprintf(pos, end - pos, "%s\t%s\t%s\n",
  1826. tmp->drv_name, tmp->ifname,
  1827. tmp->desc ? tmp->desc : "");
  1828. if (res < 0 || res >= end - pos) {
  1829. *pos = '\0';
  1830. break;
  1831. }
  1832. pos += res;
  1833. }
  1834. wpa_free_iface_info(iface);
  1835. return pos - buf;
  1836. }
  1837. static int wpa_supplicant_global_iface_interfaces(struct wpa_global *global,
  1838. char *buf, int len)
  1839. {
  1840. int res;
  1841. char *pos, *end;
  1842. struct wpa_supplicant *wpa_s;
  1843. wpa_s = global->ifaces;
  1844. pos = buf;
  1845. end = buf + len;
  1846. while (wpa_s) {
  1847. res = os_snprintf(pos, end - pos, "%s\n", wpa_s->ifname);
  1848. if (res < 0 || res >= end - pos) {
  1849. *pos = '\0';
  1850. break;
  1851. }
  1852. pos += res;
  1853. wpa_s = wpa_s->next;
  1854. }
  1855. return pos - buf;
  1856. }
  1857. char * wpa_supplicant_global_ctrl_iface_process(struct wpa_global *global,
  1858. char *buf, size_t *resp_len)
  1859. {
  1860. char *reply;
  1861. const int reply_size = 2048;
  1862. int reply_len;
  1863. wpa_hexdump_ascii(MSG_DEBUG, "RX global ctrl_iface",
  1864. (const u8 *) buf, os_strlen(buf));
  1865. reply = os_malloc(reply_size);
  1866. if (reply == NULL) {
  1867. *resp_len = 1;
  1868. return NULL;
  1869. }
  1870. os_memcpy(reply, "OK\n", 3);
  1871. reply_len = 3;
  1872. if (os_strcmp(buf, "PING") == 0) {
  1873. os_memcpy(reply, "PONG\n", 5);
  1874. reply_len = 5;
  1875. } else if (os_strncmp(buf, "INTERFACE_ADD ", 14) == 0) {
  1876. if (wpa_supplicant_global_iface_add(global, buf + 14))
  1877. reply_len = -1;
  1878. } else if (os_strncmp(buf, "INTERFACE_REMOVE ", 17) == 0) {
  1879. if (wpa_supplicant_global_iface_remove(global, buf + 17))
  1880. reply_len = -1;
  1881. } else if (os_strcmp(buf, "INTERFACE_LIST") == 0) {
  1882. reply_len = wpa_supplicant_global_iface_list(
  1883. global, reply, reply_size);
  1884. } else if (os_strcmp(buf, "INTERFACES") == 0) {
  1885. reply_len = wpa_supplicant_global_iface_interfaces(
  1886. global, reply, reply_size);
  1887. } else if (os_strcmp(buf, "TERMINATE") == 0) {
  1888. wpa_supplicant_terminate_proc(global);
  1889. } else if (os_strcmp(buf, "SUSPEND") == 0) {
  1890. wpas_notify_suspend(global);
  1891. } else if (os_strcmp(buf, "RESUME") == 0) {
  1892. wpas_notify_resume(global);
  1893. } else {
  1894. os_memcpy(reply, "UNKNOWN COMMAND\n", 16);
  1895. reply_len = 16;
  1896. }
  1897. if (reply_len < 0) {
  1898. os_memcpy(reply, "FAIL\n", 5);
  1899. reply_len = 5;
  1900. }
  1901. *resp_len = reply_len;
  1902. return reply;
  1903. }