mlme.c 66 KB

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