mlme.c 71 KB

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