util.c 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425
  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 <net/net_namespace.h>
  23. #include <net/cfg80211.h>
  24. #include <net/rtnetlink.h>
  25. #include "ieee80211_i.h"
  26. #include "driver-ops.h"
  27. #include "rate.h"
  28. #include "mesh.h"
  29. #include "wme.h"
  30. #include "led.h"
  31. #include "wep.h"
  32. /* privid for wiphys to determine whether they belong to us or not */
  33. void *mac80211_wiphy_privid = &mac80211_wiphy_privid;
  34. struct ieee80211_hw *wiphy_to_ieee80211_hw(struct wiphy *wiphy)
  35. {
  36. struct ieee80211_local *local;
  37. BUG_ON(!wiphy);
  38. local = wiphy_priv(wiphy);
  39. return &local->hw;
  40. }
  41. EXPORT_SYMBOL(wiphy_to_ieee80211_hw);
  42. u8 *ieee80211_get_bssid(struct ieee80211_hdr *hdr, size_t len,
  43. enum nl80211_iftype type)
  44. {
  45. __le16 fc = hdr->frame_control;
  46. /* drop ACK/CTS frames and incorrect hdr len (ctrl) */
  47. if (len < 16)
  48. return NULL;
  49. if (ieee80211_is_data(fc)) {
  50. if (len < 24) /* drop incorrect hdr len (data) */
  51. return NULL;
  52. if (ieee80211_has_a4(fc))
  53. return NULL;
  54. if (ieee80211_has_tods(fc))
  55. return hdr->addr1;
  56. if (ieee80211_has_fromds(fc))
  57. return hdr->addr2;
  58. return hdr->addr3;
  59. }
  60. if (ieee80211_is_mgmt(fc)) {
  61. if (len < 24) /* drop incorrect hdr len (mgmt) */
  62. return NULL;
  63. return hdr->addr3;
  64. }
  65. if (ieee80211_is_ctl(fc)) {
  66. if(ieee80211_is_pspoll(fc))
  67. return hdr->addr1;
  68. if (ieee80211_is_back_req(fc)) {
  69. switch (type) {
  70. case NL80211_IFTYPE_STATION:
  71. return hdr->addr2;
  72. case NL80211_IFTYPE_AP:
  73. case NL80211_IFTYPE_AP_VLAN:
  74. return hdr->addr1;
  75. default:
  76. break; /* fall through to the return */
  77. }
  78. }
  79. }
  80. return NULL;
  81. }
  82. void ieee80211_tx_set_protected(struct ieee80211_tx_data *tx)
  83. {
  84. struct sk_buff *skb = tx->skb;
  85. struct ieee80211_hdr *hdr;
  86. do {
  87. hdr = (struct ieee80211_hdr *) skb->data;
  88. hdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_PROTECTED);
  89. } while ((skb = skb->next));
  90. }
  91. int ieee80211_frame_duration(struct ieee80211_local *local, size_t len,
  92. int rate, int erp, int short_preamble)
  93. {
  94. int dur;
  95. /* calculate duration (in microseconds, rounded up to next higher
  96. * integer if it includes a fractional microsecond) to send frame of
  97. * len bytes (does not include FCS) at the given rate. Duration will
  98. * also include SIFS.
  99. *
  100. * rate is in 100 kbps, so divident is multiplied by 10 in the
  101. * DIV_ROUND_UP() operations.
  102. */
  103. if (local->hw.conf.channel->band == IEEE80211_BAND_5GHZ || erp) {
  104. /*
  105. * OFDM:
  106. *
  107. * N_DBPS = DATARATE x 4
  108. * N_SYM = Ceiling((16+8xLENGTH+6) / N_DBPS)
  109. * (16 = SIGNAL time, 6 = tail bits)
  110. * TXTIME = T_PREAMBLE + T_SIGNAL + T_SYM x N_SYM + Signal Ext
  111. *
  112. * T_SYM = 4 usec
  113. * 802.11a - 17.5.2: aSIFSTime = 16 usec
  114. * 802.11g - 19.8.4: aSIFSTime = 10 usec +
  115. * signal ext = 6 usec
  116. */
  117. dur = 16; /* SIFS + signal ext */
  118. dur += 16; /* 17.3.2.3: T_PREAMBLE = 16 usec */
  119. dur += 4; /* 17.3.2.3: T_SIGNAL = 4 usec */
  120. dur += 4 * DIV_ROUND_UP((16 + 8 * (len + 4) + 6) * 10,
  121. 4 * rate); /* T_SYM x N_SYM */
  122. } else {
  123. /*
  124. * 802.11b or 802.11g with 802.11b compatibility:
  125. * 18.3.4: TXTIME = PreambleLength + PLCPHeaderTime +
  126. * Ceiling(((LENGTH+PBCC)x8)/DATARATE). PBCC=0.
  127. *
  128. * 802.11 (DS): 15.3.3, 802.11b: 18.3.4
  129. * aSIFSTime = 10 usec
  130. * aPreambleLength = 144 usec or 72 usec with short preamble
  131. * aPLCPHeaderLength = 48 usec or 24 usec with short preamble
  132. */
  133. dur = 10; /* aSIFSTime = 10 usec */
  134. dur += short_preamble ? (72 + 24) : (144 + 48);
  135. dur += DIV_ROUND_UP(8 * (len + 4) * 10, rate);
  136. }
  137. return dur;
  138. }
  139. /* Exported duration function for driver use */
  140. __le16 ieee80211_generic_frame_duration(struct ieee80211_hw *hw,
  141. struct ieee80211_vif *vif,
  142. size_t frame_len,
  143. struct ieee80211_rate *rate)
  144. {
  145. struct ieee80211_local *local = hw_to_local(hw);
  146. struct ieee80211_sub_if_data *sdata;
  147. u16 dur;
  148. int erp;
  149. bool short_preamble = false;
  150. erp = 0;
  151. if (vif) {
  152. sdata = vif_to_sdata(vif);
  153. short_preamble = sdata->vif.bss_conf.use_short_preamble;
  154. if (sdata->flags & IEEE80211_SDATA_OPERATING_GMODE)
  155. erp = rate->flags & IEEE80211_RATE_ERP_G;
  156. }
  157. dur = ieee80211_frame_duration(local, frame_len, rate->bitrate, erp,
  158. short_preamble);
  159. return cpu_to_le16(dur);
  160. }
  161. EXPORT_SYMBOL(ieee80211_generic_frame_duration);
  162. __le16 ieee80211_rts_duration(struct ieee80211_hw *hw,
  163. struct ieee80211_vif *vif, size_t frame_len,
  164. const struct ieee80211_tx_info *frame_txctl)
  165. {
  166. struct ieee80211_local *local = hw_to_local(hw);
  167. struct ieee80211_rate *rate;
  168. struct ieee80211_sub_if_data *sdata;
  169. bool short_preamble;
  170. int erp;
  171. u16 dur;
  172. struct ieee80211_supported_band *sband;
  173. sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
  174. short_preamble = false;
  175. rate = &sband->bitrates[frame_txctl->control.rts_cts_rate_idx];
  176. erp = 0;
  177. if (vif) {
  178. sdata = vif_to_sdata(vif);
  179. short_preamble = sdata->vif.bss_conf.use_short_preamble;
  180. if (sdata->flags & IEEE80211_SDATA_OPERATING_GMODE)
  181. erp = rate->flags & IEEE80211_RATE_ERP_G;
  182. }
  183. /* CTS duration */
  184. dur = ieee80211_frame_duration(local, 10, rate->bitrate,
  185. erp, short_preamble);
  186. /* Data frame duration */
  187. dur += ieee80211_frame_duration(local, frame_len, rate->bitrate,
  188. erp, short_preamble);
  189. /* ACK duration */
  190. dur += ieee80211_frame_duration(local, 10, rate->bitrate,
  191. erp, short_preamble);
  192. return cpu_to_le16(dur);
  193. }
  194. EXPORT_SYMBOL(ieee80211_rts_duration);
  195. __le16 ieee80211_ctstoself_duration(struct ieee80211_hw *hw,
  196. struct ieee80211_vif *vif,
  197. size_t frame_len,
  198. const struct ieee80211_tx_info *frame_txctl)
  199. {
  200. struct ieee80211_local *local = hw_to_local(hw);
  201. struct ieee80211_rate *rate;
  202. struct ieee80211_sub_if_data *sdata;
  203. bool short_preamble;
  204. int erp;
  205. u16 dur;
  206. struct ieee80211_supported_band *sband;
  207. sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
  208. short_preamble = false;
  209. rate = &sband->bitrates[frame_txctl->control.rts_cts_rate_idx];
  210. erp = 0;
  211. if (vif) {
  212. sdata = vif_to_sdata(vif);
  213. short_preamble = sdata->vif.bss_conf.use_short_preamble;
  214. if (sdata->flags & IEEE80211_SDATA_OPERATING_GMODE)
  215. erp = rate->flags & IEEE80211_RATE_ERP_G;
  216. }
  217. /* Data frame duration */
  218. dur = ieee80211_frame_duration(local, frame_len, rate->bitrate,
  219. erp, short_preamble);
  220. if (!(frame_txctl->flags & IEEE80211_TX_CTL_NO_ACK)) {
  221. /* ACK duration */
  222. dur += ieee80211_frame_duration(local, 10, rate->bitrate,
  223. erp, short_preamble);
  224. }
  225. return cpu_to_le16(dur);
  226. }
  227. EXPORT_SYMBOL(ieee80211_ctstoself_duration);
  228. static void __ieee80211_wake_queue(struct ieee80211_hw *hw, int queue,
  229. enum queue_stop_reason reason)
  230. {
  231. struct ieee80211_local *local = hw_to_local(hw);
  232. struct ieee80211_sub_if_data *sdata;
  233. trace_wake_queue(local, queue, reason);
  234. if (WARN_ON(queue >= hw->queues))
  235. return;
  236. __clear_bit(reason, &local->queue_stop_reasons[queue]);
  237. if (local->queue_stop_reasons[queue] != 0)
  238. /* someone still has this queue stopped */
  239. return;
  240. if (skb_queue_empty(&local->pending[queue])) {
  241. rcu_read_lock();
  242. list_for_each_entry_rcu(sdata, &local->interfaces, list) {
  243. if (test_bit(SDATA_STATE_OFFCHANNEL, &sdata->state))
  244. continue;
  245. netif_wake_subqueue(sdata->dev, queue);
  246. }
  247. rcu_read_unlock();
  248. } else
  249. tasklet_schedule(&local->tx_pending_tasklet);
  250. }
  251. void ieee80211_wake_queue_by_reason(struct ieee80211_hw *hw, int queue,
  252. enum queue_stop_reason reason)
  253. {
  254. struct ieee80211_local *local = hw_to_local(hw);
  255. unsigned long flags;
  256. spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
  257. __ieee80211_wake_queue(hw, queue, reason);
  258. spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
  259. }
  260. void ieee80211_wake_queue(struct ieee80211_hw *hw, int queue)
  261. {
  262. ieee80211_wake_queue_by_reason(hw, queue,
  263. IEEE80211_QUEUE_STOP_REASON_DRIVER);
  264. }
  265. EXPORT_SYMBOL(ieee80211_wake_queue);
  266. static void __ieee80211_stop_queue(struct ieee80211_hw *hw, int queue,
  267. enum queue_stop_reason reason)
  268. {
  269. struct ieee80211_local *local = hw_to_local(hw);
  270. struct ieee80211_sub_if_data *sdata;
  271. trace_stop_queue(local, queue, reason);
  272. if (WARN_ON(queue >= hw->queues))
  273. return;
  274. __set_bit(reason, &local->queue_stop_reasons[queue]);
  275. rcu_read_lock();
  276. list_for_each_entry_rcu(sdata, &local->interfaces, list)
  277. netif_stop_subqueue(sdata->dev, queue);
  278. rcu_read_unlock();
  279. }
  280. void ieee80211_stop_queue_by_reason(struct ieee80211_hw *hw, int queue,
  281. enum queue_stop_reason reason)
  282. {
  283. struct ieee80211_local *local = hw_to_local(hw);
  284. unsigned long flags;
  285. spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
  286. __ieee80211_stop_queue(hw, queue, reason);
  287. spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
  288. }
  289. void ieee80211_stop_queue(struct ieee80211_hw *hw, int queue)
  290. {
  291. ieee80211_stop_queue_by_reason(hw, queue,
  292. IEEE80211_QUEUE_STOP_REASON_DRIVER);
  293. }
  294. EXPORT_SYMBOL(ieee80211_stop_queue);
  295. void ieee80211_add_pending_skb(struct ieee80211_local *local,
  296. struct sk_buff *skb)
  297. {
  298. struct ieee80211_hw *hw = &local->hw;
  299. unsigned long flags;
  300. int queue = skb_get_queue_mapping(skb);
  301. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  302. if (WARN_ON(!info->control.vif)) {
  303. kfree_skb(skb);
  304. return;
  305. }
  306. spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
  307. __ieee80211_stop_queue(hw, queue, IEEE80211_QUEUE_STOP_REASON_SKB_ADD);
  308. __skb_queue_tail(&local->pending[queue], skb);
  309. __ieee80211_wake_queue(hw, queue, IEEE80211_QUEUE_STOP_REASON_SKB_ADD);
  310. spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
  311. }
  312. void ieee80211_add_pending_skbs_fn(struct ieee80211_local *local,
  313. struct sk_buff_head *skbs,
  314. void (*fn)(void *data), void *data)
  315. {
  316. struct ieee80211_hw *hw = &local->hw;
  317. struct sk_buff *skb;
  318. unsigned long flags;
  319. int queue, i;
  320. spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
  321. for (i = 0; i < hw->queues; i++)
  322. __ieee80211_stop_queue(hw, i,
  323. IEEE80211_QUEUE_STOP_REASON_SKB_ADD);
  324. while ((skb = skb_dequeue(skbs))) {
  325. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  326. if (WARN_ON(!info->control.vif)) {
  327. kfree_skb(skb);
  328. continue;
  329. }
  330. queue = skb_get_queue_mapping(skb);
  331. __skb_queue_tail(&local->pending[queue], skb);
  332. }
  333. if (fn)
  334. fn(data);
  335. for (i = 0; i < hw->queues; i++)
  336. __ieee80211_wake_queue(hw, i,
  337. IEEE80211_QUEUE_STOP_REASON_SKB_ADD);
  338. spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
  339. }
  340. void ieee80211_add_pending_skbs(struct ieee80211_local *local,
  341. struct sk_buff_head *skbs)
  342. {
  343. ieee80211_add_pending_skbs_fn(local, skbs, NULL, NULL);
  344. }
  345. void ieee80211_stop_queues_by_reason(struct ieee80211_hw *hw,
  346. enum queue_stop_reason reason)
  347. {
  348. struct ieee80211_local *local = hw_to_local(hw);
  349. unsigned long flags;
  350. int i;
  351. spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
  352. for (i = 0; i < hw->queues; i++)
  353. __ieee80211_stop_queue(hw, i, reason);
  354. spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
  355. }
  356. void ieee80211_stop_queues(struct ieee80211_hw *hw)
  357. {
  358. ieee80211_stop_queues_by_reason(hw,
  359. IEEE80211_QUEUE_STOP_REASON_DRIVER);
  360. }
  361. EXPORT_SYMBOL(ieee80211_stop_queues);
  362. int ieee80211_queue_stopped(struct ieee80211_hw *hw, int queue)
  363. {
  364. struct ieee80211_local *local = hw_to_local(hw);
  365. unsigned long flags;
  366. int ret;
  367. if (WARN_ON(queue >= hw->queues))
  368. return true;
  369. spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
  370. ret = !!local->queue_stop_reasons[queue];
  371. spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
  372. return ret;
  373. }
  374. EXPORT_SYMBOL(ieee80211_queue_stopped);
  375. void ieee80211_wake_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_wake_queue(hw, i, reason);
  384. spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
  385. }
  386. void ieee80211_wake_queues(struct ieee80211_hw *hw)
  387. {
  388. ieee80211_wake_queues_by_reason(hw, IEEE80211_QUEUE_STOP_REASON_DRIVER);
  389. }
  390. EXPORT_SYMBOL(ieee80211_wake_queues);
  391. void ieee80211_iterate_active_interfaces(
  392. struct ieee80211_hw *hw,
  393. void (*iterator)(void *data, u8 *mac,
  394. struct ieee80211_vif *vif),
  395. void *data)
  396. {
  397. struct ieee80211_local *local = hw_to_local(hw);
  398. struct ieee80211_sub_if_data *sdata;
  399. mutex_lock(&local->iflist_mtx);
  400. list_for_each_entry(sdata, &local->interfaces, list) {
  401. switch (sdata->vif.type) {
  402. case NL80211_IFTYPE_MONITOR:
  403. case NL80211_IFTYPE_AP_VLAN:
  404. continue;
  405. default:
  406. break;
  407. }
  408. if (ieee80211_sdata_running(sdata))
  409. iterator(data, sdata->vif.addr,
  410. &sdata->vif);
  411. }
  412. mutex_unlock(&local->iflist_mtx);
  413. }
  414. EXPORT_SYMBOL_GPL(ieee80211_iterate_active_interfaces);
  415. void ieee80211_iterate_active_interfaces_atomic(
  416. struct ieee80211_hw *hw,
  417. void (*iterator)(void *data, u8 *mac,
  418. struct ieee80211_vif *vif),
  419. void *data)
  420. {
  421. struct ieee80211_local *local = hw_to_local(hw);
  422. struct ieee80211_sub_if_data *sdata;
  423. rcu_read_lock();
  424. list_for_each_entry_rcu(sdata, &local->interfaces, list) {
  425. switch (sdata->vif.type) {
  426. case NL80211_IFTYPE_MONITOR:
  427. case NL80211_IFTYPE_AP_VLAN:
  428. continue;
  429. default:
  430. break;
  431. }
  432. if (ieee80211_sdata_running(sdata))
  433. iterator(data, sdata->vif.addr,
  434. &sdata->vif);
  435. }
  436. rcu_read_unlock();
  437. }
  438. EXPORT_SYMBOL_GPL(ieee80211_iterate_active_interfaces_atomic);
  439. /*
  440. * Nothing should have been stuffed into the workqueue during
  441. * the suspend->resume cycle. If this WARN is seen then there
  442. * is a bug with either the driver suspend or something in
  443. * mac80211 stuffing into the workqueue which we haven't yet
  444. * cleared during mac80211's suspend cycle.
  445. */
  446. static bool ieee80211_can_queue_work(struct ieee80211_local *local)
  447. {
  448. if (WARN(local->suspended && !local->resuming,
  449. "queueing ieee80211 work while going to suspend\n"))
  450. return false;
  451. return true;
  452. }
  453. void ieee80211_queue_work(struct ieee80211_hw *hw, struct work_struct *work)
  454. {
  455. struct ieee80211_local *local = hw_to_local(hw);
  456. if (!ieee80211_can_queue_work(local))
  457. return;
  458. queue_work(local->workqueue, work);
  459. }
  460. EXPORT_SYMBOL(ieee80211_queue_work);
  461. void ieee80211_queue_delayed_work(struct ieee80211_hw *hw,
  462. struct delayed_work *dwork,
  463. unsigned long delay)
  464. {
  465. struct ieee80211_local *local = hw_to_local(hw);
  466. if (!ieee80211_can_queue_work(local))
  467. return;
  468. queue_delayed_work(local->workqueue, dwork, delay);
  469. }
  470. EXPORT_SYMBOL(ieee80211_queue_delayed_work);
  471. void ieee802_11_parse_elems(u8 *start, size_t len,
  472. struct ieee802_11_elems *elems)
  473. {
  474. ieee802_11_parse_elems_crc(start, len, elems, 0, 0);
  475. }
  476. void ieee80211_set_wmm_default(struct ieee80211_sub_if_data *sdata)
  477. {
  478. struct ieee80211_local *local = sdata->local;
  479. struct ieee80211_tx_queue_params qparam;
  480. int queue;
  481. bool use_11b;
  482. int aCWmin, aCWmax;
  483. if (!local->ops->conf_tx)
  484. return;
  485. memset(&qparam, 0, sizeof(qparam));
  486. use_11b = (local->hw.conf.channel->band == IEEE80211_BAND_2GHZ) &&
  487. !(sdata->flags & IEEE80211_SDATA_OPERATING_GMODE);
  488. for (queue = 0; queue < local_to_hw(local)->queues; queue++) {
  489. /* Set defaults according to 802.11-2007 Table 7-37 */
  490. aCWmax = 1023;
  491. if (use_11b)
  492. aCWmin = 31;
  493. else
  494. aCWmin = 15;
  495. switch (queue) {
  496. case 3: /* AC_BK */
  497. qparam.cw_max = aCWmax;
  498. qparam.cw_min = aCWmin;
  499. qparam.txop = 0;
  500. qparam.aifs = 7;
  501. break;
  502. default: /* never happens but let's not leave undefined */
  503. case 2: /* AC_BE */
  504. qparam.cw_max = aCWmax;
  505. qparam.cw_min = aCWmin;
  506. qparam.txop = 0;
  507. qparam.aifs = 3;
  508. break;
  509. case 1: /* AC_VI */
  510. qparam.cw_max = aCWmin;
  511. qparam.cw_min = (aCWmin + 1) / 2 - 1;
  512. if (use_11b)
  513. qparam.txop = 6016/32;
  514. else
  515. qparam.txop = 3008/32;
  516. qparam.aifs = 2;
  517. break;
  518. case 0: /* AC_VO */
  519. qparam.cw_max = (aCWmin + 1) / 2 - 1;
  520. qparam.cw_min = (aCWmin + 1) / 4 - 1;
  521. if (use_11b)
  522. qparam.txop = 3264/32;
  523. else
  524. qparam.txop = 1504/32;
  525. qparam.aifs = 2;
  526. break;
  527. }
  528. qparam.uapsd = false;
  529. sdata->tx_conf[queue] = qparam;
  530. drv_conf_tx(local, sdata, queue, &qparam);
  531. }
  532. /* after reinitialize QoS TX queues setting to default,
  533. * disable QoS at all */
  534. if (sdata->vif.type != NL80211_IFTYPE_MONITOR) {
  535. sdata->vif.bss_conf.qos =
  536. sdata->vif.type != NL80211_IFTYPE_STATION;
  537. ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_QOS);
  538. }
  539. }
  540. void ieee80211_sta_def_wmm_params(struct ieee80211_sub_if_data *sdata,
  541. const size_t supp_rates_len,
  542. const u8 *supp_rates)
  543. {
  544. struct ieee80211_local *local = sdata->local;
  545. int i, have_higher_than_11mbit = 0;
  546. /* cf. IEEE 802.11 9.2.12 */
  547. for (i = 0; i < supp_rates_len; i++)
  548. if ((supp_rates[i] & 0x7f) * 5 > 110)
  549. have_higher_than_11mbit = 1;
  550. if (local->hw.conf.channel->band == IEEE80211_BAND_2GHZ &&
  551. have_higher_than_11mbit)
  552. sdata->flags |= IEEE80211_SDATA_OPERATING_GMODE;
  553. else
  554. sdata->flags &= ~IEEE80211_SDATA_OPERATING_GMODE;
  555. ieee80211_set_wmm_default(sdata);
  556. }
  557. u32 ieee80211_mandatory_rates(struct ieee80211_local *local,
  558. enum ieee80211_band band)
  559. {
  560. struct ieee80211_supported_band *sband;
  561. struct ieee80211_rate *bitrates;
  562. u32 mandatory_rates;
  563. enum ieee80211_rate_flags mandatory_flag;
  564. int i;
  565. sband = local->hw.wiphy->bands[band];
  566. if (!sband) {
  567. WARN_ON(1);
  568. sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
  569. }
  570. if (band == IEEE80211_BAND_2GHZ)
  571. mandatory_flag = IEEE80211_RATE_MANDATORY_B;
  572. else
  573. mandatory_flag = IEEE80211_RATE_MANDATORY_A;
  574. bitrates = sband->bitrates;
  575. mandatory_rates = 0;
  576. for (i = 0; i < sband->n_bitrates; i++)
  577. if (bitrates[i].flags & mandatory_flag)
  578. mandatory_rates |= BIT(i);
  579. return mandatory_rates;
  580. }
  581. void ieee80211_send_auth(struct ieee80211_sub_if_data *sdata,
  582. u16 transaction, u16 auth_alg,
  583. u8 *extra, size_t extra_len, const u8 *bssid,
  584. const u8 *key, u8 key_len, u8 key_idx)
  585. {
  586. struct ieee80211_local *local = sdata->local;
  587. struct sk_buff *skb;
  588. struct ieee80211_mgmt *mgmt;
  589. int err;
  590. skb = dev_alloc_skb(local->hw.extra_tx_headroom +
  591. sizeof(*mgmt) + 6 + extra_len);
  592. if (!skb)
  593. return;
  594. skb_reserve(skb, local->hw.extra_tx_headroom);
  595. mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24 + 6);
  596. memset(mgmt, 0, 24 + 6);
  597. mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
  598. IEEE80211_STYPE_AUTH);
  599. memcpy(mgmt->da, bssid, ETH_ALEN);
  600. memcpy(mgmt->sa, sdata->vif.addr, ETH_ALEN);
  601. memcpy(mgmt->bssid, bssid, ETH_ALEN);
  602. mgmt->u.auth.auth_alg = cpu_to_le16(auth_alg);
  603. mgmt->u.auth.auth_transaction = cpu_to_le16(transaction);
  604. mgmt->u.auth.status_code = cpu_to_le16(0);
  605. if (extra)
  606. memcpy(skb_put(skb, extra_len), extra, extra_len);
  607. if (auth_alg == WLAN_AUTH_SHARED_KEY && transaction == 3) {
  608. mgmt->frame_control |= cpu_to_le16(IEEE80211_FCTL_PROTECTED);
  609. err = ieee80211_wep_encrypt(local, skb, key, key_len, key_idx);
  610. WARN_ON(err);
  611. }
  612. IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
  613. ieee80211_tx_skb(sdata, skb);
  614. }
  615. int ieee80211_build_preq_ies(struct ieee80211_local *local, u8 *buffer,
  616. const u8 *ie, size_t ie_len,
  617. enum ieee80211_band band, u32 rate_mask,
  618. u8 channel)
  619. {
  620. struct ieee80211_supported_band *sband;
  621. u8 *pos;
  622. size_t offset = 0, noffset;
  623. int supp_rates_len, i;
  624. u8 rates[32];
  625. int num_rates;
  626. int ext_rates_len;
  627. sband = local->hw.wiphy->bands[band];
  628. pos = buffer;
  629. num_rates = 0;
  630. for (i = 0; i < sband->n_bitrates; i++) {
  631. if ((BIT(i) & rate_mask) == 0)
  632. continue; /* skip rate */
  633. rates[num_rates++] = (u8) (sband->bitrates[i].bitrate / 5);
  634. }
  635. supp_rates_len = min_t(int, num_rates, 8);
  636. *pos++ = WLAN_EID_SUPP_RATES;
  637. *pos++ = supp_rates_len;
  638. memcpy(pos, rates, supp_rates_len);
  639. pos += supp_rates_len;
  640. /* insert "request information" if in custom IEs */
  641. if (ie && ie_len) {
  642. static const u8 before_extrates[] = {
  643. WLAN_EID_SSID,
  644. WLAN_EID_SUPP_RATES,
  645. WLAN_EID_REQUEST,
  646. };
  647. noffset = ieee80211_ie_split(ie, ie_len,
  648. before_extrates,
  649. ARRAY_SIZE(before_extrates),
  650. offset);
  651. memcpy(pos, ie + offset, noffset - offset);
  652. pos += noffset - offset;
  653. offset = noffset;
  654. }
  655. ext_rates_len = num_rates - supp_rates_len;
  656. if (ext_rates_len > 0) {
  657. *pos++ = WLAN_EID_EXT_SUPP_RATES;
  658. *pos++ = ext_rates_len;
  659. memcpy(pos, rates + supp_rates_len, ext_rates_len);
  660. pos += ext_rates_len;
  661. }
  662. if (channel && sband->band == IEEE80211_BAND_2GHZ) {
  663. *pos++ = WLAN_EID_DS_PARAMS;
  664. *pos++ = 1;
  665. *pos++ = channel;
  666. }
  667. /* insert custom IEs that go before HT */
  668. if (ie && ie_len) {
  669. static const u8 before_ht[] = {
  670. WLAN_EID_SSID,
  671. WLAN_EID_SUPP_RATES,
  672. WLAN_EID_REQUEST,
  673. WLAN_EID_EXT_SUPP_RATES,
  674. WLAN_EID_DS_PARAMS,
  675. WLAN_EID_SUPPORTED_REGULATORY_CLASSES,
  676. };
  677. noffset = ieee80211_ie_split(ie, ie_len,
  678. before_ht, ARRAY_SIZE(before_ht),
  679. offset);
  680. memcpy(pos, ie + offset, noffset - offset);
  681. pos += noffset - offset;
  682. offset = noffset;
  683. }
  684. if (sband->ht_cap.ht_supported) {
  685. u16 cap = sband->ht_cap.cap;
  686. __le16 tmp;
  687. *pos++ = WLAN_EID_HT_CAPABILITY;
  688. *pos++ = sizeof(struct ieee80211_ht_cap);
  689. memset(pos, 0, sizeof(struct ieee80211_ht_cap));
  690. tmp = cpu_to_le16(cap);
  691. memcpy(pos, &tmp, sizeof(u16));
  692. pos += sizeof(u16);
  693. *pos++ = sband->ht_cap.ampdu_factor |
  694. (sband->ht_cap.ampdu_density <<
  695. IEEE80211_HT_AMPDU_PARM_DENSITY_SHIFT);
  696. memcpy(pos, &sband->ht_cap.mcs, sizeof(sband->ht_cap.mcs));
  697. pos += sizeof(sband->ht_cap.mcs);
  698. pos += 2 + 4 + 1; /* ext info, BF cap, antsel */
  699. }
  700. /*
  701. * If adding more here, adjust code in main.c
  702. * that calculates local->scan_ies_len.
  703. */
  704. /* add any remaining custom IEs */
  705. if (ie && ie_len) {
  706. noffset = ie_len;
  707. memcpy(pos, ie + offset, noffset - offset);
  708. pos += noffset - offset;
  709. }
  710. return pos - buffer;
  711. }
  712. struct sk_buff *ieee80211_build_probe_req(struct ieee80211_sub_if_data *sdata,
  713. u8 *dst, u32 ratemask,
  714. const u8 *ssid, size_t ssid_len,
  715. const u8 *ie, size_t ie_len,
  716. bool directed)
  717. {
  718. struct ieee80211_local *local = sdata->local;
  719. struct sk_buff *skb;
  720. struct ieee80211_mgmt *mgmt;
  721. size_t buf_len;
  722. u8 *buf;
  723. u8 chan;
  724. /* FIXME: come up with a proper value */
  725. buf = kmalloc(200 + ie_len, GFP_KERNEL);
  726. if (!buf)
  727. return NULL;
  728. /*
  729. * Do not send DS Channel parameter for directed probe requests
  730. * in order to maximize the chance that we get a response. Some
  731. * badly-behaved APs don't respond when this parameter is included.
  732. */
  733. if (directed)
  734. chan = 0;
  735. else
  736. chan = ieee80211_frequency_to_channel(
  737. local->hw.conf.channel->center_freq);
  738. buf_len = ieee80211_build_preq_ies(local, buf, ie, ie_len,
  739. local->hw.conf.channel->band,
  740. ratemask, chan);
  741. skb = ieee80211_probereq_get(&local->hw, &sdata->vif,
  742. ssid, ssid_len,
  743. buf, buf_len);
  744. if (!skb)
  745. goto out;
  746. if (dst) {
  747. mgmt = (struct ieee80211_mgmt *) skb->data;
  748. memcpy(mgmt->da, dst, ETH_ALEN);
  749. memcpy(mgmt->bssid, dst, ETH_ALEN);
  750. }
  751. IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
  752. out:
  753. kfree(buf);
  754. return skb;
  755. }
  756. void ieee80211_send_probe_req(struct ieee80211_sub_if_data *sdata, u8 *dst,
  757. const u8 *ssid, size_t ssid_len,
  758. const u8 *ie, size_t ie_len,
  759. u32 ratemask, bool directed, bool no_cck)
  760. {
  761. struct sk_buff *skb;
  762. skb = ieee80211_build_probe_req(sdata, dst, ratemask, ssid, ssid_len,
  763. ie, ie_len, directed);
  764. if (skb) {
  765. if (no_cck)
  766. IEEE80211_SKB_CB(skb)->flags |=
  767. IEEE80211_TX_CTL_NO_CCK_RATE;
  768. ieee80211_tx_skb(sdata, skb);
  769. }
  770. }
  771. u32 ieee80211_sta_get_rates(struct ieee80211_local *local,
  772. struct ieee802_11_elems *elems,
  773. enum ieee80211_band band)
  774. {
  775. struct ieee80211_supported_band *sband;
  776. struct ieee80211_rate *bitrates;
  777. size_t num_rates;
  778. u32 supp_rates;
  779. int i, j;
  780. sband = local->hw.wiphy->bands[band];
  781. if (!sband) {
  782. WARN_ON(1);
  783. sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
  784. }
  785. bitrates = sband->bitrates;
  786. num_rates = sband->n_bitrates;
  787. supp_rates = 0;
  788. for (i = 0; i < elems->supp_rates_len +
  789. elems->ext_supp_rates_len; i++) {
  790. u8 rate = 0;
  791. int own_rate;
  792. if (i < elems->supp_rates_len)
  793. rate = elems->supp_rates[i];
  794. else if (elems->ext_supp_rates)
  795. rate = elems->ext_supp_rates
  796. [i - elems->supp_rates_len];
  797. own_rate = 5 * (rate & 0x7f);
  798. for (j = 0; j < num_rates; j++)
  799. if (bitrates[j].bitrate == own_rate)
  800. supp_rates |= BIT(j);
  801. }
  802. return supp_rates;
  803. }
  804. void ieee80211_stop_device(struct ieee80211_local *local)
  805. {
  806. ieee80211_led_radio(local, false);
  807. ieee80211_mod_tpt_led_trig(local, 0, IEEE80211_TPT_LEDTRIG_FL_RADIO);
  808. cancel_work_sync(&local->reconfig_filter);
  809. flush_workqueue(local->workqueue);
  810. drv_stop(local);
  811. }
  812. int ieee80211_reconfig(struct ieee80211_local *local)
  813. {
  814. struct ieee80211_hw *hw = &local->hw;
  815. struct ieee80211_sub_if_data *sdata;
  816. struct sta_info *sta;
  817. int res, i;
  818. #ifdef CONFIG_PM
  819. if (local->suspended)
  820. local->resuming = true;
  821. if (local->wowlan) {
  822. local->wowlan = false;
  823. res = drv_resume(local);
  824. if (res < 0) {
  825. local->resuming = false;
  826. return res;
  827. }
  828. if (res == 0)
  829. goto wake_up;
  830. WARN_ON(res > 1);
  831. /*
  832. * res is 1, which means the driver requested
  833. * to go through a regular reset on wakeup.
  834. */
  835. }
  836. #endif
  837. /* setup fragmentation threshold */
  838. drv_set_frag_threshold(local, hw->wiphy->frag_threshold);
  839. /* setup RTS threshold */
  840. drv_set_rts_threshold(local, hw->wiphy->rts_threshold);
  841. /* reset coverage class */
  842. drv_set_coverage_class(local, hw->wiphy->coverage_class);
  843. /* everything else happens only if HW was up & running */
  844. if (!local->open_count)
  845. goto wake_up;
  846. /*
  847. * Upon resume hardware can sometimes be goofy due to
  848. * various platform / driver / bus issues, so restarting
  849. * the device may at times not work immediately. Propagate
  850. * the error.
  851. */
  852. res = drv_start(local);
  853. if (res) {
  854. WARN(local->suspended, "Hardware became unavailable "
  855. "upon resume. This could be a software issue "
  856. "prior to suspend or a hardware issue.\n");
  857. return res;
  858. }
  859. ieee80211_led_radio(local, true);
  860. ieee80211_mod_tpt_led_trig(local,
  861. IEEE80211_TPT_LEDTRIG_FL_RADIO, 0);
  862. /* add interfaces */
  863. list_for_each_entry(sdata, &local->interfaces, list) {
  864. if (sdata->vif.type != NL80211_IFTYPE_AP_VLAN &&
  865. sdata->vif.type != NL80211_IFTYPE_MONITOR &&
  866. ieee80211_sdata_running(sdata))
  867. res = drv_add_interface(local, &sdata->vif);
  868. }
  869. /* add STAs back */
  870. mutex_lock(&local->sta_mtx);
  871. list_for_each_entry(sta, &local->sta_list, list) {
  872. if (sta->uploaded) {
  873. sdata = sta->sdata;
  874. if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
  875. sdata = container_of(sdata->bss,
  876. struct ieee80211_sub_if_data,
  877. u.ap);
  878. memset(&sta->sta.drv_priv, 0, hw->sta_data_size);
  879. WARN_ON(drv_sta_add(local, sdata, &sta->sta));
  880. }
  881. }
  882. mutex_unlock(&local->sta_mtx);
  883. /* reconfigure tx conf */
  884. list_for_each_entry(sdata, &local->interfaces, list) {
  885. if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN ||
  886. sdata->vif.type == NL80211_IFTYPE_MONITOR ||
  887. !ieee80211_sdata_running(sdata))
  888. continue;
  889. for (i = 0; i < hw->queues; i++)
  890. drv_conf_tx(local, sdata, i, &sdata->tx_conf[i]);
  891. }
  892. /* reconfigure hardware */
  893. ieee80211_hw_config(local, ~0);
  894. ieee80211_configure_filter(local);
  895. /* Finally also reconfigure all the BSS information */
  896. list_for_each_entry(sdata, &local->interfaces, list) {
  897. u32 changed;
  898. if (!ieee80211_sdata_running(sdata))
  899. continue;
  900. /* common change flags for all interface types */
  901. changed = BSS_CHANGED_ERP_CTS_PROT |
  902. BSS_CHANGED_ERP_PREAMBLE |
  903. BSS_CHANGED_ERP_SLOT |
  904. BSS_CHANGED_HT |
  905. BSS_CHANGED_BASIC_RATES |
  906. BSS_CHANGED_BEACON_INT |
  907. BSS_CHANGED_BSSID |
  908. BSS_CHANGED_CQM |
  909. BSS_CHANGED_QOS;
  910. switch (sdata->vif.type) {
  911. case NL80211_IFTYPE_STATION:
  912. changed |= BSS_CHANGED_ASSOC;
  913. mutex_lock(&sdata->u.mgd.mtx);
  914. ieee80211_bss_info_change_notify(sdata, changed);
  915. mutex_unlock(&sdata->u.mgd.mtx);
  916. break;
  917. case NL80211_IFTYPE_ADHOC:
  918. changed |= BSS_CHANGED_IBSS;
  919. /* fall through */
  920. case NL80211_IFTYPE_AP:
  921. changed |= BSS_CHANGED_SSID;
  922. /* fall through */
  923. case NL80211_IFTYPE_MESH_POINT:
  924. changed |= BSS_CHANGED_BEACON |
  925. BSS_CHANGED_BEACON_ENABLED;
  926. ieee80211_bss_info_change_notify(sdata, changed);
  927. break;
  928. case NL80211_IFTYPE_WDS:
  929. break;
  930. case NL80211_IFTYPE_AP_VLAN:
  931. case NL80211_IFTYPE_MONITOR:
  932. /* ignore virtual */
  933. break;
  934. case NL80211_IFTYPE_UNSPECIFIED:
  935. case NUM_NL80211_IFTYPES:
  936. case NL80211_IFTYPE_P2P_CLIENT:
  937. case NL80211_IFTYPE_P2P_GO:
  938. WARN_ON(1);
  939. break;
  940. }
  941. }
  942. /*
  943. * Clear the WLAN_STA_BLOCK_BA flag so new aggregation
  944. * sessions can be established after a resume.
  945. *
  946. * Also tear down aggregation sessions since reconfiguring
  947. * them in a hardware restart scenario is not easily done
  948. * right now, and the hardware will have lost information
  949. * about the sessions, but we and the AP still think they
  950. * are active. This is really a workaround though.
  951. */
  952. if (hw->flags & IEEE80211_HW_AMPDU_AGGREGATION) {
  953. mutex_lock(&local->sta_mtx);
  954. list_for_each_entry(sta, &local->sta_list, list) {
  955. ieee80211_sta_tear_down_BA_sessions(sta, true);
  956. clear_sta_flag(sta, WLAN_STA_BLOCK_BA);
  957. }
  958. mutex_unlock(&local->sta_mtx);
  959. }
  960. /* add back keys */
  961. list_for_each_entry(sdata, &local->interfaces, list)
  962. if (ieee80211_sdata_running(sdata))
  963. ieee80211_enable_keys(sdata);
  964. wake_up:
  965. ieee80211_wake_queues_by_reason(hw,
  966. IEEE80211_QUEUE_STOP_REASON_SUSPEND);
  967. /*
  968. * If this is for hw restart things are still running.
  969. * We may want to change that later, however.
  970. */
  971. if (!local->suspended)
  972. return 0;
  973. #ifdef CONFIG_PM
  974. /* first set suspended false, then resuming */
  975. local->suspended = false;
  976. mb();
  977. local->resuming = false;
  978. list_for_each_entry(sdata, &local->interfaces, list) {
  979. switch(sdata->vif.type) {
  980. case NL80211_IFTYPE_STATION:
  981. ieee80211_sta_restart(sdata);
  982. break;
  983. case NL80211_IFTYPE_ADHOC:
  984. ieee80211_ibss_restart(sdata);
  985. break;
  986. case NL80211_IFTYPE_MESH_POINT:
  987. ieee80211_mesh_restart(sdata);
  988. break;
  989. default:
  990. break;
  991. }
  992. }
  993. mod_timer(&local->sta_cleanup, jiffies + 1);
  994. mutex_lock(&local->sta_mtx);
  995. list_for_each_entry(sta, &local->sta_list, list)
  996. mesh_plink_restart(sta);
  997. mutex_unlock(&local->sta_mtx);
  998. #else
  999. WARN_ON(1);
  1000. #endif
  1001. return 0;
  1002. }
  1003. void ieee80211_resume_disconnect(struct ieee80211_vif *vif)
  1004. {
  1005. struct ieee80211_sub_if_data *sdata;
  1006. struct ieee80211_local *local;
  1007. struct ieee80211_key *key;
  1008. if (WARN_ON(!vif))
  1009. return;
  1010. sdata = vif_to_sdata(vif);
  1011. local = sdata->local;
  1012. if (WARN_ON(!local->resuming))
  1013. return;
  1014. if (WARN_ON(vif->type != NL80211_IFTYPE_STATION))
  1015. return;
  1016. sdata->flags |= IEEE80211_SDATA_DISCONNECT_RESUME;
  1017. mutex_lock(&local->key_mtx);
  1018. list_for_each_entry(key, &sdata->key_list, list)
  1019. key->flags |= KEY_FLAG_TAINTED;
  1020. mutex_unlock(&local->key_mtx);
  1021. }
  1022. EXPORT_SYMBOL_GPL(ieee80211_resume_disconnect);
  1023. static int check_mgd_smps(struct ieee80211_if_managed *ifmgd,
  1024. enum ieee80211_smps_mode *smps_mode)
  1025. {
  1026. if (ifmgd->associated) {
  1027. *smps_mode = ifmgd->ap_smps;
  1028. if (*smps_mode == IEEE80211_SMPS_AUTOMATIC) {
  1029. if (ifmgd->powersave)
  1030. *smps_mode = IEEE80211_SMPS_DYNAMIC;
  1031. else
  1032. *smps_mode = IEEE80211_SMPS_OFF;
  1033. }
  1034. return 1;
  1035. }
  1036. return 0;
  1037. }
  1038. /* must hold iflist_mtx */
  1039. void ieee80211_recalc_smps(struct ieee80211_local *local)
  1040. {
  1041. struct ieee80211_sub_if_data *sdata;
  1042. enum ieee80211_smps_mode smps_mode = IEEE80211_SMPS_OFF;
  1043. int count = 0;
  1044. lockdep_assert_held(&local->iflist_mtx);
  1045. /*
  1046. * This function could be improved to handle multiple
  1047. * interfaces better, but right now it makes any
  1048. * non-station interfaces force SM PS to be turned
  1049. * off. If there are multiple station interfaces it
  1050. * could also use the best possible mode, e.g. if
  1051. * one is in static and the other in dynamic then
  1052. * dynamic is ok.
  1053. */
  1054. list_for_each_entry(sdata, &local->interfaces, list) {
  1055. if (!ieee80211_sdata_running(sdata))
  1056. continue;
  1057. if (sdata->vif.type != NL80211_IFTYPE_STATION)
  1058. goto set;
  1059. count += check_mgd_smps(&sdata->u.mgd, &smps_mode);
  1060. if (count > 1) {
  1061. smps_mode = IEEE80211_SMPS_OFF;
  1062. break;
  1063. }
  1064. }
  1065. if (smps_mode == local->smps_mode)
  1066. return;
  1067. set:
  1068. local->smps_mode = smps_mode;
  1069. /* changed flag is auto-detected for this */
  1070. ieee80211_hw_config(local, 0);
  1071. }
  1072. static bool ieee80211_id_in_list(const u8 *ids, int n_ids, u8 id)
  1073. {
  1074. int i;
  1075. for (i = 0; i < n_ids; i++)
  1076. if (ids[i] == id)
  1077. return true;
  1078. return false;
  1079. }
  1080. /**
  1081. * ieee80211_ie_split - split an IE buffer according to ordering
  1082. *
  1083. * @ies: the IE buffer
  1084. * @ielen: the length of the IE buffer
  1085. * @ids: an array with element IDs that are allowed before
  1086. * the split
  1087. * @n_ids: the size of the element ID array
  1088. * @offset: offset where to start splitting in the buffer
  1089. *
  1090. * This function splits an IE buffer by updating the @offset
  1091. * variable to point to the location where the buffer should be
  1092. * split.
  1093. *
  1094. * It assumes that the given IE buffer is well-formed, this
  1095. * has to be guaranteed by the caller!
  1096. *
  1097. * It also assumes that the IEs in the buffer are ordered
  1098. * correctly, if not the result of using this function will not
  1099. * be ordered correctly either, i.e. it does no reordering.
  1100. *
  1101. * The function returns the offset where the next part of the
  1102. * buffer starts, which may be @ielen if the entire (remainder)
  1103. * of the buffer should be used.
  1104. */
  1105. size_t ieee80211_ie_split(const u8 *ies, size_t ielen,
  1106. const u8 *ids, int n_ids, size_t offset)
  1107. {
  1108. size_t pos = offset;
  1109. while (pos < ielen && ieee80211_id_in_list(ids, n_ids, ies[pos]))
  1110. pos += 2 + ies[pos + 1];
  1111. return pos;
  1112. }
  1113. size_t ieee80211_ie_split_vendor(const u8 *ies, size_t ielen, size_t offset)
  1114. {
  1115. size_t pos = offset;
  1116. while (pos < ielen && ies[pos] != WLAN_EID_VENDOR_SPECIFIC)
  1117. pos += 2 + ies[pos + 1];
  1118. return pos;
  1119. }
  1120. static void _ieee80211_enable_rssi_reports(struct ieee80211_sub_if_data *sdata,
  1121. int rssi_min_thold,
  1122. int rssi_max_thold)
  1123. {
  1124. trace_api_enable_rssi_reports(sdata, rssi_min_thold, rssi_max_thold);
  1125. if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_STATION))
  1126. return;
  1127. /*
  1128. * Scale up threshold values before storing it, as the RSSI averaging
  1129. * algorithm uses a scaled up value as well. Change this scaling
  1130. * factor if the RSSI averaging algorithm changes.
  1131. */
  1132. sdata->u.mgd.rssi_min_thold = rssi_min_thold*16;
  1133. sdata->u.mgd.rssi_max_thold = rssi_max_thold*16;
  1134. }
  1135. void ieee80211_enable_rssi_reports(struct ieee80211_vif *vif,
  1136. int rssi_min_thold,
  1137. int rssi_max_thold)
  1138. {
  1139. struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
  1140. WARN_ON(rssi_min_thold == rssi_max_thold ||
  1141. rssi_min_thold > rssi_max_thold);
  1142. _ieee80211_enable_rssi_reports(sdata, rssi_min_thold,
  1143. rssi_max_thold);
  1144. }
  1145. EXPORT_SYMBOL(ieee80211_enable_rssi_reports);
  1146. void ieee80211_disable_rssi_reports(struct ieee80211_vif *vif)
  1147. {
  1148. struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
  1149. _ieee80211_enable_rssi_reports(sdata, 0, 0);
  1150. }
  1151. EXPORT_SYMBOL(ieee80211_disable_rssi_reports);
  1152. int ieee80211_add_srates_ie(struct ieee80211_vif *vif, struct sk_buff *skb)
  1153. {
  1154. struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
  1155. struct ieee80211_local *local = sdata->local;
  1156. struct ieee80211_supported_band *sband;
  1157. int rate;
  1158. u8 i, rates, *pos;
  1159. sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
  1160. rates = sband->n_bitrates;
  1161. if (rates > 8)
  1162. rates = 8;
  1163. if (skb_tailroom(skb) < rates + 2)
  1164. return -ENOMEM;
  1165. pos = skb_put(skb, rates + 2);
  1166. *pos++ = WLAN_EID_SUPP_RATES;
  1167. *pos++ = rates;
  1168. for (i = 0; i < rates; i++) {
  1169. rate = sband->bitrates[i].bitrate;
  1170. *pos++ = (u8) (rate / 5);
  1171. }
  1172. return 0;
  1173. }
  1174. int ieee80211_add_ext_srates_ie(struct ieee80211_vif *vif, struct sk_buff *skb)
  1175. {
  1176. struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
  1177. struct ieee80211_local *local = sdata->local;
  1178. struct ieee80211_supported_band *sband;
  1179. int rate;
  1180. u8 i, exrates, *pos;
  1181. sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
  1182. exrates = sband->n_bitrates;
  1183. if (exrates > 8)
  1184. exrates -= 8;
  1185. else
  1186. exrates = 0;
  1187. if (skb_tailroom(skb) < exrates + 2)
  1188. return -ENOMEM;
  1189. if (exrates) {
  1190. pos = skb_put(skb, exrates + 2);
  1191. *pos++ = WLAN_EID_EXT_SUPP_RATES;
  1192. *pos++ = exrates;
  1193. for (i = 8; i < sband->n_bitrates; i++) {
  1194. rate = sband->bitrates[i].bitrate;
  1195. *pos++ = (u8) (rate / 5);
  1196. }
  1197. }
  1198. return 0;
  1199. }