util.c 76 KB

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