mlme.c 63 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278
  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_ASSOC_SCANS_MAX_TRIES 2
  28. #define IEEE80211_AUTH_TIMEOUT (HZ / 5)
  29. #define IEEE80211_AUTH_MAX_TRIES 3
  30. #define IEEE80211_ASSOC_TIMEOUT (HZ / 5)
  31. #define IEEE80211_ASSOC_MAX_TRIES 3
  32. #define IEEE80211_MONITORING_INTERVAL (2 * HZ)
  33. #define IEEE80211_PROBE_WAIT (HZ / 5)
  34. #define IEEE80211_PROBE_IDLE_TIME (60 * HZ)
  35. #define IEEE80211_RETRY_AUTH_INTERVAL (1 * HZ)
  36. #define TMR_RUNNING_TIMER 0
  37. #define TMR_RUNNING_CHANSW 1
  38. /* utils */
  39. static int ecw2cw(int ecw)
  40. {
  41. return (1 << ecw) - 1;
  42. }
  43. static int ieee80211_compatible_rates(struct ieee80211_bss *bss,
  44. struct ieee80211_supported_band *sband,
  45. u32 *rates)
  46. {
  47. int i, j, count;
  48. *rates = 0;
  49. count = 0;
  50. for (i = 0; i < bss->supp_rates_len; i++) {
  51. int rate = (bss->supp_rates[i] & 0x7F) * 5;
  52. for (j = 0; j < sband->n_bitrates; j++)
  53. if (sband->bitrates[j].bitrate == rate) {
  54. *rates |= BIT(j);
  55. count++;
  56. break;
  57. }
  58. }
  59. return count;
  60. }
  61. /*
  62. * ieee80211_enable_ht should be called only after the operating band
  63. * has been determined as ht configuration depends on the hw's
  64. * HT abilities for a specific band.
  65. */
  66. static u32 ieee80211_enable_ht(struct ieee80211_sub_if_data *sdata,
  67. struct ieee80211_ht_info *hti,
  68. u16 ap_ht_cap_flags)
  69. {
  70. struct ieee80211_local *local = sdata->local;
  71. struct ieee80211_supported_band *sband;
  72. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  73. struct sta_info *sta;
  74. u32 changed = 0;
  75. u16 ht_opmode;
  76. bool enable_ht = true, ht_changed;
  77. enum nl80211_channel_type channel_type = NL80211_CHAN_NO_HT;
  78. sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
  79. /* HT is not supported */
  80. if (!sband->ht_cap.ht_supported)
  81. enable_ht = false;
  82. /* check that channel matches the right operating channel */
  83. if (local->hw.conf.channel->center_freq !=
  84. ieee80211_channel_to_frequency(hti->control_chan))
  85. enable_ht = false;
  86. if (enable_ht) {
  87. channel_type = NL80211_CHAN_HT20;
  88. if (!(ap_ht_cap_flags & IEEE80211_HT_CAP_40MHZ_INTOLERANT) &&
  89. (sband->ht_cap.cap & IEEE80211_HT_CAP_SUP_WIDTH_20_40) &&
  90. (hti->ht_param & IEEE80211_HT_PARAM_CHAN_WIDTH_ANY)) {
  91. switch(hti->ht_param & IEEE80211_HT_PARAM_CHA_SEC_OFFSET) {
  92. case IEEE80211_HT_PARAM_CHA_SEC_ABOVE:
  93. if (!(local->hw.conf.channel->flags &
  94. IEEE80211_CHAN_NO_HT40PLUS))
  95. channel_type = NL80211_CHAN_HT40PLUS;
  96. break;
  97. case IEEE80211_HT_PARAM_CHA_SEC_BELOW:
  98. if (!(local->hw.conf.channel->flags &
  99. IEEE80211_CHAN_NO_HT40MINUS))
  100. channel_type = NL80211_CHAN_HT40MINUS;
  101. break;
  102. }
  103. }
  104. }
  105. ht_changed = conf_is_ht(&local->hw.conf) != enable_ht ||
  106. channel_type != local->hw.conf.channel_type;
  107. local->oper_channel_type = channel_type;
  108. if (ht_changed) {
  109. /* channel_type change automatically detected */
  110. ieee80211_hw_config(local, 0);
  111. rcu_read_lock();
  112. sta = sta_info_get(local, ifmgd->bssid);
  113. if (sta)
  114. rate_control_rate_update(local, sband, sta,
  115. IEEE80211_RC_HT_CHANGED);
  116. rcu_read_unlock();
  117. }
  118. /* disable HT */
  119. if (!enable_ht)
  120. return 0;
  121. ht_opmode = le16_to_cpu(hti->operation_mode);
  122. /* if bss configuration changed store the new one */
  123. if (!sdata->ht_opmode_valid ||
  124. sdata->vif.bss_conf.ht_operation_mode != ht_opmode) {
  125. changed |= BSS_CHANGED_HT;
  126. sdata->vif.bss_conf.ht_operation_mode = ht_opmode;
  127. sdata->ht_opmode_valid = true;
  128. }
  129. return changed;
  130. }
  131. /* frame sending functions */
  132. static void ieee80211_send_assoc(struct ieee80211_sub_if_data *sdata)
  133. {
  134. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  135. struct ieee80211_local *local = sdata->local;
  136. struct sk_buff *skb;
  137. struct ieee80211_mgmt *mgmt;
  138. u8 *pos;
  139. const u8 *ies, *ht_ie;
  140. int i, len, count, rates_len, supp_rates_len;
  141. u16 capab;
  142. struct ieee80211_bss *bss;
  143. int wmm = 0;
  144. struct ieee80211_supported_band *sband;
  145. u32 rates = 0;
  146. skb = dev_alloc_skb(local->hw.extra_tx_headroom +
  147. sizeof(*mgmt) + 200 + ifmgd->extra_ie_len +
  148. ifmgd->ssid_len);
  149. if (!skb) {
  150. printk(KERN_DEBUG "%s: failed to allocate buffer for assoc "
  151. "frame\n", sdata->dev->name);
  152. return;
  153. }
  154. skb_reserve(skb, local->hw.extra_tx_headroom);
  155. sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
  156. capab = ifmgd->capab;
  157. if (local->hw.conf.channel->band == IEEE80211_BAND_2GHZ) {
  158. if (!(local->hw.flags & IEEE80211_HW_2GHZ_SHORT_SLOT_INCAPABLE))
  159. capab |= WLAN_CAPABILITY_SHORT_SLOT_TIME;
  160. if (!(local->hw.flags & IEEE80211_HW_2GHZ_SHORT_PREAMBLE_INCAPABLE))
  161. capab |= WLAN_CAPABILITY_SHORT_PREAMBLE;
  162. }
  163. bss = ieee80211_rx_bss_get(local, ifmgd->bssid,
  164. local->hw.conf.channel->center_freq,
  165. ifmgd->ssid, ifmgd->ssid_len);
  166. if (bss) {
  167. if (bss->cbss.capability & WLAN_CAPABILITY_PRIVACY)
  168. capab |= WLAN_CAPABILITY_PRIVACY;
  169. if (bss->wmm_used)
  170. wmm = 1;
  171. /* get all rates supported by the device and the AP as
  172. * some APs don't like getting a superset of their rates
  173. * in the association request (e.g. D-Link DAP 1353 in
  174. * b-only mode) */
  175. rates_len = ieee80211_compatible_rates(bss, sband, &rates);
  176. if ((bss->cbss.capability & WLAN_CAPABILITY_SPECTRUM_MGMT) &&
  177. (local->hw.flags & IEEE80211_HW_SPECTRUM_MGMT))
  178. capab |= WLAN_CAPABILITY_SPECTRUM_MGMT;
  179. ieee80211_rx_bss_put(local, bss);
  180. } else {
  181. rates = ~0;
  182. rates_len = sband->n_bitrates;
  183. }
  184. mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
  185. memset(mgmt, 0, 24);
  186. memcpy(mgmt->da, ifmgd->bssid, ETH_ALEN);
  187. memcpy(mgmt->sa, sdata->dev->dev_addr, ETH_ALEN);
  188. memcpy(mgmt->bssid, ifmgd->bssid, ETH_ALEN);
  189. if (ifmgd->flags & IEEE80211_STA_PREV_BSSID_SET) {
  190. skb_put(skb, 10);
  191. mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
  192. IEEE80211_STYPE_REASSOC_REQ);
  193. mgmt->u.reassoc_req.capab_info = cpu_to_le16(capab);
  194. mgmt->u.reassoc_req.listen_interval =
  195. cpu_to_le16(local->hw.conf.listen_interval);
  196. memcpy(mgmt->u.reassoc_req.current_ap, ifmgd->prev_bssid,
  197. ETH_ALEN);
  198. } else {
  199. skb_put(skb, 4);
  200. mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
  201. IEEE80211_STYPE_ASSOC_REQ);
  202. mgmt->u.assoc_req.capab_info = cpu_to_le16(capab);
  203. mgmt->u.assoc_req.listen_interval =
  204. cpu_to_le16(local->hw.conf.listen_interval);
  205. }
  206. /* SSID */
  207. ies = pos = skb_put(skb, 2 + ifmgd->ssid_len);
  208. *pos++ = WLAN_EID_SSID;
  209. *pos++ = ifmgd->ssid_len;
  210. memcpy(pos, ifmgd->ssid, ifmgd->ssid_len);
  211. /* add all rates which were marked to be used above */
  212. supp_rates_len = rates_len;
  213. if (supp_rates_len > 8)
  214. supp_rates_len = 8;
  215. len = sband->n_bitrates;
  216. pos = skb_put(skb, supp_rates_len + 2);
  217. *pos++ = WLAN_EID_SUPP_RATES;
  218. *pos++ = supp_rates_len;
  219. count = 0;
  220. for (i = 0; i < sband->n_bitrates; i++) {
  221. if (BIT(i) & rates) {
  222. int rate = sband->bitrates[i].bitrate;
  223. *pos++ = (u8) (rate / 5);
  224. if (++count == 8)
  225. break;
  226. }
  227. }
  228. if (rates_len > count) {
  229. pos = skb_put(skb, rates_len - count + 2);
  230. *pos++ = WLAN_EID_EXT_SUPP_RATES;
  231. *pos++ = rates_len - count;
  232. for (i++; i < sband->n_bitrates; i++) {
  233. if (BIT(i) & rates) {
  234. int rate = sband->bitrates[i].bitrate;
  235. *pos++ = (u8) (rate / 5);
  236. }
  237. }
  238. }
  239. if (capab & WLAN_CAPABILITY_SPECTRUM_MGMT) {
  240. /* 1. power capabilities */
  241. pos = skb_put(skb, 4);
  242. *pos++ = WLAN_EID_PWR_CAPABILITY;
  243. *pos++ = 2;
  244. *pos++ = 0; /* min tx power */
  245. *pos++ = local->hw.conf.channel->max_power; /* max tx power */
  246. /* 2. supported channels */
  247. /* TODO: get this in reg domain format */
  248. pos = skb_put(skb, 2 * sband->n_channels + 2);
  249. *pos++ = WLAN_EID_SUPPORTED_CHANNELS;
  250. *pos++ = 2 * sband->n_channels;
  251. for (i = 0; i < sband->n_channels; i++) {
  252. *pos++ = ieee80211_frequency_to_channel(
  253. sband->channels[i].center_freq);
  254. *pos++ = 1; /* one channel in the subband*/
  255. }
  256. }
  257. if (ifmgd->extra_ie) {
  258. pos = skb_put(skb, ifmgd->extra_ie_len);
  259. memcpy(pos, ifmgd->extra_ie, ifmgd->extra_ie_len);
  260. }
  261. if (wmm && (ifmgd->flags & IEEE80211_STA_WMM_ENABLED)) {
  262. pos = skb_put(skb, 9);
  263. *pos++ = WLAN_EID_VENDOR_SPECIFIC;
  264. *pos++ = 7; /* len */
  265. *pos++ = 0x00; /* Microsoft OUI 00:50:F2 */
  266. *pos++ = 0x50;
  267. *pos++ = 0xf2;
  268. *pos++ = 2; /* WME */
  269. *pos++ = 0; /* WME info */
  270. *pos++ = 1; /* WME ver */
  271. *pos++ = 0;
  272. }
  273. /* wmm support is a must to HT */
  274. /*
  275. * IEEE802.11n does not allow TKIP/WEP as pairwise
  276. * ciphers in HT mode. We still associate in non-ht
  277. * mode (11a/b/g) if any one of these ciphers is
  278. * configured as pairwise.
  279. */
  280. if (wmm && (ifmgd->flags & IEEE80211_STA_WMM_ENABLED) &&
  281. sband->ht_cap.ht_supported &&
  282. (ht_ie = ieee80211_bss_get_ie(&bss->cbss, WLAN_EID_HT_INFORMATION)) &&
  283. ht_ie[1] >= sizeof(struct ieee80211_ht_info) &&
  284. (!(ifmgd->flags & IEEE80211_STA_DISABLE_11N))) {
  285. struct ieee80211_ht_info *ht_info =
  286. (struct ieee80211_ht_info *)(ht_ie + 2);
  287. u16 cap = sband->ht_cap.cap;
  288. __le16 tmp;
  289. u32 flags = local->hw.conf.channel->flags;
  290. switch (ht_info->ht_param & IEEE80211_HT_PARAM_CHA_SEC_OFFSET) {
  291. case IEEE80211_HT_PARAM_CHA_SEC_ABOVE:
  292. if (flags & IEEE80211_CHAN_NO_HT40PLUS) {
  293. cap &= ~IEEE80211_HT_CAP_SUP_WIDTH_20_40;
  294. cap &= ~IEEE80211_HT_CAP_SGI_40;
  295. }
  296. break;
  297. case IEEE80211_HT_PARAM_CHA_SEC_BELOW:
  298. if (flags & IEEE80211_CHAN_NO_HT40MINUS) {
  299. cap &= ~IEEE80211_HT_CAP_SUP_WIDTH_20_40;
  300. cap &= ~IEEE80211_HT_CAP_SGI_40;
  301. }
  302. break;
  303. }
  304. tmp = cpu_to_le16(cap);
  305. pos = skb_put(skb, sizeof(struct ieee80211_ht_cap)+2);
  306. *pos++ = WLAN_EID_HT_CAPABILITY;
  307. *pos++ = sizeof(struct ieee80211_ht_cap);
  308. memset(pos, 0, sizeof(struct ieee80211_ht_cap));
  309. memcpy(pos, &tmp, sizeof(u16));
  310. pos += sizeof(u16);
  311. /* TODO: needs a define here for << 2 */
  312. *pos++ = sband->ht_cap.ampdu_factor |
  313. (sband->ht_cap.ampdu_density << 2);
  314. memcpy(pos, &sband->ht_cap.mcs, sizeof(sband->ht_cap.mcs));
  315. }
  316. kfree(ifmgd->assocreq_ies);
  317. ifmgd->assocreq_ies_len = (skb->data + skb->len) - ies;
  318. ifmgd->assocreq_ies = kmalloc(ifmgd->assocreq_ies_len, GFP_KERNEL);
  319. if (ifmgd->assocreq_ies)
  320. memcpy(ifmgd->assocreq_ies, ies, ifmgd->assocreq_ies_len);
  321. ieee80211_tx_skb(sdata, skb, 0);
  322. }
  323. static void ieee80211_send_deauth_disassoc(struct ieee80211_sub_if_data *sdata,
  324. u16 stype, u16 reason)
  325. {
  326. struct ieee80211_local *local = sdata->local;
  327. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  328. struct sk_buff *skb;
  329. struct ieee80211_mgmt *mgmt;
  330. skb = dev_alloc_skb(local->hw.extra_tx_headroom + sizeof(*mgmt));
  331. if (!skb) {
  332. printk(KERN_DEBUG "%s: failed to allocate buffer for "
  333. "deauth/disassoc frame\n", sdata->dev->name);
  334. return;
  335. }
  336. skb_reserve(skb, local->hw.extra_tx_headroom);
  337. mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
  338. memset(mgmt, 0, 24);
  339. memcpy(mgmt->da, ifmgd->bssid, ETH_ALEN);
  340. memcpy(mgmt->sa, sdata->dev->dev_addr, ETH_ALEN);
  341. memcpy(mgmt->bssid, ifmgd->bssid, ETH_ALEN);
  342. mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT | stype);
  343. skb_put(skb, 2);
  344. /* u.deauth.reason_code == u.disassoc.reason_code */
  345. mgmt->u.deauth.reason_code = cpu_to_le16(reason);
  346. if (stype == IEEE80211_STYPE_DEAUTH)
  347. cfg80211_send_deauth(sdata->dev, (u8 *) mgmt, skb->len,
  348. GFP_KERNEL);
  349. else
  350. cfg80211_send_disassoc(sdata->dev, (u8 *) mgmt, skb->len,
  351. GFP_KERNEL);
  352. ieee80211_tx_skb(sdata, skb, ifmgd->flags & IEEE80211_STA_MFP_ENABLED);
  353. }
  354. void ieee80211_send_pspoll(struct ieee80211_local *local,
  355. struct ieee80211_sub_if_data *sdata)
  356. {
  357. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  358. struct ieee80211_pspoll *pspoll;
  359. struct sk_buff *skb;
  360. u16 fc;
  361. skb = dev_alloc_skb(local->hw.extra_tx_headroom + sizeof(*pspoll));
  362. if (!skb) {
  363. printk(KERN_DEBUG "%s: failed to allocate buffer for "
  364. "pspoll frame\n", sdata->dev->name);
  365. return;
  366. }
  367. skb_reserve(skb, local->hw.extra_tx_headroom);
  368. pspoll = (struct ieee80211_pspoll *) skb_put(skb, sizeof(*pspoll));
  369. memset(pspoll, 0, sizeof(*pspoll));
  370. fc = IEEE80211_FTYPE_CTL | IEEE80211_STYPE_PSPOLL | IEEE80211_FCTL_PM;
  371. pspoll->frame_control = cpu_to_le16(fc);
  372. pspoll->aid = cpu_to_le16(ifmgd->aid);
  373. /* aid in PS-Poll has its two MSBs each set to 1 */
  374. pspoll->aid |= cpu_to_le16(1 << 15 | 1 << 14);
  375. memcpy(pspoll->bssid, ifmgd->bssid, ETH_ALEN);
  376. memcpy(pspoll->ta, sdata->dev->dev_addr, ETH_ALEN);
  377. ieee80211_tx_skb(sdata, skb, 0);
  378. }
  379. void ieee80211_send_nullfunc(struct ieee80211_local *local,
  380. struct ieee80211_sub_if_data *sdata,
  381. int powersave)
  382. {
  383. struct sk_buff *skb;
  384. struct ieee80211_hdr *nullfunc;
  385. __le16 fc;
  386. if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_STATION))
  387. return;
  388. skb = dev_alloc_skb(local->hw.extra_tx_headroom + 24);
  389. if (!skb) {
  390. printk(KERN_DEBUG "%s: failed to allocate buffer for nullfunc "
  391. "frame\n", sdata->dev->name);
  392. return;
  393. }
  394. skb_reserve(skb, local->hw.extra_tx_headroom);
  395. nullfunc = (struct ieee80211_hdr *) skb_put(skb, 24);
  396. memset(nullfunc, 0, 24);
  397. fc = cpu_to_le16(IEEE80211_FTYPE_DATA | IEEE80211_STYPE_NULLFUNC |
  398. IEEE80211_FCTL_TODS);
  399. if (powersave)
  400. fc |= cpu_to_le16(IEEE80211_FCTL_PM);
  401. nullfunc->frame_control = fc;
  402. memcpy(nullfunc->addr1, sdata->u.mgd.bssid, ETH_ALEN);
  403. memcpy(nullfunc->addr2, sdata->dev->dev_addr, ETH_ALEN);
  404. memcpy(nullfunc->addr3, sdata->u.mgd.bssid, ETH_ALEN);
  405. ieee80211_tx_skb(sdata, skb, 0);
  406. }
  407. /* spectrum management related things */
  408. static void ieee80211_chswitch_work(struct work_struct *work)
  409. {
  410. struct ieee80211_sub_if_data *sdata =
  411. container_of(work, struct ieee80211_sub_if_data, u.mgd.chswitch_work);
  412. struct ieee80211_bss *bss;
  413. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  414. if (!netif_running(sdata->dev))
  415. return;
  416. bss = ieee80211_rx_bss_get(sdata->local, ifmgd->bssid,
  417. sdata->local->hw.conf.channel->center_freq,
  418. ifmgd->ssid, ifmgd->ssid_len);
  419. if (!bss)
  420. goto exit;
  421. sdata->local->oper_channel = sdata->local->csa_channel;
  422. /* XXX: shouldn't really modify cfg80211-owned data! */
  423. if (!ieee80211_hw_config(sdata->local, IEEE80211_CONF_CHANGE_CHANNEL))
  424. bss->cbss.channel = sdata->local->oper_channel;
  425. ieee80211_rx_bss_put(sdata->local, bss);
  426. exit:
  427. ifmgd->flags &= ~IEEE80211_STA_CSA_RECEIVED;
  428. ieee80211_wake_queues_by_reason(&sdata->local->hw,
  429. IEEE80211_QUEUE_STOP_REASON_CSA);
  430. }
  431. static void ieee80211_chswitch_timer(unsigned long data)
  432. {
  433. struct ieee80211_sub_if_data *sdata =
  434. (struct ieee80211_sub_if_data *) data;
  435. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  436. if (sdata->local->quiescing) {
  437. set_bit(TMR_RUNNING_CHANSW, &ifmgd->timers_running);
  438. return;
  439. }
  440. queue_work(sdata->local->hw.workqueue, &ifmgd->chswitch_work);
  441. }
  442. void ieee80211_sta_process_chanswitch(struct ieee80211_sub_if_data *sdata,
  443. struct ieee80211_channel_sw_ie *sw_elem,
  444. struct ieee80211_bss *bss)
  445. {
  446. struct ieee80211_channel *new_ch;
  447. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  448. int new_freq = ieee80211_channel_to_frequency(sw_elem->new_ch_num);
  449. if (ifmgd->state != IEEE80211_STA_MLME_ASSOCIATED)
  450. return;
  451. if (sdata->local->sw_scanning || sdata->local->hw_scanning)
  452. return;
  453. /* Disregard subsequent beacons if we are already running a timer
  454. processing a CSA */
  455. if (ifmgd->flags & IEEE80211_STA_CSA_RECEIVED)
  456. return;
  457. new_ch = ieee80211_get_channel(sdata->local->hw.wiphy, new_freq);
  458. if (!new_ch || new_ch->flags & IEEE80211_CHAN_DISABLED)
  459. return;
  460. sdata->local->csa_channel = new_ch;
  461. if (sw_elem->count <= 1) {
  462. queue_work(sdata->local->hw.workqueue, &ifmgd->chswitch_work);
  463. } else {
  464. ieee80211_stop_queues_by_reason(&sdata->local->hw,
  465. IEEE80211_QUEUE_STOP_REASON_CSA);
  466. ifmgd->flags |= IEEE80211_STA_CSA_RECEIVED;
  467. mod_timer(&ifmgd->chswitch_timer,
  468. jiffies +
  469. msecs_to_jiffies(sw_elem->count *
  470. bss->cbss.beacon_interval));
  471. }
  472. }
  473. static void ieee80211_handle_pwr_constr(struct ieee80211_sub_if_data *sdata,
  474. u16 capab_info, u8 *pwr_constr_elem,
  475. u8 pwr_constr_elem_len)
  476. {
  477. struct ieee80211_conf *conf = &sdata->local->hw.conf;
  478. if (!(capab_info & WLAN_CAPABILITY_SPECTRUM_MGMT))
  479. return;
  480. /* Power constraint IE length should be 1 octet */
  481. if (pwr_constr_elem_len != 1)
  482. return;
  483. if ((*pwr_constr_elem <= conf->channel->max_power) &&
  484. (*pwr_constr_elem != sdata->local->power_constr_level)) {
  485. sdata->local->power_constr_level = *pwr_constr_elem;
  486. ieee80211_hw_config(sdata->local, 0);
  487. }
  488. }
  489. /* powersave */
  490. static void ieee80211_enable_ps(struct ieee80211_local *local,
  491. struct ieee80211_sub_if_data *sdata)
  492. {
  493. struct ieee80211_conf *conf = &local->hw.conf;
  494. /*
  495. * If we are scanning right now then the parameters will
  496. * take effect when scan finishes.
  497. */
  498. if (local->hw_scanning || local->sw_scanning)
  499. return;
  500. if (conf->dynamic_ps_timeout > 0 &&
  501. !(local->hw.flags & IEEE80211_HW_SUPPORTS_DYNAMIC_PS)) {
  502. mod_timer(&local->dynamic_ps_timer, jiffies +
  503. msecs_to_jiffies(conf->dynamic_ps_timeout));
  504. } else {
  505. if (local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK)
  506. ieee80211_send_nullfunc(local, sdata, 1);
  507. conf->flags |= IEEE80211_CONF_PS;
  508. ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
  509. }
  510. }
  511. static void ieee80211_change_ps(struct ieee80211_local *local)
  512. {
  513. struct ieee80211_conf *conf = &local->hw.conf;
  514. if (local->ps_sdata) {
  515. ieee80211_enable_ps(local, local->ps_sdata);
  516. } else if (conf->flags & IEEE80211_CONF_PS) {
  517. conf->flags &= ~IEEE80211_CONF_PS;
  518. ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
  519. del_timer_sync(&local->dynamic_ps_timer);
  520. cancel_work_sync(&local->dynamic_ps_enable_work);
  521. }
  522. }
  523. /* need to hold RTNL or interface lock */
  524. void ieee80211_recalc_ps(struct ieee80211_local *local, s32 latency)
  525. {
  526. struct ieee80211_sub_if_data *sdata, *found = NULL;
  527. int count = 0;
  528. if (!(local->hw.flags & IEEE80211_HW_SUPPORTS_PS)) {
  529. local->ps_sdata = NULL;
  530. return;
  531. }
  532. list_for_each_entry(sdata, &local->interfaces, list) {
  533. if (!netif_running(sdata->dev))
  534. continue;
  535. if (sdata->vif.type != NL80211_IFTYPE_STATION)
  536. continue;
  537. found = sdata;
  538. count++;
  539. }
  540. if (count == 1 && found->u.mgd.powersave &&
  541. (found->u.mgd.flags & IEEE80211_STA_ASSOCIATED) &&
  542. !(found->u.mgd.flags & IEEE80211_STA_PROBEREQ_POLL)) {
  543. s32 beaconint_us;
  544. if (latency < 0)
  545. latency = pm_qos_requirement(PM_QOS_NETWORK_LATENCY);
  546. beaconint_us = ieee80211_tu_to_usec(
  547. found->vif.bss_conf.beacon_int);
  548. if (beaconint_us > latency) {
  549. local->ps_sdata = NULL;
  550. } else {
  551. u8 dtimper = found->vif.bss_conf.dtim_period;
  552. int maxslp = 1;
  553. if (dtimper > 1)
  554. maxslp = min_t(int, dtimper,
  555. latency / beaconint_us);
  556. local->hw.conf.max_sleep_period = maxslp;
  557. local->ps_sdata = found;
  558. }
  559. } else {
  560. local->ps_sdata = NULL;
  561. }
  562. ieee80211_change_ps(local);
  563. }
  564. void ieee80211_dynamic_ps_disable_work(struct work_struct *work)
  565. {
  566. struct ieee80211_local *local =
  567. container_of(work, struct ieee80211_local,
  568. dynamic_ps_disable_work);
  569. if (local->hw.conf.flags & IEEE80211_CONF_PS) {
  570. local->hw.conf.flags &= ~IEEE80211_CONF_PS;
  571. ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
  572. }
  573. ieee80211_wake_queues_by_reason(&local->hw,
  574. IEEE80211_QUEUE_STOP_REASON_PS);
  575. }
  576. void ieee80211_dynamic_ps_enable_work(struct work_struct *work)
  577. {
  578. struct ieee80211_local *local =
  579. container_of(work, struct ieee80211_local,
  580. dynamic_ps_enable_work);
  581. struct ieee80211_sub_if_data *sdata = local->ps_sdata;
  582. /* can only happen when PS was just disabled anyway */
  583. if (!sdata)
  584. return;
  585. if (local->hw.conf.flags & IEEE80211_CONF_PS)
  586. return;
  587. if (local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK)
  588. ieee80211_send_nullfunc(local, sdata, 1);
  589. local->hw.conf.flags |= IEEE80211_CONF_PS;
  590. ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
  591. }
  592. void ieee80211_dynamic_ps_timer(unsigned long data)
  593. {
  594. struct ieee80211_local *local = (void *) data;
  595. if (local->quiescing)
  596. return;
  597. queue_work(local->hw.workqueue, &local->dynamic_ps_enable_work);
  598. }
  599. /* MLME */
  600. static void ieee80211_sta_wmm_params(struct ieee80211_local *local,
  601. struct ieee80211_if_managed *ifmgd,
  602. u8 *wmm_param, size_t wmm_param_len)
  603. {
  604. struct ieee80211_tx_queue_params params;
  605. size_t left;
  606. int count;
  607. u8 *pos;
  608. if (!(ifmgd->flags & IEEE80211_STA_WMM_ENABLED))
  609. return;
  610. if (!wmm_param)
  611. return;
  612. if (wmm_param_len < 8 || wmm_param[5] /* version */ != 1)
  613. return;
  614. count = wmm_param[6] & 0x0f;
  615. if (count == ifmgd->wmm_last_param_set)
  616. return;
  617. ifmgd->wmm_last_param_set = count;
  618. pos = wmm_param + 8;
  619. left = wmm_param_len - 8;
  620. memset(&params, 0, sizeof(params));
  621. local->wmm_acm = 0;
  622. for (; left >= 4; left -= 4, pos += 4) {
  623. int aci = (pos[0] >> 5) & 0x03;
  624. int acm = (pos[0] >> 4) & 0x01;
  625. int queue;
  626. switch (aci) {
  627. case 1: /* AC_BK */
  628. queue = 3;
  629. if (acm)
  630. local->wmm_acm |= BIT(1) | BIT(2); /* BK/- */
  631. break;
  632. case 2: /* AC_VI */
  633. queue = 1;
  634. if (acm)
  635. local->wmm_acm |= BIT(4) | BIT(5); /* CL/VI */
  636. break;
  637. case 3: /* AC_VO */
  638. queue = 0;
  639. if (acm)
  640. local->wmm_acm |= BIT(6) | BIT(7); /* VO/NC */
  641. break;
  642. case 0: /* AC_BE */
  643. default:
  644. queue = 2;
  645. if (acm)
  646. local->wmm_acm |= BIT(0) | BIT(3); /* BE/EE */
  647. break;
  648. }
  649. params.aifs = pos[0] & 0x0f;
  650. params.cw_max = ecw2cw((pos[1] & 0xf0) >> 4);
  651. params.cw_min = ecw2cw(pos[1] & 0x0f);
  652. params.txop = get_unaligned_le16(pos + 2);
  653. #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
  654. printk(KERN_DEBUG "%s: WMM queue=%d aci=%d acm=%d aifs=%d "
  655. "cWmin=%d cWmax=%d txop=%d\n",
  656. wiphy_name(local->hw.wiphy), queue, aci, acm,
  657. params.aifs, params.cw_min, params.cw_max, params.txop);
  658. #endif
  659. if (drv_conf_tx(local, queue, &params) && local->ops->conf_tx)
  660. printk(KERN_DEBUG "%s: failed to set TX queue "
  661. "parameters for queue %d\n",
  662. wiphy_name(local->hw.wiphy), queue);
  663. }
  664. }
  665. static u32 ieee80211_handle_bss_capability(struct ieee80211_sub_if_data *sdata,
  666. u16 capab, bool erp_valid, u8 erp)
  667. {
  668. struct ieee80211_bss_conf *bss_conf = &sdata->vif.bss_conf;
  669. #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
  670. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  671. #endif
  672. u32 changed = 0;
  673. bool use_protection;
  674. bool use_short_preamble;
  675. bool use_short_slot;
  676. if (erp_valid) {
  677. use_protection = (erp & WLAN_ERP_USE_PROTECTION) != 0;
  678. use_short_preamble = (erp & WLAN_ERP_BARKER_PREAMBLE) == 0;
  679. } else {
  680. use_protection = false;
  681. use_short_preamble = !!(capab & WLAN_CAPABILITY_SHORT_PREAMBLE);
  682. }
  683. use_short_slot = !!(capab & WLAN_CAPABILITY_SHORT_SLOT_TIME);
  684. if (use_protection != bss_conf->use_cts_prot) {
  685. #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
  686. if (net_ratelimit()) {
  687. printk(KERN_DEBUG "%s: CTS protection %s (BSSID=%pM)\n",
  688. sdata->dev->name,
  689. use_protection ? "enabled" : "disabled",
  690. ifmgd->bssid);
  691. }
  692. #endif
  693. bss_conf->use_cts_prot = use_protection;
  694. changed |= BSS_CHANGED_ERP_CTS_PROT;
  695. }
  696. if (use_short_preamble != bss_conf->use_short_preamble) {
  697. #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
  698. if (net_ratelimit()) {
  699. printk(KERN_DEBUG "%s: switched to %s barker preamble"
  700. " (BSSID=%pM)\n",
  701. sdata->dev->name,
  702. use_short_preamble ? "short" : "long",
  703. ifmgd->bssid);
  704. }
  705. #endif
  706. bss_conf->use_short_preamble = use_short_preamble;
  707. changed |= BSS_CHANGED_ERP_PREAMBLE;
  708. }
  709. if (use_short_slot != bss_conf->use_short_slot) {
  710. #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
  711. if (net_ratelimit()) {
  712. printk(KERN_DEBUG "%s: switched to %s slot time"
  713. " (BSSID=%pM)\n",
  714. sdata->dev->name,
  715. use_short_slot ? "short" : "long",
  716. ifmgd->bssid);
  717. }
  718. #endif
  719. bss_conf->use_short_slot = use_short_slot;
  720. changed |= BSS_CHANGED_ERP_SLOT;
  721. }
  722. return changed;
  723. }
  724. static void ieee80211_set_associated(struct ieee80211_sub_if_data *sdata,
  725. u32 bss_info_changed)
  726. {
  727. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  728. struct ieee80211_local *local = sdata->local;
  729. struct ieee80211_conf *conf = &local_to_hw(local)->conf;
  730. struct ieee80211_bss *bss;
  731. bss_info_changed |= BSS_CHANGED_ASSOC;
  732. ifmgd->flags |= IEEE80211_STA_ASSOCIATED;
  733. bss = ieee80211_rx_bss_get(local, ifmgd->bssid,
  734. conf->channel->center_freq,
  735. ifmgd->ssid, ifmgd->ssid_len);
  736. if (bss) {
  737. /* set timing information */
  738. sdata->vif.bss_conf.beacon_int = bss->cbss.beacon_interval;
  739. sdata->vif.bss_conf.timestamp = bss->cbss.tsf;
  740. sdata->vif.bss_conf.dtim_period = bss->dtim_period;
  741. bss_info_changed |= BSS_CHANGED_BEACON_INT;
  742. bss_info_changed |= ieee80211_handle_bss_capability(sdata,
  743. bss->cbss.capability, bss->has_erp_value, bss->erp_value);
  744. ieee80211_rx_bss_put(local, bss);
  745. }
  746. ifmgd->last_probe = jiffies;
  747. ieee80211_led_assoc(local, 1);
  748. sdata->vif.bss_conf.assoc = 1;
  749. /*
  750. * For now just always ask the driver to update the basic rateset
  751. * when we have associated, we aren't checking whether it actually
  752. * changed or not.
  753. */
  754. bss_info_changed |= BSS_CHANGED_BASIC_RATES;
  755. /* And the BSSID changed - we're associated now */
  756. bss_info_changed |= BSS_CHANGED_BSSID;
  757. ieee80211_bss_info_change_notify(sdata, bss_info_changed);
  758. /* will be same as sdata */
  759. if (local->ps_sdata) {
  760. mutex_lock(&local->iflist_mtx);
  761. ieee80211_recalc_ps(local, -1);
  762. mutex_unlock(&local->iflist_mtx);
  763. }
  764. netif_tx_start_all_queues(sdata->dev);
  765. netif_carrier_on(sdata->dev);
  766. }
  767. static void ieee80211_direct_probe(struct ieee80211_sub_if_data *sdata)
  768. {
  769. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  770. struct ieee80211_local *local = sdata->local;
  771. ifmgd->direct_probe_tries++;
  772. if (ifmgd->direct_probe_tries > IEEE80211_AUTH_MAX_TRIES) {
  773. printk(KERN_DEBUG "%s: direct probe to AP %pM timed out\n",
  774. sdata->dev->name, ifmgd->bssid);
  775. ifmgd->state = IEEE80211_STA_MLME_DISABLED;
  776. ieee80211_recalc_idle(local);
  777. cfg80211_send_auth_timeout(sdata->dev, ifmgd->bssid,
  778. GFP_KERNEL);
  779. /*
  780. * Most likely AP is not in the range so remove the
  781. * bss information associated to the AP
  782. */
  783. ieee80211_rx_bss_remove(sdata, ifmgd->bssid,
  784. sdata->local->hw.conf.channel->center_freq,
  785. ifmgd->ssid, ifmgd->ssid_len);
  786. /*
  787. * We might have a pending scan which had no chance to run yet
  788. * due to state == IEEE80211_STA_MLME_DIRECT_PROBE.
  789. * Hence, queue the STAs work again
  790. */
  791. queue_work(local->hw.workqueue, &ifmgd->work);
  792. return;
  793. }
  794. printk(KERN_DEBUG "%s: direct probe to AP %pM try %d\n",
  795. sdata->dev->name, ifmgd->bssid,
  796. ifmgd->direct_probe_tries);
  797. ifmgd->state = IEEE80211_STA_MLME_DIRECT_PROBE;
  798. /* Direct probe is sent to broadcast address as some APs
  799. * will not answer to direct packet in unassociated state.
  800. */
  801. ieee80211_send_probe_req(sdata, NULL,
  802. ifmgd->ssid, ifmgd->ssid_len, NULL, 0);
  803. mod_timer(&ifmgd->timer, jiffies + IEEE80211_AUTH_TIMEOUT);
  804. }
  805. static void ieee80211_authenticate(struct ieee80211_sub_if_data *sdata)
  806. {
  807. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  808. struct ieee80211_local *local = sdata->local;
  809. ifmgd->auth_tries++;
  810. if (ifmgd->auth_tries > IEEE80211_AUTH_MAX_TRIES) {
  811. printk(KERN_DEBUG "%s: authentication with AP %pM"
  812. " timed out\n",
  813. sdata->dev->name, ifmgd->bssid);
  814. ifmgd->state = IEEE80211_STA_MLME_DISABLED;
  815. ieee80211_recalc_idle(local);
  816. cfg80211_send_auth_timeout(sdata->dev, ifmgd->bssid,
  817. GFP_KERNEL);
  818. ieee80211_rx_bss_remove(sdata, ifmgd->bssid,
  819. sdata->local->hw.conf.channel->center_freq,
  820. ifmgd->ssid, ifmgd->ssid_len);
  821. /*
  822. * We might have a pending scan which had no chance to run yet
  823. * due to state == IEEE80211_STA_MLME_AUTHENTICATE.
  824. * Hence, queue the STAs work again
  825. */
  826. queue_work(local->hw.workqueue, &ifmgd->work);
  827. return;
  828. }
  829. ifmgd->state = IEEE80211_STA_MLME_AUTHENTICATE;
  830. printk(KERN_DEBUG "%s: authenticate with AP %pM\n",
  831. sdata->dev->name, ifmgd->bssid);
  832. ieee80211_send_auth(sdata, 1, ifmgd->auth_alg, ifmgd->sme_auth_ie,
  833. ifmgd->sme_auth_ie_len, ifmgd->bssid, 0);
  834. ifmgd->auth_transaction = 2;
  835. mod_timer(&ifmgd->timer, jiffies + IEEE80211_AUTH_TIMEOUT);
  836. }
  837. /*
  838. * The disassoc 'reason' argument can be either our own reason
  839. * if self disconnected or a reason code from the AP.
  840. */
  841. static void ieee80211_set_disassoc(struct ieee80211_sub_if_data *sdata,
  842. bool deauth, bool self_disconnected,
  843. u16 reason)
  844. {
  845. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  846. struct ieee80211_local *local = sdata->local;
  847. struct ieee80211_conf *conf = &local_to_hw(local)->conf;
  848. struct ieee80211_bss *bss;
  849. struct sta_info *sta;
  850. u32 changed = 0, config_changed = 0;
  851. if (deauth) {
  852. ifmgd->direct_probe_tries = 0;
  853. ifmgd->auth_tries = 0;
  854. }
  855. ifmgd->assoc_scan_tries = 0;
  856. ifmgd->assoc_tries = 0;
  857. netif_tx_stop_all_queues(sdata->dev);
  858. netif_carrier_off(sdata->dev);
  859. rcu_read_lock();
  860. sta = sta_info_get(local, ifmgd->bssid);
  861. if (sta)
  862. ieee80211_sta_tear_down_BA_sessions(sta);
  863. rcu_read_unlock();
  864. bss = ieee80211_rx_bss_get(local, ifmgd->bssid,
  865. conf->channel->center_freq,
  866. ifmgd->ssid, ifmgd->ssid_len);
  867. if (bss)
  868. ieee80211_rx_bss_put(local, bss);
  869. if (self_disconnected) {
  870. if (deauth)
  871. ieee80211_send_deauth_disassoc(sdata,
  872. IEEE80211_STYPE_DEAUTH, reason);
  873. else
  874. ieee80211_send_deauth_disassoc(sdata,
  875. IEEE80211_STYPE_DISASSOC, reason);
  876. }
  877. ifmgd->flags &= ~IEEE80211_STA_ASSOCIATED;
  878. changed |= ieee80211_reset_erp_info(sdata);
  879. ieee80211_led_assoc(local, 0);
  880. changed |= BSS_CHANGED_ASSOC;
  881. sdata->vif.bss_conf.assoc = false;
  882. if (self_disconnected || reason == WLAN_REASON_DISASSOC_STA_HAS_LEFT) {
  883. ifmgd->state = IEEE80211_STA_MLME_DISABLED;
  884. ieee80211_rx_bss_remove(sdata, ifmgd->bssid,
  885. sdata->local->hw.conf.channel->center_freq,
  886. ifmgd->ssid, ifmgd->ssid_len);
  887. }
  888. ieee80211_set_wmm_default(sdata);
  889. ieee80211_recalc_idle(local);
  890. /* channel(_type) changes are handled by ieee80211_hw_config */
  891. local->oper_channel_type = NL80211_CHAN_NO_HT;
  892. /* on the next assoc, re-program HT parameters */
  893. sdata->ht_opmode_valid = false;
  894. local->power_constr_level = 0;
  895. del_timer_sync(&local->dynamic_ps_timer);
  896. cancel_work_sync(&local->dynamic_ps_enable_work);
  897. if (local->hw.conf.flags & IEEE80211_CONF_PS) {
  898. local->hw.conf.flags &= ~IEEE80211_CONF_PS;
  899. config_changed |= IEEE80211_CONF_CHANGE_PS;
  900. }
  901. ieee80211_hw_config(local, config_changed);
  902. /* And the BSSID changed -- not very interesting here */
  903. changed |= BSS_CHANGED_BSSID;
  904. ieee80211_bss_info_change_notify(sdata, changed);
  905. rcu_read_lock();
  906. sta = sta_info_get(local, ifmgd->bssid);
  907. if (!sta) {
  908. rcu_read_unlock();
  909. return;
  910. }
  911. sta_info_unlink(&sta);
  912. rcu_read_unlock();
  913. sta_info_destroy(sta);
  914. }
  915. static void ieee80211_associate(struct ieee80211_sub_if_data *sdata)
  916. {
  917. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  918. struct ieee80211_local *local = sdata->local;
  919. ifmgd->assoc_tries++;
  920. if (ifmgd->assoc_tries > IEEE80211_ASSOC_MAX_TRIES) {
  921. printk(KERN_DEBUG "%s: association with AP %pM"
  922. " timed out\n",
  923. sdata->dev->name, ifmgd->bssid);
  924. ifmgd->state = IEEE80211_STA_MLME_DISABLED;
  925. ieee80211_recalc_idle(local);
  926. cfg80211_send_assoc_timeout(sdata->dev, ifmgd->bssid,
  927. GFP_KERNEL);
  928. ieee80211_rx_bss_remove(sdata, ifmgd->bssid,
  929. sdata->local->hw.conf.channel->center_freq,
  930. ifmgd->ssid, ifmgd->ssid_len);
  931. /*
  932. * We might have a pending scan which had no chance to run yet
  933. * due to state == IEEE80211_STA_MLME_ASSOCIATE.
  934. * Hence, queue the STAs work again
  935. */
  936. queue_work(local->hw.workqueue, &ifmgd->work);
  937. return;
  938. }
  939. ifmgd->state = IEEE80211_STA_MLME_ASSOCIATE;
  940. printk(KERN_DEBUG "%s: associate with AP %pM\n",
  941. sdata->dev->name, ifmgd->bssid);
  942. ieee80211_send_assoc(sdata);
  943. mod_timer(&ifmgd->timer, jiffies + IEEE80211_ASSOC_TIMEOUT);
  944. }
  945. void ieee80211_sta_rx_notify(struct ieee80211_sub_if_data *sdata,
  946. struct ieee80211_hdr *hdr)
  947. {
  948. /*
  949. * We can postpone the mgd.timer whenever receiving unicast frames
  950. * from AP because we know that the connection is working both ways
  951. * at that time. But multicast frames (and hence also beacons) must
  952. * be ignored here, because we need to trigger the timer during
  953. * data idle periods for sending the periodical probe request to
  954. * the AP.
  955. */
  956. if (!is_multicast_ether_addr(hdr->addr1))
  957. mod_timer(&sdata->u.mgd.timer,
  958. jiffies + IEEE80211_MONITORING_INTERVAL);
  959. }
  960. void ieee80211_beacon_loss_work(struct work_struct *work)
  961. {
  962. struct ieee80211_sub_if_data *sdata =
  963. container_of(work, struct ieee80211_sub_if_data,
  964. u.mgd.beacon_loss_work);
  965. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  966. /*
  967. * The driver has already reported this event and we have
  968. * already sent a probe request. Maybe the AP died and the
  969. * driver keeps reporting until we disassociate... We have
  970. * to ignore that because otherwise we would continually
  971. * reset the timer and never check whether we received a
  972. * probe response!
  973. */
  974. if (ifmgd->flags & IEEE80211_STA_PROBEREQ_POLL)
  975. return;
  976. #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
  977. if (net_ratelimit()) {
  978. printk(KERN_DEBUG "%s: driver reports beacon loss from AP %pM "
  979. "- sending probe request\n", sdata->dev->name,
  980. sdata->u.mgd.bssid);
  981. }
  982. #endif
  983. ifmgd->flags |= IEEE80211_STA_PROBEREQ_POLL;
  984. mutex_lock(&sdata->local->iflist_mtx);
  985. ieee80211_recalc_ps(sdata->local, -1);
  986. mutex_unlock(&sdata->local->iflist_mtx);
  987. ieee80211_send_probe_req(sdata, ifmgd->bssid, ifmgd->ssid,
  988. ifmgd->ssid_len, NULL, 0);
  989. mod_timer(&ifmgd->timer, jiffies + IEEE80211_PROBE_WAIT);
  990. }
  991. void ieee80211_beacon_loss(struct ieee80211_vif *vif)
  992. {
  993. struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
  994. queue_work(sdata->local->hw.workqueue,
  995. &sdata->u.mgd.beacon_loss_work);
  996. }
  997. EXPORT_SYMBOL(ieee80211_beacon_loss);
  998. static void ieee80211_associated(struct ieee80211_sub_if_data *sdata)
  999. {
  1000. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  1001. struct ieee80211_local *local = sdata->local;
  1002. struct sta_info *sta;
  1003. unsigned long last_rx;
  1004. bool disassoc = false;
  1005. /* TODO: start monitoring current AP signal quality and number of
  1006. * missed beacons. Scan other channels every now and then and search
  1007. * for better APs. */
  1008. /* TODO: remove expired BSSes */
  1009. ifmgd->state = IEEE80211_STA_MLME_ASSOCIATED;
  1010. rcu_read_lock();
  1011. sta = sta_info_get(local, ifmgd->bssid);
  1012. if (!sta) {
  1013. printk(KERN_DEBUG "%s: No STA entry for own AP %pM\n",
  1014. sdata->dev->name, ifmgd->bssid);
  1015. disassoc = true;
  1016. rcu_read_unlock();
  1017. goto out;
  1018. }
  1019. last_rx = sta->last_rx;
  1020. rcu_read_unlock();
  1021. if ((ifmgd->flags & IEEE80211_STA_PROBEREQ_POLL) &&
  1022. time_after(jiffies, last_rx + IEEE80211_PROBE_WAIT)) {
  1023. printk(KERN_DEBUG "%s: no probe response from AP %pM "
  1024. "- disassociating\n",
  1025. sdata->dev->name, ifmgd->bssid);
  1026. disassoc = true;
  1027. ifmgd->flags &= ~IEEE80211_STA_PROBEREQ_POLL;
  1028. goto out;
  1029. }
  1030. /*
  1031. * Beacon filtering is only enabled with power save and then the
  1032. * stack should not check for beacon loss.
  1033. */
  1034. if (!((local->hw.flags & IEEE80211_HW_BEACON_FILTER) &&
  1035. (local->hw.conf.flags & IEEE80211_CONF_PS)) &&
  1036. time_after(jiffies,
  1037. ifmgd->last_beacon + IEEE80211_MONITORING_INTERVAL)) {
  1038. #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
  1039. if (net_ratelimit()) {
  1040. printk(KERN_DEBUG "%s: beacon loss from AP %pM "
  1041. "- sending probe request\n",
  1042. sdata->dev->name, ifmgd->bssid);
  1043. }
  1044. #endif
  1045. ifmgd->flags |= IEEE80211_STA_PROBEREQ_POLL;
  1046. mutex_lock(&local->iflist_mtx);
  1047. ieee80211_recalc_ps(local, -1);
  1048. mutex_unlock(&local->iflist_mtx);
  1049. ieee80211_send_probe_req(sdata, ifmgd->bssid, ifmgd->ssid,
  1050. ifmgd->ssid_len, NULL, 0);
  1051. mod_timer(&ifmgd->timer, jiffies + IEEE80211_PROBE_WAIT);
  1052. goto out;
  1053. }
  1054. if (time_after(jiffies, last_rx + IEEE80211_PROBE_IDLE_TIME)) {
  1055. ifmgd->flags |= IEEE80211_STA_PROBEREQ_POLL;
  1056. mutex_lock(&local->iflist_mtx);
  1057. ieee80211_recalc_ps(local, -1);
  1058. mutex_unlock(&local->iflist_mtx);
  1059. ieee80211_send_probe_req(sdata, ifmgd->bssid, ifmgd->ssid,
  1060. ifmgd->ssid_len, NULL, 0);
  1061. }
  1062. out:
  1063. if (!disassoc)
  1064. mod_timer(&ifmgd->timer,
  1065. jiffies + IEEE80211_MONITORING_INTERVAL);
  1066. else
  1067. ieee80211_set_disassoc(sdata, true, true,
  1068. WLAN_REASON_PREV_AUTH_NOT_VALID);
  1069. }
  1070. static void ieee80211_auth_completed(struct ieee80211_sub_if_data *sdata)
  1071. {
  1072. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  1073. printk(KERN_DEBUG "%s: authenticated\n", sdata->dev->name);
  1074. ifmgd->flags |= IEEE80211_STA_AUTHENTICATED;
  1075. /* Wait for SME to request association */
  1076. ifmgd->state = IEEE80211_STA_MLME_DISABLED;
  1077. ieee80211_recalc_idle(sdata->local);
  1078. }
  1079. static void ieee80211_auth_challenge(struct ieee80211_sub_if_data *sdata,
  1080. struct ieee80211_mgmt *mgmt,
  1081. size_t len)
  1082. {
  1083. u8 *pos;
  1084. struct ieee802_11_elems elems;
  1085. pos = mgmt->u.auth.variable;
  1086. ieee802_11_parse_elems(pos, len - (pos - (u8 *) mgmt), &elems);
  1087. if (!elems.challenge)
  1088. return;
  1089. ieee80211_send_auth(sdata, 3, sdata->u.mgd.auth_alg,
  1090. elems.challenge - 2, elems.challenge_len + 2,
  1091. sdata->u.mgd.bssid, 1);
  1092. sdata->u.mgd.auth_transaction = 4;
  1093. }
  1094. static void ieee80211_rx_mgmt_auth(struct ieee80211_sub_if_data *sdata,
  1095. struct ieee80211_mgmt *mgmt,
  1096. size_t len)
  1097. {
  1098. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  1099. u16 auth_alg, auth_transaction, status_code;
  1100. if (ifmgd->state != IEEE80211_STA_MLME_AUTHENTICATE)
  1101. return;
  1102. if (len < 24 + 6)
  1103. return;
  1104. if (memcmp(ifmgd->bssid, mgmt->sa, ETH_ALEN) != 0)
  1105. return;
  1106. if (memcmp(ifmgd->bssid, mgmt->bssid, ETH_ALEN) != 0)
  1107. return;
  1108. auth_alg = le16_to_cpu(mgmt->u.auth.auth_alg);
  1109. auth_transaction = le16_to_cpu(mgmt->u.auth.auth_transaction);
  1110. status_code = le16_to_cpu(mgmt->u.auth.status_code);
  1111. if (auth_alg != ifmgd->auth_alg ||
  1112. auth_transaction != ifmgd->auth_transaction)
  1113. return;
  1114. if (status_code != WLAN_STATUS_SUCCESS) {
  1115. cfg80211_send_rx_auth(sdata->dev, (u8 *) mgmt, len,
  1116. GFP_KERNEL);
  1117. ifmgd->state = IEEE80211_STA_MLME_DISABLED;
  1118. ieee80211_recalc_idle(sdata->local);
  1119. return;
  1120. }
  1121. switch (ifmgd->auth_alg) {
  1122. case WLAN_AUTH_OPEN:
  1123. case WLAN_AUTH_LEAP:
  1124. case WLAN_AUTH_FT:
  1125. ieee80211_auth_completed(sdata);
  1126. cfg80211_send_rx_auth(sdata->dev, (u8 *) mgmt, len,
  1127. GFP_KERNEL);
  1128. break;
  1129. case WLAN_AUTH_SHARED_KEY:
  1130. if (ifmgd->auth_transaction == 4) {
  1131. ieee80211_auth_completed(sdata);
  1132. cfg80211_send_rx_auth(sdata->dev, (u8 *) mgmt, len,
  1133. GFP_KERNEL);
  1134. } else
  1135. ieee80211_auth_challenge(sdata, mgmt, len);
  1136. break;
  1137. }
  1138. }
  1139. static void ieee80211_rx_mgmt_deauth(struct ieee80211_sub_if_data *sdata,
  1140. struct ieee80211_mgmt *mgmt,
  1141. size_t len)
  1142. {
  1143. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  1144. u16 reason_code;
  1145. if (len < 24 + 2)
  1146. return;
  1147. if (memcmp(ifmgd->bssid, mgmt->sa, ETH_ALEN))
  1148. return;
  1149. reason_code = le16_to_cpu(mgmt->u.deauth.reason_code);
  1150. if (ifmgd->flags & IEEE80211_STA_AUTHENTICATED)
  1151. printk(KERN_DEBUG "%s: deauthenticated (Reason: %u)\n",
  1152. sdata->dev->name, reason_code);
  1153. ieee80211_set_disassoc(sdata, true, false, 0);
  1154. ifmgd->flags &= ~IEEE80211_STA_AUTHENTICATED;
  1155. cfg80211_send_deauth(sdata->dev, (u8 *) mgmt, len, GFP_KERNEL);
  1156. }
  1157. static void ieee80211_rx_mgmt_disassoc(struct ieee80211_sub_if_data *sdata,
  1158. struct ieee80211_mgmt *mgmt,
  1159. size_t len)
  1160. {
  1161. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  1162. u16 reason_code;
  1163. if (len < 24 + 2)
  1164. return;
  1165. if (memcmp(ifmgd->bssid, mgmt->sa, ETH_ALEN))
  1166. return;
  1167. reason_code = le16_to_cpu(mgmt->u.disassoc.reason_code);
  1168. if (ifmgd->flags & IEEE80211_STA_ASSOCIATED)
  1169. printk(KERN_DEBUG "%s: disassociated (Reason: %u)\n",
  1170. sdata->dev->name, reason_code);
  1171. ieee80211_set_disassoc(sdata, false, false, reason_code);
  1172. cfg80211_send_disassoc(sdata->dev, (u8 *) mgmt, len, GFP_KERNEL);
  1173. }
  1174. static void ieee80211_rx_mgmt_assoc_resp(struct ieee80211_sub_if_data *sdata,
  1175. struct ieee80211_mgmt *mgmt,
  1176. size_t len,
  1177. int reassoc)
  1178. {
  1179. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  1180. struct ieee80211_local *local = sdata->local;
  1181. struct ieee80211_supported_band *sband;
  1182. struct sta_info *sta;
  1183. u32 rates, basic_rates;
  1184. u16 capab_info, status_code, aid;
  1185. struct ieee802_11_elems elems;
  1186. struct ieee80211_bss_conf *bss_conf = &sdata->vif.bss_conf;
  1187. u8 *pos;
  1188. u32 changed = 0;
  1189. int i, j;
  1190. bool have_higher_than_11mbit = false, newsta = false;
  1191. u16 ap_ht_cap_flags;
  1192. /* AssocResp and ReassocResp have identical structure, so process both
  1193. * of them in this function. */
  1194. if (ifmgd->state != IEEE80211_STA_MLME_ASSOCIATE)
  1195. return;
  1196. if (len < 24 + 6)
  1197. return;
  1198. if (memcmp(ifmgd->bssid, mgmt->sa, ETH_ALEN) != 0)
  1199. return;
  1200. capab_info = le16_to_cpu(mgmt->u.assoc_resp.capab_info);
  1201. status_code = le16_to_cpu(mgmt->u.assoc_resp.status_code);
  1202. aid = le16_to_cpu(mgmt->u.assoc_resp.aid);
  1203. printk(KERN_DEBUG "%s: RX %sssocResp from %pM (capab=0x%x "
  1204. "status=%d aid=%d)\n",
  1205. sdata->dev->name, reassoc ? "Rea" : "A", mgmt->sa,
  1206. capab_info, status_code, (u16)(aid & ~(BIT(15) | BIT(14))));
  1207. pos = mgmt->u.assoc_resp.variable;
  1208. ieee802_11_parse_elems(pos, len - (pos - (u8 *) mgmt), &elems);
  1209. if (status_code == WLAN_STATUS_ASSOC_REJECTED_TEMPORARILY &&
  1210. elems.timeout_int && elems.timeout_int_len == 5 &&
  1211. elems.timeout_int[0] == WLAN_TIMEOUT_ASSOC_COMEBACK) {
  1212. u32 tu, ms;
  1213. tu = get_unaligned_le32(elems.timeout_int + 1);
  1214. ms = tu * 1024 / 1000;
  1215. printk(KERN_DEBUG "%s: AP rejected association temporarily; "
  1216. "comeback duration %u TU (%u ms)\n",
  1217. sdata->dev->name, tu, ms);
  1218. if (ms > IEEE80211_ASSOC_TIMEOUT)
  1219. mod_timer(&ifmgd->timer,
  1220. jiffies + msecs_to_jiffies(ms));
  1221. return;
  1222. }
  1223. if (status_code != WLAN_STATUS_SUCCESS) {
  1224. printk(KERN_DEBUG "%s: AP denied association (code=%d)\n",
  1225. sdata->dev->name, status_code);
  1226. cfg80211_send_rx_assoc(sdata->dev, (u8 *) mgmt, len,
  1227. GFP_KERNEL);
  1228. /* Wait for SME to decide what to do next */
  1229. ifmgd->state = IEEE80211_STA_MLME_DISABLED;
  1230. ieee80211_recalc_idle(local);
  1231. return;
  1232. }
  1233. if ((aid & (BIT(15) | BIT(14))) != (BIT(15) | BIT(14)))
  1234. printk(KERN_DEBUG "%s: invalid aid value %d; bits 15:14 not "
  1235. "set\n", sdata->dev->name, aid);
  1236. aid &= ~(BIT(15) | BIT(14));
  1237. if (!elems.supp_rates) {
  1238. printk(KERN_DEBUG "%s: no SuppRates element in AssocResp\n",
  1239. sdata->dev->name);
  1240. return;
  1241. }
  1242. printk(KERN_DEBUG "%s: associated\n", sdata->dev->name);
  1243. ifmgd->aid = aid;
  1244. ifmgd->ap_capab = capab_info;
  1245. kfree(ifmgd->assocresp_ies);
  1246. ifmgd->assocresp_ies_len = len - (pos - (u8 *) mgmt);
  1247. ifmgd->assocresp_ies = kmalloc(ifmgd->assocresp_ies_len, GFP_KERNEL);
  1248. if (ifmgd->assocresp_ies)
  1249. memcpy(ifmgd->assocresp_ies, pos, ifmgd->assocresp_ies_len);
  1250. rcu_read_lock();
  1251. /* Add STA entry for the AP */
  1252. sta = sta_info_get(local, ifmgd->bssid);
  1253. if (!sta) {
  1254. newsta = true;
  1255. sta = sta_info_alloc(sdata, ifmgd->bssid, GFP_ATOMIC);
  1256. if (!sta) {
  1257. printk(KERN_DEBUG "%s: failed to alloc STA entry for"
  1258. " the AP\n", sdata->dev->name);
  1259. rcu_read_unlock();
  1260. return;
  1261. }
  1262. /* update new sta with its last rx activity */
  1263. sta->last_rx = jiffies;
  1264. }
  1265. /*
  1266. * FIXME: Do we really need to update the sta_info's information here?
  1267. * We already know about the AP (we found it in our list) so it
  1268. * should already be filled with the right info, no?
  1269. * As is stands, all this is racy because typically we assume
  1270. * the information that is filled in here (except flags) doesn't
  1271. * change while a STA structure is alive. As such, it should move
  1272. * to between the sta_info_alloc() and sta_info_insert() above.
  1273. */
  1274. set_sta_flags(sta, WLAN_STA_AUTH | WLAN_STA_ASSOC | WLAN_STA_ASSOC_AP);
  1275. if (!(ifmgd->flags & IEEE80211_STA_CONTROL_PORT))
  1276. set_sta_flags(sta, WLAN_STA_AUTHORIZED);
  1277. rates = 0;
  1278. basic_rates = 0;
  1279. sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
  1280. for (i = 0; i < elems.supp_rates_len; i++) {
  1281. int rate = (elems.supp_rates[i] & 0x7f) * 5;
  1282. bool is_basic = !!(elems.supp_rates[i] & 0x80);
  1283. if (rate > 110)
  1284. have_higher_than_11mbit = true;
  1285. for (j = 0; j < sband->n_bitrates; j++) {
  1286. if (sband->bitrates[j].bitrate == rate) {
  1287. rates |= BIT(j);
  1288. if (is_basic)
  1289. basic_rates |= BIT(j);
  1290. break;
  1291. }
  1292. }
  1293. }
  1294. for (i = 0; i < elems.ext_supp_rates_len; i++) {
  1295. int rate = (elems.ext_supp_rates[i] & 0x7f) * 5;
  1296. bool is_basic = !!(elems.ext_supp_rates[i] & 0x80);
  1297. if (rate > 110)
  1298. have_higher_than_11mbit = true;
  1299. for (j = 0; j < sband->n_bitrates; j++) {
  1300. if (sband->bitrates[j].bitrate == rate) {
  1301. rates |= BIT(j);
  1302. if (is_basic)
  1303. basic_rates |= BIT(j);
  1304. break;
  1305. }
  1306. }
  1307. }
  1308. sta->sta.supp_rates[local->hw.conf.channel->band] = rates;
  1309. sdata->vif.bss_conf.basic_rates = basic_rates;
  1310. /* cf. IEEE 802.11 9.2.12 */
  1311. if (local->hw.conf.channel->band == IEEE80211_BAND_2GHZ &&
  1312. have_higher_than_11mbit)
  1313. sdata->flags |= IEEE80211_SDATA_OPERATING_GMODE;
  1314. else
  1315. sdata->flags &= ~IEEE80211_SDATA_OPERATING_GMODE;
  1316. if (elems.ht_cap_elem && !(ifmgd->flags & IEEE80211_STA_DISABLE_11N))
  1317. ieee80211_ht_cap_ie_to_sta_ht_cap(sband,
  1318. elems.ht_cap_elem, &sta->sta.ht_cap);
  1319. ap_ht_cap_flags = sta->sta.ht_cap.cap;
  1320. rate_control_rate_init(sta);
  1321. if (ifmgd->flags & IEEE80211_STA_MFP_ENABLED)
  1322. set_sta_flags(sta, WLAN_STA_MFP);
  1323. if (elems.wmm_param)
  1324. set_sta_flags(sta, WLAN_STA_WME);
  1325. if (newsta) {
  1326. int err = sta_info_insert(sta);
  1327. if (err) {
  1328. printk(KERN_DEBUG "%s: failed to insert STA entry for"
  1329. " the AP (error %d)\n", sdata->dev->name, err);
  1330. rcu_read_unlock();
  1331. return;
  1332. }
  1333. }
  1334. rcu_read_unlock();
  1335. if (elems.wmm_param)
  1336. ieee80211_sta_wmm_params(local, ifmgd, elems.wmm_param,
  1337. elems.wmm_param_len);
  1338. else
  1339. ieee80211_set_wmm_default(sdata);
  1340. if (elems.ht_info_elem && elems.wmm_param &&
  1341. (ifmgd->flags & IEEE80211_STA_WMM_ENABLED) &&
  1342. !(ifmgd->flags & IEEE80211_STA_DISABLE_11N))
  1343. changed |= ieee80211_enable_ht(sdata, elems.ht_info_elem,
  1344. ap_ht_cap_flags);
  1345. /* set AID and assoc capability,
  1346. * ieee80211_set_associated() will tell the driver */
  1347. bss_conf->aid = aid;
  1348. bss_conf->assoc_capability = capab_info;
  1349. ieee80211_set_associated(sdata, changed);
  1350. /*
  1351. * initialise the time of last beacon to be the association time,
  1352. * otherwise beacon loss check will trigger immediately
  1353. */
  1354. ifmgd->last_beacon = jiffies;
  1355. ieee80211_associated(sdata);
  1356. cfg80211_send_rx_assoc(sdata->dev, (u8 *) mgmt, len, GFP_KERNEL);
  1357. }
  1358. static void ieee80211_rx_bss_info(struct ieee80211_sub_if_data *sdata,
  1359. struct ieee80211_mgmt *mgmt,
  1360. size_t len,
  1361. struct ieee80211_rx_status *rx_status,
  1362. struct ieee802_11_elems *elems,
  1363. bool beacon)
  1364. {
  1365. struct ieee80211_local *local = sdata->local;
  1366. int freq;
  1367. struct ieee80211_bss *bss;
  1368. struct ieee80211_channel *channel;
  1369. if (elems->ds_params && elems->ds_params_len == 1)
  1370. freq = ieee80211_channel_to_frequency(elems->ds_params[0]);
  1371. else
  1372. freq = rx_status->freq;
  1373. channel = ieee80211_get_channel(local->hw.wiphy, freq);
  1374. if (!channel || channel->flags & IEEE80211_CHAN_DISABLED)
  1375. return;
  1376. bss = ieee80211_bss_info_update(local, rx_status, mgmt, len, elems,
  1377. channel, beacon);
  1378. if (!bss)
  1379. return;
  1380. if (elems->ch_switch_elem && (elems->ch_switch_elem_len == 3) &&
  1381. (memcmp(mgmt->bssid, sdata->u.mgd.bssid, ETH_ALEN) == 0)) {
  1382. struct ieee80211_channel_sw_ie *sw_elem =
  1383. (struct ieee80211_channel_sw_ie *)elems->ch_switch_elem;
  1384. ieee80211_sta_process_chanswitch(sdata, sw_elem, bss);
  1385. }
  1386. ieee80211_rx_bss_put(local, bss);
  1387. }
  1388. static void ieee80211_rx_mgmt_probe_resp(struct ieee80211_sub_if_data *sdata,
  1389. struct ieee80211_mgmt *mgmt,
  1390. size_t len,
  1391. struct ieee80211_rx_status *rx_status)
  1392. {
  1393. struct ieee80211_if_managed *ifmgd;
  1394. size_t baselen;
  1395. struct ieee802_11_elems elems;
  1396. ifmgd = &sdata->u.mgd;
  1397. if (memcmp(mgmt->da, sdata->dev->dev_addr, ETH_ALEN))
  1398. return; /* ignore ProbeResp to foreign address */
  1399. baselen = (u8 *) mgmt->u.probe_resp.variable - (u8 *) mgmt;
  1400. if (baselen > len)
  1401. return;
  1402. ieee802_11_parse_elems(mgmt->u.probe_resp.variable, len - baselen,
  1403. &elems);
  1404. ieee80211_rx_bss_info(sdata, mgmt, len, rx_status, &elems, false);
  1405. /* direct probe may be part of the association flow */
  1406. if (ifmgd->state == IEEE80211_STA_MLME_DIRECT_PROBE) {
  1407. printk(KERN_DEBUG "%s direct probe responded\n",
  1408. sdata->dev->name);
  1409. ieee80211_authenticate(sdata);
  1410. }
  1411. if (ifmgd->flags & IEEE80211_STA_PROBEREQ_POLL) {
  1412. ifmgd->flags &= ~IEEE80211_STA_PROBEREQ_POLL;
  1413. mutex_lock(&sdata->local->iflist_mtx);
  1414. ieee80211_recalc_ps(sdata->local, -1);
  1415. mutex_unlock(&sdata->local->iflist_mtx);
  1416. }
  1417. }
  1418. /*
  1419. * This is the canonical list of information elements we care about,
  1420. * the filter code also gives us all changes to the Microsoft OUI
  1421. * (00:50:F2) vendor IE which is used for WMM which we need to track.
  1422. *
  1423. * We implement beacon filtering in software since that means we can
  1424. * avoid processing the frame here and in cfg80211, and userspace
  1425. * will not be able to tell whether the hardware supports it or not.
  1426. *
  1427. * XXX: This list needs to be dynamic -- userspace needs to be able to
  1428. * add items it requires. It also needs to be able to tell us to
  1429. * look out for other vendor IEs.
  1430. */
  1431. static const u64 care_about_ies =
  1432. (1ULL << WLAN_EID_COUNTRY) |
  1433. (1ULL << WLAN_EID_ERP_INFO) |
  1434. (1ULL << WLAN_EID_CHANNEL_SWITCH) |
  1435. (1ULL << WLAN_EID_PWR_CONSTRAINT) |
  1436. (1ULL << WLAN_EID_HT_CAPABILITY) |
  1437. (1ULL << WLAN_EID_HT_INFORMATION);
  1438. static void ieee80211_rx_mgmt_beacon(struct ieee80211_sub_if_data *sdata,
  1439. struct ieee80211_mgmt *mgmt,
  1440. size_t len,
  1441. struct ieee80211_rx_status *rx_status)
  1442. {
  1443. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  1444. size_t baselen;
  1445. struct ieee802_11_elems elems;
  1446. struct ieee80211_local *local = sdata->local;
  1447. u32 changed = 0;
  1448. bool erp_valid, directed_tim = false;
  1449. u8 erp_value = 0;
  1450. u32 ncrc;
  1451. /* Process beacon from the current BSS */
  1452. baselen = (u8 *) mgmt->u.beacon.variable - (u8 *) mgmt;
  1453. if (baselen > len)
  1454. return;
  1455. if (rx_status->freq != local->hw.conf.channel->center_freq)
  1456. return;
  1457. if (!(ifmgd->flags & IEEE80211_STA_ASSOCIATED) ||
  1458. memcmp(ifmgd->bssid, mgmt->bssid, ETH_ALEN) != 0)
  1459. return;
  1460. if (ifmgd->flags & IEEE80211_STA_PROBEREQ_POLL) {
  1461. #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
  1462. if (net_ratelimit()) {
  1463. printk(KERN_DEBUG "%s: cancelling probereq poll due "
  1464. "to a received beacon\n", sdata->dev->name);
  1465. }
  1466. #endif
  1467. ifmgd->flags &= ~IEEE80211_STA_PROBEREQ_POLL;
  1468. mutex_lock(&local->iflist_mtx);
  1469. ieee80211_recalc_ps(local, -1);
  1470. mutex_unlock(&local->iflist_mtx);
  1471. }
  1472. ncrc = crc32_be(0, (void *)&mgmt->u.beacon.beacon_int, 4);
  1473. ncrc = ieee802_11_parse_elems_crc(mgmt->u.beacon.variable,
  1474. len - baselen, &elems,
  1475. care_about_ies, ncrc);
  1476. if (local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK)
  1477. directed_tim = ieee80211_check_tim(elems.tim, elems.tim_len,
  1478. ifmgd->aid);
  1479. if (ncrc != ifmgd->beacon_crc) {
  1480. ieee80211_rx_bss_info(sdata, mgmt, len, rx_status, &elems,
  1481. true);
  1482. ieee80211_sta_wmm_params(local, ifmgd, elems.wmm_param,
  1483. elems.wmm_param_len);
  1484. }
  1485. if (local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK) {
  1486. if (directed_tim) {
  1487. if (local->hw.conf.dynamic_ps_timeout > 0) {
  1488. local->hw.conf.flags &= ~IEEE80211_CONF_PS;
  1489. ieee80211_hw_config(local,
  1490. IEEE80211_CONF_CHANGE_PS);
  1491. ieee80211_send_nullfunc(local, sdata, 0);
  1492. } else {
  1493. local->pspolling = true;
  1494. /*
  1495. * Here is assumed that the driver will be
  1496. * able to send ps-poll frame and receive a
  1497. * response even though power save mode is
  1498. * enabled, but some drivers might require
  1499. * to disable power save here. This needs
  1500. * to be investigated.
  1501. */
  1502. ieee80211_send_pspoll(local, sdata);
  1503. }
  1504. }
  1505. }
  1506. if (ncrc == ifmgd->beacon_crc)
  1507. return;
  1508. ifmgd->beacon_crc = ncrc;
  1509. if (elems.erp_info && elems.erp_info_len >= 1) {
  1510. erp_valid = true;
  1511. erp_value = elems.erp_info[0];
  1512. } else {
  1513. erp_valid = false;
  1514. }
  1515. changed |= ieee80211_handle_bss_capability(sdata,
  1516. le16_to_cpu(mgmt->u.beacon.capab_info),
  1517. erp_valid, erp_value);
  1518. if (elems.ht_cap_elem && elems.ht_info_elem && elems.wmm_param &&
  1519. !(ifmgd->flags & IEEE80211_STA_DISABLE_11N)) {
  1520. struct sta_info *sta;
  1521. struct ieee80211_supported_band *sband;
  1522. u16 ap_ht_cap_flags;
  1523. rcu_read_lock();
  1524. sta = sta_info_get(local, ifmgd->bssid);
  1525. if (!sta) {
  1526. rcu_read_unlock();
  1527. return;
  1528. }
  1529. sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
  1530. ieee80211_ht_cap_ie_to_sta_ht_cap(sband,
  1531. elems.ht_cap_elem, &sta->sta.ht_cap);
  1532. ap_ht_cap_flags = sta->sta.ht_cap.cap;
  1533. rcu_read_unlock();
  1534. changed |= ieee80211_enable_ht(sdata, elems.ht_info_elem,
  1535. ap_ht_cap_flags);
  1536. }
  1537. if (elems.country_elem) {
  1538. /* Note we are only reviewing this on beacons
  1539. * for the BSSID we are associated to */
  1540. regulatory_hint_11d(local->hw.wiphy,
  1541. elems.country_elem, elems.country_elem_len);
  1542. /* TODO: IBSS also needs this */
  1543. if (elems.pwr_constr_elem)
  1544. ieee80211_handle_pwr_constr(sdata,
  1545. le16_to_cpu(mgmt->u.probe_resp.capab_info),
  1546. elems.pwr_constr_elem,
  1547. elems.pwr_constr_elem_len);
  1548. }
  1549. ieee80211_bss_info_change_notify(sdata, changed);
  1550. }
  1551. ieee80211_rx_result ieee80211_sta_rx_mgmt(struct ieee80211_sub_if_data *sdata,
  1552. struct sk_buff *skb)
  1553. {
  1554. struct ieee80211_local *local = sdata->local;
  1555. struct ieee80211_mgmt *mgmt;
  1556. u16 fc;
  1557. if (skb->len < 24)
  1558. return RX_DROP_MONITOR;
  1559. mgmt = (struct ieee80211_mgmt *) skb->data;
  1560. fc = le16_to_cpu(mgmt->frame_control);
  1561. switch (fc & IEEE80211_FCTL_STYPE) {
  1562. case IEEE80211_STYPE_PROBE_REQ:
  1563. case IEEE80211_STYPE_PROBE_RESP:
  1564. case IEEE80211_STYPE_BEACON:
  1565. case IEEE80211_STYPE_AUTH:
  1566. case IEEE80211_STYPE_ASSOC_RESP:
  1567. case IEEE80211_STYPE_REASSOC_RESP:
  1568. case IEEE80211_STYPE_DEAUTH:
  1569. case IEEE80211_STYPE_DISASSOC:
  1570. skb_queue_tail(&sdata->u.mgd.skb_queue, skb);
  1571. queue_work(local->hw.workqueue, &sdata->u.mgd.work);
  1572. return RX_QUEUED;
  1573. }
  1574. return RX_DROP_MONITOR;
  1575. }
  1576. static void ieee80211_sta_rx_queued_mgmt(struct ieee80211_sub_if_data *sdata,
  1577. struct sk_buff *skb)
  1578. {
  1579. struct ieee80211_rx_status *rx_status;
  1580. struct ieee80211_mgmt *mgmt;
  1581. u16 fc;
  1582. rx_status = (struct ieee80211_rx_status *) skb->cb;
  1583. mgmt = (struct ieee80211_mgmt *) skb->data;
  1584. fc = le16_to_cpu(mgmt->frame_control);
  1585. switch (fc & IEEE80211_FCTL_STYPE) {
  1586. case IEEE80211_STYPE_PROBE_RESP:
  1587. ieee80211_rx_mgmt_probe_resp(sdata, mgmt, skb->len,
  1588. rx_status);
  1589. break;
  1590. case IEEE80211_STYPE_BEACON:
  1591. ieee80211_rx_mgmt_beacon(sdata, mgmt, skb->len,
  1592. rx_status);
  1593. break;
  1594. case IEEE80211_STYPE_AUTH:
  1595. ieee80211_rx_mgmt_auth(sdata, mgmt, skb->len);
  1596. break;
  1597. case IEEE80211_STYPE_ASSOC_RESP:
  1598. ieee80211_rx_mgmt_assoc_resp(sdata, mgmt, skb->len, 0);
  1599. break;
  1600. case IEEE80211_STYPE_REASSOC_RESP:
  1601. ieee80211_rx_mgmt_assoc_resp(sdata, mgmt, skb->len, 1);
  1602. break;
  1603. case IEEE80211_STYPE_DEAUTH:
  1604. ieee80211_rx_mgmt_deauth(sdata, mgmt, skb->len);
  1605. break;
  1606. case IEEE80211_STYPE_DISASSOC:
  1607. ieee80211_rx_mgmt_disassoc(sdata, mgmt, skb->len);
  1608. break;
  1609. }
  1610. kfree_skb(skb);
  1611. }
  1612. static void ieee80211_sta_timer(unsigned long data)
  1613. {
  1614. struct ieee80211_sub_if_data *sdata =
  1615. (struct ieee80211_sub_if_data *) data;
  1616. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  1617. struct ieee80211_local *local = sdata->local;
  1618. if (local->quiescing) {
  1619. set_bit(TMR_RUNNING_TIMER, &ifmgd->timers_running);
  1620. return;
  1621. }
  1622. set_bit(IEEE80211_STA_REQ_RUN, &ifmgd->request);
  1623. queue_work(local->hw.workqueue, &ifmgd->work);
  1624. }
  1625. static void ieee80211_sta_reset_auth(struct ieee80211_sub_if_data *sdata)
  1626. {
  1627. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  1628. struct ieee80211_local *local = sdata->local;
  1629. /* Reset own TSF to allow time synchronization work. */
  1630. drv_reset_tsf(local);
  1631. ifmgd->wmm_last_param_set = -1; /* allow any WMM update */
  1632. ifmgd->auth_transaction = -1;
  1633. ifmgd->flags &= ~IEEE80211_STA_ASSOCIATED;
  1634. ifmgd->assoc_scan_tries = 0;
  1635. ifmgd->direct_probe_tries = 0;
  1636. ifmgd->auth_tries = 0;
  1637. ifmgd->assoc_tries = 0;
  1638. netif_tx_stop_all_queues(sdata->dev);
  1639. netif_carrier_off(sdata->dev);
  1640. }
  1641. static int ieee80211_sta_config_auth(struct ieee80211_sub_if_data *sdata)
  1642. {
  1643. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  1644. struct ieee80211_local *local = sdata->local;
  1645. struct ieee80211_bss *bss;
  1646. u8 *bssid = ifmgd->bssid, *ssid = ifmgd->ssid;
  1647. u8 ssid_len = ifmgd->ssid_len;
  1648. u16 capa_mask = WLAN_CAPABILITY_ESS;
  1649. u16 capa_val = WLAN_CAPABILITY_ESS;
  1650. struct ieee80211_channel *chan = local->oper_channel;
  1651. bss = (void *)cfg80211_get_bss(local->hw.wiphy, chan,
  1652. bssid, ssid, ssid_len,
  1653. capa_mask, capa_val);
  1654. if (bss) {
  1655. local->oper_channel = bss->cbss.channel;
  1656. local->oper_channel_type = NL80211_CHAN_NO_HT;
  1657. ieee80211_hw_config(local, 0);
  1658. ieee80211_sta_def_wmm_params(sdata, bss->supp_rates_len,
  1659. bss->supp_rates);
  1660. if (sdata->u.mgd.mfp == IEEE80211_MFP_REQUIRED)
  1661. sdata->u.mgd.flags |= IEEE80211_STA_MFP_ENABLED;
  1662. else
  1663. sdata->u.mgd.flags &= ~IEEE80211_STA_MFP_ENABLED;
  1664. /* Send out direct probe if no probe resp was received or
  1665. * the one we have is outdated
  1666. */
  1667. if (!bss->last_probe_resp ||
  1668. time_after(jiffies, bss->last_probe_resp
  1669. + IEEE80211_SCAN_RESULT_EXPIRE))
  1670. ifmgd->state = IEEE80211_STA_MLME_DIRECT_PROBE;
  1671. else
  1672. ifmgd->state = IEEE80211_STA_MLME_AUTHENTICATE;
  1673. ieee80211_rx_bss_put(local, bss);
  1674. ieee80211_sta_reset_auth(sdata);
  1675. return 0;
  1676. } else {
  1677. if (ifmgd->assoc_scan_tries < IEEE80211_ASSOC_SCANS_MAX_TRIES) {
  1678. ifmgd->assoc_scan_tries++;
  1679. ieee80211_request_internal_scan(sdata, ifmgd->ssid,
  1680. ssid_len);
  1681. ifmgd->state = IEEE80211_STA_MLME_AUTHENTICATE;
  1682. set_bit(IEEE80211_STA_REQ_AUTH, &ifmgd->request);
  1683. } else {
  1684. ifmgd->assoc_scan_tries = 0;
  1685. ifmgd->state = IEEE80211_STA_MLME_DISABLED;
  1686. ieee80211_recalc_idle(local);
  1687. }
  1688. }
  1689. return -1;
  1690. }
  1691. static void ieee80211_sta_work(struct work_struct *work)
  1692. {
  1693. struct ieee80211_sub_if_data *sdata =
  1694. container_of(work, struct ieee80211_sub_if_data, u.mgd.work);
  1695. struct ieee80211_local *local = sdata->local;
  1696. struct ieee80211_if_managed *ifmgd;
  1697. struct sk_buff *skb;
  1698. if (!netif_running(sdata->dev))
  1699. return;
  1700. if (local->sw_scanning || local->hw_scanning)
  1701. return;
  1702. if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_STATION))
  1703. return;
  1704. /*
  1705. * Nothing should have been stuffed into the workqueue during
  1706. * the suspend->resume cycle. If this WARN is seen then there
  1707. * is a bug with either the driver suspend or something in
  1708. * mac80211 stuffing into the workqueue which we haven't yet
  1709. * cleared during mac80211's suspend cycle.
  1710. */
  1711. if (WARN_ON(local->suspended))
  1712. return;
  1713. ifmgd = &sdata->u.mgd;
  1714. while ((skb = skb_dequeue(&ifmgd->skb_queue)))
  1715. ieee80211_sta_rx_queued_mgmt(sdata, skb);
  1716. if (ifmgd->state != IEEE80211_STA_MLME_DIRECT_PROBE &&
  1717. ifmgd->state != IEEE80211_STA_MLME_AUTHENTICATE &&
  1718. ifmgd->state != IEEE80211_STA_MLME_ASSOCIATE &&
  1719. test_and_clear_bit(IEEE80211_STA_REQ_SCAN, &ifmgd->request)) {
  1720. queue_delayed_work(local->hw.workqueue, &local->scan_work,
  1721. round_jiffies_relative(0));
  1722. return;
  1723. }
  1724. if (test_and_clear_bit(IEEE80211_STA_REQ_AUTH, &ifmgd->request)) {
  1725. if (ieee80211_sta_config_auth(sdata))
  1726. return;
  1727. clear_bit(IEEE80211_STA_REQ_RUN, &ifmgd->request);
  1728. } else if (!test_and_clear_bit(IEEE80211_STA_REQ_RUN, &ifmgd->request))
  1729. return;
  1730. ieee80211_recalc_idle(local);
  1731. switch (ifmgd->state) {
  1732. case IEEE80211_STA_MLME_DISABLED:
  1733. break;
  1734. case IEEE80211_STA_MLME_DIRECT_PROBE:
  1735. ieee80211_direct_probe(sdata);
  1736. break;
  1737. case IEEE80211_STA_MLME_AUTHENTICATE:
  1738. ieee80211_authenticate(sdata);
  1739. break;
  1740. case IEEE80211_STA_MLME_ASSOCIATE:
  1741. ieee80211_associate(sdata);
  1742. break;
  1743. case IEEE80211_STA_MLME_ASSOCIATED:
  1744. ieee80211_associated(sdata);
  1745. break;
  1746. default:
  1747. WARN_ON(1);
  1748. break;
  1749. }
  1750. }
  1751. static void ieee80211_restart_sta_timer(struct ieee80211_sub_if_data *sdata)
  1752. {
  1753. if (sdata->vif.type == NL80211_IFTYPE_STATION) {
  1754. /*
  1755. * Need to update last_beacon to avoid beacon loss
  1756. * test to trigger.
  1757. */
  1758. sdata->u.mgd.last_beacon = jiffies;
  1759. queue_work(sdata->local->hw.workqueue,
  1760. &sdata->u.mgd.work);
  1761. }
  1762. }
  1763. #ifdef CONFIG_PM
  1764. void ieee80211_sta_quiesce(struct ieee80211_sub_if_data *sdata)
  1765. {
  1766. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  1767. /*
  1768. * we need to use atomic bitops for the running bits
  1769. * only because both timers might fire at the same
  1770. * time -- the code here is properly synchronised.
  1771. */
  1772. cancel_work_sync(&ifmgd->work);
  1773. cancel_work_sync(&ifmgd->beacon_loss_work);
  1774. if (del_timer_sync(&ifmgd->timer))
  1775. set_bit(TMR_RUNNING_TIMER, &ifmgd->timers_running);
  1776. cancel_work_sync(&ifmgd->chswitch_work);
  1777. if (del_timer_sync(&ifmgd->chswitch_timer))
  1778. set_bit(TMR_RUNNING_CHANSW, &ifmgd->timers_running);
  1779. }
  1780. void ieee80211_sta_restart(struct ieee80211_sub_if_data *sdata)
  1781. {
  1782. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  1783. if (test_and_clear_bit(TMR_RUNNING_TIMER, &ifmgd->timers_running))
  1784. add_timer(&ifmgd->timer);
  1785. if (test_and_clear_bit(TMR_RUNNING_CHANSW, &ifmgd->timers_running))
  1786. add_timer(&ifmgd->chswitch_timer);
  1787. }
  1788. #endif
  1789. /* interface setup */
  1790. void ieee80211_sta_setup_sdata(struct ieee80211_sub_if_data *sdata)
  1791. {
  1792. struct ieee80211_if_managed *ifmgd;
  1793. u32 hw_flags;
  1794. ifmgd = &sdata->u.mgd;
  1795. INIT_WORK(&ifmgd->work, ieee80211_sta_work);
  1796. INIT_WORK(&ifmgd->chswitch_work, ieee80211_chswitch_work);
  1797. INIT_WORK(&ifmgd->beacon_loss_work, ieee80211_beacon_loss_work);
  1798. setup_timer(&ifmgd->timer, ieee80211_sta_timer,
  1799. (unsigned long) sdata);
  1800. setup_timer(&ifmgd->chswitch_timer, ieee80211_chswitch_timer,
  1801. (unsigned long) sdata);
  1802. skb_queue_head_init(&ifmgd->skb_queue);
  1803. ifmgd->capab = WLAN_CAPABILITY_ESS;
  1804. ifmgd->flags = 0;
  1805. if (sdata->local->hw.queues >= 4)
  1806. ifmgd->flags |= IEEE80211_STA_WMM_ENABLED;
  1807. hw_flags = sdata->local->hw.flags;
  1808. }
  1809. /* configuration hooks */
  1810. void ieee80211_sta_req_auth(struct ieee80211_sub_if_data *sdata)
  1811. {
  1812. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  1813. struct ieee80211_local *local = sdata->local;
  1814. if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_STATION))
  1815. return;
  1816. if (WARN_ON(ifmgd->state == IEEE80211_STA_MLME_ASSOCIATED))
  1817. ieee80211_set_disassoc(sdata, true, true,
  1818. WLAN_REASON_DEAUTH_LEAVING);
  1819. if (WARN_ON(ifmgd->ssid_len == 0)) {
  1820. /*
  1821. * Only allow association to be started if a valid SSID
  1822. * is configured.
  1823. */
  1824. return;
  1825. }
  1826. set_bit(IEEE80211_STA_REQ_RUN, &ifmgd->request);
  1827. queue_work(local->hw.workqueue, &ifmgd->work);
  1828. }
  1829. int ieee80211_sta_set_extra_ie(struct ieee80211_sub_if_data *sdata,
  1830. const char *ie, size_t len)
  1831. {
  1832. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  1833. if (len == 0 && ifmgd->extra_ie_len == 0)
  1834. return -EALREADY;
  1835. if (len == ifmgd->extra_ie_len && ifmgd->extra_ie &&
  1836. memcmp(ifmgd->extra_ie, ie, len) == 0)
  1837. return -EALREADY;
  1838. kfree(ifmgd->extra_ie);
  1839. if (len == 0) {
  1840. ifmgd->extra_ie = NULL;
  1841. ifmgd->extra_ie_len = 0;
  1842. return 0;
  1843. }
  1844. ifmgd->extra_ie = kmalloc(len, GFP_KERNEL);
  1845. if (!ifmgd->extra_ie) {
  1846. ifmgd->extra_ie_len = 0;
  1847. return -ENOMEM;
  1848. }
  1849. memcpy(ifmgd->extra_ie, ie, len);
  1850. ifmgd->extra_ie_len = len;
  1851. return 0;
  1852. }
  1853. int ieee80211_sta_deauthenticate(struct ieee80211_sub_if_data *sdata, u16 reason)
  1854. {
  1855. printk(KERN_DEBUG "%s: deauthenticating by local choice (reason=%d)\n",
  1856. sdata->dev->name, reason);
  1857. ieee80211_set_disassoc(sdata, true, true, reason);
  1858. return 0;
  1859. }
  1860. int ieee80211_sta_disassociate(struct ieee80211_sub_if_data *sdata, u16 reason)
  1861. {
  1862. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  1863. printk(KERN_DEBUG "%s: disassociating by local choice (reason=%d)\n",
  1864. sdata->dev->name, reason);
  1865. if (!(ifmgd->flags & IEEE80211_STA_ASSOCIATED))
  1866. return -ENOLINK;
  1867. ieee80211_set_disassoc(sdata, false, true, reason);
  1868. return 0;
  1869. }
  1870. /* scan finished notification */
  1871. void ieee80211_mlme_notify_scan_completed(struct ieee80211_local *local)
  1872. {
  1873. struct ieee80211_sub_if_data *sdata = local->scan_sdata;
  1874. /* Restart STA timers */
  1875. rcu_read_lock();
  1876. list_for_each_entry_rcu(sdata, &local->interfaces, list)
  1877. ieee80211_restart_sta_timer(sdata);
  1878. rcu_read_unlock();
  1879. }
  1880. int ieee80211_max_network_latency(struct notifier_block *nb,
  1881. unsigned long data, void *dummy)
  1882. {
  1883. s32 latency_usec = (s32) data;
  1884. struct ieee80211_local *local =
  1885. container_of(nb, struct ieee80211_local,
  1886. network_latency_notifier);
  1887. mutex_lock(&local->iflist_mtx);
  1888. ieee80211_recalc_ps(local, latency_usec);
  1889. mutex_unlock(&local->iflist_mtx);
  1890. return 0;
  1891. }