util.c 87 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349
  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 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 ieee80211_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 == IEEE80211_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 ieee80211_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->hw.queues < IEEE80211_NUM_ACS)
  213. n_acs = 1;
  214. list_for_each_entry_rcu(sdata, &local->interfaces, list) {
  215. int ac;
  216. if (!sdata->dev)
  217. continue;
  218. if (sdata->vif.cab_queue != IEEE80211_INVAL_HW_QUEUE &&
  219. local->queue_stop_reasons[sdata->vif.cab_queue] != 0)
  220. continue;
  221. for (ac = 0; ac < n_acs; ac++) {
  222. int ac_queue = sdata->vif.hw_queue[ac];
  223. if (local->ops->wake_tx_queue &&
  224. (atomic_read(&sdata->txqs_len[ac]) >
  225. local->hw.txq_ac_max_pending))
  226. continue;
  227. if (ac_queue == queue ||
  228. (sdata->vif.cab_queue == queue &&
  229. local->queue_stop_reasons[ac_queue] == 0 &&
  230. skb_queue_empty(&local->pending[ac_queue])))
  231. netif_wake_subqueue(sdata->dev, ac);
  232. }
  233. }
  234. }
  235. static void __ieee80211_wake_queue(struct ieee80211_hw *hw, int queue,
  236. enum queue_stop_reason reason,
  237. bool refcounted)
  238. {
  239. struct ieee80211_local *local = hw_to_local(hw);
  240. trace_wake_queue(local, queue, reason);
  241. if (WARN_ON(queue >= hw->queues))
  242. return;
  243. if (!test_bit(reason, &local->queue_stop_reasons[queue]))
  244. return;
  245. if (!refcounted) {
  246. local->q_stop_reasons[queue][reason] = 0;
  247. } else {
  248. local->q_stop_reasons[queue][reason]--;
  249. if (WARN_ON(local->q_stop_reasons[queue][reason] < 0))
  250. local->q_stop_reasons[queue][reason] = 0;
  251. }
  252. if (local->q_stop_reasons[queue][reason] == 0)
  253. __clear_bit(reason, &local->queue_stop_reasons[queue]);
  254. if (local->queue_stop_reasons[queue] != 0)
  255. /* someone still has this queue stopped */
  256. return;
  257. if (skb_queue_empty(&local->pending[queue])) {
  258. rcu_read_lock();
  259. ieee80211_propagate_queue_wake(local, queue);
  260. rcu_read_unlock();
  261. } else
  262. tasklet_schedule(&local->tx_pending_tasklet);
  263. }
  264. void ieee80211_wake_queue_by_reason(struct ieee80211_hw *hw, int queue,
  265. enum queue_stop_reason reason,
  266. bool refcounted)
  267. {
  268. struct ieee80211_local *local = hw_to_local(hw);
  269. unsigned long flags;
  270. spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
  271. __ieee80211_wake_queue(hw, queue, reason, refcounted);
  272. spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
  273. }
  274. void ieee80211_wake_queue(struct ieee80211_hw *hw, int queue)
  275. {
  276. ieee80211_wake_queue_by_reason(hw, queue,
  277. IEEE80211_QUEUE_STOP_REASON_DRIVER,
  278. false);
  279. }
  280. EXPORT_SYMBOL(ieee80211_wake_queue);
  281. static void __ieee80211_stop_queue(struct ieee80211_hw *hw, int queue,
  282. enum queue_stop_reason reason,
  283. bool refcounted)
  284. {
  285. struct ieee80211_local *local = hw_to_local(hw);
  286. struct ieee80211_sub_if_data *sdata;
  287. int n_acs = IEEE80211_NUM_ACS;
  288. trace_stop_queue(local, queue, reason);
  289. if (WARN_ON(queue >= hw->queues))
  290. return;
  291. if (!refcounted)
  292. local->q_stop_reasons[queue][reason] = 1;
  293. else
  294. local->q_stop_reasons[queue][reason]++;
  295. if (__test_and_set_bit(reason, &local->queue_stop_reasons[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. /*
  695. * not listing WLAN_EID_CHANNEL_SWITCH_WRAPPER -- it seems possible
  696. * that if the content gets bigger it might be needed more than once
  697. */
  698. if (test_bit(id, seen_elems)) {
  699. elems->parse_error = true;
  700. left -= elen;
  701. pos += elen;
  702. continue;
  703. }
  704. break;
  705. }
  706. if (calc_crc && id < 64 && (filter & (1ULL << id)))
  707. crc = crc32_be(crc, pos - 2, elen + 2);
  708. elem_parse_failed = false;
  709. switch (id) {
  710. case WLAN_EID_LINK_ID:
  711. if (elen + 2 != sizeof(struct ieee80211_tdls_lnkie)) {
  712. elem_parse_failed = true;
  713. break;
  714. }
  715. elems->lnk_id = (void *)(pos - 2);
  716. break;
  717. case WLAN_EID_CHAN_SWITCH_TIMING:
  718. if (elen != sizeof(struct ieee80211_ch_switch_timing)) {
  719. elem_parse_failed = true;
  720. break;
  721. }
  722. elems->ch_sw_timing = (void *)pos;
  723. break;
  724. case WLAN_EID_EXT_CAPABILITY:
  725. elems->ext_capab = pos;
  726. elems->ext_capab_len = elen;
  727. break;
  728. case WLAN_EID_SSID:
  729. elems->ssid = pos;
  730. elems->ssid_len = elen;
  731. break;
  732. case WLAN_EID_SUPP_RATES:
  733. elems->supp_rates = pos;
  734. elems->supp_rates_len = elen;
  735. break;
  736. case WLAN_EID_DS_PARAMS:
  737. if (elen >= 1)
  738. elems->ds_params = pos;
  739. else
  740. elem_parse_failed = true;
  741. break;
  742. case WLAN_EID_TIM:
  743. if (elen >= sizeof(struct ieee80211_tim_ie)) {
  744. elems->tim = (void *)pos;
  745. elems->tim_len = elen;
  746. } else
  747. elem_parse_failed = true;
  748. break;
  749. case WLAN_EID_CHALLENGE:
  750. elems->challenge = pos;
  751. elems->challenge_len = elen;
  752. break;
  753. case WLAN_EID_VENDOR_SPECIFIC:
  754. if (elen >= 4 && pos[0] == 0x00 && pos[1] == 0x50 &&
  755. pos[2] == 0xf2) {
  756. /* Microsoft OUI (00:50:F2) */
  757. if (calc_crc)
  758. crc = crc32_be(crc, pos - 2, elen + 2);
  759. if (elen >= 5 && pos[3] == 2) {
  760. /* OUI Type 2 - WMM IE */
  761. if (pos[4] == 0) {
  762. elems->wmm_info = pos;
  763. elems->wmm_info_len = elen;
  764. } else if (pos[4] == 1) {
  765. elems->wmm_param = pos;
  766. elems->wmm_param_len = elen;
  767. }
  768. }
  769. }
  770. break;
  771. case WLAN_EID_RSN:
  772. elems->rsn = pos;
  773. elems->rsn_len = elen;
  774. break;
  775. case WLAN_EID_ERP_INFO:
  776. if (elen >= 1)
  777. elems->erp_info = pos;
  778. else
  779. elem_parse_failed = true;
  780. break;
  781. case WLAN_EID_EXT_SUPP_RATES:
  782. elems->ext_supp_rates = pos;
  783. elems->ext_supp_rates_len = elen;
  784. break;
  785. case WLAN_EID_HT_CAPABILITY:
  786. if (elen >= sizeof(struct ieee80211_ht_cap))
  787. elems->ht_cap_elem = (void *)pos;
  788. else
  789. elem_parse_failed = true;
  790. break;
  791. case WLAN_EID_HT_OPERATION:
  792. if (elen >= sizeof(struct ieee80211_ht_operation))
  793. elems->ht_operation = (void *)pos;
  794. else
  795. elem_parse_failed = true;
  796. break;
  797. case WLAN_EID_VHT_CAPABILITY:
  798. if (elen >= sizeof(struct ieee80211_vht_cap))
  799. elems->vht_cap_elem = (void *)pos;
  800. else
  801. elem_parse_failed = true;
  802. break;
  803. case WLAN_EID_VHT_OPERATION:
  804. if (elen >= sizeof(struct ieee80211_vht_operation))
  805. elems->vht_operation = (void *)pos;
  806. else
  807. elem_parse_failed = true;
  808. break;
  809. case WLAN_EID_OPMODE_NOTIF:
  810. if (elen > 0)
  811. elems->opmode_notif = pos;
  812. else
  813. elem_parse_failed = true;
  814. break;
  815. case WLAN_EID_MESH_ID:
  816. elems->mesh_id = pos;
  817. elems->mesh_id_len = elen;
  818. break;
  819. case WLAN_EID_MESH_CONFIG:
  820. if (elen >= sizeof(struct ieee80211_meshconf_ie))
  821. elems->mesh_config = (void *)pos;
  822. else
  823. elem_parse_failed = true;
  824. break;
  825. case WLAN_EID_PEER_MGMT:
  826. elems->peering = pos;
  827. elems->peering_len = elen;
  828. break;
  829. case WLAN_EID_MESH_AWAKE_WINDOW:
  830. if (elen >= 2)
  831. elems->awake_window = (void *)pos;
  832. break;
  833. case WLAN_EID_PREQ:
  834. elems->preq = pos;
  835. elems->preq_len = elen;
  836. break;
  837. case WLAN_EID_PREP:
  838. elems->prep = pos;
  839. elems->prep_len = elen;
  840. break;
  841. case WLAN_EID_PERR:
  842. elems->perr = pos;
  843. elems->perr_len = elen;
  844. break;
  845. case WLAN_EID_RANN:
  846. if (elen >= sizeof(struct ieee80211_rann_ie))
  847. elems->rann = (void *)pos;
  848. else
  849. elem_parse_failed = true;
  850. break;
  851. case WLAN_EID_CHANNEL_SWITCH:
  852. if (elen != sizeof(struct ieee80211_channel_sw_ie)) {
  853. elem_parse_failed = true;
  854. break;
  855. }
  856. elems->ch_switch_ie = (void *)pos;
  857. break;
  858. case WLAN_EID_EXT_CHANSWITCH_ANN:
  859. if (elen != sizeof(struct ieee80211_ext_chansw_ie)) {
  860. elem_parse_failed = true;
  861. break;
  862. }
  863. elems->ext_chansw_ie = (void *)pos;
  864. break;
  865. case WLAN_EID_SECONDARY_CHANNEL_OFFSET:
  866. if (elen != sizeof(struct ieee80211_sec_chan_offs_ie)) {
  867. elem_parse_failed = true;
  868. break;
  869. }
  870. elems->sec_chan_offs = (void *)pos;
  871. break;
  872. case WLAN_EID_CHAN_SWITCH_PARAM:
  873. if (elen !=
  874. sizeof(*elems->mesh_chansw_params_ie)) {
  875. elem_parse_failed = true;
  876. break;
  877. }
  878. elems->mesh_chansw_params_ie = (void *)pos;
  879. break;
  880. case WLAN_EID_WIDE_BW_CHANNEL_SWITCH:
  881. if (!action ||
  882. elen != sizeof(*elems->wide_bw_chansw_ie)) {
  883. elem_parse_failed = true;
  884. break;
  885. }
  886. elems->wide_bw_chansw_ie = (void *)pos;
  887. break;
  888. case WLAN_EID_CHANNEL_SWITCH_WRAPPER:
  889. if (action) {
  890. elem_parse_failed = true;
  891. break;
  892. }
  893. /*
  894. * This is a bit tricky, but as we only care about
  895. * the wide bandwidth channel switch element, so
  896. * just parse it out manually.
  897. */
  898. ie = cfg80211_find_ie(WLAN_EID_WIDE_BW_CHANNEL_SWITCH,
  899. pos, elen);
  900. if (ie) {
  901. if (ie[1] == sizeof(*elems->wide_bw_chansw_ie))
  902. elems->wide_bw_chansw_ie =
  903. (void *)(ie + 2);
  904. else
  905. elem_parse_failed = true;
  906. }
  907. break;
  908. case WLAN_EID_COUNTRY:
  909. elems->country_elem = pos;
  910. elems->country_elem_len = elen;
  911. break;
  912. case WLAN_EID_PWR_CONSTRAINT:
  913. if (elen != 1) {
  914. elem_parse_failed = true;
  915. break;
  916. }
  917. elems->pwr_constr_elem = pos;
  918. break;
  919. case WLAN_EID_CISCO_VENDOR_SPECIFIC:
  920. /* Lots of different options exist, but we only care
  921. * about the Dynamic Transmit Power Control element.
  922. * First check for the Cisco OUI, then for the DTPC
  923. * tag (0x00).
  924. */
  925. if (elen < 4) {
  926. elem_parse_failed = true;
  927. break;
  928. }
  929. if (pos[0] != 0x00 || pos[1] != 0x40 ||
  930. pos[2] != 0x96 || pos[3] != 0x00)
  931. break;
  932. if (elen != 6) {
  933. elem_parse_failed = true;
  934. break;
  935. }
  936. if (calc_crc)
  937. crc = crc32_be(crc, pos - 2, elen + 2);
  938. elems->cisco_dtpc_elem = pos;
  939. break;
  940. case WLAN_EID_TIMEOUT_INTERVAL:
  941. if (elen >= sizeof(struct ieee80211_timeout_interval_ie))
  942. elems->timeout_int = (void *)pos;
  943. else
  944. elem_parse_failed = true;
  945. break;
  946. default:
  947. break;
  948. }
  949. if (elem_parse_failed)
  950. elems->parse_error = true;
  951. else
  952. __set_bit(id, seen_elems);
  953. left -= elen;
  954. pos += elen;
  955. }
  956. if (left != 0)
  957. elems->parse_error = true;
  958. return crc;
  959. }
  960. void ieee80211_set_wmm_default(struct ieee80211_sub_if_data *sdata,
  961. bool bss_notify, bool enable_qos)
  962. {
  963. struct ieee80211_local *local = sdata->local;
  964. struct ieee80211_tx_queue_params qparam;
  965. struct ieee80211_chanctx_conf *chanctx_conf;
  966. int ac;
  967. bool use_11b;
  968. bool is_ocb; /* Use another EDCA parameters if dot11OCBActivated=true */
  969. int aCWmin, aCWmax;
  970. if (!local->ops->conf_tx)
  971. return;
  972. if (local->hw.queues < IEEE80211_NUM_ACS)
  973. return;
  974. memset(&qparam, 0, sizeof(qparam));
  975. rcu_read_lock();
  976. chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
  977. use_11b = (chanctx_conf &&
  978. chanctx_conf->def.chan->band == IEEE80211_BAND_2GHZ) &&
  979. !(sdata->flags & IEEE80211_SDATA_OPERATING_GMODE);
  980. rcu_read_unlock();
  981. is_ocb = (sdata->vif.type == NL80211_IFTYPE_OCB);
  982. /* Set defaults according to 802.11-2007 Table 7-37 */
  983. aCWmax = 1023;
  984. if (use_11b)
  985. aCWmin = 31;
  986. else
  987. aCWmin = 15;
  988. /* Confiure old 802.11b/g medium access rules. */
  989. qparam.cw_max = aCWmax;
  990. qparam.cw_min = aCWmin;
  991. qparam.txop = 0;
  992. qparam.aifs = 2;
  993. for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
  994. /* Update if QoS is enabled. */
  995. if (enable_qos) {
  996. switch (ac) {
  997. case IEEE80211_AC_BK:
  998. qparam.cw_max = aCWmax;
  999. qparam.cw_min = aCWmin;
  1000. qparam.txop = 0;
  1001. if (is_ocb)
  1002. qparam.aifs = 9;
  1003. else
  1004. qparam.aifs = 7;
  1005. break;
  1006. /* never happens but let's not leave undefined */
  1007. default:
  1008. case IEEE80211_AC_BE:
  1009. qparam.cw_max = aCWmax;
  1010. qparam.cw_min = aCWmin;
  1011. qparam.txop = 0;
  1012. if (is_ocb)
  1013. qparam.aifs = 6;
  1014. else
  1015. qparam.aifs = 3;
  1016. break;
  1017. case IEEE80211_AC_VI:
  1018. qparam.cw_max = aCWmin;
  1019. qparam.cw_min = (aCWmin + 1) / 2 - 1;
  1020. if (is_ocb)
  1021. qparam.txop = 0;
  1022. else if (use_11b)
  1023. qparam.txop = 6016/32;
  1024. else
  1025. qparam.txop = 3008/32;
  1026. if (is_ocb)
  1027. qparam.aifs = 3;
  1028. else
  1029. qparam.aifs = 2;
  1030. break;
  1031. case IEEE80211_AC_VO:
  1032. qparam.cw_max = (aCWmin + 1) / 2 - 1;
  1033. qparam.cw_min = (aCWmin + 1) / 4 - 1;
  1034. if (is_ocb)
  1035. qparam.txop = 0;
  1036. else if (use_11b)
  1037. qparam.txop = 3264/32;
  1038. else
  1039. qparam.txop = 1504/32;
  1040. qparam.aifs = 2;
  1041. break;
  1042. }
  1043. }
  1044. qparam.uapsd = false;
  1045. sdata->tx_conf[ac] = qparam;
  1046. drv_conf_tx(local, sdata, ac, &qparam);
  1047. }
  1048. if (sdata->vif.type != NL80211_IFTYPE_MONITOR &&
  1049. sdata->vif.type != NL80211_IFTYPE_P2P_DEVICE) {
  1050. sdata->vif.bss_conf.qos = enable_qos;
  1051. if (bss_notify)
  1052. ieee80211_bss_info_change_notify(sdata,
  1053. BSS_CHANGED_QOS);
  1054. }
  1055. }
  1056. void ieee80211_send_auth(struct ieee80211_sub_if_data *sdata,
  1057. u16 transaction, u16 auth_alg, u16 status,
  1058. const u8 *extra, size_t extra_len, const u8 *da,
  1059. const u8 *bssid, const u8 *key, u8 key_len, u8 key_idx,
  1060. u32 tx_flags)
  1061. {
  1062. struct ieee80211_local *local = sdata->local;
  1063. struct sk_buff *skb;
  1064. struct ieee80211_mgmt *mgmt;
  1065. int err;
  1066. /* 24 + 6 = header + auth_algo + auth_transaction + status_code */
  1067. skb = dev_alloc_skb(local->hw.extra_tx_headroom + IEEE80211_WEP_IV_LEN +
  1068. 24 + 6 + extra_len + IEEE80211_WEP_ICV_LEN);
  1069. if (!skb)
  1070. return;
  1071. skb_reserve(skb, local->hw.extra_tx_headroom + IEEE80211_WEP_IV_LEN);
  1072. mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24 + 6);
  1073. memset(mgmt, 0, 24 + 6);
  1074. mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
  1075. IEEE80211_STYPE_AUTH);
  1076. memcpy(mgmt->da, da, ETH_ALEN);
  1077. memcpy(mgmt->sa, sdata->vif.addr, ETH_ALEN);
  1078. memcpy(mgmt->bssid, bssid, ETH_ALEN);
  1079. mgmt->u.auth.auth_alg = cpu_to_le16(auth_alg);
  1080. mgmt->u.auth.auth_transaction = cpu_to_le16(transaction);
  1081. mgmt->u.auth.status_code = cpu_to_le16(status);
  1082. if (extra)
  1083. memcpy(skb_put(skb, extra_len), extra, extra_len);
  1084. if (auth_alg == WLAN_AUTH_SHARED_KEY && transaction == 3) {
  1085. mgmt->frame_control |= cpu_to_le16(IEEE80211_FCTL_PROTECTED);
  1086. err = ieee80211_wep_encrypt(local, skb, key, key_len, key_idx);
  1087. WARN_ON(err);
  1088. }
  1089. IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT |
  1090. tx_flags;
  1091. ieee80211_tx_skb(sdata, skb);
  1092. }
  1093. void ieee80211_send_deauth_disassoc(struct ieee80211_sub_if_data *sdata,
  1094. const u8 *bssid, u16 stype, u16 reason,
  1095. bool send_frame, u8 *frame_buf)
  1096. {
  1097. struct ieee80211_local *local = sdata->local;
  1098. struct sk_buff *skb;
  1099. struct ieee80211_mgmt *mgmt = (void *)frame_buf;
  1100. /* build frame */
  1101. mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT | stype);
  1102. mgmt->duration = 0; /* initialize only */
  1103. mgmt->seq_ctrl = 0; /* initialize only */
  1104. memcpy(mgmt->da, bssid, ETH_ALEN);
  1105. memcpy(mgmt->sa, sdata->vif.addr, ETH_ALEN);
  1106. memcpy(mgmt->bssid, bssid, ETH_ALEN);
  1107. /* u.deauth.reason_code == u.disassoc.reason_code */
  1108. mgmt->u.deauth.reason_code = cpu_to_le16(reason);
  1109. if (send_frame) {
  1110. skb = dev_alloc_skb(local->hw.extra_tx_headroom +
  1111. IEEE80211_DEAUTH_FRAME_LEN);
  1112. if (!skb)
  1113. return;
  1114. skb_reserve(skb, local->hw.extra_tx_headroom);
  1115. /* copy in frame */
  1116. memcpy(skb_put(skb, IEEE80211_DEAUTH_FRAME_LEN),
  1117. mgmt, IEEE80211_DEAUTH_FRAME_LEN);
  1118. if (sdata->vif.type != NL80211_IFTYPE_STATION ||
  1119. !(sdata->u.mgd.flags & IEEE80211_STA_MFP_ENABLED))
  1120. IEEE80211_SKB_CB(skb)->flags |=
  1121. IEEE80211_TX_INTFL_DONT_ENCRYPT;
  1122. ieee80211_tx_skb(sdata, skb);
  1123. }
  1124. }
  1125. static int ieee80211_build_preq_ies_band(struct ieee80211_local *local,
  1126. u8 *buffer, size_t buffer_len,
  1127. const u8 *ie, size_t ie_len,
  1128. enum ieee80211_band band,
  1129. u32 rate_mask,
  1130. struct cfg80211_chan_def *chandef,
  1131. size_t *offset)
  1132. {
  1133. struct ieee80211_supported_band *sband;
  1134. u8 *pos = buffer, *end = buffer + buffer_len;
  1135. size_t noffset;
  1136. int supp_rates_len, i;
  1137. u8 rates[32];
  1138. int num_rates;
  1139. int ext_rates_len;
  1140. int shift;
  1141. u32 rate_flags;
  1142. bool have_80mhz = false;
  1143. *offset = 0;
  1144. sband = local->hw.wiphy->bands[band];
  1145. if (WARN_ON_ONCE(!sband))
  1146. return 0;
  1147. rate_flags = ieee80211_chandef_rate_flags(chandef);
  1148. shift = ieee80211_chandef_get_shift(chandef);
  1149. num_rates = 0;
  1150. for (i = 0; i < sband->n_bitrates; i++) {
  1151. if ((BIT(i) & rate_mask) == 0)
  1152. continue; /* skip rate */
  1153. if ((rate_flags & sband->bitrates[i].flags) != rate_flags)
  1154. continue;
  1155. rates[num_rates++] =
  1156. (u8) DIV_ROUND_UP(sband->bitrates[i].bitrate,
  1157. (1 << shift) * 5);
  1158. }
  1159. supp_rates_len = min_t(int, num_rates, 8);
  1160. if (end - pos < 2 + supp_rates_len)
  1161. goto out_err;
  1162. *pos++ = WLAN_EID_SUPP_RATES;
  1163. *pos++ = supp_rates_len;
  1164. memcpy(pos, rates, supp_rates_len);
  1165. pos += supp_rates_len;
  1166. /* insert "request information" if in custom IEs */
  1167. if (ie && ie_len) {
  1168. static const u8 before_extrates[] = {
  1169. WLAN_EID_SSID,
  1170. WLAN_EID_SUPP_RATES,
  1171. WLAN_EID_REQUEST,
  1172. };
  1173. noffset = ieee80211_ie_split(ie, ie_len,
  1174. before_extrates,
  1175. ARRAY_SIZE(before_extrates),
  1176. *offset);
  1177. if (end - pos < noffset - *offset)
  1178. goto out_err;
  1179. memcpy(pos, ie + *offset, noffset - *offset);
  1180. pos += noffset - *offset;
  1181. *offset = noffset;
  1182. }
  1183. ext_rates_len = num_rates - supp_rates_len;
  1184. if (ext_rates_len > 0) {
  1185. if (end - pos < 2 + ext_rates_len)
  1186. goto out_err;
  1187. *pos++ = WLAN_EID_EXT_SUPP_RATES;
  1188. *pos++ = ext_rates_len;
  1189. memcpy(pos, rates + supp_rates_len, ext_rates_len);
  1190. pos += ext_rates_len;
  1191. }
  1192. if (chandef->chan && sband->band == IEEE80211_BAND_2GHZ) {
  1193. if (end - pos < 3)
  1194. goto out_err;
  1195. *pos++ = WLAN_EID_DS_PARAMS;
  1196. *pos++ = 1;
  1197. *pos++ = ieee80211_frequency_to_channel(
  1198. chandef->chan->center_freq);
  1199. }
  1200. /* insert custom IEs that go before HT */
  1201. if (ie && ie_len) {
  1202. static const u8 before_ht[] = {
  1203. WLAN_EID_SSID,
  1204. WLAN_EID_SUPP_RATES,
  1205. WLAN_EID_REQUEST,
  1206. WLAN_EID_EXT_SUPP_RATES,
  1207. WLAN_EID_DS_PARAMS,
  1208. WLAN_EID_SUPPORTED_REGULATORY_CLASSES,
  1209. };
  1210. noffset = ieee80211_ie_split(ie, ie_len,
  1211. before_ht, ARRAY_SIZE(before_ht),
  1212. *offset);
  1213. if (end - pos < noffset - *offset)
  1214. goto out_err;
  1215. memcpy(pos, ie + *offset, noffset - *offset);
  1216. pos += noffset - *offset;
  1217. *offset = noffset;
  1218. }
  1219. if (sband->ht_cap.ht_supported) {
  1220. if (end - pos < 2 + sizeof(struct ieee80211_ht_cap))
  1221. goto out_err;
  1222. pos = ieee80211_ie_build_ht_cap(pos, &sband->ht_cap,
  1223. sband->ht_cap.cap);
  1224. }
  1225. /*
  1226. * If adding more here, adjust code in main.c
  1227. * that calculates local->scan_ies_len.
  1228. */
  1229. /* insert custom IEs that go before VHT */
  1230. if (ie && ie_len) {
  1231. static const u8 before_vht[] = {
  1232. WLAN_EID_SSID,
  1233. WLAN_EID_SUPP_RATES,
  1234. WLAN_EID_REQUEST,
  1235. WLAN_EID_EXT_SUPP_RATES,
  1236. WLAN_EID_DS_PARAMS,
  1237. WLAN_EID_SUPPORTED_REGULATORY_CLASSES,
  1238. WLAN_EID_HT_CAPABILITY,
  1239. WLAN_EID_BSS_COEX_2040,
  1240. WLAN_EID_EXT_CAPABILITY,
  1241. WLAN_EID_SSID_LIST,
  1242. WLAN_EID_CHANNEL_USAGE,
  1243. WLAN_EID_INTERWORKING,
  1244. /* mesh ID can't happen here */
  1245. /* 60 GHz can't happen here right now */
  1246. };
  1247. noffset = ieee80211_ie_split(ie, ie_len,
  1248. before_vht, ARRAY_SIZE(before_vht),
  1249. *offset);
  1250. if (end - pos < noffset - *offset)
  1251. goto out_err;
  1252. memcpy(pos, ie + *offset, noffset - *offset);
  1253. pos += noffset - *offset;
  1254. *offset = noffset;
  1255. }
  1256. /* Check if any channel in this sband supports at least 80 MHz */
  1257. for (i = 0; i < sband->n_channels; i++) {
  1258. if (sband->channels[i].flags & (IEEE80211_CHAN_DISABLED |
  1259. IEEE80211_CHAN_NO_80MHZ))
  1260. continue;
  1261. have_80mhz = true;
  1262. break;
  1263. }
  1264. if (sband->vht_cap.vht_supported && have_80mhz) {
  1265. if (end - pos < 2 + sizeof(struct ieee80211_vht_cap))
  1266. goto out_err;
  1267. pos = ieee80211_ie_build_vht_cap(pos, &sband->vht_cap,
  1268. sband->vht_cap.cap);
  1269. }
  1270. return pos - buffer;
  1271. out_err:
  1272. WARN_ONCE(1, "not enough space for preq IEs\n");
  1273. return pos - buffer;
  1274. }
  1275. int ieee80211_build_preq_ies(struct ieee80211_local *local, u8 *buffer,
  1276. size_t buffer_len,
  1277. struct ieee80211_scan_ies *ie_desc,
  1278. const u8 *ie, size_t ie_len,
  1279. u8 bands_used, u32 *rate_masks,
  1280. struct cfg80211_chan_def *chandef)
  1281. {
  1282. size_t pos = 0, old_pos = 0, custom_ie_offset = 0;
  1283. int i;
  1284. memset(ie_desc, 0, sizeof(*ie_desc));
  1285. for (i = 0; i < IEEE80211_NUM_BANDS; i++) {
  1286. if (bands_used & BIT(i)) {
  1287. pos += ieee80211_build_preq_ies_band(local,
  1288. buffer + pos,
  1289. buffer_len - pos,
  1290. ie, ie_len, i,
  1291. rate_masks[i],
  1292. chandef,
  1293. &custom_ie_offset);
  1294. ie_desc->ies[i] = buffer + old_pos;
  1295. ie_desc->len[i] = pos - old_pos;
  1296. old_pos = pos;
  1297. }
  1298. }
  1299. /* add any remaining custom IEs */
  1300. if (ie && ie_len) {
  1301. if (WARN_ONCE(buffer_len - pos < ie_len - custom_ie_offset,
  1302. "not enough space for preq custom IEs\n"))
  1303. return pos;
  1304. memcpy(buffer + pos, ie + custom_ie_offset,
  1305. ie_len - custom_ie_offset);
  1306. ie_desc->common_ies = buffer + pos;
  1307. ie_desc->common_ie_len = ie_len - custom_ie_offset;
  1308. pos += ie_len - custom_ie_offset;
  1309. }
  1310. return pos;
  1311. };
  1312. struct sk_buff *ieee80211_build_probe_req(struct ieee80211_sub_if_data *sdata,
  1313. const u8 *src, const u8 *dst,
  1314. u32 ratemask,
  1315. struct ieee80211_channel *chan,
  1316. const u8 *ssid, size_t ssid_len,
  1317. const u8 *ie, size_t ie_len,
  1318. bool directed)
  1319. {
  1320. struct ieee80211_local *local = sdata->local;
  1321. struct cfg80211_chan_def chandef;
  1322. struct sk_buff *skb;
  1323. struct ieee80211_mgmt *mgmt;
  1324. int ies_len;
  1325. u32 rate_masks[IEEE80211_NUM_BANDS] = {};
  1326. struct ieee80211_scan_ies dummy_ie_desc;
  1327. /*
  1328. * Do not send DS Channel parameter for directed probe requests
  1329. * in order to maximize the chance that we get a response. Some
  1330. * badly-behaved APs don't respond when this parameter is included.
  1331. */
  1332. chandef.width = sdata->vif.bss_conf.chandef.width;
  1333. if (directed)
  1334. chandef.chan = NULL;
  1335. else
  1336. chandef.chan = chan;
  1337. skb = ieee80211_probereq_get(&local->hw, src, ssid, ssid_len,
  1338. 100 + ie_len);
  1339. if (!skb)
  1340. return NULL;
  1341. rate_masks[chan->band] = ratemask;
  1342. ies_len = ieee80211_build_preq_ies(local, skb_tail_pointer(skb),
  1343. skb_tailroom(skb), &dummy_ie_desc,
  1344. ie, ie_len, BIT(chan->band),
  1345. rate_masks, &chandef);
  1346. skb_put(skb, ies_len);
  1347. if (dst) {
  1348. mgmt = (struct ieee80211_mgmt *) skb->data;
  1349. memcpy(mgmt->da, dst, ETH_ALEN);
  1350. memcpy(mgmt->bssid, dst, ETH_ALEN);
  1351. }
  1352. IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
  1353. return skb;
  1354. }
  1355. void ieee80211_send_probe_req(struct ieee80211_sub_if_data *sdata,
  1356. const u8 *src, const u8 *dst,
  1357. const u8 *ssid, size_t ssid_len,
  1358. const u8 *ie, size_t ie_len,
  1359. u32 ratemask, bool directed, u32 tx_flags,
  1360. struct ieee80211_channel *channel, bool scan)
  1361. {
  1362. struct sk_buff *skb;
  1363. skb = ieee80211_build_probe_req(sdata, src, dst, ratemask, channel,
  1364. ssid, ssid_len,
  1365. ie, ie_len, directed);
  1366. if (skb) {
  1367. IEEE80211_SKB_CB(skb)->flags |= tx_flags;
  1368. if (scan)
  1369. ieee80211_tx_skb_tid_band(sdata, skb, 7, channel->band);
  1370. else
  1371. ieee80211_tx_skb(sdata, skb);
  1372. }
  1373. }
  1374. u32 ieee80211_sta_get_rates(struct ieee80211_sub_if_data *sdata,
  1375. struct ieee802_11_elems *elems,
  1376. enum ieee80211_band band, u32 *basic_rates)
  1377. {
  1378. struct ieee80211_supported_band *sband;
  1379. size_t num_rates;
  1380. u32 supp_rates, rate_flags;
  1381. int i, j, shift;
  1382. sband = sdata->local->hw.wiphy->bands[band];
  1383. rate_flags = ieee80211_chandef_rate_flags(&sdata->vif.bss_conf.chandef);
  1384. shift = ieee80211_vif_get_shift(&sdata->vif);
  1385. if (WARN_ON(!sband))
  1386. return 1;
  1387. num_rates = sband->n_bitrates;
  1388. supp_rates = 0;
  1389. for (i = 0; i < elems->supp_rates_len +
  1390. elems->ext_supp_rates_len; i++) {
  1391. u8 rate = 0;
  1392. int own_rate;
  1393. bool is_basic;
  1394. if (i < elems->supp_rates_len)
  1395. rate = elems->supp_rates[i];
  1396. else if (elems->ext_supp_rates)
  1397. rate = elems->ext_supp_rates
  1398. [i - elems->supp_rates_len];
  1399. own_rate = 5 * (rate & 0x7f);
  1400. is_basic = !!(rate & 0x80);
  1401. if (is_basic && (rate & 0x7f) == BSS_MEMBERSHIP_SELECTOR_HT_PHY)
  1402. continue;
  1403. for (j = 0; j < num_rates; j++) {
  1404. int brate;
  1405. if ((rate_flags & sband->bitrates[j].flags)
  1406. != rate_flags)
  1407. continue;
  1408. brate = DIV_ROUND_UP(sband->bitrates[j].bitrate,
  1409. 1 << shift);
  1410. if (brate == own_rate) {
  1411. supp_rates |= BIT(j);
  1412. if (basic_rates && is_basic)
  1413. *basic_rates |= BIT(j);
  1414. }
  1415. }
  1416. }
  1417. return supp_rates;
  1418. }
  1419. void ieee80211_stop_device(struct ieee80211_local *local)
  1420. {
  1421. ieee80211_led_radio(local, false);
  1422. ieee80211_mod_tpt_led_trig(local, 0, IEEE80211_TPT_LEDTRIG_FL_RADIO);
  1423. cancel_work_sync(&local->reconfig_filter);
  1424. flush_workqueue(local->workqueue);
  1425. drv_stop(local);
  1426. }
  1427. static void ieee80211_flush_completed_scan(struct ieee80211_local *local,
  1428. bool aborted)
  1429. {
  1430. /* It's possible that we don't handle the scan completion in
  1431. * time during suspend, so if it's still marked as completed
  1432. * here, queue the work and flush it to clean things up.
  1433. * Instead of calling the worker function directly here, we
  1434. * really queue it to avoid potential races with other flows
  1435. * scheduling the same work.
  1436. */
  1437. if (test_bit(SCAN_COMPLETED, &local->scanning)) {
  1438. /* If coming from reconfiguration failure, abort the scan so
  1439. * we don't attempt to continue a partial HW scan - which is
  1440. * possible otherwise if (e.g.) the 2.4 GHz portion was the
  1441. * completed scan, and a 5 GHz portion is still pending.
  1442. */
  1443. if (aborted)
  1444. set_bit(SCAN_ABORTED, &local->scanning);
  1445. ieee80211_queue_delayed_work(&local->hw, &local->scan_work, 0);
  1446. flush_delayed_work(&local->scan_work);
  1447. }
  1448. }
  1449. static void ieee80211_handle_reconfig_failure(struct ieee80211_local *local)
  1450. {
  1451. struct ieee80211_sub_if_data *sdata;
  1452. struct ieee80211_chanctx *ctx;
  1453. /*
  1454. * We get here if during resume the device can't be restarted properly.
  1455. * We might also get here if this happens during HW reset, which is a
  1456. * slightly different situation and we need to drop all connections in
  1457. * the latter case.
  1458. *
  1459. * Ask cfg80211 to turn off all interfaces, this will result in more
  1460. * warnings but at least we'll then get into a clean stopped state.
  1461. */
  1462. local->resuming = false;
  1463. local->suspended = false;
  1464. local->in_reconfig = false;
  1465. ieee80211_flush_completed_scan(local, true);
  1466. /* scheduled scan clearly can't be running any more, but tell
  1467. * cfg80211 and clear local state
  1468. */
  1469. ieee80211_sched_scan_end(local);
  1470. list_for_each_entry(sdata, &local->interfaces, list)
  1471. sdata->flags &= ~IEEE80211_SDATA_IN_DRIVER;
  1472. /* Mark channel contexts as not being in the driver any more to avoid
  1473. * removing them from the driver during the shutdown process...
  1474. */
  1475. mutex_lock(&local->chanctx_mtx);
  1476. list_for_each_entry(ctx, &local->chanctx_list, list)
  1477. ctx->driver_present = false;
  1478. mutex_unlock(&local->chanctx_mtx);
  1479. cfg80211_shutdown_all_interfaces(local->hw.wiphy);
  1480. }
  1481. static void ieee80211_assign_chanctx(struct ieee80211_local *local,
  1482. struct ieee80211_sub_if_data *sdata)
  1483. {
  1484. struct ieee80211_chanctx_conf *conf;
  1485. struct ieee80211_chanctx *ctx;
  1486. if (!local->use_chanctx)
  1487. return;
  1488. mutex_lock(&local->chanctx_mtx);
  1489. conf = rcu_dereference_protected(sdata->vif.chanctx_conf,
  1490. lockdep_is_held(&local->chanctx_mtx));
  1491. if (conf) {
  1492. ctx = container_of(conf, struct ieee80211_chanctx, conf);
  1493. drv_assign_vif_chanctx(local, sdata, ctx);
  1494. }
  1495. mutex_unlock(&local->chanctx_mtx);
  1496. }
  1497. static void ieee80211_reconfig_stations(struct ieee80211_sub_if_data *sdata)
  1498. {
  1499. struct ieee80211_local *local = sdata->local;
  1500. struct sta_info *sta;
  1501. /* add STAs back */
  1502. mutex_lock(&local->sta_mtx);
  1503. list_for_each_entry(sta, &local->sta_list, list) {
  1504. enum ieee80211_sta_state state;
  1505. if (!sta->uploaded || sta->sdata != sdata)
  1506. continue;
  1507. for (state = IEEE80211_STA_NOTEXIST;
  1508. state < sta->sta_state; state++)
  1509. WARN_ON(drv_sta_state(local, sta->sdata, sta, state,
  1510. state + 1));
  1511. }
  1512. mutex_unlock(&local->sta_mtx);
  1513. }
  1514. int ieee80211_reconfig(struct ieee80211_local *local)
  1515. {
  1516. struct ieee80211_hw *hw = &local->hw;
  1517. struct ieee80211_sub_if_data *sdata;
  1518. struct ieee80211_chanctx *ctx;
  1519. struct sta_info *sta;
  1520. int res, i;
  1521. bool reconfig_due_to_wowlan = false;
  1522. struct ieee80211_sub_if_data *sched_scan_sdata;
  1523. struct cfg80211_sched_scan_request *sched_scan_req;
  1524. bool sched_scan_stopped = false;
  1525. bool suspended = local->suspended;
  1526. /* nothing to do if HW shouldn't run */
  1527. if (!local->open_count)
  1528. goto wake_up;
  1529. #ifdef CONFIG_PM
  1530. if (suspended)
  1531. local->resuming = true;
  1532. if (local->wowlan) {
  1533. /*
  1534. * In the wowlan case, both mac80211 and the device
  1535. * are functional when the resume op is called, so
  1536. * clear local->suspended so the device could operate
  1537. * normally (e.g. pass rx frames).
  1538. */
  1539. local->suspended = false;
  1540. res = drv_resume(local);
  1541. local->wowlan = false;
  1542. if (res < 0) {
  1543. local->resuming = false;
  1544. return res;
  1545. }
  1546. if (res == 0)
  1547. goto wake_up;
  1548. WARN_ON(res > 1);
  1549. /*
  1550. * res is 1, which means the driver requested
  1551. * to go through a regular reset on wakeup.
  1552. * restore local->suspended in this case.
  1553. */
  1554. reconfig_due_to_wowlan = true;
  1555. local->suspended = true;
  1556. }
  1557. #endif
  1558. /*
  1559. * In case of hw_restart during suspend (without wowlan),
  1560. * cancel restart work, as we are reconfiguring the device
  1561. * anyway.
  1562. * Note that restart_work is scheduled on a frozen workqueue,
  1563. * so we can't deadlock in this case.
  1564. */
  1565. if (suspended && local->in_reconfig && !reconfig_due_to_wowlan)
  1566. cancel_work_sync(&local->restart_work);
  1567. local->started = false;
  1568. /*
  1569. * Upon resume hardware can sometimes be goofy due to
  1570. * various platform / driver / bus issues, so restarting
  1571. * the device may at times not work immediately. Propagate
  1572. * the error.
  1573. */
  1574. res = drv_start(local);
  1575. if (res) {
  1576. if (suspended)
  1577. WARN(1, "Hardware became unavailable upon resume. This could be a software issue prior to suspend or a hardware issue.\n");
  1578. else
  1579. WARN(1, "Hardware became unavailable during restart.\n");
  1580. ieee80211_handle_reconfig_failure(local);
  1581. return res;
  1582. }
  1583. /* setup fragmentation threshold */
  1584. drv_set_frag_threshold(local, hw->wiphy->frag_threshold);
  1585. /* setup RTS threshold */
  1586. drv_set_rts_threshold(local, hw->wiphy->rts_threshold);
  1587. /* reset coverage class */
  1588. drv_set_coverage_class(local, hw->wiphy->coverage_class);
  1589. ieee80211_led_radio(local, true);
  1590. ieee80211_mod_tpt_led_trig(local,
  1591. IEEE80211_TPT_LEDTRIG_FL_RADIO, 0);
  1592. /* add interfaces */
  1593. sdata = rtnl_dereference(local->monitor_sdata);
  1594. if (sdata) {
  1595. /* in HW restart it exists already */
  1596. WARN_ON(local->resuming);
  1597. res = drv_add_interface(local, sdata);
  1598. if (WARN_ON(res)) {
  1599. RCU_INIT_POINTER(local->monitor_sdata, NULL);
  1600. synchronize_net();
  1601. kfree(sdata);
  1602. }
  1603. }
  1604. list_for_each_entry(sdata, &local->interfaces, list) {
  1605. if (sdata->vif.type != NL80211_IFTYPE_AP_VLAN &&
  1606. sdata->vif.type != NL80211_IFTYPE_MONITOR &&
  1607. ieee80211_sdata_running(sdata)) {
  1608. res = drv_add_interface(local, sdata);
  1609. if (WARN_ON(res))
  1610. break;
  1611. }
  1612. }
  1613. /* If adding any of the interfaces failed above, roll back and
  1614. * report failure.
  1615. */
  1616. if (res) {
  1617. list_for_each_entry_continue_reverse(sdata, &local->interfaces,
  1618. list)
  1619. if (sdata->vif.type != NL80211_IFTYPE_AP_VLAN &&
  1620. sdata->vif.type != NL80211_IFTYPE_MONITOR &&
  1621. ieee80211_sdata_running(sdata))
  1622. drv_remove_interface(local, sdata);
  1623. ieee80211_handle_reconfig_failure(local);
  1624. return res;
  1625. }
  1626. /* add channel contexts */
  1627. if (local->use_chanctx) {
  1628. mutex_lock(&local->chanctx_mtx);
  1629. list_for_each_entry(ctx, &local->chanctx_list, list)
  1630. if (ctx->replace_state !=
  1631. IEEE80211_CHANCTX_REPLACES_OTHER)
  1632. WARN_ON(drv_add_chanctx(local, ctx));
  1633. mutex_unlock(&local->chanctx_mtx);
  1634. sdata = rtnl_dereference(local->monitor_sdata);
  1635. if (sdata && ieee80211_sdata_running(sdata))
  1636. ieee80211_assign_chanctx(local, sdata);
  1637. }
  1638. /* reconfigure hardware */
  1639. ieee80211_hw_config(local, ~0);
  1640. ieee80211_configure_filter(local);
  1641. /* Finally also reconfigure all the BSS information */
  1642. list_for_each_entry(sdata, &local->interfaces, list) {
  1643. u32 changed;
  1644. if (!ieee80211_sdata_running(sdata))
  1645. continue;
  1646. ieee80211_assign_chanctx(local, sdata);
  1647. switch (sdata->vif.type) {
  1648. case NL80211_IFTYPE_AP_VLAN:
  1649. case NL80211_IFTYPE_MONITOR:
  1650. break;
  1651. default:
  1652. ieee80211_reconfig_stations(sdata);
  1653. /* fall through */
  1654. case NL80211_IFTYPE_AP: /* AP stations are handled later */
  1655. for (i = 0; i < IEEE80211_NUM_ACS; i++)
  1656. drv_conf_tx(local, sdata, i,
  1657. &sdata->tx_conf[i]);
  1658. break;
  1659. }
  1660. /* common change flags for all interface types */
  1661. changed = BSS_CHANGED_ERP_CTS_PROT |
  1662. BSS_CHANGED_ERP_PREAMBLE |
  1663. BSS_CHANGED_ERP_SLOT |
  1664. BSS_CHANGED_HT |
  1665. BSS_CHANGED_BASIC_RATES |
  1666. BSS_CHANGED_BEACON_INT |
  1667. BSS_CHANGED_BSSID |
  1668. BSS_CHANGED_CQM |
  1669. BSS_CHANGED_QOS |
  1670. BSS_CHANGED_IDLE |
  1671. BSS_CHANGED_TXPOWER;
  1672. switch (sdata->vif.type) {
  1673. case NL80211_IFTYPE_STATION:
  1674. changed |= BSS_CHANGED_ASSOC |
  1675. BSS_CHANGED_ARP_FILTER |
  1676. BSS_CHANGED_PS;
  1677. /* Re-send beacon info report to the driver */
  1678. if (sdata->u.mgd.have_beacon)
  1679. changed |= BSS_CHANGED_BEACON_INFO;
  1680. sdata_lock(sdata);
  1681. ieee80211_bss_info_change_notify(sdata, changed);
  1682. sdata_unlock(sdata);
  1683. break;
  1684. case NL80211_IFTYPE_OCB:
  1685. changed |= BSS_CHANGED_OCB;
  1686. ieee80211_bss_info_change_notify(sdata, changed);
  1687. break;
  1688. case NL80211_IFTYPE_ADHOC:
  1689. changed |= BSS_CHANGED_IBSS;
  1690. /* fall through */
  1691. case NL80211_IFTYPE_AP:
  1692. changed |= BSS_CHANGED_SSID | BSS_CHANGED_P2P_PS;
  1693. if (sdata->vif.type == NL80211_IFTYPE_AP) {
  1694. changed |= BSS_CHANGED_AP_PROBE_RESP;
  1695. if (rcu_access_pointer(sdata->u.ap.beacon))
  1696. drv_start_ap(local, sdata);
  1697. }
  1698. /* fall through */
  1699. case NL80211_IFTYPE_MESH_POINT:
  1700. if (sdata->vif.bss_conf.enable_beacon) {
  1701. changed |= BSS_CHANGED_BEACON |
  1702. BSS_CHANGED_BEACON_ENABLED;
  1703. ieee80211_bss_info_change_notify(sdata, changed);
  1704. }
  1705. break;
  1706. case NL80211_IFTYPE_WDS:
  1707. case NL80211_IFTYPE_AP_VLAN:
  1708. case NL80211_IFTYPE_MONITOR:
  1709. case NL80211_IFTYPE_P2P_DEVICE:
  1710. /* nothing to do */
  1711. break;
  1712. case NL80211_IFTYPE_UNSPECIFIED:
  1713. case NUM_NL80211_IFTYPES:
  1714. case NL80211_IFTYPE_P2P_CLIENT:
  1715. case NL80211_IFTYPE_P2P_GO:
  1716. WARN_ON(1);
  1717. break;
  1718. }
  1719. }
  1720. ieee80211_recalc_ps(local);
  1721. /*
  1722. * The sta might be in psm against the ap (e.g. because
  1723. * this was the state before a hw restart), so we
  1724. * explicitly send a null packet in order to make sure
  1725. * it'll sync against the ap (and get out of psm).
  1726. */
  1727. if (!(local->hw.conf.flags & IEEE80211_CONF_PS)) {
  1728. list_for_each_entry(sdata, &local->interfaces, list) {
  1729. if (sdata->vif.type != NL80211_IFTYPE_STATION)
  1730. continue;
  1731. if (!sdata->u.mgd.associated)
  1732. continue;
  1733. ieee80211_send_nullfunc(local, sdata, false);
  1734. }
  1735. }
  1736. /* APs are now beaconing, add back stations */
  1737. mutex_lock(&local->sta_mtx);
  1738. list_for_each_entry(sta, &local->sta_list, list) {
  1739. enum ieee80211_sta_state state;
  1740. if (!sta->uploaded)
  1741. continue;
  1742. if (sta->sdata->vif.type != NL80211_IFTYPE_AP)
  1743. continue;
  1744. for (state = IEEE80211_STA_NOTEXIST;
  1745. state < sta->sta_state; state++)
  1746. WARN_ON(drv_sta_state(local, sta->sdata, sta, state,
  1747. state + 1));
  1748. }
  1749. mutex_unlock(&local->sta_mtx);
  1750. /* add back keys */
  1751. list_for_each_entry(sdata, &local->interfaces, list)
  1752. ieee80211_reset_crypto_tx_tailroom(sdata);
  1753. list_for_each_entry(sdata, &local->interfaces, list)
  1754. if (ieee80211_sdata_running(sdata))
  1755. ieee80211_enable_keys(sdata);
  1756. /* Reconfigure sched scan if it was interrupted by FW restart */
  1757. mutex_lock(&local->mtx);
  1758. sched_scan_sdata = rcu_dereference_protected(local->sched_scan_sdata,
  1759. lockdep_is_held(&local->mtx));
  1760. sched_scan_req = rcu_dereference_protected(local->sched_scan_req,
  1761. lockdep_is_held(&local->mtx));
  1762. if (sched_scan_sdata && sched_scan_req)
  1763. /*
  1764. * Sched scan stopped, but we don't want to report it. Instead,
  1765. * we're trying to reschedule. However, if more than one scan
  1766. * plan was set, we cannot reschedule since we don't know which
  1767. * scan plan was currently running (and some scan plans may have
  1768. * already finished).
  1769. */
  1770. if (sched_scan_req->n_scan_plans > 1 ||
  1771. __ieee80211_request_sched_scan_start(sched_scan_sdata,
  1772. sched_scan_req))
  1773. sched_scan_stopped = true;
  1774. mutex_unlock(&local->mtx);
  1775. if (sched_scan_stopped)
  1776. cfg80211_sched_scan_stopped_rtnl(local->hw.wiphy);
  1777. wake_up:
  1778. local->in_reconfig = false;
  1779. barrier();
  1780. if (local->monitors == local->open_count && local->monitors > 0)
  1781. ieee80211_add_virtual_monitor(local);
  1782. /*
  1783. * Clear the WLAN_STA_BLOCK_BA flag so new aggregation
  1784. * sessions can be established after a resume.
  1785. *
  1786. * Also tear down aggregation sessions since reconfiguring
  1787. * them in a hardware restart scenario is not easily done
  1788. * right now, and the hardware will have lost information
  1789. * about the sessions, but we and the AP still think they
  1790. * are active. This is really a workaround though.
  1791. */
  1792. if (ieee80211_hw_check(hw, AMPDU_AGGREGATION)) {
  1793. mutex_lock(&local->sta_mtx);
  1794. list_for_each_entry(sta, &local->sta_list, list) {
  1795. if (!local->resuming)
  1796. ieee80211_sta_tear_down_BA_sessions(
  1797. sta, AGG_STOP_LOCAL_REQUEST);
  1798. clear_sta_flag(sta, WLAN_STA_BLOCK_BA);
  1799. }
  1800. mutex_unlock(&local->sta_mtx);
  1801. }
  1802. ieee80211_wake_queues_by_reason(hw, IEEE80211_MAX_QUEUE_MAP,
  1803. IEEE80211_QUEUE_STOP_REASON_SUSPEND,
  1804. false);
  1805. /*
  1806. * If this is for hw restart things are still running.
  1807. * We may want to change that later, however.
  1808. */
  1809. if (local->open_count && (!suspended || reconfig_due_to_wowlan))
  1810. drv_reconfig_complete(local, IEEE80211_RECONFIG_TYPE_RESTART);
  1811. if (!suspended)
  1812. return 0;
  1813. #ifdef CONFIG_PM
  1814. /* first set suspended false, then resuming */
  1815. local->suspended = false;
  1816. mb();
  1817. local->resuming = false;
  1818. ieee80211_flush_completed_scan(local, false);
  1819. if (local->open_count && !reconfig_due_to_wowlan)
  1820. drv_reconfig_complete(local, IEEE80211_RECONFIG_TYPE_SUSPEND);
  1821. list_for_each_entry(sdata, &local->interfaces, list) {
  1822. if (!ieee80211_sdata_running(sdata))
  1823. continue;
  1824. if (sdata->vif.type == NL80211_IFTYPE_STATION)
  1825. ieee80211_sta_restart(sdata);
  1826. }
  1827. mod_timer(&local->sta_cleanup, jiffies + 1);
  1828. #else
  1829. WARN_ON(1);
  1830. #endif
  1831. return 0;
  1832. }
  1833. void ieee80211_resume_disconnect(struct ieee80211_vif *vif)
  1834. {
  1835. struct ieee80211_sub_if_data *sdata;
  1836. struct ieee80211_local *local;
  1837. struct ieee80211_key *key;
  1838. if (WARN_ON(!vif))
  1839. return;
  1840. sdata = vif_to_sdata(vif);
  1841. local = sdata->local;
  1842. if (WARN_ON(!local->resuming))
  1843. return;
  1844. if (WARN_ON(vif->type != NL80211_IFTYPE_STATION))
  1845. return;
  1846. sdata->flags |= IEEE80211_SDATA_DISCONNECT_RESUME;
  1847. mutex_lock(&local->key_mtx);
  1848. list_for_each_entry(key, &sdata->key_list, list)
  1849. key->flags |= KEY_FLAG_TAINTED;
  1850. mutex_unlock(&local->key_mtx);
  1851. }
  1852. EXPORT_SYMBOL_GPL(ieee80211_resume_disconnect);
  1853. void ieee80211_recalc_smps(struct ieee80211_sub_if_data *sdata)
  1854. {
  1855. struct ieee80211_local *local = sdata->local;
  1856. struct ieee80211_chanctx_conf *chanctx_conf;
  1857. struct ieee80211_chanctx *chanctx;
  1858. mutex_lock(&local->chanctx_mtx);
  1859. chanctx_conf = rcu_dereference_protected(sdata->vif.chanctx_conf,
  1860. lockdep_is_held(&local->chanctx_mtx));
  1861. /*
  1862. * This function can be called from a work, thus it may be possible
  1863. * that the chanctx_conf is removed (due to a disconnection, for
  1864. * example).
  1865. * So nothing should be done in such case.
  1866. */
  1867. if (!chanctx_conf)
  1868. goto unlock;
  1869. chanctx = container_of(chanctx_conf, struct ieee80211_chanctx, conf);
  1870. ieee80211_recalc_smps_chanctx(local, chanctx);
  1871. unlock:
  1872. mutex_unlock(&local->chanctx_mtx);
  1873. }
  1874. void ieee80211_recalc_min_chandef(struct ieee80211_sub_if_data *sdata)
  1875. {
  1876. struct ieee80211_local *local = sdata->local;
  1877. struct ieee80211_chanctx_conf *chanctx_conf;
  1878. struct ieee80211_chanctx *chanctx;
  1879. mutex_lock(&local->chanctx_mtx);
  1880. chanctx_conf = rcu_dereference_protected(sdata->vif.chanctx_conf,
  1881. lockdep_is_held(&local->chanctx_mtx));
  1882. if (WARN_ON_ONCE(!chanctx_conf))
  1883. goto unlock;
  1884. chanctx = container_of(chanctx_conf, struct ieee80211_chanctx, conf);
  1885. ieee80211_recalc_chanctx_min_def(local, chanctx);
  1886. unlock:
  1887. mutex_unlock(&local->chanctx_mtx);
  1888. }
  1889. size_t ieee80211_ie_split_vendor(const u8 *ies, size_t ielen, size_t offset)
  1890. {
  1891. size_t pos = offset;
  1892. while (pos < ielen && ies[pos] != WLAN_EID_VENDOR_SPECIFIC)
  1893. pos += 2 + ies[pos + 1];
  1894. return pos;
  1895. }
  1896. static void _ieee80211_enable_rssi_reports(struct ieee80211_sub_if_data *sdata,
  1897. int rssi_min_thold,
  1898. int rssi_max_thold)
  1899. {
  1900. trace_api_enable_rssi_reports(sdata, rssi_min_thold, rssi_max_thold);
  1901. if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_STATION))
  1902. return;
  1903. /*
  1904. * Scale up threshold values before storing it, as the RSSI averaging
  1905. * algorithm uses a scaled up value as well. Change this scaling
  1906. * factor if the RSSI averaging algorithm changes.
  1907. */
  1908. sdata->u.mgd.rssi_min_thold = rssi_min_thold*16;
  1909. sdata->u.mgd.rssi_max_thold = rssi_max_thold*16;
  1910. }
  1911. void ieee80211_enable_rssi_reports(struct ieee80211_vif *vif,
  1912. int rssi_min_thold,
  1913. int rssi_max_thold)
  1914. {
  1915. struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
  1916. WARN_ON(rssi_min_thold == rssi_max_thold ||
  1917. rssi_min_thold > rssi_max_thold);
  1918. _ieee80211_enable_rssi_reports(sdata, rssi_min_thold,
  1919. rssi_max_thold);
  1920. }
  1921. EXPORT_SYMBOL(ieee80211_enable_rssi_reports);
  1922. void ieee80211_disable_rssi_reports(struct ieee80211_vif *vif)
  1923. {
  1924. struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
  1925. _ieee80211_enable_rssi_reports(sdata, 0, 0);
  1926. }
  1927. EXPORT_SYMBOL(ieee80211_disable_rssi_reports);
  1928. u8 *ieee80211_ie_build_ht_cap(u8 *pos, struct ieee80211_sta_ht_cap *ht_cap,
  1929. u16 cap)
  1930. {
  1931. __le16 tmp;
  1932. *pos++ = WLAN_EID_HT_CAPABILITY;
  1933. *pos++ = sizeof(struct ieee80211_ht_cap);
  1934. memset(pos, 0, sizeof(struct ieee80211_ht_cap));
  1935. /* capability flags */
  1936. tmp = cpu_to_le16(cap);
  1937. memcpy(pos, &tmp, sizeof(u16));
  1938. pos += sizeof(u16);
  1939. /* AMPDU parameters */
  1940. *pos++ = ht_cap->ampdu_factor |
  1941. (ht_cap->ampdu_density <<
  1942. IEEE80211_HT_AMPDU_PARM_DENSITY_SHIFT);
  1943. /* MCS set */
  1944. memcpy(pos, &ht_cap->mcs, sizeof(ht_cap->mcs));
  1945. pos += sizeof(ht_cap->mcs);
  1946. /* extended capabilities */
  1947. pos += sizeof(__le16);
  1948. /* BF capabilities */
  1949. pos += sizeof(__le32);
  1950. /* antenna selection */
  1951. pos += sizeof(u8);
  1952. return pos;
  1953. }
  1954. u8 *ieee80211_ie_build_vht_cap(u8 *pos, struct ieee80211_sta_vht_cap *vht_cap,
  1955. u32 cap)
  1956. {
  1957. __le32 tmp;
  1958. *pos++ = WLAN_EID_VHT_CAPABILITY;
  1959. *pos++ = sizeof(struct ieee80211_vht_cap);
  1960. memset(pos, 0, sizeof(struct ieee80211_vht_cap));
  1961. /* capability flags */
  1962. tmp = cpu_to_le32(cap);
  1963. memcpy(pos, &tmp, sizeof(u32));
  1964. pos += sizeof(u32);
  1965. /* VHT MCS set */
  1966. memcpy(pos, &vht_cap->vht_mcs, sizeof(vht_cap->vht_mcs));
  1967. pos += sizeof(vht_cap->vht_mcs);
  1968. return pos;
  1969. }
  1970. u8 *ieee80211_ie_build_ht_oper(u8 *pos, struct ieee80211_sta_ht_cap *ht_cap,
  1971. const struct cfg80211_chan_def *chandef,
  1972. u16 prot_mode, bool rifs_mode)
  1973. {
  1974. struct ieee80211_ht_operation *ht_oper;
  1975. /* Build HT Information */
  1976. *pos++ = WLAN_EID_HT_OPERATION;
  1977. *pos++ = sizeof(struct ieee80211_ht_operation);
  1978. ht_oper = (struct ieee80211_ht_operation *)pos;
  1979. ht_oper->primary_chan = ieee80211_frequency_to_channel(
  1980. chandef->chan->center_freq);
  1981. switch (chandef->width) {
  1982. case NL80211_CHAN_WIDTH_160:
  1983. case NL80211_CHAN_WIDTH_80P80:
  1984. case NL80211_CHAN_WIDTH_80:
  1985. case NL80211_CHAN_WIDTH_40:
  1986. if (chandef->center_freq1 > chandef->chan->center_freq)
  1987. ht_oper->ht_param = IEEE80211_HT_PARAM_CHA_SEC_ABOVE;
  1988. else
  1989. ht_oper->ht_param = IEEE80211_HT_PARAM_CHA_SEC_BELOW;
  1990. break;
  1991. default:
  1992. ht_oper->ht_param = IEEE80211_HT_PARAM_CHA_SEC_NONE;
  1993. break;
  1994. }
  1995. if (ht_cap->cap & IEEE80211_HT_CAP_SUP_WIDTH_20_40 &&
  1996. chandef->width != NL80211_CHAN_WIDTH_20_NOHT &&
  1997. chandef->width != NL80211_CHAN_WIDTH_20)
  1998. ht_oper->ht_param |= IEEE80211_HT_PARAM_CHAN_WIDTH_ANY;
  1999. if (rifs_mode)
  2000. ht_oper->ht_param |= IEEE80211_HT_PARAM_RIFS_MODE;
  2001. ht_oper->operation_mode = cpu_to_le16(prot_mode);
  2002. ht_oper->stbc_param = 0x0000;
  2003. /* It seems that Basic MCS set and Supported MCS set
  2004. are identical for the first 10 bytes */
  2005. memset(&ht_oper->basic_set, 0, 16);
  2006. memcpy(&ht_oper->basic_set, &ht_cap->mcs, 10);
  2007. return pos + sizeof(struct ieee80211_ht_operation);
  2008. }
  2009. u8 *ieee80211_ie_build_vht_oper(u8 *pos, struct ieee80211_sta_vht_cap *vht_cap,
  2010. const struct cfg80211_chan_def *chandef)
  2011. {
  2012. struct ieee80211_vht_operation *vht_oper;
  2013. *pos++ = WLAN_EID_VHT_OPERATION;
  2014. *pos++ = sizeof(struct ieee80211_vht_operation);
  2015. vht_oper = (struct ieee80211_vht_operation *)pos;
  2016. vht_oper->center_freq_seg1_idx = ieee80211_frequency_to_channel(
  2017. chandef->center_freq1);
  2018. if (chandef->center_freq2)
  2019. vht_oper->center_freq_seg2_idx =
  2020. ieee80211_frequency_to_channel(chandef->center_freq2);
  2021. else
  2022. vht_oper->center_freq_seg2_idx = 0x00;
  2023. switch (chandef->width) {
  2024. case NL80211_CHAN_WIDTH_160:
  2025. vht_oper->chan_width = IEEE80211_VHT_CHANWIDTH_160MHZ;
  2026. break;
  2027. case NL80211_CHAN_WIDTH_80P80:
  2028. vht_oper->chan_width = IEEE80211_VHT_CHANWIDTH_80P80MHZ;
  2029. break;
  2030. case NL80211_CHAN_WIDTH_80:
  2031. vht_oper->chan_width = IEEE80211_VHT_CHANWIDTH_80MHZ;
  2032. break;
  2033. default:
  2034. vht_oper->chan_width = IEEE80211_VHT_CHANWIDTH_USE_HT;
  2035. break;
  2036. }
  2037. /* don't require special VHT peer rates */
  2038. vht_oper->basic_mcs_set = cpu_to_le16(0xffff);
  2039. return pos + sizeof(struct ieee80211_vht_operation);
  2040. }
  2041. void ieee80211_ht_oper_to_chandef(struct ieee80211_channel *control_chan,
  2042. const struct ieee80211_ht_operation *ht_oper,
  2043. struct cfg80211_chan_def *chandef)
  2044. {
  2045. enum nl80211_channel_type channel_type;
  2046. if (!ht_oper) {
  2047. cfg80211_chandef_create(chandef, control_chan,
  2048. NL80211_CHAN_NO_HT);
  2049. return;
  2050. }
  2051. switch (ht_oper->ht_param & IEEE80211_HT_PARAM_CHA_SEC_OFFSET) {
  2052. case IEEE80211_HT_PARAM_CHA_SEC_NONE:
  2053. channel_type = NL80211_CHAN_HT20;
  2054. break;
  2055. case IEEE80211_HT_PARAM_CHA_SEC_ABOVE:
  2056. channel_type = NL80211_CHAN_HT40PLUS;
  2057. break;
  2058. case IEEE80211_HT_PARAM_CHA_SEC_BELOW:
  2059. channel_type = NL80211_CHAN_HT40MINUS;
  2060. break;
  2061. default:
  2062. channel_type = NL80211_CHAN_NO_HT;
  2063. }
  2064. cfg80211_chandef_create(chandef, control_chan, channel_type);
  2065. }
  2066. void ieee80211_vht_oper_to_chandef(struct ieee80211_channel *control_chan,
  2067. const struct ieee80211_vht_operation *oper,
  2068. struct cfg80211_chan_def *chandef)
  2069. {
  2070. if (!oper)
  2071. return;
  2072. chandef->chan = control_chan;
  2073. switch (oper->chan_width) {
  2074. case IEEE80211_VHT_CHANWIDTH_USE_HT:
  2075. break;
  2076. case IEEE80211_VHT_CHANWIDTH_80MHZ:
  2077. chandef->width = NL80211_CHAN_WIDTH_80;
  2078. break;
  2079. case IEEE80211_VHT_CHANWIDTH_160MHZ:
  2080. chandef->width = NL80211_CHAN_WIDTH_160;
  2081. break;
  2082. case IEEE80211_VHT_CHANWIDTH_80P80MHZ:
  2083. chandef->width = NL80211_CHAN_WIDTH_80P80;
  2084. break;
  2085. default:
  2086. break;
  2087. }
  2088. chandef->center_freq1 =
  2089. ieee80211_channel_to_frequency(oper->center_freq_seg1_idx,
  2090. control_chan->band);
  2091. chandef->center_freq2 =
  2092. ieee80211_channel_to_frequency(oper->center_freq_seg2_idx,
  2093. control_chan->band);
  2094. }
  2095. int ieee80211_parse_bitrates(struct cfg80211_chan_def *chandef,
  2096. const struct ieee80211_supported_band *sband,
  2097. const u8 *srates, int srates_len, u32 *rates)
  2098. {
  2099. u32 rate_flags = ieee80211_chandef_rate_flags(chandef);
  2100. int shift = ieee80211_chandef_get_shift(chandef);
  2101. struct ieee80211_rate *br;
  2102. int brate, rate, i, j, count = 0;
  2103. *rates = 0;
  2104. for (i = 0; i < srates_len; i++) {
  2105. rate = srates[i] & 0x7f;
  2106. for (j = 0; j < sband->n_bitrates; j++) {
  2107. br = &sband->bitrates[j];
  2108. if ((rate_flags & br->flags) != rate_flags)
  2109. continue;
  2110. brate = DIV_ROUND_UP(br->bitrate, (1 << shift) * 5);
  2111. if (brate == rate) {
  2112. *rates |= BIT(j);
  2113. count++;
  2114. break;
  2115. }
  2116. }
  2117. }
  2118. return count;
  2119. }
  2120. int ieee80211_add_srates_ie(struct ieee80211_sub_if_data *sdata,
  2121. struct sk_buff *skb, bool need_basic,
  2122. enum ieee80211_band band)
  2123. {
  2124. struct ieee80211_local *local = sdata->local;
  2125. struct ieee80211_supported_band *sband;
  2126. int rate, shift;
  2127. u8 i, rates, *pos;
  2128. u32 basic_rates = sdata->vif.bss_conf.basic_rates;
  2129. u32 rate_flags;
  2130. shift = ieee80211_vif_get_shift(&sdata->vif);
  2131. rate_flags = ieee80211_chandef_rate_flags(&sdata->vif.bss_conf.chandef);
  2132. sband = local->hw.wiphy->bands[band];
  2133. rates = 0;
  2134. for (i = 0; i < sband->n_bitrates; i++) {
  2135. if ((rate_flags & sband->bitrates[i].flags) != rate_flags)
  2136. continue;
  2137. rates++;
  2138. }
  2139. if (rates > 8)
  2140. rates = 8;
  2141. if (skb_tailroom(skb) < rates + 2)
  2142. return -ENOMEM;
  2143. pos = skb_put(skb, rates + 2);
  2144. *pos++ = WLAN_EID_SUPP_RATES;
  2145. *pos++ = rates;
  2146. for (i = 0; i < rates; i++) {
  2147. u8 basic = 0;
  2148. if ((rate_flags & sband->bitrates[i].flags) != rate_flags)
  2149. continue;
  2150. if (need_basic && basic_rates & BIT(i))
  2151. basic = 0x80;
  2152. rate = sband->bitrates[i].bitrate;
  2153. rate = DIV_ROUND_UP(sband->bitrates[i].bitrate,
  2154. 5 * (1 << shift));
  2155. *pos++ = basic | (u8) rate;
  2156. }
  2157. return 0;
  2158. }
  2159. int ieee80211_add_ext_srates_ie(struct ieee80211_sub_if_data *sdata,
  2160. struct sk_buff *skb, bool need_basic,
  2161. enum ieee80211_band band)
  2162. {
  2163. struct ieee80211_local *local = sdata->local;
  2164. struct ieee80211_supported_band *sband;
  2165. int rate, shift;
  2166. u8 i, exrates, *pos;
  2167. u32 basic_rates = sdata->vif.bss_conf.basic_rates;
  2168. u32 rate_flags;
  2169. rate_flags = ieee80211_chandef_rate_flags(&sdata->vif.bss_conf.chandef);
  2170. shift = ieee80211_vif_get_shift(&sdata->vif);
  2171. sband = local->hw.wiphy->bands[band];
  2172. exrates = 0;
  2173. for (i = 0; i < sband->n_bitrates; i++) {
  2174. if ((rate_flags & sband->bitrates[i].flags) != rate_flags)
  2175. continue;
  2176. exrates++;
  2177. }
  2178. if (exrates > 8)
  2179. exrates -= 8;
  2180. else
  2181. exrates = 0;
  2182. if (skb_tailroom(skb) < exrates + 2)
  2183. return -ENOMEM;
  2184. if (exrates) {
  2185. pos = skb_put(skb, exrates + 2);
  2186. *pos++ = WLAN_EID_EXT_SUPP_RATES;
  2187. *pos++ = exrates;
  2188. for (i = 8; i < sband->n_bitrates; i++) {
  2189. u8 basic = 0;
  2190. if ((rate_flags & sband->bitrates[i].flags)
  2191. != rate_flags)
  2192. continue;
  2193. if (need_basic && basic_rates & BIT(i))
  2194. basic = 0x80;
  2195. rate = DIV_ROUND_UP(sband->bitrates[i].bitrate,
  2196. 5 * (1 << shift));
  2197. *pos++ = basic | (u8) rate;
  2198. }
  2199. }
  2200. return 0;
  2201. }
  2202. int ieee80211_ave_rssi(struct ieee80211_vif *vif)
  2203. {
  2204. struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
  2205. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  2206. if (WARN_ON_ONCE(sdata->vif.type != NL80211_IFTYPE_STATION)) {
  2207. /* non-managed type inferfaces */
  2208. return 0;
  2209. }
  2210. return -ewma_beacon_signal_read(&ifmgd->ave_beacon_signal);
  2211. }
  2212. EXPORT_SYMBOL_GPL(ieee80211_ave_rssi);
  2213. u8 ieee80211_mcs_to_chains(const struct ieee80211_mcs_info *mcs)
  2214. {
  2215. if (!mcs)
  2216. return 1;
  2217. /* TODO: consider rx_highest */
  2218. if (mcs->rx_mask[3])
  2219. return 4;
  2220. if (mcs->rx_mask[2])
  2221. return 3;
  2222. if (mcs->rx_mask[1])
  2223. return 2;
  2224. return 1;
  2225. }
  2226. /**
  2227. * ieee80211_calculate_rx_timestamp - calculate timestamp in frame
  2228. * @local: mac80211 hw info struct
  2229. * @status: RX status
  2230. * @mpdu_len: total MPDU length (including FCS)
  2231. * @mpdu_offset: offset into MPDU to calculate timestamp at
  2232. *
  2233. * This function calculates the RX timestamp at the given MPDU offset, taking
  2234. * into account what the RX timestamp was. An offset of 0 will just normalize
  2235. * the timestamp to TSF at beginning of MPDU reception.
  2236. */
  2237. u64 ieee80211_calculate_rx_timestamp(struct ieee80211_local *local,
  2238. struct ieee80211_rx_status *status,
  2239. unsigned int mpdu_len,
  2240. unsigned int mpdu_offset)
  2241. {
  2242. u64 ts = status->mactime;
  2243. struct rate_info ri;
  2244. u16 rate;
  2245. if (WARN_ON(!ieee80211_have_rx_timestamp(status)))
  2246. return 0;
  2247. memset(&ri, 0, sizeof(ri));
  2248. /* Fill cfg80211 rate info */
  2249. if (status->flag & RX_FLAG_HT) {
  2250. ri.mcs = status->rate_idx;
  2251. ri.flags |= RATE_INFO_FLAGS_MCS;
  2252. if (status->flag & RX_FLAG_40MHZ)
  2253. ri.bw = RATE_INFO_BW_40;
  2254. else
  2255. ri.bw = RATE_INFO_BW_20;
  2256. if (status->flag & RX_FLAG_SHORT_GI)
  2257. ri.flags |= RATE_INFO_FLAGS_SHORT_GI;
  2258. } else if (status->flag & RX_FLAG_VHT) {
  2259. ri.flags |= RATE_INFO_FLAGS_VHT_MCS;
  2260. ri.mcs = status->rate_idx;
  2261. ri.nss = status->vht_nss;
  2262. if (status->flag & RX_FLAG_40MHZ)
  2263. ri.bw = RATE_INFO_BW_40;
  2264. else if (status->vht_flag & RX_VHT_FLAG_80MHZ)
  2265. ri.bw = RATE_INFO_BW_80;
  2266. else if (status->vht_flag & RX_VHT_FLAG_160MHZ)
  2267. ri.bw = RATE_INFO_BW_160;
  2268. else
  2269. ri.bw = RATE_INFO_BW_20;
  2270. if (status->flag & RX_FLAG_SHORT_GI)
  2271. ri.flags |= RATE_INFO_FLAGS_SHORT_GI;
  2272. } else {
  2273. struct ieee80211_supported_band *sband;
  2274. int shift = 0;
  2275. int bitrate;
  2276. if (status->flag & RX_FLAG_10MHZ) {
  2277. shift = 1;
  2278. ri.bw = RATE_INFO_BW_10;
  2279. } else if (status->flag & RX_FLAG_5MHZ) {
  2280. shift = 2;
  2281. ri.bw = RATE_INFO_BW_5;
  2282. } else {
  2283. ri.bw = RATE_INFO_BW_20;
  2284. }
  2285. sband = local->hw.wiphy->bands[status->band];
  2286. bitrate = sband->bitrates[status->rate_idx].bitrate;
  2287. ri.legacy = DIV_ROUND_UP(bitrate, (1 << shift));
  2288. }
  2289. rate = cfg80211_calculate_bitrate(&ri);
  2290. if (WARN_ONCE(!rate,
  2291. "Invalid bitrate: flags=0x%x, idx=%d, vht_nss=%d\n",
  2292. status->flag, status->rate_idx, status->vht_nss))
  2293. return 0;
  2294. /* rewind from end of MPDU */
  2295. if (status->flag & RX_FLAG_MACTIME_END)
  2296. ts -= mpdu_len * 8 * 10 / rate;
  2297. ts += mpdu_offset * 8 * 10 / rate;
  2298. return ts;
  2299. }
  2300. void ieee80211_dfs_cac_cancel(struct ieee80211_local *local)
  2301. {
  2302. struct ieee80211_sub_if_data *sdata;
  2303. struct cfg80211_chan_def chandef;
  2304. mutex_lock(&local->mtx);
  2305. mutex_lock(&local->iflist_mtx);
  2306. list_for_each_entry(sdata, &local->interfaces, list) {
  2307. /* it might be waiting for the local->mtx, but then
  2308. * by the time it gets it, sdata->wdev.cac_started
  2309. * will no longer be true
  2310. */
  2311. cancel_delayed_work(&sdata->dfs_cac_timer_work);
  2312. if (sdata->wdev.cac_started) {
  2313. chandef = sdata->vif.bss_conf.chandef;
  2314. ieee80211_vif_release_channel(sdata);
  2315. cfg80211_cac_event(sdata->dev,
  2316. &chandef,
  2317. NL80211_RADAR_CAC_ABORTED,
  2318. GFP_KERNEL);
  2319. }
  2320. }
  2321. mutex_unlock(&local->iflist_mtx);
  2322. mutex_unlock(&local->mtx);
  2323. }
  2324. void ieee80211_dfs_radar_detected_work(struct work_struct *work)
  2325. {
  2326. struct ieee80211_local *local =
  2327. container_of(work, struct ieee80211_local, radar_detected_work);
  2328. struct cfg80211_chan_def chandef = local->hw.conf.chandef;
  2329. struct ieee80211_chanctx *ctx;
  2330. int num_chanctx = 0;
  2331. mutex_lock(&local->chanctx_mtx);
  2332. list_for_each_entry(ctx, &local->chanctx_list, list) {
  2333. if (ctx->replace_state == IEEE80211_CHANCTX_REPLACES_OTHER)
  2334. continue;
  2335. num_chanctx++;
  2336. chandef = ctx->conf.def;
  2337. }
  2338. mutex_unlock(&local->chanctx_mtx);
  2339. ieee80211_dfs_cac_cancel(local);
  2340. if (num_chanctx > 1)
  2341. /* XXX: multi-channel is not supported yet */
  2342. WARN_ON(1);
  2343. else
  2344. cfg80211_radar_event(local->hw.wiphy, &chandef, GFP_KERNEL);
  2345. }
  2346. void ieee80211_radar_detected(struct ieee80211_hw *hw)
  2347. {
  2348. struct ieee80211_local *local = hw_to_local(hw);
  2349. trace_api_radar_detected(local);
  2350. ieee80211_queue_work(hw, &local->radar_detected_work);
  2351. }
  2352. EXPORT_SYMBOL(ieee80211_radar_detected);
  2353. u32 ieee80211_chandef_downgrade(struct cfg80211_chan_def *c)
  2354. {
  2355. u32 ret;
  2356. int tmp;
  2357. switch (c->width) {
  2358. case NL80211_CHAN_WIDTH_20:
  2359. c->width = NL80211_CHAN_WIDTH_20_NOHT;
  2360. ret = IEEE80211_STA_DISABLE_HT | IEEE80211_STA_DISABLE_VHT;
  2361. break;
  2362. case NL80211_CHAN_WIDTH_40:
  2363. c->width = NL80211_CHAN_WIDTH_20;
  2364. c->center_freq1 = c->chan->center_freq;
  2365. ret = IEEE80211_STA_DISABLE_40MHZ |
  2366. IEEE80211_STA_DISABLE_VHT;
  2367. break;
  2368. case NL80211_CHAN_WIDTH_80:
  2369. tmp = (30 + c->chan->center_freq - c->center_freq1)/20;
  2370. /* n_P40 */
  2371. tmp /= 2;
  2372. /* freq_P40 */
  2373. c->center_freq1 = c->center_freq1 - 20 + 40 * tmp;
  2374. c->width = NL80211_CHAN_WIDTH_40;
  2375. ret = IEEE80211_STA_DISABLE_VHT;
  2376. break;
  2377. case NL80211_CHAN_WIDTH_80P80:
  2378. c->center_freq2 = 0;
  2379. c->width = NL80211_CHAN_WIDTH_80;
  2380. ret = IEEE80211_STA_DISABLE_80P80MHZ |
  2381. IEEE80211_STA_DISABLE_160MHZ;
  2382. break;
  2383. case NL80211_CHAN_WIDTH_160:
  2384. /* n_P20 */
  2385. tmp = (70 + c->chan->center_freq - c->center_freq1)/20;
  2386. /* n_P80 */
  2387. tmp /= 4;
  2388. c->center_freq1 = c->center_freq1 - 40 + 80 * tmp;
  2389. c->width = NL80211_CHAN_WIDTH_80;
  2390. ret = IEEE80211_STA_DISABLE_80P80MHZ |
  2391. IEEE80211_STA_DISABLE_160MHZ;
  2392. break;
  2393. default:
  2394. case NL80211_CHAN_WIDTH_20_NOHT:
  2395. WARN_ON_ONCE(1);
  2396. c->width = NL80211_CHAN_WIDTH_20_NOHT;
  2397. ret = IEEE80211_STA_DISABLE_HT | IEEE80211_STA_DISABLE_VHT;
  2398. break;
  2399. case NL80211_CHAN_WIDTH_5:
  2400. case NL80211_CHAN_WIDTH_10:
  2401. WARN_ON_ONCE(1);
  2402. /* keep c->width */
  2403. ret = IEEE80211_STA_DISABLE_HT | IEEE80211_STA_DISABLE_VHT;
  2404. break;
  2405. }
  2406. WARN_ON_ONCE(!cfg80211_chandef_valid(c));
  2407. return ret;
  2408. }
  2409. /*
  2410. * Returns true if smps_mode_new is strictly more restrictive than
  2411. * smps_mode_old.
  2412. */
  2413. bool ieee80211_smps_is_restrictive(enum ieee80211_smps_mode smps_mode_old,
  2414. enum ieee80211_smps_mode smps_mode_new)
  2415. {
  2416. if (WARN_ON_ONCE(smps_mode_old == IEEE80211_SMPS_AUTOMATIC ||
  2417. smps_mode_new == IEEE80211_SMPS_AUTOMATIC))
  2418. return false;
  2419. switch (smps_mode_old) {
  2420. case IEEE80211_SMPS_STATIC:
  2421. return false;
  2422. case IEEE80211_SMPS_DYNAMIC:
  2423. return smps_mode_new == IEEE80211_SMPS_STATIC;
  2424. case IEEE80211_SMPS_OFF:
  2425. return smps_mode_new != IEEE80211_SMPS_OFF;
  2426. default:
  2427. WARN_ON(1);
  2428. }
  2429. return false;
  2430. }
  2431. int ieee80211_send_action_csa(struct ieee80211_sub_if_data *sdata,
  2432. struct cfg80211_csa_settings *csa_settings)
  2433. {
  2434. struct sk_buff *skb;
  2435. struct ieee80211_mgmt *mgmt;
  2436. struct ieee80211_local *local = sdata->local;
  2437. int freq;
  2438. int hdr_len = offsetof(struct ieee80211_mgmt, u.action.u.chan_switch) +
  2439. sizeof(mgmt->u.action.u.chan_switch);
  2440. u8 *pos;
  2441. if (sdata->vif.type != NL80211_IFTYPE_ADHOC &&
  2442. sdata->vif.type != NL80211_IFTYPE_MESH_POINT)
  2443. return -EOPNOTSUPP;
  2444. skb = dev_alloc_skb(local->tx_headroom + hdr_len +
  2445. 5 + /* channel switch announcement element */
  2446. 3 + /* secondary channel offset element */
  2447. 8); /* mesh channel switch parameters element */
  2448. if (!skb)
  2449. return -ENOMEM;
  2450. skb_reserve(skb, local->tx_headroom);
  2451. mgmt = (struct ieee80211_mgmt *)skb_put(skb, hdr_len);
  2452. memset(mgmt, 0, hdr_len);
  2453. mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
  2454. IEEE80211_STYPE_ACTION);
  2455. eth_broadcast_addr(mgmt->da);
  2456. memcpy(mgmt->sa, sdata->vif.addr, ETH_ALEN);
  2457. if (ieee80211_vif_is_mesh(&sdata->vif)) {
  2458. memcpy(mgmt->bssid, sdata->vif.addr, ETH_ALEN);
  2459. } else {
  2460. struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
  2461. memcpy(mgmt->bssid, ifibss->bssid, ETH_ALEN);
  2462. }
  2463. mgmt->u.action.category = WLAN_CATEGORY_SPECTRUM_MGMT;
  2464. mgmt->u.action.u.chan_switch.action_code = WLAN_ACTION_SPCT_CHL_SWITCH;
  2465. pos = skb_put(skb, 5);
  2466. *pos++ = WLAN_EID_CHANNEL_SWITCH; /* EID */
  2467. *pos++ = 3; /* IE length */
  2468. *pos++ = csa_settings->block_tx ? 1 : 0; /* CSA mode */
  2469. freq = csa_settings->chandef.chan->center_freq;
  2470. *pos++ = ieee80211_frequency_to_channel(freq); /* channel */
  2471. *pos++ = csa_settings->count; /* count */
  2472. if (csa_settings->chandef.width == NL80211_CHAN_WIDTH_40) {
  2473. enum nl80211_channel_type ch_type;
  2474. skb_put(skb, 3);
  2475. *pos++ = WLAN_EID_SECONDARY_CHANNEL_OFFSET; /* EID */
  2476. *pos++ = 1; /* IE length */
  2477. ch_type = cfg80211_get_chandef_type(&csa_settings->chandef);
  2478. if (ch_type == NL80211_CHAN_HT40PLUS)
  2479. *pos++ = IEEE80211_HT_PARAM_CHA_SEC_ABOVE;
  2480. else
  2481. *pos++ = IEEE80211_HT_PARAM_CHA_SEC_BELOW;
  2482. }
  2483. if (ieee80211_vif_is_mesh(&sdata->vif)) {
  2484. struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
  2485. skb_put(skb, 8);
  2486. *pos++ = WLAN_EID_CHAN_SWITCH_PARAM; /* EID */
  2487. *pos++ = 6; /* IE length */
  2488. *pos++ = sdata->u.mesh.mshcfg.dot11MeshTTL; /* Mesh TTL */
  2489. *pos = 0x00; /* Mesh Flag: Tx Restrict, Initiator, Reason */
  2490. *pos |= WLAN_EID_CHAN_SWITCH_PARAM_INITIATOR;
  2491. *pos++ |= csa_settings->block_tx ?
  2492. WLAN_EID_CHAN_SWITCH_PARAM_TX_RESTRICT : 0x00;
  2493. put_unaligned_le16(WLAN_REASON_MESH_CHAN, pos); /* Reason Cd */
  2494. pos += 2;
  2495. put_unaligned_le16(ifmsh->pre_value, pos);/* Precedence Value */
  2496. pos += 2;
  2497. }
  2498. ieee80211_tx_skb(sdata, skb);
  2499. return 0;
  2500. }
  2501. bool ieee80211_cs_valid(const struct ieee80211_cipher_scheme *cs)
  2502. {
  2503. return !(cs == NULL || cs->cipher == 0 ||
  2504. cs->hdr_len < cs->pn_len + cs->pn_off ||
  2505. cs->hdr_len <= cs->key_idx_off ||
  2506. cs->key_idx_shift > 7 ||
  2507. cs->key_idx_mask == 0);
  2508. }
  2509. bool ieee80211_cs_list_valid(const struct ieee80211_cipher_scheme *cs, int n)
  2510. {
  2511. int i;
  2512. /* Ensure we have enough iftype bitmap space for all iftype values */
  2513. WARN_ON((NUM_NL80211_IFTYPES / 8 + 1) > sizeof(cs[0].iftype));
  2514. for (i = 0; i < n; i++)
  2515. if (!ieee80211_cs_valid(&cs[i]))
  2516. return false;
  2517. return true;
  2518. }
  2519. const struct ieee80211_cipher_scheme *
  2520. ieee80211_cs_get(struct ieee80211_local *local, u32 cipher,
  2521. enum nl80211_iftype iftype)
  2522. {
  2523. const struct ieee80211_cipher_scheme *l = local->hw.cipher_schemes;
  2524. int n = local->hw.n_cipher_schemes;
  2525. int i;
  2526. const struct ieee80211_cipher_scheme *cs = NULL;
  2527. for (i = 0; i < n; i++) {
  2528. if (l[i].cipher == cipher) {
  2529. cs = &l[i];
  2530. break;
  2531. }
  2532. }
  2533. if (!cs || !(cs->iftype & BIT(iftype)))
  2534. return NULL;
  2535. return cs;
  2536. }
  2537. int ieee80211_cs_headroom(struct ieee80211_local *local,
  2538. struct cfg80211_crypto_settings *crypto,
  2539. enum nl80211_iftype iftype)
  2540. {
  2541. const struct ieee80211_cipher_scheme *cs;
  2542. int headroom = IEEE80211_ENCRYPT_HEADROOM;
  2543. int i;
  2544. for (i = 0; i < crypto->n_ciphers_pairwise; i++) {
  2545. cs = ieee80211_cs_get(local, crypto->ciphers_pairwise[i],
  2546. iftype);
  2547. if (cs && headroom < cs->hdr_len)
  2548. headroom = cs->hdr_len;
  2549. }
  2550. cs = ieee80211_cs_get(local, crypto->cipher_group, iftype);
  2551. if (cs && headroom < cs->hdr_len)
  2552. headroom = cs->hdr_len;
  2553. return headroom;
  2554. }
  2555. static bool
  2556. ieee80211_extend_noa_desc(struct ieee80211_noa_data *data, u32 tsf, int i)
  2557. {
  2558. s32 end = data->desc[i].start + data->desc[i].duration - (tsf + 1);
  2559. int skip;
  2560. if (end > 0)
  2561. return false;
  2562. /* One shot NOA */
  2563. if (data->count[i] == 1)
  2564. return false;
  2565. if (data->desc[i].interval == 0)
  2566. return false;
  2567. /* End time is in the past, check for repetitions */
  2568. skip = DIV_ROUND_UP(-end, data->desc[i].interval);
  2569. if (data->count[i] < 255) {
  2570. if (data->count[i] <= skip) {
  2571. data->count[i] = 0;
  2572. return false;
  2573. }
  2574. data->count[i] -= skip;
  2575. }
  2576. data->desc[i].start += skip * data->desc[i].interval;
  2577. return true;
  2578. }
  2579. static bool
  2580. ieee80211_extend_absent_time(struct ieee80211_noa_data *data, u32 tsf,
  2581. s32 *offset)
  2582. {
  2583. bool ret = false;
  2584. int i;
  2585. for (i = 0; i < IEEE80211_P2P_NOA_DESC_MAX; i++) {
  2586. s32 cur;
  2587. if (!data->count[i])
  2588. continue;
  2589. if (ieee80211_extend_noa_desc(data, tsf + *offset, i))
  2590. ret = true;
  2591. cur = data->desc[i].start - tsf;
  2592. if (cur > *offset)
  2593. continue;
  2594. cur = data->desc[i].start + data->desc[i].duration - tsf;
  2595. if (cur > *offset)
  2596. *offset = cur;
  2597. }
  2598. return ret;
  2599. }
  2600. static u32
  2601. ieee80211_get_noa_absent_time(struct ieee80211_noa_data *data, u32 tsf)
  2602. {
  2603. s32 offset = 0;
  2604. int tries = 0;
  2605. /*
  2606. * arbitrary limit, used to avoid infinite loops when combined NoA
  2607. * descriptors cover the full time period.
  2608. */
  2609. int max_tries = 5;
  2610. ieee80211_extend_absent_time(data, tsf, &offset);
  2611. do {
  2612. if (!ieee80211_extend_absent_time(data, tsf, &offset))
  2613. break;
  2614. tries++;
  2615. } while (tries < max_tries);
  2616. return offset;
  2617. }
  2618. void ieee80211_update_p2p_noa(struct ieee80211_noa_data *data, u32 tsf)
  2619. {
  2620. u32 next_offset = BIT(31) - 1;
  2621. int i;
  2622. data->absent = 0;
  2623. data->has_next_tsf = false;
  2624. for (i = 0; i < IEEE80211_P2P_NOA_DESC_MAX; i++) {
  2625. s32 start;
  2626. if (!data->count[i])
  2627. continue;
  2628. ieee80211_extend_noa_desc(data, tsf, i);
  2629. start = data->desc[i].start - tsf;
  2630. if (start <= 0)
  2631. data->absent |= BIT(i);
  2632. if (next_offset > start)
  2633. next_offset = start;
  2634. data->has_next_tsf = true;
  2635. }
  2636. if (data->absent)
  2637. next_offset = ieee80211_get_noa_absent_time(data, tsf);
  2638. data->next_tsf = tsf + next_offset;
  2639. }
  2640. EXPORT_SYMBOL(ieee80211_update_p2p_noa);
  2641. int ieee80211_parse_p2p_noa(const struct ieee80211_p2p_noa_attr *attr,
  2642. struct ieee80211_noa_data *data, u32 tsf)
  2643. {
  2644. int ret = 0;
  2645. int i;
  2646. memset(data, 0, sizeof(*data));
  2647. for (i = 0; i < IEEE80211_P2P_NOA_DESC_MAX; i++) {
  2648. const struct ieee80211_p2p_noa_desc *desc = &attr->desc[i];
  2649. if (!desc->count || !desc->duration)
  2650. continue;
  2651. data->count[i] = desc->count;
  2652. data->desc[i].start = le32_to_cpu(desc->start_time);
  2653. data->desc[i].duration = le32_to_cpu(desc->duration);
  2654. data->desc[i].interval = le32_to_cpu(desc->interval);
  2655. if (data->count[i] > 1 &&
  2656. data->desc[i].interval < data->desc[i].duration)
  2657. continue;
  2658. ieee80211_extend_noa_desc(data, tsf, i);
  2659. ret++;
  2660. }
  2661. if (ret)
  2662. ieee80211_update_p2p_noa(data, tsf);
  2663. return ret;
  2664. }
  2665. EXPORT_SYMBOL(ieee80211_parse_p2p_noa);
  2666. void ieee80211_recalc_dtim(struct ieee80211_local *local,
  2667. struct ieee80211_sub_if_data *sdata)
  2668. {
  2669. u64 tsf = drv_get_tsf(local, sdata);
  2670. u64 dtim_count = 0;
  2671. u16 beacon_int = sdata->vif.bss_conf.beacon_int * 1024;
  2672. u8 dtim_period = sdata->vif.bss_conf.dtim_period;
  2673. struct ps_data *ps;
  2674. u8 bcns_from_dtim;
  2675. if (tsf == -1ULL || !beacon_int || !dtim_period)
  2676. return;
  2677. if (sdata->vif.type == NL80211_IFTYPE_AP ||
  2678. sdata->vif.type == NL80211_IFTYPE_AP_VLAN) {
  2679. if (!sdata->bss)
  2680. return;
  2681. ps = &sdata->bss->ps;
  2682. } else if (ieee80211_vif_is_mesh(&sdata->vif)) {
  2683. ps = &sdata->u.mesh.ps;
  2684. } else {
  2685. return;
  2686. }
  2687. /*
  2688. * actually finds last dtim_count, mac80211 will update in
  2689. * __beacon_add_tim().
  2690. * dtim_count = dtim_period - (tsf / bcn_int) % dtim_period
  2691. */
  2692. do_div(tsf, beacon_int);
  2693. bcns_from_dtim = do_div(tsf, dtim_period);
  2694. /* just had a DTIM */
  2695. if (!bcns_from_dtim)
  2696. dtim_count = 0;
  2697. else
  2698. dtim_count = dtim_period - bcns_from_dtim;
  2699. ps->dtim_count = dtim_count;
  2700. }
  2701. static u8 ieee80211_chanctx_radar_detect(struct ieee80211_local *local,
  2702. struct ieee80211_chanctx *ctx)
  2703. {
  2704. struct ieee80211_sub_if_data *sdata;
  2705. u8 radar_detect = 0;
  2706. lockdep_assert_held(&local->chanctx_mtx);
  2707. if (WARN_ON(ctx->replace_state == IEEE80211_CHANCTX_WILL_BE_REPLACED))
  2708. return 0;
  2709. list_for_each_entry(sdata, &ctx->reserved_vifs, reserved_chanctx_list)
  2710. if (sdata->reserved_radar_required)
  2711. radar_detect |= BIT(sdata->reserved_chandef.width);
  2712. /*
  2713. * An in-place reservation context should not have any assigned vifs
  2714. * until it replaces the other context.
  2715. */
  2716. WARN_ON(ctx->replace_state == IEEE80211_CHANCTX_REPLACES_OTHER &&
  2717. !list_empty(&ctx->assigned_vifs));
  2718. list_for_each_entry(sdata, &ctx->assigned_vifs, assigned_chanctx_list)
  2719. if (sdata->radar_required)
  2720. radar_detect |= BIT(sdata->vif.bss_conf.chandef.width);
  2721. return radar_detect;
  2722. }
  2723. int ieee80211_check_combinations(struct ieee80211_sub_if_data *sdata,
  2724. const struct cfg80211_chan_def *chandef,
  2725. enum ieee80211_chanctx_mode chanmode,
  2726. u8 radar_detect)
  2727. {
  2728. struct ieee80211_local *local = sdata->local;
  2729. struct ieee80211_sub_if_data *sdata_iter;
  2730. enum nl80211_iftype iftype = sdata->wdev.iftype;
  2731. int num[NUM_NL80211_IFTYPES];
  2732. struct ieee80211_chanctx *ctx;
  2733. int num_different_channels = 0;
  2734. int total = 1;
  2735. lockdep_assert_held(&local->chanctx_mtx);
  2736. if (WARN_ON(hweight32(radar_detect) > 1))
  2737. return -EINVAL;
  2738. if (WARN_ON(chandef && chanmode == IEEE80211_CHANCTX_SHARED &&
  2739. !chandef->chan))
  2740. return -EINVAL;
  2741. if (chandef)
  2742. num_different_channels = 1;
  2743. if (WARN_ON(iftype >= NUM_NL80211_IFTYPES))
  2744. return -EINVAL;
  2745. /* Always allow software iftypes */
  2746. if (local->hw.wiphy->software_iftypes & BIT(iftype)) {
  2747. if (radar_detect)
  2748. return -EINVAL;
  2749. return 0;
  2750. }
  2751. memset(num, 0, sizeof(num));
  2752. if (iftype != NL80211_IFTYPE_UNSPECIFIED)
  2753. num[iftype] = 1;
  2754. list_for_each_entry(ctx, &local->chanctx_list, list) {
  2755. if (ctx->replace_state == IEEE80211_CHANCTX_WILL_BE_REPLACED)
  2756. continue;
  2757. radar_detect |= ieee80211_chanctx_radar_detect(local, ctx);
  2758. if (ctx->mode == IEEE80211_CHANCTX_EXCLUSIVE) {
  2759. num_different_channels++;
  2760. continue;
  2761. }
  2762. if (chandef && chanmode == IEEE80211_CHANCTX_SHARED &&
  2763. cfg80211_chandef_compatible(chandef,
  2764. &ctx->conf.def))
  2765. continue;
  2766. num_different_channels++;
  2767. }
  2768. list_for_each_entry_rcu(sdata_iter, &local->interfaces, list) {
  2769. struct wireless_dev *wdev_iter;
  2770. wdev_iter = &sdata_iter->wdev;
  2771. if (sdata_iter == sdata ||
  2772. !ieee80211_sdata_running(sdata_iter) ||
  2773. local->hw.wiphy->software_iftypes & BIT(wdev_iter->iftype))
  2774. continue;
  2775. num[wdev_iter->iftype]++;
  2776. total++;
  2777. }
  2778. if (total == 1 && !radar_detect)
  2779. return 0;
  2780. return cfg80211_check_combinations(local->hw.wiphy,
  2781. num_different_channels,
  2782. radar_detect, num);
  2783. }
  2784. static void
  2785. ieee80211_iter_max_chans(const struct ieee80211_iface_combination *c,
  2786. void *data)
  2787. {
  2788. u32 *max_num_different_channels = data;
  2789. *max_num_different_channels = max(*max_num_different_channels,
  2790. c->num_different_channels);
  2791. }
  2792. int ieee80211_max_num_channels(struct ieee80211_local *local)
  2793. {
  2794. struct ieee80211_sub_if_data *sdata;
  2795. int num[NUM_NL80211_IFTYPES] = {};
  2796. struct ieee80211_chanctx *ctx;
  2797. int num_different_channels = 0;
  2798. u8 radar_detect = 0;
  2799. u32 max_num_different_channels = 1;
  2800. int err;
  2801. lockdep_assert_held(&local->chanctx_mtx);
  2802. list_for_each_entry(ctx, &local->chanctx_list, list) {
  2803. if (ctx->replace_state == IEEE80211_CHANCTX_WILL_BE_REPLACED)
  2804. continue;
  2805. num_different_channels++;
  2806. radar_detect |= ieee80211_chanctx_radar_detect(local, ctx);
  2807. }
  2808. list_for_each_entry_rcu(sdata, &local->interfaces, list)
  2809. num[sdata->wdev.iftype]++;
  2810. err = cfg80211_iter_combinations(local->hw.wiphy,
  2811. num_different_channels, radar_detect,
  2812. num, ieee80211_iter_max_chans,
  2813. &max_num_different_channels);
  2814. if (err < 0)
  2815. return err;
  2816. return max_num_different_channels;
  2817. }
  2818. u8 *ieee80211_add_wmm_info_ie(u8 *buf, u8 qosinfo)
  2819. {
  2820. *buf++ = WLAN_EID_VENDOR_SPECIFIC;
  2821. *buf++ = 7; /* len */
  2822. *buf++ = 0x00; /* Microsoft OUI 00:50:F2 */
  2823. *buf++ = 0x50;
  2824. *buf++ = 0xf2;
  2825. *buf++ = 2; /* WME */
  2826. *buf++ = 0; /* WME info */
  2827. *buf++ = 1; /* WME ver */
  2828. *buf++ = qosinfo; /* U-APSD no in use */
  2829. return buf;
  2830. }
  2831. void ieee80211_init_tx_queue(struct ieee80211_sub_if_data *sdata,
  2832. struct sta_info *sta,
  2833. struct txq_info *txqi, int tid)
  2834. {
  2835. skb_queue_head_init(&txqi->queue);
  2836. txqi->txq.vif = &sdata->vif;
  2837. if (sta) {
  2838. txqi->txq.sta = &sta->sta;
  2839. sta->sta.txq[tid] = &txqi->txq;
  2840. txqi->txq.tid = tid;
  2841. txqi->txq.ac = ieee802_1d_to_ac[tid & 7];
  2842. } else {
  2843. sdata->vif.txq = &txqi->txq;
  2844. txqi->txq.tid = 0;
  2845. txqi->txq.ac = IEEE80211_AC_BE;
  2846. }
  2847. }