mlme.c 70 KB

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