util.c 34 KB

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