mlme.c 100 KB

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