mlme.c 64 KB

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