util.c 76 KB

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