mlme.c 98 KB

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