wpa_supplicant.c 77 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680
  1. /*
  2. * WPA Supplicant
  3. * Copyright (c) 2003-2011, 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. * This file implements functions for registering and unregistering
  15. * %wpa_supplicant interfaces. In addition, this file contains number of
  16. * functions for managing network connections.
  17. */
  18. #include "includes.h"
  19. #include "common.h"
  20. #include "eapol_supp/eapol_supp_sm.h"
  21. #include "eap_peer/eap.h"
  22. #include "eap_server/eap_methods.h"
  23. #include "rsn_supp/wpa.h"
  24. #include "eloop.h"
  25. #include "config.h"
  26. #include "l2_packet/l2_packet.h"
  27. #include "wpa_supplicant_i.h"
  28. #include "driver_i.h"
  29. #include "ctrl_iface.h"
  30. #include "pcsc_funcs.h"
  31. #include "common/version.h"
  32. #include "rsn_supp/preauth.h"
  33. #include "rsn_supp/pmksa_cache.h"
  34. #include "common/wpa_ctrl.h"
  35. #include "mlme.h"
  36. #include "common/ieee802_11_defs.h"
  37. #include "p2p/p2p.h"
  38. #include "blacklist.h"
  39. #include "wpas_glue.h"
  40. #include "wps_supplicant.h"
  41. #include "ibss_rsn.h"
  42. #include "sme.h"
  43. #include "ap.h"
  44. #include "p2p_supplicant.h"
  45. #include "notify.h"
  46. #include "bgscan.h"
  47. #include "bss.h"
  48. #include "scan.h"
  49. const char *wpa_supplicant_version =
  50. "wpa_supplicant v" VERSION_STR "\n"
  51. "Copyright (c) 2003-2011, Jouni Malinen <j@w1.fi> and contributors";
  52. const char *wpa_supplicant_license =
  53. "This program is free software. You can distribute it and/or modify it\n"
  54. "under the terms of the GNU General Public License version 2.\n"
  55. "\n"
  56. "Alternatively, this software may be distributed under the terms of the\n"
  57. "BSD license. See README and COPYING for more details.\n"
  58. #ifdef EAP_TLS_OPENSSL
  59. "\nThis product includes software developed by the OpenSSL Project\n"
  60. "for use in the OpenSSL Toolkit (http://www.openssl.org/)\n"
  61. #endif /* EAP_TLS_OPENSSL */
  62. ;
  63. #ifndef CONFIG_NO_STDOUT_DEBUG
  64. /* Long text divided into parts in order to fit in C89 strings size limits. */
  65. const char *wpa_supplicant_full_license1 =
  66. "This program is free software; you can redistribute it and/or modify\n"
  67. "it under the terms of the GNU General Public License version 2 as\n"
  68. "published by the Free Software Foundation.\n"
  69. "\n"
  70. "This program is distributed in the hope that it will be useful,\n"
  71. "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
  72. "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
  73. "GNU General Public License for more details.\n"
  74. "\n";
  75. const char *wpa_supplicant_full_license2 =
  76. "You should have received a copy of the GNU General Public License\n"
  77. "along with this program; if not, write to the Free Software\n"
  78. "Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA\n"
  79. "\n"
  80. "Alternatively, this software may be distributed under the terms of the\n"
  81. "BSD license.\n"
  82. "\n"
  83. "Redistribution and use in source and binary forms, with or without\n"
  84. "modification, are permitted provided that the following conditions are\n"
  85. "met:\n"
  86. "\n";
  87. const char *wpa_supplicant_full_license3 =
  88. "1. Redistributions of source code must retain the above copyright\n"
  89. " notice, this list of conditions and the following disclaimer.\n"
  90. "\n"
  91. "2. Redistributions in binary form must reproduce the above copyright\n"
  92. " notice, this list of conditions and the following disclaimer in the\n"
  93. " documentation and/or other materials provided with the distribution.\n"
  94. "\n";
  95. const char *wpa_supplicant_full_license4 =
  96. "3. Neither the name(s) of the above-listed copyright holder(s) nor the\n"
  97. " names of its contributors may be used to endorse or promote products\n"
  98. " derived from this software without specific prior written permission.\n"
  99. "\n"
  100. "THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS\n"
  101. "\"AS IS\" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT\n"
  102. "LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR\n"
  103. "A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT\n";
  104. const char *wpa_supplicant_full_license5 =
  105. "OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,\n"
  106. "SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n"
  107. "LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,\n"
  108. "DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY\n"
  109. "THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\n"
  110. "(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE\n"
  111. "OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n"
  112. "\n";
  113. #endif /* CONFIG_NO_STDOUT_DEBUG */
  114. extern int wpa_debug_level;
  115. extern int wpa_debug_show_keys;
  116. extern int wpa_debug_timestamp;
  117. extern struct wpa_driver_ops *wpa_drivers[];
  118. /* Configure default/group WEP keys for static WEP */
  119. int wpa_set_wep_keys(struct wpa_supplicant *wpa_s, struct wpa_ssid *ssid)
  120. {
  121. int i, set = 0;
  122. for (i = 0; i < NUM_WEP_KEYS; i++) {
  123. if (ssid->wep_key_len[i] == 0)
  124. continue;
  125. set = 1;
  126. wpa_drv_set_key(wpa_s, WPA_ALG_WEP, NULL,
  127. i, i == ssid->wep_tx_keyidx, NULL, 0,
  128. ssid->wep_key[i], ssid->wep_key_len[i]);
  129. }
  130. return set;
  131. }
  132. static int wpa_supplicant_set_wpa_none_key(struct wpa_supplicant *wpa_s,
  133. struct wpa_ssid *ssid)
  134. {
  135. u8 key[32];
  136. size_t keylen;
  137. enum wpa_alg alg;
  138. u8 seq[6] = { 0 };
  139. /* IBSS/WPA-None uses only one key (Group) for both receiving and
  140. * sending unicast and multicast packets. */
  141. if (ssid->mode != WPAS_MODE_IBSS) {
  142. wpa_msg(wpa_s, MSG_INFO, "WPA: Invalid mode %d (not "
  143. "IBSS/ad-hoc) for WPA-None", ssid->mode);
  144. return -1;
  145. }
  146. if (!ssid->psk_set) {
  147. wpa_msg(wpa_s, MSG_INFO, "WPA: No PSK configured for "
  148. "WPA-None");
  149. return -1;
  150. }
  151. switch (wpa_s->group_cipher) {
  152. case WPA_CIPHER_CCMP:
  153. os_memcpy(key, ssid->psk, 16);
  154. keylen = 16;
  155. alg = WPA_ALG_CCMP;
  156. break;
  157. case WPA_CIPHER_TKIP:
  158. /* WPA-None uses the same Michael MIC key for both TX and RX */
  159. os_memcpy(key, ssid->psk, 16 + 8);
  160. os_memcpy(key + 16 + 8, ssid->psk + 16, 8);
  161. keylen = 32;
  162. alg = WPA_ALG_TKIP;
  163. break;
  164. default:
  165. wpa_msg(wpa_s, MSG_INFO, "WPA: Invalid group cipher %d for "
  166. "WPA-None", wpa_s->group_cipher);
  167. return -1;
  168. }
  169. /* TODO: should actually remember the previously used seq#, both for TX
  170. * and RX from each STA.. */
  171. return wpa_drv_set_key(wpa_s, alg, NULL, 0, 1, seq, 6, key, keylen);
  172. }
  173. static void wpa_supplicant_timeout(void *eloop_ctx, void *timeout_ctx)
  174. {
  175. struct wpa_supplicant *wpa_s = eloop_ctx;
  176. const u8 *bssid = wpa_s->bssid;
  177. if (is_zero_ether_addr(bssid))
  178. bssid = wpa_s->pending_bssid;
  179. wpa_msg(wpa_s, MSG_INFO, "Authentication with " MACSTR " timed out.",
  180. MAC2STR(bssid));
  181. wpa_blacklist_add(wpa_s, bssid);
  182. wpa_sm_notify_disassoc(wpa_s->wpa);
  183. wpa_supplicant_disassociate(wpa_s, WLAN_REASON_DEAUTH_LEAVING);
  184. wpa_s->reassociate = 1;
  185. /*
  186. * If we timed out, the AP or the local radio may be busy.
  187. * So, wait a second until scanning again.
  188. */
  189. wpa_supplicant_req_scan(wpa_s, 1, 0);
  190. }
  191. /**
  192. * wpa_supplicant_req_auth_timeout - Schedule a timeout for authentication
  193. * @wpa_s: Pointer to wpa_supplicant data
  194. * @sec: Number of seconds after which to time out authentication
  195. * @usec: Number of microseconds after which to time out authentication
  196. *
  197. * This function is used to schedule a timeout for the current authentication
  198. * attempt.
  199. */
  200. void wpa_supplicant_req_auth_timeout(struct wpa_supplicant *wpa_s,
  201. int sec, int usec)
  202. {
  203. if (wpa_s->conf && wpa_s->conf->ap_scan == 0 &&
  204. (wpa_s->drv_flags & WPA_DRIVER_FLAGS_WIRED))
  205. return;
  206. wpa_dbg(wpa_s, MSG_DEBUG, "Setting authentication timeout: %d sec "
  207. "%d usec", sec, usec);
  208. eloop_cancel_timeout(wpa_supplicant_timeout, wpa_s, NULL);
  209. eloop_register_timeout(sec, usec, wpa_supplicant_timeout, wpa_s, NULL);
  210. }
  211. /**
  212. * wpa_supplicant_cancel_auth_timeout - Cancel authentication timeout
  213. * @wpa_s: Pointer to wpa_supplicant data
  214. *
  215. * This function is used to cancel authentication timeout scheduled with
  216. * wpa_supplicant_req_auth_timeout() and it is called when authentication has
  217. * been completed.
  218. */
  219. void wpa_supplicant_cancel_auth_timeout(struct wpa_supplicant *wpa_s)
  220. {
  221. wpa_dbg(wpa_s, MSG_DEBUG, "Cancelling authentication timeout");
  222. eloop_cancel_timeout(wpa_supplicant_timeout, wpa_s, NULL);
  223. wpa_blacklist_del(wpa_s, wpa_s->bssid);
  224. }
  225. /**
  226. * wpa_supplicant_initiate_eapol - Configure EAPOL state machine
  227. * @wpa_s: Pointer to wpa_supplicant data
  228. *
  229. * This function is used to configure EAPOL state machine based on the selected
  230. * authentication mode.
  231. */
  232. void wpa_supplicant_initiate_eapol(struct wpa_supplicant *wpa_s)
  233. {
  234. #ifdef IEEE8021X_EAPOL
  235. struct eapol_config eapol_conf;
  236. struct wpa_ssid *ssid = wpa_s->current_ssid;
  237. #ifdef CONFIG_IBSS_RSN
  238. if (ssid->mode == WPAS_MODE_IBSS &&
  239. wpa_s->key_mgmt != WPA_KEY_MGMT_NONE &&
  240. wpa_s->key_mgmt != WPA_KEY_MGMT_WPA_NONE) {
  241. /*
  242. * RSN IBSS authentication is per-STA and we can disable the
  243. * per-BSSID EAPOL authentication.
  244. */
  245. eapol_sm_notify_portControl(wpa_s->eapol, ForceAuthorized);
  246. eapol_sm_notify_eap_success(wpa_s->eapol, TRUE);
  247. eapol_sm_notify_eap_fail(wpa_s->eapol, FALSE);
  248. return;
  249. }
  250. #endif /* CONFIG_IBSS_RSN */
  251. eapol_sm_notify_eap_success(wpa_s->eapol, FALSE);
  252. eapol_sm_notify_eap_fail(wpa_s->eapol, FALSE);
  253. if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE ||
  254. wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE)
  255. eapol_sm_notify_portControl(wpa_s->eapol, ForceAuthorized);
  256. else
  257. eapol_sm_notify_portControl(wpa_s->eapol, Auto);
  258. os_memset(&eapol_conf, 0, sizeof(eapol_conf));
  259. if (wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
  260. eapol_conf.accept_802_1x_keys = 1;
  261. eapol_conf.required_keys = 0;
  262. if (ssid->eapol_flags & EAPOL_FLAG_REQUIRE_KEY_UNICAST) {
  263. eapol_conf.required_keys |= EAPOL_REQUIRE_KEY_UNICAST;
  264. }
  265. if (ssid->eapol_flags & EAPOL_FLAG_REQUIRE_KEY_BROADCAST) {
  266. eapol_conf.required_keys |=
  267. EAPOL_REQUIRE_KEY_BROADCAST;
  268. }
  269. if (wpa_s->conf && (wpa_s->drv_flags & WPA_DRIVER_FLAGS_WIRED))
  270. eapol_conf.required_keys = 0;
  271. }
  272. if (wpa_s->conf)
  273. eapol_conf.fast_reauth = wpa_s->conf->fast_reauth;
  274. eapol_conf.workaround = ssid->eap_workaround;
  275. eapol_conf.eap_disabled =
  276. !wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt) &&
  277. wpa_s->key_mgmt != WPA_KEY_MGMT_IEEE8021X_NO_WPA &&
  278. wpa_s->key_mgmt != WPA_KEY_MGMT_WPS;
  279. eapol_sm_notify_config(wpa_s->eapol, &ssid->eap, &eapol_conf);
  280. #endif /* IEEE8021X_EAPOL */
  281. }
  282. /**
  283. * wpa_supplicant_set_non_wpa_policy - Set WPA parameters to non-WPA mode
  284. * @wpa_s: Pointer to wpa_supplicant data
  285. * @ssid: Configuration data for the network
  286. *
  287. * This function is used to configure WPA state machine and related parameters
  288. * to a mode where WPA is not enabled. This is called as part of the
  289. * authentication configuration when the selected network does not use WPA.
  290. */
  291. void wpa_supplicant_set_non_wpa_policy(struct wpa_supplicant *wpa_s,
  292. struct wpa_ssid *ssid)
  293. {
  294. int i;
  295. if (ssid->key_mgmt & WPA_KEY_MGMT_WPS)
  296. wpa_s->key_mgmt = WPA_KEY_MGMT_WPS;
  297. else if (ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA)
  298. wpa_s->key_mgmt = WPA_KEY_MGMT_IEEE8021X_NO_WPA;
  299. else
  300. wpa_s->key_mgmt = WPA_KEY_MGMT_NONE;
  301. wpa_sm_set_ap_wpa_ie(wpa_s->wpa, NULL, 0);
  302. wpa_sm_set_ap_rsn_ie(wpa_s->wpa, NULL, 0);
  303. wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, NULL, 0);
  304. wpa_s->pairwise_cipher = WPA_CIPHER_NONE;
  305. wpa_s->group_cipher = WPA_CIPHER_NONE;
  306. wpa_s->mgmt_group_cipher = 0;
  307. for (i = 0; i < NUM_WEP_KEYS; i++) {
  308. if (ssid->wep_key_len[i] > 5) {
  309. wpa_s->pairwise_cipher = WPA_CIPHER_WEP104;
  310. wpa_s->group_cipher = WPA_CIPHER_WEP104;
  311. break;
  312. } else if (ssid->wep_key_len[i] > 0) {
  313. wpa_s->pairwise_cipher = WPA_CIPHER_WEP40;
  314. wpa_s->group_cipher = WPA_CIPHER_WEP40;
  315. break;
  316. }
  317. }
  318. wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_RSN_ENABLED, 0);
  319. wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_KEY_MGMT, wpa_s->key_mgmt);
  320. wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_PAIRWISE,
  321. wpa_s->pairwise_cipher);
  322. wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_GROUP, wpa_s->group_cipher);
  323. #ifdef CONFIG_IEEE80211W
  324. wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_MGMT_GROUP,
  325. wpa_s->mgmt_group_cipher);
  326. #endif /* CONFIG_IEEE80211W */
  327. pmksa_cache_clear_current(wpa_s->wpa);
  328. }
  329. static void wpa_supplicant_cleanup(struct wpa_supplicant *wpa_s)
  330. {
  331. bgscan_deinit(wpa_s);
  332. scard_deinit(wpa_s->scard);
  333. wpa_s->scard = NULL;
  334. wpa_sm_set_scard_ctx(wpa_s->wpa, NULL);
  335. eapol_sm_register_scard_ctx(wpa_s->eapol, NULL);
  336. l2_packet_deinit(wpa_s->l2);
  337. wpa_s->l2 = NULL;
  338. if (wpa_s->l2_br) {
  339. l2_packet_deinit(wpa_s->l2_br);
  340. wpa_s->l2_br = NULL;
  341. }
  342. if (wpa_s->ctrl_iface) {
  343. wpa_supplicant_ctrl_iface_deinit(wpa_s->ctrl_iface);
  344. wpa_s->ctrl_iface = NULL;
  345. }
  346. if (wpa_s->conf != NULL) {
  347. struct wpa_ssid *ssid;
  348. for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next)
  349. wpas_notify_network_removed(wpa_s, ssid);
  350. wpa_config_free(wpa_s->conf);
  351. wpa_s->conf = NULL;
  352. }
  353. os_free(wpa_s->confname);
  354. wpa_s->confname = NULL;
  355. wpa_sm_set_eapol(wpa_s->wpa, NULL);
  356. eapol_sm_deinit(wpa_s->eapol);
  357. wpa_s->eapol = NULL;
  358. rsn_preauth_deinit(wpa_s->wpa);
  359. pmksa_candidate_free(wpa_s->wpa);
  360. wpa_sm_deinit(wpa_s->wpa);
  361. wpa_s->wpa = NULL;
  362. wpa_blacklist_clear(wpa_s);
  363. wpa_bss_deinit(wpa_s);
  364. wpa_supplicant_cancel_scan(wpa_s);
  365. wpa_supplicant_cancel_auth_timeout(wpa_s);
  366. ieee80211_sta_deinit(wpa_s);
  367. wpas_wps_deinit(wpa_s);
  368. wpabuf_free(wpa_s->pending_eapol_rx);
  369. wpa_s->pending_eapol_rx = NULL;
  370. #ifdef CONFIG_IBSS_RSN
  371. ibss_rsn_deinit(wpa_s->ibss_rsn);
  372. wpa_s->ibss_rsn = NULL;
  373. #endif /* CONFIG_IBSS_RSN */
  374. sme_deinit(wpa_s);
  375. #ifdef CONFIG_AP
  376. wpa_supplicant_ap_deinit(wpa_s);
  377. #endif /* CONFIG_AP */
  378. #ifdef CONFIG_P2P
  379. wpas_p2p_deinit(wpa_s);
  380. #endif /* CONFIG_P2P */
  381. os_free(wpa_s->next_scan_freqs);
  382. wpa_s->next_scan_freqs = NULL;
  383. }
  384. /**
  385. * wpa_clear_keys - Clear keys configured for the driver
  386. * @wpa_s: Pointer to wpa_supplicant data
  387. * @addr: Previously used BSSID or %NULL if not available
  388. *
  389. * This function clears the encryption keys that has been previously configured
  390. * for the driver.
  391. */
  392. void wpa_clear_keys(struct wpa_supplicant *wpa_s, const u8 *addr)
  393. {
  394. if (wpa_s->keys_cleared) {
  395. /* Some drivers (e.g., ndiswrapper & NDIS drivers) seem to have
  396. * timing issues with keys being cleared just before new keys
  397. * are set or just after association or something similar. This
  398. * shows up in group key handshake failing often because of the
  399. * client not receiving the first encrypted packets correctly.
  400. * Skipping some of the extra key clearing steps seems to help
  401. * in completing group key handshake more reliably. */
  402. wpa_dbg(wpa_s, MSG_DEBUG, "No keys have been configured - "
  403. "skip key clearing");
  404. return;
  405. }
  406. /* MLME-DELETEKEYS.request */
  407. wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 0, 0, NULL, 0, NULL, 0);
  408. wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 1, 0, NULL, 0, NULL, 0);
  409. wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 2, 0, NULL, 0, NULL, 0);
  410. wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 3, 0, NULL, 0, NULL, 0);
  411. #ifdef CONFIG_IEEE80211W
  412. wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 4, 0, NULL, 0, NULL, 0);
  413. wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 5, 0, NULL, 0, NULL, 0);
  414. #endif /* CONFIG_IEEE80211W */
  415. if (addr) {
  416. wpa_drv_set_key(wpa_s, WPA_ALG_NONE, addr, 0, 0, NULL, 0, NULL,
  417. 0);
  418. /* MLME-SETPROTECTION.request(None) */
  419. wpa_drv_mlme_setprotection(
  420. wpa_s, addr,
  421. MLME_SETPROTECTION_PROTECT_TYPE_NONE,
  422. MLME_SETPROTECTION_KEY_TYPE_PAIRWISE);
  423. }
  424. wpa_s->keys_cleared = 1;
  425. }
  426. /**
  427. * wpa_supplicant_state_txt - Get the connection state name as a text string
  428. * @state: State (wpa_state; WPA_*)
  429. * Returns: The state name as a printable text string
  430. */
  431. const char * wpa_supplicant_state_txt(enum wpa_states state)
  432. {
  433. switch (state) {
  434. case WPA_DISCONNECTED:
  435. return "DISCONNECTED";
  436. case WPA_INACTIVE:
  437. return "INACTIVE";
  438. case WPA_INTERFACE_DISABLED:
  439. return "INTERFACE_DISABLED";
  440. case WPA_SCANNING:
  441. return "SCANNING";
  442. case WPA_AUTHENTICATING:
  443. return "AUTHENTICATING";
  444. case WPA_ASSOCIATING:
  445. return "ASSOCIATING";
  446. case WPA_ASSOCIATED:
  447. return "ASSOCIATED";
  448. case WPA_4WAY_HANDSHAKE:
  449. return "4WAY_HANDSHAKE";
  450. case WPA_GROUP_HANDSHAKE:
  451. return "GROUP_HANDSHAKE";
  452. case WPA_COMPLETED:
  453. return "COMPLETED";
  454. default:
  455. return "UNKNOWN";
  456. }
  457. }
  458. /**
  459. * wpa_supplicant_set_state - Set current connection state
  460. * @wpa_s: Pointer to wpa_supplicant data
  461. * @state: The new connection state
  462. *
  463. * This function is called whenever the connection state changes, e.g.,
  464. * association is completed for WPA/WPA2 4-Way Handshake is started.
  465. */
  466. void wpa_supplicant_set_state(struct wpa_supplicant *wpa_s,
  467. enum wpa_states state)
  468. {
  469. enum wpa_states old_state = wpa_s->wpa_state;
  470. wpa_dbg(wpa_s, MSG_DEBUG, "State: %s -> %s",
  471. wpa_supplicant_state_txt(wpa_s->wpa_state),
  472. wpa_supplicant_state_txt(state));
  473. if (state != WPA_SCANNING)
  474. wpa_supplicant_notify_scanning(wpa_s, 0);
  475. if (state == WPA_COMPLETED && wpa_s->new_connection) {
  476. #if defined(CONFIG_CTRL_IFACE) || !defined(CONFIG_NO_STDOUT_DEBUG)
  477. struct wpa_ssid *ssid = wpa_s->current_ssid;
  478. wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_CONNECTED "- Connection to "
  479. MACSTR " completed %s [id=%d id_str=%s]",
  480. MAC2STR(wpa_s->bssid), wpa_s->reassociated_connection ?
  481. "(reauth)" : "(auth)",
  482. ssid ? ssid->id : -1,
  483. ssid && ssid->id_str ? ssid->id_str : "");
  484. #endif /* CONFIG_CTRL_IFACE || !CONFIG_NO_STDOUT_DEBUG */
  485. wpa_s->new_connection = 0;
  486. wpa_s->reassociated_connection = 1;
  487. wpa_drv_set_operstate(wpa_s, 1);
  488. #ifndef IEEE8021X_EAPOL
  489. wpa_drv_set_supp_port(wpa_s, 1);
  490. #endif /* IEEE8021X_EAPOL */
  491. wpa_s->after_wps = 0;
  492. #ifdef CONFIG_P2P
  493. wpas_p2p_completed(wpa_s);
  494. #endif /* CONFIG_P2P */
  495. } else if (state == WPA_DISCONNECTED || state == WPA_ASSOCIATING ||
  496. state == WPA_ASSOCIATED) {
  497. wpa_s->new_connection = 1;
  498. wpa_drv_set_operstate(wpa_s, 0);
  499. #ifndef IEEE8021X_EAPOL
  500. wpa_drv_set_supp_port(wpa_s, 0);
  501. #endif /* IEEE8021X_EAPOL */
  502. }
  503. wpa_s->wpa_state = state;
  504. if (wpa_s->wpa_state != old_state)
  505. wpas_notify_state_changed(wpa_s, wpa_s->wpa_state, old_state);
  506. }
  507. void wpa_supplicant_terminate_proc(struct wpa_global *global)
  508. {
  509. int pending = 0;
  510. #ifdef CONFIG_WPS
  511. struct wpa_supplicant *wpa_s = global->ifaces;
  512. while (wpa_s) {
  513. if (wpas_wps_terminate_pending(wpa_s) == 1)
  514. pending = 1;
  515. wpa_s = wpa_s->next;
  516. }
  517. #endif /* CONFIG_WPS */
  518. if (pending)
  519. return;
  520. eloop_terminate();
  521. }
  522. static void wpa_supplicant_terminate(int sig, void *signal_ctx)
  523. {
  524. struct wpa_global *global = signal_ctx;
  525. struct wpa_supplicant *wpa_s;
  526. for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
  527. wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_TERMINATING "- signal %d "
  528. "received", sig);
  529. }
  530. wpa_supplicant_terminate_proc(global);
  531. }
  532. void wpa_supplicant_clear_status(struct wpa_supplicant *wpa_s)
  533. {
  534. enum wpa_states old_state = wpa_s->wpa_state;
  535. wpa_s->pairwise_cipher = 0;
  536. wpa_s->group_cipher = 0;
  537. wpa_s->mgmt_group_cipher = 0;
  538. wpa_s->key_mgmt = 0;
  539. if (wpa_s->wpa_state != WPA_INTERFACE_DISABLED)
  540. wpa_s->wpa_state = WPA_DISCONNECTED;
  541. if (wpa_s->wpa_state != old_state)
  542. wpas_notify_state_changed(wpa_s, wpa_s->wpa_state, old_state);
  543. }
  544. /**
  545. * wpa_supplicant_reload_configuration - Reload configuration data
  546. * @wpa_s: Pointer to wpa_supplicant data
  547. * Returns: 0 on success or -1 if configuration parsing failed
  548. *
  549. * This function can be used to request that the configuration data is reloaded
  550. * (e.g., after configuration file change). This function is reloading
  551. * configuration only for one interface, so this may need to be called multiple
  552. * times if %wpa_supplicant is controlling multiple interfaces and all
  553. * interfaces need reconfiguration.
  554. */
  555. int wpa_supplicant_reload_configuration(struct wpa_supplicant *wpa_s)
  556. {
  557. struct wpa_config *conf;
  558. struct wpa_ssid *old_ssid;
  559. int reconf_ctrl;
  560. int old_ap_scan;
  561. if (wpa_s->confname == NULL)
  562. return -1;
  563. conf = wpa_config_read(wpa_s->confname);
  564. if (conf == NULL) {
  565. wpa_msg(wpa_s, MSG_ERROR, "Failed to parse the configuration "
  566. "file '%s' - exiting", wpa_s->confname);
  567. return -1;
  568. }
  569. conf->changed_parameters = (unsigned int) -1;
  570. reconf_ctrl = !!conf->ctrl_interface != !!wpa_s->conf->ctrl_interface
  571. || (conf->ctrl_interface && wpa_s->conf->ctrl_interface &&
  572. os_strcmp(conf->ctrl_interface,
  573. wpa_s->conf->ctrl_interface) != 0);
  574. if (reconf_ctrl && wpa_s->ctrl_iface) {
  575. wpa_supplicant_ctrl_iface_deinit(wpa_s->ctrl_iface);
  576. wpa_s->ctrl_iface = NULL;
  577. }
  578. eapol_sm_invalidate_cached_session(wpa_s->eapol);
  579. old_ssid = wpa_s->current_ssid;
  580. wpa_s->current_ssid = NULL;
  581. if (old_ssid != wpa_s->current_ssid)
  582. wpas_notify_network_changed(wpa_s);
  583. /*
  584. * TODO: should notify EAPOL SM about changes in opensc_engine_path,
  585. * pkcs11_engine_path, pkcs11_module_path.
  586. */
  587. if (wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt)) {
  588. /*
  589. * Clear forced success to clear EAP state for next
  590. * authentication.
  591. */
  592. eapol_sm_notify_eap_success(wpa_s->eapol, FALSE);
  593. }
  594. eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
  595. wpa_sm_set_config(wpa_s->wpa, NULL);
  596. wpa_sm_set_fast_reauth(wpa_s->wpa, wpa_s->conf->fast_reauth);
  597. rsn_preauth_deinit(wpa_s->wpa);
  598. old_ap_scan = wpa_s->conf->ap_scan;
  599. wpa_config_free(wpa_s->conf);
  600. wpa_s->conf = conf;
  601. if (old_ap_scan != wpa_s->conf->ap_scan)
  602. wpas_notify_ap_scan_changed(wpa_s);
  603. if (reconf_ctrl)
  604. wpa_s->ctrl_iface = wpa_supplicant_ctrl_iface_init(wpa_s);
  605. wpa_supplicant_update_config(wpa_s);
  606. wpa_supplicant_clear_status(wpa_s);
  607. if (wpa_supplicant_enabled_networks(wpa_s->conf)) {
  608. wpa_s->reassociate = 1;
  609. wpa_supplicant_req_scan(wpa_s, 0, 0);
  610. }
  611. wpa_dbg(wpa_s, MSG_DEBUG, "Reconfiguration completed");
  612. return 0;
  613. }
  614. static void wpa_supplicant_reconfig(int sig, void *signal_ctx)
  615. {
  616. struct wpa_global *global = signal_ctx;
  617. struct wpa_supplicant *wpa_s;
  618. for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
  619. wpa_dbg(wpa_s, MSG_DEBUG, "Signal %d received - reconfiguring",
  620. sig);
  621. if (wpa_supplicant_reload_configuration(wpa_s) < 0) {
  622. wpa_supplicant_terminate_proc(global);
  623. }
  624. }
  625. }
  626. enum wpa_cipher cipher_suite2driver(int cipher)
  627. {
  628. switch (cipher) {
  629. case WPA_CIPHER_NONE:
  630. return CIPHER_NONE;
  631. case WPA_CIPHER_WEP40:
  632. return CIPHER_WEP40;
  633. case WPA_CIPHER_WEP104:
  634. return CIPHER_WEP104;
  635. case WPA_CIPHER_CCMP:
  636. return CIPHER_CCMP;
  637. case WPA_CIPHER_TKIP:
  638. default:
  639. return CIPHER_TKIP;
  640. }
  641. }
  642. enum wpa_key_mgmt key_mgmt2driver(int key_mgmt)
  643. {
  644. switch (key_mgmt) {
  645. case WPA_KEY_MGMT_NONE:
  646. return KEY_MGMT_NONE;
  647. case WPA_KEY_MGMT_IEEE8021X_NO_WPA:
  648. return KEY_MGMT_802_1X_NO_WPA;
  649. case WPA_KEY_MGMT_IEEE8021X:
  650. return KEY_MGMT_802_1X;
  651. case WPA_KEY_MGMT_WPA_NONE:
  652. return KEY_MGMT_WPA_NONE;
  653. case WPA_KEY_MGMT_FT_IEEE8021X:
  654. return KEY_MGMT_FT_802_1X;
  655. case WPA_KEY_MGMT_FT_PSK:
  656. return KEY_MGMT_FT_PSK;
  657. case WPA_KEY_MGMT_IEEE8021X_SHA256:
  658. return KEY_MGMT_802_1X_SHA256;
  659. case WPA_KEY_MGMT_PSK_SHA256:
  660. return KEY_MGMT_PSK_SHA256;
  661. case WPA_KEY_MGMT_WPS:
  662. return KEY_MGMT_WPS;
  663. case WPA_KEY_MGMT_PSK:
  664. default:
  665. return KEY_MGMT_PSK;
  666. }
  667. }
  668. static int wpa_supplicant_suites_from_ai(struct wpa_supplicant *wpa_s,
  669. struct wpa_ssid *ssid,
  670. struct wpa_ie_data *ie)
  671. {
  672. int ret = wpa_sm_parse_own_wpa_ie(wpa_s->wpa, ie);
  673. if (ret) {
  674. if (ret == -2) {
  675. wpa_msg(wpa_s, MSG_INFO, "WPA: Failed to parse WPA IE "
  676. "from association info");
  677. }
  678. return -1;
  679. }
  680. wpa_dbg(wpa_s, MSG_DEBUG, "WPA: Using WPA IE from AssocReq to set "
  681. "cipher suites");
  682. if (!(ie->group_cipher & ssid->group_cipher)) {
  683. wpa_msg(wpa_s, MSG_INFO, "WPA: Driver used disabled group "
  684. "cipher 0x%x (mask 0x%x) - reject",
  685. ie->group_cipher, ssid->group_cipher);
  686. return -1;
  687. }
  688. if (!(ie->pairwise_cipher & ssid->pairwise_cipher)) {
  689. wpa_msg(wpa_s, MSG_INFO, "WPA: Driver used disabled pairwise "
  690. "cipher 0x%x (mask 0x%x) - reject",
  691. ie->pairwise_cipher, ssid->pairwise_cipher);
  692. return -1;
  693. }
  694. if (!(ie->key_mgmt & ssid->key_mgmt)) {
  695. wpa_msg(wpa_s, MSG_INFO, "WPA: Driver used disabled key "
  696. "management 0x%x (mask 0x%x) - reject",
  697. ie->key_mgmt, ssid->key_mgmt);
  698. return -1;
  699. }
  700. #ifdef CONFIG_IEEE80211W
  701. if (!(ie->capabilities & WPA_CAPABILITY_MFPC) &&
  702. ssid->ieee80211w == MGMT_FRAME_PROTECTION_REQUIRED) {
  703. wpa_msg(wpa_s, MSG_INFO, "WPA: Driver associated with an AP "
  704. "that does not support management frame protection - "
  705. "reject");
  706. return -1;
  707. }
  708. #endif /* CONFIG_IEEE80211W */
  709. return 0;
  710. }
  711. /**
  712. * wpa_supplicant_set_suites - Set authentication and encryption parameters
  713. * @wpa_s: Pointer to wpa_supplicant data
  714. * @bss: Scan results for the selected BSS, or %NULL if not available
  715. * @ssid: Configuration data for the selected network
  716. * @wpa_ie: Buffer for the WPA/RSN IE
  717. * @wpa_ie_len: Maximum wpa_ie buffer size on input. This is changed to be the
  718. * used buffer length in case the functions returns success.
  719. * Returns: 0 on success or -1 on failure
  720. *
  721. * This function is used to configure authentication and encryption parameters
  722. * based on the network configuration and scan result for the selected BSS (if
  723. * available).
  724. */
  725. int wpa_supplicant_set_suites(struct wpa_supplicant *wpa_s,
  726. struct wpa_bss *bss, struct wpa_ssid *ssid,
  727. u8 *wpa_ie, size_t *wpa_ie_len)
  728. {
  729. struct wpa_ie_data ie;
  730. int sel, proto;
  731. const u8 *bss_wpa, *bss_rsn;
  732. if (bss) {
  733. bss_wpa = wpa_bss_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
  734. bss_rsn = wpa_bss_get_ie(bss, WLAN_EID_RSN);
  735. } else
  736. bss_wpa = bss_rsn = NULL;
  737. if (bss_rsn && (ssid->proto & WPA_PROTO_RSN) &&
  738. wpa_parse_wpa_ie(bss_rsn, 2 + bss_rsn[1], &ie) == 0 &&
  739. (ie.group_cipher & ssid->group_cipher) &&
  740. (ie.pairwise_cipher & ssid->pairwise_cipher) &&
  741. (ie.key_mgmt & ssid->key_mgmt)) {
  742. wpa_dbg(wpa_s, MSG_DEBUG, "RSN: using IEEE 802.11i/D9.0");
  743. proto = WPA_PROTO_RSN;
  744. } else if (bss_wpa && (ssid->proto & WPA_PROTO_WPA) &&
  745. wpa_parse_wpa_ie(bss_wpa, 2 +bss_wpa[1], &ie) == 0 &&
  746. (ie.group_cipher & ssid->group_cipher) &&
  747. (ie.pairwise_cipher & ssid->pairwise_cipher) &&
  748. (ie.key_mgmt & ssid->key_mgmt)) {
  749. wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using IEEE 802.11i/D3.0");
  750. proto = WPA_PROTO_WPA;
  751. } else if (bss) {
  752. wpa_msg(wpa_s, MSG_WARNING, "WPA: Failed to select WPA/RSN");
  753. return -1;
  754. } else {
  755. if (ssid->proto & WPA_PROTO_RSN)
  756. proto = WPA_PROTO_RSN;
  757. else
  758. proto = WPA_PROTO_WPA;
  759. if (wpa_supplicant_suites_from_ai(wpa_s, ssid, &ie) < 0) {
  760. os_memset(&ie, 0, sizeof(ie));
  761. ie.group_cipher = ssid->group_cipher;
  762. ie.pairwise_cipher = ssid->pairwise_cipher;
  763. ie.key_mgmt = ssid->key_mgmt;
  764. #ifdef CONFIG_IEEE80211W
  765. ie.mgmt_group_cipher =
  766. ssid->ieee80211w != NO_MGMT_FRAME_PROTECTION ?
  767. WPA_CIPHER_AES_128_CMAC : 0;
  768. #endif /* CONFIG_IEEE80211W */
  769. wpa_dbg(wpa_s, MSG_DEBUG, "WPA: Set cipher suites "
  770. "based on configuration");
  771. } else
  772. proto = ie.proto;
  773. }
  774. wpa_dbg(wpa_s, MSG_DEBUG, "WPA: Selected cipher suites: group %d "
  775. "pairwise %d key_mgmt %d proto %d",
  776. ie.group_cipher, ie.pairwise_cipher, ie.key_mgmt, proto);
  777. #ifdef CONFIG_IEEE80211W
  778. if (ssid->ieee80211w) {
  779. wpa_dbg(wpa_s, MSG_DEBUG, "WPA: Selected mgmt group cipher %d",
  780. ie.mgmt_group_cipher);
  781. }
  782. #endif /* CONFIG_IEEE80211W */
  783. wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_PROTO, proto);
  784. wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_RSN_ENABLED,
  785. !!(ssid->proto & WPA_PROTO_RSN));
  786. if (bss || !wpa_s->ap_ies_from_associnfo) {
  787. if (wpa_sm_set_ap_wpa_ie(wpa_s->wpa, bss_wpa,
  788. bss_wpa ? 2 + bss_wpa[1] : 0) ||
  789. wpa_sm_set_ap_rsn_ie(wpa_s->wpa, bss_rsn,
  790. bss_rsn ? 2 + bss_rsn[1] : 0))
  791. return -1;
  792. }
  793. sel = ie.group_cipher & ssid->group_cipher;
  794. if (sel & WPA_CIPHER_CCMP) {
  795. wpa_s->group_cipher = WPA_CIPHER_CCMP;
  796. wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using GTK CCMP");
  797. } else if (sel & WPA_CIPHER_TKIP) {
  798. wpa_s->group_cipher = WPA_CIPHER_TKIP;
  799. wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using GTK TKIP");
  800. } else if (sel & WPA_CIPHER_WEP104) {
  801. wpa_s->group_cipher = WPA_CIPHER_WEP104;
  802. wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using GTK WEP104");
  803. } else if (sel & WPA_CIPHER_WEP40) {
  804. wpa_s->group_cipher = WPA_CIPHER_WEP40;
  805. wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using GTK WEP40");
  806. } else {
  807. wpa_msg(wpa_s, MSG_WARNING, "WPA: Failed to select group "
  808. "cipher");
  809. return -1;
  810. }
  811. sel = ie.pairwise_cipher & ssid->pairwise_cipher;
  812. if (sel & WPA_CIPHER_CCMP) {
  813. wpa_s->pairwise_cipher = WPA_CIPHER_CCMP;
  814. wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using PTK CCMP");
  815. } else if (sel & WPA_CIPHER_TKIP) {
  816. wpa_s->pairwise_cipher = WPA_CIPHER_TKIP;
  817. wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using PTK TKIP");
  818. } else if (sel & WPA_CIPHER_NONE) {
  819. wpa_s->pairwise_cipher = WPA_CIPHER_NONE;
  820. wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using PTK NONE");
  821. } else {
  822. wpa_msg(wpa_s, MSG_WARNING, "WPA: Failed to select pairwise "
  823. "cipher");
  824. return -1;
  825. }
  826. sel = ie.key_mgmt & ssid->key_mgmt;
  827. if (0) {
  828. #ifdef CONFIG_IEEE80211R
  829. } else if (sel & WPA_KEY_MGMT_FT_IEEE8021X) {
  830. wpa_s->key_mgmt = WPA_KEY_MGMT_FT_IEEE8021X;
  831. wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using KEY_MGMT FT/802.1X");
  832. } else if (sel & WPA_KEY_MGMT_FT_PSK) {
  833. wpa_s->key_mgmt = WPA_KEY_MGMT_FT_PSK;
  834. wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using KEY_MGMT FT/PSK");
  835. #endif /* CONFIG_IEEE80211R */
  836. #ifdef CONFIG_IEEE80211W
  837. } else if (sel & WPA_KEY_MGMT_IEEE8021X_SHA256) {
  838. wpa_s->key_mgmt = WPA_KEY_MGMT_IEEE8021X_SHA256;
  839. wpa_dbg(wpa_s, MSG_DEBUG,
  840. "WPA: using KEY_MGMT 802.1X with SHA256");
  841. } else if (sel & WPA_KEY_MGMT_PSK_SHA256) {
  842. wpa_s->key_mgmt = WPA_KEY_MGMT_PSK_SHA256;
  843. wpa_dbg(wpa_s, MSG_DEBUG,
  844. "WPA: using KEY_MGMT PSK with SHA256");
  845. #endif /* CONFIG_IEEE80211W */
  846. } else if (sel & WPA_KEY_MGMT_IEEE8021X) {
  847. wpa_s->key_mgmt = WPA_KEY_MGMT_IEEE8021X;
  848. wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using KEY_MGMT 802.1X");
  849. } else if (sel & WPA_KEY_MGMT_PSK) {
  850. wpa_s->key_mgmt = WPA_KEY_MGMT_PSK;
  851. wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using KEY_MGMT WPA-PSK");
  852. } else if (sel & WPA_KEY_MGMT_WPA_NONE) {
  853. wpa_s->key_mgmt = WPA_KEY_MGMT_WPA_NONE;
  854. wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using KEY_MGMT WPA-NONE");
  855. } else {
  856. wpa_msg(wpa_s, MSG_WARNING, "WPA: Failed to select "
  857. "authenticated key management type");
  858. return -1;
  859. }
  860. wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_KEY_MGMT, wpa_s->key_mgmt);
  861. wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_PAIRWISE,
  862. wpa_s->pairwise_cipher);
  863. wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_GROUP, wpa_s->group_cipher);
  864. #ifdef CONFIG_IEEE80211W
  865. sel = ie.mgmt_group_cipher;
  866. if (ssid->ieee80211w == NO_MGMT_FRAME_PROTECTION ||
  867. !(ie.capabilities & WPA_CAPABILITY_MFPC))
  868. sel = 0;
  869. if (sel & WPA_CIPHER_AES_128_CMAC) {
  870. wpa_s->mgmt_group_cipher = WPA_CIPHER_AES_128_CMAC;
  871. wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using MGMT group cipher "
  872. "AES-128-CMAC");
  873. } else {
  874. wpa_s->mgmt_group_cipher = 0;
  875. wpa_dbg(wpa_s, MSG_DEBUG, "WPA: not using MGMT group cipher");
  876. }
  877. wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_MGMT_GROUP,
  878. wpa_s->mgmt_group_cipher);
  879. wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_MFP, ssid->ieee80211w);
  880. #endif /* CONFIG_IEEE80211W */
  881. if (wpa_sm_set_assoc_wpa_ie_default(wpa_s->wpa, wpa_ie, wpa_ie_len)) {
  882. wpa_msg(wpa_s, MSG_WARNING, "WPA: Failed to generate WPA IE");
  883. return -1;
  884. }
  885. if (ssid->key_mgmt &
  886. (WPA_KEY_MGMT_PSK | WPA_KEY_MGMT_FT_PSK | WPA_KEY_MGMT_PSK_SHA256))
  887. wpa_sm_set_pmk(wpa_s->wpa, ssid->psk, PMK_LEN);
  888. else
  889. wpa_sm_set_pmk_from_pmksa(wpa_s->wpa);
  890. return 0;
  891. }
  892. /**
  893. * wpa_supplicant_associate - Request association
  894. * @wpa_s: Pointer to wpa_supplicant data
  895. * @bss: Scan results for the selected BSS, or %NULL if not available
  896. * @ssid: Configuration data for the selected network
  897. *
  898. * This function is used to request %wpa_supplicant to associate with a BSS.
  899. */
  900. void wpa_supplicant_associate(struct wpa_supplicant *wpa_s,
  901. struct wpa_bss *bss, struct wpa_ssid *ssid)
  902. {
  903. u8 wpa_ie[200];
  904. size_t wpa_ie_len;
  905. int use_crypt, ret, i, bssid_changed;
  906. int algs = WPA_AUTH_ALG_OPEN;
  907. enum wpa_cipher cipher_pairwise, cipher_group;
  908. struct wpa_driver_associate_params params;
  909. int wep_keys_set = 0;
  910. struct wpa_driver_capa capa;
  911. int assoc_failed = 0;
  912. struct wpa_ssid *old_ssid;
  913. if (ssid->mode == WPAS_MODE_AP || ssid->mode == WPAS_MODE_P2P_GO ||
  914. ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION) {
  915. #ifdef CONFIG_AP
  916. if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_AP)) {
  917. wpa_msg(wpa_s, MSG_INFO, "Driver does not support AP "
  918. "mode");
  919. return;
  920. }
  921. wpa_supplicant_create_ap(wpa_s, ssid);
  922. wpa_s->current_bss = bss;
  923. #else /* CONFIG_AP */
  924. wpa_msg(wpa_s, MSG_ERROR, "AP mode support not included in "
  925. "the build");
  926. #endif /* CONFIG_AP */
  927. return;
  928. }
  929. if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME) &&
  930. ssid->mode == IEEE80211_MODE_INFRA) {
  931. sme_authenticate(wpa_s, bss, ssid);
  932. return;
  933. }
  934. os_memset(&params, 0, sizeof(params));
  935. wpa_s->reassociate = 0;
  936. if (bss) {
  937. #ifdef CONFIG_IEEE80211R
  938. const u8 *ie, *md = NULL;
  939. #endif /* CONFIG_IEEE80211R */
  940. wpa_msg(wpa_s, MSG_INFO, "Trying to associate with " MACSTR
  941. " (SSID='%s' freq=%d MHz)", MAC2STR(bss->bssid),
  942. wpa_ssid_txt(bss->ssid, bss->ssid_len), bss->freq);
  943. bssid_changed = !is_zero_ether_addr(wpa_s->bssid);
  944. os_memset(wpa_s->bssid, 0, ETH_ALEN);
  945. os_memcpy(wpa_s->pending_bssid, bss->bssid, ETH_ALEN);
  946. if (bssid_changed)
  947. wpas_notify_bssid_changed(wpa_s);
  948. #ifdef CONFIG_IEEE80211R
  949. ie = wpa_bss_get_ie(bss, WLAN_EID_MOBILITY_DOMAIN);
  950. if (ie && ie[1] >= MOBILITY_DOMAIN_ID_LEN)
  951. md = ie + 2;
  952. wpa_sm_set_ft_params(wpa_s->wpa, ie, ie ? 2 + ie[1] : 0);
  953. if (md) {
  954. /* Prepare for the next transition */
  955. wpa_ft_prepare_auth_request(wpa_s->wpa, ie);
  956. }
  957. #endif /* CONFIG_IEEE80211R */
  958. #ifdef CONFIG_WPS
  959. } else if ((ssid->ssid == NULL || ssid->ssid_len == 0) &&
  960. wpa_s->conf->ap_scan == 2 &&
  961. (ssid->key_mgmt & WPA_KEY_MGMT_WPS)) {
  962. /* Use ap_scan==1 style network selection to find the network
  963. */
  964. wpa_s->scan_req = 2;
  965. wpa_s->reassociate = 1;
  966. wpa_supplicant_req_scan(wpa_s, 0, 0);
  967. return;
  968. #endif /* CONFIG_WPS */
  969. } else {
  970. wpa_msg(wpa_s, MSG_INFO, "Trying to associate with SSID '%s'",
  971. wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
  972. os_memset(wpa_s->pending_bssid, 0, ETH_ALEN);
  973. }
  974. wpa_supplicant_cancel_scan(wpa_s);
  975. /* Starting new association, so clear the possibly used WPA IE from the
  976. * previous association. */
  977. wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, NULL, 0);
  978. #ifdef IEEE8021X_EAPOL
  979. if (ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
  980. if (ssid->leap) {
  981. if (ssid->non_leap == 0)
  982. algs = WPA_AUTH_ALG_LEAP;
  983. else
  984. algs |= WPA_AUTH_ALG_LEAP;
  985. }
  986. }
  987. #endif /* IEEE8021X_EAPOL */
  988. wpa_dbg(wpa_s, MSG_DEBUG, "Automatic auth_alg selection: 0x%x", algs);
  989. if (ssid->auth_alg) {
  990. algs = ssid->auth_alg;
  991. wpa_dbg(wpa_s, MSG_DEBUG, "Overriding auth_alg selection: "
  992. "0x%x", algs);
  993. }
  994. if (bss && (wpa_bss_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE) ||
  995. wpa_bss_get_ie(bss, WLAN_EID_RSN)) &&
  996. (ssid->key_mgmt & (WPA_KEY_MGMT_IEEE8021X | WPA_KEY_MGMT_PSK |
  997. WPA_KEY_MGMT_FT_IEEE8021X |
  998. WPA_KEY_MGMT_FT_PSK |
  999. WPA_KEY_MGMT_IEEE8021X_SHA256 |
  1000. WPA_KEY_MGMT_PSK_SHA256))) {
  1001. int try_opportunistic;
  1002. try_opportunistic = ssid->proactive_key_caching &&
  1003. (ssid->proto & WPA_PROTO_RSN);
  1004. if (pmksa_cache_set_current(wpa_s->wpa, NULL, bss->bssid,
  1005. wpa_s->current_ssid,
  1006. try_opportunistic) == 0)
  1007. eapol_sm_notify_pmkid_attempt(wpa_s->eapol, 1);
  1008. wpa_ie_len = sizeof(wpa_ie);
  1009. if (wpa_supplicant_set_suites(wpa_s, bss, ssid,
  1010. wpa_ie, &wpa_ie_len)) {
  1011. wpa_msg(wpa_s, MSG_WARNING, "WPA: Failed to set WPA "
  1012. "key management and encryption suites");
  1013. return;
  1014. }
  1015. } else if (ssid->key_mgmt &
  1016. (WPA_KEY_MGMT_PSK | WPA_KEY_MGMT_IEEE8021X |
  1017. WPA_KEY_MGMT_WPA_NONE | WPA_KEY_MGMT_FT_PSK |
  1018. WPA_KEY_MGMT_FT_IEEE8021X | WPA_KEY_MGMT_PSK_SHA256 |
  1019. WPA_KEY_MGMT_IEEE8021X_SHA256)) {
  1020. wpa_ie_len = sizeof(wpa_ie);
  1021. if (wpa_supplicant_set_suites(wpa_s, NULL, ssid,
  1022. wpa_ie, &wpa_ie_len)) {
  1023. wpa_msg(wpa_s, MSG_WARNING, "WPA: Failed to set WPA "
  1024. "key management and encryption suites (no "
  1025. "scan results)");
  1026. return;
  1027. }
  1028. #ifdef CONFIG_WPS
  1029. } else if (ssid->key_mgmt & WPA_KEY_MGMT_WPS) {
  1030. struct wpabuf *wps_ie;
  1031. wps_ie = wps_build_assoc_req_ie(wpas_wps_get_req_type(ssid));
  1032. if (wps_ie && wpabuf_len(wps_ie) <= sizeof(wpa_ie)) {
  1033. wpa_ie_len = wpabuf_len(wps_ie);
  1034. os_memcpy(wpa_ie, wpabuf_head(wps_ie), wpa_ie_len);
  1035. } else
  1036. wpa_ie_len = 0;
  1037. wpabuf_free(wps_ie);
  1038. wpa_supplicant_set_non_wpa_policy(wpa_s, ssid);
  1039. if (!bss || (bss->caps & IEEE80211_CAP_PRIVACY))
  1040. params.wps = WPS_MODE_PRIVACY;
  1041. else
  1042. params.wps = WPS_MODE_OPEN;
  1043. #endif /* CONFIG_WPS */
  1044. } else {
  1045. wpa_supplicant_set_non_wpa_policy(wpa_s, ssid);
  1046. wpa_ie_len = 0;
  1047. }
  1048. #ifdef CONFIG_P2P
  1049. if (wpa_s->global->p2p) {
  1050. u8 *pos;
  1051. size_t len;
  1052. int res;
  1053. int p2p_group;
  1054. p2p_group = wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE;
  1055. pos = wpa_ie + wpa_ie_len;
  1056. len = sizeof(wpa_ie) - wpa_ie_len;
  1057. res = wpas_p2p_assoc_req_ie(wpa_s, bss, pos, len, p2p_group);
  1058. if (res >= 0)
  1059. wpa_ie_len += res;
  1060. }
  1061. wpa_s->cross_connect_disallowed = 0;
  1062. if (bss) {
  1063. struct wpabuf *p2p;
  1064. p2p = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
  1065. if (p2p) {
  1066. wpa_s->cross_connect_disallowed =
  1067. p2p_get_cross_connect_disallowed(p2p);
  1068. wpabuf_free(p2p);
  1069. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: WLAN AP %s cross "
  1070. "connection",
  1071. wpa_s->cross_connect_disallowed ?
  1072. "disallows" : "allows");
  1073. }
  1074. }
  1075. #endif /* CONFIG_P2P */
  1076. wpa_clear_keys(wpa_s, bss ? bss->bssid : NULL);
  1077. use_crypt = 1;
  1078. cipher_pairwise = cipher_suite2driver(wpa_s->pairwise_cipher);
  1079. cipher_group = cipher_suite2driver(wpa_s->group_cipher);
  1080. if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE ||
  1081. wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
  1082. if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE)
  1083. use_crypt = 0;
  1084. if (wpa_set_wep_keys(wpa_s, ssid)) {
  1085. use_crypt = 1;
  1086. wep_keys_set = 1;
  1087. }
  1088. }
  1089. if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPS)
  1090. use_crypt = 0;
  1091. #ifdef IEEE8021X_EAPOL
  1092. if (wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
  1093. if ((ssid->eapol_flags &
  1094. (EAPOL_FLAG_REQUIRE_KEY_UNICAST |
  1095. EAPOL_FLAG_REQUIRE_KEY_BROADCAST)) == 0 &&
  1096. !wep_keys_set) {
  1097. use_crypt = 0;
  1098. } else {
  1099. /* Assume that dynamic WEP-104 keys will be used and
  1100. * set cipher suites in order for drivers to expect
  1101. * encryption. */
  1102. cipher_pairwise = cipher_group = CIPHER_WEP104;
  1103. }
  1104. }
  1105. #endif /* IEEE8021X_EAPOL */
  1106. if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE) {
  1107. /* Set the key before (and later after) association */
  1108. wpa_supplicant_set_wpa_none_key(wpa_s, ssid);
  1109. }
  1110. wpa_supplicant_set_state(wpa_s, WPA_ASSOCIATING);
  1111. if (bss) {
  1112. params.bssid = bss->bssid;
  1113. params.ssid = bss->ssid;
  1114. params.ssid_len = bss->ssid_len;
  1115. params.freq = bss->freq;
  1116. } else {
  1117. params.ssid = ssid->ssid;
  1118. params.ssid_len = ssid->ssid_len;
  1119. }
  1120. if (ssid->mode == WPAS_MODE_IBSS && ssid->frequency > 0 &&
  1121. params.freq == 0)
  1122. params.freq = ssid->frequency; /* Initial channel for IBSS */
  1123. params.wpa_ie = wpa_ie;
  1124. params.wpa_ie_len = wpa_ie_len;
  1125. params.pairwise_suite = cipher_pairwise;
  1126. params.group_suite = cipher_group;
  1127. params.key_mgmt_suite = key_mgmt2driver(wpa_s->key_mgmt);
  1128. params.auth_alg = algs;
  1129. params.mode = ssid->mode;
  1130. for (i = 0; i < NUM_WEP_KEYS; i++) {
  1131. if (ssid->wep_key_len[i])
  1132. params.wep_key[i] = ssid->wep_key[i];
  1133. params.wep_key_len[i] = ssid->wep_key_len[i];
  1134. }
  1135. params.wep_tx_keyidx = ssid->wep_tx_keyidx;
  1136. if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE) &&
  1137. (params.key_mgmt_suite == KEY_MGMT_PSK ||
  1138. params.key_mgmt_suite == KEY_MGMT_FT_PSK)) {
  1139. params.passphrase = ssid->passphrase;
  1140. if (ssid->psk_set)
  1141. params.psk = ssid->psk;
  1142. }
  1143. params.drop_unencrypted = use_crypt;
  1144. #ifdef CONFIG_IEEE80211W
  1145. params.mgmt_frame_protection = ssid->ieee80211w;
  1146. if (ssid->ieee80211w != NO_MGMT_FRAME_PROTECTION && bss) {
  1147. const u8 *rsn = wpa_bss_get_ie(bss, WLAN_EID_RSN);
  1148. struct wpa_ie_data ie;
  1149. if (rsn && wpa_parse_wpa_ie(rsn, 2 + rsn[1], &ie) == 0 &&
  1150. ie.capabilities &
  1151. (WPA_CAPABILITY_MFPC | WPA_CAPABILITY_MFPR)) {
  1152. wpa_dbg(wpa_s, MSG_DEBUG, "WPA: Selected AP supports "
  1153. "MFP: require MFP");
  1154. params.mgmt_frame_protection =
  1155. MGMT_FRAME_PROTECTION_REQUIRED;
  1156. }
  1157. }
  1158. #endif /* CONFIG_IEEE80211W */
  1159. #ifdef CONFIG_P2P
  1160. if (wpa_s->global->p2p &&
  1161. (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
  1162. params.p2p = 1;
  1163. #endif /* CONFIG_P2P */
  1164. if (wpa_s->parent->set_sta_uapsd)
  1165. params.uapsd = wpa_s->parent->sta_uapsd;
  1166. else
  1167. params.uapsd = -1;
  1168. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_USER_SPACE_MLME)
  1169. ret = ieee80211_sta_associate(wpa_s, &params);
  1170. else
  1171. ret = wpa_drv_associate(wpa_s, &params);
  1172. if (ret < 0) {
  1173. wpa_msg(wpa_s, MSG_INFO, "Association request to the driver "
  1174. "failed");
  1175. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SANE_ERROR_CODES) {
  1176. /*
  1177. * The driver is known to mean what is saying, so we
  1178. * can stop right here; the association will not
  1179. * succeed.
  1180. */
  1181. wpas_connection_failed(wpa_s, wpa_s->pending_bssid);
  1182. os_memset(wpa_s->pending_bssid, 0, ETH_ALEN);
  1183. return;
  1184. }
  1185. /* try to continue anyway; new association will be tried again
  1186. * after timeout */
  1187. assoc_failed = 1;
  1188. }
  1189. if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE) {
  1190. /* Set the key after the association just in case association
  1191. * cleared the previously configured key. */
  1192. wpa_supplicant_set_wpa_none_key(wpa_s, ssid);
  1193. /* No need to timeout authentication since there is no key
  1194. * management. */
  1195. wpa_supplicant_cancel_auth_timeout(wpa_s);
  1196. wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
  1197. #ifdef CONFIG_IBSS_RSN
  1198. } else if (ssid->mode == WPAS_MODE_IBSS &&
  1199. wpa_s->key_mgmt != WPA_KEY_MGMT_NONE &&
  1200. wpa_s->key_mgmt != WPA_KEY_MGMT_WPA_NONE) {
  1201. ibss_rsn_set_psk(wpa_s->ibss_rsn, ssid->psk);
  1202. /*
  1203. * RSN IBSS authentication is per-STA and we can disable the
  1204. * per-BSSID authentication.
  1205. */
  1206. wpa_supplicant_cancel_auth_timeout(wpa_s);
  1207. #endif /* CONFIG_IBSS_RSN */
  1208. } else {
  1209. /* Timeout for IEEE 802.11 authentication and association */
  1210. int timeout = 60;
  1211. if (assoc_failed) {
  1212. /* give IBSS a bit more time */
  1213. timeout = ssid->mode == WPAS_MODE_IBSS ? 10 : 5;
  1214. } else if (wpa_s->conf->ap_scan == 1) {
  1215. /* give IBSS a bit more time */
  1216. timeout = ssid->mode == WPAS_MODE_IBSS ? 20 : 10;
  1217. }
  1218. wpa_supplicant_req_auth_timeout(wpa_s, timeout, 0);
  1219. }
  1220. if (wep_keys_set && wpa_drv_get_capa(wpa_s, &capa) == 0 &&
  1221. capa.flags & WPA_DRIVER_FLAGS_SET_KEYS_AFTER_ASSOC) {
  1222. /* Set static WEP keys again */
  1223. wpa_set_wep_keys(wpa_s, ssid);
  1224. }
  1225. if (wpa_s->current_ssid && wpa_s->current_ssid != ssid) {
  1226. /*
  1227. * Do not allow EAP session resumption between different
  1228. * network configurations.
  1229. */
  1230. eapol_sm_invalidate_cached_session(wpa_s->eapol);
  1231. }
  1232. old_ssid = wpa_s->current_ssid;
  1233. wpa_s->current_ssid = ssid;
  1234. wpa_s->current_bss = bss;
  1235. wpa_supplicant_rsn_supp_set_config(wpa_s, wpa_s->current_ssid);
  1236. wpa_supplicant_initiate_eapol(wpa_s);
  1237. if (old_ssid != wpa_s->current_ssid)
  1238. wpas_notify_network_changed(wpa_s);
  1239. }
  1240. static void wpa_supplicant_clear_connection(struct wpa_supplicant *wpa_s,
  1241. const u8 *addr)
  1242. {
  1243. struct wpa_ssid *old_ssid;
  1244. wpa_clear_keys(wpa_s, addr);
  1245. wpa_supplicant_mark_disassoc(wpa_s);
  1246. old_ssid = wpa_s->current_ssid;
  1247. wpa_s->current_ssid = NULL;
  1248. wpa_s->current_bss = NULL;
  1249. wpa_sm_set_config(wpa_s->wpa, NULL);
  1250. eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
  1251. if (old_ssid != wpa_s->current_ssid)
  1252. wpas_notify_network_changed(wpa_s);
  1253. eloop_cancel_timeout(wpa_supplicant_timeout, wpa_s, NULL);
  1254. }
  1255. /**
  1256. * wpa_supplicant_disassociate - Disassociate the current connection
  1257. * @wpa_s: Pointer to wpa_supplicant data
  1258. * @reason_code: IEEE 802.11 reason code for the disassociate frame
  1259. *
  1260. * This function is used to request %wpa_supplicant to disassociate with the
  1261. * current AP.
  1262. */
  1263. void wpa_supplicant_disassociate(struct wpa_supplicant *wpa_s,
  1264. int reason_code)
  1265. {
  1266. u8 *addr = NULL;
  1267. if (!is_zero_ether_addr(wpa_s->bssid)) {
  1268. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_USER_SPACE_MLME)
  1269. ieee80211_sta_disassociate(wpa_s, reason_code);
  1270. else
  1271. wpa_drv_disassociate(wpa_s, wpa_s->bssid, reason_code);
  1272. addr = wpa_s->bssid;
  1273. }
  1274. wpa_supplicant_clear_connection(wpa_s, addr);
  1275. }
  1276. /**
  1277. * wpa_supplicant_deauthenticate - Deauthenticate the current connection
  1278. * @wpa_s: Pointer to wpa_supplicant data
  1279. * @reason_code: IEEE 802.11 reason code for the deauthenticate frame
  1280. *
  1281. * This function is used to request %wpa_supplicant to deauthenticate from the
  1282. * current AP.
  1283. */
  1284. void wpa_supplicant_deauthenticate(struct wpa_supplicant *wpa_s,
  1285. int reason_code)
  1286. {
  1287. u8 *addr = NULL;
  1288. if (!is_zero_ether_addr(wpa_s->bssid)) {
  1289. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_USER_SPACE_MLME)
  1290. ieee80211_sta_deauthenticate(wpa_s, reason_code);
  1291. else
  1292. wpa_drv_deauthenticate(wpa_s, wpa_s->bssid,
  1293. reason_code);
  1294. addr = wpa_s->bssid;
  1295. }
  1296. wpa_supplicant_clear_connection(wpa_s, addr);
  1297. }
  1298. /**
  1299. * wpa_supplicant_enable_network - Mark a configured network as enabled
  1300. * @wpa_s: wpa_supplicant structure for a network interface
  1301. * @ssid: wpa_ssid structure for a configured network or %NULL
  1302. *
  1303. * Enables the specified network or all networks if no network specified.
  1304. */
  1305. void wpa_supplicant_enable_network(struct wpa_supplicant *wpa_s,
  1306. struct wpa_ssid *ssid)
  1307. {
  1308. struct wpa_ssid *other_ssid;
  1309. int was_disabled;
  1310. if (ssid == NULL) {
  1311. for (other_ssid = wpa_s->conf->ssid; other_ssid;
  1312. other_ssid = other_ssid->next) {
  1313. if (other_ssid->disabled == 2)
  1314. continue; /* do not change persistent P2P group
  1315. * data */
  1316. if (other_ssid == wpa_s->current_ssid &&
  1317. other_ssid->disabled)
  1318. wpa_s->reassociate = 1;
  1319. was_disabled = other_ssid->disabled;
  1320. other_ssid->disabled = 0;
  1321. if (was_disabled != other_ssid->disabled)
  1322. wpas_notify_network_enabled_changed(
  1323. wpa_s, other_ssid);
  1324. }
  1325. if (wpa_s->reassociate)
  1326. wpa_supplicant_req_scan(wpa_s, 0, 0);
  1327. } else if (ssid->disabled && ssid->disabled != 2) {
  1328. if (wpa_s->current_ssid == NULL) {
  1329. /*
  1330. * Try to reassociate since there is no current
  1331. * configuration and a new network was made available.
  1332. */
  1333. wpa_s->reassociate = 1;
  1334. wpa_supplicant_req_scan(wpa_s, 0, 0);
  1335. }
  1336. was_disabled = ssid->disabled;
  1337. ssid->disabled = 0;
  1338. if (was_disabled != ssid->disabled)
  1339. wpas_notify_network_enabled_changed(wpa_s, ssid);
  1340. }
  1341. }
  1342. /**
  1343. * wpa_supplicant_disable_network - Mark a configured network as disabled
  1344. * @wpa_s: wpa_supplicant structure for a network interface
  1345. * @ssid: wpa_ssid structure for a configured network or %NULL
  1346. *
  1347. * Disables the specified network or all networks if no network specified.
  1348. */
  1349. void wpa_supplicant_disable_network(struct wpa_supplicant *wpa_s,
  1350. struct wpa_ssid *ssid)
  1351. {
  1352. struct wpa_ssid *other_ssid;
  1353. int was_disabled;
  1354. if (ssid == NULL) {
  1355. for (other_ssid = wpa_s->conf->ssid; other_ssid;
  1356. other_ssid = other_ssid->next) {
  1357. was_disabled = other_ssid->disabled;
  1358. if (was_disabled == 2)
  1359. continue; /* do not change persistent P2P group
  1360. * data */
  1361. other_ssid->disabled = 1;
  1362. if (was_disabled != other_ssid->disabled)
  1363. wpas_notify_network_enabled_changed(
  1364. wpa_s, other_ssid);
  1365. }
  1366. if (wpa_s->current_ssid)
  1367. wpa_supplicant_disassociate(
  1368. wpa_s, WLAN_REASON_DEAUTH_LEAVING);
  1369. } else if (ssid->disabled != 2) {
  1370. if (ssid == wpa_s->current_ssid)
  1371. wpa_supplicant_disassociate(
  1372. wpa_s, WLAN_REASON_DEAUTH_LEAVING);
  1373. was_disabled = ssid->disabled;
  1374. ssid->disabled = 1;
  1375. if (was_disabled != ssid->disabled)
  1376. wpas_notify_network_enabled_changed(wpa_s, ssid);
  1377. }
  1378. }
  1379. /**
  1380. * wpa_supplicant_select_network - Attempt association with a network
  1381. * @wpa_s: wpa_supplicant structure for a network interface
  1382. * @ssid: wpa_ssid structure for a configured network or %NULL for any network
  1383. */
  1384. void wpa_supplicant_select_network(struct wpa_supplicant *wpa_s,
  1385. struct wpa_ssid *ssid)
  1386. {
  1387. struct wpa_ssid *other_ssid;
  1388. if (ssid && ssid != wpa_s->current_ssid && wpa_s->current_ssid)
  1389. wpa_supplicant_disassociate(
  1390. wpa_s, WLAN_REASON_DEAUTH_LEAVING);
  1391. /*
  1392. * Mark all other networks disabled or mark all networks enabled if no
  1393. * network specified.
  1394. */
  1395. for (other_ssid = wpa_s->conf->ssid; other_ssid;
  1396. other_ssid = other_ssid->next) {
  1397. int was_disabled = other_ssid->disabled;
  1398. if (was_disabled == 2)
  1399. continue; /* do not change persistent P2P group data */
  1400. other_ssid->disabled = ssid ? (ssid->id != other_ssid->id) : 0;
  1401. if (was_disabled != other_ssid->disabled)
  1402. wpas_notify_network_enabled_changed(wpa_s, other_ssid);
  1403. }
  1404. wpa_s->disconnected = 0;
  1405. wpa_s->reassociate = 1;
  1406. wpa_supplicant_req_scan(wpa_s, 0, 0);
  1407. if (ssid)
  1408. wpas_notify_network_selected(wpa_s, ssid);
  1409. }
  1410. /**
  1411. * wpa_supplicant_set_ap_scan - Set AP scan mode for interface
  1412. * @wpa_s: wpa_supplicant structure for a network interface
  1413. * @ap_scan: AP scan mode
  1414. * Returns: 0 if succeed or -1 if ap_scan has an invalid value
  1415. *
  1416. */
  1417. int wpa_supplicant_set_ap_scan(struct wpa_supplicant *wpa_s, int ap_scan)
  1418. {
  1419. int old_ap_scan;
  1420. if (ap_scan < 0 || ap_scan > 2)
  1421. return -1;
  1422. old_ap_scan = wpa_s->conf->ap_scan;
  1423. wpa_s->conf->ap_scan = ap_scan;
  1424. if (old_ap_scan != wpa_s->conf->ap_scan)
  1425. wpas_notify_ap_scan_changed(wpa_s);
  1426. return 0;
  1427. }
  1428. /**
  1429. * wpa_supplicant_set_debug_params - Set global debug params
  1430. * @global: wpa_global structure
  1431. * @debug_level: debug level
  1432. * @debug_timestamp: determines if show timestamp in debug data
  1433. * @debug_show_keys: determines if show keys in debug data
  1434. * Returns: 0 if succeed or -1 if debug_level has wrong value
  1435. */
  1436. int wpa_supplicant_set_debug_params(struct wpa_global *global, int debug_level,
  1437. int debug_timestamp, int debug_show_keys)
  1438. {
  1439. int old_level, old_timestamp, old_show_keys;
  1440. /* check for allowed debuglevels */
  1441. if (debug_level != MSG_MSGDUMP &&
  1442. debug_level != MSG_DEBUG &&
  1443. debug_level != MSG_INFO &&
  1444. debug_level != MSG_WARNING &&
  1445. debug_level != MSG_ERROR)
  1446. return -1;
  1447. old_level = wpa_debug_level;
  1448. old_timestamp = wpa_debug_timestamp;
  1449. old_show_keys = wpa_debug_show_keys;
  1450. wpa_debug_level = debug_level;
  1451. wpa_debug_timestamp = debug_timestamp ? 1 : 0;
  1452. wpa_debug_show_keys = debug_show_keys ? 1 : 0;
  1453. if (wpa_debug_level != old_level)
  1454. wpas_notify_debug_level_changed(global);
  1455. if (wpa_debug_timestamp != old_timestamp)
  1456. wpas_notify_debug_timestamp_changed(global);
  1457. if (wpa_debug_show_keys != old_show_keys)
  1458. wpas_notify_debug_show_keys_changed(global);
  1459. return 0;
  1460. }
  1461. /**
  1462. * wpa_supplicant_get_ssid - Get a pointer to the current network structure
  1463. * @wpa_s: Pointer to wpa_supplicant data
  1464. * Returns: A pointer to the current network structure or %NULL on failure
  1465. */
  1466. struct wpa_ssid * wpa_supplicant_get_ssid(struct wpa_supplicant *wpa_s)
  1467. {
  1468. struct wpa_ssid *entry;
  1469. u8 ssid[MAX_SSID_LEN];
  1470. int res;
  1471. size_t ssid_len;
  1472. u8 bssid[ETH_ALEN];
  1473. int wired;
  1474. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_USER_SPACE_MLME) {
  1475. if (ieee80211_sta_get_ssid(wpa_s, ssid, &ssid_len)) {
  1476. wpa_msg(wpa_s, MSG_WARNING, "Could not read SSID from "
  1477. "MLME");
  1478. return NULL;
  1479. }
  1480. } else {
  1481. res = wpa_drv_get_ssid(wpa_s, ssid);
  1482. if (res < 0) {
  1483. wpa_msg(wpa_s, MSG_WARNING, "Could not read SSID from "
  1484. "driver");
  1485. return NULL;
  1486. }
  1487. ssid_len = res;
  1488. }
  1489. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_USER_SPACE_MLME)
  1490. os_memcpy(bssid, wpa_s->bssid, ETH_ALEN);
  1491. else if (wpa_drv_get_bssid(wpa_s, bssid) < 0) {
  1492. wpa_msg(wpa_s, MSG_WARNING, "Could not read BSSID from "
  1493. "driver");
  1494. return NULL;
  1495. }
  1496. wired = wpa_s->conf->ap_scan == 0 &&
  1497. (wpa_s->drv_flags & WPA_DRIVER_FLAGS_WIRED);
  1498. entry = wpa_s->conf->ssid;
  1499. while (entry) {
  1500. if (!entry->disabled &&
  1501. ((ssid_len == entry->ssid_len &&
  1502. os_memcmp(ssid, entry->ssid, ssid_len) == 0) || wired) &&
  1503. (!entry->bssid_set ||
  1504. os_memcmp(bssid, entry->bssid, ETH_ALEN) == 0))
  1505. return entry;
  1506. #ifdef CONFIG_WPS
  1507. if (!entry->disabled &&
  1508. (entry->key_mgmt & WPA_KEY_MGMT_WPS) &&
  1509. (entry->ssid == NULL || entry->ssid_len == 0) &&
  1510. (!entry->bssid_set ||
  1511. os_memcmp(bssid, entry->bssid, ETH_ALEN) == 0))
  1512. return entry;
  1513. #endif /* CONFIG_WPS */
  1514. entry = entry->next;
  1515. }
  1516. return NULL;
  1517. }
  1518. static int wpa_supplicant_set_driver(struct wpa_supplicant *wpa_s,
  1519. const char *name)
  1520. {
  1521. int i;
  1522. size_t len;
  1523. const char *pos, *driver = name;
  1524. if (wpa_s == NULL)
  1525. return -1;
  1526. if (wpa_drivers[0] == NULL) {
  1527. wpa_msg(wpa_s, MSG_ERROR, "No driver interfaces build into "
  1528. "wpa_supplicant");
  1529. return -1;
  1530. }
  1531. if (name == NULL) {
  1532. /* default to first driver in the list */
  1533. wpa_s->driver = wpa_drivers[0];
  1534. wpa_s->global_drv_priv = wpa_s->global->drv_priv[0];
  1535. return 0;
  1536. }
  1537. do {
  1538. pos = os_strchr(driver, ',');
  1539. if (pos)
  1540. len = pos - driver;
  1541. else
  1542. len = os_strlen(driver);
  1543. for (i = 0; wpa_drivers[i]; i++) {
  1544. if (os_strlen(wpa_drivers[i]->name) == len &&
  1545. os_strncmp(driver, wpa_drivers[i]->name, len) ==
  1546. 0) {
  1547. wpa_s->driver = wpa_drivers[i];
  1548. wpa_s->global_drv_priv =
  1549. wpa_s->global->drv_priv[i];
  1550. return 0;
  1551. }
  1552. }
  1553. driver = pos + 1;
  1554. } while (pos);
  1555. wpa_msg(wpa_s, MSG_ERROR, "Unsupported driver '%s'", name);
  1556. return -1;
  1557. }
  1558. /**
  1559. * wpa_supplicant_rx_eapol - Deliver a received EAPOL frame to wpa_supplicant
  1560. * @ctx: Context pointer (wpa_s); this is the ctx variable registered
  1561. * with struct wpa_driver_ops::init()
  1562. * @src_addr: Source address of the EAPOL frame
  1563. * @buf: EAPOL data starting from the EAPOL header (i.e., no Ethernet header)
  1564. * @len: Length of the EAPOL data
  1565. *
  1566. * This function is called for each received EAPOL frame. Most driver
  1567. * interfaces rely on more generic OS mechanism for receiving frames through
  1568. * l2_packet, but if such a mechanism is not available, the driver wrapper may
  1569. * take care of received EAPOL frames and deliver them to the core supplicant
  1570. * code by calling this function.
  1571. */
  1572. void wpa_supplicant_rx_eapol(void *ctx, const u8 *src_addr,
  1573. const u8 *buf, size_t len)
  1574. {
  1575. struct wpa_supplicant *wpa_s = ctx;
  1576. wpa_dbg(wpa_s, MSG_DEBUG, "RX EAPOL from " MACSTR, MAC2STR(src_addr));
  1577. wpa_hexdump(MSG_MSGDUMP, "RX EAPOL", buf, len);
  1578. if (wpa_s->wpa_state < WPA_ASSOCIATED) {
  1579. /*
  1580. * There is possible race condition between receiving the
  1581. * association event and the EAPOL frame since they are coming
  1582. * through different paths from the driver. In order to avoid
  1583. * issues in trying to process the EAPOL frame before receiving
  1584. * association information, lets queue it for processing until
  1585. * the association event is received.
  1586. */
  1587. wpa_dbg(wpa_s, MSG_DEBUG, "Not associated - Delay processing "
  1588. "of received EAPOL frame");
  1589. wpabuf_free(wpa_s->pending_eapol_rx);
  1590. wpa_s->pending_eapol_rx = wpabuf_alloc_copy(buf, len);
  1591. if (wpa_s->pending_eapol_rx) {
  1592. os_get_time(&wpa_s->pending_eapol_rx_time);
  1593. os_memcpy(wpa_s->pending_eapol_rx_src, src_addr,
  1594. ETH_ALEN);
  1595. }
  1596. return;
  1597. }
  1598. #ifdef CONFIG_AP
  1599. if (wpa_s->ap_iface) {
  1600. wpa_supplicant_ap_rx_eapol(wpa_s, src_addr, buf, len);
  1601. return;
  1602. }
  1603. #endif /* CONFIG_AP */
  1604. if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE) {
  1605. wpa_dbg(wpa_s, MSG_DEBUG, "Ignored received EAPOL frame since "
  1606. "no key management is configured");
  1607. return;
  1608. }
  1609. if (wpa_s->eapol_received == 0 &&
  1610. (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE) ||
  1611. !wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt) ||
  1612. wpa_s->wpa_state != WPA_COMPLETED) &&
  1613. (wpa_s->current_ssid == NULL ||
  1614. wpa_s->current_ssid->mode != IEEE80211_MODE_IBSS)) {
  1615. /* Timeout for completing IEEE 802.1X and WPA authentication */
  1616. wpa_supplicant_req_auth_timeout(
  1617. wpa_s,
  1618. (wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt) ||
  1619. wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA ||
  1620. wpa_s->key_mgmt == WPA_KEY_MGMT_WPS) ?
  1621. 70 : 10, 0);
  1622. }
  1623. wpa_s->eapol_received++;
  1624. if (wpa_s->countermeasures) {
  1625. wpa_msg(wpa_s, MSG_INFO, "WPA: Countermeasures - dropped "
  1626. "EAPOL packet");
  1627. return;
  1628. }
  1629. #ifdef CONFIG_IBSS_RSN
  1630. if (wpa_s->current_ssid &&
  1631. wpa_s->current_ssid->mode == WPAS_MODE_IBSS) {
  1632. ibss_rsn_rx_eapol(wpa_s->ibss_rsn, src_addr, buf, len);
  1633. return;
  1634. }
  1635. #endif /* CONFIG_IBSS_RSN */
  1636. /* Source address of the incoming EAPOL frame could be compared to the
  1637. * current BSSID. However, it is possible that a centralized
  1638. * Authenticator could be using another MAC address than the BSSID of
  1639. * an AP, so just allow any address to be used for now. The replies are
  1640. * still sent to the current BSSID (if available), though. */
  1641. os_memcpy(wpa_s->last_eapol_src, src_addr, ETH_ALEN);
  1642. if (!wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt) &&
  1643. eapol_sm_rx_eapol(wpa_s->eapol, src_addr, buf, len) > 0)
  1644. return;
  1645. wpa_drv_poll(wpa_s);
  1646. if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE))
  1647. wpa_sm_rx_eapol(wpa_s->wpa, src_addr, buf, len);
  1648. else if (wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt)) {
  1649. /*
  1650. * Set portValid = TRUE here since we are going to skip 4-way
  1651. * handshake processing which would normally set portValid. We
  1652. * need this to allow the EAPOL state machines to be completed
  1653. * without going through EAPOL-Key handshake.
  1654. */
  1655. eapol_sm_notify_portValid(wpa_s->eapol, TRUE);
  1656. }
  1657. }
  1658. /**
  1659. * wpa_supplicant_driver_init - Initialize driver interface parameters
  1660. * @wpa_s: Pointer to wpa_supplicant data
  1661. * Returns: 0 on success, -1 on failure
  1662. *
  1663. * This function is called to initialize driver interface parameters.
  1664. * wpa_drv_init() must have been called before this function to initialize the
  1665. * driver interface.
  1666. */
  1667. int wpa_supplicant_driver_init(struct wpa_supplicant *wpa_s)
  1668. {
  1669. static int interface_count = 0;
  1670. if (wpa_s->driver->send_eapol) {
  1671. const u8 *addr = wpa_drv_get_mac_addr(wpa_s);
  1672. if (addr)
  1673. os_memcpy(wpa_s->own_addr, addr, ETH_ALEN);
  1674. } else if (!(wpa_s->drv_flags &
  1675. WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE)) {
  1676. wpa_s->l2 = l2_packet_init(wpa_s->ifname,
  1677. wpa_drv_get_mac_addr(wpa_s),
  1678. ETH_P_EAPOL,
  1679. wpa_supplicant_rx_eapol, wpa_s, 0);
  1680. if (wpa_s->l2 == NULL)
  1681. return -1;
  1682. } else {
  1683. const u8 *addr = wpa_drv_get_mac_addr(wpa_s);
  1684. if (addr)
  1685. os_memcpy(wpa_s->own_addr, addr, ETH_ALEN);
  1686. }
  1687. if (wpa_s->l2 && l2_packet_get_own_addr(wpa_s->l2, wpa_s->own_addr)) {
  1688. wpa_msg(wpa_s, MSG_ERROR, "Failed to get own L2 address");
  1689. return -1;
  1690. }
  1691. wpa_dbg(wpa_s, MSG_DEBUG, "Own MAC address: " MACSTR,
  1692. MAC2STR(wpa_s->own_addr));
  1693. if (wpa_s->bridge_ifname[0]) {
  1694. wpa_dbg(wpa_s, MSG_DEBUG, "Receiving packets from bridge "
  1695. "interface '%s'", wpa_s->bridge_ifname);
  1696. wpa_s->l2_br = l2_packet_init(wpa_s->bridge_ifname,
  1697. wpa_s->own_addr,
  1698. ETH_P_EAPOL,
  1699. wpa_supplicant_rx_eapol, wpa_s,
  1700. 0);
  1701. if (wpa_s->l2_br == NULL) {
  1702. wpa_msg(wpa_s, MSG_ERROR, "Failed to open l2_packet "
  1703. "connection for the bridge interface '%s'",
  1704. wpa_s->bridge_ifname);
  1705. return -1;
  1706. }
  1707. }
  1708. wpa_clear_keys(wpa_s, NULL);
  1709. /* Make sure that TKIP countermeasures are not left enabled (could
  1710. * happen if wpa_supplicant is killed during countermeasures. */
  1711. wpa_drv_set_countermeasures(wpa_s, 0);
  1712. wpa_dbg(wpa_s, MSG_DEBUG, "RSN: flushing PMKID list in the driver");
  1713. wpa_drv_flush_pmkid(wpa_s);
  1714. wpa_s->prev_scan_ssid = WILDCARD_SSID_SCAN;
  1715. if (wpa_supplicant_enabled_networks(wpa_s->conf)) {
  1716. wpa_supplicant_req_scan(wpa_s, interface_count, 100000);
  1717. interface_count++;
  1718. } else
  1719. wpa_supplicant_set_state(wpa_s, WPA_INACTIVE);
  1720. return 0;
  1721. }
  1722. static int wpa_supplicant_daemon(const char *pid_file)
  1723. {
  1724. wpa_printf(MSG_DEBUG, "Daemonize..");
  1725. return os_daemonize(pid_file);
  1726. }
  1727. static struct wpa_supplicant * wpa_supplicant_alloc(void)
  1728. {
  1729. struct wpa_supplicant *wpa_s;
  1730. wpa_s = os_zalloc(sizeof(*wpa_s));
  1731. if (wpa_s == NULL)
  1732. return NULL;
  1733. wpa_s->scan_req = 1;
  1734. wpa_s->new_connection = 1;
  1735. wpa_s->parent = wpa_s;
  1736. return wpa_s;
  1737. }
  1738. static int wpa_supplicant_init_iface(struct wpa_supplicant *wpa_s,
  1739. struct wpa_interface *iface)
  1740. {
  1741. const char *ifname, *driver;
  1742. struct wpa_driver_capa capa;
  1743. wpa_printf(MSG_DEBUG, "Initializing interface '%s' conf '%s' driver "
  1744. "'%s' ctrl_interface '%s' bridge '%s'", iface->ifname,
  1745. iface->confname ? iface->confname : "N/A",
  1746. iface->driver ? iface->driver : "default",
  1747. iface->ctrl_interface ? iface->ctrl_interface : "N/A",
  1748. iface->bridge_ifname ? iface->bridge_ifname : "N/A");
  1749. if (iface->confname) {
  1750. #ifdef CONFIG_BACKEND_FILE
  1751. wpa_s->confname = os_rel2abs_path(iface->confname);
  1752. if (wpa_s->confname == NULL) {
  1753. wpa_printf(MSG_ERROR, "Failed to get absolute path "
  1754. "for configuration file '%s'.",
  1755. iface->confname);
  1756. return -1;
  1757. }
  1758. wpa_printf(MSG_DEBUG, "Configuration file '%s' -> '%s'",
  1759. iface->confname, wpa_s->confname);
  1760. #else /* CONFIG_BACKEND_FILE */
  1761. wpa_s->confname = os_strdup(iface->confname);
  1762. #endif /* CONFIG_BACKEND_FILE */
  1763. wpa_s->conf = wpa_config_read(wpa_s->confname);
  1764. if (wpa_s->conf == NULL) {
  1765. wpa_printf(MSG_ERROR, "Failed to read or parse "
  1766. "configuration '%s'.", wpa_s->confname);
  1767. return -1;
  1768. }
  1769. /*
  1770. * Override ctrl_interface and driver_param if set on command
  1771. * line.
  1772. */
  1773. if (iface->ctrl_interface) {
  1774. os_free(wpa_s->conf->ctrl_interface);
  1775. wpa_s->conf->ctrl_interface =
  1776. os_strdup(iface->ctrl_interface);
  1777. }
  1778. if (iface->driver_param) {
  1779. os_free(wpa_s->conf->driver_param);
  1780. wpa_s->conf->driver_param =
  1781. os_strdup(iface->driver_param);
  1782. }
  1783. } else
  1784. wpa_s->conf = wpa_config_alloc_empty(iface->ctrl_interface,
  1785. iface->driver_param);
  1786. if (wpa_s->conf == NULL) {
  1787. wpa_printf(MSG_ERROR, "\nNo configuration found.");
  1788. return -1;
  1789. }
  1790. if (iface->ifname == NULL) {
  1791. wpa_printf(MSG_ERROR, "\nInterface name is required.");
  1792. return -1;
  1793. }
  1794. if (os_strlen(iface->ifname) >= sizeof(wpa_s->ifname)) {
  1795. wpa_printf(MSG_ERROR, "\nToo long interface name '%s'.",
  1796. iface->ifname);
  1797. return -1;
  1798. }
  1799. os_strlcpy(wpa_s->ifname, iface->ifname, sizeof(wpa_s->ifname));
  1800. if (iface->bridge_ifname) {
  1801. if (os_strlen(iface->bridge_ifname) >=
  1802. sizeof(wpa_s->bridge_ifname)) {
  1803. wpa_printf(MSG_ERROR, "\nToo long bridge interface "
  1804. "name '%s'.", iface->bridge_ifname);
  1805. return -1;
  1806. }
  1807. os_strlcpy(wpa_s->bridge_ifname, iface->bridge_ifname,
  1808. sizeof(wpa_s->bridge_ifname));
  1809. }
  1810. /* RSNA Supplicant Key Management - INITIALIZE */
  1811. eapol_sm_notify_portEnabled(wpa_s->eapol, FALSE);
  1812. eapol_sm_notify_portValid(wpa_s->eapol, FALSE);
  1813. /* Initialize driver interface and register driver event handler before
  1814. * L2 receive handler so that association events are processed before
  1815. * EAPOL-Key packets if both become available for the same select()
  1816. * call. */
  1817. driver = iface->driver;
  1818. next_driver:
  1819. if (wpa_supplicant_set_driver(wpa_s, driver) < 0)
  1820. return -1;
  1821. wpa_s->drv_priv = wpa_drv_init(wpa_s, wpa_s->ifname);
  1822. if (wpa_s->drv_priv == NULL) {
  1823. const char *pos;
  1824. pos = driver ? os_strchr(driver, ',') : NULL;
  1825. if (pos) {
  1826. wpa_dbg(wpa_s, MSG_DEBUG, "Failed to initialize "
  1827. "driver interface - try next driver wrapper");
  1828. driver = pos + 1;
  1829. goto next_driver;
  1830. }
  1831. wpa_msg(wpa_s, MSG_ERROR, "Failed to initialize driver "
  1832. "interface");
  1833. return -1;
  1834. }
  1835. if (wpa_drv_set_param(wpa_s, wpa_s->conf->driver_param) < 0) {
  1836. wpa_msg(wpa_s, MSG_ERROR, "Driver interface rejected "
  1837. "driver_param '%s'", wpa_s->conf->driver_param);
  1838. return -1;
  1839. }
  1840. ifname = wpa_drv_get_ifname(wpa_s);
  1841. if (ifname && os_strcmp(ifname, wpa_s->ifname) != 0) {
  1842. wpa_dbg(wpa_s, MSG_DEBUG, "Driver interface replaced "
  1843. "interface name with '%s'", ifname);
  1844. os_strlcpy(wpa_s->ifname, ifname, sizeof(wpa_s->ifname));
  1845. }
  1846. if (wpa_supplicant_init_wpa(wpa_s) < 0)
  1847. return -1;
  1848. wpa_sm_set_ifname(wpa_s->wpa, wpa_s->ifname,
  1849. wpa_s->bridge_ifname[0] ? wpa_s->bridge_ifname :
  1850. NULL);
  1851. wpa_sm_set_fast_reauth(wpa_s->wpa, wpa_s->conf->fast_reauth);
  1852. if (wpa_s->conf->dot11RSNAConfigPMKLifetime &&
  1853. wpa_sm_set_param(wpa_s->wpa, RSNA_PMK_LIFETIME,
  1854. wpa_s->conf->dot11RSNAConfigPMKLifetime)) {
  1855. wpa_msg(wpa_s, MSG_ERROR, "Invalid WPA parameter value for "
  1856. "dot11RSNAConfigPMKLifetime");
  1857. return -1;
  1858. }
  1859. if (wpa_s->conf->dot11RSNAConfigPMKReauthThreshold &&
  1860. wpa_sm_set_param(wpa_s->wpa, RSNA_PMK_REAUTH_THRESHOLD,
  1861. wpa_s->conf->dot11RSNAConfigPMKReauthThreshold)) {
  1862. wpa_msg(wpa_s, MSG_ERROR, "Invalid WPA parameter value for "
  1863. "dot11RSNAConfigPMKReauthThreshold");
  1864. return -1;
  1865. }
  1866. if (wpa_s->conf->dot11RSNAConfigSATimeout &&
  1867. wpa_sm_set_param(wpa_s->wpa, RSNA_SA_TIMEOUT,
  1868. wpa_s->conf->dot11RSNAConfigSATimeout)) {
  1869. wpa_msg(wpa_s, MSG_ERROR, "Invalid WPA parameter value for "
  1870. "dot11RSNAConfigSATimeout");
  1871. return -1;
  1872. }
  1873. if (wpa_drv_get_capa(wpa_s, &capa) == 0) {
  1874. wpa_s->drv_flags = capa.flags;
  1875. if (capa.flags & WPA_DRIVER_FLAGS_USER_SPACE_MLME) {
  1876. if (ieee80211_sta_init(wpa_s))
  1877. return -1;
  1878. }
  1879. wpa_s->max_scan_ssids = capa.max_scan_ssids;
  1880. wpa_s->max_remain_on_chan = capa.max_remain_on_chan;
  1881. wpa_s->max_stations = capa.max_stations;
  1882. }
  1883. if (wpa_s->max_remain_on_chan == 0)
  1884. wpa_s->max_remain_on_chan = 1000;
  1885. if (wpa_supplicant_driver_init(wpa_s) < 0)
  1886. return -1;
  1887. if (wpa_s->conf->country[0] && wpa_s->conf->country[1] &&
  1888. wpa_drv_set_country(wpa_s, wpa_s->conf->country)) {
  1889. wpa_dbg(wpa_s, MSG_DEBUG, "Failed to set country");
  1890. return -1;
  1891. }
  1892. wpa_sm_set_own_addr(wpa_s->wpa, wpa_s->own_addr);
  1893. if (wpas_wps_init(wpa_s))
  1894. return -1;
  1895. if (wpa_supplicant_init_eapol(wpa_s) < 0)
  1896. return -1;
  1897. wpa_sm_set_eapol(wpa_s->wpa, wpa_s->eapol);
  1898. wpa_s->ctrl_iface = wpa_supplicant_ctrl_iface_init(wpa_s);
  1899. if (wpa_s->ctrl_iface == NULL) {
  1900. wpa_printf(MSG_ERROR,
  1901. "Failed to initialize control interface '%s'.\n"
  1902. "You may have another wpa_supplicant process "
  1903. "already running or the file was\n"
  1904. "left by an unclean termination of wpa_supplicant "
  1905. "in which case you will need\n"
  1906. "to manually remove this file before starting "
  1907. "wpa_supplicant again.\n",
  1908. wpa_s->conf->ctrl_interface);
  1909. return -1;
  1910. }
  1911. #ifdef CONFIG_IBSS_RSN
  1912. wpa_s->ibss_rsn = ibss_rsn_init(wpa_s);
  1913. if (!wpa_s->ibss_rsn) {
  1914. wpa_dbg(wpa_s, MSG_DEBUG, "Failed to init IBSS RSN");
  1915. return -1;
  1916. }
  1917. #endif /* CONFIG_IBSS_RSN */
  1918. #ifdef CONFIG_P2P
  1919. if (wpas_p2p_init(wpa_s->global, wpa_s) < 0) {
  1920. wpa_msg(wpa_s, MSG_ERROR, "Failed to init P2P");
  1921. return -1;
  1922. }
  1923. #endif /* CONFIG_P2P */
  1924. if (wpa_bss_init(wpa_s) < 0)
  1925. return -1;
  1926. return 0;
  1927. }
  1928. static void wpa_supplicant_deinit_iface(struct wpa_supplicant *wpa_s,
  1929. int notify)
  1930. {
  1931. if (wpa_s->drv_priv) {
  1932. wpa_supplicant_deauthenticate(wpa_s,
  1933. WLAN_REASON_DEAUTH_LEAVING);
  1934. wpa_drv_set_countermeasures(wpa_s, 0);
  1935. wpa_clear_keys(wpa_s, NULL);
  1936. }
  1937. wpa_supplicant_cleanup(wpa_s);
  1938. if (notify)
  1939. wpas_notify_iface_removed(wpa_s);
  1940. if (wpa_s->drv_priv)
  1941. wpa_drv_deinit(wpa_s);
  1942. }
  1943. /**
  1944. * wpa_supplicant_add_iface - Add a new network interface
  1945. * @global: Pointer to global data from wpa_supplicant_init()
  1946. * @iface: Interface configuration options
  1947. * Returns: Pointer to the created interface or %NULL on failure
  1948. *
  1949. * This function is used to add new network interfaces for %wpa_supplicant.
  1950. * This can be called before wpa_supplicant_run() to add interfaces before the
  1951. * main event loop has been started. In addition, new interfaces can be added
  1952. * dynamically while %wpa_supplicant is already running. This could happen,
  1953. * e.g., when a hotplug network adapter is inserted.
  1954. */
  1955. struct wpa_supplicant * wpa_supplicant_add_iface(struct wpa_global *global,
  1956. struct wpa_interface *iface)
  1957. {
  1958. struct wpa_supplicant *wpa_s;
  1959. struct wpa_interface t_iface;
  1960. struct wpa_ssid *ssid;
  1961. if (global == NULL || iface == NULL)
  1962. return NULL;
  1963. wpa_s = wpa_supplicant_alloc();
  1964. if (wpa_s == NULL)
  1965. return NULL;
  1966. wpa_s->global = global;
  1967. t_iface = *iface;
  1968. if (global->params.override_driver) {
  1969. wpa_printf(MSG_DEBUG, "Override interface parameter: driver "
  1970. "('%s' -> '%s')",
  1971. iface->driver, global->params.override_driver);
  1972. t_iface.driver = global->params.override_driver;
  1973. }
  1974. if (global->params.override_ctrl_interface) {
  1975. wpa_printf(MSG_DEBUG, "Override interface parameter: "
  1976. "ctrl_interface ('%s' -> '%s')",
  1977. iface->ctrl_interface,
  1978. global->params.override_ctrl_interface);
  1979. t_iface.ctrl_interface =
  1980. global->params.override_ctrl_interface;
  1981. }
  1982. if (wpa_supplicant_init_iface(wpa_s, &t_iface)) {
  1983. wpa_printf(MSG_DEBUG, "Failed to add interface %s",
  1984. iface->ifname);
  1985. wpa_supplicant_deinit_iface(wpa_s, 0);
  1986. os_free(wpa_s);
  1987. return NULL;
  1988. }
  1989. /* Notify the control interfaces about new iface */
  1990. if (wpas_notify_iface_added(wpa_s)) {
  1991. wpa_supplicant_deinit_iface(wpa_s, 1);
  1992. os_free(wpa_s);
  1993. return NULL;
  1994. }
  1995. for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next)
  1996. wpas_notify_network_added(wpa_s, ssid);
  1997. wpa_s->next = global->ifaces;
  1998. global->ifaces = wpa_s;
  1999. wpa_dbg(wpa_s, MSG_DEBUG, "Added interface %s", wpa_s->ifname);
  2000. return wpa_s;
  2001. }
  2002. /**
  2003. * wpa_supplicant_remove_iface - Remove a network interface
  2004. * @global: Pointer to global data from wpa_supplicant_init()
  2005. * @wpa_s: Pointer to the network interface to be removed
  2006. * Returns: 0 if interface was removed, -1 if interface was not found
  2007. *
  2008. * This function can be used to dynamically remove network interfaces from
  2009. * %wpa_supplicant, e.g., when a hotplug network adapter is ejected. In
  2010. * addition, this function is used to remove all remaining interfaces when
  2011. * %wpa_supplicant is terminated.
  2012. */
  2013. int wpa_supplicant_remove_iface(struct wpa_global *global,
  2014. struct wpa_supplicant *wpa_s)
  2015. {
  2016. struct wpa_supplicant *prev;
  2017. /* Remove interface from the global list of interfaces */
  2018. prev = global->ifaces;
  2019. if (prev == wpa_s) {
  2020. global->ifaces = wpa_s->next;
  2021. } else {
  2022. while (prev && prev->next != wpa_s)
  2023. prev = prev->next;
  2024. if (prev == NULL)
  2025. return -1;
  2026. prev->next = wpa_s->next;
  2027. }
  2028. wpa_dbg(wpa_s, MSG_DEBUG, "Removing interface %s", wpa_s->ifname);
  2029. if (global->p2p_group_formation == wpa_s)
  2030. global->p2p_group_formation = NULL;
  2031. wpa_supplicant_deinit_iface(wpa_s, 1);
  2032. os_free(wpa_s);
  2033. return 0;
  2034. }
  2035. /**
  2036. * wpa_supplicant_get_iface - Get a new network interface
  2037. * @global: Pointer to global data from wpa_supplicant_init()
  2038. * @ifname: Interface name
  2039. * Returns: Pointer to the interface or %NULL if not found
  2040. */
  2041. struct wpa_supplicant * wpa_supplicant_get_iface(struct wpa_global *global,
  2042. const char *ifname)
  2043. {
  2044. struct wpa_supplicant *wpa_s;
  2045. for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
  2046. if (os_strcmp(wpa_s->ifname, ifname) == 0)
  2047. return wpa_s;
  2048. }
  2049. return NULL;
  2050. }
  2051. #ifndef CONFIG_NO_WPA_MSG
  2052. static const char * wpa_supplicant_msg_ifname_cb(void *ctx)
  2053. {
  2054. struct wpa_supplicant *wpa_s = ctx;
  2055. if (wpa_s == NULL)
  2056. return NULL;
  2057. return wpa_s->ifname;
  2058. }
  2059. #endif /* CONFIG_NO_WPA_MSG */
  2060. /**
  2061. * wpa_supplicant_init - Initialize %wpa_supplicant
  2062. * @params: Parameters for %wpa_supplicant
  2063. * Returns: Pointer to global %wpa_supplicant data, or %NULL on failure
  2064. *
  2065. * This function is used to initialize %wpa_supplicant. After successful
  2066. * initialization, the returned data pointer can be used to add and remove
  2067. * network interfaces, and eventually, to deinitialize %wpa_supplicant.
  2068. */
  2069. struct wpa_global * wpa_supplicant_init(struct wpa_params *params)
  2070. {
  2071. struct wpa_global *global;
  2072. int ret, i;
  2073. if (params == NULL)
  2074. return NULL;
  2075. #ifndef CONFIG_NO_WPA_MSG
  2076. wpa_msg_register_ifname_cb(wpa_supplicant_msg_ifname_cb);
  2077. #endif /* CONFIG_NO_WPA_MSG */
  2078. wpa_debug_open_file(params->wpa_debug_file_path);
  2079. if (params->wpa_debug_syslog)
  2080. wpa_debug_open_syslog();
  2081. ret = eap_register_methods();
  2082. if (ret) {
  2083. wpa_printf(MSG_ERROR, "Failed to register EAP methods");
  2084. if (ret == -2)
  2085. wpa_printf(MSG_ERROR, "Two or more EAP methods used "
  2086. "the same EAP type.");
  2087. return NULL;
  2088. }
  2089. global = os_zalloc(sizeof(*global));
  2090. if (global == NULL)
  2091. return NULL;
  2092. dl_list_init(&global->p2p_srv_bonjour);
  2093. dl_list_init(&global->p2p_srv_upnp);
  2094. global->params.daemonize = params->daemonize;
  2095. global->params.wait_for_monitor = params->wait_for_monitor;
  2096. global->params.dbus_ctrl_interface = params->dbus_ctrl_interface;
  2097. if (params->pid_file)
  2098. global->params.pid_file = os_strdup(params->pid_file);
  2099. if (params->ctrl_interface)
  2100. global->params.ctrl_interface =
  2101. os_strdup(params->ctrl_interface);
  2102. if (params->override_driver)
  2103. global->params.override_driver =
  2104. os_strdup(params->override_driver);
  2105. if (params->override_ctrl_interface)
  2106. global->params.override_ctrl_interface =
  2107. os_strdup(params->override_ctrl_interface);
  2108. wpa_debug_level = global->params.wpa_debug_level =
  2109. params->wpa_debug_level;
  2110. wpa_debug_show_keys = global->params.wpa_debug_show_keys =
  2111. params->wpa_debug_show_keys;
  2112. wpa_debug_timestamp = global->params.wpa_debug_timestamp =
  2113. params->wpa_debug_timestamp;
  2114. wpa_printf(MSG_DEBUG, "wpa_supplicant v" VERSION_STR);
  2115. if (eloop_init()) {
  2116. wpa_printf(MSG_ERROR, "Failed to initialize event loop");
  2117. wpa_supplicant_deinit(global);
  2118. return NULL;
  2119. }
  2120. global->ctrl_iface = wpa_supplicant_global_ctrl_iface_init(global);
  2121. if (global->ctrl_iface == NULL) {
  2122. wpa_supplicant_deinit(global);
  2123. return NULL;
  2124. }
  2125. if (wpas_notify_supplicant_initialized(global)) {
  2126. wpa_supplicant_deinit(global);
  2127. return NULL;
  2128. }
  2129. for (i = 0; wpa_drivers[i]; i++)
  2130. global->drv_count++;
  2131. if (global->drv_count == 0) {
  2132. wpa_printf(MSG_ERROR, "No drivers enabled");
  2133. wpa_supplicant_deinit(global);
  2134. return NULL;
  2135. }
  2136. global->drv_priv = os_zalloc(global->drv_count * sizeof(void *));
  2137. if (global->drv_priv == NULL) {
  2138. wpa_supplicant_deinit(global);
  2139. return NULL;
  2140. }
  2141. for (i = 0; wpa_drivers[i]; i++) {
  2142. if (!wpa_drivers[i]->global_init)
  2143. continue;
  2144. global->drv_priv[i] = wpa_drivers[i]->global_init();
  2145. if (global->drv_priv[i] == NULL) {
  2146. wpa_printf(MSG_ERROR, "Failed to initialize driver "
  2147. "'%s'", wpa_drivers[i]->name);
  2148. wpa_supplicant_deinit(global);
  2149. return NULL;
  2150. }
  2151. }
  2152. return global;
  2153. }
  2154. /**
  2155. * wpa_supplicant_run - Run the %wpa_supplicant main event loop
  2156. * @global: Pointer to global data from wpa_supplicant_init()
  2157. * Returns: 0 after successful event loop run, -1 on failure
  2158. *
  2159. * This function starts the main event loop and continues running as long as
  2160. * there are any remaining events. In most cases, this function is running as
  2161. * long as the %wpa_supplicant process in still in use.
  2162. */
  2163. int wpa_supplicant_run(struct wpa_global *global)
  2164. {
  2165. struct wpa_supplicant *wpa_s;
  2166. if (global->params.daemonize &&
  2167. wpa_supplicant_daemon(global->params.pid_file))
  2168. return -1;
  2169. if (global->params.wait_for_monitor) {
  2170. for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next)
  2171. if (wpa_s->ctrl_iface)
  2172. wpa_supplicant_ctrl_iface_wait(
  2173. wpa_s->ctrl_iface);
  2174. }
  2175. eloop_register_signal_terminate(wpa_supplicant_terminate, global);
  2176. eloop_register_signal_reconfig(wpa_supplicant_reconfig, global);
  2177. eloop_run();
  2178. return 0;
  2179. }
  2180. /**
  2181. * wpa_supplicant_deinit - Deinitialize %wpa_supplicant
  2182. * @global: Pointer to global data from wpa_supplicant_init()
  2183. *
  2184. * This function is called to deinitialize %wpa_supplicant and to free all
  2185. * allocated resources. Remaining network interfaces will also be removed.
  2186. */
  2187. void wpa_supplicant_deinit(struct wpa_global *global)
  2188. {
  2189. int i;
  2190. if (global == NULL)
  2191. return;
  2192. #ifdef CONFIG_P2P
  2193. wpas_p2p_deinit_global(global);
  2194. #endif /* CONFIG_P2P */
  2195. while (global->ifaces)
  2196. wpa_supplicant_remove_iface(global, global->ifaces);
  2197. if (global->ctrl_iface)
  2198. wpa_supplicant_global_ctrl_iface_deinit(global->ctrl_iface);
  2199. wpas_notify_supplicant_deinitialized(global);
  2200. eap_peer_unregister_methods();
  2201. #ifdef CONFIG_AP
  2202. eap_server_unregister_methods();
  2203. #endif /* CONFIG_AP */
  2204. for (i = 0; wpa_drivers[i] && global->drv_priv; i++) {
  2205. if (!global->drv_priv[i])
  2206. continue;
  2207. wpa_drivers[i]->global_deinit(global->drv_priv[i]);
  2208. }
  2209. os_free(global->drv_priv);
  2210. eloop_destroy();
  2211. if (global->params.pid_file) {
  2212. os_daemonize_terminate(global->params.pid_file);
  2213. os_free(global->params.pid_file);
  2214. }
  2215. os_free(global->params.ctrl_interface);
  2216. os_free(global->params.override_driver);
  2217. os_free(global->params.override_ctrl_interface);
  2218. os_free(global);
  2219. wpa_debug_close_syslog();
  2220. wpa_debug_close_file();
  2221. }
  2222. void wpa_supplicant_update_config(struct wpa_supplicant *wpa_s)
  2223. {
  2224. #ifdef CONFIG_WPS
  2225. wpas_wps_update_config(wpa_s);
  2226. #endif /* CONFIG_WPS */
  2227. #ifdef CONFIG_P2P
  2228. wpas_p2p_update_config(wpa_s);
  2229. #endif /* CONFIG_P2P */
  2230. wpa_s->conf->changed_parameters = 0;
  2231. }
  2232. void ieee80211_sta_free_hw_features(struct hostapd_hw_modes *hw_features,
  2233. size_t num_hw_features)
  2234. {
  2235. size_t i;
  2236. if (hw_features == NULL)
  2237. return;
  2238. for (i = 0; i < num_hw_features; i++) {
  2239. os_free(hw_features[i].channels);
  2240. os_free(hw_features[i].rates);
  2241. }
  2242. os_free(hw_features);
  2243. }
  2244. static void add_freq(int *freqs, int *num_freqs, int freq)
  2245. {
  2246. int i;
  2247. for (i = 0; i < *num_freqs; i++) {
  2248. if (freqs[i] == freq)
  2249. return;
  2250. }
  2251. freqs[*num_freqs] = freq;
  2252. (*num_freqs)++;
  2253. }
  2254. static int * get_bss_freqs_in_ess(struct wpa_supplicant *wpa_s)
  2255. {
  2256. struct wpa_bss *bss, *cbss;
  2257. const int max_freqs = 10;
  2258. int *freqs;
  2259. int num_freqs = 0;
  2260. freqs = os_zalloc(sizeof(int) * (max_freqs + 1));
  2261. if (freqs == NULL)
  2262. return NULL;
  2263. cbss = wpa_s->current_bss;
  2264. dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
  2265. if (bss == cbss)
  2266. continue;
  2267. if (bss->ssid_len == cbss->ssid_len &&
  2268. os_memcmp(bss->ssid, cbss->ssid, bss->ssid_len) == 0 &&
  2269. wpa_blacklist_get(wpa_s, bss->bssid) == NULL) {
  2270. add_freq(freqs, &num_freqs, bss->freq);
  2271. if (num_freqs == max_freqs)
  2272. break;
  2273. }
  2274. }
  2275. if (num_freqs == 0) {
  2276. os_free(freqs);
  2277. freqs = NULL;
  2278. }
  2279. return freqs;
  2280. }
  2281. void wpas_connection_failed(struct wpa_supplicant *wpa_s, const u8 *bssid)
  2282. {
  2283. int timeout;
  2284. int count;
  2285. int *freqs = NULL;
  2286. /*
  2287. * Add the failed BSSID into the blacklist and speed up next scan
  2288. * attempt if there could be other APs that could accept association.
  2289. * The current blacklist count indicates how many times we have tried
  2290. * connecting to this AP and multiple attempts mean that other APs are
  2291. * either not available or has already been tried, so that we can start
  2292. * increasing the delay here to avoid constant scanning.
  2293. */
  2294. count = wpa_blacklist_add(wpa_s, bssid);
  2295. if (count == 1 && wpa_s->current_bss) {
  2296. /*
  2297. * This BSS was not in the blacklist before. If there is
  2298. * another BSS available for the same ESS, we should try that
  2299. * next. Otherwise, we may as well try this one once more
  2300. * before allowing other, likely worse, ESSes to be considered.
  2301. */
  2302. freqs = get_bss_freqs_in_ess(wpa_s);
  2303. if (freqs) {
  2304. wpa_dbg(wpa_s, MSG_DEBUG, "Another BSS in this ESS "
  2305. "has been seen; try it next");
  2306. wpa_blacklist_add(wpa_s, bssid);
  2307. /*
  2308. * On the next scan, go through only the known channels
  2309. * used in this ESS based on previous scans to speed up
  2310. * common load balancing use case.
  2311. */
  2312. os_free(wpa_s->next_scan_freqs);
  2313. wpa_s->next_scan_freqs = freqs;
  2314. }
  2315. }
  2316. switch (count) {
  2317. case 1:
  2318. timeout = 100;
  2319. break;
  2320. case 2:
  2321. timeout = 500;
  2322. break;
  2323. case 3:
  2324. timeout = 1000;
  2325. break;
  2326. default:
  2327. timeout = 5000;
  2328. }
  2329. /*
  2330. * TODO: if more than one possible AP is available in scan results,
  2331. * could try the other ones before requesting a new scan.
  2332. */
  2333. wpa_supplicant_req_scan(wpa_s, timeout / 1000,
  2334. 1000 * (timeout % 1000));
  2335. }