mlme.c 57 KB

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