ctrl_iface.c 48 KB

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