util.c 86 KB

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