util.c 55 KB

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