ctrl_iface.c 52 KB

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