util.c 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807
  1. /*
  2. * Copyright 2002-2005, Instant802 Networks, Inc.
  3. * Copyright 2005-2006, Devicescape Software, Inc.
  4. * Copyright 2006-2007 Jiri Benc <jbenc@suse.cz>
  5. * Copyright 2007 Johannes Berg <johannes@sipsolutions.net>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. *
  11. * utilities for mac80211
  12. */
  13. #include <net/mac80211.h>
  14. #include <linux/netdevice.h>
  15. #include <linux/export.h>
  16. #include <linux/types.h>
  17. #include <linux/slab.h>
  18. #include <linux/skbuff.h>
  19. #include <linux/etherdevice.h>
  20. #include <linux/if_arp.h>
  21. #include <linux/bitmap.h>
  22. #include <linux/crc32.h>
  23. #include <net/net_namespace.h>
  24. #include <net/cfg80211.h>
  25. #include <net/rtnetlink.h>
  26. #include "ieee80211_i.h"
  27. #include "driver-ops.h"
  28. #include "rate.h"
  29. #include "mesh.h"
  30. #include "wme.h"
  31. #include "led.h"
  32. #include "wep.h"
  33. /* privid for wiphys to determine whether they belong to us or not */
  34. void *mac80211_wiphy_privid = &mac80211_wiphy_privid;
  35. struct ieee80211_hw *wiphy_to_ieee80211_hw(struct wiphy *wiphy)
  36. {
  37. struct ieee80211_local *local;
  38. BUG_ON(!wiphy);
  39. local = wiphy_priv(wiphy);
  40. return &local->hw;
  41. }
  42. EXPORT_SYMBOL(wiphy_to_ieee80211_hw);
  43. u8 *ieee80211_get_bssid(struct ieee80211_hdr *hdr, size_t len,
  44. enum nl80211_iftype type)
  45. {
  46. __le16 fc = hdr->frame_control;
  47. /* drop ACK/CTS frames and incorrect hdr len (ctrl) */
  48. if (len < 16)
  49. return NULL;
  50. if (ieee80211_is_data(fc)) {
  51. if (len < 24) /* drop incorrect hdr len (data) */
  52. return NULL;
  53. if (ieee80211_has_a4(fc))
  54. return NULL;
  55. if (ieee80211_has_tods(fc))
  56. return hdr->addr1;
  57. if (ieee80211_has_fromds(fc))
  58. return hdr->addr2;
  59. return hdr->addr3;
  60. }
  61. if (ieee80211_is_mgmt(fc)) {
  62. if (len < 24) /* drop incorrect hdr len (mgmt) */
  63. return NULL;
  64. return hdr->addr3;
  65. }
  66. if (ieee80211_is_ctl(fc)) {
  67. if(ieee80211_is_pspoll(fc))
  68. return hdr->addr1;
  69. if (ieee80211_is_back_req(fc)) {
  70. switch (type) {
  71. case NL80211_IFTYPE_STATION:
  72. return hdr->addr2;
  73. case NL80211_IFTYPE_AP:
  74. case NL80211_IFTYPE_AP_VLAN:
  75. return hdr->addr1;
  76. default:
  77. break; /* fall through to the return */
  78. }
  79. }
  80. }
  81. return NULL;
  82. }
  83. void ieee80211_tx_set_protected(struct ieee80211_tx_data *tx)
  84. {
  85. struct sk_buff *skb;
  86. struct ieee80211_hdr *hdr;
  87. skb_queue_walk(&tx->skbs, skb) {
  88. hdr = (struct ieee80211_hdr *) skb->data;
  89. hdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_PROTECTED);
  90. }
  91. }
  92. int ieee80211_frame_duration(enum ieee80211_band band, size_t len,
  93. int rate, int erp, int short_preamble)
  94. {
  95. int dur;
  96. /* calculate duration (in microseconds, rounded up to next higher
  97. * integer if it includes a fractional microsecond) to send frame of
  98. * len bytes (does not include FCS) at the given rate. Duration will
  99. * also include SIFS.
  100. *
  101. * rate is in 100 kbps, so divident is multiplied by 10 in the
  102. * DIV_ROUND_UP() operations.
  103. */
  104. if (band == IEEE80211_BAND_5GHZ || erp) {
  105. /*
  106. * OFDM:
  107. *
  108. * N_DBPS = DATARATE x 4
  109. * N_SYM = Ceiling((16+8xLENGTH+6) / N_DBPS)
  110. * (16 = SIGNAL time, 6 = tail bits)
  111. * TXTIME = T_PREAMBLE + T_SIGNAL + T_SYM x N_SYM + Signal Ext
  112. *
  113. * T_SYM = 4 usec
  114. * 802.11a - 17.5.2: aSIFSTime = 16 usec
  115. * 802.11g - 19.8.4: aSIFSTime = 10 usec +
  116. * signal ext = 6 usec
  117. */
  118. dur = 16; /* SIFS + signal ext */
  119. dur += 16; /* 17.3.2.3: T_PREAMBLE = 16 usec */
  120. dur += 4; /* 17.3.2.3: T_SIGNAL = 4 usec */
  121. dur += 4 * DIV_ROUND_UP((16 + 8 * (len + 4) + 6) * 10,
  122. 4 * rate); /* T_SYM x N_SYM */
  123. } else {
  124. /*
  125. * 802.11b or 802.11g with 802.11b compatibility:
  126. * 18.3.4: TXTIME = PreambleLength + PLCPHeaderTime +
  127. * Ceiling(((LENGTH+PBCC)x8)/DATARATE). PBCC=0.
  128. *
  129. * 802.11 (DS): 15.3.3, 802.11b: 18.3.4
  130. * aSIFSTime = 10 usec
  131. * aPreambleLength = 144 usec or 72 usec with short preamble
  132. * aPLCPHeaderLength = 48 usec or 24 usec with short preamble
  133. */
  134. dur = 10; /* aSIFSTime = 10 usec */
  135. dur += short_preamble ? (72 + 24) : (144 + 48);
  136. dur += DIV_ROUND_UP(8 * (len + 4) * 10, rate);
  137. }
  138. return dur;
  139. }
  140. /* Exported duration function for driver use */
  141. __le16 ieee80211_generic_frame_duration(struct ieee80211_hw *hw,
  142. struct ieee80211_vif *vif,
  143. enum ieee80211_band band,
  144. size_t frame_len,
  145. struct ieee80211_rate *rate)
  146. {
  147. struct ieee80211_sub_if_data *sdata;
  148. u16 dur;
  149. int erp;
  150. bool short_preamble = false;
  151. erp = 0;
  152. if (vif) {
  153. sdata = vif_to_sdata(vif);
  154. short_preamble = sdata->vif.bss_conf.use_short_preamble;
  155. if (sdata->flags & IEEE80211_SDATA_OPERATING_GMODE)
  156. erp = rate->flags & IEEE80211_RATE_ERP_G;
  157. }
  158. dur = ieee80211_frame_duration(band, frame_len, rate->bitrate, erp,
  159. short_preamble);
  160. return cpu_to_le16(dur);
  161. }
  162. EXPORT_SYMBOL(ieee80211_generic_frame_duration);
  163. __le16 ieee80211_rts_duration(struct ieee80211_hw *hw,
  164. struct ieee80211_vif *vif, size_t frame_len,
  165. const struct ieee80211_tx_info *frame_txctl)
  166. {
  167. struct ieee80211_local *local = hw_to_local(hw);
  168. struct ieee80211_rate *rate;
  169. struct ieee80211_sub_if_data *sdata;
  170. bool short_preamble;
  171. int erp;
  172. u16 dur;
  173. struct ieee80211_supported_band *sband;
  174. sband = local->hw.wiphy->bands[frame_txctl->band];
  175. short_preamble = false;
  176. rate = &sband->bitrates[frame_txctl->control.rts_cts_rate_idx];
  177. erp = 0;
  178. if (vif) {
  179. sdata = vif_to_sdata(vif);
  180. short_preamble = sdata->vif.bss_conf.use_short_preamble;
  181. if (sdata->flags & IEEE80211_SDATA_OPERATING_GMODE)
  182. erp = rate->flags & IEEE80211_RATE_ERP_G;
  183. }
  184. /* CTS duration */
  185. dur = ieee80211_frame_duration(sband->band, 10, rate->bitrate,
  186. erp, short_preamble);
  187. /* Data frame duration */
  188. dur += ieee80211_frame_duration(sband->band, frame_len, rate->bitrate,
  189. erp, short_preamble);
  190. /* ACK duration */
  191. dur += ieee80211_frame_duration(sband->band, 10, rate->bitrate,
  192. erp, short_preamble);
  193. return cpu_to_le16(dur);
  194. }
  195. EXPORT_SYMBOL(ieee80211_rts_duration);
  196. __le16 ieee80211_ctstoself_duration(struct ieee80211_hw *hw,
  197. struct ieee80211_vif *vif,
  198. size_t frame_len,
  199. const struct ieee80211_tx_info *frame_txctl)
  200. {
  201. struct ieee80211_local *local = hw_to_local(hw);
  202. struct ieee80211_rate *rate;
  203. struct ieee80211_sub_if_data *sdata;
  204. bool short_preamble;
  205. int erp;
  206. u16 dur;
  207. struct ieee80211_supported_band *sband;
  208. sband = local->hw.wiphy->bands[frame_txctl->band];
  209. short_preamble = false;
  210. rate = &sband->bitrates[frame_txctl->control.rts_cts_rate_idx];
  211. erp = 0;
  212. if (vif) {
  213. sdata = vif_to_sdata(vif);
  214. short_preamble = sdata->vif.bss_conf.use_short_preamble;
  215. if (sdata->flags & IEEE80211_SDATA_OPERATING_GMODE)
  216. erp = rate->flags & IEEE80211_RATE_ERP_G;
  217. }
  218. /* Data frame duration */
  219. dur = ieee80211_frame_duration(sband->band, frame_len, rate->bitrate,
  220. erp, short_preamble);
  221. if (!(frame_txctl->flags & IEEE80211_TX_CTL_NO_ACK)) {
  222. /* ACK duration */
  223. dur += ieee80211_frame_duration(sband->band, 10, rate->bitrate,
  224. erp, short_preamble);
  225. }
  226. return cpu_to_le16(dur);
  227. }
  228. EXPORT_SYMBOL(ieee80211_ctstoself_duration);
  229. void ieee80211_propagate_queue_wake(struct ieee80211_local *local, int queue)
  230. {
  231. struct ieee80211_sub_if_data *sdata;
  232. list_for_each_entry_rcu(sdata, &local->interfaces, list) {
  233. int ac;
  234. if (test_bit(SDATA_STATE_OFFCHANNEL, &sdata->state))
  235. continue;
  236. if (sdata->vif.cab_queue != IEEE80211_INVAL_HW_QUEUE &&
  237. local->queue_stop_reasons[sdata->vif.cab_queue] != 0)
  238. continue;
  239. for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
  240. int ac_queue = sdata->vif.hw_queue[ac];
  241. if (ac_queue == queue ||
  242. (sdata->vif.cab_queue == queue &&
  243. local->queue_stop_reasons[ac_queue] == 0 &&
  244. skb_queue_empty(&local->pending[ac_queue])))
  245. netif_wake_subqueue(sdata->dev, ac);
  246. }
  247. }
  248. }
  249. static void __ieee80211_wake_queue(struct ieee80211_hw *hw, int queue,
  250. enum queue_stop_reason reason)
  251. {
  252. struct ieee80211_local *local = hw_to_local(hw);
  253. trace_wake_queue(local, queue, reason);
  254. if (WARN_ON(queue >= hw->queues))
  255. return;
  256. if (!test_bit(reason, &local->queue_stop_reasons[queue]))
  257. return;
  258. __clear_bit(reason, &local->queue_stop_reasons[queue]);
  259. if (local->queue_stop_reasons[queue] != 0)
  260. /* someone still has this queue stopped */
  261. return;
  262. if (skb_queue_empty(&local->pending[queue])) {
  263. rcu_read_lock();
  264. ieee80211_propagate_queue_wake(local, queue);
  265. rcu_read_unlock();
  266. } else
  267. tasklet_schedule(&local->tx_pending_tasklet);
  268. }
  269. void ieee80211_wake_queue_by_reason(struct ieee80211_hw *hw, int queue,
  270. enum queue_stop_reason reason)
  271. {
  272. struct ieee80211_local *local = hw_to_local(hw);
  273. unsigned long flags;
  274. spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
  275. __ieee80211_wake_queue(hw, queue, reason);
  276. spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
  277. }
  278. void ieee80211_wake_queue(struct ieee80211_hw *hw, int queue)
  279. {
  280. ieee80211_wake_queue_by_reason(hw, queue,
  281. IEEE80211_QUEUE_STOP_REASON_DRIVER);
  282. }
  283. EXPORT_SYMBOL(ieee80211_wake_queue);
  284. static void __ieee80211_stop_queue(struct ieee80211_hw *hw, int queue,
  285. enum queue_stop_reason reason)
  286. {
  287. struct ieee80211_local *local = hw_to_local(hw);
  288. struct ieee80211_sub_if_data *sdata;
  289. trace_stop_queue(local, queue, reason);
  290. if (WARN_ON(queue >= hw->queues))
  291. return;
  292. if (test_bit(reason, &local->queue_stop_reasons[queue]))
  293. return;
  294. __set_bit(reason, &local->queue_stop_reasons[queue]);
  295. rcu_read_lock();
  296. list_for_each_entry_rcu(sdata, &local->interfaces, list) {
  297. int ac;
  298. for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
  299. if (sdata->vif.hw_queue[ac] == queue ||
  300. sdata->vif.cab_queue == queue)
  301. netif_stop_subqueue(sdata->dev, ac);
  302. }
  303. }
  304. rcu_read_unlock();
  305. }
  306. void ieee80211_stop_queue_by_reason(struct ieee80211_hw *hw, int queue,
  307. enum queue_stop_reason reason)
  308. {
  309. struct ieee80211_local *local = hw_to_local(hw);
  310. unsigned long flags;
  311. spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
  312. __ieee80211_stop_queue(hw, queue, reason);
  313. spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
  314. }
  315. void ieee80211_stop_queue(struct ieee80211_hw *hw, int queue)
  316. {
  317. ieee80211_stop_queue_by_reason(hw, queue,
  318. IEEE80211_QUEUE_STOP_REASON_DRIVER);
  319. }
  320. EXPORT_SYMBOL(ieee80211_stop_queue);
  321. void ieee80211_add_pending_skb(struct ieee80211_local *local,
  322. struct sk_buff *skb)
  323. {
  324. struct ieee80211_hw *hw = &local->hw;
  325. unsigned long flags;
  326. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  327. int queue = info->hw_queue;
  328. if (WARN_ON(!info->control.vif)) {
  329. kfree_skb(skb);
  330. return;
  331. }
  332. spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
  333. __ieee80211_stop_queue(hw, queue, IEEE80211_QUEUE_STOP_REASON_SKB_ADD);
  334. __skb_queue_tail(&local->pending[queue], skb);
  335. __ieee80211_wake_queue(hw, queue, IEEE80211_QUEUE_STOP_REASON_SKB_ADD);
  336. spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
  337. }
  338. void ieee80211_add_pending_skbs_fn(struct ieee80211_local *local,
  339. struct sk_buff_head *skbs,
  340. void (*fn)(void *data), void *data)
  341. {
  342. struct ieee80211_hw *hw = &local->hw;
  343. struct sk_buff *skb;
  344. unsigned long flags;
  345. int queue, i;
  346. spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
  347. while ((skb = skb_dequeue(skbs))) {
  348. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  349. if (WARN_ON(!info->control.vif)) {
  350. kfree_skb(skb);
  351. continue;
  352. }
  353. queue = info->hw_queue;
  354. __ieee80211_stop_queue(hw, queue,
  355. IEEE80211_QUEUE_STOP_REASON_SKB_ADD);
  356. __skb_queue_tail(&local->pending[queue], skb);
  357. }
  358. if (fn)
  359. fn(data);
  360. for (i = 0; i < hw->queues; i++)
  361. __ieee80211_wake_queue(hw, i,
  362. IEEE80211_QUEUE_STOP_REASON_SKB_ADD);
  363. spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
  364. }
  365. void ieee80211_stop_queues_by_reason(struct ieee80211_hw *hw,
  366. enum queue_stop_reason reason)
  367. {
  368. struct ieee80211_local *local = hw_to_local(hw);
  369. unsigned long flags;
  370. int i;
  371. spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
  372. for (i = 0; i < hw->queues; i++)
  373. __ieee80211_stop_queue(hw, i, reason);
  374. spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
  375. }
  376. void ieee80211_stop_queues(struct ieee80211_hw *hw)
  377. {
  378. ieee80211_stop_queues_by_reason(hw,
  379. IEEE80211_QUEUE_STOP_REASON_DRIVER);
  380. }
  381. EXPORT_SYMBOL(ieee80211_stop_queues);
  382. int ieee80211_queue_stopped(struct ieee80211_hw *hw, int queue)
  383. {
  384. struct ieee80211_local *local = hw_to_local(hw);
  385. unsigned long flags;
  386. int ret;
  387. if (WARN_ON(queue >= hw->queues))
  388. return true;
  389. spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
  390. ret = !!local->queue_stop_reasons[queue];
  391. spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
  392. return ret;
  393. }
  394. EXPORT_SYMBOL(ieee80211_queue_stopped);
  395. void ieee80211_wake_queues_by_reason(struct ieee80211_hw *hw,
  396. enum queue_stop_reason reason)
  397. {
  398. struct ieee80211_local *local = hw_to_local(hw);
  399. unsigned long flags;
  400. int i;
  401. spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
  402. for (i = 0; i < hw->queues; i++)
  403. __ieee80211_wake_queue(hw, i, reason);
  404. spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
  405. }
  406. void ieee80211_wake_queues(struct ieee80211_hw *hw)
  407. {
  408. ieee80211_wake_queues_by_reason(hw, IEEE80211_QUEUE_STOP_REASON_DRIVER);
  409. }
  410. EXPORT_SYMBOL(ieee80211_wake_queues);
  411. void ieee80211_iterate_active_interfaces(
  412. struct ieee80211_hw *hw,
  413. void (*iterator)(void *data, u8 *mac,
  414. struct ieee80211_vif *vif),
  415. void *data)
  416. {
  417. struct ieee80211_local *local = hw_to_local(hw);
  418. struct ieee80211_sub_if_data *sdata;
  419. mutex_lock(&local->iflist_mtx);
  420. list_for_each_entry(sdata, &local->interfaces, list) {
  421. switch (sdata->vif.type) {
  422. case NL80211_IFTYPE_MONITOR:
  423. case NL80211_IFTYPE_AP_VLAN:
  424. continue;
  425. default:
  426. break;
  427. }
  428. if (ieee80211_sdata_running(sdata))
  429. iterator(data, sdata->vif.addr,
  430. &sdata->vif);
  431. }
  432. mutex_unlock(&local->iflist_mtx);
  433. }
  434. EXPORT_SYMBOL_GPL(ieee80211_iterate_active_interfaces);
  435. void ieee80211_iterate_active_interfaces_atomic(
  436. struct ieee80211_hw *hw,
  437. void (*iterator)(void *data, u8 *mac,
  438. struct ieee80211_vif *vif),
  439. void *data)
  440. {
  441. struct ieee80211_local *local = hw_to_local(hw);
  442. struct ieee80211_sub_if_data *sdata;
  443. rcu_read_lock();
  444. list_for_each_entry_rcu(sdata, &local->interfaces, list) {
  445. switch (sdata->vif.type) {
  446. case NL80211_IFTYPE_MONITOR:
  447. case NL80211_IFTYPE_AP_VLAN:
  448. continue;
  449. default:
  450. break;
  451. }
  452. if (ieee80211_sdata_running(sdata))
  453. iterator(data, sdata->vif.addr,
  454. &sdata->vif);
  455. }
  456. rcu_read_unlock();
  457. }
  458. EXPORT_SYMBOL_GPL(ieee80211_iterate_active_interfaces_atomic);
  459. /*
  460. * Nothing should have been stuffed into the workqueue during
  461. * the suspend->resume cycle. If this WARN is seen then there
  462. * is a bug with either the driver suspend or something in
  463. * mac80211 stuffing into the workqueue which we haven't yet
  464. * cleared during mac80211's suspend cycle.
  465. */
  466. static bool ieee80211_can_queue_work(struct ieee80211_local *local)
  467. {
  468. if (WARN(local->suspended && !local->resuming,
  469. "queueing ieee80211 work while going to suspend\n"))
  470. return false;
  471. return true;
  472. }
  473. void ieee80211_queue_work(struct ieee80211_hw *hw, struct work_struct *work)
  474. {
  475. struct ieee80211_local *local = hw_to_local(hw);
  476. if (!ieee80211_can_queue_work(local))
  477. return;
  478. queue_work(local->workqueue, work);
  479. }
  480. EXPORT_SYMBOL(ieee80211_queue_work);
  481. void ieee80211_queue_delayed_work(struct ieee80211_hw *hw,
  482. struct delayed_work *dwork,
  483. unsigned long delay)
  484. {
  485. struct ieee80211_local *local = hw_to_local(hw);
  486. if (!ieee80211_can_queue_work(local))
  487. return;
  488. queue_delayed_work(local->workqueue, dwork, delay);
  489. }
  490. EXPORT_SYMBOL(ieee80211_queue_delayed_work);
  491. u32 ieee802_11_parse_elems_crc(u8 *start, size_t len,
  492. struct ieee802_11_elems *elems,
  493. u64 filter, u32 crc)
  494. {
  495. size_t left = len;
  496. u8 *pos = start;
  497. bool calc_crc = filter != 0;
  498. DECLARE_BITMAP(seen_elems, 256);
  499. bitmap_zero(seen_elems, 256);
  500. memset(elems, 0, sizeof(*elems));
  501. elems->ie_start = start;
  502. elems->total_len = len;
  503. while (left >= 2) {
  504. u8 id, elen;
  505. bool elem_parse_failed;
  506. id = *pos++;
  507. elen = *pos++;
  508. left -= 2;
  509. if (elen > left) {
  510. elems->parse_error = true;
  511. break;
  512. }
  513. if (id != WLAN_EID_VENDOR_SPECIFIC &&
  514. id != WLAN_EID_QUIET &&
  515. test_bit(id, seen_elems)) {
  516. elems->parse_error = true;
  517. left -= elen;
  518. pos += elen;
  519. continue;
  520. }
  521. if (calc_crc && id < 64 && (filter & (1ULL << id)))
  522. crc = crc32_be(crc, pos - 2, elen + 2);
  523. elem_parse_failed = false;
  524. switch (id) {
  525. case WLAN_EID_SSID:
  526. elems->ssid = pos;
  527. elems->ssid_len = elen;
  528. break;
  529. case WLAN_EID_SUPP_RATES:
  530. elems->supp_rates = pos;
  531. elems->supp_rates_len = elen;
  532. break;
  533. case WLAN_EID_FH_PARAMS:
  534. elems->fh_params = pos;
  535. elems->fh_params_len = elen;
  536. break;
  537. case WLAN_EID_DS_PARAMS:
  538. elems->ds_params = pos;
  539. elems->ds_params_len = elen;
  540. break;
  541. case WLAN_EID_CF_PARAMS:
  542. elems->cf_params = pos;
  543. elems->cf_params_len = elen;
  544. break;
  545. case WLAN_EID_TIM:
  546. if (elen >= sizeof(struct ieee80211_tim_ie)) {
  547. elems->tim = (void *)pos;
  548. elems->tim_len = elen;
  549. } else
  550. elem_parse_failed = true;
  551. break;
  552. case WLAN_EID_IBSS_PARAMS:
  553. elems->ibss_params = pos;
  554. elems->ibss_params_len = elen;
  555. break;
  556. case WLAN_EID_CHALLENGE:
  557. elems->challenge = pos;
  558. elems->challenge_len = elen;
  559. break;
  560. case WLAN_EID_VENDOR_SPECIFIC:
  561. if (elen >= 4 && pos[0] == 0x00 && pos[1] == 0x50 &&
  562. pos[2] == 0xf2) {
  563. /* Microsoft OUI (00:50:F2) */
  564. if (calc_crc)
  565. crc = crc32_be(crc, pos - 2, elen + 2);
  566. if (pos[3] == 1) {
  567. /* OUI Type 1 - WPA IE */
  568. elems->wpa = pos;
  569. elems->wpa_len = elen;
  570. } else if (elen >= 5 && pos[3] == 2) {
  571. /* OUI Type 2 - WMM IE */
  572. if (pos[4] == 0) {
  573. elems->wmm_info = pos;
  574. elems->wmm_info_len = elen;
  575. } else if (pos[4] == 1) {
  576. elems->wmm_param = pos;
  577. elems->wmm_param_len = elen;
  578. }
  579. }
  580. }
  581. break;
  582. case WLAN_EID_RSN:
  583. elems->rsn = pos;
  584. elems->rsn_len = elen;
  585. break;
  586. case WLAN_EID_ERP_INFO:
  587. elems->erp_info = pos;
  588. elems->erp_info_len = elen;
  589. break;
  590. case WLAN_EID_EXT_SUPP_RATES:
  591. elems->ext_supp_rates = pos;
  592. elems->ext_supp_rates_len = elen;
  593. break;
  594. case WLAN_EID_HT_CAPABILITY:
  595. if (elen >= sizeof(struct ieee80211_ht_cap))
  596. elems->ht_cap_elem = (void *)pos;
  597. else
  598. elem_parse_failed = true;
  599. break;
  600. case WLAN_EID_HT_OPERATION:
  601. if (elen >= sizeof(struct ieee80211_ht_operation))
  602. elems->ht_operation = (void *)pos;
  603. else
  604. elem_parse_failed = true;
  605. break;
  606. case WLAN_EID_MESH_ID:
  607. elems->mesh_id = pos;
  608. elems->mesh_id_len = elen;
  609. break;
  610. case WLAN_EID_MESH_CONFIG:
  611. if (elen >= sizeof(struct ieee80211_meshconf_ie))
  612. elems->mesh_config = (void *)pos;
  613. else
  614. elem_parse_failed = true;
  615. break;
  616. case WLAN_EID_PEER_MGMT:
  617. elems->peering = pos;
  618. elems->peering_len = elen;
  619. break;
  620. case WLAN_EID_PREQ:
  621. elems->preq = pos;
  622. elems->preq_len = elen;
  623. break;
  624. case WLAN_EID_PREP:
  625. elems->prep = pos;
  626. elems->prep_len = elen;
  627. break;
  628. case WLAN_EID_PERR:
  629. elems->perr = pos;
  630. elems->perr_len = elen;
  631. break;
  632. case WLAN_EID_RANN:
  633. if (elen >= sizeof(struct ieee80211_rann_ie))
  634. elems->rann = (void *)pos;
  635. else
  636. elem_parse_failed = true;
  637. break;
  638. case WLAN_EID_CHANNEL_SWITCH:
  639. elems->ch_switch_elem = pos;
  640. elems->ch_switch_elem_len = elen;
  641. break;
  642. case WLAN_EID_QUIET:
  643. if (!elems->quiet_elem) {
  644. elems->quiet_elem = pos;
  645. elems->quiet_elem_len = elen;
  646. }
  647. elems->num_of_quiet_elem++;
  648. break;
  649. case WLAN_EID_COUNTRY:
  650. elems->country_elem = pos;
  651. elems->country_elem_len = elen;
  652. break;
  653. case WLAN_EID_PWR_CONSTRAINT:
  654. elems->pwr_constr_elem = pos;
  655. elems->pwr_constr_elem_len = elen;
  656. break;
  657. case WLAN_EID_TIMEOUT_INTERVAL:
  658. elems->timeout_int = pos;
  659. elems->timeout_int_len = elen;
  660. break;
  661. default:
  662. break;
  663. }
  664. if (elem_parse_failed)
  665. elems->parse_error = true;
  666. else
  667. set_bit(id, seen_elems);
  668. left -= elen;
  669. pos += elen;
  670. }
  671. if (left != 0)
  672. elems->parse_error = true;
  673. return crc;
  674. }
  675. void ieee802_11_parse_elems(u8 *start, size_t len,
  676. struct ieee802_11_elems *elems)
  677. {
  678. ieee802_11_parse_elems_crc(start, len, elems, 0, 0);
  679. }
  680. void ieee80211_set_wmm_default(struct ieee80211_sub_if_data *sdata,
  681. bool bss_notify)
  682. {
  683. struct ieee80211_local *local = sdata->local;
  684. struct ieee80211_tx_queue_params qparam;
  685. int ac;
  686. bool use_11b;
  687. int aCWmin, aCWmax;
  688. if (!local->ops->conf_tx)
  689. return;
  690. if (local->hw.queues < IEEE80211_NUM_ACS)
  691. return;
  692. memset(&qparam, 0, sizeof(qparam));
  693. use_11b = (local->hw.conf.channel->band == IEEE80211_BAND_2GHZ) &&
  694. !(sdata->flags & IEEE80211_SDATA_OPERATING_GMODE);
  695. for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
  696. /* Set defaults according to 802.11-2007 Table 7-37 */
  697. aCWmax = 1023;
  698. if (use_11b)
  699. aCWmin = 31;
  700. else
  701. aCWmin = 15;
  702. switch (ac) {
  703. case IEEE80211_AC_BK:
  704. qparam.cw_max = aCWmax;
  705. qparam.cw_min = aCWmin;
  706. qparam.txop = 0;
  707. qparam.aifs = 7;
  708. break;
  709. default: /* never happens but let's not leave undefined */
  710. case IEEE80211_AC_BE:
  711. qparam.cw_max = aCWmax;
  712. qparam.cw_min = aCWmin;
  713. qparam.txop = 0;
  714. qparam.aifs = 3;
  715. break;
  716. case IEEE80211_AC_VI:
  717. qparam.cw_max = aCWmin;
  718. qparam.cw_min = (aCWmin + 1) / 2 - 1;
  719. if (use_11b)
  720. qparam.txop = 6016/32;
  721. else
  722. qparam.txop = 3008/32;
  723. qparam.aifs = 2;
  724. break;
  725. case IEEE80211_AC_VO:
  726. qparam.cw_max = (aCWmin + 1) / 2 - 1;
  727. qparam.cw_min = (aCWmin + 1) / 4 - 1;
  728. if (use_11b)
  729. qparam.txop = 3264/32;
  730. else
  731. qparam.txop = 1504/32;
  732. qparam.aifs = 2;
  733. break;
  734. }
  735. qparam.uapsd = false;
  736. sdata->tx_conf[ac] = qparam;
  737. drv_conf_tx(local, sdata, ac, &qparam);
  738. }
  739. /* after reinitialize QoS TX queues setting to default,
  740. * disable QoS at all */
  741. if (sdata->vif.type != NL80211_IFTYPE_MONITOR) {
  742. sdata->vif.bss_conf.qos =
  743. sdata->vif.type != NL80211_IFTYPE_STATION;
  744. if (bss_notify)
  745. ieee80211_bss_info_change_notify(sdata,
  746. BSS_CHANGED_QOS);
  747. }
  748. }
  749. void ieee80211_sta_def_wmm_params(struct ieee80211_sub_if_data *sdata,
  750. const size_t supp_rates_len,
  751. const u8 *supp_rates)
  752. {
  753. struct ieee80211_local *local = sdata->local;
  754. int i, have_higher_than_11mbit = 0;
  755. /* cf. IEEE 802.11 9.2.12 */
  756. for (i = 0; i < supp_rates_len; i++)
  757. if ((supp_rates[i] & 0x7f) * 5 > 110)
  758. have_higher_than_11mbit = 1;
  759. if (local->hw.conf.channel->band == IEEE80211_BAND_2GHZ &&
  760. have_higher_than_11mbit)
  761. sdata->flags |= IEEE80211_SDATA_OPERATING_GMODE;
  762. else
  763. sdata->flags &= ~IEEE80211_SDATA_OPERATING_GMODE;
  764. ieee80211_set_wmm_default(sdata, true);
  765. }
  766. u32 ieee80211_mandatory_rates(struct ieee80211_local *local,
  767. enum ieee80211_band band)
  768. {
  769. struct ieee80211_supported_band *sband;
  770. struct ieee80211_rate *bitrates;
  771. u32 mandatory_rates;
  772. enum ieee80211_rate_flags mandatory_flag;
  773. int i;
  774. sband = local->hw.wiphy->bands[band];
  775. if (WARN_ON(!sband))
  776. return 1;
  777. if (band == IEEE80211_BAND_2GHZ)
  778. mandatory_flag = IEEE80211_RATE_MANDATORY_B;
  779. else
  780. mandatory_flag = IEEE80211_RATE_MANDATORY_A;
  781. bitrates = sband->bitrates;
  782. mandatory_rates = 0;
  783. for (i = 0; i < sband->n_bitrates; i++)
  784. if (bitrates[i].flags & mandatory_flag)
  785. mandatory_rates |= BIT(i);
  786. return mandatory_rates;
  787. }
  788. void ieee80211_send_auth(struct ieee80211_sub_if_data *sdata,
  789. u16 transaction, u16 auth_alg,
  790. u8 *extra, size_t extra_len, const u8 *da,
  791. const u8 *bssid, const u8 *key, u8 key_len, u8 key_idx)
  792. {
  793. struct ieee80211_local *local = sdata->local;
  794. struct sk_buff *skb;
  795. struct ieee80211_mgmt *mgmt;
  796. int err;
  797. skb = dev_alloc_skb(local->hw.extra_tx_headroom +
  798. sizeof(*mgmt) + 6 + extra_len);
  799. if (!skb)
  800. return;
  801. skb_reserve(skb, local->hw.extra_tx_headroom);
  802. mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24 + 6);
  803. memset(mgmt, 0, 24 + 6);
  804. mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
  805. IEEE80211_STYPE_AUTH);
  806. memcpy(mgmt->da, da, ETH_ALEN);
  807. memcpy(mgmt->sa, sdata->vif.addr, ETH_ALEN);
  808. memcpy(mgmt->bssid, bssid, ETH_ALEN);
  809. mgmt->u.auth.auth_alg = cpu_to_le16(auth_alg);
  810. mgmt->u.auth.auth_transaction = cpu_to_le16(transaction);
  811. mgmt->u.auth.status_code = cpu_to_le16(0);
  812. if (extra)
  813. memcpy(skb_put(skb, extra_len), extra, extra_len);
  814. if (auth_alg == WLAN_AUTH_SHARED_KEY && transaction == 3) {
  815. mgmt->frame_control |= cpu_to_le16(IEEE80211_FCTL_PROTECTED);
  816. err = ieee80211_wep_encrypt(local, skb, key, key_len, key_idx);
  817. WARN_ON(err);
  818. }
  819. IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
  820. ieee80211_tx_skb(sdata, skb);
  821. }
  822. int ieee80211_build_preq_ies(struct ieee80211_local *local, u8 *buffer,
  823. const u8 *ie, size_t ie_len,
  824. enum ieee80211_band band, u32 rate_mask,
  825. u8 channel)
  826. {
  827. struct ieee80211_supported_band *sband;
  828. u8 *pos;
  829. size_t offset = 0, noffset;
  830. int supp_rates_len, i;
  831. u8 rates[32];
  832. int num_rates;
  833. int ext_rates_len;
  834. sband = local->hw.wiphy->bands[band];
  835. pos = buffer;
  836. num_rates = 0;
  837. for (i = 0; i < sband->n_bitrates; i++) {
  838. if ((BIT(i) & rate_mask) == 0)
  839. continue; /* skip rate */
  840. rates[num_rates++] = (u8) (sband->bitrates[i].bitrate / 5);
  841. }
  842. supp_rates_len = min_t(int, num_rates, 8);
  843. *pos++ = WLAN_EID_SUPP_RATES;
  844. *pos++ = supp_rates_len;
  845. memcpy(pos, rates, supp_rates_len);
  846. pos += supp_rates_len;
  847. /* insert "request information" if in custom IEs */
  848. if (ie && ie_len) {
  849. static const u8 before_extrates[] = {
  850. WLAN_EID_SSID,
  851. WLAN_EID_SUPP_RATES,
  852. WLAN_EID_REQUEST,
  853. };
  854. noffset = ieee80211_ie_split(ie, ie_len,
  855. before_extrates,
  856. ARRAY_SIZE(before_extrates),
  857. offset);
  858. memcpy(pos, ie + offset, noffset - offset);
  859. pos += noffset - offset;
  860. offset = noffset;
  861. }
  862. ext_rates_len = num_rates - supp_rates_len;
  863. if (ext_rates_len > 0) {
  864. *pos++ = WLAN_EID_EXT_SUPP_RATES;
  865. *pos++ = ext_rates_len;
  866. memcpy(pos, rates + supp_rates_len, ext_rates_len);
  867. pos += ext_rates_len;
  868. }
  869. if (channel && sband->band == IEEE80211_BAND_2GHZ) {
  870. *pos++ = WLAN_EID_DS_PARAMS;
  871. *pos++ = 1;
  872. *pos++ = channel;
  873. }
  874. /* insert custom IEs that go before HT */
  875. if (ie && ie_len) {
  876. static const u8 before_ht[] = {
  877. WLAN_EID_SSID,
  878. WLAN_EID_SUPP_RATES,
  879. WLAN_EID_REQUEST,
  880. WLAN_EID_EXT_SUPP_RATES,
  881. WLAN_EID_DS_PARAMS,
  882. WLAN_EID_SUPPORTED_REGULATORY_CLASSES,
  883. };
  884. noffset = ieee80211_ie_split(ie, ie_len,
  885. before_ht, ARRAY_SIZE(before_ht),
  886. offset);
  887. memcpy(pos, ie + offset, noffset - offset);
  888. pos += noffset - offset;
  889. offset = noffset;
  890. }
  891. if (sband->ht_cap.ht_supported)
  892. pos = ieee80211_ie_build_ht_cap(pos, &sband->ht_cap,
  893. sband->ht_cap.cap);
  894. /*
  895. * If adding more here, adjust code in main.c
  896. * that calculates local->scan_ies_len.
  897. */
  898. /* add any remaining custom IEs */
  899. if (ie && ie_len) {
  900. noffset = ie_len;
  901. memcpy(pos, ie + offset, noffset - offset);
  902. pos += noffset - offset;
  903. }
  904. return pos - buffer;
  905. }
  906. struct sk_buff *ieee80211_build_probe_req(struct ieee80211_sub_if_data *sdata,
  907. u8 *dst, u32 ratemask,
  908. const u8 *ssid, size_t ssid_len,
  909. const u8 *ie, size_t ie_len,
  910. bool directed)
  911. {
  912. struct ieee80211_local *local = sdata->local;
  913. struct sk_buff *skb;
  914. struct ieee80211_mgmt *mgmt;
  915. size_t buf_len;
  916. u8 *buf;
  917. u8 chan;
  918. /* FIXME: come up with a proper value */
  919. buf = kmalloc(200 + ie_len, GFP_KERNEL);
  920. if (!buf)
  921. return NULL;
  922. /*
  923. * Do not send DS Channel parameter for directed probe requests
  924. * in order to maximize the chance that we get a response. Some
  925. * badly-behaved APs don't respond when this parameter is included.
  926. */
  927. if (directed)
  928. chan = 0;
  929. else
  930. chan = ieee80211_frequency_to_channel(
  931. local->hw.conf.channel->center_freq);
  932. buf_len = ieee80211_build_preq_ies(local, buf, ie, ie_len,
  933. local->hw.conf.channel->band,
  934. ratemask, chan);
  935. skb = ieee80211_probereq_get(&local->hw, &sdata->vif,
  936. ssid, ssid_len,
  937. buf, buf_len);
  938. if (!skb)
  939. goto out;
  940. if (dst) {
  941. mgmt = (struct ieee80211_mgmt *) skb->data;
  942. memcpy(mgmt->da, dst, ETH_ALEN);
  943. memcpy(mgmt->bssid, dst, ETH_ALEN);
  944. }
  945. IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
  946. out:
  947. kfree(buf);
  948. return skb;
  949. }
  950. void ieee80211_send_probe_req(struct ieee80211_sub_if_data *sdata, u8 *dst,
  951. const u8 *ssid, size_t ssid_len,
  952. const u8 *ie, size_t ie_len,
  953. u32 ratemask, bool directed, bool no_cck)
  954. {
  955. struct sk_buff *skb;
  956. skb = ieee80211_build_probe_req(sdata, dst, ratemask, ssid, ssid_len,
  957. ie, ie_len, directed);
  958. if (skb) {
  959. if (no_cck)
  960. IEEE80211_SKB_CB(skb)->flags |=
  961. IEEE80211_TX_CTL_NO_CCK_RATE;
  962. ieee80211_tx_skb(sdata, skb);
  963. }
  964. }
  965. u32 ieee80211_sta_get_rates(struct ieee80211_local *local,
  966. struct ieee802_11_elems *elems,
  967. enum ieee80211_band band, u32 *basic_rates)
  968. {
  969. struct ieee80211_supported_band *sband;
  970. struct ieee80211_rate *bitrates;
  971. size_t num_rates;
  972. u32 supp_rates;
  973. int i, j;
  974. sband = local->hw.wiphy->bands[band];
  975. if (WARN_ON(!sband))
  976. return 1;
  977. bitrates = sband->bitrates;
  978. num_rates = sband->n_bitrates;
  979. supp_rates = 0;
  980. for (i = 0; i < elems->supp_rates_len +
  981. elems->ext_supp_rates_len; i++) {
  982. u8 rate = 0;
  983. int own_rate;
  984. bool is_basic;
  985. if (i < elems->supp_rates_len)
  986. rate = elems->supp_rates[i];
  987. else if (elems->ext_supp_rates)
  988. rate = elems->ext_supp_rates
  989. [i - elems->supp_rates_len];
  990. own_rate = 5 * (rate & 0x7f);
  991. is_basic = !!(rate & 0x80);
  992. if (is_basic && (rate & 0x7f) == BSS_MEMBERSHIP_SELECTOR_HT_PHY)
  993. continue;
  994. for (j = 0; j < num_rates; j++) {
  995. if (bitrates[j].bitrate == own_rate) {
  996. supp_rates |= BIT(j);
  997. if (basic_rates && is_basic)
  998. *basic_rates |= BIT(j);
  999. }
  1000. }
  1001. }
  1002. return supp_rates;
  1003. }
  1004. void ieee80211_stop_device(struct ieee80211_local *local)
  1005. {
  1006. ieee80211_led_radio(local, false);
  1007. ieee80211_mod_tpt_led_trig(local, 0, IEEE80211_TPT_LEDTRIG_FL_RADIO);
  1008. cancel_work_sync(&local->reconfig_filter);
  1009. flush_workqueue(local->workqueue);
  1010. drv_stop(local);
  1011. }
  1012. int ieee80211_reconfig(struct ieee80211_local *local)
  1013. {
  1014. struct ieee80211_hw *hw = &local->hw;
  1015. struct ieee80211_sub_if_data *sdata;
  1016. struct sta_info *sta;
  1017. int res, i;
  1018. #ifdef CONFIG_PM
  1019. if (local->suspended)
  1020. local->resuming = true;
  1021. if (local->wowlan) {
  1022. local->wowlan = false;
  1023. res = drv_resume(local);
  1024. if (res < 0) {
  1025. local->resuming = false;
  1026. return res;
  1027. }
  1028. if (res == 0)
  1029. goto wake_up;
  1030. WARN_ON(res > 1);
  1031. /*
  1032. * res is 1, which means the driver requested
  1033. * to go through a regular reset on wakeup.
  1034. */
  1035. }
  1036. #endif
  1037. /* everything else happens only if HW was up & running */
  1038. if (!local->open_count)
  1039. goto wake_up;
  1040. /*
  1041. * Upon resume hardware can sometimes be goofy due to
  1042. * various platform / driver / bus issues, so restarting
  1043. * the device may at times not work immediately. Propagate
  1044. * the error.
  1045. */
  1046. res = drv_start(local);
  1047. if (res) {
  1048. WARN(local->suspended, "Hardware became unavailable "
  1049. "upon resume. This could be a software issue "
  1050. "prior to suspend or a hardware issue.\n");
  1051. return res;
  1052. }
  1053. /* setup fragmentation threshold */
  1054. drv_set_frag_threshold(local, hw->wiphy->frag_threshold);
  1055. /* setup RTS threshold */
  1056. drv_set_rts_threshold(local, hw->wiphy->rts_threshold);
  1057. /* reset coverage class */
  1058. drv_set_coverage_class(local, hw->wiphy->coverage_class);
  1059. ieee80211_led_radio(local, true);
  1060. ieee80211_mod_tpt_led_trig(local,
  1061. IEEE80211_TPT_LEDTRIG_FL_RADIO, 0);
  1062. /* add interfaces */
  1063. sdata = rtnl_dereference(local->monitor_sdata);
  1064. if (sdata) {
  1065. res = drv_add_interface(local, sdata);
  1066. if (WARN_ON(res)) {
  1067. rcu_assign_pointer(local->monitor_sdata, NULL);
  1068. synchronize_net();
  1069. kfree(sdata);
  1070. }
  1071. }
  1072. list_for_each_entry(sdata, &local->interfaces, list) {
  1073. if (sdata->vif.type != NL80211_IFTYPE_AP_VLAN &&
  1074. sdata->vif.type != NL80211_IFTYPE_MONITOR &&
  1075. ieee80211_sdata_running(sdata))
  1076. res = drv_add_interface(local, sdata);
  1077. }
  1078. /* add STAs back */
  1079. mutex_lock(&local->sta_mtx);
  1080. list_for_each_entry(sta, &local->sta_list, list) {
  1081. if (sta->uploaded) {
  1082. enum ieee80211_sta_state state;
  1083. for (state = IEEE80211_STA_NOTEXIST;
  1084. state < sta->sta_state - 1; state++)
  1085. WARN_ON(drv_sta_state(local, sta->sdata, sta,
  1086. state, state + 1));
  1087. }
  1088. }
  1089. mutex_unlock(&local->sta_mtx);
  1090. /* reconfigure tx conf */
  1091. if (hw->queues >= IEEE80211_NUM_ACS) {
  1092. list_for_each_entry(sdata, &local->interfaces, list) {
  1093. if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN ||
  1094. sdata->vif.type == NL80211_IFTYPE_MONITOR ||
  1095. !ieee80211_sdata_running(sdata))
  1096. continue;
  1097. for (i = 0; i < IEEE80211_NUM_ACS; i++)
  1098. drv_conf_tx(local, sdata, i,
  1099. &sdata->tx_conf[i]);
  1100. }
  1101. }
  1102. /* reconfigure hardware */
  1103. ieee80211_hw_config(local, ~0);
  1104. ieee80211_configure_filter(local);
  1105. /* Finally also reconfigure all the BSS information */
  1106. list_for_each_entry(sdata, &local->interfaces, list) {
  1107. u32 changed;
  1108. if (!ieee80211_sdata_running(sdata))
  1109. continue;
  1110. /* common change flags for all interface types */
  1111. changed = BSS_CHANGED_ERP_CTS_PROT |
  1112. BSS_CHANGED_ERP_PREAMBLE |
  1113. BSS_CHANGED_ERP_SLOT |
  1114. BSS_CHANGED_HT |
  1115. BSS_CHANGED_BASIC_RATES |
  1116. BSS_CHANGED_BEACON_INT |
  1117. BSS_CHANGED_BSSID |
  1118. BSS_CHANGED_CQM |
  1119. BSS_CHANGED_QOS |
  1120. BSS_CHANGED_IDLE;
  1121. switch (sdata->vif.type) {
  1122. case NL80211_IFTYPE_STATION:
  1123. changed |= BSS_CHANGED_ASSOC |
  1124. BSS_CHANGED_ARP_FILTER;
  1125. mutex_lock(&sdata->u.mgd.mtx);
  1126. ieee80211_bss_info_change_notify(sdata, changed);
  1127. mutex_unlock(&sdata->u.mgd.mtx);
  1128. break;
  1129. case NL80211_IFTYPE_ADHOC:
  1130. changed |= BSS_CHANGED_IBSS;
  1131. /* fall through */
  1132. case NL80211_IFTYPE_AP:
  1133. changed |= BSS_CHANGED_SSID;
  1134. if (sdata->vif.type == NL80211_IFTYPE_AP)
  1135. changed |= BSS_CHANGED_AP_PROBE_RESP;
  1136. /* fall through */
  1137. case NL80211_IFTYPE_MESH_POINT:
  1138. changed |= BSS_CHANGED_BEACON |
  1139. BSS_CHANGED_BEACON_ENABLED;
  1140. ieee80211_bss_info_change_notify(sdata, changed);
  1141. break;
  1142. case NL80211_IFTYPE_WDS:
  1143. break;
  1144. case NL80211_IFTYPE_AP_VLAN:
  1145. case NL80211_IFTYPE_MONITOR:
  1146. /* ignore virtual */
  1147. break;
  1148. case NL80211_IFTYPE_UNSPECIFIED:
  1149. case NUM_NL80211_IFTYPES:
  1150. case NL80211_IFTYPE_P2P_CLIENT:
  1151. case NL80211_IFTYPE_P2P_GO:
  1152. WARN_ON(1);
  1153. break;
  1154. }
  1155. }
  1156. ieee80211_recalc_ps(local, -1);
  1157. /*
  1158. * The sta might be in psm against the ap (e.g. because
  1159. * this was the state before a hw restart), so we
  1160. * explicitly send a null packet in order to make sure
  1161. * it'll sync against the ap (and get out of psm).
  1162. */
  1163. if (!(local->hw.conf.flags & IEEE80211_CONF_PS)) {
  1164. list_for_each_entry(sdata, &local->interfaces, list) {
  1165. if (sdata->vif.type != NL80211_IFTYPE_STATION)
  1166. continue;
  1167. ieee80211_send_nullfunc(local, sdata, 0);
  1168. }
  1169. }
  1170. /* add back keys */
  1171. list_for_each_entry(sdata, &local->interfaces, list)
  1172. if (ieee80211_sdata_running(sdata))
  1173. ieee80211_enable_keys(sdata);
  1174. wake_up:
  1175. /*
  1176. * Clear the WLAN_STA_BLOCK_BA flag so new aggregation
  1177. * sessions can be established after a resume.
  1178. *
  1179. * Also tear down aggregation sessions since reconfiguring
  1180. * them in a hardware restart scenario is not easily done
  1181. * right now, and the hardware will have lost information
  1182. * about the sessions, but we and the AP still think they
  1183. * are active. This is really a workaround though.
  1184. */
  1185. if (hw->flags & IEEE80211_HW_AMPDU_AGGREGATION) {
  1186. mutex_lock(&local->sta_mtx);
  1187. list_for_each_entry(sta, &local->sta_list, list) {
  1188. ieee80211_sta_tear_down_BA_sessions(sta, true);
  1189. clear_sta_flag(sta, WLAN_STA_BLOCK_BA);
  1190. }
  1191. mutex_unlock(&local->sta_mtx);
  1192. }
  1193. ieee80211_wake_queues_by_reason(hw,
  1194. IEEE80211_QUEUE_STOP_REASON_SUSPEND);
  1195. /*
  1196. * If this is for hw restart things are still running.
  1197. * We may want to change that later, however.
  1198. */
  1199. if (!local->suspended)
  1200. return 0;
  1201. #ifdef CONFIG_PM
  1202. /* first set suspended false, then resuming */
  1203. local->suspended = false;
  1204. mb();
  1205. local->resuming = false;
  1206. list_for_each_entry(sdata, &local->interfaces, list) {
  1207. switch(sdata->vif.type) {
  1208. case NL80211_IFTYPE_STATION:
  1209. ieee80211_sta_restart(sdata);
  1210. break;
  1211. case NL80211_IFTYPE_ADHOC:
  1212. ieee80211_ibss_restart(sdata);
  1213. break;
  1214. case NL80211_IFTYPE_MESH_POINT:
  1215. ieee80211_mesh_restart(sdata);
  1216. break;
  1217. default:
  1218. break;
  1219. }
  1220. }
  1221. mod_timer(&local->sta_cleanup, jiffies + 1);
  1222. mutex_lock(&local->sta_mtx);
  1223. list_for_each_entry(sta, &local->sta_list, list)
  1224. mesh_plink_restart(sta);
  1225. mutex_unlock(&local->sta_mtx);
  1226. #else
  1227. WARN_ON(1);
  1228. #endif
  1229. return 0;
  1230. }
  1231. void ieee80211_resume_disconnect(struct ieee80211_vif *vif)
  1232. {
  1233. struct ieee80211_sub_if_data *sdata;
  1234. struct ieee80211_local *local;
  1235. struct ieee80211_key *key;
  1236. if (WARN_ON(!vif))
  1237. return;
  1238. sdata = vif_to_sdata(vif);
  1239. local = sdata->local;
  1240. if (WARN_ON(!local->resuming))
  1241. return;
  1242. if (WARN_ON(vif->type != NL80211_IFTYPE_STATION))
  1243. return;
  1244. sdata->flags |= IEEE80211_SDATA_DISCONNECT_RESUME;
  1245. mutex_lock(&local->key_mtx);
  1246. list_for_each_entry(key, &sdata->key_list, list)
  1247. key->flags |= KEY_FLAG_TAINTED;
  1248. mutex_unlock(&local->key_mtx);
  1249. }
  1250. EXPORT_SYMBOL_GPL(ieee80211_resume_disconnect);
  1251. static int check_mgd_smps(struct ieee80211_if_managed *ifmgd,
  1252. enum ieee80211_smps_mode *smps_mode)
  1253. {
  1254. if (ifmgd->associated) {
  1255. *smps_mode = ifmgd->ap_smps;
  1256. if (*smps_mode == IEEE80211_SMPS_AUTOMATIC) {
  1257. if (ifmgd->powersave)
  1258. *smps_mode = IEEE80211_SMPS_DYNAMIC;
  1259. else
  1260. *smps_mode = IEEE80211_SMPS_OFF;
  1261. }
  1262. return 1;
  1263. }
  1264. return 0;
  1265. }
  1266. /* must hold iflist_mtx */
  1267. void ieee80211_recalc_smps(struct ieee80211_local *local)
  1268. {
  1269. struct ieee80211_sub_if_data *sdata;
  1270. enum ieee80211_smps_mode smps_mode = IEEE80211_SMPS_OFF;
  1271. int count = 0;
  1272. lockdep_assert_held(&local->iflist_mtx);
  1273. /*
  1274. * This function could be improved to handle multiple
  1275. * interfaces better, but right now it makes any
  1276. * non-station interfaces force SM PS to be turned
  1277. * off. If there are multiple station interfaces it
  1278. * could also use the best possible mode, e.g. if
  1279. * one is in static and the other in dynamic then
  1280. * dynamic is ok.
  1281. */
  1282. list_for_each_entry(sdata, &local->interfaces, list) {
  1283. if (!ieee80211_sdata_running(sdata))
  1284. continue;
  1285. if (sdata->vif.type != NL80211_IFTYPE_STATION)
  1286. goto set;
  1287. count += check_mgd_smps(&sdata->u.mgd, &smps_mode);
  1288. if (count > 1) {
  1289. smps_mode = IEEE80211_SMPS_OFF;
  1290. break;
  1291. }
  1292. }
  1293. if (smps_mode == local->smps_mode)
  1294. return;
  1295. set:
  1296. local->smps_mode = smps_mode;
  1297. /* changed flag is auto-detected for this */
  1298. ieee80211_hw_config(local, 0);
  1299. }
  1300. static bool ieee80211_id_in_list(const u8 *ids, int n_ids, u8 id)
  1301. {
  1302. int i;
  1303. for (i = 0; i < n_ids; i++)
  1304. if (ids[i] == id)
  1305. return true;
  1306. return false;
  1307. }
  1308. /**
  1309. * ieee80211_ie_split - split an IE buffer according to ordering
  1310. *
  1311. * @ies: the IE buffer
  1312. * @ielen: the length of the IE buffer
  1313. * @ids: an array with element IDs that are allowed before
  1314. * the split
  1315. * @n_ids: the size of the element ID array
  1316. * @offset: offset where to start splitting in the buffer
  1317. *
  1318. * This function splits an IE buffer by updating the @offset
  1319. * variable to point to the location where the buffer should be
  1320. * split.
  1321. *
  1322. * It assumes that the given IE buffer is well-formed, this
  1323. * has to be guaranteed by the caller!
  1324. *
  1325. * It also assumes that the IEs in the buffer are ordered
  1326. * correctly, if not the result of using this function will not
  1327. * be ordered correctly either, i.e. it does no reordering.
  1328. *
  1329. * The function returns the offset where the next part of the
  1330. * buffer starts, which may be @ielen if the entire (remainder)
  1331. * of the buffer should be used.
  1332. */
  1333. size_t ieee80211_ie_split(const u8 *ies, size_t ielen,
  1334. const u8 *ids, int n_ids, size_t offset)
  1335. {
  1336. size_t pos = offset;
  1337. while (pos < ielen && ieee80211_id_in_list(ids, n_ids, ies[pos]))
  1338. pos += 2 + ies[pos + 1];
  1339. return pos;
  1340. }
  1341. size_t ieee80211_ie_split_vendor(const u8 *ies, size_t ielen, size_t offset)
  1342. {
  1343. size_t pos = offset;
  1344. while (pos < ielen && ies[pos] != WLAN_EID_VENDOR_SPECIFIC)
  1345. pos += 2 + ies[pos + 1];
  1346. return pos;
  1347. }
  1348. static void _ieee80211_enable_rssi_reports(struct ieee80211_sub_if_data *sdata,
  1349. int rssi_min_thold,
  1350. int rssi_max_thold)
  1351. {
  1352. trace_api_enable_rssi_reports(sdata, rssi_min_thold, rssi_max_thold);
  1353. if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_STATION))
  1354. return;
  1355. /*
  1356. * Scale up threshold values before storing it, as the RSSI averaging
  1357. * algorithm uses a scaled up value as well. Change this scaling
  1358. * factor if the RSSI averaging algorithm changes.
  1359. */
  1360. sdata->u.mgd.rssi_min_thold = rssi_min_thold*16;
  1361. sdata->u.mgd.rssi_max_thold = rssi_max_thold*16;
  1362. }
  1363. void ieee80211_enable_rssi_reports(struct ieee80211_vif *vif,
  1364. int rssi_min_thold,
  1365. int rssi_max_thold)
  1366. {
  1367. struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
  1368. WARN_ON(rssi_min_thold == rssi_max_thold ||
  1369. rssi_min_thold > rssi_max_thold);
  1370. _ieee80211_enable_rssi_reports(sdata, rssi_min_thold,
  1371. rssi_max_thold);
  1372. }
  1373. EXPORT_SYMBOL(ieee80211_enable_rssi_reports);
  1374. void ieee80211_disable_rssi_reports(struct ieee80211_vif *vif)
  1375. {
  1376. struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
  1377. _ieee80211_enable_rssi_reports(sdata, 0, 0);
  1378. }
  1379. EXPORT_SYMBOL(ieee80211_disable_rssi_reports);
  1380. u8 *ieee80211_ie_build_ht_cap(u8 *pos, struct ieee80211_sta_ht_cap *ht_cap,
  1381. u16 cap)
  1382. {
  1383. __le16 tmp;
  1384. *pos++ = WLAN_EID_HT_CAPABILITY;
  1385. *pos++ = sizeof(struct ieee80211_ht_cap);
  1386. memset(pos, 0, sizeof(struct ieee80211_ht_cap));
  1387. /* capability flags */
  1388. tmp = cpu_to_le16(cap);
  1389. memcpy(pos, &tmp, sizeof(u16));
  1390. pos += sizeof(u16);
  1391. /* AMPDU parameters */
  1392. *pos++ = ht_cap->ampdu_factor |
  1393. (ht_cap->ampdu_density <<
  1394. IEEE80211_HT_AMPDU_PARM_DENSITY_SHIFT);
  1395. /* MCS set */
  1396. memcpy(pos, &ht_cap->mcs, sizeof(ht_cap->mcs));
  1397. pos += sizeof(ht_cap->mcs);
  1398. /* extended capabilities */
  1399. pos += sizeof(__le16);
  1400. /* BF capabilities */
  1401. pos += sizeof(__le32);
  1402. /* antenna selection */
  1403. pos += sizeof(u8);
  1404. return pos;
  1405. }
  1406. u8 *ieee80211_ie_build_ht_oper(u8 *pos, struct ieee80211_sta_ht_cap *ht_cap,
  1407. struct ieee80211_channel *channel,
  1408. enum nl80211_channel_type channel_type,
  1409. u16 prot_mode)
  1410. {
  1411. struct ieee80211_ht_operation *ht_oper;
  1412. /* Build HT Information */
  1413. *pos++ = WLAN_EID_HT_OPERATION;
  1414. *pos++ = sizeof(struct ieee80211_ht_operation);
  1415. ht_oper = (struct ieee80211_ht_operation *)pos;
  1416. ht_oper->primary_chan =
  1417. ieee80211_frequency_to_channel(channel->center_freq);
  1418. switch (channel_type) {
  1419. case NL80211_CHAN_HT40MINUS:
  1420. ht_oper->ht_param = IEEE80211_HT_PARAM_CHA_SEC_BELOW;
  1421. break;
  1422. case NL80211_CHAN_HT40PLUS:
  1423. ht_oper->ht_param = IEEE80211_HT_PARAM_CHA_SEC_ABOVE;
  1424. break;
  1425. case NL80211_CHAN_HT20:
  1426. default:
  1427. ht_oper->ht_param = IEEE80211_HT_PARAM_CHA_SEC_NONE;
  1428. break;
  1429. }
  1430. if (ht_cap->cap & IEEE80211_HT_CAP_SUP_WIDTH_20_40 &&
  1431. channel_type != NL80211_CHAN_NO_HT &&
  1432. channel_type != NL80211_CHAN_HT20)
  1433. ht_oper->ht_param |= IEEE80211_HT_PARAM_CHAN_WIDTH_ANY;
  1434. ht_oper->operation_mode = cpu_to_le16(prot_mode);
  1435. ht_oper->stbc_param = 0x0000;
  1436. /* It seems that Basic MCS set and Supported MCS set
  1437. are identical for the first 10 bytes */
  1438. memset(&ht_oper->basic_set, 0, 16);
  1439. memcpy(&ht_oper->basic_set, &ht_cap->mcs, 10);
  1440. return pos + sizeof(struct ieee80211_ht_operation);
  1441. }
  1442. enum nl80211_channel_type
  1443. ieee80211_ht_oper_to_channel_type(struct ieee80211_ht_operation *ht_oper)
  1444. {
  1445. enum nl80211_channel_type channel_type;
  1446. if (!ht_oper)
  1447. return NL80211_CHAN_NO_HT;
  1448. switch (ht_oper->ht_param & IEEE80211_HT_PARAM_CHA_SEC_OFFSET) {
  1449. case IEEE80211_HT_PARAM_CHA_SEC_NONE:
  1450. channel_type = NL80211_CHAN_HT20;
  1451. break;
  1452. case IEEE80211_HT_PARAM_CHA_SEC_ABOVE:
  1453. channel_type = NL80211_CHAN_HT40PLUS;
  1454. break;
  1455. case IEEE80211_HT_PARAM_CHA_SEC_BELOW:
  1456. channel_type = NL80211_CHAN_HT40MINUS;
  1457. break;
  1458. default:
  1459. channel_type = NL80211_CHAN_NO_HT;
  1460. }
  1461. return channel_type;
  1462. }
  1463. int ieee80211_add_srates_ie(struct ieee80211_vif *vif,
  1464. struct sk_buff *skb, bool need_basic)
  1465. {
  1466. struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
  1467. struct ieee80211_local *local = sdata->local;
  1468. struct ieee80211_supported_band *sband;
  1469. int rate;
  1470. u8 i, rates, *pos;
  1471. u32 basic_rates = vif->bss_conf.basic_rates;
  1472. sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
  1473. rates = sband->n_bitrates;
  1474. if (rates > 8)
  1475. rates = 8;
  1476. if (skb_tailroom(skb) < rates + 2)
  1477. return -ENOMEM;
  1478. pos = skb_put(skb, rates + 2);
  1479. *pos++ = WLAN_EID_SUPP_RATES;
  1480. *pos++ = rates;
  1481. for (i = 0; i < rates; i++) {
  1482. u8 basic = 0;
  1483. if (need_basic && basic_rates & BIT(i))
  1484. basic = 0x80;
  1485. rate = sband->bitrates[i].bitrate;
  1486. *pos++ = basic | (u8) (rate / 5);
  1487. }
  1488. return 0;
  1489. }
  1490. int ieee80211_add_ext_srates_ie(struct ieee80211_vif *vif,
  1491. struct sk_buff *skb, bool need_basic)
  1492. {
  1493. struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
  1494. struct ieee80211_local *local = sdata->local;
  1495. struct ieee80211_supported_band *sband;
  1496. int rate;
  1497. u8 i, exrates, *pos;
  1498. u32 basic_rates = vif->bss_conf.basic_rates;
  1499. sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
  1500. exrates = sband->n_bitrates;
  1501. if (exrates > 8)
  1502. exrates -= 8;
  1503. else
  1504. exrates = 0;
  1505. if (skb_tailroom(skb) < exrates + 2)
  1506. return -ENOMEM;
  1507. if (exrates) {
  1508. pos = skb_put(skb, exrates + 2);
  1509. *pos++ = WLAN_EID_EXT_SUPP_RATES;
  1510. *pos++ = exrates;
  1511. for (i = 8; i < sband->n_bitrates; i++) {
  1512. u8 basic = 0;
  1513. if (need_basic && basic_rates & BIT(i))
  1514. basic = 0x80;
  1515. rate = sband->bitrates[i].bitrate;
  1516. *pos++ = basic | (u8) (rate / 5);
  1517. }
  1518. }
  1519. return 0;
  1520. }
  1521. int ieee80211_ave_rssi(struct ieee80211_vif *vif)
  1522. {
  1523. struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
  1524. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  1525. if (WARN_ON_ONCE(sdata->vif.type != NL80211_IFTYPE_STATION)) {
  1526. /* non-managed type inferfaces */
  1527. return 0;
  1528. }
  1529. return ifmgd->ave_beacon_signal;
  1530. }
  1531. EXPORT_SYMBOL_GPL(ieee80211_ave_rssi);