util.c 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366
  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 <linux/crc32.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. 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. tasklet_schedule(&local->tx_pending_tasklet);
  241. rcu_read_lock();
  242. list_for_each_entry_rcu(sdata, &local->interfaces, list)
  243. netif_tx_wake_queue(netdev_get_tx_queue(sdata->dev, queue));
  244. rcu_read_unlock();
  245. }
  246. void ieee80211_wake_queue_by_reason(struct ieee80211_hw *hw, int queue,
  247. enum queue_stop_reason reason)
  248. {
  249. struct ieee80211_local *local = hw_to_local(hw);
  250. unsigned long flags;
  251. spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
  252. __ieee80211_wake_queue(hw, queue, reason);
  253. spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
  254. }
  255. void ieee80211_wake_queue(struct ieee80211_hw *hw, int queue)
  256. {
  257. ieee80211_wake_queue_by_reason(hw, queue,
  258. IEEE80211_QUEUE_STOP_REASON_DRIVER);
  259. }
  260. EXPORT_SYMBOL(ieee80211_wake_queue);
  261. static void __ieee80211_stop_queue(struct ieee80211_hw *hw, int queue,
  262. enum queue_stop_reason reason)
  263. {
  264. struct ieee80211_local *local = hw_to_local(hw);
  265. struct ieee80211_sub_if_data *sdata;
  266. if (WARN_ON(queue >= hw->queues))
  267. return;
  268. __set_bit(reason, &local->queue_stop_reasons[queue]);
  269. rcu_read_lock();
  270. list_for_each_entry_rcu(sdata, &local->interfaces, list)
  271. netif_tx_stop_queue(netdev_get_tx_queue(sdata->dev, queue));
  272. rcu_read_unlock();
  273. }
  274. void ieee80211_stop_queue_by_reason(struct ieee80211_hw *hw, int queue,
  275. enum queue_stop_reason reason)
  276. {
  277. struct ieee80211_local *local = hw_to_local(hw);
  278. unsigned long flags;
  279. spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
  280. __ieee80211_stop_queue(hw, queue, reason);
  281. spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
  282. }
  283. void ieee80211_stop_queue(struct ieee80211_hw *hw, int queue)
  284. {
  285. ieee80211_stop_queue_by_reason(hw, queue,
  286. IEEE80211_QUEUE_STOP_REASON_DRIVER);
  287. }
  288. EXPORT_SYMBOL(ieee80211_stop_queue);
  289. void ieee80211_add_pending_skb(struct ieee80211_local *local,
  290. struct sk_buff *skb)
  291. {
  292. struct ieee80211_hw *hw = &local->hw;
  293. unsigned long flags;
  294. int queue = skb_get_queue_mapping(skb);
  295. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  296. if (WARN_ON(!info->control.vif)) {
  297. kfree_skb(skb);
  298. return;
  299. }
  300. spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
  301. __ieee80211_stop_queue(hw, queue, IEEE80211_QUEUE_STOP_REASON_SKB_ADD);
  302. __skb_queue_tail(&local->pending[queue], skb);
  303. __ieee80211_wake_queue(hw, queue, IEEE80211_QUEUE_STOP_REASON_SKB_ADD);
  304. spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
  305. }
  306. int ieee80211_add_pending_skbs(struct ieee80211_local *local,
  307. struct sk_buff_head *skbs)
  308. {
  309. struct ieee80211_hw *hw = &local->hw;
  310. struct sk_buff *skb;
  311. unsigned long flags;
  312. int queue, ret = 0, i;
  313. spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
  314. for (i = 0; i < hw->queues; i++)
  315. __ieee80211_stop_queue(hw, i,
  316. IEEE80211_QUEUE_STOP_REASON_SKB_ADD);
  317. while ((skb = skb_dequeue(skbs))) {
  318. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  319. if (WARN_ON(!info->control.vif)) {
  320. kfree_skb(skb);
  321. continue;
  322. }
  323. ret++;
  324. queue = skb_get_queue_mapping(skb);
  325. __skb_queue_tail(&local->pending[queue], skb);
  326. }
  327. for (i = 0; i < hw->queues; i++)
  328. __ieee80211_wake_queue(hw, i,
  329. IEEE80211_QUEUE_STOP_REASON_SKB_ADD);
  330. spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
  331. return ret;
  332. }
  333. void ieee80211_stop_queues_by_reason(struct ieee80211_hw *hw,
  334. enum queue_stop_reason reason)
  335. {
  336. struct ieee80211_local *local = hw_to_local(hw);
  337. unsigned long flags;
  338. int i;
  339. spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
  340. for (i = 0; i < hw->queues; i++)
  341. __ieee80211_stop_queue(hw, i, reason);
  342. spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
  343. }
  344. void ieee80211_stop_queues(struct ieee80211_hw *hw)
  345. {
  346. ieee80211_stop_queues_by_reason(hw,
  347. IEEE80211_QUEUE_STOP_REASON_DRIVER);
  348. }
  349. EXPORT_SYMBOL(ieee80211_stop_queues);
  350. int ieee80211_queue_stopped(struct ieee80211_hw *hw, int queue)
  351. {
  352. struct ieee80211_local *local = hw_to_local(hw);
  353. unsigned long flags;
  354. int ret;
  355. if (WARN_ON(queue >= hw->queues))
  356. return true;
  357. spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
  358. ret = !!local->queue_stop_reasons[queue];
  359. spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
  360. return ret;
  361. }
  362. EXPORT_SYMBOL(ieee80211_queue_stopped);
  363. void ieee80211_wake_queues_by_reason(struct ieee80211_hw *hw,
  364. enum queue_stop_reason reason)
  365. {
  366. struct ieee80211_local *local = hw_to_local(hw);
  367. unsigned long flags;
  368. int i;
  369. spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
  370. for (i = 0; i < hw->queues; i++)
  371. __ieee80211_wake_queue(hw, i, reason);
  372. spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
  373. }
  374. void ieee80211_wake_queues(struct ieee80211_hw *hw)
  375. {
  376. ieee80211_wake_queues_by_reason(hw, IEEE80211_QUEUE_STOP_REASON_DRIVER);
  377. }
  378. EXPORT_SYMBOL(ieee80211_wake_queues);
  379. void ieee80211_iterate_active_interfaces(
  380. struct ieee80211_hw *hw,
  381. void (*iterator)(void *data, u8 *mac,
  382. struct ieee80211_vif *vif),
  383. void *data)
  384. {
  385. struct ieee80211_local *local = hw_to_local(hw);
  386. struct ieee80211_sub_if_data *sdata;
  387. mutex_lock(&local->iflist_mtx);
  388. list_for_each_entry(sdata, &local->interfaces, list) {
  389. switch (sdata->vif.type) {
  390. case __NL80211_IFTYPE_AFTER_LAST:
  391. case NL80211_IFTYPE_UNSPECIFIED:
  392. case NL80211_IFTYPE_MONITOR:
  393. case NL80211_IFTYPE_AP_VLAN:
  394. continue;
  395. case NL80211_IFTYPE_AP:
  396. case NL80211_IFTYPE_STATION:
  397. case NL80211_IFTYPE_ADHOC:
  398. case NL80211_IFTYPE_WDS:
  399. case NL80211_IFTYPE_MESH_POINT:
  400. break;
  401. }
  402. if (ieee80211_sdata_running(sdata))
  403. iterator(data, sdata->vif.addr,
  404. &sdata->vif);
  405. }
  406. mutex_unlock(&local->iflist_mtx);
  407. }
  408. EXPORT_SYMBOL_GPL(ieee80211_iterate_active_interfaces);
  409. void ieee80211_iterate_active_interfaces_atomic(
  410. struct ieee80211_hw *hw,
  411. void (*iterator)(void *data, u8 *mac,
  412. struct ieee80211_vif *vif),
  413. void *data)
  414. {
  415. struct ieee80211_local *local = hw_to_local(hw);
  416. struct ieee80211_sub_if_data *sdata;
  417. rcu_read_lock();
  418. list_for_each_entry_rcu(sdata, &local->interfaces, list) {
  419. switch (sdata->vif.type) {
  420. case __NL80211_IFTYPE_AFTER_LAST:
  421. case NL80211_IFTYPE_UNSPECIFIED:
  422. case NL80211_IFTYPE_MONITOR:
  423. case NL80211_IFTYPE_AP_VLAN:
  424. continue;
  425. case NL80211_IFTYPE_AP:
  426. case NL80211_IFTYPE_STATION:
  427. case NL80211_IFTYPE_ADHOC:
  428. case NL80211_IFTYPE_WDS:
  429. case NL80211_IFTYPE_MESH_POINT:
  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. u32 ieee802_11_parse_elems_crc(u8 *start, size_t len,
  477. struct ieee802_11_elems *elems,
  478. u64 filter, u32 crc)
  479. {
  480. size_t left = len;
  481. u8 *pos = start;
  482. bool calc_crc = filter != 0;
  483. memset(elems, 0, sizeof(*elems));
  484. elems->ie_start = start;
  485. elems->total_len = len;
  486. while (left >= 2) {
  487. u8 id, elen;
  488. id = *pos++;
  489. elen = *pos++;
  490. left -= 2;
  491. if (elen > left)
  492. break;
  493. if (calc_crc && id < 64 && (filter & (1ULL << id)))
  494. crc = crc32_be(crc, pos - 2, elen + 2);
  495. switch (id) {
  496. case WLAN_EID_SSID:
  497. elems->ssid = pos;
  498. elems->ssid_len = elen;
  499. break;
  500. case WLAN_EID_SUPP_RATES:
  501. elems->supp_rates = pos;
  502. elems->supp_rates_len = elen;
  503. break;
  504. case WLAN_EID_FH_PARAMS:
  505. elems->fh_params = pos;
  506. elems->fh_params_len = elen;
  507. break;
  508. case WLAN_EID_DS_PARAMS:
  509. elems->ds_params = pos;
  510. elems->ds_params_len = elen;
  511. break;
  512. case WLAN_EID_CF_PARAMS:
  513. elems->cf_params = pos;
  514. elems->cf_params_len = elen;
  515. break;
  516. case WLAN_EID_TIM:
  517. if (elen >= sizeof(struct ieee80211_tim_ie)) {
  518. elems->tim = (void *)pos;
  519. elems->tim_len = elen;
  520. }
  521. break;
  522. case WLAN_EID_IBSS_PARAMS:
  523. elems->ibss_params = pos;
  524. elems->ibss_params_len = elen;
  525. break;
  526. case WLAN_EID_CHALLENGE:
  527. elems->challenge = pos;
  528. elems->challenge_len = elen;
  529. break;
  530. case WLAN_EID_VENDOR_SPECIFIC:
  531. if (elen >= 4 && pos[0] == 0x00 && pos[1] == 0x50 &&
  532. pos[2] == 0xf2) {
  533. /* Microsoft OUI (00:50:F2) */
  534. if (calc_crc)
  535. crc = crc32_be(crc, pos - 2, elen + 2);
  536. if (pos[3] == 1) {
  537. /* OUI Type 1 - WPA IE */
  538. elems->wpa = pos;
  539. elems->wpa_len = elen;
  540. } else if (elen >= 5 && pos[3] == 2) {
  541. /* OUI Type 2 - WMM IE */
  542. if (pos[4] == 0) {
  543. elems->wmm_info = pos;
  544. elems->wmm_info_len = elen;
  545. } else if (pos[4] == 1) {
  546. elems->wmm_param = pos;
  547. elems->wmm_param_len = elen;
  548. }
  549. }
  550. }
  551. break;
  552. case WLAN_EID_RSN:
  553. elems->rsn = pos;
  554. elems->rsn_len = elen;
  555. break;
  556. case WLAN_EID_ERP_INFO:
  557. elems->erp_info = pos;
  558. elems->erp_info_len = elen;
  559. break;
  560. case WLAN_EID_EXT_SUPP_RATES:
  561. elems->ext_supp_rates = pos;
  562. elems->ext_supp_rates_len = elen;
  563. break;
  564. case WLAN_EID_HT_CAPABILITY:
  565. if (elen >= sizeof(struct ieee80211_ht_cap))
  566. elems->ht_cap_elem = (void *)pos;
  567. break;
  568. case WLAN_EID_HT_INFORMATION:
  569. if (elen >= sizeof(struct ieee80211_ht_info))
  570. elems->ht_info_elem = (void *)pos;
  571. break;
  572. case WLAN_EID_MESH_ID:
  573. elems->mesh_id = pos;
  574. elems->mesh_id_len = elen;
  575. break;
  576. case WLAN_EID_MESH_CONFIG:
  577. if (elen >= sizeof(struct ieee80211_meshconf_ie))
  578. elems->mesh_config = (void *)pos;
  579. break;
  580. case WLAN_EID_PEER_LINK:
  581. elems->peer_link = pos;
  582. elems->peer_link_len = elen;
  583. break;
  584. case WLAN_EID_PREQ:
  585. elems->preq = pos;
  586. elems->preq_len = elen;
  587. break;
  588. case WLAN_EID_PREP:
  589. elems->prep = pos;
  590. elems->prep_len = elen;
  591. break;
  592. case WLAN_EID_PERR:
  593. elems->perr = pos;
  594. elems->perr_len = elen;
  595. break;
  596. case WLAN_EID_RANN:
  597. if (elen >= sizeof(struct ieee80211_rann_ie))
  598. elems->rann = (void *)pos;
  599. break;
  600. case WLAN_EID_CHANNEL_SWITCH:
  601. elems->ch_switch_elem = pos;
  602. elems->ch_switch_elem_len = elen;
  603. break;
  604. case WLAN_EID_QUIET:
  605. if (!elems->quiet_elem) {
  606. elems->quiet_elem = pos;
  607. elems->quiet_elem_len = elen;
  608. }
  609. elems->num_of_quiet_elem++;
  610. break;
  611. case WLAN_EID_COUNTRY:
  612. elems->country_elem = pos;
  613. elems->country_elem_len = elen;
  614. break;
  615. case WLAN_EID_PWR_CONSTRAINT:
  616. elems->pwr_constr_elem = pos;
  617. elems->pwr_constr_elem_len = elen;
  618. break;
  619. case WLAN_EID_TIMEOUT_INTERVAL:
  620. elems->timeout_int = pos;
  621. elems->timeout_int_len = elen;
  622. break;
  623. default:
  624. break;
  625. }
  626. left -= elen;
  627. pos += elen;
  628. }
  629. return crc;
  630. }
  631. void ieee80211_set_wmm_default(struct ieee80211_sub_if_data *sdata)
  632. {
  633. struct ieee80211_local *local = sdata->local;
  634. struct ieee80211_tx_queue_params qparam;
  635. int queue;
  636. bool use_11b;
  637. int aCWmin, aCWmax;
  638. if (!local->ops->conf_tx)
  639. return;
  640. memset(&qparam, 0, sizeof(qparam));
  641. use_11b = (local->hw.conf.channel->band == IEEE80211_BAND_2GHZ) &&
  642. !(sdata->flags & IEEE80211_SDATA_OPERATING_GMODE);
  643. for (queue = 0; queue < local_to_hw(local)->queues; queue++) {
  644. /* Set defaults according to 802.11-2007 Table 7-37 */
  645. aCWmax = 1023;
  646. if (use_11b)
  647. aCWmin = 31;
  648. else
  649. aCWmin = 15;
  650. switch (queue) {
  651. case 3: /* AC_BK */
  652. qparam.cw_max = aCWmax;
  653. qparam.cw_min = aCWmin;
  654. qparam.txop = 0;
  655. qparam.aifs = 7;
  656. break;
  657. default: /* never happens but let's not leave undefined */
  658. case 2: /* AC_BE */
  659. qparam.cw_max = aCWmax;
  660. qparam.cw_min = aCWmin;
  661. qparam.txop = 0;
  662. qparam.aifs = 3;
  663. break;
  664. case 1: /* AC_VI */
  665. qparam.cw_max = aCWmin;
  666. qparam.cw_min = (aCWmin + 1) / 2 - 1;
  667. if (use_11b)
  668. qparam.txop = 6016/32;
  669. else
  670. qparam.txop = 3008/32;
  671. qparam.aifs = 2;
  672. break;
  673. case 0: /* AC_VO */
  674. qparam.cw_max = (aCWmin + 1) / 2 - 1;
  675. qparam.cw_min = (aCWmin + 1) / 4 - 1;
  676. if (use_11b)
  677. qparam.txop = 3264/32;
  678. else
  679. qparam.txop = 1504/32;
  680. qparam.aifs = 2;
  681. break;
  682. }
  683. drv_conf_tx(local, queue, &qparam);
  684. }
  685. }
  686. void ieee80211_sta_def_wmm_params(struct ieee80211_sub_if_data *sdata,
  687. const size_t supp_rates_len,
  688. const u8 *supp_rates)
  689. {
  690. struct ieee80211_local *local = sdata->local;
  691. int i, have_higher_than_11mbit = 0;
  692. /* cf. IEEE 802.11 9.2.12 */
  693. for (i = 0; i < supp_rates_len; i++)
  694. if ((supp_rates[i] & 0x7f) * 5 > 110)
  695. have_higher_than_11mbit = 1;
  696. if (local->hw.conf.channel->band == IEEE80211_BAND_2GHZ &&
  697. have_higher_than_11mbit)
  698. sdata->flags |= IEEE80211_SDATA_OPERATING_GMODE;
  699. else
  700. sdata->flags &= ~IEEE80211_SDATA_OPERATING_GMODE;
  701. ieee80211_set_wmm_default(sdata);
  702. }
  703. u32 ieee80211_mandatory_rates(struct ieee80211_local *local,
  704. enum ieee80211_band band)
  705. {
  706. struct ieee80211_supported_band *sband;
  707. struct ieee80211_rate *bitrates;
  708. u32 mandatory_rates;
  709. enum ieee80211_rate_flags mandatory_flag;
  710. int i;
  711. sband = local->hw.wiphy->bands[band];
  712. if (!sband) {
  713. WARN_ON(1);
  714. sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
  715. }
  716. if (band == IEEE80211_BAND_2GHZ)
  717. mandatory_flag = IEEE80211_RATE_MANDATORY_B;
  718. else
  719. mandatory_flag = IEEE80211_RATE_MANDATORY_A;
  720. bitrates = sband->bitrates;
  721. mandatory_rates = 0;
  722. for (i = 0; i < sband->n_bitrates; i++)
  723. if (bitrates[i].flags & mandatory_flag)
  724. mandatory_rates |= BIT(i);
  725. return mandatory_rates;
  726. }
  727. void ieee80211_send_auth(struct ieee80211_sub_if_data *sdata,
  728. u16 transaction, u16 auth_alg,
  729. u8 *extra, size_t extra_len, const u8 *bssid,
  730. const u8 *key, u8 key_len, u8 key_idx)
  731. {
  732. struct ieee80211_local *local = sdata->local;
  733. struct sk_buff *skb;
  734. struct ieee80211_mgmt *mgmt;
  735. int err;
  736. skb = dev_alloc_skb(local->hw.extra_tx_headroom +
  737. sizeof(*mgmt) + 6 + extra_len);
  738. if (!skb) {
  739. printk(KERN_DEBUG "%s: failed to allocate buffer for auth "
  740. "frame\n", sdata->name);
  741. return;
  742. }
  743. skb_reserve(skb, local->hw.extra_tx_headroom);
  744. mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24 + 6);
  745. memset(mgmt, 0, 24 + 6);
  746. mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
  747. IEEE80211_STYPE_AUTH);
  748. memcpy(mgmt->da, bssid, ETH_ALEN);
  749. memcpy(mgmt->sa, sdata->vif.addr, ETH_ALEN);
  750. memcpy(mgmt->bssid, bssid, ETH_ALEN);
  751. mgmt->u.auth.auth_alg = cpu_to_le16(auth_alg);
  752. mgmt->u.auth.auth_transaction = cpu_to_le16(transaction);
  753. mgmt->u.auth.status_code = cpu_to_le16(0);
  754. if (extra)
  755. memcpy(skb_put(skb, extra_len), extra, extra_len);
  756. if (auth_alg == WLAN_AUTH_SHARED_KEY && transaction == 3) {
  757. mgmt->frame_control |= cpu_to_le16(IEEE80211_FCTL_PROTECTED);
  758. err = ieee80211_wep_encrypt(local, skb, key, key_len, key_idx);
  759. WARN_ON(err);
  760. }
  761. IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
  762. ieee80211_tx_skb(sdata, skb);
  763. }
  764. int ieee80211_build_preq_ies(struct ieee80211_local *local, u8 *buffer,
  765. const u8 *ie, size_t ie_len,
  766. enum ieee80211_band band)
  767. {
  768. struct ieee80211_supported_band *sband;
  769. u8 *pos;
  770. size_t offset = 0, noffset;
  771. int supp_rates_len, i;
  772. sband = local->hw.wiphy->bands[band];
  773. pos = buffer;
  774. supp_rates_len = min_t(int, sband->n_bitrates, 8);
  775. *pos++ = WLAN_EID_SUPP_RATES;
  776. *pos++ = supp_rates_len;
  777. for (i = 0; i < supp_rates_len; i++) {
  778. int rate = sband->bitrates[i].bitrate;
  779. *pos++ = (u8) (rate / 5);
  780. }
  781. /* insert "request information" if in custom IEs */
  782. if (ie && ie_len) {
  783. static const u8 before_extrates[] = {
  784. WLAN_EID_SSID,
  785. WLAN_EID_SUPP_RATES,
  786. WLAN_EID_REQUEST,
  787. };
  788. noffset = ieee80211_ie_split(ie, ie_len,
  789. before_extrates,
  790. ARRAY_SIZE(before_extrates),
  791. offset);
  792. memcpy(pos, ie + offset, noffset - offset);
  793. pos += noffset - offset;
  794. offset = noffset;
  795. }
  796. if (sband->n_bitrates > i) {
  797. *pos++ = WLAN_EID_EXT_SUPP_RATES;
  798. *pos++ = sband->n_bitrates - i;
  799. for (; i < sband->n_bitrates; i++) {
  800. int rate = sband->bitrates[i].bitrate;
  801. *pos++ = (u8) (rate / 5);
  802. }
  803. }
  804. /* insert custom IEs that go before HT */
  805. if (ie && ie_len) {
  806. static const u8 before_ht[] = {
  807. WLAN_EID_SSID,
  808. WLAN_EID_SUPP_RATES,
  809. WLAN_EID_REQUEST,
  810. WLAN_EID_EXT_SUPP_RATES,
  811. WLAN_EID_DS_PARAMS,
  812. WLAN_EID_SUPPORTED_REGULATORY_CLASSES,
  813. };
  814. noffset = ieee80211_ie_split(ie, ie_len,
  815. before_ht, ARRAY_SIZE(before_ht),
  816. offset);
  817. memcpy(pos, ie + offset, noffset - offset);
  818. pos += noffset - offset;
  819. offset = noffset;
  820. }
  821. if (sband->ht_cap.ht_supported) {
  822. u16 cap = sband->ht_cap.cap;
  823. __le16 tmp;
  824. if (ieee80211_disable_40mhz_24ghz &&
  825. sband->band == IEEE80211_BAND_2GHZ) {
  826. cap &= ~IEEE80211_HT_CAP_SUP_WIDTH_20_40;
  827. cap &= ~IEEE80211_HT_CAP_SGI_40;
  828. }
  829. *pos++ = WLAN_EID_HT_CAPABILITY;
  830. *pos++ = sizeof(struct ieee80211_ht_cap);
  831. memset(pos, 0, sizeof(struct ieee80211_ht_cap));
  832. tmp = cpu_to_le16(cap);
  833. memcpy(pos, &tmp, sizeof(u16));
  834. pos += sizeof(u16);
  835. *pos++ = sband->ht_cap.ampdu_factor |
  836. (sband->ht_cap.ampdu_density <<
  837. IEEE80211_HT_AMPDU_PARM_DENSITY_SHIFT);
  838. memcpy(pos, &sband->ht_cap.mcs, sizeof(sband->ht_cap.mcs));
  839. pos += sizeof(sband->ht_cap.mcs);
  840. pos += 2 + 4 + 1; /* ext info, BF cap, antsel */
  841. }
  842. /*
  843. * If adding more here, adjust code in main.c
  844. * that calculates local->scan_ies_len.
  845. */
  846. /* add any remaining custom IEs */
  847. if (ie && ie_len) {
  848. noffset = ie_len;
  849. memcpy(pos, ie + offset, noffset - offset);
  850. pos += noffset - offset;
  851. }
  852. return pos - buffer;
  853. }
  854. void ieee80211_send_probe_req(struct ieee80211_sub_if_data *sdata, u8 *dst,
  855. const u8 *ssid, size_t ssid_len,
  856. const u8 *ie, size_t ie_len)
  857. {
  858. struct ieee80211_local *local = sdata->local;
  859. struct sk_buff *skb;
  860. struct ieee80211_mgmt *mgmt;
  861. u8 *pos;
  862. skb = dev_alloc_skb(local->hw.extra_tx_headroom + sizeof(*mgmt) + 200 +
  863. ie_len);
  864. if (!skb) {
  865. printk(KERN_DEBUG "%s: failed to allocate buffer for probe "
  866. "request\n", sdata->name);
  867. return;
  868. }
  869. skb_reserve(skb, local->hw.extra_tx_headroom);
  870. mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24);
  871. memset(mgmt, 0, 24);
  872. mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
  873. IEEE80211_STYPE_PROBE_REQ);
  874. memcpy(mgmt->sa, sdata->vif.addr, ETH_ALEN);
  875. if (dst) {
  876. memcpy(mgmt->da, dst, ETH_ALEN);
  877. memcpy(mgmt->bssid, dst, ETH_ALEN);
  878. } else {
  879. memset(mgmt->da, 0xff, ETH_ALEN);
  880. memset(mgmt->bssid, 0xff, ETH_ALEN);
  881. }
  882. pos = skb_put(skb, 2 + ssid_len);
  883. *pos++ = WLAN_EID_SSID;
  884. *pos++ = ssid_len;
  885. memcpy(pos, ssid, ssid_len);
  886. pos += ssid_len;
  887. skb_put(skb, ieee80211_build_preq_ies(local, pos, ie, ie_len,
  888. local->hw.conf.channel->band));
  889. IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
  890. ieee80211_tx_skb(sdata, skb);
  891. }
  892. u32 ieee80211_sta_get_rates(struct ieee80211_local *local,
  893. struct ieee802_11_elems *elems,
  894. enum ieee80211_band band)
  895. {
  896. struct ieee80211_supported_band *sband;
  897. struct ieee80211_rate *bitrates;
  898. size_t num_rates;
  899. u32 supp_rates;
  900. int i, j;
  901. sband = local->hw.wiphy->bands[band];
  902. if (!sband) {
  903. WARN_ON(1);
  904. sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
  905. }
  906. bitrates = sband->bitrates;
  907. num_rates = sband->n_bitrates;
  908. supp_rates = 0;
  909. for (i = 0; i < elems->supp_rates_len +
  910. elems->ext_supp_rates_len; i++) {
  911. u8 rate = 0;
  912. int own_rate;
  913. if (i < elems->supp_rates_len)
  914. rate = elems->supp_rates[i];
  915. else if (elems->ext_supp_rates)
  916. rate = elems->ext_supp_rates
  917. [i - elems->supp_rates_len];
  918. own_rate = 5 * (rate & 0x7f);
  919. for (j = 0; j < num_rates; j++)
  920. if (bitrates[j].bitrate == own_rate)
  921. supp_rates |= BIT(j);
  922. }
  923. return supp_rates;
  924. }
  925. void ieee80211_stop_device(struct ieee80211_local *local)
  926. {
  927. ieee80211_led_radio(local, false);
  928. cancel_work_sync(&local->reconfig_filter);
  929. drv_stop(local);
  930. flush_workqueue(local->workqueue);
  931. }
  932. int ieee80211_reconfig(struct ieee80211_local *local)
  933. {
  934. struct ieee80211_hw *hw = &local->hw;
  935. struct ieee80211_sub_if_data *sdata;
  936. struct sta_info *sta;
  937. unsigned long flags;
  938. int res;
  939. if (local->suspended)
  940. local->resuming = true;
  941. /* restart hardware */
  942. if (local->open_count) {
  943. /*
  944. * Upon resume hardware can sometimes be goofy due to
  945. * various platform / driver / bus issues, so restarting
  946. * the device may at times not work immediately. Propagate
  947. * the error.
  948. */
  949. res = drv_start(local);
  950. if (res) {
  951. WARN(local->suspended, "Harware became unavailable "
  952. "upon resume. This is could be a software issue"
  953. "prior to suspend or a hardware issue\n");
  954. return res;
  955. }
  956. ieee80211_led_radio(local, true);
  957. }
  958. /* add interfaces */
  959. list_for_each_entry(sdata, &local->interfaces, list) {
  960. if (sdata->vif.type != NL80211_IFTYPE_AP_VLAN &&
  961. sdata->vif.type != NL80211_IFTYPE_MONITOR &&
  962. ieee80211_sdata_running(sdata))
  963. res = drv_add_interface(local, &sdata->vif);
  964. }
  965. /* add STAs back */
  966. if (local->ops->sta_notify) {
  967. spin_lock_irqsave(&local->sta_lock, flags);
  968. list_for_each_entry(sta, &local->sta_list, list) {
  969. sdata = sta->sdata;
  970. if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
  971. sdata = container_of(sdata->bss,
  972. struct ieee80211_sub_if_data,
  973. u.ap);
  974. drv_sta_notify(local, sdata, STA_NOTIFY_ADD,
  975. &sta->sta);
  976. }
  977. spin_unlock_irqrestore(&local->sta_lock, flags);
  978. }
  979. /* Clear Suspend state so that ADDBA requests can be processed */
  980. rcu_read_lock();
  981. if (hw->flags & IEEE80211_HW_AMPDU_AGGREGATION) {
  982. list_for_each_entry_rcu(sta, &local->sta_list, list) {
  983. clear_sta_flags(sta, WLAN_STA_SUSPEND);
  984. }
  985. }
  986. rcu_read_unlock();
  987. /* setup RTS threshold */
  988. drv_set_rts_threshold(local, hw->wiphy->rts_threshold);
  989. /* reconfigure hardware */
  990. ieee80211_hw_config(local, ~0);
  991. ieee80211_configure_filter(local);
  992. /* Finally also reconfigure all the BSS information */
  993. list_for_each_entry(sdata, &local->interfaces, list) {
  994. u32 changed = ~0;
  995. if (!ieee80211_sdata_running(sdata))
  996. continue;
  997. switch (sdata->vif.type) {
  998. case NL80211_IFTYPE_STATION:
  999. /* disable beacon change bits */
  1000. changed &= ~(BSS_CHANGED_BEACON |
  1001. BSS_CHANGED_BEACON_ENABLED);
  1002. /* fall through */
  1003. case NL80211_IFTYPE_ADHOC:
  1004. case NL80211_IFTYPE_AP:
  1005. case NL80211_IFTYPE_MESH_POINT:
  1006. ieee80211_bss_info_change_notify(sdata, changed);
  1007. break;
  1008. case NL80211_IFTYPE_WDS:
  1009. break;
  1010. case NL80211_IFTYPE_AP_VLAN:
  1011. case NL80211_IFTYPE_MONITOR:
  1012. /* ignore virtual */
  1013. break;
  1014. case NL80211_IFTYPE_UNSPECIFIED:
  1015. case __NL80211_IFTYPE_AFTER_LAST:
  1016. WARN_ON(1);
  1017. break;
  1018. }
  1019. }
  1020. /* add back keys */
  1021. list_for_each_entry(sdata, &local->interfaces, list)
  1022. if (ieee80211_sdata_running(sdata))
  1023. ieee80211_enable_keys(sdata);
  1024. ieee80211_wake_queues_by_reason(hw,
  1025. IEEE80211_QUEUE_STOP_REASON_SUSPEND);
  1026. /*
  1027. * If this is for hw restart things are still running.
  1028. * We may want to change that later, however.
  1029. */
  1030. if (!local->suspended)
  1031. return 0;
  1032. #ifdef CONFIG_PM
  1033. /* first set suspended false, then resuming */
  1034. local->suspended = false;
  1035. mb();
  1036. local->resuming = false;
  1037. list_for_each_entry(sdata, &local->interfaces, list) {
  1038. switch(sdata->vif.type) {
  1039. case NL80211_IFTYPE_STATION:
  1040. ieee80211_sta_restart(sdata);
  1041. break;
  1042. case NL80211_IFTYPE_ADHOC:
  1043. ieee80211_ibss_restart(sdata);
  1044. break;
  1045. case NL80211_IFTYPE_MESH_POINT:
  1046. ieee80211_mesh_restart(sdata);
  1047. break;
  1048. default:
  1049. break;
  1050. }
  1051. }
  1052. add_timer(&local->sta_cleanup);
  1053. spin_lock_irqsave(&local->sta_lock, flags);
  1054. list_for_each_entry(sta, &local->sta_list, list)
  1055. mesh_plink_restart(sta);
  1056. spin_unlock_irqrestore(&local->sta_lock, flags);
  1057. #else
  1058. WARN_ON(1);
  1059. #endif
  1060. return 0;
  1061. }
  1062. static int check_mgd_smps(struct ieee80211_if_managed *ifmgd,
  1063. enum ieee80211_smps_mode *smps_mode)
  1064. {
  1065. if (ifmgd->associated) {
  1066. *smps_mode = ifmgd->ap_smps;
  1067. if (*smps_mode == IEEE80211_SMPS_AUTOMATIC) {
  1068. if (ifmgd->powersave)
  1069. *smps_mode = IEEE80211_SMPS_DYNAMIC;
  1070. else
  1071. *smps_mode = IEEE80211_SMPS_OFF;
  1072. }
  1073. return 1;
  1074. }
  1075. return 0;
  1076. }
  1077. /* must hold iflist_mtx */
  1078. void ieee80211_recalc_smps(struct ieee80211_local *local,
  1079. struct ieee80211_sub_if_data *forsdata)
  1080. {
  1081. struct ieee80211_sub_if_data *sdata;
  1082. enum ieee80211_smps_mode smps_mode = IEEE80211_SMPS_OFF;
  1083. int count = 0;
  1084. if (forsdata)
  1085. WARN_ON(!mutex_is_locked(&forsdata->u.mgd.mtx));
  1086. WARN_ON(!mutex_is_locked(&local->iflist_mtx));
  1087. /*
  1088. * This function could be improved to handle multiple
  1089. * interfaces better, but right now it makes any
  1090. * non-station interfaces force SM PS to be turned
  1091. * off. If there are multiple station interfaces it
  1092. * could also use the best possible mode, e.g. if
  1093. * one is in static and the other in dynamic then
  1094. * dynamic is ok.
  1095. */
  1096. list_for_each_entry(sdata, &local->interfaces, list) {
  1097. if (!netif_running(sdata->dev))
  1098. continue;
  1099. if (sdata->vif.type != NL80211_IFTYPE_STATION)
  1100. goto set;
  1101. if (sdata != forsdata) {
  1102. /*
  1103. * This nested is ok -- we are holding the iflist_mtx
  1104. * so can't get here twice or so. But it's required
  1105. * since normally we acquire it first and then the
  1106. * iflist_mtx.
  1107. */
  1108. mutex_lock_nested(&sdata->u.mgd.mtx, SINGLE_DEPTH_NESTING);
  1109. count += check_mgd_smps(&sdata->u.mgd, &smps_mode);
  1110. mutex_unlock(&sdata->u.mgd.mtx);
  1111. } else
  1112. count += check_mgd_smps(&sdata->u.mgd, &smps_mode);
  1113. if (count > 1) {
  1114. smps_mode = IEEE80211_SMPS_OFF;
  1115. break;
  1116. }
  1117. }
  1118. if (smps_mode == local->smps_mode)
  1119. return;
  1120. set:
  1121. local->smps_mode = smps_mode;
  1122. /* changed flag is auto-detected for this */
  1123. ieee80211_hw_config(local, 0);
  1124. }
  1125. static bool ieee80211_id_in_list(const u8 *ids, int n_ids, u8 id)
  1126. {
  1127. int i;
  1128. for (i = 0; i < n_ids; i++)
  1129. if (ids[i] == id)
  1130. return true;
  1131. return false;
  1132. }
  1133. /**
  1134. * ieee80211_ie_split - split an IE buffer according to ordering
  1135. *
  1136. * @ies: the IE buffer
  1137. * @ielen: the length of the IE buffer
  1138. * @ids: an array with element IDs that are allowed before
  1139. * the split
  1140. * @n_ids: the size of the element ID array
  1141. * @offset: offset where to start splitting in the buffer
  1142. *
  1143. * This function splits an IE buffer by updating the @offset
  1144. * variable to point to the location where the buffer should be
  1145. * split.
  1146. *
  1147. * It assumes that the given IE buffer is well-formed, this
  1148. * has to be guaranteed by the caller!
  1149. *
  1150. * It also assumes that the IEs in the buffer are ordered
  1151. * correctly, if not the result of using this function will not
  1152. * be ordered correctly either, i.e. it does no reordering.
  1153. *
  1154. * The function returns the offset where the next part of the
  1155. * buffer starts, which may be @ielen if the entire (remainder)
  1156. * of the buffer should be used.
  1157. */
  1158. size_t ieee80211_ie_split(const u8 *ies, size_t ielen,
  1159. const u8 *ids, int n_ids, size_t offset)
  1160. {
  1161. size_t pos = offset;
  1162. while (pos < ielen && ieee80211_id_in_list(ids, n_ids, ies[pos]))
  1163. pos += 2 + ies[pos + 1];
  1164. return pos;
  1165. }
  1166. size_t ieee80211_ie_split_vendor(const u8 *ies, size_t ielen, size_t offset)
  1167. {
  1168. size_t pos = offset;
  1169. while (pos < ielen && ies[pos] != WLAN_EID_VENDOR_SPECIFIC)
  1170. pos += 2 + ies[pos + 1];
  1171. return pos;
  1172. }