ctrl_iface.c 49 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060
  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 0:
  407. ret = os_snprintf(pos, end - pos,
  408. "mode=station\n");
  409. break;
  410. case 1:
  411. ret = os_snprintf(pos, end - pos,
  412. "mode=IBSS\n");
  413. break;
  414. case 2:
  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. ie = wpa_scan_get_ie(res, WLAN_EID_SSID);
  741. ret = os_snprintf(pos, end - pos, "\t%s",
  742. ie ? wpa_ssid_txt(ie + 2, ie[1]) : "");
  743. if (ret < 0 || ret >= end - pos)
  744. return pos - buf;
  745. pos += ret;
  746. ret = os_snprintf(pos, end - pos, "\n");
  747. if (ret < 0 || ret >= end - pos)
  748. return pos - buf;
  749. pos += ret;
  750. return pos - buf;
  751. }
  752. static int wpa_supplicant_ctrl_iface_scan_results(
  753. struct wpa_supplicant *wpa_s, char *buf, size_t buflen)
  754. {
  755. char *pos, *end;
  756. struct wpa_scan_res *res;
  757. int ret;
  758. size_t i;
  759. if (wpa_s->scan_res == NULL &&
  760. wpa_supplicant_get_scan_results(wpa_s) < 0)
  761. return 0;
  762. pos = buf;
  763. end = buf + buflen;
  764. ret = os_snprintf(pos, end - pos, "bssid / frequency / signal level / "
  765. "flags / ssid\n");
  766. if (ret < 0 || ret >= end - pos)
  767. return pos - buf;
  768. pos += ret;
  769. for (i = 0; i < wpa_s->scan_res->num; i++) {
  770. res = wpa_s->scan_res->res[i];
  771. ret = wpa_supplicant_ctrl_iface_scan_result(res, pos,
  772. end - pos);
  773. if (ret < 0 || ret >= end - pos)
  774. return pos - buf;
  775. pos += ret;
  776. }
  777. return pos - buf;
  778. }
  779. static int wpa_supplicant_ctrl_iface_select_network(
  780. struct wpa_supplicant *wpa_s, char *cmd)
  781. {
  782. int id;
  783. struct wpa_ssid *ssid;
  784. /* cmd: "<network id>" or "any" */
  785. if (os_strcmp(cmd, "any") == 0) {
  786. wpa_printf(MSG_DEBUG, "CTRL_IFACE: SELECT_NETWORK any");
  787. ssid = NULL;
  788. } else {
  789. id = atoi(cmd);
  790. wpa_printf(MSG_DEBUG, "CTRL_IFACE: SELECT_NETWORK id=%d", id);
  791. ssid = wpa_config_get_network(wpa_s->conf, id);
  792. if (ssid == NULL) {
  793. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find "
  794. "network id=%d", id);
  795. return -1;
  796. }
  797. }
  798. wpa_supplicant_select_network(wpa_s, ssid);
  799. return 0;
  800. }
  801. static int wpa_supplicant_ctrl_iface_enable_network(
  802. struct wpa_supplicant *wpa_s, char *cmd)
  803. {
  804. int id;
  805. struct wpa_ssid *ssid;
  806. /* cmd: "<network id>" or "all" */
  807. if (os_strcmp(cmd, "all") == 0) {
  808. wpa_printf(MSG_DEBUG, "CTRL_IFACE: ENABLE_NETWORK all");
  809. ssid = NULL;
  810. } else {
  811. id = atoi(cmd);
  812. wpa_printf(MSG_DEBUG, "CTRL_IFACE: ENABLE_NETWORK id=%d", id);
  813. ssid = wpa_config_get_network(wpa_s->conf, id);
  814. if (ssid == NULL) {
  815. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find "
  816. "network id=%d", id);
  817. return -1;
  818. }
  819. }
  820. wpa_supplicant_enable_network(wpa_s, ssid);
  821. return 0;
  822. }
  823. static int wpa_supplicant_ctrl_iface_disable_network(
  824. struct wpa_supplicant *wpa_s, char *cmd)
  825. {
  826. int id;
  827. struct wpa_ssid *ssid;
  828. /* cmd: "<network id>" or "all" */
  829. if (os_strcmp(cmd, "all") == 0) {
  830. wpa_printf(MSG_DEBUG, "CTRL_IFACE: DISABLE_NETWORK all");
  831. ssid = NULL;
  832. } else {
  833. id = atoi(cmd);
  834. wpa_printf(MSG_DEBUG, "CTRL_IFACE: DISABLE_NETWORK id=%d", id);
  835. ssid = wpa_config_get_network(wpa_s->conf, id);
  836. if (ssid == NULL) {
  837. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find "
  838. "network id=%d", id);
  839. return -1;
  840. }
  841. }
  842. wpa_supplicant_disable_network(wpa_s, ssid);
  843. return 0;
  844. }
  845. static int wpa_supplicant_ctrl_iface_add_network(
  846. struct wpa_supplicant *wpa_s, char *buf, size_t buflen)
  847. {
  848. struct wpa_ssid *ssid;
  849. int ret;
  850. wpa_printf(MSG_DEBUG, "CTRL_IFACE: ADD_NETWORK");
  851. ssid = wpa_config_add_network(wpa_s->conf);
  852. if (ssid == NULL)
  853. return -1;
  854. wpas_notify_network_added(wpa_s, ssid);
  855. ssid->disabled = 1;
  856. wpa_config_set_network_defaults(ssid);
  857. ret = os_snprintf(buf, buflen, "%d\n", ssid->id);
  858. if (ret < 0 || (size_t) ret >= buflen)
  859. return -1;
  860. return ret;
  861. }
  862. static int wpa_supplicant_ctrl_iface_remove_network(
  863. struct wpa_supplicant *wpa_s, char *cmd)
  864. {
  865. int id;
  866. struct wpa_ssid *ssid;
  867. /* cmd: "<network id>" or "all" */
  868. if (os_strcmp(cmd, "all") == 0) {
  869. wpa_printf(MSG_DEBUG, "CTRL_IFACE: REMOVE_NETWORK all");
  870. ssid = wpa_s->conf->ssid;
  871. while (ssid) {
  872. struct wpa_ssid *remove_ssid = ssid;
  873. id = ssid->id;
  874. ssid = ssid->next;
  875. wpas_notify_network_removed(wpa_s, remove_ssid);
  876. wpa_config_remove_network(wpa_s->conf, id);
  877. }
  878. if (wpa_s->current_ssid) {
  879. eapol_sm_invalidate_cached_session(wpa_s->eapol);
  880. wpa_supplicant_disassociate(wpa_s,
  881. WLAN_REASON_DEAUTH_LEAVING);
  882. }
  883. return 0;
  884. }
  885. id = atoi(cmd);
  886. wpa_printf(MSG_DEBUG, "CTRL_IFACE: REMOVE_NETWORK id=%d", id);
  887. ssid = wpa_config_get_network(wpa_s->conf, id);
  888. if (ssid == NULL ||
  889. wpa_config_remove_network(wpa_s->conf, id) < 0) {
  890. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find network "
  891. "id=%d", id);
  892. return -1;
  893. }
  894. if (ssid == wpa_s->current_ssid) {
  895. /*
  896. * Invalidate the EAP session cache if the current network is
  897. * removed.
  898. */
  899. eapol_sm_invalidate_cached_session(wpa_s->eapol);
  900. wpa_supplicant_disassociate(wpa_s, WLAN_REASON_DEAUTH_LEAVING);
  901. }
  902. return 0;
  903. }
  904. static int wpa_supplicant_ctrl_iface_set_network(
  905. struct wpa_supplicant *wpa_s, char *cmd)
  906. {
  907. int id;
  908. struct wpa_ssid *ssid;
  909. char *name, *value;
  910. /* cmd: "<network id> <variable name> <value>" */
  911. name = os_strchr(cmd, ' ');
  912. if (name == NULL)
  913. return -1;
  914. *name++ = '\0';
  915. value = os_strchr(name, ' ');
  916. if (value == NULL)
  917. return -1;
  918. *value++ = '\0';
  919. id = atoi(cmd);
  920. wpa_printf(MSG_DEBUG, "CTRL_IFACE: SET_NETWORK id=%d name='%s'",
  921. id, name);
  922. wpa_hexdump_ascii_key(MSG_DEBUG, "CTRL_IFACE: value",
  923. (u8 *) value, os_strlen(value));
  924. ssid = wpa_config_get_network(wpa_s->conf, id);
  925. if (ssid == NULL) {
  926. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find network "
  927. "id=%d", id);
  928. return -1;
  929. }
  930. if (wpa_config_set(ssid, name, value, 0) < 0) {
  931. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Failed to set network "
  932. "variable '%s'", name);
  933. return -1;
  934. }
  935. if (wpa_s->current_ssid == ssid) {
  936. /*
  937. * Invalidate the EAP session cache if anything in the current
  938. * configuration changes.
  939. */
  940. eapol_sm_invalidate_cached_session(wpa_s->eapol);
  941. }
  942. if ((os_strcmp(name, "psk") == 0 &&
  943. value[0] == '"' && ssid->ssid_len) ||
  944. (os_strcmp(name, "ssid") == 0 && ssid->passphrase))
  945. wpa_config_update_psk(ssid);
  946. return 0;
  947. }
  948. static int wpa_supplicant_ctrl_iface_get_network(
  949. struct wpa_supplicant *wpa_s, char *cmd, char *buf, size_t buflen)
  950. {
  951. int id;
  952. size_t res;
  953. struct wpa_ssid *ssid;
  954. char *name, *value;
  955. /* cmd: "<network id> <variable name>" */
  956. name = os_strchr(cmd, ' ');
  957. if (name == NULL || buflen == 0)
  958. return -1;
  959. *name++ = '\0';
  960. id = atoi(cmd);
  961. wpa_printf(MSG_DEBUG, "CTRL_IFACE: GET_NETWORK id=%d name='%s'",
  962. id, name);
  963. ssid = wpa_config_get_network(wpa_s->conf, id);
  964. if (ssid == NULL) {
  965. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find network "
  966. "id=%d", id);
  967. return -1;
  968. }
  969. value = wpa_config_get_no_key(ssid, name);
  970. if (value == NULL) {
  971. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Failed to get network "
  972. "variable '%s'", name);
  973. return -1;
  974. }
  975. res = os_strlcpy(buf, value, buflen);
  976. if (res >= buflen) {
  977. os_free(value);
  978. return -1;
  979. }
  980. os_free(value);
  981. return res;
  982. }
  983. #ifndef CONFIG_NO_CONFIG_WRITE
  984. static int wpa_supplicant_ctrl_iface_save_config(struct wpa_supplicant *wpa_s)
  985. {
  986. int ret;
  987. if (!wpa_s->conf->update_config) {
  988. wpa_printf(MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Not allowed "
  989. "to update configuration (update_config=0)");
  990. return -1;
  991. }
  992. ret = wpa_config_write(wpa_s->confname, wpa_s->conf);
  993. if (ret) {
  994. wpa_printf(MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Failed to "
  995. "update configuration");
  996. } else {
  997. wpa_printf(MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Configuration"
  998. " updated");
  999. }
  1000. return ret;
  1001. }
  1002. #endif /* CONFIG_NO_CONFIG_WRITE */
  1003. static int ctrl_iface_get_capability_pairwise(int res, char *strict,
  1004. struct wpa_driver_capa *capa,
  1005. char *buf, size_t buflen)
  1006. {
  1007. int ret, first = 1;
  1008. char *pos, *end;
  1009. size_t len;
  1010. pos = buf;
  1011. end = pos + buflen;
  1012. if (res < 0) {
  1013. if (strict)
  1014. return 0;
  1015. len = os_strlcpy(buf, "CCMP TKIP NONE", buflen);
  1016. if (len >= buflen)
  1017. return -1;
  1018. return len;
  1019. }
  1020. if (capa->enc & WPA_DRIVER_CAPA_ENC_CCMP) {
  1021. ret = os_snprintf(pos, end - pos, "%sCCMP", first ? "" : " ");
  1022. if (ret < 0 || ret >= end - pos)
  1023. return pos - buf;
  1024. pos += ret;
  1025. first = 0;
  1026. }
  1027. if (capa->enc & WPA_DRIVER_CAPA_ENC_TKIP) {
  1028. ret = os_snprintf(pos, end - pos, "%sTKIP", first ? "" : " ");
  1029. if (ret < 0 || ret >= end - pos)
  1030. return pos - buf;
  1031. pos += ret;
  1032. first = 0;
  1033. }
  1034. if (capa->key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_WPA_NONE) {
  1035. ret = os_snprintf(pos, end - pos, "%sNONE", first ? "" : " ");
  1036. if (ret < 0 || ret >= end - pos)
  1037. return pos - buf;
  1038. pos += ret;
  1039. first = 0;
  1040. }
  1041. return pos - buf;
  1042. }
  1043. static int ctrl_iface_get_capability_group(int res, char *strict,
  1044. struct wpa_driver_capa *capa,
  1045. char *buf, size_t buflen)
  1046. {
  1047. int ret, first = 1;
  1048. char *pos, *end;
  1049. size_t len;
  1050. pos = buf;
  1051. end = pos + buflen;
  1052. if (res < 0) {
  1053. if (strict)
  1054. return 0;
  1055. len = os_strlcpy(buf, "CCMP TKIP WEP104 WEP40", buflen);
  1056. if (len >= buflen)
  1057. return -1;
  1058. return len;
  1059. }
  1060. if (capa->enc & WPA_DRIVER_CAPA_ENC_CCMP) {
  1061. ret = os_snprintf(pos, end - pos, "%sCCMP", first ? "" : " ");
  1062. if (ret < 0 || ret >= end - pos)
  1063. return pos - buf;
  1064. pos += ret;
  1065. first = 0;
  1066. }
  1067. if (capa->enc & WPA_DRIVER_CAPA_ENC_TKIP) {
  1068. ret = os_snprintf(pos, end - pos, "%sTKIP", first ? "" : " ");
  1069. if (ret < 0 || ret >= end - pos)
  1070. return pos - buf;
  1071. pos += ret;
  1072. first = 0;
  1073. }
  1074. if (capa->enc & WPA_DRIVER_CAPA_ENC_WEP104) {
  1075. ret = os_snprintf(pos, end - pos, "%sWEP104",
  1076. first ? "" : " ");
  1077. if (ret < 0 || ret >= end - pos)
  1078. return pos - buf;
  1079. pos += ret;
  1080. first = 0;
  1081. }
  1082. if (capa->enc & WPA_DRIVER_CAPA_ENC_WEP40) {
  1083. ret = os_snprintf(pos, end - pos, "%sWEP40", first ? "" : " ");
  1084. if (ret < 0 || ret >= end - pos)
  1085. return pos - buf;
  1086. pos += ret;
  1087. first = 0;
  1088. }
  1089. return pos - buf;
  1090. }
  1091. static int ctrl_iface_get_capability_key_mgmt(int res, char *strict,
  1092. struct wpa_driver_capa *capa,
  1093. char *buf, size_t buflen)
  1094. {
  1095. int ret;
  1096. char *pos, *end;
  1097. size_t len;
  1098. pos = buf;
  1099. end = pos + buflen;
  1100. if (res < 0) {
  1101. if (strict)
  1102. return 0;
  1103. len = os_strlcpy(buf, "WPA-PSK WPA-EAP IEEE8021X WPA-NONE "
  1104. "NONE", buflen);
  1105. if (len >= buflen)
  1106. return -1;
  1107. return len;
  1108. }
  1109. ret = os_snprintf(pos, end - pos, "NONE IEEE8021X");
  1110. if (ret < 0 || ret >= end - pos)
  1111. return pos - buf;
  1112. pos += ret;
  1113. if (capa->key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA |
  1114. WPA_DRIVER_CAPA_KEY_MGMT_WPA2)) {
  1115. ret = os_snprintf(pos, end - pos, " WPA-EAP");
  1116. if (ret < 0 || ret >= end - pos)
  1117. return pos - buf;
  1118. pos += ret;
  1119. }
  1120. if (capa->key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA_PSK |
  1121. WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK)) {
  1122. ret = os_snprintf(pos, end - pos, " WPA-PSK");
  1123. if (ret < 0 || ret >= end - pos)
  1124. return pos - buf;
  1125. pos += ret;
  1126. }
  1127. if (capa->key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_WPA_NONE) {
  1128. ret = os_snprintf(pos, end - pos, " WPA-NONE");
  1129. if (ret < 0 || ret >= end - pos)
  1130. return pos - buf;
  1131. pos += ret;
  1132. }
  1133. return pos - buf;
  1134. }
  1135. static int ctrl_iface_get_capability_proto(int res, char *strict,
  1136. struct wpa_driver_capa *capa,
  1137. char *buf, size_t buflen)
  1138. {
  1139. int ret, first = 1;
  1140. char *pos, *end;
  1141. size_t len;
  1142. pos = buf;
  1143. end = pos + buflen;
  1144. if (res < 0) {
  1145. if (strict)
  1146. return 0;
  1147. len = os_strlcpy(buf, "RSN WPA", buflen);
  1148. if (len >= buflen)
  1149. return -1;
  1150. return len;
  1151. }
  1152. if (capa->key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA2 |
  1153. WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK)) {
  1154. ret = os_snprintf(pos, end - pos, "%sRSN", first ? "" : " ");
  1155. if (ret < 0 || ret >= end - pos)
  1156. return pos - buf;
  1157. pos += ret;
  1158. first = 0;
  1159. }
  1160. if (capa->key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA |
  1161. WPA_DRIVER_CAPA_KEY_MGMT_WPA_PSK)) {
  1162. ret = os_snprintf(pos, end - pos, "%sWPA", first ? "" : " ");
  1163. if (ret < 0 || ret >= end - pos)
  1164. return pos - buf;
  1165. pos += ret;
  1166. first = 0;
  1167. }
  1168. return pos - buf;
  1169. }
  1170. static int ctrl_iface_get_capability_auth_alg(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, "OPEN SHARED LEAP", buflen);
  1183. if (len >= buflen)
  1184. return -1;
  1185. return len;
  1186. }
  1187. if (capa->auth & (WPA_DRIVER_AUTH_OPEN)) {
  1188. ret = os_snprintf(pos, end - pos, "%sOPEN", first ? "" : " ");
  1189. if (ret < 0 || ret >= end - pos)
  1190. return pos - buf;
  1191. pos += ret;
  1192. first = 0;
  1193. }
  1194. if (capa->auth & (WPA_DRIVER_AUTH_SHARED)) {
  1195. ret = os_snprintf(pos, end - pos, "%sSHARED",
  1196. first ? "" : " ");
  1197. if (ret < 0 || ret >= end - pos)
  1198. return pos - buf;
  1199. pos += ret;
  1200. first = 0;
  1201. }
  1202. if (capa->auth & (WPA_DRIVER_AUTH_LEAP)) {
  1203. ret = os_snprintf(pos, end - pos, "%sLEAP", first ? "" : " ");
  1204. if (ret < 0 || ret >= end - pos)
  1205. return pos - buf;
  1206. pos += ret;
  1207. first = 0;
  1208. }
  1209. return pos - buf;
  1210. }
  1211. static int wpa_supplicant_ctrl_iface_get_capability(
  1212. struct wpa_supplicant *wpa_s, const char *_field, char *buf,
  1213. size_t buflen)
  1214. {
  1215. struct wpa_driver_capa capa;
  1216. int res;
  1217. char *strict;
  1218. char field[30];
  1219. size_t len;
  1220. /* Determine whether or not strict checking was requested */
  1221. len = os_strlcpy(field, _field, sizeof(field));
  1222. if (len >= sizeof(field))
  1223. return -1;
  1224. strict = os_strchr(field, ' ');
  1225. if (strict != NULL) {
  1226. *strict++ = '\0';
  1227. if (os_strcmp(strict, "strict") != 0)
  1228. return -1;
  1229. }
  1230. wpa_printf(MSG_DEBUG, "CTRL_IFACE: GET_CAPABILITY '%s' %s",
  1231. field, strict ? strict : "");
  1232. if (os_strcmp(field, "eap") == 0) {
  1233. return eap_get_names(buf, buflen);
  1234. }
  1235. res = wpa_drv_get_capa(wpa_s, &capa);
  1236. if (os_strcmp(field, "pairwise") == 0)
  1237. return ctrl_iface_get_capability_pairwise(res, strict, &capa,
  1238. buf, buflen);
  1239. if (os_strcmp(field, "group") == 0)
  1240. return ctrl_iface_get_capability_group(res, strict, &capa,
  1241. buf, buflen);
  1242. if (os_strcmp(field, "key_mgmt") == 0)
  1243. return ctrl_iface_get_capability_key_mgmt(res, strict, &capa,
  1244. buf, buflen);
  1245. if (os_strcmp(field, "proto") == 0)
  1246. return ctrl_iface_get_capability_proto(res, strict, &capa,
  1247. buf, buflen);
  1248. if (os_strcmp(field, "auth_alg") == 0)
  1249. return ctrl_iface_get_capability_auth_alg(res, strict, &capa,
  1250. buf, buflen);
  1251. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Unknown GET_CAPABILITY field '%s'",
  1252. field);
  1253. return -1;
  1254. }
  1255. static int wpa_supplicant_ctrl_iface_bss(struct wpa_supplicant *wpa_s,
  1256. const char *cmd, char *buf,
  1257. size_t buflen)
  1258. {
  1259. u8 bssid[ETH_ALEN];
  1260. size_t i;
  1261. struct wpa_bss *bss;
  1262. int ret;
  1263. char *pos, *end;
  1264. const u8 *ie, *ie2;
  1265. if (os_strcmp(cmd, "FIRST") == 0)
  1266. bss = dl_list_first(&wpa_s->bss, struct wpa_bss, list);
  1267. else if (os_strncmp(cmd, "ID-", 3) == 0) {
  1268. i = atoi(cmd + 3);
  1269. bss = wpa_bss_get_id(wpa_s, i);
  1270. } else if (os_strncmp(cmd, "NEXT-", 5) == 0) {
  1271. i = atoi(cmd + 5);
  1272. bss = wpa_bss_get_id(wpa_s, i);
  1273. if (bss) {
  1274. struct dl_list *next = bss->list_id.next;
  1275. if (next == &wpa_s->bss_id)
  1276. bss = NULL;
  1277. else
  1278. bss = dl_list_entry(next, struct wpa_bss,
  1279. list_id);
  1280. }
  1281. } else if (hwaddr_aton(cmd, bssid) == 0)
  1282. bss = wpa_bss_get_bssid(wpa_s, bssid);
  1283. else {
  1284. struct wpa_bss *tmp;
  1285. i = atoi(cmd);
  1286. bss = NULL;
  1287. dl_list_for_each(tmp, &wpa_s->bss_id, struct wpa_bss, list_id)
  1288. {
  1289. if (i-- == 0) {
  1290. bss = tmp;
  1291. break;
  1292. }
  1293. }
  1294. }
  1295. if (bss == NULL)
  1296. return 0;
  1297. pos = buf;
  1298. end = buf + buflen;
  1299. ret = os_snprintf(pos, end - pos,
  1300. "id=%u\n"
  1301. "bssid=" MACSTR "\n"
  1302. "freq=%d\n"
  1303. "beacon_int=%d\n"
  1304. "capabilities=0x%04x\n"
  1305. "qual=%d\n"
  1306. "noise=%d\n"
  1307. "level=%d\n"
  1308. "tsf=%016llu\n"
  1309. "ie=",
  1310. bss->id,
  1311. MAC2STR(bss->bssid), bss->freq, bss->beacon_int,
  1312. bss->caps, bss->qual, bss->noise, bss->level,
  1313. (unsigned long long) bss->tsf);
  1314. if (ret < 0 || ret >= end - pos)
  1315. return pos - buf;
  1316. pos += ret;
  1317. ie = (const u8 *) (bss + 1);
  1318. for (i = 0; i < bss->ie_len; i++) {
  1319. ret = os_snprintf(pos, end - pos, "%02x", *ie++);
  1320. if (ret < 0 || ret >= end - pos)
  1321. return pos - buf;
  1322. pos += ret;
  1323. }
  1324. ret = os_snprintf(pos, end - pos, "\n");
  1325. if (ret < 0 || ret >= end - pos)
  1326. return pos - buf;
  1327. pos += ret;
  1328. ret = os_snprintf(pos, end - pos, "flags=");
  1329. if (ret < 0 || ret >= end - pos)
  1330. return pos - buf;
  1331. pos += ret;
  1332. ie = wpa_bss_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
  1333. if (ie)
  1334. pos = wpa_supplicant_ie_txt(pos, end, "WPA", ie, 2 + ie[1]);
  1335. ie2 = wpa_bss_get_ie(bss, WLAN_EID_RSN);
  1336. if (ie2)
  1337. pos = wpa_supplicant_ie_txt(pos, end, "WPA2", ie2, 2 + ie2[1]);
  1338. pos = wpa_supplicant_wps_ie_txt_bss(pos, end, bss);
  1339. if (!ie && !ie2 && bss->caps & IEEE80211_CAP_PRIVACY) {
  1340. ret = os_snprintf(pos, end - pos, "[WEP]");
  1341. if (ret < 0 || ret >= end - pos)
  1342. return pos - buf;
  1343. pos += ret;
  1344. }
  1345. if (bss->caps & IEEE80211_CAP_IBSS) {
  1346. ret = os_snprintf(pos, end - pos, "[IBSS]");
  1347. if (ret < 0 || ret >= end - pos)
  1348. return pos - buf;
  1349. pos += ret;
  1350. }
  1351. ret = os_snprintf(pos, end - pos, "\n");
  1352. if (ret < 0 || ret >= end - pos)
  1353. return pos - buf;
  1354. pos += ret;
  1355. ret = os_snprintf(pos, end - pos, "ssid=%s\n",
  1356. wpa_ssid_txt(bss->ssid, bss->ssid_len));
  1357. if (ret < 0 || ret >= end - pos)
  1358. return pos - buf;
  1359. pos += ret;
  1360. #ifdef CONFIG_WPS
  1361. ie = (const u8 *) (bss + 1);
  1362. ret = wpas_wps_scan_result_text(ie, bss->ie_len, pos, end);
  1363. if (ret < 0 || ret >= end - pos)
  1364. return pos - buf;
  1365. pos += ret;
  1366. #endif /* CONFIG_WPS */
  1367. return pos - buf;
  1368. }
  1369. static int wpa_supplicant_ctrl_iface_ap_scan(
  1370. struct wpa_supplicant *wpa_s, char *cmd)
  1371. {
  1372. int ap_scan = atoi(cmd);
  1373. return wpa_supplicant_set_ap_scan(wpa_s, ap_scan);
  1374. }
  1375. char * wpa_supplicant_ctrl_iface_process(struct wpa_supplicant *wpa_s,
  1376. char *buf, size_t *resp_len)
  1377. {
  1378. char *reply;
  1379. const int reply_size = 2048;
  1380. int ctrl_rsp = 0;
  1381. int reply_len;
  1382. if (os_strncmp(buf, WPA_CTRL_RSP, os_strlen(WPA_CTRL_RSP)) == 0 ||
  1383. os_strncmp(buf, "SET_NETWORK ", 12) == 0) {
  1384. wpa_hexdump_ascii_key(MSG_DEBUG, "RX ctrl_iface",
  1385. (const u8 *) buf, os_strlen(buf));
  1386. } else {
  1387. wpa_hexdump_ascii(MSG_DEBUG, "RX ctrl_iface",
  1388. (const u8 *) buf, os_strlen(buf));
  1389. }
  1390. reply = os_malloc(reply_size);
  1391. if (reply == NULL) {
  1392. *resp_len = 1;
  1393. return NULL;
  1394. }
  1395. os_memcpy(reply, "OK\n", 3);
  1396. reply_len = 3;
  1397. if (os_strcmp(buf, "PING") == 0) {
  1398. os_memcpy(reply, "PONG\n", 5);
  1399. reply_len = 5;
  1400. } else if (os_strcmp(buf, "MIB") == 0) {
  1401. reply_len = wpa_sm_get_mib(wpa_s->wpa, reply, reply_size);
  1402. if (reply_len >= 0) {
  1403. int res;
  1404. res = eapol_sm_get_mib(wpa_s->eapol, reply + reply_len,
  1405. reply_size - reply_len);
  1406. if (res < 0)
  1407. reply_len = -1;
  1408. else
  1409. reply_len += res;
  1410. }
  1411. } else if (os_strncmp(buf, "STATUS", 6) == 0) {
  1412. reply_len = wpa_supplicant_ctrl_iface_status(
  1413. wpa_s, buf + 6, reply, reply_size);
  1414. } else if (os_strcmp(buf, "PMKSA") == 0) {
  1415. reply_len = wpa_sm_pmksa_cache_list(wpa_s->wpa, reply,
  1416. reply_size);
  1417. } else if (os_strncmp(buf, "SET ", 4) == 0) {
  1418. if (wpa_supplicant_ctrl_iface_set(wpa_s, buf + 4))
  1419. reply_len = -1;
  1420. } else if (os_strcmp(buf, "LOGON") == 0) {
  1421. eapol_sm_notify_logoff(wpa_s->eapol, FALSE);
  1422. } else if (os_strcmp(buf, "LOGOFF") == 0) {
  1423. eapol_sm_notify_logoff(wpa_s->eapol, TRUE);
  1424. } else if (os_strcmp(buf, "REASSOCIATE") == 0) {
  1425. wpa_s->disconnected = 0;
  1426. wpa_s->reassociate = 1;
  1427. wpa_supplicant_req_scan(wpa_s, 0, 0);
  1428. } else if (os_strcmp(buf, "RECONNECT") == 0) {
  1429. if (wpa_s->disconnected) {
  1430. wpa_s->disconnected = 0;
  1431. wpa_s->reassociate = 1;
  1432. wpa_supplicant_req_scan(wpa_s, 0, 0);
  1433. }
  1434. #ifdef IEEE8021X_EAPOL
  1435. } else if (os_strncmp(buf, "PREAUTH ", 8) == 0) {
  1436. if (wpa_supplicant_ctrl_iface_preauth(wpa_s, buf + 8))
  1437. reply_len = -1;
  1438. #endif /* IEEE8021X_EAPOL */
  1439. #ifdef CONFIG_PEERKEY
  1440. } else if (os_strncmp(buf, "STKSTART ", 9) == 0) {
  1441. if (wpa_supplicant_ctrl_iface_stkstart(wpa_s, buf + 9))
  1442. reply_len = -1;
  1443. #endif /* CONFIG_PEERKEY */
  1444. #ifdef CONFIG_IEEE80211R
  1445. } else if (os_strncmp(buf, "FT_DS ", 6) == 0) {
  1446. if (wpa_supplicant_ctrl_iface_ft_ds(wpa_s, buf + 6))
  1447. reply_len = -1;
  1448. #endif /* CONFIG_IEEE80211R */
  1449. #ifdef CONFIG_WPS
  1450. } else if (os_strcmp(buf, "WPS_PBC") == 0) {
  1451. if (wpa_supplicant_ctrl_iface_wps_pbc(wpa_s, NULL))
  1452. reply_len = -1;
  1453. } else if (os_strncmp(buf, "WPS_PBC ", 8) == 0) {
  1454. if (wpa_supplicant_ctrl_iface_wps_pbc(wpa_s, buf + 8))
  1455. reply_len = -1;
  1456. } else if (os_strncmp(buf, "WPS_PIN ", 8) == 0) {
  1457. reply_len = wpa_supplicant_ctrl_iface_wps_pin(wpa_s, buf + 8,
  1458. reply,
  1459. reply_size);
  1460. #ifdef CONFIG_WPS_OOB
  1461. } else if (os_strncmp(buf, "WPS_OOB ", 8) == 0) {
  1462. if (wpa_supplicant_ctrl_iface_wps_oob(wpa_s, buf + 8))
  1463. reply_len = -1;
  1464. #endif /* CONFIG_WPS_OOB */
  1465. } else if (os_strncmp(buf, "WPS_REG ", 8) == 0) {
  1466. if (wpa_supplicant_ctrl_iface_wps_reg(wpa_s, buf + 8))
  1467. reply_len = -1;
  1468. #ifdef CONFIG_WPS_ER
  1469. } else if (os_strcmp(buf, "WPS_ER_START") == 0) {
  1470. if (wpas_wps_er_start(wpa_s))
  1471. reply_len = -1;
  1472. } else if (os_strcmp(buf, "WPS_ER_STOP") == 0) {
  1473. if (wpas_wps_er_stop(wpa_s))
  1474. reply_len = -1;
  1475. } else if (os_strncmp(buf, "WPS_ER_PIN ", 11) == 0) {
  1476. if (wpa_supplicant_ctrl_iface_wps_er_pin(wpa_s, buf + 11))
  1477. reply_len = -1;
  1478. } else if (os_strncmp(buf, "WPS_ER_PBC ", 11) == 0) {
  1479. if (wpas_wps_er_pbc(wpa_s, buf + 11))
  1480. reply_len = -1;
  1481. } else if (os_strncmp(buf, "WPS_ER_LEARN ", 13) == 0) {
  1482. if (wpa_supplicant_ctrl_iface_wps_er_learn(wpa_s, buf + 13))
  1483. reply_len = -1;
  1484. #endif /* CONFIG_WPS_ER */
  1485. #endif /* CONFIG_WPS */
  1486. #ifdef CONFIG_IBSS_RSN
  1487. } else if (os_strncmp(buf, "IBSS_RSN ", 9) == 0) {
  1488. if (wpa_supplicant_ctrl_iface_ibss_rsn(wpa_s, buf + 9))
  1489. reply_len = -1;
  1490. #endif /* CONFIG_IBSS_RSN */
  1491. } else if (os_strncmp(buf, WPA_CTRL_RSP, os_strlen(WPA_CTRL_RSP)) == 0)
  1492. {
  1493. if (wpa_supplicant_ctrl_iface_ctrl_rsp(
  1494. wpa_s, buf + os_strlen(WPA_CTRL_RSP)))
  1495. reply_len = -1;
  1496. else
  1497. ctrl_rsp = 1;
  1498. } else if (os_strcmp(buf, "RECONFIGURE") == 0) {
  1499. if (wpa_supplicant_reload_configuration(wpa_s))
  1500. reply_len = -1;
  1501. } else if (os_strcmp(buf, "TERMINATE") == 0) {
  1502. wpa_supplicant_terminate_proc(wpa_s->global);
  1503. } else if (os_strncmp(buf, "BSSID ", 6) == 0) {
  1504. if (wpa_supplicant_ctrl_iface_bssid(wpa_s, buf + 6))
  1505. reply_len = -1;
  1506. } else if (os_strcmp(buf, "LIST_NETWORKS") == 0) {
  1507. reply_len = wpa_supplicant_ctrl_iface_list_networks(
  1508. wpa_s, reply, reply_size);
  1509. } else if (os_strcmp(buf, "DISCONNECT") == 0) {
  1510. wpa_s->reassociate = 0;
  1511. wpa_s->disconnected = 1;
  1512. wpa_supplicant_disassociate(wpa_s, WLAN_REASON_DEAUTH_LEAVING);
  1513. } else if (os_strcmp(buf, "SCAN") == 0) {
  1514. wpa_s->scan_req = 2;
  1515. wpa_supplicant_req_scan(wpa_s, 0, 0);
  1516. } else if (os_strcmp(buf, "SCAN_RESULTS") == 0) {
  1517. reply_len = wpa_supplicant_ctrl_iface_scan_results(
  1518. wpa_s, reply, reply_size);
  1519. } else if (os_strncmp(buf, "SELECT_NETWORK ", 15) == 0) {
  1520. if (wpa_supplicant_ctrl_iface_select_network(wpa_s, buf + 15))
  1521. reply_len = -1;
  1522. } else if (os_strncmp(buf, "ENABLE_NETWORK ", 15) == 0) {
  1523. if (wpa_supplicant_ctrl_iface_enable_network(wpa_s, buf + 15))
  1524. reply_len = -1;
  1525. } else if (os_strncmp(buf, "DISABLE_NETWORK ", 16) == 0) {
  1526. if (wpa_supplicant_ctrl_iface_disable_network(wpa_s, buf + 16))
  1527. reply_len = -1;
  1528. } else if (os_strcmp(buf, "ADD_NETWORK") == 0) {
  1529. reply_len = wpa_supplicant_ctrl_iface_add_network(
  1530. wpa_s, reply, reply_size);
  1531. } else if (os_strncmp(buf, "REMOVE_NETWORK ", 15) == 0) {
  1532. if (wpa_supplicant_ctrl_iface_remove_network(wpa_s, buf + 15))
  1533. reply_len = -1;
  1534. } else if (os_strncmp(buf, "SET_NETWORK ", 12) == 0) {
  1535. if (wpa_supplicant_ctrl_iface_set_network(wpa_s, buf + 12))
  1536. reply_len = -1;
  1537. } else if (os_strncmp(buf, "GET_NETWORK ", 12) == 0) {
  1538. reply_len = wpa_supplicant_ctrl_iface_get_network(
  1539. wpa_s, buf + 12, reply, reply_size);
  1540. #ifndef CONFIG_NO_CONFIG_WRITE
  1541. } else if (os_strcmp(buf, "SAVE_CONFIG") == 0) {
  1542. if (wpa_supplicant_ctrl_iface_save_config(wpa_s))
  1543. reply_len = -1;
  1544. #endif /* CONFIG_NO_CONFIG_WRITE */
  1545. } else if (os_strncmp(buf, "GET_CAPABILITY ", 15) == 0) {
  1546. reply_len = wpa_supplicant_ctrl_iface_get_capability(
  1547. wpa_s, buf + 15, reply, reply_size);
  1548. } else if (os_strncmp(buf, "AP_SCAN ", 8) == 0) {
  1549. if (wpa_supplicant_ctrl_iface_ap_scan(wpa_s, buf + 8))
  1550. reply_len = -1;
  1551. } else if (os_strcmp(buf, "INTERFACE_LIST") == 0) {
  1552. reply_len = wpa_supplicant_global_iface_list(
  1553. wpa_s->global, reply, reply_size);
  1554. } else if (os_strcmp(buf, "INTERFACES") == 0) {
  1555. reply_len = wpa_supplicant_global_iface_interfaces(
  1556. wpa_s->global, reply, reply_size);
  1557. } else if (os_strncmp(buf, "BSS ", 4) == 0) {
  1558. reply_len = wpa_supplicant_ctrl_iface_bss(
  1559. wpa_s, buf + 4, reply, reply_size);
  1560. #ifdef CONFIG_AP
  1561. } else if (os_strcmp(buf, "STA-FIRST") == 0) {
  1562. reply_len = ap_ctrl_iface_sta_first(wpa_s, reply, reply_size);
  1563. } else if (os_strncmp(buf, "STA ", 4) == 0) {
  1564. reply_len = ap_ctrl_iface_sta(wpa_s, buf + 4, reply,
  1565. reply_size);
  1566. } else if (os_strncmp(buf, "STA-NEXT ", 9) == 0) {
  1567. reply_len = ap_ctrl_iface_sta_next(wpa_s, buf + 9, reply,
  1568. reply_size);
  1569. #endif /* CONFIG_AP */
  1570. } else {
  1571. os_memcpy(reply, "UNKNOWN COMMAND\n", 16);
  1572. reply_len = 16;
  1573. }
  1574. if (reply_len < 0) {
  1575. os_memcpy(reply, "FAIL\n", 5);
  1576. reply_len = 5;
  1577. }
  1578. if (ctrl_rsp)
  1579. eapol_sm_notify_ctrl_response(wpa_s->eapol);
  1580. *resp_len = reply_len;
  1581. return reply;
  1582. }
  1583. static int wpa_supplicant_global_iface_add(struct wpa_global *global,
  1584. char *cmd)
  1585. {
  1586. struct wpa_interface iface;
  1587. char *pos;
  1588. /*
  1589. * <ifname>TAB<confname>TAB<driver>TAB<ctrl_interface>TAB<driver_param>
  1590. * TAB<bridge_ifname>
  1591. */
  1592. wpa_printf(MSG_DEBUG, "CTRL_IFACE GLOBAL INTERFACE_ADD '%s'", cmd);
  1593. os_memset(&iface, 0, sizeof(iface));
  1594. do {
  1595. iface.ifname = pos = cmd;
  1596. pos = os_strchr(pos, '\t');
  1597. if (pos)
  1598. *pos++ = '\0';
  1599. if (iface.ifname[0] == '\0')
  1600. return -1;
  1601. if (pos == NULL)
  1602. break;
  1603. iface.confname = pos;
  1604. pos = os_strchr(pos, '\t');
  1605. if (pos)
  1606. *pos++ = '\0';
  1607. if (iface.confname[0] == '\0')
  1608. iface.confname = NULL;
  1609. if (pos == NULL)
  1610. break;
  1611. iface.driver = pos;
  1612. pos = os_strchr(pos, '\t');
  1613. if (pos)
  1614. *pos++ = '\0';
  1615. if (iface.driver[0] == '\0')
  1616. iface.driver = NULL;
  1617. if (pos == NULL)
  1618. break;
  1619. iface.ctrl_interface = pos;
  1620. pos = os_strchr(pos, '\t');
  1621. if (pos)
  1622. *pos++ = '\0';
  1623. if (iface.ctrl_interface[0] == '\0')
  1624. iface.ctrl_interface = NULL;
  1625. if (pos == NULL)
  1626. break;
  1627. iface.driver_param = pos;
  1628. pos = os_strchr(pos, '\t');
  1629. if (pos)
  1630. *pos++ = '\0';
  1631. if (iface.driver_param[0] == '\0')
  1632. iface.driver_param = NULL;
  1633. if (pos == NULL)
  1634. break;
  1635. iface.bridge_ifname = pos;
  1636. pos = os_strchr(pos, '\t');
  1637. if (pos)
  1638. *pos++ = '\0';
  1639. if (iface.bridge_ifname[0] == '\0')
  1640. iface.bridge_ifname = NULL;
  1641. if (pos == NULL)
  1642. break;
  1643. } while (0);
  1644. if (wpa_supplicant_get_iface(global, iface.ifname))
  1645. return -1;
  1646. return wpa_supplicant_add_iface(global, &iface) ? 0 : -1;
  1647. }
  1648. static int wpa_supplicant_global_iface_remove(struct wpa_global *global,
  1649. char *cmd)
  1650. {
  1651. struct wpa_supplicant *wpa_s;
  1652. wpa_printf(MSG_DEBUG, "CTRL_IFACE GLOBAL INTERFACE_REMOVE '%s'", cmd);
  1653. wpa_s = wpa_supplicant_get_iface(global, cmd);
  1654. if (wpa_s == NULL)
  1655. return -1;
  1656. return wpa_supplicant_remove_iface(global, wpa_s);
  1657. }
  1658. static void wpa_free_iface_info(struct wpa_interface_info *iface)
  1659. {
  1660. struct wpa_interface_info *prev;
  1661. while (iface) {
  1662. prev = iface;
  1663. iface = iface->next;
  1664. os_free(prev->ifname);
  1665. os_free(prev->desc);
  1666. os_free(prev);
  1667. }
  1668. }
  1669. static int wpa_supplicant_global_iface_list(struct wpa_global *global,
  1670. char *buf, int len)
  1671. {
  1672. int i, res;
  1673. struct wpa_interface_info *iface = NULL, *last = NULL, *tmp;
  1674. char *pos, *end;
  1675. for (i = 0; wpa_drivers[i]; i++) {
  1676. struct wpa_driver_ops *drv = wpa_drivers[i];
  1677. if (drv->get_interfaces == NULL)
  1678. continue;
  1679. tmp = drv->get_interfaces(global->drv_priv);
  1680. if (tmp == NULL)
  1681. continue;
  1682. if (last == NULL)
  1683. iface = last = tmp;
  1684. else
  1685. last->next = tmp;
  1686. while (last->next)
  1687. last = last->next;
  1688. }
  1689. pos = buf;
  1690. end = buf + len;
  1691. for (tmp = iface; tmp; tmp = tmp->next) {
  1692. res = os_snprintf(pos, end - pos, "%s\t%s\t%s\n",
  1693. tmp->drv_name, tmp->ifname,
  1694. tmp->desc ? tmp->desc : "");
  1695. if (res < 0 || res >= end - pos) {
  1696. *pos = '\0';
  1697. break;
  1698. }
  1699. pos += res;
  1700. }
  1701. wpa_free_iface_info(iface);
  1702. return pos - buf;
  1703. }
  1704. static int wpa_supplicant_global_iface_interfaces(struct wpa_global *global,
  1705. char *buf, int len)
  1706. {
  1707. int res;
  1708. char *pos, *end;
  1709. struct wpa_supplicant *wpa_s;
  1710. wpa_s = global->ifaces;
  1711. pos = buf;
  1712. end = buf + len;
  1713. while (wpa_s) {
  1714. res = os_snprintf(pos, end - pos, "%s\n", wpa_s->ifname);
  1715. if (res < 0 || res >= end - pos) {
  1716. *pos = '\0';
  1717. break;
  1718. }
  1719. pos += res;
  1720. wpa_s = wpa_s->next;
  1721. }
  1722. return pos - buf;
  1723. }
  1724. char * wpa_supplicant_global_ctrl_iface_process(struct wpa_global *global,
  1725. char *buf, size_t *resp_len)
  1726. {
  1727. char *reply;
  1728. const int reply_size = 2048;
  1729. int reply_len;
  1730. wpa_hexdump_ascii(MSG_DEBUG, "RX global ctrl_iface",
  1731. (const u8 *) buf, os_strlen(buf));
  1732. reply = os_malloc(reply_size);
  1733. if (reply == NULL) {
  1734. *resp_len = 1;
  1735. return NULL;
  1736. }
  1737. os_memcpy(reply, "OK\n", 3);
  1738. reply_len = 3;
  1739. if (os_strcmp(buf, "PING") == 0) {
  1740. os_memcpy(reply, "PONG\n", 5);
  1741. reply_len = 5;
  1742. } else if (os_strncmp(buf, "INTERFACE_ADD ", 14) == 0) {
  1743. if (wpa_supplicant_global_iface_add(global, buf + 14))
  1744. reply_len = -1;
  1745. } else if (os_strncmp(buf, "INTERFACE_REMOVE ", 17) == 0) {
  1746. if (wpa_supplicant_global_iface_remove(global, buf + 17))
  1747. reply_len = -1;
  1748. } else if (os_strcmp(buf, "INTERFACE_LIST") == 0) {
  1749. reply_len = wpa_supplicant_global_iface_list(
  1750. global, reply, reply_size);
  1751. } else if (os_strcmp(buf, "INTERFACES") == 0) {
  1752. reply_len = wpa_supplicant_global_iface_interfaces(
  1753. global, reply, reply_size);
  1754. } else if (os_strcmp(buf, "TERMINATE") == 0) {
  1755. wpa_supplicant_terminate_proc(global);
  1756. } else {
  1757. os_memcpy(reply, "UNKNOWN COMMAND\n", 16);
  1758. reply_len = 16;
  1759. }
  1760. if (reply_len < 0) {
  1761. os_memcpy(reply, "FAIL\n", 5);
  1762. reply_len = 5;
  1763. }
  1764. *resp_len = reply_len;
  1765. return reply;
  1766. }