mlme.c 58 KB

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