ctrl_iface.c 46 KB

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