mlme.c 59 KB

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