util.c 52 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085
  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. enum queue_stop_reason reason)
  377. {
  378. struct ieee80211_local *local = hw_to_local(hw);
  379. unsigned long flags;
  380. int i;
  381. spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
  382. for (i = 0; i < hw->queues; i++)
  383. __ieee80211_stop_queue(hw, i, reason);
  384. spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
  385. }
  386. void ieee80211_stop_queues(struct ieee80211_hw *hw)
  387. {
  388. ieee80211_stop_queues_by_reason(hw,
  389. IEEE80211_QUEUE_STOP_REASON_DRIVER);
  390. }
  391. EXPORT_SYMBOL(ieee80211_stop_queues);
  392. int ieee80211_queue_stopped(struct ieee80211_hw *hw, int queue)
  393. {
  394. struct ieee80211_local *local = hw_to_local(hw);
  395. unsigned long flags;
  396. int ret;
  397. if (WARN_ON(queue >= hw->queues))
  398. return true;
  399. spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
  400. ret = !!local->queue_stop_reasons[queue];
  401. spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
  402. return ret;
  403. }
  404. EXPORT_SYMBOL(ieee80211_queue_stopped);
  405. void ieee80211_wake_queues_by_reason(struct ieee80211_hw *hw,
  406. enum queue_stop_reason reason)
  407. {
  408. struct ieee80211_local *local = hw_to_local(hw);
  409. unsigned long flags;
  410. int i;
  411. spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
  412. for (i = 0; i < hw->queues; i++)
  413. __ieee80211_wake_queue(hw, i, reason);
  414. spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
  415. }
  416. void ieee80211_wake_queues(struct ieee80211_hw *hw)
  417. {
  418. ieee80211_wake_queues_by_reason(hw, IEEE80211_QUEUE_STOP_REASON_DRIVER);
  419. }
  420. EXPORT_SYMBOL(ieee80211_wake_queues);
  421. void ieee80211_iterate_active_interfaces(
  422. struct ieee80211_hw *hw, u32 iter_flags,
  423. void (*iterator)(void *data, u8 *mac,
  424. struct ieee80211_vif *vif),
  425. void *data)
  426. {
  427. struct ieee80211_local *local = hw_to_local(hw);
  428. struct ieee80211_sub_if_data *sdata;
  429. mutex_lock(&local->iflist_mtx);
  430. list_for_each_entry(sdata, &local->interfaces, list) {
  431. switch (sdata->vif.type) {
  432. case NL80211_IFTYPE_MONITOR:
  433. case NL80211_IFTYPE_AP_VLAN:
  434. continue;
  435. default:
  436. break;
  437. }
  438. if (!(iter_flags & IEEE80211_IFACE_ITER_RESUME_ALL) &&
  439. !(sdata->flags & IEEE80211_SDATA_IN_DRIVER))
  440. continue;
  441. if (ieee80211_sdata_running(sdata))
  442. iterator(data, sdata->vif.addr,
  443. &sdata->vif);
  444. }
  445. sdata = rcu_dereference_protected(local->monitor_sdata,
  446. lockdep_is_held(&local->iflist_mtx));
  447. if (sdata &&
  448. (iter_flags & IEEE80211_IFACE_ITER_RESUME_ALL ||
  449. sdata->flags & IEEE80211_SDATA_IN_DRIVER))
  450. iterator(data, sdata->vif.addr, &sdata->vif);
  451. mutex_unlock(&local->iflist_mtx);
  452. }
  453. EXPORT_SYMBOL_GPL(ieee80211_iterate_active_interfaces);
  454. void ieee80211_iterate_active_interfaces_atomic(
  455. struct ieee80211_hw *hw, u32 iter_flags,
  456. void (*iterator)(void *data, u8 *mac,
  457. struct ieee80211_vif *vif),
  458. void *data)
  459. {
  460. struct ieee80211_local *local = hw_to_local(hw);
  461. struct ieee80211_sub_if_data *sdata;
  462. rcu_read_lock();
  463. list_for_each_entry_rcu(sdata, &local->interfaces, list) {
  464. switch (sdata->vif.type) {
  465. case NL80211_IFTYPE_MONITOR:
  466. case NL80211_IFTYPE_AP_VLAN:
  467. continue;
  468. default:
  469. break;
  470. }
  471. if (!(iter_flags & IEEE80211_IFACE_ITER_RESUME_ALL) &&
  472. !(sdata->flags & IEEE80211_SDATA_IN_DRIVER))
  473. continue;
  474. if (ieee80211_sdata_running(sdata))
  475. iterator(data, sdata->vif.addr,
  476. &sdata->vif);
  477. }
  478. sdata = rcu_dereference(local->monitor_sdata);
  479. if (sdata &&
  480. (iter_flags & IEEE80211_IFACE_ITER_RESUME_ALL ||
  481. sdata->flags & IEEE80211_SDATA_IN_DRIVER))
  482. iterator(data, sdata->vif.addr, &sdata->vif);
  483. rcu_read_unlock();
  484. }
  485. EXPORT_SYMBOL_GPL(ieee80211_iterate_active_interfaces_atomic);
  486. /*
  487. * Nothing should have been stuffed into the workqueue during
  488. * the suspend->resume cycle. If this WARN is seen then there
  489. * is a bug with either the driver suspend or something in
  490. * mac80211 stuffing into the workqueue which we haven't yet
  491. * cleared during mac80211's suspend cycle.
  492. */
  493. static bool ieee80211_can_queue_work(struct ieee80211_local *local)
  494. {
  495. if (WARN(local->suspended && !local->resuming,
  496. "queueing ieee80211 work while going to suspend\n"))
  497. return false;
  498. return true;
  499. }
  500. void ieee80211_queue_work(struct ieee80211_hw *hw, struct work_struct *work)
  501. {
  502. struct ieee80211_local *local = hw_to_local(hw);
  503. if (!ieee80211_can_queue_work(local))
  504. return;
  505. queue_work(local->workqueue, work);
  506. }
  507. EXPORT_SYMBOL(ieee80211_queue_work);
  508. void ieee80211_queue_delayed_work(struct ieee80211_hw *hw,
  509. struct delayed_work *dwork,
  510. unsigned long delay)
  511. {
  512. struct ieee80211_local *local = hw_to_local(hw);
  513. if (!ieee80211_can_queue_work(local))
  514. return;
  515. queue_delayed_work(local->workqueue, dwork, delay);
  516. }
  517. EXPORT_SYMBOL(ieee80211_queue_delayed_work);
  518. u32 ieee802_11_parse_elems_crc(u8 *start, size_t len,
  519. struct ieee802_11_elems *elems,
  520. u64 filter, u32 crc)
  521. {
  522. size_t left = len;
  523. u8 *pos = start;
  524. bool calc_crc = filter != 0;
  525. DECLARE_BITMAP(seen_elems, 256);
  526. bitmap_zero(seen_elems, 256);
  527. memset(elems, 0, sizeof(*elems));
  528. elems->ie_start = start;
  529. elems->total_len = len;
  530. while (left >= 2) {
  531. u8 id, elen;
  532. bool elem_parse_failed;
  533. id = *pos++;
  534. elen = *pos++;
  535. left -= 2;
  536. if (elen > left) {
  537. elems->parse_error = true;
  538. break;
  539. }
  540. switch (id) {
  541. case WLAN_EID_SSID:
  542. case WLAN_EID_SUPP_RATES:
  543. case WLAN_EID_FH_PARAMS:
  544. case WLAN_EID_DS_PARAMS:
  545. case WLAN_EID_CF_PARAMS:
  546. case WLAN_EID_TIM:
  547. case WLAN_EID_IBSS_PARAMS:
  548. case WLAN_EID_CHALLENGE:
  549. case WLAN_EID_RSN:
  550. case WLAN_EID_ERP_INFO:
  551. case WLAN_EID_EXT_SUPP_RATES:
  552. case WLAN_EID_HT_CAPABILITY:
  553. case WLAN_EID_HT_OPERATION:
  554. case WLAN_EID_VHT_CAPABILITY:
  555. case WLAN_EID_VHT_OPERATION:
  556. case WLAN_EID_MESH_ID:
  557. case WLAN_EID_MESH_CONFIG:
  558. case WLAN_EID_PEER_MGMT:
  559. case WLAN_EID_PREQ:
  560. case WLAN_EID_PREP:
  561. case WLAN_EID_PERR:
  562. case WLAN_EID_RANN:
  563. case WLAN_EID_CHANNEL_SWITCH:
  564. case WLAN_EID_EXT_CHANSWITCH_ANN:
  565. case WLAN_EID_COUNTRY:
  566. case WLAN_EID_PWR_CONSTRAINT:
  567. case WLAN_EID_TIMEOUT_INTERVAL:
  568. if (test_bit(id, seen_elems)) {
  569. elems->parse_error = true;
  570. left -= elen;
  571. pos += elen;
  572. continue;
  573. }
  574. break;
  575. }
  576. if (calc_crc && id < 64 && (filter & (1ULL << id)))
  577. crc = crc32_be(crc, pos - 2, elen + 2);
  578. elem_parse_failed = false;
  579. switch (id) {
  580. case WLAN_EID_SSID:
  581. elems->ssid = pos;
  582. elems->ssid_len = elen;
  583. break;
  584. case WLAN_EID_SUPP_RATES:
  585. elems->supp_rates = pos;
  586. elems->supp_rates_len = elen;
  587. break;
  588. case WLAN_EID_FH_PARAMS:
  589. elems->fh_params = pos;
  590. elems->fh_params_len = elen;
  591. break;
  592. case WLAN_EID_DS_PARAMS:
  593. elems->ds_params = pos;
  594. elems->ds_params_len = elen;
  595. break;
  596. case WLAN_EID_CF_PARAMS:
  597. elems->cf_params = pos;
  598. elems->cf_params_len = elen;
  599. break;
  600. case WLAN_EID_TIM:
  601. if (elen >= sizeof(struct ieee80211_tim_ie)) {
  602. elems->tim = (void *)pos;
  603. elems->tim_len = elen;
  604. } else
  605. elem_parse_failed = true;
  606. break;
  607. case WLAN_EID_IBSS_PARAMS:
  608. elems->ibss_params = pos;
  609. elems->ibss_params_len = elen;
  610. break;
  611. case WLAN_EID_CHALLENGE:
  612. elems->challenge = pos;
  613. elems->challenge_len = elen;
  614. break;
  615. case WLAN_EID_VENDOR_SPECIFIC:
  616. if (elen >= 4 && pos[0] == 0x00 && pos[1] == 0x50 &&
  617. pos[2] == 0xf2) {
  618. /* Microsoft OUI (00:50:F2) */
  619. if (calc_crc)
  620. crc = crc32_be(crc, pos - 2, elen + 2);
  621. if (pos[3] == 1) {
  622. /* OUI Type 1 - WPA IE */
  623. elems->wpa = pos;
  624. elems->wpa_len = elen;
  625. } else if (elen >= 5 && pos[3] == 2) {
  626. /* OUI Type 2 - WMM IE */
  627. if (pos[4] == 0) {
  628. elems->wmm_info = pos;
  629. elems->wmm_info_len = elen;
  630. } else if (pos[4] == 1) {
  631. elems->wmm_param = pos;
  632. elems->wmm_param_len = elen;
  633. }
  634. }
  635. }
  636. break;
  637. case WLAN_EID_RSN:
  638. elems->rsn = pos;
  639. elems->rsn_len = elen;
  640. break;
  641. case WLAN_EID_ERP_INFO:
  642. elems->erp_info = pos;
  643. elems->erp_info_len = elen;
  644. break;
  645. case WLAN_EID_EXT_SUPP_RATES:
  646. elems->ext_supp_rates = pos;
  647. elems->ext_supp_rates_len = elen;
  648. break;
  649. case WLAN_EID_HT_CAPABILITY:
  650. if (elen >= sizeof(struct ieee80211_ht_cap))
  651. elems->ht_cap_elem = (void *)pos;
  652. else
  653. elem_parse_failed = true;
  654. break;
  655. case WLAN_EID_HT_OPERATION:
  656. if (elen >= sizeof(struct ieee80211_ht_operation))
  657. elems->ht_operation = (void *)pos;
  658. else
  659. elem_parse_failed = true;
  660. break;
  661. case WLAN_EID_VHT_CAPABILITY:
  662. if (elen >= sizeof(struct ieee80211_vht_cap))
  663. elems->vht_cap_elem = (void *)pos;
  664. else
  665. elem_parse_failed = true;
  666. break;
  667. case WLAN_EID_VHT_OPERATION:
  668. if (elen >= sizeof(struct ieee80211_vht_operation))
  669. elems->vht_operation = (void *)pos;
  670. else
  671. elem_parse_failed = true;
  672. break;
  673. case WLAN_EID_MESH_ID:
  674. elems->mesh_id = pos;
  675. elems->mesh_id_len = elen;
  676. break;
  677. case WLAN_EID_MESH_CONFIG:
  678. if (elen >= sizeof(struct ieee80211_meshconf_ie))
  679. elems->mesh_config = (void *)pos;
  680. else
  681. elem_parse_failed = true;
  682. break;
  683. case WLAN_EID_PEER_MGMT:
  684. elems->peering = pos;
  685. elems->peering_len = elen;
  686. break;
  687. case WLAN_EID_PREQ:
  688. elems->preq = pos;
  689. elems->preq_len = elen;
  690. break;
  691. case WLAN_EID_PREP:
  692. elems->prep = pos;
  693. elems->prep_len = elen;
  694. break;
  695. case WLAN_EID_PERR:
  696. elems->perr = pos;
  697. elems->perr_len = elen;
  698. break;
  699. case WLAN_EID_RANN:
  700. if (elen >= sizeof(struct ieee80211_rann_ie))
  701. elems->rann = (void *)pos;
  702. else
  703. elem_parse_failed = true;
  704. break;
  705. case WLAN_EID_CHANNEL_SWITCH:
  706. if (elen != sizeof(struct ieee80211_channel_sw_ie)) {
  707. elem_parse_failed = true;
  708. break;
  709. }
  710. elems->ch_switch_ie = (void *)pos;
  711. break;
  712. case WLAN_EID_QUIET:
  713. if (!elems->quiet_elem) {
  714. elems->quiet_elem = pos;
  715. elems->quiet_elem_len = elen;
  716. }
  717. elems->num_of_quiet_elem++;
  718. break;
  719. case WLAN_EID_COUNTRY:
  720. elems->country_elem = pos;
  721. elems->country_elem_len = elen;
  722. break;
  723. case WLAN_EID_PWR_CONSTRAINT:
  724. if (elen != 1) {
  725. elem_parse_failed = true;
  726. break;
  727. }
  728. elems->pwr_constr_elem = pos;
  729. break;
  730. case WLAN_EID_TIMEOUT_INTERVAL:
  731. elems->timeout_int = pos;
  732. elems->timeout_int_len = elen;
  733. break;
  734. default:
  735. break;
  736. }
  737. if (elem_parse_failed)
  738. elems->parse_error = true;
  739. else
  740. __set_bit(id, seen_elems);
  741. left -= elen;
  742. pos += elen;
  743. }
  744. if (left != 0)
  745. elems->parse_error = true;
  746. return crc;
  747. }
  748. void ieee802_11_parse_elems(u8 *start, size_t len,
  749. struct ieee802_11_elems *elems)
  750. {
  751. ieee802_11_parse_elems_crc(start, len, elems, 0, 0);
  752. }
  753. void ieee80211_set_wmm_default(struct ieee80211_sub_if_data *sdata,
  754. bool bss_notify)
  755. {
  756. struct ieee80211_local *local = sdata->local;
  757. struct ieee80211_tx_queue_params qparam;
  758. struct ieee80211_chanctx_conf *chanctx_conf;
  759. int ac;
  760. bool use_11b, enable_qos;
  761. int aCWmin, aCWmax;
  762. if (!local->ops->conf_tx)
  763. return;
  764. if (local->hw.queues < IEEE80211_NUM_ACS)
  765. return;
  766. memset(&qparam, 0, sizeof(qparam));
  767. rcu_read_lock();
  768. chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
  769. use_11b = (chanctx_conf &&
  770. chanctx_conf->channel->band == IEEE80211_BAND_2GHZ) &&
  771. !(sdata->flags & IEEE80211_SDATA_OPERATING_GMODE);
  772. rcu_read_unlock();
  773. /*
  774. * By default disable QoS in STA mode for old access points, which do
  775. * not support 802.11e. New APs will provide proper queue parameters,
  776. * that we will configure later.
  777. */
  778. enable_qos = (sdata->vif.type != NL80211_IFTYPE_STATION);
  779. for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
  780. /* Set defaults according to 802.11-2007 Table 7-37 */
  781. aCWmax = 1023;
  782. if (use_11b)
  783. aCWmin = 31;
  784. else
  785. aCWmin = 15;
  786. if (enable_qos) {
  787. switch (ac) {
  788. case IEEE80211_AC_BK:
  789. qparam.cw_max = aCWmax;
  790. qparam.cw_min = aCWmin;
  791. qparam.txop = 0;
  792. qparam.aifs = 7;
  793. break;
  794. /* never happens but let's not leave undefined */
  795. default:
  796. case IEEE80211_AC_BE:
  797. qparam.cw_max = aCWmax;
  798. qparam.cw_min = aCWmin;
  799. qparam.txop = 0;
  800. qparam.aifs = 3;
  801. break;
  802. case IEEE80211_AC_VI:
  803. qparam.cw_max = aCWmin;
  804. qparam.cw_min = (aCWmin + 1) / 2 - 1;
  805. if (use_11b)
  806. qparam.txop = 6016/32;
  807. else
  808. qparam.txop = 3008/32;
  809. qparam.aifs = 2;
  810. break;
  811. case IEEE80211_AC_VO:
  812. qparam.cw_max = (aCWmin + 1) / 2 - 1;
  813. qparam.cw_min = (aCWmin + 1) / 4 - 1;
  814. if (use_11b)
  815. qparam.txop = 3264/32;
  816. else
  817. qparam.txop = 1504/32;
  818. qparam.aifs = 2;
  819. break;
  820. }
  821. } else {
  822. /* Confiure old 802.11b/g medium access rules. */
  823. qparam.cw_max = aCWmax;
  824. qparam.cw_min = aCWmin;
  825. qparam.txop = 0;
  826. qparam.aifs = 2;
  827. }
  828. qparam.uapsd = false;
  829. sdata->tx_conf[ac] = qparam;
  830. drv_conf_tx(local, sdata, ac, &qparam);
  831. }
  832. if (sdata->vif.type != NL80211_IFTYPE_MONITOR &&
  833. sdata->vif.type != NL80211_IFTYPE_P2P_DEVICE) {
  834. sdata->vif.bss_conf.qos = enable_qos;
  835. if (bss_notify)
  836. ieee80211_bss_info_change_notify(sdata,
  837. BSS_CHANGED_QOS);
  838. }
  839. }
  840. void ieee80211_sta_def_wmm_params(struct ieee80211_sub_if_data *sdata,
  841. const size_t supp_rates_len,
  842. const u8 *supp_rates)
  843. {
  844. struct ieee80211_chanctx_conf *chanctx_conf;
  845. int i, have_higher_than_11mbit = 0;
  846. /* cf. IEEE 802.11 9.2.12 */
  847. for (i = 0; i < supp_rates_len; i++)
  848. if ((supp_rates[i] & 0x7f) * 5 > 110)
  849. have_higher_than_11mbit = 1;
  850. rcu_read_lock();
  851. chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
  852. if (chanctx_conf &&
  853. chanctx_conf->channel->band == IEEE80211_BAND_2GHZ &&
  854. have_higher_than_11mbit)
  855. sdata->flags |= IEEE80211_SDATA_OPERATING_GMODE;
  856. else
  857. sdata->flags &= ~IEEE80211_SDATA_OPERATING_GMODE;
  858. rcu_read_unlock();
  859. ieee80211_set_wmm_default(sdata, true);
  860. }
  861. u32 ieee80211_mandatory_rates(struct ieee80211_local *local,
  862. enum ieee80211_band band)
  863. {
  864. struct ieee80211_supported_band *sband;
  865. struct ieee80211_rate *bitrates;
  866. u32 mandatory_rates;
  867. enum ieee80211_rate_flags mandatory_flag;
  868. int i;
  869. sband = local->hw.wiphy->bands[band];
  870. if (WARN_ON(!sband))
  871. return 1;
  872. if (band == IEEE80211_BAND_2GHZ)
  873. mandatory_flag = IEEE80211_RATE_MANDATORY_B;
  874. else
  875. mandatory_flag = IEEE80211_RATE_MANDATORY_A;
  876. bitrates = sband->bitrates;
  877. mandatory_rates = 0;
  878. for (i = 0; i < sband->n_bitrates; i++)
  879. if (bitrates[i].flags & mandatory_flag)
  880. mandatory_rates |= BIT(i);
  881. return mandatory_rates;
  882. }
  883. void ieee80211_send_auth(struct ieee80211_sub_if_data *sdata,
  884. u16 transaction, u16 auth_alg, u16 status,
  885. u8 *extra, size_t extra_len, const u8 *da,
  886. const u8 *bssid, const u8 *key, u8 key_len, u8 key_idx)
  887. {
  888. struct ieee80211_local *local = sdata->local;
  889. struct sk_buff *skb;
  890. struct ieee80211_mgmt *mgmt;
  891. int err;
  892. skb = dev_alloc_skb(local->hw.extra_tx_headroom +
  893. sizeof(*mgmt) + 6 + extra_len);
  894. if (!skb)
  895. return;
  896. skb_reserve(skb, local->hw.extra_tx_headroom);
  897. mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24 + 6);
  898. memset(mgmt, 0, 24 + 6);
  899. mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
  900. IEEE80211_STYPE_AUTH);
  901. memcpy(mgmt->da, da, ETH_ALEN);
  902. memcpy(mgmt->sa, sdata->vif.addr, ETH_ALEN);
  903. memcpy(mgmt->bssid, bssid, ETH_ALEN);
  904. mgmt->u.auth.auth_alg = cpu_to_le16(auth_alg);
  905. mgmt->u.auth.auth_transaction = cpu_to_le16(transaction);
  906. mgmt->u.auth.status_code = cpu_to_le16(status);
  907. if (extra)
  908. memcpy(skb_put(skb, extra_len), extra, extra_len);
  909. if (auth_alg == WLAN_AUTH_SHARED_KEY && transaction == 3) {
  910. mgmt->frame_control |= cpu_to_le16(IEEE80211_FCTL_PROTECTED);
  911. err = ieee80211_wep_encrypt(local, skb, key, key_len, key_idx);
  912. WARN_ON(err);
  913. }
  914. IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
  915. ieee80211_tx_skb(sdata, skb);
  916. }
  917. void ieee80211_send_deauth_disassoc(struct ieee80211_sub_if_data *sdata,
  918. const u8 *bssid, u16 stype, u16 reason,
  919. bool send_frame, u8 *frame_buf)
  920. {
  921. struct ieee80211_local *local = sdata->local;
  922. struct sk_buff *skb;
  923. struct ieee80211_mgmt *mgmt = (void *)frame_buf;
  924. /* build frame */
  925. mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT | stype);
  926. mgmt->duration = 0; /* initialize only */
  927. mgmt->seq_ctrl = 0; /* initialize only */
  928. memcpy(mgmt->da, bssid, ETH_ALEN);
  929. memcpy(mgmt->sa, sdata->vif.addr, ETH_ALEN);
  930. memcpy(mgmt->bssid, bssid, ETH_ALEN);
  931. /* u.deauth.reason_code == u.disassoc.reason_code */
  932. mgmt->u.deauth.reason_code = cpu_to_le16(reason);
  933. if (send_frame) {
  934. skb = dev_alloc_skb(local->hw.extra_tx_headroom +
  935. IEEE80211_DEAUTH_FRAME_LEN);
  936. if (!skb)
  937. return;
  938. skb_reserve(skb, local->hw.extra_tx_headroom);
  939. /* copy in frame */
  940. memcpy(skb_put(skb, IEEE80211_DEAUTH_FRAME_LEN),
  941. mgmt, IEEE80211_DEAUTH_FRAME_LEN);
  942. if (sdata->vif.type != NL80211_IFTYPE_STATION ||
  943. !(sdata->u.mgd.flags & IEEE80211_STA_MFP_ENABLED))
  944. IEEE80211_SKB_CB(skb)->flags |=
  945. IEEE80211_TX_INTFL_DONT_ENCRYPT;
  946. ieee80211_tx_skb(sdata, skb);
  947. }
  948. }
  949. int ieee80211_build_preq_ies(struct ieee80211_local *local, u8 *buffer,
  950. const u8 *ie, size_t ie_len,
  951. enum ieee80211_band band, u32 rate_mask,
  952. u8 channel)
  953. {
  954. struct ieee80211_supported_band *sband;
  955. u8 *pos;
  956. size_t offset = 0, noffset;
  957. int supp_rates_len, i;
  958. u8 rates[32];
  959. int num_rates;
  960. int ext_rates_len;
  961. sband = local->hw.wiphy->bands[band];
  962. if (WARN_ON_ONCE(!sband))
  963. return 0;
  964. pos = buffer;
  965. num_rates = 0;
  966. for (i = 0; i < sband->n_bitrates; i++) {
  967. if ((BIT(i) & rate_mask) == 0)
  968. continue; /* skip rate */
  969. rates[num_rates++] = (u8) (sband->bitrates[i].bitrate / 5);
  970. }
  971. supp_rates_len = min_t(int, num_rates, 8);
  972. *pos++ = WLAN_EID_SUPP_RATES;
  973. *pos++ = supp_rates_len;
  974. memcpy(pos, rates, supp_rates_len);
  975. pos += supp_rates_len;
  976. /* insert "request information" if in custom IEs */
  977. if (ie && ie_len) {
  978. static const u8 before_extrates[] = {
  979. WLAN_EID_SSID,
  980. WLAN_EID_SUPP_RATES,
  981. WLAN_EID_REQUEST,
  982. };
  983. noffset = ieee80211_ie_split(ie, ie_len,
  984. before_extrates,
  985. ARRAY_SIZE(before_extrates),
  986. offset);
  987. memcpy(pos, ie + offset, noffset - offset);
  988. pos += noffset - offset;
  989. offset = noffset;
  990. }
  991. ext_rates_len = num_rates - supp_rates_len;
  992. if (ext_rates_len > 0) {
  993. *pos++ = WLAN_EID_EXT_SUPP_RATES;
  994. *pos++ = ext_rates_len;
  995. memcpy(pos, rates + supp_rates_len, ext_rates_len);
  996. pos += ext_rates_len;
  997. }
  998. if (channel && sband->band == IEEE80211_BAND_2GHZ) {
  999. *pos++ = WLAN_EID_DS_PARAMS;
  1000. *pos++ = 1;
  1001. *pos++ = channel;
  1002. }
  1003. /* insert custom IEs that go before HT */
  1004. if (ie && ie_len) {
  1005. static const u8 before_ht[] = {
  1006. WLAN_EID_SSID,
  1007. WLAN_EID_SUPP_RATES,
  1008. WLAN_EID_REQUEST,
  1009. WLAN_EID_EXT_SUPP_RATES,
  1010. WLAN_EID_DS_PARAMS,
  1011. WLAN_EID_SUPPORTED_REGULATORY_CLASSES,
  1012. };
  1013. noffset = ieee80211_ie_split(ie, ie_len,
  1014. before_ht, ARRAY_SIZE(before_ht),
  1015. offset);
  1016. memcpy(pos, ie + offset, noffset - offset);
  1017. pos += noffset - offset;
  1018. offset = noffset;
  1019. }
  1020. if (sband->ht_cap.ht_supported)
  1021. pos = ieee80211_ie_build_ht_cap(pos, &sband->ht_cap,
  1022. sband->ht_cap.cap);
  1023. /*
  1024. * If adding more here, adjust code in main.c
  1025. * that calculates local->scan_ies_len.
  1026. */
  1027. /* add any remaining custom IEs */
  1028. if (ie && ie_len) {
  1029. noffset = ie_len;
  1030. memcpy(pos, ie + offset, noffset - offset);
  1031. pos += noffset - offset;
  1032. }
  1033. if (sband->vht_cap.vht_supported)
  1034. pos = ieee80211_ie_build_vht_cap(pos, &sband->vht_cap,
  1035. sband->vht_cap.cap);
  1036. return pos - buffer;
  1037. }
  1038. struct sk_buff *ieee80211_build_probe_req(struct ieee80211_sub_if_data *sdata,
  1039. u8 *dst, u32 ratemask,
  1040. struct ieee80211_channel *chan,
  1041. const u8 *ssid, size_t ssid_len,
  1042. const u8 *ie, size_t ie_len,
  1043. bool directed)
  1044. {
  1045. struct ieee80211_local *local = sdata->local;
  1046. struct sk_buff *skb;
  1047. struct ieee80211_mgmt *mgmt;
  1048. size_t buf_len;
  1049. u8 *buf;
  1050. u8 chan_no;
  1051. /* FIXME: come up with a proper value */
  1052. buf = kmalloc(200 + ie_len, GFP_KERNEL);
  1053. if (!buf)
  1054. return NULL;
  1055. /*
  1056. * Do not send DS Channel parameter for directed probe requests
  1057. * in order to maximize the chance that we get a response. Some
  1058. * badly-behaved APs don't respond when this parameter is included.
  1059. */
  1060. if (directed)
  1061. chan_no = 0;
  1062. else
  1063. chan_no = ieee80211_frequency_to_channel(chan->center_freq);
  1064. buf_len = ieee80211_build_preq_ies(local, buf, ie, ie_len, chan->band,
  1065. ratemask, chan_no);
  1066. skb = ieee80211_probereq_get(&local->hw, &sdata->vif,
  1067. ssid, ssid_len,
  1068. buf, buf_len);
  1069. if (!skb)
  1070. goto out;
  1071. if (dst) {
  1072. mgmt = (struct ieee80211_mgmt *) skb->data;
  1073. memcpy(mgmt->da, dst, ETH_ALEN);
  1074. memcpy(mgmt->bssid, dst, ETH_ALEN);
  1075. }
  1076. IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
  1077. out:
  1078. kfree(buf);
  1079. return skb;
  1080. }
  1081. void ieee80211_send_probe_req(struct ieee80211_sub_if_data *sdata, u8 *dst,
  1082. const u8 *ssid, size_t ssid_len,
  1083. const u8 *ie, size_t ie_len,
  1084. u32 ratemask, bool directed, bool no_cck,
  1085. struct ieee80211_channel *channel, bool scan)
  1086. {
  1087. struct sk_buff *skb;
  1088. skb = ieee80211_build_probe_req(sdata, dst, ratemask, channel,
  1089. ssid, ssid_len,
  1090. ie, ie_len, directed);
  1091. if (skb) {
  1092. if (no_cck)
  1093. IEEE80211_SKB_CB(skb)->flags |=
  1094. IEEE80211_TX_CTL_NO_CCK_RATE;
  1095. if (scan)
  1096. ieee80211_tx_skb_tid_band(sdata, skb, 7, channel->band);
  1097. else
  1098. ieee80211_tx_skb(sdata, skb);
  1099. }
  1100. }
  1101. u32 ieee80211_sta_get_rates(struct ieee80211_local *local,
  1102. struct ieee802_11_elems *elems,
  1103. enum ieee80211_band band, u32 *basic_rates)
  1104. {
  1105. struct ieee80211_supported_band *sband;
  1106. struct ieee80211_rate *bitrates;
  1107. size_t num_rates;
  1108. u32 supp_rates;
  1109. int i, j;
  1110. sband = local->hw.wiphy->bands[band];
  1111. if (WARN_ON(!sband))
  1112. return 1;
  1113. bitrates = sband->bitrates;
  1114. num_rates = sband->n_bitrates;
  1115. supp_rates = 0;
  1116. for (i = 0; i < elems->supp_rates_len +
  1117. elems->ext_supp_rates_len; i++) {
  1118. u8 rate = 0;
  1119. int own_rate;
  1120. bool is_basic;
  1121. if (i < elems->supp_rates_len)
  1122. rate = elems->supp_rates[i];
  1123. else if (elems->ext_supp_rates)
  1124. rate = elems->ext_supp_rates
  1125. [i - elems->supp_rates_len];
  1126. own_rate = 5 * (rate & 0x7f);
  1127. is_basic = !!(rate & 0x80);
  1128. if (is_basic && (rate & 0x7f) == BSS_MEMBERSHIP_SELECTOR_HT_PHY)
  1129. continue;
  1130. for (j = 0; j < num_rates; j++) {
  1131. if (bitrates[j].bitrate == own_rate) {
  1132. supp_rates |= BIT(j);
  1133. if (basic_rates && is_basic)
  1134. *basic_rates |= BIT(j);
  1135. }
  1136. }
  1137. }
  1138. return supp_rates;
  1139. }
  1140. void ieee80211_stop_device(struct ieee80211_local *local)
  1141. {
  1142. ieee80211_led_radio(local, false);
  1143. ieee80211_mod_tpt_led_trig(local, 0, IEEE80211_TPT_LEDTRIG_FL_RADIO);
  1144. cancel_work_sync(&local->reconfig_filter);
  1145. flush_workqueue(local->workqueue);
  1146. drv_stop(local);
  1147. }
  1148. int ieee80211_reconfig(struct ieee80211_local *local)
  1149. {
  1150. struct ieee80211_hw *hw = &local->hw;
  1151. struct ieee80211_sub_if_data *sdata;
  1152. struct ieee80211_chanctx *ctx;
  1153. struct sta_info *sta;
  1154. int res, i;
  1155. #ifdef CONFIG_PM
  1156. if (local->suspended)
  1157. local->resuming = true;
  1158. if (local->wowlan) {
  1159. local->wowlan = false;
  1160. res = drv_resume(local);
  1161. if (res < 0) {
  1162. local->resuming = false;
  1163. return res;
  1164. }
  1165. if (res == 0)
  1166. goto wake_up;
  1167. WARN_ON(res > 1);
  1168. /*
  1169. * res is 1, which means the driver requested
  1170. * to go through a regular reset on wakeup.
  1171. */
  1172. }
  1173. #endif
  1174. /* everything else happens only if HW was up & running */
  1175. if (!local->open_count)
  1176. goto wake_up;
  1177. /*
  1178. * Upon resume hardware can sometimes be goofy due to
  1179. * various platform / driver / bus issues, so restarting
  1180. * the device may at times not work immediately. Propagate
  1181. * the error.
  1182. */
  1183. res = drv_start(local);
  1184. if (res) {
  1185. WARN(local->suspended, "Hardware became unavailable "
  1186. "upon resume. This could be a software issue "
  1187. "prior to suspend or a hardware issue.\n");
  1188. return res;
  1189. }
  1190. /* setup fragmentation threshold */
  1191. drv_set_frag_threshold(local, hw->wiphy->frag_threshold);
  1192. /* setup RTS threshold */
  1193. drv_set_rts_threshold(local, hw->wiphy->rts_threshold);
  1194. /* reset coverage class */
  1195. drv_set_coverage_class(local, hw->wiphy->coverage_class);
  1196. ieee80211_led_radio(local, true);
  1197. ieee80211_mod_tpt_led_trig(local,
  1198. IEEE80211_TPT_LEDTRIG_FL_RADIO, 0);
  1199. /* add interfaces */
  1200. sdata = rtnl_dereference(local->monitor_sdata);
  1201. if (sdata) {
  1202. res = drv_add_interface(local, sdata);
  1203. if (WARN_ON(res)) {
  1204. rcu_assign_pointer(local->monitor_sdata, NULL);
  1205. synchronize_net();
  1206. kfree(sdata);
  1207. }
  1208. }
  1209. list_for_each_entry(sdata, &local->interfaces, list) {
  1210. if (sdata->vif.type != NL80211_IFTYPE_AP_VLAN &&
  1211. sdata->vif.type != NL80211_IFTYPE_MONITOR &&
  1212. ieee80211_sdata_running(sdata))
  1213. res = drv_add_interface(local, sdata);
  1214. }
  1215. /* add channel contexts */
  1216. if (local->use_chanctx) {
  1217. mutex_lock(&local->chanctx_mtx);
  1218. list_for_each_entry(ctx, &local->chanctx_list, list)
  1219. WARN_ON(drv_add_chanctx(local, ctx));
  1220. mutex_unlock(&local->chanctx_mtx);
  1221. }
  1222. list_for_each_entry(sdata, &local->interfaces, list) {
  1223. struct ieee80211_chanctx_conf *ctx_conf;
  1224. if (!ieee80211_sdata_running(sdata))
  1225. continue;
  1226. mutex_lock(&local->chanctx_mtx);
  1227. ctx_conf = rcu_dereference_protected(sdata->vif.chanctx_conf,
  1228. lockdep_is_held(&local->chanctx_mtx));
  1229. if (ctx_conf) {
  1230. ctx = container_of(ctx_conf, struct ieee80211_chanctx,
  1231. conf);
  1232. drv_assign_vif_chanctx(local, sdata, ctx);
  1233. }
  1234. mutex_unlock(&local->chanctx_mtx);
  1235. }
  1236. sdata = rtnl_dereference(local->monitor_sdata);
  1237. if (sdata && local->use_chanctx && ieee80211_sdata_running(sdata)) {
  1238. struct ieee80211_chanctx_conf *ctx_conf;
  1239. mutex_lock(&local->chanctx_mtx);
  1240. ctx_conf = rcu_dereference_protected(sdata->vif.chanctx_conf,
  1241. lockdep_is_held(&local->chanctx_mtx));
  1242. if (ctx_conf) {
  1243. ctx = container_of(ctx_conf, struct ieee80211_chanctx,
  1244. conf);
  1245. drv_assign_vif_chanctx(local, sdata, ctx);
  1246. }
  1247. mutex_unlock(&local->chanctx_mtx);
  1248. }
  1249. /* add STAs back */
  1250. mutex_lock(&local->sta_mtx);
  1251. list_for_each_entry(sta, &local->sta_list, list) {
  1252. enum ieee80211_sta_state state;
  1253. if (!sta->uploaded)
  1254. continue;
  1255. /* AP-mode stations will be added later */
  1256. if (sta->sdata->vif.type == NL80211_IFTYPE_AP)
  1257. continue;
  1258. for (state = IEEE80211_STA_NOTEXIST;
  1259. state < sta->sta_state; state++)
  1260. WARN_ON(drv_sta_state(local, sta->sdata, sta, state,
  1261. state + 1));
  1262. }
  1263. mutex_unlock(&local->sta_mtx);
  1264. /* reconfigure tx conf */
  1265. if (hw->queues >= IEEE80211_NUM_ACS) {
  1266. list_for_each_entry(sdata, &local->interfaces, list) {
  1267. if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN ||
  1268. sdata->vif.type == NL80211_IFTYPE_MONITOR ||
  1269. !ieee80211_sdata_running(sdata))
  1270. continue;
  1271. for (i = 0; i < IEEE80211_NUM_ACS; i++)
  1272. drv_conf_tx(local, sdata, i,
  1273. &sdata->tx_conf[i]);
  1274. }
  1275. }
  1276. /* reconfigure hardware */
  1277. ieee80211_hw_config(local, ~0);
  1278. ieee80211_configure_filter(local);
  1279. /* Finally also reconfigure all the BSS information */
  1280. list_for_each_entry(sdata, &local->interfaces, list) {
  1281. u32 changed;
  1282. if (!ieee80211_sdata_running(sdata))
  1283. continue;
  1284. /* common change flags for all interface types */
  1285. changed = BSS_CHANGED_ERP_CTS_PROT |
  1286. BSS_CHANGED_ERP_PREAMBLE |
  1287. BSS_CHANGED_ERP_SLOT |
  1288. BSS_CHANGED_HT |
  1289. BSS_CHANGED_BASIC_RATES |
  1290. BSS_CHANGED_BEACON_INT |
  1291. BSS_CHANGED_BSSID |
  1292. BSS_CHANGED_CQM |
  1293. BSS_CHANGED_QOS |
  1294. BSS_CHANGED_IDLE |
  1295. BSS_CHANGED_TXPOWER;
  1296. switch (sdata->vif.type) {
  1297. case NL80211_IFTYPE_STATION:
  1298. changed |= BSS_CHANGED_ASSOC |
  1299. BSS_CHANGED_ARP_FILTER |
  1300. BSS_CHANGED_PS;
  1301. mutex_lock(&sdata->u.mgd.mtx);
  1302. ieee80211_bss_info_change_notify(sdata, changed);
  1303. mutex_unlock(&sdata->u.mgd.mtx);
  1304. break;
  1305. case NL80211_IFTYPE_ADHOC:
  1306. changed |= BSS_CHANGED_IBSS;
  1307. /* fall through */
  1308. case NL80211_IFTYPE_AP:
  1309. changed |= BSS_CHANGED_SSID;
  1310. if (sdata->vif.type == NL80211_IFTYPE_AP) {
  1311. changed |= BSS_CHANGED_AP_PROBE_RESP;
  1312. if (rcu_access_pointer(sdata->u.ap.beacon))
  1313. drv_start_ap(local, sdata);
  1314. }
  1315. /* fall through */
  1316. case NL80211_IFTYPE_MESH_POINT:
  1317. changed |= BSS_CHANGED_BEACON |
  1318. BSS_CHANGED_BEACON_ENABLED;
  1319. ieee80211_bss_info_change_notify(sdata, changed);
  1320. break;
  1321. case NL80211_IFTYPE_WDS:
  1322. break;
  1323. case NL80211_IFTYPE_AP_VLAN:
  1324. case NL80211_IFTYPE_MONITOR:
  1325. /* ignore virtual */
  1326. break;
  1327. case NL80211_IFTYPE_P2P_DEVICE:
  1328. changed = BSS_CHANGED_IDLE;
  1329. break;
  1330. case NL80211_IFTYPE_UNSPECIFIED:
  1331. case NUM_NL80211_IFTYPES:
  1332. case NL80211_IFTYPE_P2P_CLIENT:
  1333. case NL80211_IFTYPE_P2P_GO:
  1334. WARN_ON(1);
  1335. break;
  1336. }
  1337. }
  1338. ieee80211_recalc_ps(local, -1);
  1339. /*
  1340. * The sta might be in psm against the ap (e.g. because
  1341. * this was the state before a hw restart), so we
  1342. * explicitly send a null packet in order to make sure
  1343. * it'll sync against the ap (and get out of psm).
  1344. */
  1345. if (!(local->hw.conf.flags & IEEE80211_CONF_PS)) {
  1346. list_for_each_entry(sdata, &local->interfaces, list) {
  1347. if (sdata->vif.type != NL80211_IFTYPE_STATION)
  1348. continue;
  1349. if (!sdata->u.mgd.associated)
  1350. continue;
  1351. ieee80211_send_nullfunc(local, sdata, 0);
  1352. }
  1353. }
  1354. /* APs are now beaconing, add back stations */
  1355. mutex_lock(&local->sta_mtx);
  1356. list_for_each_entry(sta, &local->sta_list, list) {
  1357. enum ieee80211_sta_state state;
  1358. if (!sta->uploaded)
  1359. continue;
  1360. if (sta->sdata->vif.type != NL80211_IFTYPE_AP)
  1361. continue;
  1362. for (state = IEEE80211_STA_NOTEXIST;
  1363. state < sta->sta_state; state++)
  1364. WARN_ON(drv_sta_state(local, sta->sdata, sta, state,
  1365. state + 1));
  1366. }
  1367. mutex_unlock(&local->sta_mtx);
  1368. /* add back keys */
  1369. list_for_each_entry(sdata, &local->interfaces, list)
  1370. if (ieee80211_sdata_running(sdata))
  1371. ieee80211_enable_keys(sdata);
  1372. wake_up:
  1373. local->in_reconfig = false;
  1374. barrier();
  1375. /*
  1376. * Clear the WLAN_STA_BLOCK_BA flag so new aggregation
  1377. * sessions can be established after a resume.
  1378. *
  1379. * Also tear down aggregation sessions since reconfiguring
  1380. * them in a hardware restart scenario is not easily done
  1381. * right now, and the hardware will have lost information
  1382. * about the sessions, but we and the AP still think they
  1383. * are active. This is really a workaround though.
  1384. */
  1385. if (hw->flags & IEEE80211_HW_AMPDU_AGGREGATION) {
  1386. mutex_lock(&local->sta_mtx);
  1387. list_for_each_entry(sta, &local->sta_list, list) {
  1388. ieee80211_sta_tear_down_BA_sessions(sta, true);
  1389. clear_sta_flag(sta, WLAN_STA_BLOCK_BA);
  1390. }
  1391. mutex_unlock(&local->sta_mtx);
  1392. }
  1393. ieee80211_wake_queues_by_reason(hw,
  1394. IEEE80211_QUEUE_STOP_REASON_SUSPEND);
  1395. /*
  1396. * If this is for hw restart things are still running.
  1397. * We may want to change that later, however.
  1398. */
  1399. if (!local->suspended) {
  1400. drv_restart_complete(local);
  1401. return 0;
  1402. }
  1403. #ifdef CONFIG_PM
  1404. /* first set suspended false, then resuming */
  1405. local->suspended = false;
  1406. mb();
  1407. local->resuming = false;
  1408. list_for_each_entry(sdata, &local->interfaces, list) {
  1409. switch(sdata->vif.type) {
  1410. case NL80211_IFTYPE_STATION:
  1411. ieee80211_sta_restart(sdata);
  1412. break;
  1413. case NL80211_IFTYPE_ADHOC:
  1414. ieee80211_ibss_restart(sdata);
  1415. break;
  1416. case NL80211_IFTYPE_MESH_POINT:
  1417. ieee80211_mesh_restart(sdata);
  1418. break;
  1419. default:
  1420. break;
  1421. }
  1422. }
  1423. mod_timer(&local->sta_cleanup, jiffies + 1);
  1424. mutex_lock(&local->sta_mtx);
  1425. list_for_each_entry(sta, &local->sta_list, list)
  1426. mesh_plink_restart(sta);
  1427. mutex_unlock(&local->sta_mtx);
  1428. #else
  1429. WARN_ON(1);
  1430. #endif
  1431. return 0;
  1432. }
  1433. void ieee80211_resume_disconnect(struct ieee80211_vif *vif)
  1434. {
  1435. struct ieee80211_sub_if_data *sdata;
  1436. struct ieee80211_local *local;
  1437. struct ieee80211_key *key;
  1438. if (WARN_ON(!vif))
  1439. return;
  1440. sdata = vif_to_sdata(vif);
  1441. local = sdata->local;
  1442. if (WARN_ON(!local->resuming))
  1443. return;
  1444. if (WARN_ON(vif->type != NL80211_IFTYPE_STATION))
  1445. return;
  1446. sdata->flags |= IEEE80211_SDATA_DISCONNECT_RESUME;
  1447. mutex_lock(&local->key_mtx);
  1448. list_for_each_entry(key, &sdata->key_list, list)
  1449. key->flags |= KEY_FLAG_TAINTED;
  1450. mutex_unlock(&local->key_mtx);
  1451. }
  1452. EXPORT_SYMBOL_GPL(ieee80211_resume_disconnect);
  1453. void ieee80211_recalc_smps(struct ieee80211_sub_if_data *sdata)
  1454. {
  1455. struct ieee80211_local *local = sdata->local;
  1456. struct ieee80211_chanctx_conf *chanctx_conf;
  1457. struct ieee80211_chanctx *chanctx;
  1458. mutex_lock(&local->chanctx_mtx);
  1459. chanctx_conf = rcu_dereference_protected(sdata->vif.chanctx_conf,
  1460. lockdep_is_held(&local->chanctx_mtx));
  1461. if (WARN_ON_ONCE(!chanctx_conf))
  1462. goto unlock;
  1463. chanctx = container_of(chanctx_conf, struct ieee80211_chanctx, conf);
  1464. ieee80211_recalc_smps_chanctx(local, chanctx);
  1465. unlock:
  1466. mutex_unlock(&local->chanctx_mtx);
  1467. }
  1468. static bool ieee80211_id_in_list(const u8 *ids, int n_ids, u8 id)
  1469. {
  1470. int i;
  1471. for (i = 0; i < n_ids; i++)
  1472. if (ids[i] == id)
  1473. return true;
  1474. return false;
  1475. }
  1476. /**
  1477. * ieee80211_ie_split - split an IE buffer according to ordering
  1478. *
  1479. * @ies: the IE buffer
  1480. * @ielen: the length of the IE buffer
  1481. * @ids: an array with element IDs that are allowed before
  1482. * the split
  1483. * @n_ids: the size of the element ID array
  1484. * @offset: offset where to start splitting in the buffer
  1485. *
  1486. * This function splits an IE buffer by updating the @offset
  1487. * variable to point to the location where the buffer should be
  1488. * split.
  1489. *
  1490. * It assumes that the given IE buffer is well-formed, this
  1491. * has to be guaranteed by the caller!
  1492. *
  1493. * It also assumes that the IEs in the buffer are ordered
  1494. * correctly, if not the result of using this function will not
  1495. * be ordered correctly either, i.e. it does no reordering.
  1496. *
  1497. * The function returns the offset where the next part of the
  1498. * buffer starts, which may be @ielen if the entire (remainder)
  1499. * of the buffer should be used.
  1500. */
  1501. size_t ieee80211_ie_split(const u8 *ies, size_t ielen,
  1502. const u8 *ids, int n_ids, size_t offset)
  1503. {
  1504. size_t pos = offset;
  1505. while (pos < ielen && ieee80211_id_in_list(ids, n_ids, ies[pos]))
  1506. pos += 2 + ies[pos + 1];
  1507. return pos;
  1508. }
  1509. size_t ieee80211_ie_split_vendor(const u8 *ies, size_t ielen, size_t offset)
  1510. {
  1511. size_t pos = offset;
  1512. while (pos < ielen && ies[pos] != WLAN_EID_VENDOR_SPECIFIC)
  1513. pos += 2 + ies[pos + 1];
  1514. return pos;
  1515. }
  1516. static void _ieee80211_enable_rssi_reports(struct ieee80211_sub_if_data *sdata,
  1517. int rssi_min_thold,
  1518. int rssi_max_thold)
  1519. {
  1520. trace_api_enable_rssi_reports(sdata, rssi_min_thold, rssi_max_thold);
  1521. if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_STATION))
  1522. return;
  1523. /*
  1524. * Scale up threshold values before storing it, as the RSSI averaging
  1525. * algorithm uses a scaled up value as well. Change this scaling
  1526. * factor if the RSSI averaging algorithm changes.
  1527. */
  1528. sdata->u.mgd.rssi_min_thold = rssi_min_thold*16;
  1529. sdata->u.mgd.rssi_max_thold = rssi_max_thold*16;
  1530. }
  1531. void ieee80211_enable_rssi_reports(struct ieee80211_vif *vif,
  1532. int rssi_min_thold,
  1533. int rssi_max_thold)
  1534. {
  1535. struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
  1536. WARN_ON(rssi_min_thold == rssi_max_thold ||
  1537. rssi_min_thold > rssi_max_thold);
  1538. _ieee80211_enable_rssi_reports(sdata, rssi_min_thold,
  1539. rssi_max_thold);
  1540. }
  1541. EXPORT_SYMBOL(ieee80211_enable_rssi_reports);
  1542. void ieee80211_disable_rssi_reports(struct ieee80211_vif *vif)
  1543. {
  1544. struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
  1545. _ieee80211_enable_rssi_reports(sdata, 0, 0);
  1546. }
  1547. EXPORT_SYMBOL(ieee80211_disable_rssi_reports);
  1548. u8 *ieee80211_ie_build_ht_cap(u8 *pos, struct ieee80211_sta_ht_cap *ht_cap,
  1549. u16 cap)
  1550. {
  1551. __le16 tmp;
  1552. *pos++ = WLAN_EID_HT_CAPABILITY;
  1553. *pos++ = sizeof(struct ieee80211_ht_cap);
  1554. memset(pos, 0, sizeof(struct ieee80211_ht_cap));
  1555. /* capability flags */
  1556. tmp = cpu_to_le16(cap);
  1557. memcpy(pos, &tmp, sizeof(u16));
  1558. pos += sizeof(u16);
  1559. /* AMPDU parameters */
  1560. *pos++ = ht_cap->ampdu_factor |
  1561. (ht_cap->ampdu_density <<
  1562. IEEE80211_HT_AMPDU_PARM_DENSITY_SHIFT);
  1563. /* MCS set */
  1564. memcpy(pos, &ht_cap->mcs, sizeof(ht_cap->mcs));
  1565. pos += sizeof(ht_cap->mcs);
  1566. /* extended capabilities */
  1567. pos += sizeof(__le16);
  1568. /* BF capabilities */
  1569. pos += sizeof(__le32);
  1570. /* antenna selection */
  1571. pos += sizeof(u8);
  1572. return pos;
  1573. }
  1574. u8 *ieee80211_ie_build_vht_cap(u8 *pos, struct ieee80211_sta_vht_cap *vht_cap,
  1575. u32 cap)
  1576. {
  1577. __le32 tmp;
  1578. *pos++ = WLAN_EID_VHT_CAPABILITY;
  1579. *pos++ = sizeof(struct ieee80211_vht_cap);
  1580. memset(pos, 0, sizeof(struct ieee80211_vht_cap));
  1581. /* capability flags */
  1582. tmp = cpu_to_le32(cap);
  1583. memcpy(pos, &tmp, sizeof(u32));
  1584. pos += sizeof(u32);
  1585. /* VHT MCS set */
  1586. memcpy(pos, &vht_cap->vht_mcs, sizeof(vht_cap->vht_mcs));
  1587. pos += sizeof(vht_cap->vht_mcs);
  1588. return pos;
  1589. }
  1590. u8 *ieee80211_ie_build_ht_oper(u8 *pos, struct ieee80211_sta_ht_cap *ht_cap,
  1591. struct ieee80211_channel *channel,
  1592. enum nl80211_channel_type channel_type,
  1593. u16 prot_mode)
  1594. {
  1595. struct ieee80211_ht_operation *ht_oper;
  1596. /* Build HT Information */
  1597. *pos++ = WLAN_EID_HT_OPERATION;
  1598. *pos++ = sizeof(struct ieee80211_ht_operation);
  1599. ht_oper = (struct ieee80211_ht_operation *)pos;
  1600. ht_oper->primary_chan =
  1601. ieee80211_frequency_to_channel(channel->center_freq);
  1602. switch (channel_type) {
  1603. case NL80211_CHAN_HT40MINUS:
  1604. ht_oper->ht_param = IEEE80211_HT_PARAM_CHA_SEC_BELOW;
  1605. break;
  1606. case NL80211_CHAN_HT40PLUS:
  1607. ht_oper->ht_param = IEEE80211_HT_PARAM_CHA_SEC_ABOVE;
  1608. break;
  1609. case NL80211_CHAN_HT20:
  1610. default:
  1611. ht_oper->ht_param = IEEE80211_HT_PARAM_CHA_SEC_NONE;
  1612. break;
  1613. }
  1614. if (ht_cap->cap & IEEE80211_HT_CAP_SUP_WIDTH_20_40 &&
  1615. channel_type != NL80211_CHAN_NO_HT &&
  1616. channel_type != NL80211_CHAN_HT20)
  1617. ht_oper->ht_param |= IEEE80211_HT_PARAM_CHAN_WIDTH_ANY;
  1618. ht_oper->operation_mode = cpu_to_le16(prot_mode);
  1619. ht_oper->stbc_param = 0x0000;
  1620. /* It seems that Basic MCS set and Supported MCS set
  1621. are identical for the first 10 bytes */
  1622. memset(&ht_oper->basic_set, 0, 16);
  1623. memcpy(&ht_oper->basic_set, &ht_cap->mcs, 10);
  1624. return pos + sizeof(struct ieee80211_ht_operation);
  1625. }
  1626. enum nl80211_channel_type
  1627. ieee80211_ht_oper_to_channel_type(struct ieee80211_ht_operation *ht_oper)
  1628. {
  1629. enum nl80211_channel_type channel_type;
  1630. if (!ht_oper)
  1631. return NL80211_CHAN_NO_HT;
  1632. switch (ht_oper->ht_param & IEEE80211_HT_PARAM_CHA_SEC_OFFSET) {
  1633. case IEEE80211_HT_PARAM_CHA_SEC_NONE:
  1634. channel_type = NL80211_CHAN_HT20;
  1635. break;
  1636. case IEEE80211_HT_PARAM_CHA_SEC_ABOVE:
  1637. channel_type = NL80211_CHAN_HT40PLUS;
  1638. break;
  1639. case IEEE80211_HT_PARAM_CHA_SEC_BELOW:
  1640. channel_type = NL80211_CHAN_HT40MINUS;
  1641. break;
  1642. default:
  1643. channel_type = NL80211_CHAN_NO_HT;
  1644. }
  1645. return channel_type;
  1646. }
  1647. int ieee80211_add_srates_ie(struct ieee80211_sub_if_data *sdata,
  1648. struct sk_buff *skb, bool need_basic,
  1649. enum ieee80211_band band)
  1650. {
  1651. struct ieee80211_local *local = sdata->local;
  1652. struct ieee80211_supported_band *sband;
  1653. int rate;
  1654. u8 i, rates, *pos;
  1655. u32 basic_rates = sdata->vif.bss_conf.basic_rates;
  1656. sband = local->hw.wiphy->bands[band];
  1657. rates = sband->n_bitrates;
  1658. if (rates > 8)
  1659. rates = 8;
  1660. if (skb_tailroom(skb) < rates + 2)
  1661. return -ENOMEM;
  1662. pos = skb_put(skb, rates + 2);
  1663. *pos++ = WLAN_EID_SUPP_RATES;
  1664. *pos++ = rates;
  1665. for (i = 0; i < rates; i++) {
  1666. u8 basic = 0;
  1667. if (need_basic && basic_rates & BIT(i))
  1668. basic = 0x80;
  1669. rate = sband->bitrates[i].bitrate;
  1670. *pos++ = basic | (u8) (rate / 5);
  1671. }
  1672. return 0;
  1673. }
  1674. int ieee80211_add_ext_srates_ie(struct ieee80211_sub_if_data *sdata,
  1675. struct sk_buff *skb, bool need_basic,
  1676. enum ieee80211_band band)
  1677. {
  1678. struct ieee80211_local *local = sdata->local;
  1679. struct ieee80211_supported_band *sband;
  1680. int rate;
  1681. u8 i, exrates, *pos;
  1682. u32 basic_rates = sdata->vif.bss_conf.basic_rates;
  1683. sband = local->hw.wiphy->bands[band];
  1684. exrates = sband->n_bitrates;
  1685. if (exrates > 8)
  1686. exrates -= 8;
  1687. else
  1688. exrates = 0;
  1689. if (skb_tailroom(skb) < exrates + 2)
  1690. return -ENOMEM;
  1691. if (exrates) {
  1692. pos = skb_put(skb, exrates + 2);
  1693. *pos++ = WLAN_EID_EXT_SUPP_RATES;
  1694. *pos++ = exrates;
  1695. for (i = 8; i < sband->n_bitrates; i++) {
  1696. u8 basic = 0;
  1697. if (need_basic && basic_rates & BIT(i))
  1698. basic = 0x80;
  1699. rate = sband->bitrates[i].bitrate;
  1700. *pos++ = basic | (u8) (rate / 5);
  1701. }
  1702. }
  1703. return 0;
  1704. }
  1705. int ieee80211_ave_rssi(struct ieee80211_vif *vif)
  1706. {
  1707. struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
  1708. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  1709. if (WARN_ON_ONCE(sdata->vif.type != NL80211_IFTYPE_STATION)) {
  1710. /* non-managed type inferfaces */
  1711. return 0;
  1712. }
  1713. return ifmgd->ave_beacon_signal;
  1714. }
  1715. EXPORT_SYMBOL_GPL(ieee80211_ave_rssi);
  1716. u8 ieee80211_mcs_to_chains(const struct ieee80211_mcs_info *mcs)
  1717. {
  1718. if (!mcs)
  1719. return 1;
  1720. /* TODO: consider rx_highest */
  1721. if (mcs->rx_mask[3])
  1722. return 4;
  1723. if (mcs->rx_mask[2])
  1724. return 3;
  1725. if (mcs->rx_mask[1])
  1726. return 2;
  1727. return 1;
  1728. }
  1729. /**
  1730. * ieee80211_calculate_rx_timestamp - calculate timestamp in frame
  1731. * @local: mac80211 hw info struct
  1732. * @status: RX status
  1733. * @mpdu_len: total MPDU length (including FCS)
  1734. * @mpdu_offset: offset into MPDU to calculate timestamp at
  1735. *
  1736. * This function calculates the RX timestamp at the given MPDU offset, taking
  1737. * into account what the RX timestamp was. An offset of 0 will just normalize
  1738. * the timestamp to TSF at beginning of MPDU reception.
  1739. */
  1740. u64 ieee80211_calculate_rx_timestamp(struct ieee80211_local *local,
  1741. struct ieee80211_rx_status *status,
  1742. unsigned int mpdu_len,
  1743. unsigned int mpdu_offset)
  1744. {
  1745. u64 ts = status->mactime;
  1746. struct rate_info ri;
  1747. u16 rate;
  1748. if (WARN_ON(!ieee80211_have_rx_timestamp(status)))
  1749. return 0;
  1750. memset(&ri, 0, sizeof(ri));
  1751. /* Fill cfg80211 rate info */
  1752. if (status->flag & RX_FLAG_HT) {
  1753. ri.mcs = status->rate_idx;
  1754. ri.flags |= RATE_INFO_FLAGS_MCS;
  1755. if (status->flag & RX_FLAG_40MHZ)
  1756. ri.flags |= RATE_INFO_FLAGS_40_MHZ_WIDTH;
  1757. if (status->flag & RX_FLAG_SHORT_GI)
  1758. ri.flags |= RATE_INFO_FLAGS_SHORT_GI;
  1759. } else {
  1760. struct ieee80211_supported_band *sband;
  1761. sband = local->hw.wiphy->bands[status->band];
  1762. ri.legacy = sband->bitrates[status->rate_idx].bitrate;
  1763. }
  1764. rate = cfg80211_calculate_bitrate(&ri);
  1765. /* rewind from end of MPDU */
  1766. if (status->flag & RX_FLAG_MACTIME_END)
  1767. ts -= mpdu_len * 8 * 10 / rate;
  1768. ts += mpdu_offset * 8 * 10 / rate;
  1769. return ts;
  1770. }