util.c 59 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297
  1. /*
  2. * Copyright 2002-2005, Instant802 Networks, Inc.
  3. * Copyright 2005-2006, Devicescape Software, Inc.
  4. * Copyright 2006-2007 Jiri Benc <jbenc@suse.cz>
  5. * Copyright 2007 Johannes Berg <johannes@sipsolutions.net>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. *
  11. * utilities for mac80211
  12. */
  13. #include <net/mac80211.h>
  14. #include <linux/netdevice.h>
  15. #include <linux/export.h>
  16. #include <linux/types.h>
  17. #include <linux/slab.h>
  18. #include <linux/skbuff.h>
  19. #include <linux/etherdevice.h>
  20. #include <linux/if_arp.h>
  21. #include <linux/bitmap.h>
  22. #include <linux/crc32.h>
  23. #include <net/net_namespace.h>
  24. #include <net/cfg80211.h>
  25. #include <net/rtnetlink.h>
  26. #include "ieee80211_i.h"
  27. #include "driver-ops.h"
  28. #include "rate.h"
  29. #include "mesh.h"
  30. #include "wme.h"
  31. #include "led.h"
  32. #include "wep.h"
  33. /* privid for wiphys to determine whether they belong to us or not */
  34. void *mac80211_wiphy_privid = &mac80211_wiphy_privid;
  35. struct ieee80211_hw *wiphy_to_ieee80211_hw(struct wiphy *wiphy)
  36. {
  37. struct ieee80211_local *local;
  38. BUG_ON(!wiphy);
  39. local = wiphy_priv(wiphy);
  40. return &local->hw;
  41. }
  42. EXPORT_SYMBOL(wiphy_to_ieee80211_hw);
  43. u8 *ieee80211_get_bssid(struct ieee80211_hdr *hdr, size_t len,
  44. enum nl80211_iftype type)
  45. {
  46. __le16 fc = hdr->frame_control;
  47. /* drop ACK/CTS frames and incorrect hdr len (ctrl) */
  48. if (len < 16)
  49. return NULL;
  50. if (ieee80211_is_data(fc)) {
  51. if (len < 24) /* drop incorrect hdr len (data) */
  52. return NULL;
  53. if (ieee80211_has_a4(fc))
  54. return NULL;
  55. if (ieee80211_has_tods(fc))
  56. return hdr->addr1;
  57. if (ieee80211_has_fromds(fc))
  58. return hdr->addr2;
  59. return hdr->addr3;
  60. }
  61. if (ieee80211_is_mgmt(fc)) {
  62. if (len < 24) /* drop incorrect hdr len (mgmt) */
  63. return NULL;
  64. return hdr->addr3;
  65. }
  66. if (ieee80211_is_ctl(fc)) {
  67. if(ieee80211_is_pspoll(fc))
  68. return hdr->addr1;
  69. if (ieee80211_is_back_req(fc)) {
  70. switch (type) {
  71. case NL80211_IFTYPE_STATION:
  72. return hdr->addr2;
  73. case NL80211_IFTYPE_AP:
  74. case NL80211_IFTYPE_AP_VLAN:
  75. return hdr->addr1;
  76. default:
  77. break; /* fall through to the return */
  78. }
  79. }
  80. }
  81. return NULL;
  82. }
  83. void ieee80211_tx_set_protected(struct ieee80211_tx_data *tx)
  84. {
  85. struct sk_buff *skb;
  86. struct ieee80211_hdr *hdr;
  87. skb_queue_walk(&tx->skbs, skb) {
  88. hdr = (struct ieee80211_hdr *) skb->data;
  89. hdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_PROTECTED);
  90. }
  91. }
  92. int ieee80211_frame_duration(enum ieee80211_band band, size_t len,
  93. int rate, int erp, int short_preamble,
  94. int shift)
  95. {
  96. int dur;
  97. /* calculate duration (in microseconds, rounded up to next higher
  98. * integer if it includes a fractional microsecond) to send frame of
  99. * len bytes (does not include FCS) at the given rate. Duration will
  100. * also include SIFS.
  101. *
  102. * rate is in 100 kbps, so divident is multiplied by 10 in the
  103. * DIV_ROUND_UP() operations.
  104. *
  105. * shift may be 2 for 5 MHz channels or 1 for 10 MHz channels, and
  106. * is assumed to be 0 otherwise.
  107. */
  108. if (band == IEEE80211_BAND_5GHZ || erp) {
  109. /*
  110. * OFDM:
  111. *
  112. * N_DBPS = DATARATE x 4
  113. * N_SYM = Ceiling((16+8xLENGTH+6) / N_DBPS)
  114. * (16 = SIGNAL time, 6 = tail bits)
  115. * TXTIME = T_PREAMBLE + T_SIGNAL + T_SYM x N_SYM + Signal Ext
  116. *
  117. * T_SYM = 4 usec
  118. * 802.11a - 18.5.2: aSIFSTime = 16 usec
  119. * 802.11g - 19.8.4: aSIFSTime = 10 usec +
  120. * signal ext = 6 usec
  121. */
  122. dur = 16; /* SIFS + signal ext */
  123. dur += 16; /* IEEE 802.11-2012 18.3.2.4: T_PREAMBLE = 16 usec */
  124. dur += 4; /* IEEE 802.11-2012 18.3.2.4: T_SIGNAL = 4 usec */
  125. /* IEEE 802.11-2012 18.3.2.4: all values above are:
  126. * * times 4 for 5 MHz
  127. * * times 2 for 10 MHz
  128. */
  129. dur *= 1 << shift;
  130. /* rates should already consider the channel bandwidth,
  131. * don't apply divisor again.
  132. */
  133. dur += 4 * DIV_ROUND_UP((16 + 8 * (len + 4) + 6) * 10,
  134. 4 * rate); /* T_SYM x N_SYM */
  135. } else {
  136. /*
  137. * 802.11b or 802.11g with 802.11b compatibility:
  138. * 18.3.4: TXTIME = PreambleLength + PLCPHeaderTime +
  139. * Ceiling(((LENGTH+PBCC)x8)/DATARATE). PBCC=0.
  140. *
  141. * 802.11 (DS): 15.3.3, 802.11b: 18.3.4
  142. * aSIFSTime = 10 usec
  143. * aPreambleLength = 144 usec or 72 usec with short preamble
  144. * aPLCPHeaderLength = 48 usec or 24 usec with short preamble
  145. */
  146. dur = 10; /* aSIFSTime = 10 usec */
  147. dur += short_preamble ? (72 + 24) : (144 + 48);
  148. dur += DIV_ROUND_UP(8 * (len + 4) * 10, rate);
  149. }
  150. return dur;
  151. }
  152. /* Exported duration function for driver use */
  153. __le16 ieee80211_generic_frame_duration(struct ieee80211_hw *hw,
  154. struct ieee80211_vif *vif,
  155. enum ieee80211_band band,
  156. size_t frame_len,
  157. struct ieee80211_rate *rate)
  158. {
  159. struct ieee80211_sub_if_data *sdata;
  160. u16 dur;
  161. int erp, shift = 0;
  162. bool short_preamble = false;
  163. erp = 0;
  164. if (vif) {
  165. sdata = vif_to_sdata(vif);
  166. short_preamble = sdata->vif.bss_conf.use_short_preamble;
  167. if (sdata->flags & IEEE80211_SDATA_OPERATING_GMODE)
  168. erp = rate->flags & IEEE80211_RATE_ERP_G;
  169. shift = ieee80211_vif_get_shift(vif);
  170. }
  171. dur = ieee80211_frame_duration(band, frame_len, rate->bitrate, erp,
  172. short_preamble, shift);
  173. return cpu_to_le16(dur);
  174. }
  175. EXPORT_SYMBOL(ieee80211_generic_frame_duration);
  176. __le16 ieee80211_rts_duration(struct ieee80211_hw *hw,
  177. struct ieee80211_vif *vif, size_t frame_len,
  178. const struct ieee80211_tx_info *frame_txctl)
  179. {
  180. struct ieee80211_local *local = hw_to_local(hw);
  181. struct ieee80211_rate *rate;
  182. struct ieee80211_sub_if_data *sdata;
  183. bool short_preamble;
  184. int erp, shift = 0, bitrate;
  185. u16 dur;
  186. struct ieee80211_supported_band *sband;
  187. sband = local->hw.wiphy->bands[frame_txctl->band];
  188. short_preamble = false;
  189. rate = &sband->bitrates[frame_txctl->control.rts_cts_rate_idx];
  190. erp = 0;
  191. if (vif) {
  192. sdata = vif_to_sdata(vif);
  193. short_preamble = sdata->vif.bss_conf.use_short_preamble;
  194. if (sdata->flags & IEEE80211_SDATA_OPERATING_GMODE)
  195. erp = rate->flags & IEEE80211_RATE_ERP_G;
  196. shift = ieee80211_vif_get_shift(vif);
  197. }
  198. bitrate = DIV_ROUND_UP(rate->bitrate, 1 << shift);
  199. /* CTS duration */
  200. dur = ieee80211_frame_duration(sband->band, 10, bitrate,
  201. erp, short_preamble, shift);
  202. /* Data frame duration */
  203. dur += ieee80211_frame_duration(sband->band, frame_len, bitrate,
  204. erp, short_preamble, shift);
  205. /* ACK duration */
  206. dur += ieee80211_frame_duration(sband->band, 10, bitrate,
  207. erp, short_preamble, shift);
  208. return cpu_to_le16(dur);
  209. }
  210. EXPORT_SYMBOL(ieee80211_rts_duration);
  211. __le16 ieee80211_ctstoself_duration(struct ieee80211_hw *hw,
  212. struct ieee80211_vif *vif,
  213. size_t frame_len,
  214. const struct ieee80211_tx_info *frame_txctl)
  215. {
  216. struct ieee80211_local *local = hw_to_local(hw);
  217. struct ieee80211_rate *rate;
  218. struct ieee80211_sub_if_data *sdata;
  219. bool short_preamble;
  220. int erp, shift = 0, bitrate;
  221. u16 dur;
  222. struct ieee80211_supported_band *sband;
  223. sband = local->hw.wiphy->bands[frame_txctl->band];
  224. short_preamble = false;
  225. rate = &sband->bitrates[frame_txctl->control.rts_cts_rate_idx];
  226. erp = 0;
  227. if (vif) {
  228. sdata = vif_to_sdata(vif);
  229. short_preamble = sdata->vif.bss_conf.use_short_preamble;
  230. if (sdata->flags & IEEE80211_SDATA_OPERATING_GMODE)
  231. erp = rate->flags & IEEE80211_RATE_ERP_G;
  232. shift = ieee80211_vif_get_shift(vif);
  233. }
  234. bitrate = DIV_ROUND_UP(rate->bitrate, 1 << shift);
  235. /* Data frame duration */
  236. dur = ieee80211_frame_duration(sband->band, frame_len, bitrate,
  237. erp, short_preamble, shift);
  238. if (!(frame_txctl->flags & IEEE80211_TX_CTL_NO_ACK)) {
  239. /* ACK duration */
  240. dur += ieee80211_frame_duration(sband->band, 10, bitrate,
  241. erp, short_preamble, shift);
  242. }
  243. return cpu_to_le16(dur);
  244. }
  245. EXPORT_SYMBOL(ieee80211_ctstoself_duration);
  246. void ieee80211_propagate_queue_wake(struct ieee80211_local *local, int queue)
  247. {
  248. struct ieee80211_sub_if_data *sdata;
  249. int n_acs = IEEE80211_NUM_ACS;
  250. if (local->hw.queues < IEEE80211_NUM_ACS)
  251. n_acs = 1;
  252. list_for_each_entry_rcu(sdata, &local->interfaces, list) {
  253. int ac;
  254. if (!sdata->dev)
  255. continue;
  256. if (test_bit(SDATA_STATE_OFFCHANNEL, &sdata->state))
  257. continue;
  258. if (sdata->vif.cab_queue != IEEE80211_INVAL_HW_QUEUE &&
  259. local->queue_stop_reasons[sdata->vif.cab_queue] != 0)
  260. continue;
  261. for (ac = 0; ac < n_acs; ac++) {
  262. int ac_queue = sdata->vif.hw_queue[ac];
  263. if (ac_queue == queue ||
  264. (sdata->vif.cab_queue == queue &&
  265. local->queue_stop_reasons[ac_queue] == 0 &&
  266. skb_queue_empty(&local->pending[ac_queue])))
  267. netif_wake_subqueue(sdata->dev, ac);
  268. }
  269. }
  270. }
  271. static void __ieee80211_wake_queue(struct ieee80211_hw *hw, int queue,
  272. enum queue_stop_reason reason)
  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. __clear_bit(reason, &local->queue_stop_reasons[queue]);
  281. if (local->queue_stop_reasons[queue] != 0)
  282. /* someone still has this queue stopped */
  283. return;
  284. if (skb_queue_empty(&local->pending[queue])) {
  285. rcu_read_lock();
  286. ieee80211_propagate_queue_wake(local, queue);
  287. rcu_read_unlock();
  288. } else
  289. tasklet_schedule(&local->tx_pending_tasklet);
  290. }
  291. void ieee80211_wake_queue_by_reason(struct ieee80211_hw *hw, int queue,
  292. enum queue_stop_reason reason)
  293. {
  294. struct ieee80211_local *local = hw_to_local(hw);
  295. unsigned long flags;
  296. spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
  297. __ieee80211_wake_queue(hw, queue, reason);
  298. spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
  299. }
  300. void ieee80211_wake_queue(struct ieee80211_hw *hw, int queue)
  301. {
  302. ieee80211_wake_queue_by_reason(hw, queue,
  303. IEEE80211_QUEUE_STOP_REASON_DRIVER);
  304. }
  305. EXPORT_SYMBOL(ieee80211_wake_queue);
  306. static void __ieee80211_stop_queue(struct ieee80211_hw *hw, int queue,
  307. enum queue_stop_reason reason)
  308. {
  309. struct ieee80211_local *local = hw_to_local(hw);
  310. struct ieee80211_sub_if_data *sdata;
  311. int n_acs = IEEE80211_NUM_ACS;
  312. trace_stop_queue(local, queue, reason);
  313. if (WARN_ON(queue >= hw->queues))
  314. return;
  315. if (test_bit(reason, &local->queue_stop_reasons[queue]))
  316. return;
  317. __set_bit(reason, &local->queue_stop_reasons[queue]);
  318. if (local->hw.queues < IEEE80211_NUM_ACS)
  319. n_acs = 1;
  320. rcu_read_lock();
  321. list_for_each_entry_rcu(sdata, &local->interfaces, list) {
  322. int ac;
  323. if (!sdata->dev)
  324. continue;
  325. for (ac = 0; ac < n_acs; ac++) {
  326. if (sdata->vif.hw_queue[ac] == queue ||
  327. sdata->vif.cab_queue == queue)
  328. netif_stop_subqueue(sdata->dev, ac);
  329. }
  330. }
  331. rcu_read_unlock();
  332. }
  333. void ieee80211_stop_queue_by_reason(struct ieee80211_hw *hw, int queue,
  334. enum queue_stop_reason reason)
  335. {
  336. struct ieee80211_local *local = hw_to_local(hw);
  337. unsigned long flags;
  338. spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
  339. __ieee80211_stop_queue(hw, queue, reason);
  340. spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
  341. }
  342. void ieee80211_stop_queue(struct ieee80211_hw *hw, int queue)
  343. {
  344. ieee80211_stop_queue_by_reason(hw, queue,
  345. IEEE80211_QUEUE_STOP_REASON_DRIVER);
  346. }
  347. EXPORT_SYMBOL(ieee80211_stop_queue);
  348. void ieee80211_add_pending_skb(struct ieee80211_local *local,
  349. struct sk_buff *skb)
  350. {
  351. struct ieee80211_hw *hw = &local->hw;
  352. unsigned long flags;
  353. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  354. int queue = info->hw_queue;
  355. if (WARN_ON(!info->control.vif)) {
  356. ieee80211_free_txskb(&local->hw, skb);
  357. return;
  358. }
  359. spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
  360. __ieee80211_stop_queue(hw, queue, IEEE80211_QUEUE_STOP_REASON_SKB_ADD);
  361. __skb_queue_tail(&local->pending[queue], skb);
  362. __ieee80211_wake_queue(hw, queue, IEEE80211_QUEUE_STOP_REASON_SKB_ADD);
  363. spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
  364. }
  365. void ieee80211_add_pending_skbs_fn(struct ieee80211_local *local,
  366. struct sk_buff_head *skbs,
  367. void (*fn)(void *data), void *data)
  368. {
  369. struct ieee80211_hw *hw = &local->hw;
  370. struct sk_buff *skb;
  371. unsigned long flags;
  372. int queue, i;
  373. spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
  374. while ((skb = skb_dequeue(skbs))) {
  375. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  376. if (WARN_ON(!info->control.vif)) {
  377. ieee80211_free_txskb(&local->hw, skb);
  378. continue;
  379. }
  380. queue = info->hw_queue;
  381. __ieee80211_stop_queue(hw, queue,
  382. IEEE80211_QUEUE_STOP_REASON_SKB_ADD);
  383. __skb_queue_tail(&local->pending[queue], skb);
  384. }
  385. if (fn)
  386. fn(data);
  387. for (i = 0; i < hw->queues; i++)
  388. __ieee80211_wake_queue(hw, i,
  389. IEEE80211_QUEUE_STOP_REASON_SKB_ADD);
  390. spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
  391. }
  392. void ieee80211_stop_queues_by_reason(struct ieee80211_hw *hw,
  393. unsigned long queues,
  394. enum queue_stop_reason reason)
  395. {
  396. struct ieee80211_local *local = hw_to_local(hw);
  397. unsigned long flags;
  398. int i;
  399. spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
  400. for_each_set_bit(i, &queues, hw->queues)
  401. __ieee80211_stop_queue(hw, i, reason);
  402. spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
  403. }
  404. void ieee80211_stop_queues(struct ieee80211_hw *hw)
  405. {
  406. ieee80211_stop_queues_by_reason(hw, IEEE80211_MAX_QUEUE_MAP,
  407. IEEE80211_QUEUE_STOP_REASON_DRIVER);
  408. }
  409. EXPORT_SYMBOL(ieee80211_stop_queues);
  410. int ieee80211_queue_stopped(struct ieee80211_hw *hw, int queue)
  411. {
  412. struct ieee80211_local *local = hw_to_local(hw);
  413. unsigned long flags;
  414. int ret;
  415. if (WARN_ON(queue >= hw->queues))
  416. return true;
  417. spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
  418. ret = test_bit(IEEE80211_QUEUE_STOP_REASON_DRIVER,
  419. &local->queue_stop_reasons[queue]);
  420. spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
  421. return ret;
  422. }
  423. EXPORT_SYMBOL(ieee80211_queue_stopped);
  424. void ieee80211_wake_queues_by_reason(struct ieee80211_hw *hw,
  425. unsigned long queues,
  426. enum queue_stop_reason reason)
  427. {
  428. struct ieee80211_local *local = hw_to_local(hw);
  429. unsigned long flags;
  430. int i;
  431. spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
  432. for_each_set_bit(i, &queues, hw->queues)
  433. __ieee80211_wake_queue(hw, i, reason);
  434. spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
  435. }
  436. void ieee80211_wake_queues(struct ieee80211_hw *hw)
  437. {
  438. ieee80211_wake_queues_by_reason(hw, IEEE80211_MAX_QUEUE_MAP,
  439. IEEE80211_QUEUE_STOP_REASON_DRIVER);
  440. }
  441. EXPORT_SYMBOL(ieee80211_wake_queues);
  442. void ieee80211_flush_queues(struct ieee80211_local *local,
  443. struct ieee80211_sub_if_data *sdata)
  444. {
  445. u32 queues;
  446. if (!local->ops->flush)
  447. return;
  448. if (sdata && local->hw.flags & IEEE80211_HW_QUEUE_CONTROL) {
  449. int ac;
  450. queues = 0;
  451. for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
  452. queues |= BIT(sdata->vif.hw_queue[ac]);
  453. if (sdata->vif.cab_queue != IEEE80211_INVAL_HW_QUEUE)
  454. queues |= BIT(sdata->vif.cab_queue);
  455. } else {
  456. /* all queues */
  457. queues = BIT(local->hw.queues) - 1;
  458. }
  459. ieee80211_stop_queues_by_reason(&local->hw, IEEE80211_MAX_QUEUE_MAP,
  460. IEEE80211_QUEUE_STOP_REASON_FLUSH);
  461. drv_flush(local, queues, false);
  462. ieee80211_wake_queues_by_reason(&local->hw, IEEE80211_MAX_QUEUE_MAP,
  463. IEEE80211_QUEUE_STOP_REASON_FLUSH);
  464. }
  465. void ieee80211_iterate_active_interfaces(
  466. struct ieee80211_hw *hw, u32 iter_flags,
  467. void (*iterator)(void *data, u8 *mac,
  468. struct ieee80211_vif *vif),
  469. void *data)
  470. {
  471. struct ieee80211_local *local = hw_to_local(hw);
  472. struct ieee80211_sub_if_data *sdata;
  473. mutex_lock(&local->iflist_mtx);
  474. list_for_each_entry(sdata, &local->interfaces, list) {
  475. switch (sdata->vif.type) {
  476. case NL80211_IFTYPE_MONITOR:
  477. if (!(sdata->u.mntr_flags & MONITOR_FLAG_ACTIVE))
  478. continue;
  479. break;
  480. case NL80211_IFTYPE_AP_VLAN:
  481. continue;
  482. default:
  483. break;
  484. }
  485. if (!(iter_flags & IEEE80211_IFACE_ITER_RESUME_ALL) &&
  486. !(sdata->flags & IEEE80211_SDATA_IN_DRIVER))
  487. continue;
  488. if (ieee80211_sdata_running(sdata))
  489. iterator(data, sdata->vif.addr,
  490. &sdata->vif);
  491. }
  492. sdata = rcu_dereference_protected(local->monitor_sdata,
  493. lockdep_is_held(&local->iflist_mtx));
  494. if (sdata &&
  495. (iter_flags & IEEE80211_IFACE_ITER_RESUME_ALL ||
  496. sdata->flags & IEEE80211_SDATA_IN_DRIVER))
  497. iterator(data, sdata->vif.addr, &sdata->vif);
  498. mutex_unlock(&local->iflist_mtx);
  499. }
  500. EXPORT_SYMBOL_GPL(ieee80211_iterate_active_interfaces);
  501. void ieee80211_iterate_active_interfaces_atomic(
  502. struct ieee80211_hw *hw, u32 iter_flags,
  503. void (*iterator)(void *data, u8 *mac,
  504. struct ieee80211_vif *vif),
  505. void *data)
  506. {
  507. struct ieee80211_local *local = hw_to_local(hw);
  508. struct ieee80211_sub_if_data *sdata;
  509. rcu_read_lock();
  510. list_for_each_entry_rcu(sdata, &local->interfaces, list) {
  511. switch (sdata->vif.type) {
  512. case NL80211_IFTYPE_MONITOR:
  513. if (!(sdata->u.mntr_flags & MONITOR_FLAG_ACTIVE))
  514. continue;
  515. break;
  516. case NL80211_IFTYPE_AP_VLAN:
  517. continue;
  518. default:
  519. break;
  520. }
  521. if (!(iter_flags & IEEE80211_IFACE_ITER_RESUME_ALL) &&
  522. !(sdata->flags & IEEE80211_SDATA_IN_DRIVER))
  523. continue;
  524. if (ieee80211_sdata_running(sdata))
  525. iterator(data, sdata->vif.addr,
  526. &sdata->vif);
  527. }
  528. sdata = rcu_dereference(local->monitor_sdata);
  529. if (sdata &&
  530. (iter_flags & IEEE80211_IFACE_ITER_RESUME_ALL ||
  531. sdata->flags & IEEE80211_SDATA_IN_DRIVER))
  532. iterator(data, sdata->vif.addr, &sdata->vif);
  533. rcu_read_unlock();
  534. }
  535. EXPORT_SYMBOL_GPL(ieee80211_iterate_active_interfaces_atomic);
  536. /*
  537. * Nothing should have been stuffed into the workqueue during
  538. * the suspend->resume cycle. If this WARN is seen then there
  539. * is a bug with either the driver suspend or something in
  540. * mac80211 stuffing into the workqueue which we haven't yet
  541. * cleared during mac80211's suspend cycle.
  542. */
  543. static bool ieee80211_can_queue_work(struct ieee80211_local *local)
  544. {
  545. if (WARN(local->suspended && !local->resuming,
  546. "queueing ieee80211 work while going to suspend\n"))
  547. return false;
  548. return true;
  549. }
  550. void ieee80211_queue_work(struct ieee80211_hw *hw, struct work_struct *work)
  551. {
  552. struct ieee80211_local *local = hw_to_local(hw);
  553. if (!ieee80211_can_queue_work(local))
  554. return;
  555. queue_work(local->workqueue, work);
  556. }
  557. EXPORT_SYMBOL(ieee80211_queue_work);
  558. void ieee80211_queue_delayed_work(struct ieee80211_hw *hw,
  559. struct delayed_work *dwork,
  560. unsigned long delay)
  561. {
  562. struct ieee80211_local *local = hw_to_local(hw);
  563. if (!ieee80211_can_queue_work(local))
  564. return;
  565. queue_delayed_work(local->workqueue, dwork, delay);
  566. }
  567. EXPORT_SYMBOL(ieee80211_queue_delayed_work);
  568. u32 ieee802_11_parse_elems_crc(const u8 *start, size_t len, bool action,
  569. struct ieee802_11_elems *elems,
  570. u64 filter, u32 crc)
  571. {
  572. size_t left = len;
  573. const u8 *pos = start;
  574. bool calc_crc = filter != 0;
  575. DECLARE_BITMAP(seen_elems, 256);
  576. const u8 *ie;
  577. bitmap_zero(seen_elems, 256);
  578. memset(elems, 0, sizeof(*elems));
  579. elems->ie_start = start;
  580. elems->total_len = len;
  581. while (left >= 2) {
  582. u8 id, elen;
  583. bool elem_parse_failed;
  584. id = *pos++;
  585. elen = *pos++;
  586. left -= 2;
  587. if (elen > left) {
  588. elems->parse_error = true;
  589. break;
  590. }
  591. switch (id) {
  592. case WLAN_EID_SSID:
  593. case WLAN_EID_SUPP_RATES:
  594. case WLAN_EID_FH_PARAMS:
  595. case WLAN_EID_DS_PARAMS:
  596. case WLAN_EID_CF_PARAMS:
  597. case WLAN_EID_TIM:
  598. case WLAN_EID_IBSS_PARAMS:
  599. case WLAN_EID_CHALLENGE:
  600. case WLAN_EID_RSN:
  601. case WLAN_EID_ERP_INFO:
  602. case WLAN_EID_EXT_SUPP_RATES:
  603. case WLAN_EID_HT_CAPABILITY:
  604. case WLAN_EID_HT_OPERATION:
  605. case WLAN_EID_VHT_CAPABILITY:
  606. case WLAN_EID_VHT_OPERATION:
  607. case WLAN_EID_MESH_ID:
  608. case WLAN_EID_MESH_CONFIG:
  609. case WLAN_EID_PEER_MGMT:
  610. case WLAN_EID_PREQ:
  611. case WLAN_EID_PREP:
  612. case WLAN_EID_PERR:
  613. case WLAN_EID_RANN:
  614. case WLAN_EID_CHANNEL_SWITCH:
  615. case WLAN_EID_EXT_CHANSWITCH_ANN:
  616. case WLAN_EID_COUNTRY:
  617. case WLAN_EID_PWR_CONSTRAINT:
  618. case WLAN_EID_TIMEOUT_INTERVAL:
  619. case WLAN_EID_SECONDARY_CHANNEL_OFFSET:
  620. case WLAN_EID_WIDE_BW_CHANNEL_SWITCH:
  621. /*
  622. * not listing WLAN_EID_CHANNEL_SWITCH_WRAPPER -- it seems possible
  623. * that if the content gets bigger it might be needed more than once
  624. */
  625. if (test_bit(id, seen_elems)) {
  626. elems->parse_error = true;
  627. left -= elen;
  628. pos += elen;
  629. continue;
  630. }
  631. break;
  632. }
  633. if (calc_crc && id < 64 && (filter & (1ULL << id)))
  634. crc = crc32_be(crc, pos - 2, elen + 2);
  635. elem_parse_failed = false;
  636. switch (id) {
  637. case WLAN_EID_SSID:
  638. elems->ssid = pos;
  639. elems->ssid_len = elen;
  640. break;
  641. case WLAN_EID_SUPP_RATES:
  642. elems->supp_rates = pos;
  643. elems->supp_rates_len = elen;
  644. break;
  645. case WLAN_EID_DS_PARAMS:
  646. if (elen >= 1)
  647. elems->ds_params = pos;
  648. else
  649. elem_parse_failed = true;
  650. break;
  651. case WLAN_EID_TIM:
  652. if (elen >= sizeof(struct ieee80211_tim_ie)) {
  653. elems->tim = (void *)pos;
  654. elems->tim_len = elen;
  655. } else
  656. elem_parse_failed = true;
  657. break;
  658. case WLAN_EID_CHALLENGE:
  659. elems->challenge = pos;
  660. elems->challenge_len = elen;
  661. break;
  662. case WLAN_EID_VENDOR_SPECIFIC:
  663. if (elen >= 4 && pos[0] == 0x00 && pos[1] == 0x50 &&
  664. pos[2] == 0xf2) {
  665. /* Microsoft OUI (00:50:F2) */
  666. if (calc_crc)
  667. crc = crc32_be(crc, pos - 2, elen + 2);
  668. if (elen >= 5 && pos[3] == 2) {
  669. /* OUI Type 2 - WMM IE */
  670. if (pos[4] == 0) {
  671. elems->wmm_info = pos;
  672. elems->wmm_info_len = elen;
  673. } else if (pos[4] == 1) {
  674. elems->wmm_param = pos;
  675. elems->wmm_param_len = elen;
  676. }
  677. }
  678. }
  679. break;
  680. case WLAN_EID_RSN:
  681. elems->rsn = pos;
  682. elems->rsn_len = elen;
  683. break;
  684. case WLAN_EID_ERP_INFO:
  685. if (elen >= 1)
  686. elems->erp_info = pos;
  687. else
  688. elem_parse_failed = true;
  689. break;
  690. case WLAN_EID_EXT_SUPP_RATES:
  691. elems->ext_supp_rates = pos;
  692. elems->ext_supp_rates_len = elen;
  693. break;
  694. case WLAN_EID_HT_CAPABILITY:
  695. if (elen >= sizeof(struct ieee80211_ht_cap))
  696. elems->ht_cap_elem = (void *)pos;
  697. else
  698. elem_parse_failed = true;
  699. break;
  700. case WLAN_EID_HT_OPERATION:
  701. if (elen >= sizeof(struct ieee80211_ht_operation))
  702. elems->ht_operation = (void *)pos;
  703. else
  704. elem_parse_failed = true;
  705. break;
  706. case WLAN_EID_VHT_CAPABILITY:
  707. if (elen >= sizeof(struct ieee80211_vht_cap))
  708. elems->vht_cap_elem = (void *)pos;
  709. else
  710. elem_parse_failed = true;
  711. break;
  712. case WLAN_EID_VHT_OPERATION:
  713. if (elen >= sizeof(struct ieee80211_vht_operation))
  714. elems->vht_operation = (void *)pos;
  715. else
  716. elem_parse_failed = true;
  717. break;
  718. case WLAN_EID_OPMODE_NOTIF:
  719. if (elen > 0)
  720. elems->opmode_notif = pos;
  721. else
  722. elem_parse_failed = true;
  723. break;
  724. case WLAN_EID_MESH_ID:
  725. elems->mesh_id = pos;
  726. elems->mesh_id_len = elen;
  727. break;
  728. case WLAN_EID_MESH_CONFIG:
  729. if (elen >= sizeof(struct ieee80211_meshconf_ie))
  730. elems->mesh_config = (void *)pos;
  731. else
  732. elem_parse_failed = true;
  733. break;
  734. case WLAN_EID_PEER_MGMT:
  735. elems->peering = pos;
  736. elems->peering_len = elen;
  737. break;
  738. case WLAN_EID_MESH_AWAKE_WINDOW:
  739. if (elen >= 2)
  740. elems->awake_window = (void *)pos;
  741. break;
  742. case WLAN_EID_PREQ:
  743. elems->preq = pos;
  744. elems->preq_len = elen;
  745. break;
  746. case WLAN_EID_PREP:
  747. elems->prep = pos;
  748. elems->prep_len = elen;
  749. break;
  750. case WLAN_EID_PERR:
  751. elems->perr = pos;
  752. elems->perr_len = elen;
  753. break;
  754. case WLAN_EID_RANN:
  755. if (elen >= sizeof(struct ieee80211_rann_ie))
  756. elems->rann = (void *)pos;
  757. else
  758. elem_parse_failed = true;
  759. break;
  760. case WLAN_EID_CHANNEL_SWITCH:
  761. if (elen != sizeof(struct ieee80211_channel_sw_ie)) {
  762. elem_parse_failed = true;
  763. break;
  764. }
  765. elems->ch_switch_ie = (void *)pos;
  766. break;
  767. case WLAN_EID_EXT_CHANSWITCH_ANN:
  768. if (elen != sizeof(struct ieee80211_ext_chansw_ie)) {
  769. elem_parse_failed = true;
  770. break;
  771. }
  772. elems->ext_chansw_ie = (void *)pos;
  773. break;
  774. case WLAN_EID_SECONDARY_CHANNEL_OFFSET:
  775. if (elen != sizeof(struct ieee80211_sec_chan_offs_ie)) {
  776. elem_parse_failed = true;
  777. break;
  778. }
  779. elems->sec_chan_offs = (void *)pos;
  780. break;
  781. case WLAN_EID_WIDE_BW_CHANNEL_SWITCH:
  782. if (!action ||
  783. elen != sizeof(*elems->wide_bw_chansw_ie)) {
  784. elem_parse_failed = true;
  785. break;
  786. }
  787. elems->wide_bw_chansw_ie = (void *)pos;
  788. break;
  789. case WLAN_EID_CHANNEL_SWITCH_WRAPPER:
  790. if (action) {
  791. elem_parse_failed = true;
  792. break;
  793. }
  794. /*
  795. * This is a bit tricky, but as we only care about
  796. * the wide bandwidth channel switch element, so
  797. * just parse it out manually.
  798. */
  799. ie = cfg80211_find_ie(WLAN_EID_WIDE_BW_CHANNEL_SWITCH,
  800. pos, elen);
  801. if (ie) {
  802. if (ie[1] == sizeof(*elems->wide_bw_chansw_ie))
  803. elems->wide_bw_chansw_ie =
  804. (void *)(ie + 2);
  805. else
  806. elem_parse_failed = true;
  807. }
  808. break;
  809. case WLAN_EID_COUNTRY:
  810. elems->country_elem = pos;
  811. elems->country_elem_len = elen;
  812. break;
  813. case WLAN_EID_PWR_CONSTRAINT:
  814. if (elen != 1) {
  815. elem_parse_failed = true;
  816. break;
  817. }
  818. elems->pwr_constr_elem = pos;
  819. break;
  820. case WLAN_EID_TIMEOUT_INTERVAL:
  821. if (elen >= sizeof(struct ieee80211_timeout_interval_ie))
  822. elems->timeout_int = (void *)pos;
  823. else
  824. elem_parse_failed = true;
  825. break;
  826. default:
  827. break;
  828. }
  829. if (elem_parse_failed)
  830. elems->parse_error = true;
  831. else
  832. __set_bit(id, seen_elems);
  833. left -= elen;
  834. pos += elen;
  835. }
  836. if (left != 0)
  837. elems->parse_error = true;
  838. return crc;
  839. }
  840. void ieee80211_set_wmm_default(struct ieee80211_sub_if_data *sdata,
  841. bool bss_notify)
  842. {
  843. struct ieee80211_local *local = sdata->local;
  844. struct ieee80211_tx_queue_params qparam;
  845. struct ieee80211_chanctx_conf *chanctx_conf;
  846. int ac;
  847. bool use_11b, enable_qos;
  848. int aCWmin, aCWmax;
  849. if (!local->ops->conf_tx)
  850. return;
  851. if (local->hw.queues < IEEE80211_NUM_ACS)
  852. return;
  853. memset(&qparam, 0, sizeof(qparam));
  854. rcu_read_lock();
  855. chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
  856. use_11b = (chanctx_conf &&
  857. chanctx_conf->def.chan->band == IEEE80211_BAND_2GHZ) &&
  858. !(sdata->flags & IEEE80211_SDATA_OPERATING_GMODE);
  859. rcu_read_unlock();
  860. /*
  861. * By default disable QoS in STA mode for old access points, which do
  862. * not support 802.11e. New APs will provide proper queue parameters,
  863. * that we will configure later.
  864. */
  865. enable_qos = (sdata->vif.type != NL80211_IFTYPE_STATION);
  866. for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
  867. /* Set defaults according to 802.11-2007 Table 7-37 */
  868. aCWmax = 1023;
  869. if (use_11b)
  870. aCWmin = 31;
  871. else
  872. aCWmin = 15;
  873. if (enable_qos) {
  874. switch (ac) {
  875. case IEEE80211_AC_BK:
  876. qparam.cw_max = aCWmax;
  877. qparam.cw_min = aCWmin;
  878. qparam.txop = 0;
  879. qparam.aifs = 7;
  880. break;
  881. /* never happens but let's not leave undefined */
  882. default:
  883. case IEEE80211_AC_BE:
  884. qparam.cw_max = aCWmax;
  885. qparam.cw_min = aCWmin;
  886. qparam.txop = 0;
  887. qparam.aifs = 3;
  888. break;
  889. case IEEE80211_AC_VI:
  890. qparam.cw_max = aCWmin;
  891. qparam.cw_min = (aCWmin + 1) / 2 - 1;
  892. if (use_11b)
  893. qparam.txop = 6016/32;
  894. else
  895. qparam.txop = 3008/32;
  896. qparam.aifs = 2;
  897. break;
  898. case IEEE80211_AC_VO:
  899. qparam.cw_max = (aCWmin + 1) / 2 - 1;
  900. qparam.cw_min = (aCWmin + 1) / 4 - 1;
  901. if (use_11b)
  902. qparam.txop = 3264/32;
  903. else
  904. qparam.txop = 1504/32;
  905. qparam.aifs = 2;
  906. break;
  907. }
  908. } else {
  909. /* Confiure old 802.11b/g medium access rules. */
  910. qparam.cw_max = aCWmax;
  911. qparam.cw_min = aCWmin;
  912. qparam.txop = 0;
  913. qparam.aifs = 2;
  914. }
  915. qparam.uapsd = false;
  916. sdata->tx_conf[ac] = qparam;
  917. drv_conf_tx(local, sdata, ac, &qparam);
  918. }
  919. if (sdata->vif.type != NL80211_IFTYPE_MONITOR &&
  920. sdata->vif.type != NL80211_IFTYPE_P2P_DEVICE) {
  921. sdata->vif.bss_conf.qos = enable_qos;
  922. if (bss_notify)
  923. ieee80211_bss_info_change_notify(sdata,
  924. BSS_CHANGED_QOS);
  925. }
  926. }
  927. void ieee80211_send_auth(struct ieee80211_sub_if_data *sdata,
  928. u16 transaction, u16 auth_alg, u16 status,
  929. const u8 *extra, size_t extra_len, const u8 *da,
  930. const u8 *bssid, const u8 *key, u8 key_len, u8 key_idx,
  931. u32 tx_flags)
  932. {
  933. struct ieee80211_local *local = sdata->local;
  934. struct sk_buff *skb;
  935. struct ieee80211_mgmt *mgmt;
  936. int err;
  937. skb = dev_alloc_skb(local->hw.extra_tx_headroom +
  938. sizeof(*mgmt) + 6 + extra_len);
  939. if (!skb)
  940. return;
  941. skb_reserve(skb, local->hw.extra_tx_headroom);
  942. mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24 + 6);
  943. memset(mgmt, 0, 24 + 6);
  944. mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
  945. IEEE80211_STYPE_AUTH);
  946. memcpy(mgmt->da, da, ETH_ALEN);
  947. memcpy(mgmt->sa, sdata->vif.addr, ETH_ALEN);
  948. memcpy(mgmt->bssid, bssid, ETH_ALEN);
  949. mgmt->u.auth.auth_alg = cpu_to_le16(auth_alg);
  950. mgmt->u.auth.auth_transaction = cpu_to_le16(transaction);
  951. mgmt->u.auth.status_code = cpu_to_le16(status);
  952. if (extra)
  953. memcpy(skb_put(skb, extra_len), extra, extra_len);
  954. if (auth_alg == WLAN_AUTH_SHARED_KEY && transaction == 3) {
  955. mgmt->frame_control |= cpu_to_le16(IEEE80211_FCTL_PROTECTED);
  956. err = ieee80211_wep_encrypt(local, skb, key, key_len, key_idx);
  957. WARN_ON(err);
  958. }
  959. IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT |
  960. tx_flags;
  961. ieee80211_tx_skb(sdata, skb);
  962. }
  963. void ieee80211_send_deauth_disassoc(struct ieee80211_sub_if_data *sdata,
  964. const u8 *bssid, u16 stype, u16 reason,
  965. bool send_frame, u8 *frame_buf)
  966. {
  967. struct ieee80211_local *local = sdata->local;
  968. struct sk_buff *skb;
  969. struct ieee80211_mgmt *mgmt = (void *)frame_buf;
  970. /* build frame */
  971. mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT | stype);
  972. mgmt->duration = 0; /* initialize only */
  973. mgmt->seq_ctrl = 0; /* initialize only */
  974. memcpy(mgmt->da, bssid, ETH_ALEN);
  975. memcpy(mgmt->sa, sdata->vif.addr, ETH_ALEN);
  976. memcpy(mgmt->bssid, bssid, ETH_ALEN);
  977. /* u.deauth.reason_code == u.disassoc.reason_code */
  978. mgmt->u.deauth.reason_code = cpu_to_le16(reason);
  979. if (send_frame) {
  980. skb = dev_alloc_skb(local->hw.extra_tx_headroom +
  981. IEEE80211_DEAUTH_FRAME_LEN);
  982. if (!skb)
  983. return;
  984. skb_reserve(skb, local->hw.extra_tx_headroom);
  985. /* copy in frame */
  986. memcpy(skb_put(skb, IEEE80211_DEAUTH_FRAME_LEN),
  987. mgmt, IEEE80211_DEAUTH_FRAME_LEN);
  988. if (sdata->vif.type != NL80211_IFTYPE_STATION ||
  989. !(sdata->u.mgd.flags & IEEE80211_STA_MFP_ENABLED))
  990. IEEE80211_SKB_CB(skb)->flags |=
  991. IEEE80211_TX_INTFL_DONT_ENCRYPT;
  992. ieee80211_tx_skb(sdata, skb);
  993. }
  994. }
  995. int ieee80211_build_preq_ies(struct ieee80211_local *local, u8 *buffer,
  996. size_t buffer_len, const u8 *ie, size_t ie_len,
  997. enum ieee80211_band band, u32 rate_mask,
  998. struct cfg80211_chan_def *chandef)
  999. {
  1000. struct ieee80211_supported_band *sband;
  1001. u8 *pos = buffer, *end = buffer + buffer_len;
  1002. size_t offset = 0, noffset;
  1003. int supp_rates_len, i;
  1004. u8 rates[32];
  1005. int num_rates;
  1006. int ext_rates_len;
  1007. int shift;
  1008. u32 rate_flags;
  1009. sband = local->hw.wiphy->bands[band];
  1010. if (WARN_ON_ONCE(!sband))
  1011. return 0;
  1012. rate_flags = ieee80211_chandef_rate_flags(chandef);
  1013. shift = ieee80211_chandef_get_shift(chandef);
  1014. num_rates = 0;
  1015. for (i = 0; i < sband->n_bitrates; i++) {
  1016. if ((BIT(i) & rate_mask) == 0)
  1017. continue; /* skip rate */
  1018. if ((rate_flags & sband->bitrates[i].flags) != rate_flags)
  1019. continue;
  1020. rates[num_rates++] =
  1021. (u8) DIV_ROUND_UP(sband->bitrates[i].bitrate,
  1022. (1 << shift) * 5);
  1023. }
  1024. supp_rates_len = min_t(int, num_rates, 8);
  1025. if (end - pos < 2 + supp_rates_len)
  1026. goto out_err;
  1027. *pos++ = WLAN_EID_SUPP_RATES;
  1028. *pos++ = supp_rates_len;
  1029. memcpy(pos, rates, supp_rates_len);
  1030. pos += supp_rates_len;
  1031. /* insert "request information" if in custom IEs */
  1032. if (ie && ie_len) {
  1033. static const u8 before_extrates[] = {
  1034. WLAN_EID_SSID,
  1035. WLAN_EID_SUPP_RATES,
  1036. WLAN_EID_REQUEST,
  1037. };
  1038. noffset = ieee80211_ie_split(ie, ie_len,
  1039. before_extrates,
  1040. ARRAY_SIZE(before_extrates),
  1041. offset);
  1042. if (end - pos < noffset - offset)
  1043. goto out_err;
  1044. memcpy(pos, ie + offset, noffset - offset);
  1045. pos += noffset - offset;
  1046. offset = noffset;
  1047. }
  1048. ext_rates_len = num_rates - supp_rates_len;
  1049. if (ext_rates_len > 0) {
  1050. if (end - pos < 2 + ext_rates_len)
  1051. goto out_err;
  1052. *pos++ = WLAN_EID_EXT_SUPP_RATES;
  1053. *pos++ = ext_rates_len;
  1054. memcpy(pos, rates + supp_rates_len, ext_rates_len);
  1055. pos += ext_rates_len;
  1056. }
  1057. if (chandef->chan && sband->band == IEEE80211_BAND_2GHZ) {
  1058. if (end - pos < 3)
  1059. goto out_err;
  1060. *pos++ = WLAN_EID_DS_PARAMS;
  1061. *pos++ = 1;
  1062. *pos++ = ieee80211_frequency_to_channel(
  1063. chandef->chan->center_freq);
  1064. }
  1065. /* insert custom IEs that go before HT */
  1066. if (ie && ie_len) {
  1067. static const u8 before_ht[] = {
  1068. WLAN_EID_SSID,
  1069. WLAN_EID_SUPP_RATES,
  1070. WLAN_EID_REQUEST,
  1071. WLAN_EID_EXT_SUPP_RATES,
  1072. WLAN_EID_DS_PARAMS,
  1073. WLAN_EID_SUPPORTED_REGULATORY_CLASSES,
  1074. };
  1075. noffset = ieee80211_ie_split(ie, ie_len,
  1076. before_ht, ARRAY_SIZE(before_ht),
  1077. offset);
  1078. if (end - pos < noffset - offset)
  1079. goto out_err;
  1080. memcpy(pos, ie + offset, noffset - offset);
  1081. pos += noffset - offset;
  1082. offset = noffset;
  1083. }
  1084. if (sband->ht_cap.ht_supported) {
  1085. if (end - pos < 2 + sizeof(struct ieee80211_ht_cap))
  1086. goto out_err;
  1087. pos = ieee80211_ie_build_ht_cap(pos, &sband->ht_cap,
  1088. sband->ht_cap.cap);
  1089. }
  1090. /*
  1091. * If adding more here, adjust code in main.c
  1092. * that calculates local->scan_ies_len.
  1093. */
  1094. /* add any remaining custom IEs */
  1095. if (ie && ie_len) {
  1096. noffset = ie_len;
  1097. if (end - pos < noffset - offset)
  1098. goto out_err;
  1099. memcpy(pos, ie + offset, noffset - offset);
  1100. pos += noffset - offset;
  1101. }
  1102. if (sband->vht_cap.vht_supported) {
  1103. if (end - pos < 2 + sizeof(struct ieee80211_vht_cap))
  1104. goto out_err;
  1105. pos = ieee80211_ie_build_vht_cap(pos, &sband->vht_cap,
  1106. sband->vht_cap.cap);
  1107. }
  1108. return pos - buffer;
  1109. out_err:
  1110. WARN_ONCE(1, "not enough space for preq IEs\n");
  1111. return pos - buffer;
  1112. }
  1113. struct sk_buff *ieee80211_build_probe_req(struct ieee80211_sub_if_data *sdata,
  1114. u8 *dst, u32 ratemask,
  1115. struct ieee80211_channel *chan,
  1116. const u8 *ssid, size_t ssid_len,
  1117. const u8 *ie, size_t ie_len,
  1118. bool directed)
  1119. {
  1120. struct ieee80211_local *local = sdata->local;
  1121. struct cfg80211_chan_def chandef;
  1122. struct sk_buff *skb;
  1123. struct ieee80211_mgmt *mgmt;
  1124. int ies_len;
  1125. /*
  1126. * Do not send DS Channel parameter for directed probe requests
  1127. * in order to maximize the chance that we get a response. Some
  1128. * badly-behaved APs don't respond when this parameter is included.
  1129. */
  1130. chandef.width = sdata->vif.bss_conf.chandef.width;
  1131. if (directed)
  1132. chandef.chan = NULL;
  1133. else
  1134. chandef.chan = chan;
  1135. skb = ieee80211_probereq_get(&local->hw, &sdata->vif,
  1136. ssid, ssid_len, 100 + ie_len);
  1137. if (!skb)
  1138. return NULL;
  1139. ies_len = ieee80211_build_preq_ies(local, skb_tail_pointer(skb),
  1140. skb_tailroom(skb),
  1141. ie, ie_len, chan->band,
  1142. ratemask, &chandef);
  1143. skb_put(skb, ies_len);
  1144. if (dst) {
  1145. mgmt = (struct ieee80211_mgmt *) skb->data;
  1146. memcpy(mgmt->da, dst, ETH_ALEN);
  1147. memcpy(mgmt->bssid, dst, ETH_ALEN);
  1148. }
  1149. IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
  1150. return skb;
  1151. }
  1152. void ieee80211_send_probe_req(struct ieee80211_sub_if_data *sdata, u8 *dst,
  1153. const u8 *ssid, size_t ssid_len,
  1154. const u8 *ie, size_t ie_len,
  1155. u32 ratemask, bool directed, u32 tx_flags,
  1156. struct ieee80211_channel *channel, bool scan)
  1157. {
  1158. struct sk_buff *skb;
  1159. skb = ieee80211_build_probe_req(sdata, dst, ratemask, channel,
  1160. ssid, ssid_len,
  1161. ie, ie_len, directed);
  1162. if (skb) {
  1163. IEEE80211_SKB_CB(skb)->flags |= tx_flags;
  1164. if (scan)
  1165. ieee80211_tx_skb_tid_band(sdata, skb, 7, channel->band);
  1166. else
  1167. ieee80211_tx_skb(sdata, skb);
  1168. }
  1169. }
  1170. u32 ieee80211_sta_get_rates(struct ieee80211_sub_if_data *sdata,
  1171. struct ieee802_11_elems *elems,
  1172. enum ieee80211_band band, u32 *basic_rates)
  1173. {
  1174. struct ieee80211_supported_band *sband;
  1175. struct ieee80211_rate *bitrates;
  1176. size_t num_rates;
  1177. u32 supp_rates, rate_flags;
  1178. int i, j, shift;
  1179. sband = sdata->local->hw.wiphy->bands[band];
  1180. rate_flags = ieee80211_chandef_rate_flags(&sdata->vif.bss_conf.chandef);
  1181. shift = ieee80211_vif_get_shift(&sdata->vif);
  1182. if (WARN_ON(!sband))
  1183. return 1;
  1184. bitrates = sband->bitrates;
  1185. num_rates = sband->n_bitrates;
  1186. supp_rates = 0;
  1187. for (i = 0; i < elems->supp_rates_len +
  1188. elems->ext_supp_rates_len; i++) {
  1189. u8 rate = 0;
  1190. int own_rate;
  1191. bool is_basic;
  1192. if (i < elems->supp_rates_len)
  1193. rate = elems->supp_rates[i];
  1194. else if (elems->ext_supp_rates)
  1195. rate = elems->ext_supp_rates
  1196. [i - elems->supp_rates_len];
  1197. own_rate = 5 * (rate & 0x7f);
  1198. is_basic = !!(rate & 0x80);
  1199. if (is_basic && (rate & 0x7f) == BSS_MEMBERSHIP_SELECTOR_HT_PHY)
  1200. continue;
  1201. for (j = 0; j < num_rates; j++) {
  1202. int brate;
  1203. if ((rate_flags & sband->bitrates[j].flags)
  1204. != rate_flags)
  1205. continue;
  1206. brate = DIV_ROUND_UP(sband->bitrates[j].bitrate,
  1207. 1 << shift);
  1208. if (brate == own_rate) {
  1209. supp_rates |= BIT(j);
  1210. if (basic_rates && is_basic)
  1211. *basic_rates |= BIT(j);
  1212. }
  1213. }
  1214. }
  1215. return supp_rates;
  1216. }
  1217. void ieee80211_stop_device(struct ieee80211_local *local)
  1218. {
  1219. ieee80211_led_radio(local, false);
  1220. ieee80211_mod_tpt_led_trig(local, 0, IEEE80211_TPT_LEDTRIG_FL_RADIO);
  1221. cancel_work_sync(&local->reconfig_filter);
  1222. flush_workqueue(local->workqueue);
  1223. drv_stop(local);
  1224. }
  1225. static void ieee80211_assign_chanctx(struct ieee80211_local *local,
  1226. struct ieee80211_sub_if_data *sdata)
  1227. {
  1228. struct ieee80211_chanctx_conf *conf;
  1229. struct ieee80211_chanctx *ctx;
  1230. if (!local->use_chanctx)
  1231. return;
  1232. mutex_lock(&local->chanctx_mtx);
  1233. conf = rcu_dereference_protected(sdata->vif.chanctx_conf,
  1234. lockdep_is_held(&local->chanctx_mtx));
  1235. if (conf) {
  1236. ctx = container_of(conf, struct ieee80211_chanctx, conf);
  1237. drv_assign_vif_chanctx(local, sdata, ctx);
  1238. }
  1239. mutex_unlock(&local->chanctx_mtx);
  1240. }
  1241. int ieee80211_reconfig(struct ieee80211_local *local)
  1242. {
  1243. struct ieee80211_hw *hw = &local->hw;
  1244. struct ieee80211_sub_if_data *sdata;
  1245. struct ieee80211_chanctx *ctx;
  1246. struct sta_info *sta;
  1247. int res, i;
  1248. bool reconfig_due_to_wowlan = false;
  1249. #ifdef CONFIG_PM
  1250. if (local->suspended)
  1251. local->resuming = true;
  1252. if (local->wowlan) {
  1253. local->wowlan = false;
  1254. res = drv_resume(local);
  1255. if (res < 0) {
  1256. local->resuming = false;
  1257. return res;
  1258. }
  1259. if (res == 0)
  1260. goto wake_up;
  1261. WARN_ON(res > 1);
  1262. /*
  1263. * res is 1, which means the driver requested
  1264. * to go through a regular reset on wakeup.
  1265. */
  1266. reconfig_due_to_wowlan = true;
  1267. }
  1268. #endif
  1269. /* everything else happens only if HW was up & running */
  1270. if (!local->open_count)
  1271. goto wake_up;
  1272. /*
  1273. * Upon resume hardware can sometimes be goofy due to
  1274. * various platform / driver / bus issues, so restarting
  1275. * the device may at times not work immediately. Propagate
  1276. * the error.
  1277. */
  1278. res = drv_start(local);
  1279. if (res) {
  1280. WARN(local->suspended, "Hardware became unavailable "
  1281. "upon resume. This could be a software issue "
  1282. "prior to suspend or a hardware issue.\n");
  1283. return res;
  1284. }
  1285. /* setup fragmentation threshold */
  1286. drv_set_frag_threshold(local, hw->wiphy->frag_threshold);
  1287. /* setup RTS threshold */
  1288. drv_set_rts_threshold(local, hw->wiphy->rts_threshold);
  1289. /* reset coverage class */
  1290. drv_set_coverage_class(local, hw->wiphy->coverage_class);
  1291. ieee80211_led_radio(local, true);
  1292. ieee80211_mod_tpt_led_trig(local,
  1293. IEEE80211_TPT_LEDTRIG_FL_RADIO, 0);
  1294. /* add interfaces */
  1295. sdata = rtnl_dereference(local->monitor_sdata);
  1296. if (sdata) {
  1297. /* in HW restart it exists already */
  1298. WARN_ON(local->resuming);
  1299. res = drv_add_interface(local, sdata);
  1300. if (WARN_ON(res)) {
  1301. rcu_assign_pointer(local->monitor_sdata, NULL);
  1302. synchronize_net();
  1303. kfree(sdata);
  1304. }
  1305. }
  1306. list_for_each_entry(sdata, &local->interfaces, list) {
  1307. if (sdata->vif.type != NL80211_IFTYPE_AP_VLAN &&
  1308. sdata->vif.type != NL80211_IFTYPE_MONITOR &&
  1309. ieee80211_sdata_running(sdata))
  1310. res = drv_add_interface(local, sdata);
  1311. }
  1312. /* add channel contexts */
  1313. if (local->use_chanctx) {
  1314. mutex_lock(&local->chanctx_mtx);
  1315. list_for_each_entry(ctx, &local->chanctx_list, list)
  1316. WARN_ON(drv_add_chanctx(local, ctx));
  1317. mutex_unlock(&local->chanctx_mtx);
  1318. }
  1319. list_for_each_entry(sdata, &local->interfaces, list) {
  1320. if (!ieee80211_sdata_running(sdata))
  1321. continue;
  1322. ieee80211_assign_chanctx(local, sdata);
  1323. }
  1324. sdata = rtnl_dereference(local->monitor_sdata);
  1325. if (sdata && ieee80211_sdata_running(sdata))
  1326. ieee80211_assign_chanctx(local, sdata);
  1327. /* add STAs back */
  1328. mutex_lock(&local->sta_mtx);
  1329. list_for_each_entry(sta, &local->sta_list, list) {
  1330. enum ieee80211_sta_state state;
  1331. if (!sta->uploaded)
  1332. continue;
  1333. /* AP-mode stations will be added later */
  1334. if (sta->sdata->vif.type == NL80211_IFTYPE_AP)
  1335. continue;
  1336. for (state = IEEE80211_STA_NOTEXIST;
  1337. state < sta->sta_state; state++)
  1338. WARN_ON(drv_sta_state(local, sta->sdata, sta, state,
  1339. state + 1));
  1340. }
  1341. mutex_unlock(&local->sta_mtx);
  1342. /* reconfigure tx conf */
  1343. if (hw->queues >= IEEE80211_NUM_ACS) {
  1344. list_for_each_entry(sdata, &local->interfaces, list) {
  1345. if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN ||
  1346. sdata->vif.type == NL80211_IFTYPE_MONITOR ||
  1347. !ieee80211_sdata_running(sdata))
  1348. continue;
  1349. for (i = 0; i < IEEE80211_NUM_ACS; i++)
  1350. drv_conf_tx(local, sdata, i,
  1351. &sdata->tx_conf[i]);
  1352. }
  1353. }
  1354. /* reconfigure hardware */
  1355. ieee80211_hw_config(local, ~0);
  1356. ieee80211_configure_filter(local);
  1357. /* Finally also reconfigure all the BSS information */
  1358. list_for_each_entry(sdata, &local->interfaces, list) {
  1359. u32 changed;
  1360. if (!ieee80211_sdata_running(sdata))
  1361. continue;
  1362. /* common change flags for all interface types */
  1363. changed = BSS_CHANGED_ERP_CTS_PROT |
  1364. BSS_CHANGED_ERP_PREAMBLE |
  1365. BSS_CHANGED_ERP_SLOT |
  1366. BSS_CHANGED_HT |
  1367. BSS_CHANGED_BASIC_RATES |
  1368. BSS_CHANGED_BEACON_INT |
  1369. BSS_CHANGED_BSSID |
  1370. BSS_CHANGED_CQM |
  1371. BSS_CHANGED_QOS |
  1372. BSS_CHANGED_IDLE |
  1373. BSS_CHANGED_TXPOWER;
  1374. switch (sdata->vif.type) {
  1375. case NL80211_IFTYPE_STATION:
  1376. changed |= BSS_CHANGED_ASSOC |
  1377. BSS_CHANGED_ARP_FILTER |
  1378. BSS_CHANGED_PS;
  1379. /* Re-send beacon info report to the driver */
  1380. if (sdata->u.mgd.have_beacon)
  1381. changed |= BSS_CHANGED_BEACON_INFO;
  1382. sdata_lock(sdata);
  1383. ieee80211_bss_info_change_notify(sdata, changed);
  1384. sdata_unlock(sdata);
  1385. break;
  1386. case NL80211_IFTYPE_ADHOC:
  1387. changed |= BSS_CHANGED_IBSS;
  1388. /* fall through */
  1389. case NL80211_IFTYPE_AP:
  1390. changed |= BSS_CHANGED_SSID | BSS_CHANGED_P2P_PS;
  1391. if (sdata->vif.type == NL80211_IFTYPE_AP) {
  1392. changed |= BSS_CHANGED_AP_PROBE_RESP;
  1393. if (rcu_access_pointer(sdata->u.ap.beacon))
  1394. drv_start_ap(local, sdata);
  1395. }
  1396. /* fall through */
  1397. case NL80211_IFTYPE_MESH_POINT:
  1398. if (sdata->vif.bss_conf.enable_beacon) {
  1399. changed |= BSS_CHANGED_BEACON |
  1400. BSS_CHANGED_BEACON_ENABLED;
  1401. ieee80211_bss_info_change_notify(sdata, changed);
  1402. }
  1403. break;
  1404. case NL80211_IFTYPE_WDS:
  1405. break;
  1406. case NL80211_IFTYPE_AP_VLAN:
  1407. case NL80211_IFTYPE_MONITOR:
  1408. /* ignore virtual */
  1409. break;
  1410. case NL80211_IFTYPE_P2P_DEVICE:
  1411. changed = BSS_CHANGED_IDLE;
  1412. break;
  1413. case NL80211_IFTYPE_UNSPECIFIED:
  1414. case NUM_NL80211_IFTYPES:
  1415. case NL80211_IFTYPE_P2P_CLIENT:
  1416. case NL80211_IFTYPE_P2P_GO:
  1417. WARN_ON(1);
  1418. break;
  1419. }
  1420. }
  1421. ieee80211_recalc_ps(local, -1);
  1422. /*
  1423. * The sta might be in psm against the ap (e.g. because
  1424. * this was the state before a hw restart), so we
  1425. * explicitly send a null packet in order to make sure
  1426. * it'll sync against the ap (and get out of psm).
  1427. */
  1428. if (!(local->hw.conf.flags & IEEE80211_CONF_PS)) {
  1429. list_for_each_entry(sdata, &local->interfaces, list) {
  1430. if (sdata->vif.type != NL80211_IFTYPE_STATION)
  1431. continue;
  1432. if (!sdata->u.mgd.associated)
  1433. continue;
  1434. ieee80211_send_nullfunc(local, sdata, 0);
  1435. }
  1436. }
  1437. /* APs are now beaconing, add back stations */
  1438. mutex_lock(&local->sta_mtx);
  1439. list_for_each_entry(sta, &local->sta_list, list) {
  1440. enum ieee80211_sta_state state;
  1441. if (!sta->uploaded)
  1442. continue;
  1443. if (sta->sdata->vif.type != NL80211_IFTYPE_AP)
  1444. continue;
  1445. for (state = IEEE80211_STA_NOTEXIST;
  1446. state < sta->sta_state; state++)
  1447. WARN_ON(drv_sta_state(local, sta->sdata, sta, state,
  1448. state + 1));
  1449. }
  1450. mutex_unlock(&local->sta_mtx);
  1451. /* add back keys */
  1452. list_for_each_entry(sdata, &local->interfaces, list)
  1453. if (ieee80211_sdata_running(sdata))
  1454. ieee80211_enable_keys(sdata);
  1455. wake_up:
  1456. local->in_reconfig = false;
  1457. barrier();
  1458. if (local->monitors == local->open_count && local->monitors > 0)
  1459. ieee80211_add_virtual_monitor(local);
  1460. /*
  1461. * Clear the WLAN_STA_BLOCK_BA flag so new aggregation
  1462. * sessions can be established after a resume.
  1463. *
  1464. * Also tear down aggregation sessions since reconfiguring
  1465. * them in a hardware restart scenario is not easily done
  1466. * right now, and the hardware will have lost information
  1467. * about the sessions, but we and the AP still think they
  1468. * are active. This is really a workaround though.
  1469. */
  1470. if (hw->flags & IEEE80211_HW_AMPDU_AGGREGATION) {
  1471. mutex_lock(&local->sta_mtx);
  1472. list_for_each_entry(sta, &local->sta_list, list) {
  1473. ieee80211_sta_tear_down_BA_sessions(
  1474. sta, AGG_STOP_LOCAL_REQUEST);
  1475. clear_sta_flag(sta, WLAN_STA_BLOCK_BA);
  1476. }
  1477. mutex_unlock(&local->sta_mtx);
  1478. }
  1479. ieee80211_wake_queues_by_reason(hw, IEEE80211_MAX_QUEUE_MAP,
  1480. IEEE80211_QUEUE_STOP_REASON_SUSPEND);
  1481. /*
  1482. * If this is for hw restart things are still running.
  1483. * We may want to change that later, however.
  1484. */
  1485. if (!local->suspended || reconfig_due_to_wowlan)
  1486. drv_restart_complete(local);
  1487. if (!local->suspended)
  1488. return 0;
  1489. #ifdef CONFIG_PM
  1490. /* first set suspended false, then resuming */
  1491. local->suspended = false;
  1492. mb();
  1493. local->resuming = false;
  1494. list_for_each_entry(sdata, &local->interfaces, list) {
  1495. if (!ieee80211_sdata_running(sdata))
  1496. continue;
  1497. if (sdata->vif.type == NL80211_IFTYPE_STATION)
  1498. ieee80211_sta_restart(sdata);
  1499. }
  1500. mod_timer(&local->sta_cleanup, jiffies + 1);
  1501. #else
  1502. WARN_ON(1);
  1503. #endif
  1504. return 0;
  1505. }
  1506. void ieee80211_resume_disconnect(struct ieee80211_vif *vif)
  1507. {
  1508. struct ieee80211_sub_if_data *sdata;
  1509. struct ieee80211_local *local;
  1510. struct ieee80211_key *key;
  1511. if (WARN_ON(!vif))
  1512. return;
  1513. sdata = vif_to_sdata(vif);
  1514. local = sdata->local;
  1515. if (WARN_ON(!local->resuming))
  1516. return;
  1517. if (WARN_ON(vif->type != NL80211_IFTYPE_STATION))
  1518. return;
  1519. sdata->flags |= IEEE80211_SDATA_DISCONNECT_RESUME;
  1520. mutex_lock(&local->key_mtx);
  1521. list_for_each_entry(key, &sdata->key_list, list)
  1522. key->flags |= KEY_FLAG_TAINTED;
  1523. mutex_unlock(&local->key_mtx);
  1524. }
  1525. EXPORT_SYMBOL_GPL(ieee80211_resume_disconnect);
  1526. void ieee80211_recalc_smps(struct ieee80211_sub_if_data *sdata)
  1527. {
  1528. struct ieee80211_local *local = sdata->local;
  1529. struct ieee80211_chanctx_conf *chanctx_conf;
  1530. struct ieee80211_chanctx *chanctx;
  1531. mutex_lock(&local->chanctx_mtx);
  1532. chanctx_conf = rcu_dereference_protected(sdata->vif.chanctx_conf,
  1533. lockdep_is_held(&local->chanctx_mtx));
  1534. if (WARN_ON_ONCE(!chanctx_conf))
  1535. goto unlock;
  1536. chanctx = container_of(chanctx_conf, struct ieee80211_chanctx, conf);
  1537. ieee80211_recalc_smps_chanctx(local, chanctx);
  1538. unlock:
  1539. mutex_unlock(&local->chanctx_mtx);
  1540. }
  1541. static bool ieee80211_id_in_list(const u8 *ids, int n_ids, u8 id)
  1542. {
  1543. int i;
  1544. for (i = 0; i < n_ids; i++)
  1545. if (ids[i] == id)
  1546. return true;
  1547. return false;
  1548. }
  1549. /**
  1550. * ieee80211_ie_split - split an IE buffer according to ordering
  1551. *
  1552. * @ies: the IE buffer
  1553. * @ielen: the length of the IE buffer
  1554. * @ids: an array with element IDs that are allowed before
  1555. * the split
  1556. * @n_ids: the size of the element ID array
  1557. * @offset: offset where to start splitting in the buffer
  1558. *
  1559. * This function splits an IE buffer by updating the @offset
  1560. * variable to point to the location where the buffer should be
  1561. * split.
  1562. *
  1563. * It assumes that the given IE buffer is well-formed, this
  1564. * has to be guaranteed by the caller!
  1565. *
  1566. * It also assumes that the IEs in the buffer are ordered
  1567. * correctly, if not the result of using this function will not
  1568. * be ordered correctly either, i.e. it does no reordering.
  1569. *
  1570. * The function returns the offset where the next part of the
  1571. * buffer starts, which may be @ielen if the entire (remainder)
  1572. * of the buffer should be used.
  1573. */
  1574. size_t ieee80211_ie_split(const u8 *ies, size_t ielen,
  1575. const u8 *ids, int n_ids, size_t offset)
  1576. {
  1577. size_t pos = offset;
  1578. while (pos < ielen && ieee80211_id_in_list(ids, n_ids, ies[pos]))
  1579. pos += 2 + ies[pos + 1];
  1580. return pos;
  1581. }
  1582. size_t ieee80211_ie_split_vendor(const u8 *ies, size_t ielen, size_t offset)
  1583. {
  1584. size_t pos = offset;
  1585. while (pos < ielen && ies[pos] != WLAN_EID_VENDOR_SPECIFIC)
  1586. pos += 2 + ies[pos + 1];
  1587. return pos;
  1588. }
  1589. static void _ieee80211_enable_rssi_reports(struct ieee80211_sub_if_data *sdata,
  1590. int rssi_min_thold,
  1591. int rssi_max_thold)
  1592. {
  1593. trace_api_enable_rssi_reports(sdata, rssi_min_thold, rssi_max_thold);
  1594. if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_STATION))
  1595. return;
  1596. /*
  1597. * Scale up threshold values before storing it, as the RSSI averaging
  1598. * algorithm uses a scaled up value as well. Change this scaling
  1599. * factor if the RSSI averaging algorithm changes.
  1600. */
  1601. sdata->u.mgd.rssi_min_thold = rssi_min_thold*16;
  1602. sdata->u.mgd.rssi_max_thold = rssi_max_thold*16;
  1603. }
  1604. void ieee80211_enable_rssi_reports(struct ieee80211_vif *vif,
  1605. int rssi_min_thold,
  1606. int rssi_max_thold)
  1607. {
  1608. struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
  1609. WARN_ON(rssi_min_thold == rssi_max_thold ||
  1610. rssi_min_thold > rssi_max_thold);
  1611. _ieee80211_enable_rssi_reports(sdata, rssi_min_thold,
  1612. rssi_max_thold);
  1613. }
  1614. EXPORT_SYMBOL(ieee80211_enable_rssi_reports);
  1615. void ieee80211_disable_rssi_reports(struct ieee80211_vif *vif)
  1616. {
  1617. struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
  1618. _ieee80211_enable_rssi_reports(sdata, 0, 0);
  1619. }
  1620. EXPORT_SYMBOL(ieee80211_disable_rssi_reports);
  1621. u8 *ieee80211_ie_build_ht_cap(u8 *pos, struct ieee80211_sta_ht_cap *ht_cap,
  1622. u16 cap)
  1623. {
  1624. __le16 tmp;
  1625. *pos++ = WLAN_EID_HT_CAPABILITY;
  1626. *pos++ = sizeof(struct ieee80211_ht_cap);
  1627. memset(pos, 0, sizeof(struct ieee80211_ht_cap));
  1628. /* capability flags */
  1629. tmp = cpu_to_le16(cap);
  1630. memcpy(pos, &tmp, sizeof(u16));
  1631. pos += sizeof(u16);
  1632. /* AMPDU parameters */
  1633. *pos++ = ht_cap->ampdu_factor |
  1634. (ht_cap->ampdu_density <<
  1635. IEEE80211_HT_AMPDU_PARM_DENSITY_SHIFT);
  1636. /* MCS set */
  1637. memcpy(pos, &ht_cap->mcs, sizeof(ht_cap->mcs));
  1638. pos += sizeof(ht_cap->mcs);
  1639. /* extended capabilities */
  1640. pos += sizeof(__le16);
  1641. /* BF capabilities */
  1642. pos += sizeof(__le32);
  1643. /* antenna selection */
  1644. pos += sizeof(u8);
  1645. return pos;
  1646. }
  1647. u8 *ieee80211_ie_build_vht_cap(u8 *pos, struct ieee80211_sta_vht_cap *vht_cap,
  1648. u32 cap)
  1649. {
  1650. __le32 tmp;
  1651. *pos++ = WLAN_EID_VHT_CAPABILITY;
  1652. *pos++ = sizeof(struct ieee80211_vht_cap);
  1653. memset(pos, 0, sizeof(struct ieee80211_vht_cap));
  1654. /* capability flags */
  1655. tmp = cpu_to_le32(cap);
  1656. memcpy(pos, &tmp, sizeof(u32));
  1657. pos += sizeof(u32);
  1658. /* VHT MCS set */
  1659. memcpy(pos, &vht_cap->vht_mcs, sizeof(vht_cap->vht_mcs));
  1660. pos += sizeof(vht_cap->vht_mcs);
  1661. return pos;
  1662. }
  1663. u8 *ieee80211_ie_build_ht_oper(u8 *pos, struct ieee80211_sta_ht_cap *ht_cap,
  1664. const struct cfg80211_chan_def *chandef,
  1665. u16 prot_mode)
  1666. {
  1667. struct ieee80211_ht_operation *ht_oper;
  1668. /* Build HT Information */
  1669. *pos++ = WLAN_EID_HT_OPERATION;
  1670. *pos++ = sizeof(struct ieee80211_ht_operation);
  1671. ht_oper = (struct ieee80211_ht_operation *)pos;
  1672. ht_oper->primary_chan = ieee80211_frequency_to_channel(
  1673. chandef->chan->center_freq);
  1674. switch (chandef->width) {
  1675. case NL80211_CHAN_WIDTH_160:
  1676. case NL80211_CHAN_WIDTH_80P80:
  1677. case NL80211_CHAN_WIDTH_80:
  1678. case NL80211_CHAN_WIDTH_40:
  1679. if (chandef->center_freq1 > chandef->chan->center_freq)
  1680. ht_oper->ht_param = IEEE80211_HT_PARAM_CHA_SEC_ABOVE;
  1681. else
  1682. ht_oper->ht_param = IEEE80211_HT_PARAM_CHA_SEC_BELOW;
  1683. break;
  1684. default:
  1685. ht_oper->ht_param = IEEE80211_HT_PARAM_CHA_SEC_NONE;
  1686. break;
  1687. }
  1688. if (ht_cap->cap & IEEE80211_HT_CAP_SUP_WIDTH_20_40 &&
  1689. chandef->width != NL80211_CHAN_WIDTH_20_NOHT &&
  1690. chandef->width != NL80211_CHAN_WIDTH_20)
  1691. ht_oper->ht_param |= IEEE80211_HT_PARAM_CHAN_WIDTH_ANY;
  1692. ht_oper->operation_mode = cpu_to_le16(prot_mode);
  1693. ht_oper->stbc_param = 0x0000;
  1694. /* It seems that Basic MCS set and Supported MCS set
  1695. are identical for the first 10 bytes */
  1696. memset(&ht_oper->basic_set, 0, 16);
  1697. memcpy(&ht_oper->basic_set, &ht_cap->mcs, 10);
  1698. return pos + sizeof(struct ieee80211_ht_operation);
  1699. }
  1700. void ieee80211_ht_oper_to_chandef(struct ieee80211_channel *control_chan,
  1701. const struct ieee80211_ht_operation *ht_oper,
  1702. struct cfg80211_chan_def *chandef)
  1703. {
  1704. enum nl80211_channel_type channel_type;
  1705. if (!ht_oper) {
  1706. cfg80211_chandef_create(chandef, control_chan,
  1707. NL80211_CHAN_NO_HT);
  1708. return;
  1709. }
  1710. switch (ht_oper->ht_param & IEEE80211_HT_PARAM_CHA_SEC_OFFSET) {
  1711. case IEEE80211_HT_PARAM_CHA_SEC_NONE:
  1712. channel_type = NL80211_CHAN_HT20;
  1713. break;
  1714. case IEEE80211_HT_PARAM_CHA_SEC_ABOVE:
  1715. channel_type = NL80211_CHAN_HT40PLUS;
  1716. break;
  1717. case IEEE80211_HT_PARAM_CHA_SEC_BELOW:
  1718. channel_type = NL80211_CHAN_HT40MINUS;
  1719. break;
  1720. default:
  1721. channel_type = NL80211_CHAN_NO_HT;
  1722. }
  1723. cfg80211_chandef_create(chandef, control_chan, channel_type);
  1724. }
  1725. int ieee80211_parse_bitrates(struct cfg80211_chan_def *chandef,
  1726. const struct ieee80211_supported_band *sband,
  1727. const u8 *srates, int srates_len, u32 *rates)
  1728. {
  1729. u32 rate_flags = ieee80211_chandef_rate_flags(chandef);
  1730. int shift = ieee80211_chandef_get_shift(chandef);
  1731. struct ieee80211_rate *br;
  1732. int brate, rate, i, j, count = 0;
  1733. *rates = 0;
  1734. for (i = 0; i < srates_len; i++) {
  1735. rate = srates[i] & 0x7f;
  1736. for (j = 0; j < sband->n_bitrates; j++) {
  1737. br = &sband->bitrates[j];
  1738. if ((rate_flags & br->flags) != rate_flags)
  1739. continue;
  1740. brate = DIV_ROUND_UP(br->bitrate, (1 << shift) * 5);
  1741. if (brate == rate) {
  1742. *rates |= BIT(j);
  1743. count++;
  1744. break;
  1745. }
  1746. }
  1747. }
  1748. return count;
  1749. }
  1750. int ieee80211_add_srates_ie(struct ieee80211_sub_if_data *sdata,
  1751. struct sk_buff *skb, bool need_basic,
  1752. enum ieee80211_band band)
  1753. {
  1754. struct ieee80211_local *local = sdata->local;
  1755. struct ieee80211_supported_band *sband;
  1756. int rate, shift;
  1757. u8 i, rates, *pos;
  1758. u32 basic_rates = sdata->vif.bss_conf.basic_rates;
  1759. u32 rate_flags;
  1760. shift = ieee80211_vif_get_shift(&sdata->vif);
  1761. rate_flags = ieee80211_chandef_rate_flags(&sdata->vif.bss_conf.chandef);
  1762. sband = local->hw.wiphy->bands[band];
  1763. rates = 0;
  1764. for (i = 0; i < sband->n_bitrates; i++) {
  1765. if ((rate_flags & sband->bitrates[i].flags) != rate_flags)
  1766. continue;
  1767. rates++;
  1768. }
  1769. if (rates > 8)
  1770. rates = 8;
  1771. if (skb_tailroom(skb) < rates + 2)
  1772. return -ENOMEM;
  1773. pos = skb_put(skb, rates + 2);
  1774. *pos++ = WLAN_EID_SUPP_RATES;
  1775. *pos++ = rates;
  1776. for (i = 0; i < rates; i++) {
  1777. u8 basic = 0;
  1778. if ((rate_flags & sband->bitrates[i].flags) != rate_flags)
  1779. continue;
  1780. if (need_basic && basic_rates & BIT(i))
  1781. basic = 0x80;
  1782. rate = sband->bitrates[i].bitrate;
  1783. rate = DIV_ROUND_UP(sband->bitrates[i].bitrate,
  1784. 5 * (1 << shift));
  1785. *pos++ = basic | (u8) rate;
  1786. }
  1787. return 0;
  1788. }
  1789. int ieee80211_add_ext_srates_ie(struct ieee80211_sub_if_data *sdata,
  1790. struct sk_buff *skb, bool need_basic,
  1791. enum ieee80211_band band)
  1792. {
  1793. struct ieee80211_local *local = sdata->local;
  1794. struct ieee80211_supported_band *sband;
  1795. int rate, skip, shift;
  1796. u8 i, exrates, *pos;
  1797. u32 basic_rates = sdata->vif.bss_conf.basic_rates;
  1798. u32 rate_flags;
  1799. rate_flags = ieee80211_chandef_rate_flags(&sdata->vif.bss_conf.chandef);
  1800. shift = ieee80211_vif_get_shift(&sdata->vif);
  1801. sband = local->hw.wiphy->bands[band];
  1802. exrates = 0;
  1803. for (i = 0; i < sband->n_bitrates; i++) {
  1804. if ((rate_flags & sband->bitrates[i].flags) != rate_flags)
  1805. continue;
  1806. exrates++;
  1807. }
  1808. if (exrates > 8)
  1809. exrates -= 8;
  1810. else
  1811. exrates = 0;
  1812. if (skb_tailroom(skb) < exrates + 2)
  1813. return -ENOMEM;
  1814. if (exrates) {
  1815. pos = skb_put(skb, exrates + 2);
  1816. *pos++ = WLAN_EID_EXT_SUPP_RATES;
  1817. *pos++ = exrates;
  1818. skip = 0;
  1819. for (i = 8; i < sband->n_bitrates; i++) {
  1820. u8 basic = 0;
  1821. if ((rate_flags & sband->bitrates[i].flags)
  1822. != rate_flags)
  1823. continue;
  1824. if (skip++ < 8)
  1825. continue;
  1826. if (need_basic && basic_rates & BIT(i))
  1827. basic = 0x80;
  1828. rate = DIV_ROUND_UP(sband->bitrates[i].bitrate,
  1829. 5 * (1 << shift));
  1830. *pos++ = basic | (u8) rate;
  1831. }
  1832. }
  1833. return 0;
  1834. }
  1835. int ieee80211_ave_rssi(struct ieee80211_vif *vif)
  1836. {
  1837. struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
  1838. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  1839. if (WARN_ON_ONCE(sdata->vif.type != NL80211_IFTYPE_STATION)) {
  1840. /* non-managed type inferfaces */
  1841. return 0;
  1842. }
  1843. return ifmgd->ave_beacon_signal / 16;
  1844. }
  1845. EXPORT_SYMBOL_GPL(ieee80211_ave_rssi);
  1846. u8 ieee80211_mcs_to_chains(const struct ieee80211_mcs_info *mcs)
  1847. {
  1848. if (!mcs)
  1849. return 1;
  1850. /* TODO: consider rx_highest */
  1851. if (mcs->rx_mask[3])
  1852. return 4;
  1853. if (mcs->rx_mask[2])
  1854. return 3;
  1855. if (mcs->rx_mask[1])
  1856. return 2;
  1857. return 1;
  1858. }
  1859. /**
  1860. * ieee80211_calculate_rx_timestamp - calculate timestamp in frame
  1861. * @local: mac80211 hw info struct
  1862. * @status: RX status
  1863. * @mpdu_len: total MPDU length (including FCS)
  1864. * @mpdu_offset: offset into MPDU to calculate timestamp at
  1865. *
  1866. * This function calculates the RX timestamp at the given MPDU offset, taking
  1867. * into account what the RX timestamp was. An offset of 0 will just normalize
  1868. * the timestamp to TSF at beginning of MPDU reception.
  1869. */
  1870. u64 ieee80211_calculate_rx_timestamp(struct ieee80211_local *local,
  1871. struct ieee80211_rx_status *status,
  1872. unsigned int mpdu_len,
  1873. unsigned int mpdu_offset)
  1874. {
  1875. u64 ts = status->mactime;
  1876. struct rate_info ri;
  1877. u16 rate;
  1878. if (WARN_ON(!ieee80211_have_rx_timestamp(status)))
  1879. return 0;
  1880. memset(&ri, 0, sizeof(ri));
  1881. /* Fill cfg80211 rate info */
  1882. if (status->flag & RX_FLAG_HT) {
  1883. ri.mcs = status->rate_idx;
  1884. ri.flags |= RATE_INFO_FLAGS_MCS;
  1885. if (status->flag & RX_FLAG_40MHZ)
  1886. ri.flags |= RATE_INFO_FLAGS_40_MHZ_WIDTH;
  1887. if (status->flag & RX_FLAG_SHORT_GI)
  1888. ri.flags |= RATE_INFO_FLAGS_SHORT_GI;
  1889. } else if (status->flag & RX_FLAG_VHT) {
  1890. ri.flags |= RATE_INFO_FLAGS_VHT_MCS;
  1891. ri.mcs = status->rate_idx;
  1892. ri.nss = status->vht_nss;
  1893. if (status->flag & RX_FLAG_40MHZ)
  1894. ri.flags |= RATE_INFO_FLAGS_40_MHZ_WIDTH;
  1895. if (status->flag & RX_FLAG_80MHZ)
  1896. ri.flags |= RATE_INFO_FLAGS_80_MHZ_WIDTH;
  1897. if (status->flag & RX_FLAG_80P80MHZ)
  1898. ri.flags |= RATE_INFO_FLAGS_80P80_MHZ_WIDTH;
  1899. if (status->flag & RX_FLAG_160MHZ)
  1900. ri.flags |= RATE_INFO_FLAGS_160_MHZ_WIDTH;
  1901. if (status->flag & RX_FLAG_SHORT_GI)
  1902. ri.flags |= RATE_INFO_FLAGS_SHORT_GI;
  1903. } else {
  1904. struct ieee80211_supported_band *sband;
  1905. int shift = 0;
  1906. int bitrate;
  1907. if (status->flag & RX_FLAG_10MHZ)
  1908. shift = 1;
  1909. if (status->flag & RX_FLAG_5MHZ)
  1910. shift = 2;
  1911. sband = local->hw.wiphy->bands[status->band];
  1912. bitrate = sband->bitrates[status->rate_idx].bitrate;
  1913. ri.legacy = DIV_ROUND_UP(bitrate, (1 << shift));
  1914. }
  1915. rate = cfg80211_calculate_bitrate(&ri);
  1916. /* rewind from end of MPDU */
  1917. if (status->flag & RX_FLAG_MACTIME_END)
  1918. ts -= mpdu_len * 8 * 10 / rate;
  1919. ts += mpdu_offset * 8 * 10 / rate;
  1920. return ts;
  1921. }
  1922. void ieee80211_dfs_cac_cancel(struct ieee80211_local *local)
  1923. {
  1924. struct ieee80211_sub_if_data *sdata;
  1925. mutex_lock(&local->iflist_mtx);
  1926. list_for_each_entry(sdata, &local->interfaces, list) {
  1927. cancel_delayed_work_sync(&sdata->dfs_cac_timer_work);
  1928. if (sdata->wdev.cac_started) {
  1929. ieee80211_vif_release_channel(sdata);
  1930. cfg80211_cac_event(sdata->dev,
  1931. NL80211_RADAR_CAC_ABORTED,
  1932. GFP_KERNEL);
  1933. }
  1934. }
  1935. mutex_unlock(&local->iflist_mtx);
  1936. }
  1937. void ieee80211_dfs_radar_detected_work(struct work_struct *work)
  1938. {
  1939. struct ieee80211_local *local =
  1940. container_of(work, struct ieee80211_local, radar_detected_work);
  1941. struct cfg80211_chan_def chandef;
  1942. ieee80211_dfs_cac_cancel(local);
  1943. if (local->use_chanctx)
  1944. /* currently not handled */
  1945. WARN_ON(1);
  1946. else {
  1947. chandef = local->hw.conf.chandef;
  1948. cfg80211_radar_event(local->hw.wiphy, &chandef, GFP_KERNEL);
  1949. }
  1950. }
  1951. void ieee80211_radar_detected(struct ieee80211_hw *hw)
  1952. {
  1953. struct ieee80211_local *local = hw_to_local(hw);
  1954. trace_api_radar_detected(local);
  1955. ieee80211_queue_work(hw, &local->radar_detected_work);
  1956. }
  1957. EXPORT_SYMBOL(ieee80211_radar_detected);