mlme.c 71 KB

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