util.c 44 KB

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