util.c 83 KB

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