mlme.c 68 KB

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