ctrl_iface.c 43 KB

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