mlme.c 111 KB

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