wpa_supplicant.c 97 KB

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