mlme.c 72 KB

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