util.c 87 KB

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