ctrl_iface.c 49 KB

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