wpa_supplicant.c 113 KB

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