util.c 91 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517
  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. * Copyright 2013-2014 Intel Mobile Communications GmbH
  7. * Copyright (C) 2015-2017 Intel Deutschland GmbH
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2 as
  11. * published by the Free Software Foundation.
  12. *
  13. * utilities for mac80211
  14. */
  15. #include <net/mac80211.h>
  16. #include <linux/netdevice.h>
  17. #include <linux/export.h>
  18. #include <linux/types.h>
  19. #include <linux/slab.h>
  20. #include <linux/skbuff.h>
  21. #include <linux/etherdevice.h>
  22. #include <linux/if_arp.h>
  23. #include <linux/bitmap.h>
  24. #include <linux/crc32.h>
  25. #include <net/net_namespace.h>
  26. #include <net/cfg80211.h>
  27. #include <net/rtnetlink.h>
  28. #include "ieee80211_i.h"
  29. #include "driver-ops.h"
  30. #include "rate.h"
  31. #include "mesh.h"
  32. #include "wme.h"
  33. #include "led.h"
  34. #include "wep.h"
  35. /* privid for wiphys to determine whether they belong to us or not */
  36. const void *const mac80211_wiphy_privid = &mac80211_wiphy_privid;
  37. struct ieee80211_hw *wiphy_to_ieee80211_hw(struct wiphy *wiphy)
  38. {
  39. struct ieee80211_local *local;
  40. BUG_ON(!wiphy);
  41. local = wiphy_priv(wiphy);
  42. return &local->hw;
  43. }
  44. EXPORT_SYMBOL(wiphy_to_ieee80211_hw);
  45. void ieee80211_tx_set_protected(struct ieee80211_tx_data *tx)
  46. {
  47. struct sk_buff *skb;
  48. struct ieee80211_hdr *hdr;
  49. skb_queue_walk(&tx->skbs, skb) {
  50. hdr = (struct ieee80211_hdr *) skb->data;
  51. hdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_PROTECTED);
  52. }
  53. }
  54. int ieee80211_frame_duration(enum nl80211_band band, size_t len,
  55. int rate, int erp, int short_preamble,
  56. int shift)
  57. {
  58. int dur;
  59. /* calculate duration (in microseconds, rounded up to next higher
  60. * integer if it includes a fractional microsecond) to send frame of
  61. * len bytes (does not include FCS) at the given rate. Duration will
  62. * also include SIFS.
  63. *
  64. * rate is in 100 kbps, so divident is multiplied by 10 in the
  65. * DIV_ROUND_UP() operations.
  66. *
  67. * shift may be 2 for 5 MHz channels or 1 for 10 MHz channels, and
  68. * is assumed to be 0 otherwise.
  69. */
  70. if (band == NL80211_BAND_5GHZ || erp) {
  71. /*
  72. * OFDM:
  73. *
  74. * N_DBPS = DATARATE x 4
  75. * N_SYM = Ceiling((16+8xLENGTH+6) / N_DBPS)
  76. * (16 = SIGNAL time, 6 = tail bits)
  77. * TXTIME = T_PREAMBLE + T_SIGNAL + T_SYM x N_SYM + Signal Ext
  78. *
  79. * T_SYM = 4 usec
  80. * 802.11a - 18.5.2: aSIFSTime = 16 usec
  81. * 802.11g - 19.8.4: aSIFSTime = 10 usec +
  82. * signal ext = 6 usec
  83. */
  84. dur = 16; /* SIFS + signal ext */
  85. dur += 16; /* IEEE 802.11-2012 18.3.2.4: T_PREAMBLE = 16 usec */
  86. dur += 4; /* IEEE 802.11-2012 18.3.2.4: T_SIGNAL = 4 usec */
  87. /* IEEE 802.11-2012 18.3.2.4: all values above are:
  88. * * times 4 for 5 MHz
  89. * * times 2 for 10 MHz
  90. */
  91. dur *= 1 << shift;
  92. /* rates should already consider the channel bandwidth,
  93. * don't apply divisor again.
  94. */
  95. dur += 4 * DIV_ROUND_UP((16 + 8 * (len + 4) + 6) * 10,
  96. 4 * rate); /* T_SYM x N_SYM */
  97. } else {
  98. /*
  99. * 802.11b or 802.11g with 802.11b compatibility:
  100. * 18.3.4: TXTIME = PreambleLength + PLCPHeaderTime +
  101. * Ceiling(((LENGTH+PBCC)x8)/DATARATE). PBCC=0.
  102. *
  103. * 802.11 (DS): 15.3.3, 802.11b: 18.3.4
  104. * aSIFSTime = 10 usec
  105. * aPreambleLength = 144 usec or 72 usec with short preamble
  106. * aPLCPHeaderLength = 48 usec or 24 usec with short preamble
  107. */
  108. dur = 10; /* aSIFSTime = 10 usec */
  109. dur += short_preamble ? (72 + 24) : (144 + 48);
  110. dur += DIV_ROUND_UP(8 * (len + 4) * 10, rate);
  111. }
  112. return dur;
  113. }
  114. /* Exported duration function for driver use */
  115. __le16 ieee80211_generic_frame_duration(struct ieee80211_hw *hw,
  116. struct ieee80211_vif *vif,
  117. enum nl80211_band band,
  118. size_t frame_len,
  119. struct ieee80211_rate *rate)
  120. {
  121. struct ieee80211_sub_if_data *sdata;
  122. u16 dur;
  123. int erp, shift = 0;
  124. bool short_preamble = false;
  125. erp = 0;
  126. if (vif) {
  127. sdata = vif_to_sdata(vif);
  128. short_preamble = sdata->vif.bss_conf.use_short_preamble;
  129. if (sdata->flags & IEEE80211_SDATA_OPERATING_GMODE)
  130. erp = rate->flags & IEEE80211_RATE_ERP_G;
  131. shift = ieee80211_vif_get_shift(vif);
  132. }
  133. dur = ieee80211_frame_duration(band, frame_len, rate->bitrate, erp,
  134. short_preamble, shift);
  135. return cpu_to_le16(dur);
  136. }
  137. EXPORT_SYMBOL(ieee80211_generic_frame_duration);
  138. __le16 ieee80211_rts_duration(struct ieee80211_hw *hw,
  139. struct ieee80211_vif *vif, size_t frame_len,
  140. const struct ieee80211_tx_info *frame_txctl)
  141. {
  142. struct ieee80211_local *local = hw_to_local(hw);
  143. struct ieee80211_rate *rate;
  144. struct ieee80211_sub_if_data *sdata;
  145. bool short_preamble;
  146. int erp, shift = 0, bitrate;
  147. u16 dur;
  148. struct ieee80211_supported_band *sband;
  149. sband = local->hw.wiphy->bands[frame_txctl->band];
  150. short_preamble = false;
  151. rate = &sband->bitrates[frame_txctl->control.rts_cts_rate_idx];
  152. erp = 0;
  153. if (vif) {
  154. sdata = vif_to_sdata(vif);
  155. short_preamble = sdata->vif.bss_conf.use_short_preamble;
  156. if (sdata->flags & IEEE80211_SDATA_OPERATING_GMODE)
  157. erp = rate->flags & IEEE80211_RATE_ERP_G;
  158. shift = ieee80211_vif_get_shift(vif);
  159. }
  160. bitrate = DIV_ROUND_UP(rate->bitrate, 1 << shift);
  161. /* CTS duration */
  162. dur = ieee80211_frame_duration(sband->band, 10, bitrate,
  163. erp, short_preamble, shift);
  164. /* Data frame duration */
  165. dur += ieee80211_frame_duration(sband->band, frame_len, bitrate,
  166. erp, short_preamble, shift);
  167. /* ACK duration */
  168. dur += ieee80211_frame_duration(sband->band, 10, bitrate,
  169. erp, short_preamble, shift);
  170. return cpu_to_le16(dur);
  171. }
  172. EXPORT_SYMBOL(ieee80211_rts_duration);
  173. __le16 ieee80211_ctstoself_duration(struct ieee80211_hw *hw,
  174. struct ieee80211_vif *vif,
  175. size_t frame_len,
  176. const struct ieee80211_tx_info *frame_txctl)
  177. {
  178. struct ieee80211_local *local = hw_to_local(hw);
  179. struct ieee80211_rate *rate;
  180. struct ieee80211_sub_if_data *sdata;
  181. bool short_preamble;
  182. int erp, shift = 0, bitrate;
  183. u16 dur;
  184. struct ieee80211_supported_band *sband;
  185. sband = local->hw.wiphy->bands[frame_txctl->band];
  186. short_preamble = false;
  187. rate = &sband->bitrates[frame_txctl->control.rts_cts_rate_idx];
  188. erp = 0;
  189. if (vif) {
  190. sdata = vif_to_sdata(vif);
  191. short_preamble = sdata->vif.bss_conf.use_short_preamble;
  192. if (sdata->flags & IEEE80211_SDATA_OPERATING_GMODE)
  193. erp = rate->flags & IEEE80211_RATE_ERP_G;
  194. shift = ieee80211_vif_get_shift(vif);
  195. }
  196. bitrate = DIV_ROUND_UP(rate->bitrate, 1 << shift);
  197. /* Data frame duration */
  198. dur = ieee80211_frame_duration(sband->band, frame_len, bitrate,
  199. erp, short_preamble, shift);
  200. if (!(frame_txctl->flags & IEEE80211_TX_CTL_NO_ACK)) {
  201. /* ACK duration */
  202. dur += ieee80211_frame_duration(sband->band, 10, bitrate,
  203. erp, short_preamble, shift);
  204. }
  205. return cpu_to_le16(dur);
  206. }
  207. EXPORT_SYMBOL(ieee80211_ctstoself_duration);
  208. void ieee80211_propagate_queue_wake(struct ieee80211_local *local, int queue)
  209. {
  210. struct ieee80211_sub_if_data *sdata;
  211. int n_acs = IEEE80211_NUM_ACS;
  212. if (local->ops->wake_tx_queue)
  213. return;
  214. if (local->hw.queues < IEEE80211_NUM_ACS)
  215. n_acs = 1;
  216. list_for_each_entry_rcu(sdata, &local->interfaces, list) {
  217. int ac;
  218. if (!sdata->dev)
  219. continue;
  220. if (sdata->vif.cab_queue != IEEE80211_INVAL_HW_QUEUE &&
  221. local->queue_stop_reasons[sdata->vif.cab_queue] != 0)
  222. continue;
  223. for (ac = 0; ac < n_acs; ac++) {
  224. int ac_queue = sdata->vif.hw_queue[ac];
  225. if (ac_queue == queue ||
  226. (sdata->vif.cab_queue == queue &&
  227. local->queue_stop_reasons[ac_queue] == 0 &&
  228. skb_queue_empty(&local->pending[ac_queue])))
  229. netif_wake_subqueue(sdata->dev, ac);
  230. }
  231. }
  232. }
  233. static void __ieee80211_wake_queue(struct ieee80211_hw *hw, int queue,
  234. enum queue_stop_reason reason,
  235. bool refcounted)
  236. {
  237. struct ieee80211_local *local = hw_to_local(hw);
  238. trace_wake_queue(local, queue, reason);
  239. if (WARN_ON(queue >= hw->queues))
  240. return;
  241. if (!test_bit(reason, &local->queue_stop_reasons[queue]))
  242. return;
  243. if (!refcounted) {
  244. local->q_stop_reasons[queue][reason] = 0;
  245. } else {
  246. local->q_stop_reasons[queue][reason]--;
  247. if (WARN_ON(local->q_stop_reasons[queue][reason] < 0))
  248. local->q_stop_reasons[queue][reason] = 0;
  249. }
  250. if (local->q_stop_reasons[queue][reason] == 0)
  251. __clear_bit(reason, &local->queue_stop_reasons[queue]);
  252. if (local->queue_stop_reasons[queue] != 0)
  253. /* someone still has this queue stopped */
  254. return;
  255. if (skb_queue_empty(&local->pending[queue])) {
  256. rcu_read_lock();
  257. ieee80211_propagate_queue_wake(local, queue);
  258. rcu_read_unlock();
  259. } else
  260. tasklet_schedule(&local->tx_pending_tasklet);
  261. }
  262. void ieee80211_wake_queue_by_reason(struct ieee80211_hw *hw, int queue,
  263. enum queue_stop_reason reason,
  264. bool refcounted)
  265. {
  266. struct ieee80211_local *local = hw_to_local(hw);
  267. unsigned long flags;
  268. spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
  269. __ieee80211_wake_queue(hw, queue, reason, refcounted);
  270. spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
  271. }
  272. void ieee80211_wake_queue(struct ieee80211_hw *hw, int queue)
  273. {
  274. ieee80211_wake_queue_by_reason(hw, queue,
  275. IEEE80211_QUEUE_STOP_REASON_DRIVER,
  276. false);
  277. }
  278. EXPORT_SYMBOL(ieee80211_wake_queue);
  279. static void __ieee80211_stop_queue(struct ieee80211_hw *hw, int queue,
  280. enum queue_stop_reason reason,
  281. bool refcounted)
  282. {
  283. struct ieee80211_local *local = hw_to_local(hw);
  284. struct ieee80211_sub_if_data *sdata;
  285. int n_acs = IEEE80211_NUM_ACS;
  286. trace_stop_queue(local, queue, reason);
  287. if (WARN_ON(queue >= hw->queues))
  288. return;
  289. if (!refcounted)
  290. local->q_stop_reasons[queue][reason] = 1;
  291. else
  292. local->q_stop_reasons[queue][reason]++;
  293. if (__test_and_set_bit(reason, &local->queue_stop_reasons[queue]))
  294. return;
  295. if (local->ops->wake_tx_queue)
  296. return;
  297. if (local->hw.queues < IEEE80211_NUM_ACS)
  298. n_acs = 1;
  299. rcu_read_lock();
  300. list_for_each_entry_rcu(sdata, &local->interfaces, list) {
  301. int ac;
  302. if (!sdata->dev)
  303. continue;
  304. for (ac = 0; ac < n_acs; ac++) {
  305. if (sdata->vif.hw_queue[ac] == queue ||
  306. sdata->vif.cab_queue == queue)
  307. netif_stop_subqueue(sdata->dev, ac);
  308. }
  309. }
  310. rcu_read_unlock();
  311. }
  312. void ieee80211_stop_queue_by_reason(struct ieee80211_hw *hw, int queue,
  313. enum queue_stop_reason reason,
  314. bool refcounted)
  315. {
  316. struct ieee80211_local *local = hw_to_local(hw);
  317. unsigned long flags;
  318. spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
  319. __ieee80211_stop_queue(hw, queue, reason, refcounted);
  320. spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
  321. }
  322. void ieee80211_stop_queue(struct ieee80211_hw *hw, int queue)
  323. {
  324. ieee80211_stop_queue_by_reason(hw, queue,
  325. IEEE80211_QUEUE_STOP_REASON_DRIVER,
  326. false);
  327. }
  328. EXPORT_SYMBOL(ieee80211_stop_queue);
  329. void ieee80211_add_pending_skb(struct ieee80211_local *local,
  330. struct sk_buff *skb)
  331. {
  332. struct ieee80211_hw *hw = &local->hw;
  333. unsigned long flags;
  334. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  335. int queue = info->hw_queue;
  336. if (WARN_ON(!info->control.vif)) {
  337. ieee80211_free_txskb(&local->hw, skb);
  338. return;
  339. }
  340. spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
  341. __ieee80211_stop_queue(hw, queue, IEEE80211_QUEUE_STOP_REASON_SKB_ADD,
  342. false);
  343. __skb_queue_tail(&local->pending[queue], skb);
  344. __ieee80211_wake_queue(hw, queue, IEEE80211_QUEUE_STOP_REASON_SKB_ADD,
  345. false);
  346. spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
  347. }
  348. void ieee80211_add_pending_skbs(struct ieee80211_local *local,
  349. struct sk_buff_head *skbs)
  350. {
  351. struct ieee80211_hw *hw = &local->hw;
  352. struct sk_buff *skb;
  353. unsigned long flags;
  354. int queue, i;
  355. spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
  356. while ((skb = skb_dequeue(skbs))) {
  357. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  358. if (WARN_ON(!info->control.vif)) {
  359. ieee80211_free_txskb(&local->hw, skb);
  360. continue;
  361. }
  362. queue = info->hw_queue;
  363. __ieee80211_stop_queue(hw, queue,
  364. IEEE80211_QUEUE_STOP_REASON_SKB_ADD,
  365. false);
  366. __skb_queue_tail(&local->pending[queue], skb);
  367. }
  368. for (i = 0; i < hw->queues; i++)
  369. __ieee80211_wake_queue(hw, i,
  370. IEEE80211_QUEUE_STOP_REASON_SKB_ADD,
  371. false);
  372. spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
  373. }
  374. void ieee80211_stop_queues_by_reason(struct ieee80211_hw *hw,
  375. unsigned long queues,
  376. enum queue_stop_reason reason,
  377. bool refcounted)
  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_each_set_bit(i, &queues, hw->queues)
  384. __ieee80211_stop_queue(hw, i, reason, refcounted);
  385. spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
  386. }
  387. void ieee80211_stop_queues(struct ieee80211_hw *hw)
  388. {
  389. ieee80211_stop_queues_by_reason(hw, IEEE80211_MAX_QUEUE_MAP,
  390. IEEE80211_QUEUE_STOP_REASON_DRIVER,
  391. false);
  392. }
  393. EXPORT_SYMBOL(ieee80211_stop_queues);
  394. int ieee80211_queue_stopped(struct ieee80211_hw *hw, int queue)
  395. {
  396. struct ieee80211_local *local = hw_to_local(hw);
  397. unsigned long flags;
  398. int ret;
  399. if (WARN_ON(queue >= hw->queues))
  400. return true;
  401. spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
  402. ret = test_bit(IEEE80211_QUEUE_STOP_REASON_DRIVER,
  403. &local->queue_stop_reasons[queue]);
  404. spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
  405. return ret;
  406. }
  407. EXPORT_SYMBOL(ieee80211_queue_stopped);
  408. void ieee80211_wake_queues_by_reason(struct ieee80211_hw *hw,
  409. unsigned long queues,
  410. enum queue_stop_reason reason,
  411. bool refcounted)
  412. {
  413. struct ieee80211_local *local = hw_to_local(hw);
  414. unsigned long flags;
  415. int i;
  416. spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
  417. for_each_set_bit(i, &queues, hw->queues)
  418. __ieee80211_wake_queue(hw, i, reason, refcounted);
  419. spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
  420. }
  421. void ieee80211_wake_queues(struct ieee80211_hw *hw)
  422. {
  423. ieee80211_wake_queues_by_reason(hw, IEEE80211_MAX_QUEUE_MAP,
  424. IEEE80211_QUEUE_STOP_REASON_DRIVER,
  425. false);
  426. }
  427. EXPORT_SYMBOL(ieee80211_wake_queues);
  428. static unsigned int
  429. ieee80211_get_vif_queues(struct ieee80211_local *local,
  430. struct ieee80211_sub_if_data *sdata)
  431. {
  432. unsigned int queues;
  433. if (sdata && ieee80211_hw_check(&local->hw, QUEUE_CONTROL)) {
  434. int ac;
  435. queues = 0;
  436. for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
  437. queues |= BIT(sdata->vif.hw_queue[ac]);
  438. if (sdata->vif.cab_queue != IEEE80211_INVAL_HW_QUEUE)
  439. queues |= BIT(sdata->vif.cab_queue);
  440. } else {
  441. /* all queues */
  442. queues = BIT(local->hw.queues) - 1;
  443. }
  444. return queues;
  445. }
  446. void __ieee80211_flush_queues(struct ieee80211_local *local,
  447. struct ieee80211_sub_if_data *sdata,
  448. unsigned int queues, bool drop)
  449. {
  450. if (!local->ops->flush)
  451. return;
  452. /*
  453. * If no queue was set, or if the HW doesn't support
  454. * IEEE80211_HW_QUEUE_CONTROL - flush all queues
  455. */
  456. if (!queues || !ieee80211_hw_check(&local->hw, QUEUE_CONTROL))
  457. queues = ieee80211_get_vif_queues(local, sdata);
  458. ieee80211_stop_queues_by_reason(&local->hw, queues,
  459. IEEE80211_QUEUE_STOP_REASON_FLUSH,
  460. false);
  461. drv_flush(local, sdata, queues, drop);
  462. ieee80211_wake_queues_by_reason(&local->hw, queues,
  463. IEEE80211_QUEUE_STOP_REASON_FLUSH,
  464. false);
  465. }
  466. void ieee80211_flush_queues(struct ieee80211_local *local,
  467. struct ieee80211_sub_if_data *sdata, bool drop)
  468. {
  469. __ieee80211_flush_queues(local, sdata, 0, drop);
  470. }
  471. void ieee80211_stop_vif_queues(struct ieee80211_local *local,
  472. struct ieee80211_sub_if_data *sdata,
  473. enum queue_stop_reason reason)
  474. {
  475. ieee80211_stop_queues_by_reason(&local->hw,
  476. ieee80211_get_vif_queues(local, sdata),
  477. reason, true);
  478. }
  479. void ieee80211_wake_vif_queues(struct ieee80211_local *local,
  480. struct ieee80211_sub_if_data *sdata,
  481. enum queue_stop_reason reason)
  482. {
  483. ieee80211_wake_queues_by_reason(&local->hw,
  484. ieee80211_get_vif_queues(local, sdata),
  485. reason, true);
  486. }
  487. static void __iterate_interfaces(struct ieee80211_local *local,
  488. u32 iter_flags,
  489. void (*iterator)(void *data, u8 *mac,
  490. struct ieee80211_vif *vif),
  491. void *data)
  492. {
  493. struct ieee80211_sub_if_data *sdata;
  494. bool active_only = iter_flags & IEEE80211_IFACE_ITER_ACTIVE;
  495. list_for_each_entry_rcu(sdata, &local->interfaces, list) {
  496. switch (sdata->vif.type) {
  497. case NL80211_IFTYPE_MONITOR:
  498. if (!(sdata->u.mntr.flags & MONITOR_FLAG_ACTIVE))
  499. continue;
  500. break;
  501. case NL80211_IFTYPE_AP_VLAN:
  502. continue;
  503. default:
  504. break;
  505. }
  506. if (!(iter_flags & IEEE80211_IFACE_ITER_RESUME_ALL) &&
  507. active_only && !(sdata->flags & IEEE80211_SDATA_IN_DRIVER))
  508. continue;
  509. if (ieee80211_sdata_running(sdata) || !active_only)
  510. iterator(data, sdata->vif.addr,
  511. &sdata->vif);
  512. }
  513. sdata = rcu_dereference_check(local->monitor_sdata,
  514. lockdep_is_held(&local->iflist_mtx) ||
  515. lockdep_rtnl_is_held());
  516. if (sdata &&
  517. (iter_flags & IEEE80211_IFACE_ITER_RESUME_ALL || !active_only ||
  518. sdata->flags & IEEE80211_SDATA_IN_DRIVER))
  519. iterator(data, sdata->vif.addr, &sdata->vif);
  520. }
  521. void ieee80211_iterate_interfaces(
  522. struct ieee80211_hw *hw, u32 iter_flags,
  523. void (*iterator)(void *data, u8 *mac,
  524. struct ieee80211_vif *vif),
  525. void *data)
  526. {
  527. struct ieee80211_local *local = hw_to_local(hw);
  528. mutex_lock(&local->iflist_mtx);
  529. __iterate_interfaces(local, iter_flags, iterator, data);
  530. mutex_unlock(&local->iflist_mtx);
  531. }
  532. EXPORT_SYMBOL_GPL(ieee80211_iterate_interfaces);
  533. void ieee80211_iterate_active_interfaces_atomic(
  534. struct ieee80211_hw *hw, u32 iter_flags,
  535. void (*iterator)(void *data, u8 *mac,
  536. struct ieee80211_vif *vif),
  537. void *data)
  538. {
  539. struct ieee80211_local *local = hw_to_local(hw);
  540. rcu_read_lock();
  541. __iterate_interfaces(local, iter_flags | IEEE80211_IFACE_ITER_ACTIVE,
  542. iterator, data);
  543. rcu_read_unlock();
  544. }
  545. EXPORT_SYMBOL_GPL(ieee80211_iterate_active_interfaces_atomic);
  546. void ieee80211_iterate_active_interfaces_rtnl(
  547. struct ieee80211_hw *hw, u32 iter_flags,
  548. void (*iterator)(void *data, u8 *mac,
  549. struct ieee80211_vif *vif),
  550. void *data)
  551. {
  552. struct ieee80211_local *local = hw_to_local(hw);
  553. ASSERT_RTNL();
  554. __iterate_interfaces(local, iter_flags | IEEE80211_IFACE_ITER_ACTIVE,
  555. iterator, data);
  556. }
  557. EXPORT_SYMBOL_GPL(ieee80211_iterate_active_interfaces_rtnl);
  558. static void __iterate_stations(struct ieee80211_local *local,
  559. void (*iterator)(void *data,
  560. struct ieee80211_sta *sta),
  561. void *data)
  562. {
  563. struct sta_info *sta;
  564. list_for_each_entry_rcu(sta, &local->sta_list, list) {
  565. if (!sta->uploaded)
  566. continue;
  567. iterator(data, &sta->sta);
  568. }
  569. }
  570. void ieee80211_iterate_stations_atomic(struct ieee80211_hw *hw,
  571. void (*iterator)(void *data,
  572. struct ieee80211_sta *sta),
  573. void *data)
  574. {
  575. struct ieee80211_local *local = hw_to_local(hw);
  576. rcu_read_lock();
  577. __iterate_stations(local, iterator, data);
  578. rcu_read_unlock();
  579. }
  580. EXPORT_SYMBOL_GPL(ieee80211_iterate_stations_atomic);
  581. struct ieee80211_vif *wdev_to_ieee80211_vif(struct wireless_dev *wdev)
  582. {
  583. struct ieee80211_sub_if_data *sdata = IEEE80211_WDEV_TO_SUB_IF(wdev);
  584. if (!ieee80211_sdata_running(sdata) ||
  585. !(sdata->flags & IEEE80211_SDATA_IN_DRIVER))
  586. return NULL;
  587. return &sdata->vif;
  588. }
  589. EXPORT_SYMBOL_GPL(wdev_to_ieee80211_vif);
  590. struct wireless_dev *ieee80211_vif_to_wdev(struct ieee80211_vif *vif)
  591. {
  592. struct ieee80211_sub_if_data *sdata;
  593. if (!vif)
  594. return NULL;
  595. sdata = vif_to_sdata(vif);
  596. if (!ieee80211_sdata_running(sdata) ||
  597. !(sdata->flags & IEEE80211_SDATA_IN_DRIVER))
  598. return NULL;
  599. return &sdata->wdev;
  600. }
  601. EXPORT_SYMBOL_GPL(ieee80211_vif_to_wdev);
  602. /*
  603. * Nothing should have been stuffed into the workqueue during
  604. * the suspend->resume cycle. Since we can't check each caller
  605. * of this function if we are already quiescing / suspended,
  606. * check here and don't WARN since this can actually happen when
  607. * the rx path (for example) is racing against __ieee80211_suspend
  608. * and suspending / quiescing was set after the rx path checked
  609. * them.
  610. */
  611. static bool ieee80211_can_queue_work(struct ieee80211_local *local)
  612. {
  613. if (local->quiescing || (local->suspended && !local->resuming)) {
  614. pr_warn("queueing ieee80211 work while going to suspend\n");
  615. return false;
  616. }
  617. return true;
  618. }
  619. void ieee80211_queue_work(struct ieee80211_hw *hw, struct work_struct *work)
  620. {
  621. struct ieee80211_local *local = hw_to_local(hw);
  622. if (!ieee80211_can_queue_work(local))
  623. return;
  624. queue_work(local->workqueue, work);
  625. }
  626. EXPORT_SYMBOL(ieee80211_queue_work);
  627. void ieee80211_queue_delayed_work(struct ieee80211_hw *hw,
  628. struct delayed_work *dwork,
  629. unsigned long delay)
  630. {
  631. struct ieee80211_local *local = hw_to_local(hw);
  632. if (!ieee80211_can_queue_work(local))
  633. return;
  634. queue_delayed_work(local->workqueue, dwork, delay);
  635. }
  636. EXPORT_SYMBOL(ieee80211_queue_delayed_work);
  637. u32 ieee802_11_parse_elems_crc(const u8 *start, size_t len, bool action,
  638. struct ieee802_11_elems *elems,
  639. u64 filter, u32 crc)
  640. {
  641. size_t left = len;
  642. const u8 *pos = start;
  643. bool calc_crc = filter != 0;
  644. DECLARE_BITMAP(seen_elems, 256);
  645. const u8 *ie;
  646. bitmap_zero(seen_elems, 256);
  647. memset(elems, 0, sizeof(*elems));
  648. elems->ie_start = start;
  649. elems->total_len = len;
  650. while (left >= 2) {
  651. u8 id, elen;
  652. bool elem_parse_failed;
  653. id = *pos++;
  654. elen = *pos++;
  655. left -= 2;
  656. if (elen > left) {
  657. elems->parse_error = true;
  658. break;
  659. }
  660. switch (id) {
  661. case WLAN_EID_SSID:
  662. case WLAN_EID_SUPP_RATES:
  663. case WLAN_EID_FH_PARAMS:
  664. case WLAN_EID_DS_PARAMS:
  665. case WLAN_EID_CF_PARAMS:
  666. case WLAN_EID_TIM:
  667. case WLAN_EID_IBSS_PARAMS:
  668. case WLAN_EID_CHALLENGE:
  669. case WLAN_EID_RSN:
  670. case WLAN_EID_ERP_INFO:
  671. case WLAN_EID_EXT_SUPP_RATES:
  672. case WLAN_EID_HT_CAPABILITY:
  673. case WLAN_EID_HT_OPERATION:
  674. case WLAN_EID_VHT_CAPABILITY:
  675. case WLAN_EID_VHT_OPERATION:
  676. case WLAN_EID_MESH_ID:
  677. case WLAN_EID_MESH_CONFIG:
  678. case WLAN_EID_PEER_MGMT:
  679. case WLAN_EID_PREQ:
  680. case WLAN_EID_PREP:
  681. case WLAN_EID_PERR:
  682. case WLAN_EID_RANN:
  683. case WLAN_EID_CHANNEL_SWITCH:
  684. case WLAN_EID_EXT_CHANSWITCH_ANN:
  685. case WLAN_EID_COUNTRY:
  686. case WLAN_EID_PWR_CONSTRAINT:
  687. case WLAN_EID_TIMEOUT_INTERVAL:
  688. case WLAN_EID_SECONDARY_CHANNEL_OFFSET:
  689. case WLAN_EID_WIDE_BW_CHANNEL_SWITCH:
  690. case WLAN_EID_CHAN_SWITCH_PARAM:
  691. case WLAN_EID_EXT_CAPABILITY:
  692. case WLAN_EID_CHAN_SWITCH_TIMING:
  693. case WLAN_EID_LINK_ID:
  694. case WLAN_EID_BSS_MAX_IDLE_PERIOD:
  695. /*
  696. * not listing WLAN_EID_CHANNEL_SWITCH_WRAPPER -- it seems possible
  697. * that if the content gets bigger it might be needed more than once
  698. */
  699. if (test_bit(id, seen_elems)) {
  700. elems->parse_error = true;
  701. left -= elen;
  702. pos += elen;
  703. continue;
  704. }
  705. break;
  706. }
  707. if (calc_crc && id < 64 && (filter & (1ULL << id)))
  708. crc = crc32_be(crc, pos - 2, elen + 2);
  709. elem_parse_failed = false;
  710. switch (id) {
  711. case WLAN_EID_LINK_ID:
  712. if (elen + 2 != sizeof(struct ieee80211_tdls_lnkie)) {
  713. elem_parse_failed = true;
  714. break;
  715. }
  716. elems->lnk_id = (void *)(pos - 2);
  717. break;
  718. case WLAN_EID_CHAN_SWITCH_TIMING:
  719. if (elen != sizeof(struct ieee80211_ch_switch_timing)) {
  720. elem_parse_failed = true;
  721. break;
  722. }
  723. elems->ch_sw_timing = (void *)pos;
  724. break;
  725. case WLAN_EID_EXT_CAPABILITY:
  726. elems->ext_capab = pos;
  727. elems->ext_capab_len = elen;
  728. break;
  729. case WLAN_EID_SSID:
  730. elems->ssid = pos;
  731. elems->ssid_len = elen;
  732. break;
  733. case WLAN_EID_SUPP_RATES:
  734. elems->supp_rates = pos;
  735. elems->supp_rates_len = elen;
  736. break;
  737. case WLAN_EID_DS_PARAMS:
  738. if (elen >= 1)
  739. elems->ds_params = pos;
  740. else
  741. elem_parse_failed = true;
  742. break;
  743. case WLAN_EID_TIM:
  744. if (elen >= sizeof(struct ieee80211_tim_ie)) {
  745. elems->tim = (void *)pos;
  746. elems->tim_len = elen;
  747. } else
  748. elem_parse_failed = true;
  749. break;
  750. case WLAN_EID_CHALLENGE:
  751. elems->challenge = pos;
  752. elems->challenge_len = elen;
  753. break;
  754. case WLAN_EID_VENDOR_SPECIFIC:
  755. if (elen >= 4 && pos[0] == 0x00 && pos[1] == 0x50 &&
  756. pos[2] == 0xf2) {
  757. /* Microsoft OUI (00:50:F2) */
  758. if (calc_crc)
  759. crc = crc32_be(crc, pos - 2, elen + 2);
  760. if (elen >= 5 && pos[3] == 2) {
  761. /* OUI Type 2 - WMM IE */
  762. if (pos[4] == 0) {
  763. elems->wmm_info = pos;
  764. elems->wmm_info_len = elen;
  765. } else if (pos[4] == 1) {
  766. elems->wmm_param = pos;
  767. elems->wmm_param_len = elen;
  768. }
  769. }
  770. }
  771. break;
  772. case WLAN_EID_RSN:
  773. elems->rsn = pos;
  774. elems->rsn_len = elen;
  775. break;
  776. case WLAN_EID_ERP_INFO:
  777. if (elen >= 1)
  778. elems->erp_info = pos;
  779. else
  780. elem_parse_failed = true;
  781. break;
  782. case WLAN_EID_EXT_SUPP_RATES:
  783. elems->ext_supp_rates = pos;
  784. elems->ext_supp_rates_len = elen;
  785. break;
  786. case WLAN_EID_HT_CAPABILITY:
  787. if (elen >= sizeof(struct ieee80211_ht_cap))
  788. elems->ht_cap_elem = (void *)pos;
  789. else
  790. elem_parse_failed = true;
  791. break;
  792. case WLAN_EID_HT_OPERATION:
  793. if (elen >= sizeof(struct ieee80211_ht_operation))
  794. elems->ht_operation = (void *)pos;
  795. else
  796. elem_parse_failed = true;
  797. break;
  798. case WLAN_EID_VHT_CAPABILITY:
  799. if (elen >= sizeof(struct ieee80211_vht_cap))
  800. elems->vht_cap_elem = (void *)pos;
  801. else
  802. elem_parse_failed = true;
  803. break;
  804. case WLAN_EID_VHT_OPERATION:
  805. if (elen >= sizeof(struct ieee80211_vht_operation))
  806. elems->vht_operation = (void *)pos;
  807. else
  808. elem_parse_failed = true;
  809. break;
  810. case WLAN_EID_OPMODE_NOTIF:
  811. if (elen > 0)
  812. elems->opmode_notif = pos;
  813. else
  814. elem_parse_failed = true;
  815. break;
  816. case WLAN_EID_MESH_ID:
  817. elems->mesh_id = pos;
  818. elems->mesh_id_len = elen;
  819. break;
  820. case WLAN_EID_MESH_CONFIG:
  821. if (elen >= sizeof(struct ieee80211_meshconf_ie))
  822. elems->mesh_config = (void *)pos;
  823. else
  824. elem_parse_failed = true;
  825. break;
  826. case WLAN_EID_PEER_MGMT:
  827. elems->peering = pos;
  828. elems->peering_len = elen;
  829. break;
  830. case WLAN_EID_MESH_AWAKE_WINDOW:
  831. if (elen >= 2)
  832. elems->awake_window = (void *)pos;
  833. break;
  834. case WLAN_EID_PREQ:
  835. elems->preq = pos;
  836. elems->preq_len = elen;
  837. break;
  838. case WLAN_EID_PREP:
  839. elems->prep = pos;
  840. elems->prep_len = elen;
  841. break;
  842. case WLAN_EID_PERR:
  843. elems->perr = pos;
  844. elems->perr_len = elen;
  845. break;
  846. case WLAN_EID_RANN:
  847. if (elen >= sizeof(struct ieee80211_rann_ie))
  848. elems->rann = (void *)pos;
  849. else
  850. elem_parse_failed = true;
  851. break;
  852. case WLAN_EID_CHANNEL_SWITCH:
  853. if (elen != sizeof(struct ieee80211_channel_sw_ie)) {
  854. elem_parse_failed = true;
  855. break;
  856. }
  857. elems->ch_switch_ie = (void *)pos;
  858. break;
  859. case WLAN_EID_EXT_CHANSWITCH_ANN:
  860. if (elen != sizeof(struct ieee80211_ext_chansw_ie)) {
  861. elem_parse_failed = true;
  862. break;
  863. }
  864. elems->ext_chansw_ie = (void *)pos;
  865. break;
  866. case WLAN_EID_SECONDARY_CHANNEL_OFFSET:
  867. if (elen != sizeof(struct ieee80211_sec_chan_offs_ie)) {
  868. elem_parse_failed = true;
  869. break;
  870. }
  871. elems->sec_chan_offs = (void *)pos;
  872. break;
  873. case WLAN_EID_CHAN_SWITCH_PARAM:
  874. if (elen !=
  875. sizeof(*elems->mesh_chansw_params_ie)) {
  876. elem_parse_failed = true;
  877. break;
  878. }
  879. elems->mesh_chansw_params_ie = (void *)pos;
  880. break;
  881. case WLAN_EID_WIDE_BW_CHANNEL_SWITCH:
  882. if (!action ||
  883. elen != sizeof(*elems->wide_bw_chansw_ie)) {
  884. elem_parse_failed = true;
  885. break;
  886. }
  887. elems->wide_bw_chansw_ie = (void *)pos;
  888. break;
  889. case WLAN_EID_CHANNEL_SWITCH_WRAPPER:
  890. if (action) {
  891. elem_parse_failed = true;
  892. break;
  893. }
  894. /*
  895. * This is a bit tricky, but as we only care about
  896. * the wide bandwidth channel switch element, so
  897. * just parse it out manually.
  898. */
  899. ie = cfg80211_find_ie(WLAN_EID_WIDE_BW_CHANNEL_SWITCH,
  900. pos, elen);
  901. if (ie) {
  902. if (ie[1] == sizeof(*elems->wide_bw_chansw_ie))
  903. elems->wide_bw_chansw_ie =
  904. (void *)(ie + 2);
  905. else
  906. elem_parse_failed = true;
  907. }
  908. break;
  909. case WLAN_EID_COUNTRY:
  910. elems->country_elem = pos;
  911. elems->country_elem_len = elen;
  912. break;
  913. case WLAN_EID_PWR_CONSTRAINT:
  914. if (elen != 1) {
  915. elem_parse_failed = true;
  916. break;
  917. }
  918. elems->pwr_constr_elem = pos;
  919. break;
  920. case WLAN_EID_CISCO_VENDOR_SPECIFIC:
  921. /* Lots of different options exist, but we only care
  922. * about the Dynamic Transmit Power Control element.
  923. * First check for the Cisco OUI, then for the DTPC
  924. * tag (0x00).
  925. */
  926. if (elen < 4) {
  927. elem_parse_failed = true;
  928. break;
  929. }
  930. if (pos[0] != 0x00 || pos[1] != 0x40 ||
  931. pos[2] != 0x96 || pos[3] != 0x00)
  932. break;
  933. if (elen != 6) {
  934. elem_parse_failed = true;
  935. break;
  936. }
  937. if (calc_crc)
  938. crc = crc32_be(crc, pos - 2, elen + 2);
  939. elems->cisco_dtpc_elem = pos;
  940. break;
  941. case WLAN_EID_TIMEOUT_INTERVAL:
  942. if (elen >= sizeof(struct ieee80211_timeout_interval_ie))
  943. elems->timeout_int = (void *)pos;
  944. else
  945. elem_parse_failed = true;
  946. break;
  947. case WLAN_EID_BSS_MAX_IDLE_PERIOD:
  948. if (elen >= sizeof(*elems->max_idle_period_ie))
  949. elems->max_idle_period_ie = (void *)pos;
  950. break;
  951. default:
  952. break;
  953. }
  954. if (elem_parse_failed)
  955. elems->parse_error = true;
  956. else
  957. __set_bit(id, seen_elems);
  958. left -= elen;
  959. pos += elen;
  960. }
  961. if (left != 0)
  962. elems->parse_error = true;
  963. return crc;
  964. }
  965. void ieee80211_set_wmm_default(struct ieee80211_sub_if_data *sdata,
  966. bool bss_notify, bool enable_qos)
  967. {
  968. struct ieee80211_local *local = sdata->local;
  969. struct ieee80211_tx_queue_params qparam;
  970. struct ieee80211_chanctx_conf *chanctx_conf;
  971. int ac;
  972. bool use_11b;
  973. bool is_ocb; /* Use another EDCA parameters if dot11OCBActivated=true */
  974. int aCWmin, aCWmax;
  975. if (!local->ops->conf_tx)
  976. return;
  977. if (local->hw.queues < IEEE80211_NUM_ACS)
  978. return;
  979. memset(&qparam, 0, sizeof(qparam));
  980. rcu_read_lock();
  981. chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
  982. use_11b = (chanctx_conf &&
  983. chanctx_conf->def.chan->band == NL80211_BAND_2GHZ) &&
  984. !(sdata->flags & IEEE80211_SDATA_OPERATING_GMODE);
  985. rcu_read_unlock();
  986. is_ocb = (sdata->vif.type == NL80211_IFTYPE_OCB);
  987. /* Set defaults according to 802.11-2007 Table 7-37 */
  988. aCWmax = 1023;
  989. if (use_11b)
  990. aCWmin = 31;
  991. else
  992. aCWmin = 15;
  993. /* Confiure old 802.11b/g medium access rules. */
  994. qparam.cw_max = aCWmax;
  995. qparam.cw_min = aCWmin;
  996. qparam.txop = 0;
  997. qparam.aifs = 2;
  998. for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
  999. /* Update if QoS is enabled. */
  1000. if (enable_qos) {
  1001. switch (ac) {
  1002. case IEEE80211_AC_BK:
  1003. qparam.cw_max = aCWmax;
  1004. qparam.cw_min = aCWmin;
  1005. qparam.txop = 0;
  1006. if (is_ocb)
  1007. qparam.aifs = 9;
  1008. else
  1009. qparam.aifs = 7;
  1010. break;
  1011. /* never happens but let's not leave undefined */
  1012. default:
  1013. case IEEE80211_AC_BE:
  1014. qparam.cw_max = aCWmax;
  1015. qparam.cw_min = aCWmin;
  1016. qparam.txop = 0;
  1017. if (is_ocb)
  1018. qparam.aifs = 6;
  1019. else
  1020. qparam.aifs = 3;
  1021. break;
  1022. case IEEE80211_AC_VI:
  1023. qparam.cw_max = aCWmin;
  1024. qparam.cw_min = (aCWmin + 1) / 2 - 1;
  1025. if (is_ocb)
  1026. qparam.txop = 0;
  1027. else if (use_11b)
  1028. qparam.txop = 6016/32;
  1029. else
  1030. qparam.txop = 3008/32;
  1031. if (is_ocb)
  1032. qparam.aifs = 3;
  1033. else
  1034. qparam.aifs = 2;
  1035. break;
  1036. case IEEE80211_AC_VO:
  1037. qparam.cw_max = (aCWmin + 1) / 2 - 1;
  1038. qparam.cw_min = (aCWmin + 1) / 4 - 1;
  1039. if (is_ocb)
  1040. qparam.txop = 0;
  1041. else if (use_11b)
  1042. qparam.txop = 3264/32;
  1043. else
  1044. qparam.txop = 1504/32;
  1045. qparam.aifs = 2;
  1046. break;
  1047. }
  1048. }
  1049. qparam.uapsd = false;
  1050. sdata->tx_conf[ac] = qparam;
  1051. drv_conf_tx(local, sdata, ac, &qparam);
  1052. }
  1053. if (sdata->vif.type != NL80211_IFTYPE_MONITOR &&
  1054. sdata->vif.type != NL80211_IFTYPE_P2P_DEVICE &&
  1055. sdata->vif.type != NL80211_IFTYPE_NAN) {
  1056. sdata->vif.bss_conf.qos = enable_qos;
  1057. if (bss_notify)
  1058. ieee80211_bss_info_change_notify(sdata,
  1059. BSS_CHANGED_QOS);
  1060. }
  1061. }
  1062. void ieee80211_send_auth(struct ieee80211_sub_if_data *sdata,
  1063. u16 transaction, u16 auth_alg, u16 status,
  1064. const u8 *extra, size_t extra_len, const u8 *da,
  1065. const u8 *bssid, const u8 *key, u8 key_len, u8 key_idx,
  1066. u32 tx_flags)
  1067. {
  1068. struct ieee80211_local *local = sdata->local;
  1069. struct sk_buff *skb;
  1070. struct ieee80211_mgmt *mgmt;
  1071. int err;
  1072. /* 24 + 6 = header + auth_algo + auth_transaction + status_code */
  1073. skb = dev_alloc_skb(local->hw.extra_tx_headroom + IEEE80211_WEP_IV_LEN +
  1074. 24 + 6 + extra_len + IEEE80211_WEP_ICV_LEN);
  1075. if (!skb)
  1076. return;
  1077. skb_reserve(skb, local->hw.extra_tx_headroom + IEEE80211_WEP_IV_LEN);
  1078. mgmt = skb_put_zero(skb, 24 + 6);
  1079. mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
  1080. IEEE80211_STYPE_AUTH);
  1081. memcpy(mgmt->da, da, ETH_ALEN);
  1082. memcpy(mgmt->sa, sdata->vif.addr, ETH_ALEN);
  1083. memcpy(mgmt->bssid, bssid, ETH_ALEN);
  1084. mgmt->u.auth.auth_alg = cpu_to_le16(auth_alg);
  1085. mgmt->u.auth.auth_transaction = cpu_to_le16(transaction);
  1086. mgmt->u.auth.status_code = cpu_to_le16(status);
  1087. if (extra)
  1088. memcpy(skb_put(skb, extra_len), extra, extra_len);
  1089. if (auth_alg == WLAN_AUTH_SHARED_KEY && transaction == 3) {
  1090. mgmt->frame_control |= cpu_to_le16(IEEE80211_FCTL_PROTECTED);
  1091. err = ieee80211_wep_encrypt(local, skb, key, key_len, key_idx);
  1092. WARN_ON(err);
  1093. }
  1094. IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT |
  1095. tx_flags;
  1096. ieee80211_tx_skb(sdata, skb);
  1097. }
  1098. void ieee80211_send_deauth_disassoc(struct ieee80211_sub_if_data *sdata,
  1099. const u8 *bssid, u16 stype, u16 reason,
  1100. bool send_frame, u8 *frame_buf)
  1101. {
  1102. struct ieee80211_local *local = sdata->local;
  1103. struct sk_buff *skb;
  1104. struct ieee80211_mgmt *mgmt = (void *)frame_buf;
  1105. /* build frame */
  1106. mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT | stype);
  1107. mgmt->duration = 0; /* initialize only */
  1108. mgmt->seq_ctrl = 0; /* initialize only */
  1109. memcpy(mgmt->da, bssid, ETH_ALEN);
  1110. memcpy(mgmt->sa, sdata->vif.addr, ETH_ALEN);
  1111. memcpy(mgmt->bssid, bssid, ETH_ALEN);
  1112. /* u.deauth.reason_code == u.disassoc.reason_code */
  1113. mgmt->u.deauth.reason_code = cpu_to_le16(reason);
  1114. if (send_frame) {
  1115. skb = dev_alloc_skb(local->hw.extra_tx_headroom +
  1116. IEEE80211_DEAUTH_FRAME_LEN);
  1117. if (!skb)
  1118. return;
  1119. skb_reserve(skb, local->hw.extra_tx_headroom);
  1120. /* copy in frame */
  1121. memcpy(skb_put(skb, IEEE80211_DEAUTH_FRAME_LEN),
  1122. mgmt, IEEE80211_DEAUTH_FRAME_LEN);
  1123. if (sdata->vif.type != NL80211_IFTYPE_STATION ||
  1124. !(sdata->u.mgd.flags & IEEE80211_STA_MFP_ENABLED))
  1125. IEEE80211_SKB_CB(skb)->flags |=
  1126. IEEE80211_TX_INTFL_DONT_ENCRYPT;
  1127. ieee80211_tx_skb(sdata, skb);
  1128. }
  1129. }
  1130. static int ieee80211_build_preq_ies_band(struct ieee80211_local *local,
  1131. u8 *buffer, size_t buffer_len,
  1132. const u8 *ie, size_t ie_len,
  1133. enum nl80211_band band,
  1134. u32 rate_mask,
  1135. struct cfg80211_chan_def *chandef,
  1136. size_t *offset)
  1137. {
  1138. struct ieee80211_supported_band *sband;
  1139. u8 *pos = buffer, *end = buffer + buffer_len;
  1140. size_t noffset;
  1141. int supp_rates_len, i;
  1142. u8 rates[32];
  1143. int num_rates;
  1144. int ext_rates_len;
  1145. int shift;
  1146. u32 rate_flags;
  1147. bool have_80mhz = false;
  1148. *offset = 0;
  1149. sband = local->hw.wiphy->bands[band];
  1150. if (WARN_ON_ONCE(!sband))
  1151. return 0;
  1152. rate_flags = ieee80211_chandef_rate_flags(chandef);
  1153. shift = ieee80211_chandef_get_shift(chandef);
  1154. num_rates = 0;
  1155. for (i = 0; i < sband->n_bitrates; i++) {
  1156. if ((BIT(i) & rate_mask) == 0)
  1157. continue; /* skip rate */
  1158. if ((rate_flags & sband->bitrates[i].flags) != rate_flags)
  1159. continue;
  1160. rates[num_rates++] =
  1161. (u8) DIV_ROUND_UP(sband->bitrates[i].bitrate,
  1162. (1 << shift) * 5);
  1163. }
  1164. supp_rates_len = min_t(int, num_rates, 8);
  1165. if (end - pos < 2 + supp_rates_len)
  1166. goto out_err;
  1167. *pos++ = WLAN_EID_SUPP_RATES;
  1168. *pos++ = supp_rates_len;
  1169. memcpy(pos, rates, supp_rates_len);
  1170. pos += supp_rates_len;
  1171. /* insert "request information" if in custom IEs */
  1172. if (ie && ie_len) {
  1173. static const u8 before_extrates[] = {
  1174. WLAN_EID_SSID,
  1175. WLAN_EID_SUPP_RATES,
  1176. WLAN_EID_REQUEST,
  1177. };
  1178. noffset = ieee80211_ie_split(ie, ie_len,
  1179. before_extrates,
  1180. ARRAY_SIZE(before_extrates),
  1181. *offset);
  1182. if (end - pos < noffset - *offset)
  1183. goto out_err;
  1184. memcpy(pos, ie + *offset, noffset - *offset);
  1185. pos += noffset - *offset;
  1186. *offset = noffset;
  1187. }
  1188. ext_rates_len = num_rates - supp_rates_len;
  1189. if (ext_rates_len > 0) {
  1190. if (end - pos < 2 + ext_rates_len)
  1191. goto out_err;
  1192. *pos++ = WLAN_EID_EXT_SUPP_RATES;
  1193. *pos++ = ext_rates_len;
  1194. memcpy(pos, rates + supp_rates_len, ext_rates_len);
  1195. pos += ext_rates_len;
  1196. }
  1197. if (chandef->chan && sband->band == NL80211_BAND_2GHZ) {
  1198. if (end - pos < 3)
  1199. goto out_err;
  1200. *pos++ = WLAN_EID_DS_PARAMS;
  1201. *pos++ = 1;
  1202. *pos++ = ieee80211_frequency_to_channel(
  1203. chandef->chan->center_freq);
  1204. }
  1205. /* insert custom IEs that go before HT */
  1206. if (ie && ie_len) {
  1207. static const u8 before_ht[] = {
  1208. WLAN_EID_SSID,
  1209. WLAN_EID_SUPP_RATES,
  1210. WLAN_EID_REQUEST,
  1211. WLAN_EID_EXT_SUPP_RATES,
  1212. WLAN_EID_DS_PARAMS,
  1213. WLAN_EID_SUPPORTED_REGULATORY_CLASSES,
  1214. };
  1215. noffset = ieee80211_ie_split(ie, ie_len,
  1216. before_ht, ARRAY_SIZE(before_ht),
  1217. *offset);
  1218. if (end - pos < noffset - *offset)
  1219. goto out_err;
  1220. memcpy(pos, ie + *offset, noffset - *offset);
  1221. pos += noffset - *offset;
  1222. *offset = noffset;
  1223. }
  1224. if (sband->ht_cap.ht_supported) {
  1225. if (end - pos < 2 + sizeof(struct ieee80211_ht_cap))
  1226. goto out_err;
  1227. pos = ieee80211_ie_build_ht_cap(pos, &sband->ht_cap,
  1228. sband->ht_cap.cap);
  1229. }
  1230. /*
  1231. * If adding more here, adjust code in main.c
  1232. * that calculates local->scan_ies_len.
  1233. */
  1234. /* insert custom IEs that go before VHT */
  1235. if (ie && ie_len) {
  1236. static const u8 before_vht[] = {
  1237. WLAN_EID_SSID,
  1238. WLAN_EID_SUPP_RATES,
  1239. WLAN_EID_REQUEST,
  1240. WLAN_EID_EXT_SUPP_RATES,
  1241. WLAN_EID_DS_PARAMS,
  1242. WLAN_EID_SUPPORTED_REGULATORY_CLASSES,
  1243. WLAN_EID_HT_CAPABILITY,
  1244. WLAN_EID_BSS_COEX_2040,
  1245. WLAN_EID_EXT_CAPABILITY,
  1246. WLAN_EID_SSID_LIST,
  1247. WLAN_EID_CHANNEL_USAGE,
  1248. WLAN_EID_INTERWORKING,
  1249. /* mesh ID can't happen here */
  1250. /* 60 GHz can't happen here right now */
  1251. };
  1252. noffset = ieee80211_ie_split(ie, ie_len,
  1253. before_vht, ARRAY_SIZE(before_vht),
  1254. *offset);
  1255. if (end - pos < noffset - *offset)
  1256. goto out_err;
  1257. memcpy(pos, ie + *offset, noffset - *offset);
  1258. pos += noffset - *offset;
  1259. *offset = noffset;
  1260. }
  1261. /* Check if any channel in this sband supports at least 80 MHz */
  1262. for (i = 0; i < sband->n_channels; i++) {
  1263. if (sband->channels[i].flags & (IEEE80211_CHAN_DISABLED |
  1264. IEEE80211_CHAN_NO_80MHZ))
  1265. continue;
  1266. have_80mhz = true;
  1267. break;
  1268. }
  1269. if (sband->vht_cap.vht_supported && have_80mhz) {
  1270. if (end - pos < 2 + sizeof(struct ieee80211_vht_cap))
  1271. goto out_err;
  1272. pos = ieee80211_ie_build_vht_cap(pos, &sband->vht_cap,
  1273. sband->vht_cap.cap);
  1274. }
  1275. return pos - buffer;
  1276. out_err:
  1277. WARN_ONCE(1, "not enough space for preq IEs\n");
  1278. return pos - buffer;
  1279. }
  1280. int ieee80211_build_preq_ies(struct ieee80211_local *local, u8 *buffer,
  1281. size_t buffer_len,
  1282. struct ieee80211_scan_ies *ie_desc,
  1283. const u8 *ie, size_t ie_len,
  1284. u8 bands_used, u32 *rate_masks,
  1285. struct cfg80211_chan_def *chandef)
  1286. {
  1287. size_t pos = 0, old_pos = 0, custom_ie_offset = 0;
  1288. int i;
  1289. memset(ie_desc, 0, sizeof(*ie_desc));
  1290. for (i = 0; i < NUM_NL80211_BANDS; i++) {
  1291. if (bands_used & BIT(i)) {
  1292. pos += ieee80211_build_preq_ies_band(local,
  1293. buffer + pos,
  1294. buffer_len - pos,
  1295. ie, ie_len, i,
  1296. rate_masks[i],
  1297. chandef,
  1298. &custom_ie_offset);
  1299. ie_desc->ies[i] = buffer + old_pos;
  1300. ie_desc->len[i] = pos - old_pos;
  1301. old_pos = pos;
  1302. }
  1303. }
  1304. /* add any remaining custom IEs */
  1305. if (ie && ie_len) {
  1306. if (WARN_ONCE(buffer_len - pos < ie_len - custom_ie_offset,
  1307. "not enough space for preq custom IEs\n"))
  1308. return pos;
  1309. memcpy(buffer + pos, ie + custom_ie_offset,
  1310. ie_len - custom_ie_offset);
  1311. ie_desc->common_ies = buffer + pos;
  1312. ie_desc->common_ie_len = ie_len - custom_ie_offset;
  1313. pos += ie_len - custom_ie_offset;
  1314. }
  1315. return pos;
  1316. };
  1317. struct sk_buff *ieee80211_build_probe_req(struct ieee80211_sub_if_data *sdata,
  1318. const u8 *src, const u8 *dst,
  1319. u32 ratemask,
  1320. struct ieee80211_channel *chan,
  1321. const u8 *ssid, size_t ssid_len,
  1322. const u8 *ie, size_t ie_len,
  1323. bool directed)
  1324. {
  1325. struct ieee80211_local *local = sdata->local;
  1326. struct cfg80211_chan_def chandef;
  1327. struct sk_buff *skb;
  1328. struct ieee80211_mgmt *mgmt;
  1329. int ies_len;
  1330. u32 rate_masks[NUM_NL80211_BANDS] = {};
  1331. struct ieee80211_scan_ies dummy_ie_desc;
  1332. /*
  1333. * Do not send DS Channel parameter for directed probe requests
  1334. * in order to maximize the chance that we get a response. Some
  1335. * badly-behaved APs don't respond when this parameter is included.
  1336. */
  1337. chandef.width = sdata->vif.bss_conf.chandef.width;
  1338. if (directed)
  1339. chandef.chan = NULL;
  1340. else
  1341. chandef.chan = chan;
  1342. skb = ieee80211_probereq_get(&local->hw, src, ssid, ssid_len,
  1343. 100 + ie_len);
  1344. if (!skb)
  1345. return NULL;
  1346. rate_masks[chan->band] = ratemask;
  1347. ies_len = ieee80211_build_preq_ies(local, skb_tail_pointer(skb),
  1348. skb_tailroom(skb), &dummy_ie_desc,
  1349. ie, ie_len, BIT(chan->band),
  1350. rate_masks, &chandef);
  1351. skb_put(skb, ies_len);
  1352. if (dst) {
  1353. mgmt = (struct ieee80211_mgmt *) skb->data;
  1354. memcpy(mgmt->da, dst, ETH_ALEN);
  1355. memcpy(mgmt->bssid, dst, ETH_ALEN);
  1356. }
  1357. IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
  1358. return skb;
  1359. }
  1360. void ieee80211_send_probe_req(struct ieee80211_sub_if_data *sdata,
  1361. const u8 *src, const u8 *dst,
  1362. const u8 *ssid, size_t ssid_len,
  1363. const u8 *ie, size_t ie_len,
  1364. u32 ratemask, bool directed, u32 tx_flags,
  1365. struct ieee80211_channel *channel, bool scan)
  1366. {
  1367. struct sk_buff *skb;
  1368. skb = ieee80211_build_probe_req(sdata, src, dst, ratemask, channel,
  1369. ssid, ssid_len,
  1370. ie, ie_len, directed);
  1371. if (skb) {
  1372. IEEE80211_SKB_CB(skb)->flags |= tx_flags;
  1373. if (scan)
  1374. ieee80211_tx_skb_tid_band(sdata, skb, 7, channel->band);
  1375. else
  1376. ieee80211_tx_skb(sdata, skb);
  1377. }
  1378. }
  1379. u32 ieee80211_sta_get_rates(struct ieee80211_sub_if_data *sdata,
  1380. struct ieee802_11_elems *elems,
  1381. enum nl80211_band band, u32 *basic_rates)
  1382. {
  1383. struct ieee80211_supported_band *sband;
  1384. size_t num_rates;
  1385. u32 supp_rates, rate_flags;
  1386. int i, j, shift;
  1387. sband = sdata->local->hw.wiphy->bands[band];
  1388. if (WARN_ON(!sband))
  1389. return 1;
  1390. rate_flags = ieee80211_chandef_rate_flags(&sdata->vif.bss_conf.chandef);
  1391. shift = ieee80211_vif_get_shift(&sdata->vif);
  1392. num_rates = sband->n_bitrates;
  1393. supp_rates = 0;
  1394. for (i = 0; i < elems->supp_rates_len +
  1395. elems->ext_supp_rates_len; i++) {
  1396. u8 rate = 0;
  1397. int own_rate;
  1398. bool is_basic;
  1399. if (i < elems->supp_rates_len)
  1400. rate = elems->supp_rates[i];
  1401. else if (elems->ext_supp_rates)
  1402. rate = elems->ext_supp_rates
  1403. [i - elems->supp_rates_len];
  1404. own_rate = 5 * (rate & 0x7f);
  1405. is_basic = !!(rate & 0x80);
  1406. if (is_basic && (rate & 0x7f) == BSS_MEMBERSHIP_SELECTOR_HT_PHY)
  1407. continue;
  1408. for (j = 0; j < num_rates; j++) {
  1409. int brate;
  1410. if ((rate_flags & sband->bitrates[j].flags)
  1411. != rate_flags)
  1412. continue;
  1413. brate = DIV_ROUND_UP(sband->bitrates[j].bitrate,
  1414. 1 << shift);
  1415. if (brate == own_rate) {
  1416. supp_rates |= BIT(j);
  1417. if (basic_rates && is_basic)
  1418. *basic_rates |= BIT(j);
  1419. }
  1420. }
  1421. }
  1422. return supp_rates;
  1423. }
  1424. void ieee80211_stop_device(struct ieee80211_local *local)
  1425. {
  1426. ieee80211_led_radio(local, false);
  1427. ieee80211_mod_tpt_led_trig(local, 0, IEEE80211_TPT_LEDTRIG_FL_RADIO);
  1428. cancel_work_sync(&local->reconfig_filter);
  1429. flush_workqueue(local->workqueue);
  1430. drv_stop(local);
  1431. }
  1432. static void ieee80211_flush_completed_scan(struct ieee80211_local *local,
  1433. bool aborted)
  1434. {
  1435. /* It's possible that we don't handle the scan completion in
  1436. * time during suspend, so if it's still marked as completed
  1437. * here, queue the work and flush it to clean things up.
  1438. * Instead of calling the worker function directly here, we
  1439. * really queue it to avoid potential races with other flows
  1440. * scheduling the same work.
  1441. */
  1442. if (test_bit(SCAN_COMPLETED, &local->scanning)) {
  1443. /* If coming from reconfiguration failure, abort the scan so
  1444. * we don't attempt to continue a partial HW scan - which is
  1445. * possible otherwise if (e.g.) the 2.4 GHz portion was the
  1446. * completed scan, and a 5 GHz portion is still pending.
  1447. */
  1448. if (aborted)
  1449. set_bit(SCAN_ABORTED, &local->scanning);
  1450. ieee80211_queue_delayed_work(&local->hw, &local->scan_work, 0);
  1451. flush_delayed_work(&local->scan_work);
  1452. }
  1453. }
  1454. static void ieee80211_handle_reconfig_failure(struct ieee80211_local *local)
  1455. {
  1456. struct ieee80211_sub_if_data *sdata;
  1457. struct ieee80211_chanctx *ctx;
  1458. /*
  1459. * We get here if during resume the device can't be restarted properly.
  1460. * We might also get here if this happens during HW reset, which is a
  1461. * slightly different situation and we need to drop all connections in
  1462. * the latter case.
  1463. *
  1464. * Ask cfg80211 to turn off all interfaces, this will result in more
  1465. * warnings but at least we'll then get into a clean stopped state.
  1466. */
  1467. local->resuming = false;
  1468. local->suspended = false;
  1469. local->in_reconfig = false;
  1470. ieee80211_flush_completed_scan(local, true);
  1471. /* scheduled scan clearly can't be running any more, but tell
  1472. * cfg80211 and clear local state
  1473. */
  1474. ieee80211_sched_scan_end(local);
  1475. list_for_each_entry(sdata, &local->interfaces, list)
  1476. sdata->flags &= ~IEEE80211_SDATA_IN_DRIVER;
  1477. /* Mark channel contexts as not being in the driver any more to avoid
  1478. * removing them from the driver during the shutdown process...
  1479. */
  1480. mutex_lock(&local->chanctx_mtx);
  1481. list_for_each_entry(ctx, &local->chanctx_list, list)
  1482. ctx->driver_present = false;
  1483. mutex_unlock(&local->chanctx_mtx);
  1484. cfg80211_shutdown_all_interfaces(local->hw.wiphy);
  1485. }
  1486. static void ieee80211_assign_chanctx(struct ieee80211_local *local,
  1487. struct ieee80211_sub_if_data *sdata)
  1488. {
  1489. struct ieee80211_chanctx_conf *conf;
  1490. struct ieee80211_chanctx *ctx;
  1491. if (!local->use_chanctx)
  1492. return;
  1493. mutex_lock(&local->chanctx_mtx);
  1494. conf = rcu_dereference_protected(sdata->vif.chanctx_conf,
  1495. lockdep_is_held(&local->chanctx_mtx));
  1496. if (conf) {
  1497. ctx = container_of(conf, struct ieee80211_chanctx, conf);
  1498. drv_assign_vif_chanctx(local, sdata, ctx);
  1499. }
  1500. mutex_unlock(&local->chanctx_mtx);
  1501. }
  1502. static void ieee80211_reconfig_stations(struct ieee80211_sub_if_data *sdata)
  1503. {
  1504. struct ieee80211_local *local = sdata->local;
  1505. struct sta_info *sta;
  1506. /* add STAs back */
  1507. mutex_lock(&local->sta_mtx);
  1508. list_for_each_entry(sta, &local->sta_list, list) {
  1509. enum ieee80211_sta_state state;
  1510. if (!sta->uploaded || sta->sdata != sdata)
  1511. continue;
  1512. for (state = IEEE80211_STA_NOTEXIST;
  1513. state < sta->sta_state; state++)
  1514. WARN_ON(drv_sta_state(local, sta->sdata, sta, state,
  1515. state + 1));
  1516. }
  1517. mutex_unlock(&local->sta_mtx);
  1518. }
  1519. static int ieee80211_reconfig_nan(struct ieee80211_sub_if_data *sdata)
  1520. {
  1521. struct cfg80211_nan_func *func, **funcs;
  1522. int res, id, i = 0;
  1523. res = drv_start_nan(sdata->local, sdata,
  1524. &sdata->u.nan.conf);
  1525. if (WARN_ON(res))
  1526. return res;
  1527. funcs = kzalloc((sdata->local->hw.max_nan_de_entries + 1) *
  1528. sizeof(*funcs), GFP_KERNEL);
  1529. if (!funcs)
  1530. return -ENOMEM;
  1531. /* Add all the functions:
  1532. * This is a little bit ugly. We need to call a potentially sleeping
  1533. * callback for each NAN function, so we can't hold the spinlock.
  1534. */
  1535. spin_lock_bh(&sdata->u.nan.func_lock);
  1536. idr_for_each_entry(&sdata->u.nan.function_inst_ids, func, id)
  1537. funcs[i++] = func;
  1538. spin_unlock_bh(&sdata->u.nan.func_lock);
  1539. for (i = 0; funcs[i]; i++) {
  1540. res = drv_add_nan_func(sdata->local, sdata, funcs[i]);
  1541. if (WARN_ON(res))
  1542. ieee80211_nan_func_terminated(&sdata->vif,
  1543. funcs[i]->instance_id,
  1544. NL80211_NAN_FUNC_TERM_REASON_ERROR,
  1545. GFP_KERNEL);
  1546. }
  1547. kfree(funcs);
  1548. return 0;
  1549. }
  1550. int ieee80211_reconfig(struct ieee80211_local *local)
  1551. {
  1552. struct ieee80211_hw *hw = &local->hw;
  1553. struct ieee80211_sub_if_data *sdata;
  1554. struct ieee80211_chanctx *ctx;
  1555. struct sta_info *sta;
  1556. int res, i;
  1557. bool reconfig_due_to_wowlan = false;
  1558. struct ieee80211_sub_if_data *sched_scan_sdata;
  1559. struct cfg80211_sched_scan_request *sched_scan_req;
  1560. bool sched_scan_stopped = false;
  1561. bool suspended = local->suspended;
  1562. /* nothing to do if HW shouldn't run */
  1563. if (!local->open_count)
  1564. goto wake_up;
  1565. #ifdef CONFIG_PM
  1566. if (suspended)
  1567. local->resuming = true;
  1568. if (local->wowlan) {
  1569. /*
  1570. * In the wowlan case, both mac80211 and the device
  1571. * are functional when the resume op is called, so
  1572. * clear local->suspended so the device could operate
  1573. * normally (e.g. pass rx frames).
  1574. */
  1575. local->suspended = false;
  1576. res = drv_resume(local);
  1577. local->wowlan = false;
  1578. if (res < 0) {
  1579. local->resuming = false;
  1580. return res;
  1581. }
  1582. if (res == 0)
  1583. goto wake_up;
  1584. WARN_ON(res > 1);
  1585. /*
  1586. * res is 1, which means the driver requested
  1587. * to go through a regular reset on wakeup.
  1588. * restore local->suspended in this case.
  1589. */
  1590. reconfig_due_to_wowlan = true;
  1591. local->suspended = true;
  1592. }
  1593. #endif
  1594. /*
  1595. * In case of hw_restart during suspend (without wowlan),
  1596. * cancel restart work, as we are reconfiguring the device
  1597. * anyway.
  1598. * Note that restart_work is scheduled on a frozen workqueue,
  1599. * so we can't deadlock in this case.
  1600. */
  1601. if (suspended && local->in_reconfig && !reconfig_due_to_wowlan)
  1602. cancel_work_sync(&local->restart_work);
  1603. local->started = false;
  1604. /*
  1605. * Upon resume hardware can sometimes be goofy due to
  1606. * various platform / driver / bus issues, so restarting
  1607. * the device may at times not work immediately. Propagate
  1608. * the error.
  1609. */
  1610. res = drv_start(local);
  1611. if (res) {
  1612. if (suspended)
  1613. WARN(1, "Hardware became unavailable upon resume. This could be a software issue prior to suspend or a hardware issue.\n");
  1614. else
  1615. WARN(1, "Hardware became unavailable during restart.\n");
  1616. ieee80211_handle_reconfig_failure(local);
  1617. return res;
  1618. }
  1619. /* setup fragmentation threshold */
  1620. drv_set_frag_threshold(local, hw->wiphy->frag_threshold);
  1621. /* setup RTS threshold */
  1622. drv_set_rts_threshold(local, hw->wiphy->rts_threshold);
  1623. /* reset coverage class */
  1624. drv_set_coverage_class(local, hw->wiphy->coverage_class);
  1625. ieee80211_led_radio(local, true);
  1626. ieee80211_mod_tpt_led_trig(local,
  1627. IEEE80211_TPT_LEDTRIG_FL_RADIO, 0);
  1628. /* add interfaces */
  1629. sdata = rtnl_dereference(local->monitor_sdata);
  1630. if (sdata) {
  1631. /* in HW restart it exists already */
  1632. WARN_ON(local->resuming);
  1633. res = drv_add_interface(local, sdata);
  1634. if (WARN_ON(res)) {
  1635. RCU_INIT_POINTER(local->monitor_sdata, NULL);
  1636. synchronize_net();
  1637. kfree(sdata);
  1638. }
  1639. }
  1640. list_for_each_entry(sdata, &local->interfaces, list) {
  1641. if (sdata->vif.type != NL80211_IFTYPE_AP_VLAN &&
  1642. sdata->vif.type != NL80211_IFTYPE_MONITOR &&
  1643. ieee80211_sdata_running(sdata)) {
  1644. res = drv_add_interface(local, sdata);
  1645. if (WARN_ON(res))
  1646. break;
  1647. }
  1648. }
  1649. /* If adding any of the interfaces failed above, roll back and
  1650. * report failure.
  1651. */
  1652. if (res) {
  1653. list_for_each_entry_continue_reverse(sdata, &local->interfaces,
  1654. list)
  1655. if (sdata->vif.type != NL80211_IFTYPE_AP_VLAN &&
  1656. sdata->vif.type != NL80211_IFTYPE_MONITOR &&
  1657. ieee80211_sdata_running(sdata))
  1658. drv_remove_interface(local, sdata);
  1659. ieee80211_handle_reconfig_failure(local);
  1660. return res;
  1661. }
  1662. /* add channel contexts */
  1663. if (local->use_chanctx) {
  1664. mutex_lock(&local->chanctx_mtx);
  1665. list_for_each_entry(ctx, &local->chanctx_list, list)
  1666. if (ctx->replace_state !=
  1667. IEEE80211_CHANCTX_REPLACES_OTHER)
  1668. WARN_ON(drv_add_chanctx(local, ctx));
  1669. mutex_unlock(&local->chanctx_mtx);
  1670. sdata = rtnl_dereference(local->monitor_sdata);
  1671. if (sdata && ieee80211_sdata_running(sdata))
  1672. ieee80211_assign_chanctx(local, sdata);
  1673. }
  1674. /* reconfigure hardware */
  1675. ieee80211_hw_config(local, ~0);
  1676. ieee80211_configure_filter(local);
  1677. /* Finally also reconfigure all the BSS information */
  1678. list_for_each_entry(sdata, &local->interfaces, list) {
  1679. u32 changed;
  1680. if (!ieee80211_sdata_running(sdata))
  1681. continue;
  1682. ieee80211_assign_chanctx(local, sdata);
  1683. switch (sdata->vif.type) {
  1684. case NL80211_IFTYPE_AP_VLAN:
  1685. case NL80211_IFTYPE_MONITOR:
  1686. break;
  1687. default:
  1688. ieee80211_reconfig_stations(sdata);
  1689. /* fall through */
  1690. case NL80211_IFTYPE_AP: /* AP stations are handled later */
  1691. for (i = 0; i < IEEE80211_NUM_ACS; i++)
  1692. drv_conf_tx(local, sdata, i,
  1693. &sdata->tx_conf[i]);
  1694. break;
  1695. }
  1696. /* common change flags for all interface types */
  1697. changed = BSS_CHANGED_ERP_CTS_PROT |
  1698. BSS_CHANGED_ERP_PREAMBLE |
  1699. BSS_CHANGED_ERP_SLOT |
  1700. BSS_CHANGED_HT |
  1701. BSS_CHANGED_BASIC_RATES |
  1702. BSS_CHANGED_BEACON_INT |
  1703. BSS_CHANGED_BSSID |
  1704. BSS_CHANGED_CQM |
  1705. BSS_CHANGED_QOS |
  1706. BSS_CHANGED_IDLE |
  1707. BSS_CHANGED_TXPOWER;
  1708. if (sdata->vif.mu_mimo_owner)
  1709. changed |= BSS_CHANGED_MU_GROUPS;
  1710. switch (sdata->vif.type) {
  1711. case NL80211_IFTYPE_STATION:
  1712. changed |= BSS_CHANGED_ASSOC |
  1713. BSS_CHANGED_ARP_FILTER |
  1714. BSS_CHANGED_PS;
  1715. /* Re-send beacon info report to the driver */
  1716. if (sdata->u.mgd.have_beacon)
  1717. changed |= BSS_CHANGED_BEACON_INFO;
  1718. if (sdata->vif.bss_conf.max_idle_period ||
  1719. sdata->vif.bss_conf.protected_keep_alive)
  1720. changed |= BSS_CHANGED_KEEP_ALIVE;
  1721. sdata_lock(sdata);
  1722. ieee80211_bss_info_change_notify(sdata, changed);
  1723. sdata_unlock(sdata);
  1724. break;
  1725. case NL80211_IFTYPE_OCB:
  1726. changed |= BSS_CHANGED_OCB;
  1727. ieee80211_bss_info_change_notify(sdata, changed);
  1728. break;
  1729. case NL80211_IFTYPE_ADHOC:
  1730. changed |= BSS_CHANGED_IBSS;
  1731. /* fall through */
  1732. case NL80211_IFTYPE_AP:
  1733. changed |= BSS_CHANGED_SSID | BSS_CHANGED_P2P_PS;
  1734. if (sdata->vif.type == NL80211_IFTYPE_AP) {
  1735. changed |= BSS_CHANGED_AP_PROBE_RESP;
  1736. if (rcu_access_pointer(sdata->u.ap.beacon))
  1737. drv_start_ap(local, sdata);
  1738. }
  1739. /* fall through */
  1740. case NL80211_IFTYPE_MESH_POINT:
  1741. if (sdata->vif.bss_conf.enable_beacon) {
  1742. changed |= BSS_CHANGED_BEACON |
  1743. BSS_CHANGED_BEACON_ENABLED;
  1744. ieee80211_bss_info_change_notify(sdata, changed);
  1745. }
  1746. break;
  1747. case NL80211_IFTYPE_NAN:
  1748. res = ieee80211_reconfig_nan(sdata);
  1749. if (res < 0) {
  1750. ieee80211_handle_reconfig_failure(local);
  1751. return res;
  1752. }
  1753. break;
  1754. case NL80211_IFTYPE_WDS:
  1755. case NL80211_IFTYPE_AP_VLAN:
  1756. case NL80211_IFTYPE_MONITOR:
  1757. case NL80211_IFTYPE_P2P_DEVICE:
  1758. /* nothing to do */
  1759. break;
  1760. case NL80211_IFTYPE_UNSPECIFIED:
  1761. case NUM_NL80211_IFTYPES:
  1762. case NL80211_IFTYPE_P2P_CLIENT:
  1763. case NL80211_IFTYPE_P2P_GO:
  1764. WARN_ON(1);
  1765. break;
  1766. }
  1767. }
  1768. ieee80211_recalc_ps(local);
  1769. /*
  1770. * The sta might be in psm against the ap (e.g. because
  1771. * this was the state before a hw restart), so we
  1772. * explicitly send a null packet in order to make sure
  1773. * it'll sync against the ap (and get out of psm).
  1774. */
  1775. if (!(local->hw.conf.flags & IEEE80211_CONF_PS)) {
  1776. list_for_each_entry(sdata, &local->interfaces, list) {
  1777. if (sdata->vif.type != NL80211_IFTYPE_STATION)
  1778. continue;
  1779. if (!sdata->u.mgd.associated)
  1780. continue;
  1781. ieee80211_send_nullfunc(local, sdata, false);
  1782. }
  1783. }
  1784. /* APs are now beaconing, add back stations */
  1785. mutex_lock(&local->sta_mtx);
  1786. list_for_each_entry(sta, &local->sta_list, list) {
  1787. enum ieee80211_sta_state state;
  1788. if (!sta->uploaded)
  1789. continue;
  1790. if (sta->sdata->vif.type != NL80211_IFTYPE_AP)
  1791. continue;
  1792. for (state = IEEE80211_STA_NOTEXIST;
  1793. state < sta->sta_state; state++)
  1794. WARN_ON(drv_sta_state(local, sta->sdata, sta, state,
  1795. state + 1));
  1796. }
  1797. mutex_unlock(&local->sta_mtx);
  1798. /* add back keys */
  1799. list_for_each_entry(sdata, &local->interfaces, list)
  1800. ieee80211_reset_crypto_tx_tailroom(sdata);
  1801. list_for_each_entry(sdata, &local->interfaces, list)
  1802. if (ieee80211_sdata_running(sdata))
  1803. ieee80211_enable_keys(sdata);
  1804. /* Reconfigure sched scan if it was interrupted by FW restart */
  1805. mutex_lock(&local->mtx);
  1806. sched_scan_sdata = rcu_dereference_protected(local->sched_scan_sdata,
  1807. lockdep_is_held(&local->mtx));
  1808. sched_scan_req = rcu_dereference_protected(local->sched_scan_req,
  1809. lockdep_is_held(&local->mtx));
  1810. if (sched_scan_sdata && sched_scan_req)
  1811. /*
  1812. * Sched scan stopped, but we don't want to report it. Instead,
  1813. * we're trying to reschedule. However, if more than one scan
  1814. * plan was set, we cannot reschedule since we don't know which
  1815. * scan plan was currently running (and some scan plans may have
  1816. * already finished).
  1817. */
  1818. if (sched_scan_req->n_scan_plans > 1 ||
  1819. __ieee80211_request_sched_scan_start(sched_scan_sdata,
  1820. sched_scan_req)) {
  1821. RCU_INIT_POINTER(local->sched_scan_sdata, NULL);
  1822. RCU_INIT_POINTER(local->sched_scan_req, NULL);
  1823. sched_scan_stopped = true;
  1824. }
  1825. mutex_unlock(&local->mtx);
  1826. if (sched_scan_stopped)
  1827. cfg80211_sched_scan_stopped_rtnl(local->hw.wiphy, 0);
  1828. wake_up:
  1829. if (local->in_reconfig) {
  1830. local->in_reconfig = false;
  1831. barrier();
  1832. /* Restart deferred ROCs */
  1833. mutex_lock(&local->mtx);
  1834. ieee80211_start_next_roc(local);
  1835. mutex_unlock(&local->mtx);
  1836. }
  1837. if (local->monitors == local->open_count && local->monitors > 0)
  1838. ieee80211_add_virtual_monitor(local);
  1839. /*
  1840. * Clear the WLAN_STA_BLOCK_BA flag so new aggregation
  1841. * sessions can be established after a resume.
  1842. *
  1843. * Also tear down aggregation sessions since reconfiguring
  1844. * them in a hardware restart scenario is not easily done
  1845. * right now, and the hardware will have lost information
  1846. * about the sessions, but we and the AP still think they
  1847. * are active. This is really a workaround though.
  1848. */
  1849. if (ieee80211_hw_check(hw, AMPDU_AGGREGATION)) {
  1850. mutex_lock(&local->sta_mtx);
  1851. list_for_each_entry(sta, &local->sta_list, list) {
  1852. if (!local->resuming)
  1853. ieee80211_sta_tear_down_BA_sessions(
  1854. sta, AGG_STOP_LOCAL_REQUEST);
  1855. clear_sta_flag(sta, WLAN_STA_BLOCK_BA);
  1856. }
  1857. mutex_unlock(&local->sta_mtx);
  1858. }
  1859. ieee80211_wake_queues_by_reason(hw, IEEE80211_MAX_QUEUE_MAP,
  1860. IEEE80211_QUEUE_STOP_REASON_SUSPEND,
  1861. false);
  1862. /*
  1863. * If this is for hw restart things are still running.
  1864. * We may want to change that later, however.
  1865. */
  1866. if (local->open_count && (!suspended || reconfig_due_to_wowlan))
  1867. drv_reconfig_complete(local, IEEE80211_RECONFIG_TYPE_RESTART);
  1868. if (!suspended)
  1869. return 0;
  1870. #ifdef CONFIG_PM
  1871. /* first set suspended false, then resuming */
  1872. local->suspended = false;
  1873. mb();
  1874. local->resuming = false;
  1875. ieee80211_flush_completed_scan(local, false);
  1876. if (local->open_count && !reconfig_due_to_wowlan)
  1877. drv_reconfig_complete(local, IEEE80211_RECONFIG_TYPE_SUSPEND);
  1878. list_for_each_entry(sdata, &local->interfaces, list) {
  1879. if (!ieee80211_sdata_running(sdata))
  1880. continue;
  1881. if (sdata->vif.type == NL80211_IFTYPE_STATION)
  1882. ieee80211_sta_restart(sdata);
  1883. }
  1884. mod_timer(&local->sta_cleanup, jiffies + 1);
  1885. #else
  1886. WARN_ON(1);
  1887. #endif
  1888. return 0;
  1889. }
  1890. void ieee80211_resume_disconnect(struct ieee80211_vif *vif)
  1891. {
  1892. struct ieee80211_sub_if_data *sdata;
  1893. struct ieee80211_local *local;
  1894. struct ieee80211_key *key;
  1895. if (WARN_ON(!vif))
  1896. return;
  1897. sdata = vif_to_sdata(vif);
  1898. local = sdata->local;
  1899. if (WARN_ON(!local->resuming))
  1900. return;
  1901. if (WARN_ON(vif->type != NL80211_IFTYPE_STATION))
  1902. return;
  1903. sdata->flags |= IEEE80211_SDATA_DISCONNECT_RESUME;
  1904. mutex_lock(&local->key_mtx);
  1905. list_for_each_entry(key, &sdata->key_list, list)
  1906. key->flags |= KEY_FLAG_TAINTED;
  1907. mutex_unlock(&local->key_mtx);
  1908. }
  1909. EXPORT_SYMBOL_GPL(ieee80211_resume_disconnect);
  1910. void ieee80211_recalc_smps(struct ieee80211_sub_if_data *sdata)
  1911. {
  1912. struct ieee80211_local *local = sdata->local;
  1913. struct ieee80211_chanctx_conf *chanctx_conf;
  1914. struct ieee80211_chanctx *chanctx;
  1915. mutex_lock(&local->chanctx_mtx);
  1916. chanctx_conf = rcu_dereference_protected(sdata->vif.chanctx_conf,
  1917. lockdep_is_held(&local->chanctx_mtx));
  1918. /*
  1919. * This function can be called from a work, thus it may be possible
  1920. * that the chanctx_conf is removed (due to a disconnection, for
  1921. * example).
  1922. * So nothing should be done in such case.
  1923. */
  1924. if (!chanctx_conf)
  1925. goto unlock;
  1926. chanctx = container_of(chanctx_conf, struct ieee80211_chanctx, conf);
  1927. ieee80211_recalc_smps_chanctx(local, chanctx);
  1928. unlock:
  1929. mutex_unlock(&local->chanctx_mtx);
  1930. }
  1931. void ieee80211_recalc_min_chandef(struct ieee80211_sub_if_data *sdata)
  1932. {
  1933. struct ieee80211_local *local = sdata->local;
  1934. struct ieee80211_chanctx_conf *chanctx_conf;
  1935. struct ieee80211_chanctx *chanctx;
  1936. mutex_lock(&local->chanctx_mtx);
  1937. chanctx_conf = rcu_dereference_protected(sdata->vif.chanctx_conf,
  1938. lockdep_is_held(&local->chanctx_mtx));
  1939. if (WARN_ON_ONCE(!chanctx_conf))
  1940. goto unlock;
  1941. chanctx = container_of(chanctx_conf, struct ieee80211_chanctx, conf);
  1942. ieee80211_recalc_chanctx_min_def(local, chanctx);
  1943. unlock:
  1944. mutex_unlock(&local->chanctx_mtx);
  1945. }
  1946. size_t ieee80211_ie_split_vendor(const u8 *ies, size_t ielen, size_t offset)
  1947. {
  1948. size_t pos = offset;
  1949. while (pos < ielen && ies[pos] != WLAN_EID_VENDOR_SPECIFIC)
  1950. pos += 2 + ies[pos + 1];
  1951. return pos;
  1952. }
  1953. static void _ieee80211_enable_rssi_reports(struct ieee80211_sub_if_data *sdata,
  1954. int rssi_min_thold,
  1955. int rssi_max_thold)
  1956. {
  1957. trace_api_enable_rssi_reports(sdata, rssi_min_thold, rssi_max_thold);
  1958. if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_STATION))
  1959. return;
  1960. /*
  1961. * Scale up threshold values before storing it, as the RSSI averaging
  1962. * algorithm uses a scaled up value as well. Change this scaling
  1963. * factor if the RSSI averaging algorithm changes.
  1964. */
  1965. sdata->u.mgd.rssi_min_thold = rssi_min_thold*16;
  1966. sdata->u.mgd.rssi_max_thold = rssi_max_thold*16;
  1967. }
  1968. void ieee80211_enable_rssi_reports(struct ieee80211_vif *vif,
  1969. int rssi_min_thold,
  1970. int rssi_max_thold)
  1971. {
  1972. struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
  1973. WARN_ON(rssi_min_thold == rssi_max_thold ||
  1974. rssi_min_thold > rssi_max_thold);
  1975. _ieee80211_enable_rssi_reports(sdata, rssi_min_thold,
  1976. rssi_max_thold);
  1977. }
  1978. EXPORT_SYMBOL(ieee80211_enable_rssi_reports);
  1979. void ieee80211_disable_rssi_reports(struct ieee80211_vif *vif)
  1980. {
  1981. struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
  1982. _ieee80211_enable_rssi_reports(sdata, 0, 0);
  1983. }
  1984. EXPORT_SYMBOL(ieee80211_disable_rssi_reports);
  1985. u8 *ieee80211_ie_build_ht_cap(u8 *pos, struct ieee80211_sta_ht_cap *ht_cap,
  1986. u16 cap)
  1987. {
  1988. __le16 tmp;
  1989. *pos++ = WLAN_EID_HT_CAPABILITY;
  1990. *pos++ = sizeof(struct ieee80211_ht_cap);
  1991. memset(pos, 0, sizeof(struct ieee80211_ht_cap));
  1992. /* capability flags */
  1993. tmp = cpu_to_le16(cap);
  1994. memcpy(pos, &tmp, sizeof(u16));
  1995. pos += sizeof(u16);
  1996. /* AMPDU parameters */
  1997. *pos++ = ht_cap->ampdu_factor |
  1998. (ht_cap->ampdu_density <<
  1999. IEEE80211_HT_AMPDU_PARM_DENSITY_SHIFT);
  2000. /* MCS set */
  2001. memcpy(pos, &ht_cap->mcs, sizeof(ht_cap->mcs));
  2002. pos += sizeof(ht_cap->mcs);
  2003. /* extended capabilities */
  2004. pos += sizeof(__le16);
  2005. /* BF capabilities */
  2006. pos += sizeof(__le32);
  2007. /* antenna selection */
  2008. pos += sizeof(u8);
  2009. return pos;
  2010. }
  2011. u8 *ieee80211_ie_build_vht_cap(u8 *pos, struct ieee80211_sta_vht_cap *vht_cap,
  2012. u32 cap)
  2013. {
  2014. __le32 tmp;
  2015. *pos++ = WLAN_EID_VHT_CAPABILITY;
  2016. *pos++ = sizeof(struct ieee80211_vht_cap);
  2017. memset(pos, 0, sizeof(struct ieee80211_vht_cap));
  2018. /* capability flags */
  2019. tmp = cpu_to_le32(cap);
  2020. memcpy(pos, &tmp, sizeof(u32));
  2021. pos += sizeof(u32);
  2022. /* VHT MCS set */
  2023. memcpy(pos, &vht_cap->vht_mcs, sizeof(vht_cap->vht_mcs));
  2024. pos += sizeof(vht_cap->vht_mcs);
  2025. return pos;
  2026. }
  2027. u8 *ieee80211_ie_build_ht_oper(u8 *pos, struct ieee80211_sta_ht_cap *ht_cap,
  2028. const struct cfg80211_chan_def *chandef,
  2029. u16 prot_mode, bool rifs_mode)
  2030. {
  2031. struct ieee80211_ht_operation *ht_oper;
  2032. /* Build HT Information */
  2033. *pos++ = WLAN_EID_HT_OPERATION;
  2034. *pos++ = sizeof(struct ieee80211_ht_operation);
  2035. ht_oper = (struct ieee80211_ht_operation *)pos;
  2036. ht_oper->primary_chan = ieee80211_frequency_to_channel(
  2037. chandef->chan->center_freq);
  2038. switch (chandef->width) {
  2039. case NL80211_CHAN_WIDTH_160:
  2040. case NL80211_CHAN_WIDTH_80P80:
  2041. case NL80211_CHAN_WIDTH_80:
  2042. case NL80211_CHAN_WIDTH_40:
  2043. if (chandef->center_freq1 > chandef->chan->center_freq)
  2044. ht_oper->ht_param = IEEE80211_HT_PARAM_CHA_SEC_ABOVE;
  2045. else
  2046. ht_oper->ht_param = IEEE80211_HT_PARAM_CHA_SEC_BELOW;
  2047. break;
  2048. default:
  2049. ht_oper->ht_param = IEEE80211_HT_PARAM_CHA_SEC_NONE;
  2050. break;
  2051. }
  2052. if (ht_cap->cap & IEEE80211_HT_CAP_SUP_WIDTH_20_40 &&
  2053. chandef->width != NL80211_CHAN_WIDTH_20_NOHT &&
  2054. chandef->width != NL80211_CHAN_WIDTH_20)
  2055. ht_oper->ht_param |= IEEE80211_HT_PARAM_CHAN_WIDTH_ANY;
  2056. if (rifs_mode)
  2057. ht_oper->ht_param |= IEEE80211_HT_PARAM_RIFS_MODE;
  2058. ht_oper->operation_mode = cpu_to_le16(prot_mode);
  2059. ht_oper->stbc_param = 0x0000;
  2060. /* It seems that Basic MCS set and Supported MCS set
  2061. are identical for the first 10 bytes */
  2062. memset(&ht_oper->basic_set, 0, 16);
  2063. memcpy(&ht_oper->basic_set, &ht_cap->mcs, 10);
  2064. return pos + sizeof(struct ieee80211_ht_operation);
  2065. }
  2066. void ieee80211_ie_build_wide_bw_cs(u8 *pos,
  2067. const struct cfg80211_chan_def *chandef)
  2068. {
  2069. *pos++ = WLAN_EID_WIDE_BW_CHANNEL_SWITCH; /* EID */
  2070. *pos++ = 3; /* IE length */
  2071. /* New channel width */
  2072. switch (chandef->width) {
  2073. case NL80211_CHAN_WIDTH_80:
  2074. *pos++ = IEEE80211_VHT_CHANWIDTH_80MHZ;
  2075. break;
  2076. case NL80211_CHAN_WIDTH_160:
  2077. *pos++ = IEEE80211_VHT_CHANWIDTH_160MHZ;
  2078. break;
  2079. case NL80211_CHAN_WIDTH_80P80:
  2080. *pos++ = IEEE80211_VHT_CHANWIDTH_80P80MHZ;
  2081. break;
  2082. default:
  2083. *pos++ = IEEE80211_VHT_CHANWIDTH_USE_HT;
  2084. }
  2085. /* new center frequency segment 0 */
  2086. *pos++ = ieee80211_frequency_to_channel(chandef->center_freq1);
  2087. /* new center frequency segment 1 */
  2088. if (chandef->center_freq2)
  2089. *pos++ = ieee80211_frequency_to_channel(chandef->center_freq2);
  2090. else
  2091. *pos++ = 0;
  2092. }
  2093. u8 *ieee80211_ie_build_vht_oper(u8 *pos, struct ieee80211_sta_vht_cap *vht_cap,
  2094. const struct cfg80211_chan_def *chandef)
  2095. {
  2096. struct ieee80211_vht_operation *vht_oper;
  2097. *pos++ = WLAN_EID_VHT_OPERATION;
  2098. *pos++ = sizeof(struct ieee80211_vht_operation);
  2099. vht_oper = (struct ieee80211_vht_operation *)pos;
  2100. vht_oper->center_freq_seg0_idx = ieee80211_frequency_to_channel(
  2101. chandef->center_freq1);
  2102. if (chandef->center_freq2)
  2103. vht_oper->center_freq_seg1_idx =
  2104. ieee80211_frequency_to_channel(chandef->center_freq2);
  2105. else
  2106. vht_oper->center_freq_seg1_idx = 0x00;
  2107. switch (chandef->width) {
  2108. case NL80211_CHAN_WIDTH_160:
  2109. /*
  2110. * Convert 160 MHz channel width to new style as interop
  2111. * workaround.
  2112. */
  2113. vht_oper->chan_width = IEEE80211_VHT_CHANWIDTH_80MHZ;
  2114. vht_oper->center_freq_seg1_idx = vht_oper->center_freq_seg0_idx;
  2115. if (chandef->chan->center_freq < chandef->center_freq1)
  2116. vht_oper->center_freq_seg0_idx -= 8;
  2117. else
  2118. vht_oper->center_freq_seg0_idx += 8;
  2119. break;
  2120. case NL80211_CHAN_WIDTH_80P80:
  2121. /*
  2122. * Convert 80+80 MHz channel width to new style as interop
  2123. * workaround.
  2124. */
  2125. vht_oper->chan_width = IEEE80211_VHT_CHANWIDTH_80MHZ;
  2126. break;
  2127. case NL80211_CHAN_WIDTH_80:
  2128. vht_oper->chan_width = IEEE80211_VHT_CHANWIDTH_80MHZ;
  2129. break;
  2130. default:
  2131. vht_oper->chan_width = IEEE80211_VHT_CHANWIDTH_USE_HT;
  2132. break;
  2133. }
  2134. /* don't require special VHT peer rates */
  2135. vht_oper->basic_mcs_set = cpu_to_le16(0xffff);
  2136. return pos + sizeof(struct ieee80211_vht_operation);
  2137. }
  2138. bool ieee80211_chandef_ht_oper(const struct ieee80211_ht_operation *ht_oper,
  2139. struct cfg80211_chan_def *chandef)
  2140. {
  2141. enum nl80211_channel_type channel_type;
  2142. if (!ht_oper)
  2143. return false;
  2144. switch (ht_oper->ht_param & IEEE80211_HT_PARAM_CHA_SEC_OFFSET) {
  2145. case IEEE80211_HT_PARAM_CHA_SEC_NONE:
  2146. channel_type = NL80211_CHAN_HT20;
  2147. break;
  2148. case IEEE80211_HT_PARAM_CHA_SEC_ABOVE:
  2149. channel_type = NL80211_CHAN_HT40PLUS;
  2150. break;
  2151. case IEEE80211_HT_PARAM_CHA_SEC_BELOW:
  2152. channel_type = NL80211_CHAN_HT40MINUS;
  2153. break;
  2154. default:
  2155. channel_type = NL80211_CHAN_NO_HT;
  2156. return false;
  2157. }
  2158. cfg80211_chandef_create(chandef, chandef->chan, channel_type);
  2159. return true;
  2160. }
  2161. bool ieee80211_chandef_vht_oper(const struct ieee80211_vht_operation *oper,
  2162. struct cfg80211_chan_def *chandef)
  2163. {
  2164. struct cfg80211_chan_def new = *chandef;
  2165. int cf1, cf2;
  2166. if (!oper)
  2167. return false;
  2168. cf1 = ieee80211_channel_to_frequency(oper->center_freq_seg0_idx,
  2169. chandef->chan->band);
  2170. cf2 = ieee80211_channel_to_frequency(oper->center_freq_seg1_idx,
  2171. chandef->chan->band);
  2172. switch (oper->chan_width) {
  2173. case IEEE80211_VHT_CHANWIDTH_USE_HT:
  2174. break;
  2175. case IEEE80211_VHT_CHANWIDTH_80MHZ:
  2176. new.width = NL80211_CHAN_WIDTH_80;
  2177. new.center_freq1 = cf1;
  2178. /* If needed, adjust based on the newer interop workaround. */
  2179. if (oper->center_freq_seg1_idx) {
  2180. unsigned int diff;
  2181. diff = abs(oper->center_freq_seg1_idx -
  2182. oper->center_freq_seg0_idx);
  2183. if (diff == 8) {
  2184. new.width = NL80211_CHAN_WIDTH_160;
  2185. new.center_freq1 = cf2;
  2186. } else if (diff > 8) {
  2187. new.width = NL80211_CHAN_WIDTH_80P80;
  2188. new.center_freq2 = cf2;
  2189. }
  2190. }
  2191. break;
  2192. case IEEE80211_VHT_CHANWIDTH_160MHZ:
  2193. new.width = NL80211_CHAN_WIDTH_160;
  2194. new.center_freq1 = cf1;
  2195. break;
  2196. case IEEE80211_VHT_CHANWIDTH_80P80MHZ:
  2197. new.width = NL80211_CHAN_WIDTH_80P80;
  2198. new.center_freq1 = cf1;
  2199. new.center_freq2 = cf2;
  2200. break;
  2201. default:
  2202. return false;
  2203. }
  2204. if (!cfg80211_chandef_valid(&new))
  2205. return false;
  2206. *chandef = new;
  2207. return true;
  2208. }
  2209. int ieee80211_parse_bitrates(struct cfg80211_chan_def *chandef,
  2210. const struct ieee80211_supported_band *sband,
  2211. const u8 *srates, int srates_len, u32 *rates)
  2212. {
  2213. u32 rate_flags = ieee80211_chandef_rate_flags(chandef);
  2214. int shift = ieee80211_chandef_get_shift(chandef);
  2215. struct ieee80211_rate *br;
  2216. int brate, rate, i, j, count = 0;
  2217. *rates = 0;
  2218. for (i = 0; i < srates_len; i++) {
  2219. rate = srates[i] & 0x7f;
  2220. for (j = 0; j < sband->n_bitrates; j++) {
  2221. br = &sband->bitrates[j];
  2222. if ((rate_flags & br->flags) != rate_flags)
  2223. continue;
  2224. brate = DIV_ROUND_UP(br->bitrate, (1 << shift) * 5);
  2225. if (brate == rate) {
  2226. *rates |= BIT(j);
  2227. count++;
  2228. break;
  2229. }
  2230. }
  2231. }
  2232. return count;
  2233. }
  2234. int ieee80211_add_srates_ie(struct ieee80211_sub_if_data *sdata,
  2235. struct sk_buff *skb, bool need_basic,
  2236. enum nl80211_band band)
  2237. {
  2238. struct ieee80211_local *local = sdata->local;
  2239. struct ieee80211_supported_band *sband;
  2240. int rate, shift;
  2241. u8 i, rates, *pos;
  2242. u32 basic_rates = sdata->vif.bss_conf.basic_rates;
  2243. u32 rate_flags;
  2244. shift = ieee80211_vif_get_shift(&sdata->vif);
  2245. rate_flags = ieee80211_chandef_rate_flags(&sdata->vif.bss_conf.chandef);
  2246. sband = local->hw.wiphy->bands[band];
  2247. rates = 0;
  2248. for (i = 0; i < sband->n_bitrates; i++) {
  2249. if ((rate_flags & sband->bitrates[i].flags) != rate_flags)
  2250. continue;
  2251. rates++;
  2252. }
  2253. if (rates > 8)
  2254. rates = 8;
  2255. if (skb_tailroom(skb) < rates + 2)
  2256. return -ENOMEM;
  2257. pos = skb_put(skb, rates + 2);
  2258. *pos++ = WLAN_EID_SUPP_RATES;
  2259. *pos++ = rates;
  2260. for (i = 0; i < rates; i++) {
  2261. u8 basic = 0;
  2262. if ((rate_flags & sband->bitrates[i].flags) != rate_flags)
  2263. continue;
  2264. if (need_basic && basic_rates & BIT(i))
  2265. basic = 0x80;
  2266. rate = DIV_ROUND_UP(sband->bitrates[i].bitrate,
  2267. 5 * (1 << shift));
  2268. *pos++ = basic | (u8) rate;
  2269. }
  2270. return 0;
  2271. }
  2272. int ieee80211_add_ext_srates_ie(struct ieee80211_sub_if_data *sdata,
  2273. struct sk_buff *skb, bool need_basic,
  2274. enum nl80211_band band)
  2275. {
  2276. struct ieee80211_local *local = sdata->local;
  2277. struct ieee80211_supported_band *sband;
  2278. int rate, shift;
  2279. u8 i, exrates, *pos;
  2280. u32 basic_rates = sdata->vif.bss_conf.basic_rates;
  2281. u32 rate_flags;
  2282. rate_flags = ieee80211_chandef_rate_flags(&sdata->vif.bss_conf.chandef);
  2283. shift = ieee80211_vif_get_shift(&sdata->vif);
  2284. sband = local->hw.wiphy->bands[band];
  2285. exrates = 0;
  2286. for (i = 0; i < sband->n_bitrates; i++) {
  2287. if ((rate_flags & sband->bitrates[i].flags) != rate_flags)
  2288. continue;
  2289. exrates++;
  2290. }
  2291. if (exrates > 8)
  2292. exrates -= 8;
  2293. else
  2294. exrates = 0;
  2295. if (skb_tailroom(skb) < exrates + 2)
  2296. return -ENOMEM;
  2297. if (exrates) {
  2298. pos = skb_put(skb, exrates + 2);
  2299. *pos++ = WLAN_EID_EXT_SUPP_RATES;
  2300. *pos++ = exrates;
  2301. for (i = 8; i < sband->n_bitrates; i++) {
  2302. u8 basic = 0;
  2303. if ((rate_flags & sband->bitrates[i].flags)
  2304. != rate_flags)
  2305. continue;
  2306. if (need_basic && basic_rates & BIT(i))
  2307. basic = 0x80;
  2308. rate = DIV_ROUND_UP(sband->bitrates[i].bitrate,
  2309. 5 * (1 << shift));
  2310. *pos++ = basic | (u8) rate;
  2311. }
  2312. }
  2313. return 0;
  2314. }
  2315. int ieee80211_ave_rssi(struct ieee80211_vif *vif)
  2316. {
  2317. struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
  2318. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  2319. if (WARN_ON_ONCE(sdata->vif.type != NL80211_IFTYPE_STATION)) {
  2320. /* non-managed type inferfaces */
  2321. return 0;
  2322. }
  2323. return -ewma_beacon_signal_read(&ifmgd->ave_beacon_signal);
  2324. }
  2325. EXPORT_SYMBOL_GPL(ieee80211_ave_rssi);
  2326. u8 ieee80211_mcs_to_chains(const struct ieee80211_mcs_info *mcs)
  2327. {
  2328. if (!mcs)
  2329. return 1;
  2330. /* TODO: consider rx_highest */
  2331. if (mcs->rx_mask[3])
  2332. return 4;
  2333. if (mcs->rx_mask[2])
  2334. return 3;
  2335. if (mcs->rx_mask[1])
  2336. return 2;
  2337. return 1;
  2338. }
  2339. /**
  2340. * ieee80211_calculate_rx_timestamp - calculate timestamp in frame
  2341. * @local: mac80211 hw info struct
  2342. * @status: RX status
  2343. * @mpdu_len: total MPDU length (including FCS)
  2344. * @mpdu_offset: offset into MPDU to calculate timestamp at
  2345. *
  2346. * This function calculates the RX timestamp at the given MPDU offset, taking
  2347. * into account what the RX timestamp was. An offset of 0 will just normalize
  2348. * the timestamp to TSF at beginning of MPDU reception.
  2349. */
  2350. u64 ieee80211_calculate_rx_timestamp(struct ieee80211_local *local,
  2351. struct ieee80211_rx_status *status,
  2352. unsigned int mpdu_len,
  2353. unsigned int mpdu_offset)
  2354. {
  2355. u64 ts = status->mactime;
  2356. struct rate_info ri;
  2357. u16 rate;
  2358. if (WARN_ON(!ieee80211_have_rx_timestamp(status)))
  2359. return 0;
  2360. memset(&ri, 0, sizeof(ri));
  2361. /* Fill cfg80211 rate info */
  2362. switch (status->encoding) {
  2363. case RX_ENC_HT:
  2364. ri.mcs = status->rate_idx;
  2365. ri.flags |= RATE_INFO_FLAGS_MCS;
  2366. ri.bw = status->bw;
  2367. if (status->enc_flags & RX_ENC_FLAG_SHORT_GI)
  2368. ri.flags |= RATE_INFO_FLAGS_SHORT_GI;
  2369. break;
  2370. case RX_ENC_VHT:
  2371. ri.flags |= RATE_INFO_FLAGS_VHT_MCS;
  2372. ri.mcs = status->rate_idx;
  2373. ri.nss = status->nss;
  2374. ri.bw = status->bw;
  2375. if (status->enc_flags & RX_ENC_FLAG_SHORT_GI)
  2376. ri.flags |= RATE_INFO_FLAGS_SHORT_GI;
  2377. break;
  2378. default:
  2379. WARN_ON(1);
  2380. /* fall through */
  2381. case RX_ENC_LEGACY: {
  2382. struct ieee80211_supported_band *sband;
  2383. int shift = 0;
  2384. int bitrate;
  2385. ri.bw = status->bw;
  2386. switch (status->bw) {
  2387. case RATE_INFO_BW_10:
  2388. shift = 1;
  2389. break;
  2390. case RATE_INFO_BW_5:
  2391. shift = 2;
  2392. break;
  2393. }
  2394. sband = local->hw.wiphy->bands[status->band];
  2395. bitrate = sband->bitrates[status->rate_idx].bitrate;
  2396. ri.legacy = DIV_ROUND_UP(bitrate, (1 << shift));
  2397. if (status->flag & RX_FLAG_MACTIME_PLCP_START) {
  2398. /* TODO: handle HT/VHT preambles */
  2399. if (status->band == NL80211_BAND_5GHZ) {
  2400. ts += 20 << shift;
  2401. mpdu_offset += 2;
  2402. } else if (status->enc_flags & RX_ENC_FLAG_SHORTPRE) {
  2403. ts += 96;
  2404. } else {
  2405. ts += 192;
  2406. }
  2407. }
  2408. break;
  2409. }
  2410. }
  2411. rate = cfg80211_calculate_bitrate(&ri);
  2412. if (WARN_ONCE(!rate,
  2413. "Invalid bitrate: flags=0x%llx, idx=%d, vht_nss=%d\n",
  2414. (unsigned long long)status->flag, status->rate_idx,
  2415. status->nss))
  2416. return 0;
  2417. /* rewind from end of MPDU */
  2418. if (status->flag & RX_FLAG_MACTIME_END)
  2419. ts -= mpdu_len * 8 * 10 / rate;
  2420. ts += mpdu_offset * 8 * 10 / rate;
  2421. return ts;
  2422. }
  2423. void ieee80211_dfs_cac_cancel(struct ieee80211_local *local)
  2424. {
  2425. struct ieee80211_sub_if_data *sdata;
  2426. struct cfg80211_chan_def chandef;
  2427. /* for interface list, to avoid linking iflist_mtx and chanctx_mtx */
  2428. ASSERT_RTNL();
  2429. mutex_lock(&local->mtx);
  2430. list_for_each_entry(sdata, &local->interfaces, list) {
  2431. /* it might be waiting for the local->mtx, but then
  2432. * by the time it gets it, sdata->wdev.cac_started
  2433. * will no longer be true
  2434. */
  2435. cancel_delayed_work(&sdata->dfs_cac_timer_work);
  2436. if (sdata->wdev.cac_started) {
  2437. chandef = sdata->vif.bss_conf.chandef;
  2438. ieee80211_vif_release_channel(sdata);
  2439. cfg80211_cac_event(sdata->dev,
  2440. &chandef,
  2441. NL80211_RADAR_CAC_ABORTED,
  2442. GFP_KERNEL);
  2443. }
  2444. }
  2445. mutex_unlock(&local->mtx);
  2446. }
  2447. void ieee80211_dfs_radar_detected_work(struct work_struct *work)
  2448. {
  2449. struct ieee80211_local *local =
  2450. container_of(work, struct ieee80211_local, radar_detected_work);
  2451. struct cfg80211_chan_def chandef = local->hw.conf.chandef;
  2452. struct ieee80211_chanctx *ctx;
  2453. int num_chanctx = 0;
  2454. mutex_lock(&local->chanctx_mtx);
  2455. list_for_each_entry(ctx, &local->chanctx_list, list) {
  2456. if (ctx->replace_state == IEEE80211_CHANCTX_REPLACES_OTHER)
  2457. continue;
  2458. num_chanctx++;
  2459. chandef = ctx->conf.def;
  2460. }
  2461. mutex_unlock(&local->chanctx_mtx);
  2462. rtnl_lock();
  2463. ieee80211_dfs_cac_cancel(local);
  2464. rtnl_unlock();
  2465. if (num_chanctx > 1)
  2466. /* XXX: multi-channel is not supported yet */
  2467. WARN_ON(1);
  2468. else
  2469. cfg80211_radar_event(local->hw.wiphy, &chandef, GFP_KERNEL);
  2470. }
  2471. void ieee80211_radar_detected(struct ieee80211_hw *hw)
  2472. {
  2473. struct ieee80211_local *local = hw_to_local(hw);
  2474. trace_api_radar_detected(local);
  2475. schedule_work(&local->radar_detected_work);
  2476. }
  2477. EXPORT_SYMBOL(ieee80211_radar_detected);
  2478. u32 ieee80211_chandef_downgrade(struct cfg80211_chan_def *c)
  2479. {
  2480. u32 ret;
  2481. int tmp;
  2482. switch (c->width) {
  2483. case NL80211_CHAN_WIDTH_20:
  2484. c->width = NL80211_CHAN_WIDTH_20_NOHT;
  2485. ret = IEEE80211_STA_DISABLE_HT | IEEE80211_STA_DISABLE_VHT;
  2486. break;
  2487. case NL80211_CHAN_WIDTH_40:
  2488. c->width = NL80211_CHAN_WIDTH_20;
  2489. c->center_freq1 = c->chan->center_freq;
  2490. ret = IEEE80211_STA_DISABLE_40MHZ |
  2491. IEEE80211_STA_DISABLE_VHT;
  2492. break;
  2493. case NL80211_CHAN_WIDTH_80:
  2494. tmp = (30 + c->chan->center_freq - c->center_freq1)/20;
  2495. /* n_P40 */
  2496. tmp /= 2;
  2497. /* freq_P40 */
  2498. c->center_freq1 = c->center_freq1 - 20 + 40 * tmp;
  2499. c->width = NL80211_CHAN_WIDTH_40;
  2500. ret = IEEE80211_STA_DISABLE_VHT;
  2501. break;
  2502. case NL80211_CHAN_WIDTH_80P80:
  2503. c->center_freq2 = 0;
  2504. c->width = NL80211_CHAN_WIDTH_80;
  2505. ret = IEEE80211_STA_DISABLE_80P80MHZ |
  2506. IEEE80211_STA_DISABLE_160MHZ;
  2507. break;
  2508. case NL80211_CHAN_WIDTH_160:
  2509. /* n_P20 */
  2510. tmp = (70 + c->chan->center_freq - c->center_freq1)/20;
  2511. /* n_P80 */
  2512. tmp /= 4;
  2513. c->center_freq1 = c->center_freq1 - 40 + 80 * tmp;
  2514. c->width = NL80211_CHAN_WIDTH_80;
  2515. ret = IEEE80211_STA_DISABLE_80P80MHZ |
  2516. IEEE80211_STA_DISABLE_160MHZ;
  2517. break;
  2518. default:
  2519. case NL80211_CHAN_WIDTH_20_NOHT:
  2520. WARN_ON_ONCE(1);
  2521. c->width = NL80211_CHAN_WIDTH_20_NOHT;
  2522. ret = IEEE80211_STA_DISABLE_HT | IEEE80211_STA_DISABLE_VHT;
  2523. break;
  2524. case NL80211_CHAN_WIDTH_5:
  2525. case NL80211_CHAN_WIDTH_10:
  2526. WARN_ON_ONCE(1);
  2527. /* keep c->width */
  2528. ret = IEEE80211_STA_DISABLE_HT | IEEE80211_STA_DISABLE_VHT;
  2529. break;
  2530. }
  2531. WARN_ON_ONCE(!cfg80211_chandef_valid(c));
  2532. return ret;
  2533. }
  2534. /*
  2535. * Returns true if smps_mode_new is strictly more restrictive than
  2536. * smps_mode_old.
  2537. */
  2538. bool ieee80211_smps_is_restrictive(enum ieee80211_smps_mode smps_mode_old,
  2539. enum ieee80211_smps_mode smps_mode_new)
  2540. {
  2541. if (WARN_ON_ONCE(smps_mode_old == IEEE80211_SMPS_AUTOMATIC ||
  2542. smps_mode_new == IEEE80211_SMPS_AUTOMATIC))
  2543. return false;
  2544. switch (smps_mode_old) {
  2545. case IEEE80211_SMPS_STATIC:
  2546. return false;
  2547. case IEEE80211_SMPS_DYNAMIC:
  2548. return smps_mode_new == IEEE80211_SMPS_STATIC;
  2549. case IEEE80211_SMPS_OFF:
  2550. return smps_mode_new != IEEE80211_SMPS_OFF;
  2551. default:
  2552. WARN_ON(1);
  2553. }
  2554. return false;
  2555. }
  2556. int ieee80211_send_action_csa(struct ieee80211_sub_if_data *sdata,
  2557. struct cfg80211_csa_settings *csa_settings)
  2558. {
  2559. struct sk_buff *skb;
  2560. struct ieee80211_mgmt *mgmt;
  2561. struct ieee80211_local *local = sdata->local;
  2562. int freq;
  2563. int hdr_len = offsetof(struct ieee80211_mgmt, u.action.u.chan_switch) +
  2564. sizeof(mgmt->u.action.u.chan_switch);
  2565. u8 *pos;
  2566. if (sdata->vif.type != NL80211_IFTYPE_ADHOC &&
  2567. sdata->vif.type != NL80211_IFTYPE_MESH_POINT)
  2568. return -EOPNOTSUPP;
  2569. skb = dev_alloc_skb(local->tx_headroom + hdr_len +
  2570. 5 + /* channel switch announcement element */
  2571. 3 + /* secondary channel offset element */
  2572. 5 + /* wide bandwidth channel switch announcement */
  2573. 8); /* mesh channel switch parameters element */
  2574. if (!skb)
  2575. return -ENOMEM;
  2576. skb_reserve(skb, local->tx_headroom);
  2577. mgmt = skb_put_zero(skb, hdr_len);
  2578. mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
  2579. IEEE80211_STYPE_ACTION);
  2580. eth_broadcast_addr(mgmt->da);
  2581. memcpy(mgmt->sa, sdata->vif.addr, ETH_ALEN);
  2582. if (ieee80211_vif_is_mesh(&sdata->vif)) {
  2583. memcpy(mgmt->bssid, sdata->vif.addr, ETH_ALEN);
  2584. } else {
  2585. struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
  2586. memcpy(mgmt->bssid, ifibss->bssid, ETH_ALEN);
  2587. }
  2588. mgmt->u.action.category = WLAN_CATEGORY_SPECTRUM_MGMT;
  2589. mgmt->u.action.u.chan_switch.action_code = WLAN_ACTION_SPCT_CHL_SWITCH;
  2590. pos = skb_put(skb, 5);
  2591. *pos++ = WLAN_EID_CHANNEL_SWITCH; /* EID */
  2592. *pos++ = 3; /* IE length */
  2593. *pos++ = csa_settings->block_tx ? 1 : 0; /* CSA mode */
  2594. freq = csa_settings->chandef.chan->center_freq;
  2595. *pos++ = ieee80211_frequency_to_channel(freq); /* channel */
  2596. *pos++ = csa_settings->count; /* count */
  2597. if (csa_settings->chandef.width == NL80211_CHAN_WIDTH_40) {
  2598. enum nl80211_channel_type ch_type;
  2599. skb_put(skb, 3);
  2600. *pos++ = WLAN_EID_SECONDARY_CHANNEL_OFFSET; /* EID */
  2601. *pos++ = 1; /* IE length */
  2602. ch_type = cfg80211_get_chandef_type(&csa_settings->chandef);
  2603. if (ch_type == NL80211_CHAN_HT40PLUS)
  2604. *pos++ = IEEE80211_HT_PARAM_CHA_SEC_ABOVE;
  2605. else
  2606. *pos++ = IEEE80211_HT_PARAM_CHA_SEC_BELOW;
  2607. }
  2608. if (ieee80211_vif_is_mesh(&sdata->vif)) {
  2609. struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
  2610. skb_put(skb, 8);
  2611. *pos++ = WLAN_EID_CHAN_SWITCH_PARAM; /* EID */
  2612. *pos++ = 6; /* IE length */
  2613. *pos++ = sdata->u.mesh.mshcfg.dot11MeshTTL; /* Mesh TTL */
  2614. *pos = 0x00; /* Mesh Flag: Tx Restrict, Initiator, Reason */
  2615. *pos |= WLAN_EID_CHAN_SWITCH_PARAM_INITIATOR;
  2616. *pos++ |= csa_settings->block_tx ?
  2617. WLAN_EID_CHAN_SWITCH_PARAM_TX_RESTRICT : 0x00;
  2618. put_unaligned_le16(WLAN_REASON_MESH_CHAN, pos); /* Reason Cd */
  2619. pos += 2;
  2620. put_unaligned_le16(ifmsh->pre_value, pos);/* Precedence Value */
  2621. pos += 2;
  2622. }
  2623. if (csa_settings->chandef.width == NL80211_CHAN_WIDTH_80 ||
  2624. csa_settings->chandef.width == NL80211_CHAN_WIDTH_80P80 ||
  2625. csa_settings->chandef.width == NL80211_CHAN_WIDTH_160) {
  2626. skb_put(skb, 5);
  2627. ieee80211_ie_build_wide_bw_cs(pos, &csa_settings->chandef);
  2628. }
  2629. ieee80211_tx_skb(sdata, skb);
  2630. return 0;
  2631. }
  2632. bool ieee80211_cs_valid(const struct ieee80211_cipher_scheme *cs)
  2633. {
  2634. return !(cs == NULL || cs->cipher == 0 ||
  2635. cs->hdr_len < cs->pn_len + cs->pn_off ||
  2636. cs->hdr_len <= cs->key_idx_off ||
  2637. cs->key_idx_shift > 7 ||
  2638. cs->key_idx_mask == 0);
  2639. }
  2640. bool ieee80211_cs_list_valid(const struct ieee80211_cipher_scheme *cs, int n)
  2641. {
  2642. int i;
  2643. /* Ensure we have enough iftype bitmap space for all iftype values */
  2644. WARN_ON((NUM_NL80211_IFTYPES / 8 + 1) > sizeof(cs[0].iftype));
  2645. for (i = 0; i < n; i++)
  2646. if (!ieee80211_cs_valid(&cs[i]))
  2647. return false;
  2648. return true;
  2649. }
  2650. const struct ieee80211_cipher_scheme *
  2651. ieee80211_cs_get(struct ieee80211_local *local, u32 cipher,
  2652. enum nl80211_iftype iftype)
  2653. {
  2654. const struct ieee80211_cipher_scheme *l = local->hw.cipher_schemes;
  2655. int n = local->hw.n_cipher_schemes;
  2656. int i;
  2657. const struct ieee80211_cipher_scheme *cs = NULL;
  2658. for (i = 0; i < n; i++) {
  2659. if (l[i].cipher == cipher) {
  2660. cs = &l[i];
  2661. break;
  2662. }
  2663. }
  2664. if (!cs || !(cs->iftype & BIT(iftype)))
  2665. return NULL;
  2666. return cs;
  2667. }
  2668. int ieee80211_cs_headroom(struct ieee80211_local *local,
  2669. struct cfg80211_crypto_settings *crypto,
  2670. enum nl80211_iftype iftype)
  2671. {
  2672. const struct ieee80211_cipher_scheme *cs;
  2673. int headroom = IEEE80211_ENCRYPT_HEADROOM;
  2674. int i;
  2675. for (i = 0; i < crypto->n_ciphers_pairwise; i++) {
  2676. cs = ieee80211_cs_get(local, crypto->ciphers_pairwise[i],
  2677. iftype);
  2678. if (cs && headroom < cs->hdr_len)
  2679. headroom = cs->hdr_len;
  2680. }
  2681. cs = ieee80211_cs_get(local, crypto->cipher_group, iftype);
  2682. if (cs && headroom < cs->hdr_len)
  2683. headroom = cs->hdr_len;
  2684. return headroom;
  2685. }
  2686. static bool
  2687. ieee80211_extend_noa_desc(struct ieee80211_noa_data *data, u32 tsf, int i)
  2688. {
  2689. s32 end = data->desc[i].start + data->desc[i].duration - (tsf + 1);
  2690. int skip;
  2691. if (end > 0)
  2692. return false;
  2693. /* One shot NOA */
  2694. if (data->count[i] == 1)
  2695. return false;
  2696. if (data->desc[i].interval == 0)
  2697. return false;
  2698. /* End time is in the past, check for repetitions */
  2699. skip = DIV_ROUND_UP(-end, data->desc[i].interval);
  2700. if (data->count[i] < 255) {
  2701. if (data->count[i] <= skip) {
  2702. data->count[i] = 0;
  2703. return false;
  2704. }
  2705. data->count[i] -= skip;
  2706. }
  2707. data->desc[i].start += skip * data->desc[i].interval;
  2708. return true;
  2709. }
  2710. static bool
  2711. ieee80211_extend_absent_time(struct ieee80211_noa_data *data, u32 tsf,
  2712. s32 *offset)
  2713. {
  2714. bool ret = false;
  2715. int i;
  2716. for (i = 0; i < IEEE80211_P2P_NOA_DESC_MAX; i++) {
  2717. s32 cur;
  2718. if (!data->count[i])
  2719. continue;
  2720. if (ieee80211_extend_noa_desc(data, tsf + *offset, i))
  2721. ret = true;
  2722. cur = data->desc[i].start - tsf;
  2723. if (cur > *offset)
  2724. continue;
  2725. cur = data->desc[i].start + data->desc[i].duration - tsf;
  2726. if (cur > *offset)
  2727. *offset = cur;
  2728. }
  2729. return ret;
  2730. }
  2731. static u32
  2732. ieee80211_get_noa_absent_time(struct ieee80211_noa_data *data, u32 tsf)
  2733. {
  2734. s32 offset = 0;
  2735. int tries = 0;
  2736. /*
  2737. * arbitrary limit, used to avoid infinite loops when combined NoA
  2738. * descriptors cover the full time period.
  2739. */
  2740. int max_tries = 5;
  2741. ieee80211_extend_absent_time(data, tsf, &offset);
  2742. do {
  2743. if (!ieee80211_extend_absent_time(data, tsf, &offset))
  2744. break;
  2745. tries++;
  2746. } while (tries < max_tries);
  2747. return offset;
  2748. }
  2749. void ieee80211_update_p2p_noa(struct ieee80211_noa_data *data, u32 tsf)
  2750. {
  2751. u32 next_offset = BIT(31) - 1;
  2752. int i;
  2753. data->absent = 0;
  2754. data->has_next_tsf = false;
  2755. for (i = 0; i < IEEE80211_P2P_NOA_DESC_MAX; i++) {
  2756. s32 start;
  2757. if (!data->count[i])
  2758. continue;
  2759. ieee80211_extend_noa_desc(data, tsf, i);
  2760. start = data->desc[i].start - tsf;
  2761. if (start <= 0)
  2762. data->absent |= BIT(i);
  2763. if (next_offset > start)
  2764. next_offset = start;
  2765. data->has_next_tsf = true;
  2766. }
  2767. if (data->absent)
  2768. next_offset = ieee80211_get_noa_absent_time(data, tsf);
  2769. data->next_tsf = tsf + next_offset;
  2770. }
  2771. EXPORT_SYMBOL(ieee80211_update_p2p_noa);
  2772. int ieee80211_parse_p2p_noa(const struct ieee80211_p2p_noa_attr *attr,
  2773. struct ieee80211_noa_data *data, u32 tsf)
  2774. {
  2775. int ret = 0;
  2776. int i;
  2777. memset(data, 0, sizeof(*data));
  2778. for (i = 0; i < IEEE80211_P2P_NOA_DESC_MAX; i++) {
  2779. const struct ieee80211_p2p_noa_desc *desc = &attr->desc[i];
  2780. if (!desc->count || !desc->duration)
  2781. continue;
  2782. data->count[i] = desc->count;
  2783. data->desc[i].start = le32_to_cpu(desc->start_time);
  2784. data->desc[i].duration = le32_to_cpu(desc->duration);
  2785. data->desc[i].interval = le32_to_cpu(desc->interval);
  2786. if (data->count[i] > 1 &&
  2787. data->desc[i].interval < data->desc[i].duration)
  2788. continue;
  2789. ieee80211_extend_noa_desc(data, tsf, i);
  2790. ret++;
  2791. }
  2792. if (ret)
  2793. ieee80211_update_p2p_noa(data, tsf);
  2794. return ret;
  2795. }
  2796. EXPORT_SYMBOL(ieee80211_parse_p2p_noa);
  2797. void ieee80211_recalc_dtim(struct ieee80211_local *local,
  2798. struct ieee80211_sub_if_data *sdata)
  2799. {
  2800. u64 tsf = drv_get_tsf(local, sdata);
  2801. u64 dtim_count = 0;
  2802. u16 beacon_int = sdata->vif.bss_conf.beacon_int * 1024;
  2803. u8 dtim_period = sdata->vif.bss_conf.dtim_period;
  2804. struct ps_data *ps;
  2805. u8 bcns_from_dtim;
  2806. if (tsf == -1ULL || !beacon_int || !dtim_period)
  2807. return;
  2808. if (sdata->vif.type == NL80211_IFTYPE_AP ||
  2809. sdata->vif.type == NL80211_IFTYPE_AP_VLAN) {
  2810. if (!sdata->bss)
  2811. return;
  2812. ps = &sdata->bss->ps;
  2813. } else if (ieee80211_vif_is_mesh(&sdata->vif)) {
  2814. ps = &sdata->u.mesh.ps;
  2815. } else {
  2816. return;
  2817. }
  2818. /*
  2819. * actually finds last dtim_count, mac80211 will update in
  2820. * __beacon_add_tim().
  2821. * dtim_count = dtim_period - (tsf / bcn_int) % dtim_period
  2822. */
  2823. do_div(tsf, beacon_int);
  2824. bcns_from_dtim = do_div(tsf, dtim_period);
  2825. /* just had a DTIM */
  2826. if (!bcns_from_dtim)
  2827. dtim_count = 0;
  2828. else
  2829. dtim_count = dtim_period - bcns_from_dtim;
  2830. ps->dtim_count = dtim_count;
  2831. }
  2832. static u8 ieee80211_chanctx_radar_detect(struct ieee80211_local *local,
  2833. struct ieee80211_chanctx *ctx)
  2834. {
  2835. struct ieee80211_sub_if_data *sdata;
  2836. u8 radar_detect = 0;
  2837. lockdep_assert_held(&local->chanctx_mtx);
  2838. if (WARN_ON(ctx->replace_state == IEEE80211_CHANCTX_WILL_BE_REPLACED))
  2839. return 0;
  2840. list_for_each_entry(sdata, &ctx->reserved_vifs, reserved_chanctx_list)
  2841. if (sdata->reserved_radar_required)
  2842. radar_detect |= BIT(sdata->reserved_chandef.width);
  2843. /*
  2844. * An in-place reservation context should not have any assigned vifs
  2845. * until it replaces the other context.
  2846. */
  2847. WARN_ON(ctx->replace_state == IEEE80211_CHANCTX_REPLACES_OTHER &&
  2848. !list_empty(&ctx->assigned_vifs));
  2849. list_for_each_entry(sdata, &ctx->assigned_vifs, assigned_chanctx_list)
  2850. if (sdata->radar_required)
  2851. radar_detect |= BIT(sdata->vif.bss_conf.chandef.width);
  2852. return radar_detect;
  2853. }
  2854. int ieee80211_check_combinations(struct ieee80211_sub_if_data *sdata,
  2855. const struct cfg80211_chan_def *chandef,
  2856. enum ieee80211_chanctx_mode chanmode,
  2857. u8 radar_detect)
  2858. {
  2859. struct ieee80211_local *local = sdata->local;
  2860. struct ieee80211_sub_if_data *sdata_iter;
  2861. enum nl80211_iftype iftype = sdata->wdev.iftype;
  2862. struct ieee80211_chanctx *ctx;
  2863. int total = 1;
  2864. struct iface_combination_params params = {
  2865. .radar_detect = radar_detect,
  2866. };
  2867. lockdep_assert_held(&local->chanctx_mtx);
  2868. if (WARN_ON(hweight32(radar_detect) > 1))
  2869. return -EINVAL;
  2870. if (WARN_ON(chandef && chanmode == IEEE80211_CHANCTX_SHARED &&
  2871. !chandef->chan))
  2872. return -EINVAL;
  2873. if (WARN_ON(iftype >= NUM_NL80211_IFTYPES))
  2874. return -EINVAL;
  2875. if (sdata->vif.type == NL80211_IFTYPE_AP ||
  2876. sdata->vif.type == NL80211_IFTYPE_MESH_POINT) {
  2877. /*
  2878. * always passing this is harmless, since it'll be the
  2879. * same value that cfg80211 finds if it finds the same
  2880. * interface ... and that's always allowed
  2881. */
  2882. params.new_beacon_int = sdata->vif.bss_conf.beacon_int;
  2883. }
  2884. /* Always allow software iftypes */
  2885. if (local->hw.wiphy->software_iftypes & BIT(iftype)) {
  2886. if (radar_detect)
  2887. return -EINVAL;
  2888. return 0;
  2889. }
  2890. if (chandef)
  2891. params.num_different_channels = 1;
  2892. if (iftype != NL80211_IFTYPE_UNSPECIFIED)
  2893. params.iftype_num[iftype] = 1;
  2894. list_for_each_entry(ctx, &local->chanctx_list, list) {
  2895. if (ctx->replace_state == IEEE80211_CHANCTX_WILL_BE_REPLACED)
  2896. continue;
  2897. params.radar_detect |=
  2898. ieee80211_chanctx_radar_detect(local, ctx);
  2899. if (ctx->mode == IEEE80211_CHANCTX_EXCLUSIVE) {
  2900. params.num_different_channels++;
  2901. continue;
  2902. }
  2903. if (chandef && chanmode == IEEE80211_CHANCTX_SHARED &&
  2904. cfg80211_chandef_compatible(chandef,
  2905. &ctx->conf.def))
  2906. continue;
  2907. params.num_different_channels++;
  2908. }
  2909. list_for_each_entry_rcu(sdata_iter, &local->interfaces, list) {
  2910. struct wireless_dev *wdev_iter;
  2911. wdev_iter = &sdata_iter->wdev;
  2912. if (sdata_iter == sdata ||
  2913. !ieee80211_sdata_running(sdata_iter) ||
  2914. local->hw.wiphy->software_iftypes & BIT(wdev_iter->iftype))
  2915. continue;
  2916. params.iftype_num[wdev_iter->iftype]++;
  2917. total++;
  2918. }
  2919. if (total == 1 && !params.radar_detect)
  2920. return 0;
  2921. return cfg80211_check_combinations(local->hw.wiphy, &params);
  2922. }
  2923. static void
  2924. ieee80211_iter_max_chans(const struct ieee80211_iface_combination *c,
  2925. void *data)
  2926. {
  2927. u32 *max_num_different_channels = data;
  2928. *max_num_different_channels = max(*max_num_different_channels,
  2929. c->num_different_channels);
  2930. }
  2931. int ieee80211_max_num_channels(struct ieee80211_local *local)
  2932. {
  2933. struct ieee80211_sub_if_data *sdata;
  2934. struct ieee80211_chanctx *ctx;
  2935. u32 max_num_different_channels = 1;
  2936. int err;
  2937. struct iface_combination_params params = {0};
  2938. lockdep_assert_held(&local->chanctx_mtx);
  2939. list_for_each_entry(ctx, &local->chanctx_list, list) {
  2940. if (ctx->replace_state == IEEE80211_CHANCTX_WILL_BE_REPLACED)
  2941. continue;
  2942. params.num_different_channels++;
  2943. params.radar_detect |=
  2944. ieee80211_chanctx_radar_detect(local, ctx);
  2945. }
  2946. list_for_each_entry_rcu(sdata, &local->interfaces, list)
  2947. params.iftype_num[sdata->wdev.iftype]++;
  2948. err = cfg80211_iter_combinations(local->hw.wiphy, &params,
  2949. ieee80211_iter_max_chans,
  2950. &max_num_different_channels);
  2951. if (err < 0)
  2952. return err;
  2953. return max_num_different_channels;
  2954. }
  2955. u8 *ieee80211_add_wmm_info_ie(u8 *buf, u8 qosinfo)
  2956. {
  2957. *buf++ = WLAN_EID_VENDOR_SPECIFIC;
  2958. *buf++ = 7; /* len */
  2959. *buf++ = 0x00; /* Microsoft OUI 00:50:F2 */
  2960. *buf++ = 0x50;
  2961. *buf++ = 0xf2;
  2962. *buf++ = 2; /* WME */
  2963. *buf++ = 0; /* WME info */
  2964. *buf++ = 1; /* WME ver */
  2965. *buf++ = qosinfo; /* U-APSD no in use */
  2966. return buf;
  2967. }
  2968. void ieee80211_txq_get_depth(struct ieee80211_txq *txq,
  2969. unsigned long *frame_cnt,
  2970. unsigned long *byte_cnt)
  2971. {
  2972. struct txq_info *txqi = to_txq_info(txq);
  2973. u32 frag_cnt = 0, frag_bytes = 0;
  2974. struct sk_buff *skb;
  2975. skb_queue_walk(&txqi->frags, skb) {
  2976. frag_cnt++;
  2977. frag_bytes += skb->len;
  2978. }
  2979. if (frame_cnt)
  2980. *frame_cnt = txqi->tin.backlog_packets + frag_cnt;
  2981. if (byte_cnt)
  2982. *byte_cnt = txqi->tin.backlog_bytes + frag_bytes;
  2983. }
  2984. EXPORT_SYMBOL(ieee80211_txq_get_depth);
  2985. const u8 ieee80211_ac_to_qos_mask[IEEE80211_NUM_ACS] = {
  2986. IEEE80211_WMM_IE_STA_QOSINFO_AC_VO,
  2987. IEEE80211_WMM_IE_STA_QOSINFO_AC_VI,
  2988. IEEE80211_WMM_IE_STA_QOSINFO_AC_BE,
  2989. IEEE80211_WMM_IE_STA_QOSINFO_AC_BK
  2990. };