ctrl_iface.c 47 KB

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