mlme.c 123 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387
  1. /*
  2. * BSS client mode implementation
  3. * Copyright 2003-2008, Jouni Malinen <j@w1.fi>
  4. * Copyright 2004, Instant802 Networks, Inc.
  5. * Copyright 2005, Devicescape Software, Inc.
  6. * Copyright 2006-2007 Jiri Benc <jbenc@suse.cz>
  7. * Copyright 2007, Michael Wu <flamingice@sourmilk.net>
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2 as
  11. * published by the Free Software Foundation.
  12. */
  13. #include <linux/delay.h>
  14. #include <linux/if_ether.h>
  15. #include <linux/skbuff.h>
  16. #include <linux/if_arp.h>
  17. #include <linux/etherdevice.h>
  18. #include <linux/moduleparam.h>
  19. #include <linux/rtnetlink.h>
  20. #include <linux/pm_qos.h>
  21. #include <linux/crc32.h>
  22. #include <linux/slab.h>
  23. #include <linux/export.h>
  24. #include <net/mac80211.h>
  25. #include <asm/unaligned.h>
  26. #include "ieee80211_i.h"
  27. #include "driver-ops.h"
  28. #include "rate.h"
  29. #include "led.h"
  30. #define IEEE80211_AUTH_TIMEOUT (HZ / 5)
  31. #define IEEE80211_AUTH_TIMEOUT_LONG (HZ / 2)
  32. #define IEEE80211_AUTH_TIMEOUT_SHORT (HZ / 10)
  33. #define IEEE80211_AUTH_MAX_TRIES 3
  34. #define IEEE80211_AUTH_WAIT_ASSOC (HZ * 5)
  35. #define IEEE80211_ASSOC_TIMEOUT (HZ / 5)
  36. #define IEEE80211_ASSOC_TIMEOUT_LONG (HZ / 2)
  37. #define IEEE80211_ASSOC_TIMEOUT_SHORT (HZ / 10)
  38. #define IEEE80211_ASSOC_MAX_TRIES 3
  39. static int max_nullfunc_tries = 2;
  40. module_param(max_nullfunc_tries, int, 0644);
  41. MODULE_PARM_DESC(max_nullfunc_tries,
  42. "Maximum nullfunc tx tries before disconnecting (reason 4).");
  43. static int max_probe_tries = 5;
  44. module_param(max_probe_tries, int, 0644);
  45. MODULE_PARM_DESC(max_probe_tries,
  46. "Maximum probe tries before disconnecting (reason 4).");
  47. /*
  48. * Beacon loss timeout is calculated as N frames times the
  49. * advertised beacon interval. This may need to be somewhat
  50. * higher than what hardware might detect to account for
  51. * delays in the host processing frames. But since we also
  52. * probe on beacon miss before declaring the connection lost
  53. * default to what we want.
  54. */
  55. static int beacon_loss_count = 7;
  56. module_param(beacon_loss_count, int, 0644);
  57. MODULE_PARM_DESC(beacon_loss_count,
  58. "Number of beacon intervals before we decide beacon was lost.");
  59. /*
  60. * Time the connection can be idle before we probe
  61. * it to see if we can still talk to the AP.
  62. */
  63. #define IEEE80211_CONNECTION_IDLE_TIME (30 * HZ)
  64. /*
  65. * Time we wait for a probe response after sending
  66. * a probe request because of beacon loss or for
  67. * checking the connection still works.
  68. */
  69. static int probe_wait_ms = 500;
  70. module_param(probe_wait_ms, int, 0644);
  71. MODULE_PARM_DESC(probe_wait_ms,
  72. "Maximum time(ms) to wait for probe response"
  73. " before disconnecting (reason 4).");
  74. /*
  75. * Weight given to the latest Beacon frame when calculating average signal
  76. * strength for Beacon frames received in the current BSS. This must be
  77. * between 1 and 15.
  78. */
  79. #define IEEE80211_SIGNAL_AVE_WEIGHT 3
  80. /*
  81. * How many Beacon frames need to have been used in average signal strength
  82. * before starting to indicate signal change events.
  83. */
  84. #define IEEE80211_SIGNAL_AVE_MIN_COUNT 4
  85. /*
  86. * We can have multiple work items (and connection probing)
  87. * scheduling this timer, but we need to take care to only
  88. * reschedule it when it should fire _earlier_ than it was
  89. * asked for before, or if it's not pending right now. This
  90. * function ensures that. Note that it then is required to
  91. * run this function for all timeouts after the first one
  92. * has happened -- the work that runs from this timer will
  93. * do that.
  94. */
  95. static void run_again(struct ieee80211_sub_if_data *sdata,
  96. unsigned long timeout)
  97. {
  98. sdata_assert_lock(sdata);
  99. if (!timer_pending(&sdata->u.mgd.timer) ||
  100. time_before(timeout, sdata->u.mgd.timer.expires))
  101. mod_timer(&sdata->u.mgd.timer, timeout);
  102. }
  103. void ieee80211_sta_reset_beacon_monitor(struct ieee80211_sub_if_data *sdata)
  104. {
  105. if (sdata->vif.driver_flags & IEEE80211_VIF_BEACON_FILTER)
  106. return;
  107. if (sdata->local->hw.flags & IEEE80211_HW_CONNECTION_MONITOR)
  108. return;
  109. mod_timer(&sdata->u.mgd.bcn_mon_timer,
  110. round_jiffies_up(jiffies + sdata->u.mgd.beacon_timeout));
  111. }
  112. void ieee80211_sta_reset_conn_monitor(struct ieee80211_sub_if_data *sdata)
  113. {
  114. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  115. if (unlikely(!sdata->u.mgd.associated))
  116. return;
  117. if (sdata->local->hw.flags & IEEE80211_HW_CONNECTION_MONITOR)
  118. return;
  119. mod_timer(&sdata->u.mgd.conn_mon_timer,
  120. round_jiffies_up(jiffies + IEEE80211_CONNECTION_IDLE_TIME));
  121. ifmgd->probe_send_count = 0;
  122. }
  123. static int ecw2cw(int ecw)
  124. {
  125. return (1 << ecw) - 1;
  126. }
  127. static u32
  128. ieee80211_determine_chantype(struct ieee80211_sub_if_data *sdata,
  129. struct ieee80211_supported_band *sband,
  130. struct ieee80211_channel *channel,
  131. const struct ieee80211_ht_operation *ht_oper,
  132. const struct ieee80211_vht_operation *vht_oper,
  133. struct cfg80211_chan_def *chandef, bool tracking)
  134. {
  135. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  136. struct cfg80211_chan_def vht_chandef;
  137. u32 ht_cfreq, ret;
  138. chandef->chan = channel;
  139. chandef->width = NL80211_CHAN_WIDTH_20_NOHT;
  140. chandef->center_freq1 = channel->center_freq;
  141. chandef->center_freq2 = 0;
  142. if (!ht_oper || !sband->ht_cap.ht_supported) {
  143. ret = IEEE80211_STA_DISABLE_HT | IEEE80211_STA_DISABLE_VHT;
  144. goto out;
  145. }
  146. chandef->width = NL80211_CHAN_WIDTH_20;
  147. ht_cfreq = ieee80211_channel_to_frequency(ht_oper->primary_chan,
  148. channel->band);
  149. /* check that channel matches the right operating channel */
  150. if (!tracking && channel->center_freq != ht_cfreq) {
  151. /*
  152. * It's possible that some APs are confused here;
  153. * Netgear WNDR3700 sometimes reports 4 higher than
  154. * the actual channel in association responses, but
  155. * since we look at probe response/beacon data here
  156. * it should be OK.
  157. */
  158. sdata_info(sdata,
  159. "Wrong control channel: center-freq: %d ht-cfreq: %d ht->primary_chan: %d band: %d - Disabling HT\n",
  160. channel->center_freq, ht_cfreq,
  161. ht_oper->primary_chan, channel->band);
  162. ret = IEEE80211_STA_DISABLE_HT | IEEE80211_STA_DISABLE_VHT;
  163. goto out;
  164. }
  165. /* check 40 MHz support, if we have it */
  166. if (sband->ht_cap.cap & IEEE80211_HT_CAP_SUP_WIDTH_20_40) {
  167. switch (ht_oper->ht_param & IEEE80211_HT_PARAM_CHA_SEC_OFFSET) {
  168. case IEEE80211_HT_PARAM_CHA_SEC_ABOVE:
  169. chandef->width = NL80211_CHAN_WIDTH_40;
  170. chandef->center_freq1 += 10;
  171. break;
  172. case IEEE80211_HT_PARAM_CHA_SEC_BELOW:
  173. chandef->width = NL80211_CHAN_WIDTH_40;
  174. chandef->center_freq1 -= 10;
  175. break;
  176. }
  177. } else {
  178. /* 40 MHz (and 80 MHz) must be supported for VHT */
  179. ret = IEEE80211_STA_DISABLE_VHT;
  180. /* also mark 40 MHz disabled */
  181. ret |= IEEE80211_STA_DISABLE_40MHZ;
  182. goto out;
  183. }
  184. if (!vht_oper || !sband->vht_cap.vht_supported) {
  185. ret = IEEE80211_STA_DISABLE_VHT;
  186. goto out;
  187. }
  188. vht_chandef.chan = channel;
  189. vht_chandef.center_freq1 =
  190. ieee80211_channel_to_frequency(vht_oper->center_freq_seg1_idx,
  191. channel->band);
  192. vht_chandef.center_freq2 = 0;
  193. switch (vht_oper->chan_width) {
  194. case IEEE80211_VHT_CHANWIDTH_USE_HT:
  195. vht_chandef.width = chandef->width;
  196. break;
  197. case IEEE80211_VHT_CHANWIDTH_80MHZ:
  198. vht_chandef.width = NL80211_CHAN_WIDTH_80;
  199. break;
  200. case IEEE80211_VHT_CHANWIDTH_160MHZ:
  201. vht_chandef.width = NL80211_CHAN_WIDTH_160;
  202. break;
  203. case IEEE80211_VHT_CHANWIDTH_80P80MHZ:
  204. vht_chandef.width = NL80211_CHAN_WIDTH_80P80;
  205. vht_chandef.center_freq2 =
  206. ieee80211_channel_to_frequency(
  207. vht_oper->center_freq_seg2_idx,
  208. channel->band);
  209. break;
  210. default:
  211. if (!(ifmgd->flags & IEEE80211_STA_DISABLE_VHT))
  212. sdata_info(sdata,
  213. "AP VHT operation IE has invalid channel width (%d), disable VHT\n",
  214. vht_oper->chan_width);
  215. ret = IEEE80211_STA_DISABLE_VHT;
  216. goto out;
  217. }
  218. if (!cfg80211_chandef_valid(&vht_chandef)) {
  219. if (!(ifmgd->flags & IEEE80211_STA_DISABLE_VHT))
  220. sdata_info(sdata,
  221. "AP VHT information is invalid, disable VHT\n");
  222. ret = IEEE80211_STA_DISABLE_VHT;
  223. goto out;
  224. }
  225. if (cfg80211_chandef_identical(chandef, &vht_chandef)) {
  226. ret = 0;
  227. goto out;
  228. }
  229. if (!cfg80211_chandef_compatible(chandef, &vht_chandef)) {
  230. if (!(ifmgd->flags & IEEE80211_STA_DISABLE_VHT))
  231. sdata_info(sdata,
  232. "AP VHT information doesn't match HT, disable VHT\n");
  233. ret = IEEE80211_STA_DISABLE_VHT;
  234. goto out;
  235. }
  236. *chandef = vht_chandef;
  237. ret = 0;
  238. out:
  239. /* don't print the message below for VHT mismatch if VHT is disabled */
  240. if (ret & IEEE80211_STA_DISABLE_VHT)
  241. vht_chandef = *chandef;
  242. /*
  243. * Ignore the DISABLED flag when we're already connected and only
  244. * tracking the APs beacon for bandwidth changes - otherwise we
  245. * might get disconnected here if we connect to an AP, update our
  246. * regulatory information based on the AP's country IE and the
  247. * information we have is wrong/outdated and disables the channel
  248. * that we're actually using for the connection to the AP.
  249. */
  250. while (!cfg80211_chandef_usable(sdata->local->hw.wiphy, chandef,
  251. tracking ? 0 :
  252. IEEE80211_CHAN_DISABLED)) {
  253. if (WARN_ON(chandef->width == NL80211_CHAN_WIDTH_20_NOHT)) {
  254. ret = IEEE80211_STA_DISABLE_HT |
  255. IEEE80211_STA_DISABLE_VHT;
  256. break;
  257. }
  258. ret |= ieee80211_chandef_downgrade(chandef);
  259. }
  260. if (chandef->width != vht_chandef.width && !tracking)
  261. sdata_info(sdata,
  262. "capabilities/regulatory prevented using AP HT/VHT configuration, downgraded\n");
  263. WARN_ON_ONCE(!cfg80211_chandef_valid(chandef));
  264. return ret;
  265. }
  266. static int ieee80211_config_bw(struct ieee80211_sub_if_data *sdata,
  267. struct sta_info *sta,
  268. const struct ieee80211_ht_operation *ht_oper,
  269. const struct ieee80211_vht_operation *vht_oper,
  270. const u8 *bssid, u32 *changed)
  271. {
  272. struct ieee80211_local *local = sdata->local;
  273. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  274. struct ieee80211_supported_band *sband;
  275. struct ieee80211_channel *chan;
  276. struct cfg80211_chan_def chandef;
  277. u16 ht_opmode;
  278. u32 flags;
  279. enum ieee80211_sta_rx_bandwidth new_sta_bw;
  280. int ret;
  281. /* if HT was/is disabled, don't track any bandwidth changes */
  282. if (ifmgd->flags & IEEE80211_STA_DISABLE_HT || !ht_oper)
  283. return 0;
  284. /* don't check VHT if we associated as non-VHT station */
  285. if (ifmgd->flags & IEEE80211_STA_DISABLE_VHT)
  286. vht_oper = NULL;
  287. if (WARN_ON_ONCE(!sta))
  288. return -EINVAL;
  289. /*
  290. * if bss configuration changed store the new one -
  291. * this may be applicable even if channel is identical
  292. */
  293. ht_opmode = le16_to_cpu(ht_oper->operation_mode);
  294. if (sdata->vif.bss_conf.ht_operation_mode != ht_opmode) {
  295. *changed |= BSS_CHANGED_HT;
  296. sdata->vif.bss_conf.ht_operation_mode = ht_opmode;
  297. }
  298. chan = sdata->vif.bss_conf.chandef.chan;
  299. sband = local->hw.wiphy->bands[chan->band];
  300. /* calculate new channel (type) based on HT/VHT operation IEs */
  301. flags = ieee80211_determine_chantype(sdata, sband, chan, ht_oper,
  302. vht_oper, &chandef, true);
  303. /*
  304. * Downgrade the new channel if we associated with restricted
  305. * capabilities. For example, if we associated as a 20 MHz STA
  306. * to a 40 MHz AP (due to regulatory, capabilities or config
  307. * reasons) then switching to a 40 MHz channel now won't do us
  308. * any good -- we couldn't use it with the AP.
  309. */
  310. if (ifmgd->flags & IEEE80211_STA_DISABLE_80P80MHZ &&
  311. chandef.width == NL80211_CHAN_WIDTH_80P80)
  312. flags |= ieee80211_chandef_downgrade(&chandef);
  313. if (ifmgd->flags & IEEE80211_STA_DISABLE_160MHZ &&
  314. chandef.width == NL80211_CHAN_WIDTH_160)
  315. flags |= ieee80211_chandef_downgrade(&chandef);
  316. if (ifmgd->flags & IEEE80211_STA_DISABLE_40MHZ &&
  317. chandef.width > NL80211_CHAN_WIDTH_20)
  318. flags |= ieee80211_chandef_downgrade(&chandef);
  319. if (cfg80211_chandef_identical(&chandef, &sdata->vif.bss_conf.chandef))
  320. return 0;
  321. sdata_info(sdata,
  322. "AP %pM changed bandwidth, new config is %d MHz, width %d (%d/%d MHz)\n",
  323. ifmgd->bssid, chandef.chan->center_freq, chandef.width,
  324. chandef.center_freq1, chandef.center_freq2);
  325. if (flags != (ifmgd->flags & (IEEE80211_STA_DISABLE_HT |
  326. IEEE80211_STA_DISABLE_VHT |
  327. IEEE80211_STA_DISABLE_40MHZ |
  328. IEEE80211_STA_DISABLE_80P80MHZ |
  329. IEEE80211_STA_DISABLE_160MHZ)) ||
  330. !cfg80211_chandef_valid(&chandef)) {
  331. sdata_info(sdata,
  332. "AP %pM changed bandwidth in a way we can't support - disconnect\n",
  333. ifmgd->bssid);
  334. return -EINVAL;
  335. }
  336. switch (chandef.width) {
  337. case NL80211_CHAN_WIDTH_20_NOHT:
  338. case NL80211_CHAN_WIDTH_20:
  339. new_sta_bw = IEEE80211_STA_RX_BW_20;
  340. break;
  341. case NL80211_CHAN_WIDTH_40:
  342. new_sta_bw = IEEE80211_STA_RX_BW_40;
  343. break;
  344. case NL80211_CHAN_WIDTH_80:
  345. new_sta_bw = IEEE80211_STA_RX_BW_80;
  346. break;
  347. case NL80211_CHAN_WIDTH_80P80:
  348. case NL80211_CHAN_WIDTH_160:
  349. new_sta_bw = IEEE80211_STA_RX_BW_160;
  350. break;
  351. default:
  352. return -EINVAL;
  353. }
  354. if (new_sta_bw > sta->cur_max_bandwidth)
  355. new_sta_bw = sta->cur_max_bandwidth;
  356. if (new_sta_bw < sta->sta.bandwidth) {
  357. sta->sta.bandwidth = new_sta_bw;
  358. rate_control_rate_update(local, sband, sta,
  359. IEEE80211_RC_BW_CHANGED);
  360. }
  361. ret = ieee80211_vif_change_bandwidth(sdata, &chandef, changed);
  362. if (ret) {
  363. sdata_info(sdata,
  364. "AP %pM changed bandwidth to incompatible one - disconnect\n",
  365. ifmgd->bssid);
  366. return ret;
  367. }
  368. if (new_sta_bw > sta->sta.bandwidth) {
  369. sta->sta.bandwidth = new_sta_bw;
  370. rate_control_rate_update(local, sband, sta,
  371. IEEE80211_RC_BW_CHANGED);
  372. }
  373. return 0;
  374. }
  375. /* frame sending functions */
  376. static void ieee80211_add_ht_ie(struct ieee80211_sub_if_data *sdata,
  377. struct sk_buff *skb, u8 ap_ht_param,
  378. struct ieee80211_supported_band *sband,
  379. struct ieee80211_channel *channel,
  380. enum ieee80211_smps_mode smps)
  381. {
  382. u8 *pos;
  383. u32 flags = channel->flags;
  384. u16 cap;
  385. struct ieee80211_sta_ht_cap ht_cap;
  386. BUILD_BUG_ON(sizeof(ht_cap) != sizeof(sband->ht_cap));
  387. memcpy(&ht_cap, &sband->ht_cap, sizeof(ht_cap));
  388. ieee80211_apply_htcap_overrides(sdata, &ht_cap);
  389. /* determine capability flags */
  390. cap = ht_cap.cap;
  391. switch (ap_ht_param & IEEE80211_HT_PARAM_CHA_SEC_OFFSET) {
  392. case IEEE80211_HT_PARAM_CHA_SEC_ABOVE:
  393. if (flags & IEEE80211_CHAN_NO_HT40PLUS) {
  394. cap &= ~IEEE80211_HT_CAP_SUP_WIDTH_20_40;
  395. cap &= ~IEEE80211_HT_CAP_SGI_40;
  396. }
  397. break;
  398. case IEEE80211_HT_PARAM_CHA_SEC_BELOW:
  399. if (flags & IEEE80211_CHAN_NO_HT40MINUS) {
  400. cap &= ~IEEE80211_HT_CAP_SUP_WIDTH_20_40;
  401. cap &= ~IEEE80211_HT_CAP_SGI_40;
  402. }
  403. break;
  404. }
  405. /*
  406. * If 40 MHz was disabled associate as though we weren't
  407. * capable of 40 MHz -- some broken APs will never fall
  408. * back to trying to transmit in 20 MHz.
  409. */
  410. if (sdata->u.mgd.flags & IEEE80211_STA_DISABLE_40MHZ) {
  411. cap &= ~IEEE80211_HT_CAP_SUP_WIDTH_20_40;
  412. cap &= ~IEEE80211_HT_CAP_SGI_40;
  413. }
  414. /* set SM PS mode properly */
  415. cap &= ~IEEE80211_HT_CAP_SM_PS;
  416. switch (smps) {
  417. case IEEE80211_SMPS_AUTOMATIC:
  418. case IEEE80211_SMPS_NUM_MODES:
  419. WARN_ON(1);
  420. case IEEE80211_SMPS_OFF:
  421. cap |= WLAN_HT_CAP_SM_PS_DISABLED <<
  422. IEEE80211_HT_CAP_SM_PS_SHIFT;
  423. break;
  424. case IEEE80211_SMPS_STATIC:
  425. cap |= WLAN_HT_CAP_SM_PS_STATIC <<
  426. IEEE80211_HT_CAP_SM_PS_SHIFT;
  427. break;
  428. case IEEE80211_SMPS_DYNAMIC:
  429. cap |= WLAN_HT_CAP_SM_PS_DYNAMIC <<
  430. IEEE80211_HT_CAP_SM_PS_SHIFT;
  431. break;
  432. }
  433. /* reserve and fill IE */
  434. pos = skb_put(skb, sizeof(struct ieee80211_ht_cap) + 2);
  435. ieee80211_ie_build_ht_cap(pos, &ht_cap, cap);
  436. }
  437. static void ieee80211_add_vht_ie(struct ieee80211_sub_if_data *sdata,
  438. struct sk_buff *skb,
  439. struct ieee80211_supported_band *sband,
  440. struct ieee80211_vht_cap *ap_vht_cap)
  441. {
  442. u8 *pos;
  443. u32 cap;
  444. struct ieee80211_sta_vht_cap vht_cap;
  445. BUILD_BUG_ON(sizeof(vht_cap) != sizeof(sband->vht_cap));
  446. memcpy(&vht_cap, &sband->vht_cap, sizeof(vht_cap));
  447. ieee80211_apply_vhtcap_overrides(sdata, &vht_cap);
  448. /* determine capability flags */
  449. cap = vht_cap.cap;
  450. if (sdata->u.mgd.flags & IEEE80211_STA_DISABLE_80P80MHZ) {
  451. cap &= ~IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160_80PLUS80MHZ;
  452. cap |= IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160MHZ;
  453. }
  454. if (sdata->u.mgd.flags & IEEE80211_STA_DISABLE_160MHZ) {
  455. cap &= ~IEEE80211_VHT_CAP_SHORT_GI_160;
  456. cap &= ~IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160MHZ;
  457. }
  458. /*
  459. * Some APs apparently get confused if our capabilities are better
  460. * than theirs, so restrict what we advertise in the assoc request.
  461. */
  462. if (!(ap_vht_cap->vht_cap_info &
  463. cpu_to_le32(IEEE80211_VHT_CAP_SU_BEAMFORMER_CAPABLE)))
  464. cap &= ~IEEE80211_VHT_CAP_SU_BEAMFORMEE_CAPABLE;
  465. /* reserve and fill IE */
  466. pos = skb_put(skb, sizeof(struct ieee80211_vht_cap) + 2);
  467. ieee80211_ie_build_vht_cap(pos, &vht_cap, cap);
  468. }
  469. static void ieee80211_send_assoc(struct ieee80211_sub_if_data *sdata)
  470. {
  471. struct ieee80211_local *local = sdata->local;
  472. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  473. struct ieee80211_mgd_assoc_data *assoc_data = ifmgd->assoc_data;
  474. struct sk_buff *skb;
  475. struct ieee80211_mgmt *mgmt;
  476. u8 *pos, qos_info;
  477. size_t offset = 0, noffset;
  478. int i, count, rates_len, supp_rates_len, shift;
  479. u16 capab;
  480. struct ieee80211_supported_band *sband;
  481. struct ieee80211_chanctx_conf *chanctx_conf;
  482. struct ieee80211_channel *chan;
  483. u32 rate_flags, rates = 0;
  484. sdata_assert_lock(sdata);
  485. rcu_read_lock();
  486. chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
  487. if (WARN_ON(!chanctx_conf)) {
  488. rcu_read_unlock();
  489. return;
  490. }
  491. chan = chanctx_conf->def.chan;
  492. rate_flags = ieee80211_chandef_rate_flags(&chanctx_conf->def);
  493. rcu_read_unlock();
  494. sband = local->hw.wiphy->bands[chan->band];
  495. shift = ieee80211_vif_get_shift(&sdata->vif);
  496. if (assoc_data->supp_rates_len) {
  497. /*
  498. * Get all rates supported by the device and the AP as
  499. * some APs don't like getting a superset of their rates
  500. * in the association request (e.g. D-Link DAP 1353 in
  501. * b-only mode)...
  502. */
  503. rates_len = ieee80211_parse_bitrates(&chanctx_conf->def, sband,
  504. assoc_data->supp_rates,
  505. assoc_data->supp_rates_len,
  506. &rates);
  507. } else {
  508. /*
  509. * In case AP not provide any supported rates information
  510. * before association, we send information element(s) with
  511. * all rates that we support.
  512. */
  513. rates_len = 0;
  514. for (i = 0; i < sband->n_bitrates; i++) {
  515. if ((rate_flags & sband->bitrates[i].flags)
  516. != rate_flags)
  517. continue;
  518. rates |= BIT(i);
  519. rates_len++;
  520. }
  521. }
  522. skb = alloc_skb(local->hw.extra_tx_headroom +
  523. sizeof(*mgmt) + /* bit too much but doesn't matter */
  524. 2 + assoc_data->ssid_len + /* SSID */
  525. 4 + rates_len + /* (extended) rates */
  526. 4 + /* power capability */
  527. 2 + 2 * sband->n_channels + /* supported channels */
  528. 2 + sizeof(struct ieee80211_ht_cap) + /* HT */
  529. 2 + sizeof(struct ieee80211_vht_cap) + /* VHT */
  530. assoc_data->ie_len + /* extra IEs */
  531. 9, /* WMM */
  532. GFP_KERNEL);
  533. if (!skb)
  534. return;
  535. skb_reserve(skb, local->hw.extra_tx_headroom);
  536. capab = WLAN_CAPABILITY_ESS;
  537. if (sband->band == IEEE80211_BAND_2GHZ) {
  538. if (!(local->hw.flags & IEEE80211_HW_2GHZ_SHORT_SLOT_INCAPABLE))
  539. capab |= WLAN_CAPABILITY_SHORT_SLOT_TIME;
  540. if (!(local->hw.flags & IEEE80211_HW_2GHZ_SHORT_PREAMBLE_INCAPABLE))
  541. capab |= WLAN_CAPABILITY_SHORT_PREAMBLE;
  542. }
  543. if (assoc_data->capability & WLAN_CAPABILITY_PRIVACY)
  544. capab |= WLAN_CAPABILITY_PRIVACY;
  545. if ((assoc_data->capability & WLAN_CAPABILITY_SPECTRUM_MGMT) &&
  546. (local->hw.flags & IEEE80211_HW_SPECTRUM_MGMT))
  547. capab |= WLAN_CAPABILITY_SPECTRUM_MGMT;
  548. mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
  549. memset(mgmt, 0, 24);
  550. memcpy(mgmt->da, assoc_data->bss->bssid, ETH_ALEN);
  551. memcpy(mgmt->sa, sdata->vif.addr, ETH_ALEN);
  552. memcpy(mgmt->bssid, assoc_data->bss->bssid, ETH_ALEN);
  553. if (!is_zero_ether_addr(assoc_data->prev_bssid)) {
  554. skb_put(skb, 10);
  555. mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
  556. IEEE80211_STYPE_REASSOC_REQ);
  557. mgmt->u.reassoc_req.capab_info = cpu_to_le16(capab);
  558. mgmt->u.reassoc_req.listen_interval =
  559. cpu_to_le16(local->hw.conf.listen_interval);
  560. memcpy(mgmt->u.reassoc_req.current_ap, assoc_data->prev_bssid,
  561. ETH_ALEN);
  562. } else {
  563. skb_put(skb, 4);
  564. mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
  565. IEEE80211_STYPE_ASSOC_REQ);
  566. mgmt->u.assoc_req.capab_info = cpu_to_le16(capab);
  567. mgmt->u.assoc_req.listen_interval =
  568. cpu_to_le16(local->hw.conf.listen_interval);
  569. }
  570. /* SSID */
  571. pos = skb_put(skb, 2 + assoc_data->ssid_len);
  572. *pos++ = WLAN_EID_SSID;
  573. *pos++ = assoc_data->ssid_len;
  574. memcpy(pos, assoc_data->ssid, assoc_data->ssid_len);
  575. /* add all rates which were marked to be used above */
  576. supp_rates_len = rates_len;
  577. if (supp_rates_len > 8)
  578. supp_rates_len = 8;
  579. pos = skb_put(skb, supp_rates_len + 2);
  580. *pos++ = WLAN_EID_SUPP_RATES;
  581. *pos++ = supp_rates_len;
  582. count = 0;
  583. for (i = 0; i < sband->n_bitrates; i++) {
  584. if (BIT(i) & rates) {
  585. int rate = DIV_ROUND_UP(sband->bitrates[i].bitrate,
  586. 5 * (1 << shift));
  587. *pos++ = (u8) rate;
  588. if (++count == 8)
  589. break;
  590. }
  591. }
  592. if (rates_len > count) {
  593. pos = skb_put(skb, rates_len - count + 2);
  594. *pos++ = WLAN_EID_EXT_SUPP_RATES;
  595. *pos++ = rates_len - count;
  596. for (i++; i < sband->n_bitrates; i++) {
  597. if (BIT(i) & rates) {
  598. int rate;
  599. rate = DIV_ROUND_UP(sband->bitrates[i].bitrate,
  600. 5 * (1 << shift));
  601. *pos++ = (u8) rate;
  602. }
  603. }
  604. }
  605. if (capab & WLAN_CAPABILITY_SPECTRUM_MGMT) {
  606. /* 1. power capabilities */
  607. pos = skb_put(skb, 4);
  608. *pos++ = WLAN_EID_PWR_CAPABILITY;
  609. *pos++ = 2;
  610. *pos++ = 0; /* min tx power */
  611. /* max tx power */
  612. *pos++ = ieee80211_chandef_max_power(&chanctx_conf->def);
  613. /* 2. supported channels */
  614. /* TODO: get this in reg domain format */
  615. pos = skb_put(skb, 2 * sband->n_channels + 2);
  616. *pos++ = WLAN_EID_SUPPORTED_CHANNELS;
  617. *pos++ = 2 * sband->n_channels;
  618. for (i = 0; i < sband->n_channels; i++) {
  619. *pos++ = ieee80211_frequency_to_channel(
  620. sband->channels[i].center_freq);
  621. *pos++ = 1; /* one channel in the subband*/
  622. }
  623. }
  624. /* if present, add any custom IEs that go before HT */
  625. if (assoc_data->ie_len) {
  626. static const u8 before_ht[] = {
  627. WLAN_EID_SSID,
  628. WLAN_EID_SUPP_RATES,
  629. WLAN_EID_EXT_SUPP_RATES,
  630. WLAN_EID_PWR_CAPABILITY,
  631. WLAN_EID_SUPPORTED_CHANNELS,
  632. WLAN_EID_RSN,
  633. WLAN_EID_QOS_CAPA,
  634. WLAN_EID_RRM_ENABLED_CAPABILITIES,
  635. WLAN_EID_MOBILITY_DOMAIN,
  636. WLAN_EID_SUPPORTED_REGULATORY_CLASSES,
  637. };
  638. noffset = ieee80211_ie_split(assoc_data->ie, assoc_data->ie_len,
  639. before_ht, ARRAY_SIZE(before_ht),
  640. offset);
  641. pos = skb_put(skb, noffset - offset);
  642. memcpy(pos, assoc_data->ie + offset, noffset - offset);
  643. offset = noffset;
  644. }
  645. if (WARN_ON_ONCE((ifmgd->flags & IEEE80211_STA_DISABLE_HT) &&
  646. !(ifmgd->flags & IEEE80211_STA_DISABLE_VHT)))
  647. ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
  648. if (!(ifmgd->flags & IEEE80211_STA_DISABLE_HT))
  649. ieee80211_add_ht_ie(sdata, skb, assoc_data->ap_ht_param,
  650. sband, chan, sdata->smps_mode);
  651. if (!(ifmgd->flags & IEEE80211_STA_DISABLE_VHT))
  652. ieee80211_add_vht_ie(sdata, skb, sband,
  653. &assoc_data->ap_vht_cap);
  654. /* if present, add any custom non-vendor IEs that go after HT */
  655. if (assoc_data->ie_len) {
  656. noffset = ieee80211_ie_split_vendor(assoc_data->ie,
  657. assoc_data->ie_len,
  658. offset);
  659. pos = skb_put(skb, noffset - offset);
  660. memcpy(pos, assoc_data->ie + offset, noffset - offset);
  661. offset = noffset;
  662. }
  663. if (assoc_data->wmm) {
  664. if (assoc_data->uapsd) {
  665. qos_info = ifmgd->uapsd_queues;
  666. qos_info |= (ifmgd->uapsd_max_sp_len <<
  667. IEEE80211_WMM_IE_STA_QOSINFO_SP_SHIFT);
  668. } else {
  669. qos_info = 0;
  670. }
  671. pos = skb_put(skb, 9);
  672. *pos++ = WLAN_EID_VENDOR_SPECIFIC;
  673. *pos++ = 7; /* len */
  674. *pos++ = 0x00; /* Microsoft OUI 00:50:F2 */
  675. *pos++ = 0x50;
  676. *pos++ = 0xf2;
  677. *pos++ = 2; /* WME */
  678. *pos++ = 0; /* WME info */
  679. *pos++ = 1; /* WME ver */
  680. *pos++ = qos_info;
  681. }
  682. /* add any remaining custom (i.e. vendor specific here) IEs */
  683. if (assoc_data->ie_len) {
  684. noffset = assoc_data->ie_len;
  685. pos = skb_put(skb, noffset - offset);
  686. memcpy(pos, assoc_data->ie + offset, noffset - offset);
  687. }
  688. drv_mgd_prepare_tx(local, sdata);
  689. IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
  690. if (local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS)
  691. IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_CTL_REQ_TX_STATUS |
  692. IEEE80211_TX_INTFL_MLME_CONN_TX;
  693. ieee80211_tx_skb(sdata, skb);
  694. }
  695. void ieee80211_send_pspoll(struct ieee80211_local *local,
  696. struct ieee80211_sub_if_data *sdata)
  697. {
  698. struct ieee80211_pspoll *pspoll;
  699. struct sk_buff *skb;
  700. skb = ieee80211_pspoll_get(&local->hw, &sdata->vif);
  701. if (!skb)
  702. return;
  703. pspoll = (struct ieee80211_pspoll *) skb->data;
  704. pspoll->frame_control |= cpu_to_le16(IEEE80211_FCTL_PM);
  705. IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
  706. ieee80211_tx_skb(sdata, skb);
  707. }
  708. void ieee80211_send_nullfunc(struct ieee80211_local *local,
  709. struct ieee80211_sub_if_data *sdata,
  710. int powersave)
  711. {
  712. struct sk_buff *skb;
  713. struct ieee80211_hdr_3addr *nullfunc;
  714. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  715. skb = ieee80211_nullfunc_get(&local->hw, &sdata->vif);
  716. if (!skb)
  717. return;
  718. nullfunc = (struct ieee80211_hdr_3addr *) skb->data;
  719. if (powersave)
  720. nullfunc->frame_control |= cpu_to_le16(IEEE80211_FCTL_PM);
  721. IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT |
  722. IEEE80211_TX_INTFL_OFFCHAN_TX_OK;
  723. if (local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS)
  724. IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_CTL_REQ_TX_STATUS;
  725. if (ifmgd->flags & IEEE80211_STA_CONNECTION_POLL)
  726. IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_CTL_USE_MINRATE;
  727. ieee80211_tx_skb(sdata, skb);
  728. }
  729. static void ieee80211_send_4addr_nullfunc(struct ieee80211_local *local,
  730. struct ieee80211_sub_if_data *sdata)
  731. {
  732. struct sk_buff *skb;
  733. struct ieee80211_hdr *nullfunc;
  734. __le16 fc;
  735. if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_STATION))
  736. return;
  737. skb = dev_alloc_skb(local->hw.extra_tx_headroom + 30);
  738. if (!skb)
  739. return;
  740. skb_reserve(skb, local->hw.extra_tx_headroom);
  741. nullfunc = (struct ieee80211_hdr *) skb_put(skb, 30);
  742. memset(nullfunc, 0, 30);
  743. fc = cpu_to_le16(IEEE80211_FTYPE_DATA | IEEE80211_STYPE_NULLFUNC |
  744. IEEE80211_FCTL_FROMDS | IEEE80211_FCTL_TODS);
  745. nullfunc->frame_control = fc;
  746. memcpy(nullfunc->addr1, sdata->u.mgd.bssid, ETH_ALEN);
  747. memcpy(nullfunc->addr2, sdata->vif.addr, ETH_ALEN);
  748. memcpy(nullfunc->addr3, sdata->u.mgd.bssid, ETH_ALEN);
  749. memcpy(nullfunc->addr4, sdata->vif.addr, ETH_ALEN);
  750. IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
  751. ieee80211_tx_skb(sdata, skb);
  752. }
  753. /* spectrum management related things */
  754. static void ieee80211_chswitch_work(struct work_struct *work)
  755. {
  756. struct ieee80211_sub_if_data *sdata =
  757. container_of(work, struct ieee80211_sub_if_data, u.mgd.chswitch_work);
  758. struct ieee80211_local *local = sdata->local;
  759. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  760. u32 changed = 0;
  761. int ret;
  762. if (!ieee80211_sdata_running(sdata))
  763. return;
  764. sdata_lock(sdata);
  765. if (!ifmgd->associated)
  766. goto out;
  767. ret = ieee80211_vif_change_channel(sdata, &changed);
  768. if (ret) {
  769. sdata_info(sdata,
  770. "vif channel switch failed, disconnecting\n");
  771. ieee80211_queue_work(&sdata->local->hw,
  772. &ifmgd->csa_connection_drop_work);
  773. goto out;
  774. }
  775. if (!local->use_chanctx) {
  776. local->_oper_chandef = sdata->csa_chandef;
  777. /* Call "hw_config" only if doing sw channel switch.
  778. * Otherwise update the channel directly
  779. */
  780. if (!local->ops->channel_switch)
  781. ieee80211_hw_config(local, 0);
  782. else
  783. local->hw.conf.chandef = local->_oper_chandef;
  784. }
  785. /* XXX: shouldn't really modify cfg80211-owned data! */
  786. ifmgd->associated->channel = sdata->csa_chandef.chan;
  787. /* XXX: wait for a beacon first? */
  788. ieee80211_wake_queues_by_reason(&local->hw,
  789. IEEE80211_MAX_QUEUE_MAP,
  790. IEEE80211_QUEUE_STOP_REASON_CSA);
  791. ieee80211_bss_info_change_notify(sdata, changed);
  792. out:
  793. sdata->vif.csa_active = false;
  794. ifmgd->flags &= ~IEEE80211_STA_CSA_RECEIVED;
  795. sdata_unlock(sdata);
  796. }
  797. void ieee80211_chswitch_done(struct ieee80211_vif *vif, bool success)
  798. {
  799. struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
  800. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  801. trace_api_chswitch_done(sdata, success);
  802. if (!success) {
  803. sdata_info(sdata,
  804. "driver channel switch failed, disconnecting\n");
  805. ieee80211_queue_work(&sdata->local->hw,
  806. &ifmgd->csa_connection_drop_work);
  807. } else {
  808. ieee80211_queue_work(&sdata->local->hw, &ifmgd->chswitch_work);
  809. }
  810. }
  811. EXPORT_SYMBOL(ieee80211_chswitch_done);
  812. static void ieee80211_chswitch_timer(unsigned long data)
  813. {
  814. struct ieee80211_sub_if_data *sdata =
  815. (struct ieee80211_sub_if_data *) data;
  816. ieee80211_queue_work(&sdata->local->hw, &sdata->u.mgd.chswitch_work);
  817. }
  818. static void
  819. ieee80211_sta_process_chanswitch(struct ieee80211_sub_if_data *sdata,
  820. u64 timestamp, struct ieee802_11_elems *elems,
  821. bool beacon)
  822. {
  823. struct ieee80211_local *local = sdata->local;
  824. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  825. struct cfg80211_bss *cbss = ifmgd->associated;
  826. struct ieee80211_chanctx *chanctx;
  827. enum ieee80211_band current_band;
  828. struct ieee80211_csa_ie csa_ie;
  829. int res;
  830. sdata_assert_lock(sdata);
  831. if (!cbss)
  832. return;
  833. if (local->scanning)
  834. return;
  835. /* disregard subsequent announcements if we are already processing */
  836. if (ifmgd->flags & IEEE80211_STA_CSA_RECEIVED)
  837. return;
  838. current_band = cbss->channel->band;
  839. memset(&csa_ie, 0, sizeof(csa_ie));
  840. res = ieee80211_parse_ch_switch_ie(sdata, elems, beacon, current_band,
  841. ifmgd->flags,
  842. ifmgd->associated->bssid, &csa_ie);
  843. if (res < 0)
  844. ieee80211_queue_work(&local->hw,
  845. &ifmgd->csa_connection_drop_work);
  846. if (res)
  847. return;
  848. if (!cfg80211_chandef_usable(local->hw.wiphy, &csa_ie.chandef,
  849. IEEE80211_CHAN_DISABLED)) {
  850. sdata_info(sdata,
  851. "AP %pM switches to unsupported channel (%d MHz, width:%d, CF1/2: %d/%d MHz), disconnecting\n",
  852. ifmgd->associated->bssid,
  853. csa_ie.chandef.chan->center_freq,
  854. csa_ie.chandef.width, csa_ie.chandef.center_freq1,
  855. csa_ie.chandef.center_freq2);
  856. ieee80211_queue_work(&local->hw,
  857. &ifmgd->csa_connection_drop_work);
  858. return;
  859. }
  860. ifmgd->flags |= IEEE80211_STA_CSA_RECEIVED;
  861. sdata->vif.csa_active = true;
  862. mutex_lock(&local->chanctx_mtx);
  863. if (local->use_chanctx) {
  864. u32 num_chanctx = 0;
  865. list_for_each_entry(chanctx, &local->chanctx_list, list)
  866. num_chanctx++;
  867. if (num_chanctx > 1 ||
  868. !(local->hw.flags & IEEE80211_HW_CHANCTX_STA_CSA)) {
  869. sdata_info(sdata,
  870. "not handling chan-switch with channel contexts\n");
  871. ieee80211_queue_work(&local->hw,
  872. &ifmgd->csa_connection_drop_work);
  873. mutex_unlock(&local->chanctx_mtx);
  874. return;
  875. }
  876. }
  877. if (WARN_ON(!rcu_access_pointer(sdata->vif.chanctx_conf))) {
  878. ieee80211_queue_work(&local->hw,
  879. &ifmgd->csa_connection_drop_work);
  880. mutex_unlock(&local->chanctx_mtx);
  881. return;
  882. }
  883. chanctx = container_of(rcu_access_pointer(sdata->vif.chanctx_conf),
  884. struct ieee80211_chanctx, conf);
  885. if (chanctx->refcount > 1) {
  886. sdata_info(sdata,
  887. "channel switch with multiple interfaces on the same channel, disconnecting\n");
  888. ieee80211_queue_work(&local->hw,
  889. &ifmgd->csa_connection_drop_work);
  890. mutex_unlock(&local->chanctx_mtx);
  891. return;
  892. }
  893. mutex_unlock(&local->chanctx_mtx);
  894. sdata->csa_chandef = csa_ie.chandef;
  895. if (csa_ie.mode)
  896. ieee80211_stop_queues_by_reason(&local->hw,
  897. IEEE80211_MAX_QUEUE_MAP,
  898. IEEE80211_QUEUE_STOP_REASON_CSA);
  899. if (local->ops->channel_switch) {
  900. /* use driver's channel switch callback */
  901. struct ieee80211_channel_switch ch_switch = {
  902. .timestamp = timestamp,
  903. .block_tx = csa_ie.mode,
  904. .chandef = csa_ie.chandef,
  905. .count = csa_ie.count,
  906. };
  907. drv_channel_switch(local, &ch_switch);
  908. return;
  909. }
  910. /* channel switch handled in software */
  911. if (csa_ie.count <= 1)
  912. ieee80211_queue_work(&local->hw, &ifmgd->chswitch_work);
  913. else
  914. mod_timer(&ifmgd->chswitch_timer,
  915. TU_TO_EXP_TIME(csa_ie.count * cbss->beacon_interval));
  916. }
  917. static u32 ieee80211_handle_pwr_constr(struct ieee80211_sub_if_data *sdata,
  918. struct ieee80211_channel *channel,
  919. const u8 *country_ie, u8 country_ie_len,
  920. const u8 *pwr_constr_elem)
  921. {
  922. struct ieee80211_country_ie_triplet *triplet;
  923. int chan = ieee80211_frequency_to_channel(channel->center_freq);
  924. int i, chan_pwr, chan_increment, new_ap_level;
  925. bool have_chan_pwr = false;
  926. /* Invalid IE */
  927. if (country_ie_len % 2 || country_ie_len < IEEE80211_COUNTRY_IE_MIN_LEN)
  928. return 0;
  929. triplet = (void *)(country_ie + 3);
  930. country_ie_len -= 3;
  931. switch (channel->band) {
  932. default:
  933. WARN_ON_ONCE(1);
  934. /* fall through */
  935. case IEEE80211_BAND_2GHZ:
  936. case IEEE80211_BAND_60GHZ:
  937. chan_increment = 1;
  938. break;
  939. case IEEE80211_BAND_5GHZ:
  940. chan_increment = 4;
  941. break;
  942. }
  943. /* find channel */
  944. while (country_ie_len >= 3) {
  945. u8 first_channel = triplet->chans.first_channel;
  946. if (first_channel >= IEEE80211_COUNTRY_EXTENSION_ID)
  947. goto next;
  948. for (i = 0; i < triplet->chans.num_channels; i++) {
  949. if (first_channel + i * chan_increment == chan) {
  950. have_chan_pwr = true;
  951. chan_pwr = triplet->chans.max_power;
  952. break;
  953. }
  954. }
  955. if (have_chan_pwr)
  956. break;
  957. next:
  958. triplet++;
  959. country_ie_len -= 3;
  960. }
  961. if (!have_chan_pwr)
  962. return 0;
  963. new_ap_level = max_t(int, 0, chan_pwr - *pwr_constr_elem);
  964. if (sdata->ap_power_level == new_ap_level)
  965. return 0;
  966. sdata_info(sdata,
  967. "Limiting TX power to %d (%d - %d) dBm as advertised by %pM\n",
  968. new_ap_level, chan_pwr, *pwr_constr_elem,
  969. sdata->u.mgd.bssid);
  970. sdata->ap_power_level = new_ap_level;
  971. if (__ieee80211_recalc_txpower(sdata))
  972. return BSS_CHANGED_TXPOWER;
  973. return 0;
  974. }
  975. /* powersave */
  976. static void ieee80211_enable_ps(struct ieee80211_local *local,
  977. struct ieee80211_sub_if_data *sdata)
  978. {
  979. struct ieee80211_conf *conf = &local->hw.conf;
  980. /*
  981. * If we are scanning right now then the parameters will
  982. * take effect when scan finishes.
  983. */
  984. if (local->scanning)
  985. return;
  986. if (conf->dynamic_ps_timeout > 0 &&
  987. !(local->hw.flags & IEEE80211_HW_SUPPORTS_DYNAMIC_PS)) {
  988. mod_timer(&local->dynamic_ps_timer, jiffies +
  989. msecs_to_jiffies(conf->dynamic_ps_timeout));
  990. } else {
  991. if (local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK)
  992. ieee80211_send_nullfunc(local, sdata, 1);
  993. if ((local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK) &&
  994. (local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS))
  995. return;
  996. conf->flags |= IEEE80211_CONF_PS;
  997. ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
  998. }
  999. }
  1000. static void ieee80211_change_ps(struct ieee80211_local *local)
  1001. {
  1002. struct ieee80211_conf *conf = &local->hw.conf;
  1003. if (local->ps_sdata) {
  1004. ieee80211_enable_ps(local, local->ps_sdata);
  1005. } else if (conf->flags & IEEE80211_CONF_PS) {
  1006. conf->flags &= ~IEEE80211_CONF_PS;
  1007. ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
  1008. del_timer_sync(&local->dynamic_ps_timer);
  1009. cancel_work_sync(&local->dynamic_ps_enable_work);
  1010. }
  1011. }
  1012. static bool ieee80211_powersave_allowed(struct ieee80211_sub_if_data *sdata)
  1013. {
  1014. struct ieee80211_if_managed *mgd = &sdata->u.mgd;
  1015. struct sta_info *sta = NULL;
  1016. bool authorized = false;
  1017. if (!mgd->powersave)
  1018. return false;
  1019. if (mgd->broken_ap)
  1020. return false;
  1021. if (!mgd->associated)
  1022. return false;
  1023. if (mgd->flags & IEEE80211_STA_CONNECTION_POLL)
  1024. return false;
  1025. if (!mgd->have_beacon)
  1026. return false;
  1027. rcu_read_lock();
  1028. sta = sta_info_get(sdata, mgd->bssid);
  1029. if (sta)
  1030. authorized = test_sta_flag(sta, WLAN_STA_AUTHORIZED);
  1031. rcu_read_unlock();
  1032. return authorized;
  1033. }
  1034. /* need to hold RTNL or interface lock */
  1035. void ieee80211_recalc_ps(struct ieee80211_local *local, s32 latency)
  1036. {
  1037. struct ieee80211_sub_if_data *sdata, *found = NULL;
  1038. int count = 0;
  1039. int timeout;
  1040. if (!(local->hw.flags & IEEE80211_HW_SUPPORTS_PS)) {
  1041. local->ps_sdata = NULL;
  1042. return;
  1043. }
  1044. list_for_each_entry(sdata, &local->interfaces, list) {
  1045. if (!ieee80211_sdata_running(sdata))
  1046. continue;
  1047. if (sdata->vif.type == NL80211_IFTYPE_AP) {
  1048. /* If an AP vif is found, then disable PS
  1049. * by setting the count to zero thereby setting
  1050. * ps_sdata to NULL.
  1051. */
  1052. count = 0;
  1053. break;
  1054. }
  1055. if (sdata->vif.type != NL80211_IFTYPE_STATION)
  1056. continue;
  1057. found = sdata;
  1058. count++;
  1059. }
  1060. if (count == 1 && ieee80211_powersave_allowed(found)) {
  1061. s32 beaconint_us;
  1062. if (latency < 0)
  1063. latency = pm_qos_request(PM_QOS_NETWORK_LATENCY);
  1064. beaconint_us = ieee80211_tu_to_usec(
  1065. found->vif.bss_conf.beacon_int);
  1066. timeout = local->dynamic_ps_forced_timeout;
  1067. if (timeout < 0) {
  1068. /*
  1069. * Go to full PSM if the user configures a very low
  1070. * latency requirement.
  1071. * The 2000 second value is there for compatibility
  1072. * until the PM_QOS_NETWORK_LATENCY is configured
  1073. * with real values.
  1074. */
  1075. if (latency > (1900 * USEC_PER_MSEC) &&
  1076. latency != (2000 * USEC_PER_SEC))
  1077. timeout = 0;
  1078. else
  1079. timeout = 100;
  1080. }
  1081. local->hw.conf.dynamic_ps_timeout = timeout;
  1082. if (beaconint_us > latency) {
  1083. local->ps_sdata = NULL;
  1084. } else {
  1085. int maxslp = 1;
  1086. u8 dtimper = found->u.mgd.dtim_period;
  1087. /* If the TIM IE is invalid, pretend the value is 1 */
  1088. if (!dtimper)
  1089. dtimper = 1;
  1090. else if (dtimper > 1)
  1091. maxslp = min_t(int, dtimper,
  1092. latency / beaconint_us);
  1093. local->hw.conf.max_sleep_period = maxslp;
  1094. local->hw.conf.ps_dtim_period = dtimper;
  1095. local->ps_sdata = found;
  1096. }
  1097. } else {
  1098. local->ps_sdata = NULL;
  1099. }
  1100. ieee80211_change_ps(local);
  1101. }
  1102. void ieee80211_recalc_ps_vif(struct ieee80211_sub_if_data *sdata)
  1103. {
  1104. bool ps_allowed = ieee80211_powersave_allowed(sdata);
  1105. if (sdata->vif.bss_conf.ps != ps_allowed) {
  1106. sdata->vif.bss_conf.ps = ps_allowed;
  1107. ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_PS);
  1108. }
  1109. }
  1110. void ieee80211_dynamic_ps_disable_work(struct work_struct *work)
  1111. {
  1112. struct ieee80211_local *local =
  1113. container_of(work, struct ieee80211_local,
  1114. dynamic_ps_disable_work);
  1115. if (local->hw.conf.flags & IEEE80211_CONF_PS) {
  1116. local->hw.conf.flags &= ~IEEE80211_CONF_PS;
  1117. ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
  1118. }
  1119. ieee80211_wake_queues_by_reason(&local->hw,
  1120. IEEE80211_MAX_QUEUE_MAP,
  1121. IEEE80211_QUEUE_STOP_REASON_PS);
  1122. }
  1123. void ieee80211_dynamic_ps_enable_work(struct work_struct *work)
  1124. {
  1125. struct ieee80211_local *local =
  1126. container_of(work, struct ieee80211_local,
  1127. dynamic_ps_enable_work);
  1128. struct ieee80211_sub_if_data *sdata = local->ps_sdata;
  1129. struct ieee80211_if_managed *ifmgd;
  1130. unsigned long flags;
  1131. int q;
  1132. /* can only happen when PS was just disabled anyway */
  1133. if (!sdata)
  1134. return;
  1135. ifmgd = &sdata->u.mgd;
  1136. if (local->hw.conf.flags & IEEE80211_CONF_PS)
  1137. return;
  1138. if (local->hw.conf.dynamic_ps_timeout > 0) {
  1139. /* don't enter PS if TX frames are pending */
  1140. if (drv_tx_frames_pending(local)) {
  1141. mod_timer(&local->dynamic_ps_timer, jiffies +
  1142. msecs_to_jiffies(
  1143. local->hw.conf.dynamic_ps_timeout));
  1144. return;
  1145. }
  1146. /*
  1147. * transmission can be stopped by others which leads to
  1148. * dynamic_ps_timer expiry. Postpone the ps timer if it
  1149. * is not the actual idle state.
  1150. */
  1151. spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
  1152. for (q = 0; q < local->hw.queues; q++) {
  1153. if (local->queue_stop_reasons[q]) {
  1154. spin_unlock_irqrestore(&local->queue_stop_reason_lock,
  1155. flags);
  1156. mod_timer(&local->dynamic_ps_timer, jiffies +
  1157. msecs_to_jiffies(
  1158. local->hw.conf.dynamic_ps_timeout));
  1159. return;
  1160. }
  1161. }
  1162. spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
  1163. }
  1164. if ((local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK) &&
  1165. !(ifmgd->flags & IEEE80211_STA_NULLFUNC_ACKED)) {
  1166. if (drv_tx_frames_pending(local)) {
  1167. mod_timer(&local->dynamic_ps_timer, jiffies +
  1168. msecs_to_jiffies(
  1169. local->hw.conf.dynamic_ps_timeout));
  1170. } else {
  1171. ieee80211_send_nullfunc(local, sdata, 1);
  1172. /* Flush to get the tx status of nullfunc frame */
  1173. ieee80211_flush_queues(local, sdata);
  1174. }
  1175. }
  1176. if (!((local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS) &&
  1177. (local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK)) ||
  1178. (ifmgd->flags & IEEE80211_STA_NULLFUNC_ACKED)) {
  1179. ifmgd->flags &= ~IEEE80211_STA_NULLFUNC_ACKED;
  1180. local->hw.conf.flags |= IEEE80211_CONF_PS;
  1181. ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
  1182. }
  1183. }
  1184. void ieee80211_dynamic_ps_timer(unsigned long data)
  1185. {
  1186. struct ieee80211_local *local = (void *) data;
  1187. if (local->quiescing || local->suspended)
  1188. return;
  1189. ieee80211_queue_work(&local->hw, &local->dynamic_ps_enable_work);
  1190. }
  1191. void ieee80211_dfs_cac_timer_work(struct work_struct *work)
  1192. {
  1193. struct delayed_work *delayed_work =
  1194. container_of(work, struct delayed_work, work);
  1195. struct ieee80211_sub_if_data *sdata =
  1196. container_of(delayed_work, struct ieee80211_sub_if_data,
  1197. dfs_cac_timer_work);
  1198. struct cfg80211_chan_def chandef = sdata->vif.bss_conf.chandef;
  1199. ieee80211_vif_release_channel(sdata);
  1200. cfg80211_cac_event(sdata->dev, &chandef,
  1201. NL80211_RADAR_CAC_FINISHED,
  1202. GFP_KERNEL);
  1203. }
  1204. /* MLME */
  1205. static bool ieee80211_sta_wmm_params(struct ieee80211_local *local,
  1206. struct ieee80211_sub_if_data *sdata,
  1207. const u8 *wmm_param, size_t wmm_param_len)
  1208. {
  1209. struct ieee80211_tx_queue_params params;
  1210. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  1211. size_t left;
  1212. int count;
  1213. const u8 *pos;
  1214. u8 uapsd_queues = 0;
  1215. if (!local->ops->conf_tx)
  1216. return false;
  1217. if (local->hw.queues < IEEE80211_NUM_ACS)
  1218. return false;
  1219. if (!wmm_param)
  1220. return false;
  1221. if (wmm_param_len < 8 || wmm_param[5] /* version */ != 1)
  1222. return false;
  1223. if (ifmgd->flags & IEEE80211_STA_UAPSD_ENABLED)
  1224. uapsd_queues = ifmgd->uapsd_queues;
  1225. count = wmm_param[6] & 0x0f;
  1226. if (count == ifmgd->wmm_last_param_set)
  1227. return false;
  1228. ifmgd->wmm_last_param_set = count;
  1229. pos = wmm_param + 8;
  1230. left = wmm_param_len - 8;
  1231. memset(&params, 0, sizeof(params));
  1232. sdata->wmm_acm = 0;
  1233. for (; left >= 4; left -= 4, pos += 4) {
  1234. int aci = (pos[0] >> 5) & 0x03;
  1235. int acm = (pos[0] >> 4) & 0x01;
  1236. bool uapsd = false;
  1237. int queue;
  1238. switch (aci) {
  1239. case 1: /* AC_BK */
  1240. queue = 3;
  1241. if (acm)
  1242. sdata->wmm_acm |= BIT(1) | BIT(2); /* BK/- */
  1243. if (uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_BK)
  1244. uapsd = true;
  1245. break;
  1246. case 2: /* AC_VI */
  1247. queue = 1;
  1248. if (acm)
  1249. sdata->wmm_acm |= BIT(4) | BIT(5); /* CL/VI */
  1250. if (uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_VI)
  1251. uapsd = true;
  1252. break;
  1253. case 3: /* AC_VO */
  1254. queue = 0;
  1255. if (acm)
  1256. sdata->wmm_acm |= BIT(6) | BIT(7); /* VO/NC */
  1257. if (uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_VO)
  1258. uapsd = true;
  1259. break;
  1260. case 0: /* AC_BE */
  1261. default:
  1262. queue = 2;
  1263. if (acm)
  1264. sdata->wmm_acm |= BIT(0) | BIT(3); /* BE/EE */
  1265. if (uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_BE)
  1266. uapsd = true;
  1267. break;
  1268. }
  1269. params.aifs = pos[0] & 0x0f;
  1270. params.cw_max = ecw2cw((pos[1] & 0xf0) >> 4);
  1271. params.cw_min = ecw2cw(pos[1] & 0x0f);
  1272. params.txop = get_unaligned_le16(pos + 2);
  1273. params.acm = acm;
  1274. params.uapsd = uapsd;
  1275. mlme_dbg(sdata,
  1276. "WMM queue=%d aci=%d acm=%d aifs=%d cWmin=%d cWmax=%d txop=%d uapsd=%d\n",
  1277. queue, aci, acm,
  1278. params.aifs, params.cw_min, params.cw_max,
  1279. params.txop, params.uapsd);
  1280. sdata->tx_conf[queue] = params;
  1281. if (drv_conf_tx(local, sdata, queue, &params))
  1282. sdata_err(sdata,
  1283. "failed to set TX queue parameters for queue %d\n",
  1284. queue);
  1285. }
  1286. /* enable WMM or activate new settings */
  1287. sdata->vif.bss_conf.qos = true;
  1288. return true;
  1289. }
  1290. static void __ieee80211_stop_poll(struct ieee80211_sub_if_data *sdata)
  1291. {
  1292. lockdep_assert_held(&sdata->local->mtx);
  1293. sdata->u.mgd.flags &= ~IEEE80211_STA_CONNECTION_POLL;
  1294. ieee80211_run_deferred_scan(sdata->local);
  1295. }
  1296. static void ieee80211_stop_poll(struct ieee80211_sub_if_data *sdata)
  1297. {
  1298. mutex_lock(&sdata->local->mtx);
  1299. __ieee80211_stop_poll(sdata);
  1300. mutex_unlock(&sdata->local->mtx);
  1301. }
  1302. static u32 ieee80211_handle_bss_capability(struct ieee80211_sub_if_data *sdata,
  1303. u16 capab, bool erp_valid, u8 erp)
  1304. {
  1305. struct ieee80211_bss_conf *bss_conf = &sdata->vif.bss_conf;
  1306. u32 changed = 0;
  1307. bool use_protection;
  1308. bool use_short_preamble;
  1309. bool use_short_slot;
  1310. if (erp_valid) {
  1311. use_protection = (erp & WLAN_ERP_USE_PROTECTION) != 0;
  1312. use_short_preamble = (erp & WLAN_ERP_BARKER_PREAMBLE) == 0;
  1313. } else {
  1314. use_protection = false;
  1315. use_short_preamble = !!(capab & WLAN_CAPABILITY_SHORT_PREAMBLE);
  1316. }
  1317. use_short_slot = !!(capab & WLAN_CAPABILITY_SHORT_SLOT_TIME);
  1318. if (ieee80211_get_sdata_band(sdata) == IEEE80211_BAND_5GHZ)
  1319. use_short_slot = true;
  1320. if (use_protection != bss_conf->use_cts_prot) {
  1321. bss_conf->use_cts_prot = use_protection;
  1322. changed |= BSS_CHANGED_ERP_CTS_PROT;
  1323. }
  1324. if (use_short_preamble != bss_conf->use_short_preamble) {
  1325. bss_conf->use_short_preamble = use_short_preamble;
  1326. changed |= BSS_CHANGED_ERP_PREAMBLE;
  1327. }
  1328. if (use_short_slot != bss_conf->use_short_slot) {
  1329. bss_conf->use_short_slot = use_short_slot;
  1330. changed |= BSS_CHANGED_ERP_SLOT;
  1331. }
  1332. return changed;
  1333. }
  1334. static void ieee80211_set_associated(struct ieee80211_sub_if_data *sdata,
  1335. struct cfg80211_bss *cbss,
  1336. u32 bss_info_changed)
  1337. {
  1338. struct ieee80211_bss *bss = (void *)cbss->priv;
  1339. struct ieee80211_local *local = sdata->local;
  1340. struct ieee80211_bss_conf *bss_conf = &sdata->vif.bss_conf;
  1341. bss_info_changed |= BSS_CHANGED_ASSOC;
  1342. bss_info_changed |= ieee80211_handle_bss_capability(sdata,
  1343. bss_conf->assoc_capability, bss->has_erp_value, bss->erp_value);
  1344. sdata->u.mgd.beacon_timeout = usecs_to_jiffies(ieee80211_tu_to_usec(
  1345. beacon_loss_count * bss_conf->beacon_int));
  1346. sdata->u.mgd.associated = cbss;
  1347. memcpy(sdata->u.mgd.bssid, cbss->bssid, ETH_ALEN);
  1348. sdata->u.mgd.flags |= IEEE80211_STA_RESET_SIGNAL_AVE;
  1349. if (sdata->vif.p2p) {
  1350. const struct cfg80211_bss_ies *ies;
  1351. rcu_read_lock();
  1352. ies = rcu_dereference(cbss->ies);
  1353. if (ies) {
  1354. int ret;
  1355. ret = cfg80211_get_p2p_attr(
  1356. ies->data, ies->len,
  1357. IEEE80211_P2P_ATTR_ABSENCE_NOTICE,
  1358. (u8 *) &bss_conf->p2p_noa_attr,
  1359. sizeof(bss_conf->p2p_noa_attr));
  1360. if (ret >= 2) {
  1361. sdata->u.mgd.p2p_noa_index =
  1362. bss_conf->p2p_noa_attr.index;
  1363. bss_info_changed |= BSS_CHANGED_P2P_PS;
  1364. }
  1365. }
  1366. rcu_read_unlock();
  1367. }
  1368. /* just to be sure */
  1369. ieee80211_stop_poll(sdata);
  1370. ieee80211_led_assoc(local, 1);
  1371. if (sdata->u.mgd.have_beacon) {
  1372. /*
  1373. * If the AP is buggy we may get here with no DTIM period
  1374. * known, so assume it's 1 which is the only safe assumption
  1375. * in that case, although if the TIM IE is broken powersave
  1376. * probably just won't work at all.
  1377. */
  1378. bss_conf->dtim_period = sdata->u.mgd.dtim_period ?: 1;
  1379. bss_conf->beacon_rate = bss->beacon_rate;
  1380. bss_info_changed |= BSS_CHANGED_BEACON_INFO;
  1381. } else {
  1382. bss_conf->beacon_rate = NULL;
  1383. bss_conf->dtim_period = 0;
  1384. }
  1385. bss_conf->assoc = 1;
  1386. /* Tell the driver to monitor connection quality (if supported) */
  1387. if (sdata->vif.driver_flags & IEEE80211_VIF_SUPPORTS_CQM_RSSI &&
  1388. bss_conf->cqm_rssi_thold)
  1389. bss_info_changed |= BSS_CHANGED_CQM;
  1390. /* Enable ARP filtering */
  1391. if (bss_conf->arp_addr_cnt)
  1392. bss_info_changed |= BSS_CHANGED_ARP_FILTER;
  1393. ieee80211_bss_info_change_notify(sdata, bss_info_changed);
  1394. mutex_lock(&local->iflist_mtx);
  1395. ieee80211_recalc_ps(local, -1);
  1396. mutex_unlock(&local->iflist_mtx);
  1397. ieee80211_recalc_smps(sdata);
  1398. ieee80211_recalc_ps_vif(sdata);
  1399. netif_carrier_on(sdata->dev);
  1400. }
  1401. static void ieee80211_set_disassoc(struct ieee80211_sub_if_data *sdata,
  1402. u16 stype, u16 reason, bool tx,
  1403. u8 *frame_buf)
  1404. {
  1405. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  1406. struct ieee80211_local *local = sdata->local;
  1407. u32 changed = 0;
  1408. sdata_assert_lock(sdata);
  1409. if (WARN_ON_ONCE(tx && !frame_buf))
  1410. return;
  1411. if (WARN_ON(!ifmgd->associated))
  1412. return;
  1413. ieee80211_stop_poll(sdata);
  1414. ifmgd->associated = NULL;
  1415. netif_carrier_off(sdata->dev);
  1416. /*
  1417. * if we want to get out of ps before disassoc (why?) we have
  1418. * to do it before sending disassoc, as otherwise the null-packet
  1419. * won't be valid.
  1420. */
  1421. if (local->hw.conf.flags & IEEE80211_CONF_PS) {
  1422. local->hw.conf.flags &= ~IEEE80211_CONF_PS;
  1423. ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
  1424. }
  1425. local->ps_sdata = NULL;
  1426. /* disable per-vif ps */
  1427. ieee80211_recalc_ps_vif(sdata);
  1428. /* flush out any pending frame (e.g. DELBA) before deauth/disassoc */
  1429. if (tx)
  1430. ieee80211_flush_queues(local, sdata);
  1431. /* deauthenticate/disassociate now */
  1432. if (tx || frame_buf)
  1433. ieee80211_send_deauth_disassoc(sdata, ifmgd->bssid, stype,
  1434. reason, tx, frame_buf);
  1435. /* flush out frame */
  1436. if (tx)
  1437. ieee80211_flush_queues(local, sdata);
  1438. /* clear bssid only after building the needed mgmt frames */
  1439. memset(ifmgd->bssid, 0, ETH_ALEN);
  1440. /* remove AP and TDLS peers */
  1441. sta_info_flush_defer(sdata);
  1442. /* finally reset all BSS / config parameters */
  1443. changed |= ieee80211_reset_erp_info(sdata);
  1444. ieee80211_led_assoc(local, 0);
  1445. changed |= BSS_CHANGED_ASSOC;
  1446. sdata->vif.bss_conf.assoc = false;
  1447. ifmgd->p2p_noa_index = -1;
  1448. memset(&sdata->vif.bss_conf.p2p_noa_attr, 0,
  1449. sizeof(sdata->vif.bss_conf.p2p_noa_attr));
  1450. /* on the next assoc, re-program HT/VHT parameters */
  1451. memset(&ifmgd->ht_capa, 0, sizeof(ifmgd->ht_capa));
  1452. memset(&ifmgd->ht_capa_mask, 0, sizeof(ifmgd->ht_capa_mask));
  1453. memset(&ifmgd->vht_capa, 0, sizeof(ifmgd->vht_capa));
  1454. memset(&ifmgd->vht_capa_mask, 0, sizeof(ifmgd->vht_capa_mask));
  1455. sdata->ap_power_level = IEEE80211_UNSET_POWER_LEVEL;
  1456. del_timer_sync(&local->dynamic_ps_timer);
  1457. cancel_work_sync(&local->dynamic_ps_enable_work);
  1458. /* Disable ARP filtering */
  1459. if (sdata->vif.bss_conf.arp_addr_cnt)
  1460. changed |= BSS_CHANGED_ARP_FILTER;
  1461. sdata->vif.bss_conf.qos = false;
  1462. changed |= BSS_CHANGED_QOS;
  1463. /* The BSSID (not really interesting) and HT changed */
  1464. changed |= BSS_CHANGED_BSSID | BSS_CHANGED_HT;
  1465. ieee80211_bss_info_change_notify(sdata, changed);
  1466. /* disassociated - set to defaults now */
  1467. ieee80211_set_wmm_default(sdata, false);
  1468. del_timer_sync(&sdata->u.mgd.conn_mon_timer);
  1469. del_timer_sync(&sdata->u.mgd.bcn_mon_timer);
  1470. del_timer_sync(&sdata->u.mgd.timer);
  1471. del_timer_sync(&sdata->u.mgd.chswitch_timer);
  1472. sdata->vif.bss_conf.dtim_period = 0;
  1473. sdata->vif.bss_conf.beacon_rate = NULL;
  1474. ifmgd->have_beacon = false;
  1475. ifmgd->flags = 0;
  1476. ieee80211_vif_release_channel(sdata);
  1477. sdata->encrypt_headroom = IEEE80211_ENCRYPT_HEADROOM;
  1478. }
  1479. void ieee80211_sta_rx_notify(struct ieee80211_sub_if_data *sdata,
  1480. struct ieee80211_hdr *hdr)
  1481. {
  1482. /*
  1483. * We can postpone the mgd.timer whenever receiving unicast frames
  1484. * from AP because we know that the connection is working both ways
  1485. * at that time. But multicast frames (and hence also beacons) must
  1486. * be ignored here, because we need to trigger the timer during
  1487. * data idle periods for sending the periodic probe request to the
  1488. * AP we're connected to.
  1489. */
  1490. if (is_multicast_ether_addr(hdr->addr1))
  1491. return;
  1492. ieee80211_sta_reset_conn_monitor(sdata);
  1493. }
  1494. static void ieee80211_reset_ap_probe(struct ieee80211_sub_if_data *sdata)
  1495. {
  1496. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  1497. struct ieee80211_local *local = sdata->local;
  1498. mutex_lock(&local->mtx);
  1499. if (!(ifmgd->flags & IEEE80211_STA_CONNECTION_POLL))
  1500. goto out;
  1501. __ieee80211_stop_poll(sdata);
  1502. mutex_lock(&local->iflist_mtx);
  1503. ieee80211_recalc_ps(local, -1);
  1504. mutex_unlock(&local->iflist_mtx);
  1505. if (sdata->local->hw.flags & IEEE80211_HW_CONNECTION_MONITOR)
  1506. goto out;
  1507. /*
  1508. * We've received a probe response, but are not sure whether
  1509. * we have or will be receiving any beacons or data, so let's
  1510. * schedule the timers again, just in case.
  1511. */
  1512. ieee80211_sta_reset_beacon_monitor(sdata);
  1513. mod_timer(&ifmgd->conn_mon_timer,
  1514. round_jiffies_up(jiffies +
  1515. IEEE80211_CONNECTION_IDLE_TIME));
  1516. out:
  1517. mutex_unlock(&local->mtx);
  1518. }
  1519. void ieee80211_sta_tx_notify(struct ieee80211_sub_if_data *sdata,
  1520. struct ieee80211_hdr *hdr, bool ack)
  1521. {
  1522. if (!ieee80211_is_data(hdr->frame_control))
  1523. return;
  1524. if (ieee80211_is_nullfunc(hdr->frame_control) &&
  1525. sdata->u.mgd.probe_send_count > 0) {
  1526. if (ack)
  1527. ieee80211_sta_reset_conn_monitor(sdata);
  1528. else
  1529. sdata->u.mgd.nullfunc_failed = true;
  1530. ieee80211_queue_work(&sdata->local->hw, &sdata->work);
  1531. return;
  1532. }
  1533. if (ack)
  1534. ieee80211_sta_reset_conn_monitor(sdata);
  1535. }
  1536. static void ieee80211_mgd_probe_ap_send(struct ieee80211_sub_if_data *sdata)
  1537. {
  1538. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  1539. const u8 *ssid;
  1540. u8 *dst = ifmgd->associated->bssid;
  1541. u8 unicast_limit = max(1, max_probe_tries - 3);
  1542. /*
  1543. * Try sending broadcast probe requests for the last three
  1544. * probe requests after the first ones failed since some
  1545. * buggy APs only support broadcast probe requests.
  1546. */
  1547. if (ifmgd->probe_send_count >= unicast_limit)
  1548. dst = NULL;
  1549. /*
  1550. * When the hardware reports an accurate Tx ACK status, it's
  1551. * better to send a nullfunc frame instead of a probe request,
  1552. * as it will kick us off the AP quickly if we aren't associated
  1553. * anymore. The timeout will be reset if the frame is ACKed by
  1554. * the AP.
  1555. */
  1556. ifmgd->probe_send_count++;
  1557. if (sdata->local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS) {
  1558. ifmgd->nullfunc_failed = false;
  1559. ieee80211_send_nullfunc(sdata->local, sdata, 0);
  1560. } else {
  1561. int ssid_len;
  1562. rcu_read_lock();
  1563. ssid = ieee80211_bss_get_ie(ifmgd->associated, WLAN_EID_SSID);
  1564. if (WARN_ON_ONCE(ssid == NULL))
  1565. ssid_len = 0;
  1566. else
  1567. ssid_len = ssid[1];
  1568. ieee80211_send_probe_req(sdata, dst, ssid + 2, ssid_len, NULL,
  1569. 0, (u32) -1, true, 0,
  1570. ifmgd->associated->channel, false);
  1571. rcu_read_unlock();
  1572. }
  1573. ifmgd->probe_timeout = jiffies + msecs_to_jiffies(probe_wait_ms);
  1574. run_again(sdata, ifmgd->probe_timeout);
  1575. if (sdata->local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS)
  1576. ieee80211_flush_queues(sdata->local, sdata);
  1577. }
  1578. static void ieee80211_mgd_probe_ap(struct ieee80211_sub_if_data *sdata,
  1579. bool beacon)
  1580. {
  1581. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  1582. bool already = false;
  1583. if (!ieee80211_sdata_running(sdata))
  1584. return;
  1585. sdata_lock(sdata);
  1586. if (!ifmgd->associated)
  1587. goto out;
  1588. mutex_lock(&sdata->local->mtx);
  1589. if (sdata->local->tmp_channel || sdata->local->scanning) {
  1590. mutex_unlock(&sdata->local->mtx);
  1591. goto out;
  1592. }
  1593. if (beacon) {
  1594. mlme_dbg_ratelimited(sdata,
  1595. "detected beacon loss from AP (missed %d beacons) - probing\n",
  1596. beacon_loss_count);
  1597. ieee80211_cqm_rssi_notify(&sdata->vif,
  1598. NL80211_CQM_RSSI_BEACON_LOSS_EVENT,
  1599. GFP_KERNEL);
  1600. }
  1601. /*
  1602. * The driver/our work has already reported this event or the
  1603. * connection monitoring has kicked in and we have already sent
  1604. * a probe request. Or maybe the AP died and the driver keeps
  1605. * reporting until we disassociate...
  1606. *
  1607. * In either case we have to ignore the current call to this
  1608. * function (except for setting the correct probe reason bit)
  1609. * because otherwise we would reset the timer every time and
  1610. * never check whether we received a probe response!
  1611. */
  1612. if (ifmgd->flags & IEEE80211_STA_CONNECTION_POLL)
  1613. already = true;
  1614. ifmgd->flags |= IEEE80211_STA_CONNECTION_POLL;
  1615. mutex_unlock(&sdata->local->mtx);
  1616. if (already)
  1617. goto out;
  1618. mutex_lock(&sdata->local->iflist_mtx);
  1619. ieee80211_recalc_ps(sdata->local, -1);
  1620. mutex_unlock(&sdata->local->iflist_mtx);
  1621. ifmgd->probe_send_count = 0;
  1622. ieee80211_mgd_probe_ap_send(sdata);
  1623. out:
  1624. sdata_unlock(sdata);
  1625. }
  1626. struct sk_buff *ieee80211_ap_probereq_get(struct ieee80211_hw *hw,
  1627. struct ieee80211_vif *vif)
  1628. {
  1629. struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
  1630. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  1631. struct cfg80211_bss *cbss;
  1632. struct sk_buff *skb;
  1633. const u8 *ssid;
  1634. int ssid_len;
  1635. if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_STATION))
  1636. return NULL;
  1637. sdata_assert_lock(sdata);
  1638. if (ifmgd->associated)
  1639. cbss = ifmgd->associated;
  1640. else if (ifmgd->auth_data)
  1641. cbss = ifmgd->auth_data->bss;
  1642. else if (ifmgd->assoc_data)
  1643. cbss = ifmgd->assoc_data->bss;
  1644. else
  1645. return NULL;
  1646. rcu_read_lock();
  1647. ssid = ieee80211_bss_get_ie(cbss, WLAN_EID_SSID);
  1648. if (WARN_ON_ONCE(ssid == NULL))
  1649. ssid_len = 0;
  1650. else
  1651. ssid_len = ssid[1];
  1652. skb = ieee80211_build_probe_req(sdata, cbss->bssid,
  1653. (u32) -1, cbss->channel,
  1654. ssid + 2, ssid_len,
  1655. NULL, 0, true);
  1656. rcu_read_unlock();
  1657. return skb;
  1658. }
  1659. EXPORT_SYMBOL(ieee80211_ap_probereq_get);
  1660. static void __ieee80211_disconnect(struct ieee80211_sub_if_data *sdata)
  1661. {
  1662. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  1663. u8 frame_buf[IEEE80211_DEAUTH_FRAME_LEN];
  1664. sdata_lock(sdata);
  1665. if (!ifmgd->associated) {
  1666. sdata_unlock(sdata);
  1667. return;
  1668. }
  1669. ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DEAUTH,
  1670. WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY,
  1671. true, frame_buf);
  1672. ifmgd->flags &= ~IEEE80211_STA_CSA_RECEIVED;
  1673. sdata->vif.csa_active = false;
  1674. ieee80211_wake_queues_by_reason(&sdata->local->hw,
  1675. IEEE80211_MAX_QUEUE_MAP,
  1676. IEEE80211_QUEUE_STOP_REASON_CSA);
  1677. cfg80211_tx_mlme_mgmt(sdata->dev, frame_buf,
  1678. IEEE80211_DEAUTH_FRAME_LEN);
  1679. sdata_unlock(sdata);
  1680. }
  1681. static void ieee80211_beacon_connection_loss_work(struct work_struct *work)
  1682. {
  1683. struct ieee80211_sub_if_data *sdata =
  1684. container_of(work, struct ieee80211_sub_if_data,
  1685. u.mgd.beacon_connection_loss_work);
  1686. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  1687. struct sta_info *sta;
  1688. if (ifmgd->associated) {
  1689. rcu_read_lock();
  1690. sta = sta_info_get(sdata, ifmgd->bssid);
  1691. if (sta)
  1692. sta->beacon_loss_count++;
  1693. rcu_read_unlock();
  1694. }
  1695. if (ifmgd->connection_loss) {
  1696. sdata_info(sdata, "Connection to AP %pM lost\n",
  1697. ifmgd->bssid);
  1698. __ieee80211_disconnect(sdata);
  1699. } else {
  1700. ieee80211_mgd_probe_ap(sdata, true);
  1701. }
  1702. }
  1703. static void ieee80211_csa_connection_drop_work(struct work_struct *work)
  1704. {
  1705. struct ieee80211_sub_if_data *sdata =
  1706. container_of(work, struct ieee80211_sub_if_data,
  1707. u.mgd.csa_connection_drop_work);
  1708. __ieee80211_disconnect(sdata);
  1709. }
  1710. void ieee80211_beacon_loss(struct ieee80211_vif *vif)
  1711. {
  1712. struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
  1713. struct ieee80211_hw *hw = &sdata->local->hw;
  1714. trace_api_beacon_loss(sdata);
  1715. sdata->u.mgd.connection_loss = false;
  1716. ieee80211_queue_work(hw, &sdata->u.mgd.beacon_connection_loss_work);
  1717. }
  1718. EXPORT_SYMBOL(ieee80211_beacon_loss);
  1719. void ieee80211_connection_loss(struct ieee80211_vif *vif)
  1720. {
  1721. struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
  1722. struct ieee80211_hw *hw = &sdata->local->hw;
  1723. trace_api_connection_loss(sdata);
  1724. sdata->u.mgd.connection_loss = true;
  1725. ieee80211_queue_work(hw, &sdata->u.mgd.beacon_connection_loss_work);
  1726. }
  1727. EXPORT_SYMBOL(ieee80211_connection_loss);
  1728. static void ieee80211_destroy_auth_data(struct ieee80211_sub_if_data *sdata,
  1729. bool assoc)
  1730. {
  1731. struct ieee80211_mgd_auth_data *auth_data = sdata->u.mgd.auth_data;
  1732. sdata_assert_lock(sdata);
  1733. if (!assoc) {
  1734. sta_info_destroy_addr(sdata, auth_data->bss->bssid);
  1735. memset(sdata->u.mgd.bssid, 0, ETH_ALEN);
  1736. ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BSSID);
  1737. sdata->u.mgd.flags = 0;
  1738. ieee80211_vif_release_channel(sdata);
  1739. }
  1740. cfg80211_put_bss(sdata->local->hw.wiphy, auth_data->bss);
  1741. kfree(auth_data);
  1742. sdata->u.mgd.auth_data = NULL;
  1743. }
  1744. static void ieee80211_auth_challenge(struct ieee80211_sub_if_data *sdata,
  1745. struct ieee80211_mgmt *mgmt, size_t len)
  1746. {
  1747. struct ieee80211_local *local = sdata->local;
  1748. struct ieee80211_mgd_auth_data *auth_data = sdata->u.mgd.auth_data;
  1749. u8 *pos;
  1750. struct ieee802_11_elems elems;
  1751. u32 tx_flags = 0;
  1752. pos = mgmt->u.auth.variable;
  1753. ieee802_11_parse_elems(pos, len - (pos - (u8 *) mgmt), false, &elems);
  1754. if (!elems.challenge)
  1755. return;
  1756. auth_data->expected_transaction = 4;
  1757. drv_mgd_prepare_tx(sdata->local, sdata);
  1758. if (local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS)
  1759. tx_flags = IEEE80211_TX_CTL_REQ_TX_STATUS |
  1760. IEEE80211_TX_INTFL_MLME_CONN_TX;
  1761. ieee80211_send_auth(sdata, 3, auth_data->algorithm, 0,
  1762. elems.challenge - 2, elems.challenge_len + 2,
  1763. auth_data->bss->bssid, auth_data->bss->bssid,
  1764. auth_data->key, auth_data->key_len,
  1765. auth_data->key_idx, tx_flags);
  1766. }
  1767. static void ieee80211_rx_mgmt_auth(struct ieee80211_sub_if_data *sdata,
  1768. struct ieee80211_mgmt *mgmt, size_t len)
  1769. {
  1770. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  1771. u8 bssid[ETH_ALEN];
  1772. u16 auth_alg, auth_transaction, status_code;
  1773. struct sta_info *sta;
  1774. sdata_assert_lock(sdata);
  1775. if (len < 24 + 6)
  1776. return;
  1777. if (!ifmgd->auth_data || ifmgd->auth_data->done)
  1778. return;
  1779. memcpy(bssid, ifmgd->auth_data->bss->bssid, ETH_ALEN);
  1780. if (!ether_addr_equal(bssid, mgmt->bssid))
  1781. return;
  1782. auth_alg = le16_to_cpu(mgmt->u.auth.auth_alg);
  1783. auth_transaction = le16_to_cpu(mgmt->u.auth.auth_transaction);
  1784. status_code = le16_to_cpu(mgmt->u.auth.status_code);
  1785. if (auth_alg != ifmgd->auth_data->algorithm ||
  1786. auth_transaction != ifmgd->auth_data->expected_transaction) {
  1787. sdata_info(sdata, "%pM unexpected authentication state: alg %d (expected %d) transact %d (expected %d)\n",
  1788. mgmt->sa, auth_alg, ifmgd->auth_data->algorithm,
  1789. auth_transaction,
  1790. ifmgd->auth_data->expected_transaction);
  1791. return;
  1792. }
  1793. if (status_code != WLAN_STATUS_SUCCESS) {
  1794. sdata_info(sdata, "%pM denied authentication (status %d)\n",
  1795. mgmt->sa, status_code);
  1796. ieee80211_destroy_auth_data(sdata, false);
  1797. cfg80211_rx_mlme_mgmt(sdata->dev, (u8 *)mgmt, len);
  1798. return;
  1799. }
  1800. switch (ifmgd->auth_data->algorithm) {
  1801. case WLAN_AUTH_OPEN:
  1802. case WLAN_AUTH_LEAP:
  1803. case WLAN_AUTH_FT:
  1804. case WLAN_AUTH_SAE:
  1805. break;
  1806. case WLAN_AUTH_SHARED_KEY:
  1807. if (ifmgd->auth_data->expected_transaction != 4) {
  1808. ieee80211_auth_challenge(sdata, mgmt, len);
  1809. /* need another frame */
  1810. return;
  1811. }
  1812. break;
  1813. default:
  1814. WARN_ONCE(1, "invalid auth alg %d",
  1815. ifmgd->auth_data->algorithm);
  1816. return;
  1817. }
  1818. sdata_info(sdata, "authenticated\n");
  1819. ifmgd->auth_data->done = true;
  1820. ifmgd->auth_data->timeout = jiffies + IEEE80211_AUTH_WAIT_ASSOC;
  1821. ifmgd->auth_data->timeout_started = true;
  1822. run_again(sdata, ifmgd->auth_data->timeout);
  1823. if (ifmgd->auth_data->algorithm == WLAN_AUTH_SAE &&
  1824. ifmgd->auth_data->expected_transaction != 2) {
  1825. /*
  1826. * Report auth frame to user space for processing since another
  1827. * round of Authentication frames is still needed.
  1828. */
  1829. cfg80211_rx_mlme_mgmt(sdata->dev, (u8 *)mgmt, len);
  1830. return;
  1831. }
  1832. /* move station state to auth */
  1833. mutex_lock(&sdata->local->sta_mtx);
  1834. sta = sta_info_get(sdata, bssid);
  1835. if (!sta) {
  1836. WARN_ONCE(1, "%s: STA %pM not found", sdata->name, bssid);
  1837. goto out_err;
  1838. }
  1839. if (sta_info_move_state(sta, IEEE80211_STA_AUTH)) {
  1840. sdata_info(sdata, "failed moving %pM to auth\n", bssid);
  1841. goto out_err;
  1842. }
  1843. mutex_unlock(&sdata->local->sta_mtx);
  1844. cfg80211_rx_mlme_mgmt(sdata->dev, (u8 *)mgmt, len);
  1845. return;
  1846. out_err:
  1847. mutex_unlock(&sdata->local->sta_mtx);
  1848. /* ignore frame -- wait for timeout */
  1849. }
  1850. static void ieee80211_rx_mgmt_deauth(struct ieee80211_sub_if_data *sdata,
  1851. struct ieee80211_mgmt *mgmt, size_t len)
  1852. {
  1853. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  1854. const u8 *bssid = NULL;
  1855. u16 reason_code;
  1856. sdata_assert_lock(sdata);
  1857. if (len < 24 + 2)
  1858. return;
  1859. if (!ifmgd->associated ||
  1860. !ether_addr_equal(mgmt->bssid, ifmgd->associated->bssid))
  1861. return;
  1862. bssid = ifmgd->associated->bssid;
  1863. reason_code = le16_to_cpu(mgmt->u.deauth.reason_code);
  1864. sdata_info(sdata, "deauthenticated from %pM (Reason: %u)\n",
  1865. bssid, reason_code);
  1866. ieee80211_set_disassoc(sdata, 0, 0, false, NULL);
  1867. cfg80211_rx_mlme_mgmt(sdata->dev, (u8 *)mgmt, len);
  1868. }
  1869. static void ieee80211_rx_mgmt_disassoc(struct ieee80211_sub_if_data *sdata,
  1870. struct ieee80211_mgmt *mgmt, size_t len)
  1871. {
  1872. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  1873. u16 reason_code;
  1874. sdata_assert_lock(sdata);
  1875. if (len < 24 + 2)
  1876. return;
  1877. if (!ifmgd->associated ||
  1878. !ether_addr_equal(mgmt->bssid, ifmgd->associated->bssid))
  1879. return;
  1880. reason_code = le16_to_cpu(mgmt->u.disassoc.reason_code);
  1881. sdata_info(sdata, "disassociated from %pM (Reason: %u)\n",
  1882. mgmt->sa, reason_code);
  1883. ieee80211_set_disassoc(sdata, 0, 0, false, NULL);
  1884. cfg80211_rx_mlme_mgmt(sdata->dev, (u8 *)mgmt, len);
  1885. }
  1886. static void ieee80211_get_rates(struct ieee80211_supported_band *sband,
  1887. u8 *supp_rates, unsigned int supp_rates_len,
  1888. u32 *rates, u32 *basic_rates,
  1889. bool *have_higher_than_11mbit,
  1890. int *min_rate, int *min_rate_index,
  1891. int shift, u32 rate_flags)
  1892. {
  1893. int i, j;
  1894. for (i = 0; i < supp_rates_len; i++) {
  1895. int rate = supp_rates[i] & 0x7f;
  1896. bool is_basic = !!(supp_rates[i] & 0x80);
  1897. if ((rate * 5 * (1 << shift)) > 110)
  1898. *have_higher_than_11mbit = true;
  1899. /*
  1900. * BSS_MEMBERSHIP_SELECTOR_HT_PHY is defined in 802.11n-2009
  1901. * 7.3.2.2 as a magic value instead of a rate. Hence, skip it.
  1902. *
  1903. * Note: Even through the membership selector and the basic
  1904. * rate flag share the same bit, they are not exactly
  1905. * the same.
  1906. */
  1907. if (!!(supp_rates[i] & 0x80) &&
  1908. (supp_rates[i] & 0x7f) == BSS_MEMBERSHIP_SELECTOR_HT_PHY)
  1909. continue;
  1910. for (j = 0; j < sband->n_bitrates; j++) {
  1911. struct ieee80211_rate *br;
  1912. int brate;
  1913. br = &sband->bitrates[j];
  1914. if ((rate_flags & br->flags) != rate_flags)
  1915. continue;
  1916. brate = DIV_ROUND_UP(br->bitrate, (1 << shift) * 5);
  1917. if (brate == rate) {
  1918. *rates |= BIT(j);
  1919. if (is_basic)
  1920. *basic_rates |= BIT(j);
  1921. if ((rate * 5) < *min_rate) {
  1922. *min_rate = rate * 5;
  1923. *min_rate_index = j;
  1924. }
  1925. break;
  1926. }
  1927. }
  1928. }
  1929. }
  1930. static void ieee80211_destroy_assoc_data(struct ieee80211_sub_if_data *sdata,
  1931. bool assoc)
  1932. {
  1933. struct ieee80211_mgd_assoc_data *assoc_data = sdata->u.mgd.assoc_data;
  1934. sdata_assert_lock(sdata);
  1935. if (!assoc) {
  1936. sta_info_destroy_addr(sdata, assoc_data->bss->bssid);
  1937. memset(sdata->u.mgd.bssid, 0, ETH_ALEN);
  1938. ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BSSID);
  1939. sdata->u.mgd.flags = 0;
  1940. ieee80211_vif_release_channel(sdata);
  1941. }
  1942. kfree(assoc_data);
  1943. sdata->u.mgd.assoc_data = NULL;
  1944. }
  1945. static bool ieee80211_assoc_success(struct ieee80211_sub_if_data *sdata,
  1946. struct cfg80211_bss *cbss,
  1947. struct ieee80211_mgmt *mgmt, size_t len)
  1948. {
  1949. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  1950. struct ieee80211_local *local = sdata->local;
  1951. struct ieee80211_supported_band *sband;
  1952. struct sta_info *sta;
  1953. u8 *pos;
  1954. u16 capab_info, aid;
  1955. struct ieee802_11_elems elems;
  1956. struct ieee80211_bss_conf *bss_conf = &sdata->vif.bss_conf;
  1957. const struct cfg80211_bss_ies *bss_ies = NULL;
  1958. struct ieee80211_mgd_assoc_data *assoc_data = ifmgd->assoc_data;
  1959. u32 changed = 0;
  1960. int err;
  1961. bool ret;
  1962. /* AssocResp and ReassocResp have identical structure */
  1963. aid = le16_to_cpu(mgmt->u.assoc_resp.aid);
  1964. capab_info = le16_to_cpu(mgmt->u.assoc_resp.capab_info);
  1965. if ((aid & (BIT(15) | BIT(14))) != (BIT(15) | BIT(14)))
  1966. sdata_info(sdata, "invalid AID value 0x%x; bits 15:14 not set\n",
  1967. aid);
  1968. aid &= ~(BIT(15) | BIT(14));
  1969. ifmgd->broken_ap = false;
  1970. if (aid == 0 || aid > IEEE80211_MAX_AID) {
  1971. sdata_info(sdata, "invalid AID value %d (out of range), turn off PS\n",
  1972. aid);
  1973. aid = 0;
  1974. ifmgd->broken_ap = true;
  1975. }
  1976. pos = mgmt->u.assoc_resp.variable;
  1977. ieee802_11_parse_elems(pos, len - (pos - (u8 *) mgmt), false, &elems);
  1978. if (!elems.supp_rates) {
  1979. sdata_info(sdata, "no SuppRates element in AssocResp\n");
  1980. return false;
  1981. }
  1982. ifmgd->aid = aid;
  1983. /*
  1984. * Some APs are erroneously not including some information in their
  1985. * (re)association response frames. Try to recover by using the data
  1986. * from the beacon or probe response. This seems to afflict mobile
  1987. * 2G/3G/4G wifi routers, reported models include the "Onda PN51T",
  1988. * "Vodafone PocketWiFi 2", "ZTE MF60" and a similar T-Mobile device.
  1989. */
  1990. if ((assoc_data->wmm && !elems.wmm_param) ||
  1991. (!(ifmgd->flags & IEEE80211_STA_DISABLE_HT) &&
  1992. (!elems.ht_cap_elem || !elems.ht_operation)) ||
  1993. (!(ifmgd->flags & IEEE80211_STA_DISABLE_VHT) &&
  1994. (!elems.vht_cap_elem || !elems.vht_operation))) {
  1995. const struct cfg80211_bss_ies *ies;
  1996. struct ieee802_11_elems bss_elems;
  1997. rcu_read_lock();
  1998. ies = rcu_dereference(cbss->ies);
  1999. if (ies)
  2000. bss_ies = kmemdup(ies, sizeof(*ies) + ies->len,
  2001. GFP_ATOMIC);
  2002. rcu_read_unlock();
  2003. if (!bss_ies)
  2004. return false;
  2005. ieee802_11_parse_elems(bss_ies->data, bss_ies->len,
  2006. false, &bss_elems);
  2007. if (assoc_data->wmm &&
  2008. !elems.wmm_param && bss_elems.wmm_param) {
  2009. elems.wmm_param = bss_elems.wmm_param;
  2010. sdata_info(sdata,
  2011. "AP bug: WMM param missing from AssocResp\n");
  2012. }
  2013. /*
  2014. * Also check if we requested HT/VHT, otherwise the AP doesn't
  2015. * have to include the IEs in the (re)association response.
  2016. */
  2017. if (!elems.ht_cap_elem && bss_elems.ht_cap_elem &&
  2018. !(ifmgd->flags & IEEE80211_STA_DISABLE_HT)) {
  2019. elems.ht_cap_elem = bss_elems.ht_cap_elem;
  2020. sdata_info(sdata,
  2021. "AP bug: HT capability missing from AssocResp\n");
  2022. }
  2023. if (!elems.ht_operation && bss_elems.ht_operation &&
  2024. !(ifmgd->flags & IEEE80211_STA_DISABLE_HT)) {
  2025. elems.ht_operation = bss_elems.ht_operation;
  2026. sdata_info(sdata,
  2027. "AP bug: HT operation missing from AssocResp\n");
  2028. }
  2029. if (!elems.vht_cap_elem && bss_elems.vht_cap_elem &&
  2030. !(ifmgd->flags & IEEE80211_STA_DISABLE_VHT)) {
  2031. elems.vht_cap_elem = bss_elems.vht_cap_elem;
  2032. sdata_info(sdata,
  2033. "AP bug: VHT capa missing from AssocResp\n");
  2034. }
  2035. if (!elems.vht_operation && bss_elems.vht_operation &&
  2036. !(ifmgd->flags & IEEE80211_STA_DISABLE_VHT)) {
  2037. elems.vht_operation = bss_elems.vht_operation;
  2038. sdata_info(sdata,
  2039. "AP bug: VHT operation missing from AssocResp\n");
  2040. }
  2041. }
  2042. /*
  2043. * We previously checked these in the beacon/probe response, so
  2044. * they should be present here. This is just a safety net.
  2045. */
  2046. if (!(ifmgd->flags & IEEE80211_STA_DISABLE_HT) &&
  2047. (!elems.wmm_param || !elems.ht_cap_elem || !elems.ht_operation)) {
  2048. sdata_info(sdata,
  2049. "HT AP is missing WMM params or HT capability/operation\n");
  2050. ret = false;
  2051. goto out;
  2052. }
  2053. if (!(ifmgd->flags & IEEE80211_STA_DISABLE_VHT) &&
  2054. (!elems.vht_cap_elem || !elems.vht_operation)) {
  2055. sdata_info(sdata,
  2056. "VHT AP is missing VHT capability/operation\n");
  2057. ret = false;
  2058. goto out;
  2059. }
  2060. mutex_lock(&sdata->local->sta_mtx);
  2061. /*
  2062. * station info was already allocated and inserted before
  2063. * the association and should be available to us
  2064. */
  2065. sta = sta_info_get(sdata, cbss->bssid);
  2066. if (WARN_ON(!sta)) {
  2067. mutex_unlock(&sdata->local->sta_mtx);
  2068. ret = false;
  2069. goto out;
  2070. }
  2071. sband = local->hw.wiphy->bands[ieee80211_get_sdata_band(sdata)];
  2072. /* Set up internal HT/VHT capabilities */
  2073. if (elems.ht_cap_elem && !(ifmgd->flags & IEEE80211_STA_DISABLE_HT))
  2074. ieee80211_ht_cap_ie_to_sta_ht_cap(sdata, sband,
  2075. elems.ht_cap_elem, sta);
  2076. if (elems.vht_cap_elem && !(ifmgd->flags & IEEE80211_STA_DISABLE_VHT))
  2077. ieee80211_vht_cap_ie_to_sta_vht_cap(sdata, sband,
  2078. elems.vht_cap_elem, sta);
  2079. /*
  2080. * Some APs, e.g. Netgear WNDR3700, report invalid HT operation data
  2081. * in their association response, so ignore that data for our own
  2082. * configuration. If it changed since the last beacon, we'll get the
  2083. * next beacon and update then.
  2084. */
  2085. /*
  2086. * If an operating mode notification IE is present, override the
  2087. * NSS calculation (that would be done in rate_control_rate_init())
  2088. * and use the # of streams from that element.
  2089. */
  2090. if (elems.opmode_notif &&
  2091. !(*elems.opmode_notif & IEEE80211_OPMODE_NOTIF_RX_NSS_TYPE_BF)) {
  2092. u8 nss;
  2093. nss = *elems.opmode_notif & IEEE80211_OPMODE_NOTIF_RX_NSS_MASK;
  2094. nss >>= IEEE80211_OPMODE_NOTIF_RX_NSS_SHIFT;
  2095. nss += 1;
  2096. sta->sta.rx_nss = nss;
  2097. }
  2098. rate_control_rate_init(sta);
  2099. if (ifmgd->flags & IEEE80211_STA_MFP_ENABLED)
  2100. set_sta_flag(sta, WLAN_STA_MFP);
  2101. if (elems.wmm_param)
  2102. set_sta_flag(sta, WLAN_STA_WME);
  2103. err = sta_info_move_state(sta, IEEE80211_STA_ASSOC);
  2104. if (!err && !(ifmgd->flags & IEEE80211_STA_CONTROL_PORT))
  2105. err = sta_info_move_state(sta, IEEE80211_STA_AUTHORIZED);
  2106. if (err) {
  2107. sdata_info(sdata,
  2108. "failed to move station %pM to desired state\n",
  2109. sta->sta.addr);
  2110. WARN_ON(__sta_info_destroy(sta));
  2111. mutex_unlock(&sdata->local->sta_mtx);
  2112. ret = false;
  2113. goto out;
  2114. }
  2115. mutex_unlock(&sdata->local->sta_mtx);
  2116. /*
  2117. * Always handle WMM once after association regardless
  2118. * of the first value the AP uses. Setting -1 here has
  2119. * that effect because the AP values is an unsigned
  2120. * 4-bit value.
  2121. */
  2122. ifmgd->wmm_last_param_set = -1;
  2123. if (!(ifmgd->flags & IEEE80211_STA_DISABLE_WMM) && elems.wmm_param)
  2124. ieee80211_sta_wmm_params(local, sdata, elems.wmm_param,
  2125. elems.wmm_param_len);
  2126. else
  2127. ieee80211_set_wmm_default(sdata, false);
  2128. changed |= BSS_CHANGED_QOS;
  2129. /* set AID and assoc capability,
  2130. * ieee80211_set_associated() will tell the driver */
  2131. bss_conf->aid = aid;
  2132. bss_conf->assoc_capability = capab_info;
  2133. ieee80211_set_associated(sdata, cbss, changed);
  2134. /*
  2135. * If we're using 4-addr mode, let the AP know that we're
  2136. * doing so, so that it can create the STA VLAN on its side
  2137. */
  2138. if (ifmgd->use_4addr)
  2139. ieee80211_send_4addr_nullfunc(local, sdata);
  2140. /*
  2141. * Start timer to probe the connection to the AP now.
  2142. * Also start the timer that will detect beacon loss.
  2143. */
  2144. ieee80211_sta_rx_notify(sdata, (struct ieee80211_hdr *)mgmt);
  2145. ieee80211_sta_reset_beacon_monitor(sdata);
  2146. ret = true;
  2147. out:
  2148. kfree(bss_ies);
  2149. return ret;
  2150. }
  2151. static void ieee80211_rx_mgmt_assoc_resp(struct ieee80211_sub_if_data *sdata,
  2152. struct ieee80211_mgmt *mgmt,
  2153. size_t len)
  2154. {
  2155. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  2156. struct ieee80211_mgd_assoc_data *assoc_data = ifmgd->assoc_data;
  2157. u16 capab_info, status_code, aid;
  2158. struct ieee802_11_elems elems;
  2159. u8 *pos;
  2160. bool reassoc;
  2161. struct cfg80211_bss *bss;
  2162. sdata_assert_lock(sdata);
  2163. if (!assoc_data)
  2164. return;
  2165. if (!ether_addr_equal(assoc_data->bss->bssid, mgmt->bssid))
  2166. return;
  2167. /*
  2168. * AssocResp and ReassocResp have identical structure, so process both
  2169. * of them in this function.
  2170. */
  2171. if (len < 24 + 6)
  2172. return;
  2173. reassoc = ieee80211_is_reassoc_req(mgmt->frame_control);
  2174. capab_info = le16_to_cpu(mgmt->u.assoc_resp.capab_info);
  2175. status_code = le16_to_cpu(mgmt->u.assoc_resp.status_code);
  2176. aid = le16_to_cpu(mgmt->u.assoc_resp.aid);
  2177. sdata_info(sdata,
  2178. "RX %sssocResp from %pM (capab=0x%x status=%d aid=%d)\n",
  2179. reassoc ? "Rea" : "A", mgmt->sa,
  2180. capab_info, status_code, (u16)(aid & ~(BIT(15) | BIT(14))));
  2181. pos = mgmt->u.assoc_resp.variable;
  2182. ieee802_11_parse_elems(pos, len - (pos - (u8 *) mgmt), false, &elems);
  2183. if (status_code == WLAN_STATUS_ASSOC_REJECTED_TEMPORARILY &&
  2184. elems.timeout_int &&
  2185. elems.timeout_int->type == WLAN_TIMEOUT_ASSOC_COMEBACK) {
  2186. u32 tu, ms;
  2187. tu = le32_to_cpu(elems.timeout_int->value);
  2188. ms = tu * 1024 / 1000;
  2189. sdata_info(sdata,
  2190. "%pM rejected association temporarily; comeback duration %u TU (%u ms)\n",
  2191. mgmt->sa, tu, ms);
  2192. assoc_data->timeout = jiffies + msecs_to_jiffies(ms);
  2193. assoc_data->timeout_started = true;
  2194. if (ms > IEEE80211_ASSOC_TIMEOUT)
  2195. run_again(sdata, assoc_data->timeout);
  2196. return;
  2197. }
  2198. bss = assoc_data->bss;
  2199. if (status_code != WLAN_STATUS_SUCCESS) {
  2200. sdata_info(sdata, "%pM denied association (code=%d)\n",
  2201. mgmt->sa, status_code);
  2202. ieee80211_destroy_assoc_data(sdata, false);
  2203. } else {
  2204. if (!ieee80211_assoc_success(sdata, bss, mgmt, len)) {
  2205. /* oops -- internal error -- send timeout for now */
  2206. ieee80211_destroy_assoc_data(sdata, false);
  2207. cfg80211_assoc_timeout(sdata->dev, bss);
  2208. return;
  2209. }
  2210. sdata_info(sdata, "associated\n");
  2211. /*
  2212. * destroy assoc_data afterwards, as otherwise an idle
  2213. * recalc after assoc_data is NULL but before associated
  2214. * is set can cause the interface to go idle
  2215. */
  2216. ieee80211_destroy_assoc_data(sdata, true);
  2217. }
  2218. cfg80211_rx_assoc_resp(sdata->dev, bss, (u8 *)mgmt, len);
  2219. }
  2220. static void ieee80211_rx_bss_info(struct ieee80211_sub_if_data *sdata,
  2221. struct ieee80211_mgmt *mgmt, size_t len,
  2222. struct ieee80211_rx_status *rx_status,
  2223. struct ieee802_11_elems *elems)
  2224. {
  2225. struct ieee80211_local *local = sdata->local;
  2226. int freq;
  2227. struct ieee80211_bss *bss;
  2228. struct ieee80211_channel *channel;
  2229. sdata_assert_lock(sdata);
  2230. if (elems->ds_params)
  2231. freq = ieee80211_channel_to_frequency(elems->ds_params[0],
  2232. rx_status->band);
  2233. else
  2234. freq = rx_status->freq;
  2235. channel = ieee80211_get_channel(local->hw.wiphy, freq);
  2236. if (!channel || channel->flags & IEEE80211_CHAN_DISABLED)
  2237. return;
  2238. bss = ieee80211_bss_info_update(local, rx_status, mgmt, len, elems,
  2239. channel);
  2240. if (bss) {
  2241. ieee80211_rx_bss_put(local, bss);
  2242. sdata->vif.bss_conf.beacon_rate = bss->beacon_rate;
  2243. }
  2244. }
  2245. static void ieee80211_rx_mgmt_probe_resp(struct ieee80211_sub_if_data *sdata,
  2246. struct sk_buff *skb)
  2247. {
  2248. struct ieee80211_mgmt *mgmt = (void *)skb->data;
  2249. struct ieee80211_if_managed *ifmgd;
  2250. struct ieee80211_rx_status *rx_status = (void *) skb->cb;
  2251. size_t baselen, len = skb->len;
  2252. struct ieee802_11_elems elems;
  2253. ifmgd = &sdata->u.mgd;
  2254. sdata_assert_lock(sdata);
  2255. if (!ether_addr_equal(mgmt->da, sdata->vif.addr))
  2256. return; /* ignore ProbeResp to foreign address */
  2257. baselen = (u8 *) mgmt->u.probe_resp.variable - (u8 *) mgmt;
  2258. if (baselen > len)
  2259. return;
  2260. ieee802_11_parse_elems(mgmt->u.probe_resp.variable, len - baselen,
  2261. false, &elems);
  2262. ieee80211_rx_bss_info(sdata, mgmt, len, rx_status, &elems);
  2263. if (ifmgd->associated &&
  2264. ether_addr_equal(mgmt->bssid, ifmgd->associated->bssid))
  2265. ieee80211_reset_ap_probe(sdata);
  2266. if (ifmgd->auth_data && !ifmgd->auth_data->bss->proberesp_ies &&
  2267. ether_addr_equal(mgmt->bssid, ifmgd->auth_data->bss->bssid)) {
  2268. /* got probe response, continue with auth */
  2269. sdata_info(sdata, "direct probe responded\n");
  2270. ifmgd->auth_data->tries = 0;
  2271. ifmgd->auth_data->timeout = jiffies;
  2272. ifmgd->auth_data->timeout_started = true;
  2273. run_again(sdata, ifmgd->auth_data->timeout);
  2274. }
  2275. }
  2276. /*
  2277. * This is the canonical list of information elements we care about,
  2278. * the filter code also gives us all changes to the Microsoft OUI
  2279. * (00:50:F2) vendor IE which is used for WMM which we need to track.
  2280. *
  2281. * We implement beacon filtering in software since that means we can
  2282. * avoid processing the frame here and in cfg80211, and userspace
  2283. * will not be able to tell whether the hardware supports it or not.
  2284. *
  2285. * XXX: This list needs to be dynamic -- userspace needs to be able to
  2286. * add items it requires. It also needs to be able to tell us to
  2287. * look out for other vendor IEs.
  2288. */
  2289. static const u64 care_about_ies =
  2290. (1ULL << WLAN_EID_COUNTRY) |
  2291. (1ULL << WLAN_EID_ERP_INFO) |
  2292. (1ULL << WLAN_EID_CHANNEL_SWITCH) |
  2293. (1ULL << WLAN_EID_PWR_CONSTRAINT) |
  2294. (1ULL << WLAN_EID_HT_CAPABILITY) |
  2295. (1ULL << WLAN_EID_HT_OPERATION);
  2296. static void ieee80211_rx_mgmt_beacon(struct ieee80211_sub_if_data *sdata,
  2297. struct ieee80211_mgmt *mgmt, size_t len,
  2298. struct ieee80211_rx_status *rx_status)
  2299. {
  2300. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  2301. struct ieee80211_bss_conf *bss_conf = &sdata->vif.bss_conf;
  2302. size_t baselen;
  2303. struct ieee802_11_elems elems;
  2304. struct ieee80211_local *local = sdata->local;
  2305. struct ieee80211_chanctx_conf *chanctx_conf;
  2306. struct ieee80211_channel *chan;
  2307. struct sta_info *sta;
  2308. u32 changed = 0;
  2309. bool erp_valid;
  2310. u8 erp_value = 0;
  2311. u32 ncrc;
  2312. u8 *bssid;
  2313. u8 deauth_buf[IEEE80211_DEAUTH_FRAME_LEN];
  2314. sdata_assert_lock(sdata);
  2315. /* Process beacon from the current BSS */
  2316. baselen = (u8 *) mgmt->u.beacon.variable - (u8 *) mgmt;
  2317. if (baselen > len)
  2318. return;
  2319. rcu_read_lock();
  2320. chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
  2321. if (!chanctx_conf) {
  2322. rcu_read_unlock();
  2323. return;
  2324. }
  2325. if (rx_status->freq != chanctx_conf->def.chan->center_freq) {
  2326. rcu_read_unlock();
  2327. return;
  2328. }
  2329. chan = chanctx_conf->def.chan;
  2330. rcu_read_unlock();
  2331. if (ifmgd->assoc_data && ifmgd->assoc_data->need_beacon &&
  2332. ether_addr_equal(mgmt->bssid, ifmgd->assoc_data->bss->bssid)) {
  2333. ieee802_11_parse_elems(mgmt->u.beacon.variable,
  2334. len - baselen, false, &elems);
  2335. ieee80211_rx_bss_info(sdata, mgmt, len, rx_status, &elems);
  2336. if (elems.tim && !elems.parse_error) {
  2337. const struct ieee80211_tim_ie *tim_ie = elems.tim;
  2338. ifmgd->dtim_period = tim_ie->dtim_period;
  2339. }
  2340. ifmgd->have_beacon = true;
  2341. ifmgd->assoc_data->need_beacon = false;
  2342. if (local->hw.flags & IEEE80211_HW_TIMING_BEACON_ONLY) {
  2343. sdata->vif.bss_conf.sync_tsf =
  2344. le64_to_cpu(mgmt->u.beacon.timestamp);
  2345. sdata->vif.bss_conf.sync_device_ts =
  2346. rx_status->device_timestamp;
  2347. if (elems.tim)
  2348. sdata->vif.bss_conf.sync_dtim_count =
  2349. elems.tim->dtim_count;
  2350. else
  2351. sdata->vif.bss_conf.sync_dtim_count = 0;
  2352. }
  2353. /* continue assoc process */
  2354. ifmgd->assoc_data->timeout = jiffies;
  2355. ifmgd->assoc_data->timeout_started = true;
  2356. run_again(sdata, ifmgd->assoc_data->timeout);
  2357. return;
  2358. }
  2359. if (!ifmgd->associated ||
  2360. !ether_addr_equal(mgmt->bssid, ifmgd->associated->bssid))
  2361. return;
  2362. bssid = ifmgd->associated->bssid;
  2363. /* Track average RSSI from the Beacon frames of the current AP */
  2364. ifmgd->last_beacon_signal = rx_status->signal;
  2365. if (ifmgd->flags & IEEE80211_STA_RESET_SIGNAL_AVE) {
  2366. ifmgd->flags &= ~IEEE80211_STA_RESET_SIGNAL_AVE;
  2367. ifmgd->ave_beacon_signal = rx_status->signal * 16;
  2368. ifmgd->last_cqm_event_signal = 0;
  2369. ifmgd->count_beacon_signal = 1;
  2370. ifmgd->last_ave_beacon_signal = 0;
  2371. } else {
  2372. ifmgd->ave_beacon_signal =
  2373. (IEEE80211_SIGNAL_AVE_WEIGHT * rx_status->signal * 16 +
  2374. (16 - IEEE80211_SIGNAL_AVE_WEIGHT) *
  2375. ifmgd->ave_beacon_signal) / 16;
  2376. ifmgd->count_beacon_signal++;
  2377. }
  2378. if (ifmgd->rssi_min_thold != ifmgd->rssi_max_thold &&
  2379. ifmgd->count_beacon_signal >= IEEE80211_SIGNAL_AVE_MIN_COUNT) {
  2380. int sig = ifmgd->ave_beacon_signal;
  2381. int last_sig = ifmgd->last_ave_beacon_signal;
  2382. /*
  2383. * if signal crosses either of the boundaries, invoke callback
  2384. * with appropriate parameters
  2385. */
  2386. if (sig > ifmgd->rssi_max_thold &&
  2387. (last_sig <= ifmgd->rssi_min_thold || last_sig == 0)) {
  2388. ifmgd->last_ave_beacon_signal = sig;
  2389. drv_rssi_callback(local, sdata, RSSI_EVENT_HIGH);
  2390. } else if (sig < ifmgd->rssi_min_thold &&
  2391. (last_sig >= ifmgd->rssi_max_thold ||
  2392. last_sig == 0)) {
  2393. ifmgd->last_ave_beacon_signal = sig;
  2394. drv_rssi_callback(local, sdata, RSSI_EVENT_LOW);
  2395. }
  2396. }
  2397. if (bss_conf->cqm_rssi_thold &&
  2398. ifmgd->count_beacon_signal >= IEEE80211_SIGNAL_AVE_MIN_COUNT &&
  2399. !(sdata->vif.driver_flags & IEEE80211_VIF_SUPPORTS_CQM_RSSI)) {
  2400. int sig = ifmgd->ave_beacon_signal / 16;
  2401. int last_event = ifmgd->last_cqm_event_signal;
  2402. int thold = bss_conf->cqm_rssi_thold;
  2403. int hyst = bss_conf->cqm_rssi_hyst;
  2404. if (sig < thold &&
  2405. (last_event == 0 || sig < last_event - hyst)) {
  2406. ifmgd->last_cqm_event_signal = sig;
  2407. ieee80211_cqm_rssi_notify(
  2408. &sdata->vif,
  2409. NL80211_CQM_RSSI_THRESHOLD_EVENT_LOW,
  2410. GFP_KERNEL);
  2411. } else if (sig > thold &&
  2412. (last_event == 0 || sig > last_event + hyst)) {
  2413. ifmgd->last_cqm_event_signal = sig;
  2414. ieee80211_cqm_rssi_notify(
  2415. &sdata->vif,
  2416. NL80211_CQM_RSSI_THRESHOLD_EVENT_HIGH,
  2417. GFP_KERNEL);
  2418. }
  2419. }
  2420. if (ifmgd->flags & IEEE80211_STA_CONNECTION_POLL) {
  2421. mlme_dbg_ratelimited(sdata,
  2422. "cancelling AP probe due to a received beacon\n");
  2423. ieee80211_reset_ap_probe(sdata);
  2424. }
  2425. /*
  2426. * Push the beacon loss detection into the future since
  2427. * we are processing a beacon from the AP just now.
  2428. */
  2429. ieee80211_sta_reset_beacon_monitor(sdata);
  2430. ncrc = crc32_be(0, (void *)&mgmt->u.beacon.beacon_int, 4);
  2431. ncrc = ieee802_11_parse_elems_crc(mgmt->u.beacon.variable,
  2432. len - baselen, false, &elems,
  2433. care_about_ies, ncrc);
  2434. if (local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK) {
  2435. bool directed_tim = ieee80211_check_tim(elems.tim,
  2436. elems.tim_len,
  2437. ifmgd->aid);
  2438. if (directed_tim) {
  2439. if (local->hw.conf.dynamic_ps_timeout > 0) {
  2440. if (local->hw.conf.flags & IEEE80211_CONF_PS) {
  2441. local->hw.conf.flags &= ~IEEE80211_CONF_PS;
  2442. ieee80211_hw_config(local,
  2443. IEEE80211_CONF_CHANGE_PS);
  2444. }
  2445. ieee80211_send_nullfunc(local, sdata, 0);
  2446. } else if (!local->pspolling && sdata->u.mgd.powersave) {
  2447. local->pspolling = true;
  2448. /*
  2449. * Here is assumed that the driver will be
  2450. * able to send ps-poll frame and receive a
  2451. * response even though power save mode is
  2452. * enabled, but some drivers might require
  2453. * to disable power save here. This needs
  2454. * to be investigated.
  2455. */
  2456. ieee80211_send_pspoll(local, sdata);
  2457. }
  2458. }
  2459. }
  2460. if (sdata->vif.p2p) {
  2461. struct ieee80211_p2p_noa_attr noa = {};
  2462. int ret;
  2463. ret = cfg80211_get_p2p_attr(mgmt->u.beacon.variable,
  2464. len - baselen,
  2465. IEEE80211_P2P_ATTR_ABSENCE_NOTICE,
  2466. (u8 *) &noa, sizeof(noa));
  2467. if (ret >= 2) {
  2468. if (sdata->u.mgd.p2p_noa_index != noa.index) {
  2469. /* valid noa_attr and index changed */
  2470. sdata->u.mgd.p2p_noa_index = noa.index;
  2471. memcpy(&bss_conf->p2p_noa_attr, &noa, sizeof(noa));
  2472. changed |= BSS_CHANGED_P2P_PS;
  2473. /*
  2474. * make sure we update all information, the CRC
  2475. * mechanism doesn't look at P2P attributes.
  2476. */
  2477. ifmgd->beacon_crc_valid = false;
  2478. }
  2479. } else if (sdata->u.mgd.p2p_noa_index != -1) {
  2480. /* noa_attr not found and we had valid noa_attr before */
  2481. sdata->u.mgd.p2p_noa_index = -1;
  2482. memset(&bss_conf->p2p_noa_attr, 0, sizeof(bss_conf->p2p_noa_attr));
  2483. changed |= BSS_CHANGED_P2P_PS;
  2484. ifmgd->beacon_crc_valid = false;
  2485. }
  2486. }
  2487. if (ncrc == ifmgd->beacon_crc && ifmgd->beacon_crc_valid)
  2488. return;
  2489. ifmgd->beacon_crc = ncrc;
  2490. ifmgd->beacon_crc_valid = true;
  2491. ieee80211_rx_bss_info(sdata, mgmt, len, rx_status, &elems);
  2492. ieee80211_sta_process_chanswitch(sdata, rx_status->mactime,
  2493. &elems, true);
  2494. if (!(ifmgd->flags & IEEE80211_STA_DISABLE_WMM) &&
  2495. ieee80211_sta_wmm_params(local, sdata, elems.wmm_param,
  2496. elems.wmm_param_len))
  2497. changed |= BSS_CHANGED_QOS;
  2498. /*
  2499. * If we haven't had a beacon before, tell the driver about the
  2500. * DTIM period (and beacon timing if desired) now.
  2501. */
  2502. if (!ifmgd->have_beacon) {
  2503. /* a few bogus AP send dtim_period = 0 or no TIM IE */
  2504. if (elems.tim)
  2505. bss_conf->dtim_period = elems.tim->dtim_period ?: 1;
  2506. else
  2507. bss_conf->dtim_period = 1;
  2508. if (local->hw.flags & IEEE80211_HW_TIMING_BEACON_ONLY) {
  2509. sdata->vif.bss_conf.sync_tsf =
  2510. le64_to_cpu(mgmt->u.beacon.timestamp);
  2511. sdata->vif.bss_conf.sync_device_ts =
  2512. rx_status->device_timestamp;
  2513. if (elems.tim)
  2514. sdata->vif.bss_conf.sync_dtim_count =
  2515. elems.tim->dtim_count;
  2516. else
  2517. sdata->vif.bss_conf.sync_dtim_count = 0;
  2518. }
  2519. changed |= BSS_CHANGED_BEACON_INFO;
  2520. ifmgd->have_beacon = true;
  2521. mutex_lock(&local->iflist_mtx);
  2522. ieee80211_recalc_ps(local, -1);
  2523. mutex_unlock(&local->iflist_mtx);
  2524. ieee80211_recalc_ps_vif(sdata);
  2525. }
  2526. if (elems.erp_info) {
  2527. erp_valid = true;
  2528. erp_value = elems.erp_info[0];
  2529. } else {
  2530. erp_valid = false;
  2531. }
  2532. changed |= ieee80211_handle_bss_capability(sdata,
  2533. le16_to_cpu(mgmt->u.beacon.capab_info),
  2534. erp_valid, erp_value);
  2535. mutex_lock(&local->sta_mtx);
  2536. sta = sta_info_get(sdata, bssid);
  2537. if (ieee80211_config_bw(sdata, sta, elems.ht_operation,
  2538. elems.vht_operation, bssid, &changed)) {
  2539. mutex_unlock(&local->sta_mtx);
  2540. ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DEAUTH,
  2541. WLAN_REASON_DEAUTH_LEAVING,
  2542. true, deauth_buf);
  2543. cfg80211_tx_mlme_mgmt(sdata->dev, deauth_buf,
  2544. sizeof(deauth_buf));
  2545. return;
  2546. }
  2547. if (sta && elems.opmode_notif)
  2548. ieee80211_vht_handle_opmode(sdata, sta, *elems.opmode_notif,
  2549. rx_status->band, true);
  2550. mutex_unlock(&local->sta_mtx);
  2551. if (elems.country_elem && elems.pwr_constr_elem &&
  2552. mgmt->u.probe_resp.capab_info &
  2553. cpu_to_le16(WLAN_CAPABILITY_SPECTRUM_MGMT))
  2554. changed |= ieee80211_handle_pwr_constr(sdata, chan,
  2555. elems.country_elem,
  2556. elems.country_elem_len,
  2557. elems.pwr_constr_elem);
  2558. ieee80211_bss_info_change_notify(sdata, changed);
  2559. }
  2560. void ieee80211_sta_rx_queued_mgmt(struct ieee80211_sub_if_data *sdata,
  2561. struct sk_buff *skb)
  2562. {
  2563. struct ieee80211_rx_status *rx_status;
  2564. struct ieee80211_mgmt *mgmt;
  2565. u16 fc;
  2566. struct ieee802_11_elems elems;
  2567. int ies_len;
  2568. rx_status = (struct ieee80211_rx_status *) skb->cb;
  2569. mgmt = (struct ieee80211_mgmt *) skb->data;
  2570. fc = le16_to_cpu(mgmt->frame_control);
  2571. sdata_lock(sdata);
  2572. switch (fc & IEEE80211_FCTL_STYPE) {
  2573. case IEEE80211_STYPE_BEACON:
  2574. ieee80211_rx_mgmt_beacon(sdata, mgmt, skb->len, rx_status);
  2575. break;
  2576. case IEEE80211_STYPE_PROBE_RESP:
  2577. ieee80211_rx_mgmt_probe_resp(sdata, skb);
  2578. break;
  2579. case IEEE80211_STYPE_AUTH:
  2580. ieee80211_rx_mgmt_auth(sdata, mgmt, skb->len);
  2581. break;
  2582. case IEEE80211_STYPE_DEAUTH:
  2583. ieee80211_rx_mgmt_deauth(sdata, mgmt, skb->len);
  2584. break;
  2585. case IEEE80211_STYPE_DISASSOC:
  2586. ieee80211_rx_mgmt_disassoc(sdata, mgmt, skb->len);
  2587. break;
  2588. case IEEE80211_STYPE_ASSOC_RESP:
  2589. case IEEE80211_STYPE_REASSOC_RESP:
  2590. ieee80211_rx_mgmt_assoc_resp(sdata, mgmt, skb->len);
  2591. break;
  2592. case IEEE80211_STYPE_ACTION:
  2593. if (mgmt->u.action.category == WLAN_CATEGORY_SPECTRUM_MGMT) {
  2594. ies_len = skb->len -
  2595. offsetof(struct ieee80211_mgmt,
  2596. u.action.u.chan_switch.variable);
  2597. if (ies_len < 0)
  2598. break;
  2599. ieee802_11_parse_elems(
  2600. mgmt->u.action.u.chan_switch.variable,
  2601. ies_len, true, &elems);
  2602. if (elems.parse_error)
  2603. break;
  2604. ieee80211_sta_process_chanswitch(sdata,
  2605. rx_status->mactime,
  2606. &elems, false);
  2607. } else if (mgmt->u.action.category == WLAN_CATEGORY_PUBLIC) {
  2608. ies_len = skb->len -
  2609. offsetof(struct ieee80211_mgmt,
  2610. u.action.u.ext_chan_switch.variable);
  2611. if (ies_len < 0)
  2612. break;
  2613. ieee802_11_parse_elems(
  2614. mgmt->u.action.u.ext_chan_switch.variable,
  2615. ies_len, true, &elems);
  2616. if (elems.parse_error)
  2617. break;
  2618. /* for the handling code pretend this was also an IE */
  2619. elems.ext_chansw_ie =
  2620. &mgmt->u.action.u.ext_chan_switch.data;
  2621. ieee80211_sta_process_chanswitch(sdata,
  2622. rx_status->mactime,
  2623. &elems, false);
  2624. }
  2625. break;
  2626. }
  2627. sdata_unlock(sdata);
  2628. }
  2629. static void ieee80211_sta_timer(unsigned long data)
  2630. {
  2631. struct ieee80211_sub_if_data *sdata =
  2632. (struct ieee80211_sub_if_data *) data;
  2633. ieee80211_queue_work(&sdata->local->hw, &sdata->work);
  2634. }
  2635. static void ieee80211_sta_connection_lost(struct ieee80211_sub_if_data *sdata,
  2636. u8 *bssid, u8 reason, bool tx)
  2637. {
  2638. u8 frame_buf[IEEE80211_DEAUTH_FRAME_LEN];
  2639. ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DEAUTH, reason,
  2640. tx, frame_buf);
  2641. cfg80211_tx_mlme_mgmt(sdata->dev, frame_buf,
  2642. IEEE80211_DEAUTH_FRAME_LEN);
  2643. }
  2644. static int ieee80211_probe_auth(struct ieee80211_sub_if_data *sdata)
  2645. {
  2646. struct ieee80211_local *local = sdata->local;
  2647. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  2648. struct ieee80211_mgd_auth_data *auth_data = ifmgd->auth_data;
  2649. u32 tx_flags = 0;
  2650. sdata_assert_lock(sdata);
  2651. if (WARN_ON_ONCE(!auth_data))
  2652. return -EINVAL;
  2653. auth_data->tries++;
  2654. if (auth_data->tries > IEEE80211_AUTH_MAX_TRIES) {
  2655. sdata_info(sdata, "authentication with %pM timed out\n",
  2656. auth_data->bss->bssid);
  2657. /*
  2658. * Most likely AP is not in the range so remove the
  2659. * bss struct for that AP.
  2660. */
  2661. cfg80211_unlink_bss(local->hw.wiphy, auth_data->bss);
  2662. return -ETIMEDOUT;
  2663. }
  2664. drv_mgd_prepare_tx(local, sdata);
  2665. if (auth_data->bss->proberesp_ies) {
  2666. u16 trans = 1;
  2667. u16 status = 0;
  2668. sdata_info(sdata, "send auth to %pM (try %d/%d)\n",
  2669. auth_data->bss->bssid, auth_data->tries,
  2670. IEEE80211_AUTH_MAX_TRIES);
  2671. auth_data->expected_transaction = 2;
  2672. if (auth_data->algorithm == WLAN_AUTH_SAE) {
  2673. trans = auth_data->sae_trans;
  2674. status = auth_data->sae_status;
  2675. auth_data->expected_transaction = trans;
  2676. }
  2677. if (local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS)
  2678. tx_flags = IEEE80211_TX_CTL_REQ_TX_STATUS |
  2679. IEEE80211_TX_INTFL_MLME_CONN_TX;
  2680. ieee80211_send_auth(sdata, trans, auth_data->algorithm, status,
  2681. auth_data->data, auth_data->data_len,
  2682. auth_data->bss->bssid,
  2683. auth_data->bss->bssid, NULL, 0, 0,
  2684. tx_flags);
  2685. } else {
  2686. const u8 *ssidie;
  2687. sdata_info(sdata, "direct probe to %pM (try %d/%i)\n",
  2688. auth_data->bss->bssid, auth_data->tries,
  2689. IEEE80211_AUTH_MAX_TRIES);
  2690. rcu_read_lock();
  2691. ssidie = ieee80211_bss_get_ie(auth_data->bss, WLAN_EID_SSID);
  2692. if (!ssidie) {
  2693. rcu_read_unlock();
  2694. return -EINVAL;
  2695. }
  2696. /*
  2697. * Direct probe is sent to broadcast address as some APs
  2698. * will not answer to direct packet in unassociated state.
  2699. */
  2700. ieee80211_send_probe_req(sdata, NULL, ssidie + 2, ssidie[1],
  2701. NULL, 0, (u32) -1, true, 0,
  2702. auth_data->bss->channel, false);
  2703. rcu_read_unlock();
  2704. }
  2705. if (tx_flags == 0) {
  2706. auth_data->timeout = jiffies + IEEE80211_AUTH_TIMEOUT;
  2707. auth_data->timeout_started = true;
  2708. run_again(sdata, auth_data->timeout);
  2709. } else {
  2710. auth_data->timeout =
  2711. round_jiffies_up(jiffies + IEEE80211_AUTH_TIMEOUT_LONG);
  2712. auth_data->timeout_started = true;
  2713. run_again(sdata, auth_data->timeout);
  2714. }
  2715. return 0;
  2716. }
  2717. static int ieee80211_do_assoc(struct ieee80211_sub_if_data *sdata)
  2718. {
  2719. struct ieee80211_mgd_assoc_data *assoc_data = sdata->u.mgd.assoc_data;
  2720. struct ieee80211_local *local = sdata->local;
  2721. sdata_assert_lock(sdata);
  2722. assoc_data->tries++;
  2723. if (assoc_data->tries > IEEE80211_ASSOC_MAX_TRIES) {
  2724. sdata_info(sdata, "association with %pM timed out\n",
  2725. assoc_data->bss->bssid);
  2726. /*
  2727. * Most likely AP is not in the range so remove the
  2728. * bss struct for that AP.
  2729. */
  2730. cfg80211_unlink_bss(local->hw.wiphy, assoc_data->bss);
  2731. return -ETIMEDOUT;
  2732. }
  2733. sdata_info(sdata, "associate with %pM (try %d/%d)\n",
  2734. assoc_data->bss->bssid, assoc_data->tries,
  2735. IEEE80211_ASSOC_MAX_TRIES);
  2736. ieee80211_send_assoc(sdata);
  2737. if (!(local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS)) {
  2738. assoc_data->timeout = jiffies + IEEE80211_ASSOC_TIMEOUT;
  2739. assoc_data->timeout_started = true;
  2740. run_again(sdata, assoc_data->timeout);
  2741. } else {
  2742. assoc_data->timeout =
  2743. round_jiffies_up(jiffies +
  2744. IEEE80211_ASSOC_TIMEOUT_LONG);
  2745. assoc_data->timeout_started = true;
  2746. run_again(sdata, assoc_data->timeout);
  2747. }
  2748. return 0;
  2749. }
  2750. void ieee80211_mgd_conn_tx_status(struct ieee80211_sub_if_data *sdata,
  2751. __le16 fc, bool acked)
  2752. {
  2753. struct ieee80211_local *local = sdata->local;
  2754. sdata->u.mgd.status_fc = fc;
  2755. sdata->u.mgd.status_acked = acked;
  2756. sdata->u.mgd.status_received = true;
  2757. ieee80211_queue_work(&local->hw, &sdata->work);
  2758. }
  2759. void ieee80211_sta_work(struct ieee80211_sub_if_data *sdata)
  2760. {
  2761. struct ieee80211_local *local = sdata->local;
  2762. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  2763. sdata_lock(sdata);
  2764. if (ifmgd->status_received) {
  2765. __le16 fc = ifmgd->status_fc;
  2766. bool status_acked = ifmgd->status_acked;
  2767. ifmgd->status_received = false;
  2768. if (ifmgd->auth_data &&
  2769. (ieee80211_is_probe_req(fc) || ieee80211_is_auth(fc))) {
  2770. if (status_acked) {
  2771. ifmgd->auth_data->timeout =
  2772. jiffies + IEEE80211_AUTH_TIMEOUT_SHORT;
  2773. run_again(sdata, ifmgd->auth_data->timeout);
  2774. } else {
  2775. ifmgd->auth_data->timeout = jiffies - 1;
  2776. }
  2777. ifmgd->auth_data->timeout_started = true;
  2778. } else if (ifmgd->assoc_data &&
  2779. (ieee80211_is_assoc_req(fc) ||
  2780. ieee80211_is_reassoc_req(fc))) {
  2781. if (status_acked) {
  2782. ifmgd->assoc_data->timeout =
  2783. jiffies + IEEE80211_ASSOC_TIMEOUT_SHORT;
  2784. run_again(sdata, ifmgd->assoc_data->timeout);
  2785. } else {
  2786. ifmgd->assoc_data->timeout = jiffies - 1;
  2787. }
  2788. ifmgd->assoc_data->timeout_started = true;
  2789. }
  2790. }
  2791. if (ifmgd->auth_data && ifmgd->auth_data->timeout_started &&
  2792. time_after(jiffies, ifmgd->auth_data->timeout)) {
  2793. if (ifmgd->auth_data->done) {
  2794. /*
  2795. * ok ... we waited for assoc but userspace didn't,
  2796. * so let's just kill the auth data
  2797. */
  2798. ieee80211_destroy_auth_data(sdata, false);
  2799. } else if (ieee80211_probe_auth(sdata)) {
  2800. u8 bssid[ETH_ALEN];
  2801. memcpy(bssid, ifmgd->auth_data->bss->bssid, ETH_ALEN);
  2802. ieee80211_destroy_auth_data(sdata, false);
  2803. cfg80211_auth_timeout(sdata->dev, bssid);
  2804. }
  2805. } else if (ifmgd->auth_data && ifmgd->auth_data->timeout_started)
  2806. run_again(sdata, ifmgd->auth_data->timeout);
  2807. if (ifmgd->assoc_data && ifmgd->assoc_data->timeout_started &&
  2808. time_after(jiffies, ifmgd->assoc_data->timeout)) {
  2809. if ((ifmgd->assoc_data->need_beacon && !ifmgd->have_beacon) ||
  2810. ieee80211_do_assoc(sdata)) {
  2811. struct cfg80211_bss *bss = ifmgd->assoc_data->bss;
  2812. ieee80211_destroy_assoc_data(sdata, false);
  2813. cfg80211_assoc_timeout(sdata->dev, bss);
  2814. }
  2815. } else if (ifmgd->assoc_data && ifmgd->assoc_data->timeout_started)
  2816. run_again(sdata, ifmgd->assoc_data->timeout);
  2817. if (ifmgd->flags & IEEE80211_STA_CONNECTION_POLL &&
  2818. ifmgd->associated) {
  2819. u8 bssid[ETH_ALEN];
  2820. int max_tries;
  2821. memcpy(bssid, ifmgd->associated->bssid, ETH_ALEN);
  2822. if (local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS)
  2823. max_tries = max_nullfunc_tries;
  2824. else
  2825. max_tries = max_probe_tries;
  2826. /* ACK received for nullfunc probing frame */
  2827. if (!ifmgd->probe_send_count)
  2828. ieee80211_reset_ap_probe(sdata);
  2829. else if (ifmgd->nullfunc_failed) {
  2830. if (ifmgd->probe_send_count < max_tries) {
  2831. mlme_dbg(sdata,
  2832. "No ack for nullfunc frame to AP %pM, try %d/%i\n",
  2833. bssid, ifmgd->probe_send_count,
  2834. max_tries);
  2835. ieee80211_mgd_probe_ap_send(sdata);
  2836. } else {
  2837. mlme_dbg(sdata,
  2838. "No ack for nullfunc frame to AP %pM, disconnecting.\n",
  2839. bssid);
  2840. ieee80211_sta_connection_lost(sdata, bssid,
  2841. WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY,
  2842. false);
  2843. }
  2844. } else if (time_is_after_jiffies(ifmgd->probe_timeout))
  2845. run_again(sdata, ifmgd->probe_timeout);
  2846. else if (local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS) {
  2847. mlme_dbg(sdata,
  2848. "Failed to send nullfunc to AP %pM after %dms, disconnecting\n",
  2849. bssid, probe_wait_ms);
  2850. ieee80211_sta_connection_lost(sdata, bssid,
  2851. WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY, false);
  2852. } else if (ifmgd->probe_send_count < max_tries) {
  2853. mlme_dbg(sdata,
  2854. "No probe response from AP %pM after %dms, try %d/%i\n",
  2855. bssid, probe_wait_ms,
  2856. ifmgd->probe_send_count, max_tries);
  2857. ieee80211_mgd_probe_ap_send(sdata);
  2858. } else {
  2859. /*
  2860. * We actually lost the connection ... or did we?
  2861. * Let's make sure!
  2862. */
  2863. wiphy_debug(local->hw.wiphy,
  2864. "%s: No probe response from AP %pM"
  2865. " after %dms, disconnecting.\n",
  2866. sdata->name,
  2867. bssid, probe_wait_ms);
  2868. ieee80211_sta_connection_lost(sdata, bssid,
  2869. WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY, false);
  2870. }
  2871. }
  2872. sdata_unlock(sdata);
  2873. }
  2874. static void ieee80211_sta_bcn_mon_timer(unsigned long data)
  2875. {
  2876. struct ieee80211_sub_if_data *sdata =
  2877. (struct ieee80211_sub_if_data *) data;
  2878. struct ieee80211_local *local = sdata->local;
  2879. if (local->quiescing)
  2880. return;
  2881. sdata->u.mgd.connection_loss = false;
  2882. ieee80211_queue_work(&sdata->local->hw,
  2883. &sdata->u.mgd.beacon_connection_loss_work);
  2884. }
  2885. static void ieee80211_sta_conn_mon_timer(unsigned long data)
  2886. {
  2887. struct ieee80211_sub_if_data *sdata =
  2888. (struct ieee80211_sub_if_data *) data;
  2889. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  2890. struct ieee80211_local *local = sdata->local;
  2891. if (local->quiescing)
  2892. return;
  2893. ieee80211_queue_work(&local->hw, &ifmgd->monitor_work);
  2894. }
  2895. static void ieee80211_sta_monitor_work(struct work_struct *work)
  2896. {
  2897. struct ieee80211_sub_if_data *sdata =
  2898. container_of(work, struct ieee80211_sub_if_data,
  2899. u.mgd.monitor_work);
  2900. ieee80211_mgd_probe_ap(sdata, false);
  2901. }
  2902. static void ieee80211_restart_sta_timer(struct ieee80211_sub_if_data *sdata)
  2903. {
  2904. u32 flags;
  2905. if (sdata->vif.type == NL80211_IFTYPE_STATION) {
  2906. __ieee80211_stop_poll(sdata);
  2907. /* let's probe the connection once */
  2908. flags = sdata->local->hw.flags;
  2909. if (!(flags & IEEE80211_HW_CONNECTION_MONITOR))
  2910. ieee80211_queue_work(&sdata->local->hw,
  2911. &sdata->u.mgd.monitor_work);
  2912. /* and do all the other regular work too */
  2913. ieee80211_queue_work(&sdata->local->hw, &sdata->work);
  2914. }
  2915. }
  2916. #ifdef CONFIG_PM
  2917. void ieee80211_sta_restart(struct ieee80211_sub_if_data *sdata)
  2918. {
  2919. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  2920. sdata_lock(sdata);
  2921. if (!ifmgd->associated) {
  2922. sdata_unlock(sdata);
  2923. return;
  2924. }
  2925. if (sdata->flags & IEEE80211_SDATA_DISCONNECT_RESUME) {
  2926. sdata->flags &= ~IEEE80211_SDATA_DISCONNECT_RESUME;
  2927. mlme_dbg(sdata, "driver requested disconnect after resume\n");
  2928. ieee80211_sta_connection_lost(sdata,
  2929. ifmgd->associated->bssid,
  2930. WLAN_REASON_UNSPECIFIED,
  2931. true);
  2932. sdata_unlock(sdata);
  2933. return;
  2934. }
  2935. sdata_unlock(sdata);
  2936. }
  2937. #endif
  2938. /* interface setup */
  2939. void ieee80211_sta_setup_sdata(struct ieee80211_sub_if_data *sdata)
  2940. {
  2941. struct ieee80211_if_managed *ifmgd;
  2942. ifmgd = &sdata->u.mgd;
  2943. INIT_WORK(&ifmgd->monitor_work, ieee80211_sta_monitor_work);
  2944. INIT_WORK(&ifmgd->chswitch_work, ieee80211_chswitch_work);
  2945. INIT_WORK(&ifmgd->beacon_connection_loss_work,
  2946. ieee80211_beacon_connection_loss_work);
  2947. INIT_WORK(&ifmgd->csa_connection_drop_work,
  2948. ieee80211_csa_connection_drop_work);
  2949. INIT_WORK(&ifmgd->request_smps_work, ieee80211_request_smps_mgd_work);
  2950. setup_timer(&ifmgd->timer, ieee80211_sta_timer,
  2951. (unsigned long) sdata);
  2952. setup_timer(&ifmgd->bcn_mon_timer, ieee80211_sta_bcn_mon_timer,
  2953. (unsigned long) sdata);
  2954. setup_timer(&ifmgd->conn_mon_timer, ieee80211_sta_conn_mon_timer,
  2955. (unsigned long) sdata);
  2956. setup_timer(&ifmgd->chswitch_timer, ieee80211_chswitch_timer,
  2957. (unsigned long) sdata);
  2958. ifmgd->flags = 0;
  2959. ifmgd->powersave = sdata->wdev.ps;
  2960. ifmgd->uapsd_queues = sdata->local->hw.uapsd_queues;
  2961. ifmgd->uapsd_max_sp_len = sdata->local->hw.uapsd_max_sp_len;
  2962. ifmgd->p2p_noa_index = -1;
  2963. if (sdata->local->hw.flags & IEEE80211_HW_SUPPORTS_DYNAMIC_SMPS)
  2964. ifmgd->req_smps = IEEE80211_SMPS_AUTOMATIC;
  2965. else
  2966. ifmgd->req_smps = IEEE80211_SMPS_OFF;
  2967. }
  2968. /* scan finished notification */
  2969. void ieee80211_mlme_notify_scan_completed(struct ieee80211_local *local)
  2970. {
  2971. struct ieee80211_sub_if_data *sdata;
  2972. /* Restart STA timers */
  2973. rcu_read_lock();
  2974. list_for_each_entry_rcu(sdata, &local->interfaces, list) {
  2975. if (ieee80211_sdata_running(sdata))
  2976. ieee80211_restart_sta_timer(sdata);
  2977. }
  2978. rcu_read_unlock();
  2979. }
  2980. int ieee80211_max_network_latency(struct notifier_block *nb,
  2981. unsigned long data, void *dummy)
  2982. {
  2983. s32 latency_usec = (s32) data;
  2984. struct ieee80211_local *local =
  2985. container_of(nb, struct ieee80211_local,
  2986. network_latency_notifier);
  2987. mutex_lock(&local->iflist_mtx);
  2988. ieee80211_recalc_ps(local, latency_usec);
  2989. mutex_unlock(&local->iflist_mtx);
  2990. return 0;
  2991. }
  2992. static u8 ieee80211_ht_vht_rx_chains(struct ieee80211_sub_if_data *sdata,
  2993. struct cfg80211_bss *cbss)
  2994. {
  2995. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  2996. const u8 *ht_cap_ie, *vht_cap_ie;
  2997. const struct ieee80211_ht_cap *ht_cap;
  2998. const struct ieee80211_vht_cap *vht_cap;
  2999. u8 chains = 1;
  3000. if (ifmgd->flags & IEEE80211_STA_DISABLE_HT)
  3001. return chains;
  3002. ht_cap_ie = ieee80211_bss_get_ie(cbss, WLAN_EID_HT_CAPABILITY);
  3003. if (ht_cap_ie && ht_cap_ie[1] >= sizeof(*ht_cap)) {
  3004. ht_cap = (void *)(ht_cap_ie + 2);
  3005. chains = ieee80211_mcs_to_chains(&ht_cap->mcs);
  3006. /*
  3007. * TODO: use "Tx Maximum Number Spatial Streams Supported" and
  3008. * "Tx Unequal Modulation Supported" fields.
  3009. */
  3010. }
  3011. if (ifmgd->flags & IEEE80211_STA_DISABLE_VHT)
  3012. return chains;
  3013. vht_cap_ie = ieee80211_bss_get_ie(cbss, WLAN_EID_VHT_CAPABILITY);
  3014. if (vht_cap_ie && vht_cap_ie[1] >= sizeof(*vht_cap)) {
  3015. u8 nss;
  3016. u16 tx_mcs_map;
  3017. vht_cap = (void *)(vht_cap_ie + 2);
  3018. tx_mcs_map = le16_to_cpu(vht_cap->supp_mcs.tx_mcs_map);
  3019. for (nss = 8; nss > 0; nss--) {
  3020. if (((tx_mcs_map >> (2 * (nss - 1))) & 3) !=
  3021. IEEE80211_VHT_MCS_NOT_SUPPORTED)
  3022. break;
  3023. }
  3024. /* TODO: use "Tx Highest Supported Long GI Data Rate" field? */
  3025. chains = max(chains, nss);
  3026. }
  3027. return chains;
  3028. }
  3029. static int ieee80211_prep_channel(struct ieee80211_sub_if_data *sdata,
  3030. struct cfg80211_bss *cbss)
  3031. {
  3032. struct ieee80211_local *local = sdata->local;
  3033. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  3034. const struct ieee80211_ht_operation *ht_oper = NULL;
  3035. const struct ieee80211_vht_operation *vht_oper = NULL;
  3036. struct ieee80211_supported_band *sband;
  3037. struct cfg80211_chan_def chandef;
  3038. int ret;
  3039. sband = local->hw.wiphy->bands[cbss->channel->band];
  3040. ifmgd->flags &= ~(IEEE80211_STA_DISABLE_40MHZ |
  3041. IEEE80211_STA_DISABLE_80P80MHZ |
  3042. IEEE80211_STA_DISABLE_160MHZ);
  3043. rcu_read_lock();
  3044. if (!(ifmgd->flags & IEEE80211_STA_DISABLE_HT) &&
  3045. sband->ht_cap.ht_supported) {
  3046. const u8 *ht_oper_ie, *ht_cap;
  3047. ht_oper_ie = ieee80211_bss_get_ie(cbss, WLAN_EID_HT_OPERATION);
  3048. if (ht_oper_ie && ht_oper_ie[1] >= sizeof(*ht_oper))
  3049. ht_oper = (void *)(ht_oper_ie + 2);
  3050. ht_cap = ieee80211_bss_get_ie(cbss, WLAN_EID_HT_CAPABILITY);
  3051. if (!ht_cap || ht_cap[1] < sizeof(struct ieee80211_ht_cap)) {
  3052. ifmgd->flags |= IEEE80211_STA_DISABLE_HT;
  3053. ht_oper = NULL;
  3054. }
  3055. }
  3056. if (!(ifmgd->flags & IEEE80211_STA_DISABLE_VHT) &&
  3057. sband->vht_cap.vht_supported) {
  3058. const u8 *vht_oper_ie, *vht_cap;
  3059. vht_oper_ie = ieee80211_bss_get_ie(cbss,
  3060. WLAN_EID_VHT_OPERATION);
  3061. if (vht_oper_ie && vht_oper_ie[1] >= sizeof(*vht_oper))
  3062. vht_oper = (void *)(vht_oper_ie + 2);
  3063. if (vht_oper && !ht_oper) {
  3064. vht_oper = NULL;
  3065. sdata_info(sdata,
  3066. "AP advertised VHT without HT, disabling both\n");
  3067. ifmgd->flags |= IEEE80211_STA_DISABLE_HT;
  3068. ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
  3069. }
  3070. vht_cap = ieee80211_bss_get_ie(cbss, WLAN_EID_VHT_CAPABILITY);
  3071. if (!vht_cap || vht_cap[1] < sizeof(struct ieee80211_vht_cap)) {
  3072. ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
  3073. vht_oper = NULL;
  3074. }
  3075. }
  3076. ifmgd->flags |= ieee80211_determine_chantype(sdata, sband,
  3077. cbss->channel,
  3078. ht_oper, vht_oper,
  3079. &chandef, false);
  3080. sdata->needed_rx_chains = min(ieee80211_ht_vht_rx_chains(sdata, cbss),
  3081. local->rx_chains);
  3082. rcu_read_unlock();
  3083. /* will change later if needed */
  3084. sdata->smps_mode = IEEE80211_SMPS_OFF;
  3085. /*
  3086. * If this fails (possibly due to channel context sharing
  3087. * on incompatible channels, e.g. 80+80 and 160 sharing the
  3088. * same control channel) try to use a smaller bandwidth.
  3089. */
  3090. ret = ieee80211_vif_use_channel(sdata, &chandef,
  3091. IEEE80211_CHANCTX_SHARED);
  3092. /* don't downgrade for 5 and 10 MHz channels, though. */
  3093. if (chandef.width == NL80211_CHAN_WIDTH_5 ||
  3094. chandef.width == NL80211_CHAN_WIDTH_10)
  3095. return ret;
  3096. while (ret && chandef.width != NL80211_CHAN_WIDTH_20_NOHT) {
  3097. ifmgd->flags |= ieee80211_chandef_downgrade(&chandef);
  3098. ret = ieee80211_vif_use_channel(sdata, &chandef,
  3099. IEEE80211_CHANCTX_SHARED);
  3100. }
  3101. return ret;
  3102. }
  3103. static int ieee80211_prep_connection(struct ieee80211_sub_if_data *sdata,
  3104. struct cfg80211_bss *cbss, bool assoc)
  3105. {
  3106. struct ieee80211_local *local = sdata->local;
  3107. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  3108. struct ieee80211_bss *bss = (void *)cbss->priv;
  3109. struct sta_info *new_sta = NULL;
  3110. bool have_sta = false;
  3111. int err;
  3112. if (WARN_ON(!ifmgd->auth_data && !ifmgd->assoc_data))
  3113. return -EINVAL;
  3114. if (assoc) {
  3115. rcu_read_lock();
  3116. have_sta = sta_info_get(sdata, cbss->bssid);
  3117. rcu_read_unlock();
  3118. }
  3119. if (!have_sta) {
  3120. new_sta = sta_info_alloc(sdata, cbss->bssid, GFP_KERNEL);
  3121. if (!new_sta)
  3122. return -ENOMEM;
  3123. }
  3124. if (new_sta) {
  3125. u32 rates = 0, basic_rates = 0;
  3126. bool have_higher_than_11mbit;
  3127. int min_rate = INT_MAX, min_rate_index = -1;
  3128. struct ieee80211_chanctx_conf *chanctx_conf;
  3129. struct ieee80211_supported_band *sband;
  3130. const struct cfg80211_bss_ies *ies;
  3131. int shift;
  3132. u32 rate_flags;
  3133. sband = local->hw.wiphy->bands[cbss->channel->band];
  3134. err = ieee80211_prep_channel(sdata, cbss);
  3135. if (err) {
  3136. sta_info_free(local, new_sta);
  3137. return -EINVAL;
  3138. }
  3139. shift = ieee80211_vif_get_shift(&sdata->vif);
  3140. rcu_read_lock();
  3141. chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
  3142. if (WARN_ON(!chanctx_conf)) {
  3143. rcu_read_unlock();
  3144. return -EINVAL;
  3145. }
  3146. rate_flags = ieee80211_chandef_rate_flags(&chanctx_conf->def);
  3147. rcu_read_unlock();
  3148. ieee80211_get_rates(sband, bss->supp_rates,
  3149. bss->supp_rates_len,
  3150. &rates, &basic_rates,
  3151. &have_higher_than_11mbit,
  3152. &min_rate, &min_rate_index,
  3153. shift, rate_flags);
  3154. /*
  3155. * This used to be a workaround for basic rates missing
  3156. * in the association response frame. Now that we no
  3157. * longer use the basic rates from there, it probably
  3158. * doesn't happen any more, but keep the workaround so
  3159. * in case some *other* APs are buggy in different ways
  3160. * we can connect -- with a warning.
  3161. */
  3162. if (!basic_rates && min_rate_index >= 0) {
  3163. sdata_info(sdata,
  3164. "No basic rates, using min rate instead\n");
  3165. basic_rates = BIT(min_rate_index);
  3166. }
  3167. new_sta->sta.supp_rates[cbss->channel->band] = rates;
  3168. sdata->vif.bss_conf.basic_rates = basic_rates;
  3169. /* cf. IEEE 802.11 9.2.12 */
  3170. if (cbss->channel->band == IEEE80211_BAND_2GHZ &&
  3171. have_higher_than_11mbit)
  3172. sdata->flags |= IEEE80211_SDATA_OPERATING_GMODE;
  3173. else
  3174. sdata->flags &= ~IEEE80211_SDATA_OPERATING_GMODE;
  3175. memcpy(ifmgd->bssid, cbss->bssid, ETH_ALEN);
  3176. /* set timing information */
  3177. sdata->vif.bss_conf.beacon_int = cbss->beacon_interval;
  3178. rcu_read_lock();
  3179. ies = rcu_dereference(cbss->beacon_ies);
  3180. if (ies) {
  3181. const u8 *tim_ie;
  3182. sdata->vif.bss_conf.sync_tsf = ies->tsf;
  3183. sdata->vif.bss_conf.sync_device_ts =
  3184. bss->device_ts_beacon;
  3185. tim_ie = cfg80211_find_ie(WLAN_EID_TIM,
  3186. ies->data, ies->len);
  3187. if (tim_ie && tim_ie[1] >= 2)
  3188. sdata->vif.bss_conf.sync_dtim_count = tim_ie[2];
  3189. else
  3190. sdata->vif.bss_conf.sync_dtim_count = 0;
  3191. } else if (!(local->hw.flags &
  3192. IEEE80211_HW_TIMING_BEACON_ONLY)) {
  3193. ies = rcu_dereference(cbss->proberesp_ies);
  3194. /* must be non-NULL since beacon IEs were NULL */
  3195. sdata->vif.bss_conf.sync_tsf = ies->tsf;
  3196. sdata->vif.bss_conf.sync_device_ts =
  3197. bss->device_ts_presp;
  3198. sdata->vif.bss_conf.sync_dtim_count = 0;
  3199. } else {
  3200. sdata->vif.bss_conf.sync_tsf = 0;
  3201. sdata->vif.bss_conf.sync_device_ts = 0;
  3202. sdata->vif.bss_conf.sync_dtim_count = 0;
  3203. }
  3204. rcu_read_unlock();
  3205. /* tell driver about BSSID, basic rates and timing */
  3206. ieee80211_bss_info_change_notify(sdata,
  3207. BSS_CHANGED_BSSID | BSS_CHANGED_BASIC_RATES |
  3208. BSS_CHANGED_BEACON_INT);
  3209. if (assoc)
  3210. sta_info_pre_move_state(new_sta, IEEE80211_STA_AUTH);
  3211. err = sta_info_insert(new_sta);
  3212. new_sta = NULL;
  3213. if (err) {
  3214. sdata_info(sdata,
  3215. "failed to insert STA entry for the AP (error %d)\n",
  3216. err);
  3217. return err;
  3218. }
  3219. } else
  3220. WARN_ON_ONCE(!ether_addr_equal(ifmgd->bssid, cbss->bssid));
  3221. return 0;
  3222. }
  3223. /* config hooks */
  3224. int ieee80211_mgd_auth(struct ieee80211_sub_if_data *sdata,
  3225. struct cfg80211_auth_request *req)
  3226. {
  3227. struct ieee80211_local *local = sdata->local;
  3228. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  3229. struct ieee80211_mgd_auth_data *auth_data;
  3230. u16 auth_alg;
  3231. int err;
  3232. /* prepare auth data structure */
  3233. switch (req->auth_type) {
  3234. case NL80211_AUTHTYPE_OPEN_SYSTEM:
  3235. auth_alg = WLAN_AUTH_OPEN;
  3236. break;
  3237. case NL80211_AUTHTYPE_SHARED_KEY:
  3238. if (IS_ERR(local->wep_tx_tfm))
  3239. return -EOPNOTSUPP;
  3240. auth_alg = WLAN_AUTH_SHARED_KEY;
  3241. break;
  3242. case NL80211_AUTHTYPE_FT:
  3243. auth_alg = WLAN_AUTH_FT;
  3244. break;
  3245. case NL80211_AUTHTYPE_NETWORK_EAP:
  3246. auth_alg = WLAN_AUTH_LEAP;
  3247. break;
  3248. case NL80211_AUTHTYPE_SAE:
  3249. auth_alg = WLAN_AUTH_SAE;
  3250. break;
  3251. default:
  3252. return -EOPNOTSUPP;
  3253. }
  3254. auth_data = kzalloc(sizeof(*auth_data) + req->sae_data_len +
  3255. req->ie_len, GFP_KERNEL);
  3256. if (!auth_data)
  3257. return -ENOMEM;
  3258. auth_data->bss = req->bss;
  3259. if (req->sae_data_len >= 4) {
  3260. __le16 *pos = (__le16 *) req->sae_data;
  3261. auth_data->sae_trans = le16_to_cpu(pos[0]);
  3262. auth_data->sae_status = le16_to_cpu(pos[1]);
  3263. memcpy(auth_data->data, req->sae_data + 4,
  3264. req->sae_data_len - 4);
  3265. auth_data->data_len += req->sae_data_len - 4;
  3266. }
  3267. if (req->ie && req->ie_len) {
  3268. memcpy(&auth_data->data[auth_data->data_len],
  3269. req->ie, req->ie_len);
  3270. auth_data->data_len += req->ie_len;
  3271. }
  3272. if (req->key && req->key_len) {
  3273. auth_data->key_len = req->key_len;
  3274. auth_data->key_idx = req->key_idx;
  3275. memcpy(auth_data->key, req->key, req->key_len);
  3276. }
  3277. auth_data->algorithm = auth_alg;
  3278. /* try to authenticate/probe */
  3279. if ((ifmgd->auth_data && !ifmgd->auth_data->done) ||
  3280. ifmgd->assoc_data) {
  3281. err = -EBUSY;
  3282. goto err_free;
  3283. }
  3284. if (ifmgd->auth_data)
  3285. ieee80211_destroy_auth_data(sdata, false);
  3286. /* prep auth_data so we don't go into idle on disassoc */
  3287. ifmgd->auth_data = auth_data;
  3288. if (ifmgd->associated) {
  3289. u8 frame_buf[IEEE80211_DEAUTH_FRAME_LEN];
  3290. ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DEAUTH,
  3291. WLAN_REASON_UNSPECIFIED,
  3292. false, frame_buf);
  3293. cfg80211_tx_mlme_mgmt(sdata->dev, frame_buf,
  3294. sizeof(frame_buf));
  3295. }
  3296. sdata_info(sdata, "authenticate with %pM\n", req->bss->bssid);
  3297. err = ieee80211_prep_connection(sdata, req->bss, false);
  3298. if (err)
  3299. goto err_clear;
  3300. err = ieee80211_probe_auth(sdata);
  3301. if (err) {
  3302. sta_info_destroy_addr(sdata, req->bss->bssid);
  3303. goto err_clear;
  3304. }
  3305. /* hold our own reference */
  3306. cfg80211_ref_bss(local->hw.wiphy, auth_data->bss);
  3307. return 0;
  3308. err_clear:
  3309. memset(ifmgd->bssid, 0, ETH_ALEN);
  3310. ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BSSID);
  3311. ifmgd->auth_data = NULL;
  3312. err_free:
  3313. kfree(auth_data);
  3314. return err;
  3315. }
  3316. static bool ieee80211_usable_wmm_params(struct ieee80211_sub_if_data *sdata,
  3317. const u8 *wmm_param, int len)
  3318. {
  3319. const u8 *pos;
  3320. size_t left;
  3321. if (len < 8)
  3322. return false;
  3323. if (wmm_param[5] != 1 /* version */)
  3324. return false;
  3325. pos = wmm_param + 8;
  3326. left = len - 8;
  3327. for (; left >= 4; left -= 4, pos += 4) {
  3328. u8 aifsn = pos[0] & 0x0f;
  3329. u8 ecwmin = pos[1] & 0x0f;
  3330. u8 ecwmax = (pos[1] & 0xf0) >> 4;
  3331. int aci = (pos[0] >> 5) & 0x03;
  3332. if (aifsn < 2) {
  3333. sdata_info(sdata,
  3334. "AP has invalid WMM params (AIFSN=%d for ACI %d), disabling WMM\n",
  3335. aifsn, aci);
  3336. return false;
  3337. }
  3338. if (ecwmin > ecwmax) {
  3339. sdata_info(sdata,
  3340. "AP has invalid WMM params (ECWmin/max=%d/%d for ACI %d), disabling WMM\n",
  3341. ecwmin, ecwmax, aci);
  3342. return false;
  3343. }
  3344. }
  3345. return true;
  3346. }
  3347. int ieee80211_mgd_assoc(struct ieee80211_sub_if_data *sdata,
  3348. struct cfg80211_assoc_request *req)
  3349. {
  3350. struct ieee80211_local *local = sdata->local;
  3351. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  3352. struct ieee80211_bss *bss = (void *)req->bss->priv;
  3353. struct ieee80211_mgd_assoc_data *assoc_data;
  3354. const struct cfg80211_bss_ies *beacon_ies;
  3355. struct ieee80211_supported_band *sband;
  3356. const u8 *ssidie, *ht_ie, *vht_ie;
  3357. int i, err;
  3358. assoc_data = kzalloc(sizeof(*assoc_data) + req->ie_len, GFP_KERNEL);
  3359. if (!assoc_data)
  3360. return -ENOMEM;
  3361. rcu_read_lock();
  3362. ssidie = ieee80211_bss_get_ie(req->bss, WLAN_EID_SSID);
  3363. if (!ssidie) {
  3364. rcu_read_unlock();
  3365. kfree(assoc_data);
  3366. return -EINVAL;
  3367. }
  3368. memcpy(assoc_data->ssid, ssidie + 2, ssidie[1]);
  3369. assoc_data->ssid_len = ssidie[1];
  3370. rcu_read_unlock();
  3371. if (ifmgd->associated) {
  3372. u8 frame_buf[IEEE80211_DEAUTH_FRAME_LEN];
  3373. ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DEAUTH,
  3374. WLAN_REASON_UNSPECIFIED,
  3375. false, frame_buf);
  3376. cfg80211_tx_mlme_mgmt(sdata->dev, frame_buf,
  3377. sizeof(frame_buf));
  3378. }
  3379. if (ifmgd->auth_data && !ifmgd->auth_data->done) {
  3380. err = -EBUSY;
  3381. goto err_free;
  3382. }
  3383. if (ifmgd->assoc_data) {
  3384. err = -EBUSY;
  3385. goto err_free;
  3386. }
  3387. if (ifmgd->auth_data) {
  3388. bool match;
  3389. /* keep sta info, bssid if matching */
  3390. match = ether_addr_equal(ifmgd->bssid, req->bss->bssid);
  3391. ieee80211_destroy_auth_data(sdata, match);
  3392. }
  3393. /* prepare assoc data */
  3394. ifmgd->beacon_crc_valid = false;
  3395. assoc_data->wmm = bss->wmm_used &&
  3396. (local->hw.queues >= IEEE80211_NUM_ACS);
  3397. if (assoc_data->wmm) {
  3398. /* try to check validity of WMM params IE */
  3399. const struct cfg80211_bss_ies *ies;
  3400. const u8 *wp, *start, *end;
  3401. rcu_read_lock();
  3402. ies = rcu_dereference(req->bss->ies);
  3403. start = ies->data;
  3404. end = start + ies->len;
  3405. while (true) {
  3406. wp = cfg80211_find_vendor_ie(
  3407. WLAN_OUI_MICROSOFT,
  3408. WLAN_OUI_TYPE_MICROSOFT_WMM,
  3409. start, end - start);
  3410. if (!wp)
  3411. break;
  3412. start = wp + wp[1] + 2;
  3413. /* if this IE is too short, try the next */
  3414. if (wp[1] <= 4)
  3415. continue;
  3416. /* if this IE is WMM params, we found what we wanted */
  3417. if (wp[6] == 1)
  3418. break;
  3419. }
  3420. if (!wp || !ieee80211_usable_wmm_params(sdata, wp + 2,
  3421. wp[1] - 2)) {
  3422. assoc_data->wmm = false;
  3423. ifmgd->flags |= IEEE80211_STA_DISABLE_WMM;
  3424. }
  3425. rcu_read_unlock();
  3426. }
  3427. /*
  3428. * IEEE802.11n does not allow TKIP/WEP as pairwise ciphers in HT mode.
  3429. * We still associate in non-HT mode (11a/b/g) if any one of these
  3430. * ciphers is configured as pairwise.
  3431. * We can set this to true for non-11n hardware, that'll be checked
  3432. * separately along with the peer capabilities.
  3433. */
  3434. for (i = 0; i < req->crypto.n_ciphers_pairwise; i++) {
  3435. if (req->crypto.ciphers_pairwise[i] == WLAN_CIPHER_SUITE_WEP40 ||
  3436. req->crypto.ciphers_pairwise[i] == WLAN_CIPHER_SUITE_TKIP ||
  3437. req->crypto.ciphers_pairwise[i] == WLAN_CIPHER_SUITE_WEP104) {
  3438. ifmgd->flags |= IEEE80211_STA_DISABLE_HT;
  3439. ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
  3440. netdev_info(sdata->dev,
  3441. "disabling HT/VHT due to WEP/TKIP use\n");
  3442. }
  3443. }
  3444. if (req->flags & ASSOC_REQ_DISABLE_HT) {
  3445. ifmgd->flags |= IEEE80211_STA_DISABLE_HT;
  3446. ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
  3447. }
  3448. if (req->flags & ASSOC_REQ_DISABLE_VHT)
  3449. ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
  3450. /* Also disable HT if we don't support it or the AP doesn't use WMM */
  3451. sband = local->hw.wiphy->bands[req->bss->channel->band];
  3452. if (!sband->ht_cap.ht_supported ||
  3453. local->hw.queues < IEEE80211_NUM_ACS || !bss->wmm_used ||
  3454. ifmgd->flags & IEEE80211_STA_DISABLE_WMM) {
  3455. ifmgd->flags |= IEEE80211_STA_DISABLE_HT;
  3456. if (!bss->wmm_used &&
  3457. !(ifmgd->flags & IEEE80211_STA_DISABLE_WMM))
  3458. netdev_info(sdata->dev,
  3459. "disabling HT as WMM/QoS is not supported by the AP\n");
  3460. }
  3461. /* disable VHT if we don't support it or the AP doesn't use WMM */
  3462. if (!sband->vht_cap.vht_supported ||
  3463. local->hw.queues < IEEE80211_NUM_ACS || !bss->wmm_used ||
  3464. ifmgd->flags & IEEE80211_STA_DISABLE_WMM) {
  3465. ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
  3466. if (!bss->wmm_used &&
  3467. !(ifmgd->flags & IEEE80211_STA_DISABLE_WMM))
  3468. netdev_info(sdata->dev,
  3469. "disabling VHT as WMM/QoS is not supported by the AP\n");
  3470. }
  3471. memcpy(&ifmgd->ht_capa, &req->ht_capa, sizeof(ifmgd->ht_capa));
  3472. memcpy(&ifmgd->ht_capa_mask, &req->ht_capa_mask,
  3473. sizeof(ifmgd->ht_capa_mask));
  3474. memcpy(&ifmgd->vht_capa, &req->vht_capa, sizeof(ifmgd->vht_capa));
  3475. memcpy(&ifmgd->vht_capa_mask, &req->vht_capa_mask,
  3476. sizeof(ifmgd->vht_capa_mask));
  3477. if (req->ie && req->ie_len) {
  3478. memcpy(assoc_data->ie, req->ie, req->ie_len);
  3479. assoc_data->ie_len = req->ie_len;
  3480. }
  3481. assoc_data->bss = req->bss;
  3482. if (ifmgd->req_smps == IEEE80211_SMPS_AUTOMATIC) {
  3483. if (ifmgd->powersave)
  3484. sdata->smps_mode = IEEE80211_SMPS_DYNAMIC;
  3485. else
  3486. sdata->smps_mode = IEEE80211_SMPS_OFF;
  3487. } else
  3488. sdata->smps_mode = ifmgd->req_smps;
  3489. assoc_data->capability = req->bss->capability;
  3490. assoc_data->supp_rates = bss->supp_rates;
  3491. assoc_data->supp_rates_len = bss->supp_rates_len;
  3492. rcu_read_lock();
  3493. ht_ie = ieee80211_bss_get_ie(req->bss, WLAN_EID_HT_OPERATION);
  3494. if (ht_ie && ht_ie[1] >= sizeof(struct ieee80211_ht_operation))
  3495. assoc_data->ap_ht_param =
  3496. ((struct ieee80211_ht_operation *)(ht_ie + 2))->ht_param;
  3497. else
  3498. ifmgd->flags |= IEEE80211_STA_DISABLE_HT;
  3499. vht_ie = ieee80211_bss_get_ie(req->bss, WLAN_EID_VHT_CAPABILITY);
  3500. if (vht_ie && vht_ie[1] >= sizeof(struct ieee80211_vht_cap))
  3501. memcpy(&assoc_data->ap_vht_cap, vht_ie + 2,
  3502. sizeof(struct ieee80211_vht_cap));
  3503. else
  3504. ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
  3505. rcu_read_unlock();
  3506. if (bss->wmm_used && bss->uapsd_supported &&
  3507. (sdata->local->hw.flags & IEEE80211_HW_SUPPORTS_UAPSD) &&
  3508. sdata->wmm_acm != 0xff) {
  3509. assoc_data->uapsd = true;
  3510. ifmgd->flags |= IEEE80211_STA_UAPSD_ENABLED;
  3511. } else {
  3512. assoc_data->uapsd = false;
  3513. ifmgd->flags &= ~IEEE80211_STA_UAPSD_ENABLED;
  3514. }
  3515. if (req->prev_bssid)
  3516. memcpy(assoc_data->prev_bssid, req->prev_bssid, ETH_ALEN);
  3517. if (req->use_mfp) {
  3518. ifmgd->mfp = IEEE80211_MFP_REQUIRED;
  3519. ifmgd->flags |= IEEE80211_STA_MFP_ENABLED;
  3520. } else {
  3521. ifmgd->mfp = IEEE80211_MFP_DISABLED;
  3522. ifmgd->flags &= ~IEEE80211_STA_MFP_ENABLED;
  3523. }
  3524. if (req->crypto.control_port)
  3525. ifmgd->flags |= IEEE80211_STA_CONTROL_PORT;
  3526. else
  3527. ifmgd->flags &= ~IEEE80211_STA_CONTROL_PORT;
  3528. sdata->control_port_protocol = req->crypto.control_port_ethertype;
  3529. sdata->control_port_no_encrypt = req->crypto.control_port_no_encrypt;
  3530. sdata->encrypt_headroom = ieee80211_cs_headroom(local, &req->crypto,
  3531. sdata->vif.type);
  3532. /* kick off associate process */
  3533. ifmgd->assoc_data = assoc_data;
  3534. ifmgd->dtim_period = 0;
  3535. ifmgd->have_beacon = false;
  3536. err = ieee80211_prep_connection(sdata, req->bss, true);
  3537. if (err)
  3538. goto err_clear;
  3539. rcu_read_lock();
  3540. beacon_ies = rcu_dereference(req->bss->beacon_ies);
  3541. if (sdata->local->hw.flags & IEEE80211_HW_NEED_DTIM_BEFORE_ASSOC &&
  3542. !beacon_ies) {
  3543. /*
  3544. * Wait up to one beacon interval ...
  3545. * should this be more if we miss one?
  3546. */
  3547. sdata_info(sdata, "waiting for beacon from %pM\n",
  3548. ifmgd->bssid);
  3549. assoc_data->timeout = TU_TO_EXP_TIME(req->bss->beacon_interval);
  3550. assoc_data->timeout_started = true;
  3551. assoc_data->need_beacon = true;
  3552. } else if (beacon_ies) {
  3553. const u8 *tim_ie = cfg80211_find_ie(WLAN_EID_TIM,
  3554. beacon_ies->data,
  3555. beacon_ies->len);
  3556. u8 dtim_count = 0;
  3557. if (tim_ie && tim_ie[1] >= sizeof(struct ieee80211_tim_ie)) {
  3558. const struct ieee80211_tim_ie *tim;
  3559. tim = (void *)(tim_ie + 2);
  3560. ifmgd->dtim_period = tim->dtim_period;
  3561. dtim_count = tim->dtim_count;
  3562. }
  3563. ifmgd->have_beacon = true;
  3564. assoc_data->timeout = jiffies;
  3565. assoc_data->timeout_started = true;
  3566. if (local->hw.flags & IEEE80211_HW_TIMING_BEACON_ONLY) {
  3567. sdata->vif.bss_conf.sync_tsf = beacon_ies->tsf;
  3568. sdata->vif.bss_conf.sync_device_ts =
  3569. bss->device_ts_beacon;
  3570. sdata->vif.bss_conf.sync_dtim_count = dtim_count;
  3571. }
  3572. } else {
  3573. assoc_data->timeout = jiffies;
  3574. assoc_data->timeout_started = true;
  3575. }
  3576. rcu_read_unlock();
  3577. run_again(sdata, assoc_data->timeout);
  3578. if (bss->corrupt_data) {
  3579. char *corrupt_type = "data";
  3580. if (bss->corrupt_data & IEEE80211_BSS_CORRUPT_BEACON) {
  3581. if (bss->corrupt_data &
  3582. IEEE80211_BSS_CORRUPT_PROBE_RESP)
  3583. corrupt_type = "beacon and probe response";
  3584. else
  3585. corrupt_type = "beacon";
  3586. } else if (bss->corrupt_data & IEEE80211_BSS_CORRUPT_PROBE_RESP)
  3587. corrupt_type = "probe response";
  3588. sdata_info(sdata, "associating with AP with corrupt %s\n",
  3589. corrupt_type);
  3590. }
  3591. return 0;
  3592. err_clear:
  3593. memset(ifmgd->bssid, 0, ETH_ALEN);
  3594. ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BSSID);
  3595. ifmgd->assoc_data = NULL;
  3596. err_free:
  3597. kfree(assoc_data);
  3598. return err;
  3599. }
  3600. int ieee80211_mgd_deauth(struct ieee80211_sub_if_data *sdata,
  3601. struct cfg80211_deauth_request *req)
  3602. {
  3603. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  3604. u8 frame_buf[IEEE80211_DEAUTH_FRAME_LEN];
  3605. bool tx = !req->local_state_change;
  3606. bool report_frame = false;
  3607. sdata_info(sdata,
  3608. "deauthenticating from %pM by local choice (reason=%d)\n",
  3609. req->bssid, req->reason_code);
  3610. if (ifmgd->auth_data) {
  3611. drv_mgd_prepare_tx(sdata->local, sdata);
  3612. ieee80211_send_deauth_disassoc(sdata, req->bssid,
  3613. IEEE80211_STYPE_DEAUTH,
  3614. req->reason_code, tx,
  3615. frame_buf);
  3616. ieee80211_destroy_auth_data(sdata, false);
  3617. report_frame = true;
  3618. goto out;
  3619. }
  3620. if (ifmgd->associated &&
  3621. ether_addr_equal(ifmgd->associated->bssid, req->bssid)) {
  3622. ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DEAUTH,
  3623. req->reason_code, tx, frame_buf);
  3624. report_frame = true;
  3625. }
  3626. out:
  3627. if (report_frame)
  3628. cfg80211_tx_mlme_mgmt(sdata->dev, frame_buf,
  3629. IEEE80211_DEAUTH_FRAME_LEN);
  3630. return 0;
  3631. }
  3632. int ieee80211_mgd_disassoc(struct ieee80211_sub_if_data *sdata,
  3633. struct cfg80211_disassoc_request *req)
  3634. {
  3635. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  3636. u8 bssid[ETH_ALEN];
  3637. u8 frame_buf[IEEE80211_DEAUTH_FRAME_LEN];
  3638. /*
  3639. * cfg80211 should catch this ... but it's racy since
  3640. * we can receive a disassoc frame, process it, hand it
  3641. * to cfg80211 while that's in a locked section already
  3642. * trying to tell us that the user wants to disconnect.
  3643. */
  3644. if (ifmgd->associated != req->bss)
  3645. return -ENOLINK;
  3646. sdata_info(sdata,
  3647. "disassociating from %pM by local choice (reason=%d)\n",
  3648. req->bss->bssid, req->reason_code);
  3649. memcpy(bssid, req->bss->bssid, ETH_ALEN);
  3650. ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DISASSOC,
  3651. req->reason_code, !req->local_state_change,
  3652. frame_buf);
  3653. cfg80211_tx_mlme_mgmt(sdata->dev, frame_buf,
  3654. IEEE80211_DEAUTH_FRAME_LEN);
  3655. return 0;
  3656. }
  3657. void ieee80211_mgd_stop(struct ieee80211_sub_if_data *sdata)
  3658. {
  3659. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  3660. /*
  3661. * Make sure some work items will not run after this,
  3662. * they will not do anything but might not have been
  3663. * cancelled when disconnecting.
  3664. */
  3665. cancel_work_sync(&ifmgd->monitor_work);
  3666. cancel_work_sync(&ifmgd->beacon_connection_loss_work);
  3667. cancel_work_sync(&ifmgd->request_smps_work);
  3668. cancel_work_sync(&ifmgd->csa_connection_drop_work);
  3669. cancel_work_sync(&ifmgd->chswitch_work);
  3670. sdata_lock(sdata);
  3671. if (ifmgd->assoc_data) {
  3672. struct cfg80211_bss *bss = ifmgd->assoc_data->bss;
  3673. ieee80211_destroy_assoc_data(sdata, false);
  3674. cfg80211_assoc_timeout(sdata->dev, bss);
  3675. }
  3676. if (ifmgd->auth_data)
  3677. ieee80211_destroy_auth_data(sdata, false);
  3678. del_timer_sync(&ifmgd->timer);
  3679. sdata_unlock(sdata);
  3680. }
  3681. void ieee80211_cqm_rssi_notify(struct ieee80211_vif *vif,
  3682. enum nl80211_cqm_rssi_threshold_event rssi_event,
  3683. gfp_t gfp)
  3684. {
  3685. struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
  3686. trace_api_cqm_rssi_notify(sdata, rssi_event);
  3687. cfg80211_cqm_rssi_notify(sdata->dev, rssi_event, gfp);
  3688. }
  3689. EXPORT_SYMBOL(ieee80211_cqm_rssi_notify);