ctrl_iface.c 46 KB

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