wpa_supplicant.c 95 KB

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