ctrl_iface.c 53 KB

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