wpa_supplicant.c 128 KB

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