util.c 36 KB

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