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