ctrl_iface.c 40 KB

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