ctrl_iface.c 50 KB

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