mlme.c 98 KB

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