util.c 55 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179
  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. int n_acs = IEEE80211_NUM_ACS;
  233. if (local->hw.queues < IEEE80211_NUM_ACS)
  234. n_acs = 1;
  235. list_for_each_entry_rcu(sdata, &local->interfaces, list) {
  236. int ac;
  237. if (!sdata->dev)
  238. continue;
  239. if (test_bit(SDATA_STATE_OFFCHANNEL, &sdata->state))
  240. continue;
  241. if (sdata->vif.cab_queue != IEEE80211_INVAL_HW_QUEUE &&
  242. local->queue_stop_reasons[sdata->vif.cab_queue] != 0)
  243. continue;
  244. for (ac = 0; ac < n_acs; ac++) {
  245. int ac_queue = sdata->vif.hw_queue[ac];
  246. if (ac_queue == queue ||
  247. (sdata->vif.cab_queue == queue &&
  248. local->queue_stop_reasons[ac_queue] == 0 &&
  249. skb_queue_empty(&local->pending[ac_queue])))
  250. netif_wake_subqueue(sdata->dev, ac);
  251. }
  252. }
  253. }
  254. static void __ieee80211_wake_queue(struct ieee80211_hw *hw, int queue,
  255. enum queue_stop_reason reason)
  256. {
  257. struct ieee80211_local *local = hw_to_local(hw);
  258. trace_wake_queue(local, queue, reason);
  259. if (WARN_ON(queue >= hw->queues))
  260. return;
  261. if (!test_bit(reason, &local->queue_stop_reasons[queue]))
  262. return;
  263. __clear_bit(reason, &local->queue_stop_reasons[queue]);
  264. if (local->queue_stop_reasons[queue] != 0)
  265. /* someone still has this queue stopped */
  266. return;
  267. if (skb_queue_empty(&local->pending[queue])) {
  268. rcu_read_lock();
  269. ieee80211_propagate_queue_wake(local, queue);
  270. rcu_read_unlock();
  271. } else
  272. tasklet_schedule(&local->tx_pending_tasklet);
  273. }
  274. void ieee80211_wake_queue_by_reason(struct ieee80211_hw *hw, int queue,
  275. enum queue_stop_reason reason)
  276. {
  277. struct ieee80211_local *local = hw_to_local(hw);
  278. unsigned long flags;
  279. spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
  280. __ieee80211_wake_queue(hw, queue, reason);
  281. spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
  282. }
  283. void ieee80211_wake_queue(struct ieee80211_hw *hw, int queue)
  284. {
  285. ieee80211_wake_queue_by_reason(hw, queue,
  286. IEEE80211_QUEUE_STOP_REASON_DRIVER);
  287. }
  288. EXPORT_SYMBOL(ieee80211_wake_queue);
  289. static void __ieee80211_stop_queue(struct ieee80211_hw *hw, int queue,
  290. enum queue_stop_reason reason)
  291. {
  292. struct ieee80211_local *local = hw_to_local(hw);
  293. struct ieee80211_sub_if_data *sdata;
  294. int n_acs = IEEE80211_NUM_ACS;
  295. trace_stop_queue(local, queue, reason);
  296. if (WARN_ON(queue >= hw->queues))
  297. return;
  298. if (test_bit(reason, &local->queue_stop_reasons[queue]))
  299. return;
  300. __set_bit(reason, &local->queue_stop_reasons[queue]);
  301. if (local->hw.queues < IEEE80211_NUM_ACS)
  302. n_acs = 1;
  303. rcu_read_lock();
  304. list_for_each_entry_rcu(sdata, &local->interfaces, list) {
  305. int ac;
  306. if (!sdata->dev)
  307. continue;
  308. for (ac = 0; ac < n_acs; ac++) {
  309. if (sdata->vif.hw_queue[ac] == queue ||
  310. sdata->vif.cab_queue == queue)
  311. netif_stop_subqueue(sdata->dev, ac);
  312. }
  313. }
  314. rcu_read_unlock();
  315. }
  316. void ieee80211_stop_queue_by_reason(struct ieee80211_hw *hw, int queue,
  317. enum queue_stop_reason reason)
  318. {
  319. struct ieee80211_local *local = hw_to_local(hw);
  320. unsigned long flags;
  321. spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
  322. __ieee80211_stop_queue(hw, queue, reason);
  323. spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
  324. }
  325. void ieee80211_stop_queue(struct ieee80211_hw *hw, int queue)
  326. {
  327. ieee80211_stop_queue_by_reason(hw, queue,
  328. IEEE80211_QUEUE_STOP_REASON_DRIVER);
  329. }
  330. EXPORT_SYMBOL(ieee80211_stop_queue);
  331. void ieee80211_add_pending_skb(struct ieee80211_local *local,
  332. struct sk_buff *skb)
  333. {
  334. struct ieee80211_hw *hw = &local->hw;
  335. unsigned long flags;
  336. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  337. int queue = info->hw_queue;
  338. if (WARN_ON(!info->control.vif)) {
  339. ieee80211_free_txskb(&local->hw, skb);
  340. return;
  341. }
  342. spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
  343. __ieee80211_stop_queue(hw, queue, IEEE80211_QUEUE_STOP_REASON_SKB_ADD);
  344. __skb_queue_tail(&local->pending[queue], skb);
  345. __ieee80211_wake_queue(hw, queue, IEEE80211_QUEUE_STOP_REASON_SKB_ADD);
  346. spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
  347. }
  348. void ieee80211_add_pending_skbs_fn(struct ieee80211_local *local,
  349. struct sk_buff_head *skbs,
  350. void (*fn)(void *data), void *data)
  351. {
  352. struct ieee80211_hw *hw = &local->hw;
  353. struct sk_buff *skb;
  354. unsigned long flags;
  355. int queue, i;
  356. spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
  357. while ((skb = skb_dequeue(skbs))) {
  358. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  359. if (WARN_ON(!info->control.vif)) {
  360. ieee80211_free_txskb(&local->hw, skb);
  361. continue;
  362. }
  363. queue = info->hw_queue;
  364. __ieee80211_stop_queue(hw, queue,
  365. IEEE80211_QUEUE_STOP_REASON_SKB_ADD);
  366. __skb_queue_tail(&local->pending[queue], skb);
  367. }
  368. if (fn)
  369. fn(data);
  370. for (i = 0; i < hw->queues; i++)
  371. __ieee80211_wake_queue(hw, i,
  372. IEEE80211_QUEUE_STOP_REASON_SKB_ADD);
  373. spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
  374. }
  375. void ieee80211_stop_queues_by_reason(struct ieee80211_hw *hw,
  376. enum queue_stop_reason reason)
  377. {
  378. struct ieee80211_local *local = hw_to_local(hw);
  379. unsigned long flags;
  380. int i;
  381. spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
  382. for (i = 0; i < hw->queues; i++)
  383. __ieee80211_stop_queue(hw, i, reason);
  384. spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
  385. }
  386. void ieee80211_stop_queues(struct ieee80211_hw *hw)
  387. {
  388. ieee80211_stop_queues_by_reason(hw,
  389. IEEE80211_QUEUE_STOP_REASON_DRIVER);
  390. }
  391. EXPORT_SYMBOL(ieee80211_stop_queues);
  392. int ieee80211_queue_stopped(struct ieee80211_hw *hw, int queue)
  393. {
  394. struct ieee80211_local *local = hw_to_local(hw);
  395. unsigned long flags;
  396. int ret;
  397. if (WARN_ON(queue >= hw->queues))
  398. return true;
  399. spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
  400. ret = !!local->queue_stop_reasons[queue];
  401. spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
  402. return ret;
  403. }
  404. EXPORT_SYMBOL(ieee80211_queue_stopped);
  405. void ieee80211_wake_queues_by_reason(struct ieee80211_hw *hw,
  406. enum queue_stop_reason reason)
  407. {
  408. struct ieee80211_local *local = hw_to_local(hw);
  409. unsigned long flags;
  410. int i;
  411. spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
  412. for (i = 0; i < hw->queues; i++)
  413. __ieee80211_wake_queue(hw, i, reason);
  414. spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
  415. }
  416. void ieee80211_wake_queues(struct ieee80211_hw *hw)
  417. {
  418. ieee80211_wake_queues_by_reason(hw, IEEE80211_QUEUE_STOP_REASON_DRIVER);
  419. }
  420. EXPORT_SYMBOL(ieee80211_wake_queues);
  421. void ieee80211_flush_queues(struct ieee80211_local *local,
  422. struct ieee80211_sub_if_data *sdata)
  423. {
  424. u32 queues;
  425. if (!local->ops->flush)
  426. return;
  427. if (sdata && local->hw.flags & IEEE80211_HW_QUEUE_CONTROL) {
  428. int ac;
  429. queues = 0;
  430. for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
  431. queues |= BIT(sdata->vif.hw_queue[ac]);
  432. if (sdata->vif.cab_queue != IEEE80211_INVAL_HW_QUEUE)
  433. queues |= BIT(sdata->vif.cab_queue);
  434. } else {
  435. /* all queues */
  436. queues = BIT(local->hw.queues) - 1;
  437. }
  438. drv_flush(local, queues, false);
  439. }
  440. void ieee80211_iterate_active_interfaces(
  441. struct ieee80211_hw *hw, u32 iter_flags,
  442. void (*iterator)(void *data, u8 *mac,
  443. struct ieee80211_vif *vif),
  444. void *data)
  445. {
  446. struct ieee80211_local *local = hw_to_local(hw);
  447. struct ieee80211_sub_if_data *sdata;
  448. mutex_lock(&local->iflist_mtx);
  449. list_for_each_entry(sdata, &local->interfaces, list) {
  450. switch (sdata->vif.type) {
  451. case NL80211_IFTYPE_MONITOR:
  452. case NL80211_IFTYPE_AP_VLAN:
  453. continue;
  454. default:
  455. break;
  456. }
  457. if (!(iter_flags & IEEE80211_IFACE_ITER_RESUME_ALL) &&
  458. !(sdata->flags & IEEE80211_SDATA_IN_DRIVER))
  459. continue;
  460. if (ieee80211_sdata_running(sdata))
  461. iterator(data, sdata->vif.addr,
  462. &sdata->vif);
  463. }
  464. sdata = rcu_dereference_protected(local->monitor_sdata,
  465. lockdep_is_held(&local->iflist_mtx));
  466. if (sdata &&
  467. (iter_flags & IEEE80211_IFACE_ITER_RESUME_ALL ||
  468. sdata->flags & IEEE80211_SDATA_IN_DRIVER))
  469. iterator(data, sdata->vif.addr, &sdata->vif);
  470. mutex_unlock(&local->iflist_mtx);
  471. }
  472. EXPORT_SYMBOL_GPL(ieee80211_iterate_active_interfaces);
  473. void ieee80211_iterate_active_interfaces_atomic(
  474. struct ieee80211_hw *hw, u32 iter_flags,
  475. void (*iterator)(void *data, u8 *mac,
  476. struct ieee80211_vif *vif),
  477. void *data)
  478. {
  479. struct ieee80211_local *local = hw_to_local(hw);
  480. struct ieee80211_sub_if_data *sdata;
  481. rcu_read_lock();
  482. list_for_each_entry_rcu(sdata, &local->interfaces, list) {
  483. switch (sdata->vif.type) {
  484. case NL80211_IFTYPE_MONITOR:
  485. case NL80211_IFTYPE_AP_VLAN:
  486. continue;
  487. default:
  488. break;
  489. }
  490. if (!(iter_flags & IEEE80211_IFACE_ITER_RESUME_ALL) &&
  491. !(sdata->flags & IEEE80211_SDATA_IN_DRIVER))
  492. continue;
  493. if (ieee80211_sdata_running(sdata))
  494. iterator(data, sdata->vif.addr,
  495. &sdata->vif);
  496. }
  497. sdata = rcu_dereference(local->monitor_sdata);
  498. if (sdata &&
  499. (iter_flags & IEEE80211_IFACE_ITER_RESUME_ALL ||
  500. sdata->flags & IEEE80211_SDATA_IN_DRIVER))
  501. iterator(data, sdata->vif.addr, &sdata->vif);
  502. rcu_read_unlock();
  503. }
  504. EXPORT_SYMBOL_GPL(ieee80211_iterate_active_interfaces_atomic);
  505. /*
  506. * Nothing should have been stuffed into the workqueue during
  507. * the suspend->resume cycle. If this WARN is seen then there
  508. * is a bug with either the driver suspend or something in
  509. * mac80211 stuffing into the workqueue which we haven't yet
  510. * cleared during mac80211's suspend cycle.
  511. */
  512. static bool ieee80211_can_queue_work(struct ieee80211_local *local)
  513. {
  514. if (WARN(local->suspended && !local->resuming,
  515. "queueing ieee80211 work while going to suspend\n"))
  516. return false;
  517. return true;
  518. }
  519. void ieee80211_queue_work(struct ieee80211_hw *hw, struct work_struct *work)
  520. {
  521. struct ieee80211_local *local = hw_to_local(hw);
  522. if (!ieee80211_can_queue_work(local))
  523. return;
  524. queue_work(local->workqueue, work);
  525. }
  526. EXPORT_SYMBOL(ieee80211_queue_work);
  527. void ieee80211_queue_delayed_work(struct ieee80211_hw *hw,
  528. struct delayed_work *dwork,
  529. unsigned long delay)
  530. {
  531. struct ieee80211_local *local = hw_to_local(hw);
  532. if (!ieee80211_can_queue_work(local))
  533. return;
  534. queue_delayed_work(local->workqueue, dwork, delay);
  535. }
  536. EXPORT_SYMBOL(ieee80211_queue_delayed_work);
  537. u32 ieee802_11_parse_elems_crc(u8 *start, size_t len,
  538. struct ieee802_11_elems *elems,
  539. u64 filter, u32 crc)
  540. {
  541. size_t left = len;
  542. u8 *pos = start;
  543. bool calc_crc = filter != 0;
  544. DECLARE_BITMAP(seen_elems, 256);
  545. bitmap_zero(seen_elems, 256);
  546. memset(elems, 0, sizeof(*elems));
  547. elems->ie_start = start;
  548. elems->total_len = len;
  549. while (left >= 2) {
  550. u8 id, elen;
  551. bool elem_parse_failed;
  552. id = *pos++;
  553. elen = *pos++;
  554. left -= 2;
  555. if (elen > left) {
  556. elems->parse_error = true;
  557. break;
  558. }
  559. switch (id) {
  560. case WLAN_EID_SSID:
  561. case WLAN_EID_SUPP_RATES:
  562. case WLAN_EID_FH_PARAMS:
  563. case WLAN_EID_DS_PARAMS:
  564. case WLAN_EID_CF_PARAMS:
  565. case WLAN_EID_TIM:
  566. case WLAN_EID_IBSS_PARAMS:
  567. case WLAN_EID_CHALLENGE:
  568. case WLAN_EID_RSN:
  569. case WLAN_EID_ERP_INFO:
  570. case WLAN_EID_EXT_SUPP_RATES:
  571. case WLAN_EID_HT_CAPABILITY:
  572. case WLAN_EID_HT_OPERATION:
  573. case WLAN_EID_VHT_CAPABILITY:
  574. case WLAN_EID_VHT_OPERATION:
  575. case WLAN_EID_MESH_ID:
  576. case WLAN_EID_MESH_CONFIG:
  577. case WLAN_EID_PEER_MGMT:
  578. case WLAN_EID_PREQ:
  579. case WLAN_EID_PREP:
  580. case WLAN_EID_PERR:
  581. case WLAN_EID_RANN:
  582. case WLAN_EID_CHANNEL_SWITCH:
  583. case WLAN_EID_EXT_CHANSWITCH_ANN:
  584. case WLAN_EID_COUNTRY:
  585. case WLAN_EID_PWR_CONSTRAINT:
  586. case WLAN_EID_TIMEOUT_INTERVAL:
  587. if (test_bit(id, seen_elems)) {
  588. elems->parse_error = true;
  589. left -= elen;
  590. pos += elen;
  591. continue;
  592. }
  593. break;
  594. }
  595. if (calc_crc && id < 64 && (filter & (1ULL << id)))
  596. crc = crc32_be(crc, pos - 2, elen + 2);
  597. elem_parse_failed = false;
  598. switch (id) {
  599. case WLAN_EID_SSID:
  600. elems->ssid = pos;
  601. elems->ssid_len = elen;
  602. break;
  603. case WLAN_EID_SUPP_RATES:
  604. elems->supp_rates = pos;
  605. elems->supp_rates_len = elen;
  606. break;
  607. case WLAN_EID_FH_PARAMS:
  608. elems->fh_params = pos;
  609. elems->fh_params_len = elen;
  610. break;
  611. case WLAN_EID_DS_PARAMS:
  612. elems->ds_params = pos;
  613. elems->ds_params_len = elen;
  614. break;
  615. case WLAN_EID_CF_PARAMS:
  616. elems->cf_params = pos;
  617. elems->cf_params_len = elen;
  618. break;
  619. case WLAN_EID_TIM:
  620. if (elen >= sizeof(struct ieee80211_tim_ie)) {
  621. elems->tim = (void *)pos;
  622. elems->tim_len = elen;
  623. } else
  624. elem_parse_failed = true;
  625. break;
  626. case WLAN_EID_IBSS_PARAMS:
  627. elems->ibss_params = pos;
  628. elems->ibss_params_len = elen;
  629. break;
  630. case WLAN_EID_CHALLENGE:
  631. elems->challenge = pos;
  632. elems->challenge_len = elen;
  633. break;
  634. case WLAN_EID_VENDOR_SPECIFIC:
  635. if (elen >= 4 && pos[0] == 0x00 && pos[1] == 0x50 &&
  636. pos[2] == 0xf2) {
  637. /* Microsoft OUI (00:50:F2) */
  638. if (calc_crc)
  639. crc = crc32_be(crc, pos - 2, elen + 2);
  640. if (elen >= 5 && pos[3] == 2) {
  641. /* OUI Type 2 - WMM IE */
  642. if (pos[4] == 0) {
  643. elems->wmm_info = pos;
  644. elems->wmm_info_len = elen;
  645. } else if (pos[4] == 1) {
  646. elems->wmm_param = pos;
  647. elems->wmm_param_len = elen;
  648. }
  649. }
  650. }
  651. break;
  652. case WLAN_EID_RSN:
  653. elems->rsn = pos;
  654. elems->rsn_len = elen;
  655. break;
  656. case WLAN_EID_ERP_INFO:
  657. elems->erp_info = pos;
  658. elems->erp_info_len = elen;
  659. break;
  660. case WLAN_EID_EXT_SUPP_RATES:
  661. elems->ext_supp_rates = pos;
  662. elems->ext_supp_rates_len = elen;
  663. break;
  664. case WLAN_EID_HT_CAPABILITY:
  665. if (elen >= sizeof(struct ieee80211_ht_cap))
  666. elems->ht_cap_elem = (void *)pos;
  667. else
  668. elem_parse_failed = true;
  669. break;
  670. case WLAN_EID_HT_OPERATION:
  671. if (elen >= sizeof(struct ieee80211_ht_operation))
  672. elems->ht_operation = (void *)pos;
  673. else
  674. elem_parse_failed = true;
  675. break;
  676. case WLAN_EID_VHT_CAPABILITY:
  677. if (elen >= sizeof(struct ieee80211_vht_cap))
  678. elems->vht_cap_elem = (void *)pos;
  679. else
  680. elem_parse_failed = true;
  681. break;
  682. case WLAN_EID_VHT_OPERATION:
  683. if (elen >= sizeof(struct ieee80211_vht_operation))
  684. elems->vht_operation = (void *)pos;
  685. else
  686. elem_parse_failed = true;
  687. break;
  688. case WLAN_EID_OPMODE_NOTIF:
  689. if (elen > 0)
  690. elems->opmode_notif = pos;
  691. else
  692. elem_parse_failed = true;
  693. break;
  694. case WLAN_EID_MESH_ID:
  695. elems->mesh_id = pos;
  696. elems->mesh_id_len = elen;
  697. break;
  698. case WLAN_EID_MESH_CONFIG:
  699. if (elen >= sizeof(struct ieee80211_meshconf_ie))
  700. elems->mesh_config = (void *)pos;
  701. else
  702. elem_parse_failed = true;
  703. break;
  704. case WLAN_EID_PEER_MGMT:
  705. elems->peering = pos;
  706. elems->peering_len = elen;
  707. break;
  708. case WLAN_EID_MESH_AWAKE_WINDOW:
  709. if (elen >= 2)
  710. elems->awake_window = (void *)pos;
  711. break;
  712. case WLAN_EID_PREQ:
  713. elems->preq = pos;
  714. elems->preq_len = elen;
  715. break;
  716. case WLAN_EID_PREP:
  717. elems->prep = pos;
  718. elems->prep_len = elen;
  719. break;
  720. case WLAN_EID_PERR:
  721. elems->perr = pos;
  722. elems->perr_len = elen;
  723. break;
  724. case WLAN_EID_RANN:
  725. if (elen >= sizeof(struct ieee80211_rann_ie))
  726. elems->rann = (void *)pos;
  727. else
  728. elem_parse_failed = true;
  729. break;
  730. case WLAN_EID_CHANNEL_SWITCH:
  731. if (elen != sizeof(struct ieee80211_channel_sw_ie)) {
  732. elem_parse_failed = true;
  733. break;
  734. }
  735. elems->ch_switch_ie = (void *)pos;
  736. break;
  737. case WLAN_EID_QUIET:
  738. if (!elems->quiet_elem) {
  739. elems->quiet_elem = pos;
  740. elems->quiet_elem_len = elen;
  741. }
  742. elems->num_of_quiet_elem++;
  743. break;
  744. case WLAN_EID_COUNTRY:
  745. elems->country_elem = pos;
  746. elems->country_elem_len = elen;
  747. break;
  748. case WLAN_EID_PWR_CONSTRAINT:
  749. if (elen != 1) {
  750. elem_parse_failed = true;
  751. break;
  752. }
  753. elems->pwr_constr_elem = pos;
  754. break;
  755. case WLAN_EID_TIMEOUT_INTERVAL:
  756. elems->timeout_int = pos;
  757. elems->timeout_int_len = elen;
  758. break;
  759. default:
  760. break;
  761. }
  762. if (elem_parse_failed)
  763. elems->parse_error = true;
  764. else
  765. __set_bit(id, seen_elems);
  766. left -= elen;
  767. pos += elen;
  768. }
  769. if (left != 0)
  770. elems->parse_error = true;
  771. return crc;
  772. }
  773. void ieee802_11_parse_elems(u8 *start, size_t len,
  774. struct ieee802_11_elems *elems)
  775. {
  776. ieee802_11_parse_elems_crc(start, len, elems, 0, 0);
  777. }
  778. void ieee80211_set_wmm_default(struct ieee80211_sub_if_data *sdata,
  779. bool bss_notify)
  780. {
  781. struct ieee80211_local *local = sdata->local;
  782. struct ieee80211_tx_queue_params qparam;
  783. struct ieee80211_chanctx_conf *chanctx_conf;
  784. int ac;
  785. bool use_11b, enable_qos;
  786. int aCWmin, aCWmax;
  787. if (!local->ops->conf_tx)
  788. return;
  789. if (local->hw.queues < IEEE80211_NUM_ACS)
  790. return;
  791. memset(&qparam, 0, sizeof(qparam));
  792. rcu_read_lock();
  793. chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
  794. use_11b = (chanctx_conf &&
  795. chanctx_conf->def.chan->band == IEEE80211_BAND_2GHZ) &&
  796. !(sdata->flags & IEEE80211_SDATA_OPERATING_GMODE);
  797. rcu_read_unlock();
  798. /*
  799. * By default disable QoS in STA mode for old access points, which do
  800. * not support 802.11e. New APs will provide proper queue parameters,
  801. * that we will configure later.
  802. */
  803. enable_qos = (sdata->vif.type != NL80211_IFTYPE_STATION);
  804. for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
  805. /* Set defaults according to 802.11-2007 Table 7-37 */
  806. aCWmax = 1023;
  807. if (use_11b)
  808. aCWmin = 31;
  809. else
  810. aCWmin = 15;
  811. if (enable_qos) {
  812. switch (ac) {
  813. case IEEE80211_AC_BK:
  814. qparam.cw_max = aCWmax;
  815. qparam.cw_min = aCWmin;
  816. qparam.txop = 0;
  817. qparam.aifs = 7;
  818. break;
  819. /* never happens but let's not leave undefined */
  820. default:
  821. case IEEE80211_AC_BE:
  822. qparam.cw_max = aCWmax;
  823. qparam.cw_min = aCWmin;
  824. qparam.txop = 0;
  825. qparam.aifs = 3;
  826. break;
  827. case IEEE80211_AC_VI:
  828. qparam.cw_max = aCWmin;
  829. qparam.cw_min = (aCWmin + 1) / 2 - 1;
  830. if (use_11b)
  831. qparam.txop = 6016/32;
  832. else
  833. qparam.txop = 3008/32;
  834. qparam.aifs = 2;
  835. break;
  836. case IEEE80211_AC_VO:
  837. qparam.cw_max = (aCWmin + 1) / 2 - 1;
  838. qparam.cw_min = (aCWmin + 1) / 4 - 1;
  839. if (use_11b)
  840. qparam.txop = 3264/32;
  841. else
  842. qparam.txop = 1504/32;
  843. qparam.aifs = 2;
  844. break;
  845. }
  846. } else {
  847. /* Confiure old 802.11b/g medium access rules. */
  848. qparam.cw_max = aCWmax;
  849. qparam.cw_min = aCWmin;
  850. qparam.txop = 0;
  851. qparam.aifs = 2;
  852. }
  853. qparam.uapsd = false;
  854. sdata->tx_conf[ac] = qparam;
  855. drv_conf_tx(local, sdata, ac, &qparam);
  856. }
  857. if (sdata->vif.type != NL80211_IFTYPE_MONITOR &&
  858. sdata->vif.type != NL80211_IFTYPE_P2P_DEVICE) {
  859. sdata->vif.bss_conf.qos = enable_qos;
  860. if (bss_notify)
  861. ieee80211_bss_info_change_notify(sdata,
  862. BSS_CHANGED_QOS);
  863. }
  864. }
  865. void ieee80211_sta_def_wmm_params(struct ieee80211_sub_if_data *sdata,
  866. const size_t supp_rates_len,
  867. const u8 *supp_rates)
  868. {
  869. struct ieee80211_chanctx_conf *chanctx_conf;
  870. int i, have_higher_than_11mbit = 0;
  871. /* cf. IEEE 802.11 9.2.12 */
  872. for (i = 0; i < supp_rates_len; i++)
  873. if ((supp_rates[i] & 0x7f) * 5 > 110)
  874. have_higher_than_11mbit = 1;
  875. rcu_read_lock();
  876. chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
  877. if (chanctx_conf &&
  878. chanctx_conf->def.chan->band == IEEE80211_BAND_2GHZ &&
  879. have_higher_than_11mbit)
  880. sdata->flags |= IEEE80211_SDATA_OPERATING_GMODE;
  881. else
  882. sdata->flags &= ~IEEE80211_SDATA_OPERATING_GMODE;
  883. rcu_read_unlock();
  884. ieee80211_set_wmm_default(sdata, true);
  885. }
  886. u32 ieee80211_mandatory_rates(struct ieee80211_local *local,
  887. enum ieee80211_band band)
  888. {
  889. struct ieee80211_supported_band *sband;
  890. struct ieee80211_rate *bitrates;
  891. u32 mandatory_rates;
  892. enum ieee80211_rate_flags mandatory_flag;
  893. int i;
  894. sband = local->hw.wiphy->bands[band];
  895. if (WARN_ON(!sband))
  896. return 1;
  897. if (band == IEEE80211_BAND_2GHZ)
  898. mandatory_flag = IEEE80211_RATE_MANDATORY_B;
  899. else
  900. mandatory_flag = IEEE80211_RATE_MANDATORY_A;
  901. bitrates = sband->bitrates;
  902. mandatory_rates = 0;
  903. for (i = 0; i < sband->n_bitrates; i++)
  904. if (bitrates[i].flags & mandatory_flag)
  905. mandatory_rates |= BIT(i);
  906. return mandatory_rates;
  907. }
  908. void ieee80211_send_auth(struct ieee80211_sub_if_data *sdata,
  909. u16 transaction, u16 auth_alg, u16 status,
  910. const u8 *extra, size_t extra_len, const u8 *da,
  911. const u8 *bssid, const u8 *key, u8 key_len, u8 key_idx,
  912. u32 tx_flags)
  913. {
  914. struct ieee80211_local *local = sdata->local;
  915. struct sk_buff *skb;
  916. struct ieee80211_mgmt *mgmt;
  917. int err;
  918. skb = dev_alloc_skb(local->hw.extra_tx_headroom +
  919. sizeof(*mgmt) + 6 + extra_len);
  920. if (!skb)
  921. return;
  922. skb_reserve(skb, local->hw.extra_tx_headroom);
  923. mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24 + 6);
  924. memset(mgmt, 0, 24 + 6);
  925. mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
  926. IEEE80211_STYPE_AUTH);
  927. memcpy(mgmt->da, da, ETH_ALEN);
  928. memcpy(mgmt->sa, sdata->vif.addr, ETH_ALEN);
  929. memcpy(mgmt->bssid, bssid, ETH_ALEN);
  930. mgmt->u.auth.auth_alg = cpu_to_le16(auth_alg);
  931. mgmt->u.auth.auth_transaction = cpu_to_le16(transaction);
  932. mgmt->u.auth.status_code = cpu_to_le16(status);
  933. if (extra)
  934. memcpy(skb_put(skb, extra_len), extra, extra_len);
  935. if (auth_alg == WLAN_AUTH_SHARED_KEY && transaction == 3) {
  936. mgmt->frame_control |= cpu_to_le16(IEEE80211_FCTL_PROTECTED);
  937. err = ieee80211_wep_encrypt(local, skb, key, key_len, key_idx);
  938. WARN_ON(err);
  939. }
  940. IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT |
  941. tx_flags;
  942. ieee80211_tx_skb(sdata, skb);
  943. }
  944. void ieee80211_send_deauth_disassoc(struct ieee80211_sub_if_data *sdata,
  945. const u8 *bssid, u16 stype, u16 reason,
  946. bool send_frame, u8 *frame_buf)
  947. {
  948. struct ieee80211_local *local = sdata->local;
  949. struct sk_buff *skb;
  950. struct ieee80211_mgmt *mgmt = (void *)frame_buf;
  951. /* build frame */
  952. mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT | stype);
  953. mgmt->duration = 0; /* initialize only */
  954. mgmt->seq_ctrl = 0; /* initialize only */
  955. memcpy(mgmt->da, bssid, ETH_ALEN);
  956. memcpy(mgmt->sa, sdata->vif.addr, ETH_ALEN);
  957. memcpy(mgmt->bssid, bssid, ETH_ALEN);
  958. /* u.deauth.reason_code == u.disassoc.reason_code */
  959. mgmt->u.deauth.reason_code = cpu_to_le16(reason);
  960. if (send_frame) {
  961. skb = dev_alloc_skb(local->hw.extra_tx_headroom +
  962. IEEE80211_DEAUTH_FRAME_LEN);
  963. if (!skb)
  964. return;
  965. skb_reserve(skb, local->hw.extra_tx_headroom);
  966. /* copy in frame */
  967. memcpy(skb_put(skb, IEEE80211_DEAUTH_FRAME_LEN),
  968. mgmt, IEEE80211_DEAUTH_FRAME_LEN);
  969. if (sdata->vif.type != NL80211_IFTYPE_STATION ||
  970. !(sdata->u.mgd.flags & IEEE80211_STA_MFP_ENABLED))
  971. IEEE80211_SKB_CB(skb)->flags |=
  972. IEEE80211_TX_INTFL_DONT_ENCRYPT;
  973. ieee80211_tx_skb(sdata, skb);
  974. }
  975. }
  976. int ieee80211_build_preq_ies(struct ieee80211_local *local, u8 *buffer,
  977. size_t buffer_len, const u8 *ie, size_t ie_len,
  978. enum ieee80211_band band, u32 rate_mask,
  979. u8 channel)
  980. {
  981. struct ieee80211_supported_band *sband;
  982. u8 *pos = buffer, *end = buffer + buffer_len;
  983. size_t offset = 0, noffset;
  984. int supp_rates_len, i;
  985. u8 rates[32];
  986. int num_rates;
  987. int ext_rates_len;
  988. sband = local->hw.wiphy->bands[band];
  989. if (WARN_ON_ONCE(!sband))
  990. return 0;
  991. num_rates = 0;
  992. for (i = 0; i < sband->n_bitrates; i++) {
  993. if ((BIT(i) & rate_mask) == 0)
  994. continue; /* skip rate */
  995. rates[num_rates++] = (u8) (sband->bitrates[i].bitrate / 5);
  996. }
  997. supp_rates_len = min_t(int, num_rates, 8);
  998. if (end - pos < 2 + supp_rates_len)
  999. goto out_err;
  1000. *pos++ = WLAN_EID_SUPP_RATES;
  1001. *pos++ = supp_rates_len;
  1002. memcpy(pos, rates, supp_rates_len);
  1003. pos += supp_rates_len;
  1004. /* insert "request information" if in custom IEs */
  1005. if (ie && ie_len) {
  1006. static const u8 before_extrates[] = {
  1007. WLAN_EID_SSID,
  1008. WLAN_EID_SUPP_RATES,
  1009. WLAN_EID_REQUEST,
  1010. };
  1011. noffset = ieee80211_ie_split(ie, ie_len,
  1012. before_extrates,
  1013. ARRAY_SIZE(before_extrates),
  1014. offset);
  1015. if (end - pos < noffset - offset)
  1016. goto out_err;
  1017. memcpy(pos, ie + offset, noffset - offset);
  1018. pos += noffset - offset;
  1019. offset = noffset;
  1020. }
  1021. ext_rates_len = num_rates - supp_rates_len;
  1022. if (ext_rates_len > 0) {
  1023. if (end - pos < 2 + ext_rates_len)
  1024. goto out_err;
  1025. *pos++ = WLAN_EID_EXT_SUPP_RATES;
  1026. *pos++ = ext_rates_len;
  1027. memcpy(pos, rates + supp_rates_len, ext_rates_len);
  1028. pos += ext_rates_len;
  1029. }
  1030. if (channel && sband->band == IEEE80211_BAND_2GHZ) {
  1031. if (end - pos < 3)
  1032. goto out_err;
  1033. *pos++ = WLAN_EID_DS_PARAMS;
  1034. *pos++ = 1;
  1035. *pos++ = channel;
  1036. }
  1037. /* insert custom IEs that go before HT */
  1038. if (ie && ie_len) {
  1039. static const u8 before_ht[] = {
  1040. WLAN_EID_SSID,
  1041. WLAN_EID_SUPP_RATES,
  1042. WLAN_EID_REQUEST,
  1043. WLAN_EID_EXT_SUPP_RATES,
  1044. WLAN_EID_DS_PARAMS,
  1045. WLAN_EID_SUPPORTED_REGULATORY_CLASSES,
  1046. };
  1047. noffset = ieee80211_ie_split(ie, ie_len,
  1048. before_ht, ARRAY_SIZE(before_ht),
  1049. offset);
  1050. if (end - pos < noffset - offset)
  1051. goto out_err;
  1052. memcpy(pos, ie + offset, noffset - offset);
  1053. pos += noffset - offset;
  1054. offset = noffset;
  1055. }
  1056. if (sband->ht_cap.ht_supported) {
  1057. if (end - pos < 2 + sizeof(struct ieee80211_ht_cap))
  1058. goto out_err;
  1059. pos = ieee80211_ie_build_ht_cap(pos, &sband->ht_cap,
  1060. sband->ht_cap.cap);
  1061. }
  1062. /*
  1063. * If adding more here, adjust code in main.c
  1064. * that calculates local->scan_ies_len.
  1065. */
  1066. /* add any remaining custom IEs */
  1067. if (ie && ie_len) {
  1068. noffset = ie_len;
  1069. if (end - pos < noffset - offset)
  1070. goto out_err;
  1071. memcpy(pos, ie + offset, noffset - offset);
  1072. pos += noffset - offset;
  1073. }
  1074. if (sband->vht_cap.vht_supported) {
  1075. if (end - pos < 2 + sizeof(struct ieee80211_vht_cap))
  1076. goto out_err;
  1077. pos = ieee80211_ie_build_vht_cap(pos, &sband->vht_cap,
  1078. sband->vht_cap.cap);
  1079. }
  1080. return pos - buffer;
  1081. out_err:
  1082. WARN_ONCE(1, "not enough space for preq IEs\n");
  1083. return pos - buffer;
  1084. }
  1085. struct sk_buff *ieee80211_build_probe_req(struct ieee80211_sub_if_data *sdata,
  1086. u8 *dst, u32 ratemask,
  1087. struct ieee80211_channel *chan,
  1088. const u8 *ssid, size_t ssid_len,
  1089. const u8 *ie, size_t ie_len,
  1090. bool directed)
  1091. {
  1092. struct ieee80211_local *local = sdata->local;
  1093. struct sk_buff *skb;
  1094. struct ieee80211_mgmt *mgmt;
  1095. u8 chan_no;
  1096. int ies_len;
  1097. /*
  1098. * Do not send DS Channel parameter for directed probe requests
  1099. * in order to maximize the chance that we get a response. Some
  1100. * badly-behaved APs don't respond when this parameter is included.
  1101. */
  1102. if (directed)
  1103. chan_no = 0;
  1104. else
  1105. chan_no = ieee80211_frequency_to_channel(chan->center_freq);
  1106. skb = ieee80211_probereq_get(&local->hw, &sdata->vif,
  1107. ssid, ssid_len, 100 + ie_len);
  1108. if (!skb)
  1109. return NULL;
  1110. ies_len = ieee80211_build_preq_ies(local, skb_tail_pointer(skb),
  1111. skb_tailroom(skb),
  1112. ie, ie_len, chan->band,
  1113. ratemask, chan_no);
  1114. skb_put(skb, ies_len);
  1115. if (dst) {
  1116. mgmt = (struct ieee80211_mgmt *) skb->data;
  1117. memcpy(mgmt->da, dst, ETH_ALEN);
  1118. memcpy(mgmt->bssid, dst, ETH_ALEN);
  1119. }
  1120. IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
  1121. return skb;
  1122. }
  1123. void ieee80211_send_probe_req(struct ieee80211_sub_if_data *sdata, u8 *dst,
  1124. const u8 *ssid, size_t ssid_len,
  1125. const u8 *ie, size_t ie_len,
  1126. u32 ratemask, bool directed, u32 tx_flags,
  1127. struct ieee80211_channel *channel, bool scan)
  1128. {
  1129. struct sk_buff *skb;
  1130. skb = ieee80211_build_probe_req(sdata, dst, ratemask, channel,
  1131. ssid, ssid_len,
  1132. ie, ie_len, directed);
  1133. if (skb) {
  1134. IEEE80211_SKB_CB(skb)->flags |= tx_flags;
  1135. if (scan)
  1136. ieee80211_tx_skb_tid_band(sdata, skb, 7, channel->band);
  1137. else
  1138. ieee80211_tx_skb(sdata, skb);
  1139. }
  1140. }
  1141. u32 ieee80211_sta_get_rates(struct ieee80211_local *local,
  1142. struct ieee802_11_elems *elems,
  1143. enum ieee80211_band band, u32 *basic_rates)
  1144. {
  1145. struct ieee80211_supported_band *sband;
  1146. struct ieee80211_rate *bitrates;
  1147. size_t num_rates;
  1148. u32 supp_rates;
  1149. int i, j;
  1150. sband = local->hw.wiphy->bands[band];
  1151. if (WARN_ON(!sband))
  1152. return 1;
  1153. bitrates = sband->bitrates;
  1154. num_rates = sband->n_bitrates;
  1155. supp_rates = 0;
  1156. for (i = 0; i < elems->supp_rates_len +
  1157. elems->ext_supp_rates_len; i++) {
  1158. u8 rate = 0;
  1159. int own_rate;
  1160. bool is_basic;
  1161. if (i < elems->supp_rates_len)
  1162. rate = elems->supp_rates[i];
  1163. else if (elems->ext_supp_rates)
  1164. rate = elems->ext_supp_rates
  1165. [i - elems->supp_rates_len];
  1166. own_rate = 5 * (rate & 0x7f);
  1167. is_basic = !!(rate & 0x80);
  1168. if (is_basic && (rate & 0x7f) == BSS_MEMBERSHIP_SELECTOR_HT_PHY)
  1169. continue;
  1170. for (j = 0; j < num_rates; j++) {
  1171. if (bitrates[j].bitrate == own_rate) {
  1172. supp_rates |= BIT(j);
  1173. if (basic_rates && is_basic)
  1174. *basic_rates |= BIT(j);
  1175. }
  1176. }
  1177. }
  1178. return supp_rates;
  1179. }
  1180. void ieee80211_stop_device(struct ieee80211_local *local)
  1181. {
  1182. ieee80211_led_radio(local, false);
  1183. ieee80211_mod_tpt_led_trig(local, 0, IEEE80211_TPT_LEDTRIG_FL_RADIO);
  1184. cancel_work_sync(&local->reconfig_filter);
  1185. flush_workqueue(local->workqueue);
  1186. drv_stop(local);
  1187. }
  1188. static void ieee80211_assign_chanctx(struct ieee80211_local *local,
  1189. struct ieee80211_sub_if_data *sdata)
  1190. {
  1191. struct ieee80211_chanctx_conf *conf;
  1192. struct ieee80211_chanctx *ctx;
  1193. if (!local->use_chanctx)
  1194. return;
  1195. mutex_lock(&local->chanctx_mtx);
  1196. conf = rcu_dereference_protected(sdata->vif.chanctx_conf,
  1197. lockdep_is_held(&local->chanctx_mtx));
  1198. if (conf) {
  1199. ctx = container_of(conf, struct ieee80211_chanctx, conf);
  1200. drv_assign_vif_chanctx(local, sdata, ctx);
  1201. }
  1202. mutex_unlock(&local->chanctx_mtx);
  1203. }
  1204. int ieee80211_reconfig(struct ieee80211_local *local)
  1205. {
  1206. struct ieee80211_hw *hw = &local->hw;
  1207. struct ieee80211_sub_if_data *sdata;
  1208. struct ieee80211_chanctx *ctx;
  1209. struct sta_info *sta;
  1210. int res, i;
  1211. bool reconfig_due_to_wowlan = false;
  1212. #ifdef CONFIG_PM
  1213. if (local->suspended)
  1214. local->resuming = true;
  1215. if (local->wowlan) {
  1216. local->wowlan = false;
  1217. res = drv_resume(local);
  1218. if (res < 0) {
  1219. local->resuming = false;
  1220. return res;
  1221. }
  1222. if (res == 0)
  1223. goto wake_up;
  1224. WARN_ON(res > 1);
  1225. /*
  1226. * res is 1, which means the driver requested
  1227. * to go through a regular reset on wakeup.
  1228. */
  1229. reconfig_due_to_wowlan = true;
  1230. }
  1231. #endif
  1232. /* everything else happens only if HW was up & running */
  1233. if (!local->open_count)
  1234. goto wake_up;
  1235. /*
  1236. * Upon resume hardware can sometimes be goofy due to
  1237. * various platform / driver / bus issues, so restarting
  1238. * the device may at times not work immediately. Propagate
  1239. * the error.
  1240. */
  1241. res = drv_start(local);
  1242. if (res) {
  1243. WARN(local->suspended, "Hardware became unavailable "
  1244. "upon resume. This could be a software issue "
  1245. "prior to suspend or a hardware issue.\n");
  1246. return res;
  1247. }
  1248. /* setup fragmentation threshold */
  1249. drv_set_frag_threshold(local, hw->wiphy->frag_threshold);
  1250. /* setup RTS threshold */
  1251. drv_set_rts_threshold(local, hw->wiphy->rts_threshold);
  1252. /* reset coverage class */
  1253. drv_set_coverage_class(local, hw->wiphy->coverage_class);
  1254. ieee80211_led_radio(local, true);
  1255. ieee80211_mod_tpt_led_trig(local,
  1256. IEEE80211_TPT_LEDTRIG_FL_RADIO, 0);
  1257. /* add interfaces */
  1258. sdata = rtnl_dereference(local->monitor_sdata);
  1259. if (sdata) {
  1260. res = drv_add_interface(local, sdata);
  1261. if (WARN_ON(res)) {
  1262. rcu_assign_pointer(local->monitor_sdata, NULL);
  1263. synchronize_net();
  1264. kfree(sdata);
  1265. }
  1266. }
  1267. list_for_each_entry(sdata, &local->interfaces, list) {
  1268. if (sdata->vif.type != NL80211_IFTYPE_AP_VLAN &&
  1269. sdata->vif.type != NL80211_IFTYPE_MONITOR &&
  1270. ieee80211_sdata_running(sdata))
  1271. res = drv_add_interface(local, sdata);
  1272. }
  1273. /* add channel contexts */
  1274. if (local->use_chanctx) {
  1275. mutex_lock(&local->chanctx_mtx);
  1276. list_for_each_entry(ctx, &local->chanctx_list, list)
  1277. WARN_ON(drv_add_chanctx(local, ctx));
  1278. mutex_unlock(&local->chanctx_mtx);
  1279. }
  1280. list_for_each_entry(sdata, &local->interfaces, list) {
  1281. if (!ieee80211_sdata_running(sdata))
  1282. continue;
  1283. ieee80211_assign_chanctx(local, sdata);
  1284. }
  1285. sdata = rtnl_dereference(local->monitor_sdata);
  1286. if (sdata && ieee80211_sdata_running(sdata))
  1287. ieee80211_assign_chanctx(local, sdata);
  1288. /* add STAs back */
  1289. mutex_lock(&local->sta_mtx);
  1290. list_for_each_entry(sta, &local->sta_list, list) {
  1291. enum ieee80211_sta_state state;
  1292. if (!sta->uploaded)
  1293. continue;
  1294. /* AP-mode stations will be added later */
  1295. if (sta->sdata->vif.type == NL80211_IFTYPE_AP)
  1296. continue;
  1297. for (state = IEEE80211_STA_NOTEXIST;
  1298. state < sta->sta_state; state++)
  1299. WARN_ON(drv_sta_state(local, sta->sdata, sta, state,
  1300. state + 1));
  1301. }
  1302. mutex_unlock(&local->sta_mtx);
  1303. /* reconfigure tx conf */
  1304. if (hw->queues >= IEEE80211_NUM_ACS) {
  1305. list_for_each_entry(sdata, &local->interfaces, list) {
  1306. if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN ||
  1307. sdata->vif.type == NL80211_IFTYPE_MONITOR ||
  1308. !ieee80211_sdata_running(sdata))
  1309. continue;
  1310. for (i = 0; i < IEEE80211_NUM_ACS; i++)
  1311. drv_conf_tx(local, sdata, i,
  1312. &sdata->tx_conf[i]);
  1313. }
  1314. }
  1315. /* reconfigure hardware */
  1316. ieee80211_hw_config(local, ~0);
  1317. ieee80211_configure_filter(local);
  1318. /* Finally also reconfigure all the BSS information */
  1319. list_for_each_entry(sdata, &local->interfaces, list) {
  1320. u32 changed;
  1321. if (!ieee80211_sdata_running(sdata))
  1322. continue;
  1323. /* common change flags for all interface types */
  1324. changed = BSS_CHANGED_ERP_CTS_PROT |
  1325. BSS_CHANGED_ERP_PREAMBLE |
  1326. BSS_CHANGED_ERP_SLOT |
  1327. BSS_CHANGED_HT |
  1328. BSS_CHANGED_BASIC_RATES |
  1329. BSS_CHANGED_BEACON_INT |
  1330. BSS_CHANGED_BSSID |
  1331. BSS_CHANGED_CQM |
  1332. BSS_CHANGED_QOS |
  1333. BSS_CHANGED_IDLE |
  1334. BSS_CHANGED_TXPOWER;
  1335. switch (sdata->vif.type) {
  1336. case NL80211_IFTYPE_STATION:
  1337. changed |= BSS_CHANGED_ASSOC |
  1338. BSS_CHANGED_ARP_FILTER |
  1339. BSS_CHANGED_PS;
  1340. if (sdata->u.mgd.dtim_period)
  1341. changed |= BSS_CHANGED_DTIM_PERIOD;
  1342. mutex_lock(&sdata->u.mgd.mtx);
  1343. ieee80211_bss_info_change_notify(sdata, changed);
  1344. mutex_unlock(&sdata->u.mgd.mtx);
  1345. break;
  1346. case NL80211_IFTYPE_ADHOC:
  1347. changed |= BSS_CHANGED_IBSS;
  1348. /* fall through */
  1349. case NL80211_IFTYPE_AP:
  1350. changed |= BSS_CHANGED_SSID | BSS_CHANGED_P2P_PS;
  1351. if (sdata->vif.type == NL80211_IFTYPE_AP) {
  1352. changed |= BSS_CHANGED_AP_PROBE_RESP;
  1353. if (rcu_access_pointer(sdata->u.ap.beacon))
  1354. drv_start_ap(local, sdata);
  1355. }
  1356. /* fall through */
  1357. case NL80211_IFTYPE_MESH_POINT:
  1358. if (sdata->vif.bss_conf.enable_beacon) {
  1359. changed |= BSS_CHANGED_BEACON |
  1360. BSS_CHANGED_BEACON_ENABLED;
  1361. ieee80211_bss_info_change_notify(sdata, changed);
  1362. }
  1363. break;
  1364. case NL80211_IFTYPE_WDS:
  1365. break;
  1366. case NL80211_IFTYPE_AP_VLAN:
  1367. case NL80211_IFTYPE_MONITOR:
  1368. /* ignore virtual */
  1369. break;
  1370. case NL80211_IFTYPE_P2P_DEVICE:
  1371. changed = BSS_CHANGED_IDLE;
  1372. break;
  1373. case NL80211_IFTYPE_UNSPECIFIED:
  1374. case NUM_NL80211_IFTYPES:
  1375. case NL80211_IFTYPE_P2P_CLIENT:
  1376. case NL80211_IFTYPE_P2P_GO:
  1377. WARN_ON(1);
  1378. break;
  1379. }
  1380. }
  1381. ieee80211_recalc_ps(local, -1);
  1382. /*
  1383. * The sta might be in psm against the ap (e.g. because
  1384. * this was the state before a hw restart), so we
  1385. * explicitly send a null packet in order to make sure
  1386. * it'll sync against the ap (and get out of psm).
  1387. */
  1388. if (!(local->hw.conf.flags & IEEE80211_CONF_PS)) {
  1389. list_for_each_entry(sdata, &local->interfaces, list) {
  1390. if (sdata->vif.type != NL80211_IFTYPE_STATION)
  1391. continue;
  1392. if (!sdata->u.mgd.associated)
  1393. continue;
  1394. ieee80211_send_nullfunc(local, sdata, 0);
  1395. }
  1396. }
  1397. /* APs are now beaconing, add back stations */
  1398. mutex_lock(&local->sta_mtx);
  1399. list_for_each_entry(sta, &local->sta_list, list) {
  1400. enum ieee80211_sta_state state;
  1401. if (!sta->uploaded)
  1402. continue;
  1403. if (sta->sdata->vif.type != NL80211_IFTYPE_AP)
  1404. continue;
  1405. for (state = IEEE80211_STA_NOTEXIST;
  1406. state < sta->sta_state; state++)
  1407. WARN_ON(drv_sta_state(local, sta->sdata, sta, state,
  1408. state + 1));
  1409. }
  1410. mutex_unlock(&local->sta_mtx);
  1411. /* add back keys */
  1412. list_for_each_entry(sdata, &local->interfaces, list)
  1413. if (ieee80211_sdata_running(sdata))
  1414. ieee80211_enable_keys(sdata);
  1415. wake_up:
  1416. local->in_reconfig = false;
  1417. barrier();
  1418. /*
  1419. * Clear the WLAN_STA_BLOCK_BA flag so new aggregation
  1420. * sessions can be established after a resume.
  1421. *
  1422. * Also tear down aggregation sessions since reconfiguring
  1423. * them in a hardware restart scenario is not easily done
  1424. * right now, and the hardware will have lost information
  1425. * about the sessions, but we and the AP still think they
  1426. * are active. This is really a workaround though.
  1427. */
  1428. if (hw->flags & IEEE80211_HW_AMPDU_AGGREGATION) {
  1429. mutex_lock(&local->sta_mtx);
  1430. list_for_each_entry(sta, &local->sta_list, list) {
  1431. ieee80211_sta_tear_down_BA_sessions(
  1432. sta, AGG_STOP_LOCAL_REQUEST);
  1433. clear_sta_flag(sta, WLAN_STA_BLOCK_BA);
  1434. }
  1435. mutex_unlock(&local->sta_mtx);
  1436. }
  1437. ieee80211_wake_queues_by_reason(hw,
  1438. IEEE80211_QUEUE_STOP_REASON_SUSPEND);
  1439. /*
  1440. * If this is for hw restart things are still running.
  1441. * We may want to change that later, however.
  1442. */
  1443. if (!local->suspended || reconfig_due_to_wowlan)
  1444. drv_restart_complete(local);
  1445. if (!local->suspended)
  1446. return 0;
  1447. #ifdef CONFIG_PM
  1448. /* first set suspended false, then resuming */
  1449. local->suspended = false;
  1450. mb();
  1451. local->resuming = false;
  1452. mod_timer(&local->sta_cleanup, jiffies + 1);
  1453. #else
  1454. WARN_ON(1);
  1455. #endif
  1456. return 0;
  1457. }
  1458. void ieee80211_resume_disconnect(struct ieee80211_vif *vif)
  1459. {
  1460. struct ieee80211_sub_if_data *sdata;
  1461. struct ieee80211_local *local;
  1462. struct ieee80211_key *key;
  1463. if (WARN_ON(!vif))
  1464. return;
  1465. sdata = vif_to_sdata(vif);
  1466. local = sdata->local;
  1467. if (WARN_ON(!local->resuming))
  1468. return;
  1469. if (WARN_ON(vif->type != NL80211_IFTYPE_STATION))
  1470. return;
  1471. sdata->flags |= IEEE80211_SDATA_DISCONNECT_RESUME;
  1472. mutex_lock(&local->key_mtx);
  1473. list_for_each_entry(key, &sdata->key_list, list)
  1474. key->flags |= KEY_FLAG_TAINTED;
  1475. mutex_unlock(&local->key_mtx);
  1476. }
  1477. EXPORT_SYMBOL_GPL(ieee80211_resume_disconnect);
  1478. void ieee80211_recalc_smps(struct ieee80211_sub_if_data *sdata)
  1479. {
  1480. struct ieee80211_local *local = sdata->local;
  1481. struct ieee80211_chanctx_conf *chanctx_conf;
  1482. struct ieee80211_chanctx *chanctx;
  1483. mutex_lock(&local->chanctx_mtx);
  1484. chanctx_conf = rcu_dereference_protected(sdata->vif.chanctx_conf,
  1485. lockdep_is_held(&local->chanctx_mtx));
  1486. if (WARN_ON_ONCE(!chanctx_conf))
  1487. goto unlock;
  1488. chanctx = container_of(chanctx_conf, struct ieee80211_chanctx, conf);
  1489. ieee80211_recalc_smps_chanctx(local, chanctx);
  1490. unlock:
  1491. mutex_unlock(&local->chanctx_mtx);
  1492. }
  1493. static bool ieee80211_id_in_list(const u8 *ids, int n_ids, u8 id)
  1494. {
  1495. int i;
  1496. for (i = 0; i < n_ids; i++)
  1497. if (ids[i] == id)
  1498. return true;
  1499. return false;
  1500. }
  1501. /**
  1502. * ieee80211_ie_split - split an IE buffer according to ordering
  1503. *
  1504. * @ies: the IE buffer
  1505. * @ielen: the length of the IE buffer
  1506. * @ids: an array with element IDs that are allowed before
  1507. * the split
  1508. * @n_ids: the size of the element ID array
  1509. * @offset: offset where to start splitting in the buffer
  1510. *
  1511. * This function splits an IE buffer by updating the @offset
  1512. * variable to point to the location where the buffer should be
  1513. * split.
  1514. *
  1515. * It assumes that the given IE buffer is well-formed, this
  1516. * has to be guaranteed by the caller!
  1517. *
  1518. * It also assumes that the IEs in the buffer are ordered
  1519. * correctly, if not the result of using this function will not
  1520. * be ordered correctly either, i.e. it does no reordering.
  1521. *
  1522. * The function returns the offset where the next part of the
  1523. * buffer starts, which may be @ielen if the entire (remainder)
  1524. * of the buffer should be used.
  1525. */
  1526. size_t ieee80211_ie_split(const u8 *ies, size_t ielen,
  1527. const u8 *ids, int n_ids, size_t offset)
  1528. {
  1529. size_t pos = offset;
  1530. while (pos < ielen && ieee80211_id_in_list(ids, n_ids, ies[pos]))
  1531. pos += 2 + ies[pos + 1];
  1532. return pos;
  1533. }
  1534. size_t ieee80211_ie_split_vendor(const u8 *ies, size_t ielen, size_t offset)
  1535. {
  1536. size_t pos = offset;
  1537. while (pos < ielen && ies[pos] != WLAN_EID_VENDOR_SPECIFIC)
  1538. pos += 2 + ies[pos + 1];
  1539. return pos;
  1540. }
  1541. static void _ieee80211_enable_rssi_reports(struct ieee80211_sub_if_data *sdata,
  1542. int rssi_min_thold,
  1543. int rssi_max_thold)
  1544. {
  1545. trace_api_enable_rssi_reports(sdata, rssi_min_thold, rssi_max_thold);
  1546. if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_STATION))
  1547. return;
  1548. /*
  1549. * Scale up threshold values before storing it, as the RSSI averaging
  1550. * algorithm uses a scaled up value as well. Change this scaling
  1551. * factor if the RSSI averaging algorithm changes.
  1552. */
  1553. sdata->u.mgd.rssi_min_thold = rssi_min_thold*16;
  1554. sdata->u.mgd.rssi_max_thold = rssi_max_thold*16;
  1555. }
  1556. void ieee80211_enable_rssi_reports(struct ieee80211_vif *vif,
  1557. int rssi_min_thold,
  1558. int rssi_max_thold)
  1559. {
  1560. struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
  1561. WARN_ON(rssi_min_thold == rssi_max_thold ||
  1562. rssi_min_thold > rssi_max_thold);
  1563. _ieee80211_enable_rssi_reports(sdata, rssi_min_thold,
  1564. rssi_max_thold);
  1565. }
  1566. EXPORT_SYMBOL(ieee80211_enable_rssi_reports);
  1567. void ieee80211_disable_rssi_reports(struct ieee80211_vif *vif)
  1568. {
  1569. struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
  1570. _ieee80211_enable_rssi_reports(sdata, 0, 0);
  1571. }
  1572. EXPORT_SYMBOL(ieee80211_disable_rssi_reports);
  1573. u8 *ieee80211_ie_build_ht_cap(u8 *pos, struct ieee80211_sta_ht_cap *ht_cap,
  1574. u16 cap)
  1575. {
  1576. __le16 tmp;
  1577. *pos++ = WLAN_EID_HT_CAPABILITY;
  1578. *pos++ = sizeof(struct ieee80211_ht_cap);
  1579. memset(pos, 0, sizeof(struct ieee80211_ht_cap));
  1580. /* capability flags */
  1581. tmp = cpu_to_le16(cap);
  1582. memcpy(pos, &tmp, sizeof(u16));
  1583. pos += sizeof(u16);
  1584. /* AMPDU parameters */
  1585. *pos++ = ht_cap->ampdu_factor |
  1586. (ht_cap->ampdu_density <<
  1587. IEEE80211_HT_AMPDU_PARM_DENSITY_SHIFT);
  1588. /* MCS set */
  1589. memcpy(pos, &ht_cap->mcs, sizeof(ht_cap->mcs));
  1590. pos += sizeof(ht_cap->mcs);
  1591. /* extended capabilities */
  1592. pos += sizeof(__le16);
  1593. /* BF capabilities */
  1594. pos += sizeof(__le32);
  1595. /* antenna selection */
  1596. pos += sizeof(u8);
  1597. return pos;
  1598. }
  1599. u8 *ieee80211_ie_build_vht_cap(u8 *pos, struct ieee80211_sta_vht_cap *vht_cap,
  1600. u32 cap)
  1601. {
  1602. __le32 tmp;
  1603. *pos++ = WLAN_EID_VHT_CAPABILITY;
  1604. *pos++ = sizeof(struct ieee80211_vht_cap);
  1605. memset(pos, 0, sizeof(struct ieee80211_vht_cap));
  1606. /* capability flags */
  1607. tmp = cpu_to_le32(cap);
  1608. memcpy(pos, &tmp, sizeof(u32));
  1609. pos += sizeof(u32);
  1610. /* VHT MCS set */
  1611. memcpy(pos, &vht_cap->vht_mcs, sizeof(vht_cap->vht_mcs));
  1612. pos += sizeof(vht_cap->vht_mcs);
  1613. return pos;
  1614. }
  1615. u8 *ieee80211_ie_build_ht_oper(u8 *pos, struct ieee80211_sta_ht_cap *ht_cap,
  1616. const struct cfg80211_chan_def *chandef,
  1617. u16 prot_mode)
  1618. {
  1619. struct ieee80211_ht_operation *ht_oper;
  1620. /* Build HT Information */
  1621. *pos++ = WLAN_EID_HT_OPERATION;
  1622. *pos++ = sizeof(struct ieee80211_ht_operation);
  1623. ht_oper = (struct ieee80211_ht_operation *)pos;
  1624. ht_oper->primary_chan = ieee80211_frequency_to_channel(
  1625. chandef->chan->center_freq);
  1626. switch (chandef->width) {
  1627. case NL80211_CHAN_WIDTH_160:
  1628. case NL80211_CHAN_WIDTH_80P80:
  1629. case NL80211_CHAN_WIDTH_80:
  1630. case NL80211_CHAN_WIDTH_40:
  1631. if (chandef->center_freq1 > chandef->chan->center_freq)
  1632. ht_oper->ht_param = IEEE80211_HT_PARAM_CHA_SEC_ABOVE;
  1633. else
  1634. ht_oper->ht_param = IEEE80211_HT_PARAM_CHA_SEC_BELOW;
  1635. break;
  1636. default:
  1637. ht_oper->ht_param = IEEE80211_HT_PARAM_CHA_SEC_NONE;
  1638. break;
  1639. }
  1640. if (ht_cap->cap & IEEE80211_HT_CAP_SUP_WIDTH_20_40 &&
  1641. chandef->width != NL80211_CHAN_WIDTH_20_NOHT &&
  1642. chandef->width != NL80211_CHAN_WIDTH_20)
  1643. ht_oper->ht_param |= IEEE80211_HT_PARAM_CHAN_WIDTH_ANY;
  1644. ht_oper->operation_mode = cpu_to_le16(prot_mode);
  1645. ht_oper->stbc_param = 0x0000;
  1646. /* It seems that Basic MCS set and Supported MCS set
  1647. are identical for the first 10 bytes */
  1648. memset(&ht_oper->basic_set, 0, 16);
  1649. memcpy(&ht_oper->basic_set, &ht_cap->mcs, 10);
  1650. return pos + sizeof(struct ieee80211_ht_operation);
  1651. }
  1652. void ieee80211_ht_oper_to_chandef(struct ieee80211_channel *control_chan,
  1653. const struct ieee80211_ht_operation *ht_oper,
  1654. struct cfg80211_chan_def *chandef)
  1655. {
  1656. enum nl80211_channel_type channel_type;
  1657. if (!ht_oper) {
  1658. cfg80211_chandef_create(chandef, control_chan,
  1659. NL80211_CHAN_NO_HT);
  1660. return;
  1661. }
  1662. switch (ht_oper->ht_param & IEEE80211_HT_PARAM_CHA_SEC_OFFSET) {
  1663. case IEEE80211_HT_PARAM_CHA_SEC_NONE:
  1664. channel_type = NL80211_CHAN_HT20;
  1665. break;
  1666. case IEEE80211_HT_PARAM_CHA_SEC_ABOVE:
  1667. channel_type = NL80211_CHAN_HT40PLUS;
  1668. break;
  1669. case IEEE80211_HT_PARAM_CHA_SEC_BELOW:
  1670. channel_type = NL80211_CHAN_HT40MINUS;
  1671. break;
  1672. default:
  1673. channel_type = NL80211_CHAN_NO_HT;
  1674. }
  1675. cfg80211_chandef_create(chandef, control_chan, channel_type);
  1676. }
  1677. int ieee80211_add_srates_ie(struct ieee80211_sub_if_data *sdata,
  1678. struct sk_buff *skb, bool need_basic,
  1679. enum ieee80211_band band)
  1680. {
  1681. struct ieee80211_local *local = sdata->local;
  1682. struct ieee80211_supported_band *sband;
  1683. int rate;
  1684. u8 i, rates, *pos;
  1685. u32 basic_rates = sdata->vif.bss_conf.basic_rates;
  1686. sband = local->hw.wiphy->bands[band];
  1687. rates = sband->n_bitrates;
  1688. if (rates > 8)
  1689. rates = 8;
  1690. if (skb_tailroom(skb) < rates + 2)
  1691. return -ENOMEM;
  1692. pos = skb_put(skb, rates + 2);
  1693. *pos++ = WLAN_EID_SUPP_RATES;
  1694. *pos++ = rates;
  1695. for (i = 0; i < rates; i++) {
  1696. u8 basic = 0;
  1697. if (need_basic && basic_rates & BIT(i))
  1698. basic = 0x80;
  1699. rate = sband->bitrates[i].bitrate;
  1700. *pos++ = basic | (u8) (rate / 5);
  1701. }
  1702. return 0;
  1703. }
  1704. int ieee80211_add_ext_srates_ie(struct ieee80211_sub_if_data *sdata,
  1705. struct sk_buff *skb, bool need_basic,
  1706. enum ieee80211_band band)
  1707. {
  1708. struct ieee80211_local *local = sdata->local;
  1709. struct ieee80211_supported_band *sband;
  1710. int rate;
  1711. u8 i, exrates, *pos;
  1712. u32 basic_rates = sdata->vif.bss_conf.basic_rates;
  1713. sband = local->hw.wiphy->bands[band];
  1714. exrates = sband->n_bitrates;
  1715. if (exrates > 8)
  1716. exrates -= 8;
  1717. else
  1718. exrates = 0;
  1719. if (skb_tailroom(skb) < exrates + 2)
  1720. return -ENOMEM;
  1721. if (exrates) {
  1722. pos = skb_put(skb, exrates + 2);
  1723. *pos++ = WLAN_EID_EXT_SUPP_RATES;
  1724. *pos++ = exrates;
  1725. for (i = 8; i < sband->n_bitrates; i++) {
  1726. u8 basic = 0;
  1727. if (need_basic && basic_rates & BIT(i))
  1728. basic = 0x80;
  1729. rate = sband->bitrates[i].bitrate;
  1730. *pos++ = basic | (u8) (rate / 5);
  1731. }
  1732. }
  1733. return 0;
  1734. }
  1735. int ieee80211_ave_rssi(struct ieee80211_vif *vif)
  1736. {
  1737. struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
  1738. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  1739. if (WARN_ON_ONCE(sdata->vif.type != NL80211_IFTYPE_STATION)) {
  1740. /* non-managed type inferfaces */
  1741. return 0;
  1742. }
  1743. return ifmgd->ave_beacon_signal;
  1744. }
  1745. EXPORT_SYMBOL_GPL(ieee80211_ave_rssi);
  1746. u8 ieee80211_mcs_to_chains(const struct ieee80211_mcs_info *mcs)
  1747. {
  1748. if (!mcs)
  1749. return 1;
  1750. /* TODO: consider rx_highest */
  1751. if (mcs->rx_mask[3])
  1752. return 4;
  1753. if (mcs->rx_mask[2])
  1754. return 3;
  1755. if (mcs->rx_mask[1])
  1756. return 2;
  1757. return 1;
  1758. }
  1759. /**
  1760. * ieee80211_calculate_rx_timestamp - calculate timestamp in frame
  1761. * @local: mac80211 hw info struct
  1762. * @status: RX status
  1763. * @mpdu_len: total MPDU length (including FCS)
  1764. * @mpdu_offset: offset into MPDU to calculate timestamp at
  1765. *
  1766. * This function calculates the RX timestamp at the given MPDU offset, taking
  1767. * into account what the RX timestamp was. An offset of 0 will just normalize
  1768. * the timestamp to TSF at beginning of MPDU reception.
  1769. */
  1770. u64 ieee80211_calculate_rx_timestamp(struct ieee80211_local *local,
  1771. struct ieee80211_rx_status *status,
  1772. unsigned int mpdu_len,
  1773. unsigned int mpdu_offset)
  1774. {
  1775. u64 ts = status->mactime;
  1776. struct rate_info ri;
  1777. u16 rate;
  1778. if (WARN_ON(!ieee80211_have_rx_timestamp(status)))
  1779. return 0;
  1780. memset(&ri, 0, sizeof(ri));
  1781. /* Fill cfg80211 rate info */
  1782. if (status->flag & RX_FLAG_HT) {
  1783. ri.mcs = status->rate_idx;
  1784. ri.flags |= RATE_INFO_FLAGS_MCS;
  1785. if (status->flag & RX_FLAG_40MHZ)
  1786. ri.flags |= RATE_INFO_FLAGS_40_MHZ_WIDTH;
  1787. if (status->flag & RX_FLAG_SHORT_GI)
  1788. ri.flags |= RATE_INFO_FLAGS_SHORT_GI;
  1789. } else if (status->flag & RX_FLAG_VHT) {
  1790. ri.flags |= RATE_INFO_FLAGS_VHT_MCS;
  1791. ri.mcs = status->rate_idx;
  1792. ri.nss = status->vht_nss;
  1793. if (status->flag & RX_FLAG_40MHZ)
  1794. ri.flags |= RATE_INFO_FLAGS_40_MHZ_WIDTH;
  1795. if (status->flag & RX_FLAG_80MHZ)
  1796. ri.flags |= RATE_INFO_FLAGS_80_MHZ_WIDTH;
  1797. if (status->flag & RX_FLAG_80P80MHZ)
  1798. ri.flags |= RATE_INFO_FLAGS_80P80_MHZ_WIDTH;
  1799. if (status->flag & RX_FLAG_160MHZ)
  1800. ri.flags |= RATE_INFO_FLAGS_160_MHZ_WIDTH;
  1801. if (status->flag & RX_FLAG_SHORT_GI)
  1802. ri.flags |= RATE_INFO_FLAGS_SHORT_GI;
  1803. } else {
  1804. struct ieee80211_supported_band *sband;
  1805. sband = local->hw.wiphy->bands[status->band];
  1806. ri.legacy = sband->bitrates[status->rate_idx].bitrate;
  1807. }
  1808. rate = cfg80211_calculate_bitrate(&ri);
  1809. /* rewind from end of MPDU */
  1810. if (status->flag & RX_FLAG_MACTIME_END)
  1811. ts -= mpdu_len * 8 * 10 / rate;
  1812. ts += mpdu_offset * 8 * 10 / rate;
  1813. return ts;
  1814. }
  1815. void ieee80211_dfs_cac_cancel(struct ieee80211_local *local)
  1816. {
  1817. struct ieee80211_sub_if_data *sdata;
  1818. mutex_lock(&local->iflist_mtx);
  1819. list_for_each_entry(sdata, &local->interfaces, list) {
  1820. cancel_delayed_work_sync(&sdata->dfs_cac_timer_work);
  1821. if (sdata->wdev.cac_started) {
  1822. ieee80211_vif_release_channel(sdata);
  1823. cfg80211_cac_event(sdata->dev,
  1824. NL80211_RADAR_CAC_ABORTED,
  1825. GFP_KERNEL);
  1826. }
  1827. }
  1828. mutex_unlock(&local->iflist_mtx);
  1829. }
  1830. void ieee80211_dfs_radar_detected_work(struct work_struct *work)
  1831. {
  1832. struct ieee80211_local *local =
  1833. container_of(work, struct ieee80211_local, radar_detected_work);
  1834. struct cfg80211_chan_def chandef;
  1835. ieee80211_dfs_cac_cancel(local);
  1836. if (local->use_chanctx)
  1837. /* currently not handled */
  1838. WARN_ON(1);
  1839. else {
  1840. cfg80211_chandef_create(&chandef, local->hw.conf.channel,
  1841. local->hw.conf.channel_type);
  1842. cfg80211_radar_event(local->hw.wiphy, &chandef, GFP_KERNEL);
  1843. }
  1844. }
  1845. void ieee80211_radar_detected(struct ieee80211_hw *hw)
  1846. {
  1847. struct ieee80211_local *local = hw_to_local(hw);
  1848. trace_api_radar_detected(local);
  1849. ieee80211_queue_work(hw, &local->radar_detected_work);
  1850. }
  1851. EXPORT_SYMBOL(ieee80211_radar_detected);