mlme.c 65 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362
  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_IDLE_TIME (60 * HZ)
  34. #define IEEE80211_RETRY_AUTH_INTERVAL (1 * HZ)
  35. /* utils */
  36. static int ecw2cw(int ecw)
  37. {
  38. return (1 << ecw) - 1;
  39. }
  40. static u8 *ieee80211_bss_get_ie(struct ieee80211_bss *bss, u8 ie)
  41. {
  42. u8 *end, *pos;
  43. pos = bss->cbss.information_elements;
  44. if (pos == NULL)
  45. return NULL;
  46. end = pos + bss->cbss.len_information_elements;
  47. while (pos + 1 < end) {
  48. if (pos + 2 + pos[1] > end)
  49. break;
  50. if (pos[0] == ie)
  51. return pos;
  52. pos += 2 + pos[1];
  53. }
  54. return NULL;
  55. }
  56. static int ieee80211_compatible_rates(struct ieee80211_bss *bss,
  57. struct ieee80211_supported_band *sband,
  58. u32 *rates)
  59. {
  60. int i, j, count;
  61. *rates = 0;
  62. count = 0;
  63. for (i = 0; i < bss->supp_rates_len; i++) {
  64. int rate = (bss->supp_rates[i] & 0x7F) * 5;
  65. for (j = 0; j < sband->n_bitrates; j++)
  66. if (sband->bitrates[j].bitrate == rate) {
  67. *rates |= BIT(j);
  68. count++;
  69. break;
  70. }
  71. }
  72. return count;
  73. }
  74. /*
  75. * ieee80211_enable_ht should be called only after the operating band
  76. * has been determined as ht configuration depends on the hw's
  77. * HT abilities for a specific band.
  78. */
  79. static u32 ieee80211_enable_ht(struct ieee80211_sub_if_data *sdata,
  80. struct ieee80211_ht_info *hti,
  81. u16 ap_ht_cap_flags)
  82. {
  83. struct ieee80211_local *local = sdata->local;
  84. struct ieee80211_supported_band *sband;
  85. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  86. struct ieee80211_bss_ht_conf ht;
  87. struct sta_info *sta;
  88. u32 changed = 0;
  89. bool enable_ht = true, ht_changed;
  90. enum nl80211_channel_type channel_type = NL80211_CHAN_NO_HT;
  91. sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
  92. memset(&ht, 0, sizeof(ht));
  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. channel_type = NL80211_CHAN_HT40PLUS;
  108. break;
  109. case IEEE80211_HT_PARAM_CHA_SEC_BELOW:
  110. channel_type = NL80211_CHAN_HT40MINUS;
  111. break;
  112. }
  113. }
  114. }
  115. ht_changed = conf_is_ht(&local->hw.conf) != enable_ht ||
  116. channel_type != local->hw.conf.channel_type;
  117. local->oper_channel_type = channel_type;
  118. if (ht_changed) {
  119. /* channel_type change automatically detected */
  120. ieee80211_hw_config(local, 0);
  121. rcu_read_lock();
  122. sta = sta_info_get(local, ifmgd->bssid);
  123. if (sta)
  124. rate_control_rate_update(local, sband, sta,
  125. IEEE80211_RC_HT_CHANGED);
  126. rcu_read_unlock();
  127. }
  128. /* disable HT */
  129. if (!enable_ht)
  130. return 0;
  131. ht.operation_mode = le16_to_cpu(hti->operation_mode);
  132. /* if bss configuration changed store the new one */
  133. if (memcmp(&sdata->vif.bss_conf.ht, &ht, sizeof(ht))) {
  134. changed |= BSS_CHANGED_HT;
  135. sdata->vif.bss_conf.ht = ht;
  136. }
  137. return changed;
  138. }
  139. /* frame sending functions */
  140. static void ieee80211_send_assoc(struct ieee80211_sub_if_data *sdata)
  141. {
  142. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  143. struct ieee80211_local *local = sdata->local;
  144. struct sk_buff *skb;
  145. struct ieee80211_mgmt *mgmt;
  146. u8 *pos, *ies, *ht_ie;
  147. int i, len, count, rates_len, supp_rates_len;
  148. u16 capab;
  149. struct ieee80211_bss *bss;
  150. int wmm = 0;
  151. struct ieee80211_supported_band *sband;
  152. u32 rates = 0;
  153. skb = dev_alloc_skb(local->hw.extra_tx_headroom +
  154. sizeof(*mgmt) + 200 + ifmgd->extra_ie_len +
  155. ifmgd->ssid_len);
  156. if (!skb) {
  157. printk(KERN_DEBUG "%s: failed to allocate buffer for assoc "
  158. "frame\n", sdata->dev->name);
  159. return;
  160. }
  161. skb_reserve(skb, local->hw.extra_tx_headroom);
  162. sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
  163. capab = ifmgd->capab;
  164. if (local->hw.conf.channel->band == IEEE80211_BAND_2GHZ) {
  165. if (!(local->hw.flags & IEEE80211_HW_2GHZ_SHORT_SLOT_INCAPABLE))
  166. capab |= WLAN_CAPABILITY_SHORT_SLOT_TIME;
  167. if (!(local->hw.flags & IEEE80211_HW_2GHZ_SHORT_PREAMBLE_INCAPABLE))
  168. capab |= WLAN_CAPABILITY_SHORT_PREAMBLE;
  169. }
  170. bss = ieee80211_rx_bss_get(local, ifmgd->bssid,
  171. local->hw.conf.channel->center_freq,
  172. ifmgd->ssid, ifmgd->ssid_len);
  173. if (bss) {
  174. if (bss->cbss.capability & WLAN_CAPABILITY_PRIVACY)
  175. capab |= WLAN_CAPABILITY_PRIVACY;
  176. if (bss->wmm_used)
  177. wmm = 1;
  178. /* get all rates supported by the device and the AP as
  179. * some APs don't like getting a superset of their rates
  180. * in the association request (e.g. D-Link DAP 1353 in
  181. * b-only mode) */
  182. rates_len = ieee80211_compatible_rates(bss, sband, &rates);
  183. if ((bss->cbss.capability & WLAN_CAPABILITY_SPECTRUM_MGMT) &&
  184. (local->hw.flags & IEEE80211_HW_SPECTRUM_MGMT))
  185. capab |= WLAN_CAPABILITY_SPECTRUM_MGMT;
  186. ieee80211_rx_bss_put(local, bss);
  187. } else {
  188. rates = ~0;
  189. rates_len = sband->n_bitrates;
  190. }
  191. mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
  192. memset(mgmt, 0, 24);
  193. memcpy(mgmt->da, ifmgd->bssid, ETH_ALEN);
  194. memcpy(mgmt->sa, sdata->dev->dev_addr, ETH_ALEN);
  195. memcpy(mgmt->bssid, ifmgd->bssid, ETH_ALEN);
  196. if (ifmgd->flags & IEEE80211_STA_PREV_BSSID_SET) {
  197. skb_put(skb, 10);
  198. mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
  199. IEEE80211_STYPE_REASSOC_REQ);
  200. mgmt->u.reassoc_req.capab_info = cpu_to_le16(capab);
  201. mgmt->u.reassoc_req.listen_interval =
  202. cpu_to_le16(local->hw.conf.listen_interval);
  203. memcpy(mgmt->u.reassoc_req.current_ap, ifmgd->prev_bssid,
  204. ETH_ALEN);
  205. } else {
  206. skb_put(skb, 4);
  207. mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
  208. IEEE80211_STYPE_ASSOC_REQ);
  209. mgmt->u.assoc_req.capab_info = cpu_to_le16(capab);
  210. mgmt->u.assoc_req.listen_interval =
  211. cpu_to_le16(local->hw.conf.listen_interval);
  212. }
  213. /* SSID */
  214. ies = pos = skb_put(skb, 2 + ifmgd->ssid_len);
  215. *pos++ = WLAN_EID_SSID;
  216. *pos++ = ifmgd->ssid_len;
  217. memcpy(pos, ifmgd->ssid, ifmgd->ssid_len);
  218. /* add all rates which were marked to be used above */
  219. supp_rates_len = rates_len;
  220. if (supp_rates_len > 8)
  221. supp_rates_len = 8;
  222. len = sband->n_bitrates;
  223. pos = skb_put(skb, supp_rates_len + 2);
  224. *pos++ = WLAN_EID_SUPP_RATES;
  225. *pos++ = supp_rates_len;
  226. count = 0;
  227. for (i = 0; i < sband->n_bitrates; i++) {
  228. if (BIT(i) & rates) {
  229. int rate = sband->bitrates[i].bitrate;
  230. *pos++ = (u8) (rate / 5);
  231. if (++count == 8)
  232. break;
  233. }
  234. }
  235. if (rates_len > count) {
  236. pos = skb_put(skb, rates_len - count + 2);
  237. *pos++ = WLAN_EID_EXT_SUPP_RATES;
  238. *pos++ = rates_len - count;
  239. for (i++; i < sband->n_bitrates; i++) {
  240. if (BIT(i) & rates) {
  241. int rate = sband->bitrates[i].bitrate;
  242. *pos++ = (u8) (rate / 5);
  243. }
  244. }
  245. }
  246. if (capab & WLAN_CAPABILITY_SPECTRUM_MGMT) {
  247. /* 1. power capabilities */
  248. pos = skb_put(skb, 4);
  249. *pos++ = WLAN_EID_PWR_CAPABILITY;
  250. *pos++ = 2;
  251. *pos++ = 0; /* min tx power */
  252. *pos++ = local->hw.conf.channel->max_power; /* max tx power */
  253. /* 2. supported channels */
  254. /* TODO: get this in reg domain format */
  255. pos = skb_put(skb, 2 * sband->n_channels + 2);
  256. *pos++ = WLAN_EID_SUPPORTED_CHANNELS;
  257. *pos++ = 2 * sband->n_channels;
  258. for (i = 0; i < sband->n_channels; i++) {
  259. *pos++ = ieee80211_frequency_to_channel(
  260. sband->channels[i].center_freq);
  261. *pos++ = 1; /* one channel in the subband*/
  262. }
  263. }
  264. if (ifmgd->extra_ie) {
  265. pos = skb_put(skb, ifmgd->extra_ie_len);
  266. memcpy(pos, ifmgd->extra_ie, ifmgd->extra_ie_len);
  267. }
  268. if (wmm && (ifmgd->flags & IEEE80211_STA_WMM_ENABLED)) {
  269. pos = skb_put(skb, 9);
  270. *pos++ = WLAN_EID_VENDOR_SPECIFIC;
  271. *pos++ = 7; /* len */
  272. *pos++ = 0x00; /* Microsoft OUI 00:50:F2 */
  273. *pos++ = 0x50;
  274. *pos++ = 0xf2;
  275. *pos++ = 2; /* WME */
  276. *pos++ = 0; /* WME info */
  277. *pos++ = 1; /* WME ver */
  278. *pos++ = 0;
  279. }
  280. /* wmm support is a must to HT */
  281. /*
  282. * IEEE802.11n does not allow TKIP/WEP as pairwise
  283. * ciphers in HT mode. We still associate in non-ht
  284. * mode (11a/b/g) if any one of these ciphers is
  285. * configured as pairwise.
  286. */
  287. if (wmm && (ifmgd->flags & IEEE80211_STA_WMM_ENABLED) &&
  288. sband->ht_cap.ht_supported &&
  289. (ht_ie = ieee80211_bss_get_ie(bss, WLAN_EID_HT_INFORMATION)) &&
  290. ht_ie[1] >= sizeof(struct ieee80211_ht_info) &&
  291. (!(ifmgd->flags & IEEE80211_STA_TKIP_WEP_USED))) {
  292. struct ieee80211_ht_info *ht_info =
  293. (struct ieee80211_ht_info *)(ht_ie + 2);
  294. u16 cap = sband->ht_cap.cap;
  295. __le16 tmp;
  296. u32 flags = local->hw.conf.channel->flags;
  297. switch (ht_info->ht_param & IEEE80211_HT_PARAM_CHA_SEC_OFFSET) {
  298. case IEEE80211_HT_PARAM_CHA_SEC_ABOVE:
  299. if (flags & IEEE80211_CHAN_NO_FAT_ABOVE) {
  300. cap &= ~IEEE80211_HT_CAP_SUP_WIDTH_20_40;
  301. cap &= ~IEEE80211_HT_CAP_SGI_40;
  302. }
  303. break;
  304. case IEEE80211_HT_PARAM_CHA_SEC_BELOW:
  305. if (flags & IEEE80211_CHAN_NO_FAT_BELOW) {
  306. cap &= ~IEEE80211_HT_CAP_SUP_WIDTH_20_40;
  307. cap &= ~IEEE80211_HT_CAP_SGI_40;
  308. }
  309. break;
  310. }
  311. tmp = cpu_to_le16(cap);
  312. pos = skb_put(skb, sizeof(struct ieee80211_ht_cap)+2);
  313. *pos++ = WLAN_EID_HT_CAPABILITY;
  314. *pos++ = sizeof(struct ieee80211_ht_cap);
  315. memset(pos, 0, sizeof(struct ieee80211_ht_cap));
  316. memcpy(pos, &tmp, sizeof(u16));
  317. pos += sizeof(u16);
  318. /* TODO: needs a define here for << 2 */
  319. *pos++ = sband->ht_cap.ampdu_factor |
  320. (sband->ht_cap.ampdu_density << 2);
  321. memcpy(pos, &sband->ht_cap.mcs, sizeof(sband->ht_cap.mcs));
  322. }
  323. kfree(ifmgd->assocreq_ies);
  324. ifmgd->assocreq_ies_len = (skb->data + skb->len) - ies;
  325. ifmgd->assocreq_ies = kmalloc(ifmgd->assocreq_ies_len, GFP_KERNEL);
  326. if (ifmgd->assocreq_ies)
  327. memcpy(ifmgd->assocreq_ies, ies, ifmgd->assocreq_ies_len);
  328. ieee80211_tx_skb(sdata, skb, 0);
  329. }
  330. static void ieee80211_send_deauth_disassoc(struct ieee80211_sub_if_data *sdata,
  331. u16 stype, u16 reason)
  332. {
  333. struct ieee80211_local *local = sdata->local;
  334. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  335. struct sk_buff *skb;
  336. struct ieee80211_mgmt *mgmt;
  337. skb = dev_alloc_skb(local->hw.extra_tx_headroom + sizeof(*mgmt));
  338. if (!skb) {
  339. printk(KERN_DEBUG "%s: failed to allocate buffer for "
  340. "deauth/disassoc frame\n", sdata->dev->name);
  341. return;
  342. }
  343. skb_reserve(skb, local->hw.extra_tx_headroom);
  344. mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
  345. memset(mgmt, 0, 24);
  346. memcpy(mgmt->da, ifmgd->bssid, ETH_ALEN);
  347. memcpy(mgmt->sa, sdata->dev->dev_addr, ETH_ALEN);
  348. memcpy(mgmt->bssid, ifmgd->bssid, ETH_ALEN);
  349. mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT | stype);
  350. skb_put(skb, 2);
  351. /* u.deauth.reason_code == u.disassoc.reason_code */
  352. mgmt->u.deauth.reason_code = cpu_to_le16(reason);
  353. if (stype == IEEE80211_STYPE_DEAUTH)
  354. cfg80211_send_deauth(sdata->dev, (u8 *) mgmt, skb->len);
  355. else
  356. cfg80211_send_disassoc(sdata->dev, (u8 *) mgmt, skb->len);
  357. ieee80211_tx_skb(sdata, skb, ifmgd->flags & IEEE80211_STA_MFP_ENABLED);
  358. }
  359. void ieee80211_send_pspoll(struct ieee80211_local *local,
  360. struct ieee80211_sub_if_data *sdata)
  361. {
  362. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  363. struct ieee80211_pspoll *pspoll;
  364. struct sk_buff *skb;
  365. u16 fc;
  366. skb = dev_alloc_skb(local->hw.extra_tx_headroom + sizeof(*pspoll));
  367. if (!skb) {
  368. printk(KERN_DEBUG "%s: failed to allocate buffer for "
  369. "pspoll frame\n", sdata->dev->name);
  370. return;
  371. }
  372. skb_reserve(skb, local->hw.extra_tx_headroom);
  373. pspoll = (struct ieee80211_pspoll *) skb_put(skb, sizeof(*pspoll));
  374. memset(pspoll, 0, sizeof(*pspoll));
  375. fc = IEEE80211_FTYPE_CTL | IEEE80211_STYPE_PSPOLL | IEEE80211_FCTL_PM;
  376. pspoll->frame_control = cpu_to_le16(fc);
  377. pspoll->aid = cpu_to_le16(ifmgd->aid);
  378. /* aid in PS-Poll has its two MSBs each set to 1 */
  379. pspoll->aid |= cpu_to_le16(1 << 15 | 1 << 14);
  380. memcpy(pspoll->bssid, ifmgd->bssid, ETH_ALEN);
  381. memcpy(pspoll->ta, sdata->dev->dev_addr, ETH_ALEN);
  382. ieee80211_tx_skb(sdata, skb, 0);
  383. }
  384. void ieee80211_send_nullfunc(struct ieee80211_local *local,
  385. struct ieee80211_sub_if_data *sdata,
  386. int powersave)
  387. {
  388. struct sk_buff *skb;
  389. struct ieee80211_hdr *nullfunc;
  390. __le16 fc;
  391. if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_STATION))
  392. return;
  393. skb = dev_alloc_skb(local->hw.extra_tx_headroom + 24);
  394. if (!skb) {
  395. printk(KERN_DEBUG "%s: failed to allocate buffer for nullfunc "
  396. "frame\n", sdata->dev->name);
  397. return;
  398. }
  399. skb_reserve(skb, local->hw.extra_tx_headroom);
  400. nullfunc = (struct ieee80211_hdr *) skb_put(skb, 24);
  401. memset(nullfunc, 0, 24);
  402. fc = cpu_to_le16(IEEE80211_FTYPE_DATA | IEEE80211_STYPE_NULLFUNC |
  403. IEEE80211_FCTL_TODS);
  404. if (powersave)
  405. fc |= cpu_to_le16(IEEE80211_FCTL_PM);
  406. nullfunc->frame_control = fc;
  407. memcpy(nullfunc->addr1, sdata->u.mgd.bssid, ETH_ALEN);
  408. memcpy(nullfunc->addr2, sdata->dev->dev_addr, ETH_ALEN);
  409. memcpy(nullfunc->addr3, sdata->u.mgd.bssid, ETH_ALEN);
  410. ieee80211_tx_skb(sdata, skb, 0);
  411. }
  412. /* powersave */
  413. static void ieee80211_enable_ps(struct ieee80211_local *local,
  414. struct ieee80211_sub_if_data *sdata)
  415. {
  416. struct ieee80211_conf *conf = &local->hw.conf;
  417. /*
  418. * If we are scanning right now then the parameters will
  419. * take effect when scan finishes.
  420. */
  421. if (local->hw_scanning || local->sw_scanning)
  422. return;
  423. if (conf->dynamic_ps_timeout > 0 &&
  424. !(local->hw.flags & IEEE80211_HW_SUPPORTS_DYNAMIC_PS)) {
  425. mod_timer(&local->dynamic_ps_timer, jiffies +
  426. msecs_to_jiffies(conf->dynamic_ps_timeout));
  427. } else {
  428. if (local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK)
  429. ieee80211_send_nullfunc(local, sdata, 1);
  430. conf->flags |= IEEE80211_CONF_PS;
  431. ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
  432. }
  433. }
  434. static void ieee80211_change_ps(struct ieee80211_local *local)
  435. {
  436. struct ieee80211_conf *conf = &local->hw.conf;
  437. if (local->ps_sdata) {
  438. if (!(local->ps_sdata->u.mgd.flags & IEEE80211_STA_ASSOCIATED))
  439. return;
  440. ieee80211_enable_ps(local, local->ps_sdata);
  441. } else if (conf->flags & IEEE80211_CONF_PS) {
  442. conf->flags &= ~IEEE80211_CONF_PS;
  443. ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
  444. del_timer_sync(&local->dynamic_ps_timer);
  445. cancel_work_sync(&local->dynamic_ps_enable_work);
  446. }
  447. }
  448. /* need to hold RTNL or interface lock */
  449. void ieee80211_recalc_ps(struct ieee80211_local *local, s32 latency)
  450. {
  451. struct ieee80211_sub_if_data *sdata, *found = NULL;
  452. int count = 0;
  453. if (!(local->hw.flags & IEEE80211_HW_SUPPORTS_PS)) {
  454. local->ps_sdata = NULL;
  455. return;
  456. }
  457. list_for_each_entry(sdata, &local->interfaces, list) {
  458. if (!netif_running(sdata->dev))
  459. continue;
  460. if (sdata->vif.type != NL80211_IFTYPE_STATION)
  461. continue;
  462. found = sdata;
  463. count++;
  464. }
  465. if (count == 1 && found->u.mgd.powersave) {
  466. s32 beaconint_us;
  467. if (latency < 0)
  468. latency = pm_qos_requirement(PM_QOS_NETWORK_LATENCY);
  469. beaconint_us = ieee80211_tu_to_usec(
  470. found->vif.bss_conf.beacon_int);
  471. if (beaconint_us > latency) {
  472. local->ps_sdata = NULL;
  473. } else {
  474. u8 dtimper = found->vif.bss_conf.dtim_period;
  475. int maxslp = 1;
  476. if (dtimper > 1)
  477. maxslp = min_t(int, dtimper,
  478. latency / beaconint_us);
  479. local->hw.conf.max_sleep_period = maxslp;
  480. local->ps_sdata = found;
  481. }
  482. } else {
  483. local->ps_sdata = NULL;
  484. }
  485. ieee80211_change_ps(local);
  486. }
  487. void ieee80211_dynamic_ps_disable_work(struct work_struct *work)
  488. {
  489. struct ieee80211_local *local =
  490. container_of(work, struct ieee80211_local,
  491. dynamic_ps_disable_work);
  492. if (local->hw.conf.flags & IEEE80211_CONF_PS) {
  493. local->hw.conf.flags &= ~IEEE80211_CONF_PS;
  494. ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
  495. }
  496. ieee80211_wake_queues_by_reason(&local->hw,
  497. IEEE80211_QUEUE_STOP_REASON_PS);
  498. }
  499. void ieee80211_dynamic_ps_enable_work(struct work_struct *work)
  500. {
  501. struct ieee80211_local *local =
  502. container_of(work, struct ieee80211_local,
  503. dynamic_ps_enable_work);
  504. struct ieee80211_sub_if_data *sdata = local->ps_sdata;
  505. /* can only happen when PS was just disabled anyway */
  506. if (!sdata)
  507. return;
  508. if (local->hw.conf.flags & IEEE80211_CONF_PS)
  509. return;
  510. if (local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK)
  511. ieee80211_send_nullfunc(local, sdata, 1);
  512. local->hw.conf.flags |= IEEE80211_CONF_PS;
  513. ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
  514. }
  515. void ieee80211_dynamic_ps_timer(unsigned long data)
  516. {
  517. struct ieee80211_local *local = (void *) data;
  518. queue_work(local->hw.workqueue, &local->dynamic_ps_enable_work);
  519. }
  520. /* MLME */
  521. static void ieee80211_sta_wmm_params(struct ieee80211_local *local,
  522. struct ieee80211_if_managed *ifmgd,
  523. u8 *wmm_param, size_t wmm_param_len)
  524. {
  525. struct ieee80211_tx_queue_params params;
  526. size_t left;
  527. int count;
  528. u8 *pos;
  529. if (!(ifmgd->flags & IEEE80211_STA_WMM_ENABLED))
  530. return;
  531. if (!wmm_param)
  532. return;
  533. if (wmm_param_len < 8 || wmm_param[5] /* version */ != 1)
  534. return;
  535. count = wmm_param[6] & 0x0f;
  536. if (count == ifmgd->wmm_last_param_set)
  537. return;
  538. ifmgd->wmm_last_param_set = count;
  539. pos = wmm_param + 8;
  540. left = wmm_param_len - 8;
  541. memset(&params, 0, sizeof(params));
  542. local->wmm_acm = 0;
  543. for (; left >= 4; left -= 4, pos += 4) {
  544. int aci = (pos[0] >> 5) & 0x03;
  545. int acm = (pos[0] >> 4) & 0x01;
  546. int queue;
  547. switch (aci) {
  548. case 1: /* AC_BK */
  549. queue = 3;
  550. if (acm)
  551. local->wmm_acm |= BIT(1) | BIT(2); /* BK/- */
  552. break;
  553. case 2: /* AC_VI */
  554. queue = 1;
  555. if (acm)
  556. local->wmm_acm |= BIT(4) | BIT(5); /* CL/VI */
  557. break;
  558. case 3: /* AC_VO */
  559. queue = 0;
  560. if (acm)
  561. local->wmm_acm |= BIT(6) | BIT(7); /* VO/NC */
  562. break;
  563. case 0: /* AC_BE */
  564. default:
  565. queue = 2;
  566. if (acm)
  567. local->wmm_acm |= BIT(0) | BIT(3); /* BE/EE */
  568. break;
  569. }
  570. params.aifs = pos[0] & 0x0f;
  571. params.cw_max = ecw2cw((pos[1] & 0xf0) >> 4);
  572. params.cw_min = ecw2cw(pos[1] & 0x0f);
  573. params.txop = get_unaligned_le16(pos + 2);
  574. #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
  575. printk(KERN_DEBUG "%s: WMM queue=%d aci=%d acm=%d aifs=%d "
  576. "cWmin=%d cWmax=%d txop=%d\n",
  577. local->mdev->name, queue, aci, acm, params.aifs, params.cw_min,
  578. params.cw_max, params.txop);
  579. #endif
  580. if (drv_conf_tx(local, queue, &params) && local->ops->conf_tx)
  581. printk(KERN_DEBUG "%s: failed to set TX queue "
  582. "parameters for queue %d\n", local->mdev->name,
  583. queue);
  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 |= BSS_CHANGED_BEACON_INT;
  717. bss_info_changed |= ieee80211_handle_bss_capability(sdata,
  718. bss->cbss.capability, bss->has_erp_value, bss->erp_value);
  719. cfg80211_hold_bss(&bss->cbss);
  720. ieee80211_rx_bss_put(local, bss);
  721. }
  722. ifmgd->flags |= IEEE80211_STA_PREV_BSSID_SET;
  723. memcpy(ifmgd->prev_bssid, sdata->u.mgd.bssid, ETH_ALEN);
  724. ieee80211_sta_send_associnfo(sdata);
  725. ifmgd->last_probe = jiffies;
  726. ieee80211_led_assoc(local, 1);
  727. sdata->vif.bss_conf.assoc = 1;
  728. /*
  729. * For now just always ask the driver to update the basic rateset
  730. * when we have associated, we aren't checking whether it actually
  731. * changed or not.
  732. */
  733. bss_info_changed |= BSS_CHANGED_BASIC_RATES;
  734. ieee80211_bss_info_change_notify(sdata, bss_info_changed);
  735. /* will be same as sdata */
  736. if (local->ps_sdata) {
  737. mutex_lock(&local->iflist_mtx);
  738. ieee80211_recalc_ps(local, -1);
  739. mutex_unlock(&local->iflist_mtx);
  740. }
  741. netif_tx_start_all_queues(sdata->dev);
  742. netif_carrier_on(sdata->dev);
  743. ieee80211_sta_send_apinfo(sdata);
  744. }
  745. static void ieee80211_direct_probe(struct ieee80211_sub_if_data *sdata)
  746. {
  747. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  748. struct ieee80211_local *local = sdata->local;
  749. ifmgd->direct_probe_tries++;
  750. if (ifmgd->direct_probe_tries > IEEE80211_AUTH_MAX_TRIES) {
  751. printk(KERN_DEBUG "%s: direct probe to AP %pM timed out\n",
  752. sdata->dev->name, ifmgd->bssid);
  753. ifmgd->state = IEEE80211_STA_MLME_DISABLED;
  754. cfg80211_send_auth_timeout(sdata->dev, ifmgd->bssid);
  755. /*
  756. * Most likely AP is not in the range so remove the
  757. * bss information associated to the AP
  758. */
  759. ieee80211_rx_bss_remove(sdata, ifmgd->bssid,
  760. sdata->local->hw.conf.channel->center_freq,
  761. ifmgd->ssid, ifmgd->ssid_len);
  762. /*
  763. * We might have a pending scan which had no chance to run yet
  764. * due to state == IEEE80211_STA_MLME_DIRECT_PROBE.
  765. * Hence, queue the STAs work again
  766. */
  767. queue_work(local->hw.workqueue, &ifmgd->work);
  768. return;
  769. }
  770. printk(KERN_DEBUG "%s: direct probe to AP %pM try %d\n",
  771. sdata->dev->name, ifmgd->bssid,
  772. ifmgd->direct_probe_tries);
  773. ifmgd->state = IEEE80211_STA_MLME_DIRECT_PROBE;
  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 (ifmgd->state == IEEE80211_STA_MLME_DIRECT_PROBE) {
  1444. printk(KERN_DEBUG "%s direct probe responded\n",
  1445. sdata->dev->name);
  1446. ieee80211_authenticate(sdata);
  1447. }
  1448. if (ifmgd->flags & IEEE80211_STA_PROBEREQ_POLL)
  1449. ifmgd->flags &= ~IEEE80211_STA_PROBEREQ_POLL;
  1450. }
  1451. /*
  1452. * This is the canonical list of information elements we care about,
  1453. * the filter code also gives us all changes to the Microsoft OUI
  1454. * (00:50:F2) vendor IE which is used for WMM which we need to track.
  1455. *
  1456. * We implement beacon filtering in software since that means we can
  1457. * avoid processing the frame here and in cfg80211, and userspace
  1458. * will not be able to tell whether the hardware supports it or not.
  1459. *
  1460. * XXX: This list needs to be dynamic -- userspace needs to be able to
  1461. * add items it requires. It also needs to be able to tell us to
  1462. * look out for other vendor IEs.
  1463. */
  1464. static const u64 care_about_ies =
  1465. (1ULL << WLAN_EID_COUNTRY) |
  1466. (1ULL << WLAN_EID_ERP_INFO) |
  1467. (1ULL << WLAN_EID_CHANNEL_SWITCH) |
  1468. (1ULL << WLAN_EID_PWR_CONSTRAINT) |
  1469. (1ULL << WLAN_EID_HT_CAPABILITY) |
  1470. (1ULL << WLAN_EID_HT_INFORMATION);
  1471. static void ieee80211_rx_mgmt_beacon(struct ieee80211_sub_if_data *sdata,
  1472. struct ieee80211_mgmt *mgmt,
  1473. size_t len,
  1474. struct ieee80211_rx_status *rx_status)
  1475. {
  1476. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  1477. size_t baselen;
  1478. struct ieee802_11_elems elems;
  1479. struct ieee80211_local *local = sdata->local;
  1480. u32 changed = 0;
  1481. bool erp_valid, directed_tim = false;
  1482. u8 erp_value = 0;
  1483. u32 ncrc;
  1484. /* Process beacon from the current BSS */
  1485. baselen = (u8 *) mgmt->u.beacon.variable - (u8 *) mgmt;
  1486. if (baselen > len)
  1487. return;
  1488. if (rx_status->freq != local->hw.conf.channel->center_freq)
  1489. return;
  1490. if (!(ifmgd->flags & IEEE80211_STA_ASSOCIATED) ||
  1491. memcmp(ifmgd->bssid, mgmt->bssid, ETH_ALEN) != 0)
  1492. return;
  1493. ncrc = crc32_be(0, (void *)&mgmt->u.beacon.beacon_int, 4);
  1494. ncrc = ieee802_11_parse_elems_crc(mgmt->u.beacon.variable,
  1495. len - baselen, &elems,
  1496. care_about_ies, ncrc);
  1497. if (local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK)
  1498. directed_tim = ieee80211_check_tim(elems.tim, elems.tim_len,
  1499. ifmgd->aid);
  1500. ncrc = crc32_be(ncrc, (void *)&directed_tim, sizeof(directed_tim));
  1501. if (ncrc == ifmgd->beacon_crc)
  1502. return;
  1503. ifmgd->beacon_crc = ncrc;
  1504. ieee80211_rx_bss_info(sdata, mgmt, len, rx_status, &elems, true);
  1505. ieee80211_sta_wmm_params(local, ifmgd, elems.wmm_param,
  1506. elems.wmm_param_len);
  1507. if (local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK) {
  1508. if (directed_tim) {
  1509. if (local->hw.conf.dynamic_ps_timeout > 0) {
  1510. local->hw.conf.flags &= ~IEEE80211_CONF_PS;
  1511. ieee80211_hw_config(local,
  1512. IEEE80211_CONF_CHANGE_PS);
  1513. ieee80211_send_nullfunc(local, sdata, 0);
  1514. } else {
  1515. local->pspolling = true;
  1516. /*
  1517. * Here is assumed that the driver will be
  1518. * able to send ps-poll frame and receive a
  1519. * response even though power save mode is
  1520. * enabled, but some drivers might require
  1521. * to disable power save here. This needs
  1522. * to be investigated.
  1523. */
  1524. ieee80211_send_pspoll(local, sdata);
  1525. }
  1526. }
  1527. }
  1528. if (elems.erp_info && elems.erp_info_len >= 1) {
  1529. erp_valid = true;
  1530. erp_value = elems.erp_info[0];
  1531. } else {
  1532. erp_valid = false;
  1533. }
  1534. changed |= ieee80211_handle_bss_capability(sdata,
  1535. le16_to_cpu(mgmt->u.beacon.capab_info),
  1536. erp_valid, erp_value);
  1537. if (elems.ht_cap_elem && elems.ht_info_elem && elems.wmm_param &&
  1538. !(ifmgd->flags & IEEE80211_STA_TKIP_WEP_USED)) {
  1539. struct sta_info *sta;
  1540. struct ieee80211_supported_band *sband;
  1541. u16 ap_ht_cap_flags;
  1542. rcu_read_lock();
  1543. sta = sta_info_get(local, ifmgd->bssid);
  1544. if (!sta) {
  1545. rcu_read_unlock();
  1546. return;
  1547. }
  1548. sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
  1549. ieee80211_ht_cap_ie_to_sta_ht_cap(sband,
  1550. elems.ht_cap_elem, &sta->sta.ht_cap);
  1551. ap_ht_cap_flags = sta->sta.ht_cap.cap;
  1552. rcu_read_unlock();
  1553. changed |= ieee80211_enable_ht(sdata, elems.ht_info_elem,
  1554. ap_ht_cap_flags);
  1555. }
  1556. if (elems.country_elem) {
  1557. /* Note we are only reviewing this on beacons
  1558. * for the BSSID we are associated to */
  1559. regulatory_hint_11d(local->hw.wiphy,
  1560. elems.country_elem, elems.country_elem_len);
  1561. /* TODO: IBSS also needs this */
  1562. if (elems.pwr_constr_elem)
  1563. ieee80211_handle_pwr_constr(sdata,
  1564. le16_to_cpu(mgmt->u.probe_resp.capab_info),
  1565. elems.pwr_constr_elem,
  1566. elems.pwr_constr_elem_len);
  1567. }
  1568. ieee80211_bss_info_change_notify(sdata, changed);
  1569. }
  1570. ieee80211_rx_result ieee80211_sta_rx_mgmt(struct ieee80211_sub_if_data *sdata,
  1571. struct sk_buff *skb,
  1572. struct ieee80211_rx_status *rx_status)
  1573. {
  1574. struct ieee80211_local *local = sdata->local;
  1575. struct ieee80211_mgmt *mgmt;
  1576. u16 fc;
  1577. if (skb->len < 24)
  1578. return RX_DROP_MONITOR;
  1579. mgmt = (struct ieee80211_mgmt *) skb->data;
  1580. fc = le16_to_cpu(mgmt->frame_control);
  1581. switch (fc & IEEE80211_FCTL_STYPE) {
  1582. case IEEE80211_STYPE_PROBE_REQ:
  1583. case IEEE80211_STYPE_PROBE_RESP:
  1584. case IEEE80211_STYPE_BEACON:
  1585. memcpy(skb->cb, rx_status, sizeof(*rx_status));
  1586. case IEEE80211_STYPE_AUTH:
  1587. case IEEE80211_STYPE_ASSOC_RESP:
  1588. case IEEE80211_STYPE_REASSOC_RESP:
  1589. case IEEE80211_STYPE_DEAUTH:
  1590. case IEEE80211_STYPE_DISASSOC:
  1591. skb_queue_tail(&sdata->u.mgd.skb_queue, skb);
  1592. queue_work(local->hw.workqueue, &sdata->u.mgd.work);
  1593. return RX_QUEUED;
  1594. }
  1595. return RX_DROP_MONITOR;
  1596. }
  1597. static void ieee80211_sta_rx_queued_mgmt(struct ieee80211_sub_if_data *sdata,
  1598. struct sk_buff *skb)
  1599. {
  1600. struct ieee80211_rx_status *rx_status;
  1601. struct ieee80211_mgmt *mgmt;
  1602. u16 fc;
  1603. rx_status = (struct ieee80211_rx_status *) skb->cb;
  1604. mgmt = (struct ieee80211_mgmt *) skb->data;
  1605. fc = le16_to_cpu(mgmt->frame_control);
  1606. switch (fc & IEEE80211_FCTL_STYPE) {
  1607. case IEEE80211_STYPE_PROBE_RESP:
  1608. ieee80211_rx_mgmt_probe_resp(sdata, mgmt, skb->len,
  1609. rx_status);
  1610. break;
  1611. case IEEE80211_STYPE_BEACON:
  1612. ieee80211_rx_mgmt_beacon(sdata, mgmt, skb->len,
  1613. rx_status);
  1614. break;
  1615. case IEEE80211_STYPE_AUTH:
  1616. ieee80211_rx_mgmt_auth(sdata, mgmt, skb->len);
  1617. break;
  1618. case IEEE80211_STYPE_ASSOC_RESP:
  1619. ieee80211_rx_mgmt_assoc_resp(sdata, mgmt, skb->len, 0);
  1620. break;
  1621. case IEEE80211_STYPE_REASSOC_RESP:
  1622. ieee80211_rx_mgmt_assoc_resp(sdata, mgmt, skb->len, 1);
  1623. break;
  1624. case IEEE80211_STYPE_DEAUTH:
  1625. ieee80211_rx_mgmt_deauth(sdata, mgmt, skb->len);
  1626. break;
  1627. case IEEE80211_STYPE_DISASSOC:
  1628. ieee80211_rx_mgmt_disassoc(sdata, mgmt, skb->len);
  1629. break;
  1630. }
  1631. kfree_skb(skb);
  1632. }
  1633. static void ieee80211_sta_timer(unsigned long data)
  1634. {
  1635. struct ieee80211_sub_if_data *sdata =
  1636. (struct ieee80211_sub_if_data *) data;
  1637. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  1638. struct ieee80211_local *local = sdata->local;
  1639. set_bit(IEEE80211_STA_REQ_RUN, &ifmgd->request);
  1640. queue_work(local->hw.workqueue, &ifmgd->work);
  1641. }
  1642. static void ieee80211_sta_reset_auth(struct ieee80211_sub_if_data *sdata)
  1643. {
  1644. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  1645. struct ieee80211_local *local = sdata->local;
  1646. /* Reset own TSF to allow time synchronization work. */
  1647. drv_reset_tsf(local);
  1648. ifmgd->wmm_last_param_set = -1; /* allow any WMM update */
  1649. if (ifmgd->auth_algs & IEEE80211_AUTH_ALG_OPEN)
  1650. ifmgd->auth_alg = WLAN_AUTH_OPEN;
  1651. else if (ifmgd->auth_algs & IEEE80211_AUTH_ALG_SHARED_KEY)
  1652. ifmgd->auth_alg = WLAN_AUTH_SHARED_KEY;
  1653. else if (ifmgd->auth_algs & IEEE80211_AUTH_ALG_LEAP)
  1654. ifmgd->auth_alg = WLAN_AUTH_LEAP;
  1655. else if (ifmgd->auth_algs & IEEE80211_AUTH_ALG_FT)
  1656. ifmgd->auth_alg = WLAN_AUTH_FT;
  1657. else
  1658. ifmgd->auth_alg = WLAN_AUTH_OPEN;
  1659. ifmgd->auth_transaction = -1;
  1660. ifmgd->flags &= ~IEEE80211_STA_ASSOCIATED;
  1661. ifmgd->assoc_scan_tries = 0;
  1662. ifmgd->direct_probe_tries = 0;
  1663. ifmgd->auth_tries = 0;
  1664. ifmgd->assoc_tries = 0;
  1665. netif_tx_stop_all_queues(sdata->dev);
  1666. netif_carrier_off(sdata->dev);
  1667. }
  1668. static int ieee80211_sta_config_auth(struct ieee80211_sub_if_data *sdata)
  1669. {
  1670. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  1671. struct ieee80211_local *local = sdata->local;
  1672. struct ieee80211_bss *bss;
  1673. u8 *bssid = ifmgd->bssid, *ssid = ifmgd->ssid;
  1674. u8 ssid_len = ifmgd->ssid_len;
  1675. u16 capa_mask = WLAN_CAPABILITY_ESS;
  1676. u16 capa_val = WLAN_CAPABILITY_ESS;
  1677. struct ieee80211_channel *chan = local->oper_channel;
  1678. if (!(ifmgd->flags & IEEE80211_STA_EXT_SME) &&
  1679. ifmgd->flags & (IEEE80211_STA_AUTO_SSID_SEL |
  1680. IEEE80211_STA_AUTO_BSSID_SEL |
  1681. IEEE80211_STA_AUTO_CHANNEL_SEL)) {
  1682. capa_mask |= WLAN_CAPABILITY_PRIVACY;
  1683. if (sdata->default_key)
  1684. capa_val |= WLAN_CAPABILITY_PRIVACY;
  1685. }
  1686. if (ifmgd->flags & IEEE80211_STA_AUTO_CHANNEL_SEL)
  1687. chan = NULL;
  1688. if (ifmgd->flags & IEEE80211_STA_AUTO_BSSID_SEL)
  1689. bssid = NULL;
  1690. if (ifmgd->flags & IEEE80211_STA_AUTO_SSID_SEL) {
  1691. ssid = NULL;
  1692. ssid_len = 0;
  1693. }
  1694. bss = (void *)cfg80211_get_bss(local->hw.wiphy, chan,
  1695. bssid, ssid, ssid_len,
  1696. capa_mask, capa_val);
  1697. if (bss) {
  1698. ieee80211_set_freq(sdata, bss->cbss.channel->center_freq);
  1699. if (!(ifmgd->flags & IEEE80211_STA_SSID_SET))
  1700. ieee80211_sta_set_ssid(sdata, bss->ssid,
  1701. bss->ssid_len);
  1702. ieee80211_sta_set_bssid(sdata, bss->cbss.bssid);
  1703. ieee80211_sta_def_wmm_params(sdata, bss->supp_rates_len,
  1704. bss->supp_rates);
  1705. if (sdata->u.mgd.mfp == IEEE80211_MFP_REQUIRED)
  1706. sdata->u.mgd.flags |= IEEE80211_STA_MFP_ENABLED;
  1707. else
  1708. sdata->u.mgd.flags &= ~IEEE80211_STA_MFP_ENABLED;
  1709. /* Send out direct probe if no probe resp was received or
  1710. * the one we have is outdated
  1711. */
  1712. if (!bss->last_probe_resp ||
  1713. time_after(jiffies, bss->last_probe_resp
  1714. + IEEE80211_SCAN_RESULT_EXPIRE))
  1715. ifmgd->state = IEEE80211_STA_MLME_DIRECT_PROBE;
  1716. else
  1717. ifmgd->state = IEEE80211_STA_MLME_AUTHENTICATE;
  1718. ieee80211_rx_bss_put(local, bss);
  1719. ieee80211_sta_reset_auth(sdata);
  1720. return 0;
  1721. } else {
  1722. if (ifmgd->assoc_scan_tries < IEEE80211_ASSOC_SCANS_MAX_TRIES) {
  1723. u8 ssid_len = 0;
  1724. if (!(ifmgd->flags & IEEE80211_STA_AUTO_SSID_SEL))
  1725. ssid_len = ifmgd->ssid_len;
  1726. ifmgd->assoc_scan_tries++;
  1727. ieee80211_request_internal_scan(sdata, ifmgd->ssid,
  1728. ssid_len);
  1729. ifmgd->state = IEEE80211_STA_MLME_AUTHENTICATE;
  1730. set_bit(IEEE80211_STA_REQ_AUTH, &ifmgd->request);
  1731. } else {
  1732. ifmgd->assoc_scan_tries = 0;
  1733. ifmgd->state = IEEE80211_STA_MLME_DISABLED;
  1734. }
  1735. }
  1736. return -1;
  1737. }
  1738. static void ieee80211_sta_work(struct work_struct *work)
  1739. {
  1740. struct ieee80211_sub_if_data *sdata =
  1741. container_of(work, struct ieee80211_sub_if_data, u.mgd.work);
  1742. struct ieee80211_local *local = sdata->local;
  1743. struct ieee80211_if_managed *ifmgd;
  1744. struct sk_buff *skb;
  1745. if (!netif_running(sdata->dev))
  1746. return;
  1747. if (local->sw_scanning || local->hw_scanning)
  1748. return;
  1749. if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_STATION))
  1750. return;
  1751. ifmgd = &sdata->u.mgd;
  1752. while ((skb = skb_dequeue(&ifmgd->skb_queue)))
  1753. ieee80211_sta_rx_queued_mgmt(sdata, skb);
  1754. if (ifmgd->state != IEEE80211_STA_MLME_DIRECT_PROBE &&
  1755. ifmgd->state != IEEE80211_STA_MLME_AUTHENTICATE &&
  1756. ifmgd->state != IEEE80211_STA_MLME_ASSOCIATE &&
  1757. test_and_clear_bit(IEEE80211_STA_REQ_SCAN, &ifmgd->request)) {
  1758. queue_delayed_work(local->hw.workqueue, &local->scan_work,
  1759. round_jiffies_relative(0));
  1760. return;
  1761. }
  1762. if (test_and_clear_bit(IEEE80211_STA_REQ_AUTH, &ifmgd->request)) {
  1763. if (ieee80211_sta_config_auth(sdata))
  1764. return;
  1765. clear_bit(IEEE80211_STA_REQ_RUN, &ifmgd->request);
  1766. } else if (!test_and_clear_bit(IEEE80211_STA_REQ_RUN, &ifmgd->request))
  1767. return;
  1768. switch (ifmgd->state) {
  1769. case IEEE80211_STA_MLME_DISABLED:
  1770. break;
  1771. case IEEE80211_STA_MLME_DIRECT_PROBE:
  1772. ieee80211_direct_probe(sdata);
  1773. break;
  1774. case IEEE80211_STA_MLME_AUTHENTICATE:
  1775. ieee80211_authenticate(sdata);
  1776. break;
  1777. case IEEE80211_STA_MLME_ASSOCIATE:
  1778. ieee80211_associate(sdata);
  1779. break;
  1780. case IEEE80211_STA_MLME_ASSOCIATED:
  1781. ieee80211_associated(sdata);
  1782. break;
  1783. default:
  1784. WARN_ON(1);
  1785. break;
  1786. }
  1787. if (ieee80211_privacy_mismatch(sdata)) {
  1788. printk(KERN_DEBUG "%s: privacy configuration mismatch and "
  1789. "mixed-cell disabled - disassociate\n", sdata->dev->name);
  1790. ieee80211_set_disassoc(sdata, false, true,
  1791. WLAN_REASON_UNSPECIFIED);
  1792. }
  1793. }
  1794. static void ieee80211_restart_sta_timer(struct ieee80211_sub_if_data *sdata)
  1795. {
  1796. if (sdata->vif.type == NL80211_IFTYPE_STATION) {
  1797. /*
  1798. * Need to update last_beacon to avoid beacon loss
  1799. * test to trigger.
  1800. */
  1801. sdata->u.mgd.last_beacon = jiffies;
  1802. queue_work(sdata->local->hw.workqueue,
  1803. &sdata->u.mgd.work);
  1804. }
  1805. }
  1806. /* interface setup */
  1807. void ieee80211_sta_setup_sdata(struct ieee80211_sub_if_data *sdata)
  1808. {
  1809. struct ieee80211_if_managed *ifmgd;
  1810. u32 hw_flags;
  1811. ifmgd = &sdata->u.mgd;
  1812. INIT_WORK(&ifmgd->work, ieee80211_sta_work);
  1813. INIT_WORK(&ifmgd->chswitch_work, ieee80211_chswitch_work);
  1814. INIT_WORK(&ifmgd->beacon_loss_work, ieee80211_beacon_loss_work);
  1815. setup_timer(&ifmgd->timer, ieee80211_sta_timer,
  1816. (unsigned long) sdata);
  1817. setup_timer(&ifmgd->chswitch_timer, ieee80211_chswitch_timer,
  1818. (unsigned long) sdata);
  1819. skb_queue_head_init(&ifmgd->skb_queue);
  1820. ifmgd->capab = WLAN_CAPABILITY_ESS;
  1821. ifmgd->auth_algs = IEEE80211_AUTH_ALG_OPEN |
  1822. IEEE80211_AUTH_ALG_SHARED_KEY;
  1823. ifmgd->flags |= IEEE80211_STA_CREATE_IBSS |
  1824. IEEE80211_STA_AUTO_BSSID_SEL |
  1825. IEEE80211_STA_AUTO_CHANNEL_SEL;
  1826. if (sdata->local->hw.queues >= 4)
  1827. ifmgd->flags |= IEEE80211_STA_WMM_ENABLED;
  1828. hw_flags = sdata->local->hw.flags;
  1829. if (hw_flags & IEEE80211_HW_SUPPORTS_PS) {
  1830. ifmgd->powersave = CONFIG_MAC80211_DEFAULT_PS_VALUE;
  1831. sdata->local->hw.conf.dynamic_ps_timeout = 500;
  1832. }
  1833. }
  1834. /* configuration hooks */
  1835. void ieee80211_sta_req_auth(struct ieee80211_sub_if_data *sdata)
  1836. {
  1837. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  1838. struct ieee80211_local *local = sdata->local;
  1839. if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_STATION))
  1840. return;
  1841. if ((ifmgd->flags & (IEEE80211_STA_BSSID_SET |
  1842. IEEE80211_STA_AUTO_BSSID_SEL)) &&
  1843. (ifmgd->flags & (IEEE80211_STA_SSID_SET |
  1844. IEEE80211_STA_AUTO_SSID_SEL))) {
  1845. if (ifmgd->state == IEEE80211_STA_MLME_ASSOCIATED)
  1846. ieee80211_set_disassoc(sdata, true, true,
  1847. WLAN_REASON_DEAUTH_LEAVING);
  1848. if (!(ifmgd->flags & IEEE80211_STA_EXT_SME) ||
  1849. ifmgd->state != IEEE80211_STA_MLME_ASSOCIATE)
  1850. set_bit(IEEE80211_STA_REQ_AUTH, &ifmgd->request);
  1851. else if (ifmgd->flags & IEEE80211_STA_EXT_SME)
  1852. set_bit(IEEE80211_STA_REQ_RUN, &ifmgd->request);
  1853. queue_work(local->hw.workqueue, &ifmgd->work);
  1854. }
  1855. }
  1856. int ieee80211_sta_commit(struct ieee80211_sub_if_data *sdata)
  1857. {
  1858. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  1859. if (ifmgd->ssid_len)
  1860. ifmgd->flags |= IEEE80211_STA_SSID_SET;
  1861. else
  1862. ifmgd->flags &= ~IEEE80211_STA_SSID_SET;
  1863. return 0;
  1864. }
  1865. int ieee80211_sta_set_ssid(struct ieee80211_sub_if_data *sdata, char *ssid, size_t len)
  1866. {
  1867. struct ieee80211_if_managed *ifmgd;
  1868. if (len > IEEE80211_MAX_SSID_LEN)
  1869. return -EINVAL;
  1870. ifmgd = &sdata->u.mgd;
  1871. if (ifmgd->ssid_len != len || memcmp(ifmgd->ssid, ssid, len) != 0) {
  1872. /*
  1873. * Do not use reassociation if SSID is changed (different ESS).
  1874. */
  1875. ifmgd->flags &= ~IEEE80211_STA_PREV_BSSID_SET;
  1876. memset(ifmgd->ssid, 0, sizeof(ifmgd->ssid));
  1877. memcpy(ifmgd->ssid, ssid, len);
  1878. ifmgd->ssid_len = len;
  1879. }
  1880. return ieee80211_sta_commit(sdata);
  1881. }
  1882. int ieee80211_sta_get_ssid(struct ieee80211_sub_if_data *sdata, char *ssid, size_t *len)
  1883. {
  1884. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  1885. memcpy(ssid, ifmgd->ssid, ifmgd->ssid_len);
  1886. *len = ifmgd->ssid_len;
  1887. return 0;
  1888. }
  1889. int ieee80211_sta_set_bssid(struct ieee80211_sub_if_data *sdata, u8 *bssid)
  1890. {
  1891. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  1892. if (is_valid_ether_addr(bssid)) {
  1893. memcpy(ifmgd->bssid, bssid, ETH_ALEN);
  1894. ifmgd->flags |= IEEE80211_STA_BSSID_SET;
  1895. } else {
  1896. memset(ifmgd->bssid, 0, ETH_ALEN);
  1897. ifmgd->flags &= ~IEEE80211_STA_BSSID_SET;
  1898. }
  1899. if (netif_running(sdata->dev))
  1900. ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BSSID);
  1901. return ieee80211_sta_commit(sdata);
  1902. }
  1903. int ieee80211_sta_set_extra_ie(struct ieee80211_sub_if_data *sdata,
  1904. const char *ie, size_t len)
  1905. {
  1906. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  1907. kfree(ifmgd->extra_ie);
  1908. if (len == 0) {
  1909. ifmgd->extra_ie = NULL;
  1910. ifmgd->extra_ie_len = 0;
  1911. return 0;
  1912. }
  1913. ifmgd->extra_ie = kmalloc(len, GFP_KERNEL);
  1914. if (!ifmgd->extra_ie) {
  1915. ifmgd->extra_ie_len = 0;
  1916. return -ENOMEM;
  1917. }
  1918. memcpy(ifmgd->extra_ie, ie, len);
  1919. ifmgd->extra_ie_len = len;
  1920. return 0;
  1921. }
  1922. int ieee80211_sta_deauthenticate(struct ieee80211_sub_if_data *sdata, u16 reason)
  1923. {
  1924. printk(KERN_DEBUG "%s: deauthenticating by local choice (reason=%d)\n",
  1925. sdata->dev->name, reason);
  1926. ieee80211_set_disassoc(sdata, true, true, reason);
  1927. return 0;
  1928. }
  1929. int ieee80211_sta_disassociate(struct ieee80211_sub_if_data *sdata, u16 reason)
  1930. {
  1931. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  1932. printk(KERN_DEBUG "%s: disassociating by local choice (reason=%d)\n",
  1933. sdata->dev->name, reason);
  1934. if (!(ifmgd->flags & IEEE80211_STA_ASSOCIATED))
  1935. return -ENOLINK;
  1936. ieee80211_set_disassoc(sdata, false, true, reason);
  1937. return 0;
  1938. }
  1939. /* scan finished notification */
  1940. void ieee80211_mlme_notify_scan_completed(struct ieee80211_local *local)
  1941. {
  1942. struct ieee80211_sub_if_data *sdata = local->scan_sdata;
  1943. /* Restart STA timers */
  1944. rcu_read_lock();
  1945. list_for_each_entry_rcu(sdata, &local->interfaces, list)
  1946. ieee80211_restart_sta_timer(sdata);
  1947. rcu_read_unlock();
  1948. }
  1949. int ieee80211_max_network_latency(struct notifier_block *nb,
  1950. unsigned long data, void *dummy)
  1951. {
  1952. s32 latency_usec = (s32) data;
  1953. struct ieee80211_local *local =
  1954. container_of(nb, struct ieee80211_local,
  1955. network_latency_notifier);
  1956. mutex_lock(&local->iflist_mtx);
  1957. ieee80211_recalc_ps(local, latency_usec);
  1958. mutex_unlock(&local->iflist_mtx);
  1959. return 0;
  1960. }