ctrl_iface.c 47 KB

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