ctrl_iface.c 49 KB

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