util.c 83 KB

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