mlme.c 70 KB

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