mlme.c 56 KB

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