mlme.c 71 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563
  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/rtnetlink.h>
  19. #include <linux/pm_qos_params.h>
  20. #include <linux/crc32.h>
  21. #include <linux/slab.h>
  22. #include <net/mac80211.h>
  23. #include <asm/unaligned.h>
  24. #include "ieee80211_i.h"
  25. #include "driver-ops.h"
  26. #include "rate.h"
  27. #include "led.h"
  28. #define IEEE80211_MAX_NULLFUNC_TRIES 2
  29. #define IEEE80211_MAX_PROBE_TRIES 5
  30. /*
  31. * Beacon loss timeout is calculated as N frames times the
  32. * advertised beacon interval. This may need to be somewhat
  33. * higher than what hardware might detect to account for
  34. * delays in the host processing frames. But since we also
  35. * probe on beacon miss before declaring the connection lost
  36. * default to what we want.
  37. */
  38. #define IEEE80211_BEACON_LOSS_COUNT 7
  39. /*
  40. * Time the connection can be idle before we probe
  41. * it to see if we can still talk to the AP.
  42. */
  43. #define IEEE80211_CONNECTION_IDLE_TIME (30 * HZ)
  44. /*
  45. * Time we wait for a probe response after sending
  46. * a probe request because of beacon loss or for
  47. * checking the connection still works.
  48. */
  49. #define IEEE80211_PROBE_WAIT (HZ / 2)
  50. /*
  51. * Weight given to the latest Beacon frame when calculating average signal
  52. * strength for Beacon frames received in the current BSS. This must be
  53. * between 1 and 15.
  54. */
  55. #define IEEE80211_SIGNAL_AVE_WEIGHT 3
  56. /*
  57. * How many Beacon frames need to have been used in average signal strength
  58. * before starting to indicate signal change events.
  59. */
  60. #define IEEE80211_SIGNAL_AVE_MIN_COUNT 4
  61. #define TMR_RUNNING_TIMER 0
  62. #define TMR_RUNNING_CHANSW 1
  63. /*
  64. * All cfg80211 functions have to be called outside a locked
  65. * section so that they can acquire a lock themselves... This
  66. * is much simpler than queuing up things in cfg80211, but we
  67. * do need some indirection for that here.
  68. */
  69. enum rx_mgmt_action {
  70. /* no action required */
  71. RX_MGMT_NONE,
  72. /* caller must call cfg80211_send_rx_auth() */
  73. RX_MGMT_CFG80211_AUTH,
  74. /* caller must call cfg80211_send_rx_assoc() */
  75. RX_MGMT_CFG80211_ASSOC,
  76. /* caller must call cfg80211_send_deauth() */
  77. RX_MGMT_CFG80211_DEAUTH,
  78. /* caller must call cfg80211_send_disassoc() */
  79. RX_MGMT_CFG80211_DISASSOC,
  80. /* caller must tell cfg80211 about internal error */
  81. RX_MGMT_CFG80211_ASSOC_ERROR,
  82. };
  83. /* utils */
  84. static inline void ASSERT_MGD_MTX(struct ieee80211_if_managed *ifmgd)
  85. {
  86. lockdep_assert_held(&ifmgd->mtx);
  87. }
  88. /*
  89. * We can have multiple work items (and connection probing)
  90. * scheduling this timer, but we need to take care to only
  91. * reschedule it when it should fire _earlier_ than it was
  92. * asked for before, or if it's not pending right now. This
  93. * function ensures that. Note that it then is required to
  94. * run this function for all timeouts after the first one
  95. * has happened -- the work that runs from this timer will
  96. * do that.
  97. */
  98. static void run_again(struct ieee80211_if_managed *ifmgd,
  99. unsigned long timeout)
  100. {
  101. ASSERT_MGD_MTX(ifmgd);
  102. if (!timer_pending(&ifmgd->timer) ||
  103. time_before(timeout, ifmgd->timer.expires))
  104. mod_timer(&ifmgd->timer, timeout);
  105. }
  106. void ieee80211_sta_reset_beacon_monitor(struct ieee80211_sub_if_data *sdata)
  107. {
  108. if (sdata->local->hw.flags & IEEE80211_HW_BEACON_FILTER)
  109. return;
  110. mod_timer(&sdata->u.mgd.bcn_mon_timer,
  111. round_jiffies_up(jiffies + sdata->u.mgd.beacon_timeout));
  112. }
  113. void ieee80211_sta_reset_conn_monitor(struct ieee80211_sub_if_data *sdata)
  114. {
  115. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  116. if (sdata->local->hw.flags & IEEE80211_HW_CONNECTION_MONITOR)
  117. return;
  118. mod_timer(&sdata->u.mgd.conn_mon_timer,
  119. round_jiffies_up(jiffies + IEEE80211_CONNECTION_IDLE_TIME));
  120. ifmgd->probe_send_count = 0;
  121. }
  122. static int ecw2cw(int ecw)
  123. {
  124. return (1 << ecw) - 1;
  125. }
  126. /*
  127. * ieee80211_enable_ht should be called only after the operating band
  128. * has been determined as ht configuration depends on the hw's
  129. * HT abilities for a specific band.
  130. */
  131. static u32 ieee80211_enable_ht(struct ieee80211_sub_if_data *sdata,
  132. struct ieee80211_ht_info *hti,
  133. const u8 *bssid, u16 ap_ht_cap_flags)
  134. {
  135. struct ieee80211_local *local = sdata->local;
  136. struct ieee80211_supported_band *sband;
  137. struct sta_info *sta;
  138. u32 changed = 0;
  139. u16 ht_opmode;
  140. bool enable_ht = true;
  141. enum nl80211_channel_type prev_chantype;
  142. enum nl80211_channel_type channel_type = NL80211_CHAN_NO_HT;
  143. sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
  144. prev_chantype = sdata->vif.bss_conf.channel_type;
  145. /* HT is not supported */
  146. if (!sband->ht_cap.ht_supported)
  147. enable_ht = false;
  148. /* check that channel matches the right operating channel */
  149. if (local->hw.conf.channel->center_freq !=
  150. ieee80211_channel_to_frequency(hti->control_chan))
  151. enable_ht = false;
  152. if (enable_ht) {
  153. channel_type = NL80211_CHAN_HT20;
  154. if (!(ap_ht_cap_flags & IEEE80211_HT_CAP_40MHZ_INTOLERANT) &&
  155. (sband->ht_cap.cap & IEEE80211_HT_CAP_SUP_WIDTH_20_40) &&
  156. (hti->ht_param & IEEE80211_HT_PARAM_CHAN_WIDTH_ANY)) {
  157. switch(hti->ht_param & IEEE80211_HT_PARAM_CHA_SEC_OFFSET) {
  158. case IEEE80211_HT_PARAM_CHA_SEC_ABOVE:
  159. if (!(local->hw.conf.channel->flags &
  160. IEEE80211_CHAN_NO_HT40PLUS))
  161. channel_type = NL80211_CHAN_HT40PLUS;
  162. break;
  163. case IEEE80211_HT_PARAM_CHA_SEC_BELOW:
  164. if (!(local->hw.conf.channel->flags &
  165. IEEE80211_CHAN_NO_HT40MINUS))
  166. channel_type = NL80211_CHAN_HT40MINUS;
  167. break;
  168. }
  169. }
  170. }
  171. if (local->tmp_channel)
  172. local->tmp_channel_type = channel_type;
  173. if (!ieee80211_set_channel_type(local, sdata, channel_type)) {
  174. /* can only fail due to HT40+/- mismatch */
  175. channel_type = NL80211_CHAN_HT20;
  176. WARN_ON(!ieee80211_set_channel_type(local, sdata, channel_type));
  177. }
  178. /* channel_type change automatically detected */
  179. ieee80211_hw_config(local, 0);
  180. if (prev_chantype != channel_type) {
  181. rcu_read_lock();
  182. sta = sta_info_get(sdata, bssid);
  183. if (sta)
  184. rate_control_rate_update(local, sband, sta,
  185. IEEE80211_RC_HT_CHANGED,
  186. channel_type);
  187. rcu_read_unlock();
  188. }
  189. ht_opmode = le16_to_cpu(hti->operation_mode);
  190. /* if bss configuration changed store the new one */
  191. if (sdata->ht_opmode_valid != enable_ht ||
  192. sdata->vif.bss_conf.ht_operation_mode != ht_opmode ||
  193. prev_chantype != channel_type) {
  194. changed |= BSS_CHANGED_HT;
  195. sdata->vif.bss_conf.ht_operation_mode = ht_opmode;
  196. sdata->ht_opmode_valid = enable_ht;
  197. }
  198. return changed;
  199. }
  200. /* frame sending functions */
  201. static void ieee80211_send_deauth_disassoc(struct ieee80211_sub_if_data *sdata,
  202. const u8 *bssid, u16 stype, u16 reason,
  203. void *cookie, bool send_frame)
  204. {
  205. struct ieee80211_local *local = sdata->local;
  206. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  207. struct sk_buff *skb;
  208. struct ieee80211_mgmt *mgmt;
  209. skb = dev_alloc_skb(local->hw.extra_tx_headroom + sizeof(*mgmt));
  210. if (!skb) {
  211. printk(KERN_DEBUG "%s: failed to allocate buffer for "
  212. "deauth/disassoc frame\n", sdata->name);
  213. return;
  214. }
  215. skb_reserve(skb, local->hw.extra_tx_headroom);
  216. mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
  217. memset(mgmt, 0, 24);
  218. memcpy(mgmt->da, bssid, ETH_ALEN);
  219. memcpy(mgmt->sa, sdata->vif.addr, ETH_ALEN);
  220. memcpy(mgmt->bssid, bssid, ETH_ALEN);
  221. mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT | stype);
  222. skb_put(skb, 2);
  223. /* u.deauth.reason_code == u.disassoc.reason_code */
  224. mgmt->u.deauth.reason_code = cpu_to_le16(reason);
  225. if (stype == IEEE80211_STYPE_DEAUTH)
  226. if (cookie)
  227. __cfg80211_send_deauth(sdata->dev, (u8 *)mgmt, skb->len);
  228. else
  229. cfg80211_send_deauth(sdata->dev, (u8 *)mgmt, skb->len);
  230. else
  231. if (cookie)
  232. __cfg80211_send_disassoc(sdata->dev, (u8 *)mgmt, skb->len);
  233. else
  234. cfg80211_send_disassoc(sdata->dev, (u8 *)mgmt, skb->len);
  235. if (!(ifmgd->flags & IEEE80211_STA_MFP_ENABLED))
  236. IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
  237. if (send_frame)
  238. ieee80211_tx_skb(sdata, skb);
  239. else
  240. kfree_skb(skb);
  241. }
  242. void ieee80211_send_pspoll(struct ieee80211_local *local,
  243. struct ieee80211_sub_if_data *sdata)
  244. {
  245. struct ieee80211_pspoll *pspoll;
  246. struct sk_buff *skb;
  247. skb = ieee80211_pspoll_get(&local->hw, &sdata->vif);
  248. if (!skb)
  249. return;
  250. pspoll = (struct ieee80211_pspoll *) skb->data;
  251. pspoll->frame_control |= cpu_to_le16(IEEE80211_FCTL_PM);
  252. IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
  253. ieee80211_tx_skb(sdata, skb);
  254. }
  255. void ieee80211_send_nullfunc(struct ieee80211_local *local,
  256. struct ieee80211_sub_if_data *sdata,
  257. int powersave)
  258. {
  259. struct sk_buff *skb;
  260. struct ieee80211_hdr_3addr *nullfunc;
  261. skb = ieee80211_nullfunc_get(&local->hw, &sdata->vif);
  262. if (!skb)
  263. return;
  264. nullfunc = (struct ieee80211_hdr_3addr *) skb->data;
  265. if (powersave)
  266. nullfunc->frame_control |= cpu_to_le16(IEEE80211_FCTL_PM);
  267. IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
  268. ieee80211_tx_skb(sdata, skb);
  269. }
  270. static void ieee80211_send_4addr_nullfunc(struct ieee80211_local *local,
  271. struct ieee80211_sub_if_data *sdata)
  272. {
  273. struct sk_buff *skb;
  274. struct ieee80211_hdr *nullfunc;
  275. __le16 fc;
  276. if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_STATION))
  277. return;
  278. skb = dev_alloc_skb(local->hw.extra_tx_headroom + 30);
  279. if (!skb) {
  280. printk(KERN_DEBUG "%s: failed to allocate buffer for 4addr "
  281. "nullfunc frame\n", sdata->name);
  282. return;
  283. }
  284. skb_reserve(skb, local->hw.extra_tx_headroom);
  285. nullfunc = (struct ieee80211_hdr *) skb_put(skb, 30);
  286. memset(nullfunc, 0, 30);
  287. fc = cpu_to_le16(IEEE80211_FTYPE_DATA | IEEE80211_STYPE_NULLFUNC |
  288. IEEE80211_FCTL_FROMDS | IEEE80211_FCTL_TODS);
  289. nullfunc->frame_control = fc;
  290. memcpy(nullfunc->addr1, sdata->u.mgd.bssid, ETH_ALEN);
  291. memcpy(nullfunc->addr2, sdata->vif.addr, ETH_ALEN);
  292. memcpy(nullfunc->addr3, sdata->u.mgd.bssid, ETH_ALEN);
  293. memcpy(nullfunc->addr4, sdata->vif.addr, ETH_ALEN);
  294. IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
  295. ieee80211_tx_skb(sdata, skb);
  296. }
  297. /* spectrum management related things */
  298. static void ieee80211_chswitch_work(struct work_struct *work)
  299. {
  300. struct ieee80211_sub_if_data *sdata =
  301. container_of(work, struct ieee80211_sub_if_data, u.mgd.chswitch_work);
  302. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  303. if (!ieee80211_sdata_running(sdata))
  304. return;
  305. mutex_lock(&ifmgd->mtx);
  306. if (!ifmgd->associated)
  307. goto out;
  308. sdata->local->oper_channel = sdata->local->csa_channel;
  309. if (!sdata->local->ops->channel_switch) {
  310. /* call "hw_config" only if doing sw channel switch */
  311. ieee80211_hw_config(sdata->local,
  312. IEEE80211_CONF_CHANGE_CHANNEL);
  313. }
  314. /* XXX: shouldn't really modify cfg80211-owned data! */
  315. ifmgd->associated->channel = sdata->local->oper_channel;
  316. ieee80211_wake_queues_by_reason(&sdata->local->hw,
  317. IEEE80211_QUEUE_STOP_REASON_CSA);
  318. out:
  319. ifmgd->flags &= ~IEEE80211_STA_CSA_RECEIVED;
  320. mutex_unlock(&ifmgd->mtx);
  321. }
  322. void ieee80211_chswitch_done(struct ieee80211_vif *vif, bool success)
  323. {
  324. struct ieee80211_sub_if_data *sdata;
  325. struct ieee80211_if_managed *ifmgd;
  326. sdata = vif_to_sdata(vif);
  327. ifmgd = &sdata->u.mgd;
  328. trace_api_chswitch_done(sdata, success);
  329. if (!success) {
  330. /*
  331. * If the channel switch was not successful, stay
  332. * around on the old channel. We currently lack
  333. * good handling of this situation, possibly we
  334. * should just drop the association.
  335. */
  336. sdata->local->csa_channel = sdata->local->oper_channel;
  337. }
  338. ieee80211_queue_work(&sdata->local->hw, &ifmgd->chswitch_work);
  339. }
  340. EXPORT_SYMBOL(ieee80211_chswitch_done);
  341. static void ieee80211_chswitch_timer(unsigned long data)
  342. {
  343. struct ieee80211_sub_if_data *sdata =
  344. (struct ieee80211_sub_if_data *) data;
  345. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  346. if (sdata->local->quiescing) {
  347. set_bit(TMR_RUNNING_CHANSW, &ifmgd->timers_running);
  348. return;
  349. }
  350. ieee80211_queue_work(&sdata->local->hw, &ifmgd->chswitch_work);
  351. }
  352. void ieee80211_sta_process_chanswitch(struct ieee80211_sub_if_data *sdata,
  353. struct ieee80211_channel_sw_ie *sw_elem,
  354. struct ieee80211_bss *bss,
  355. u64 timestamp)
  356. {
  357. struct cfg80211_bss *cbss =
  358. container_of((void *)bss, struct cfg80211_bss, priv);
  359. struct ieee80211_channel *new_ch;
  360. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  361. int new_freq = ieee80211_channel_to_frequency(sw_elem->new_ch_num);
  362. ASSERT_MGD_MTX(ifmgd);
  363. if (!ifmgd->associated)
  364. return;
  365. if (sdata->local->scanning)
  366. return;
  367. /* Disregard subsequent beacons if we are already running a timer
  368. processing a CSA */
  369. if (ifmgd->flags & IEEE80211_STA_CSA_RECEIVED)
  370. return;
  371. new_ch = ieee80211_get_channel(sdata->local->hw.wiphy, new_freq);
  372. if (!new_ch || new_ch->flags & IEEE80211_CHAN_DISABLED)
  373. return;
  374. sdata->local->csa_channel = new_ch;
  375. if (sdata->local->ops->channel_switch) {
  376. /* use driver's channel switch callback */
  377. struct ieee80211_channel_switch ch_switch;
  378. memset(&ch_switch, 0, sizeof(ch_switch));
  379. ch_switch.timestamp = timestamp;
  380. if (sw_elem->mode) {
  381. ch_switch.block_tx = true;
  382. ieee80211_stop_queues_by_reason(&sdata->local->hw,
  383. IEEE80211_QUEUE_STOP_REASON_CSA);
  384. }
  385. ch_switch.channel = new_ch;
  386. ch_switch.count = sw_elem->count;
  387. ifmgd->flags |= IEEE80211_STA_CSA_RECEIVED;
  388. drv_channel_switch(sdata->local, &ch_switch);
  389. return;
  390. }
  391. /* channel switch handled in software */
  392. if (sw_elem->count <= 1) {
  393. ieee80211_queue_work(&sdata->local->hw, &ifmgd->chswitch_work);
  394. } else {
  395. if (sw_elem->mode)
  396. ieee80211_stop_queues_by_reason(&sdata->local->hw,
  397. IEEE80211_QUEUE_STOP_REASON_CSA);
  398. ifmgd->flags |= IEEE80211_STA_CSA_RECEIVED;
  399. mod_timer(&ifmgd->chswitch_timer,
  400. jiffies +
  401. msecs_to_jiffies(sw_elem->count *
  402. cbss->beacon_interval));
  403. }
  404. }
  405. static void ieee80211_handle_pwr_constr(struct ieee80211_sub_if_data *sdata,
  406. u16 capab_info, u8 *pwr_constr_elem,
  407. u8 pwr_constr_elem_len)
  408. {
  409. struct ieee80211_conf *conf = &sdata->local->hw.conf;
  410. if (!(capab_info & WLAN_CAPABILITY_SPECTRUM_MGMT))
  411. return;
  412. /* Power constraint IE length should be 1 octet */
  413. if (pwr_constr_elem_len != 1)
  414. return;
  415. if ((*pwr_constr_elem <= conf->channel->max_power) &&
  416. (*pwr_constr_elem != sdata->local->power_constr_level)) {
  417. sdata->local->power_constr_level = *pwr_constr_elem;
  418. ieee80211_hw_config(sdata->local, 0);
  419. }
  420. }
  421. void ieee80211_enable_dyn_ps(struct ieee80211_vif *vif)
  422. {
  423. struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
  424. struct ieee80211_local *local = sdata->local;
  425. struct ieee80211_conf *conf = &local->hw.conf;
  426. WARN_ON(sdata->vif.type != NL80211_IFTYPE_STATION ||
  427. !(local->hw.flags & IEEE80211_HW_SUPPORTS_PS) ||
  428. (local->hw.flags & IEEE80211_HW_SUPPORTS_DYNAMIC_PS));
  429. local->disable_dynamic_ps = false;
  430. conf->dynamic_ps_timeout = local->dynamic_ps_user_timeout;
  431. }
  432. EXPORT_SYMBOL(ieee80211_enable_dyn_ps);
  433. void ieee80211_disable_dyn_ps(struct ieee80211_vif *vif)
  434. {
  435. struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
  436. struct ieee80211_local *local = sdata->local;
  437. struct ieee80211_conf *conf = &local->hw.conf;
  438. WARN_ON(sdata->vif.type != NL80211_IFTYPE_STATION ||
  439. !(local->hw.flags & IEEE80211_HW_SUPPORTS_PS) ||
  440. (local->hw.flags & IEEE80211_HW_SUPPORTS_DYNAMIC_PS));
  441. local->disable_dynamic_ps = true;
  442. conf->dynamic_ps_timeout = 0;
  443. del_timer_sync(&local->dynamic_ps_timer);
  444. ieee80211_queue_work(&local->hw,
  445. &local->dynamic_ps_enable_work);
  446. }
  447. EXPORT_SYMBOL(ieee80211_disable_dyn_ps);
  448. /* powersave */
  449. static void ieee80211_enable_ps(struct ieee80211_local *local,
  450. struct ieee80211_sub_if_data *sdata)
  451. {
  452. struct ieee80211_conf *conf = &local->hw.conf;
  453. /*
  454. * If we are scanning right now then the parameters will
  455. * take effect when scan finishes.
  456. */
  457. if (local->scanning)
  458. return;
  459. if (conf->dynamic_ps_timeout > 0 &&
  460. !(local->hw.flags & IEEE80211_HW_SUPPORTS_DYNAMIC_PS)) {
  461. mod_timer(&local->dynamic_ps_timer, jiffies +
  462. msecs_to_jiffies(conf->dynamic_ps_timeout));
  463. } else {
  464. if (local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK)
  465. ieee80211_send_nullfunc(local, sdata, 1);
  466. if ((local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK) &&
  467. (local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS))
  468. return;
  469. conf->flags |= IEEE80211_CONF_PS;
  470. ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
  471. }
  472. }
  473. static void ieee80211_change_ps(struct ieee80211_local *local)
  474. {
  475. struct ieee80211_conf *conf = &local->hw.conf;
  476. if (local->ps_sdata) {
  477. ieee80211_enable_ps(local, local->ps_sdata);
  478. } else if (conf->flags & IEEE80211_CONF_PS) {
  479. conf->flags &= ~IEEE80211_CONF_PS;
  480. ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
  481. del_timer_sync(&local->dynamic_ps_timer);
  482. cancel_work_sync(&local->dynamic_ps_enable_work);
  483. }
  484. }
  485. /* need to hold RTNL or interface lock */
  486. void ieee80211_recalc_ps(struct ieee80211_local *local, s32 latency)
  487. {
  488. struct ieee80211_sub_if_data *sdata, *found = NULL;
  489. int count = 0;
  490. int timeout;
  491. if (!(local->hw.flags & IEEE80211_HW_SUPPORTS_PS)) {
  492. local->ps_sdata = NULL;
  493. return;
  494. }
  495. if (!list_empty(&local->work_list)) {
  496. local->ps_sdata = NULL;
  497. goto change;
  498. }
  499. list_for_each_entry(sdata, &local->interfaces, list) {
  500. if (!ieee80211_sdata_running(sdata))
  501. continue;
  502. if (sdata->vif.type != NL80211_IFTYPE_STATION)
  503. continue;
  504. found = sdata;
  505. count++;
  506. }
  507. if (count == 1 && found->u.mgd.powersave &&
  508. found->u.mgd.associated &&
  509. found->u.mgd.associated->beacon_ies &&
  510. !(found->u.mgd.flags & (IEEE80211_STA_BEACON_POLL |
  511. IEEE80211_STA_CONNECTION_POLL))) {
  512. struct ieee80211_conf *conf = &local->hw.conf;
  513. s32 beaconint_us;
  514. if (latency < 0)
  515. latency = pm_qos_request(PM_QOS_NETWORK_LATENCY);
  516. beaconint_us = ieee80211_tu_to_usec(
  517. found->vif.bss_conf.beacon_int);
  518. timeout = local->dynamic_ps_forced_timeout;
  519. if (timeout < 0) {
  520. /*
  521. * Go to full PSM if the user configures a very low
  522. * latency requirement.
  523. * The 2000 second value is there for compatibility
  524. * until the PM_QOS_NETWORK_LATENCY is configured
  525. * with real values.
  526. */
  527. if (latency > (1900 * USEC_PER_MSEC) &&
  528. latency != (2000 * USEC_PER_SEC))
  529. timeout = 0;
  530. else
  531. timeout = 100;
  532. }
  533. local->dynamic_ps_user_timeout = timeout;
  534. if (!local->disable_dynamic_ps)
  535. conf->dynamic_ps_timeout =
  536. local->dynamic_ps_user_timeout;
  537. if (beaconint_us > latency) {
  538. local->ps_sdata = NULL;
  539. } else {
  540. struct ieee80211_bss *bss;
  541. int maxslp = 1;
  542. u8 dtimper;
  543. bss = (void *)found->u.mgd.associated->priv;
  544. dtimper = bss->dtim_period;
  545. /* If the TIM IE is invalid, pretend the value is 1 */
  546. if (!dtimper)
  547. dtimper = 1;
  548. else if (dtimper > 1)
  549. maxslp = min_t(int, dtimper,
  550. latency / beaconint_us);
  551. local->hw.conf.max_sleep_period = maxslp;
  552. local->hw.conf.ps_dtim_period = dtimper;
  553. local->ps_sdata = found;
  554. }
  555. } else {
  556. local->ps_sdata = NULL;
  557. }
  558. change:
  559. ieee80211_change_ps(local);
  560. }
  561. void ieee80211_dynamic_ps_disable_work(struct work_struct *work)
  562. {
  563. struct ieee80211_local *local =
  564. container_of(work, struct ieee80211_local,
  565. dynamic_ps_disable_work);
  566. if (local->hw.conf.flags & IEEE80211_CONF_PS) {
  567. local->hw.conf.flags &= ~IEEE80211_CONF_PS;
  568. ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
  569. }
  570. ieee80211_wake_queues_by_reason(&local->hw,
  571. IEEE80211_QUEUE_STOP_REASON_PS);
  572. }
  573. void ieee80211_dynamic_ps_enable_work(struct work_struct *work)
  574. {
  575. struct ieee80211_local *local =
  576. container_of(work, struct ieee80211_local,
  577. dynamic_ps_enable_work);
  578. struct ieee80211_sub_if_data *sdata = local->ps_sdata;
  579. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  580. /* can only happen when PS was just disabled anyway */
  581. if (!sdata)
  582. return;
  583. if (local->hw.conf.flags & IEEE80211_CONF_PS)
  584. return;
  585. if ((local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK) &&
  586. (!(ifmgd->flags & IEEE80211_STA_NULLFUNC_ACKED)))
  587. ieee80211_send_nullfunc(local, sdata, 1);
  588. if (!((local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS) &&
  589. (local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK)) ||
  590. (ifmgd->flags & IEEE80211_STA_NULLFUNC_ACKED)) {
  591. ifmgd->flags &= ~IEEE80211_STA_NULLFUNC_ACKED;
  592. local->hw.conf.flags |= IEEE80211_CONF_PS;
  593. ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
  594. }
  595. }
  596. void ieee80211_dynamic_ps_timer(unsigned long data)
  597. {
  598. struct ieee80211_local *local = (void *) data;
  599. if (local->quiescing || local->suspended)
  600. return;
  601. ieee80211_queue_work(&local->hw, &local->dynamic_ps_enable_work);
  602. }
  603. /* MLME */
  604. static void ieee80211_sta_wmm_params(struct ieee80211_local *local,
  605. struct ieee80211_sub_if_data *sdata,
  606. u8 *wmm_param, size_t wmm_param_len)
  607. {
  608. struct ieee80211_tx_queue_params params;
  609. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  610. size_t left;
  611. int count;
  612. u8 *pos, uapsd_queues = 0;
  613. if (!local->ops->conf_tx)
  614. return;
  615. if (local->hw.queues < 4)
  616. return;
  617. if (!wmm_param)
  618. return;
  619. if (wmm_param_len < 8 || wmm_param[5] /* version */ != 1)
  620. return;
  621. if (ifmgd->flags & IEEE80211_STA_UAPSD_ENABLED)
  622. uapsd_queues = local->uapsd_queues;
  623. count = wmm_param[6] & 0x0f;
  624. if (count == ifmgd->wmm_last_param_set)
  625. return;
  626. ifmgd->wmm_last_param_set = count;
  627. pos = wmm_param + 8;
  628. left = wmm_param_len - 8;
  629. memset(&params, 0, sizeof(params));
  630. local->wmm_acm = 0;
  631. for (; left >= 4; left -= 4, pos += 4) {
  632. int aci = (pos[0] >> 5) & 0x03;
  633. int acm = (pos[0] >> 4) & 0x01;
  634. bool uapsd = false;
  635. int queue;
  636. switch (aci) {
  637. case 1: /* AC_BK */
  638. queue = 3;
  639. if (acm)
  640. local->wmm_acm |= BIT(1) | BIT(2); /* BK/- */
  641. if (uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_BK)
  642. uapsd = true;
  643. break;
  644. case 2: /* AC_VI */
  645. queue = 1;
  646. if (acm)
  647. local->wmm_acm |= BIT(4) | BIT(5); /* CL/VI */
  648. if (uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_VI)
  649. uapsd = true;
  650. break;
  651. case 3: /* AC_VO */
  652. queue = 0;
  653. if (acm)
  654. local->wmm_acm |= BIT(6) | BIT(7); /* VO/NC */
  655. if (uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_VO)
  656. uapsd = true;
  657. break;
  658. case 0: /* AC_BE */
  659. default:
  660. queue = 2;
  661. if (acm)
  662. local->wmm_acm |= BIT(0) | BIT(3); /* BE/EE */
  663. if (uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_BE)
  664. uapsd = true;
  665. break;
  666. }
  667. params.aifs = pos[0] & 0x0f;
  668. params.cw_max = ecw2cw((pos[1] & 0xf0) >> 4);
  669. params.cw_min = ecw2cw(pos[1] & 0x0f);
  670. params.txop = get_unaligned_le16(pos + 2);
  671. params.uapsd = uapsd;
  672. #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
  673. wiphy_debug(local->hw.wiphy,
  674. "WMM queue=%d aci=%d acm=%d aifs=%d "
  675. "cWmin=%d cWmax=%d txop=%d uapsd=%d\n",
  676. queue, aci, acm,
  677. params.aifs, params.cw_min, params.cw_max,
  678. params.txop, params.uapsd);
  679. #endif
  680. if (drv_conf_tx(local, queue, &params))
  681. wiphy_debug(local->hw.wiphy,
  682. "failed to set TX queue parameters for queue %d\n",
  683. queue);
  684. }
  685. /* enable WMM or activate new settings */
  686. sdata->vif.bss_conf.qos = true;
  687. ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_QOS);
  688. }
  689. static u32 ieee80211_handle_bss_capability(struct ieee80211_sub_if_data *sdata,
  690. u16 capab, bool erp_valid, u8 erp)
  691. {
  692. struct ieee80211_bss_conf *bss_conf = &sdata->vif.bss_conf;
  693. u32 changed = 0;
  694. bool use_protection;
  695. bool use_short_preamble;
  696. bool use_short_slot;
  697. if (erp_valid) {
  698. use_protection = (erp & WLAN_ERP_USE_PROTECTION) != 0;
  699. use_short_preamble = (erp & WLAN_ERP_BARKER_PREAMBLE) == 0;
  700. } else {
  701. use_protection = false;
  702. use_short_preamble = !!(capab & WLAN_CAPABILITY_SHORT_PREAMBLE);
  703. }
  704. use_short_slot = !!(capab & WLAN_CAPABILITY_SHORT_SLOT_TIME);
  705. if (sdata->local->hw.conf.channel->band == IEEE80211_BAND_5GHZ)
  706. use_short_slot = true;
  707. if (use_protection != bss_conf->use_cts_prot) {
  708. bss_conf->use_cts_prot = use_protection;
  709. changed |= BSS_CHANGED_ERP_CTS_PROT;
  710. }
  711. if (use_short_preamble != bss_conf->use_short_preamble) {
  712. bss_conf->use_short_preamble = use_short_preamble;
  713. changed |= BSS_CHANGED_ERP_PREAMBLE;
  714. }
  715. if (use_short_slot != bss_conf->use_short_slot) {
  716. bss_conf->use_short_slot = use_short_slot;
  717. changed |= BSS_CHANGED_ERP_SLOT;
  718. }
  719. return changed;
  720. }
  721. static void ieee80211_set_associated(struct ieee80211_sub_if_data *sdata,
  722. struct cfg80211_bss *cbss,
  723. u32 bss_info_changed)
  724. {
  725. struct ieee80211_bss *bss = (void *)cbss->priv;
  726. struct ieee80211_local *local = sdata->local;
  727. struct ieee80211_bss_conf *bss_conf = &sdata->vif.bss_conf;
  728. bss_info_changed |= BSS_CHANGED_ASSOC;
  729. /* set timing information */
  730. bss_conf->beacon_int = cbss->beacon_interval;
  731. bss_conf->timestamp = cbss->tsf;
  732. bss_info_changed |= BSS_CHANGED_BEACON_INT;
  733. bss_info_changed |= ieee80211_handle_bss_capability(sdata,
  734. cbss->capability, bss->has_erp_value, bss->erp_value);
  735. sdata->u.mgd.beacon_timeout = usecs_to_jiffies(ieee80211_tu_to_usec(
  736. IEEE80211_BEACON_LOSS_COUNT * bss_conf->beacon_int));
  737. sdata->u.mgd.associated = cbss;
  738. memcpy(sdata->u.mgd.bssid, cbss->bssid, ETH_ALEN);
  739. sdata->u.mgd.flags |= IEEE80211_STA_RESET_SIGNAL_AVE;
  740. /* just to be sure */
  741. sdata->u.mgd.flags &= ~(IEEE80211_STA_CONNECTION_POLL |
  742. IEEE80211_STA_BEACON_POLL);
  743. ieee80211_led_assoc(local, 1);
  744. if (local->hw.flags & IEEE80211_HW_NEED_DTIM_PERIOD)
  745. bss_conf->dtim_period = bss->dtim_period;
  746. else
  747. bss_conf->dtim_period = 0;
  748. bss_conf->assoc = 1;
  749. /*
  750. * For now just always ask the driver to update the basic rateset
  751. * when we have associated, we aren't checking whether it actually
  752. * changed or not.
  753. */
  754. bss_info_changed |= BSS_CHANGED_BASIC_RATES;
  755. /* And the BSSID changed - we're associated now */
  756. bss_info_changed |= BSS_CHANGED_BSSID;
  757. /* Tell the driver to monitor connection quality (if supported) */
  758. if ((local->hw.flags & IEEE80211_HW_SUPPORTS_CQM_RSSI) &&
  759. bss_conf->cqm_rssi_thold)
  760. bss_info_changed |= BSS_CHANGED_CQM;
  761. /* Enable ARP filtering */
  762. if (bss_conf->arp_filter_enabled != sdata->arp_filter_state) {
  763. bss_conf->arp_filter_enabled = sdata->arp_filter_state;
  764. bss_info_changed |= BSS_CHANGED_ARP_FILTER;
  765. }
  766. ieee80211_bss_info_change_notify(sdata, bss_info_changed);
  767. mutex_lock(&local->iflist_mtx);
  768. ieee80211_recalc_ps(local, -1);
  769. ieee80211_recalc_smps(local);
  770. mutex_unlock(&local->iflist_mtx);
  771. netif_tx_start_all_queues(sdata->dev);
  772. netif_carrier_on(sdata->dev);
  773. }
  774. static void ieee80211_set_disassoc(struct ieee80211_sub_if_data *sdata,
  775. bool remove_sta, bool tx)
  776. {
  777. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  778. struct ieee80211_local *local = sdata->local;
  779. struct sta_info *sta;
  780. u32 changed = 0, config_changed = 0;
  781. u8 bssid[ETH_ALEN];
  782. ASSERT_MGD_MTX(ifmgd);
  783. if (WARN_ON(!ifmgd->associated))
  784. return;
  785. memcpy(bssid, ifmgd->associated->bssid, ETH_ALEN);
  786. ifmgd->associated = NULL;
  787. memset(ifmgd->bssid, 0, ETH_ALEN);
  788. /*
  789. * we need to commit the associated = NULL change because the
  790. * scan code uses that to determine whether this iface should
  791. * go to/wake up from powersave or not -- and could otherwise
  792. * wake the queues erroneously.
  793. */
  794. smp_mb();
  795. /*
  796. * Thus, we can only afterwards stop the queues -- to account
  797. * for the case where another CPU is finishing a scan at this
  798. * time -- we don't want the scan code to enable queues.
  799. */
  800. netif_tx_stop_all_queues(sdata->dev);
  801. netif_carrier_off(sdata->dev);
  802. mutex_lock(&local->sta_mtx);
  803. sta = sta_info_get(sdata, bssid);
  804. if (sta) {
  805. set_sta_flags(sta, WLAN_STA_BLOCK_BA);
  806. ieee80211_sta_tear_down_BA_sessions(sta, tx);
  807. }
  808. mutex_unlock(&local->sta_mtx);
  809. changed |= ieee80211_reset_erp_info(sdata);
  810. ieee80211_led_assoc(local, 0);
  811. changed |= BSS_CHANGED_ASSOC;
  812. sdata->vif.bss_conf.assoc = false;
  813. ieee80211_set_wmm_default(sdata);
  814. /* channel(_type) changes are handled by ieee80211_hw_config */
  815. WARN_ON(!ieee80211_set_channel_type(local, sdata, NL80211_CHAN_NO_HT));
  816. /* on the next assoc, re-program HT parameters */
  817. sdata->ht_opmode_valid = false;
  818. local->power_constr_level = 0;
  819. del_timer_sync(&local->dynamic_ps_timer);
  820. cancel_work_sync(&local->dynamic_ps_enable_work);
  821. if (local->hw.conf.flags & IEEE80211_CONF_PS) {
  822. local->hw.conf.flags &= ~IEEE80211_CONF_PS;
  823. config_changed |= IEEE80211_CONF_CHANGE_PS;
  824. }
  825. ieee80211_hw_config(local, config_changed);
  826. /* Disable ARP filtering */
  827. if (sdata->vif.bss_conf.arp_filter_enabled) {
  828. sdata->vif.bss_conf.arp_filter_enabled = false;
  829. changed |= BSS_CHANGED_ARP_FILTER;
  830. }
  831. /* The BSSID (not really interesting) and HT changed */
  832. changed |= BSS_CHANGED_BSSID | BSS_CHANGED_HT;
  833. ieee80211_bss_info_change_notify(sdata, changed);
  834. if (remove_sta)
  835. sta_info_destroy_addr(sdata, bssid);
  836. del_timer_sync(&sdata->u.mgd.conn_mon_timer);
  837. del_timer_sync(&sdata->u.mgd.bcn_mon_timer);
  838. del_timer_sync(&sdata->u.mgd.timer);
  839. del_timer_sync(&sdata->u.mgd.chswitch_timer);
  840. }
  841. void ieee80211_sta_rx_notify(struct ieee80211_sub_if_data *sdata,
  842. struct ieee80211_hdr *hdr)
  843. {
  844. /*
  845. * We can postpone the mgd.timer whenever receiving unicast frames
  846. * from AP because we know that the connection is working both ways
  847. * at that time. But multicast frames (and hence also beacons) must
  848. * be ignored here, because we need to trigger the timer during
  849. * data idle periods for sending the periodic probe request to the
  850. * AP we're connected to.
  851. */
  852. if (is_multicast_ether_addr(hdr->addr1))
  853. return;
  854. /*
  855. * In case we receive frames after disassociation.
  856. */
  857. if (!sdata->u.mgd.associated)
  858. return;
  859. ieee80211_sta_reset_conn_monitor(sdata);
  860. }
  861. static void ieee80211_reset_ap_probe(struct ieee80211_sub_if_data *sdata)
  862. {
  863. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  864. if (!(ifmgd->flags & (IEEE80211_STA_BEACON_POLL |
  865. IEEE80211_STA_CONNECTION_POLL)))
  866. return;
  867. ifmgd->flags &= ~(IEEE80211_STA_CONNECTION_POLL |
  868. IEEE80211_STA_BEACON_POLL);
  869. mutex_lock(&sdata->local->iflist_mtx);
  870. ieee80211_recalc_ps(sdata->local, -1);
  871. mutex_unlock(&sdata->local->iflist_mtx);
  872. if (sdata->local->hw.flags & IEEE80211_HW_CONNECTION_MONITOR)
  873. return;
  874. /*
  875. * We've received a probe response, but are not sure whether
  876. * we have or will be receiving any beacons or data, so let's
  877. * schedule the timers again, just in case.
  878. */
  879. ieee80211_sta_reset_beacon_monitor(sdata);
  880. mod_timer(&ifmgd->conn_mon_timer,
  881. round_jiffies_up(jiffies +
  882. IEEE80211_CONNECTION_IDLE_TIME));
  883. }
  884. void ieee80211_sta_tx_notify(struct ieee80211_sub_if_data *sdata,
  885. struct ieee80211_hdr *hdr, bool ack)
  886. {
  887. if (!ieee80211_is_data(hdr->frame_control))
  888. return;
  889. if (ack)
  890. ieee80211_sta_reset_conn_monitor(sdata);
  891. if (ieee80211_is_nullfunc(hdr->frame_control) &&
  892. sdata->u.mgd.probe_send_count > 0) {
  893. if (ack)
  894. sdata->u.mgd.probe_send_count = 0;
  895. else
  896. sdata->u.mgd.nullfunc_failed = true;
  897. ieee80211_queue_work(&sdata->local->hw, &sdata->work);
  898. }
  899. }
  900. static void ieee80211_mgd_probe_ap_send(struct ieee80211_sub_if_data *sdata)
  901. {
  902. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  903. const u8 *ssid;
  904. u8 *dst = ifmgd->associated->bssid;
  905. u8 unicast_limit = max(1, IEEE80211_MAX_PROBE_TRIES - 3);
  906. /*
  907. * Try sending broadcast probe requests for the last three
  908. * probe requests after the first ones failed since some
  909. * buggy APs only support broadcast probe requests.
  910. */
  911. if (ifmgd->probe_send_count >= unicast_limit)
  912. dst = NULL;
  913. /*
  914. * When the hardware reports an accurate Tx ACK status, it's
  915. * better to send a nullfunc frame instead of a probe request,
  916. * as it will kick us off the AP quickly if we aren't associated
  917. * anymore. The timeout will be reset if the frame is ACKed by
  918. * the AP.
  919. */
  920. if (sdata->local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS) {
  921. ifmgd->nullfunc_failed = false;
  922. ieee80211_send_nullfunc(sdata->local, sdata, 0);
  923. } else {
  924. ssid = ieee80211_bss_get_ie(ifmgd->associated, WLAN_EID_SSID);
  925. ieee80211_send_probe_req(sdata, dst, ssid + 2, ssid[1], NULL, 0);
  926. }
  927. ifmgd->probe_send_count++;
  928. ifmgd->probe_timeout = jiffies + IEEE80211_PROBE_WAIT;
  929. run_again(ifmgd, ifmgd->probe_timeout);
  930. }
  931. static void ieee80211_mgd_probe_ap(struct ieee80211_sub_if_data *sdata,
  932. bool beacon)
  933. {
  934. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  935. bool already = false;
  936. if (!ieee80211_sdata_running(sdata))
  937. return;
  938. if (sdata->local->scanning)
  939. return;
  940. if (sdata->local->tmp_channel)
  941. return;
  942. mutex_lock(&ifmgd->mtx);
  943. if (!ifmgd->associated)
  944. goto out;
  945. #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
  946. if (beacon && net_ratelimit())
  947. printk(KERN_DEBUG "%s: detected beacon loss from AP "
  948. "- sending probe request\n", sdata->name);
  949. #endif
  950. /*
  951. * The driver/our work has already reported this event or the
  952. * connection monitoring has kicked in and we have already sent
  953. * a probe request. Or maybe the AP died and the driver keeps
  954. * reporting until we disassociate...
  955. *
  956. * In either case we have to ignore the current call to this
  957. * function (except for setting the correct probe reason bit)
  958. * because otherwise we would reset the timer every time and
  959. * never check whether we received a probe response!
  960. */
  961. if (ifmgd->flags & (IEEE80211_STA_BEACON_POLL |
  962. IEEE80211_STA_CONNECTION_POLL))
  963. already = true;
  964. if (beacon)
  965. ifmgd->flags |= IEEE80211_STA_BEACON_POLL;
  966. else
  967. ifmgd->flags |= IEEE80211_STA_CONNECTION_POLL;
  968. if (already)
  969. goto out;
  970. mutex_lock(&sdata->local->iflist_mtx);
  971. ieee80211_recalc_ps(sdata->local, -1);
  972. mutex_unlock(&sdata->local->iflist_mtx);
  973. ifmgd->probe_send_count = 0;
  974. ieee80211_mgd_probe_ap_send(sdata);
  975. out:
  976. mutex_unlock(&ifmgd->mtx);
  977. }
  978. struct sk_buff *ieee80211_ap_probereq_get(struct ieee80211_hw *hw,
  979. struct ieee80211_vif *vif)
  980. {
  981. struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
  982. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  983. struct sk_buff *skb;
  984. const u8 *ssid;
  985. if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_STATION))
  986. return NULL;
  987. ASSERT_MGD_MTX(ifmgd);
  988. if (!ifmgd->associated)
  989. return NULL;
  990. ssid = ieee80211_bss_get_ie(ifmgd->associated, WLAN_EID_SSID);
  991. skb = ieee80211_build_probe_req(sdata, ifmgd->associated->bssid,
  992. ssid + 2, ssid[1], NULL, 0);
  993. return skb;
  994. }
  995. EXPORT_SYMBOL(ieee80211_ap_probereq_get);
  996. static void __ieee80211_connection_loss(struct ieee80211_sub_if_data *sdata)
  997. {
  998. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  999. struct ieee80211_local *local = sdata->local;
  1000. u8 bssid[ETH_ALEN];
  1001. mutex_lock(&ifmgd->mtx);
  1002. if (!ifmgd->associated) {
  1003. mutex_unlock(&ifmgd->mtx);
  1004. return;
  1005. }
  1006. memcpy(bssid, ifmgd->associated->bssid, ETH_ALEN);
  1007. printk(KERN_DEBUG "Connection to AP %pM lost.\n", bssid);
  1008. ieee80211_set_disassoc(sdata, true, true);
  1009. mutex_unlock(&ifmgd->mtx);
  1010. mutex_lock(&local->mtx);
  1011. ieee80211_recalc_idle(local);
  1012. mutex_unlock(&local->mtx);
  1013. /*
  1014. * must be outside lock due to cfg80211,
  1015. * but that's not a problem.
  1016. */
  1017. ieee80211_send_deauth_disassoc(sdata, bssid,
  1018. IEEE80211_STYPE_DEAUTH,
  1019. WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY,
  1020. NULL, true);
  1021. }
  1022. void ieee80211_beacon_connection_loss_work(struct work_struct *work)
  1023. {
  1024. struct ieee80211_sub_if_data *sdata =
  1025. container_of(work, struct ieee80211_sub_if_data,
  1026. u.mgd.beacon_connection_loss_work);
  1027. if (sdata->local->hw.flags & IEEE80211_HW_CONNECTION_MONITOR)
  1028. __ieee80211_connection_loss(sdata);
  1029. else
  1030. ieee80211_mgd_probe_ap(sdata, true);
  1031. }
  1032. void ieee80211_beacon_loss(struct ieee80211_vif *vif)
  1033. {
  1034. struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
  1035. struct ieee80211_hw *hw = &sdata->local->hw;
  1036. trace_api_beacon_loss(sdata);
  1037. WARN_ON(hw->flags & IEEE80211_HW_CONNECTION_MONITOR);
  1038. ieee80211_queue_work(hw, &sdata->u.mgd.beacon_connection_loss_work);
  1039. }
  1040. EXPORT_SYMBOL(ieee80211_beacon_loss);
  1041. void ieee80211_connection_loss(struct ieee80211_vif *vif)
  1042. {
  1043. struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
  1044. struct ieee80211_hw *hw = &sdata->local->hw;
  1045. trace_api_connection_loss(sdata);
  1046. WARN_ON(!(hw->flags & IEEE80211_HW_CONNECTION_MONITOR));
  1047. ieee80211_queue_work(hw, &sdata->u.mgd.beacon_connection_loss_work);
  1048. }
  1049. EXPORT_SYMBOL(ieee80211_connection_loss);
  1050. static enum rx_mgmt_action __must_check
  1051. ieee80211_rx_mgmt_deauth(struct ieee80211_sub_if_data *sdata,
  1052. struct ieee80211_mgmt *mgmt, size_t len)
  1053. {
  1054. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  1055. const u8 *bssid = NULL;
  1056. u16 reason_code;
  1057. if (len < 24 + 2)
  1058. return RX_MGMT_NONE;
  1059. ASSERT_MGD_MTX(ifmgd);
  1060. bssid = ifmgd->associated->bssid;
  1061. reason_code = le16_to_cpu(mgmt->u.deauth.reason_code);
  1062. printk(KERN_DEBUG "%s: deauthenticated from %pM (Reason: %u)\n",
  1063. sdata->name, bssid, reason_code);
  1064. ieee80211_set_disassoc(sdata, true, false);
  1065. mutex_lock(&sdata->local->mtx);
  1066. ieee80211_recalc_idle(sdata->local);
  1067. mutex_unlock(&sdata->local->mtx);
  1068. return RX_MGMT_CFG80211_DEAUTH;
  1069. }
  1070. static enum rx_mgmt_action __must_check
  1071. ieee80211_rx_mgmt_disassoc(struct ieee80211_sub_if_data *sdata,
  1072. struct ieee80211_mgmt *mgmt, size_t len)
  1073. {
  1074. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  1075. u16 reason_code;
  1076. if (len < 24 + 2)
  1077. return RX_MGMT_NONE;
  1078. ASSERT_MGD_MTX(ifmgd);
  1079. if (WARN_ON(!ifmgd->associated))
  1080. return RX_MGMT_NONE;
  1081. if (WARN_ON(memcmp(ifmgd->associated->bssid, mgmt->sa, ETH_ALEN)))
  1082. return RX_MGMT_NONE;
  1083. reason_code = le16_to_cpu(mgmt->u.disassoc.reason_code);
  1084. printk(KERN_DEBUG "%s: disassociated from %pM (Reason: %u)\n",
  1085. sdata->name, mgmt->sa, reason_code);
  1086. ieee80211_set_disassoc(sdata, true, false);
  1087. mutex_lock(&sdata->local->mtx);
  1088. ieee80211_recalc_idle(sdata->local);
  1089. mutex_unlock(&sdata->local->mtx);
  1090. return RX_MGMT_CFG80211_DISASSOC;
  1091. }
  1092. static bool ieee80211_assoc_success(struct ieee80211_work *wk,
  1093. struct ieee80211_mgmt *mgmt, size_t len)
  1094. {
  1095. struct ieee80211_sub_if_data *sdata = wk->sdata;
  1096. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  1097. struct ieee80211_local *local = sdata->local;
  1098. struct ieee80211_supported_band *sband;
  1099. struct sta_info *sta;
  1100. struct cfg80211_bss *cbss = wk->assoc.bss;
  1101. u8 *pos;
  1102. u32 rates, basic_rates;
  1103. u16 capab_info, aid;
  1104. struct ieee802_11_elems elems;
  1105. struct ieee80211_bss_conf *bss_conf = &sdata->vif.bss_conf;
  1106. u32 changed = 0;
  1107. int i, j, err;
  1108. bool have_higher_than_11mbit = false;
  1109. u16 ap_ht_cap_flags;
  1110. /* AssocResp and ReassocResp have identical structure */
  1111. aid = le16_to_cpu(mgmt->u.assoc_resp.aid);
  1112. capab_info = le16_to_cpu(mgmt->u.assoc_resp.capab_info);
  1113. if ((aid & (BIT(15) | BIT(14))) != (BIT(15) | BIT(14)))
  1114. printk(KERN_DEBUG "%s: invalid aid value %d; bits 15:14 not "
  1115. "set\n", sdata->name, aid);
  1116. aid &= ~(BIT(15) | BIT(14));
  1117. pos = mgmt->u.assoc_resp.variable;
  1118. ieee802_11_parse_elems(pos, len - (pos - (u8 *) mgmt), &elems);
  1119. if (!elems.supp_rates) {
  1120. printk(KERN_DEBUG "%s: no SuppRates element in AssocResp\n",
  1121. sdata->name);
  1122. return false;
  1123. }
  1124. ifmgd->aid = aid;
  1125. sta = sta_info_alloc(sdata, cbss->bssid, GFP_KERNEL);
  1126. if (!sta) {
  1127. printk(KERN_DEBUG "%s: failed to alloc STA entry for"
  1128. " the AP\n", sdata->name);
  1129. return false;
  1130. }
  1131. set_sta_flags(sta, WLAN_STA_AUTH | WLAN_STA_ASSOC |
  1132. WLAN_STA_ASSOC_AP);
  1133. if (!(ifmgd->flags & IEEE80211_STA_CONTROL_PORT))
  1134. set_sta_flags(sta, WLAN_STA_AUTHORIZED);
  1135. rates = 0;
  1136. basic_rates = 0;
  1137. sband = local->hw.wiphy->bands[wk->chan->band];
  1138. for (i = 0; i < elems.supp_rates_len; i++) {
  1139. int rate = (elems.supp_rates[i] & 0x7f) * 5;
  1140. bool is_basic = !!(elems.supp_rates[i] & 0x80);
  1141. if (rate > 110)
  1142. have_higher_than_11mbit = true;
  1143. for (j = 0; j < sband->n_bitrates; j++) {
  1144. if (sband->bitrates[j].bitrate == rate) {
  1145. rates |= BIT(j);
  1146. if (is_basic)
  1147. basic_rates |= BIT(j);
  1148. break;
  1149. }
  1150. }
  1151. }
  1152. for (i = 0; i < elems.ext_supp_rates_len; i++) {
  1153. int rate = (elems.ext_supp_rates[i] & 0x7f) * 5;
  1154. bool is_basic = !!(elems.ext_supp_rates[i] & 0x80);
  1155. if (rate > 110)
  1156. have_higher_than_11mbit = true;
  1157. for (j = 0; j < sband->n_bitrates; j++) {
  1158. if (sband->bitrates[j].bitrate == rate) {
  1159. rates |= BIT(j);
  1160. if (is_basic)
  1161. basic_rates |= BIT(j);
  1162. break;
  1163. }
  1164. }
  1165. }
  1166. sta->sta.supp_rates[wk->chan->band] = rates;
  1167. sdata->vif.bss_conf.basic_rates = basic_rates;
  1168. /* cf. IEEE 802.11 9.2.12 */
  1169. if (wk->chan->band == IEEE80211_BAND_2GHZ &&
  1170. have_higher_than_11mbit)
  1171. sdata->flags |= IEEE80211_SDATA_OPERATING_GMODE;
  1172. else
  1173. sdata->flags &= ~IEEE80211_SDATA_OPERATING_GMODE;
  1174. if (elems.ht_cap_elem && !(ifmgd->flags & IEEE80211_STA_DISABLE_11N))
  1175. ieee80211_ht_cap_ie_to_sta_ht_cap(sband,
  1176. elems.ht_cap_elem, &sta->sta.ht_cap);
  1177. ap_ht_cap_flags = sta->sta.ht_cap.cap;
  1178. rate_control_rate_init(sta);
  1179. if (ifmgd->flags & IEEE80211_STA_MFP_ENABLED)
  1180. set_sta_flags(sta, WLAN_STA_MFP);
  1181. if (elems.wmm_param)
  1182. set_sta_flags(sta, WLAN_STA_WME);
  1183. err = sta_info_insert(sta);
  1184. sta = NULL;
  1185. if (err) {
  1186. printk(KERN_DEBUG "%s: failed to insert STA entry for"
  1187. " the AP (error %d)\n", sdata->name, err);
  1188. return false;
  1189. }
  1190. /*
  1191. * Always handle WMM once after association regardless
  1192. * of the first value the AP uses. Setting -1 here has
  1193. * that effect because the AP values is an unsigned
  1194. * 4-bit value.
  1195. */
  1196. ifmgd->wmm_last_param_set = -1;
  1197. if (elems.wmm_param)
  1198. ieee80211_sta_wmm_params(local, sdata, elems.wmm_param,
  1199. elems.wmm_param_len);
  1200. else
  1201. ieee80211_set_wmm_default(sdata);
  1202. local->oper_channel = wk->chan;
  1203. if (elems.ht_info_elem && elems.wmm_param &&
  1204. (sdata->local->hw.queues >= 4) &&
  1205. !(ifmgd->flags & IEEE80211_STA_DISABLE_11N))
  1206. changed |= ieee80211_enable_ht(sdata, elems.ht_info_elem,
  1207. cbss->bssid, ap_ht_cap_flags);
  1208. /* set AID and assoc capability,
  1209. * ieee80211_set_associated() will tell the driver */
  1210. bss_conf->aid = aid;
  1211. bss_conf->assoc_capability = capab_info;
  1212. ieee80211_set_associated(sdata, cbss, changed);
  1213. /*
  1214. * If we're using 4-addr mode, let the AP know that we're
  1215. * doing so, so that it can create the STA VLAN on its side
  1216. */
  1217. if (ifmgd->use_4addr)
  1218. ieee80211_send_4addr_nullfunc(local, sdata);
  1219. /*
  1220. * Start timer to probe the connection to the AP now.
  1221. * Also start the timer that will detect beacon loss.
  1222. */
  1223. ieee80211_sta_rx_notify(sdata, (struct ieee80211_hdr *)mgmt);
  1224. ieee80211_sta_reset_beacon_monitor(sdata);
  1225. return true;
  1226. }
  1227. static void ieee80211_rx_bss_info(struct ieee80211_sub_if_data *sdata,
  1228. struct ieee80211_mgmt *mgmt,
  1229. size_t len,
  1230. struct ieee80211_rx_status *rx_status,
  1231. struct ieee802_11_elems *elems,
  1232. bool beacon)
  1233. {
  1234. struct ieee80211_local *local = sdata->local;
  1235. int freq;
  1236. struct ieee80211_bss *bss;
  1237. struct ieee80211_channel *channel;
  1238. bool need_ps = false;
  1239. if (sdata->u.mgd.associated) {
  1240. bss = (void *)sdata->u.mgd.associated->priv;
  1241. /* not previously set so we may need to recalc */
  1242. need_ps = !bss->dtim_period;
  1243. }
  1244. if (elems->ds_params && elems->ds_params_len == 1)
  1245. freq = ieee80211_channel_to_frequency(elems->ds_params[0]);
  1246. else
  1247. freq = rx_status->freq;
  1248. channel = ieee80211_get_channel(local->hw.wiphy, freq);
  1249. if (!channel || channel->flags & IEEE80211_CHAN_DISABLED)
  1250. return;
  1251. bss = ieee80211_bss_info_update(local, rx_status, mgmt, len, elems,
  1252. channel, beacon);
  1253. if (bss)
  1254. ieee80211_rx_bss_put(local, bss);
  1255. if (!sdata->u.mgd.associated)
  1256. return;
  1257. if (need_ps) {
  1258. mutex_lock(&local->iflist_mtx);
  1259. ieee80211_recalc_ps(local, -1);
  1260. mutex_unlock(&local->iflist_mtx);
  1261. }
  1262. if (elems->ch_switch_elem && (elems->ch_switch_elem_len == 3) &&
  1263. (memcmp(mgmt->bssid, sdata->u.mgd.associated->bssid,
  1264. ETH_ALEN) == 0)) {
  1265. struct ieee80211_channel_sw_ie *sw_elem =
  1266. (struct ieee80211_channel_sw_ie *)elems->ch_switch_elem;
  1267. ieee80211_sta_process_chanswitch(sdata, sw_elem,
  1268. bss, rx_status->mactime);
  1269. }
  1270. }
  1271. static void ieee80211_rx_mgmt_probe_resp(struct ieee80211_sub_if_data *sdata,
  1272. struct sk_buff *skb)
  1273. {
  1274. struct ieee80211_mgmt *mgmt = (void *)skb->data;
  1275. struct ieee80211_if_managed *ifmgd;
  1276. struct ieee80211_rx_status *rx_status = (void *) skb->cb;
  1277. size_t baselen, len = skb->len;
  1278. struct ieee802_11_elems elems;
  1279. ifmgd = &sdata->u.mgd;
  1280. ASSERT_MGD_MTX(ifmgd);
  1281. if (memcmp(mgmt->da, sdata->vif.addr, ETH_ALEN))
  1282. return; /* ignore ProbeResp to foreign address */
  1283. baselen = (u8 *) mgmt->u.probe_resp.variable - (u8 *) mgmt;
  1284. if (baselen > len)
  1285. return;
  1286. ieee802_11_parse_elems(mgmt->u.probe_resp.variable, len - baselen,
  1287. &elems);
  1288. ieee80211_rx_bss_info(sdata, mgmt, len, rx_status, &elems, false);
  1289. if (ifmgd->associated &&
  1290. memcmp(mgmt->bssid, ifmgd->associated->bssid, ETH_ALEN) == 0)
  1291. ieee80211_reset_ap_probe(sdata);
  1292. }
  1293. /*
  1294. * This is the canonical list of information elements we care about,
  1295. * the filter code also gives us all changes to the Microsoft OUI
  1296. * (00:50:F2) vendor IE which is used for WMM which we need to track.
  1297. *
  1298. * We implement beacon filtering in software since that means we can
  1299. * avoid processing the frame here and in cfg80211, and userspace
  1300. * will not be able to tell whether the hardware supports it or not.
  1301. *
  1302. * XXX: This list needs to be dynamic -- userspace needs to be able to
  1303. * add items it requires. It also needs to be able to tell us to
  1304. * look out for other vendor IEs.
  1305. */
  1306. static const u64 care_about_ies =
  1307. (1ULL << WLAN_EID_COUNTRY) |
  1308. (1ULL << WLAN_EID_ERP_INFO) |
  1309. (1ULL << WLAN_EID_CHANNEL_SWITCH) |
  1310. (1ULL << WLAN_EID_PWR_CONSTRAINT) |
  1311. (1ULL << WLAN_EID_HT_CAPABILITY) |
  1312. (1ULL << WLAN_EID_HT_INFORMATION);
  1313. static void ieee80211_rx_mgmt_beacon(struct ieee80211_sub_if_data *sdata,
  1314. struct ieee80211_mgmt *mgmt,
  1315. size_t len,
  1316. struct ieee80211_rx_status *rx_status)
  1317. {
  1318. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  1319. struct ieee80211_bss_conf *bss_conf = &sdata->vif.bss_conf;
  1320. size_t baselen;
  1321. struct ieee802_11_elems elems;
  1322. struct ieee80211_local *local = sdata->local;
  1323. u32 changed = 0;
  1324. bool erp_valid, directed_tim = false;
  1325. u8 erp_value = 0;
  1326. u32 ncrc;
  1327. u8 *bssid;
  1328. ASSERT_MGD_MTX(ifmgd);
  1329. /* Process beacon from the current BSS */
  1330. baselen = (u8 *) mgmt->u.beacon.variable - (u8 *) mgmt;
  1331. if (baselen > len)
  1332. return;
  1333. if (rx_status->freq != local->hw.conf.channel->center_freq)
  1334. return;
  1335. /*
  1336. * We might have received a number of frames, among them a
  1337. * disassoc frame and a beacon...
  1338. */
  1339. if (!ifmgd->associated)
  1340. return;
  1341. bssid = ifmgd->associated->bssid;
  1342. /*
  1343. * And in theory even frames from a different AP we were just
  1344. * associated to a split-second ago!
  1345. */
  1346. if (memcmp(bssid, mgmt->bssid, ETH_ALEN) != 0)
  1347. return;
  1348. /* Track average RSSI from the Beacon frames of the current AP */
  1349. ifmgd->last_beacon_signal = rx_status->signal;
  1350. if (ifmgd->flags & IEEE80211_STA_RESET_SIGNAL_AVE) {
  1351. ifmgd->flags &= ~IEEE80211_STA_RESET_SIGNAL_AVE;
  1352. ifmgd->ave_beacon_signal = rx_status->signal * 16;
  1353. ifmgd->last_cqm_event_signal = 0;
  1354. ifmgd->count_beacon_signal = 1;
  1355. } else {
  1356. ifmgd->ave_beacon_signal =
  1357. (IEEE80211_SIGNAL_AVE_WEIGHT * rx_status->signal * 16 +
  1358. (16 - IEEE80211_SIGNAL_AVE_WEIGHT) *
  1359. ifmgd->ave_beacon_signal) / 16;
  1360. ifmgd->count_beacon_signal++;
  1361. }
  1362. if (bss_conf->cqm_rssi_thold &&
  1363. ifmgd->count_beacon_signal >= IEEE80211_SIGNAL_AVE_MIN_COUNT &&
  1364. !(local->hw.flags & IEEE80211_HW_SUPPORTS_CQM_RSSI)) {
  1365. int sig = ifmgd->ave_beacon_signal / 16;
  1366. int last_event = ifmgd->last_cqm_event_signal;
  1367. int thold = bss_conf->cqm_rssi_thold;
  1368. int hyst = bss_conf->cqm_rssi_hyst;
  1369. if (sig < thold &&
  1370. (last_event == 0 || sig < last_event - hyst)) {
  1371. ifmgd->last_cqm_event_signal = sig;
  1372. ieee80211_cqm_rssi_notify(
  1373. &sdata->vif,
  1374. NL80211_CQM_RSSI_THRESHOLD_EVENT_LOW,
  1375. GFP_KERNEL);
  1376. } else if (sig > thold &&
  1377. (last_event == 0 || sig > last_event + hyst)) {
  1378. ifmgd->last_cqm_event_signal = sig;
  1379. ieee80211_cqm_rssi_notify(
  1380. &sdata->vif,
  1381. NL80211_CQM_RSSI_THRESHOLD_EVENT_HIGH,
  1382. GFP_KERNEL);
  1383. }
  1384. }
  1385. if (ifmgd->flags & IEEE80211_STA_BEACON_POLL) {
  1386. #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
  1387. if (net_ratelimit()) {
  1388. printk(KERN_DEBUG "%s: cancelling probereq poll due "
  1389. "to a received beacon\n", sdata->name);
  1390. }
  1391. #endif
  1392. ifmgd->flags &= ~IEEE80211_STA_BEACON_POLL;
  1393. mutex_lock(&local->iflist_mtx);
  1394. ieee80211_recalc_ps(local, -1);
  1395. mutex_unlock(&local->iflist_mtx);
  1396. }
  1397. /*
  1398. * Push the beacon loss detection into the future since
  1399. * we are processing a beacon from the AP just now.
  1400. */
  1401. ieee80211_sta_reset_beacon_monitor(sdata);
  1402. ncrc = crc32_be(0, (void *)&mgmt->u.beacon.beacon_int, 4);
  1403. ncrc = ieee802_11_parse_elems_crc(mgmt->u.beacon.variable,
  1404. len - baselen, &elems,
  1405. care_about_ies, ncrc);
  1406. if (local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK)
  1407. directed_tim = ieee80211_check_tim(elems.tim, elems.tim_len,
  1408. ifmgd->aid);
  1409. if (ncrc != ifmgd->beacon_crc || !ifmgd->beacon_crc_valid) {
  1410. ieee80211_rx_bss_info(sdata, mgmt, len, rx_status, &elems,
  1411. true);
  1412. ieee80211_sta_wmm_params(local, sdata, elems.wmm_param,
  1413. elems.wmm_param_len);
  1414. }
  1415. if (local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK) {
  1416. if (directed_tim) {
  1417. if (local->hw.conf.dynamic_ps_timeout > 0) {
  1418. local->hw.conf.flags &= ~IEEE80211_CONF_PS;
  1419. ieee80211_hw_config(local,
  1420. IEEE80211_CONF_CHANGE_PS);
  1421. ieee80211_send_nullfunc(local, sdata, 0);
  1422. } else {
  1423. local->pspolling = true;
  1424. /*
  1425. * Here is assumed that the driver will be
  1426. * able to send ps-poll frame and receive a
  1427. * response even though power save mode is
  1428. * enabled, but some drivers might require
  1429. * to disable power save here. This needs
  1430. * to be investigated.
  1431. */
  1432. ieee80211_send_pspoll(local, sdata);
  1433. }
  1434. }
  1435. }
  1436. if (ncrc == ifmgd->beacon_crc && ifmgd->beacon_crc_valid)
  1437. return;
  1438. ifmgd->beacon_crc = ncrc;
  1439. ifmgd->beacon_crc_valid = true;
  1440. if (elems.erp_info && elems.erp_info_len >= 1) {
  1441. erp_valid = true;
  1442. erp_value = elems.erp_info[0];
  1443. } else {
  1444. erp_valid = false;
  1445. }
  1446. changed |= ieee80211_handle_bss_capability(sdata,
  1447. le16_to_cpu(mgmt->u.beacon.capab_info),
  1448. erp_valid, erp_value);
  1449. if (elems.ht_cap_elem && elems.ht_info_elem && elems.wmm_param &&
  1450. !(ifmgd->flags & IEEE80211_STA_DISABLE_11N)) {
  1451. struct sta_info *sta;
  1452. struct ieee80211_supported_band *sband;
  1453. u16 ap_ht_cap_flags;
  1454. rcu_read_lock();
  1455. sta = sta_info_get(sdata, bssid);
  1456. if (WARN_ON(!sta)) {
  1457. rcu_read_unlock();
  1458. return;
  1459. }
  1460. sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
  1461. ieee80211_ht_cap_ie_to_sta_ht_cap(sband,
  1462. elems.ht_cap_elem, &sta->sta.ht_cap);
  1463. ap_ht_cap_flags = sta->sta.ht_cap.cap;
  1464. rcu_read_unlock();
  1465. changed |= ieee80211_enable_ht(sdata, elems.ht_info_elem,
  1466. bssid, ap_ht_cap_flags);
  1467. }
  1468. /* Note: country IE parsing is done for us by cfg80211 */
  1469. if (elems.country_elem) {
  1470. /* TODO: IBSS also needs this */
  1471. if (elems.pwr_constr_elem)
  1472. ieee80211_handle_pwr_constr(sdata,
  1473. le16_to_cpu(mgmt->u.probe_resp.capab_info),
  1474. elems.pwr_constr_elem,
  1475. elems.pwr_constr_elem_len);
  1476. }
  1477. ieee80211_bss_info_change_notify(sdata, changed);
  1478. }
  1479. void ieee80211_sta_rx_queued_mgmt(struct ieee80211_sub_if_data *sdata,
  1480. struct sk_buff *skb)
  1481. {
  1482. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  1483. struct ieee80211_rx_status *rx_status;
  1484. struct ieee80211_mgmt *mgmt;
  1485. enum rx_mgmt_action rma = RX_MGMT_NONE;
  1486. u16 fc;
  1487. rx_status = (struct ieee80211_rx_status *) skb->cb;
  1488. mgmt = (struct ieee80211_mgmt *) skb->data;
  1489. fc = le16_to_cpu(mgmt->frame_control);
  1490. mutex_lock(&ifmgd->mtx);
  1491. if (ifmgd->associated &&
  1492. memcmp(ifmgd->associated->bssid, mgmt->bssid, ETH_ALEN) == 0) {
  1493. switch (fc & IEEE80211_FCTL_STYPE) {
  1494. case IEEE80211_STYPE_BEACON:
  1495. ieee80211_rx_mgmt_beacon(sdata, mgmt, skb->len,
  1496. rx_status);
  1497. break;
  1498. case IEEE80211_STYPE_PROBE_RESP:
  1499. ieee80211_rx_mgmt_probe_resp(sdata, skb);
  1500. break;
  1501. case IEEE80211_STYPE_DEAUTH:
  1502. rma = ieee80211_rx_mgmt_deauth(sdata, mgmt, skb->len);
  1503. break;
  1504. case IEEE80211_STYPE_DISASSOC:
  1505. rma = ieee80211_rx_mgmt_disassoc(sdata, mgmt, skb->len);
  1506. break;
  1507. case IEEE80211_STYPE_ACTION:
  1508. switch (mgmt->u.action.category) {
  1509. case WLAN_CATEGORY_SPECTRUM_MGMT:
  1510. ieee80211_sta_process_chanswitch(sdata,
  1511. &mgmt->u.action.u.chan_switch.sw_elem,
  1512. (void *)ifmgd->associated->priv,
  1513. rx_status->mactime);
  1514. break;
  1515. }
  1516. }
  1517. mutex_unlock(&ifmgd->mtx);
  1518. switch (rma) {
  1519. case RX_MGMT_NONE:
  1520. /* no action */
  1521. break;
  1522. case RX_MGMT_CFG80211_DEAUTH:
  1523. cfg80211_send_deauth(sdata->dev, (u8 *)mgmt, skb->len);
  1524. break;
  1525. case RX_MGMT_CFG80211_DISASSOC:
  1526. cfg80211_send_disassoc(sdata->dev, (u8 *)mgmt, skb->len);
  1527. break;
  1528. default:
  1529. WARN(1, "unexpected: %d", rma);
  1530. }
  1531. return;
  1532. }
  1533. mutex_unlock(&ifmgd->mtx);
  1534. if (skb->len >= 24 + 2 /* mgmt + deauth reason */ &&
  1535. (fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_DEAUTH) {
  1536. struct ieee80211_local *local = sdata->local;
  1537. struct ieee80211_work *wk;
  1538. mutex_lock(&local->mtx);
  1539. list_for_each_entry(wk, &local->work_list, list) {
  1540. if (wk->sdata != sdata)
  1541. continue;
  1542. if (wk->type != IEEE80211_WORK_ASSOC &&
  1543. wk->type != IEEE80211_WORK_ASSOC_BEACON_WAIT)
  1544. continue;
  1545. if (memcmp(mgmt->bssid, wk->filter_ta, ETH_ALEN))
  1546. continue;
  1547. if (memcmp(mgmt->sa, wk->filter_ta, ETH_ALEN))
  1548. continue;
  1549. /*
  1550. * Printing the message only here means we can't
  1551. * spuriously print it, but it also means that it
  1552. * won't be printed when the frame comes in before
  1553. * we even tried to associate or in similar cases.
  1554. *
  1555. * Ultimately, I suspect cfg80211 should print the
  1556. * messages instead.
  1557. */
  1558. printk(KERN_DEBUG
  1559. "%s: deauthenticated from %pM (Reason: %u)\n",
  1560. sdata->name, mgmt->bssid,
  1561. le16_to_cpu(mgmt->u.deauth.reason_code));
  1562. list_del_rcu(&wk->list);
  1563. free_work(wk);
  1564. break;
  1565. }
  1566. mutex_unlock(&local->mtx);
  1567. cfg80211_send_deauth(sdata->dev, (u8 *)mgmt, skb->len);
  1568. }
  1569. }
  1570. static void ieee80211_sta_timer(unsigned long data)
  1571. {
  1572. struct ieee80211_sub_if_data *sdata =
  1573. (struct ieee80211_sub_if_data *) data;
  1574. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  1575. struct ieee80211_local *local = sdata->local;
  1576. if (local->quiescing) {
  1577. set_bit(TMR_RUNNING_TIMER, &ifmgd->timers_running);
  1578. return;
  1579. }
  1580. ieee80211_queue_work(&local->hw, &sdata->work);
  1581. }
  1582. static void ieee80211_sta_connection_lost(struct ieee80211_sub_if_data *sdata,
  1583. u8 *bssid)
  1584. {
  1585. struct ieee80211_local *local = sdata->local;
  1586. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  1587. ifmgd->flags &= ~(IEEE80211_STA_CONNECTION_POLL |
  1588. IEEE80211_STA_BEACON_POLL);
  1589. ieee80211_set_disassoc(sdata, true, true);
  1590. mutex_unlock(&ifmgd->mtx);
  1591. mutex_lock(&local->mtx);
  1592. ieee80211_recalc_idle(local);
  1593. mutex_unlock(&local->mtx);
  1594. /*
  1595. * must be outside lock due to cfg80211,
  1596. * but that's not a problem.
  1597. */
  1598. ieee80211_send_deauth_disassoc(sdata, bssid,
  1599. IEEE80211_STYPE_DEAUTH,
  1600. WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY,
  1601. NULL, true);
  1602. mutex_lock(&ifmgd->mtx);
  1603. }
  1604. void ieee80211_sta_work(struct ieee80211_sub_if_data *sdata)
  1605. {
  1606. struct ieee80211_local *local = sdata->local;
  1607. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  1608. /* then process the rest of the work */
  1609. mutex_lock(&ifmgd->mtx);
  1610. if (ifmgd->flags & (IEEE80211_STA_BEACON_POLL |
  1611. IEEE80211_STA_CONNECTION_POLL) &&
  1612. ifmgd->associated) {
  1613. u8 bssid[ETH_ALEN];
  1614. int max_tries;
  1615. memcpy(bssid, ifmgd->associated->bssid, ETH_ALEN);
  1616. if (local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS)
  1617. max_tries = IEEE80211_MAX_NULLFUNC_TRIES;
  1618. else
  1619. max_tries = IEEE80211_MAX_PROBE_TRIES;
  1620. /* ACK received for nullfunc probing frame */
  1621. if (!ifmgd->probe_send_count)
  1622. ieee80211_reset_ap_probe(sdata);
  1623. else if (ifmgd->nullfunc_failed) {
  1624. if (ifmgd->probe_send_count < max_tries) {
  1625. #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
  1626. wiphy_debug(local->hw.wiphy,
  1627. "%s: No ack for nullfunc frame to"
  1628. " AP %pM, try %d\n",
  1629. sdata->name, bssid,
  1630. ifmgd->probe_send_count);
  1631. #endif
  1632. ieee80211_mgd_probe_ap_send(sdata);
  1633. } else {
  1634. #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
  1635. wiphy_debug(local->hw.wiphy,
  1636. "%s: No ack for nullfunc frame to"
  1637. " AP %pM, disconnecting.\n",
  1638. sdata->name, bssid);
  1639. #endif
  1640. ieee80211_sta_connection_lost(sdata, bssid);
  1641. }
  1642. } else if (time_is_after_jiffies(ifmgd->probe_timeout))
  1643. run_again(ifmgd, ifmgd->probe_timeout);
  1644. else if (local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS) {
  1645. #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
  1646. wiphy_debug(local->hw.wiphy,
  1647. "%s: Failed to send nullfunc to AP %pM"
  1648. " after %dms, disconnecting.\n",
  1649. sdata->name,
  1650. bssid, (1000 * IEEE80211_PROBE_WAIT)/HZ);
  1651. #endif
  1652. ieee80211_sta_connection_lost(sdata, bssid);
  1653. } else if (ifmgd->probe_send_count < max_tries) {
  1654. #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
  1655. wiphy_debug(local->hw.wiphy,
  1656. "%s: No probe response from AP %pM"
  1657. " after %dms, try %d\n",
  1658. sdata->name,
  1659. bssid, (1000 * IEEE80211_PROBE_WAIT)/HZ,
  1660. ifmgd->probe_send_count);
  1661. #endif
  1662. ieee80211_mgd_probe_ap_send(sdata);
  1663. } else {
  1664. /*
  1665. * We actually lost the connection ... or did we?
  1666. * Let's make sure!
  1667. */
  1668. wiphy_debug(local->hw.wiphy,
  1669. "%s: No probe response from AP %pM"
  1670. " after %dms, disconnecting.\n",
  1671. sdata->name,
  1672. bssid, (1000 * IEEE80211_PROBE_WAIT)/HZ);
  1673. ieee80211_sta_connection_lost(sdata, bssid);
  1674. }
  1675. }
  1676. mutex_unlock(&ifmgd->mtx);
  1677. }
  1678. static void ieee80211_sta_bcn_mon_timer(unsigned long data)
  1679. {
  1680. struct ieee80211_sub_if_data *sdata =
  1681. (struct ieee80211_sub_if_data *) data;
  1682. struct ieee80211_local *local = sdata->local;
  1683. if (local->quiescing)
  1684. return;
  1685. ieee80211_queue_work(&sdata->local->hw,
  1686. &sdata->u.mgd.beacon_connection_loss_work);
  1687. }
  1688. static void ieee80211_sta_conn_mon_timer(unsigned long data)
  1689. {
  1690. struct ieee80211_sub_if_data *sdata =
  1691. (struct ieee80211_sub_if_data *) data;
  1692. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  1693. struct ieee80211_local *local = sdata->local;
  1694. if (local->quiescing)
  1695. return;
  1696. ieee80211_queue_work(&local->hw, &ifmgd->monitor_work);
  1697. }
  1698. static void ieee80211_sta_monitor_work(struct work_struct *work)
  1699. {
  1700. struct ieee80211_sub_if_data *sdata =
  1701. container_of(work, struct ieee80211_sub_if_data,
  1702. u.mgd.monitor_work);
  1703. ieee80211_mgd_probe_ap(sdata, false);
  1704. }
  1705. static void ieee80211_restart_sta_timer(struct ieee80211_sub_if_data *sdata)
  1706. {
  1707. if (sdata->vif.type == NL80211_IFTYPE_STATION) {
  1708. sdata->u.mgd.flags &= ~(IEEE80211_STA_BEACON_POLL |
  1709. IEEE80211_STA_CONNECTION_POLL);
  1710. /* let's probe the connection once */
  1711. ieee80211_queue_work(&sdata->local->hw,
  1712. &sdata->u.mgd.monitor_work);
  1713. /* and do all the other regular work too */
  1714. ieee80211_queue_work(&sdata->local->hw, &sdata->work);
  1715. }
  1716. }
  1717. #ifdef CONFIG_PM
  1718. void ieee80211_sta_quiesce(struct ieee80211_sub_if_data *sdata)
  1719. {
  1720. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  1721. /*
  1722. * we need to use atomic bitops for the running bits
  1723. * only because both timers might fire at the same
  1724. * time -- the code here is properly synchronised.
  1725. */
  1726. cancel_work_sync(&ifmgd->request_smps_work);
  1727. cancel_work_sync(&ifmgd->beacon_connection_loss_work);
  1728. if (del_timer_sync(&ifmgd->timer))
  1729. set_bit(TMR_RUNNING_TIMER, &ifmgd->timers_running);
  1730. cancel_work_sync(&ifmgd->chswitch_work);
  1731. if (del_timer_sync(&ifmgd->chswitch_timer))
  1732. set_bit(TMR_RUNNING_CHANSW, &ifmgd->timers_running);
  1733. cancel_work_sync(&ifmgd->monitor_work);
  1734. /* these will just be re-established on connection */
  1735. del_timer_sync(&ifmgd->conn_mon_timer);
  1736. del_timer_sync(&ifmgd->bcn_mon_timer);
  1737. }
  1738. void ieee80211_sta_restart(struct ieee80211_sub_if_data *sdata)
  1739. {
  1740. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  1741. if (test_and_clear_bit(TMR_RUNNING_TIMER, &ifmgd->timers_running))
  1742. add_timer(&ifmgd->timer);
  1743. if (test_and_clear_bit(TMR_RUNNING_CHANSW, &ifmgd->timers_running))
  1744. add_timer(&ifmgd->chswitch_timer);
  1745. ieee80211_sta_reset_beacon_monitor(sdata);
  1746. ieee80211_restart_sta_timer(sdata);
  1747. }
  1748. #endif
  1749. /* interface setup */
  1750. void ieee80211_sta_setup_sdata(struct ieee80211_sub_if_data *sdata)
  1751. {
  1752. struct ieee80211_if_managed *ifmgd;
  1753. ifmgd = &sdata->u.mgd;
  1754. INIT_WORK(&ifmgd->monitor_work, ieee80211_sta_monitor_work);
  1755. INIT_WORK(&ifmgd->chswitch_work, ieee80211_chswitch_work);
  1756. INIT_WORK(&ifmgd->beacon_connection_loss_work,
  1757. ieee80211_beacon_connection_loss_work);
  1758. INIT_WORK(&ifmgd->request_smps_work, ieee80211_request_smps_work);
  1759. setup_timer(&ifmgd->timer, ieee80211_sta_timer,
  1760. (unsigned long) sdata);
  1761. setup_timer(&ifmgd->bcn_mon_timer, ieee80211_sta_bcn_mon_timer,
  1762. (unsigned long) sdata);
  1763. setup_timer(&ifmgd->conn_mon_timer, ieee80211_sta_conn_mon_timer,
  1764. (unsigned long) sdata);
  1765. setup_timer(&ifmgd->chswitch_timer, ieee80211_chswitch_timer,
  1766. (unsigned long) sdata);
  1767. ifmgd->flags = 0;
  1768. mutex_init(&ifmgd->mtx);
  1769. if (sdata->local->hw.flags & IEEE80211_HW_SUPPORTS_DYNAMIC_SMPS)
  1770. ifmgd->req_smps = IEEE80211_SMPS_AUTOMATIC;
  1771. else
  1772. ifmgd->req_smps = IEEE80211_SMPS_OFF;
  1773. }
  1774. /* scan finished notification */
  1775. void ieee80211_mlme_notify_scan_completed(struct ieee80211_local *local)
  1776. {
  1777. struct ieee80211_sub_if_data *sdata = local->scan_sdata;
  1778. /* Restart STA timers */
  1779. rcu_read_lock();
  1780. list_for_each_entry_rcu(sdata, &local->interfaces, list)
  1781. ieee80211_restart_sta_timer(sdata);
  1782. rcu_read_unlock();
  1783. }
  1784. int ieee80211_max_network_latency(struct notifier_block *nb,
  1785. unsigned long data, void *dummy)
  1786. {
  1787. s32 latency_usec = (s32) data;
  1788. struct ieee80211_local *local =
  1789. container_of(nb, struct ieee80211_local,
  1790. network_latency_notifier);
  1791. mutex_lock(&local->iflist_mtx);
  1792. ieee80211_recalc_ps(local, latency_usec);
  1793. mutex_unlock(&local->iflist_mtx);
  1794. return 0;
  1795. }
  1796. /* config hooks */
  1797. static enum work_done_result
  1798. ieee80211_probe_auth_done(struct ieee80211_work *wk,
  1799. struct sk_buff *skb)
  1800. {
  1801. if (!skb) {
  1802. cfg80211_send_auth_timeout(wk->sdata->dev, wk->filter_ta);
  1803. return WORK_DONE_DESTROY;
  1804. }
  1805. if (wk->type == IEEE80211_WORK_AUTH) {
  1806. cfg80211_send_rx_auth(wk->sdata->dev, skb->data, skb->len);
  1807. return WORK_DONE_DESTROY;
  1808. }
  1809. mutex_lock(&wk->sdata->u.mgd.mtx);
  1810. ieee80211_rx_mgmt_probe_resp(wk->sdata, skb);
  1811. mutex_unlock(&wk->sdata->u.mgd.mtx);
  1812. wk->type = IEEE80211_WORK_AUTH;
  1813. wk->probe_auth.tries = 0;
  1814. return WORK_DONE_REQUEUE;
  1815. }
  1816. int ieee80211_mgd_auth(struct ieee80211_sub_if_data *sdata,
  1817. struct cfg80211_auth_request *req)
  1818. {
  1819. const u8 *ssid;
  1820. struct ieee80211_work *wk;
  1821. u16 auth_alg;
  1822. if (req->local_state_change)
  1823. return 0; /* no need to update mac80211 state */
  1824. switch (req->auth_type) {
  1825. case NL80211_AUTHTYPE_OPEN_SYSTEM:
  1826. auth_alg = WLAN_AUTH_OPEN;
  1827. break;
  1828. case NL80211_AUTHTYPE_SHARED_KEY:
  1829. if (IS_ERR(sdata->local->wep_tx_tfm))
  1830. return -EOPNOTSUPP;
  1831. auth_alg = WLAN_AUTH_SHARED_KEY;
  1832. break;
  1833. case NL80211_AUTHTYPE_FT:
  1834. auth_alg = WLAN_AUTH_FT;
  1835. break;
  1836. case NL80211_AUTHTYPE_NETWORK_EAP:
  1837. auth_alg = WLAN_AUTH_LEAP;
  1838. break;
  1839. default:
  1840. return -EOPNOTSUPP;
  1841. }
  1842. wk = kzalloc(sizeof(*wk) + req->ie_len, GFP_KERNEL);
  1843. if (!wk)
  1844. return -ENOMEM;
  1845. memcpy(wk->filter_ta, req->bss->bssid, ETH_ALEN);
  1846. if (req->ie && req->ie_len) {
  1847. memcpy(wk->ie, req->ie, req->ie_len);
  1848. wk->ie_len = req->ie_len;
  1849. }
  1850. if (req->key && req->key_len) {
  1851. wk->probe_auth.key_len = req->key_len;
  1852. wk->probe_auth.key_idx = req->key_idx;
  1853. memcpy(wk->probe_auth.key, req->key, req->key_len);
  1854. }
  1855. ssid = ieee80211_bss_get_ie(req->bss, WLAN_EID_SSID);
  1856. memcpy(wk->probe_auth.ssid, ssid + 2, ssid[1]);
  1857. wk->probe_auth.ssid_len = ssid[1];
  1858. wk->probe_auth.algorithm = auth_alg;
  1859. wk->probe_auth.privacy = req->bss->capability & WLAN_CAPABILITY_PRIVACY;
  1860. /* if we already have a probe, don't probe again */
  1861. if (req->bss->proberesp_ies)
  1862. wk->type = IEEE80211_WORK_AUTH;
  1863. else
  1864. wk->type = IEEE80211_WORK_DIRECT_PROBE;
  1865. wk->chan = req->bss->channel;
  1866. wk->sdata = sdata;
  1867. wk->done = ieee80211_probe_auth_done;
  1868. ieee80211_add_work(wk);
  1869. return 0;
  1870. }
  1871. static enum work_done_result ieee80211_assoc_done(struct ieee80211_work *wk,
  1872. struct sk_buff *skb)
  1873. {
  1874. struct ieee80211_mgmt *mgmt;
  1875. struct ieee80211_rx_status *rx_status;
  1876. struct ieee802_11_elems elems;
  1877. u16 status;
  1878. if (!skb) {
  1879. cfg80211_send_assoc_timeout(wk->sdata->dev, wk->filter_ta);
  1880. return WORK_DONE_DESTROY;
  1881. }
  1882. if (wk->type == IEEE80211_WORK_ASSOC_BEACON_WAIT) {
  1883. mutex_lock(&wk->sdata->u.mgd.mtx);
  1884. rx_status = (void *) skb->cb;
  1885. ieee802_11_parse_elems(skb->data + 24 + 12, skb->len - 24 - 12, &elems);
  1886. ieee80211_rx_bss_info(wk->sdata, (void *)skb->data, skb->len, rx_status,
  1887. &elems, true);
  1888. mutex_unlock(&wk->sdata->u.mgd.mtx);
  1889. wk->type = IEEE80211_WORK_ASSOC;
  1890. /* not really done yet */
  1891. return WORK_DONE_REQUEUE;
  1892. }
  1893. mgmt = (void *)skb->data;
  1894. status = le16_to_cpu(mgmt->u.assoc_resp.status_code);
  1895. if (status == WLAN_STATUS_SUCCESS) {
  1896. mutex_lock(&wk->sdata->u.mgd.mtx);
  1897. if (!ieee80211_assoc_success(wk, mgmt, skb->len)) {
  1898. mutex_unlock(&wk->sdata->u.mgd.mtx);
  1899. /* oops -- internal error -- send timeout for now */
  1900. cfg80211_send_assoc_timeout(wk->sdata->dev,
  1901. wk->filter_ta);
  1902. return WORK_DONE_DESTROY;
  1903. }
  1904. mutex_unlock(&wk->sdata->u.mgd.mtx);
  1905. }
  1906. cfg80211_send_rx_assoc(wk->sdata->dev, skb->data, skb->len);
  1907. return WORK_DONE_DESTROY;
  1908. }
  1909. int ieee80211_mgd_assoc(struct ieee80211_sub_if_data *sdata,
  1910. struct cfg80211_assoc_request *req)
  1911. {
  1912. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  1913. struct ieee80211_bss *bss = (void *)req->bss->priv;
  1914. struct ieee80211_work *wk;
  1915. const u8 *ssid;
  1916. int i;
  1917. mutex_lock(&ifmgd->mtx);
  1918. if (ifmgd->associated) {
  1919. if (!req->prev_bssid ||
  1920. memcmp(req->prev_bssid, ifmgd->associated->bssid,
  1921. ETH_ALEN)) {
  1922. /*
  1923. * We are already associated and the request was not a
  1924. * reassociation request from the current BSS, so
  1925. * reject it.
  1926. */
  1927. mutex_unlock(&ifmgd->mtx);
  1928. return -EALREADY;
  1929. }
  1930. /* Trying to reassociate - clear previous association state */
  1931. ieee80211_set_disassoc(sdata, true, false);
  1932. }
  1933. mutex_unlock(&ifmgd->mtx);
  1934. wk = kzalloc(sizeof(*wk) + req->ie_len, GFP_KERNEL);
  1935. if (!wk)
  1936. return -ENOMEM;
  1937. ifmgd->flags &= ~IEEE80211_STA_DISABLE_11N;
  1938. ifmgd->flags &= ~IEEE80211_STA_NULLFUNC_ACKED;
  1939. ifmgd->beacon_crc_valid = false;
  1940. for (i = 0; i < req->crypto.n_ciphers_pairwise; i++)
  1941. if (req->crypto.ciphers_pairwise[i] == WLAN_CIPHER_SUITE_WEP40 ||
  1942. req->crypto.ciphers_pairwise[i] == WLAN_CIPHER_SUITE_TKIP ||
  1943. req->crypto.ciphers_pairwise[i] == WLAN_CIPHER_SUITE_WEP104)
  1944. ifmgd->flags |= IEEE80211_STA_DISABLE_11N;
  1945. if (req->ie && req->ie_len) {
  1946. memcpy(wk->ie, req->ie, req->ie_len);
  1947. wk->ie_len = req->ie_len;
  1948. } else
  1949. wk->ie_len = 0;
  1950. wk->assoc.bss = req->bss;
  1951. memcpy(wk->filter_ta, req->bss->bssid, ETH_ALEN);
  1952. /* new association always uses requested smps mode */
  1953. if (ifmgd->req_smps == IEEE80211_SMPS_AUTOMATIC) {
  1954. if (ifmgd->powersave)
  1955. ifmgd->ap_smps = IEEE80211_SMPS_DYNAMIC;
  1956. else
  1957. ifmgd->ap_smps = IEEE80211_SMPS_OFF;
  1958. } else
  1959. ifmgd->ap_smps = ifmgd->req_smps;
  1960. wk->assoc.smps = ifmgd->ap_smps;
  1961. /*
  1962. * IEEE802.11n does not allow TKIP/WEP as pairwise ciphers in HT mode.
  1963. * We still associate in non-HT mode (11a/b/g) if any one of these
  1964. * ciphers is configured as pairwise.
  1965. * We can set this to true for non-11n hardware, that'll be checked
  1966. * separately along with the peer capabilities.
  1967. */
  1968. wk->assoc.use_11n = !(ifmgd->flags & IEEE80211_STA_DISABLE_11N);
  1969. wk->assoc.capability = req->bss->capability;
  1970. wk->assoc.wmm_used = bss->wmm_used;
  1971. wk->assoc.supp_rates = bss->supp_rates;
  1972. wk->assoc.supp_rates_len = bss->supp_rates_len;
  1973. wk->assoc.ht_information_ie =
  1974. ieee80211_bss_get_ie(req->bss, WLAN_EID_HT_INFORMATION);
  1975. if (bss->wmm_used && bss->uapsd_supported &&
  1976. (sdata->local->hw.flags & IEEE80211_HW_SUPPORTS_UAPSD)) {
  1977. wk->assoc.uapsd_used = true;
  1978. ifmgd->flags |= IEEE80211_STA_UAPSD_ENABLED;
  1979. } else {
  1980. wk->assoc.uapsd_used = false;
  1981. ifmgd->flags &= ~IEEE80211_STA_UAPSD_ENABLED;
  1982. }
  1983. ssid = ieee80211_bss_get_ie(req->bss, WLAN_EID_SSID);
  1984. memcpy(wk->assoc.ssid, ssid + 2, ssid[1]);
  1985. wk->assoc.ssid_len = ssid[1];
  1986. if (req->prev_bssid)
  1987. memcpy(wk->assoc.prev_bssid, req->prev_bssid, ETH_ALEN);
  1988. wk->chan = req->bss->channel;
  1989. wk->sdata = sdata;
  1990. wk->done = ieee80211_assoc_done;
  1991. if (!bss->dtim_period &&
  1992. sdata->local->hw.flags & IEEE80211_HW_NEED_DTIM_PERIOD)
  1993. wk->type = IEEE80211_WORK_ASSOC_BEACON_WAIT;
  1994. else
  1995. wk->type = IEEE80211_WORK_ASSOC;
  1996. if (req->use_mfp) {
  1997. ifmgd->mfp = IEEE80211_MFP_REQUIRED;
  1998. ifmgd->flags |= IEEE80211_STA_MFP_ENABLED;
  1999. } else {
  2000. ifmgd->mfp = IEEE80211_MFP_DISABLED;
  2001. ifmgd->flags &= ~IEEE80211_STA_MFP_ENABLED;
  2002. }
  2003. if (req->crypto.control_port)
  2004. ifmgd->flags |= IEEE80211_STA_CONTROL_PORT;
  2005. else
  2006. ifmgd->flags &= ~IEEE80211_STA_CONTROL_PORT;
  2007. sdata->control_port_protocol = req->crypto.control_port_ethertype;
  2008. sdata->control_port_no_encrypt = req->crypto.control_port_no_encrypt;
  2009. ieee80211_add_work(wk);
  2010. return 0;
  2011. }
  2012. int ieee80211_mgd_deauth(struct ieee80211_sub_if_data *sdata,
  2013. struct cfg80211_deauth_request *req,
  2014. void *cookie)
  2015. {
  2016. struct ieee80211_local *local = sdata->local;
  2017. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  2018. struct ieee80211_work *wk;
  2019. u8 bssid[ETH_ALEN];
  2020. bool assoc_bss = false;
  2021. mutex_lock(&ifmgd->mtx);
  2022. memcpy(bssid, req->bss->bssid, ETH_ALEN);
  2023. if (ifmgd->associated == req->bss) {
  2024. ieee80211_set_disassoc(sdata, false, true);
  2025. mutex_unlock(&ifmgd->mtx);
  2026. assoc_bss = true;
  2027. } else {
  2028. bool not_auth_yet = false;
  2029. mutex_unlock(&ifmgd->mtx);
  2030. mutex_lock(&local->mtx);
  2031. list_for_each_entry(wk, &local->work_list, list) {
  2032. if (wk->sdata != sdata)
  2033. continue;
  2034. if (wk->type != IEEE80211_WORK_DIRECT_PROBE &&
  2035. wk->type != IEEE80211_WORK_AUTH &&
  2036. wk->type != IEEE80211_WORK_ASSOC &&
  2037. wk->type != IEEE80211_WORK_ASSOC_BEACON_WAIT)
  2038. continue;
  2039. if (memcmp(req->bss->bssid, wk->filter_ta, ETH_ALEN))
  2040. continue;
  2041. not_auth_yet = wk->type == IEEE80211_WORK_DIRECT_PROBE;
  2042. list_del_rcu(&wk->list);
  2043. free_work(wk);
  2044. break;
  2045. }
  2046. mutex_unlock(&local->mtx);
  2047. /*
  2048. * If somebody requests authentication and we haven't
  2049. * sent out an auth frame yet there's no need to send
  2050. * out a deauth frame either. If the state was PROBE,
  2051. * then this is the case. If it's AUTH we have sent a
  2052. * frame, and if it's IDLE we have completed the auth
  2053. * process already.
  2054. */
  2055. if (not_auth_yet) {
  2056. __cfg80211_auth_canceled(sdata->dev, bssid);
  2057. return 0;
  2058. }
  2059. }
  2060. printk(KERN_DEBUG "%s: deauthenticating from %pM by local choice (reason=%d)\n",
  2061. sdata->name, bssid, req->reason_code);
  2062. ieee80211_send_deauth_disassoc(sdata, bssid, IEEE80211_STYPE_DEAUTH,
  2063. req->reason_code, cookie,
  2064. !req->local_state_change);
  2065. if (assoc_bss)
  2066. sta_info_destroy_addr(sdata, bssid);
  2067. mutex_lock(&sdata->local->mtx);
  2068. ieee80211_recalc_idle(sdata->local);
  2069. mutex_unlock(&sdata->local->mtx);
  2070. return 0;
  2071. }
  2072. int ieee80211_mgd_disassoc(struct ieee80211_sub_if_data *sdata,
  2073. struct cfg80211_disassoc_request *req,
  2074. void *cookie)
  2075. {
  2076. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  2077. u8 bssid[ETH_ALEN];
  2078. mutex_lock(&ifmgd->mtx);
  2079. /*
  2080. * cfg80211 should catch this ... but it's racy since
  2081. * we can receive a disassoc frame, process it, hand it
  2082. * to cfg80211 while that's in a locked section already
  2083. * trying to tell us that the user wants to disconnect.
  2084. */
  2085. if (ifmgd->associated != req->bss) {
  2086. mutex_unlock(&ifmgd->mtx);
  2087. return -ENOLINK;
  2088. }
  2089. printk(KERN_DEBUG "%s: disassociating from %pM by local choice (reason=%d)\n",
  2090. sdata->name, req->bss->bssid, req->reason_code);
  2091. memcpy(bssid, req->bss->bssid, ETH_ALEN);
  2092. ieee80211_set_disassoc(sdata, false, true);
  2093. mutex_unlock(&ifmgd->mtx);
  2094. ieee80211_send_deauth_disassoc(sdata, req->bss->bssid,
  2095. IEEE80211_STYPE_DISASSOC, req->reason_code,
  2096. cookie, !req->local_state_change);
  2097. sta_info_destroy_addr(sdata, bssid);
  2098. mutex_lock(&sdata->local->mtx);
  2099. ieee80211_recalc_idle(sdata->local);
  2100. mutex_unlock(&sdata->local->mtx);
  2101. return 0;
  2102. }
  2103. void ieee80211_cqm_rssi_notify(struct ieee80211_vif *vif,
  2104. enum nl80211_cqm_rssi_threshold_event rssi_event,
  2105. gfp_t gfp)
  2106. {
  2107. struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
  2108. trace_api_cqm_rssi_notify(sdata, rssi_event);
  2109. cfg80211_cqm_rssi_notify(sdata->dev, rssi_event, gfp);
  2110. }
  2111. EXPORT_SYMBOL(ieee80211_cqm_rssi_notify);