mlme.c 67 KB

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