ctrl_iface.c 40 KB

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