util.c 56 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211
  1. /*
  2. * Copyright 2002-2005, Instant802 Networks, Inc.
  3. * Copyright 2005-2006, Devicescape Software, Inc.
  4. * Copyright 2006-2007 Jiri Benc <jbenc@suse.cz>
  5. * Copyright 2007 Johannes Berg <johannes@sipsolutions.net>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. *
  11. * utilities for mac80211
  12. */
  13. #include <net/mac80211.h>
  14. #include <linux/netdevice.h>
  15. #include <linux/export.h>
  16. #include <linux/types.h>
  17. #include <linux/slab.h>
  18. #include <linux/skbuff.h>
  19. #include <linux/etherdevice.h>
  20. #include <linux/if_arp.h>
  21. #include <linux/bitmap.h>
  22. #include <linux/crc32.h>
  23. #include <net/net_namespace.h>
  24. #include <net/cfg80211.h>
  25. #include <net/rtnetlink.h>
  26. #include "ieee80211_i.h"
  27. #include "driver-ops.h"
  28. #include "rate.h"
  29. #include "mesh.h"
  30. #include "wme.h"
  31. #include "led.h"
  32. #include "wep.h"
  33. /* privid for wiphys to determine whether they belong to us or not */
  34. void *mac80211_wiphy_privid = &mac80211_wiphy_privid;
  35. struct ieee80211_hw *wiphy_to_ieee80211_hw(struct wiphy *wiphy)
  36. {
  37. struct ieee80211_local *local;
  38. BUG_ON(!wiphy);
  39. local = wiphy_priv(wiphy);
  40. return &local->hw;
  41. }
  42. EXPORT_SYMBOL(wiphy_to_ieee80211_hw);
  43. u8 *ieee80211_get_bssid(struct ieee80211_hdr *hdr, size_t len,
  44. enum nl80211_iftype type)
  45. {
  46. __le16 fc = hdr->frame_control;
  47. /* drop ACK/CTS frames and incorrect hdr len (ctrl) */
  48. if (len < 16)
  49. return NULL;
  50. if (ieee80211_is_data(fc)) {
  51. if (len < 24) /* drop incorrect hdr len (data) */
  52. return NULL;
  53. if (ieee80211_has_a4(fc))
  54. return NULL;
  55. if (ieee80211_has_tods(fc))
  56. return hdr->addr1;
  57. if (ieee80211_has_fromds(fc))
  58. return hdr->addr2;
  59. return hdr->addr3;
  60. }
  61. if (ieee80211_is_mgmt(fc)) {
  62. if (len < 24) /* drop incorrect hdr len (mgmt) */
  63. return NULL;
  64. return hdr->addr3;
  65. }
  66. if (ieee80211_is_ctl(fc)) {
  67. if(ieee80211_is_pspoll(fc))
  68. return hdr->addr1;
  69. if (ieee80211_is_back_req(fc)) {
  70. switch (type) {
  71. case NL80211_IFTYPE_STATION:
  72. return hdr->addr2;
  73. case NL80211_IFTYPE_AP:
  74. case NL80211_IFTYPE_AP_VLAN:
  75. return hdr->addr1;
  76. default:
  77. break; /* fall through to the return */
  78. }
  79. }
  80. }
  81. return NULL;
  82. }
  83. void ieee80211_tx_set_protected(struct ieee80211_tx_data *tx)
  84. {
  85. struct sk_buff *skb;
  86. struct ieee80211_hdr *hdr;
  87. skb_queue_walk(&tx->skbs, skb) {
  88. hdr = (struct ieee80211_hdr *) skb->data;
  89. hdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_PROTECTED);
  90. }
  91. }
  92. int ieee80211_frame_duration(enum ieee80211_band band, size_t len,
  93. int rate, int erp, int short_preamble)
  94. {
  95. int dur;
  96. /* calculate duration (in microseconds, rounded up to next higher
  97. * integer if it includes a fractional microsecond) to send frame of
  98. * len bytes (does not include FCS) at the given rate. Duration will
  99. * also include SIFS.
  100. *
  101. * rate is in 100 kbps, so divident is multiplied by 10 in the
  102. * DIV_ROUND_UP() operations.
  103. */
  104. if (band == IEEE80211_BAND_5GHZ || erp) {
  105. /*
  106. * OFDM:
  107. *
  108. * N_DBPS = DATARATE x 4
  109. * N_SYM = Ceiling((16+8xLENGTH+6) / N_DBPS)
  110. * (16 = SIGNAL time, 6 = tail bits)
  111. * TXTIME = T_PREAMBLE + T_SIGNAL + T_SYM x N_SYM + Signal Ext
  112. *
  113. * T_SYM = 4 usec
  114. * 802.11a - 17.5.2: aSIFSTime = 16 usec
  115. * 802.11g - 19.8.4: aSIFSTime = 10 usec +
  116. * signal ext = 6 usec
  117. */
  118. dur = 16; /* SIFS + signal ext */
  119. dur += 16; /* 17.3.2.3: T_PREAMBLE = 16 usec */
  120. dur += 4; /* 17.3.2.3: T_SIGNAL = 4 usec */
  121. dur += 4 * DIV_ROUND_UP((16 + 8 * (len + 4) + 6) * 10,
  122. 4 * rate); /* T_SYM x N_SYM */
  123. } else {
  124. /*
  125. * 802.11b or 802.11g with 802.11b compatibility:
  126. * 18.3.4: TXTIME = PreambleLength + PLCPHeaderTime +
  127. * Ceiling(((LENGTH+PBCC)x8)/DATARATE). PBCC=0.
  128. *
  129. * 802.11 (DS): 15.3.3, 802.11b: 18.3.4
  130. * aSIFSTime = 10 usec
  131. * aPreambleLength = 144 usec or 72 usec with short preamble
  132. * aPLCPHeaderLength = 48 usec or 24 usec with short preamble
  133. */
  134. dur = 10; /* aSIFSTime = 10 usec */
  135. dur += short_preamble ? (72 + 24) : (144 + 48);
  136. dur += DIV_ROUND_UP(8 * (len + 4) * 10, rate);
  137. }
  138. return dur;
  139. }
  140. /* Exported duration function for driver use */
  141. __le16 ieee80211_generic_frame_duration(struct ieee80211_hw *hw,
  142. struct ieee80211_vif *vif,
  143. enum ieee80211_band band,
  144. size_t frame_len,
  145. struct ieee80211_rate *rate)
  146. {
  147. struct ieee80211_sub_if_data *sdata;
  148. u16 dur;
  149. int erp;
  150. bool short_preamble = false;
  151. erp = 0;
  152. if (vif) {
  153. sdata = vif_to_sdata(vif);
  154. short_preamble = sdata->vif.bss_conf.use_short_preamble;
  155. if (sdata->flags & IEEE80211_SDATA_OPERATING_GMODE)
  156. erp = rate->flags & IEEE80211_RATE_ERP_G;
  157. }
  158. dur = ieee80211_frame_duration(band, frame_len, rate->bitrate, erp,
  159. short_preamble);
  160. return cpu_to_le16(dur);
  161. }
  162. EXPORT_SYMBOL(ieee80211_generic_frame_duration);
  163. __le16 ieee80211_rts_duration(struct ieee80211_hw *hw,
  164. struct ieee80211_vif *vif, size_t frame_len,
  165. const struct ieee80211_tx_info *frame_txctl)
  166. {
  167. struct ieee80211_local *local = hw_to_local(hw);
  168. struct ieee80211_rate *rate;
  169. struct ieee80211_sub_if_data *sdata;
  170. bool short_preamble;
  171. int erp;
  172. u16 dur;
  173. struct ieee80211_supported_band *sband;
  174. sband = local->hw.wiphy->bands[frame_txctl->band];
  175. short_preamble = false;
  176. rate = &sband->bitrates[frame_txctl->control.rts_cts_rate_idx];
  177. erp = 0;
  178. if (vif) {
  179. sdata = vif_to_sdata(vif);
  180. short_preamble = sdata->vif.bss_conf.use_short_preamble;
  181. if (sdata->flags & IEEE80211_SDATA_OPERATING_GMODE)
  182. erp = rate->flags & IEEE80211_RATE_ERP_G;
  183. }
  184. /* CTS duration */
  185. dur = ieee80211_frame_duration(sband->band, 10, rate->bitrate,
  186. erp, short_preamble);
  187. /* Data frame duration */
  188. dur += ieee80211_frame_duration(sband->band, frame_len, rate->bitrate,
  189. erp, short_preamble);
  190. /* ACK duration */
  191. dur += ieee80211_frame_duration(sband->band, 10, rate->bitrate,
  192. erp, short_preamble);
  193. return cpu_to_le16(dur);
  194. }
  195. EXPORT_SYMBOL(ieee80211_rts_duration);
  196. __le16 ieee80211_ctstoself_duration(struct ieee80211_hw *hw,
  197. struct ieee80211_vif *vif,
  198. size_t frame_len,
  199. const struct ieee80211_tx_info *frame_txctl)
  200. {
  201. struct ieee80211_local *local = hw_to_local(hw);
  202. struct ieee80211_rate *rate;
  203. struct ieee80211_sub_if_data *sdata;
  204. bool short_preamble;
  205. int erp;
  206. u16 dur;
  207. struct ieee80211_supported_band *sband;
  208. sband = local->hw.wiphy->bands[frame_txctl->band];
  209. short_preamble = false;
  210. rate = &sband->bitrates[frame_txctl->control.rts_cts_rate_idx];
  211. erp = 0;
  212. if (vif) {
  213. sdata = vif_to_sdata(vif);
  214. short_preamble = sdata->vif.bss_conf.use_short_preamble;
  215. if (sdata->flags & IEEE80211_SDATA_OPERATING_GMODE)
  216. erp = rate->flags & IEEE80211_RATE_ERP_G;
  217. }
  218. /* Data frame duration */
  219. dur = ieee80211_frame_duration(sband->band, frame_len, rate->bitrate,
  220. erp, short_preamble);
  221. if (!(frame_txctl->flags & IEEE80211_TX_CTL_NO_ACK)) {
  222. /* ACK duration */
  223. dur += ieee80211_frame_duration(sband->band, 10, rate->bitrate,
  224. erp, short_preamble);
  225. }
  226. return cpu_to_le16(dur);
  227. }
  228. EXPORT_SYMBOL(ieee80211_ctstoself_duration);
  229. void ieee80211_propagate_queue_wake(struct ieee80211_local *local, int queue)
  230. {
  231. struct ieee80211_sub_if_data *sdata;
  232. int n_acs = IEEE80211_NUM_ACS;
  233. if (local->hw.queues < IEEE80211_NUM_ACS)
  234. n_acs = 1;
  235. list_for_each_entry_rcu(sdata, &local->interfaces, list) {
  236. int ac;
  237. if (!sdata->dev)
  238. continue;
  239. if (test_bit(SDATA_STATE_OFFCHANNEL, &sdata->state))
  240. continue;
  241. if (sdata->vif.cab_queue != IEEE80211_INVAL_HW_QUEUE &&
  242. local->queue_stop_reasons[sdata->vif.cab_queue] != 0)
  243. continue;
  244. for (ac = 0; ac < n_acs; ac++) {
  245. int ac_queue = sdata->vif.hw_queue[ac];
  246. if (ac_queue == queue ||
  247. (sdata->vif.cab_queue == queue &&
  248. local->queue_stop_reasons[ac_queue] == 0 &&
  249. skb_queue_empty(&local->pending[ac_queue])))
  250. netif_wake_subqueue(sdata->dev, ac);
  251. }
  252. }
  253. }
  254. static void __ieee80211_wake_queue(struct ieee80211_hw *hw, int queue,
  255. enum queue_stop_reason reason)
  256. {
  257. struct ieee80211_local *local = hw_to_local(hw);
  258. trace_wake_queue(local, queue, reason);
  259. if (WARN_ON(queue >= hw->queues))
  260. return;
  261. if (!test_bit(reason, &local->queue_stop_reasons[queue]))
  262. return;
  263. __clear_bit(reason, &local->queue_stop_reasons[queue]);
  264. if (local->queue_stop_reasons[queue] != 0)
  265. /* someone still has this queue stopped */
  266. return;
  267. if (skb_queue_empty(&local->pending[queue])) {
  268. rcu_read_lock();
  269. ieee80211_propagate_queue_wake(local, queue);
  270. rcu_read_unlock();
  271. } else
  272. tasklet_schedule(&local->tx_pending_tasklet);
  273. }
  274. void ieee80211_wake_queue_by_reason(struct ieee80211_hw *hw, int queue,
  275. enum queue_stop_reason reason)
  276. {
  277. struct ieee80211_local *local = hw_to_local(hw);
  278. unsigned long flags;
  279. spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
  280. __ieee80211_wake_queue(hw, queue, reason);
  281. spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
  282. }
  283. void ieee80211_wake_queue(struct ieee80211_hw *hw, int queue)
  284. {
  285. ieee80211_wake_queue_by_reason(hw, queue,
  286. IEEE80211_QUEUE_STOP_REASON_DRIVER);
  287. }
  288. EXPORT_SYMBOL(ieee80211_wake_queue);
  289. static void __ieee80211_stop_queue(struct ieee80211_hw *hw, int queue,
  290. enum queue_stop_reason reason)
  291. {
  292. struct ieee80211_local *local = hw_to_local(hw);
  293. struct ieee80211_sub_if_data *sdata;
  294. int n_acs = IEEE80211_NUM_ACS;
  295. trace_stop_queue(local, queue, reason);
  296. if (WARN_ON(queue >= hw->queues))
  297. return;
  298. if (test_bit(reason, &local->queue_stop_reasons[queue]))
  299. return;
  300. __set_bit(reason, &local->queue_stop_reasons[queue]);
  301. if (local->hw.queues < IEEE80211_NUM_ACS)
  302. n_acs = 1;
  303. rcu_read_lock();
  304. list_for_each_entry_rcu(sdata, &local->interfaces, list) {
  305. int ac;
  306. if (!sdata->dev)
  307. continue;
  308. for (ac = 0; ac < n_acs; ac++) {
  309. if (sdata->vif.hw_queue[ac] == queue ||
  310. sdata->vif.cab_queue == queue)
  311. netif_stop_subqueue(sdata->dev, ac);
  312. }
  313. }
  314. rcu_read_unlock();
  315. }
  316. void ieee80211_stop_queue_by_reason(struct ieee80211_hw *hw, int queue,
  317. enum queue_stop_reason reason)
  318. {
  319. struct ieee80211_local *local = hw_to_local(hw);
  320. unsigned long flags;
  321. spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
  322. __ieee80211_stop_queue(hw, queue, reason);
  323. spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
  324. }
  325. void ieee80211_stop_queue(struct ieee80211_hw *hw, int queue)
  326. {
  327. ieee80211_stop_queue_by_reason(hw, queue,
  328. IEEE80211_QUEUE_STOP_REASON_DRIVER);
  329. }
  330. EXPORT_SYMBOL(ieee80211_stop_queue);
  331. void ieee80211_add_pending_skb(struct ieee80211_local *local,
  332. struct sk_buff *skb)
  333. {
  334. struct ieee80211_hw *hw = &local->hw;
  335. unsigned long flags;
  336. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  337. int queue = info->hw_queue;
  338. if (WARN_ON(!info->control.vif)) {
  339. ieee80211_free_txskb(&local->hw, skb);
  340. return;
  341. }
  342. spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
  343. __ieee80211_stop_queue(hw, queue, IEEE80211_QUEUE_STOP_REASON_SKB_ADD);
  344. __skb_queue_tail(&local->pending[queue], skb);
  345. __ieee80211_wake_queue(hw, queue, IEEE80211_QUEUE_STOP_REASON_SKB_ADD);
  346. spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
  347. }
  348. void ieee80211_add_pending_skbs_fn(struct ieee80211_local *local,
  349. struct sk_buff_head *skbs,
  350. void (*fn)(void *data), void *data)
  351. {
  352. struct ieee80211_hw *hw = &local->hw;
  353. struct sk_buff *skb;
  354. unsigned long flags;
  355. int queue, i;
  356. spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
  357. while ((skb = skb_dequeue(skbs))) {
  358. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  359. if (WARN_ON(!info->control.vif)) {
  360. ieee80211_free_txskb(&local->hw, skb);
  361. continue;
  362. }
  363. queue = info->hw_queue;
  364. __ieee80211_stop_queue(hw, queue,
  365. IEEE80211_QUEUE_STOP_REASON_SKB_ADD);
  366. __skb_queue_tail(&local->pending[queue], skb);
  367. }
  368. if (fn)
  369. fn(data);
  370. for (i = 0; i < hw->queues; i++)
  371. __ieee80211_wake_queue(hw, i,
  372. IEEE80211_QUEUE_STOP_REASON_SKB_ADD);
  373. spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
  374. }
  375. void ieee80211_stop_queues_by_reason(struct ieee80211_hw *hw,
  376. unsigned long queues,
  377. enum queue_stop_reason reason)
  378. {
  379. struct ieee80211_local *local = hw_to_local(hw);
  380. unsigned long flags;
  381. int i;
  382. spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
  383. for_each_set_bit(i, &queues, hw->queues)
  384. __ieee80211_stop_queue(hw, i, reason);
  385. spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
  386. }
  387. void ieee80211_stop_queues(struct ieee80211_hw *hw)
  388. {
  389. ieee80211_stop_queues_by_reason(hw, IEEE80211_MAX_QUEUE_MAP,
  390. IEEE80211_QUEUE_STOP_REASON_DRIVER);
  391. }
  392. EXPORT_SYMBOL(ieee80211_stop_queues);
  393. int ieee80211_queue_stopped(struct ieee80211_hw *hw, int queue)
  394. {
  395. struct ieee80211_local *local = hw_to_local(hw);
  396. unsigned long flags;
  397. int ret;
  398. if (WARN_ON(queue >= hw->queues))
  399. return true;
  400. spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
  401. ret = test_bit(IEEE80211_QUEUE_STOP_REASON_DRIVER,
  402. &local->queue_stop_reasons[queue]);
  403. spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
  404. return ret;
  405. }
  406. EXPORT_SYMBOL(ieee80211_queue_stopped);
  407. void ieee80211_wake_queues_by_reason(struct ieee80211_hw *hw,
  408. unsigned long queues,
  409. enum queue_stop_reason reason)
  410. {
  411. struct ieee80211_local *local = hw_to_local(hw);
  412. unsigned long flags;
  413. int i;
  414. spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
  415. for_each_set_bit(i, &queues, hw->queues)
  416. __ieee80211_wake_queue(hw, i, reason);
  417. spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
  418. }
  419. void ieee80211_wake_queues(struct ieee80211_hw *hw)
  420. {
  421. ieee80211_wake_queues_by_reason(hw, IEEE80211_MAX_QUEUE_MAP,
  422. IEEE80211_QUEUE_STOP_REASON_DRIVER);
  423. }
  424. EXPORT_SYMBOL(ieee80211_wake_queues);
  425. void ieee80211_flush_queues(struct ieee80211_local *local,
  426. struct ieee80211_sub_if_data *sdata)
  427. {
  428. u32 queues;
  429. if (!local->ops->flush)
  430. return;
  431. if (sdata && local->hw.flags & IEEE80211_HW_QUEUE_CONTROL) {
  432. int ac;
  433. queues = 0;
  434. for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
  435. queues |= BIT(sdata->vif.hw_queue[ac]);
  436. if (sdata->vif.cab_queue != IEEE80211_INVAL_HW_QUEUE)
  437. queues |= BIT(sdata->vif.cab_queue);
  438. } else {
  439. /* all queues */
  440. queues = BIT(local->hw.queues) - 1;
  441. }
  442. ieee80211_stop_queues_by_reason(&local->hw, IEEE80211_MAX_QUEUE_MAP,
  443. IEEE80211_QUEUE_STOP_REASON_FLUSH);
  444. drv_flush(local, queues, false);
  445. ieee80211_wake_queues_by_reason(&local->hw, IEEE80211_MAX_QUEUE_MAP,
  446. IEEE80211_QUEUE_STOP_REASON_FLUSH);
  447. }
  448. void ieee80211_iterate_active_interfaces(
  449. struct ieee80211_hw *hw, u32 iter_flags,
  450. void (*iterator)(void *data, u8 *mac,
  451. struct ieee80211_vif *vif),
  452. void *data)
  453. {
  454. struct ieee80211_local *local = hw_to_local(hw);
  455. struct ieee80211_sub_if_data *sdata;
  456. mutex_lock(&local->iflist_mtx);
  457. list_for_each_entry(sdata, &local->interfaces, list) {
  458. switch (sdata->vif.type) {
  459. case NL80211_IFTYPE_MONITOR:
  460. if (!(sdata->u.mntr_flags & MONITOR_FLAG_ACTIVE))
  461. continue;
  462. break;
  463. case NL80211_IFTYPE_AP_VLAN:
  464. continue;
  465. default:
  466. break;
  467. }
  468. if (!(iter_flags & IEEE80211_IFACE_ITER_RESUME_ALL) &&
  469. !(sdata->flags & IEEE80211_SDATA_IN_DRIVER))
  470. continue;
  471. if (ieee80211_sdata_running(sdata))
  472. iterator(data, sdata->vif.addr,
  473. &sdata->vif);
  474. }
  475. sdata = rcu_dereference_protected(local->monitor_sdata,
  476. lockdep_is_held(&local->iflist_mtx));
  477. if (sdata &&
  478. (iter_flags & IEEE80211_IFACE_ITER_RESUME_ALL ||
  479. sdata->flags & IEEE80211_SDATA_IN_DRIVER))
  480. iterator(data, sdata->vif.addr, &sdata->vif);
  481. mutex_unlock(&local->iflist_mtx);
  482. }
  483. EXPORT_SYMBOL_GPL(ieee80211_iterate_active_interfaces);
  484. void ieee80211_iterate_active_interfaces_atomic(
  485. struct ieee80211_hw *hw, u32 iter_flags,
  486. void (*iterator)(void *data, u8 *mac,
  487. struct ieee80211_vif *vif),
  488. void *data)
  489. {
  490. struct ieee80211_local *local = hw_to_local(hw);
  491. struct ieee80211_sub_if_data *sdata;
  492. rcu_read_lock();
  493. list_for_each_entry_rcu(sdata, &local->interfaces, list) {
  494. switch (sdata->vif.type) {
  495. case NL80211_IFTYPE_MONITOR:
  496. if (!(sdata->u.mntr_flags & MONITOR_FLAG_ACTIVE))
  497. continue;
  498. break;
  499. case NL80211_IFTYPE_AP_VLAN:
  500. continue;
  501. default:
  502. break;
  503. }
  504. if (!(iter_flags & IEEE80211_IFACE_ITER_RESUME_ALL) &&
  505. !(sdata->flags & IEEE80211_SDATA_IN_DRIVER))
  506. continue;
  507. if (ieee80211_sdata_running(sdata))
  508. iterator(data, sdata->vif.addr,
  509. &sdata->vif);
  510. }
  511. sdata = rcu_dereference(local->monitor_sdata);
  512. if (sdata &&
  513. (iter_flags & IEEE80211_IFACE_ITER_RESUME_ALL ||
  514. sdata->flags & IEEE80211_SDATA_IN_DRIVER))
  515. iterator(data, sdata->vif.addr, &sdata->vif);
  516. rcu_read_unlock();
  517. }
  518. EXPORT_SYMBOL_GPL(ieee80211_iterate_active_interfaces_atomic);
  519. /*
  520. * Nothing should have been stuffed into the workqueue during
  521. * the suspend->resume cycle. If this WARN is seen then there
  522. * is a bug with either the driver suspend or something in
  523. * mac80211 stuffing into the workqueue which we haven't yet
  524. * cleared during mac80211's suspend cycle.
  525. */
  526. static bool ieee80211_can_queue_work(struct ieee80211_local *local)
  527. {
  528. if (WARN(local->suspended && !local->resuming,
  529. "queueing ieee80211 work while going to suspend\n"))
  530. return false;
  531. return true;
  532. }
  533. void ieee80211_queue_work(struct ieee80211_hw *hw, struct work_struct *work)
  534. {
  535. struct ieee80211_local *local = hw_to_local(hw);
  536. if (!ieee80211_can_queue_work(local))
  537. return;
  538. queue_work(local->workqueue, work);
  539. }
  540. EXPORT_SYMBOL(ieee80211_queue_work);
  541. void ieee80211_queue_delayed_work(struct ieee80211_hw *hw,
  542. struct delayed_work *dwork,
  543. unsigned long delay)
  544. {
  545. struct ieee80211_local *local = hw_to_local(hw);
  546. if (!ieee80211_can_queue_work(local))
  547. return;
  548. queue_delayed_work(local->workqueue, dwork, delay);
  549. }
  550. EXPORT_SYMBOL(ieee80211_queue_delayed_work);
  551. u32 ieee802_11_parse_elems_crc(const u8 *start, size_t len, bool action,
  552. struct ieee802_11_elems *elems,
  553. u64 filter, u32 crc)
  554. {
  555. size_t left = len;
  556. const u8 *pos = start;
  557. bool calc_crc = filter != 0;
  558. DECLARE_BITMAP(seen_elems, 256);
  559. const u8 *ie;
  560. bitmap_zero(seen_elems, 256);
  561. memset(elems, 0, sizeof(*elems));
  562. elems->ie_start = start;
  563. elems->total_len = len;
  564. while (left >= 2) {
  565. u8 id, elen;
  566. bool elem_parse_failed;
  567. id = *pos++;
  568. elen = *pos++;
  569. left -= 2;
  570. if (elen > left) {
  571. elems->parse_error = true;
  572. break;
  573. }
  574. switch (id) {
  575. case WLAN_EID_SSID:
  576. case WLAN_EID_SUPP_RATES:
  577. case WLAN_EID_FH_PARAMS:
  578. case WLAN_EID_DS_PARAMS:
  579. case WLAN_EID_CF_PARAMS:
  580. case WLAN_EID_TIM:
  581. case WLAN_EID_IBSS_PARAMS:
  582. case WLAN_EID_CHALLENGE:
  583. case WLAN_EID_RSN:
  584. case WLAN_EID_ERP_INFO:
  585. case WLAN_EID_EXT_SUPP_RATES:
  586. case WLAN_EID_HT_CAPABILITY:
  587. case WLAN_EID_HT_OPERATION:
  588. case WLAN_EID_VHT_CAPABILITY:
  589. case WLAN_EID_VHT_OPERATION:
  590. case WLAN_EID_MESH_ID:
  591. case WLAN_EID_MESH_CONFIG:
  592. case WLAN_EID_PEER_MGMT:
  593. case WLAN_EID_PREQ:
  594. case WLAN_EID_PREP:
  595. case WLAN_EID_PERR:
  596. case WLAN_EID_RANN:
  597. case WLAN_EID_CHANNEL_SWITCH:
  598. case WLAN_EID_EXT_CHANSWITCH_ANN:
  599. case WLAN_EID_COUNTRY:
  600. case WLAN_EID_PWR_CONSTRAINT:
  601. case WLAN_EID_TIMEOUT_INTERVAL:
  602. case WLAN_EID_SECONDARY_CHANNEL_OFFSET:
  603. case WLAN_EID_WIDE_BW_CHANNEL_SWITCH:
  604. /*
  605. * not listing WLAN_EID_CHANNEL_SWITCH_WRAPPER -- it seems possible
  606. * that if the content gets bigger it might be needed more than once
  607. */
  608. if (test_bit(id, seen_elems)) {
  609. elems->parse_error = true;
  610. left -= elen;
  611. pos += elen;
  612. continue;
  613. }
  614. break;
  615. }
  616. if (calc_crc && id < 64 && (filter & (1ULL << id)))
  617. crc = crc32_be(crc, pos - 2, elen + 2);
  618. elem_parse_failed = false;
  619. switch (id) {
  620. case WLAN_EID_SSID:
  621. elems->ssid = pos;
  622. elems->ssid_len = elen;
  623. break;
  624. case WLAN_EID_SUPP_RATES:
  625. elems->supp_rates = pos;
  626. elems->supp_rates_len = elen;
  627. break;
  628. case WLAN_EID_DS_PARAMS:
  629. if (elen >= 1)
  630. elems->ds_params = pos;
  631. else
  632. elem_parse_failed = true;
  633. break;
  634. case WLAN_EID_TIM:
  635. if (elen >= sizeof(struct ieee80211_tim_ie)) {
  636. elems->tim = (void *)pos;
  637. elems->tim_len = elen;
  638. } else
  639. elem_parse_failed = true;
  640. break;
  641. case WLAN_EID_CHALLENGE:
  642. elems->challenge = pos;
  643. elems->challenge_len = elen;
  644. break;
  645. case WLAN_EID_VENDOR_SPECIFIC:
  646. if (elen >= 4 && pos[0] == 0x00 && pos[1] == 0x50 &&
  647. pos[2] == 0xf2) {
  648. /* Microsoft OUI (00:50:F2) */
  649. if (calc_crc)
  650. crc = crc32_be(crc, pos - 2, elen + 2);
  651. if (elen >= 5 && pos[3] == 2) {
  652. /* OUI Type 2 - WMM IE */
  653. if (pos[4] == 0) {
  654. elems->wmm_info = pos;
  655. elems->wmm_info_len = elen;
  656. } else if (pos[4] == 1) {
  657. elems->wmm_param = pos;
  658. elems->wmm_param_len = elen;
  659. }
  660. }
  661. }
  662. break;
  663. case WLAN_EID_RSN:
  664. elems->rsn = pos;
  665. elems->rsn_len = elen;
  666. break;
  667. case WLAN_EID_ERP_INFO:
  668. if (elen >= 1)
  669. elems->erp_info = pos;
  670. else
  671. elem_parse_failed = true;
  672. break;
  673. case WLAN_EID_EXT_SUPP_RATES:
  674. elems->ext_supp_rates = pos;
  675. elems->ext_supp_rates_len = elen;
  676. break;
  677. case WLAN_EID_HT_CAPABILITY:
  678. if (elen >= sizeof(struct ieee80211_ht_cap))
  679. elems->ht_cap_elem = (void *)pos;
  680. else
  681. elem_parse_failed = true;
  682. break;
  683. case WLAN_EID_HT_OPERATION:
  684. if (elen >= sizeof(struct ieee80211_ht_operation))
  685. elems->ht_operation = (void *)pos;
  686. else
  687. elem_parse_failed = true;
  688. break;
  689. case WLAN_EID_VHT_CAPABILITY:
  690. if (elen >= sizeof(struct ieee80211_vht_cap))
  691. elems->vht_cap_elem = (void *)pos;
  692. else
  693. elem_parse_failed = true;
  694. break;
  695. case WLAN_EID_VHT_OPERATION:
  696. if (elen >= sizeof(struct ieee80211_vht_operation))
  697. elems->vht_operation = (void *)pos;
  698. else
  699. elem_parse_failed = true;
  700. break;
  701. case WLAN_EID_OPMODE_NOTIF:
  702. if (elen > 0)
  703. elems->opmode_notif = pos;
  704. else
  705. elem_parse_failed = true;
  706. break;
  707. case WLAN_EID_MESH_ID:
  708. elems->mesh_id = pos;
  709. elems->mesh_id_len = elen;
  710. break;
  711. case WLAN_EID_MESH_CONFIG:
  712. if (elen >= sizeof(struct ieee80211_meshconf_ie))
  713. elems->mesh_config = (void *)pos;
  714. else
  715. elem_parse_failed = true;
  716. break;
  717. case WLAN_EID_PEER_MGMT:
  718. elems->peering = pos;
  719. elems->peering_len = elen;
  720. break;
  721. case WLAN_EID_MESH_AWAKE_WINDOW:
  722. if (elen >= 2)
  723. elems->awake_window = (void *)pos;
  724. break;
  725. case WLAN_EID_PREQ:
  726. elems->preq = pos;
  727. elems->preq_len = elen;
  728. break;
  729. case WLAN_EID_PREP:
  730. elems->prep = pos;
  731. elems->prep_len = elen;
  732. break;
  733. case WLAN_EID_PERR:
  734. elems->perr = pos;
  735. elems->perr_len = elen;
  736. break;
  737. case WLAN_EID_RANN:
  738. if (elen >= sizeof(struct ieee80211_rann_ie))
  739. elems->rann = (void *)pos;
  740. else
  741. elem_parse_failed = true;
  742. break;
  743. case WLAN_EID_CHANNEL_SWITCH:
  744. if (elen != sizeof(struct ieee80211_channel_sw_ie)) {
  745. elem_parse_failed = true;
  746. break;
  747. }
  748. elems->ch_switch_ie = (void *)pos;
  749. break;
  750. case WLAN_EID_EXT_CHANSWITCH_ANN:
  751. if (elen != sizeof(struct ieee80211_ext_chansw_ie)) {
  752. elem_parse_failed = true;
  753. break;
  754. }
  755. elems->ext_chansw_ie = (void *)pos;
  756. break;
  757. case WLAN_EID_SECONDARY_CHANNEL_OFFSET:
  758. if (elen != sizeof(struct ieee80211_sec_chan_offs_ie)) {
  759. elem_parse_failed = true;
  760. break;
  761. }
  762. elems->sec_chan_offs = (void *)pos;
  763. break;
  764. case WLAN_EID_WIDE_BW_CHANNEL_SWITCH:
  765. if (!action ||
  766. elen != sizeof(*elems->wide_bw_chansw_ie)) {
  767. elem_parse_failed = true;
  768. break;
  769. }
  770. elems->wide_bw_chansw_ie = (void *)pos;
  771. break;
  772. case WLAN_EID_CHANNEL_SWITCH_WRAPPER:
  773. if (action) {
  774. elem_parse_failed = true;
  775. break;
  776. }
  777. /*
  778. * This is a bit tricky, but as we only care about
  779. * the wide bandwidth channel switch element, so
  780. * just parse it out manually.
  781. */
  782. ie = cfg80211_find_ie(WLAN_EID_WIDE_BW_CHANNEL_SWITCH,
  783. pos, elen);
  784. if (ie) {
  785. if (ie[1] == sizeof(*elems->wide_bw_chansw_ie))
  786. elems->wide_bw_chansw_ie =
  787. (void *)(ie + 2);
  788. else
  789. elem_parse_failed = true;
  790. }
  791. break;
  792. case WLAN_EID_COUNTRY:
  793. elems->country_elem = pos;
  794. elems->country_elem_len = elen;
  795. break;
  796. case WLAN_EID_PWR_CONSTRAINT:
  797. if (elen != 1) {
  798. elem_parse_failed = true;
  799. break;
  800. }
  801. elems->pwr_constr_elem = pos;
  802. break;
  803. case WLAN_EID_TIMEOUT_INTERVAL:
  804. if (elen >= sizeof(struct ieee80211_timeout_interval_ie))
  805. elems->timeout_int = (void *)pos;
  806. else
  807. elem_parse_failed = true;
  808. break;
  809. default:
  810. break;
  811. }
  812. if (elem_parse_failed)
  813. elems->parse_error = true;
  814. else
  815. __set_bit(id, seen_elems);
  816. left -= elen;
  817. pos += elen;
  818. }
  819. if (left != 0)
  820. elems->parse_error = true;
  821. return crc;
  822. }
  823. void ieee80211_set_wmm_default(struct ieee80211_sub_if_data *sdata,
  824. bool bss_notify)
  825. {
  826. struct ieee80211_local *local = sdata->local;
  827. struct ieee80211_tx_queue_params qparam;
  828. struct ieee80211_chanctx_conf *chanctx_conf;
  829. int ac;
  830. bool use_11b, enable_qos;
  831. int aCWmin, aCWmax;
  832. if (!local->ops->conf_tx)
  833. return;
  834. if (local->hw.queues < IEEE80211_NUM_ACS)
  835. return;
  836. memset(&qparam, 0, sizeof(qparam));
  837. rcu_read_lock();
  838. chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
  839. use_11b = (chanctx_conf &&
  840. chanctx_conf->def.chan->band == IEEE80211_BAND_2GHZ) &&
  841. !(sdata->flags & IEEE80211_SDATA_OPERATING_GMODE);
  842. rcu_read_unlock();
  843. /*
  844. * By default disable QoS in STA mode for old access points, which do
  845. * not support 802.11e. New APs will provide proper queue parameters,
  846. * that we will configure later.
  847. */
  848. enable_qos = (sdata->vif.type != NL80211_IFTYPE_STATION);
  849. for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
  850. /* Set defaults according to 802.11-2007 Table 7-37 */
  851. aCWmax = 1023;
  852. if (use_11b)
  853. aCWmin = 31;
  854. else
  855. aCWmin = 15;
  856. if (enable_qos) {
  857. switch (ac) {
  858. case IEEE80211_AC_BK:
  859. qparam.cw_max = aCWmax;
  860. qparam.cw_min = aCWmin;
  861. qparam.txop = 0;
  862. qparam.aifs = 7;
  863. break;
  864. /* never happens but let's not leave undefined */
  865. default:
  866. case IEEE80211_AC_BE:
  867. qparam.cw_max = aCWmax;
  868. qparam.cw_min = aCWmin;
  869. qparam.txop = 0;
  870. qparam.aifs = 3;
  871. break;
  872. case IEEE80211_AC_VI:
  873. qparam.cw_max = aCWmin;
  874. qparam.cw_min = (aCWmin + 1) / 2 - 1;
  875. if (use_11b)
  876. qparam.txop = 6016/32;
  877. else
  878. qparam.txop = 3008/32;
  879. qparam.aifs = 2;
  880. break;
  881. case IEEE80211_AC_VO:
  882. qparam.cw_max = (aCWmin + 1) / 2 - 1;
  883. qparam.cw_min = (aCWmin + 1) / 4 - 1;
  884. if (use_11b)
  885. qparam.txop = 3264/32;
  886. else
  887. qparam.txop = 1504/32;
  888. qparam.aifs = 2;
  889. break;
  890. }
  891. } else {
  892. /* Confiure old 802.11b/g medium access rules. */
  893. qparam.cw_max = aCWmax;
  894. qparam.cw_min = aCWmin;
  895. qparam.txop = 0;
  896. qparam.aifs = 2;
  897. }
  898. qparam.uapsd = false;
  899. sdata->tx_conf[ac] = qparam;
  900. drv_conf_tx(local, sdata, ac, &qparam);
  901. }
  902. if (sdata->vif.type != NL80211_IFTYPE_MONITOR &&
  903. sdata->vif.type != NL80211_IFTYPE_P2P_DEVICE) {
  904. sdata->vif.bss_conf.qos = enable_qos;
  905. if (bss_notify)
  906. ieee80211_bss_info_change_notify(sdata,
  907. BSS_CHANGED_QOS);
  908. }
  909. }
  910. void ieee80211_sta_def_wmm_params(struct ieee80211_sub_if_data *sdata,
  911. const size_t supp_rates_len,
  912. const u8 *supp_rates)
  913. {
  914. struct ieee80211_chanctx_conf *chanctx_conf;
  915. int i, have_higher_than_11mbit = 0;
  916. /* cf. IEEE 802.11 9.2.12 */
  917. for (i = 0; i < supp_rates_len; i++)
  918. if ((supp_rates[i] & 0x7f) * 5 > 110)
  919. have_higher_than_11mbit = 1;
  920. rcu_read_lock();
  921. chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
  922. if (chanctx_conf &&
  923. chanctx_conf->def.chan->band == IEEE80211_BAND_2GHZ &&
  924. have_higher_than_11mbit)
  925. sdata->flags |= IEEE80211_SDATA_OPERATING_GMODE;
  926. else
  927. sdata->flags &= ~IEEE80211_SDATA_OPERATING_GMODE;
  928. rcu_read_unlock();
  929. ieee80211_set_wmm_default(sdata, true);
  930. }
  931. void ieee80211_send_auth(struct ieee80211_sub_if_data *sdata,
  932. u16 transaction, u16 auth_alg, u16 status,
  933. const u8 *extra, size_t extra_len, const u8 *da,
  934. const u8 *bssid, const u8 *key, u8 key_len, u8 key_idx,
  935. u32 tx_flags)
  936. {
  937. struct ieee80211_local *local = sdata->local;
  938. struct sk_buff *skb;
  939. struct ieee80211_mgmt *mgmt;
  940. int err;
  941. skb = dev_alloc_skb(local->hw.extra_tx_headroom +
  942. sizeof(*mgmt) + 6 + extra_len);
  943. if (!skb)
  944. return;
  945. skb_reserve(skb, local->hw.extra_tx_headroom);
  946. mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24 + 6);
  947. memset(mgmt, 0, 24 + 6);
  948. mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
  949. IEEE80211_STYPE_AUTH);
  950. memcpy(mgmt->da, da, ETH_ALEN);
  951. memcpy(mgmt->sa, sdata->vif.addr, ETH_ALEN);
  952. memcpy(mgmt->bssid, bssid, ETH_ALEN);
  953. mgmt->u.auth.auth_alg = cpu_to_le16(auth_alg);
  954. mgmt->u.auth.auth_transaction = cpu_to_le16(transaction);
  955. mgmt->u.auth.status_code = cpu_to_le16(status);
  956. if (extra)
  957. memcpy(skb_put(skb, extra_len), extra, extra_len);
  958. if (auth_alg == WLAN_AUTH_SHARED_KEY && transaction == 3) {
  959. mgmt->frame_control |= cpu_to_le16(IEEE80211_FCTL_PROTECTED);
  960. err = ieee80211_wep_encrypt(local, skb, key, key_len, key_idx);
  961. WARN_ON(err);
  962. }
  963. IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT |
  964. tx_flags;
  965. ieee80211_tx_skb(sdata, skb);
  966. }
  967. void ieee80211_send_deauth_disassoc(struct ieee80211_sub_if_data *sdata,
  968. const u8 *bssid, u16 stype, u16 reason,
  969. bool send_frame, u8 *frame_buf)
  970. {
  971. struct ieee80211_local *local = sdata->local;
  972. struct sk_buff *skb;
  973. struct ieee80211_mgmt *mgmt = (void *)frame_buf;
  974. /* build frame */
  975. mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT | stype);
  976. mgmt->duration = 0; /* initialize only */
  977. mgmt->seq_ctrl = 0; /* initialize only */
  978. memcpy(mgmt->da, bssid, ETH_ALEN);
  979. memcpy(mgmt->sa, sdata->vif.addr, ETH_ALEN);
  980. memcpy(mgmt->bssid, bssid, ETH_ALEN);
  981. /* u.deauth.reason_code == u.disassoc.reason_code */
  982. mgmt->u.deauth.reason_code = cpu_to_le16(reason);
  983. if (send_frame) {
  984. skb = dev_alloc_skb(local->hw.extra_tx_headroom +
  985. IEEE80211_DEAUTH_FRAME_LEN);
  986. if (!skb)
  987. return;
  988. skb_reserve(skb, local->hw.extra_tx_headroom);
  989. /* copy in frame */
  990. memcpy(skb_put(skb, IEEE80211_DEAUTH_FRAME_LEN),
  991. mgmt, IEEE80211_DEAUTH_FRAME_LEN);
  992. if (sdata->vif.type != NL80211_IFTYPE_STATION ||
  993. !(sdata->u.mgd.flags & IEEE80211_STA_MFP_ENABLED))
  994. IEEE80211_SKB_CB(skb)->flags |=
  995. IEEE80211_TX_INTFL_DONT_ENCRYPT;
  996. ieee80211_tx_skb(sdata, skb);
  997. }
  998. }
  999. int ieee80211_build_preq_ies(struct ieee80211_local *local, u8 *buffer,
  1000. size_t buffer_len, const u8 *ie, size_t ie_len,
  1001. enum ieee80211_band band, u32 rate_mask,
  1002. u8 channel)
  1003. {
  1004. struct ieee80211_supported_band *sband;
  1005. u8 *pos = buffer, *end = buffer + buffer_len;
  1006. size_t offset = 0, noffset;
  1007. int supp_rates_len, i;
  1008. u8 rates[32];
  1009. int num_rates;
  1010. int ext_rates_len;
  1011. sband = local->hw.wiphy->bands[band];
  1012. if (WARN_ON_ONCE(!sband))
  1013. return 0;
  1014. num_rates = 0;
  1015. for (i = 0; i < sband->n_bitrates; i++) {
  1016. if ((BIT(i) & rate_mask) == 0)
  1017. continue; /* skip rate */
  1018. rates[num_rates++] = (u8) (sband->bitrates[i].bitrate / 5);
  1019. }
  1020. supp_rates_len = min_t(int, num_rates, 8);
  1021. if (end - pos < 2 + supp_rates_len)
  1022. goto out_err;
  1023. *pos++ = WLAN_EID_SUPP_RATES;
  1024. *pos++ = supp_rates_len;
  1025. memcpy(pos, rates, supp_rates_len);
  1026. pos += supp_rates_len;
  1027. /* insert "request information" if in custom IEs */
  1028. if (ie && ie_len) {
  1029. static const u8 before_extrates[] = {
  1030. WLAN_EID_SSID,
  1031. WLAN_EID_SUPP_RATES,
  1032. WLAN_EID_REQUEST,
  1033. };
  1034. noffset = ieee80211_ie_split(ie, ie_len,
  1035. before_extrates,
  1036. ARRAY_SIZE(before_extrates),
  1037. offset);
  1038. if (end - pos < noffset - offset)
  1039. goto out_err;
  1040. memcpy(pos, ie + offset, noffset - offset);
  1041. pos += noffset - offset;
  1042. offset = noffset;
  1043. }
  1044. ext_rates_len = num_rates - supp_rates_len;
  1045. if (ext_rates_len > 0) {
  1046. if (end - pos < 2 + ext_rates_len)
  1047. goto out_err;
  1048. *pos++ = WLAN_EID_EXT_SUPP_RATES;
  1049. *pos++ = ext_rates_len;
  1050. memcpy(pos, rates + supp_rates_len, ext_rates_len);
  1051. pos += ext_rates_len;
  1052. }
  1053. if (channel && sband->band == IEEE80211_BAND_2GHZ) {
  1054. if (end - pos < 3)
  1055. goto out_err;
  1056. *pos++ = WLAN_EID_DS_PARAMS;
  1057. *pos++ = 1;
  1058. *pos++ = channel;
  1059. }
  1060. /* insert custom IEs that go before HT */
  1061. if (ie && ie_len) {
  1062. static const u8 before_ht[] = {
  1063. WLAN_EID_SSID,
  1064. WLAN_EID_SUPP_RATES,
  1065. WLAN_EID_REQUEST,
  1066. WLAN_EID_EXT_SUPP_RATES,
  1067. WLAN_EID_DS_PARAMS,
  1068. WLAN_EID_SUPPORTED_REGULATORY_CLASSES,
  1069. };
  1070. noffset = ieee80211_ie_split(ie, ie_len,
  1071. before_ht, ARRAY_SIZE(before_ht),
  1072. offset);
  1073. if (end - pos < noffset - offset)
  1074. goto out_err;
  1075. memcpy(pos, ie + offset, noffset - offset);
  1076. pos += noffset - offset;
  1077. offset = noffset;
  1078. }
  1079. if (sband->ht_cap.ht_supported) {
  1080. if (end - pos < 2 + sizeof(struct ieee80211_ht_cap))
  1081. goto out_err;
  1082. pos = ieee80211_ie_build_ht_cap(pos, &sband->ht_cap,
  1083. sband->ht_cap.cap);
  1084. }
  1085. /*
  1086. * If adding more here, adjust code in main.c
  1087. * that calculates local->scan_ies_len.
  1088. */
  1089. /* add any remaining custom IEs */
  1090. if (ie && ie_len) {
  1091. noffset = ie_len;
  1092. if (end - pos < noffset - offset)
  1093. goto out_err;
  1094. memcpy(pos, ie + offset, noffset - offset);
  1095. pos += noffset - offset;
  1096. }
  1097. if (sband->vht_cap.vht_supported) {
  1098. if (end - pos < 2 + sizeof(struct ieee80211_vht_cap))
  1099. goto out_err;
  1100. pos = ieee80211_ie_build_vht_cap(pos, &sband->vht_cap,
  1101. sband->vht_cap.cap);
  1102. }
  1103. return pos - buffer;
  1104. out_err:
  1105. WARN_ONCE(1, "not enough space for preq IEs\n");
  1106. return pos - buffer;
  1107. }
  1108. struct sk_buff *ieee80211_build_probe_req(struct ieee80211_sub_if_data *sdata,
  1109. u8 *dst, u32 ratemask,
  1110. struct ieee80211_channel *chan,
  1111. const u8 *ssid, size_t ssid_len,
  1112. const u8 *ie, size_t ie_len,
  1113. bool directed)
  1114. {
  1115. struct ieee80211_local *local = sdata->local;
  1116. struct sk_buff *skb;
  1117. struct ieee80211_mgmt *mgmt;
  1118. u8 chan_no;
  1119. int ies_len;
  1120. /*
  1121. * Do not send DS Channel parameter for directed probe requests
  1122. * in order to maximize the chance that we get a response. Some
  1123. * badly-behaved APs don't respond when this parameter is included.
  1124. */
  1125. if (directed)
  1126. chan_no = 0;
  1127. else
  1128. chan_no = ieee80211_frequency_to_channel(chan->center_freq);
  1129. skb = ieee80211_probereq_get(&local->hw, &sdata->vif,
  1130. ssid, ssid_len, 100 + ie_len);
  1131. if (!skb)
  1132. return NULL;
  1133. ies_len = ieee80211_build_preq_ies(local, skb_tail_pointer(skb),
  1134. skb_tailroom(skb),
  1135. ie, ie_len, chan->band,
  1136. ratemask, chan_no);
  1137. skb_put(skb, ies_len);
  1138. if (dst) {
  1139. mgmt = (struct ieee80211_mgmt *) skb->data;
  1140. memcpy(mgmt->da, dst, ETH_ALEN);
  1141. memcpy(mgmt->bssid, dst, ETH_ALEN);
  1142. }
  1143. IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
  1144. return skb;
  1145. }
  1146. void ieee80211_send_probe_req(struct ieee80211_sub_if_data *sdata, u8 *dst,
  1147. const u8 *ssid, size_t ssid_len,
  1148. const u8 *ie, size_t ie_len,
  1149. u32 ratemask, bool directed, u32 tx_flags,
  1150. struct ieee80211_channel *channel, bool scan)
  1151. {
  1152. struct sk_buff *skb;
  1153. skb = ieee80211_build_probe_req(sdata, dst, ratemask, channel,
  1154. ssid, ssid_len,
  1155. ie, ie_len, directed);
  1156. if (skb) {
  1157. IEEE80211_SKB_CB(skb)->flags |= tx_flags;
  1158. if (scan)
  1159. ieee80211_tx_skb_tid_band(sdata, skb, 7, channel->band);
  1160. else
  1161. ieee80211_tx_skb(sdata, skb);
  1162. }
  1163. }
  1164. u32 ieee80211_sta_get_rates(struct ieee80211_local *local,
  1165. struct ieee802_11_elems *elems,
  1166. enum ieee80211_band band, u32 *basic_rates)
  1167. {
  1168. struct ieee80211_supported_band *sband;
  1169. struct ieee80211_rate *bitrates;
  1170. size_t num_rates;
  1171. u32 supp_rates;
  1172. int i, j;
  1173. sband = local->hw.wiphy->bands[band];
  1174. if (WARN_ON(!sband))
  1175. return 1;
  1176. bitrates = sband->bitrates;
  1177. num_rates = sband->n_bitrates;
  1178. supp_rates = 0;
  1179. for (i = 0; i < elems->supp_rates_len +
  1180. elems->ext_supp_rates_len; i++) {
  1181. u8 rate = 0;
  1182. int own_rate;
  1183. bool is_basic;
  1184. if (i < elems->supp_rates_len)
  1185. rate = elems->supp_rates[i];
  1186. else if (elems->ext_supp_rates)
  1187. rate = elems->ext_supp_rates
  1188. [i - elems->supp_rates_len];
  1189. own_rate = 5 * (rate & 0x7f);
  1190. is_basic = !!(rate & 0x80);
  1191. if (is_basic && (rate & 0x7f) == BSS_MEMBERSHIP_SELECTOR_HT_PHY)
  1192. continue;
  1193. for (j = 0; j < num_rates; j++) {
  1194. if (bitrates[j].bitrate == own_rate) {
  1195. supp_rates |= BIT(j);
  1196. if (basic_rates && is_basic)
  1197. *basic_rates |= BIT(j);
  1198. }
  1199. }
  1200. }
  1201. return supp_rates;
  1202. }
  1203. void ieee80211_stop_device(struct ieee80211_local *local)
  1204. {
  1205. ieee80211_led_radio(local, false);
  1206. ieee80211_mod_tpt_led_trig(local, 0, IEEE80211_TPT_LEDTRIG_FL_RADIO);
  1207. cancel_work_sync(&local->reconfig_filter);
  1208. flush_workqueue(local->workqueue);
  1209. drv_stop(local);
  1210. }
  1211. static void ieee80211_assign_chanctx(struct ieee80211_local *local,
  1212. struct ieee80211_sub_if_data *sdata)
  1213. {
  1214. struct ieee80211_chanctx_conf *conf;
  1215. struct ieee80211_chanctx *ctx;
  1216. if (!local->use_chanctx)
  1217. return;
  1218. mutex_lock(&local->chanctx_mtx);
  1219. conf = rcu_dereference_protected(sdata->vif.chanctx_conf,
  1220. lockdep_is_held(&local->chanctx_mtx));
  1221. if (conf) {
  1222. ctx = container_of(conf, struct ieee80211_chanctx, conf);
  1223. drv_assign_vif_chanctx(local, sdata, ctx);
  1224. }
  1225. mutex_unlock(&local->chanctx_mtx);
  1226. }
  1227. int ieee80211_reconfig(struct ieee80211_local *local)
  1228. {
  1229. struct ieee80211_hw *hw = &local->hw;
  1230. struct ieee80211_sub_if_data *sdata;
  1231. struct ieee80211_chanctx *ctx;
  1232. struct sta_info *sta;
  1233. int res, i;
  1234. bool reconfig_due_to_wowlan = false;
  1235. #ifdef CONFIG_PM
  1236. if (local->suspended)
  1237. local->resuming = true;
  1238. if (local->wowlan) {
  1239. local->wowlan = false;
  1240. res = drv_resume(local);
  1241. if (res < 0) {
  1242. local->resuming = false;
  1243. return res;
  1244. }
  1245. if (res == 0)
  1246. goto wake_up;
  1247. WARN_ON(res > 1);
  1248. /*
  1249. * res is 1, which means the driver requested
  1250. * to go through a regular reset on wakeup.
  1251. */
  1252. reconfig_due_to_wowlan = true;
  1253. }
  1254. #endif
  1255. /* everything else happens only if HW was up & running */
  1256. if (!local->open_count)
  1257. goto wake_up;
  1258. /*
  1259. * Upon resume hardware can sometimes be goofy due to
  1260. * various platform / driver / bus issues, so restarting
  1261. * the device may at times not work immediately. Propagate
  1262. * the error.
  1263. */
  1264. res = drv_start(local);
  1265. if (res) {
  1266. WARN(local->suspended, "Hardware became unavailable "
  1267. "upon resume. This could be a software issue "
  1268. "prior to suspend or a hardware issue.\n");
  1269. return res;
  1270. }
  1271. /* setup fragmentation threshold */
  1272. drv_set_frag_threshold(local, hw->wiphy->frag_threshold);
  1273. /* setup RTS threshold */
  1274. drv_set_rts_threshold(local, hw->wiphy->rts_threshold);
  1275. /* reset coverage class */
  1276. drv_set_coverage_class(local, hw->wiphy->coverage_class);
  1277. ieee80211_led_radio(local, true);
  1278. ieee80211_mod_tpt_led_trig(local,
  1279. IEEE80211_TPT_LEDTRIG_FL_RADIO, 0);
  1280. /* add interfaces */
  1281. sdata = rtnl_dereference(local->monitor_sdata);
  1282. if (sdata) {
  1283. /* in HW restart it exists already */
  1284. WARN_ON(local->resuming);
  1285. res = drv_add_interface(local, sdata);
  1286. if (WARN_ON(res)) {
  1287. rcu_assign_pointer(local->monitor_sdata, NULL);
  1288. synchronize_net();
  1289. kfree(sdata);
  1290. }
  1291. }
  1292. list_for_each_entry(sdata, &local->interfaces, list) {
  1293. if (sdata->vif.type != NL80211_IFTYPE_AP_VLAN &&
  1294. sdata->vif.type != NL80211_IFTYPE_MONITOR &&
  1295. ieee80211_sdata_running(sdata))
  1296. res = drv_add_interface(local, sdata);
  1297. }
  1298. /* add channel contexts */
  1299. if (local->use_chanctx) {
  1300. mutex_lock(&local->chanctx_mtx);
  1301. list_for_each_entry(ctx, &local->chanctx_list, list)
  1302. WARN_ON(drv_add_chanctx(local, ctx));
  1303. mutex_unlock(&local->chanctx_mtx);
  1304. }
  1305. list_for_each_entry(sdata, &local->interfaces, list) {
  1306. if (!ieee80211_sdata_running(sdata))
  1307. continue;
  1308. ieee80211_assign_chanctx(local, sdata);
  1309. }
  1310. sdata = rtnl_dereference(local->monitor_sdata);
  1311. if (sdata && ieee80211_sdata_running(sdata))
  1312. ieee80211_assign_chanctx(local, sdata);
  1313. /* add STAs back */
  1314. mutex_lock(&local->sta_mtx);
  1315. list_for_each_entry(sta, &local->sta_list, list) {
  1316. enum ieee80211_sta_state state;
  1317. if (!sta->uploaded)
  1318. continue;
  1319. /* AP-mode stations will be added later */
  1320. if (sta->sdata->vif.type == NL80211_IFTYPE_AP)
  1321. continue;
  1322. for (state = IEEE80211_STA_NOTEXIST;
  1323. state < sta->sta_state; state++)
  1324. WARN_ON(drv_sta_state(local, sta->sdata, sta, state,
  1325. state + 1));
  1326. }
  1327. mutex_unlock(&local->sta_mtx);
  1328. /* reconfigure tx conf */
  1329. if (hw->queues >= IEEE80211_NUM_ACS) {
  1330. list_for_each_entry(sdata, &local->interfaces, list) {
  1331. if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN ||
  1332. sdata->vif.type == NL80211_IFTYPE_MONITOR ||
  1333. !ieee80211_sdata_running(sdata))
  1334. continue;
  1335. for (i = 0; i < IEEE80211_NUM_ACS; i++)
  1336. drv_conf_tx(local, sdata, i,
  1337. &sdata->tx_conf[i]);
  1338. }
  1339. }
  1340. /* reconfigure hardware */
  1341. ieee80211_hw_config(local, ~0);
  1342. ieee80211_configure_filter(local);
  1343. /* Finally also reconfigure all the BSS information */
  1344. list_for_each_entry(sdata, &local->interfaces, list) {
  1345. u32 changed;
  1346. if (!ieee80211_sdata_running(sdata))
  1347. continue;
  1348. /* common change flags for all interface types */
  1349. changed = BSS_CHANGED_ERP_CTS_PROT |
  1350. BSS_CHANGED_ERP_PREAMBLE |
  1351. BSS_CHANGED_ERP_SLOT |
  1352. BSS_CHANGED_HT |
  1353. BSS_CHANGED_BASIC_RATES |
  1354. BSS_CHANGED_BEACON_INT |
  1355. BSS_CHANGED_BSSID |
  1356. BSS_CHANGED_CQM |
  1357. BSS_CHANGED_QOS |
  1358. BSS_CHANGED_IDLE |
  1359. BSS_CHANGED_TXPOWER;
  1360. switch (sdata->vif.type) {
  1361. case NL80211_IFTYPE_STATION:
  1362. changed |= BSS_CHANGED_ASSOC |
  1363. BSS_CHANGED_ARP_FILTER |
  1364. BSS_CHANGED_PS;
  1365. /* Re-send beacon info report to the driver */
  1366. if (sdata->u.mgd.have_beacon)
  1367. changed |= BSS_CHANGED_BEACON_INFO;
  1368. sdata_lock(sdata);
  1369. ieee80211_bss_info_change_notify(sdata, changed);
  1370. sdata_unlock(sdata);
  1371. break;
  1372. case NL80211_IFTYPE_ADHOC:
  1373. changed |= BSS_CHANGED_IBSS;
  1374. /* fall through */
  1375. case NL80211_IFTYPE_AP:
  1376. changed |= BSS_CHANGED_SSID | BSS_CHANGED_P2P_PS;
  1377. if (sdata->vif.type == NL80211_IFTYPE_AP) {
  1378. changed |= BSS_CHANGED_AP_PROBE_RESP;
  1379. if (rcu_access_pointer(sdata->u.ap.beacon))
  1380. drv_start_ap(local, sdata);
  1381. }
  1382. /* fall through */
  1383. case NL80211_IFTYPE_MESH_POINT:
  1384. if (sdata->vif.bss_conf.enable_beacon) {
  1385. changed |= BSS_CHANGED_BEACON |
  1386. BSS_CHANGED_BEACON_ENABLED;
  1387. ieee80211_bss_info_change_notify(sdata, changed);
  1388. }
  1389. break;
  1390. case NL80211_IFTYPE_WDS:
  1391. break;
  1392. case NL80211_IFTYPE_AP_VLAN:
  1393. case NL80211_IFTYPE_MONITOR:
  1394. /* ignore virtual */
  1395. break;
  1396. case NL80211_IFTYPE_P2P_DEVICE:
  1397. changed = BSS_CHANGED_IDLE;
  1398. break;
  1399. case NL80211_IFTYPE_UNSPECIFIED:
  1400. case NUM_NL80211_IFTYPES:
  1401. case NL80211_IFTYPE_P2P_CLIENT:
  1402. case NL80211_IFTYPE_P2P_GO:
  1403. WARN_ON(1);
  1404. break;
  1405. }
  1406. }
  1407. ieee80211_recalc_ps(local, -1);
  1408. /*
  1409. * The sta might be in psm against the ap (e.g. because
  1410. * this was the state before a hw restart), so we
  1411. * explicitly send a null packet in order to make sure
  1412. * it'll sync against the ap (and get out of psm).
  1413. */
  1414. if (!(local->hw.conf.flags & IEEE80211_CONF_PS)) {
  1415. list_for_each_entry(sdata, &local->interfaces, list) {
  1416. if (sdata->vif.type != NL80211_IFTYPE_STATION)
  1417. continue;
  1418. if (!sdata->u.mgd.associated)
  1419. continue;
  1420. ieee80211_send_nullfunc(local, sdata, 0);
  1421. }
  1422. }
  1423. /* APs are now beaconing, add back stations */
  1424. mutex_lock(&local->sta_mtx);
  1425. list_for_each_entry(sta, &local->sta_list, list) {
  1426. enum ieee80211_sta_state state;
  1427. if (!sta->uploaded)
  1428. continue;
  1429. if (sta->sdata->vif.type != NL80211_IFTYPE_AP)
  1430. continue;
  1431. for (state = IEEE80211_STA_NOTEXIST;
  1432. state < sta->sta_state; state++)
  1433. WARN_ON(drv_sta_state(local, sta->sdata, sta, state,
  1434. state + 1));
  1435. }
  1436. mutex_unlock(&local->sta_mtx);
  1437. /* add back keys */
  1438. list_for_each_entry(sdata, &local->interfaces, list)
  1439. if (ieee80211_sdata_running(sdata))
  1440. ieee80211_enable_keys(sdata);
  1441. wake_up:
  1442. local->in_reconfig = false;
  1443. barrier();
  1444. if (local->monitors == local->open_count && local->monitors > 0)
  1445. ieee80211_add_virtual_monitor(local);
  1446. /*
  1447. * Clear the WLAN_STA_BLOCK_BA flag so new aggregation
  1448. * sessions can be established after a resume.
  1449. *
  1450. * Also tear down aggregation sessions since reconfiguring
  1451. * them in a hardware restart scenario is not easily done
  1452. * right now, and the hardware will have lost information
  1453. * about the sessions, but we and the AP still think they
  1454. * are active. This is really a workaround though.
  1455. */
  1456. if (hw->flags & IEEE80211_HW_AMPDU_AGGREGATION) {
  1457. mutex_lock(&local->sta_mtx);
  1458. list_for_each_entry(sta, &local->sta_list, list) {
  1459. ieee80211_sta_tear_down_BA_sessions(
  1460. sta, AGG_STOP_LOCAL_REQUEST);
  1461. clear_sta_flag(sta, WLAN_STA_BLOCK_BA);
  1462. }
  1463. mutex_unlock(&local->sta_mtx);
  1464. }
  1465. ieee80211_wake_queues_by_reason(hw, IEEE80211_MAX_QUEUE_MAP,
  1466. IEEE80211_QUEUE_STOP_REASON_SUSPEND);
  1467. /*
  1468. * If this is for hw restart things are still running.
  1469. * We may want to change that later, however.
  1470. */
  1471. if (!local->suspended || reconfig_due_to_wowlan)
  1472. drv_restart_complete(local);
  1473. if (!local->suspended)
  1474. return 0;
  1475. #ifdef CONFIG_PM
  1476. /* first set suspended false, then resuming */
  1477. local->suspended = false;
  1478. mb();
  1479. local->resuming = false;
  1480. list_for_each_entry(sdata, &local->interfaces, list) {
  1481. if (!ieee80211_sdata_running(sdata))
  1482. continue;
  1483. if (sdata->vif.type == NL80211_IFTYPE_STATION)
  1484. ieee80211_sta_restart(sdata);
  1485. }
  1486. mod_timer(&local->sta_cleanup, jiffies + 1);
  1487. #else
  1488. WARN_ON(1);
  1489. #endif
  1490. return 0;
  1491. }
  1492. void ieee80211_resume_disconnect(struct ieee80211_vif *vif)
  1493. {
  1494. struct ieee80211_sub_if_data *sdata;
  1495. struct ieee80211_local *local;
  1496. struct ieee80211_key *key;
  1497. if (WARN_ON(!vif))
  1498. return;
  1499. sdata = vif_to_sdata(vif);
  1500. local = sdata->local;
  1501. if (WARN_ON(!local->resuming))
  1502. return;
  1503. if (WARN_ON(vif->type != NL80211_IFTYPE_STATION))
  1504. return;
  1505. sdata->flags |= IEEE80211_SDATA_DISCONNECT_RESUME;
  1506. mutex_lock(&local->key_mtx);
  1507. list_for_each_entry(key, &sdata->key_list, list)
  1508. key->flags |= KEY_FLAG_TAINTED;
  1509. mutex_unlock(&local->key_mtx);
  1510. }
  1511. EXPORT_SYMBOL_GPL(ieee80211_resume_disconnect);
  1512. void ieee80211_recalc_smps(struct ieee80211_sub_if_data *sdata)
  1513. {
  1514. struct ieee80211_local *local = sdata->local;
  1515. struct ieee80211_chanctx_conf *chanctx_conf;
  1516. struct ieee80211_chanctx *chanctx;
  1517. mutex_lock(&local->chanctx_mtx);
  1518. chanctx_conf = rcu_dereference_protected(sdata->vif.chanctx_conf,
  1519. lockdep_is_held(&local->chanctx_mtx));
  1520. if (WARN_ON_ONCE(!chanctx_conf))
  1521. goto unlock;
  1522. chanctx = container_of(chanctx_conf, struct ieee80211_chanctx, conf);
  1523. ieee80211_recalc_smps_chanctx(local, chanctx);
  1524. unlock:
  1525. mutex_unlock(&local->chanctx_mtx);
  1526. }
  1527. static bool ieee80211_id_in_list(const u8 *ids, int n_ids, u8 id)
  1528. {
  1529. int i;
  1530. for (i = 0; i < n_ids; i++)
  1531. if (ids[i] == id)
  1532. return true;
  1533. return false;
  1534. }
  1535. /**
  1536. * ieee80211_ie_split - split an IE buffer according to ordering
  1537. *
  1538. * @ies: the IE buffer
  1539. * @ielen: the length of the IE buffer
  1540. * @ids: an array with element IDs that are allowed before
  1541. * the split
  1542. * @n_ids: the size of the element ID array
  1543. * @offset: offset where to start splitting in the buffer
  1544. *
  1545. * This function splits an IE buffer by updating the @offset
  1546. * variable to point to the location where the buffer should be
  1547. * split.
  1548. *
  1549. * It assumes that the given IE buffer is well-formed, this
  1550. * has to be guaranteed by the caller!
  1551. *
  1552. * It also assumes that the IEs in the buffer are ordered
  1553. * correctly, if not the result of using this function will not
  1554. * be ordered correctly either, i.e. it does no reordering.
  1555. *
  1556. * The function returns the offset where the next part of the
  1557. * buffer starts, which may be @ielen if the entire (remainder)
  1558. * of the buffer should be used.
  1559. */
  1560. size_t ieee80211_ie_split(const u8 *ies, size_t ielen,
  1561. const u8 *ids, int n_ids, size_t offset)
  1562. {
  1563. size_t pos = offset;
  1564. while (pos < ielen && ieee80211_id_in_list(ids, n_ids, ies[pos]))
  1565. pos += 2 + ies[pos + 1];
  1566. return pos;
  1567. }
  1568. size_t ieee80211_ie_split_vendor(const u8 *ies, size_t ielen, size_t offset)
  1569. {
  1570. size_t pos = offset;
  1571. while (pos < ielen && ies[pos] != WLAN_EID_VENDOR_SPECIFIC)
  1572. pos += 2 + ies[pos + 1];
  1573. return pos;
  1574. }
  1575. static void _ieee80211_enable_rssi_reports(struct ieee80211_sub_if_data *sdata,
  1576. int rssi_min_thold,
  1577. int rssi_max_thold)
  1578. {
  1579. trace_api_enable_rssi_reports(sdata, rssi_min_thold, rssi_max_thold);
  1580. if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_STATION))
  1581. return;
  1582. /*
  1583. * Scale up threshold values before storing it, as the RSSI averaging
  1584. * algorithm uses a scaled up value as well. Change this scaling
  1585. * factor if the RSSI averaging algorithm changes.
  1586. */
  1587. sdata->u.mgd.rssi_min_thold = rssi_min_thold*16;
  1588. sdata->u.mgd.rssi_max_thold = rssi_max_thold*16;
  1589. }
  1590. void ieee80211_enable_rssi_reports(struct ieee80211_vif *vif,
  1591. int rssi_min_thold,
  1592. int rssi_max_thold)
  1593. {
  1594. struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
  1595. WARN_ON(rssi_min_thold == rssi_max_thold ||
  1596. rssi_min_thold > rssi_max_thold);
  1597. _ieee80211_enable_rssi_reports(sdata, rssi_min_thold,
  1598. rssi_max_thold);
  1599. }
  1600. EXPORT_SYMBOL(ieee80211_enable_rssi_reports);
  1601. void ieee80211_disable_rssi_reports(struct ieee80211_vif *vif)
  1602. {
  1603. struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
  1604. _ieee80211_enable_rssi_reports(sdata, 0, 0);
  1605. }
  1606. EXPORT_SYMBOL(ieee80211_disable_rssi_reports);
  1607. u8 *ieee80211_ie_build_ht_cap(u8 *pos, struct ieee80211_sta_ht_cap *ht_cap,
  1608. u16 cap)
  1609. {
  1610. __le16 tmp;
  1611. *pos++ = WLAN_EID_HT_CAPABILITY;
  1612. *pos++ = sizeof(struct ieee80211_ht_cap);
  1613. memset(pos, 0, sizeof(struct ieee80211_ht_cap));
  1614. /* capability flags */
  1615. tmp = cpu_to_le16(cap);
  1616. memcpy(pos, &tmp, sizeof(u16));
  1617. pos += sizeof(u16);
  1618. /* AMPDU parameters */
  1619. *pos++ = ht_cap->ampdu_factor |
  1620. (ht_cap->ampdu_density <<
  1621. IEEE80211_HT_AMPDU_PARM_DENSITY_SHIFT);
  1622. /* MCS set */
  1623. memcpy(pos, &ht_cap->mcs, sizeof(ht_cap->mcs));
  1624. pos += sizeof(ht_cap->mcs);
  1625. /* extended capabilities */
  1626. pos += sizeof(__le16);
  1627. /* BF capabilities */
  1628. pos += sizeof(__le32);
  1629. /* antenna selection */
  1630. pos += sizeof(u8);
  1631. return pos;
  1632. }
  1633. u8 *ieee80211_ie_build_vht_cap(u8 *pos, struct ieee80211_sta_vht_cap *vht_cap,
  1634. u32 cap)
  1635. {
  1636. __le32 tmp;
  1637. *pos++ = WLAN_EID_VHT_CAPABILITY;
  1638. *pos++ = sizeof(struct ieee80211_vht_cap);
  1639. memset(pos, 0, sizeof(struct ieee80211_vht_cap));
  1640. /* capability flags */
  1641. tmp = cpu_to_le32(cap);
  1642. memcpy(pos, &tmp, sizeof(u32));
  1643. pos += sizeof(u32);
  1644. /* VHT MCS set */
  1645. memcpy(pos, &vht_cap->vht_mcs, sizeof(vht_cap->vht_mcs));
  1646. pos += sizeof(vht_cap->vht_mcs);
  1647. return pos;
  1648. }
  1649. u8 *ieee80211_ie_build_ht_oper(u8 *pos, struct ieee80211_sta_ht_cap *ht_cap,
  1650. const struct cfg80211_chan_def *chandef,
  1651. u16 prot_mode)
  1652. {
  1653. struct ieee80211_ht_operation *ht_oper;
  1654. /* Build HT Information */
  1655. *pos++ = WLAN_EID_HT_OPERATION;
  1656. *pos++ = sizeof(struct ieee80211_ht_operation);
  1657. ht_oper = (struct ieee80211_ht_operation *)pos;
  1658. ht_oper->primary_chan = ieee80211_frequency_to_channel(
  1659. chandef->chan->center_freq);
  1660. switch (chandef->width) {
  1661. case NL80211_CHAN_WIDTH_160:
  1662. case NL80211_CHAN_WIDTH_80P80:
  1663. case NL80211_CHAN_WIDTH_80:
  1664. case NL80211_CHAN_WIDTH_40:
  1665. if (chandef->center_freq1 > chandef->chan->center_freq)
  1666. ht_oper->ht_param = IEEE80211_HT_PARAM_CHA_SEC_ABOVE;
  1667. else
  1668. ht_oper->ht_param = IEEE80211_HT_PARAM_CHA_SEC_BELOW;
  1669. break;
  1670. default:
  1671. ht_oper->ht_param = IEEE80211_HT_PARAM_CHA_SEC_NONE;
  1672. break;
  1673. }
  1674. if (ht_cap->cap & IEEE80211_HT_CAP_SUP_WIDTH_20_40 &&
  1675. chandef->width != NL80211_CHAN_WIDTH_20_NOHT &&
  1676. chandef->width != NL80211_CHAN_WIDTH_20)
  1677. ht_oper->ht_param |= IEEE80211_HT_PARAM_CHAN_WIDTH_ANY;
  1678. ht_oper->operation_mode = cpu_to_le16(prot_mode);
  1679. ht_oper->stbc_param = 0x0000;
  1680. /* It seems that Basic MCS set and Supported MCS set
  1681. are identical for the first 10 bytes */
  1682. memset(&ht_oper->basic_set, 0, 16);
  1683. memcpy(&ht_oper->basic_set, &ht_cap->mcs, 10);
  1684. return pos + sizeof(struct ieee80211_ht_operation);
  1685. }
  1686. void ieee80211_ht_oper_to_chandef(struct ieee80211_channel *control_chan,
  1687. const struct ieee80211_ht_operation *ht_oper,
  1688. struct cfg80211_chan_def *chandef)
  1689. {
  1690. enum nl80211_channel_type channel_type;
  1691. if (!ht_oper) {
  1692. cfg80211_chandef_create(chandef, control_chan,
  1693. NL80211_CHAN_NO_HT);
  1694. return;
  1695. }
  1696. switch (ht_oper->ht_param & IEEE80211_HT_PARAM_CHA_SEC_OFFSET) {
  1697. case IEEE80211_HT_PARAM_CHA_SEC_NONE:
  1698. channel_type = NL80211_CHAN_HT20;
  1699. break;
  1700. case IEEE80211_HT_PARAM_CHA_SEC_ABOVE:
  1701. channel_type = NL80211_CHAN_HT40PLUS;
  1702. break;
  1703. case IEEE80211_HT_PARAM_CHA_SEC_BELOW:
  1704. channel_type = NL80211_CHAN_HT40MINUS;
  1705. break;
  1706. default:
  1707. channel_type = NL80211_CHAN_NO_HT;
  1708. }
  1709. cfg80211_chandef_create(chandef, control_chan, channel_type);
  1710. }
  1711. int ieee80211_add_srates_ie(struct ieee80211_sub_if_data *sdata,
  1712. struct sk_buff *skb, bool need_basic,
  1713. enum ieee80211_band band)
  1714. {
  1715. struct ieee80211_local *local = sdata->local;
  1716. struct ieee80211_supported_band *sband;
  1717. int rate;
  1718. u8 i, rates, *pos;
  1719. u32 basic_rates = sdata->vif.bss_conf.basic_rates;
  1720. sband = local->hw.wiphy->bands[band];
  1721. rates = sband->n_bitrates;
  1722. if (rates > 8)
  1723. rates = 8;
  1724. if (skb_tailroom(skb) < rates + 2)
  1725. return -ENOMEM;
  1726. pos = skb_put(skb, rates + 2);
  1727. *pos++ = WLAN_EID_SUPP_RATES;
  1728. *pos++ = rates;
  1729. for (i = 0; i < rates; i++) {
  1730. u8 basic = 0;
  1731. if (need_basic && basic_rates & BIT(i))
  1732. basic = 0x80;
  1733. rate = sband->bitrates[i].bitrate;
  1734. *pos++ = basic | (u8) (rate / 5);
  1735. }
  1736. return 0;
  1737. }
  1738. int ieee80211_add_ext_srates_ie(struct ieee80211_sub_if_data *sdata,
  1739. struct sk_buff *skb, bool need_basic,
  1740. enum ieee80211_band band)
  1741. {
  1742. struct ieee80211_local *local = sdata->local;
  1743. struct ieee80211_supported_band *sband;
  1744. int rate;
  1745. u8 i, exrates, *pos;
  1746. u32 basic_rates = sdata->vif.bss_conf.basic_rates;
  1747. sband = local->hw.wiphy->bands[band];
  1748. exrates = sband->n_bitrates;
  1749. if (exrates > 8)
  1750. exrates -= 8;
  1751. else
  1752. exrates = 0;
  1753. if (skb_tailroom(skb) < exrates + 2)
  1754. return -ENOMEM;
  1755. if (exrates) {
  1756. pos = skb_put(skb, exrates + 2);
  1757. *pos++ = WLAN_EID_EXT_SUPP_RATES;
  1758. *pos++ = exrates;
  1759. for (i = 8; i < sband->n_bitrates; i++) {
  1760. u8 basic = 0;
  1761. if (need_basic && basic_rates & BIT(i))
  1762. basic = 0x80;
  1763. rate = sband->bitrates[i].bitrate;
  1764. *pos++ = basic | (u8) (rate / 5);
  1765. }
  1766. }
  1767. return 0;
  1768. }
  1769. int ieee80211_ave_rssi(struct ieee80211_vif *vif)
  1770. {
  1771. struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
  1772. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  1773. if (WARN_ON_ONCE(sdata->vif.type != NL80211_IFTYPE_STATION)) {
  1774. /* non-managed type inferfaces */
  1775. return 0;
  1776. }
  1777. return ifmgd->ave_beacon_signal / 16;
  1778. }
  1779. EXPORT_SYMBOL_GPL(ieee80211_ave_rssi);
  1780. u8 ieee80211_mcs_to_chains(const struct ieee80211_mcs_info *mcs)
  1781. {
  1782. if (!mcs)
  1783. return 1;
  1784. /* TODO: consider rx_highest */
  1785. if (mcs->rx_mask[3])
  1786. return 4;
  1787. if (mcs->rx_mask[2])
  1788. return 3;
  1789. if (mcs->rx_mask[1])
  1790. return 2;
  1791. return 1;
  1792. }
  1793. /**
  1794. * ieee80211_calculate_rx_timestamp - calculate timestamp in frame
  1795. * @local: mac80211 hw info struct
  1796. * @status: RX status
  1797. * @mpdu_len: total MPDU length (including FCS)
  1798. * @mpdu_offset: offset into MPDU to calculate timestamp at
  1799. *
  1800. * This function calculates the RX timestamp at the given MPDU offset, taking
  1801. * into account what the RX timestamp was. An offset of 0 will just normalize
  1802. * the timestamp to TSF at beginning of MPDU reception.
  1803. */
  1804. u64 ieee80211_calculate_rx_timestamp(struct ieee80211_local *local,
  1805. struct ieee80211_rx_status *status,
  1806. unsigned int mpdu_len,
  1807. unsigned int mpdu_offset)
  1808. {
  1809. u64 ts = status->mactime;
  1810. struct rate_info ri;
  1811. u16 rate;
  1812. if (WARN_ON(!ieee80211_have_rx_timestamp(status)))
  1813. return 0;
  1814. memset(&ri, 0, sizeof(ri));
  1815. /* Fill cfg80211 rate info */
  1816. if (status->flag & RX_FLAG_HT) {
  1817. ri.mcs = status->rate_idx;
  1818. ri.flags |= RATE_INFO_FLAGS_MCS;
  1819. if (status->flag & RX_FLAG_40MHZ)
  1820. ri.flags |= RATE_INFO_FLAGS_40_MHZ_WIDTH;
  1821. if (status->flag & RX_FLAG_SHORT_GI)
  1822. ri.flags |= RATE_INFO_FLAGS_SHORT_GI;
  1823. } else if (status->flag & RX_FLAG_VHT) {
  1824. ri.flags |= RATE_INFO_FLAGS_VHT_MCS;
  1825. ri.mcs = status->rate_idx;
  1826. ri.nss = status->vht_nss;
  1827. if (status->flag & RX_FLAG_40MHZ)
  1828. ri.flags |= RATE_INFO_FLAGS_40_MHZ_WIDTH;
  1829. if (status->flag & RX_FLAG_80MHZ)
  1830. ri.flags |= RATE_INFO_FLAGS_80_MHZ_WIDTH;
  1831. if (status->flag & RX_FLAG_80P80MHZ)
  1832. ri.flags |= RATE_INFO_FLAGS_80P80_MHZ_WIDTH;
  1833. if (status->flag & RX_FLAG_160MHZ)
  1834. ri.flags |= RATE_INFO_FLAGS_160_MHZ_WIDTH;
  1835. if (status->flag & RX_FLAG_SHORT_GI)
  1836. ri.flags |= RATE_INFO_FLAGS_SHORT_GI;
  1837. } else {
  1838. struct ieee80211_supported_band *sband;
  1839. sband = local->hw.wiphy->bands[status->band];
  1840. ri.legacy = sband->bitrates[status->rate_idx].bitrate;
  1841. }
  1842. rate = cfg80211_calculate_bitrate(&ri);
  1843. /* rewind from end of MPDU */
  1844. if (status->flag & RX_FLAG_MACTIME_END)
  1845. ts -= mpdu_len * 8 * 10 / rate;
  1846. ts += mpdu_offset * 8 * 10 / rate;
  1847. return ts;
  1848. }
  1849. void ieee80211_dfs_cac_cancel(struct ieee80211_local *local)
  1850. {
  1851. struct ieee80211_sub_if_data *sdata;
  1852. mutex_lock(&local->iflist_mtx);
  1853. list_for_each_entry(sdata, &local->interfaces, list) {
  1854. cancel_delayed_work_sync(&sdata->dfs_cac_timer_work);
  1855. if (sdata->wdev.cac_started) {
  1856. ieee80211_vif_release_channel(sdata);
  1857. cfg80211_cac_event(sdata->dev,
  1858. NL80211_RADAR_CAC_ABORTED,
  1859. GFP_KERNEL);
  1860. }
  1861. }
  1862. mutex_unlock(&local->iflist_mtx);
  1863. }
  1864. void ieee80211_dfs_radar_detected_work(struct work_struct *work)
  1865. {
  1866. struct ieee80211_local *local =
  1867. container_of(work, struct ieee80211_local, radar_detected_work);
  1868. struct cfg80211_chan_def chandef;
  1869. ieee80211_dfs_cac_cancel(local);
  1870. if (local->use_chanctx)
  1871. /* currently not handled */
  1872. WARN_ON(1);
  1873. else {
  1874. chandef = local->hw.conf.chandef;
  1875. cfg80211_radar_event(local->hw.wiphy, &chandef, GFP_KERNEL);
  1876. }
  1877. }
  1878. void ieee80211_radar_detected(struct ieee80211_hw *hw)
  1879. {
  1880. struct ieee80211_local *local = hw_to_local(hw);
  1881. trace_api_radar_detected(local);
  1882. ieee80211_queue_work(hw, &local->radar_detected_work);
  1883. }
  1884. EXPORT_SYMBOL(ieee80211_radar_detected);