mlme.c 71 KB

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