mlme.c 55 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028
  1. /*
  2. * BSS client mode implementation
  3. * Copyright 2003-2008, Jouni Malinen <j@w1.fi>
  4. * Copyright 2004, Instant802 Networks, Inc.
  5. * Copyright 2005, Devicescape Software, Inc.
  6. * Copyright 2006-2007 Jiri Benc <jbenc@suse.cz>
  7. * Copyright 2007, Michael Wu <flamingice@sourmilk.net>
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2 as
  11. * published by the Free Software Foundation.
  12. */
  13. #include <linux/delay.h>
  14. #include <linux/if_ether.h>
  15. #include <linux/skbuff.h>
  16. #include <linux/if_arp.h>
  17. #include <linux/etherdevice.h>
  18. #include <linux/rtnetlink.h>
  19. #include <linux/pm_qos_params.h>
  20. #include <linux/crc32.h>
  21. #include <net/mac80211.h>
  22. #include <asm/unaligned.h>
  23. #include "ieee80211_i.h"
  24. #include "driver-ops.h"
  25. #include "rate.h"
  26. #include "led.h"
  27. #define IEEE80211_AUTH_TIMEOUT (HZ / 5)
  28. #define IEEE80211_AUTH_MAX_TRIES 3
  29. #define IEEE80211_ASSOC_TIMEOUT (HZ / 5)
  30. #define IEEE80211_ASSOC_MAX_TRIES 3
  31. #define IEEE80211_MAX_PROBE_TRIES 5
  32. /*
  33. * beacon loss detection timeout
  34. * XXX: should depend on beacon interval
  35. */
  36. #define IEEE80211_BEACON_LOSS_TIME (2 * HZ)
  37. /*
  38. * Time the connection can be idle before we probe
  39. * it to see if we can still talk to the AP.
  40. */
  41. #define IEEE80211_CONNECTION_IDLE_TIME (30 * HZ)
  42. /*
  43. * Time we wait for a probe response after sending
  44. * a probe request because of beacon loss or for
  45. * checking the connection still works.
  46. */
  47. #define IEEE80211_PROBE_WAIT (HZ / 2)
  48. #define TMR_RUNNING_TIMER 0
  49. #define TMR_RUNNING_CHANSW 1
  50. /*
  51. * All cfg80211 functions have to be called outside a locked
  52. * section so that they can acquire a lock themselves... This
  53. * is much simpler than queuing up things in cfg80211, but we
  54. * do need some indirection for that here.
  55. */
  56. enum rx_mgmt_action {
  57. /* no action required */
  58. RX_MGMT_NONE,
  59. /* caller must call cfg80211_send_rx_auth() */
  60. RX_MGMT_CFG80211_AUTH,
  61. /* caller must call cfg80211_send_rx_assoc() */
  62. RX_MGMT_CFG80211_ASSOC,
  63. /* caller must call cfg80211_send_deauth() */
  64. RX_MGMT_CFG80211_DEAUTH,
  65. /* caller must call cfg80211_send_disassoc() */
  66. RX_MGMT_CFG80211_DISASSOC,
  67. /* caller must tell cfg80211 about internal error */
  68. RX_MGMT_CFG80211_ASSOC_ERROR,
  69. };
  70. /* utils */
  71. static inline void ASSERT_MGD_MTX(struct ieee80211_if_managed *ifmgd)
  72. {
  73. WARN_ON(!mutex_is_locked(&ifmgd->mtx));
  74. }
  75. /*
  76. * We can have multiple work items (and connection probing)
  77. * scheduling this timer, but we need to take care to only
  78. * reschedule it when it should fire _earlier_ than it was
  79. * asked for before, or if it's not pending right now. This
  80. * function ensures that. Note that it then is required to
  81. * run this function for all timeouts after the first one
  82. * has happened -- the work that runs from this timer will
  83. * do that.
  84. */
  85. static void run_again(struct ieee80211_if_managed *ifmgd,
  86. unsigned long timeout)
  87. {
  88. ASSERT_MGD_MTX(ifmgd);
  89. if (!timer_pending(&ifmgd->timer) ||
  90. time_before(timeout, ifmgd->timer.expires))
  91. mod_timer(&ifmgd->timer, timeout);
  92. }
  93. static void mod_beacon_timer(struct ieee80211_sub_if_data *sdata)
  94. {
  95. if (sdata->local->hw.flags & IEEE80211_HW_BEACON_FILTER)
  96. return;
  97. mod_timer(&sdata->u.mgd.bcn_mon_timer,
  98. round_jiffies_up(jiffies + IEEE80211_BEACON_LOSS_TIME));
  99. }
  100. static int ecw2cw(int ecw)
  101. {
  102. return (1 << ecw) - 1;
  103. }
  104. /*
  105. * ieee80211_enable_ht should be called only after the operating band
  106. * has been determined as ht configuration depends on the hw's
  107. * HT abilities for a specific band.
  108. */
  109. static u32 ieee80211_enable_ht(struct ieee80211_sub_if_data *sdata,
  110. struct ieee80211_ht_info *hti,
  111. const u8 *bssid, u16 ap_ht_cap_flags)
  112. {
  113. struct ieee80211_local *local = sdata->local;
  114. struct ieee80211_supported_band *sband;
  115. struct sta_info *sta;
  116. u32 changed = 0;
  117. u16 ht_opmode;
  118. bool enable_ht = true, ht_changed;
  119. enum nl80211_channel_type channel_type = NL80211_CHAN_NO_HT;
  120. sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
  121. /* HT is not supported */
  122. if (!sband->ht_cap.ht_supported)
  123. enable_ht = false;
  124. /* check that channel matches the right operating channel */
  125. if (local->hw.conf.channel->center_freq !=
  126. ieee80211_channel_to_frequency(hti->control_chan))
  127. enable_ht = false;
  128. if (enable_ht) {
  129. channel_type = NL80211_CHAN_HT20;
  130. if (!(ap_ht_cap_flags & IEEE80211_HT_CAP_40MHZ_INTOLERANT) &&
  131. (sband->ht_cap.cap & IEEE80211_HT_CAP_SUP_WIDTH_20_40) &&
  132. (hti->ht_param & IEEE80211_HT_PARAM_CHAN_WIDTH_ANY)) {
  133. switch(hti->ht_param & IEEE80211_HT_PARAM_CHA_SEC_OFFSET) {
  134. case IEEE80211_HT_PARAM_CHA_SEC_ABOVE:
  135. if (!(local->hw.conf.channel->flags &
  136. IEEE80211_CHAN_NO_HT40PLUS))
  137. channel_type = NL80211_CHAN_HT40PLUS;
  138. break;
  139. case IEEE80211_HT_PARAM_CHA_SEC_BELOW:
  140. if (!(local->hw.conf.channel->flags &
  141. IEEE80211_CHAN_NO_HT40MINUS))
  142. channel_type = NL80211_CHAN_HT40MINUS;
  143. break;
  144. }
  145. }
  146. }
  147. ht_changed = conf_is_ht(&local->hw.conf) != enable_ht ||
  148. channel_type != local->hw.conf.channel_type;
  149. local->oper_channel_type = channel_type;
  150. if (ht_changed) {
  151. /* channel_type change automatically detected */
  152. ieee80211_hw_config(local, 0);
  153. rcu_read_lock();
  154. sta = sta_info_get(sdata, bssid);
  155. if (sta)
  156. rate_control_rate_update(local, sband, sta,
  157. IEEE80211_RC_HT_CHANGED);
  158. rcu_read_unlock();
  159. }
  160. /* disable HT */
  161. if (!enable_ht)
  162. return 0;
  163. ht_opmode = le16_to_cpu(hti->operation_mode);
  164. /* if bss configuration changed store the new one */
  165. if (!sdata->ht_opmode_valid ||
  166. sdata->vif.bss_conf.ht_operation_mode != ht_opmode) {
  167. changed |= BSS_CHANGED_HT;
  168. sdata->vif.bss_conf.ht_operation_mode = ht_opmode;
  169. sdata->ht_opmode_valid = true;
  170. }
  171. return changed;
  172. }
  173. /* frame sending functions */
  174. static void ieee80211_send_deauth_disassoc(struct ieee80211_sub_if_data *sdata,
  175. const u8 *bssid, u16 stype, u16 reason,
  176. void *cookie)
  177. {
  178. struct ieee80211_local *local = sdata->local;
  179. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  180. struct sk_buff *skb;
  181. struct ieee80211_mgmt *mgmt;
  182. skb = dev_alloc_skb(local->hw.extra_tx_headroom + sizeof(*mgmt));
  183. if (!skb) {
  184. printk(KERN_DEBUG "%s: failed to allocate buffer for "
  185. "deauth/disassoc frame\n", sdata->name);
  186. return;
  187. }
  188. skb_reserve(skb, local->hw.extra_tx_headroom);
  189. mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
  190. memset(mgmt, 0, 24);
  191. memcpy(mgmt->da, bssid, ETH_ALEN);
  192. memcpy(mgmt->sa, sdata->vif.addr, ETH_ALEN);
  193. memcpy(mgmt->bssid, bssid, ETH_ALEN);
  194. mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT | stype);
  195. skb_put(skb, 2);
  196. /* u.deauth.reason_code == u.disassoc.reason_code */
  197. mgmt->u.deauth.reason_code = cpu_to_le16(reason);
  198. if (stype == IEEE80211_STYPE_DEAUTH)
  199. if (cookie)
  200. __cfg80211_send_deauth(sdata->dev, (u8 *)mgmt, skb->len);
  201. else
  202. cfg80211_send_deauth(sdata->dev, (u8 *)mgmt, skb->len);
  203. else
  204. if (cookie)
  205. __cfg80211_send_disassoc(sdata->dev, (u8 *)mgmt, skb->len);
  206. else
  207. cfg80211_send_disassoc(sdata->dev, (u8 *)mgmt, skb->len);
  208. if (!(ifmgd->flags & IEEE80211_STA_MFP_ENABLED))
  209. IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
  210. ieee80211_tx_skb(sdata, skb);
  211. }
  212. void ieee80211_send_pspoll(struct ieee80211_local *local,
  213. struct ieee80211_sub_if_data *sdata)
  214. {
  215. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  216. struct ieee80211_pspoll *pspoll;
  217. struct sk_buff *skb;
  218. u16 fc;
  219. skb = dev_alloc_skb(local->hw.extra_tx_headroom + sizeof(*pspoll));
  220. if (!skb) {
  221. printk(KERN_DEBUG "%s: failed to allocate buffer for "
  222. "pspoll frame\n", sdata->name);
  223. return;
  224. }
  225. skb_reserve(skb, local->hw.extra_tx_headroom);
  226. pspoll = (struct ieee80211_pspoll *) skb_put(skb, sizeof(*pspoll));
  227. memset(pspoll, 0, sizeof(*pspoll));
  228. fc = IEEE80211_FTYPE_CTL | IEEE80211_STYPE_PSPOLL | IEEE80211_FCTL_PM;
  229. pspoll->frame_control = cpu_to_le16(fc);
  230. pspoll->aid = cpu_to_le16(ifmgd->aid);
  231. /* aid in PS-Poll has its two MSBs each set to 1 */
  232. pspoll->aid |= cpu_to_le16(1 << 15 | 1 << 14);
  233. memcpy(pspoll->bssid, ifmgd->bssid, ETH_ALEN);
  234. memcpy(pspoll->ta, sdata->vif.addr, ETH_ALEN);
  235. IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
  236. ieee80211_tx_skb(sdata, skb);
  237. }
  238. void ieee80211_send_nullfunc(struct ieee80211_local *local,
  239. struct ieee80211_sub_if_data *sdata,
  240. int powersave)
  241. {
  242. struct sk_buff *skb;
  243. struct ieee80211_hdr *nullfunc;
  244. __le16 fc;
  245. if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_STATION))
  246. return;
  247. skb = dev_alloc_skb(local->hw.extra_tx_headroom + 24);
  248. if (!skb) {
  249. printk(KERN_DEBUG "%s: failed to allocate buffer for nullfunc "
  250. "frame\n", sdata->name);
  251. return;
  252. }
  253. skb_reserve(skb, local->hw.extra_tx_headroom);
  254. nullfunc = (struct ieee80211_hdr *) skb_put(skb, 24);
  255. memset(nullfunc, 0, 24);
  256. fc = cpu_to_le16(IEEE80211_FTYPE_DATA | IEEE80211_STYPE_NULLFUNC |
  257. IEEE80211_FCTL_TODS);
  258. if (powersave)
  259. fc |= cpu_to_le16(IEEE80211_FCTL_PM);
  260. nullfunc->frame_control = fc;
  261. memcpy(nullfunc->addr1, sdata->u.mgd.bssid, ETH_ALEN);
  262. memcpy(nullfunc->addr2, sdata->vif.addr, ETH_ALEN);
  263. memcpy(nullfunc->addr3, sdata->u.mgd.bssid, ETH_ALEN);
  264. IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
  265. ieee80211_tx_skb(sdata, skb);
  266. }
  267. /* spectrum management related things */
  268. static void ieee80211_chswitch_work(struct work_struct *work)
  269. {
  270. struct ieee80211_sub_if_data *sdata =
  271. container_of(work, struct ieee80211_sub_if_data, u.mgd.chswitch_work);
  272. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  273. if (!ieee80211_sdata_running(sdata))
  274. return;
  275. mutex_lock(&ifmgd->mtx);
  276. if (!ifmgd->associated)
  277. goto out;
  278. sdata->local->oper_channel = sdata->local->csa_channel;
  279. ieee80211_hw_config(sdata->local, IEEE80211_CONF_CHANGE_CHANNEL);
  280. /* XXX: shouldn't really modify cfg80211-owned data! */
  281. ifmgd->associated->channel = sdata->local->oper_channel;
  282. ieee80211_wake_queues_by_reason(&sdata->local->hw,
  283. IEEE80211_QUEUE_STOP_REASON_CSA);
  284. out:
  285. ifmgd->flags &= ~IEEE80211_STA_CSA_RECEIVED;
  286. mutex_unlock(&ifmgd->mtx);
  287. }
  288. static void ieee80211_chswitch_timer(unsigned long data)
  289. {
  290. struct ieee80211_sub_if_data *sdata =
  291. (struct ieee80211_sub_if_data *) data;
  292. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  293. if (sdata->local->quiescing) {
  294. set_bit(TMR_RUNNING_CHANSW, &ifmgd->timers_running);
  295. return;
  296. }
  297. ieee80211_queue_work(&sdata->local->hw, &ifmgd->chswitch_work);
  298. }
  299. void ieee80211_sta_process_chanswitch(struct ieee80211_sub_if_data *sdata,
  300. struct ieee80211_channel_sw_ie *sw_elem,
  301. struct ieee80211_bss *bss)
  302. {
  303. struct cfg80211_bss *cbss =
  304. container_of((void *)bss, struct cfg80211_bss, priv);
  305. struct ieee80211_channel *new_ch;
  306. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  307. int new_freq = ieee80211_channel_to_frequency(sw_elem->new_ch_num);
  308. ASSERT_MGD_MTX(ifmgd);
  309. if (!ifmgd->associated)
  310. return;
  311. if (sdata->local->scanning)
  312. return;
  313. /* Disregard subsequent beacons if we are already running a timer
  314. processing a CSA */
  315. if (ifmgd->flags & IEEE80211_STA_CSA_RECEIVED)
  316. return;
  317. new_ch = ieee80211_get_channel(sdata->local->hw.wiphy, new_freq);
  318. if (!new_ch || new_ch->flags & IEEE80211_CHAN_DISABLED)
  319. return;
  320. sdata->local->csa_channel = new_ch;
  321. if (sw_elem->count <= 1) {
  322. ieee80211_queue_work(&sdata->local->hw, &ifmgd->chswitch_work);
  323. } else {
  324. ieee80211_stop_queues_by_reason(&sdata->local->hw,
  325. IEEE80211_QUEUE_STOP_REASON_CSA);
  326. ifmgd->flags |= IEEE80211_STA_CSA_RECEIVED;
  327. mod_timer(&ifmgd->chswitch_timer,
  328. jiffies +
  329. msecs_to_jiffies(sw_elem->count *
  330. cbss->beacon_interval));
  331. }
  332. }
  333. static void ieee80211_handle_pwr_constr(struct ieee80211_sub_if_data *sdata,
  334. u16 capab_info, u8 *pwr_constr_elem,
  335. u8 pwr_constr_elem_len)
  336. {
  337. struct ieee80211_conf *conf = &sdata->local->hw.conf;
  338. if (!(capab_info & WLAN_CAPABILITY_SPECTRUM_MGMT))
  339. return;
  340. /* Power constraint IE length should be 1 octet */
  341. if (pwr_constr_elem_len != 1)
  342. return;
  343. if ((*pwr_constr_elem <= conf->channel->max_power) &&
  344. (*pwr_constr_elem != sdata->local->power_constr_level)) {
  345. sdata->local->power_constr_level = *pwr_constr_elem;
  346. ieee80211_hw_config(sdata->local, 0);
  347. }
  348. }
  349. /* powersave */
  350. static void ieee80211_enable_ps(struct ieee80211_local *local,
  351. struct ieee80211_sub_if_data *sdata)
  352. {
  353. struct ieee80211_conf *conf = &local->hw.conf;
  354. /*
  355. * If we are scanning right now then the parameters will
  356. * take effect when scan finishes.
  357. */
  358. if (local->scanning)
  359. return;
  360. if (conf->dynamic_ps_timeout > 0 &&
  361. !(local->hw.flags & IEEE80211_HW_SUPPORTS_DYNAMIC_PS)) {
  362. mod_timer(&local->dynamic_ps_timer, jiffies +
  363. msecs_to_jiffies(conf->dynamic_ps_timeout));
  364. } else {
  365. if (local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK)
  366. ieee80211_send_nullfunc(local, sdata, 1);
  367. conf->flags |= IEEE80211_CONF_PS;
  368. ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
  369. }
  370. }
  371. static void ieee80211_change_ps(struct ieee80211_local *local)
  372. {
  373. struct ieee80211_conf *conf = &local->hw.conf;
  374. if (local->ps_sdata) {
  375. ieee80211_enable_ps(local, local->ps_sdata);
  376. } else if (conf->flags & IEEE80211_CONF_PS) {
  377. conf->flags &= ~IEEE80211_CONF_PS;
  378. ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
  379. del_timer_sync(&local->dynamic_ps_timer);
  380. cancel_work_sync(&local->dynamic_ps_enable_work);
  381. }
  382. }
  383. /* need to hold RTNL or interface lock */
  384. void ieee80211_recalc_ps(struct ieee80211_local *local, s32 latency)
  385. {
  386. struct ieee80211_sub_if_data *sdata, *found = NULL;
  387. int count = 0;
  388. if (!(local->hw.flags & IEEE80211_HW_SUPPORTS_PS)) {
  389. local->ps_sdata = NULL;
  390. return;
  391. }
  392. if (!list_empty(&local->work_list)) {
  393. local->ps_sdata = NULL;
  394. goto change;
  395. }
  396. list_for_each_entry(sdata, &local->interfaces, list) {
  397. if (!ieee80211_sdata_running(sdata))
  398. continue;
  399. if (sdata->vif.type != NL80211_IFTYPE_STATION)
  400. continue;
  401. found = sdata;
  402. count++;
  403. }
  404. if (count == 1 && found->u.mgd.powersave &&
  405. found->u.mgd.associated &&
  406. !(found->u.mgd.flags & (IEEE80211_STA_BEACON_POLL |
  407. IEEE80211_STA_CONNECTION_POLL))) {
  408. s32 beaconint_us;
  409. if (latency < 0)
  410. latency = pm_qos_requirement(PM_QOS_NETWORK_LATENCY);
  411. beaconint_us = ieee80211_tu_to_usec(
  412. found->vif.bss_conf.beacon_int);
  413. if (beaconint_us > latency) {
  414. local->ps_sdata = NULL;
  415. } else {
  416. u8 dtimper = found->vif.bss_conf.dtim_period;
  417. int maxslp = 1;
  418. if (dtimper > 1)
  419. maxslp = min_t(int, dtimper,
  420. latency / beaconint_us);
  421. local->hw.conf.max_sleep_period = maxslp;
  422. local->ps_sdata = found;
  423. }
  424. } else {
  425. local->ps_sdata = NULL;
  426. }
  427. change:
  428. ieee80211_change_ps(local);
  429. }
  430. void ieee80211_dynamic_ps_disable_work(struct work_struct *work)
  431. {
  432. struct ieee80211_local *local =
  433. container_of(work, struct ieee80211_local,
  434. dynamic_ps_disable_work);
  435. if (local->hw.conf.flags & IEEE80211_CONF_PS) {
  436. local->hw.conf.flags &= ~IEEE80211_CONF_PS;
  437. ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
  438. }
  439. ieee80211_wake_queues_by_reason(&local->hw,
  440. IEEE80211_QUEUE_STOP_REASON_PS);
  441. }
  442. void ieee80211_dynamic_ps_enable_work(struct work_struct *work)
  443. {
  444. struct ieee80211_local *local =
  445. container_of(work, struct ieee80211_local,
  446. dynamic_ps_enable_work);
  447. struct ieee80211_sub_if_data *sdata = local->ps_sdata;
  448. /* can only happen when PS was just disabled anyway */
  449. if (!sdata)
  450. return;
  451. if (local->hw.conf.flags & IEEE80211_CONF_PS)
  452. return;
  453. if (local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK)
  454. ieee80211_send_nullfunc(local, sdata, 1);
  455. local->hw.conf.flags |= IEEE80211_CONF_PS;
  456. ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
  457. }
  458. void ieee80211_dynamic_ps_timer(unsigned long data)
  459. {
  460. struct ieee80211_local *local = (void *) data;
  461. if (local->quiescing || local->suspended)
  462. return;
  463. ieee80211_queue_work(&local->hw, &local->dynamic_ps_enable_work);
  464. }
  465. /* MLME */
  466. static void ieee80211_sta_wmm_params(struct ieee80211_local *local,
  467. struct ieee80211_if_managed *ifmgd,
  468. u8 *wmm_param, size_t wmm_param_len)
  469. {
  470. struct ieee80211_tx_queue_params params;
  471. size_t left;
  472. int count;
  473. u8 *pos;
  474. if (local->hw.queues < 4)
  475. return;
  476. if (!wmm_param)
  477. return;
  478. if (wmm_param_len < 8 || wmm_param[5] /* version */ != 1)
  479. return;
  480. count = wmm_param[6] & 0x0f;
  481. if (count == ifmgd->wmm_last_param_set)
  482. return;
  483. ifmgd->wmm_last_param_set = count;
  484. pos = wmm_param + 8;
  485. left = wmm_param_len - 8;
  486. memset(&params, 0, sizeof(params));
  487. local->wmm_acm = 0;
  488. for (; left >= 4; left -= 4, pos += 4) {
  489. int aci = (pos[0] >> 5) & 0x03;
  490. int acm = (pos[0] >> 4) & 0x01;
  491. int queue;
  492. switch (aci) {
  493. case 1: /* AC_BK */
  494. queue = 3;
  495. if (acm)
  496. local->wmm_acm |= BIT(1) | BIT(2); /* BK/- */
  497. break;
  498. case 2: /* AC_VI */
  499. queue = 1;
  500. if (acm)
  501. local->wmm_acm |= BIT(4) | BIT(5); /* CL/VI */
  502. break;
  503. case 3: /* AC_VO */
  504. queue = 0;
  505. if (acm)
  506. local->wmm_acm |= BIT(6) | BIT(7); /* VO/NC */
  507. break;
  508. case 0: /* AC_BE */
  509. default:
  510. queue = 2;
  511. if (acm)
  512. local->wmm_acm |= BIT(0) | BIT(3); /* BE/EE */
  513. break;
  514. }
  515. params.aifs = pos[0] & 0x0f;
  516. params.cw_max = ecw2cw((pos[1] & 0xf0) >> 4);
  517. params.cw_min = ecw2cw(pos[1] & 0x0f);
  518. params.txop = get_unaligned_le16(pos + 2);
  519. #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
  520. printk(KERN_DEBUG "%s: WMM queue=%d aci=%d acm=%d aifs=%d "
  521. "cWmin=%d cWmax=%d txop=%d\n",
  522. wiphy_name(local->hw.wiphy), queue, aci, acm,
  523. params.aifs, params.cw_min, params.cw_max, params.txop);
  524. #endif
  525. if (drv_conf_tx(local, queue, &params) && local->ops->conf_tx)
  526. printk(KERN_DEBUG "%s: failed to set TX queue "
  527. "parameters for queue %d\n",
  528. wiphy_name(local->hw.wiphy), queue);
  529. }
  530. }
  531. static u32 ieee80211_handle_bss_capability(struct ieee80211_sub_if_data *sdata,
  532. u16 capab, bool erp_valid, u8 erp)
  533. {
  534. struct ieee80211_bss_conf *bss_conf = &sdata->vif.bss_conf;
  535. u32 changed = 0;
  536. bool use_protection;
  537. bool use_short_preamble;
  538. bool use_short_slot;
  539. if (erp_valid) {
  540. use_protection = (erp & WLAN_ERP_USE_PROTECTION) != 0;
  541. use_short_preamble = (erp & WLAN_ERP_BARKER_PREAMBLE) == 0;
  542. } else {
  543. use_protection = false;
  544. use_short_preamble = !!(capab & WLAN_CAPABILITY_SHORT_PREAMBLE);
  545. }
  546. use_short_slot = !!(capab & WLAN_CAPABILITY_SHORT_SLOT_TIME);
  547. if (use_protection != bss_conf->use_cts_prot) {
  548. bss_conf->use_cts_prot = use_protection;
  549. changed |= BSS_CHANGED_ERP_CTS_PROT;
  550. }
  551. if (use_short_preamble != bss_conf->use_short_preamble) {
  552. bss_conf->use_short_preamble = use_short_preamble;
  553. changed |= BSS_CHANGED_ERP_PREAMBLE;
  554. }
  555. if (use_short_slot != bss_conf->use_short_slot) {
  556. bss_conf->use_short_slot = use_short_slot;
  557. changed |= BSS_CHANGED_ERP_SLOT;
  558. }
  559. return changed;
  560. }
  561. static void ieee80211_set_associated(struct ieee80211_sub_if_data *sdata,
  562. struct cfg80211_bss *cbss,
  563. u32 bss_info_changed)
  564. {
  565. struct ieee80211_bss *bss = (void *)cbss->priv;
  566. struct ieee80211_local *local = sdata->local;
  567. bss_info_changed |= BSS_CHANGED_ASSOC;
  568. /* set timing information */
  569. sdata->vif.bss_conf.beacon_int = cbss->beacon_interval;
  570. sdata->vif.bss_conf.timestamp = cbss->tsf;
  571. sdata->vif.bss_conf.dtim_period = bss->dtim_period;
  572. bss_info_changed |= BSS_CHANGED_BEACON_INT;
  573. bss_info_changed |= ieee80211_handle_bss_capability(sdata,
  574. cbss->capability, bss->has_erp_value, bss->erp_value);
  575. sdata->u.mgd.associated = cbss;
  576. memcpy(sdata->u.mgd.bssid, cbss->bssid, ETH_ALEN);
  577. /* just to be sure */
  578. sdata->u.mgd.flags &= ~(IEEE80211_STA_CONNECTION_POLL |
  579. IEEE80211_STA_BEACON_POLL);
  580. /*
  581. * Always handle WMM once after association regardless
  582. * of the first value the AP uses. Setting -1 here has
  583. * that effect because the AP values is an unsigned
  584. * 4-bit value.
  585. */
  586. sdata->u.mgd.wmm_last_param_set = -1;
  587. ieee80211_led_assoc(local, 1);
  588. sdata->vif.bss_conf.assoc = 1;
  589. /*
  590. * For now just always ask the driver to update the basic rateset
  591. * when we have associated, we aren't checking whether it actually
  592. * changed or not.
  593. */
  594. bss_info_changed |= BSS_CHANGED_BASIC_RATES;
  595. /* And the BSSID changed - we're associated now */
  596. bss_info_changed |= BSS_CHANGED_BSSID;
  597. ieee80211_bss_info_change_notify(sdata, bss_info_changed);
  598. mutex_lock(&local->iflist_mtx);
  599. ieee80211_recalc_ps(local, -1);
  600. ieee80211_recalc_smps(local, sdata);
  601. mutex_unlock(&local->iflist_mtx);
  602. netif_start_queue(sdata->dev);
  603. netif_carrier_on(sdata->dev);
  604. }
  605. static void ieee80211_set_disassoc(struct ieee80211_sub_if_data *sdata)
  606. {
  607. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  608. struct ieee80211_local *local = sdata->local;
  609. struct sta_info *sta;
  610. u32 changed = 0, config_changed = 0;
  611. u8 bssid[ETH_ALEN];
  612. ASSERT_MGD_MTX(ifmgd);
  613. if (WARN_ON(!ifmgd->associated))
  614. return;
  615. memcpy(bssid, ifmgd->associated->bssid, ETH_ALEN);
  616. ifmgd->associated = NULL;
  617. memset(ifmgd->bssid, 0, ETH_ALEN);
  618. /*
  619. * we need to commit the associated = NULL change because the
  620. * scan code uses that to determine whether this iface should
  621. * go to/wake up from powersave or not -- and could otherwise
  622. * wake the queues erroneously.
  623. */
  624. smp_mb();
  625. /*
  626. * Thus, we can only afterwards stop the queues -- to account
  627. * for the case where another CPU is finishing a scan at this
  628. * time -- we don't want the scan code to enable queues.
  629. */
  630. netif_stop_queue(sdata->dev);
  631. netif_carrier_off(sdata->dev);
  632. rcu_read_lock();
  633. sta = sta_info_get(sdata, bssid);
  634. if (sta)
  635. ieee80211_sta_tear_down_BA_sessions(sta);
  636. rcu_read_unlock();
  637. changed |= ieee80211_reset_erp_info(sdata);
  638. ieee80211_led_assoc(local, 0);
  639. changed |= BSS_CHANGED_ASSOC;
  640. sdata->vif.bss_conf.assoc = false;
  641. ieee80211_set_wmm_default(sdata);
  642. /* channel(_type) changes are handled by ieee80211_hw_config */
  643. local->oper_channel_type = NL80211_CHAN_NO_HT;
  644. /* on the next assoc, re-program HT parameters */
  645. sdata->ht_opmode_valid = false;
  646. local->power_constr_level = 0;
  647. del_timer_sync(&local->dynamic_ps_timer);
  648. cancel_work_sync(&local->dynamic_ps_enable_work);
  649. if (local->hw.conf.flags & IEEE80211_CONF_PS) {
  650. local->hw.conf.flags &= ~IEEE80211_CONF_PS;
  651. config_changed |= IEEE80211_CONF_CHANGE_PS;
  652. }
  653. ieee80211_hw_config(local, config_changed);
  654. /* And the BSSID changed -- not very interesting here */
  655. changed |= BSS_CHANGED_BSSID;
  656. ieee80211_bss_info_change_notify(sdata, changed);
  657. rcu_read_lock();
  658. sta = sta_info_get(sdata, bssid);
  659. if (!sta) {
  660. rcu_read_unlock();
  661. return;
  662. }
  663. sta_info_unlink(&sta);
  664. rcu_read_unlock();
  665. sta_info_destroy(sta);
  666. }
  667. void ieee80211_sta_rx_notify(struct ieee80211_sub_if_data *sdata,
  668. struct ieee80211_hdr *hdr)
  669. {
  670. /*
  671. * We can postpone the mgd.timer whenever receiving unicast frames
  672. * from AP because we know that the connection is working both ways
  673. * at that time. But multicast frames (and hence also beacons) must
  674. * be ignored here, because we need to trigger the timer during
  675. * data idle periods for sending the periodic probe request to the
  676. * AP we're connected to.
  677. */
  678. if (is_multicast_ether_addr(hdr->addr1))
  679. return;
  680. mod_timer(&sdata->u.mgd.conn_mon_timer,
  681. round_jiffies_up(jiffies + IEEE80211_CONNECTION_IDLE_TIME));
  682. }
  683. static void ieee80211_mgd_probe_ap_send(struct ieee80211_sub_if_data *sdata)
  684. {
  685. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  686. const u8 *ssid;
  687. ssid = ieee80211_bss_get_ie(ifmgd->associated, WLAN_EID_SSID);
  688. ieee80211_send_probe_req(sdata, ifmgd->associated->bssid,
  689. ssid + 2, ssid[1], NULL, 0);
  690. ifmgd->probe_send_count++;
  691. ifmgd->probe_timeout = jiffies + IEEE80211_PROBE_WAIT;
  692. run_again(ifmgd, ifmgd->probe_timeout);
  693. }
  694. static void ieee80211_mgd_probe_ap(struct ieee80211_sub_if_data *sdata,
  695. bool beacon)
  696. {
  697. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  698. bool already = false;
  699. if (!ieee80211_sdata_running(sdata))
  700. return;
  701. if (sdata->local->scanning)
  702. return;
  703. mutex_lock(&ifmgd->mtx);
  704. if (!ifmgd->associated)
  705. goto out;
  706. #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
  707. if (beacon && net_ratelimit())
  708. printk(KERN_DEBUG "%s: detected beacon loss from AP "
  709. "- sending probe request\n", sdata->name);
  710. #endif
  711. /*
  712. * The driver/our work has already reported this event or the
  713. * connection monitoring has kicked in and we have already sent
  714. * a probe request. Or maybe the AP died and the driver keeps
  715. * reporting until we disassociate...
  716. *
  717. * In either case we have to ignore the current call to this
  718. * function (except for setting the correct probe reason bit)
  719. * because otherwise we would reset the timer every time and
  720. * never check whether we received a probe response!
  721. */
  722. if (ifmgd->flags & (IEEE80211_STA_BEACON_POLL |
  723. IEEE80211_STA_CONNECTION_POLL))
  724. already = true;
  725. if (beacon)
  726. ifmgd->flags |= IEEE80211_STA_BEACON_POLL;
  727. else
  728. ifmgd->flags |= IEEE80211_STA_CONNECTION_POLL;
  729. if (already)
  730. goto out;
  731. mutex_lock(&sdata->local->iflist_mtx);
  732. ieee80211_recalc_ps(sdata->local, -1);
  733. mutex_unlock(&sdata->local->iflist_mtx);
  734. ifmgd->probe_send_count = 0;
  735. ieee80211_mgd_probe_ap_send(sdata);
  736. out:
  737. mutex_unlock(&ifmgd->mtx);
  738. }
  739. void ieee80211_beacon_loss_work(struct work_struct *work)
  740. {
  741. struct ieee80211_sub_if_data *sdata =
  742. container_of(work, struct ieee80211_sub_if_data,
  743. u.mgd.beacon_loss_work);
  744. ieee80211_mgd_probe_ap(sdata, true);
  745. }
  746. void ieee80211_beacon_loss(struct ieee80211_vif *vif)
  747. {
  748. struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
  749. ieee80211_queue_work(&sdata->local->hw, &sdata->u.mgd.beacon_loss_work);
  750. }
  751. EXPORT_SYMBOL(ieee80211_beacon_loss);
  752. static enum rx_mgmt_action __must_check
  753. ieee80211_rx_mgmt_deauth(struct ieee80211_sub_if_data *sdata,
  754. struct ieee80211_mgmt *mgmt, size_t len)
  755. {
  756. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  757. const u8 *bssid = NULL;
  758. u16 reason_code;
  759. if (len < 24 + 2)
  760. return RX_MGMT_NONE;
  761. ASSERT_MGD_MTX(ifmgd);
  762. bssid = ifmgd->associated->bssid;
  763. reason_code = le16_to_cpu(mgmt->u.deauth.reason_code);
  764. printk(KERN_DEBUG "%s: deauthenticated from %pM (Reason: %u)\n",
  765. sdata->name, bssid, reason_code);
  766. ieee80211_set_disassoc(sdata);
  767. ieee80211_recalc_idle(sdata->local);
  768. return RX_MGMT_CFG80211_DEAUTH;
  769. }
  770. static enum rx_mgmt_action __must_check
  771. ieee80211_rx_mgmt_disassoc(struct ieee80211_sub_if_data *sdata,
  772. struct ieee80211_mgmt *mgmt, size_t len)
  773. {
  774. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  775. u16 reason_code;
  776. if (len < 24 + 2)
  777. return RX_MGMT_NONE;
  778. ASSERT_MGD_MTX(ifmgd);
  779. if (WARN_ON(!ifmgd->associated))
  780. return RX_MGMT_NONE;
  781. if (WARN_ON(memcmp(ifmgd->associated->bssid, mgmt->sa, ETH_ALEN)))
  782. return RX_MGMT_NONE;
  783. reason_code = le16_to_cpu(mgmt->u.disassoc.reason_code);
  784. printk(KERN_DEBUG "%s: disassociated from %pM (Reason: %u)\n",
  785. sdata->name, mgmt->sa, reason_code);
  786. ieee80211_set_disassoc(sdata);
  787. ieee80211_recalc_idle(sdata->local);
  788. return RX_MGMT_CFG80211_DISASSOC;
  789. }
  790. static bool ieee80211_assoc_success(struct ieee80211_work *wk,
  791. struct ieee80211_mgmt *mgmt, size_t len)
  792. {
  793. struct ieee80211_sub_if_data *sdata = wk->sdata;
  794. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  795. struct ieee80211_local *local = sdata->local;
  796. struct ieee80211_supported_band *sband;
  797. struct sta_info *sta;
  798. struct cfg80211_bss *cbss = wk->assoc.bss;
  799. u8 *pos;
  800. u32 rates, basic_rates;
  801. u16 capab_info, aid;
  802. struct ieee802_11_elems elems;
  803. struct ieee80211_bss_conf *bss_conf = &sdata->vif.bss_conf;
  804. u32 changed = 0;
  805. int i, j, err;
  806. bool have_higher_than_11mbit = false;
  807. u16 ap_ht_cap_flags;
  808. /* AssocResp and ReassocResp have identical structure */
  809. aid = le16_to_cpu(mgmt->u.assoc_resp.aid);
  810. capab_info = le16_to_cpu(mgmt->u.assoc_resp.capab_info);
  811. if ((aid & (BIT(15) | BIT(14))) != (BIT(15) | BIT(14)))
  812. printk(KERN_DEBUG "%s: invalid aid value %d; bits 15:14 not "
  813. "set\n", sdata->name, aid);
  814. aid &= ~(BIT(15) | BIT(14));
  815. pos = mgmt->u.assoc_resp.variable;
  816. ieee802_11_parse_elems(pos, len - (pos - (u8 *) mgmt), &elems);
  817. if (!elems.supp_rates) {
  818. printk(KERN_DEBUG "%s: no SuppRates element in AssocResp\n",
  819. sdata->name);
  820. return false;
  821. }
  822. ifmgd->aid = aid;
  823. sta = sta_info_alloc(sdata, cbss->bssid, GFP_KERNEL);
  824. if (!sta) {
  825. printk(KERN_DEBUG "%s: failed to alloc STA entry for"
  826. " the AP\n", sdata->name);
  827. return false;
  828. }
  829. set_sta_flags(sta, WLAN_STA_AUTH | WLAN_STA_ASSOC |
  830. WLAN_STA_ASSOC_AP);
  831. if (!(ifmgd->flags & IEEE80211_STA_CONTROL_PORT))
  832. set_sta_flags(sta, WLAN_STA_AUTHORIZED);
  833. rates = 0;
  834. basic_rates = 0;
  835. sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
  836. for (i = 0; i < elems.supp_rates_len; i++) {
  837. int rate = (elems.supp_rates[i] & 0x7f) * 5;
  838. bool is_basic = !!(elems.supp_rates[i] & 0x80);
  839. if (rate > 110)
  840. have_higher_than_11mbit = true;
  841. for (j = 0; j < sband->n_bitrates; j++) {
  842. if (sband->bitrates[j].bitrate == rate) {
  843. rates |= BIT(j);
  844. if (is_basic)
  845. basic_rates |= BIT(j);
  846. break;
  847. }
  848. }
  849. }
  850. for (i = 0; i < elems.ext_supp_rates_len; i++) {
  851. int rate = (elems.ext_supp_rates[i] & 0x7f) * 5;
  852. bool is_basic = !!(elems.ext_supp_rates[i] & 0x80);
  853. if (rate > 110)
  854. have_higher_than_11mbit = true;
  855. for (j = 0; j < sband->n_bitrates; j++) {
  856. if (sband->bitrates[j].bitrate == rate) {
  857. rates |= BIT(j);
  858. if (is_basic)
  859. basic_rates |= BIT(j);
  860. break;
  861. }
  862. }
  863. }
  864. sta->sta.supp_rates[local->hw.conf.channel->band] = rates;
  865. sdata->vif.bss_conf.basic_rates = basic_rates;
  866. /* cf. IEEE 802.11 9.2.12 */
  867. if (local->hw.conf.channel->band == IEEE80211_BAND_2GHZ &&
  868. have_higher_than_11mbit)
  869. sdata->flags |= IEEE80211_SDATA_OPERATING_GMODE;
  870. else
  871. sdata->flags &= ~IEEE80211_SDATA_OPERATING_GMODE;
  872. if (elems.ht_cap_elem && !(ifmgd->flags & IEEE80211_STA_DISABLE_11N))
  873. ieee80211_ht_cap_ie_to_sta_ht_cap(sband,
  874. elems.ht_cap_elem, &sta->sta.ht_cap);
  875. ap_ht_cap_flags = sta->sta.ht_cap.cap;
  876. rate_control_rate_init(sta);
  877. if (ifmgd->flags & IEEE80211_STA_MFP_ENABLED)
  878. set_sta_flags(sta, WLAN_STA_MFP);
  879. if (elems.wmm_param)
  880. set_sta_flags(sta, WLAN_STA_WME);
  881. err = sta_info_insert(sta);
  882. sta = NULL;
  883. if (err) {
  884. printk(KERN_DEBUG "%s: failed to insert STA entry for"
  885. " the AP (error %d)\n", sdata->name, err);
  886. return RX_MGMT_CFG80211_ASSOC_ERROR;
  887. }
  888. if (elems.wmm_param)
  889. ieee80211_sta_wmm_params(local, ifmgd, elems.wmm_param,
  890. elems.wmm_param_len);
  891. else
  892. ieee80211_set_wmm_default(sdata);
  893. if (elems.ht_info_elem && elems.wmm_param &&
  894. (sdata->local->hw.queues >= 4) &&
  895. !(ifmgd->flags & IEEE80211_STA_DISABLE_11N))
  896. changed |= ieee80211_enable_ht(sdata, elems.ht_info_elem,
  897. cbss->bssid, ap_ht_cap_flags);
  898. /* set AID and assoc capability,
  899. * ieee80211_set_associated() will tell the driver */
  900. bss_conf->aid = aid;
  901. bss_conf->assoc_capability = capab_info;
  902. ieee80211_set_associated(sdata, cbss, changed);
  903. /*
  904. * Start timer to probe the connection to the AP now.
  905. * Also start the timer that will detect beacon loss.
  906. */
  907. ieee80211_sta_rx_notify(sdata, (struct ieee80211_hdr *)mgmt);
  908. mod_beacon_timer(sdata);
  909. return true;
  910. }
  911. static void ieee80211_rx_bss_info(struct ieee80211_sub_if_data *sdata,
  912. struct ieee80211_mgmt *mgmt,
  913. size_t len,
  914. struct ieee80211_rx_status *rx_status,
  915. struct ieee802_11_elems *elems,
  916. bool beacon)
  917. {
  918. struct ieee80211_local *local = sdata->local;
  919. int freq;
  920. struct ieee80211_bss *bss;
  921. struct ieee80211_channel *channel;
  922. if (elems->ds_params && elems->ds_params_len == 1)
  923. freq = ieee80211_channel_to_frequency(elems->ds_params[0]);
  924. else
  925. freq = rx_status->freq;
  926. channel = ieee80211_get_channel(local->hw.wiphy, freq);
  927. if (!channel || channel->flags & IEEE80211_CHAN_DISABLED)
  928. return;
  929. bss = ieee80211_bss_info_update(local, rx_status, mgmt, len, elems,
  930. channel, beacon);
  931. if (bss)
  932. ieee80211_rx_bss_put(local, bss);
  933. if (!sdata->u.mgd.associated)
  934. return;
  935. if (elems->ch_switch_elem && (elems->ch_switch_elem_len == 3) &&
  936. (memcmp(mgmt->bssid, sdata->u.mgd.associated->bssid,
  937. ETH_ALEN) == 0)) {
  938. struct ieee80211_channel_sw_ie *sw_elem =
  939. (struct ieee80211_channel_sw_ie *)elems->ch_switch_elem;
  940. ieee80211_sta_process_chanswitch(sdata, sw_elem, bss);
  941. }
  942. }
  943. static void ieee80211_rx_mgmt_probe_resp(struct ieee80211_sub_if_data *sdata,
  944. struct sk_buff *skb)
  945. {
  946. struct ieee80211_mgmt *mgmt = (void *)skb->data;
  947. struct ieee80211_if_managed *ifmgd;
  948. struct ieee80211_rx_status *rx_status = (void *) skb->cb;
  949. size_t baselen, len = skb->len;
  950. struct ieee802_11_elems elems;
  951. ifmgd = &sdata->u.mgd;
  952. ASSERT_MGD_MTX(ifmgd);
  953. if (memcmp(mgmt->da, sdata->vif.addr, ETH_ALEN))
  954. return; /* ignore ProbeResp to foreign address */
  955. baselen = (u8 *) mgmt->u.probe_resp.variable - (u8 *) mgmt;
  956. if (baselen > len)
  957. return;
  958. ieee802_11_parse_elems(mgmt->u.probe_resp.variable, len - baselen,
  959. &elems);
  960. ieee80211_rx_bss_info(sdata, mgmt, len, rx_status, &elems, false);
  961. if (ifmgd->associated &&
  962. memcmp(mgmt->bssid, ifmgd->associated->bssid, ETH_ALEN) == 0 &&
  963. ifmgd->flags & (IEEE80211_STA_BEACON_POLL |
  964. IEEE80211_STA_CONNECTION_POLL)) {
  965. ifmgd->flags &= ~(IEEE80211_STA_CONNECTION_POLL |
  966. IEEE80211_STA_BEACON_POLL);
  967. mutex_lock(&sdata->local->iflist_mtx);
  968. ieee80211_recalc_ps(sdata->local, -1);
  969. mutex_unlock(&sdata->local->iflist_mtx);
  970. /*
  971. * We've received a probe response, but are not sure whether
  972. * we have or will be receiving any beacons or data, so let's
  973. * schedule the timers again, just in case.
  974. */
  975. mod_beacon_timer(sdata);
  976. mod_timer(&ifmgd->conn_mon_timer,
  977. round_jiffies_up(jiffies +
  978. IEEE80211_CONNECTION_IDLE_TIME));
  979. }
  980. }
  981. /*
  982. * This is the canonical list of information elements we care about,
  983. * the filter code also gives us all changes to the Microsoft OUI
  984. * (00:50:F2) vendor IE which is used for WMM which we need to track.
  985. *
  986. * We implement beacon filtering in software since that means we can
  987. * avoid processing the frame here and in cfg80211, and userspace
  988. * will not be able to tell whether the hardware supports it or not.
  989. *
  990. * XXX: This list needs to be dynamic -- userspace needs to be able to
  991. * add items it requires. It also needs to be able to tell us to
  992. * look out for other vendor IEs.
  993. */
  994. static const u64 care_about_ies =
  995. (1ULL << WLAN_EID_COUNTRY) |
  996. (1ULL << WLAN_EID_ERP_INFO) |
  997. (1ULL << WLAN_EID_CHANNEL_SWITCH) |
  998. (1ULL << WLAN_EID_PWR_CONSTRAINT) |
  999. (1ULL << WLAN_EID_HT_CAPABILITY) |
  1000. (1ULL << WLAN_EID_HT_INFORMATION);
  1001. static void ieee80211_rx_mgmt_beacon(struct ieee80211_sub_if_data *sdata,
  1002. struct ieee80211_mgmt *mgmt,
  1003. size_t len,
  1004. struct ieee80211_rx_status *rx_status)
  1005. {
  1006. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  1007. size_t baselen;
  1008. struct ieee802_11_elems elems;
  1009. struct ieee80211_local *local = sdata->local;
  1010. u32 changed = 0;
  1011. bool erp_valid, directed_tim = false;
  1012. u8 erp_value = 0;
  1013. u32 ncrc;
  1014. u8 *bssid;
  1015. ASSERT_MGD_MTX(ifmgd);
  1016. /* Process beacon from the current BSS */
  1017. baselen = (u8 *) mgmt->u.beacon.variable - (u8 *) mgmt;
  1018. if (baselen > len)
  1019. return;
  1020. if (rx_status->freq != local->hw.conf.channel->center_freq)
  1021. return;
  1022. /*
  1023. * We might have received a number of frames, among them a
  1024. * disassoc frame and a beacon...
  1025. */
  1026. if (!ifmgd->associated)
  1027. return;
  1028. bssid = ifmgd->associated->bssid;
  1029. /*
  1030. * And in theory even frames from a different AP we were just
  1031. * associated to a split-second ago!
  1032. */
  1033. if (memcmp(bssid, mgmt->bssid, ETH_ALEN) != 0)
  1034. return;
  1035. if (ifmgd->flags & IEEE80211_STA_BEACON_POLL) {
  1036. #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
  1037. if (net_ratelimit()) {
  1038. printk(KERN_DEBUG "%s: cancelling probereq poll due "
  1039. "to a received beacon\n", sdata->name);
  1040. }
  1041. #endif
  1042. ifmgd->flags &= ~IEEE80211_STA_BEACON_POLL;
  1043. mutex_lock(&local->iflist_mtx);
  1044. ieee80211_recalc_ps(local, -1);
  1045. mutex_unlock(&local->iflist_mtx);
  1046. }
  1047. /*
  1048. * Push the beacon loss detection into the future since
  1049. * we are processing a beacon from the AP just now.
  1050. */
  1051. mod_beacon_timer(sdata);
  1052. ncrc = crc32_be(0, (void *)&mgmt->u.beacon.beacon_int, 4);
  1053. ncrc = ieee802_11_parse_elems_crc(mgmt->u.beacon.variable,
  1054. len - baselen, &elems,
  1055. care_about_ies, ncrc);
  1056. if (local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK)
  1057. directed_tim = ieee80211_check_tim(elems.tim, elems.tim_len,
  1058. ifmgd->aid);
  1059. if (ncrc != ifmgd->beacon_crc) {
  1060. ieee80211_rx_bss_info(sdata, mgmt, len, rx_status, &elems,
  1061. true);
  1062. ieee80211_sta_wmm_params(local, ifmgd, elems.wmm_param,
  1063. elems.wmm_param_len);
  1064. }
  1065. if (local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK) {
  1066. if (directed_tim) {
  1067. if (local->hw.conf.dynamic_ps_timeout > 0) {
  1068. local->hw.conf.flags &= ~IEEE80211_CONF_PS;
  1069. ieee80211_hw_config(local,
  1070. IEEE80211_CONF_CHANGE_PS);
  1071. ieee80211_send_nullfunc(local, sdata, 0);
  1072. } else {
  1073. local->pspolling = true;
  1074. /*
  1075. * Here is assumed that the driver will be
  1076. * able to send ps-poll frame and receive a
  1077. * response even though power save mode is
  1078. * enabled, but some drivers might require
  1079. * to disable power save here. This needs
  1080. * to be investigated.
  1081. */
  1082. ieee80211_send_pspoll(local, sdata);
  1083. }
  1084. }
  1085. }
  1086. if (ncrc == ifmgd->beacon_crc)
  1087. return;
  1088. ifmgd->beacon_crc = ncrc;
  1089. if (elems.erp_info && elems.erp_info_len >= 1) {
  1090. erp_valid = true;
  1091. erp_value = elems.erp_info[0];
  1092. } else {
  1093. erp_valid = false;
  1094. }
  1095. changed |= ieee80211_handle_bss_capability(sdata,
  1096. le16_to_cpu(mgmt->u.beacon.capab_info),
  1097. erp_valid, erp_value);
  1098. if (elems.ht_cap_elem && elems.ht_info_elem && elems.wmm_param &&
  1099. !(ifmgd->flags & IEEE80211_STA_DISABLE_11N)) {
  1100. struct sta_info *sta;
  1101. struct ieee80211_supported_band *sband;
  1102. u16 ap_ht_cap_flags;
  1103. rcu_read_lock();
  1104. sta = sta_info_get(sdata, bssid);
  1105. if (WARN_ON(!sta)) {
  1106. rcu_read_unlock();
  1107. return;
  1108. }
  1109. sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
  1110. ieee80211_ht_cap_ie_to_sta_ht_cap(sband,
  1111. elems.ht_cap_elem, &sta->sta.ht_cap);
  1112. ap_ht_cap_flags = sta->sta.ht_cap.cap;
  1113. rcu_read_unlock();
  1114. changed |= ieee80211_enable_ht(sdata, elems.ht_info_elem,
  1115. bssid, ap_ht_cap_flags);
  1116. }
  1117. /* Note: country IE parsing is done for us by cfg80211 */
  1118. if (elems.country_elem) {
  1119. /* TODO: IBSS also needs this */
  1120. if (elems.pwr_constr_elem)
  1121. ieee80211_handle_pwr_constr(sdata,
  1122. le16_to_cpu(mgmt->u.probe_resp.capab_info),
  1123. elems.pwr_constr_elem,
  1124. elems.pwr_constr_elem_len);
  1125. }
  1126. ieee80211_bss_info_change_notify(sdata, changed);
  1127. }
  1128. ieee80211_rx_result ieee80211_sta_rx_mgmt(struct ieee80211_sub_if_data *sdata,
  1129. struct sk_buff *skb)
  1130. {
  1131. struct ieee80211_local *local = sdata->local;
  1132. struct ieee80211_mgmt *mgmt;
  1133. u16 fc;
  1134. if (skb->len < 24)
  1135. return RX_DROP_MONITOR;
  1136. mgmt = (struct ieee80211_mgmt *) skb->data;
  1137. fc = le16_to_cpu(mgmt->frame_control);
  1138. switch (fc & IEEE80211_FCTL_STYPE) {
  1139. case IEEE80211_STYPE_PROBE_RESP:
  1140. case IEEE80211_STYPE_BEACON:
  1141. case IEEE80211_STYPE_DEAUTH:
  1142. case IEEE80211_STYPE_DISASSOC:
  1143. case IEEE80211_STYPE_ACTION:
  1144. skb_queue_tail(&sdata->u.mgd.skb_queue, skb);
  1145. ieee80211_queue_work(&local->hw, &sdata->u.mgd.work);
  1146. return RX_QUEUED;
  1147. }
  1148. return RX_DROP_MONITOR;
  1149. }
  1150. static void ieee80211_sta_rx_queued_mgmt(struct ieee80211_sub_if_data *sdata,
  1151. struct sk_buff *skb)
  1152. {
  1153. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  1154. struct ieee80211_rx_status *rx_status;
  1155. struct ieee80211_mgmt *mgmt;
  1156. enum rx_mgmt_action rma = RX_MGMT_NONE;
  1157. u16 fc;
  1158. rx_status = (struct ieee80211_rx_status *) skb->cb;
  1159. mgmt = (struct ieee80211_mgmt *) skb->data;
  1160. fc = le16_to_cpu(mgmt->frame_control);
  1161. mutex_lock(&ifmgd->mtx);
  1162. if (ifmgd->associated &&
  1163. memcmp(ifmgd->associated->bssid, mgmt->bssid, ETH_ALEN) == 0) {
  1164. switch (fc & IEEE80211_FCTL_STYPE) {
  1165. case IEEE80211_STYPE_BEACON:
  1166. ieee80211_rx_mgmt_beacon(sdata, mgmt, skb->len,
  1167. rx_status);
  1168. break;
  1169. case IEEE80211_STYPE_PROBE_RESP:
  1170. ieee80211_rx_mgmt_probe_resp(sdata, skb);
  1171. break;
  1172. case IEEE80211_STYPE_DEAUTH:
  1173. rma = ieee80211_rx_mgmt_deauth(sdata, mgmt, skb->len);
  1174. break;
  1175. case IEEE80211_STYPE_DISASSOC:
  1176. rma = ieee80211_rx_mgmt_disassoc(sdata, mgmt, skb->len);
  1177. break;
  1178. case IEEE80211_STYPE_ACTION:
  1179. /* XXX: differentiate, can only happen for CSA now! */
  1180. ieee80211_sta_process_chanswitch(sdata,
  1181. &mgmt->u.action.u.chan_switch.sw_elem,
  1182. (void *)ifmgd->associated->priv);
  1183. break;
  1184. }
  1185. mutex_unlock(&ifmgd->mtx);
  1186. switch (rma) {
  1187. case RX_MGMT_NONE:
  1188. /* no action */
  1189. break;
  1190. case RX_MGMT_CFG80211_DEAUTH:
  1191. cfg80211_send_deauth(sdata->dev, (u8 *)mgmt, skb->len);
  1192. break;
  1193. case RX_MGMT_CFG80211_DISASSOC:
  1194. cfg80211_send_disassoc(sdata->dev, (u8 *)mgmt, skb->len);
  1195. break;
  1196. default:
  1197. WARN(1, "unexpected: %d", rma);
  1198. }
  1199. goto out;
  1200. }
  1201. mutex_unlock(&ifmgd->mtx);
  1202. if (skb->len >= 24 + 2 /* mgmt + deauth reason */ &&
  1203. (fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_DEAUTH)
  1204. cfg80211_send_deauth(sdata->dev, (u8 *)mgmt, skb->len);
  1205. out:
  1206. kfree_skb(skb);
  1207. }
  1208. static void ieee80211_sta_timer(unsigned long data)
  1209. {
  1210. struct ieee80211_sub_if_data *sdata =
  1211. (struct ieee80211_sub_if_data *) data;
  1212. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  1213. struct ieee80211_local *local = sdata->local;
  1214. if (local->quiescing) {
  1215. set_bit(TMR_RUNNING_TIMER, &ifmgd->timers_running);
  1216. return;
  1217. }
  1218. ieee80211_queue_work(&local->hw, &ifmgd->work);
  1219. }
  1220. static void ieee80211_sta_work(struct work_struct *work)
  1221. {
  1222. struct ieee80211_sub_if_data *sdata =
  1223. container_of(work, struct ieee80211_sub_if_data, u.mgd.work);
  1224. struct ieee80211_local *local = sdata->local;
  1225. struct ieee80211_if_managed *ifmgd;
  1226. struct sk_buff *skb;
  1227. if (!ieee80211_sdata_running(sdata))
  1228. return;
  1229. if (local->scanning)
  1230. return;
  1231. if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_STATION))
  1232. return;
  1233. /*
  1234. * ieee80211_queue_work() should have picked up most cases,
  1235. * here we'll pick the the rest.
  1236. */
  1237. if (WARN(local->suspended, "STA MLME work scheduled while "
  1238. "going to suspend\n"))
  1239. return;
  1240. ifmgd = &sdata->u.mgd;
  1241. /* first process frames to avoid timing out while a frame is pending */
  1242. while ((skb = skb_dequeue(&ifmgd->skb_queue)))
  1243. ieee80211_sta_rx_queued_mgmt(sdata, skb);
  1244. /* then process the rest of the work */
  1245. mutex_lock(&ifmgd->mtx);
  1246. if (ifmgd->flags & (IEEE80211_STA_BEACON_POLL |
  1247. IEEE80211_STA_CONNECTION_POLL) &&
  1248. ifmgd->associated) {
  1249. u8 bssid[ETH_ALEN];
  1250. memcpy(bssid, ifmgd->associated->bssid, ETH_ALEN);
  1251. if (time_is_after_jiffies(ifmgd->probe_timeout))
  1252. run_again(ifmgd, ifmgd->probe_timeout);
  1253. else if (ifmgd->probe_send_count < IEEE80211_MAX_PROBE_TRIES) {
  1254. #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
  1255. printk(KERN_DEBUG "No probe response from AP %pM"
  1256. " after %dms, try %d\n", bssid,
  1257. (1000 * IEEE80211_PROBE_WAIT)/HZ,
  1258. ifmgd->probe_send_count);
  1259. #endif
  1260. ieee80211_mgd_probe_ap_send(sdata);
  1261. } else {
  1262. /*
  1263. * We actually lost the connection ... or did we?
  1264. * Let's make sure!
  1265. */
  1266. ifmgd->flags &= ~(IEEE80211_STA_CONNECTION_POLL |
  1267. IEEE80211_STA_BEACON_POLL);
  1268. printk(KERN_DEBUG "No probe response from AP %pM"
  1269. " after %dms, disconnecting.\n",
  1270. bssid, (1000 * IEEE80211_PROBE_WAIT)/HZ);
  1271. ieee80211_set_disassoc(sdata);
  1272. ieee80211_recalc_idle(local);
  1273. mutex_unlock(&ifmgd->mtx);
  1274. /*
  1275. * must be outside lock due to cfg80211,
  1276. * but that's not a problem.
  1277. */
  1278. ieee80211_send_deauth_disassoc(sdata, bssid,
  1279. IEEE80211_STYPE_DEAUTH,
  1280. WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY,
  1281. NULL);
  1282. mutex_lock(&ifmgd->mtx);
  1283. }
  1284. }
  1285. mutex_unlock(&ifmgd->mtx);
  1286. }
  1287. static void ieee80211_sta_bcn_mon_timer(unsigned long data)
  1288. {
  1289. struct ieee80211_sub_if_data *sdata =
  1290. (struct ieee80211_sub_if_data *) data;
  1291. struct ieee80211_local *local = sdata->local;
  1292. if (local->quiescing)
  1293. return;
  1294. ieee80211_queue_work(&sdata->local->hw, &sdata->u.mgd.beacon_loss_work);
  1295. }
  1296. static void ieee80211_sta_conn_mon_timer(unsigned long data)
  1297. {
  1298. struct ieee80211_sub_if_data *sdata =
  1299. (struct ieee80211_sub_if_data *) data;
  1300. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  1301. struct ieee80211_local *local = sdata->local;
  1302. if (local->quiescing)
  1303. return;
  1304. ieee80211_queue_work(&local->hw, &ifmgd->monitor_work);
  1305. }
  1306. static void ieee80211_sta_monitor_work(struct work_struct *work)
  1307. {
  1308. struct ieee80211_sub_if_data *sdata =
  1309. container_of(work, struct ieee80211_sub_if_data,
  1310. u.mgd.monitor_work);
  1311. ieee80211_mgd_probe_ap(sdata, false);
  1312. }
  1313. static void ieee80211_restart_sta_timer(struct ieee80211_sub_if_data *sdata)
  1314. {
  1315. if (sdata->vif.type == NL80211_IFTYPE_STATION) {
  1316. sdata->u.mgd.flags &= ~(IEEE80211_STA_BEACON_POLL |
  1317. IEEE80211_STA_CONNECTION_POLL);
  1318. /* let's probe the connection once */
  1319. ieee80211_queue_work(&sdata->local->hw,
  1320. &sdata->u.mgd.monitor_work);
  1321. /* and do all the other regular work too */
  1322. ieee80211_queue_work(&sdata->local->hw,
  1323. &sdata->u.mgd.work);
  1324. }
  1325. }
  1326. #ifdef CONFIG_PM
  1327. void ieee80211_sta_quiesce(struct ieee80211_sub_if_data *sdata)
  1328. {
  1329. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  1330. /*
  1331. * we need to use atomic bitops for the running bits
  1332. * only because both timers might fire at the same
  1333. * time -- the code here is properly synchronised.
  1334. */
  1335. cancel_work_sync(&ifmgd->work);
  1336. cancel_work_sync(&ifmgd->beacon_loss_work);
  1337. if (del_timer_sync(&ifmgd->timer))
  1338. set_bit(TMR_RUNNING_TIMER, &ifmgd->timers_running);
  1339. cancel_work_sync(&ifmgd->chswitch_work);
  1340. if (del_timer_sync(&ifmgd->chswitch_timer))
  1341. set_bit(TMR_RUNNING_CHANSW, &ifmgd->timers_running);
  1342. cancel_work_sync(&ifmgd->monitor_work);
  1343. /* these will just be re-established on connection */
  1344. del_timer_sync(&ifmgd->conn_mon_timer);
  1345. del_timer_sync(&ifmgd->bcn_mon_timer);
  1346. }
  1347. void ieee80211_sta_restart(struct ieee80211_sub_if_data *sdata)
  1348. {
  1349. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  1350. if (test_and_clear_bit(TMR_RUNNING_TIMER, &ifmgd->timers_running))
  1351. add_timer(&ifmgd->timer);
  1352. if (test_and_clear_bit(TMR_RUNNING_CHANSW, &ifmgd->timers_running))
  1353. add_timer(&ifmgd->chswitch_timer);
  1354. }
  1355. #endif
  1356. /* interface setup */
  1357. void ieee80211_sta_setup_sdata(struct ieee80211_sub_if_data *sdata)
  1358. {
  1359. struct ieee80211_if_managed *ifmgd;
  1360. ifmgd = &sdata->u.mgd;
  1361. INIT_WORK(&ifmgd->work, ieee80211_sta_work);
  1362. INIT_WORK(&ifmgd->monitor_work, ieee80211_sta_monitor_work);
  1363. INIT_WORK(&ifmgd->chswitch_work, ieee80211_chswitch_work);
  1364. INIT_WORK(&ifmgd->beacon_loss_work, ieee80211_beacon_loss_work);
  1365. setup_timer(&ifmgd->timer, ieee80211_sta_timer,
  1366. (unsigned long) sdata);
  1367. setup_timer(&ifmgd->bcn_mon_timer, ieee80211_sta_bcn_mon_timer,
  1368. (unsigned long) sdata);
  1369. setup_timer(&ifmgd->conn_mon_timer, ieee80211_sta_conn_mon_timer,
  1370. (unsigned long) sdata);
  1371. setup_timer(&ifmgd->chswitch_timer, ieee80211_chswitch_timer,
  1372. (unsigned long) sdata);
  1373. skb_queue_head_init(&ifmgd->skb_queue);
  1374. ifmgd->flags = 0;
  1375. mutex_init(&ifmgd->mtx);
  1376. if (sdata->local->hw.flags & IEEE80211_HW_SUPPORTS_DYNAMIC_SMPS)
  1377. ifmgd->req_smps = IEEE80211_SMPS_AUTOMATIC;
  1378. else
  1379. ifmgd->req_smps = IEEE80211_SMPS_OFF;
  1380. }
  1381. /* scan finished notification */
  1382. void ieee80211_mlme_notify_scan_completed(struct ieee80211_local *local)
  1383. {
  1384. struct ieee80211_sub_if_data *sdata = local->scan_sdata;
  1385. /* Restart STA timers */
  1386. rcu_read_lock();
  1387. list_for_each_entry_rcu(sdata, &local->interfaces, list)
  1388. ieee80211_restart_sta_timer(sdata);
  1389. rcu_read_unlock();
  1390. }
  1391. int ieee80211_max_network_latency(struct notifier_block *nb,
  1392. unsigned long data, void *dummy)
  1393. {
  1394. s32 latency_usec = (s32) data;
  1395. struct ieee80211_local *local =
  1396. container_of(nb, struct ieee80211_local,
  1397. network_latency_notifier);
  1398. mutex_lock(&local->iflist_mtx);
  1399. ieee80211_recalc_ps(local, latency_usec);
  1400. mutex_unlock(&local->iflist_mtx);
  1401. return 0;
  1402. }
  1403. /* config hooks */
  1404. static enum work_done_result
  1405. ieee80211_probe_auth_done(struct ieee80211_work *wk,
  1406. struct sk_buff *skb)
  1407. {
  1408. if (!skb) {
  1409. cfg80211_send_auth_timeout(wk->sdata->dev, wk->filter_ta);
  1410. return WORK_DONE_DESTROY;
  1411. }
  1412. if (wk->type == IEEE80211_WORK_AUTH) {
  1413. cfg80211_send_rx_auth(wk->sdata->dev, skb->data, skb->len);
  1414. return WORK_DONE_DESTROY;
  1415. }
  1416. mutex_lock(&wk->sdata->u.mgd.mtx);
  1417. ieee80211_rx_mgmt_probe_resp(wk->sdata, skb);
  1418. mutex_unlock(&wk->sdata->u.mgd.mtx);
  1419. wk->type = IEEE80211_WORK_AUTH;
  1420. wk->probe_auth.tries = 0;
  1421. return WORK_DONE_REQUEUE;
  1422. }
  1423. int ieee80211_mgd_auth(struct ieee80211_sub_if_data *sdata,
  1424. struct cfg80211_auth_request *req)
  1425. {
  1426. const u8 *ssid;
  1427. struct ieee80211_work *wk;
  1428. u16 auth_alg;
  1429. switch (req->auth_type) {
  1430. case NL80211_AUTHTYPE_OPEN_SYSTEM:
  1431. auth_alg = WLAN_AUTH_OPEN;
  1432. break;
  1433. case NL80211_AUTHTYPE_SHARED_KEY:
  1434. auth_alg = WLAN_AUTH_SHARED_KEY;
  1435. break;
  1436. case NL80211_AUTHTYPE_FT:
  1437. auth_alg = WLAN_AUTH_FT;
  1438. break;
  1439. case NL80211_AUTHTYPE_NETWORK_EAP:
  1440. auth_alg = WLAN_AUTH_LEAP;
  1441. break;
  1442. default:
  1443. return -EOPNOTSUPP;
  1444. }
  1445. wk = kzalloc(sizeof(*wk) + req->ie_len, GFP_KERNEL);
  1446. if (!wk)
  1447. return -ENOMEM;
  1448. memcpy(wk->filter_ta, req->bss->bssid, ETH_ALEN);;
  1449. if (req->ie && req->ie_len) {
  1450. memcpy(wk->ie, req->ie, req->ie_len);
  1451. wk->ie_len = req->ie_len;
  1452. }
  1453. if (req->key && req->key_len) {
  1454. wk->probe_auth.key_len = req->key_len;
  1455. wk->probe_auth.key_idx = req->key_idx;
  1456. memcpy(wk->probe_auth.key, req->key, req->key_len);
  1457. }
  1458. ssid = ieee80211_bss_get_ie(req->bss, WLAN_EID_SSID);
  1459. memcpy(wk->probe_auth.ssid, ssid + 2, ssid[1]);
  1460. wk->probe_auth.ssid_len = ssid[1];
  1461. wk->probe_auth.algorithm = auth_alg;
  1462. wk->probe_auth.privacy = req->bss->capability & WLAN_CAPABILITY_PRIVACY;
  1463. wk->type = IEEE80211_WORK_DIRECT_PROBE;
  1464. wk->chan = req->bss->channel;
  1465. wk->sdata = sdata;
  1466. wk->done = ieee80211_probe_auth_done;
  1467. /*
  1468. * XXX: if still associated need to tell AP that we're going
  1469. * to sleep and then change channel etc.
  1470. * For now switch channel here, later will be handled
  1471. * by submitting this as an off-channel work item.
  1472. */
  1473. sdata->local->oper_channel = req->bss->channel;
  1474. ieee80211_hw_config(sdata->local, 0);
  1475. ieee80211_add_work(wk);
  1476. return 0;
  1477. }
  1478. static enum work_done_result ieee80211_assoc_done(struct ieee80211_work *wk,
  1479. struct sk_buff *skb)
  1480. {
  1481. struct ieee80211_mgmt *mgmt;
  1482. u16 status;
  1483. if (!skb) {
  1484. cfg80211_send_assoc_timeout(wk->sdata->dev, wk->filter_ta);
  1485. return WORK_DONE_DESTROY;
  1486. }
  1487. mgmt = (void *)skb->data;
  1488. status = le16_to_cpu(mgmt->u.assoc_resp.status_code);
  1489. if (status == WLAN_STATUS_SUCCESS) {
  1490. mutex_lock(&wk->sdata->u.mgd.mtx);
  1491. if (!ieee80211_assoc_success(wk, mgmt, skb->len)) {
  1492. mutex_unlock(&wk->sdata->u.mgd.mtx);
  1493. /* oops -- internal error -- send timeout for now */
  1494. cfg80211_send_assoc_timeout(wk->sdata->dev,
  1495. wk->filter_ta);
  1496. return WORK_DONE_DESTROY;
  1497. }
  1498. mutex_unlock(&wk->sdata->u.mgd.mtx);
  1499. }
  1500. cfg80211_send_rx_assoc(wk->sdata->dev, skb->data, skb->len);
  1501. return WORK_DONE_DESTROY;
  1502. }
  1503. int ieee80211_mgd_assoc(struct ieee80211_sub_if_data *sdata,
  1504. struct cfg80211_assoc_request *req)
  1505. {
  1506. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  1507. struct ieee80211_bss *bss = (void *)req->bss->priv;
  1508. struct ieee80211_work *wk;
  1509. const u8 *ssid;
  1510. int i;
  1511. mutex_lock(&ifmgd->mtx);
  1512. if (ifmgd->associated) {
  1513. mutex_unlock(&ifmgd->mtx);
  1514. return -EALREADY;
  1515. }
  1516. mutex_unlock(&ifmgd->mtx);
  1517. wk = kzalloc(sizeof(*wk) + req->ie_len, GFP_KERNEL);
  1518. if (!wk)
  1519. return -ENOMEM;
  1520. ifmgd->flags &= ~IEEE80211_STA_DISABLE_11N;
  1521. for (i = 0; i < req->crypto.n_ciphers_pairwise; i++)
  1522. if (req->crypto.ciphers_pairwise[i] == WLAN_CIPHER_SUITE_WEP40 ||
  1523. req->crypto.ciphers_pairwise[i] == WLAN_CIPHER_SUITE_TKIP ||
  1524. req->crypto.ciphers_pairwise[i] == WLAN_CIPHER_SUITE_WEP104)
  1525. ifmgd->flags |= IEEE80211_STA_DISABLE_11N;
  1526. if (req->ie && req->ie_len) {
  1527. memcpy(wk->ie, req->ie, req->ie_len);
  1528. wk->ie_len = req->ie_len;
  1529. } else
  1530. wk->ie_len = 0;
  1531. wk->assoc.bss = req->bss;
  1532. memcpy(wk->filter_ta, req->bss->bssid, ETH_ALEN);
  1533. /* new association always uses requested smps mode */
  1534. if (ifmgd->req_smps == IEEE80211_SMPS_AUTOMATIC) {
  1535. if (ifmgd->powersave)
  1536. ifmgd->ap_smps = IEEE80211_SMPS_DYNAMIC;
  1537. else
  1538. ifmgd->ap_smps = IEEE80211_SMPS_OFF;
  1539. } else
  1540. ifmgd->ap_smps = ifmgd->req_smps;
  1541. wk->assoc.smps = ifmgd->ap_smps;
  1542. /*
  1543. * IEEE802.11n does not allow TKIP/WEP as pairwise ciphers in HT mode.
  1544. * We still associate in non-HT mode (11a/b/g) if any one of these
  1545. * ciphers is configured as pairwise.
  1546. * We can set this to true for non-11n hardware, that'll be checked
  1547. * separately along with the peer capabilities.
  1548. */
  1549. wk->assoc.use_11n = !(ifmgd->flags & IEEE80211_STA_DISABLE_11N);
  1550. wk->assoc.capability = req->bss->capability;
  1551. wk->assoc.wmm_used = bss->wmm_used;
  1552. wk->assoc.supp_rates = bss->supp_rates;
  1553. wk->assoc.supp_rates_len = bss->supp_rates_len;
  1554. wk->assoc.ht_information_ie =
  1555. ieee80211_bss_get_ie(req->bss, WLAN_EID_HT_INFORMATION);
  1556. ssid = ieee80211_bss_get_ie(req->bss, WLAN_EID_SSID);
  1557. memcpy(wk->assoc.ssid, ssid + 2, ssid[1]);
  1558. wk->assoc.ssid_len = ssid[1];
  1559. if (req->prev_bssid)
  1560. memcpy(wk->assoc.prev_bssid, req->prev_bssid, ETH_ALEN);
  1561. wk->type = IEEE80211_WORK_ASSOC;
  1562. wk->chan = req->bss->channel;
  1563. wk->sdata = sdata;
  1564. wk->done = ieee80211_assoc_done;
  1565. if (req->use_mfp) {
  1566. ifmgd->mfp = IEEE80211_MFP_REQUIRED;
  1567. ifmgd->flags |= IEEE80211_STA_MFP_ENABLED;
  1568. } else {
  1569. ifmgd->mfp = IEEE80211_MFP_DISABLED;
  1570. ifmgd->flags &= ~IEEE80211_STA_MFP_ENABLED;
  1571. }
  1572. if (req->crypto.control_port)
  1573. ifmgd->flags |= IEEE80211_STA_CONTROL_PORT;
  1574. else
  1575. ifmgd->flags &= ~IEEE80211_STA_CONTROL_PORT;
  1576. sdata->local->oper_channel = req->bss->channel;
  1577. ieee80211_hw_config(sdata->local, 0);
  1578. ieee80211_add_work(wk);
  1579. return 0;
  1580. }
  1581. int ieee80211_mgd_deauth(struct ieee80211_sub_if_data *sdata,
  1582. struct cfg80211_deauth_request *req,
  1583. void *cookie)
  1584. {
  1585. struct ieee80211_local *local = sdata->local;
  1586. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  1587. struct ieee80211_work *wk;
  1588. const u8 *bssid = req->bss->bssid;
  1589. mutex_lock(&ifmgd->mtx);
  1590. if (ifmgd->associated == req->bss) {
  1591. bssid = req->bss->bssid;
  1592. ieee80211_set_disassoc(sdata);
  1593. mutex_unlock(&ifmgd->mtx);
  1594. } else {
  1595. bool not_auth_yet = false;
  1596. mutex_unlock(&ifmgd->mtx);
  1597. mutex_lock(&local->work_mtx);
  1598. list_for_each_entry(wk, &local->work_list, list) {
  1599. if (wk->type != IEEE80211_WORK_DIRECT_PROBE)
  1600. continue;
  1601. if (memcmp(req->bss->bssid, wk->filter_ta, ETH_ALEN))
  1602. continue;
  1603. not_auth_yet = true;
  1604. list_del(&wk->list);
  1605. free_work(wk);
  1606. break;
  1607. }
  1608. mutex_unlock(&local->work_mtx);
  1609. /*
  1610. * If somebody requests authentication and we haven't
  1611. * sent out an auth frame yet there's no need to send
  1612. * out a deauth frame either. If the state was PROBE,
  1613. * then this is the case. If it's AUTH we have sent a
  1614. * frame, and if it's IDLE we have completed the auth
  1615. * process already.
  1616. */
  1617. if (not_auth_yet) {
  1618. __cfg80211_auth_canceled(sdata->dev, bssid);
  1619. return 0;
  1620. }
  1621. }
  1622. printk(KERN_DEBUG "%s: deauthenticating from %pM by local choice (reason=%d)\n",
  1623. sdata->name, bssid, req->reason_code);
  1624. ieee80211_send_deauth_disassoc(sdata, bssid,
  1625. IEEE80211_STYPE_DEAUTH, req->reason_code,
  1626. cookie);
  1627. ieee80211_recalc_idle(sdata->local);
  1628. return 0;
  1629. }
  1630. int ieee80211_mgd_disassoc(struct ieee80211_sub_if_data *sdata,
  1631. struct cfg80211_disassoc_request *req,
  1632. void *cookie)
  1633. {
  1634. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  1635. mutex_lock(&ifmgd->mtx);
  1636. /*
  1637. * cfg80211 should catch this ... but it's racy since
  1638. * we can receive a disassoc frame, process it, hand it
  1639. * to cfg80211 while that's in a locked section already
  1640. * trying to tell us that the user wants to disconnect.
  1641. */
  1642. if (ifmgd->associated != req->bss) {
  1643. mutex_unlock(&ifmgd->mtx);
  1644. return -ENOLINK;
  1645. }
  1646. printk(KERN_DEBUG "%s: disassociating from %pM by local choice (reason=%d)\n",
  1647. sdata->name, req->bss->bssid, req->reason_code);
  1648. ieee80211_set_disassoc(sdata);
  1649. mutex_unlock(&ifmgd->mtx);
  1650. ieee80211_send_deauth_disassoc(sdata, req->bss->bssid,
  1651. IEEE80211_STYPE_DISASSOC, req->reason_code,
  1652. cookie);
  1653. ieee80211_recalc_idle(sdata->local);
  1654. return 0;
  1655. }