mlme.c 66 KB

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