mlme.c 63 KB

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