ctrl_iface.c 45 KB

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