ctrl_iface.c 53 KB

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