ctrl_iface.c 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675
  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. results = wpa_s->scan_res;
  1063. if (results == NULL)
  1064. return 0;
  1065. if (hwaddr_aton(cmd, bssid) == 0) {
  1066. for (i = 0; i < results->num; i++) {
  1067. if (os_memcmp(bssid, results->res[i]->bssid, ETH_ALEN)
  1068. == 0)
  1069. break;
  1070. }
  1071. } else
  1072. i = atoi(cmd);
  1073. if (i >= results->num || results->res[i] == NULL)
  1074. return 0; /* no match found */
  1075. bss = results->res[i];
  1076. pos = buf;
  1077. end = buf + buflen;
  1078. ret = os_snprintf(pos, end - pos,
  1079. "bssid=" MACSTR "\n"
  1080. "freq=%d\n"
  1081. "beacon_int=%d\n"
  1082. "capabilities=0x%04x\n"
  1083. "qual=%d\n"
  1084. "noise=%d\n"
  1085. "level=%d\n"
  1086. "tsf=%016llu\n"
  1087. "ie=",
  1088. MAC2STR(bss->bssid), bss->freq, bss->beacon_int,
  1089. bss->caps, bss->qual, bss->noise, bss->level,
  1090. (unsigned long long) bss->tsf);
  1091. if (ret < 0 || ret >= end - pos)
  1092. return pos - buf;
  1093. pos += ret;
  1094. ie = (const u8 *) (bss + 1);
  1095. for (i = 0; i < bss->ie_len; i++) {
  1096. ret = os_snprintf(pos, end - pos, "%02x", *ie++);
  1097. if (ret < 0 || ret >= end - pos)
  1098. return pos - buf;
  1099. pos += ret;
  1100. }
  1101. ret = os_snprintf(pos, end - pos, "\n");
  1102. if (ret < 0 || ret >= end - pos)
  1103. return pos - buf;
  1104. pos += ret;
  1105. ret = os_snprintf(pos, end - pos, "flags=");
  1106. if (ret < 0 || ret >= end - pos)
  1107. return pos - buf;
  1108. pos += ret;
  1109. ie = wpa_scan_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
  1110. if (ie)
  1111. pos = wpa_supplicant_ie_txt(pos, end, "WPA", ie, 2 + ie[1]);
  1112. ie2 = wpa_scan_get_ie(bss, WLAN_EID_RSN);
  1113. if (ie2)
  1114. pos = wpa_supplicant_ie_txt(pos, end, "WPA2", ie2, 2 + ie2[1]);
  1115. if (!ie && !ie2 && bss->caps & IEEE80211_CAP_PRIVACY) {
  1116. ret = os_snprintf(pos, end - pos, "[WEP]");
  1117. if (ret < 0 || ret >= end - pos)
  1118. return pos - buf;
  1119. pos += ret;
  1120. }
  1121. if (bss->caps & IEEE80211_CAP_IBSS) {
  1122. ret = os_snprintf(pos, end - pos, "[IBSS]");
  1123. if (ret < 0 || ret >= end - pos)
  1124. return pos - buf;
  1125. pos += ret;
  1126. }
  1127. ret = os_snprintf(pos, end - pos, "\n");
  1128. if (ret < 0 || ret >= end - pos)
  1129. return pos - buf;
  1130. pos += ret;
  1131. ie = wpa_scan_get_ie(bss, WLAN_EID_SSID);
  1132. ret = os_snprintf(pos, end - pos, "ssid=%s\n",
  1133. ie ? wpa_ssid_txt(ie + 2, ie[1]) : "");
  1134. if (ret < 0 || ret >= end - pos)
  1135. return pos - buf;
  1136. pos += ret;
  1137. return pos - buf;
  1138. }
  1139. static int wpa_supplicant_ctrl_iface_ap_scan(
  1140. struct wpa_supplicant *wpa_s, char *cmd)
  1141. {
  1142. int ap_scan = atoi(cmd);
  1143. if (ap_scan < 0 || ap_scan > 2)
  1144. return -1;
  1145. wpa_s->conf->ap_scan = ap_scan;
  1146. return 0;
  1147. }
  1148. char * wpa_supplicant_ctrl_iface_process(struct wpa_supplicant *wpa_s,
  1149. char *buf, size_t *resp_len)
  1150. {
  1151. char *reply;
  1152. const int reply_size = 2048;
  1153. int ctrl_rsp = 0;
  1154. int reply_len;
  1155. if (os_strncmp(buf, WPA_CTRL_RSP, os_strlen(WPA_CTRL_RSP)) == 0 ||
  1156. os_strncmp(buf, "SET_NETWORK ", 12) == 0) {
  1157. wpa_hexdump_ascii_key(MSG_DEBUG, "RX ctrl_iface",
  1158. (const u8 *) buf, os_strlen(buf));
  1159. } else {
  1160. wpa_hexdump_ascii(MSG_DEBUG, "RX ctrl_iface",
  1161. (const u8 *) buf, os_strlen(buf));
  1162. }
  1163. reply = os_malloc(reply_size);
  1164. if (reply == NULL) {
  1165. *resp_len = 1;
  1166. return NULL;
  1167. }
  1168. os_memcpy(reply, "OK\n", 3);
  1169. reply_len = 3;
  1170. if (os_strcmp(buf, "PING") == 0) {
  1171. os_memcpy(reply, "PONG\n", 5);
  1172. reply_len = 5;
  1173. } else if (os_strcmp(buf, "MIB") == 0) {
  1174. reply_len = wpa_sm_get_mib(wpa_s->wpa, reply, reply_size);
  1175. if (reply_len >= 0) {
  1176. int res;
  1177. res = eapol_sm_get_mib(wpa_s->eapol, reply + reply_len,
  1178. reply_size - reply_len);
  1179. if (res < 0)
  1180. reply_len = -1;
  1181. else
  1182. reply_len += res;
  1183. }
  1184. } else if (os_strncmp(buf, "STATUS", 6) == 0) {
  1185. reply_len = wpa_supplicant_ctrl_iface_status(
  1186. wpa_s, buf + 6, reply, reply_size);
  1187. } else if (os_strcmp(buf, "PMKSA") == 0) {
  1188. reply_len = pmksa_cache_list(wpa_s->wpa, reply, reply_size);
  1189. } else if (os_strncmp(buf, "SET ", 4) == 0) {
  1190. if (wpa_supplicant_ctrl_iface_set(wpa_s, buf + 4))
  1191. reply_len = -1;
  1192. } else if (os_strcmp(buf, "LOGON") == 0) {
  1193. eapol_sm_notify_logoff(wpa_s->eapol, FALSE);
  1194. } else if (os_strcmp(buf, "LOGOFF") == 0) {
  1195. eapol_sm_notify_logoff(wpa_s->eapol, TRUE);
  1196. } else if (os_strcmp(buf, "REASSOCIATE") == 0) {
  1197. wpa_s->disconnected = 0;
  1198. wpa_s->reassociate = 1;
  1199. wpa_supplicant_req_scan(wpa_s, 0, 0);
  1200. } else if (os_strcmp(buf, "RECONNECT") == 0) {
  1201. if (wpa_s->disconnected) {
  1202. wpa_s->disconnected = 0;
  1203. wpa_s->reassociate = 1;
  1204. wpa_supplicant_req_scan(wpa_s, 0, 0);
  1205. }
  1206. #ifdef IEEE8021X_EAPOL
  1207. } else if (os_strncmp(buf, "PREAUTH ", 8) == 0) {
  1208. if (wpa_supplicant_ctrl_iface_preauth(wpa_s, buf + 8))
  1209. reply_len = -1;
  1210. #endif /* IEEE8021X_EAPOL */
  1211. #ifdef CONFIG_PEERKEY
  1212. } else if (os_strncmp(buf, "STKSTART ", 9) == 0) {
  1213. if (wpa_supplicant_ctrl_iface_stkstart(wpa_s, buf + 9))
  1214. reply_len = -1;
  1215. #endif /* CONFIG_PEERKEY */
  1216. #ifdef CONFIG_IEEE80211R
  1217. } else if (os_strncmp(buf, "FT_DS ", 6) == 0) {
  1218. if (wpa_supplicant_ctrl_iface_ft_ds(wpa_s, buf + 6))
  1219. reply_len = -1;
  1220. #endif /* CONFIG_IEEE80211R */
  1221. } else if (os_strncmp(buf, WPA_CTRL_RSP, os_strlen(WPA_CTRL_RSP)) == 0)
  1222. {
  1223. if (wpa_supplicant_ctrl_iface_ctrl_rsp(
  1224. wpa_s, buf + os_strlen(WPA_CTRL_RSP)))
  1225. reply_len = -1;
  1226. else
  1227. ctrl_rsp = 1;
  1228. } else if (os_strcmp(buf, "RECONFIGURE") == 0) {
  1229. if (wpa_supplicant_reload_configuration(wpa_s))
  1230. reply_len = -1;
  1231. } else if (os_strcmp(buf, "TERMINATE") == 0) {
  1232. eloop_terminate();
  1233. } else if (os_strncmp(buf, "BSSID ", 6) == 0) {
  1234. if (wpa_supplicant_ctrl_iface_bssid(wpa_s, buf + 6))
  1235. reply_len = -1;
  1236. } else if (os_strcmp(buf, "LIST_NETWORKS") == 0) {
  1237. reply_len = wpa_supplicant_ctrl_iface_list_networks(
  1238. wpa_s, reply, reply_size);
  1239. } else if (os_strcmp(buf, "DISCONNECT") == 0) {
  1240. wpa_s->reassociate = 0;
  1241. wpa_s->disconnected = 1;
  1242. wpa_supplicant_disassociate(wpa_s, WLAN_REASON_DEAUTH_LEAVING);
  1243. } else if (os_strcmp(buf, "SCAN") == 0) {
  1244. wpa_s->scan_req = 2;
  1245. wpa_supplicant_req_scan(wpa_s, 0, 0);
  1246. } else if (os_strcmp(buf, "SCAN_RESULTS") == 0) {
  1247. reply_len = wpa_supplicant_ctrl_iface_scan_results(
  1248. wpa_s, reply, reply_size);
  1249. } else if (os_strncmp(buf, "SELECT_NETWORK ", 15) == 0) {
  1250. if (wpa_supplicant_ctrl_iface_select_network(wpa_s, buf + 15))
  1251. reply_len = -1;
  1252. } else if (os_strncmp(buf, "ENABLE_NETWORK ", 15) == 0) {
  1253. if (wpa_supplicant_ctrl_iface_enable_network(wpa_s, buf + 15))
  1254. reply_len = -1;
  1255. } else if (os_strncmp(buf, "DISABLE_NETWORK ", 16) == 0) {
  1256. if (wpa_supplicant_ctrl_iface_disable_network(wpa_s, buf + 16))
  1257. reply_len = -1;
  1258. } else if (os_strcmp(buf, "ADD_NETWORK") == 0) {
  1259. reply_len = wpa_supplicant_ctrl_iface_add_network(
  1260. wpa_s, reply, reply_size);
  1261. } else if (os_strncmp(buf, "REMOVE_NETWORK ", 15) == 0) {
  1262. if (wpa_supplicant_ctrl_iface_remove_network(wpa_s, buf + 15))
  1263. reply_len = -1;
  1264. } else if (os_strncmp(buf, "SET_NETWORK ", 12) == 0) {
  1265. if (wpa_supplicant_ctrl_iface_set_network(wpa_s, buf + 12))
  1266. reply_len = -1;
  1267. } else if (os_strncmp(buf, "GET_NETWORK ", 12) == 0) {
  1268. reply_len = wpa_supplicant_ctrl_iface_get_network(
  1269. wpa_s, buf + 12, reply, reply_size);
  1270. #ifndef CONFIG_NO_CONFIG_WRITE
  1271. } else if (os_strcmp(buf, "SAVE_CONFIG") == 0) {
  1272. if (wpa_supplicant_ctrl_iface_save_config(wpa_s))
  1273. reply_len = -1;
  1274. #endif /* CONFIG_NO_CONFIG_WRITE */
  1275. } else if (os_strncmp(buf, "GET_CAPABILITY ", 15) == 0) {
  1276. reply_len = wpa_supplicant_ctrl_iface_get_capability(
  1277. wpa_s, buf + 15, reply, reply_size);
  1278. } else if (os_strncmp(buf, "AP_SCAN ", 8) == 0) {
  1279. if (wpa_supplicant_ctrl_iface_ap_scan(wpa_s, buf + 8))
  1280. reply_len = -1;
  1281. } else if (os_strcmp(buf, "INTERFACES") == 0) {
  1282. reply_len = wpa_supplicant_global_iface_interfaces(
  1283. wpa_s->global, reply, reply_size);
  1284. } else if (os_strncmp(buf, "BSS ", 4) == 0) {
  1285. reply_len = wpa_supplicant_ctrl_iface_bss(
  1286. wpa_s, buf + 4, reply, reply_size);
  1287. } else {
  1288. os_memcpy(reply, "UNKNOWN COMMAND\n", 16);
  1289. reply_len = 16;
  1290. }
  1291. if (reply_len < 0) {
  1292. os_memcpy(reply, "FAIL\n", 5);
  1293. reply_len = 5;
  1294. }
  1295. if (ctrl_rsp)
  1296. eapol_sm_notify_ctrl_response(wpa_s->eapol);
  1297. *resp_len = reply_len;
  1298. return reply;
  1299. }
  1300. static int wpa_supplicant_global_iface_add(struct wpa_global *global,
  1301. char *cmd)
  1302. {
  1303. struct wpa_interface iface;
  1304. char *pos;
  1305. /*
  1306. * <ifname>TAB<confname>TAB<driver>TAB<ctrl_interface>TAB<driver_param>
  1307. * TAB<bridge_ifname>
  1308. */
  1309. wpa_printf(MSG_DEBUG, "CTRL_IFACE GLOBAL INTERFACE_ADD '%s'", cmd);
  1310. os_memset(&iface, 0, sizeof(iface));
  1311. do {
  1312. iface.ifname = pos = cmd;
  1313. pos = os_strchr(pos, '\t');
  1314. if (pos)
  1315. *pos++ = '\0';
  1316. if (iface.ifname[0] == '\0')
  1317. return -1;
  1318. if (pos == NULL)
  1319. break;
  1320. iface.confname = pos;
  1321. pos = os_strchr(pos, '\t');
  1322. if (pos)
  1323. *pos++ = '\0';
  1324. if (iface.confname[0] == '\0')
  1325. iface.confname = NULL;
  1326. if (pos == NULL)
  1327. break;
  1328. iface.driver = pos;
  1329. pos = os_strchr(pos, '\t');
  1330. if (pos)
  1331. *pos++ = '\0';
  1332. if (iface.driver[0] == '\0')
  1333. iface.driver = NULL;
  1334. if (pos == NULL)
  1335. break;
  1336. iface.ctrl_interface = pos;
  1337. pos = os_strchr(pos, '\t');
  1338. if (pos)
  1339. *pos++ = '\0';
  1340. if (iface.ctrl_interface[0] == '\0')
  1341. iface.ctrl_interface = NULL;
  1342. if (pos == NULL)
  1343. break;
  1344. iface.driver_param = pos;
  1345. pos = os_strchr(pos, '\t');
  1346. if (pos)
  1347. *pos++ = '\0';
  1348. if (iface.driver_param[0] == '\0')
  1349. iface.driver_param = NULL;
  1350. if (pos == NULL)
  1351. break;
  1352. iface.bridge_ifname = pos;
  1353. pos = os_strchr(pos, '\t');
  1354. if (pos)
  1355. *pos++ = '\0';
  1356. if (iface.bridge_ifname[0] == '\0')
  1357. iface.bridge_ifname = NULL;
  1358. if (pos == NULL)
  1359. break;
  1360. } while (0);
  1361. if (wpa_supplicant_get_iface(global, iface.ifname))
  1362. return -1;
  1363. return wpa_supplicant_add_iface(global, &iface) ? 0 : -1;
  1364. }
  1365. static int wpa_supplicant_global_iface_remove(struct wpa_global *global,
  1366. char *cmd)
  1367. {
  1368. struct wpa_supplicant *wpa_s;
  1369. wpa_printf(MSG_DEBUG, "CTRL_IFACE GLOBAL INTERFACE_REMOVE '%s'", cmd);
  1370. wpa_s = wpa_supplicant_get_iface(global, cmd);
  1371. if (wpa_s == NULL)
  1372. return -1;
  1373. return wpa_supplicant_remove_iface(global, wpa_s);
  1374. }
  1375. static int wpa_supplicant_global_iface_interfaces(struct wpa_global *global,
  1376. char *buf, int len)
  1377. {
  1378. int res;
  1379. char *pos, *end;
  1380. struct wpa_supplicant *wpa_s;
  1381. wpa_s = global->ifaces;
  1382. pos = buf;
  1383. end = buf + len;
  1384. while (wpa_s) {
  1385. res = os_snprintf(pos, end - pos, "%s\n", wpa_s->ifname);
  1386. if (res < 0 || res >= end - pos) {
  1387. *pos = '\0';
  1388. break;
  1389. }
  1390. pos += res;
  1391. wpa_s = wpa_s->next;
  1392. }
  1393. return pos - buf;
  1394. }
  1395. char * wpa_supplicant_global_ctrl_iface_process(struct wpa_global *global,
  1396. char *buf, size_t *resp_len)
  1397. {
  1398. char *reply;
  1399. const int reply_size = 2048;
  1400. int reply_len;
  1401. wpa_hexdump_ascii(MSG_DEBUG, "RX global ctrl_iface",
  1402. (const u8 *) buf, os_strlen(buf));
  1403. reply = os_malloc(reply_size);
  1404. if (reply == NULL) {
  1405. *resp_len = 1;
  1406. return NULL;
  1407. }
  1408. os_memcpy(reply, "OK\n", 3);
  1409. reply_len = 3;
  1410. if (os_strcmp(buf, "PING") == 0) {
  1411. os_memcpy(reply, "PONG\n", 5);
  1412. reply_len = 5;
  1413. } else if (os_strncmp(buf, "INTERFACE_ADD ", 14) == 0) {
  1414. if (wpa_supplicant_global_iface_add(global, buf + 14))
  1415. reply_len = -1;
  1416. } else if (os_strncmp(buf, "INTERFACE_REMOVE ", 17) == 0) {
  1417. if (wpa_supplicant_global_iface_remove(global, buf + 17))
  1418. reply_len = -1;
  1419. } else if (os_strcmp(buf, "INTERFACES") == 0) {
  1420. reply_len = wpa_supplicant_global_iface_interfaces(
  1421. global, reply, reply_size);
  1422. } else if (os_strcmp(buf, "TERMINATE") == 0) {
  1423. eloop_terminate();
  1424. } else {
  1425. os_memcpy(reply, "UNKNOWN COMMAND\n", 16);
  1426. reply_len = 16;
  1427. }
  1428. if (reply_len < 0) {
  1429. os_memcpy(reply, "FAIL\n", 5);
  1430. reply_len = 5;
  1431. }
  1432. *resp_len = reply_len;
  1433. return reply;
  1434. }