ctrl_iface.c 40 KB

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