mlme.c 68 KB

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